delegation.c 37.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10
/*
 * linux/fs/nfs/delegation.c
 *
 * Copyright (C) 2004 Trond Myklebust
 *
 * NFS file delegation management
 *
 */
#include <linux/completion.h>
11
#include <linux/kthread.h>
L
Linus Torvalds 已提交
12 13
#include <linux/module.h>
#include <linux/sched.h>
14
#include <linux/slab.h>
L
Linus Torvalds 已提交
15
#include <linux/spinlock.h>
J
Jeff Layton 已提交
16
#include <linux/iversion.h>
L
Linus Torvalds 已提交
17 18 19 20 21

#include <linux/nfs4.h>
#include <linux/nfs_fs.h>
#include <linux/nfs_xdr.h>

22
#include "nfs4_fs.h"
23
#include "nfs4session.h"
L
Linus Torvalds 已提交
24
#include "delegation.h"
25
#include "internal.h"
26
#include "nfs4trace.h"
L
Linus Torvalds 已提交
27

28 29
#define NFS_DEFAULT_DELEGATION_WATERMARK (5000U)

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 36
	put_cred(delegation->cred);
	delegation->cred = NULL;
37
	kfree_rcu(delegation, rcu);
38 39
}

40 41 42 43 44
static void nfs_mark_delegation_revoked(struct nfs_delegation *delegation)
{
	if (!test_and_set_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
		delegation->stateid.type = NFS4_INVALID_STATEID_TYPE;
		atomic_long_dec(&nfs_active_delegations);
45 46
		if (!test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
			nfs_clear_verifier_delegated(delegation->inode);
47 48 49
	}
}

50 51 52 53 54 55 56 57 58 59 60 61
static struct nfs_delegation *nfs_get_delegation(struct nfs_delegation *delegation)
{
	refcount_inc(&delegation->refcount);
	return delegation;
}

static void nfs_put_delegation(struct nfs_delegation *delegation)
{
	if (refcount_dec_and_test(&delegation->refcount))
		__nfs_free_delegation(delegation);
}

62 63 64
static void nfs_free_delegation(struct nfs_delegation *delegation)
{
	nfs_mark_delegation_revoked(delegation);
65
	nfs_put_delegation(delegation);
66 67
}

68 69 70 71 72
/**
 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
 * @delegation: delegation to process
 *
 */
73 74 75 76 77
void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
{
	set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
}

78 79 80 81 82 83 84 85 86 87 88
static bool
nfs4_is_valid_delegation(const struct nfs_delegation *delegation,
		fmode_t flags)
{
	if (delegation != NULL && (delegation->type & flags) == flags &&
	    !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
	    !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
		return true;
	return false;
}

89 90 91 92 93 94 95 96 97 98
struct nfs_delegation *nfs4_get_valid_delegation(const struct inode *inode)
{
	struct nfs_delegation *delegation;

	delegation = rcu_dereference(NFS_I(inode)->delegation);
	if (nfs4_is_valid_delegation(delegation, 0))
		return delegation;
	return NULL;
}

P
Peng Tao 已提交
99 100
static int
nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
101 102 103 104 105 106 107
{
	struct nfs_delegation *delegation;
	int ret = 0;

	flags &= FMODE_READ|FMODE_WRITE;
	rcu_read_lock();
	delegation = rcu_dereference(NFS_I(inode)->delegation);
108
	if (nfs4_is_valid_delegation(delegation, flags)) {
P
Peng Tao 已提交
109 110
		if (mark)
			nfs_mark_delegation_referenced(delegation);
111 112 113 114 115
		ret = 1;
	}
	rcu_read_unlock();
	return ret;
}
P
Peng Tao 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
/**
 * nfs_have_delegation - check if inode has a delegation, mark it
 * NFS_DELEGATION_REFERENCED if there is one.
 * @inode: inode to check
 * @flags: delegation types to check for
 *
 * Returns one if inode has the indicated delegation, otherwise zero.
 */
int nfs4_have_delegation(struct inode *inode, fmode_t flags)
{
	return nfs4_do_check_delegation(inode, flags, true);
}

/*
 * nfs4_check_delegation - check if inode has a delegation, do not mark
 * NFS_DELEGATION_REFERENCED if it has one.
 */
int nfs4_check_delegation(struct inode *inode, fmode_t flags)
{
	return nfs4_do_check_delegation(inode, flags, false);
}
137

138
static int nfs_delegation_claim_locks(struct nfs4_state *state, const nfs4_stateid *stateid)
139 140 141
{
	struct inode *inode = state->inode;
	struct file_lock *fl;
142 143
	struct file_lock_context *flctx = inode->i_flctx;
	struct list_head *list;
144
	int status = 0;
145

146
	if (flctx == NULL)
147
		goto out;
148

149
	list = &flctx->flc_posix;
150
	spin_lock(&flctx->flc_lock);
151 152
restart:
	list_for_each_entry(fl, list, fl_list) {
153
		if (nfs_file_open_context(fl->fl_file)->state != state)
154
			continue;
155
		spin_unlock(&flctx->flc_lock);
156
		status = nfs4_lock_delegation_recall(fl, state, stateid);
157
		if (status < 0)
158
			goto out;
159
		spin_lock(&flctx->flc_lock);
160
	}
161 162 163
	if (list == &flctx->flc_posix) {
		list = &flctx->flc_flock;
		goto restart;
164
	}
165
	spin_unlock(&flctx->flc_lock);
166
out:
167 168 169
	return status;
}

170 171
static int nfs_delegation_claim_opens(struct inode *inode,
		const nfs4_stateid *stateid, fmode_t type)
L
Linus Torvalds 已提交
172 173 174
{
	struct nfs_inode *nfsi = NFS_I(inode);
	struct nfs_open_context *ctx;
175
	struct nfs4_state_owner *sp;
L
Linus Torvalds 已提交
176
	struct nfs4_state *state;
177
	unsigned int seq;
178
	int err;
L
Linus Torvalds 已提交
179 180

again:
181 182
	rcu_read_lock();
	list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
L
Linus Torvalds 已提交
183 184 185 186 187
		state = ctx->state;
		if (state == NULL)
			continue;
		if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
			continue;
188 189
		if (!nfs4_valid_open_stateid(state))
			continue;
190
		if (!nfs4_stateid_match(&state->stateid, stateid))
191
			continue;
192 193 194
		if (!get_nfs_open_context(ctx))
			continue;
		rcu_read_unlock();
195
		sp = state->owner;
196 197
		/* Block nfs4_proc_unlck */
		mutex_lock(&sp->so_delegreturn_mutex);
198
		seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
199
		err = nfs4_open_delegation_recall(ctx, state, stateid);
200
		if (!err)
201
			err = nfs_delegation_claim_locks(state, stateid);
202 203
		if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
			err = -EAGAIN;
204
		mutex_unlock(&sp->so_delegreturn_mutex);
L
Linus Torvalds 已提交
205
		put_nfs_open_context(ctx);
206
		if (err != 0)
207
			return err;
L
Linus Torvalds 已提交
208 209
		goto again;
	}
210
	rcu_read_unlock();
211
	return 0;
L
Linus Torvalds 已提交
212 213
}

214 215 216 217
/**
 * nfs_inode_reclaim_delegation - process a delegation reclaim request
 * @inode: inode to process
 * @cred: credential to use for request
218 219 220
 * @type: delegation type
 * @stateid: delegation stateid
 * @pagemod_limit: write delegation "space_limit"
221
 *
L
Linus Torvalds 已提交
222
 */
223
void nfs_inode_reclaim_delegation(struct inode *inode, const struct cred *cred,
224 225 226
				  fmode_t type,
				  const nfs4_stateid *stateid,
				  unsigned long pagemod_limit)
L
Linus Torvalds 已提交
227
{
228
	struct nfs_delegation *delegation;
229
	const struct cred *oldcred = NULL;
L
Linus Torvalds 已提交
230

231 232 233 234
	rcu_read_lock();
	delegation = rcu_dereference(NFS_I(inode)->delegation);
	if (delegation != NULL) {
		spin_lock(&delegation->lock);
235
		if (nfs4_is_valid_delegation(delegation, 0)) {
236 237 238
			nfs4_stateid_copy(&delegation->stateid, stateid);
			delegation->type = type;
			delegation->pagemod_limit = pagemod_limit;
239
			oldcred = delegation->cred;
240
			delegation->cred = get_cred(cred);
241 242 243 244
			clear_bit(NFS_DELEGATION_NEED_RECLAIM,
				  &delegation->flags);
			spin_unlock(&delegation->lock);
			rcu_read_unlock();
245
			put_cred(oldcred);
246
			trace_nfs4_reclaim_delegation(inode, type);
247
			return;
248
		}
249 250
		/* We appear to have raced with a delegation return. */
		spin_unlock(&delegation->lock);
251
	}
252
	rcu_read_unlock();
253
	nfs_inode_set_delegation(inode, cred, type, stateid, pagemod_limit);
L
Linus Torvalds 已提交
254 255
}

256 257
static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
{
258
	const struct cred *cred;
259 260
	int res = 0;

261 262 263 264 265
	if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
		spin_lock(&delegation->lock);
		cred = get_cred(delegation->cred);
		spin_unlock(&delegation->lock);
		res = nfs4_proc_delegreturn(inode, cred,
266 267
				&delegation->stateid,
				issync);
268 269
		put_cred(cred);
	}
270 271 272
	return res;
}

