2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "xfs_trans.h"
27 #include "xfs_dmapi.h"
28 #include "xfs_mount.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_alloc_btree.h"
31 #include "xfs_ialloc_btree.h"
32 #include "xfs_alloc.h"
33 #include "xfs_btree.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dir_sf.h"
36 #include "xfs_dir2_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_error.h"
41 #include "xfs_ioctl32.h"
43 #include <linux/dcache.h>
44 #include <linux/smp_lock.h>
46 static struct vm_operations_struct xfs_file_vm_ops;
47 #ifdef CONFIG_XFS_DMAPI
48 static struct vm_operations_struct xfs_dmapi_file_vm_ops;
59 struct iovec iov = {buf, count};
60 struct file *file = iocb->ki_filp;
61 vnode_t *vp = vn_from_inode(file->f_dentry->d_inode);
64 BUG_ON(iocb->ki_pos != pos);
66 if (unlikely(file->f_flags & O_DIRECT))
67 ioflags |= IO_ISDIRECT;
68 VOP_READ(vp, iocb, &iov, 1, &iocb->ki_pos, ioflags, NULL, rval);
79 return __xfs_file_read(iocb, buf, IO_ISAIO, count, pos);
83 xfs_file_aio_read_invis(
89 return __xfs_file_read(iocb, buf, IO_ISAIO|IO_INVIS, count, pos);
95 const char __user *buf,
100 struct iovec iov = {(void __user *)buf, count};
101 struct file *file = iocb->ki_filp;
102 struct inode *inode = file->f_mapping->host;
103 vnode_t *vp = vn_from_inode(inode);
106 BUG_ON(iocb->ki_pos != pos);
107 if (unlikely(file->f_flags & O_DIRECT))
108 ioflags |= IO_ISDIRECT;
110 VOP_WRITE(vp, iocb, &iov, 1, &iocb->ki_pos, ioflags, NULL, rval);
117 const char __user *buf,
121 return __xfs_file_write(iocb, buf, IO_ISAIO, count, pos);
125 xfs_file_aio_write_invis(
127 const char __user *buf,
131 return __xfs_file_write(iocb, buf, IO_ISAIO|IO_INVIS, count, pos);
134 STATIC inline ssize_t
137 const struct iovec *iov,
139 unsigned long nr_segs,
142 struct inode *inode = file->f_mapping->host;
143 vnode_t *vp = vn_from_inode(inode);
147 init_sync_kiocb(&kiocb, file);
148 kiocb.ki_pos = *ppos;
150 if (unlikely(file->f_flags & O_DIRECT))
151 ioflags |= IO_ISDIRECT;
152 VOP_READ(vp, &kiocb, iov, nr_segs, &kiocb.ki_pos, ioflags, NULL, rval);
154 *ppos = kiocb.ki_pos;
161 const struct iovec *iov,
162 unsigned long nr_segs,
165 return __xfs_file_readv(file, iov, 0, nr_segs, ppos);
169 xfs_file_readv_invis(
171 const struct iovec *iov,
172 unsigned long nr_segs,
175 return __xfs_file_readv(file, iov, IO_INVIS, nr_segs, ppos);
178 STATIC inline ssize_t
181 const struct iovec *iov,
183 unsigned long nr_segs,
186 struct inode *inode = file->f_mapping->host;
187 vnode_t *vp = vn_from_inode(inode);
191 init_sync_kiocb(&kiocb, file);
192 kiocb.ki_pos = *ppos;
193 if (unlikely(file->f_flags & O_DIRECT))
194 ioflags |= IO_ISDIRECT;
196 VOP_WRITE(vp, &kiocb, iov, nr_segs, &kiocb.ki_pos, ioflags, NULL, rval);
198 *ppos = kiocb.ki_pos;
205 const struct iovec *iov,
206 unsigned long nr_segs,
209 return __xfs_file_writev(file, iov, 0, nr_segs, ppos);
213 xfs_file_writev_invis(
215 const struct iovec *iov,
216 unsigned long nr_segs,
219 return __xfs_file_writev(file, iov, IO_INVIS, nr_segs, ppos);
230 vnode_t *vp = vn_from_inode(filp->f_dentry->d_inode);
233 VOP_SENDFILE(vp, filp, pos, 0, count, actor, target, NULL, rval);
238 xfs_file_sendfile_invis(
245 vnode_t *vp = vn_from_inode(filp->f_dentry->d_inode);
248 VOP_SENDFILE(vp, filp, pos, IO_INVIS, count, actor, target, NULL, rval);
253 xfs_file_splice_read(
256 struct pipe_inode_info *pipe,
260 vnode_t *vp = vn_from_inode(infilp->f_dentry->d_inode);
263 VOP_SPLICE_READ(vp, infilp, ppos, pipe, len, flags, 0, NULL, rval);
268 xfs_file_splice_read_invis(
271 struct pipe_inode_info *pipe,
275 vnode_t *vp = vn_from_inode(infilp->f_dentry->d_inode);
278 VOP_SPLICE_READ(vp, infilp, ppos, pipe, len, flags, IO_INVIS, NULL, rval);
283 xfs_file_splice_write(
284 struct pipe_inode_info *pipe,
285 struct file *outfilp,
290 vnode_t *vp = vn_from_inode(outfilp->f_dentry->d_inode);
293 VOP_SPLICE_WRITE(vp, pipe, outfilp, ppos, len, flags, 0, NULL, rval);
298 xfs_file_splice_write_invis(
299 struct pipe_inode_info *pipe,
300 struct file *outfilp,
305 vnode_t *vp = vn_from_inode(outfilp->f_dentry->d_inode);
308 VOP_SPLICE_WRITE(vp, pipe, outfilp, ppos, len, flags, IO_INVIS, NULL, rval);
317 vnode_t *vp = vn_from_inode(inode);
320 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
322 VOP_OPEN(vp, NULL, error);
330 vnode_t *vp = vn_from_inode(filp->f_dentry->d_inode);
333 VOP_CLOSE(vp, 0, file_count(filp) > 1 ? L_FALSE : L_TRUE, NULL, error);
342 vnode_t *vp = vn_from_inode(inode);
346 VOP_RELEASE(vp, error);
353 struct dentry *dentry,
356 struct inode *inode = dentry->d_inode;
357 vnode_t *vp = vn_from_inode(inode);
359 int flags = FSYNC_WAIT;
365 VOP_FSYNC(vp, flags, NULL, (xfs_off_t)0, (xfs_off_t)-1, error);
369 #ifdef CONFIG_XFS_DMAPI
372 struct vm_area_struct *area,
373 unsigned long address,
376 struct inode *inode = area->vm_file->f_dentry->d_inode;
377 vnode_t *vp = vn_from_inode(inode);
379 ASSERT_ALWAYS(vp->v_vfsp->vfs_flag & VFS_DMI);
380 if (XFS_SEND_MMAP(XFS_VFSTOM(vp->v_vfsp), area, 0))
382 return filemap_nopage(area, address, type);
384 #endif /* CONFIG_XFS_DMAPI */
393 vnode_t *vp = vn_from_inode(filp->f_dentry->d_inode);
398 int namelen, size = 0;
399 size_t rlen = PAGE_CACHE_SIZE;
400 xfs_off_t start_offset, curr_offset;
401 xfs_dirent_t *dbp = NULL;
403 /* Try fairly hard to get memory */
405 if ((read_buf = (caddr_t)kmalloc(rlen, GFP_KERNEL)))
408 } while (rlen >= 1024);
410 if (read_buf == NULL)
414 uio.uio_segflg = UIO_SYSSPACE;
415 curr_offset = filp->f_pos;
416 if (filp->f_pos != 0x7fffffff)
417 uio.uio_offset = filp->f_pos;
419 uio.uio_offset = 0xffffffff;
422 uio.uio_resid = iov.iov_len = rlen;
423 iov.iov_base = read_buf;
426 start_offset = uio.uio_offset;
428 VOP_READDIR(vp, &uio, NULL, &eof, error);
429 if ((uio.uio_offset == start_offset) || error) {
434 size = rlen - uio.uio_resid;
435 dbp = (xfs_dirent_t *)read_buf;
437 namelen = strlen(dbp->d_name);
439 if (filldir(dirent, dbp->d_name, namelen,
440 (loff_t) curr_offset & 0x7fffffff,
445 size -= dbp->d_reclen;
446 curr_offset = (loff_t)dbp->d_off /* & 0x7fffffff */;
447 dbp = (xfs_dirent_t *)((char *)dbp + dbp->d_reclen);
453 filp->f_pos = uio.uio_offset & 0x7fffffff;
455 filp->f_pos = curr_offset;
465 struct vm_area_struct *vma)
467 vma->vm_ops = &xfs_file_vm_ops;
469 #ifdef CONFIG_XFS_DMAPI
470 if (vn_from_inode(filp->f_dentry->d_inode)->v_vfsp->vfs_flag & VFS_DMI)
471 vma->vm_ops = &xfs_dmapi_file_vm_ops;
472 #endif /* CONFIG_XFS_DMAPI */
486 struct inode *inode = filp->f_dentry->d_inode;
487 vnode_t *vp = vn_from_inode(inode);
489 VOP_IOCTL(vp, inode, filp, 0, cmd, (void __user *)arg, error);
492 /* NOTE: some of the ioctl's return positive #'s as a
493 * byte count indicating success, such as
494 * readlink_by_handle. So we don't "sign flip"
495 * like most other routines. This means true
496 * errors need to be returned as a negative value.
502 xfs_file_ioctl_invis(
507 struct inode *inode = filp->f_dentry->d_inode;
508 vnode_t *vp = vn_from_inode(inode);
511 VOP_IOCTL(vp, inode, filp, IO_INVIS, cmd, (void __user *)arg, error);
514 /* NOTE: some of the ioctl's return positive #'s as a
515 * byte count indicating success, such as
516 * readlink_by_handle. So we don't "sign flip"
517 * like most other routines. This means true
518 * errors need to be returned as a negative value.
523 #ifdef CONFIG_XFS_DMAPI
524 #ifdef HAVE_VMOP_MPROTECT
527 struct vm_area_struct *vma,
528 unsigned int newflags)
530 vnode_t *vp = vn_from_inode(vma->vm_file->f_dentry->d_inode);
533 if (vp->v_vfsp->vfs_flag & VFS_DMI) {
534 if ((vma->vm_flags & VM_MAYSHARE) &&
535 (newflags & VM_WRITE) && !(vma->vm_flags & VM_WRITE)) {
536 xfs_mount_t *mp = XFS_VFSTOM(vp->v_vfsp);
538 error = XFS_SEND_MMAP(mp, vma, VM_WRITE);
543 #endif /* HAVE_VMOP_MPROTECT */
544 #endif /* CONFIG_XFS_DMAPI */
546 #ifdef HAVE_FOP_OPEN_EXEC
547 /* If the user is attempting to execute a file that is offline then
548 * we have to trigger a DMAPI READ event before the file is marked as busy
549 * otherwise the invisible I/O will not be able to write to the file to bring
556 vnode_t *vp = vn_from_inode(inode);
557 xfs_mount_t *mp = XFS_VFSTOM(vp->v_vfsp);
561 if (vp->v_vfsp->vfs_flag & VFS_DMI) {
567 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ)) {
568 error = -XFS_SEND_DATA(mp, DM_EVENT_READ, vp,
575 #endif /* HAVE_FOP_OPEN_EXEC */
577 const struct file_operations xfs_file_operations = {
578 .llseek = generic_file_llseek,
579 .read = do_sync_read,
580 .write = do_sync_write,
581 .readv = xfs_file_readv,
582 .writev = xfs_file_writev,
583 .aio_read = xfs_file_aio_read,
584 .aio_write = xfs_file_aio_write,
585 .sendfile = xfs_file_sendfile,
586 .splice_read = xfs_file_splice_read,
587 .splice_write = xfs_file_splice_write,
588 .unlocked_ioctl = xfs_file_ioctl,
590 .compat_ioctl = xfs_file_compat_ioctl,
592 .mmap = xfs_file_mmap,
593 .open = xfs_file_open,
594 .flush = xfs_file_close,
595 .release = xfs_file_release,
596 .fsync = xfs_file_fsync,
597 #ifdef HAVE_FOP_OPEN_EXEC
598 .open_exec = xfs_file_open_exec,
602 const struct file_operations xfs_invis_file_operations = {
603 .llseek = generic_file_llseek,
604 .read = do_sync_read,
605 .write = do_sync_write,
606 .readv = xfs_file_readv_invis,
607 .writev = xfs_file_writev_invis,
608 .aio_read = xfs_file_aio_read_invis,
609 .aio_write = xfs_file_aio_write_invis,
610 .sendfile = xfs_file_sendfile_invis,
611 .splice_read = xfs_file_splice_read_invis,
612 .splice_write = xfs_file_splice_write_invis,
613 .unlocked_ioctl = xfs_file_ioctl_invis,
615 .compat_ioctl = xfs_file_compat_invis_ioctl,
617 .mmap = xfs_file_mmap,
618 .open = xfs_file_open,
619 .flush = xfs_file_close,
620 .release = xfs_file_release,
621 .fsync = xfs_file_fsync,
625 const struct file_operations xfs_dir_file_operations = {
626 .read = generic_read_dir,
627 .readdir = xfs_file_readdir,
628 .unlocked_ioctl = xfs_file_ioctl,
630 .compat_ioctl = xfs_file_compat_ioctl,
632 .fsync = xfs_file_fsync,
635 static struct vm_operations_struct xfs_file_vm_ops = {
636 .nopage = filemap_nopage,
637 .populate = filemap_populate,
640 #ifdef CONFIG_XFS_DMAPI
641 static struct vm_operations_struct xfs_dmapi_file_vm_ops = {
642 .nopage = xfs_vm_nopage,
643 .populate = filemap_populate,
644 #ifdef HAVE_VMOP_MPROTECT
645 .mprotect = xfs_vm_mprotect,
648 #endif /* CONFIG_XFS_DMAPI */