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
);
331 vnode_t
*vp
= vn_from_inode(inode
);
335 VOP_RELEASE(vp
, error
);
342 struct dentry
*dentry
,
345 struct inode
*inode
= dentry
->d_inode
;
346 vnode_t
*vp
= vn_from_inode(inode
);
348 int flags
= FSYNC_WAIT
;
352 VOP_FSYNC(vp
, flags
, NULL
, (xfs_off_t
)0, (xfs_off_t
)-1, error
);
356 #ifdef CONFIG_XFS_DMAPI
359 struct vm_area_struct
*area
,
360 unsigned long address
,
363 struct inode
*inode
= area
->vm_file
->f_dentry
->d_inode
;
364 vnode_t
*vp
= vn_from_inode(inode
);
365 xfs_mount_t
*mp
= XFS_VFSTOM(vp
->v_vfsp
);
368 ASSERT_ALWAYS(vp
->v_vfsp
->vfs_flag
& VFS_DMI
);
370 error
= XFS_SEND_MMAP(mp
, area
, 0);
374 return filemap_nopage(area
, address
, type
);
376 #endif /* CONFIG_XFS_DMAPI */
385 vnode_t
*vp
= vn_from_inode(filp
->f_dentry
->d_inode
);
390 int namelen
, size
= 0;
391 size_t rlen
= PAGE_CACHE_SIZE
;
392 xfs_off_t start_offset
, curr_offset
;
393 xfs_dirent_t
*dbp
= NULL
;
395 /* Try fairly hard to get memory */
397 if ((read_buf
= (caddr_t
)kmalloc(rlen
, GFP_KERNEL
)))
400 } while (rlen
>= 1024);
402 if (read_buf
== NULL
)
406 uio
.uio_segflg
= UIO_SYSSPACE
;
407 curr_offset
= filp
->f_pos
;
408 if (filp
->f_pos
!= 0x7fffffff)
409 uio
.uio_offset
= filp
->f_pos
;
411 uio
.uio_offset
= 0xffffffff;
414 uio
.uio_resid
= iov
.iov_len
= rlen
;
415 iov
.iov_base
= read_buf
;
418 start_offset
= uio
.uio_offset
;
420 VOP_READDIR(vp
, &uio
, NULL
, &eof
, error
);
421 if ((uio
.uio_offset
== start_offset
) || error
) {
426 size
= rlen
- uio
.uio_resid
;
427 dbp
= (xfs_dirent_t
*)read_buf
;
429 namelen
= strlen(dbp
->d_name
);
431 if (filldir(dirent
, dbp
->d_name
, namelen
,
432 (loff_t
) curr_offset
& 0x7fffffff,
437 size
-= dbp
->d_reclen
;
438 curr_offset
= (loff_t
)dbp
->d_off
/* & 0x7fffffff */;
439 dbp
= (xfs_dirent_t
*)((char *)dbp
+ dbp
->d_reclen
);
445 filp
->f_pos
= uio
.uio_offset
& 0x7fffffff;
447 filp
->f_pos
= curr_offset
;
457 struct vm_area_struct
*vma
)
459 struct inode
*ip
= filp
->f_dentry
->d_inode
;
460 vnode_t
*vp
= vn_from_inode(ip
);
464 vma
->vm_ops
= &xfs_file_vm_ops
;
466 #ifdef CONFIG_XFS_DMAPI
467 if (vp
->v_vfsp
->vfs_flag
& VFS_DMI
) {
468 vma
->vm_ops
= &xfs_dmapi_file_vm_ops
;
470 #endif /* CONFIG_XFS_DMAPI */
472 vattr
.va_mask
= XFS_AT_UPDATIME
;
473 VOP_SETATTR(vp
, &vattr
, XFS_AT_UPDATIME
, NULL
, error
);
475 __vn_revalidate(vp
, &vattr
); /* update flags */
487 struct inode
*inode
= filp
->f_dentry
->d_inode
;
488 vnode_t
*vp
= vn_from_inode(inode
);
490 VOP_IOCTL(vp
, inode
, filp
, 0, cmd
, (void __user
*)arg
, error
);
493 /* NOTE: some of the ioctl's return positive #'s as a
494 * byte count indicating success, such as
495 * readlink_by_handle. So we don't "sign flip"
496 * like most other routines. This means true
497 * errors need to be returned as a negative value.
503 xfs_file_ioctl_invis(
508 struct inode
*inode
= filp
->f_dentry
->d_inode
;
509 vnode_t
*vp
= vn_from_inode(inode
);
512 VOP_IOCTL(vp
, inode
, filp
, IO_INVIS
, cmd
, (void __user
*)arg
, error
);
515 /* NOTE: some of the ioctl's return positive #'s as a
516 * byte count indicating success, such as
517 * readlink_by_handle. So we don't "sign flip"
518 * like most other routines. This means true
519 * errors need to be returned as a negative value.
524 #ifdef CONFIG_XFS_DMAPI
525 #ifdef HAVE_VMOP_MPROTECT
528 struct vm_area_struct
*vma
,
529 unsigned int newflags
)
531 vnode_t
*vp
= vn_from_inode(vma
->vm_file
->f_dentry
->d_inode
);
534 if (vp
->v_vfsp
->vfs_flag
& VFS_DMI
) {
535 if ((vma
->vm_flags
& VM_MAYSHARE
) &&
536 (newflags
& VM_WRITE
) && !(vma
->vm_flags
& VM_WRITE
)) {
537 xfs_mount_t
*mp
= XFS_VFSTOM(vp
->v_vfsp
);
539 error
= XFS_SEND_MMAP(mp
, vma
, VM_WRITE
);
544 #endif /* HAVE_VMOP_MPROTECT */
545 #endif /* CONFIG_XFS_DMAPI */
547 #ifdef HAVE_FOP_OPEN_EXEC
548 /* If the user is attempting to execute a file that is offline then
549 * we have to trigger a DMAPI READ event before the file is marked as busy
550 * otherwise the invisible I/O will not be able to write to the file to bring
557 vnode_t
*vp
= vn_from_inode(inode
);
558 xfs_mount_t
*mp
= XFS_VFSTOM(vp
->v_vfsp
);
562 if (vp
->v_vfsp
->vfs_flag
& VFS_DMI
) {
568 if (DM_EVENT_ENABLED(vp
->v_vfsp
, ip
, DM_EVENT_READ
)) {
569 error
= -XFS_SEND_DATA(mp
, DM_EVENT_READ
, vp
,
576 #endif /* HAVE_FOP_OPEN_EXEC */
578 const struct file_operations xfs_file_operations
= {
579 .llseek
= generic_file_llseek
,
580 .read
= do_sync_read
,
581 .write
= do_sync_write
,
582 .readv
= xfs_file_readv
,
583 .writev
= xfs_file_writev
,
584 .aio_read
= xfs_file_aio_read
,
585 .aio_write
= xfs_file_aio_write
,
586 .sendfile
= xfs_file_sendfile
,
587 .splice_read
= xfs_file_splice_read
,
588 .splice_write
= xfs_file_splice_write
,
589 .unlocked_ioctl
= xfs_file_ioctl
,
591 .compat_ioctl
= xfs_file_compat_ioctl
,
593 .mmap
= xfs_file_mmap
,
594 .open
= xfs_file_open
,
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 .release
= xfs_file_release
,
620 .fsync
= xfs_file_fsync
,
624 const struct file_operations xfs_dir_file_operations
= {
625 .read
= generic_read_dir
,
626 .readdir
= xfs_file_readdir
,
627 .unlocked_ioctl
= xfs_file_ioctl
,
629 .compat_ioctl
= xfs_file_compat_ioctl
,
631 .fsync
= xfs_file_fsync
,
634 static struct vm_operations_struct xfs_file_vm_ops
= {
635 .nopage
= filemap_nopage
,
636 .populate
= filemap_populate
,
639 #ifdef CONFIG_XFS_DMAPI
640 static struct vm_operations_struct xfs_dmapi_file_vm_ops
= {
641 .nopage
= xfs_vm_nopage
,
642 .populate
= filemap_populate
,
643 #ifdef HAVE_VMOP_MPROTECT
644 .mprotect
= xfs_vm_mprotect
,
647 #endif /* CONFIG_XFS_DMAPI */