[BACK]Return to xfs_file.c CVS log [TXT][DIR] Up to [Development] / xfs-linux / linux-2.4

File: [Development] / xfs-linux / linux-2.4 / Attic / xfs_file.c (download)

Revision 1.127, Thu Jun 15 03:54:32 2006 UTC (11 years, 4 months ago) by nathans.longdrop.melbourne.sgi.com
Branch: MAIN
Changes since 1.126: +9 -14 lines

Remove unnecessary local from open_exec dmapi path.
Merge of xfs-linux-melb:xfs-kern:26247a by kenmcd.

/*
 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
 * All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
#include "xfs.h"
#include "xfs_bit.h"
#include "xfs_log.h"
#include "xfs_inum.h"
#include "xfs_sb.h"
#include "xfs_ag.h"
#include "xfs_dir.h"
#include "xfs_dir2.h"
#include "xfs_trans.h"
#include "xfs_dmapi.h"
#include "xfs_mount.h"
#include "xfs_bmap_btree.h"
#include "xfs_alloc_btree.h"
#include "xfs_ialloc_btree.h"
#include "xfs_alloc.h"
#include "xfs_btree.h"
#include "xfs_attr_sf.h"
#include "xfs_dir_sf.h"
#include "xfs_dir2_sf.h"
#include "xfs_dinode.h"
#include "xfs_inode.h"
#include "xfs_error.h"
#include "xfs_rw.h"
#include <linux/iobuf.h>

#include <linux/dcache.h>
#include <linux/smp_lock.h>
#include <linux/mman.h> /* for PROT_WRITE */

static struct vm_operations_struct xfs_file_vm_ops;
#ifdef CONFIG_XFS_DMAPI
static struct vm_operations_struct xfs_dmapi_file_vm_ops;
#endif

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,22)
#define	do_down_read(x)	down_read(x)
#define do_up_read(x)	up_read(x)
#else
#define	do_down_read(x)
#define do_up_read(x)
#endif

STATIC inline ssize_t
__xfs_file_read(
	struct file	*file,
	char		*buf,
	int		ioflags,
	size_t		size,
	loff_t		*offset)
{
	struct inode	*inode = file->f_dentry->d_inode;
	bhv_vnode_t	*vp = vn_from_inode(inode);
	ssize_t		rval;

	if (unlikely(file->f_flags & O_DIRECT)) {
		ioflags |= IO_ISDIRECT;
		do_down_read(&inode->i_alloc_sem);
		rval = bhv_vop_read(vp, file, buf, size, offset, ioflags, NULL);
		do_up_read(&inode->i_alloc_sem);
	} else {
		rval = bhv_vop_read(vp, file, buf, size, offset, ioflags, NULL);
	}
	return rval;
}

STATIC ssize_t
xfs_file_read(
	struct file	*file,
	char		*buf,
	size_t		size,
	loff_t		*offset)
{
	return __xfs_file_read(file, buf, 0, size, offset);
}

STATIC ssize_t
xfs_file_read_invis(
	struct file	*file,
	char		*buf,
	size_t		size,
	loff_t		*offset)
{
	return __xfs_file_read(file, buf, IO_INVIS, size, offset);
}


STATIC inline ssize_t
__xfs_file_write(
	struct file	*file,
	const char	*buf,
	int		ioflags,
	size_t		count,
	loff_t		*ppos)
{
	struct inode	*inode = file->f_dentry->d_inode;
	bhv_vnode_t	*vp = vn_from_inode(inode);
	loff_t		pos;
	ssize_t		rval;	/* Use negative errors in this f'n */

	if ((ssize_t) count < 0)
		return -EINVAL;

	if (!access_ok(VERIFY_READ, buf, count))
		return -EFAULT;

	pos = *ppos;
	if (pos < 0)
		return -EINVAL;

	rval = file->f_error;
	if (rval) {
		file->f_error = 0;
		return rval;
	}

	/* We allow multiple direct writers in, there is no
	 * potential call to vmtruncate in that path.
	 */
	if (unlikely(file->f_flags & O_DIRECT)) {
		ioflags |= IO_ISDIRECT;
		do_down_read(&inode->i_alloc_sem);
		rval = bhv_vop_write(vp, file, buf, count, &pos, ioflags, NULL);
		*ppos = pos;
		do_up_read(&inode->i_alloc_sem);
	} else {
		down(&inode->i_sem);
		rval = bhv_vop_write(vp, file, buf, count, &pos, ioflags, NULL);
		*ppos = pos;
		up(&inode->i_sem);
	}
	return rval;
}

STATIC inline ssize_t
xfs_file_write(
	struct file	*file,
	const char	*buf,
	size_t		count,
	loff_t		*ppos)
{
	return __xfs_file_write(file, buf, 0, count, ppos);
}

STATIC inline ssize_t
xfs_file_write_invis(
	struct file	*file,
	const char	*buf,
	size_t		count,
	loff_t		*ppos)
{
	return __xfs_file_write(file, buf, IO_INVIS, count, ppos);
}

