blob: 9a5d911a7edc77cae53cac45aa43f196b32caf20 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4state.c
3 *
4 * Client-side XDR for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 * Implementation of the NFSv4 state model. For the time being,
37 * this is minimal, but will be made much more complex in a
38 * subsequent patch.
39 */
40
Trond Myklebust6f43ddc2007-07-08 16:49:11 -040041#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/slab.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020043#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/nfs_fs.h>
Trond Myklebust5043e902006-01-03 09:55:23 +010045#include <linux/kthread.h>
46#include <linux/module.h>
Trond Myklebust9f958ab2007-07-02 13:58:33 -040047#include <linux/random.h>
Randy Dunlap8c7597f2010-10-22 16:18:52 -070048#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/workqueue.h>
50#include <linux/bitops.h>
Chuck Lever0aaaf5c2011-12-06 16:13:48 -050051#include <linux/jiffies.h>
Trond Myklebust3e178982022-01-29 13:32:45 -050052#include <linux/sched/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Chuck Lever05f4c352012-09-14 17:24:32 -040054#include <linux/sunrpc/clnt.h>
55
Trond Myklebust4ce79712005-06-22 17:16:21 +000056#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include "callback.h"
58#include "delegation.h"
David Howells24c8dbb2006-08-22 20:06:10 -040059#include "internal.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040060#include "nfs4idmap.h"
Trond Myklebust76e697b2012-11-26 14:20:49 -050061#include "nfs4session.h"
Andy Adamson974cec82010-10-20 00:18:02 -040062#include "pnfs.h"
Stanislav Kinsburskybbe0a3a2012-08-20 18:00:36 +040063#include "netns.h"
Chuck Lever511ba522019-11-05 11:04:07 -050064#include "nfs4trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Chuck Levere3c0fb72012-05-21 22:45:24 -040066#define NFSDBG_FACILITY NFSDBG_STATE
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define OPENOWNER_POOL_SIZE 8
69
Trond Myklebust40882de2023-03-13 18:45:53 -040070static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp);
71
Trond Myklebust93b717f2016-05-16 17:42:43 -040072const nfs4_stateid zero_stateid = {
Linus Torvaldse0714ec2016-05-27 17:20:27 -070073 { .data = { 0 } },
Trond Myklebust93b717f2016-05-16 17:42:43 -040074 .type = NFS4_SPECIAL_STATEID_TYPE,
75};
Trond Myklebustfcd88432017-11-07 12:39:44 -050076const nfs4_stateid invalid_stateid = {
77 {
Trond Myklebust445f2882017-11-18 13:50:11 -050078 /* Funky initialiser keeps older gcc versions happy */
79 .data = { 0xff, 0xff, 0xff, 0xff, 0 },
Trond Myklebustfcd88432017-11-07 12:39:44 -050080 },
81 .type = NFS4_INVALID_STATEID_TYPE,
82};
83
Fred Isaman2409a972016-10-06 12:11:21 -040084const nfs4_stateid current_stateid = {
85 {
86 /* Funky initialiser keeps older gcc versions happy */
87 .data = { 0x0, 0x0, 0x0, 0x1, 0 },
88 },
89 .type = NFS4_SPECIAL_STATEID_TYPE,
90};
91
Chuck Lever05f4c352012-09-14 17:24:32 -040092static DEFINE_MUTEX(nfs_clid_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Donald Buczek5b596832019-07-07 21:26:10 +020094static int nfs4_setup_state_renewal(struct nfs_client *clp)
95{
96 int status;
97 struct nfs_fsinfo fsinfo;
Donald Buczek5b596832019-07-07 21:26:10 +020098
99 if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
100 nfs4_schedule_state_renewal(clp);
101 return 0;
102 }
103
Donald Buczek5b596832019-07-07 21:26:10 +0200104 status = nfs4_proc_get_lease_time(clp, &fsinfo);
105 if (status == 0) {
Robert Milkowski7dc29932020-01-30 09:43:25 +0000106 nfs4_set_lease_period(clp, fsinfo.lease_time * HZ);
Donald Buczek5b596832019-07-07 21:26:10 +0200107 nfs4_schedule_state_renewal(clp);
108 }
109
110 return status;
111}
112
NeilBrowna52458b2018-12-03 11:30:31 +1100113int nfs4_init_clientid(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Trond Myklebustfd954ae2011-04-24 14:28:18 -0400115 struct nfs4_setclientid_res clid = {
116 .clientid = clp->cl_clientid,
117 .confirm = clp->cl_confirm,
118 };
Chuck Leverf738f512009-03-18 20:48:06 -0400119 unsigned short port;
120 int status;
Stanislav Kinsburskybbe0a3a2012-08-20 18:00:36 +0400121 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Chuck Leverf738f512009-03-18 20:48:06 -0400122
Trond Myklebustfd954ae2011-04-24 14:28:18 -0400123 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
124 goto do_confirm;
Stanislav Kinsburskybbe0a3a2012-08-20 18:00:36 +0400125 port = nn->nfs_callback_tcpport;
Chuck Leverf738f512009-03-18 20:48:06 -0400126 if (clp->cl_addr.ss_family == AF_INET6)
Stanislav Kinsbursky29dcc162012-08-20 18:00:41 +0400127 port = nn->nfs_callback_tcpport6;
Chuck Leverf738f512009-03-18 20:48:06 -0400128
Trond Myklebustbb8b27e2010-04-16 16:43:06 -0400129 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
130 if (status != 0)
131 goto out;
Trond Myklebustfd954ae2011-04-24 14:28:18 -0400132 clp->cl_clientid = clid.clientid;
133 clp->cl_confirm = clid.confirm;
134 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
135do_confirm:
Trond Myklebustbb8b27e2010-04-16 16:43:06 -0400136 status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
137 if (status != 0)
138 goto out;
Trond Myklebustfd954ae2011-04-24 14:28:18 -0400139 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
Donald Buczek5b596832019-07-07 21:26:10 +0200140 nfs4_setup_state_renewal(clp);
Trond Myklebustbb8b27e2010-04-16 16:43:06 -0400141out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return status;
143}
144
Chuck Lever05f4c352012-09-14 17:24:32 -0400145/**
146 * nfs40_discover_server_trunking - Detect server IP address trunking (mv0)
147 *
148 * @clp: nfs_client under test
149 * @result: OUT: found nfs_client, or clp
150 * @cred: credential to use for trunking test
151 *
152 * Returns zero, a negative errno, or a negative NFS4ERR status.
153 * If zero is returned, an nfs_client pointer is planted in
154 * "result".
155 *
156 * Note: The returned client may not yet be marked ready.
157 */
158int nfs40_discover_server_trunking(struct nfs_client *clp,
159 struct nfs_client **result,
NeilBrowna52458b2018-12-03 11:30:31 +1100160 const struct cred *cred)
Chuck Lever05f4c352012-09-14 17:24:32 -0400161{
162 struct nfs4_setclientid_res clid = {
163 .clientid = clp->cl_clientid,
164 .confirm = clp->cl_confirm,
165 };
Trond Myklebust9f623872012-10-01 16:17:31 -0700166 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Chuck Lever05f4c352012-09-14 17:24:32 -0400167 unsigned short port;
168 int status;
169
Trond Myklebust9f623872012-10-01 16:17:31 -0700170 port = nn->nfs_callback_tcpport;
Chuck Lever05f4c352012-09-14 17:24:32 -0400171 if (clp->cl_addr.ss_family == AF_INET6)
Trond Myklebust9f623872012-10-01 16:17:31 -0700172 port = nn->nfs_callback_tcpport6;
Chuck Lever05f4c352012-09-14 17:24:32 -0400173
174 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
175 if (status != 0)
176 goto out;
177 clp->cl_clientid = clid.clientid;
178 clp->cl_confirm = clid.confirm;
179
180 status = nfs40_walk_client_list(clp, result, cred);
Trond Myklebust202c3122013-01-18 22:56:23 -0500181 if (status == 0) {
Chuck Lever05f4c352012-09-14 17:24:32 -0400182 /* Sustain the lease, even if it's empty. If the clientid4
183 * goes stale it's of no use for trunking discovery. */
184 nfs4_schedule_state_renewal(*result);
ZhangXiaoxuf02f3752019-05-06 11:57:03 +0800185
186 /* If the client state need to recover, do it. */
187 if (clp->cl_state)
188 nfs4_schedule_state_manager(clp);
Chuck Lever05f4c352012-09-14 17:24:32 -0400189 }
Chuck Lever05f4c352012-09-14 17:24:32 -0400190out:
191 return status;
192}
193
NeilBrowna52458b2018-12-03 11:30:31 +1100194const struct cred *nfs4_get_machine_cred(struct nfs_client *clp)
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400195{
NeilBrowna52458b2018-12-03 11:30:31 +1100196 return get_cred(rpc_machine_cred());
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400197}
198
Chuck Leverd688f7b2013-07-24 12:28:28 -0400199static void nfs4_root_machine_cred(struct nfs_client *clp)
200{
Chuck Leverd688f7b2013-07-24 12:28:28 -0400201
NeilBrown5e169232018-12-03 11:30:30 +1100202 /* Force root creds instead of machine */
203 clp->cl_principal = NULL;
204 clp->cl_rpcclient->cl_principal = NULL;
Chuck Leverd688f7b2013-07-24 12:28:28 -0400205}
206
NeilBrowna52458b2018-12-03 11:30:31 +1100207static const struct cred *
Chuck Lever24d292b2010-12-24 01:32:43 +0000208nfs4_get_renew_cred_server_locked(struct nfs_server *server)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100209{
NeilBrowna52458b2018-12-03 11:30:31 +1100210 const struct cred *cred = NULL;
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100211 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400212 struct rb_node *pos;
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100213
Chuck Lever24d292b2010-12-24 01:32:43 +0000214 for (pos = rb_first(&server->state_owners);
215 pos != NULL;
216 pos = rb_next(pos)) {
217 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100218 if (list_empty(&sp->so_states))
219 continue;
NeilBrowna52458b2018-12-03 11:30:31 +1100220 cred = get_cred(sp->so_cred);
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100221 break;
222 }
223 return cred;
224}
225
Chuck Lever24d292b2010-12-24 01:32:43 +0000226/**
NeilBrownf15e1e82018-12-03 11:30:30 +1100227 * nfs4_get_renew_cred - Acquire credential for a renew operation
Chuck Lever24d292b2010-12-24 01:32:43 +0000228 * @clp: client state handle
229 *
230 * Returns an rpc_cred with reference count bumped, or NULL.
231 * Caller must hold clp->cl_lock.
232 */
NeilBrowna52458b2018-12-03 11:30:31 +1100233const struct cred *nfs4_get_renew_cred(struct nfs_client *clp)
Chuck Lever24d292b2010-12-24 01:32:43 +0000234{
NeilBrowna52458b2018-12-03 11:30:31 +1100235 const struct cred *cred = NULL;
Chuck Lever24d292b2010-12-24 01:32:43 +0000236 struct nfs_server *server;
237
Sachin Prabhue49a29b2012-03-16 19:25:52 +0000238 /* Use machine credentials if available */
NeilBrownf15e1e82018-12-03 11:30:30 +1100239 cred = nfs4_get_machine_cred(clp);
Sachin Prabhue49a29b2012-03-16 19:25:52 +0000240 if (cred != NULL)
241 goto out;
242
NeilBrownf15e1e82018-12-03 11:30:30 +1100243 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000244 rcu_read_lock();
245 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
246 cred = nfs4_get_renew_cred_server_locked(server);
247 if (cred != NULL)
248 break;
249 }
250 rcu_read_unlock();
NeilBrownf15e1e82018-12-03 11:30:30 +1100251 spin_unlock(&clp->cl_lock);
Sachin Prabhue49a29b2012-03-16 19:25:52 +0000252
253out:
Chuck Lever24d292b2010-12-24 01:32:43 +0000254 return cred;
255}
256
Andy Adamson62f288a2013-06-19 16:39:44 -0400257static void nfs4_end_drain_slot_table(struct nfs4_slot_table *tbl)
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500258{
Andy Adamson774d5f12013-05-20 14:13:50 -0400259 if (test_and_clear_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500260 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500261 nfs41_wake_slot_table(tbl);
Trond Myklebust961a8282012-01-17 22:57:37 -0500262 spin_unlock(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800263 }
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500264}
265
Andy Adamson62f288a2013-06-19 16:39:44 -0400266static void nfs4_end_drain_session(struct nfs_client *clp)
267{
268 struct nfs4_session *ses = clp->cl_session;
269
Chuck Lever2cf8bca2013-08-09 12:49:56 -0400270 if (clp->cl_slot_tbl) {
271 nfs4_end_drain_slot_table(clp->cl_slot_tbl);
272 return;
273 }
274
Andy Adamson62f288a2013-06-19 16:39:44 -0400275 if (ses != NULL) {
276 nfs4_end_drain_slot_table(&ses->bc_slot_table);
277 nfs4_end_drain_slot_table(&ses->fc_slot_table);
278 }
279}
280
Andy Adamson774d5f12013-05-20 14:13:50 -0400281static int nfs4_drain_slot_tbl(struct nfs4_slot_table *tbl)
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500282{
Andy Adamson774d5f12013-05-20 14:13:50 -0400283 set_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state);
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500284 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonb6bf6e72012-02-17 13:05:23 -0500285 if (tbl->highest_used_slotid != NFS4_NO_SLOT) {
Wolfram Sang16735d02013-11-14 14:32:02 -0800286 reinit_completion(&tbl->complete);
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500287 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson42acd022011-01-06 02:04:34 +0000288 return wait_for_completion_interruptible(&tbl->complete);
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500289 }
290 spin_unlock(&tbl->slot_tbl_lock);
291 return 0;
292}
293
Andy Adamson42acd022011-01-06 02:04:34 +0000294static int nfs4_begin_drain_session(struct nfs_client *clp)
295{
296 struct nfs4_session *ses = clp->cl_session;
Trond Myklebust8aafd2f2018-08-11 11:52:38 -0400297 int ret;
Andy Adamson42acd022011-01-06 02:04:34 +0000298
Chuck Lever2cf8bca2013-08-09 12:49:56 -0400299 if (clp->cl_slot_tbl)
300 return nfs4_drain_slot_tbl(clp->cl_slot_tbl);
301
Andy Adamson42acd022011-01-06 02:04:34 +0000302 /* back channel */
Andy Adamson774d5f12013-05-20 14:13:50 -0400303 ret = nfs4_drain_slot_tbl(&ses->bc_slot_table);
Andy Adamson42acd022011-01-06 02:04:34 +0000304 if (ret)
305 return ret;
306 /* fore channel */
Andy Adamson774d5f12013-05-20 14:13:50 -0400307 return nfs4_drain_slot_tbl(&ses->fc_slot_table);
Andy Adamson42acd022011-01-06 02:04:34 +0000308}
309
Chuck Leverc9fdeb22013-10-17 14:13:02 -0400310#if defined(CONFIG_NFS_V4_1)
311
Trond Myklebustbda197f2012-06-05 10:22:14 -0400312static void nfs41_finish_session_reset(struct nfs_client *clp)
313{
314 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
315 clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
316 /* create_session negotiated new slot table */
Trond Myklebustbda197f2012-06-05 10:22:14 -0400317 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
Donald Buczekea51efa2019-07-07 21:26:09 +0200318 nfs4_setup_state_renewal(clp);
Trond Myklebustbda197f2012-06-05 10:22:14 -0400319}
320
NeilBrowna52458b2018-12-03 11:30:31 +1100321int nfs41_init_clientid(struct nfs_client *clp, const struct cred *cred)
Andy Adamson4d643d12009-12-04 15:52:24 -0500322{
323 int status;
324
Trond Myklebustfd954ae2011-04-24 14:28:18 -0400325 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
326 goto do_confirm;
Andy Adamson4d643d12009-12-04 15:52:24 -0500327 status = nfs4_proc_exchange_id(clp, cred);
Ricardo Labiaga9430fb6b2009-12-06 12:23:46 -0500328 if (status != 0)
329 goto out;
Trond Myklebustfd954ae2011-04-24 14:28:18 -0400330 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
331do_confirm:
Trond Myklebust848f5bd2012-05-25 17:51:23 -0400332 status = nfs4_proc_create_session(clp, cred);
Ricardo Labiaga9430fb6b2009-12-06 12:23:46 -0500333 if (status != 0)
334 goto out;
Trond Myklebust40882de2023-03-13 18:45:53 -0400335 if (!(clp->cl_exchange_flags & EXCHGID4_FLAG_CONFIRMED_R))
336 nfs4_state_start_reclaim_reboot(clp);
Trond Myklebustbda197f2012-06-05 10:22:14 -0400337 nfs41_finish_session_reset(clp);
Ricardo Labiaga9430fb6b2009-12-06 12:23:46 -0500338 nfs_mark_client_ready(clp, NFS_CS_READY);
339out:
Andy Adamson4d643d12009-12-04 15:52:24 -0500340 return status;
341}
342
Chuck Lever05f4c352012-09-14 17:24:32 -0400343/**
344 * nfs41_discover_server_trunking - Detect server IP address trunking (mv1)
345 *
346 * @clp: nfs_client under test
347 * @result: OUT: found nfs_client, or clp
348 * @cred: credential to use for trunking test
349 *
350 * Returns NFS4_OK, a negative errno, or a negative NFS4ERR status.
351 * If NFS4_OK is returned, an nfs_client pointer is planted in
352 * "result".
353 *
354 * Note: The returned client may not yet be marked ready.
355 */
356int nfs41_discover_server_trunking(struct nfs_client *clp,
357 struct nfs_client **result,
NeilBrowna52458b2018-12-03 11:30:31 +1100358 const struct cred *cred)
Chuck Lever05f4c352012-09-14 17:24:32 -0400359{
360 int status;
361
362 status = nfs4_proc_exchange_id(clp, cred);
363 if (status != NFS4_OK)
364 return status;
365
Trond Myklebust48d66b92015-03-03 20:28:59 -0500366 status = nfs41_walk_client_list(clp, result, cred);
367 if (status < 0)
368 return status;
369 if (clp != *result)
370 return 0;
371
Chuck Lever8dcbec62017-06-08 11:52:44 -0400372 /*
373 * Purge state if the client id was established in a prior
374 * instance and the client id could not have arrived on the
375 * server via Transparent State Migration.
376 */
377 if (clp->cl_exchange_flags & EXCHGID4_FLAG_CONFIRMED_R) {
378 if (!test_bit(NFS_CS_TSM_POSSIBLE, &clp->cl_flags))
379 set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
380 else
381 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
382 }
Trond Myklebust48d66b92015-03-03 20:28:59 -0500383 nfs4_schedule_state_manager(clp);
384 status = nfs_wait_client_init_complete(clp);
385 if (status < 0)
386 nfs_put_client(clp);
387 return status;
Chuck Lever05f4c352012-09-14 17:24:32 -0400388}
389
Andy Adamsonb4b82602009-04-01 09:22:49 -0400390#endif /* CONFIG_NFS_V4_1 */
391
Chuck Lever24d292b2010-12-24 01:32:43 +0000392/**
Chuck Lever73d8bde2013-07-24 12:28:37 -0400393 * nfs4_get_clid_cred - Acquire credential for a setclientid operation
Chuck Lever24d292b2010-12-24 01:32:43 +0000394 * @clp: client state handle
395 *
NeilBrowna52458b2018-12-03 11:30:31 +1100396 * Returns a cred with reference count bumped, or NULL.
Chuck Lever24d292b2010-12-24 01:32:43 +0000397 */
NeilBrowna52458b2018-12-03 11:30:31 +1100398const struct cred *nfs4_get_clid_cred(struct nfs_client *clp)
Chuck Lever24d292b2010-12-24 01:32:43 +0000399{
NeilBrowna52458b2018-12-03 11:30:31 +1100400 const struct cred *cred;
Chuck Lever24d292b2010-12-24 01:32:43 +0000401
NeilBrownf15e1e82018-12-03 11:30:30 +1100402 cred = nfs4_get_machine_cred(clp);
Chuck Lever24d292b2010-12-24 01:32:43 +0000403 return cred;
404}
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406static struct nfs4_state_owner *
NeilBrowna52458b2018-12-03 11:30:31 +1100407nfs4_find_state_owner_locked(struct nfs_server *server, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Chuck Lever24d292b2010-12-24 01:32:43 +0000409 struct rb_node **p = &server->state_owners.rb_node,
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400410 *parent = NULL;
Chuck Lever414adf12011-12-06 16:13:39 -0500411 struct nfs4_state_owner *sp;
NeilBrowna52458b2018-12-03 11:30:31 +1100412 int cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400414 while (*p != NULL) {
415 parent = *p;
Chuck Lever24d292b2010-12-24 01:32:43 +0000416 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
NeilBrowna52458b2018-12-03 11:30:31 +1100417 cmp = cred_fscmp(cred, sp->so_cred);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400418
NeilBrowna52458b2018-12-03 11:30:31 +1100419 if (cmp < 0)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400420 p = &parent->rb_left;
NeilBrowna52458b2018-12-03 11:30:31 +1100421 else if (cmp > 0)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400422 p = &parent->rb_right;
423 else {
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500424 if (!list_empty(&sp->so_lru))
425 list_del_init(&sp->so_lru);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400426 atomic_inc(&sp->so_count);
Chuck Lever414adf12011-12-06 16:13:39 -0500427 return sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Chuck Lever414adf12011-12-06 16:13:39 -0500430 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400433static struct nfs4_state_owner *
Chuck Lever24d292b2010-12-24 01:32:43 +0000434nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400435{
Chuck Lever24d292b2010-12-24 01:32:43 +0000436 struct nfs_server *server = new->so_server;
437 struct rb_node **p = &server->state_owners.rb_node,
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400438 *parent = NULL;
439 struct nfs4_state_owner *sp;
NeilBrowna52458b2018-12-03 11:30:31 +1100440 int cmp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400441
442 while (*p != NULL) {
443 parent = *p;
Chuck Lever24d292b2010-12-24 01:32:43 +0000444 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
NeilBrowna52458b2018-12-03 11:30:31 +1100445 cmp = cred_fscmp(new->so_cred, sp->so_cred);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400446
NeilBrowna52458b2018-12-03 11:30:31 +1100447 if (cmp < 0)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400448 p = &parent->rb_left;
NeilBrowna52458b2018-12-03 11:30:31 +1100449 else if (cmp > 0)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400450 p = &parent->rb_right;
451 else {
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500452 if (!list_empty(&sp->so_lru))
453 list_del_init(&sp->so_lru);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400454 atomic_inc(&sp->so_count);
455 return sp;
456 }
457 }
Chuck Lever24d292b2010-12-24 01:32:43 +0000458 rb_link_node(&new->so_server_node, parent, p);
459 rb_insert_color(&new->so_server_node, &server->state_owners);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400460 return new;
461}
462
463static void
Chuck Lever24d292b2010-12-24 01:32:43 +0000464nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400465{
Chuck Lever24d292b2010-12-24 01:32:43 +0000466 struct nfs_server *server = sp->so_server;
467
468 if (!RB_EMPTY_NODE(&sp->so_server_node))
469 rb_erase(&sp->so_server_node, &server->state_owners);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400470}
471
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500472static void
473nfs4_init_seqid_counter(struct nfs_seqid_counter *sc)
474{
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400475 sc->create_time = ktime_get();
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500476 sc->flags = 0;
477 sc->counter = 0;
478 spin_lock_init(&sc->lock);
479 INIT_LIST_HEAD(&sc->list);
480 rpc_init_wait_queue(&sc->wait, "Seqid_waitqueue");
481}
482
483static void
484nfs4_destroy_seqid_counter(struct nfs_seqid_counter *sc)
485{
486 rpc_destroy_wait_queue(&sc->wait);
487}
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489/*
490 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
491 * create a new state_owner.
492 *
493 */
494static struct nfs4_state_owner *
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500495nfs4_alloc_state_owner(struct nfs_server *server,
NeilBrowna52458b2018-12-03 11:30:31 +1100496 const struct cred *cred,
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500497 gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 struct nfs4_state_owner *sp;
500
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500501 sp = kzalloc(sizeof(*sp), gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (!sp)
503 return NULL;
Bo Liu724e2df2022-06-15 02:27:45 -0400504 sp->so_seqid.owner_id = ida_alloc(&server->openowner_id, gfp_flags);
Matthew Wilcoxaae57302018-03-14 19:48:27 -0700505 if (sp->so_seqid.owner_id < 0) {
506 kfree(sp);
507 return NULL;
508 }
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500509 sp->so_server = server;
NeilBrowna52458b2018-12-03 11:30:31 +1100510 sp->so_cred = get_cred(cred);
Trond Myklebustec073422005-10-20 14:22:47 -0700511 spin_lock_init(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 INIT_LIST_HEAD(&sp->so_states);
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500513 nfs4_init_seqid_counter(&sp->so_seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 atomic_set(&sp->so_count, 1);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500515 INIT_LIST_HEAD(&sp->so_lru);
Ahmed S. Darwish76246c92020-07-20 17:55:27 +0200516 seqcount_spinlock_init(&sp->so_reclaim_seqcount, &sp->so_lock);
Trond Myklebust65b62a22013-02-07 10:54:07 -0500517 mutex_init(&sp->so_delegreturn_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return sp;
519}
520
Adrian Bunk1d2e88e2008-05-02 13:42:45 -0700521static void
NeilBrown86cfb042016-12-19 11:48:23 +1100522nfs4_reset_state_owner(struct nfs4_state_owner *sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
NeilBrown86cfb042016-12-19 11:48:23 +1100524 /* This state_owner is no longer usable, but must
525 * remain in place so that state recovery can find it
526 * and the opens associated with it.
527 * It may also be used for new 'open' request to
528 * return a delegation to the server.
529 * So update the 'create_time' so that it looks like
530 * a new state_owner. This will cause the server to
531 * request an OPEN_CONFIRM to start a new sequence.
532 */
533 sp->so_seqid.create_time = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500536static void nfs4_free_state_owner(struct nfs4_state_owner *sp)
537{
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500538 nfs4_destroy_seqid_counter(&sp->so_seqid);
NeilBrowna52458b2018-12-03 11:30:31 +1100539 put_cred(sp->so_cred);
Bo Liu724e2df2022-06-15 02:27:45 -0400540 ida_free(&sp->so_server->openowner_id, sp->so_seqid.owner_id);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500541 kfree(sp);
542}
543
544static void nfs4_gc_state_owners(struct nfs_server *server)
545{
546 struct nfs_client *clp = server->nfs_client;
547 struct nfs4_state_owner *sp, *tmp;
548 unsigned long time_min, time_max;
549 LIST_HEAD(doomed);
550
551 spin_lock(&clp->cl_lock);
552 time_max = jiffies;
553 time_min = (long)time_max - (long)clp->cl_lease_time;
554 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
555 /* NB: LRU is sorted so that oldest is at the head */
556 if (time_in_range(sp->so_expires, time_min, time_max))
557 break;
558 list_move(&sp->so_lru, &doomed);
559 nfs4_remove_state_owner_locked(sp);
560 }
561 spin_unlock(&clp->cl_lock);
562
563 list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
564 list_del(&sp->so_lru);
565 nfs4_free_state_owner(sp);
566 }
567}
568
Chuck Lever24d292b2010-12-24 01:32:43 +0000569/**
570 * nfs4_get_state_owner - Look up a state owner given a credential
571 * @server: nfs_server to search
572 * @cred: RPC credential to match
Trond Myklebust302fad72019-02-18 13:32:38 -0500573 * @gfp_flags: allocation mode
Chuck Lever24d292b2010-12-24 01:32:43 +0000574 *
575 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
576 */
577struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
NeilBrowna52458b2018-12-03 11:30:31 +1100578 const struct cred *cred,
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500579 gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
David Howells7539bba2006-08-22 20:06:09 -0400581 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 struct nfs4_state_owner *sp, *new;
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000585 sp = nfs4_find_state_owner_locked(server, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 spin_unlock(&clp->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (sp != NULL)
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500588 goto out;
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500589 new = nfs4_alloc_state_owner(server, cred, gfp_flags);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400590 if (new == NULL)
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500591 goto out;
Matthew Wilcoxaae57302018-03-14 19:48:27 -0700592 spin_lock(&clp->cl_lock);
593 sp = nfs4_insert_state_owner_locked(new);
594 spin_unlock(&clp->cl_lock);
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500595 if (sp != new)
596 nfs4_free_state_owner(new);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500597out:
598 nfs4_gc_state_owners(server);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400599 return sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
Chuck Lever24d292b2010-12-24 01:32:43 +0000602/**
603 * nfs4_put_state_owner - Release a nfs4_state_owner
604 * @sp: state owner data to release
Trond Myklebust7bf97bc2012-04-21 12:36:19 -0400605 *
606 * Note that we keep released state owners on an LRU
607 * list.
608 * This caches valid state owners so that they can be
609 * reused, to avoid the OPEN_CONFIRM on minor version 0.
610 * It also pins the uniquifier of dropped state owners for
611 * a while, to ensure that those state owner names are
612 * never reused.
Chuck Lever24d292b2010-12-24 01:32:43 +0000613 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614void nfs4_put_state_owner(struct nfs4_state_owner *sp)
615{
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500616 struct nfs_server *server = sp->so_server;
617 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
620 return;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500621
Trond Myklebust7bf97bc2012-04-21 12:36:19 -0400622 sp->so_expires = jiffies;
623 list_add_tail(&sp->so_lru, &server->state_owners_lru);
624 spin_unlock(&clp->cl_lock);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500625}
626
627/**
628 * nfs4_purge_state_owners - Release all cached state owners
629 * @server: nfs_server with cached state owners to release
Trond Myklebustc77e2282019-08-03 10:11:27 -0400630 * @head: resulting list of state owners
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500631 *
632 * Called at umount time. Remaining state owners will be on
633 * the LRU with ref count of zero.
Trond Myklebustc77e2282019-08-03 10:11:27 -0400634 * Note that the state owners are not freed, but are added
635 * to the list @head, which can later be used as an argument
636 * to nfs4_free_state_owners.
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500637 */
Trond Myklebustc77e2282019-08-03 10:11:27 -0400638void nfs4_purge_state_owners(struct nfs_server *server, struct list_head *head)
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500639{
640 struct nfs_client *clp = server->nfs_client;
641 struct nfs4_state_owner *sp, *tmp;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500642
643 spin_lock(&clp->cl_lock);
644 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
Trond Myklebustc77e2282019-08-03 10:11:27 -0400645 list_move(&sp->so_lru, head);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500646 nfs4_remove_state_owner_locked(sp);
647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 spin_unlock(&clp->cl_lock);
Trond Myklebustc77e2282019-08-03 10:11:27 -0400649}
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500650
Trond Myklebustc77e2282019-08-03 10:11:27 -0400651/**
Trond Myklebust6453bcd2021-03-04 20:29:50 -0500652 * nfs4_free_state_owners - Release all cached state owners
Trond Myklebustc77e2282019-08-03 10:11:27 -0400653 * @head: resulting list of state owners
654 *
655 * Frees a list of state owners that was generated by
656 * nfs4_purge_state_owners
657 */
658void nfs4_free_state_owners(struct list_head *head)
659{
660 struct nfs4_state_owner *sp, *tmp;
661
662 list_for_each_entry_safe(sp, tmp, head, so_lru) {
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500663 list_del(&sp->so_lru);
664 nfs4_free_state_owner(sp);
665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
668static struct nfs4_state *
669nfs4_alloc_open_state(void)
670{
671 struct nfs4_state *state;
672
Trond Myklebust9c00fd92022-01-29 13:42:01 -0500673 state = kzalloc(sizeof(*state), GFP_KERNEL_ACCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (!state)
675 return NULL;
Trond Myklebustace9fad2018-09-02 19:19:07 -0400676 refcount_set(&state->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 INIT_LIST_HEAD(&state->lock_states);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000678 spin_lock_init(&state->state_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400679 seqlock_init(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -0500680 init_waitqueue_head(&state->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return state;
682}
683
Trond Myklebust4cecb762005-11-04 15:32:58 -0500684void
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500685nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
Trond Myklebust4cecb762005-11-04 15:32:58 -0500686{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500687 if (state->state == fmode)
Trond Myklebust4cecb762005-11-04 15:32:58 -0500688 return;
689 /* NB! List reordering - see the reclaim code for why. */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500690 if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
691 if (fmode & FMODE_WRITE)
Trond Myklebust4cecb762005-11-04 15:32:58 -0500692 list_move(&state->open_states, &state->owner->so_states);
693 else
694 list_move_tail(&state->open_states, &state->owner->so_states);
695 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500696 state->state = fmode;
Trond Myklebust4cecb762005-11-04 15:32:58 -0500697}
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699static struct nfs4_state *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700__nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
701{
702 struct nfs_inode *nfsi = NFS_I(inode);
703 struct nfs4_state *state;
704
Trond Myklebust9ae075f2018-09-02 19:15:15 -0400705 list_for_each_entry_rcu(state, &nfsi->open_states, inode_states) {
Trond Myklebust1c816ef2007-07-03 14:41:19 -0400706 if (state->owner != owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 continue;
Trond Myklebust5d422302013-03-14 16:57:48 -0400708 if (!nfs4_valid_open_stateid(state))
709 continue;
Trond Myklebustace9fad2018-09-02 19:19:07 -0400710 if (refcount_inc_not_zero(&state->count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713 return NULL;
714}
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716static void
717nfs4_free_open_state(struct nfs4_state *state)
718{
Trond Myklebust9ae075f2018-09-02 19:15:15 -0400719 kfree_rcu(state, rcu_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
722struct nfs4_state *
723nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
724{
725 struct nfs4_state *state, *new;
726 struct nfs_inode *nfsi = NFS_I(inode);
727
Trond Myklebust9ae075f2018-09-02 19:15:15 -0400728 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 state = __nfs4_find_state_byowner(inode, owner);
Trond Myklebust9ae075f2018-09-02 19:15:15 -0400730 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (state)
732 goto out;
733 new = nfs4_alloc_open_state();
Trond Myklebustec073422005-10-20 14:22:47 -0700734 spin_lock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 spin_lock(&inode->i_lock);
736 state = __nfs4_find_state_byowner(inode, owner);
737 if (state == NULL && new != NULL) {
738 state = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 state->owner = owner;
740 atomic_inc(&owner->so_count);
Dave Chinner0444d762011-03-29 18:08:50 +1100741 ihold(inode);
742 state->inode = inode;
J. Bruce Fields29fe8392020-05-11 10:02:48 -0400743 list_add_rcu(&state->inode_states, &nfsi->open_states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700745 /* Note: The reclaim code dictates that we add stateless
746 * and read-only stateids to the end of the list */
747 list_add_tail(&state->open_states, &owner->so_states);
748 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 } else {
750 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700751 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (new)
753 nfs4_free_open_state(new);
754 }
755out:
756 return state;
757}
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759void nfs4_put_open_state(struct nfs4_state *state)
760{
761 struct inode *inode = state->inode;
762 struct nfs4_state_owner *owner = state->owner;
763
Trond Myklebustace9fad2018-09-02 19:19:07 -0400764 if (!refcount_dec_and_lock(&state->count, &owner->so_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return;
Trond Myklebustec073422005-10-20 14:22:47 -0700766 spin_lock(&inode->i_lock);
Trond Myklebust9ae075f2018-09-02 19:15:15 -0400767 list_del_rcu(&state->inode_states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 list_del(&state->open_states);
Trond Myklebustec073422005-10-20 14:22:47 -0700769 spin_unlock(&inode->i_lock);
770 spin_unlock(&owner->so_lock);
Trond Myklebustb7b7dac2020-01-27 09:58:17 -0500771 nfs4_inode_return_delegation_on_close(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 nfs4_free_open_state(state);
774 nfs4_put_state_owner(owner);
775}
776
777/*
Trond Myklebust83c9d412005-10-18 14:20:13 -0700778 * Close the current file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 */
Al Viro643168c2011-06-22 18:20:23 -0400780static void __nfs4_close(struct nfs4_state *state,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400781 fmode_t fmode, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 struct nfs4_state_owner *owner = state->owner;
Trond Myklebust003707c2007-07-05 18:07:55 -0400784 int call_close = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500785 fmode_t newstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 atomic_inc(&owner->so_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 /* Protect against nfs4_find_state() */
Trond Myklebustec073422005-10-20 14:22:47 -0700789 spin_lock(&owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500790 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100791 case FMODE_READ:
792 state->n_rdonly--;
793 break;
794 case FMODE_WRITE:
795 state->n_wronly--;
796 break;
797 case FMODE_READ|FMODE_WRITE:
798 state->n_rdwr--;
799 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400800 newstate = FMODE_READ|FMODE_WRITE;
Trond Myklebuste7616922006-01-03 09:55:13 +0100801 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400802 if (state->n_rdonly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100803 newstate &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -0400804 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
805 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
806 }
807 if (state->n_wronly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100808 newstate &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -0400809 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
810 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
811 }
812 if (newstate == 0)
813 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebuste7616922006-01-03 09:55:13 +0100814 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400815 nfs4_state_set_mode_locked(state, newstate);
Trond Myklebustec073422005-10-20 14:22:47 -0700816 spin_unlock(&owner->so_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500817
Trond Myklebust003707c2007-07-05 18:07:55 -0400818 if (!call_close) {
Trond Myklebustb39e6252007-06-11 23:05:07 -0400819 nfs4_put_open_state(state);
820 nfs4_put_state_owner(owner);
Trond Myklebust1f7977c2012-09-20 20:31:51 -0400821 } else
822 nfs4_do_close(state, gfp_mask, wait);
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400823}
824
Al Viro643168c2011-06-22 18:20:23 -0400825void nfs4_close_state(struct nfs4_state *state, fmode_t fmode)
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400826{
Trond Myklebustda48f262022-01-29 13:57:38 -0500827 __nfs4_close(state, fmode, GFP_KERNEL, 0);
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400828}
829
Al Viro643168c2011-06-22 18:20:23 -0400830void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode)
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400831{
Al Viro643168c2011-06-22 18:20:23 -0400832 __nfs4_close(state, fmode, GFP_KERNEL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
835/*
836 * Search the state->lock_states for an existing lock_owner
NeilBrown3f8f2542016-12-19 11:33:13 +1100837 * that is compatible with either of the given owners.
838 * If the second is non-zero, then the first refers to a Posix-lock
839 * owner (current->files) and the second refers to a flock/OFD
840 * owner (struct file*). In that case, prefer a match for the first
841 * owner.
842 * If both sorts of locks are held on the one file we cannot know
843 * which stateid was intended to be used, so a "correct" choice cannot
844 * be made. Failing that, a "consistent" choice is preferable. The
845 * consistent choice we make is to prefer the first owner, that of a
846 * Posix lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 */
848static struct nfs4_lock_state *
NeilBrown8d424432016-10-13 15:26:47 +1100849__nfs4_find_lock_state(struct nfs4_state *state,
850 fl_owner_t fl_owner, fl_owner_t fl_owner2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
NeilBrown3f8f2542016-12-19 11:33:13 +1100852 struct nfs4_lock_state *pos, *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 list_for_each_entry(pos, &state->lock_states, ls_locks) {
NeilBrown3f8f2542016-12-19 11:33:13 +1100854 if (pos->ls_owner == fl_owner) {
855 ret = pos;
856 break;
857 }
858 if (pos->ls_owner == fl_owner2)
859 ret = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
NeilBrown3f8f2542016-12-19 11:33:13 +1100861 if (ret)
Elena Reshetova194bc1f2017-10-20 12:53:36 +0300862 refcount_inc(&ret->ls_count);
NeilBrown3f8f2542016-12-19 11:33:13 +1100863 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866/*
867 * Return a compatible lock_state. If no initialized lock_state structure
868 * exists, return an uninitialized one.
869 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 */
Jeff Layton8003d3c42014-05-01 06:28:45 -0400871static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 struct nfs4_lock_state *lsp;
Chuck Lever24d292b2010-12-24 01:32:43 +0000874 struct nfs_server *server = state->owner->so_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Trond Myklebust9c00fd92022-01-29 13:42:01 -0500876 lsp = kzalloc(sizeof(*lsp), GFP_KERNEL_ACCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (lsp == NULL)
878 return NULL;
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500879 nfs4_init_seqid_counter(&lsp->ls_seqid);
Elena Reshetova194bc1f2017-10-20 12:53:36 +0300880 refcount_set(&lsp->ls_count, 1);
Trond Myklebustb64aec82009-07-21 16:47:46 -0400881 lsp->ls_state = state;
Jeff Layton8003d3c42014-05-01 06:28:45 -0400882 lsp->ls_owner = fl_owner;
Bo Liu724e2df2022-06-15 02:27:45 -0400883 lsp->ls_seqid.owner_id = ida_alloc(&server->lockowner_id, GFP_KERNEL_ACCOUNT);
Trond Myklebust48c22eb2012-01-17 22:04:25 -0500884 if (lsp->ls_seqid.owner_id < 0)
Trond Myklebustd2d7ce22012-01-17 22:04:25 -0500885 goto out_free;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000886 INIT_LIST_HEAD(&lsp->ls_locks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return lsp;
Trond Myklebustd2d7ce22012-01-17 22:04:25 -0500888out_free:
889 kfree(lsp);
890 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
Trond Myklebust5ae67c42012-03-19 16:17:18 -0400893void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400894{
Bo Liu724e2df2022-06-15 02:27:45 -0400895 ida_free(&server->lockowner_id, lsp->ls_seqid.owner_id);
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500896 nfs4_destroy_seqid_counter(&lsp->ls_seqid);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400897 kfree(lsp);
898}
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900/*
901 * Return a compatible lock_state. If no initialized lock_state structure
902 * exists, return an uninitialized one.
903 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 */
Jeff Layton8003d3c42014-05-01 06:28:45 -0400905static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000907 struct nfs4_lock_state *lsp, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000909 for(;;) {
910 spin_lock(&state->state_lock);
Wei Yongjun68e33bd2017-01-12 15:39:18 +0000911 lsp = __nfs4_find_lock_state(state, owner, NULL);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000912 if (lsp != NULL)
913 break;
914 if (new != NULL) {
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000915 list_add(&new->ls_locks, &state->lock_states);
916 set_bit(LK_STATE_IN_USE, &state->flags);
917 lsp = new;
918 new = NULL;
919 break;
920 }
921 spin_unlock(&state->state_lock);
Jeff Layton8003d3c42014-05-01 06:28:45 -0400922 new = nfs4_alloc_lock_state(state, owner);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000923 if (new == NULL)
924 return NULL;
925 }
926 spin_unlock(&state->state_lock);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400927 if (new != NULL)
Trond Myklebust5ae67c42012-03-19 16:17:18 -0400928 nfs4_free_lock_state(state->owner->so_server, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return lsp;
930}
931
932/*
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000933 * Release reference to lock_state, and free it if we see that
934 * it is no longer in use
935 */
Trond Myklebustfaf5f492005-10-18 14:20:15 -0700936void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000937{
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -0400938 struct nfs_server *server;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000939 struct nfs4_state *state;
940
941 if (lsp == NULL)
942 return;
943 state = lsp->ls_state;
Elena Reshetova194bc1f2017-10-20 12:53:36 +0300944 if (!refcount_dec_and_lock(&lsp->ls_count, &state->state_lock))
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000945 return;
946 list_del(&lsp->ls_locks);
947 if (list_empty(&state->lock_states))
948 clear_bit(LK_STATE_IN_USE, &state->flags);
949 spin_unlock(&state->state_lock);
Jeff Layton0c0e0d32014-09-08 08:26:01 -0400950 server = state->owner->so_server;
951 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
952 struct nfs_client *clp = server->nfs_client;
953
954 clp->cl_mvops->free_lock_state(server, lsp);
955 } else
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -0400956 nfs4_free_lock_state(server, lsp);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000957}
958
959static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
960{
961 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
962
963 dst->fl_u.nfs4_fl.owner = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +0300964 refcount_inc(&lsp->ls_count);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000965}
966
967static void nfs4_fl_release_lock(struct file_lock *fl)
968{
969 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
970}
971
Alexey Dobriyan6aed6282009-09-21 17:01:11 -0700972static const struct file_lock_operations nfs4_fl_lock_ops = {
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000973 .fl_copy_lock = nfs4_fl_copy_lock,
974 .fl_release_private = nfs4_fl_release_lock,
975};
976
977int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
978{
979 struct nfs4_lock_state *lsp;
980
981 if (fl->fl_ops != NULL)
982 return 0;
Jeff Layton8003d3c42014-05-01 06:28:45 -0400983 lsp = nfs4_get_lock_state(state, fl->fl_owner);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000984 if (lsp == NULL)
985 return -ENOMEM;
986 fl->fl_u.nfs4_fl.owner = lsp;
987 fl->fl_ops = &nfs4_fl_lock_ops;
988 return 0;
989}
990
Trond Myklebust5521abf2013-03-16 20:54:34 -0400991static int nfs4_copy_lock_stateid(nfs4_stateid *dst,
992 struct nfs4_state *state,
NeilBrown17393472016-10-13 15:26:47 +1100993 const struct nfs_lock_context *l_ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000995 struct nfs4_lock_state *lsp;
NeilBrown8d424432016-10-13 15:26:47 +1100996 fl_owner_t fl_owner, fl_flock_owner;
Trond Myklebust5521abf2013-03-16 20:54:34 -0400997 int ret = -ENOENT;
Trond Myklebust4fc87962012-03-08 17:42:01 -0500998
NeilBrown17393472016-10-13 15:26:47 +1100999 if (l_ctx == NULL)
Trond Myklebust2a369152012-08-13 18:54:45 -04001000 goto out;
1001
Trond Myklebust4fc87962012-03-08 17:42:01 -05001002 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
1003 goto out;
1004
NeilBrownd51fdb82016-10-13 15:26:47 +11001005 fl_owner = l_ctx->lockowner;
NeilBrown8d424432016-10-13 15:26:47 +11001006 fl_flock_owner = l_ctx->open_context->flock_owner;
1007
Trond Myklebust4fc87962012-03-08 17:42:01 -05001008 spin_lock(&state->state_lock);
NeilBrown8d424432016-10-13 15:26:47 +11001009 lsp = __nfs4_find_lock_state(state, fl_owner, fl_flock_owner);
NeilBrownef1820f2013-09-04 17:04:49 +10001010 if (lsp && test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
1011 ret = -EIO;
1012 else if (lsp != NULL && test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) {
Trond Myklebust4fc87962012-03-08 17:42:01 -05001013 nfs4_stateid_copy(dst, &lsp->ls_stateid);
Trond Myklebust5521abf2013-03-16 20:54:34 -04001014 ret = 0;
Trond Myklebust4fc87962012-03-08 17:42:01 -05001015 }
1016 spin_unlock(&state->state_lock);
1017 nfs4_put_lock_state(lsp);
1018out:
1019 return ret;
1020}
1021
Trond Myklebustc82bac62017-11-06 15:28:06 -05001022bool nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
Trond Myklebust4fc87962012-03-08 17:42:01 -05001023{
Trond Myklebustc82bac62017-11-06 15:28:06 -05001024 bool ret;
Trond Myklebust92b40e92013-04-20 01:25:45 -04001025 const nfs4_stateid *src;
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001026 int seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001028 do {
Trond Myklebustc82bac62017-11-06 15:28:06 -05001029 ret = false;
Trond Myklebust92b40e92013-04-20 01:25:45 -04001030 src = &zero_stateid;
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001031 seq = read_seqbegin(&state->seqlock);
Trond Myklebustc82bac62017-11-06 15:28:06 -05001032 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
Trond Myklebust92b40e92013-04-20 01:25:45 -04001033 src = &state->open_stateid;
Trond Myklebustc82bac62017-11-06 15:28:06 -05001034 ret = true;
1035 }
Trond Myklebust92b40e92013-04-20 01:25:45 -04001036 nfs4_stateid_copy(dst, src);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001037 } while (read_seqretry(&state->seqlock, seq));
Trond Myklebustc82bac62017-11-06 15:28:06 -05001038 return ret;
Trond Myklebust4fc87962012-03-08 17:42:01 -05001039}
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00001040
Trond Myklebust4fc87962012-03-08 17:42:01 -05001041/*
1042 * Byte-range lock aware utility to initialize the stateid of read/write
1043 * requests.
1044 */
Trond Myklebustabf4e132016-05-16 17:42:44 -04001045int nfs4_select_rw_stateid(struct nfs4_state *state,
NeilBrown17393472016-10-13 15:26:47 +11001046 fmode_t fmode, const struct nfs_lock_context *l_ctx,
NeilBrowna52458b2018-12-03 11:30:31 +11001047 nfs4_stateid *dst, const struct cred **cred)
Trond Myklebust4fc87962012-03-08 17:42:01 -05001048{
Trond Myklebustabf4e132016-05-16 17:42:44 -04001049 int ret;
1050
Trond Myklebust7ebeb7f2016-09-22 13:39:20 -04001051 if (!nfs4_valid_open_stateid(state))
1052 return -EIO;
Trond Myklebustabf4e132016-05-16 17:42:44 -04001053 if (cred != NULL)
1054 *cred = NULL;
NeilBrown17393472016-10-13 15:26:47 +11001055 ret = nfs4_copy_lock_stateid(dst, state, l_ctx);
NeilBrownef1820f2013-09-04 17:04:49 +10001056 if (ret == -EIO)
1057 /* A lost lock - don't even consider delegations */
1058 goto out;
Andy Adamson146d70c2014-02-18 10:36:05 -05001059 /* returns true if delegation stateid found and copied */
Trond Myklebustabf4e132016-05-16 17:42:44 -04001060 if (nfs4_copy_delegation_stateid(state->inode, fmode, dst, cred)) {
Andy Adamson146d70c2014-02-18 10:36:05 -05001061 ret = 0;
Trond Myklebust5521abf2013-03-16 20:54:34 -04001062 goto out;
Andy Adamson146d70c2014-02-18 10:36:05 -05001063 }
Trond Myklebust5521abf2013-03-16 20:54:34 -04001064 if (ret != -ENOENT)
NeilBrownef1820f2013-09-04 17:04:49 +10001065 /* nfs4_copy_delegation_stateid() didn't over-write
1066 * dst, so it still has the lock stateid which we now
1067 * choose to use.
1068 */
Trond Myklebust5521abf2013-03-16 20:54:34 -04001069 goto out;
Trond Myklebustd9aba2b2019-07-16 15:38:28 -04001070 ret = nfs4_copy_open_stateid(dst, state) ? 0 : -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04001071out:
Trond Myklebust3b664862013-03-17 15:31:15 -04001072 if (nfs_server_capable(state->inode, NFS_CAP_STATEID_NFSV41))
1073 dst->seqid = 0;
Trond Myklebust5521abf2013-03-16 20:54:34 -04001074 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001077struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001079 struct nfs_seqid *new;
1080
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001081 new = kmalloc(sizeof(*new), gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001082 if (new == NULL)
1083 return ERR_PTR(-ENOMEM);
1084 new->sequence = counter;
1085 INIT_LIST_HEAD(&new->list);
1086 new->task = NULL;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001087 return new;
1088}
1089
Trond Myklebust72211db2009-12-15 14:47:36 -05001090void nfs_release_seqid(struct nfs_seqid *seqid)
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001091{
Trond Myklebust4601df22012-01-20 18:47:05 -05001092 struct nfs_seqid_counter *sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001093
Trond Myklebusta6796412015-01-23 19:04:44 -05001094 if (seqid == NULL || list_empty(&seqid->list))
Trond Myklebust4601df22012-01-20 18:47:05 -05001095 return;
1096 sequence = seqid->sequence;
1097 spin_lock(&sequence->lock);
1098 list_del_init(&seqid->list);
1099 if (!list_empty(&sequence->list)) {
1100 struct nfs_seqid *next;
1101
1102 next = list_first_entry(&sequence->list,
1103 struct nfs_seqid, list);
1104 rpc_wake_up_queued_task(&sequence->wait, next->task);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05001105 }
Trond Myklebust4601df22012-01-20 18:47:05 -05001106 spin_unlock(&sequence->lock);
Trond Myklebust72211db2009-12-15 14:47:36 -05001107}
1108
1109void nfs_free_seqid(struct nfs_seqid *seqid)
1110{
1111 nfs_release_seqid(seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001112 kfree(seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001116 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
1117 * failed with a seqid incrementing error -
Toralf Förster16a6ddc2013-12-12 19:09:00 +01001118 * see comments nfs4.h:seqid_mutating_error()
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001119 */
Trond Myklebust88d90932007-07-02 14:03:03 -04001120static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001122 switch (status) {
1123 case 0:
1124 break;
1125 case -NFS4ERR_BAD_SEQID:
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001126 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
1127 return;
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001128 pr_warn_ratelimited("NFS: v4 server returned a bad"
Dan Muntz497799e2008-02-13 13:09:35 -08001129 " sequence-id error on an"
1130 " unconfirmed sequence %p!\n",
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001131 seqid->sequence);
Gustavo A. R. Silvaffb81712020-11-20 12:26:46 -06001132 return;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001133 case -NFS4ERR_STALE_CLIENTID:
1134 case -NFS4ERR_STALE_STATEID:
1135 case -NFS4ERR_BAD_STATEID:
1136 case -NFS4ERR_BADXDR:
1137 case -NFS4ERR_RESOURCE:
1138 case -NFS4ERR_NOFILEHANDLE:
Chuck Lever406dab82017-01-26 15:14:52 -05001139 case -NFS4ERR_MOVED:
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001140 /* Non-seqid mutating errors */
1141 return;
zhengbin8b98a532019-12-19 18:34:47 +08001142 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001143 /*
1144 * Note: no locking needed as we are guaranteed to be first
1145 * on the sequence list
1146 */
1147 seqid->sequence->counter++;
1148}
1149
1150void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
1151{
Trond Myklebusta6796412015-01-23 19:04:44 -05001152 struct nfs4_state_owner *sp;
Benny Halevy34dc1ad2009-04-01 09:22:52 -04001153
Trond Myklebusta6796412015-01-23 19:04:44 -05001154 if (seqid == NULL)
1155 return;
1156
1157 sp = container_of(seqid->sequence, struct nfs4_state_owner, so_seqid);
Benny Halevy34dc1ad2009-04-01 09:22:52 -04001158 if (status == -NFS4ERR_BAD_SEQID)
NeilBrown86cfb042016-12-19 11:48:23 +11001159 nfs4_reset_state_owner(sp);
Trond Myklebusta6796412015-01-23 19:04:44 -05001160 if (!nfs4_has_session(sp->so_server->nfs_client))
Benny Halevy34dc1ad2009-04-01 09:22:52 -04001161 nfs_increment_seqid(status, seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001162}
1163
1164/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001165 * Increment the seqid if the LOCK/LOCKU succeeded, or
1166 * failed with a seqid incrementing error -
Toralf Förster16a6ddc2013-12-12 19:09:00 +01001167 * see comments nfs4.h:seqid_mutating_error()
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001168 */
1169void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
1170{
Trond Myklebusta6796412015-01-23 19:04:44 -05001171 if (seqid != NULL)
1172 nfs_increment_seqid(status, seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001173}
1174
1175int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
1176{
Trond Myklebusta6796412015-01-23 19:04:44 -05001177 struct nfs_seqid_counter *sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001178 int status = 0;
1179
Trond Myklebusta6796412015-01-23 19:04:44 -05001180 if (seqid == NULL)
1181 goto out;
1182 sequence = seqid->sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001183 spin_lock(&sequence->lock);
Trond Myklebust4601df22012-01-20 18:47:05 -05001184 seqid->task = task;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05001185 if (list_empty(&seqid->list))
1186 list_add_tail(&seqid->list, &sequence->list);
1187 if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
1188 goto unlock;
Trond Myklebust5d008372008-02-22 16:34:17 -05001189 rpc_sleep_on(&sequence->wait, task, NULL);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05001190 status = -EAGAIN;
1191unlock:
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001192 spin_unlock(&sequence->lock);
Trond Myklebusta6796412015-01-23 19:04:44 -05001193out:
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001194 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
Trond Myklebuste005e802008-12-23 15:21:48 -05001197static int nfs4_run_state_manager(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Trond Myklebuste005e802008-12-23 15:21:48 -05001199static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
Trond Myklebust433fbe42006-01-03 09:55:22 +01001200{
Trond Myklebust43d20e82021-07-13 12:28:22 -04001201 clear_and_wake_up_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
Trond Myklebust433fbe42006-01-03 09:55:22 +01001202 rpc_wake_up(&clp->cl_rpcwaitq);
1203}
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205/*
Trond Myklebuste005e802008-12-23 15:21:48 -05001206 * Schedule the nfs_client asynchronous state management routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 */
Trond Myklebustb0d3ded2008-12-23 15:21:50 -05001208void nfs4_schedule_state_manager(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Trond Myklebust5043e902006-01-03 09:55:23 +01001210 struct task_struct *task;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001211 char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
Trond Myklebust956fd462023-09-24 13:14:15 -04001212 struct rpc_clnt *clnt = clp->cl_rpcclient;
1213 bool swapon = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Trond Myklebust956fd462023-09-24 13:14:15 -04001215 if (clnt->cl_shutdown)
Benjamin Coddington6ad477a2023-06-15 14:07:32 -04001216 return;
1217
Trond Myklebustaeabb3c2018-11-19 20:11:45 -05001218 set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
Trond Myklebust956fd462023-09-24 13:14:15 -04001219
1220 if (atomic_read(&clnt->cl_swapper)) {
1221 swapon = !test_and_set_bit(NFS4CLNT_MANAGER_AVAILABLE,
1222 &clp->cl_state);
1223 if (!swapon) {
1224 wake_up_var(&clp->cl_state);
1225 return;
1226 }
NeilBrown4dc73c62022-03-07 10:41:44 +11001227 }
Trond Myklebust956fd462023-09-24 13:14:15 -04001228
1229 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1230 return;
1231
Trond Myklebust5043e902006-01-03 09:55:23 +01001232 __module_get(THIS_MODULE);
Elena Reshetova212bf412017-10-20 12:53:38 +03001233 refcount_inc(&clp->cl_count);
Trond Myklebust2446ab62012-03-01 17:00:56 -05001234
1235 /* The rcu_read_lock() is not strictly necessary, as the state
1236 * manager is the only thread that ever changes the rpc_xprt
1237 * after it's initialized. At this point, we're single threaded. */
1238 rcu_read_lock();
1239 snprintf(buf, sizeof(buf), "%s-manager",
1240 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
1241 rcu_read_unlock();
Kees Cookf1701682013-07-03 15:04:58 -07001242 task = kthread_run(nfs4_run_state_manager, clp, "%s", buf);
Trond Myklebust2446ab62012-03-01 17:00:56 -05001243 if (IS_ERR(task)) {
1244 printk(KERN_ERR "%s: kthread_run: %ld\n",
1245 __func__, PTR_ERR(task));
Trond Myklebustb4e4f662022-12-06 12:42:59 -05001246 if (!nfs_client_init_is_complete(clp))
1247 nfs_mark_client_ready(clp, PTR_ERR(task));
Trond Myklebust956fd462023-09-24 13:14:15 -04001248 if (swapon)
1249 clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);
Trond Myklebust2446ab62012-03-01 17:00:56 -05001250 nfs4_clear_state_manager_bit(clp);
1251 nfs_put_client(clp);
1252 module_put(THIS_MODULE);
1253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
1256/*
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001257 * Schedule a lease recovery attempt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001259void nfs4_schedule_lease_recovery(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
1261 if (!clp)
1262 return;
Trond Myklebuste598d842008-12-23 15:21:42 -05001263 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1264 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001265 dprintk("%s: scheduling lease recovery for server %s\n", __func__,
1266 clp->cl_hostname);
Trond Myklebuste005e802008-12-23 15:21:48 -05001267 nfs4_schedule_state_manager(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
Andy Adamson9cb81962012-03-07 10:49:41 -05001269EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Chuck Leverc9fdeb22013-10-17 14:13:02 -04001271/**
1272 * nfs4_schedule_migration_recovery - trigger migration recovery
1273 *
1274 * @server: FSID that is migrating
1275 *
1276 * Returns zero if recovery has started, otherwise a negative NFS4ERR
1277 * value is returned.
1278 */
1279int nfs4_schedule_migration_recovery(const struct nfs_server *server)
1280{
1281 struct nfs_client *clp = server->nfs_client;
1282
1283 if (server->fh_expire_type != NFS4_FH_PERSISTENT) {
1284 pr_err("NFS: volatile file handles not supported (server %s)\n",
1285 clp->cl_hostname);
1286 return -NFS4ERR_IO;
1287 }
1288
1289 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
1290 return -NFS4ERR_IO;
1291
1292 dprintk("%s: scheduling migration recovery for (%llx:%llx) on %s\n",
1293 __func__,
1294 (unsigned long long)server->fsid.major,
1295 (unsigned long long)server->fsid.minor,
1296 clp->cl_hostname);
1297
1298 set_bit(NFS_MIG_IN_TRANSITION,
1299 &((struct nfs_server *)server)->mig_status);
1300 set_bit(NFS4CLNT_MOVED, &clp->cl_state);
1301
1302 nfs4_schedule_state_manager(clp);
1303 return 0;
1304}
1305EXPORT_SYMBOL_GPL(nfs4_schedule_migration_recovery);
1306
Chuck Leverb7f7a662013-10-17 14:13:35 -04001307/**
1308 * nfs4_schedule_lease_moved_recovery - start lease-moved recovery
1309 *
1310 * @clp: server to check for moved leases
1311 *
1312 */
1313void nfs4_schedule_lease_moved_recovery(struct nfs_client *clp)
1314{
1315 dprintk("%s: scheduling lease-moved recovery for client ID %llx on %s\n",
1316 __func__, clp->cl_clientid, clp->cl_hostname);
1317
1318 set_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state);
1319 nfs4_schedule_state_manager(clp);
1320}
1321EXPORT_SYMBOL_GPL(nfs4_schedule_lease_moved_recovery);
1322
Trond Myklebust33021272012-11-26 13:13:29 -05001323int nfs4_wait_clnt_recover(struct nfs_client *clp)
1324{
1325 int res;
1326
1327 might_sleep();
1328
Elena Reshetova212bf412017-10-20 12:53:38 +03001329 refcount_inc(&clp->cl_count);
NeilBrown74316202014-07-07 15:16:04 +10001330 res = wait_on_bit_action(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Peter Zijlstraf5d39b02022-08-22 13:18:22 +02001331 nfs_wait_bit_killable,
1332 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
Trond Myklebust33021272012-11-26 13:13:29 -05001333 if (res)
Chuck Lever0625c2d2013-10-17 14:14:10 -04001334 goto out;
Trond Myklebust33021272012-11-26 13:13:29 -05001335 if (clp->cl_cons_state < 0)
Chuck Lever0625c2d2013-10-17 14:14:10 -04001336 res = clp->cl_cons_state;
1337out:
1338 nfs_put_client(clp);
1339 return res;
Trond Myklebust33021272012-11-26 13:13:29 -05001340}
1341
1342int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1343{
1344 unsigned int loop;
1345 int ret;
1346
1347 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1348 ret = nfs4_wait_clnt_recover(clp);
1349 if (ret != 0)
1350 break;
1351 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1352 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1353 break;
1354 nfs4_schedule_state_manager(clp);
1355 ret = -EIO;
1356 }
1357 return ret;
1358}
1359
Trond Myklebustad1e3962012-03-10 11:23:15 -05001360/*
1361 * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
1362 * @clp: client to process
1363 *
1364 * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
1365 * resend of the SETCLIENTID and hence re-establish the
1366 * callback channel. Then return all existing delegations.
1367 */
1368static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
1369{
1370 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1371 nfs_expire_all_delegations(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001372 dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
1373 clp->cl_hostname);
Trond Myklebustad1e3962012-03-10 11:23:15 -05001374}
1375
Trond Myklebust042b60b2011-08-24 15:07:37 -04001376void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
1377{
Trond Myklebustad1e3962012-03-10 11:23:15 -05001378 nfs40_handle_cb_pathdown(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04001379 nfs4_schedule_state_manager(clp);
1380}
1381
Trond Myklebustf9feab12011-03-09 16:12:46 -05001382static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001383{
1384
Trond Myklebust7ebeb7f2016-09-22 13:39:20 -04001385 if (!nfs4_valid_open_stateid(state))
1386 return 0;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001387 set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1388 /* Don't recover state that expired before the reboot */
1389 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1390 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1391 return 0;
1392 }
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001393 set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001394 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1395 return 1;
1396}
1397
Trond Myklebust4f14c192014-02-12 19:15:06 -05001398int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001399{
Trond Myklebust7ebeb7f2016-09-22 13:39:20 -04001400 if (!nfs4_valid_open_stateid(state))
1401 return 0;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001402 set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1403 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001404 set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001405 set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1406 return 1;
1407}
1408
Trond Myklebust5d422302013-03-14 16:57:48 -04001409int nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001410{
1411 struct nfs_client *clp = server->nfs_client;
1412
Trond Myklebust7ebeb7f2016-09-22 13:39:20 -04001413 if (!nfs4_state_mark_reclaim_nograce(clp, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04001414 return -EBADF;
Trond Myklebust994b15b2018-09-05 14:07:14 -04001415 nfs_inode_find_delegation_state_and_recover(state->inode,
1416 &state->stateid);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001417 dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
1418 clp->cl_hostname);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001419 nfs4_schedule_state_manager(clp);
Trond Myklebust5d422302013-03-14 16:57:48 -04001420 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001421}
Andy Adamson9cb81962012-03-07 10:49:41 -05001422EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001423
Trond Myklebust6c2d8f82016-09-22 13:39:07 -04001424static struct nfs4_lock_state *
1425nfs_state_find_lock_state_by_stateid(struct nfs4_state *state,
1426 const nfs4_stateid *stateid)
1427{
1428 struct nfs4_lock_state *pos;
1429
1430 list_for_each_entry(pos, &state->lock_states, ls_locks) {
1431 if (!test_bit(NFS_LOCK_INITIALIZED, &pos->ls_flags))
1432 continue;
Trond Myklebust42c304c2019-10-26 10:16:15 -04001433 if (nfs4_stateid_match_or_older(&pos->ls_stateid, stateid))
Trond Myklebust6c2d8f82016-09-22 13:39:07 -04001434 return pos;
1435 }
1436 return NULL;
1437}
1438
1439static bool nfs_state_lock_state_matches_stateid(struct nfs4_state *state,
1440 const nfs4_stateid *stateid)
1441{
1442 bool found = false;
1443
1444 if (test_bit(LK_STATE_IN_USE, &state->flags)) {
1445 spin_lock(&state->state_lock);
1446 if (nfs_state_find_lock_state_by_stateid(state, stateid))
1447 found = true;
1448 spin_unlock(&state->state_lock);
1449 }
1450 return found;
1451}
1452
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001453void nfs_inode_find_state_and_recover(struct inode *inode,
1454 const nfs4_stateid *stateid)
1455{
1456 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1457 struct nfs_inode *nfsi = NFS_I(inode);
1458 struct nfs_open_context *ctx;
1459 struct nfs4_state *state;
1460 bool found = false;
1461
Trond Myklebust0de43972018-09-02 15:57:01 -04001462 rcu_read_lock();
1463 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001464 state = ctx->state;
1465 if (state == NULL)
1466 continue;
Trond Myklebust42c304c2019-10-26 10:16:15 -04001467 if (nfs4_stateid_match_or_older(&state->stateid, stateid) &&
Trond Myklebust7ebeb7f2016-09-22 13:39:20 -04001468 nfs4_state_mark_reclaim_nograce(clp, state)) {
Trond Myklebust6c2d8f82016-09-22 13:39:07 -04001469 found = true;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001470 continue;
Trond Myklebust6c2d8f82016-09-22 13:39:07 -04001471 }
Trond Myklebust42c304c2019-10-26 10:16:15 -04001472 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1473 nfs4_stateid_match_or_older(&state->open_stateid, stateid) &&
Trond Myklebust46280d92017-11-06 15:28:11 -05001474 nfs4_state_mark_reclaim_nograce(clp, state)) {
1475 found = true;
1476 continue;
1477 }
Trond Myklebust7ebeb7f2016-09-22 13:39:20 -04001478 if (nfs_state_lock_state_matches_stateid(state, stateid) &&
1479 nfs4_state_mark_reclaim_nograce(clp, state))
Trond Myklebust6c2d8f82016-09-22 13:39:07 -04001480 found = true;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001481 }
Trond Myklebust0de43972018-09-02 15:57:01 -04001482 rcu_read_unlock();
Trond Myklebust6c2d8f82016-09-22 13:39:07 -04001483
1484 nfs_inode_find_delegation_state_and_recover(inode, stateid);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001485 if (found)
1486 nfs4_schedule_state_manager(clp);
1487}
1488
Trond Myklebust86dbd08b2019-07-22 09:44:04 +01001489static void nfs4_state_mark_open_context_bad(struct nfs4_state *state, int err)
Trond Myklebustc58c8442013-03-18 19:45:14 -04001490{
1491 struct inode *inode = state->inode;
1492 struct nfs_inode *nfsi = NFS_I(inode);
1493 struct nfs_open_context *ctx;
1494
Trond Myklebust0de43972018-09-02 15:57:01 -04001495 rcu_read_lock();
1496 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebustc58c8442013-03-18 19:45:14 -04001497 if (ctx->state != state)
1498 continue;
1499 set_bit(NFS_CONTEXT_BAD, &ctx->flags);
Trond Myklebust86dbd08b2019-07-22 09:44:04 +01001500 pr_warn("NFSv4: state recovery failed for open file %pd2, "
1501 "error = %d\n", ctx->dentry, err);
Trond Myklebustc58c8442013-03-18 19:45:14 -04001502 }
Trond Myklebust0de43972018-09-02 15:57:01 -04001503 rcu_read_unlock();
Trond Myklebustc58c8442013-03-18 19:45:14 -04001504}
1505
Trond Myklebust5d422302013-03-14 16:57:48 -04001506static void nfs4_state_mark_recovery_failed(struct nfs4_state *state, int error)
1507{
1508 set_bit(NFS_STATE_RECOVERY_FAILED, &state->flags);
Trond Myklebust86dbd08b2019-07-22 09:44:04 +01001509 nfs4_state_mark_open_context_bad(state, error);
Trond Myklebust5d422302013-03-14 16:57:48 -04001510}
1511
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001512
Trond Myklebust02860012008-12-23 15:21:40 -05001513static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
1515 struct inode *inode = state->inode;
Trond Myklebust19e03c52008-12-23 15:21:44 -05001516 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 struct file_lock *fl;
NeilBrowndce26302017-12-13 09:57:09 +11001518 struct nfs4_lock_state *lsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 int status = 0;
Jeff Layton17b985d2022-11-16 09:55:36 -05001520 struct file_lock_context *flctx = locks_inode_context(inode);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001521 struct list_head *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001523 if (flctx == NULL)
Trond Myklebust3f09df72009-06-17 13:23:00 -07001524 return 0;
1525
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001526 list = &flctx->flc_posix;
1527
Trond Myklebust3f09df72009-06-17 13:23:00 -07001528 /* Guard against delegation returns and new lock/unlock calls */
Trond Myklebust19e03c52008-12-23 15:21:44 -05001529 down_write(&nfsi->rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -05001530 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001531restart:
1532 list_for_each_entry(fl, list, fl_list) {
Jeff Layton5263e312015-01-16 15:05:55 -05001533 if (nfs_file_open_context(fl->fl_file)->state != state)
1534 continue;
Jeff Layton6109c852015-01-16 15:05:57 -05001535 spin_unlock(&flctx->flc_lock);
Jeff Layton5263e312015-01-16 15:05:55 -05001536 status = ops->recover_lock(state, fl);
1537 switch (status) {
1538 case 0:
1539 break;
Trond Myklebust67e7b522019-08-07 07:31:27 -04001540 case -ETIMEDOUT:
Jeff Layton5263e312015-01-16 15:05:55 -05001541 case -ESTALE:
1542 case -NFS4ERR_ADMIN_REVOKED:
1543 case -NFS4ERR_STALE_STATEID:
1544 case -NFS4ERR_BAD_STATEID:
1545 case -NFS4ERR_EXPIRED:
1546 case -NFS4ERR_NO_GRACE:
1547 case -NFS4ERR_STALE_CLIENTID:
1548 case -NFS4ERR_BADSESSION:
1549 case -NFS4ERR_BADSLOT:
1550 case -NFS4ERR_BAD_HIGH_SLOT:
1551 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1552 goto out;
1553 default:
1554 pr_err("NFS: %s: unhandled error %d\n",
1555 __func__, status);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001556 fallthrough;
Jeff Layton5263e312015-01-16 15:05:55 -05001557 case -ENOMEM:
1558 case -NFS4ERR_DENIED:
1559 case -NFS4ERR_RECLAIM_BAD:
1560 case -NFS4ERR_RECLAIM_CONFLICT:
NeilBrowndce26302017-12-13 09:57:09 +11001561 lsp = fl->fl_u.nfs4_fl.owner;
1562 if (lsp)
1563 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
Jeff Layton5263e312015-01-16 15:05:55 -05001564 status = 0;
1565 }
Jeff Layton6109c852015-01-16 15:05:57 -05001566 spin_lock(&flctx->flc_lock);
Jeff Layton5263e312015-01-16 15:05:55 -05001567 }
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001568 if (list == &flctx->flc_posix) {
1569 list = &flctx->flc_flock;
1570 goto restart;
1571 }
Jeff Layton6109c852015-01-16 15:05:57 -05001572 spin_unlock(&flctx->flc_lock);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001573out:
Trond Myklebust19e03c52008-12-23 15:21:44 -05001574 up_write(&nfsi->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return status;
1576}
1577
Anna Schumaker80f42362018-09-20 16:12:13 -04001578#ifdef CONFIG_NFS_V4_2
1579static void nfs42_complete_copies(struct nfs4_state_owner *sp, struct nfs4_state *state)
1580{
1581 struct nfs4_copy_state *copy;
1582
Olga Kornievskaia0e65a322019-06-14 14:38:40 -04001583 if (!test_bit(NFS_CLNT_DST_SSC_COPY_STATE, &state->flags) &&
1584 !test_bit(NFS_CLNT_SRC_SSC_COPY_STATE, &state->flags))
Anna Schumaker80f42362018-09-20 16:12:13 -04001585 return;
1586
1587 spin_lock(&sp->so_server->nfs_client->cl_lock);
1588 list_for_each_entry(copy, &sp->so_server->ss_copies, copies) {
Olga Kornievskaia0e65a322019-06-14 14:38:40 -04001589 if ((test_bit(NFS_CLNT_DST_SSC_COPY_STATE, &state->flags) &&
1590 !nfs4_stateid_match_other(&state->stateid,
1591 &copy->parent_dst_state->stateid)))
1592 continue;
Anna Schumaker80f42362018-09-20 16:12:13 -04001593 copy->flags = 1;
Olga Kornievskaia0e65a322019-06-14 14:38:40 -04001594 if (test_and_clear_bit(NFS_CLNT_DST_SSC_COPY_STATE,
1595 &state->flags)) {
1596 clear_bit(NFS_CLNT_SRC_SSC_COPY_STATE, &state->flags);
1597 complete(&copy->completion);
1598 }
1599 }
1600 list_for_each_entry(copy, &sp->so_server->ss_copies, src_copies) {
1601 if ((test_bit(NFS_CLNT_SRC_SSC_COPY_STATE, &state->flags) &&
1602 !nfs4_stateid_match_other(&state->stateid,
1603 &copy->parent_src_state->stateid)))
1604 continue;
1605 copy->flags = 1;
1606 if (test_and_clear_bit(NFS_CLNT_DST_SSC_COPY_STATE,
1607 &state->flags))
1608 complete(&copy->completion);
Anna Schumaker80f42362018-09-20 16:12:13 -04001609 }
1610 spin_unlock(&sp->so_server->nfs_client->cl_lock);
1611}
1612#else /* !CONFIG_NFS_V4_2 */
1613static inline void nfs42_complete_copies(struct nfs4_state_owner *sp,
1614 struct nfs4_state *state)
1615{
1616}
1617#endif /* CONFIG_NFS_V4_2 */
1618
Anna Schumakercb7a8382018-09-11 16:23:46 -04001619static int __nfs4_reclaim_open_state(struct nfs4_state_owner *sp, struct nfs4_state *state,
NeilBrown3e2910c2022-03-21 10:59:51 +11001620 const struct nfs4_state_recovery_ops *ops,
1621 int *lost_locks)
Anna Schumakercb7a8382018-09-11 16:23:46 -04001622{
1623 struct nfs4_lock_state *lock;
1624 int status;
1625
1626 status = ops->recover_open(sp, state);
1627 if (status < 0)
1628 return status;
1629
1630 status = nfs4_reclaim_locks(state, ops);
1631 if (status < 0)
1632 return status;
1633
1634 if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) {
1635 spin_lock(&state->state_lock);
1636 list_for_each_entry(lock, &state->lock_states, ls_locks) {
Chuck Lever21f86d22019-11-05 11:04:13 -05001637 trace_nfs4_state_lock_reclaim(state, lock);
NeilBrownef8d98f2022-10-10 10:26:51 +11001638 if (!test_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags) &&
1639 !test_bit(NFS_LOCK_UNLOCKING, &lock->ls_flags))
NeilBrown3e2910c2022-03-21 10:59:51 +11001640 *lost_locks += 1;
Anna Schumakercb7a8382018-09-11 16:23:46 -04001641 }
1642 spin_unlock(&state->state_lock);
1643 }
1644
Anna Schumaker80f42362018-09-20 16:12:13 -04001645 nfs42_complete_copies(sp, state);
Anna Schumakercb7a8382018-09-11 16:23:46 -04001646 clear_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1647 return status;
1648}
1649
NeilBrown3e2910c2022-03-21 10:59:51 +11001650static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp,
1651 const struct nfs4_state_recovery_ops *ops,
1652 int *lost_locks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
1654 struct nfs4_state *state;
Trond Myklebustc34fae02019-07-22 09:54:29 +01001655 unsigned int loop = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 int status = 0;
Olga Kornievskaia0b9018b2019-10-08 16:34:36 -04001657#ifdef CONFIG_NFS_V4_2
1658 bool found_ssc_copy_state = false;
1659#endif /* CONFIG_NFS_V4_2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 /* Note: we rely on the sp->so_states list being ordered
1662 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1663 * states first.
1664 * This is needed to ensure that the server won't give us any
1665 * read delegations that we have to return if, say, we are
1666 * recovering after a network partition or a reboot from a
1667 * server that doesn't support a grace period.
1668 */
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001669 spin_lock(&sp->so_lock);
Trond Myklebustabbec2da2014-06-05 10:42:37 -04001670 raw_write_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebustc137afa2013-02-07 14:41:11 -05001671restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 list_for_each_entry(state, &sp->so_states, open_states) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001673 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1674 continue;
Trond Myklebust5d422302013-03-14 16:57:48 -04001675 if (!nfs4_valid_open_stateid(state))
1676 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 if (state->state == 0)
1678 continue;
Olga Kornievskaia0b9018b2019-10-08 16:34:36 -04001679#ifdef CONFIG_NFS_V4_2
1680 if (test_bit(NFS_SRV_SSC_COPY_STATE, &state->flags)) {
1681 nfs4_state_mark_recovery_failed(state, -EIO);
1682 found_ssc_copy_state = true;
1683 continue;
1684 }
1685#endif /* CONFIG_NFS_V4_2 */
Trond Myklebustace9fad2018-09-02 19:19:07 -04001686 refcount_inc(&state->count);
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001687 spin_unlock(&sp->so_lock);
NeilBrown3e2910c2022-03-21 10:59:51 +11001688 status = __nfs4_reclaim_open_state(sp, state, ops, lost_locks);
Anna Schumakercb7a8382018-09-11 16:23:46 -04001689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 switch (status) {
Anna Schumaker35a61602018-09-11 16:23:47 -04001691 default:
Trond Myklebustc34fae02019-07-22 09:54:29 +01001692 if (status >= 0) {
1693 loop = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 break;
Trond Myklebustc34fae02019-07-22 09:54:29 +01001695 }
Anna Schumaker35a61602018-09-11 16:23:47 -04001696 printk(KERN_ERR "NFS: %s: unhandled error %d\n", __func__, status);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001697 fallthrough;
Anna Schumaker35a61602018-09-11 16:23:47 -04001698 case -ENOENT:
1699 case -ENOMEM:
1700 case -EACCES:
1701 case -EROFS:
1702 case -EIO:
1703 case -ESTALE:
1704 /* Open state on this file cannot be recovered */
1705 nfs4_state_mark_recovery_failed(state, status);
1706 break;
1707 case -EAGAIN:
1708 ssleep(1);
Trond Myklebustc34fae02019-07-22 09:54:29 +01001709 if (loop++ < 10) {
1710 set_bit(ops->state_flag_bit, &state->flags);
1711 break;
1712 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001713 fallthrough;
Anna Schumaker35a61602018-09-11 16:23:47 -04001714 case -NFS4ERR_ADMIN_REVOKED:
1715 case -NFS4ERR_STALE_STATEID:
1716 case -NFS4ERR_OLD_STATEID:
1717 case -NFS4ERR_BAD_STATEID:
1718 case -NFS4ERR_RECLAIM_BAD:
1719 case -NFS4ERR_RECLAIM_CONFLICT:
1720 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1721 break;
1722 case -NFS4ERR_EXPIRED:
1723 case -NFS4ERR_NO_GRACE:
1724 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001725 fallthrough;
Anna Schumaker35a61602018-09-11 16:23:47 -04001726 case -NFS4ERR_STALE_CLIENTID:
1727 case -NFS4ERR_BADSESSION:
1728 case -NFS4ERR_BADSLOT:
1729 case -NFS4ERR_BAD_HIGH_SLOT:
1730 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust67e7b522019-08-07 07:31:27 -04001731 case -ETIMEDOUT:
Anna Schumaker35a61602018-09-11 16:23:47 -04001732 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 }
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001734 nfs4_put_open_state(state);
Trond Myklebustc137afa2013-02-07 14:41:11 -05001735 spin_lock(&sp->so_lock);
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001736 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
Trond Myklebustabbec2da2014-06-05 10:42:37 -04001738 raw_write_seqcount_end(&sp->so_reclaim_seqcount);
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001739 spin_unlock(&sp->so_lock);
Olga Kornievskaia0b9018b2019-10-08 16:34:36 -04001740#ifdef CONFIG_NFS_V4_2
1741 if (found_ssc_copy_state)
1742 return -EIO;
1743#endif /* CONFIG_NFS_V4_2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 return 0;
1745out_err:
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001746 nfs4_put_open_state(state);
Trond Myklebustc137afa2013-02-07 14:41:11 -05001747 spin_lock(&sp->so_lock);
Trond Myklebustabbec2da2014-06-05 10:42:37 -04001748 raw_write_seqcount_end(&sp->so_reclaim_seqcount);
Trond Myklebustc137afa2013-02-07 14:41:11 -05001749 spin_unlock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return status;
1751}
1752
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001753static void nfs4_clear_open_state(struct nfs4_state *state)
1754{
1755 struct nfs4_lock_state *lock;
1756
1757 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1758 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1759 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1760 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust4b44b402011-12-09 16:31:52 -05001761 spin_lock(&state->state_lock);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001762 list_for_each_entry(lock, &state->lock_states, ls_locks) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001763 lock->ls_seqid.flags = 0;
Trond Myklebust795a88c2012-09-10 13:26:49 -04001764 clear_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001765 }
Trond Myklebust4b44b402011-12-09 16:31:52 -05001766 spin_unlock(&state->state_lock);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001767}
1768
Chuck Lever24d292b2010-12-24 01:32:43 +00001769static void nfs4_reset_seqids(struct nfs_server *server,
1770 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001771{
Chuck Lever24d292b2010-12-24 01:32:43 +00001772 struct nfs_client *clp = server->nfs_client;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001773 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001774 struct rb_node *pos;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001775 struct nfs4_state *state;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001776
Chuck Lever24d292b2010-12-24 01:32:43 +00001777 spin_lock(&clp->cl_lock);
1778 for (pos = rb_first(&server->state_owners);
1779 pos != NULL;
1780 pos = rb_next(pos)) {
1781 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001782 sp->so_seqid.flags = 0;
Trond Myklebustec073422005-10-20 14:22:47 -07001783 spin_lock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001784 list_for_each_entry(state, &sp->so_states, open_states) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001785 if (mark_reclaim(clp, state))
1786 nfs4_clear_open_state(state);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001787 }
Trond Myklebustec073422005-10-20 14:22:47 -07001788 spin_unlock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001789 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001790 spin_unlock(&clp->cl_lock);
1791}
1792
1793static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1794 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1795{
1796 struct nfs_server *server;
1797
1798 rcu_read_lock();
1799 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1800 nfs4_reset_seqids(server, mark_reclaim);
1801 rcu_read_unlock();
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001802}
1803
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001804static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1805{
Trond Myklebuste59679f2022-10-16 14:44:33 -04001806 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001807 /* Mark all delegations for reclaim */
1808 nfs_delegation_mark_reclaim(clp);
1809 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1810}
1811
Trond Myklebust0048fdd2017-05-04 13:44:04 -04001812static int nfs4_reclaim_complete(struct nfs_client *clp,
Trond Myklebust965e9c22013-05-20 11:05:17 -04001813 const struct nfs4_state_recovery_ops *ops,
NeilBrowna52458b2018-12-03 11:30:31 +11001814 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05001815{
1816 /* Notify the server we're done reclaiming our state */
1817 if (ops->reclaim_complete)
Trond Myklebust0048fdd2017-05-04 13:44:04 -04001818 return ops->reclaim_complete(clp, cred);
1819 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05001820}
1821
Chuck Lever24d292b2010-12-24 01:32:43 +00001822static void nfs4_clear_reclaim_server(struct nfs_server *server)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001823{
Chuck Lever24d292b2010-12-24 01:32:43 +00001824 struct nfs_client *clp = server->nfs_client;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001825 struct nfs4_state_owner *sp;
1826 struct rb_node *pos;
1827 struct nfs4_state *state;
1828
Chuck Lever24d292b2010-12-24 01:32:43 +00001829 spin_lock(&clp->cl_lock);
1830 for (pos = rb_first(&server->state_owners);
1831 pos != NULL;
1832 pos = rb_next(pos)) {
1833 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001834 spin_lock(&sp->so_lock);
1835 list_for_each_entry(state, &sp->so_states, open_states) {
Chuck Lever24d292b2010-12-24 01:32:43 +00001836 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1837 &state->flags))
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001838 continue;
1839 nfs4_state_mark_reclaim_nograce(clp, state);
1840 }
1841 spin_unlock(&sp->so_lock);
1842 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001843 spin_unlock(&clp->cl_lock);
1844}
1845
1846static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1847{
1848 struct nfs_server *server;
1849
1850 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1851 return 0;
1852
1853 rcu_read_lock();
1854 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1855 nfs4_clear_reclaim_server(server);
1856 rcu_read_unlock();
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001857
1858 nfs_delegation_reap_unclaimed(clp);
Trond Myklebust6eaa6142010-10-04 17:59:08 -04001859 return 1;
1860}
1861
1862static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1863{
Trond Myklebust965e9c22013-05-20 11:05:17 -04001864 const struct nfs4_state_recovery_ops *ops;
NeilBrowna52458b2018-12-03 11:30:31 +11001865 const struct cred *cred;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04001866 int err;
Trond Myklebust965e9c22013-05-20 11:05:17 -04001867
Trond Myklebust6eaa6142010-10-04 17:59:08 -04001868 if (!nfs4_state_clear_reclaim_reboot(clp))
1869 return;
Trond Myklebust965e9c22013-05-20 11:05:17 -04001870 ops = clp->cl_mvops->reboot_recovery_ops;
Chuck Lever73d8bde2013-07-24 12:28:37 -04001871 cred = nfs4_get_clid_cred(clp);
Trond Myklebust0048fdd2017-05-04 13:44:04 -04001872 err = nfs4_reclaim_complete(clp, ops, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11001873 put_cred(cred);
Trond Myklebust0048fdd2017-05-04 13:44:04 -04001874 if (err == -NFS4ERR_CONN_NOT_BOUND_TO_SESSION)
1875 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001876}
1877
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001878static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1879{
Trond Myklebust45870d62016-09-22 13:38:59 -04001880 nfs_mark_test_expired_all_delegations(clp);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001881 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1882}
1883
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001884static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
Trond Myklebuste598d842008-12-23 15:21:42 -05001885{
1886 switch (error) {
Anna Schumaker000d3f92018-09-11 16:23:48 -04001887 case 0:
1888 break;
1889 case -NFS4ERR_CB_PATH_DOWN:
1890 nfs40_handle_cb_pathdown(clp);
1891 break;
1892 case -NFS4ERR_NO_GRACE:
1893 nfs4_state_end_reclaim_reboot(clp);
1894 break;
1895 case -NFS4ERR_STALE_CLIENTID:
1896 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1897 nfs4_state_start_reclaim_reboot(clp);
1898 break;
1899 case -NFS4ERR_EXPIRED:
1900 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1901 nfs4_state_start_reclaim_nograce(clp);
1902 break;
1903 case -NFS4ERR_BADSESSION:
1904 case -NFS4ERR_BADSLOT:
1905 case -NFS4ERR_BAD_HIGH_SLOT:
1906 case -NFS4ERR_DEADSESSION:
1907 case -NFS4ERR_SEQ_FALSE_RETRY:
1908 case -NFS4ERR_SEQ_MISORDERED:
1909 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1910 /* Zero session reset errors */
1911 break;
1912 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1913 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1914 break;
1915 default:
1916 dprintk("%s: failed to handle error %d for server %s\n",
1917 __func__, error, clp->cl_hostname);
1918 return error;
Trond Myklebuste598d842008-12-23 15:21:42 -05001919 }
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001920 dprintk("%s: handled error %d for server %s\n", __func__, error,
1921 clp->cl_hostname);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001922 return 0;
Trond Myklebuste598d842008-12-23 15:21:42 -05001923}
1924
Trond Myklebust02860012008-12-23 15:21:40 -05001925static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
Chuck Lever24d292b2010-12-24 01:32:43 +00001927 struct nfs4_state_owner *sp;
1928 struct nfs_server *server;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001929 struct rb_node *pos;
Trond Myklebustc77e2282019-08-03 10:11:27 -04001930 LIST_HEAD(freeme);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 int status = 0;
NeilBrown3e2910c2022-03-21 10:59:51 +11001932 int lost_locks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001934restart:
Chuck Lever24d292b2010-12-24 01:32:43 +00001935 rcu_read_lock();
1936 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
Trond Myklebustc77e2282019-08-03 10:11:27 -04001937 nfs4_purge_state_owners(server, &freeme);
Chuck Lever24d292b2010-12-24 01:32:43 +00001938 spin_lock(&clp->cl_lock);
1939 for (pos = rb_first(&server->state_owners);
1940 pos != NULL;
1941 pos = rb_next(pos)) {
1942 sp = rb_entry(pos,
1943 struct nfs4_state_owner, so_server_node);
1944 if (!test_and_clear_bit(ops->owner_flag_bit,
1945 &sp->so_flags))
1946 continue;
Trond Myklebust4a0954e2015-10-02 11:11:16 -04001947 if (!atomic_inc_not_zero(&sp->so_count))
1948 continue;
Chuck Lever24d292b2010-12-24 01:32:43 +00001949 spin_unlock(&clp->cl_lock);
1950 rcu_read_unlock();
1951
NeilBrown3e2910c2022-03-21 10:59:51 +11001952 status = nfs4_reclaim_open_state(sp, ops, &lost_locks);
Chuck Lever24d292b2010-12-24 01:32:43 +00001953 if (status < 0) {
NeilBrown3e2910c2022-03-21 10:59:51 +11001954 if (lost_locks)
1955 pr_warn("NFS: %s: lost %d locks\n",
1956 clp->cl_hostname, lost_locks);
Chuck Lever24d292b2010-12-24 01:32:43 +00001957 set_bit(ops->owner_flag_bit, &sp->so_flags);
1958 nfs4_put_state_owner(sp);
Trond Myklebustdf817ba32014-09-27 17:41:51 -04001959 status = nfs4_recovery_handle_error(clp, status);
1960 return (status != 0) ? status : -EAGAIN;
Chuck Lever24d292b2010-12-24 01:32:43 +00001961 }
1962
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001963 nfs4_put_state_owner(sp);
Chuck Lever24d292b2010-12-24 01:32:43 +00001964 goto restart;
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001965 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001966 spin_unlock(&clp->cl_lock);
Trond Myklebust02860012008-12-23 15:21:40 -05001967 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001968 rcu_read_unlock();
Trond Myklebustc77e2282019-08-03 10:11:27 -04001969 nfs4_free_state_owners(&freeme);
NeilBrown3e2910c2022-03-21 10:59:51 +11001970 if (lost_locks)
1971 pr_warn("NFS: %s: lost %d locks\n",
1972 clp->cl_hostname, lost_locks);
Trond Myklebustdf817ba32014-09-27 17:41:51 -04001973 return 0;
Trond Myklebust02860012008-12-23 15:21:40 -05001974}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Trond Myklebust02860012008-12-23 15:21:40 -05001976static int nfs4_check_lease(struct nfs_client *clp)
1977{
NeilBrowna52458b2018-12-03 11:30:31 +11001978 const struct cred *cred;
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001979 const struct nfs4_state_maintenance_ops *ops =
1980 clp->cl_mvops->state_renewal_ops;
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001981 int status;
Trond Myklebust02860012008-12-23 15:21:40 -05001982
Trond Myklebust0f605b562008-12-23 15:21:42 -05001983 /* Is the client already known to have an expired lease? */
1984 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1985 return 0;
NeilBrownf15e1e82018-12-03 11:30:30 +11001986 cred = ops->get_state_renewal_cred(clp);
Trond Myklebust0f605b562008-12-23 15:21:42 -05001987 if (cred == NULL) {
Chuck Lever73d8bde2013-07-24 12:28:37 -04001988 cred = nfs4_get_clid_cred(clp);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001989 status = -ENOKEY;
Trond Myklebust0f605b562008-12-23 15:21:42 -05001990 if (cred == NULL)
1991 goto out;
Trond Myklebust02860012008-12-23 15:21:40 -05001992 }
Benny Halevy8e69514f2009-04-01 09:22:45 -04001993 status = ops->renew_lease(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11001994 put_cred(cred);
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04001995 if (status == -ETIMEDOUT) {
1996 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1997 return 0;
1998 }
Trond Myklebust0f605b562008-12-23 15:21:42 -05001999out:
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05002000 return nfs4_recovery_handle_error(clp, status);
Trond Myklebust02860012008-12-23 15:21:40 -05002001}
2002
Andy Adamson47b803c2012-10-01 20:42:32 -04002003/* Set NFS4CLNT_LEASE_EXPIRED and reclaim reboot state for all v4.0 errors
2004 * and for recoverable errors on EXCHANGE_ID for v4.1
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04002005 */
2006static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
2007{
2008 switch (status) {
Trond Myklebust89a21732012-05-25 15:00:06 -04002009 case -NFS4ERR_SEQ_MISORDERED:
2010 if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
2011 return -ESERVERFAULT;
2012 /* Lease confirmation error: retry after purging the lease */
2013 ssleep(1);
Andy Adamson47b803c2012-10-01 20:42:32 -04002014 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
2015 break;
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04002016 case -NFS4ERR_STALE_CLIENTID:
2017 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
Andy Adamson47b803c2012-10-01 20:42:32 -04002018 nfs4_state_start_reclaim_reboot(clp);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04002019 break;
Chuck Leverde734832012-07-11 16:30:50 -04002020 case -NFS4ERR_CLID_INUSE:
2021 pr_err("NFS: Server %s reports our clientid is in use\n",
2022 clp->cl_hostname);
2023 nfs_mark_client_ready(clp, -EPERM);
2024 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
2025 return -EPERM;
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04002026 case -EACCES:
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04002027 case -NFS4ERR_DELAY:
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04002028 case -EAGAIN:
2029 ssleep(1);
2030 break;
2031
2032 case -NFS4ERR_MINOR_VERS_MISMATCH:
2033 if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
2034 nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04002035 dprintk("%s: exit with error %d for server %s\n",
2036 __func__, -EPROTONOSUPPORT, clp->cl_hostname);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04002037 return -EPROTONOSUPPORT;
Olga Kornievskaiaea027cb2021-11-15 16:30:40 -05002038 case -ENOSPC:
2039 if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
2040 nfs_mark_client_ready(clp, -EIO);
2041 return -EIO;
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04002042 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
2043 * in nfs4_exchange_id */
2044 default:
Trond Myklebustcc0a9842012-05-28 15:12:27 -04002045 dprintk("%s: exit with error %d for server %s\n", __func__,
2046 status, clp->cl_hostname);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04002047 return status;
2048 }
2049 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04002050 dprintk("%s: handled error %d for server %s\n", __func__, status,
2051 clp->cl_hostname);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04002052 return 0;
2053}
2054
Trond Myklebustb42353f2012-06-05 11:19:47 -04002055static int nfs4_establish_lease(struct nfs_client *clp)
Trond Myklebust02860012008-12-23 15:21:40 -05002056{
NeilBrowna52458b2018-12-03 11:30:31 +11002057 const struct cred *cred;
Trond Myklebustc48f4f32010-06-16 09:52:27 -04002058 const struct nfs4_state_recovery_ops *ops =
2059 clp->cl_mvops->reboot_recovery_ops;
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04002060 int status;
Trond Myklebust02860012008-12-23 15:21:40 -05002061
Trond Myklebust8aafd2f2018-08-11 11:52:38 -04002062 status = nfs4_begin_drain_session(clp);
2063 if (status != 0)
2064 return status;
Chuck Lever73d8bde2013-07-24 12:28:37 -04002065 cred = nfs4_get_clid_cred(clp);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04002066 if (cred == NULL)
2067 return -ENOENT;
2068 status = ops->establish_clid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11002069 put_cred(cred);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04002070 if (status != 0)
Trond Myklebustb42353f2012-06-05 11:19:47 -04002071 return status;
2072 pnfs_destroy_all_layouts(clp);
2073 return 0;
2074}
2075
Chuck Lever6bbb4ae82012-07-11 16:30:59 -04002076/*
2077 * Returns zero or a negative errno. NFS4ERR values are converted
2078 * to local errno values.
2079 */
Trond Myklebustb42353f2012-06-05 11:19:47 -04002080static int nfs4_reclaim_lease(struct nfs_client *clp)
2081{
2082 int status;
2083
2084 status = nfs4_establish_lease(clp);
2085 if (status < 0)
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04002086 return nfs4_handle_reclaim_lease_error(clp, status);
Trond Myklebustb42353f2012-06-05 11:19:47 -04002087 if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
2088 nfs4_state_start_reclaim_nograce(clp);
2089 if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
2090 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
2091 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
2092 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
2093 return 0;
2094}
2095
2096static int nfs4_purge_lease(struct nfs_client *clp)
2097{
2098 int status;
2099
2100 status = nfs4_establish_lease(clp);
2101 if (status < 0)
2102 return nfs4_handle_reclaim_lease_error(clp, status);
2103 clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
2104 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
2105 nfs4_state_start_reclaim_nograce(clp);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04002106 return 0;
Trond Myklebust02860012008-12-23 15:21:40 -05002107}
2108
Chuck Leverc9fdeb22013-10-17 14:13:02 -04002109/*
2110 * Try remote migration of one FSID from a source server to a
2111 * destination server. The source server provides a list of
2112 * potential destinations.
2113 *
2114 * Returns zero or a negative NFS4ERR status code.
2115 */
NeilBrowna52458b2018-12-03 11:30:31 +11002116static int nfs4_try_migration(struct nfs_server *server, const struct cred *cred)
Chuck Leverc9fdeb22013-10-17 14:13:02 -04002117{
2118 struct nfs_client *clp = server->nfs_client;
2119 struct nfs4_fs_locations *locations = NULL;
2120 struct inode *inode;
2121 struct page *page;
2122 int status, result;
2123
2124 dprintk("--> %s: FSID %llx:%llx on \"%s\"\n", __func__,
2125 (unsigned long long)server->fsid.major,
2126 (unsigned long long)server->fsid.minor,
2127 clp->cl_hostname);
2128
2129 result = 0;
2130 page = alloc_page(GFP_KERNEL);
2131 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2132 if (page == NULL || locations == NULL) {
2133 dprintk("<-- %s: no memory\n", __func__);
2134 goto out;
2135 }
Benjamin Coddingtonc3ed2222022-05-14 07:05:13 -04002136 locations->fattr = nfs_alloc_fattr();
2137 if (locations->fattr == NULL) {
2138 dprintk("<-- %s: no memory\n", __func__);
2139 goto out;
2140 }
Chuck Leverc9fdeb22013-10-17 14:13:02 -04002141
David Howells2b0143b2015-03-17 22:25:59 +00002142 inode = d_inode(server->super->s_root);
Olga Kornievskaia1976b2b2022-01-12 10:27:38 -05002143 result = nfs4_proc_get_locations(server, NFS_FH(inode), locations,
2144 page, cred);
Chuck Leverc9fdeb22013-10-17 14:13:02 -04002145 if (result) {
2146 dprintk("<-- %s: failed to retrieve fs_locations: %d\n",
2147 __func__, result);
2148 goto out;
2149 }
2150
2151 result = -NFS4ERR_NXIO;
Olga Kornievskaia90e12a32021-12-09 14:53:29 -05002152 if (!locations->nlocations)
2153 goto out;
2154
Benjamin Coddingtonc3ed2222022-05-14 07:05:13 -04002155 if (!(locations->fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)) {
Chuck Leverc9fdeb22013-10-17 14:13:02 -04002156 dprintk("<-- %s: No fs_locations data, migration skipped\n",
2157 __func__);
2158 goto out;
2159 }
2160
Trond Myklebust8aafd2f2018-08-11 11:52:38 -04002161 status = nfs4_begin_drain_session(clp);
Wenwen Wang1e672e32019-08-20 22:21:21 -05002162 if (status != 0) {
2163 result = status;
2164 goto out;
2165 }
Chuck Leverc9fdeb22013-10-17 14:13:02 -04002166
2167 status = nfs4_replace_transport(server, locations);
2168 if (status != 0) {
2169 dprintk("<-- %s: failed to replace transport: %d\n",
2170 __func__, status);
2171 goto out;
2172 }
2173
2174 result = 0;
2175 dprintk("<-- %s: migration succeeded\n", __func__);
2176
2177out:
2178 if (page != NULL)
2179 __free_page(page);
Benjamin Coddingtonc3ed2222022-05-14 07:05:13 -04002180 if (locations != NULL)
2181 kfree(locations->fattr);
Chuck Leverc9fdeb22013-10-17 14:13:02 -04002182 kfree(locations);
2183 if (result) {
2184 pr_err("NFS: migration recovery failed (server %s)\n",
2185 clp->cl_hostname);
2186 set_bit(NFS_MIG_FAILED, &server->mig_status);
2187 }
2188 return result;
2189}
2190
2191/*
2192 * Returns zero or a negative NFS4ERR status code.
2193 */
2194static int nfs4_handle_migration(struct nfs_client *clp)
2195{
2196 const struct nfs4_state_maintenance_ops *ops =
2197 clp->cl_mvops->state_renewal_ops;
2198 struct nfs_server *server;
NeilBrowna52458b2018-12-03 11:30:31 +11002199 const struct cred *cred;
Chuck Leverc9fdeb22013-10-17 14:13:02 -04002200
2201 dprintk("%s: migration reported on \"%s\"\n", __func__,
2202 clp->cl_hostname);
2203
NeilBrownf15e1e82018-12-03 11:30:30 +11002204 cred = ops->get_state_renewal_cred(clp);
Chuck Leverc9fdeb22013-10-17 14:13:02 -04002205 if (cred == NULL)
2206 return -NFS4ERR_NOENT;
2207
2208 clp->cl_mig_gen++;
2209restart:
2210 rcu_read_lock();
2211 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
2212 int status;
2213
2214 if (server->mig_gen == clp->cl_mig_gen)
2215 continue;
2216 server->mig_gen = clp->cl_mig_gen;
2217
2218 if (!test_and_clear_bit(NFS_MIG_IN_TRANSITION,
2219 &server->mig_status))
2220 continue;
2221
2222 rcu_read_unlock();
2223 status = nfs4_try_migration(server, cred);
2224 if (status < 0) {
NeilBrowna52458b2018-12-03 11:30:31 +11002225 put_cred(cred);
Chuck Leverc9fdeb22013-10-17 14:13:02 -04002226 return status;
2227 }
2228 goto restart;
2229 }
2230 rcu_read_unlock();
NeilBrowna52458b2018-12-03 11:30:31 +11002231 put_cred(cred);
Chuck Leverc9fdeb22013-10-17 14:13:02 -04002232 return 0;
2233}
2234
Chuck Leverb7f7a662013-10-17 14:13:35 -04002235/*
2236 * Test each nfs_server on the clp's cl_superblocks list to see
2237 * if it's moved to another server. Stop when the server no longer
2238 * returns NFS4ERR_LEASE_MOVED.
2239 */
2240static int nfs4_handle_lease_moved(struct nfs_client *clp)
2241{
2242 const struct nfs4_state_maintenance_ops *ops =
2243 clp->cl_mvops->state_renewal_ops;
2244 struct nfs_server *server;
NeilBrowna52458b2018-12-03 11:30:31 +11002245 const struct cred *cred;
Chuck Leverb7f7a662013-10-17 14:13:35 -04002246
2247 dprintk("%s: lease moved reported on \"%s\"\n", __func__,
2248 clp->cl_hostname);
2249
NeilBrownf15e1e82018-12-03 11:30:30 +11002250 cred = ops->get_state_renewal_cred(clp);
Chuck Leverb7f7a662013-10-17 14:13:35 -04002251 if (cred == NULL)
2252 return -NFS4ERR_NOENT;
2253
2254 clp->cl_mig_gen++;
2255restart:
2256 rcu_read_lock();
2257 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
2258 struct inode *inode;
2259 int status;
2260
2261 if (server->mig_gen == clp->cl_mig_gen)
2262 continue;
2263 server->mig_gen = clp->cl_mig_gen;
2264
2265 rcu_read_unlock();
2266
David Howells2b0143b2015-03-17 22:25:59 +00002267 inode = d_inode(server->super->s_root);
Chuck Leverb7f7a662013-10-17 14:13:35 -04002268 status = nfs4_proc_fsid_present(inode, cred);
2269 if (status != -NFS4ERR_MOVED)
2270 goto restart; /* wasn't this one */
2271 if (nfs4_try_migration(server, cred) == -NFS4ERR_LEASE_MOVED)
2272 goto restart; /* there are more */
2273 goto out;
2274 }
2275 rcu_read_unlock();
2276
2277out:
NeilBrowna52458b2018-12-03 11:30:31 +11002278 put_cred(cred);
Chuck Leverb7f7a662013-10-17 14:13:35 -04002279 return 0;
2280}
2281
Chuck Lever05f4c352012-09-14 17:24:32 -04002282/**
2283 * nfs4_discover_server_trunking - Detect server IP address trunking
2284 *
2285 * @clp: nfs_client under test
2286 * @result: OUT: found nfs_client, or clp
2287 *
2288 * Returns zero or a negative errno. If zero is returned,
2289 * an nfs_client pointer is planted in "result".
2290 *
2291 * Note: since we are invoked in process context, and
2292 * not from inside the state manager, we cannot use
2293 * nfs4_handle_reclaim_lease_error().
2294 */
2295int nfs4_discover_server_trunking(struct nfs_client *clp,
2296 struct nfs_client **result)
2297{
2298 const struct nfs4_state_recovery_ops *ops =
2299 clp->cl_mvops->reboot_recovery_ops;
Chuck Lever05f4c352012-09-14 17:24:32 -04002300 struct rpc_clnt *clnt;
NeilBrowna52458b2018-12-03 11:30:31 +11002301 const struct cred *cred;
Chuck Lever4edaa302013-03-16 15:56:20 -04002302 int i, status;
Chuck Lever05f4c352012-09-14 17:24:32 -04002303
2304 dprintk("NFS: %s: testing '%s'\n", __func__, clp->cl_hostname);
2305
Chuck Lever05f4c352012-09-14 17:24:32 -04002306 clnt = clp->cl_rpcclient;
Chuck Lever05f4c352012-09-14 17:24:32 -04002307 i = 0;
2308
2309 mutex_lock(&nfs_clid_init_mutex);
Chuck Lever05f4c352012-09-14 17:24:32 -04002310again:
Trond Myklebustea33e6c2013-04-05 13:22:50 -04002311 status = -ENOENT;
Chuck Lever73d8bde2013-07-24 12:28:37 -04002312 cred = nfs4_get_clid_cred(clp);
Chuck Lever05f4c352012-09-14 17:24:32 -04002313 if (cred == NULL)
2314 goto out_unlock;
2315
2316 status = ops->detect_trunking(clp, result, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11002317 put_cred(cred);
Chuck Lever05f4c352012-09-14 17:24:32 -04002318 switch (status) {
2319 case 0:
Trond Myklebust898fc112017-06-21 10:16:56 -04002320 case -EINTR:
2321 case -ERESTARTSYS:
Chuck Lever05f4c352012-09-14 17:24:32 -04002322 break;
Chuck Lever05f4c352012-09-14 17:24:32 -04002323 case -ETIMEDOUT:
Trond Myklebust150e7262014-03-18 14:23:11 -04002324 if (clnt->cl_softrtry)
2325 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002326 fallthrough;
Trond Myklebust150e7262014-03-18 14:23:11 -04002327 case -NFS4ERR_DELAY:
Chuck Lever05f4c352012-09-14 17:24:32 -04002328 case -EAGAIN:
2329 ssleep(1);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002330 fallthrough;
Trond Myklebust202c3122013-01-18 22:56:23 -05002331 case -NFS4ERR_STALE_CLIENTID:
Chuck Lever05f4c352012-09-14 17:24:32 -04002332 dprintk("NFS: %s after status %d, retrying\n",
2333 __func__, status);
2334 goto again;
Chuck Lever4edaa302013-03-16 15:56:20 -04002335 case -EACCES:
Chuck Leverd688f7b2013-07-24 12:28:28 -04002336 if (i++ == 0) {
2337 nfs4_root_machine_cred(clp);
2338 goto again;
2339 }
Jeff Layton6d769f12013-11-13 09:08:21 -05002340 if (clnt->cl_auth->au_flavor == RPC_AUTH_UNIX)
Chuck Lever4edaa302013-03-16 15:56:20 -04002341 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002342 fallthrough;
Chuck Lever05f4c352012-09-14 17:24:32 -04002343 case -NFS4ERR_CLID_INUSE:
2344 case -NFS4ERR_WRONGSEC:
Jeff Layton6d769f12013-11-13 09:08:21 -05002345 /* No point in retrying if we already used RPC_AUTH_UNIX */
2346 if (clnt->cl_auth->au_flavor == RPC_AUTH_UNIX) {
2347 status = -EPERM;
2348 break;
2349 }
Chuck Lever79d852b2013-04-22 15:42:48 -04002350 clnt = rpc_clone_client_set_auth(clnt, RPC_AUTH_UNIX);
Chuck Lever05f4c352012-09-14 17:24:32 -04002351 if (IS_ERR(clnt)) {
2352 status = PTR_ERR(clnt);
2353 break;
2354 }
Trond Myklebustb193d59a2013-04-04 15:55:00 -04002355 /* Note: this is safe because we haven't yet marked the
2356 * client as ready, so we are the only user of
2357 * clp->cl_rpcclient
2358 */
2359 clnt = xchg(&clp->cl_rpcclient, clnt);
2360 rpc_shutdown_client(clnt);
2361 clnt = clp->cl_rpcclient;
Chuck Lever05f4c352012-09-14 17:24:32 -04002362 goto again;
2363
2364 case -NFS4ERR_MINOR_VERS_MISMATCH:
2365 status = -EPROTONOSUPPORT;
2366 break;
2367
2368 case -EKEYEXPIRED:
Chuck Lever05f4c352012-09-14 17:24:32 -04002369 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
2370 * in nfs4_exchange_id */
2371 status = -EKEYEXPIRED;
Trond Myklebustea33e6c2013-04-05 13:22:50 -04002372 break;
2373 default:
2374 pr_warn("NFS: %s unhandled error %d. Exiting with error EIO\n",
2375 __func__, status);
2376 status = -EIO;
Chuck Lever05f4c352012-09-14 17:24:32 -04002377 }
2378
2379out_unlock:
2380 mutex_unlock(&nfs_clid_init_mutex);
Chuck Lever05f4c352012-09-14 17:24:32 -04002381 dprintk("NFS: %s: status = %d\n", __func__, status);
2382 return status;
2383}
2384
Andy Adamson76db6d92009-04-01 09:22:38 -04002385#ifdef CONFIG_NFS_V4_1
Trond Myklebust9f594792012-05-27 13:02:53 -04002386void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05002387{
Trond Myklebust444f72f2011-05-26 14:26:35 -04002388 struct nfs_client *clp = session->clp;
2389
Trond Myklebust9f594792012-05-27 13:02:53 -04002390 switch (err) {
2391 default:
2392 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2393 break;
2394 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2395 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2396 }
Trond Myklebustd94cbf62016-12-04 19:34:38 -05002397 nfs4_schedule_state_manager(clp);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05002398}
Andy Adamsoncbdabc72011-03-01 01:34:20 +00002399EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05002400
Anna Schumaker3f10a6a2015-07-13 14:01:31 -04002401void nfs41_notify_server(struct nfs_client *clp)
Trond Myklebustac074832012-11-21 09:06:11 -05002402{
2403 /* Use CHECK_LEASE to ping the server with a SEQUENCE */
2404 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
2405 nfs4_schedule_state_manager(clp);
2406}
2407
Trond Myklebust0f79fd62010-03-02 13:06:21 -05002408static void nfs4_reset_all_state(struct nfs_client *clp)
2409{
2410 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
Chuck Lever2c820d92012-05-21 22:45:33 -04002411 set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
Trond Myklebustbe0bfed2012-05-25 16:02:15 -04002412 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05002413 nfs4_state_start_reclaim_nograce(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04002414 dprintk("%s: scheduling reset of all state for server %s!\n",
2415 __func__, clp->cl_hostname);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05002416 nfs4_schedule_state_manager(clp);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05002417 }
2418}
2419
2420static void nfs41_handle_server_reboot(struct nfs_client *clp)
2421{
2422 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
2423 nfs4_state_start_reclaim_reboot(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04002424 dprintk("%s: server %s rebooted!\n", __func__,
2425 clp->cl_hostname);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05002426 nfs4_schedule_state_manager(clp);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05002427 }
2428}
2429
Trond Myklebust8b895ce2015-07-05 15:12:06 -04002430static void nfs41_handle_all_state_revoked(struct nfs_client *clp)
Trond Myklebust0f79fd62010-03-02 13:06:21 -05002431{
Trond Myklebust0f79fd62010-03-02 13:06:21 -05002432 nfs4_reset_all_state(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04002433 dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05002434}
2435
Trond Myklebust8b895ce2015-07-05 15:12:06 -04002436static void nfs41_handle_some_state_revoked(struct nfs_client *clp)
2437{
Trond Myklebust45870d62016-09-22 13:38:59 -04002438 nfs4_state_start_reclaim_nograce(clp);
Trond Myklebust8b895ce2015-07-05 15:12:06 -04002439 nfs4_schedule_state_manager(clp);
2440
2441 dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
2442}
2443
Trond Myklebust0f79fd62010-03-02 13:06:21 -05002444static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
2445{
Trond Myklebust40992872015-07-05 15:20:53 -04002446 /* FIXME: For now, we destroy all layouts. */
2447 pnfs_destroy_all_layouts(clp);
Scott Mayhew8ca017c2019-05-06 11:59:05 -04002448 nfs_test_expired_all_delegations(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04002449 dprintk("%s: Recallable state revoked on server %s!\n", __func__,
2450 clp->cl_hostname);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05002451}
2452
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04002453static void nfs41_handle_backchannel_fault(struct nfs_client *clp)
Trond Myklebust0f79fd62010-03-02 13:06:21 -05002454{
Trond Myklebustb1352902015-07-05 15:26:58 -04002455 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2456 nfs4_schedule_state_manager(clp);
2457
Trond Myklebustcc0a9842012-05-28 15:12:27 -04002458 dprintk("%s: server %s declared a backchannel fault\n", __func__,
2459 clp->cl_hostname);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05002460}
2461
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04002462static void nfs41_handle_cb_path_down(struct nfs_client *clp)
2463{
2464 if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
2465 &clp->cl_state) == 0)
2466 nfs4_schedule_state_manager(clp);
2467}
2468
Trond Myklebust0a014a42016-09-22 13:38:51 -04002469void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags,
2470 bool recovery)
Alexandros Batsakis0629e372009-12-05 13:46:14 -05002471{
2472 if (!flags)
2473 return;
Chuck Lever2c820d92012-05-21 22:45:33 -04002474
2475 dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
2476 __func__, clp->cl_hostname, clp->cl_clientid, flags);
Trond Myklebust0a014a42016-09-22 13:38:51 -04002477 /*
2478 * If we're called from the state manager thread, then assume we're
2479 * already handling the RECLAIM_NEEDED and/or STATE_REVOKED.
2480 * Those flags are expected to remain set until we're done
2481 * recovering (see RFC5661, section 18.46.3).
2482 */
2483 if (recovery)
2484 goto out_recovery;
Chuck Lever2c820d92012-05-21 22:45:33 -04002485
Trond Myklebust111d4892011-12-01 16:37:42 -05002486 if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
Trond Myklebust0f79fd62010-03-02 13:06:21 -05002487 nfs41_handle_server_reboot(clp);
Trond Myklebust8b895ce2015-07-05 15:12:06 -04002488 if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED))
2489 nfs41_handle_all_state_revoked(clp);
2490 if (flags & (SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
Chuck Leverd1c23312013-10-17 14:13:58 -04002491 SEQ4_STATUS_ADMIN_STATE_REVOKED))
Trond Myklebust8b895ce2015-07-05 15:12:06 -04002492 nfs41_handle_some_state_revoked(clp);
Chuck Leverd1c23312013-10-17 14:13:58 -04002493 if (flags & SEQ4_STATUS_LEASE_MOVED)
2494 nfs4_schedule_lease_moved_recovery(clp);
Trond Myklebust111d4892011-12-01 16:37:42 -05002495 if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
Trond Myklebust0f79fd62010-03-02 13:06:21 -05002496 nfs41_handle_recallable_state_revoked(clp);
Trond Myklebust0a014a42016-09-22 13:38:51 -04002497out_recovery:
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04002498 if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
2499 nfs41_handle_backchannel_fault(clp);
2500 else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
2501 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
Trond Myklebust0f79fd62010-03-02 13:06:21 -05002502 nfs41_handle_cb_path_down(clp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -05002503}
2504
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04002505static int nfs4_reset_session(struct nfs_client *clp)
2506{
NeilBrowna52458b2018-12-03 11:30:31 +11002507 const struct cred *cred;
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04002508 int status;
2509
Trond Myklebust1a47e7a2012-06-05 10:53:38 -04002510 if (!nfs4_has_session(clp))
2511 return 0;
Trond Myklebust8aafd2f2018-08-11 11:52:38 -04002512 status = nfs4_begin_drain_session(clp);
2513 if (status != 0)
2514 return status;
Chuck Lever73d8bde2013-07-24 12:28:37 -04002515 cred = nfs4_get_clid_cred(clp);
Trond Myklebust848f5bd2012-05-25 17:51:23 -04002516 status = nfs4_proc_destroy_session(clp->cl_session, cred);
Trond Myklebustc489ee22013-01-30 13:04:10 -05002517 switch (status) {
2518 case 0:
2519 case -NFS4ERR_BADSESSION:
2520 case -NFS4ERR_DEADSESSION:
2521 break;
2522 case -NFS4ERR_BACK_CHAN_BUSY:
2523 case -NFS4ERR_DELAY:
2524 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2525 status = 0;
2526 ssleep(1);
2527 goto out;
2528 default:
Ricardo Labiagaf4558482009-12-07 09:16:09 -05002529 status = nfs4_recovery_handle_error(clp, status);
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04002530 goto out;
2531 }
2532
2533 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
Trond Myklebust848f5bd2012-05-25 17:51:23 -04002534 status = nfs4_proc_create_session(clp, cred);
Andy Adamson41f54a52010-01-21 14:54:13 -05002535 if (status) {
Trond Myklebustcc0a9842012-05-28 15:12:27 -04002536 dprintk("%s: session reset failed with status %d for server %s!\n",
2537 __func__, status, clp->cl_hostname);
Trond Myklebustf2c1b512012-05-27 14:46:46 -04002538 status = nfs4_handle_reclaim_lease_error(clp, status);
Andy Adamson41f54a52010-01-21 14:54:13 -05002539 goto out;
2540 }
Trond Myklebustbda197f2012-06-05 10:22:14 -04002541 nfs41_finish_session_reset(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04002542 dprintk("%s: session reset was successful for server %s!\n",
2543 __func__, clp->cl_hostname);
Andy Adamson41f54a52010-01-21 14:54:13 -05002544out:
NeilBrowna52458b2018-12-03 11:30:31 +11002545 put_cred(cred);
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04002546 return status;
2547}
Andy Adamson76db6d92009-04-01 09:22:38 -04002548
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04002549static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2550{
NeilBrowna52458b2018-12-03 11:30:31 +11002551 const struct cred *cred;
Trond Myklebust2cf047c2012-05-25 17:57:41 -04002552 int ret;
2553
Trond Myklebust1a47e7a2012-06-05 10:53:38 -04002554 if (!nfs4_has_session(clp))
2555 return 0;
Trond Myklebust8aafd2f2018-08-11 11:52:38 -04002556 ret = nfs4_begin_drain_session(clp);
2557 if (ret != 0)
2558 return ret;
Chuck Lever73d8bde2013-07-24 12:28:37 -04002559 cred = nfs4_get_clid_cred(clp);
Trond Myklebust2cf047c2012-05-25 17:57:41 -04002560 ret = nfs4_proc_bind_conn_to_session(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11002561 put_cred(cred);
Trond Myklebust43ac5442012-05-27 13:47:21 -04002562 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
Trond Myklebustbf674c82012-05-27 12:53:10 -04002563 switch (ret) {
2564 case 0:
Trond Myklebustcc0a9842012-05-28 15:12:27 -04002565 dprintk("%s: bind_conn_to_session was successful for server %s!\n",
2566 __func__, clp->cl_hostname);
Trond Myklebustbf674c82012-05-27 12:53:10 -04002567 break;
2568 case -NFS4ERR_DELAY:
2569 ssleep(1);
2570 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2571 break;
2572 default:
2573 return nfs4_recovery_handle_error(clp, ret);
2574 }
2575 return 0;
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04002576}
Trond Myklebustb5fdf842020-02-18 15:58:31 -05002577
2578static void nfs4_layoutreturn_any_run(struct nfs_client *clp)
2579{
2580 int iomode = 0;
2581
2582 if (test_and_clear_bit(NFS4CLNT_RECALL_ANY_LAYOUT_READ, &clp->cl_state))
2583 iomode += IOMODE_READ;
2584 if (test_and_clear_bit(NFS4CLNT_RECALL_ANY_LAYOUT_RW, &clp->cl_state))
2585 iomode += IOMODE_RW;
2586 /* Note: IOMODE_READ + IOMODE_RW == IOMODE_ANY */
2587 if (iomode) {
2588 pnfs_layout_return_unused_byclid(clp, iomode);
2589 set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
2590 }
2591}
Andy Adamson76db6d92009-04-01 09:22:38 -04002592#else /* CONFIG_NFS_V4_1 */
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04002593static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04002594
2595static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2596{
2597 return 0;
2598}
Trond Myklebustb5fdf842020-02-18 15:58:31 -05002599
2600static void nfs4_layoutreturn_any_run(struct nfs_client *clp)
2601{
2602}
Andy Adamson76db6d92009-04-01 09:22:38 -04002603#endif /* CONFIG_NFS_V4_1 */
2604
Trond Myklebuste005e802008-12-23 15:21:48 -05002605static void nfs4_state_manager(struct nfs_client *clp)
Trond Myklebust02860012008-12-23 15:21:40 -05002606{
Trond Myklebust3e178982022-01-29 13:32:45 -05002607 unsigned int memflags;
Trond Myklebust02860012008-12-23 15:21:40 -05002608 int status = 0;
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04002609 const char *section = "", *section_sep = "";
Trond Myklebust02860012008-12-23 15:21:40 -05002610
Trond Myklebust3e178982022-01-29 13:32:45 -05002611 /*
2612 * State recovery can deadlock if the direct reclaim code tries
2613 * start NFS writeback. So ensure memory allocations are all
2614 * GFP_NOFS.
2615 */
2616 memflags = memalloc_nofs_save();
2617
Trond Myklebust02860012008-12-23 15:21:40 -05002618 /* Ensure exclusive access to NFSv4 state */
Trond Myklebust47c21992011-04-15 17:34:18 -04002619 do {
Chuck Lever511ba522019-11-05 11:04:07 -05002620 trace_nfs4_state_mgr(clp);
Trond Myklebustaeabb3c2018-11-19 20:11:45 -05002621 clear_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
Chuck Lever2c820d92012-05-21 22:45:33 -04002622 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04002623 section = "purge state";
Trond Myklebustb42353f2012-06-05 11:19:47 -04002624 status = nfs4_purge_lease(clp);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04002625 if (status < 0)
2626 goto out_error;
Trond Myklebustb42353f2012-06-05 11:19:47 -04002627 continue;
Chuck Lever2c820d92012-05-21 22:45:33 -04002628 }
2629
Trond Myklebustb42353f2012-06-05 11:19:47 -04002630 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04002631 section = "lease expired";
Trond Myklebustb79a4a12008-12-23 15:21:41 -05002632 /* We're going to have to re-establish a clientid */
2633 status = nfs4_reclaim_lease(clp);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04002634 if (status < 0)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05002635 goto out_error;
Trond Myklebustb42353f2012-06-05 11:19:47 -04002636 continue;
Trond Myklebuste598d842008-12-23 15:21:42 -05002637 }
2638
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04002639 /* Initialize or reset the session */
Trond Myklebust1a47e7a2012-06-05 10:53:38 -04002640 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04002641 section = "reset session";
Andy Adamson4d643d12009-12-04 15:52:24 -05002642 status = nfs4_reset_session(clp);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05002643 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
2644 continue;
2645 if (status < 0)
Andy Adamson76db6d92009-04-01 09:22:38 -04002646 goto out_error;
Andy Adamson76db6d92009-04-01 09:22:38 -04002647 }
Trond Myklebustb6d408b2009-12-03 15:53:22 -05002648
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04002649 /* Send BIND_CONN_TO_SESSION */
2650 if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
Trond Myklebust1a47e7a2012-06-05 10:53:38 -04002651 &clp->cl_state)) {
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04002652 section = "bind conn to session";
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04002653 status = nfs4_bind_conn_to_session(clp);
2654 if (status < 0)
2655 goto out_error;
Trond Myklebustbf674c82012-05-27 12:53:10 -04002656 continue;
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04002657 }
2658
Trond Myklebust5df904a2012-11-21 09:22:14 -05002659 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
2660 section = "check lease";
2661 status = nfs4_check_lease(clp);
2662 if (status < 0)
2663 goto out_error;
Olga Kornievskaia8faaa6d2014-09-24 18:11:28 -04002664 continue;
Chuck Leverc9fdeb22013-10-17 14:13:02 -04002665 }
2666
2667 if (test_and_clear_bit(NFS4CLNT_MOVED, &clp->cl_state)) {
2668 section = "migration";
2669 status = nfs4_handle_migration(clp);
2670 if (status < 0)
2671 goto out_error;
Trond Myklebust5df904a2012-11-21 09:22:14 -05002672 }
2673
Chuck Leverb7f7a662013-10-17 14:13:35 -04002674 if (test_and_clear_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state)) {
2675 section = "lease moved";
2676 status = nfs4_handle_lease_moved(clp);
2677 if (status < 0)
2678 goto out_error;
2679 }
2680
Trond Myklebustb79a4a12008-12-23 15:21:41 -05002681 /* First recover reboot state... */
Trond Myklebuste345e882009-12-03 15:52:41 -05002682 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04002683 section = "reclaim reboot";
Andy Adamson591d71c2009-04-01 09:22:47 -04002684 status = nfs4_do_reclaim(clp,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04002685 clp->cl_mvops->reboot_recovery_ops);
Trond Myklebustdf817ba32014-09-27 17:41:51 -04002686 if (status == -EAGAIN)
Trond Myklebustb6d408b2009-12-03 15:53:22 -05002687 continue;
2688 if (status < 0)
2689 goto out_error;
Trond Myklebustdf817ba32014-09-27 17:41:51 -04002690 nfs4_state_end_reclaim_reboot(clp);
Trond Myklebust5d917cb2022-10-16 14:44:32 -04002691 continue;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05002692 }
2693
Trond Myklebust45870d62016-09-22 13:38:59 -04002694 /* Detect expired delegations... */
2695 if (test_and_clear_bit(NFS4CLNT_DELEGATION_EXPIRED, &clp->cl_state)) {
2696 section = "detect expired delegations";
2697 nfs_reap_expired_delegations(clp);
2698 continue;
2699 }
2700
Trond Myklebustb79a4a12008-12-23 15:21:41 -05002701 /* Now recover expired state... */
Trond Myklebust67e7b522019-08-07 07:31:27 -04002702 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04002703 section = "reclaim nograce";
Andy Adamson591d71c2009-04-01 09:22:47 -04002704 status = nfs4_do_reclaim(clp,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04002705 clp->cl_mvops->nograce_recovery_ops);
Trond Myklebustdf817ba32014-09-27 17:41:51 -04002706 if (status == -EAGAIN)
Trond Myklebustb6d408b2009-12-03 15:53:22 -05002707 continue;
2708 if (status < 0)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05002709 goto out_error;
Trond Myklebust67e7b522019-08-07 07:31:27 -04002710 clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05002711 }
Trond Myklebust707fb4b2008-12-23 15:21:47 -05002712
Trond Myklebust3e178982022-01-29 13:32:45 -05002713 memalloc_nofs_restore(memflags);
Alexandros Batsakis5601a002009-12-14 21:27:58 -08002714 nfs4_end_drain_session(clp);
Trond Myklebustaeabb3c2018-11-19 20:11:45 -05002715 nfs4_clear_state_manager_bit(clp);
2716
Trond Myklebusted1cc052023-09-17 19:05:50 -04002717 if (test_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state) &&
2718 !test_and_set_bit(NFS4CLNT_MANAGER_RUNNING,
2719 &clp->cl_state)) {
2720 memflags = memalloc_nofs_save();
2721 continue;
2722 }
2723
Trond Myklebustb5fdf842020-02-18 15:58:31 -05002724 if (!test_and_set_bit(NFS4CLNT_RECALL_RUNNING, &clp->cl_state)) {
Trond Myklebustaeabb3c2018-11-19 20:11:45 -05002725 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
2726 nfs_client_return_marked_delegations(clp);
2727 set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
2728 }
Trond Myklebustb5fdf842020-02-18 15:58:31 -05002729 nfs4_layoutreturn_any_run(clp);
2730 clear_bit(NFS4CLNT_RECALL_RUNNING, &clp->cl_state);
Trond Myklebust707fb4b2008-12-23 15:21:47 -05002731 }
Trond Myklebuste005e802008-12-23 15:21:48 -05002732
NeilBrown4dc73c62022-03-07 10:41:44 +11002733 return;
2734
Trond Myklebusta1aa09be2018-11-05 12:17:01 -05002735 } while (refcount_read(&clp->cl_count) > 1 && !signalled());
Trond Myklebust21a446c2018-11-05 11:10:50 -05002736 goto out_drain;
2737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738out_error:
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04002739 if (strlen(section))
2740 section_sep = ": ";
Chuck Lever511ba522019-11-05 11:04:07 -05002741 trace_nfs4_state_mgr_failed(clp, section, status);
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04002742 pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
2743 " with error %d\n", section_sep, section,
2744 clp->cl_hostname, -status);
Chuck Leverffe5a832012-09-14 17:23:23 -04002745 ssleep(1);
Trond Myklebust21a446c2018-11-05 11:10:50 -05002746out_drain:
Trond Myklebust3e178982022-01-29 13:32:45 -05002747 memalloc_nofs_restore(memflags);
Alexandros Batsakis5601a002009-12-14 21:27:58 -08002748 nfs4_end_drain_session(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05002749 nfs4_clear_state_manager_bit(clp);
2750}
2751
2752static int nfs4_run_state_manager(void *ptr)
2753{
2754 struct nfs_client *clp = ptr;
NeilBrown4dc73c62022-03-07 10:41:44 +11002755 struct rpc_clnt *cl = clp->cl_rpcclient;
2756
2757 while (cl != cl->cl_parent)
2758 cl = cl->cl_parent;
Trond Myklebuste005e802008-12-23 15:21:48 -05002759
2760 allow_signal(SIGKILL);
NeilBrown4dc73c62022-03-07 10:41:44 +11002761again:
Trond Myklebuste005e802008-12-23 15:21:48 -05002762 nfs4_state_manager(clp);
Trond Myklebust956fd462023-09-24 13:14:15 -04002763
2764 if (test_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state) &&
2765 !test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state)) {
NeilBrown4dc73c62022-03-07 10:41:44 +11002766 wait_var_event_interruptible(&clp->cl_state,
2767 test_bit(NFS4CLNT_RUN_MANAGER,
2768 &clp->cl_state));
Trond Myklebust956fd462023-09-24 13:14:15 -04002769 if (!atomic_read(&cl->cl_swapper))
2770 clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);
2771 if (refcount_read(&clp->cl_count) > 1 && !signalled() &&
2772 !test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state))
NeilBrown4dc73c62022-03-07 10:41:44 +11002773 goto again;
2774 /* Either no longer a swapper, or were signalled */
Trond Myklebust956fd462023-09-24 13:14:15 -04002775 clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);
NeilBrown4dc73c62022-03-07 10:41:44 +11002776 }
NeilBrown4dc73c62022-03-07 10:41:44 +11002777
2778 if (refcount_read(&clp->cl_count) > 1 && !signalled() &&
2779 test_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state) &&
Trond Myklebust956fd462023-09-24 13:14:15 -04002780 !test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state))
NeilBrown4dc73c62022-03-07 10:41:44 +11002781 goto again;
2782
Trond Myklebuste005e802008-12-23 15:21:48 -05002783 nfs_put_client(clp);
NeilBrown080abad2022-06-23 14:47:34 +10002784 module_put_and_kthread_exit(0);
Trond Myklebuste005e802008-12-23 15:21:48 -05002785 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786}