blob: 7d73df5a05d1e21cd6fcf0360901e8b4d9cd53d3 [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>
45#include <linux/nfs_idmap.h>
Trond Myklebust5043e902006-01-03 09:55:23 +010046#include <linux/kthread.h>
47#include <linux/module.h>
Trond Myklebust9f958ab2007-07-02 13:58:33 -040048#include <linux/random.h>
Randy Dunlap8c7597f2010-10-22 16:18:52 -070049#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/workqueue.h>
51#include <linux/bitops.h>
Chuck Lever0aaaf5c2011-12-06 16:13:48 -050052#include <linux/jiffies.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"
Trond Myklebust76e697b2012-11-26 14:20:49 -050060#include "nfs4session.h"
Andy Adamson974cec82010-10-20 00:18:02 -040061#include "pnfs.h"
Stanislav Kinsburskybbe0a3a2012-08-20 18:00:36 +040062#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Chuck Levere3c0fb72012-05-21 22:45:24 -040064#define NFSDBG_FACILITY NFSDBG_STATE
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#define OPENOWNER_POOL_SIZE 8
67
Trond Myklebust4ce79712005-06-22 17:16:21 +000068const nfs4_stateid zero_stateid;
Chuck Lever05f4c352012-09-14 17:24:32 -040069static DEFINE_MUTEX(nfs_clid_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static LIST_HEAD(nfs4_clientid_list);
71
Andy Adamson591d71c2009-04-01 09:22:47 -040072int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Trond Myklebustfd954ae2011-04-24 14:28:18 -040074 struct nfs4_setclientid_res clid = {
75 .clientid = clp->cl_clientid,
76 .confirm = clp->cl_confirm,
77 };
Chuck Leverf738f512009-03-18 20:48:06 -040078 unsigned short port;
79 int status;
Stanislav Kinsburskybbe0a3a2012-08-20 18:00:36 +040080 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Chuck Leverf738f512009-03-18 20:48:06 -040081
Trond Myklebustfd954ae2011-04-24 14:28:18 -040082 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
83 goto do_confirm;
Stanislav Kinsburskybbe0a3a2012-08-20 18:00:36 +040084 port = nn->nfs_callback_tcpport;
Chuck Leverf738f512009-03-18 20:48:06 -040085 if (clp->cl_addr.ss_family == AF_INET6)
Stanislav Kinsbursky29dcc162012-08-20 18:00:41 +040086 port = nn->nfs_callback_tcpport6;
Chuck Leverf738f512009-03-18 20:48:06 -040087
Trond Myklebustbb8b27e2010-04-16 16:43:06 -040088 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
89 if (status != 0)
90 goto out;
Trond Myklebustfd954ae2011-04-24 14:28:18 -040091 clp->cl_clientid = clid.clientid;
92 clp->cl_confirm = clid.confirm;
93 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
94do_confirm:
Trond Myklebustbb8b27e2010-04-16 16:43:06 -040095 status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
96 if (status != 0)
97 goto out;
Trond Myklebustfd954ae2011-04-24 14:28:18 -040098 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
Trond Myklebustbb8b27e2010-04-16 16:43:06 -040099 nfs4_schedule_state_renewal(clp);
100out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return status;
102}
103
Chuck Lever05f4c352012-09-14 17:24:32 -0400104/**
105 * nfs40_discover_server_trunking - Detect server IP address trunking (mv0)
106 *
107 * @clp: nfs_client under test
108 * @result: OUT: found nfs_client, or clp
109 * @cred: credential to use for trunking test
110 *
111 * Returns zero, a negative errno, or a negative NFS4ERR status.
112 * If zero is returned, an nfs_client pointer is planted in
113 * "result".
114 *
115 * Note: The returned client may not yet be marked ready.
116 */
117int nfs40_discover_server_trunking(struct nfs_client *clp,
118 struct nfs_client **result,
119 struct rpc_cred *cred)
120{
121 struct nfs4_setclientid_res clid = {
122 .clientid = clp->cl_clientid,
123 .confirm = clp->cl_confirm,
124 };
Trond Myklebust9f623872012-10-01 16:17:31 -0700125 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Chuck Lever05f4c352012-09-14 17:24:32 -0400126 unsigned short port;
127 int status;
128
Trond Myklebust9f623872012-10-01 16:17:31 -0700129 port = nn->nfs_callback_tcpport;
Chuck Lever05f4c352012-09-14 17:24:32 -0400130 if (clp->cl_addr.ss_family == AF_INET6)
Trond Myklebust9f623872012-10-01 16:17:31 -0700131 port = nn->nfs_callback_tcpport6;
Chuck Lever05f4c352012-09-14 17:24:32 -0400132
133 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
134 if (status != 0)
135 goto out;
136 clp->cl_clientid = clid.clientid;
137 clp->cl_confirm = clid.confirm;
138
139 status = nfs40_walk_client_list(clp, result, cred);
140 switch (status) {
141 case -NFS4ERR_STALE_CLIENTID:
142 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
143 case 0:
144 /* Sustain the lease, even if it's empty. If the clientid4
145 * goes stale it's of no use for trunking discovery. */
146 nfs4_schedule_state_renewal(*result);
147 break;
148 }
149
150out:
151 return status;
152}
153
Andy Adamsona7b72102009-04-01 09:22:46 -0400154struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400155{
156 struct rpc_cred *cred = NULL;
157
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400158 if (clp->cl_machine_cred != NULL)
159 cred = get_rpccred(clp->cl_machine_cred);
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400160 return cred;
161}
162
163static void nfs4_clear_machine_cred(struct nfs_client *clp)
164{
165 struct rpc_cred *cred;
166
167 spin_lock(&clp->cl_lock);
168 cred = clp->cl_machine_cred;
169 clp->cl_machine_cred = NULL;
170 spin_unlock(&clp->cl_lock);
171 if (cred != NULL)
172 put_rpccred(cred);
173}
174
Chuck Lever24d292b2010-12-24 01:32:43 +0000175static struct rpc_cred *
176nfs4_get_renew_cred_server_locked(struct nfs_server *server)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100177{
Chuck Lever24d292b2010-12-24 01:32:43 +0000178 struct rpc_cred *cred = NULL;
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100179 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400180 struct rb_node *pos;
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100181
Chuck Lever24d292b2010-12-24 01:32:43 +0000182 for (pos = rb_first(&server->state_owners);
183 pos != NULL;
184 pos = rb_next(pos)) {
185 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100186 if (list_empty(&sp->so_states))
187 continue;
188 cred = get_rpccred(sp->so_cred);
189 break;
190 }
191 return cred;
192}
193
Chuck Lever24d292b2010-12-24 01:32:43 +0000194/**
195 * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
196 * @clp: client state handle
197 *
198 * Returns an rpc_cred with reference count bumped, or NULL.
199 * Caller must hold clp->cl_lock.
200 */
201struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
202{
203 struct rpc_cred *cred = NULL;
204 struct nfs_server *server;
205
Sachin Prabhue49a29b2012-03-16 19:25:52 +0000206 /* Use machine credentials if available */
207 cred = nfs4_get_machine_cred_locked(clp);
208 if (cred != NULL)
209 goto out;
210
Chuck Lever24d292b2010-12-24 01:32:43 +0000211 rcu_read_lock();
212 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
213 cred = nfs4_get_renew_cred_server_locked(server);
214 if (cred != NULL)
215 break;
216 }
217 rcu_read_unlock();
Sachin Prabhue49a29b2012-03-16 19:25:52 +0000218
219out:
Chuck Lever24d292b2010-12-24 01:32:43 +0000220 return cred;
221}
222
Andy Adamsonb4b82602009-04-01 09:22:49 -0400223#if defined(CONFIG_NFS_V4_1)
224
Ricardo Labiaga9430fb62009-12-06 12:23:46 -0500225static int nfs41_setup_state_renewal(struct nfs_client *clp)
226{
227 int status;
228 struct nfs_fsinfo fsinfo;
229
Andy Adamsond6fb79d2011-03-01 01:34:11 +0000230 if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
231 nfs4_schedule_state_renewal(clp);
232 return 0;
233 }
234
Ricardo Labiaga9430fb62009-12-06 12:23:46 -0500235 status = nfs4_proc_get_lease_time(clp, &fsinfo);
236 if (status == 0) {
237 /* Update lease time and schedule renewal */
238 spin_lock(&clp->cl_lock);
239 clp->cl_lease_time = fsinfo.lease_time * HZ;
240 clp->cl_last_renewal = jiffies;
241 spin_unlock(&clp->cl_lock);
242
243 nfs4_schedule_state_renewal(clp);
244 }
245
246 return status;
247}
248
Andy Adamson42acd022011-01-06 02:04:34 +0000249/*
250 * Back channel returns NFS4ERR_DELAY for new requests when
251 * NFS4_SESSION_DRAINING is set so there is no work to be done when draining
252 * is ended.
253 */
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800254static void nfs4_end_drain_session(struct nfs_client *clp)
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500255{
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800256 struct nfs4_session *ses = clp->cl_session;
Trond Myklebust961a8282012-01-17 22:57:37 -0500257 struct nfs4_slot_table *tbl;
Trond Myklebust97e548a2012-11-20 14:45:48 -0500258 unsigned int i;
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800259
Trond Myklebusta2118c32010-06-16 09:52:26 -0400260 if (ses == NULL)
261 return;
Trond Myklebust961a8282012-01-17 22:57:37 -0500262 tbl = &ses->fc_slot_table;
Trond Myklebusta2118c32010-06-16 09:52:26 -0400263 if (test_and_clear_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500264 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebust97e548a2012-11-20 14:45:48 -0500265 for (i = 0; i <= tbl->max_slotid; i++) {
Trond Myklebust275e7e22012-11-01 17:07:07 -0400266 if (rpc_wake_up_next(&tbl->slot_tbl_waitq) == NULL)
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800267 break;
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800268 }
Trond Myklebust961a8282012-01-17 22:57:37 -0500269 spin_unlock(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800270 }
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500271}
272
Trond Myklebust5d633602012-11-23 13:09:38 -0500273/*
274 * Signal state manager thread if session fore channel is drained
275 */
276void nfs4_session_drain_complete(struct nfs4_session *session,
277 struct nfs4_slot_table *tbl)
278{
279 if (nfs4_session_draining(session))
280 complete(&tbl->complete);
281}
282
Andy Adamson42acd022011-01-06 02:04:34 +0000283static int nfs4_wait_on_slot_tbl(struct nfs4_slot_table *tbl)
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500284{
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500285 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonb6bf6e72012-02-17 13:05:23 -0500286 if (tbl->highest_used_slotid != NFS4_NO_SLOT) {
Andy Adamson42acd022011-01-06 02:04:34 +0000287 INIT_COMPLETION(tbl->complete);
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500288 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson42acd022011-01-06 02:04:34 +0000289 return wait_for_completion_interruptible(&tbl->complete);
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500290 }
291 spin_unlock(&tbl->slot_tbl_lock);
292 return 0;
293}
294
Andy Adamson42acd022011-01-06 02:04:34 +0000295static int nfs4_begin_drain_session(struct nfs_client *clp)
296{
297 struct nfs4_session *ses = clp->cl_session;
298 int ret = 0;
299
300 set_bit(NFS4_SESSION_DRAINING, &ses->session_state);
301 /* back channel */
302 ret = nfs4_wait_on_slot_tbl(&ses->bc_slot_table);
303 if (ret)
304 return ret;
305 /* fore channel */
306 return nfs4_wait_on_slot_tbl(&ses->fc_slot_table);
307}
308
Trond Myklebustbda197f2012-06-05 10:22:14 -0400309static void nfs41_finish_session_reset(struct nfs_client *clp)
310{
311 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
312 clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
313 /* create_session negotiated new slot table */
Trond Myklebustbda197f2012-06-05 10:22:14 -0400314 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
315 nfs41_setup_state_renewal(clp);
316}
317
Andy Adamson4d643d12009-12-04 15:52:24 -0500318int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
319{
320 int status;
321
Trond Myklebustfd954ae2011-04-24 14:28:18 -0400322 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
323 goto do_confirm;
Trond Myklebust38045412009-12-15 17:36:57 -0500324 nfs4_begin_drain_session(clp);
Andy Adamson4d643d12009-12-04 15:52:24 -0500325 status = nfs4_proc_exchange_id(clp, cred);
Ricardo Labiaga9430fb62009-12-06 12:23:46 -0500326 if (status != 0)
327 goto out;
Trond Myklebustfd954ae2011-04-24 14:28:18 -0400328 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
329do_confirm:
Trond Myklebust848f5bd2012-05-25 17:51:23 -0400330 status = nfs4_proc_create_session(clp, cred);
Ricardo Labiaga9430fb62009-12-06 12:23:46 -0500331 if (status != 0)
332 goto out;
Trond Myklebustbda197f2012-06-05 10:22:14 -0400333 nfs41_finish_session_reset(clp);
Ricardo Labiaga9430fb62009-12-06 12:23:46 -0500334 nfs_mark_client_ready(clp, NFS_CS_READY);
335out:
Andy Adamson4d643d12009-12-04 15:52:24 -0500336 return status;
337}
338
Chuck Lever05f4c352012-09-14 17:24:32 -0400339/**
340 * nfs41_discover_server_trunking - Detect server IP address trunking (mv1)
341 *
342 * @clp: nfs_client under test
343 * @result: OUT: found nfs_client, or clp
344 * @cred: credential to use for trunking test
345 *
346 * Returns NFS4_OK, a negative errno, or a negative NFS4ERR status.
347 * If NFS4_OK is returned, an nfs_client pointer is planted in
348 * "result".
349 *
350 * Note: The returned client may not yet be marked ready.
351 */
352int nfs41_discover_server_trunking(struct nfs_client *clp,
353 struct nfs_client **result,
354 struct rpc_cred *cred)
355{
356 int status;
357
358 status = nfs4_proc_exchange_id(clp, cred);
359 if (status != NFS4_OK)
360 return status;
Weston Andros Adamsonfd483572012-10-02 11:33:22 -0700361 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
Chuck Lever05f4c352012-09-14 17:24:32 -0400362
363 return nfs41_walk_client_list(clp, result, cred);
364}
365
Andy Adamsonb4b82602009-04-01 09:22:49 -0400366struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp)
367{
368 struct rpc_cred *cred;
369
370 spin_lock(&clp->cl_lock);
371 cred = nfs4_get_machine_cred_locked(clp);
372 spin_unlock(&clp->cl_lock);
373 return cred;
374}
375
376#endif /* CONFIG_NFS_V4_1 */
377
Chuck Lever24d292b2010-12-24 01:32:43 +0000378static struct rpc_cred *
379nfs4_get_setclientid_cred_server(struct nfs_server *server)
Trond Myklebust286d7d62006-01-03 09:55:26 +0100380{
Chuck Lever24d292b2010-12-24 01:32:43 +0000381 struct nfs_client *clp = server->nfs_client;
382 struct rpc_cred *cred = NULL;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100383 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400384 struct rb_node *pos;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100385
Trond Myklebust6dc9d572008-12-23 15:21:41 -0500386 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000387 pos = rb_first(&server->state_owners);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400388 if (pos != NULL) {
Chuck Lever24d292b2010-12-24 01:32:43 +0000389 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400390 cred = get_rpccred(sp->so_cred);
Trond Myklebust286d7d62006-01-03 09:55:26 +0100391 }
Trond Myklebust6dc9d572008-12-23 15:21:41 -0500392 spin_unlock(&clp->cl_lock);
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400393 return cred;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100394}
395
Chuck Lever24d292b2010-12-24 01:32:43 +0000396/**
397 * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation
398 * @clp: client state handle
399 *
400 * Returns an rpc_cred with reference count bumped, or NULL.
401 */
402struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
403{
404 struct nfs_server *server;
405 struct rpc_cred *cred;
406
407 spin_lock(&clp->cl_lock);
408 cred = nfs4_get_machine_cred_locked(clp);
409 spin_unlock(&clp->cl_lock);
410 if (cred != NULL)
411 goto out;
412
413 rcu_read_lock();
414 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
415 cred = nfs4_get_setclientid_cred_server(server);
416 if (cred != NULL)
417 break;
418 }
419 rcu_read_unlock();
420
421out:
422 return cred;
423}
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425static struct nfs4_state_owner *
Chuck Lever24d292b2010-12-24 01:32:43 +0000426nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Chuck Lever24d292b2010-12-24 01:32:43 +0000428 struct rb_node **p = &server->state_owners.rb_node,
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400429 *parent = NULL;
Chuck Lever414adf12011-12-06 16:13:39 -0500430 struct nfs4_state_owner *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400432 while (*p != NULL) {
433 parent = *p;
Chuck Lever24d292b2010-12-24 01:32:43 +0000434 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400435
436 if (cred < sp->so_cred)
437 p = &parent->rb_left;
438 else if (cred > sp->so_cred)
439 p = &parent->rb_right;
440 else {
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500441 if (!list_empty(&sp->so_lru))
442 list_del_init(&sp->so_lru);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400443 atomic_inc(&sp->so_count);
Chuck Lever414adf12011-12-06 16:13:39 -0500444 return sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
Chuck Lever414adf12011-12-06 16:13:39 -0500447 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400450static struct nfs4_state_owner *
Chuck Lever24d292b2010-12-24 01:32:43 +0000451nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400452{
Chuck Lever24d292b2010-12-24 01:32:43 +0000453 struct nfs_server *server = new->so_server;
454 struct rb_node **p = &server->state_owners.rb_node,
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400455 *parent = NULL;
456 struct nfs4_state_owner *sp;
Trond Myklebust9157c312012-01-17 22:04:24 -0500457 int err;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400458
459 while (*p != NULL) {
460 parent = *p;
Chuck Lever24d292b2010-12-24 01:32:43 +0000461 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400462
463 if (new->so_cred < sp->so_cred)
464 p = &parent->rb_left;
465 else if (new->so_cred > sp->so_cred)
466 p = &parent->rb_right;
467 else {
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500468 if (!list_empty(&sp->so_lru))
469 list_del_init(&sp->so_lru);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400470 atomic_inc(&sp->so_count);
471 return sp;
472 }
473 }
Trond Myklebust48c22eb2012-01-17 22:04:25 -0500474 err = ida_get_new(&server->openowner_id, &new->so_seqid.owner_id);
Trond Myklebust9157c312012-01-17 22:04:24 -0500475 if (err)
476 return ERR_PTR(err);
Chuck Lever24d292b2010-12-24 01:32:43 +0000477 rb_link_node(&new->so_server_node, parent, p);
478 rb_insert_color(&new->so_server_node, &server->state_owners);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400479 return new;
480}
481
482static void
Chuck Lever24d292b2010-12-24 01:32:43 +0000483nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400484{
Chuck Lever24d292b2010-12-24 01:32:43 +0000485 struct nfs_server *server = sp->so_server;
486
487 if (!RB_EMPTY_NODE(&sp->so_server_node))
488 rb_erase(&sp->so_server_node, &server->state_owners);
Trond Myklebust48c22eb2012-01-17 22:04:25 -0500489 ida_remove(&server->openowner_id, sp->so_seqid.owner_id);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400490}
491
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500492static void
493nfs4_init_seqid_counter(struct nfs_seqid_counter *sc)
494{
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400495 sc->create_time = ktime_get();
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500496 sc->flags = 0;
497 sc->counter = 0;
498 spin_lock_init(&sc->lock);
499 INIT_LIST_HEAD(&sc->list);
500 rpc_init_wait_queue(&sc->wait, "Seqid_waitqueue");
501}
502
503static void
504nfs4_destroy_seqid_counter(struct nfs_seqid_counter *sc)
505{
506 rpc_destroy_wait_queue(&sc->wait);
507}
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509/*
510 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
511 * create a new state_owner.
512 *
513 */
514static struct nfs4_state_owner *
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500515nfs4_alloc_state_owner(struct nfs_server *server,
516 struct rpc_cred *cred,
517 gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 struct nfs4_state_owner *sp;
520
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500521 sp = kzalloc(sizeof(*sp), gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (!sp)
523 return NULL;
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500524 sp->so_server = server;
525 sp->so_cred = get_rpccred(cred);
Trond Myklebustec073422005-10-20 14:22:47 -0700526 spin_lock_init(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 INIT_LIST_HEAD(&sp->so_states);
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500528 nfs4_init_seqid_counter(&sp->so_seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 atomic_set(&sp->so_count, 1);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500530 INIT_LIST_HEAD(&sp->so_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return sp;
532}
533
Adrian Bunk1d2e88e2008-05-02 13:42:45 -0700534static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535nfs4_drop_state_owner(struct nfs4_state_owner *sp)
536{
Trond Myklebustc77365c2012-04-21 12:31:05 -0400537 struct rb_node *rb_node = &sp->so_server_node;
538
539 if (!RB_EMPTY_NODE(rb_node)) {
Chuck Lever24d292b2010-12-24 01:32:43 +0000540 struct nfs_server *server = sp->so_server;
541 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400542
543 spin_lock(&clp->cl_lock);
Trond Myklebustc77365c2012-04-21 12:31:05 -0400544 if (!RB_EMPTY_NODE(rb_node)) {
545 rb_erase(rb_node, &server->state_owners);
546 RB_CLEAR_NODE(rb_node);
547 }
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400548 spin_unlock(&clp->cl_lock);
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500552static void nfs4_free_state_owner(struct nfs4_state_owner *sp)
553{
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500554 nfs4_destroy_seqid_counter(&sp->so_seqid);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500555 put_rpccred(sp->so_cred);
556 kfree(sp);
557}
558
559static void nfs4_gc_state_owners(struct nfs_server *server)
560{
561 struct nfs_client *clp = server->nfs_client;
562 struct nfs4_state_owner *sp, *tmp;
563 unsigned long time_min, time_max;
564 LIST_HEAD(doomed);
565
566 spin_lock(&clp->cl_lock);
567 time_max = jiffies;
568 time_min = (long)time_max - (long)clp->cl_lease_time;
569 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
570 /* NB: LRU is sorted so that oldest is at the head */
571 if (time_in_range(sp->so_expires, time_min, time_max))
572 break;
573 list_move(&sp->so_lru, &doomed);
574 nfs4_remove_state_owner_locked(sp);
575 }
576 spin_unlock(&clp->cl_lock);
577
578 list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
579 list_del(&sp->so_lru);
580 nfs4_free_state_owner(sp);
581 }
582}
583
Chuck Lever24d292b2010-12-24 01:32:43 +0000584/**
585 * nfs4_get_state_owner - Look up a state owner given a credential
586 * @server: nfs_server to search
587 * @cred: RPC credential to match
588 *
589 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
590 */
591struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500592 struct rpc_cred *cred,
593 gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
David Howells7539bba2006-08-22 20:06:09 -0400595 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 struct nfs4_state_owner *sp, *new;
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000599 sp = nfs4_find_state_owner_locked(server, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 spin_unlock(&clp->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (sp != NULL)
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500602 goto out;
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500603 new = nfs4_alloc_state_owner(server, cred, gfp_flags);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400604 if (new == NULL)
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500605 goto out;
Trond Myklebust9157c312012-01-17 22:04:24 -0500606 do {
607 if (ida_pre_get(&server->openowner_id, gfp_flags) == 0)
608 break;
609 spin_lock(&clp->cl_lock);
610 sp = nfs4_insert_state_owner_locked(new);
611 spin_unlock(&clp->cl_lock);
612 } while (sp == ERR_PTR(-EAGAIN));
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500613 if (sp != new)
614 nfs4_free_state_owner(new);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500615out:
616 nfs4_gc_state_owners(server);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400617 return sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Chuck Lever24d292b2010-12-24 01:32:43 +0000620/**
621 * nfs4_put_state_owner - Release a nfs4_state_owner
622 * @sp: state owner data to release
Trond Myklebust7bf97bc2012-04-21 12:36:19 -0400623 *
624 * Note that we keep released state owners on an LRU
625 * list.
626 * This caches valid state owners so that they can be
627 * reused, to avoid the OPEN_CONFIRM on minor version 0.
628 * It also pins the uniquifier of dropped state owners for
629 * a while, to ensure that those state owner names are
630 * never reused.
Chuck Lever24d292b2010-12-24 01:32:43 +0000631 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632void nfs4_put_state_owner(struct nfs4_state_owner *sp)
633{
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500634 struct nfs_server *server = sp->so_server;
635 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
638 return;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500639
Trond Myklebust7bf97bc2012-04-21 12:36:19 -0400640 sp->so_expires = jiffies;
641 list_add_tail(&sp->so_lru, &server->state_owners_lru);
642 spin_unlock(&clp->cl_lock);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500643}
644
645/**
646 * nfs4_purge_state_owners - Release all cached state owners
647 * @server: nfs_server with cached state owners to release
648 *
649 * Called at umount time. Remaining state owners will be on
650 * the LRU with ref count of zero.
651 */
652void nfs4_purge_state_owners(struct nfs_server *server)
653{
654 struct nfs_client *clp = server->nfs_client;
655 struct nfs4_state_owner *sp, *tmp;
656 LIST_HEAD(doomed);
657
658 spin_lock(&clp->cl_lock);
659 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
660 list_move(&sp->so_lru, &doomed);
661 nfs4_remove_state_owner_locked(sp);
662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 spin_unlock(&clp->cl_lock);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500664
665 list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
666 list_del(&sp->so_lru);
667 nfs4_free_state_owner(sp);
668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671static struct nfs4_state *
672nfs4_alloc_open_state(void)
673{
674 struct nfs4_state *state;
675
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400676 state = kzalloc(sizeof(*state), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (!state)
678 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 atomic_set(&state->count, 1);
680 INIT_LIST_HEAD(&state->lock_states);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000681 spin_lock_init(&state->state_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400682 seqlock_init(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return state;
684}
685
Trond Myklebust4cecb762005-11-04 15:32:58 -0500686void
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500687nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
Trond Myklebust4cecb762005-11-04 15:32:58 -0500688{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500689 if (state->state == fmode)
Trond Myklebust4cecb762005-11-04 15:32:58 -0500690 return;
691 /* NB! List reordering - see the reclaim code for why. */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500692 if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
693 if (fmode & FMODE_WRITE)
Trond Myklebust4cecb762005-11-04 15:32:58 -0500694 list_move(&state->open_states, &state->owner->so_states);
695 else
696 list_move_tail(&state->open_states, &state->owner->so_states);
697 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500698 state->state = fmode;
Trond Myklebust4cecb762005-11-04 15:32:58 -0500699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701static struct nfs4_state *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702__nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
703{
704 struct nfs_inode *nfsi = NFS_I(inode);
705 struct nfs4_state *state;
706
707 list_for_each_entry(state, &nfsi->open_states, inode_states) {
Trond Myklebust1c816ef2007-07-03 14:41:19 -0400708 if (state->owner != owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 continue;
Trond Myklebust1c816ef2007-07-03 14:41:19 -0400710 if (atomic_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{
719 kfree(state);
720}
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
728 spin_lock(&inode->i_lock);
729 state = __nfs4_find_state_byowner(inode, owner);
730 spin_unlock(&inode->i_lock);
731 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);
741 list_add(&state->inode_states, &nfsi->open_states);
Dave Chinner0444d762011-03-29 18:08:50 +1100742 ihold(inode);
743 state->inode = inode;
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 Myklebustec073422005-10-20 14:22:47 -0700764 if (!atomic_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 Myklebustba6830312007-07-27 10:23:05 -0400767 list_del(&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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 nfs4_free_open_state(state);
773 nfs4_put_state_owner(owner);
774}
775
776/*
Trond Myklebust83c9d412005-10-18 14:20:13 -0700777 * Close the current file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 */
Al Viro643168c2011-06-22 18:20:23 -0400779static void __nfs4_close(struct nfs4_state *state,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400780 fmode_t fmode, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 struct nfs4_state_owner *owner = state->owner;
Trond Myklebust003707c2007-07-05 18:07:55 -0400783 int call_close = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500784 fmode_t newstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 atomic_inc(&owner->so_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /* Protect against nfs4_find_state() */
Trond Myklebustec073422005-10-20 14:22:47 -0700788 spin_lock(&owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500789 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100790 case FMODE_READ:
791 state->n_rdonly--;
792 break;
793 case FMODE_WRITE:
794 state->n_wronly--;
795 break;
796 case FMODE_READ|FMODE_WRITE:
797 state->n_rdwr--;
798 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400799 newstate = FMODE_READ|FMODE_WRITE;
Trond Myklebuste7616922006-01-03 09:55:13 +0100800 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400801 if (state->n_rdonly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100802 newstate &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -0400803 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
804 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
805 }
806 if (state->n_wronly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100807 newstate &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -0400808 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
809 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
810 }
811 if (newstate == 0)
812 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebuste7616922006-01-03 09:55:13 +0100813 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400814 nfs4_state_set_mode_locked(state, newstate);
Trond Myklebustec073422005-10-20 14:22:47 -0700815 spin_unlock(&owner->so_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500816
Trond Myklebust003707c2007-07-05 18:07:55 -0400817 if (!call_close) {
Trond Myklebustb39e6252007-06-11 23:05:07 -0400818 nfs4_put_open_state(state);
819 nfs4_put_state_owner(owner);
Trond Myklebust1f7977c2012-09-20 20:31:51 -0400820 } else
821 nfs4_do_close(state, gfp_mask, wait);
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400822}
823
Al Viro643168c2011-06-22 18:20:23 -0400824void nfs4_close_state(struct nfs4_state *state, fmode_t fmode)
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400825{
Al Viro643168c2011-06-22 18:20:23 -0400826 __nfs4_close(state, fmode, GFP_NOFS, 0);
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400827}
828
Al Viro643168c2011-06-22 18:20:23 -0400829void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode)
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400830{
Al Viro643168c2011-06-22 18:20:23 -0400831 __nfs4_close(state, fmode, GFP_KERNEL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
834/*
835 * Search the state->lock_states for an existing lock_owner
836 * that is compatible with current->files
837 */
838static struct nfs4_lock_state *
Trond Myklebust77041ed2010-07-01 12:49:11 -0400839__nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 struct nfs4_lock_state *pos;
842 list_for_each_entry(pos, &state->lock_states, ls_locks) {
Trond Myklebust77041ed2010-07-01 12:49:11 -0400843 if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 continue;
Trond Myklebust77041ed2010-07-01 12:49:11 -0400845 switch (pos->ls_owner.lo_type) {
846 case NFS4_POSIX_LOCK_TYPE:
847 if (pos->ls_owner.lo_u.posix_owner != fl_owner)
848 continue;
849 break;
850 case NFS4_FLOCK_LOCK_TYPE:
851 if (pos->ls_owner.lo_u.flock_owner != fl_pid)
852 continue;
853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 atomic_inc(&pos->ls_count);
855 return pos;
856 }
857 return NULL;
858}
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860/*
861 * Return a compatible lock_state. If no initialized lock_state structure
862 * exists, return an uninitialized one.
863 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 */
Trond Myklebust77041ed2010-07-01 12:49:11 -0400865static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 struct nfs4_lock_state *lsp;
Chuck Lever24d292b2010-12-24 01:32:43 +0000868 struct nfs_server *server = state->owner->so_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400870 lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (lsp == NULL)
872 return NULL;
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500873 nfs4_init_seqid_counter(&lsp->ls_seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 atomic_set(&lsp->ls_count, 1);
Trond Myklebustb64aec82009-07-21 16:47:46 -0400875 lsp->ls_state = state;
Trond Myklebust77041ed2010-07-01 12:49:11 -0400876 lsp->ls_owner.lo_type = type;
877 switch (lsp->ls_owner.lo_type) {
878 case NFS4_FLOCK_LOCK_TYPE:
879 lsp->ls_owner.lo_u.flock_owner = fl_pid;
880 break;
881 case NFS4_POSIX_LOCK_TYPE:
882 lsp->ls_owner.lo_u.posix_owner = fl_owner;
883 break;
884 default:
Trond Myklebustd2d7ce22012-01-17 22:04:25 -0500885 goto out_free;
Trond Myklebust77041ed2010-07-01 12:49:11 -0400886 }
Trond Myklebust48c22eb2012-01-17 22:04:25 -0500887 lsp->ls_seqid.owner_id = ida_simple_get(&server->lockowner_id, 0, 0, GFP_NOFS);
888 if (lsp->ls_seqid.owner_id < 0)
Trond Myklebustd2d7ce22012-01-17 22:04:25 -0500889 goto out_free;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000890 INIT_LIST_HEAD(&lsp->ls_locks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return lsp;
Trond Myklebustd2d7ce22012-01-17 22:04:25 -0500892out_free:
893 kfree(lsp);
894 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
896
Trond Myklebust5ae67c42012-03-19 16:17:18 -0400897void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400898{
Trond Myklebust48c22eb2012-01-17 22:04:25 -0500899 ida_simple_remove(&server->lockowner_id, lsp->ls_seqid.owner_id);
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500900 nfs4_destroy_seqid_counter(&lsp->ls_seqid);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400901 kfree(lsp);
902}
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904/*
905 * Return a compatible lock_state. If no initialized lock_state structure
906 * exists, return an uninitialized one.
907 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 */
Trond Myklebust77041ed2010-07-01 12:49:11 -0400909static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner, pid_t pid, unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000911 struct nfs4_lock_state *lsp, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000913 for(;;) {
914 spin_lock(&state->state_lock);
Trond Myklebust77041ed2010-07-01 12:49:11 -0400915 lsp = __nfs4_find_lock_state(state, owner, pid, type);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000916 if (lsp != NULL)
917 break;
918 if (new != NULL) {
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000919 list_add(&new->ls_locks, &state->lock_states);
920 set_bit(LK_STATE_IN_USE, &state->flags);
921 lsp = new;
922 new = NULL;
923 break;
924 }
925 spin_unlock(&state->state_lock);
Trond Myklebust77041ed2010-07-01 12:49:11 -0400926 new = nfs4_alloc_lock_state(state, owner, pid, type);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000927 if (new == NULL)
928 return NULL;
929 }
930 spin_unlock(&state->state_lock);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400931 if (new != NULL)
Trond Myklebust5ae67c42012-03-19 16:17:18 -0400932 nfs4_free_lock_state(state->owner->so_server, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return lsp;
934}
935
936/*
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000937 * Release reference to lock_state, and free it if we see that
938 * it is no longer in use
939 */
Trond Myklebustfaf5f492005-10-18 14:20:15 -0700940void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000941{
942 struct nfs4_state *state;
943
944 if (lsp == NULL)
945 return;
946 state = lsp->ls_state;
947 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
948 return;
949 list_del(&lsp->ls_locks);
950 if (list_empty(&state->lock_states))
951 clear_bit(LK_STATE_IN_USE, &state->flags);
952 spin_unlock(&state->state_lock);
Trond Myklebust795a88c2012-09-10 13:26:49 -0400953 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustcf470c32012-03-07 13:49:12 -0500954 if (nfs4_release_lockowner(lsp) == 0)
955 return;
956 }
Trond Myklebust5ae67c42012-03-19 16:17:18 -0400957 nfs4_free_lock_state(lsp->ls_state->owner->so_server, lsp);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000958}
959
960static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
961{
962 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
963
964 dst->fl_u.nfs4_fl.owner = lsp;
965 atomic_inc(&lsp->ls_count);
966}
967
968static void nfs4_fl_release_lock(struct file_lock *fl)
969{
970 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
971}
972
Alexey Dobriyan6aed6282009-09-21 17:01:11 -0700973static const struct file_lock_operations nfs4_fl_lock_ops = {
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000974 .fl_copy_lock = nfs4_fl_copy_lock,
975 .fl_release_private = nfs4_fl_release_lock,
976};
977
978int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
979{
980 struct nfs4_lock_state *lsp;
981
982 if (fl->fl_ops != NULL)
983 return 0;
Trond Myklebust77041ed2010-07-01 12:49:11 -0400984 if (fl->fl_flags & FL_POSIX)
985 lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
986 else if (fl->fl_flags & FL_FLOCK)
Trond Myklebust17280172012-03-11 13:11:00 -0400987 lsp = nfs4_get_lock_state(state, NULL, fl->fl_pid,
988 NFS4_FLOCK_LOCK_TYPE);
Trond Myklebust77041ed2010-07-01 12:49:11 -0400989 else
990 return -EINVAL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000991 if (lsp == NULL)
992 return -ENOMEM;
993 fl->fl_u.nfs4_fl.owner = lsp;
994 fl->fl_ops = &nfs4_fl_lock_ops;
995 return 0;
996}
997
Trond Myklebust4fc87962012-03-08 17:42:01 -0500998static bool nfs4_copy_lock_stateid(nfs4_stateid *dst, struct nfs4_state *state,
Trond Myklebust2a369152012-08-13 18:54:45 -0400999 const struct nfs_lockowner *lockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00001001 struct nfs4_lock_state *lsp;
Trond Myklebust2a369152012-08-13 18:54:45 -04001002 fl_owner_t fl_owner;
1003 pid_t fl_pid;
Trond Myklebust4fc87962012-03-08 17:42:01 -05001004 bool ret = false;
1005
Trond Myklebust2a369152012-08-13 18:54:45 -04001006
1007 if (lockowner == NULL)
1008 goto out;
1009
Trond Myklebust4fc87962012-03-08 17:42:01 -05001010 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
1011 goto out;
1012
Trond Myklebust2a369152012-08-13 18:54:45 -04001013 fl_owner = lockowner->l_owner;
1014 fl_pid = lockowner->l_pid;
Trond Myklebust4fc87962012-03-08 17:42:01 -05001015 spin_lock(&state->state_lock);
1016 lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
Trond Myklebust795a88c2012-09-10 13:26:49 -04001017 if (lsp != NULL && test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) {
Trond Myklebust4fc87962012-03-08 17:42:01 -05001018 nfs4_stateid_copy(dst, &lsp->ls_stateid);
1019 ret = true;
1020 }
1021 spin_unlock(&state->state_lock);
1022 nfs4_put_lock_state(lsp);
1023out:
1024 return ret;
1025}
1026
1027static void nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
1028{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001029 int seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001031 do {
1032 seq = read_seqbegin(&state->seqlock);
Trond Myklebustf597c532012-03-04 18:13:56 -05001033 nfs4_stateid_copy(dst, &state->stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001034 } while (read_seqretry(&state->seqlock, seq));
Trond Myklebust4fc87962012-03-08 17:42:01 -05001035}
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00001036
Trond Myklebust4fc87962012-03-08 17:42:01 -05001037/*
1038 * Byte-range lock aware utility to initialize the stateid of read/write
1039 * requests.
1040 */
1041void nfs4_select_rw_stateid(nfs4_stateid *dst, struct nfs4_state *state,
Trond Myklebust2a369152012-08-13 18:54:45 -04001042 fmode_t fmode, const struct nfs_lockowner *lockowner)
Trond Myklebust4fc87962012-03-08 17:42:01 -05001043{
1044 if (nfs4_copy_delegation_stateid(dst, state->inode, fmode))
1045 return;
Trond Myklebust2a369152012-08-13 18:54:45 -04001046 if (nfs4_copy_lock_stateid(dst, state, lockowner))
Trond Myklebust4fc87962012-03-08 17:42:01 -05001047 return;
1048 nfs4_copy_open_stateid(dst, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001051struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001053 struct nfs_seqid *new;
1054
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001055 new = kmalloc(sizeof(*new), gfp_mask);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001056 if (new != NULL) {
1057 new->sequence = counter;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05001058 INIT_LIST_HEAD(&new->list);
Trond Myklebust4601df22012-01-20 18:47:05 -05001059 new->task = NULL;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001060 }
1061 return new;
1062}
1063
Trond Myklebust72211db2009-12-15 14:47:36 -05001064void nfs_release_seqid(struct nfs_seqid *seqid)
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001065{
Trond Myklebust4601df22012-01-20 18:47:05 -05001066 struct nfs_seqid_counter *sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001067
Trond Myklebust4601df22012-01-20 18:47:05 -05001068 if (list_empty(&seqid->list))
1069 return;
1070 sequence = seqid->sequence;
1071 spin_lock(&sequence->lock);
1072 list_del_init(&seqid->list);
1073 if (!list_empty(&sequence->list)) {
1074 struct nfs_seqid *next;
1075
1076 next = list_first_entry(&sequence->list,
1077 struct nfs_seqid, list);
1078 rpc_wake_up_queued_task(&sequence->wait, next->task);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05001079 }
Trond Myklebust4601df22012-01-20 18:47:05 -05001080 spin_unlock(&sequence->lock);
Trond Myklebust72211db2009-12-15 14:47:36 -05001081}
1082
1083void nfs_free_seqid(struct nfs_seqid *seqid)
1084{
1085 nfs_release_seqid(seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001086 kfree(seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001090 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
1091 * failed with a seqid incrementing error -
1092 * see comments nfs_fs.h:seqid_mutating_error()
1093 */
Trond Myklebust88d90932007-07-02 14:03:03 -04001094static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001096 switch (status) {
1097 case 0:
1098 break;
1099 case -NFS4ERR_BAD_SEQID:
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001100 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
1101 return;
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001102 pr_warn_ratelimited("NFS: v4 server returned a bad"
Dan Muntz497799e2008-02-13 13:09:35 -08001103 " sequence-id error on an"
1104 " unconfirmed sequence %p!\n",
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001105 seqid->sequence);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001106 case -NFS4ERR_STALE_CLIENTID:
1107 case -NFS4ERR_STALE_STATEID:
1108 case -NFS4ERR_BAD_STATEID:
1109 case -NFS4ERR_BADXDR:
1110 case -NFS4ERR_RESOURCE:
1111 case -NFS4ERR_NOFILEHANDLE:
1112 /* Non-seqid mutating errors */
1113 return;
1114 };
1115 /*
1116 * Note: no locking needed as we are guaranteed to be first
1117 * on the sequence list
1118 */
1119 seqid->sequence->counter++;
1120}
1121
1122void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
1123{
Benny Halevy34dc1ad2009-04-01 09:22:52 -04001124 struct nfs4_state_owner *sp = container_of(seqid->sequence,
1125 struct nfs4_state_owner, so_seqid);
1126 struct nfs_server *server = sp->so_server;
1127
1128 if (status == -NFS4ERR_BAD_SEQID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 nfs4_drop_state_owner(sp);
Benny Halevy34dc1ad2009-04-01 09:22:52 -04001130 if (!nfs4_has_session(server->nfs_client))
1131 nfs_increment_seqid(status, seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001132}
1133
1134/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001135 * Increment the seqid if the LOCK/LOCKU succeeded, or
1136 * failed with a seqid incrementing error -
1137 * see comments nfs_fs.h:seqid_mutating_error()
1138 */
1139void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
1140{
Trond Myklebust88d90932007-07-02 14:03:03 -04001141 nfs_increment_seqid(status, seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001142}
1143
1144int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
1145{
Trond Myklebust7ba127a2012-01-17 22:04:25 -05001146 struct nfs_seqid_counter *sequence = seqid->sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001147 int status = 0;
1148
1149 spin_lock(&sequence->lock);
Trond Myklebust4601df22012-01-20 18:47:05 -05001150 seqid->task = task;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05001151 if (list_empty(&seqid->list))
1152 list_add_tail(&seqid->list, &sequence->list);
1153 if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
1154 goto unlock;
Trond Myklebust5d008372008-02-22 16:34:17 -05001155 rpc_sleep_on(&sequence->wait, task, NULL);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05001156 status = -EAGAIN;
1157unlock:
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001158 spin_unlock(&sequence->lock);
1159 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
Trond Myklebuste005e802008-12-23 15:21:48 -05001162static int nfs4_run_state_manager(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Trond Myklebuste005e802008-12-23 15:21:48 -05001164static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
Trond Myklebust433fbe42006-01-03 09:55:22 +01001165{
1166 smp_mb__before_clear_bit();
Trond Myklebuste005e802008-12-23 15:21:48 -05001167 clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
Trond Myklebust433fbe42006-01-03 09:55:22 +01001168 smp_mb__after_clear_bit();
Trond Myklebuste005e802008-12-23 15:21:48 -05001169 wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
Trond Myklebust433fbe42006-01-03 09:55:22 +01001170 rpc_wake_up(&clp->cl_rpcwaitq);
1171}
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173/*
Trond Myklebuste005e802008-12-23 15:21:48 -05001174 * Schedule the nfs_client asynchronous state management routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 */
Trond Myklebustb0d3ded2008-12-23 15:21:50 -05001176void nfs4_schedule_state_manager(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Trond Myklebust5043e902006-01-03 09:55:23 +01001178 struct task_struct *task;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001179 char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Trond Myklebuste005e802008-12-23 15:21:48 -05001181 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1182 return;
Trond Myklebust5043e902006-01-03 09:55:23 +01001183 __module_get(THIS_MODULE);
1184 atomic_inc(&clp->cl_count);
Trond Myklebust2446ab62012-03-01 17:00:56 -05001185
1186 /* The rcu_read_lock() is not strictly necessary, as the state
1187 * manager is the only thread that ever changes the rpc_xprt
1188 * after it's initialized. At this point, we're single threaded. */
1189 rcu_read_lock();
1190 snprintf(buf, sizeof(buf), "%s-manager",
1191 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
1192 rcu_read_unlock();
1193 task = kthread_run(nfs4_run_state_manager, clp, buf);
1194 if (IS_ERR(task)) {
1195 printk(KERN_ERR "%s: kthread_run: %ld\n",
1196 __func__, PTR_ERR(task));
1197 nfs4_clear_state_manager_bit(clp);
1198 nfs_put_client(clp);
1199 module_put(THIS_MODULE);
1200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
1203/*
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001204 * Schedule a lease recovery attempt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001206void nfs4_schedule_lease_recovery(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208 if (!clp)
1209 return;
Trond Myklebuste598d842008-12-23 15:21:42 -05001210 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1211 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001212 dprintk("%s: scheduling lease recovery for server %s\n", __func__,
1213 clp->cl_hostname);
Trond Myklebuste005e802008-12-23 15:21:48 -05001214 nfs4_schedule_state_manager(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
Andy Adamson9cb81962012-03-07 10:49:41 -05001216EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Trond Myklebust33021272012-11-26 13:13:29 -05001218int nfs4_wait_clnt_recover(struct nfs_client *clp)
1219{
1220 int res;
1221
1222 might_sleep();
1223
1224 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
1225 nfs_wait_bit_killable, TASK_KILLABLE);
1226 if (res)
1227 return res;
1228
1229 if (clp->cl_cons_state < 0)
1230 return clp->cl_cons_state;
1231 return 0;
1232}
1233
1234int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1235{
1236 unsigned int loop;
1237 int ret;
1238
1239 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1240 ret = nfs4_wait_clnt_recover(clp);
1241 if (ret != 0)
1242 break;
1243 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1244 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1245 break;
1246 nfs4_schedule_state_manager(clp);
1247 ret = -EIO;
1248 }
1249 return ret;
1250}
1251
Trond Myklebustad1e3962012-03-10 11:23:15 -05001252/*
1253 * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
1254 * @clp: client to process
1255 *
1256 * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
1257 * resend of the SETCLIENTID and hence re-establish the
1258 * callback channel. Then return all existing delegations.
1259 */
1260static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
1261{
1262 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1263 nfs_expire_all_delegations(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001264 dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
1265 clp->cl_hostname);
Trond Myklebustad1e3962012-03-10 11:23:15 -05001266}
1267
Trond Myklebust042b60b2011-08-24 15:07:37 -04001268void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
1269{
Trond Myklebustad1e3962012-03-10 11:23:15 -05001270 nfs40_handle_cb_pathdown(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04001271 nfs4_schedule_state_manager(clp);
1272}
1273
Trond Myklebustf9feab12011-03-09 16:12:46 -05001274static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001275{
1276
1277 set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1278 /* Don't recover state that expired before the reboot */
1279 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1280 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1281 return 0;
1282 }
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001283 set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001284 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1285 return 1;
1286}
1287
Trond Myklebustf9feab12011-03-09 16:12:46 -05001288static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001289{
1290 set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1291 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001292 set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001293 set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1294 return 1;
1295}
1296
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001297void nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
1298{
1299 struct nfs_client *clp = server->nfs_client;
1300
1301 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001302 dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
1303 clp->cl_hostname);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001304 nfs4_schedule_state_manager(clp);
1305}
Andy Adamson9cb81962012-03-07 10:49:41 -05001306EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001307
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001308void nfs_inode_find_state_and_recover(struct inode *inode,
1309 const nfs4_stateid *stateid)
1310{
1311 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1312 struct nfs_inode *nfsi = NFS_I(inode);
1313 struct nfs_open_context *ctx;
1314 struct nfs4_state *state;
1315 bool found = false;
1316
1317 spin_lock(&inode->i_lock);
1318 list_for_each_entry(ctx, &nfsi->open_files, list) {
1319 state = ctx->state;
1320 if (state == NULL)
1321 continue;
1322 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
1323 continue;
Trond Myklebustf597c532012-03-04 18:13:56 -05001324 if (!nfs4_stateid_match(&state->stateid, stateid))
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001325 continue;
1326 nfs4_state_mark_reclaim_nograce(clp, state);
1327 found = true;
1328 }
1329 spin_unlock(&inode->i_lock);
1330 if (found)
1331 nfs4_schedule_state_manager(clp);
1332}
1333
1334
Trond Myklebust02860012008-12-23 15:21:40 -05001335static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 struct inode *inode = state->inode;
Trond Myklebust19e03c52008-12-23 15:21:44 -05001338 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 struct file_lock *fl;
1340 int status = 0;
1341
Trond Myklebust3f09df72009-06-17 13:23:00 -07001342 if (inode->i_flock == NULL)
1343 return 0;
1344
1345 /* Guard against delegation returns and new lock/unlock calls */
Trond Myklebust19e03c52008-12-23 15:21:44 -05001346 down_write(&nfsi->rwsem);
Trond Myklebust3f09df72009-06-17 13:23:00 -07001347 /* Protect inode->i_flock using the BKL */
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001348 lock_flocks();
Harvey Harrison90dc7d22008-02-20 13:03:05 -08001349 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
Trond Myklebust43b2a332005-11-04 15:35:30 -05001350 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 continue;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001352 if (nfs_file_open_context(fl->fl_file)->state != state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 continue;
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001354 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 status = ops->recover_lock(state, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 switch (status) {
Trond Myklebust965b5d62009-06-17 13:22:59 -07001357 case 0:
1358 break;
1359 case -ESTALE:
1360 case -NFS4ERR_ADMIN_REVOKED:
1361 case -NFS4ERR_STALE_STATEID:
1362 case -NFS4ERR_BAD_STATEID:
1363 case -NFS4ERR_EXPIRED:
1364 case -NFS4ERR_NO_GRACE:
1365 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust9c4c7612009-12-03 15:58:56 -05001366 case -NFS4ERR_BADSESSION:
1367 case -NFS4ERR_BADSLOT:
1368 case -NFS4ERR_BAD_HIGH_SLOT:
1369 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001370 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05001372 printk(KERN_ERR "NFS: %s: unhandled error %d. "
1373 "Zeroing state\n", __func__, status);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001374 case -ENOMEM:
1375 case -NFS4ERR_DENIED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 case -NFS4ERR_RECLAIM_BAD:
1377 case -NFS4ERR_RECLAIM_CONFLICT:
Trond Myklebust43b2a332005-11-04 15:35:30 -05001378 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001379 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001381 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001383 unlock_flocks();
Trond Myklebust965b5d62009-06-17 13:22:59 -07001384out:
Trond Myklebust19e03c52008-12-23 15:21:44 -05001385 up_write(&nfsi->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return status;
1387}
1388
Trond Myklebust02860012008-12-23 15:21:40 -05001389static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 struct nfs4_state *state;
1392 struct nfs4_lock_state *lock;
1393 int status = 0;
1394
1395 /* Note: we rely on the sp->so_states list being ordered
1396 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1397 * states first.
1398 * This is needed to ensure that the server won't give us any
1399 * read delegations that we have to return if, say, we are
1400 * recovering after a network partition or a reboot from a
1401 * server that doesn't support a grace period.
1402 */
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001403restart:
1404 spin_lock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 list_for_each_entry(state, &sp->so_states, open_states) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001406 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1407 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (state->state == 0)
1409 continue;
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001410 atomic_inc(&state->count);
1411 spin_unlock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 status = ops->recover_open(sp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (status >= 0) {
Trond Myklebust02860012008-12-23 15:21:40 -05001414 status = nfs4_reclaim_locks(state, ops);
1415 if (status >= 0) {
Trond Myklebust4b44b402011-12-09 16:31:52 -05001416 spin_lock(&state->state_lock);
Trond Myklebust02860012008-12-23 15:21:40 -05001417 list_for_each_entry(lock, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04001418 if (!test_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags))
William Dauchy96dcadc2012-03-14 12:32:04 +01001419 pr_warn_ratelimited("NFS: "
1420 "%s: Lock reclaim "
Weston Andros Adamsona0308892012-01-26 13:32:23 -05001421 "failed!\n", __func__);
Trond Myklebust02860012008-12-23 15:21:40 -05001422 }
Trond Myklebust4b44b402011-12-09 16:31:52 -05001423 spin_unlock(&state->state_lock);
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001424 nfs4_put_open_state(state);
1425 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428 switch (status) {
1429 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05001430 printk(KERN_ERR "NFS: %s: unhandled error %d. "
1431 "Zeroing state\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 case -ENOENT:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001433 case -ENOMEM:
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001434 case -ESTALE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 /*
1436 * Open state on this file cannot be recovered
1437 * All we can do is revert to using the zero stateid.
1438 */
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05001439 memset(&state->stateid, 0,
1440 sizeof(state->stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 /* Mark the file as being 'closed' */
1442 state->state = 0;
1443 break;
Trond Myklebust168667c2010-10-19 19:47:49 -04001444 case -EKEYEXPIRED:
1445 /*
1446 * User RPCSEC_GSS context has expired.
1447 * We cannot recover this stateid now, so
1448 * skip it and allow recovery thread to
1449 * proceed.
1450 */
1451 break;
Trond Myklebust965b5d62009-06-17 13:22:59 -07001452 case -NFS4ERR_ADMIN_REVOKED:
1453 case -NFS4ERR_STALE_STATEID:
1454 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001455 case -NFS4ERR_RECLAIM_BAD:
1456 case -NFS4ERR_RECLAIM_CONFLICT:
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001457 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001458 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 case -NFS4ERR_EXPIRED:
1460 case -NFS4ERR_NO_GRACE:
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001461 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust9c4c7612009-12-03 15:58:56 -05001463 case -NFS4ERR_BADSESSION:
1464 case -NFS4ERR_BADSLOT:
1465 case -NFS4ERR_BAD_HIGH_SLOT:
1466 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 goto out_err;
1468 }
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001469 nfs4_put_open_state(state);
1470 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001472 spin_unlock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return 0;
1474out_err:
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001475 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return status;
1477}
1478
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001479static void nfs4_clear_open_state(struct nfs4_state *state)
1480{
1481 struct nfs4_lock_state *lock;
1482
1483 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1484 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1485 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1486 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust4b44b402011-12-09 16:31:52 -05001487 spin_lock(&state->state_lock);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001488 list_for_each_entry(lock, &state->lock_states, ls_locks) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001489 lock->ls_seqid.flags = 0;
Trond Myklebust795a88c2012-09-10 13:26:49 -04001490 clear_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001491 }
Trond Myklebust4b44b402011-12-09 16:31:52 -05001492 spin_unlock(&state->state_lock);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001493}
1494
Chuck Lever24d292b2010-12-24 01:32:43 +00001495static void nfs4_reset_seqids(struct nfs_server *server,
1496 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001497{
Chuck Lever24d292b2010-12-24 01:32:43 +00001498 struct nfs_client *clp = server->nfs_client;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001499 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001500 struct rb_node *pos;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001501 struct nfs4_state *state;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001502
Chuck Lever24d292b2010-12-24 01:32:43 +00001503 spin_lock(&clp->cl_lock);
1504 for (pos = rb_first(&server->state_owners);
1505 pos != NULL;
1506 pos = rb_next(pos)) {
1507 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001508 sp->so_seqid.flags = 0;
Trond Myklebustec073422005-10-20 14:22:47 -07001509 spin_lock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001510 list_for_each_entry(state, &sp->so_states, open_states) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001511 if (mark_reclaim(clp, state))
1512 nfs4_clear_open_state(state);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001513 }
Trond Myklebustec073422005-10-20 14:22:47 -07001514 spin_unlock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001515 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001516 spin_unlock(&clp->cl_lock);
1517}
1518
1519static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1520 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1521{
1522 struct nfs_server *server;
1523
1524 rcu_read_lock();
1525 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1526 nfs4_reset_seqids(server, mark_reclaim);
1527 rcu_read_unlock();
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001528}
1529
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001530static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1531{
1532 /* Mark all delegations for reclaim */
1533 nfs_delegation_mark_reclaim(clp);
1534 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1535}
1536
Ricardo Labiagafce5c832009-12-05 16:08:41 -05001537static void nfs4_reclaim_complete(struct nfs_client *clp,
1538 const struct nfs4_state_recovery_ops *ops)
1539{
1540 /* Notify the server we're done reclaiming our state */
1541 if (ops->reclaim_complete)
1542 (void)ops->reclaim_complete(clp);
1543}
1544
Chuck Lever24d292b2010-12-24 01:32:43 +00001545static void nfs4_clear_reclaim_server(struct nfs_server *server)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001546{
Chuck Lever24d292b2010-12-24 01:32:43 +00001547 struct nfs_client *clp = server->nfs_client;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001548 struct nfs4_state_owner *sp;
1549 struct rb_node *pos;
1550 struct nfs4_state *state;
1551
Chuck Lever24d292b2010-12-24 01:32:43 +00001552 spin_lock(&clp->cl_lock);
1553 for (pos = rb_first(&server->state_owners);
1554 pos != NULL;
1555 pos = rb_next(pos)) {
1556 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001557 spin_lock(&sp->so_lock);
1558 list_for_each_entry(state, &sp->so_states, open_states) {
Chuck Lever24d292b2010-12-24 01:32:43 +00001559 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1560 &state->flags))
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001561 continue;
1562 nfs4_state_mark_reclaim_nograce(clp, state);
1563 }
1564 spin_unlock(&sp->so_lock);
1565 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001566 spin_unlock(&clp->cl_lock);
1567}
1568
1569static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1570{
1571 struct nfs_server *server;
1572
1573 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1574 return 0;
1575
1576 rcu_read_lock();
1577 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1578 nfs4_clear_reclaim_server(server);
1579 rcu_read_unlock();
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001580
1581 nfs_delegation_reap_unclaimed(clp);
Trond Myklebust6eaa6142010-10-04 17:59:08 -04001582 return 1;
1583}
1584
1585static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1586{
1587 if (!nfs4_state_clear_reclaim_reboot(clp))
1588 return;
1589 nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001590}
1591
1592static void nfs_delegation_clear_all(struct nfs_client *clp)
1593{
1594 nfs_delegation_mark_reclaim(clp);
1595 nfs_delegation_reap_unclaimed(clp);
1596}
1597
1598static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1599{
1600 nfs_delegation_clear_all(clp);
1601 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1602}
1603
Trond Myklebust168667c2010-10-19 19:47:49 -04001604static void nfs4_warn_keyexpired(const char *s)
1605{
1606 printk_ratelimited(KERN_WARNING "Error: state manager"
1607 " encountered RPCSEC_GSS session"
1608 " expired against NFSv4 server %s.\n",
1609 s);
1610}
1611
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001612static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
Trond Myklebuste598d842008-12-23 15:21:42 -05001613{
1614 switch (error) {
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001615 case 0:
1616 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001617 case -NFS4ERR_CB_PATH_DOWN:
Trond Myklebustad1e3962012-03-10 11:23:15 -05001618 nfs40_handle_cb_pathdown(clp);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001619 break;
Trond Myklebustc8b7ae3d2009-12-03 15:53:21 -05001620 case -NFS4ERR_NO_GRACE:
1621 nfs4_state_end_reclaim_reboot(clp);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001622 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001623 case -NFS4ERR_STALE_CLIENTID:
1624 case -NFS4ERR_LEASE_MOVED:
1625 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Trond Myklebust6eaa6142010-10-04 17:59:08 -04001626 nfs4_state_clear_reclaim_reboot(clp);
Trond Myklebuste598d842008-12-23 15:21:42 -05001627 nfs4_state_start_reclaim_reboot(clp);
1628 break;
1629 case -NFS4ERR_EXPIRED:
1630 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1631 nfs4_state_start_reclaim_nograce(clp);
Andy Adamson8ba9bf82009-12-04 15:55:34 -05001632 break;
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001633 case -NFS4ERR_BADSESSION:
1634 case -NFS4ERR_BADSLOT:
1635 case -NFS4ERR_BAD_HIGH_SLOT:
1636 case -NFS4ERR_DEADSESSION:
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001637 case -NFS4ERR_SEQ_FALSE_RETRY:
1638 case -NFS4ERR_SEQ_MISORDERED:
Andy Adamson6df08182009-12-04 15:55:05 -05001639 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05001640 /* Zero session reset errors */
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001641 break;
Trond Myklebust7c5d7252012-05-27 12:58:48 -04001642 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1643 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1644 break;
Trond Myklebust168667c2010-10-19 19:47:49 -04001645 case -EKEYEXPIRED:
1646 /* Nothing we can do */
1647 nfs4_warn_keyexpired(clp->cl_hostname);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001648 break;
1649 default:
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001650 dprintk("%s: failed to handle error %d for server %s\n",
1651 __func__, error, clp->cl_hostname);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001652 return error;
Trond Myklebuste598d842008-12-23 15:21:42 -05001653 }
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001654 dprintk("%s: handled error %d for server %s\n", __func__, error,
1655 clp->cl_hostname);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001656 return 0;
Trond Myklebuste598d842008-12-23 15:21:42 -05001657}
1658
Trond Myklebust02860012008-12-23 15:21:40 -05001659static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Chuck Lever24d292b2010-12-24 01:32:43 +00001661 struct nfs4_state_owner *sp;
1662 struct nfs_server *server;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001663 struct rb_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 int status = 0;
1665
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001666restart:
Chuck Lever24d292b2010-12-24 01:32:43 +00001667 rcu_read_lock();
1668 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001669 nfs4_purge_state_owners(server);
Chuck Lever24d292b2010-12-24 01:32:43 +00001670 spin_lock(&clp->cl_lock);
1671 for (pos = rb_first(&server->state_owners);
1672 pos != NULL;
1673 pos = rb_next(pos)) {
1674 sp = rb_entry(pos,
1675 struct nfs4_state_owner, so_server_node);
1676 if (!test_and_clear_bit(ops->owner_flag_bit,
1677 &sp->so_flags))
1678 continue;
1679 atomic_inc(&sp->so_count);
1680 spin_unlock(&clp->cl_lock);
1681 rcu_read_unlock();
1682
1683 status = nfs4_reclaim_open_state(sp, ops);
1684 if (status < 0) {
1685 set_bit(ops->owner_flag_bit, &sp->so_flags);
1686 nfs4_put_state_owner(sp);
1687 return nfs4_recovery_handle_error(clp, status);
1688 }
1689
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001690 nfs4_put_state_owner(sp);
Chuck Lever24d292b2010-12-24 01:32:43 +00001691 goto restart;
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001692 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001693 spin_unlock(&clp->cl_lock);
Trond Myklebust02860012008-12-23 15:21:40 -05001694 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001695 rcu_read_unlock();
Trond Myklebust02860012008-12-23 15:21:40 -05001696 return status;
1697}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Trond Myklebust02860012008-12-23 15:21:40 -05001699static int nfs4_check_lease(struct nfs_client *clp)
1700{
1701 struct rpc_cred *cred;
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001702 const struct nfs4_state_maintenance_ops *ops =
1703 clp->cl_mvops->state_renewal_ops;
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001704 int status;
Trond Myklebust02860012008-12-23 15:21:40 -05001705
Trond Myklebust0f605b52008-12-23 15:21:42 -05001706 /* Is the client already known to have an expired lease? */
1707 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1708 return 0;
Andy Adamsona7b72102009-04-01 09:22:46 -04001709 spin_lock(&clp->cl_lock);
1710 cred = ops->get_state_renewal_cred_locked(clp);
1711 spin_unlock(&clp->cl_lock);
Trond Myklebust0f605b52008-12-23 15:21:42 -05001712 if (cred == NULL) {
1713 cred = nfs4_get_setclientid_cred(clp);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001714 status = -ENOKEY;
Trond Myklebust0f605b52008-12-23 15:21:42 -05001715 if (cred == NULL)
1716 goto out;
Trond Myklebust02860012008-12-23 15:21:40 -05001717 }
Benny Halevy8e69514f2009-04-01 09:22:45 -04001718 status = ops->renew_lease(clp, cred);
Trond Myklebust0f605b52008-12-23 15:21:42 -05001719 put_rpccred(cred);
1720out:
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001721 return nfs4_recovery_handle_error(clp, status);
Trond Myklebust02860012008-12-23 15:21:40 -05001722}
1723
Andy Adamson47b803c2012-10-01 20:42:32 -04001724/* Set NFS4CLNT_LEASE_EXPIRED and reclaim reboot state for all v4.0 errors
1725 * and for recoverable errors on EXCHANGE_ID for v4.1
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001726 */
1727static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
1728{
1729 switch (status) {
Trond Myklebust89a21732012-05-25 15:00:06 -04001730 case -NFS4ERR_SEQ_MISORDERED:
1731 if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
1732 return -ESERVERFAULT;
1733 /* Lease confirmation error: retry after purging the lease */
1734 ssleep(1);
Andy Adamson47b803c2012-10-01 20:42:32 -04001735 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1736 break;
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001737 case -NFS4ERR_STALE_CLIENTID:
1738 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
Andy Adamson47b803c2012-10-01 20:42:32 -04001739 nfs4_state_clear_reclaim_reboot(clp);
1740 nfs4_state_start_reclaim_reboot(clp);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001741 break;
Chuck Leverde734832012-07-11 16:30:50 -04001742 case -NFS4ERR_CLID_INUSE:
1743 pr_err("NFS: Server %s reports our clientid is in use\n",
1744 clp->cl_hostname);
1745 nfs_mark_client_ready(clp, -EPERM);
1746 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1747 return -EPERM;
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001748 case -EACCES:
1749 if (clp->cl_machine_cred == NULL)
1750 return -EACCES;
1751 /* Handle case where the user hasn't set up machine creds */
1752 nfs4_clear_machine_cred(clp);
1753 case -NFS4ERR_DELAY:
1754 case -ETIMEDOUT:
1755 case -EAGAIN:
1756 ssleep(1);
1757 break;
1758
1759 case -NFS4ERR_MINOR_VERS_MISMATCH:
1760 if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
1761 nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001762 dprintk("%s: exit with error %d for server %s\n",
1763 __func__, -EPROTONOSUPPORT, clp->cl_hostname);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001764 return -EPROTONOSUPPORT;
1765 case -EKEYEXPIRED:
1766 nfs4_warn_keyexpired(clp->cl_hostname);
1767 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1768 * in nfs4_exchange_id */
1769 default:
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001770 dprintk("%s: exit with error %d for server %s\n", __func__,
1771 status, clp->cl_hostname);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001772 return status;
1773 }
1774 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001775 dprintk("%s: handled error %d for server %s\n", __func__, status,
1776 clp->cl_hostname);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001777 return 0;
1778}
1779
Trond Myklebustb42353f2012-06-05 11:19:47 -04001780static int nfs4_establish_lease(struct nfs_client *clp)
Trond Myklebust02860012008-12-23 15:21:40 -05001781{
1782 struct rpc_cred *cred;
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001783 const struct nfs4_state_recovery_ops *ops =
1784 clp->cl_mvops->reboot_recovery_ops;
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001785 int status;
Trond Myklebust02860012008-12-23 15:21:40 -05001786
Andy Adamson90a16612009-04-01 09:22:48 -04001787 cred = ops->get_clid_cred(clp);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001788 if (cred == NULL)
1789 return -ENOENT;
1790 status = ops->establish_clid(clp, cred);
1791 put_rpccred(cred);
1792 if (status != 0)
Trond Myklebustb42353f2012-06-05 11:19:47 -04001793 return status;
1794 pnfs_destroy_all_layouts(clp);
1795 return 0;
1796}
1797
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04001798/*
1799 * Returns zero or a negative errno. NFS4ERR values are converted
1800 * to local errno values.
1801 */
Trond Myklebustb42353f2012-06-05 11:19:47 -04001802static int nfs4_reclaim_lease(struct nfs_client *clp)
1803{
1804 int status;
1805
1806 status = nfs4_establish_lease(clp);
1807 if (status < 0)
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001808 return nfs4_handle_reclaim_lease_error(clp, status);
Trond Myklebustb42353f2012-06-05 11:19:47 -04001809 if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
1810 nfs4_state_start_reclaim_nograce(clp);
1811 if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1812 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1813 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1814 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1815 return 0;
1816}
1817
1818static int nfs4_purge_lease(struct nfs_client *clp)
1819{
1820 int status;
1821
1822 status = nfs4_establish_lease(clp);
1823 if (status < 0)
1824 return nfs4_handle_reclaim_lease_error(clp, status);
1825 clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
1826 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1827 nfs4_state_start_reclaim_nograce(clp);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001828 return 0;
Trond Myklebust02860012008-12-23 15:21:40 -05001829}
1830
Chuck Lever05f4c352012-09-14 17:24:32 -04001831/**
1832 * nfs4_discover_server_trunking - Detect server IP address trunking
1833 *
1834 * @clp: nfs_client under test
1835 * @result: OUT: found nfs_client, or clp
1836 *
1837 * Returns zero or a negative errno. If zero is returned,
1838 * an nfs_client pointer is planted in "result".
1839 *
1840 * Note: since we are invoked in process context, and
1841 * not from inside the state manager, we cannot use
1842 * nfs4_handle_reclaim_lease_error().
1843 */
1844int nfs4_discover_server_trunking(struct nfs_client *clp,
1845 struct nfs_client **result)
1846{
1847 const struct nfs4_state_recovery_ops *ops =
1848 clp->cl_mvops->reboot_recovery_ops;
1849 rpc_authflavor_t *flavors, flav, save;
1850 struct rpc_clnt *clnt;
1851 struct rpc_cred *cred;
1852 int i, len, status;
1853
1854 dprintk("NFS: %s: testing '%s'\n", __func__, clp->cl_hostname);
1855
1856 len = NFS_MAX_SECFLAVORS;
1857 flavors = kcalloc(len, sizeof(*flavors), GFP_KERNEL);
1858 if (flavors == NULL) {
1859 status = -ENOMEM;
1860 goto out;
1861 }
1862 len = rpcauth_list_flavors(flavors, len);
1863 if (len < 0) {
1864 status = len;
1865 goto out_free;
1866 }
1867 clnt = clp->cl_rpcclient;
1868 save = clnt->cl_auth->au_flavor;
1869 i = 0;
1870
1871 mutex_lock(&nfs_clid_init_mutex);
1872 status = -ENOENT;
1873again:
1874 cred = ops->get_clid_cred(clp);
1875 if (cred == NULL)
1876 goto out_unlock;
1877
1878 status = ops->detect_trunking(clp, result, cred);
1879 put_rpccred(cred);
1880 switch (status) {
1881 case 0:
1882 break;
1883
1884 case -EACCES:
1885 if (clp->cl_machine_cred == NULL)
1886 break;
1887 /* Handle case where the user hasn't set up machine creds */
1888 nfs4_clear_machine_cred(clp);
1889 case -NFS4ERR_DELAY:
1890 case -ETIMEDOUT:
1891 case -EAGAIN:
1892 ssleep(1);
1893 dprintk("NFS: %s after status %d, retrying\n",
1894 __func__, status);
1895 goto again;
1896
1897 case -NFS4ERR_CLID_INUSE:
1898 case -NFS4ERR_WRONGSEC:
1899 status = -EPERM;
1900 if (i >= len)
1901 break;
1902
1903 flav = flavors[i++];
1904 if (flav == save)
1905 flav = flavors[i++];
1906 clnt = rpc_clone_client_set_auth(clnt, flav);
1907 if (IS_ERR(clnt)) {
1908 status = PTR_ERR(clnt);
1909 break;
1910 }
1911 clp->cl_rpcclient = clnt;
1912 goto again;
1913
1914 case -NFS4ERR_MINOR_VERS_MISMATCH:
1915 status = -EPROTONOSUPPORT;
1916 break;
1917
1918 case -EKEYEXPIRED:
1919 nfs4_warn_keyexpired(clp->cl_hostname);
1920 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1921 * in nfs4_exchange_id */
1922 status = -EKEYEXPIRED;
1923 }
1924
1925out_unlock:
1926 mutex_unlock(&nfs_clid_init_mutex);
1927out_free:
1928 kfree(flavors);
1929out:
1930 dprintk("NFS: %s: status = %d\n", __func__, status);
1931 return status;
1932}
1933
Andy Adamson76db6d92009-04-01 09:22:38 -04001934#ifdef CONFIG_NFS_V4_1
Trond Myklebust9f594792012-05-27 13:02:53 -04001935void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001936{
Trond Myklebust444f72f2011-05-26 14:26:35 -04001937 struct nfs_client *clp = session->clp;
1938
Trond Myklebust9f594792012-05-27 13:02:53 -04001939 switch (err) {
1940 default:
1941 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1942 break;
1943 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1944 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1945 }
Trond Myklebust444f72f2011-05-26 14:26:35 -04001946 nfs4_schedule_lease_recovery(clp);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001947}
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00001948EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001949
Trond Myklebustac074832012-11-21 09:06:11 -05001950static void nfs41_ping_server(struct nfs_client *clp)
1951{
1952 /* Use CHECK_LEASE to ping the server with a SEQUENCE */
1953 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1954 nfs4_schedule_state_manager(clp);
1955}
1956
1957void nfs41_server_notify_target_slotid_update(struct nfs_client *clp)
1958{
1959 nfs41_ping_server(clp);
1960}
1961
Trond Myklebustc10e4492012-11-26 16:16:54 -05001962void nfs41_server_notify_highest_slotid_update(struct nfs_client *clp)
1963{
1964 nfs41_ping_server(clp);
1965}
1966
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001967static void nfs4_reset_all_state(struct nfs_client *clp)
1968{
1969 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
Chuck Lever2c820d92012-05-21 22:45:33 -04001970 set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
Trond Myklebustbe0bfed2012-05-25 16:02:15 -04001971 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001972 nfs4_state_start_reclaim_nograce(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001973 dprintk("%s: scheduling reset of all state for server %s!\n",
1974 __func__, clp->cl_hostname);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001975 nfs4_schedule_state_manager(clp);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001976 }
1977}
1978
1979static void nfs41_handle_server_reboot(struct nfs_client *clp)
1980{
1981 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1982 nfs4_state_start_reclaim_reboot(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001983 dprintk("%s: server %s rebooted!\n", __func__,
1984 clp->cl_hostname);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001985 nfs4_schedule_state_manager(clp);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001986 }
1987}
1988
1989static void nfs41_handle_state_revoked(struct nfs_client *clp)
1990{
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001991 nfs4_reset_all_state(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001992 dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001993}
1994
1995static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
1996{
1997 /* This will need to handle layouts too */
1998 nfs_expire_all_delegations(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001999 dprintk("%s: Recallable state revoked on server %s!\n", __func__,
2000 clp->cl_hostname);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05002001}
2002
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04002003static void nfs41_handle_backchannel_fault(struct nfs_client *clp)
Trond Myklebust0f79fd62010-03-02 13:06:21 -05002004{
2005 nfs_expire_all_delegations(clp);
2006 if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05002007 nfs4_schedule_state_manager(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04002008 dprintk("%s: server %s declared a backchannel fault\n", __func__,
2009 clp->cl_hostname);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05002010}
2011
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04002012static void nfs41_handle_cb_path_down(struct nfs_client *clp)
2013{
2014 if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
2015 &clp->cl_state) == 0)
2016 nfs4_schedule_state_manager(clp);
2017}
2018
Alexandros Batsakis0629e372009-12-05 13:46:14 -05002019void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
2020{
2021 if (!flags)
2022 return;
Chuck Lever2c820d92012-05-21 22:45:33 -04002023
2024 dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
2025 __func__, clp->cl_hostname, clp->cl_clientid, flags);
2026
Trond Myklebust111d4892011-12-01 16:37:42 -05002027 if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
Trond Myklebust0f79fd62010-03-02 13:06:21 -05002028 nfs41_handle_server_reboot(clp);
Trond Myklebust111d4892011-12-01 16:37:42 -05002029 if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
Alexandros Batsakis0629e372009-12-05 13:46:14 -05002030 SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
2031 SEQ4_STATUS_ADMIN_STATE_REVOKED |
Trond Myklebust0f79fd62010-03-02 13:06:21 -05002032 SEQ4_STATUS_LEASE_MOVED))
2033 nfs41_handle_state_revoked(clp);
Trond Myklebust111d4892011-12-01 16:37:42 -05002034 if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
Trond Myklebust0f79fd62010-03-02 13:06:21 -05002035 nfs41_handle_recallable_state_revoked(clp);
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04002036 if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
2037 nfs41_handle_backchannel_fault(clp);
2038 else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
2039 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
Trond Myklebust0f79fd62010-03-02 13:06:21 -05002040 nfs41_handle_cb_path_down(clp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -05002041}
2042
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04002043static int nfs4_reset_session(struct nfs_client *clp)
2044{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04002045 struct rpc_cred *cred;
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04002046 int status;
2047
Trond Myklebust1a47e7a2012-06-05 10:53:38 -04002048 if (!nfs4_has_session(clp))
2049 return 0;
Trond Myklebust38045412009-12-15 17:36:57 -05002050 nfs4_begin_drain_session(clp);
Trond Myklebust848f5bd2012-05-25 17:51:23 -04002051 cred = nfs4_get_exchange_id_cred(clp);
2052 status = nfs4_proc_destroy_session(clp->cl_session, cred);
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04002053 if (status && status != -NFS4ERR_BADSESSION &&
2054 status != -NFS4ERR_DEADSESSION) {
Ricardo Labiagaf4558482009-12-07 09:16:09 -05002055 status = nfs4_recovery_handle_error(clp, status);
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04002056 goto out;
2057 }
2058
2059 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
Trond Myklebust848f5bd2012-05-25 17:51:23 -04002060 status = nfs4_proc_create_session(clp, cred);
Andy Adamson41f54a52010-01-21 14:54:13 -05002061 if (status) {
Trond Myklebustcc0a9842012-05-28 15:12:27 -04002062 dprintk("%s: session reset failed with status %d for server %s!\n",
2063 __func__, status, clp->cl_hostname);
Trond Myklebustf2c1b512012-05-27 14:46:46 -04002064 status = nfs4_handle_reclaim_lease_error(clp, status);
Andy Adamson41f54a52010-01-21 14:54:13 -05002065 goto out;
2066 }
Trond Myklebustbda197f2012-06-05 10:22:14 -04002067 nfs41_finish_session_reset(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04002068 dprintk("%s: session reset was successful for server %s!\n",
2069 __func__, clp->cl_hostname);
Andy Adamson41f54a52010-01-21 14:54:13 -05002070out:
Trond Myklebust848f5bd2012-05-25 17:51:23 -04002071 if (cred)
2072 put_rpccred(cred);
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04002073 return status;
2074}
Andy Adamson76db6d92009-04-01 09:22:38 -04002075
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04002076static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2077{
Trond Myklebust2cf047c2012-05-25 17:57:41 -04002078 struct rpc_cred *cred;
2079 int ret;
2080
Trond Myklebust1a47e7a2012-06-05 10:53:38 -04002081 if (!nfs4_has_session(clp))
2082 return 0;
Trond Myklebust43ac5442012-05-27 13:47:21 -04002083 nfs4_begin_drain_session(clp);
Trond Myklebust2cf047c2012-05-25 17:57:41 -04002084 cred = nfs4_get_exchange_id_cred(clp);
2085 ret = nfs4_proc_bind_conn_to_session(clp, cred);
2086 if (cred)
2087 put_rpccred(cred);
Trond Myklebust43ac5442012-05-27 13:47:21 -04002088 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
Trond Myklebustbf674c82012-05-27 12:53:10 -04002089 switch (ret) {
2090 case 0:
Trond Myklebustcc0a9842012-05-28 15:12:27 -04002091 dprintk("%s: bind_conn_to_session was successful for server %s!\n",
2092 __func__, clp->cl_hostname);
Trond Myklebustbf674c82012-05-27 12:53:10 -04002093 break;
2094 case -NFS4ERR_DELAY:
2095 ssleep(1);
2096 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2097 break;
2098 default:
2099 return nfs4_recovery_handle_error(clp, ret);
2100 }
2101 return 0;
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04002102}
Andy Adamson76db6d92009-04-01 09:22:38 -04002103#else /* CONFIG_NFS_V4_1 */
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04002104static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
Alexandros Batsakis5601a002009-12-14 21:27:58 -08002105static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04002106
2107static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2108{
2109 return 0;
2110}
Andy Adamson76db6d92009-04-01 09:22:38 -04002111#endif /* CONFIG_NFS_V4_1 */
2112
Trond Myklebuste005e802008-12-23 15:21:48 -05002113static void nfs4_state_manager(struct nfs_client *clp)
Trond Myklebust02860012008-12-23 15:21:40 -05002114{
Trond Myklebust02860012008-12-23 15:21:40 -05002115 int status = 0;
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04002116 const char *section = "", *section_sep = "";
Trond Myklebust02860012008-12-23 15:21:40 -05002117
Trond Myklebust02860012008-12-23 15:21:40 -05002118 /* Ensure exclusive access to NFSv4 state */
Trond Myklebust47c21992011-04-15 17:34:18 -04002119 do {
Chuck Lever2c820d92012-05-21 22:45:33 -04002120 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04002121 section = "purge state";
Trond Myklebustb42353f2012-06-05 11:19:47 -04002122 status = nfs4_purge_lease(clp);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04002123 if (status < 0)
2124 goto out_error;
Trond Myklebustb42353f2012-06-05 11:19:47 -04002125 continue;
Chuck Lever2c820d92012-05-21 22:45:33 -04002126 }
2127
Trond Myklebustb42353f2012-06-05 11:19:47 -04002128 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04002129 section = "lease expired";
Trond Myklebustb79a4a12008-12-23 15:21:41 -05002130 /* We're going to have to re-establish a clientid */
2131 status = nfs4_reclaim_lease(clp);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04002132 if (status < 0)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05002133 goto out_error;
Trond Myklebustb42353f2012-06-05 11:19:47 -04002134 continue;
Trond Myklebuste598d842008-12-23 15:21:42 -05002135 }
2136
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04002137 /* Initialize or reset the session */
Trond Myklebust1a47e7a2012-06-05 10:53:38 -04002138 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04002139 section = "reset session";
Andy Adamson4d643d12009-12-04 15:52:24 -05002140 status = nfs4_reset_session(clp);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05002141 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
2142 continue;
2143 if (status < 0)
Andy Adamson76db6d92009-04-01 09:22:38 -04002144 goto out_error;
Andy Adamson76db6d92009-04-01 09:22:38 -04002145 }
Trond Myklebustb6d408b2009-12-03 15:53:22 -05002146
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04002147 /* Send BIND_CONN_TO_SESSION */
2148 if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
Trond Myklebust1a47e7a2012-06-05 10:53:38 -04002149 &clp->cl_state)) {
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04002150 section = "bind conn to session";
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04002151 status = nfs4_bind_conn_to_session(clp);
2152 if (status < 0)
2153 goto out_error;
Trond Myklebustbf674c82012-05-27 12:53:10 -04002154 continue;
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04002155 }
2156
Trond Myklebust5df904a2012-11-21 09:22:14 -05002157 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
2158 section = "check lease";
2159 status = nfs4_check_lease(clp);
2160 if (status < 0)
2161 goto out_error;
2162 continue;
2163 }
2164
Trond Myklebustb79a4a12008-12-23 15:21:41 -05002165 /* First recover reboot state... */
Trond Myklebuste345e882009-12-03 15:52:41 -05002166 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04002167 section = "reclaim reboot";
Andy Adamson591d71c2009-04-01 09:22:47 -04002168 status = nfs4_do_reclaim(clp,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04002169 clp->cl_mvops->reboot_recovery_ops);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05002170 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
Andy Adamson6df08182009-12-04 15:55:05 -05002171 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04002172 continue;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05002173 nfs4_state_end_reclaim_reboot(clp);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05002174 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
2175 continue;
2176 if (status < 0)
2177 goto out_error;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05002178 }
2179
2180 /* Now recover expired state... */
2181 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04002182 section = "reclaim nograce";
Andy Adamson591d71c2009-04-01 09:22:47 -04002183 status = nfs4_do_reclaim(clp,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04002184 clp->cl_mvops->nograce_recovery_ops);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05002185 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
Andy Adamson6df08182009-12-04 15:55:05 -05002186 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
Trond Myklebustb6d408b2009-12-03 15:53:22 -05002187 test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
2188 continue;
2189 if (status < 0)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05002190 goto out_error;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05002191 }
Trond Myklebust707fb4b2008-12-23 15:21:47 -05002192
Alexandros Batsakis5601a002009-12-14 21:27:58 -08002193 nfs4_end_drain_session(clp);
Trond Myklebust707fb4b2008-12-23 15:21:47 -05002194 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
2195 nfs_client_return_marked_delegations(clp);
2196 continue;
2197 }
Trond Myklebuste005e802008-12-23 15:21:48 -05002198
2199 nfs4_clear_state_manager_bit(clp);
Trond Myklebustf3c76492008-12-23 15:21:48 -05002200 /* Did we race with an attempt to give us more work? */
2201 if (clp->cl_state == 0)
2202 break;
2203 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
2204 break;
Trond Myklebust47c21992011-04-15 17:34:18 -04002205 } while (atomic_read(&clp->cl_count) > 1);
Trond Myklebuste005e802008-12-23 15:21:48 -05002206 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207out_error:
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04002208 if (strlen(section))
2209 section_sep = ": ";
2210 pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
2211 " with error %d\n", section_sep, section,
2212 clp->cl_hostname, -status);
Chuck Leverffe5a832012-09-14 17:23:23 -04002213 ssleep(1);
Alexandros Batsakis5601a002009-12-14 21:27:58 -08002214 nfs4_end_drain_session(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05002215 nfs4_clear_state_manager_bit(clp);
2216}
2217
2218static int nfs4_run_state_manager(void *ptr)
2219{
2220 struct nfs_client *clp = ptr;
2221
2222 allow_signal(SIGKILL);
2223 nfs4_state_manager(clp);
2224 nfs_put_client(clp);
2225 module_put_and_exit(0);
2226 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227}
2228
2229/*
2230 * Local variables:
2231 * c-basic-offset: 8
2232 * End:
2233 */