]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/nfs/delegation.c
NFSv4: Fix up another delegation related race
[linux-2.6-omap-h63xx.git] / fs / nfs / delegation.c
1 /*
2  * linux/fs/nfs/delegation.c
3  *
4  * Copyright (C) 2004 Trond Myklebust
5  *
6  * NFS file delegation management
7  *
8  */
9 #include <linux/completion.h>
10 #include <linux/kthread.h>
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/spinlock.h>
14
15 #include <linux/nfs4.h>
16 #include <linux/nfs_fs.h>
17 #include <linux/nfs_xdr.h>
18
19 #include "nfs4_fs.h"
20 #include "delegation.h"
21 #include "internal.h"
22
23 static void nfs_do_free_delegation(struct nfs_delegation *delegation)
24 {
25         kfree(delegation);
26 }
27
28 static void nfs_free_delegation_callback(struct rcu_head *head)
29 {
30         struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu);
31
32         nfs_do_free_delegation(delegation);
33 }
34
35 static void nfs_free_delegation(struct nfs_delegation *delegation)
36 {
37         struct rpc_cred *cred;
38
39         cred = rcu_dereference(delegation->cred);
40         rcu_assign_pointer(delegation->cred, NULL);
41         call_rcu(&delegation->rcu, nfs_free_delegation_callback);
42         if (cred)
43                 put_rpccred(cred);
44 }
45
46 static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
47 {
48         struct inode *inode = state->inode;
49         struct file_lock *fl;
50         int status;
51
52         for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
53                 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
54                         continue;
55                 if (nfs_file_open_context(fl->fl_file) != ctx)
56                         continue;
57                 status = nfs4_lock_delegation_recall(state, fl);
58                 if (status >= 0)
59                         continue;
60                 switch (status) {
61                         default:
62                                 printk(KERN_ERR "%s: unhandled error %d.\n",
63                                                 __func__, status);
64                         case -NFS4ERR_EXPIRED:
65                                 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
66                         case -NFS4ERR_STALE_CLIENTID:
67                                 nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs_client);
68                                 goto out_err;
69                 }
70         }
71         return 0;
72 out_err:
73         return status;
74 }
75
76 static void nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
77 {
78         struct nfs_inode *nfsi = NFS_I(inode);
79         struct nfs_open_context *ctx;
80         struct nfs4_state *state;
81         int err;
82
83 again:
84         spin_lock(&inode->i_lock);
85         list_for_each_entry(ctx, &nfsi->open_files, list) {
86                 state = ctx->state;
87                 if (state == NULL)
88                         continue;
89                 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
90                         continue;
91                 if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
92                         continue;
93                 get_nfs_open_context(ctx);
94                 spin_unlock(&inode->i_lock);
95                 err = nfs4_open_delegation_recall(ctx, state, stateid);
96                 if (err >= 0)
97                         err = nfs_delegation_claim_locks(ctx, state);
98                 put_nfs_open_context(ctx);
99                 if (err != 0)
100                         return;
101                 goto again;
102         }
103         spin_unlock(&inode->i_lock);
104 }
105
106 /*
107  * Set up a delegation on an inode
108  */
109 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
110 {
111         struct nfs_delegation *delegation = NFS_I(inode)->delegation;
112         struct rpc_cred *oldcred;
113
114         if (delegation == NULL)
115                 return;
116         memcpy(delegation->stateid.data, res->delegation.data,
117                         sizeof(delegation->stateid.data));
118         delegation->type = res->delegation_type;
119         delegation->maxsize = res->maxsize;
120         oldcred = delegation->cred;
121         delegation->cred = get_rpccred(cred);
122         delegation->flags &= ~NFS_DELEGATION_NEED_RECLAIM;
123         NFS_I(inode)->delegation_state = delegation->type;
124         smp_wmb();
125         put_rpccred(oldcred);
126 }
127
128 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
129 {
130         int res = 0;
131
132         res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
133         nfs_free_delegation(delegation);
134         return res;
135 }
136
137 static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
138 {
139         struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
140
141         if (delegation == NULL)
142                 goto nomatch;
143         spin_lock(&delegation->lock);
144         if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
145                                 sizeof(delegation->stateid.data)) != 0)
146                 goto nomatch_unlock;
147         list_del_rcu(&delegation->super_list);
148         nfsi->delegation_state = 0;
149         rcu_assign_pointer(nfsi->delegation, NULL);
150         spin_unlock(&delegation->lock);
151         return delegation;
152 nomatch_unlock:
153         spin_unlock(&delegation->lock);
154 nomatch:
155         return NULL;
156 }
157
158 /*
159  * Set up a delegation on an inode
160  */
161 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
162 {
163         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
164         struct nfs_inode *nfsi = NFS_I(inode);
165         struct nfs_delegation *delegation;
166         struct nfs_delegation *freeme = NULL;
167         int status = 0;
168
169         delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
170         if (delegation == NULL)
171                 return -ENOMEM;
172         memcpy(delegation->stateid.data, res->delegation.data,
173                         sizeof(delegation->stateid.data));
174         delegation->type = res->delegation_type;
175         delegation->maxsize = res->maxsize;
176         delegation->change_attr = nfsi->change_attr;
177         delegation->cred = get_rpccred(cred);
178         delegation->inode = inode;
179         spin_lock_init(&delegation->lock);
180
181         spin_lock(&clp->cl_lock);
182         if (rcu_dereference(nfsi->delegation) != NULL) {
183                 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
184                                         sizeof(delegation->stateid)) == 0 &&
185                                 delegation->type == nfsi->delegation->type) {
186                         goto out;
187                 }
188                 /*
189                  * Deal with broken servers that hand out two
190                  * delegations for the same file.
191                  */
192                 dfprintk(FILE, "%s: server %s handed out "
193                                 "a duplicate delegation!\n",
194                                 __func__, clp->cl_hostname);
195                 if (delegation->type <= nfsi->delegation->type) {
196                         freeme = delegation;
197                         delegation = NULL;
198                         goto out;
199                 }
200                 freeme = nfs_detach_delegation_locked(nfsi, NULL);
201         }
202         list_add_rcu(&delegation->super_list, &clp->cl_delegations);
203         nfsi->delegation_state = delegation->type;
204         rcu_assign_pointer(nfsi->delegation, delegation);
205         delegation = NULL;
206
207         /* Ensure we revalidate the attributes and page cache! */
208         spin_lock(&inode->i_lock);
209         nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
210         spin_unlock(&inode->i_lock);
211
212 out:
213         spin_unlock(&clp->cl_lock);
214         if (delegation != NULL)
215                 nfs_free_delegation(delegation);
216         if (freeme != NULL)
217                 nfs_do_return_delegation(inode, freeme, 0);
218         return status;
219 }
220
221 /* Sync all data to disk upon delegation return */
222 static void nfs_msync_inode(struct inode *inode)
223 {
224         filemap_fdatawrite(inode->i_mapping);
225         nfs_wb_all(inode);
226         filemap_fdatawait(inode->i_mapping);
227 }
228
229 /*
230  * Basic procedure for returning a delegation to the server
231  */
232 static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
233 {
234         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
235         struct nfs_inode *nfsi = NFS_I(inode);
236
237         nfs_msync_inode(inode);
238         down_read(&clp->cl_sem);
239         /* Guard against new delegated open calls */
240         down_write(&nfsi->rwsem);
241         nfs_delegation_claim_opens(inode, &delegation->stateid);
242         up_write(&nfsi->rwsem);
243         up_read(&clp->cl_sem);
244         nfs_msync_inode(inode);
245
246         return nfs_do_return_delegation(inode, delegation, 1);
247 }
248
249 /*
250  * This function returns the delegation without reclaiming opens
251  * or protecting against delegation reclaims.
252  * It is therefore really only safe to be called from
253  * nfs4_clear_inode()
254  */
255 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
256 {
257         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
258         struct nfs_inode *nfsi = NFS_I(inode);
259         struct nfs_delegation *delegation;
260
261         if (rcu_dereference(nfsi->delegation) != NULL) {
262                 spin_lock(&clp->cl_lock);
263                 delegation = nfs_detach_delegation_locked(nfsi, NULL);
264                 spin_unlock(&clp->cl_lock);
265                 if (delegation != NULL)
266                         nfs_do_return_delegation(inode, delegation, 0);
267         }
268 }
269
270 int nfs_inode_return_delegation(struct inode *inode)
271 {
272         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
273         struct nfs_inode *nfsi = NFS_I(inode);
274         struct nfs_delegation *delegation;
275         int err = 0;
276
277         if (rcu_dereference(nfsi->delegation) != NULL) {
278                 spin_lock(&clp->cl_lock);
279                 delegation = nfs_detach_delegation_locked(nfsi, NULL);
280                 spin_unlock(&clp->cl_lock);
281                 if (delegation != NULL)
282                         err = __nfs_inode_return_delegation(inode, delegation);
283         }
284         return err;
285 }
286
287 /*
288  * Return all delegations associated to a super block
289  */
290 void nfs_return_all_delegations(struct super_block *sb)
291 {
292         struct nfs_client *clp = NFS_SB(sb)->nfs_client;
293         struct nfs_delegation *delegation;
294         struct inode *inode;
295
296         if (clp == NULL)
297                 return;
298 restart:
299         rcu_read_lock();
300         list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
301                 if (delegation->inode->i_sb != sb)
302                         continue;
303                 inode = igrab(delegation->inode);
304                 if (inode == NULL)
305                         continue;
306                 spin_lock(&clp->cl_lock);
307                 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
308                 spin_unlock(&clp->cl_lock);
309                 rcu_read_unlock();
310                 if (delegation != NULL)
311                         __nfs_inode_return_delegation(inode, delegation);
312                 iput(inode);
313                 goto restart;
314         }
315         rcu_read_unlock();
316 }
317
318 static int nfs_do_expire_all_delegations(void *ptr)
319 {
320         struct nfs_client *clp = ptr;
321         struct nfs_delegation *delegation;
322         struct inode *inode;
323
324         allow_signal(SIGKILL);
325 restart:
326         if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
327                 goto out;
328         if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
329                 goto out;
330         rcu_read_lock();
331         list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
332                 inode = igrab(delegation->inode);
333                 if (inode == NULL)
334                         continue;
335                 spin_lock(&clp->cl_lock);
336                 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
337                 spin_unlock(&clp->cl_lock);
338                 rcu_read_unlock();
339                 if (delegation)
340                         __nfs_inode_return_delegation(inode, delegation);
341                 iput(inode);
342                 goto restart;
343         }
344         rcu_read_unlock();
345 out:
346         nfs_put_client(clp);
347         module_put_and_exit(0);
348 }
349
350 void nfs_expire_all_delegations(struct nfs_client *clp)
351 {
352         struct task_struct *task;
353
354         __module_get(THIS_MODULE);
355         atomic_inc(&clp->cl_count);
356         task = kthread_run(nfs_do_expire_all_delegations, clp,
357                                 "%s-delegreturn",
358                                 rpc_peeraddr2str(clp->cl_rpcclient,
359                                                         RPC_DISPLAY_ADDR));
360         if (!IS_ERR(task))
361                 return;
362         nfs_put_client(clp);
363         module_put(THIS_MODULE);
364 }
365
366 /*
367  * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
368  */
369 void nfs_handle_cb_pathdown(struct nfs_client *clp)
370 {
371         struct nfs_delegation *delegation;
372         struct inode *inode;
373
374         if (clp == NULL)
375                 return;
376 restart:
377         rcu_read_lock();
378         list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
379                 inode = igrab(delegation->inode);
380                 if (inode == NULL)
381                         continue;
382                 spin_lock(&clp->cl_lock);
383                 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
384                 spin_unlock(&clp->cl_lock);
385                 rcu_read_unlock();
386                 if (delegation != NULL)
387                         __nfs_inode_return_delegation(inode, delegation);
388                 iput(inode);
389                 goto restart;
390         }
391         rcu_read_unlock();
392 }
393
394 struct recall_threadargs {
395         struct inode *inode;
396         struct nfs_client *clp;
397         const nfs4_stateid *stateid;
398
399         struct completion started;
400         int result;
401 };
402
403 static int recall_thread(void *data)
404 {
405         struct recall_threadargs *args = (struct recall_threadargs *)data;
406         struct inode *inode = igrab(args->inode);
407         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
408         struct nfs_inode *nfsi = NFS_I(inode);
409         struct nfs_delegation *delegation;
410
411         daemonize("nfsv4-delegreturn");
412
413         nfs_msync_inode(inode);
414         down_read(&clp->cl_sem);
415         down_write(&nfsi->rwsem);
416         spin_lock(&clp->cl_lock);
417         delegation = nfs_detach_delegation_locked(nfsi, args->stateid);
418         if (delegation != NULL)
419                 args->result = 0;
420         else
421                 args->result = -ENOENT;
422         spin_unlock(&clp->cl_lock);
423         complete(&args->started);
424         nfs_delegation_claim_opens(inode, args->stateid);
425         up_write(&nfsi->rwsem);
426         up_read(&clp->cl_sem);
427         nfs_msync_inode(inode);
428
429         if (delegation != NULL)
430                 nfs_do_return_delegation(inode, delegation, 1);
431         iput(inode);
432         module_put_and_exit(0);
433 }
434
435 /*
436  * Asynchronous delegation recall!
437  */
438 int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
439 {
440         struct recall_threadargs data = {
441                 .inode = inode,
442                 .stateid = stateid,
443         };
444         int status;
445
446         init_completion(&data.started);
447         __module_get(THIS_MODULE);
448         status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
449         if (status < 0)
450                 goto out_module_put;
451         wait_for_completion(&data.started);
452         return data.result;
453 out_module_put:
454         module_put(THIS_MODULE);
455         return status;
456 }
457
458 /*
459  * Retrieve the inode associated with a delegation
460  */
461 struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
462 {
463         struct nfs_delegation *delegation;
464         struct inode *res = NULL;
465         rcu_read_lock();
466         list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
467                 if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
468                         res = igrab(delegation->inode);
469                         break;
470                 }
471         }
472         rcu_read_unlock();
473         return res;
474 }
475
476 /*
477  * Mark all delegations as needing to be reclaimed
478  */
479 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
480 {
481         struct nfs_delegation *delegation;
482         rcu_read_lock();
483         list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
484                 delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
485         rcu_read_unlock();
486 }
487
488 /*
489  * Reap all unclaimed delegations after reboot recovery is done
490  */
491 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
492 {
493         struct nfs_delegation *delegation;
494 restart:
495         rcu_read_lock();
496         list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
497                 if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
498                         continue;
499                 spin_lock(&clp->cl_lock);
500                 delegation = nfs_detach_delegation_locked(NFS_I(delegation->inode), NULL);
501                 spin_unlock(&clp->cl_lock);
502                 rcu_read_unlock();
503                 if (delegation != NULL)
504                         nfs_free_delegation(delegation);
505                 goto restart;
506         }
507         rcu_read_unlock();
508 }
509
510 int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
511 {
512         struct nfs_inode *nfsi = NFS_I(inode);
513         struct nfs_delegation *delegation;
514         int ret = 0;
515
516         rcu_read_lock();
517         delegation = rcu_dereference(nfsi->delegation);
518         if (delegation != NULL) {
519                 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
520                 ret = 1;
521         }
522         rcu_read_unlock();
523         return ret;
524 }