xfs_file.c 11.2 KB
Newer Older
1
/*
2
 * Copyright (c) 2000-2004 Silicon Graphics, Inc.  All Rights Reserved.
3 4 5 6 7 8 9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 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.
 *
 * Further, this software is distributed without any warranty that it is
 * free of the rightful claim of any third person regarding infringement
Russell Cattelan's avatar
Russell Cattelan committed
14
 * or the like.  Any license provided herein, whether implied or
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 * otherwise, applies only to this software file.  Patent licenses, if
 * any, provided herein do not apply to combinations of this program with
 * other software, or any other product whatsoever.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write the Free Software Foundation, Inc., 59
 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
 *
 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
 * Mountain View, CA  94043, or:
 *
 * http://www.sgi.com
 *
 * For further information regarding this notice, see:
 *
 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
 */

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include "xfs.h"
#include "xfs_inum.h"
#include "xfs_log.h"
#include "xfs_sb.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"

55
#include <linux/dcache.h>
56
#include <linux/smp_lock.h>
57 58 59 60

static struct vm_operations_struct linvfs_file_vm_ops;


61 62
STATIC inline ssize_t
__linvfs_read(
63
	struct kiocb		*iocb,
64
	char			__user *buf,
65
	int			ioflags,
66 67
	size_t			count,
	loff_t			pos)
68
{
69
	struct iovec		iov = {buf, count};
70
	struct file		*file = iocb->ki_filp;
71 72
	vnode_t			*vp = LINVFS_GET_VP(file->f_dentry->d_inode);
	ssize_t			rval;
73

74
	BUG_ON(iocb->ki_pos != pos);
75

76 77
	if (unlikely(file->f_flags & O_DIRECT))
		ioflags |= IO_ISDIRECT;
78 79
	VOP_READ(vp, iocb, &iov, 1, &iocb->ki_pos, ioflags, NULL, rval);
	return rval;
80 81 82 83
}


STATIC ssize_t
84 85
linvfs_read(
	struct kiocb		*iocb,
86
	char			__user *buf,
87 88 89 90 91 92 93 94 95
	size_t			count,
	loff_t			pos)
{
	return __linvfs_read(iocb, buf, 0, count, pos);
}

STATIC ssize_t
linvfs_read_invis(
	struct kiocb		*iocb,
96
	char			__user *buf,
97 98 99 100 101 102 103 104 105
	size_t			count,
	loff_t			pos)
{
	return __linvfs_read(iocb, buf, IO_INVIS, count, pos);
}


STATIC inline ssize_t
__linvfs_write(
106
	struct kiocb	*iocb,
107
	const char	__user *buf,
108
	int		ioflags,
109 110
	size_t		count,
	loff_t		pos)
111
{
112
	struct iovec	iov = {(void __user *)buf, count};
113
	struct file	*file = iocb->ki_filp;
114
	struct inode	*inode = file->f_mapping->host;
115
	vnode_t		*vp = LINVFS_GET_VP(inode);
116
	ssize_t		rval;
117

118
	BUG_ON(iocb->ki_pos != pos);
119
	if (unlikely(file->f_flags & O_DIRECT))
120
		ioflags |= IO_ISDIRECT;
121

122
	VOP_WRITE(vp, iocb, &iov, 1, &iocb->ki_pos, ioflags, NULL, rval);
123
	return rval;
124
}
125

126

127
STATIC ssize_t
128 129
linvfs_write(
	struct kiocb		*iocb,
130
	const char		__user *buf,
131 132 133 134 135 136 137 138 139
	size_t			count,
	loff_t			pos)
{
	return __linvfs_write(iocb, buf, 0, count, pos);
}

STATIC ssize_t
linvfs_write_invis(
	struct kiocb		*iocb,
140
	const char		__user *buf,
141 142 143 144 145 146 147 148 149
	size_t			count,
	loff_t			pos)
{
	return __linvfs_write(iocb, buf, IO_INVIS, count, pos);
}


STATIC inline ssize_t
__linvfs_readv(
150 151
	struct file		*file,
	const struct iovec 	*iov,
152
	int			ioflags,
153
	unsigned long		nr_segs,
154 155
	loff_t			*ppos)
{
156
	struct inode	*inode = file->f_mapping->host;
157 158
	vnode_t		*vp = LINVFS_GET_VP(inode);
	struct		kiocb kiocb;
159
	ssize_t		rval;
160

161 162
	init_sync_kiocb(&kiocb, file);
	kiocb.ki_pos = *ppos;
163 164 165

	if (unlikely(file->f_flags & O_DIRECT))
		ioflags |= IO_ISDIRECT;
166
	VOP_READ(vp, &kiocb, iov, nr_segs, &kiocb.ki_pos, ioflags, NULL, rval);
167

168 169
	*ppos = kiocb.ki_pos;
	return rval;
170
}
171 172

STATIC ssize_t
173
linvfs_readv(
174
	struct file		*file,
175 176
	const struct iovec 	*iov,
	unsigned long		nr_segs,
177
	loff_t			*ppos)
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
{
	return __linvfs_readv(file, iov, 0, nr_segs, ppos);
}

