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