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