[PATCH] knfsd: nfsd4: setclientid simplification
[linux-2.6-block.git] / fs / nfsd / nfs4state.c
CommitLineData
1da177e4
LT
1/*
2* linux/fs/nfsd/nfs4state.c
3*
4* Copyright (c) 2001 The Regents of the University of Michigan.
5* All rights reserved.
6*
7* Kendrick Smith <kmsmith@umich.edu>
8* Andy Adamson <kandros@umich.edu>
9*
10* Redistribution and use in source and binary forms, with or without
11* modification, are permitted provided that the following conditions
12* are met:
13*
14* 1. Redistributions of source code must retain the above copyright
15* notice, this list of conditions and the following disclaimer.
16* 2. Redistributions in binary form must reproduce the above copyright
17* notice, this list of conditions and the following disclaimer in the
18* documentation and/or other materials provided with the distribution.
19* 3. Neither the name of the University nor the names of its
20* contributors may be used to endorse or promote products derived
21* from this software without specific prior written permission.
22*
23* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*
35*/
36
37#include <linux/param.h>
38#include <linux/major.h>
39#include <linux/slab.h>
40
41#include <linux/sunrpc/svc.h>
42#include <linux/nfsd/nfsd.h>
43#include <linux/nfsd/cache.h>
44#include <linux/mount.h>
45#include <linux/workqueue.h>
46#include <linux/smp_lock.h>
47#include <linux/kthread.h>
48#include <linux/nfs4.h>
49#include <linux/nfsd/state.h>
50#include <linux/nfsd/xdr4.h>
51
52#define NFSDDBG_FACILITY NFSDDBG_PROC
53
54/* Globals */
55static time_t lease_time = 90; /* default lease time */
d99a05ad 56static time_t user_lease_time = 90;
1da177e4
LT
57time_t boot_time;
58static time_t grace_end = 0;
59static u32 current_clientid = 1;
60static u32 current_ownerid = 1;
61static u32 current_fileid = 1;
62static u32 current_delegid = 1;
63static u32 nfs4_init;
64stateid_t zerostateid; /* bits all 0 */
65stateid_t onestateid; /* bits all 1 */
66
1da177e4
LT
67/* forward declarations */
68struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
69static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
70static void release_stateid_lockowners(struct nfs4_stateid *open_stp);
71
72/* Locking:
73 *
74 * client_sema:
75 * protects clientid_hashtbl[], clientstr_hashtbl[],
76 * unconfstr_hashtbl[], uncofid_hashtbl[].
77 */
78static DECLARE_MUTEX(client_sema);
79
e60d4398
N
80kmem_cache_t *stateowner_slab = NULL;
81kmem_cache_t *file_slab = NULL;
5ac049ac 82kmem_cache_t *stateid_slab = NULL;
5b2d21c1 83kmem_cache_t *deleg_slab = NULL;
e60d4398 84
1da177e4
LT
85void
86nfs4_lock_state(void)
87{
88 down(&client_sema);
89}
90
91void
92nfs4_unlock_state(void)
93{
94 up(&client_sema);
95}
96
97static inline u32
98opaque_hashval(const void *ptr, int nbytes)
99{
100 unsigned char *cptr = (unsigned char *) ptr;
101
102 u32 x = 0;
103 while (nbytes--) {
104 x *= 37;
105 x += *cptr++;
106 }
107 return x;
108}
109
110/* forward declarations */
111static void release_stateowner(struct nfs4_stateowner *sop);
112static void release_stateid(struct nfs4_stateid *stp, int flags);
1da177e4
LT
113
114/*
115 * Delegation state
116 */
117
118/* recall_lock protects the del_recall_lru */
ac4d8ff2 119spinlock_t recall_lock = SPIN_LOCK_UNLOCKED;
1da177e4
LT
120static struct list_head del_recall_lru;
121
13cd2184
N
122static void
123free_nfs4_file(struct kref *kref)
124{
125 struct nfs4_file *fp = container_of(kref, struct nfs4_file, fi_ref);
126 list_del(&fp->fi_hash);
127 iput(fp->fi_inode);
128 kmem_cache_free(file_slab, fp);
129}
130
131static inline void
132put_nfs4_file(struct nfs4_file *fi)
133{
134 kref_put(&fi->fi_ref, free_nfs4_file);
135}
136
137static inline void
138get_nfs4_file(struct nfs4_file *fi)
139{
140 kref_get(&fi->fi_ref);
141}
142
1da177e4
LT
143static struct nfs4_delegation *
144alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
145{
146 struct nfs4_delegation *dp;
147 struct nfs4_file *fp = stp->st_file;
148 struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback;
149
150 dprintk("NFSD alloc_init_deleg\n");
5b2d21c1
N
151 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
152 if (dp == NULL)
1da177e4
LT
153 return dp;
154 INIT_LIST_HEAD(&dp->dl_del_perfile);
155 INIT_LIST_HEAD(&dp->dl_del_perclnt);
156 INIT_LIST_HEAD(&dp->dl_recall_lru);
157 dp->dl_client = clp;
13cd2184 158 get_nfs4_file(fp);
1da177e4
LT
159 dp->dl_file = fp;
160 dp->dl_flock = NULL;
161 get_file(stp->st_vfs_file);
162 dp->dl_vfs_file = stp->st_vfs_file;
163 dp->dl_type = type;
164 dp->dl_recall.cbr_dp = NULL;
165 dp->dl_recall.cbr_ident = cb->cb_ident;
166 dp->dl_recall.cbr_trunc = 0;
167 dp->dl_stateid.si_boot = boot_time;
168 dp->dl_stateid.si_stateownerid = current_delegid++;
169 dp->dl_stateid.si_fileid = 0;
170 dp->dl_stateid.si_generation = 0;
171 dp->dl_fhlen = current_fh->fh_handle.fh_size;
172 memcpy(dp->dl_fhval, &current_fh->fh_handle.fh_base,
173 current_fh->fh_handle.fh_size);
174 dp->dl_time = 0;
175 atomic_set(&dp->dl_count, 1);
8beefa24 176 list_add(&dp->dl_del_perfile, &fp->fi_delegations);
1da177e4 177 list_add(&dp->dl_del_perclnt, &clp->cl_del_perclnt);
1da177e4
LT
178 return dp;
179}
180
181void
182nfs4_put_delegation(struct nfs4_delegation *dp)
183{
184 if (atomic_dec_and_test(&dp->dl_count)) {
185 dprintk("NFSD: freeing dp %p\n",dp);
13cd2184 186 put_nfs4_file(dp->dl_file);
5b2d21c1 187 kmem_cache_free(deleg_slab, dp);
1da177e4
LT
188 }
189}
190
191/* Remove the associated file_lock first, then remove the delegation.
192 * lease_modify() is called to remove the FS_LEASE file_lock from
193 * the i_flock list, eventually calling nfsd's lock_manager
194 * fl_release_callback.
195 */
196static void
197nfs4_close_delegation(struct nfs4_delegation *dp)
198{
199 struct file *filp = dp->dl_vfs_file;
200
201 dprintk("NFSD: close_delegation dp %p\n",dp);
202 dp->dl_vfs_file = NULL;
203 /* The following nfsd_close may not actually close the file,
204 * but we want to remove the lease in any case. */
c907132d
N
205 if (dp->dl_flock)
206 setlease(filp, F_UNLCK, &dp->dl_flock);
1da177e4 207 nfsd_close(filp);
1da177e4
LT
208}
209
210/* Called under the state lock. */
211static void
212unhash_delegation(struct nfs4_delegation *dp)
213{
214 list_del_init(&dp->dl_del_perfile);
215 list_del_init(&dp->dl_del_perclnt);
216 spin_lock(&recall_lock);
217 list_del_init(&dp->dl_recall_lru);
218 spin_unlock(&recall_lock);
219 nfs4_close_delegation(dp);
220 nfs4_put_delegation(dp);
221}
222
223/*
224 * SETCLIENTID state
225 */
226
227/* Hash tables for nfs4_clientid state */
228#define CLIENT_HASH_BITS 4
229#define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
230#define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
231
232#define clientid_hashval(id) \
233 ((id) & CLIENT_HASH_MASK)
234#define clientstr_hashval(name, namelen) \
235 (opaque_hashval((name), (namelen)) & CLIENT_HASH_MASK)
236/*
237 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
238 * used in reboot/reset lease grace period processing
239 *
240 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
241 * setclientid_confirmed info.
242 *
243 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
244 * setclientid info.
245 *
246 * client_lru holds client queue ordered by nfs4_client.cl_time
247 * for lease renewal.
248 *
249 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
250 * for last close replay.
251 */
252static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
253static int reclaim_str_hashtbl_size = 0;
254static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
255static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
256static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
257static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
258static struct list_head client_lru;
259static struct list_head close_lru;
260
261static inline void
262renew_client(struct nfs4_client *clp)
263{
264 /*
265 * Move client to the end to the LRU list.
266 */
267 dprintk("renewing client (clientid %08x/%08x)\n",
268 clp->cl_clientid.cl_boot,
269 clp->cl_clientid.cl_id);
270 list_move_tail(&clp->cl_lru, &client_lru);
271 clp->cl_time = get_seconds();
272}
273
274/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
275static int
276STALE_CLIENTID(clientid_t *clid)
277{
278 if (clid->cl_boot == boot_time)
279 return 0;
280 dprintk("NFSD stale clientid (%08x/%08x)\n",
281 clid->cl_boot, clid->cl_id);
282 return 1;
283}
284
285/*
286 * XXX Should we use a slab cache ?
287 * This type of memory management is somewhat inefficient, but we use it
288 * anyway since SETCLIENTID is not a common operation.
289 */
290static inline struct nfs4_client *
291alloc_client(struct xdr_netobj name)
292{
293 struct nfs4_client *clp;
294
295 if ((clp = kmalloc(sizeof(struct nfs4_client), GFP_KERNEL))!= NULL) {
296 memset(clp, 0, sizeof(*clp));
297 if ((clp->cl_name.data = kmalloc(name.len, GFP_KERNEL)) != NULL) {
298 memcpy(clp->cl_name.data, name.data, name.len);
299 clp->cl_name.len = name.len;
300 }
301 else {
302 kfree(clp);
303 clp = NULL;
304 }
305 }
306 return clp;
307}
308
309static inline void
310free_client(struct nfs4_client *clp)
311{
312 if (clp->cl_cred.cr_group_info)
313 put_group_info(clp->cl_cred.cr_group_info);
314 kfree(clp->cl_name.data);
315 kfree(clp);
316}
317
318void
319put_nfs4_client(struct nfs4_client *clp)
320{
321 if (atomic_dec_and_test(&clp->cl_count))
322 free_client(clp);
323}
324
325static void
326expire_client(struct nfs4_client *clp)
327{
328 struct nfs4_stateowner *sop;
329 struct nfs4_delegation *dp;
330 struct nfs4_callback *cb = &clp->cl_callback;
331 struct rpc_clnt *clnt = clp->cl_callback.cb_client;
332 struct list_head reaplist;
333
334 dprintk("NFSD: expire_client cl_count %d\n",
335 atomic_read(&clp->cl_count));
336
337 /* shutdown rpc client, ending any outstanding recall rpcs */
338 if (atomic_read(&cb->cb_set) == 1 && clnt) {
339 rpc_shutdown_client(clnt);
340 clnt = clp->cl_callback.cb_client = NULL;
341 }
342
343 INIT_LIST_HEAD(&reaplist);
344 spin_lock(&recall_lock);
345 while (!list_empty(&clp->cl_del_perclnt)) {
346 dp = list_entry(clp->cl_del_perclnt.next, struct nfs4_delegation, dl_del_perclnt);
347 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
348 dp->dl_flock);
349 list_del_init(&dp->dl_del_perclnt);
350 list_move(&dp->dl_recall_lru, &reaplist);
351 }
352 spin_unlock(&recall_lock);
353 while (!list_empty(&reaplist)) {
354 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
355 list_del_init(&dp->dl_recall_lru);
356 unhash_delegation(dp);
357 }
358 list_del(&clp->cl_idhash);
359 list_del(&clp->cl_strhash);
360 list_del(&clp->cl_lru);
361 while (!list_empty(&clp->cl_perclient)) {
362 sop = list_entry(clp->cl_perclient.next, struct nfs4_stateowner, so_perclient);
363 release_stateowner(sop);
364 }
365 put_nfs4_client(clp);
366}
367
368static struct nfs4_client *
369create_client(struct xdr_netobj name) {
370 struct nfs4_client *clp;
371
372 if (!(clp = alloc_client(name)))
373 goto out;
374 atomic_set(&clp->cl_count, 1);
375 atomic_set(&clp->cl_callback.cb_set, 0);
376 clp->cl_callback.cb_parsed = 0;
377 INIT_LIST_HEAD(&clp->cl_idhash);
378 INIT_LIST_HEAD(&clp->cl_strhash);
379 INIT_LIST_HEAD(&clp->cl_perclient);
380 INIT_LIST_HEAD(&clp->cl_del_perclnt);
381 INIT_LIST_HEAD(&clp->cl_lru);
382out:
383 return clp;
384}
385
386static void
387copy_verf(struct nfs4_client *target, nfs4_verifier *source) {
388 memcpy(target->cl_verifier.data, source->data, sizeof(target->cl_verifier.data));
389}
390
391static void
392copy_clid(struct nfs4_client *target, struct nfs4_client *source) {
393 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
394 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
395}
396
397static void
398copy_cred(struct svc_cred *target, struct svc_cred *source) {
399
400 target->cr_uid = source->cr_uid;
401 target->cr_gid = source->cr_gid;
402 target->cr_group_info = source->cr_group_info;
403 get_group_info(target->cr_group_info);
404}
405
406static int
407cmp_name(struct xdr_netobj *n1, struct xdr_netobj *n2) {
408 if (!n1 || !n2)
409 return 0;
410 return((n1->len == n2->len) && !memcmp(n1->data, n2->data, n2->len));
411}
412
413static int
414cmp_verf(nfs4_verifier *v1, nfs4_verifier *v2) {
415 return(!memcmp(v1->data,v2->data,sizeof(v1->data)));
416}
417
418static int
419cmp_clid(clientid_t * cl1, clientid_t * cl2) {
420 return((cl1->cl_boot == cl2->cl_boot) &&
421 (cl1->cl_id == cl2->cl_id));
422}
423
424/* XXX what about NGROUP */
425static int
426cmp_creds(struct svc_cred *cr1, struct svc_cred *cr2){
427 return(cr1->cr_uid == cr2->cr_uid);
428
429}
430
431static void
432gen_clid(struct nfs4_client *clp) {
433 clp->cl_clientid.cl_boot = boot_time;
434 clp->cl_clientid.cl_id = current_clientid++;
435}
436
437static void
438gen_confirm(struct nfs4_client *clp) {
439 struct timespec tv;
440 u32 * p;
441
442 tv = CURRENT_TIME;
443 p = (u32 *)clp->cl_confirm.data;
444 *p++ = tv.tv_sec;
445 *p++ = tv.tv_nsec;
446}
447
448static int
449check_name(struct xdr_netobj name) {
450
451 if (name.len == 0)
452 return 0;
453 if (name.len > NFS4_OPAQUE_LIMIT) {
454 printk("NFSD: check_name: name too long(%d)!\n", name.len);
455 return 0;
456 }
457 return 1;
458}
459
460void
461add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
462{
463 unsigned int idhashval;
464
465 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
466 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
467 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
468 list_add_tail(&clp->cl_lru, &client_lru);
469 clp->cl_time = get_seconds();
470}
471
472void
473move_to_confirmed(struct nfs4_client *clp)
474{
475 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
476 unsigned int strhashval;
477
478 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
479 list_del_init(&clp->cl_strhash);
480 list_del_init(&clp->cl_idhash);
481 list_add(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
482 strhashval = clientstr_hashval(clp->cl_name.data,
483 clp->cl_name.len);
484 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
485 renew_client(clp);
486}
487
488static struct nfs4_client *
489find_confirmed_client(clientid_t *clid)
490{
491 struct nfs4_client *clp;
492 unsigned int idhashval = clientid_hashval(clid->cl_id);
493
494 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
495 if (cmp_clid(&clp->cl_clientid, clid))
496 return clp;
497 }
498 return NULL;
499}
500
501static struct nfs4_client *
502find_unconfirmed_client(clientid_t *clid)
503{
504 struct nfs4_client *clp;
505 unsigned int idhashval = clientid_hashval(clid->cl_id);
506
507 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
508 if (cmp_clid(&clp->cl_clientid, clid))
509 return clp;
510 }
511 return NULL;
512}
513
514/* a helper function for parse_callback */
515static int
516parse_octet(unsigned int *lenp, char **addrp)
517{
518 unsigned int len = *lenp;
519 char *p = *addrp;
520 int n = -1;
521 char c;
522
523 for (;;) {
524 if (!len)
525 break;
526 len--;
527 c = *p++;
528 if (c == '.')
529 break;
530 if ((c < '0') || (c > '9')) {
531 n = -1;
532 break;
533 }
534 if (n < 0)
535 n = 0;
536 n = (n * 10) + (c - '0');
537 if (n > 255) {
538 n = -1;
539 break;
540 }
541 }
542 *lenp = len;
543 *addrp = p;
544 return n;
545}
546
547/* parse and set the setclientid ipv4 callback address */
548int
549parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
550{
551 int temp = 0;
552 u32 cbaddr = 0;
553 u16 cbport = 0;
554 u32 addrlen = addr_len;
555 char *addr = addr_val;
556 int i, shift;
557
558 /* ipaddress */
559 shift = 24;
560 for(i = 4; i > 0 ; i--) {
561 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
562 return 0;
563 }
564 cbaddr |= (temp << shift);
565 if (shift > 0)
566 shift -= 8;
567 }
568 *cbaddrp = cbaddr;
569
570 /* port */
571 shift = 8;
572 for(i = 2; i > 0 ; i--) {
573 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
574 return 0;
575 }
576 cbport |= (temp << shift);
577 if (shift > 0)
578 shift -= 8;
579 }
580 *cbportp = cbport;
581 return 1;
582}
583
584void
585gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
586{
587 struct nfs4_callback *cb = &clp->cl_callback;
588
589 /* Currently, we only support tcp for the callback channel */
590 if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
591 goto out_err;
592
593 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
594 &cb->cb_addr, &cb->cb_port)))
595 goto out_err;
596 cb->cb_prog = se->se_callback_prog;
597 cb->cb_ident = se->se_callback_ident;
598 cb->cb_parsed = 1;
599 return;
600out_err:
601 printk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
602 "will not receive delegations\n",
603 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
604
605 cb->cb_parsed = 0;
606 return;
607}
608
609/*
610 * RFC 3010 has a complex implmentation description of processing a
611 * SETCLIENTID request consisting of 5 bullets, labeled as
612 * CASE0 - CASE4 below.
613 *
614 * NOTES:
615 * callback information will be processed in a future patch
616 *
617 * an unconfirmed record is added when:
618 * NORMAL (part of CASE 4): there is no confirmed nor unconfirmed record.
619 * CASE 1: confirmed record found with matching name, principal,
620 * verifier, and clientid.
621 * CASE 2: confirmed record found with matching name, principal,
622 * and there is no unconfirmed record with matching
623 * name and principal
624 *
625 * an unconfirmed record is replaced when:
626 * CASE 3: confirmed record found with matching name, principal,
627 * and an unconfirmed record is found with matching
628 * name, principal, and with clientid and
629 * confirm that does not match the confirmed record.
630 * CASE 4: there is no confirmed record with matching name and
631 * principal. there is an unconfirmed record with
632 * matching name, principal.
633 *
634 * an unconfirmed record is deleted when:
635 * CASE 1: an unconfirmed record that matches input name, verifier,
636 * and confirmed clientid.
637 * CASE 4: any unconfirmed records with matching name and principal
638 * that exist after an unconfirmed record has been replaced
639 * as described above.
640 *
641 */
642int
643nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_setclientid *setclid)
644{
645 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
646 struct xdr_netobj clname = {
647 .len = setclid->se_namelen,
648 .data = setclid->se_name,
649 };
650 nfs4_verifier clverifier = setclid->se_verf;
651 unsigned int strhashval;
652 struct nfs4_client * conf, * unconf, * new, * clp;
653 int status;
654
655 status = nfserr_inval;
656 if (!check_name(clname))
657 goto out;
658
659 /*
660 * XXX The Duplicate Request Cache (DRC) has been checked (??)
661 * We get here on a DRC miss.
662 */
663
664 strhashval = clientstr_hashval(clname.data, clname.len);
665
666 conf = NULL;
667 nfs4_lock_state();
668 list_for_each_entry(clp, &conf_str_hashtbl[strhashval], cl_strhash) {
669 if (!cmp_name(&clp->cl_name, &clname))
670 continue;
671 /*
672 * CASE 0:
673 * clname match, confirmed, different principal
674 * or different ip_address
675 */
676 status = nfserr_clid_inuse;
7dea9d28
N
677 if (!cmp_creds(&clp->cl_cred,&rqstp->rq_cred)
678 || clp->cl_addr != ip_addr) {
1da177e4
LT
679 printk("NFSD: setclientid: string in use by client"
680 "(clientid %08x/%08x)\n",
681 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
682 goto out;
683 }
1da177e4
LT
684 conf = clp;
685 break;
686 }
687 unconf = NULL;
688 list_for_each_entry(clp, &unconf_str_hashtbl[strhashval], cl_strhash) {
689 if (!cmp_name(&clp->cl_name, &clname))
690 continue;
691 /* cl_name match from a previous SETCLIENTID operation */
692 unconf = clp;
693 break;
694 }
695 status = nfserr_resource;
696 if (!conf) {
697 /*
698 * CASE 4:
699 * placed first, because it is the normal case.
700 */
701 if (unconf)
702 expire_client(unconf);
703 if (!(new = create_client(clname)))
704 goto out;
705 copy_verf(new, &clverifier);
706 new->cl_addr = ip_addr;
707 copy_cred(&new->cl_cred,&rqstp->rq_cred);
708 gen_clid(new);
709 gen_confirm(new);
710 gen_callback(new, setclid);
711 add_to_unconfirmed(new, strhashval);
712 } else if (cmp_verf(&conf->cl_verifier, &clverifier)) {
713 /*
714 * CASE 1:
715 * cl_name match, confirmed, principal match
716 * verifier match: probable callback update
717 *
718 * remove any unconfirmed nfs4_client with
719 * matching cl_name, cl_verifier, and cl_clientid
720 *
721 * create and insert an unconfirmed nfs4_client with same
722 * cl_name, cl_verifier, and cl_clientid as existing
723 * nfs4_client, but with the new callback info and a
724 * new cl_confirm
725 */
726 if ((unconf) &&
727 cmp_verf(&unconf->cl_verifier, &conf->cl_verifier) &&
728 cmp_clid(&unconf->cl_clientid, &conf->cl_clientid)) {
729 expire_client(unconf);
730 }
731 if (!(new = create_client(clname)))
732 goto out;
733 copy_verf(new,&conf->cl_verifier);
734 new->cl_addr = ip_addr;
735 copy_cred(&new->cl_cred,&rqstp->rq_cred);
736 copy_clid(new, conf);
737 gen_confirm(new);
738 gen_callback(new, setclid);
739 add_to_unconfirmed(new,strhashval);
740 } else if (!unconf) {
741 /*
742 * CASE 2:
743 * clname match, confirmed, principal match
744 * verfier does not match
745 * no unconfirmed. create a new unconfirmed nfs4_client
746 * using input clverifier, clname, and callback info
747 * and generate a new cl_clientid and cl_confirm.
748 */
749 if (!(new = create_client(clname)))
750 goto out;
751 copy_verf(new,&clverifier);
752 new->cl_addr = ip_addr;
753 copy_cred(&new->cl_cred,&rqstp->rq_cred);
754 gen_clid(new);
755 gen_confirm(new);
756 gen_callback(new, setclid);
757 add_to_unconfirmed(new, strhashval);
758 } else if (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm)) {
759 /*
760 * CASE3:
761 * confirmed found (name, principal match)
762 * confirmed verifier does not match input clverifier
763 *
764 * unconfirmed found (name match)
765 * confirmed->cl_confirm != unconfirmed->cl_confirm
766 *
767 * remove unconfirmed.
768 *
769 * create an unconfirmed nfs4_client
770 * with same cl_name as existing confirmed nfs4_client,
771 * but with new callback info, new cl_clientid,
772 * new cl_verifier and a new cl_confirm
773 */
774 expire_client(unconf);
775 if (!(new = create_client(clname)))
776 goto out;
777 copy_verf(new,&clverifier);
778 new->cl_addr = ip_addr;
779 copy_cred(&new->cl_cred,&rqstp->rq_cred);
780 gen_clid(new);
781 gen_confirm(new);
782 gen_callback(new, setclid);
783 add_to_unconfirmed(new, strhashval);
784 } else {
785 /* No cases hit !!! */
786 status = nfserr_inval;
787 goto out;
788
789 }
790 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
791 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
792 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
793 status = nfs_ok;
794out:
795 nfs4_unlock_state();
796 return status;
797}
798
799
800/*
801 * RFC 3010 has a complex implmentation description of processing a
802 * SETCLIENTID_CONFIRM request consisting of 4 bullets describing
803 * processing on a DRC miss, labeled as CASE1 - CASE4 below.
804 *
805 * NOTE: callback information will be processed here in a future patch
806 */
807int
808nfsd4_setclientid_confirm(struct svc_rqst *rqstp, struct nfsd4_setclientid_confirm *setclientid_confirm)
809{
810 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
811 struct nfs4_client *clp, *conf = NULL, *unconf = NULL;
812 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
813 clientid_t * clid = &setclientid_confirm->sc_clientid;
814 int status;
815
816 if (STALE_CLIENTID(clid))
817 return nfserr_stale_clientid;
818 /*
819 * XXX The Duplicate Request Cache (DRC) has been checked (??)
820 * We get here on a DRC miss.
821 */
822
823 nfs4_lock_state();
824 clp = find_confirmed_client(clid);
825 if (clp) {
826 status = nfserr_inval;
827 /*
828 * Found a record for this clientid. If the IP addresses
829 * don't match, return ERR_INVAL just as if the record had
830 * not been found.
831 */
832 if (clp->cl_addr != ip_addr) {
833 printk("NFSD: setclientid: string in use by client"
834 "(clientid %08x/%08x)\n",
835 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
836 goto out;
837 }
838 conf = clp;
839 }
840 clp = find_unconfirmed_client(clid);
841 if (clp) {
842 status = nfserr_inval;
843 if (clp->cl_addr != ip_addr) {
844 printk("NFSD: setclientid: string in use by client"
845 "(clientid %08x/%08x)\n",
846 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
847 goto out;
848 }
849 unconf = clp;
850 }
851 /* CASE 1:
852 * unconf record that matches input clientid and input confirm.
853 * conf record that matches input clientid.
854 * conf and unconf records match names, verifiers
855 */
856 if ((conf && unconf) &&
857 (cmp_verf(&unconf->cl_confirm, &confirm)) &&
858 (cmp_verf(&conf->cl_verifier, &unconf->cl_verifier)) &&
859 (cmp_name(&conf->cl_name,&unconf->cl_name)) &&
860 (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm))) {
861 if (!cmp_creds(&conf->cl_cred, &unconf->cl_cred))
862 status = nfserr_clid_inuse;
863 else {
864 expire_client(conf);
865 clp = unconf;
866 move_to_confirmed(unconf);
867 status = nfs_ok;
868 }
869 goto out;
870 }
871 /* CASE 2:
872 * conf record that matches input clientid.
873 * if unconf record that matches input clientid, then unconf->cl_name
874 * or unconf->cl_verifier don't match the conf record.
875 */
876 if ((conf && !unconf) ||
877 ((conf && unconf) &&
878 (!cmp_verf(&conf->cl_verifier, &unconf->cl_verifier) ||
879 !cmp_name(&conf->cl_name, &unconf->cl_name)))) {
880 if (!cmp_creds(&conf->cl_cred,&rqstp->rq_cred)) {
881 status = nfserr_clid_inuse;
882 } else {
883 clp = conf;
884 status = nfs_ok;
885 }
886 goto out;
887 }
888 /* CASE 3:
889 * conf record not found.
890 * unconf record found.
891 * unconf->cl_confirm matches input confirm
892 */
893 if (!conf && unconf && cmp_verf(&unconf->cl_confirm, &confirm)) {
894 if (!cmp_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
895 status = nfserr_clid_inuse;
896 } else {
897 status = nfs_ok;
898 clp = unconf;
899 move_to_confirmed(unconf);
900 }
901 goto out;
902 }
903 /* CASE 4:
904 * conf record not found, or if conf, then conf->cl_confirm does not
905 * match input confirm.
906 * unconf record not found, or if unconf, then unconf->cl_confirm
907 * does not match input confirm.
908 */
909 if ((!conf || (conf && !cmp_verf(&conf->cl_confirm, &confirm))) &&
910 (!unconf || (unconf && !cmp_verf(&unconf->cl_confirm, &confirm)))) {
911 status = nfserr_stale_clientid;
912 goto out;
913 }
914 /* check that we have hit one of the cases...*/
915 status = nfserr_inval;
916 goto out;
917out:
918 if (!status)
919 nfsd4_probe_callback(clp);
920 nfs4_unlock_state();
921 return status;
922}
923
924/*
925 * Open owner state (share locks)
926 */
927
928/* hash tables for nfs4_stateowner */
929#define OWNER_HASH_BITS 8
930#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
931#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
932
933#define ownerid_hashval(id) \
934 ((id) & OWNER_HASH_MASK)
935#define ownerstr_hashval(clientid, ownername) \
936 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
937
938static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
939static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
940
941/* hash table for nfs4_file */
942#define FILE_HASH_BITS 8
943#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
944#define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
945/* hash table for (open)nfs4_stateid */
946#define STATEID_HASH_BITS 10
947#define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
948#define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
949
950#define file_hashval(x) \
951 hash_ptr(x, FILE_HASH_BITS)
952#define stateid_hashval(owner_id, file_id) \
953 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
954
955static struct list_head file_hashtbl[FILE_HASH_SIZE];
956static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
957
958/* OPEN Share state helper functions */
959static inline struct nfs4_file *
960alloc_init_file(struct inode *ino)
961{
962 struct nfs4_file *fp;
963 unsigned int hashval = file_hashval(ino);
964
e60d4398
N
965 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
966 if (fp) {
13cd2184 967 kref_init(&fp->fi_ref);
1da177e4 968 INIT_LIST_HEAD(&fp->fi_hash);
8beefa24
N
969 INIT_LIST_HEAD(&fp->fi_stateids);
970 INIT_LIST_HEAD(&fp->fi_delegations);
1da177e4
LT
971 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
972 fp->fi_inode = igrab(ino);
973 fp->fi_id = current_fileid++;
1da177e4
LT
974 return fp;
975 }
976 return NULL;
977}
978
e60d4398
N
979static void
980nfsd4_free_slab(kmem_cache_t **slab)
1da177e4 981{
e60d4398
N
982 int status;
983
984 if (*slab == NULL)
985 return;
986 status = kmem_cache_destroy(*slab);
987 *slab = NULL;
988 WARN_ON(status);
1da177e4
LT
989}
990
991static void
992nfsd4_free_slabs(void)
993{
e60d4398
N
994 nfsd4_free_slab(&stateowner_slab);
995 nfsd4_free_slab(&file_slab);
5ac049ac 996 nfsd4_free_slab(&stateid_slab);
5b2d21c1 997 nfsd4_free_slab(&deleg_slab);
e60d4398 998}
1da177e4 999
e60d4398
N
1000static int
1001nfsd4_init_slabs(void)
1002{
1003 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
1004 sizeof(struct nfs4_stateowner), 0, 0, NULL, NULL);
1005 if (stateowner_slab == NULL)
1006 goto out_nomem;
1007 file_slab = kmem_cache_create("nfsd4_files",
1008 sizeof(struct nfs4_file), 0, 0, NULL, NULL);
1009 if (file_slab == NULL)
1010 goto out_nomem;
5ac049ac
N
1011 stateid_slab = kmem_cache_create("nfsd4_stateids",
1012 sizeof(struct nfs4_stateid), 0, 0, NULL, NULL);
1013 if (stateid_slab == NULL)
1014 goto out_nomem;
5b2d21c1
N
1015 deleg_slab = kmem_cache_create("nfsd4_delegations",
1016 sizeof(struct nfs4_delegation), 0, 0, NULL, NULL);
1017 if (deleg_slab == NULL)
1018 goto out_nomem;
e60d4398
N
1019 return 0;
1020out_nomem:
1021 nfsd4_free_slabs();
1022 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1023 return -ENOMEM;
1da177e4
LT
1024}
1025
1026void
1027nfs4_free_stateowner(struct kref *kref)
1028{
1029 struct nfs4_stateowner *sop =
1030 container_of(kref, struct nfs4_stateowner, so_ref);
1031 kfree(sop->so_owner.data);
1032 kmem_cache_free(stateowner_slab, sop);
1033}
1034
1035static inline struct nfs4_stateowner *
1036alloc_stateowner(struct xdr_netobj *owner)
1037{
1038 struct nfs4_stateowner *sop;
1039
1040 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1041 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1042 memcpy(sop->so_owner.data, owner->data, owner->len);
1043 sop->so_owner.len = owner->len;
1044 kref_init(&sop->so_ref);
1045 return sop;
1046 }
1047 kmem_cache_free(stateowner_slab, sop);
1048 }
1049 return NULL;
1050}
1051
1052static struct nfs4_stateowner *
1053alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1054 struct nfs4_stateowner *sop;
1055 struct nfs4_replay *rp;
1056 unsigned int idhashval;
1057
1058 if (!(sop = alloc_stateowner(&open->op_owner)))
1059 return NULL;
1060 idhashval = ownerid_hashval(current_ownerid);
1061 INIT_LIST_HEAD(&sop->so_idhash);
1062 INIT_LIST_HEAD(&sop->so_strhash);
1063 INIT_LIST_HEAD(&sop->so_perclient);
1064 INIT_LIST_HEAD(&sop->so_perfilestate);
1065 INIT_LIST_HEAD(&sop->so_perlockowner); /* not used */
1066 INIT_LIST_HEAD(&sop->so_close_lru);
1067 sop->so_time = 0;
1068 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1069 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
1070 list_add(&sop->so_perclient, &clp->cl_perclient);
1da177e4
LT
1071 sop->so_is_open_owner = 1;
1072 sop->so_id = current_ownerid++;
1073 sop->so_client = clp;
1074 sop->so_seqid = open->op_seqid;
1075 sop->so_confirmed = 0;
1076 rp = &sop->so_replay;
1077 rp->rp_status = NFSERR_SERVERFAULT;
1078 rp->rp_buflen = 0;
1079 rp->rp_buf = rp->rp_ibuf;
1080 return sop;
1081}
1082
1083static void
1084release_stateid_lockowners(struct nfs4_stateid *open_stp)
1085{
1086 struct nfs4_stateowner *lock_sop;
1087
1088 while (!list_empty(&open_stp->st_perlockowner)) {
1089 lock_sop = list_entry(open_stp->st_perlockowner.next,
1090 struct nfs4_stateowner, so_perlockowner);
1091 /* list_del(&open_stp->st_perlockowner); */
1092 BUG_ON(lock_sop->so_is_open_owner);
1093 release_stateowner(lock_sop);
1094 }
1095}
1096
1097static void
1098unhash_stateowner(struct nfs4_stateowner *sop)
1099{
1100 struct nfs4_stateid *stp;
1101
1102 list_del(&sop->so_idhash);
1103 list_del(&sop->so_strhash);
6fa305de 1104 if (sop->so_is_open_owner)
1da177e4 1105 list_del(&sop->so_perclient);
1da177e4
LT
1106 list_del(&sop->so_perlockowner);
1107 while (!list_empty(&sop->so_perfilestate)) {
1108 stp = list_entry(sop->so_perfilestate.next,
1109 struct nfs4_stateid, st_perfilestate);
1110 if (sop->so_is_open_owner)
1111 release_stateid(stp, OPEN_STATE);
1112 else
1113 release_stateid(stp, LOCK_STATE);
1114 }
1115}
1116
1117static void
1118release_stateowner(struct nfs4_stateowner *sop)
1119{
1120 unhash_stateowner(sop);
1121 list_del(&sop->so_close_lru);
1122 nfs4_put_stateowner(sop);
1123}
1124
1125static inline void
1126init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1127 struct nfs4_stateowner *sop = open->op_stateowner;
1128 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1129
1130 INIT_LIST_HEAD(&stp->st_hash);
1131 INIT_LIST_HEAD(&stp->st_perfilestate);
1132 INIT_LIST_HEAD(&stp->st_perlockowner);
1133 INIT_LIST_HEAD(&stp->st_perfile);
1134 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
1135 list_add(&stp->st_perfilestate, &sop->so_perfilestate);
8beefa24 1136 list_add(&stp->st_perfile, &fp->fi_stateids);
1da177e4 1137 stp->st_stateowner = sop;
13cd2184 1138 get_nfs4_file(fp);
1da177e4
LT
1139 stp->st_file = fp;
1140 stp->st_stateid.si_boot = boot_time;
1141 stp->st_stateid.si_stateownerid = sop->so_id;
1142 stp->st_stateid.si_fileid = fp->fi_id;
1143 stp->st_stateid.si_generation = 0;
1144 stp->st_access_bmap = 0;
1145 stp->st_deny_bmap = 0;
1146 __set_bit(open->op_share_access, &stp->st_access_bmap);
1147 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
1148}
1149
1150static void
1151release_stateid(struct nfs4_stateid *stp, int flags)
1152{
1153 struct file *filp = stp->st_vfs_file;
1154
1155 list_del(&stp->st_hash);
1da177e4
LT
1156 list_del(&stp->st_perfile);
1157 list_del(&stp->st_perfilestate);
1158 if (flags & OPEN_STATE) {
1159 release_stateid_lockowners(stp);
1160 stp->st_vfs_file = NULL;
1161 nfsd_close(filp);
1da177e4
LT
1162 } else if (flags & LOCK_STATE)
1163 locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
13cd2184 1164 put_nfs4_file(stp->st_file);
5ac049ac 1165 kmem_cache_free(stateid_slab, stp);
1da177e4
LT
1166 stp = NULL;
1167}
1168
1da177e4
LT
1169void
1170move_to_close_lru(struct nfs4_stateowner *sop)
1171{
1172 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1173
1174 unhash_stateowner(sop);
1175 list_add_tail(&sop->so_close_lru, &close_lru);
1176 sop->so_time = get_seconds();
1177}
1178
1179void
1180release_state_owner(struct nfs4_stateid *stp, int flag)
1181{
1182 struct nfs4_stateowner *sop = stp->st_stateowner;
1da177e4
LT
1183
1184 dprintk("NFSD: release_state_owner\n");
1185 release_stateid(stp, flag);
1186
1187 /* place unused nfs4_stateowners on so_close_lru list to be
1188 * released by the laundromat service after the lease period
1189 * to enable us to handle CLOSE replay
1190 */
1191 if (sop->so_confirmed && list_empty(&sop->so_perfilestate))
1192 move_to_close_lru(sop);
1da177e4
LT
1193}
1194
1195static int
1196cmp_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, clientid_t *clid) {
1197 return ((sop->so_owner.len == owner->len) &&
1198 !memcmp(sop->so_owner.data, owner->data, owner->len) &&
1199 (sop->so_client->cl_clientid.cl_id == clid->cl_id));
1200}
1201
1202static struct nfs4_stateowner *
1203find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1204{
1205 struct nfs4_stateowner *so = NULL;
1206
1207 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
1208 if (cmp_owner_str(so, &open->op_owner, &open->op_clientid))
1209 return so;
1210 }
1211 return NULL;
1212}
1213
1214/* search file_hashtbl[] for file */
1215static struct nfs4_file *
1216find_file(struct inode *ino)
1217{
1218 unsigned int hashval = file_hashval(ino);
1219 struct nfs4_file *fp;
1220
1221 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
13cd2184
N
1222 if (fp->fi_inode == ino) {
1223 get_nfs4_file(fp);
1da177e4 1224 return fp;
13cd2184 1225 }
1da177e4
LT
1226 }
1227 return NULL;
1228}
1229
1230#define TEST_ACCESS(x) ((x > 0 || x < 4)?1:0)
1231#define TEST_DENY(x) ((x >= 0 || x < 5)?1:0)
1232
1233void
1234set_access(unsigned int *access, unsigned long bmap) {
1235 int i;
1236
1237 *access = 0;
1238 for (i = 1; i < 4; i++) {
1239 if (test_bit(i, &bmap))
1240 *access |= i;
1241 }
1242}
1243
1244void
1245set_deny(unsigned int *deny, unsigned long bmap) {
1246 int i;
1247
1248 *deny = 0;
1249 for (i = 0; i < 4; i++) {
1250 if (test_bit(i, &bmap))
1251 *deny |= i ;
1252 }
1253}
1254
1255static int
1256test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1257 unsigned int access, deny;
1258
1259 set_access(&access, stp->st_access_bmap);
1260 set_deny(&deny, stp->st_deny_bmap);
1261 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1262 return 0;
1263 return 1;
1264}
1265
1266/*
1267 * Called to check deny when READ with all zero stateid or
1268 * WRITE with all zero or all one stateid
1269 */
1270int
1271nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1272{
1273 struct inode *ino = current_fh->fh_dentry->d_inode;
1274 struct nfs4_file *fp;
1275 struct nfs4_stateid *stp;
13cd2184 1276 int ret;
1da177e4
LT
1277
1278 dprintk("NFSD: nfs4_share_conflict\n");
1279
1280 fp = find_file(ino);
13cd2184
N
1281 if (!fp)
1282 return nfs_ok;
1283 ret = nfserr_share_denied;
1da177e4 1284 /* Search for conflicting share reservations */
13cd2184
N
1285 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1286 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1287 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1288 goto out;
1da177e4 1289 }
13cd2184
N
1290 ret = nfs_ok;
1291out:
1292 put_nfs4_file(fp);
1293 return ret;
1da177e4
LT
1294}
1295
1296static inline void
1297nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1298{
1299 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1300 put_write_access(filp->f_dentry->d_inode);
1301 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1302 }
1303}
1304
1305/*
1306 * Recall a delegation
1307 */
1308static int
1309do_recall(void *__dp)
1310{
1311 struct nfs4_delegation *dp = __dp;
1312
1313 daemonize("nfsv4-recall");
1314
1315 nfsd4_cb_recall(dp);
1316 return 0;
1317}
1318
1319/*
1320 * Spawn a thread to perform a recall on the delegation represented
1321 * by the lease (file_lock)
1322 *
1323 * Called from break_lease() with lock_kernel() held.
1324 * Note: we assume break_lease will only call this *once* for any given
1325 * lease.
1326 */
1327static
1328void nfsd_break_deleg_cb(struct file_lock *fl)
1329{
1330 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1331 struct task_struct *t;
1332
1333 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1334 if (!dp)
1335 return;
1336
1337 /* We're assuming the state code never drops its reference
1338 * without first removing the lease. Since we're in this lease
1339 * callback (and since the lease code is serialized by the kernel
1340 * lock) we know the server hasn't removed the lease yet, we know
1341 * it's safe to take a reference: */
1342 atomic_inc(&dp->dl_count);
1343
1344 spin_lock(&recall_lock);
1345 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1346 spin_unlock(&recall_lock);
1347
1348 /* only place dl_time is set. protected by lock_kernel*/
1349 dp->dl_time = get_seconds();
1350
1351 /* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */
1352 fl->fl_break_time = jiffies + NFSD_LEASE_TIME * HZ;
1353
1354 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1355 if (IS_ERR(t)) {
1356 struct nfs4_client *clp = dp->dl_client;
1357
1358 printk(KERN_INFO "NFSD: Callback thread failed for "
1359 "for client (clientid %08x/%08x)\n",
1360 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1361 nfs4_put_delegation(dp);
1362 }
1363}
1364
1365/*
1366 * The file_lock is being reapd.
1367 *
1368 * Called by locks_free_lock() with lock_kernel() held.
1369 */
1370static
1371void nfsd_release_deleg_cb(struct file_lock *fl)
1372{
1373 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1374
1375 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1376
1377 if (!(fl->fl_flags & FL_LEASE) || !dp)
1378 return;
1379 dp->dl_flock = NULL;
1380}
1381
1382/*
1383 * Set the delegation file_lock back pointer.
1384 *
1385 * Called from __setlease() with lock_kernel() held.
1386 */
1387static
1388void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1389{
1390 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1391
1392 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1393 if (!dp)
1394 return;
1395 dp->dl_flock = new;
1396}
1397
1398/*
1399 * Called from __setlease() with lock_kernel() held
1400 */
1401static
1402int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1403{
1404 struct nfs4_delegation *onlistd =
1405 (struct nfs4_delegation *)onlist->fl_owner;
1406 struct nfs4_delegation *tryd =
1407 (struct nfs4_delegation *)try->fl_owner;
1408
1409 if (onlist->fl_lmops != try->fl_lmops)
1410 return 0;
1411
1412 return onlistd->dl_client == tryd->dl_client;
1413}
1414
1415
1416static
1417int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1418{
1419 if (arg & F_UNLCK)
1420 return lease_modify(onlist, arg);
1421 else
1422 return -EAGAIN;
1423}
1424
1425struct lock_manager_operations nfsd_lease_mng_ops = {
1426 .fl_break = nfsd_break_deleg_cb,
1427 .fl_release_private = nfsd_release_deleg_cb,
1428 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1429 .fl_mylease = nfsd_same_client_deleg_cb,
1430 .fl_change = nfsd_change_deleg_cb,
1431};
1432
1433
1434/*
1435 * nfsd4_process_open1()
1436 * lookup stateowner.
1437 * found:
1438 * check confirmed
1439 * confirmed:
1440 * check seqid
1441 * not confirmed:
1442 * delete owner
1443 * create new owner
1444 * notfound:
1445 * verify clientid
1446 * create new owner
1447 *
1448 * called with nfs4_lock_state() held.
1449 */
1450int
1451nfsd4_process_open1(struct nfsd4_open *open)
1452{
1453 int status;
1454 clientid_t *clientid = &open->op_clientid;
1455 struct nfs4_client *clp = NULL;
1456 unsigned int strhashval;
1457 struct nfs4_stateowner *sop = NULL;
1458
1459 status = nfserr_inval;
1460 if (!check_name(open->op_owner))
1461 goto out;
1462
1463 if (STALE_CLIENTID(&open->op_clientid))
1464 return nfserr_stale_clientid;
1465
1466 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1467 sop = find_openstateowner_str(strhashval, open);
1468 if (sop) {
1469 open->op_stateowner = sop;
1470 /* check for replay */
1471 if (open->op_seqid == sop->so_seqid){
1472 if (sop->so_replay.rp_buflen)
1473 return NFSERR_REPLAY_ME;
1474 else {
1475 /* The original OPEN failed so spectacularly
1476 * that we don't even have replay data saved!
1477 * Therefore, we have no choice but to continue
1478 * processing this OPEN; presumably, we'll
1479 * fail again for the same reason.
1480 */
1481 dprintk("nfsd4_process_open1:"
1482 " replay with no replay cache\n");
1483 goto renew;
1484 }
1485 } else if (sop->so_confirmed) {
1486 if (open->op_seqid == sop->so_seqid + 1)
1487 goto renew;
1488 status = nfserr_bad_seqid;
1489 goto out;
1490 } else {
1491 /* If we get here, we received an OPEN for an
1492 * unconfirmed nfs4_stateowner. Since the seqid's are
1493 * different, purge the existing nfs4_stateowner, and
1494 * instantiate a new one.
1495 */
1496 clp = sop->so_client;
1497 release_stateowner(sop);
1498 }
1499 } else {
1500 /* nfs4_stateowner not found.
1501 * Verify clientid and instantiate new nfs4_stateowner.
1502 * If verify fails this is presumably the result of the
1503 * client's lease expiring.
1504 */
1505 status = nfserr_expired;
1506 clp = find_confirmed_client(clientid);
1507 if (clp == NULL)
1508 goto out;
1509 }
1510 status = nfserr_resource;
1511 sop = alloc_init_open_stateowner(strhashval, clp, open);
1512 if (sop == NULL)
1513 goto out;
1514 open->op_stateowner = sop;
1515renew:
1516 status = nfs_ok;
1517 renew_client(sop->so_client);
1518out:
1519 if (status && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1520 status = nfserr_reclaim_bad;
1521 return status;
1522}
1523
4a6e43e6
N
1524static inline int
1525nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1526{
1527 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1528 return nfserr_openmode;
1529 else
1530 return nfs_ok;
1531}
1532
52f4fb43
N
1533static struct nfs4_delegation *
1534find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1535{
1536 struct nfs4_delegation *dp;
1537
8beefa24 1538 list_for_each_entry(dp, &fp->fi_delegations, dl_del_perfile) {
52f4fb43
N
1539 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1540 return dp;
1541 }
1542 return NULL;
1543}
1544
c44c5eeb 1545static int
567d9829
N
1546nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1547 struct nfs4_delegation **dp)
1548{
1549 int flags;
c44c5eeb 1550 int status = nfserr_bad_stateid;
567d9829
N
1551
1552 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1553 if (*dp == NULL)
c44c5eeb 1554 goto out;
567d9829
N
1555 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1556 RD_STATE : WR_STATE;
1557 status = nfs4_check_delegmode(*dp, flags);
1558 if (status)
1559 *dp = NULL;
c44c5eeb
N
1560out:
1561 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1562 return nfs_ok;
1563 if (status)
1564 return status;
1565 open->op_stateowner->so_confirmed = 1;
1566 return nfs_ok;
567d9829
N
1567}
1568
1da177e4
LT
1569static int
1570nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1571{
1572 struct nfs4_stateid *local;
1573 int status = nfserr_share_denied;
1574 struct nfs4_stateowner *sop = open->op_stateowner;
1575
8beefa24 1576 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
1da177e4
LT
1577 /* ignore lock owners */
1578 if (local->st_stateowner->so_is_open_owner == 0)
1579 continue;
1580 /* remember if we have seen this open owner */
1581 if (local->st_stateowner == sop)
1582 *stpp = local;
1583 /* check for conflicting share reservations */
1584 if (!test_share(local, open))
1585 goto out;
1586 }
1587 status = 0;
1588out:
1589 return status;
1590}
1591
5ac049ac
N
1592static inline struct nfs4_stateid *
1593nfs4_alloc_stateid(void)
1594{
1595 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
1596}
1597
1da177e4
LT
1598static int
1599nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
567d9829 1600 struct nfs4_delegation *dp,
1da177e4
LT
1601 struct svc_fh *cur_fh, int flags)
1602{
1603 struct nfs4_stateid *stp;
1da177e4 1604
5ac049ac 1605 stp = nfs4_alloc_stateid();
1da177e4
LT
1606 if (stp == NULL)
1607 return nfserr_resource;
1608
567d9829
N
1609 if (dp) {
1610 get_file(dp->dl_vfs_file);
1611 stp->st_vfs_file = dp->dl_vfs_file;
1612 } else {
1613 int status;
1614 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1615 &stp->st_vfs_file);
1616 if (status) {
1617 if (status == nfserr_dropit)
1618 status = nfserr_jukebox;
5ac049ac 1619 kmem_cache_free(stateid_slab, stp);
567d9829
N
1620 return status;
1621 }
1da177e4 1622 }
1da177e4
LT
1623 *stpp = stp;
1624 return 0;
1625}
1626
1627static inline int
1628nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1629 struct nfsd4_open *open)
1630{
1631 struct iattr iattr = {
1632 .ia_valid = ATTR_SIZE,
1633 .ia_size = 0,
1634 };
1635 if (!open->op_truncate)
1636 return 0;
1637 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
1638 return -EINVAL;
1639 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1640}
1641
1642static int
1643nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1644{
1645 struct file *filp = stp->st_vfs_file;
1646 struct inode *inode = filp->f_dentry->d_inode;
1647 unsigned int share_access;
1648 int status;
1649
1650 set_access(&share_access, stp->st_access_bmap);
1651 share_access = ~share_access;
1652 share_access &= open->op_share_access;
1653
1654 if (!(share_access & NFS4_SHARE_ACCESS_WRITE))
1655 return nfsd4_truncate(rqstp, cur_fh, open);
1656
1657 status = get_write_access(inode);
1658 if (status)
1659 return nfserrno(status);
1660 status = nfsd4_truncate(rqstp, cur_fh, open);
1661 if (status) {
1662 put_write_access(inode);
1663 return status;
1664 }
1665 /* remember the open */
1666 filp->f_mode = (filp->f_mode | FMODE_WRITE) & ~FMODE_READ;
1667 set_bit(open->op_share_access, &stp->st_access_bmap);
1668 set_bit(open->op_share_deny, &stp->st_deny_bmap);
1669
1670 return nfs_ok;
1671}
1672
1673
1674/* decrement seqid on successful reclaim, it will be bumped in encode_open */
1675static void
1676nfs4_set_claim_prev(struct nfsd4_open *open, int *status)
1677{
1678 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) {
1679 if (*status)
1680 *status = nfserr_reclaim_bad;
1681 else {
1682 open->op_stateowner->so_confirmed = 1;
1683 open->op_stateowner->so_seqid--;
1684 }
1685 }
1686}
1687
1688/*
1689 * Attempt to hand out a delegation.
1690 */
1691static void
1692nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1693{
1694 struct nfs4_delegation *dp;
1695 struct nfs4_stateowner *sop = stp->st_stateowner;
1696 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1697 struct file_lock fl, *flp = &fl;
1698 int status, flag = 0;
1699
1700 flag = NFS4_OPEN_DELEGATE_NONE;
7b190fec
N
1701 open->op_recall = 0;
1702 switch (open->op_claim_type) {
1703 case NFS4_OPEN_CLAIM_PREVIOUS:
1704 if (!atomic_read(&cb->cb_set))
1705 open->op_recall = 1;
1706 flag = open->op_delegate_type;
1707 if (flag == NFS4_OPEN_DELEGATE_NONE)
1708 goto out;
1709 break;
1710 case NFS4_OPEN_CLAIM_NULL:
1711 /* Let's not give out any delegations till everyone's
1712 * had the chance to reclaim theirs.... */
1713 if (nfs4_in_grace())
1714 goto out;
1715 if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
1716 goto out;
1717 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1718 flag = NFS4_OPEN_DELEGATE_WRITE;
1719 else
1720 flag = NFS4_OPEN_DELEGATE_READ;
1721 break;
1722 default:
1723 goto out;
1724 }
1da177e4
LT
1725
1726 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1727 if (dp == NULL) {
1728 flag = NFS4_OPEN_DELEGATE_NONE;
1729 goto out;
1730 }
1731 locks_init_lock(&fl);
1732 fl.fl_lmops = &nfsd_lease_mng_ops;
1733 fl.fl_flags = FL_LEASE;
1734 fl.fl_end = OFFSET_MAX;
1735 fl.fl_owner = (fl_owner_t)dp;
1736 fl.fl_file = stp->st_vfs_file;
1737 fl.fl_pid = current->tgid;
1738
1739 /* setlease checks to see if delegation should be handed out.
1740 * the lock_manager callbacks fl_mylease and fl_change are used
1741 */
1742 if ((status = setlease(stp->st_vfs_file,
1743 flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) {
1744 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
c907132d 1745 unhash_delegation(dp);
1da177e4
LT
1746 flag = NFS4_OPEN_DELEGATE_NONE;
1747 goto out;
1748 }
1749
1750 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1751
1752 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1753 dp->dl_stateid.si_boot,
1754 dp->dl_stateid.si_stateownerid,
1755 dp->dl_stateid.si_fileid,
1756 dp->dl_stateid.si_generation);
1757out:
7b190fec
N
1758 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
1759 && flag == NFS4_OPEN_DELEGATE_NONE
1760 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
1761 printk("NFSD: WARNING: refusing delegation reclaim\n");
1da177e4
LT
1762 open->op_delegate_type = flag;
1763}
1764
1765/*
1766 * called with nfs4_lock_state() held.
1767 */
1768int
1769nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1770{
1771 struct nfs4_file *fp = NULL;
1772 struct inode *ino = current_fh->fh_dentry->d_inode;
1773 struct nfs4_stateid *stp = NULL;
567d9829 1774 struct nfs4_delegation *dp = NULL;
1da177e4
LT
1775 int status;
1776
1777 status = nfserr_inval;
1778 if (!TEST_ACCESS(open->op_share_access) || !TEST_DENY(open->op_share_deny))
1779 goto out;
1780 /*
1781 * Lookup file; if found, lookup stateid and check open request,
1782 * and check for delegations in the process of being recalled.
1783 * If not found, create the nfs4_file struct
1784 */
1785 fp = find_file(ino);
1786 if (fp) {
1787 if ((status = nfs4_check_open(fp, open, &stp)))
1788 goto out;
c44c5eeb
N
1789 status = nfs4_check_deleg(fp, open, &dp);
1790 if (status)
1791 goto out;
1da177e4 1792 } else {
c44c5eeb
N
1793 status = nfserr_bad_stateid;
1794 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1795 goto out;
1da177e4
LT
1796 status = nfserr_resource;
1797 fp = alloc_init_file(ino);
1798 if (fp == NULL)
1799 goto out;
1800 }
1801
1802 /*
1803 * OPEN the file, or upgrade an existing OPEN.
1804 * If truncate fails, the OPEN fails.
1805 */
1806 if (stp) {
1807 /* Stateid was found, this is an OPEN upgrade */
1808 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1809 if (status)
1810 goto out;
1811 } else {
1812 /* Stateid was not found, this is a new OPEN */
1813 int flags = 0;
1814 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1815 flags = MAY_WRITE;
1816 else
1817 flags = MAY_READ;
567d9829
N
1818 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1819 if (status)
1da177e4
LT
1820 goto out;
1821 init_stateid(stp, fp, open);
1822 status = nfsd4_truncate(rqstp, current_fh, open);
1823 if (status) {
1824 release_stateid(stp, OPEN_STATE);
1825 goto out;
1826 }
1827 }
1828 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1829
1830 /*
1831 * Attempt to hand out a delegation. No error return, because the
1832 * OPEN succeeds even if we fail.
1833 */
1834 nfs4_open_delegation(current_fh, open, stp);
1835
1836 status = nfs_ok;
1837
1838 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1839 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1840 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1841out:
13cd2184
N
1842 if (fp)
1843 put_nfs4_file(fp);
1da177e4
LT
1844 /* CLAIM_PREVIOUS has different error returns */
1845 nfs4_set_claim_prev(open, &status);
1846 /*
1847 * To finish the open response, we just need to set the rflags.
1848 */
1849 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1850 if (!open->op_stateowner->so_confirmed)
1851 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1852
1853 return status;
1854}
1855
58da282b 1856static struct workqueue_struct *laundry_wq;
1da177e4
LT
1857static struct work_struct laundromat_work;
1858static void laundromat_main(void *);
1859static DECLARE_WORK(laundromat_work, laundromat_main, NULL);
1860
1861int
1862nfsd4_renew(clientid_t *clid)
1863{
1864 struct nfs4_client *clp;
1865 int status;
1866
1867 nfs4_lock_state();
1868 dprintk("process_renew(%08x/%08x): starting\n",
1869 clid->cl_boot, clid->cl_id);
1870 status = nfserr_stale_clientid;
1871 if (STALE_CLIENTID(clid))
1872 goto out;
1873 clp = find_confirmed_client(clid);
1874 status = nfserr_expired;
1875 if (clp == NULL) {
1876 /* We assume the client took too long to RENEW. */
1877 dprintk("nfsd4_renew: clientid not found!\n");
1878 goto out;
1879 }
1880 renew_client(clp);
1881 status = nfserr_cb_path_down;
1882 if (!list_empty(&clp->cl_del_perclnt)
1883 && !atomic_read(&clp->cl_callback.cb_set))
1884 goto out;
1885 status = nfs_ok;
1886out:
1887 nfs4_unlock_state();
1888 return status;
1889}
1890
1891time_t
1892nfs4_laundromat(void)
1893{
1894 struct nfs4_client *clp;
1895 struct nfs4_stateowner *sop;
1896 struct nfs4_delegation *dp;
1897 struct list_head *pos, *next, reaplist;
1898 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1899 time_t t, clientid_val = NFSD_LEASE_TIME;
1900 time_t u, test_val = NFSD_LEASE_TIME;
1901
1902 nfs4_lock_state();
1903
1904 dprintk("NFSD: laundromat service - starting\n");
1905 list_for_each_safe(pos, next, &client_lru) {
1906 clp = list_entry(pos, struct nfs4_client, cl_lru);
1907 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1908 t = clp->cl_time - cutoff;
1909 if (clientid_val > t)
1910 clientid_val = t;
1911 break;
1912 }
1913 dprintk("NFSD: purging unused client (clientid %08x)\n",
1914 clp->cl_clientid.cl_id);
1915 expire_client(clp);
1916 }
1917 INIT_LIST_HEAD(&reaplist);
1918 spin_lock(&recall_lock);
1919 list_for_each_safe(pos, next, &del_recall_lru) {
1920 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1921 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1922 u = dp->dl_time - cutoff;
1923 if (test_val > u)
1924 test_val = u;
1925 break;
1926 }
1927 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1928 dp, dp->dl_flock);
1929 list_move(&dp->dl_recall_lru, &reaplist);
1930 }
1931 spin_unlock(&recall_lock);
1932 list_for_each_safe(pos, next, &reaplist) {
1933 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1934 list_del_init(&dp->dl_recall_lru);
1935 unhash_delegation(dp);
1936 }
1937 test_val = NFSD_LEASE_TIME;
1938 list_for_each_safe(pos, next, &close_lru) {
1939 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1940 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1941 u = sop->so_time - cutoff;
1942 if (test_val > u)
1943 test_val = u;
1944 break;
1945 }
1946 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1947 sop->so_id);
1948 list_del(&sop->so_close_lru);
1949 nfs4_put_stateowner(sop);
1950 }
1951 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1952 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1953 nfs4_unlock_state();
1954 return clientid_val;
1955}
1956
1957void
1958laundromat_main(void *not_used)
1959{
1960 time_t t;
1961
1962 t = nfs4_laundromat();
1963 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
58da282b 1964 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
1da177e4
LT
1965}
1966
1967/* search ownerid_hashtbl[] and close_lru for stateid owner
1968 * (stateid->si_stateownerid)
1969 */
1970struct nfs4_stateowner *
1971find_openstateowner_id(u32 st_id, int flags) {
1972 struct nfs4_stateowner *local = NULL;
1973
1974 dprintk("NFSD: find_openstateowner_id %d\n", st_id);
1975 if (flags & CLOSE_STATE) {
1976 list_for_each_entry(local, &close_lru, so_close_lru) {
1977 if (local->so_id == st_id)
1978 return local;
1979 }
1980 }
1981 return NULL;
1982}
1983
1984static inline int
1985nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
1986{
1987 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_dentry->d_inode;
1988}
1989
1990static int
1991STALE_STATEID(stateid_t *stateid)
1992{
1993 if (stateid->si_boot == boot_time)
1994 return 0;
1995 printk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
1996 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
1997 stateid->si_generation);
1998 return 1;
1999}
2000
2001static inline int
2002access_permit_read(unsigned long access_bmap)
2003{
2004 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
2005 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
2006 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
2007}
2008
2009static inline int
2010access_permit_write(unsigned long access_bmap)
2011{
2012 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
2013 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
2014}
2015
2016static
2017int nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
2018{
2019 int status = nfserr_openmode;
2020
2021 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2022 goto out;
2023 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2024 goto out;
2025 status = nfs_ok;
2026out:
2027 return status;
2028}
2029
1da177e4
LT
2030static inline int
2031check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2032{
2033 /* Trying to call delegreturn with a special stateid? Yuch: */
2034 if (!(flags & (RD_STATE | WR_STATE)))
2035 return nfserr_bad_stateid;
2036 else if (ONE_STATEID(stateid) && (flags & RD_STATE))
2037 return nfs_ok;
2038 else if (nfs4_in_grace()) {
2039 /* Answer in remaining cases depends on existance of
2040 * conflicting state; so we must wait out the grace period. */
2041 return nfserr_grace;
2042 } else if (flags & WR_STATE)
2043 return nfs4_share_conflict(current_fh,
2044 NFS4_SHARE_DENY_WRITE);
2045 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2046 return nfs4_share_conflict(current_fh,
2047 NFS4_SHARE_DENY_READ);
2048}
2049
2050/*
2051 * Allow READ/WRITE during grace period on recovered state only for files
2052 * that are not able to provide mandatory locking.
2053 */
2054static inline int
2055io_during_grace_disallowed(struct inode *inode, int flags)
2056{
2057 return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE))
2058 && MANDATORY_LOCK(inode);
2059}
2060
2061/*
2062* Checks for stateid operations
2063*/
2064int
2065nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2066{
2067 struct nfs4_stateid *stp = NULL;
2068 struct nfs4_delegation *dp = NULL;
2069 stateid_t *stidp;
2070 struct inode *ino = current_fh->fh_dentry->d_inode;
2071 int status;
2072
2073 dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2074 stateid->si_boot, stateid->si_stateownerid,
2075 stateid->si_fileid, stateid->si_generation);
2076 if (filpp)
2077 *filpp = NULL;
2078
2079 if (io_during_grace_disallowed(ino, flags))
2080 return nfserr_grace;
2081
2082 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2083 return check_special_stateids(current_fh, stateid, flags);
2084
2085 /* STALE STATEID */
2086 status = nfserr_stale_stateid;
2087 if (STALE_STATEID(stateid))
2088 goto out;
2089
2090 /* BAD STATEID */
2091 status = nfserr_bad_stateid;
2092 if (!stateid->si_fileid) { /* delegation stateid */
2093 if(!(dp = find_delegation_stateid(ino, stateid))) {
2094 dprintk("NFSD: delegation stateid not found\n");
2095 if (nfs4_in_grace())
2096 status = nfserr_grace;
2097 goto out;
2098 }
2099 stidp = &dp->dl_stateid;
2100 } else { /* open or lock stateid */
2101 if (!(stp = find_stateid(stateid, flags))) {
2102 dprintk("NFSD: open or lock stateid not found\n");
2103 if (nfs4_in_grace())
2104 status = nfserr_grace;
2105 goto out;
2106 }
2107 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
2108 goto out;
2109 if (!stp->st_stateowner->so_confirmed)
2110 goto out;
2111 stidp = &stp->st_stateid;
2112 }
2113 if (stateid->si_generation > stidp->si_generation)
2114 goto out;
2115
2116 /* OLD STATEID */
2117 status = nfserr_old_stateid;
2118 if (stateid->si_generation < stidp->si_generation)
2119 goto out;
2120 if (stp) {
2121 if ((status = nfs4_check_openmode(stp,flags)))
2122 goto out;
2123 renew_client(stp->st_stateowner->so_client);
2124 if (filpp)
2125 *filpp = stp->st_vfs_file;
2126 } else if (dp) {
2127 if ((status = nfs4_check_delegmode(dp, flags)))
2128 goto out;
2129 renew_client(dp->dl_client);
2130 if (flags & DELEG_RET)
2131 unhash_delegation(dp);
2132 if (filpp)
2133 *filpp = dp->dl_vfs_file;
2134 }
2135 status = nfs_ok;
2136out:
2137 return status;
2138}
2139
2140
2141/*
2142 * Checks for sequence id mutating operations.
2143 */
2144int
2145nfs4_preprocess_seqid_op(struct svc_fh *current_fh, u32 seqid, stateid_t *stateid, int flags, struct nfs4_stateowner **sopp, struct nfs4_stateid **stpp, clientid_t *lockclid)
2146{
2147 int status;
2148 struct nfs4_stateid *stp;
2149 struct nfs4_stateowner *sop;
2150
2151 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2152 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2153 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2154 stateid->si_generation);
2155
2156 *stpp = NULL;
2157 *sopp = NULL;
2158
2159 status = nfserr_bad_stateid;
2160 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2161 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
2162 goto out;
2163 }
2164
2165 status = nfserr_stale_stateid;
2166 if (STALE_STATEID(stateid))
2167 goto out;
2168 /*
2169 * We return BAD_STATEID if filehandle doesn't match stateid,
2170 * the confirmed flag is incorrecly set, or the generation
2171 * number is incorrect.
2172 * If there is no entry in the openfile table for this id,
2173 * we can't always return BAD_STATEID;
2174 * this might be a retransmitted CLOSE which has arrived after
2175 * the openfile has been released.
2176 */
2177 if (!(stp = find_stateid(stateid, flags)))
2178 goto no_nfs4_stateid;
2179
2180 status = nfserr_bad_stateid;
2181
2182 /* for new lock stateowners:
2183 * check that the lock->v.new.open_stateid
2184 * refers to an open stateowner
2185 *
2186 * check that the lockclid (nfs4_lock->v.new.clientid) is the same
2187 * as the open_stateid->st_stateowner->so_client->clientid
2188 */
2189 if (lockclid) {
2190 struct nfs4_stateowner *sop = stp->st_stateowner;
2191 struct nfs4_client *clp = sop->so_client;
2192
2193 if (!sop->so_is_open_owner)
2194 goto out;
2195 if (!cmp_clid(&clp->cl_clientid, lockclid))
2196 goto out;
2197 }
2198
2199 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
2200 printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
2201 goto out;
2202 }
2203
2204 *stpp = stp;
2205 *sopp = sop = stp->st_stateowner;
2206
2207 /*
2208 * We now validate the seqid and stateid generation numbers.
2209 * For the moment, we ignore the possibility of
2210 * generation number wraparound.
2211 */
2212 if (seqid != sop->so_seqid + 1)
2213 goto check_replay;
2214
2215 if (sop->so_confirmed) {
2216 if (flags & CONFIRM) {
2217 printk("NFSD: preprocess_seqid_op: expected unconfirmed stateowner!\n");
2218 goto out;
2219 }
2220 }
2221 else {
2222 if (!(flags & CONFIRM)) {
2223 printk("NFSD: preprocess_seqid_op: stateowner not confirmed yet!\n");
2224 goto out;
2225 }
2226 }
2227 if (stateid->si_generation > stp->st_stateid.si_generation) {
2228 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
2229 goto out;
2230 }
2231
2232 status = nfserr_old_stateid;
2233 if (stateid->si_generation < stp->st_stateid.si_generation) {
2234 printk("NFSD: preprocess_seqid_op: old stateid!\n");
2235 goto out;
2236 }
2237 /* XXX renew the client lease here */
2238 status = nfs_ok;
2239
2240out:
2241 return status;
2242
2243no_nfs4_stateid:
2244
2245 /*
2246 * We determine whether this is a bad stateid or a replay,
2247 * starting by trying to look up the stateowner.
2248 * If stateowner is not found - stateid is bad.
2249 */
2250 if (!(sop = find_openstateowner_id(stateid->si_stateownerid, flags))) {
2251 printk("NFSD: preprocess_seqid_op: no stateowner or nfs4_stateid!\n");
2252 status = nfserr_bad_stateid;
2253 goto out;
2254 }
2255 *sopp = sop;
2256
2257check_replay:
2258 if (seqid == sop->so_seqid) {
2259 printk("NFSD: preprocess_seqid_op: retransmission?\n");
2260 /* indicate replay to calling function */
2261 status = NFSERR_REPLAY_ME;
2262 } else {
2263 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d\n", sop->so_seqid +1, seqid);
2264
2265 *sopp = NULL;
2266 status = nfserr_bad_seqid;
2267 }
2268 goto out;
2269}
2270
2271int
2272nfsd4_open_confirm(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_confirm *oc)
2273{
2274 int status;
2275 struct nfs4_stateowner *sop;
2276 struct nfs4_stateid *stp;
2277
2278 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
2279 (int)current_fh->fh_dentry->d_name.len,
2280 current_fh->fh_dentry->d_name.name);
2281
2282 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2283 goto out;
2284
2285 nfs4_lock_state();
2286
2287 if ((status = nfs4_preprocess_seqid_op(current_fh, oc->oc_seqid,
2288 &oc->oc_req_stateid,
2289 CHECK_FH | CONFIRM | OPEN_STATE,
2290 &oc->oc_stateowner, &stp, NULL)))
2291 goto out;
2292
2293 sop = oc->oc_stateowner;
2294 sop->so_confirmed = 1;
2295 update_stateid(&stp->st_stateid);
2296 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2297 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2298 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2299 stp->st_stateid.si_boot,
2300 stp->st_stateid.si_stateownerid,
2301 stp->st_stateid.si_fileid,
2302 stp->st_stateid.si_generation);
2303out:
2304 if (oc->oc_stateowner)
2305 nfs4_get_stateowner(oc->oc_stateowner);
2306 nfs4_unlock_state();
2307 return status;
2308}
2309
2310
2311/*
2312 * unset all bits in union bitmap (bmap) that
2313 * do not exist in share (from successful OPEN_DOWNGRADE)
2314 */
2315static void
2316reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2317{
2318 int i;
2319 for (i = 1; i < 4; i++) {
2320 if ((i & access) != i)
2321 __clear_bit(i, bmap);
2322 }
2323}
2324
2325static void
2326reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2327{
2328 int i;
2329 for (i = 0; i < 4; i++) {
2330 if ((i & deny) != i)
2331 __clear_bit(i, bmap);
2332 }
2333}
2334
2335int
2336nfsd4_open_downgrade(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_downgrade *od)
2337{
2338 int status;
2339 struct nfs4_stateid *stp;
2340 unsigned int share_access;
2341
2342 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
2343 (int)current_fh->fh_dentry->d_name.len,
2344 current_fh->fh_dentry->d_name.name);
2345
2346 if (!TEST_ACCESS(od->od_share_access) || !TEST_DENY(od->od_share_deny))
2347 return nfserr_inval;
2348
2349 nfs4_lock_state();
2350 if ((status = nfs4_preprocess_seqid_op(current_fh, od->od_seqid,
2351 &od->od_stateid,
2352 CHECK_FH | OPEN_STATE,
2353 &od->od_stateowner, &stp, NULL)))
2354 goto out;
2355
2356 status = nfserr_inval;
2357 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2358 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2359 stp->st_access_bmap, od->od_share_access);
2360 goto out;
2361 }
2362 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2363 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2364 stp->st_deny_bmap, od->od_share_deny);
2365 goto out;
2366 }
2367 set_access(&share_access, stp->st_access_bmap);
2368 nfs4_file_downgrade(stp->st_vfs_file,
2369 share_access & ~od->od_share_access);
2370
2371 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2372 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2373
2374 update_stateid(&stp->st_stateid);
2375 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2376 status = nfs_ok;
2377out:
2378 if (od->od_stateowner)
2379 nfs4_get_stateowner(od->od_stateowner);
2380 nfs4_unlock_state();
2381 return status;
2382}
2383
2384/*
2385 * nfs4_unlock_state() called after encode
2386 */
2387int
2388nfsd4_close(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_close *close)
2389{
2390 int status;
2391 struct nfs4_stateid *stp;
2392
2393 dprintk("NFSD: nfsd4_close on file %.*s\n",
2394 (int)current_fh->fh_dentry->d_name.len,
2395 current_fh->fh_dentry->d_name.name);
2396
2397 nfs4_lock_state();
2398 /* check close_lru for replay */
2399 if ((status = nfs4_preprocess_seqid_op(current_fh, close->cl_seqid,
2400 &close->cl_stateid,
2401 CHECK_FH | OPEN_STATE | CLOSE_STATE,
2402 &close->cl_stateowner, &stp, NULL)))
2403 goto out;
2404 /*
2405 * Return success, but first update the stateid.
2406 */
2407 status = nfs_ok;
2408 update_stateid(&stp->st_stateid);
2409 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2410
2411 /* release_state_owner() calls nfsd_close() if needed */
2412 release_state_owner(stp, OPEN_STATE);
2413out:
2414 if (close->cl_stateowner)
2415 nfs4_get_stateowner(close->cl_stateowner);
2416 nfs4_unlock_state();
2417 return status;
2418}
2419
2420int
2421nfsd4_delegreturn(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_delegreturn *dr)
2422{
2423 int status;
2424
2425 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2426 goto out;
2427
2428 nfs4_lock_state();
2429 status = nfs4_preprocess_stateid_op(current_fh, &dr->dr_stateid, DELEG_RET, NULL);
2430 nfs4_unlock_state();
2431out:
2432 return status;
2433}
2434
2435
2436/*
2437 * Lock owner state (byte-range locks)
2438 */
2439#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2440#define LOCK_HASH_BITS 8
2441#define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2442#define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2443
2444#define lockownerid_hashval(id) \
2445 ((id) & LOCK_HASH_MASK)
2446
2447static inline unsigned int
2448lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2449 struct xdr_netobj *ownername)
2450{
2451 return (file_hashval(inode) + cl_id
2452 + opaque_hashval(ownername->data, ownername->len))
2453 & LOCK_HASH_MASK;
2454}
2455
2456static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2457static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2458static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2459
2460struct nfs4_stateid *
2461find_stateid(stateid_t *stid, int flags)
2462{
2463 struct nfs4_stateid *local = NULL;
2464 u32 st_id = stid->si_stateownerid;
2465 u32 f_id = stid->si_fileid;
2466 unsigned int hashval;
2467
2468 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
2469 if ((flags & LOCK_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2470 hashval = stateid_hashval(st_id, f_id);
2471 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2472 if ((local->st_stateid.si_stateownerid == st_id) &&
2473 (local->st_stateid.si_fileid == f_id))
2474 return local;
2475 }
2476 }
2477 if ((flags & OPEN_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2478 hashval = stateid_hashval(st_id, f_id);
2479 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2480 if ((local->st_stateid.si_stateownerid == st_id) &&
2481 (local->st_stateid.si_fileid == f_id))
2482 return local;
2483 }
2484 } else
2485 printk("NFSD: find_stateid: ERROR: no state flag\n");
2486 return NULL;
2487}
2488
2489static struct nfs4_delegation *
2490find_delegation_stateid(struct inode *ino, stateid_t *stid)
2491{
13cd2184
N
2492 struct nfs4_file *fp;
2493 struct nfs4_delegation *dl;
1da177e4
LT
2494
2495 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2496 stid->si_boot, stid->si_stateownerid,
2497 stid->si_fileid, stid->si_generation);
2498
1da177e4 2499 fp = find_file(ino);
13cd2184
N
2500 if (!fp)
2501 return NULL;
2502 dl = find_delegation_file(fp, stid);
2503 put_nfs4_file(fp);
2504 return dl;
1da177e4
LT
2505}
2506
2507/*
2508 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2509 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2510 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2511 * locking, this prevents us from being completely protocol-compliant. The
2512 * real solution to this problem is to start using unsigned file offsets in
2513 * the VFS, but this is a very deep change!
2514 */
2515static inline void
2516nfs4_transform_lock_offset(struct file_lock *lock)
2517{
2518 if (lock->fl_start < 0)
2519 lock->fl_start = OFFSET_MAX;
2520 if (lock->fl_end < 0)
2521 lock->fl_end = OFFSET_MAX;
2522}
2523
2524int
2525nfs4_verify_lock_stateowner(struct nfs4_stateowner *sop, unsigned int hashval)
2526{
2527 struct nfs4_stateowner *local = NULL;
2528 int status = 0;
2529
2530 if (hashval >= LOCK_HASH_SIZE)
2531 goto out;
2532 list_for_each_entry(local, &lock_ownerid_hashtbl[hashval], so_idhash) {
2533 if (local == sop) {
2534 status = 1;
2535 goto out;
2536 }
2537 }
2538out:
2539 return status;
2540}
2541
2542
2543static inline void
2544nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2545{
2546 struct nfs4_stateowner *sop = (struct nfs4_stateowner *) fl->fl_owner;
2547 unsigned int hval = lockownerid_hashval(sop->so_id);
2548
2549 deny->ld_sop = NULL;
2550 if (nfs4_verify_lock_stateowner(sop, hval)) {
2551 kref_get(&sop->so_ref);
2552 deny->ld_sop = sop;
2553 deny->ld_clientid = sop->so_client->cl_clientid;
2554 }
2555 deny->ld_start = fl->fl_start;
2556 deny->ld_length = ~(u64)0;
2557 if (fl->fl_end != ~(u64)0)
2558 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2559 deny->ld_type = NFS4_READ_LT;
2560 if (fl->fl_type != F_RDLCK)
2561 deny->ld_type = NFS4_WRITE_LT;
2562}
2563
2564static struct nfs4_stateowner *
2565find_lockstateowner(struct xdr_netobj *owner, clientid_t *clid)
2566{
2567 struct nfs4_stateowner *local = NULL;
2568 int i;
2569
2570 for (i = 0; i < LOCK_HASH_SIZE; i++) {
2571 list_for_each_entry(local, &lock_ownerid_hashtbl[i], so_idhash) {
2572 if (!cmp_owner_str(local, owner, clid))
2573 continue;
2574 return local;
2575 }
2576 }
2577 return NULL;
2578}
2579
2580static struct nfs4_stateowner *
2581find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2582 struct xdr_netobj *owner)
2583{
2584 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2585 struct nfs4_stateowner *op;
2586
2587 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
2588 if (cmp_owner_str(op, owner, clid))
2589 return op;
2590 }
2591 return NULL;
2592}
2593
2594/*
2595 * Alloc a lock owner structure.
2596 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2597 * occured.
2598 *
2599 * strhashval = lock_ownerstr_hashval
2600 * so_seqid = lock->lk_new_lock_seqid - 1: it gets bumped in encode
2601 */
2602
2603static struct nfs4_stateowner *
2604alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2605 struct nfs4_stateowner *sop;
2606 struct nfs4_replay *rp;
2607 unsigned int idhashval;
2608
2609 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2610 return NULL;
2611 idhashval = lockownerid_hashval(current_ownerid);
2612 INIT_LIST_HEAD(&sop->so_idhash);
2613 INIT_LIST_HEAD(&sop->so_strhash);
2614 INIT_LIST_HEAD(&sop->so_perclient);
2615 INIT_LIST_HEAD(&sop->so_perfilestate);
2616 INIT_LIST_HEAD(&sop->so_perlockowner);
2617 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2618 sop->so_time = 0;
2619 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2620 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
2621 list_add(&sop->so_perlockowner, &open_stp->st_perlockowner);
2622 sop->so_is_open_owner = 0;
2623 sop->so_id = current_ownerid++;
2624 sop->so_client = clp;
2625 sop->so_seqid = lock->lk_new_lock_seqid - 1;
2626 sop->so_confirmed = 1;
2627 rp = &sop->so_replay;
2628 rp->rp_status = NFSERR_SERVERFAULT;
2629 rp->rp_buflen = 0;
2630 rp->rp_buf = rp->rp_ibuf;
2631 return sop;
2632}
2633
2634struct nfs4_stateid *
2635alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2636{
2637 struct nfs4_stateid *stp;
2638 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2639
5ac049ac
N
2640 stp = nfs4_alloc_stateid();
2641 if (stp == NULL)
1da177e4
LT
2642 goto out;
2643 INIT_LIST_HEAD(&stp->st_hash);
2644 INIT_LIST_HEAD(&stp->st_perfile);
2645 INIT_LIST_HEAD(&stp->st_perfilestate);
2646 INIT_LIST_HEAD(&stp->st_perlockowner); /* not used */
2647 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
8beefa24 2648 list_add(&stp->st_perfile, &fp->fi_stateids);
1da177e4
LT
2649 list_add(&stp->st_perfilestate, &sop->so_perfilestate);
2650 stp->st_stateowner = sop;
13cd2184 2651 get_nfs4_file(fp);
1da177e4
LT
2652 stp->st_file = fp;
2653 stp->st_stateid.si_boot = boot_time;
2654 stp->st_stateid.si_stateownerid = sop->so_id;
2655 stp->st_stateid.si_fileid = fp->fi_id;
2656 stp->st_stateid.si_generation = 0;
2657 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2658 stp->st_access_bmap = open_stp->st_access_bmap;
2659 stp->st_deny_bmap = open_stp->st_deny_bmap;
2660
2661out:
2662 return stp;
2663}
2664
2665int
2666check_lock_length(u64 offset, u64 length)
2667{
2668 return ((length == 0) || ((length != ~(u64)0) &&
2669 LOFF_OVERFLOW(offset, length)));
2670}
2671
2672/*
2673 * LOCK operation
2674 */
2675int
2676nfsd4_lock(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lock *lock)
2677{
2678 struct nfs4_stateowner *lock_sop = NULL, *open_sop = NULL;
2679 struct nfs4_stateid *lock_stp;
2680 struct file *filp;
2681 struct file_lock file_lock;
2682 struct file_lock *conflock;
2683 int status = 0;
2684 unsigned int strhashval;
2685
2686 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2687 (long long) lock->lk_offset,
2688 (long long) lock->lk_length);
2689
2690 if (nfs4_in_grace() && !lock->lk_reclaim)
2691 return nfserr_grace;
2692 if (!nfs4_in_grace() && lock->lk_reclaim)
2693 return nfserr_no_grace;
2694
2695 if (check_lock_length(lock->lk_offset, lock->lk_length))
2696 return nfserr_inval;
2697
2698 nfs4_lock_state();
2699
2700 if (lock->lk_is_new) {
2701 /*
2702 * Client indicates that this is a new lockowner.
2703 * Use open owner and open stateid to create lock owner and lock
2704 * stateid.
2705 */
2706 struct nfs4_stateid *open_stp = NULL;
2707 struct nfs4_file *fp;
2708
2709 status = nfserr_stale_clientid;
2710 if (STALE_CLIENTID(&lock->lk_new_clientid)) {
2711 printk("NFSD: nfsd4_lock: clientid is stale!\n");
2712 goto out;
2713 }
2714
2715 /* is the new lock seqid presented by the client zero? */
2716 status = nfserr_bad_seqid;
2717 if (lock->v.new.lock_seqid != 0)
2718 goto out;
2719
2720 /* validate and update open stateid and open seqid */
2721 status = nfs4_preprocess_seqid_op(current_fh,
2722 lock->lk_new_open_seqid,
2723 &lock->lk_new_open_stateid,
2724 CHECK_FH | OPEN_STATE,
2725 &open_sop, &open_stp,
2726 &lock->v.new.clientid);
2727 if (status) {
2728 if (lock->lk_reclaim)
2729 status = nfserr_reclaim_bad;
2730 goto out;
2731 }
2732 /* create lockowner and lock stateid */
2733 fp = open_stp->st_file;
2734 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2735 open_sop->so_client->cl_clientid.cl_id,
2736 &lock->v.new.owner);
2737 /*
2738 * If we already have this lock owner, the client is in
2739 * error (or our bookeeping is wrong!)
2740 * for asking for a 'new lock'.
2741 */
2742 status = nfserr_bad_stateid;
2743 lock_sop = find_lockstateowner(&lock->v.new.owner,
2744 &lock->v.new.clientid);
2745 if (lock_sop)
2746 goto out;
2747 status = nfserr_resource;
2748 if (!(lock->lk_stateowner = alloc_init_lock_stateowner(strhashval, open_sop->so_client, open_stp, lock)))
2749 goto out;
2750 if ((lock_stp = alloc_init_lock_stateid(lock->lk_stateowner,
2751 fp, open_stp)) == NULL) {
2752 release_stateowner(lock->lk_stateowner);
2753 lock->lk_stateowner = NULL;
2754 goto out;
2755 }
2756 /* bump the open seqid used to create the lock */
2757 open_sop->so_seqid++;
2758 } else {
2759 /* lock (lock owner + lock stateid) already exists */
2760 status = nfs4_preprocess_seqid_op(current_fh,
2761 lock->lk_old_lock_seqid,
2762 &lock->lk_old_lock_stateid,
2763 CHECK_FH | LOCK_STATE,
2764 &lock->lk_stateowner, &lock_stp, NULL);
2765 if (status)
2766 goto out;
2767 }
2768 /* lock->lk_stateowner and lock_stp have been created or found */
2769 filp = lock_stp->st_vfs_file;
2770
2771 if ((status = fh_verify(rqstp, current_fh, S_IFREG, MAY_LOCK))) {
2772 printk("NFSD: nfsd4_lock: permission denied!\n");
2773 goto out;
2774 }
2775
2776 locks_init_lock(&file_lock);
2777 switch (lock->lk_type) {
2778 case NFS4_READ_LT:
2779 case NFS4_READW_LT:
2780 file_lock.fl_type = F_RDLCK;
2781 break;
2782 case NFS4_WRITE_LT:
2783 case NFS4_WRITEW_LT:
2784 file_lock.fl_type = F_WRLCK;
2785 break;
2786 default:
2787 status = nfserr_inval;
2788 goto out;
2789 }
2790 file_lock.fl_owner = (fl_owner_t) lock->lk_stateowner;
2791 file_lock.fl_pid = current->tgid;
2792 file_lock.fl_file = filp;
2793 file_lock.fl_flags = FL_POSIX;
2794
2795 file_lock.fl_start = lock->lk_offset;
2796 if ((lock->lk_length == ~(u64)0) ||
2797 LOFF_OVERFLOW(lock->lk_offset, lock->lk_length))
2798 file_lock.fl_end = ~(u64)0;
2799 else
2800 file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
2801 nfs4_transform_lock_offset(&file_lock);
2802
2803 /*
2804 * Try to lock the file in the VFS.
2805 * Note: locks.c uses the BKL to protect the inode's lock list.
2806 */
2807
2808 status = posix_lock_file(filp, &file_lock);
2809 if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
2810 file_lock.fl_ops->fl_release_private(&file_lock);
2811 dprintk("NFSD: nfsd4_lock: posix_lock_file status %d\n",status);
2812 switch (-status) {
2813 case 0: /* success! */
2814 update_stateid(&lock_stp->st_stateid);
2815 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2816 sizeof(stateid_t));
2817 goto out;
2818 case (EAGAIN):
2819 goto conflicting_lock;
2820 case (EDEADLK):
2821 status = nfserr_deadlock;
2822 default:
2823 dprintk("NFSD: nfsd4_lock: posix_lock_file() failed! status %d\n",status);
2824 goto out_destroy_new_stateid;
2825 }
2826
2827conflicting_lock:
2828 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2829 status = nfserr_denied;
2830 /* XXX There is a race here. Future patch needed to provide
2831 * an atomic posix_lock_and_test_file
2832 */
2833 if (!(conflock = posix_test_lock(filp, &file_lock))) {
2834 status = nfserr_serverfault;
2835 goto out;
2836 }
2837 nfs4_set_lock_denied(conflock, &lock->lk_denied);
2838
2839out_destroy_new_stateid:
2840 if (lock->lk_is_new) {
2841 dprintk("NFSD: nfsd4_lock: destroy new stateid!\n");
2842 /*
2843 * An error encountered after instantiation of the new
2844 * stateid has forced us to destroy it.
2845 */
2846 if (!seqid_mutating_err(status))
2847 open_sop->so_seqid--;
2848
2849 release_state_owner(lock_stp, LOCK_STATE);
2850 }
2851out:
2852 if (lock->lk_stateowner)
2853 nfs4_get_stateowner(lock->lk_stateowner);
2854 nfs4_unlock_state();
2855 return status;
2856}
2857
2858/*
2859 * LOCKT operation
2860 */
2861int
2862nfsd4_lockt(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lockt *lockt)
2863{
2864 struct inode *inode;
2865 struct file file;
2866 struct file_lock file_lock;
2867 struct file_lock *conflicting_lock;
2868 int status;
2869
2870 if (nfs4_in_grace())
2871 return nfserr_grace;
2872
2873 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2874 return nfserr_inval;
2875
2876 lockt->lt_stateowner = NULL;
2877 nfs4_lock_state();
2878
2879 status = nfserr_stale_clientid;
2880 if (STALE_CLIENTID(&lockt->lt_clientid)) {
2881 printk("NFSD: nfsd4_lockt: clientid is stale!\n");
2882 goto out;
2883 }
2884
2885 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0))) {
2886 printk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
2887 if (status == nfserr_symlink)
2888 status = nfserr_inval;
2889 goto out;
2890 }
2891
2892 inode = current_fh->fh_dentry->d_inode;
2893 locks_init_lock(&file_lock);
2894 switch (lockt->lt_type) {
2895 case NFS4_READ_LT:
2896 case NFS4_READW_LT:
2897 file_lock.fl_type = F_RDLCK;
2898 break;
2899 case NFS4_WRITE_LT:
2900 case NFS4_WRITEW_LT:
2901 file_lock.fl_type = F_WRLCK;
2902 break;
2903 default:
2904 printk("NFSD: nfs4_lockt: bad lock type!\n");
2905 status = nfserr_inval;
2906 goto out;
2907 }
2908
2909 lockt->lt_stateowner = find_lockstateowner_str(inode,
2910 &lockt->lt_clientid, &lockt->lt_owner);
2911 if (lockt->lt_stateowner)
2912 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2913 file_lock.fl_pid = current->tgid;
2914 file_lock.fl_flags = FL_POSIX;
2915
2916 file_lock.fl_start = lockt->lt_offset;
2917 if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length))
2918 file_lock.fl_end = ~(u64)0;
2919 else
2920 file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
2921
2922 nfs4_transform_lock_offset(&file_lock);
2923
2924 /* posix_test_lock uses the struct file _only_ to resolve the inode.
2925 * since LOCKT doesn't require an OPEN, and therefore a struct
2926 * file may not exist, pass posix_test_lock a struct file with
2927 * only the dentry:inode set.
2928 */
2929 memset(&file, 0, sizeof (struct file));
2930 file.f_dentry = current_fh->fh_dentry;
2931
2932 status = nfs_ok;
2933 conflicting_lock = posix_test_lock(&file, &file_lock);
2934 if (conflicting_lock) {
2935 status = nfserr_denied;
2936 nfs4_set_lock_denied(conflicting_lock, &lockt->lt_denied);
2937 }
2938out:
2939 nfs4_unlock_state();
2940 return status;
2941}
2942
2943int
2944nfsd4_locku(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_locku *locku)
2945{
2946 struct nfs4_stateid *stp;
2947 struct file *filp = NULL;
2948 struct file_lock file_lock;
2949 int status;
2950
2951 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2952 (long long) locku->lu_offset,
2953 (long long) locku->lu_length);
2954
2955 if (check_lock_length(locku->lu_offset, locku->lu_length))
2956 return nfserr_inval;
2957
2958 nfs4_lock_state();
2959
2960 if ((status = nfs4_preprocess_seqid_op(current_fh,
2961 locku->lu_seqid,
2962 &locku->lu_stateid,
2963 CHECK_FH | LOCK_STATE,
2964 &locku->lu_stateowner, &stp, NULL)))
2965 goto out;
2966
2967 filp = stp->st_vfs_file;
2968 BUG_ON(!filp);
2969 locks_init_lock(&file_lock);
2970 file_lock.fl_type = F_UNLCK;
2971 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
2972 file_lock.fl_pid = current->tgid;
2973 file_lock.fl_file = filp;
2974 file_lock.fl_flags = FL_POSIX;
2975 file_lock.fl_start = locku->lu_offset;
2976
2977 if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
2978 file_lock.fl_end = ~(u64)0;
2979 else
2980 file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
2981 nfs4_transform_lock_offset(&file_lock);
2982
2983 /*
2984 * Try to unlock the file in the VFS.
2985 */
2986 status = posix_lock_file(filp, &file_lock);
2987 if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
2988 file_lock.fl_ops->fl_release_private(&file_lock);
2989 if (status) {
2990 printk("NFSD: nfs4_locku: posix_lock_file failed!\n");
2991 goto out_nfserr;
2992 }
2993 /*
2994 * OK, unlock succeeded; the only thing left to do is update the stateid.
2995 */
2996 update_stateid(&stp->st_stateid);
2997 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
2998
2999out:
3000 if (locku->lu_stateowner)
3001 nfs4_get_stateowner(locku->lu_stateowner);
3002 nfs4_unlock_state();
3003 return status;
3004
3005out_nfserr:
3006 status = nfserrno(status);
3007 goto out;
3008}
3009
3010/*
3011 * returns
3012 * 1: locks held by lockowner
3013 * 0: no locks held by lockowner
3014 */
3015static int
3016check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
3017{
3018 struct file_lock **flpp;
3019 struct inode *inode = filp->f_dentry->d_inode;
3020 int status = 0;
3021
3022 lock_kernel();
3023 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
3024 if ((*flpp)->fl_owner == (fl_owner_t)lowner)
3025 status = 1;
3026 goto out;
3027 }
3028out:
3029 unlock_kernel();
3030 return status;
3031}
3032
3033int
3034nfsd4_release_lockowner(struct svc_rqst *rqstp, struct nfsd4_release_lockowner *rlockowner)
3035{
3036 clientid_t *clid = &rlockowner->rl_clientid;
3037 struct nfs4_stateowner *local = NULL;
3038 struct xdr_netobj *owner = &rlockowner->rl_owner;
3039 int status;
3040
3041 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
3042 clid->cl_boot, clid->cl_id);
3043
3044 /* XXX check for lease expiration */
3045
3046 status = nfserr_stale_clientid;
3047 if (STALE_CLIENTID(clid)) {
3048 printk("NFSD: nfsd4_release_lockowner: clientid is stale!\n");
3049 return status;
3050 }
3051
3052 nfs4_lock_state();
3053
3054 status = nfs_ok;
3055 local = find_lockstateowner(owner, clid);
3056 if (local) {
3057 struct nfs4_stateid *stp;
3058
3059 /* check for any locks held by any stateid
3060 * associated with the (lock) stateowner */
3061 status = nfserr_locks_held;
3062 list_for_each_entry(stp, &local->so_perfilestate,
3063 st_perfilestate) {
3064 if (check_for_locks(stp->st_vfs_file, local))
3065 goto out;
3066 }
3067 /* no locks held by (lock) stateowner */
3068 status = nfs_ok;
3069 release_stateowner(local);
3070 }
3071out:
3072 nfs4_unlock_state();
3073 return status;
3074}
3075
3076static inline struct nfs4_client_reclaim *
3077alloc_reclaim(int namelen)
3078{
3079 struct nfs4_client_reclaim *crp = NULL;
3080
3081 crp = kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
3082 if (!crp)
3083 return NULL;
3084 crp->cr_name.data = kmalloc(namelen, GFP_KERNEL);
3085 if (!crp->cr_name.data) {
3086 kfree(crp);
3087 return NULL;
3088 }
3089 return crp;
3090}
3091
3092/*
3093 * failure => all reset bets are off, nfserr_no_grace...
3094 */
3095static int
3096nfs4_client_to_reclaim(char *name, int namlen)
3097{
3098 unsigned int strhashval;
3099 struct nfs4_client_reclaim *crp = NULL;
3100
3101 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", namlen, name);
3102 crp = alloc_reclaim(namlen);
3103 if (!crp)
3104 return 0;
3105 strhashval = clientstr_hashval(name, namlen);
3106 INIT_LIST_HEAD(&crp->cr_strhash);
3107 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
3108 memcpy(crp->cr_name.data, name, namlen);
3109 crp->cr_name.len = namlen;
3110 reclaim_str_hashtbl_size++;
3111 return 1;
3112}
3113
3114static void
3115nfs4_release_reclaim(void)
3116{
3117 struct nfs4_client_reclaim *crp = NULL;
3118 int i;
3119
1da177e4
LT
3120 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3121 while (!list_empty(&reclaim_str_hashtbl[i])) {
3122 crp = list_entry(reclaim_str_hashtbl[i].next,
3123 struct nfs4_client_reclaim, cr_strhash);
3124 list_del(&crp->cr_strhash);
3125 kfree(crp->cr_name.data);
3126 kfree(crp);
3127 reclaim_str_hashtbl_size--;
3128 }
3129 }
3130 BUG_ON(reclaim_str_hashtbl_size);
3131}
3132
3133/*
3134 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
3135struct nfs4_client_reclaim *
3136nfs4_find_reclaim_client(clientid_t *clid)
3137{
3138 unsigned int strhashval;
3139 struct nfs4_client *clp;
3140 struct nfs4_client_reclaim *crp = NULL;
3141
3142
3143 /* find clientid in conf_id_hashtbl */
3144 clp = find_confirmed_client(clid);
3145 if (clp == NULL)
3146 return NULL;
3147
3148 dprintk("NFSD: nfs4_find_reclaim_client for %.*s\n",
3149 clp->cl_name.len, clp->cl_name.data);
3150
3151 /* find clp->cl_name in reclaim_str_hashtbl */
3152 strhashval = clientstr_hashval(clp->cl_name.data, clp->cl_name.len);
3153 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
3154 if (cmp_name(&crp->cr_name, &clp->cl_name)) {
3155 return crp;
3156 }
3157 }
3158 return NULL;
3159}
3160
3161/*
3162* Called from OPEN. Look for clientid in reclaim list.
3163*/
3164int
3165nfs4_check_open_reclaim(clientid_t *clid)
3166{
dfc83565 3167 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
1da177e4
LT
3168}
3169
ac4d8ff2 3170/* initialization to perform at module load time: */
1da177e4 3171
ac4d8ff2
N
3172void
3173nfs4_state_init(void)
1da177e4
LT
3174{
3175 int i;
1da177e4 3176
1da177e4
LT
3177 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3178 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3179 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3180 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3181 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3182 }
3183 for (i = 0; i < FILE_HASH_SIZE; i++) {
3184 INIT_LIST_HEAD(&file_hashtbl[i]);
3185 }
3186 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3187 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3188 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3189 }
3190 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3191 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3192 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3193 }
3194 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3195 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3196 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3197 }
1da177e4 3198 memset(&onestateid, ~0, sizeof(stateid_t));
1da177e4
LT
3199 INIT_LIST_HEAD(&close_lru);
3200 INIT_LIST_HEAD(&client_lru);
3201 INIT_LIST_HEAD(&del_recall_lru);
ac4d8ff2
N
3202 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3203 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3204 reclaim_str_hashtbl_size = 0;
ac4d8ff2
N
3205}
3206
3207/* initialization to perform when the nfsd service is started: */
3208
3209static void
3210__nfs4_state_start(void)
3211{
3212 time_t grace_time;
3213
1da177e4 3214 boot_time = get_seconds();
d99a05ad
N
3215 grace_time = max(user_lease_time, lease_time);
3216 lease_time = user_lease_time;
3217 printk("NFSD: starting %ld-second grace period\n", grace_time);
1da177e4 3218 grace_end = boot_time + grace_time;
58da282b
N
3219 laundry_wq = create_singlethread_workqueue("nfsd4");
3220 queue_delayed_work(laundry_wq, &laundromat_work, NFSD_LEASE_TIME*HZ);
1da177e4
LT
3221}
3222
3223int
76a3550e 3224nfs4_state_start(void)
1da177e4
LT
3225{
3226 int status;
3227
3228 if (nfs4_init)
3229 return 0;
3230 status = nfsd4_init_slabs();
3231 if (status)
3232 return status;
76a3550e 3233 __nfs4_state_start();
1da177e4
LT
3234 nfs4_init = 1;
3235 return 0;
3236}
3237
3238int
3239nfs4_in_grace(void)
3240{
3241 return get_seconds() < grace_end;
3242}
3243
3244void
3245set_no_grace(void)
3246{
3247 printk("NFSD: ERROR in reboot recovery. State reclaims will fail.\n");
3248 grace_end = get_seconds();
3249}
3250
3251time_t
3252nfs4_lease_time(void)
3253{
3254 return lease_time;
3255}
3256
3257static void
3258__nfs4_state_shutdown(void)
3259{
3260 int i;
3261 struct nfs4_client *clp = NULL;
3262 struct nfs4_delegation *dp = NULL;
3263 struct nfs4_stateowner *sop = NULL;
3264 struct list_head *pos, *next, reaplist;
3265
3266 list_for_each_safe(pos, next, &close_lru) {
3267 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
3268 list_del(&sop->so_close_lru);
3269 nfs4_put_stateowner(sop);
3270 }
3271
3272 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3273 while (!list_empty(&conf_id_hashtbl[i])) {
3274 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3275 expire_client(clp);
3276 }
3277 while (!list_empty(&unconf_str_hashtbl[i])) {
3278 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3279 expire_client(clp);
3280 }
3281 }
3282 INIT_LIST_HEAD(&reaplist);
3283 spin_lock(&recall_lock);
3284 list_for_each_safe(pos, next, &del_recall_lru) {
3285 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3286 list_move(&dp->dl_recall_lru, &reaplist);
3287 }
3288 spin_unlock(&recall_lock);
3289 list_for_each_safe(pos, next, &reaplist) {
3290 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3291 list_del_init(&dp->dl_recall_lru);
3292 unhash_delegation(dp);
3293 }
3294
1da177e4 3295 cancel_delayed_work(&laundromat_work);
58da282b
N
3296 flush_workqueue(laundry_wq);
3297 destroy_workqueue(laundry_wq);
1da177e4 3298 nfs4_init = 0;
1da177e4
LT
3299}
3300
3301void
3302nfs4_state_shutdown(void)
3303{
3304 nfs4_lock_state();
3305 nfs4_release_reclaim();
3306 __nfs4_state_shutdown();
3307 nfsd4_free_slabs();
3308 nfs4_unlock_state();
3309}
3310
3311/*
3312 * Called when leasetime is changed.
3313 *
d99a05ad
N
3314 * The only way the protocol gives us to handle on-the-fly lease changes is to
3315 * simulate a reboot. Instead of doing that, we just wait till the next time
3316 * we start to register any changes in lease time. If the administrator
3317 * really wants to change the lease time *now*, they can go ahead and bring
3318 * nfsd down and then back up again after changing the lease time.
1da177e4
LT
3319 */
3320void
3321nfs4_reset_lease(time_t leasetime)
3322{
d99a05ad
N
3323 lock_kernel();
3324 user_lease_time = leasetime;
3325 unlock_kernel();
1da177e4 3326}