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
)
70 } /* vn_mode_to_vtype() */
71 EXPORT_SYMBOL(vn_mode_to_vtype
);
74 vn_vtype_to_mode(vtype_t vtype
)
98 } /* vn_vtype_to_mode() */
99 EXPORT_SYMBOL(vn_vtype_to_mode
);
106 vp
= kmem_cache_alloc(vn_cache
, flag
);
114 EXPORT_SYMBOL(vn_alloc
);
119 kmem_cache_free(vn_cache
, vp
);
121 EXPORT_SYMBOL(vn_free
);
124 vn_open(const char *path
, uio_seg_t seg
, int flags
, int mode
,
125 vnode_t
**vpp
, int x1
, void *x2
)
129 int rc
, saved_umask
= 0;
133 ASSERT(flags
& (FWRITE
| FREAD
));
134 ASSERT(seg
== UIO_SYSSPACE
);
138 if (!(flags
& FCREAT
) && (flags
& FWRITE
))
141 /* Note for filp_open() the two low bits must be remapped to mean:
142 * 01 - read-only -> 00 read-only
143 * 10 - write-only -> 01 write-only
144 * 11 - read-write -> 10 read-write
149 saved_umask
= xchg(¤t
->fs
->umask
, 0);
151 fp
= filp_open(path
, flags
, mode
);
154 (void)xchg(¤t
->fs
->umask
, saved_umask
);
157 return (-PTR_ERR(fp
));
159 #ifdef HAVE_2ARGS_VFS_GETATTR
160 rc
= vfs_getattr(&fp
->f_path
, &stat
);
162 rc
= vfs_getattr(fp
->f_path
.mnt
, fp
->f_dentry
, &stat
);
169 vp
= vn_alloc(KM_SLEEP
);
175 saved_gfp
= mapping_gfp_mask(fp
->f_mapping
);
176 mapping_set_gfp_mask(fp
->f_mapping
, saved_gfp
& ~(__GFP_IO
|__GFP_FS
));
178 mutex_enter(&vp
->v_lock
);
179 vp
->v_type
= vn_mode_to_vtype(stat
.mode
);
181 vp
->v_gfp_mask
= saved_gfp
;
183 mutex_exit(&vp
->v_lock
);
187 EXPORT_SYMBOL(vn_open
);
190 vn_openat(const char *path
, uio_seg_t seg
, int flags
, int mode
,
191 vnode_t
**vpp
, int x1
, void *x2
, vnode_t
*vp
, int fd
)
196 ASSERT(vp
== rootdir
);
198 len
= strlen(path
) + 2;
199 realpath
= kmalloc(len
, kmem_flags_convert(KM_SLEEP
));
203 (void)snprintf(realpath
, len
, "/%s", path
);
204 rc
= vn_open(realpath
, seg
, flags
, mode
, vpp
, x1
, x2
);
209 EXPORT_SYMBOL(vn_openat
);
212 vn_rdwr(uio_rw_t uio
, vnode_t
*vp
, void *addr
, ssize_t len
, offset_t off
,
213 uio_seg_t seg
, int ioflag
, rlim64_t x2
, void *x3
, ssize_t
*residp
)
216 mm_segment_t saved_fs
;
220 ASSERT(uio
== UIO_WRITE
|| uio
== UIO_READ
);
223 ASSERT(seg
== UIO_SYSSPACE
);
224 ASSERT((ioflag
& ~FAPPEND
) == 0);
225 ASSERT(x2
== RLIM64_INFINITY
);
230 if (ioflag
& FAPPEND
)
233 /* Writable user data segment must be briefly increased for this
234 * process so we can use the user space read call paths to write
235 * in to memory allocated by the kernel. */
240 rc
= vfs_write(fp
, addr
, len
, &offset
);
242 rc
= vfs_read(fp
, addr
, len
, &offset
);
259 EXPORT_SYMBOL(vn_rdwr
);
262 vn_close(vnode_t
*vp
, int flags
, int x1
, int x2
, void *x3
, void *x4
)
269 mapping_set_gfp_mask(vp
->v_file
->f_mapping
, vp
->v_gfp_mask
);
270 rc
= filp_close(vp
->v_file
, 0);
275 EXPORT_SYMBOL(vn_close
);
277 /* vn_seek() does not actually seek it only performs bounds checking on the
278 * proposed seek. We perform minimal checking and allow vn_rdwr() to catch
279 * anything more serious. */
281 vn_seek(vnode_t
*vp
, offset_t ooff
, offset_t
*noffp
, void *ct
)
283 return ((*noffp
< 0 || *noffp
> MAXOFFSET_T
) ? EINVAL
: 0);
285 EXPORT_SYMBOL(vn_seek
);
288 * spl_basename() takes a NULL-terminated string s as input containing a path.
289 * It returns a char pointer to a string and a length that describe the
290 * basename of the path. If the basename is not "." or "/", it will be an index
291 * into the string. While the string should be NULL terminated, the section
292 * referring to the basename is not. spl_basename is dual-licensed GPLv2+ and
293 * CC0. Anyone wishing to reuse it in another codebase may pick either license.
296 spl_basename(const char *s
, const char **str
, int *len
)
311 while (i
&& s
[i
--] == '/');
321 for (end
= i
; i
; i
--) {
333 static struct dentry
*
334 spl_kern_path_locked(const char *name
, struct path
*path
)
337 struct dentry
*dentry
;
338 const char *basename
;
345 spl_basename(name
, &basename
, &len
);
347 /* We do not accept "." or ".." */
348 if (len
<= 2 && basename
[0] == '.')
349 if (len
== 1 || basename
[1] == '.')
350 return (ERR_PTR(-EACCES
));
352 rc
= kern_path(name
, LOOKUP_PARENT
, &parent
);
354 return (ERR_PTR(rc
));
356 spl_inode_lock(parent
.dentry
->d_inode
);
358 dentry
= lookup_one_len(basename
, parent
.dentry
, len
);
359 if (IS_ERR(dentry
)) {
360 spl_inode_unlock(parent
.dentry
->d_inode
);
369 /* Based on do_unlinkat() from linux/fs/namei.c */
371 vn_remove(const char *path
, uio_seg_t seg
, int flags
)
373 struct dentry
*dentry
;
375 struct inode
*inode
= NULL
;
378 ASSERT(seg
== UIO_SYSSPACE
);
379 ASSERT(flags
== RMFILE
);
381 dentry
= spl_kern_path_locked(path
, &parent
);
382 rc
= PTR_ERR(dentry
);
383 if (!IS_ERR(dentry
)) {
384 if (parent
.dentry
->d_name
.name
[parent
.dentry
->d_name
.len
]) {
389 inode
= dentry
->d_inode
;
391 atomic_inc(&inode
->i_count
);
397 #ifdef HAVE_2ARGS_VFS_UNLINK
398 rc
= vfs_unlink(parent
.dentry
->d_inode
, dentry
);
400 rc
= vfs_unlink(parent
.dentry
->d_inode
, dentry
, NULL
);
401 #endif /* HAVE_2ARGS_VFS_UNLINK */
408 spl_inode_unlock(parent
.dentry
->d_inode
);
410 iput(inode
); /* truncate the inode here */
416 rc
= !dentry
->d_inode
? -ENOENT
:
417 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
420 EXPORT_SYMBOL(vn_remove
);
422 /* Based on do_rename() from linux/fs/namei.c */
424 vn_rename(const char *oldname
, const char *newname
, int x1
)
426 struct dentry
*old_dir
, *new_dir
;
427 struct dentry
*old_dentry
, *new_dentry
;
429 struct path old_parent
, new_parent
;
432 old_dentry
= spl_kern_path_locked(oldname
, &old_parent
);
433 if (IS_ERR(old_dentry
)) {
434 rc
= PTR_ERR(old_dentry
);
438 spl_inode_unlock(old_parent
.dentry
->d_inode
);
440 new_dentry
= spl_kern_path_locked(newname
, &new_parent
);
441 if (IS_ERR(new_dentry
)) {
442 rc
= PTR_ERR(new_dentry
);
446 spl_inode_unlock(new_parent
.dentry
->d_inode
);
449 if (old_parent
.mnt
!= new_parent
.mnt
)
452 old_dir
= old_parent
.dentry
;
453 new_dir
= new_parent
.dentry
;
454 trap
= lock_rename(new_dir
, old_dir
);
456 /* source should not be ancestor of target */
458 if (old_dentry
== trap
)
461 /* target should not be an ancestor of source */
463 if (new_dentry
== trap
)
466 /* source must exist */
468 if (!old_dentry
->d_inode
)
471 /* unless the source is a directory trailing slashes give -ENOTDIR */
472 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
474 if (old_dentry
->d_name
.name
[old_dentry
->d_name
.len
])
476 if (new_dentry
->d_name
.name
[new_dentry
->d_name
.len
])
480 #if defined(HAVE_4ARGS_VFS_RENAME)
481 rc
= vfs_rename(old_dir
->d_inode
, old_dentry
,
482 new_dir
->d_inode
, new_dentry
);
483 #elif defined(HAVE_5ARGS_VFS_RENAME)
484 rc
= vfs_rename(old_dir
->d_inode
, old_dentry
,
485 new_dir
->d_inode
, new_dentry
, NULL
);
487 rc
= vfs_rename(old_dir
->d_inode
, old_dentry
,
488 new_dir
->d_inode
, new_dentry
, NULL
, 0);
491 unlock_rename(new_dir
, old_dir
);
494 path_put(&new_parent
);
497 path_put(&old_parent
);
501 EXPORT_SYMBOL(vn_rename
);
504 vn_getattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, void *x3
, void *x4
)
516 #ifdef HAVE_2ARGS_VFS_GETATTR
517 rc
= vfs_getattr(&fp
->f_path
, &stat
);
519 rc
= vfs_getattr(fp
->f_path
.mnt
, fp
->f_dentry
, &stat
);
524 vap
->va_type
= vn_mode_to_vtype(stat
.mode
);
525 vap
->va_mode
= stat
.mode
;
526 vap
->va_uid
= KUID_TO_SUID(stat
.uid
);
527 vap
->va_gid
= KGID_TO_SGID(stat
.gid
);
529 vap
->va_nodeid
= stat
.ino
;
530 vap
->va_nlink
= stat
.nlink
;
531 vap
->va_size
= stat
.size
;
532 vap
->va_blksize
= stat
.blksize
;
533 vap
->va_atime
= stat
.atime
;
534 vap
->va_mtime
= stat
.mtime
;
535 vap
->va_ctime
= stat
.ctime
;
536 vap
->va_rdev
= stat
.rdev
;
537 vap
->va_nblocks
= stat
.blocks
;
541 EXPORT_SYMBOL(vn_getattr
);
543 int vn_fsync(vnode_t
*vp
, int flags
, void *x3
, void *x4
)
556 * May enter XFS which generates a warning when PF_FSTRANS is set.
557 * To avoid this the flag is cleared over vfs_sync() and then reset.
559 fstrans
= spl_fstrans_check();
561 current
->flags
&= ~(PF_FSTRANS
);
563 error
= -spl_filp_fsync(vp
->v_file
, datasync
);
565 current
->flags
|= PF_FSTRANS
;
569 EXPORT_SYMBOL(vn_fsync
);
571 int vn_space(vnode_t
*vp
, int cmd
, struct flock
*bfp
, int flag
,
572 offset_t offset
, void *x6
, void *x7
)
574 int error
= EOPNOTSUPP
;
576 if (cmd
!= F_FREESP
|| bfp
->l_whence
!= 0)
581 ASSERT(bfp
->l_start
>= 0 && bfp
->l_len
> 0);
583 #ifdef FALLOC_FL_PUNCH_HOLE
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
);
595 #ifdef HAVE_INODE_TRUNCATE_RANGE
596 if (vp
->v_file
->f_dentry
&& vp
->v_file
->f_dentry
->d_inode
&&
597 vp
->v_file
->f_dentry
->d_inode
->i_op
&&
598 vp
->v_file
->f_dentry
->d_inode
->i_op
->truncate_range
) {
599 off_t end
= bfp
->l_start
+ bfp
->l_len
;
601 * Judging from the code in shmem_truncate_range(),
602 * it seems the kernel expects the end offset to be
603 * inclusive and aligned to the end of a page.
605 if (end
% PAGE_SIZE
!= 0) {
606 end
&= ~(off_t
)(PAGE_SIZE
- 1);
607 if (end
<= bfp
->l_start
)
612 vp
->v_file
->f_dentry
->d_inode
->i_op
->truncate_range(
613 vp
->v_file
->f_dentry
->d_inode
,
622 EXPORT_SYMBOL(vn_space
);
624 /* Function must be called while holding the vn_file_lock */
630 ASSERT(spin_is_locked(&vn_file_lock
));
632 list_for_each_entry(fp
, &vn_file_list
, f_list
) {
633 if (fd
== fp
->f_fd
&& fp
->f_task
== current
) {
634 ASSERT(atomic_read(&fp
->f_ref
) != 0);
651 /* Already open just take an extra reference */
652 spin_lock(&vn_file_lock
);
656 atomic_inc(&fp
->f_ref
);
657 spin_unlock(&vn_file_lock
);
661 spin_unlock(&vn_file_lock
);
663 /* File was not yet opened create the object and setup */
664 fp
= kmem_cache_alloc(vn_file_cache
, KM_SLEEP
);
668 mutex_enter(&fp
->f_lock
);
671 fp
->f_task
= current
;
673 atomic_inc(&fp
->f_ref
);
679 vp
= vn_alloc(KM_SLEEP
);
683 #ifdef HAVE_2ARGS_VFS_GETATTR
684 rc
= vfs_getattr(&lfp
->f_path
, &stat
);
686 rc
= vfs_getattr(lfp
->f_path
.mnt
, lfp
->f_dentry
, &stat
);
691 mutex_enter(&vp
->v_lock
);
692 vp
->v_type
= vn_mode_to_vtype(stat
.mode
);
694 mutex_exit(&vp
->v_lock
);
699 /* Put it on the tracking list */
700 spin_lock(&vn_file_lock
);
701 list_add(&fp
->f_list
, &vn_file_list
);
702 spin_unlock(&vn_file_lock
);
704 mutex_exit(&fp
->f_lock
);
712 mutex_exit(&fp
->f_lock
);
713 kmem_cache_free(vn_file_cache
, fp
);
719 static void releasef_locked(file_t
*fp
)
724 /* Unlinked from list, no refs, safe to free outside mutex */
726 vn_free(fp
->f_vnode
);
728 kmem_cache_free(vn_file_cache
, fp
);
736 spin_lock(&vn_file_lock
);
739 atomic_dec(&fp
->f_ref
);
740 if (atomic_read(&fp
->f_ref
) > 0) {
741 spin_unlock(&vn_file_lock
);
745 list_del(&fp
->f_list
);
748 spin_unlock(&vn_file_lock
);
752 EXPORT_SYMBOL(releasef
);
755 #ifdef HAVE_SET_FS_PWD_WITH_CONST
756 vn_set_fs_pwd(struct fs_struct
*fs
, const struct path
*path
)
758 vn_set_fs_pwd(struct fs_struct
*fs
, struct path
*path
)
759 #endif /* HAVE_SET_FS_PWD_WITH_CONST */
763 #ifdef HAVE_FS_STRUCT_SPINLOCK
764 spin_lock(&fs
->lock
);
768 spin_unlock(&fs
->lock
);
770 write_lock(&fs
->lock
);
774 write_unlock(&fs
->lock
);
775 #endif /* HAVE_FS_STRUCT_SPINLOCK */
782 vn_set_pwd(const char *filename
)
785 mm_segment_t saved_fs
;
789 * user_path_dir() and __user_walk() both expect 'filename' to be
790 * a user space address so we must briefly increase the data segment
791 * size to ensure strncpy_from_user() does not fail with -EFAULT.
796 rc
= user_path_dir(filename
, &path
);
800 rc
= inode_permission(path
.dentry
->d_inode
, MAY_EXEC
| MAY_ACCESS
);
804 vn_set_fs_pwd(current
->fs
, &path
);
813 EXPORT_SYMBOL(vn_set_pwd
);
816 vn_cache_constructor(void *buf
, void *cdrarg
, int kmflags
)
818 struct vnode
*vp
= buf
;
820 mutex_init(&vp
->v_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
823 } /* vn_cache_constructor() */
826 vn_cache_destructor(void *buf
, void *cdrarg
)
828 struct vnode
*vp
= buf
;
830 mutex_destroy(&vp
->v_lock
);
831 } /* vn_cache_destructor() */
834 vn_file_cache_constructor(void *buf
, void *cdrarg
, int kmflags
)
838 atomic_set(&fp
->f_ref
, 0);
839 mutex_init(&fp
->f_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
840 INIT_LIST_HEAD(&fp
->f_list
);
843 } /* file_cache_constructor() */
846 vn_file_cache_destructor(void *buf
, void *cdrarg
)
850 mutex_destroy(&fp
->f_lock
);
851 } /* vn_file_cache_destructor() */
856 vn_cache
= kmem_cache_create("spl_vn_cache",
857 sizeof(struct vnode
), 64,
858 vn_cache_constructor
,
860 NULL
, NULL
, NULL
, KMC_KMEM
);
862 vn_file_cache
= kmem_cache_create("spl_vn_file_cache",
864 vn_file_cache_constructor
,
865 vn_file_cache_destructor
,
866 NULL
, NULL
, NULL
, KMC_KMEM
);
873 file_t
*fp
, *next_fp
;
876 spin_lock(&vn_file_lock
);
878 list_for_each_entry_safe(fp
, next_fp
, &vn_file_list
, f_list
) {
879 list_del(&fp
->f_list
);
884 spin_unlock(&vn_file_lock
);
887 printk(KERN_WARNING
"WARNING: %d vnode files leaked\n", leaked
);
889 kmem_cache_destroy(vn_file_cache
);
890 kmem_cache_destroy(vn_cache
);