NFS: Fix put_nfs_open_context
[deliverable/linux.git] / fs / nfs / nfs4state.c
... / ...
CommitLineData
1/*
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/kernel.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/kthread.h>
47#include <linux/module.h>
48#include <linux/random.h>
49#include <linux/workqueue.h>
50#include <linux/bitops.h>
51
52#include "nfs4_fs.h"
53#include "callback.h"
54#include "delegation.h"
55#include "internal.h"
56
57#define OPENOWNER_POOL_SIZE 8
58
59const nfs4_stateid zero_stateid;
60
61static LIST_HEAD(nfs4_clientid_list);
62
63static int nfs4_init_client(struct nfs_client *clp, struct rpc_cred *cred)
64{
65 int status = nfs4_proc_setclientid(clp, NFS4_CALLBACK,
66 nfs_callback_tcpport, cred);
67 if (status == 0)
68 status = nfs4_proc_setclientid_confirm(clp, cred);
69 if (status == 0)
70 nfs4_schedule_state_renewal(clp);
71 return status;
72}
73
74struct rpc_cred *nfs4_get_renew_cred(struct nfs_client *clp)
75{
76 struct nfs4_state_owner *sp;
77 struct rb_node *pos;
78 struct rpc_cred *cred = NULL;
79
80 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);
82 if (list_empty(&sp->so_states))
83 continue;
84 cred = get_rpccred(sp->so_cred);
85 break;
86 }
87 return cred;
88}
89
90static struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
91{
92 struct nfs4_state_owner *sp;
93 struct rb_node *pos;
94
95 pos = rb_first(&clp->cl_state_owners);
96 if (pos != NULL) {
97 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
98 return get_rpccred(sp->so_cred);
99 }
100 return NULL;
101}
102
103static 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
158static struct nfs4_state_owner *
159nfs4_find_state_owner(struct nfs_server *server, struct rpc_cred *cred)
160{
161 struct nfs_client *clp = server->nfs_client;
162 struct rb_node **p = &clp->cl_state_owners.rb_node,
163 *parent = NULL;
164 struct nfs4_state_owner *sp, *res = NULL;
165
166 while (*p != NULL) {
167 parent = *p;
168 sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
169
170 if (server < sp->so_server) {
171 p = &parent->rb_left;
172 continue;
173 }
174 if (server > sp->so_server) {
175 p = &parent->rb_right;
176 continue;
177 }
178 if (cred < sp->so_cred)
179 p = &parent->rb_left;
180 else if (cred > sp->so_cred)
181 p = &parent->rb_right;
182 else {
183 atomic_inc(&sp->so_count);
184 res = sp;
185 break;
186 }
187 }
188 return res;
189}
190
191static struct nfs4_state_owner *
192nfs4_insert_state_owner(struct nfs_client *clp, struct nfs4_state_owner *new)
193{
194 struct rb_node **p = &clp->cl_state_owners.rb_node,
195 *parent = NULL;
196 struct nfs4_state_owner *sp;
197
198 while (*p != NULL) {
199 parent = *p;
200 sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
201
202 if (new->so_server < sp->so_server) {
203 p = &parent->rb_left;
204 continue;
205 }
206 if (new->so_server > sp->so_server) {
207 p = &parent->rb_right;
208 continue;
209 }
210 if (new->so_cred < sp->so_cred)
211 p = &parent->rb_left;
212 else if (new->so_cred > sp->so_cred)
213 p = &parent->rb_right;
214 else {
215 atomic_inc(&sp->so_count);
216 return sp;
217 }
218 }
219 nfs_alloc_unique_id(&clp->cl_openowner_id, &new->so_owner_id, 1, 64);
220 rb_link_node(&new->so_client_node, parent, p);
221 rb_insert_color(&new->so_client_node, &clp->cl_state_owners);
222 return new;
223}
224
225static void
226nfs4_remove_state_owner(struct nfs_client *clp, struct nfs4_state_owner *sp)
227{
228 if (!RB_EMPTY_NODE(&sp->so_client_node))
229 rb_erase(&sp->so_client_node, &clp->cl_state_owners);
230 nfs_free_unique_id(&clp->cl_openowner_id, &sp->so_owner_id);
231}
232
233/*
234 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
235 * create a new state_owner.
236 *
237 */
238static struct nfs4_state_owner *
239nfs4_alloc_state_owner(void)
240{
241 struct nfs4_state_owner *sp;
242
243 sp = kzalloc(sizeof(*sp),GFP_KERNEL);
244 if (!sp)
245 return NULL;
246 spin_lock_init(&sp->so_lock);
247 INIT_LIST_HEAD(&sp->so_states);
248 INIT_LIST_HEAD(&sp->so_delegations);
249 rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
250 sp->so_seqid.sequence = &sp->so_sequence;
251 spin_lock_init(&sp->so_sequence.lock);
252 INIT_LIST_HEAD(&sp->so_sequence.list);
253 atomic_set(&sp->so_count, 1);
254 return sp;
255}
256
257void
258nfs4_drop_state_owner(struct nfs4_state_owner *sp)
259{
260 if (!RB_EMPTY_NODE(&sp->so_client_node)) {
261 struct nfs_client *clp = sp->so_client;
262
263 spin_lock(&clp->cl_lock);
264 rb_erase(&sp->so_client_node, &clp->cl_state_owners);
265 RB_CLEAR_NODE(&sp->so_client_node);
266 spin_unlock(&clp->cl_lock);
267 }
268}
269
270/*
271 * Note: must be called with clp->cl_sem held in order to prevent races
272 * with reboot recovery!
273 */
274struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, struct rpc_cred *cred)
275{
276 struct nfs_client *clp = server->nfs_client;
277 struct nfs4_state_owner *sp, *new;
278
279 spin_lock(&clp->cl_lock);
280 sp = nfs4_find_state_owner(server, cred);
281 spin_unlock(&clp->cl_lock);
282 if (sp != NULL)
283 return sp;
284 new = nfs4_alloc_state_owner();
285 if (new == NULL)
286 return NULL;
287 new->so_client = clp;
288 new->so_server = server;
289 new->so_cred = cred;
290 spin_lock(&clp->cl_lock);
291 sp = nfs4_insert_state_owner(clp, new);
292 spin_unlock(&clp->cl_lock);
293 if (sp == new)
294 get_rpccred(cred);
295 else
296 kfree(new);
297 return sp;
298}
299
300/*
301 * Must be called with clp->cl_sem held in order to avoid races
302 * with state recovery...
303 */
304void nfs4_put_state_owner(struct nfs4_state_owner *sp)
305{
306 struct nfs_client *clp = sp->so_client;
307 struct rpc_cred *cred = sp->so_cred;
308
309 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
310 return;
311 nfs4_remove_state_owner(clp, sp);
312 spin_unlock(&clp->cl_lock);
313 put_rpccred(cred);
314 kfree(sp);
315}
316
317static struct nfs4_state *
318nfs4_alloc_open_state(void)
319{
320 struct nfs4_state *state;
321
322 state = kzalloc(sizeof(*state), GFP_KERNEL);
323 if (!state)
324 return NULL;
325 atomic_set(&state->count, 1);
326 INIT_LIST_HEAD(&state->lock_states);
327 spin_lock_init(&state->state_lock);
328 seqlock_init(&state->seqlock);
329 return state;
330}
331
332void
333nfs4_state_set_mode_locked(struct nfs4_state *state, mode_t mode)
334{
335 if (state->state == mode)
336 return;
337 /* NB! List reordering - see the reclaim code for why. */
338 if ((mode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
339 if (mode & FMODE_WRITE)
340 list_move(&state->open_states, &state->owner->so_states);
341 else
342 list_move_tail(&state->open_states, &state->owner->so_states);
343 }
344 if (mode == 0)
345 list_del_init(&state->inode_states);
346 state->state = mode;
347}
348
349static struct nfs4_state *
350__nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
351{
352 struct nfs_inode *nfsi = NFS_I(inode);
353 struct nfs4_state *state;
354
355 list_for_each_entry(state, &nfsi->open_states, inode_states) {
356 if (state->owner != owner)
357 continue;
358 if (atomic_inc_not_zero(&state->count))
359 return state;
360 }
361 return NULL;
362}
363
364static void
365nfs4_free_open_state(struct nfs4_state *state)
366{
367 kfree(state);
368}
369
370struct nfs4_state *
371nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
372{
373 struct nfs4_state *state, *new;
374 struct nfs_inode *nfsi = NFS_I(inode);
375
376 spin_lock(&inode->i_lock);
377 state = __nfs4_find_state_byowner(inode, owner);
378 spin_unlock(&inode->i_lock);
379 if (state)
380 goto out;
381 new = nfs4_alloc_open_state();
382 spin_lock(&owner->so_lock);
383 spin_lock(&inode->i_lock);
384 state = __nfs4_find_state_byowner(inode, owner);
385 if (state == NULL && new != NULL) {
386 state = new;
387 state->owner = owner;
388 atomic_inc(&owner->so_count);
389 list_add(&state->inode_states, &nfsi->open_states);
390 state->inode = igrab(inode);
391 spin_unlock(&inode->i_lock);
392 /* Note: The reclaim code dictates that we add stateless
393 * and read-only stateids to the end of the list */
394 list_add_tail(&state->open_states, &owner->so_states);
395 spin_unlock(&owner->so_lock);
396 } else {
397 spin_unlock(&inode->i_lock);
398 spin_unlock(&owner->so_lock);
399 if (new)
400 nfs4_free_open_state(new);
401 }
402out:
403 return state;
404}
405
406/*
407 * Beware! Caller must be holding exactly one
408 * reference to clp->cl_sem!
409 */
410void nfs4_put_open_state(struct nfs4_state *state)
411{
412 struct inode *inode = state->inode;
413 struct nfs4_state_owner *owner = state->owner;
414
415 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
416 return;
417 spin_lock(&inode->i_lock);
418 if (!list_empty(&state->inode_states))
419 list_del(&state->inode_states);
420 list_del(&state->open_states);
421 spin_unlock(&inode->i_lock);
422 spin_unlock(&owner->so_lock);
423 iput(inode);
424 nfs4_free_open_state(state);
425 nfs4_put_state_owner(owner);
426}
427
428/*
429 * Close the current file.
430 */
431void nfs4_close_state(struct path *path, struct nfs4_state *state, mode_t mode)
432{
433 struct nfs4_state_owner *owner = state->owner;
434 int call_close = 0;
435 int newstate;
436
437 atomic_inc(&owner->so_count);
438 /* Protect against nfs4_find_state() */
439 spin_lock(&owner->so_lock);
440 switch (mode & (FMODE_READ | FMODE_WRITE)) {
441 case FMODE_READ:
442 state->n_rdonly--;
443 break;
444 case FMODE_WRITE:
445 state->n_wronly--;
446 break;
447 case FMODE_READ|FMODE_WRITE:
448 state->n_rdwr--;
449 }
450 newstate = FMODE_READ|FMODE_WRITE;
451 if (state->n_rdwr == 0) {
452 if (state->n_rdonly == 0) {
453 newstate &= ~FMODE_READ;
454 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
455 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
456 }
457 if (state->n_wronly == 0) {
458 newstate &= ~FMODE_WRITE;
459 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
460 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
461 }
462 if (newstate == 0)
463 clear_bit(NFS_DELEGATED_STATE, &state->flags);
464 }
465 nfs4_state_set_mode_locked(state, newstate);
466 spin_unlock(&owner->so_lock);
467
468 if (!call_close) {
469 nfs4_put_open_state(state);
470 nfs4_put_state_owner(owner);
471 } else
472 nfs4_do_close(path, state);
473}
474
475/*
476 * Search the state->lock_states for an existing lock_owner
477 * that is compatible with current->files
478 */
479static struct nfs4_lock_state *
480__nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
481{
482 struct nfs4_lock_state *pos;
483 list_for_each_entry(pos, &state->lock_states, ls_locks) {
484 if (pos->ls_owner != fl_owner)
485 continue;
486 atomic_inc(&pos->ls_count);
487 return pos;
488 }
489 return NULL;
490}
491
492/*
493 * Return a compatible lock_state. If no initialized lock_state structure
494 * exists, return an uninitialized one.
495 *
496 */
497static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
498{
499 struct nfs4_lock_state *lsp;
500 struct nfs_client *clp = state->owner->so_client;
501
502 lsp = kzalloc(sizeof(*lsp), GFP_KERNEL);
503 if (lsp == NULL)
504 return NULL;
505 lsp->ls_seqid.sequence = &state->owner->so_sequence;
506 atomic_set(&lsp->ls_count, 1);
507 lsp->ls_owner = fl_owner;
508 spin_lock(&clp->cl_lock);
509 nfs_alloc_unique_id(&clp->cl_lockowner_id, &lsp->ls_id, 1, 64);
510 spin_unlock(&clp->cl_lock);
511 INIT_LIST_HEAD(&lsp->ls_locks);
512 return lsp;
513}
514
515static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
516{
517 struct nfs_client *clp = lsp->ls_state->owner->so_client;
518
519 spin_lock(&clp->cl_lock);
520 nfs_free_unique_id(&clp->cl_lockowner_id, &lsp->ls_id);
521 spin_unlock(&clp->cl_lock);
522 kfree(lsp);
523}
524
525/*
526 * Return a compatible lock_state. If no initialized lock_state structure
527 * exists, return an uninitialized one.
528 *
529 * The caller must be holding clp->cl_sem
530 */
531static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
532{
533 struct nfs4_lock_state *lsp, *new = NULL;
534
535 for(;;) {
536 spin_lock(&state->state_lock);
537 lsp = __nfs4_find_lock_state(state, owner);
538 if (lsp != NULL)
539 break;
540 if (new != NULL) {
541 new->ls_state = state;
542 list_add(&new->ls_locks, &state->lock_states);
543 set_bit(LK_STATE_IN_USE, &state->flags);
544 lsp = new;
545 new = NULL;
546 break;
547 }
548 spin_unlock(&state->state_lock);
549 new = nfs4_alloc_lock_state(state, owner);
550 if (new == NULL)
551 return NULL;
552 }
553 spin_unlock(&state->state_lock);
554 if (new != NULL)
555 nfs4_free_lock_state(new);
556 return lsp;
557}
558
559/*
560 * Release reference to lock_state, and free it if we see that
561 * it is no longer in use
562 */
563void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
564{
565 struct nfs4_state *state;
566
567 if (lsp == NULL)
568 return;
569 state = lsp->ls_state;
570 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
571 return;
572 list_del(&lsp->ls_locks);
573 if (list_empty(&state->lock_states))
574 clear_bit(LK_STATE_IN_USE, &state->flags);
575 spin_unlock(&state->state_lock);
576 nfs4_free_lock_state(lsp);
577}
578
579static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
580{
581 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
582
583 dst->fl_u.nfs4_fl.owner = lsp;
584 atomic_inc(&lsp->ls_count);
585}
586
587static void nfs4_fl_release_lock(struct file_lock *fl)
588{
589 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
590}
591
592static struct file_lock_operations nfs4_fl_lock_ops = {
593 .fl_copy_lock = nfs4_fl_copy_lock,
594 .fl_release_private = nfs4_fl_release_lock,
595};
596
597int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
598{
599 struct nfs4_lock_state *lsp;
600
601 if (fl->fl_ops != NULL)
602 return 0;
603 lsp = nfs4_get_lock_state(state, fl->fl_owner);
604 if (lsp == NULL)
605 return -ENOMEM;
606 fl->fl_u.nfs4_fl.owner = lsp;
607 fl->fl_ops = &nfs4_fl_lock_ops;
608 return 0;
609}
610
611/*
612 * Byte-range lock aware utility to initialize the stateid of read/write
613 * requests.
614 */
615void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
616{
617 struct nfs4_lock_state *lsp;
618 int seq;
619
620 do {
621 seq = read_seqbegin(&state->seqlock);
622 memcpy(dst, &state->stateid, sizeof(*dst));
623 } while (read_seqretry(&state->seqlock, seq));
624 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
625 return;
626
627 spin_lock(&state->state_lock);
628 lsp = __nfs4_find_lock_state(state, fl_owner);
629 if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
630 memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
631 spin_unlock(&state->state_lock);
632 nfs4_put_lock_state(lsp);
633}
634
635struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter)
636{
637 struct rpc_sequence *sequence = counter->sequence;
638 struct nfs_seqid *new;
639
640 new = kmalloc(sizeof(*new), GFP_KERNEL);
641 if (new != NULL) {
642 new->sequence = counter;
643 spin_lock(&sequence->lock);
644 list_add_tail(&new->list, &sequence->list);
645 spin_unlock(&sequence->lock);
646 }
647 return new;
648}
649
650void nfs_free_seqid(struct nfs_seqid *seqid)
651{
652 struct rpc_sequence *sequence = seqid->sequence->sequence;
653
654 spin_lock(&sequence->lock);
655 list_del(&seqid->list);
656 spin_unlock(&sequence->lock);
657 rpc_wake_up(&sequence->wait);
658 kfree(seqid);
659}
660
661/*
662 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
663 * failed with a seqid incrementing error -
664 * see comments nfs_fs.h:seqid_mutating_error()
665 */
666static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
667{
668 switch (status) {
669 case 0:
670 break;
671 case -NFS4ERR_BAD_SEQID:
672 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
673 return;
674 printk(KERN_WARNING "NFS: v4 server returned a bad"
675 "sequence-id error on an"
676 "unconfirmed sequence %p!\n",
677 seqid->sequence);
678 case -NFS4ERR_STALE_CLIENTID:
679 case -NFS4ERR_STALE_STATEID:
680 case -NFS4ERR_BAD_STATEID:
681 case -NFS4ERR_BADXDR:
682 case -NFS4ERR_RESOURCE:
683 case -NFS4ERR_NOFILEHANDLE:
684 /* Non-seqid mutating errors */
685 return;
686 };
687 /*
688 * Note: no locking needed as we are guaranteed to be first
689 * on the sequence list
690 */
691 seqid->sequence->counter++;
692}
693
694void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
695{
696 if (status == -NFS4ERR_BAD_SEQID) {
697 struct nfs4_state_owner *sp = container_of(seqid->sequence,
698 struct nfs4_state_owner, so_seqid);
699 nfs4_drop_state_owner(sp);
700 }
701 nfs_increment_seqid(status, seqid);
702}
703
704/*
705 * Increment the seqid if the LOCK/LOCKU succeeded, or
706 * failed with a seqid incrementing error -
707 * see comments nfs_fs.h:seqid_mutating_error()
708 */
709void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
710{
711 nfs_increment_seqid(status, seqid);
712}
713
714int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
715{
716 struct rpc_sequence *sequence = seqid->sequence->sequence;
717 int status = 0;
718
719 if (sequence->list.next == &seqid->list)
720 goto out;
721 spin_lock(&sequence->lock);
722 if (sequence->list.next != &seqid->list) {
723 rpc_sleep_on(&sequence->wait, task, NULL, NULL);
724 status = -EAGAIN;
725 }
726 spin_unlock(&sequence->lock);
727out:
728 return status;
729}
730
731static int reclaimer(void *);
732
733static inline void nfs4_clear_recover_bit(struct nfs_client *clp)
734{
735 smp_mb__before_clear_bit();
736 clear_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state);
737 smp_mb__after_clear_bit();
738 wake_up_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER);
739 rpc_wake_up(&clp->cl_rpcwaitq);
740}
741
742/*
743 * State recovery routine
744 */
745static void nfs4_recover_state(struct nfs_client *clp)
746{
747 struct task_struct *task;
748
749 __module_get(THIS_MODULE);
750 atomic_inc(&clp->cl_count);
751 task = kthread_run(reclaimer, clp, "%u.%u.%u.%u-reclaim",
752 NIPQUAD(clp->cl_addr.sin_addr));
753 if (!IS_ERR(task))
754 return;
755 nfs4_clear_recover_bit(clp);
756 nfs_put_client(clp);
757 module_put(THIS_MODULE);
758}
759
760/*
761 * Schedule a state recovery attempt
762 */
763void nfs4_schedule_state_recovery(struct nfs_client *clp)
764{
765 if (!clp)
766 return;
767 if (test_and_set_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
768 nfs4_recover_state(clp);
769}
770
771static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops *ops, struct nfs4_state *state)
772{
773 struct inode *inode = state->inode;
774 struct file_lock *fl;
775 int status = 0;
776
777 for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
778 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
779 continue;
780 if (((struct nfs_open_context *)fl->fl_file->private_data)->state != state)
781 continue;
782 status = ops->recover_lock(state, fl);
783 if (status >= 0)
784 continue;
785 switch (status) {
786 default:
787 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
788 __FUNCTION__, status);
789 case -NFS4ERR_EXPIRED:
790 case -NFS4ERR_NO_GRACE:
791 case -NFS4ERR_RECLAIM_BAD:
792 case -NFS4ERR_RECLAIM_CONFLICT:
793 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
794 break;
795 case -NFS4ERR_STALE_CLIENTID:
796 goto out_err;
797 }
798 }
799 return 0;
800out_err:
801 return status;
802}
803
804static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops *ops, struct nfs4_state_owner *sp)
805{
806 struct nfs4_state *state;
807 struct nfs4_lock_state *lock;
808 int status = 0;
809
810 /* Note: we rely on the sp->so_states list being ordered
811 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
812 * states first.
813 * This is needed to ensure that the server won't give us any
814 * read delegations that we have to return if, say, we are
815 * recovering after a network partition or a reboot from a
816 * server that doesn't support a grace period.
817 */
818 list_for_each_entry(state, &sp->so_states, open_states) {
819 if (state->state == 0)
820 continue;
821 status = ops->recover_open(sp, state);
822 if (status >= 0) {
823 status = nfs4_reclaim_locks(ops, state);
824 if (status < 0)
825 goto out_err;
826 list_for_each_entry(lock, &state->lock_states, ls_locks) {
827 if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
828 printk("%s: Lock reclaim failed!\n",
829 __FUNCTION__);
830 }
831 continue;
832 }
833 switch (status) {
834 default:
835 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
836 __FUNCTION__, status);
837 case -ENOENT:
838 case -NFS4ERR_RECLAIM_BAD:
839 case -NFS4ERR_RECLAIM_CONFLICT:
840 /*
841 * Open state on this file cannot be recovered
842 * All we can do is revert to using the zero stateid.
843 */
844 memset(state->stateid.data, 0,
845 sizeof(state->stateid.data));
846 /* Mark the file as being 'closed' */
847 state->state = 0;
848 break;
849 case -NFS4ERR_EXPIRED:
850 case -NFS4ERR_NO_GRACE:
851 case -NFS4ERR_STALE_CLIENTID:
852 goto out_err;
853 }
854 }
855 return 0;
856out_err:
857 return status;
858}
859
860static void nfs4_state_mark_reclaim(struct nfs_client *clp)
861{
862 struct nfs4_state_owner *sp;
863 struct rb_node *pos;
864 struct nfs4_state *state;
865 struct nfs4_lock_state *lock;
866
867 /* Reset all sequence ids to zero */
868 for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
869 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
870 sp->so_seqid.counter = 0;
871 sp->so_seqid.flags = 0;
872 spin_lock(&sp->so_lock);
873 list_for_each_entry(state, &sp->so_states, open_states) {
874 clear_bit(NFS_DELEGATED_STATE, &state->flags);
875 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
876 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
877 clear_bit(NFS_O_RDWR_STATE, &state->flags);
878 list_for_each_entry(lock, &state->lock_states, ls_locks) {
879 lock->ls_seqid.counter = 0;
880 lock->ls_seqid.flags = 0;
881 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
882 }
883 }
884 spin_unlock(&sp->so_lock);
885 }
886}
887
888static int reclaimer(void *ptr)
889{
890 struct nfs_client *clp = ptr;
891 struct nfs4_state_owner *sp;
892 struct rb_node *pos;
893 struct nfs4_state_recovery_ops *ops;
894 struct rpc_cred *cred;
895 int status = 0;
896
897 allow_signal(SIGKILL);
898
899 /* Ensure exclusive access to NFSv4 state */
900 lock_kernel();
901 down_write(&clp->cl_sem);
902 /* Are there any NFS mounts out there? */
903 if (list_empty(&clp->cl_superblocks))
904 goto out;
905restart_loop:
906 ops = &nfs4_network_partition_recovery_ops;
907 /* Are there any open files on this volume? */
908 cred = nfs4_get_renew_cred(clp);
909 if (cred != NULL) {
910 /* Yes there are: try to renew the old lease */
911 status = nfs4_proc_renew(clp, cred);
912 switch (status) {
913 case 0:
914 case -NFS4ERR_CB_PATH_DOWN:
915 put_rpccred(cred);
916 goto out;
917 case -NFS4ERR_STALE_CLIENTID:
918 case -NFS4ERR_LEASE_MOVED:
919 ops = &nfs4_reboot_recovery_ops;
920 }
921 } else {
922 /* "reboot" to ensure we clear all state on the server */
923 clp->cl_boot_time = CURRENT_TIME;
924 cred = nfs4_get_setclientid_cred(clp);
925 }
926 /* We're going to have to re-establish a clientid */
927 nfs4_state_mark_reclaim(clp);
928 status = -ENOENT;
929 if (cred != NULL) {
930 status = nfs4_init_client(clp, cred);
931 put_rpccred(cred);
932 }
933 if (status)
934 goto out_error;
935 /* Mark all delegations for reclaim */
936 nfs_delegation_mark_reclaim(clp);
937 /* Note: list is protected by exclusive lock on cl->cl_sem */
938 for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
939 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
940 status = nfs4_reclaim_open_state(ops, sp);
941 if (status < 0) {
942 if (status == -NFS4ERR_NO_GRACE) {
943 ops = &nfs4_network_partition_recovery_ops;
944 status = nfs4_reclaim_open_state(ops, sp);
945 }
946 if (status == -NFS4ERR_STALE_CLIENTID)
947 goto restart_loop;
948 if (status == -NFS4ERR_EXPIRED)
949 goto restart_loop;
950 }
951 }
952 nfs_delegation_reap_unclaimed(clp);
953out:
954 up_write(&clp->cl_sem);
955 unlock_kernel();
956 if (status == -NFS4ERR_CB_PATH_DOWN)
957 nfs_handle_cb_pathdown(clp);
958 nfs4_clear_recover_bit(clp);
959 nfs_put_client(clp);
960 module_put_and_exit(0);
961 return 0;
962out_error:
963 printk(KERN_WARNING "Error: state recovery failed on NFSv4 server %u.%u.%u.%u with error %d\n",
964 NIPQUAD(clp->cl_addr.sin_addr), -status);
965 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
966 goto out;
967}
968
969/*
970 * Local variables:
971 * c-basic-offset: 8
972 * End:
973 */
This page took 0.026701 seconds and 5 git commands to generate.