blob: f535c219cf3a9eddcd5bcd8aead84116d70be356 [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
41#include <linux/config.h>
42#include <linux/slab.h>
43#include <linux/smp_lock.h>
44#include <linux/nfs_fs.h>
45#include <linux/nfs_idmap.h>
46#include <linux/workqueue.h>
47#include <linux/bitops.h>
48
Trond Myklebust4ce79712005-06-22 17:16:21 +000049#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include "callback.h"
51#include "delegation.h"
52
53#define OPENOWNER_POOL_SIZE 8
54
Trond Myklebust4ce79712005-06-22 17:16:21 +000055const nfs4_stateid zero_stateid;
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static DEFINE_SPINLOCK(state_spinlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static LIST_HEAD(nfs4_clientid_list);
59
60static void nfs4_recover_state(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62void
63init_nfsv4_state(struct nfs_server *server)
64{
65 server->nfs4_state = NULL;
66 INIT_LIST_HEAD(&server->nfs4_siblings);
67}
68
69void
70destroy_nfsv4_state(struct nfs_server *server)
71{
72 if (server->mnt_path) {
73 kfree(server->mnt_path);
74 server->mnt_path = NULL;
75 }
76 if (server->nfs4_state) {
77 nfs4_put_client(server->nfs4_state);
78 server->nfs4_state = NULL;
79 }
80}
81
82/*
83 * nfs4_get_client(): returns an empty client structure
84 * nfs4_put_client(): drops reference to client structure
85 *
86 * Since these are allocated/deallocated very rarely, we don't
87 * bother putting them in a slab cache...
88 */
89static struct nfs4_client *
90nfs4_alloc_client(struct in_addr *addr)
91{
92 struct nfs4_client *clp;
93
94 if (nfs_callback_up() < 0)
95 return NULL;
96 if ((clp = kmalloc(sizeof(*clp), GFP_KERNEL)) == NULL) {
97 nfs_callback_down();
98 return NULL;
99 }
100 memset(clp, 0, sizeof(*clp));
101 memcpy(&clp->cl_addr, addr, sizeof(clp->cl_addr));
102 init_rwsem(&clp->cl_sem);
103 INIT_LIST_HEAD(&clp->cl_delegations);
104 INIT_LIST_HEAD(&clp->cl_state_owners);
105 INIT_LIST_HEAD(&clp->cl_unused);
106 spin_lock_init(&clp->cl_lock);
107 atomic_set(&clp->cl_count, 1);
108 INIT_WORK(&clp->cl_recoverd, nfs4_recover_state, clp);
109 INIT_WORK(&clp->cl_renewd, nfs4_renew_state, clp);
110 INIT_LIST_HEAD(&clp->cl_superblocks);
111 init_waitqueue_head(&clp->cl_waitq);
112 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS4 client");
J. Bruce Fields6a192752005-06-22 17:16:23 +0000113 clp->cl_rpcclient = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 clp->cl_boot_time = CURRENT_TIME;
115 clp->cl_state = 1 << NFS4CLNT_OK;
116 return clp;
117}
118
119static void
120nfs4_free_client(struct nfs4_client *clp)
121{
122 struct nfs4_state_owner *sp;
123
124 while (!list_empty(&clp->cl_unused)) {
125 sp = list_entry(clp->cl_unused.next,
126 struct nfs4_state_owner,
127 so_list);
128 list_del(&sp->so_list);
129 kfree(sp);
130 }
131 BUG_ON(!list_empty(&clp->cl_state_owners));
132 if (clp->cl_cred)
133 put_rpccred(clp->cl_cred);
134 nfs_idmap_delete(clp);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000135 if (!IS_ERR(clp->cl_rpcclient))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 rpc_shutdown_client(clp->cl_rpcclient);
137 kfree(clp);
138 nfs_callback_down();
139}
140
141static struct nfs4_client *__nfs4_find_client(struct in_addr *addr)
142{
143 struct nfs4_client *clp;
144 list_for_each_entry(clp, &nfs4_clientid_list, cl_servers) {
145 if (memcmp(&clp->cl_addr, addr, sizeof(clp->cl_addr)) == 0) {
146 atomic_inc(&clp->cl_count);
147 return clp;
148 }
149 }
150 return NULL;
151}
152
153struct nfs4_client *nfs4_find_client(struct in_addr *addr)
154{
155 struct nfs4_client *clp;
156 spin_lock(&state_spinlock);
157 clp = __nfs4_find_client(addr);
158 spin_unlock(&state_spinlock);
159 return clp;
160}
161
162struct nfs4_client *
163nfs4_get_client(struct in_addr *addr)
164{
165 struct nfs4_client *clp, *new = NULL;
166
167 spin_lock(&state_spinlock);
168 for (;;) {
169 clp = __nfs4_find_client(addr);
170 if (clp != NULL)
171 break;
172 clp = new;
173 if (clp != NULL) {
174 list_add(&clp->cl_servers, &nfs4_clientid_list);
175 new = NULL;
176 break;
177 }
178 spin_unlock(&state_spinlock);
179 new = nfs4_alloc_client(addr);
180 spin_lock(&state_spinlock);
181 if (new == NULL)
182 break;
183 }
184 spin_unlock(&state_spinlock);
185 if (new)
186 nfs4_free_client(new);
187 return clp;
188}
189
190void
191nfs4_put_client(struct nfs4_client *clp)
192{
193 if (!atomic_dec_and_lock(&clp->cl_count, &state_spinlock))
194 return;
195 list_del(&clp->cl_servers);
196 spin_unlock(&state_spinlock);
197 BUG_ON(!list_empty(&clp->cl_superblocks));
198 wake_up_all(&clp->cl_waitq);
199 rpc_wake_up(&clp->cl_rpcwaitq);
200 nfs4_kill_renewd(clp);
201 nfs4_free_client(clp);
202}
203
204static int __nfs4_init_client(struct nfs4_client *clp)
205{
206 int status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, nfs_callback_tcpport);
207 if (status == 0)
208 status = nfs4_proc_setclientid_confirm(clp);
209 if (status == 0)
210 nfs4_schedule_state_renewal(clp);
211 return status;
212}
213
214int nfs4_init_client(struct nfs4_client *clp)
215{
216 return nfs4_map_errors(__nfs4_init_client(clp));
217}
218
219u32
220nfs4_alloc_lockowner_id(struct nfs4_client *clp)
221{
222 return clp->cl_lockowner_id ++;
223}
224
225static struct nfs4_state_owner *
226nfs4_client_grab_unused(struct nfs4_client *clp, struct rpc_cred *cred)
227{
228 struct nfs4_state_owner *sp = NULL;
229
230 if (!list_empty(&clp->cl_unused)) {
231 sp = list_entry(clp->cl_unused.next, struct nfs4_state_owner, so_list);
232 atomic_inc(&sp->so_count);
233 sp->so_cred = cred;
234 list_move(&sp->so_list, &clp->cl_state_owners);
235 clp->cl_nunused--;
236 }
237 return sp;
238}
239
240static struct nfs4_state_owner *
241nfs4_find_state_owner(struct nfs4_client *clp, struct rpc_cred *cred)
242{
243 struct nfs4_state_owner *sp, *res = NULL;
244
245 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
246 if (sp->so_cred != cred)
247 continue;
248 atomic_inc(&sp->so_count);
249 /* Move to the head of the list */
250 list_move(&sp->so_list, &clp->cl_state_owners);
251 res = sp;
252 break;
253 }
254 return res;
255}
256
257/*
258 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
259 * create a new state_owner.
260 *
261 */
262static struct nfs4_state_owner *
263nfs4_alloc_state_owner(void)
264{
265 struct nfs4_state_owner *sp;
266
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700267 sp = kzalloc(sizeof(*sp),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (!sp)
269 return NULL;
270 init_MUTEX(&sp->so_sema);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 INIT_LIST_HEAD(&sp->so_states);
272 INIT_LIST_HEAD(&sp->so_delegations);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700273 rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
274 sp->so_seqid.sequence = &sp->so_sequence;
275 spin_lock_init(&sp->so_sequence.lock);
276 INIT_LIST_HEAD(&sp->so_sequence.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 atomic_set(&sp->so_count, 1);
278 return sp;
279}
280
281void
282nfs4_drop_state_owner(struct nfs4_state_owner *sp)
283{
284 struct nfs4_client *clp = sp->so_client;
285 spin_lock(&clp->cl_lock);
286 list_del_init(&sp->so_list);
287 spin_unlock(&clp->cl_lock);
288}
289
290/*
291 * Note: must be called with clp->cl_sem held in order to prevent races
292 * with reboot recovery!
293 */
294struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, struct rpc_cred *cred)
295{
296 struct nfs4_client *clp = server->nfs4_state;
297 struct nfs4_state_owner *sp, *new;
298
299 get_rpccred(cred);
300 new = nfs4_alloc_state_owner();
301 spin_lock(&clp->cl_lock);
302 sp = nfs4_find_state_owner(clp, cred);
303 if (sp == NULL)
304 sp = nfs4_client_grab_unused(clp, cred);
305 if (sp == NULL && new != NULL) {
306 list_add(&new->so_list, &clp->cl_state_owners);
307 new->so_client = clp;
308 new->so_id = nfs4_alloc_lockowner_id(clp);
309 new->so_cred = cred;
310 sp = new;
311 new = NULL;
312 }
313 spin_unlock(&clp->cl_lock);
314 if (new)
315 kfree(new);
316 if (sp != NULL)
317 return sp;
318 put_rpccred(cred);
319 return NULL;
320}
321
322/*
323 * Must be called with clp->cl_sem held in order to avoid races
324 * with state recovery...
325 */
326void nfs4_put_state_owner(struct nfs4_state_owner *sp)
327{
328 struct nfs4_client *clp = sp->so_client;
329 struct rpc_cred *cred = sp->so_cred;
330
331 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
332 return;
333 if (clp->cl_nunused >= OPENOWNER_POOL_SIZE)
334 goto out_free;
335 if (list_empty(&sp->so_list))
336 goto out_free;
337 list_move(&sp->so_list, &clp->cl_unused);
338 clp->cl_nunused++;
339 spin_unlock(&clp->cl_lock);
340 put_rpccred(cred);
341 cred = NULL;
342 return;
343out_free:
344 list_del(&sp->so_list);
345 spin_unlock(&clp->cl_lock);
346 put_rpccred(cred);
347 kfree(sp);
348}
349
350static struct nfs4_state *
351nfs4_alloc_open_state(void)
352{
353 struct nfs4_state *state;
354
355 state = kmalloc(sizeof(*state), GFP_KERNEL);
356 if (!state)
357 return NULL;
358 state->state = 0;
359 state->nreaders = 0;
360 state->nwriters = 0;
361 state->flags = 0;
362 memset(state->stateid.data, 0, sizeof(state->stateid.data));
363 atomic_set(&state->count, 1);
364 INIT_LIST_HEAD(&state->lock_states);
365 init_MUTEX(&state->lock_sema);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000366 spin_lock_init(&state->state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return state;
368}
369
370static struct nfs4_state *
371__nfs4_find_state(struct inode *inode, struct rpc_cred *cred, mode_t mode)
372{
373 struct nfs_inode *nfsi = NFS_I(inode);
374 struct nfs4_state *state;
375
376 mode &= (FMODE_READ|FMODE_WRITE);
377 list_for_each_entry(state, &nfsi->open_states, inode_states) {
378 if (state->owner->so_cred != cred)
379 continue;
380 if ((mode & FMODE_READ) != 0 && state->nreaders == 0)
381 continue;
382 if ((mode & FMODE_WRITE) != 0 && state->nwriters == 0)
383 continue;
384 if ((state->state & mode) != mode)
385 continue;
386 atomic_inc(&state->count);
387 if (mode & FMODE_READ)
388 state->nreaders++;
389 if (mode & FMODE_WRITE)
390 state->nwriters++;
391 return state;
392 }
393 return NULL;
394}
395
396static struct nfs4_state *
397__nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
398{
399 struct nfs_inode *nfsi = NFS_I(inode);
400 struct nfs4_state *state;
401
402 list_for_each_entry(state, &nfsi->open_states, inode_states) {
403 /* Is this in the process of being freed? */
404 if (state->nreaders == 0 && state->nwriters == 0)
405 continue;
406 if (state->owner == owner) {
407 atomic_inc(&state->count);
408 return state;
409 }
410 }
411 return NULL;
412}
413
414struct nfs4_state *
415nfs4_find_state(struct inode *inode, struct rpc_cred *cred, mode_t mode)
416{
417 struct nfs4_state *state;
418
419 spin_lock(&inode->i_lock);
420 state = __nfs4_find_state(inode, cred, mode);
421 spin_unlock(&inode->i_lock);
422 return state;
423}
424
425static void
426nfs4_free_open_state(struct nfs4_state *state)
427{
428 kfree(state);
429}
430
431struct nfs4_state *
432nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
433{
434 struct nfs4_state *state, *new;
435 struct nfs_inode *nfsi = NFS_I(inode);
436
437 spin_lock(&inode->i_lock);
438 state = __nfs4_find_state_byowner(inode, owner);
439 spin_unlock(&inode->i_lock);
440 if (state)
441 goto out;
442 new = nfs4_alloc_open_state();
443 spin_lock(&inode->i_lock);
444 state = __nfs4_find_state_byowner(inode, owner);
445 if (state == NULL && new != NULL) {
446 state = new;
447 /* Caller *must* be holding owner->so_sem */
448 /* Note: The reclaim code dictates that we add stateless
449 * and read-only stateids to the end of the list */
450 list_add_tail(&state->open_states, &owner->so_states);
451 state->owner = owner;
452 atomic_inc(&owner->so_count);
453 list_add(&state->inode_states, &nfsi->open_states);
454 state->inode = igrab(inode);
455 spin_unlock(&inode->i_lock);
456 } else {
457 spin_unlock(&inode->i_lock);
458 if (new)
459 nfs4_free_open_state(new);
460 }
461out:
462 return state;
463}
464
465/*
466 * Beware! Caller must be holding exactly one
467 * reference to clp->cl_sem and owner->so_sema!
468 */
469void nfs4_put_open_state(struct nfs4_state *state)
470{
471 struct inode *inode = state->inode;
472 struct nfs4_state_owner *owner = state->owner;
473
474 if (!atomic_dec_and_lock(&state->count, &inode->i_lock))
475 return;
476 if (!list_empty(&state->inode_states))
477 list_del(&state->inode_states);
478 spin_unlock(&inode->i_lock);
479 list_del(&state->open_states);
480 iput(inode);
481 BUG_ON (state->state != 0);
482 nfs4_free_open_state(state);
483 nfs4_put_state_owner(owner);
484}
485
486/*
487 * Beware! Caller must be holding no references to clp->cl_sem!
488 * of owner->so_sema!
489 */
490void nfs4_close_state(struct nfs4_state *state, mode_t mode)
491{
492 struct inode *inode = state->inode;
493 struct nfs4_state_owner *owner = state->owner;
494 struct nfs4_client *clp = owner->so_client;
495 int newstate;
496
497 atomic_inc(&owner->so_count);
498 down_read(&clp->cl_sem);
499 down(&owner->so_sema);
500 /* Protect against nfs4_find_state() */
501 spin_lock(&inode->i_lock);
502 if (mode & FMODE_READ)
503 state->nreaders--;
504 if (mode & FMODE_WRITE)
505 state->nwriters--;
506 if (state->nwriters == 0) {
507 if (state->nreaders == 0)
508 list_del_init(&state->inode_states);
509 /* See reclaim code */
510 list_move_tail(&state->open_states, &owner->so_states);
511 }
512 spin_unlock(&inode->i_lock);
513 newstate = 0;
514 if (state->state != 0) {
515 if (state->nreaders)
516 newstate |= FMODE_READ;
517 if (state->nwriters)
518 newstate |= FMODE_WRITE;
519 if (state->state == newstate)
520 goto out;
521 if (nfs4_do_close(inode, state, newstate) == -EINPROGRESS)
522 return;
523 }
524out:
525 nfs4_put_open_state(state);
526 up(&owner->so_sema);
527 nfs4_put_state_owner(owner);
528 up_read(&clp->cl_sem);
529}
530
531/*
532 * Search the state->lock_states for an existing lock_owner
533 * that is compatible with current->files
534 */
535static struct nfs4_lock_state *
536__nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
537{
538 struct nfs4_lock_state *pos;
539 list_for_each_entry(pos, &state->lock_states, ls_locks) {
540 if (pos->ls_owner != fl_owner)
541 continue;
542 atomic_inc(&pos->ls_count);
543 return pos;
544 }
545 return NULL;
546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548/*
549 * Return a compatible lock_state. If no initialized lock_state structure
550 * exists, return an uninitialized one.
551 *
552 * The caller must be holding state->lock_sema
553 */
554static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
555{
556 struct nfs4_lock_state *lsp;
557 struct nfs4_client *clp = state->owner->so_client;
558
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700559 lsp = kzalloc(sizeof(*lsp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (lsp == NULL)
561 return NULL;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700562 lsp->ls_seqid.sequence = &state->owner->so_sequence;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 atomic_set(&lsp->ls_count, 1);
564 lsp->ls_owner = fl_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 spin_lock(&clp->cl_lock);
566 lsp->ls_id = nfs4_alloc_lockowner_id(clp);
567 spin_unlock(&clp->cl_lock);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000568 INIT_LIST_HEAD(&lsp->ls_locks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return lsp;
570}
571
572/*
573 * Return a compatible lock_state. If no initialized lock_state structure
574 * exists, return an uninitialized one.
575 *
576 * The caller must be holding state->lock_sema and clp->cl_sem
577 */
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000578static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000580 struct nfs4_lock_state *lsp, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000582 for(;;) {
583 spin_lock(&state->state_lock);
584 lsp = __nfs4_find_lock_state(state, owner);
585 if (lsp != NULL)
586 break;
587 if (new != NULL) {
588 new->ls_state = state;
589 list_add(&new->ls_locks, &state->lock_states);
590 set_bit(LK_STATE_IN_USE, &state->flags);
591 lsp = new;
592 new = NULL;
593 break;
594 }
595 spin_unlock(&state->state_lock);
596 new = nfs4_alloc_lock_state(state, owner);
597 if (new == NULL)
598 return NULL;
599 }
600 spin_unlock(&state->state_lock);
601 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return lsp;
603}
604
605/*
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000606 * Release reference to lock_state, and free it if we see that
607 * it is no longer in use
608 */
609static void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
610{
611 struct nfs4_state *state;
612
613 if (lsp == NULL)
614 return;
615 state = lsp->ls_state;
616 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
617 return;
618 list_del(&lsp->ls_locks);
619 if (list_empty(&state->lock_states))
620 clear_bit(LK_STATE_IN_USE, &state->flags);
621 spin_unlock(&state->state_lock);
622 kfree(lsp);
623}
624
625static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
626{
627 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
628
629 dst->fl_u.nfs4_fl.owner = lsp;
630 atomic_inc(&lsp->ls_count);
631}
632
633static void nfs4_fl_release_lock(struct file_lock *fl)
634{
635 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
636}
637
638static struct file_lock_operations nfs4_fl_lock_ops = {
639 .fl_copy_lock = nfs4_fl_copy_lock,
640 .fl_release_private = nfs4_fl_release_lock,
641};
642
643int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
644{
645 struct nfs4_lock_state *lsp;
646
647 if (fl->fl_ops != NULL)
648 return 0;
649 lsp = nfs4_get_lock_state(state, fl->fl_owner);
650 if (lsp == NULL)
651 return -ENOMEM;
652 fl->fl_u.nfs4_fl.owner = lsp;
653 fl->fl_ops = &nfs4_fl_lock_ops;
654 return 0;
655}
656
657/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 * Byte-range lock aware utility to initialize the stateid of read/write
659 * requests.
660 */
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000661void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000663 struct nfs4_lock_state *lsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 memcpy(dst, &state->stateid, sizeof(*dst));
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 rpc_sequence *sequence = counter->sequence;
680 struct nfs_seqid *new;
681
682 new = kmalloc(sizeof(*new), GFP_KERNEL);
683 if (new != NULL) {
684 new->sequence = counter;
685 new->task = NULL;
686 spin_lock(&sequence->lock);
687 list_add_tail(&new->list, &sequence->list);
688 spin_unlock(&sequence->lock);
689 }
690 return new;
691}
692
693void nfs_free_seqid(struct nfs_seqid *seqid)
694{
695 struct rpc_sequence *sequence = seqid->sequence->sequence;
696 struct rpc_task *next = NULL;
697
698 spin_lock(&sequence->lock);
699 list_del(&seqid->list);
700 if (!list_empty(&sequence->list)) {
701 next = list_entry(sequence->list.next, struct nfs_seqid, list)->task;
702 if (next)
703 rpc_wake_up_task(next);
704 }
705 spin_unlock(&sequence->lock);
706 kfree(seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700710 * Called with sp->so_sema and clp->cl_sem held.
711 *
712 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
713 * failed with a seqid incrementing error -
714 * see comments nfs_fs.h:seqid_mutating_error()
715 */
716static inline void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700718 switch (status) {
719 case 0:
720 break;
721 case -NFS4ERR_BAD_SEQID:
722 case -NFS4ERR_STALE_CLIENTID:
723 case -NFS4ERR_STALE_STATEID:
724 case -NFS4ERR_BAD_STATEID:
725 case -NFS4ERR_BADXDR:
726 case -NFS4ERR_RESOURCE:
727 case -NFS4ERR_NOFILEHANDLE:
728 /* Non-seqid mutating errors */
729 return;
730 };
731 /*
732 * Note: no locking needed as we are guaranteed to be first
733 * on the sequence list
734 */
735 seqid->sequence->counter++;
736}
737
738void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
739{
740 if (status == -NFS4ERR_BAD_SEQID) {
741 struct nfs4_state_owner *sp = container_of(seqid->sequence,
742 struct nfs4_state_owner, so_seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 nfs4_drop_state_owner(sp);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700744 }
745 return nfs_increment_seqid(status, seqid);
746}
747
748/*
749 * Called with ls->lock_sema and clp->cl_sem held.
750 *
751 * Increment the seqid if the LOCK/LOCKU succeeded, or
752 * failed with a seqid incrementing error -
753 * see comments nfs_fs.h:seqid_mutating_error()
754 */
755void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
756{
757 return nfs_increment_seqid(status, seqid);
758}
759
760int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
761{
762 struct rpc_sequence *sequence = seqid->sequence->sequence;
763 int status = 0;
764
765 spin_lock(&sequence->lock);
766 if (sequence->list.next != &seqid->list) {
767 seqid->task = task;
768 rpc_sleep_on(&sequence->wait, task, NULL, NULL);
769 status = -EAGAIN;
770 }
771 spin_unlock(&sequence->lock);
772 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
775static int reclaimer(void *);
776struct reclaimer_args {
777 struct nfs4_client *clp;
778 struct completion complete;
779};
780
781/*
782 * State recovery routine
783 */
784void
785nfs4_recover_state(void *data)
786{
787 struct nfs4_client *clp = (struct nfs4_client *)data;
788 struct reclaimer_args args = {
789 .clp = clp,
790 };
791 might_sleep();
792
793 init_completion(&args.complete);
794
795 if (kernel_thread(reclaimer, &args, CLONE_KERNEL) < 0)
796 goto out_failed_clear;
797 wait_for_completion(&args.complete);
798 return;
799out_failed_clear:
800 set_bit(NFS4CLNT_OK, &clp->cl_state);
801 wake_up_all(&clp->cl_waitq);
802 rpc_wake_up(&clp->cl_rpcwaitq);
803}
804
805/*
806 * Schedule a state recovery attempt
807 */
808void
809nfs4_schedule_state_recovery(struct nfs4_client *clp)
810{
811 if (!clp)
812 return;
813 if (test_and_clear_bit(NFS4CLNT_OK, &clp->cl_state))
814 schedule_work(&clp->cl_recoverd);
815}
816
817static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops *ops, struct nfs4_state *state)
818{
819 struct inode *inode = state->inode;
820 struct file_lock *fl;
821 int status = 0;
822
823 for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
824 if (!(fl->fl_flags & FL_POSIX))
825 continue;
826 if (((struct nfs_open_context *)fl->fl_file->private_data)->state != state)
827 continue;
828 status = ops->recover_lock(state, fl);
829 if (status >= 0)
830 continue;
831 switch (status) {
832 default:
833 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
834 __FUNCTION__, status);
835 case -NFS4ERR_EXPIRED:
836 case -NFS4ERR_NO_GRACE:
837 case -NFS4ERR_RECLAIM_BAD:
838 case -NFS4ERR_RECLAIM_CONFLICT:
839 /* kill_proc(fl->fl_owner, SIGLOST, 1); */
840 break;
841 case -NFS4ERR_STALE_CLIENTID:
842 goto out_err;
843 }
844 }
845 return 0;
846out_err:
847 return status;
848}
849
850static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops *ops, struct nfs4_state_owner *sp)
851{
852 struct nfs4_state *state;
853 struct nfs4_lock_state *lock;
854 int status = 0;
855
856 /* Note: we rely on the sp->so_states list being ordered
857 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
858 * states first.
859 * This is needed to ensure that the server won't give us any
860 * read delegations that we have to return if, say, we are
861 * recovering after a network partition or a reboot from a
862 * server that doesn't support a grace period.
863 */
864 list_for_each_entry(state, &sp->so_states, open_states) {
865 if (state->state == 0)
866 continue;
867 status = ops->recover_open(sp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (status >= 0) {
869 status = nfs4_reclaim_locks(ops, state);
870 if (status < 0)
871 goto out_err;
872 list_for_each_entry(lock, &state->lock_states, ls_locks) {
873 if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
874 printk("%s: Lock reclaim failed!\n",
875 __FUNCTION__);
876 }
877 continue;
878 }
879 switch (status) {
880 default:
881 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
882 __FUNCTION__, status);
883 case -ENOENT:
884 case -NFS4ERR_RECLAIM_BAD:
885 case -NFS4ERR_RECLAIM_CONFLICT:
886 /*
887 * Open state on this file cannot be recovered
888 * All we can do is revert to using the zero stateid.
889 */
890 memset(state->stateid.data, 0,
891 sizeof(state->stateid.data));
892 /* Mark the file as being 'closed' */
893 state->state = 0;
894 break;
895 case -NFS4ERR_EXPIRED:
896 case -NFS4ERR_NO_GRACE:
897 case -NFS4ERR_STALE_CLIENTID:
898 goto out_err;
899 }
900 }
901 return 0;
902out_err:
903 return status;
904}
905
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700906static void nfs4_state_mark_reclaim(struct nfs4_client *clp)
907{
908 struct nfs4_state_owner *sp;
909 struct nfs4_state *state;
910 struct nfs4_lock_state *lock;
911
912 /* Reset all sequence ids to zero */
913 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
914 sp->so_seqid.counter = 0;
915 sp->so_seqid.flags = 0;
916 list_for_each_entry(state, &sp->so_states, open_states) {
917 list_for_each_entry(lock, &state->lock_states, ls_locks) {
918 lock->ls_seqid.counter = 0;
919 lock->ls_seqid.flags = 0;
920 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
921 }
922 }
923 }
924}
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926static int reclaimer(void *ptr)
927{
928 struct reclaimer_args *args = (struct reclaimer_args *)ptr;
929 struct nfs4_client *clp = args->clp;
930 struct nfs4_state_owner *sp;
931 struct nfs4_state_recovery_ops *ops;
932 int status = 0;
933
934 daemonize("%u.%u.%u.%u-reclaim", NIPQUAD(clp->cl_addr));
935 allow_signal(SIGKILL);
936
937 atomic_inc(&clp->cl_count);
938 complete(&args->complete);
939
940 /* Ensure exclusive access to NFSv4 state */
941 lock_kernel();
942 down_write(&clp->cl_sem);
943 /* Are there any NFS mounts out there? */
944 if (list_empty(&clp->cl_superblocks))
945 goto out;
946restart_loop:
947 status = nfs4_proc_renew(clp);
948 switch (status) {
949 case 0:
950 case -NFS4ERR_CB_PATH_DOWN:
951 goto out;
952 case -NFS4ERR_STALE_CLIENTID:
953 case -NFS4ERR_LEASE_MOVED:
954 ops = &nfs4_reboot_recovery_ops;
955 break;
956 default:
957 ops = &nfs4_network_partition_recovery_ops;
958 };
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700959 nfs4_state_mark_reclaim(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 status = __nfs4_init_client(clp);
961 if (status)
962 goto out_error;
963 /* Mark all delegations for reclaim */
964 nfs_delegation_mark_reclaim(clp);
965 /* Note: list is protected by exclusive lock on cl->cl_sem */
966 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
967 status = nfs4_reclaim_open_state(ops, sp);
968 if (status < 0) {
969 if (status == -NFS4ERR_NO_GRACE) {
970 ops = &nfs4_network_partition_recovery_ops;
971 status = nfs4_reclaim_open_state(ops, sp);
972 }
973 if (status == -NFS4ERR_STALE_CLIENTID)
974 goto restart_loop;
975 if (status == -NFS4ERR_EXPIRED)
976 goto restart_loop;
977 }
978 }
979 nfs_delegation_reap_unclaimed(clp);
980out:
981 set_bit(NFS4CLNT_OK, &clp->cl_state);
982 up_write(&clp->cl_sem);
983 unlock_kernel();
984 wake_up_all(&clp->cl_waitq);
985 rpc_wake_up(&clp->cl_rpcwaitq);
986 if (status == -NFS4ERR_CB_PATH_DOWN)
987 nfs_handle_cb_pathdown(clp);
988 nfs4_put_client(clp);
989 return 0;
990out_error:
991 printk(KERN_WARNING "Error: state recovery failed on NFSv4 server %u.%u.%u.%u with error %d\n",
992 NIPQUAD(clp->cl_addr.s_addr), -status);
993 goto out;
994}
995
996/*
997 * Local variables:
998 * c-basic-offset: 8
999 * End:
1000 */