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://github.com/behlendorf/spl/>.
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 \*****************************************************************************/
27 #include <sys/vnode.h>
28 #include <spl-debug.h>
30 #ifdef SS_DEBUG_SUBSYS
31 #undef SS_DEBUG_SUBSYS
34 #define SS_DEBUG_SUBSYS SS_VNODE
36 vnode_t
*rootdir
= (vnode_t
*)0xabcd1234;
37 EXPORT_SYMBOL(rootdir
);
39 static spl_kmem_cache_t
*vn_cache
;
40 static spl_kmem_cache_t
*vn_file_cache
;
42 static DEFINE_SPINLOCK(vn_file_lock
);
43 static LIST_HEAD(vn_file_list
);
46 vn_mode_to_vtype(mode_t mode
)
73 } /* vn_mode_to_vtype() */
74 EXPORT_SYMBOL(vn_mode_to_vtype
);
77 vn_vtype_to_mode(vtype_t vtype
)
101 } /* vn_vtype_to_mode() */
102 EXPORT_SYMBOL(vn_vtype_to_mode
);
110 vp
= kmem_cache_alloc(vn_cache
, flag
);
118 EXPORT_SYMBOL(vn_alloc
);
124 kmem_cache_free(vn_cache
, vp
);
127 EXPORT_SYMBOL(vn_free
);
130 vn_open(const char *path
, uio_seg_t seg
, int flags
, int mode
,
131 vnode_t
**vpp
, int x1
, void *x2
)
135 int rc
, saved_umask
= 0;
140 ASSERT(flags
& (FWRITE
| FREAD
));
141 ASSERT(seg
== UIO_SYSSPACE
);
145 if (!(flags
& FCREAT
) && (flags
& FWRITE
))
148 /* Note for filp_open() the two low bits must be remapped to mean:
149 * 01 - read-only -> 00 read-only
150 * 10 - write-only -> 01 write-only
151 * 11 - read-write -> 10 read-write
156 saved_umask
= xchg(¤t
->fs
->umask
, 0);
158 fp
= filp_open(path
, flags
, mode
);
161 (void)xchg(¤t
->fs
->umask
, saved_umask
);
164 SRETURN(-PTR_ERR(fp
));
166 rc
= vfs_getattr(fp
->f_vfsmnt
, fp
->f_dentry
, &stat
);
172 vp
= vn_alloc(KM_SLEEP
);
178 saved_gfp
= mapping_gfp_mask(fp
->f_mapping
);
179 mapping_set_gfp_mask(fp
->f_mapping
, saved_gfp
& ~(__GFP_IO
|__GFP_FS
));
181 mutex_enter(&vp
->v_lock
);
182 vp
->v_type
= vn_mode_to_vtype(stat
.mode
);
184 vp
->v_gfp_mask
= saved_gfp
;
186 mutex_exit(&vp
->v_lock
);
190 EXPORT_SYMBOL(vn_open
);
193 vn_openat(const char *path
, uio_seg_t seg
, int flags
, int mode
,
194 vnode_t
**vpp
, int x1
, void *x2
, vnode_t
*vp
, int fd
)
200 ASSERT(vp
== rootdir
);
202 len
= strlen(path
) + 2;
203 realpath
= kmalloc(len
, GFP_KERNEL
);
207 (void)snprintf(realpath
, len
, "/%s", path
);
208 rc
= vn_open(realpath
, seg
, flags
, mode
, vpp
, x1
, x2
);
213 EXPORT_SYMBOL(vn_openat
);
216 vn_rdwr(uio_rw_t uio
, vnode_t
*vp
, void *addr
, ssize_t len
, offset_t off
,
217 uio_seg_t seg
, int ioflag
, rlim64_t x2
, void *x3
, ssize_t
*residp
)
220 mm_segment_t saved_fs
;
225 ASSERT(uio
== UIO_WRITE
|| uio
== UIO_READ
);
228 ASSERT(seg
== UIO_SYSSPACE
);
229 ASSERT((ioflag
& ~FAPPEND
) == 0);
230 ASSERT(x2
== RLIM64_INFINITY
);
235 if (ioflag
& FAPPEND
)
238 /* Writable user data segment must be briefly increased for this
239 * process so we can use the user space read call paths to write
240 * in to memory allocated by the kernel. */
245 rc
= vfs_write(fp
, addr
, len
, &offset
);
247 rc
= vfs_read(fp
, addr
, len
, &offset
);
263 EXPORT_SYMBOL(vn_rdwr
);
266 vn_close(vnode_t
*vp
, int flags
, int x1
, int x2
, void *x3
, void *x4
)
274 mapping_set_gfp_mask(vp
->v_file
->f_mapping
, vp
->v_gfp_mask
);
275 rc
= filp_close(vp
->v_file
, 0);
280 EXPORT_SYMBOL(vn_close
);
282 /* vn_seek() does not actually seek it only performs bounds checking on the
283 * proposed seek. We perform minimal checking and allow vn_rdwr() to catch
284 * anything more serious. */
286 vn_seek(vnode_t
*vp
, offset_t ooff
, offset_t
*noffp
, void *ct
)
288 return ((*noffp
< 0 || *noffp
> MAXOFFSET_T
) ? EINVAL
: 0);
290 EXPORT_SYMBOL(vn_seek
);
292 static struct dentry
*
293 vn_lookup_hash(struct nameidata
*nd
)
295 return lookup_one_len((const char *)nd
->last
.name
,
296 nd
->nd_dentry
, nd
->last
.len
);
297 } /* lookup_hash() */
300 vn_path_release(struct nameidata
*nd
)
306 /* Modified do_unlinkat() from linux/fs/namei.c, only uses exported symbols */
308 vn_remove(const char *path
, uio_seg_t seg
, int flags
)
310 struct dentry
*dentry
;
312 struct inode
*inode
= NULL
;
316 ASSERT(seg
== UIO_SYSSPACE
);
317 ASSERT(flags
== RMFILE
);
319 rc
= spl_kern_path_parent(path
, &nd
);
324 if (nd
.last_type
!= LAST_NORM
)
327 spl_inode_lock_nested(nd
.nd_dentry
->d_inode
, I_MUTEX_PARENT
);
328 dentry
= vn_lookup_hash(&nd
);
329 rc
= PTR_ERR(dentry
);
330 if (!IS_ERR(dentry
)) {
331 /* Why not before? Because we want correct rc value */
332 if (nd
.last
.name
[nd
.last
.len
])
335 inode
= dentry
->d_inode
;
337 atomic_inc(&inode
->i_count
);
338 #ifdef HAVE_2ARGS_VFS_UNLINK
339 rc
= vfs_unlink(nd
.nd_dentry
->d_inode
, dentry
);
341 rc
= vfs_unlink(nd
.nd_dentry
->d_inode
, dentry
, nd
.nd_mnt
);
342 #endif /* HAVE_2ARGS_VFS_UNLINK */
347 spl_inode_unlock(nd
.nd_dentry
->d_inode
);
349 iput(inode
); /* truncate the inode here */
351 vn_path_release(&nd
);
356 rc
= !dentry
->d_inode
? -ENOENT
:
357 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
360 EXPORT_SYMBOL(vn_remove
);
362 /* Modified do_rename() from linux/fs/namei.c, only uses exported symbols */
364 vn_rename(const char *oldname
, const char *newname
, int x1
)
366 struct dentry
*old_dir
, *new_dir
;
367 struct dentry
*old_dentry
, *new_dentry
;
369 struct nameidata oldnd
, newnd
;
373 rc
= spl_kern_path_parent(oldname
, &oldnd
);
377 rc
= spl_kern_path_parent(newname
, &newnd
);
382 if (oldnd
.nd_mnt
!= newnd
.nd_mnt
)
385 old_dir
= oldnd
.nd_dentry
;
387 if (oldnd
.last_type
!= LAST_NORM
)
390 new_dir
= newnd
.nd_dentry
;
391 if (newnd
.last_type
!= LAST_NORM
)
394 trap
= lock_rename(new_dir
, old_dir
);
396 old_dentry
= vn_lookup_hash(&oldnd
);
398 rc
= PTR_ERR(old_dentry
);
399 if (IS_ERR(old_dentry
))
402 /* source must exist */
404 if (!old_dentry
->d_inode
)
407 /* unless the source is a directory trailing slashes give -ENOTDIR */
408 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
410 if (oldnd
.last
.name
[oldnd
.last
.len
])
412 if (newnd
.last
.name
[newnd
.last
.len
])
416 /* source should not be ancestor of target */
418 if (old_dentry
== trap
)
421 new_dentry
= vn_lookup_hash(&newnd
);
422 rc
= PTR_ERR(new_dentry
);
423 if (IS_ERR(new_dentry
))
426 /* target should not be an ancestor of source */
428 if (new_dentry
== trap
)
431 #ifdef HAVE_4ARGS_VFS_RENAME
432 rc
= vfs_rename(old_dir
->d_inode
, old_dentry
,
433 new_dir
->d_inode
, new_dentry
);
435 rc
= vfs_rename(old_dir
->d_inode
, old_dentry
, oldnd
.nd_mnt
,
436 new_dir
->d_inode
, new_dentry
, newnd
.nd_mnt
);
437 #endif /* HAVE_4ARGS_VFS_RENAME */
443 unlock_rename(new_dir
, old_dir
);
445 vn_path_release(&newnd
);
447 vn_path_release(&oldnd
);
451 EXPORT_SYMBOL(vn_rename
);
454 vn_getattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, void *x3
, void *x4
)
467 rc
= vfs_getattr(fp
->f_vfsmnt
, fp
->f_dentry
, &stat
);
471 vap
->va_type
= vn_mode_to_vtype(stat
.mode
);
472 vap
->va_mode
= stat
.mode
;
473 vap
->va_uid
= stat
.uid
;
474 vap
->va_gid
= stat
.gid
;
476 vap
->va_nodeid
= stat
.ino
;
477 vap
->va_nlink
= stat
.nlink
;
478 vap
->va_size
= stat
.size
;
479 vap
->va_blksize
= stat
.blksize
;
480 vap
->va_atime
= stat
.atime
;
481 vap
->va_mtime
= stat
.mtime
;
482 vap
->va_ctime
= stat
.ctime
;
483 vap
->va_rdev
= stat
.rdev
;
484 vap
->va_nblocks
= stat
.blocks
;
488 EXPORT_SYMBOL(vn_getattr
);
490 int vn_fsync(vnode_t
*vp
, int flags
, void *x3
, void *x4
)
501 SRETURN(-spl_filp_fsync(vp
->v_file
, datasync
));
503 EXPORT_SYMBOL(vn_fsync
);
505 /* Function must be called while holding the vn_file_lock */
511 ASSERT(spin_is_locked(&vn_file_lock
));
513 list_for_each_entry(fp
, &vn_file_list
, f_list
) {
514 if (fd
== fp
->f_fd
&& fp
->f_task
== current
) {
515 ASSERT(atomic_read(&fp
->f_ref
) != 0);
533 /* Already open just take an extra reference */
534 spin_lock(&vn_file_lock
);
538 atomic_inc(&fp
->f_ref
);
539 spin_unlock(&vn_file_lock
);
543 spin_unlock(&vn_file_lock
);
545 /* File was not yet opened create the object and setup */
546 fp
= kmem_cache_alloc(vn_file_cache
, KM_SLEEP
);
550 mutex_enter(&fp
->f_lock
);
553 fp
->f_task
= current
;
555 atomic_inc(&fp
->f_ref
);
559 SGOTO(out_mutex
, rc
);
561 vp
= vn_alloc(KM_SLEEP
);
565 if (vfs_getattr(lfp
->f_vfsmnt
, lfp
->f_dentry
, &stat
))
566 SGOTO(out_vnode
, rc
);
568 mutex_enter(&vp
->v_lock
);
569 vp
->v_type
= vn_mode_to_vtype(stat
.mode
);
571 mutex_exit(&vp
->v_lock
);
576 /* Put it on the tracking list */
577 spin_lock(&vn_file_lock
);
578 list_add(&fp
->f_list
, &vn_file_list
);
579 spin_unlock(&vn_file_lock
);
581 mutex_exit(&fp
->f_lock
);
589 mutex_exit(&fp
->f_lock
);
590 kmem_cache_free(vn_file_cache
, fp
);
596 static void releasef_locked(file_t
*fp
)
601 /* Unlinked from list, no refs, safe to free outside mutex */
603 vn_free(fp
->f_vnode
);
605 kmem_cache_free(vn_file_cache
, fp
);
614 spin_lock(&vn_file_lock
);
617 atomic_dec(&fp
->f_ref
);
618 if (atomic_read(&fp
->f_ref
) > 0) {
619 spin_unlock(&vn_file_lock
);
624 list_del(&fp
->f_list
);
627 spin_unlock(&vn_file_lock
);
632 EXPORT_SYMBOL(releasef
);
634 #ifndef HAVE_SET_FS_PWD
635 # ifdef HAVE_2ARGS_SET_FS_PWD
636 /* Used from 2.6.25 - 2.6.31+ */
638 set_fs_pwd(struct fs_struct
*fs
, struct path
*path
)
642 # ifdef HAVE_FS_STRUCT_SPINLOCK
643 spin_lock(&fs
->lock
);
647 spin_unlock(&fs
->lock
);
649 write_lock(&fs
->lock
);
653 write_unlock(&fs
->lock
);
654 # endif /* HAVE_FS_STRUCT_SPINLOCK */
660 /* Used from 2.6.11 - 2.6.24 */
662 set_fs_pwd(struct fs_struct
*fs
, struct vfsmount
*mnt
, struct dentry
*dentry
)
664 struct dentry
*old_pwd
;
665 struct vfsmount
*old_pwdmnt
;
667 write_lock(&fs
->lock
);
669 old_pwdmnt
= fs
->pwdmnt
;
670 fs
->pwdmnt
= mntget(mnt
);
671 fs
->pwd
= dget(dentry
);
672 write_unlock(&fs
->lock
);
679 # endif /* HAVE_2ARGS_SET_FS_PWD */
680 #endif /* HAVE_SET_FS_PWD */
683 vn_set_pwd(const char *filename
)
685 #if defined(HAVE_2ARGS_SET_FS_PWD) && defined(HAVE_USER_PATH_DIR)
689 #endif /* HAVE_2ARGS_SET_FS_PWD */
690 mm_segment_t saved_fs
;
695 * user_path_dir() and __user_walk() both expect 'filename' to be
696 * a user space address so we must briefly increase the data segment
697 * size to ensure strncpy_from_user() does not fail with -EFAULT.
702 #ifdef HAVE_2ARGS_SET_FS_PWD
703 # ifdef HAVE_USER_PATH_DIR
704 rc
= user_path_dir(filename
, &path
);
708 rc
= inode_permission(path
.dentry
->d_inode
, MAY_EXEC
| MAY_ACCESS
);
710 SGOTO(dput_and_out
, rc
);
712 set_fs_pwd(current
->fs
, &path
);
717 rc
= __user_walk(filename
,
718 LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
|LOOKUP_CHDIR
, &nd
);
722 rc
= vfs_permission(&nd
, MAY_EXEC
);
724 SGOTO(dput_and_out
, rc
);
726 set_fs_pwd(current
->fs
, &nd
.path
);
730 # endif /* HAVE_USER_PATH_DIR */
732 rc
= __user_walk(filename
,
733 LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
|LOOKUP_CHDIR
, &nd
);
737 rc
= vfs_permission(&nd
, MAY_EXEC
);
739 SGOTO(dput_and_out
, rc
);
741 set_fs_pwd(current
->fs
, nd
.nd_mnt
, nd
.nd_dentry
);
744 vn_path_release(&nd
);
745 #endif /* HAVE_2ARGS_SET_FS_PWD */
751 EXPORT_SYMBOL(vn_set_pwd
);
754 vn_cache_constructor(void *buf
, void *cdrarg
, int kmflags
)
756 struct vnode
*vp
= buf
;
758 mutex_init(&vp
->v_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
761 } /* vn_cache_constructor() */
764 vn_cache_destructor(void *buf
, void *cdrarg
)
766 struct vnode
*vp
= buf
;
768 mutex_destroy(&vp
->v_lock
);
769 } /* vn_cache_destructor() */
772 vn_file_cache_constructor(void *buf
, void *cdrarg
, int kmflags
)
776 atomic_set(&fp
->f_ref
, 0);
777 mutex_init(&fp
->f_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
778 INIT_LIST_HEAD(&fp
->f_list
);
781 } /* file_cache_constructor() */
784 vn_file_cache_destructor(void *buf
, void *cdrarg
)
788 mutex_destroy(&fp
->f_lock
);
789 } /* vn_file_cache_destructor() */
795 vn_cache
= kmem_cache_create("spl_vn_cache",
796 sizeof(struct vnode
), 64,
797 vn_cache_constructor
,
799 NULL
, NULL
, NULL
, KMC_KMEM
);
801 vn_file_cache
= kmem_cache_create("spl_vn_file_cache",
803 vn_file_cache_constructor
,
804 vn_file_cache_destructor
,
805 NULL
, NULL
, NULL
, KMC_KMEM
);
812 file_t
*fp
, *next_fp
;
816 spin_lock(&vn_file_lock
);
818 list_for_each_entry_safe(fp
, next_fp
, &vn_file_list
, f_list
) {
819 list_del(&fp
->f_list
);
824 kmem_cache_destroy(vn_file_cache
);
825 vn_file_cache
= NULL
;
826 spin_unlock(&vn_file_lock
);
829 SWARN("Warning %d files leaked\n", leaked
);
831 kmem_cache_destroy(vn_cache
);