blob: d8b47624fee203de73b26fba07cc2da10ef6c144 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/delegation.c
3 *
4 * Copyright (C) 2004 Trond Myklebust
5 *
6 * NFS file delegation management
7 *
8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/completion.h>
Trond Myklebust58d97142006-01-03 09:55:24 +010010#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/spinlock.h>
Jeff Layton1eb5d982018-01-09 08:21:17 -050015#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include <linux/nfs4.h>
18#include <linux/nfs_fs.h>
19#include <linux/nfs_xdr.h>
20
Trond Myklebust4ce79712005-06-22 17:16:21 +000021#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "delegation.h"
David Howells24c8dbb2006-08-22 20:06:10 -040023#include "internal.h"
Trond Myklebustca8acf82013-08-13 10:36:56 -040024#include "nfs4trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Trond Myklebust905f8d12007-08-06 12:18:34 -040026static void nfs_free_delegation(struct nfs_delegation *delegation)
27{
Trond Myklebuste00b8a22011-01-27 14:55:39 -050028 if (delegation->cred) {
29 put_rpccred(delegation->cred);
30 delegation->cred = NULL;
31 }
Lai Jiangshan26f04dd2011-05-01 06:21:54 -070032 kfree_rcu(delegation, rcu);
Trond Myklebust8383e462007-07-06 15:12:04 -040033}
34
Chuck Leverd3978bb2010-12-24 01:33:04 +000035/**
36 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
37 * @delegation: delegation to process
38 *
39 */
Trond Myklebustb7391f42008-12-23 15:21:52 -050040void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
41{
42 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
43}
44
Trond Myklebustaa05c872016-09-22 13:38:54 -040045static bool
46nfs4_is_valid_delegation(const struct nfs_delegation *delegation,
47 fmode_t flags)
48{
49 if (delegation != NULL && (delegation->type & flags) == flags &&
50 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
51 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
52 return true;
53 return false;
54}
55
Peng Tao15bb3af2014-07-03 13:05:00 +080056static int
57nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
Trond Myklebustb7391f42008-12-23 15:21:52 -050058{
59 struct nfs_delegation *delegation;
60 int ret = 0;
61
62 flags &= FMODE_READ|FMODE_WRITE;
63 rcu_read_lock();
64 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustaa05c872016-09-22 13:38:54 -040065 if (nfs4_is_valid_delegation(delegation, flags)) {
Peng Tao15bb3af2014-07-03 13:05:00 +080066 if (mark)
67 nfs_mark_delegation_referenced(delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -050068 ret = 1;
69 }
70 rcu_read_unlock();
71 return ret;
72}
Peng Tao15bb3af2014-07-03 13:05:00 +080073/**
74 * nfs_have_delegation - check if inode has a delegation, mark it
75 * NFS_DELEGATION_REFERENCED if there is one.
76 * @inode: inode to check
77 * @flags: delegation types to check for
78 *
79 * Returns one if inode has the indicated delegation, otherwise zero.
80 */
81int nfs4_have_delegation(struct inode *inode, fmode_t flags)
82{
83 return nfs4_do_check_delegation(inode, flags, true);
84}
85
86/*
87 * nfs4_check_delegation - check if inode has a delegation, do not mark
88 * NFS_DELEGATION_REFERENCED if it has one.
89 */
90int nfs4_check_delegation(struct inode *inode, fmode_t flags)
91{
92 return nfs4_do_check_delegation(inode, flags, false);
93}
Trond Myklebustb7391f42008-12-23 15:21:52 -050094
Trond Myklebustdb4f2e62013-04-01 15:56:46 -040095static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -050096{
97 struct inode *inode = state->inode;
98 struct file_lock *fl;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -050099 struct file_lock_context *flctx = inode->i_flctx;
100 struct list_head *list;
Trond Myklebustd5122202009-06-17 13:22:58 -0700101 int status = 0;
Trond Myklebust888e6942005-11-04 15:38:11 -0500102
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500103 if (flctx == NULL)
Trond Myklebust65b62a22013-02-07 10:54:07 -0500104 goto out;
Jeff Layton314d7cc2013-04-10 15:36:48 -0400105
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500106 list = &flctx->flc_posix;
Jeff Layton6109c852015-01-16 15:05:57 -0500107 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500108restart:
109 list_for_each_entry(fl, list, fl_list) {
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400110 if (nfs_file_open_context(fl->fl_file) != ctx)
Trond Myklebust888e6942005-11-04 15:38:11 -0500111 continue;
Jeff Layton6109c852015-01-16 15:05:57 -0500112 spin_unlock(&flctx->flc_lock);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -0400113 status = nfs4_lock_delegation_recall(fl, state, stateid);
Trond Myklebustd5122202009-06-17 13:22:58 -0700114 if (status < 0)
Trond Myklebust3f09df72009-06-17 13:23:00 -0700115 goto out;
Jeff Layton6109c852015-01-16 15:05:57 -0500116 spin_lock(&flctx->flc_lock);
Trond Myklebust888e6942005-11-04 15:38:11 -0500117 }
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500118 if (list == &flctx->flc_posix) {
119 list = &flctx->flc_flock;
120 goto restart;
Jeff Layton5263e312015-01-16 15:05:55 -0500121 }
Jeff Layton6109c852015-01-16 15:05:57 -0500122 spin_unlock(&flctx->flc_lock);
Trond Myklebust3f09df72009-06-17 13:23:00 -0700123out:
Trond Myklebust888e6942005-11-04 15:38:11 -0500124 return status;
125}
126
Trond Myklebust24311f82015-09-20 10:50:17 -0400127static int nfs_delegation_claim_opens(struct inode *inode,
128 const nfs4_stateid *stateid, fmode_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 struct nfs_inode *nfsi = NFS_I(inode);
131 struct nfs_open_context *ctx;
Trond Myklebustd25be542013-02-05 11:43:28 -0500132 struct nfs4_state_owner *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 struct nfs4_state *state;
Trond Myklebustd25be542013-02-05 11:43:28 -0500134 unsigned int seq;
Trond Myklebust888e6942005-11-04 15:38:11 -0500135 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137again:
138 spin_lock(&inode->i_lock);
139 list_for_each_entry(ctx, &nfsi->open_files, list) {
140 state = ctx->state;
141 if (state == NULL)
142 continue;
143 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
144 continue;
Trond Myklebustf8ebf7a2014-10-17 23:02:52 +0300145 if (!nfs4_valid_open_stateid(state))
146 continue;
Trond Myklebustf597c532012-03-04 18:13:56 -0500147 if (!nfs4_stateid_match(&state->stateid, stateid))
Trond Myklebust90163022007-07-05 14:55:18 -0400148 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 get_nfs_open_context(ctx);
150 spin_unlock(&inode->i_lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500151 sp = state->owner;
Trond Myklebust65b62a22013-02-07 10:54:07 -0500152 /* Block nfs4_proc_unlck */
153 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebustd25be542013-02-05 11:43:28 -0500154 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebust24311f82015-09-20 10:50:17 -0400155 err = nfs4_open_delegation_recall(ctx, state, stateid, type);
Trond Myklebustd25be542013-02-05 11:43:28 -0500156 if (!err)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -0400157 err = nfs_delegation_claim_locks(ctx, state, stateid);
Trond Myklebustd25be542013-02-05 11:43:28 -0500158 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
159 err = -EAGAIN;
Trond Myklebust65b62a22013-02-07 10:54:07 -0500160 mutex_unlock(&sp->so_delegreturn_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 put_nfs_open_context(ctx);
Trond Myklebust888e6942005-11-04 15:38:11 -0500162 if (err != 0)
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500163 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 goto again;
165 }
166 spin_unlock(&inode->i_lock);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Chuck Leverd3978bb2010-12-24 01:33:04 +0000170/**
171 * nfs_inode_reclaim_delegation - process a delegation reclaim request
172 * @inode: inode to process
173 * @cred: credential to use for request
174 * @res: new delegation state from server
175 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 */
Chuck Leverd3978bb2010-12-24 01:33:04 +0000177void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred,
178 struct nfs_openres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Trond Myklebust8f649c32010-05-01 12:36:18 -0400180 struct nfs_delegation *delegation;
181 struct rpc_cred *oldcred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Trond Myklebust8f649c32010-05-01 12:36:18 -0400183 rcu_read_lock();
184 delegation = rcu_dereference(NFS_I(inode)->delegation);
185 if (delegation != NULL) {
186 spin_lock(&delegation->lock);
187 if (delegation->inode != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500188 nfs4_stateid_copy(&delegation->stateid, &res->delegation);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400189 delegation->type = res->delegation_type;
Trond Myklebust7d160a62015-09-05 19:06:57 -0400190 delegation->pagemod_limit = res->pagemod_limit;
Trond Myklebust8f649c32010-05-01 12:36:18 -0400191 oldcred = delegation->cred;
192 delegation->cred = get_rpccred(cred);
193 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
194 &delegation->flags);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400195 spin_unlock(&delegation->lock);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400196 rcu_read_unlock();
Trond Myklebust7c0af9f2015-02-26 12:54:46 -0500197 put_rpccred(oldcred);
Trond Myklebustca8acf82013-08-13 10:36:56 -0400198 trace_nfs4_reclaim_delegation(inode, res->delegation_type);
Trond Myklebustb1a318d2016-09-22 13:39:12 -0400199 return;
Trond Myklebust8f649c32010-05-01 12:36:18 -0400200 }
Trond Myklebustb1a318d2016-09-22 13:39:12 -0400201 /* We appear to have raced with a delegation return. */
202 spin_unlock(&delegation->lock);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400203 }
Trond Myklebustb1a318d2016-09-22 13:39:12 -0400204 rcu_read_unlock();
205 nfs_inode_set_delegation(inode, cred, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Trond Myklebust57bfa892008-01-25 16:38:18 -0500208static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
209{
210 int res = 0;
211
Trond Myklebust869f9df2014-11-10 18:43:56 -0500212 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
213 res = nfs4_proc_delegreturn(inode,
214 delegation->cred,
215 &delegation->stateid,
216 issync);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500217 nfs_free_delegation(delegation);
218 return res;
219}
220
Trond Myklebust86e89482008-12-23 15:21:39 -0500221static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
222{
223 struct inode *inode = NULL;
224
225 spin_lock(&delegation->lock);
226 if (delegation->inode != NULL)
227 inode = igrab(delegation->inode);
228 spin_unlock(&delegation->lock);
229 return inode;
230}
231
Chuck Leverdda4b222010-12-24 01:32:54 +0000232static struct nfs_delegation *
Trond Myklebustd25be542013-02-05 11:43:28 -0500233nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
Trond Myklebust57bfa892008-01-25 16:38:18 -0500234{
Trond Myklebustd25be542013-02-05 11:43:28 -0500235 struct nfs_delegation *ret = NULL;
236 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500237
238 if (delegation == NULL)
Trond Myklebustd25be542013-02-05 11:43:28 -0500239 goto out;
240 spin_lock(&delegation->lock);
241 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
242 ret = delegation;
243 spin_unlock(&delegation->lock);
244out:
245 return ret;
246}
247
248static struct nfs_delegation *
249nfs_start_delegation_return(struct nfs_inode *nfsi)
250{
251 struct nfs_delegation *delegation;
252
253 rcu_read_lock();
254 delegation = nfs_start_delegation_return_locked(nfsi);
255 rcu_read_unlock();
256 return delegation;
257}
258
259static void
260nfs_abort_delegation_return(struct nfs_delegation *delegation,
261 struct nfs_client *clp)
262{
Chuck Leverdda4b222010-12-24 01:32:54 +0000263
Trond Myklebust34310432008-12-23 15:21:38 -0500264 spin_lock(&delegation->lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500265 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
266 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
267 spin_unlock(&delegation->lock);
268 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
269}
270
271static struct nfs_delegation *
272nfs_detach_delegation_locked(struct nfs_inode *nfsi,
273 struct nfs_delegation *delegation,
274 struct nfs_client *clp)
275{
276 struct nfs_delegation *deleg_cur =
277 rcu_dereference_protected(nfsi->delegation,
278 lockdep_is_held(&clp->cl_lock));
279
280 if (deleg_cur == NULL || delegation != deleg_cur)
281 return NULL;
282
283 spin_lock(&delegation->lock);
284 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500285 list_del_rcu(&delegation->super_list);
Trond Myklebust86e89482008-12-23 15:21:39 -0500286 delegation->inode = NULL;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500287 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust34310432008-12-23 15:21:38 -0500288 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500289 return delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500290}
291
Chuck Leverdda4b222010-12-24 01:32:54 +0000292static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
Trond Myklebustd25be542013-02-05 11:43:28 -0500293 struct nfs_delegation *delegation,
294 struct nfs_server *server)
Chuck Leverdda4b222010-12-24 01:32:54 +0000295{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000296 struct nfs_client *clp = server->nfs_client;
Chuck Leverdda4b222010-12-24 01:32:54 +0000297
298 spin_lock(&clp->cl_lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500299 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
Chuck Leverdda4b222010-12-24 01:32:54 +0000300 spin_unlock(&clp->cl_lock);
301 return delegation;
302}
303
Trond Myklebustd25be542013-02-05 11:43:28 -0500304static struct nfs_delegation *
305nfs_inode_detach_delegation(struct inode *inode)
306{
307 struct nfs_inode *nfsi = NFS_I(inode);
308 struct nfs_server *server = NFS_SERVER(inode);
309 struct nfs_delegation *delegation;
310
311 delegation = nfs_start_delegation_return(nfsi);
312 if (delegation == NULL)
313 return NULL;
314 return nfs_detach_delegation(nfsi, delegation, server);
315}
316
Trond Myklebustcf6726e2014-12-19 11:22:28 -0500317static void
318nfs_update_inplace_delegation(struct nfs_delegation *delegation,
319 const struct nfs_delegation *update)
320{
321 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
322 delegation->stateid.seqid = update->stateid.seqid;
323 smp_wmb();
324 delegation->type = update->type;
325 }
326}
327
Chuck Leverd3978bb2010-12-24 01:33:04 +0000328/**
329 * nfs_inode_set_delegation - set up a delegation on an inode
330 * @inode: inode to which delegation applies
331 * @cred: cred to use for subsequent delegation processing
332 * @res: new delegation state from server
333 *
334 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 */
336int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
337{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000338 struct nfs_server *server = NFS_SERVER(inode);
339 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct nfs_inode *nfsi = NFS_I(inode);
David Howells17d2c0a2010-05-01 12:37:18 -0400341 struct nfs_delegation *delegation, *old_delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500342 struct nfs_delegation *freeme = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 int status = 0;
344
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400345 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (delegation == NULL)
347 return -ENOMEM;
Trond Myklebustf597c532012-03-04 18:13:56 -0500348 nfs4_stateid_copy(&delegation->stateid, &res->delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 delegation->type = res->delegation_type;
Trond Myklebust7d160a62015-09-05 19:06:57 -0400350 delegation->pagemod_limit = res->pagemod_limit;
Jeff Layton1eb5d982018-01-09 08:21:17 -0500351 delegation->change_attr = inode_peek_iversion_raw(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 delegation->cred = get_rpccred(cred);
353 delegation->inode = inode;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500354 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
Trond Myklebust34310432008-12-23 15:21:38 -0500355 spin_lock_init(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 spin_lock(&clp->cl_lock);
David Howells17d2c0a2010-05-01 12:37:18 -0400358 old_delegation = rcu_dereference_protected(nfsi->delegation,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000359 lockdep_is_held(&clp->cl_lock));
David Howells17d2c0a2010-05-01 12:37:18 -0400360 if (old_delegation != NULL) {
Trond Myklebustcf6726e2014-12-19 11:22:28 -0500361 /* Is this an update of the existing delegation? */
362 if (nfs4_stateid_match_other(&old_delegation->stateid,
363 &delegation->stateid)) {
364 nfs_update_inplace_delegation(old_delegation,
365 delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500366 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500368 /*
369 * Deal with broken servers that hand out two
370 * delegations for the same file.
Trond Myklebust17280172012-03-11 13:11:00 -0400371 * Allow for upgrades to a WRITE delegation, but
372 * nothing else.
Trond Myklebust57bfa892008-01-25 16:38:18 -0500373 */
374 dfprintk(FILE, "%s: server %s handed out "
375 "a duplicate delegation!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700376 __func__, clp->cl_hostname);
Trond Myklebust17280172012-03-11 13:11:00 -0400377 if (delegation->type == old_delegation->type ||
378 !(delegation->type & FMODE_WRITE)) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500379 freeme = delegation;
380 delegation = NULL;
381 goto out;
382 }
Trond Myklebustade04642015-02-27 14:25:50 -0500383 if (test_and_set_bit(NFS_DELEGATION_RETURNING,
384 &old_delegation->flags))
385 goto out;
386 freeme = nfs_detach_delegation_locked(nfsi,
Trond Myklebustd25be542013-02-05 11:43:28 -0500387 old_delegation, clp);
388 if (freeme == NULL)
389 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
Trond Myklebust38942ba2015-03-04 15:59:05 -0500391 list_add_tail_rcu(&delegation->super_list, &server->delegations);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500392 rcu_assign_pointer(nfsi->delegation, delegation);
393 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400394
Trond Myklebustca8acf82013-08-13 10:36:56 -0400395 trace_nfs4_set_delegation(inode, res->delegation_type);
Trond Myklebust412c77c2007-07-03 16:10:55 -0400396
Trond Myklebust57bfa892008-01-25 16:38:18 -0500397out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400399 if (delegation != NULL)
400 nfs_free_delegation(delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500401 if (freeme != NULL)
402 nfs_do_return_delegation(inode, freeme, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return status;
404}
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/*
407 * Basic procedure for returning a delegation to the server
408 */
Trond Myklebustd25be542013-02-05 11:43:28 -0500409static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Trond Myklebustd25be542013-02-05 11:43:28 -0500411 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust869f9df2014-11-10 18:43:56 -0500413 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Trond Myklebustd25be542013-02-05 11:43:28 -0500415 if (delegation == NULL)
416 return 0;
417 do {
Trond Myklebust869f9df2014-11-10 18:43:56 -0500418 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
419 break;
Trond Myklebust24311f82015-09-20 10:50:17 -0400420 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
421 delegation->type);
Trond Myklebustd25be542013-02-05 11:43:28 -0500422 if (!issync || err != -EAGAIN)
423 break;
424 /*
425 * Guard against state recovery
426 */
427 err = nfs4_wait_clnt_recover(clp);
428 } while (err == 0);
429
430 if (err) {
431 nfs_abort_delegation_return(delegation, clp);
432 goto out;
433 }
434 if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500435 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500437 err = nfs_do_return_delegation(inode, delegation, issync);
438out:
439 return err;
Trond Myklebust90163022007-07-05 14:55:18 -0400440}
441
Trond Myklebustb7571442013-04-03 14:33:49 -0400442static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
443{
444 bool ret = false;
445
Trond Myklebustec3ca4e52015-02-26 14:05:05 -0500446 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
447 goto out;
Trond Myklebustb7571442013-04-03 14:33:49 -0400448 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
449 ret = true;
450 if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
451 struct inode *inode;
452
453 spin_lock(&delegation->lock);
454 inode = delegation->inode;
455 if (inode && list_empty(&NFS_I(inode)->open_files))
456 ret = true;
457 spin_unlock(&delegation->lock);
458 }
Trond Myklebustec3ca4e52015-02-26 14:05:05 -0500459out:
Trond Myklebustb7571442013-04-03 14:33:49 -0400460 return ret;
461}
462
Chuck Leverd3978bb2010-12-24 01:33:04 +0000463/**
464 * nfs_client_return_marked_delegations - return previously marked delegations
465 * @clp: nfs_client to process
466 *
Trond Myklebustdc327ed2012-05-06 19:46:30 -0400467 * Note that this function is designed to be called by the state
468 * manager thread. For this reason, it cannot flush the dirty data,
469 * since that could deadlock in case of a state recovery error.
470 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000471 * Returns zero on success, or a negative errno value.
Trond Myklebust515d8612008-12-23 15:21:46 -0500472 */
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500473int nfs_client_return_marked_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500474{
475 struct nfs_delegation *delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000476 struct nfs_server *server;
Trond Myklebust515d8612008-12-23 15:21:46 -0500477 struct inode *inode;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500478 int err = 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500479
480restart:
481 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000482 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
483 list_for_each_entry_rcu(delegation, &server->delegations,
484 super_list) {
Trond Myklebustb7571442013-04-03 14:33:49 -0400485 if (!nfs_delegation_need_return(delegation))
Chuck Leverd3978bb2010-12-24 01:33:04 +0000486 continue;
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500487 if (!nfs_sb_active(server->super))
Chuck Leverd3978bb2010-12-24 01:33:04 +0000488 continue;
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500489 inode = nfs_delegation_grab_inode(delegation);
490 if (inode == NULL) {
491 rcu_read_unlock();
492 nfs_sb_deactive(server->super);
493 goto restart;
494 }
Trond Myklebustd25be542013-02-05 11:43:28 -0500495 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
Chuck Leverd3978bb2010-12-24 01:33:04 +0000496 rcu_read_unlock();
497
Trond Myklebustd25be542013-02-05 11:43:28 -0500498 err = nfs_end_delegation_return(inode, delegation, 0);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000499 iput(inode);
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500500 nfs_sb_deactive(server->super);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000501 if (!err)
502 goto restart;
503 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
504 return err;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500505 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500506 }
507 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500508 return 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500509}
510
Chuck Leverd3978bb2010-12-24 01:33:04 +0000511/**
512 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
513 * @inode: inode to process
514 *
515 * Does not protect against delegation reclaims, therefore really only safe
516 * to be called from nfs4_clear_inode().
Trond Myklebuste6f81072008-01-24 18:14:34 -0500517 */
518void nfs_inode_return_delegation_noreclaim(struct inode *inode)
519{
Trond Myklebuste6f81072008-01-24 18:14:34 -0500520 struct nfs_delegation *delegation;
521
Trond Myklebustd25be542013-02-05 11:43:28 -0500522 delegation = nfs_inode_detach_delegation(inode);
523 if (delegation != NULL)
Trond Myklebust5fcdfac2015-03-25 13:19:42 -0400524 nfs_do_return_delegation(inode, delegation, 1);
Trond Myklebuste6f81072008-01-24 18:14:34 -0500525}
526
Chuck Leverd3978bb2010-12-24 01:33:04 +0000527/**
528 * nfs_inode_return_delegation - synchronously return a delegation
529 * @inode: inode to process
530 *
Trond Myklebustc57d1bc2012-05-06 19:34:17 -0400531 * This routine will always flush any dirty data to disk on the
532 * assumption that if we need to return the delegation, then
533 * we should stop caching.
534 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000535 * Returns zero on success, or a negative errno value.
536 */
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400537int nfs4_inode_return_delegation(struct inode *inode)
Trond Myklebust90163022007-07-05 14:55:18 -0400538{
Trond Myklebust90163022007-07-05 14:55:18 -0400539 struct nfs_inode *nfsi = NFS_I(inode);
540 struct nfs_delegation *delegation;
541 int err = 0;
542
Trond Myklebustc57d1bc2012-05-06 19:34:17 -0400543 nfs_wb_all(inode);
Trond Myklebustd25be542013-02-05 11:43:28 -0500544 delegation = nfs_start_delegation_return(nfsi);
545 if (delegation != NULL)
546 err = nfs_end_delegation_return(inode, delegation, 1);
Trond Myklebust90163022007-07-05 14:55:18 -0400547 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Trond Myklebustb7571442013-04-03 14:33:49 -0400550static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
551 struct nfs_delegation *delegation)
552{
553 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
554 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
555}
556
Trond Myklebusted1e62112011-07-25 15:37:29 -0400557static void nfs_mark_return_delegation(struct nfs_server *server,
558 struct nfs_delegation *delegation)
Trond Myklebust6411bd42008-12-23 15:21:51 -0500559{
560 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebusted1e62112011-07-25 15:37:29 -0400561 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
Trond Myklebust6411bd42008-12-23 15:21:51 -0500562}
563
Trond Myklebust5c31e232013-04-03 19:04:58 -0400564static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
565{
566 struct nfs_delegation *delegation;
567 bool ret = false;
568
569 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
570 nfs_mark_return_delegation(server, delegation);
571 ret = true;
572 }
573 return ret;
574}
575
Trond Myklebustb02ba0b2013-04-03 19:23:58 -0400576static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
577{
578 struct nfs_server *server;
579
580 rcu_read_lock();
581 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
582 nfs_server_mark_return_all_delegations(server);
583 rcu_read_unlock();
584}
585
586static void nfs_delegation_run_state_manager(struct nfs_client *clp)
587{
588 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
589 nfs4_schedule_state_manager(clp);
590}
591
592/**
593 * nfs_expire_all_delegations
594 * @clp: client to process
595 *
596 */
597void nfs_expire_all_delegations(struct nfs_client *clp)
598{
599 nfs_client_mark_return_all_delegations(clp);
600 nfs_delegation_run_state_manager(clp);
601}
602
Chuck Leverd3978bb2010-12-24 01:33:04 +0000603/**
604 * nfs_super_return_all_delegations - return delegations for one superblock
605 * @sb: sb to process
606 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 */
Bryan Schumakereeebf912012-06-20 15:53:41 -0400608void nfs_server_return_all_delegations(struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000610 struct nfs_client *clp = server->nfs_client;
Trond Myklebust5c31e232013-04-03 19:04:58 -0400611 bool need_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 if (clp == NULL)
614 return;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000615
Trond Myklebust8383e462007-07-06 15:12:04 -0400616 rcu_read_lock();
Trond Myklebust5c31e232013-04-03 19:04:58 -0400617 need_wait = nfs_server_mark_return_all_delegations(server);
Trond Myklebust8383e462007-07-06 15:12:04 -0400618 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000619
Trond Myklebust5c31e232013-04-03 19:04:58 -0400620 if (need_wait) {
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500621 nfs4_schedule_state_manager(clp);
Trond Myklebust5c31e232013-04-03 19:04:58 -0400622 nfs4_wait_clnt_recover(clp);
623 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500624}
625
Trond Myklebust826e0012013-04-03 19:27:52 -0400626static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000627 fmode_t flags)
Trond Myklebust515d8612008-12-23 15:21:46 -0500628{
629 struct nfs_delegation *delegation;
630
Chuck Leverd3978bb2010-12-24 01:33:04 +0000631 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500632 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
633 continue;
634 if (delegation->type & flags)
Trond Myklebust826e0012013-04-03 19:27:52 -0400635 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500636 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000637}
638
Trond Myklebust826e0012013-04-03 19:27:52 -0400639static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000640 fmode_t flags)
641{
642 struct nfs_server *server;
643
644 rcu_read_lock();
645 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
Trond Myklebust826e0012013-04-03 19:27:52 -0400646 nfs_mark_return_unused_delegation_types(server, flags);
Trond Myklebust515d8612008-12-23 15:21:46 -0500647 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Trond Myklebust41020b62016-09-22 13:38:58 -0400650static void nfs_mark_delegation_revoked(struct nfs_server *server,
651 struct nfs_delegation *delegation)
Trond Myklebust869f9df2014-11-10 18:43:56 -0500652{
Trond Myklebust41020b62016-09-22 13:38:58 -0400653 set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
Trond Myklebust059b43e2016-09-22 13:39:06 -0400654 delegation->stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust41020b62016-09-22 13:38:58 -0400655 nfs_mark_return_delegation(server, delegation);
Trond Myklebust869f9df2014-11-10 18:43:56 -0500656}
657
Trond Myklebust41020b62016-09-22 13:38:58 -0400658static bool nfs_revoke_delegation(struct inode *inode,
659 const nfs4_stateid *stateid)
660{
661 struct nfs_delegation *delegation;
Trond Myklebust7f048832016-09-22 13:39:14 -0400662 nfs4_stateid tmp;
Trond Myklebust41020b62016-09-22 13:38:58 -0400663 bool ret = false;
664
665 rcu_read_lock();
666 delegation = rcu_dereference(NFS_I(inode)->delegation);
667 if (delegation == NULL)
668 goto out;
Trond Myklebust7f048832016-09-22 13:39:14 -0400669 if (stateid == NULL) {
670 nfs4_stateid_copy(&tmp, &delegation->stateid);
671 stateid = &tmp;
672 } else if (!nfs4_stateid_match(stateid, &delegation->stateid))
Trond Myklebust41020b62016-09-22 13:38:58 -0400673 goto out;
674 nfs_mark_delegation_revoked(NFS_SERVER(inode), delegation);
675 ret = true;
676out:
677 rcu_read_unlock();
Trond Myklebust7f048832016-09-22 13:39:14 -0400678 if (ret)
679 nfs_inode_find_state_and_recover(inode, stateid);
Trond Myklebust41020b62016-09-22 13:38:58 -0400680 return ret;
681}
682
683void nfs_remove_bad_delegation(struct inode *inode,
684 const nfs4_stateid *stateid)
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500685{
686 struct nfs_delegation *delegation;
687
Trond Myklebust41020b62016-09-22 13:38:58 -0400688 if (!nfs_revoke_delegation(inode, stateid))
689 return;
Trond Myklebustd25be542013-02-05 11:43:28 -0500690 delegation = nfs_inode_detach_delegation(inode);
Trond Myklebust7f048832016-09-22 13:39:14 -0400691 if (delegation)
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500692 nfs_free_delegation(delegation);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500693}
Andy Adamson9cb81962012-03-07 10:49:41 -0500694EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500695
Chuck Leverd3978bb2010-12-24 01:33:04 +0000696/**
Trond Myklebust826e0012013-04-03 19:27:52 -0400697 * nfs_expire_unused_delegation_types
Chuck Leverd3978bb2010-12-24 01:33:04 +0000698 * @clp: client to process
699 * @flags: delegation types to expire
700 *
701 */
Trond Myklebust826e0012013-04-03 19:27:52 -0400702void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500703{
Trond Myklebust826e0012013-04-03 19:27:52 -0400704 nfs_client_mark_return_unused_delegation_types(clp, flags);
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500705 nfs_delegation_run_state_manager(clp);
706}
707
Chuck Leverd3978bb2010-12-24 01:33:04 +0000708static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
Trond Myklebustb7391f42008-12-23 15:21:52 -0500709{
710 struct nfs_delegation *delegation;
711
Chuck Leverd3978bb2010-12-24 01:33:04 +0000712 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebustb7391f42008-12-23 15:21:52 -0500713 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
714 continue;
Trond Myklebustb7571442013-04-03 14:33:49 -0400715 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -0500716 }
Trond Myklebustb7391f42008-12-23 15:21:52 -0500717}
718
Chuck Leverd3978bb2010-12-24 01:33:04 +0000719/**
720 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
721 * @clp: nfs_client to process
722 *
723 */
Trond Myklebustb7391f42008-12-23 15:21:52 -0500724void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
725{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000726 struct nfs_server *server;
727
728 rcu_read_lock();
729 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
730 nfs_mark_return_unreferenced_delegations(server);
731 rcu_read_unlock();
732
Trond Myklebustb7391f42008-12-23 15:21:52 -0500733 nfs_delegation_run_state_manager(clp);
734}
735
Chuck Leverd3978bb2010-12-24 01:33:04 +0000736/**
737 * nfs_async_inode_return_delegation - asynchronously return a delegation
738 * @inode: inode to process
Trond Myklebust8e663f02012-03-04 18:13:56 -0500739 * @stateid: state ID information
Chuck Leverd3978bb2010-12-24 01:33:04 +0000740 *
741 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 */
Chuck Leverd3978bb2010-12-24 01:33:04 +0000743int nfs_async_inode_return_delegation(struct inode *inode,
744 const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Trond Myklebusted1e62112011-07-25 15:37:29 -0400746 struct nfs_server *server = NFS_SERVER(inode);
747 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6411bd42008-12-23 15:21:51 -0500748 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Trond Myklebust6411bd42008-12-23 15:21:51 -0500750 rcu_read_lock();
751 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebust755a48a2014-03-02 22:03:12 -0500752 if (delegation == NULL)
753 goto out_enoent;
Trond Myklebust4816fda2015-09-20 14:58:42 -0400754 if (stateid != NULL &&
755 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
Trond Myklebust755a48a2014-03-02 22:03:12 -0500756 goto out_enoent;
Trond Myklebusted1e62112011-07-25 15:37:29 -0400757 nfs_mark_return_delegation(server, delegation);
Trond Myklebust6411bd42008-12-23 15:21:51 -0500758 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000759
Trond Myklebust6411bd42008-12-23 15:21:51 -0500760 nfs_delegation_run_state_manager(clp);
761 return 0;
Trond Myklebust755a48a2014-03-02 22:03:12 -0500762out_enoent:
763 rcu_read_unlock();
764 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Chuck Leverd3978bb2010-12-24 01:33:04 +0000767static struct inode *
768nfs_delegation_find_inode_server(struct nfs_server *server,
769 const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 struct nfs_delegation *delegation;
772 struct inode *res = NULL;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000773
774 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500775 spin_lock(&delegation->lock);
776 if (delegation->inode != NULL &&
777 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 res = igrab(delegation->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
Trond Myklebust86e89482008-12-23 15:21:39 -0500780 spin_unlock(&delegation->lock);
781 if (res != NULL)
782 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000784 return res;
785}
786
787/**
788 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
789 * @clp: client state handle
790 * @fhandle: filehandle from a delegation recall
791 *
792 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
793 * cannot be found.
794 */
795struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
796 const struct nfs_fh *fhandle)
797{
798 struct nfs_server *server;
799 struct inode *res = NULL;
800
801 rcu_read_lock();
802 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
803 res = nfs_delegation_find_inode_server(server, fhandle);
804 if (res != NULL)
805 break;
806 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400807 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return res;
809}
810
Chuck Leverd3978bb2010-12-24 01:33:04 +0000811static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
812{
813 struct nfs_delegation *delegation;
814
Trond Myklebust45870d62016-09-22 13:38:59 -0400815 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
816 /*
817 * If the delegation may have been admin revoked, then we
818 * cannot reclaim it.
819 */
820 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
821 continue;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000822 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Trond Myklebust45870d62016-09-22 13:38:59 -0400823 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000824}
825
826/**
827 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
828 * @clp: nfs_client to process
829 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 */
David Howellsadfa6f92006-08-22 20:06:08 -0400831void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000833 struct nfs_server *server;
834
Trond Myklebust8383e462007-07-06 15:12:04 -0400835 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000836 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
837 nfs_delegation_mark_reclaim_server(server);
Trond Myklebust8383e462007-07-06 15:12:04 -0400838 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Chuck Leverd3978bb2010-12-24 01:33:04 +0000841/**
842 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
843 * @clp: nfs_client to process
844 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 */
David Howellsadfa6f92006-08-22 20:06:08 -0400846void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Trond Myklebust8383e462007-07-06 15:12:04 -0400848 struct nfs_delegation *delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000849 struct nfs_server *server;
Trond Myklebust86e89482008-12-23 15:21:39 -0500850 struct inode *inode;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000851
Trond Myklebust8383e462007-07-06 15:12:04 -0400852restart:
853 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000854 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
855 list_for_each_entry_rcu(delegation, &server->delegations,
856 super_list) {
Trond Myklebustec3ca4e52015-02-26 14:05:05 -0500857 if (test_bit(NFS_DELEGATION_RETURNING,
858 &delegation->flags))
859 continue;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000860 if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
861 &delegation->flags) == 0)
862 continue;
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500863 if (!nfs_sb_active(server->super))
Chuck Leverd3978bb2010-12-24 01:33:04 +0000864 continue;
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500865 inode = nfs_delegation_grab_inode(delegation);
866 if (inode == NULL) {
867 rcu_read_unlock();
868 nfs_sb_deactive(server->super);
869 goto restart;
870 }
Trond Myklebustb04b22f2015-02-26 13:59:38 -0500871 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
Chuck Leverd3978bb2010-12-24 01:33:04 +0000872 rcu_read_unlock();
Trond Myklebustb04b22f2015-02-26 13:59:38 -0500873 if (delegation != NULL) {
874 delegation = nfs_detach_delegation(NFS_I(inode),
875 delegation, server);
876 if (delegation != NULL)
877 nfs_free_delegation(delegation);
878 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000879 iput(inode);
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500880 nfs_sb_deactive(server->super);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000881 goto restart;
882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400884 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500886
Trond Myklebustbb3d1a32016-09-22 13:39:00 -0400887static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
888{
889 return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
890 BIT(NFS4CLNT_LEASE_EXPIRED) |
891 BIT(NFS4CLNT_SESSION_RESET))) != 0;
892}
893
Trond Myklebust45870d62016-09-22 13:38:59 -0400894static void nfs_mark_test_expired_delegation(struct nfs_server *server,
895 struct nfs_delegation *delegation)
896{
Trond Myklebust059b43e2016-09-22 13:39:06 -0400897 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
898 return;
Trond Myklebust45870d62016-09-22 13:38:59 -0400899 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
900 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
901 set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
902}
903
Trond Myklebustbb3d1a32016-09-22 13:39:00 -0400904static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
905 struct inode *inode)
906{
907 struct nfs_delegation *delegation;
908
909 rcu_read_lock();
910 delegation = rcu_dereference(NFS_I(inode)->delegation);
911 if (delegation)
912 nfs_mark_test_expired_delegation(server, delegation);
913 rcu_read_unlock();
914
915}
916
Trond Myklebust45870d62016-09-22 13:38:59 -0400917static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
918{
919 struct nfs_delegation *delegation;
920
921 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
922 nfs_mark_test_expired_delegation(server, delegation);
923}
924
925/**
926 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
927 * @clp: nfs_client to process
928 *
929 * Iterates through all the delegations associated with this server and
930 * marks them as needing to be checked for validity.
931 */
932void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
933{
934 struct nfs_server *server;
935
936 rcu_read_lock();
937 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
938 nfs_delegation_mark_test_expired_server(server);
939 rcu_read_unlock();
940}
941
942/**
943 * nfs_reap_expired_delegations - reap expired delegations
944 * @clp: nfs_client to process
945 *
946 * Iterates through all the delegations associated with this server and
947 * checks if they have may have been revoked. This function is usually
948 * expected to be called in cases where the server may have lost its
949 * lease.
950 */
951void nfs_reap_expired_delegations(struct nfs_client *clp)
952{
953 const struct nfs4_minor_version_ops *ops = clp->cl_mvops;
954 struct nfs_delegation *delegation;
955 struct nfs_server *server;
956 struct inode *inode;
957 struct rpc_cred *cred;
958 nfs4_stateid stateid;
959
960restart:
961 rcu_read_lock();
962 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
963 list_for_each_entry_rcu(delegation, &server->delegations,
964 super_list) {
965 if (test_bit(NFS_DELEGATION_RETURNING,
966 &delegation->flags))
967 continue;
968 if (test_bit(NFS_DELEGATION_TEST_EXPIRED,
969 &delegation->flags) == 0)
970 continue;
971 if (!nfs_sb_active(server->super))
972 continue;
973 inode = nfs_delegation_grab_inode(delegation);
974 if (inode == NULL) {
975 rcu_read_unlock();
976 nfs_sb_deactive(server->super);
977 goto restart;
978 }
979 cred = get_rpccred_rcu(delegation->cred);
980 nfs4_stateid_copy(&stateid, &delegation->stateid);
981 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
982 rcu_read_unlock();
983 if (cred != NULL &&
984 ops->test_and_free_expired(server, &stateid, cred) < 0) {
985 nfs_revoke_delegation(inode, &stateid);
986 nfs_inode_find_state_and_recover(inode, &stateid);
987 }
988 put_rpccred(cred);
Trond Myklebustbb3d1a32016-09-22 13:39:00 -0400989 if (nfs4_server_rebooted(clp)) {
990 nfs_inode_mark_test_expired_delegation(server,inode);
991 iput(inode);
992 nfs_sb_deactive(server->super);
993 return;
994 }
Trond Myklebust45870d62016-09-22 13:38:59 -0400995 iput(inode);
996 nfs_sb_deactive(server->super);
997 goto restart;
998 }
999 }
1000 rcu_read_unlock();
1001}
1002
Trond Myklebust6c2d8f82016-09-22 13:39:07 -04001003void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
1004 const nfs4_stateid *stateid)
1005{
1006 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1007 struct nfs_delegation *delegation;
1008 bool found = false;
1009
1010 rcu_read_lock();
1011 delegation = rcu_dereference(NFS_I(inode)->delegation);
1012 if (delegation &&
1013 nfs4_stateid_match_other(&delegation->stateid, stateid)) {
1014 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1015 found = true;
1016 }
1017 rcu_read_unlock();
1018 if (found)
1019 nfs4_schedule_state_manager(clp);
1020}
1021
Chuck Leverd3978bb2010-12-24 01:33:04 +00001022/**
1023 * nfs_delegations_present - check for existence of delegations
1024 * @clp: client state handle
1025 *
1026 * Returns one if there are any nfs_delegation structures attached
1027 * to this nfs_client.
1028 */
1029int nfs_delegations_present(struct nfs_client *clp)
1030{
1031 struct nfs_server *server;
1032 int ret = 0;
1033
1034 rcu_read_lock();
1035 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1036 if (!list_empty(&server->delegations)) {
1037 ret = 1;
1038 break;
1039 }
1040 rcu_read_unlock();
1041 return ret;
1042}
1043
1044/**
Trond Myklebust12f275c2017-11-06 15:28:05 -05001045 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1046 * @dst: stateid to refresh
1047 * @inode: inode to check
1048 *
1049 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1050 * that matches our delegation stateid. Otherwise "false" is returned.
1051 */
1052bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
1053{
1054 struct nfs_delegation *delegation;
1055 bool ret = false;
1056 if (!inode)
1057 goto out;
1058
1059 rcu_read_lock();
1060 delegation = rcu_dereference(NFS_I(inode)->delegation);
1061 if (delegation != NULL &&
1062 nfs4_stateid_match_other(dst, &delegation->stateid)) {
1063 dst->seqid = delegation->stateid.seqid;
1064 return ret;
1065 }
1066 rcu_read_unlock();
1067out:
1068 return ret;
1069}
1070
1071/**
Chuck Leverd3978bb2010-12-24 01:33:04 +00001072 * nfs4_copy_delegation_stateid - Copy inode's state ID information
Chuck Leverd3978bb2010-12-24 01:33:04 +00001073 * @inode: inode to check
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001074 * @flags: delegation type requirement
Trond Myklebustabf4e132016-05-16 17:42:44 -04001075 * @dst: stateid data structure to fill in
1076 * @cred: optional argument to retrieve credential
Chuck Leverd3978bb2010-12-24 01:33:04 +00001077 *
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001078 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1079 * otherwise "false" is returned.
Chuck Leverd3978bb2010-12-24 01:33:04 +00001080 */
Trond Myklebustabf4e132016-05-16 17:42:44 -04001081bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
1082 nfs4_stateid *dst, struct rpc_cred **cred)
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001083{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001084 struct nfs_inode *nfsi = NFS_I(inode);
1085 struct nfs_delegation *delegation;
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001086 bool ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001087
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001088 flags &= FMODE_READ|FMODE_WRITE;
Trond Myklebust8383e462007-07-06 15:12:04 -04001089 rcu_read_lock();
1090 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebustaa05c872016-09-22 13:38:54 -04001091 ret = nfs4_is_valid_delegation(delegation, flags);
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001092 if (ret) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001093 nfs4_stateid_copy(dst, &delegation->stateid);
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001094 nfs_mark_delegation_referenced(delegation);
Trond Myklebustabf4e132016-05-16 17:42:44 -04001095 if (cred)
1096 *cred = get_rpccred(delegation->cred);
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001097 }
Trond Myklebust8383e462007-07-06 15:12:04 -04001098 rcu_read_unlock();
1099 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001100}
Trond Myklebust5445b1f2015-09-05 19:06:58 -04001101
1102/**
1103 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1104 * @inode: inode to check
1105 *
1106 * This function checks the number of outstanding writes to the file
1107 * against the delegation 'space_limit' field to see if
1108 * the spec requires us to flush the file on close.
1109 */
1110bool nfs4_delegation_flush_on_close(const struct inode *inode)
1111{
1112 struct nfs_inode *nfsi = NFS_I(inode);
1113 struct nfs_delegation *delegation;
1114 bool ret = true;
1115
1116 rcu_read_lock();
1117 delegation = rcu_dereference(nfsi->delegation);
1118 if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1119 goto out;
Trond Myklebusta6b6d5b2017-08-01 15:39:46 -04001120 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)
Trond Myklebust5445b1f2015-09-05 19:06:58 -04001121 ret = false;
1122out:
1123 rcu_read_unlock();
1124 return ret;
1125}