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>
14 #include <linux/sched.h>
15 #include <linux/wait.h>
17 struct nfs_unlinkdata {
18 struct hlist_node list;
19 struct nfs_removeargs args;
20 struct nfs_removeres res;
22 struct rpc_cred *cred;
26 * nfs_free_unlinkdata - release data from a sillydelete operation.
27 * @data: pointer to unlink structure.
30 nfs_free_unlinkdata(struct nfs_unlinkdata *data)
33 put_rpccred(data->cred);
34 kfree(data->args.name.name);
38 #define NAME_ALLOC_LEN(len) ((len+16) & ~15)
40 * nfs_copy_dname - copy dentry name to data structure
41 * @dentry: pointer to dentry
42 * @data: nfs_unlinkdata
44 static int nfs_copy_dname(struct dentry *dentry, struct nfs_unlinkdata *data)
47 int len = dentry->d_name.len;
49 str = kmemdup(dentry->d_name.name, NAME_ALLOC_LEN(len), GFP_KERNEL);
52 data->args.name.len = len;
53 data->args.name.name = str;
57 static void nfs_free_dname(struct nfs_unlinkdata *data)
59 kfree(data->args.name.name);
60 data->args.name.name = NULL;
61 data->args.name.len = 0;
64 static void nfs_dec_sillycount(struct inode *dir)
66 struct nfs_inode *nfsi = NFS_I(dir);
67 if (atomic_dec_return(&nfsi->silly_count) == 1)
68 wake_up(&nfsi->waitqueue);
72 * nfs_async_unlink_init - Initialize the RPC info
73 * task: rpc_task of the sillydelete
75 static void nfs_async_unlink_init(struct rpc_task *task, void *calldata)
77 struct nfs_unlinkdata *data = calldata;
78 struct inode *dir = data->dir;
79 struct rpc_message msg = {
80 .rpc_argp = &data->args,
81 .rpc_resp = &data->res,
82 .rpc_cred = data->cred,
85 NFS_PROTO(dir)->unlink_setup(&msg, dir);
86 rpc_call_setup(task, &msg, 0);
90 * nfs_async_unlink_done - Sillydelete post-processing
91 * @task: rpc_task of the sillydelete
93 * Do the directory attribute update.
95 static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
97 struct nfs_unlinkdata *data = calldata;
98 struct inode *dir = data->dir;
100 if (!NFS_PROTO(dir)->unlink_done(task, dir))
101 rpc_restart_call(task);
105 * nfs_async_unlink_release - Release the sillydelete data.
106 * @task: rpc_task of the sillydelete
108 * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
109 * rpc_task would be freed too.
111 static void nfs_async_unlink_release(void *calldata)
113 struct nfs_unlinkdata *data = calldata;
115 nfs_dec_sillycount(data->dir);
116 nfs_free_unlinkdata(data);
119 static const struct rpc_call_ops nfs_unlink_ops = {
120 .rpc_call_prepare = nfs_async_unlink_init,
121 .rpc_call_done = nfs_async_unlink_done,
122 .rpc_release = nfs_async_unlink_release,
125 static int nfs_do_call_unlink(struct dentry *parent, struct inode *dir, struct nfs_unlinkdata *data)
127 struct rpc_task *task;
128 struct dentry *alias;
130 alias = d_lookup(parent, &data->args.name);
134 * Hey, we raced with lookup... See if we need to transfer
135 * the sillyrename information to the aliased dentry.
137 nfs_free_dname(data);
138 spin_lock(&alias->d_lock);
139 if (!(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
140 alias->d_fsdata = data;
141 alias->d_flags ^= DCACHE_NFSFS_RENAMED;
144 spin_unlock(&alias->d_lock);
145 nfs_dec_sillycount(dir);
149 data->dir = igrab(dir);
151 nfs_dec_sillycount(dir);
154 data->args.fh = NFS_FH(dir);
155 nfs_fattr_init(&data->res.dir_attr);
157 task = rpc_run_task(NFS_CLIENT(dir), RPC_TASK_ASYNC, &nfs_unlink_ops, data);
163 static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data)
165 struct dentry *parent;
170 parent = dget_parent(dentry);
173 dir = parent->d_inode;
174 if (nfs_copy_dname(dentry, data) != 0)
176 /* Non-exclusive lock protects against concurrent lookup() calls */
177 spin_lock(&dir->i_lock);
178 if (atomic_inc_not_zero(&NFS_I(dir)->silly_count) == 0) {
179 /* Deferred delete */
180 hlist_add_head(&data->list, &NFS_I(dir)->silly_list);
181 spin_unlock(&dir->i_lock);
185 spin_unlock(&dir->i_lock);
186 ret = nfs_do_call_unlink(parent, dir, data);
193 void nfs_block_sillyrename(struct dentry *dentry)
195 struct nfs_inode *nfsi = NFS_I(dentry->d_inode);
197 wait_event(nfsi->waitqueue, atomic_cmpxchg(&nfsi->silly_count, 1, 0) == 1);
200 void nfs_unblock_sillyrename(struct dentry *dentry)
202 struct inode *dir = dentry->d_inode;
203 struct nfs_inode *nfsi = NFS_I(dir);
204 struct nfs_unlinkdata *data;
206 atomic_inc(&nfsi->silly_count);
207 spin_lock(&dir->i_lock);
208 while (!hlist_empty(&nfsi->silly_list)) {
209 if (!atomic_inc_not_zero(&nfsi->silly_count))
211 data = hlist_entry(nfsi->silly_list.first, struct nfs_unlinkdata, list);
212 hlist_del(&data->list);
213 spin_unlock(&dir->i_lock);
214 if (nfs_do_call_unlink(dentry, dir, data) == 0)
215 nfs_free_unlinkdata(data);
216 spin_lock(&dir->i_lock);
218 spin_unlock(&dir->i_lock);
222 * nfs_async_unlink - asynchronous unlinking of a file
223 * @dir: parent directory of dentry
224 * @dentry: dentry to unlink
227 nfs_async_unlink(struct inode *dir, struct dentry *dentry)
229 struct nfs_unlinkdata *data;
230 int status = -ENOMEM;
232 data = kzalloc(sizeof(*data), GFP_KERNEL);
236 data->cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
237 if (IS_ERR(data->cred)) {
238 status = PTR_ERR(data->cred);
243 spin_lock(&dentry->d_lock);
244 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
246 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
247 dentry->d_fsdata = data;
248 spin_unlock(&dentry->d_lock);
251 spin_unlock(&dentry->d_lock);
252 put_rpccred(data->cred);
260 * nfs_complete_unlink - Initialize completion of the sillydelete
261 * @dentry: dentry to delete
264 * Since we're most likely to be called by dentry_iput(), we
265 * only use the dentry to find the sillydelete. We then copy the name
269 nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
271 struct nfs_unlinkdata *data = NULL;
273 spin_lock(&dentry->d_lock);
274 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
275 dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
276 data = dentry->d_fsdata;
278 spin_unlock(&dentry->d_lock);
280 if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data)))
281 nfs_free_unlinkdata(data);