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