2 * Copyright (c) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
39 #include "xfs_trans.h"
40 #include "xfs_dmapi.h"
41 #include "xfs_mount.h"
42 #include "xfs_bmap_btree.h"
43 #include "xfs_alloc_btree.h"
44 #include "xfs_ialloc_btree.h"
45 #include "xfs_alloc.h"
46 #include "xfs_btree.h"
47 #include "xfs_attr_sf.h"
48 #include "xfs_dir_sf.h"
49 #include "xfs_dir2_sf.h"
50 #include "xfs_dinode.h"
51 #include "xfs_inode.h"
52 #include "xfs_error.h"
54 #include "xfs_ioctl32.h"
56 #include <linux/dcache.h>
57 #include <linux/smp_lock.h>
59 static struct vm_operations_struct linvfs_file_vm_ops
;
60 #ifdef CONFIG_XFS_DMAPI
61 static struct vm_operations_struct linvfs_dmapi_file_vm_ops
;
72 struct iovec iov
= {buf
, count
};
73 struct file
*file
= iocb
->ki_filp
;
74 vnode_t
*vp
= LINVFS_GET_VP(file
->f_dentry
->d_inode
);
77 BUG_ON(iocb
->ki_pos
!= pos
);
79 if (unlikely(file
->f_flags
& O_DIRECT
))
80 ioflags
|= IO_ISDIRECT
;
81 VOP_READ(vp
, iocb
, &iov
, 1, &iocb
->ki_pos
, ioflags
, NULL
, rval
);
93 return __linvfs_read(iocb
, buf
, IO_ISAIO
, count
, pos
);
97 linvfs_aio_read_invis(
103 return __linvfs_read(iocb
, buf
, IO_ISAIO
|IO_INVIS
, count
, pos
);
107 STATIC
inline ssize_t
110 const char __user
*buf
,
115 struct iovec iov
= {(void __user
*)buf
, count
};
116 struct file
*file
= iocb
->ki_filp
;
117 struct inode
*inode
= file
->f_mapping
->host
;
118 vnode_t
*vp
= LINVFS_GET_VP(inode
);
121 BUG_ON(iocb
->ki_pos
!= pos
);
122 if (unlikely(file
->f_flags
& O_DIRECT
))
123 ioflags
|= IO_ISDIRECT
;
125 VOP_WRITE(vp
, iocb
, &iov
, 1, &iocb
->ki_pos
, ioflags
, NULL
, rval
);
133 const char __user
*buf
,
137 return __linvfs_write(iocb
, buf
, IO_ISAIO
, count
, pos
);
141 linvfs_aio_write_invis(
143 const char __user
*buf
,
147 return __linvfs_write(iocb
, buf
, IO_ISAIO
|IO_INVIS
, count
, pos
);
151 STATIC
inline ssize_t
154 const struct iovec
*iov
,
156 unsigned long nr_segs
,
159 struct inode
*inode
= file
->f_mapping
->host
;
160 vnode_t
*vp
= LINVFS_GET_VP(inode
);
164 init_sync_kiocb(&kiocb
, file
);
165 kiocb
.ki_pos
= *ppos
;
167 if (unlikely(file
->f_flags
& O_DIRECT
))
168 ioflags
|= IO_ISDIRECT
;
169 VOP_READ(vp
, &kiocb
, iov
, nr_segs
, &kiocb
.ki_pos
, ioflags
, NULL
, rval
);
171 *ppos
= kiocb
.ki_pos
;
178 const struct iovec
*iov
,
179 unsigned long nr_segs
,
182 return __linvfs_readv(file
, iov
, 0, nr_segs
, ppos
);
188 const struct iovec
*iov
,
189 unsigned long nr_segs
,
192 return __linvfs_readv(file
, iov
, IO_INVIS
, nr_segs
, ppos
);
196 STATIC
inline ssize_t
199 const struct iovec
*iov
,
201 unsigned long nr_segs
,
204 struct inode
*inode
= file
->f_mapping
->host
;
205 vnode_t
*vp
= LINVFS_GET_VP(inode
);
209 init_sync_kiocb(&kiocb
, file
);
210 kiocb
.ki_pos
= *ppos
;
211 if (unlikely(file
->f_flags
& O_DIRECT
))
212 ioflags
|= IO_ISDIRECT
;
214 VOP_WRITE(vp
, &kiocb
, iov
, nr_segs
, &kiocb
.ki_pos
, ioflags
, NULL
, rval
);
216 *ppos
= kiocb
.ki_pos
;
224 const struct iovec
*iov
,
225 unsigned long nr_segs
,
228 return __linvfs_writev(file
, iov
, 0, nr_segs
, ppos
);
234 const struct iovec
*iov
,
235 unsigned long nr_segs
,
238 return __linvfs_writev(file
, iov
, IO_INVIS
, nr_segs
, ppos
);
249 vnode_t
*vp
= LINVFS_GET_VP(filp
->f_dentry
->d_inode
);
252 VOP_SENDFILE(vp
, filp
, ppos
, 0, count
, actor
, target
, NULL
, rval
);
262 vnode_t
*vp
= LINVFS_GET_VP(inode
);
265 if (!(filp
->f_flags
& O_LARGEFILE
) && i_size_read(inode
) > MAX_NON_LFS
)
269 VOP_OPEN(vp
, NULL
, error
);
279 vnode_t
*vp
= LINVFS_GET_VP(inode
);
283 VOP_RELEASE(vp
, error
);
291 struct dentry
*dentry
,
294 struct inode
*inode
= dentry
->d_inode
;
295 vnode_t
*vp
= LINVFS_GET_VP(inode
);
297 int flags
= FSYNC_WAIT
;
303 VOP_FSYNC(vp
, flags
, NULL
, (xfs_off_t
)0, (xfs_off_t
)-1, error
);
308 * linvfs_readdir maps to VOP_READDIR().
309 * We need to build a uio, cred, ...
312 #define nextdp(dp) ((struct xfs_dirent *)((char *)(dp) + (dp)->d_reclen))
314 #ifdef CONFIG_XFS_DMAPI
317 linvfs_filemap_nopage(
318 struct vm_area_struct
*area
,
319 unsigned long address
,
322 struct inode
*inode
= area
->vm_file
->f_dentry
->d_inode
;
323 vnode_t
*vp
= LINVFS_GET_VP(inode
);
324 xfs_mount_t
*mp
= XFS_VFSTOM(vp
->v_vfsp
);
327 ASSERT_ALWAYS(vp
->v_vfsp
->vfs_flag
& VFS_DMI
);
329 error
= XFS_SEND_MMAP(mp
, area
, 0);
333 return filemap_nopage(area
, address
, type
);
336 #endif /* CONFIG_XFS_DMAPI */
351 int namelen
, size
= 0;
352 size_t rlen
= PAGE_CACHE_SIZE
;
353 xfs_off_t start_offset
, curr_offset
;
354 xfs_dirent_t
*dbp
= NULL
;
356 vp
= LINVFS_GET_VP(filp
->f_dentry
->d_inode
);
359 /* Try fairly hard to get memory */
361 if ((read_buf
= (caddr_t
)kmalloc(rlen
, GFP_KERNEL
)))
364 } while (rlen
>= 1024);
366 if (read_buf
== NULL
)
370 uio
.uio_segflg
= UIO_SYSSPACE
;
371 curr_offset
= filp
->f_pos
;
372 if (filp
->f_pos
!= 0x7fffffff)
373 uio
.uio_offset
= filp
->f_pos
;
375 uio
.uio_offset
= 0xffffffff;
378 uio
.uio_resid
= iov
.iov_len
= rlen
;
379 iov
.iov_base
= read_buf
;
382 start_offset
= uio
.uio_offset
;
384 VOP_READDIR(vp
, &uio
, NULL
, &eof
, error
);
385 if ((uio
.uio_offset
== start_offset
) || error
) {
390 size
= rlen
- uio
.uio_resid
;
391 dbp
= (xfs_dirent_t
*)read_buf
;
393 namelen
= strlen(dbp
->d_name
);
395 if (filldir(dirent
, dbp
->d_name
, namelen
,
396 (loff_t
) curr_offset
& 0x7fffffff,
401 size
-= dbp
->d_reclen
;
402 curr_offset
= (loff_t
)dbp
->d_off
/* & 0x7fffffff */;
409 filp
->f_pos
= uio
.uio_offset
& 0x7fffffff;
411 filp
->f_pos
= curr_offset
;
422 struct vm_area_struct
*vma
)
424 struct inode
*ip
= filp
->f_dentry
->d_inode
;
425 vnode_t
*vp
= LINVFS_GET_VP(ip
);
426 vattr_t va
= { .va_mask
= XFS_AT_UPDATIME
};
429 vma
->vm_ops
= &linvfs_file_vm_ops
;
431 #ifdef CONFIG_XFS_DMAPI
432 if (vp
->v_vfsp
->vfs_flag
& VFS_DMI
) {
433 vma
->vm_ops
= &linvfs_dmapi_file_vm_ops
;
435 #endif /* CONFIG_XFS_DMAPI */
437 VOP_SETATTR(vp
, &va
, XFS_AT_UPDATIME
, NULL
, error
);
439 vn_revalidate(vp
); /* update Linux inode flags */
451 struct inode
*inode
= filp
->f_dentry
->d_inode
;
452 vnode_t
*vp
= LINVFS_GET_VP(inode
);
454 VOP_IOCTL(vp
, inode
, filp
, 0, cmd
, (void __user
*)arg
, error
);
457 /* NOTE: some of the ioctl's return positive #'s as a
458 * byte count indicating success, such as
459 * readlink_by_handle. So we don't "sign flip"
460 * like most other routines. This means true
461 * errors need to be returned as a negative value.
473 struct inode
*inode
= filp
->f_dentry
->d_inode
;
474 vnode_t
*vp
= LINVFS_GET_VP(inode
);
477 VOP_IOCTL(vp
, inode
, filp
, IO_INVIS
, cmd
, (void __user
*)arg
, error
);
480 /* NOTE: some of the ioctl's return positive #'s as a
481 * byte count indicating success, such as
482 * readlink_by_handle. So we don't "sign flip"
483 * like most other routines. This means true
484 * errors need to be returned as a negative value.
489 #ifdef CONFIG_XFS_DMAPI
490 #ifdef HAVE_VMOP_MPROTECT
493 struct vm_area_struct
*vma
,
494 unsigned int newflags
)
496 vnode_t
*vp
= LINVFS_GET_VP(vma
->vm_file
->f_dentry
->d_inode
);
499 if (vp
->v_vfsp
->vfs_flag
& VFS_DMI
) {
500 if ((vma
->vm_flags
& VM_MAYSHARE
) &&
501 (newflags
& VM_WRITE
) && !(vma
->vm_flags
& VM_WRITE
)) {
502 xfs_mount_t
*mp
= XFS_VFSTOM(vp
->v_vfsp
);
504 error
= XFS_SEND_MMAP(mp
, vma
, VM_WRITE
);
509 #endif /* HAVE_VMOP_MPROTECT */
510 #endif /* CONFIG_XFS_DMAPI */
512 #ifdef HAVE_FOP_OPEN_EXEC
513 /* If the user is attempting to execute a file that is offline then
514 * we have to trigger a DMAPI READ event before the file is marked as busy
515 * otherwise the invisible I/O will not be able to write to the file to bring
522 vnode_t
*vp
= LINVFS_GET_VP(inode
);
523 xfs_mount_t
*mp
= XFS_VFSTOM(vp
->v_vfsp
);
528 if (vp
->v_vfsp
->vfs_flag
& VFS_DMI
) {
529 bdp
= vn_bhv_lookup(VN_BHV_HEAD(vp
), &xfs_vnodeops
);
534 ip
= XFS_BHVTOI(bdp
);
535 if (DM_EVENT_ENABLED(vp
->v_vfsp
, ip
, DM_EVENT_READ
)) {
536 error
= -XFS_SEND_DATA(mp
, DM_EVENT_READ
, vp
,
543 #endif /* HAVE_FOP_OPEN_EXEC */
545 struct file_operations linvfs_file_operations
= {
546 .llseek
= generic_file_llseek
,
547 .read
= do_sync_read
,
548 .write
= do_sync_write
,
549 .readv
= linvfs_readv
,
550 .writev
= linvfs_writev
,
551 .aio_read
= linvfs_aio_read
,
552 .aio_write
= linvfs_aio_write
,
553 .sendfile
= linvfs_sendfile
,
554 .unlocked_ioctl
= linvfs_ioctl
,
556 .compat_ioctl
= linvfs_compat_ioctl
,
558 .mmap
= linvfs_file_mmap
,
560 .release
= linvfs_release
,
561 .fsync
= linvfs_fsync
,
562 #ifdef HAVE_FOP_OPEN_EXEC
563 .open_exec
= linvfs_open_exec
,
567 struct file_operations linvfs_invis_file_operations
= {
568 .llseek
= generic_file_llseek
,
569 .read
= do_sync_read
,
570 .write
= do_sync_write
,
571 .readv
= linvfs_readv_invis
,
572 .writev
= linvfs_writev_invis
,
573 .aio_read
= linvfs_aio_read_invis
,
574 .aio_write
= linvfs_aio_write_invis
,
575 .sendfile
= linvfs_sendfile
,
576 .unlocked_ioctl
= linvfs_ioctl_invis
,
578 .compat_ioctl
= linvfs_compat_invis_ioctl
,
580 .mmap
= linvfs_file_mmap
,
582 .release
= linvfs_release
,
583 .fsync
= linvfs_fsync
,
587 struct file_operations linvfs_dir_operations
= {
588 .read
= generic_read_dir
,
589 .readdir
= linvfs_readdir
,
590 .unlocked_ioctl
= linvfs_ioctl
,
592 .compat_ioctl
= linvfs_compat_ioctl
,
594 .fsync
= linvfs_fsync
,
597 static struct vm_operations_struct linvfs_file_vm_ops
= {
598 .nopage
= filemap_nopage
,
599 .populate
= filemap_populate
,
602 #ifdef CONFIG_XFS_DMAPI
603 static struct vm_operations_struct linvfs_dmapi_file_vm_ops
= {
604 .nopage
= linvfs_filemap_nopage
,
605 .populate
= filemap_populate
,
606 #ifdef HAVE_VMOP_MPROTECT
607 .mprotect
= linvfs_mprotect
,
610 #endif /* CONFIG_XFS_DMAPI */