blob: ab1bf5bb021f55cfaf3c45dcbdb226acfc3da3e0 [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
Andy Adamsond6fb79d2011-03-01 01:34:11 +0000156 if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
157 nfs4_schedule_state_renewal(clp);
158 return 0;
159 }
160
Ricardo Labiaga9430fb62009-12-06 12:23:46 -0500161 status = nfs4_proc_get_lease_time(clp, &fsinfo);
162 if (status == 0) {
163 /* Update lease time and schedule renewal */
164 spin_lock(&clp->cl_lock);
165 clp->cl_lease_time = fsinfo.lease_time * HZ;
166 clp->cl_last_renewal = jiffies;
167 spin_unlock(&clp->cl_lock);
168
169 nfs4_schedule_state_renewal(clp);
170 }
171
172 return status;
173}
174
Andy Adamson42acd022011-01-06 02:04:34 +0000175/*
176 * Back channel returns NFS4ERR_DELAY for new requests when
177 * NFS4_SESSION_DRAINING is set so there is no work to be done when draining
178 * is ended.
179 */
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800180static void nfs4_end_drain_session(struct nfs_client *clp)
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500181{
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800182 struct nfs4_session *ses = clp->cl_session;
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800183 int max_slots;
184
Trond Myklebusta2118c32010-06-16 09:52:26 -0400185 if (ses == NULL)
186 return;
187 if (test_and_clear_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800188 spin_lock(&ses->fc_slot_table.slot_tbl_lock);
189 max_slots = ses->fc_slot_table.max_slots;
190 while (max_slots--) {
191 struct rpc_task *task;
192
193 task = rpc_wake_up_next(&ses->fc_slot_table.
194 slot_tbl_waitq);
195 if (!task)
196 break;
197 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
198 }
199 spin_unlock(&ses->fc_slot_table.slot_tbl_lock);
200 }
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500201}
202
Andy Adamson42acd022011-01-06 02:04:34 +0000203static int nfs4_wait_on_slot_tbl(struct nfs4_slot_table *tbl)
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500204{
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500205 spin_lock(&tbl->slot_tbl_lock);
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500206 if (tbl->highest_used_slotid != -1) {
Andy Adamson42acd022011-01-06 02:04:34 +0000207 INIT_COMPLETION(tbl->complete);
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500208 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson42acd022011-01-06 02:04:34 +0000209 return wait_for_completion_interruptible(&tbl->complete);
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -0500210 }
211 spin_unlock(&tbl->slot_tbl_lock);
212 return 0;
213}
214
Andy Adamson42acd022011-01-06 02:04:34 +0000215static int nfs4_begin_drain_session(struct nfs_client *clp)
216{
217 struct nfs4_session *ses = clp->cl_session;
218 int ret = 0;
219
220 set_bit(NFS4_SESSION_DRAINING, &ses->session_state);
221 /* back channel */
222 ret = nfs4_wait_on_slot_tbl(&ses->bc_slot_table);
223 if (ret)
224 return ret;
225 /* fore channel */
226 return nfs4_wait_on_slot_tbl(&ses->fc_slot_table);
227}
228
Andy Adamson4d643d12009-12-04 15:52:24 -0500229int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
230{
231 int status;
232
Trond Myklebust38045412009-12-15 17:36:57 -0500233 nfs4_begin_drain_session(clp);
Andy Adamson4d643d12009-12-04 15:52:24 -0500234 status = nfs4_proc_exchange_id(clp, cred);
Ricardo Labiaga9430fb62009-12-06 12:23:46 -0500235 if (status != 0)
236 goto out;
237 status = nfs4_proc_create_session(clp);
238 if (status != 0)
239 goto out;
240 nfs41_setup_state_renewal(clp);
241 nfs_mark_client_ready(clp, NFS_CS_READY);
242out:
Andy Adamson4d643d12009-12-04 15:52:24 -0500243 return status;
244}
245
Andy Adamsonb4b82602009-04-01 09:22:49 -0400246struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp)
247{
248 struct rpc_cred *cred;
249
250 spin_lock(&clp->cl_lock);
251 cred = nfs4_get_machine_cred_locked(clp);
252 spin_unlock(&clp->cl_lock);
253 return cred;
254}
255
256#endif /* CONFIG_NFS_V4_1 */
257
Chuck Lever24d292b2010-12-24 01:32:43 +0000258static struct rpc_cred *
259nfs4_get_setclientid_cred_server(struct nfs_server *server)
Trond Myklebust286d7d62006-01-03 09:55:26 +0100260{
Chuck Lever24d292b2010-12-24 01:32:43 +0000261 struct nfs_client *clp = server->nfs_client;
262 struct rpc_cred *cred = NULL;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100263 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400264 struct rb_node *pos;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100265
Trond Myklebust6dc9d572008-12-23 15:21:41 -0500266 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000267 pos = rb_first(&server->state_owners);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400268 if (pos != NULL) {
Chuck Lever24d292b2010-12-24 01:32:43 +0000269 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400270 cred = get_rpccred(sp->so_cred);
Trond Myklebust286d7d62006-01-03 09:55:26 +0100271 }
Trond Myklebust6dc9d572008-12-23 15:21:41 -0500272 spin_unlock(&clp->cl_lock);
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400273 return cred;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100274}
275
Chuck Lever24d292b2010-12-24 01:32:43 +0000276/**
277 * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation
278 * @clp: client state handle
279 *
280 * Returns an rpc_cred with reference count bumped, or NULL.
281 */
282struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
283{
284 struct nfs_server *server;
285 struct rpc_cred *cred;
286
287 spin_lock(&clp->cl_lock);
288 cred = nfs4_get_machine_cred_locked(clp);
289 spin_unlock(&clp->cl_lock);
290 if (cred != NULL)
291 goto out;
292
293 rcu_read_lock();
294 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
295 cred = nfs4_get_setclientid_cred_server(server);
296 if (cred != NULL)
297 break;
298 }
299 rcu_read_unlock();
300
301out:
302 return cred;
303}
304
305static void nfs_alloc_unique_id_locked(struct rb_root *root,
306 struct nfs_unique_id *new,
307 __u64 minval, int maxbits)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400308{
309 struct rb_node **p, *parent;
310 struct nfs_unique_id *pos;
311 __u64 mask = ~0ULL;
312
313 if (maxbits < 64)
314 mask = (1ULL << maxbits) - 1ULL;
315
316 /* Ensure distribution is more or less flat */
317 get_random_bytes(&new->id, sizeof(new->id));
318 new->id &= mask;
319 if (new->id < minval)
320 new->id += minval;
321retry:
322 p = &root->rb_node;
323 parent = NULL;
324
325 while (*p != NULL) {
326 parent = *p;
327 pos = rb_entry(parent, struct nfs_unique_id, rb_node);
328
329 if (new->id < pos->id)
330 p = &(*p)->rb_left;
331 else if (new->id > pos->id)
332 p = &(*p)->rb_right;
333 else
334 goto id_exists;
335 }
336 rb_link_node(&new->rb_node, parent, p);
337 rb_insert_color(&new->rb_node, root);
338 return;
339id_exists:
340 for (;;) {
341 new->id++;
342 if (new->id < minval || (new->id & mask) != new->id) {
343 new->id = minval;
344 break;
345 }
346 parent = rb_next(parent);
347 if (parent == NULL)
348 break;
349 pos = rb_entry(parent, struct nfs_unique_id, rb_node);
350 if (new->id < pos->id)
351 break;
352 }
353 goto retry;
354}
355
356static void nfs_free_unique_id(struct rb_root *root, struct nfs_unique_id *id)
357{
358 rb_erase(&id->rb_node, root);
359}
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361static struct nfs4_state_owner *
Chuck Lever24d292b2010-12-24 01:32:43 +0000362nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Chuck Lever24d292b2010-12-24 01:32:43 +0000364 struct rb_node **p = &server->state_owners.rb_node,
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400365 *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 struct nfs4_state_owner *sp, *res = NULL;
367
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400368 while (*p != NULL) {
369 parent = *p;
Chuck Lever24d292b2010-12-24 01:32:43 +0000370 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400371
Trond Myklebust6f2e64d2007-07-06 10:53:21 -0400372 if (server < sp->so_server) {
373 p = &parent->rb_left;
374 continue;
375 }
376 if (server > sp->so_server) {
377 p = &parent->rb_right;
378 continue;
379 }
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400380 if (cred < sp->so_cred)
381 p = &parent->rb_left;
382 else if (cred > sp->so_cred)
383 p = &parent->rb_right;
384 else {
385 atomic_inc(&sp->so_count);
386 res = sp;
387 break;
388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390 return res;
391}
392
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400393static struct nfs4_state_owner *
Chuck Lever24d292b2010-12-24 01:32:43 +0000394nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400395{
Chuck Lever24d292b2010-12-24 01:32:43 +0000396 struct nfs_server *server = new->so_server;
397 struct rb_node **p = &server->state_owners.rb_node,
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400398 *parent = NULL;
399 struct nfs4_state_owner *sp;
400
401 while (*p != NULL) {
402 parent = *p;
Chuck Lever24d292b2010-12-24 01:32:43 +0000403 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400404
405 if (new->so_cred < sp->so_cred)
406 p = &parent->rb_left;
407 else if (new->so_cred > sp->so_cred)
408 p = &parent->rb_right;
409 else {
410 atomic_inc(&sp->so_count);
411 return sp;
412 }
413 }
Chuck Lever24d292b2010-12-24 01:32:43 +0000414 nfs_alloc_unique_id_locked(&server->openowner_id,
415 &new->so_owner_id, 1, 64);
416 rb_link_node(&new->so_server_node, parent, p);
417 rb_insert_color(&new->so_server_node, &server->state_owners);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400418 return new;
419}
420
421static void
Chuck Lever24d292b2010-12-24 01:32:43 +0000422nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400423{
Chuck Lever24d292b2010-12-24 01:32:43 +0000424 struct nfs_server *server = sp->so_server;
425
426 if (!RB_EMPTY_NODE(&sp->so_server_node))
427 rb_erase(&sp->so_server_node, &server->state_owners);
428 nfs_free_unique_id(&server->openowner_id, &sp->so_owner_id);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400429}
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/*
432 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
433 * create a new state_owner.
434 *
435 */
436static struct nfs4_state_owner *
437nfs4_alloc_state_owner(void)
438{
439 struct nfs4_state_owner *sp;
440
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400441 sp = kzalloc(sizeof(*sp),GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (!sp)
443 return NULL;
Trond Myklebustec073422005-10-20 14:22:47 -0700444 spin_lock_init(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 INIT_LIST_HEAD(&sp->so_states);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700446 rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
447 sp->so_seqid.sequence = &sp->so_sequence;
448 spin_lock_init(&sp->so_sequence.lock);
449 INIT_LIST_HEAD(&sp->so_sequence.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 atomic_set(&sp->so_count, 1);
451 return sp;
452}
453
Adrian Bunk1d2e88e2008-05-02 13:42:45 -0700454static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455nfs4_drop_state_owner(struct nfs4_state_owner *sp)
456{
Chuck Lever24d292b2010-12-24 01:32:43 +0000457 if (!RB_EMPTY_NODE(&sp->so_server_node)) {
458 struct nfs_server *server = sp->so_server;
459 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400460
461 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000462 rb_erase(&sp->so_server_node, &server->state_owners);
463 RB_CLEAR_NODE(&sp->so_server_node);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400464 spin_unlock(&clp->cl_lock);
465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Chuck Lever24d292b2010-12-24 01:32:43 +0000468/**
469 * nfs4_get_state_owner - Look up a state owner given a credential
470 * @server: nfs_server to search
471 * @cred: RPC credential to match
472 *
473 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
474 */
475struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
476 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
David Howells7539bba2006-08-22 20:06:09 -0400478 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 struct nfs4_state_owner *sp, *new;
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000482 sp = nfs4_find_state_owner_locked(server, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 spin_unlock(&clp->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (sp != NULL)
485 return sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400486 new = nfs4_alloc_state_owner();
487 if (new == NULL)
488 return NULL;
Trond Myklebust6f2e64d2007-07-06 10:53:21 -0400489 new->so_server = server;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400490 new->so_cred = cred;
491 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000492 sp = nfs4_insert_state_owner_locked(new);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400493 spin_unlock(&clp->cl_lock);
494 if (sp == new)
495 get_rpccred(cred);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500496 else {
497 rpc_destroy_wait_queue(&new->so_sequence.wait);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400498 kfree(new);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500499 }
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400500 return sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Chuck Lever24d292b2010-12-24 01:32:43 +0000503/**
504 * nfs4_put_state_owner - Release a nfs4_state_owner
505 * @sp: state owner data to release
506 *
507 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508void nfs4_put_state_owner(struct nfs4_state_owner *sp)
509{
Trond Myklebust1f0e8902010-06-24 15:11:43 -0400510 struct nfs_client *clp = sp->so_server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 struct rpc_cred *cred = sp->so_cred;
512
513 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
514 return;
Chuck Lever24d292b2010-12-24 01:32:43 +0000515 nfs4_remove_state_owner_locked(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 spin_unlock(&clp->cl_lock);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500517 rpc_destroy_wait_queue(&sp->so_sequence.wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 put_rpccred(cred);
519 kfree(sp);
520}
521
522static struct nfs4_state *
523nfs4_alloc_open_state(void)
524{
525 struct nfs4_state *state;
526
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400527 state = kzalloc(sizeof(*state), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (!state)
529 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 atomic_set(&state->count, 1);
531 INIT_LIST_HEAD(&state->lock_states);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000532 spin_lock_init(&state->state_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400533 seqlock_init(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return state;
535}
536
Trond Myklebust4cecb762005-11-04 15:32:58 -0500537void
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500538nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
Trond Myklebust4cecb762005-11-04 15:32:58 -0500539{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500540 if (state->state == fmode)
Trond Myklebust4cecb762005-11-04 15:32:58 -0500541 return;
542 /* NB! List reordering - see the reclaim code for why. */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500543 if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
544 if (fmode & FMODE_WRITE)
Trond Myklebust4cecb762005-11-04 15:32:58 -0500545 list_move(&state->open_states, &state->owner->so_states);
546 else
547 list_move_tail(&state->open_states, &state->owner->so_states);
548 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500549 state->state = fmode;
Trond Myklebust4cecb762005-11-04 15:32:58 -0500550}
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552static struct nfs4_state *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553__nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
554{
555 struct nfs_inode *nfsi = NFS_I(inode);
556 struct nfs4_state *state;
557
558 list_for_each_entry(state, &nfsi->open_states, inode_states) {
Trond Myklebust1c816ef2007-07-03 14:41:19 -0400559 if (state->owner != owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 continue;
Trond Myklebust1c816ef2007-07-03 14:41:19 -0400561 if (atomic_inc_not_zero(&state->count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564 return NULL;
565}
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567static void
568nfs4_free_open_state(struct nfs4_state *state)
569{
570 kfree(state);
571}
572
573struct nfs4_state *
574nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
575{
576 struct nfs4_state *state, *new;
577 struct nfs_inode *nfsi = NFS_I(inode);
578
579 spin_lock(&inode->i_lock);
580 state = __nfs4_find_state_byowner(inode, owner);
581 spin_unlock(&inode->i_lock);
582 if (state)
583 goto out;
584 new = nfs4_alloc_open_state();
Trond Myklebustec073422005-10-20 14:22:47 -0700585 spin_lock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 spin_lock(&inode->i_lock);
587 state = __nfs4_find_state_byowner(inode, owner);
588 if (state == NULL && new != NULL) {
589 state = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 state->owner = owner;
591 atomic_inc(&owner->so_count);
592 list_add(&state->inode_states, &nfsi->open_states);
593 state->inode = igrab(inode);
594 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700595 /* Note: The reclaim code dictates that we add stateless
596 * and read-only stateids to the end of the list */
597 list_add_tail(&state->open_states, &owner->so_states);
598 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 } else {
600 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700601 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (new)
603 nfs4_free_open_state(new);
604 }
605out:
606 return state;
607}
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609void nfs4_put_open_state(struct nfs4_state *state)
610{
611 struct inode *inode = state->inode;
612 struct nfs4_state_owner *owner = state->owner;
613
Trond Myklebustec073422005-10-20 14:22:47 -0700614 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return;
Trond Myklebustec073422005-10-20 14:22:47 -0700616 spin_lock(&inode->i_lock);
Trond Myklebustba6830312007-07-27 10:23:05 -0400617 list_del(&state->inode_states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 list_del(&state->open_states);
Trond Myklebustec073422005-10-20 14:22:47 -0700619 spin_unlock(&inode->i_lock);
620 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 nfs4_free_open_state(state);
623 nfs4_put_state_owner(owner);
624}
625
626/*
Trond Myklebust83c9d412005-10-18 14:20:13 -0700627 * Close the current file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 */
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400629static void __nfs4_close(struct path *path, struct nfs4_state *state,
630 fmode_t fmode, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct nfs4_state_owner *owner = state->owner;
Trond Myklebust003707c2007-07-05 18:07:55 -0400633 int call_close = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500634 fmode_t newstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 atomic_inc(&owner->so_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /* Protect against nfs4_find_state() */
Trond Myklebustec073422005-10-20 14:22:47 -0700638 spin_lock(&owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500639 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100640 case FMODE_READ:
641 state->n_rdonly--;
642 break;
643 case FMODE_WRITE:
644 state->n_wronly--;
645 break;
646 case FMODE_READ|FMODE_WRITE:
647 state->n_rdwr--;
648 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400649 newstate = FMODE_READ|FMODE_WRITE;
Trond Myklebuste7616922006-01-03 09:55:13 +0100650 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400651 if (state->n_rdonly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100652 newstate &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -0400653 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
654 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
655 }
656 if (state->n_wronly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100657 newstate &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -0400658 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
659 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
660 }
661 if (newstate == 0)
662 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebuste7616922006-01-03 09:55:13 +0100663 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400664 nfs4_state_set_mode_locked(state, newstate);
Trond Myklebustec073422005-10-20 14:22:47 -0700665 spin_unlock(&owner->so_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500666
Trond Myklebust003707c2007-07-05 18:07:55 -0400667 if (!call_close) {
Trond Myklebustb39e6252007-06-11 23:05:07 -0400668 nfs4_put_open_state(state);
669 nfs4_put_state_owner(owner);
Fred Isamanf7e89172011-01-06 11:36:32 +0000670 } else {
671 bool roc = pnfs_roc(state->inode);
672
673 nfs4_do_close(path, state, gfp_mask, wait, roc);
674 }
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400675}
676
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500677void nfs4_close_state(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_NOFS, 0);
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400680}
681
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500682void nfs4_close_sync(struct path *path, struct nfs4_state *state, fmode_t fmode)
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400683{
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400684 __nfs4_close(path, state, fmode, GFP_KERNEL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687/*
688 * Search the state->lock_states for an existing lock_owner
689 * that is compatible with current->files
690 */
691static struct nfs4_lock_state *
Trond Myklebust77041ed2010-07-01 12:49:11 -0400692__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 -0700693{
694 struct nfs4_lock_state *pos;
695 list_for_each_entry(pos, &state->lock_states, ls_locks) {
Trond Myklebust77041ed2010-07-01 12:49:11 -0400696 if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 continue;
Trond Myklebust77041ed2010-07-01 12:49:11 -0400698 switch (pos->ls_owner.lo_type) {
699 case NFS4_POSIX_LOCK_TYPE:
700 if (pos->ls_owner.lo_u.posix_owner != fl_owner)
701 continue;
702 break;
703 case NFS4_FLOCK_LOCK_TYPE:
704 if (pos->ls_owner.lo_u.flock_owner != fl_pid)
705 continue;
706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 atomic_inc(&pos->ls_count);
708 return pos;
709 }
710 return NULL;
711}
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713/*
714 * Return a compatible lock_state. If no initialized lock_state structure
715 * exists, return an uninitialized one.
716 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 */
Trond Myklebust77041ed2010-07-01 12:49:11 -0400718static 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 -0700719{
720 struct nfs4_lock_state *lsp;
Chuck Lever24d292b2010-12-24 01:32:43 +0000721 struct nfs_server *server = state->owner->so_server;
722 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400724 lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (lsp == NULL)
726 return NULL;
Trond Myklebustd0dc3702008-01-10 16:07:54 -0500727 rpc_init_wait_queue(&lsp->ls_sequence.wait, "lock_seqid_waitqueue");
728 spin_lock_init(&lsp->ls_sequence.lock);
729 INIT_LIST_HEAD(&lsp->ls_sequence.list);
730 lsp->ls_seqid.sequence = &lsp->ls_sequence;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 atomic_set(&lsp->ls_count, 1);
Trond Myklebustb64aec82009-07-21 16:47:46 -0400732 lsp->ls_state = state;
Trond Myklebust77041ed2010-07-01 12:49:11 -0400733 lsp->ls_owner.lo_type = type;
734 switch (lsp->ls_owner.lo_type) {
735 case NFS4_FLOCK_LOCK_TYPE:
736 lsp->ls_owner.lo_u.flock_owner = fl_pid;
737 break;
738 case NFS4_POSIX_LOCK_TYPE:
739 lsp->ls_owner.lo_u.posix_owner = fl_owner;
740 break;
741 default:
742 kfree(lsp);
743 return NULL;
744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000746 nfs_alloc_unique_id_locked(&server->lockowner_id, &lsp->ls_id, 1, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 spin_unlock(&clp->cl_lock);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000748 INIT_LIST_HEAD(&lsp->ls_locks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return lsp;
750}
751
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400752static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
753{
Chuck Lever24d292b2010-12-24 01:32:43 +0000754 struct nfs_server *server = lsp->ls_state->owner->so_server;
755 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400756
757 spin_lock(&clp->cl_lock);
Chuck Lever24d292b2010-12-24 01:32:43 +0000758 nfs_free_unique_id(&server->lockowner_id, &lsp->ls_id);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400759 spin_unlock(&clp->cl_lock);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500760 rpc_destroy_wait_queue(&lsp->ls_sequence.wait);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400761 kfree(lsp);
762}
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764/*
765 * Return a compatible lock_state. If no initialized lock_state structure
766 * exists, return an uninitialized one.
767 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 */
Trond Myklebust77041ed2010-07-01 12:49:11 -0400769static 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 -0700770{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000771 struct nfs4_lock_state *lsp, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000773 for(;;) {
774 spin_lock(&state->state_lock);
Trond Myklebust77041ed2010-07-01 12:49:11 -0400775 lsp = __nfs4_find_lock_state(state, owner, pid, type);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000776 if (lsp != NULL)
777 break;
778 if (new != NULL) {
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000779 list_add(&new->ls_locks, &state->lock_states);
780 set_bit(LK_STATE_IN_USE, &state->flags);
781 lsp = new;
782 new = NULL;
783 break;
784 }
785 spin_unlock(&state->state_lock);
Trond Myklebust77041ed2010-07-01 12:49:11 -0400786 new = nfs4_alloc_lock_state(state, owner, pid, type);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000787 if (new == NULL)
788 return NULL;
789 }
790 spin_unlock(&state->state_lock);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400791 if (new != NULL)
792 nfs4_free_lock_state(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return lsp;
794}
795
796/*
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000797 * Release reference to lock_state, and free it if we see that
798 * it is no longer in use
799 */
Trond Myklebustfaf5f492005-10-18 14:20:15 -0700800void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000801{
802 struct nfs4_state *state;
803
804 if (lsp == NULL)
805 return;
806 state = lsp->ls_state;
807 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
808 return;
809 list_del(&lsp->ls_locks);
810 if (list_empty(&state->lock_states))
811 clear_bit(LK_STATE_IN_USE, &state->flags);
812 spin_unlock(&state->state_lock);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -0400813 if (lsp->ls_flags & NFS_LOCK_INITIALIZED)
814 nfs4_release_lockowner(lsp);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400815 nfs4_free_lock_state(lsp);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000816}
817
818static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
819{
820 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
821
822 dst->fl_u.nfs4_fl.owner = lsp;
823 atomic_inc(&lsp->ls_count);
824}
825
826static void nfs4_fl_release_lock(struct file_lock *fl)
827{
828 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
829}
830
Alexey Dobriyan6aed6282009-09-21 17:01:11 -0700831static const struct file_lock_operations nfs4_fl_lock_ops = {
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000832 .fl_copy_lock = nfs4_fl_copy_lock,
833 .fl_release_private = nfs4_fl_release_lock,
834};
835
836int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
837{
838 struct nfs4_lock_state *lsp;
839
840 if (fl->fl_ops != NULL)
841 return 0;
Trond Myklebust77041ed2010-07-01 12:49:11 -0400842 if (fl->fl_flags & FL_POSIX)
843 lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
844 else if (fl->fl_flags & FL_FLOCK)
845 lsp = nfs4_get_lock_state(state, 0, fl->fl_pid, NFS4_FLOCK_LOCK_TYPE);
846 else
847 return -EINVAL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000848 if (lsp == NULL)
849 return -ENOMEM;
850 fl->fl_u.nfs4_fl.owner = lsp;
851 fl->fl_ops = &nfs4_fl_lock_ops;
852 return 0;
853}
854
855/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 * Byte-range lock aware utility to initialize the stateid of read/write
857 * requests.
858 */
Trond Myklebust77041ed2010-07-01 12:49:11 -0400859void 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 -0700860{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000861 struct nfs4_lock_state *lsp;
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400862 int seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400864 do {
865 seq = read_seqbegin(&state->seqlock);
866 memcpy(dst, &state->stateid, sizeof(*dst));
867 } while (read_seqretry(&state->seqlock, seq));
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000868 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
869 return;
870
871 spin_lock(&state->state_lock);
Trond Myklebust77041ed2010-07-01 12:49:11 -0400872 lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000873 if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
874 memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
875 spin_unlock(&state->state_lock);
876 nfs4_put_lock_state(lsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400879struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700881 struct nfs_seqid *new;
882
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400883 new = kmalloc(sizeof(*new), gfp_mask);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700884 if (new != NULL) {
885 new->sequence = counter;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500886 INIT_LIST_HEAD(&new->list);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700887 }
888 return new;
889}
890
Trond Myklebust72211db2009-12-15 14:47:36 -0500891void nfs_release_seqid(struct nfs_seqid *seqid)
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700892{
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500893 if (!list_empty(&seqid->list)) {
894 struct rpc_sequence *sequence = seqid->sequence->sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700895
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500896 spin_lock(&sequence->lock);
Trond Myklebust72211db2009-12-15 14:47:36 -0500897 list_del_init(&seqid->list);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500898 spin_unlock(&sequence->lock);
899 rpc_wake_up(&sequence->wait);
900 }
Trond Myklebust72211db2009-12-15 14:47:36 -0500901}
902
903void nfs_free_seqid(struct nfs_seqid *seqid)
904{
905 nfs_release_seqid(seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700906 kfree(seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700910 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
911 * failed with a seqid incrementing error -
912 * see comments nfs_fs.h:seqid_mutating_error()
913 */
Trond Myklebust88d90932007-07-02 14:03:03 -0400914static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500916 BUG_ON(list_first_entry(&seqid->sequence->sequence->list, struct nfs_seqid, list) != seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700917 switch (status) {
918 case 0:
919 break;
920 case -NFS4ERR_BAD_SEQID:
Trond Myklebust6f43ddc2007-07-08 16:49:11 -0400921 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
922 return;
923 printk(KERN_WARNING "NFS: v4 server returned a bad"
Dan Muntz497799e2008-02-13 13:09:35 -0800924 " sequence-id error on an"
925 " unconfirmed sequence %p!\n",
Trond Myklebust6f43ddc2007-07-08 16:49:11 -0400926 seqid->sequence);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700927 case -NFS4ERR_STALE_CLIENTID:
928 case -NFS4ERR_STALE_STATEID:
929 case -NFS4ERR_BAD_STATEID:
930 case -NFS4ERR_BADXDR:
931 case -NFS4ERR_RESOURCE:
932 case -NFS4ERR_NOFILEHANDLE:
933 /* Non-seqid mutating errors */
934 return;
935 };
936 /*
937 * Note: no locking needed as we are guaranteed to be first
938 * on the sequence list
939 */
940 seqid->sequence->counter++;
941}
942
943void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
944{
Benny Halevy34dc1ad2009-04-01 09:22:52 -0400945 struct nfs4_state_owner *sp = container_of(seqid->sequence,
946 struct nfs4_state_owner, so_seqid);
947 struct nfs_server *server = sp->so_server;
948
949 if (status == -NFS4ERR_BAD_SEQID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 nfs4_drop_state_owner(sp);
Benny Halevy34dc1ad2009-04-01 09:22:52 -0400951 if (!nfs4_has_session(server->nfs_client))
952 nfs_increment_seqid(status, seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700953}
954
955/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700956 * Increment the seqid if the LOCK/LOCKU succeeded, or
957 * failed with a seqid incrementing error -
958 * see comments nfs_fs.h:seqid_mutating_error()
959 */
960void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
961{
Trond Myklebust88d90932007-07-02 14:03:03 -0400962 nfs_increment_seqid(status, seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700963}
964
965int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
966{
967 struct rpc_sequence *sequence = seqid->sequence->sequence;
968 int status = 0;
969
970 spin_lock(&sequence->lock);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500971 if (list_empty(&seqid->list))
972 list_add_tail(&seqid->list, &sequence->list);
973 if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
974 goto unlock;
Trond Myklebust5d008372008-02-22 16:34:17 -0500975 rpc_sleep_on(&sequence->wait, task, NULL);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500976 status = -EAGAIN;
977unlock:
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700978 spin_unlock(&sequence->lock);
979 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
Trond Myklebuste005e802008-12-23 15:21:48 -0500982static int nfs4_run_state_manager(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Trond Myklebuste005e802008-12-23 15:21:48 -0500984static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
Trond Myklebust433fbe42006-01-03 09:55:22 +0100985{
986 smp_mb__before_clear_bit();
Trond Myklebuste005e802008-12-23 15:21:48 -0500987 clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
Trond Myklebust433fbe42006-01-03 09:55:22 +0100988 smp_mb__after_clear_bit();
Trond Myklebuste005e802008-12-23 15:21:48 -0500989 wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
Trond Myklebust433fbe42006-01-03 09:55:22 +0100990 rpc_wake_up(&clp->cl_rpcwaitq);
991}
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993/*
Trond Myklebuste005e802008-12-23 15:21:48 -0500994 * Schedule the nfs_client asynchronous state management routine
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 */
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500996void nfs4_schedule_state_manager(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Trond Myklebust5043e902006-01-03 09:55:23 +0100998 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Trond Myklebuste005e802008-12-23 15:21:48 -05001000 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1001 return;
Trond Myklebust5043e902006-01-03 09:55:23 +01001002 __module_get(THIS_MODULE);
1003 atomic_inc(&clp->cl_count);
Trond Myklebuste005e802008-12-23 15:21:48 -05001004 task = kthread_run(nfs4_run_state_manager, clp, "%s-manager",
Chuck Lever5d8515c2007-12-10 14:57:16 -05001005 rpc_peeraddr2str(clp->cl_rpcclient,
1006 RPC_DISPLAY_ADDR));
Trond Myklebust5043e902006-01-03 09:55:23 +01001007 if (!IS_ERR(task))
1008 return;
Trond Myklebuste005e802008-12-23 15:21:48 -05001009 nfs4_clear_state_manager_bit(clp);
David Howells24c8dbb2006-08-22 20:06:10 -04001010 nfs_put_client(clp);
Trond Myklebust5043e902006-01-03 09:55:23 +01001011 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
1014/*
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001015 * Schedule a lease recovery attempt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001017void nfs4_schedule_lease_recovery(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 if (!clp)
1020 return;
Trond Myklebuste598d842008-12-23 15:21:42 -05001021 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1022 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
Trond Myklebuste005e802008-12-23 15:21:48 -05001023 nfs4_schedule_state_manager(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
Trond Myklebustf9feab12011-03-09 16:12:46 -05001026static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001027{
1028
1029 set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1030 /* Don't recover state that expired before the reboot */
1031 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1032 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1033 return 0;
1034 }
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001035 set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001036 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1037 return 1;
1038}
1039
Trond Myklebustf9feab12011-03-09 16:12:46 -05001040static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001041{
1042 set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1043 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001044 set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001045 set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1046 return 1;
1047}
1048
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001049void nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
1050{
1051 struct nfs_client *clp = server->nfs_client;
1052
1053 nfs4_state_mark_reclaim_nograce(clp, state);
1054 nfs4_schedule_state_manager(clp);
1055}
1056
Trond Myklebust02860012008-12-23 15:21:40 -05001057static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
1059 struct inode *inode = state->inode;
Trond Myklebust19e03c52008-12-23 15:21:44 -05001060 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 struct file_lock *fl;
1062 int status = 0;
1063
Trond Myklebust3f09df72009-06-17 13:23:00 -07001064 if (inode->i_flock == NULL)
1065 return 0;
1066
1067 /* Guard against delegation returns and new lock/unlock calls */
Trond Myklebust19e03c52008-12-23 15:21:44 -05001068 down_write(&nfsi->rwsem);
Trond Myklebust3f09df72009-06-17 13:23:00 -07001069 /* Protect inode->i_flock using the BKL */
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001070 lock_flocks();
Harvey Harrison90dc7d22008-02-20 13:03:05 -08001071 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
Trond Myklebust43b2a332005-11-04 15:35:30 -05001072 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 continue;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001074 if (nfs_file_open_context(fl->fl_file)->state != state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 continue;
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001076 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 status = ops->recover_lock(state, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 switch (status) {
Trond Myklebust965b5d62009-06-17 13:22:59 -07001079 case 0:
1080 break;
1081 case -ESTALE:
1082 case -NFS4ERR_ADMIN_REVOKED:
1083 case -NFS4ERR_STALE_STATEID:
1084 case -NFS4ERR_BAD_STATEID:
1085 case -NFS4ERR_EXPIRED:
1086 case -NFS4ERR_NO_GRACE:
1087 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust9c4c7612009-12-03 15:58:56 -05001088 case -NFS4ERR_BADSESSION:
1089 case -NFS4ERR_BADSLOT:
1090 case -NFS4ERR_BAD_HIGH_SLOT:
1091 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001092 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 default:
1094 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001095 __func__, status);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001096 case -ENOMEM:
1097 case -NFS4ERR_DENIED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 case -NFS4ERR_RECLAIM_BAD:
1099 case -NFS4ERR_RECLAIM_CONFLICT:
Trond Myklebust43b2a332005-11-04 15:35:30 -05001100 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001101 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001103 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001105 unlock_flocks();
Trond Myklebust965b5d62009-06-17 13:22:59 -07001106out:
Trond Myklebust19e03c52008-12-23 15:21:44 -05001107 up_write(&nfsi->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return status;
1109}
1110
Trond Myklebust02860012008-12-23 15:21:40 -05001111static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
1113 struct nfs4_state *state;
1114 struct nfs4_lock_state *lock;
1115 int status = 0;
1116
1117 /* Note: we rely on the sp->so_states list being ordered
1118 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1119 * states first.
1120 * This is needed to ensure that the server won't give us any
1121 * read delegations that we have to return if, say, we are
1122 * recovering after a network partition or a reboot from a
1123 * server that doesn't support a grace period.
1124 */
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001125restart:
1126 spin_lock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 list_for_each_entry(state, &sp->so_states, open_states) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001128 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1129 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (state->state == 0)
1131 continue;
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001132 atomic_inc(&state->count);
1133 spin_unlock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 status = ops->recover_open(sp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (status >= 0) {
Trond Myklebust02860012008-12-23 15:21:40 -05001136 status = nfs4_reclaim_locks(state, ops);
1137 if (status >= 0) {
1138 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1139 if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
1140 printk("%s: Lock reclaim failed!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001141 __func__);
Trond Myklebust02860012008-12-23 15:21:40 -05001142 }
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001143 nfs4_put_open_state(state);
1144 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147 switch (status) {
1148 default:
1149 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001150 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 case -ENOENT:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001152 case -ENOMEM:
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001153 case -ESTALE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 /*
1155 * Open state on this file cannot be recovered
1156 * All we can do is revert to using the zero stateid.
1157 */
1158 memset(state->stateid.data, 0,
1159 sizeof(state->stateid.data));
1160 /* Mark the file as being 'closed' */
1161 state->state = 0;
1162 break;
Trond Myklebust168667c2010-10-19 19:47:49 -04001163 case -EKEYEXPIRED:
1164 /*
1165 * User RPCSEC_GSS context has expired.
1166 * We cannot recover this stateid now, so
1167 * skip it and allow recovery thread to
1168 * proceed.
1169 */
1170 break;
Trond Myklebust965b5d62009-06-17 13:22:59 -07001171 case -NFS4ERR_ADMIN_REVOKED:
1172 case -NFS4ERR_STALE_STATEID:
1173 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001174 case -NFS4ERR_RECLAIM_BAD:
1175 case -NFS4ERR_RECLAIM_CONFLICT:
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001176 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 case -NFS4ERR_EXPIRED:
1179 case -NFS4ERR_NO_GRACE:
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001180 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust9c4c7612009-12-03 15:58:56 -05001182 case -NFS4ERR_BADSESSION:
1183 case -NFS4ERR_BADSLOT:
1184 case -NFS4ERR_BAD_HIGH_SLOT:
1185 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 goto out_err;
1187 }
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001188 nfs4_put_open_state(state);
1189 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001191 spin_unlock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return 0;
1193out_err:
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001194 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return status;
1196}
1197
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001198static void nfs4_clear_open_state(struct nfs4_state *state)
1199{
1200 struct nfs4_lock_state *lock;
1201
1202 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1203 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1204 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1205 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1206 list_for_each_entry(lock, &state->lock_states, ls_locks) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001207 lock->ls_seqid.flags = 0;
1208 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
1209 }
1210}
1211
Chuck Lever24d292b2010-12-24 01:32:43 +00001212static void nfs4_reset_seqids(struct nfs_server *server,
1213 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001214{
Chuck Lever24d292b2010-12-24 01:32:43 +00001215 struct nfs_client *clp = server->nfs_client;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001216 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001217 struct rb_node *pos;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001218 struct nfs4_state *state;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001219
Chuck Lever24d292b2010-12-24 01:32:43 +00001220 spin_lock(&clp->cl_lock);
1221 for (pos = rb_first(&server->state_owners);
1222 pos != NULL;
1223 pos = rb_next(pos)) {
1224 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001225 sp->so_seqid.flags = 0;
Trond Myklebustec073422005-10-20 14:22:47 -07001226 spin_lock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001227 list_for_each_entry(state, &sp->so_states, open_states) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001228 if (mark_reclaim(clp, state))
1229 nfs4_clear_open_state(state);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001230 }
Trond Myklebustec073422005-10-20 14:22:47 -07001231 spin_unlock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001232 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001233 spin_unlock(&clp->cl_lock);
1234}
1235
1236static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1237 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1238{
1239 struct nfs_server *server;
1240
1241 rcu_read_lock();
1242 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1243 nfs4_reset_seqids(server, mark_reclaim);
1244 rcu_read_unlock();
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001245}
1246
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001247static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1248{
1249 /* Mark all delegations for reclaim */
1250 nfs_delegation_mark_reclaim(clp);
1251 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1252}
1253
Ricardo Labiagafce5c832009-12-05 16:08:41 -05001254static void nfs4_reclaim_complete(struct nfs_client *clp,
1255 const struct nfs4_state_recovery_ops *ops)
1256{
1257 /* Notify the server we're done reclaiming our state */
1258 if (ops->reclaim_complete)
1259 (void)ops->reclaim_complete(clp);
1260}
1261
Chuck Lever24d292b2010-12-24 01:32:43 +00001262static void nfs4_clear_reclaim_server(struct nfs_server *server)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001263{
Chuck Lever24d292b2010-12-24 01:32:43 +00001264 struct nfs_client *clp = server->nfs_client;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001265 struct nfs4_state_owner *sp;
1266 struct rb_node *pos;
1267 struct nfs4_state *state;
1268
Chuck Lever24d292b2010-12-24 01:32:43 +00001269 spin_lock(&clp->cl_lock);
1270 for (pos = rb_first(&server->state_owners);
1271 pos != NULL;
1272 pos = rb_next(pos)) {
1273 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001274 spin_lock(&sp->so_lock);
1275 list_for_each_entry(state, &sp->so_states, open_states) {
Chuck Lever24d292b2010-12-24 01:32:43 +00001276 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1277 &state->flags))
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001278 continue;
1279 nfs4_state_mark_reclaim_nograce(clp, state);
1280 }
1281 spin_unlock(&sp->so_lock);
1282 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001283 spin_unlock(&clp->cl_lock);
1284}
1285
1286static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1287{
1288 struct nfs_server *server;
1289
1290 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1291 return 0;
1292
1293 rcu_read_lock();
1294 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1295 nfs4_clear_reclaim_server(server);
1296 rcu_read_unlock();
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001297
1298 nfs_delegation_reap_unclaimed(clp);
Trond Myklebust6eaa6142010-10-04 17:59:08 -04001299 return 1;
1300}
1301
1302static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1303{
1304 if (!nfs4_state_clear_reclaim_reboot(clp))
1305 return;
1306 nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001307}
1308
1309static void nfs_delegation_clear_all(struct nfs_client *clp)
1310{
1311 nfs_delegation_mark_reclaim(clp);
1312 nfs_delegation_reap_unclaimed(clp);
1313}
1314
1315static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1316{
1317 nfs_delegation_clear_all(clp);
1318 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1319}
1320
Trond Myklebust168667c2010-10-19 19:47:49 -04001321static void nfs4_warn_keyexpired(const char *s)
1322{
1323 printk_ratelimited(KERN_WARNING "Error: state manager"
1324 " encountered RPCSEC_GSS session"
1325 " expired against NFSv4 server %s.\n",
1326 s);
1327}
1328
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001329static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
Trond Myklebuste598d842008-12-23 15:21:42 -05001330{
1331 switch (error) {
1332 case -NFS4ERR_CB_PATH_DOWN:
Trond Myklebust707fb4b2008-12-23 15:21:47 -05001333 nfs_handle_cb_pathdown(clp);
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001334 return 0;
Trond Myklebustc8b7ae3d2009-12-03 15:53:21 -05001335 case -NFS4ERR_NO_GRACE:
1336 nfs4_state_end_reclaim_reboot(clp);
1337 return 0;
Trond Myklebuste598d842008-12-23 15:21:42 -05001338 case -NFS4ERR_STALE_CLIENTID:
1339 case -NFS4ERR_LEASE_MOVED:
1340 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Trond Myklebust6eaa6142010-10-04 17:59:08 -04001341 nfs4_state_clear_reclaim_reboot(clp);
Trond Myklebuste598d842008-12-23 15:21:42 -05001342 nfs4_state_start_reclaim_reboot(clp);
1343 break;
1344 case -NFS4ERR_EXPIRED:
1345 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1346 nfs4_state_start_reclaim_nograce(clp);
Andy Adamson8ba9bf82009-12-04 15:55:34 -05001347 break;
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001348 case -NFS4ERR_BADSESSION:
1349 case -NFS4ERR_BADSLOT:
1350 case -NFS4ERR_BAD_HIGH_SLOT:
1351 case -NFS4ERR_DEADSESSION:
1352 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1353 case -NFS4ERR_SEQ_FALSE_RETRY:
1354 case -NFS4ERR_SEQ_MISORDERED:
Andy Adamson6df08182009-12-04 15:55:05 -05001355 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05001356 /* Zero session reset errors */
1357 return 0;
Trond Myklebust168667c2010-10-19 19:47:49 -04001358 case -EKEYEXPIRED:
1359 /* Nothing we can do */
1360 nfs4_warn_keyexpired(clp->cl_hostname);
1361 return 0;
Trond Myklebuste598d842008-12-23 15:21:42 -05001362 }
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001363 return error;
Trond Myklebuste598d842008-12-23 15:21:42 -05001364}
1365
Trond Myklebust02860012008-12-23 15:21:40 -05001366static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
Chuck Lever24d292b2010-12-24 01:32:43 +00001368 struct nfs4_state_owner *sp;
1369 struct nfs_server *server;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001370 struct rb_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 int status = 0;
1372
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001373restart:
Chuck Lever24d292b2010-12-24 01:32:43 +00001374 rcu_read_lock();
1375 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1376 spin_lock(&clp->cl_lock);
1377 for (pos = rb_first(&server->state_owners);
1378 pos != NULL;
1379 pos = rb_next(pos)) {
1380 sp = rb_entry(pos,
1381 struct nfs4_state_owner, so_server_node);
1382 if (!test_and_clear_bit(ops->owner_flag_bit,
1383 &sp->so_flags))
1384 continue;
1385 atomic_inc(&sp->so_count);
1386 spin_unlock(&clp->cl_lock);
1387 rcu_read_unlock();
1388
1389 status = nfs4_reclaim_open_state(sp, ops);
1390 if (status < 0) {
1391 set_bit(ops->owner_flag_bit, &sp->so_flags);
1392 nfs4_put_state_owner(sp);
1393 return nfs4_recovery_handle_error(clp, status);
1394 }
1395
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001396 nfs4_put_state_owner(sp);
Chuck Lever24d292b2010-12-24 01:32:43 +00001397 goto restart;
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001398 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001399 spin_unlock(&clp->cl_lock);
Trond Myklebust02860012008-12-23 15:21:40 -05001400 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001401 rcu_read_unlock();
Trond Myklebust02860012008-12-23 15:21:40 -05001402 return status;
1403}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Trond Myklebust02860012008-12-23 15:21:40 -05001405static int nfs4_check_lease(struct nfs_client *clp)
1406{
1407 struct rpc_cred *cred;
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001408 const struct nfs4_state_maintenance_ops *ops =
1409 clp->cl_mvops->state_renewal_ops;
Trond Myklebust02860012008-12-23 15:21:40 -05001410 int status = -NFS4ERR_EXPIRED;
1411
Trond Myklebust0f605b52008-12-23 15:21:42 -05001412 /* Is the client already known to have an expired lease? */
1413 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1414 return 0;
Andy Adamsona7b72102009-04-01 09:22:46 -04001415 spin_lock(&clp->cl_lock);
1416 cred = ops->get_state_renewal_cred_locked(clp);
1417 spin_unlock(&clp->cl_lock);
Trond Myklebust0f605b52008-12-23 15:21:42 -05001418 if (cred == NULL) {
1419 cred = nfs4_get_setclientid_cred(clp);
1420 if (cred == NULL)
1421 goto out;
Trond Myklebust02860012008-12-23 15:21:40 -05001422 }
Benny Halevy8e69514f2009-04-01 09:22:45 -04001423 status = ops->renew_lease(clp, cred);
Trond Myklebust0f605b52008-12-23 15:21:42 -05001424 put_rpccred(cred);
1425out:
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001426 return nfs4_recovery_handle_error(clp, status);
Trond Myklebust02860012008-12-23 15:21:40 -05001427}
1428
1429static int nfs4_reclaim_lease(struct nfs_client *clp)
1430{
1431 struct rpc_cred *cred;
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001432 const struct nfs4_state_recovery_ops *ops =
1433 clp->cl_mvops->reboot_recovery_ops;
Trond Myklebust02860012008-12-23 15:21:40 -05001434 int status = -ENOENT;
1435
Andy Adamson90a16612009-04-01 09:22:48 -04001436 cred = ops->get_clid_cred(clp);
Trond Myklebust02860012008-12-23 15:21:40 -05001437 if (cred != NULL) {
Andy Adamson591d71c2009-04-01 09:22:47 -04001438 status = ops->establish_clid(clp, cred);
Trond Myklebust02860012008-12-23 15:21:40 -05001439 put_rpccred(cred);
1440 /* Handle case where the user hasn't set up machine creds */
1441 if (status == -EACCES && cred == clp->cl_machine_cred) {
1442 nfs4_clear_machine_cred(clp);
1443 status = -EAGAIN;
1444 }
Benny Halevyc2e713d2009-04-01 09:21:51 -04001445 if (status == -NFS4ERR_MINOR_VERS_MISMATCH)
1446 status = -EPROTONOSUPPORT;
Trond Myklebust02860012008-12-23 15:21:40 -05001447 }
1448 return status;
1449}
1450
Andy Adamson76db6d92009-04-01 09:22:38 -04001451#ifdef CONFIG_NFS_V4_1
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001452void nfs4_schedule_session_recovery(struct nfs4_session *session)
1453{
1454 nfs4_schedule_lease_recovery(session->clp);
1455}
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00001456EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001457
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001458void nfs41_handle_recall_slot(struct nfs_client *clp)
1459{
1460 set_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001461 nfs4_schedule_state_manager(clp);
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001462}
1463
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001464static void nfs4_reset_all_state(struct nfs_client *clp)
1465{
1466 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1467 clp->cl_boot_time = CURRENT_TIME;
1468 nfs4_state_start_reclaim_nograce(clp);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001469 nfs4_schedule_state_manager(clp);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001470 }
1471}
1472
1473static void nfs41_handle_server_reboot(struct nfs_client *clp)
1474{
1475 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1476 nfs4_state_start_reclaim_reboot(clp);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001477 nfs4_schedule_state_manager(clp);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001478 }
1479}
1480
1481static void nfs41_handle_state_revoked(struct nfs_client *clp)
1482{
1483 /* Temporary */
1484 nfs4_reset_all_state(clp);
1485}
1486
1487static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
1488{
1489 /* This will need to handle layouts too */
1490 nfs_expire_all_delegations(clp);
1491}
1492
1493static void nfs41_handle_cb_path_down(struct nfs_client *clp)
1494{
1495 nfs_expire_all_delegations(clp);
1496 if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001497 nfs4_schedule_state_manager(clp);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001498}
1499
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001500void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
1501{
1502 if (!flags)
1503 return;
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001504 else if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
1505 nfs41_handle_server_reboot(clp);
1506 else if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001507 SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
1508 SEQ4_STATUS_ADMIN_STATE_REVOKED |
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001509 SEQ4_STATUS_LEASE_MOVED))
1510 nfs41_handle_state_revoked(clp);
1511 else if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
1512 nfs41_handle_recallable_state_revoked(clp);
1513 else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001514 SEQ4_STATUS_BACKCHANNEL_FAULT |
1515 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001516 nfs41_handle_cb_path_down(clp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001517}
1518
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001519static int nfs4_reset_session(struct nfs_client *clp)
1520{
1521 int status;
1522
Trond Myklebust38045412009-12-15 17:36:57 -05001523 nfs4_begin_drain_session(clp);
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001524 status = nfs4_proc_destroy_session(clp->cl_session);
1525 if (status && status != -NFS4ERR_BADSESSION &&
1526 status != -NFS4ERR_DEADSESSION) {
Ricardo Labiagaf4558482009-12-07 09:16:09 -05001527 status = nfs4_recovery_handle_error(clp, status);
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001528 goto out;
1529 }
1530
1531 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
Trond Myklebustf26468f2009-12-05 19:32:11 -05001532 status = nfs4_proc_create_session(clp);
Andy Adamson41f54a52010-01-21 14:54:13 -05001533 if (status) {
Ricardo Labiagaf4558482009-12-07 09:16:09 -05001534 status = nfs4_recovery_handle_error(clp, status);
Andy Adamson41f54a52010-01-21 14:54:13 -05001535 goto out;
1536 }
1537 /* create_session negotiated new slot table */
1538 clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05001539
Andy Adamson41f54a52010-01-21 14:54:13 -05001540 /* Let the state manager reestablish state */
1541 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001542 nfs41_setup_state_renewal(clp);
Andy Adamson41f54a52010-01-21 14:54:13 -05001543out:
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001544 return status;
1545}
Andy Adamson76db6d92009-04-01 09:22:38 -04001546
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001547static int nfs4_recall_slot(struct nfs_client *clp)
1548{
1549 struct nfs4_slot_table *fc_tbl = &clp->cl_session->fc_slot_table;
1550 struct nfs4_channel_attrs *fc_attrs = &clp->cl_session->fc_attrs;
1551 struct nfs4_slot *new, *old;
1552 int i;
1553
1554 nfs4_begin_drain_session(clp);
1555 new = kmalloc(fc_tbl->target_max_slots * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001556 GFP_NOFS);
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001557 if (!new)
1558 return -ENOMEM;
1559
1560 spin_lock(&fc_tbl->slot_tbl_lock);
1561 for (i = 0; i < fc_tbl->target_max_slots; i++)
1562 new[i].seq_nr = fc_tbl->slots[i].seq_nr;
1563 old = fc_tbl->slots;
1564 fc_tbl->slots = new;
1565 fc_tbl->max_slots = fc_tbl->target_max_slots;
1566 fc_tbl->target_max_slots = 0;
1567 fc_attrs->max_reqs = fc_tbl->max_slots;
1568 spin_unlock(&fc_tbl->slot_tbl_lock);
1569
1570 kfree(old);
1571 nfs4_end_drain_session(clp);
1572 return 0;
1573}
1574
Andy Adamson76db6d92009-04-01 09:22:38 -04001575#else /* CONFIG_NFS_V4_1 */
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001576static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001577static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001578static int nfs4_recall_slot(struct nfs_client *clp) { return 0; }
Andy Adamson76db6d92009-04-01 09:22:38 -04001579#endif /* CONFIG_NFS_V4_1 */
1580
Andy Adamson78722e92009-06-16 14:43:15 -04001581/* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
1582 * on EXCHANGE_ID for v4.1
1583 */
1584static void nfs4_set_lease_expired(struct nfs_client *clp, int status)
1585{
1586 if (nfs4_has_session(clp)) {
1587 switch (status) {
1588 case -NFS4ERR_DELAY:
1589 case -NFS4ERR_CLID_INUSE:
1590 case -EAGAIN:
1591 break;
1592
Trond Myklebust168667c2010-10-19 19:47:49 -04001593 case -EKEYEXPIRED:
1594 nfs4_warn_keyexpired(clp->cl_hostname);
Andy Adamson78722e92009-06-16 14:43:15 -04001595 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1596 * in nfs4_exchange_id */
1597 default:
1598 return;
1599 }
1600 }
1601 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1602}
1603
Trond Myklebuste005e802008-12-23 15:21:48 -05001604static void nfs4_state_manager(struct nfs_client *clp)
Trond Myklebust02860012008-12-23 15:21:40 -05001605{
Trond Myklebust02860012008-12-23 15:21:40 -05001606 int status = 0;
1607
Trond Myklebust02860012008-12-23 15:21:40 -05001608 /* Ensure exclusive access to NFSv4 state */
Trond Myklebustf3c76492008-12-23 15:21:48 -05001609 for(;;) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001610 if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
1611 /* We're going to have to re-establish a clientid */
1612 status = nfs4_reclaim_lease(clp);
1613 if (status) {
Andy Adamson78722e92009-06-16 14:43:15 -04001614 nfs4_set_lease_expired(clp, status);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001615 if (test_bit(NFS4CLNT_LEASE_EXPIRED,
1616 &clp->cl_state))
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001617 continue;
Andy Adamson76db6d92009-04-01 09:22:38 -04001618 if (clp->cl_cons_state ==
1619 NFS_CS_SESSION_INITING)
1620 nfs_mark_client_ready(clp, status);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001621 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 }
Trond Myklebuste598d842008-12-23 15:21:42 -05001623 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
Ricardo Labiaga7cab89b2009-12-08 14:35:28 -05001624 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
Andy Adamson974cec82010-10-20 00:18:02 -04001625 pnfs_destroy_all_layouts(clp);
Trond Myklebuste598d842008-12-23 15:21:42 -05001626 }
1627
1628 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
1629 status = nfs4_check_lease(clp);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001630 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
Trond Myklebuste598d842008-12-23 15:21:42 -05001631 continue;
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001632 if (status < 0 && status != -NFS4ERR_CB_PATH_DOWN)
1633 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001635
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001636 /* Initialize or reset the session */
Andy Adamson6df08182009-12-04 15:55:05 -05001637 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)
Trond Myklebust7111dc72009-08-24 19:21:29 -04001638 && nfs4_has_session(clp)) {
Andy Adamson4d643d12009-12-04 15:52:24 -05001639 status = nfs4_reset_session(clp);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001640 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1641 continue;
1642 if (status < 0)
Andy Adamson76db6d92009-04-01 09:22:38 -04001643 goto out_error;
Andy Adamson76db6d92009-04-01 09:22:38 -04001644 }
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001645
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001646 /* First recover reboot state... */
Trond Myklebuste345e882009-12-03 15:52:41 -05001647 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
Andy Adamson591d71c2009-04-01 09:22:47 -04001648 status = nfs4_do_reclaim(clp,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001649 clp->cl_mvops->reboot_recovery_ops);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001650 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
Andy Adamson6df08182009-12-04 15:55:05 -05001651 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001652 continue;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001653 nfs4_state_end_reclaim_reboot(clp);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001654 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1655 continue;
1656 if (status < 0)
1657 goto out_error;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001658 }
1659
1660 /* Now recover expired state... */
1661 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
Andy Adamson591d71c2009-04-01 09:22:47 -04001662 status = nfs4_do_reclaim(clp,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001663 clp->cl_mvops->nograce_recovery_ops);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001664 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
Andy Adamson6df08182009-12-04 15:55:05 -05001665 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001666 test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1667 continue;
1668 if (status < 0)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001669 goto out_error;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001670 }
Trond Myklebust707fb4b2008-12-23 15:21:47 -05001671
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001672 nfs4_end_drain_session(clp);
Trond Myklebust707fb4b2008-12-23 15:21:47 -05001673 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
1674 nfs_client_return_marked_delegations(clp);
1675 continue;
1676 }
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001677 /* Recall session slots */
1678 if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state)
1679 && nfs4_has_session(clp)) {
1680 status = nfs4_recall_slot(clp);
1681 if (status < 0)
1682 goto out_error;
1683 continue;
1684 }
1685
Trond Myklebuste005e802008-12-23 15:21:48 -05001686
1687 nfs4_clear_state_manager_bit(clp);
Trond Myklebustf3c76492008-12-23 15:21:48 -05001688 /* Did we race with an attempt to give us more work? */
1689 if (clp->cl_state == 0)
1690 break;
1691 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1692 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
Trond Myklebuste005e802008-12-23 15:21:48 -05001694 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695out_error:
Trond Myklebuste005e802008-12-23 15:21:48 -05001696 printk(KERN_WARNING "Error: state manager failed on NFSv4 server %s"
Chuck Lever5d8515c2007-12-10 14:57:16 -05001697 " with error %d\n", clp->cl_hostname, -status);
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001698 nfs4_end_drain_session(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05001699 nfs4_clear_state_manager_bit(clp);
1700}
1701
1702static int nfs4_run_state_manager(void *ptr)
1703{
1704 struct nfs_client *clp = ptr;
1705
1706 allow_signal(SIGKILL);
1707 nfs4_state_manager(clp);
1708 nfs_put_client(clp);
1709 module_put_and_exit(0);
1710 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711}
1712
1713/*
1714 * Local variables:
1715 * c-basic-offset: 8
1716 * End:
1717 */