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