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