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