2 * linux/fs/nfs/unlink.c
4 * nfs sillydelete handling
8 #include <linux/slab.h>
9 #include <linux/string.h>
10 #include <linux/dcache.h>
11 #include <linux/sunrpc/sched.h>
12 #include <linux/sunrpc/clnt.h>
13 #include <linux/nfs_fs.h>
16 struct nfs_unlinkdata {
17 struct nfs_removeargs args;
18 struct nfs_removeres res;
20 struct rpc_cred *cred;
24 * nfs_free_unlinkdata - release data from a sillydelete operation.
25 * @data: pointer to unlink structure.
28 nfs_free_unlinkdata(struct nfs_unlinkdata *data)
31 put_rpccred(data->cred);
32 kfree(data->args.name.name);
36 #define NAME_ALLOC_LEN(len) ((len+16) & ~15)
38 * nfs_copy_dname - copy dentry name to data structure
39 * @dentry: pointer to dentry
40 * @data: nfs_unlinkdata
42 static int nfs_copy_dname(struct dentry *dentry, struct nfs_unlinkdata *data)
45 int len = dentry->d_name.len;
47 str = kmemdup(dentry->d_name.name, NAME_ALLOC_LEN(len), GFP_KERNEL);
50 data->args.name.len = len;
51 data->args.name.name = str;
56 * nfs_async_unlink_init - Initialize the RPC info
57 * task: rpc_task of the sillydelete
59 static void nfs_async_unlink_init(struct rpc_task *task, void *calldata)
61 struct nfs_unlinkdata *data = calldata;
62 struct inode *dir = data->dir;
63 struct rpc_message msg = {
64 .rpc_argp = &data->args,
65 .rpc_resp = &data->res,
66 .rpc_cred = data->cred,
69 nfs_begin_data_update(dir);
70 NFS_PROTO(dir)->unlink_setup(&msg, dir);
71 rpc_call_setup(task, &msg, 0);
75 * nfs_async_unlink_done - Sillydelete post-processing
76 * @task: rpc_task of the sillydelete
78 * Do the directory attribute update.
80 static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
82 struct nfs_unlinkdata *data = calldata;
83 struct inode *dir = data->dir;
85 if (!NFS_PROTO(dir)->unlink_done(task, dir))
86 rpc_restart_call(task);
88 nfs_end_data_update(dir);
92 * nfs_async_unlink_release - Release the sillydelete data.
93 * @task: rpc_task of the sillydelete
95 * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
96 * rpc_task would be freed too.
98 static void nfs_async_unlink_release(void *calldata)
100 struct nfs_unlinkdata *data = calldata;
101 nfs_free_unlinkdata(data);
104 static const struct rpc_call_ops nfs_unlink_ops = {
105 .rpc_call_prepare = nfs_async_unlink_init,
106 .rpc_call_done = nfs_async_unlink_done,
107 .rpc_release = nfs_async_unlink_release,
110 static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data)
112 struct rpc_task *task;
113 struct dentry *parent;
116 if (nfs_copy_dname(dentry, data) < 0)
119 parent = dget_parent(dentry);
122 dir = igrab(parent->d_inode);
128 data->args.fh = NFS_FH(dir);
129 nfs_fattr_init(&data->res.dir_attr);
131 task = rpc_run_task(NFS_CLIENT(dir), RPC_TASK_ASYNC, &nfs_unlink_ops, data);
140 * nfs_async_unlink - asynchronous unlinking of a file
141 * @dir: parent directory of dentry
142 * @dentry: dentry to unlink
145 nfs_async_unlink(struct inode *dir, struct dentry *dentry)
147 struct nfs_unlinkdata *data;
148 int status = -ENOMEM;
150 data = kzalloc(sizeof(*data), GFP_KERNEL);
154 data->cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
155 if (IS_ERR(data->cred)) {
156 status = PTR_ERR(data->cred);
161 spin_lock(&dentry->d_lock);
162 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
164 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
165 dentry->d_fsdata = data;
166 spin_unlock(&dentry->d_lock);
169 spin_unlock(&dentry->d_lock);
170 put_rpccred(data->cred);
178 * nfs_complete_unlink - Initialize completion of the sillydelete
179 * @dentry: dentry to delete
182 * Since we're most likely to be called by dentry_iput(), we
183 * only use the dentry to find the sillydelete. We then copy the name
187 nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
189 struct nfs_unlinkdata *data = NULL;
191 spin_lock(&dentry->d_lock);
192 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
193 dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
194 data = dentry->d_fsdata;
196 spin_unlock(&dentry->d_lock);
198 if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data)))
199 nfs_free_unlinkdata(data);