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