Merge branch 'for-upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/bluetoot...
[deliverable/linux.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/spinlock.h>
15
16 #include <linux/nfs4.h>
17 #include <linux/nfs_fs.h>
18 #include <linux/nfs_xdr.h>
19
20 #include "nfs4_fs.h"
21 #include "delegation.h"
22 #include "internal.h"
23 #include "nfs4trace.h"
24
25 static void nfs_free_delegation(struct nfs_delegation *delegation)
26 {
27 if (delegation->cred) {
28 put_rpccred(delegation->cred);
29 delegation->cred = NULL;
30 }
31 kfree_rcu(delegation, rcu);
32 }
33
34 /**
35 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
36 * @delegation: delegation to process
37 *
38 */
39 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
40 {
41 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
42 }
43
44 static int
45 nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
46 {
47 struct nfs_delegation *delegation;
48 int ret = 0;
49
50 flags &= FMODE_READ|FMODE_WRITE;
51 rcu_read_lock();
52 delegation = rcu_dereference(NFS_I(inode)->delegation);
53 if (delegation != NULL && (delegation->type & flags) == flags &&
54 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
55 if (mark)
56 nfs_mark_delegation_referenced(delegation);
57 ret = 1;
58 }
59 rcu_read_unlock();
60 return ret;
61 }
62 /**
63 * nfs_have_delegation - check if inode has a delegation, mark it
64 * NFS_DELEGATION_REFERENCED if there is one.
65 * @inode: inode to check
66 * @flags: delegation types to check for
67 *
68 * Returns one if inode has the indicated delegation, otherwise zero.
69 */
70 int nfs4_have_delegation(struct inode *inode, fmode_t flags)
71 {
72 return nfs4_do_check_delegation(inode, flags, true);
73 }
74
75 /*
76 * nfs4_check_delegation - check if inode has a delegation, do not mark
77 * NFS_DELEGATION_REFERENCED if it has one.
78 */
79 int nfs4_check_delegation(struct inode *inode, fmode_t flags)
80 {
81 return nfs4_do_check_delegation(inode, flags, false);
82 }
83
84 static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
85 {
86 struct inode *inode = state->inode;
87 struct file_lock *fl;
88 int status = 0;
89
90 if (inode->i_flock == NULL)
91 goto out;
92
93 /* Protect inode->i_flock using the i_lock */
94 spin_lock(&inode->i_lock);
95 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
96 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
97 continue;
98 if (nfs_file_open_context(fl->fl_file) != ctx)
99 continue;
100 spin_unlock(&inode->i_lock);
101 status = nfs4_lock_delegation_recall(fl, state, stateid);
102 if (status < 0)
103 goto out;
104 spin_lock(&inode->i_lock);
105 }
106 spin_unlock(&inode->i_lock);
107 out:
108 return status;
109 }
110
111 static int nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
112 {
113 struct nfs_inode *nfsi = NFS_I(inode);
114 struct nfs_open_context *ctx;
115 struct nfs4_state_owner *sp;
116 struct nfs4_state *state;
117 unsigned int seq;
118 int err;
119
120 again:
121 spin_lock(&inode->i_lock);
122 list_for_each_entry(ctx, &nfsi->open_files, list) {
123 state = ctx->state;
124 if (state == NULL)
125 continue;
126 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
127 continue;
128 if (!nfs4_valid_open_stateid(state))
129 continue;
130 if (!nfs4_stateid_match(&state->stateid, stateid))
131 continue;
132 get_nfs_open_context(ctx);
133 spin_unlock(&inode->i_lock);
134 sp = state->owner;
135 /* Block nfs4_proc_unlck */
136 mutex_lock(&sp->so_delegreturn_mutex);
137 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
138 err = nfs4_open_delegation_recall(ctx, state, stateid);
139 if (!err)
140 err = nfs_delegation_claim_locks(ctx, state, stateid);
141 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
142 err = -EAGAIN;
143 mutex_unlock(&sp->so_delegreturn_mutex);
144 put_nfs_open_context(ctx);
145 if (err != 0)
146 return err;
147 goto again;
148 }
149 spin_unlock(&inode->i_lock);
150 return 0;
151 }
152
153 /**
154 * nfs_inode_reclaim_delegation - process a delegation reclaim request
155 * @inode: inode to process
156 * @cred: credential to use for request
157 * @res: new delegation state from server
158 *
159 */
160 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred,
161 struct nfs_openres *res)
162 {
163 struct nfs_delegation *delegation;
164 struct rpc_cred *oldcred = NULL;
165
166 rcu_read_lock();
167 delegation = rcu_dereference(NFS_I(inode)->delegation);
168 if (delegation != NULL) {
169 spin_lock(&delegation->lock);
170 if (delegation->inode != NULL) {
171 nfs4_stateid_copy(&delegation->stateid, &res->delegation);
172 delegation->type = res->delegation_type;
173 delegation->maxsize = res->maxsize;
174 oldcred = delegation->cred;
175 delegation->cred = get_rpccred(cred);
176 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
177 &delegation->flags);
178 NFS_I(inode)->delegation_state = delegation->type;
179 spin_unlock(&delegation->lock);
180 put_rpccred(oldcred);
181 rcu_read_unlock();
182 trace_nfs4_reclaim_delegation(inode, res->delegation_type);
183 } else {
184 /* We appear to have raced with a delegation return. */
185 spin_unlock(&delegation->lock);
186 rcu_read_unlock();
187 nfs_inode_set_delegation(inode, cred, res);
188 }
189 } else {
190 rcu_read_unlock();
191 }
192 }
193
194 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
195 {
196 int res = 0;
197
198 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
199 res = nfs4_proc_delegreturn(inode,
200 delegation->cred,
201 &delegation->stateid,
202 issync);
203 nfs_free_delegation(delegation);
204 return res;
205 }
206
207 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
208 {
209 struct inode *inode = NULL;
210
211 spin_lock(&delegation->lock);
212 if (delegation->inode != NULL)
213 inode = igrab(delegation->inode);
214 spin_unlock(&delegation->lock);
215 return inode;
216 }
217
218 static struct nfs_delegation *
219 nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
220 {
221 struct nfs_delegation *ret = NULL;
222 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
223
224 if (delegation == NULL)
225 goto out;
226 spin_lock(&delegation->lock);
227 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
228 ret = delegation;
229 spin_unlock(&delegation->lock);
230 out:
231 return ret;
232 }
233
234 static struct nfs_delegation *
235 nfs_start_delegation_return(struct nfs_inode *nfsi)
236 {
237 struct nfs_delegation *delegation;
238
239 rcu_read_lock();
240 delegation = nfs_start_delegation_return_locked(nfsi);
241 rcu_read_unlock();
242 return delegation;
243 }
244
245 static void
246 nfs_abort_delegation_return(struct nfs_delegation *delegation,
247 struct nfs_client *clp)
248 {
249
250 spin_lock(&delegation->lock);
251 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
252 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
253 spin_unlock(&delegation->lock);
254 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
255 }
256
257 static struct nfs_delegation *
258 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
259 struct nfs_delegation *delegation,
260 struct nfs_client *clp)
261 {
262 struct nfs_delegation *deleg_cur =
263 rcu_dereference_protected(nfsi->delegation,
264 lockdep_is_held(&clp->cl_lock));
265
266 if (deleg_cur == NULL || delegation != deleg_cur)
267 return NULL;
268
269 spin_lock(&delegation->lock);
270 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
271 list_del_rcu(&delegation->super_list);
272 delegation->inode = NULL;
273 nfsi->delegation_state = 0;
274 rcu_assign_pointer(nfsi->delegation, NULL);
275 spin_unlock(&delegation->lock);
276 return delegation;
277 }
278
279 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
280 struct nfs_delegation *delegation,
281 struct nfs_server *server)
282 {
283 struct nfs_client *clp = server->nfs_client;
284
285 spin_lock(&clp->cl_lock);
286 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
287 spin_unlock(&clp->cl_lock);
288 return delegation;
289 }
290
291 static struct nfs_delegation *
292 nfs_inode_detach_delegation(struct inode *inode)
293 {
294 struct nfs_inode *nfsi = NFS_I(inode);
295 struct nfs_server *server = NFS_SERVER(inode);
296 struct nfs_delegation *delegation;
297
298 delegation = nfs_start_delegation_return(nfsi);
299 if (delegation == NULL)
300 return NULL;
301 return nfs_detach_delegation(nfsi, delegation, server);
302 }
303
304 /**
305 * nfs_inode_set_delegation - set up a delegation on an inode
306 * @inode: inode to which delegation applies
307 * @cred: cred to use for subsequent delegation processing
308 * @res: new delegation state from server
309 *
310 * Returns zero on success, or a negative errno value.
311 */
312 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
313 {
314 struct nfs_server *server = NFS_SERVER(inode);
315 struct nfs_client *clp = server->nfs_client;
316 struct nfs_inode *nfsi = NFS_I(inode);
317 struct nfs_delegation *delegation, *old_delegation;
318 struct nfs_delegation *freeme = NULL;
319 int status = 0;
320
321 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
322 if (delegation == NULL)
323 return -ENOMEM;
324 nfs4_stateid_copy(&delegation->stateid, &res->delegation);
325 delegation->type = res->delegation_type;
326 delegation->maxsize = res->maxsize;
327 delegation->change_attr = inode->i_version;
328 delegation->cred = get_rpccred(cred);
329 delegation->inode = inode;
330 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
331 spin_lock_init(&delegation->lock);
332
333 spin_lock(&clp->cl_lock);
334 old_delegation = rcu_dereference_protected(nfsi->delegation,
335 lockdep_is_held(&clp->cl_lock));
336 if (old_delegation != NULL) {
337 if (nfs4_stateid_match(&delegation->stateid,
338 &old_delegation->stateid) &&
339 delegation->type == old_delegation->type) {
340 goto out;
341 }
342 /*
343 * Deal with broken servers that hand out two
344 * delegations for the same file.
345 * Allow for upgrades to a WRITE delegation, but
346 * nothing else.
347 */
348 dfprintk(FILE, "%s: server %s handed out "
349 "a duplicate delegation!\n",
350 __func__, clp->cl_hostname);
351 if (delegation->type == old_delegation->type ||
352 !(delegation->type & FMODE_WRITE)) {
353 freeme = delegation;
354 delegation = NULL;
355 goto out;
356 }
357 freeme = nfs_detach_delegation_locked(nfsi,
358 old_delegation, clp);
359 if (freeme == NULL)
360 goto out;
361 }
362 list_add_rcu(&delegation->super_list, &server->delegations);
363 nfsi->delegation_state = delegation->type;
364 rcu_assign_pointer(nfsi->delegation, delegation);
365 delegation = NULL;
366
367 /* Ensure we revalidate the attributes and page cache! */
368 spin_lock(&inode->i_lock);
369 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
370 spin_unlock(&inode->i_lock);
371 trace_nfs4_set_delegation(inode, res->delegation_type);
372
373 out:
374 spin_unlock(&clp->cl_lock);
375 if (delegation != NULL)
376 nfs_free_delegation(delegation);
377 if (freeme != NULL)
378 nfs_do_return_delegation(inode, freeme, 0);
379 return status;
380 }
381
382 /*
383 * Basic procedure for returning a delegation to the server
384 */
385 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
386 {
387 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
388 struct nfs_inode *nfsi = NFS_I(inode);
389 int err = 0;
390
391 if (delegation == NULL)
392 return 0;
393 do {
394 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
395 break;
396 err = nfs_delegation_claim_opens(inode, &delegation->stateid);
397 if (!issync || err != -EAGAIN)
398 break;
399 /*
400 * Guard against state recovery
401 */
402 err = nfs4_wait_clnt_recover(clp);
403 } while (err == 0);
404
405 if (err) {
406 nfs_abort_delegation_return(delegation, clp);
407 goto out;
408 }
409 if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
410 goto out;
411
412 err = nfs_do_return_delegation(inode, delegation, issync);
413 out:
414 return err;
415 }
416
417 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
418 {
419 bool ret = false;
420
421 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
422 ret = true;
423 if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
424 struct inode *inode;
425
426 spin_lock(&delegation->lock);
427 inode = delegation->inode;
428 if (inode && list_empty(&NFS_I(inode)->open_files))
429 ret = true;
430 spin_unlock(&delegation->lock);
431 }
432 return ret;
433 }
434
435 /**
436 * nfs_client_return_marked_delegations - return previously marked delegations
437 * @clp: nfs_client to process
438 *
439 * Note that this function is designed to be called by the state
440 * manager thread. For this reason, it cannot flush the dirty data,
441 * since that could deadlock in case of a state recovery error.
442 *
443 * Returns zero on success, or a negative errno value.
444 */
445 int nfs_client_return_marked_delegations(struct nfs_client *clp)
446 {
447 struct nfs_delegation *delegation;
448 struct nfs_server *server;
449 struct inode *inode;
450 int err = 0;
451
452 restart:
453 rcu_read_lock();
454 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
455 list_for_each_entry_rcu(delegation, &server->delegations,
456 super_list) {
457 if (!nfs_delegation_need_return(delegation))
458 continue;
459 inode = nfs_delegation_grab_inode(delegation);
460 if (inode == NULL)
461 continue;
462 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
463 rcu_read_unlock();
464
465 err = nfs_end_delegation_return(inode, delegation, 0);
466 iput(inode);
467 if (!err)
468 goto restart;
469 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
470 return err;
471 }
472 }
473 rcu_read_unlock();
474 return 0;
475 }
476
477 /**
478 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
479 * @inode: inode to process
480 *
481 * Does not protect against delegation reclaims, therefore really only safe
482 * to be called from nfs4_clear_inode().
483 */
484 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
485 {
486 struct nfs_delegation *delegation;
487
488 delegation = nfs_inode_detach_delegation(inode);
489 if (delegation != NULL)
490 nfs_do_return_delegation(inode, delegation, 0);
491 }
492
493 /**
494 * nfs_inode_return_delegation - synchronously return a delegation
495 * @inode: inode to process
496 *
497 * This routine will always flush any dirty data to disk on the
498 * assumption that if we need to return the delegation, then
499 * we should stop caching.
500 *
501 * Returns zero on success, or a negative errno value.
502 */
503 int nfs4_inode_return_delegation(struct inode *inode)
504 {
505 struct nfs_inode *nfsi = NFS_I(inode);
506 struct nfs_delegation *delegation;
507 int err = 0;
508
509 nfs_wb_all(inode);
510 delegation = nfs_start_delegation_return(nfsi);
511 if (delegation != NULL)
512 err = nfs_end_delegation_return(inode, delegation, 1);
513 return err;
514 }
515
516 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
517 struct nfs_delegation *delegation)
518 {
519 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
520 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
521 }
522
523 static void nfs_mark_return_delegation(struct nfs_server *server,
524 struct nfs_delegation *delegation)
525 {
526 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
527 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
528 }
529
530 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
531 {
532 struct nfs_delegation *delegation;
533 bool ret = false;
534
535 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
536 nfs_mark_return_delegation(server, delegation);
537 ret = true;
538 }
539 return ret;
540 }
541
542 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
543 {
544 struct nfs_server *server;
545
546 rcu_read_lock();
547 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
548 nfs_server_mark_return_all_delegations(server);
549 rcu_read_unlock();
550 }
551
552 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
553 {
554 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
555 nfs4_schedule_state_manager(clp);
556 }
557
558 /**
559 * nfs_expire_all_delegations
560 * @clp: client to process
561 *
562 */
563 void nfs_expire_all_delegations(struct nfs_client *clp)
564 {
565 nfs_client_mark_return_all_delegations(clp);
566 nfs_delegation_run_state_manager(clp);
567 }
568
569 /**
570 * nfs_super_return_all_delegations - return delegations for one superblock
571 * @sb: sb to process
572 *
573 */
574 void nfs_server_return_all_delegations(struct nfs_server *server)
575 {
576 struct nfs_client *clp = server->nfs_client;
577 bool need_wait;
578
579 if (clp == NULL)
580 return;
581
582 rcu_read_lock();
583 need_wait = nfs_server_mark_return_all_delegations(server);
584 rcu_read_unlock();
585
586 if (need_wait) {
587 nfs4_schedule_state_manager(clp);
588 nfs4_wait_clnt_recover(clp);
589 }
590 }
591
592 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
593 fmode_t flags)
594 {
595 struct nfs_delegation *delegation;
596
597 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
598 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
599 continue;
600 if (delegation->type & flags)
601 nfs_mark_return_if_closed_delegation(server, delegation);
602 }
603 }
604
605 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
606 fmode_t flags)
607 {
608 struct nfs_server *server;
609
610 rcu_read_lock();
611 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
612 nfs_mark_return_unused_delegation_types(server, flags);
613 rcu_read_unlock();
614 }
615
616 static void nfs_revoke_delegation(struct inode *inode)
617 {
618 struct nfs_delegation *delegation;
619 rcu_read_lock();
620 delegation = rcu_dereference(NFS_I(inode)->delegation);
621 if (delegation != NULL) {
622 set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
623 nfs_mark_return_delegation(NFS_SERVER(inode), delegation);
624 }
625 rcu_read_unlock();
626 }
627
628 void nfs_remove_bad_delegation(struct inode *inode)
629 {
630 struct nfs_delegation *delegation;
631
632 nfs_revoke_delegation(inode);
633 delegation = nfs_inode_detach_delegation(inode);
634 if (delegation) {
635 nfs_inode_find_state_and_recover(inode, &delegation->stateid);
636 nfs_free_delegation(delegation);
637 }
638 }
639 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
640
641 /**
642 * nfs_expire_unused_delegation_types
643 * @clp: client to process
644 * @flags: delegation types to expire
645 *
646 */
647 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
648 {
649 nfs_client_mark_return_unused_delegation_types(clp, flags);
650 nfs_delegation_run_state_manager(clp);
651 }
652
653 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
654 {
655 struct nfs_delegation *delegation;
656
657 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
658 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
659 continue;
660 nfs_mark_return_if_closed_delegation(server, delegation);
661 }
662 }
663
664 /**
665 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
666 * @clp: nfs_client to process
667 *
668 */
669 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
670 {
671 struct nfs_server *server;
672
673 rcu_read_lock();
674 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
675 nfs_mark_return_unreferenced_delegations(server);
676 rcu_read_unlock();
677
678 nfs_delegation_run_state_manager(clp);
679 }
680
681 /**
682 * nfs_async_inode_return_delegation - asynchronously return a delegation
683 * @inode: inode to process
684 * @stateid: state ID information
685 *
686 * Returns zero on success, or a negative errno value.
687 */
688 int nfs_async_inode_return_delegation(struct inode *inode,
689 const nfs4_stateid *stateid)
690 {
691 struct nfs_server *server = NFS_SERVER(inode);
692 struct nfs_client *clp = server->nfs_client;
693 struct nfs_delegation *delegation;
694
695 filemap_flush(inode->i_mapping);
696
697 rcu_read_lock();
698 delegation = rcu_dereference(NFS_I(inode)->delegation);
699 if (delegation == NULL)
700 goto out_enoent;
701
702 if (!clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
703 goto out_enoent;
704 nfs_mark_return_delegation(server, delegation);
705 rcu_read_unlock();
706
707 nfs_delegation_run_state_manager(clp);
708 return 0;
709 out_enoent:
710 rcu_read_unlock();
711 return -ENOENT;
712 }
713
714 static struct inode *
715 nfs_delegation_find_inode_server(struct nfs_server *server,
716 const struct nfs_fh *fhandle)
717 {
718 struct nfs_delegation *delegation;
719 struct inode *res = NULL;
720
721 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
722 spin_lock(&delegation->lock);
723 if (delegation->inode != NULL &&
724 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
725 res = igrab(delegation->inode);
726 }
727 spin_unlock(&delegation->lock);
728 if (res != NULL)
729 break;
730 }
731 return res;
732 }
733
734 /**
735 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
736 * @clp: client state handle
737 * @fhandle: filehandle from a delegation recall
738 *
739 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
740 * cannot be found.
741 */
742 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
743 const struct nfs_fh *fhandle)
744 {
745 struct nfs_server *server;
746 struct inode *res = NULL;
747
748 rcu_read_lock();
749 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
750 res = nfs_delegation_find_inode_server(server, fhandle);
751 if (res != NULL)
752 break;
753 }
754 rcu_read_unlock();
755 return res;
756 }
757
758 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
759 {
760 struct nfs_delegation *delegation;
761
762 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
763 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
764 }
765
766 /**
767 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
768 * @clp: nfs_client to process
769 *
770 */
771 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
772 {
773 struct nfs_server *server;
774
775 rcu_read_lock();
776 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
777 nfs_delegation_mark_reclaim_server(server);
778 rcu_read_unlock();
779 }
780
781 /**
782 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
783 * @clp: nfs_client to process
784 *
785 */
786 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
787 {
788 struct nfs_delegation *delegation;
789 struct nfs_server *server;
790 struct inode *inode;
791
792 restart:
793 rcu_read_lock();
794 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
795 list_for_each_entry_rcu(delegation, &server->delegations,
796 super_list) {
797 if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
798 &delegation->flags) == 0)
799 continue;
800 inode = nfs_delegation_grab_inode(delegation);
801 if (inode == NULL)
802 continue;
803 delegation = nfs_detach_delegation(NFS_I(inode),
804 delegation, server);
805 rcu_read_unlock();
806
807 if (delegation != NULL)
808 nfs_free_delegation(delegation);
809 iput(inode);
810 goto restart;
811 }
812 }
813 rcu_read_unlock();
814 }
815
816 /**
817 * nfs_delegations_present - check for existence of delegations
818 * @clp: client state handle
819 *
820 * Returns one if there are any nfs_delegation structures attached
821 * to this nfs_client.
822 */
823 int nfs_delegations_present(struct nfs_client *clp)
824 {
825 struct nfs_server *server;
826 int ret = 0;
827
828 rcu_read_lock();
829 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
830 if (!list_empty(&server->delegations)) {
831 ret = 1;
832 break;
833 }
834 rcu_read_unlock();
835 return ret;
836 }
837
838 /**
839 * nfs4_copy_delegation_stateid - Copy inode's state ID information
840 * @dst: stateid data structure to fill in
841 * @inode: inode to check
842 * @flags: delegation type requirement
843 *
844 * Returns "true" and fills in "dst->data" * if inode had a delegation,
845 * otherwise "false" is returned.
846 */
847 bool nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode,
848 fmode_t flags)
849 {
850 struct nfs_inode *nfsi = NFS_I(inode);
851 struct nfs_delegation *delegation;
852 bool ret;
853
854 flags &= FMODE_READ|FMODE_WRITE;
855 rcu_read_lock();
856 delegation = rcu_dereference(nfsi->delegation);
857 ret = (delegation != NULL && (delegation->type & flags) == flags);
858 if (ret) {
859 nfs4_stateid_copy(dst, &delegation->stateid);
860 nfs_mark_delegation_referenced(delegation);
861 }
862 rcu_read_unlock();
863 return ret;
864 }
This page took 0.050127 seconds and 5 git commands to generate.