blob: 523cc2cbb5e1b4b5d145de85d31bf03fc4cbabca [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
David Howellsadfa6f92006-08-22 20:06:08 -040074struct rpc_cred *nfs4_get_renew_cred(struct nfs_client *clp)
Trond Myklebustb4454fe2006-01-03 09:55:25 +010075{
76 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -040077 struct rb_node *pos;
Trond Myklebustb4454fe2006-01-03 09:55:25 +010078 struct rpc_cred *cred = NULL;
79
Trond Myklebust9f958ab2007-07-02 13:58:33 -040080 for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
81 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
Trond Myklebustb4454fe2006-01-03 09:55:25 +010082 if (list_empty(&sp->so_states))
83 continue;
84 cred = get_rpccred(sp->so_cred);
85 break;
86 }
87 return cred;
88}
89
Trond Myklebust10afec92007-05-14 17:16:04 -040090static struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
Trond Myklebust286d7d62006-01-03 09:55:26 +010091{
92 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -040093 struct rb_node *pos;
Trond Myklebust286d7d62006-01-03 09:55:26 +010094
Trond Myklebust9f958ab2007-07-02 13:58:33 -040095 pos = rb_first(&clp->cl_state_owners);
96 if (pos != NULL) {
97 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
Trond Myklebust286d7d62006-01-03 09:55:26 +010098 return get_rpccred(sp->so_cred);
99 }
100 return NULL;
101}
102
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400103static void nfs_alloc_unique_id(struct rb_root *root, struct nfs_unique_id *new,
104 __u64 minval, int maxbits)
105{
106 struct rb_node **p, *parent;
107 struct nfs_unique_id *pos;
108 __u64 mask = ~0ULL;
109
110 if (maxbits < 64)
111 mask = (1ULL << maxbits) - 1ULL;
112
113 /* Ensure distribution is more or less flat */
114 get_random_bytes(&new->id, sizeof(new->id));
115 new->id &= mask;
116 if (new->id < minval)
117 new->id += minval;
118retry:
119 p = &root->rb_node;
120 parent = NULL;
121
122 while (*p != NULL) {
123 parent = *p;
124 pos = rb_entry(parent, struct nfs_unique_id, rb_node);
125
126 if (new->id < pos->id)
127 p = &(*p)->rb_left;
128 else if (new->id > pos->id)
129 p = &(*p)->rb_right;
130 else
131 goto id_exists;
132 }
133 rb_link_node(&new->rb_node, parent, p);
134 rb_insert_color(&new->rb_node, root);
135 return;
136id_exists:
137 for (;;) {
138 new->id++;
139 if (new->id < minval || (new->id & mask) != new->id) {
140 new->id = minval;
141 break;
142 }
143 parent = rb_next(parent);
144 if (parent == NULL)
145 break;
146 pos = rb_entry(parent, struct nfs_unique_id, rb_node);
147 if (new->id < pos->id)
148 break;
149 }
150 goto retry;
151}
152
153static void nfs_free_unique_id(struct rb_root *root, struct nfs_unique_id *id)
154{
155 rb_erase(&id->rb_node, root);
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static struct nfs4_state_owner *
David Howellsadfa6f92006-08-22 20:06:08 -0400159nfs4_find_state_owner(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400161 struct rb_node **p = &clp->cl_state_owners.rb_node,
162 *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 struct nfs4_state_owner *sp, *res = NULL;
164
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400165 while (*p != NULL) {
166 parent = *p;
167 sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
168
169 if (cred < sp->so_cred)
170 p = &parent->rb_left;
171 else if (cred > sp->so_cred)
172 p = &parent->rb_right;
173 else {
174 atomic_inc(&sp->so_count);
175 res = sp;
176 break;
177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179 return res;
180}
181
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400182static struct nfs4_state_owner *
183nfs4_insert_state_owner(struct nfs_client *clp, struct nfs4_state_owner *new)
184{
185 struct rb_node **p = &clp->cl_state_owners.rb_node,
186 *parent = NULL;
187 struct nfs4_state_owner *sp;
188
189 while (*p != NULL) {
190 parent = *p;
191 sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
192
193 if (new->so_cred < sp->so_cred)
194 p = &parent->rb_left;
195 else if (new->so_cred > sp->so_cred)
196 p = &parent->rb_right;
197 else {
198 atomic_inc(&sp->so_count);
199 return sp;
200 }
201 }
202 nfs_alloc_unique_id(&clp->cl_openowner_id, &new->so_owner_id, 1, 64);
203 rb_link_node(&new->so_client_node, parent, p);
204 rb_insert_color(&new->so_client_node, &clp->cl_state_owners);
205 return new;
206}
207
208static void
209nfs4_remove_state_owner(struct nfs_client *clp, struct nfs4_state_owner *sp)
210{
211 if (!RB_EMPTY_NODE(&sp->so_client_node))
212 rb_erase(&sp->so_client_node, &clp->cl_state_owners);
213 nfs_free_unique_id(&clp->cl_openowner_id, &sp->so_owner_id);
214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
217 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
218 * create a new state_owner.
219 *
220 */
221static struct nfs4_state_owner *
222nfs4_alloc_state_owner(void)
223{
224 struct nfs4_state_owner *sp;
225
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700226 sp = kzalloc(sizeof(*sp),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (!sp)
228 return NULL;
Trond Myklebustec073422005-10-20 14:22:47 -0700229 spin_lock_init(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 INIT_LIST_HEAD(&sp->so_states);
231 INIT_LIST_HEAD(&sp->so_delegations);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700232 rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
233 sp->so_seqid.sequence = &sp->so_sequence;
234 spin_lock_init(&sp->so_sequence.lock);
235 INIT_LIST_HEAD(&sp->so_sequence.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 atomic_set(&sp->so_count, 1);
237 return sp;
238}
239
240void
241nfs4_drop_state_owner(struct nfs4_state_owner *sp)
242{
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400243 if (!RB_EMPTY_NODE(&sp->so_client_node)) {
244 struct nfs_client *clp = sp->so_client;
245
246 spin_lock(&clp->cl_lock);
247 rb_erase(&sp->so_client_node, &clp->cl_state_owners);
248 RB_CLEAR_NODE(&sp->so_client_node);
249 spin_unlock(&clp->cl_lock);
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253/*
254 * Note: must be called with clp->cl_sem held in order to prevent races
255 * with reboot recovery!
256 */
257struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, struct rpc_cred *cred)
258{
David Howells7539bba2006-08-22 20:06:09 -0400259 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 struct nfs4_state_owner *sp, *new;
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 spin_lock(&clp->cl_lock);
263 sp = nfs4_find_state_owner(clp, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 spin_unlock(&clp->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (sp != NULL)
266 return sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400267 new = nfs4_alloc_state_owner();
268 if (new == NULL)
269 return NULL;
270 new->so_client = clp;
271 new->so_cred = cred;
272 spin_lock(&clp->cl_lock);
273 sp = nfs4_insert_state_owner(clp, new);
274 spin_unlock(&clp->cl_lock);
275 if (sp == new)
276 get_rpccred(cred);
277 else
278 kfree(new);
279 return sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282/*
283 * Must be called with clp->cl_sem held in order to avoid races
284 * with state recovery...
285 */
286void nfs4_put_state_owner(struct nfs4_state_owner *sp)
287{
David Howellsadfa6f92006-08-22 20:06:08 -0400288 struct nfs_client *clp = sp->so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 struct rpc_cred *cred = sp->so_cred;
290
291 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
292 return;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400293 nfs4_remove_state_owner(clp, sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 spin_unlock(&clp->cl_lock);
295 put_rpccred(cred);
296 kfree(sp);
297}
298
299static struct nfs4_state *
300nfs4_alloc_open_state(void)
301{
302 struct nfs4_state *state;
303
Trond Myklebuste7616922006-01-03 09:55:13 +0100304 state = kzalloc(sizeof(*state), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (!state)
306 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 atomic_set(&state->count, 1);
308 INIT_LIST_HEAD(&state->lock_states);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000309 spin_lock_init(&state->state_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400310 seqlock_init(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return state;
312}
313
Trond Myklebust4cecb762005-11-04 15:32:58 -0500314void
315nfs4_state_set_mode_locked(struct nfs4_state *state, mode_t mode)
316{
317 if (state->state == mode)
318 return;
319 /* NB! List reordering - see the reclaim code for why. */
320 if ((mode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
321 if (mode & FMODE_WRITE)
322 list_move(&state->open_states, &state->owner->so_states);
323 else
324 list_move_tail(&state->open_states, &state->owner->so_states);
325 }
326 if (mode == 0)
327 list_del_init(&state->inode_states);
328 state->state = mode;
329}
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331static struct nfs4_state *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332__nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
333{
334 struct nfs_inode *nfsi = NFS_I(inode);
335 struct nfs4_state *state;
336
337 list_for_each_entry(state, &nfsi->open_states, inode_states) {
Trond Myklebust1c816ef2007-07-03 14:41:19 -0400338 if (state->owner != owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 continue;
Trond Myklebust1c816ef2007-07-03 14:41:19 -0400340 if (atomic_inc_not_zero(&state->count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 return NULL;
344}
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346static void
347nfs4_free_open_state(struct nfs4_state *state)
348{
349 kfree(state);
350}
351
352struct nfs4_state *
353nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
354{
355 struct nfs4_state *state, *new;
356 struct nfs_inode *nfsi = NFS_I(inode);
357
358 spin_lock(&inode->i_lock);
359 state = __nfs4_find_state_byowner(inode, owner);
360 spin_unlock(&inode->i_lock);
361 if (state)
362 goto out;
363 new = nfs4_alloc_open_state();
Trond Myklebustec073422005-10-20 14:22:47 -0700364 spin_lock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 spin_lock(&inode->i_lock);
366 state = __nfs4_find_state_byowner(inode, owner);
367 if (state == NULL && new != NULL) {
368 state = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 state->owner = owner;
370 atomic_inc(&owner->so_count);
371 list_add(&state->inode_states, &nfsi->open_states);
372 state->inode = igrab(inode);
373 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700374 /* Note: The reclaim code dictates that we add stateless
375 * and read-only stateids to the end of the list */
376 list_add_tail(&state->open_states, &owner->so_states);
377 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 } else {
379 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700380 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (new)
382 nfs4_free_open_state(new);
383 }
384out:
385 return state;
386}
387
388/*
389 * Beware! Caller must be holding exactly one
Trond Myklebuste6dfa552005-10-18 14:20:13 -0700390 * reference to clp->cl_sem!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 */
392void nfs4_put_open_state(struct nfs4_state *state)
393{
394 struct inode *inode = state->inode;
395 struct nfs4_state_owner *owner = state->owner;
396
Trond Myklebustec073422005-10-20 14:22:47 -0700397 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return;
Trond Myklebustec073422005-10-20 14:22:47 -0700399 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (!list_empty(&state->inode_states))
401 list_del(&state->inode_states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 list_del(&state->open_states);
Trond Myklebustec073422005-10-20 14:22:47 -0700403 spin_unlock(&inode->i_lock);
404 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 nfs4_free_open_state(state);
407 nfs4_put_state_owner(owner);
408}
409
410/*
Trond Myklebust83c9d412005-10-18 14:20:13 -0700411 * Close the current file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 */
Trond Myklebust4a35bd42007-06-05 10:31:33 -0400413void nfs4_close_state(struct path *path, struct nfs4_state *state, mode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 struct nfs4_state_owner *owner = state->owner;
Trond Myklebust003707c2007-07-05 18:07:55 -0400416 int call_close = 0;
417 int newstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 atomic_inc(&owner->so_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /* Protect against nfs4_find_state() */
Trond Myklebustec073422005-10-20 14:22:47 -0700421 spin_lock(&owner->so_lock);
Trond Myklebuste7616922006-01-03 09:55:13 +0100422 switch (mode & (FMODE_READ | FMODE_WRITE)) {
423 case FMODE_READ:
424 state->n_rdonly--;
425 break;
426 case FMODE_WRITE:
427 state->n_wronly--;
428 break;
429 case FMODE_READ|FMODE_WRITE:
430 state->n_rdwr--;
431 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400432 newstate = FMODE_READ|FMODE_WRITE;
Trond Myklebuste7616922006-01-03 09:55:13 +0100433 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400434 if (state->n_rdonly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100435 newstate &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -0400436 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
437 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
438 }
439 if (state->n_wronly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100440 newstate &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -0400441 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
442 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
443 }
444 if (newstate == 0)
445 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebuste7616922006-01-03 09:55:13 +0100446 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400447 nfs4_state_set_mode_locked(state, newstate);
Trond Myklebustec073422005-10-20 14:22:47 -0700448 spin_unlock(&owner->so_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500449
Trond Myklebust003707c2007-07-05 18:07:55 -0400450 if (!call_close) {
Trond Myklebustb39e6252007-06-11 23:05:07 -0400451 nfs4_put_open_state(state);
452 nfs4_put_state_owner(owner);
453 } else
454 nfs4_do_close(path, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457/*
458 * Search the state->lock_states for an existing lock_owner
459 * that is compatible with current->files
460 */
461static struct nfs4_lock_state *
462__nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
463{
464 struct nfs4_lock_state *pos;
465 list_for_each_entry(pos, &state->lock_states, ls_locks) {
466 if (pos->ls_owner != fl_owner)
467 continue;
468 atomic_inc(&pos->ls_count);
469 return pos;
470 }
471 return NULL;
472}
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474/*
475 * Return a compatible lock_state. If no initialized lock_state structure
476 * exists, return an uninitialized one.
477 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 */
479static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
480{
481 struct nfs4_lock_state *lsp;
David Howellsadfa6f92006-08-22 20:06:08 -0400482 struct nfs_client *clp = state->owner->so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700484 lsp = kzalloc(sizeof(*lsp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (lsp == NULL)
486 return NULL;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700487 lsp->ls_seqid.sequence = &state->owner->so_sequence;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 atomic_set(&lsp->ls_count, 1);
489 lsp->ls_owner = fl_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 spin_lock(&clp->cl_lock);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400491 nfs_alloc_unique_id(&clp->cl_lockowner_id, &lsp->ls_id, 1, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 spin_unlock(&clp->cl_lock);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000493 INIT_LIST_HEAD(&lsp->ls_locks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return lsp;
495}
496
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400497static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
498{
499 struct nfs_client *clp = lsp->ls_state->owner->so_client;
500
501 spin_lock(&clp->cl_lock);
502 nfs_free_unique_id(&clp->cl_lockowner_id, &lsp->ls_id);
503 spin_unlock(&clp->cl_lock);
504 kfree(lsp);
505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507/*
508 * Return a compatible lock_state. If no initialized lock_state structure
509 * exists, return an uninitialized one.
510 *
Trond Myklebuste6dfa552005-10-18 14:20:13 -0700511 * The caller must be holding clp->cl_sem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 */
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000513static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000515 struct nfs4_lock_state *lsp, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000517 for(;;) {
518 spin_lock(&state->state_lock);
519 lsp = __nfs4_find_lock_state(state, owner);
520 if (lsp != NULL)
521 break;
522 if (new != NULL) {
523 new->ls_state = state;
524 list_add(&new->ls_locks, &state->lock_states);
525 set_bit(LK_STATE_IN_USE, &state->flags);
526 lsp = new;
527 new = NULL;
528 break;
529 }
530 spin_unlock(&state->state_lock);
531 new = nfs4_alloc_lock_state(state, owner);
532 if (new == NULL)
533 return NULL;
534 }
535 spin_unlock(&state->state_lock);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400536 if (new != NULL)
537 nfs4_free_lock_state(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return lsp;
539}
540
541/*
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000542 * Release reference to lock_state, and free it if we see that
543 * it is no longer in use
544 */
Trond Myklebustfaf5f492005-10-18 14:20:15 -0700545void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000546{
547 struct nfs4_state *state;
548
549 if (lsp == NULL)
550 return;
551 state = lsp->ls_state;
552 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
553 return;
554 list_del(&lsp->ls_locks);
555 if (list_empty(&state->lock_states))
556 clear_bit(LK_STATE_IN_USE, &state->flags);
557 spin_unlock(&state->state_lock);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400558 nfs4_free_lock_state(lsp);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000559}
560
561static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
562{
563 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
564
565 dst->fl_u.nfs4_fl.owner = lsp;
566 atomic_inc(&lsp->ls_count);
567}
568
569static void nfs4_fl_release_lock(struct file_lock *fl)
570{
571 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
572}
573
574static struct file_lock_operations nfs4_fl_lock_ops = {
575 .fl_copy_lock = nfs4_fl_copy_lock,
576 .fl_release_private = nfs4_fl_release_lock,
577};
578
579int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
580{
581 struct nfs4_lock_state *lsp;
582
583 if (fl->fl_ops != NULL)
584 return 0;
585 lsp = nfs4_get_lock_state(state, fl->fl_owner);
586 if (lsp == NULL)
587 return -ENOMEM;
588 fl->fl_u.nfs4_fl.owner = lsp;
589 fl->fl_ops = &nfs4_fl_lock_ops;
590 return 0;
591}
592
593/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 * Byte-range lock aware utility to initialize the stateid of read/write
595 * requests.
596 */
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000597void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000599 struct nfs4_lock_state *lsp;
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400600 int seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400602 do {
603 seq = read_seqbegin(&state->seqlock);
604 memcpy(dst, &state->stateid, sizeof(*dst));
605 } while (read_seqretry(&state->seqlock, seq));
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000606 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
607 return;
608
609 spin_lock(&state->state_lock);
610 lsp = __nfs4_find_lock_state(state, fl_owner);
611 if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
612 memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
613 spin_unlock(&state->state_lock);
614 nfs4_put_lock_state(lsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700617struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Trond Myklebust36f20c62005-11-25 17:09:57 -0500619 struct rpc_sequence *sequence = counter->sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700620 struct nfs_seqid *new;
621
622 new = kmalloc(sizeof(*new), GFP_KERNEL);
623 if (new != NULL) {
624 new->sequence = counter;
Trond Myklebust36f20c62005-11-25 17:09:57 -0500625 spin_lock(&sequence->lock);
626 list_add_tail(&new->list, &sequence->list);
627 spin_unlock(&sequence->lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700628 }
629 return new;
630}
631
632void nfs_free_seqid(struct nfs_seqid *seqid)
633{
634 struct rpc_sequence *sequence = seqid->sequence->sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700635
Trond Myklebust36f20c62005-11-25 17:09:57 -0500636 spin_lock(&sequence->lock);
637 list_del(&seqid->list);
638 spin_unlock(&sequence->lock);
639 rpc_wake_up(&sequence->wait);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700640 kfree(seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700644 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
645 * failed with a seqid incrementing error -
646 * see comments nfs_fs.h:seqid_mutating_error()
647 */
Trond Myklebust88d90932007-07-02 14:03:03 -0400648static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700650 switch (status) {
651 case 0:
652 break;
653 case -NFS4ERR_BAD_SEQID:
Trond Myklebust6f43ddc2007-07-08 16:49:11 -0400654 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
655 return;
656 printk(KERN_WARNING "NFS: v4 server returned a bad"
657 "sequence-id error on an"
658 "unconfirmed sequence %p!\n",
659 seqid->sequence);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700660 case -NFS4ERR_STALE_CLIENTID:
661 case -NFS4ERR_STALE_STATEID:
662 case -NFS4ERR_BAD_STATEID:
663 case -NFS4ERR_BADXDR:
664 case -NFS4ERR_RESOURCE:
665 case -NFS4ERR_NOFILEHANDLE:
666 /* Non-seqid mutating errors */
667 return;
668 };
669 /*
670 * Note: no locking needed as we are guaranteed to be first
671 * on the sequence list
672 */
673 seqid->sequence->counter++;
674}
675
676void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
677{
678 if (status == -NFS4ERR_BAD_SEQID) {
679 struct nfs4_state_owner *sp = container_of(seqid->sequence,
680 struct nfs4_state_owner, so_seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 nfs4_drop_state_owner(sp);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700682 }
Trond Myklebust88d90932007-07-02 14:03:03 -0400683 nfs_increment_seqid(status, seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700684}
685
686/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700687 * Increment the seqid if the LOCK/LOCKU succeeded, or
688 * failed with a seqid incrementing error -
689 * see comments nfs_fs.h:seqid_mutating_error()
690 */
691void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
692{
Trond Myklebust88d90932007-07-02 14:03:03 -0400693 nfs_increment_seqid(status, seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700694}
695
696int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
697{
698 struct rpc_sequence *sequence = seqid->sequence->sequence;
699 int status = 0;
700
Trond Myklebust4e513362005-10-20 14:22:41 -0700701 if (sequence->list.next == &seqid->list)
702 goto out;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700703 spin_lock(&sequence->lock);
Trond Myklebust36f20c62005-11-25 17:09:57 -0500704 if (sequence->list.next != &seqid->list) {
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700705 rpc_sleep_on(&sequence->wait, task, NULL, NULL);
706 status = -EAGAIN;
Trond Myklebust36f20c62005-11-25 17:09:57 -0500707 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700708 spin_unlock(&sequence->lock);
Trond Myklebust4e513362005-10-20 14:22:41 -0700709out:
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700710 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
713static int reclaimer(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
David Howellsadfa6f92006-08-22 20:06:08 -0400715static inline void nfs4_clear_recover_bit(struct nfs_client *clp)
Trond Myklebust433fbe42006-01-03 09:55:22 +0100716{
717 smp_mb__before_clear_bit();
718 clear_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state);
719 smp_mb__after_clear_bit();
720 wake_up_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER);
721 rpc_wake_up(&clp->cl_rpcwaitq);
722}
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724/*
725 * State recovery routine
726 */
David Howellsadfa6f92006-08-22 20:06:08 -0400727static void nfs4_recover_state(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Trond Myklebust5043e902006-01-03 09:55:23 +0100729 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Trond Myklebust5043e902006-01-03 09:55:23 +0100731 __module_get(THIS_MODULE);
732 atomic_inc(&clp->cl_count);
733 task = kthread_run(reclaimer, clp, "%u.%u.%u.%u-reclaim",
David Howells24c8dbb2006-08-22 20:06:10 -0400734 NIPQUAD(clp->cl_addr.sin_addr));
Trond Myklebust5043e902006-01-03 09:55:23 +0100735 if (!IS_ERR(task))
736 return;
Trond Myklebust433fbe42006-01-03 09:55:22 +0100737 nfs4_clear_recover_bit(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400738 nfs_put_client(clp);
Trond Myklebust5043e902006-01-03 09:55:23 +0100739 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742/*
743 * Schedule a state recovery attempt
744 */
David Howellsadfa6f92006-08-22 20:06:08 -0400745void nfs4_schedule_state_recovery(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 if (!clp)
748 return;
Trond Myklebust433fbe42006-01-03 09:55:22 +0100749 if (test_and_set_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
Trond Myklebust5043e902006-01-03 09:55:23 +0100750 nfs4_recover_state(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
753static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops *ops, struct nfs4_state *state)
754{
755 struct inode *inode = state->inode;
756 struct file_lock *fl;
757 int status = 0;
758
759 for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
Trond Myklebust43b2a332005-11-04 15:35:30 -0500760 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 continue;
762 if (((struct nfs_open_context *)fl->fl_file->private_data)->state != state)
763 continue;
764 status = ops->recover_lock(state, fl);
765 if (status >= 0)
766 continue;
767 switch (status) {
768 default:
769 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
770 __FUNCTION__, status);
771 case -NFS4ERR_EXPIRED:
772 case -NFS4ERR_NO_GRACE:
773 case -NFS4ERR_RECLAIM_BAD:
774 case -NFS4ERR_RECLAIM_CONFLICT:
Trond Myklebust43b2a332005-11-04 15:35:30 -0500775 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 break;
777 case -NFS4ERR_STALE_CLIENTID:
778 goto out_err;
779 }
780 }
781 return 0;
782out_err:
783 return status;
784}
785
786static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops *ops, struct nfs4_state_owner *sp)
787{
788 struct nfs4_state *state;
789 struct nfs4_lock_state *lock;
790 int status = 0;
791
792 /* Note: we rely on the sp->so_states list being ordered
793 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
794 * states first.
795 * This is needed to ensure that the server won't give us any
796 * read delegations that we have to return if, say, we are
797 * recovering after a network partition or a reboot from a
798 * server that doesn't support a grace period.
799 */
800 list_for_each_entry(state, &sp->so_states, open_states) {
801 if (state->state == 0)
802 continue;
803 status = ops->recover_open(sp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (status >= 0) {
805 status = nfs4_reclaim_locks(ops, state);
806 if (status < 0)
807 goto out_err;
808 list_for_each_entry(lock, &state->lock_states, ls_locks) {
809 if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
810 printk("%s: Lock reclaim failed!\n",
811 __FUNCTION__);
812 }
813 continue;
814 }
815 switch (status) {
816 default:
817 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
818 __FUNCTION__, status);
819 case -ENOENT:
820 case -NFS4ERR_RECLAIM_BAD:
821 case -NFS4ERR_RECLAIM_CONFLICT:
822 /*
823 * Open state on this file cannot be recovered
824 * All we can do is revert to using the zero stateid.
825 */
826 memset(state->stateid.data, 0,
827 sizeof(state->stateid.data));
828 /* Mark the file as being 'closed' */
829 state->state = 0;
830 break;
831 case -NFS4ERR_EXPIRED:
832 case -NFS4ERR_NO_GRACE:
833 case -NFS4ERR_STALE_CLIENTID:
834 goto out_err;
835 }
836 }
837 return 0;
838out_err:
839 return status;
840}
841
David Howellsadfa6f92006-08-22 20:06:08 -0400842static void nfs4_state_mark_reclaim(struct nfs_client *clp)
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700843{
844 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400845 struct rb_node *pos;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700846 struct nfs4_state *state;
847 struct nfs4_lock_state *lock;
848
849 /* Reset all sequence ids to zero */
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400850 for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
851 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700852 sp->so_seqid.counter = 0;
853 sp->so_seqid.flags = 0;
Trond Myklebustec073422005-10-20 14:22:47 -0700854 spin_lock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700855 list_for_each_entry(state, &sp->so_states, open_states) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400856 clear_bit(NFS_DELEGATED_STATE, &state->flags);
857 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
858 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
859 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700860 list_for_each_entry(lock, &state->lock_states, ls_locks) {
861 lock->ls_seqid.counter = 0;
862 lock->ls_seqid.flags = 0;
863 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
864 }
865 }
Trond Myklebustec073422005-10-20 14:22:47 -0700866 spin_unlock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700867 }
868}
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870static int reclaimer(void *ptr)
871{
David Howellsadfa6f92006-08-22 20:06:08 -0400872 struct nfs_client *clp = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400874 struct rb_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 struct nfs4_state_recovery_ops *ops;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100876 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 int status = 0;
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 allow_signal(SIGKILL);
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /* Ensure exclusive access to NFSv4 state */
882 lock_kernel();
883 down_write(&clp->cl_sem);
884 /* Are there any NFS mounts out there? */
885 if (list_empty(&clp->cl_superblocks))
886 goto out;
887restart_loop:
Trond Myklebust286d7d62006-01-03 09:55:26 +0100888 ops = &nfs4_network_partition_recovery_ops;
889 /* Are there any open files on this volume? */
890 cred = nfs4_get_renew_cred(clp);
891 if (cred != NULL) {
892 /* Yes there are: try to renew the old lease */
893 status = nfs4_proc_renew(clp, cred);
894 switch (status) {
895 case 0:
896 case -NFS4ERR_CB_PATH_DOWN:
897 put_rpccred(cred);
898 goto out;
899 case -NFS4ERR_STALE_CLIENTID:
900 case -NFS4ERR_LEASE_MOVED:
901 ops = &nfs4_reboot_recovery_ops;
902 }
903 } else {
904 /* "reboot" to ensure we clear all state on the server */
905 clp->cl_boot_time = CURRENT_TIME;
906 cred = nfs4_get_setclientid_cred(clp);
907 }
908 /* We're going to have to re-establish a clientid */
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700909 nfs4_state_mark_reclaim(clp);
Trond Myklebust286d7d62006-01-03 09:55:26 +0100910 status = -ENOENT;
911 if (cred != NULL) {
912 status = nfs4_init_client(clp, cred);
913 put_rpccred(cred);
914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (status)
916 goto out_error;
917 /* Mark all delegations for reclaim */
918 nfs_delegation_mark_reclaim(clp);
919 /* Note: list is protected by exclusive lock on cl->cl_sem */
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400920 for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
921 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 status = nfs4_reclaim_open_state(ops, sp);
923 if (status < 0) {
924 if (status == -NFS4ERR_NO_GRACE) {
925 ops = &nfs4_network_partition_recovery_ops;
926 status = nfs4_reclaim_open_state(ops, sp);
927 }
928 if (status == -NFS4ERR_STALE_CLIENTID)
929 goto restart_loop;
930 if (status == -NFS4ERR_EXPIRED)
931 goto restart_loop;
932 }
933 }
934 nfs_delegation_reap_unclaimed(clp);
935out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 up_write(&clp->cl_sem);
937 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (status == -NFS4ERR_CB_PATH_DOWN)
939 nfs_handle_cb_pathdown(clp);
Trond Myklebust433fbe42006-01-03 09:55:22 +0100940 nfs4_clear_recover_bit(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400941 nfs_put_client(clp);
Trond Myklebust5043e902006-01-03 09:55:23 +0100942 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return 0;
944out_error:
945 printk(KERN_WARNING "Error: state recovery failed on NFSv4 server %u.%u.%u.%u with error %d\n",
David Howells24c8dbb2006-08-22 20:06:10 -0400946 NIPQUAD(clp->cl_addr.sin_addr), -status);
Trond Myklebust51581f32006-03-20 13:44:47 -0500947 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 goto out;
949}
950
951/*
952 * Local variables:
953 * c-basic-offset: 8
954 * End:
955 */