blob: bd8ed01cb0ea273c1551f58feab71e0913ff0da9 [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
Trond Myklebust4ce79712005-06-22 17:16:21 +000054#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include "callback.h"
56#include "delegation.h"
David Howells24c8dbb2006-08-22 20:06:10 -040057#include "internal.h"
Andy Adamson974cec82010-10-20 00:18:02 -040058#include "pnfs.h"
Stanislav Kinsburskybbe0a3a2012-08-20 18:00:36 +040059#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Chuck Levere3c0fb72012-05-21 22:45:24 -040061#define NFSDBG_FACILITY NFSDBG_STATE
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define OPENOWNER_POOL_SIZE 8
64
Trond Myklebust4ce79712005-06-22 17:16:21 +000065const nfs4_stateid zero_stateid;
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static LIST_HEAD(nfs4_clientid_list);
68
Andy Adamson591d71c2009-04-01 09:22:47 -040069int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Trond Myklebustfd954ae2011-04-24 14:28:18 -040071 struct nfs4_setclientid_res clid = {
72 .clientid = clp->cl_clientid,
73 .confirm = clp->cl_confirm,
74 };
Chuck Leverf738f512009-03-18 20:48:06 -040075 unsigned short port;
76 int status;
Stanislav Kinsburskybbe0a3a2012-08-20 18:00:36 +040077 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Chuck Leverf738f512009-03-18 20:48:06 -040078
Trond Myklebustfd954ae2011-04-24 14:28:18 -040079 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
80 goto do_confirm;
Stanislav Kinsburskybbe0a3a2012-08-20 18:00:36 +040081 port = nn->nfs_callback_tcpport;
Chuck Leverf738f512009-03-18 20:48:06 -040082 if (clp->cl_addr.ss_family == AF_INET6)
Stanislav Kinsbursky29dcc162012-08-20 18:00:41 +040083 port = nn->nfs_callback_tcpport6;
Chuck Leverf738f512009-03-18 20:48:06 -040084
Trond Myklebustbb8b27e2010-04-16 16:43:06 -040085 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
86 if (status != 0)
87 goto out;
Trond Myklebustfd954ae2011-04-24 14:28:18 -040088 clp->cl_clientid = clid.clientid;
89 clp->cl_confirm = clid.confirm;
90 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
91do_confirm:
Trond Myklebustbb8b27e2010-04-16 16:43:06 -040092 status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
93 if (status != 0)
94 goto out;
Trond Myklebustfd954ae2011-04-24 14:28:18 -040095 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
Trond Myklebustbb8b27e2010-04-16 16:43:06 -040096 nfs4_schedule_state_renewal(clp);
97out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return status;
99}
100
Andy Adamsona7b72102009-04-01 09:22:46 -0400101struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400102{
103 struct rpc_cred *cred = NULL;
104
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400105 if (clp->cl_machine_cred != NULL)
106 cred = get_rpccred(clp->cl_machine_cred);
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400107 return cred;
108}
109
110static void nfs4_clear_machine_cred(struct nfs_client *clp)
111{
112 struct rpc_cred *cred;
113
114 spin_lock(&clp->cl_lock);
115 cred = clp->cl_machine_cred;
116 clp->cl_machine_cred = NULL;
117 spin_unlock(&clp->cl_lock);
118 if (cred != NULL)
119 put_rpccred(cred);
120}
121
Chuck Lever24d292b2010-12-24 01:32:43 +0000122static struct rpc_cred *
123nfs4_get_renew_cred_server_locked(struct nfs_server *server)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100124{
Chuck Lever24d292b2010-12-24 01:32:43 +0000125 struct rpc_cred *cred = NULL;
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100126 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400127 struct rb_node *pos;
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100128
Chuck Lever24d292b2010-12-24 01:32:43 +0000129 for (pos = rb_first(&server->state_owners);
130 pos != NULL;
131 pos = rb_next(pos)) {
132 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100133 if (list_empty(&sp->so_states))
134 continue;
135 cred = get_rpccred(sp->so_cred);
136 break;
137 }
138 return cred;
139}
140
Chuck Lever24d292b2010-12-24 01:32:43 +0000141/**
142 * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
143 * @clp: client state handle
144 *
145 * Returns an rpc_cred with reference count bumped, or NULL.
146 * Caller must hold clp->cl_lock.
147 */
148struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
149{
150 struct rpc_cred *cred = NULL;
151 struct nfs_server *server;
152
Sachin Prabhue49a29b2012-03-16 19:25:52 +0000153 /* Use machine credentials if available */
154 cred = nfs4_get_machine_cred_locked(clp);
155 if (cred != NULL)
156 goto out;
157
Chuck Lever24d292b2010-12-24 01:32:43 +0000158 rcu_read_lock();
159 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
160 cred = nfs4_get_renew_cred_server_locked(server);
161 if (cred != NULL)
162 break;
163 }
164 rcu_read_unlock();
Sachin Prabhue49a29b2012-03-16 19:25:52 +0000165
166out:
Chuck Lever24d292b2010-12-24 01:32:43 +0000167 return cred;
168}
169
Andy Adamsonb4b82602009-04-01 09:22:49 -0400170#if defined(CONFIG_NFS_V4_1)
171
Ricardo Labiaga9430fb62009-12-06 12:23:46 -0500172static int nfs41_setup_state_renewal(struct nfs_client *clp)
173{
174 int status;
175 struct nfs_fsinfo fsinfo;
176
Andy Adamsond6fb79d2011-03-01 01:34:11 +0000177 if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
178 nfs4_schedule_state_renewal(clp);
179 return 0;
180 }
181
Ricardo Labiaga9430fb62009-12-06 12:23:46 -0500182 status = nfs4_proc_get_lease_time(clp, &fsinfo);
183 if (status == 0) {
184 /* Update lease time and schedule renewal */
185 spin_lock(&clp->cl_lock);
186 clp->cl_lease_time = fsinfo.lease_time * HZ;
187 clp->cl_last_renewal = jiffies;
188 spin_unlock(&clp->cl_lock);
189
190 nfs4_schedule_state_renewal(clp);
191 }
192
193 return status;
194}
195
Andy Adamson42acd022011-01-06 02:04:34 +0000196/*
197 * Back channel returns NFS4ERR_DELAY for new requests when
198 * NFS4_SESSION_DRAINING is set so there is no work to be done when draining
199 * is ended.
200 */
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800201static void nfs4_end_drain_session(struct nfs_client *clp)
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500202{
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800203 struct nfs4_session *ses = clp->cl_session;
Trond Myklebust961a8282012-01-17 22:57:37 -0500204 struct nfs4_slot_table *tbl;
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800205 int max_slots;
206
Trond Myklebusta2118c32010-06-16 09:52:26 -0400207 if (ses == NULL)
208 return;
Trond Myklebust961a8282012-01-17 22:57:37 -0500209 tbl = &ses->fc_slot_table;
Trond Myklebusta2118c32010-06-16 09:52:26 -0400210 if (test_and_clear_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500211 spin_lock(&tbl->slot_tbl_lock);
212 max_slots = tbl->max_slots;
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800213 while (max_slots--) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500214 if (rpc_wake_up_first(&tbl->slot_tbl_waitq,
215 nfs4_set_task_privileged,
216 NULL) == NULL)
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800217 break;
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800218 }
Trond Myklebust961a8282012-01-17 22:57:37 -0500219 spin_unlock(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800220 }
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500221}
222
Andy Adamson42acd022011-01-06 02:04:34 +0000223static int nfs4_wait_on_slot_tbl(struct nfs4_slot_table *tbl)
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500224{
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500225 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonb6bf6e72012-02-17 13:05:23 -0500226 if (tbl->highest_used_slotid != NFS4_NO_SLOT) {
Andy Adamson42acd022011-01-06 02:04:34 +0000227 INIT_COMPLETION(tbl->complete);
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500228 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson42acd022011-01-06 02:04:34 +0000229 return wait_for_completion_interruptible(&tbl->complete);
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500230 }
231 spin_unlock(&tbl->slot_tbl_lock);
232 return 0;
233}
234
Andy Adamson42acd022011-01-06 02:04:34 +0000235static int nfs4_begin_drain_session(struct nfs_client *clp)
236{
237 struct nfs4_session *ses = clp->cl_session;
238 int ret = 0;
239
240 set_bit(NFS4_SESSION_DRAINING, &ses->session_state);
241 /* back channel */
242 ret = nfs4_wait_on_slot_tbl(&ses->bc_slot_table);
243 if (ret)
244 return ret;
245 /* fore channel */
246 return nfs4_wait_on_slot_tbl(&ses->fc_slot_table);
247}
248
Trond Myklebustbda197f2012-06-05 10:22:14 -0400249static void nfs41_finish_session_reset(struct nfs_client *clp)
250{
251 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
252 clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
253 /* create_session negotiated new slot table */
254 clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
255 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
256 nfs41_setup_state_renewal(clp);
257}
258
Andy Adamson4d643d12009-12-04 15:52:24 -0500259int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
260{
261 int status;
262
Trond Myklebustfd954ae2011-04-24 14:28:18 -0400263 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
264 goto do_confirm;
Trond Myklebust38045412009-12-15 17:36:57 -0500265 nfs4_begin_drain_session(clp);
Andy Adamson4d643d12009-12-04 15:52:24 -0500266 status = nfs4_proc_exchange_id(clp, cred);
Ricardo Labiaga9430fb62009-12-06 12:23:46 -0500267 if (status != 0)
268 goto out;
Trond Myklebustfd954ae2011-04-24 14:28:18 -0400269 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
270do_confirm:
Trond Myklebust848f5bd2012-05-25 17:51:23 -0400271 status = nfs4_proc_create_session(clp, cred);
Ricardo Labiaga9430fb62009-12-06 12:23:46 -0500272 if (status != 0)
273 goto out;
Trond Myklebustbda197f2012-06-05 10:22:14 -0400274 nfs41_finish_session_reset(clp);
Ricardo Labiaga9430fb62009-12-06 12:23:46 -0500275 nfs_mark_client_ready(clp, NFS_CS_READY);
276out:
Andy Adamson4d643d12009-12-04 15:52:24 -0500277 return status;
278}
279
Andy Adamsonb4b82602009-04-01 09:22:49 -0400280struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp)
281{
282 struct rpc_cred *cred;
283
284 spin_lock(&clp->cl_lock);
285 cred = nfs4_get_machine_cred_locked(clp);
286 spin_unlock(&clp->cl_lock);
287 return cred;
288}
289
290#endif /* CONFIG_NFS_V4_1 */
291
Chuck Lever24d292b2010-12-24 01:32:43 +0000292static struct rpc_cred *
293nfs4_get_setclientid_cred_server(struct nfs_server *server)
Trond Myklebust286d7d62006-01-03 09:55:26 +0100294{
Chuck Lever24d292b2010-12-24 01:32:43 +0000295 struct nfs_client *clp = server->nfs_client;
296 struct rpc_cred *cred = NULL;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100297 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400298 struct rb_node *pos;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100299
Trond Myklebust6dc9d572008-12-23 15:21:41 -0500300 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000301 pos = rb_first(&server->state_owners);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400302 if (pos != NULL) {
Chuck Lever24d292b2010-12-24 01:32:43 +0000303 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400304 cred = get_rpccred(sp->so_cred);
Trond Myklebust286d7d62006-01-03 09:55:26 +0100305 }
Trond Myklebust6dc9d572008-12-23 15:21:41 -0500306 spin_unlock(&clp->cl_lock);
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400307 return cred;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100308}
309
Chuck Lever24d292b2010-12-24 01:32:43 +0000310/**
311 * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation
312 * @clp: client state handle
313 *
314 * Returns an rpc_cred with reference count bumped, or NULL.
315 */
316struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
317{
318 struct nfs_server *server;
319 struct rpc_cred *cred;
320
321 spin_lock(&clp->cl_lock);
322 cred = nfs4_get_machine_cred_locked(clp);
323 spin_unlock(&clp->cl_lock);
324 if (cred != NULL)
325 goto out;
326
327 rcu_read_lock();
328 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
329 cred = nfs4_get_setclientid_cred_server(server);
330 if (cred != NULL)
331 break;
332 }
333 rcu_read_unlock();
334
335out:
336 return cred;
337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339static struct nfs4_state_owner *
Chuck Lever24d292b2010-12-24 01:32:43 +0000340nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Chuck Lever24d292b2010-12-24 01:32:43 +0000342 struct rb_node **p = &server->state_owners.rb_node,
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400343 *parent = NULL;
Chuck Lever414adf12011-12-06 16:13:39 -0500344 struct nfs4_state_owner *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400346 while (*p != NULL) {
347 parent = *p;
Chuck Lever24d292b2010-12-24 01:32:43 +0000348 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400349
350 if (cred < sp->so_cred)
351 p = &parent->rb_left;
352 else if (cred > sp->so_cred)
353 p = &parent->rb_right;
354 else {
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500355 if (!list_empty(&sp->so_lru))
356 list_del_init(&sp->so_lru);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400357 atomic_inc(&sp->so_count);
Chuck Lever414adf12011-12-06 16:13:39 -0500358 return sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
Chuck Lever414adf12011-12-06 16:13:39 -0500361 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400364static struct nfs4_state_owner *
Chuck Lever24d292b2010-12-24 01:32:43 +0000365nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400366{
Chuck Lever24d292b2010-12-24 01:32:43 +0000367 struct nfs_server *server = new->so_server;
368 struct rb_node **p = &server->state_owners.rb_node,
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400369 *parent = NULL;
370 struct nfs4_state_owner *sp;
Trond Myklebust9157c312012-01-17 22:04:24 -0500371 int err;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400372
373 while (*p != NULL) {
374 parent = *p;
Chuck Lever24d292b2010-12-24 01:32:43 +0000375 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400376
377 if (new->so_cred < sp->so_cred)
378 p = &parent->rb_left;
379 else if (new->so_cred > sp->so_cred)
380 p = &parent->rb_right;
381 else {
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500382 if (!list_empty(&sp->so_lru))
383 list_del_init(&sp->so_lru);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400384 atomic_inc(&sp->so_count);
385 return sp;
386 }
387 }
Trond Myklebust48c22eb2012-01-17 22:04:25 -0500388 err = ida_get_new(&server->openowner_id, &new->so_seqid.owner_id);
Trond Myklebust9157c312012-01-17 22:04:24 -0500389 if (err)
390 return ERR_PTR(err);
Chuck Lever24d292b2010-12-24 01:32:43 +0000391 rb_link_node(&new->so_server_node, parent, p);
392 rb_insert_color(&new->so_server_node, &server->state_owners);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400393 return new;
394}
395
396static void
Chuck Lever24d292b2010-12-24 01:32:43 +0000397nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400398{
Chuck Lever24d292b2010-12-24 01:32:43 +0000399 struct nfs_server *server = sp->so_server;
400
401 if (!RB_EMPTY_NODE(&sp->so_server_node))
402 rb_erase(&sp->so_server_node, &server->state_owners);
Trond Myklebust48c22eb2012-01-17 22:04:25 -0500403 ida_remove(&server->openowner_id, sp->so_seqid.owner_id);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400404}
405
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500406static void
407nfs4_init_seqid_counter(struct nfs_seqid_counter *sc)
408{
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400409 sc->create_time = ktime_get();
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500410 sc->flags = 0;
411 sc->counter = 0;
412 spin_lock_init(&sc->lock);
413 INIT_LIST_HEAD(&sc->list);
414 rpc_init_wait_queue(&sc->wait, "Seqid_waitqueue");
415}
416
417static void
418nfs4_destroy_seqid_counter(struct nfs_seqid_counter *sc)
419{
420 rpc_destroy_wait_queue(&sc->wait);
421}
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423/*
424 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
425 * create a new state_owner.
426 *
427 */
428static struct nfs4_state_owner *
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500429nfs4_alloc_state_owner(struct nfs_server *server,
430 struct rpc_cred *cred,
431 gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 struct nfs4_state_owner *sp;
434
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500435 sp = kzalloc(sizeof(*sp), gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (!sp)
437 return NULL;
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500438 sp->so_server = server;
439 sp->so_cred = get_rpccred(cred);
Trond Myklebustec073422005-10-20 14:22:47 -0700440 spin_lock_init(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 INIT_LIST_HEAD(&sp->so_states);
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500442 nfs4_init_seqid_counter(&sp->so_seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 atomic_set(&sp->so_count, 1);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500444 INIT_LIST_HEAD(&sp->so_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return sp;
446}
447
Adrian Bunk1d2e88e2008-05-02 13:42:45 -0700448static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449nfs4_drop_state_owner(struct nfs4_state_owner *sp)
450{
Trond Myklebustc77365c2012-04-21 12:31:05 -0400451 struct rb_node *rb_node = &sp->so_server_node;
452
453 if (!RB_EMPTY_NODE(rb_node)) {
Chuck Lever24d292b2010-12-24 01:32:43 +0000454 struct nfs_server *server = sp->so_server;
455 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400456
457 spin_lock(&clp->cl_lock);
Trond Myklebustc77365c2012-04-21 12:31:05 -0400458 if (!RB_EMPTY_NODE(rb_node)) {
459 rb_erase(rb_node, &server->state_owners);
460 RB_CLEAR_NODE(rb_node);
461 }
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400462 spin_unlock(&clp->cl_lock);
463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500466static void nfs4_free_state_owner(struct nfs4_state_owner *sp)
467{
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500468 nfs4_destroy_seqid_counter(&sp->so_seqid);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500469 put_rpccred(sp->so_cred);
470 kfree(sp);
471}
472
473static void nfs4_gc_state_owners(struct nfs_server *server)
474{
475 struct nfs_client *clp = server->nfs_client;
476 struct nfs4_state_owner *sp, *tmp;
477 unsigned long time_min, time_max;
478 LIST_HEAD(doomed);
479
480 spin_lock(&clp->cl_lock);
481 time_max = jiffies;
482 time_min = (long)time_max - (long)clp->cl_lease_time;
483 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
484 /* NB: LRU is sorted so that oldest is at the head */
485 if (time_in_range(sp->so_expires, time_min, time_max))
486 break;
487 list_move(&sp->so_lru, &doomed);
488 nfs4_remove_state_owner_locked(sp);
489 }
490 spin_unlock(&clp->cl_lock);
491
492 list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
493 list_del(&sp->so_lru);
494 nfs4_free_state_owner(sp);
495 }
496}
497
Chuck Lever24d292b2010-12-24 01:32:43 +0000498/**
499 * nfs4_get_state_owner - Look up a state owner given a credential
500 * @server: nfs_server to search
501 * @cred: RPC credential to match
502 *
503 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
504 */
505struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500506 struct rpc_cred *cred,
507 gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
David Howells7539bba2006-08-22 20:06:09 -0400509 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 struct nfs4_state_owner *sp, *new;
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000513 sp = nfs4_find_state_owner_locked(server, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 spin_unlock(&clp->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (sp != NULL)
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500516 goto out;
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500517 new = nfs4_alloc_state_owner(server, cred, gfp_flags);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400518 if (new == NULL)
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500519 goto out;
Trond Myklebust9157c312012-01-17 22:04:24 -0500520 do {
521 if (ida_pre_get(&server->openowner_id, gfp_flags) == 0)
522 break;
523 spin_lock(&clp->cl_lock);
524 sp = nfs4_insert_state_owner_locked(new);
525 spin_unlock(&clp->cl_lock);
526 } while (sp == ERR_PTR(-EAGAIN));
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500527 if (sp != new)
528 nfs4_free_state_owner(new);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500529out:
530 nfs4_gc_state_owners(server);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400531 return sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Chuck Lever24d292b2010-12-24 01:32:43 +0000534/**
535 * nfs4_put_state_owner - Release a nfs4_state_owner
536 * @sp: state owner data to release
Trond Myklebust7bf97bc2012-04-21 12:36:19 -0400537 *
538 * Note that we keep released state owners on an LRU
539 * list.
540 * This caches valid state owners so that they can be
541 * reused, to avoid the OPEN_CONFIRM on minor version 0.
542 * It also pins the uniquifier of dropped state owners for
543 * a while, to ensure that those state owner names are
544 * never reused.
Chuck Lever24d292b2010-12-24 01:32:43 +0000545 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546void nfs4_put_state_owner(struct nfs4_state_owner *sp)
547{
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500548 struct nfs_server *server = sp->so_server;
549 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
552 return;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500553
Trond Myklebust7bf97bc2012-04-21 12:36:19 -0400554 sp->so_expires = jiffies;
555 list_add_tail(&sp->so_lru, &server->state_owners_lru);
556 spin_unlock(&clp->cl_lock);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500557}
558
559/**
560 * nfs4_purge_state_owners - Release all cached state owners
561 * @server: nfs_server with cached state owners to release
562 *
563 * Called at umount time. Remaining state owners will be on
564 * the LRU with ref count of zero.
565 */
566void nfs4_purge_state_owners(struct nfs_server *server)
567{
568 struct nfs_client *clp = server->nfs_client;
569 struct nfs4_state_owner *sp, *tmp;
570 LIST_HEAD(doomed);
571
572 spin_lock(&clp->cl_lock);
573 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
574 list_move(&sp->so_lru, &doomed);
575 nfs4_remove_state_owner_locked(sp);
576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 spin_unlock(&clp->cl_lock);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500578
579 list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
580 list_del(&sp->so_lru);
581 nfs4_free_state_owner(sp);
582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
585static struct nfs4_state *
586nfs4_alloc_open_state(void)
587{
588 struct nfs4_state *state;
589
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400590 state = kzalloc(sizeof(*state), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (!state)
592 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 atomic_set(&state->count, 1);
594 INIT_LIST_HEAD(&state->lock_states);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000595 spin_lock_init(&state->state_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400596 seqlock_init(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return state;
598}
599
Trond Myklebust4cecb762005-11-04 15:32:58 -0500600void
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500601nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
Trond Myklebust4cecb762005-11-04 15:32:58 -0500602{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500603 if (state->state == fmode)
Trond Myklebust4cecb762005-11-04 15:32:58 -0500604 return;
605 /* NB! List reordering - see the reclaim code for why. */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500606 if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
607 if (fmode & FMODE_WRITE)
Trond Myklebust4cecb762005-11-04 15:32:58 -0500608 list_move(&state->open_states, &state->owner->so_states);
609 else
610 list_move_tail(&state->open_states, &state->owner->so_states);
611 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500612 state->state = fmode;
Trond Myklebust4cecb762005-11-04 15:32:58 -0500613}
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615static struct nfs4_state *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616__nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
617{
618 struct nfs_inode *nfsi = NFS_I(inode);
619 struct nfs4_state *state;
620
621 list_for_each_entry(state, &nfsi->open_states, inode_states) {
Trond Myklebust1c816ef2007-07-03 14:41:19 -0400622 if (state->owner != owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 continue;
Trond Myklebust1c816ef2007-07-03 14:41:19 -0400624 if (atomic_inc_not_zero(&state->count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627 return NULL;
628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630static void
631nfs4_free_open_state(struct nfs4_state *state)
632{
633 kfree(state);
634}
635
636struct nfs4_state *
637nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
638{
639 struct nfs4_state *state, *new;
640 struct nfs_inode *nfsi = NFS_I(inode);
641
642 spin_lock(&inode->i_lock);
643 state = __nfs4_find_state_byowner(inode, owner);
644 spin_unlock(&inode->i_lock);
645 if (state)
646 goto out;
647 new = nfs4_alloc_open_state();
Trond Myklebustec073422005-10-20 14:22:47 -0700648 spin_lock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 spin_lock(&inode->i_lock);
650 state = __nfs4_find_state_byowner(inode, owner);
651 if (state == NULL && new != NULL) {
652 state = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 state->owner = owner;
654 atomic_inc(&owner->so_count);
655 list_add(&state->inode_states, &nfsi->open_states);
Dave Chinner0444d762011-03-29 18:08:50 +1100656 ihold(inode);
657 state->inode = inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700659 /* Note: The reclaim code dictates that we add stateless
660 * and read-only stateids to the end of the list */
661 list_add_tail(&state->open_states, &owner->so_states);
662 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 } else {
664 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700665 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (new)
667 nfs4_free_open_state(new);
668 }
669out:
670 return state;
671}
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673void nfs4_put_open_state(struct nfs4_state *state)
674{
675 struct inode *inode = state->inode;
676 struct nfs4_state_owner *owner = state->owner;
677
Trond Myklebustec073422005-10-20 14:22:47 -0700678 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return;
Trond Myklebustec073422005-10-20 14:22:47 -0700680 spin_lock(&inode->i_lock);
Trond Myklebustba6830312007-07-27 10:23:05 -0400681 list_del(&state->inode_states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 list_del(&state->open_states);
Trond Myklebustec073422005-10-20 14:22:47 -0700683 spin_unlock(&inode->i_lock);
684 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 nfs4_free_open_state(state);
687 nfs4_put_state_owner(owner);
688}
689
690/*
Trond Myklebust83c9d412005-10-18 14:20:13 -0700691 * Close the current file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 */
Al Viro643168c2011-06-22 18:20:23 -0400693static void __nfs4_close(struct nfs4_state *state,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400694 fmode_t fmode, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 struct nfs4_state_owner *owner = state->owner;
Trond Myklebust003707c2007-07-05 18:07:55 -0400697 int call_close = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500698 fmode_t newstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 atomic_inc(&owner->so_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 /* Protect against nfs4_find_state() */
Trond Myklebustec073422005-10-20 14:22:47 -0700702 spin_lock(&owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500703 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100704 case FMODE_READ:
705 state->n_rdonly--;
706 break;
707 case FMODE_WRITE:
708 state->n_wronly--;
709 break;
710 case FMODE_READ|FMODE_WRITE:
711 state->n_rdwr--;
712 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400713 newstate = FMODE_READ|FMODE_WRITE;
Trond Myklebuste7616922006-01-03 09:55:13 +0100714 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400715 if (state->n_rdonly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100716 newstate &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -0400717 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
718 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
719 }
720 if (state->n_wronly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100721 newstate &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -0400722 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
723 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
724 }
725 if (newstate == 0)
726 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebuste7616922006-01-03 09:55:13 +0100727 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400728 nfs4_state_set_mode_locked(state, newstate);
Trond Myklebustec073422005-10-20 14:22:47 -0700729 spin_unlock(&owner->so_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500730
Trond Myklebust003707c2007-07-05 18:07:55 -0400731 if (!call_close) {
Trond Myklebustb39e6252007-06-11 23:05:07 -0400732 nfs4_put_open_state(state);
733 nfs4_put_state_owner(owner);
Trond Myklebust1f7977c2012-09-20 20:31:51 -0400734 } else
735 nfs4_do_close(state, gfp_mask, wait);
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400736}
737
Al Viro643168c2011-06-22 18:20:23 -0400738void nfs4_close_state(struct nfs4_state *state, fmode_t fmode)
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400739{
Al Viro643168c2011-06-22 18:20:23 -0400740 __nfs4_close(state, fmode, GFP_NOFS, 0);
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400741}
742
Al Viro643168c2011-06-22 18:20:23 -0400743void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode)
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400744{
Al Viro643168c2011-06-22 18:20:23 -0400745 __nfs4_close(state, fmode, GFP_KERNEL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
748/*
749 * Search the state->lock_states for an existing lock_owner
750 * that is compatible with current->files
751 */
752static struct nfs4_lock_state *
Trond Myklebust77041ed2010-07-01 12:49:11 -0400753__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 -0700754{
755 struct nfs4_lock_state *pos;
756 list_for_each_entry(pos, &state->lock_states, ls_locks) {
Trond Myklebust77041ed2010-07-01 12:49:11 -0400757 if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 continue;
Trond Myklebust77041ed2010-07-01 12:49:11 -0400759 switch (pos->ls_owner.lo_type) {
760 case NFS4_POSIX_LOCK_TYPE:
761 if (pos->ls_owner.lo_u.posix_owner != fl_owner)
762 continue;
763 break;
764 case NFS4_FLOCK_LOCK_TYPE:
765 if (pos->ls_owner.lo_u.flock_owner != fl_pid)
766 continue;
767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 atomic_inc(&pos->ls_count);
769 return pos;
770 }
771 return NULL;
772}
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774/*
775 * Return a compatible lock_state. If no initialized lock_state structure
776 * exists, return an uninitialized one.
777 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 */
Trond Myklebust77041ed2010-07-01 12:49:11 -0400779static 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 -0700780{
781 struct nfs4_lock_state *lsp;
Chuck Lever24d292b2010-12-24 01:32:43 +0000782 struct nfs_server *server = state->owner->so_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400784 lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (lsp == NULL)
786 return NULL;
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500787 nfs4_init_seqid_counter(&lsp->ls_seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 atomic_set(&lsp->ls_count, 1);
Trond Myklebustb64aec82009-07-21 16:47:46 -0400789 lsp->ls_state = state;
Trond Myklebust77041ed2010-07-01 12:49:11 -0400790 lsp->ls_owner.lo_type = type;
791 switch (lsp->ls_owner.lo_type) {
792 case NFS4_FLOCK_LOCK_TYPE:
793 lsp->ls_owner.lo_u.flock_owner = fl_pid;
794 break;
795 case NFS4_POSIX_LOCK_TYPE:
796 lsp->ls_owner.lo_u.posix_owner = fl_owner;
797 break;
798 default:
Trond Myklebustd2d7ce22012-01-17 22:04:25 -0500799 goto out_free;
Trond Myklebust77041ed2010-07-01 12:49:11 -0400800 }
Trond Myklebust48c22eb2012-01-17 22:04:25 -0500801 lsp->ls_seqid.owner_id = ida_simple_get(&server->lockowner_id, 0, 0, GFP_NOFS);
802 if (lsp->ls_seqid.owner_id < 0)
Trond Myklebustd2d7ce22012-01-17 22:04:25 -0500803 goto out_free;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000804 INIT_LIST_HEAD(&lsp->ls_locks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return lsp;
Trond Myklebustd2d7ce22012-01-17 22:04:25 -0500806out_free:
807 kfree(lsp);
808 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
Trond Myklebust5ae67c42012-03-19 16:17:18 -0400811void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400812{
Trond Myklebust48c22eb2012-01-17 22:04:25 -0500813 ida_simple_remove(&server->lockowner_id, lsp->ls_seqid.owner_id);
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500814 nfs4_destroy_seqid_counter(&lsp->ls_seqid);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400815 kfree(lsp);
816}
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818/*
819 * Return a compatible lock_state. If no initialized lock_state structure
820 * exists, return an uninitialized one.
821 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 */
Trond Myklebust77041ed2010-07-01 12:49:11 -0400823static 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 -0700824{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000825 struct nfs4_lock_state *lsp, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000827 for(;;) {
828 spin_lock(&state->state_lock);
Trond Myklebust77041ed2010-07-01 12:49:11 -0400829 lsp = __nfs4_find_lock_state(state, owner, pid, type);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000830 if (lsp != NULL)
831 break;
832 if (new != NULL) {
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000833 list_add(&new->ls_locks, &state->lock_states);
834 set_bit(LK_STATE_IN_USE, &state->flags);
835 lsp = new;
836 new = NULL;
837 break;
838 }
839 spin_unlock(&state->state_lock);
Trond Myklebust77041ed2010-07-01 12:49:11 -0400840 new = nfs4_alloc_lock_state(state, owner, pid, type);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000841 if (new == NULL)
842 return NULL;
843 }
844 spin_unlock(&state->state_lock);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400845 if (new != NULL)
Trond Myklebust5ae67c42012-03-19 16:17:18 -0400846 nfs4_free_lock_state(state->owner->so_server, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return lsp;
848}
849
850/*
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000851 * Release reference to lock_state, and free it if we see that
852 * it is no longer in use
853 */
Trond Myklebustfaf5f492005-10-18 14:20:15 -0700854void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000855{
856 struct nfs4_state *state;
857
858 if (lsp == NULL)
859 return;
860 state = lsp->ls_state;
861 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
862 return;
863 list_del(&lsp->ls_locks);
864 if (list_empty(&state->lock_states))
865 clear_bit(LK_STATE_IN_USE, &state->flags);
866 spin_unlock(&state->state_lock);
Trond Myklebust795a88c2012-09-10 13:26:49 -0400867 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustcf470c32012-03-07 13:49:12 -0500868 if (nfs4_release_lockowner(lsp) == 0)
869 return;
870 }
Trond Myklebust5ae67c42012-03-19 16:17:18 -0400871 nfs4_free_lock_state(lsp->ls_state->owner->so_server, lsp);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000872}
873
874static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
875{
876 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
877
878 dst->fl_u.nfs4_fl.owner = lsp;
879 atomic_inc(&lsp->ls_count);
880}
881
882static void nfs4_fl_release_lock(struct file_lock *fl)
883{
884 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
885}
886
Alexey Dobriyan6aed6282009-09-21 17:01:11 -0700887static const struct file_lock_operations nfs4_fl_lock_ops = {
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000888 .fl_copy_lock = nfs4_fl_copy_lock,
889 .fl_release_private = nfs4_fl_release_lock,
890};
891
892int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
893{
894 struct nfs4_lock_state *lsp;
895
896 if (fl->fl_ops != NULL)
897 return 0;
Trond Myklebust77041ed2010-07-01 12:49:11 -0400898 if (fl->fl_flags & FL_POSIX)
899 lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
900 else if (fl->fl_flags & FL_FLOCK)
Trond Myklebust17280172012-03-11 13:11:00 -0400901 lsp = nfs4_get_lock_state(state, NULL, fl->fl_pid,
902 NFS4_FLOCK_LOCK_TYPE);
Trond Myklebust77041ed2010-07-01 12:49:11 -0400903 else
904 return -EINVAL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000905 if (lsp == NULL)
906 return -ENOMEM;
907 fl->fl_u.nfs4_fl.owner = lsp;
908 fl->fl_ops = &nfs4_fl_lock_ops;
909 return 0;
910}
911
Trond Myklebust4fc87962012-03-08 17:42:01 -0500912static bool nfs4_copy_lock_stateid(nfs4_stateid *dst, struct nfs4_state *state,
Trond Myklebust2a369152012-08-13 18:54:45 -0400913 const struct nfs_lockowner *lockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000915 struct nfs4_lock_state *lsp;
Trond Myklebust2a369152012-08-13 18:54:45 -0400916 fl_owner_t fl_owner;
917 pid_t fl_pid;
Trond Myklebust4fc87962012-03-08 17:42:01 -0500918 bool ret = false;
919
Trond Myklebust2a369152012-08-13 18:54:45 -0400920
921 if (lockowner == NULL)
922 goto out;
923
Trond Myklebust4fc87962012-03-08 17:42:01 -0500924 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
925 goto out;
926
Trond Myklebust2a369152012-08-13 18:54:45 -0400927 fl_owner = lockowner->l_owner;
928 fl_pid = lockowner->l_pid;
Trond Myklebust4fc87962012-03-08 17:42:01 -0500929 spin_lock(&state->state_lock);
930 lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
Trond Myklebust795a88c2012-09-10 13:26:49 -0400931 if (lsp != NULL && test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) {
Trond Myklebust4fc87962012-03-08 17:42:01 -0500932 nfs4_stateid_copy(dst, &lsp->ls_stateid);
933 ret = true;
934 }
935 spin_unlock(&state->state_lock);
936 nfs4_put_lock_state(lsp);
937out:
938 return ret;
939}
940
941static void nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
942{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400943 int seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400945 do {
946 seq = read_seqbegin(&state->seqlock);
Trond Myklebustf597c532012-03-04 18:13:56 -0500947 nfs4_stateid_copy(dst, &state->stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400948 } while (read_seqretry(&state->seqlock, seq));
Trond Myklebust4fc87962012-03-08 17:42:01 -0500949}
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000950
Trond Myklebust4fc87962012-03-08 17:42:01 -0500951/*
952 * Byte-range lock aware utility to initialize the stateid of read/write
953 * requests.
954 */
955void nfs4_select_rw_stateid(nfs4_stateid *dst, struct nfs4_state *state,
Trond Myklebust2a369152012-08-13 18:54:45 -0400956 fmode_t fmode, const struct nfs_lockowner *lockowner)
Trond Myklebust4fc87962012-03-08 17:42:01 -0500957{
958 if (nfs4_copy_delegation_stateid(dst, state->inode, fmode))
959 return;
Trond Myklebust2a369152012-08-13 18:54:45 -0400960 if (nfs4_copy_lock_stateid(dst, state, lockowner))
Trond Myklebust4fc87962012-03-08 17:42:01 -0500961 return;
962 nfs4_copy_open_stateid(dst, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400965struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700967 struct nfs_seqid *new;
968
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400969 new = kmalloc(sizeof(*new), gfp_mask);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700970 if (new != NULL) {
971 new->sequence = counter;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500972 INIT_LIST_HEAD(&new->list);
Trond Myklebust4601df22012-01-20 18:47:05 -0500973 new->task = NULL;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700974 }
975 return new;
976}
977
Trond Myklebust72211db2009-12-15 14:47:36 -0500978void nfs_release_seqid(struct nfs_seqid *seqid)
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700979{
Trond Myklebust4601df22012-01-20 18:47:05 -0500980 struct nfs_seqid_counter *sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700981
Trond Myklebust4601df22012-01-20 18:47:05 -0500982 if (list_empty(&seqid->list))
983 return;
984 sequence = seqid->sequence;
985 spin_lock(&sequence->lock);
986 list_del_init(&seqid->list);
987 if (!list_empty(&sequence->list)) {
988 struct nfs_seqid *next;
989
990 next = list_first_entry(&sequence->list,
991 struct nfs_seqid, list);
992 rpc_wake_up_queued_task(&sequence->wait, next->task);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500993 }
Trond Myklebust4601df22012-01-20 18:47:05 -0500994 spin_unlock(&sequence->lock);
Trond Myklebust72211db2009-12-15 14:47:36 -0500995}
996
997void nfs_free_seqid(struct nfs_seqid *seqid)
998{
999 nfs_release_seqid(seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001000 kfree(seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001004 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
1005 * failed with a seqid incrementing error -
1006 * see comments nfs_fs.h:seqid_mutating_error()
1007 */
Trond Myklebust88d90932007-07-02 14:03:03 -04001008static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Trond Myklebust7ba127a2012-01-17 22:04:25 -05001010 BUG_ON(list_first_entry(&seqid->sequence->list, struct nfs_seqid, list) != seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001011 switch (status) {
1012 case 0:
1013 break;
1014 case -NFS4ERR_BAD_SEQID:
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001015 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
1016 return;
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001017 pr_warn_ratelimited("NFS: v4 server returned a bad"
Dan Muntz497799e2008-02-13 13:09:35 -08001018 " sequence-id error on an"
1019 " unconfirmed sequence %p!\n",
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001020 seqid->sequence);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001021 case -NFS4ERR_STALE_CLIENTID:
1022 case -NFS4ERR_STALE_STATEID:
1023 case -NFS4ERR_BAD_STATEID:
1024 case -NFS4ERR_BADXDR:
1025 case -NFS4ERR_RESOURCE:
1026 case -NFS4ERR_NOFILEHANDLE:
1027 /* Non-seqid mutating errors */
1028 return;
1029 };
1030 /*
1031 * Note: no locking needed as we are guaranteed to be first
1032 * on the sequence list
1033 */
1034 seqid->sequence->counter++;
1035}
1036
1037void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
1038{
Benny Halevy34dc1ad2009-04-01 09:22:52 -04001039 struct nfs4_state_owner *sp = container_of(seqid->sequence,
1040 struct nfs4_state_owner, so_seqid);
1041 struct nfs_server *server = sp->so_server;
1042
1043 if (status == -NFS4ERR_BAD_SEQID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 nfs4_drop_state_owner(sp);
Benny Halevy34dc1ad2009-04-01 09:22:52 -04001045 if (!nfs4_has_session(server->nfs_client))
1046 nfs_increment_seqid(status, seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001047}
1048
1049/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001050 * Increment the seqid if the LOCK/LOCKU succeeded, or
1051 * failed with a seqid incrementing error -
1052 * see comments nfs_fs.h:seqid_mutating_error()
1053 */
1054void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
1055{
Trond Myklebust88d90932007-07-02 14:03:03 -04001056 nfs_increment_seqid(status, seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001057}
1058
1059int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
1060{
Trond Myklebust7ba127a2012-01-17 22:04:25 -05001061 struct nfs_seqid_counter *sequence = seqid->sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001062 int status = 0;
1063
1064 spin_lock(&sequence->lock);
Trond Myklebust4601df22012-01-20 18:47:05 -05001065 seqid->task = task;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05001066 if (list_empty(&seqid->list))
1067 list_add_tail(&seqid->list, &sequence->list);
1068 if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
1069 goto unlock;
Trond Myklebust5d008372008-02-22 16:34:17 -05001070 rpc_sleep_on(&sequence->wait, task, NULL);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05001071 status = -EAGAIN;
1072unlock:
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001073 spin_unlock(&sequence->lock);
1074 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Trond Myklebuste005e802008-12-23 15:21:48 -05001077static int nfs4_run_state_manager(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Trond Myklebuste005e802008-12-23 15:21:48 -05001079static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
Trond Myklebust433fbe42006-01-03 09:55:22 +01001080{
1081 smp_mb__before_clear_bit();
Trond Myklebuste005e802008-12-23 15:21:48 -05001082 clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
Trond Myklebust433fbe42006-01-03 09:55:22 +01001083 smp_mb__after_clear_bit();
Trond Myklebuste005e802008-12-23 15:21:48 -05001084 wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
Trond Myklebust433fbe42006-01-03 09:55:22 +01001085 rpc_wake_up(&clp->cl_rpcwaitq);
1086}
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088/*
Trond Myklebuste005e802008-12-23 15:21:48 -05001089 * Schedule the nfs_client asynchronous state management routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 */
Trond Myklebustb0d3ded2008-12-23 15:21:50 -05001091void nfs4_schedule_state_manager(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Trond Myklebust5043e902006-01-03 09:55:23 +01001093 struct task_struct *task;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001094 char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Trond Myklebuste005e802008-12-23 15:21:48 -05001096 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1097 return;
Trond Myklebust5043e902006-01-03 09:55:23 +01001098 __module_get(THIS_MODULE);
1099 atomic_inc(&clp->cl_count);
Trond Myklebust2446ab62012-03-01 17:00:56 -05001100
1101 /* The rcu_read_lock() is not strictly necessary, as the state
1102 * manager is the only thread that ever changes the rpc_xprt
1103 * after it's initialized. At this point, we're single threaded. */
1104 rcu_read_lock();
1105 snprintf(buf, sizeof(buf), "%s-manager",
1106 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
1107 rcu_read_unlock();
1108 task = kthread_run(nfs4_run_state_manager, clp, buf);
1109 if (IS_ERR(task)) {
1110 printk(KERN_ERR "%s: kthread_run: %ld\n",
1111 __func__, PTR_ERR(task));
1112 nfs4_clear_state_manager_bit(clp);
1113 nfs_put_client(clp);
1114 module_put(THIS_MODULE);
1115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
1118/*
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001119 * Schedule a lease recovery attempt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001121void nfs4_schedule_lease_recovery(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 if (!clp)
1124 return;
Trond Myklebuste598d842008-12-23 15:21:42 -05001125 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1126 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001127 dprintk("%s: scheduling lease recovery for server %s\n", __func__,
1128 clp->cl_hostname);
Trond Myklebuste005e802008-12-23 15:21:48 -05001129 nfs4_schedule_state_manager(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
Andy Adamson9cb81962012-03-07 10:49:41 -05001131EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Trond Myklebustad1e3962012-03-10 11:23:15 -05001133/*
1134 * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
1135 * @clp: client to process
1136 *
1137 * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
1138 * resend of the SETCLIENTID and hence re-establish the
1139 * callback channel. Then return all existing delegations.
1140 */
1141static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
1142{
1143 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1144 nfs_expire_all_delegations(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001145 dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
1146 clp->cl_hostname);
Trond Myklebustad1e3962012-03-10 11:23:15 -05001147}
1148
Trond Myklebust042b60b2011-08-24 15:07:37 -04001149void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
1150{
Trond Myklebustad1e3962012-03-10 11:23:15 -05001151 nfs40_handle_cb_pathdown(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04001152 nfs4_schedule_state_manager(clp);
1153}
1154
Trond Myklebustf9feab12011-03-09 16:12:46 -05001155static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001156{
1157
1158 set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1159 /* Don't recover state that expired before the reboot */
1160 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1161 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1162 return 0;
1163 }
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001164 set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001165 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1166 return 1;
1167}
1168
Trond Myklebustf9feab12011-03-09 16:12:46 -05001169static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001170{
1171 set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1172 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001173 set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001174 set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1175 return 1;
1176}
1177
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001178void nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
1179{
1180 struct nfs_client *clp = server->nfs_client;
1181
1182 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001183 dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
1184 clp->cl_hostname);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001185 nfs4_schedule_state_manager(clp);
1186}
Andy Adamson9cb81962012-03-07 10:49:41 -05001187EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001188
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001189void nfs_inode_find_state_and_recover(struct inode *inode,
1190 const nfs4_stateid *stateid)
1191{
1192 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1193 struct nfs_inode *nfsi = NFS_I(inode);
1194 struct nfs_open_context *ctx;
1195 struct nfs4_state *state;
1196 bool found = false;
1197
1198 spin_lock(&inode->i_lock);
1199 list_for_each_entry(ctx, &nfsi->open_files, list) {
1200 state = ctx->state;
1201 if (state == NULL)
1202 continue;
1203 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
1204 continue;
Trond Myklebustf597c532012-03-04 18:13:56 -05001205 if (!nfs4_stateid_match(&state->stateid, stateid))
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001206 continue;
1207 nfs4_state_mark_reclaim_nograce(clp, state);
1208 found = true;
1209 }
1210 spin_unlock(&inode->i_lock);
1211 if (found)
1212 nfs4_schedule_state_manager(clp);
1213}
1214
1215
Trond Myklebust02860012008-12-23 15:21:40 -05001216static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
1218 struct inode *inode = state->inode;
Trond Myklebust19e03c52008-12-23 15:21:44 -05001219 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 struct file_lock *fl;
1221 int status = 0;
1222
Trond Myklebust3f09df72009-06-17 13:23:00 -07001223 if (inode->i_flock == NULL)
1224 return 0;
1225
1226 /* Guard against delegation returns and new lock/unlock calls */
Trond Myklebust19e03c52008-12-23 15:21:44 -05001227 down_write(&nfsi->rwsem);
Trond Myklebust3f09df72009-06-17 13:23:00 -07001228 /* Protect inode->i_flock using the BKL */
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001229 lock_flocks();
Harvey Harrison90dc7d22008-02-20 13:03:05 -08001230 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
Trond Myklebust43b2a332005-11-04 15:35:30 -05001231 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 continue;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001233 if (nfs_file_open_context(fl->fl_file)->state != state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 continue;
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001235 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 status = ops->recover_lock(state, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 switch (status) {
Trond Myklebust965b5d62009-06-17 13:22:59 -07001238 case 0:
1239 break;
1240 case -ESTALE:
1241 case -NFS4ERR_ADMIN_REVOKED:
1242 case -NFS4ERR_STALE_STATEID:
1243 case -NFS4ERR_BAD_STATEID:
1244 case -NFS4ERR_EXPIRED:
1245 case -NFS4ERR_NO_GRACE:
1246 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust9c4c7612009-12-03 15:58:56 -05001247 case -NFS4ERR_BADSESSION:
1248 case -NFS4ERR_BADSLOT:
1249 case -NFS4ERR_BAD_HIGH_SLOT:
1250 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001251 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05001253 printk(KERN_ERR "NFS: %s: unhandled error %d. "
1254 "Zeroing state\n", __func__, status);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001255 case -ENOMEM:
1256 case -NFS4ERR_DENIED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 case -NFS4ERR_RECLAIM_BAD:
1258 case -NFS4ERR_RECLAIM_CONFLICT:
Trond Myklebust43b2a332005-11-04 15:35:30 -05001259 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001260 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001262 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001264 unlock_flocks();
Trond Myklebust965b5d62009-06-17 13:22:59 -07001265out:
Trond Myklebust19e03c52008-12-23 15:21:44 -05001266 up_write(&nfsi->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return status;
1268}
1269
Trond Myklebust02860012008-12-23 15:21:40 -05001270static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
1272 struct nfs4_state *state;
1273 struct nfs4_lock_state *lock;
1274 int status = 0;
1275
1276 /* Note: we rely on the sp->so_states list being ordered
1277 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1278 * states first.
1279 * This is needed to ensure that the server won't give us any
1280 * read delegations that we have to return if, say, we are
1281 * recovering after a network partition or a reboot from a
1282 * server that doesn't support a grace period.
1283 */
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001284restart:
1285 spin_lock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 list_for_each_entry(state, &sp->so_states, open_states) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001287 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1288 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (state->state == 0)
1290 continue;
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001291 atomic_inc(&state->count);
1292 spin_unlock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 status = ops->recover_open(sp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (status >= 0) {
Trond Myklebust02860012008-12-23 15:21:40 -05001295 status = nfs4_reclaim_locks(state, ops);
1296 if (status >= 0) {
Trond Myklebust4b44b402011-12-09 16:31:52 -05001297 spin_lock(&state->state_lock);
Trond Myklebust02860012008-12-23 15:21:40 -05001298 list_for_each_entry(lock, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04001299 if (!test_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags))
William Dauchy96dcadc2012-03-14 12:32:04 +01001300 pr_warn_ratelimited("NFS: "
1301 "%s: Lock reclaim "
Weston Andros Adamsona0308892012-01-26 13:32:23 -05001302 "failed!\n", __func__);
Trond Myklebust02860012008-12-23 15:21:40 -05001303 }
Trond Myklebust4b44b402011-12-09 16:31:52 -05001304 spin_unlock(&state->state_lock);
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001305 nfs4_put_open_state(state);
1306 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309 switch (status) {
1310 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05001311 printk(KERN_ERR "NFS: %s: unhandled error %d. "
1312 "Zeroing state\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 case -ENOENT:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001314 case -ENOMEM:
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001315 case -ESTALE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 /*
1317 * Open state on this file cannot be recovered
1318 * All we can do is revert to using the zero stateid.
1319 */
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05001320 memset(&state->stateid, 0,
1321 sizeof(state->stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 /* Mark the file as being 'closed' */
1323 state->state = 0;
1324 break;
Trond Myklebust168667c2010-10-19 19:47:49 -04001325 case -EKEYEXPIRED:
1326 /*
1327 * User RPCSEC_GSS context has expired.
1328 * We cannot recover this stateid now, so
1329 * skip it and allow recovery thread to
1330 * proceed.
1331 */
1332 break;
Trond Myklebust965b5d62009-06-17 13:22:59 -07001333 case -NFS4ERR_ADMIN_REVOKED:
1334 case -NFS4ERR_STALE_STATEID:
1335 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001336 case -NFS4ERR_RECLAIM_BAD:
1337 case -NFS4ERR_RECLAIM_CONFLICT:
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001338 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001339 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 case -NFS4ERR_EXPIRED:
1341 case -NFS4ERR_NO_GRACE:
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001342 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust9c4c7612009-12-03 15:58:56 -05001344 case -NFS4ERR_BADSESSION:
1345 case -NFS4ERR_BADSLOT:
1346 case -NFS4ERR_BAD_HIGH_SLOT:
1347 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 goto out_err;
1349 }
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001350 nfs4_put_open_state(state);
1351 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001353 spin_unlock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return 0;
1355out_err:
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001356 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return status;
1358}
1359
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001360static void nfs4_clear_open_state(struct nfs4_state *state)
1361{
1362 struct nfs4_lock_state *lock;
1363
1364 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1365 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1366 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1367 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust4b44b402011-12-09 16:31:52 -05001368 spin_lock(&state->state_lock);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001369 list_for_each_entry(lock, &state->lock_states, ls_locks) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001370 lock->ls_seqid.flags = 0;
Trond Myklebust795a88c2012-09-10 13:26:49 -04001371 clear_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001372 }
Trond Myklebust4b44b402011-12-09 16:31:52 -05001373 spin_unlock(&state->state_lock);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001374}
1375
Chuck Lever24d292b2010-12-24 01:32:43 +00001376static void nfs4_reset_seqids(struct nfs_server *server,
1377 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001378{
Chuck Lever24d292b2010-12-24 01:32:43 +00001379 struct nfs_client *clp = server->nfs_client;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001380 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001381 struct rb_node *pos;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001382 struct nfs4_state *state;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001383
Chuck Lever24d292b2010-12-24 01:32:43 +00001384 spin_lock(&clp->cl_lock);
1385 for (pos = rb_first(&server->state_owners);
1386 pos != NULL;
1387 pos = rb_next(pos)) {
1388 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001389 sp->so_seqid.flags = 0;
Trond Myklebustec073422005-10-20 14:22:47 -07001390 spin_lock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001391 list_for_each_entry(state, &sp->so_states, open_states) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001392 if (mark_reclaim(clp, state))
1393 nfs4_clear_open_state(state);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001394 }
Trond Myklebustec073422005-10-20 14:22:47 -07001395 spin_unlock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001396 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001397 spin_unlock(&clp->cl_lock);
1398}
1399
1400static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1401 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1402{
1403 struct nfs_server *server;
1404
1405 rcu_read_lock();
1406 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1407 nfs4_reset_seqids(server, mark_reclaim);
1408 rcu_read_unlock();
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001409}
1410
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001411static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1412{
1413 /* Mark all delegations for reclaim */
1414 nfs_delegation_mark_reclaim(clp);
1415 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1416}
1417
Ricardo Labiagafce5c832009-12-05 16:08:41 -05001418static void nfs4_reclaim_complete(struct nfs_client *clp,
1419 const struct nfs4_state_recovery_ops *ops)
1420{
1421 /* Notify the server we're done reclaiming our state */
1422 if (ops->reclaim_complete)
1423 (void)ops->reclaim_complete(clp);
1424}
1425
Chuck Lever24d292b2010-12-24 01:32:43 +00001426static void nfs4_clear_reclaim_server(struct nfs_server *server)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001427{
Chuck Lever24d292b2010-12-24 01:32:43 +00001428 struct nfs_client *clp = server->nfs_client;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001429 struct nfs4_state_owner *sp;
1430 struct rb_node *pos;
1431 struct nfs4_state *state;
1432
Chuck Lever24d292b2010-12-24 01:32:43 +00001433 spin_lock(&clp->cl_lock);
1434 for (pos = rb_first(&server->state_owners);
1435 pos != NULL;
1436 pos = rb_next(pos)) {
1437 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001438 spin_lock(&sp->so_lock);
1439 list_for_each_entry(state, &sp->so_states, open_states) {
Chuck Lever24d292b2010-12-24 01:32:43 +00001440 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1441 &state->flags))
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001442 continue;
1443 nfs4_state_mark_reclaim_nograce(clp, state);
1444 }
1445 spin_unlock(&sp->so_lock);
1446 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001447 spin_unlock(&clp->cl_lock);
1448}
1449
1450static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1451{
1452 struct nfs_server *server;
1453
1454 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1455 return 0;
1456
1457 rcu_read_lock();
1458 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1459 nfs4_clear_reclaim_server(server);
1460 rcu_read_unlock();
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001461
1462 nfs_delegation_reap_unclaimed(clp);
Trond Myklebust6eaa6142010-10-04 17:59:08 -04001463 return 1;
1464}
1465
1466static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1467{
1468 if (!nfs4_state_clear_reclaim_reboot(clp))
1469 return;
1470 nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001471}
1472
1473static void nfs_delegation_clear_all(struct nfs_client *clp)
1474{
1475 nfs_delegation_mark_reclaim(clp);
1476 nfs_delegation_reap_unclaimed(clp);
1477}
1478
1479static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1480{
1481 nfs_delegation_clear_all(clp);
1482 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1483}
1484
Trond Myklebust168667c2010-10-19 19:47:49 -04001485static void nfs4_warn_keyexpired(const char *s)
1486{
1487 printk_ratelimited(KERN_WARNING "Error: state manager"
1488 " encountered RPCSEC_GSS session"
1489 " expired against NFSv4 server %s.\n",
1490 s);
1491}
1492
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001493static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
Trond Myklebuste598d842008-12-23 15:21:42 -05001494{
1495 switch (error) {
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001496 case 0:
1497 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001498 case -NFS4ERR_CB_PATH_DOWN:
Trond Myklebustad1e3962012-03-10 11:23:15 -05001499 nfs40_handle_cb_pathdown(clp);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001500 break;
Trond Myklebustc8b7ae3d2009-12-03 15:53:21 -05001501 case -NFS4ERR_NO_GRACE:
1502 nfs4_state_end_reclaim_reboot(clp);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001503 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001504 case -NFS4ERR_STALE_CLIENTID:
1505 case -NFS4ERR_LEASE_MOVED:
1506 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Trond Myklebust6eaa6142010-10-04 17:59:08 -04001507 nfs4_state_clear_reclaim_reboot(clp);
Trond Myklebuste598d842008-12-23 15:21:42 -05001508 nfs4_state_start_reclaim_reboot(clp);
1509 break;
1510 case -NFS4ERR_EXPIRED:
1511 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1512 nfs4_state_start_reclaim_nograce(clp);
Andy Adamson8ba9bf82009-12-04 15:55:34 -05001513 break;
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001514 case -NFS4ERR_BADSESSION:
1515 case -NFS4ERR_BADSLOT:
1516 case -NFS4ERR_BAD_HIGH_SLOT:
1517 case -NFS4ERR_DEADSESSION:
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001518 case -NFS4ERR_SEQ_FALSE_RETRY:
1519 case -NFS4ERR_SEQ_MISORDERED:
Andy Adamson6df08182009-12-04 15:55:05 -05001520 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05001521 /* Zero session reset errors */
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001522 break;
Trond Myklebust7c5d7252012-05-27 12:58:48 -04001523 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1524 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1525 break;
Trond Myklebust168667c2010-10-19 19:47:49 -04001526 case -EKEYEXPIRED:
1527 /* Nothing we can do */
1528 nfs4_warn_keyexpired(clp->cl_hostname);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001529 break;
1530 default:
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001531 dprintk("%s: failed to handle error %d for server %s\n",
1532 __func__, error, clp->cl_hostname);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001533 return error;
Trond Myklebuste598d842008-12-23 15:21:42 -05001534 }
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001535 dprintk("%s: handled error %d for server %s\n", __func__, error,
1536 clp->cl_hostname);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001537 return 0;
Trond Myklebuste598d842008-12-23 15:21:42 -05001538}
1539
Trond Myklebust02860012008-12-23 15:21:40 -05001540static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Chuck Lever24d292b2010-12-24 01:32:43 +00001542 struct nfs4_state_owner *sp;
1543 struct nfs_server *server;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001544 struct rb_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 int status = 0;
1546
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001547restart:
Chuck Lever24d292b2010-12-24 01:32:43 +00001548 rcu_read_lock();
1549 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001550 nfs4_purge_state_owners(server);
Chuck Lever24d292b2010-12-24 01:32:43 +00001551 spin_lock(&clp->cl_lock);
1552 for (pos = rb_first(&server->state_owners);
1553 pos != NULL;
1554 pos = rb_next(pos)) {
1555 sp = rb_entry(pos,
1556 struct nfs4_state_owner, so_server_node);
1557 if (!test_and_clear_bit(ops->owner_flag_bit,
1558 &sp->so_flags))
1559 continue;
1560 atomic_inc(&sp->so_count);
1561 spin_unlock(&clp->cl_lock);
1562 rcu_read_unlock();
1563
1564 status = nfs4_reclaim_open_state(sp, ops);
1565 if (status < 0) {
1566 set_bit(ops->owner_flag_bit, &sp->so_flags);
1567 nfs4_put_state_owner(sp);
1568 return nfs4_recovery_handle_error(clp, status);
1569 }
1570
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001571 nfs4_put_state_owner(sp);
Chuck Lever24d292b2010-12-24 01:32:43 +00001572 goto restart;
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001573 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001574 spin_unlock(&clp->cl_lock);
Trond Myklebust02860012008-12-23 15:21:40 -05001575 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001576 rcu_read_unlock();
Trond Myklebust02860012008-12-23 15:21:40 -05001577 return status;
1578}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Trond Myklebust02860012008-12-23 15:21:40 -05001580static int nfs4_check_lease(struct nfs_client *clp)
1581{
1582 struct rpc_cred *cred;
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001583 const struct nfs4_state_maintenance_ops *ops =
1584 clp->cl_mvops->state_renewal_ops;
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001585 int status;
Trond Myklebust02860012008-12-23 15:21:40 -05001586
Trond Myklebust0f605b52008-12-23 15:21:42 -05001587 /* Is the client already known to have an expired lease? */
1588 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1589 return 0;
Andy Adamsona7b72102009-04-01 09:22:46 -04001590 spin_lock(&clp->cl_lock);
1591 cred = ops->get_state_renewal_cred_locked(clp);
1592 spin_unlock(&clp->cl_lock);
Trond Myklebust0f605b52008-12-23 15:21:42 -05001593 if (cred == NULL) {
1594 cred = nfs4_get_setclientid_cred(clp);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001595 status = -ENOKEY;
Trond Myklebust0f605b52008-12-23 15:21:42 -05001596 if (cred == NULL)
1597 goto out;
Trond Myklebust02860012008-12-23 15:21:40 -05001598 }
Benny Halevy8e69514f2009-04-01 09:22:45 -04001599 status = ops->renew_lease(clp, cred);
Trond Myklebust0f605b52008-12-23 15:21:42 -05001600 put_rpccred(cred);
1601out:
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001602 return nfs4_recovery_handle_error(clp, status);
Trond Myklebust02860012008-12-23 15:21:40 -05001603}
1604
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001605/* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
1606 * on EXCHANGE_ID for v4.1
1607 */
1608static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
1609{
1610 switch (status) {
Trond Myklebust89a21732012-05-25 15:00:06 -04001611 case -NFS4ERR_SEQ_MISORDERED:
1612 if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
1613 return -ESERVERFAULT;
1614 /* Lease confirmation error: retry after purging the lease */
1615 ssleep(1);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001616 case -NFS4ERR_STALE_CLIENTID:
1617 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1618 break;
Chuck Leverde734832012-07-11 16:30:50 -04001619 case -NFS4ERR_CLID_INUSE:
1620 pr_err("NFS: Server %s reports our clientid is in use\n",
1621 clp->cl_hostname);
1622 nfs_mark_client_ready(clp, -EPERM);
1623 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1624 return -EPERM;
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001625 case -EACCES:
1626 if (clp->cl_machine_cred == NULL)
1627 return -EACCES;
1628 /* Handle case where the user hasn't set up machine creds */
1629 nfs4_clear_machine_cred(clp);
1630 case -NFS4ERR_DELAY:
1631 case -ETIMEDOUT:
1632 case -EAGAIN:
1633 ssleep(1);
1634 break;
1635
1636 case -NFS4ERR_MINOR_VERS_MISMATCH:
1637 if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
1638 nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001639 dprintk("%s: exit with error %d for server %s\n",
1640 __func__, -EPROTONOSUPPORT, clp->cl_hostname);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001641 return -EPROTONOSUPPORT;
1642 case -EKEYEXPIRED:
1643 nfs4_warn_keyexpired(clp->cl_hostname);
1644 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1645 * in nfs4_exchange_id */
1646 default:
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001647 dprintk("%s: exit with error %d for server %s\n", __func__,
1648 status, clp->cl_hostname);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001649 return status;
1650 }
1651 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001652 dprintk("%s: handled error %d for server %s\n", __func__, status,
1653 clp->cl_hostname);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001654 return 0;
1655}
1656
Trond Myklebustb42353f2012-06-05 11:19:47 -04001657static int nfs4_establish_lease(struct nfs_client *clp)
Trond Myklebust02860012008-12-23 15:21:40 -05001658{
1659 struct rpc_cred *cred;
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001660 const struct nfs4_state_recovery_ops *ops =
1661 clp->cl_mvops->reboot_recovery_ops;
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001662 int status;
Trond Myklebust02860012008-12-23 15:21:40 -05001663
Andy Adamson90a16612009-04-01 09:22:48 -04001664 cred = ops->get_clid_cred(clp);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001665 if (cred == NULL)
1666 return -ENOENT;
1667 status = ops->establish_clid(clp, cred);
1668 put_rpccred(cred);
1669 if (status != 0)
Trond Myklebustb42353f2012-06-05 11:19:47 -04001670 return status;
1671 pnfs_destroy_all_layouts(clp);
1672 return 0;
1673}
1674
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04001675/*
1676 * Returns zero or a negative errno. NFS4ERR values are converted
1677 * to local errno values.
1678 */
Trond Myklebustb42353f2012-06-05 11:19:47 -04001679static int nfs4_reclaim_lease(struct nfs_client *clp)
1680{
1681 int status;
1682
1683 status = nfs4_establish_lease(clp);
1684 if (status < 0)
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001685 return nfs4_handle_reclaim_lease_error(clp, status);
Trond Myklebustb42353f2012-06-05 11:19:47 -04001686 if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
1687 nfs4_state_start_reclaim_nograce(clp);
1688 if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1689 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1690 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1691 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1692 return 0;
1693}
1694
1695static int nfs4_purge_lease(struct nfs_client *clp)
1696{
1697 int status;
1698
1699 status = nfs4_establish_lease(clp);
1700 if (status < 0)
1701 return nfs4_handle_reclaim_lease_error(clp, status);
1702 clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
1703 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1704 nfs4_state_start_reclaim_nograce(clp);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001705 return 0;
Trond Myklebust02860012008-12-23 15:21:40 -05001706}
1707
Andy Adamson76db6d92009-04-01 09:22:38 -04001708#ifdef CONFIG_NFS_V4_1
Trond Myklebust9f594792012-05-27 13:02:53 -04001709void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001710{
Trond Myklebust444f72f2011-05-26 14:26:35 -04001711 struct nfs_client *clp = session->clp;
1712
Trond Myklebust9f594792012-05-27 13:02:53 -04001713 switch (err) {
1714 default:
1715 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1716 break;
1717 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1718 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1719 }
Trond Myklebust444f72f2011-05-26 14:26:35 -04001720 nfs4_schedule_lease_recovery(clp);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001721}
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00001722EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001723
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001724void nfs41_handle_recall_slot(struct nfs_client *clp)
1725{
1726 set_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001727 dprintk("%s: scheduling slot recall for server %s\n", __func__,
1728 clp->cl_hostname);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001729 nfs4_schedule_state_manager(clp);
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001730}
1731
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001732static void nfs4_reset_all_state(struct nfs_client *clp)
1733{
1734 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
Chuck Lever2c820d92012-05-21 22:45:33 -04001735 set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
Trond Myklebustbe0bfed2012-05-25 16:02:15 -04001736 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001737 nfs4_state_start_reclaim_nograce(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001738 dprintk("%s: scheduling reset of all state for server %s!\n",
1739 __func__, clp->cl_hostname);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001740 nfs4_schedule_state_manager(clp);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001741 }
1742}
1743
1744static void nfs41_handle_server_reboot(struct nfs_client *clp)
1745{
1746 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1747 nfs4_state_start_reclaim_reboot(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001748 dprintk("%s: server %s rebooted!\n", __func__,
1749 clp->cl_hostname);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001750 nfs4_schedule_state_manager(clp);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001751 }
1752}
1753
1754static void nfs41_handle_state_revoked(struct nfs_client *clp)
1755{
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001756 nfs4_reset_all_state(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001757 dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001758}
1759
1760static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
1761{
1762 /* This will need to handle layouts too */
1763 nfs_expire_all_delegations(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001764 dprintk("%s: Recallable state revoked on server %s!\n", __func__,
1765 clp->cl_hostname);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001766}
1767
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04001768static void nfs41_handle_backchannel_fault(struct nfs_client *clp)
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001769{
1770 nfs_expire_all_delegations(clp);
1771 if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001772 nfs4_schedule_state_manager(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001773 dprintk("%s: server %s declared a backchannel fault\n", __func__,
1774 clp->cl_hostname);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001775}
1776
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04001777static void nfs41_handle_cb_path_down(struct nfs_client *clp)
1778{
1779 if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
1780 &clp->cl_state) == 0)
1781 nfs4_schedule_state_manager(clp);
1782}
1783
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001784void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
1785{
1786 if (!flags)
1787 return;
Chuck Lever2c820d92012-05-21 22:45:33 -04001788
1789 dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
1790 __func__, clp->cl_hostname, clp->cl_clientid, flags);
1791
Trond Myklebust111d4892011-12-01 16:37:42 -05001792 if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001793 nfs41_handle_server_reboot(clp);
Trond Myklebust111d4892011-12-01 16:37:42 -05001794 if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001795 SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
1796 SEQ4_STATUS_ADMIN_STATE_REVOKED |
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001797 SEQ4_STATUS_LEASE_MOVED))
1798 nfs41_handle_state_revoked(clp);
Trond Myklebust111d4892011-12-01 16:37:42 -05001799 if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001800 nfs41_handle_recallable_state_revoked(clp);
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04001801 if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
1802 nfs41_handle_backchannel_fault(clp);
1803 else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
1804 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001805 nfs41_handle_cb_path_down(clp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001806}
1807
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001808static int nfs4_reset_session(struct nfs_client *clp)
1809{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04001810 struct rpc_cred *cred;
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001811 int status;
1812
Trond Myklebust1a47e7a2012-06-05 10:53:38 -04001813 if (!nfs4_has_session(clp))
1814 return 0;
Trond Myklebust38045412009-12-15 17:36:57 -05001815 nfs4_begin_drain_session(clp);
Trond Myklebust848f5bd2012-05-25 17:51:23 -04001816 cred = nfs4_get_exchange_id_cred(clp);
1817 status = nfs4_proc_destroy_session(clp->cl_session, cred);
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001818 if (status && status != -NFS4ERR_BADSESSION &&
1819 status != -NFS4ERR_DEADSESSION) {
Ricardo Labiagaf4558482009-12-07 09:16:09 -05001820 status = nfs4_recovery_handle_error(clp, status);
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001821 goto out;
1822 }
1823
1824 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
Trond Myklebust848f5bd2012-05-25 17:51:23 -04001825 status = nfs4_proc_create_session(clp, cred);
Andy Adamson41f54a52010-01-21 14:54:13 -05001826 if (status) {
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001827 dprintk("%s: session reset failed with status %d for server %s!\n",
1828 __func__, status, clp->cl_hostname);
Trond Myklebustf2c1b512012-05-27 14:46:46 -04001829 status = nfs4_handle_reclaim_lease_error(clp, status);
Andy Adamson41f54a52010-01-21 14:54:13 -05001830 goto out;
1831 }
Trond Myklebustbda197f2012-06-05 10:22:14 -04001832 nfs41_finish_session_reset(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001833 dprintk("%s: session reset was successful for server %s!\n",
1834 __func__, clp->cl_hostname);
Andy Adamson41f54a52010-01-21 14:54:13 -05001835out:
Trond Myklebust848f5bd2012-05-25 17:51:23 -04001836 if (cred)
1837 put_rpccred(cred);
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001838 return status;
1839}
Andy Adamson76db6d92009-04-01 09:22:38 -04001840
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001841static int nfs4_recall_slot(struct nfs_client *clp)
1842{
Trond Myklebust60f00152012-06-05 10:51:00 -04001843 struct nfs4_slot_table *fc_tbl;
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001844 struct nfs4_slot *new, *old;
1845 int i;
1846
Trond Myklebust60f00152012-06-05 10:51:00 -04001847 if (!nfs4_has_session(clp))
1848 return 0;
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001849 nfs4_begin_drain_session(clp);
Trond Myklebust60f00152012-06-05 10:51:00 -04001850 fc_tbl = &clp->cl_session->fc_slot_table;
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001851 new = kmalloc(fc_tbl->target_max_slots * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001852 GFP_NOFS);
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001853 if (!new)
1854 return -ENOMEM;
1855
1856 spin_lock(&fc_tbl->slot_tbl_lock);
1857 for (i = 0; i < fc_tbl->target_max_slots; i++)
1858 new[i].seq_nr = fc_tbl->slots[i].seq_nr;
1859 old = fc_tbl->slots;
1860 fc_tbl->slots = new;
1861 fc_tbl->max_slots = fc_tbl->target_max_slots;
1862 fc_tbl->target_max_slots = 0;
Trond Myklebust60f00152012-06-05 10:51:00 -04001863 clp->cl_session->fc_attrs.max_reqs = fc_tbl->max_slots;
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001864 spin_unlock(&fc_tbl->slot_tbl_lock);
1865
1866 kfree(old);
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001867 return 0;
1868}
1869
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04001870static int nfs4_bind_conn_to_session(struct nfs_client *clp)
1871{
Trond Myklebust2cf047c2012-05-25 17:57:41 -04001872 struct rpc_cred *cred;
1873 int ret;
1874
Trond Myklebust1a47e7a2012-06-05 10:53:38 -04001875 if (!nfs4_has_session(clp))
1876 return 0;
Trond Myklebust43ac5442012-05-27 13:47:21 -04001877 nfs4_begin_drain_session(clp);
Trond Myklebust2cf047c2012-05-25 17:57:41 -04001878 cred = nfs4_get_exchange_id_cred(clp);
1879 ret = nfs4_proc_bind_conn_to_session(clp, cred);
1880 if (cred)
1881 put_rpccred(cred);
Trond Myklebust43ac5442012-05-27 13:47:21 -04001882 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
Trond Myklebustbf674c82012-05-27 12:53:10 -04001883 switch (ret) {
1884 case 0:
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001885 dprintk("%s: bind_conn_to_session was successful for server %s!\n",
1886 __func__, clp->cl_hostname);
Trond Myklebustbf674c82012-05-27 12:53:10 -04001887 break;
1888 case -NFS4ERR_DELAY:
1889 ssleep(1);
1890 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1891 break;
1892 default:
1893 return nfs4_recovery_handle_error(clp, ret);
1894 }
1895 return 0;
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04001896}
Andy Adamson76db6d92009-04-01 09:22:38 -04001897#else /* CONFIG_NFS_V4_1 */
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001898static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001899static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001900static int nfs4_recall_slot(struct nfs_client *clp) { return 0; }
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04001901
1902static int nfs4_bind_conn_to_session(struct nfs_client *clp)
1903{
1904 return 0;
1905}
Andy Adamson76db6d92009-04-01 09:22:38 -04001906#endif /* CONFIG_NFS_V4_1 */
1907
Trond Myklebuste005e802008-12-23 15:21:48 -05001908static void nfs4_state_manager(struct nfs_client *clp)
Trond Myklebust02860012008-12-23 15:21:40 -05001909{
Trond Myklebust02860012008-12-23 15:21:40 -05001910 int status = 0;
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04001911 const char *section = "", *section_sep = "";
Trond Myklebust02860012008-12-23 15:21:40 -05001912
Trond Myklebust02860012008-12-23 15:21:40 -05001913 /* Ensure exclusive access to NFSv4 state */
Trond Myklebust47c21992011-04-15 17:34:18 -04001914 do {
Chuck Lever2c820d92012-05-21 22:45:33 -04001915 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04001916 section = "purge state";
Trond Myklebustb42353f2012-06-05 11:19:47 -04001917 status = nfs4_purge_lease(clp);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001918 if (status < 0)
1919 goto out_error;
Trond Myklebustb42353f2012-06-05 11:19:47 -04001920 continue;
Chuck Lever2c820d92012-05-21 22:45:33 -04001921 }
1922
Trond Myklebustb42353f2012-06-05 11:19:47 -04001923 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04001924 section = "lease expired";
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001925 /* We're going to have to re-establish a clientid */
1926 status = nfs4_reclaim_lease(clp);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001927 if (status < 0)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001928 goto out_error;
Trond Myklebustb42353f2012-06-05 11:19:47 -04001929 continue;
Trond Myklebuste598d842008-12-23 15:21:42 -05001930 }
1931
1932 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04001933 section = "check lease";
Trond Myklebuste598d842008-12-23 15:21:42 -05001934 status = nfs4_check_lease(clp);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001935 if (status < 0)
1936 goto out_error;
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001937 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
Trond Myklebuste598d842008-12-23 15:21:42 -05001938 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001940
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001941 /* Initialize or reset the session */
Trond Myklebust1a47e7a2012-06-05 10:53:38 -04001942 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04001943 section = "reset session";
Andy Adamson4d643d12009-12-04 15:52:24 -05001944 status = nfs4_reset_session(clp);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001945 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1946 continue;
1947 if (status < 0)
Andy Adamson76db6d92009-04-01 09:22:38 -04001948 goto out_error;
Andy Adamson76db6d92009-04-01 09:22:38 -04001949 }
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001950
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04001951 /* Send BIND_CONN_TO_SESSION */
1952 if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
Trond Myklebust1a47e7a2012-06-05 10:53:38 -04001953 &clp->cl_state)) {
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04001954 section = "bind conn to session";
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04001955 status = nfs4_bind_conn_to_session(clp);
1956 if (status < 0)
1957 goto out_error;
Trond Myklebustbf674c82012-05-27 12:53:10 -04001958 continue;
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04001959 }
1960
Trond Myklebust1a2dd942012-06-05 10:40:47 -04001961 /* Recall session slots */
Trond Myklebust60f00152012-06-05 10:51:00 -04001962 if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state)) {
Trond Myklebust1a2dd942012-06-05 10:40:47 -04001963 section = "recall slot";
1964 status = nfs4_recall_slot(clp);
1965 if (status < 0)
1966 goto out_error;
1967 continue;
1968 }
1969
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001970 /* First recover reboot state... */
Trond Myklebuste345e882009-12-03 15:52:41 -05001971 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04001972 section = "reclaim reboot";
Andy Adamson591d71c2009-04-01 09:22:47 -04001973 status = nfs4_do_reclaim(clp,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001974 clp->cl_mvops->reboot_recovery_ops);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001975 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
Andy Adamson6df08182009-12-04 15:55:05 -05001976 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001977 continue;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001978 nfs4_state_end_reclaim_reboot(clp);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001979 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1980 continue;
1981 if (status < 0)
1982 goto out_error;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001983 }
1984
1985 /* Now recover expired state... */
1986 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04001987 section = "reclaim nograce";
Andy Adamson591d71c2009-04-01 09:22:47 -04001988 status = nfs4_do_reclaim(clp,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001989 clp->cl_mvops->nograce_recovery_ops);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001990 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
Andy Adamson6df08182009-12-04 15:55:05 -05001991 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001992 test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1993 continue;
1994 if (status < 0)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001995 goto out_error;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001996 }
Trond Myklebust707fb4b2008-12-23 15:21:47 -05001997
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001998 nfs4_end_drain_session(clp);
Trond Myklebust707fb4b2008-12-23 15:21:47 -05001999 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
2000 nfs_client_return_marked_delegations(clp);
2001 continue;
2002 }
Trond Myklebuste005e802008-12-23 15:21:48 -05002003
2004 nfs4_clear_state_manager_bit(clp);
Trond Myklebustf3c76492008-12-23 15:21:48 -05002005 /* Did we race with an attempt to give us more work? */
2006 if (clp->cl_state == 0)
2007 break;
2008 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
2009 break;
Trond Myklebust47c21992011-04-15 17:34:18 -04002010 } while (atomic_read(&clp->cl_count) > 1);
Trond Myklebuste005e802008-12-23 15:21:48 -05002011 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012out_error:
Weston Andros Adamson8ed27d42012-05-29 15:57:59 -04002013 if (strlen(section))
2014 section_sep = ": ";
2015 pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
2016 " with error %d\n", section_sep, section,
2017 clp->cl_hostname, -status);
Alexandros Batsakis5601a002009-12-14 21:27:58 -08002018 nfs4_end_drain_session(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05002019 nfs4_clear_state_manager_bit(clp);
2020}
2021
2022static int nfs4_run_state_manager(void *ptr)
2023{
2024 struct nfs_client *clp = ptr;
2025
2026 allow_signal(SIGKILL);
2027 nfs4_state_manager(clp);
2028 nfs_put_client(clp);
2029 module_put_and_exit(0);
2030 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
2033/*
2034 * Local variables:
2035 * c-basic-offset: 8
2036 * End:
2037 */