1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/fs/nfs/delegation.c
4 *
5 * Copyright (C) 2004 Trond Myklebust
6 *
7 * NFS file delegation management
8 *
9 */
10 #include <linux/completion.h>
11 #include <linux/kthread.h>
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/iversion.h>
17
18 #include <linux/nfs4.h>
19 #include <linux/nfs_fs.h>
20 #include <linux/nfs_xdr.h>
21
22 #include "nfs4_fs.h"
23 #include "nfs4session.h"
24 #include "delegation.h"
25 #include "internal.h"
26 #include "nfs4trace.h"
27
28 #define NFS_DEFAULT_DELEGATION_WATERMARK (5000U)
29
30 static atomic_long_t nfs_active_delegations;
31 static unsigned nfs_delegation_watermark = NFS_DEFAULT_DELEGATION_WATERMARK;
32
__nfs_free_delegation(struct nfs_delegation * delegation)33 static void __nfs_free_delegation(struct nfs_delegation *delegation)
34 {
35 put_cred(delegation->cred);
36 delegation->cred = NULL;
37 kfree_rcu(delegation, rcu);
38 }
39
nfs_mark_delegation_revoked(struct nfs_delegation * delegation)40 static void nfs_mark_delegation_revoked(struct nfs_delegation *delegation)
41 {
42 if (!test_and_set_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
43 delegation->stateid.type = NFS4_INVALID_STATEID_TYPE;
44 atomic_long_dec(&nfs_active_delegations);
45 if (!test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
46 nfs_clear_verifier_delegated(delegation->inode);
47 }
48 }
49
nfs_get_delegation(struct nfs_delegation * delegation)50 static struct nfs_delegation *nfs_get_delegation(struct nfs_delegation *delegation)
51 {
52 refcount_inc(&delegation->refcount);
53 return delegation;
54 }
55
nfs_put_delegation(struct nfs_delegation * delegation)56 static void nfs_put_delegation(struct nfs_delegation *delegation)
57 {
58 if (refcount_dec_and_test(&delegation->refcount))
59 __nfs_free_delegation(delegation);
60 }
61
nfs_free_delegation(struct nfs_delegation * delegation)62 static void nfs_free_delegation(struct nfs_delegation *delegation)
63 {
64 nfs_mark_delegation_revoked(delegation);
65 nfs_put_delegation(delegation);
66 }
67
68 /**
69 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
70 * @delegation: delegation to process
71 *
72 */
nfs_mark_delegation_referenced(struct nfs_delegation * delegation)73 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
74 {
75 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
76 }
77
nfs_mark_return_delegation(struct nfs_server * server,struct nfs_delegation * delegation)78 static void nfs_mark_return_delegation(struct nfs_server *server,
79 struct nfs_delegation *delegation)
80 {
81 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
82 set_bit(NFS4SERV_DELEGRETURN, &server->delegation_flags);
83 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
84 }
85
nfs4_is_valid_delegation(const struct nfs_delegation * delegation,fmode_t type)86 static bool nfs4_is_valid_delegation(const struct nfs_delegation *delegation,
87 fmode_t type)
88 {
89 if (delegation != NULL && (delegation->type & type) == type &&
90 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
91 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
92 return true;
93 return false;
94 }
95
nfs4_get_valid_delegation(const struct inode * inode)96 struct nfs_delegation *nfs4_get_valid_delegation(const struct inode *inode)
97 {
98 struct nfs_delegation *delegation;
99
100 delegation = rcu_dereference(NFS_I(inode)->delegation);
101 if (nfs4_is_valid_delegation(delegation, 0))
102 return delegation;
103 return NULL;
104 }
105
nfs4_do_check_delegation(struct inode * inode,fmode_t type,int flags,bool mark)106 static int nfs4_do_check_delegation(struct inode *inode, fmode_t type,
107 int flags, bool mark)
108 {
109 struct nfs_delegation *delegation;
110 int ret = 0;
111
112 type &= FMODE_READ|FMODE_WRITE;
113 rcu_read_lock();
114 delegation = rcu_dereference(NFS_I(inode)->delegation);
115 if (nfs4_is_valid_delegation(delegation, type)) {
116 if (mark)
117 nfs_mark_delegation_referenced(delegation);
118 ret = 1;
119 if ((flags & NFS_DELEGATION_FLAG_TIME) &&
120 !test_bit(NFS_DELEGATION_DELEGTIME, &delegation->flags))
121 ret = 0;
122 }
123 rcu_read_unlock();
124 return ret;
125 }
126 /**
127 * nfs4_have_delegation - check if inode has a delegation, mark it
128 * NFS_DELEGATION_REFERENCED if there is one.
129 * @inode: inode to check
130 * @type: delegation types to check for
131 * @flags: various modifiers
132 *
133 * Returns one if inode has the indicated delegation, otherwise zero.
134 */
nfs4_have_delegation(struct inode * inode,fmode_t type,int flags)135 int nfs4_have_delegation(struct inode *inode, fmode_t type, int flags)
136 {
137 return nfs4_do_check_delegation(inode, type, flags, true);
138 }
139
140 /*
141 * nfs4_check_delegation - check if inode has a delegation, do not mark
142 * NFS_DELEGATION_REFERENCED if it has one.
143 */
nfs4_check_delegation(struct inode * inode,fmode_t type)144 int nfs4_check_delegation(struct inode *inode, fmode_t type)
145 {
146 return nfs4_do_check_delegation(inode, type, 0, false);
147 }
148
nfs_delegation_claim_locks(struct nfs4_state * state,const nfs4_stateid * stateid)149 static int nfs_delegation_claim_locks(struct nfs4_state *state, const nfs4_stateid *stateid)
150 {
151 struct inode *inode = state->inode;
152 struct file_lock *fl;
153 struct file_lock_context *flctx = locks_inode_context(inode);
154 struct list_head *list;
155 int status = 0;
156
157 if (flctx == NULL)
158 goto out;
159
160 list = &flctx->flc_posix;
161 spin_lock(&flctx->flc_lock);
162 restart:
163 for_each_file_lock(fl, list) {
164 if (nfs_file_open_context(fl->c.flc_file)->state != state)
165 continue;
166 spin_unlock(&flctx->flc_lock);
167 status = nfs4_lock_delegation_recall(fl, state, stateid);
168 if (status < 0)
169 goto out;
170 spin_lock(&flctx->flc_lock);
171 }
172 if (list == &flctx->flc_posix) {
173 list = &flctx->flc_flock;
174 goto restart;
175 }
176 spin_unlock(&flctx->flc_lock);
177 out:
178 return status;
179 }
180
nfs_delegation_claim_opens(struct inode * inode,const nfs4_stateid * stateid,fmode_t type)181 static int nfs_delegation_claim_opens(struct inode *inode,
182 const nfs4_stateid *stateid, fmode_t type)
183 {
184 struct nfs_inode *nfsi = NFS_I(inode);
185 struct nfs_open_context *ctx;
186 struct nfs4_state_owner *sp;
187 struct nfs4_state *state;
188 int err;
189
190 again:
191 rcu_read_lock();
192 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
193 state = ctx->state;
194 if (state == NULL)
195 continue;
196 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
197 continue;
198 if (!nfs4_valid_open_stateid(state))
199 continue;
200 if (!nfs4_stateid_match(&state->stateid, stateid))
201 continue;
202 if (!get_nfs_open_context(ctx))
203 continue;
204 rcu_read_unlock();
205 sp = state->owner;
206 /* Block nfs4_proc_unlck */
207 mutex_lock(&sp->so_delegreturn_mutex);
208 err = nfs4_open_delegation_recall(ctx, state, stateid);
209 if (!err)
210 err = nfs_delegation_claim_locks(state, stateid);
211 mutex_unlock(&sp->so_delegreturn_mutex);
212 put_nfs_open_context(ctx);
213 if (err != 0)
214 return err;
215 goto again;
216 }
217 rcu_read_unlock();
218 return 0;
219 }
220
221 /**
222 * nfs_inode_reclaim_delegation - process a delegation reclaim request
223 * @inode: inode to process
224 * @cred: credential to use for request
225 * @type: delegation type
226 * @stateid: delegation stateid
227 * @pagemod_limit: write delegation "space_limit"
228 * @deleg_type: raw delegation type
229 *
230 */
nfs_inode_reclaim_delegation(struct inode * inode,const struct cred * cred,fmode_t type,const nfs4_stateid * stateid,unsigned long pagemod_limit,u32 deleg_type)231 void nfs_inode_reclaim_delegation(struct inode *inode, const struct cred *cred,
232 fmode_t type, const nfs4_stateid *stateid,
233 unsigned long pagemod_limit, u32 deleg_type)
234 {
235 struct nfs_delegation *delegation;
236 const struct cred *oldcred = NULL;
237
238 rcu_read_lock();
239 delegation = rcu_dereference(NFS_I(inode)->delegation);
240 if (delegation != NULL) {
241 spin_lock(&delegation->lock);
242 nfs4_stateid_copy(&delegation->stateid, stateid);
243 delegation->type = type;
244 delegation->pagemod_limit = pagemod_limit;
245 oldcred = delegation->cred;
246 delegation->cred = get_cred(cred);
247 switch (deleg_type) {
248 case NFS4_OPEN_DELEGATE_READ_ATTRS_DELEG:
249 case NFS4_OPEN_DELEGATE_WRITE_ATTRS_DELEG:
250 set_bit(NFS_DELEGATION_DELEGTIME, &delegation->flags);
251 break;
252 default:
253 clear_bit(NFS_DELEGATION_DELEGTIME, &delegation->flags);
254 }
255 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
256 if (test_and_clear_bit(NFS_DELEGATION_REVOKED,
257 &delegation->flags))
258 atomic_long_inc(&nfs_active_delegations);
259 spin_unlock(&delegation->lock);
260 rcu_read_unlock();
261 put_cred(oldcred);
262 trace_nfs4_reclaim_delegation(inode, type);
263 } else {
264 rcu_read_unlock();
265 nfs_inode_set_delegation(inode, cred, type, stateid,
266 pagemod_limit, deleg_type);
267 }
268 }
269
nfs_do_return_delegation(struct inode * inode,struct nfs_delegation * delegation,int issync)270 static int nfs_do_return_delegation(struct inode *inode,
271 struct nfs_delegation *delegation,
272 int issync)
273 {
274 const struct cred *cred;
275 int res = 0;
276
277 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
278 spin_lock(&delegation->lock);
279 cred = get_cred(delegation->cred);
280 spin_unlock(&delegation->lock);
281 res = nfs4_proc_delegreturn(inode, cred, &delegation->stateid,
282 delegation, issync);
283 put_cred(cred);
284 }
285 return res;
286 }
287
nfs_delegation_grab_inode(struct nfs_delegation * delegation)288 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
289 {
290 struct inode *inode = NULL;
291
292 spin_lock(&delegation->lock);
293 if (delegation->inode != NULL)
294 inode = igrab(delegation->inode);
295 if (!inode)
296 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
297 spin_unlock(&delegation->lock);
298 return inode;
299 }
300
301 static struct nfs_delegation *
nfs_start_delegation_return_locked(struct nfs_inode * nfsi)302 nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
303 {
304 struct nfs_delegation *ret = NULL;
305 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
306
307 if (delegation == NULL)
308 goto out;
309 spin_lock(&delegation->lock);
310 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
311 clear_bit(NFS_DELEGATION_RETURN_DELAYED, &delegation->flags);
312 /* Refcount matched in nfs_end_delegation_return() */
313 ret = nfs_get_delegation(delegation);
314 }
315 spin_unlock(&delegation->lock);
316 if (ret)
317 nfs_clear_verifier_delegated(&nfsi->vfs_inode);
318 out:
319 return ret;
320 }
321
322 static struct nfs_delegation *
nfs_start_delegation_return(struct nfs_inode * nfsi)323 nfs_start_delegation_return(struct nfs_inode *nfsi)
324 {
325 struct nfs_delegation *delegation;
326
327 rcu_read_lock();
328 delegation = nfs_start_delegation_return_locked(nfsi);
329 rcu_read_unlock();
330 return delegation;
331 }
332
nfs_abort_delegation_return(struct nfs_delegation * delegation,struct nfs_server * server,int err)333 static void nfs_abort_delegation_return(struct nfs_delegation *delegation,
334 struct nfs_server *server, int err)
335 {
336 spin_lock(&delegation->lock);
337 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
338 if (err == -EAGAIN) {
339 set_bit(NFS_DELEGATION_RETURN_DELAYED, &delegation->flags);
340 set_bit(NFS4SERV_DELEGRETURN_DELAYED,
341 &server->delegation_flags);
342 set_bit(NFS4CLNT_DELEGRETURN_DELAYED,
343 &server->nfs_client->cl_state);
344 }
345 spin_unlock(&delegation->lock);
346 }
347
348 static struct nfs_delegation *
nfs_detach_delegation_locked(struct nfs_inode * nfsi,struct nfs_delegation * delegation,struct nfs_client * clp)349 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
350 struct nfs_delegation *delegation,
351 struct nfs_client *clp)
352 {
353 struct nfs_delegation *deleg_cur =
354 rcu_dereference_protected(nfsi->delegation,
355 lockdep_is_held(&clp->cl_lock));
356
357 if (deleg_cur == NULL || delegation != deleg_cur)
358 return NULL;
359
360 spin_lock(&delegation->lock);
361 if (!delegation->inode) {
362 spin_unlock(&delegation->lock);
363 return NULL;
364 }
365 list_del_rcu(&delegation->super_list);
366 delegation->inode = NULL;
367 rcu_assign_pointer(nfsi->delegation, NULL);
368 spin_unlock(&delegation->lock);
369 return delegation;
370 }
371
nfs_detach_delegation(struct nfs_inode * nfsi,struct nfs_delegation * delegation,struct nfs_server * server)372 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
373 struct nfs_delegation *delegation,
374 struct nfs_server *server)
375 {
376 struct nfs_client *clp = server->nfs_client;
377
378 spin_lock(&clp->cl_lock);
379 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
380 spin_unlock(&clp->cl_lock);
381 return delegation;
382 }
383
384 static struct nfs_delegation *
nfs_inode_detach_delegation(struct inode * inode)385 nfs_inode_detach_delegation(struct inode *inode)
386 {
387 struct nfs_inode *nfsi = NFS_I(inode);
388 struct nfs_server *server = NFS_SERVER(inode);
389 struct nfs_delegation *delegation;
390
391 rcu_read_lock();
392 delegation = rcu_dereference(nfsi->delegation);
393 if (delegation != NULL)
394 delegation = nfs_detach_delegation(nfsi, delegation, server);
395 rcu_read_unlock();
396 return delegation;
397 }
398
399 static void
nfs_update_delegation_cred(struct nfs_delegation * delegation,const struct cred * cred)400 nfs_update_delegation_cred(struct nfs_delegation *delegation,
401 const struct cred *cred)
402 {
403 const struct cred *old;
404
405 if (cred_fscmp(delegation->cred, cred) != 0) {
406 old = xchg(&delegation->cred, get_cred(cred));
407 put_cred(old);
408 }
409 }
410
411 static void
nfs_update_inplace_delegation(struct nfs_delegation * delegation,const struct nfs_delegation * update)412 nfs_update_inplace_delegation(struct nfs_delegation *delegation,
413 const struct nfs_delegation *update)
414 {
415 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
416 delegation->stateid.seqid = update->stateid.seqid;
417 smp_wmb();
418 delegation->type = update->type;
419 delegation->pagemod_limit = update->pagemod_limit;
420 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
421 delegation->change_attr = update->change_attr;
422 nfs_update_delegation_cred(delegation, update->cred);
423 /* smp_mb__before_atomic() is implicit due to xchg() */
424 clear_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
425 atomic_long_inc(&nfs_active_delegations);
426 }
427 }
428 }
429
430 /**
431 * nfs_inode_set_delegation - set up a delegation on an inode
432 * @inode: inode to which delegation applies
433 * @cred: cred to use for subsequent delegation processing
434 * @type: delegation type
435 * @stateid: delegation stateid
436 * @pagemod_limit: write delegation "space_limit"
437 * @deleg_type: raw delegation type
438 *
439 * Returns zero on success, or a negative errno value.
440 */
nfs_inode_set_delegation(struct inode * inode,const struct cred * cred,fmode_t type,const nfs4_stateid * stateid,unsigned long pagemod_limit,u32 deleg_type)441 int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred,
442 fmode_t type, const nfs4_stateid *stateid,
443 unsigned long pagemod_limit, u32 deleg_type)
444 {
445 struct nfs_server *server = NFS_SERVER(inode);
446 struct nfs_client *clp = server->nfs_client;
447 struct nfs_inode *nfsi = NFS_I(inode);
448 struct nfs_delegation *delegation, *old_delegation;
449 struct nfs_delegation *freeme = NULL;
450 int status = 0;
451
452 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL_ACCOUNT);
453 if (delegation == NULL)
454 return -ENOMEM;
455 nfs4_stateid_copy(&delegation->stateid, stateid);
456 refcount_set(&delegation->refcount, 1);
457 delegation->type = type;
458 delegation->pagemod_limit = pagemod_limit;
459 delegation->change_attr = inode_peek_iversion_raw(inode);
460 delegation->cred = get_cred(cred);
461 delegation->inode = inode;
462 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
463 switch (deleg_type) {
464 case NFS4_OPEN_DELEGATE_READ_ATTRS_DELEG:
465 case NFS4_OPEN_DELEGATE_WRITE_ATTRS_DELEG:
466 delegation->flags |= BIT(NFS_DELEGATION_DELEGTIME);
467 }
468 delegation->test_gen = 0;
469 spin_lock_init(&delegation->lock);
470
471 spin_lock(&clp->cl_lock);
472 old_delegation = rcu_dereference_protected(nfsi->delegation,
473 lockdep_is_held(&clp->cl_lock));
474 if (old_delegation == NULL)
475 goto add_new;
476 /* Is this an update of the existing delegation? */
477 if (nfs4_stateid_match_other(&old_delegation->stateid,
478 &delegation->stateid)) {
479 spin_lock(&old_delegation->lock);
480 nfs_update_inplace_delegation(old_delegation,
481 delegation);
482 spin_unlock(&old_delegation->lock);
483 goto out;
484 }
485 if (!test_bit(NFS_DELEGATION_REVOKED, &old_delegation->flags)) {
486 /*
487 * Deal with broken servers that hand out two
488 * delegations for the same file.
489 * Allow for upgrades to a WRITE delegation, but
490 * nothing else.
491 */
492 dfprintk(FILE, "%s: server %s handed out "
493 "a duplicate delegation!\n",
494 __func__, clp->cl_hostname);
495 if (delegation->type == old_delegation->type ||
496 !(delegation->type & FMODE_WRITE)) {
497 freeme = delegation;
498 delegation = NULL;
499 goto out;
500 }
501 if (test_and_set_bit(NFS_DELEGATION_RETURNING,
502 &old_delegation->flags))
503 goto out;
504 }
505 freeme = nfs_detach_delegation_locked(nfsi, old_delegation, clp);
506 if (freeme == NULL)
507 goto out;
508 add_new:
509 /*
510 * If we didn't revalidate the change attribute before setting
511 * the delegation, then pre-emptively ask for a full attribute
512 * cache revalidation.
513 */
514 spin_lock(&inode->i_lock);
515 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_CHANGE)
516 nfs_set_cache_invalid(inode,
517 NFS_INO_INVALID_ATIME | NFS_INO_INVALID_CTIME |
518 NFS_INO_INVALID_MTIME | NFS_INO_INVALID_SIZE |
519 NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK |
520 NFS_INO_INVALID_OTHER | NFS_INO_INVALID_DATA |
521 NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL |
522 NFS_INO_INVALID_XATTR);
523 spin_unlock(&inode->i_lock);
524
525 list_add_tail_rcu(&delegation->super_list, &server->delegations);
526 rcu_assign_pointer(nfsi->delegation, delegation);
527 delegation = NULL;
528
529 atomic_long_inc(&nfs_active_delegations);
530
531 trace_nfs4_set_delegation(inode, type);
532
533 /* If we hold writebacks and have delegated mtime then update */
534 if (deleg_type == NFS4_OPEN_DELEGATE_WRITE_ATTRS_DELEG &&
535 nfs_have_writebacks(inode))
536 nfs_update_delegated_mtime(inode);
537 out:
538 spin_unlock(&clp->cl_lock);
539 if (delegation != NULL)
540 __nfs_free_delegation(delegation);
541 if (freeme != NULL) {
542 nfs_do_return_delegation(inode, freeme, 0);
543 nfs_free_delegation(freeme);
544 }
545 return status;
546 }
547
548 /*
549 * Basic procedure for returning a delegation to the server
550 */
nfs_end_delegation_return(struct inode * inode,struct nfs_delegation * delegation,int issync)551 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
552 {
553 struct nfs_server *server = NFS_SERVER(inode);
554 unsigned int mode = O_WRONLY | O_RDWR;
555 int err = 0;
556
557 if (delegation == NULL)
558 return 0;
559
560 if (!issync)
561 mode |= O_NONBLOCK;
562 /* Recall of any remaining application leases */
563 err = break_lease(inode, mode);
564
565 while (err == 0) {
566 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
567 break;
568 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
569 delegation->type);
570 if (!issync || err != -EAGAIN)
571 break;
572 /*
573 * Guard against state recovery
574 */
575 err = nfs4_wait_clnt_recover(server->nfs_client);
576 }
577
578 if (err) {
579 nfs_abort_delegation_return(delegation, server, err);
580 goto out;
581 }
582
583 err = nfs_do_return_delegation(inode, delegation, issync);
584 out:
585 /* Refcount matched in nfs_start_delegation_return_locked() */
586 nfs_put_delegation(delegation);
587 return err;
588 }
589
nfs_delegation_need_return(struct nfs_delegation * delegation)590 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
591 {
592 bool ret = false;
593
594 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
595 ret = true;
596 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags) ||
597 test_bit(NFS_DELEGATION_RETURN_DELAYED, &delegation->flags) ||
598 test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
599 ret = false;
600
601 return ret;
602 }
603
nfs_server_return_marked_delegations(struct nfs_server * server,void __always_unused * data)604 static int nfs_server_return_marked_delegations(struct nfs_server *server,
605 void __always_unused *data)
606 {
607 struct nfs_delegation *delegation;
608 struct nfs_delegation *prev;
609 struct inode *inode;
610 struct inode *place_holder = NULL;
611 struct nfs_delegation *place_holder_deleg = NULL;
612 int err = 0;
613
614 if (!test_and_clear_bit(NFS4SERV_DELEGRETURN,
615 &server->delegation_flags))
616 return 0;
617 restart:
618 /*
619 * To avoid quadratic looping we hold a reference
620 * to an inode place_holder. Each time we restart, we
621 * list delegation in the server from the delegations
622 * of that inode.
623 * prev is an RCU-protected pointer to a delegation which
624 * wasn't marked for return and might be a good choice for
625 * the next place_holder.
626 */
627 prev = NULL;
628 delegation = NULL;
629 rcu_read_lock();
630 if (place_holder)
631 delegation = rcu_dereference(NFS_I(place_holder)->delegation);
632 if (!delegation || delegation != place_holder_deleg)
633 delegation = list_entry_rcu(server->delegations.next,
634 struct nfs_delegation, super_list);
635 list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
636 struct inode *to_put = NULL;
637
638 if (test_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags))
639 continue;
640 if (!nfs_delegation_need_return(delegation)) {
641 if (nfs4_is_valid_delegation(delegation, 0))
642 prev = delegation;
643 continue;
644 }
645 inode = nfs_delegation_grab_inode(delegation);
646 if (inode == NULL)
647 continue;
648
649 if (prev) {
650 struct inode *tmp = nfs_delegation_grab_inode(prev);
651 if (tmp) {
652 to_put = place_holder;
653 place_holder = tmp;
654 place_holder_deleg = prev;
655 }
656 }
657
658 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
659 rcu_read_unlock();
660
661 iput(to_put);
662
663 err = nfs_end_delegation_return(inode, delegation, 0);
664 iput(inode);
665 cond_resched();
666 if (!err)
667 goto restart;
668 set_bit(NFS4SERV_DELEGRETURN, &server->delegation_flags);
669 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
670 goto out;
671 }
672 rcu_read_unlock();
673 out:
674 iput(place_holder);
675 return err;
676 }
677
nfs_server_clear_delayed_delegations(struct nfs_server * server)678 static bool nfs_server_clear_delayed_delegations(struct nfs_server *server)
679 {
680 struct nfs_delegation *d;
681 bool ret = false;
682
683 if (!test_and_clear_bit(NFS4SERV_DELEGRETURN_DELAYED,
684 &server->delegation_flags))
685 goto out;
686 list_for_each_entry_rcu (d, &server->delegations, super_list) {
687 if (!test_bit(NFS_DELEGATION_RETURN_DELAYED, &d->flags))
688 continue;
689 nfs_mark_return_delegation(server, d);
690 clear_bit(NFS_DELEGATION_RETURN_DELAYED, &d->flags);
691 ret = true;
692 }
693 out:
694 return ret;
695 }
696
nfs_client_clear_delayed_delegations(struct nfs_client * clp)697 static bool nfs_client_clear_delayed_delegations(struct nfs_client *clp)
698 {
699 struct nfs_server *server;
700 bool ret = false;
701
702 if (!test_and_clear_bit(NFS4CLNT_DELEGRETURN_DELAYED, &clp->cl_state))
703 goto out;
704 rcu_read_lock();
705 list_for_each_entry_rcu (server, &clp->cl_superblocks, client_link) {
706 if (nfs_server_clear_delayed_delegations(server))
707 ret = true;
708 }
709 rcu_read_unlock();
710 out:
711 return ret;
712 }
713
714 /**
715 * nfs_client_return_marked_delegations - return previously marked delegations
716 * @clp: nfs_client to process
717 *
718 * Note that this function is designed to be called by the state
719 * manager thread. For this reason, it cannot flush the dirty data,
720 * since that could deadlock in case of a state recovery error.
721 *
722 * Returns zero on success, or a negative errno value.
723 */
nfs_client_return_marked_delegations(struct nfs_client * clp)724 int nfs_client_return_marked_delegations(struct nfs_client *clp)
725 {
726 int err = nfs_client_for_each_server(
727 clp, nfs_server_return_marked_delegations, NULL);
728 if (err)
729 return err;
730 /* If a return was delayed, sleep to prevent hard looping */
731 if (nfs_client_clear_delayed_delegations(clp))
732 ssleep(1);
733 return 0;
734 }
735
736 /**
737 * nfs_inode_evict_delegation - return delegation, don't reclaim opens
738 * @inode: inode to process
739 *
740 * Does not protect against delegation reclaims, therefore really only safe
741 * to be called from nfs4_clear_inode(). Guaranteed to always free
742 * the delegation structure.
743 */
nfs_inode_evict_delegation(struct inode * inode)744 void nfs_inode_evict_delegation(struct inode *inode)
745 {
746 struct nfs_delegation *delegation;
747
748 delegation = nfs_inode_detach_delegation(inode);
749 if (delegation != NULL) {
750 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
751 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
752 nfs_do_return_delegation(inode, delegation, 1);
753 nfs_free_delegation(delegation);
754 }
755 }
756
757 /**
758 * nfs4_inode_return_delegation - synchronously return a delegation
759 * @inode: inode to process
760 *
761 * This routine will always flush any dirty data to disk on the
762 * assumption that if we need to return the delegation, then
763 * we should stop caching.
764 *
765 * Returns zero on success, or a negative errno value.
766 */
nfs4_inode_return_delegation(struct inode * inode)767 int nfs4_inode_return_delegation(struct inode *inode)
768 {
769 struct nfs_inode *nfsi = NFS_I(inode);
770 struct nfs_delegation *delegation;
771
772 delegation = nfs_start_delegation_return(nfsi);
773 if (delegation != NULL) {
774 /* Synchronous recall of any application leases */
775 break_lease(inode, O_WRONLY | O_RDWR);
776 if (S_ISREG(inode->i_mode))
777 nfs_wb_all(inode);
778 return nfs_end_delegation_return(inode, delegation, 1);
779 }
780 return 0;
781 }
782
783 /**
784 * nfs4_inode_set_return_delegation_on_close - asynchronously return a delegation
785 * @inode: inode to process
786 *
787 * This routine is called to request that the delegation be returned as soon
788 * as the file is closed. If the file is already closed, the delegation is
789 * immediately returned.
790 */
nfs4_inode_set_return_delegation_on_close(struct inode * inode)791 void nfs4_inode_set_return_delegation_on_close(struct inode *inode)
792 {
793 struct nfs_delegation *delegation;
794 struct nfs_delegation *ret = NULL;
795
796 if (!inode)
797 return;
798 rcu_read_lock();
799 delegation = nfs4_get_valid_delegation(inode);
800 if (!delegation)
801 goto out;
802 spin_lock(&delegation->lock);
803 if (!delegation->inode)
804 goto out_unlock;
805 if (list_empty(&NFS_I(inode)->open_files) &&
806 !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
807 /* Refcount matched in nfs_end_delegation_return() */
808 ret = nfs_get_delegation(delegation);
809 } else
810 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
811 out_unlock:
812 spin_unlock(&delegation->lock);
813 if (ret)
814 nfs_clear_verifier_delegated(inode);
815 out:
816 rcu_read_unlock();
817 nfs_end_delegation_return(inode, ret, 0);
818 }
819
820 /**
821 * nfs4_inode_return_delegation_on_close - asynchronously return a delegation
822 * @inode: inode to process
823 *
824 * This routine is called on file close in order to determine if the
825 * inode delegation needs to be returned immediately.
826 */
nfs4_inode_return_delegation_on_close(struct inode * inode)827 void nfs4_inode_return_delegation_on_close(struct inode *inode)
828 {
829 struct nfs_delegation *delegation;
830 struct nfs_delegation *ret = NULL;
831
832 if (!inode)
833 return;
834 rcu_read_lock();
835 delegation = nfs4_get_valid_delegation(inode);
836 if (!delegation)
837 goto out;
838 if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) ||
839 atomic_long_read(&nfs_active_delegations) >= nfs_delegation_watermark) {
840 spin_lock(&delegation->lock);
841 if (delegation->inode &&
842 list_empty(&NFS_I(inode)->open_files) &&
843 !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
844 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
845 /* Refcount matched in nfs_end_delegation_return() */
846 ret = nfs_get_delegation(delegation);
847 }
848 spin_unlock(&delegation->lock);
849 if (ret)
850 nfs_clear_verifier_delegated(inode);
851 }
852 out:
853 rcu_read_unlock();
854 nfs_end_delegation_return(inode, ret, 0);
855 }
856
857 /**
858 * nfs4_inode_make_writeable
859 * @inode: pointer to inode
860 *
861 * Make the inode writeable by returning the delegation if necessary
862 *
863 * Returns zero on success, or a negative errno value.
864 */
nfs4_inode_make_writeable(struct inode * inode)865 int nfs4_inode_make_writeable(struct inode *inode)
866 {
867 struct nfs_delegation *delegation;
868
869 rcu_read_lock();
870 delegation = nfs4_get_valid_delegation(inode);
871 if (delegation == NULL ||
872 (nfs4_has_session(NFS_SERVER(inode)->nfs_client) &&
873 (delegation->type & FMODE_WRITE))) {
874 rcu_read_unlock();
875 return 0;
876 }
877 rcu_read_unlock();
878 return nfs4_inode_return_delegation(inode);
879 }
880
881 static void
nfs_mark_return_if_closed_delegation(struct nfs_server * server,struct nfs_delegation * delegation)882 nfs_mark_return_if_closed_delegation(struct nfs_server *server,
883 struct nfs_delegation *delegation)
884 {
885 struct inode *inode;
886
887 if (test_bit(NFS_DELEGATION_RETURN, &delegation->flags) ||
888 test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags))
889 return;
890 spin_lock(&delegation->lock);
891 inode = delegation->inode;
892 if (!inode)
893 goto out;
894 if (list_empty(&NFS_I(inode)->open_files))
895 nfs_mark_return_delegation(server, delegation);
896 else
897 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
898 out:
899 spin_unlock(&delegation->lock);
900 }
901
nfs_server_mark_return_all_delegations(struct nfs_server * server)902 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
903 {
904 struct nfs_delegation *delegation;
905 bool ret = false;
906
907 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
908 nfs_mark_return_delegation(server, delegation);
909 ret = true;
910 }
911 return ret;
912 }
913
nfs_client_mark_return_all_delegations(struct nfs_client * clp)914 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
915 {
916 struct nfs_server *server;
917
918 rcu_read_lock();
919 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
920 nfs_server_mark_return_all_delegations(server);
921 rcu_read_unlock();
922 }
923
nfs_delegation_run_state_manager(struct nfs_client * clp)924 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
925 {
926 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
927 nfs4_schedule_state_manager(clp);
928 }
929
930 /**
931 * nfs_expire_all_delegations
932 * @clp: client to process
933 *
934 */
nfs_expire_all_delegations(struct nfs_client * clp)935 void nfs_expire_all_delegations(struct nfs_client *clp)
936 {
937 nfs_client_mark_return_all_delegations(clp);
938 nfs_delegation_run_state_manager(clp);
939 }
940
941 /**
942 * nfs_server_return_all_delegations - return delegations for one superblock
943 * @server: pointer to nfs_server to process
944 *
945 */
nfs_server_return_all_delegations(struct nfs_server * server)946 void nfs_server_return_all_delegations(struct nfs_server *server)
947 {
948 struct nfs_client *clp = server->nfs_client;
949 bool need_wait;
950
951 if (clp == NULL)
952 return;
953
954 rcu_read_lock();
955 need_wait = nfs_server_mark_return_all_delegations(server);
956 rcu_read_unlock();
957
958 if (need_wait) {
959 nfs4_schedule_state_manager(clp);
960 nfs4_wait_clnt_recover(clp);
961 }
962 }
963
nfs_mark_return_unused_delegation_types(struct nfs_server * server,fmode_t flags)964 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
965 fmode_t flags)
966 {
967 struct nfs_delegation *delegation;
968
969 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
970 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
971 continue;
972 if (delegation->type & flags)
973 nfs_mark_return_if_closed_delegation(server, delegation);
974 }
975 }
976
nfs_client_mark_return_unused_delegation_types(struct nfs_client * clp,fmode_t flags)977 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
978 fmode_t flags)
979 {
980 struct nfs_server *server;
981
982 rcu_read_lock();
983 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
984 nfs_mark_return_unused_delegation_types(server, flags);
985 rcu_read_unlock();
986 }
987
nfs_revoke_delegation(struct inode * inode,const nfs4_stateid * stateid)988 static void nfs_revoke_delegation(struct inode *inode,
989 const nfs4_stateid *stateid)
990 {
991 struct nfs_delegation *delegation;
992 nfs4_stateid tmp;
993 bool ret = false;
994
995 rcu_read_lock();
996 delegation = rcu_dereference(NFS_I(inode)->delegation);
997 if (delegation == NULL)
998 goto out;
999 if (stateid == NULL) {
1000 nfs4_stateid_copy(&tmp, &delegation->stateid);
1001 stateid = &tmp;
1002 } else {
1003 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
1004 goto out;
1005 spin_lock(&delegation->lock);
1006 if (stateid->seqid) {
1007 if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) {
1008 spin_unlock(&delegation->lock);
1009 goto out;
1010 }
1011 delegation->stateid.seqid = stateid->seqid;
1012 }
1013 spin_unlock(&delegation->lock);
1014 }
1015 nfs_mark_delegation_revoked(delegation);
1016 ret = true;
1017 out:
1018 rcu_read_unlock();
1019 if (ret)
1020 nfs_inode_find_state_and_recover(inode, stateid);
1021 }
1022
nfs_remove_bad_delegation(struct inode * inode,const nfs4_stateid * stateid)1023 void nfs_remove_bad_delegation(struct inode *inode,
1024 const nfs4_stateid *stateid)
1025 {
1026 nfs_revoke_delegation(inode, stateid);
1027 }
1028 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
1029
nfs_delegation_mark_returned(struct inode * inode,const nfs4_stateid * stateid)1030 void nfs_delegation_mark_returned(struct inode *inode,
1031 const nfs4_stateid *stateid)
1032 {
1033 struct nfs_delegation *delegation;
1034
1035 if (!inode)
1036 return;
1037
1038 rcu_read_lock();
1039 delegation = rcu_dereference(NFS_I(inode)->delegation);
1040 if (!delegation)
1041 goto out_rcu_unlock;
1042
1043 spin_lock(&delegation->lock);
1044 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
1045 goto out_spin_unlock;
1046 if (stateid->seqid) {
1047 /* If delegation->stateid is newer, dont mark as returned */
1048 if (nfs4_stateid_is_newer(&delegation->stateid, stateid))
1049 goto out_clear_returning;
1050 if (delegation->stateid.seqid != stateid->seqid)
1051 delegation->stateid.seqid = stateid->seqid;
1052 }
1053
1054 nfs_mark_delegation_revoked(delegation);
1055 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
1056 spin_unlock(&delegation->lock);
1057 if (nfs_detach_delegation(NFS_I(inode), delegation, NFS_SERVER(inode)))
1058 nfs_put_delegation(delegation);
1059 goto out_rcu_unlock;
1060
1061 out_clear_returning:
1062 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
1063 out_spin_unlock:
1064 spin_unlock(&delegation->lock);
1065 out_rcu_unlock:
1066 rcu_read_unlock();
1067
1068 nfs_inode_find_state_and_recover(inode, stateid);
1069 }
1070
1071 /**
1072 * nfs_expire_unused_delegation_types
1073 * @clp: client to process
1074 * @flags: delegation types to expire
1075 *
1076 */
nfs_expire_unused_delegation_types(struct nfs_client * clp,fmode_t flags)1077 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
1078 {
1079 nfs_client_mark_return_unused_delegation_types(clp, flags);
1080 nfs_delegation_run_state_manager(clp);
1081 }
1082
nfs_mark_return_unreferenced_delegations(struct nfs_server * server)1083 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
1084 {
1085 struct nfs_delegation *delegation;
1086
1087 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1088 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
1089 continue;
1090 nfs_mark_return_if_closed_delegation(server, delegation);
1091 }
1092 }
1093
1094 /**
1095 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
1096 * @clp: nfs_client to process
1097 *
1098 */
nfs_expire_unreferenced_delegations(struct nfs_client * clp)1099 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
1100 {
1101 struct nfs_server *server;
1102
1103 rcu_read_lock();
1104 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1105 nfs_mark_return_unreferenced_delegations(server);
1106 rcu_read_unlock();
1107
1108 nfs_delegation_run_state_manager(clp);
1109 }
1110
1111 /**
1112 * nfs_async_inode_return_delegation - asynchronously return a delegation
1113 * @inode: inode to process
1114 * @stateid: state ID information
1115 *
1116 * Returns zero on success, or a negative errno value.
1117 */
nfs_async_inode_return_delegation(struct inode * inode,const nfs4_stateid * stateid)1118 int nfs_async_inode_return_delegation(struct inode *inode,
1119 const nfs4_stateid *stateid)
1120 {
1121 struct nfs_server *server = NFS_SERVER(inode);
1122 struct nfs_client *clp = server->nfs_client;
1123 struct nfs_delegation *delegation;
1124
1125 rcu_read_lock();
1126 delegation = nfs4_get_valid_delegation(inode);
1127 if (delegation == NULL)
1128 goto out_enoent;
1129 if (stateid != NULL &&
1130 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
1131 goto out_enoent;
1132 nfs_mark_return_delegation(server, delegation);
1133 rcu_read_unlock();
1134
1135 /* If there are any application leases or delegations, recall them */
1136 break_lease(inode, O_WRONLY | O_RDWR | O_NONBLOCK);
1137
1138 nfs_delegation_run_state_manager(clp);
1139 return 0;
1140 out_enoent:
1141 rcu_read_unlock();
1142 return -ENOENT;
1143 }
1144
1145 static struct inode *
nfs_delegation_find_inode_server(struct nfs_server * server,const struct nfs_fh * fhandle)1146 nfs_delegation_find_inode_server(struct nfs_server *server,
1147 const struct nfs_fh *fhandle)
1148 {
1149 struct nfs_delegation *delegation;
1150 struct super_block *freeme = NULL;
1151 struct inode *res = NULL;
1152
1153 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1154 spin_lock(&delegation->lock);
1155 if (delegation->inode != NULL &&
1156 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
1157 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
1158 if (nfs_sb_active(server->super)) {
1159 freeme = server->super;
1160 res = igrab(delegation->inode);
1161 }
1162 spin_unlock(&delegation->lock);
1163 if (res != NULL)
1164 return res;
1165 if (freeme) {
1166 rcu_read_unlock();
1167 nfs_sb_deactive(freeme);
1168 rcu_read_lock();
1169 }
1170 return ERR_PTR(-EAGAIN);
1171 }
1172 spin_unlock(&delegation->lock);
1173 }
1174 return ERR_PTR(-ENOENT);
1175 }
1176
1177 /**
1178 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
1179 * @clp: client state handle
1180 * @fhandle: filehandle from a delegation recall
1181 *
1182 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
1183 * cannot be found.
1184 */
nfs_delegation_find_inode(struct nfs_client * clp,const struct nfs_fh * fhandle)1185 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
1186 const struct nfs_fh *fhandle)
1187 {
1188 struct nfs_server *server;
1189 struct inode *res;
1190
1191 rcu_read_lock();
1192 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1193 res = nfs_delegation_find_inode_server(server, fhandle);
1194 if (res != ERR_PTR(-ENOENT)) {
1195 rcu_read_unlock();
1196 return res;
1197 }
1198 }
1199 rcu_read_unlock();
1200 return ERR_PTR(-ENOENT);
1201 }
1202
nfs_delegation_mark_reclaim_server(struct nfs_server * server)1203 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
1204 {
1205 struct nfs_delegation *delegation;
1206
1207 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1208 /*
1209 * If the delegation may have been admin revoked, then we
1210 * cannot reclaim it.
1211 */
1212 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
1213 continue;
1214 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1215 }
1216 }
1217
1218 /**
1219 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
1220 * @clp: nfs_client to process
1221 *
1222 */
nfs_delegation_mark_reclaim(struct nfs_client * clp)1223 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
1224 {
1225 struct nfs_server *server;
1226
1227 rcu_read_lock();
1228 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1229 nfs_delegation_mark_reclaim_server(server);
1230 rcu_read_unlock();
1231 }
1232
nfs_server_reap_unclaimed_delegations(struct nfs_server * server,void __always_unused * data)1233 static int nfs_server_reap_unclaimed_delegations(struct nfs_server *server,
1234 void __always_unused *data)
1235 {
1236 struct nfs_delegation *delegation;
1237 struct inode *inode;
1238 restart:
1239 rcu_read_lock();
1240 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1241 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1242 &delegation->flags) ||
1243 test_bit(NFS_DELEGATION_RETURNING,
1244 &delegation->flags) ||
1245 test_bit(NFS_DELEGATION_NEED_RECLAIM,
1246 &delegation->flags) == 0)
1247 continue;
1248 inode = nfs_delegation_grab_inode(delegation);
1249 if (inode == NULL)
1250 continue;
1251 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
1252 rcu_read_unlock();
1253 if (delegation != NULL) {
1254 if (nfs_detach_delegation(NFS_I(inode), delegation,
1255 server) != NULL)
1256 nfs_free_delegation(delegation);
1257 /* Match nfs_start_delegation_return_locked */
1258 nfs_put_delegation(delegation);
1259 }
1260 iput(inode);
1261 cond_resched();
1262 goto restart;
1263 }
1264 rcu_read_unlock();
1265 return 0;
1266 }
1267
1268 /**
1269 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
1270 * @clp: nfs_client to process
1271 *
1272 */
nfs_delegation_reap_unclaimed(struct nfs_client * clp)1273 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
1274 {
1275 nfs_client_for_each_server(clp, nfs_server_reap_unclaimed_delegations,
1276 NULL);
1277 }
1278
nfs4_server_rebooted(const struct nfs_client * clp)1279 static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
1280 {
1281 return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
1282 BIT(NFS4CLNT_LEASE_EXPIRED) |
1283 BIT(NFS4CLNT_SESSION_RESET))) != 0;
1284 }
1285
nfs_mark_test_expired_delegation(struct nfs_server * server,struct nfs_delegation * delegation)1286 static void nfs_mark_test_expired_delegation(struct nfs_server *server,
1287 struct nfs_delegation *delegation)
1288 {
1289 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
1290 return;
1291 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1292 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1293 set_bit(NFS4SERV_DELEGATION_EXPIRED, &server->delegation_flags);
1294 set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
1295 }
1296
nfs_inode_mark_test_expired_delegation(struct nfs_server * server,struct inode * inode)1297 static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
1298 struct inode *inode)
1299 {
1300 struct nfs_delegation *delegation;
1301
1302 rcu_read_lock();
1303 delegation = rcu_dereference(NFS_I(inode)->delegation);
1304 if (delegation)
1305 nfs_mark_test_expired_delegation(server, delegation);
1306 rcu_read_unlock();
1307
1308 }
1309
nfs_delegation_mark_test_expired_server(struct nfs_server * server)1310 static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
1311 {
1312 struct nfs_delegation *delegation;
1313
1314 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
1315 nfs_mark_test_expired_delegation(server, delegation);
1316 }
1317
1318 /**
1319 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1320 * @clp: nfs_client to process
1321 *
1322 * Iterates through all the delegations associated with this server and
1323 * marks them as needing to be checked for validity.
1324 */
nfs_mark_test_expired_all_delegations(struct nfs_client * clp)1325 void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
1326 {
1327 struct nfs_server *server;
1328
1329 rcu_read_lock();
1330 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1331 nfs_delegation_mark_test_expired_server(server);
1332 rcu_read_unlock();
1333 }
1334
1335 /**
1336 * nfs_test_expired_all_delegations - test all delegations for a client
1337 * @clp: nfs_client to process
1338 *
1339 * Helper for handling "recallable state revoked" status from server.
1340 */
nfs_test_expired_all_delegations(struct nfs_client * clp)1341 void nfs_test_expired_all_delegations(struct nfs_client *clp)
1342 {
1343 nfs_mark_test_expired_all_delegations(clp);
1344 nfs4_schedule_state_manager(clp);
1345 }
1346
1347 static void
nfs_delegation_test_free_expired(struct inode * inode,nfs4_stateid * stateid,const struct cred * cred)1348 nfs_delegation_test_free_expired(struct inode *inode,
1349 nfs4_stateid *stateid,
1350 const struct cred *cred)
1351 {
1352 struct nfs_server *server = NFS_SERVER(inode);
1353 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
1354 int status;
1355
1356 if (!cred)
1357 return;
1358 status = ops->test_and_free_expired(server, stateid, cred);
1359 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
1360 nfs_remove_bad_delegation(inode, stateid);
1361 }
1362
nfs_server_reap_expired_delegations(struct nfs_server * server,void __always_unused * data)1363 static int nfs_server_reap_expired_delegations(struct nfs_server *server,
1364 void __always_unused *data)
1365 {
1366 struct nfs_delegation *delegation;
1367 struct inode *inode;
1368 const struct cred *cred;
1369 nfs4_stateid stateid;
1370 unsigned long gen = ++server->delegation_gen;
1371
1372 if (!test_and_clear_bit(NFS4SERV_DELEGATION_EXPIRED,
1373 &server->delegation_flags))
1374 return 0;
1375 restart:
1376 rcu_read_lock();
1377 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1378 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1379 &delegation->flags) ||
1380 test_bit(NFS_DELEGATION_RETURNING,
1381 &delegation->flags) ||
1382 test_bit(NFS_DELEGATION_TEST_EXPIRED,
1383 &delegation->flags) == 0 ||
1384 delegation->test_gen == gen)
1385 continue;
1386 inode = nfs_delegation_grab_inode(delegation);
1387 if (inode == NULL)
1388 continue;
1389 spin_lock(&delegation->lock);
1390 cred = get_cred_rcu(delegation->cred);
1391 nfs4_stateid_copy(&stateid, &delegation->stateid);
1392 spin_unlock(&delegation->lock);
1393 delegation->test_gen = gen;
1394 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1395 rcu_read_unlock();
1396 nfs_delegation_test_free_expired(inode, &stateid, cred);
1397 put_cred(cred);
1398 if (!nfs4_server_rebooted(server->nfs_client)) {
1399 iput(inode);
1400 cond_resched();
1401 goto restart;
1402 }
1403 nfs_inode_mark_test_expired_delegation(server,inode);
1404 set_bit(NFS4SERV_DELEGATION_EXPIRED, &server->delegation_flags);
1405 set_bit(NFS4CLNT_DELEGATION_EXPIRED,
1406 &server->nfs_client->cl_state);
1407 iput(inode);
1408 return -EAGAIN;
1409 }
1410 rcu_read_unlock();
1411 return 0;
1412 }
1413
1414 /**
1415 * nfs_reap_expired_delegations - reap expired delegations
1416 * @clp: nfs_client to process
1417 *
1418 * Iterates through all the delegations associated with this server and
1419 * checks if they have may have been revoked. This function is usually
1420 * expected to be called in cases where the server may have lost its
1421 * lease.
1422 */
nfs_reap_expired_delegations(struct nfs_client * clp)1423 void nfs_reap_expired_delegations(struct nfs_client *clp)
1424 {
1425 nfs_client_for_each_server(clp, nfs_server_reap_expired_delegations,
1426 NULL);
1427 }
1428
nfs_inode_find_delegation_state_and_recover(struct inode * inode,const nfs4_stateid * stateid)1429 void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
1430 const nfs4_stateid *stateid)
1431 {
1432 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1433 struct nfs_delegation *delegation;
1434 bool found = false;
1435
1436 rcu_read_lock();
1437 delegation = rcu_dereference(NFS_I(inode)->delegation);
1438 if (delegation &&
1439 nfs4_stateid_match_or_older(&delegation->stateid, stateid) &&
1440 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1441 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1442 found = true;
1443 }
1444 rcu_read_unlock();
1445 if (found)
1446 nfs4_schedule_state_manager(clp);
1447 }
1448
1449 /**
1450 * nfs_delegations_present - check for existence of delegations
1451 * @clp: client state handle
1452 *
1453 * Returns one if there are any nfs_delegation structures attached
1454 * to this nfs_client.
1455 */
nfs_delegations_present(struct nfs_client * clp)1456 int nfs_delegations_present(struct nfs_client *clp)
1457 {
1458 struct nfs_server *server;
1459 int ret = 0;
1460
1461 rcu_read_lock();
1462 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1463 if (!list_empty(&server->delegations)) {
1464 ret = 1;
1465 break;
1466 }
1467 rcu_read_unlock();
1468 return ret;
1469 }
1470
1471 /**
1472 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1473 * @dst: stateid to refresh
1474 * @inode: inode to check
1475 *
1476 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1477 * that matches our delegation stateid. Otherwise "false" is returned.
1478 */
nfs4_refresh_delegation_stateid(nfs4_stateid * dst,struct inode * inode)1479 bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
1480 {
1481 struct nfs_delegation *delegation;
1482 bool ret = false;
1483 if (!inode)
1484 goto out;
1485
1486 rcu_read_lock();
1487 delegation = rcu_dereference(NFS_I(inode)->delegation);
1488 if (delegation != NULL &&
1489 nfs4_stateid_match_other(dst, &delegation->stateid) &&
1490 nfs4_stateid_is_newer(&delegation->stateid, dst) &&
1491 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1492 dst->seqid = delegation->stateid.seqid;
1493 ret = true;
1494 }
1495 rcu_read_unlock();
1496 out:
1497 return ret;
1498 }
1499
1500 /**
1501 * nfs4_copy_delegation_stateid - Copy inode's state ID information
1502 * @inode: inode to check
1503 * @flags: delegation type requirement
1504 * @dst: stateid data structure to fill in
1505 * @cred: optional argument to retrieve credential
1506 *
1507 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1508 * otherwise "false" is returned.
1509 */
nfs4_copy_delegation_stateid(struct inode * inode,fmode_t flags,nfs4_stateid * dst,const struct cred ** cred)1510 bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
1511 nfs4_stateid *dst, const struct cred **cred)
1512 {
1513 struct nfs_inode *nfsi = NFS_I(inode);
1514 struct nfs_delegation *delegation;
1515 bool ret = false;
1516
1517 flags &= FMODE_READ|FMODE_WRITE;
1518 rcu_read_lock();
1519 delegation = rcu_dereference(nfsi->delegation);
1520 if (!delegation)
1521 goto out;
1522 spin_lock(&delegation->lock);
1523 ret = nfs4_is_valid_delegation(delegation, flags);
1524 if (ret) {
1525 nfs4_stateid_copy(dst, &delegation->stateid);
1526 nfs_mark_delegation_referenced(delegation);
1527 if (cred)
1528 *cred = get_cred(delegation->cred);
1529 }
1530 spin_unlock(&delegation->lock);
1531 out:
1532 rcu_read_unlock();
1533 return ret;
1534 }
1535
1536 /**
1537 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1538 * @inode: inode to check
1539 *
1540 * This function checks the number of outstanding writes to the file
1541 * against the delegation 'space_limit' field to see if
1542 * the spec requires us to flush the file on close.
1543 */
nfs4_delegation_flush_on_close(const struct inode * inode)1544 bool nfs4_delegation_flush_on_close(const struct inode *inode)
1545 {
1546 struct nfs_inode *nfsi = NFS_I(inode);
1547 struct nfs_delegation *delegation;
1548 bool ret = true;
1549
1550 rcu_read_lock();
1551 delegation = rcu_dereference(nfsi->delegation);
1552 if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1553 goto out;
1554 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)
1555 ret = false;
1556 out:
1557 rcu_read_unlock();
1558 return ret;
1559 }
1560
1561 module_param_named(delegation_watermark, nfs_delegation_watermark, uint, 0644);
1562