blob: ac816b303f3aa9cfdb7f948fdba332b8b9455876 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/slab.h>
42#include <linux/smp_lock.h>
43#include <linux/nfs_fs.h>
44#include <linux/nfs_idmap.h>
Trond Myklebust5043e902006-01-03 09:55:23 +010045#include <linux/kthread.h>
46#include <linux/module.h>
Trond Myklebust9f958ab2007-07-02 13:58:33 -040047#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/workqueue.h>
49#include <linux/bitops.h>
50
Trond Myklebust4ce79712005-06-22 17:16:21 +000051#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include "callback.h"
53#include "delegation.h"
David Howells24c8dbb2006-08-22 20:06:10 -040054#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#define OPENOWNER_POOL_SIZE 8
57
Trond Myklebust4ce79712005-06-22 17:16:21 +000058const nfs4_stateid zero_stateid;
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static LIST_HEAD(nfs4_clientid_list);
61
David Howellsadfa6f92006-08-22 20:06:08 -040062static int nfs4_init_client(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Trond Myklebust286d7d62006-01-03 09:55:26 +010064 int status = nfs4_proc_setclientid(clp, NFS4_CALLBACK,
65 nfs_callback_tcpport, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (status == 0)
Trond Myklebust286d7d62006-01-03 09:55:26 +010067 status = nfs4_proc_setclientid_confirm(clp, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (status == 0)
69 nfs4_schedule_state_renewal(clp);
70 return status;
71}
72
David Howellsadfa6f92006-08-22 20:06:08 -040073struct rpc_cred *nfs4_get_renew_cred(struct nfs_client *clp)
Trond Myklebustb4454fe2006-01-03 09:55:25 +010074{
75 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -040076 struct rb_node *pos;
Trond Myklebustb4454fe2006-01-03 09:55:25 +010077 struct rpc_cred *cred = NULL;
78
Trond Myklebust9f958ab2007-07-02 13:58:33 -040079 for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
80 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
Trond Myklebustb4454fe2006-01-03 09:55:25 +010081 if (list_empty(&sp->so_states))
82 continue;
83 cred = get_rpccred(sp->so_cred);
84 break;
85 }
86 return cred;
87}
88
Trond Myklebust10afec92007-05-14 17:16:04 -040089static struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
Trond Myklebust286d7d62006-01-03 09:55:26 +010090{
91 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -040092 struct rb_node *pos;
Trond Myklebust286d7d62006-01-03 09:55:26 +010093
Trond Myklebust9f958ab2007-07-02 13:58:33 -040094 pos = rb_first(&clp->cl_state_owners);
95 if (pos != NULL) {
96 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
Trond Myklebust286d7d62006-01-03 09:55:26 +010097 return get_rpccred(sp->so_cred);
98 }
99 return NULL;
100}
101
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400102static void nfs_alloc_unique_id(struct rb_root *root, struct nfs_unique_id *new,
103 __u64 minval, int maxbits)
104{
105 struct rb_node **p, *parent;
106 struct nfs_unique_id *pos;
107 __u64 mask = ~0ULL;
108
109 if (maxbits < 64)
110 mask = (1ULL << maxbits) - 1ULL;
111
112 /* Ensure distribution is more or less flat */
113 get_random_bytes(&new->id, sizeof(new->id));
114 new->id &= mask;
115 if (new->id < minval)
116 new->id += minval;
117retry:
118 p = &root->rb_node;
119 parent = NULL;
120
121 while (*p != NULL) {
122 parent = *p;
123 pos = rb_entry(parent, struct nfs_unique_id, rb_node);
124
125 if (new->id < pos->id)
126 p = &(*p)->rb_left;
127 else if (new->id > pos->id)
128 p = &(*p)->rb_right;
129 else
130 goto id_exists;
131 }
132 rb_link_node(&new->rb_node, parent, p);
133 rb_insert_color(&new->rb_node, root);
134 return;
135id_exists:
136 for (;;) {
137 new->id++;
138 if (new->id < minval || (new->id & mask) != new->id) {
139 new->id = minval;
140 break;
141 }
142 parent = rb_next(parent);
143 if (parent == NULL)
144 break;
145 pos = rb_entry(parent, struct nfs_unique_id, rb_node);
146 if (new->id < pos->id)
147 break;
148 }
149 goto retry;
150}
151
152static void nfs_free_unique_id(struct rb_root *root, struct nfs_unique_id *id)
153{
154 rb_erase(&id->rb_node, root);
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static struct nfs4_state_owner *
David Howellsadfa6f92006-08-22 20:06:08 -0400158nfs4_find_state_owner(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400160 struct rb_node **p = &clp->cl_state_owners.rb_node,
161 *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 struct nfs4_state_owner *sp, *res = NULL;
163
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400164 while (*p != NULL) {
165 parent = *p;
166 sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
167
168 if (cred < sp->so_cred)
169 p = &parent->rb_left;
170 else if (cred > sp->so_cred)
171 p = &parent->rb_right;
172 else {
173 atomic_inc(&sp->so_count);
174 res = sp;
175 break;
176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
178 return res;
179}
180
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400181static struct nfs4_state_owner *
182nfs4_insert_state_owner(struct nfs_client *clp, struct nfs4_state_owner *new)
183{
184 struct rb_node **p = &clp->cl_state_owners.rb_node,
185 *parent = NULL;
186 struct nfs4_state_owner *sp;
187
188 while (*p != NULL) {
189 parent = *p;
190 sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
191
192 if (new->so_cred < sp->so_cred)
193 p = &parent->rb_left;
194 else if (new->so_cred > sp->so_cred)
195 p = &parent->rb_right;
196 else {
197 atomic_inc(&sp->so_count);
198 return sp;
199 }
200 }
201 nfs_alloc_unique_id(&clp->cl_openowner_id, &new->so_owner_id, 1, 64);
202 rb_link_node(&new->so_client_node, parent, p);
203 rb_insert_color(&new->so_client_node, &clp->cl_state_owners);
204 return new;
205}
206
207static void
208nfs4_remove_state_owner(struct nfs_client *clp, struct nfs4_state_owner *sp)
209{
210 if (!RB_EMPTY_NODE(&sp->so_client_node))
211 rb_erase(&sp->so_client_node, &clp->cl_state_owners);
212 nfs_free_unique_id(&clp->cl_openowner_id, &sp->so_owner_id);
213}
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215/*
216 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
217 * create a new state_owner.
218 *
219 */
220static struct nfs4_state_owner *
221nfs4_alloc_state_owner(void)
222{
223 struct nfs4_state_owner *sp;
224
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700225 sp = kzalloc(sizeof(*sp),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (!sp)
227 return NULL;
Trond Myklebustec073422005-10-20 14:22:47 -0700228 spin_lock_init(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 INIT_LIST_HEAD(&sp->so_states);
230 INIT_LIST_HEAD(&sp->so_delegations);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700231 rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
232 sp->so_seqid.sequence = &sp->so_sequence;
233 spin_lock_init(&sp->so_sequence.lock);
234 INIT_LIST_HEAD(&sp->so_sequence.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 atomic_set(&sp->so_count, 1);
236 return sp;
237}
238
239void
240nfs4_drop_state_owner(struct nfs4_state_owner *sp)
241{
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400242 if (!RB_EMPTY_NODE(&sp->so_client_node)) {
243 struct nfs_client *clp = sp->so_client;
244
245 spin_lock(&clp->cl_lock);
246 rb_erase(&sp->so_client_node, &clp->cl_state_owners);
247 RB_CLEAR_NODE(&sp->so_client_node);
248 spin_unlock(&clp->cl_lock);
249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252/*
253 * Note: must be called with clp->cl_sem held in order to prevent races
254 * with reboot recovery!
255 */
256struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, struct rpc_cred *cred)
257{
David Howells7539bba2006-08-22 20:06:09 -0400258 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 struct nfs4_state_owner *sp, *new;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 spin_lock(&clp->cl_lock);
262 sp = nfs4_find_state_owner(clp, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 spin_unlock(&clp->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (sp != NULL)
265 return sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400266 new = nfs4_alloc_state_owner();
267 if (new == NULL)
268 return NULL;
269 new->so_client = clp;
270 new->so_cred = cred;
271 spin_lock(&clp->cl_lock);
272 sp = nfs4_insert_state_owner(clp, new);
273 spin_unlock(&clp->cl_lock);
274 if (sp == new)
275 get_rpccred(cred);
276 else
277 kfree(new);
278 return sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281/*
282 * Must be called with clp->cl_sem held in order to avoid races
283 * with state recovery...
284 */
285void nfs4_put_state_owner(struct nfs4_state_owner *sp)
286{
David Howellsadfa6f92006-08-22 20:06:08 -0400287 struct nfs_client *clp = sp->so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 struct rpc_cred *cred = sp->so_cred;
289
290 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
291 return;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400292 nfs4_remove_state_owner(clp, sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 spin_unlock(&clp->cl_lock);
294 put_rpccred(cred);
295 kfree(sp);
296}
297
298static struct nfs4_state *
299nfs4_alloc_open_state(void)
300{
301 struct nfs4_state *state;
302
Trond Myklebuste7616922006-01-03 09:55:13 +0100303 state = kzalloc(sizeof(*state), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (!state)
305 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 atomic_set(&state->count, 1);
307 INIT_LIST_HEAD(&state->lock_states);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000308 spin_lock_init(&state->state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return state;
310}
311
Trond Myklebust4cecb762005-11-04 15:32:58 -0500312void
313nfs4_state_set_mode_locked(struct nfs4_state *state, mode_t mode)
314{
315 if (state->state == mode)
316 return;
317 /* NB! List reordering - see the reclaim code for why. */
318 if ((mode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
319 if (mode & FMODE_WRITE)
320 list_move(&state->open_states, &state->owner->so_states);
321 else
322 list_move_tail(&state->open_states, &state->owner->so_states);
323 }
324 if (mode == 0)
325 list_del_init(&state->inode_states);
326 state->state = mode;
327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329static struct nfs4_state *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330__nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
331{
332 struct nfs_inode *nfsi = NFS_I(inode);
333 struct nfs4_state *state;
334
335 list_for_each_entry(state, &nfsi->open_states, inode_states) {
Trond Myklebust1c816ef2007-07-03 14:41:19 -0400336 if (state->owner != owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 continue;
Trond Myklebust1c816ef2007-07-03 14:41:19 -0400338 if (atomic_inc_not_zero(&state->count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341 return NULL;
342}
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344static void
345nfs4_free_open_state(struct nfs4_state *state)
346{
347 kfree(state);
348}
349
350struct nfs4_state *
351nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
352{
353 struct nfs4_state *state, *new;
354 struct nfs_inode *nfsi = NFS_I(inode);
355
356 spin_lock(&inode->i_lock);
357 state = __nfs4_find_state_byowner(inode, owner);
358 spin_unlock(&inode->i_lock);
359 if (state)
360 goto out;
361 new = nfs4_alloc_open_state();
Trond Myklebustec073422005-10-20 14:22:47 -0700362 spin_lock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 spin_lock(&inode->i_lock);
364 state = __nfs4_find_state_byowner(inode, owner);
365 if (state == NULL && new != NULL) {
366 state = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 state->owner = owner;
368 atomic_inc(&owner->so_count);
369 list_add(&state->inode_states, &nfsi->open_states);
370 state->inode = igrab(inode);
371 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700372 /* Note: The reclaim code dictates that we add stateless
373 * and read-only stateids to the end of the list */
374 list_add_tail(&state->open_states, &owner->so_states);
375 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 } else {
377 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700378 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (new)
380 nfs4_free_open_state(new);
381 }
382out:
383 return state;
384}
385
386/*
387 * Beware! Caller must be holding exactly one
Trond Myklebuste6dfa552005-10-18 14:20:13 -0700388 * reference to clp->cl_sem!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 */
390void nfs4_put_open_state(struct nfs4_state *state)
391{
392 struct inode *inode = state->inode;
393 struct nfs4_state_owner *owner = state->owner;
394
Trond Myklebustec073422005-10-20 14:22:47 -0700395 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return;
Trond Myklebustec073422005-10-20 14:22:47 -0700397 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (!list_empty(&state->inode_states))
399 list_del(&state->inode_states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 list_del(&state->open_states);
Trond Myklebustec073422005-10-20 14:22:47 -0700401 spin_unlock(&inode->i_lock);
402 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 nfs4_free_open_state(state);
405 nfs4_put_state_owner(owner);
406}
407
408/*
Trond Myklebust83c9d412005-10-18 14:20:13 -0700409 * Close the current file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 */
Trond Myklebust4a35bd42007-06-05 10:31:33 -0400411void nfs4_close_state(struct path *path, struct nfs4_state *state, mode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 struct inode *inode = state->inode;
414 struct nfs4_state_owner *owner = state->owner;
Trond Myklebust4cecb762005-11-04 15:32:58 -0500415 int oldstate, newstate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 atomic_inc(&owner->so_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /* Protect against nfs4_find_state() */
Trond Myklebustec073422005-10-20 14:22:47 -0700419 spin_lock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 spin_lock(&inode->i_lock);
Trond Myklebuste7616922006-01-03 09:55:13 +0100421 switch (mode & (FMODE_READ | FMODE_WRITE)) {
422 case FMODE_READ:
423 state->n_rdonly--;
424 break;
425 case FMODE_WRITE:
426 state->n_wronly--;
427 break;
428 case FMODE_READ|FMODE_WRITE:
429 state->n_rdwr--;
430 }
Trond Myklebust4cecb762005-11-04 15:32:58 -0500431 oldstate = newstate = state->state;
Trond Myklebuste7616922006-01-03 09:55:13 +0100432 if (state->n_rdwr == 0) {
433 if (state->n_rdonly == 0)
434 newstate &= ~FMODE_READ;
435 if (state->n_wronly == 0)
436 newstate &= ~FMODE_WRITE;
437 }
Trond Myklebust4cecb762005-11-04 15:32:58 -0500438 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
439 nfs4_state_set_mode_locked(state, newstate);
440 oldstate = newstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700443 spin_unlock(&owner->so_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500444
Trond Myklebustb39e6252007-06-11 23:05:07 -0400445 if (oldstate == newstate) {
446 nfs4_put_open_state(state);
447 nfs4_put_state_owner(owner);
448 } else
449 nfs4_do_close(path, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
452/*
453 * Search the state->lock_states for an existing lock_owner
454 * that is compatible with current->files
455 */
456static struct nfs4_lock_state *
457__nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
458{
459 struct nfs4_lock_state *pos;
460 list_for_each_entry(pos, &state->lock_states, ls_locks) {
461 if (pos->ls_owner != fl_owner)
462 continue;
463 atomic_inc(&pos->ls_count);
464 return pos;
465 }
466 return NULL;
467}
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469/*
470 * Return a compatible lock_state. If no initialized lock_state structure
471 * exists, return an uninitialized one.
472 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 */
474static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
475{
476 struct nfs4_lock_state *lsp;
David Howellsadfa6f92006-08-22 20:06:08 -0400477 struct nfs_client *clp = state->owner->so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700479 lsp = kzalloc(sizeof(*lsp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (lsp == NULL)
481 return NULL;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700482 lsp->ls_seqid.sequence = &state->owner->so_sequence;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 atomic_set(&lsp->ls_count, 1);
484 lsp->ls_owner = fl_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 spin_lock(&clp->cl_lock);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400486 nfs_alloc_unique_id(&clp->cl_lockowner_id, &lsp->ls_id, 1, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 spin_unlock(&clp->cl_lock);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000488 INIT_LIST_HEAD(&lsp->ls_locks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return lsp;
490}
491
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400492static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
493{
494 struct nfs_client *clp = lsp->ls_state->owner->so_client;
495
496 spin_lock(&clp->cl_lock);
497 nfs_free_unique_id(&clp->cl_lockowner_id, &lsp->ls_id);
498 spin_unlock(&clp->cl_lock);
499 kfree(lsp);
500}
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502/*
503 * Return a compatible lock_state. If no initialized lock_state structure
504 * exists, return an uninitialized one.
505 *
Trond Myklebuste6dfa552005-10-18 14:20:13 -0700506 * The caller must be holding clp->cl_sem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 */
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000508static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000510 struct nfs4_lock_state *lsp, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000512 for(;;) {
513 spin_lock(&state->state_lock);
514 lsp = __nfs4_find_lock_state(state, owner);
515 if (lsp != NULL)
516 break;
517 if (new != NULL) {
518 new->ls_state = state;
519 list_add(&new->ls_locks, &state->lock_states);
520 set_bit(LK_STATE_IN_USE, &state->flags);
521 lsp = new;
522 new = NULL;
523 break;
524 }
525 spin_unlock(&state->state_lock);
526 new = nfs4_alloc_lock_state(state, owner);
527 if (new == NULL)
528 return NULL;
529 }
530 spin_unlock(&state->state_lock);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400531 if (new != NULL)
532 nfs4_free_lock_state(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return lsp;
534}
535
536/*
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000537 * Release reference to lock_state, and free it if we see that
538 * it is no longer in use
539 */
Trond Myklebustfaf5f492005-10-18 14:20:15 -0700540void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000541{
542 struct nfs4_state *state;
543
544 if (lsp == NULL)
545 return;
546 state = lsp->ls_state;
547 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
548 return;
549 list_del(&lsp->ls_locks);
550 if (list_empty(&state->lock_states))
551 clear_bit(LK_STATE_IN_USE, &state->flags);
552 spin_unlock(&state->state_lock);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400553 nfs4_free_lock_state(lsp);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000554}
555
556static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
557{
558 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
559
560 dst->fl_u.nfs4_fl.owner = lsp;
561 atomic_inc(&lsp->ls_count);
562}
563
564static void nfs4_fl_release_lock(struct file_lock *fl)
565{
566 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
567}
568
569static struct file_lock_operations nfs4_fl_lock_ops = {
570 .fl_copy_lock = nfs4_fl_copy_lock,
571 .fl_release_private = nfs4_fl_release_lock,
572};
573
574int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
575{
576 struct nfs4_lock_state *lsp;
577
578 if (fl->fl_ops != NULL)
579 return 0;
580 lsp = nfs4_get_lock_state(state, fl->fl_owner);
581 if (lsp == NULL)
582 return -ENOMEM;
583 fl->fl_u.nfs4_fl.owner = lsp;
584 fl->fl_ops = &nfs4_fl_lock_ops;
585 return 0;
586}
587
588/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 * Byte-range lock aware utility to initialize the stateid of read/write
590 * requests.
591 */
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000592void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000594 struct nfs4_lock_state *lsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 memcpy(dst, &state->stateid, sizeof(*dst));
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000597 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
598 return;
599
600 spin_lock(&state->state_lock);
601 lsp = __nfs4_find_lock_state(state, fl_owner);
602 if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
603 memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
604 spin_unlock(&state->state_lock);
605 nfs4_put_lock_state(lsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700608struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Trond Myklebust36f20c62005-11-25 17:09:57 -0500610 struct rpc_sequence *sequence = counter->sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700611 struct nfs_seqid *new;
612
613 new = kmalloc(sizeof(*new), GFP_KERNEL);
614 if (new != NULL) {
615 new->sequence = counter;
Trond Myklebust36f20c62005-11-25 17:09:57 -0500616 spin_lock(&sequence->lock);
617 list_add_tail(&new->list, &sequence->list);
618 spin_unlock(&sequence->lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700619 }
620 return new;
621}
622
623void nfs_free_seqid(struct nfs_seqid *seqid)
624{
625 struct rpc_sequence *sequence = seqid->sequence->sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700626
Trond Myklebust36f20c62005-11-25 17:09:57 -0500627 spin_lock(&sequence->lock);
628 list_del(&seqid->list);
629 spin_unlock(&sequence->lock);
630 rpc_wake_up(&sequence->wait);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700631 kfree(seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700635 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
636 * failed with a seqid incrementing error -
637 * see comments nfs_fs.h:seqid_mutating_error()
638 */
Trond Myklebust88d90932007-07-02 14:03:03 -0400639static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700641 switch (status) {
642 case 0:
643 break;
644 case -NFS4ERR_BAD_SEQID:
645 case -NFS4ERR_STALE_CLIENTID:
646 case -NFS4ERR_STALE_STATEID:
647 case -NFS4ERR_BAD_STATEID:
648 case -NFS4ERR_BADXDR:
649 case -NFS4ERR_RESOURCE:
650 case -NFS4ERR_NOFILEHANDLE:
651 /* Non-seqid mutating errors */
652 return;
653 };
654 /*
655 * Note: no locking needed as we are guaranteed to be first
656 * on the sequence list
657 */
658 seqid->sequence->counter++;
659}
660
661void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
662{
663 if (status == -NFS4ERR_BAD_SEQID) {
664 struct nfs4_state_owner *sp = container_of(seqid->sequence,
665 struct nfs4_state_owner, so_seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 nfs4_drop_state_owner(sp);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700667 }
Trond Myklebust88d90932007-07-02 14:03:03 -0400668 nfs_increment_seqid(status, seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700669}
670
671/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700672 * Increment the seqid if the LOCK/LOCKU succeeded, or
673 * failed with a seqid incrementing error -
674 * see comments nfs_fs.h:seqid_mutating_error()
675 */
676void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
677{
Trond Myklebust88d90932007-07-02 14:03:03 -0400678 nfs_increment_seqid(status, seqid);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700679}
680
681int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
682{
683 struct rpc_sequence *sequence = seqid->sequence->sequence;
684 int status = 0;
685
Trond Myklebust4e513362005-10-20 14:22:41 -0700686 if (sequence->list.next == &seqid->list)
687 goto out;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700688 spin_lock(&sequence->lock);
Trond Myklebust36f20c62005-11-25 17:09:57 -0500689 if (sequence->list.next != &seqid->list) {
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700690 rpc_sleep_on(&sequence->wait, task, NULL, NULL);
691 status = -EAGAIN;
Trond Myklebust36f20c62005-11-25 17:09:57 -0500692 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700693 spin_unlock(&sequence->lock);
Trond Myklebust4e513362005-10-20 14:22:41 -0700694out:
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700695 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
698static int reclaimer(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
David Howellsadfa6f92006-08-22 20:06:08 -0400700static inline void nfs4_clear_recover_bit(struct nfs_client *clp)
Trond Myklebust433fbe42006-01-03 09:55:22 +0100701{
702 smp_mb__before_clear_bit();
703 clear_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state);
704 smp_mb__after_clear_bit();
705 wake_up_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER);
706 rpc_wake_up(&clp->cl_rpcwaitq);
707}
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709/*
710 * State recovery routine
711 */
David Howellsadfa6f92006-08-22 20:06:08 -0400712static void nfs4_recover_state(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Trond Myklebust5043e902006-01-03 09:55:23 +0100714 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Trond Myklebust5043e902006-01-03 09:55:23 +0100716 __module_get(THIS_MODULE);
717 atomic_inc(&clp->cl_count);
718 task = kthread_run(reclaimer, clp, "%u.%u.%u.%u-reclaim",
David Howells24c8dbb2006-08-22 20:06:10 -0400719 NIPQUAD(clp->cl_addr.sin_addr));
Trond Myklebust5043e902006-01-03 09:55:23 +0100720 if (!IS_ERR(task))
721 return;
Trond Myklebust433fbe42006-01-03 09:55:22 +0100722 nfs4_clear_recover_bit(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400723 nfs_put_client(clp);
Trond Myklebust5043e902006-01-03 09:55:23 +0100724 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
727/*
728 * Schedule a state recovery attempt
729 */
David Howellsadfa6f92006-08-22 20:06:08 -0400730void nfs4_schedule_state_recovery(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 if (!clp)
733 return;
Trond Myklebust433fbe42006-01-03 09:55:22 +0100734 if (test_and_set_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
Trond Myklebust5043e902006-01-03 09:55:23 +0100735 nfs4_recover_state(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
738static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops *ops, struct nfs4_state *state)
739{
740 struct inode *inode = state->inode;
741 struct file_lock *fl;
742 int status = 0;
743
744 for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
Trond Myklebust43b2a332005-11-04 15:35:30 -0500745 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 continue;
747 if (((struct nfs_open_context *)fl->fl_file->private_data)->state != state)
748 continue;
749 status = ops->recover_lock(state, fl);
750 if (status >= 0)
751 continue;
752 switch (status) {
753 default:
754 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
755 __FUNCTION__, status);
756 case -NFS4ERR_EXPIRED:
757 case -NFS4ERR_NO_GRACE:
758 case -NFS4ERR_RECLAIM_BAD:
759 case -NFS4ERR_RECLAIM_CONFLICT:
Trond Myklebust43b2a332005-11-04 15:35:30 -0500760 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 break;
762 case -NFS4ERR_STALE_CLIENTID:
763 goto out_err;
764 }
765 }
766 return 0;
767out_err:
768 return status;
769}
770
771static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops *ops, struct nfs4_state_owner *sp)
772{
773 struct nfs4_state *state;
774 struct nfs4_lock_state *lock;
775 int status = 0;
776
777 /* Note: we rely on the sp->so_states list being ordered
778 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
779 * states first.
780 * This is needed to ensure that the server won't give us any
781 * read delegations that we have to return if, say, we are
782 * recovering after a network partition or a reboot from a
783 * server that doesn't support a grace period.
784 */
785 list_for_each_entry(state, &sp->so_states, open_states) {
786 if (state->state == 0)
787 continue;
788 status = ops->recover_open(sp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (status >= 0) {
790 status = nfs4_reclaim_locks(ops, state);
791 if (status < 0)
792 goto out_err;
793 list_for_each_entry(lock, &state->lock_states, ls_locks) {
794 if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
795 printk("%s: Lock reclaim failed!\n",
796 __FUNCTION__);
797 }
798 continue;
799 }
800 switch (status) {
801 default:
802 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
803 __FUNCTION__, status);
804 case -ENOENT:
805 case -NFS4ERR_RECLAIM_BAD:
806 case -NFS4ERR_RECLAIM_CONFLICT:
807 /*
808 * Open state on this file cannot be recovered
809 * All we can do is revert to using the zero stateid.
810 */
811 memset(state->stateid.data, 0,
812 sizeof(state->stateid.data));
813 /* Mark the file as being 'closed' */
814 state->state = 0;
815 break;
816 case -NFS4ERR_EXPIRED:
817 case -NFS4ERR_NO_GRACE:
818 case -NFS4ERR_STALE_CLIENTID:
819 goto out_err;
820 }
821 }
822 return 0;
823out_err:
824 return status;
825}
826
David Howellsadfa6f92006-08-22 20:06:08 -0400827static void nfs4_state_mark_reclaim(struct nfs_client *clp)
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700828{
829 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400830 struct rb_node *pos;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700831 struct nfs4_state *state;
832 struct nfs4_lock_state *lock;
833
834 /* Reset all sequence ids to zero */
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400835 for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
836 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700837 sp->so_seqid.counter = 0;
838 sp->so_seqid.flags = 0;
Trond Myklebustec073422005-10-20 14:22:47 -0700839 spin_lock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700840 list_for_each_entry(state, &sp->so_states, open_states) {
841 list_for_each_entry(lock, &state->lock_states, ls_locks) {
842 lock->ls_seqid.counter = 0;
843 lock->ls_seqid.flags = 0;
844 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
845 }
846 }
Trond Myklebustec073422005-10-20 14:22:47 -0700847 spin_unlock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700848 }
849}
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851static int reclaimer(void *ptr)
852{
David Howellsadfa6f92006-08-22 20:06:08 -0400853 struct nfs_client *clp = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 struct nfs4_state_owner *sp;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400855 struct rb_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 struct nfs4_state_recovery_ops *ops;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100857 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 int status = 0;
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 allow_signal(SIGKILL);
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /* Ensure exclusive access to NFSv4 state */
863 lock_kernel();
864 down_write(&clp->cl_sem);
865 /* Are there any NFS mounts out there? */
866 if (list_empty(&clp->cl_superblocks))
867 goto out;
868restart_loop:
Trond Myklebust286d7d62006-01-03 09:55:26 +0100869 ops = &nfs4_network_partition_recovery_ops;
870 /* Are there any open files on this volume? */
871 cred = nfs4_get_renew_cred(clp);
872 if (cred != NULL) {
873 /* Yes there are: try to renew the old lease */
874 status = nfs4_proc_renew(clp, cred);
875 switch (status) {
876 case 0:
877 case -NFS4ERR_CB_PATH_DOWN:
878 put_rpccred(cred);
879 goto out;
880 case -NFS4ERR_STALE_CLIENTID:
881 case -NFS4ERR_LEASE_MOVED:
882 ops = &nfs4_reboot_recovery_ops;
883 }
884 } else {
885 /* "reboot" to ensure we clear all state on the server */
886 clp->cl_boot_time = CURRENT_TIME;
887 cred = nfs4_get_setclientid_cred(clp);
888 }
889 /* We're going to have to re-establish a clientid */
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700890 nfs4_state_mark_reclaim(clp);
Trond Myklebust286d7d62006-01-03 09:55:26 +0100891 status = -ENOENT;
892 if (cred != NULL) {
893 status = nfs4_init_client(clp, cred);
894 put_rpccred(cred);
895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (status)
897 goto out_error;
898 /* Mark all delegations for reclaim */
899 nfs_delegation_mark_reclaim(clp);
900 /* Note: list is protected by exclusive lock on cl->cl_sem */
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400901 for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
902 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 status = nfs4_reclaim_open_state(ops, sp);
904 if (status < 0) {
905 if (status == -NFS4ERR_NO_GRACE) {
906 ops = &nfs4_network_partition_recovery_ops;
907 status = nfs4_reclaim_open_state(ops, sp);
908 }
909 if (status == -NFS4ERR_STALE_CLIENTID)
910 goto restart_loop;
911 if (status == -NFS4ERR_EXPIRED)
912 goto restart_loop;
913 }
914 }
915 nfs_delegation_reap_unclaimed(clp);
916out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 up_write(&clp->cl_sem);
918 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (status == -NFS4ERR_CB_PATH_DOWN)
920 nfs_handle_cb_pathdown(clp);
Trond Myklebust433fbe42006-01-03 09:55:22 +0100921 nfs4_clear_recover_bit(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400922 nfs_put_client(clp);
Trond Myklebust5043e902006-01-03 09:55:23 +0100923 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return 0;
925out_error:
926 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 -0400927 NIPQUAD(clp->cl_addr.sin_addr), -status);
Trond Myklebust51581f32006-03-20 13:44:47 -0500928 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 goto out;
930}
931
932/*
933 * Local variables:
934 * c-basic-offset: 8
935 * End:
936 */