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