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