2008-05-26 04:38:26 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the SPL: Solaris Porting Layer.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 Lawrence Livermore National Security, LLC.
|
|
|
|
* Produced at Lawrence Livermore National Laboratory
|
|
|
|
* Written by:
|
|
|
|
* Brian Behlendorf <behlendorf1@llnl.gov>,
|
|
|
|
* Herb Wartens <wartens2@llnl.gov>,
|
|
|
|
* Jim Garlick <garlick@llnl.gov>
|
|
|
|
* UCRL-CODE-235197
|
|
|
|
*
|
|
|
|
* This 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.
|
|
|
|
*
|
|
|
|
* This is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* 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
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2008-03-07 23:07:02 +00:00
|
|
|
#include <sys/sysmacros.h>
|
2008-03-12 20:52:46 +00:00
|
|
|
#include <sys/vnode.h>
|
2008-03-07 23:07:02 +00:00
|
|
|
|
2008-04-21 17:29:47 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_SUBSYSTEM
|
|
|
|
#undef DEBUG_SUBSYSTEM
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define DEBUG_SUBSYSTEM S_VNODE
|
|
|
|
|
2008-03-12 20:52:46 +00:00
|
|
|
void *rootdir = NULL;
|
|
|
|
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
|
|
|
|
|
|
|
static spinlock_t vn_file_lock = SPIN_LOCK_UNLOCKED;
|
|
|
|
static LIST_HEAD(vn_file_list);
|
2008-03-14 00:04:01 +00:00
|
|
|
|
2008-03-12 20:52:46 +00:00
|
|
|
static vtype_t
|
|
|
|
vn_get_sol_type(umode_t mode)
|
|
|
|
{
|
|
|
|
if (S_ISREG(mode))
|
|
|
|
return VREG;
|
|
|
|
|
|
|
|
if (S_ISDIR(mode))
|
|
|
|
return VDIR;
|
|
|
|
|
|
|
|
if (S_ISCHR(mode))
|
|
|
|
return VCHR;
|
|
|
|
|
|
|
|
if (S_ISBLK(mode))
|
|
|
|
return VBLK;
|
|
|
|
|
|
|
|
if (S_ISFIFO(mode))
|
|
|
|
return VFIFO;
|
|
|
|
|
|
|
|
if (S_ISLNK(mode))
|
|
|
|
return VLNK;
|
|
|
|
|
|
|
|
if (S_ISSOCK(mode))
|
|
|
|
return VSOCK;
|
|
|
|
|
|
|
|
if (S_ISCHR(mode))
|
|
|
|
return VCHR;
|
|
|
|
|
|
|
|
return VNON;
|
|
|
|
} /* vn_get_sol_type() */
|
|
|
|
|
2008-03-14 00:04:01 +00:00
|
|
|
vnode_t *
|
|
|
|
vn_alloc(int flag)
|
|
|
|
{
|
|
|
|
vnode_t *vp;
|
2008-04-21 17:29:47 +00:00
|
|
|
ENTRY;
|
2008-03-14 00:04:01 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-04-21 17:29:47 +00:00
|
|
|
RETURN(vp);
|
2008-03-14 00:04:01 +00:00
|
|
|
} /* vn_alloc() */
|
|
|
|
EXPORT_SYMBOL(vn_alloc);
|
|
|
|
|
|
|
|
void
|
|
|
|
vn_free(vnode_t *vp)
|
|
|
|
{
|
2008-04-21 17:29:47 +00:00
|
|
|
ENTRY;
|
2008-03-14 00:04:01 +00:00
|
|
|
kmem_cache_free(vn_cache, vp);
|
2008-04-21 17:29:47 +00:00
|
|
|
EXIT;
|
2008-03-14 00:04:01 +00:00
|
|
|
} /* 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
|
|
|
{
|
2008-03-12 20:52:46 +00:00
|
|
|
struct file *fp;
|
|
|
|
struct kstat stat;
|
2008-06-02 17:28:49 +00:00
|
|
|
int rc, saved_umask = 0;
|
2008-03-07 23:07:02 +00:00
|
|
|
vnode_t *vp;
|
2008-04-21 17:29:47 +00:00
|
|
|
ENTRY;
|
2008-03-07 23:07:02 +00:00
|
|
|
|
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;
|
|
|
|
|
2008-04-04 17:08:12 +00:00
|
|
|
/* Note for filp_open() the two low bits must be remapped to mean:
|
|
|
|
* 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
|
|
|
|
2008-03-12 20:52:46 +00:00
|
|
|
fp = filp_open(path, flags, mode);
|
2008-03-07 23:07:02 +00:00
|
|
|
|
|
|
|
if (flags & FCREAT)
|
2008-03-12 20:52:46 +00:00
|
|
|
(void)xchg(¤t->fs->umask, saved_umask);
|
2008-03-07 23:07:02 +00:00
|
|
|
|
2008-03-12 20:52:46 +00:00
|
|
|
if (IS_ERR(fp))
|
2008-04-21 17:29:47 +00:00
|
|
|
RETURN(-PTR_ERR(fp));
|
2008-03-07 23:07:02 +00:00
|
|
|
|
2008-03-12 20:52:46 +00:00
|
|
|
rc = vfs_getattr(fp->f_vfsmnt, fp->f_dentry, &stat);
|
|
|
|
if (rc) {
|
|
|
|
filp_close(fp, 0);
|
2008-04-21 17:29:47 +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);
|
2008-04-21 17:29:47 +00:00
|
|
|
RETURN(ENOMEM);
|
2008-03-12 20:52:46 +00:00
|
|
|
}
|
2008-03-07 23:07:02 +00:00
|
|
|
|
2008-03-18 23:20:30 +00:00
|
|
|
mutex_enter(&vp->v_lock);
|
2008-03-12 20:52:46 +00:00
|
|
|
vp->v_type = vn_get_sol_type(stat.mode);
|
2008-03-18 23:20:30 +00:00
|
|
|
vp->v_file = fp;
|
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
|
|
|
|
2008-04-21 17:29:47 +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,
|
2008-03-12 20:52:46 +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-04-21 17:29:47 +00:00
|
|
|
ENTRY;
|
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;
|
|
|
|
realpath = kmalloc(len, GFP_KERNEL);
|
2008-03-12 20:52:46 +00:00
|
|
|
if (!realpath)
|
2008-04-21 17:29:47 +00:00
|
|
|
RETURN(ENOMEM);
|
2008-03-07 23:07:02 +00:00
|
|
|
|
2008-11-04 23:30:15 +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);
|
|
|
|
|
2008-04-21 17:29:47 +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,
|
2008-03-14 00:04:01 +00:00
|
|
|
uio_seg_t seg, int x1, rlim64_t x2, void *x3, ssize_t *residp)
|
2008-03-07 23:07:02 +00:00
|
|
|
{
|
2008-03-12 20:52:46 +00:00
|
|
|
loff_t offset;
|
|
|
|
mm_segment_t saved_fs;
|
|
|
|
struct file *fp;
|
|
|
|
int rc;
|
2008-04-21 17:29:47 +00:00
|
|
|
ENTRY;
|
2008-03-12 20:52:46 +00:00
|
|
|
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(uio == UIO_WRITE || uio == UIO_READ);
|
|
|
|
ASSERT(vp);
|
|
|
|
ASSERT(vp->v_file);
|
|
|
|
ASSERT(seg == UIO_SYSSPACE);
|
|
|
|
ASSERT(x1 == 0);
|
|
|
|
ASSERT(x2 == RLIM64_INFINITY);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
|
|
|
offset = off;
|
2008-03-18 23:20:30 +00:00
|
|
|
fp = vp->v_file;
|
2008-03-12 20:52:46 +00:00
|
|
|
|
|
|
|
/* Writable user data segment must be briefly increased for this
|
|
|
|
* process so we can use the user space read call paths to write
|
|
|
|
* in to memory allocated by the kernel. */
|
|
|
|
saved_fs = get_fs();
|
|
|
|
set_fs(get_ds());
|
|
|
|
|
|
|
|
if (uio & UIO_WRITE)
|
|
|
|
rc = vfs_write(fp, addr, len, &offset);
|
|
|
|
else
|
|
|
|
rc = vfs_read(fp, addr, len, &offset);
|
|
|
|
|
|
|
|
set_fs(saved_fs);
|
|
|
|
|
|
|
|
if (rc < 0)
|
2008-04-21 17:29:47 +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)
|
2008-04-21 17:29:47 +00:00
|
|
|
RETURN(EIO);
|
2008-03-07 23:07:02 +00:00
|
|
|
}
|
|
|
|
|
2008-04-21 17:29:47 +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
|
|
|
ENTRY;
|
2008-03-12 20:52:46 +00:00
|
|
|
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(vp);
|
|
|
|
ASSERT(vp->v_file);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
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
|
|
|
|
2008-04-21 17:29:47 +00:00
|
|
|
RETURN(-rc);
|
2008-03-12 20:52:46 +00:00
|
|
|
} /* vn_close() */
|
|
|
|
EXPORT_SYMBOL(vn_close);
|
|
|
|
|
2009-01-09 21:59:39 +00:00
|
|
|
/* vn_seek() does not actually seek it only performs bounds checking on the
|
|
|
|
* proposed seek. We perform minimal checking and allow vn_rdwr() to catch
|
|
|
|
* anything more serious. */
|
|
|
|
int
|
|
|
|
vn_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
|
|
|
|
{
|
|
|
|
return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vn_seek);
|
|
|
|
|
|
|
|
static struct dentry *
|
|
|
|
vn_lookup_hash(struct nameidata *nd)
|
2008-03-12 20:52:46 +00:00
|
|
|
{
|
2008-06-02 17:28:49 +00:00
|
|
|
return lookup_one_len(nd->last.name, nd->nd_dentry, nd->last.len);
|
2008-03-12 20:52:46 +00:00
|
|
|
} /* lookup_hash() */
|
|
|
|
|
2009-01-09 21:59:39 +00:00
|
|
|
static void
|
|
|
|
vn_path_release(struct nameidata *nd)
|
2008-06-02 17:28:49 +00:00
|
|
|
{
|
|
|
|
dput(nd->nd_dentry);
|
|
|
|
mntput(nd->nd_mnt);
|
|
|
|
}
|
|
|
|
|
2008-03-12 20:52:46 +00:00
|
|
|
/* Modified do_unlinkat() from linux/fs/namei.c, only uses exported symbols */
|
|
|
|
int
|
2008-03-14 00:04:01 +00:00
|
|
|
vn_remove(const char *path, uio_seg_t seg, int flags)
|
2008-03-12 20:52:46 +00:00
|
|
|
{
|
|
|
|
struct dentry *dentry;
|
|
|
|
struct nameidata nd;
|
|
|
|
struct inode *inode = NULL;
|
|
|
|
int rc = 0;
|
2008-08-11 22:13:47 +00:00
|
|
|
ENTRY;
|
2008-03-12 20:52:46 +00:00
|
|
|
|
2008-08-11 22:13:47 +00:00
|
|
|
ASSERT(seg == UIO_SYSSPACE);
|
|
|
|
ASSERT(flags == RMFILE);
|
2008-03-12 21:33:28 +00:00
|
|
|
|
2008-03-12 20:52:46 +00:00
|
|
|
rc = path_lookup(path, LOOKUP_PARENT, &nd);
|
|
|
|
if (rc)
|
2008-04-21 17:29:47 +00:00
|
|
|
GOTO(exit, rc);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
|
|
|
rc = -EISDIR;
|
|
|
|
if (nd.last_type != LAST_NORM)
|
2008-04-21 17:29:47 +00:00
|
|
|
GOTO(exit1, rc);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
2008-08-11 22:13:47 +00:00
|
|
|
#ifdef HAVE_INODE_I_MUTEX
|
2008-06-02 17:28:49 +00:00
|
|
|
mutex_lock_nested(&nd.nd_dentry->d_inode->i_mutex, I_MUTEX_PARENT);
|
2008-08-11 22:13:47 +00:00
|
|
|
#else
|
|
|
|
down(&nd.nd_dentry->d_inode->i_sem);
|
2009-05-20 18:31:55 +00:00
|
|
|
#endif /* HAVE_INODE_I_MUTEX */
|
2008-06-02 17:28:49 +00:00
|
|
|
dentry = vn_lookup_hash(&nd);
|
2008-03-12 20:52:46 +00:00
|
|
|
rc = PTR_ERR(dentry);
|
|
|
|
if (!IS_ERR(dentry)) {
|
|
|
|
/* Why not before? Because we want correct rc value */
|
|
|
|
if (nd.last.name[nd.last.len])
|
2008-04-21 17:29:47 +00:00
|
|
|
GOTO(slashes, rc);
|
|
|
|
|
2008-03-12 20:52:46 +00:00
|
|
|
inode = dentry->d_inode;
|
|
|
|
if (inode)
|
|
|
|
atomic_inc(&inode->i_count);
|
2009-05-20 18:31:55 +00:00
|
|
|
#ifdef HAVE_2ARGS_VFS_UNLINK
|
2008-06-02 17:28:49 +00:00
|
|
|
rc = vfs_unlink(nd.nd_dentry->d_inode, dentry);
|
2009-05-20 18:31:55 +00:00
|
|
|
#else
|
|
|
|
rc = vfs_unlink(nd.nd_dentry->d_inode, dentry, nd.mnt);
|
|
|
|
#endif /* HAVE_2ARGS_VFS_UNLINK */
|
2008-03-12 20:52:46 +00:00
|
|
|
exit2:
|
|
|
|
dput(dentry);
|
|
|
|
}
|
2008-08-11 22:13:47 +00:00
|
|
|
#ifdef HAVE_INODE_I_MUTEX
|
2008-06-02 17:28:49 +00:00
|
|
|
mutex_unlock(&nd.nd_dentry->d_inode->i_mutex);
|
2008-08-11 22:13:47 +00:00
|
|
|
#else
|
|
|
|
up(&nd.nd_dentry->d_inode->i_sem);
|
2009-05-20 18:31:55 +00:00
|
|
|
#endif /* HAVE_INODE_I_MUTEX */
|
2008-03-12 20:52:46 +00:00
|
|
|
if (inode)
|
|
|
|
iput(inode); /* truncate the inode here */
|
|
|
|
exit1:
|
2008-06-02 17:28:49 +00:00
|
|
|
vn_path_release(&nd);
|
2008-03-12 20:52:46 +00:00
|
|
|
exit:
|
2008-04-21 17:29:47 +00:00
|
|
|
RETURN(-rc);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
|
|
|
slashes:
|
|
|
|
rc = !dentry->d_inode ? -ENOENT :
|
|
|
|
S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
|
2008-04-21 17:29:47 +00:00
|
|
|
GOTO(exit2, rc);
|
2008-03-12 20:52:46 +00:00
|
|
|
} /* vn_remove() */
|
|
|
|
EXPORT_SYMBOL(vn_remove);
|
|
|
|
|
|
|
|
/* Modified do_rename() from linux/fs/namei.c, only uses exported symbols */
|
|
|
|
int
|
|
|
|
vn_rename(const char *oldname, const char *newname, int x1)
|
|
|
|
{
|
2009-05-20 18:31:55 +00:00
|
|
|
struct dentry *old_dir, *new_dir;
|
|
|
|
struct dentry *old_dentry, *new_dentry;
|
|
|
|
struct dentry *trap;
|
2008-03-12 20:52:46 +00:00
|
|
|
struct nameidata oldnd, newnd;
|
|
|
|
int rc = 0;
|
2008-04-21 17:29:47 +00:00
|
|
|
ENTRY;
|
2008-03-12 20:52:46 +00:00
|
|
|
|
|
|
|
rc = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
|
|
|
|
if (rc)
|
2008-04-21 17:29:47 +00:00
|
|
|
GOTO(exit, rc);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
|
|
|
rc = path_lookup(newname, LOOKUP_PARENT, &newnd);
|
|
|
|
if (rc)
|
2008-04-21 17:29:47 +00:00
|
|
|
GOTO(exit1, rc);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
|
|
|
rc = -EXDEV;
|
2008-06-02 17:28:49 +00:00
|
|
|
if (oldnd.nd_mnt != newnd.nd_mnt)
|
2008-04-21 17:29:47 +00:00
|
|
|
GOTO(exit2, rc);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
2008-06-02 17:28:49 +00:00
|
|
|
old_dir = oldnd.nd_dentry;
|
2008-03-12 20:52:46 +00:00
|
|
|
rc = -EBUSY;
|
|
|
|
if (oldnd.last_type != LAST_NORM)
|
2008-04-21 17:29:47 +00:00
|
|
|
GOTO(exit2, rc);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
2008-06-02 17:28:49 +00:00
|
|
|
new_dir = newnd.nd_dentry;
|
2008-03-12 20:52:46 +00:00
|
|
|
if (newnd.last_type != LAST_NORM)
|
2008-04-21 17:29:47 +00:00
|
|
|
GOTO(exit2, rc);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
|
|
|
trap = lock_rename(new_dir, old_dir);
|
|
|
|
|
2008-06-02 17:28:49 +00:00
|
|
|
old_dentry = vn_lookup_hash(&oldnd);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
|
|
|
rc = PTR_ERR(old_dentry);
|
|
|
|
if (IS_ERR(old_dentry))
|
2008-04-21 17:29:47 +00:00
|
|
|
GOTO(exit3, rc);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
|
|
|
/* source must exist */
|
|
|
|
rc = -ENOENT;
|
|
|
|
if (!old_dentry->d_inode)
|
2008-04-21 17:29:47 +00:00
|
|
|
GOTO(exit4, rc);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
|
|
|
/* unless the source is a directory trailing slashes give -ENOTDIR */
|
|
|
|
if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
|
|
|
|
rc = -ENOTDIR;
|
|
|
|
if (oldnd.last.name[oldnd.last.len])
|
2008-04-21 17:29:47 +00:00
|
|
|
GOTO(exit4, rc);
|
2008-03-12 20:52:46 +00:00
|
|
|
if (newnd.last.name[newnd.last.len])
|
2008-04-21 17:29:47 +00:00
|
|
|
GOTO(exit4, rc);
|
2008-03-12 20:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* source should not be ancestor of target */
|
|
|
|
rc = -EINVAL;
|
|
|
|
if (old_dentry == trap)
|
2008-04-21 17:29:47 +00:00
|
|
|
GOTO(exit4, rc);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
2008-06-02 17:28:49 +00:00
|
|
|
new_dentry = vn_lookup_hash(&newnd);
|
2008-03-12 20:52:46 +00:00
|
|
|
rc = PTR_ERR(new_dentry);
|
|
|
|
if (IS_ERR(new_dentry))
|
2008-04-21 17:29:47 +00:00
|
|
|
GOTO(exit4, rc);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
|
|
|
/* target should not be an ancestor of source */
|
|
|
|
rc = -ENOTEMPTY;
|
|
|
|
if (new_dentry == trap)
|
2008-04-21 17:29:47 +00:00
|
|
|
GOTO(exit5, rc);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
2009-05-20 18:31:55 +00:00
|
|
|
#ifdef HAVE_4ARGS_VFS_RENAME
|
2008-03-12 20:52:46 +00:00
|
|
|
rc = vfs_rename(old_dir->d_inode, old_dentry,
|
|
|
|
new_dir->d_inode, new_dentry);
|
2009-05-20 18:31:55 +00:00
|
|
|
#else
|
|
|
|
rc = vfs_rename(old_dir->d_inode, old_dentry, oldnd.mnt,
|
|
|
|
new_dir->d_inode, new_dentry, newnd.mnt);
|
|
|
|
#endif /* HAVE_4ARGS_VFS_RENAME */
|
2008-03-12 20:52:46 +00:00
|
|
|
exit5:
|
|
|
|
dput(new_dentry);
|
|
|
|
exit4:
|
|
|
|
dput(old_dentry);
|
|
|
|
exit3:
|
|
|
|
unlock_rename(new_dir, old_dir);
|
|
|
|
exit2:
|
2008-06-02 17:28:49 +00:00
|
|
|
vn_path_release(&newnd);
|
2008-03-12 20:52:46 +00:00
|
|
|
exit1:
|
2008-06-02 17:28:49 +00:00
|
|
|
vn_path_release(&oldnd);
|
2008-03-12 20:52:46 +00:00
|
|
|
exit:
|
2008-04-21 17:29:47 +00:00
|
|
|
RETURN(-rc);
|
2008-03-07 23:07:02 +00:00
|
|
|
}
|
2008-03-12 20:52:46 +00:00
|
|
|
EXPORT_SYMBOL(vn_rename);
|
2008-03-07 23:07:02 +00:00
|
|
|
|
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;
|
|
|
|
struct kstat stat;
|
|
|
|
int rc;
|
2008-04-21 17:29:47 +00:00
|
|
|
ENTRY;
|
2008-03-12 20:52:46 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
rc = vfs_getattr(fp->f_vfsmnt, fp->f_dentry, &stat);
|
|
|
|
if (rc)
|
2008-04-21 17:29:47 +00:00
|
|
|
RETURN(-rc);
|
2008-03-12 20:52:46 +00:00
|
|
|
|
|
|
|
vap->va_type = vn_get_sol_type(stat.mode);
|
|
|
|
vap->va_mode = stat.mode;
|
|
|
|
vap->va_uid = stat.uid;
|
|
|
|
vap->va_gid = stat.gid;
|
|
|
|
vap->va_fsid = 0;
|
|
|
|
vap->va_nodeid = stat.ino;
|
|
|
|
vap->va_nlink = stat.nlink;
|
|
|
|
vap->va_size = stat.size;
|
|
|
|
vap->va_blocksize = stat.blksize;
|
|
|
|
vap->va_atime.tv_sec = stat.atime.tv_sec;
|
|
|
|
vap->va_atime.tv_usec = stat.atime.tv_nsec / NSEC_PER_USEC;
|
|
|
|
vap->va_mtime.tv_sec = stat.mtime.tv_sec;
|
|
|
|
vap->va_mtime.tv_usec = stat.mtime.tv_nsec / NSEC_PER_USEC;
|
|
|
|
vap->va_ctime.tv_sec = stat.ctime.tv_sec;
|
|
|
|
vap->va_ctime.tv_usec = stat.ctime.tv_nsec / NSEC_PER_USEC;
|
|
|
|
vap->va_rdev = stat.rdev;
|
|
|
|
vap->va_blocks = stat.blocks;
|
|
|
|
|
2008-04-21 17:29:47 +00:00
|
|
|
RETURN(0);
|
2008-03-07 23:07:02 +00:00
|
|
|
}
|
2008-03-12 20:52:46 +00:00
|
|
|
EXPORT_SYMBOL(vn_getattr);
|
|
|
|
|
2008-03-12 21:33:28 +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;
|
2008-04-21 17:29:47 +00:00
|
|
|
ENTRY;
|
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;
|
|
|
|
|
2008-04-21 17:29:47 +00:00
|
|
|
RETURN(-file_fsync(vp->v_file, vp->v_file->f_dentry, datasync));
|
2008-03-12 20:52:46 +00:00
|
|
|
} /* vn_fsync() */
|
|
|
|
EXPORT_SYMBOL(vn_fsync);
|
2008-03-14 00:04:01 +00:00
|
|
|
|
2008-03-18 23:20:30 +00:00
|
|
|
/* Function must be called while holding the vn_file_lock */
|
|
|
|
static file_t *
|
|
|
|
file_find(int fd)
|
|
|
|
{
|
|
|
|
file_t *fp;
|
|
|
|
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(spin_is_locked(&vn_file_lock));
|
2008-03-18 23:20:30 +00:00
|
|
|
|
|
|
|
list_for_each_entry(fp, &vn_file_list, f_list) {
|
|
|
|
if (fd == fp->f_fd) {
|
2008-04-21 17:29:47 +00:00
|
|
|
ASSERT(atomic_read(&fp->f_ref) != 0);
|
2008-03-18 23:20:30 +00:00
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
} /* file_find() */
|
|
|
|
|
|
|
|
file_t *
|
|
|
|
vn_getf(int fd)
|
|
|
|
{
|
|
|
|
struct kstat stat;
|
|
|
|
struct file *lfp;
|
|
|
|
file_t *fp;
|
|
|
|
vnode_t *vp;
|
2008-04-21 17:29:47 +00:00
|
|
|
int rc = 0;
|
|
|
|
ENTRY;
|
2008-03-18 23:20:30 +00:00
|
|
|
|
|
|
|
/* Already open just take an extra reference */
|
|
|
|
spin_lock(&vn_file_lock);
|
|
|
|
|
|
|
|
fp = file_find(fd);
|
|
|
|
if (fp) {
|
|
|
|
atomic_inc(&fp->f_ref);
|
|
|
|
spin_unlock(&vn_file_lock);
|
2008-04-21 17:29:47 +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)
|
2008-04-21 17:29:47 +00:00
|
|
|
GOTO(out, rc);
|
2008-03-18 23:20:30 +00:00
|
|
|
|
|
|
|
mutex_enter(&fp->f_lock);
|
|
|
|
|
|
|
|
fp->f_fd = fd;
|
|
|
|
fp->f_offset = 0;
|
|
|
|
atomic_inc(&fp->f_ref);
|
|
|
|
|
|
|
|
lfp = fget(fd);
|
|
|
|
if (lfp == NULL)
|
2008-04-21 17:29:47 +00:00
|
|
|
GOTO(out_mutex, rc);
|
2008-03-18 23:20:30 +00:00
|
|
|
|
|
|
|
vp = vn_alloc(KM_SLEEP);
|
|
|
|
if (vp == NULL)
|
2008-04-21 17:29:47 +00:00
|
|
|
GOTO(out_fget, rc);
|
2008-03-18 23:20:30 +00:00
|
|
|
|
|
|
|
if (vfs_getattr(lfp->f_vfsmnt, lfp->f_dentry, &stat))
|
2008-04-21 17:29:47 +00:00
|
|
|
GOTO(out_vnode, rc);
|
2008-03-18 23:20:30 +00:00
|
|
|
|
|
|
|
mutex_enter(&vp->v_lock);
|
|
|
|
vp->v_type = vn_get_sol_type(stat.mode);
|
|
|
|
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);
|
2008-04-21 17:29:47 +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:
|
2008-04-21 17:29:47 +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)
|
|
|
|
{
|
|
|
|
file_t *fp;
|
2008-04-21 17:29:47 +00:00
|
|
|
ENTRY;
|
2008-03-18 23:20:30 +00:00
|
|
|
|
|
|
|
spin_lock(&vn_file_lock);
|
|
|
|
fp = file_find(fd);
|
|
|
|
if (fp) {
|
|
|
|
atomic_dec(&fp->f_ref);
|
|
|
|
if (atomic_read(&fp->f_ref) > 0) {
|
|
|
|
spin_unlock(&vn_file_lock);
|
2008-04-21 17:29:47 +00:00
|
|
|
EXIT;
|
2008-03-18 23:20:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_del(&fp->f_list);
|
|
|
|
releasef_locked(fp);
|
|
|
|
}
|
|
|
|
spin_unlock(&vn_file_lock);
|
|
|
|
|
2008-04-21 17:29:47 +00:00
|
|
|
EXIT;
|
2008-03-18 23:20:30 +00:00
|
|
|
return;
|
|
|
|
} /* releasef() */
|
|
|
|
EXPORT_SYMBOL(releasef);
|
|
|
|
|
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);
|
|
|
|
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
|
|
|
|
|
|
|
return (0);
|
|
|
|
} /* file_cache_constructor() */
|
|
|
|
|
|
|
|
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
|
|
|
|
vn_init(void)
|
|
|
|
{
|
2008-04-21 17:29:47 +00:00
|
|
|
ENTRY;
|
2008-06-02 17:28:49 +00:00
|
|
|
vn_cache = kmem_cache_create("spl_vn_cache",
|
|
|
|
sizeof(struct vnode), 64,
|
2008-03-18 04:56:43 +00:00
|
|
|
vn_cache_constructor,
|
|
|
|
vn_cache_destructor,
|
2008-03-14 00:04:01 +00:00
|
|
|
NULL, NULL, NULL, 0);
|
2008-03-18 23:20:30 +00:00
|
|
|
|
|
|
|
vn_file_cache = kmem_cache_create("spl_vn_file_cache",
|
|
|
|
sizeof(file_t), 64,
|
|
|
|
vn_file_cache_constructor,
|
|
|
|
vn_file_cache_destructor,
|
|
|
|
NULL, NULL, NULL, 0);
|
2008-04-21 17:29:47 +00:00
|
|
|
RETURN(0);
|
2008-03-14 00:04:01 +00:00
|
|
|
} /* vn_init() */
|
|
|
|
|
|
|
|
void
|
|
|
|
vn_fini(void)
|
|
|
|
{
|
2008-03-18 23:20:30 +00:00
|
|
|
file_t *fp, *next_fp;
|
2008-06-13 23:41:06 +00:00
|
|
|
int leaked = 0;
|
2008-04-21 17:29:47 +00:00
|
|
|
ENTRY;
|
2008-03-18 23:20:30 +00:00
|
|
|
|
|
|
|
spin_lock(&vn_file_lock);
|
|
|
|
|
|
|
|
list_for_each_entry_safe(fp, next_fp, &vn_file_list, f_list) {
|
|
|
|
list_del(&fp->f_list);
|
|
|
|
releasef_locked(fp);
|
|
|
|
leaked++;
|
|
|
|
}
|
|
|
|
|
2008-06-13 23:41:06 +00:00
|
|
|
kmem_cache_destroy(vn_file_cache);
|
2008-03-18 23:20:30 +00:00
|
|
|
vn_file_cache = NULL;
|
|
|
|
spin_unlock(&vn_file_lock);
|
|
|
|
|
|
|
|
if (leaked > 0)
|
2008-04-21 17:29:47 +00:00
|
|
|
CWARN("Warning %d files leaked\n", leaked);
|
2008-03-18 23:20:30 +00:00
|
|
|
|
2008-06-13 23:41:06 +00:00
|
|
|
kmem_cache_destroy(vn_cache);
|
2008-03-18 23:20:30 +00:00
|
|
|
|
2008-04-21 17:29:47 +00:00
|
|
|
EXIT;
|
2008-03-18 23:20:30 +00:00
|
|
|
return;
|
2008-03-14 00:04:01 +00:00
|
|
|
} /* vn_fini() */
|