vserver 1.9.3
[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/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 "callback.h"
50 #include "delegation.h"
51
52 #define OPENOWNER_POOL_SIZE     8
53
54 static spinlock_t               state_spinlock = SPIN_LOCK_UNLOCKED;
55
56 nfs4_stateid zero_stateid;
57
58 #if 0
59 nfs4_stateid one_stateid =
60         { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
61 #endif
62
63 static LIST_HEAD(nfs4_clientid_list);
64
65 static void nfs4_recover_state(void *);
66 extern void nfs4_renew_state(void *);
67
68 void
69 init_nfsv4_state(struct nfs_server *server)
70 {
71         server->nfs4_state = NULL;
72         INIT_LIST_HEAD(&server->nfs4_siblings);
73 }
74
75 void
76 destroy_nfsv4_state(struct nfs_server *server)
77 {
78         if (server->mnt_path) {
79                 kfree(server->mnt_path);
80                 server->mnt_path = NULL;
81         }
82         if (server->nfs4_state) {
83                 nfs4_put_client(server->nfs4_state);
84                 server->nfs4_state = NULL;
85         }
86 }
87
88 /*
89  * nfs4_get_client(): returns an empty client structure
90  * nfs4_put_client(): drops reference to client structure
91  *
92  * Since these are allocated/deallocated very rarely, we don't
93  * bother putting them in a slab cache...
94  */
95 static struct nfs4_client *
96 nfs4_alloc_client(struct in_addr *addr)
97 {
98         struct nfs4_client *clp;
99
100         if (nfs_callback_up() < 0)
101                 return NULL;
102         if ((clp = kmalloc(sizeof(*clp), GFP_KERNEL)) == NULL) {
103                 nfs_callback_down();
104                 return NULL;
105         }
106         memset(clp, 0, sizeof(*clp));
107         memcpy(&clp->cl_addr, addr, sizeof(clp->cl_addr));
108         init_rwsem(&clp->cl_sem);
109         INIT_LIST_HEAD(&clp->cl_delegations);
110         INIT_LIST_HEAD(&clp->cl_state_owners);
111         INIT_LIST_HEAD(&clp->cl_unused);
112         spin_lock_init(&clp->cl_lock);
113         atomic_set(&clp->cl_count, 1);
114         INIT_WORK(&clp->cl_recoverd, nfs4_recover_state, clp);
115         INIT_WORK(&clp->cl_renewd, nfs4_renew_state, clp);
116         INIT_LIST_HEAD(&clp->cl_superblocks);
117         init_waitqueue_head(&clp->cl_waitq);
118         rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS4 client");
119         clp->cl_state = 1 << NFS4CLNT_OK;
120         return clp;
121 }
122
123 static void
124 nfs4_free_client(struct nfs4_client *clp)
125 {
126         struct nfs4_state_owner *sp;
127
128         while (!list_empty(&clp->cl_unused)) {
129                 sp = list_entry(clp->cl_unused.next,
130                                 struct nfs4_state_owner,
131                                 so_list);
132                 list_del(&sp->so_list);
133                 kfree(sp);
134         }
135         BUG_ON(!list_empty(&clp->cl_state_owners));
136         if (clp->cl_cred)
137                 put_rpccred(clp->cl_cred);
138         nfs_idmap_delete(clp);
139         if (clp->cl_rpcclient)
140                 rpc_shutdown_client(clp->cl_rpcclient);
141         kfree(clp);
142         nfs_callback_down();
143 }
144
145 static struct nfs4_client *__nfs4_find_client(struct in_addr *addr)
146 {
147         struct nfs4_client *clp;
148         list_for_each_entry(clp, &nfs4_clientid_list, cl_servers) {
149                 if (memcmp(&clp->cl_addr, addr, sizeof(clp->cl_addr)) == 0) {
150                         atomic_inc(&clp->cl_count);
151                         return clp;
152                 }
153         }
154         return NULL;
155 }
156
157 struct nfs4_client *nfs4_find_client(struct in_addr *addr)
158 {
159         struct nfs4_client *clp;
160         spin_lock(&state_spinlock);
161         clp = __nfs4_find_client(addr);
162         spin_unlock(&state_spinlock);
163         return clp;
164 }
165
166 struct nfs4_client *
167 nfs4_get_client(struct in_addr *addr)
168 {
169         struct nfs4_client *clp, *new = NULL;
170
171         spin_lock(&state_spinlock);
172         for (;;) {
173                 clp = __nfs4_find_client(addr);
174                 if (clp != NULL)
175                         break;
176                 clp = new;
177                 if (clp != NULL) {
178                         list_add(&clp->cl_servers, &nfs4_clientid_list);
179                         new = NULL;
180                         break;
181                 }
182                 spin_unlock(&state_spinlock);
183                 new = nfs4_alloc_client(addr);
184                 spin_lock(&state_spinlock);
185                 if (new == NULL)
186                         break;
187         }
188         spin_unlock(&state_spinlock);
189         if (new)
190                 nfs4_free_client(new);
191         return clp;
192 }
193
194 void
195 nfs4_put_client(struct nfs4_client *clp)
196 {
197         if (!atomic_dec_and_lock(&clp->cl_count, &state_spinlock))
198                 return;
199         list_del(&clp->cl_servers);
200         spin_unlock(&state_spinlock);
201         BUG_ON(!list_empty(&clp->cl_superblocks));
202         wake_up_all(&clp->cl_waitq);
203         rpc_wake_up(&clp->cl_rpcwaitq);
204         nfs4_kill_renewd(clp);
205         nfs4_free_client(clp);
206 }
207
208 int nfs4_init_client(struct nfs4_client *clp)
209 {
210         int status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, nfs_callback_tcpport);
211         if (status == 0)
212                 status = nfs4_proc_setclientid_confirm(clp);
213         if (status == 0)
214                 nfs4_schedule_state_renewal(clp);
215         return status;
216 }
217
218 u32
219 nfs4_alloc_lockowner_id(struct nfs4_client *clp)
220 {
221         return clp->cl_lockowner_id ++;
222 }
223
224 static struct nfs4_state_owner *
225 nfs4_client_grab_unused(struct nfs4_client *clp, struct rpc_cred *cred)
226 {
227         struct nfs4_state_owner *sp = NULL;
228
229         if (!list_empty(&clp->cl_unused)) {
230                 sp = list_entry(clp->cl_unused.next, struct nfs4_state_owner, so_list);
231                 atomic_inc(&sp->so_count);
232                 sp->so_cred = cred;
233                 list_move(&sp->so_list, &clp->cl_state_owners);
234                 clp->cl_nunused--;
235         }
236         return sp;
237 }
238
239 static struct nfs4_state_owner *
240 nfs4_find_state_owner(struct nfs4_client *clp, struct rpc_cred *cred)
241 {
242         struct nfs4_state_owner *sp, *res = NULL;
243
244         list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
245                 if (sp->so_cred != cred)
246                         continue;
247                 atomic_inc(&sp->so_count);
248                 /* Move to the head of the list */
249                 list_move(&sp->so_list, &clp->cl_state_owners);
250                 res = sp;
251                 break;
252         }
253         return res;
254 }
255
256 /*
257  * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
258  * create a new state_owner.
259  *
260  */
261 static struct nfs4_state_owner *
262 nfs4_alloc_state_owner(void)
263 {
264         struct nfs4_state_owner *sp;
265
266         sp = kmalloc(sizeof(*sp),GFP_KERNEL);
267         if (!sp)
268                 return NULL;
269         init_MUTEX(&sp->so_sema);
270         sp->so_seqid = 0;                 /* arbitrary */
271         INIT_LIST_HEAD(&sp->so_states);
272         INIT_LIST_HEAD(&sp->so_delegations);
273         atomic_set(&sp->so_count, 1);
274         return sp;
275 }
276
277 static void
278 nfs4_unhash_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         if (new)
311                 kfree(new);
312         if (sp != NULL)
313                 return sp;
314         put_rpccred(cred);
315         return NULL;
316 }
317
318 /*
319  * Must be called with clp->cl_sem held in order to avoid races
320  * with state recovery...
321  */
322 void nfs4_put_state_owner(struct nfs4_state_owner *sp)
323 {
324         struct nfs4_client *clp = sp->so_client;
325         struct rpc_cred *cred = sp->so_cred;
326
327         if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
328                 return;
329         if (clp->cl_nunused >= OPENOWNER_POOL_SIZE)
330                 goto out_free;
331         if (list_empty(&sp->so_list))
332                 goto out_free;
333         list_move(&sp->so_list, &clp->cl_unused);
334         clp->cl_nunused++;
335         spin_unlock(&clp->cl_lock);
336         put_rpccred(cred);
337         cred = NULL;
338         return;
339 out_free:
340         list_del(&sp->so_list);
341         spin_unlock(&clp->cl_lock);
342         put_rpccred(cred);
343         kfree(sp);
344 }
345
346 static struct nfs4_state *
347 nfs4_alloc_open_state(void)
348 {
349         struct nfs4_state *state;
350
351         state = kmalloc(sizeof(*state), GFP_KERNEL);
352         if (!state)
353                 return NULL;
354         state->state = 0;
355         state->nreaders = 0;
356         state->nwriters = 0;
357         state->flags = 0;
358         memset(state->stateid.data, 0, sizeof(state->stateid.data));
359         atomic_set(&state->count, 1);
360         INIT_LIST_HEAD(&state->lock_states);
361         init_MUTEX(&state->lock_sema);
362         rwlock_init(&state->state_lock);
363         return state;
364 }
365
366 static struct nfs4_state *
367 __nfs4_find_state(struct inode *inode, struct rpc_cred *cred, mode_t mode)
368 {
369         struct nfs_inode *nfsi = NFS_I(inode);
370         struct nfs4_state *state;
371
372         mode &= (FMODE_READ|FMODE_WRITE);
373         list_for_each_entry(state, &nfsi->open_states, inode_states) {
374                 if (state->owner->so_cred != cred)
375                         continue;
376                 if ((mode & FMODE_READ) != 0 && state->nreaders == 0)
377                         continue;
378                 if ((mode & FMODE_WRITE) != 0 && state->nwriters == 0)
379                         continue;
380                 if ((state->state & mode) != mode)
381                         continue;
382                 atomic_inc(&state->count);
383                 if (mode & FMODE_READ)
384                         state->nreaders++;
385                 if (mode & FMODE_WRITE)
386                         state->nwriters++;
387                 return state;
388         }
389         return NULL;
390 }
391
392 static struct nfs4_state *
393 __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
394 {
395         struct nfs_inode *nfsi = NFS_I(inode);
396         struct nfs4_state *state;
397
398         list_for_each_entry(state, &nfsi->open_states, inode_states) {
399                 /* Is this in the process of being freed? */
400                 if (state->nreaders == 0 && state->nwriters == 0)
401                         continue;
402                 if (state->owner == owner) {
403                         atomic_inc(&state->count);
404                         return state;
405                 }
406         }
407         return NULL;
408 }
409
410 struct nfs4_state *
411 nfs4_find_state(struct inode *inode, struct rpc_cred *cred, mode_t mode)
412 {
413         struct nfs4_state *state;
414
415         spin_lock(&inode->i_lock);
416         state = __nfs4_find_state(inode, cred, mode);
417         spin_unlock(&inode->i_lock);
418         return state;
419 }
420
421 static void
422 nfs4_free_open_state(struct nfs4_state *state)
423 {
424         kfree(state);
425 }
426
427 struct nfs4_state *
428 nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
429 {
430         struct nfs4_state *state, *new;
431         struct nfs_inode *nfsi = NFS_I(inode);
432
433         spin_lock(&inode->i_lock);
434         state = __nfs4_find_state_byowner(inode, owner);
435         spin_unlock(&inode->i_lock);
436         if (state)
437                 goto out;
438         new = nfs4_alloc_open_state();
439         spin_lock(&inode->i_lock);
440         state = __nfs4_find_state_byowner(inode, owner);
441         if (state == NULL && new != NULL) {
442                 state = new;
443                 /* Caller *must* be holding owner->so_sem */
444                 list_add(&state->open_states, &owner->so_states);
445                 state->owner = owner;
446                 atomic_inc(&owner->so_count);
447                 list_add(&state->inode_states, &nfsi->open_states);
448                 state->inode = inode;
449                 spin_unlock(&inode->i_lock);
450         } else {
451                 spin_unlock(&inode->i_lock);
452                 if (new)
453                         nfs4_free_open_state(new);
454         }
455 out:
456         return state;
457 }
458
459 /*
460  * Beware! Caller must be holding exactly one
461  * reference to clp->cl_sem and owner->so_sema!
462  */
463 void nfs4_put_open_state(struct nfs4_state *state)
464 {
465         struct inode *inode = state->inode;
466         struct nfs4_state_owner *owner = state->owner;
467
468         if (!atomic_dec_and_lock(&state->count, &inode->i_lock))
469                 return;
470         if (!list_empty(&state->inode_states))
471                 list_del(&state->inode_states);
472         spin_unlock(&inode->i_lock);
473         list_del(&state->open_states);
474         BUG_ON (state->state != 0);
475         nfs4_free_open_state(state);
476         nfs4_put_state_owner(owner);
477 }
478
479 /*
480  * Beware! Caller must be holding no references to clp->cl_sem!
481  * of owner->so_sema!
482  */
483 void nfs4_close_state(struct nfs4_state *state, mode_t mode)
484 {
485         struct inode *inode = state->inode;
486         struct nfs4_state_owner *owner = state->owner;
487         struct nfs4_client *clp = owner->so_client;
488         int newstate;
489         int status = 0;
490
491         atomic_inc(&owner->so_count);
492         down_read(&clp->cl_sem);
493         down(&owner->so_sema);
494         /* Protect against nfs4_find_state() */
495         spin_lock(&inode->i_lock);
496         if (mode & FMODE_READ)
497                 state->nreaders--;
498         if (mode & FMODE_WRITE)
499                 state->nwriters--;
500         if (state->nwriters == 0 && state->nreaders == 0)
501                 list_del_init(&state->inode_states);
502         spin_unlock(&inode->i_lock);
503         newstate = 0;
504         if (state->state != 0) {
505                 if (state->nreaders)
506                         newstate |= FMODE_READ;
507                 if (state->nwriters)
508                         newstate |= FMODE_WRITE;
509                 if (state->state == newstate)
510                         goto out;
511                 if (newstate != 0)
512                         status = nfs4_do_downgrade(inode, state, newstate);
513                 else
514                         status = nfs4_do_close(inode, state);
515         }
516 out:
517         nfs4_put_open_state(state);
518         up(&owner->so_sema);
519         nfs4_put_state_owner(owner);
520         up_read(&clp->cl_sem);
521 }
522
523 /*
524  * Search the state->lock_states for an existing lock_owner
525  * that is compatible with current->files
526  */
527 static struct nfs4_lock_state *
528 __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
529 {
530         struct nfs4_lock_state *pos;
531         list_for_each_entry(pos, &state->lock_states, ls_locks) {
532                 if (pos->ls_owner != fl_owner)
533                         continue;
534                 atomic_inc(&pos->ls_count);
535                 return pos;
536         }
537         return NULL;
538 }
539
540 struct nfs4_lock_state *
541 nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
542 {
543         struct nfs4_lock_state *lsp;
544         read_lock(&state->state_lock);
545         lsp = __nfs4_find_lock_state(state, fl_owner);
546         read_unlock(&state->state_lock);
547         return lsp;
548 }
549
550 /*
551  * Return a compatible lock_state. If no initialized lock_state structure
552  * exists, return an uninitialized one.
553  *
554  * The caller must be holding state->lock_sema
555  */
556 static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
557 {
558         struct nfs4_lock_state *lsp;
559         struct nfs4_client *clp = state->owner->so_client;
560
561         lsp = kmalloc(sizeof(*lsp), GFP_KERNEL);
562         if (lsp == NULL)
563                 return NULL;
564         lsp->ls_flags = 0;
565         lsp->ls_seqid = 0;      /* arbitrary */
566         lsp->ls_id = -1; 
567         memset(lsp->ls_stateid.data, 0, sizeof(lsp->ls_stateid.data));
568         atomic_set(&lsp->ls_count, 1);
569         lsp->ls_owner = fl_owner;
570         INIT_LIST_HEAD(&lsp->ls_locks);
571         spin_lock(&clp->cl_lock);
572         lsp->ls_id = nfs4_alloc_lockowner_id(clp);
573         spin_unlock(&clp->cl_lock);
574         return lsp;
575 }
576
577 /*
578  * Return a compatible lock_state. If no initialized lock_state structure
579  * exists, return an uninitialized one.
580  *
581  * The caller must be holding state->lock_sema and clp->cl_sem
582  */
583 struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
584 {
585         struct nfs4_lock_state * lsp;
586         
587         lsp = nfs4_find_lock_state(state, owner);
588         if (lsp == NULL)
589                 lsp = nfs4_alloc_lock_state(state, owner);
590         return lsp;
591 }
592
593 /*
594  * Byte-range lock aware utility to initialize the stateid of read/write
595  * requests.
596  */
597 void
598 nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
599 {
600         if (test_bit(LK_STATE_IN_USE, &state->flags)) {
601                 struct nfs4_lock_state *lsp;
602
603                 lsp = nfs4_find_lock_state(state, fl_owner);
604                 if (lsp) {
605                         memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
606                         nfs4_put_lock_state(lsp);
607                         return;
608                 }
609         }
610         memcpy(dst, &state->stateid, sizeof(*dst));
611 }
612
613 /*
614 * Called with state->lock_sema and clp->cl_sem held.
615 */
616 void nfs4_increment_lock_seqid(int status, struct nfs4_lock_state *lsp)
617 {
618         if (status == NFS_OK || seqid_mutating_err(-status))
619                 lsp->ls_seqid++;
620 }
621
622 /* 
623 * Check to see if the request lock (type FL_UNLK) effects the fl lock.
624 *
625 * fl and request must have the same posix owner
626 *
627 * return: 
628 * 0 -> fl not effected by request
629 * 1 -> fl consumed by request
630 */
631
632 static int
633 nfs4_check_unlock(struct file_lock *fl, struct file_lock *request)
634 {
635         if (fl->fl_start >= request->fl_start && fl->fl_end <= request->fl_end)
636                 return 1;
637         return 0;
638 }
639
640 /*
641  * Post an initialized lock_state on the state->lock_states list.
642  */
643 void nfs4_notify_setlk(struct nfs4_state *state, struct file_lock *request, struct nfs4_lock_state *lsp)
644 {
645         if (!list_empty(&lsp->ls_locks))
646                 return;
647         atomic_inc(&lsp->ls_count);
648         write_lock(&state->state_lock);
649         list_add(&lsp->ls_locks, &state->lock_states);
650         set_bit(LK_STATE_IN_USE, &state->flags);
651         write_unlock(&state->state_lock);
652 }
653
654 /* 
655  * to decide to 'reap' lock state:
656  * 1) search i_flock for file_locks with fl.lock_state = to ls.
657  * 2) determine if unlock will consume found lock. 
658  *      if so, reap
659  *
660  *      else, don't reap.
661  *
662  */
663 void
664 nfs4_notify_unlck(struct nfs4_state *state, struct file_lock *request, struct nfs4_lock_state *lsp)
665 {
666         struct inode *inode = state->inode;
667         struct file_lock *fl;
668
669         for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
670                 if (!(fl->fl_flags & FL_POSIX))
671                         continue;
672                 if (fl->fl_owner != lsp->ls_owner)
673                         continue;
674                 /* Exit if we find at least one lock which is not consumed */
675                 if (nfs4_check_unlock(fl,request) == 0)
676                         return;
677         }
678
679         write_lock(&state->state_lock);
680         list_del_init(&lsp->ls_locks);
681         if (list_empty(&state->lock_states))
682                 clear_bit(LK_STATE_IN_USE, &state->flags);
683         write_unlock(&state->state_lock);
684         nfs4_put_lock_state(lsp);
685 }
686
687 /*
688  * Release reference to lock_state, and free it if we see that
689  * it is no longer in use
690  */
691 void
692 nfs4_put_lock_state(struct nfs4_lock_state *lsp)
693 {
694         if (!atomic_dec_and_test(&lsp->ls_count))
695                 return;
696         BUG_ON (!list_empty(&lsp->ls_locks));
697         kfree(lsp);
698 }
699
700 /*
701 * Called with sp->so_sema and clp->cl_sem held.
702 *
703 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
704 * failed with a seqid incrementing error -
705 * see comments nfs_fs.h:seqid_mutating_error()
706 */
707 void nfs4_increment_seqid(int status, struct nfs4_state_owner *sp)
708 {
709         if (status == NFS_OK || seqid_mutating_err(-status))
710                 sp->so_seqid++;
711         /* If the server returns BAD_SEQID, unhash state_owner here */
712         if (status == -NFS4ERR_BAD_SEQID)
713                 nfs4_unhash_state_owner(sp);
714 }
715
716 static int reclaimer(void *);
717 struct reclaimer_args {
718         struct nfs4_client *clp;
719         struct completion complete;
720 };
721
722 /*
723  * State recovery routine
724  */
725 void
726 nfs4_recover_state(void *data)
727 {
728         struct nfs4_client *clp = (struct nfs4_client *)data;
729         struct reclaimer_args args = {
730                 .clp = clp,
731         };
732         might_sleep();
733
734         init_completion(&args.complete);
735
736         if (kernel_thread(reclaimer, &args, CLONE_KERNEL) < 0)
737                 goto out_failed_clear;
738         wait_for_completion(&args.complete);
739         return;
740 out_failed_clear:
741         set_bit(NFS4CLNT_OK, &clp->cl_state);
742         wake_up_all(&clp->cl_waitq);
743         rpc_wake_up(&clp->cl_rpcwaitq);
744 }
745
746 /*
747  * Schedule a state recovery attempt
748  */
749 void
750 nfs4_schedule_state_recovery(struct nfs4_client *clp)
751 {
752         if (!clp)
753                 return;
754         if (test_and_clear_bit(NFS4CLNT_OK, &clp->cl_state))
755                 schedule_work(&clp->cl_recoverd);
756 }
757
758 static int nfs4_reclaim_locks(struct nfs4_state *state)
759 {
760         struct inode *inode = state->inode;
761         struct file_lock *fl;
762         int status = 0;
763
764         for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
765                 if (!(fl->fl_flags & FL_POSIX))
766                         continue;
767                 if (((struct nfs_open_context *)fl->fl_file->private_data)->state != state)
768                         continue;
769                 status = nfs4_lock_reclaim(state, fl);
770                 if (status >= 0)
771                         continue;
772                 switch (status) {
773                         default:
774                                 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
775                                                 __FUNCTION__, status);
776                         case -NFS4ERR_EXPIRED:
777                         case -NFS4ERR_NO_GRACE:
778                         case -NFS4ERR_RECLAIM_BAD:
779                         case -NFS4ERR_RECLAIM_CONFLICT:
780                                 /* kill_proc(fl->fl_owner, SIGLOST, 1); */
781                                 break;
782                         case -NFS4ERR_STALE_CLIENTID:
783                                 goto out_err;
784                 }
785         }
786         return 0;
787 out_err:
788         return status;
789 }
790
791 static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp)
792 {
793         struct nfs4_state *state;
794         struct nfs4_lock_state *lock;
795         int status = 0;
796
797         list_for_each_entry(state, &sp->so_states, open_states) {
798                 if (state->state == 0)
799                         continue;
800                 status = nfs4_open_reclaim(sp, state);
801                 list_for_each_entry(lock, &state->lock_states, ls_locks)
802                         lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
803                 if (status >= 0) {
804                         status = nfs4_reclaim_locks(state);
805                         if (status < 0)
806                                 goto out_err;
807                         list_for_each_entry(lock, &state->lock_states, ls_locks) {
808                                 if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
809                                         printk("%s: Lock reclaim failed!\n",
810                                                         __FUNCTION__);
811                         }
812                         continue;
813                 }
814                 switch (status) {
815                         default:
816                                 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
817                                                 __FUNCTION__, status);
818                         case -NFS4ERR_EXPIRED:
819                         case -NFS4ERR_NO_GRACE:
820                         case -NFS4ERR_RECLAIM_BAD:
821                         case -NFS4ERR_RECLAIM_CONFLICT:
822                                 /*
823                                  * Open state on this file cannot be recovered
824                                  * All we can do is revert to using the zero stateid.
825                                  */
826                                 memset(state->stateid.data, 0,
827                                         sizeof(state->stateid.data));
828                                 /* Mark the file as being 'closed' */
829                                 state->state = 0;
830                                 break;
831                         case -NFS4ERR_STALE_CLIENTID:
832                                 goto out_err;
833                 }
834         }
835         return 0;
836 out_err:
837         return status;
838 }
839
840 static int reclaimer(void *ptr)
841 {
842         struct reclaimer_args *args = (struct reclaimer_args *)ptr;
843         struct nfs4_client *clp = args->clp;
844         struct nfs4_state_owner *sp;
845         int status = 0;
846
847         daemonize("%u.%u.%u.%u-reclaim", NIPQUAD(clp->cl_addr));
848         allow_signal(SIGKILL);
849
850         atomic_inc(&clp->cl_count);
851         complete(&args->complete);
852
853         /* Ensure exclusive access to NFSv4 state */
854         lock_kernel();
855         down_write(&clp->cl_sem);
856         /* Are there any NFS mounts out there? */
857         if (list_empty(&clp->cl_superblocks))
858                 goto out;
859 restart_loop:
860         status = nfs4_proc_renew(clp);
861         if (status == 0 || status == -NFS4ERR_CB_PATH_DOWN)
862                 goto out;
863         status = nfs4_init_client(clp);
864         if (status)
865                 goto out_error;
866         /* Mark all delagations for reclaim */
867         nfs_delegation_mark_reclaim(clp);
868         /* Note: list is protected by exclusive lock on cl->cl_sem */
869         list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
870                 status = nfs4_reclaim_open_state(sp);
871                 if (status < 0) {
872                         if (status == -NFS4ERR_STALE_CLIENTID)
873                                 goto restart_loop;
874                         goto out_error;
875                 }
876         }
877         nfs_delegation_reap_unclaimed(clp);
878 out:
879         set_bit(NFS4CLNT_OK, &clp->cl_state);
880         up_write(&clp->cl_sem);
881         unlock_kernel();
882         wake_up_all(&clp->cl_waitq);
883         rpc_wake_up(&clp->cl_rpcwaitq);
884         if (status == -NFS4ERR_CB_PATH_DOWN)
885                 nfs_handle_cb_pathdown(clp);
886         nfs4_put_client(clp);
887         return 0;
888 out_error:
889         printk(KERN_WARNING "Error: state recovery failed on NFSv4 server %u.%u.%u.%u with error %d\n",
890                                 NIPQUAD(clp->cl_addr.s_addr), -status);
891         goto out;
892 }
893
894 /*
895  * Local variables:
896  *  c-basic-offset: 8
897  * End:
898  */