2017-10-04 16:33:43 +00:00
|
|
|
/*
|
2010-05-17 22:18:00 +00:00
|
|
|
* Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
|
|
|
|
* Copyright (C) 2007 The Regents of the University of California.
|
|
|
|
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
|
|
|
|
* Written by Brian Behlendorf <behlendorf1@llnl.gov>.
|
2008-05-26 04:38:26 +00:00
|
|
|
* UCRL-CODE-235197
|
|
|
|
*
|
2010-05-17 22:18:00 +00:00
|
|
|
* This file is part of the SPL, Solaris Porting Layer.
|
2013-03-05 01:26:55 +00:00
|
|
|
* For details, see <http://zfsonlinux.org/>.
|
2010-05-17 22:18:00 +00:00
|
|
|
*
|
|
|
|
* The SPL is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version.
|
2008-05-26 04:38:26 +00:00
|
|
|
*
|
2010-05-17 22:18:00 +00:00
|
|
|
* The SPL is distributed in the hope that it will be useful, but WITHOUT
|
2008-05-26 04:38:26 +00:00
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2010-05-17 22:18:00 +00:00
|
|
|
* with the SPL. If not, see <http://www.gnu.org/licenses/>.
|
2018-02-07 19:49:38 +00:00
|
|
|
*
|
2010-05-17 22:18:00 +00:00
|
|
|
* Solaris Porting Layer (SPL) Vnode Implementation.
|
2017-10-04 16:33:43 +00:00
|
|
|
*/
|
2008-05-26 04:38:26 +00:00
|
|
|
|
2013-07-13 20:34:56 +00:00
|
|
|
#include <sys/cred.h>
|
2008-03-12 20:52:46 +00:00
|
|
|
#include <sys/vnode.h>
|
2014-12-08 18:04:42 +00:00
|
|
|
#include <sys/kmem_cache.h>
|
2012-09-03 12:56:26 +00:00
|
|
|
#include <linux/falloc.h>
|
2014-11-14 18:18:05 +00:00
|
|
|
#include <linux/file_compat.h>
|
2008-04-21 17:29:47 +00:00
|
|
|
|
2009-10-01 23:06:15 +00:00
|
|
|
vnode_t *rootdir = (vnode_t *)0xabcd1234;
|
2008-03-12 20:52:46 +00:00
|
|
|
EXPORT_SYMBOL(rootdir);
|
|
|
|
|
2008-08-05 04:16:09 +00:00
|
|
|
static spl_kmem_cache_t *vn_cache;
|
|
|
|
static spl_kmem_cache_t *vn_file_cache;
|
2008-03-18 23:20:30 +00:00
|
|
|
|
2011-04-20 19:01:11 +00:00
|
|
|
static DEFINE_SPINLOCK(vn_file_lock);
|
2008-03-18 23:20:30 +00:00
|
|
|
static LIST_HEAD(vn_file_list);
|
2008-03-14 00:04:01 +00:00
|
|
|
|
2011-01-11 19:53:05 +00:00
|
|
|
vtype_t
|
|
|
|
vn_mode_to_vtype(mode_t mode)
|
2008-03-12 20:52:46 +00:00
|
|
|
{
|
|
|
|
if (S_ISREG(mode))
|
2018-02-07 19:49:38 +00:00
|
|
|
return (VREG);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
|
|
|
if (S_ISDIR(mode))
|
2018-02-07 19:49:38 +00:00
|
|
|
return (VDIR);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
|
|
|
if (S_ISCHR(mode))
|
2018-02-07 19:49:38 +00:00
|
|
|
return (VCHR);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
|
|
|
if (S_ISBLK(mode))
|
2018-02-07 19:49:38 +00:00
|
|
|
return (VBLK);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
|
|
|
if (S_ISFIFO(mode))
|
2018-02-07 19:49:38 +00:00
|
|
|
return (VFIFO);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
|
|
|
if (S_ISLNK(mode))
|
2018-02-07 19:49:38 +00:00
|
|
|
return (VLNK);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
|
|
|
if (S_ISSOCK(mode))
|
2018-02-07 19:49:38 +00:00
|
|
|
return (VSOCK);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
return (VNON);
|
2011-01-11 19:53:05 +00:00
|
|
|
} /* vn_mode_to_vtype() */
|
|
|
|
EXPORT_SYMBOL(vn_mode_to_vtype);
|
|
|
|
|
|
|
|
mode_t
|
|
|
|
vn_vtype_to_mode(vtype_t vtype)
|
|
|
|
{
|
|
|
|
if (vtype == VREG)
|
2018-02-07 19:49:38 +00:00
|
|
|
return (S_IFREG);
|
2011-01-11 19:53:05 +00:00
|
|
|
|
|
|
|
if (vtype == VDIR)
|
2018-02-07 19:49:38 +00:00
|
|
|
return (S_IFDIR);
|
2011-01-11 19:53:05 +00:00
|
|
|
|
|
|
|
if (vtype == VCHR)
|
2018-02-07 19:49:38 +00:00
|
|
|
return (S_IFCHR);
|
2011-01-11 19:53:05 +00:00
|
|
|
|
|
|
|
if (vtype == VBLK)
|
2018-02-07 19:49:38 +00:00
|
|
|
return (S_IFBLK);
|
2011-01-11 19:53:05 +00:00
|
|
|
|
|
|
|
if (vtype == VFIFO)
|
2018-02-07 19:49:38 +00:00
|
|
|
return (S_IFIFO);
|
2011-01-11 19:53:05 +00:00
|
|
|
|
|
|
|
if (vtype == VLNK)
|
2018-02-07 19:49:38 +00:00
|
|
|
return (S_IFLNK);
|
2011-01-11 19:53:05 +00:00
|
|
|
|
|
|
|
if (vtype == VSOCK)
|
2018-02-07 19:49:38 +00:00
|
|
|
return (S_IFSOCK);
|
2011-01-11 19:53:05 +00:00
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
return (VNON);
|
2011-01-11 19:53:05 +00:00
|
|
|
} /* vn_vtype_to_mode() */
|
|
|
|
EXPORT_SYMBOL(vn_vtype_to_mode);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
2008-03-14 00:04:01 +00:00
|
|
|
vnode_t *
|
|
|
|
vn_alloc(int flag)
|
|
|
|
{
|
|
|
|
vnode_t *vp;
|
|
|
|
|
|
|
|
vp = kmem_cache_alloc(vn_cache, flag);
|
|
|
|
if (vp != NULL) {
|
2008-03-18 23:20:30 +00:00
|
|
|
vp->v_file = NULL;
|
2008-03-14 00:04:01 +00:00
|
|
|
vp->v_type = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-05 22:30:35 +00:00
|
|
|
return (vp);
|
2008-03-14 00:04:01 +00:00
|
|
|
} /* vn_alloc() */
|
|
|
|
EXPORT_SYMBOL(vn_alloc);
|
|
|
|
|
|
|
|
void
|
|
|
|
vn_free(vnode_t *vp)
|
|
|
|
{
|
|
|
|
kmem_cache_free(vn_cache, vp);
|
|
|
|
} /* vn_free() */
|
|
|
|
EXPORT_SYMBOL(vn_free);
|
|
|
|
|
2008-03-07 23:07:02 +00:00
|
|
|
int
|
2008-03-14 00:04:01 +00:00
|
|
|
vn_open(const char *path, uio_seg_t seg, int flags, int mode,
|
2008-03-12 20:52:46 +00:00
|
|
|
vnode_t **vpp, int x1, void *x2)
|
2008-03-07 23:07:02 +00:00
|
|
|
{
|
2010-01-07 16:58:30 +00:00
|
|
|
struct file *fp;
|
|
|
|
struct kstat stat;
|
|
|
|
int rc, saved_umask = 0;
|
2010-08-12 16:24:31 +00:00
|
|
|
gfp_t saved_gfp;
|
2008-03-07 23:07:02 +00:00
|
|
|
vnode_t *vp;
|
|
|
|
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(flags & (FWRITE | FREAD));
|
|
|
|
ASSERT(seg == UIO_SYSSPACE);
|
|
|
|
ASSERT(vpp);
|
2008-03-12 20:52:46 +00:00
|
|
|
*vpp = NULL;
|
|
|
|
|
|
|
|
if (!(flags & FCREAT) && (flags & FWRITE))
|
|
|
|
flags |= FEXCL;
|
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
/*
|
|
|
|
* Note for filp_open() the two low bits must be remapped to mean:
|
2008-04-04 17:08:12 +00:00
|
|
|
* 01 - read-only -> 00 read-only
|
|
|
|
* 10 - write-only -> 01 write-only
|
|
|
|
* 11 - read-write -> 10 read-write
|
|
|
|
*/
|
|
|
|
flags--;
|
2008-03-07 23:07:02 +00:00
|
|
|
|
|
|
|
if (flags & FCREAT)
|
2008-03-12 20:52:46 +00:00
|
|
|
saved_umask = xchg(¤t->fs->umask, 0);
|
2008-03-07 23:07:02 +00:00
|
|
|
|
2010-01-07 16:58:30 +00:00
|
|
|
fp = filp_open(path, flags, mode);
|
2008-03-07 23:07:02 +00:00
|
|
|
|
|
|
|
if (flags & FCREAT)
|
2018-02-07 19:49:38 +00:00
|
|
|
(void) xchg(¤t->fs->umask, saved_umask);
|
2008-03-07 23:07:02 +00:00
|
|
|
|
2010-01-07 16:58:30 +00:00
|
|
|
if (IS_ERR(fp))
|
2014-11-05 22:30:35 +00:00
|
|
|
return (-PTR_ERR(fp));
|
2008-03-07 23:07:02 +00:00
|
|
|
|
2017-03-07 21:18:53 +00:00
|
|
|
#if defined(HAVE_4ARGS_VFS_GETATTR)
|
|
|
|
rc = vfs_getattr(&fp->f_path, &stat, STATX_TYPE, AT_STATX_SYNC_AS_STAT);
|
|
|
|
#elif defined(HAVE_2ARGS_VFS_GETATTR)
|
2013-03-04 05:02:43 +00:00
|
|
|
rc = vfs_getattr(&fp->f_path, &stat);
|
|
|
|
#else
|
2013-03-04 04:45:33 +00:00
|
|
|
rc = vfs_getattr(fp->f_path.mnt, fp->f_dentry, &stat);
|
2013-03-04 05:02:43 +00:00
|
|
|
#endif
|
2008-03-12 20:52:46 +00:00
|
|
|
if (rc) {
|
|
|
|
filp_close(fp, 0);
|
2014-11-05 22:30:35 +00:00
|
|
|
return (-rc);
|
2008-03-07 23:07:02 +00:00
|
|
|
}
|
|
|
|
|
2008-03-14 00:04:01 +00:00
|
|
|
vp = vn_alloc(KM_SLEEP);
|
2008-03-12 20:52:46 +00:00
|
|
|
if (!vp) {
|
|
|
|
filp_close(fp, 0);
|
2014-11-05 22:30:35 +00:00
|
|
|
return (ENOMEM);
|
2008-03-12 20:52:46 +00:00
|
|
|
}
|
2008-03-07 23:07:02 +00:00
|
|
|
|
2010-08-12 16:24:31 +00:00
|
|
|
saved_gfp = mapping_gfp_mask(fp->f_mapping);
|
|
|
|
mapping_set_gfp_mask(fp->f_mapping, saved_gfp & ~(__GFP_IO|__GFP_FS));
|
|
|
|
|
2008-03-18 23:20:30 +00:00
|
|
|
mutex_enter(&vp->v_lock);
|
2011-01-11 19:53:05 +00:00
|
|
|
vp->v_type = vn_mode_to_vtype(stat.mode);
|
2008-03-18 23:20:30 +00:00
|
|
|
vp->v_file = fp;
|
2010-08-12 16:24:31 +00:00
|
|
|
vp->v_gfp_mask = saved_gfp;
|
2008-03-12 20:52:46 +00:00
|
|
|
*vpp = vp;
|
2008-03-18 23:20:30 +00:00
|
|
|
mutex_exit(&vp->v_lock);
|
2008-03-07 23:07:02 +00:00
|
|
|
|
2014-11-05 22:30:35 +00:00
|
|
|
return (0);
|
2008-03-12 20:52:46 +00:00
|
|
|
} /* vn_open() */
|
|
|
|
EXPORT_SYMBOL(vn_open);
|
2008-03-07 23:07:02 +00:00
|
|
|
|
|
|
|
int
|
2008-03-14 00:04:01 +00:00
|
|
|
vn_openat(const char *path, uio_seg_t seg, int flags, int mode,
|
2018-02-07 19:49:38 +00:00
|
|
|
vnode_t **vpp, int x1, void *x2, vnode_t *vp, int fd)
|
2008-03-07 23:07:02 +00:00
|
|
|
{
|
2008-03-12 20:52:46 +00:00
|
|
|
char *realpath;
|
2008-11-04 23:30:15 +00:00
|
|
|
int len, rc;
|
2008-03-07 23:07:02 +00:00
|
|
|
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(vp == rootdir);
|
2008-03-07 23:07:02 +00:00
|
|
|
|
2008-11-04 23:30:15 +00:00
|
|
|
len = strlen(path) + 2;
|
2015-01-21 17:21:51 +00:00
|
|
|
realpath = kmalloc(len, kmem_flags_convert(KM_SLEEP));
|
2008-03-12 20:52:46 +00:00
|
|
|
if (!realpath)
|
2014-11-05 22:30:35 +00:00
|
|
|
return (ENOMEM);
|
2008-03-07 23:07:02 +00:00
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
(void) snprintf(realpath, len, "/%s", path);
|
2008-03-12 20:52:46 +00:00
|
|
|
rc = vn_open(realpath, seg, flags, mode, vpp, x1, x2);
|
|
|
|
kfree(realpath);
|
|
|
|
|
2014-11-05 22:30:35 +00:00
|
|
|
return (rc);
|
2008-03-12 20:52:46 +00:00
|
|
|
} /* vn_openat() */
|
|
|
|
EXPORT_SYMBOL(vn_openat);
|
2008-03-07 23:07:02 +00:00
|
|
|
|
|
|
|
int
|
2008-03-12 20:52:46 +00:00
|
|
|
vn_rdwr(uio_rw_t uio, vnode_t *vp, void *addr, ssize_t len, offset_t off,
|
2010-04-23 19:19:10 +00:00
|
|
|
uio_seg_t seg, int ioflag, rlim64_t x2, void *x3, ssize_t *residp)
|
2008-03-07 23:07:02 +00:00
|
|
|
{
|
2017-11-16 01:19:23 +00:00
|
|
|
struct file *fp = vp->v_file;
|
|
|
|
loff_t offset = off;
|
2008-03-12 20:52:46 +00:00
|
|
|
int rc;
|
|
|
|
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(uio == UIO_WRITE || uio == UIO_READ);
|
|
|
|
ASSERT(seg == UIO_SYSSPACE);
|
2010-04-23 19:19:10 +00:00
|
|
|
ASSERT((ioflag & ~FAPPEND) == 0);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
2010-04-23 19:19:10 +00:00
|
|
|
if (ioflag & FAPPEND)
|
|
|
|
offset = fp->f_pos;
|
|
|
|
|
2008-03-12 20:52:46 +00:00
|
|
|
if (uio & UIO_WRITE)
|
2017-11-16 01:19:23 +00:00
|
|
|
rc = spl_kernel_write(fp, addr, len, &offset);
|
2008-03-12 20:52:46 +00:00
|
|
|
else
|
2017-11-16 01:19:23 +00:00
|
|
|
rc = spl_kernel_read(fp, addr, len, &offset);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
2011-10-12 10:49:18 +00:00
|
|
|
fp->f_pos = offset;
|
2008-03-12 20:52:46 +00:00
|
|
|
|
|
|
|
if (rc < 0)
|
2014-11-05 22:30:35 +00:00
|
|
|
return (-rc);
|
2008-03-07 23:07:02 +00:00
|
|
|
|
2008-03-12 20:52:46 +00:00
|
|
|
if (residp) {
|
|
|
|
*residp = len - rc;
|
2008-03-07 23:07:02 +00:00
|
|
|
} else {
|
2008-03-12 20:52:46 +00:00
|
|
|
if (rc != len)
|
2014-11-05 22:30:35 +00:00
|
|
|
return (EIO);
|
2008-03-07 23:07:02 +00:00
|
|
|
}
|
|
|
|
|
2014-11-05 22:30:35 +00:00
|
|
|
return (0);
|
2008-03-12 20:52:46 +00:00
|
|
|
} /* vn_rdwr() */
|
|
|
|
EXPORT_SYMBOL(vn_rdwr);
|
|
|
|
|
|
|
|
int
|
2008-03-12 21:33:28 +00:00
|
|
|
vn_close(vnode_t *vp, int flags, int x1, int x2, void *x3, void *x4)
|
2008-03-12 20:52:46 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(vp);
|
|
|
|
ASSERT(vp->v_file);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
2010-08-12 16:24:31 +00:00
|
|
|
mapping_set_gfp_mask(vp->v_file->f_mapping, vp->v_gfp_mask);
|
2009-01-09 21:59:39 +00:00
|
|
|
rc = filp_close(vp->v_file, 0);
|
|
|
|
vn_free(vp);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
2014-11-05 22:30:35 +00:00
|
|
|
return (-rc);
|
2008-03-12 20:52:46 +00:00
|
|
|
} /* vn_close() */
|
|
|
|
EXPORT_SYMBOL(vn_close);
|
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
/*
|
|
|
|
* vn_seek() does not actually seek it only performs bounds checking on the
|
2009-01-09 21:59:39 +00:00
|
|
|
* proposed seek. We perform minimal checking and allow vn_rdwr() to catch
|
2018-02-07 19:49:38 +00:00
|
|
|
* anything more serious.
|
|
|
|
*/
|
2009-01-09 21:59:39 +00:00
|
|
|
int
|
2011-03-01 22:02:06 +00:00
|
|
|
vn_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, void *ct)
|
2009-01-09 21:59:39 +00:00
|
|
|
{
|
|
|
|
return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vn_seek);
|
|
|
|
|
2008-03-12 20:52:46 +00:00
|
|
|
int
|
2008-03-12 23:48:28 +00:00
|
|
|
vn_getattr(vnode_t *vp, vattr_t *vap, int flags, void *x3, void *x4)
|
2008-03-07 23:07:02 +00:00
|
|
|
{
|
2008-03-12 20:52:46 +00:00
|
|
|
struct file *fp;
|
2011-01-11 19:56:00 +00:00
|
|
|
struct kstat stat;
|
2008-03-12 20:52:46 +00:00
|
|
|
int rc;
|
|
|
|
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(vp);
|
|
|
|
ASSERT(vp->v_file);
|
|
|
|
ASSERT(vap);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
2008-03-18 23:20:30 +00:00
|
|
|
fp = vp->v_file;
|
2008-03-12 20:52:46 +00:00
|
|
|
|
2017-03-07 21:18:53 +00:00
|
|
|
#if defined(HAVE_4ARGS_VFS_GETATTR)
|
|
|
|
rc = vfs_getattr(&fp->f_path, &stat, STATX_BASIC_STATS,
|
|
|
|
AT_STATX_SYNC_AS_STAT);
|
|
|
|
#elif defined(HAVE_2ARGS_VFS_GETATTR)
|
2013-03-04 05:02:43 +00:00
|
|
|
rc = vfs_getattr(&fp->f_path, &stat);
|
|
|
|
#else
|
|
|
|
rc = vfs_getattr(fp->f_path.mnt, fp->f_dentry, &stat);
|
|
|
|
#endif
|
2008-03-12 20:52:46 +00:00
|
|
|
if (rc)
|
2014-11-05 22:30:35 +00:00
|
|
|
return (-rc);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
vap->va_type = vn_mode_to_vtype(stat.mode);
|
|
|
|
vap->va_mode = stat.mode;
|
|
|
|
vap->va_uid = KUID_TO_SUID(stat.uid);
|
|
|
|
vap->va_gid = KGID_TO_SGID(stat.gid);
|
|
|
|
vap->va_fsid = 0;
|
|
|
|
vap->va_nodeid = stat.ino;
|
|
|
|
vap->va_nlink = stat.nlink;
|
|
|
|
vap->va_size = stat.size;
|
|
|
|
vap->va_blksize = stat.blksize;
|
|
|
|
vap->va_atime = stat.atime;
|
|
|
|
vap->va_mtime = stat.mtime;
|
|
|
|
vap->va_ctime = stat.ctime;
|
|
|
|
vap->va_rdev = stat.rdev;
|
|
|
|
vap->va_nblocks = stat.blocks;
|
2008-03-12 20:52:46 +00:00
|
|
|
|
2014-11-05 22:30:35 +00:00
|
|
|
return (0);
|
2008-03-07 23:07:02 +00:00
|
|
|
}
|
2008-03-12 20:52:46 +00:00
|
|
|
EXPORT_SYMBOL(vn_getattr);
|
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
int
|
|
|
|
vn_fsync(vnode_t *vp, int flags, void *x3, void *x4)
|
2008-03-12 20:52:46 +00:00
|
|
|
{
|
2008-03-12 23:48:28 +00:00
|
|
|
int datasync = 0;
|
2015-04-07 17:05:17 +00:00
|
|
|
int error;
|
|
|
|
int fstrans;
|
2008-03-12 23:48:28 +00:00
|
|
|
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(vp);
|
|
|
|
ASSERT(vp->v_file);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
2008-03-12 23:48:28 +00:00
|
|
|
if (flags & FDSYNC)
|
|
|
|
datasync = 1;
|
|
|
|
|
2015-04-07 17:05:17 +00:00
|
|
|
/*
|
|
|
|
* May enter XFS which generates a warning when PF_FSTRANS is set.
|
|
|
|
* To avoid this the flag is cleared over vfs_sync() and then reset.
|
|
|
|
*/
|
2017-05-09 17:36:54 +00:00
|
|
|
fstrans = __spl_pf_fstrans_check();
|
2015-04-07 17:05:17 +00:00
|
|
|
if (fstrans)
|
2017-05-09 17:36:54 +00:00
|
|
|
current->flags &= ~(__SPL_PF_FSTRANS);
|
2015-04-07 17:05:17 +00:00
|
|
|
|
|
|
|
error = -spl_filp_fsync(vp->v_file, datasync);
|
|
|
|
if (fstrans)
|
2017-05-09 17:36:54 +00:00
|
|
|
current->flags |= __SPL_PF_FSTRANS;
|
2015-04-07 17:05:17 +00:00
|
|
|
|
|
|
|
return (error);
|
2008-03-12 20:52:46 +00:00
|
|
|
} /* vn_fsync() */
|
|
|
|
EXPORT_SYMBOL(vn_fsync);
|
2008-03-14 00:04:01 +00:00
|
|
|
|
2012-09-03 12:56:26 +00:00
|
|
|
int vn_space(vnode_t *vp, int cmd, struct flock *bfp, int flag,
|
|
|
|
offset_t offset, void *x6, void *x7)
|
|
|
|
{
|
|
|
|
int error = EOPNOTSUPP;
|
2016-04-26 11:33:52 +00:00
|
|
|
#ifdef FALLOC_FL_PUNCH_HOLE
|
|
|
|
int fstrans;
|
|
|
|
#endif
|
2012-09-03 12:56:26 +00:00
|
|
|
|
|
|
|
if (cmd != F_FREESP || bfp->l_whence != 0)
|
2014-11-05 22:30:35 +00:00
|
|
|
return (EOPNOTSUPP);
|
2012-09-03 12:56:26 +00:00
|
|
|
|
|
|
|
ASSERT(vp);
|
|
|
|
ASSERT(vp->v_file);
|
|
|
|
ASSERT(bfp->l_start >= 0 && bfp->l_len > 0);
|
|
|
|
|
|
|
|
#ifdef FALLOC_FL_PUNCH_HOLE
|
2016-04-26 11:33:52 +00:00
|
|
|
/*
|
|
|
|
* May enter XFS which generates a warning when PF_FSTRANS is set.
|
|
|
|
* To avoid this the flag is cleared over vfs_sync() and then reset.
|
|
|
|
*/
|
2017-05-09 17:36:54 +00:00
|
|
|
fstrans = __spl_pf_fstrans_check();
|
2016-04-26 11:33:52 +00:00
|
|
|
if (fstrans)
|
2017-05-09 17:36:54 +00:00
|
|
|
current->flags &= ~(__SPL_PF_FSTRANS);
|
2016-04-26 11:33:52 +00:00
|
|
|
|
2013-01-08 17:42:49 +00:00
|
|
|
/*
|
|
|
|
* When supported by the underlying file system preferentially
|
|
|
|
* use the fallocate() callback to preallocate the space.
|
|
|
|
*/
|
|
|
|
error = -spl_filp_fallocate(vp->v_file,
|
|
|
|
FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
|
|
|
|
bfp->l_start, bfp->l_len);
|
2016-04-26 11:33:52 +00:00
|
|
|
|
|
|
|
if (fstrans)
|
2017-05-09 17:36:54 +00:00
|
|
|
current->flags |= __SPL_PF_FSTRANS;
|
2016-04-26 11:33:52 +00:00
|
|
|
|
2013-01-08 17:42:49 +00:00
|
|
|
if (error == 0)
|
2014-11-05 22:30:35 +00:00
|
|
|
return (0);
|
2012-09-03 12:56:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_INODE_TRUNCATE_RANGE
|
|
|
|
if (vp->v_file->f_dentry && vp->v_file->f_dentry->d_inode &&
|
|
|
|
vp->v_file->f_dentry->d_inode->i_op &&
|
|
|
|
vp->v_file->f_dentry->d_inode->i_op->truncate_range) {
|
|
|
|
off_t end = bfp->l_start + bfp->l_len;
|
|
|
|
/*
|
|
|
|
* Judging from the code in shmem_truncate_range(),
|
|
|
|
* it seems the kernel expects the end offset to be
|
|
|
|
* inclusive and aligned to the end of a page.
|
|
|
|
*/
|
|
|
|
if (end % PAGE_SIZE != 0) {
|
|
|
|
end &= ~(off_t)(PAGE_SIZE - 1);
|
|
|
|
if (end <= bfp->l_start)
|
2014-11-05 22:30:35 +00:00
|
|
|
return (0);
|
2012-09-03 12:56:26 +00:00
|
|
|
}
|
|
|
|
--end;
|
|
|
|
|
|
|
|
vp->v_file->f_dentry->d_inode->i_op->truncate_range(
|
|
|
|
vp->v_file->f_dentry->d_inode,
|
|
|
|
bfp->l_start, end
|
|
|
|
);
|
2014-11-05 22:30:35 +00:00
|
|
|
return (0);
|
2012-09-03 12:56:26 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-11-05 22:30:35 +00:00
|
|
|
return (error);
|
2012-09-03 12:56:26 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vn_space);
|
|
|
|
|
2008-03-18 23:20:30 +00:00
|
|
|
/* Function must be called while holding the vn_file_lock */
|
|
|
|
static file_t *
|
2015-02-17 15:12:56 +00:00
|
|
|
file_find(int fd, struct task_struct *task)
|
2008-03-18 23:20:30 +00:00
|
|
|
{
|
2018-02-07 19:49:38 +00:00
|
|
|
file_t *fp;
|
2008-03-18 23:20:30 +00:00
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
list_for_each_entry(fp, &vn_file_list, f_list) {
|
2015-02-17 15:12:56 +00:00
|
|
|
if (fd == fp->f_fd && fp->f_task == task) {
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(atomic_read(&fp->f_ref) != 0);
|
2018-02-07 19:49:38 +00:00
|
|
|
return (fp);
|
2008-03-18 23:20:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
return (NULL);
|
2008-03-18 23:20:30 +00:00
|
|
|
} /* file_find() */
|
|
|
|
|
|
|
|
file_t *
|
|
|
|
vn_getf(int fd)
|
|
|
|
{
|
2018-02-07 19:49:38 +00:00
|
|
|
struct kstat stat;
|
2008-03-18 23:20:30 +00:00
|
|
|
struct file *lfp;
|
|
|
|
file_t *fp;
|
|
|
|
vnode_t *vp;
|
2008-04-21 17:29:47 +00:00
|
|
|
int rc = 0;
|
2008-03-18 23:20:30 +00:00
|
|
|
|
2015-04-16 14:29:41 +00:00
|
|
|
if (fd < 0)
|
|
|
|
return (NULL);
|
|
|
|
|
2008-03-18 23:20:30 +00:00
|
|
|
/* Already open just take an extra reference */
|
|
|
|
spin_lock(&vn_file_lock);
|
|
|
|
|
2015-02-17 15:12:56 +00:00
|
|
|
fp = file_find(fd, current);
|
2008-03-18 23:20:30 +00:00
|
|
|
if (fp) {
|
2015-11-04 21:41:13 +00:00
|
|
|
lfp = fget(fd);
|
|
|
|
fput(fp->f_file);
|
|
|
|
/*
|
|
|
|
* areleasef() can cause us to see a stale reference when
|
|
|
|
* userspace has reused a file descriptor before areleasef()
|
|
|
|
* has run. fput() the stale reference and replace it. We
|
|
|
|
* retain the original reference count such that the concurrent
|
|
|
|
* areleasef() will decrement its reference and terminate.
|
|
|
|
*/
|
|
|
|
if (lfp != fp->f_file) {
|
|
|
|
fp->f_file = lfp;
|
|
|
|
fp->f_vnode->v_file = lfp;
|
|
|
|
}
|
2008-03-18 23:20:30 +00:00
|
|
|
atomic_inc(&fp->f_ref);
|
|
|
|
spin_unlock(&vn_file_lock);
|
2014-11-05 22:30:35 +00:00
|
|
|
return (fp);
|
2008-03-18 23:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock(&vn_file_lock);
|
|
|
|
|
|
|
|
/* File was not yet opened create the object and setup */
|
2008-06-25 20:57:45 +00:00
|
|
|
fp = kmem_cache_alloc(vn_file_cache, KM_SLEEP);
|
2008-03-18 23:20:30 +00:00
|
|
|
if (fp == NULL)
|
2014-11-05 22:30:35 +00:00
|
|
|
goto out;
|
2008-03-18 23:20:30 +00:00
|
|
|
|
|
|
|
mutex_enter(&fp->f_lock);
|
|
|
|
|
|
|
|
fp->f_fd = fd;
|
2011-10-11 16:50:52 +00:00
|
|
|
fp->f_task = current;
|
2008-03-18 23:20:30 +00:00
|
|
|
fp->f_offset = 0;
|
|
|
|
atomic_inc(&fp->f_ref);
|
|
|
|
|
|
|
|
lfp = fget(fd);
|
|
|
|
if (lfp == NULL)
|
2014-11-05 22:30:35 +00:00
|
|
|
goto out_mutex;
|
2008-03-18 23:20:30 +00:00
|
|
|
|
|
|
|
vp = vn_alloc(KM_SLEEP);
|
|
|
|
if (vp == NULL)
|
2014-11-05 22:30:35 +00:00
|
|
|
goto out_fget;
|
2008-03-18 23:20:30 +00:00
|
|
|
|
2017-03-07 21:18:53 +00:00
|
|
|
#if defined(HAVE_4ARGS_VFS_GETATTR)
|
2018-02-07 19:49:38 +00:00
|
|
|
rc = vfs_getattr(&lfp->f_path, &stat, STATX_TYPE,
|
|
|
|
AT_STATX_SYNC_AS_STAT);
|
2017-03-07 21:18:53 +00:00
|
|
|
#elif defined(HAVE_2ARGS_VFS_GETATTR)
|
2013-03-04 05:02:43 +00:00
|
|
|
rc = vfs_getattr(&lfp->f_path, &stat);
|
|
|
|
#else
|
|
|
|
rc = vfs_getattr(lfp->f_path.mnt, lfp->f_dentry, &stat);
|
|
|
|
#endif
|
2018-02-07 19:49:38 +00:00
|
|
|
if (rc)
|
2014-11-05 22:30:35 +00:00
|
|
|
goto out_vnode;
|
2008-03-18 23:20:30 +00:00
|
|
|
|
|
|
|
mutex_enter(&vp->v_lock);
|
2011-01-11 19:53:05 +00:00
|
|
|
vp->v_type = vn_mode_to_vtype(stat.mode);
|
2008-03-18 23:20:30 +00:00
|
|
|
vp->v_file = lfp;
|
|
|
|
mutex_exit(&vp->v_lock);
|
|
|
|
|
|
|
|
fp->f_vnode = vp;
|
|
|
|
fp->f_file = lfp;
|
|
|
|
|
|
|
|
/* Put it on the tracking list */
|
|
|
|
spin_lock(&vn_file_lock);
|
|
|
|
list_add(&fp->f_list, &vn_file_list);
|
|
|
|
spin_unlock(&vn_file_lock);
|
|
|
|
|
|
|
|
mutex_exit(&fp->f_lock);
|
2014-11-05 22:30:35 +00:00
|
|
|
return (fp);
|
2008-03-18 23:20:30 +00:00
|
|
|
|
|
|
|
out_vnode:
|
|
|
|
vn_free(vp);
|
|
|
|
out_fget:
|
|
|
|
fput(lfp);
|
|
|
|
out_mutex:
|
|
|
|
mutex_exit(&fp->f_lock);
|
|
|
|
kmem_cache_free(vn_file_cache, fp);
|
|
|
|
out:
|
2018-02-07 19:49:38 +00:00
|
|
|
return (NULL);
|
2008-03-18 23:20:30 +00:00
|
|
|
} /* getf() */
|
|
|
|
EXPORT_SYMBOL(getf);
|
|
|
|
|
|
|
|
static void releasef_locked(file_t *fp)
|
|
|
|
{
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(fp->f_file);
|
|
|
|
ASSERT(fp->f_vnode);
|
2008-03-18 23:20:30 +00:00
|
|
|
|
|
|
|
/* Unlinked from list, no refs, safe to free outside mutex */
|
|
|
|
fput(fp->f_file);
|
|
|
|
vn_free(fp->f_vnode);
|
|
|
|
|
|
|
|
kmem_cache_free(vn_file_cache, fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
vn_releasef(int fd)
|
2015-02-17 15:12:56 +00:00
|
|
|
{
|
|
|
|
areleasef(fd, P_FINFO(current));
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(releasef);
|
|
|
|
|
|
|
|
void
|
|
|
|
vn_areleasef(int fd, uf_info_t *fip)
|
2008-03-18 23:20:30 +00:00
|
|
|
{
|
|
|
|
file_t *fp;
|
2015-02-17 15:12:56 +00:00
|
|
|
struct task_struct *task = (struct task_struct *)fip;
|
2008-03-18 23:20:30 +00:00
|
|
|
|
2015-04-16 14:29:41 +00:00
|
|
|
if (fd < 0)
|
|
|
|
return;
|
|
|
|
|
2008-03-18 23:20:30 +00:00
|
|
|
spin_lock(&vn_file_lock);
|
2015-02-17 15:12:56 +00:00
|
|
|
fp = file_find(fd, task);
|
2008-03-18 23:20:30 +00:00
|
|
|
if (fp) {
|
|
|
|
atomic_dec(&fp->f_ref);
|
|
|
|
if (atomic_read(&fp->f_ref) > 0) {
|
|
|
|
spin_unlock(&vn_file_lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
list_del(&fp->f_list);
|
2008-03-18 23:20:30 +00:00
|
|
|
releasef_locked(fp);
|
|
|
|
}
|
|
|
|
spin_unlock(&vn_file_lock);
|
|
|
|
} /* releasef() */
|
2015-02-17 15:12:56 +00:00
|
|
|
EXPORT_SYMBOL(areleasef);
|
|
|
|
|
2008-03-18 23:20:30 +00:00
|
|
|
|
2014-10-02 16:47:09 +00:00
|
|
|
static void
|
|
|
|
#ifdef HAVE_SET_FS_PWD_WITH_CONST
|
|
|
|
vn_set_fs_pwd(struct fs_struct *fs, const struct path *path)
|
|
|
|
#else
|
|
|
|
vn_set_fs_pwd(struct fs_struct *fs, struct path *path)
|
|
|
|
#endif /* HAVE_SET_FS_PWD_WITH_CONST */
|
2009-10-01 23:06:15 +00:00
|
|
|
{
|
2010-11-09 19:15:32 +00:00
|
|
|
struct path old_pwd;
|
|
|
|
|
2014-10-02 16:47:09 +00:00
|
|
|
#ifdef HAVE_FS_STRUCT_SPINLOCK
|
2010-11-09 19:15:32 +00:00
|
|
|
spin_lock(&fs->lock);
|
|
|
|
old_pwd = fs->pwd;
|
|
|
|
fs->pwd = *path;
|
|
|
|
path_get(path);
|
|
|
|
spin_unlock(&fs->lock);
|
2014-10-02 16:47:09 +00:00
|
|
|
#else
|
2010-11-09 19:15:32 +00:00
|
|
|
write_lock(&fs->lock);
|
|
|
|
old_pwd = fs->pwd;
|
|
|
|
fs->pwd = *path;
|
|
|
|
path_get(path);
|
|
|
|
write_unlock(&fs->lock);
|
2014-10-02 16:47:09 +00:00
|
|
|
#endif /* HAVE_FS_STRUCT_SPINLOCK */
|
2010-11-09 19:15:32 +00:00
|
|
|
|
|
|
|
if (old_pwd.dentry)
|
|
|
|
path_put(&old_pwd);
|
2009-10-01 23:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vn_set_pwd(const char *filename)
|
|
|
|
{
|
2018-02-07 19:49:38 +00:00
|
|
|
struct path path;
|
|
|
|
mm_segment_t saved_fs;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* user_path_dir() and __user_walk() both expect 'filename' to be
|
|
|
|
* a user space address so we must briefly increase the data segment
|
|
|
|
* size to ensure strncpy_from_user() does not fail with -EFAULT.
|
|
|
|
*/
|
|
|
|
saved_fs = get_fs();
|
|
|
|
set_fs(get_ds());
|
|
|
|
|
|
|
|
rc = user_path_dir(filename, &path);
|
|
|
|
if (rc)
|
2014-11-05 22:30:35 +00:00
|
|
|
goto out;
|
2009-10-01 23:06:15 +00:00
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
rc = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_ACCESS);
|
|
|
|
if (rc)
|
2014-11-05 22:30:35 +00:00
|
|
|
goto dput_and_out;
|
2009-10-01 23:06:15 +00:00
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
vn_set_fs_pwd(current->fs, &path);
|
2009-10-01 23:06:15 +00:00
|
|
|
|
|
|
|
dput_and_out:
|
2018-02-07 19:49:38 +00:00
|
|
|
path_put(&path);
|
2009-10-01 23:06:15 +00:00
|
|
|
out:
|
2010-04-22 19:48:40 +00:00
|
|
|
set_fs(saved_fs);
|
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
return (-rc);
|
2009-10-01 23:06:15 +00:00
|
|
|
} /* vn_set_pwd() */
|
|
|
|
EXPORT_SYMBOL(vn_set_pwd);
|
|
|
|
|
2008-03-14 00:04:01 +00:00
|
|
|
static int
|
|
|
|
vn_cache_constructor(void *buf, void *cdrarg, int kmflags)
|
|
|
|
{
|
|
|
|
struct vnode *vp = buf;
|
|
|
|
|
|
|
|
mutex_init(&vp->v_lock, NULL, MUTEX_DEFAULT, NULL);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
} /* vn_cache_constructor() */
|
|
|
|
|
|
|
|
static void
|
|
|
|
vn_cache_destructor(void *buf, void *cdrarg)
|
|
|
|
{
|
|
|
|
struct vnode *vp = buf;
|
|
|
|
|
|
|
|
mutex_destroy(&vp->v_lock);
|
|
|
|
} /* vn_cache_destructor() */
|
|
|
|
|
2008-03-18 23:20:30 +00:00
|
|
|
static int
|
|
|
|
vn_file_cache_constructor(void *buf, void *cdrarg, int kmflags)
|
|
|
|
{
|
|
|
|
file_t *fp = buf;
|
|
|
|
|
|
|
|
atomic_set(&fp->f_ref, 0);
|
2018-02-07 19:49:38 +00:00
|
|
|
mutex_init(&fp->f_lock, NULL, MUTEX_DEFAULT, NULL);
|
2008-03-20 23:30:15 +00:00
|
|
|
INIT_LIST_HEAD(&fp->f_list);
|
2008-03-18 23:20:30 +00:00
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
return (0);
|
2018-02-19 11:40:38 +00:00
|
|
|
} /* vn_file_cache_constructor() */
|
2008-03-18 23:20:30 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
vn_file_cache_destructor(void *buf, void *cdrarg)
|
|
|
|
{
|
|
|
|
file_t *fp = buf;
|
|
|
|
|
|
|
|
mutex_destroy(&fp->f_lock);
|
|
|
|
} /* vn_file_cache_destructor() */
|
|
|
|
|
2008-03-14 00:04:01 +00:00
|
|
|
int
|
2011-11-09 20:29:51 +00:00
|
|
|
spl_vn_init(void)
|
2008-03-14 00:04:01 +00:00
|
|
|
{
|
2008-06-02 17:28:49 +00:00
|
|
|
vn_cache = kmem_cache_create("spl_vn_cache",
|
2018-02-07 19:49:38 +00:00
|
|
|
sizeof (struct vnode), 64, vn_cache_constructor,
|
|
|
|
vn_cache_destructor, NULL, NULL, NULL, 0);
|
2008-03-18 23:20:30 +00:00
|
|
|
|
|
|
|
vn_file_cache = kmem_cache_create("spl_vn_file_cache",
|
2018-02-07 19:49:38 +00:00
|
|
|
sizeof (file_t), 64, vn_file_cache_constructor,
|
|
|
|
vn_file_cache_destructor, NULL, NULL, NULL, 0);
|
|
|
|
|
2014-11-05 22:30:35 +00:00
|
|
|
return (0);
|
2018-02-19 11:40:38 +00:00
|
|
|
} /* spl_vn_init() */
|
2008-03-14 00:04:01 +00:00
|
|
|
|
|
|
|
void
|
2011-11-09 20:29:51 +00:00
|
|
|
spl_vn_fini(void)
|
2008-03-14 00:04:01 +00:00
|
|
|
{
|
2018-02-07 19:49:38 +00:00
|
|
|
file_t *fp, *next_fp;
|
2008-06-13 23:41:06 +00:00
|
|
|
int leaked = 0;
|
2008-03-18 23:20:30 +00:00
|
|
|
|
|
|
|
spin_lock(&vn_file_lock);
|
|
|
|
|
2018-02-07 19:49:38 +00:00
|
|
|
list_for_each_entry_safe(fp, next_fp, &vn_file_list, f_list) {
|
|
|
|
list_del(&fp->f_list);
|
2008-03-18 23:20:30 +00:00
|
|
|
releasef_locked(fp);
|
|
|
|
leaked++;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock(&vn_file_lock);
|
|
|
|
|
|
|
|
if (leaked > 0)
|
2014-11-05 22:30:35 +00:00
|
|
|
printk(KERN_WARNING "WARNING: %d vnode files leaked\n", leaked);
|
2008-03-18 23:20:30 +00:00
|
|
|
|
2012-06-11 16:12:37 +00:00
|
|
|
kmem_cache_destroy(vn_file_cache);
|
2008-06-13 23:41:06 +00:00
|
|
|
kmem_cache_destroy(vn_cache);
|
2018-02-19 11:40:38 +00:00
|
|
|
} /* spl_vn_fini() */
|