blob: 4dfb34b43ffb29348ff5653f546b6d0931388666 [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);
Dave Chinner0444d762011-03-29 18:08:50 +1100593 ihold(inode);
594 state->inode = inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 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 Myklebustba683032007-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/*
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001016 * Schedule a lease recovery attempt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001018void nfs4_schedule_lease_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 Myklebustf9feab12011-03-09 16:12:46 -05001027static int 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 Myklebustf9feab12011-03-09 16:12:46 -05001041static int 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 Myklebust0400a6b2011-03-09 16:00:53 -05001050void nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
1051{
1052 struct nfs_client *clp = server->nfs_client;
1053
1054 nfs4_state_mark_reclaim_nograce(clp, state);
1055 nfs4_schedule_state_manager(clp);
1056}
1057
Trond Myklebust02860012008-12-23 15:21:40 -05001058static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
1060 struct inode *inode = state->inode;
Trond Myklebust19e03c52008-12-23 15:21:44 -05001061 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 struct file_lock *fl;
1063 int status = 0;
1064
Trond Myklebust3f09df72009-06-17 13:23:00 -07001065 if (inode->i_flock == NULL)
1066 return 0;
1067
1068 /* Guard against delegation returns and new lock/unlock calls */
Trond Myklebust19e03c52008-12-23 15:21:44 -05001069 down_write(&nfsi->rwsem);
Trond Myklebust3f09df72009-06-17 13:23:00 -07001070 /* Protect inode->i_flock using the BKL */
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001071 lock_flocks();
Harvey Harrison90dc7d22008-02-20 13:03:05 -08001072 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
Trond Myklebust43b2a332005-11-04 15:35:30 -05001073 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 continue;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001075 if (nfs_file_open_context(fl->fl_file)->state != state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 continue;
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001077 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 status = ops->recover_lock(state, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 switch (status) {
Trond Myklebust965b5d62009-06-17 13:22:59 -07001080 case 0:
1081 break;
1082 case -ESTALE:
1083 case -NFS4ERR_ADMIN_REVOKED:
1084 case -NFS4ERR_STALE_STATEID:
1085 case -NFS4ERR_BAD_STATEID:
1086 case -NFS4ERR_EXPIRED:
1087 case -NFS4ERR_NO_GRACE:
1088 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust9c4c7612009-12-03 15:58:56 -05001089 case -NFS4ERR_BADSESSION:
1090 case -NFS4ERR_BADSLOT:
1091 case -NFS4ERR_BAD_HIGH_SLOT:
1092 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001093 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 default:
1095 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001096 __func__, status);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001097 case -ENOMEM:
1098 case -NFS4ERR_DENIED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 case -NFS4ERR_RECLAIM_BAD:
1100 case -NFS4ERR_RECLAIM_CONFLICT:
Trond Myklebust43b2a332005-11-04 15:35:30 -05001101 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001102 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001104 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001106 unlock_flocks();
Trond Myklebust965b5d62009-06-17 13:22:59 -07001107out:
Trond Myklebust19e03c52008-12-23 15:21:44 -05001108 up_write(&nfsi->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return status;
1110}
1111
Trond Myklebust02860012008-12-23 15:21:40 -05001112static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 struct nfs4_state *state;
1115 struct nfs4_lock_state *lock;
1116 int status = 0;
1117
1118 /* Note: we rely on the sp->so_states list being ordered
1119 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1120 * states first.
1121 * This is needed to ensure that the server won't give us any
1122 * read delegations that we have to return if, say, we are
1123 * recovering after a network partition or a reboot from a
1124 * server that doesn't support a grace period.
1125 */
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001126restart:
1127 spin_lock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 list_for_each_entry(state, &sp->so_states, open_states) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001129 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1130 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (state->state == 0)
1132 continue;
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001133 atomic_inc(&state->count);
1134 spin_unlock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 status = ops->recover_open(sp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (status >= 0) {
Trond Myklebust02860012008-12-23 15:21:40 -05001137 status = nfs4_reclaim_locks(state, ops);
1138 if (status >= 0) {
1139 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1140 if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
1141 printk("%s: Lock reclaim failed!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001142 __func__);
Trond Myklebust02860012008-12-23 15:21:40 -05001143 }
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001144 nfs4_put_open_state(state);
1145 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148 switch (status) {
1149 default:
1150 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001151 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 case -ENOENT:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001153 case -ENOMEM:
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001154 case -ESTALE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 /*
1156 * Open state on this file cannot be recovered
1157 * All we can do is revert to using the zero stateid.
1158 */
1159 memset(state->stateid.data, 0,
1160 sizeof(state->stateid.data));
1161 /* Mark the file as being 'closed' */
1162 state->state = 0;
1163 break;
Trond Myklebust168667c2010-10-19 19:47:49 -04001164 case -EKEYEXPIRED:
1165 /*
1166 * User RPCSEC_GSS context has expired.
1167 * We cannot recover this stateid now, so
1168 * skip it and allow recovery thread to
1169 * proceed.
1170 */
1171 break;
Trond Myklebust965b5d62009-06-17 13:22:59 -07001172 case -NFS4ERR_ADMIN_REVOKED:
1173 case -NFS4ERR_STALE_STATEID:
1174 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001175 case -NFS4ERR_RECLAIM_BAD:
1176 case -NFS4ERR_RECLAIM_CONFLICT:
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001177 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001178 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 case -NFS4ERR_EXPIRED:
1180 case -NFS4ERR_NO_GRACE:
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001181 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust9c4c7612009-12-03 15:58:56 -05001183 case -NFS4ERR_BADSESSION:
1184 case -NFS4ERR_BADSLOT:
1185 case -NFS4ERR_BAD_HIGH_SLOT:
1186 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 goto out_err;
1188 }
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001189 nfs4_put_open_state(state);
1190 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001192 spin_unlock(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return 0;
1194out_err:
Trond Myklebustfe1d8192008-12-23 15:21:43 -05001195 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return status;
1197}
1198
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001199static void nfs4_clear_open_state(struct nfs4_state *state)
1200{
1201 struct nfs4_lock_state *lock;
1202
1203 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1204 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1205 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1206 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1207 list_for_each_entry(lock, &state->lock_states, ls_locks) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001208 lock->ls_seqid.flags = 0;
1209 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
1210 }
1211}
1212
Chuck Lever24d292b2010-12-24 01:32:43 +00001213static void nfs4_reset_seqids(struct nfs_server *server,
1214 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001215{
Chuck Lever24d292b2010-12-24 01:32:43 +00001216 struct nfs_client *clp = server->nfs_client;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001217 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001218 struct rb_node *pos;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001219 struct nfs4_state *state;
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001220
Chuck Lever24d292b2010-12-24 01:32:43 +00001221 spin_lock(&clp->cl_lock);
1222 for (pos = rb_first(&server->state_owners);
1223 pos != NULL;
1224 pos = rb_next(pos)) {
1225 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001226 sp->so_seqid.flags = 0;
Trond Myklebustec073422005-10-20 14:22:47 -07001227 spin_lock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001228 list_for_each_entry(state, &sp->so_states, open_states) {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001229 if (mark_reclaim(clp, state))
1230 nfs4_clear_open_state(state);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001231 }
Trond Myklebustec073422005-10-20 14:22:47 -07001232 spin_unlock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001233 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001234 spin_unlock(&clp->cl_lock);
1235}
1236
1237static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1238 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1239{
1240 struct nfs_server *server;
1241
1242 rcu_read_lock();
1243 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1244 nfs4_reset_seqids(server, mark_reclaim);
1245 rcu_read_unlock();
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001246}
1247
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001248static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1249{
1250 /* Mark all delegations for reclaim */
1251 nfs_delegation_mark_reclaim(clp);
1252 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1253}
1254
Ricardo Labiagafce5c832009-12-05 16:08:41 -05001255static void nfs4_reclaim_complete(struct nfs_client *clp,
1256 const struct nfs4_state_recovery_ops *ops)
1257{
1258 /* Notify the server we're done reclaiming our state */
1259 if (ops->reclaim_complete)
1260 (void)ops->reclaim_complete(clp);
1261}
1262
Chuck Lever24d292b2010-12-24 01:32:43 +00001263static void nfs4_clear_reclaim_server(struct nfs_server *server)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001264{
Chuck Lever24d292b2010-12-24 01:32:43 +00001265 struct nfs_client *clp = server->nfs_client;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001266 struct nfs4_state_owner *sp;
1267 struct rb_node *pos;
1268 struct nfs4_state *state;
1269
Chuck Lever24d292b2010-12-24 01:32:43 +00001270 spin_lock(&clp->cl_lock);
1271 for (pos = rb_first(&server->state_owners);
1272 pos != NULL;
1273 pos = rb_next(pos)) {
1274 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001275 spin_lock(&sp->so_lock);
1276 list_for_each_entry(state, &sp->so_states, open_states) {
Chuck Lever24d292b2010-12-24 01:32:43 +00001277 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1278 &state->flags))
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001279 continue;
1280 nfs4_state_mark_reclaim_nograce(clp, state);
1281 }
1282 spin_unlock(&sp->so_lock);
1283 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001284 spin_unlock(&clp->cl_lock);
1285}
1286
1287static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1288{
1289 struct nfs_server *server;
1290
1291 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1292 return 0;
1293
1294 rcu_read_lock();
1295 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1296 nfs4_clear_reclaim_server(server);
1297 rcu_read_unlock();
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001298
1299 nfs_delegation_reap_unclaimed(clp);
Trond Myklebust6eaa6142010-10-04 17:59:08 -04001300 return 1;
1301}
1302
1303static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1304{
1305 if (!nfs4_state_clear_reclaim_reboot(clp))
1306 return;
1307 nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001308}
1309
1310static void nfs_delegation_clear_all(struct nfs_client *clp)
1311{
1312 nfs_delegation_mark_reclaim(clp);
1313 nfs_delegation_reap_unclaimed(clp);
1314}
1315
1316static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1317{
1318 nfs_delegation_clear_all(clp);
1319 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1320}
1321
Trond Myklebust168667c2010-10-19 19:47:49 -04001322static void nfs4_warn_keyexpired(const char *s)
1323{
1324 printk_ratelimited(KERN_WARNING "Error: state manager"
1325 " encountered RPCSEC_GSS session"
1326 " expired against NFSv4 server %s.\n",
1327 s);
1328}
1329
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001330static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
Trond Myklebuste598d842008-12-23 15:21:42 -05001331{
1332 switch (error) {
1333 case -NFS4ERR_CB_PATH_DOWN:
Trond Myklebust707fb4b2008-12-23 15:21:47 -05001334 nfs_handle_cb_pathdown(clp);
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001335 return 0;
Trond Myklebustc8b7ae3d2009-12-03 15:53:21 -05001336 case -NFS4ERR_NO_GRACE:
1337 nfs4_state_end_reclaim_reboot(clp);
1338 return 0;
Trond Myklebuste598d842008-12-23 15:21:42 -05001339 case -NFS4ERR_STALE_CLIENTID:
1340 case -NFS4ERR_LEASE_MOVED:
1341 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Trond Myklebust6eaa6142010-10-04 17:59:08 -04001342 nfs4_state_clear_reclaim_reboot(clp);
Trond Myklebuste598d842008-12-23 15:21:42 -05001343 nfs4_state_start_reclaim_reboot(clp);
1344 break;
1345 case -NFS4ERR_EXPIRED:
1346 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1347 nfs4_state_start_reclaim_nograce(clp);
Andy Adamson8ba9bf82009-12-04 15:55:34 -05001348 break;
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001349 case -NFS4ERR_BADSESSION:
1350 case -NFS4ERR_BADSLOT:
1351 case -NFS4ERR_BAD_HIGH_SLOT:
1352 case -NFS4ERR_DEADSESSION:
1353 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1354 case -NFS4ERR_SEQ_FALSE_RETRY:
1355 case -NFS4ERR_SEQ_MISORDERED:
Andy Adamson6df08182009-12-04 15:55:05 -05001356 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05001357 /* Zero session reset errors */
1358 return 0;
Trond Myklebust168667c2010-10-19 19:47:49 -04001359 case -EKEYEXPIRED:
1360 /* Nothing we can do */
1361 nfs4_warn_keyexpired(clp->cl_hostname);
1362 return 0;
Trond Myklebuste598d842008-12-23 15:21:42 -05001363 }
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001364 return error;
Trond Myklebuste598d842008-12-23 15:21:42 -05001365}
1366
Trond Myklebust02860012008-12-23 15:21:40 -05001367static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Chuck Lever24d292b2010-12-24 01:32:43 +00001369 struct nfs4_state_owner *sp;
1370 struct nfs_server *server;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001371 struct rb_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 int status = 0;
1373
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001374restart:
Chuck Lever24d292b2010-12-24 01:32:43 +00001375 rcu_read_lock();
1376 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1377 spin_lock(&clp->cl_lock);
1378 for (pos = rb_first(&server->state_owners);
1379 pos != NULL;
1380 pos = rb_next(pos)) {
1381 sp = rb_entry(pos,
1382 struct nfs4_state_owner, so_server_node);
1383 if (!test_and_clear_bit(ops->owner_flag_bit,
1384 &sp->so_flags))
1385 continue;
1386 atomic_inc(&sp->so_count);
1387 spin_unlock(&clp->cl_lock);
1388 rcu_read_unlock();
1389
1390 status = nfs4_reclaim_open_state(sp, ops);
1391 if (status < 0) {
1392 set_bit(ops->owner_flag_bit, &sp->so_flags);
1393 nfs4_put_state_owner(sp);
1394 return nfs4_recovery_handle_error(clp, status);
1395 }
1396
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001397 nfs4_put_state_owner(sp);
Chuck Lever24d292b2010-12-24 01:32:43 +00001398 goto restart;
Trond Myklebust7eff03a2008-12-23 15:21:43 -05001399 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001400 spin_unlock(&clp->cl_lock);
Trond Myklebust02860012008-12-23 15:21:40 -05001401 }
Chuck Lever24d292b2010-12-24 01:32:43 +00001402 rcu_read_unlock();
Trond Myklebust02860012008-12-23 15:21:40 -05001403 return status;
1404}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Trond Myklebust02860012008-12-23 15:21:40 -05001406static int nfs4_check_lease(struct nfs_client *clp)
1407{
1408 struct rpc_cred *cred;
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001409 const struct nfs4_state_maintenance_ops *ops =
1410 clp->cl_mvops->state_renewal_ops;
Trond Myklebust02860012008-12-23 15:21:40 -05001411 int status = -NFS4ERR_EXPIRED;
1412
Trond Myklebust0f605b52008-12-23 15:21:42 -05001413 /* Is the client already known to have an expired lease? */
1414 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1415 return 0;
Andy Adamsona7b72102009-04-01 09:22:46 -04001416 spin_lock(&clp->cl_lock);
1417 cred = ops->get_state_renewal_cred_locked(clp);
1418 spin_unlock(&clp->cl_lock);
Trond Myklebust0f605b52008-12-23 15:21:42 -05001419 if (cred == NULL) {
1420 cred = nfs4_get_setclientid_cred(clp);
1421 if (cred == NULL)
1422 goto out;
Trond Myklebust02860012008-12-23 15:21:40 -05001423 }
Benny Halevy8e69514f2009-04-01 09:22:45 -04001424 status = ops->renew_lease(clp, cred);
Trond Myklebust0f605b52008-12-23 15:21:42 -05001425 put_rpccred(cred);
1426out:
Trond Myklebust4f7cdf12009-12-03 15:53:20 -05001427 return nfs4_recovery_handle_error(clp, status);
Trond Myklebust02860012008-12-23 15:21:40 -05001428}
1429
1430static int nfs4_reclaim_lease(struct nfs_client *clp)
1431{
1432 struct rpc_cred *cred;
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001433 const struct nfs4_state_recovery_ops *ops =
1434 clp->cl_mvops->reboot_recovery_ops;
Trond Myklebust02860012008-12-23 15:21:40 -05001435 int status = -ENOENT;
1436
Andy Adamson90a16612009-04-01 09:22:48 -04001437 cred = ops->get_clid_cred(clp);
Trond Myklebust02860012008-12-23 15:21:40 -05001438 if (cred != NULL) {
Andy Adamson591d71c2009-04-01 09:22:47 -04001439 status = ops->establish_clid(clp, cred);
Trond Myklebust02860012008-12-23 15:21:40 -05001440 put_rpccred(cred);
1441 /* Handle case where the user hasn't set up machine creds */
1442 if (status == -EACCES && cred == clp->cl_machine_cred) {
1443 nfs4_clear_machine_cred(clp);
1444 status = -EAGAIN;
1445 }
Benny Halevyc2e713d2009-04-01 09:21:51 -04001446 if (status == -NFS4ERR_MINOR_VERS_MISMATCH)
1447 status = -EPROTONOSUPPORT;
Trond Myklebust02860012008-12-23 15:21:40 -05001448 }
1449 return status;
1450}
1451
Andy Adamson76db6d92009-04-01 09:22:38 -04001452#ifdef CONFIG_NFS_V4_1
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001453void nfs4_schedule_session_recovery(struct nfs4_session *session)
1454{
1455 nfs4_schedule_lease_recovery(session->clp);
1456}
Andy Adamsoncbdabc72011-03-01 01:34:20 +00001457EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001458
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001459void nfs41_handle_recall_slot(struct nfs_client *clp)
1460{
1461 set_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001462 nfs4_schedule_state_manager(clp);
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001463}
1464
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001465static void nfs4_reset_all_state(struct nfs_client *clp)
1466{
1467 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1468 clp->cl_boot_time = CURRENT_TIME;
1469 nfs4_state_start_reclaim_nograce(clp);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001470 nfs4_schedule_state_manager(clp);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001471 }
1472}
1473
1474static void nfs41_handle_server_reboot(struct nfs_client *clp)
1475{
1476 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1477 nfs4_state_start_reclaim_reboot(clp);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001478 nfs4_schedule_state_manager(clp);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001479 }
1480}
1481
1482static void nfs41_handle_state_revoked(struct nfs_client *clp)
1483{
1484 /* Temporary */
1485 nfs4_reset_all_state(clp);
1486}
1487
1488static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
1489{
1490 /* This will need to handle layouts too */
1491 nfs_expire_all_delegations(clp);
1492}
1493
1494static void nfs41_handle_cb_path_down(struct nfs_client *clp)
1495{
1496 nfs_expire_all_delegations(clp);
1497 if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001498 nfs4_schedule_state_manager(clp);
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001499}
1500
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001501void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
1502{
1503 if (!flags)
1504 return;
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001505 else if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
1506 nfs41_handle_server_reboot(clp);
1507 else if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001508 SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
1509 SEQ4_STATUS_ADMIN_STATE_REVOKED |
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001510 SEQ4_STATUS_LEASE_MOVED))
1511 nfs41_handle_state_revoked(clp);
1512 else if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
1513 nfs41_handle_recallable_state_revoked(clp);
1514 else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001515 SEQ4_STATUS_BACKCHANNEL_FAULT |
1516 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
Trond Myklebust0f79fd62010-03-02 13:06:21 -05001517 nfs41_handle_cb_path_down(clp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -05001518}
1519
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001520static int nfs4_reset_session(struct nfs_client *clp)
1521{
1522 int status;
1523
Trond Myklebust38045412009-12-15 17:36:57 -05001524 nfs4_begin_drain_session(clp);
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001525 status = nfs4_proc_destroy_session(clp->cl_session);
1526 if (status && status != -NFS4ERR_BADSESSION &&
1527 status != -NFS4ERR_DEADSESSION) {
Ricardo Labiagaf4558482009-12-07 09:16:09 -05001528 status = nfs4_recovery_handle_error(clp, status);
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001529 goto out;
1530 }
1531
1532 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
Trond Myklebustf26468f2009-12-05 19:32:11 -05001533 status = nfs4_proc_create_session(clp);
Andy Adamson41f54a52010-01-21 14:54:13 -05001534 if (status) {
Ricardo Labiagaf4558482009-12-07 09:16:09 -05001535 status = nfs4_recovery_handle_error(clp, status);
Andy Adamson41f54a52010-01-21 14:54:13 -05001536 goto out;
1537 }
1538 /* create_session negotiated new slot table */
1539 clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05001540
Andy Adamson41f54a52010-01-21 14:54:13 -05001541 /* Let the state manager reestablish state */
1542 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001543 nfs41_setup_state_renewal(clp);
Andy Adamson41f54a52010-01-21 14:54:13 -05001544out:
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001545 return status;
1546}
Andy Adamson76db6d92009-04-01 09:22:38 -04001547
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001548static int nfs4_recall_slot(struct nfs_client *clp)
1549{
1550 struct nfs4_slot_table *fc_tbl = &clp->cl_session->fc_slot_table;
1551 struct nfs4_channel_attrs *fc_attrs = &clp->cl_session->fc_attrs;
1552 struct nfs4_slot *new, *old;
1553 int i;
1554
1555 nfs4_begin_drain_session(clp);
1556 new = kmalloc(fc_tbl->target_max_slots * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001557 GFP_NOFS);
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001558 if (!new)
1559 return -ENOMEM;
1560
1561 spin_lock(&fc_tbl->slot_tbl_lock);
1562 for (i = 0; i < fc_tbl->target_max_slots; i++)
1563 new[i].seq_nr = fc_tbl->slots[i].seq_nr;
1564 old = fc_tbl->slots;
1565 fc_tbl->slots = new;
1566 fc_tbl->max_slots = fc_tbl->target_max_slots;
1567 fc_tbl->target_max_slots = 0;
1568 fc_attrs->max_reqs = fc_tbl->max_slots;
1569 spin_unlock(&fc_tbl->slot_tbl_lock);
1570
1571 kfree(old);
1572 nfs4_end_drain_session(clp);
1573 return 0;
1574}
1575
Andy Adamson76db6d92009-04-01 09:22:38 -04001576#else /* CONFIG_NFS_V4_1 */
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001577static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001578static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001579static int nfs4_recall_slot(struct nfs_client *clp) { return 0; }
Andy Adamson76db6d92009-04-01 09:22:38 -04001580#endif /* CONFIG_NFS_V4_1 */
1581
Andy Adamson78722e92009-06-16 14:43:15 -04001582/* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
1583 * on EXCHANGE_ID for v4.1
1584 */
1585static void nfs4_set_lease_expired(struct nfs_client *clp, int status)
1586{
1587 if (nfs4_has_session(clp)) {
1588 switch (status) {
1589 case -NFS4ERR_DELAY:
1590 case -NFS4ERR_CLID_INUSE:
1591 case -EAGAIN:
1592 break;
1593
Trond Myklebust168667c2010-10-19 19:47:49 -04001594 case -EKEYEXPIRED:
1595 nfs4_warn_keyexpired(clp->cl_hostname);
Andy Adamson78722e92009-06-16 14:43:15 -04001596 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1597 * in nfs4_exchange_id */
1598 default:
1599 return;
1600 }
1601 }
1602 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1603}
1604
Trond Myklebuste005e802008-12-23 15:21:48 -05001605static void nfs4_state_manager(struct nfs_client *clp)
Trond Myklebust02860012008-12-23 15:21:40 -05001606{
Trond Myklebust02860012008-12-23 15:21:40 -05001607 int status = 0;
1608
Trond Myklebust02860012008-12-23 15:21:40 -05001609 /* Ensure exclusive access to NFSv4 state */
Trond Myklebust47c21992011-04-15 17:34:18 -04001610 do {
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001611 if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
1612 /* We're going to have to re-establish a clientid */
1613 status = nfs4_reclaim_lease(clp);
1614 if (status) {
Andy Adamson78722e92009-06-16 14:43:15 -04001615 nfs4_set_lease_expired(clp, status);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001616 if (test_bit(NFS4CLNT_LEASE_EXPIRED,
1617 &clp->cl_state))
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001618 continue;
Andy Adamson76db6d92009-04-01 09:22:38 -04001619 if (clp->cl_cons_state ==
1620 NFS_CS_SESSION_INITING)
1621 nfs_mark_client_ready(clp, status);
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001622 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
Trond Myklebuste598d842008-12-23 15:21:42 -05001624 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
Ricardo Labiaga7cab89b2009-12-08 14:35:28 -05001625 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
Andy Adamson974cec82010-10-20 00:18:02 -04001626 pnfs_destroy_all_layouts(clp);
Trond Myklebuste598d842008-12-23 15:21:42 -05001627 }
1628
1629 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
1630 status = nfs4_check_lease(clp);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001631 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
Trond Myklebuste598d842008-12-23 15:21:42 -05001632 continue;
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001633 if (status < 0 && status != -NFS4ERR_CB_PATH_DOWN)
1634 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001636
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001637 /* Initialize or reset the session */
Andy Adamson6df08182009-12-04 15:55:05 -05001638 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)
Trond Myklebust7111dc72009-08-24 19:21:29 -04001639 && nfs4_has_session(clp)) {
Andy Adamson4d643d12009-12-04 15:52:24 -05001640 status = nfs4_reset_session(clp);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001641 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1642 continue;
1643 if (status < 0)
Andy Adamson76db6d92009-04-01 09:22:38 -04001644 goto out_error;
Andy Adamson76db6d92009-04-01 09:22:38 -04001645 }
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001646
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001647 /* First recover reboot state... */
Trond Myklebuste345e882009-12-03 15:52:41 -05001648 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &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->reboot_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))
Andy Adamsonc3fad1b2009-04-01 09:22:39 -04001653 continue;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001654 nfs4_state_end_reclaim_reboot(clp);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001655 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1656 continue;
1657 if (status < 0)
1658 goto out_error;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001659 }
1660
1661 /* Now recover expired state... */
1662 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
Andy Adamson591d71c2009-04-01 09:22:47 -04001663 status = nfs4_do_reclaim(clp,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04001664 clp->cl_mvops->nograce_recovery_ops);
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001665 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
Andy Adamson6df08182009-12-04 15:55:05 -05001666 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
Trond Myklebustb6d408b2009-12-03 15:53:22 -05001667 test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1668 continue;
1669 if (status < 0)
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001670 goto out_error;
Trond Myklebustb79a4a12008-12-23 15:21:41 -05001671 }
Trond Myklebust707fb4b2008-12-23 15:21:47 -05001672
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001673 nfs4_end_drain_session(clp);
Trond Myklebust707fb4b2008-12-23 15:21:47 -05001674 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
1675 nfs_client_return_marked_delegations(clp);
1676 continue;
1677 }
Andy Adamsonb9efa1b2010-01-20 16:06:27 -05001678 /* Recall session slots */
1679 if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state)
1680 && nfs4_has_session(clp)) {
1681 status = nfs4_recall_slot(clp);
1682 if (status < 0)
1683 goto out_error;
1684 continue;
1685 }
1686
Trond Myklebuste005e802008-12-23 15:21:48 -05001687
1688 nfs4_clear_state_manager_bit(clp);
Trond Myklebustf3c76492008-12-23 15:21:48 -05001689 /* Did we race with an attempt to give us more work? */
1690 if (clp->cl_state == 0)
1691 break;
1692 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1693 break;
Trond Myklebust47c21992011-04-15 17:34:18 -04001694 } while (atomic_read(&clp->cl_count) > 1);
Trond Myklebuste005e802008-12-23 15:21:48 -05001695 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696out_error:
Trond Myklebuste005e802008-12-23 15:21:48 -05001697 printk(KERN_WARNING "Error: state manager failed on NFSv4 server %s"
Chuck Lever5d8515c2007-12-10 14:57:16 -05001698 " with error %d\n", clp->cl_hostname, -status);
Alexandros Batsakis5601a002009-12-14 21:27:58 -08001699 nfs4_end_drain_session(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05001700 nfs4_clear_state_manager_bit(clp);
1701}
1702
1703static int nfs4_run_state_manager(void *ptr)
1704{
1705 struct nfs_client *clp = ptr;
1706
1707 allow_signal(SIGKILL);
1708 nfs4_state_manager(clp);
1709 nfs_put_client(clp);
1710 module_put_and_exit(0);
1711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
1714/*
1715 * Local variables:
1716 * c-basic-offset: 8
1717 * End:
1718 */