273 274 275 276 277 278 279
static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
{
	struct inode *inode = NULL;

	spin_lock(&delegation->lock);
	if (delegation->inode != NULL)
		inode = igrab(delegation->inode);
280 281
	if (!inode)
		set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
282 283 284 285
	spin_unlock(&delegation->lock);
	return inode;
}

286 287 288 289 290 291 292 293 294
static struct nfs_delegation *
nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
{
	struct nfs_delegation *ret = NULL;
	struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);

	if (delegation == NULL)
		goto out;
	spin_lock(&delegation->lock);
295 296 297 298
	if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
		/* Refcount matched in nfs_end_delegation_return() */
		ret = nfs_get_delegation(delegation);
	}
299
	spin_unlock(&delegation->lock);
300 301
	if (ret)
		nfs_clear_verifier_delegated(&nfsi->vfs_inode);
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
out:
	return ret;
}

static struct nfs_delegation *
nfs_start_delegation_return(struct nfs_inode *nfsi)
{
	struct nfs_delegation *delegation;

	rcu_read_lock();
	delegation = nfs_start_delegation_return_locked(nfsi);
	rcu_read_unlock();
	return delegation;
}

static void
nfs_abort_delegation_return(struct nfs_delegation *delegation,
		struct nfs_client *clp)
{

	spin_lock(&delegation->lock);
	clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
	set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
	spin_unlock(&delegation->lock);
	set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
}

329 330
static struct nfs_delegation *
nfs_detach_delegation_locked(struct nfs_inode *nfsi,
331 332
		struct nfs_delegation *delegation,
		struct nfs_client *clp)
