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