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