STATIC ssize_t
linvfs_readv_invis(
	struct file		*file,
	const struct iovec 	*iov,
	unsigned long		nr_segs,
	loff_t			*ppos)
{
	return __linvfs_readv(file, iov, IO_INVIS, nr_segs, ppos);
}


STATIC inline ssize_t
__linvfs_writev(
	struct file		*file,
	const struct iovec 	*iov,
	int			ioflags,
	unsigned long		nr_segs,
	loff_t			*ppos)
200
{
201
	struct inode	*inode = file->f_mapping->host;
202 203
	vnode_t		*vp = LINVFS_GET_VP(inode);
	struct		kiocb kiocb;
204
	ssize_t		rval;
205

206 207
	init_sync_kiocb(&kiocb, file);
	kiocb.ki_pos = *ppos;
208
	if (unlikely(file->f_flags & O_DIRECT))
209
		ioflags |= IO_ISDIRECT;
210

211
	VOP_WRITE(vp, &kiocb, iov, nr_segs, &kiocb.ki_pos, ioflags, NULL, rval);
212 213 214

	*ppos = kiocb.ki_pos;
	return rval;
215 216
}

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

STATIC ssize_t
linvfs_writev(
	struct file		*file,
	const struct iovec 	*iov,
	unsigned long		nr_segs,
	loff_t			*ppos)
{
	return __linvfs_writev(file, iov, 0, nr_segs, ppos);
}

STATIC ssize_t
linvfs_writev_invis(
	struct file		*file,
	const struct iovec 	*iov,
	unsigned long		nr_segs,
	loff_t			*ppos)
{
	return __linvfs_writev(file, iov, IO_INVIS, nr_segs, ppos);
}

238 239 240 241 242 243
STATIC ssize_t
linvfs_sendfile(
	struct file		*filp,
	loff_t			*ppos,
	size_t			count,
	read_actor_t		actor,
244
	void			*target)
245 246
{
	vnode_t			*vp = LINVFS_GET_VP(filp->f_dentry->d_inode);
247
	ssize_t			rval;
248

249 250
	VOP_SENDFILE(vp, filp, ppos, 0, count, actor, target, NULL, rval);
	return rval;
251 252
}

253

254 255 256 257 258 259 260 261
STATIC int
linvfs_open(
	struct inode	*inode,
	struct file	*filp)
{
	vnode_t		*vp = LINVFS_GET_VP(inode);
	int		error;

262
	if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
		return -EFBIG;

	ASSERT(vp);
	VOP_OPEN(vp, NULL, error);
	return -error;
}


STATIC int
linvfs_release(
	struct inode	*inode,
	struct file	*filp)
{
	vnode_t		*vp = LINVFS_GET_VP(inode);
	int		error = 0;

	if (vp)
		VOP_RELEASE(vp, error);
	return -error;
}


STATIC int
linvfs_fsync(
	struct file	*filp,
	struct dentry	*dentry,
	int		datasync)
{
	struct inode	*inode = dentry->d_inode;
	vnode_t		*vp = LINVFS_GET_VP(inode);
	int		error;
	int		flags = FSYNC_WAIT;

	if (datasync)
		flags |= FSYNC_DATA;

	ASSERT(vp);
300
	VOP_FSYNC(vp, flags, NULL, (xfs_off_t)0, (xfs_off_t)-1, error);
301 302 303 304 305 306 307 308
	return -error;
}

/*
 * linvfs_readdir maps to VOP_READDIR().
 * We need to build a uio, cred, ...
 */

Russell Cattelan's avatar
Russell Cattelan committed
309
#define nextdp(dp)      ((struct xfs_dirent *)((char *)(dp) + (dp)->d_reclen))
310 311 312 313 314 315 316 317 318 319 320 321 322 323

STATIC int
linvfs_readdir(
	struct file	*filp,
	void		*dirent,
	filldir_t	filldir)
{
	int		error = 0;
	vnode_t		*vp;
	uio_t		uio;
	iovec_t		iov;
	int		eof = 0;
	caddr_t		read_buf;
	int		namelen, size = 0;
324 325
	size_t		rlen = PAGE_CACHE_SIZE;
	xfs_off_t	start_offset, curr_offset;
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	xfs_dirent_t	*dbp = NULL;

	vp = LINVFS_GET_VP(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;
343 344 345 346 347
	curr_offset = filp->f_pos;
	if (filp->f_pos != 0x7fffffff)
		uio.uio_offset = filp->f_pos;
	else
		uio.uio_offset = 0xffffffff;
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367

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

		start_offset = uio.uio_offset;

		VOP_READDIR(vp, &uio, NULL, &eof, error);
		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,
368
					(loff_t) curr_offset & 0x7fffffff,
369 370 371 372 373
					(ino_t) dbp->d_ino,
					DT_UNKNOWN)) {
				goto done;
			}
			size -= dbp->d_reclen;
