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 spinlock_t vn_file_lock = SPIN_LOCK_UNLOCKED;
43 static LIST_HEAD(vn_file_list);
46 vn_get_sol_type(umode_t mode)
73 } /* vn_get_sol_type() */
81 vp = kmem_cache_alloc(vn_cache, flag);
89 EXPORT_SYMBOL(vn_alloc);
95 kmem_cache_free(vn_cache, vp);
98 EXPORT_SYMBOL(vn_free);
101 vn_open(const char *path, uio_seg_t seg, int flags, int mode,
102 vnode_t **vpp, int x1, void *x2)
106 int rc, saved_umask = 0;
111 ASSERT(flags & (FWRITE | FREAD));
112 ASSERT(seg == UIO_SYSSPACE);
116 if (!(flags & FCREAT) && (flags & FWRITE))
119 /* Note for filp_open() the two low bits must be remapped to mean:
120 * 01 - read-only -> 00 read-only
121 * 10 - write-only -> 01 write-only
122 * 11 - read-write -> 10 read-write
127 saved_umask = xchg(¤t->fs->umask, 0);
129 fp = filp_open(path, flags, mode);
132 (void)xchg(¤t->fs->umask, saved_umask);
135 SRETURN(-PTR_ERR(fp));
137 rc = vfs_getattr(fp->f_vfsmnt, fp->f_dentry, &stat);
143 vp = vn_alloc(KM_SLEEP);
149 saved_gfp = mapping_gfp_mask(fp->f_mapping);
150 mapping_set_gfp_mask(fp->f_mapping, saved_gfp & ~(__GFP_IO|__GFP_FS));
152 mutex_enter(&vp->v_lock);
153 vp->v_type = vn_get_sol_type(stat.mode);
155 vp->v_gfp_mask = saved_gfp;
157 mutex_exit(&vp->v_lock);
161 EXPORT_SYMBOL(vn_open);
164 vn_openat(const char *path, uio_seg_t seg, int flags, int mode,
165 vnode_t **vpp, int x1, void *x2, vnode_t *vp, int fd)
171 ASSERT(vp == rootdir);
173 len = strlen(path) + 2;
174 realpath = kmalloc(len, GFP_KERNEL);
178 (void)snprintf(realpath, len, "/%s", path);
179 rc = vn_open(realpath, seg, flags, mode, vpp, x1, x2);
184 EXPORT_SYMBOL(vn_openat);
187 vn_rdwr(uio_rw_t uio, vnode_t *vp, void *addr, ssize_t len, offset_t off,
188 uio_seg_t seg, int ioflag, rlim64_t x2, void *x3, ssize_t *residp)
191 mm_segment_t saved_fs;
196 ASSERT(uio == UIO_WRITE || uio == UIO_READ);
199 ASSERT(seg == UIO_SYSSPACE);
200 ASSERT((ioflag & ~FAPPEND) == 0);
201 ASSERT(x2 == RLIM64_INFINITY);
206 if (ioflag & FAPPEND)
209 /* Writable user data segment must be briefly increased for this
210 * process so we can use the user space read call paths to write
211 * in to memory allocated by the kernel. */
216 rc = vfs_write(fp, addr, len, &offset);
218 rc = vfs_read(fp, addr, len, &offset);
234 EXPORT_SYMBOL(vn_rdwr);
237 vn_close(vnode_t *vp, int flags, int x1, int x2, void *x3, void *x4)
245 mapping_set_gfp_mask(vp->v_file->f_mapping, vp->v_gfp_mask);
246 rc = filp_close(vp->v_file, 0);
251 EXPORT_SYMBOL(vn_close);
253 /* vn_seek() does not actually seek it only performs bounds checking on the
254 * proposed seek. We perform minimal checking and allow vn_rdwr() to catch
255 * anything more serious. */
257 vn_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
259 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
261 EXPORT_SYMBOL(vn_seek);
263 static struct dentry *
264 vn_lookup_hash(struct nameidata *nd)
266 return lookup_one_len((const char *)nd->last.name,
267 nd->nd_dentry, nd->last.len);
268 } /* lookup_hash() */
271 vn_path_release(struct nameidata *nd)
277 /* Modified do_unlinkat() from linux/fs/namei.c, only uses exported symbols */
279 vn_remove(const char *path, uio_seg_t seg, int flags)
281 struct dentry *dentry;
283 struct inode *inode = NULL;
287 ASSERT(seg == UIO_SYSSPACE);
288 ASSERT(flags == RMFILE);
290 rc = path_lookup(path, LOOKUP_PARENT, &nd);
295 if (nd.last_type != LAST_NORM)
298 #ifdef HAVE_INODE_I_MUTEX
299 mutex_lock_nested(&nd.nd_dentry->d_inode->i_mutex, I_MUTEX_PARENT);
301 down(&nd.nd_dentry->d_inode->i_sem);
302 #endif /* HAVE_INODE_I_MUTEX */
303 dentry = vn_lookup_hash(&nd);
304 rc = PTR_ERR(dentry);
305 if (!IS_ERR(dentry)) {
306 /* Why not before? Because we want correct rc value */
307 if (nd.last.name[nd.last.len])
310 inode = dentry->d_inode;
312 atomic_inc(&inode->i_count);
313 #ifdef HAVE_2ARGS_VFS_UNLINK
314 rc = vfs_unlink(nd.nd_dentry->d_inode, dentry);
316 rc = vfs_unlink(nd.nd_dentry->d_inode, dentry, nd.nd_mnt);
317 #endif /* HAVE_2ARGS_VFS_UNLINK */
321 #ifdef HAVE_INODE_I_MUTEX
322 mutex_unlock(&nd.nd_dentry->d_inode->i_mutex);
324 up(&nd.nd_dentry->d_inode->i_sem);
325 #endif /* HAVE_INODE_I_MUTEX */
327 iput(inode); /* truncate the inode here */
329 vn_path_release(&nd);
334 rc = !dentry->d_inode ? -ENOENT :
335 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
338 EXPORT_SYMBOL(vn_remove);
340 /* Modified do_rename() from linux/fs/namei.c, only uses exported symbols */
342 vn_rename(const char *oldname, const char *newname, int x1)
344 struct dentry *old_dir, *new_dir;
345 struct dentry *old_dentry, *new_dentry;
347 struct nameidata oldnd, newnd;
351 rc = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
355 rc = path_lookup(newname, LOOKUP_PARENT, &newnd);
360 if (oldnd.nd_mnt != newnd.nd_mnt)
363 old_dir = oldnd.nd_dentry;
365 if (oldnd.last_type != LAST_NORM)
368 new_dir = newnd.nd_dentry;
369 if (newnd.last_type != LAST_NORM)
372 trap = lock_rename(new_dir, old_dir);
374 old_dentry = vn_lookup_hash(&oldnd);
376 rc = PTR_ERR(old_dentry);
377 if (IS_ERR(old_dentry))
380 /* source must exist */
382 if (!old_dentry->d_inode)
385 /* unless the source is a directory trailing slashes give -ENOTDIR */
386 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
388 if (oldnd.last.name[oldnd.last.len])
390 if (newnd.last.name[newnd.last.len])
394 /* source should not be ancestor of target */
396 if (old_dentry == trap)
399 new_dentry = vn_lookup_hash(&newnd);
400 rc = PTR_ERR(new_dentry);
401 if (IS_ERR(new_dentry))
404 /* target should not be an ancestor of source */
406 if (new_dentry == trap)
409 #ifdef HAVE_4ARGS_VFS_RENAME
410 rc = vfs_rename(old_dir->d_inode, old_dentry,
411 new_dir->d_inode, new_dentry);
413 rc = vfs_rename(old_dir->d_inode, old_dentry, oldnd.nd_mnt,
414 new_dir->d_inode, new_dentry, newnd.nd_mnt);
415 #endif /* HAVE_4ARGS_VFS_RENAME */
421 unlock_rename(new_dir, old_dir);
423 vn_path_release(&newnd);
425 vn_path_release(&oldnd);
429 EXPORT_SYMBOL(vn_rename);
432 vn_getattr(vnode_t *vp, vattr_t *vap, int flags, void *x3, void *x4)
445 rc = vfs_getattr(fp->f_vfsmnt, fp->f_dentry, &stat);
449 vap->va_type = vn_get_sol_type(stat.mode);
450 vap->va_mode = stat.mode;
451 vap->va_uid = stat.uid;
452 vap->va_gid = stat.gid;
454 vap->va_nodeid = stat.ino;
455 vap->va_nlink = stat.nlink;
456 vap->va_size = stat.size;
457 vap->va_blocksize = stat.blksize;
458 vap->va_atime.tv_sec = stat.atime.tv_sec;
459 vap->va_atime.tv_usec = stat.atime.tv_nsec / NSEC_PER_USEC;
460 vap->va_mtime.tv_sec = stat.mtime.tv_sec;
461 vap->va_mtime.tv_usec = stat.mtime.tv_nsec / NSEC_PER_USEC;
462 vap->va_ctime.tv_sec = stat.ctime.tv_sec;
463 vap->va_ctime.tv_usec = stat.ctime.tv_nsec / NSEC_PER_USEC;
464 vap->va_rdev = stat.rdev;
465 vap->va_blocks = stat.blocks;
469 EXPORT_SYMBOL(vn_getattr);
471 int vn_fsync(vnode_t *vp, int flags, void *x3, void *x4)
482 SRETURN(-spl_filp_fsync(vp->v_file, datasync));
484 EXPORT_SYMBOL(vn_fsync);
486 /* Function must be called while holding the vn_file_lock */
492 ASSERT(spin_is_locked(&vn_file_lock));
494 list_for_each_entry(fp, &vn_file_list, f_list) {
495 if (fd == fp->f_fd) {
496 ASSERT(atomic_read(&fp->f_ref) != 0);
514 /* Already open just take an extra reference */
515 spin_lock(&vn_file_lock);
519 atomic_inc(&fp->f_ref);
520 spin_unlock(&vn_file_lock);
524 spin_unlock(&vn_file_lock);
526 /* File was not yet opened create the object and setup */
527 fp = kmem_cache_alloc(vn_file_cache, KM_SLEEP);
531 mutex_enter(&fp->f_lock);
535 atomic_inc(&fp->f_ref);
539 SGOTO(out_mutex, rc);
541 vp = vn_alloc(KM_SLEEP);
545 if (vfs_getattr(lfp->f_vfsmnt, lfp->f_dentry, &stat))
546 SGOTO(out_vnode, rc);
548 mutex_enter(&vp->v_lock);
549 vp->v_type = vn_get_sol_type(stat.mode);
551 mutex_exit(&vp->v_lock);
556 /* Put it on the tracking list */
557 spin_lock(&vn_file_lock);
558 list_add(&fp->f_list, &vn_file_list);
559 spin_unlock(&vn_file_lock);
561 mutex_exit(&fp->f_lock);
569 mutex_exit(&fp->f_lock);
570 kmem_cache_free(vn_file_cache, fp);
576 static void releasef_locked(file_t *fp)
581 /* Unlinked from list, no refs, safe to free outside mutex */
583 vn_free(fp->f_vnode);
585 kmem_cache_free(vn_file_cache, fp);
594 spin_lock(&vn_file_lock);
597 atomic_dec(&fp->f_ref);
598 if (atomic_read(&fp->f_ref) > 0) {
599 spin_unlock(&vn_file_lock);
604 list_del(&fp->f_list);
607 spin_unlock(&vn_file_lock);
612 EXPORT_SYMBOL(releasef);
614 #ifndef HAVE_SET_FS_PWD
615 # ifdef HAVE_2ARGS_SET_FS_PWD
616 /* Used from 2.6.25 - 2.6.31+ */
618 set_fs_pwd(struct fs_struct *fs, struct path *path)
622 # ifdef HAVE_FS_STRUCT_SPINLOCK
623 spin_lock(&fs->lock);
627 spin_unlock(&fs->lock);
629 write_lock(&fs->lock);
633 write_unlock(&fs->lock);
634 # endif /* HAVE_FS_STRUCT_SPINLOCK */
640 /* Used from 2.6.11 - 2.6.24 */
642 set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt, struct dentry *dentry)
644 struct dentry *old_pwd;
645 struct vfsmount *old_pwdmnt;
647 write_lock(&fs->lock);
649 old_pwdmnt = fs->pwdmnt;
650 fs->pwdmnt = mntget(mnt);
651 fs->pwd = dget(dentry);
652 write_unlock(&fs->lock);
659 # endif /* HAVE_2ARGS_SET_FS_PWD */
660 #endif /* HAVE_SET_FS_PWD */
663 vn_set_pwd(const char *filename)
665 #if defined(HAVE_2ARGS_SET_FS_PWD) && defined(HAVE_USER_PATH_DIR)
669 #endif /* HAVE_2ARGS_SET_FS_PWD */
670 mm_segment_t saved_fs;
675 * user_path_dir() and __user_walk() both expect 'filename' to be
676 * a user space address so we must briefly increase the data segment
677 * size to ensure strncpy_from_user() does not fail with -EFAULT.
682 #ifdef HAVE_2ARGS_SET_FS_PWD
683 # ifdef HAVE_USER_PATH_DIR
684 rc = user_path_dir(filename, &path);
688 rc = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_ACCESS);
690 SGOTO(dput_and_out, rc);
692 set_fs_pwd(current->fs, &path);
697 rc = __user_walk(filename,
698 LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_CHDIR, &nd);
702 rc = vfs_permission(&nd, MAY_EXEC);
704 SGOTO(dput_and_out, rc);
706 set_fs_pwd(current->fs, &nd.path);
710 # endif /* HAVE_USER_PATH_DIR */
712 rc = __user_walk(filename,
713 LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_CHDIR, &nd);
717 rc = vfs_permission(&nd, MAY_EXEC);
719 SGOTO(dput_and_out, rc);
721 set_fs_pwd(current->fs, nd.nd_mnt, nd.nd_dentry);
724 vn_path_release(&nd);
725 #endif /* HAVE_2ARGS_SET_FS_PWD */
731 EXPORT_SYMBOL(vn_set_pwd);
734 vn_cache_constructor(void *buf, void *cdrarg, int kmflags)
736 struct vnode *vp = buf;
738 mutex_init(&vp->v_lock, NULL, MUTEX_DEFAULT, NULL);
741 } /* vn_cache_constructor() */
744 vn_cache_destructor(void *buf, void *cdrarg)
746 struct vnode *vp = buf;
748 mutex_destroy(&vp->v_lock);
749 } /* vn_cache_destructor() */
752 vn_file_cache_constructor(void *buf, void *cdrarg, int kmflags)
756 atomic_set(&fp->f_ref, 0);
757 mutex_init(&fp->f_lock, NULL, MUTEX_DEFAULT, NULL);
758 INIT_LIST_HEAD(&fp->f_list);
761 } /* file_cache_constructor() */
764 vn_file_cache_destructor(void *buf, void *cdrarg)
768 mutex_destroy(&fp->f_lock);
769 } /* vn_file_cache_destructor() */
775 vn_cache = kmem_cache_create("spl_vn_cache",
776 sizeof(struct vnode), 64,
777 vn_cache_constructor,
779 NULL, NULL, NULL, 0);
781 vn_file_cache = kmem_cache_create("spl_vn_file_cache",
783 vn_file_cache_constructor,
784 vn_file_cache_destructor,
785 NULL, NULL, NULL, 0);
792 file_t *fp, *next_fp;
796 spin_lock(&vn_file_lock);
798 list_for_each_entry_safe(fp, next_fp, &vn_file_list, f_list) {
799 list_del(&fp->f_list);
804 kmem_cache_destroy(vn_file_cache);
805 vn_file_cache = NULL;
806 spin_unlock(&vn_file_lock);
809 SWARN("Warning %d files leaked\n", leaked);
811 kmem_cache_destroy(vn_cache);