STATIC int
xfs_file_open(
	struct inode	*inode,
	struct file	*filp)
{
	if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
		return -EFBIG;
	return -bhv_vop_open(vn_from_inode(inode), NULL);
}

STATIC int
xfs_file_close(
	struct file	*filp)
{
	return -bhv_vop_close(vn_from_inode(filp->f_dentry->d_inode), 0,
				file_count(filp) > 1 ? L_FALSE : L_TRUE, NULL);
}

STATIC int
xfs_file_release(
	struct inode	*inode,
	struct file	*filp)
{
	bhv_vnode_t	*vp = vn_from_inode(inode);

	if (vp)
		return -bhv_vop_release(vp);
	return 0;
}

STATIC int
xfs_file_fsync(
	struct file	*filp,
	struct dentry	*dentry,
	int		datasync)
{
	struct inode	*inode = dentry->d_inode;
	bhv_vnode_t	*vp = vn_from_inode(inode);
	int		flags = FSYNC_WAIT;
	int		error;

	error = fsync_inode_data_buffers(inode);
	if (error)
		return error;
	if (datasync)
		flags |= FSYNC_DATA;
	if (VN_TRUNC(vp))
		VUNTRUNCATE(vp);
	return -bhv_vop_fsync(vp, flags, NULL, (xfs_off_t)0, (xfs_off_t)-1);
}

STATIC int
xfs_file_readdir(
	struct file	*filp,
	void		*dirent,
	filldir_t	filldir)
{
	int		error = 0;
	bhv_vnode_t	*vp;
	uio_t		uio;
	iovec_t		iov;
	int		eof = 0;
	caddr_t		read_buf;
	int		namelen, size = 0;
	size_t		rlen = PAGE_CACHE_SIZE;
	xfs_off_t	start_offset, curr_offset;
	xfs_dirent_t	*dbp = NULL;

	vp = vn_from_inode(filp->f_dentry->d_inode);
	ASSERT(vp);

	/* Try fairly hard to get memory */
	do {
		if ((read_buf = (caddr_t)kmalloc(rlen, GFP_KERNEL)))
			break;
		rlen >>= 1;
	} while (rlen >= 1024);

	if (read_buf == NULL)
		return -ENOMEM;

	uio.uio_iov = &iov;
	uio.uio_segflg = UIO_SYSSPACE;
	curr_offset = filp->f_pos;
	if (filp->f_pos != 0x7fffffff)
		uio.uio_offset = filp->f_pos;
	else
		uio.uio_offset = 0xffffffff;

	while (!eof) {
		uio.uio_resid = iov.iov_len = rlen;
		iov.iov_base = read_buf;
		uio.uio_iovcnt = 1;

		start_offset = uio.uio_offset;

		error = bhv_vop_readdir(vp, &uio, NULL, &eof);
		if ((uio.uio_offset == start_offset) || error) {
			size = 0;
			break;
		}

		size = rlen - uio.uio_resid;
		dbp = (xfs_dirent_t *)read_buf;
		while (size > 0) {
			namelen = strlen(dbp->d_name);

			if (filldir(dirent, dbp->d_name, namelen,
					(loff_t) curr_offset & 0x7fffffff,
					(ino_t) dbp->d_ino,
					DT_UNKNOWN)) {
				goto done;
			}
			size -= dbp->d_reclen;
			curr_offset = (loff_t)dbp->d_off /* & 0x7fffffff */;
			dbp = (xfs_dirent_t *)((char *)dbp + dbp->d_reclen);
		}
	}
done:
	if (!error) {
		if (size == 0)
			filp->f_pos = uio.uio_offset & 0x7fffffff;
		else if (dbp)
			filp->f_pos = curr_offset;
	}

	kfree(read_buf);
	return -error;
}

#ifdef CONFIG_XFS_DMAPI

STATIC struct page *
xfs_vm_nopage(
	struct vm_area_struct	*area,
	unsigned long		address,
	int			unused)
{
	struct inode	*inode = area->vm_file->f_dentry->d_inode;
	bhv_vnode_t	*vp = vn_from_inode(inode);

	ASSERT_ALWAYS(vp->v_vfsp->vfs_flag & VFS_DMI);
	if (XFS_SEND_MMAP(XFS_VFSTOM(vp->v_vfsp), area, 0))
		return NULL;
	return filemap_nopage(area, address, unused);
}

#endif /* CONFIG_XFS_DMAPI */


STATIC int
xfs_file_mmap(
	struct file	*filp,
	struct vm_area_struct *vma)
{
	struct inode	*ip = filp->f_dentry->d_inode;

	vma->vm_ops = &xfs_file_vm_ops;

#ifdef CONFIG_XFS_DMAPI
	if (vn_from_inode(ip)->v_vfsp->vfs_flag & VFS_DMI)
		vma->vm_ops = &xfs_dmapi_file_vm_ops;
#endif /* CONFIG_XFS_DMAPI */