374
			curr_offset = (loff_t)dbp->d_off /* & 0x7fffffff */;
375 376 377 378 379 380
			dbp = nextdp(dbp);
		}
	}
done:
	if (!error) {
		if (size == 0)
381
			filp->f_pos = uio.uio_offset & 0x7fffffff;
382
		else if (dbp)
383
			filp->f_pos = curr_offset;
384 385 386 387 388 389
	}

	kfree(read_buf);
	return -error;
}

390

391 392 393 394 395 396 397
STATIC int
linvfs_file_mmap(
	struct file	*filp,
	struct vm_area_struct *vma)
{
	struct inode	*ip = filp->f_dentry->d_inode;
	vnode_t		*vp = LINVFS_GET_VP(ip);
398
	vattr_t		va = { .va_mask = XFS_AT_UPDATIME };
399 400 401
	int		error;

	if ((vp->v_type == VREG) && (vp->v_vfsp->vfs_flag & VFS_DMI)) {
402 403 404
		xfs_mount_t	*mp = XFS_VFSTOM(vp->v_vfsp);

		error = -XFS_SEND_MMAP(mp, vma, 0);
405 406 407 408 409 410
		if (error)
			return error;
	}

	vma->vm_ops = &linvfs_file_vm_ops;

411
	VOP_SETATTR(vp, &va, XFS_AT_UPDATIME, NULL, error);
412 413 414 415 416 417 418 419 420 421 422 423 424 425
	return 0;
}


STATIC int
linvfs_ioctl(
	struct inode	*inode,
	struct file	*filp,
	unsigned int	cmd,
	unsigned long	arg)
{
	int		error;
	vnode_t		*vp = LINVFS_GET_VP(inode);

426
	unlock_kernel();
427
	VOP_IOCTL(vp, inode, filp, 0, cmd, (void __user *)arg, error);
428
	VMODIFY(vp);
429
	lock_kernel();
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449

	/* 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
linvfs_ioctl_invis(
	struct inode	*inode,
	struct file	*filp,
	unsigned int	cmd,
	unsigned long	arg)
{
	int		error;
	vnode_t		*vp = LINVFS_GET_VP(inode);

450
	unlock_kernel();
451
	ASSERT(vp);
452
	VOP_IOCTL(vp, inode, filp, IO_INVIS, cmd, (void __user *)arg, error);
453
	VMODIFY(vp);
454
	lock_kernel();
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475

	/* 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 HAVE_VMOP_MPROTECT
STATIC int
linvfs_mprotect(
	struct vm_area_struct *vma,
	unsigned int	newflags)
{
	vnode_t		*vp = LINVFS_GET_VP(vma->vm_file->f_dentry->d_inode);
	int		error = 0;

	if ((vp->v_type == VREG) && (vp->v_vfsp->vfs_flag & VFS_DMI)) {
		if ((vma->vm_flags & VM_MAYSHARE) &&
476
		    (newflags & VM_WRITE) && !(vma->vm_flags & VM_WRITE)) {
477 478 479
			xfs_mount_t	*mp = XFS_VFSTOM(vp->v_vfsp);

			error = XFS_SEND_MMAP(mp, vma, VM_WRITE);
480 481 482 483 484 485 486 487 488
		    }
	}
	return error;
}
#endif /* HAVE_VMOP_MPROTECT */


struct file_operations linvfs_file_operations = {
	.llseek		= generic_file_llseek,
489 490
	.read		= do_sync_read,
	.write		= do_sync_write,
491 492
	.readv		= linvfs_readv,
	.writev		= linvfs_writev,
493 494
	.aio_read	= linvfs_read,
	.aio_write	= linvfs_write,
495
	.sendfile	= linvfs_sendfile,
496 497 498 499 500 501 502
	.ioctl		= linvfs_ioctl,
	.mmap		= linvfs_file_mmap,
	.open		= linvfs_open,
	.release	= linvfs_release,
	.fsync		= linvfs_fsync,
};

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
struct file_operations linvfs_invis_file_operations = {
	.llseek		= generic_file_llseek,
	.read		= do_sync_read,
	.write		= do_sync_write,
	.readv		= linvfs_readv_invis,
	.writev		= linvfs_writev_invis,
	.aio_read	= linvfs_read_invis,
	.aio_write	= linvfs_write_invis,
	.sendfile	= linvfs_sendfile,
	.ioctl		= linvfs_ioctl_invis,
	.mmap		= linvfs_file_mmap,
	.open		= linvfs_open,
	.release	= linvfs_release,
	.fsync		= linvfs_fsync,
};


520 521 522 523 524 525 526 527 528 529 530 531 532
struct file_operations linvfs_dir_operations = {
	.read		= generic_read_dir,
	.readdir	= linvfs_readdir,
	.ioctl		= linvfs_ioctl,
	.fsync		= linvfs_fsync,
};

static struct vm_operations_struct linvfs_file_vm_ops = {
	.nopage		= filemap_nopage,
#ifdef HAVE_VMOP_MPROTECT
	.mprotect	= linvfs_mprotect,
#endif
};