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 \*****************************************************************************/
27 #include <sys/vnode.h>
28 #include <linux/falloc.h>
29 #include <spl-debug.h>
31 #ifdef SS_DEBUG_SUBSYS
32 #undef SS_DEBUG_SUBSYS
35 #define SS_DEBUG_SUBSYS SS_VNODE
37 vnode_t *rootdir = (vnode_t *)0xabcd1234;
38 EXPORT_SYMBOL(rootdir);
40 static spl_kmem_cache_t *vn_cache;
41 static spl_kmem_cache_t *vn_file_cache;
43 static DEFINE_SPINLOCK(vn_file_lock);
44 static LIST_HEAD(vn_file_list);
46 #ifdef HAVE_KERN_PATH_PARENT_HEADER
47 #ifndef HAVE_KERN_PATH_PARENT_SYMBOL
48 kern_path_parent_t kern_path_parent_fn = SYMBOL_POISON;
49 EXPORT_SYMBOL(kern_path_parent_fn);
50 #endif /* HAVE_KERN_PATH_PARENT_SYMBOL */
51 #endif /* HAVE_KERN_PATH_PARENT_HEADER */
53 #ifdef HAVE_KERN_PATH_LOCKED
54 kern_path_locked_t kern_path_locked_fn = SYMBOL_POISON;
55 #endif /* HAVE_KERN_PATH_LOCKED */
58 vn_mode_to_vtype(mode_t mode)
85 } /* vn_mode_to_vtype() */
86 EXPORT_SYMBOL(vn_mode_to_vtype);
89 vn_vtype_to_mode(vtype_t vtype)
113 } /* vn_vtype_to_mode() */
114 EXPORT_SYMBOL(vn_vtype_to_mode);
122 vp = kmem_cache_alloc(vn_cache, flag);
130 EXPORT_SYMBOL(vn_alloc);
136 kmem_cache_free(vn_cache, vp);
139 EXPORT_SYMBOL(vn_free);
142 vn_open(const char *path, uio_seg_t seg, int flags, int mode,
143 vnode_t **vpp, int x1, void *x2)
147 int rc, saved_umask = 0;
152 ASSERT(flags & (FWRITE | FREAD));
153 ASSERT(seg == UIO_SYSSPACE);
157 if (!(flags & FCREAT) && (flags & FWRITE))
160 /* Note for filp_open() the two low bits must be remapped to mean:
161 * 01 - read-only -> 00 read-only
162 * 10 - write-only -> 01 write-only
163 * 11 - read-write -> 10 read-write
168 saved_umask = xchg(¤t->fs->umask, 0);
170 fp = filp_open(path, flags, mode);
173 (void)xchg(¤t->fs->umask, saved_umask);
176 SRETURN(-PTR_ERR(fp));
178 #ifdef HAVE_2ARGS_VFS_GETATTR
179 rc = vfs_getattr(&fp->f_path, &stat);
181 rc = vfs_getattr(fp->f_path.mnt, fp->f_dentry, &stat);
188 vp = vn_alloc(KM_SLEEP);
194 saved_gfp = mapping_gfp_mask(fp->f_mapping);
195 mapping_set_gfp_mask(fp->f_mapping, saved_gfp & ~(__GFP_IO|__GFP_FS));
197 mutex_enter(&vp->v_lock);
198 vp->v_type = vn_mode_to_vtype(stat.mode);
200 vp->v_gfp_mask = saved_gfp;
202 mutex_exit(&vp->v_lock);
206 EXPORT_SYMBOL(vn_open);
209 vn_openat(const char *path, uio_seg_t seg, int flags, int mode,
210 vnode_t **vpp, int x1, void *x2, vnode_t *vp, int fd)
216 ASSERT(vp == rootdir);
218 len = strlen(path) + 2;
219 realpath = kmalloc(len, GFP_KERNEL);
223 (void)snprintf(realpath, len, "/%s", path);
224 rc = vn_open(realpath, seg, flags, mode, vpp, x1, x2);
229 EXPORT_SYMBOL(vn_openat);
232 vn_rdwr(uio_rw_t uio, vnode_t *vp, void *addr, ssize_t len, offset_t off,
233 uio_seg_t seg, int ioflag, rlim64_t x2, void *x3, ssize_t *residp)
236 mm_segment_t saved_fs;
241 ASSERT(uio == UIO_WRITE || uio == UIO_READ);
244 ASSERT(seg == UIO_SYSSPACE);
245 ASSERT((ioflag & ~FAPPEND) == 0);
246 ASSERT(x2 == RLIM64_INFINITY);
251 if (ioflag & FAPPEND)
254 /* Writable user data segment must be briefly increased for this
255 * process so we can use the user space read call paths to write
256 * in to memory allocated by the kernel. */
261 rc = vfs_write(fp, addr, len, &offset);
263 rc = vfs_read(fp, addr, len, &offset);
280 EXPORT_SYMBOL(vn_rdwr);
283 vn_close(vnode_t *vp, int flags, int x1, int x2, void *x3, void *x4)
291 mapping_set_gfp_mask(vp->v_file->f_mapping, vp->v_gfp_mask);
292 rc = filp_close(vp->v_file, 0);
297 EXPORT_SYMBOL(vn_close);
299 /* vn_seek() does not actually seek it only performs bounds checking on the
300 * proposed seek. We perform minimal checking and allow vn_rdwr() to catch
301 * anything more serious. */
303 vn_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, void *ct)
305 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
307 EXPORT_SYMBOL(vn_seek);
309 #ifdef HAVE_KERN_PATH_LOCKED
310 /* Based on do_unlinkat() from linux/fs/namei.c */
312 vn_remove(const char *path, uio_seg_t seg, int flags)
314 struct dentry *dentry;
316 struct inode *inode = NULL;
320 ASSERT(seg == UIO_SYSSPACE);
321 ASSERT(flags == RMFILE);
323 dentry = spl_kern_path_locked(path, &parent);
324 rc = PTR_ERR(dentry);
325 if (!IS_ERR(dentry)) {
326 if (parent.dentry->d_name.name[parent.dentry->d_name.len])
327 SGOTO(slashes, rc = 0);
329 inode = dentry->d_inode;
331 SGOTO(slashes, rc = 0);
336 rc = vfs_unlink(parent.dentry->d_inode, dentry);
343 spl_inode_unlock(parent.dentry->d_inode);
345 iput(inode); /* truncate the inode here */
351 rc = !dentry->d_inode ? -ENOENT :
352 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
355 EXPORT_SYMBOL(vn_remove);
357 /* Based on do_rename() from linux/fs/namei.c */
359 vn_rename(const char *oldname, const char *newname, int x1)
361 struct dentry *old_dir, *new_dir;
362 struct dentry *old_dentry, *new_dentry;
364 struct path old_parent, new_parent;
368 old_dentry = spl_kern_path_locked(oldname, &old_parent);
369 if (IS_ERR(old_dentry))
370 SGOTO(exit, rc = PTR_ERR(old_dentry));
372 spl_inode_unlock(old_parent.dentry->d_inode);
374 new_dentry = spl_kern_path_locked(newname, &new_parent);
375 if (IS_ERR(new_dentry))
376 SGOTO(exit2, rc = PTR_ERR(new_dentry));
378 spl_inode_unlock(new_parent.dentry->d_inode);
381 if (old_parent.mnt != new_parent.mnt)
384 old_dir = old_parent.dentry;
385 new_dir = new_parent.dentry;
386 trap = lock_rename(new_dir, old_dir);
388 /* source should not be ancestor of target */
390 if (old_dentry == trap)
393 /* target should not be an ancestor of source */
395 if (new_dentry == trap)
398 /* source must exist */
400 if (!old_dentry->d_inode)
403 /* unless the source is a directory trailing slashes give -ENOTDIR */
404 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
406 if (old_dentry->d_name.name[old_dentry->d_name.len])
408 if (new_dentry->d_name.name[new_dentry->d_name.len])
412 #ifdef HAVE_4ARGS_VFS_RENAME
413 rc = vfs_rename(old_dir->d_inode, old_dentry,
414 new_dir->d_inode, new_dentry);
416 rc = vfs_rename(old_dir->d_inode, old_dentry, oldnd.nd_mnt,
417 new_dir->d_inode, new_dentry, newnd.nd_mnt);
418 #endif /* HAVE_4ARGS_VFS_RENAME */
420 unlock_rename(new_dir, old_dir);
423 path_put(&new_parent);
426 path_put(&old_parent);
430 EXPORT_SYMBOL(vn_rename);
433 static struct dentry *
434 vn_lookup_hash(struct nameidata *nd)
436 return lookup_one_len((const char *)nd->last.name,
437 nd->nd_dentry, nd->last.len);
438 } /* lookup_hash() */
441 vn_path_release(struct nameidata *nd)
447 /* Modified do_unlinkat() from linux/fs/namei.c, only uses exported symbols */
449 vn_remove(const char *path, uio_seg_t seg, int flags)
451 struct dentry *dentry;
453 struct inode *inode = NULL;
457 ASSERT(seg == UIO_SYSSPACE);
458 ASSERT(flags == RMFILE);
460 rc = spl_kern_path_parent(path, &nd);
465 if (nd.last_type != LAST_NORM)
468 spl_inode_lock_nested(nd.nd_dentry->d_inode, I_MUTEX_PARENT);
469 dentry = vn_lookup_hash(&nd);
470 rc = PTR_ERR(dentry);
471 if (!IS_ERR(dentry)) {
472 /* Why not before? Because we want correct rc value */
473 if (nd.last.name[nd.last.len])
476 inode = dentry->d_inode;
478 atomic_inc(&inode->i_count);
479 #ifdef HAVE_2ARGS_VFS_UNLINK
480 rc = vfs_unlink(nd.nd_dentry->d_inode, dentry);
482 rc = vfs_unlink(nd.nd_dentry->d_inode, dentry, nd.nd_mnt);
483 #endif /* HAVE_2ARGS_VFS_UNLINK */
488 spl_inode_unlock(nd.nd_dentry->d_inode);
490 iput(inode); /* truncate the inode here */
492 vn_path_release(&nd);
497 rc = !dentry->d_inode ? -ENOENT :
498 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
501 EXPORT_SYMBOL(vn_remove);
503 /* Modified do_rename() from linux/fs/namei.c, only uses exported symbols */
505 vn_rename(const char *oldname, const char *newname, int x1)
507 struct dentry *old_dir, *new_dir;
508 struct dentry *old_dentry, *new_dentry;
510 struct nameidata oldnd, newnd;
514 rc = spl_kern_path_parent(oldname, &oldnd);
518 rc = spl_kern_path_parent(newname, &newnd);
523 if (oldnd.nd_mnt != newnd.nd_mnt)
526 old_dir = oldnd.nd_dentry;
528 if (oldnd.last_type != LAST_NORM)
531 new_dir = newnd.nd_dentry;
532 if (newnd.last_type != LAST_NORM)
535 trap = lock_rename(new_dir, old_dir);
537 old_dentry = vn_lookup_hash(&oldnd);
539 rc = PTR_ERR(old_dentry);
540 if (IS_ERR(old_dentry))
543 /* source must exist */
545 if (!old_dentry->d_inode)
548 /* unless the source is a directory trailing slashes give -ENOTDIR */
549 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
551 if (oldnd.last.name[oldnd.last.len])
553 if (newnd.last.name[newnd.last.len])
557 /* source should not be ancestor of target */
559 if (old_dentry == trap)
562 new_dentry = vn_lookup_hash(&newnd);
563 rc = PTR_ERR(new_dentry);
564 if (IS_ERR(new_dentry))
567 /* target should not be an ancestor of source */
569 if (new_dentry == trap)
572 #ifdef HAVE_4ARGS_VFS_RENAME
573 rc = vfs_rename(old_dir->d_inode, old_dentry,
574 new_dir->d_inode, new_dentry);
576 rc = vfs_rename(old_dir->d_inode, old_dentry, oldnd.nd_mnt,
577 new_dir->d_inode, new_dentry, newnd.nd_mnt);
578 #endif /* HAVE_4ARGS_VFS_RENAME */
584 unlock_rename(new_dir, old_dir);
586 vn_path_release(&newnd);
588 vn_path_release(&oldnd);
592 EXPORT_SYMBOL(vn_rename);
593 #endif /* HAVE_KERN_PATH_LOCKED */
596 vn_getattr(vnode_t *vp, vattr_t *vap, int flags, void *x3, void *x4)
609 #ifdef HAVE_2ARGS_VFS_GETATTR
610 rc = vfs_getattr(&fp->f_path, &stat);
612 rc = vfs_getattr(fp->f_path.mnt, fp->f_dentry, &stat);
617 vap->va_type = vn_mode_to_vtype(stat.mode);
618 vap->va_mode = stat.mode;
619 vap->va_uid = stat.uid;
620 vap->va_gid = stat.gid;
622 vap->va_nodeid = stat.ino;
623 vap->va_nlink = stat.nlink;
624 vap->va_size = stat.size;
625 vap->va_blksize = stat.blksize;
626 vap->va_atime = stat.atime;
627 vap->va_mtime = stat.mtime;
628 vap->va_ctime = stat.ctime;
629 vap->va_rdev = stat.rdev;
630 vap->va_nblocks = stat.blocks;
634 EXPORT_SYMBOL(vn_getattr);
636 int vn_fsync(vnode_t *vp, int flags, void *x3, void *x4)
647 SRETURN(-spl_filp_fsync(vp->v_file, datasync));
649 EXPORT_SYMBOL(vn_fsync);
651 int vn_space(vnode_t *vp, int cmd, struct flock *bfp, int flag,
652 offset_t offset, void *x6, void *x7)
654 int error = EOPNOTSUPP;
657 if (cmd != F_FREESP || bfp->l_whence != 0)
662 ASSERT(bfp->l_start >= 0 && bfp->l_len > 0);
664 #ifdef FALLOC_FL_PUNCH_HOLE
666 * When supported by the underlying file system preferentially
667 * use the fallocate() callback to preallocate the space.
669 error = -spl_filp_fallocate(vp->v_file,
670 FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
671 bfp->l_start, bfp->l_len);
676 #ifdef HAVE_INODE_TRUNCATE_RANGE
677 if (vp->v_file->f_dentry && vp->v_file->f_dentry->d_inode &&
678 vp->v_file->f_dentry->d_inode->i_op &&
679 vp->v_file->f_dentry->d_inode->i_op->truncate_range) {
680 off_t end = bfp->l_start + bfp->l_len;
682 * Judging from the code in shmem_truncate_range(),
683 * it seems the kernel expects the end offset to be
684 * inclusive and aligned to the end of a page.
686 if (end % PAGE_SIZE != 0) {
687 end &= ~(off_t)(PAGE_SIZE - 1);
688 if (end <= bfp->l_start)
693 vp->v_file->f_dentry->d_inode->i_op->truncate_range(
694 vp->v_file->f_dentry->d_inode,
703 EXPORT_SYMBOL(vn_space);
705 /* Function must be called while holding the vn_file_lock */
711 ASSERT(spin_is_locked(&vn_file_lock));
713 list_for_each_entry(fp, &vn_file_list, f_list) {
714 if (fd == fp->f_fd && fp->f_task == current) {
715 ASSERT(atomic_read(&fp->f_ref) != 0);
733 /* Already open just take an extra reference */
734 spin_lock(&vn_file_lock);
738 atomic_inc(&fp->f_ref);
739 spin_unlock(&vn_file_lock);
743 spin_unlock(&vn_file_lock);
745 /* File was not yet opened create the object and setup */
746 fp = kmem_cache_alloc(vn_file_cache, KM_SLEEP);
750 mutex_enter(&fp->f_lock);
753 fp->f_task = current;
755 atomic_inc(&fp->f_ref);
759 SGOTO(out_mutex, rc);
761 vp = vn_alloc(KM_SLEEP);
765 #ifdef HAVE_2ARGS_VFS_GETATTR
766 rc = vfs_getattr(&lfp->f_path, &stat);
768 rc = vfs_getattr(lfp->f_path.mnt, lfp->f_dentry, &stat);
771 SGOTO(out_vnode, rc);
773 mutex_enter(&vp->v_lock);
774 vp->v_type = vn_mode_to_vtype(stat.mode);
776 mutex_exit(&vp->v_lock);
781 /* Put it on the tracking list */
782 spin_lock(&vn_file_lock);
783 list_add(&fp->f_list, &vn_file_list);
784 spin_unlock(&vn_file_lock);
786 mutex_exit(&fp->f_lock);
794 mutex_exit(&fp->f_lock);
795 kmem_cache_free(vn_file_cache, fp);
801 static void releasef_locked(file_t *fp)
806 /* Unlinked from list, no refs, safe to free outside mutex */
808 vn_free(fp->f_vnode);
810 kmem_cache_free(vn_file_cache, fp);
819 spin_lock(&vn_file_lock);
822 atomic_dec(&fp->f_ref);
823 if (atomic_read(&fp->f_ref) > 0) {
824 spin_unlock(&vn_file_lock);
829 list_del(&fp->f_list);
832 spin_unlock(&vn_file_lock);
837 EXPORT_SYMBOL(releasef);
839 #ifndef HAVE_SET_FS_PWD
841 # ifdef HAVE_SET_FS_PWD_WITH_CONST
842 set_fs_pwd(struct fs_struct *fs, const struct path *path)
844 set_fs_pwd(struct fs_struct *fs, struct path *path)
849 # ifdef HAVE_FS_STRUCT_SPINLOCK
850 spin_lock(&fs->lock);
854 spin_unlock(&fs->lock);
856 write_lock(&fs->lock);
860 write_unlock(&fs->lock);
861 # endif /* HAVE_FS_STRUCT_SPINLOCK */
866 #endif /* HAVE_SET_FS_PWD */
869 vn_set_pwd(const char *filename)
871 #ifdef HAVE_USER_PATH_DIR
875 #endif /* HAVE_USER_PATH_DIR */
876 mm_segment_t saved_fs;
881 * user_path_dir() and __user_walk() both expect 'filename' to be
882 * a user space address so we must briefly increase the data segment
883 * size to ensure strncpy_from_user() does not fail with -EFAULT.
888 # ifdef HAVE_USER_PATH_DIR
889 rc = user_path_dir(filename, &path);
893 rc = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_ACCESS);
895 SGOTO(dput_and_out, rc);
897 set_fs_pwd(current->fs, &path);
902 rc = __user_walk(filename,
903 LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_CHDIR, &nd);
907 rc = vfs_permission(&nd, MAY_EXEC);
909 SGOTO(dput_and_out, rc);
911 set_fs_pwd(current->fs, &nd.path);
915 # endif /* HAVE_USER_PATH_DIR */
921 EXPORT_SYMBOL(vn_set_pwd);
924 vn_cache_constructor(void *buf, void *cdrarg, int kmflags)
926 struct vnode *vp = buf;
928 mutex_init(&vp->v_lock, NULL, MUTEX_DEFAULT, NULL);
931 } /* vn_cache_constructor() */
934 vn_cache_destructor(void *buf, void *cdrarg)
936 struct vnode *vp = buf;
938 mutex_destroy(&vp->v_lock);
939 } /* vn_cache_destructor() */
942 vn_file_cache_constructor(void *buf, void *cdrarg, int kmflags)
946 atomic_set(&fp->f_ref, 0);
947 mutex_init(&fp->f_lock, NULL, MUTEX_DEFAULT, NULL);
948 INIT_LIST_HEAD(&fp->f_list);
951 } /* file_cache_constructor() */
954 vn_file_cache_destructor(void *buf, void *cdrarg)
958 mutex_destroy(&fp->f_lock);
959 } /* vn_file_cache_destructor() */
961 int spl_vn_init_kallsyms_lookup(void)
963 #ifdef HAVE_KERN_PATH_PARENT_HEADER
964 #ifndef HAVE_KERN_PATH_PARENT_SYMBOL
965 kern_path_parent_fn = (kern_path_parent_t)
966 spl_kallsyms_lookup_name("kern_path_parent");
967 if (!kern_path_parent_fn) {
968 printk(KERN_ERR "Error: Unknown symbol kern_path_parent\n");
971 #endif /* HAVE_KERN_PATH_PARENT_SYMBOL */
972 #endif /* HAVE_KERN_PATH_PARENT_HEADER */
974 #ifdef HAVE_KERN_PATH_LOCKED
975 kern_path_locked_fn = (kern_path_locked_t)
976 spl_kallsyms_lookup_name("kern_path_locked");
977 if (!kern_path_locked_fn) {
978 printk(KERN_ERR "Error: Unknown symbol kern_path_locked\n");
990 vn_cache = kmem_cache_create("spl_vn_cache",
991 sizeof(struct vnode), 64,
992 vn_cache_constructor,
994 NULL, NULL, NULL, KMC_KMEM);
996 vn_file_cache = kmem_cache_create("spl_vn_file_cache",
998 vn_file_cache_constructor,
999 vn_file_cache_destructor,
1000 NULL, NULL, NULL, KMC_KMEM);
1007 file_t *fp, *next_fp;
1011 spin_lock(&vn_file_lock);
1013 list_for_each_entry_safe(fp, next_fp, &vn_file_list, f_list) {
1014 list_del(&fp->f_list);
1015 releasef_locked(fp);
1019 spin_unlock(&vn_file_lock);
1022 SWARN("Warning %d files leaked\n", leaked);
1024 kmem_cache_destroy(vn_file_cache);
1025 kmem_cache_destroy(vn_cache);