Cleanup vn_rename() and vn_remove()

zfsonlinux/spl#bcb15891ab394e11615eee08bba1fd85ac32e158 implemented
Linux 3.6+ support by adding duplicate vn_rename and vn_remove
functions. The new ones were cleaner, but the duplicate functions made
the codebase less maintainable. This adds some compatibility shims that
allow us to retire the older vn_rename and vn_remove in favor of the new
ones on old kernels. The result is a net 143 line reduction in lines of
code and a cleaner codebase.

Signed-off-by: Richard Yao <ryao@gentoo.org>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #370
This commit is contained in:
Richard Yao 2014-06-29 01:00:02 -04:00 committed by Brian Behlendorf
parent 2fc44f66ec
commit ec18fe3ce8
5 changed files with 129 additions and 305 deletions

View File

@ -28,7 +28,6 @@ AC_DEFUN([SPL_AC_CONFIG_KERNEL], [
SPL_AC_TYPE_UINTPTR_T
SPL_AC_2ARGS_REGISTER_SYSCTL
SPL_AC_SHRINKER_CALLBACK
SPL_AC_PATH_IN_NAMEIDATA
SPL_AC_TASK_CURR
SPL_AC_CTL_UNNUMBERED
SPL_AC_CTL_NAME
@ -81,9 +80,7 @@ AC_DEFUN([SPL_AC_CONFIG_KERNEL], [
SPL_AC_KERNEL_FALLOCATE
SPL_AC_SHRINK_DCACHE_MEMORY
SPL_AC_SHRINK_ICACHE_MEMORY
SPL_AC_KERN_PATH_PARENT_HEADER
SPL_AC_KERN_PATH_PARENT_SYMBOL
SPL_AC_KERN_PATH_LOCKED
SPL_AC_KERN_PATH
SPL_AC_CONFIG_KALLSYMS
SPL_AC_CONFIG_ZLIB_INFLATE
SPL_AC_CONFIG_ZLIB_DEFLATE
@ -990,28 +987,6 @@ AC_DEFUN([SPL_AC_SHRINKER_CALLBACK],[
EXTRA_KCFLAGS="$tmp_flags"
])
dnl #
dnl # 2.6.25 API change,
dnl # struct path entry added to struct nameidata
dnl #
AC_DEFUN([SPL_AC_PATH_IN_NAMEIDATA],
[AC_MSG_CHECKING([whether struct path used in struct nameidata])
SPL_LINUX_TRY_COMPILE([
#include <linux/namei.h>
],[
struct nameidata nd __attribute__ ((unused));
nd.path.mnt = NULL;
nd.path.dentry = NULL;
],[
AC_MSG_RESULT(yes)
AC_DEFINE(HAVE_PATH_IN_NAMEIDATA, 1,
[struct path used in struct nameidata])
],[
AC_MSG_RESULT(no)
])
])
dnl #
dnl # Custom SPL patch may export this system it is not required
dnl #
@ -2279,56 +2254,41 @@ AC_DEFUN([SPL_AC_SHRINK_ICACHE_MEMORY],
])
dnl #
dnl # 2.6.39 API compat,
dnl # The path_lookup() function has been renamed to kern_path_parent()
dnl # and the flags argument has been removed. The only behavior now
dnl # offered is that of LOOKUP_PARENT. The spl already always passed
dnl # this flag so dropping the flag does not impact us.
dnl # 2.6.28 API change
dnl # The kern_path() function has been introduced. We adopt it as the new way
dnl # of looking up paths. When it is not available, we emulate it using the
dnl # older interfaces.
dnl #
AC_DEFUN([SPL_AC_KERN_PATH_PARENT_HEADER], [
SPL_CHECK_SYMBOL_HEADER(
[kern_path_parent],
[int kern_path_parent(const char \*, struct nameidata \*)],
[include/linux/namei.h],
[AC_DEFINE(HAVE_KERN_PATH_PARENT_HEADER, 1,
[kern_path_parent() is available])],
[])
])
dnl #
dnl # 3.1 API compat,
dnl # The kern_path_parent() symbol is no longer exported by the kernel.
dnl # However, it remains the prefered interface and since we still have
dnl # access to the prototype we dynamically lookup the required address.
dnl #
AC_DEFUN([SPL_AC_KERN_PATH_PARENT_SYMBOL],
[AC_MSG_CHECKING([whether kern_path_parent() is available])
AC_DEFUN([SPL_AC_KERN_PATH],
[AC_MSG_CHECKING([whether kern_path() is available])
SPL_LINUX_TRY_COMPILE_SYMBOL([
#include <linux/namei.h>
], [
kern_path_parent(NULL, NULL);
], [kern_path_parent], [fs/namei.c], [
int r = kern_path(NULL, 0, NULL);
], [kern_path], [fs/namei.c], [
AC_MSG_RESULT(yes)
AC_DEFINE(HAVE_KERN_PATH_PARENT_SYMBOL, 1,
[kern_path_parent() is available])
AC_DEFINE(HAVE_KERN_PATH, 1,
[kern_path() is available])
], [
AC_MSG_RESULT(no)
])
])
AC_MSG_CHECKING([whether path_lookup() is available])
SPL_LINUX_TRY_COMPILE_SYMBOL([
#include <linux/namei.h>
], [
int r = path_lookup(NULL, 0, NULL);
], [path_lookup], [fs/namei.c], [
AC_MSG_RESULT(yes)
AC_DEFINE(HAVE_KERN_PATH, 1,
[kern_path() is available])
], [
AC_MSG_RESULT(no)
AC_MSG_ERROR([
*** Neither kern_path() nor path_lookup() is available.
*** Please file an issue:
*** https://github.com/zfsonlinux/spl/issues/new])
dnl #
dnl # 3.6 API compat,
dnl # The kern_path_parent() function was replaced by the kern_path_locked()
dnl # function to eliminate all struct nameidata usage outside fs/namei.c.
dnl #
AC_DEFUN([SPL_AC_KERN_PATH_LOCKED], [
SPL_CHECK_SYMBOL_HEADER(
[kern_path_locked],
[struct dentry \*kern_path_locked(const char \*, struct path \*)],
[include/linux/namei.h],
[AC_DEFINE(HAVE_KERN_PATH_LOCKED, 1,
[kern_path_locked() is available])],
[])
])
])
])
dnl #

View File

@ -91,23 +91,5 @@ spl_filp_fallocate(struct file *fp, int mode, loff_t offset, loff_t len)
#define spl_inode_unlock(ip) (up(&(ip)->i_sem))
#endif /* HAVE_INODE_I_MUTEX */
#ifdef HAVE_KERN_PATH_PARENT_HEADER
# ifndef HAVE_KERN_PATH_PARENT_SYMBOL
typedef int (*kern_path_parent_t)(const char *, struct nameidata *);
extern kern_path_parent_t kern_path_parent_fn;
# define spl_kern_path_parent(path, nd) kern_path_parent_fn(path, nd)
# else
# define spl_kern_path_parent(path, nd) kern_path_parent(path, nd)
# endif /* HAVE_KERN_PATH_PARENT_SYMBOL */
#else
# define spl_kern_path_parent(path, nd) path_lookup(path, LOOKUP_PARENT, nd)
#endif /* HAVE_KERN_PATH_PARENT_HEADER */
#ifdef HAVE_KERN_PATH_LOCKED
typedef struct dentry * (*kern_path_locked_t)(const char *, struct path *);
extern kern_path_locked_t kern_path_locked_fn;
# define spl_kern_path_locked(name, path) kern_path_locked_fn(name, path)
#endif /* HAVE_KERN_PATH_LOCKED */
#endif /* SPL_FILE_COMPAT_H */

View File

@ -103,14 +103,6 @@
#define CREATE_XATTR_DIR 0x04
#define ATTR_NOACLCHECK 0x20
#ifdef HAVE_PATH_IN_NAMEIDATA
# define nd_dentry path.dentry
# define nd_mnt path.mnt
#else
# define nd_dentry dentry
# define nd_mnt mnt
#endif
typedef enum vtype {
VNON = 0,
VREG = 1,
@ -194,7 +186,6 @@ extern file_t *vn_getf(int fd);
extern void vn_releasef(int fd);
extern int vn_set_pwd(const char *filename);
int spl_vn_init_kallsyms_lookup(void);
int spl_vn_init(void);
void spl_vn_fini(void);

View File

@ -664,9 +664,6 @@ __init spl_init(void)
if ((rc = spl_kmem_init_kallsyms_lookup()))
SGOTO(out10, rc);
if ((rc = spl_vn_init_kallsyms_lookup()))
SGOTO(out10, rc);
printk(KERN_NOTICE "SPL: Loaded module v%s-%s%s\n", SPL_META_VERSION,
SPL_META_RELEASE, SPL_DEBUG_STR);
SRETURN(rc);

View File

@ -44,17 +44,6 @@ static spl_kmem_cache_t *vn_file_cache;
static DEFINE_SPINLOCK(vn_file_lock);
static LIST_HEAD(vn_file_list);
#ifdef HAVE_KERN_PATH_PARENT_HEADER
#ifndef HAVE_KERN_PATH_PARENT_SYMBOL
kern_path_parent_t kern_path_parent_fn = SYMBOL_POISON;
EXPORT_SYMBOL(kern_path_parent_fn);
#endif /* HAVE_KERN_PATH_PARENT_SYMBOL */
#endif /* HAVE_KERN_PATH_PARENT_HEADER */
#ifdef HAVE_KERN_PATH_LOCKED
kern_path_locked_t kern_path_locked_fn = SYMBOL_POISON;
#endif /* HAVE_KERN_PATH_LOCKED */
vtype_t
vn_mode_to_vtype(mode_t mode)
{
@ -307,7 +296,104 @@ vn_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, void *ct)
}
EXPORT_SYMBOL(vn_seek);
#ifdef HAVE_KERN_PATH_LOCKED
/*
* kern_path() was introduced in Linux 2.6.28. We duplicate it as a
* compatibility shim for earlier kernels.
*/
#ifndef HAVE_KERN_PATH
int
kern_path(const char *name, unsigned int flags, struct path *path)
{
struct nameidata nd;
int rc = path_lookup(name, flags, &nd);
if (!rc)
*path = nd.path;
return rc;
}
#endif /* HAVE_KERN_PATH */
/*
* 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));
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)
@ -328,11 +414,10 @@ vn_remove(const char *path, uio_seg_t seg, int flags)
SGOTO(slashes, rc = 0);
inode = dentry->d_inode;
if (!inode)
SGOTO(slashes, rc = 0);
if (inode)
ihold(inode);
atomic_inc(&inode->i_count);
else
SGOTO(slashes, rc = 0);
#ifdef HAVE_2ARGS_VFS_UNLINK
rc = vfs_unlink(parent.dentry->d_inode, dentry);
@ -437,172 +522,6 @@ exit:
}
EXPORT_SYMBOL(vn_rename);
#else
static struct dentry *
vn_lookup_hash(struct nameidata *nd)
{
return lookup_one_len((const char *)nd->last.name,
nd->nd_dentry, nd->last.len);
} /* lookup_hash() */
static void
vn_path_release(struct nameidata *nd)
{
dput(nd->nd_dentry);
mntput(nd->nd_mnt);
}
/* Modified do_unlinkat() from linux/fs/namei.c, only uses exported symbols */
int
vn_remove(const char *path, uio_seg_t seg, int flags)
{
struct dentry *dentry;
struct nameidata nd;
struct inode *inode = NULL;
int rc = 0;
SENTRY;
ASSERT(seg == UIO_SYSSPACE);
ASSERT(flags == RMFILE);
rc = spl_kern_path_parent(path, &nd);
if (rc)
SGOTO(exit, rc);
rc = -EISDIR;
if (nd.last_type != LAST_NORM)
SGOTO(exit1, rc);
spl_inode_lock_nested(nd.nd_dentry->d_inode, I_MUTEX_PARENT);
dentry = vn_lookup_hash(&nd);
rc = PTR_ERR(dentry);
if (!IS_ERR(dentry)) {
/* Why not before? Because we want correct rc value */
if (nd.last.name[nd.last.len])
SGOTO(slashes, rc);
inode = dentry->d_inode;
if (inode)
atomic_inc(&inode->i_count);
#ifdef HAVE_2ARGS_VFS_UNLINK
rc = vfs_unlink(nd.nd_dentry->d_inode, dentry);
#else
rc = vfs_unlink(nd.nd_dentry->d_inode, dentry, NULL);
#endif /* HAVE_2ARGS_VFS_UNLINK */
exit2:
dput(dentry);
}
spl_inode_unlock(nd.nd_dentry->d_inode);
if (inode)
iput(inode); /* truncate the inode here */
exit1:
vn_path_release(&nd);
exit:
SRETURN(-rc);
slashes:
rc = !dentry->d_inode ? -ENOENT :
S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
SGOTO(exit2, rc);
} /* 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)
{
struct dentry *old_dir, *new_dir;
struct dentry *old_dentry, *new_dentry;
struct dentry *trap;
struct nameidata oldnd, newnd;
int rc = 0;
SENTRY;
rc = spl_kern_path_parent(oldname, &oldnd);
if (rc)
SGOTO(exit, rc);
rc = spl_kern_path_parent(newname, &newnd);
if (rc)
SGOTO(exit1, rc);
rc = -EXDEV;
if (oldnd.nd_mnt != newnd.nd_mnt)
SGOTO(exit2, rc);
old_dir = oldnd.nd_dentry;
rc = -EBUSY;
if (oldnd.last_type != LAST_NORM)
SGOTO(exit2, rc);
new_dir = newnd.nd_dentry;
if (newnd.last_type != LAST_NORM)
SGOTO(exit2, rc);
trap = lock_rename(new_dir, old_dir);
old_dentry = vn_lookup_hash(&oldnd);
rc = PTR_ERR(old_dentry);
if (IS_ERR(old_dentry))
SGOTO(exit3, rc);
/* source must exist */
rc = -ENOENT;
if (!old_dentry->d_inode)
SGOTO(exit4, rc);
/* 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])
SGOTO(exit4, rc);
if (newnd.last.name[newnd.last.len])
SGOTO(exit4, rc);
}
/* source should not be ancestor of target */
rc = -EINVAL;
if (old_dentry == trap)
SGOTO(exit4, rc);
new_dentry = vn_lookup_hash(&newnd);
rc = PTR_ERR(new_dentry);
if (IS_ERR(new_dentry))
SGOTO(exit4, rc);
/* target should not be an ancestor of source */
rc = -ENOTEMPTY;
if (new_dentry == trap)
SGOTO(exit5, rc);
#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
exit5:
dput(new_dentry);
exit4:
dput(old_dentry);
exit3:
unlock_rename(new_dir, old_dir);
exit2:
vn_path_release(&newnd);
exit1:
vn_path_release(&oldnd);
exit:
SRETURN(-rc);
}
EXPORT_SYMBOL(vn_rename);
#endif /* HAVE_KERN_PATH_LOCKED */
int
vn_getattr(vnode_t *vp, vattr_t *vap, int flags, void *x3, void *x4)
{
@ -969,31 +888,6 @@ vn_file_cache_destructor(void *buf, void *cdrarg)
mutex_destroy(&fp->f_lock);
} /* vn_file_cache_destructor() */
int spl_vn_init_kallsyms_lookup(void)
{
#ifdef HAVE_KERN_PATH_PARENT_HEADER
#ifndef HAVE_KERN_PATH_PARENT_SYMBOL
kern_path_parent_fn = (kern_path_parent_t)
spl_kallsyms_lookup_name("kern_path_parent");
if (!kern_path_parent_fn) {
printk(KERN_ERR "Error: Unknown symbol kern_path_parent\n");
return -EFAULT;
}
#endif /* HAVE_KERN_PATH_PARENT_SYMBOL */
#endif /* HAVE_KERN_PATH_PARENT_HEADER */
#ifdef HAVE_KERN_PATH_LOCKED
kern_path_locked_fn = (kern_path_locked_t)
spl_kallsyms_lookup_name("kern_path_locked");
if (!kern_path_locked_fn) {
printk(KERN_ERR "Error: Unknown symbol kern_path_locked\n");
return -EFAULT;
}
#endif
return (0);
}
int
spl_vn_init(void)
{