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