blob: 856a8934f610ef4addd8ea4b51deb5d918dd5e53 [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>
43#include <linux/smp_lock.h>
44#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/workqueue.h>
50#include <linux/bitops.h>
51
Trond Myklebust4ce79712005-06-22 17:16:21 +000052#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include "callback.h"
54#include "delegation.h"
David Howells24c8dbb2006-08-22 20:06:10 -040055#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#define OPENOWNER_POOL_SIZE 8
58
Trond Myklebust4ce79712005-06-22 17:16:21 +000059const nfs4_stateid zero_stateid;
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static LIST_HEAD(nfs4_clientid_list);
62
David Howellsadfa6f92006-08-22 20:06:08 -040063static int nfs4_init_client(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Trond Myklebust286d7d62006-01-03 09:55:26 +010065 int status = nfs4_proc_setclientid(clp, NFS4_CALLBACK,
66 nfs_callback_tcpport, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 if (status == 0)
Trond Myklebust286d7d62006-01-03 09:55:26 +010068 status = nfs4_proc_setclientid_confirm(clp, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (status == 0)
70 nfs4_schedule_state_renewal(clp);
71 return status;
72}
73
Trond Myklebusta2b2bb82008-04-08 16:02:17 -040074static struct rpc_cred *nfs4_get_machine_cred(struct nfs_client *clp)
75{
76 struct rpc_cred *cred = NULL;
77
78 spin_lock(&clp->cl_lock);
79 if (clp->cl_machine_cred != NULL)
80 cred = get_rpccred(clp->cl_machine_cred);
81 spin_unlock(&clp->cl_lock);
82 return cred;
83}
84
85static void nfs4_clear_machine_cred(struct nfs_client *clp)
86{
87 struct rpc_cred *cred;
88
89 spin_lock(&clp->cl_lock);
90 cred = clp->cl_machine_cred;
91 clp->cl_machine_cred = NULL;
92 spin_unlock(&clp->cl_lock);
93 if (cred != NULL)
94 put_rpccred(cred);
95}
96
David Howellsadfa6f92006-08-22 20:06:08 -040097struct rpc_cred *nfs4_get_renew_cred(struct nfs_client *clp)
Trond Myklebustb4454fe2006-01-03 09:55:25 +010098{
99 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400100 struct rb_node *pos;
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100101 struct rpc_cred *cred = NULL;
102
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400103 for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
104 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100105 if (list_empty(&sp->so_states))
106 continue;
107 cred = get_rpccred(sp->so_cred);
108 break;
109 }
110 return cred;
111}
112
Trond Myklebust10afec92007-05-14 17:16:04 -0400113static struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
Trond Myklebust286d7d62006-01-03 09:55:26 +0100114{
115 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400116 struct rb_node *pos;
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400117 struct rpc_cred *cred;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100118
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400119 cred = nfs4_get_machine_cred(clp);
120 if (cred != NULL)
121 goto out;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400122 pos = rb_first(&clp->cl_state_owners);
123 if (pos != NULL) {
124 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400125 cred = get_rpccred(sp->so_cred);
Trond Myklebust286d7d62006-01-03 09:55:26 +0100126 }
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400127out:
128 return cred;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100129}
130
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400131static void nfs_alloc_unique_id(struct rb_root *root, struct nfs_unique_id *new,
132 __u64 minval, int maxbits)
133{
134 struct rb_node **p, *parent;
135 struct nfs_unique_id *pos;
136 __u64 mask = ~0ULL;
137
138 if (maxbits < 64)
139 mask = (1ULL << maxbits) - 1ULL;
140
141 /* Ensure distribution is more or less flat */
142 get_random_bytes(&new->id, sizeof(new->id));
143 new->id &= mask;
144 if (new->id < minval)
145 new->id += minval;
146retry:
147 p = &root->rb_node;
148 parent = NULL;
149
150 while (*p != NULL) {
151 parent = *p;
152 pos = rb_entry(parent, struct nfs_unique_id, rb_node);
153
154 if (new->id < pos->id)
155 p = &(*p)->rb_left;
156 else if (new->id > pos->id)
157 p = &(*p)->rb_right;
158 else
159 goto id_exists;
160 }
161 rb_link_node(&new->rb_node, parent, p);
162 rb_insert_color(&new->rb_node, root);
163 return;
164id_exists:
165 for (;;) {
166 new->id++;
167 if (new->id < minval || (new->id & mask) != new->id) {
168 new->id = minval;
169 break;
170 }
171 parent = rb_next(parent);
172 if (parent == NULL)
173 break;
174 pos = rb_entry(parent, struct nfs_unique_id, rb_node);
175 if (new->id < pos->id)
176 break;
177 }
178 goto retry;
179}
180
181static void nfs_free_unique_id(struct rb_root *root, struct nfs_unique_id *id)
182{
183 rb_erase(&id->rb_node, root);
184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static struct nfs4_state_owner *
Trond Myklebust6f2e64d2007-07-06 10:53:21 -0400187nfs4_find_state_owner(struct nfs_server *server, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Trond Myklebust6f2e64d2007-07-06 10:53:21 -0400189 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400190 struct rb_node **p = &clp->cl_state_owners.rb_node,
191 *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 struct nfs4_state_owner *sp, *res = NULL;
193
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400194 while (*p != NULL) {
195 parent = *p;
196 sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
197
Trond Myklebust6f2e64d2007-07-06 10:53:21 -0400198 if (server < sp->so_server) {
199 p = &parent->rb_left;
200 continue;
201 }
202 if (server > sp->so_server) {
203 p = &parent->rb_right;
204 continue;
205 }
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400206 if (cred < sp->so_cred)
207 p = &parent->rb_left;
208 else if (cred > sp->so_cred)
209 p = &parent->rb_right;
210 else {
211 atomic_inc(&sp->so_count);
212 res = sp;
213 break;
214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216 return res;
217}
218
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400219static struct nfs4_state_owner *
220nfs4_insert_state_owner(struct nfs_client *clp, struct nfs4_state_owner *new)
221{
222 struct rb_node **p = &clp->cl_state_owners.rb_node,
223 *parent = NULL;
224 struct nfs4_state_owner *sp;
225
226 while (*p != NULL) {
227 parent = *p;
228 sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
229
Trond Myklebust6f2e64d2007-07-06 10:53:21 -0400230 if (new->so_server < sp->so_server) {
231 p = &parent->rb_left;
232 continue;
233 }
234 if (new->so_server > sp->so_server) {
235 p = &parent->rb_right;
236 continue;
237 }
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400238 if (new->so_cred < sp->so_cred)
239 p = &parent->rb_left;
240 else if (new->so_cred > sp->so_cred)
241 p = &parent->rb_right;
242 else {
243 atomic_inc(&sp->so_count);
244 return sp;
245 }
246 }
247 nfs_alloc_unique_id(&clp->cl_openowner_id, &new->so_owner_id, 1, 64);
248 rb_link_node(&new->so_client_node, parent, p);
249 rb_insert_color(&new->so_client_node, &clp->cl_state_owners);
250 return new;
251}
252
253static void
254nfs4_remove_state_owner(struct nfs_client *clp, struct nfs4_state_owner *sp)
255{
256 if (!RB_EMPTY_NODE(&sp->so_client_node))
257 rb_erase(&sp->so_client_node, &clp->cl_state_owners);
258 nfs_free_unique_id(&clp->cl_openowner_id, &sp->so_owner_id);
259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261/*
262 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
263 * create a new state_owner.
264 *
265 */
266static struct nfs4_state_owner *
267nfs4_alloc_state_owner(void)
268{
269 struct nfs4_state_owner *sp;
270
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700271 sp = kzalloc(sizeof(*sp),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (!sp)
273 return NULL;
Trond Myklebustec073422005-10-20 14:22:47 -0700274 spin_lock_init(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 INIT_LIST_HEAD(&sp->so_states);
276 INIT_LIST_HEAD(&sp->so_delegations);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700277 rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
278 sp->so_seqid.sequence = &sp->so_sequence;
279 spin_lock_init(&sp->so_sequence.lock);
280 INIT_LIST_HEAD(&sp->so_sequence.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 atomic_set(&sp->so_count, 1);
282 return sp;
283}
284
Adrian Bunk1d2e88e2008-05-02 13:42:45 -0700285static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286nfs4_drop_state_owner(struct nfs4_state_owner *sp)
287{
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400288 if (!RB_EMPTY_NODE(&sp->so_client_node)) {
289 struct nfs_client *clp = sp->so_client;
290
291 spin_lock(&clp->cl_lock);
292 rb_erase(&sp->so_client_node, &clp->cl_state_owners);
293 RB_CLEAR_NODE(&sp->so_client_node);
294 spin_unlock(&clp->cl_lock);
295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298/*
299 * Note: must be called with clp->cl_sem held in order to prevent races
300 * with reboot recovery!
301 */
302struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, struct rpc_cred *cred)
303{
David Howells7539bba2006-08-22 20:06:09 -0400304 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct nfs4_state_owner *sp, *new;
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 spin_lock(&clp->cl_lock);
Trond Myklebust6f2e64d2007-07-06 10:53:21 -0400308 sp = nfs4_find_state_owner(server, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 spin_unlock(&clp->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (sp != NULL)
311 return sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400312 new = nfs4_alloc_state_owner();
313 if (new == NULL)
314 return NULL;
315 new->so_client = clp;
Trond Myklebust6f2e64d2007-07-06 10:53:21 -0400316 new->so_server = server;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400317 new->so_cred = cred;
318 spin_lock(&clp->cl_lock);
319 sp = nfs4_insert_state_owner(clp, new);
320 spin_unlock(&clp->cl_lock);
321 if (sp == new)
322 get_rpccred(cred);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500323 else {
324 rpc_destroy_wait_queue(&new->so_sequence.wait);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400325 kfree(new);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500326 }
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400327 return sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330/*
331 * Must be called with clp->cl_sem held in order to avoid races
332 * with state recovery...
333 */
334void nfs4_put_state_owner(struct nfs4_state_owner *sp)
335{
David Howellsadfa6f92006-08-22 20:06:08 -0400336 struct nfs_client *clp = sp->so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 struct rpc_cred *cred = sp->so_cred;
338
339 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
340 return;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400341 nfs4_remove_state_owner(clp, sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 spin_unlock(&clp->cl_lock);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500343 rpc_destroy_wait_queue(&sp->so_sequence.wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 put_rpccred(cred);
345 kfree(sp);
346}
347
348static struct nfs4_state *
349nfs4_alloc_open_state(void)
350{
351 struct nfs4_state *state;
352
Trond Myklebuste7616922006-01-03 09:55:13 +0100353 state = kzalloc(sizeof(*state), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (!state)
355 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 atomic_set(&state->count, 1);
357 INIT_LIST_HEAD(&state->lock_states);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000358 spin_lock_init(&state->state_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400359 seqlock_init(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return state;
361}
362
Trond Myklebust4cecb762005-11-04 15:32:58 -0500363void
364nfs4_state_set_mode_locked(struct nfs4_state *state, mode_t mode)
365{
366 if (state->state == mode)
367 return;
368 /* NB! List reordering - see the reclaim code for why. */
369 if ((mode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
370 if (mode & FMODE_WRITE)
371 list_move(&state->open_states, &state->owner->so_states);
372 else
373 list_move_tail(&state->open_states, &state->owner->so_states);
374 }
Trond Myklebust4cecb762005-11-04 15:32:58 -0500375 state->state = mode;
376}
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378static struct nfs4_state *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379__nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
380{
381 struct nfs_inode *nfsi = NFS_I(inode);
382 struct nfs4_state *state;
383
384 list_for_each_entry(state, &nfsi->open_states, inode_states) {
Trond Myklebust1c816ef2007-07-03 14:41:19 -0400385 if (state->owner != owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 continue;
Trond Myklebust1c816ef2007-07-03 14:41:19 -0400387 if (atomic_inc_not_zero(&state->count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390 return NULL;
391}
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393static void
394nfs4_free_open_state(struct nfs4_state *state)
395{
396 kfree(state);
397}
398
399struct nfs4_state *
400nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
401{
402 struct nfs4_state *state, *new;
403 struct nfs_inode *nfsi = NFS_I(inode);
404
405 spin_lock(&inode->i_lock);
406 state = __nfs4_find_state_byowner(inode, owner);
407 spin_unlock(&inode->i_lock);
408 if (state)
409 goto out;
410 new = nfs4_alloc_open_state();
Trond Myklebustec073422005-10-20 14:22:47 -0700411 spin_lock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 spin_lock(&inode->i_lock);
413 state = __nfs4_find_state_byowner(inode, owner);
414 if (state == NULL && new != NULL) {
415 state = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 state->owner = owner;
417 atomic_inc(&owner->so_count);
418 list_add(&state->inode_states, &nfsi->open_states);
419 state->inode = igrab(inode);
420 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700421 /* Note: The reclaim code dictates that we add stateless
422 * and read-only stateids to the end of the list */
423 list_add_tail(&state->open_states, &owner->so_states);
424 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 } else {
426 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700427 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (new)
429 nfs4_free_open_state(new);
430 }
431out:
432 return state;
433}
434
435/*
436 * Beware! Caller must be holding exactly one
Trond Myklebuste6dfa552005-10-18 14:20:13 -0700437 * reference to clp->cl_sem!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 */
439void nfs4_put_open_state(struct nfs4_state *state)
440{
441 struct inode *inode = state->inode;
442 struct nfs4_state_owner *owner = state->owner;
443
Trond Myklebustec073422005-10-20 14:22:47 -0700444 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return;
Trond Myklebustec073422005-10-20 14:22:47 -0700446 spin_lock(&inode->i_lock);
Trond Myklebustba6830312007-07-27 10:23:05 -0400447 list_del(&state->inode_states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 list_del(&state->open_states);
Trond Myklebustec073422005-10-20 14:22:47 -0700449 spin_unlock(&inode->i_lock);
450 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 nfs4_free_open_state(state);
453 nfs4_put_state_owner(owner);
454}
455
456/*
Trond Myklebust83c9d412005-10-18 14:20:13 -0700457 * Close the current file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400459static void __nfs4_close(struct path *path, struct nfs4_state *state, mode_t mode, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct nfs4_state_owner *owner = state->owner;
Trond Myklebust003707c2007-07-05 18:07:55 -0400462 int call_close = 0;
463 int newstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 atomic_inc(&owner->so_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /* Protect against nfs4_find_state() */
Trond Myklebustec073422005-10-20 14:22:47 -0700467 spin_lock(&owner->so_lock);
Trond Myklebuste7616922006-01-03 09:55:13 +0100468 switch (mode & (FMODE_READ | FMODE_WRITE)) {
469 case FMODE_READ:
470 state->n_rdonly--;
471 break;
472 case FMODE_WRITE:
473 state->n_wronly--;
474 break;
475 case FMODE_READ|FMODE_WRITE:
476 state->n_rdwr--;
477 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400478 newstate = FMODE_READ|FMODE_WRITE;
Trond Myklebuste7616922006-01-03 09:55:13 +0100479 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400480 if (state->n_rdonly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100481 newstate &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -0400482 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
483 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
484 }
485 if (state->n_wronly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100486 newstate &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -0400487 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
488 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
489 }
490 if (newstate == 0)
491 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebuste7616922006-01-03 09:55:13 +0100492 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400493 nfs4_state_set_mode_locked(state, newstate);
Trond Myklebustec073422005-10-20 14:22:47 -0700494 spin_unlock(&owner->so_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500495
Trond Myklebust003707c2007-07-05 18:07:55 -0400496 if (!call_close) {
Trond Myklebustb39e6252007-06-11 23:05:07 -0400497 nfs4_put_open_state(state);
498 nfs4_put_state_owner(owner);
499 } else
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400500 nfs4_do_close(path, state, wait);
501}
502
503void nfs4_close_state(struct path *path, struct nfs4_state *state, mode_t mode)
504{
505 __nfs4_close(path, state, mode, 0);
506}
507
508void nfs4_close_sync(struct path *path, struct nfs4_state *state, mode_t mode)
509{
510 __nfs4_close(path, state, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513/*
514 * Search the state->lock_states for an existing lock_owner
515 * that is compatible with current->files
516 */
517static struct nfs4_lock_state *
518__nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
519{
520 struct nfs4_lock_state *pos;
521 list_for_each_entry(pos, &state->lock_states, ls_locks) {
522 if (pos->ls_owner != fl_owner)
523 continue;
524 atomic_inc(&pos->ls_count);
525 return pos;
526 }
527 return NULL;
528}
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530/*
531 * Return a compatible lock_state. If no initialized lock_state structure
532 * exists, return an uninitialized one.
533 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 */
535static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
536{
537 struct nfs4_lock_state *lsp;
David Howellsadfa6f92006-08-22 20:06:08 -0400538 struct nfs_client *clp = state->owner->so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700540 lsp = kzalloc(sizeof(*lsp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (lsp == NULL)
542 return NULL;
Trond Myklebustd0dc3702008-01-10 16:07:54 -0500543 rpc_init_wait_queue(&lsp->ls_sequence.wait, "lock_seqid_waitqueue");
544 spin_lock_init(&lsp->ls_sequence.lock);
545 INIT_LIST_HEAD(&lsp->ls_sequence.list);
546 lsp->ls_seqid.sequence = &lsp->ls_sequence;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 atomic_set(&lsp->ls_count, 1);
548 lsp->ls_owner = fl_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 spin_lock(&clp->cl_lock);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400550 nfs_alloc_unique_id(&clp->cl_lockowner_id, &lsp->ls_id, 1, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 spin_unlock(&clp->cl_lock);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000552 INIT_LIST_HEAD(&lsp->ls_locks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return lsp;
554}
555
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400556static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
557{
558 struct nfs_client *clp = lsp->ls_state->owner->so_client;
559
560 spin_lock(&clp->cl_lock);
561 nfs_free_unique_id(&clp->cl_lockowner_id, &lsp->ls_id);
562 spin_unlock(&clp->cl_lock);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500563 rpc_destroy_wait_queue(&lsp->ls_sequence.wait);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400564 kfree(lsp);
565}
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567/*
568 * Return a compatible lock_state. If no initialized lock_state structure
569 * exists, return an uninitialized one.
570 *
Trond Myklebuste6dfa552005-10-18 14:20:13 -0700571 * The caller must be holding clp->cl_sem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 */
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000573static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000575 struct nfs4_lock_state *lsp, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000577 for(;;) {
578 spin_lock(&state->state_lock);
579 lsp = __nfs4_find_lock_state(state, owner);
580 if (lsp != NULL)
581 break;
582 if (new != NULL) {
583 new->ls_state = state;
584 list_add(&new->ls_locks, &state->lock_states);
585 set_bit(LK_STATE_IN_USE, &state->flags);
586 lsp = new;
587 new = NULL;
588 break;
589 }
590 spin_unlock(&state->state_lock);
591 new = nfs4_alloc_lock_state(state, owner);
592 if (new == NULL)
593 return NULL;
594 }
595 spin_unlock(&state->state_lock);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400596 if (new != NULL)
597 nfs4_free_lock_state(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return lsp;
599}
600
601/*
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000602 * Release reference to lock_state, and free it if we see that
603 * it is no longer in use
604 */
Trond Myklebustfaf5f492005-10-18 14:20:15 -0700605void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000606{
607 struct nfs4_state *state;
608
609 if (lsp == NULL)
610 return;
611 state = lsp->ls_state;
612 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
613 return;
614 list_del(&lsp->ls_locks);
615 if (list_empty(&state->lock_states))
616 clear_bit(LK_STATE_IN_USE, &state->flags);
617 spin_unlock(&state->state_lock);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400618 nfs4_free_lock_state(lsp);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000619}
620
621static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
622{
623 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
624
625 dst->fl_u.nfs4_fl.owner = lsp;
626 atomic_inc(&lsp->ls_count);
627}
628
629static void nfs4_fl_release_lock(struct file_lock *fl)
630{
631 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
632}
633
634static struct file_lock_operations nfs4_fl_lock_ops = {
635 .fl_copy_lock = nfs4_fl_copy_lock,
636 .fl_release_private = nfs4_fl_release_lock,
637};
638
639int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
640{
641 struct nfs4_lock_state *lsp;
642
643 if (fl->fl_ops != NULL)
644 return 0;
645 lsp = nfs4_get_lock_state(state, fl->fl_owner);
646 if (lsp == NULL)
647 return -ENOMEM;
648 fl->fl_u.nfs4_fl.owner = lsp;
649 fl->fl_ops = &nfs4_fl_lock_ops;
650 return 0;
651}
652
653/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 * Byte-range lock aware utility to initialize the stateid of read/write
655 * requests.
656 */
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000657void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000659 struct nfs4_lock_state *lsp;
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400660 int seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400662 do {
663 seq = read_seqbegin(&state->seqlock);
664 memcpy(dst, &state->stateid, sizeof(*dst));
665 } while (read_seqretry(&state->seqlock, seq));
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000666 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
667 return;
668
669 spin_lock(&state->state_lock);
670 lsp = __nfs4_find_lock_state(state, fl_owner);
671 if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
672 memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
673 spin_unlock(&state->state_lock);
674 nfs4_put_lock_state(lsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700677struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700679 struct nfs_seqid *new;
680
681 new = kmalloc(sizeof(*new), GFP_KERNEL);
682 if (new != NULL) {
683 new->sequence = counter;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500684 INIT_LIST_HEAD(&new->list);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700685 }
686 return new;
687}
688
689void nfs_free_seqid(struct nfs_seqid *seqid)
690{
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500691 if (!list_empty(&seqid->list)) {
692 struct rpc_sequence *sequence = seqid->sequence->sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700693
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500694 spin_lock(&sequence->lock);
695 list_del(&seqid->list);
696 spin_unlock(&sequence->lock);
697 rpc_wake_up(&sequence->wait);
698 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700699 kfree(seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700703 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
704 * failed with a seqid incrementing error -
705 * see comments nfs_fs.h:seqid_mutating_error()
706 */
Trond Myklebust88d90932007-07-02 14:03:03 -0400707static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500709 BUG_ON(list_first_entry(&seqid->sequence->sequence->list, struct nfs_seqid, list) != seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700710 switch (status) {
711 case 0:
712 break;
713 case -NFS4ERR_BAD_SEQID:
Trond Myklebust6f43ddc2007-07-08 16:49:11 -0400714 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
715 return;
716 printk(KERN_WARNING "NFS: v4 server returned a bad"
Dan Muntz497799e2008-02-13 13:09:35 -0800717 " sequence-id error on an"
718 " unconfirmed sequence %p!\n",
Trond Myklebust6f43ddc2007-07-08 16:49:11 -0400719 seqid->sequence);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700720 case -NFS4ERR_STALE_CLIENTID:
721 case -NFS4ERR_STALE_STATEID:
722 case -NFS4ERR_BAD_STATEID:
723 case -NFS4ERR_BADXDR:
724 case -NFS4ERR_RESOURCE:
725 case -NFS4ERR_NOFILEHANDLE:
726 /* Non-seqid mutating errors */
727 return;
728 };
729 /*
730 * Note: no locking needed as we are guaranteed to be first
731 * on the sequence list
732 */
733 seqid->sequence->counter++;
734}
735
736void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
737{
738 if (status == -NFS4ERR_BAD_SEQID) {
739 struct nfs4_state_owner *sp = container_of(seqid->sequence,
740 struct nfs4_state_owner, so_seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 nfs4_drop_state_owner(sp);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700742 }
Trond Myklebust88d90932007-07-02 14:03:03 -0400743 nfs_increment_seqid(status, seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700744}
745
746/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700747 * Increment the seqid if the LOCK/LOCKU succeeded, or
748 * failed with a seqid incrementing error -
749 * see comments nfs_fs.h:seqid_mutating_error()
750 */
751void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
752{
Trond Myklebust88d90932007-07-02 14:03:03 -0400753 nfs_increment_seqid(status, seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700754}
755
756int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
757{
758 struct rpc_sequence *sequence = seqid->sequence->sequence;
759 int status = 0;
760
761 spin_lock(&sequence->lock);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500762 if (list_empty(&seqid->list))
763 list_add_tail(&seqid->list, &sequence->list);
764 if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
765 goto unlock;
Trond Myklebust5d008372008-02-22 16:34:17 -0500766 rpc_sleep_on(&sequence->wait, task, NULL);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -0500767 status = -EAGAIN;
768unlock:
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700769 spin_unlock(&sequence->lock);
770 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773static int reclaimer(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
David Howellsadfa6f92006-08-22 20:06:08 -0400775static inline void nfs4_clear_recover_bit(struct nfs_client *clp)
Trond Myklebust433fbe42006-01-03 09:55:22 +0100776{
777 smp_mb__before_clear_bit();
778 clear_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state);
779 smp_mb__after_clear_bit();
780 wake_up_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER);
781 rpc_wake_up(&clp->cl_rpcwaitq);
782}
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784/*
785 * State recovery routine
786 */
David Howellsadfa6f92006-08-22 20:06:08 -0400787static void nfs4_recover_state(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Trond Myklebust5043e902006-01-03 09:55:23 +0100789 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Trond Myklebust5043e902006-01-03 09:55:23 +0100791 __module_get(THIS_MODULE);
792 atomic_inc(&clp->cl_count);
Chuck Lever5d8515c2007-12-10 14:57:16 -0500793 task = kthread_run(reclaimer, clp, "%s-reclaim",
794 rpc_peeraddr2str(clp->cl_rpcclient,
795 RPC_DISPLAY_ADDR));
Trond Myklebust5043e902006-01-03 09:55:23 +0100796 if (!IS_ERR(task))
797 return;
Trond Myklebust433fbe42006-01-03 09:55:22 +0100798 nfs4_clear_recover_bit(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400799 nfs_put_client(clp);
Trond Myklebust5043e902006-01-03 09:55:23 +0100800 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
803/*
804 * Schedule a state recovery attempt
805 */
David Howellsadfa6f92006-08-22 20:06:08 -0400806void nfs4_schedule_state_recovery(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 if (!clp)
809 return;
Trond Myklebust433fbe42006-01-03 09:55:22 +0100810 if (test_and_set_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
Trond Myklebust5043e902006-01-03 09:55:23 +0100811 nfs4_recover_state(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
814static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops *ops, struct nfs4_state *state)
815{
816 struct inode *inode = state->inode;
817 struct file_lock *fl;
818 int status = 0;
819
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800820 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
Trond Myklebust43b2a332005-11-04 15:35:30 -0500821 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 continue;
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400823 if (nfs_file_open_context(fl->fl_file)->state != state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 continue;
825 status = ops->recover_lock(state, fl);
826 if (status >= 0)
827 continue;
828 switch (status) {
829 default:
830 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700831 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 case -NFS4ERR_EXPIRED:
833 case -NFS4ERR_NO_GRACE:
834 case -NFS4ERR_RECLAIM_BAD:
835 case -NFS4ERR_RECLAIM_CONFLICT:
Trond Myklebust43b2a332005-11-04 15:35:30 -0500836 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 break;
838 case -NFS4ERR_STALE_CLIENTID:
839 goto out_err;
840 }
841 }
842 return 0;
843out_err:
844 return status;
845}
846
847static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops *ops, struct nfs4_state_owner *sp)
848{
849 struct nfs4_state *state;
850 struct nfs4_lock_state *lock;
851 int status = 0;
852
853 /* Note: we rely on the sp->so_states list being ordered
854 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
855 * states first.
856 * This is needed to ensure that the server won't give us any
857 * read delegations that we have to return if, say, we are
858 * recovering after a network partition or a reboot from a
859 * server that doesn't support a grace period.
860 */
861 list_for_each_entry(state, &sp->so_states, open_states) {
862 if (state->state == 0)
863 continue;
864 status = ops->recover_open(sp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (status >= 0) {
866 status = nfs4_reclaim_locks(ops, state);
867 if (status < 0)
868 goto out_err;
869 list_for_each_entry(lock, &state->lock_states, ls_locks) {
870 if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
871 printk("%s: Lock reclaim failed!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700872 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874 continue;
875 }
876 switch (status) {
877 default:
878 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700879 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 case -ENOENT:
881 case -NFS4ERR_RECLAIM_BAD:
882 case -NFS4ERR_RECLAIM_CONFLICT:
883 /*
884 * Open state on this file cannot be recovered
885 * All we can do is revert to using the zero stateid.
886 */
887 memset(state->stateid.data, 0,
888 sizeof(state->stateid.data));
889 /* Mark the file as being 'closed' */
890 state->state = 0;
891 break;
892 case -NFS4ERR_EXPIRED:
893 case -NFS4ERR_NO_GRACE:
894 case -NFS4ERR_STALE_CLIENTID:
895 goto out_err;
896 }
897 }
898 return 0;
899out_err:
900 return status;
901}
902
David Howellsadfa6f92006-08-22 20:06:08 -0400903static void nfs4_state_mark_reclaim(struct nfs_client *clp)
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700904{
905 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400906 struct rb_node *pos;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700907 struct nfs4_state *state;
908 struct nfs4_lock_state *lock;
909
910 /* Reset all sequence ids to zero */
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400911 for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
912 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700913 sp->so_seqid.counter = 0;
914 sp->so_seqid.flags = 0;
Trond Myklebustec073422005-10-20 14:22:47 -0700915 spin_lock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700916 list_for_each_entry(state, &sp->so_states, open_states) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400917 clear_bit(NFS_DELEGATED_STATE, &state->flags);
918 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
919 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
920 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700921 list_for_each_entry(lock, &state->lock_states, ls_locks) {
922 lock->ls_seqid.counter = 0;
923 lock->ls_seqid.flags = 0;
924 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
925 }
926 }
Trond Myklebustec073422005-10-20 14:22:47 -0700927 spin_unlock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700928 }
929}
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931static int reclaimer(void *ptr)
932{
David Howellsadfa6f92006-08-22 20:06:08 -0400933 struct nfs_client *clp = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400935 struct rb_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 struct nfs4_state_recovery_ops *ops;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100937 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 int status = 0;
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 allow_signal(SIGKILL);
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /* Ensure exclusive access to NFSv4 state */
943 lock_kernel();
944 down_write(&clp->cl_sem);
945 /* Are there any NFS mounts out there? */
946 if (list_empty(&clp->cl_superblocks))
947 goto out;
948restart_loop:
Trond Myklebust286d7d62006-01-03 09:55:26 +0100949 ops = &nfs4_network_partition_recovery_ops;
950 /* Are there any open files on this volume? */
951 cred = nfs4_get_renew_cred(clp);
952 if (cred != NULL) {
953 /* Yes there are: try to renew the old lease */
954 status = nfs4_proc_renew(clp, cred);
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400955 put_rpccred(cred);
Trond Myklebust286d7d62006-01-03 09:55:26 +0100956 switch (status) {
957 case 0:
958 case -NFS4ERR_CB_PATH_DOWN:
Trond Myklebust286d7d62006-01-03 09:55:26 +0100959 goto out;
960 case -NFS4ERR_STALE_CLIENTID:
961 case -NFS4ERR_LEASE_MOVED:
962 ops = &nfs4_reboot_recovery_ops;
963 }
964 } else {
965 /* "reboot" to ensure we clear all state on the server */
966 clp->cl_boot_time = CURRENT_TIME;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100967 }
968 /* We're going to have to re-establish a clientid */
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700969 nfs4_state_mark_reclaim(clp);
Trond Myklebust286d7d62006-01-03 09:55:26 +0100970 status = -ENOENT;
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400971 cred = nfs4_get_setclientid_cred(clp);
Trond Myklebust286d7d62006-01-03 09:55:26 +0100972 if (cred != NULL) {
973 status = nfs4_init_client(clp, cred);
974 put_rpccred(cred);
Trond Myklebusta2b2bb82008-04-08 16:02:17 -0400975 /* Handle case where the user hasn't set up machine creds */
976 if (status == -EACCES && cred == clp->cl_machine_cred) {
977 nfs4_clear_machine_cred(clp);
978 goto restart_loop;
979 }
Trond Myklebust286d7d62006-01-03 09:55:26 +0100980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (status)
982 goto out_error;
983 /* Mark all delegations for reclaim */
984 nfs_delegation_mark_reclaim(clp);
985 /* Note: list is protected by exclusive lock on cl->cl_sem */
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400986 for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
987 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 status = nfs4_reclaim_open_state(ops, sp);
989 if (status < 0) {
990 if (status == -NFS4ERR_NO_GRACE) {
991 ops = &nfs4_network_partition_recovery_ops;
992 status = nfs4_reclaim_open_state(ops, sp);
993 }
994 if (status == -NFS4ERR_STALE_CLIENTID)
995 goto restart_loop;
996 if (status == -NFS4ERR_EXPIRED)
997 goto restart_loop;
998 }
999 }
1000 nfs_delegation_reap_unclaimed(clp);
1001out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 up_write(&clp->cl_sem);
1003 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (status == -NFS4ERR_CB_PATH_DOWN)
1005 nfs_handle_cb_pathdown(clp);
Trond Myklebust433fbe42006-01-03 09:55:22 +01001006 nfs4_clear_recover_bit(clp);
David Howells24c8dbb2006-08-22 20:06:10 -04001007 nfs_put_client(clp);
Trond Myklebust5043e902006-01-03 09:55:23 +01001008 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return 0;
1010out_error:
Chuck Lever5d8515c2007-12-10 14:57:16 -05001011 printk(KERN_WARNING "Error: state recovery failed on NFSv4 server %s"
1012 " with error %d\n", clp->cl_hostname, -status);
Trond Myklebust51581f32006-03-20 13:44:47 -05001013 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 goto out;
1015}
1016
1017/*
1018 * Local variables:
1019 * c-basic-offset: 8
1020 * End:
1021 */