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 * spl_basename() takes a NULL-terminated string s as input containing a path.
301 * It returns a char pointer to a string and a length that describe the
302 * basename of the path. If the basename is not "." or "/", it will be an index
303 * into the string. While the string should be NULL terminated, the section
304 * referring to the basename is not. spl_basename is dual-licensed GPLv2+ and
305 * CC0. Anyone wishing to reuse it in another codebase may pick either license.
308 spl_basename(const char *s
, const char **str
, int *len
)
323 while (i
&& s
[i
--] == '/');
333 for (end
= i
; i
; i
--) {
345 static struct dentry
*
346 spl_kern_path_locked(const char *name
, struct path
*path
)
349 struct dentry
*dentry
;
350 const char *basename
;
357 spl_basename(name
, &basename
, &len
);
359 /* We do not accept "." or ".." */
360 if (len
<= 2 && basename
[0] == '.')
361 if (len
== 1 || basename
[1] == '.')
362 return (ERR_PTR(-EACCES
));
364 rc
= kern_path(name
, LOOKUP_PARENT
, &parent
);
366 return (ERR_PTR(rc
));
368 spl_inode_lock(parent
.dentry
->d_inode
);
370 dentry
= lookup_one_len(basename
, parent
.dentry
, len
);
371 if (IS_ERR(dentry
)) {
372 spl_inode_unlock(parent
.dentry
->d_inode
);
381 /* Based on do_unlinkat() from linux/fs/namei.c */
383 vn_remove(const char *path
, uio_seg_t seg
, int flags
)
385 struct dentry
*dentry
;
387 struct inode
*inode
= NULL
;
391 ASSERT(seg
== UIO_SYSSPACE
);
392 ASSERT(flags
== RMFILE
);
394 dentry
= spl_kern_path_locked(path
, &parent
);
395 rc
= PTR_ERR(dentry
);
396 if (!IS_ERR(dentry
)) {
397 if (parent
.dentry
->d_name
.name
[parent
.dentry
->d_name
.len
])
398 SGOTO(slashes
, rc
= 0);
400 inode
= dentry
->d_inode
;
402 atomic_inc(&inode
->i_count
);
404 SGOTO(slashes
, rc
= 0);
406 #ifdef HAVE_2ARGS_VFS_UNLINK
407 rc
= vfs_unlink(parent
.dentry
->d_inode
, dentry
);
409 rc
= vfs_unlink(parent
.dentry
->d_inode
, dentry
, NULL
);
410 #endif /* HAVE_2ARGS_VFS_UNLINK */
417 spl_inode_unlock(parent
.dentry
->d_inode
);
419 iput(inode
); /* truncate the inode here */
425 rc
= !dentry
->d_inode
? -ENOENT
:
426 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
429 EXPORT_SYMBOL(vn_remove
);
431 /* Based on do_rename() from linux/fs/namei.c */
433 vn_rename(const char *oldname
, const char *newname
, int x1
)
435 struct dentry
*old_dir
, *new_dir
;
436 struct dentry
*old_dentry
, *new_dentry
;
438 struct path old_parent
, new_parent
;
442 old_dentry
= spl_kern_path_locked(oldname
, &old_parent
);
443 if (IS_ERR(old_dentry
))
444 SGOTO(exit
, rc
= PTR_ERR(old_dentry
));
446 spl_inode_unlock(old_parent
.dentry
->d_inode
);
448 new_dentry
= spl_kern_path_locked(newname
, &new_parent
);
449 if (IS_ERR(new_dentry
))
450 SGOTO(exit2
, rc
= PTR_ERR(new_dentry
));
452 spl_inode_unlock(new_parent
.dentry
->d_inode
);
455 if (old_parent
.mnt
!= new_parent
.mnt
)
458 old_dir
= old_parent
.dentry
;
459 new_dir
= new_parent
.dentry
;
460 trap
= lock_rename(new_dir
, old_dir
);
462 /* source should not be ancestor of target */
464 if (old_dentry
== trap
)
467 /* target should not be an ancestor of source */
469 if (new_dentry
== trap
)
472 /* source must exist */
474 if (!old_dentry
->d_inode
)
477 /* unless the source is a directory trailing slashes give -ENOTDIR */
478 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
480 if (old_dentry
->d_name
.name
[old_dentry
->d_name
.len
])
482 if (new_dentry
->d_name
.name
[new_dentry
->d_name
.len
])
486 #if defined(HAVE_4ARGS_VFS_RENAME)
487 rc
= vfs_rename(old_dir
->d_inode
, old_dentry
,
488 new_dir
->d_inode
, new_dentry
);
489 #elif defined(HAVE_5ARGS_VFS_RENAME)
490 rc
= vfs_rename(old_dir
->d_inode
, old_dentry
,
491 new_dir
->d_inode
, new_dentry
, NULL
);
493 rc
= vfs_rename(old_dir
->d_inode
, old_dentry
,
494 new_dir
->d_inode
, new_dentry
, NULL
, 0);
497 unlock_rename(new_dir
, old_dir
);
500 path_put(&new_parent
);
503 path_put(&old_parent
);
507 EXPORT_SYMBOL(vn_rename
);
510 vn_getattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, void *x3
, void *x4
)
523 #ifdef HAVE_2ARGS_VFS_GETATTR
524 rc
= vfs_getattr(&fp
->f_path
, &stat
);
526 rc
= vfs_getattr(fp
->f_path
.mnt
, fp
->f_dentry
, &stat
);
531 vap
->va_type
= vn_mode_to_vtype(stat
.mode
);
532 vap
->va_mode
= stat
.mode
;
533 vap
->va_uid
= KUID_TO_SUID(stat
.uid
);
534 vap
->va_gid
= KGID_TO_SGID(stat
.gid
);
536 vap
->va_nodeid
= stat
.ino
;
537 vap
->va_nlink
= stat
.nlink
;
538 vap
->va_size
= stat
.size
;
539 vap
->va_blksize
= stat
.blksize
;
540 vap
->va_atime
= stat
.atime
;
541 vap
->va_mtime
= stat
.mtime
;
542 vap
->va_ctime
= stat
.ctime
;
543 vap
->va_rdev
= stat
.rdev
;
544 vap
->va_nblocks
= stat
.blocks
;
548 EXPORT_SYMBOL(vn_getattr
);
550 int vn_fsync(vnode_t
*vp
, int flags
, void *x3
, void *x4
)
561 SRETURN(-spl_filp_fsync(vp
->v_file
, datasync
));
563 EXPORT_SYMBOL(vn_fsync
);
565 int vn_space(vnode_t
*vp
, int cmd
, struct flock
*bfp
, int flag
,
566 offset_t offset
, void *x6
, void *x7
)
568 int error
= EOPNOTSUPP
;
571 if (cmd
!= F_FREESP
|| bfp
->l_whence
!= 0)
576 ASSERT(bfp
->l_start
>= 0 && bfp
->l_len
> 0);
578 #ifdef FALLOC_FL_PUNCH_HOLE
580 * When supported by the underlying file system preferentially
581 * use the fallocate() callback to preallocate the space.
583 error
= -spl_filp_fallocate(vp
->v_file
,
584 FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
,
585 bfp
->l_start
, bfp
->l_len
);
590 #ifdef HAVE_INODE_TRUNCATE_RANGE
591 if (vp
->v_file
->f_dentry
&& vp
->v_file
->f_dentry
->d_inode
&&
592 vp
->v_file
->f_dentry
->d_inode
->i_op
&&
593 vp
->v_file
->f_dentry
->d_inode
->i_op
->truncate_range
) {
594 off_t end
= bfp
->l_start
+ bfp
->l_len
;
596 * Judging from the code in shmem_truncate_range(),
597 * it seems the kernel expects the end offset to be
598 * inclusive and aligned to the end of a page.
600 if (end
% PAGE_SIZE
!= 0) {
601 end
&= ~(off_t
)(PAGE_SIZE
- 1);
602 if (end
<= bfp
->l_start
)
607 vp
->v_file
->f_dentry
->d_inode
->i_op
->truncate_range(
608 vp
->v_file
->f_dentry
->d_inode
,
617 EXPORT_SYMBOL(vn_space
);
619 /* Function must be called while holding the vn_file_lock */
625 ASSERT(spin_is_locked(&vn_file_lock
));
627 list_for_each_entry(fp
, &vn_file_list
, f_list
) {
628 if (fd
== fp
->f_fd
&& fp
->f_task
== current
) {
629 ASSERT(atomic_read(&fp
->f_ref
) != 0);
647 /* Already open just take an extra reference */
648 spin_lock(&vn_file_lock
);
652 atomic_inc(&fp
->f_ref
);
653 spin_unlock(&vn_file_lock
);
657 spin_unlock(&vn_file_lock
);
659 /* File was not yet opened create the object and setup */
660 fp
= kmem_cache_alloc(vn_file_cache
, KM_SLEEP
);
664 mutex_enter(&fp
->f_lock
);
667 fp
->f_task
= current
;
669 atomic_inc(&fp
->f_ref
);
673 SGOTO(out_mutex
, rc
);
675 vp
= vn_alloc(KM_SLEEP
);
679 #ifdef HAVE_2ARGS_VFS_GETATTR
680 rc
= vfs_getattr(&lfp
->f_path
, &stat
);
682 rc
= vfs_getattr(lfp
->f_path
.mnt
, lfp
->f_dentry
, &stat
);
685 SGOTO(out_vnode
, rc
);
687 mutex_enter(&vp
->v_lock
);
688 vp
->v_type
= vn_mode_to_vtype(stat
.mode
);
690 mutex_exit(&vp
->v_lock
);
695 /* Put it on the tracking list */
696 spin_lock(&vn_file_lock
);
697 list_add(&fp
->f_list
, &vn_file_list
);
698 spin_unlock(&vn_file_lock
);
700 mutex_exit(&fp
->f_lock
);
708 mutex_exit(&fp
->f_lock
);
709 kmem_cache_free(vn_file_cache
, fp
);
715 static void releasef_locked(file_t
*fp
)
720 /* Unlinked from list, no refs, safe to free outside mutex */
722 vn_free(fp
->f_vnode
);
724 kmem_cache_free(vn_file_cache
, fp
);
733 spin_lock(&vn_file_lock
);
736 atomic_dec(&fp
->f_ref
);
737 if (atomic_read(&fp
->f_ref
) > 0) {
738 spin_unlock(&vn_file_lock
);
743 list_del(&fp
->f_list
);
746 spin_unlock(&vn_file_lock
);
751 EXPORT_SYMBOL(releasef
);
754 #ifdef HAVE_SET_FS_PWD_WITH_CONST
755 vn_set_fs_pwd(struct fs_struct
*fs
, const struct path
*path
)
757 vn_set_fs_pwd(struct fs_struct
*fs
, struct path
*path
)
758 #endif /* HAVE_SET_FS_PWD_WITH_CONST */
762 #ifdef HAVE_FS_STRUCT_SPINLOCK
763 spin_lock(&fs
->lock
);
767 spin_unlock(&fs
->lock
);
769 write_lock(&fs
->lock
);
773 write_unlock(&fs
->lock
);
774 #endif /* HAVE_FS_STRUCT_SPINLOCK */
781 vn_set_pwd(const char *filename
)
784 mm_segment_t saved_fs
;
789 * user_path_dir() and __user_walk() both expect 'filename' to be
790 * a user space address so we must briefly increase the data segment
791 * size to ensure strncpy_from_user() does not fail with -EFAULT.
796 rc
= user_path_dir(filename
, &path
);
800 rc
= inode_permission(path
.dentry
->d_inode
, MAY_EXEC
| MAY_ACCESS
);
802 SGOTO(dput_and_out
, rc
);
804 vn_set_fs_pwd(current
->fs
, &path
);
813 EXPORT_SYMBOL(vn_set_pwd
);
816 vn_cache_constructor(void *buf
, void *cdrarg
, int kmflags
)
818 struct vnode
*vp
= buf
;
820 mutex_init(&vp
->v_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
823 } /* vn_cache_constructor() */
826 vn_cache_destructor(void *buf
, void *cdrarg
)
828 struct vnode
*vp
= buf
;
830 mutex_destroy(&vp
->v_lock
);
831 } /* vn_cache_destructor() */
834 vn_file_cache_constructor(void *buf
, void *cdrarg
, int kmflags
)
838 atomic_set(&fp
->f_ref
, 0);
839 mutex_init(&fp
->f_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
840 INIT_LIST_HEAD(&fp
->f_list
);
843 } /* file_cache_constructor() */
846 vn_file_cache_destructor(void *buf
, void *cdrarg
)
850 mutex_destroy(&fp
->f_lock
);
851 } /* vn_file_cache_destructor() */
857 vn_cache
= kmem_cache_create("spl_vn_cache",
858 sizeof(struct vnode
), 64,
859 vn_cache_constructor
,
861 NULL
, NULL
, NULL
, KMC_KMEM
);
863 vn_file_cache
= kmem_cache_create("spl_vn_file_cache",
865 vn_file_cache_constructor
,
866 vn_file_cache_destructor
,
867 NULL
, NULL
, NULL
, KMC_KMEM
);
874 file_t
*fp
, *next_fp
;
878 spin_lock(&vn_file_lock
);
880 list_for_each_entry_safe(fp
, next_fp
, &vn_file_list
, f_list
) {
881 list_del(&fp
->f_list
);
886 spin_unlock(&vn_file_lock
);
889 SWARN("Warning %d files leaked\n", leaked
);
891 kmem_cache_destroy(vn_file_cache
);
892 kmem_cache_destroy(vn_cache
);