	UPDATE_ATIME(ip);
	return 0;
}


STATIC int
xfs_file_ioctl(
	struct inode	*inode,
	struct file	*filp,
	unsigned int	cmd,
	unsigned long	arg)
{
	bhv_vnode_t	*vp = vn_from_inode(inode);
	int		error;

	unlock_kernel();
	error = bhv_vop_ioctl(vp, inode, filp, 0, cmd, arg);
	VMODIFY(vp);
	lock_kernel();

	/* NOTE:  some of the ioctl's return positive #'s as a
	 *	  byte count indicating success, such as
	 *	  readlink_by_handle.  So we don't "sign flip"
	 *	  like most other routines.  This means true
	 *	  errors need to be returned as a negative value.
	 */
	return error;
}

STATIC int
xfs_file_ioctl_invis(
	struct inode	*inode,
	struct file	*filp,
	unsigned int	cmd,
	unsigned long	arg)
{
	bhv_vnode_t	*vp = vn_from_inode(inode);
	int		error;

	unlock_kernel();
	error = bhv_vop_ioctl(vp, inode, filp, IO_INVIS, cmd, arg);
	VMODIFY(vp);
	lock_kernel();

	/* NOTE:  some of the ioctl's return positive #'s as a
	 *	  byte count indicating success, such as
	 *	  readlink_by_handle.  So we don't "sign flip"
	 *	  like most other routines.  This means true
	 *	  errors need to be returned as a negative value.
	 */
	return error;
}

#ifdef CONFIG_XFS_DMAPI
#ifdef HAVE_VMOP_MPROTECT
STATIC int
xfs_vm_mprotect(
	struct vm_area_struct *vma,
	unsigned int	newflags)
{
	bhv_vnode_t	*vp = vn_from_inode(vma->vm_file->f_dentry->d_inode);
	int		error = 0;

	if (vp->v_vfsp->vfs_flag & VFS_DMI) {
		if ((vma->vm_flags & VM_MAYSHARE) &&
		    (newflags & PROT_WRITE) && !(vma->vm_flags & PROT_WRITE)) {
			xfs_mount_t	*mp = XFS_VFSTOM(vp->v_vfsp);

			error = XFS_SEND_MMAP(mp, vma, VM_WRITE);
		    }
	}
	return error;
}
#endif /* HAVE_VMOP_MPROTECT */
#endif /* CONFIG_XFS_DMAPI */


#ifdef HAVE_FOP_OPEN_EXEC
/* If the user is attempting to execute a file that is offline then
 * we have to trigger a DMAPI READ event before the file is marked as busy
 * otherwise the invisible I/O will not be able to write to the file to bring
 * it back online.
 */
STATIC int
xfs_file_open_exec(
	struct inode	*inode)
{
	bhv_vnode_t	*vp = vn_from_inode(inode);

	if (unlikely(vp->v_vfsp->vfs_flag & VFS_DMI)) {
		xfs_mount_t	*mp = XFS_VFSTOM(vp->v_vfsp);
		xfs_inode_t	*ip = xfs_vtoi(vp);

		if (!ip)
			return -EINVAL;
		if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ))
			return -XFS_SEND_DATA(mp, DM_EVENT_READ, vp,
					       0, 0, 0, NULL);
	}
	return 0;
}
#endif /* HAVE_FOP_OPEN_EXEC */

struct file_operations xfs_file_operations = {
	.llseek		= generic_file_llseek,
	.read		= xfs_file_read,
	.write		= xfs_file_write,
	.ioctl		= xfs_file_ioctl,
	.mmap		= xfs_file_mmap,
	.open		= xfs_file_open,
	.flush		= xfs_file_close,
	.release	= xfs_file_release,
	.fsync		= xfs_file_fsync,
#ifdef HAVE_FOP_OPEN_EXEC
	.open_exec	= xfs_file_open_exec,
#endif
};

struct file_operations xfs_invis_file_operations = {
	.llseek		= generic_file_llseek,
	.read		= xfs_file_read_invis,
	.write		= xfs_file_write_invis,
	.ioctl		= xfs_file_ioctl_invis,
	.mmap		= xfs_file_mmap,
	.open		= xfs_file_open,
	.flush		= xfs_file_close,
	.release	= xfs_file_release,
	.fsync		= xfs_file_fsync,
};


struct file_operations xfs_dir_file_operations = {
	.read		= generic_read_dir,
	.readdir	= xfs_file_readdir,
	.ioctl		= xfs_file_ioctl,
	.fsync		= xfs_file_fsync,
};

static struct vm_operations_struct xfs_file_vm_ops = {
	.nopage		= filemap_nopage,
};

#ifdef CONFIG_XFS_DMAPI
static struct vm_operations_struct xfs_dmapi_file_vm_ops = {
	.nopage		= xfs_vm_nopage,
#ifdef HAVE_VMOP_MPROTECT
	.mprotect	= xfs_vm_mprotect,
#endif
};
#endif /* CONFIG_XFS_DMAPI */