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