/*
* Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
* Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU General Public License v.2.
*/
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/completion.h>
#include <linux/buffer_head.h>
#include <linux/pagemap.h>
#include <linux/uio.h>
#include <linux/blkdev.h>
#include <linux/mm.h>
#include <linux/smp_lock.h>
#include <linux/gfs2_ioctl.h>
#include <linux/fs.h>
#include <asm/semaphore.h>
#include <asm/uaccess.h>
#include "gfs2.h"
#include "bmap.h"
#include "dir.h"
#include "glock.h"
#include "glops.h"
#include "inode.h"
#include "lm.h"
#include "log.h"
#include "meta_io.h"
#include "ops_file.h"
#include "ops_vm.h"
#include "quota.h"
#include "rgrp.h"
#include "trans.h"
/* "bad" is for NFS support */
struct filldir_bad_entry {
char *fbe_name;
unsigned int fbe_length;
uint64_t fbe_offset;
struct gfs2_inum fbe_inum;
unsigned int fbe_type;
};
struct filldir_bad {
struct gfs2_sbd *fdb_sbd;
struct filldir_bad_entry *fdb_entry;
unsigned int fdb_entry_num;
unsigned int fdb_entry_off;
char *fdb_name;
unsigned int fdb_name_size;
unsigned int fdb_name_off;
};
/* For regular, non-NFS */
struct filldir_reg {
struct gfs2_sbd *fdr_sbd;
int fdr_prefetch;
filldir_t fdr_filldir;
void *fdr_opaque;
};
static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
unsigned long offset, unsigned long size)
{
char *kaddr;
unsigned long count = desc->count;
if (size > count)
size = count;
kaddr = kmap(page);
memcpy(desc->arg.buf, kaddr + offset, size);
kunmap(page);
desc->count = count - size;
desc->written += size;
desc->arg.buf += size;
return size;
}
int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
char *buf, loff_t *pos, unsigned size)
{
struct inode *inode = ip->i_vnode;
read_descriptor_t desc;
desc.written = 0;
desc.arg.buf = buf;
desc.count = size;
desc.error = 0;
do_generic_mapping_read(inode->i_mapping, ra_state, NULL, pos, &desc, gfs2_read_actor);
return desc.written ? desc.written : desc.error;
}
/**
* gfs2_llseek - seek to a location in a file
* @file: the file
* @offset: the offset
* @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
*
* SEEK_END requires the glock for the file because it references the
* file's size.
*
* Returns: The new offset, or errno
*/
static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
{
struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
struct gfs2_holder i_gh;
loff_t error;
atomic_inc(&ip->i_sbd->sd_ops_file);
if (origin == 2) {
error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
&i_gh);
if (!error) {
error = remote_llseek(file, offset, origin);
gfs2_glock_dq_uninit(&i_gh);
}
} else
error = remote_llseek(file, offset, origin);
return error;
}
static ssize_t gfs2_direct_IO_read(struct kiocb *iocb, const struct iovec *iov,
loff_t offset, unsigned long nr_segs)
{
struct file *file = iocb->ki_filp;
struct address_space *mapping = file->f_mapping;
ssize_t retval;
retval = filemap_write_and_wait(mapping);
if (retval == 0) {
retval = mapping->a_ops->direct_IO(READ, iocb, iov, offset,
nr_segs);
}
return retval;
}
/**
* __gfs2_file_aio_read - The main GFS2 read function
*
* N.B. This is almost, but not quite the same as __generic_file_aio_read()
* the important subtle different being that inode->i_size isn't valid
* unless we are holding a lock, and we do this _only_ on the O_DIRECT
* path since otherwise locking is done entirely at the page cache
* layer.
*/
static ssize_t __gfs2_file_aio_read(struct kiocb *iocb,
const struct iovec *iov,
unsigned long nr_segs, loff_t *ppos)
{
struct file *filp = iocb->ki_filp;
struct gfs2_inode *ip = get_v2ip(filp->f_mapping->host);
struct gfs2_holder gh;
ssize_t retval;
unsigned long seg;