Commit e4eff1a6 authored by Trond Myklebust's avatar Trond Myklebust

SUNRPC: Clean up the sillyrename code

Fix a couple of bugs:
 - Don't rely on the parent dentry still being valid when the call completes.
   Fixes a race with shrink_dcache_for_umount_subtree()

 - Don't remove the file if the filehandle has been labelled as stale.

Fix a couple of inefficiencies
 - Remove the global list of sillyrenamed files. Instead we can cache the
   sillyrename information in the dentry->d_fsdata
 - Move common code from unlink_setup/unlink_done into fs/nfs/unlink.c
Signed-off-by: default avatarTrond Myklebust <Trond.Myklebust@netapp.com>
parent 4fdc17b2
...@@ -869,7 +869,7 @@ static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) ...@@ -869,7 +869,7 @@ static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
lock_kernel(); lock_kernel();
drop_nlink(inode); drop_nlink(inode);
nfs_complete_unlink(dentry); nfs_complete_unlink(dentry, inode);
unlock_kernel(); unlock_kernel();
} }
/* When creating a negative dentry, we want to renew d_time */ /* When creating a negative dentry, we want to renew d_time */
...@@ -1411,7 +1411,7 @@ static int nfs_sillyrename(struct inode *dir, struct dentry *dentry) ...@@ -1411,7 +1411,7 @@ static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
nfs_renew_times(dentry); nfs_renew_times(dentry);
nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
d_move(dentry, sdentry); d_move(dentry, sdentry);
error = nfs_async_unlink(dentry); error = nfs_async_unlink(dir, dentry);
/* If we return 0 we don't unlink */ /* If we return 0 we don't unlink */
} }
dput(sdentry); dput(sdentry);
......
...@@ -370,41 +370,21 @@ nfs3_proc_remove(struct inode *dir, struct qstr *name) ...@@ -370,41 +370,21 @@ nfs3_proc_remove(struct inode *dir, struct qstr *name)
return status; return status;
} }
static int static void
nfs3_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name) nfs3_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
{ {
struct unlinkxdr {
struct nfs_removeargs arg;
struct nfs_removeres res;
} *ptr;
ptr = kmalloc(sizeof(*ptr), GFP_KERNEL);
if (!ptr)
return -ENOMEM;
ptr->arg.fh = NFS_FH(dir->d_inode);
ptr->arg.name.name = name->name;
ptr->arg.name.len = name->len;
nfs_fattr_init(&ptr->res.dir_attr);
msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE]; msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
msg->rpc_argp = &ptr->arg;
msg->rpc_resp = &ptr->res;
return 0;
} }
static int static int
nfs3_proc_unlink_done(struct dentry *dir, struct rpc_task *task) nfs3_proc_unlink_done(struct rpc_task *task, struct inode *dir)
{ {
struct rpc_message *msg = &task->tk_msg; struct nfs_removeres *res;
struct nfs_fattr *dir_attr; if (nfs3_async_handle_jukebox(task, dir))
if (nfs3_async_handle_jukebox(task, dir->d_inode))
return 1;
if (msg->rpc_argp) {
dir_attr = &((struct nfs_removeres*)msg->rpc_resp)->dir_attr;
nfs_post_op_update_inode(dir->d_inode, dir_attr);
kfree(msg->rpc_argp);
}
return 0; return 0;
res = task->tk_msg.rpc_resp;
nfs_post_op_update_inode(dir, &res->dir_attr);
return 1;
} }
static int static int
......
...@@ -1962,48 +1962,26 @@ static int nfs4_proc_remove(struct inode *dir, struct qstr *name) ...@@ -1962,48 +1962,26 @@ static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
return err; return err;
} }
struct unlink_desc { static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
struct nfs_removeargs args;
struct nfs_removeres res;
};
static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
struct qstr *name)
{ {
struct nfs_server *server = NFS_SERVER(dir->d_inode); struct nfs_server *server = NFS_SERVER(dir);
struct unlink_desc *up; struct nfs_removeargs *args = msg->rpc_argp;
struct nfs_removeres *res = msg->rpc_resp;
up = kmalloc(sizeof(*up), GFP_KERNEL);
if (!up)
return -ENOMEM;
up->args.fh = NFS_FH(dir->d_inode);
up->args.name.len = name->len;
up->args.name.name = name->name;
up->args.bitmask = server->attr_bitmask;
up->res.server = server;
nfs_fattr_init(&up->res.dir_attr);
args->bitmask = server->attr_bitmask;
res->server = server;
msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE]; msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
msg->rpc_argp = &up->args;
msg->rpc_resp = &up->res;
return 0;
} }
static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task) static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
{ {
struct rpc_message *msg = &task->tk_msg; struct nfs_removeres *res = task->tk_msg.rpc_resp;
struct unlink_desc *up;
if (msg->rpc_resp != NULL) { if (nfs4_async_handle_error(task, res->server) == -EAGAIN)
up = container_of(msg->rpc_resp, struct unlink_desc, res);
update_changeattr(dir->d_inode, &up->res.cinfo);
nfs_post_op_update_inode(dir->d_inode, &up->res.dir_attr);
kfree(up);
msg->rpc_resp = NULL;
msg->rpc_argp = NULL;
}
return 0; return 0;
update_changeattr(dir, &res->cinfo);
nfs_post_op_update_inode(dir, &res->dir_attr);
return 1;
} }
static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name, static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
......
...@@ -291,32 +291,16 @@ nfs_proc_remove(struct inode *dir, struct qstr *name) ...@@ -291,32 +291,16 @@ nfs_proc_remove(struct inode *dir, struct qstr *name)
return status; return status;
} }
static int static void
nfs_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name) nfs_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
{ {
struct nfs_removeargs *arg;
arg = kmalloc(sizeof(*arg), GFP_KERNEL);
if (!arg)
return -ENOMEM;
arg->fh = NFS_FH(dir->d_inode);
arg->name.name = name->name;
arg->name.len = name->len;
msg->rpc_proc = &nfs_procedures[NFSPROC_REMOVE]; msg->rpc_proc = &nfs_procedures[NFSPROC_REMOVE];
msg->rpc_argp = arg;
return 0;
} }
static int static int nfs_proc_unlink_done(struct rpc_task *task, struct inode *dir)
nfs_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
{ {
struct rpc_message *msg = &task->tk_msg; nfs_mark_for_revalidate(dir);
return 1;
if (msg->rpc_argp) {
nfs_mark_for_revalidate(dir->d_inode);
kfree(msg->rpc_argp);
}
return 0;
} }
static int static int
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
* *
* nfs sillydelete handling * nfs sillydelete handling
* *
* NOTE: we rely on holding the BKL for list manipulation protection.
*/ */
#include <linux/slab.h> #include <linux/slab.h>
...@@ -15,46 +14,23 @@ ...@@ -15,46 +14,23 @@
struct nfs_unlinkdata { struct nfs_unlinkdata {
struct nfs_unlinkdata *next; struct nfs_removeargs args;
struct dentry *dir, *dentry; struct nfs_removeres res;
struct qstr name; struct inode *dir;
struct rpc_task task;
struct rpc_cred *cred; struct rpc_cred *cred;
unsigned int count;
}; };
static struct nfs_unlinkdata *nfs_deletes;
static RPC_WAITQ(nfs_delete_queue, "nfs_delete_queue");
/**
* nfs_detach_unlinkdata - Remove asynchronous unlink from global list
* @data: pointer to descriptor
*/
static inline void
nfs_detach_unlinkdata(struct nfs_unlinkdata *data)
{
struct nfs_unlinkdata **q;
for (q = &nfs_deletes; *q != NULL; q = &((*q)->next)) {
if (*q == data) {
*q = data->next;
break;
}
}
}
/** /**
* nfs_put_unlinkdata - release data from a sillydelete operation. * nfs_free_unlinkdata - release data from a sillydelete operation.
* @data: pointer to unlink structure. * @data: pointer to unlink structure.
*/ */
static void static void
nfs_put_unlinkdata(struct nfs_unlinkdata *data) nfs_free_unlinkdata(struct nfs_unlinkdata *data)
{ {
if (--data->count == 0) { iput(data->dir);
nfs_detach_unlinkdata(data); put_rpccred(data->cred);
kfree(data->name.name); kfree(data->args.name.name);
kfree(data); kfree(data);
}
} }
#define NAME_ALLOC_LEN(len) ((len+16) & ~15) #define NAME_ALLOC_LEN(len) ((len+16) & ~15)
...@@ -63,50 +39,36 @@ nfs_put_unlinkdata(struct nfs_unlinkdata *data) ...@@ -63,50 +39,36 @@ nfs_put_unlinkdata(struct nfs_unlinkdata *data)
* @dentry: pointer to dentry * @dentry: pointer to dentry
* @data: nfs_unlinkdata * @data: nfs_unlinkdata
*/ */
static inline void static int nfs_copy_dname(struct dentry *dentry, struct nfs_unlinkdata *data)
nfs_copy_dname(struct dentry *dentry, struct nfs_unlinkdata *data)
{ {
char *str; char *str;
int len = dentry->d_name.len; int len = dentry->d_name.len;
str = kmalloc(NAME_ALLOC_LEN(len), GFP_KERNEL); str = kmemdup(dentry->d_name.name, NAME_ALLOC_LEN(len), GFP_KERNEL);
if (!str) if (!str)
return; return -ENOMEM;
memcpy(str, dentry->d_name.name, len); data->args.name.len = len;
if (!data->name.len) { data->args.name.name = str;
data->name.len = len; return 0;
data->name.name = str;
} else
kfree(str);
} }
/** /**
* nfs_async_unlink_init - Initialize the RPC info * nfs_async_unlink_init - Initialize the RPC info
* @task: rpc_task of the sillydelete * task: rpc_task of the sillydelete
*
* We delay initializing RPC info until after the call to dentry_iput()
* in order to minimize races against rename().
*/ */
static void nfs_async_unlink_init(struct rpc_task *task, void *calldata) static void nfs_async_unlink_init(struct rpc_task *task, void *calldata)
{ {
struct nfs_unlinkdata *data = calldata; struct nfs_unlinkdata *data = calldata;
struct dentry *dir = data->dir; struct inode *dir = data->dir;
struct rpc_message msg = { struct rpc_message msg = {
.rpc_argp = &data->args,
.rpc_resp = &data->res,
.rpc_cred = data->cred, .rpc_cred = data->cred,
}; };
int status = -ENOENT;
if (!data->name.len) nfs_begin_data_update(dir);
goto out_err; NFS_PROTO(dir)->unlink_setup(&msg, dir);
status = NFS_PROTO(dir->d_inode)->unlink_setup(&msg, dir, &data->name);
if (status < 0)
goto out_err;
nfs_begin_data_update(dir->d_inode);
rpc_call_setup(task, &msg, 0); rpc_call_setup(task, &msg, 0);
return;
out_err:
rpc_exit(task, status);
} }
/** /**
...@@ -118,18 +80,12 @@ static void nfs_async_unlink_init(struct rpc_task *task, void *calldata) ...@@ -118,18 +80,12 @@ static void nfs_async_unlink_init(struct rpc_task *task, void *calldata)
static void nfs_async_unlink_done(struct rpc_task *task, void *calldata) static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
{ {
struct nfs_unlinkdata *data = calldata; struct nfs_unlinkdata *data = calldata;
struct dentry *dir = data->dir; struct inode *dir = data->dir;
struct inode *dir_i;
if (!NFS_PROTO(dir)->unlink_done(task, dir))
if (!dir) rpc_restart_call(task);
return; else
dir_i = dir->d_inode; nfs_end_data_update(dir);
nfs_end_data_update(dir_i);
if (NFS_PROTO(dir_i)->unlink_done(dir, task))
return;
put_rpccred(data->cred);
data->cred = NULL;
dput(dir);
} }
/** /**
...@@ -142,7 +98,7 @@ static void nfs_async_unlink_done(struct rpc_task *task, void *calldata) ...@@ -142,7 +98,7 @@ static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
static void nfs_async_unlink_release(void *calldata) static void nfs_async_unlink_release(void *calldata)
{ {
struct nfs_unlinkdata *data = calldata; struct nfs_unlinkdata *data = calldata;
nfs_put_unlinkdata(data); nfs_free_unlinkdata(data);
} }
static const struct rpc_call_ops nfs_unlink_ops = { static const struct rpc_call_ops nfs_unlink_ops = {
...@@ -151,73 +107,94 @@ static const struct rpc_call_ops nfs_unlink_ops = { ...@@ -151,73 +107,94 @@ static const struct rpc_call_ops nfs_unlink_ops = {
.rpc_release = nfs_async_unlink_release, .rpc_release = nfs_async_unlink_release,
}; };
static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data)
{
struct rpc_task *task;
struct dentry *parent;
struct inode *dir;
if (nfs_copy_dname(dentry, data) < 0)
goto out_free;
parent = dget_parent(dentry);
if (parent == NULL)
goto out_free;
dir = igrab(parent->d_inode);
dput(parent);
if (dir == NULL)
goto out_free;
data->dir = dir;
data->args.fh = NFS_FH(dir);
nfs_fattr_init(&data->res.dir_attr);
task = rpc_run_task(NFS_CLIENT(dir), RPC_TASK_ASYNC, &nfs_unlink_ops, data);
if (!IS_ERR(task))
rpc_put_task(task);
return 1;
out_free:
return 0;
}
/** /**
* nfs_async_unlink - asynchronous unlinking of a file * nfs_async_unlink - asynchronous unlinking of a file
* @dir: parent directory of dentry
* @dentry: dentry to unlink * @dentry: dentry to unlink
*/ */
int int
nfs_async_unlink(struct dentry *dentry) nfs_async_unlink(struct inode *dir, struct dentry *dentry)
{ {
struct dentry *dir = dentry->d_parent;
struct nfs_unlinkdata *data; struct nfs_unlinkdata *data;
struct rpc_clnt *clnt = NFS_CLIENT(dir->d_inode);
int status = -ENOMEM; int status = -ENOMEM;
data = kzalloc(sizeof(*data), GFP_KERNEL); data = kzalloc(sizeof(*data), GFP_KERNEL);
if (!data) if (data == NULL)
goto out; goto out;
data->cred = rpcauth_lookupcred(clnt->cl_auth, 0); data->cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
if (IS_ERR(data->cred)) { if (IS_ERR(data->cred)) {
status = PTR_ERR(data->cred); status = PTR_ERR(data->cred);
goto out_free; goto out_free;
} }
data->dir = dget(dir);
data->dentry = dentry;
data->next = nfs_deletes;
nfs_deletes = data;
data->count = 1;
rpc_init_task(&data->task, clnt, RPC_TASK_ASYNC, &nfs_unlink_ops, data);
status = -EBUSY;
spin_lock(&dentry->d_lock); spin_lock(&dentry->d_lock);
if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
goto out_unlock;
dentry->d_flags |= DCACHE_NFSFS_RENAMED; dentry->d_flags |= DCACHE_NFSFS_RENAMED;
dentry->d_fsdata = data;
spin_unlock(&dentry->d_lock); spin_unlock(&dentry->d_lock);
return 0;
rpc_sleep_on(&nfs_delete_queue, &data->task, NULL, NULL); out_unlock:
status = 0; spin_unlock(&dentry->d_lock);
out: put_rpccred(data->cred);
return status;
out_free: out_free:
kfree(data); kfree(data);
out:
return status; return status;
} }
/** /**
* nfs_complete_unlink - Initialize completion of the sillydelete * nfs_complete_unlink - Initialize completion of the sillydelete
* @dentry: dentry to delete * @dentry: dentry to delete
* @inode: inode
* *
* Since we're most likely to be called by dentry_iput(), we * Since we're most likely to be called by dentry_iput(), we
* only use the dentry to find the sillydelete. We then copy the name * only use the dentry to find the sillydelete. We then copy the name
* into the qstr. * into the qstr.
*/ */
void void
nfs_complete_unlink(struct dentry *dentry) nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
{ {
struct nfs_unlinkdata *data; struct nfs_unlinkdata *data = NULL;
for(data = nfs_deletes; data != NULL; data = data->next) {
if (dentry == data->dentry)
break;
}
if (!data)
return;
data->count++;
nfs_copy_dname(dentry, data);
spin_lock(&dentry->d_lock); spin_lock(&dentry->d_lock);
if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
dentry->d_flags &= ~DCACHE_NFSFS_RENAMED; dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
data = dentry->d_fsdata;
}
spin_unlock(&dentry->d_lock); spin_unlock(&dentry->d_lock);
rpc_wake_up_task(&data->task);
nfs_put_unlinkdata(data); if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data)))
nfs_free_unlinkdata(data);
} }
...@@ -407,8 +407,8 @@ extern void nfs_release_automount_timer(void); ...@@ -407,8 +407,8 @@ extern void nfs_release_automount_timer(void);
/* /*
* linux/fs/nfs/unlink.c * linux/fs/nfs/unlink.c
*/ */
extern int nfs_async_unlink(struct dentry *); extern int nfs_async_unlink(struct inode *dir, struct dentry *dentry);
extern void nfs_complete_unlink(struct dentry *); extern void nfs_complete_unlink(struct dentry *dentry, struct inode *);
/* /*
* linux/fs/nfs/write.c * linux/fs/nfs/write.c
......
...@@ -791,9 +791,8 @@ struct nfs_rpc_ops { ...@@ -791,9 +791,8 @@ struct nfs_rpc_ops {
int (*create) (struct inode *, struct dentry *, int (*create) (struct inode *, struct dentry *,
struct iattr *, int, struct nameidata *); struct iattr *, int, struct nameidata *);
int (*remove) (struct inode *, struct qstr *); int (*remove) (struct inode *, struct qstr *);
int (*unlink_setup) (struct rpc_message *, void (*unlink_setup) (struct rpc_message *, struct inode *dir);
struct dentry *, struct qstr *); int (*unlink_done) (struct rpc_task *, struct inode *);
int (*unlink_done) (struct dentry *, struct rpc_task *);
int (*rename) (struct inode *, struct qstr *, int (*rename) (struct inode *, struct qstr *,
struct inode *, struct qstr *); struct inode *, struct qstr *);
int (*link) (struct inode *, struct inode *, struct qstr *); int (*link) (struct inode *, struct inode *, struct qstr *);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment