forked from Minki/linux
cc53ce53c8
Add a dentry op (d_manage) to permit a filesystem to hold a process and make it sleep when it tries to transit away from one of that filesystem's directories during a pathwalk. The operation is keyed off a new dentry flag (DCACHE_MANAGE_TRANSIT). The filesystem is allowed to be selective about which processes it holds and which it permits to continue on or prohibits from transiting from each flagged directory. This will allow autofs to hold up client processes whilst letting its userspace daemon through to maintain the directory or the stuff behind it or mounted upon it. The ->d_manage() dentry operation: int (*d_manage)(struct path *path, bool mounting_here); takes a pointer to the directory about to be transited away from and a flag indicating whether the transit is undertaken by do_add_mount() or do_move_mount() skipping through a pile of filesystems mounted on a mountpoint. It should return 0 if successful and to let the process continue on its way; -EISDIR to prohibit the caller from skipping to overmounted filesystems or automounting, and to use this directory; or some other error code to return to the user. ->d_manage() is called with namespace_sem writelocked if mounting_here is true and no other locks held, so it may sleep. However, if mounting_here is true, it may not initiate or wait for a mount or unmount upon the parameter directory, even if the act is actually performed by userspace. Within fs/namei.c, follow_managed() is extended to check with d_manage() first on each managed directory, before transiting away from it or attempting to automount upon it. follow_down() is renamed follow_down_one() and should only be used where the filesystem deliberately intends to avoid management steps (e.g. autofs). A new follow_down() is added that incorporates the loop done by all other callers of follow_down() (do_add/move_mount(), autofs and NFSD; whilst AFS, NFS and CIFS do use it, their use is removed by converting them to use d_automount()). The new follow_down() calls d_manage() as appropriate. It also takes an extra parameter to indicate if it is being called from mount code (with namespace_sem writelocked) which it passes to d_manage(). follow_down() ignores automount points so that it can be used to mount on them. __follow_mount_rcu() is made to abort rcu-walk mode if it hits a directory with DCACHE_MANAGE_TRANSIT set on the basis that we're probably going to have to sleep. It would be possible to enter d_manage() in rcu-walk mode too, and have that determine whether to abort or not itself. That would allow the autofs daemon to continue on in rcu-walk mode. Note that DCACHE_MANAGE_TRANSIT on a directory should be cleared when it isn't required as every tranist from that directory will cause d_manage() to be invoked. It can always be set again when necessary. ========================== WHAT THIS MEANS FOR AUTOFS ========================== Autofs currently uses the lookup() inode op and the d_revalidate() dentry op to trigger the automounting of indirect mounts, and both of these can be called with i_mutex held. autofs knows that the i_mutex will be held by the caller in lookup(), and so can drop it before invoking the daemon - but this isn't so for d_revalidate(), since the lock is only held on _some_ of the code paths that call it. This means that autofs can't risk dropping i_mutex from its d_revalidate() function before it calls the daemon. The bug could manifest itself as, for example, a process that's trying to validate an automount dentry that gets made to wait because that dentry is expired and needs cleaning up: mkdir S ffffffff8014e05a 0 32580 24956 Call Trace: [<ffffffff885371fd>] :autofs4:autofs4_wait+0x674/0x897 [<ffffffff80127f7d>] avc_has_perm+0x46/0x58 [<ffffffff8009fdcf>] autoremove_wake_function+0x0/0x2e [<ffffffff88537be6>] :autofs4:autofs4_expire_wait+0x41/0x6b [<ffffffff88535cfc>] :autofs4:autofs4_revalidate+0x91/0x149 [<ffffffff80036d96>] __lookup_hash+0xa0/0x12f [<ffffffff80057a2f>] lookup_create+0x46/0x80 [<ffffffff800e6e31>] sys_mkdirat+0x56/0xe4 versus the automount daemon which wants to remove that dentry, but can't because the normal process is holding the i_mutex lock: automount D ffffffff8014e05a 0 32581 1 32561 Call Trace: [<ffffffff80063c3f>] __mutex_lock_slowpath+0x60/0x9b [<ffffffff8000ccf1>] do_path_lookup+0x2ca/0x2f1 [<ffffffff80063c89>] .text.lock.mutex+0xf/0x14 [<ffffffff800e6d55>] do_rmdir+0x77/0xde [<ffffffff8005d229>] tracesys+0x71/0xe0 [<ffffffff8005d28d>] tracesys+0xd5/0xe0 which means that the system is deadlocked. This patch allows autofs to hold up normal processes whilst the daemon goes ahead and does things to the dentry tree behind the automouter point without risking a deadlock as almost no locks are held in d_manage() and none in d_automount(). Signed-off-by: David Howells <dhowells@redhat.com> Was-Acked-by: Ian Kent <raven@themaw.net> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
273 lines
6.9 KiB
C
273 lines
6.9 KiB
C
/*
|
|
* linux/fs/nfs/namespace.c
|
|
*
|
|
* Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
|
|
* - Modified by David Howells <dhowells@redhat.com>
|
|
*
|
|
* NFS namespace
|
|
*/
|
|
|
|
#include <linux/dcache.h>
|
|
#include <linux/gfp.h>
|
|
#include <linux/mount.h>
|
|
#include <linux/namei.h>
|
|
#include <linux/nfs_fs.h>
|
|
#include <linux/string.h>
|
|
#include <linux/sunrpc/clnt.h>
|
|
#include <linux/vfs.h>
|
|
#include "internal.h"
|
|
|
|
#define NFSDBG_FACILITY NFSDBG_VFS
|
|
|
|
static void nfs_expire_automounts(struct work_struct *work);
|
|
|
|
static LIST_HEAD(nfs_automount_list);
|
|
static DECLARE_DELAYED_WORK(nfs_automount_task, nfs_expire_automounts);
|
|
int nfs_mountpoint_expiry_timeout = 500 * HZ;
|
|
|
|
static struct vfsmount *nfs_do_submount(const struct vfsmount *mnt_parent,
|
|
const struct dentry *dentry,
|
|
struct nfs_fh *fh,
|
|
struct nfs_fattr *fattr);
|
|
|
|
/*
|
|
* nfs_path - reconstruct the path given an arbitrary dentry
|
|
* @base - arbitrary string to prepend to the path
|
|
* @droot - pointer to root dentry for mountpoint
|
|
* @dentry - pointer to dentry
|
|
* @buffer - result buffer
|
|
* @buflen - length of buffer
|
|
*
|
|
* Helper function for constructing the path from the
|
|
* root dentry to an arbitrary hashed dentry.
|
|
*
|
|
* This is mainly for use in figuring out the path on the
|
|
* server side when automounting on top of an existing partition.
|
|
*/
|
|
char *nfs_path(const char *base,
|
|
const struct dentry *droot,
|
|
const struct dentry *dentry,
|
|
char *buffer, ssize_t buflen)
|
|
{
|
|
char *end;
|
|
int namelen;
|
|
unsigned seq;
|
|
|
|
rename_retry:
|
|
end = buffer+buflen;
|
|
*--end = '\0';
|
|
buflen--;
|
|
|
|
seq = read_seqbegin(&rename_lock);
|
|
rcu_read_lock();
|
|
while (!IS_ROOT(dentry) && dentry != droot) {
|
|
namelen = dentry->d_name.len;
|
|
buflen -= namelen + 1;
|
|
if (buflen < 0)
|
|
goto Elong_unlock;
|
|
end -= namelen;
|
|
memcpy(end, dentry->d_name.name, namelen);
|
|
*--end = '/';
|
|
dentry = dentry->d_parent;
|
|
}
|
|
rcu_read_unlock();
|
|
if (read_seqretry(&rename_lock, seq))
|
|
goto rename_retry;
|
|
if (*end != '/') {
|
|
if (--buflen < 0)
|
|
goto Elong;
|
|
*--end = '/';
|
|
}
|
|
namelen = strlen(base);
|
|
/* Strip off excess slashes in base string */
|
|
while (namelen > 0 && base[namelen - 1] == '/')
|
|
namelen--;
|
|
buflen -= namelen;
|
|
if (buflen < 0)
|
|
goto Elong;
|
|
end -= namelen;
|
|
memcpy(end, base, namelen);
|
|
return end;
|
|
Elong_unlock:
|
|
rcu_read_unlock();
|
|
if (read_seqretry(&rename_lock, seq))
|
|
goto rename_retry;
|
|
Elong:
|
|
return ERR_PTR(-ENAMETOOLONG);
|
|
}
|
|
|
|
/*
|
|
* nfs_follow_mountpoint - handle crossing a mountpoint on the server
|
|
* @dentry - dentry of mountpoint
|
|
* @nd - nameidata info
|
|
*
|
|
* When we encounter a mountpoint on the server, we want to set up
|
|
* a mountpoint on the client too, to prevent inode numbers from
|
|
* colliding, and to allow "df" to work properly.
|
|
* On NFSv4, we also want to allow for the fact that different
|
|
* filesystems may be migrated to different servers in a failover
|
|
* situation, and that different filesystems may want to use
|
|
* different security flavours.
|
|
*/
|
|
static void * nfs_follow_mountpoint(struct dentry *dentry, struct nameidata *nd)
|
|
{
|
|
struct vfsmount *mnt;
|
|
struct nfs_server *server = NFS_SERVER(dentry->d_inode);
|
|
struct dentry *parent;
|
|
struct nfs_fh *fh = NULL;
|
|
struct nfs_fattr *fattr = NULL;
|
|
int err;
|
|
|
|
dprintk("--> nfs_follow_mountpoint()\n");
|
|
|
|
err = -ESTALE;
|
|
if (IS_ROOT(dentry))
|
|
goto out_err;
|
|
|
|
err = -ENOMEM;
|
|
fh = nfs_alloc_fhandle();
|
|
fattr = nfs_alloc_fattr();
|
|
if (fh == NULL || fattr == NULL)
|
|
goto out_err;
|
|
|
|
dprintk("%s: enter\n", __func__);
|
|
dput(nd->path.dentry);
|
|
nd->path.dentry = dget(dentry);
|
|
|
|
/* Look it up again */
|
|
parent = dget_parent(nd->path.dentry);
|
|
err = server->nfs_client->rpc_ops->lookup(parent->d_inode,
|
|
&nd->path.dentry->d_name,
|
|
fh, fattr);
|
|
dput(parent);
|
|
if (err != 0)
|
|
goto out_err;
|
|
|
|
if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
|
|
mnt = nfs_do_refmount(nd->path.mnt, nd->path.dentry);
|
|
else
|
|
mnt = nfs_do_submount(nd->path.mnt, nd->path.dentry, fh,
|
|
fattr);
|
|
err = PTR_ERR(mnt);
|
|
if (IS_ERR(mnt))
|
|
goto out_err;
|
|
|
|
mntget(mnt);
|
|
err = do_add_mount(mnt, &nd->path, nd->path.mnt->mnt_flags|MNT_SHRINKABLE,
|
|
&nfs_automount_list);
|
|
if (err < 0) {
|
|
mntput(mnt);
|
|
if (err == -EBUSY)
|
|
goto out_follow;
|
|
goto out_err;
|
|
}
|
|
path_put(&nd->path);
|
|
nd->path.mnt = mnt;
|
|
nd->path.dentry = dget(mnt->mnt_root);
|
|
schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
|
|
out:
|
|
nfs_free_fattr(fattr);
|
|
nfs_free_fhandle(fh);
|
|
dprintk("%s: done, returned %d\n", __func__, err);
|
|
|
|
dprintk("<-- nfs_follow_mountpoint() = %d\n", err);
|
|
return ERR_PTR(err);
|
|
out_err:
|
|
path_put(&nd->path);
|
|
goto out;
|
|
out_follow:
|
|
err = follow_down(&nd->path, false);
|
|
goto out;
|
|
}
|
|
|
|
const struct inode_operations nfs_mountpoint_inode_operations = {
|
|
.follow_link = nfs_follow_mountpoint,
|
|
.getattr = nfs_getattr,
|
|
};
|
|
|
|
const struct inode_operations nfs_referral_inode_operations = {
|
|
.follow_link = nfs_follow_mountpoint,
|
|
};
|
|
|
|
static void nfs_expire_automounts(struct work_struct *work)
|
|
{
|
|
struct list_head *list = &nfs_automount_list;
|
|
|
|
mark_mounts_for_expiry(list);
|
|
if (!list_empty(list))
|
|
schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
|
|
}
|
|
|
|
void nfs_release_automount_timer(void)
|
|
{
|
|
if (list_empty(&nfs_automount_list))
|
|
cancel_delayed_work(&nfs_automount_task);
|
|
}
|
|
|
|
/*
|
|
* Clone a mountpoint of the appropriate type
|
|
*/
|
|
static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
|
|
const char *devname,
|
|
struct nfs_clone_mount *mountdata)
|
|
{
|
|
#ifdef CONFIG_NFS_V4
|
|
struct vfsmount *mnt = ERR_PTR(-EINVAL);
|
|
switch (server->nfs_client->rpc_ops->version) {
|
|
case 2:
|
|
case 3:
|
|
mnt = vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
|
|
break;
|
|
case 4:
|
|
mnt = vfs_kern_mount(&nfs4_xdev_fs_type, 0, devname, mountdata);
|
|
}
|
|
return mnt;
|
|
#else
|
|
return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
|
|
#endif
|
|
}
|
|
|
|
/**
|
|
* nfs_do_submount - set up mountpoint when crossing a filesystem boundary
|
|
* @mnt_parent - mountpoint of parent directory
|
|
* @dentry - parent directory
|
|
* @fh - filehandle for new root dentry
|
|
* @fattr - attributes for new root inode
|
|
*
|
|
*/
|
|
static struct vfsmount *nfs_do_submount(const struct vfsmount *mnt_parent,
|
|
const struct dentry *dentry,
|
|
struct nfs_fh *fh,
|
|
struct nfs_fattr *fattr)
|
|
{
|
|
struct nfs_clone_mount mountdata = {
|
|
.sb = mnt_parent->mnt_sb,
|
|
.dentry = dentry,
|
|
.fh = fh,
|
|
.fattr = fattr,
|
|
};
|
|
struct vfsmount *mnt = ERR_PTR(-ENOMEM);
|
|
char *page = (char *) __get_free_page(GFP_USER);
|
|
char *devname;
|
|
|
|
dprintk("--> nfs_do_submount()\n");
|
|
|
|
dprintk("%s: submounting on %s/%s\n", __func__,
|
|
dentry->d_parent->d_name.name,
|
|
dentry->d_name.name);
|
|
if (page == NULL)
|
|
goto out;
|
|
devname = nfs_devname(mnt_parent, dentry, page, PAGE_SIZE);
|
|
mnt = (struct vfsmount *)devname;
|
|
if (IS_ERR(devname))
|
|
goto free_page;
|
|
mnt = nfs_do_clone_mount(NFS_SB(mnt_parent->mnt_sb), devname, &mountdata);
|
|
free_page:
|
|
free_page((unsigned long)page);
|
|
out:
|
|
dprintk("%s: done\n", __func__);
|
|
|
|
dprintk("<-- nfs_do_submount() = %p\n", mnt);
|
|
return mnt;
|
|
}
|