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