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