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