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/>.
24 * Solaris Porting Layer (SPL) Vnode Implementation.
28 #include <sys/vnode.h>
29 #include <sys/kmem_cache.h>
30 #include <linux/falloc.h>
31 #include <linux/file_compat.h>
33 vnode_t
*rootdir
= (vnode_t
*)0xabcd1234;
34 EXPORT_SYMBOL(rootdir
);
36 static spl_kmem_cache_t
*vn_cache
;
37 static spl_kmem_cache_t
*vn_file_cache
;
39 static DEFINE_SPINLOCK(vn_file_lock
);
40 static LIST_HEAD(vn_file_list
);
43 vn_mode_to_vtype(mode_t mode
)
67 } /* vn_mode_to_vtype() */
68 EXPORT_SYMBOL(vn_mode_to_vtype
);
71 vn_vtype_to_mode(vtype_t vtype
)
95 } /* vn_vtype_to_mode() */
96 EXPORT_SYMBOL(vn_vtype_to_mode
);
103 vp
= kmem_cache_alloc(vn_cache
, flag
);
111 EXPORT_SYMBOL(vn_alloc
);
116 kmem_cache_free(vn_cache
, vp
);
118 EXPORT_SYMBOL(vn_free
);
121 vn_open(const char *path
, uio_seg_t seg
, int flags
, int mode
, vnode_t
**vpp
,
126 int rc
, saved_umask
= 0;
130 ASSERT(flags
& (FWRITE
| FREAD
));
131 ASSERT(seg
== UIO_SYSSPACE
);
135 if (!(flags
& FCREAT
) && (flags
& FWRITE
))
139 * Note for filp_open() the two low bits must be remapped to mean:
140 * 01 - read-only -> 00 read-only
141 * 10 - write-only -> 01 write-only
142 * 11 - read-write -> 10 read-write
147 saved_umask
= xchg(¤t
->fs
->umask
, 0);
149 fp
= filp_open(path
, flags
, mode
);
152 (void) xchg(¤t
->fs
->umask
, saved_umask
);
155 return (-PTR_ERR(fp
));
157 #if defined(HAVE_4ARGS_VFS_GETATTR)
158 rc
= vfs_getattr(&fp
->f_path
, &stat
, STATX_TYPE
, AT_STATX_SYNC_AS_STAT
);
159 #elif defined(HAVE_2ARGS_VFS_GETATTR)
160 rc
= vfs_getattr(&fp
->f_path
, &stat
);
162 rc
= vfs_getattr(fp
->f_path
.mnt
, fp
->f_dentry
, &stat
);
169 vp
= vn_alloc(KM_SLEEP
);
175 saved_gfp
= mapping_gfp_mask(fp
->f_mapping
);
176 mapping_set_gfp_mask(fp
->f_mapping
, saved_gfp
& ~(__GFP_IO
|__GFP_FS
));
178 mutex_enter(&vp
->v_lock
);
179 vp
->v_type
= vn_mode_to_vtype(stat
.mode
);
181 vp
->v_gfp_mask
= saved_gfp
;
183 mutex_exit(&vp
->v_lock
);
187 EXPORT_SYMBOL(vn_open
);
190 vn_openat(const char *path
, uio_seg_t seg
, int flags
, int mode
,
191 vnode_t
**vpp
, int x1
, void *x2
, vnode_t
*vp
, int fd
)
196 ASSERT(vp
== rootdir
);
198 len
= strlen(path
) + 2;
199 realpath
= kmalloc(len
, kmem_flags_convert(KM_SLEEP
));
203 (void) snprintf(realpath
, len
, "/%s", path
);
204 rc
= vn_open(realpath
, seg
, flags
, mode
, vpp
, x1
, x2
);
209 EXPORT_SYMBOL(vn_openat
);
212 vn_rdwr(uio_rw_t uio
, vnode_t
*vp
, void *addr
, ssize_t len
, offset_t off
,
213 uio_seg_t seg
, int ioflag
, rlim64_t x2
, void *x3
, ssize_t
*residp
)
215 struct file
*fp
= vp
->v_file
;
219 ASSERT(uio
== UIO_WRITE
|| uio
== UIO_READ
);
220 ASSERT(seg
== UIO_SYSSPACE
);
221 ASSERT((ioflag
& ~FAPPEND
) == 0);
223 if (ioflag
& FAPPEND
)
227 rc
= spl_kernel_write(fp
, addr
, len
, &offset
);
229 rc
= spl_kernel_read(fp
, addr
, len
, &offset
);
245 EXPORT_SYMBOL(vn_rdwr
);
248 vn_close(vnode_t
*vp
, int flags
, int x1
, int x2
, void *x3
, void *x4
)
255 mapping_set_gfp_mask(vp
->v_file
->f_mapping
, vp
->v_gfp_mask
);
256 rc
= filp_close(vp
->v_file
, 0);
261 EXPORT_SYMBOL(vn_close
);
264 * vn_seek() does not actually seek it only performs bounds checking on the
265 * proposed seek. We perform minimal checking and allow vn_rdwr() to catch
266 * anything more serious.
269 vn_seek(vnode_t
*vp
, offset_t ooff
, offset_t
*noffp
, void *ct
)
271 return ((*noffp
< 0 || *noffp
> MAXOFFSET_T
) ? EINVAL
: 0);
273 EXPORT_SYMBOL(vn_seek
);
276 vn_getattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, void *x3
, void *x4
)
288 #if defined(HAVE_4ARGS_VFS_GETATTR)
289 rc
= vfs_getattr(&fp
->f_path
, &stat
, STATX_BASIC_STATS
,
290 AT_STATX_SYNC_AS_STAT
);
291 #elif defined(HAVE_2ARGS_VFS_GETATTR)
292 rc
= vfs_getattr(&fp
->f_path
, &stat
);
294 rc
= vfs_getattr(fp
->f_path
.mnt
, fp
->f_dentry
, &stat
);
299 vap
->va_type
= vn_mode_to_vtype(stat
.mode
);
300 vap
->va_mode
= stat
.mode
;
301 vap
->va_uid
= KUID_TO_SUID(stat
.uid
);
302 vap
->va_gid
= KGID_TO_SGID(stat
.gid
);
304 vap
->va_nodeid
= stat
.ino
;
305 vap
->va_nlink
= stat
.nlink
;
306 vap
->va_size
= stat
.size
;
307 vap
->va_blksize
= stat
.blksize
;
308 vap
->va_atime
= stat
.atime
;
309 vap
->va_mtime
= stat
.mtime
;
310 vap
->va_ctime
= stat
.ctime
;
311 vap
->va_rdev
= stat
.rdev
;
312 vap
->va_nblocks
= stat
.blocks
;
316 EXPORT_SYMBOL(vn_getattr
);
319 vn_fsync(vnode_t
*vp
, int flags
, void *x3
, void *x4
)
332 * May enter XFS which generates a warning when PF_FSTRANS is set.
333 * To avoid this the flag is cleared over vfs_sync() and then reset.
335 fstrans
= __spl_pf_fstrans_check();
337 current
->flags
&= ~(__SPL_PF_FSTRANS
);
339 error
= -spl_filp_fsync(vp
->v_file
, datasync
);
341 current
->flags
|= __SPL_PF_FSTRANS
;
345 EXPORT_SYMBOL(vn_fsync
);
347 int vn_space(vnode_t
*vp
, int cmd
, struct flock
*bfp
, int flag
,
348 offset_t offset
, void *x6
, void *x7
)
350 int error
= EOPNOTSUPP
;
351 #ifdef FALLOC_FL_PUNCH_HOLE
355 if (cmd
!= F_FREESP
|| bfp
->l_whence
!= 0)
360 ASSERT(bfp
->l_start
>= 0 && bfp
->l_len
> 0);
362 #ifdef FALLOC_FL_PUNCH_HOLE
364 * May enter XFS which generates a warning when PF_FSTRANS is set.
365 * To avoid this the flag is cleared over vfs_sync() and then reset.
367 fstrans
= __spl_pf_fstrans_check();
369 current
->flags
&= ~(__SPL_PF_FSTRANS
);
372 * When supported by the underlying file system preferentially
373 * use the fallocate() callback to preallocate the space.
375 error
= -spl_filp_fallocate(vp
->v_file
,
376 FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
,
377 bfp
->l_start
, bfp
->l_len
);
380 current
->flags
|= __SPL_PF_FSTRANS
;
386 #ifdef HAVE_INODE_TRUNCATE_RANGE
387 if (vp
->v_file
->f_dentry
&& vp
->v_file
->f_dentry
->d_inode
&&
388 vp
->v_file
->f_dentry
->d_inode
->i_op
&&
389 vp
->v_file
->f_dentry
->d_inode
->i_op
->truncate_range
) {
390 off_t end
= bfp
->l_start
+ bfp
->l_len
;
392 * Judging from the code in shmem_truncate_range(),
393 * it seems the kernel expects the end offset to be
394 * inclusive and aligned to the end of a page.
396 if (end
% PAGE_SIZE
!= 0) {
397 end
&= ~(off_t
)(PAGE_SIZE
- 1);
398 if (end
<= bfp
->l_start
)
403 vp
->v_file
->f_dentry
->d_inode
->i_op
->truncate_range(
404 vp
->v_file
->f_dentry
->d_inode
, bfp
->l_start
, end
);
412 EXPORT_SYMBOL(vn_space
);
414 /* Function must be called while holding the vn_file_lock */
416 file_find(int fd
, struct task_struct
*task
)
420 list_for_each_entry(fp
, &vn_file_list
, f_list
) {
421 if (fd
== fp
->f_fd
&& fp
->f_task
== task
) {
422 ASSERT(atomic_read(&fp
->f_ref
) != 0);
442 /* Already open just take an extra reference */
443 spin_lock(&vn_file_lock
);
445 fp
= file_find(fd
, current
);
450 * areleasef() can cause us to see a stale reference when
451 * userspace has reused a file descriptor before areleasef()
452 * has run. fput() the stale reference and replace it. We
453 * retain the original reference count such that the concurrent
454 * areleasef() will decrement its reference and terminate.
456 if (lfp
!= fp
->f_file
) {
458 fp
->f_vnode
->v_file
= lfp
;
460 atomic_inc(&fp
->f_ref
);
461 spin_unlock(&vn_file_lock
);
465 spin_unlock(&vn_file_lock
);
467 /* File was not yet opened create the object and setup */
468 fp
= kmem_cache_alloc(vn_file_cache
, KM_SLEEP
);
472 mutex_enter(&fp
->f_lock
);
475 fp
->f_task
= current
;
477 atomic_inc(&fp
->f_ref
);
483 vp
= vn_alloc(KM_SLEEP
);
487 #if defined(HAVE_4ARGS_VFS_GETATTR)
488 rc
= vfs_getattr(&lfp
->f_path
, &stat
, STATX_TYPE
,
489 AT_STATX_SYNC_AS_STAT
);
490 #elif defined(HAVE_2ARGS_VFS_GETATTR)
491 rc
= vfs_getattr(&lfp
->f_path
, &stat
);
493 rc
= vfs_getattr(lfp
->f_path
.mnt
, lfp
->f_dentry
, &stat
);
498 mutex_enter(&vp
->v_lock
);
499 vp
->v_type
= vn_mode_to_vtype(stat
.mode
);
501 mutex_exit(&vp
->v_lock
);
506 /* Put it on the tracking list */
507 spin_lock(&vn_file_lock
);
508 list_add(&fp
->f_list
, &vn_file_list
);
509 spin_unlock(&vn_file_lock
);
511 mutex_exit(&fp
->f_lock
);
519 mutex_exit(&fp
->f_lock
);
520 kmem_cache_free(vn_file_cache
, fp
);
526 static void releasef_locked(file_t
*fp
)
531 /* Unlinked from list, no refs, safe to free outside mutex */
533 vn_free(fp
->f_vnode
);
535 kmem_cache_free(vn_file_cache
, fp
);
541 areleasef(fd
, P_FINFO(current
));
543 EXPORT_SYMBOL(releasef
);
546 vn_areleasef(int fd
, uf_info_t
*fip
)
549 struct task_struct
*task
= (struct task_struct
*)fip
;
554 spin_lock(&vn_file_lock
);
555 fp
= file_find(fd
, task
);
557 atomic_dec(&fp
->f_ref
);
558 if (atomic_read(&fp
->f_ref
) > 0) {
559 spin_unlock(&vn_file_lock
);
563 list_del(&fp
->f_list
);
566 spin_unlock(&vn_file_lock
);
568 EXPORT_SYMBOL(areleasef
);
572 #ifdef HAVE_SET_FS_PWD_WITH_CONST
573 vn_set_fs_pwd(struct fs_struct
*fs
, const struct path
*path
)
575 vn_set_fs_pwd(struct fs_struct
*fs
, struct path
*path
)
576 #endif /* HAVE_SET_FS_PWD_WITH_CONST */
580 #ifdef HAVE_FS_STRUCT_SPINLOCK
581 spin_lock(&fs
->lock
);
585 spin_unlock(&fs
->lock
);
587 write_lock(&fs
->lock
);
591 write_unlock(&fs
->lock
);
592 #endif /* HAVE_FS_STRUCT_SPINLOCK */
599 vn_set_pwd(const char *filename
)
602 mm_segment_t saved_fs
;
606 * user_path_dir() and __user_walk() both expect 'filename' to be
607 * a user space address so we must briefly increase the data segment
608 * size to ensure strncpy_from_user() does not fail with -EFAULT.
613 rc
= user_path_dir(filename
, &path
);
617 rc
= inode_permission(path
.dentry
->d_inode
, MAY_EXEC
| MAY_ACCESS
);
621 vn_set_fs_pwd(current
->fs
, &path
);
630 EXPORT_SYMBOL(vn_set_pwd
);
633 vn_cache_constructor(void *buf
, void *cdrarg
, int kmflags
)
635 struct vnode
*vp
= buf
;
637 mutex_init(&vp
->v_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
640 } /* vn_cache_constructor() */
643 vn_cache_destructor(void *buf
, void *cdrarg
)
645 struct vnode
*vp
= buf
;
647 mutex_destroy(&vp
->v_lock
);
648 } /* vn_cache_destructor() */
651 vn_file_cache_constructor(void *buf
, void *cdrarg
, int kmflags
)
655 atomic_set(&fp
->f_ref
, 0);
656 mutex_init(&fp
->f_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
657 INIT_LIST_HEAD(&fp
->f_list
);
660 } /* vn_file_cache_constructor() */
663 vn_file_cache_destructor(void *buf
, void *cdrarg
)
667 mutex_destroy(&fp
->f_lock
);
668 } /* vn_file_cache_destructor() */
673 vn_cache
= kmem_cache_create("spl_vn_cache",
674 sizeof (struct vnode
), 64, vn_cache_constructor
,
675 vn_cache_destructor
, NULL
, NULL
, NULL
, 0);
677 vn_file_cache
= kmem_cache_create("spl_vn_file_cache",
678 sizeof (file_t
), 64, vn_file_cache_constructor
,
679 vn_file_cache_destructor
, NULL
, NULL
, NULL
, 0);
682 } /* spl_vn_init() */
687 file_t
*fp
, *next_fp
;
690 spin_lock(&vn_file_lock
);
692 list_for_each_entry_safe(fp
, next_fp
, &vn_file_list
, f_list
) {
693 list_del(&fp
->f_list
);
698 spin_unlock(&vn_file_lock
);
701 printk(KERN_WARNING
"WARNING: %d vnode files leaked\n", leaked
);
703 kmem_cache_destroy(vn_file_cache
);
704 kmem_cache_destroy(vn_cache
);
705 } /* spl_vn_fini() */