blob: c679b9ecef634c80d4738e3cc2a9624f51c327c2 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Chuck Levere3c0fb72012-05-21 22:45:24 -040060#define NFSDBG_FACILITY NFSDBG_STATE
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define OPENOWNER_POOL_SIZE 8
63
Trond Myklebust4ce79712005-06-22 17:16:21 +000064const nfs4_stateid zero_stateid;
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static LIST_HEAD(nfs4_clientid_list);
67
Andy Adamson591d71c2009-04-01 09:22:47 -040068int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Trond Myklebustfd954ae2011-04-24 14:28:18 -040070 struct nfs4_setclientid_res clid = {
71 .clientid = clp->cl_clientid,
72 .confirm = clp->cl_confirm,
73 };
Chuck Leverf738f512009-03-18 20:48:06 -040074 unsigned short port;
75 int status;
76
Trond Myklebustfd954ae2011-04-24 14:28:18 -040077 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
78 goto do_confirm;
Chuck Leverf738f512009-03-18 20:48:06 -040079 port = nfs_callback_tcpport;
80 if (clp->cl_addr.ss_family == AF_INET6)
81 port = nfs_callback_tcpport6;
82
Trond Myklebustbb8b27e2010-04-16 16:43:06 -040083 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
84 if (status != 0)
85 goto out;
Trond Myklebustfd954ae2011-04-24 14:28:18 -040086 clp->cl_clientid = clid.clientid;
87 clp->cl_confirm = clid.confirm;
88 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
89do_confirm:
Trond Myklebustbb8b27e2010-04-16 16:43:06 -040090 status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
91 if (status != 0)
92 goto out;
Trond Myklebustfd954ae2011-04-24 14:28:18 -040093 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
Trond Myklebustbb8b27e2010-04-16 16:43:06 -040094 nfs4_schedule_state_renewal(clp);
95out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return status;
97}
98
Andy Adamsona7b72102009-04-01 09:22:46 -040099struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400100{
101 struct rpc_cred *cred = NULL;
102
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400103 if (clp->cl_machine_cred != NULL)
104 cred = get_rpccred(clp->cl_machine_cred);
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400105 return cred;
106}
107
108static void nfs4_clear_machine_cred(struct nfs_client *clp)
109{
110 struct rpc_cred *cred;
111
112 spin_lock(&clp->cl_lock);
113 cred = clp->cl_machine_cred;
114 clp->cl_machine_cred = NULL;
115 spin_unlock(&clp->cl_lock);
116 if (cred != NULL)
117 put_rpccred(cred);
118}
119
Chuck Lever24d292b2010-12-24 01:32:43 +0000120static struct rpc_cred *
121nfs4_get_renew_cred_server_locked(struct nfs_server *server)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100122{
Chuck Lever24d292b2010-12-24 01:32:43 +0000123 struct rpc_cred *cred = NULL;
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100124 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400125 struct rb_node *pos;
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100126
Chuck Lever24d292b2010-12-24 01:32:43 +0000127 for (pos = rb_first(&server->state_owners);
128 pos != NULL;
129 pos = rb_next(pos)) {
130 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100131 if (list_empty(&sp->so_states))
132 continue;
133 cred = get_rpccred(sp->so_cred);
134 break;
135 }
136 return cred;
137}
138
Chuck Lever24d292b2010-12-24 01:32:43 +0000139/**
140 * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
141 * @clp: client state handle
142 *
143 * Returns an rpc_cred with reference count bumped, or NULL.
144 * Caller must hold clp->cl_lock.
145 */
146struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
147{
148 struct rpc_cred *cred = NULL;
149 struct nfs_server *server;
150
Sachin Prabhue49a29b2012-03-16 19:25:52 +0000151 /* Use machine credentials if available */
152 cred = nfs4_get_machine_cred_locked(clp);
153 if (cred != NULL)
154 goto out;
155
Chuck Lever24d292b2010-12-24 01:32:43 +0000156 rcu_read_lock();
157 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
158 cred = nfs4_get_renew_cred_server_locked(server);
159 if (cred != NULL)
160 break;
161 }
162 rcu_read_unlock();
Sachin Prabhue49a29b2012-03-16 19:25:52 +0000163
164out:
Chuck Lever24d292b2010-12-24 01:32:43 +0000165 return cred;
166}
167
Andy Adamsonb4b82602009-04-01 09:22:49 -0400168#if defined(CONFIG_NFS_V4_1)
169
Ricardo Labiaga9430fb62009-12-06 12:23:46 -0500170static int nfs41_setup_state_renewal(struct nfs_client *clp)
171{
172 int status;
173 struct nfs_fsinfo fsinfo;
174
Andy Adamsond6fb79d2011-03-01 01:34:11 +0000175 if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
176 nfs4_schedule_state_renewal(clp);
177 return 0;
178 }
179
Ricardo Labiaga9430fb62009-12-06 12:23:46 -0500180 status = nfs4_proc_get_lease_time(clp, &fsinfo);
181 if (status == 0) {
182 /* Update lease time and schedule renewal */
183 spin_lock(&clp->cl_lock);
184 clp->cl_lease_time = fsinfo.lease_time * HZ;
185 clp->cl_last_renewal = jiffies;
186 spin_unlock(&clp->cl_lock);
187
188 nfs4_schedule_state_renewal(clp);
189 }
190
191 return status;
192}
193
Andy Adamson42acd022011-01-06 02:04:34 +0000194/*
195 * Back channel returns NFS4ERR_DELAY for new requests when
196 * NFS4_SESSION_DRAINING is set so there is no work to be done when draining
197 * is ended.
198 */
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800199static void nfs4_end_drain_session(struct nfs_client *clp)
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500200{
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800201 struct nfs4_session *ses = clp->cl_session;
Trond Myklebust961a8282012-01-17 22:57:37 -0500202 struct nfs4_slot_table *tbl;
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800203 int max_slots;
204
Trond Myklebusta2118c32010-06-16 09:52:26 -0400205 if (ses == NULL)
206 return;
Trond Myklebust961a8282012-01-17 22:57:37 -0500207 tbl = &ses->fc_slot_table;
Trond Myklebusta2118c32010-06-16 09:52:26 -0400208 if (test_and_clear_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500209 spin_lock(&tbl->slot_tbl_lock);
210 max_slots = tbl->max_slots;
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800211 while (max_slots--) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500212 if (rpc_wake_up_first(&tbl->slot_tbl_waitq,
213 nfs4_set_task_privileged,
214 NULL) == NULL)
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800215 break;
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800216 }
Trond Myklebust961a8282012-01-17 22:57:37 -0500217 spin_unlock(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800218 }
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500219}
220
Andy Adamson42acd022011-01-06 02:04:34 +0000221static int nfs4_wait_on_slot_tbl(struct nfs4_slot_table *tbl)
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500222{
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500223 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonb6bf6e72012-02-17 13:05:23 -0500224 if (tbl->highest_used_slotid != NFS4_NO_SLOT) {
Andy Adamson42acd022011-01-06 02:04:34 +0000225 INIT_COMPLETION(tbl->complete);
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500226 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson42acd022011-01-06 02:04:34 +0000227 return wait_for_completion_interruptible(&tbl->complete);
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500228 }
229 spin_unlock(&tbl->slot_tbl_lock);
230 return 0;
231}
232
Andy Adamson42acd022011-01-06 02:04:34 +0000233static int nfs4_begin_drain_session(struct nfs_client *clp)
234{
235 struct nfs4_session *ses = clp->cl_session;
236 int ret = 0;
237
238 set_bit(NFS4_SESSION_DRAINING, &ses->session_state);
239 /* back channel */
240 ret = nfs4_wait_on_slot_tbl(&ses->bc_slot_table);
241 if (ret)
242 return ret;
243 /* fore channel */
244 return nfs4_wait_on_slot_tbl(&ses->fc_slot_table);
245}
246
Andy Adamson4d643d12009-12-04 15:52:24 -0500247int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
248{
249 int status;
250
Trond Myklebustfd954ae2011-04-24 14:28:18 -0400251 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
252 goto do_confirm;
Trond Myklebust38045412009-12-15 17:36:57 -0500253 nfs4_begin_drain_session(clp);
Andy Adamson4d643d12009-12-04 15:52:24 -0500254 status = nfs4_proc_exchange_id(clp, cred);
Ricardo Labiaga9430fb62009-12-06 12:23:46 -0500255 if (status != 0)
256 goto out;
Trond Myklebustfd954ae2011-04-24 14:28:18 -0400257 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
258do_confirm:
Trond Myklebust848f5bd2012-05-25 17:51:23 -0400259 status = nfs4_proc_create_session(clp, cred);
Ricardo Labiaga9430fb62009-12-06 12:23:46 -0500260 if (status != 0)
261 goto out;
Trond Myklebustfd954ae2011-04-24 14:28:18 -0400262 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
Ricardo Labiaga9430fb62009-12-06 12:23:46 -0500263 nfs41_setup_state_renewal(clp);
264 nfs_mark_client_ready(clp, NFS_CS_READY);
265out:
Andy Adamson4d643d12009-12-04 15:52:24 -0500266 return status;
267}
268
Andy Adamsonb4b82602009-04-01 09:22:49 -0400269struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp)
270{
271 struct rpc_cred *cred;
272
273 spin_lock(&clp->cl_lock);
274 cred = nfs4_get_machine_cred_locked(clp);
275 spin_unlock(&clp->cl_lock);
276 return cred;
277}
278
279#endif /* CONFIG_NFS_V4_1 */
280
Chuck Lever24d292b2010-12-24 01:32:43 +0000281static struct rpc_cred *
282nfs4_get_setclientid_cred_server(struct nfs_server *server)
Trond Myklebust286d7d62006-01-03 09:55:26 +0100283{
Chuck Lever24d292b2010-12-24 01:32:43 +0000284 struct nfs_client *clp = server->nfs_client;
285 struct rpc_cred *cred = NULL;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100286 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400287 struct rb_node *pos;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100288
Trond Myklebust6dc9d572008-12-23 15:21:41 -0500289 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000290 pos = rb_first(&server->state_owners);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400291 if (pos != NULL) {
Chuck Lever24d292b2010-12-24 01:32:43 +0000292 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400293 cred = get_rpccred(sp->so_cred);
Trond Myklebust286d7d62006-01-03 09:55:26 +0100294 }
Trond Myklebust6dc9d572008-12-23 15:21:41 -0500295 spin_unlock(&clp->cl_lock);
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400296 return cred;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100297}
298
Chuck Lever24d292b2010-12-24 01:32:43 +0000299/**
300 * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation
301 * @clp: client state handle
302 *
303 * Returns an rpc_cred with reference count bumped, or NULL.
304 */
305struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
306{
307 struct nfs_server *server;
308 struct rpc_cred *cred;
309
310 spin_lock(&clp->cl_lock);
311 cred = nfs4_get_machine_cred_locked(clp);
312 spin_unlock(&clp->cl_lock);
313 if (cred != NULL)
314 goto out;
315
316 rcu_read_lock();
317 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
318 cred = nfs4_get_setclientid_cred_server(server);
319 if (cred != NULL)
320 break;
321 }
322 rcu_read_unlock();
323
324out:
325 return cred;
326}
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328static struct nfs4_state_owner *
Chuck Lever24d292b2010-12-24 01:32:43 +0000329nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Chuck Lever24d292b2010-12-24 01:32:43 +0000331 struct rb_node **p = &server->state_owners.rb_node,
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400332 *parent = NULL;
Chuck Lever414adf12011-12-06 16:13:39 -0500333 struct nfs4_state_owner *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400335 while (*p != NULL) {
336 parent = *p;
Chuck Lever24d292b2010-12-24 01:32:43 +0000337 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400338
339 if (cred < sp->so_cred)
340 p = &parent->rb_left;
341 else if (cred > sp->so_cred)
342 p = &parent->rb_right;
343 else {
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500344 if (!list_empty(&sp->so_lru))
345 list_del_init(&sp->so_lru);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400346 atomic_inc(&sp->so_count);
Chuck Lever414adf12011-12-06 16:13:39 -0500347 return sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Chuck Lever414adf12011-12-06 16:13:39 -0500350 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400353static struct nfs4_state_owner *
Chuck Lever24d292b2010-12-24 01:32:43 +0000354nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400355{
Chuck Lever24d292b2010-12-24 01:32:43 +0000356 struct nfs_server *server = new->so_server;
357 struct rb_node **p = &server->state_owners.rb_node,
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400358 *parent = NULL;
359 struct nfs4_state_owner *sp;
Trond Myklebust9157c312012-01-17 22:04:24 -0500360 int err;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400361
362 while (*p != NULL) {
363 parent = *p;
Chuck Lever24d292b2010-12-24 01:32:43 +0000364 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400365
366 if (new->so_cred < sp->so_cred)
367 p = &parent->rb_left;
368 else if (new->so_cred > sp->so_cred)
369 p = &parent->rb_right;
370 else {
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500371 if (!list_empty(&sp->so_lru))
372 list_del_init(&sp->so_lru);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400373 atomic_inc(&sp->so_count);
374 return sp;
375 }
376 }
Trond Myklebust48c22eb2012-01-17 22:04:25 -0500377 err = ida_get_new(&server->openowner_id, &new->so_seqid.owner_id);
Trond Myklebust9157c312012-01-17 22:04:24 -0500378 if (err)
379 return ERR_PTR(err);
Chuck Lever24d292b2010-12-24 01:32:43 +0000380 rb_link_node(&new->so_server_node, parent, p);
381 rb_insert_color(&new->so_server_node, &server->state_owners);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400382 return new;
383}
384
385static void
Chuck Lever24d292b2010-12-24 01:32:43 +0000386nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400387{
Chuck Lever24d292b2010-12-24 01:32:43 +0000388 struct nfs_server *server = sp->so_server;
389
390 if (!RB_EMPTY_NODE(&sp->so_server_node))
391 rb_erase(&sp->so_server_node, &server->state_owners);
Trond Myklebust48c22eb2012-01-17 22:04:25 -0500392 ida_remove(&server->openowner_id, sp->so_seqid.owner_id);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400393}
394
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500395static void
396nfs4_init_seqid_counter(struct nfs_seqid_counter *sc)
397{
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400398 sc->create_time = ktime_get();
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500399 sc->flags = 0;
400 sc->counter = 0;
401 spin_lock_init(&sc->lock);
402 INIT_LIST_HEAD(&sc->list);
403 rpc_init_wait_queue(&sc->wait, "Seqid_waitqueue");
404}
405
406static void
407nfs4_destroy_seqid_counter(struct nfs_seqid_counter *sc)
408{
409 rpc_destroy_wait_queue(&sc->wait);
410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412/*
413 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
414 * create a new state_owner.
415 *
416 */
417static struct nfs4_state_owner *
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500418nfs4_alloc_state_owner(struct nfs_server *server,
419 struct rpc_cred *cred,
420 gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 struct nfs4_state_owner *sp;
423
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500424 sp = kzalloc(sizeof(*sp), gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (!sp)
426 return NULL;
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500427 sp->so_server = server;
428 sp->so_cred = get_rpccred(cred);
Trond Myklebustec073422005-10-20 14:22:47 -0700429 spin_lock_init(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 INIT_LIST_HEAD(&sp->so_states);
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500431 nfs4_init_seqid_counter(&sp->so_seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 atomic_set(&sp->so_count, 1);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500433 INIT_LIST_HEAD(&sp->so_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return sp;
435}
436
Adrian Bunk1d2e88e2008-05-02 13:42:45 -0700437static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438nfs4_drop_state_owner(struct nfs4_state_owner *sp)
439{
Trond Myklebustc77365c2012-04-21 12:31:05 -0400440 struct rb_node *rb_node = &sp->so_server_node;
441
442 if (!RB_EMPTY_NODE(rb_node)) {
Chuck Lever24d292b2010-12-24 01:32:43 +0000443 struct nfs_server *server = sp->so_server;
444 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400445
446 spin_lock(&clp->cl_lock);
Trond Myklebustc77365c2012-04-21 12:31:05 -0400447 if (!RB_EMPTY_NODE(rb_node)) {
448 rb_erase(rb_node, &server->state_owners);
449 RB_CLEAR_NODE(rb_node);
450 }
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400451 spin_unlock(&clp->cl_lock);
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500455static void nfs4_free_state_owner(struct nfs4_state_owner *sp)
456{
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500457 nfs4_destroy_seqid_counter(&sp->so_seqid);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500458 put_rpccred(sp->so_cred);
459 kfree(sp);
460}
461
462static void nfs4_gc_state_owners(struct nfs_server *server)
463{
464 struct nfs_client *clp = server->nfs_client;
465 struct nfs4_state_owner *sp, *tmp;
466 unsigned long time_min, time_max;
467 LIST_HEAD(doomed);
468
469 spin_lock(&clp->cl_lock);
470 time_max = jiffies;
471 time_min = (long)time_max - (long)clp->cl_lease_time;
472 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
473 /* NB: LRU is sorted so that oldest is at the head */
474 if (time_in_range(sp->so_expires, time_min, time_max))
475 break;
476 list_move(&sp->so_lru, &doomed);
477 nfs4_remove_state_owner_locked(sp);
478 }
479 spin_unlock(&clp->cl_lock);
480
481 list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
482 list_del(&sp->so_lru);
483 nfs4_free_state_owner(sp);
484 }
485}
486
Chuck Lever24d292b2010-12-24 01:32:43 +0000487/**
488 * nfs4_get_state_owner - Look up a state owner given a credential
489 * @server: nfs_server to search
490 * @cred: RPC credential to match
491 *
492 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
493 */
494struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500495 struct rpc_cred *cred,
496 gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
David Howells7539bba2006-08-22 20:06:09 -0400498 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct nfs4_state_owner *sp, *new;
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000502 sp = nfs4_find_state_owner_locked(server, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 spin_unlock(&clp->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (sp != NULL)
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500505 goto out;
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500506 new = nfs4_alloc_state_owner(server, cred, gfp_flags);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400507 if (new == NULL)
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500508 goto out;
Trond Myklebust9157c312012-01-17 22:04:24 -0500509 do {
510 if (ida_pre_get(&server->openowner_id, gfp_flags) == 0)
511 break;
512 spin_lock(&clp->cl_lock);
513 sp = nfs4_insert_state_owner_locked(new);
514 spin_unlock(&clp->cl_lock);
515 } while (sp == ERR_PTR(-EAGAIN));
Trond Myklebustd1e284d2012-01-17 22:04:24 -0500516 if (sp != new)
517 nfs4_free_state_owner(new);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500518out:
519 nfs4_gc_state_owners(server);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400520 return sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Chuck Lever24d292b2010-12-24 01:32:43 +0000523/**
524 * nfs4_put_state_owner - Release a nfs4_state_owner
525 * @sp: state owner data to release
Trond Myklebust7bf97bc2012-04-21 12:36:19 -0400526 *
527 * Note that we keep released state owners on an LRU
528 * list.
529 * This caches valid state owners so that they can be
530 * reused, to avoid the OPEN_CONFIRM on minor version 0.
531 * It also pins the uniquifier of dropped state owners for
532 * a while, to ensure that those state owner names are
533 * never reused.
Chuck Lever24d292b2010-12-24 01:32:43 +0000534 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535void nfs4_put_state_owner(struct nfs4_state_owner *sp)
536{
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500537 struct nfs_server *server = sp->so_server;
538 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
541 return;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500542
Trond Myklebust7bf97bc2012-04-21 12:36:19 -0400543 sp->so_expires = jiffies;
544 list_add_tail(&sp->so_lru, &server->state_owners_lru);
545 spin_unlock(&clp->cl_lock);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500546}
547
548/**
549 * nfs4_purge_state_owners - Release all cached state owners
550 * @server: nfs_server with cached state owners to release
551 *
552 * Called at umount time. Remaining state owners will be on
553 * the LRU with ref count of zero.
554 */
555void nfs4_purge_state_owners(struct nfs_server *server)
556{
557 struct nfs_client *clp = server->nfs_client;
558 struct nfs4_state_owner *sp, *tmp;
559 LIST_HEAD(doomed);
560
561 spin_lock(&clp->cl_lock);
562 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
563 list_move(&sp->so_lru, &doomed);
564 nfs4_remove_state_owner_locked(sp);
565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 spin_unlock(&clp->cl_lock);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500567
568 list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
569 list_del(&sp->so_lru);
570 nfs4_free_state_owner(sp);
571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574static struct nfs4_state *
575nfs4_alloc_open_state(void)
576{
577 struct nfs4_state *state;
578
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400579 state = kzalloc(sizeof(*state), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (!state)
581 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 atomic_set(&state->count, 1);
583 INIT_LIST_HEAD(&state->lock_states);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000584 spin_lock_init(&state->state_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400585 seqlock_init(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return state;
587}
588
Trond Myklebust4cecb762005-11-04 15:32:58 -0500589void
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500590nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
Trond Myklebust4cecb762005-11-04 15:32:58 -0500591{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500592 if (state->state == fmode)
Trond Myklebust4cecb762005-11-04 15:32:58 -0500593 return;
594 /* NB! List reordering - see the reclaim code for why. */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500595 if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
596 if (fmode & FMODE_WRITE)
Trond Myklebust4cecb762005-11-04 15:32:58 -0500597 list_move(&state->open_states, &state->owner->so_states);
598 else
599 list_move_tail(&state->open_states, &state->owner->so_states);
600 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500601 state->state = fmode;
Trond Myklebust4cecb762005-11-04 15:32:58 -0500602}
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604static struct nfs4_state *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605__nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
606{
607 struct nfs_inode *nfsi = NFS_I(inode);
608 struct nfs4_state *state;
609
610 list_for_each_entry(state, &nfsi->open_states, inode_states) {
Trond Myklebust1c816ef2007-07-03 14:41:19 -0400611 if (state->owner != owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 continue;
Trond Myklebust1c816ef2007-07-03 14:41:19 -0400613 if (atomic_inc_not_zero(&state->count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616 return NULL;
617}
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619static void
620nfs4_free_open_state(struct nfs4_state *state)
621{
622 kfree(state);
623}
624
625struct nfs4_state *
626nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
627{
628 struct nfs4_state *state, *new;
629 struct nfs_inode *nfsi = NFS_I(inode);
630
631 spin_lock(&inode->i_lock);
632 state = __nfs4_find_state_byowner(inode, owner);
633 spin_unlock(&inode->i_lock);
634 if (state)
635 goto out;
636 new = nfs4_alloc_open_state();
Trond Myklebustec073422005-10-20 14:22:47 -0700637 spin_lock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 spin_lock(&inode->i_lock);
639 state = __nfs4_find_state_byowner(inode, owner);
640 if (state == NULL && new != NULL) {
641 state = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 state->owner = owner;
643 atomic_inc(&owner->so_count);
644 list_add(&state->inode_states, &nfsi->open_states);
Dave Chinner0444d762011-03-29 18:08:50 +1100645 ihold(inode);
646 state->inode = inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700648 /* Note: The reclaim code dictates that we add stateless
649 * and read-only stateids to the end of the list */
650 list_add_tail(&state->open_states, &owner->so_states);
651 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 } else {
653 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700654 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (new)
656 nfs4_free_open_state(new);
657 }
658out:
659 return state;
660}
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662void nfs4_put_open_state(struct nfs4_state *state)
663{
664 struct inode *inode = state->inode;
665 struct nfs4_state_owner *owner = state->owner;
666
Trond Myklebustec073422005-10-20 14:22:47 -0700667 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return;
Trond Myklebustec073422005-10-20 14:22:47 -0700669 spin_lock(&inode->i_lock);
Trond Myklebustba6830312007-07-27 10:23:05 -0400670 list_del(&state->inode_states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 list_del(&state->open_states);
Trond Myklebustec073422005-10-20 14:22:47 -0700672 spin_unlock(&inode->i_lock);
673 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 nfs4_free_open_state(state);
676 nfs4_put_state_owner(owner);
677}
678
679/*
Trond Myklebust83c9d412005-10-18 14:20:13 -0700680 * Close the current file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 */
Al Viro643168c2011-06-22 18:20:23 -0400682static void __nfs4_close(struct nfs4_state *state,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400683 fmode_t fmode, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 struct nfs4_state_owner *owner = state->owner;
Trond Myklebust003707c2007-07-05 18:07:55 -0400686 int call_close = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500687 fmode_t newstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 atomic_inc(&owner->so_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /* Protect against nfs4_find_state() */
Trond Myklebustec073422005-10-20 14:22:47 -0700691 spin_lock(&owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500692 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100693 case FMODE_READ:
694 state->n_rdonly--;
695 break;
696 case FMODE_WRITE:
697 state->n_wronly--;
698 break;
699 case FMODE_READ|FMODE_WRITE:
700 state->n_rdwr--;
701 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400702 newstate = FMODE_READ|FMODE_WRITE;
Trond Myklebuste7616922006-01-03 09:55:13 +0100703 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400704 if (state->n_rdonly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100705 newstate &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -0400706 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
707 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
708 }
709 if (state->n_wronly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100710 newstate &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -0400711 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
712 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
713 }
714 if (newstate == 0)
715 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebuste7616922006-01-03 09:55:13 +0100716 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400717 nfs4_state_set_mode_locked(state, newstate);
Trond Myklebustec073422005-10-20 14:22:47 -0700718 spin_unlock(&owner->so_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500719
Trond Myklebust003707c2007-07-05 18:07:55 -0400720 if (!call_close) {
Trond Myklebustb39e6252007-06-11 23:05:07 -0400721 nfs4_put_open_state(state);
722 nfs4_put_state_owner(owner);
Fred Isamanf7e89172011-01-06 11:36:32 +0000723 } else {
724 bool roc = pnfs_roc(state->inode);
725
Al Viro643168c2011-06-22 18:20:23 -0400726 nfs4_do_close(state, gfp_mask, wait, roc);
Fred Isamanf7e89172011-01-06 11:36:32 +0000727 }
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400728}
729
Al Viro643168c2011-06-22 18:20:23 -0400730void nfs4_close_state(struct nfs4_state *state, fmode_t fmode)
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400731{
Al Viro643168c2011-06-22 18:20:23 -0400732 __nfs4_close(state, fmode, GFP_NOFS, 0);
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400733}
734
Al Viro643168c2011-06-22 18:20:23 -0400735void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode)
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400736{
Al Viro643168c2011-06-22 18:20:23 -0400737 __nfs4_close(state, fmode, GFP_KERNEL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
740/*
741 * Search the state->lock_states for an existing lock_owner
742 * that is compatible with current->files
743 */
744static struct nfs4_lock_state *
Trond Myklebust77041ed2010-07-01 12:49:11 -0400745__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 -0700746{
747 struct nfs4_lock_state *pos;
748 list_for_each_entry(pos, &state->lock_states, ls_locks) {
Trond Myklebust77041ed2010-07-01 12:49:11 -0400749 if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 continue;
Trond Myklebust77041ed2010-07-01 12:49:11 -0400751 switch (pos->ls_owner.lo_type) {
752 case NFS4_POSIX_LOCK_TYPE:
753 if (pos->ls_owner.lo_u.posix_owner != fl_owner)
754 continue;
755 break;
756 case NFS4_FLOCK_LOCK_TYPE:
757 if (pos->ls_owner.lo_u.flock_owner != fl_pid)
758 continue;
759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 atomic_inc(&pos->ls_count);
761 return pos;
762 }
763 return NULL;
764}
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766/*
767 * Return a compatible lock_state. If no initialized lock_state structure
768 * exists, return an uninitialized one.
769 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 */
Trond Myklebust77041ed2010-07-01 12:49:11 -0400771static 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 -0700772{
773 struct nfs4_lock_state *lsp;
Chuck Lever24d292b2010-12-24 01:32:43 +0000774 struct nfs_server *server = state->owner->so_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400776 lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (lsp == NULL)
778 return NULL;
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500779 nfs4_init_seqid_counter(&lsp->ls_seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 atomic_set(&lsp->ls_count, 1);
Trond Myklebustb64aec82009-07-21 16:47:46 -0400781 lsp->ls_state = state;
Trond Myklebust77041ed2010-07-01 12:49:11 -0400782 lsp->ls_owner.lo_type = type;
783 switch (lsp->ls_owner.lo_type) {
784 case NFS4_FLOCK_LOCK_TYPE:
785 lsp->ls_owner.lo_u.flock_owner = fl_pid;
786 break;
787 case NFS4_POSIX_LOCK_TYPE:
788 lsp->ls_owner.lo_u.posix_owner = fl_owner;
789 break;
790 default:
Trond Myklebustd2d7ce22012-01-17 22:04:25 -0500791 goto out_free;
Trond Myklebust77041ed2010-07-01 12:49:11 -0400792 }
Trond Myklebust48c22eb2012-01-17 22:04:25 -0500793 lsp->ls_seqid.owner_id = ida_simple_get(&server->lockowner_id, 0, 0, GFP_NOFS);
794 if (lsp->ls_seqid.owner_id < 0)
Trond Myklebustd2d7ce22012-01-17 22:04:25 -0500795 goto out_free;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000796 INIT_LIST_HEAD(&lsp->ls_locks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return lsp;
Trond Myklebustd2d7ce22012-01-17 22:04:25 -0500798out_free:
799 kfree(lsp);
800 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
Trond Myklebust5ae67c42012-03-19 16:17:18 -0400803void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400804{
Trond Myklebust48c22eb2012-01-17 22:04:25 -0500805 ida_simple_remove(&server->lockowner_id, lsp->ls_seqid.owner_id);
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500806 nfs4_destroy_seqid_counter(&lsp->ls_seqid);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400807 kfree(lsp);
808}
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810/*
811 * Return a compatible lock_state. If no initialized lock_state structure
812 * exists, return an uninitialized one.
813 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 */
Trond Myklebust77041ed2010-07-01 12:49:11 -0400815static 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 -0700816{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000817 struct nfs4_lock_state *lsp, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000819 for(;;) {
820 spin_lock(&state->state_lock);
Trond Myklebust77041ed2010-07-01 12:49:11 -0400821 lsp = __nfs4_find_lock_state(state, owner, pid, type);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000822 if (lsp != NULL)
823 break;
824 if (new != NULL) {
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000825 list_add(&new->ls_locks, &state->lock_states);
826 set_bit(LK_STATE_IN_USE, &state->flags);
827 lsp = new;
828 new = NULL;
829 break;
830 }
831 spin_unlock(&state->state_lock);
Trond Myklebust77041ed2010-07-01 12:49:11 -0400832 new = nfs4_alloc_lock_state(state, owner, pid, type);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000833 if (new == NULL)
834 return NULL;
835 }
836 spin_unlock(&state->state_lock);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400837 if (new != NULL)
Trond Myklebust5ae67c42012-03-19 16:17:18 -0400838 nfs4_free_lock_state(state->owner->so_server, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return lsp;
840}
841
842/*
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000843 * Release reference to lock_state, and free it if we see that
844 * it is no longer in use
845 */
Trond Myklebustfaf5f492005-10-18 14:20:15 -0700846void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000847{
848 struct nfs4_state *state;
849
850 if (lsp == NULL)
851 return;
852 state = lsp->ls_state;
853 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
854 return;
855 list_del(&lsp->ls_locks);
856 if (list_empty(&state->lock_states))
857 clear_bit(LK_STATE_IN_USE, &state->flags);
858 spin_unlock(&state->state_lock);
Trond Myklebustcf470c32012-03-07 13:49:12 -0500859 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
860 if (nfs4_release_lockowner(lsp) == 0)
861 return;
862 }
Trond Myklebust5ae67c42012-03-19 16:17:18 -0400863 nfs4_free_lock_state(lsp->ls_state->owner->so_server, lsp);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000864}
865
866static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
867{
868 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
869
870 dst->fl_u.nfs4_fl.owner = lsp;
871 atomic_inc(&lsp->ls_count);
872}
873
874static void nfs4_fl_release_lock(struct file_lock *fl)
875{
876 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
877}
878
Alexey Dobriyan6aed6282009-09-21 17:01:11 -0700879static const struct file_lock_operations nfs4_fl_lock_ops = {
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000880 .fl_copy_lock = nfs4_fl_copy_lock,
881 .fl_release_private = nfs4_fl_release_lock,
882};
883
884int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
885{
886 struct nfs4_lock_state *lsp;
887
888 if (fl->fl_ops != NULL)
889 return 0;
Trond Myklebust77041ed2010-07-01 12:49:11 -0400890 if (fl->fl_flags & FL_POSIX)
891 lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
892 else if (fl->fl_flags & FL_FLOCK)
Trond Myklebust17280172012-03-11 13:11:00 -0400893 lsp = nfs4_get_lock_state(state, NULL, fl->fl_pid,
894 NFS4_FLOCK_LOCK_TYPE);
Trond Myklebust77041ed2010-07-01 12:49:11 -0400895 else
896 return -EINVAL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000897 if (lsp == NULL)
898 return -ENOMEM;
899 fl->fl_u.nfs4_fl.owner = lsp;
900 fl->fl_ops = &nfs4_fl_lock_ops;
901 return 0;
902}
903
Trond Myklebust4fc87962012-03-08 17:42:01 -0500904static bool nfs4_copy_lock_stateid(nfs4_stateid *dst, struct nfs4_state *state,
905 fl_owner_t fl_owner, pid_t fl_pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000907 struct nfs4_lock_state *lsp;
Trond Myklebust4fc87962012-03-08 17:42:01 -0500908 bool ret = false;
909
910 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
911 goto out;
912
913 spin_lock(&state->state_lock);
914 lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
915 if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0) {
916 nfs4_stateid_copy(dst, &lsp->ls_stateid);
917 ret = true;
918 }
919 spin_unlock(&state->state_lock);
920 nfs4_put_lock_state(lsp);
921out:
922 return ret;
923}
924
925static void nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
926{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400927 int seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400929 do {
930 seq = read_seqbegin(&state->seqlock);
Trond Myklebustf597c532012-03-04 18:13:56 -0500931 nfs4_stateid_copy(dst, &state->stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400932 } while (read_seqretry(&state->seqlock, seq));
Trond Myklebust4fc87962012-03-08 17:42:01 -0500933}
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000934
Trond Myklebust4fc87962012-03-08 17:42:01 -0500935/*
936 * Byte-range lock aware utility to initialize the stateid of read/write
937 * requests.
938 */
939void nfs4_select_rw_stateid(nfs4_stateid *dst, struct nfs4_state *state,
940 fmode_t fmode, fl_owner_t fl_owner, pid_t fl_pid)
941{
942 if (nfs4_copy_delegation_stateid(dst, state->inode, fmode))
943 return;
944 if (nfs4_copy_lock_stateid(dst, state, fl_owner, fl_pid))
945 return;
946 nfs4_copy_open_stateid(dst, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400949struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700951 struct nfs_seqid *new;
952
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400953 new = kmalloc(sizeof(*new), gfp_mask);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700954 if (new != NULL) {
955 new->sequence = counter;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500956 INIT_LIST_HEAD(&new->list);
Trond Myklebust4601df22012-01-20 18:47:05 -0500957 new->task = NULL;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700958 }
959 return new;
960}
961
Trond Myklebust72211db2009-12-15 14:47:36 -0500962void nfs_release_seqid(struct nfs_seqid *seqid)
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700963{
Trond Myklebust4601df22012-01-20 18:47:05 -0500964 struct nfs_seqid_counter *sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700965
Trond Myklebust4601df22012-01-20 18:47:05 -0500966 if (list_empty(&seqid->list))
967 return;
968 sequence = seqid->sequence;
969 spin_lock(&sequence->lock);
970 list_del_init(&seqid->list);
971 if (!list_empty(&sequence->list)) {
972 struct nfs_seqid *next;
973
974 next = list_first_entry(&sequence->list,
975 struct nfs_seqid, list);
976 rpc_wake_up_queued_task(&sequence->wait, next->task);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500977 }
Trond Myklebust4601df22012-01-20 18:47:05 -0500978 spin_unlock(&sequence->lock);
Trond Myklebust72211db2009-12-15 14:47:36 -0500979}
980
981void nfs_free_seqid(struct nfs_seqid *seqid)
982{
983 nfs_release_seqid(seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700984 kfree(seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700988 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
989 * failed with a seqid incrementing error -
990 * see comments nfs_fs.h:seqid_mutating_error()
991 */
Trond Myklebust88d90932007-07-02 14:03:03 -0400992static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Trond Myklebust7ba127a2012-01-17 22:04:25 -0500994 BUG_ON(list_first_entry(&seqid->sequence->list, struct nfs_seqid, list) != seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700995 switch (status) {
996 case 0:
997 break;
998 case -NFS4ERR_BAD_SEQID:
Trond Myklebust6f43ddc2007-07-08 16:49:11 -0400999 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
1000 return;
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001001 pr_warn_ratelimited("NFS: v4 server returned a bad"
Dan Muntz497799e2008-02-13 13:09:35 -08001002 " sequence-id error on an"
1003 " unconfirmed sequence %p!\n",
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001004 seqid->sequence);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001005 case -NFS4ERR_STALE_CLIENTID:
1006 case -NFS4ERR_STALE_STATEID:
1007 case -NFS4ERR_BAD_STATEID:
1008 case -NFS4ERR_BADXDR:
1009 case -NFS4ERR_RESOURCE:
1010 case -NFS4ERR_NOFILEHANDLE:
1011 /* Non-seqid mutating errors */
1012 return;
1013 };
1014 /*
1015 * Note: no locking needed as we are guaranteed to be first
1016 * on the sequence list
1017 */
1018 seqid->sequence->counter++;
1019}
1020
1021void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
1022{
Benny Halevy34dc1ad2009-04-01 09:22:52 -04001023 struct nfs4_state_owner *sp = container_of(seqid->sequence,
1024 struct nfs4_state_owner, so_seqid);
1025 struct nfs_server *server = sp->so_server;
1026
1027 if (status == -NFS4ERR_BAD_SEQID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 nfs4_drop_state_owner(sp);
Benny Halevy34dc1ad2009-04-01 09:22:52 -04001029 if (!nfs4_has_session(server->nfs_client))
1030 nfs_increment_seqid(status, seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001031}
1032
1033/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001034 * Increment the seqid if the LOCK/LOCKU succeeded, or
1035 * failed with a seqid incrementing error -
1036 * see comments nfs_fs.h:seqid_mutating_error()
1037 */
1038void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
1039{
Trond Myklebust88d90932007-07-02 14:03:03 -04001040 nfs_increment_seqid(status, seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001041}
1042
1043int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
1044{
Trond Myklebust7ba127a2012-01-17 22:04:25 -05001045 struct nfs_seqid_counter *sequence = seqid->sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001046 int status = 0;
1047
1048 spin_lock(&sequence->lock);
Trond Myklebust4601df22012-01-20 18:47:05 -05001049 seqid->task = task;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05001050 if (list_empty(&seqid->list))
1051 list_add_tail(&seqid->list, &sequence->list);
1052 if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
1053 goto unlock;
Trond Myklebust5d008372008-02-22 16:34:17 -05001054 rpc_sleep_on(&sequence->wait, task, NULL);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05001055 status = -EAGAIN;
1056unlock:
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001057 spin_unlock(&sequence->lock);
1058 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
Trond Myklebuste005e802008-12-23 15:21:48 -05001061static int nfs4_run_state_manager(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Trond Myklebuste005e802008-12-23 15:21:48 -05001063static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
Trond Myklebust433fbe42006-01-03 09:55:22 +01001064{
1065 smp_mb__before_clear_bit();
Trond Myklebuste005e802008-12-23 15:21:48 -05001066 clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
Trond Myklebust433fbe42006-01-03 09:55:22 +01001067 smp_mb__after_clear_bit();
Trond Myklebuste005e802008-12-23 15:21:48 -05001068 wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
Trond Myklebust433fbe42006-01-03 09:55:22 +01001069 rpc_wake_up(&clp->cl_rpcwaitq);
1070}
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072/*
Trond Myklebuste005e802008-12-23 15:21:48 -05001073 * Schedule the nfs_client asynchronous state management routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 */
Trond Myklebustb0d3ded2008-12-23 15:21:50 -05001075void nfs4_schedule_state_manager(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Trond Myklebust5043e902006-01-03 09:55:23 +01001077 struct task_struct *task;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001078 char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Trond Myklebuste005e802008-12-23 15:21:48 -05001080 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1081 return;
Trond Myklebust5043e902006-01-03 09:55:23 +01001082 __module_get(THIS_MODULE);
1083 atomic_inc(&clp->cl_count);
Trond Myklebust2446ab62012-03-01 17:00:56 -05001084
1085 /* The rcu_read_lock() is not strictly necessary, as the state
1086 * manager is the only thread that ever changes the rpc_xprt
1087 * after it's initialized. At this point, we're single threaded. */
1088 rcu_read_lock();
1089 snprintf(buf, sizeof(buf), "%s-manager",
1090 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
1091 rcu_read_unlock();
1092 task = kthread_run(nfs4_run_state_manager, clp, buf);
1093 if (IS_ERR(task)) {
1094 printk(KERN_ERR "%s: kthread_run: %ld\n",
1095 __func__, PTR_ERR(task));
1096 nfs4_clear_state_manager_bit(clp);
1097 nfs_put_client(clp);
1098 module_put(THIS_MODULE);
1099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
1102/*
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001103 * Schedule a lease recovery attempt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001105void nfs4_schedule_lease_recovery(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
1107 if (!clp)
1108 return;
Trond Myklebuste598d842008-12-23 15:21:42 -05001109 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1110 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001111 dprintk("%s: scheduling lease recovery for server %s\n", __func__,
1112 clp->cl_hostname);
Trond Myklebuste005e802008-12-23 15:21:48 -05001113 nfs4_schedule_state_manager(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
Andy Adamson9cb81962012-03-07 10:49:41 -05001115EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Trond Myklebustad1e3962012-03-10 11:23:15 -05001117/*
1118 * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
1119 * @clp: client to process
1120 *
1121 * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
1122 * resend of the SETCLIENTID and hence re-establish the
1123 * callback channel. Then return all existing delegations.
1124 */
1125static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
1126{
1127 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1128 nfs_expire_all_delegations(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001129 dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
1130 clp->cl_hostname);
Trond Myklebustad1e3962012-03-10 11:23:15 -05001131}
1132
Trond Myklebust042b60b2011-08-24 15:07:37 -04001133void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
1134{
Trond Myklebustad1e3962012-03-10 11:23:15 -05001135 nfs40_handle_cb_pathdown(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04001136 nfs4_schedule_state_manager(clp);
1137}
1138
Trond Myklebustf9feab12011-03-09 16:12:46 -05001139static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001140{
1141
1142 set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1143 /* Don't recover state that expired before the reboot */
1144 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1145 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1146 return 0;
1147 }
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001148 set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001149 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1150 return 1;
1151}
1152
Trond Myklebustf9feab12011-03-09 16:12:46 -05001153static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001154{
1155 set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1156 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001157 set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001158 set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1159 return 1;
1160}
1161
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001162void nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
1163{
1164 struct nfs_client *clp = server->nfs_client;
1165
1166 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001167 dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
1168 clp->cl_hostname);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001169 nfs4_schedule_state_manager(clp);
1170}
Andy Adamson9cb81962012-03-07 10:49:41 -05001171EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001172
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001173void nfs_inode_find_state_and_recover(struct inode *inode,
1174 const nfs4_stateid *stateid)
1175{
1176 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1177 struct nfs_inode *nfsi = NFS_I(inode);
1178 struct nfs_open_context *ctx;
1179 struct nfs4_state *state;
1180 bool found = false;
1181
1182 spin_lock(&inode->i_lock);
1183 list_for_each_entry(ctx, &nfsi->open_files, list) {
1184 state = ctx->state;
1185 if (state == NULL)
1186 continue;
1187 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
1188 continue;
Trond Myklebustf597c532012-03-04 18:13:56 -05001189 if (!nfs4_stateid_match(&state->stateid, stateid))
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001190 continue;
1191 nfs4_state_mark_reclaim_nograce(clp, state);
1192 found = true;
1193 }
1194 spin_unlock(&inode->i_lock);
1195 if (found)
1196 nfs4_schedule_state_manager(clp);
1197}
1198
1199
Trond Myklebust02860012008-12-23 15:21:40 -05001200static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 struct inode *inode = state->inode;
Trond Myklebust19e03c52008-12-23 15:21:44 -05001203 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 struct file_lock *fl;
1205 int status = 0;
1206
Trond Myklebust3f09df72009-06-17 13:23:00 -07001207 if (inode->i_flock == NULL)
1208 return 0;
1209
1210 /* Guard against delegation returns and new lock/unlock calls */
Trond Myklebust19e03c52008-12-23 15:21:44 -05001211 down_write(&nfsi->rwsem);
Trond Myklebust3f09df72009-06-17 13:23:00 -07001212 /* Protect inode->i_flock using the BKL */
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001213 lock_flocks();
Harvey Harrison90dc7d22008-02-20 13:03:05 -08001214 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
Trond Myklebust43b2a332005-11-04 15:35:30 -05001215 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 continue;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001217 if (nfs_file_open_context(fl->fl_file)->state != state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 continue;
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001219 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 status = ops->recover_lock(state, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 switch (status) {
Trond Myklebust965b5d62009-06-17 13:22:59 -07001222 case 0:
1223 break;
1224 case -ESTALE:
1225 case -NFS4ERR_ADMIN_REVOKED:
1226 case -NFS4ERR_STALE_STATEID:
1227 case -NFS4ERR_BAD_STATEID:
1228 case -NFS4ERR_EXPIRED:
1229 case -NFS4ERR_NO_GRACE:
1230 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust9c4c7612009-12-03 15:58:56 -05001231 case -NFS4ERR_BADSESSION:
1232 case -NFS4ERR_BADSLOT:
1233 case -NFS4ERR_BAD_HIGH_SLOT:
1234 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001235 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05001237 printk(KERN_ERR "NFS: %s: unhandled error %d. "
1238 "Zeroing state\n", __func__, status);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001239 case -ENOMEM:
1240 case -NFS4ERR_DENIED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 case -NFS4ERR_RECLAIM_BAD:
1242 case -NFS4ERR_RECLAIM_CONFLICT:
Trond Myklebust43b2a332005-11-04 15:35:30 -05001243 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001244 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001246 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001248 unlock_flocks();
Trond Myklebust965b5d62009-06-17 13:22:59 -07001249out:
Trond Myklebust19e03c52008-12-23 15:21:44 -05001250 up_write(&nfsi->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return status;
1252}
1253
Trond Myklebust02860012008-12-23 15:21:40 -05001254static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 struct nfs4_state *state;
1257 struct nfs4_lock_state *lock;
1258 int status = 0;
1259
1260 /* Note: we rely on the sp->so_states list being ordered
1261 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1262 * states first.
1263 * This is needed to ensure that the server won't give us any
1264 * read delegations that we have to return if, say, we are
1265 * recovering after a network partition or a reboot from a
1266 * server that doesn't support a grace period.
1267 */
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001268restart:
1269 spin_lock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 list_for_each_entry(state, &sp->so_states, open_states) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001271 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1272 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (state->state == 0)
1274 continue;
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001275 atomic_inc(&state->count);
1276 spin_unlock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 status = ops->recover_open(sp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (status >= 0) {
Trond Myklebust02860012008-12-23 15:21:40 -05001279 status = nfs4_reclaim_locks(state, ops);
1280 if (status >= 0) {
Trond Myklebust4b44b402011-12-09 16:31:52 -05001281 spin_lock(&state->state_lock);
Trond Myklebust02860012008-12-23 15:21:40 -05001282 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1283 if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
William Dauchy96dcadc2012-03-14 12:32:04 +01001284 pr_warn_ratelimited("NFS: "
1285 "%s: Lock reclaim "
Weston Andros Adamsona0308892012-01-26 13:32:23 -05001286 "failed!\n", __func__);
Trond Myklebust02860012008-12-23 15:21:40 -05001287 }
Trond Myklebust4b44b402011-12-09 16:31:52 -05001288 spin_unlock(&state->state_lock);
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001289 nfs4_put_open_state(state);
1290 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
1293 switch (status) {
1294 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05001295 printk(KERN_ERR "NFS: %s: unhandled error %d. "
1296 "Zeroing state\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 case -ENOENT:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001298 case -ENOMEM:
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001299 case -ESTALE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 /*
1301 * Open state on this file cannot be recovered
1302 * All we can do is revert to using the zero stateid.
1303 */
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05001304 memset(&state->stateid, 0,
1305 sizeof(state->stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 /* Mark the file as being 'closed' */
1307 state->state = 0;
1308 break;
Trond Myklebust168667c2010-10-19 19:47:49 -04001309 case -EKEYEXPIRED:
1310 /*
1311 * User RPCSEC_GSS context has expired.
1312 * We cannot recover this stateid now, so
1313 * skip it and allow recovery thread to
1314 * proceed.
1315 */
1316 break;
Trond Myklebust965b5d62009-06-17 13:22:59 -07001317 case -NFS4ERR_ADMIN_REVOKED:
1318 case -NFS4ERR_STALE_STATEID:
1319 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001320 case -NFS4ERR_RECLAIM_BAD:
1321 case -NFS4ERR_RECLAIM_CONFLICT:
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001322 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001323 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 case -NFS4ERR_EXPIRED:
1325 case -NFS4ERR_NO_GRACE:
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001326 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust9c4c7612009-12-03 15:58:56 -05001328 case -NFS4ERR_BADSESSION:
1329 case -NFS4ERR_BADSLOT:
1330 case -NFS4ERR_BAD_HIGH_SLOT:
1331 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 goto out_err;
1333 }
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001334 nfs4_put_open_state(state);
1335 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001337 spin_unlock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return 0;
1339out_err:
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001340 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return status;
1342}
1343
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001344static void nfs4_clear_open_state(struct nfs4_state *state)
1345{
1346 struct nfs4_lock_state *lock;
1347
1348 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1349 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1350 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1351 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust4b44b402011-12-09 16:31:52 -05001352 spin_lock(&state->state_lock);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001353 list_for_each_entry(lock, &state->lock_states, ls_locks) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001354 lock->ls_seqid.flags = 0;
1355 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
1356 }
Trond Myklebust4b44b402011-12-09 16:31:52 -05001357 spin_unlock(&state->state_lock);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001358}
1359
Chuck Lever24d292b2010-12-24 01:32:43 +00001360static void nfs4_reset_seqids(struct nfs_server *server,
1361 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001362{
Chuck Lever24d292b2010-12-24 01:32:43 +00001363 struct nfs_client *clp = server->nfs_client;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001364 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001365 struct rb_node *pos;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001366 struct nfs4_state *state;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001367
Chuck Lever24d292b2010-12-24 01:32:43 +00001368 spin_lock(&clp->cl_lock);
1369 for (pos = rb_first(&server->state_owners);
1370 pos != NULL;
1371 pos = rb_next(pos)) {
1372 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001373 sp->so_seqid.flags = 0;
Trond Myklebustec073422005-10-20 14:22:47 -07001374 spin_lock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001375 list_for_each_entry(state, &sp->so_states, open_states) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001376 if (mark_reclaim(clp, state))
1377 nfs4_clear_open_state(state);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001378 }
Trond Myklebustec073422005-10-20 14:22:47 -07001379 spin_unlock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001380 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001381 spin_unlock(&clp->cl_lock);
1382}
1383
1384static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1385 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1386{
1387 struct nfs_server *server;
1388
1389 rcu_read_lock();
1390 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1391 nfs4_reset_seqids(server, mark_reclaim);
1392 rcu_read_unlock();
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001393}
1394
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001395static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1396{
1397 /* Mark all delegations for reclaim */
1398 nfs_delegation_mark_reclaim(clp);
1399 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1400}
1401
Ricardo Labiagafce5c832009-12-05 16:08:41 -05001402static void nfs4_reclaim_complete(struct nfs_client *clp,
1403 const struct nfs4_state_recovery_ops *ops)
1404{
1405 /* Notify the server we're done reclaiming our state */
1406 if (ops->reclaim_complete)
1407 (void)ops->reclaim_complete(clp);
1408}
1409
Chuck Lever24d292b2010-12-24 01:32:43 +00001410static void nfs4_clear_reclaim_server(struct nfs_server *server)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001411{
Chuck Lever24d292b2010-12-24 01:32:43 +00001412 struct nfs_client *clp = server->nfs_client;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001413 struct nfs4_state_owner *sp;
1414 struct rb_node *pos;
1415 struct nfs4_state *state;
1416
Chuck Lever24d292b2010-12-24 01:32:43 +00001417 spin_lock(&clp->cl_lock);
1418 for (pos = rb_first(&server->state_owners);
1419 pos != NULL;
1420 pos = rb_next(pos)) {
1421 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001422 spin_lock(&sp->so_lock);
1423 list_for_each_entry(state, &sp->so_states, open_states) {
Chuck Lever24d292b2010-12-24 01:32:43 +00001424 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1425 &state->flags))
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001426 continue;
1427 nfs4_state_mark_reclaim_nograce(clp, state);
1428 }
1429 spin_unlock(&sp->so_lock);
1430 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001431 spin_unlock(&clp->cl_lock);
1432}
1433
1434static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1435{
1436 struct nfs_server *server;
1437
1438 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1439 return 0;
1440
1441 rcu_read_lock();
1442 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1443 nfs4_clear_reclaim_server(server);
1444 rcu_read_unlock();
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001445
1446 nfs_delegation_reap_unclaimed(clp);
Trond Myklebust6eaa6142010-10-04 17:59:08 -04001447 return 1;
1448}
1449
1450static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1451{
1452 if (!nfs4_state_clear_reclaim_reboot(clp))
1453 return;
1454 nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001455}
1456
1457static void nfs_delegation_clear_all(struct nfs_client *clp)
1458{
1459 nfs_delegation_mark_reclaim(clp);
1460 nfs_delegation_reap_unclaimed(clp);
1461}
1462
1463static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1464{
1465 nfs_delegation_clear_all(clp);
1466 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1467}
1468
Trond Myklebust168667c2010-10-19 19:47:49 -04001469static void nfs4_warn_keyexpired(const char *s)
1470{
1471 printk_ratelimited(KERN_WARNING "Error: state manager"
1472 " encountered RPCSEC_GSS session"
1473 " expired against NFSv4 server %s.\n",
1474 s);
1475}
1476
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001477static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
Trond Myklebuste598d842008-12-23 15:21:42 -05001478{
1479 switch (error) {
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001480 case 0:
1481 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001482 case -NFS4ERR_CB_PATH_DOWN:
Trond Myklebustad1e3962012-03-10 11:23:15 -05001483 nfs40_handle_cb_pathdown(clp);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001484 break;
Trond Myklebustc8b7ae3d2009-12-03 15:53:21 -05001485 case -NFS4ERR_NO_GRACE:
1486 nfs4_state_end_reclaim_reboot(clp);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001487 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001488 case -NFS4ERR_STALE_CLIENTID:
1489 case -NFS4ERR_LEASE_MOVED:
1490 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Trond Myklebust6eaa6142010-10-04 17:59:08 -04001491 nfs4_state_clear_reclaim_reboot(clp);
Trond Myklebuste598d842008-12-23 15:21:42 -05001492 nfs4_state_start_reclaim_reboot(clp);
1493 break;
1494 case -NFS4ERR_EXPIRED:
1495 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1496 nfs4_state_start_reclaim_nograce(clp);
Andy Adamson8ba9bf82009-12-04 15:55:34 -05001497 break;
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001498 case -NFS4ERR_BADSESSION:
1499 case -NFS4ERR_BADSLOT:
1500 case -NFS4ERR_BAD_HIGH_SLOT:
1501 case -NFS4ERR_DEADSESSION:
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001502 case -NFS4ERR_SEQ_FALSE_RETRY:
1503 case -NFS4ERR_SEQ_MISORDERED:
Andy Adamson6df08182009-12-04 15:55:05 -05001504 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05001505 /* Zero session reset errors */
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001506 break;
Trond Myklebust7c5d7252012-05-27 12:58:48 -04001507 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1508 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1509 break;
Trond Myklebust168667c2010-10-19 19:47:49 -04001510 case -EKEYEXPIRED:
1511 /* Nothing we can do */
1512 nfs4_warn_keyexpired(clp->cl_hostname);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001513 break;
1514 default:
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001515 dprintk("%s: failed to handle error %d for server %s\n",
1516 __func__, error, clp->cl_hostname);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001517 return error;
Trond Myklebuste598d842008-12-23 15:21:42 -05001518 }
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001519 dprintk("%s: handled error %d for server %s\n", __func__, error,
1520 clp->cl_hostname);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001521 return 0;
Trond Myklebuste598d842008-12-23 15:21:42 -05001522}
1523
Trond Myklebust02860012008-12-23 15:21:40 -05001524static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
Chuck Lever24d292b2010-12-24 01:32:43 +00001526 struct nfs4_state_owner *sp;
1527 struct nfs_server *server;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001528 struct rb_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 int status = 0;
1530
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001531restart:
Chuck Lever24d292b2010-12-24 01:32:43 +00001532 rcu_read_lock();
1533 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001534 nfs4_purge_state_owners(server);
Chuck Lever24d292b2010-12-24 01:32:43 +00001535 spin_lock(&clp->cl_lock);
1536 for (pos = rb_first(&server->state_owners);
1537 pos != NULL;
1538 pos = rb_next(pos)) {
1539 sp = rb_entry(pos,
1540 struct nfs4_state_owner, so_server_node);
1541 if (!test_and_clear_bit(ops->owner_flag_bit,
1542 &sp->so_flags))
1543 continue;
1544 atomic_inc(&sp->so_count);
1545 spin_unlock(&clp->cl_lock);
1546 rcu_read_unlock();
1547
1548 status = nfs4_reclaim_open_state(sp, ops);
1549 if (status < 0) {
1550 set_bit(ops->owner_flag_bit, &sp->so_flags);
1551 nfs4_put_state_owner(sp);
1552 return nfs4_recovery_handle_error(clp, status);
1553 }
1554
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001555 nfs4_put_state_owner(sp);
Chuck Lever24d292b2010-12-24 01:32:43 +00001556 goto restart;
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001557 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001558 spin_unlock(&clp->cl_lock);
Trond Myklebust02860012008-12-23 15:21:40 -05001559 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001560 rcu_read_unlock();
Trond Myklebust02860012008-12-23 15:21:40 -05001561 return status;
1562}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Trond Myklebust02860012008-12-23 15:21:40 -05001564static int nfs4_check_lease(struct nfs_client *clp)
1565{
1566 struct rpc_cred *cred;
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001567 const struct nfs4_state_maintenance_ops *ops =
1568 clp->cl_mvops->state_renewal_ops;
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001569 int status;
Trond Myklebust02860012008-12-23 15:21:40 -05001570
Trond Myklebust0f605b52008-12-23 15:21:42 -05001571 /* Is the client already known to have an expired lease? */
1572 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1573 return 0;
Andy Adamsona7b72102009-04-01 09:22:46 -04001574 spin_lock(&clp->cl_lock);
1575 cred = ops->get_state_renewal_cred_locked(clp);
1576 spin_unlock(&clp->cl_lock);
Trond Myklebust0f605b52008-12-23 15:21:42 -05001577 if (cred == NULL) {
1578 cred = nfs4_get_setclientid_cred(clp);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001579 status = -ENOKEY;
Trond Myklebust0f605b52008-12-23 15:21:42 -05001580 if (cred == NULL)
1581 goto out;
Trond Myklebust02860012008-12-23 15:21:40 -05001582 }
Benny Halevy8e69514f2009-04-01 09:22:45 -04001583 status = ops->renew_lease(clp, cred);
Trond Myklebust0f605b52008-12-23 15:21:42 -05001584 put_rpccred(cred);
1585out:
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001586 return nfs4_recovery_handle_error(clp, status);
Trond Myklebust02860012008-12-23 15:21:40 -05001587}
1588
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001589/* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
1590 * on EXCHANGE_ID for v4.1
1591 */
1592static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
1593{
1594 switch (status) {
Trond Myklebust89a21732012-05-25 15:00:06 -04001595 case -NFS4ERR_SEQ_MISORDERED:
1596 if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
1597 return -ESERVERFAULT;
1598 /* Lease confirmation error: retry after purging the lease */
1599 ssleep(1);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001600 case -NFS4ERR_CLID_INUSE:
1601 case -NFS4ERR_STALE_CLIENTID:
1602 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1603 break;
1604 case -EACCES:
1605 if (clp->cl_machine_cred == NULL)
1606 return -EACCES;
1607 /* Handle case where the user hasn't set up machine creds */
1608 nfs4_clear_machine_cred(clp);
1609 case -NFS4ERR_DELAY:
1610 case -ETIMEDOUT:
1611 case -EAGAIN:
1612 ssleep(1);
1613 break;
1614
1615 case -NFS4ERR_MINOR_VERS_MISMATCH:
1616 if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
1617 nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001618 dprintk("%s: exit with error %d for server %s\n",
1619 __func__, -EPROTONOSUPPORT, clp->cl_hostname);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001620 return -EPROTONOSUPPORT;
1621 case -EKEYEXPIRED:
1622 nfs4_warn_keyexpired(clp->cl_hostname);
1623 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1624 * in nfs4_exchange_id */
1625 default:
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001626 dprintk("%s: exit with error %d for server %s\n", __func__,
1627 status, clp->cl_hostname);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001628 return status;
1629 }
1630 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001631 dprintk("%s: handled error %d for server %s\n", __func__, status,
1632 clp->cl_hostname);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001633 return 0;
1634}
1635
Trond Myklebust02860012008-12-23 15:21:40 -05001636static int nfs4_reclaim_lease(struct nfs_client *clp)
1637{
1638 struct rpc_cred *cred;
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001639 const struct nfs4_state_recovery_ops *ops =
1640 clp->cl_mvops->reboot_recovery_ops;
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001641 int status;
Trond Myklebust02860012008-12-23 15:21:40 -05001642
Andy Adamson90a16612009-04-01 09:22:48 -04001643 cred = ops->get_clid_cred(clp);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001644 if (cred == NULL)
1645 return -ENOENT;
1646 status = ops->establish_clid(clp, cred);
1647 put_rpccred(cred);
1648 if (status != 0)
1649 return nfs4_handle_reclaim_lease_error(clp, status);
1650 return 0;
Trond Myklebust02860012008-12-23 15:21:40 -05001651}
1652
Andy Adamson76db6d92009-04-01 09:22:38 -04001653#ifdef CONFIG_NFS_V4_1
Trond Myklebust9f594792012-05-27 13:02:53 -04001654void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001655{
Trond Myklebust444f72f2011-05-26 14:26:35 -04001656 struct nfs_client *clp = session->clp;
1657
Trond Myklebust9f594792012-05-27 13:02:53 -04001658 switch (err) {
1659 default:
1660 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1661 break;
1662 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1663 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1664 }
Trond Myklebust444f72f2011-05-26 14:26:35 -04001665 nfs4_schedule_lease_recovery(clp);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001666}
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00001667EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001668
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001669void nfs41_handle_recall_slot(struct nfs_client *clp)
1670{
1671 set_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001672 dprintk("%s: scheduling slot recall for server %s\n", __func__,
1673 clp->cl_hostname);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001674 nfs4_schedule_state_manager(clp);
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001675}
1676
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001677static void nfs4_reset_all_state(struct nfs_client *clp)
1678{
1679 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
Chuck Lever2c820d92012-05-21 22:45:33 -04001680 set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
Trond Myklebustbe0bfed2012-05-25 16:02:15 -04001681 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001682 nfs4_state_start_reclaim_nograce(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001683 dprintk("%s: scheduling reset of all state for server %s!\n",
1684 __func__, clp->cl_hostname);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001685 nfs4_schedule_state_manager(clp);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001686 }
1687}
1688
1689static void nfs41_handle_server_reboot(struct nfs_client *clp)
1690{
1691 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1692 nfs4_state_start_reclaim_reboot(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001693 dprintk("%s: server %s rebooted!\n", __func__,
1694 clp->cl_hostname);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001695 nfs4_schedule_state_manager(clp);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001696 }
1697}
1698
1699static void nfs41_handle_state_revoked(struct nfs_client *clp)
1700{
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001701 nfs4_reset_all_state(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001702 dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001703}
1704
1705static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
1706{
1707 /* This will need to handle layouts too */
1708 nfs_expire_all_delegations(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001709 dprintk("%s: Recallable state revoked on server %s!\n", __func__,
1710 clp->cl_hostname);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001711}
1712
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04001713static void nfs41_handle_backchannel_fault(struct nfs_client *clp)
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001714{
1715 nfs_expire_all_delegations(clp);
1716 if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001717 nfs4_schedule_state_manager(clp);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001718 dprintk("%s: server %s declared a backchannel fault\n", __func__,
1719 clp->cl_hostname);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001720}
1721
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04001722static void nfs41_handle_cb_path_down(struct nfs_client *clp)
1723{
1724 if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
1725 &clp->cl_state) == 0)
1726 nfs4_schedule_state_manager(clp);
1727}
1728
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001729void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
1730{
1731 if (!flags)
1732 return;
Chuck Lever2c820d92012-05-21 22:45:33 -04001733
1734 dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
1735 __func__, clp->cl_hostname, clp->cl_clientid, flags);
1736
Trond Myklebust111d4892011-12-01 16:37:42 -05001737 if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001738 nfs41_handle_server_reboot(clp);
Trond Myklebust111d4892011-12-01 16:37:42 -05001739 if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001740 SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
1741 SEQ4_STATUS_ADMIN_STATE_REVOKED |
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001742 SEQ4_STATUS_LEASE_MOVED))
1743 nfs41_handle_state_revoked(clp);
Trond Myklebust111d4892011-12-01 16:37:42 -05001744 if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001745 nfs41_handle_recallable_state_revoked(clp);
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04001746 if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
1747 nfs41_handle_backchannel_fault(clp);
1748 else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
1749 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001750 nfs41_handle_cb_path_down(clp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001751}
1752
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001753static int nfs4_reset_session(struct nfs_client *clp)
1754{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04001755 struct rpc_cred *cred;
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001756 int status;
1757
Trond Myklebust38045412009-12-15 17:36:57 -05001758 nfs4_begin_drain_session(clp);
Trond Myklebust848f5bd2012-05-25 17:51:23 -04001759 cred = nfs4_get_exchange_id_cred(clp);
1760 status = nfs4_proc_destroy_session(clp->cl_session, cred);
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001761 if (status && status != -NFS4ERR_BADSESSION &&
1762 status != -NFS4ERR_DEADSESSION) {
Ricardo Labiagaf4558482009-12-07 09:16:09 -05001763 status = nfs4_recovery_handle_error(clp, status);
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001764 goto out;
1765 }
1766
1767 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
Trond Myklebust848f5bd2012-05-25 17:51:23 -04001768 status = nfs4_proc_create_session(clp, cred);
Andy Adamson41f54a52010-01-21 14:54:13 -05001769 if (status) {
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001770 dprintk("%s: session reset failed with status %d for server %s!\n",
1771 __func__, status, clp->cl_hostname);
Trond Myklebustf2c1b512012-05-27 14:46:46 -04001772 status = nfs4_handle_reclaim_lease_error(clp, status);
Andy Adamson41f54a52010-01-21 14:54:13 -05001773 goto out;
1774 }
Trond Myklebust444f72f2011-05-26 14:26:35 -04001775 clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
Andy Adamson41f54a52010-01-21 14:54:13 -05001776 /* create_session negotiated new slot table */
1777 clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04001778 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001779 dprintk("%s: session reset was successful for server %s!\n",
1780 __func__, clp->cl_hostname);
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05001781
Andy Adamson41f54a52010-01-21 14:54:13 -05001782 /* Let the state manager reestablish state */
1783 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001784 nfs41_setup_state_renewal(clp);
Andy Adamson41f54a52010-01-21 14:54:13 -05001785out:
Trond Myklebust848f5bd2012-05-25 17:51:23 -04001786 if (cred)
1787 put_rpccred(cred);
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001788 return status;
1789}
Andy Adamson76db6d92009-04-01 09:22:38 -04001790
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001791static int nfs4_recall_slot(struct nfs_client *clp)
1792{
1793 struct nfs4_slot_table *fc_tbl = &clp->cl_session->fc_slot_table;
1794 struct nfs4_channel_attrs *fc_attrs = &clp->cl_session->fc_attrs;
1795 struct nfs4_slot *new, *old;
1796 int i;
1797
1798 nfs4_begin_drain_session(clp);
1799 new = kmalloc(fc_tbl->target_max_slots * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001800 GFP_NOFS);
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001801 if (!new)
1802 return -ENOMEM;
1803
1804 spin_lock(&fc_tbl->slot_tbl_lock);
1805 for (i = 0; i < fc_tbl->target_max_slots; i++)
1806 new[i].seq_nr = fc_tbl->slots[i].seq_nr;
1807 old = fc_tbl->slots;
1808 fc_tbl->slots = new;
1809 fc_tbl->max_slots = fc_tbl->target_max_slots;
1810 fc_tbl->target_max_slots = 0;
1811 fc_attrs->max_reqs = fc_tbl->max_slots;
1812 spin_unlock(&fc_tbl->slot_tbl_lock);
1813
1814 kfree(old);
1815 nfs4_end_drain_session(clp);
1816 return 0;
1817}
1818
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04001819static int nfs4_bind_conn_to_session(struct nfs_client *clp)
1820{
Trond Myklebust2cf047c2012-05-25 17:57:41 -04001821 struct rpc_cred *cred;
1822 int ret;
1823
Trond Myklebust43ac5442012-05-27 13:47:21 -04001824 nfs4_begin_drain_session(clp);
Trond Myklebust2cf047c2012-05-25 17:57:41 -04001825 cred = nfs4_get_exchange_id_cred(clp);
1826 ret = nfs4_proc_bind_conn_to_session(clp, cred);
1827 if (cred)
1828 put_rpccred(cred);
Trond Myklebust43ac5442012-05-27 13:47:21 -04001829 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
Trond Myklebustbf674c82012-05-27 12:53:10 -04001830 switch (ret) {
1831 case 0:
Trond Myklebustcc0a9842012-05-28 15:12:27 -04001832 dprintk("%s: bind_conn_to_session was successful for server %s!\n",
1833 __func__, clp->cl_hostname);
Trond Myklebustbf674c82012-05-27 12:53:10 -04001834 break;
1835 case -NFS4ERR_DELAY:
1836 ssleep(1);
1837 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1838 break;
1839 default:
1840 return nfs4_recovery_handle_error(clp, ret);
1841 }
1842 return 0;
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04001843}
Andy Adamson76db6d92009-04-01 09:22:38 -04001844#else /* CONFIG_NFS_V4_1 */
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001845static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001846static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001847static int nfs4_recall_slot(struct nfs_client *clp) { return 0; }
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04001848
1849static int nfs4_bind_conn_to_session(struct nfs_client *clp)
1850{
1851 return 0;
1852}
Andy Adamson76db6d92009-04-01 09:22:38 -04001853#endif /* CONFIG_NFS_V4_1 */
1854
Trond Myklebuste005e802008-12-23 15:21:48 -05001855static void nfs4_state_manager(struct nfs_client *clp)
Trond Myklebust02860012008-12-23 15:21:40 -05001856{
Trond Myklebust02860012008-12-23 15:21:40 -05001857 int status = 0;
1858
Trond Myklebust02860012008-12-23 15:21:40 -05001859 /* Ensure exclusive access to NFSv4 state */
Trond Myklebust47c21992011-04-15 17:34:18 -04001860 do {
Chuck Lever2c820d92012-05-21 22:45:33 -04001861 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001862 status = nfs4_reclaim_lease(clp);
1863 if (status < 0)
1864 goto out_error;
Chuck Lever2c820d92012-05-21 22:45:33 -04001865 clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
1866 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1867 }
1868
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001869 if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
1870 /* We're going to have to re-establish a clientid */
1871 status = nfs4_reclaim_lease(clp);
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001872 if (status < 0)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001873 goto out_error;
Trond Myklebust2a6ee6a2012-05-25 15:00:06 -04001874 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1875 continue;
Trond Myklebuste598d842008-12-23 15:21:42 -05001876 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04001877
1878 if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH,
1879 &clp->cl_state))
1880 nfs4_state_start_reclaim_nograce(clp);
1881 else
1882 set_bit(NFS4CLNT_RECLAIM_REBOOT,
1883 &clp->cl_state);
1884
Andy Adamson974cec82010-10-20 00:18:02 -04001885 pnfs_destroy_all_layouts(clp);
Trond Myklebuste598d842008-12-23 15:21:42 -05001886 }
1887
1888 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
1889 status = nfs4_check_lease(clp);
Trond Myklebust4f38e4a2011-12-01 16:31:34 -05001890 if (status < 0)
1891 goto out_error;
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001892 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
Trond Myklebuste598d842008-12-23 15:21:42 -05001893 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 }
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001895
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001896 /* Initialize or reset the session */
Andy Adamson6df08182009-12-04 15:55:05 -05001897 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)
Trond Myklebust7111dc72009-08-24 19:21:29 -04001898 && nfs4_has_session(clp)) {
Andy Adamson4d643d12009-12-04 15:52:24 -05001899 status = nfs4_reset_session(clp);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001900 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1901 continue;
1902 if (status < 0)
Andy Adamson76db6d92009-04-01 09:22:38 -04001903 goto out_error;
Andy Adamson76db6d92009-04-01 09:22:38 -04001904 }
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001905
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04001906 /* Send BIND_CONN_TO_SESSION */
1907 if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
1908 &clp->cl_state) && nfs4_has_session(clp)) {
1909 status = nfs4_bind_conn_to_session(clp);
1910 if (status < 0)
1911 goto out_error;
Trond Myklebustbf674c82012-05-27 12:53:10 -04001912 continue;
Weston Andros Adamsona9e64442012-05-24 12:26:37 -04001913 }
1914
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001915 /* First recover reboot state... */
Trond Myklebuste345e882009-12-03 15:52:41 -05001916 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
Andy Adamson591d71c2009-04-01 09:22:47 -04001917 status = nfs4_do_reclaim(clp,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001918 clp->cl_mvops->reboot_recovery_ops);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001919 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
Andy Adamson6df08182009-12-04 15:55:05 -05001920 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001921 continue;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001922 nfs4_state_end_reclaim_reboot(clp);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001923 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1924 continue;
1925 if (status < 0)
1926 goto out_error;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001927 }
1928
1929 /* Now recover expired state... */
1930 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
Andy Adamson591d71c2009-04-01 09:22:47 -04001931 status = nfs4_do_reclaim(clp,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001932 clp->cl_mvops->nograce_recovery_ops);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001933 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
Andy Adamson6df08182009-12-04 15:55:05 -05001934 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001935 test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1936 continue;
1937 if (status < 0)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001938 goto out_error;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001939 }
Trond Myklebust707fb4b2008-12-23 15:21:47 -05001940
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001941 nfs4_end_drain_session(clp);
Trond Myklebust707fb4b2008-12-23 15:21:47 -05001942 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
1943 nfs_client_return_marked_delegations(clp);
1944 continue;
1945 }
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001946 /* Recall session slots */
1947 if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state)
1948 && nfs4_has_session(clp)) {
1949 status = nfs4_recall_slot(clp);
1950 if (status < 0)
1951 goto out_error;
1952 continue;
1953 }
1954
Trond Myklebuste005e802008-12-23 15:21:48 -05001955
1956 nfs4_clear_state_manager_bit(clp);
Trond Myklebustf3c76492008-12-23 15:21:48 -05001957 /* Did we race with an attempt to give us more work? */
1958 if (clp->cl_state == 0)
1959 break;
1960 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1961 break;
Trond Myklebust47c21992011-04-15 17:34:18 -04001962 } while (atomic_read(&clp->cl_count) > 1);
Trond Myklebuste005e802008-12-23 15:21:48 -05001963 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964out_error:
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001965 pr_warn_ratelimited("NFS: state manager failed on NFSv4 server %s"
Chuck Lever5d8515c2007-12-10 14:57:16 -05001966 " with error %d\n", clp->cl_hostname, -status);
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001967 nfs4_end_drain_session(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05001968 nfs4_clear_state_manager_bit(clp);
1969}
1970
1971static int nfs4_run_state_manager(void *ptr)
1972{
1973 struct nfs_client *clp = ptr;
1974
1975 allow_signal(SIGKILL);
1976 nfs4_state_manager(clp);
1977 nfs_put_client(clp);
1978 module_put_and_exit(0);
1979 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980}
1981
1982/*
1983 * Local variables:
1984 * c-basic-offset: 8
1985 * End:
1986 */