Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * linux/fs/nfs/delegation.c | |
4 | * | |
5 | * Copyright (C) 2004 Trond Myklebust | |
6 | * | |
7 | * NFS file delegation management | |
8 | * | |
9 | */ | |
1da177e4 | 10 | #include <linux/completion.h> |
58d9714a | 11 | #include <linux/kthread.h> |
1da177e4 LT |
12 | #include <linux/module.h> |
13 | #include <linux/sched.h> | |
5a0e3ad6 | 14 | #include <linux/slab.h> |
1da177e4 | 15 | #include <linux/spinlock.h> |
1eb5d98f | 16 | #include <linux/iversion.h> |
1da177e4 LT |
17 | |
18 | #include <linux/nfs4.h> | |
19 | #include <linux/nfs_fs.h> | |
20 | #include <linux/nfs_xdr.h> | |
21 | ||
4ce79717 | 22 | #include "nfs4_fs.h" |
c01d3645 | 23 | #include "nfs4session.h" |
1da177e4 | 24 | #include "delegation.h" |
24c8dbbb | 25 | #include "internal.h" |
ca8acf8d | 26 | #include "nfs4trace.h" |
1da177e4 | 27 | |
905f8d16 TM |
28 | static void nfs_free_delegation(struct nfs_delegation *delegation) |
29 | { | |
a52458b4 N |
30 | put_cred(delegation->cred); |
31 | delegation->cred = NULL; | |
26f04dde | 32 | kfree_rcu(delegation, rcu); |
8383e460 TM |
33 | } |
34 | ||
d3978bb3 CL |
35 | /** |
36 | * nfs_mark_delegation_referenced - set delegation's REFERENCED flag | |
37 | * @delegation: delegation to process | |
38 | * | |
39 | */ | |
b7391f44 TM |
40 | void nfs_mark_delegation_referenced(struct nfs_delegation *delegation) |
41 | { | |
42 | set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags); | |
43 | } | |
44 | ||
aa05c87f TM |
45 | static bool |
46 | nfs4_is_valid_delegation(const struct nfs_delegation *delegation, | |
47 | fmode_t flags) | |
48 | { | |
49 | if (delegation != NULL && (delegation->type & flags) == flags && | |
50 | !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) && | |
51 | !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) | |
52 | return true; | |
53 | return false; | |
54 | } | |
55 | ||
15bb3afe PT |
56 | static int |
57 | nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark) | |
b7391f44 TM |
58 | { |
59 | struct nfs_delegation *delegation; | |
60 | int ret = 0; | |
61 | ||
62 | flags &= FMODE_READ|FMODE_WRITE; | |
63 | rcu_read_lock(); | |
64 | delegation = rcu_dereference(NFS_I(inode)->delegation); | |
aa05c87f | 65 | if (nfs4_is_valid_delegation(delegation, flags)) { |
15bb3afe PT |
66 | if (mark) |
67 | nfs_mark_delegation_referenced(delegation); | |
b7391f44 TM |
68 | ret = 1; |
69 | } | |
70 | rcu_read_unlock(); | |
71 | return ret; | |
72 | } | |
15bb3afe PT |
73 | /** |
74 | * nfs_have_delegation - check if inode has a delegation, mark it | |
75 | * NFS_DELEGATION_REFERENCED if there is one. | |
76 | * @inode: inode to check | |
77 | * @flags: delegation types to check for | |
78 | * | |
79 | * Returns one if inode has the indicated delegation, otherwise zero. | |
80 | */ | |
81 | int nfs4_have_delegation(struct inode *inode, fmode_t flags) | |
82 | { | |
83 | return nfs4_do_check_delegation(inode, flags, true); | |
84 | } | |
85 | ||
86 | /* | |
87 | * nfs4_check_delegation - check if inode has a delegation, do not mark | |
88 | * NFS_DELEGATION_REFERENCED if it has one. | |
89 | */ | |
90 | int nfs4_check_delegation(struct inode *inode, fmode_t flags) | |
91 | { | |
92 | return nfs4_do_check_delegation(inode, flags, false); | |
93 | } | |
b7391f44 | 94 | |
44f411c3 | 95 | static int nfs_delegation_claim_locks(struct nfs4_state *state, const nfs4_stateid *stateid) |
888e694c TM |
96 | { |
97 | struct inode *inode = state->inode; | |
98 | struct file_lock *fl; | |
bd61e0a9 JL |
99 | struct file_lock_context *flctx = inode->i_flctx; |
100 | struct list_head *list; | |
d5122201 | 101 | int status = 0; |
888e694c | 102 | |
bd61e0a9 | 103 | if (flctx == NULL) |
65b62a29 | 104 | goto out; |
314d7cc0 | 105 | |
bd61e0a9 | 106 | list = &flctx->flc_posix; |
6109c850 | 107 | spin_lock(&flctx->flc_lock); |
bd61e0a9 JL |
108 | restart: |
109 | list_for_each_entry(fl, list, fl_list) { | |
44f411c3 | 110 | if (nfs_file_open_context(fl->fl_file)->state != state) |
888e694c | 111 | continue; |
6109c850 | 112 | spin_unlock(&flctx->flc_lock); |
db4f2e63 | 113 | status = nfs4_lock_delegation_recall(fl, state, stateid); |
d5122201 | 114 | if (status < 0) |
3f09df70 | 115 | goto out; |
6109c850 | 116 | spin_lock(&flctx->flc_lock); |
888e694c | 117 | } |
bd61e0a9 JL |
118 | if (list == &flctx->flc_posix) { |
119 | list = &flctx->flc_flock; | |
120 | goto restart; | |
888e694c | 121 | } |
6109c850 | 122 | spin_unlock(&flctx->flc_lock); |
3f09df70 | 123 | out: |
888e694c TM |
124 | return status; |
125 | } | |
126 | ||
24311f88 TM |
127 | static int nfs_delegation_claim_opens(struct inode *inode, |
128 | const nfs4_stateid *stateid, fmode_t type) | |
1da177e4 LT |
129 | { |
130 | struct nfs_inode *nfsi = NFS_I(inode); | |
131 | struct nfs_open_context *ctx; | |
d25be546 | 132 | struct nfs4_state_owner *sp; |
1da177e4 | 133 | struct nfs4_state *state; |
d25be546 | 134 | unsigned int seq; |
888e694c | 135 | int err; |
1da177e4 LT |
136 | |
137 | again: | |
0de43976 TM |
138 | rcu_read_lock(); |
139 | list_for_each_entry_rcu(ctx, &nfsi->open_files, list) { | |
1da177e4 LT |
140 | state = ctx->state; |
141 | if (state == NULL) | |
142 | continue; | |
143 | if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) | |
144 | continue; | |
f8ebf7a8 TM |
145 | if (!nfs4_valid_open_stateid(state)) |
146 | continue; | |
f597c537 | 147 | if (!nfs4_stateid_match(&state->stateid, stateid)) |
90163027 | 148 | continue; |
0de43976 TM |
149 | if (!get_nfs_open_context(ctx)) |
150 | continue; | |
151 | rcu_read_unlock(); | |
d25be546 | 152 | sp = state->owner; |
65b62a29 TM |
153 | /* Block nfs4_proc_unlck */ |
154 | mutex_lock(&sp->so_delegreturn_mutex); | |
d25be546 | 155 | seq = raw_seqcount_begin(&sp->so_reclaim_seqcount); |
5eb8d18c | 156 | err = nfs4_open_delegation_recall(ctx, state, stateid); |
d25be546 | 157 | if (!err) |
44f411c3 | 158 | err = nfs_delegation_claim_locks(state, stateid); |
d25be546 TM |
159 | if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) |
160 | err = -EAGAIN; | |
65b62a29 | 161 | mutex_unlock(&sp->so_delegreturn_mutex); |
1da177e4 | 162 | put_nfs_open_context(ctx); |
888e694c | 163 | if (err != 0) |
d18cc1fd | 164 | return err; |
1da177e4 LT |
165 | goto again; |
166 | } | |
0de43976 | 167 | rcu_read_unlock(); |
d18cc1fd | 168 | return 0; |
1da177e4 LT |
169 | } |
170 | ||
d3978bb3 CL |
171 | /** |
172 | * nfs_inode_reclaim_delegation - process a delegation reclaim request | |
173 | * @inode: inode to process | |
174 | * @cred: credential to use for request | |
35156bff TM |
175 | * @type: delegation type |
176 | * @stateid: delegation stateid | |
177 | * @pagemod_limit: write delegation "space_limit" | |
d3978bb3 | 178 | * |
1da177e4 | 179 | */ |
a52458b4 | 180 | void nfs_inode_reclaim_delegation(struct inode *inode, const struct cred *cred, |
35156bff TM |
181 | fmode_t type, |
182 | const nfs4_stateid *stateid, | |
183 | unsigned long pagemod_limit) | |
1da177e4 | 184 | { |
8f649c37 | 185 | struct nfs_delegation *delegation; |
a52458b4 | 186 | const struct cred *oldcred = NULL; |
1da177e4 | 187 | |
8f649c37 TM |
188 | rcu_read_lock(); |
189 | delegation = rcu_dereference(NFS_I(inode)->delegation); | |
190 | if (delegation != NULL) { | |
191 | spin_lock(&delegation->lock); | |
192 | if (delegation->inode != NULL) { | |
35156bff TM |
193 | nfs4_stateid_copy(&delegation->stateid, stateid); |
194 | delegation->type = type; | |
195 | delegation->pagemod_limit = pagemod_limit; | |
8f649c37 | 196 | oldcred = delegation->cred; |
a52458b4 | 197 | delegation->cred = get_cred(cred); |
8f649c37 TM |
198 | clear_bit(NFS_DELEGATION_NEED_RECLAIM, |
199 | &delegation->flags); | |
8f649c37 | 200 | spin_unlock(&delegation->lock); |
8f649c37 | 201 | rcu_read_unlock(); |
a52458b4 | 202 | put_cred(oldcred); |
35156bff | 203 | trace_nfs4_reclaim_delegation(inode, type); |
b1a318de | 204 | return; |
8f649c37 | 205 | } |
b1a318de TM |
206 | /* We appear to have raced with a delegation return. */ |
207 | spin_unlock(&delegation->lock); | |
8f649c37 | 208 | } |
b1a318de | 209 | rcu_read_unlock(); |
35156bff | 210 | nfs_inode_set_delegation(inode, cred, type, stateid, pagemod_limit); |
1da177e4 LT |
211 | } |
212 | ||
57bfa891 TM |
213 | static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync) |
214 | { | |
215 | int res = 0; | |
216 | ||
869f9dfa TM |
217 | if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) |
218 | res = nfs4_proc_delegreturn(inode, | |
219 | delegation->cred, | |
220 | &delegation->stateid, | |
221 | issync); | |
57bfa891 TM |
222 | nfs_free_delegation(delegation); |
223 | return res; | |
224 | } | |
225 | ||
86e89489 TM |
226 | static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation) |
227 | { | |
228 | struct inode *inode = NULL; | |
229 | ||
230 | spin_lock(&delegation->lock); | |
231 | if (delegation->inode != NULL) | |
232 | inode = igrab(delegation->inode); | |
6f9449be TM |
233 | if (!inode) |
234 | set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags); | |
86e89489 TM |
235 | spin_unlock(&delegation->lock); |
236 | return inode; | |
237 | } | |
238 | ||
d25be546 TM |
239 | static struct nfs_delegation * |
240 | nfs_start_delegation_return_locked(struct nfs_inode *nfsi) | |
241 | { | |
242 | struct nfs_delegation *ret = NULL; | |
243 | struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation); | |
244 | ||
245 | if (delegation == NULL) | |
246 | goto out; | |
247 | spin_lock(&delegation->lock); | |
248 | if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) | |
249 | ret = delegation; | |
250 | spin_unlock(&delegation->lock); | |
251 | out: | |
252 | return ret; | |
253 | } | |
254 | ||
255 | static struct nfs_delegation * | |
256 | nfs_start_delegation_return(struct nfs_inode *nfsi) | |
257 | { | |
258 | struct nfs_delegation *delegation; | |
259 | ||
260 | rcu_read_lock(); | |
261 | delegation = nfs_start_delegation_return_locked(nfsi); | |
262 | rcu_read_unlock(); | |
263 | return delegation; | |
264 | } | |
265 | ||
266 | static void | |
267 | nfs_abort_delegation_return(struct nfs_delegation *delegation, | |
268 | struct nfs_client *clp) | |
269 | { | |
270 | ||
271 | spin_lock(&delegation->lock); | |
272 | clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags); | |
273 | set_bit(NFS_DELEGATION_RETURN, &delegation->flags); | |
274 | spin_unlock(&delegation->lock); | |
275 | set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state); | |
276 | } | |
277 | ||
dda4b225 CL |
278 | static struct nfs_delegation * |
279 | nfs_detach_delegation_locked(struct nfs_inode *nfsi, | |
d25be546 TM |
280 | struct nfs_delegation *delegation, |
281 | struct nfs_client *clp) | |
57bfa891 | 282 | { |
d25be546 | 283 | struct nfs_delegation *deleg_cur = |
17d2c0a0 | 284 | rcu_dereference_protected(nfsi->delegation, |
d25be546 | 285 | lockdep_is_held(&clp->cl_lock)); |
57bfa891 | 286 | |
d25be546 TM |
287 | if (deleg_cur == NULL || delegation != deleg_cur) |
288 | return NULL; | |
dda4b225 | 289 | |
34310430 | 290 | spin_lock(&delegation->lock); |
d25be546 | 291 | set_bit(NFS_DELEGATION_RETURNING, &delegation->flags); |
57bfa891 | 292 | list_del_rcu(&delegation->super_list); |
86e89489 | 293 | delegation->inode = NULL; |
57bfa891 | 294 | rcu_assign_pointer(nfsi->delegation, NULL); |
34310430 | 295 | spin_unlock(&delegation->lock); |
57bfa891 | 296 | return delegation; |
57bfa891 TM |
297 | } |
298 | ||
dda4b225 | 299 | static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi, |
d25be546 TM |
300 | struct nfs_delegation *delegation, |
301 | struct nfs_server *server) | |
dda4b225 | 302 | { |
d3978bb3 | 303 | struct nfs_client *clp = server->nfs_client; |
dda4b225 CL |
304 | |
305 | spin_lock(&clp->cl_lock); | |
d25be546 | 306 | delegation = nfs_detach_delegation_locked(nfsi, delegation, clp); |
dda4b225 CL |
307 | spin_unlock(&clp->cl_lock); |
308 | return delegation; | |
309 | } | |
310 | ||
d25be546 TM |
311 | static struct nfs_delegation * |
312 | nfs_inode_detach_delegation(struct inode *inode) | |
313 | { | |
314 | struct nfs_inode *nfsi = NFS_I(inode); | |
315 | struct nfs_server *server = NFS_SERVER(inode); | |
316 | struct nfs_delegation *delegation; | |
317 | ||
318 | delegation = nfs_start_delegation_return(nfsi); | |
319 | if (delegation == NULL) | |
320 | return NULL; | |
321 | return nfs_detach_delegation(nfsi, delegation, server); | |
322 | } | |
323 | ||
cf6726e2 TM |
324 | static void |
325 | nfs_update_inplace_delegation(struct nfs_delegation *delegation, | |
326 | const struct nfs_delegation *update) | |
327 | { | |
328 | if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) { | |
329 | delegation->stateid.seqid = update->stateid.seqid; | |
330 | smp_wmb(); | |
331 | delegation->type = update->type; | |
332 | } | |
333 | } | |
334 | ||
d3978bb3 CL |
335 | /** |
336 | * nfs_inode_set_delegation - set up a delegation on an inode | |
337 | * @inode: inode to which delegation applies | |
338 | * @cred: cred to use for subsequent delegation processing | |
35156bff TM |
339 | * @type: delegation type |
340 | * @stateid: delegation stateid | |
341 | * @pagemod_limit: write delegation "space_limit" | |
d3978bb3 CL |
342 | * |
343 | * Returns zero on success, or a negative errno value. | |
1da177e4 | 344 | */ |
a52458b4 | 345 | int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred, |
35156bff TM |
346 | fmode_t type, |
347 | const nfs4_stateid *stateid, | |
348 | unsigned long pagemod_limit) | |
1da177e4 | 349 | { |
d3978bb3 CL |
350 | struct nfs_server *server = NFS_SERVER(inode); |
351 | struct nfs_client *clp = server->nfs_client; | |
1da177e4 | 352 | struct nfs_inode *nfsi = NFS_I(inode); |
17d2c0a0 | 353 | struct nfs_delegation *delegation, *old_delegation; |
57bfa891 | 354 | struct nfs_delegation *freeme = NULL; |
1da177e4 LT |
355 | int status = 0; |
356 | ||
8535b2be | 357 | delegation = kmalloc(sizeof(*delegation), GFP_NOFS); |
1da177e4 LT |
358 | if (delegation == NULL) |
359 | return -ENOMEM; | |
35156bff TM |
360 | nfs4_stateid_copy(&delegation->stateid, stateid); |
361 | delegation->type = type; | |
362 | delegation->pagemod_limit = pagemod_limit; | |
1eb5d98f | 363 | delegation->change_attr = inode_peek_iversion_raw(inode); |
a52458b4 | 364 | delegation->cred = get_cred(cred); |
1da177e4 | 365 | delegation->inode = inode; |
b7391f44 | 366 | delegation->flags = 1<<NFS_DELEGATION_REFERENCED; |
34310430 | 367 | spin_lock_init(&delegation->lock); |
1da177e4 LT |
368 | |
369 | spin_lock(&clp->cl_lock); | |
17d2c0a0 | 370 | old_delegation = rcu_dereference_protected(nfsi->delegation, |
d3978bb3 | 371 | lockdep_is_held(&clp->cl_lock)); |
17d2c0a0 | 372 | if (old_delegation != NULL) { |
cf6726e2 TM |
373 | /* Is this an update of the existing delegation? */ |
374 | if (nfs4_stateid_match_other(&old_delegation->stateid, | |
375 | &delegation->stateid)) { | |
376 | nfs_update_inplace_delegation(old_delegation, | |
377 | delegation); | |
57bfa891 | 378 | goto out; |
1da177e4 | 379 | } |
57bfa891 TM |
380 | /* |
381 | * Deal with broken servers that hand out two | |
382 | * delegations for the same file. | |
17280175 TM |
383 | * Allow for upgrades to a WRITE delegation, but |
384 | * nothing else. | |
57bfa891 TM |
385 | */ |
386 | dfprintk(FILE, "%s: server %s handed out " | |
387 | "a duplicate delegation!\n", | |
3110ff80 | 388 | __func__, clp->cl_hostname); |
17280175 TM |
389 | if (delegation->type == old_delegation->type || |
390 | !(delegation->type & FMODE_WRITE)) { | |
57bfa891 TM |
391 | freeme = delegation; |
392 | delegation = NULL; | |
393 | goto out; | |
394 | } | |
ade04647 TM |
395 | if (test_and_set_bit(NFS_DELEGATION_RETURNING, |
396 | &old_delegation->flags)) | |
397 | goto out; | |
398 | freeme = nfs_detach_delegation_locked(nfsi, | |
d25be546 TM |
399 | old_delegation, clp); |
400 | if (freeme == NULL) | |
401 | goto out; | |
1da177e4 | 402 | } |
38942ba2 | 403 | list_add_tail_rcu(&delegation->super_list, &server->delegations); |
57bfa891 TM |
404 | rcu_assign_pointer(nfsi->delegation, delegation); |
405 | delegation = NULL; | |
412c77ce | 406 | |
35156bff | 407 | trace_nfs4_set_delegation(inode, type); |
412c77ce | 408 | |
97c2c17a TM |
409 | spin_lock(&inode->i_lock); |
410 | if (NFS_I(inode)->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME)) | |
411 | NFS_I(inode)->cache_validity |= NFS_INO_REVAL_FORCED; | |
412 | spin_unlock(&inode->i_lock); | |
57bfa891 | 413 | out: |
1da177e4 | 414 | spin_unlock(&clp->cl_lock); |
603c83da TM |
415 | if (delegation != NULL) |
416 | nfs_free_delegation(delegation); | |
57bfa891 TM |
417 | if (freeme != NULL) |
418 | nfs_do_return_delegation(inode, freeme, 0); | |
1da177e4 LT |
419 | return status; |
420 | } | |
421 | ||
1da177e4 LT |
422 | /* |
423 | * Basic procedure for returning a delegation to the server | |
424 | */ | |
d25be546 | 425 | static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync) |
1da177e4 | 426 | { |
d25be546 | 427 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
1da177e4 | 428 | struct nfs_inode *nfsi = NFS_I(inode); |
869f9dfa | 429 | int err = 0; |
1da177e4 | 430 | |
d25be546 TM |
431 | if (delegation == NULL) |
432 | return 0; | |
433 | do { | |
869f9dfa TM |
434 | if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) |
435 | break; | |
24311f88 TM |
436 | err = nfs_delegation_claim_opens(inode, &delegation->stateid, |
437 | delegation->type); | |
d25be546 TM |
438 | if (!issync || err != -EAGAIN) |
439 | break; | |
440 | /* | |
441 | * Guard against state recovery | |
442 | */ | |
443 | err = nfs4_wait_clnt_recover(clp); | |
444 | } while (err == 0); | |
445 | ||
446 | if (err) { | |
447 | nfs_abort_delegation_return(delegation, clp); | |
448 | goto out; | |
449 | } | |
450 | if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode))) | |
d18cc1fd | 451 | goto out; |
1da177e4 | 452 | |
d18cc1fd TM |
453 | err = nfs_do_return_delegation(inode, delegation, issync); |
454 | out: | |
455 | return err; | |
90163027 TM |
456 | } |
457 | ||
b757144f TM |
458 | static bool nfs_delegation_need_return(struct nfs_delegation *delegation) |
459 | { | |
460 | bool ret = false; | |
461 | ||
ec3ca4e5 TM |
462 | if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) |
463 | goto out; | |
b757144f TM |
464 | if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags)) |
465 | ret = true; | |
466 | if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) { | |
467 | struct inode *inode; | |
468 | ||
469 | spin_lock(&delegation->lock); | |
470 | inode = delegation->inode; | |
471 | if (inode && list_empty(&NFS_I(inode)->open_files)) | |
472 | ret = true; | |
473 | spin_unlock(&delegation->lock); | |
474 | } | |
ec3ca4e5 | 475 | out: |
b757144f TM |
476 | return ret; |
477 | } | |
478 | ||
d3978bb3 CL |
479 | /** |
480 | * nfs_client_return_marked_delegations - return previously marked delegations | |
481 | * @clp: nfs_client to process | |
482 | * | |
dc327ed4 TM |
483 | * Note that this function is designed to be called by the state |
484 | * manager thread. For this reason, it cannot flush the dirty data, | |
485 | * since that could deadlock in case of a state recovery error. | |
486 | * | |
d3978bb3 | 487 | * Returns zero on success, or a negative errno value. |
515d8611 | 488 | */ |
d18cc1fd | 489 | int nfs_client_return_marked_delegations(struct nfs_client *clp) |
515d8611 TM |
490 | { |
491 | struct nfs_delegation *delegation; | |
e04bbf6b | 492 | struct nfs_delegation *prev; |
d3978bb3 | 493 | struct nfs_server *server; |
515d8611 | 494 | struct inode *inode; |
e04bbf6b N |
495 | struct inode *place_holder = NULL; |
496 | struct nfs_delegation *place_holder_deleg = NULL; | |
d18cc1fd | 497 | int err = 0; |
515d8611 TM |
498 | |
499 | restart: | |
e04bbf6b N |
500 | /* |
501 | * To avoid quadratic looping we hold a reference | |
502 | * to an inode place_holder. Each time we restart, we | |
503 | * list nfs_servers from the server of that inode, and | |
504 | * delegation in the server from the delegations of that | |
505 | * inode. | |
506 | * prev is an RCU-protected pointer to a delegation which | |
507 | * wasn't marked for return and might be a good choice for | |
508 | * the next place_holder. | |
509 | */ | |
515d8611 | 510 | rcu_read_lock(); |
e04bbf6b N |
511 | prev = NULL; |
512 | if (place_holder) | |
513 | server = NFS_SERVER(place_holder); | |
514 | else | |
515 | server = list_entry_rcu(clp->cl_superblocks.next, | |
516 | struct nfs_server, client_link); | |
517 | list_for_each_entry_from_rcu(server, &clp->cl_superblocks, client_link) { | |
518 | delegation = NULL; | |
519 | if (place_holder && server == NFS_SERVER(place_holder)) | |
520 | delegation = rcu_dereference(NFS_I(place_holder)->delegation); | |
521 | if (!delegation || delegation != place_holder_deleg) | |
522 | delegation = list_entry_rcu(server->delegations.next, | |
523 | struct nfs_delegation, super_list); | |
524 | list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) { | |
525 | struct inode *to_put = NULL; | |
526 | ||
527 | if (!nfs_delegation_need_return(delegation)) { | |
528 | prev = delegation; | |
d3978bb3 | 529 | continue; |
e04bbf6b | 530 | } |
9f0f8e12 | 531 | if (!nfs_sb_active(server->super)) |
f3893491 | 532 | break; /* continue in outer loop */ |
e04bbf6b N |
533 | |
534 | if (prev) { | |
535 | struct inode *tmp; | |
536 | ||
537 | tmp = nfs_delegation_grab_inode(prev); | |
538 | if (tmp) { | |
539 | to_put = place_holder; | |
540 | place_holder = tmp; | |
541 | place_holder_deleg = prev; | |
542 | } | |
543 | } | |
544 | ||
9f0f8e12 TM |
545 | inode = nfs_delegation_grab_inode(delegation); |
546 | if (inode == NULL) { | |
547 | rcu_read_unlock(); | |
e04bbf6b N |
548 | if (to_put) |
549 | iput(to_put); | |
9f0f8e12 TM |
550 | nfs_sb_deactive(server->super); |
551 | goto restart; | |
552 | } | |
d25be546 | 553 | delegation = nfs_start_delegation_return_locked(NFS_I(inode)); |
d3978bb3 CL |
554 | rcu_read_unlock(); |
555 | ||
e04bbf6b N |
556 | if (to_put) |
557 | iput(to_put); | |
558 | ||
d25be546 | 559 | err = nfs_end_delegation_return(inode, delegation, 0); |
d3978bb3 | 560 | iput(inode); |
9f0f8e12 | 561 | nfs_sb_deactive(server->super); |
3ca951b6 | 562 | cond_resched(); |
d3978bb3 CL |
563 | if (!err) |
564 | goto restart; | |
565 | set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state); | |
e04bbf6b N |
566 | if (place_holder) |
567 | iput(place_holder); | |
d3978bb3 | 568 | return err; |
d18cc1fd | 569 | } |
515d8611 TM |
570 | } |
571 | rcu_read_unlock(); | |
e04bbf6b N |
572 | if (place_holder) |
573 | iput(place_holder); | |
d18cc1fd | 574 | return 0; |
515d8611 TM |
575 | } |
576 | ||
d3978bb3 CL |
577 | /** |
578 | * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens | |
579 | * @inode: inode to process | |
580 | * | |
581 | * Does not protect against delegation reclaims, therefore really only safe | |
582 | * to be called from nfs4_clear_inode(). | |
e6f81075 TM |
583 | */ |
584 | void nfs_inode_return_delegation_noreclaim(struct inode *inode) | |
585 | { | |
e6f81075 TM |
586 | struct nfs_delegation *delegation; |
587 | ||
d25be546 TM |
588 | delegation = nfs_inode_detach_delegation(inode); |
589 | if (delegation != NULL) | |
5fcdfacc | 590 | nfs_do_return_delegation(inode, delegation, 1); |
e6f81075 TM |
591 | } |
592 | ||
d3978bb3 CL |
593 | /** |
594 | * nfs_inode_return_delegation - synchronously return a delegation | |
595 | * @inode: inode to process | |
596 | * | |
c57d1bc5 TM |
597 | * This routine will always flush any dirty data to disk on the |
598 | * assumption that if we need to return the delegation, then | |
599 | * we should stop caching. | |
600 | * | |
d3978bb3 CL |
601 | * Returns zero on success, or a negative errno value. |
602 | */ | |
57ec14c5 | 603 | int nfs4_inode_return_delegation(struct inode *inode) |
90163027 | 604 | { |
90163027 TM |
605 | struct nfs_inode *nfsi = NFS_I(inode); |
606 | struct nfs_delegation *delegation; | |
607 | int err = 0; | |
608 | ||
c57d1bc5 | 609 | nfs_wb_all(inode); |
d25be546 TM |
610 | delegation = nfs_start_delegation_return(nfsi); |
611 | if (delegation != NULL) | |
612 | err = nfs_end_delegation_return(inode, delegation, 1); | |
90163027 | 613 | return err; |
1da177e4 LT |
614 | } |
615 | ||
c01d3645 TM |
616 | /** |
617 | * nfs4_inode_make_writeable | |
618 | * @inode: pointer to inode | |
619 | * | |
620 | * Make the inode writeable by returning the delegation if necessary | |
621 | * | |
622 | * Returns zero on success, or a negative errno value. | |
623 | */ | |
624 | int nfs4_inode_make_writeable(struct inode *inode) | |
625 | { | |
626 | if (!nfs4_has_session(NFS_SERVER(inode)->nfs_client) || | |
627 | !nfs4_check_delegation(inode, FMODE_WRITE)) | |
628 | return nfs4_inode_return_delegation(inode); | |
629 | return 0; | |
630 | } | |
631 | ||
b757144f TM |
632 | static void nfs_mark_return_if_closed_delegation(struct nfs_server *server, |
633 | struct nfs_delegation *delegation) | |
634 | { | |
635 | set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags); | |
636 | set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state); | |
637 | } | |
638 | ||
ed1e6211 TM |
639 | static void nfs_mark_return_delegation(struct nfs_server *server, |
640 | struct nfs_delegation *delegation) | |
6411bd4a TM |
641 | { |
642 | set_bit(NFS_DELEGATION_RETURN, &delegation->flags); | |
ed1e6211 | 643 | set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state); |
6411bd4a TM |
644 | } |
645 | ||
5c31e236 TM |
646 | static bool nfs_server_mark_return_all_delegations(struct nfs_server *server) |
647 | { | |
648 | struct nfs_delegation *delegation; | |
649 | bool ret = false; | |
650 | ||
651 | list_for_each_entry_rcu(delegation, &server->delegations, super_list) { | |
652 | nfs_mark_return_delegation(server, delegation); | |
653 | ret = true; | |
654 | } | |
655 | return ret; | |
656 | } | |
657 | ||
b02ba0b6 TM |
658 | static void nfs_client_mark_return_all_delegations(struct nfs_client *clp) |
659 | { | |
660 | struct nfs_server *server; | |
661 | ||
662 | rcu_read_lock(); | |
663 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) | |
664 | nfs_server_mark_return_all_delegations(server); | |
665 | rcu_read_unlock(); | |
666 | } | |
667 | ||
668 | static void nfs_delegation_run_state_manager(struct nfs_client *clp) | |
669 | { | |
670 | if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) | |
671 | nfs4_schedule_state_manager(clp); | |
672 | } | |
673 | ||
674 | /** | |
675 | * nfs_expire_all_delegations | |
676 | * @clp: client to process | |
677 | * | |
678 | */ | |
679 | void nfs_expire_all_delegations(struct nfs_client *clp) | |
680 | { | |
681 | nfs_client_mark_return_all_delegations(clp); | |
682 | nfs_delegation_run_state_manager(clp); | |
683 | } | |
684 | ||
d3978bb3 CL |
685 | /** |
686 | * nfs_super_return_all_delegations - return delegations for one superblock | |
302fad7b | 687 | * @server: pointer to nfs_server to process |
d3978bb3 | 688 | * |
1da177e4 | 689 | */ |
eeebf916 | 690 | void nfs_server_return_all_delegations(struct nfs_server *server) |
1da177e4 | 691 | { |
d3978bb3 | 692 | struct nfs_client *clp = server->nfs_client; |
5c31e236 | 693 | bool need_wait; |
1da177e4 LT |
694 | |
695 | if (clp == NULL) | |
696 | return; | |
d3978bb3 | 697 | |
8383e460 | 698 | rcu_read_lock(); |
5c31e236 | 699 | need_wait = nfs_server_mark_return_all_delegations(server); |
8383e460 | 700 | rcu_read_unlock(); |
d3978bb3 | 701 | |
5c31e236 | 702 | if (need_wait) { |
d18cc1fd | 703 | nfs4_schedule_state_manager(clp); |
5c31e236 TM |
704 | nfs4_wait_clnt_recover(clp); |
705 | } | |
515d8611 TM |
706 | } |
707 | ||
826e0013 | 708 | static void nfs_mark_return_unused_delegation_types(struct nfs_server *server, |
d3978bb3 | 709 | fmode_t flags) |
515d8611 TM |
710 | { |
711 | struct nfs_delegation *delegation; | |
712 | ||
d3978bb3 | 713 | list_for_each_entry_rcu(delegation, &server->delegations, super_list) { |
c79571a5 AB |
714 | if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE)) |
715 | continue; | |
716 | if (delegation->type & flags) | |
826e0013 | 717 | nfs_mark_return_if_closed_delegation(server, delegation); |
707fb4b3 | 718 | } |
d3978bb3 CL |
719 | } |
720 | ||
826e0013 | 721 | static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp, |
d3978bb3 CL |
722 | fmode_t flags) |
723 | { | |
724 | struct nfs_server *server; | |
725 | ||
726 | rcu_read_lock(); | |
727 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) | |
826e0013 | 728 | nfs_mark_return_unused_delegation_types(server, flags); |
515d8611 | 729 | rcu_read_unlock(); |
1da177e4 LT |
730 | } |
731 | ||
41020b67 TM |
732 | static void nfs_mark_delegation_revoked(struct nfs_server *server, |
733 | struct nfs_delegation *delegation) | |
734 | { | |
735 | set_bit(NFS_DELEGATION_REVOKED, &delegation->flags); | |
059b43e9 | 736 | delegation->stateid.type = NFS4_INVALID_STATEID_TYPE; |
41020b67 TM |
737 | nfs_mark_return_delegation(server, delegation); |
738 | } | |
739 | ||
740 | static bool nfs_revoke_delegation(struct inode *inode, | |
741 | const nfs4_stateid *stateid) | |
869f9dfa TM |
742 | { |
743 | struct nfs_delegation *delegation; | |
7f048831 | 744 | nfs4_stateid tmp; |
41020b67 TM |
745 | bool ret = false; |
746 | ||
869f9dfa TM |
747 | rcu_read_lock(); |
748 | delegation = rcu_dereference(NFS_I(inode)->delegation); | |
41020b67 TM |
749 | if (delegation == NULL) |
750 | goto out; | |
7f048831 TM |
751 | if (stateid == NULL) { |
752 | nfs4_stateid_copy(&tmp, &delegation->stateid); | |
753 | stateid = &tmp; | |
754 | } else if (!nfs4_stateid_match(stateid, &delegation->stateid)) | |
41020b67 TM |
755 | goto out; |
756 | nfs_mark_delegation_revoked(NFS_SERVER(inode), delegation); | |
757 | ret = true; | |
758 | out: | |
869f9dfa | 759 | rcu_read_unlock(); |
7f048831 TM |
760 | if (ret) |
761 | nfs_inode_find_state_and_recover(inode, stateid); | |
41020b67 | 762 | return ret; |
869f9dfa TM |
763 | } |
764 | ||
41020b67 TM |
765 | void nfs_remove_bad_delegation(struct inode *inode, |
766 | const nfs4_stateid *stateid) | |
a1d0b5ee TM |
767 | { |
768 | struct nfs_delegation *delegation; | |
769 | ||
41020b67 TM |
770 | if (!nfs_revoke_delegation(inode, stateid)) |
771 | return; | |
d25be546 | 772 | delegation = nfs_inode_detach_delegation(inode); |
7f048831 | 773 | if (delegation) |
a1d0b5ee | 774 | nfs_free_delegation(delegation); |
a1d0b5ee | 775 | } |
9cb81968 | 776 | EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation); |
a1d0b5ee | 777 | |
d3978bb3 | 778 | /** |
826e0013 | 779 | * nfs_expire_unused_delegation_types |
d3978bb3 CL |
780 | * @clp: client to process |
781 | * @flags: delegation types to expire | |
782 | * | |
783 | */ | |
826e0013 | 784 | void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags) |
58d9714a | 785 | { |
826e0013 | 786 | nfs_client_mark_return_unused_delegation_types(clp, flags); |
b0d3ded1 | 787 | nfs_delegation_run_state_manager(clp); |
58d9714a TM |
788 | } |
789 | ||
d3978bb3 | 790 | static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server) |
b7391f44 TM |
791 | { |
792 | struct nfs_delegation *delegation; | |
793 | ||
d3978bb3 | 794 | list_for_each_entry_rcu(delegation, &server->delegations, super_list) { |
b7391f44 TM |
795 | if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags)) |
796 | continue; | |
b757144f | 797 | nfs_mark_return_if_closed_delegation(server, delegation); |
b7391f44 | 798 | } |
b7391f44 TM |
799 | } |
800 | ||
d3978bb3 CL |
801 | /** |
802 | * nfs_expire_unreferenced_delegations - Eliminate unused delegations | |
803 | * @clp: nfs_client to process | |
804 | * | |
805 | */ | |
b7391f44 TM |
806 | void nfs_expire_unreferenced_delegations(struct nfs_client *clp) |
807 | { | |
d3978bb3 CL |
808 | struct nfs_server *server; |
809 | ||
810 | rcu_read_lock(); | |
811 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) | |
812 | nfs_mark_return_unreferenced_delegations(server); | |
813 | rcu_read_unlock(); | |
814 | ||
b7391f44 TM |
815 | nfs_delegation_run_state_manager(clp); |
816 | } | |
817 | ||
d3978bb3 CL |
818 | /** |
819 | * nfs_async_inode_return_delegation - asynchronously return a delegation | |
820 | * @inode: inode to process | |
8e663f0e | 821 | * @stateid: state ID information |
d3978bb3 CL |
822 | * |
823 | * Returns zero on success, or a negative errno value. | |
1da177e4 | 824 | */ |
d3978bb3 CL |
825 | int nfs_async_inode_return_delegation(struct inode *inode, |
826 | const nfs4_stateid *stateid) | |
1da177e4 | 827 | { |
ed1e6211 TM |
828 | struct nfs_server *server = NFS_SERVER(inode); |
829 | struct nfs_client *clp = server->nfs_client; | |
6411bd4a | 830 | struct nfs_delegation *delegation; |
1da177e4 | 831 | |
6411bd4a TM |
832 | rcu_read_lock(); |
833 | delegation = rcu_dereference(NFS_I(inode)->delegation); | |
755a48a7 TM |
834 | if (delegation == NULL) |
835 | goto out_enoent; | |
4816fdad TM |
836 | if (stateid != NULL && |
837 | !clp->cl_mvops->match_stateid(&delegation->stateid, stateid)) | |
755a48a7 | 838 | goto out_enoent; |
ed1e6211 | 839 | nfs_mark_return_delegation(server, delegation); |
6411bd4a | 840 | rcu_read_unlock(); |
d3978bb3 | 841 | |
6411bd4a TM |
842 | nfs_delegation_run_state_manager(clp); |
843 | return 0; | |
755a48a7 TM |
844 | out_enoent: |
845 | rcu_read_unlock(); | |
846 | return -ENOENT; | |
1da177e4 LT |
847 | } |
848 | ||
d3978bb3 CL |
849 | static struct inode * |
850 | nfs_delegation_find_inode_server(struct nfs_server *server, | |
851 | const struct nfs_fh *fhandle) | |
1da177e4 LT |
852 | { |
853 | struct nfs_delegation *delegation; | |
e39d8a18 | 854 | struct inode *freeme, *res = NULL; |
d3978bb3 CL |
855 | |
856 | list_for_each_entry_rcu(delegation, &server->delegations, super_list) { | |
86e89489 TM |
857 | spin_lock(&delegation->lock); |
858 | if (delegation->inode != NULL && | |
859 | nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) { | |
e39d8a18 TM |
860 | freeme = igrab(delegation->inode); |
861 | if (freeme && nfs_sb_active(freeme->i_sb)) | |
862 | res = freeme; | |
6c342655 TM |
863 | spin_unlock(&delegation->lock); |
864 | if (res != NULL) | |
865 | return res; | |
e39d8a18 TM |
866 | if (freeme) { |
867 | rcu_read_unlock(); | |
868 | iput(freeme); | |
869 | rcu_read_lock(); | |
870 | } | |
6c342655 | 871 | return ERR_PTR(-EAGAIN); |
1da177e4 | 872 | } |
86e89489 | 873 | spin_unlock(&delegation->lock); |
1da177e4 | 874 | } |
6c342655 | 875 | return ERR_PTR(-ENOENT); |
d3978bb3 CL |
876 | } |
877 | ||
878 | /** | |
879 | * nfs_delegation_find_inode - retrieve the inode associated with a delegation | |
880 | * @clp: client state handle | |
881 | * @fhandle: filehandle from a delegation recall | |
882 | * | |
883 | * Returns pointer to inode matching "fhandle," or NULL if a matching inode | |
884 | * cannot be found. | |
885 | */ | |
886 | struct inode *nfs_delegation_find_inode(struct nfs_client *clp, | |
887 | const struct nfs_fh *fhandle) | |
888 | { | |
889 | struct nfs_server *server; | |
6c342655 | 890 | struct inode *res; |
d3978bb3 CL |
891 | |
892 | rcu_read_lock(); | |
893 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { | |
894 | res = nfs_delegation_find_inode_server(server, fhandle); | |
d5681f59 AS |
895 | if (res != ERR_PTR(-ENOENT)) { |
896 | rcu_read_unlock(); | |
6c342655 | 897 | return res; |
d5681f59 | 898 | } |
d3978bb3 | 899 | } |
8383e460 | 900 | rcu_read_unlock(); |
6c342655 | 901 | return ERR_PTR(-ENOENT); |
1da177e4 LT |
902 | } |
903 | ||
d3978bb3 CL |
904 | static void nfs_delegation_mark_reclaim_server(struct nfs_server *server) |
905 | { | |
906 | struct nfs_delegation *delegation; | |
907 | ||
45870d69 TM |
908 | list_for_each_entry_rcu(delegation, &server->delegations, super_list) { |
909 | /* | |
910 | * If the delegation may have been admin revoked, then we | |
911 | * cannot reclaim it. | |
912 | */ | |
913 | if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) | |
914 | continue; | |
d3978bb3 | 915 | set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags); |
45870d69 | 916 | } |
d3978bb3 CL |
917 | } |
918 | ||
919 | /** | |
920 | * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed | |
921 | * @clp: nfs_client to process | |
922 | * | |
1da177e4 | 923 | */ |
adfa6f98 | 924 | void nfs_delegation_mark_reclaim(struct nfs_client *clp) |
1da177e4 | 925 | { |
d3978bb3 CL |
926 | struct nfs_server *server; |
927 | ||
8383e460 | 928 | rcu_read_lock(); |
d3978bb3 CL |
929 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) |
930 | nfs_delegation_mark_reclaim_server(server); | |
8383e460 | 931 | rcu_read_unlock(); |
1da177e4 LT |
932 | } |
933 | ||
d3978bb3 CL |
934 | /** |
935 | * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done | |
936 | * @clp: nfs_client to process | |
937 | * | |
1da177e4 | 938 | */ |
adfa6f98 | 939 | void nfs_delegation_reap_unclaimed(struct nfs_client *clp) |
1da177e4 | 940 | { |
8383e460 | 941 | struct nfs_delegation *delegation; |
d3978bb3 | 942 | struct nfs_server *server; |
86e89489 | 943 | struct inode *inode; |
d3978bb3 | 944 | |
8383e460 TM |
945 | restart: |
946 | rcu_read_lock(); | |
d3978bb3 CL |
947 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { |
948 | list_for_each_entry_rcu(delegation, &server->delegations, | |
949 | super_list) { | |
6f9449be TM |
950 | if (test_bit(NFS_DELEGATION_INODE_FREEING, |
951 | &delegation->flags) || | |
952 | test_bit(NFS_DELEGATION_RETURNING, | |
953 | &delegation->flags) || | |
954 | test_bit(NFS_DELEGATION_NEED_RECLAIM, | |
d3978bb3 CL |
955 | &delegation->flags) == 0) |
956 | continue; | |
9f0f8e12 | 957 | if (!nfs_sb_active(server->super)) |
f3893491 | 958 | break; /* continue in outer loop */ |
9f0f8e12 TM |
959 | inode = nfs_delegation_grab_inode(delegation); |
960 | if (inode == NULL) { | |
961 | rcu_read_unlock(); | |
962 | nfs_sb_deactive(server->super); | |
963 | goto restart; | |
964 | } | |
b04b22f4 | 965 | delegation = nfs_start_delegation_return_locked(NFS_I(inode)); |
d3978bb3 | 966 | rcu_read_unlock(); |
b04b22f4 TM |
967 | if (delegation != NULL) { |
968 | delegation = nfs_detach_delegation(NFS_I(inode), | |
969 | delegation, server); | |
970 | if (delegation != NULL) | |
971 | nfs_free_delegation(delegation); | |
972 | } | |
d3978bb3 | 973 | iput(inode); |
9f0f8e12 | 974 | nfs_sb_deactive(server->super); |
3ca951b6 | 975 | cond_resched(); |
d3978bb3 CL |
976 | goto restart; |
977 | } | |
1da177e4 | 978 | } |
8383e460 | 979 | rcu_read_unlock(); |
1da177e4 | 980 | } |
3e4f6290 | 981 | |
bb3d1a3b TM |
982 | static inline bool nfs4_server_rebooted(const struct nfs_client *clp) |
983 | { | |
984 | return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) | | |
985 | BIT(NFS4CLNT_LEASE_EXPIRED) | | |
986 | BIT(NFS4CLNT_SESSION_RESET))) != 0; | |
987 | } | |
988 | ||
45870d69 TM |
989 | static void nfs_mark_test_expired_delegation(struct nfs_server *server, |
990 | struct nfs_delegation *delegation) | |
991 | { | |
059b43e9 TM |
992 | if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE) |
993 | return; | |
45870d69 TM |
994 | clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags); |
995 | set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags); | |
996 | set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state); | |
997 | } | |
998 | ||
bb3d1a3b TM |
999 | static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server, |
1000 | struct inode *inode) | |
1001 | { | |
1002 | struct nfs_delegation *delegation; | |
1003 | ||
1004 | rcu_read_lock(); | |
1005 | delegation = rcu_dereference(NFS_I(inode)->delegation); | |
1006 | if (delegation) | |
1007 | nfs_mark_test_expired_delegation(server, delegation); | |
1008 | rcu_read_unlock(); | |
1009 | ||
1010 | } | |
1011 | ||
45870d69 TM |
1012 | static void nfs_delegation_mark_test_expired_server(struct nfs_server *server) |
1013 | { | |
1014 | struct nfs_delegation *delegation; | |
1015 | ||
1016 | list_for_each_entry_rcu(delegation, &server->delegations, super_list) | |
1017 | nfs_mark_test_expired_delegation(server, delegation); | |
1018 | } | |
1019 | ||
1020 | /** | |
1021 | * nfs_mark_test_expired_all_delegations - mark all delegations for testing | |
1022 | * @clp: nfs_client to process | |
1023 | * | |
1024 | * Iterates through all the delegations associated with this server and | |
1025 | * marks them as needing to be checked for validity. | |
1026 | */ | |
1027 | void nfs_mark_test_expired_all_delegations(struct nfs_client *clp) | |
1028 | { | |
1029 | struct nfs_server *server; | |
1030 | ||
1031 | rcu_read_lock(); | |
1032 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) | |
1033 | nfs_delegation_mark_test_expired_server(server); | |
1034 | rcu_read_unlock(); | |
1035 | } | |
1036 | ||
8ca017c8 SM |
1037 | /** |
1038 | * nfs_test_expired_all_delegations - test all delegations for a client | |
1039 | * @clp: nfs_client to process | |
1040 | * | |
1041 | * Helper for handling "recallable state revoked" status from server. | |
1042 | */ | |
1043 | void nfs_test_expired_all_delegations(struct nfs_client *clp) | |
1044 | { | |
1045 | nfs_mark_test_expired_all_delegations(clp); | |
1046 | nfs4_schedule_state_manager(clp); | |
1047 | } | |
1048 | ||
ad114089 TM |
1049 | static void |
1050 | nfs_delegation_test_free_expired(struct inode *inode, | |
1051 | nfs4_stateid *stateid, | |
1052 | const struct cred *cred) | |
1053 | { | |
1054 | struct nfs_server *server = NFS_SERVER(inode); | |
1055 | const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops; | |
1056 | int status; | |
1057 | ||
1058 | if (!cred) | |
1059 | return; | |
1060 | status = ops->test_and_free_expired(server, stateid, cred); | |
1061 | if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) | |
1062 | nfs_remove_bad_delegation(inode, stateid); | |
1063 | } | |
1064 | ||
45870d69 TM |
1065 | /** |
1066 | * nfs_reap_expired_delegations - reap expired delegations | |
1067 | * @clp: nfs_client to process | |
1068 | * | |
1069 | * Iterates through all the delegations associated with this server and | |
1070 | * checks if they have may have been revoked. This function is usually | |
1071 | * expected to be called in cases where the server may have lost its | |
1072 | * lease. | |
1073 | */ | |
1074 | void nfs_reap_expired_delegations(struct nfs_client *clp) | |
1075 | { | |
45870d69 TM |
1076 | struct nfs_delegation *delegation; |
1077 | struct nfs_server *server; | |
1078 | struct inode *inode; | |
a52458b4 | 1079 | const struct cred *cred; |
45870d69 TM |
1080 | nfs4_stateid stateid; |
1081 | ||
1082 | restart: | |
1083 | rcu_read_lock(); | |
1084 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { | |
1085 | list_for_each_entry_rcu(delegation, &server->delegations, | |
1086 | super_list) { | |
6f9449be TM |
1087 | if (test_bit(NFS_DELEGATION_INODE_FREEING, |
1088 | &delegation->flags) || | |
1089 | test_bit(NFS_DELEGATION_RETURNING, | |
1090 | &delegation->flags) || | |
1091 | test_bit(NFS_DELEGATION_TEST_EXPIRED, | |
45870d69 TM |
1092 | &delegation->flags) == 0) |
1093 | continue; | |
1094 | if (!nfs_sb_active(server->super)) | |
f3893491 | 1095 | break; /* continue in outer loop */ |
45870d69 TM |
1096 | inode = nfs_delegation_grab_inode(delegation); |
1097 | if (inode == NULL) { | |
1098 | rcu_read_unlock(); | |
1099 | nfs_sb_deactive(server->super); | |
1100 | goto restart; | |
1101 | } | |
a52458b4 | 1102 | cred = get_cred_rcu(delegation->cred); |
45870d69 TM |
1103 | nfs4_stateid_copy(&stateid, &delegation->stateid); |
1104 | clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags); | |
1105 | rcu_read_unlock(); | |
ad114089 | 1106 | nfs_delegation_test_free_expired(inode, &stateid, cred); |
a52458b4 | 1107 | put_cred(cred); |
bb3d1a3b TM |
1108 | if (nfs4_server_rebooted(clp)) { |
1109 | nfs_inode_mark_test_expired_delegation(server,inode); | |
1110 | iput(inode); | |
1111 | nfs_sb_deactive(server->super); | |
1112 | return; | |
1113 | } | |
45870d69 TM |
1114 | iput(inode); |
1115 | nfs_sb_deactive(server->super); | |
3ca951b6 | 1116 | cond_resched(); |
45870d69 TM |
1117 | goto restart; |
1118 | } | |
1119 | } | |
1120 | rcu_read_unlock(); | |
1121 | } | |
1122 | ||
6c2d8f8d TM |
1123 | void nfs_inode_find_delegation_state_and_recover(struct inode *inode, |
1124 | const nfs4_stateid *stateid) | |
1125 | { | |
1126 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; | |
1127 | struct nfs_delegation *delegation; | |
1128 | bool found = false; | |
1129 | ||
1130 | rcu_read_lock(); | |
1131 | delegation = rcu_dereference(NFS_I(inode)->delegation); | |
1132 | if (delegation && | |
1133 | nfs4_stateid_match_other(&delegation->stateid, stateid)) { | |
1134 | nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation); | |
1135 | found = true; | |
1136 | } | |
1137 | rcu_read_unlock(); | |
1138 | if (found) | |
1139 | nfs4_schedule_state_manager(clp); | |
1140 | } | |
1141 | ||
d3978bb3 CL |
1142 | /** |
1143 | * nfs_delegations_present - check for existence of delegations | |
1144 | * @clp: client state handle | |
1145 | * | |
1146 | * Returns one if there are any nfs_delegation structures attached | |
1147 | * to this nfs_client. | |
1148 | */ | |
1149 | int nfs_delegations_present(struct nfs_client *clp) | |
1150 | { | |
1151 | struct nfs_server *server; | |
1152 | int ret = 0; | |
1153 | ||
1154 | rcu_read_lock(); | |
1155 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) | |
1156 | if (!list_empty(&server->delegations)) { | |
1157 | ret = 1; | |
1158 | break; | |
1159 | } | |
1160 | rcu_read_unlock(); | |
1161 | return ret; | |
1162 | } | |
1163 | ||
12f275cd TM |
1164 | /** |
1165 | * nfs4_refresh_delegation_stateid - Update delegation stateid seqid | |
1166 | * @dst: stateid to refresh | |
1167 | * @inode: inode to check | |
1168 | * | |
1169 | * Returns "true" and updates "dst->seqid" * if inode had a delegation | |
1170 | * that matches our delegation stateid. Otherwise "false" is returned. | |
1171 | */ | |
1172 | bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode) | |
1173 | { | |
1174 | struct nfs_delegation *delegation; | |
1175 | bool ret = false; | |
1176 | if (!inode) | |
1177 | goto out; | |
1178 | ||
1179 | rcu_read_lock(); | |
1180 | delegation = rcu_dereference(NFS_I(inode)->delegation); | |
1181 | if (delegation != NULL && | |
1182 | nfs4_stateid_match_other(dst, &delegation->stateid)) { | |
1183 | dst->seqid = delegation->stateid.seqid; | |
1184 | return ret; | |
1185 | } | |
1186 | rcu_read_unlock(); | |
1187 | out: | |
1188 | return ret; | |
1189 | } | |
1190 | ||
d3978bb3 CL |
1191 | /** |
1192 | * nfs4_copy_delegation_stateid - Copy inode's state ID information | |
d3978bb3 | 1193 | * @inode: inode to check |
0032a7a7 | 1194 | * @flags: delegation type requirement |
abf4e13c TM |
1195 | * @dst: stateid data structure to fill in |
1196 | * @cred: optional argument to retrieve credential | |
d3978bb3 | 1197 | * |
0032a7a7 TM |
1198 | * Returns "true" and fills in "dst->data" * if inode had a delegation, |
1199 | * otherwise "false" is returned. | |
d3978bb3 | 1200 | */ |
abf4e13c | 1201 | bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags, |
a52458b4 | 1202 | nfs4_stateid *dst, const struct cred **cred) |
3e4f6290 | 1203 | { |
3e4f6290 TM |
1204 | struct nfs_inode *nfsi = NFS_I(inode); |
1205 | struct nfs_delegation *delegation; | |
0032a7a7 | 1206 | bool ret; |
3e4f6290 | 1207 | |
0032a7a7 | 1208 | flags &= FMODE_READ|FMODE_WRITE; |
8383e460 TM |
1209 | rcu_read_lock(); |
1210 | delegation = rcu_dereference(nfsi->delegation); | |
aa05c87f | 1211 | ret = nfs4_is_valid_delegation(delegation, flags); |
0032a7a7 | 1212 | if (ret) { |
f597c537 | 1213 | nfs4_stateid_copy(dst, &delegation->stateid); |
0032a7a7 | 1214 | nfs_mark_delegation_referenced(delegation); |
abf4e13c | 1215 | if (cred) |
a52458b4 | 1216 | *cred = get_cred(delegation->cred); |
3e4f6290 | 1217 | } |
8383e460 TM |
1218 | rcu_read_unlock(); |
1219 | return ret; | |
3e4f6290 | 1220 | } |
5445b1fb TM |
1221 | |
1222 | /** | |
1223 | * nfs4_delegation_flush_on_close - Check if we must flush file on close | |
1224 | * @inode: inode to check | |
1225 | * | |
1226 | * This function checks the number of outstanding writes to the file | |
1227 | * against the delegation 'space_limit' field to see if | |
1228 | * the spec requires us to flush the file on close. | |
1229 | */ | |
1230 | bool nfs4_delegation_flush_on_close(const struct inode *inode) | |
1231 | { | |
1232 | struct nfs_inode *nfsi = NFS_I(inode); | |
1233 | struct nfs_delegation *delegation; | |
1234 | bool ret = true; | |
1235 | ||
1236 | rcu_read_lock(); | |
1237 | delegation = rcu_dereference(nfsi->delegation); | |
1238 | if (delegation == NULL || !(delegation->type & FMODE_WRITE)) | |
1239 | goto out; | |
a6b6d5b8 | 1240 | if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit) |
5445b1fb TM |
1241 | ret = false; |
1242 | out: | |
1243 | rcu_read_unlock(); | |
1244 | return ret; | |
1245 | } |