blob: 2336d532cf66c3677441871842fb7b9ec31a9aad [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;
Andy Adamson2c2618c2011-01-06 02:04:31 +0000235 status = nfs4_set_callback_sessionid(clp);
236 if (status != 0) {
237 printk(KERN_WARNING "Sessionid not set. No callback service\n");
238 nfs_callback_down(1);
239 status = 0;
240 }
Ricardo Labiaga9430fb62009-12-06 12:23:46 -0500241 nfs41_setup_state_renewal(clp);
242 nfs_mark_client_ready(clp, NFS_CS_READY);
243out:
Andy Adamson4d643d12009-12-04 15:52:24 -0500244 return status;
245}
246
Andy Adamsonb4b82602009-04-01 09:22:49 -0400247struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp)
248{
249 struct rpc_cred *cred;
250
251 spin_lock(&clp->cl_lock);
252 cred = nfs4_get_machine_cred_locked(clp);
253 spin_unlock(&clp->cl_lock);
254 return cred;
255}
256
257#endif /* CONFIG_NFS_V4_1 */
258
Chuck Lever24d292b2010-12-24 01:32:43 +0000259static struct rpc_cred *
260nfs4_get_setclientid_cred_server(struct nfs_server *server)
Trond Myklebust286d7d62006-01-03 09:55:26 +0100261{
Chuck Lever24d292b2010-12-24 01:32:43 +0000262 struct nfs_client *clp = server->nfs_client;
263 struct rpc_cred *cred = NULL;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100264 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400265 struct rb_node *pos;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100266
Trond Myklebust6dc9d572008-12-23 15:21:41 -0500267 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000268 pos = rb_first(&server->state_owners);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400269 if (pos != NULL) {
Chuck Lever24d292b2010-12-24 01:32:43 +0000270 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400271 cred = get_rpccred(sp->so_cred);
Trond Myklebust286d7d62006-01-03 09:55:26 +0100272 }
Trond Myklebust6dc9d572008-12-23 15:21:41 -0500273 spin_unlock(&clp->cl_lock);
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400274 return cred;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100275}
276
Chuck Lever24d292b2010-12-24 01:32:43 +0000277/**
278 * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation
279 * @clp: client state handle
280 *
281 * Returns an rpc_cred with reference count bumped, or NULL.
282 */
283struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
284{
285 struct nfs_server *server;
286 struct rpc_cred *cred;
287
288 spin_lock(&clp->cl_lock);
289 cred = nfs4_get_machine_cred_locked(clp);
290 spin_unlock(&clp->cl_lock);
291 if (cred != NULL)
292 goto out;
293
294 rcu_read_lock();
295 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
296 cred = nfs4_get_setclientid_cred_server(server);
297 if (cred != NULL)
298 break;
299 }
300 rcu_read_unlock();
301
302out:
303 return cred;
304}
305
306static void nfs_alloc_unique_id_locked(struct rb_root *root,
307 struct nfs_unique_id *new,
308 __u64 minval, int maxbits)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400309{
310 struct rb_node **p, *parent;
311 struct nfs_unique_id *pos;
312 __u64 mask = ~0ULL;
313
314 if (maxbits < 64)
315 mask = (1ULL << maxbits) - 1ULL;
316
317 /* Ensure distribution is more or less flat */
318 get_random_bytes(&new->id, sizeof(new->id));
319 new->id &= mask;
320 if (new->id < minval)
321 new->id += minval;
322retry:
323 p = &root->rb_node;
324 parent = NULL;
325
326 while (*p != NULL) {
327 parent = *p;
328 pos = rb_entry(parent, struct nfs_unique_id, rb_node);
329
330 if (new->id < pos->id)
331 p = &(*p)->rb_left;
332 else if (new->id > pos->id)
333 p = &(*p)->rb_right;
334 else
335 goto id_exists;
336 }
337 rb_link_node(&new->rb_node, parent, p);
338 rb_insert_color(&new->rb_node, root);
339 return;
340id_exists:
341 for (;;) {
342 new->id++;
343 if (new->id < minval || (new->id & mask) != new->id) {
344 new->id = minval;
345 break;
346 }
347 parent = rb_next(parent);
348 if (parent == NULL)
349 break;
350 pos = rb_entry(parent, struct nfs_unique_id, rb_node);
351 if (new->id < pos->id)
352 break;
353 }
354 goto retry;
355}
356
357static void nfs_free_unique_id(struct rb_root *root, struct nfs_unique_id *id)
358{
359 rb_erase(&id->rb_node, root);
360}
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362static struct nfs4_state_owner *
Chuck Lever24d292b2010-12-24 01:32:43 +0000363nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Chuck Lever24d292b2010-12-24 01:32:43 +0000365 struct rb_node **p = &server->state_owners.rb_node,
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400366 *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct nfs4_state_owner *sp, *res = NULL;
368
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400369 while (*p != NULL) {
370 parent = *p;
Chuck Lever24d292b2010-12-24 01:32:43 +0000371 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400372
Trond Myklebust6f2e64d2007-07-06 10:53:21 -0400373 if (server < sp->so_server) {
374 p = &parent->rb_left;
375 continue;
376 }
377 if (server > sp->so_server) {
378 p = &parent->rb_right;
379 continue;
380 }
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400381 if (cred < sp->so_cred)
382 p = &parent->rb_left;
383 else if (cred > sp->so_cred)
384 p = &parent->rb_right;
385 else {
386 atomic_inc(&sp->so_count);
387 res = sp;
388 break;
389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391 return res;
392}
393
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400394static struct nfs4_state_owner *
Chuck Lever24d292b2010-12-24 01:32:43 +0000395nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400396{
Chuck Lever24d292b2010-12-24 01:32:43 +0000397 struct nfs_server *server = new->so_server;
398 struct rb_node **p = &server->state_owners.rb_node,
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400399 *parent = NULL;
400 struct nfs4_state_owner *sp;
401
402 while (*p != NULL) {
403 parent = *p;
Chuck Lever24d292b2010-12-24 01:32:43 +0000404 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400405
406 if (new->so_cred < sp->so_cred)
407 p = &parent->rb_left;
408 else if (new->so_cred > sp->so_cred)
409 p = &parent->rb_right;
410 else {
411 atomic_inc(&sp->so_count);
412 return sp;
413 }
414 }
Chuck Lever24d292b2010-12-24 01:32:43 +0000415 nfs_alloc_unique_id_locked(&server->openowner_id,
416 &new->so_owner_id, 1, 64);
417 rb_link_node(&new->so_server_node, parent, p);
418 rb_insert_color(&new->so_server_node, &server->state_owners);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400419 return new;
420}
421
422static void
Chuck Lever24d292b2010-12-24 01:32:43 +0000423nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400424{
Chuck Lever24d292b2010-12-24 01:32:43 +0000425 struct nfs_server *server = sp->so_server;
426
427 if (!RB_EMPTY_NODE(&sp->so_server_node))
428 rb_erase(&sp->so_server_node, &server->state_owners);
429 nfs_free_unique_id(&server->openowner_id, &sp->so_owner_id);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400430}
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432/*
433 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
434 * create a new state_owner.
435 *
436 */
437static struct nfs4_state_owner *
438nfs4_alloc_state_owner(void)
439{
440 struct nfs4_state_owner *sp;
441
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400442 sp = kzalloc(sizeof(*sp),GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (!sp)
444 return NULL;
Trond Myklebustec073422005-10-20 14:22:47 -0700445 spin_lock_init(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 INIT_LIST_HEAD(&sp->so_states);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700447 rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
448 sp->so_seqid.sequence = &sp->so_sequence;
449 spin_lock_init(&sp->so_sequence.lock);
450 INIT_LIST_HEAD(&sp->so_sequence.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 atomic_set(&sp->so_count, 1);
452 return sp;
453}
454
Adrian Bunk1d2e88e2008-05-02 13:42:45 -0700455static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456nfs4_drop_state_owner(struct nfs4_state_owner *sp)
457{
Chuck Lever24d292b2010-12-24 01:32:43 +0000458 if (!RB_EMPTY_NODE(&sp->so_server_node)) {
459 struct nfs_server *server = sp->so_server;
460 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400461
462 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000463 rb_erase(&sp->so_server_node, &server->state_owners);
464 RB_CLEAR_NODE(&sp->so_server_node);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400465 spin_unlock(&clp->cl_lock);
466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Chuck Lever24d292b2010-12-24 01:32:43 +0000469/**
470 * nfs4_get_state_owner - Look up a state owner given a credential
471 * @server: nfs_server to search
472 * @cred: RPC credential to match
473 *
474 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
475 */
476struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
477 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
David Howells7539bba2006-08-22 20:06:09 -0400479 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct nfs4_state_owner *sp, *new;
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000483 sp = nfs4_find_state_owner_locked(server, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 spin_unlock(&clp->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (sp != NULL)
486 return sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400487 new = nfs4_alloc_state_owner();
488 if (new == NULL)
489 return NULL;
Trond Myklebust6f2e64d2007-07-06 10:53:21 -0400490 new->so_server = server;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400491 new->so_cred = cred;
492 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000493 sp = nfs4_insert_state_owner_locked(new);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400494 spin_unlock(&clp->cl_lock);
495 if (sp == new)
496 get_rpccred(cred);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500497 else {
498 rpc_destroy_wait_queue(&new->so_sequence.wait);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400499 kfree(new);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500500 }
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400501 return sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Chuck Lever24d292b2010-12-24 01:32:43 +0000504/**
505 * nfs4_put_state_owner - Release a nfs4_state_owner
506 * @sp: state owner data to release
507 *
508 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509void nfs4_put_state_owner(struct nfs4_state_owner *sp)
510{
Trond Myklebust1f0e8902010-06-24 15:11:43 -0400511 struct nfs_client *clp = sp->so_server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 struct rpc_cred *cred = sp->so_cred;
513
514 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
515 return;
Chuck Lever24d292b2010-12-24 01:32:43 +0000516 nfs4_remove_state_owner_locked(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 spin_unlock(&clp->cl_lock);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500518 rpc_destroy_wait_queue(&sp->so_sequence.wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 put_rpccred(cred);
520 kfree(sp);
521}
522
523static struct nfs4_state *
524nfs4_alloc_open_state(void)
525{
526 struct nfs4_state *state;
527
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400528 state = kzalloc(sizeof(*state), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (!state)
530 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 atomic_set(&state->count, 1);
532 INIT_LIST_HEAD(&state->lock_states);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000533 spin_lock_init(&state->state_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400534 seqlock_init(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return state;
536}
537
Trond Myklebust4cecb762005-11-04 15:32:58 -0500538void
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500539nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
Trond Myklebust4cecb762005-11-04 15:32:58 -0500540{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500541 if (state->state == fmode)
Trond Myklebust4cecb762005-11-04 15:32:58 -0500542 return;
543 /* NB! List reordering - see the reclaim code for why. */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500544 if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
545 if (fmode & FMODE_WRITE)
Trond Myklebust4cecb762005-11-04 15:32:58 -0500546 list_move(&state->open_states, &state->owner->so_states);
547 else
548 list_move_tail(&state->open_states, &state->owner->so_states);
549 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500550 state->state = fmode;
Trond Myklebust4cecb762005-11-04 15:32:58 -0500551}
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553static struct nfs4_state *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554__nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
555{
556 struct nfs_inode *nfsi = NFS_I(inode);
557 struct nfs4_state *state;
558
559 list_for_each_entry(state, &nfsi->open_states, inode_states) {
Trond Myklebust1c816ef2007-07-03 14:41:19 -0400560 if (state->owner != owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 continue;
Trond Myklebust1c816ef2007-07-03 14:41:19 -0400562 if (atomic_inc_not_zero(&state->count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565 return NULL;
566}
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568static void
569nfs4_free_open_state(struct nfs4_state *state)
570{
571 kfree(state);
572}
573
574struct nfs4_state *
575nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
576{
577 struct nfs4_state *state, *new;
578 struct nfs_inode *nfsi = NFS_I(inode);
579
580 spin_lock(&inode->i_lock);
581 state = __nfs4_find_state_byowner(inode, owner);
582 spin_unlock(&inode->i_lock);
583 if (state)
584 goto out;
585 new = nfs4_alloc_open_state();
Trond Myklebustec073422005-10-20 14:22:47 -0700586 spin_lock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 spin_lock(&inode->i_lock);
588 state = __nfs4_find_state_byowner(inode, owner);
589 if (state == NULL && new != NULL) {
590 state = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 state->owner = owner;
592 atomic_inc(&owner->so_count);
593 list_add(&state->inode_states, &nfsi->open_states);
594 state->inode = igrab(inode);
595 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700596 /* Note: The reclaim code dictates that we add stateless
597 * and read-only stateids to the end of the list */
598 list_add_tail(&state->open_states, &owner->so_states);
599 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 } else {
601 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700602 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (new)
604 nfs4_free_open_state(new);
605 }
606out:
607 return state;
608}
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610void nfs4_put_open_state(struct nfs4_state *state)
611{
612 struct inode *inode = state->inode;
613 struct nfs4_state_owner *owner = state->owner;
614
Trond Myklebustec073422005-10-20 14:22:47 -0700615 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return;
Trond Myklebustec073422005-10-20 14:22:47 -0700617 spin_lock(&inode->i_lock);
Trond Myklebustba6830312007-07-27 10:23:05 -0400618 list_del(&state->inode_states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 list_del(&state->open_states);
Trond Myklebustec073422005-10-20 14:22:47 -0700620 spin_unlock(&inode->i_lock);
621 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 nfs4_free_open_state(state);
624 nfs4_put_state_owner(owner);
625}
626
627/*
Trond Myklebust83c9d412005-10-18 14:20:13 -0700628 * Close the current file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 */
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400630static void __nfs4_close(struct path *path, struct nfs4_state *state,
631 fmode_t fmode, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 struct nfs4_state_owner *owner = state->owner;
Trond Myklebust003707c2007-07-05 18:07:55 -0400634 int call_close = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500635 fmode_t newstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 atomic_inc(&owner->so_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /* Protect against nfs4_find_state() */
Trond Myklebustec073422005-10-20 14:22:47 -0700639 spin_lock(&owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500640 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100641 case FMODE_READ:
642 state->n_rdonly--;
643 break;
644 case FMODE_WRITE:
645 state->n_wronly--;
646 break;
647 case FMODE_READ|FMODE_WRITE:
648 state->n_rdwr--;
649 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400650 newstate = FMODE_READ|FMODE_WRITE;
Trond Myklebuste7616922006-01-03 09:55:13 +0100651 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400652 if (state->n_rdonly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100653 newstate &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -0400654 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
655 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
656 }
657 if (state->n_wronly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100658 newstate &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -0400659 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
660 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
661 }
662 if (newstate == 0)
663 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebuste7616922006-01-03 09:55:13 +0100664 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400665 nfs4_state_set_mode_locked(state, newstate);
Trond Myklebustec073422005-10-20 14:22:47 -0700666 spin_unlock(&owner->so_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500667
Trond Myklebust003707c2007-07-05 18:07:55 -0400668 if (!call_close) {
Trond Myklebustb39e6252007-06-11 23:05:07 -0400669 nfs4_put_open_state(state);
670 nfs4_put_state_owner(owner);
Fred Isamanf7e89172011-01-06 11:36:32 +0000671 } else {
672 bool roc = pnfs_roc(state->inode);
673
674 nfs4_do_close(path, state, gfp_mask, wait, roc);
675 }
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400676}
677
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500678void nfs4_close_state(struct path *path, struct nfs4_state *state, fmode_t fmode)
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400679{
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400680 __nfs4_close(path, state, fmode, GFP_NOFS, 0);
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400681}
682
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500683void nfs4_close_sync(struct path *path, struct nfs4_state *state, fmode_t fmode)
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400684{
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400685 __nfs4_close(path, state, fmode, GFP_KERNEL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688/*
689 * Search the state->lock_states for an existing lock_owner
690 * that is compatible with current->files
691 */
692static struct nfs4_lock_state *
Trond Myklebust77041ed2010-07-01 12:49:11 -0400693__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 -0700694{
695 struct nfs4_lock_state *pos;
696 list_for_each_entry(pos, &state->lock_states, ls_locks) {
Trond Myklebust77041ed2010-07-01 12:49:11 -0400697 if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 continue;
Trond Myklebust77041ed2010-07-01 12:49:11 -0400699 switch (pos->ls_owner.lo_type) {
700 case NFS4_POSIX_LOCK_TYPE:
701 if (pos->ls_owner.lo_u.posix_owner != fl_owner)
702 continue;
703 break;
704 case NFS4_FLOCK_LOCK_TYPE:
705 if (pos->ls_owner.lo_u.flock_owner != fl_pid)
706 continue;
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 atomic_inc(&pos->ls_count);
709 return pos;
710 }
711 return NULL;
712}
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714/*
715 * Return a compatible lock_state. If no initialized lock_state structure
716 * exists, return an uninitialized one.
717 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 */
Trond Myklebust77041ed2010-07-01 12:49:11 -0400719static 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 -0700720{
721 struct nfs4_lock_state *lsp;
Chuck Lever24d292b2010-12-24 01:32:43 +0000722 struct nfs_server *server = state->owner->so_server;
723 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400725 lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (lsp == NULL)
727 return NULL;
Trond Myklebustd0dc3702008-01-10 16:07:54 -0500728 rpc_init_wait_queue(&lsp->ls_sequence.wait, "lock_seqid_waitqueue");
729 spin_lock_init(&lsp->ls_sequence.lock);
730 INIT_LIST_HEAD(&lsp->ls_sequence.list);
731 lsp->ls_seqid.sequence = &lsp->ls_sequence;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 atomic_set(&lsp->ls_count, 1);
Trond Myklebustb64aec82009-07-21 16:47:46 -0400733 lsp->ls_state = state;
Trond Myklebust77041ed2010-07-01 12:49:11 -0400734 lsp->ls_owner.lo_type = type;
735 switch (lsp->ls_owner.lo_type) {
736 case NFS4_FLOCK_LOCK_TYPE:
737 lsp->ls_owner.lo_u.flock_owner = fl_pid;
738 break;
739 case NFS4_POSIX_LOCK_TYPE:
740 lsp->ls_owner.lo_u.posix_owner = fl_owner;
741 break;
742 default:
743 kfree(lsp);
744 return NULL;
745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000747 nfs_alloc_unique_id_locked(&server->lockowner_id, &lsp->ls_id, 1, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 spin_unlock(&clp->cl_lock);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000749 INIT_LIST_HEAD(&lsp->ls_locks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return lsp;
751}
752
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400753static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
754{
Chuck Lever24d292b2010-12-24 01:32:43 +0000755 struct nfs_server *server = lsp->ls_state->owner->so_server;
756 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400757
758 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000759 nfs_free_unique_id(&server->lockowner_id, &lsp->ls_id);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400760 spin_unlock(&clp->cl_lock);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500761 rpc_destroy_wait_queue(&lsp->ls_sequence.wait);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400762 kfree(lsp);
763}
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765/*
766 * Return a compatible lock_state. If no initialized lock_state structure
767 * exists, return an uninitialized one.
768 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 */
Trond Myklebust77041ed2010-07-01 12:49:11 -0400770static 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 -0700771{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000772 struct nfs4_lock_state *lsp, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000774 for(;;) {
775 spin_lock(&state->state_lock);
Trond Myklebust77041ed2010-07-01 12:49:11 -0400776 lsp = __nfs4_find_lock_state(state, owner, pid, type);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000777 if (lsp != NULL)
778 break;
779 if (new != NULL) {
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000780 list_add(&new->ls_locks, &state->lock_states);
781 set_bit(LK_STATE_IN_USE, &state->flags);
782 lsp = new;
783 new = NULL;
784 break;
785 }
786 spin_unlock(&state->state_lock);
Trond Myklebust77041ed2010-07-01 12:49:11 -0400787 new = nfs4_alloc_lock_state(state, owner, pid, type);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000788 if (new == NULL)
789 return NULL;
790 }
791 spin_unlock(&state->state_lock);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400792 if (new != NULL)
793 nfs4_free_lock_state(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return lsp;
795}
796
797/*
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000798 * Release reference to lock_state, and free it if we see that
799 * it is no longer in use
800 */
Trond Myklebustfaf5f492005-10-18 14:20:15 -0700801void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000802{
803 struct nfs4_state *state;
804
805 if (lsp == NULL)
806 return;
807 state = lsp->ls_state;
808 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
809 return;
810 list_del(&lsp->ls_locks);
811 if (list_empty(&state->lock_states))
812 clear_bit(LK_STATE_IN_USE, &state->flags);
813 spin_unlock(&state->state_lock);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -0400814 if (lsp->ls_flags & NFS_LOCK_INITIALIZED)
815 nfs4_release_lockowner(lsp);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400816 nfs4_free_lock_state(lsp);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000817}
818
819static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
820{
821 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
822
823 dst->fl_u.nfs4_fl.owner = lsp;
824 atomic_inc(&lsp->ls_count);
825}
826
827static void nfs4_fl_release_lock(struct file_lock *fl)
828{
829 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
830}
831
Alexey Dobriyan6aed6282009-09-21 17:01:11 -0700832static const struct file_lock_operations nfs4_fl_lock_ops = {
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000833 .fl_copy_lock = nfs4_fl_copy_lock,
834 .fl_release_private = nfs4_fl_release_lock,
835};
836
837int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
838{
839 struct nfs4_lock_state *lsp;
840
841 if (fl->fl_ops != NULL)
842 return 0;
Trond Myklebust77041ed2010-07-01 12:49:11 -0400843 if (fl->fl_flags & FL_POSIX)
844 lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
845 else if (fl->fl_flags & FL_FLOCK)
846 lsp = nfs4_get_lock_state(state, 0, fl->fl_pid, NFS4_FLOCK_LOCK_TYPE);
847 else
848 return -EINVAL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000849 if (lsp == NULL)
850 return -ENOMEM;
851 fl->fl_u.nfs4_fl.owner = lsp;
852 fl->fl_ops = &nfs4_fl_lock_ops;
853 return 0;
854}
855
856/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 * Byte-range lock aware utility to initialize the stateid of read/write
858 * requests.
859 */
Trond Myklebust77041ed2010-07-01 12:49:11 -0400860void 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 -0700861{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000862 struct nfs4_lock_state *lsp;
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400863 int seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400865 do {
866 seq = read_seqbegin(&state->seqlock);
867 memcpy(dst, &state->stateid, sizeof(*dst));
868 } while (read_seqretry(&state->seqlock, seq));
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000869 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
870 return;
871
872 spin_lock(&state->state_lock);
Trond Myklebust77041ed2010-07-01 12:49:11 -0400873 lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000874 if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
875 memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
876 spin_unlock(&state->state_lock);
877 nfs4_put_lock_state(lsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400880struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700882 struct nfs_seqid *new;
883
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400884 new = kmalloc(sizeof(*new), gfp_mask);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700885 if (new != NULL) {
886 new->sequence = counter;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500887 INIT_LIST_HEAD(&new->list);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700888 }
889 return new;
890}
891
Trond Myklebust72211db2009-12-15 14:47:36 -0500892void nfs_release_seqid(struct nfs_seqid *seqid)
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700893{
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500894 if (!list_empty(&seqid->list)) {
895 struct rpc_sequence *sequence = seqid->sequence->sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700896
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500897 spin_lock(&sequence->lock);
Trond Myklebust72211db2009-12-15 14:47:36 -0500898 list_del_init(&seqid->list);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500899 spin_unlock(&sequence->lock);
900 rpc_wake_up(&sequence->wait);
901 }
Trond Myklebust72211db2009-12-15 14:47:36 -0500902}
903
904void nfs_free_seqid(struct nfs_seqid *seqid)
905{
906 nfs_release_seqid(seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700907 kfree(seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700911 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
912 * failed with a seqid incrementing error -
913 * see comments nfs_fs.h:seqid_mutating_error()
914 */
Trond Myklebust88d90932007-07-02 14:03:03 -0400915static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500917 BUG_ON(list_first_entry(&seqid->sequence->sequence->list, struct nfs_seqid, list) != seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700918 switch (status) {
919 case 0:
920 break;
921 case -NFS4ERR_BAD_SEQID:
Trond Myklebust6f43ddc2007-07-08 16:49:11 -0400922 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
923 return;
924 printk(KERN_WARNING "NFS: v4 server returned a bad"
Dan Muntz497799e2008-02-13 13:09:35 -0800925 " sequence-id error on an"
926 " unconfirmed sequence %p!\n",
Trond Myklebust6f43ddc2007-07-08 16:49:11 -0400927 seqid->sequence);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700928 case -NFS4ERR_STALE_CLIENTID:
929 case -NFS4ERR_STALE_STATEID:
930 case -NFS4ERR_BAD_STATEID:
931 case -NFS4ERR_BADXDR:
932 case -NFS4ERR_RESOURCE:
933 case -NFS4ERR_NOFILEHANDLE:
934 /* Non-seqid mutating errors */
935 return;
936 };
937 /*
938 * Note: no locking needed as we are guaranteed to be first
939 * on the sequence list
940 */
941 seqid->sequence->counter++;
942}
943
944void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
945{
Benny Halevy34dc1ad2009-04-01 09:22:52 -0400946 struct nfs4_state_owner *sp = container_of(seqid->sequence,
947 struct nfs4_state_owner, so_seqid);
948 struct nfs_server *server = sp->so_server;
949
950 if (status == -NFS4ERR_BAD_SEQID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 nfs4_drop_state_owner(sp);
Benny Halevy34dc1ad2009-04-01 09:22:52 -0400952 if (!nfs4_has_session(server->nfs_client))
953 nfs_increment_seqid(status, seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700954}
955
956/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700957 * Increment the seqid if the LOCK/LOCKU succeeded, or
958 * failed with a seqid incrementing error -
959 * see comments nfs_fs.h:seqid_mutating_error()
960 */
961void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
962{
Trond Myklebust88d90932007-07-02 14:03:03 -0400963 nfs_increment_seqid(status, seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700964}
965
966int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
967{
968 struct rpc_sequence *sequence = seqid->sequence->sequence;
969 int status = 0;
970
971 spin_lock(&sequence->lock);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500972 if (list_empty(&seqid->list))
973 list_add_tail(&seqid->list, &sequence->list);
974 if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
975 goto unlock;
Trond Myklebust5d008372008-02-22 16:34:17 -0500976 rpc_sleep_on(&sequence->wait, task, NULL);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500977 status = -EAGAIN;
978unlock:
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700979 spin_unlock(&sequence->lock);
980 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
Trond Myklebuste005e802008-12-23 15:21:48 -0500983static int nfs4_run_state_manager(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Trond Myklebuste005e802008-12-23 15:21:48 -0500985static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
Trond Myklebust433fbe42006-01-03 09:55:22 +0100986{
987 smp_mb__before_clear_bit();
Trond Myklebuste005e802008-12-23 15:21:48 -0500988 clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
Trond Myklebust433fbe42006-01-03 09:55:22 +0100989 smp_mb__after_clear_bit();
Trond Myklebuste005e802008-12-23 15:21:48 -0500990 wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
Trond Myklebust433fbe42006-01-03 09:55:22 +0100991 rpc_wake_up(&clp->cl_rpcwaitq);
992}
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994/*
Trond Myklebuste005e802008-12-23 15:21:48 -0500995 * Schedule the nfs_client asynchronous state management routine
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 */
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500997void nfs4_schedule_state_manager(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Trond Myklebust5043e902006-01-03 09:55:23 +0100999 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Trond Myklebuste005e802008-12-23 15:21:48 -05001001 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1002 return;
Trond Myklebust5043e902006-01-03 09:55:23 +01001003 __module_get(THIS_MODULE);
1004 atomic_inc(&clp->cl_count);
Trond Myklebuste005e802008-12-23 15:21:48 -05001005 task = kthread_run(nfs4_run_state_manager, clp, "%s-manager",
Chuck Lever5d8515c2007-12-10 14:57:16 -05001006 rpc_peeraddr2str(clp->cl_rpcclient,
1007 RPC_DISPLAY_ADDR));
Trond Myklebust5043e902006-01-03 09:55:23 +01001008 if (!IS_ERR(task))
1009 return;
Trond Myklebuste005e802008-12-23 15:21:48 -05001010 nfs4_clear_state_manager_bit(clp);
David Howells24c8dbb2006-08-22 20:06:10 -04001011 nfs_put_client(clp);
Trond Myklebust5043e902006-01-03 09:55:23 +01001012 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
1015/*
1016 * Schedule a state recovery attempt
1017 */
David Howellsadfa6f92006-08-22 20:06:08 -04001018void nfs4_schedule_state_recovery(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 if (!clp)
1021 return;
Trond Myklebuste598d842008-12-23 15:21:42 -05001022 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1023 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
Trond Myklebuste005e802008-12-23 15:21:48 -05001024 nfs4_schedule_state_manager(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05001027int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001028{
1029
1030 set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1031 /* Don't recover state that expired before the reboot */
1032 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1033 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1034 return 0;
1035 }
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001036 set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001037 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1038 return 1;
1039}
1040
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001041int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001042{
1043 set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1044 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001045 set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001046 set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1047 return 1;
1048}
1049
Trond Myklebust02860012008-12-23 15:21:40 -05001050static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 struct inode *inode = state->inode;
Trond Myklebust19e03c52008-12-23 15:21:44 -05001053 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 struct file_lock *fl;
1055 int status = 0;
1056
Trond Myklebust3f09df72009-06-17 13:23:00 -07001057 if (inode->i_flock == NULL)
1058 return 0;
1059
1060 /* Guard against delegation returns and new lock/unlock calls */
Trond Myklebust19e03c52008-12-23 15:21:44 -05001061 down_write(&nfsi->rwsem);
Trond Myklebust3f09df72009-06-17 13:23:00 -07001062 /* Protect inode->i_flock using the BKL */
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001063 lock_flocks();
Harvey Harrison90dc7d22008-02-20 13:03:05 -08001064 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
Trond Myklebust43b2a332005-11-04 15:35:30 -05001065 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 continue;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001067 if (nfs_file_open_context(fl->fl_file)->state != state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 continue;
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001069 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 status = ops->recover_lock(state, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 switch (status) {
Trond Myklebust965b5d62009-06-17 13:22:59 -07001072 case 0:
1073 break;
1074 case -ESTALE:
1075 case -NFS4ERR_ADMIN_REVOKED:
1076 case -NFS4ERR_STALE_STATEID:
1077 case -NFS4ERR_BAD_STATEID:
1078 case -NFS4ERR_EXPIRED:
1079 case -NFS4ERR_NO_GRACE:
1080 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust9c4c7612009-12-03 15:58:56 -05001081 case -NFS4ERR_BADSESSION:
1082 case -NFS4ERR_BADSLOT:
1083 case -NFS4ERR_BAD_HIGH_SLOT:
1084 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001085 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 default:
1087 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001088 __func__, status);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001089 case -ENOMEM:
1090 case -NFS4ERR_DENIED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 case -NFS4ERR_RECLAIM_BAD:
1092 case -NFS4ERR_RECLAIM_CONFLICT:
Trond Myklebust43b2a332005-11-04 15:35:30 -05001093 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001094 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001096 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001098 unlock_flocks();
Trond Myklebust965b5d62009-06-17 13:22:59 -07001099out:
Trond Myklebust19e03c52008-12-23 15:21:44 -05001100 up_write(&nfsi->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return status;
1102}
1103
Trond Myklebust02860012008-12-23 15:21:40 -05001104static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
1106 struct nfs4_state *state;
1107 struct nfs4_lock_state *lock;
1108 int status = 0;
1109
1110 /* Note: we rely on the sp->so_states list being ordered
1111 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1112 * states first.
1113 * This is needed to ensure that the server won't give us any
1114 * read delegations that we have to return if, say, we are
1115 * recovering after a network partition or a reboot from a
1116 * server that doesn't support a grace period.
1117 */
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001118restart:
1119 spin_lock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 list_for_each_entry(state, &sp->so_states, open_states) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001121 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1122 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (state->state == 0)
1124 continue;
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001125 atomic_inc(&state->count);
1126 spin_unlock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 status = ops->recover_open(sp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (status >= 0) {
Trond Myklebust02860012008-12-23 15:21:40 -05001129 status = nfs4_reclaim_locks(state, ops);
1130 if (status >= 0) {
1131 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1132 if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
1133 printk("%s: Lock reclaim failed!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001134 __func__);
Trond Myklebust02860012008-12-23 15:21:40 -05001135 }
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001136 nfs4_put_open_state(state);
1137 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140 switch (status) {
1141 default:
1142 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001143 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 case -ENOENT:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001145 case -ENOMEM:
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001146 case -ESTALE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 /*
1148 * Open state on this file cannot be recovered
1149 * All we can do is revert to using the zero stateid.
1150 */
1151 memset(state->stateid.data, 0,
1152 sizeof(state->stateid.data));
1153 /* Mark the file as being 'closed' */
1154 state->state = 0;
1155 break;
Trond Myklebust168667c2010-10-19 19:47:49 -04001156 case -EKEYEXPIRED:
1157 /*
1158 * User RPCSEC_GSS context has expired.
1159 * We cannot recover this stateid now, so
1160 * skip it and allow recovery thread to
1161 * proceed.
1162 */
1163 break;
Trond Myklebust965b5d62009-06-17 13:22:59 -07001164 case -NFS4ERR_ADMIN_REVOKED:
1165 case -NFS4ERR_STALE_STATEID:
1166 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001167 case -NFS4ERR_RECLAIM_BAD:
1168 case -NFS4ERR_RECLAIM_CONFLICT:
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001169 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001170 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 case -NFS4ERR_EXPIRED:
1172 case -NFS4ERR_NO_GRACE:
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001173 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust9c4c7612009-12-03 15:58:56 -05001175 case -NFS4ERR_BADSESSION:
1176 case -NFS4ERR_BADSLOT:
1177 case -NFS4ERR_BAD_HIGH_SLOT:
1178 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 goto out_err;
1180 }
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001181 nfs4_put_open_state(state);
1182 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001184 spin_unlock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return 0;
1186out_err:
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001187 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return status;
1189}
1190
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001191static void nfs4_clear_open_state(struct nfs4_state *state)
1192{
1193 struct nfs4_lock_state *lock;
1194
1195 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1196 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1197 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1198 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1199 list_for_each_entry(lock, &state->lock_states, ls_locks) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001200 lock->ls_seqid.flags = 0;
1201 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
1202 }
1203}
1204
Chuck Lever24d292b2010-12-24 01:32:43 +00001205static void nfs4_reset_seqids(struct nfs_server *server,
1206 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001207{
Chuck Lever24d292b2010-12-24 01:32:43 +00001208 struct nfs_client *clp = server->nfs_client;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001209 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001210 struct rb_node *pos;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001211 struct nfs4_state *state;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001212
Chuck Lever24d292b2010-12-24 01:32:43 +00001213 spin_lock(&clp->cl_lock);
1214 for (pos = rb_first(&server->state_owners);
1215 pos != NULL;
1216 pos = rb_next(pos)) {
1217 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001218 sp->so_seqid.flags = 0;
Trond Myklebustec073422005-10-20 14:22:47 -07001219 spin_lock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001220 list_for_each_entry(state, &sp->so_states, open_states) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001221 if (mark_reclaim(clp, state))
1222 nfs4_clear_open_state(state);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001223 }
Trond Myklebustec073422005-10-20 14:22:47 -07001224 spin_unlock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001225 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001226 spin_unlock(&clp->cl_lock);
1227}
1228
1229static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1230 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1231{
1232 struct nfs_server *server;
1233
1234 rcu_read_lock();
1235 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1236 nfs4_reset_seqids(server, mark_reclaim);
1237 rcu_read_unlock();
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001238}
1239
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001240static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1241{
1242 /* Mark all delegations for reclaim */
1243 nfs_delegation_mark_reclaim(clp);
1244 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1245}
1246
Ricardo Labiagafce5c832009-12-05 16:08:41 -05001247static void nfs4_reclaim_complete(struct nfs_client *clp,
1248 const struct nfs4_state_recovery_ops *ops)
1249{
1250 /* Notify the server we're done reclaiming our state */
1251 if (ops->reclaim_complete)
1252 (void)ops->reclaim_complete(clp);
1253}
1254
Chuck Lever24d292b2010-12-24 01:32:43 +00001255static void nfs4_clear_reclaim_server(struct nfs_server *server)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001256{
Chuck Lever24d292b2010-12-24 01:32:43 +00001257 struct nfs_client *clp = server->nfs_client;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001258 struct nfs4_state_owner *sp;
1259 struct rb_node *pos;
1260 struct nfs4_state *state;
1261
Chuck Lever24d292b2010-12-24 01:32:43 +00001262 spin_lock(&clp->cl_lock);
1263 for (pos = rb_first(&server->state_owners);
1264 pos != NULL;
1265 pos = rb_next(pos)) {
1266 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001267 spin_lock(&sp->so_lock);
1268 list_for_each_entry(state, &sp->so_states, open_states) {
Chuck Lever24d292b2010-12-24 01:32:43 +00001269 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1270 &state->flags))
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001271 continue;
1272 nfs4_state_mark_reclaim_nograce(clp, state);
1273 }
1274 spin_unlock(&sp->so_lock);
1275 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001276 spin_unlock(&clp->cl_lock);
1277}
1278
1279static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1280{
1281 struct nfs_server *server;
1282
1283 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1284 return 0;
1285
1286 rcu_read_lock();
1287 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1288 nfs4_clear_reclaim_server(server);
1289 rcu_read_unlock();
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001290
1291 nfs_delegation_reap_unclaimed(clp);
Trond Myklebust6eaa6142010-10-04 17:59:08 -04001292 return 1;
1293}
1294
1295static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1296{
1297 if (!nfs4_state_clear_reclaim_reboot(clp))
1298 return;
1299 nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001300}
1301
1302static void nfs_delegation_clear_all(struct nfs_client *clp)
1303{
1304 nfs_delegation_mark_reclaim(clp);
1305 nfs_delegation_reap_unclaimed(clp);
1306}
1307
1308static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1309{
1310 nfs_delegation_clear_all(clp);
1311 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1312}
1313
Trond Myklebust168667c2010-10-19 19:47:49 -04001314static void nfs4_warn_keyexpired(const char *s)
1315{
1316 printk_ratelimited(KERN_WARNING "Error: state manager"
1317 " encountered RPCSEC_GSS session"
1318 " expired against NFSv4 server %s.\n",
1319 s);
1320}
1321
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001322static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
Trond Myklebuste598d842008-12-23 15:21:42 -05001323{
1324 switch (error) {
1325 case -NFS4ERR_CB_PATH_DOWN:
Trond Myklebust707fb4b2008-12-23 15:21:47 -05001326 nfs_handle_cb_pathdown(clp);
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001327 return 0;
Trond Myklebustc8b7ae3d2009-12-03 15:53:21 -05001328 case -NFS4ERR_NO_GRACE:
1329 nfs4_state_end_reclaim_reboot(clp);
1330 return 0;
Trond Myklebuste598d842008-12-23 15:21:42 -05001331 case -NFS4ERR_STALE_CLIENTID:
1332 case -NFS4ERR_LEASE_MOVED:
1333 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Trond Myklebust6eaa6142010-10-04 17:59:08 -04001334 nfs4_state_clear_reclaim_reboot(clp);
Trond Myklebuste598d842008-12-23 15:21:42 -05001335 nfs4_state_start_reclaim_reboot(clp);
1336 break;
1337 case -NFS4ERR_EXPIRED:
1338 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1339 nfs4_state_start_reclaim_nograce(clp);
Andy Adamson8ba9bf82009-12-04 15:55:34 -05001340 break;
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001341 case -NFS4ERR_BADSESSION:
1342 case -NFS4ERR_BADSLOT:
1343 case -NFS4ERR_BAD_HIGH_SLOT:
1344 case -NFS4ERR_DEADSESSION:
1345 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1346 case -NFS4ERR_SEQ_FALSE_RETRY:
1347 case -NFS4ERR_SEQ_MISORDERED:
Andy Adamson6df08182009-12-04 15:55:05 -05001348 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05001349 /* Zero session reset errors */
1350 return 0;
Trond Myklebust168667c2010-10-19 19:47:49 -04001351 case -EKEYEXPIRED:
1352 /* Nothing we can do */
1353 nfs4_warn_keyexpired(clp->cl_hostname);
1354 return 0;
Trond Myklebuste598d842008-12-23 15:21:42 -05001355 }
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001356 return error;
Trond Myklebuste598d842008-12-23 15:21:42 -05001357}
1358
Trond Myklebust02860012008-12-23 15:21:40 -05001359static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Chuck Lever24d292b2010-12-24 01:32:43 +00001361 struct nfs4_state_owner *sp;
1362 struct nfs_server *server;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001363 struct rb_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 int status = 0;
1365
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001366restart:
Chuck Lever24d292b2010-12-24 01:32:43 +00001367 rcu_read_lock();
1368 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1369 spin_lock(&clp->cl_lock);
1370 for (pos = rb_first(&server->state_owners);
1371 pos != NULL;
1372 pos = rb_next(pos)) {
1373 sp = rb_entry(pos,
1374 struct nfs4_state_owner, so_server_node);
1375 if (!test_and_clear_bit(ops->owner_flag_bit,
1376 &sp->so_flags))
1377 continue;
1378 atomic_inc(&sp->so_count);
1379 spin_unlock(&clp->cl_lock);
1380 rcu_read_unlock();
1381
1382 status = nfs4_reclaim_open_state(sp, ops);
1383 if (status < 0) {
1384 set_bit(ops->owner_flag_bit, &sp->so_flags);
1385 nfs4_put_state_owner(sp);
1386 return nfs4_recovery_handle_error(clp, status);
1387 }
1388
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001389 nfs4_put_state_owner(sp);
Chuck Lever24d292b2010-12-24 01:32:43 +00001390 goto restart;
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001391 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001392 spin_unlock(&clp->cl_lock);
Trond Myklebust02860012008-12-23 15:21:40 -05001393 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001394 rcu_read_unlock();
Trond Myklebust02860012008-12-23 15:21:40 -05001395 return status;
1396}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Trond Myklebust02860012008-12-23 15:21:40 -05001398static int nfs4_check_lease(struct nfs_client *clp)
1399{
1400 struct rpc_cred *cred;
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001401 const struct nfs4_state_maintenance_ops *ops =
1402 clp->cl_mvops->state_renewal_ops;
Trond Myklebust02860012008-12-23 15:21:40 -05001403 int status = -NFS4ERR_EXPIRED;
1404
Trond Myklebust0f605b52008-12-23 15:21:42 -05001405 /* Is the client already known to have an expired lease? */
1406 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1407 return 0;
Andy Adamsona7b72102009-04-01 09:22:46 -04001408 spin_lock(&clp->cl_lock);
1409 cred = ops->get_state_renewal_cred_locked(clp);
1410 spin_unlock(&clp->cl_lock);
Trond Myklebust0f605b52008-12-23 15:21:42 -05001411 if (cred == NULL) {
1412 cred = nfs4_get_setclientid_cred(clp);
1413 if (cred == NULL)
1414 goto out;
Trond Myklebust02860012008-12-23 15:21:40 -05001415 }
Benny Halevy8e69514f2009-04-01 09:22:45 -04001416 status = ops->renew_lease(clp, cred);
Trond Myklebust0f605b52008-12-23 15:21:42 -05001417 put_rpccred(cred);
1418out:
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001419 return nfs4_recovery_handle_error(clp, status);
Trond Myklebust02860012008-12-23 15:21:40 -05001420}
1421
1422static int nfs4_reclaim_lease(struct nfs_client *clp)
1423{
1424 struct rpc_cred *cred;
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001425 const struct nfs4_state_recovery_ops *ops =
1426 clp->cl_mvops->reboot_recovery_ops;
Trond Myklebust02860012008-12-23 15:21:40 -05001427 int status = -ENOENT;
1428
Andy Adamson90a16612009-04-01 09:22:48 -04001429 cred = ops->get_clid_cred(clp);
Trond Myklebust02860012008-12-23 15:21:40 -05001430 if (cred != NULL) {
Andy Adamson591d71c2009-04-01 09:22:47 -04001431 status = ops->establish_clid(clp, cred);
Trond Myklebust02860012008-12-23 15:21:40 -05001432 put_rpccred(cred);
1433 /* Handle case where the user hasn't set up machine creds */
1434 if (status == -EACCES && cred == clp->cl_machine_cred) {
1435 nfs4_clear_machine_cred(clp);
1436 status = -EAGAIN;
1437 }
Benny Halevyc2e713d2009-04-01 09:21:51 -04001438 if (status == -NFS4ERR_MINOR_VERS_MISMATCH)
1439 status = -EPROTONOSUPPORT;
Trond Myklebust02860012008-12-23 15:21:40 -05001440 }
1441 return status;
1442}
1443
Andy Adamson76db6d92009-04-01 09:22:38 -04001444#ifdef CONFIG_NFS_V4_1
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001445void nfs41_handle_recall_slot(struct nfs_client *clp)
1446{
1447 set_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
1448 nfs4_schedule_state_recovery(clp);
1449}
1450
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001451static void nfs4_reset_all_state(struct nfs_client *clp)
1452{
1453 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1454 clp->cl_boot_time = CURRENT_TIME;
1455 nfs4_state_start_reclaim_nograce(clp);
1456 nfs4_schedule_state_recovery(clp);
1457 }
1458}
1459
1460static void nfs41_handle_server_reboot(struct nfs_client *clp)
1461{
1462 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1463 nfs4_state_start_reclaim_reboot(clp);
1464 nfs4_schedule_state_recovery(clp);
1465 }
1466}
1467
1468static void nfs41_handle_state_revoked(struct nfs_client *clp)
1469{
1470 /* Temporary */
1471 nfs4_reset_all_state(clp);
1472}
1473
1474static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
1475{
1476 /* This will need to handle layouts too */
1477 nfs_expire_all_delegations(clp);
1478}
1479
1480static void nfs41_handle_cb_path_down(struct nfs_client *clp)
1481{
1482 nfs_expire_all_delegations(clp);
1483 if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
1484 nfs4_schedule_state_recovery(clp);
1485}
1486
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001487void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
1488{
1489 if (!flags)
1490 return;
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001491 else if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
1492 nfs41_handle_server_reboot(clp);
1493 else if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001494 SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
1495 SEQ4_STATUS_ADMIN_STATE_REVOKED |
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001496 SEQ4_STATUS_LEASE_MOVED))
1497 nfs41_handle_state_revoked(clp);
1498 else if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
1499 nfs41_handle_recallable_state_revoked(clp);
1500 else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001501 SEQ4_STATUS_BACKCHANNEL_FAULT |
1502 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001503 nfs41_handle_cb_path_down(clp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001504}
1505
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001506static int nfs4_reset_session(struct nfs_client *clp)
1507{
1508 int status;
1509
Trond Myklebust38045412009-12-15 17:36:57 -05001510 nfs4_begin_drain_session(clp);
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001511 status = nfs4_proc_destroy_session(clp->cl_session);
1512 if (status && status != -NFS4ERR_BADSESSION &&
1513 status != -NFS4ERR_DEADSESSION) {
Ricardo Labiagaf4558482009-12-07 09:16:09 -05001514 status = nfs4_recovery_handle_error(clp, status);
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001515 goto out;
1516 }
1517
1518 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
Trond Myklebustf26468f2009-12-05 19:32:11 -05001519 status = nfs4_proc_create_session(clp);
Andy Adamson41f54a52010-01-21 14:54:13 -05001520 if (status) {
Ricardo Labiagaf4558482009-12-07 09:16:09 -05001521 status = nfs4_recovery_handle_error(clp, status);
Andy Adamson41f54a52010-01-21 14:54:13 -05001522 goto out;
1523 }
1524 /* create_session negotiated new slot table */
1525 clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05001526
Andy Adamson41f54a52010-01-21 14:54:13 -05001527 /* Let the state manager reestablish state */
1528 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001529 nfs41_setup_state_renewal(clp);
Andy Adamson41f54a52010-01-21 14:54:13 -05001530out:
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001531 return status;
1532}
Andy Adamson76db6d92009-04-01 09:22:38 -04001533
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001534static int nfs4_recall_slot(struct nfs_client *clp)
1535{
1536 struct nfs4_slot_table *fc_tbl = &clp->cl_session->fc_slot_table;
1537 struct nfs4_channel_attrs *fc_attrs = &clp->cl_session->fc_attrs;
1538 struct nfs4_slot *new, *old;
1539 int i;
1540
1541 nfs4_begin_drain_session(clp);
1542 new = kmalloc(fc_tbl->target_max_slots * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001543 GFP_NOFS);
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001544 if (!new)
1545 return -ENOMEM;
1546
1547 spin_lock(&fc_tbl->slot_tbl_lock);
1548 for (i = 0; i < fc_tbl->target_max_slots; i++)
1549 new[i].seq_nr = fc_tbl->slots[i].seq_nr;
1550 old = fc_tbl->slots;
1551 fc_tbl->slots = new;
1552 fc_tbl->max_slots = fc_tbl->target_max_slots;
1553 fc_tbl->target_max_slots = 0;
1554 fc_attrs->max_reqs = fc_tbl->max_slots;
1555 spin_unlock(&fc_tbl->slot_tbl_lock);
1556
1557 kfree(old);
1558 nfs4_end_drain_session(clp);
1559 return 0;
1560}
1561
Andy Adamson76db6d92009-04-01 09:22:38 -04001562#else /* CONFIG_NFS_V4_1 */
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001563static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001564static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001565static int nfs4_recall_slot(struct nfs_client *clp) { return 0; }
Andy Adamson76db6d92009-04-01 09:22:38 -04001566#endif /* CONFIG_NFS_V4_1 */
1567
Andy Adamson78722e92009-06-16 14:43:15 -04001568/* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
1569 * on EXCHANGE_ID for v4.1
1570 */
1571static void nfs4_set_lease_expired(struct nfs_client *clp, int status)
1572{
1573 if (nfs4_has_session(clp)) {
1574 switch (status) {
1575 case -NFS4ERR_DELAY:
1576 case -NFS4ERR_CLID_INUSE:
1577 case -EAGAIN:
1578 break;
1579
Trond Myklebust168667c2010-10-19 19:47:49 -04001580 case -EKEYEXPIRED:
1581 nfs4_warn_keyexpired(clp->cl_hostname);
Andy Adamson78722e92009-06-16 14:43:15 -04001582 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1583 * in nfs4_exchange_id */
1584 default:
1585 return;
1586 }
1587 }
1588 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1589}
1590
Trond Myklebuste005e802008-12-23 15:21:48 -05001591static void nfs4_state_manager(struct nfs_client *clp)
Trond Myklebust02860012008-12-23 15:21:40 -05001592{
Trond Myklebust02860012008-12-23 15:21:40 -05001593 int status = 0;
1594
Trond Myklebust02860012008-12-23 15:21:40 -05001595 /* Ensure exclusive access to NFSv4 state */
Trond Myklebustf3c76492008-12-23 15:21:48 -05001596 for(;;) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001597 if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
1598 /* We're going to have to re-establish a clientid */
1599 status = nfs4_reclaim_lease(clp);
1600 if (status) {
Andy Adamson78722e92009-06-16 14:43:15 -04001601 nfs4_set_lease_expired(clp, status);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001602 if (test_bit(NFS4CLNT_LEASE_EXPIRED,
1603 &clp->cl_state))
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001604 continue;
Andy Adamson76db6d92009-04-01 09:22:38 -04001605 if (clp->cl_cons_state ==
1606 NFS_CS_SESSION_INITING)
1607 nfs_mark_client_ready(clp, status);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001608 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
Trond Myklebuste598d842008-12-23 15:21:42 -05001610 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
Ricardo Labiaga7cab89b2009-12-08 14:35:28 -05001611 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
Andy Adamson974cec82010-10-20 00:18:02 -04001612 pnfs_destroy_all_layouts(clp);
Trond Myklebuste598d842008-12-23 15:21:42 -05001613 }
1614
1615 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
1616 status = nfs4_check_lease(clp);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001617 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
Trond Myklebuste598d842008-12-23 15:21:42 -05001618 continue;
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001619 if (status < 0 && status != -NFS4ERR_CB_PATH_DOWN)
1620 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 }
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001622
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001623 /* Initialize or reset the session */
Andy Adamson6df08182009-12-04 15:55:05 -05001624 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)
Trond Myklebust7111dc72009-08-24 19:21:29 -04001625 && nfs4_has_session(clp)) {
Andy Adamson4d643d12009-12-04 15:52:24 -05001626 status = nfs4_reset_session(clp);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001627 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1628 continue;
1629 if (status < 0)
Andy Adamson76db6d92009-04-01 09:22:38 -04001630 goto out_error;
Andy Adamson76db6d92009-04-01 09:22:38 -04001631 }
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001632
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001633 /* First recover reboot state... */
Trond Myklebuste345e882009-12-03 15:52:41 -05001634 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
Andy Adamson591d71c2009-04-01 09:22:47 -04001635 status = nfs4_do_reclaim(clp,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001636 clp->cl_mvops->reboot_recovery_ops);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001637 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
Andy Adamson6df08182009-12-04 15:55:05 -05001638 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001639 continue;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001640 nfs4_state_end_reclaim_reboot(clp);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001641 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1642 continue;
1643 if (status < 0)
1644 goto out_error;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001645 }
1646
1647 /* Now recover expired state... */
1648 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
Andy Adamson591d71c2009-04-01 09:22:47 -04001649 status = nfs4_do_reclaim(clp,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001650 clp->cl_mvops->nograce_recovery_ops);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001651 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
Andy Adamson6df08182009-12-04 15:55:05 -05001652 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001653 test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1654 continue;
1655 if (status < 0)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001656 goto out_error;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001657 }
Trond Myklebust707fb4b2008-12-23 15:21:47 -05001658
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001659 nfs4_end_drain_session(clp);
Trond Myklebust707fb4b2008-12-23 15:21:47 -05001660 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
1661 nfs_client_return_marked_delegations(clp);
1662 continue;
1663 }
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001664 /* Recall session slots */
1665 if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state)
1666 && nfs4_has_session(clp)) {
1667 status = nfs4_recall_slot(clp);
1668 if (status < 0)
1669 goto out_error;
1670 continue;
1671 }
1672
Trond Myklebuste005e802008-12-23 15:21:48 -05001673
1674 nfs4_clear_state_manager_bit(clp);
Trond Myklebustf3c76492008-12-23 15:21:48 -05001675 /* Did we race with an attempt to give us more work? */
1676 if (clp->cl_state == 0)
1677 break;
1678 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1679 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
Trond Myklebuste005e802008-12-23 15:21:48 -05001681 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682out_error:
Trond Myklebuste005e802008-12-23 15:21:48 -05001683 printk(KERN_WARNING "Error: state manager failed on NFSv4 server %s"
Chuck Lever5d8515c2007-12-10 14:57:16 -05001684 " with error %d\n", clp->cl_hostname, -status);
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001685 nfs4_end_drain_session(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05001686 nfs4_clear_state_manager_bit(clp);
1687}
1688
1689static int nfs4_run_state_manager(void *ptr)
1690{
1691 struct nfs_client *clp = ptr;
1692
1693 allow_signal(SIGKILL);
1694 nfs4_state_manager(clp);
1695 nfs_put_client(clp);
1696 module_put_and_exit(0);
1697 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
1699
1700/*
1701 * Local variables:
1702 * c-basic-offset: 8
1703 * End:
1704 */