NFS: Stop sillyname renames and unmounts from racing
[safe/jmp/linux-2.6] / fs / nfs / unlink.c
1 /*
2  *  linux/fs/nfs/unlink.c
3  *
4  * nfs sillydelete handling
5  *
6  */
7
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>
16
17 #include "internal.h"
18
19 struct nfs_unlinkdata {
20         struct hlist_node list;
21         struct nfs_removeargs args;
22         struct nfs_removeres res;
23         struct inode *dir;
24         struct rpc_cred *cred;
25 };
26
27 /**
28  * nfs_free_unlinkdata - release data from a sillydelete operation.
29  * @data: pointer to unlink structure.
30  */
31 static void
32 nfs_free_unlinkdata(struct nfs_unlinkdata *data)
33 {
34         iput(data->dir);
35         put_rpccred(data->cred);
36         kfree(data->args.name.name);
37         kfree(data);
38 }
39
40 #define NAME_ALLOC_LEN(len)     ((len+16) & ~15)
41 /**
42  * nfs_copy_dname - copy dentry name to data structure
43  * @dentry: pointer to dentry
44  * @data: nfs_unlinkdata
45  */
46 static int nfs_copy_dname(struct dentry *dentry, struct nfs_unlinkdata *data)
47 {
48         char            *str;
49         int             len = dentry->d_name.len;
50
51         str = kmemdup(dentry->d_name.name, NAME_ALLOC_LEN(len), GFP_KERNEL);
52         if (!str)
53                 return -ENOMEM;
54         data->args.name.len = len;
55         data->args.name.name = str;
56         return 0;
57 }
58
59 static void nfs_free_dname(struct nfs_unlinkdata *data)
60 {
61         kfree(data->args.name.name);
62         data->args.name.name = NULL;
63         data->args.name.len = 0;
64 }
65
66 static void nfs_dec_sillycount(struct inode *dir)
67 {
68         struct nfs_inode *nfsi = NFS_I(dir);
69         if (atomic_dec_return(&nfsi->silly_count) == 1)
70                 wake_up(&nfsi->waitqueue);
71 }
72
73 /**
74  * nfs_async_unlink_init - Initialize the RPC info
75  * task: rpc_task of the sillydelete
76  */
77 static void nfs_async_unlink_init(struct rpc_task *task, void *calldata)
78 {
79         struct nfs_unlinkdata *data = calldata;
80         struct inode *dir = data->dir;
81         struct rpc_message msg = {
82                 .rpc_argp = &data->args,
83                 .rpc_resp = &data->res,
84                 .rpc_cred = data->cred,
85         };
86
87         NFS_PROTO(dir)->unlink_setup(&msg, dir);
88         rpc_call_setup(task, &msg, 0);
89 }
90
91 /**
92  * nfs_async_unlink_done - Sillydelete post-processing
93  * @task: rpc_task of the sillydelete
94  *
95  * Do the directory attribute update.
96  */
97 static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
98 {
99         struct nfs_unlinkdata *data = calldata;
100         struct inode *dir = data->dir;
101
102         if (!NFS_PROTO(dir)->unlink_done(task, dir))
103                 rpc_restart_call(task);
104 }
105
106 /**
107  * nfs_async_unlink_release - Release the sillydelete data.
108  * @task: rpc_task of the sillydelete
109  *
110  * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
111  * rpc_task would be freed too.
112  */
113 static void nfs_async_unlink_release(void *calldata)
114 {
115         struct nfs_unlinkdata   *data = calldata;
116
117         nfs_dec_sillycount(data->dir);
118         nfs_sb_deactive(NFS_SERVER(data->dir));
119         nfs_free_unlinkdata(data);
120 }
121
122 static const struct rpc_call_ops nfs_unlink_ops = {
123         .rpc_call_prepare = nfs_async_unlink_init,
124         .rpc_call_done = nfs_async_unlink_done,
125         .rpc_release = nfs_async_unlink_release,
126 };
127
128 static int nfs_do_call_unlink(struct dentry *parent, struct inode *dir, struct nfs_unlinkdata *data)
129 {
130         struct rpc_task *task;
131         struct dentry *alias;
132
133         alias = d_lookup(parent, &data->args.name);
134         if (alias != NULL) {
135                 int ret = 0;
136
137                 /*
138                  * Hey, we raced with lookup... See if we need to transfer
139                  * the sillyrename information to the aliased dentry.
140                  */
141                 nfs_free_dname(data);
142                 spin_lock(&alias->d_lock);
143                 if (alias->d_inode != NULL &&
144                     !(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
145                         alias->d_fsdata = data;
146                         alias->d_flags |= DCACHE_NFSFS_RENAMED;
147                         ret = 1;
148                 }
149                 spin_unlock(&alias->d_lock);
150                 nfs_dec_sillycount(dir);
151                 dput(alias);
152                 return ret;
153         }
154         data->dir = igrab(dir);
155         if (!data->dir) {
156                 nfs_dec_sillycount(dir);
157                 return 0;
158         }
159         nfs_sb_active(NFS_SERVER(dir));
160         data->args.fh = NFS_FH(dir);
161         nfs_fattr_init(&data->res.dir_attr);
162
163         task = rpc_run_task(NFS_CLIENT(dir), RPC_TASK_ASYNC, &nfs_unlink_ops, data);
164         if (!IS_ERR(task))
165                 rpc_put_task(task);
166         return 1;
167 }
168
169 static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data)
170 {
171         struct dentry *parent;
172         struct inode *dir;
173         int ret = 0;
174
175
176         parent = dget_parent(dentry);
177         if (parent == NULL)
178                 goto out_free;
179         dir = parent->d_inode;
180         if (nfs_copy_dname(dentry, data) != 0)
181                 goto out_dput;
182         /* Non-exclusive lock protects against concurrent lookup() calls */
183         spin_lock(&dir->i_lock);
184         if (atomic_inc_not_zero(&NFS_I(dir)->silly_count) == 0) {
185                 /* Deferred delete */
186                 hlist_add_head(&data->list, &NFS_I(dir)->silly_list);
187                 spin_unlock(&dir->i_lock);
188                 ret = 1;
189                 goto out_dput;
190         }
191         spin_unlock(&dir->i_lock);
192         ret = nfs_do_call_unlink(parent, dir, data);
193 out_dput:
194         dput(parent);
195 out_free:
196         return ret;
197 }
198
199 void nfs_block_sillyrename(struct dentry *dentry)
200 {
201         struct nfs_inode *nfsi = NFS_I(dentry->d_inode);
202
203         wait_event(nfsi->waitqueue, atomic_cmpxchg(&nfsi->silly_count, 1, 0) == 1);
204 }
205
206 void nfs_unblock_sillyrename(struct dentry *dentry)
207 {
208         struct inode *dir = dentry->d_inode;
209         struct nfs_inode *nfsi = NFS_I(dir);
210         struct nfs_unlinkdata *data;
211
212         atomic_inc(&nfsi->silly_count);
213         spin_lock(&dir->i_lock);
214         while (!hlist_empty(&nfsi->silly_list)) {
215                 if (!atomic_inc_not_zero(&nfsi->silly_count))
216                         break;
217                 data = hlist_entry(nfsi->silly_list.first, struct nfs_unlinkdata, list);
218                 hlist_del(&data->list);
219                 spin_unlock(&dir->i_lock);
220                 if (nfs_do_call_unlink(dentry, dir, data) == 0)
221                         nfs_free_unlinkdata(data);
222                 spin_lock(&dir->i_lock);
223         }
224         spin_unlock(&dir->i_lock);
225 }
226
227 /**
228  * nfs_async_unlink - asynchronous unlinking of a file
229  * @dir: parent directory of dentry
230  * @dentry: dentry to unlink
231  */
232 int
233 nfs_async_unlink(struct inode *dir, struct dentry *dentry)
234 {
235         struct nfs_unlinkdata *data;
236         int status = -ENOMEM;
237
238         data = kzalloc(sizeof(*data), GFP_KERNEL);
239         if (data == NULL)
240                 goto out;
241
242         data->cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
243         if (IS_ERR(data->cred)) {
244                 status = PTR_ERR(data->cred);
245                 goto out_free;
246         }
247
248         status = -EBUSY;
249         spin_lock(&dentry->d_lock);
250         if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
251                 goto out_unlock;
252         dentry->d_flags |= DCACHE_NFSFS_RENAMED;
253         dentry->d_fsdata = data;
254         spin_unlock(&dentry->d_lock);
255         return 0;
256 out_unlock:
257         spin_unlock(&dentry->d_lock);
258         put_rpccred(data->cred);
259 out_free:
260         kfree(data);
261 out:
262         return status;
263 }
264
265 /**
266  * nfs_complete_unlink - Initialize completion of the sillydelete
267  * @dentry: dentry to delete
268  * @inode: inode
269  *
270  * Since we're most likely to be called by dentry_iput(), we
271  * only use the dentry to find the sillydelete. We then copy the name
272  * into the qstr.
273  */
274 void
275 nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
276 {
277         struct nfs_unlinkdata   *data = NULL;
278
279         spin_lock(&dentry->d_lock);
280         if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
281                 dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
282                 data = dentry->d_fsdata;
283         }
284         spin_unlock(&dentry->d_lock);
285
286         if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data)))
287                 nfs_free_unlinkdata(data);
288 }