mirror of
https://git.proxmox.com/git/mirror_zfs.git
synced 2026-05-22 02:27:36 +03:00
Remove vn_rename and vn_remove
Both vn_rename and vn_remove have been historically problematic to implement reliably. Rather than fixing them yet again they are being removed. Reviewed-by: Arkadiusz Bubala <arkadiusz.bubala@open-e.com> Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov> Closes #648 Closes #661
This commit is contained in:
@@ -282,223 +282,6 @@ vn_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, void *ct)
|
||||
}
|
||||
EXPORT_SYMBOL(vn_seek);
|
||||
|
||||
/*
|
||||
* spl_basename() takes a NULL-terminated string s as input containing a path.
|
||||
* It returns a char pointer to a string and a length that describe the
|
||||
* basename of the path. If the basename is not "." or "/", it will be an index
|
||||
* into the string. While the string should be NULL terminated, the section
|
||||
* referring to the basename is not. spl_basename is dual-licensed GPLv2+ and
|
||||
* CC0. Anyone wishing to reuse it in another codebase may pick either license.
|
||||
*/
|
||||
static void
|
||||
spl_basename(const char *s, const char **str, int *len)
|
||||
{
|
||||
size_t i, end;
|
||||
|
||||
ASSERT(str);
|
||||
ASSERT(len);
|
||||
|
||||
if (!s || !*s) {
|
||||
*str = ".";
|
||||
*len = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
i = strlen(s) - 1;
|
||||
|
||||
while (i && s[i--] == '/');
|
||||
|
||||
if (i == 0) {
|
||||
*str = "/";
|
||||
*len = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
end = i;
|
||||
|
||||
for (end = i; i; i--) {
|
||||
if (s[i] == '/') {
|
||||
*str = &s[i+1];
|
||||
*len = end - i + 1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
*str = s;
|
||||
*len = end + 1;
|
||||
}
|
||||
|
||||
static struct dentry *
|
||||
spl_kern_path_locked(const char *name, struct path *path)
|
||||
{
|
||||
struct path parent;
|
||||
struct dentry *dentry;
|
||||
const char *basename;
|
||||
int len;
|
||||
int rc;
|
||||
|
||||
ASSERT(name);
|
||||
ASSERT(path);
|
||||
|
||||
spl_basename(name, &basename, &len);
|
||||
|
||||
/* We do not accept "." or ".." */
|
||||
if (len <= 2 && basename[0] == '.')
|
||||
if (len == 1 || basename[1] == '.')
|
||||
return (ERR_PTR(-EACCES));
|
||||
|
||||
rc = kern_path(name, LOOKUP_PARENT, &parent);
|
||||
if (rc)
|
||||
return (ERR_PTR(rc));
|
||||
|
||||
/* use I_MUTEX_PARENT because vfs_unlink needs it */
|
||||
spl_inode_lock_nested(parent.dentry->d_inode, I_MUTEX_PARENT);
|
||||
|
||||
dentry = lookup_one_len(basename, parent.dentry, len);
|
||||
if (IS_ERR(dentry)) {
|
||||
spl_inode_unlock(parent.dentry->d_inode);
|
||||
path_put(&parent);
|
||||
} else {
|
||||
*path = parent;
|
||||
}
|
||||
|
||||
return (dentry);
|
||||
}
|
||||
|
||||
/* Based on do_unlinkat() from linux/fs/namei.c */
|
||||
int
|
||||
vn_remove(const char *path, uio_seg_t seg, int flags)
|
||||
{
|
||||
struct dentry *dentry;
|
||||
struct path parent;
|
||||
struct inode *inode = NULL;
|
||||
int rc = 0;
|
||||
|
||||
ASSERT(seg == UIO_SYSSPACE);
|
||||
ASSERT(flags == RMFILE);
|
||||
|
||||
dentry = spl_kern_path_locked(path, &parent);
|
||||
rc = PTR_ERR(dentry);
|
||||
if (!IS_ERR(dentry)) {
|
||||
if (parent.dentry->d_name.name[parent.dentry->d_name.len]) {
|
||||
rc = 0;
|
||||
goto slashes;
|
||||
}
|
||||
|
||||
inode = dentry->d_inode;
|
||||
if (inode) {
|
||||
atomic_inc(&inode->i_count);
|
||||
} else {
|
||||
rc = 0;
|
||||
goto slashes;
|
||||
}
|
||||
|
||||
#ifdef HAVE_2ARGS_VFS_UNLINK
|
||||
rc = vfs_unlink(parent.dentry->d_inode, dentry);
|
||||
#else
|
||||
rc = vfs_unlink(parent.dentry->d_inode, dentry, NULL);
|
||||
#endif /* HAVE_2ARGS_VFS_UNLINK */
|
||||
exit1:
|
||||
dput(dentry);
|
||||
} else {
|
||||
return (-rc);
|
||||
}
|
||||
|
||||
spl_inode_unlock(parent.dentry->d_inode);
|
||||
if (inode)
|
||||
iput(inode); /* truncate the inode here */
|
||||
|
||||
path_put(&parent);
|
||||
return (-rc);
|
||||
|
||||
slashes:
|
||||
rc = !dentry->d_inode ? -ENOENT :
|
||||
S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
|
||||
goto exit1;
|
||||
} /* vn_remove() */
|
||||
EXPORT_SYMBOL(vn_remove);
|
||||
|
||||
/* Based on do_rename() from linux/fs/namei.c */
|
||||
int
|
||||
vn_rename(const char *oldname, const char *newname, int x1)
|
||||
{
|
||||
struct dentry *old_dir, *new_dir;
|
||||
struct dentry *old_dentry, *new_dentry;
|
||||
struct dentry *trap;
|
||||
struct path old_parent, new_parent;
|
||||
int rc = 0;
|
||||
|
||||
old_dentry = spl_kern_path_locked(oldname, &old_parent);
|
||||
if (IS_ERR(old_dentry)) {
|
||||
rc = PTR_ERR(old_dentry);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
spl_inode_unlock(old_parent.dentry->d_inode);
|
||||
|
||||
new_dentry = spl_kern_path_locked(newname, &new_parent);
|
||||
if (IS_ERR(new_dentry)) {
|
||||
rc = PTR_ERR(new_dentry);
|
||||
goto exit2;
|
||||
}
|
||||
|
||||
spl_inode_unlock(new_parent.dentry->d_inode);
|
||||
|
||||
rc = -EXDEV;
|
||||
if (old_parent.mnt != new_parent.mnt)
|
||||
goto exit3;
|
||||
|
||||
old_dir = old_parent.dentry;
|
||||
new_dir = new_parent.dentry;
|
||||
trap = lock_rename(new_dir, old_dir);
|
||||
|
||||
/* source should not be ancestor of target */
|
||||
rc = -EINVAL;
|
||||
if (old_dentry == trap)
|
||||
goto exit4;
|
||||
|
||||
/* target should not be an ancestor of source */
|
||||
rc = -ENOTEMPTY;
|
||||
if (new_dentry == trap)
|
||||
goto exit4;
|
||||
|
||||
/* source must exist */
|
||||
rc = -ENOENT;
|
||||
if (!old_dentry->d_inode)
|
||||
goto exit4;
|
||||
|
||||
/* unless the source is a directory trailing slashes give -ENOTDIR */
|
||||
if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
|
||||
rc = -ENOTDIR;
|
||||
if (old_dentry->d_name.name[old_dentry->d_name.len])
|
||||
goto exit4;
|
||||
if (new_dentry->d_name.name[new_dentry->d_name.len])
|
||||
goto exit4;
|
||||
}
|
||||
|
||||
#if defined(HAVE_4ARGS_VFS_RENAME)
|
||||
rc = vfs_rename(old_dir->d_inode, old_dentry,
|
||||
new_dir->d_inode, new_dentry);
|
||||
#elif defined(HAVE_5ARGS_VFS_RENAME)
|
||||
rc = vfs_rename(old_dir->d_inode, old_dentry,
|
||||
new_dir->d_inode, new_dentry, NULL);
|
||||
#else
|
||||
rc = vfs_rename(old_dir->d_inode, old_dentry,
|
||||
new_dir->d_inode, new_dentry, NULL, 0);
|
||||
#endif
|
||||
exit4:
|
||||
unlock_rename(new_dir, old_dir);
|
||||
exit3:
|
||||
dput(new_dentry);
|
||||
path_put(&new_parent);
|
||||
exit2:
|
||||
dput(old_dentry);
|
||||
path_put(&old_parent);
|
||||
exit:
|
||||
return (-rc);
|
||||
}
|
||||
EXPORT_SYMBOL(vn_rename);
|
||||
|
||||
int
|
||||
vn_getattr(vnode_t *vp, vattr_t *vap, int flags, void *x3, void *x4)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user