333
{
334
	struct nfs_delegation *deleg_cur =
335
		rcu_dereference_protected(nfsi->delegation,
336
				lockdep_is_held(&clp->cl_lock));
337

338 339
	if (deleg_cur == NULL || delegation != deleg_cur)
		return NULL;
340

341
	spin_lock(&delegation->lock);
342 343 344 345
	if (!delegation->inode) {
		spin_unlock(&delegation->lock);
		return NULL;
	}
346
	list_del_rcu(&delegation->super_list);
347
	delegation->inode = NULL;
348
	rcu_assign_pointer(nfsi->delegation, NULL);
349
	spin_unlock(&delegation->lock);
350 351 352
	return delegation;
}

353
static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
354 355
		struct nfs_delegation *delegation,
		struct nfs_server *server)
356
{
357
	struct nfs_client *clp = server->nfs_client;
358 359

	spin_lock(&clp->cl_lock);
360
	delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
361 362 363 364
	spin_unlock(&clp->cl_lock);
	return delegation;
}

365 366 367 368 369 370 371
static struct nfs_delegation *
nfs_inode_detach_delegation(struct inode *inode)
{
	struct nfs_inode *nfsi = NFS_I(inode);
	struct nfs_server *server = NFS_SERVER(inode);
	struct nfs_delegation *delegation;

372 373 374 375 376 377
	rcu_read_lock();
	delegation = rcu_dereference(nfsi->delegation);
	if (delegation != NULL)
		delegation = nfs_detach_delegation(nfsi, delegation, server);
	rcu_read_unlock();
	return delegation;
378 379
}

380 381 382 383 384 385 386 387
static void
nfs_update_inplace_delegation(struct nfs_delegation *delegation,
		const struct nfs_delegation *update)
{
	if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
		delegation->stateid.seqid = update->stateid.seqid;
		smp_wmb();
		delegation->type = update->type;
388 389
		if (test_and_clear_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
			atomic_long_inc(&nfs_active_delegations);
390 391 392
	}
}

393 394 395 396
/**
 * nfs_inode_set_delegation - set up a delegation on an inode
 * @inode: inode to which delegation applies
 * @cred: cred to use for subsequent delegation processing
397 398 399
 * @type: delegation type
 * @stateid: delegation stateid
 * @pagemod_limit: write delegation "space_limit"
400 401
 *
 * Returns zero on success, or a negative errno value.
L
Linus Torvalds 已提交
402
 */
403
int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred,
404 405 406
				  fmode_t type,
				  const nfs4_stateid *stateid,
				  unsigned long pagemod_limit)
L
Linus Torvalds 已提交
407
{
408 409
	struct nfs_server *server = NFS_SERVER(inode);
	struct nfs_client *clp = server->nfs_client;
L
Linus Torvalds 已提交
410
	struct nfs_inode *nfsi = NFS_I(inode);
411
	struct nfs_delegation *delegation, *old_delegation;
412
	struct nfs_delegation *freeme = NULL;
L
Linus Torvalds 已提交
413 414
	int status = 0;

415
	delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
L
Linus Torvalds 已提交
416 417
	if (delegation == NULL)
		return -ENOMEM;
418
	nfs4_stateid_copy(&delegation->stateid, stateid);
419
	refcount_set(&delegation->refcount, 1);
420 421
	delegation->type = type;
	delegation->pagemod_limit = pagemod_limit;
J
Jeff Layton 已提交
422
	delegation->change_attr = inode_peek_iversion_raw(inode);
423
	delegation->cred = get_cred(cred);
L
Linus Torvalds 已提交
424
	delegation->inode = inode;
425
	delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
426
	spin_lock_init(&delegation->lock);
L
Linus Torvalds 已提交
427 428

	spin_lock(&clp->cl_lock);
429
	old_delegation = rcu_dereference_protected(nfsi->delegation,
430
					lockdep_is_held(&clp->cl_lock));
431 432 433 434 435 436 437 438 439 440 441 442
	if (old_delegation == NULL)
		goto add_new;
	/* Is this an update of the existing delegation? */
	if (nfs4_stateid_match_other(&old_delegation->stateid,
				&delegation->stateid)) {
		spin_lock(&old_delegation->lock);
		nfs_update_inplace_delegation(old_delegation,
				delegation);
		spin_unlock(&old_delegation->lock);
		goto out;
	}
	if (!test_bit(NFS_DELEGATION_REVOKED, &old_delegation->flags)) {
443 444 445
		/*
		 * Deal with broken servers that hand out two
		 * delegations for the same file.
446 447
		 * Allow for upgrades to a WRITE delegation, but
		 * nothing else.
448 449 450
		 */
		dfprintk(FILE, "%s: server %s handed out "
				"a duplicate delegation!\n",
451
				__func__, clp->cl_hostname);
452 453
		if (delegation->type == old_delegation->type ||
		    !(delegation->type & FMODE_WRITE)) {
454 455 456 457
			freeme = delegation;
			delegation = NULL;
			goto out;
		}
458 459 460
		if (test_and_set_bit(NFS_DELEGATION_RETURNING,
					&old_delegation->flags))
			goto out;
L
Linus Torvalds 已提交
461
	}
462 463 464 465
	freeme = nfs_detach_delegation_locked(nfsi, old_delegation, clp);
	if (freeme == NULL)
		goto out;
add_new:
466
	list_add_tail_rcu(&delegation->super_list, &server->delegations);
467 468
	rcu_assign_pointer(nfsi->delegation, delegation);
	delegation = NULL;
469

470 471
	atomic_long_inc(&nfs_active_delegations);

472
	trace_nfs4_set_delegation(inode, type);
473

474 475 476 477
	spin_lock(&inode->i_lock);
	if (NFS_I(inode)->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME))
		NFS_I(inode)->cache_validity |= NFS_INO_REVAL_FORCED;
	spin_unlock(&inode->i_lock);
478
out:
L
Linus Torvalds 已提交
479
	spin_unlock(&clp->cl_lock);
480
	if (delegation != NULL)
