blob: 47c8dcdada8aaa52a783992a5e00edc4647bb562 [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>
52
Trond Myklebust4ce79712005-06-22 17:16:21 +000053#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "callback.h"
55#include "delegation.h"
David Howells24c8dbb2006-08-22 20:06:10 -040056#include "internal.h"
Andy Adamson974cec82010-10-20 00:18:02 -040057#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define OPENOWNER_POOL_SIZE 8
60
Trond Myklebust4ce79712005-06-22 17:16:21 +000061const nfs4_stateid zero_stateid;
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static LIST_HEAD(nfs4_clientid_list);
64
Andy Adamson591d71c2009-04-01 09:22:47 -040065int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Trond Myklebustbb8b27e2010-04-16 16:43:06 -040067 struct nfs4_setclientid_res clid;
Chuck Leverf738f512009-03-18 20:48:06 -040068 unsigned short port;
69 int status;
70
71 port = nfs_callback_tcpport;
72 if (clp->cl_addr.ss_family == AF_INET6)
73 port = nfs_callback_tcpport6;
74
Trond Myklebustbb8b27e2010-04-16 16:43:06 -040075 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
76 if (status != 0)
77 goto out;
78 status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
79 if (status != 0)
80 goto out;
81 clp->cl_clientid = clid.clientid;
82 nfs4_schedule_state_renewal(clp);
83out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return status;
85}
86
Andy Adamsona7b72102009-04-01 09:22:46 -040087struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
Trond Myklebusta2b2bb82008-04-08 16:02:17 -040088{
89 struct rpc_cred *cred = NULL;
90
Trond Myklebusta2b2bb82008-04-08 16:02:17 -040091 if (clp->cl_machine_cred != NULL)
92 cred = get_rpccred(clp->cl_machine_cred);
Trond Myklebusta2b2bb82008-04-08 16:02:17 -040093 return cred;
94}
95
96static void nfs4_clear_machine_cred(struct nfs_client *clp)
97{
98 struct rpc_cred *cred;
99
100 spin_lock(&clp->cl_lock);
101 cred = clp->cl_machine_cred;
102 clp->cl_machine_cred = NULL;
103 spin_unlock(&clp->cl_lock);
104 if (cred != NULL)
105 put_rpccred(cred);
106}
107
Chuck Lever24d292b2010-12-24 01:32:43 +0000108static struct rpc_cred *
109nfs4_get_renew_cred_server_locked(struct nfs_server *server)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100110{
Chuck Lever24d292b2010-12-24 01:32:43 +0000111 struct rpc_cred *cred = NULL;
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100112 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400113 struct rb_node *pos;
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100114
Chuck Lever24d292b2010-12-24 01:32:43 +0000115 for (pos = rb_first(&server->state_owners);
116 pos != NULL;
117 pos = rb_next(pos)) {
118 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100119 if (list_empty(&sp->so_states))
120 continue;
121 cred = get_rpccred(sp->so_cred);
122 break;
123 }
124 return cred;
125}
126
Chuck Lever24d292b2010-12-24 01:32:43 +0000127/**
128 * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
129 * @clp: client state handle
130 *
131 * Returns an rpc_cred with reference count bumped, or NULL.
132 * Caller must hold clp->cl_lock.
133 */
134struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
135{
136 struct rpc_cred *cred = NULL;
137 struct nfs_server *server;
138
139 rcu_read_lock();
140 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
141 cred = nfs4_get_renew_cred_server_locked(server);
142 if (cred != NULL)
143 break;
144 }
145 rcu_read_unlock();
146 return cred;
147}
148
Andy Adamsonb4b82602009-04-01 09:22:49 -0400149#if defined(CONFIG_NFS_V4_1)
150
Ricardo Labiaga9430fb62009-12-06 12:23:46 -0500151static int nfs41_setup_state_renewal(struct nfs_client *clp)
152{
153 int status;
154 struct nfs_fsinfo fsinfo;
155
156 status = nfs4_proc_get_lease_time(clp, &fsinfo);
157 if (status == 0) {
158 /* Update lease time and schedule renewal */
159 spin_lock(&clp->cl_lock);
160 clp->cl_lease_time = fsinfo.lease_time * HZ;
161 clp->cl_last_renewal = jiffies;
162 spin_unlock(&clp->cl_lock);
163
164 nfs4_schedule_state_renewal(clp);
165 }
166
167 return status;
168}
169
Andy Adamson42acd022011-01-06 02:04:34 +0000170/*
171 * Back channel returns NFS4ERR_DELAY for new requests when
172 * NFS4_SESSION_DRAINING is set so there is no work to be done when draining
173 * is ended.
174 */
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800175static void nfs4_end_drain_session(struct nfs_client *clp)
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500176{
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800177 struct nfs4_session *ses = clp->cl_session;
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800178 int max_slots;
179
Trond Myklebusta2118c32010-06-16 09:52:26 -0400180 if (ses == NULL)
181 return;
182 if (test_and_clear_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800183 spin_lock(&ses->fc_slot_table.slot_tbl_lock);
184 max_slots = ses->fc_slot_table.max_slots;
185 while (max_slots--) {
186 struct rpc_task *task;
187
188 task = rpc_wake_up_next(&ses->fc_slot_table.
189 slot_tbl_waitq);
190 if (!task)
191 break;
192 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
193 }
194 spin_unlock(&ses->fc_slot_table.slot_tbl_lock);
195 }
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500196}
197
Andy Adamson42acd022011-01-06 02:04:34 +0000198static int nfs4_wait_on_slot_tbl(struct nfs4_slot_table *tbl)
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500199{
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500200 spin_lock(&tbl->slot_tbl_lock);
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500201 if (tbl->highest_used_slotid != -1) {
Andy Adamson42acd022011-01-06 02:04:34 +0000202 INIT_COMPLETION(tbl->complete);
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500203 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson42acd022011-01-06 02:04:34 +0000204 return wait_for_completion_interruptible(&tbl->complete);
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500205 }
206 spin_unlock(&tbl->slot_tbl_lock);
207 return 0;
208}
209
Andy Adamson42acd022011-01-06 02:04:34 +0000210static int nfs4_begin_drain_session(struct nfs_client *clp)
211{
212 struct nfs4_session *ses = clp->cl_session;
213 int ret = 0;
214
215 set_bit(NFS4_SESSION_DRAINING, &ses->session_state);
216 /* back channel */
217 ret = nfs4_wait_on_slot_tbl(&ses->bc_slot_table);
218 if (ret)
219 return ret;
220 /* fore channel */
221 return nfs4_wait_on_slot_tbl(&ses->fc_slot_table);
222}
223
Andy Adamson4d643d12009-12-04 15:52:24 -0500224int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
225{
226 int status;
227
Trond Myklebust38045412009-12-15 17:36:57 -0500228 nfs4_begin_drain_session(clp);
Andy Adamson4d643d12009-12-04 15:52:24 -0500229 status = nfs4_proc_exchange_id(clp, cred);
Ricardo Labiaga9430fb62009-12-06 12:23:46 -0500230 if (status != 0)
231 goto out;
232 status = nfs4_proc_create_session(clp);
233 if (status != 0)
234 goto out;
235 nfs41_setup_state_renewal(clp);
236 nfs_mark_client_ready(clp, NFS_CS_READY);
237out:
Andy Adamson4d643d12009-12-04 15:52:24 -0500238 return status;
239}
240
Andy Adamsonb4b82602009-04-01 09:22:49 -0400241struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp)
242{
243 struct rpc_cred *cred;
244
245 spin_lock(&clp->cl_lock);
246 cred = nfs4_get_machine_cred_locked(clp);
247 spin_unlock(&clp->cl_lock);
248 return cred;
249}
250
251#endif /* CONFIG_NFS_V4_1 */
252
Chuck Lever24d292b2010-12-24 01:32:43 +0000253static struct rpc_cred *
254nfs4_get_setclientid_cred_server(struct nfs_server *server)
Trond Myklebust286d7d62006-01-03 09:55:26 +0100255{
Chuck Lever24d292b2010-12-24 01:32:43 +0000256 struct nfs_client *clp = server->nfs_client;
257 struct rpc_cred *cred = NULL;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100258 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400259 struct rb_node *pos;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100260
Trond Myklebust6dc9d572008-12-23 15:21:41 -0500261 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000262 pos = rb_first(&server->state_owners);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400263 if (pos != NULL) {
Chuck Lever24d292b2010-12-24 01:32:43 +0000264 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400265 cred = get_rpccred(sp->so_cred);
Trond Myklebust286d7d62006-01-03 09:55:26 +0100266 }
Trond Myklebust6dc9d572008-12-23 15:21:41 -0500267 spin_unlock(&clp->cl_lock);
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400268 return cred;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100269}
270
Chuck Lever24d292b2010-12-24 01:32:43 +0000271/**
272 * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation
273 * @clp: client state handle
274 *
275 * Returns an rpc_cred with reference count bumped, or NULL.
276 */
277struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
278{
279 struct nfs_server *server;
280 struct rpc_cred *cred;
281
282 spin_lock(&clp->cl_lock);
283 cred = nfs4_get_machine_cred_locked(clp);
284 spin_unlock(&clp->cl_lock);
285 if (cred != NULL)
286 goto out;
287
288 rcu_read_lock();
289 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
290 cred = nfs4_get_setclientid_cred_server(server);
291 if (cred != NULL)
292 break;
293 }
294 rcu_read_unlock();
295
296out:
297 return cred;
298}
299
300static void nfs_alloc_unique_id_locked(struct rb_root *root,
301 struct nfs_unique_id *new,
302 __u64 minval, int maxbits)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400303{
304 struct rb_node **p, *parent;
305 struct nfs_unique_id *pos;
306 __u64 mask = ~0ULL;
307
308 if (maxbits < 64)
309 mask = (1ULL << maxbits) - 1ULL;
310
311 /* Ensure distribution is more or less flat */
312 get_random_bytes(&new->id, sizeof(new->id));
313 new->id &= mask;
314 if (new->id < minval)
315 new->id += minval;
316retry:
317 p = &root->rb_node;
318 parent = NULL;
319
320 while (*p != NULL) {
321 parent = *p;
322 pos = rb_entry(parent, struct nfs_unique_id, rb_node);
323
324 if (new->id < pos->id)
325 p = &(*p)->rb_left;
326 else if (new->id > pos->id)
327 p = &(*p)->rb_right;
328 else
329 goto id_exists;
330 }
331 rb_link_node(&new->rb_node, parent, p);
332 rb_insert_color(&new->rb_node, root);
333 return;
334id_exists:
335 for (;;) {
336 new->id++;
337 if (new->id < minval || (new->id & mask) != new->id) {
338 new->id = minval;
339 break;
340 }
341 parent = rb_next(parent);
342 if (parent == NULL)
343 break;
344 pos = rb_entry(parent, struct nfs_unique_id, rb_node);
345 if (new->id < pos->id)
346 break;
347 }
348 goto retry;
349}
350
351static void nfs_free_unique_id(struct rb_root *root, struct nfs_unique_id *id)
352{
353 rb_erase(&id->rb_node, root);
354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356static struct nfs4_state_owner *
Chuck Lever24d292b2010-12-24 01:32:43 +0000357nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Chuck Lever24d292b2010-12-24 01:32:43 +0000359 struct rb_node **p = &server->state_owners.rb_node,
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400360 *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 struct nfs4_state_owner *sp, *res = NULL;
362
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400363 while (*p != NULL) {
364 parent = *p;
Chuck Lever24d292b2010-12-24 01:32:43 +0000365 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400366
Trond Myklebust6f2e64d2007-07-06 10:53:21 -0400367 if (server < sp->so_server) {
368 p = &parent->rb_left;
369 continue;
370 }
371 if (server > sp->so_server) {
372 p = &parent->rb_right;
373 continue;
374 }
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400375 if (cred < sp->so_cred)
376 p = &parent->rb_left;
377 else if (cred > sp->so_cred)
378 p = &parent->rb_right;
379 else {
380 atomic_inc(&sp->so_count);
381 res = sp;
382 break;
383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385 return res;
386}
387
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400388static struct nfs4_state_owner *
Chuck Lever24d292b2010-12-24 01:32:43 +0000389nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400390{
Chuck Lever24d292b2010-12-24 01:32:43 +0000391 struct nfs_server *server = new->so_server;
392 struct rb_node **p = &server->state_owners.rb_node,
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400393 *parent = NULL;
394 struct nfs4_state_owner *sp;
395
396 while (*p != NULL) {
397 parent = *p;
Chuck Lever24d292b2010-12-24 01:32:43 +0000398 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400399
400 if (new->so_cred < sp->so_cred)
401 p = &parent->rb_left;
402 else if (new->so_cred > sp->so_cred)
403 p = &parent->rb_right;
404 else {
405 atomic_inc(&sp->so_count);
406 return sp;
407 }
408 }
Chuck Lever24d292b2010-12-24 01:32:43 +0000409 nfs_alloc_unique_id_locked(&server->openowner_id,
410 &new->so_owner_id, 1, 64);
411 rb_link_node(&new->so_server_node, parent, p);
412 rb_insert_color(&new->so_server_node, &server->state_owners);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400413 return new;
414}
415
416static void
Chuck Lever24d292b2010-12-24 01:32:43 +0000417nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400418{
Chuck Lever24d292b2010-12-24 01:32:43 +0000419 struct nfs_server *server = sp->so_server;
420
421 if (!RB_EMPTY_NODE(&sp->so_server_node))
422 rb_erase(&sp->so_server_node, &server->state_owners);
423 nfs_free_unique_id(&server->openowner_id, &sp->so_owner_id);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400424}
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426/*
427 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
428 * create a new state_owner.
429 *
430 */
431static struct nfs4_state_owner *
432nfs4_alloc_state_owner(void)
433{
434 struct nfs4_state_owner *sp;
435
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400436 sp = kzalloc(sizeof(*sp),GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (!sp)
438 return NULL;
Trond Myklebustec073422005-10-20 14:22:47 -0700439 spin_lock_init(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 INIT_LIST_HEAD(&sp->so_states);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700441 rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
442 sp->so_seqid.sequence = &sp->so_sequence;
443 spin_lock_init(&sp->so_sequence.lock);
444 INIT_LIST_HEAD(&sp->so_sequence.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 atomic_set(&sp->so_count, 1);
446 return sp;
447}
448
Adrian Bunk1d2e88e2008-05-02 13:42:45 -0700449static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450nfs4_drop_state_owner(struct nfs4_state_owner *sp)
451{
Chuck Lever24d292b2010-12-24 01:32:43 +0000452 if (!RB_EMPTY_NODE(&sp->so_server_node)) {
453 struct nfs_server *server = sp->so_server;
454 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400455
456 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000457 rb_erase(&sp->so_server_node, &server->state_owners);
458 RB_CLEAR_NODE(&sp->so_server_node);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400459 spin_unlock(&clp->cl_lock);
460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Chuck Lever24d292b2010-12-24 01:32:43 +0000463/**
464 * nfs4_get_state_owner - Look up a state owner given a credential
465 * @server: nfs_server to search
466 * @cred: RPC credential to match
467 *
468 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
469 */
470struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
471 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
David Howells7539bba2006-08-22 20:06:09 -0400473 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct nfs4_state_owner *sp, *new;
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000477 sp = nfs4_find_state_owner_locked(server, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 spin_unlock(&clp->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (sp != NULL)
480 return sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400481 new = nfs4_alloc_state_owner();
482 if (new == NULL)
483 return NULL;
Trond Myklebust6f2e64d2007-07-06 10:53:21 -0400484 new->so_server = server;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400485 new->so_cred = cred;
486 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000487 sp = nfs4_insert_state_owner_locked(new);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400488 spin_unlock(&clp->cl_lock);
489 if (sp == new)
490 get_rpccred(cred);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500491 else {
492 rpc_destroy_wait_queue(&new->so_sequence.wait);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400493 kfree(new);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500494 }
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400495 return sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Chuck Lever24d292b2010-12-24 01:32:43 +0000498/**
499 * nfs4_put_state_owner - Release a nfs4_state_owner
500 * @sp: state owner data to release
501 *
502 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503void nfs4_put_state_owner(struct nfs4_state_owner *sp)
504{
Trond Myklebust1f0e8902010-06-24 15:11:43 -0400505 struct nfs_client *clp = sp->so_server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 struct rpc_cred *cred = sp->so_cred;
507
508 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
509 return;
Chuck Lever24d292b2010-12-24 01:32:43 +0000510 nfs4_remove_state_owner_locked(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 spin_unlock(&clp->cl_lock);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500512 rpc_destroy_wait_queue(&sp->so_sequence.wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 put_rpccred(cred);
514 kfree(sp);
515}
516
517static struct nfs4_state *
518nfs4_alloc_open_state(void)
519{
520 struct nfs4_state *state;
521
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400522 state = kzalloc(sizeof(*state), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (!state)
524 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 atomic_set(&state->count, 1);
526 INIT_LIST_HEAD(&state->lock_states);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000527 spin_lock_init(&state->state_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400528 seqlock_init(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return state;
530}
531
Trond Myklebust4cecb762005-11-04 15:32:58 -0500532void
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500533nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
Trond Myklebust4cecb762005-11-04 15:32:58 -0500534{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500535 if (state->state == fmode)
Trond Myklebust4cecb762005-11-04 15:32:58 -0500536 return;
537 /* NB! List reordering - see the reclaim code for why. */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500538 if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
539 if (fmode & FMODE_WRITE)
Trond Myklebust4cecb762005-11-04 15:32:58 -0500540 list_move(&state->open_states, &state->owner->so_states);
541 else
542 list_move_tail(&state->open_states, &state->owner->so_states);
543 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500544 state->state = fmode;
Trond Myklebust4cecb762005-11-04 15:32:58 -0500545}
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547static struct nfs4_state *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548__nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
549{
550 struct nfs_inode *nfsi = NFS_I(inode);
551 struct nfs4_state *state;
552
553 list_for_each_entry(state, &nfsi->open_states, inode_states) {
Trond Myklebust1c816ef2007-07-03 14:41:19 -0400554 if (state->owner != owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 continue;
Trond Myklebust1c816ef2007-07-03 14:41:19 -0400556 if (atomic_inc_not_zero(&state->count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559 return NULL;
560}
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562static void
563nfs4_free_open_state(struct nfs4_state *state)
564{
565 kfree(state);
566}
567
568struct nfs4_state *
569nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
570{
571 struct nfs4_state *state, *new;
572 struct nfs_inode *nfsi = NFS_I(inode);
573
574 spin_lock(&inode->i_lock);
575 state = __nfs4_find_state_byowner(inode, owner);
576 spin_unlock(&inode->i_lock);
577 if (state)
578 goto out;
579 new = nfs4_alloc_open_state();
Trond Myklebustec073422005-10-20 14:22:47 -0700580 spin_lock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 spin_lock(&inode->i_lock);
582 state = __nfs4_find_state_byowner(inode, owner);
583 if (state == NULL && new != NULL) {
584 state = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 state->owner = owner;
586 atomic_inc(&owner->so_count);
587 list_add(&state->inode_states, &nfsi->open_states);
588 state->inode = igrab(inode);
589 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700590 /* Note: The reclaim code dictates that we add stateless
591 * and read-only stateids to the end of the list */
592 list_add_tail(&state->open_states, &owner->so_states);
593 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 } else {
595 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700596 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (new)
598 nfs4_free_open_state(new);
599 }
600out:
601 return state;
602}
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604void nfs4_put_open_state(struct nfs4_state *state)
605{
606 struct inode *inode = state->inode;
607 struct nfs4_state_owner *owner = state->owner;
608
Trond Myklebustec073422005-10-20 14:22:47 -0700609 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return;
Trond Myklebustec073422005-10-20 14:22:47 -0700611 spin_lock(&inode->i_lock);
Trond Myklebustba683032007-07-27 10:23:05 -0400612 list_del(&state->inode_states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 list_del(&state->open_states);
Trond Myklebustec073422005-10-20 14:22:47 -0700614 spin_unlock(&inode->i_lock);
615 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 nfs4_free_open_state(state);
618 nfs4_put_state_owner(owner);
619}
620
621/*
Trond Myklebust83c9d412005-10-18 14:20:13 -0700622 * Close the current file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 */
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400624static void __nfs4_close(struct path *path, struct nfs4_state *state,
625 fmode_t fmode, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 struct nfs4_state_owner *owner = state->owner;
Trond Myklebust003707c2007-07-05 18:07:55 -0400628 int call_close = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500629 fmode_t newstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 atomic_inc(&owner->so_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /* Protect against nfs4_find_state() */
Trond Myklebustec073422005-10-20 14:22:47 -0700633 spin_lock(&owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500634 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100635 case FMODE_READ:
636 state->n_rdonly--;
637 break;
638 case FMODE_WRITE:
639 state->n_wronly--;
640 break;
641 case FMODE_READ|FMODE_WRITE:
642 state->n_rdwr--;
643 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400644 newstate = FMODE_READ|FMODE_WRITE;
Trond Myklebuste7616922006-01-03 09:55:13 +0100645 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400646 if (state->n_rdonly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100647 newstate &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -0400648 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
649 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
650 }
651 if (state->n_wronly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100652 newstate &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -0400653 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
654 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
655 }
656 if (newstate == 0)
657 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebuste7616922006-01-03 09:55:13 +0100658 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400659 nfs4_state_set_mode_locked(state, newstate);
Trond Myklebustec073422005-10-20 14:22:47 -0700660 spin_unlock(&owner->so_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500661
Trond Myklebust003707c2007-07-05 18:07:55 -0400662 if (!call_close) {
Trond Myklebustb39e6252007-06-11 23:05:07 -0400663 nfs4_put_open_state(state);
664 nfs4_put_state_owner(owner);
Fred Isamanf7e89172011-01-06 11:36:32 +0000665 } else {
666 bool roc = pnfs_roc(state->inode);
667
668 nfs4_do_close(path, state, gfp_mask, wait, roc);
669 }
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400670}
671
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500672void nfs4_close_state(struct path *path, struct nfs4_state *state, fmode_t fmode)
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400673{
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400674 __nfs4_close(path, state, fmode, GFP_NOFS, 0);
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400675}
676
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500677void nfs4_close_sync(struct path *path, struct nfs4_state *state, fmode_t fmode)
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400678{
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400679 __nfs4_close(path, state, fmode, GFP_KERNEL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
682/*
683 * Search the state->lock_states for an existing lock_owner
684 * that is compatible with current->files
685 */
686static struct nfs4_lock_state *
Trond Myklebust77041ed2010-07-01 12:49:11 -0400687__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 -0700688{
689 struct nfs4_lock_state *pos;
690 list_for_each_entry(pos, &state->lock_states, ls_locks) {
Trond Myklebust77041ed2010-07-01 12:49:11 -0400691 if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 continue;
Trond Myklebust77041ed2010-07-01 12:49:11 -0400693 switch (pos->ls_owner.lo_type) {
694 case NFS4_POSIX_LOCK_TYPE:
695 if (pos->ls_owner.lo_u.posix_owner != fl_owner)
696 continue;
697 break;
698 case NFS4_FLOCK_LOCK_TYPE:
699 if (pos->ls_owner.lo_u.flock_owner != fl_pid)
700 continue;
701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 atomic_inc(&pos->ls_count);
703 return pos;
704 }
705 return NULL;
706}
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708/*
709 * Return a compatible lock_state. If no initialized lock_state structure
710 * exists, return an uninitialized one.
711 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 */
Trond Myklebust77041ed2010-07-01 12:49:11 -0400713static 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 -0700714{
715 struct nfs4_lock_state *lsp;
Chuck Lever24d292b2010-12-24 01:32:43 +0000716 struct nfs_server *server = state->owner->so_server;
717 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400719 lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (lsp == NULL)
721 return NULL;
Trond Myklebustd0dc3702008-01-10 16:07:54 -0500722 rpc_init_wait_queue(&lsp->ls_sequence.wait, "lock_seqid_waitqueue");
723 spin_lock_init(&lsp->ls_sequence.lock);
724 INIT_LIST_HEAD(&lsp->ls_sequence.list);
725 lsp->ls_seqid.sequence = &lsp->ls_sequence;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 atomic_set(&lsp->ls_count, 1);
Trond Myklebustb64aec82009-07-21 16:47:46 -0400727 lsp->ls_state = state;
Trond Myklebust77041ed2010-07-01 12:49:11 -0400728 lsp->ls_owner.lo_type = type;
729 switch (lsp->ls_owner.lo_type) {
730 case NFS4_FLOCK_LOCK_TYPE:
731 lsp->ls_owner.lo_u.flock_owner = fl_pid;
732 break;
733 case NFS4_POSIX_LOCK_TYPE:
734 lsp->ls_owner.lo_u.posix_owner = fl_owner;
735 break;
736 default:
737 kfree(lsp);
738 return NULL;
739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000741 nfs_alloc_unique_id_locked(&server->lockowner_id, &lsp->ls_id, 1, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 spin_unlock(&clp->cl_lock);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000743 INIT_LIST_HEAD(&lsp->ls_locks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return lsp;
745}
746
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400747static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
748{
Chuck Lever24d292b2010-12-24 01:32:43 +0000749 struct nfs_server *server = lsp->ls_state->owner->so_server;
750 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400751
752 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000753 nfs_free_unique_id(&server->lockowner_id, &lsp->ls_id);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400754 spin_unlock(&clp->cl_lock);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500755 rpc_destroy_wait_queue(&lsp->ls_sequence.wait);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400756 kfree(lsp);
757}
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759/*
760 * Return a compatible lock_state. If no initialized lock_state structure
761 * exists, return an uninitialized one.
762 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 */
Trond Myklebust77041ed2010-07-01 12:49:11 -0400764static 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 -0700765{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000766 struct nfs4_lock_state *lsp, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000768 for(;;) {
769 spin_lock(&state->state_lock);
Trond Myklebust77041ed2010-07-01 12:49:11 -0400770 lsp = __nfs4_find_lock_state(state, owner, pid, type);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000771 if (lsp != NULL)
772 break;
773 if (new != NULL) {
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000774 list_add(&new->ls_locks, &state->lock_states);
775 set_bit(LK_STATE_IN_USE, &state->flags);
776 lsp = new;
777 new = NULL;
778 break;
779 }
780 spin_unlock(&state->state_lock);
Trond Myklebust77041ed2010-07-01 12:49:11 -0400781 new = nfs4_alloc_lock_state(state, owner, pid, type);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000782 if (new == NULL)
783 return NULL;
784 }
785 spin_unlock(&state->state_lock);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400786 if (new != NULL)
787 nfs4_free_lock_state(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return lsp;
789}
790
791/*
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000792 * Release reference to lock_state, and free it if we see that
793 * it is no longer in use
794 */
Trond Myklebustfaf5f492005-10-18 14:20:15 -0700795void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000796{
797 struct nfs4_state *state;
798
799 if (lsp == NULL)
800 return;
801 state = lsp->ls_state;
802 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
803 return;
804 list_del(&lsp->ls_locks);
805 if (list_empty(&state->lock_states))
806 clear_bit(LK_STATE_IN_USE, &state->flags);
807 spin_unlock(&state->state_lock);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -0400808 if (lsp->ls_flags & NFS_LOCK_INITIALIZED)
809 nfs4_release_lockowner(lsp);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400810 nfs4_free_lock_state(lsp);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000811}
812
813static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
814{
815 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
816
817 dst->fl_u.nfs4_fl.owner = lsp;
818 atomic_inc(&lsp->ls_count);
819}
820
821static void nfs4_fl_release_lock(struct file_lock *fl)
822{
823 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
824}
825
Alexey Dobriyan6aed6282009-09-21 17:01:11 -0700826static const struct file_lock_operations nfs4_fl_lock_ops = {
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000827 .fl_copy_lock = nfs4_fl_copy_lock,
828 .fl_release_private = nfs4_fl_release_lock,
829};
830
831int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
832{
833 struct nfs4_lock_state *lsp;
834
835 if (fl->fl_ops != NULL)
836 return 0;
Trond Myklebust77041ed2010-07-01 12:49:11 -0400837 if (fl->fl_flags & FL_POSIX)
838 lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
839 else if (fl->fl_flags & FL_FLOCK)
840 lsp = nfs4_get_lock_state(state, 0, fl->fl_pid, NFS4_FLOCK_LOCK_TYPE);
841 else
842 return -EINVAL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000843 if (lsp == NULL)
844 return -ENOMEM;
845 fl->fl_u.nfs4_fl.owner = lsp;
846 fl->fl_ops = &nfs4_fl_lock_ops;
847 return 0;
848}
849
850/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 * Byte-range lock aware utility to initialize the stateid of read/write
852 * requests.
853 */
Trond Myklebust77041ed2010-07-01 12:49:11 -0400854void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000856 struct nfs4_lock_state *lsp;
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400857 int seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400859 do {
860 seq = read_seqbegin(&state->seqlock);
861 memcpy(dst, &state->stateid, sizeof(*dst));
862 } while (read_seqretry(&state->seqlock, seq));
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000863 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
864 return;
865
866 spin_lock(&state->state_lock);
Trond Myklebust77041ed2010-07-01 12:49:11 -0400867 lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000868 if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
869 memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
870 spin_unlock(&state->state_lock);
871 nfs4_put_lock_state(lsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400874struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700876 struct nfs_seqid *new;
877
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400878 new = kmalloc(sizeof(*new), gfp_mask);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700879 if (new != NULL) {
880 new->sequence = counter;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500881 INIT_LIST_HEAD(&new->list);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700882 }
883 return new;
884}
885
Trond Myklebust72211db2009-12-15 14:47:36 -0500886void nfs_release_seqid(struct nfs_seqid *seqid)
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700887{
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500888 if (!list_empty(&seqid->list)) {
889 struct rpc_sequence *sequence = seqid->sequence->sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700890
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500891 spin_lock(&sequence->lock);
Trond Myklebust72211db2009-12-15 14:47:36 -0500892 list_del_init(&seqid->list);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500893 spin_unlock(&sequence->lock);
894 rpc_wake_up(&sequence->wait);
895 }
Trond Myklebust72211db2009-12-15 14:47:36 -0500896}
897
898void nfs_free_seqid(struct nfs_seqid *seqid)
899{
900 nfs_release_seqid(seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700901 kfree(seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700905 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
906 * failed with a seqid incrementing error -
907 * see comments nfs_fs.h:seqid_mutating_error()
908 */
Trond Myklebust88d90932007-07-02 14:03:03 -0400909static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500911 BUG_ON(list_first_entry(&seqid->sequence->sequence->list, struct nfs_seqid, list) != seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700912 switch (status) {
913 case 0:
914 break;
915 case -NFS4ERR_BAD_SEQID:
Trond Myklebust6f43ddc2007-07-08 16:49:11 -0400916 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
917 return;
918 printk(KERN_WARNING "NFS: v4 server returned a bad"
Dan Muntz497799e2008-02-13 13:09:35 -0800919 " sequence-id error on an"
920 " unconfirmed sequence %p!\n",
Trond Myklebust6f43ddc2007-07-08 16:49:11 -0400921 seqid->sequence);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700922 case -NFS4ERR_STALE_CLIENTID:
923 case -NFS4ERR_STALE_STATEID:
924 case -NFS4ERR_BAD_STATEID:
925 case -NFS4ERR_BADXDR:
926 case -NFS4ERR_RESOURCE:
927 case -NFS4ERR_NOFILEHANDLE:
928 /* Non-seqid mutating errors */
929 return;
930 };
931 /*
932 * Note: no locking needed as we are guaranteed to be first
933 * on the sequence list
934 */
935 seqid->sequence->counter++;
936}
937
938void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
939{
Benny Halevy34dc1ad2009-04-01 09:22:52 -0400940 struct nfs4_state_owner *sp = container_of(seqid->sequence,
941 struct nfs4_state_owner, so_seqid);
942 struct nfs_server *server = sp->so_server;
943
944 if (status == -NFS4ERR_BAD_SEQID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 nfs4_drop_state_owner(sp);
Benny Halevy34dc1ad2009-04-01 09:22:52 -0400946 if (!nfs4_has_session(server->nfs_client))
947 nfs_increment_seqid(status, seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700948}
949
950/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700951 * Increment the seqid if the LOCK/LOCKU succeeded, or
952 * failed with a seqid incrementing error -
953 * see comments nfs_fs.h:seqid_mutating_error()
954 */
955void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
956{
Trond Myklebust88d90932007-07-02 14:03:03 -0400957 nfs_increment_seqid(status, seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700958}
959
960int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
961{
962 struct rpc_sequence *sequence = seqid->sequence->sequence;
963 int status = 0;
964
965 spin_lock(&sequence->lock);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500966 if (list_empty(&seqid->list))
967 list_add_tail(&seqid->list, &sequence->list);
968 if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
969 goto unlock;
Trond Myklebust5d008372008-02-22 16:34:17 -0500970 rpc_sleep_on(&sequence->wait, task, NULL);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500971 status = -EAGAIN;
972unlock:
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700973 spin_unlock(&sequence->lock);
974 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Trond Myklebuste005e802008-12-23 15:21:48 -0500977static int nfs4_run_state_manager(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Trond Myklebuste005e802008-12-23 15:21:48 -0500979static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
Trond Myklebust433fbe42006-01-03 09:55:22 +0100980{
981 smp_mb__before_clear_bit();
Trond Myklebuste005e802008-12-23 15:21:48 -0500982 clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
Trond Myklebust433fbe42006-01-03 09:55:22 +0100983 smp_mb__after_clear_bit();
Trond Myklebuste005e802008-12-23 15:21:48 -0500984 wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
Trond Myklebust433fbe42006-01-03 09:55:22 +0100985 rpc_wake_up(&clp->cl_rpcwaitq);
986}
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988/*
Trond Myklebuste005e802008-12-23 15:21:48 -0500989 * Schedule the nfs_client asynchronous state management routine
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 */
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500991void nfs4_schedule_state_manager(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Trond Myklebust5043e902006-01-03 09:55:23 +0100993 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Trond Myklebuste005e802008-12-23 15:21:48 -0500995 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
996 return;
Trond Myklebust5043e902006-01-03 09:55:23 +0100997 __module_get(THIS_MODULE);
998 atomic_inc(&clp->cl_count);
Trond Myklebuste005e802008-12-23 15:21:48 -0500999 task = kthread_run(nfs4_run_state_manager, clp, "%s-manager",
Chuck Lever5d8515c2007-12-10 14:57:16 -05001000 rpc_peeraddr2str(clp->cl_rpcclient,
1001 RPC_DISPLAY_ADDR));
Trond Myklebust5043e902006-01-03 09:55:23 +01001002 if (!IS_ERR(task))
1003 return;
Trond Myklebuste005e802008-12-23 15:21:48 -05001004 nfs4_clear_state_manager_bit(clp);
David Howells24c8dbb2006-08-22 20:06:10 -04001005 nfs_put_client(clp);
Trond Myklebust5043e902006-01-03 09:55:23 +01001006 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
1009/*
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001010 * Schedule a lease recovery attempt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001012void nfs4_schedule_lease_recovery(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 if (!clp)
1015 return;
Trond Myklebuste598d842008-12-23 15:21:42 -05001016 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1017 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
Trond Myklebuste005e802008-12-23 15:21:48 -05001018 nfs4_schedule_state_manager(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05001021int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001022{
1023
1024 set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1025 /* Don't recover state that expired before the reboot */
1026 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1027 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1028 return 0;
1029 }
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001030 set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001031 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1032 return 1;
1033}
1034
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001035int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001036{
1037 set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1038 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001039 set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001040 set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1041 return 1;
1042}
1043
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001044void nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
1045{
1046 struct nfs_client *clp = server->nfs_client;
1047
1048 nfs4_state_mark_reclaim_nograce(clp, state);
1049 nfs4_schedule_state_manager(clp);
1050}
1051
Trond Myklebust02860012008-12-23 15:21:40 -05001052static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 struct inode *inode = state->inode;
Trond Myklebust19e03c52008-12-23 15:21:44 -05001055 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 struct file_lock *fl;
1057 int status = 0;
1058
Trond Myklebust3f09df72009-06-17 13:23:00 -07001059 if (inode->i_flock == NULL)
1060 return 0;
1061
1062 /* Guard against delegation returns and new lock/unlock calls */
Trond Myklebust19e03c52008-12-23 15:21:44 -05001063 down_write(&nfsi->rwsem);
Trond Myklebust3f09df72009-06-17 13:23:00 -07001064 /* Protect inode->i_flock using the BKL */
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001065 lock_flocks();
Harvey Harrison90dc7d22008-02-20 13:03:05 -08001066 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
Trond Myklebust43b2a332005-11-04 15:35:30 -05001067 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 continue;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001069 if (nfs_file_open_context(fl->fl_file)->state != state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 continue;
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001071 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 status = ops->recover_lock(state, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 switch (status) {
Trond Myklebust965b5d62009-06-17 13:22:59 -07001074 case 0:
1075 break;
1076 case -ESTALE:
1077 case -NFS4ERR_ADMIN_REVOKED:
1078 case -NFS4ERR_STALE_STATEID:
1079 case -NFS4ERR_BAD_STATEID:
1080 case -NFS4ERR_EXPIRED:
1081 case -NFS4ERR_NO_GRACE:
1082 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust9c4c7612009-12-03 15:58:56 -05001083 case -NFS4ERR_BADSESSION:
1084 case -NFS4ERR_BADSLOT:
1085 case -NFS4ERR_BAD_HIGH_SLOT:
1086 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001087 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 default:
1089 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001090 __func__, status);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001091 case -ENOMEM:
1092 case -NFS4ERR_DENIED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 case -NFS4ERR_RECLAIM_BAD:
1094 case -NFS4ERR_RECLAIM_CONFLICT:
Trond Myklebust43b2a332005-11-04 15:35:30 -05001095 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001096 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001098 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001100 unlock_flocks();
Trond Myklebust965b5d62009-06-17 13:22:59 -07001101out:
Trond Myklebust19e03c52008-12-23 15:21:44 -05001102 up_write(&nfsi->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return status;
1104}
1105
Trond Myklebust02860012008-12-23 15:21:40 -05001106static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
1108 struct nfs4_state *state;
1109 struct nfs4_lock_state *lock;
1110 int status = 0;
1111
1112 /* Note: we rely on the sp->so_states list being ordered
1113 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1114 * states first.
1115 * This is needed to ensure that the server won't give us any
1116 * read delegations that we have to return if, say, we are
1117 * recovering after a network partition or a reboot from a
1118 * server that doesn't support a grace period.
1119 */
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001120restart:
1121 spin_lock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 list_for_each_entry(state, &sp->so_states, open_states) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001123 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1124 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (state->state == 0)
1126 continue;
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001127 atomic_inc(&state->count);
1128 spin_unlock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 status = ops->recover_open(sp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (status >= 0) {
Trond Myklebust02860012008-12-23 15:21:40 -05001131 status = nfs4_reclaim_locks(state, ops);
1132 if (status >= 0) {
1133 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1134 if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
1135 printk("%s: Lock reclaim failed!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001136 __func__);
Trond Myklebust02860012008-12-23 15:21:40 -05001137 }
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001138 nfs4_put_open_state(state);
1139 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
1142 switch (status) {
1143 default:
1144 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001145 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 case -ENOENT:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001147 case -ENOMEM:
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001148 case -ESTALE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 /*
1150 * Open state on this file cannot be recovered
1151 * All we can do is revert to using the zero stateid.
1152 */
1153 memset(state->stateid.data, 0,
1154 sizeof(state->stateid.data));
1155 /* Mark the file as being 'closed' */
1156 state->state = 0;
1157 break;
Trond Myklebust168667c2010-10-19 19:47:49 -04001158 case -EKEYEXPIRED:
1159 /*
1160 * User RPCSEC_GSS context has expired.
1161 * We cannot recover this stateid now, so
1162 * skip it and allow recovery thread to
1163 * proceed.
1164 */
1165 break;
Trond Myklebust965b5d62009-06-17 13:22:59 -07001166 case -NFS4ERR_ADMIN_REVOKED:
1167 case -NFS4ERR_STALE_STATEID:
1168 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001169 case -NFS4ERR_RECLAIM_BAD:
1170 case -NFS4ERR_RECLAIM_CONFLICT:
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001171 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001172 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 case -NFS4ERR_EXPIRED:
1174 case -NFS4ERR_NO_GRACE:
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001175 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust9c4c7612009-12-03 15:58:56 -05001177 case -NFS4ERR_BADSESSION:
1178 case -NFS4ERR_BADSLOT:
1179 case -NFS4ERR_BAD_HIGH_SLOT:
1180 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 goto out_err;
1182 }
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001183 nfs4_put_open_state(state);
1184 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001186 spin_unlock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return 0;
1188out_err:
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001189 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return status;
1191}
1192
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001193static void nfs4_clear_open_state(struct nfs4_state *state)
1194{
1195 struct nfs4_lock_state *lock;
1196
1197 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1198 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1199 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1200 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1201 list_for_each_entry(lock, &state->lock_states, ls_locks) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001202 lock->ls_seqid.flags = 0;
1203 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
1204 }
1205}
1206
Chuck Lever24d292b2010-12-24 01:32:43 +00001207static void nfs4_reset_seqids(struct nfs_server *server,
1208 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001209{
Chuck Lever24d292b2010-12-24 01:32:43 +00001210 struct nfs_client *clp = server->nfs_client;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001211 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001212 struct rb_node *pos;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001213 struct nfs4_state *state;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001214
Chuck Lever24d292b2010-12-24 01:32:43 +00001215 spin_lock(&clp->cl_lock);
1216 for (pos = rb_first(&server->state_owners);
1217 pos != NULL;
1218 pos = rb_next(pos)) {
1219 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001220 sp->so_seqid.flags = 0;
Trond Myklebustec073422005-10-20 14:22:47 -07001221 spin_lock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001222 list_for_each_entry(state, &sp->so_states, open_states) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001223 if (mark_reclaim(clp, state))
1224 nfs4_clear_open_state(state);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001225 }
Trond Myklebustec073422005-10-20 14:22:47 -07001226 spin_unlock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001227 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001228 spin_unlock(&clp->cl_lock);
1229}
1230
1231static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1232 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1233{
1234 struct nfs_server *server;
1235
1236 rcu_read_lock();
1237 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1238 nfs4_reset_seqids(server, mark_reclaim);
1239 rcu_read_unlock();
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001240}
1241
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001242static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1243{
1244 /* Mark all delegations for reclaim */
1245 nfs_delegation_mark_reclaim(clp);
1246 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1247}
1248
Ricardo Labiagafce5c832009-12-05 16:08:41 -05001249static void nfs4_reclaim_complete(struct nfs_client *clp,
1250 const struct nfs4_state_recovery_ops *ops)
1251{
1252 /* Notify the server we're done reclaiming our state */
1253 if (ops->reclaim_complete)
1254 (void)ops->reclaim_complete(clp);
1255}
1256
Chuck Lever24d292b2010-12-24 01:32:43 +00001257static void nfs4_clear_reclaim_server(struct nfs_server *server)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001258{
Chuck Lever24d292b2010-12-24 01:32:43 +00001259 struct nfs_client *clp = server->nfs_client;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001260 struct nfs4_state_owner *sp;
1261 struct rb_node *pos;
1262 struct nfs4_state *state;
1263
Chuck Lever24d292b2010-12-24 01:32:43 +00001264 spin_lock(&clp->cl_lock);
1265 for (pos = rb_first(&server->state_owners);
1266 pos != NULL;
1267 pos = rb_next(pos)) {
1268 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001269 spin_lock(&sp->so_lock);
1270 list_for_each_entry(state, &sp->so_states, open_states) {
Chuck Lever24d292b2010-12-24 01:32:43 +00001271 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1272 &state->flags))
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001273 continue;
1274 nfs4_state_mark_reclaim_nograce(clp, state);
1275 }
1276 spin_unlock(&sp->so_lock);
1277 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001278 spin_unlock(&clp->cl_lock);
1279}
1280
1281static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1282{
1283 struct nfs_server *server;
1284
1285 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1286 return 0;
1287
1288 rcu_read_lock();
1289 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1290 nfs4_clear_reclaim_server(server);
1291 rcu_read_unlock();
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001292
1293 nfs_delegation_reap_unclaimed(clp);
Trond Myklebust6eaa6142010-10-04 17:59:08 -04001294 return 1;
1295}
1296
1297static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1298{
1299 if (!nfs4_state_clear_reclaim_reboot(clp))
1300 return;
1301 nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001302}
1303
1304static void nfs_delegation_clear_all(struct nfs_client *clp)
1305{
1306 nfs_delegation_mark_reclaim(clp);
1307 nfs_delegation_reap_unclaimed(clp);
1308}
1309
1310static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1311{
1312 nfs_delegation_clear_all(clp);
1313 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1314}
1315
Trond Myklebust168667c2010-10-19 19:47:49 -04001316static void nfs4_warn_keyexpired(const char *s)
1317{
1318 printk_ratelimited(KERN_WARNING "Error: state manager"
1319 " encountered RPCSEC_GSS session"
1320 " expired against NFSv4 server %s.\n",
1321 s);
1322}
1323
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001324static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
Trond Myklebuste598d842008-12-23 15:21:42 -05001325{
1326 switch (error) {
1327 case -NFS4ERR_CB_PATH_DOWN:
Trond Myklebust707fb4b2008-12-23 15:21:47 -05001328 nfs_handle_cb_pathdown(clp);
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001329 return 0;
Trond Myklebustc8b7ae3d2009-12-03 15:53:21 -05001330 case -NFS4ERR_NO_GRACE:
1331 nfs4_state_end_reclaim_reboot(clp);
1332 return 0;
Trond Myklebuste598d842008-12-23 15:21:42 -05001333 case -NFS4ERR_STALE_CLIENTID:
1334 case -NFS4ERR_LEASE_MOVED:
1335 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Trond Myklebust6eaa6142010-10-04 17:59:08 -04001336 nfs4_state_clear_reclaim_reboot(clp);
Trond Myklebuste598d842008-12-23 15:21:42 -05001337 nfs4_state_start_reclaim_reboot(clp);
1338 break;
1339 case -NFS4ERR_EXPIRED:
1340 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1341 nfs4_state_start_reclaim_nograce(clp);
Andy Adamson8ba9bf82009-12-04 15:55:34 -05001342 break;
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001343 case -NFS4ERR_BADSESSION:
1344 case -NFS4ERR_BADSLOT:
1345 case -NFS4ERR_BAD_HIGH_SLOT:
1346 case -NFS4ERR_DEADSESSION:
1347 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1348 case -NFS4ERR_SEQ_FALSE_RETRY:
1349 case -NFS4ERR_SEQ_MISORDERED:
Andy Adamson6df08182009-12-04 15:55:05 -05001350 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05001351 /* Zero session reset errors */
1352 return 0;
Trond Myklebust168667c2010-10-19 19:47:49 -04001353 case -EKEYEXPIRED:
1354 /* Nothing we can do */
1355 nfs4_warn_keyexpired(clp->cl_hostname);
1356 return 0;
Trond Myklebuste598d842008-12-23 15:21:42 -05001357 }
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001358 return error;
Trond Myklebuste598d842008-12-23 15:21:42 -05001359}
1360
Trond Myklebust02860012008-12-23 15:21:40 -05001361static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Chuck Lever24d292b2010-12-24 01:32:43 +00001363 struct nfs4_state_owner *sp;
1364 struct nfs_server *server;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001365 struct rb_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 int status = 0;
1367
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001368restart:
Chuck Lever24d292b2010-12-24 01:32:43 +00001369 rcu_read_lock();
1370 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1371 spin_lock(&clp->cl_lock);
1372 for (pos = rb_first(&server->state_owners);
1373 pos != NULL;
1374 pos = rb_next(pos)) {
1375 sp = rb_entry(pos,
1376 struct nfs4_state_owner, so_server_node);
1377 if (!test_and_clear_bit(ops->owner_flag_bit,
1378 &sp->so_flags))
1379 continue;
1380 atomic_inc(&sp->so_count);
1381 spin_unlock(&clp->cl_lock);
1382 rcu_read_unlock();
1383
1384 status = nfs4_reclaim_open_state(sp, ops);
1385 if (status < 0) {
1386 set_bit(ops->owner_flag_bit, &sp->so_flags);
1387 nfs4_put_state_owner(sp);
1388 return nfs4_recovery_handle_error(clp, status);
1389 }
1390
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001391 nfs4_put_state_owner(sp);
Chuck Lever24d292b2010-12-24 01:32:43 +00001392 goto restart;
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001393 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001394 spin_unlock(&clp->cl_lock);
Trond Myklebust02860012008-12-23 15:21:40 -05001395 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001396 rcu_read_unlock();
Trond Myklebust02860012008-12-23 15:21:40 -05001397 return status;
1398}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Trond Myklebust02860012008-12-23 15:21:40 -05001400static int nfs4_check_lease(struct nfs_client *clp)
1401{
1402 struct rpc_cred *cred;
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001403 const struct nfs4_state_maintenance_ops *ops =
1404 clp->cl_mvops->state_renewal_ops;
Trond Myklebust02860012008-12-23 15:21:40 -05001405 int status = -NFS4ERR_EXPIRED;
1406
Trond Myklebust0f605b52008-12-23 15:21:42 -05001407 /* Is the client already known to have an expired lease? */
1408 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1409 return 0;
Andy Adamsona7b72102009-04-01 09:22:46 -04001410 spin_lock(&clp->cl_lock);
1411 cred = ops->get_state_renewal_cred_locked(clp);
1412 spin_unlock(&clp->cl_lock);
Trond Myklebust0f605b52008-12-23 15:21:42 -05001413 if (cred == NULL) {
1414 cred = nfs4_get_setclientid_cred(clp);
1415 if (cred == NULL)
1416 goto out;
Trond Myklebust02860012008-12-23 15:21:40 -05001417 }
Benny Halevy8e69514f2009-04-01 09:22:45 -04001418 status = ops->renew_lease(clp, cred);
Trond Myklebust0f605b52008-12-23 15:21:42 -05001419 put_rpccred(cred);
1420out:
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001421 return nfs4_recovery_handle_error(clp, status);
Trond Myklebust02860012008-12-23 15:21:40 -05001422}
1423
1424static int nfs4_reclaim_lease(struct nfs_client *clp)
1425{
1426 struct rpc_cred *cred;
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001427 const struct nfs4_state_recovery_ops *ops =
1428 clp->cl_mvops->reboot_recovery_ops;
Trond Myklebust02860012008-12-23 15:21:40 -05001429 int status = -ENOENT;
1430
Andy Adamson90a16612009-04-01 09:22:48 -04001431 cred = ops->get_clid_cred(clp);
Trond Myklebust02860012008-12-23 15:21:40 -05001432 if (cred != NULL) {
Andy Adamson591d71c2009-04-01 09:22:47 -04001433 status = ops->establish_clid(clp, cred);
Trond Myklebust02860012008-12-23 15:21:40 -05001434 put_rpccred(cred);
1435 /* Handle case where the user hasn't set up machine creds */
1436 if (status == -EACCES && cred == clp->cl_machine_cred) {
1437 nfs4_clear_machine_cred(clp);
1438 status = -EAGAIN;
1439 }
Benny Halevyc2e713d2009-04-01 09:21:51 -04001440 if (status == -NFS4ERR_MINOR_VERS_MISMATCH)
1441 status = -EPROTONOSUPPORT;
Trond Myklebust02860012008-12-23 15:21:40 -05001442 }
1443 return status;
1444}
1445
Andy Adamson76db6d92009-04-01 09:22:38 -04001446#ifdef CONFIG_NFS_V4_1
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001447void nfs4_schedule_session_recovery(struct nfs4_session *session)
1448{
1449 nfs4_schedule_lease_recovery(session->clp);
1450}
1451
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001452void nfs41_handle_recall_slot(struct nfs_client *clp)
1453{
1454 set_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001455 nfs4_schedule_state_manager(clp);
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001456}
1457
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001458static void nfs4_reset_all_state(struct nfs_client *clp)
1459{
1460 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1461 clp->cl_boot_time = CURRENT_TIME;
1462 nfs4_state_start_reclaim_nograce(clp);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001463 nfs4_schedule_state_manager(clp);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001464 }
1465}
1466
1467static void nfs41_handle_server_reboot(struct nfs_client *clp)
1468{
1469 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1470 nfs4_state_start_reclaim_reboot(clp);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001471 nfs4_schedule_state_manager(clp);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001472 }
1473}
1474
1475static void nfs41_handle_state_revoked(struct nfs_client *clp)
1476{
1477 /* Temporary */
1478 nfs4_reset_all_state(clp);
1479}
1480
1481static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
1482{
1483 /* This will need to handle layouts too */
1484 nfs_expire_all_delegations(clp);
1485}
1486
1487static void nfs41_handle_cb_path_down(struct nfs_client *clp)
1488{
1489 nfs_expire_all_delegations(clp);
1490 if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001491 nfs4_schedule_state_manager(clp);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001492}
1493
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001494void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
1495{
1496 if (!flags)
1497 return;
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001498 else if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
1499 nfs41_handle_server_reboot(clp);
1500 else if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001501 SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
1502 SEQ4_STATUS_ADMIN_STATE_REVOKED |
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001503 SEQ4_STATUS_LEASE_MOVED))
1504 nfs41_handle_state_revoked(clp);
1505 else if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
1506 nfs41_handle_recallable_state_revoked(clp);
1507 else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001508 SEQ4_STATUS_BACKCHANNEL_FAULT |
1509 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001510 nfs41_handle_cb_path_down(clp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001511}
1512
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001513static int nfs4_reset_session(struct nfs_client *clp)
1514{
1515 int status;
1516
Trond Myklebust38045412009-12-15 17:36:57 -05001517 nfs4_begin_drain_session(clp);
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001518 status = nfs4_proc_destroy_session(clp->cl_session);
1519 if (status && status != -NFS4ERR_BADSESSION &&
1520 status != -NFS4ERR_DEADSESSION) {
Ricardo Labiagaf4558482009-12-07 09:16:09 -05001521 status = nfs4_recovery_handle_error(clp, status);
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001522 goto out;
1523 }
1524
1525 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
Trond Myklebustf26468f2009-12-05 19:32:11 -05001526 status = nfs4_proc_create_session(clp);
Andy Adamson41f54a52010-01-21 14:54:13 -05001527 if (status) {
Ricardo Labiagaf4558482009-12-07 09:16:09 -05001528 status = nfs4_recovery_handle_error(clp, status);
Andy Adamson41f54a52010-01-21 14:54:13 -05001529 goto out;
1530 }
1531 /* create_session negotiated new slot table */
1532 clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05001533
Andy Adamson41f54a52010-01-21 14:54:13 -05001534 /* Let the state manager reestablish state */
1535 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001536 nfs41_setup_state_renewal(clp);
Andy Adamson41f54a52010-01-21 14:54:13 -05001537out:
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001538 return status;
1539}
Andy Adamson76db6d92009-04-01 09:22:38 -04001540
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001541static int nfs4_recall_slot(struct nfs_client *clp)
1542{
1543 struct nfs4_slot_table *fc_tbl = &clp->cl_session->fc_slot_table;
1544 struct nfs4_channel_attrs *fc_attrs = &clp->cl_session->fc_attrs;
1545 struct nfs4_slot *new, *old;
1546 int i;
1547
1548 nfs4_begin_drain_session(clp);
1549 new = kmalloc(fc_tbl->target_max_slots * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001550 GFP_NOFS);
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001551 if (!new)
1552 return -ENOMEM;
1553
1554 spin_lock(&fc_tbl->slot_tbl_lock);
1555 for (i = 0; i < fc_tbl->target_max_slots; i++)
1556 new[i].seq_nr = fc_tbl->slots[i].seq_nr;
1557 old = fc_tbl->slots;
1558 fc_tbl->slots = new;
1559 fc_tbl->max_slots = fc_tbl->target_max_slots;
1560 fc_tbl->target_max_slots = 0;
1561 fc_attrs->max_reqs = fc_tbl->max_slots;
1562 spin_unlock(&fc_tbl->slot_tbl_lock);
1563
1564 kfree(old);
1565 nfs4_end_drain_session(clp);
1566 return 0;
1567}
1568
Andy Adamson76db6d92009-04-01 09:22:38 -04001569#else /* CONFIG_NFS_V4_1 */
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001570static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001571static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001572static int nfs4_recall_slot(struct nfs_client *clp) { return 0; }
Andy Adamson76db6d92009-04-01 09:22:38 -04001573#endif /* CONFIG_NFS_V4_1 */
1574
Andy Adamson78722e92009-06-16 14:43:15 -04001575/* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
1576 * on EXCHANGE_ID for v4.1
1577 */
1578static void nfs4_set_lease_expired(struct nfs_client *clp, int status)
1579{
1580 if (nfs4_has_session(clp)) {
1581 switch (status) {
1582 case -NFS4ERR_DELAY:
1583 case -NFS4ERR_CLID_INUSE:
1584 case -EAGAIN:
1585 break;
1586
Trond Myklebust168667c2010-10-19 19:47:49 -04001587 case -EKEYEXPIRED:
1588 nfs4_warn_keyexpired(clp->cl_hostname);
Andy Adamson78722e92009-06-16 14:43:15 -04001589 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1590 * in nfs4_exchange_id */
1591 default:
1592 return;
1593 }
1594 }
1595 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1596}
1597
Trond Myklebuste005e802008-12-23 15:21:48 -05001598static void nfs4_state_manager(struct nfs_client *clp)
Trond Myklebust02860012008-12-23 15:21:40 -05001599{
Trond Myklebust02860012008-12-23 15:21:40 -05001600 int status = 0;
1601
Trond Myklebust02860012008-12-23 15:21:40 -05001602 /* Ensure exclusive access to NFSv4 state */
Trond Myklebustf3c76492008-12-23 15:21:48 -05001603 for(;;) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001604 if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
1605 /* We're going to have to re-establish a clientid */
1606 status = nfs4_reclaim_lease(clp);
1607 if (status) {
Andy Adamson78722e92009-06-16 14:43:15 -04001608 nfs4_set_lease_expired(clp, status);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001609 if (test_bit(NFS4CLNT_LEASE_EXPIRED,
1610 &clp->cl_state))
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001611 continue;
Andy Adamson76db6d92009-04-01 09:22:38 -04001612 if (clp->cl_cons_state ==
1613 NFS_CS_SESSION_INITING)
1614 nfs_mark_client_ready(clp, status);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001615 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
Trond Myklebuste598d842008-12-23 15:21:42 -05001617 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
Ricardo Labiaga7cab89b2009-12-08 14:35:28 -05001618 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
Andy Adamson974cec82010-10-20 00:18:02 -04001619 pnfs_destroy_all_layouts(clp);
Trond Myklebuste598d842008-12-23 15:21:42 -05001620 }
1621
1622 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
1623 status = nfs4_check_lease(clp);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001624 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
Trond Myklebuste598d842008-12-23 15:21:42 -05001625 continue;
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001626 if (status < 0 && status != -NFS4ERR_CB_PATH_DOWN)
1627 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001629
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001630 /* Initialize or reset the session */
Andy Adamson6df08182009-12-04 15:55:05 -05001631 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)
Trond Myklebust7111dc72009-08-24 19:21:29 -04001632 && nfs4_has_session(clp)) {
Andy Adamson4d643d12009-12-04 15:52:24 -05001633 status = nfs4_reset_session(clp);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001634 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1635 continue;
1636 if (status < 0)
Andy Adamson76db6d92009-04-01 09:22:38 -04001637 goto out_error;
Andy Adamson76db6d92009-04-01 09:22:38 -04001638 }
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001639
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001640 /* First recover reboot state... */
Trond Myklebuste345e882009-12-03 15:52:41 -05001641 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
Andy Adamson591d71c2009-04-01 09:22:47 -04001642 status = nfs4_do_reclaim(clp,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001643 clp->cl_mvops->reboot_recovery_ops);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001644 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
Andy Adamson6df08182009-12-04 15:55:05 -05001645 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001646 continue;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001647 nfs4_state_end_reclaim_reboot(clp);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001648 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1649 continue;
1650 if (status < 0)
1651 goto out_error;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001652 }
1653
1654 /* Now recover expired state... */
1655 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
Andy Adamson591d71c2009-04-01 09:22:47 -04001656 status = nfs4_do_reclaim(clp,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001657 clp->cl_mvops->nograce_recovery_ops);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001658 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
Andy Adamson6df08182009-12-04 15:55:05 -05001659 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001660 test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1661 continue;
1662 if (status < 0)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001663 goto out_error;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001664 }
Trond Myklebust707fb4b2008-12-23 15:21:47 -05001665
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001666 nfs4_end_drain_session(clp);
Trond Myklebust707fb4b2008-12-23 15:21:47 -05001667 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
1668 nfs_client_return_marked_delegations(clp);
1669 continue;
1670 }
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001671 /* Recall session slots */
1672 if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state)
1673 && nfs4_has_session(clp)) {
1674 status = nfs4_recall_slot(clp);
1675 if (status < 0)
1676 goto out_error;
1677 continue;
1678 }
1679
Trond Myklebuste005e802008-12-23 15:21:48 -05001680
1681 nfs4_clear_state_manager_bit(clp);
Trond Myklebustf3c76492008-12-23 15:21:48 -05001682 /* Did we race with an attempt to give us more work? */
1683 if (clp->cl_state == 0)
1684 break;
1685 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1686 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
Trond Myklebuste005e802008-12-23 15:21:48 -05001688 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689out_error:
Trond Myklebuste005e802008-12-23 15:21:48 -05001690 printk(KERN_WARNING "Error: state manager failed on NFSv4 server %s"
Chuck Lever5d8515c2007-12-10 14:57:16 -05001691 " with error %d\n", clp->cl_hostname, -status);
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001692 nfs4_end_drain_session(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05001693 nfs4_clear_state_manager_bit(clp);
1694}
1695
1696static int nfs4_run_state_manager(void *ptr)
1697{
1698 struct nfs_client *clp = ptr;
1699
1700 allow_signal(SIGKILL);
1701 nfs4_state_manager(clp);
1702 nfs_put_client(clp);
1703 module_put_and_exit(0);
1704 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
1707/*
1708 * Local variables:
1709 * c-basic-offset: 8
1710 * End:
1711 */