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