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 <linux/falloc.h>
30 #include <spl-debug.h>
32 #ifdef SS_DEBUG_SUBSYS
33 #undef SS_DEBUG_SUBSYS
36 #define SS_DEBUG_SUBSYS SS_VNODE
38 vnode_t *rootdir = (vnode_t *)0xabcd1234;
39 EXPORT_SYMBOL(rootdir);
41 static spl_kmem_cache_t *vn_cache;
42 static spl_kmem_cache_t *vn_file_cache;
44 static DEFINE_SPINLOCK(vn_file_lock);
45 static LIST_HEAD(vn_file_list);
48 vn_mode_to_vtype(mode_t mode)
75 } /* vn_mode_to_vtype() */
76 EXPORT_SYMBOL(vn_mode_to_vtype);
79 vn_vtype_to_mode(vtype_t vtype)
103 } /* vn_vtype_to_mode() */
104 EXPORT_SYMBOL(vn_vtype_to_mode);
112 vp = kmem_cache_alloc(vn_cache, flag);
120 EXPORT_SYMBOL(vn_alloc);
126 kmem_cache_free(vn_cache, vp);
129 EXPORT_SYMBOL(vn_free);
132 vn_open(const char *path, uio_seg_t seg, int flags, int mode,
133 vnode_t **vpp, int x1, void *x2)
137 int rc, saved_umask = 0;
142 ASSERT(flags & (FWRITE | FREAD));
143 ASSERT(seg == UIO_SYSSPACE);
147 if (!(flags & FCREAT) && (flags & FWRITE))
150 /* Note for filp_open() the two low bits must be remapped to mean:
151 * 01 - read-only -> 00 read-only
152 * 10 - write-only -> 01 write-only
153 * 11 - read-write -> 10 read-write
158 saved_umask = xchg(¤t->fs->umask, 0);
160 fp = filp_open(path, flags, mode);
163 (void)xchg(¤t->fs->umask, saved_umask);
166 SRETURN(-PTR_ERR(fp));
168 #ifdef HAVE_2ARGS_VFS_GETATTR
169 rc = vfs_getattr(&fp->f_path, &stat);
171 rc = vfs_getattr(fp->f_path.mnt, fp->f_dentry, &stat);
178 vp = vn_alloc(KM_SLEEP);
184 saved_gfp = mapping_gfp_mask(fp->f_mapping);
185 mapping_set_gfp_mask(fp->f_mapping, saved_gfp & ~(__GFP_IO|__GFP_FS));
187 mutex_enter(&vp->v_lock);
188 vp->v_type = vn_mode_to_vtype(stat.mode);
190 vp->v_gfp_mask = saved_gfp;
192 mutex_exit(&vp->v_lock);
196 EXPORT_SYMBOL(vn_open);
199 vn_openat(const char *path, uio_seg_t seg, int flags, int mode,
200 vnode_t **vpp, int x1, void *x2, vnode_t *vp, int fd)
206 ASSERT(vp == rootdir);
208 len = strlen(path) + 2;
209 realpath = kmalloc(len, GFP_KERNEL);
213 (void)snprintf(realpath, len, "/%s", path);
214 rc = vn_open(realpath, seg, flags, mode, vpp, x1, x2);
219 EXPORT_SYMBOL(vn_openat);
222 vn_rdwr(uio_rw_t uio, vnode_t *vp, void *addr, ssize_t len, offset_t off,
223 uio_seg_t seg, int ioflag, rlim64_t x2, void *x3, ssize_t *residp)
226 mm_segment_t saved_fs;
231 ASSERT(uio == UIO_WRITE || uio == UIO_READ);
234 ASSERT(seg == UIO_SYSSPACE);
235 ASSERT((ioflag & ~FAPPEND) == 0);
236 ASSERT(x2 == RLIM64_INFINITY);
241 if (ioflag & FAPPEND)
244 /* Writable user data segment must be briefly increased for this
245 * process so we can use the user space read call paths to write
246 * in to memory allocated by the kernel. */
251 rc = vfs_write(fp, addr, len, &offset);
253 rc = vfs_read(fp, addr, len, &offset);
270 EXPORT_SYMBOL(vn_rdwr);
273 vn_close(vnode_t *vp, int flags, int x1, int x2, void *x3, void *x4)
281 mapping_set_gfp_mask(vp->v_file->f_mapping, vp->v_gfp_mask);
282 rc = filp_close(vp->v_file, 0);
287 EXPORT_SYMBOL(vn_close);
289 /* vn_seek() does not actually seek it only performs bounds checking on the
290 * proposed seek. We perform minimal checking and allow vn_rdwr() to catch
291 * anything more serious. */
293 vn_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, void *ct)
295 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
297 EXPORT_SYMBOL(vn_seek);
300 * kern_path() was introduced in Linux 2.6.28. We duplicate it as a
301 * compatibility shim for earlier kernels.
303 #ifndef HAVE_KERN_PATH
305 kern_path(const char *name, unsigned int flags, struct path *path)
308 int rc = path_lookup(name, flags, &nd);
313 #endif /* HAVE_KERN_PATH */
316 * spl_basename() takes a NULL-terminated string s as input containing a path.
317 * It returns a char pointer to a string and a length that describe the
318 * basename of the path. If the basename is not "." or "/", it will be an index
319 * into the string. While the string should be NULL terminated, the section
320 * referring to the basename is not. spl_basename is dual-licensed GPLv2+ and
321 * CC0. Anyone wishing to reuse it in another codebase may pick either license.
324 spl_basename(const char *s, const char **str, int *len)
339 while (i && s[i--] == '/');
349 for (end = i; i; i--) {
361 static struct dentry *
362 spl_kern_path_locked(const char *name, struct path *path)
365 struct dentry *dentry;
366 const char *basename;
373 spl_basename(name, &basename, &len);
375 /* We do not accept "." or ".." */
376 if (len <= 2 && basename[0] == '.')
377 if (len == 1 || basename[1] == '.')
378 return (ERR_PTR(-EACCES));
380 rc = kern_path(name, LOOKUP_PARENT, &parent);
382 return (ERR_PTR(rc));
384 spl_inode_lock_nested(parent.dentry->d_inode, I_MUTEX_PARENT);
386 dentry = lookup_one_len(basename, parent.dentry, len);
387 if (IS_ERR(dentry)) {
388 spl_inode_unlock(parent.dentry->d_inode);
397 /* Based on do_unlinkat() from linux/fs/namei.c */
399 vn_remove(const char *path, uio_seg_t seg, int flags)
401 struct dentry *dentry;
403 struct inode *inode = NULL;
407 ASSERT(seg == UIO_SYSSPACE);
408 ASSERT(flags == RMFILE);
410 dentry = spl_kern_path_locked(path, &parent);
411 rc = PTR_ERR(dentry);
412 if (!IS_ERR(dentry)) {
413 if (parent.dentry->d_name.name[parent.dentry->d_name.len])
414 SGOTO(slashes, rc = 0);
416 inode = dentry->d_inode;
418 atomic_inc(&inode->i_count);
420 SGOTO(slashes, rc = 0);
422 #ifdef HAVE_2ARGS_VFS_UNLINK
423 rc = vfs_unlink(parent.dentry->d_inode, dentry);
425 rc = vfs_unlink(parent.dentry->d_inode, dentry, NULL);
426 #endif /* HAVE_2ARGS_VFS_UNLINK */
433 spl_inode_unlock(parent.dentry->d_inode);
435 iput(inode); /* truncate the inode here */
441 rc = !dentry->d_inode ? -ENOENT :
442 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
445 EXPORT_SYMBOL(vn_remove);
447 /* Based on do_rename() from linux/fs/namei.c */
449 vn_rename(const char *oldname, const char *newname, int x1)
451 struct dentry *old_dir, *new_dir;
452 struct dentry *old_dentry, *new_dentry;
454 struct path old_parent, new_parent;
458 old_dentry = spl_kern_path_locked(oldname, &old_parent);
459 if (IS_ERR(old_dentry))
460 SGOTO(exit, rc = PTR_ERR(old_dentry));
462 spl_inode_unlock(old_parent.dentry->d_inode);
464 new_dentry = spl_kern_path_locked(newname, &new_parent);
465 if (IS_ERR(new_dentry))
466 SGOTO(exit2, rc = PTR_ERR(new_dentry));
468 spl_inode_unlock(new_parent.dentry->d_inode);
471 if (old_parent.mnt != new_parent.mnt)
474 old_dir = old_parent.dentry;
475 new_dir = new_parent.dentry;
476 trap = lock_rename(new_dir, old_dir);
478 /* source should not be ancestor of target */
480 if (old_dentry == trap)
483 /* target should not be an ancestor of source */
485 if (new_dentry == trap)
488 /* source must exist */
490 if (!old_dentry->d_inode)
493 /* unless the source is a directory trailing slashes give -ENOTDIR */
494 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
496 if (old_dentry->d_name.name[old_dentry->d_name.len])
498 if (new_dentry->d_name.name[new_dentry->d_name.len])
502 #if defined(HAVE_4ARGS_VFS_RENAME)
503 rc = vfs_rename(old_dir->d_inode, old_dentry,
504 new_dir->d_inode, new_dentry);
505 #elif defined(HAVE_5ARGS_VFS_RENAME)
506 rc = vfs_rename(old_dir->d_inode, old_dentry,
507 new_dir->d_inode, new_dentry, NULL);
509 rc = vfs_rename(old_dir->d_inode, old_dentry,
510 new_dir->d_inode, new_dentry, NULL, 0);
513 unlock_rename(new_dir, old_dir);
516 path_put(&new_parent);
519 path_put(&old_parent);
523 EXPORT_SYMBOL(vn_rename);
526 vn_getattr(vnode_t *vp, vattr_t *vap, int flags, void *x3, void *x4)
539 #ifdef HAVE_2ARGS_VFS_GETATTR
540 rc = vfs_getattr(&fp->f_path, &stat);
542 rc = vfs_getattr(fp->f_path.mnt, fp->f_dentry, &stat);
547 vap->va_type = vn_mode_to_vtype(stat.mode);
548 vap->va_mode = stat.mode;
549 vap->va_uid = KUID_TO_SUID(stat.uid);
550 vap->va_gid = KGID_TO_SGID(stat.gid);
552 vap->va_nodeid = stat.ino;
553 vap->va_nlink = stat.nlink;
554 vap->va_size = stat.size;
555 vap->va_blksize = stat.blksize;
556 vap->va_atime = stat.atime;
557 vap->va_mtime = stat.mtime;
558 vap->va_ctime = stat.ctime;
559 vap->va_rdev = stat.rdev;
560 vap->va_nblocks = stat.blocks;
564 EXPORT_SYMBOL(vn_getattr);
566 int vn_fsync(vnode_t *vp, int flags, void *x3, void *x4)
577 SRETURN(-spl_filp_fsync(vp->v_file, datasync));
579 EXPORT_SYMBOL(vn_fsync);
581 int vn_space(vnode_t *vp, int cmd, struct flock *bfp, int flag,
582 offset_t offset, void *x6, void *x7)
584 int error = EOPNOTSUPP;
587 if (cmd != F_FREESP || bfp->l_whence != 0)
592 ASSERT(bfp->l_start >= 0 && bfp->l_len > 0);
594 #ifdef FALLOC_FL_PUNCH_HOLE
596 * When supported by the underlying file system preferentially
597 * use the fallocate() callback to preallocate the space.
599 error = -spl_filp_fallocate(vp->v_file,
600 FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
601 bfp->l_start, bfp->l_len);
606 #ifdef HAVE_INODE_TRUNCATE_RANGE
607 if (vp->v_file->f_dentry && vp->v_file->f_dentry->d_inode &&
608 vp->v_file->f_dentry->d_inode->i_op &&
609 vp->v_file->f_dentry->d_inode->i_op->truncate_range) {
610 off_t end = bfp->l_start + bfp->l_len;
612 * Judging from the code in shmem_truncate_range(),
613 * it seems the kernel expects the end offset to be
614 * inclusive and aligned to the end of a page.
616 if (end % PAGE_SIZE != 0) {
617 end &= ~(off_t)(PAGE_SIZE - 1);
618 if (end <= bfp->l_start)
623 vp->v_file->f_dentry->d_inode->i_op->truncate_range(
624 vp->v_file->f_dentry->d_inode,
633 EXPORT_SYMBOL(vn_space);
635 /* Function must be called while holding the vn_file_lock */
641 ASSERT(spin_is_locked(&vn_file_lock));
643 list_for_each_entry(fp, &vn_file_list, f_list) {
644 if (fd == fp->f_fd && fp->f_task == current) {
645 ASSERT(atomic_read(&fp->f_ref) != 0);
663 /* Already open just take an extra reference */
664 spin_lock(&vn_file_lock);
668 atomic_inc(&fp->f_ref);
669 spin_unlock(&vn_file_lock);
673 spin_unlock(&vn_file_lock);
675 /* File was not yet opened create the object and setup */
676 fp = kmem_cache_alloc(vn_file_cache, KM_SLEEP);
680 mutex_enter(&fp->f_lock);
683 fp->f_task = current;
685 atomic_inc(&fp->f_ref);
689 SGOTO(out_mutex, rc);
691 vp = vn_alloc(KM_SLEEP);
695 #ifdef HAVE_2ARGS_VFS_GETATTR
696 rc = vfs_getattr(&lfp->f_path, &stat);
698 rc = vfs_getattr(lfp->f_path.mnt, lfp->f_dentry, &stat);
701 SGOTO(out_vnode, rc);
703 mutex_enter(&vp->v_lock);
704 vp->v_type = vn_mode_to_vtype(stat.mode);
706 mutex_exit(&vp->v_lock);
711 /* Put it on the tracking list */
712 spin_lock(&vn_file_lock);
713 list_add(&fp->f_list, &vn_file_list);
714 spin_unlock(&vn_file_lock);
716 mutex_exit(&fp->f_lock);
724 mutex_exit(&fp->f_lock);
725 kmem_cache_free(vn_file_cache, fp);
731 static void releasef_locked(file_t *fp)
736 /* Unlinked from list, no refs, safe to free outside mutex */
738 vn_free(fp->f_vnode);
740 kmem_cache_free(vn_file_cache, fp);
749 spin_lock(&vn_file_lock);
752 atomic_dec(&fp->f_ref);
753 if (atomic_read(&fp->f_ref) > 0) {
754 spin_unlock(&vn_file_lock);
759 list_del(&fp->f_list);
762 spin_unlock(&vn_file_lock);
767 EXPORT_SYMBOL(releasef);
769 #ifndef HAVE_SET_FS_PWD
771 # ifdef HAVE_SET_FS_PWD_WITH_CONST
772 set_fs_pwd(struct fs_struct *fs, const struct path *path)
774 set_fs_pwd(struct fs_struct *fs, struct path *path)
779 # ifdef HAVE_FS_STRUCT_SPINLOCK
780 spin_lock(&fs->lock);
784 spin_unlock(&fs->lock);
786 write_lock(&fs->lock);
790 write_unlock(&fs->lock);
791 # endif /* HAVE_FS_STRUCT_SPINLOCK */
796 #endif /* HAVE_SET_FS_PWD */
799 vn_set_pwd(const char *filename)
801 #ifdef HAVE_USER_PATH_DIR
805 #endif /* HAVE_USER_PATH_DIR */
806 mm_segment_t saved_fs;
811 * user_path_dir() and __user_walk() both expect 'filename' to be
812 * a user space address so we must briefly increase the data segment
813 * size to ensure strncpy_from_user() does not fail with -EFAULT.
818 # ifdef HAVE_USER_PATH_DIR
819 rc = user_path_dir(filename, &path);
823 rc = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_ACCESS);
825 SGOTO(dput_and_out, rc);
827 set_fs_pwd(current->fs, &path);
832 rc = __user_walk(filename,
833 LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_CHDIR, &nd);
837 rc = vfs_permission(&nd, MAY_EXEC);
839 SGOTO(dput_and_out, rc);
841 set_fs_pwd(current->fs, &nd.path);
845 # endif /* HAVE_USER_PATH_DIR */
851 EXPORT_SYMBOL(vn_set_pwd);
854 vn_cache_constructor(void *buf, void *cdrarg, int kmflags)
856 struct vnode *vp = buf;
858 mutex_init(&vp->v_lock, NULL, MUTEX_DEFAULT, NULL);
861 } /* vn_cache_constructor() */
864 vn_cache_destructor(void *buf, void *cdrarg)
866 struct vnode *vp = buf;
868 mutex_destroy(&vp->v_lock);
869 } /* vn_cache_destructor() */
872 vn_file_cache_constructor(void *buf, void *cdrarg, int kmflags)
876 atomic_set(&fp->f_ref, 0);
877 mutex_init(&fp->f_lock, NULL, MUTEX_DEFAULT, NULL);
878 INIT_LIST_HEAD(&fp->f_list);
881 } /* file_cache_constructor() */
884 vn_file_cache_destructor(void *buf, void *cdrarg)
888 mutex_destroy(&fp->f_lock);
889 } /* vn_file_cache_destructor() */
895 vn_cache = kmem_cache_create("spl_vn_cache",
896 sizeof(struct vnode), 64,
897 vn_cache_constructor,
899 NULL, NULL, NULL, KMC_KMEM);
901 vn_file_cache = kmem_cache_create("spl_vn_file_cache",
903 vn_file_cache_constructor,
904 vn_file_cache_destructor,
905 NULL, NULL, NULL, KMC_KMEM);
912 file_t *fp, *next_fp;
916 spin_lock(&vn_file_lock);
918 list_for_each_entry_safe(fp, next_fp, &vn_file_list, f_list) {
919 list_del(&fp->f_list);
924 spin_unlock(&vn_file_lock);
927 SWARN("Warning %d files leaked\n", leaked);
929 kmem_cache_destroy(vn_file_cache);
930 kmem_cache_destroy(vn_cache);