Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mfashe...
[deliverable/linux.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 */
34af946a 126static DEFINE_SPINLOCK(recall_lock);
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
ba5a6a19
BF
1240static int access_valid(u32 x)
1241{
1242 return (x > 0 && x < 4);
1243}
1244
1245static int deny_valid(u32 x)
1246{
1247 return (x >= 0 && x < 5);
1248}
1da177e4 1249
fd39ca9a 1250static void
1da177e4
LT
1251set_access(unsigned int *access, unsigned long bmap) {
1252 int i;
1253
1254 *access = 0;
1255 for (i = 1; i < 4; i++) {
1256 if (test_bit(i, &bmap))
1257 *access |= i;
1258 }
1259}
1260
fd39ca9a 1261static void
1da177e4
LT
1262set_deny(unsigned int *deny, unsigned long bmap) {
1263 int i;
1264
1265 *deny = 0;
1266 for (i = 0; i < 4; i++) {
1267 if (test_bit(i, &bmap))
1268 *deny |= i ;
1269 }
1270}
1271
1272static int
1273test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1274 unsigned int access, deny;
1275
1276 set_access(&access, stp->st_access_bmap);
1277 set_deny(&deny, stp->st_deny_bmap);
1278 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1279 return 0;
1280 return 1;
1281}
1282
1283/*
1284 * Called to check deny when READ with all zero stateid or
1285 * WRITE with all zero or all one stateid
1286 */
fd39ca9a 1287static int
1da177e4
LT
1288nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1289{
1290 struct inode *ino = current_fh->fh_dentry->d_inode;
1291 struct nfs4_file *fp;
1292 struct nfs4_stateid *stp;
13cd2184 1293 int ret;
1da177e4
LT
1294
1295 dprintk("NFSD: nfs4_share_conflict\n");
1296
1297 fp = find_file(ino);
13cd2184
N
1298 if (!fp)
1299 return nfs_ok;
b700949b 1300 ret = nfserr_locked;
1da177e4 1301 /* Search for conflicting share reservations */
13cd2184
N
1302 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1303 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1304 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1305 goto out;
1da177e4 1306 }
13cd2184
N
1307 ret = nfs_ok;
1308out:
1309 put_nfs4_file(fp);
1310 return ret;
1da177e4
LT
1311}
1312
1313static inline void
1314nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1315{
1316 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1317 put_write_access(filp->f_dentry->d_inode);
1318 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1319 }
1320}
1321
1322/*
1323 * Recall a delegation
1324 */
1325static int
1326do_recall(void *__dp)
1327{
1328 struct nfs4_delegation *dp = __dp;
1329
1330 daemonize("nfsv4-recall");
1331
1332 nfsd4_cb_recall(dp);
1333 return 0;
1334}
1335
1336/*
1337 * Spawn a thread to perform a recall on the delegation represented
1338 * by the lease (file_lock)
1339 *
1340 * Called from break_lease() with lock_kernel() held.
1341 * Note: we assume break_lease will only call this *once* for any given
1342 * lease.
1343 */
1344static
1345void nfsd_break_deleg_cb(struct file_lock *fl)
1346{
1347 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1348 struct task_struct *t;
1349
1350 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1351 if (!dp)
1352 return;
1353
1354 /* We're assuming the state code never drops its reference
1355 * without first removing the lease. Since we're in this lease
1356 * callback (and since the lease code is serialized by the kernel
1357 * lock) we know the server hasn't removed the lease yet, we know
1358 * it's safe to take a reference: */
1359 atomic_inc(&dp->dl_count);
1360
1361 spin_lock(&recall_lock);
1362 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1363 spin_unlock(&recall_lock);
1364
1365 /* only place dl_time is set. protected by lock_kernel*/
1366 dp->dl_time = get_seconds();
1367
1368 /* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */
1369 fl->fl_break_time = jiffies + NFSD_LEASE_TIME * HZ;
1370
1371 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1372 if (IS_ERR(t)) {
1373 struct nfs4_client *clp = dp->dl_client;
1374
1375 printk(KERN_INFO "NFSD: Callback thread failed for "
1376 "for client (clientid %08x/%08x)\n",
1377 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1378 nfs4_put_delegation(dp);
1379 }
1380}
1381
1382/*
1383 * The file_lock is being reapd.
1384 *
1385 * Called by locks_free_lock() with lock_kernel() held.
1386 */
1387static
1388void nfsd_release_deleg_cb(struct file_lock *fl)
1389{
1390 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1391
1392 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1393
1394 if (!(fl->fl_flags & FL_LEASE) || !dp)
1395 return;
1396 dp->dl_flock = NULL;
1397}
1398
1399/*
1400 * Set the delegation file_lock back pointer.
1401 *
1402 * Called from __setlease() with lock_kernel() held.
1403 */
1404static
1405void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1406{
1407 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1408
1409 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1410 if (!dp)
1411 return;
1412 dp->dl_flock = new;
1413}
1414
1415/*
1416 * Called from __setlease() with lock_kernel() held
1417 */
1418static
1419int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1420{
1421 struct nfs4_delegation *onlistd =
1422 (struct nfs4_delegation *)onlist->fl_owner;
1423 struct nfs4_delegation *tryd =
1424 (struct nfs4_delegation *)try->fl_owner;
1425
1426 if (onlist->fl_lmops != try->fl_lmops)
1427 return 0;
1428
1429 return onlistd->dl_client == tryd->dl_client;
1430}
1431
1432
1433static
1434int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1435{
1436 if (arg & F_UNLCK)
1437 return lease_modify(onlist, arg);
1438 else
1439 return -EAGAIN;
1440}
1441
fd39ca9a 1442static struct lock_manager_operations nfsd_lease_mng_ops = {
1da177e4
LT
1443 .fl_break = nfsd_break_deleg_cb,
1444 .fl_release_private = nfsd_release_deleg_cb,
1445 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1446 .fl_mylease = nfsd_same_client_deleg_cb,
1447 .fl_change = nfsd_change_deleg_cb,
1448};
1449
1450
1da177e4
LT
1451int
1452nfsd4_process_open1(struct nfsd4_open *open)
1453{
1da177e4
LT
1454 clientid_t *clientid = &open->op_clientid;
1455 struct nfs4_client *clp = NULL;
1456 unsigned int strhashval;
1457 struct nfs4_stateowner *sop = NULL;
1458
1da177e4 1459 if (!check_name(open->op_owner))
0f442aa2 1460 return nfserr_inval;
1da177e4
LT
1461
1462 if (STALE_CLIENTID(&open->op_clientid))
1463 return nfserr_stale_clientid;
1464
1465 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1466 sop = find_openstateowner_str(strhashval, open);
0f442aa2
BF
1467 open->op_stateowner = sop;
1468 if (!sop) {
1469 /* Make sure the client's lease hasn't expired. */
1da177e4
LT
1470 clp = find_confirmed_client(clientid);
1471 if (clp == NULL)
0f442aa2
BF
1472 return nfserr_expired;
1473 goto renew;
1da177e4 1474 }
0f442aa2
BF
1475 if (!sop->so_confirmed) {
1476 /* Replace unconfirmed owners without checking for replay. */
1477 clp = sop->so_client;
1478 release_stateowner(sop);
1479 open->op_stateowner = NULL;
1480 goto renew;
1481 }
1482 if (open->op_seqid == sop->so_seqid - 1) {
1483 if (sop->so_replay.rp_buflen)
1484 return NFSERR_REPLAY_ME;
1485 /* The original OPEN failed so spectacularly
1486 * that we don't even have replay data saved!
1487 * Therefore, we have no choice but to continue
1488 * processing this OPEN; presumably, we'll
1489 * fail again for the same reason.
1490 */
1491 dprintk("nfsd4_process_open1: replay with no replay cache\n");
1492 goto renew;
1493 }
1494 if (open->op_seqid != sop->so_seqid)
1495 return nfserr_bad_seqid;
1da177e4 1496renew:
0f442aa2
BF
1497 if (open->op_stateowner == NULL) {
1498 sop = alloc_init_open_stateowner(strhashval, clp, open);
1499 if (sop == NULL)
1500 return nfserr_resource;
1501 open->op_stateowner = sop;
1502 }
1503 list_del_init(&sop->so_close_lru);
1da177e4 1504 renew_client(sop->so_client);
0f442aa2 1505 return nfs_ok;
1da177e4
LT
1506}
1507
4a6e43e6
N
1508static inline int
1509nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1510{
1511 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1512 return nfserr_openmode;
1513 else
1514 return nfs_ok;
1515}
1516
52f4fb43
N
1517static struct nfs4_delegation *
1518find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1519{
1520 struct nfs4_delegation *dp;
1521
ea1da636 1522 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) {
52f4fb43
N
1523 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1524 return dp;
1525 }
1526 return NULL;
1527}
1528
c44c5eeb 1529static int
567d9829
N
1530nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1531 struct nfs4_delegation **dp)
1532{
1533 int flags;
c44c5eeb 1534 int status = nfserr_bad_stateid;
567d9829
N
1535
1536 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1537 if (*dp == NULL)
c44c5eeb 1538 goto out;
567d9829
N
1539 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1540 RD_STATE : WR_STATE;
1541 status = nfs4_check_delegmode(*dp, flags);
1542 if (status)
1543 *dp = NULL;
c44c5eeb
N
1544out:
1545 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1546 return nfs_ok;
1547 if (status)
1548 return status;
1549 open->op_stateowner->so_confirmed = 1;
1550 return nfs_ok;
567d9829
N
1551}
1552
1da177e4
LT
1553static int
1554nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1555{
1556 struct nfs4_stateid *local;
1557 int status = nfserr_share_denied;
1558 struct nfs4_stateowner *sop = open->op_stateowner;
1559
8beefa24 1560 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
1da177e4
LT
1561 /* ignore lock owners */
1562 if (local->st_stateowner->so_is_open_owner == 0)
1563 continue;
1564 /* remember if we have seen this open owner */
1565 if (local->st_stateowner == sop)
1566 *stpp = local;
1567 /* check for conflicting share reservations */
1568 if (!test_share(local, open))
1569 goto out;
1570 }
1571 status = 0;
1572out:
1573 return status;
1574}
1575
5ac049ac
N
1576static inline struct nfs4_stateid *
1577nfs4_alloc_stateid(void)
1578{
1579 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
1580}
1581
1da177e4
LT
1582static int
1583nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
567d9829 1584 struct nfs4_delegation *dp,
1da177e4
LT
1585 struct svc_fh *cur_fh, int flags)
1586{
1587 struct nfs4_stateid *stp;
1da177e4 1588
5ac049ac 1589 stp = nfs4_alloc_stateid();
1da177e4
LT
1590 if (stp == NULL)
1591 return nfserr_resource;
1592
567d9829
N
1593 if (dp) {
1594 get_file(dp->dl_vfs_file);
1595 stp->st_vfs_file = dp->dl_vfs_file;
1596 } else {
1597 int status;
1598 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1599 &stp->st_vfs_file);
1600 if (status) {
1601 if (status == nfserr_dropit)
1602 status = nfserr_jukebox;
5ac049ac 1603 kmem_cache_free(stateid_slab, stp);
567d9829
N
1604 return status;
1605 }
1da177e4 1606 }
1da177e4
LT
1607 *stpp = stp;
1608 return 0;
1609}
1610
1611static inline int
1612nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1613 struct nfsd4_open *open)
1614{
1615 struct iattr iattr = {
1616 .ia_valid = ATTR_SIZE,
1617 .ia_size = 0,
1618 };
1619 if (!open->op_truncate)
1620 return 0;
1621 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
9246585a 1622 return nfserr_inval;
1da177e4
LT
1623 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1624}
1625
1626static int
1627nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1628{
1629 struct file *filp = stp->st_vfs_file;
1630 struct inode *inode = filp->f_dentry->d_inode;
6c26d08f 1631 unsigned int share_access, new_writer;
1da177e4
LT
1632 int status;
1633
1634 set_access(&share_access, stp->st_access_bmap);
6c26d08f
BF
1635 new_writer = (~share_access) & open->op_share_access
1636 & NFS4_SHARE_ACCESS_WRITE;
1da177e4 1637
6c26d08f
BF
1638 if (new_writer) {
1639 status = get_write_access(inode);
1640 if (status)
1641 return nfserrno(status);
1642 }
1da177e4
LT
1643 status = nfsd4_truncate(rqstp, cur_fh, open);
1644 if (status) {
6c26d08f
BF
1645 if (new_writer)
1646 put_write_access(inode);
1da177e4
LT
1647 return status;
1648 }
1649 /* remember the open */
6c26d08f 1650 filp->f_mode |= open->op_share_access;
1da177e4
LT
1651 set_bit(open->op_share_access, &stp->st_access_bmap);
1652 set_bit(open->op_share_deny, &stp->st_deny_bmap);
1653
1654 return nfs_ok;
1655}
1656
1657
1da177e4 1658static void
37515177 1659nfs4_set_claim_prev(struct nfsd4_open *open)
1da177e4 1660{
37515177
N
1661 open->op_stateowner->so_confirmed = 1;
1662 open->op_stateowner->so_client->cl_firststate = 1;
1da177e4
LT
1663}
1664
1665/*
1666 * Attempt to hand out a delegation.
1667 */
1668static void
1669nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1670{
1671 struct nfs4_delegation *dp;
1672 struct nfs4_stateowner *sop = stp->st_stateowner;
1673 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1674 struct file_lock fl, *flp = &fl;
1675 int status, flag = 0;
1676
1677 flag = NFS4_OPEN_DELEGATE_NONE;
7b190fec
N
1678 open->op_recall = 0;
1679 switch (open->op_claim_type) {
1680 case NFS4_OPEN_CLAIM_PREVIOUS:
1681 if (!atomic_read(&cb->cb_set))
1682 open->op_recall = 1;
1683 flag = open->op_delegate_type;
1684 if (flag == NFS4_OPEN_DELEGATE_NONE)
1685 goto out;
1686 break;
1687 case NFS4_OPEN_CLAIM_NULL:
1688 /* Let's not give out any delegations till everyone's
1689 * had the chance to reclaim theirs.... */
1690 if (nfs4_in_grace())
1691 goto out;
1692 if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
1693 goto out;
1694 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1695 flag = NFS4_OPEN_DELEGATE_WRITE;
1696 else
1697 flag = NFS4_OPEN_DELEGATE_READ;
1698 break;
1699 default:
1700 goto out;
1701 }
1da177e4
LT
1702
1703 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1704 if (dp == NULL) {
1705 flag = NFS4_OPEN_DELEGATE_NONE;
1706 goto out;
1707 }
1708 locks_init_lock(&fl);
1709 fl.fl_lmops = &nfsd_lease_mng_ops;
1710 fl.fl_flags = FL_LEASE;
1711 fl.fl_end = OFFSET_MAX;
1712 fl.fl_owner = (fl_owner_t)dp;
1713 fl.fl_file = stp->st_vfs_file;
1714 fl.fl_pid = current->tgid;
1715
1716 /* setlease checks to see if delegation should be handed out.
1717 * the lock_manager callbacks fl_mylease and fl_change are used
1718 */
1719 if ((status = setlease(stp->st_vfs_file,
1720 flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) {
1721 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
c907132d 1722 unhash_delegation(dp);
1da177e4
LT
1723 flag = NFS4_OPEN_DELEGATE_NONE;
1724 goto out;
1725 }
1726
1727 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1728
1729 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1730 dp->dl_stateid.si_boot,
1731 dp->dl_stateid.si_stateownerid,
1732 dp->dl_stateid.si_fileid,
1733 dp->dl_stateid.si_generation);
1734out:
7b190fec
N
1735 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
1736 && flag == NFS4_OPEN_DELEGATE_NONE
1737 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
1738 printk("NFSD: WARNING: refusing delegation reclaim\n");
1da177e4
LT
1739 open->op_delegate_type = flag;
1740}
1741
1742/*
1743 * called with nfs4_lock_state() held.
1744 */
1745int
1746nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1747{
1748 struct nfs4_file *fp = NULL;
1749 struct inode *ino = current_fh->fh_dentry->d_inode;
1750 struct nfs4_stateid *stp = NULL;
567d9829 1751 struct nfs4_delegation *dp = NULL;
1da177e4
LT
1752 int status;
1753
1754 status = nfserr_inval;
ba5a6a19
BF
1755 if (!access_valid(open->op_share_access)
1756 || !deny_valid(open->op_share_deny))
1da177e4
LT
1757 goto out;
1758 /*
1759 * Lookup file; if found, lookup stateid and check open request,
1760 * and check for delegations in the process of being recalled.
1761 * If not found, create the nfs4_file struct
1762 */
1763 fp = find_file(ino);
1764 if (fp) {
1765 if ((status = nfs4_check_open(fp, open, &stp)))
1766 goto out;
c44c5eeb
N
1767 status = nfs4_check_deleg(fp, open, &dp);
1768 if (status)
1769 goto out;
1da177e4 1770 } else {
c44c5eeb
N
1771 status = nfserr_bad_stateid;
1772 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1773 goto out;
1da177e4
LT
1774 status = nfserr_resource;
1775 fp = alloc_init_file(ino);
1776 if (fp == NULL)
1777 goto out;
1778 }
1779
1780 /*
1781 * OPEN the file, or upgrade an existing OPEN.
1782 * If truncate fails, the OPEN fails.
1783 */
1784 if (stp) {
1785 /* Stateid was found, this is an OPEN upgrade */
1786 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1787 if (status)
1788 goto out;
444c2c07 1789 update_stateid(&stp->st_stateid);
1da177e4
LT
1790 } else {
1791 /* Stateid was not found, this is a new OPEN */
1792 int flags = 0;
9ecb6a08
BF
1793 if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
1794 flags |= MAY_READ;
1da177e4 1795 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
9ecb6a08 1796 flags |= MAY_WRITE;
567d9829
N
1797 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1798 if (status)
1da177e4
LT
1799 goto out;
1800 init_stateid(stp, fp, open);
1801 status = nfsd4_truncate(rqstp, current_fh, open);
1802 if (status) {
1803 release_stateid(stp, OPEN_STATE);
1804 goto out;
1805 }
1806 }
1807 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1808
1809 /*
1810 * Attempt to hand out a delegation. No error return, because the
1811 * OPEN succeeds even if we fail.
1812 */
1813 nfs4_open_delegation(current_fh, open, stp);
1814
1815 status = nfs_ok;
1816
1817 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1818 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1819 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1820out:
13cd2184
N
1821 if (fp)
1822 put_nfs4_file(fp);
37515177
N
1823 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1824 nfs4_set_claim_prev(open);
1da177e4
LT
1825 /*
1826 * To finish the open response, we just need to set the rflags.
1827 */
1828 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1829 if (!open->op_stateowner->so_confirmed)
1830 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1831
1832 return status;
1833}
1834
58da282b 1835static struct workqueue_struct *laundry_wq;
1da177e4
LT
1836static struct work_struct laundromat_work;
1837static void laundromat_main(void *);
1838static DECLARE_WORK(laundromat_work, laundromat_main, NULL);
1839
1840int
1841nfsd4_renew(clientid_t *clid)
1842{
1843 struct nfs4_client *clp;
1844 int status;
1845
1846 nfs4_lock_state();
1847 dprintk("process_renew(%08x/%08x): starting\n",
1848 clid->cl_boot, clid->cl_id);
1849 status = nfserr_stale_clientid;
1850 if (STALE_CLIENTID(clid))
1851 goto out;
1852 clp = find_confirmed_client(clid);
1853 status = nfserr_expired;
1854 if (clp == NULL) {
1855 /* We assume the client took too long to RENEW. */
1856 dprintk("nfsd4_renew: clientid not found!\n");
1857 goto out;
1858 }
1859 renew_client(clp);
1860 status = nfserr_cb_path_down;
ea1da636 1861 if (!list_empty(&clp->cl_delegations)
1da177e4
LT
1862 && !atomic_read(&clp->cl_callback.cb_set))
1863 goto out;
1864 status = nfs_ok;
1865out:
1866 nfs4_unlock_state();
1867 return status;
1868}
1869
a76b4319
N
1870static void
1871end_grace(void)
1872{
1873 dprintk("NFSD: end of grace period\n");
c7b9a459 1874 nfsd4_recdir_purge_old();
a76b4319
N
1875 in_grace = 0;
1876}
1877
fd39ca9a 1878static time_t
1da177e4
LT
1879nfs4_laundromat(void)
1880{
1881 struct nfs4_client *clp;
1882 struct nfs4_stateowner *sop;
1883 struct nfs4_delegation *dp;
1884 struct list_head *pos, *next, reaplist;
1885 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1886 time_t t, clientid_val = NFSD_LEASE_TIME;
1887 time_t u, test_val = NFSD_LEASE_TIME;
1888
1889 nfs4_lock_state();
1890
1891 dprintk("NFSD: laundromat service - starting\n");
a76b4319
N
1892 if (in_grace)
1893 end_grace();
1da177e4
LT
1894 list_for_each_safe(pos, next, &client_lru) {
1895 clp = list_entry(pos, struct nfs4_client, cl_lru);
1896 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1897 t = clp->cl_time - cutoff;
1898 if (clientid_val > t)
1899 clientid_val = t;
1900 break;
1901 }
1902 dprintk("NFSD: purging unused client (clientid %08x)\n",
1903 clp->cl_clientid.cl_id);
c7b9a459 1904 nfsd4_remove_clid_dir(clp);
1da177e4
LT
1905 expire_client(clp);
1906 }
1907 INIT_LIST_HEAD(&reaplist);
1908 spin_lock(&recall_lock);
1909 list_for_each_safe(pos, next, &del_recall_lru) {
1910 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1911 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1912 u = dp->dl_time - cutoff;
1913 if (test_val > u)
1914 test_val = u;
1915 break;
1916 }
1917 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1918 dp, dp->dl_flock);
1919 list_move(&dp->dl_recall_lru, &reaplist);
1920 }
1921 spin_unlock(&recall_lock);
1922 list_for_each_safe(pos, next, &reaplist) {
1923 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1924 list_del_init(&dp->dl_recall_lru);
1925 unhash_delegation(dp);
1926 }
1927 test_val = NFSD_LEASE_TIME;
1928 list_for_each_safe(pos, next, &close_lru) {
1929 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1930 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1931 u = sop->so_time - cutoff;
1932 if (test_val > u)
1933 test_val = u;
1934 break;
1935 }
1936 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1937 sop->so_id);
358dd55a 1938 release_stateowner(sop);
1da177e4
LT
1939 }
1940 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1941 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1942 nfs4_unlock_state();
1943 return clientid_val;
1944}
1945
1946void
1947laundromat_main(void *not_used)
1948{
1949 time_t t;
1950
1951 t = nfs4_laundromat();
1952 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
58da282b 1953 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
1da177e4
LT
1954}
1955
fd39ca9a 1956static struct nfs4_stateowner *
f8816512
N
1957search_close_lru(u32 st_id, int flags)
1958{
1da177e4
LT
1959 struct nfs4_stateowner *local = NULL;
1960
1da177e4
LT
1961 if (flags & CLOSE_STATE) {
1962 list_for_each_entry(local, &close_lru, so_close_lru) {
1963 if (local->so_id == st_id)
1964 return local;
1965 }
1966 }
1967 return NULL;
1968}
1969
1970static inline int
1971nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
1972{
1973 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_dentry->d_inode;
1974}
1975
1976static int
1977STALE_STATEID(stateid_t *stateid)
1978{
1979 if (stateid->si_boot == boot_time)
1980 return 0;
849823c5 1981 dprintk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
1da177e4
LT
1982 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
1983 stateid->si_generation);
1984 return 1;
1985}
1986
1987static inline int
1988access_permit_read(unsigned long access_bmap)
1989{
1990 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
1991 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
1992 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
1993}
1994
1995static inline int
1996access_permit_write(unsigned long access_bmap)
1997{
1998 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
1999 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
2000}
2001
2002static
2003int nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
2004{
2005 int status = nfserr_openmode;
2006
2007 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2008 goto out;
2009 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2010 goto out;
2011 status = nfs_ok;
2012out:
2013 return status;
2014}
2015
1da177e4
LT
2016static inline int
2017check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2018{
2019 /* Trying to call delegreturn with a special stateid? Yuch: */
2020 if (!(flags & (RD_STATE | WR_STATE)))
2021 return nfserr_bad_stateid;
2022 else if (ONE_STATEID(stateid) && (flags & RD_STATE))
2023 return nfs_ok;
2024 else if (nfs4_in_grace()) {
2025 /* Answer in remaining cases depends on existance of
2026 * conflicting state; so we must wait out the grace period. */
2027 return nfserr_grace;
2028 } else if (flags & WR_STATE)
2029 return nfs4_share_conflict(current_fh,
2030 NFS4_SHARE_DENY_WRITE);
2031 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2032 return nfs4_share_conflict(current_fh,
2033 NFS4_SHARE_DENY_READ);
2034}
2035
2036/*
2037 * Allow READ/WRITE during grace period on recovered state only for files
2038 * that are not able to provide mandatory locking.
2039 */
2040static inline int
2041io_during_grace_disallowed(struct inode *inode, int flags)
2042{
2043 return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE))
2044 && MANDATORY_LOCK(inode);
2045}
2046
2047/*
2048* Checks for stateid operations
2049*/
2050int
2051nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2052{
2053 struct nfs4_stateid *stp = NULL;
2054 struct nfs4_delegation *dp = NULL;
2055 stateid_t *stidp;
2056 struct inode *ino = current_fh->fh_dentry->d_inode;
2057 int status;
2058
2059 dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2060 stateid->si_boot, stateid->si_stateownerid,
2061 stateid->si_fileid, stateid->si_generation);
2062 if (filpp)
2063 *filpp = NULL;
2064
2065 if (io_during_grace_disallowed(ino, flags))
2066 return nfserr_grace;
2067
2068 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2069 return check_special_stateids(current_fh, stateid, flags);
2070
2071 /* STALE STATEID */
2072 status = nfserr_stale_stateid;
2073 if (STALE_STATEID(stateid))
2074 goto out;
2075
2076 /* BAD STATEID */
2077 status = nfserr_bad_stateid;
2078 if (!stateid->si_fileid) { /* delegation stateid */
2079 if(!(dp = find_delegation_stateid(ino, stateid))) {
2080 dprintk("NFSD: delegation stateid not found\n");
1da177e4
LT
2081 goto out;
2082 }
2083 stidp = &dp->dl_stateid;
2084 } else { /* open or lock stateid */
2085 if (!(stp = find_stateid(stateid, flags))) {
2086 dprintk("NFSD: open or lock stateid not found\n");
1da177e4
LT
2087 goto out;
2088 }
2089 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
2090 goto out;
2091 if (!stp->st_stateowner->so_confirmed)
2092 goto out;
2093 stidp = &stp->st_stateid;
2094 }
2095 if (stateid->si_generation > stidp->si_generation)
2096 goto out;
2097
2098 /* OLD STATEID */
2099 status = nfserr_old_stateid;
2100 if (stateid->si_generation < stidp->si_generation)
2101 goto out;
2102 if (stp) {
2103 if ((status = nfs4_check_openmode(stp,flags)))
2104 goto out;
2105 renew_client(stp->st_stateowner->so_client);
2106 if (filpp)
2107 *filpp = stp->st_vfs_file;
2108 } else if (dp) {
2109 if ((status = nfs4_check_delegmode(dp, flags)))
2110 goto out;
2111 renew_client(dp->dl_client);
2112 if (flags & DELEG_RET)
2113 unhash_delegation(dp);
2114 if (filpp)
2115 *filpp = dp->dl_vfs_file;
2116 }
2117 status = nfs_ok;
2118out:
2119 return status;
2120}
2121
4c4cd222
N
2122static inline int
2123setlkflg (int type)
2124{
2125 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
2126 RD_STATE : WR_STATE;
2127}
1da177e4
LT
2128
2129/*
2130 * Checks for sequence id mutating operations.
2131 */
fd39ca9a 2132static int
4c4cd222 2133nfs4_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 2134{
1da177e4
LT
2135 struct nfs4_stateid *stp;
2136 struct nfs4_stateowner *sop;
2137
2138 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2139 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2140 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2141 stateid->si_generation);
3a4f98bb 2142
1da177e4
LT
2143 *stpp = NULL;
2144 *sopp = NULL;
2145
1da177e4
LT
2146 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2147 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
3a4f98bb 2148 return nfserr_bad_stateid;
1da177e4
LT
2149 }
2150
1da177e4 2151 if (STALE_STATEID(stateid))
3a4f98bb 2152 return nfserr_stale_stateid;
1da177e4
LT
2153 /*
2154 * We return BAD_STATEID if filehandle doesn't match stateid,
2155 * the confirmed flag is incorrecly set, or the generation
2156 * number is incorrect.
1da177e4 2157 */
f8816512
N
2158 stp = find_stateid(stateid, flags);
2159 if (stp == NULL) {
2160 /*
2161 * Also, we should make sure this isn't just the result of
2162 * a replayed close:
2163 */
2164 sop = search_close_lru(stateid->si_stateownerid, flags);
2165 if (sop == NULL)
2166 return nfserr_bad_stateid;
2167 *sopp = sop;
2168 goto check_replay;
2169 }
1da177e4 2170
4c4cd222 2171 if (lock) {
1da177e4 2172 struct nfs4_stateowner *sop = stp->st_stateowner;
4c4cd222 2173 clientid_t *lockclid = &lock->v.new.clientid;
1da177e4 2174 struct nfs4_client *clp = sop->so_client;
4c4cd222
N
2175 int lkflg = 0;
2176 int status;
2177
2178 lkflg = setlkflg(lock->lk_type);
2179
2180 if (lock->lk_is_new) {
2181 if (!sop->so_is_open_owner)
2182 return nfserr_bad_stateid;
2183 if (!cmp_clid(&clp->cl_clientid, lockclid))
2184 return nfserr_bad_stateid;
2185 /* stp is the open stateid */
2186 status = nfs4_check_openmode(stp, lkflg);
2187 if (status)
2188 return status;
2189 } else {
2190 /* stp is the lock stateid */
2191 status = nfs4_check_openmode(stp->st_openstp, lkflg);
2192 if (status)
2193 return status;
2194 }
1da177e4 2195
1da177e4
LT
2196 }
2197
2198 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
2199 printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
3a4f98bb 2200 return nfserr_bad_stateid;
1da177e4
LT
2201 }
2202
2203 *stpp = stp;
2204 *sopp = sop = stp->st_stateowner;
2205
2206 /*
2207 * We now validate the seqid and stateid generation numbers.
2208 * For the moment, we ignore the possibility of
2209 * generation number wraparound.
2210 */
bd9aac52 2211 if (seqid != sop->so_seqid)
1da177e4
LT
2212 goto check_replay;
2213
3a4f98bb
N
2214 if (sop->so_confirmed && flags & CONFIRM) {
2215 printk("NFSD: preprocess_seqid_op: expected"
2216 " unconfirmed stateowner!\n");
2217 return nfserr_bad_stateid;
1da177e4 2218 }
3a4f98bb
N
2219 if (!sop->so_confirmed && !(flags & CONFIRM)) {
2220 printk("NFSD: preprocess_seqid_op: stateowner not"
2221 " confirmed yet!\n");
2222 return nfserr_bad_stateid;
1da177e4
LT
2223 }
2224 if (stateid->si_generation > stp->st_stateid.si_generation) {
2225 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
3a4f98bb 2226 return nfserr_bad_stateid;
1da177e4
LT
2227 }
2228
1da177e4
LT
2229 if (stateid->si_generation < stp->st_stateid.si_generation) {
2230 printk("NFSD: preprocess_seqid_op: old stateid!\n");
3a4f98bb 2231 return nfserr_old_stateid;
1da177e4 2232 }
52fd004e 2233 renew_client(sop->so_client);
3a4f98bb 2234 return nfs_ok;
1da177e4 2235
1da177e4 2236check_replay:
bd9aac52 2237 if (seqid == sop->so_seqid - 1) {
849823c5 2238 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
1da177e4 2239 /* indicate replay to calling function */
3a4f98bb 2240 return NFSERR_REPLAY_ME;
1da177e4 2241 }
3a4f98bb
N
2242 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
2243 sop->so_seqid, seqid);
2244 *sopp = NULL;
2245 return nfserr_bad_seqid;
1da177e4
LT
2246}
2247
2248int
f2327d9a 2249nfsd4_open_confirm(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_confirm *oc, struct nfs4_stateowner **replay_owner)
1da177e4
LT
2250{
2251 int status;
2252 struct nfs4_stateowner *sop;
2253 struct nfs4_stateid *stp;
2254
2255 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
2256 (int)current_fh->fh_dentry->d_name.len,
2257 current_fh->fh_dentry->d_name.name);
2258
a8cddc5d
BF
2259 status = fh_verify(rqstp, current_fh, S_IFREG, 0);
2260 if (status)
2261 return status;
1da177e4
LT
2262
2263 nfs4_lock_state();
2264
2265 if ((status = nfs4_preprocess_seqid_op(current_fh, oc->oc_seqid,
2266 &oc->oc_req_stateid,
2267 CHECK_FH | CONFIRM | OPEN_STATE,
2268 &oc->oc_stateowner, &stp, NULL)))
2269 goto out;
2270
2271 sop = oc->oc_stateowner;
2272 sop->so_confirmed = 1;
2273 update_stateid(&stp->st_stateid);
2274 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2275 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2276 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2277 stp->st_stateid.si_boot,
2278 stp->st_stateid.si_stateownerid,
2279 stp->st_stateid.si_fileid,
2280 stp->st_stateid.si_generation);
c7b9a459
N
2281
2282 nfsd4_create_clid_dir(sop->so_client);
1da177e4 2283out:
f2327d9a 2284 if (oc->oc_stateowner) {
1da177e4 2285 nfs4_get_stateowner(oc->oc_stateowner);
f2327d9a
NB
2286 *replay_owner = oc->oc_stateowner;
2287 }
1da177e4
LT
2288 nfs4_unlock_state();
2289 return status;
2290}
2291
2292
2293/*
2294 * unset all bits in union bitmap (bmap) that
2295 * do not exist in share (from successful OPEN_DOWNGRADE)
2296 */
2297static void
2298reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2299{
2300 int i;
2301 for (i = 1; i < 4; i++) {
2302 if ((i & access) != i)
2303 __clear_bit(i, bmap);
2304 }
2305}
2306
2307static void
2308reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2309{
2310 int i;
2311 for (i = 0; i < 4; i++) {
2312 if ((i & deny) != i)
2313 __clear_bit(i, bmap);
2314 }
2315}
2316
2317int
f2327d9a 2318nfsd4_open_downgrade(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_downgrade *od, struct nfs4_stateowner **replay_owner)
1da177e4
LT
2319{
2320 int status;
2321 struct nfs4_stateid *stp;
2322 unsigned int share_access;
2323
2324 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
2325 (int)current_fh->fh_dentry->d_name.len,
2326 current_fh->fh_dentry->d_name.name);
2327
ba5a6a19
BF
2328 if (!access_valid(od->od_share_access)
2329 || !deny_valid(od->od_share_deny))
1da177e4
LT
2330 return nfserr_inval;
2331
2332 nfs4_lock_state();
2333 if ((status = nfs4_preprocess_seqid_op(current_fh, od->od_seqid,
2334 &od->od_stateid,
2335 CHECK_FH | OPEN_STATE,
2336 &od->od_stateowner, &stp, NULL)))
2337 goto out;
2338
2339 status = nfserr_inval;
2340 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2341 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2342 stp->st_access_bmap, od->od_share_access);
2343 goto out;
2344 }
2345 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2346 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2347 stp->st_deny_bmap, od->od_share_deny);
2348 goto out;
2349 }
2350 set_access(&share_access, stp->st_access_bmap);
2351 nfs4_file_downgrade(stp->st_vfs_file,
2352 share_access & ~od->od_share_access);
2353
2354 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2355 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2356
2357 update_stateid(&stp->st_stateid);
2358 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2359 status = nfs_ok;
2360out:
f2327d9a 2361 if (od->od_stateowner) {
1da177e4 2362 nfs4_get_stateowner(od->od_stateowner);
f2327d9a
NB
2363 *replay_owner = od->od_stateowner;
2364 }
1da177e4
LT
2365 nfs4_unlock_state();
2366 return status;
2367}
2368
2369/*
2370 * nfs4_unlock_state() called after encode
2371 */
2372int
f2327d9a 2373nfsd4_close(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_close *close, struct nfs4_stateowner **replay_owner)
1da177e4
LT
2374{
2375 int status;
2376 struct nfs4_stateid *stp;
2377
2378 dprintk("NFSD: nfsd4_close on file %.*s\n",
2379 (int)current_fh->fh_dentry->d_name.len,
2380 current_fh->fh_dentry->d_name.name);
2381
2382 nfs4_lock_state();
2383 /* check close_lru for replay */
2384 if ((status = nfs4_preprocess_seqid_op(current_fh, close->cl_seqid,
2385 &close->cl_stateid,
2386 CHECK_FH | OPEN_STATE | CLOSE_STATE,
2387 &close->cl_stateowner, &stp, NULL)))
2388 goto out;
1da177e4
LT
2389 status = nfs_ok;
2390 update_stateid(&stp->st_stateid);
2391 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2392
04ef5954
BF
2393 /* release_stateid() calls nfsd_close() if needed */
2394 release_stateid(stp, OPEN_STATE);
2395
2396 /* place unused nfs4_stateowners on so_close_lru list to be
2397 * released by the laundromat service after the lease period
2398 * to enable us to handle CLOSE replay
2399 */
2400 if (list_empty(&close->cl_stateowner->so_stateids))
2401 move_to_close_lru(close->cl_stateowner);
1da177e4 2402out:
f2327d9a 2403 if (close->cl_stateowner) {
1da177e4 2404 nfs4_get_stateowner(close->cl_stateowner);
f2327d9a
NB
2405 *replay_owner = close->cl_stateowner;
2406 }
1da177e4
LT
2407 nfs4_unlock_state();
2408 return status;
2409}
2410
2411int
2412nfsd4_delegreturn(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_delegreturn *dr)
2413{
2414 int status;
2415
2416 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2417 goto out;
2418
2419 nfs4_lock_state();
2420 status = nfs4_preprocess_stateid_op(current_fh, &dr->dr_stateid, DELEG_RET, NULL);
2421 nfs4_unlock_state();
2422out:
2423 return status;
2424}
2425
2426
2427/*
2428 * Lock owner state (byte-range locks)
2429 */
2430#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2431#define LOCK_HASH_BITS 8
2432#define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2433#define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2434
2435#define lockownerid_hashval(id) \
2436 ((id) & LOCK_HASH_MASK)
2437
2438static inline unsigned int
2439lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2440 struct xdr_netobj *ownername)
2441{
2442 return (file_hashval(inode) + cl_id
2443 + opaque_hashval(ownername->data, ownername->len))
2444 & LOCK_HASH_MASK;
2445}
2446
2447static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2448static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2449static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2450
fd39ca9a 2451static struct nfs4_stateid *
1da177e4
LT
2452find_stateid(stateid_t *stid, int flags)
2453{
2454 struct nfs4_stateid *local = NULL;
2455 u32 st_id = stid->si_stateownerid;
2456 u32 f_id = stid->si_fileid;
2457 unsigned int hashval;
2458
2459 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
2460 if ((flags & LOCK_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2461 hashval = stateid_hashval(st_id, f_id);
2462 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2463 if ((local->st_stateid.si_stateownerid == st_id) &&
2464 (local->st_stateid.si_fileid == f_id))
2465 return local;
2466 }
2467 }
2468 if ((flags & OPEN_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2469 hashval = stateid_hashval(st_id, f_id);
2470 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2471 if ((local->st_stateid.si_stateownerid == st_id) &&
2472 (local->st_stateid.si_fileid == f_id))
2473 return local;
2474 }
849823c5 2475 }
1da177e4
LT
2476 return NULL;
2477}
2478
2479static struct nfs4_delegation *
2480find_delegation_stateid(struct inode *ino, stateid_t *stid)
2481{
13cd2184
N
2482 struct nfs4_file *fp;
2483 struct nfs4_delegation *dl;
1da177e4
LT
2484
2485 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2486 stid->si_boot, stid->si_stateownerid,
2487 stid->si_fileid, stid->si_generation);
2488
1da177e4 2489 fp = find_file(ino);
13cd2184
N
2490 if (!fp)
2491 return NULL;
2492 dl = find_delegation_file(fp, stid);
2493 put_nfs4_file(fp);
2494 return dl;
1da177e4
LT
2495}
2496
2497/*
2498 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2499 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2500 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2501 * locking, this prevents us from being completely protocol-compliant. The
2502 * real solution to this problem is to start using unsigned file offsets in
2503 * the VFS, but this is a very deep change!
2504 */
2505static inline void
2506nfs4_transform_lock_offset(struct file_lock *lock)
2507{
2508 if (lock->fl_start < 0)
2509 lock->fl_start = OFFSET_MAX;
2510 if (lock->fl_end < 0)
2511 lock->fl_end = OFFSET_MAX;
2512}
2513
d5b9026a
N
2514/* Hack!: For now, we're defining this just so we can use a pointer to it
2515 * as a unique cookie to identify our (NFSv4's) posix locks. */
e465a77f 2516static struct lock_manager_operations nfsd_posix_mng_ops = {
d5b9026a 2517};
1da177e4
LT
2518
2519static inline void
2520nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2521{
d5b9026a
N
2522 struct nfs4_stateowner *sop;
2523 unsigned int hval;
1da177e4 2524
d5b9026a
N
2525 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
2526 sop = (struct nfs4_stateowner *) fl->fl_owner;
2527 hval = lockownerid_hashval(sop->so_id);
1da177e4
LT
2528 kref_get(&sop->so_ref);
2529 deny->ld_sop = sop;
2530 deny->ld_clientid = sop->so_client->cl_clientid;
d5b9026a
N
2531 } else {
2532 deny->ld_sop = NULL;
2533 deny->ld_clientid.cl_boot = 0;
2534 deny->ld_clientid.cl_id = 0;
1da177e4
LT
2535 }
2536 deny->ld_start = fl->fl_start;
2537 deny->ld_length = ~(u64)0;
2538 if (fl->fl_end != ~(u64)0)
2539 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2540 deny->ld_type = NFS4_READ_LT;
2541 if (fl->fl_type != F_RDLCK)
2542 deny->ld_type = NFS4_WRITE_LT;
2543}
2544
1da177e4
LT
2545static struct nfs4_stateowner *
2546find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2547 struct xdr_netobj *owner)
2548{
2549 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2550 struct nfs4_stateowner *op;
2551
2552 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
2553 if (cmp_owner_str(op, owner, clid))
2554 return op;
2555 }
2556 return NULL;
2557}
2558
2559/*
2560 * Alloc a lock owner structure.
2561 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2562 * occured.
2563 *
2564 * strhashval = lock_ownerstr_hashval
1da177e4
LT
2565 */
2566
2567static struct nfs4_stateowner *
2568alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2569 struct nfs4_stateowner *sop;
2570 struct nfs4_replay *rp;
2571 unsigned int idhashval;
2572
2573 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2574 return NULL;
2575 idhashval = lockownerid_hashval(current_ownerid);
2576 INIT_LIST_HEAD(&sop->so_idhash);
2577 INIT_LIST_HEAD(&sop->so_strhash);
2578 INIT_LIST_HEAD(&sop->so_perclient);
ea1da636
N
2579 INIT_LIST_HEAD(&sop->so_stateids);
2580 INIT_LIST_HEAD(&sop->so_perstateid);
1da177e4
LT
2581 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2582 sop->so_time = 0;
2583 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2584 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
ea1da636 2585 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
1da177e4
LT
2586 sop->so_is_open_owner = 0;
2587 sop->so_id = current_ownerid++;
2588 sop->so_client = clp;
b59e3c0e
NB
2589 /* It is the openowner seqid that will be incremented in encode in the
2590 * case of new lockowners; so increment the lock seqid manually: */
2591 sop->so_seqid = lock->lk_new_lock_seqid + 1;
1da177e4
LT
2592 sop->so_confirmed = 1;
2593 rp = &sop->so_replay;
de1ae286 2594 rp->rp_status = nfserr_serverfault;
1da177e4
LT
2595 rp->rp_buflen = 0;
2596 rp->rp_buf = rp->rp_ibuf;
2597 return sop;
2598}
2599
fd39ca9a 2600static struct nfs4_stateid *
1da177e4
LT
2601alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2602{
2603 struct nfs4_stateid *stp;
2604 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2605
5ac049ac
N
2606 stp = nfs4_alloc_stateid();
2607 if (stp == NULL)
1da177e4
LT
2608 goto out;
2609 INIT_LIST_HEAD(&stp->st_hash);
2610 INIT_LIST_HEAD(&stp->st_perfile);
ea1da636
N
2611 INIT_LIST_HEAD(&stp->st_perstateowner);
2612 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
1da177e4 2613 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
8beefa24 2614 list_add(&stp->st_perfile, &fp->fi_stateids);
ea1da636 2615 list_add(&stp->st_perstateowner, &sop->so_stateids);
1da177e4 2616 stp->st_stateowner = sop;
13cd2184 2617 get_nfs4_file(fp);
1da177e4
LT
2618 stp->st_file = fp;
2619 stp->st_stateid.si_boot = boot_time;
2620 stp->st_stateid.si_stateownerid = sop->so_id;
2621 stp->st_stateid.si_fileid = fp->fi_id;
2622 stp->st_stateid.si_generation = 0;
2623 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2624 stp->st_access_bmap = open_stp->st_access_bmap;
2625 stp->st_deny_bmap = open_stp->st_deny_bmap;
4c4cd222 2626 stp->st_openstp = open_stp;
1da177e4
LT
2627
2628out:
2629 return stp;
2630}
2631
fd39ca9a 2632static int
1da177e4
LT
2633check_lock_length(u64 offset, u64 length)
2634{
2635 return ((length == 0) || ((length != ~(u64)0) &&
2636 LOFF_OVERFLOW(offset, length)));
2637}
2638
2639/*
2640 * LOCK operation
2641 */
2642int
f2327d9a 2643nfsd4_lock(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lock *lock, struct nfs4_stateowner **replay_owner)
1da177e4 2644{
3e9e3dbe 2645 struct nfs4_stateowner *open_sop = NULL;
b59e3c0e 2646 struct nfs4_stateowner *lock_sop = NULL;
1da177e4
LT
2647 struct nfs4_stateid *lock_stp;
2648 struct file *filp;
2649 struct file_lock file_lock;
8dc7c311 2650 struct file_lock conflock;
1da177e4
LT
2651 int status = 0;
2652 unsigned int strhashval;
2653
2654 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2655 (long long) lock->lk_offset,
2656 (long long) lock->lk_length);
2657
1da177e4
LT
2658 if (check_lock_length(lock->lk_offset, lock->lk_length))
2659 return nfserr_inval;
2660
a6f6ef2f
AA
2661 if ((status = fh_verify(rqstp, current_fh, S_IFREG, MAY_LOCK))) {
2662 dprintk("NFSD: nfsd4_lock: permission denied!\n");
2663 return status;
2664 }
2665
1da177e4
LT
2666 nfs4_lock_state();
2667
2668 if (lock->lk_is_new) {
893f8770
N
2669 /*
2670 * Client indicates that this is a new lockowner.
2671 * Use open owner and open stateid to create lock owner and
2672 * lock stateid.
2673 */
1da177e4
LT
2674 struct nfs4_stateid *open_stp = NULL;
2675 struct nfs4_file *fp;
2676
2677 status = nfserr_stale_clientid;
849823c5 2678 if (STALE_CLIENTID(&lock->lk_new_clientid))
1da177e4 2679 goto out;
1da177e4 2680
1da177e4
LT
2681 /* validate and update open stateid and open seqid */
2682 status = nfs4_preprocess_seqid_op(current_fh,
2683 lock->lk_new_open_seqid,
2684 &lock->lk_new_open_stateid,
2685 CHECK_FH | OPEN_STATE,
3a65588a 2686 &lock->lk_replay_owner, &open_stp,
b59e3c0e 2687 lock);
37515177 2688 if (status)
1da177e4 2689 goto out;
3a65588a 2690 open_sop = lock->lk_replay_owner;
1da177e4
LT
2691 /* create lockowner and lock stateid */
2692 fp = open_stp->st_file;
2693 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2694 open_sop->so_client->cl_clientid.cl_id,
2695 &lock->v.new.owner);
3e9e3dbe
N
2696 /* XXX: Do we need to check for duplicate stateowners on
2697 * the same file, or should they just be allowed (and
2698 * create new stateids)? */
1da177e4 2699 status = nfserr_resource;
b59e3c0e
NB
2700 lock_sop = alloc_init_lock_stateowner(strhashval,
2701 open_sop->so_client, open_stp, lock);
2702 if (lock_sop == NULL)
1da177e4 2703 goto out;
b59e3c0e 2704 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
8a280510 2705 if (lock_stp == NULL)
1da177e4 2706 goto out;
1da177e4
LT
2707 } else {
2708 /* lock (lock owner + lock stateid) already exists */
2709 status = nfs4_preprocess_seqid_op(current_fh,
2710 lock->lk_old_lock_seqid,
2711 &lock->lk_old_lock_stateid,
2712 CHECK_FH | LOCK_STATE,
3a65588a 2713 &lock->lk_replay_owner, &lock_stp, lock);
1da177e4
LT
2714 if (status)
2715 goto out;
3a65588a 2716 lock_sop = lock->lk_replay_owner;
1da177e4 2717 }
3a65588a 2718 /* lock->lk_replay_owner and lock_stp have been created or found */
1da177e4
LT
2719 filp = lock_stp->st_vfs_file;
2720
0dd395dc
N
2721 status = nfserr_grace;
2722 if (nfs4_in_grace() && !lock->lk_reclaim)
2723 goto out;
2724 status = nfserr_no_grace;
2725 if (!nfs4_in_grace() && lock->lk_reclaim)
2726 goto out;
2727
1da177e4
LT
2728 locks_init_lock(&file_lock);
2729 switch (lock->lk_type) {
2730 case NFS4_READ_LT:
2731 case NFS4_READW_LT:
2732 file_lock.fl_type = F_RDLCK;
2733 break;
2734 case NFS4_WRITE_LT:
2735 case NFS4_WRITEW_LT:
2736 file_lock.fl_type = F_WRLCK;
2737 break;
2738 default:
2739 status = nfserr_inval;
2740 goto out;
2741 }
b59e3c0e 2742 file_lock.fl_owner = (fl_owner_t)lock_sop;
1da177e4
LT
2743 file_lock.fl_pid = current->tgid;
2744 file_lock.fl_file = filp;
2745 file_lock.fl_flags = FL_POSIX;
d5b9026a 2746 file_lock.fl_lmops = &nfsd_posix_mng_ops;
1da177e4
LT
2747
2748 file_lock.fl_start = lock->lk_offset;
2749 if ((lock->lk_length == ~(u64)0) ||
2750 LOFF_OVERFLOW(lock->lk_offset, lock->lk_length))
2751 file_lock.fl_end = ~(u64)0;
2752 else
2753 file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
2754 nfs4_transform_lock_offset(&file_lock);
2755
2756 /*
2757 * Try to lock the file in the VFS.
2758 * Note: locks.c uses the BKL to protect the inode's lock list.
2759 */
2760
eb76b3fd
AA
2761 /* XXX?: Just to divert the locks_release_private at the start of
2762 * locks_copy_lock: */
2763 conflock.fl_ops = NULL;
2764 conflock.fl_lmops = NULL;
2765 status = posix_lock_file_conf(filp, &file_lock, &conflock);
2766 dprintk("NFSD: nfsd4_lock: posix_lock_file_conf status %d\n",status);
1da177e4
LT
2767 switch (-status) {
2768 case 0: /* success! */
2769 update_stateid(&lock_stp->st_stateid);
2770 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2771 sizeof(stateid_t));
eb76b3fd
AA
2772 break;
2773 case (EAGAIN): /* conflock holds conflicting lock */
2774 status = nfserr_denied;
2775 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2776 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
2777 break;
1da177e4
LT
2778 case (EDEADLK):
2779 status = nfserr_deadlock;
eb76b3fd 2780 break;
1da177e4 2781 default:
eb76b3fd
AA
2782 dprintk("NFSD: nfsd4_lock: posix_lock_file_conf() failed! status %d\n",status);
2783 status = nfserr_resource;
2784 break;
1da177e4 2785 }
1da177e4 2786out:
8a280510
BF
2787 if (status && lock->lk_is_new && lock_sop)
2788 release_stateowner(lock_sop);
3a65588a
BF
2789 if (lock->lk_replay_owner) {
2790 nfs4_get_stateowner(lock->lk_replay_owner);
2791 *replay_owner = lock->lk_replay_owner;
f2327d9a 2792 }
1da177e4
LT
2793 nfs4_unlock_state();
2794 return status;
2795}
2796
2797/*
2798 * LOCKT operation
2799 */
2800int
2801nfsd4_lockt(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lockt *lockt)
2802{
2803 struct inode *inode;
2804 struct file file;
2805 struct file_lock file_lock;
8dc7c311 2806 struct file_lock conflock;
1da177e4
LT
2807 int status;
2808
2809 if (nfs4_in_grace())
2810 return nfserr_grace;
2811
2812 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2813 return nfserr_inval;
2814
2815 lockt->lt_stateowner = NULL;
2816 nfs4_lock_state();
2817
2818 status = nfserr_stale_clientid;
849823c5 2819 if (STALE_CLIENTID(&lockt->lt_clientid))
1da177e4 2820 goto out;
1da177e4
LT
2821
2822 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0))) {
849823c5 2823 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
1da177e4
LT
2824 if (status == nfserr_symlink)
2825 status = nfserr_inval;
2826 goto out;
2827 }
2828
2829 inode = current_fh->fh_dentry->d_inode;
2830 locks_init_lock(&file_lock);
2831 switch (lockt->lt_type) {
2832 case NFS4_READ_LT:
2833 case NFS4_READW_LT:
2834 file_lock.fl_type = F_RDLCK;
2835 break;
2836 case NFS4_WRITE_LT:
2837 case NFS4_WRITEW_LT:
2838 file_lock.fl_type = F_WRLCK;
2839 break;
2840 default:
2841 printk("NFSD: nfs4_lockt: bad lock type!\n");
2842 status = nfserr_inval;
2843 goto out;
2844 }
2845
2846 lockt->lt_stateowner = find_lockstateowner_str(inode,
2847 &lockt->lt_clientid, &lockt->lt_owner);
2848 if (lockt->lt_stateowner)
2849 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2850 file_lock.fl_pid = current->tgid;
2851 file_lock.fl_flags = FL_POSIX;
d5b9026a 2852 file_lock.fl_lmops = &nfsd_posix_mng_ops;
1da177e4
LT
2853
2854 file_lock.fl_start = lockt->lt_offset;
2855 if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length))
2856 file_lock.fl_end = ~(u64)0;
2857 else
2858 file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
2859
2860 nfs4_transform_lock_offset(&file_lock);
2861
2862 /* posix_test_lock uses the struct file _only_ to resolve the inode.
2863 * since LOCKT doesn't require an OPEN, and therefore a struct
2864 * file may not exist, pass posix_test_lock a struct file with
2865 * only the dentry:inode set.
2866 */
2867 memset(&file, 0, sizeof (struct file));
2868 file.f_dentry = current_fh->fh_dentry;
2869
2870 status = nfs_ok;
8dc7c311 2871 if (posix_test_lock(&file, &file_lock, &conflock)) {
1da177e4 2872 status = nfserr_denied;
8dc7c311 2873 nfs4_set_lock_denied(&conflock, &lockt->lt_denied);
1da177e4
LT
2874 }
2875out:
2876 nfs4_unlock_state();
2877 return status;
2878}
2879
2880int
f2327d9a 2881nfsd4_locku(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_locku *locku, struct nfs4_stateowner **replay_owner)
1da177e4
LT
2882{
2883 struct nfs4_stateid *stp;
2884 struct file *filp = NULL;
2885 struct file_lock file_lock;
2886 int status;
2887
2888 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2889 (long long) locku->lu_offset,
2890 (long long) locku->lu_length);
2891
2892 if (check_lock_length(locku->lu_offset, locku->lu_length))
2893 return nfserr_inval;
2894
2895 nfs4_lock_state();
2896
2897 if ((status = nfs4_preprocess_seqid_op(current_fh,
2898 locku->lu_seqid,
2899 &locku->lu_stateid,
2900 CHECK_FH | LOCK_STATE,
2901 &locku->lu_stateowner, &stp, NULL)))
2902 goto out;
2903
2904 filp = stp->st_vfs_file;
2905 BUG_ON(!filp);
2906 locks_init_lock(&file_lock);
2907 file_lock.fl_type = F_UNLCK;
2908 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
2909 file_lock.fl_pid = current->tgid;
2910 file_lock.fl_file = filp;
2911 file_lock.fl_flags = FL_POSIX;
d5b9026a 2912 file_lock.fl_lmops = &nfsd_posix_mng_ops;
1da177e4
LT
2913 file_lock.fl_start = locku->lu_offset;
2914
2915 if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
2916 file_lock.fl_end = ~(u64)0;
2917 else
2918 file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
2919 nfs4_transform_lock_offset(&file_lock);
2920
2921 /*
2922 * Try to unlock the file in the VFS.
2923 */
2924 status = posix_lock_file(filp, &file_lock);
1da177e4 2925 if (status) {
849823c5 2926 dprintk("NFSD: nfs4_locku: posix_lock_file failed!\n");
1da177e4
LT
2927 goto out_nfserr;
2928 }
2929 /*
2930 * OK, unlock succeeded; the only thing left to do is update the stateid.
2931 */
2932 update_stateid(&stp->st_stateid);
2933 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
2934
2935out:
f2327d9a 2936 if (locku->lu_stateowner) {
1da177e4 2937 nfs4_get_stateowner(locku->lu_stateowner);
f2327d9a
NB
2938 *replay_owner = locku->lu_stateowner;
2939 }
1da177e4
LT
2940 nfs4_unlock_state();
2941 return status;
2942
2943out_nfserr:
2944 status = nfserrno(status);
2945 goto out;
2946}
2947
2948/*
2949 * returns
2950 * 1: locks held by lockowner
2951 * 0: no locks held by lockowner
2952 */
2953static int
2954check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
2955{
2956 struct file_lock **flpp;
2957 struct inode *inode = filp->f_dentry->d_inode;
2958 int status = 0;
2959
2960 lock_kernel();
2961 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
796dadfd 2962 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
1da177e4
LT
2963 status = 1;
2964 goto out;
796dadfd 2965 }
1da177e4
LT
2966 }
2967out:
2968 unlock_kernel();
2969 return status;
2970}
2971
2972int
2973nfsd4_release_lockowner(struct svc_rqst *rqstp, struct nfsd4_release_lockowner *rlockowner)
2974{
2975 clientid_t *clid = &rlockowner->rl_clientid;
3e9e3dbe
N
2976 struct nfs4_stateowner *sop;
2977 struct nfs4_stateid *stp;
1da177e4 2978 struct xdr_netobj *owner = &rlockowner->rl_owner;
3e9e3dbe
N
2979 struct list_head matches;
2980 int i;
1da177e4
LT
2981 int status;
2982
2983 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
2984 clid->cl_boot, clid->cl_id);
2985
2986 /* XXX check for lease expiration */
2987
2988 status = nfserr_stale_clientid;
849823c5 2989 if (STALE_CLIENTID(clid))
1da177e4 2990 return status;
1da177e4
LT
2991
2992 nfs4_lock_state();
2993
3e9e3dbe
N
2994 status = nfserr_locks_held;
2995 /* XXX: we're doing a linear search through all the lockowners.
2996 * Yipes! For now we'll just hope clients aren't really using
2997 * release_lockowner much, but eventually we have to fix these
2998 * data structures. */
2999 INIT_LIST_HEAD(&matches);
3000 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3001 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
3002 if (!cmp_owner_str(sop, owner, clid))
3003 continue;
3004 list_for_each_entry(stp, &sop->so_stateids,
3005 st_perstateowner) {
3006 if (check_for_locks(stp->st_vfs_file, sop))
3007 goto out;
3008 /* Note: so_perclient unused for lockowners,
3009 * so it's OK to fool with here. */
3010 list_add(&sop->so_perclient, &matches);
3011 }
1da177e4 3012 }
3e9e3dbe
N
3013 }
3014 /* Clients probably won't expect us to return with some (but not all)
3015 * of the lockowner state released; so don't release any until all
3016 * have been checked. */
3017 status = nfs_ok;
0fa822e4
N
3018 while (!list_empty(&matches)) {
3019 sop = list_entry(matches.next, struct nfs4_stateowner,
3020 so_perclient);
3021 /* unhash_stateowner deletes so_perclient only
3022 * for openowners. */
3023 list_del(&sop->so_perclient);
3e9e3dbe 3024 release_stateowner(sop);
1da177e4
LT
3025 }
3026out:
3027 nfs4_unlock_state();
3028 return status;
3029}
3030
3031static inline struct nfs4_client_reclaim *
a55370a3 3032alloc_reclaim(void)
1da177e4 3033{
a55370a3 3034 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
1da177e4
LT
3035}
3036
c7b9a459
N
3037int
3038nfs4_has_reclaimed_state(const char *name)
3039{
3040 unsigned int strhashval = clientstr_hashval(name);
3041 struct nfs4_client *clp;
3042
3043 clp = find_confirmed_client_by_str(name, strhashval);
3044 return clp ? 1 : 0;
3045}
3046
1da177e4
LT
3047/*
3048 * failure => all reset bets are off, nfserr_no_grace...
3049 */
190e4fbf
N
3050int
3051nfs4_client_to_reclaim(const char *name)
1da177e4
LT
3052{
3053 unsigned int strhashval;
3054 struct nfs4_client_reclaim *crp = NULL;
3055
a55370a3
N
3056 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
3057 crp = alloc_reclaim();
1da177e4
LT
3058 if (!crp)
3059 return 0;
a55370a3 3060 strhashval = clientstr_hashval(name);
1da177e4
LT
3061 INIT_LIST_HEAD(&crp->cr_strhash);
3062 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
a55370a3 3063 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
1da177e4
LT
3064 reclaim_str_hashtbl_size++;
3065 return 1;
3066}
3067
3068static void
3069nfs4_release_reclaim(void)
3070{
3071 struct nfs4_client_reclaim *crp = NULL;
3072 int i;
3073
1da177e4
LT
3074 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3075 while (!list_empty(&reclaim_str_hashtbl[i])) {
3076 crp = list_entry(reclaim_str_hashtbl[i].next,
3077 struct nfs4_client_reclaim, cr_strhash);
3078 list_del(&crp->cr_strhash);
1da177e4
LT
3079 kfree(crp);
3080 reclaim_str_hashtbl_size--;
3081 }
3082 }
3083 BUG_ON(reclaim_str_hashtbl_size);
3084}
3085
3086/*
3087 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
fd39ca9a 3088static struct nfs4_client_reclaim *
1da177e4
LT
3089nfs4_find_reclaim_client(clientid_t *clid)
3090{
3091 unsigned int strhashval;
3092 struct nfs4_client *clp;
3093 struct nfs4_client_reclaim *crp = NULL;
3094
3095
3096 /* find clientid in conf_id_hashtbl */
3097 clp = find_confirmed_client(clid);
3098 if (clp == NULL)
3099 return NULL;
3100
a55370a3
N
3101 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3102 clp->cl_name.len, clp->cl_name.data,
3103 clp->cl_recdir);
1da177e4
LT
3104
3105 /* find clp->cl_name in reclaim_str_hashtbl */
a55370a3 3106 strhashval = clientstr_hashval(clp->cl_recdir);
1da177e4 3107 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
a55370a3 3108 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
1da177e4
LT
3109 return crp;
3110 }
3111 }
3112 return NULL;
3113}
3114
3115/*
3116* Called from OPEN. Look for clientid in reclaim list.
3117*/
3118int
3119nfs4_check_open_reclaim(clientid_t *clid)
3120{
dfc83565 3121 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
1da177e4
LT
3122}
3123
ac4d8ff2 3124/* initialization to perform at module load time: */
1da177e4 3125
ac4d8ff2
N
3126void
3127nfs4_state_init(void)
1da177e4
LT
3128{
3129 int i;
1da177e4 3130
1da177e4
LT
3131 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3132 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3133 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3134 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3135 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3136 }
3137 for (i = 0; i < FILE_HASH_SIZE; i++) {
3138 INIT_LIST_HEAD(&file_hashtbl[i]);
3139 }
3140 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3141 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3142 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3143 }
3144 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3145 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3146 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3147 }
3148 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3149 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3150 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3151 }
1da177e4 3152 memset(&onestateid, ~0, sizeof(stateid_t));
1da177e4
LT
3153 INIT_LIST_HEAD(&close_lru);
3154 INIT_LIST_HEAD(&client_lru);
3155 INIT_LIST_HEAD(&del_recall_lru);
ac4d8ff2
N
3156 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3157 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3158 reclaim_str_hashtbl_size = 0;
ac4d8ff2
N
3159}
3160
190e4fbf
N
3161static void
3162nfsd4_load_reboot_recovery_data(void)
3163{
3164 int status;
3165
0964a3d3
N
3166 nfs4_lock_state();
3167 nfsd4_init_recdir(user_recovery_dirname);
190e4fbf 3168 status = nfsd4_recdir_load();
0964a3d3 3169 nfs4_unlock_state();
190e4fbf
N
3170 if (status)
3171 printk("NFSD: Failure reading reboot recovery data\n");
3172}
3173
ac4d8ff2
N
3174/* initialization to perform when the nfsd service is started: */
3175
3176static void
3177__nfs4_state_start(void)
3178{
3179 time_t grace_time;
3180
1da177e4 3181 boot_time = get_seconds();
d99a05ad
N
3182 grace_time = max(user_lease_time, lease_time);
3183 lease_time = user_lease_time;
a76b4319 3184 in_grace = 1;
d99a05ad 3185 printk("NFSD: starting %ld-second grace period\n", grace_time);
58da282b 3186 laundry_wq = create_singlethread_workqueue("nfsd4");
a76b4319 3187 queue_delayed_work(laundry_wq, &laundromat_work, grace_time*HZ);
1da177e4
LT
3188}
3189
3190int
76a3550e 3191nfs4_state_start(void)
1da177e4
LT
3192{
3193 int status;
3194
3195 if (nfs4_init)
3196 return 0;
3197 status = nfsd4_init_slabs();
3198 if (status)
3199 return status;
190e4fbf 3200 nfsd4_load_reboot_recovery_data();
76a3550e 3201 __nfs4_state_start();
1da177e4
LT
3202 nfs4_init = 1;
3203 return 0;
3204}
3205
3206int
3207nfs4_in_grace(void)
3208{
a76b4319 3209 return in_grace;
1da177e4
LT
3210}
3211
3212time_t
3213nfs4_lease_time(void)
3214{
3215 return lease_time;
3216}
3217
3218static void
3219__nfs4_state_shutdown(void)
3220{
3221 int i;
3222 struct nfs4_client *clp = NULL;
3223 struct nfs4_delegation *dp = NULL;
1da177e4
LT
3224 struct list_head *pos, *next, reaplist;
3225
1da177e4
LT
3226 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3227 while (!list_empty(&conf_id_hashtbl[i])) {
3228 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3229 expire_client(clp);
3230 }
3231 while (!list_empty(&unconf_str_hashtbl[i])) {
3232 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3233 expire_client(clp);
3234 }
3235 }
3236 INIT_LIST_HEAD(&reaplist);
3237 spin_lock(&recall_lock);
3238 list_for_each_safe(pos, next, &del_recall_lru) {
3239 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3240 list_move(&dp->dl_recall_lru, &reaplist);
3241 }
3242 spin_unlock(&recall_lock);
3243 list_for_each_safe(pos, next, &reaplist) {
3244 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3245 list_del_init(&dp->dl_recall_lru);
3246 unhash_delegation(dp);
3247 }
3248
1da177e4 3249 cancel_delayed_work(&laundromat_work);
190e4fbf 3250 nfsd4_shutdown_recdir();
1da177e4 3251 nfs4_init = 0;
1da177e4
LT
3252}
3253
3254void
3255nfs4_state_shutdown(void)
3256{
5e8d5c29
N
3257 cancel_rearming_delayed_workqueue(laundry_wq, &laundromat_work);
3258 destroy_workqueue(laundry_wq);
1da177e4
LT
3259 nfs4_lock_state();
3260 nfs4_release_reclaim();
3261 __nfs4_state_shutdown();
3262 nfsd4_free_slabs();
3263 nfs4_unlock_state();
3264}
3265
0964a3d3
N
3266static void
3267nfs4_set_recdir(char *recdir)
3268{
3269 nfs4_lock_state();
3270 strcpy(user_recovery_dirname, recdir);
3271 nfs4_unlock_state();
3272}
3273
3274/*
3275 * Change the NFSv4 recovery directory to recdir.
3276 */
3277int
3278nfs4_reset_recoverydir(char *recdir)
3279{
3280 int status;
3281 struct nameidata nd;
3282
3283 status = path_lookup(recdir, LOOKUP_FOLLOW, &nd);
3284 if (status)
3285 return status;
3286 status = -ENOTDIR;
3287 if (S_ISDIR(nd.dentry->d_inode->i_mode)) {
3288 nfs4_set_recdir(recdir);
3289 status = 0;
3290 }
3291 path_release(&nd);
3292 return status;
3293}
3294
1da177e4
LT
3295/*
3296 * Called when leasetime is changed.
3297 *
d99a05ad
N
3298 * The only way the protocol gives us to handle on-the-fly lease changes is to
3299 * simulate a reboot. Instead of doing that, we just wait till the next time
3300 * we start to register any changes in lease time. If the administrator
3301 * really wants to change the lease time *now*, they can go ahead and bring
3302 * nfsd down and then back up again after changing the lease time.
1da177e4
LT
3303 */
3304void
3305nfs4_reset_lease(time_t leasetime)
3306{
d99a05ad
N
3307 lock_kernel();
3308 user_lease_time = leasetime;
3309 unlock_kernel();
1da177e4 3310}
This page took 0.528104 seconds and 5 git commands to generate.