481
		__nfs_free_delegation(delegation);
482
	if (freeme != NULL) {
483
		nfs_do_return_delegation(inode, freeme, 0);
484 485
		nfs_free_delegation(freeme);
	}
L
Linus Torvalds 已提交
486 487 488 489 490 491
	return status;
}

/*
 * Basic procedure for returning a delegation to the server
 */
492
static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
L
Linus Torvalds 已提交
493
{
494
	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
495
	int err = 0;
L
Linus Torvalds 已提交
496

497 498 499
	if (delegation == NULL)
		return 0;
	do {
500 501
		if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
			break;
502 503
		err = nfs_delegation_claim_opens(inode, &delegation->stateid,
				delegation->type);
504 505 506 507 508 509 510 511 512 513 514 515
		if (!issync || err != -EAGAIN)
			break;
		/*
		 * Guard against state recovery
		 */
		err = nfs4_wait_clnt_recover(clp);
	} while (err == 0);

	if (err) {
		nfs_abort_delegation_return(delegation, clp);
		goto out;
	}
L
Linus Torvalds 已提交
516

517 518
	err = nfs_do_return_delegation(inode, delegation, issync);
out:
519 520
	/* Refcount matched in nfs_start_delegation_return_locked() */
	nfs_put_delegation(delegation);
521
	return err;
522 523
}

524 525 526 527 528 529
static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
{
	bool ret = false;

	if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
		ret = true;
530
	else if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags)) {
531 532 533 534 535 536 537 538
		struct inode *inode;

		spin_lock(&delegation->lock);
		inode = delegation->inode;
		if (inode && list_empty(&NFS_I(inode)->open_files))
			ret = true;
		spin_unlock(&delegation->lock);
	}
539 540
	if (ret)
		clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
541 542 543 544
	if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags) ||
	    test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
		ret = false;

545 546 547
	return ret;
}

548 549 550 551
/**
 * nfs_client_return_marked_delegations - return previously marked delegations
 * @clp: nfs_client to process
 *
552 553 554 555
 * Note that this function is designed to be called by the state
 * manager thread. For this reason, it cannot flush the dirty data,
 * since that could deadlock in case of a state recovery error.
 *
556
 * Returns zero on success, or a negative errno value.
557
 */
558
int nfs_client_return_marked_delegations(struct nfs_client *clp)
559 560
{
	struct nfs_delegation *delegation;
561
	struct nfs_delegation *prev;
562
	struct nfs_server *server;
563
	struct inode *inode;
564 565
	struct inode *place_holder = NULL;
	struct nfs_delegation *place_holder_deleg = NULL;
566
	int err = 0;
567 568

restart:
569 570 571 572 573 574 575 576 577 578
	/*
	 * To avoid quadratic looping we hold a reference
	 * to an inode place_holder.  Each time we restart, we
	 * list nfs_servers from the server of that inode, and
	 * delegation in the server from the delegations of that
	 * inode.
	 * prev is an RCU-protected pointer to a delegation which
	 * wasn't marked for return and might be a good choice for
	 * the next place_holder.
	 */
579
	rcu_read_lock();
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
	prev = NULL;
	if (place_holder)
		server = NFS_SERVER(place_holder);
	else
		server = list_entry_rcu(clp->cl_superblocks.next,
					struct nfs_server, client_link);
	list_for_each_entry_from_rcu(server, &clp->cl_superblocks, client_link) {
		delegation = NULL;
		if (place_holder && server == NFS_SERVER(place_holder))
			delegation = rcu_dereference(NFS_I(place_holder)->delegation);
		if (!delegation || delegation != place_holder_deleg)
			delegation = list_entry_rcu(server->delegations.next,
						    struct nfs_delegation, super_list);
		list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
			struct inode *to_put = NULL;

			if (!nfs_delegation_need_return(delegation)) {
				prev = delegation;
598
				continue;
599
			}
600
			if (!nfs_sb_active(server->super))
601
				break; /* continue in outer loop */
602 603 604 605 606 607 608 609 610 611 612 613

			if (prev) {
				struct inode *tmp;

				tmp = nfs_delegation_grab_inode(prev);
				if (tmp) {
					to_put = place_holder;
					place_holder = tmp;
					place_holder_deleg = prev;
				}
			}

614 615 616
			inode = nfs_delegation_grab_inode(delegation);
			if (inode == NULL) {
				rcu_read_unlock();
617 618
				if (to_put)
					iput(to_put);
619 620 621
				nfs_sb_deactive(server->super);
				goto restart;
			}
622
			delegation = nfs_start_delegation_return_locked(NFS_I(inode));
623 624
			rcu_read_unlock();

625 626 627
			if (to_put)
				iput(to_put);

628
			err = nfs_end_delegation_return(inode, delegation, 0);
629
			iput(inode);
630
			nfs_sb_deactive(server->super);
631
			cond_resched();
632 633 634
			if (!err)
				goto restart;
			set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
635 636
			if (place_holder)
				iput(place_holder);
637
			return err;
638
		}
639 640
	}
	rcu_read_unlock();
641 642
	if (place_holder)
		iput(place_holder);
643
	return 0;
644 645
}

646
/**
647
 * nfs_inode_evict_delegation - return delegation, don't reclaim opens
648 649 650
 * @inode: inode to process
 *
 * Does not protect against delegation reclaims, therefore really only safe
651 652
 * to be called from nfs4_clear_inode(). Guaranteed to always free
 * the delegation structure.
653
 */
654
void nfs_inode_evict_delegation(struct inode *inode)
655 656 657
{
	struct nfs_delegation *delegation;

658
	delegation = nfs_inode_detach_delegation(inode);
659
	if (delegation != NULL) {
660
		set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
661
		set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
662
		nfs_do_return_delegation(inode, delegation, 1);
663
		nfs_free_delegation(delegation);
664
	}
665 666
}

