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
);
264 EXPORT_SYMBOL(vn_rdwr
);
267 vn_close(vnode_t
*vp
, int flags
, int x1
, int x2
, void *x3
, void *x4
)
275 mapping_set_gfp_mask(vp
->v_file
->f_mapping
, vp
->v_gfp_mask
);
276 rc
= filp_close(vp
->v_file
, 0);
281 EXPORT_SYMBOL(vn_close
);
283 /* vn_seek() does not actually seek it only performs bounds checking on the
284 * proposed seek. We perform minimal checking and allow vn_rdwr() to catch
285 * anything more serious. */
287 vn_seek(vnode_t
*vp
, offset_t ooff
, offset_t
*noffp
, void *ct
)
289 return ((*noffp
< 0 || *noffp
> MAXOFFSET_T
) ? EINVAL
: 0);
291 EXPORT_SYMBOL(vn_seek
);
293 static struct dentry
*
294 vn_lookup_hash(struct nameidata
*nd
)
296 return lookup_one_len((const char *)nd
->last
.name
,
297 nd
->nd_dentry
, nd
->last
.len
);
298 } /* lookup_hash() */
301 vn_path_release(struct nameidata
*nd
)
307 /* Modified do_unlinkat() from linux/fs/namei.c, only uses exported symbols */
309 vn_remove(const char *path
, uio_seg_t seg
, int flags
)
311 struct dentry
*dentry
;
313 struct inode
*inode
= NULL
;
317 ASSERT(seg
== UIO_SYSSPACE
);
318 ASSERT(flags
== RMFILE
);
320 rc
= spl_kern_path_parent(path
, &nd
);
325 if (nd
.last_type
!= LAST_NORM
)
328 spl_inode_lock_nested(nd
.nd_dentry
->d_inode
, I_MUTEX_PARENT
);
329 dentry
= vn_lookup_hash(&nd
);
330 rc
= PTR_ERR(dentry
);
331 if (!IS_ERR(dentry
)) {
332 /* Why not before? Because we want correct rc value */
333 if (nd
.last
.name
[nd
.last
.len
])
336 inode
= dentry
->d_inode
;
338 atomic_inc(&inode
->i_count
);
339 #ifdef HAVE_2ARGS_VFS_UNLINK
340 rc
= vfs_unlink(nd
.nd_dentry
->d_inode
, dentry
);
342 rc
= vfs_unlink(nd
.nd_dentry
->d_inode
, dentry
, nd
.nd_mnt
);
343 #endif /* HAVE_2ARGS_VFS_UNLINK */
348 spl_inode_unlock(nd
.nd_dentry
->d_inode
);
350 iput(inode
); /* truncate the inode here */
352 vn_path_release(&nd
);
357 rc
= !dentry
->d_inode
? -ENOENT
:
358 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
361 EXPORT_SYMBOL(vn_remove
);
363 /* Modified do_rename() from linux/fs/namei.c, only uses exported symbols */
365 vn_rename(const char *oldname
, const char *newname
, int x1
)
367 struct dentry
*old_dir
, *new_dir
;
368 struct dentry
*old_dentry
, *new_dentry
;
370 struct nameidata oldnd
, newnd
;
374 rc
= spl_kern_path_parent(oldname
, &oldnd
);
378 rc
= spl_kern_path_parent(newname
, &newnd
);
383 if (oldnd
.nd_mnt
!= newnd
.nd_mnt
)
386 old_dir
= oldnd
.nd_dentry
;
388 if (oldnd
.last_type
!= LAST_NORM
)
391 new_dir
= newnd
.nd_dentry
;
392 if (newnd
.last_type
!= LAST_NORM
)
395 trap
= lock_rename(new_dir
, old_dir
);
397 old_dentry
= vn_lookup_hash(&oldnd
);
399 rc
= PTR_ERR(old_dentry
);
400 if (IS_ERR(old_dentry
))
403 /* source must exist */
405 if (!old_dentry
->d_inode
)
408 /* unless the source is a directory trailing slashes give -ENOTDIR */
409 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
411 if (oldnd
.last
.name
[oldnd
.last
.len
])
413 if (newnd
.last
.name
[newnd
.last
.len
])
417 /* source should not be ancestor of target */
419 if (old_dentry
== trap
)
422 new_dentry
= vn_lookup_hash(&newnd
);
423 rc
= PTR_ERR(new_dentry
);
424 if (IS_ERR(new_dentry
))
427 /* target should not be an ancestor of source */
429 if (new_dentry
== trap
)
432 #ifdef HAVE_4ARGS_VFS_RENAME
433 rc
= vfs_rename(old_dir
->d_inode
, old_dentry
,
434 new_dir
->d_inode
, new_dentry
);
436 rc
= vfs_rename(old_dir
->d_inode
, old_dentry
, oldnd
.nd_mnt
,
437 new_dir
->d_inode
, new_dentry
, newnd
.nd_mnt
);
438 #endif /* HAVE_4ARGS_VFS_RENAME */
444 unlock_rename(new_dir
, old_dir
);
446 vn_path_release(&newnd
);
448 vn_path_release(&oldnd
);
452 EXPORT_SYMBOL(vn_rename
);
455 vn_getattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, void *x3
, void *x4
)
468 rc
= vfs_getattr(fp
->f_vfsmnt
, fp
->f_dentry
, &stat
);
472 vap
->va_type
= vn_mode_to_vtype(stat
.mode
);
473 vap
->va_mode
= stat
.mode
;
474 vap
->va_uid
= stat
.uid
;
475 vap
->va_gid
= stat
.gid
;
477 vap
->va_nodeid
= stat
.ino
;
478 vap
->va_nlink
= stat
.nlink
;
479 vap
->va_size
= stat
.size
;
480 vap
->va_blksize
= stat
.blksize
;
481 vap
->va_atime
= stat
.atime
;
482 vap
->va_mtime
= stat
.mtime
;
483 vap
->va_ctime
= stat
.ctime
;
484 vap
->va_rdev
= stat
.rdev
;
485 vap
->va_nblocks
= stat
.blocks
;
489 EXPORT_SYMBOL(vn_getattr
);
491 int vn_fsync(vnode_t
*vp
, int flags
, void *x3
, void *x4
)
502 SRETURN(-spl_filp_fsync(vp
->v_file
, datasync
));
504 EXPORT_SYMBOL(vn_fsync
);
506 /* Function must be called while holding the vn_file_lock */
512 ASSERT(spin_is_locked(&vn_file_lock
));
514 list_for_each_entry(fp
, &vn_file_list
, f_list
) {
515 if (fd
== fp
->f_fd
&& fp
->f_task
== current
) {
516 ASSERT(atomic_read(&fp
->f_ref
) != 0);
534 /* Already open just take an extra reference */
535 spin_lock(&vn_file_lock
);
539 atomic_inc(&fp
->f_ref
);
540 spin_unlock(&vn_file_lock
);
544 spin_unlock(&vn_file_lock
);
546 /* File was not yet opened create the object and setup */
547 fp
= kmem_cache_alloc(vn_file_cache
, KM_SLEEP
);
551 mutex_enter(&fp
->f_lock
);
554 fp
->f_task
= current
;
556 atomic_inc(&fp
->f_ref
);
560 SGOTO(out_mutex
, rc
);
562 vp
= vn_alloc(KM_SLEEP
);
566 if (vfs_getattr(lfp
->f_vfsmnt
, lfp
->f_dentry
, &stat
))
567 SGOTO(out_vnode
, rc
);
569 mutex_enter(&vp
->v_lock
);
570 vp
->v_type
= vn_mode_to_vtype(stat
.mode
);
572 mutex_exit(&vp
->v_lock
);
577 /* Put it on the tracking list */
578 spin_lock(&vn_file_lock
);
579 list_add(&fp
->f_list
, &vn_file_list
);
580 spin_unlock(&vn_file_lock
);
582 mutex_exit(&fp
->f_lock
);
590 mutex_exit(&fp
->f_lock
);
591 kmem_cache_free(vn_file_cache
, fp
);
597 static void releasef_locked(file_t
*fp
)
602 /* Unlinked from list, no refs, safe to free outside mutex */
604 vn_free(fp
->f_vnode
);
606 kmem_cache_free(vn_file_cache
, fp
);
615 spin_lock(&vn_file_lock
);
618 atomic_dec(&fp
->f_ref
);
619 if (atomic_read(&fp
->f_ref
) > 0) {
620 spin_unlock(&vn_file_lock
);
625 list_del(&fp
->f_list
);
628 spin_unlock(&vn_file_lock
);
633 EXPORT_SYMBOL(releasef
);
635 #ifndef HAVE_SET_FS_PWD
636 # ifdef HAVE_2ARGS_SET_FS_PWD
637 /* Used from 2.6.25 - 2.6.31+ */
639 set_fs_pwd(struct fs_struct
*fs
, struct path
*path
)
643 # ifdef HAVE_FS_STRUCT_SPINLOCK
644 spin_lock(&fs
->lock
);
648 spin_unlock(&fs
->lock
);
650 write_lock(&fs
->lock
);
654 write_unlock(&fs
->lock
);
655 # endif /* HAVE_FS_STRUCT_SPINLOCK */
661 /* Used from 2.6.11 - 2.6.24 */
663 set_fs_pwd(struct fs_struct
*fs
, struct vfsmount
*mnt
, struct dentry
*dentry
)
665 struct dentry
*old_pwd
;
666 struct vfsmount
*old_pwdmnt
;
668 write_lock(&fs
->lock
);
670 old_pwdmnt
= fs
->pwdmnt
;
671 fs
->pwdmnt
= mntget(mnt
);
672 fs
->pwd
= dget(dentry
);
673 write_unlock(&fs
->lock
);
680 # endif /* HAVE_2ARGS_SET_FS_PWD */
681 #endif /* HAVE_SET_FS_PWD */
684 vn_set_pwd(const char *filename
)
686 #if defined(HAVE_2ARGS_SET_FS_PWD) && defined(HAVE_USER_PATH_DIR)
690 #endif /* HAVE_2ARGS_SET_FS_PWD */
691 mm_segment_t saved_fs
;
696 * user_path_dir() and __user_walk() both expect 'filename' to be
697 * a user space address so we must briefly increase the data segment
698 * size to ensure strncpy_from_user() does not fail with -EFAULT.
703 #ifdef HAVE_2ARGS_SET_FS_PWD
704 # ifdef HAVE_USER_PATH_DIR
705 rc
= user_path_dir(filename
, &path
);
709 rc
= inode_permission(path
.dentry
->d_inode
, MAY_EXEC
| MAY_ACCESS
);
711 SGOTO(dput_and_out
, rc
);
713 set_fs_pwd(current
->fs
, &path
);
718 rc
= __user_walk(filename
,
719 LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
|LOOKUP_CHDIR
, &nd
);
723 rc
= vfs_permission(&nd
, MAY_EXEC
);
725 SGOTO(dput_and_out
, rc
);
727 set_fs_pwd(current
->fs
, &nd
.path
);
731 # endif /* HAVE_USER_PATH_DIR */
733 rc
= __user_walk(filename
,
734 LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
|LOOKUP_CHDIR
, &nd
);
738 rc
= vfs_permission(&nd
, MAY_EXEC
);
740 SGOTO(dput_and_out
, rc
);
742 set_fs_pwd(current
->fs
, nd
.nd_mnt
, nd
.nd_dentry
);
745 vn_path_release(&nd
);
746 #endif /* HAVE_2ARGS_SET_FS_PWD */
752 EXPORT_SYMBOL(vn_set_pwd
);
755 vn_cache_constructor(void *buf
, void *cdrarg
, int kmflags
)
757 struct vnode
*vp
= buf
;
759 mutex_init(&vp
->v_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
762 } /* vn_cache_constructor() */
765 vn_cache_destructor(void *buf
, void *cdrarg
)
767 struct vnode
*vp
= buf
;
769 mutex_destroy(&vp
->v_lock
);
770 } /* vn_cache_destructor() */
773 vn_file_cache_constructor(void *buf
, void *cdrarg
, int kmflags
)
777 atomic_set(&fp
->f_ref
, 0);
778 mutex_init(&fp
->f_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
779 INIT_LIST_HEAD(&fp
->f_list
);
782 } /* file_cache_constructor() */
785 vn_file_cache_destructor(void *buf
, void *cdrarg
)
789 mutex_destroy(&fp
->f_lock
);
790 } /* vn_file_cache_destructor() */
796 vn_cache
= kmem_cache_create("spl_vn_cache",
797 sizeof(struct vnode
), 64,
798 vn_cache_constructor
,
800 NULL
, NULL
, NULL
, KMC_KMEM
);
802 vn_file_cache
= kmem_cache_create("spl_vn_file_cache",
804 vn_file_cache_constructor
,
805 vn_file_cache_destructor
,
806 NULL
, NULL
, NULL
, KMC_KMEM
);
813 file_t
*fp
, *next_fp
;
817 spin_lock(&vn_file_lock
);
819 list_for_each_entry_safe(fp
, next_fp
, &vn_file_list
, f_list
) {
820 list_del(&fp
->f_list
);
825 kmem_cache_destroy(vn_file_cache
);
826 vn_file_cache
= NULL
;
827 spin_unlock(&vn_file_lock
);
830 SWARN("Warning %d files leaked\n", leaked
);
832 kmem_cache_destroy(vn_cache
);