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 hlist_node list;
18 struct nfs_removeargs args;
19 struct nfs_removeres res;
21 struct rpc_cred *cred;
25 * nfs_free_unlinkdata - release data from a sillydelete operation.
26 * @data: pointer to unlink structure.
29 nfs_free_unlinkdata(struct nfs_unlinkdata *data)
32 put_rpccred(data->cred);
33 kfree(data->args.name.name);
37 #define NAME_ALLOC_LEN(len) ((len+16) & ~15)
39 * nfs_copy_dname - copy dentry name to data structure
40 * @dentry: pointer to dentry
41 * @data: nfs_unlinkdata
43 static int nfs_copy_dname(struct dentry *dentry, struct nfs_unlinkdata *data)
46 int len = dentry->d_name.len;
48 str = kmemdup(dentry->d_name.name, NAME_ALLOC_LEN(len), GFP_KERNEL);
51 data->args.name.len = len;
52 data->args.name.name = str;
56 static void nfs_free_dname(struct nfs_unlinkdata *data)
58 kfree(data->args.name.name);
59 data->args.name.name = NULL;
60 data->args.name.len = 0;
63 static void nfs_dec_sillycount(struct inode *dir)
65 struct nfs_inode *nfsi = NFS_I(dir);
66 if (atomic_dec_return(&nfsi->silly_count) == 1)
67 wake_up(&nfsi->waitqueue);
71 * nfs_async_unlink_init - Initialize the RPC info
72 * task: rpc_task of the sillydelete
74 static void nfs_async_unlink_init(struct rpc_task *task, void *calldata)
76 struct nfs_unlinkdata *data = calldata;
77 struct inode *dir = data->dir;
78 struct rpc_message msg = {
79 .rpc_argp = &data->args,
80 .rpc_resp = &data->res,
81 .rpc_cred = data->cred,
84 NFS_PROTO(dir)->unlink_setup(&msg, dir);
85 rpc_call_setup(task, &msg, 0);
89 * nfs_async_unlink_done - Sillydelete post-processing
90 * @task: rpc_task of the sillydelete
92 * Do the directory attribute update.
94 static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
96 struct nfs_unlinkdata *data = calldata;
97 struct inode *dir = data->dir;
99 if (!NFS_PROTO(dir)->unlink_done(task, dir))
100 rpc_restart_call(task);
104 * nfs_async_unlink_release - Release the sillydelete data.
105 * @task: rpc_task of the sillydelete
107 * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
108 * rpc_task would be freed too.
110 static void nfs_async_unlink_release(void *calldata)
112 struct nfs_unlinkdata *data = calldata;
114 nfs_dec_sillycount(data->dir);
115 nfs_free_unlinkdata(data);
118 static const struct rpc_call_ops nfs_unlink_ops = {
119 .rpc_call_prepare = nfs_async_unlink_init,
120 .rpc_call_done = nfs_async_unlink_done,
121 .rpc_release = nfs_async_unlink_release,
124 static int nfs_do_call_unlink(struct dentry *parent, struct inode *dir, struct nfs_unlinkdata *data)
126 struct rpc_task *task;
127 struct dentry *alias;
129 alias = d_lookup(parent, &data->args.name);
133 * Hey, we raced with lookup... See if we need to transfer
134 * the sillyrename information to the aliased dentry.
136 nfs_free_dname(data);
137 spin_lock(&alias->d_lock);
138 if (!(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
139 alias->d_fsdata = data;
140 alias->d_flags ^= DCACHE_NFSFS_RENAMED;
143 spin_unlock(&alias->d_lock);
144 nfs_dec_sillycount(dir);
148 data->dir = igrab(dir);
150 nfs_dec_sillycount(dir);
153 data->args.fh = NFS_FH(dir);
154 nfs_fattr_init(&data->res.dir_attr);
156 task = rpc_run_task(NFS_CLIENT(dir), RPC_TASK_ASYNC, &nfs_unlink_ops, data);
162 static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data)
164 struct dentry *parent;
169 parent = dget_parent(dentry);
172 dir = parent->d_inode;
173 if (nfs_copy_dname(dentry, data) == 0)
175 /* Non-exclusive lock protects against concurrent lookup() calls */
176 spin_lock(&dir->i_lock);
177 if (atomic_inc_not_zero(&NFS_I(dir)->silly_count) == 0) {
178 /* Deferred delete */
179 hlist_add_head(&data->list, &NFS_I(dir)->silly_list);
180 spin_unlock(&dir->i_lock);
184 spin_unlock(&dir->i_lock);
185 ret = nfs_do_call_unlink(parent, dir, data);
192 void nfs_block_sillyrename(struct dentry *dentry)
194 struct nfs_inode *nfsi = NFS_I(dentry->d_inode);
196 wait_event(nfsi->waitqueue, atomic_cmpxchg(&nfsi->silly_count, 1, 0) == 1);
199 void nfs_unblock_sillyrename(struct dentry *dentry)
201 struct inode *dir = dentry->d_inode;
202 struct nfs_inode *nfsi = NFS_I(dir);
203 struct nfs_unlinkdata *data;
205 atomic_inc(&nfsi->silly_count);
206 spin_lock(&dir->i_lock);
207 while (!hlist_empty(&nfsi->silly_list)) {
208 if (!atomic_inc_not_zero(&nfsi->silly_count))
210 data = hlist_entry(nfsi->silly_list.first, struct nfs_unlinkdata, list);
211 hlist_del(&data->list);
212 spin_unlock(&dir->i_lock);
213 if (nfs_do_call_unlink(dentry, dir, data) == 0)
214 nfs_free_unlinkdata(data);
215 spin_lock(&dir->i_lock);
217 spin_unlock(&dir->i_lock);
221 * nfs_async_unlink - asynchronous unlinking of a file
222 * @dir: parent directory of dentry
223 * @dentry: dentry to unlink
226 nfs_async_unlink(struct inode *dir, struct dentry *dentry)
228 struct nfs_unlinkdata *data;
229 int status = -ENOMEM;
231 data = kzalloc(sizeof(*data), GFP_KERNEL);
235 data->cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
236 if (IS_ERR(data->cred)) {
237 status = PTR_ERR(data->cred);
242 spin_lock(&dentry->d_lock);
243 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
245 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
246 dentry->d_fsdata = data;
247 spin_unlock(&dentry->d_lock);
250 spin_unlock(&dentry->d_lock);
251 put_rpccred(data->cred);
259 * nfs_complete_unlink - Initialize completion of the sillydelete
260 * @dentry: dentry to delete
263 * Since we're most likely to be called by dentry_iput(), we
264 * only use the dentry to find the sillydelete. We then copy the name
268 nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
270 struct nfs_unlinkdata *data = NULL;
272 spin_lock(&dentry->d_lock);
273 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
274 dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
275 data = dentry->d_fsdata;
277 spin_unlock(&dentry->d_lock);
279 if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data)))
280 nfs_free_unlinkdata(data);