667 668 669 670
/**
 * nfs_inode_return_delegation - synchronously return a delegation
 * @inode: inode to process
 *
671 672 673 674
 * This routine will always flush any dirty data to disk on the
 * assumption that if we need to return the delegation, then
 * we should stop caching.
 *
675 676
 * Returns zero on success, or a negative errno value.
 */
677
int nfs4_inode_return_delegation(struct inode *inode)
678 679 680 681 682
{
	struct nfs_inode *nfsi = NFS_I(inode);
	struct nfs_delegation *delegation;
	int err = 0;

683
	nfs_wb_all(inode);
684 685 686
	delegation = nfs_start_delegation_return(nfsi);
	if (delegation != NULL)
		err = nfs_end_delegation_return(inode, delegation, 1);
687
	return err;
L
Linus Torvalds 已提交
688 689
}

690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
/**
 * nfs_inode_return_delegation_on_close - asynchronously return a delegation
 * @inode: inode to process
 *
 * This routine is called on file close in order to determine if the
 * inode delegation needs to be returned immediately.
 */
void nfs4_inode_return_delegation_on_close(struct inode *inode)
{
	struct nfs_delegation *delegation;
	struct nfs_delegation *ret = NULL;

	if (!inode)
		return;
	rcu_read_lock();
	delegation = nfs4_get_valid_delegation(inode);
	if (!delegation)
		goto out;
708 709
	if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) ||
	    atomic_long_read(&nfs_active_delegations) >= nfs_delegation_watermark) {
710 711 712 713 714
		spin_lock(&delegation->lock);
		if (delegation->inode &&
		    list_empty(&NFS_I(inode)->open_files) &&
		    !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
			clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
715 716
			/* Refcount matched in nfs_end_delegation_return() */
			ret = nfs_get_delegation(delegation);
717 718
		}
		spin_unlock(&delegation->lock);
719 720
		if (ret)
			nfs_clear_verifier_delegated(inode);
721 722 723 724 725 726
	}
out:
	rcu_read_unlock();
	nfs_end_delegation_return(inode, ret, 0);
}

727 728 729 730 731 732 733 734 735 736
/**
 * nfs4_inode_make_writeable
 * @inode: pointer to inode
 *
 * Make the inode writeable by returning the delegation if necessary
 *
 * Returns zero on success, or a negative errno value.
 */
int nfs4_inode_make_writeable(struct inode *inode)
{
737 738 739 740 741 742 743 744 745 746 747 748
	struct nfs_delegation *delegation;

	rcu_read_lock();
	delegation = nfs4_get_valid_delegation(inode);
	if (delegation == NULL ||
	    (nfs4_has_session(NFS_SERVER(inode)->nfs_client) &&
	     (delegation->type & FMODE_WRITE))) {
		rcu_read_unlock();
		return 0;
	}
	rcu_read_unlock();
	return nfs4_inode_return_delegation(inode);
749 750
}

751 752 753 754 755 756 757
static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
		struct nfs_delegation *delegation)
{
	set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
	set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
}

758 759
static void nfs_mark_return_delegation(struct nfs_server *server,
		struct nfs_delegation *delegation)
760 761
{
	set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
762
	set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
763 764
}

765 766 767 768 769 770 771 772 773 774 775 776
static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
{
	struct nfs_delegation *delegation;
	bool ret = false;

	list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
		nfs_mark_return_delegation(server, delegation);
		ret = true;
	}
	return ret;
}

777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
{
	struct nfs_server *server;

	rcu_read_lock();
	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
		nfs_server_mark_return_all_delegations(server);
	rcu_read_unlock();
}

static void nfs_delegation_run_state_manager(struct nfs_client *clp)
{
	if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
		nfs4_schedule_state_manager(clp);
}

/**
 * nfs_expire_all_delegations
 * @clp: client to process
 *
 */
void nfs_expire_all_delegations(struct nfs_client *clp)
{
	nfs_client_mark_return_all_delegations(clp);
	nfs_delegation_run_state_manager(clp);
}

804 805
/**
 * nfs_super_return_all_delegations - return delegations for one superblock
806
 * @server: pointer to nfs_server to process
807
 *
L
Linus Torvalds 已提交
808
 */
809
void nfs_server_return_all_delegations(struct nfs_server *server)
L
Linus Torvalds 已提交
810
{
811
	struct nfs_client *clp = server->nfs_client;
812
	bool need_wait;
L
Linus Torvalds 已提交
813 814 815

	if (clp == NULL)
		return;
816

817
	rcu_read_lock();
818
	need_wait = nfs_server_mark_return_all_delegations(server);
819
	rcu_read_unlock();
820

821
	if (need_wait) {
822
		nfs4_schedule_state_manager(clp);
823 824
		nfs4_wait_clnt_recover(clp);
	}
825 826
}

827
static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
828
						 fmode_t flags)
829 830 831
{
	struct nfs_delegation *delegation;

832
	list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
833 834 835
		if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
			continue;
		if (delegation->type & flags)
836
			nfs_mark_return_if_closed_delegation(server, delegation);
837
	}
838 839
}

840
static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
841 842 843 844 845 846
							fmode_t flags)
{
	struct nfs_server *server;

	rcu_read_lock();
	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
847
		nfs_mark_return_unused_delegation_types(server, flags);
848
	rcu_read_unlock();
L
Linus Torvalds 已提交
849 850
}

851
static void nfs_revoke_delegation(struct inode *inode,
852
		const nfs4_stateid *stateid)
