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