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