853 854
{
	struct nfs_delegation *delegation;
855
	nfs4_stateid tmp;
856 857
	bool ret = false;

858 859
	rcu_read_lock();
	delegation = rcu_dereference(NFS_I(inode)->delegation);
860 861
	if (delegation == NULL)
		goto out;
862 863 864
	if (stateid == NULL) {
		nfs4_stateid_copy(&tmp, &delegation->stateid);
		stateid = &tmp;
865 866 867 868 869 870 871 872 873 874 875 876 877
	} else {
		if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
			goto out;
		spin_lock(&delegation->lock);
		if (stateid->seqid) {
			if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) {
				spin_unlock(&delegation->lock);
				goto out;
			}
			delegation->stateid.seqid = stateid->seqid;
		}
		spin_unlock(&delegation->lock);
	}
878
	nfs_mark_delegation_revoked(delegation);
879 880
	ret = true;
out:
881
	rcu_read_unlock();
882 883
	if (ret)
		nfs_inode_find_state_and_recover(inode, stateid);
884 885
}

886 887
void nfs_remove_bad_delegation(struct inode *inode,
		const nfs4_stateid *stateid)
888
{
889
	nfs_revoke_delegation(inode, stateid);
890
}
A
Andy Adamson 已提交
891
EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
892

893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
void nfs_delegation_mark_returned(struct inode *inode,
		const nfs4_stateid *stateid)
{
	struct nfs_delegation *delegation;

	if (!inode)
		return;

	rcu_read_lock();
	delegation = rcu_dereference(NFS_I(inode)->delegation);
	if (!delegation)
		goto out_rcu_unlock;

	spin_lock(&delegation->lock);
	if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
		goto out_spin_unlock;
	if (stateid->seqid) {
		/* If delegation->stateid is newer, dont mark as returned */
		if (nfs4_stateid_is_newer(&delegation->stateid, stateid))
			goto out_clear_returning;
		if (delegation->stateid.seqid != stateid->seqid)
			delegation->stateid.seqid = stateid->seqid;
	}

917
	nfs_mark_delegation_revoked(delegation);
918 919 920 921 922 923 924 925 926 927 928

out_clear_returning:
	clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
out_spin_unlock:
	spin_unlock(&delegation->lock);
out_rcu_unlock:
	rcu_read_unlock();

	nfs_inode_find_state_and_recover(inode, stateid);
}

929
/**
930
 * nfs_expire_unused_delegation_types
931 932 933 934
 * @clp: client to process
 * @flags: delegation types to expire
 *
 */
935
void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
936
{
937
	nfs_client_mark_return_unused_delegation_types(clp, flags);
938
	nfs_delegation_run_state_manager(clp);
939 940
}

941
static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
942 943 944
{
	struct nfs_delegation *delegation;

945
	list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
946 947
		if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
			continue;
948
		nfs_mark_return_if_closed_delegation(server, delegation);
949 950 951
	}
}

952 953 954 955 956
/**
 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
 * @clp: nfs_client to process
 *
 */
957 958
void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
{
959 960 961 962 963 964 965
	struct nfs_server *server;

	rcu_read_lock();
	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
		nfs_mark_return_unreferenced_delegations(server);
	rcu_read_unlock();

966 967 968
	nfs_delegation_run_state_manager(clp);
}

969 970 971
/**
 * nfs_async_inode_return_delegation - asynchronously return a delegation
 * @inode: inode to process
972
 * @stateid: state ID information
973 974
 *
 * Returns zero on success, or a negative errno value.
L
Linus Torvalds 已提交
975
 */
976 977
int nfs_async_inode_return_delegation(struct inode *inode,
				      const nfs4_stateid *stateid)
L
Linus Torvalds 已提交
978
{
979 980
	struct nfs_server *server = NFS_SERVER(inode);
	struct nfs_client *clp = server->nfs_client;
981
	struct nfs_delegation *delegation;
L
Linus Torvalds 已提交
982

983
	rcu_read_lock();
984
	delegation = nfs4_get_valid_delegation(inode);
985 986
	if (delegation == NULL)
		goto out_enoent;
987 988
	if (stateid != NULL &&
	    !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
989
		goto out_enoent;
990
	nfs_mark_return_delegation(server, delegation);
991
	rcu_read_unlock();
992

993 994
	nfs_delegation_run_state_manager(clp);
	return 0;
995 996 997
out_enoent:
	rcu_read_unlock();
	return -ENOENT;
L
Linus Torvalds 已提交
998 999
}

1000 1001 1002
static struct inode *
nfs_delegation_find_inode_server(struct nfs_server *server,
				 const struct nfs_fh *fhandle)
L
Linus Torvalds 已提交
1003 1004
{
	struct nfs_delegation *delegation;
1005
	struct inode *freeme, *res = NULL;
1006 1007

	list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1008 1009
		spin_lock(&delegation->lock);
		if (delegation->inode != NULL &&
1010
		    !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
1011
		    nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
1012 1013 1014
			freeme = igrab(delegation->inode);
			if (freeme && nfs_sb_active(freeme->i_sb))
				res = freeme;
1015 1016 1017
			spin_unlock(&delegation->lock);
			if (res != NULL)
				return res;
1018 1019 1020 1021 1022
			if (freeme) {
				rcu_read_unlock();
				iput(freeme);
				rcu_read_lock();
			}
1023
			return ERR_PTR(-EAGAIN);
L
Linus Torvalds 已提交
1024
		}
1025
		spin_unlock(&delegation->lock);
L
Linus Torvalds 已提交
1026
	}
1027
	return ERR_PTR(-ENOENT);
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
}

/**
 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
 * @clp: client state handle
 * @fhandle: filehandle from a delegation recall
 *
 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
 * cannot be found.
 */
struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
					const struct nfs_fh *fhandle)
{
	struct nfs_server *server;
1042
	struct inode *res;
1043 1044 1045 1046

	rcu_read_lock();
	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
		res = nfs_delegation_find_inode_server(server, fhandle);
1047 1048
		if (res != ERR_PTR(-ENOENT)) {
			rcu_read_unlock();
1049
			return res;
1050
		}
1051
	}
1052
	rcu_read_unlock();
1053
	return ERR_PTR(-ENOENT);
L
Linus Torvalds 已提交
1054 1055
}

1056 1057 1058 1059
static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
{
	struct nfs_delegation *delegation;

1060 1061 1062 1063 1064 1065 1066
	list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
		/*
		 * If the delegation may have been admin revoked, then we
		 * cannot reclaim it.
		 */
		if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
			continue;
1067
		set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1068
	}
1069 1070 1071 1072 1073 1074
}

/**
 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
 * @clp: nfs_client to process
 *
L
Linus Torvalds 已提交
1075
 */
1076
void nfs_delegation_mark_reclaim(struct nfs_client *clp)
L
Linus Torvalds 已提交
1077
{
1078 1079
	struct nfs_server *server;

1080
	rcu_read_lock();
1081 1082
	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
		nfs_delegation_mark_reclaim_server(server);
1083
	rcu_read_unlock();
L
Linus Torvalds 已提交
1084 1085
}

1086 1087 1088 1089
/**
 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
 * @clp: nfs_client to process
 *
L
Linus Torvalds 已提交
1090
 */
1091
void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
L
Linus Torvalds 已提交
1092
{
1093
	struct nfs_delegation *delegation;
1094
	struct nfs_server *server;
1095
	struct inode *inode;
1096

1097 1098
restart:
	rcu_read_lock();
1099 1100 1101
	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
		list_for_each_entry_rcu(delegation, &server->delegations,
								super_list) {
1102 1103 1104 1105 1106
			if (test_bit(NFS_DELEGATION_INODE_FREEING,
						&delegation->flags) ||
			    test_bit(NFS_DELEGATION_RETURNING,
						&delegation->flags) ||
			    test_bit(NFS_DELEGATION_NEED_RECLAIM,
1107 1108
						&delegation->flags) == 0)
				continue;
1109
			if (!nfs_sb_active(server->super))
1110
				break; /* continue in outer loop */
1111 1112 1113 1114 1115 1116
			inode = nfs_delegation_grab_inode(delegation);
			if (inode == NULL) {
				rcu_read_unlock();
				nfs_sb_deactive(server->super);
				goto restart;
			}
1117
			delegation = nfs_start_delegation_return_locked(NFS_I(inode));
1118
			rcu_read_unlock();
1119
			if (delegation != NULL) {
1120 1121
				if (nfs_detach_delegation(NFS_I(inode), delegation,
							server) != NULL)
1122
					nfs_free_delegation(delegation);
1123 1124
				/* Match nfs_start_delegation_return_locked */
				nfs_put_delegation(delegation);
1125
			}
1126
			iput(inode);
1127
			nfs_sb_deactive(server->super);
1128
			cond_resched();
1129 1130
			goto restart;
		}
L
Linus Torvalds 已提交
1131
	}
1132
	rcu_read_unlock();
L
Linus Torvalds 已提交
1133
}
1134

1135 1136 1137 1138 1139 1140 1141
static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
{
	return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
				BIT(NFS4CLNT_LEASE_EXPIRED) |
				BIT(NFS4CLNT_SESSION_RESET))) != 0;
}

1142 1143 1144
static void nfs_mark_test_expired_delegation(struct nfs_server *server,
	    struct nfs_delegation *delegation)
{
1145 1146
	if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
		return;
1147 1148 1149 1150 1151
	clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
	set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
	set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
}

1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
		struct inode *inode)
{
	struct nfs_delegation *delegation;

	rcu_read_lock();
	delegation = rcu_dereference(NFS_I(inode)->delegation);
	if (delegation)
		nfs_mark_test_expired_delegation(server, delegation);
	rcu_read_unlock();

}

1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
{
	struct nfs_delegation *delegation;

	list_for_each_entry_rcu(delegation, &server->delegations, super_list)
		nfs_mark_test_expired_delegation(server, delegation);
}

/**
 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
 * @clp: nfs_client to process
 *
 * Iterates through all the delegations associated with this server and
 * marks them as needing to be checked for validity.
 */
void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
{
	struct nfs_server *server;

	rcu_read_lock();
	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
		nfs_delegation_mark_test_expired_server(server);
	rcu_read_unlock();
}

1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
/**
 * nfs_test_expired_all_delegations - test all delegations for a client
 * @clp: nfs_client to process
 *
 * Helper for handling "recallable state revoked" status from server.
 */
void nfs_test_expired_all_delegations(struct nfs_client *clp)
{
	nfs_mark_test_expired_all_delegations(clp);
	nfs4_schedule_state_manager(clp);
}

1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
static void
nfs_delegation_test_free_expired(struct inode *inode,
		nfs4_stateid *stateid,
		const struct cred *cred)
{
	struct nfs_server *server = NFS_SERVER(inode);
	const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
	int status;

	if (!cred)
		return;
	status = ops->test_and_free_expired(server, stateid, cred);
	if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
		nfs_remove_bad_delegation(inode, stateid);
}

1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
/**
 * nfs_reap_expired_delegations - reap expired delegations
 * @clp: nfs_client to process
 *
 * Iterates through all the delegations associated with this server and
 * checks if they have may have been revoked. This function is usually
 * expected to be called in cases where the server may have lost its
 * lease.
 */
