1 /*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Vnode Implementation.
25 \*****************************************************************************/
28 #include <sys/vnode.h>
29 #include <sys/kmem_cache.h>
30 #include <linux/falloc.h>
31 #include <linux/file_compat.h>
33 vnode_t
*rootdir
= (vnode_t
*)0xabcd1234;
34 EXPORT_SYMBOL(rootdir
);
36 static spl_kmem_cache_t
*vn_cache
;
37 static spl_kmem_cache_t
*vn_file_cache
;
39 static DEFINE_SPINLOCK(vn_file_lock
);
40 static LIST_HEAD(vn_file_list
);
43 vn_mode_to_vtype(mode_t mode
)
67 } /* vn_mode_to_vtype() */
68 EXPORT_SYMBOL(vn_mode_to_vtype
);
71 vn_vtype_to_mode(vtype_t vtype
)
95 } /* vn_vtype_to_mode() */
96 EXPORT_SYMBOL(vn_vtype_to_mode
);
103 vp
= kmem_cache_alloc(vn_cache
, flag
);
111 EXPORT_SYMBOL(vn_alloc
);
116 kmem_cache_free(vn_cache
, vp
);
118 EXPORT_SYMBOL(vn_free
);
121 vn_open(const char *path
, uio_seg_t seg
, int flags
, int mode
,
122 vnode_t
**vpp
, int x1
, void *x2
)
126 int rc
, saved_umask
= 0;
130 ASSERT(flags
& (FWRITE
| FREAD
));
131 ASSERT(seg
== UIO_SYSSPACE
);
135 if (!(flags
& FCREAT
) && (flags
& FWRITE
))
138 /* Note for filp_open() the two low bits must be remapped to mean:
139 * 01 - read-only -> 00 read-only
140 * 10 - write-only -> 01 write-only
141 * 11 - read-write -> 10 read-write
146 saved_umask
= xchg(¤t
->fs
->umask
, 0);
148 fp
= filp_open(path
, flags
, mode
);
151 (void)xchg(¤t
->fs
->umask
, saved_umask
);
154 return (-PTR_ERR(fp
));
156 #if defined(HAVE_4ARGS_VFS_GETATTR)
157 rc
= vfs_getattr(&fp
->f_path
, &stat
, STATX_TYPE
, AT_STATX_SYNC_AS_STAT
);
158 #elif defined(HAVE_2ARGS_VFS_GETATTR)
159 rc
= vfs_getattr(&fp
->f_path
, &stat
);
161 rc
= vfs_getattr(fp
->f_path
.mnt
, fp
->f_dentry
, &stat
);
168 vp
= vn_alloc(KM_SLEEP
);
174 saved_gfp
= mapping_gfp_mask(fp
->f_mapping
);
175 mapping_set_gfp_mask(fp
->f_mapping
, saved_gfp
& ~(__GFP_IO
|__GFP_FS
));
177 mutex_enter(&vp
->v_lock
);
178 vp
->v_type
= vn_mode_to_vtype(stat
.mode
);
180 vp
->v_gfp_mask
= saved_gfp
;
182 mutex_exit(&vp
->v_lock
);
186 EXPORT_SYMBOL(vn_open
);
189 vn_openat(const char *path
, uio_seg_t seg
, int flags
, int mode
,
190 vnode_t
**vpp
, int x1
, void *x2
, vnode_t
*vp
, int fd
)
195 ASSERT(vp
== rootdir
);
197 len
= strlen(path
) + 2;
198 realpath
= kmalloc(len
, kmem_flags_convert(KM_SLEEP
));
202 (void)snprintf(realpath
, len
, "/%s", path
);
203 rc
= vn_open(realpath
, seg
, flags
, mode
, vpp
, x1
, x2
);
208 EXPORT_SYMBOL(vn_openat
);
211 vn_rdwr(uio_rw_t uio
, vnode_t
*vp
, void *addr
, ssize_t len
, offset_t off
,
212 uio_seg_t seg
, int ioflag
, rlim64_t x2
, void *x3
, ssize_t
*residp
)
214 struct file
*fp
= vp
->v_file
;
218 ASSERT(uio
== UIO_WRITE
|| uio
== UIO_READ
);
219 ASSERT(seg
== UIO_SYSSPACE
);
220 ASSERT((ioflag
& ~FAPPEND
) == 0);
222 if (ioflag
& FAPPEND
)
226 rc
= spl_kernel_write(fp
, addr
, len
, &offset
);
228 rc
= spl_kernel_read(fp
, addr
, len
, &offset
);
244 EXPORT_SYMBOL(vn_rdwr
);
247 vn_close(vnode_t
*vp
, int flags
, int x1
, int x2
, void *x3
, void *x4
)
254 mapping_set_gfp_mask(vp
->v_file
->f_mapping
, vp
->v_gfp_mask
);
255 rc
= filp_close(vp
->v_file
, 0);
260 EXPORT_SYMBOL(vn_close
);
262 /* vn_seek() does not actually seek it only performs bounds checking on the
263 * proposed seek. We perform minimal checking and allow vn_rdwr() to catch
264 * anything more serious. */
266 vn_seek(vnode_t
*vp
, offset_t ooff
, offset_t
*noffp
, void *ct
)
268 return ((*noffp
< 0 || *noffp
> MAXOFFSET_T
) ? EINVAL
: 0);
270 EXPORT_SYMBOL(vn_seek
);
273 * spl_basename() takes a NULL-terminated string s as input containing a path.
274 * It returns a char pointer to a string and a length that describe the
275 * basename of the path. If the basename is not "." or "/", it will be an index
276 * into the string. While the string should be NULL terminated, the section
277 * referring to the basename is not. spl_basename is dual-licensed GPLv2+ and
278 * CC0. Anyone wishing to reuse it in another codebase may pick either license.
281 spl_basename(const char *s
, const char **str
, int *len
)
296 while (i
&& s
[i
--] == '/');
306 for (end
= i
; i
; i
--) {
318 static struct dentry
*
319 spl_kern_path_locked(const char *name
, struct path
*path
)
322 struct dentry
*dentry
;
323 const char *basename
;
330 spl_basename(name
, &basename
, &len
);
332 /* We do not accept "." or ".." */
333 if (len
<= 2 && basename
[0] == '.')
334 if (len
== 1 || basename
[1] == '.')
335 return (ERR_PTR(-EACCES
));
337 rc
= kern_path(name
, LOOKUP_PARENT
, &parent
);
339 return (ERR_PTR(rc
));
341 /* use I_MUTEX_PARENT because vfs_unlink needs it */
342 spl_inode_lock_nested(parent
.dentry
->d_inode
, I_MUTEX_PARENT
);
344 dentry
= lookup_one_len(basename
, parent
.dentry
, len
);
345 if (IS_ERR(dentry
)) {
346 spl_inode_unlock(parent
.dentry
->d_inode
);
355 /* Based on do_unlinkat() from linux/fs/namei.c */
357 vn_remove(const char *path
, uio_seg_t seg
, int flags
)
359 struct dentry
*dentry
;
361 struct inode
*inode
= NULL
;
364 ASSERT(seg
== UIO_SYSSPACE
);
365 ASSERT(flags
== RMFILE
);
367 dentry
= spl_kern_path_locked(path
, &parent
);
368 rc
= PTR_ERR(dentry
);
369 if (!IS_ERR(dentry
)) {
370 if (parent
.dentry
->d_name
.name
[parent
.dentry
->d_name
.len
]) {
375 inode
= dentry
->d_inode
;
377 atomic_inc(&inode
->i_count
);
383 #ifdef HAVE_2ARGS_VFS_UNLINK
384 rc
= vfs_unlink(parent
.dentry
->d_inode
, dentry
);
386 rc
= vfs_unlink(parent
.dentry
->d_inode
, dentry
, NULL
);
387 #endif /* HAVE_2ARGS_VFS_UNLINK */
394 spl_inode_unlock(parent
.dentry
->d_inode
);
396 iput(inode
); /* truncate the inode here */
402 rc
= !dentry
->d_inode
? -ENOENT
:
403 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
406 EXPORT_SYMBOL(vn_remove
);
408 /* Based on do_rename() from linux/fs/namei.c */
410 vn_rename(const char *oldname
, const char *newname
, int x1
)
412 struct dentry
*old_dir
, *new_dir
;
413 struct dentry
*old_dentry
, *new_dentry
;
415 struct path old_parent
, new_parent
;
418 old_dentry
= spl_kern_path_locked(oldname
, &old_parent
);
419 if (IS_ERR(old_dentry
)) {
420 rc
= PTR_ERR(old_dentry
);
424 spl_inode_unlock(old_parent
.dentry
->d_inode
);
426 new_dentry
= spl_kern_path_locked(newname
, &new_parent
);
427 if (IS_ERR(new_dentry
)) {
428 rc
= PTR_ERR(new_dentry
);
432 spl_inode_unlock(new_parent
.dentry
->d_inode
);
435 if (old_parent
.mnt
!= new_parent
.mnt
)
438 old_dir
= old_parent
.dentry
;
439 new_dir
= new_parent
.dentry
;
440 trap
= lock_rename(new_dir
, old_dir
);
442 /* source should not be ancestor of target */
444 if (old_dentry
== trap
)
447 /* target should not be an ancestor of source */
449 if (new_dentry
== trap
)
452 /* source must exist */
454 if (!old_dentry
->d_inode
)
457 /* unless the source is a directory trailing slashes give -ENOTDIR */
458 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
460 if (old_dentry
->d_name
.name
[old_dentry
->d_name
.len
])
462 if (new_dentry
->d_name
.name
[new_dentry
->d_name
.len
])
466 #if defined(HAVE_4ARGS_VFS_RENAME)
467 rc
= vfs_rename(old_dir
->d_inode
, old_dentry
,
468 new_dir
->d_inode
, new_dentry
);
469 #elif defined(HAVE_5ARGS_VFS_RENAME)
470 rc
= vfs_rename(old_dir
->d_inode
, old_dentry
,
471 new_dir
->d_inode
, new_dentry
, NULL
);
473 rc
= vfs_rename(old_dir
->d_inode
, old_dentry
,
474 new_dir
->d_inode
, new_dentry
, NULL
, 0);
477 unlock_rename(new_dir
, old_dir
);
480 path_put(&new_parent
);
483 path_put(&old_parent
);
487 EXPORT_SYMBOL(vn_rename
);
490 vn_getattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, void *x3
, void *x4
)
502 #if defined(HAVE_4ARGS_VFS_GETATTR)
503 rc
= vfs_getattr(&fp
->f_path
, &stat
, STATX_BASIC_STATS
,
504 AT_STATX_SYNC_AS_STAT
);
505 #elif defined(HAVE_2ARGS_VFS_GETATTR)
506 rc
= vfs_getattr(&fp
->f_path
, &stat
);
508 rc
= vfs_getattr(fp
->f_path
.mnt
, fp
->f_dentry
, &stat
);
513 vap
->va_type
= vn_mode_to_vtype(stat
.mode
);
514 vap
->va_mode
= stat
.mode
;
515 vap
->va_uid
= KUID_TO_SUID(stat
.uid
);
516 vap
->va_gid
= KGID_TO_SGID(stat
.gid
);
518 vap
->va_nodeid
= stat
.ino
;
519 vap
->va_nlink
= stat
.nlink
;
520 vap
->va_size
= stat
.size
;
521 vap
->va_blksize
= stat
.blksize
;
522 vap
->va_atime
= stat
.atime
;
523 vap
->va_mtime
= stat
.mtime
;
524 vap
->va_ctime
= stat
.ctime
;
525 vap
->va_rdev
= stat
.rdev
;
526 vap
->va_nblocks
= stat
.blocks
;
530 EXPORT_SYMBOL(vn_getattr
);
532 int vn_fsync(vnode_t
*vp
, int flags
, void *x3
, void *x4
)
545 * May enter XFS which generates a warning when PF_FSTRANS is set.
546 * To avoid this the flag is cleared over vfs_sync() and then reset.
548 fstrans
= __spl_pf_fstrans_check();
550 current
->flags
&= ~(__SPL_PF_FSTRANS
);
552 error
= -spl_filp_fsync(vp
->v_file
, datasync
);
554 current
->flags
|= __SPL_PF_FSTRANS
;
558 EXPORT_SYMBOL(vn_fsync
);
560 int vn_space(vnode_t
*vp
, int cmd
, struct flock
*bfp
, int flag
,
561 offset_t offset
, void *x6
, void *x7
)
563 int error
= EOPNOTSUPP
;
564 #ifdef FALLOC_FL_PUNCH_HOLE
568 if (cmd
!= F_FREESP
|| bfp
->l_whence
!= 0)
573 ASSERT(bfp
->l_start
>= 0 && bfp
->l_len
> 0);
575 #ifdef FALLOC_FL_PUNCH_HOLE
577 * May enter XFS which generates a warning when PF_FSTRANS is set.
578 * To avoid this the flag is cleared over vfs_sync() and then reset.
580 fstrans
= __spl_pf_fstrans_check();
582 current
->flags
&= ~(__SPL_PF_FSTRANS
);
585 * When supported by the underlying file system preferentially
586 * use the fallocate() callback to preallocate the space.
588 error
= -spl_filp_fallocate(vp
->v_file
,
589 FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
,
590 bfp
->l_start
, bfp
->l_len
);
593 current
->flags
|= __SPL_PF_FSTRANS
;
599 #ifdef HAVE_INODE_TRUNCATE_RANGE
600 if (vp
->v_file
->f_dentry
&& vp
->v_file
->f_dentry
->d_inode
&&
601 vp
->v_file
->f_dentry
->d_inode
->i_op
&&
602 vp
->v_file
->f_dentry
->d_inode
->i_op
->truncate_range
) {
603 off_t end
= bfp
->l_start
+ bfp
->l_len
;
605 * Judging from the code in shmem_truncate_range(),
606 * it seems the kernel expects the end offset to be
607 * inclusive and aligned to the end of a page.
609 if (end
% PAGE_SIZE
!= 0) {
610 end
&= ~(off_t
)(PAGE_SIZE
- 1);
611 if (end
<= bfp
->l_start
)
616 vp
->v_file
->f_dentry
->d_inode
->i_op
->truncate_range(
617 vp
->v_file
->f_dentry
->d_inode
,
626 EXPORT_SYMBOL(vn_space
);
628 /* Function must be called while holding the vn_file_lock */
630 file_find(int fd
, struct task_struct
*task
)
634 ASSERT(spin_is_locked(&vn_file_lock
));
636 list_for_each_entry(fp
, &vn_file_list
, f_list
) {
637 if (fd
== fp
->f_fd
&& fp
->f_task
== task
) {
638 ASSERT(atomic_read(&fp
->f_ref
) != 0);
658 /* Already open just take an extra reference */
659 spin_lock(&vn_file_lock
);
661 fp
= file_find(fd
, current
);
666 * areleasef() can cause us to see a stale reference when
667 * userspace has reused a file descriptor before areleasef()
668 * has run. fput() the stale reference and replace it. We
669 * retain the original reference count such that the concurrent
670 * areleasef() will decrement its reference and terminate.
672 if (lfp
!= fp
->f_file
) {
674 fp
->f_vnode
->v_file
= lfp
;
676 atomic_inc(&fp
->f_ref
);
677 spin_unlock(&vn_file_lock
);
681 spin_unlock(&vn_file_lock
);
683 /* File was not yet opened create the object and setup */
684 fp
= kmem_cache_alloc(vn_file_cache
, KM_SLEEP
);
688 mutex_enter(&fp
->f_lock
);
691 fp
->f_task
= current
;
693 atomic_inc(&fp
->f_ref
);
699 vp
= vn_alloc(KM_SLEEP
);
703 #if defined(HAVE_4ARGS_VFS_GETATTR)
704 rc
= vfs_getattr(&lfp
->f_path
, &stat
, STATX_TYPE
, AT_STATX_SYNC_AS_STAT
);
705 #elif defined(HAVE_2ARGS_VFS_GETATTR)
706 rc
= vfs_getattr(&lfp
->f_path
, &stat
);
708 rc
= vfs_getattr(lfp
->f_path
.mnt
, lfp
->f_dentry
, &stat
);
713 mutex_enter(&vp
->v_lock
);
714 vp
->v_type
= vn_mode_to_vtype(stat
.mode
);
716 mutex_exit(&vp
->v_lock
);
721 /* Put it on the tracking list */
722 spin_lock(&vn_file_lock
);
723 list_add(&fp
->f_list
, &vn_file_list
);
724 spin_unlock(&vn_file_lock
);
726 mutex_exit(&fp
->f_lock
);
734 mutex_exit(&fp
->f_lock
);
735 kmem_cache_free(vn_file_cache
, fp
);
741 static void releasef_locked(file_t
*fp
)
746 /* Unlinked from list, no refs, safe to free outside mutex */
748 vn_free(fp
->f_vnode
);
750 kmem_cache_free(vn_file_cache
, fp
);
756 areleasef(fd
, P_FINFO(current
));
758 EXPORT_SYMBOL(releasef
);
761 vn_areleasef(int fd
, uf_info_t
*fip
)
764 struct task_struct
*task
= (struct task_struct
*)fip
;
769 spin_lock(&vn_file_lock
);
770 fp
= file_find(fd
, task
);
772 atomic_dec(&fp
->f_ref
);
773 if (atomic_read(&fp
->f_ref
) > 0) {
774 spin_unlock(&vn_file_lock
);
778 list_del(&fp
->f_list
);
781 spin_unlock(&vn_file_lock
);
785 EXPORT_SYMBOL(areleasef
);
789 #ifdef HAVE_SET_FS_PWD_WITH_CONST
790 vn_set_fs_pwd(struct fs_struct
*fs
, const struct path
*path
)
792 vn_set_fs_pwd(struct fs_struct
*fs
, struct path
*path
)
793 #endif /* HAVE_SET_FS_PWD_WITH_CONST */
797 #ifdef HAVE_FS_STRUCT_SPINLOCK
798 spin_lock(&fs
->lock
);
802 spin_unlock(&fs
->lock
);
804 write_lock(&fs
->lock
);
808 write_unlock(&fs
->lock
);
809 #endif /* HAVE_FS_STRUCT_SPINLOCK */
816 vn_set_pwd(const char *filename
)
819 mm_segment_t saved_fs
;
823 * user_path_dir() and __user_walk() both expect 'filename' to be
824 * a user space address so we must briefly increase the data segment
825 * size to ensure strncpy_from_user() does not fail with -EFAULT.
830 rc
= user_path_dir(filename
, &path
);
834 rc
= inode_permission(path
.dentry
->d_inode
, MAY_EXEC
| MAY_ACCESS
);
838 vn_set_fs_pwd(current
->fs
, &path
);
847 EXPORT_SYMBOL(vn_set_pwd
);
850 vn_cache_constructor(void *buf
, void *cdrarg
, int kmflags
)
852 struct vnode
*vp
= buf
;
854 mutex_init(&vp
->v_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
857 } /* vn_cache_constructor() */
860 vn_cache_destructor(void *buf
, void *cdrarg
)
862 struct vnode
*vp
= buf
;
864 mutex_destroy(&vp
->v_lock
);
865 } /* vn_cache_destructor() */
868 vn_file_cache_constructor(void *buf
, void *cdrarg
, int kmflags
)
872 atomic_set(&fp
->f_ref
, 0);
873 mutex_init(&fp
->f_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
874 INIT_LIST_HEAD(&fp
->f_list
);
877 } /* file_cache_constructor() */
880 vn_file_cache_destructor(void *buf
, void *cdrarg
)
884 mutex_destroy(&fp
->f_lock
);
885 } /* vn_file_cache_destructor() */
890 vn_cache
= kmem_cache_create("spl_vn_cache",
891 sizeof(struct vnode
), 64,
892 vn_cache_constructor
,
894 NULL
, NULL
, NULL
, 0);
896 vn_file_cache
= kmem_cache_create("spl_vn_file_cache",
898 vn_file_cache_constructor
,
899 vn_file_cache_destructor
,
900 NULL
, NULL
, NULL
, 0);
907 file_t
*fp
, *next_fp
;
910 spin_lock(&vn_file_lock
);
912 list_for_each_entry_safe(fp
, next_fp
, &vn_file_list
, f_list
) {
913 list_del(&fp
->f_list
);
918 spin_unlock(&vn_file_lock
);
921 printk(KERN_WARNING
"WARNING: %d vnode files leaked\n", leaked
);
923 kmem_cache_destroy(vn_file_cache
);
924 kmem_cache_destroy(vn_cache
);