void nfs_reap_expired_delegations(struct nfs_client *clp)
{
	struct nfs_delegation *delegation;
	struct nfs_server *server;
	struct inode *inode;
1232
	const struct cred *cred;
1233 1234 1235 1236 1237 1238 1239
	nfs4_stateid stateid;

restart:
	rcu_read_lock();
	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
		list_for_each_entry_rcu(delegation, &server->delegations,
								super_list) {
1240 1241 1242 1243 1244
			if (test_bit(NFS_DELEGATION_INODE_FREEING,
						&delegation->flags) ||
			    test_bit(NFS_DELEGATION_RETURNING,
						&delegation->flags) ||
			    test_bit(NFS_DELEGATION_TEST_EXPIRED,
1245 1246 1247
						&delegation->flags) == 0)
				continue;
			if (!nfs_sb_active(server->super))
1248
				break; /* continue in outer loop */
1249 1250 1251 1252 1253 1254
			inode = nfs_delegation_grab_inode(delegation);
			if (inode == NULL) {
				rcu_read_unlock();
				nfs_sb_deactive(server->super);
				goto restart;
			}
1255
			cred = get_cred_rcu(delegation->cred);
1256 1257 1258
			nfs4_stateid_copy(&stateid, &delegation->stateid);
			clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
			rcu_read_unlock();
1259
			nfs_delegation_test_free_expired(inode, &stateid, cred);
1260
			put_cred(cred);
1261 1262 1263 1264 1265 1266
			if (nfs4_server_rebooted(clp)) {
				nfs_inode_mark_test_expired_delegation(server,inode);
				iput(inode);
				nfs_sb_deactive(server->super);
				return;
			}
1267 1268
			iput(inode);
			nfs_sb_deactive(server->super);
1269
			cond_resched();
1270 1271 1272 1273 1274 1275
			goto restart;
		}
	}
	rcu_read_unlock();
}

1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
		const nfs4_stateid *stateid)
{
	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
	struct nfs_delegation *delegation;
	bool found = false;

	rcu_read_lock();
	delegation = rcu_dereference(NFS_I(inode)->delegation);
	if (delegation &&
1286 1287
	    nfs4_stateid_match_or_older(&delegation->stateid, stateid) &&
	    !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1288 1289 1290 1291 1292 1293 1294 1295
		nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
		found = true;
	}
	rcu_read_unlock();
	if (found)
		nfs4_schedule_state_manager(clp);
}

1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
/**
 * nfs_delegations_present - check for existence of delegations
 * @clp: client state handle
 *
 * Returns one if there are any nfs_delegation structures attached
 * to this nfs_client.
 */
int nfs_delegations_present(struct nfs_client *clp)
{
	struct nfs_server *server;
	int ret = 0;

	rcu_read_lock();
	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
		if (!list_empty(&server->delegations)) {
			ret = 1;
			break;
		}
	rcu_read_unlock();
	return ret;
}

1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
/**
 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
 * @dst: stateid to refresh
 * @inode: inode to check
 *
 * Returns "true" and updates "dst->seqid" * if inode had a delegation
 * that matches our delegation stateid. Otherwise "false" is returned.
 */
bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
{
	struct nfs_delegation *delegation;
	bool ret = false;
	if (!inode)
		goto out;

	rcu_read_lock();
	delegation = rcu_dereference(NFS_I(inode)->delegation);
	if (delegation != NULL &&
1336
	    nfs4_stateid_match_other(dst, &delegation->stateid) &&
1337
	    nfs4_stateid_is_newer(&delegation->stateid, dst) &&
1338
	    !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1339
		dst->seqid = delegation->stateid.seqid;
1340
		ret = true;
1341 1342 1343 1344 1345 1346
	}
	rcu_read_unlock();
out:
	return ret;
}

1347 1348 1349
/**
 * nfs4_copy_delegation_stateid - Copy inode's state ID information
 * @inode: inode to check
1350
 * @flags: delegation type requirement
1351 1352
 * @dst: stateid data structure to fill in
 * @cred: optional argument to retrieve credential
1353
 *
1354 1355
 * Returns "true" and fills in "dst->data" * if inode had a delegation,
 * otherwise "false" is returned.
1356
 */
1357
bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
1358
		nfs4_stateid *dst, const struct cred **cred)
1359 1360 1361
{
	struct nfs_inode *nfsi = NFS_I(inode);
	struct nfs_delegation *delegation;
1362
	bool ret;
1363

1364
	flags &= FMODE_READ|FMODE_WRITE;
1365 1366
	rcu_read_lock();
	delegation = rcu_dereference(nfsi->delegation);
1367
	ret = nfs4_is_valid_delegation(delegation, flags);
1368
	if (ret) {
1369
		nfs4_stateid_copy(dst, &delegation->stateid);
1370
		nfs_mark_delegation_referenced(delegation);
1371
		if (cred)
1372
			*cred = get_cred(delegation->cred);
1373
	}
1374 1375
	rcu_read_unlock();
	return ret;
1376
}
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395

/**
 * nfs4_delegation_flush_on_close - Check if we must flush file on close
 * @inode: inode to check
 *
 * This function checks the number of outstanding writes to the file
 * against the delegation 'space_limit' field to see if
 * the spec requires us to flush the file on close.
 */
bool nfs4_delegation_flush_on_close(const struct inode *inode)
{
	struct nfs_inode *nfsi = NFS_I(inode);
	struct nfs_delegation *delegation;
	bool ret = true;

	rcu_read_lock();
	delegation = rcu_dereference(nfsi->delegation);
	if (delegation == NULL || !(delegation->type & FMODE_WRITE))
		goto out;
1396
	if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)
1397 1398 1399 1400 1401
		ret = false;
out:
	rcu_read_unlock();
	return ret;
}
1402 1403

module_param_named(delegation_watermark, nfs_delegation_watermark, uint, 0644);