[PATCH] knfsd: nfsd4: rename state list fields
[linux-2.6/btrfs-unstable.git] / fs / nfsd / nfs4state.c
blob22e76e3f06a57fe40d1d06c9216320caf3431074
1 /*
2 * linux/fs/nfsd/nfs4state.c
4 * Copyright (c) 2001 The Regents of the University of Michigan.
5 * All rights reserved.
7 * Kendrick Smith <kmsmith@umich.edu>
8 * Andy Adamson <kandros@umich.edu>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
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.
37 #include <linux/param.h>
38 #include <linux/major.h>
39 #include <linux/slab.h>
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>
52 #define NFSDDBG_FACILITY NFSDDBG_PROC
54 /* Globals */
55 static time_t lease_time = 90; /* default lease time */
56 static time_t user_lease_time = 90;
57 static time_t boot_time;
58 static int in_grace = 1;
59 static u32 current_clientid = 1;
60 static u32 current_ownerid = 1;
61 static u32 current_fileid = 1;
62 static u32 current_delegid = 1;
63 static u32 nfs4_init;
64 static stateid_t zerostateid; /* bits all 0 */
65 static stateid_t onestateid; /* bits all 1 */
67 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
68 #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
70 /* forward declarations */
71 static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
72 static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
73 static void release_stateid_lockowners(struct nfs4_stateid *open_stp);
75 /* Locking:
77 * client_sema:
78 * protects clientid_hashtbl[], clientstr_hashtbl[],
79 * unconfstr_hashtbl[], uncofid_hashtbl[].
81 static DECLARE_MUTEX(client_sema);
83 static kmem_cache_t *stateowner_slab = NULL;
84 static kmem_cache_t *file_slab = NULL;
85 static kmem_cache_t *stateid_slab = NULL;
86 static kmem_cache_t *deleg_slab = NULL;
88 void
89 nfs4_lock_state(void)
91 down(&client_sema);
94 void
95 nfs4_unlock_state(void)
97 up(&client_sema);
100 static inline u32
101 opaque_hashval(const void *ptr, int nbytes)
103 unsigned char *cptr = (unsigned char *) ptr;
105 u32 x = 0;
106 while (nbytes--) {
107 x *= 37;
108 x += *cptr++;
110 return x;
113 /* forward declarations */
114 static void release_stateowner(struct nfs4_stateowner *sop);
115 static void release_stateid(struct nfs4_stateid *stp, int flags);
118 * Delegation state
121 /* recall_lock protects the del_recall_lru */
122 static spinlock_t recall_lock = SPIN_LOCK_UNLOCKED;
123 static struct list_head del_recall_lru;
125 static void
126 free_nfs4_file(struct kref *kref)
128 struct nfs4_file *fp = container_of(kref, struct nfs4_file, fi_ref);
129 list_del(&fp->fi_hash);
130 iput(fp->fi_inode);
131 kmem_cache_free(file_slab, fp);
134 static inline void
135 put_nfs4_file(struct nfs4_file *fi)
137 kref_put(&fi->fi_ref, free_nfs4_file);
140 static inline void
141 get_nfs4_file(struct nfs4_file *fi)
143 kref_get(&fi->fi_ref);
146 static struct nfs4_delegation *
147 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
149 struct nfs4_delegation *dp;
150 struct nfs4_file *fp = stp->st_file;
151 struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback;
153 dprintk("NFSD alloc_init_deleg\n");
154 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
155 if (dp == NULL)
156 return dp;
157 INIT_LIST_HEAD(&dp->dl_perfile);
158 INIT_LIST_HEAD(&dp->dl_perclnt);
159 INIT_LIST_HEAD(&dp->dl_recall_lru);
160 dp->dl_client = clp;
161 get_nfs4_file(fp);
162 dp->dl_file = fp;
163 dp->dl_flock = NULL;
164 get_file(stp->st_vfs_file);
165 dp->dl_vfs_file = stp->st_vfs_file;
166 dp->dl_type = type;
167 dp->dl_recall.cbr_dp = NULL;
168 dp->dl_recall.cbr_ident = cb->cb_ident;
169 dp->dl_recall.cbr_trunc = 0;
170 dp->dl_stateid.si_boot = boot_time;
171 dp->dl_stateid.si_stateownerid = current_delegid++;
172 dp->dl_stateid.si_fileid = 0;
173 dp->dl_stateid.si_generation = 0;
174 dp->dl_fhlen = current_fh->fh_handle.fh_size;
175 memcpy(dp->dl_fhval, &current_fh->fh_handle.fh_base,
176 current_fh->fh_handle.fh_size);
177 dp->dl_time = 0;
178 atomic_set(&dp->dl_count, 1);
179 list_add(&dp->dl_perfile, &fp->fi_delegations);
180 list_add(&dp->dl_perclnt, &clp->cl_delegations);
181 return dp;
184 void
185 nfs4_put_delegation(struct nfs4_delegation *dp)
187 if (atomic_dec_and_test(&dp->dl_count)) {
188 dprintk("NFSD: freeing dp %p\n",dp);
189 put_nfs4_file(dp->dl_file);
190 kmem_cache_free(deleg_slab, dp);
194 /* Remove the associated file_lock first, then remove the delegation.
195 * lease_modify() is called to remove the FS_LEASE file_lock from
196 * the i_flock list, eventually calling nfsd's lock_manager
197 * fl_release_callback.
199 static void
200 nfs4_close_delegation(struct nfs4_delegation *dp)
202 struct file *filp = dp->dl_vfs_file;
204 dprintk("NFSD: close_delegation dp %p\n",dp);
205 dp->dl_vfs_file = NULL;
206 /* The following nfsd_close may not actually close the file,
207 * but we want to remove the lease in any case. */
208 if (dp->dl_flock)
209 setlease(filp, F_UNLCK, &dp->dl_flock);
210 nfsd_close(filp);
213 /* Called under the state lock. */
214 static void
215 unhash_delegation(struct nfs4_delegation *dp)
217 list_del_init(&dp->dl_perfile);
218 list_del_init(&dp->dl_perclnt);
219 spin_lock(&recall_lock);
220 list_del_init(&dp->dl_recall_lru);
221 spin_unlock(&recall_lock);
222 nfs4_close_delegation(dp);
223 nfs4_put_delegation(dp);
227 * SETCLIENTID state
230 /* Hash tables for nfs4_clientid state */
231 #define CLIENT_HASH_BITS 4
232 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
233 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
235 #define clientid_hashval(id) \
236 ((id) & CLIENT_HASH_MASK)
237 #define clientstr_hashval(name) \
238 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
240 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
241 * used in reboot/reset lease grace period processing
243 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
244 * setclientid_confirmed info.
246 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
247 * setclientid info.
249 * client_lru holds client queue ordered by nfs4_client.cl_time
250 * for lease renewal.
252 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
253 * for last close replay.
255 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
256 static int reclaim_str_hashtbl_size = 0;
257 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
258 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
259 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
260 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
261 static struct list_head client_lru;
262 static struct list_head close_lru;
264 static inline void
265 renew_client(struct nfs4_client *clp)
268 * Move client to the end to the LRU list.
270 dprintk("renewing client (clientid %08x/%08x)\n",
271 clp->cl_clientid.cl_boot,
272 clp->cl_clientid.cl_id);
273 list_move_tail(&clp->cl_lru, &client_lru);
274 clp->cl_time = get_seconds();
277 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
278 static int
279 STALE_CLIENTID(clientid_t *clid)
281 if (clid->cl_boot == boot_time)
282 return 0;
283 dprintk("NFSD stale clientid (%08x/%08x)\n",
284 clid->cl_boot, clid->cl_id);
285 return 1;
289 * XXX Should we use a slab cache ?
290 * This type of memory management is somewhat inefficient, but we use it
291 * anyway since SETCLIENTID is not a common operation.
293 static inline struct nfs4_client *
294 alloc_client(struct xdr_netobj name)
296 struct nfs4_client *clp;
298 if ((clp = kmalloc(sizeof(struct nfs4_client), GFP_KERNEL))!= NULL) {
299 memset(clp, 0, sizeof(*clp));
300 if ((clp->cl_name.data = kmalloc(name.len, GFP_KERNEL)) != NULL) {
301 memcpy(clp->cl_name.data, name.data, name.len);
302 clp->cl_name.len = name.len;
304 else {
305 kfree(clp);
306 clp = NULL;
309 return clp;
312 static inline void
313 free_client(struct nfs4_client *clp)
315 if (clp->cl_cred.cr_group_info)
316 put_group_info(clp->cl_cred.cr_group_info);
317 kfree(clp->cl_name.data);
318 kfree(clp);
321 void
322 put_nfs4_client(struct nfs4_client *clp)
324 if (atomic_dec_and_test(&clp->cl_count))
325 free_client(clp);
328 static void
329 expire_client(struct nfs4_client *clp)
331 struct nfs4_stateowner *sop;
332 struct nfs4_delegation *dp;
333 struct nfs4_callback *cb = &clp->cl_callback;
334 struct rpc_clnt *clnt = clp->cl_callback.cb_client;
335 struct list_head reaplist;
337 dprintk("NFSD: expire_client cl_count %d\n",
338 atomic_read(&clp->cl_count));
340 /* shutdown rpc client, ending any outstanding recall rpcs */
341 if (atomic_read(&cb->cb_set) == 1 && clnt) {
342 rpc_shutdown_client(clnt);
343 clnt = clp->cl_callback.cb_client = NULL;
346 INIT_LIST_HEAD(&reaplist);
347 spin_lock(&recall_lock);
348 while (!list_empty(&clp->cl_delegations)) {
349 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
350 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
351 dp->dl_flock);
352 list_del_init(&dp->dl_perclnt);
353 list_move(&dp->dl_recall_lru, &reaplist);
355 spin_unlock(&recall_lock);
356 while (!list_empty(&reaplist)) {
357 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
358 list_del_init(&dp->dl_recall_lru);
359 unhash_delegation(dp);
361 list_del(&clp->cl_idhash);
362 list_del(&clp->cl_strhash);
363 list_del(&clp->cl_lru);
364 while (!list_empty(&clp->cl_openowners)) {
365 sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
366 release_stateowner(sop);
368 put_nfs4_client(clp);
371 static struct nfs4_client *
372 create_client(struct xdr_netobj name, char *recdir) {
373 struct nfs4_client *clp;
375 if (!(clp = alloc_client(name)))
376 goto out;
377 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
378 atomic_set(&clp->cl_count, 1);
379 atomic_set(&clp->cl_callback.cb_set, 0);
380 clp->cl_callback.cb_parsed = 0;
381 INIT_LIST_HEAD(&clp->cl_idhash);
382 INIT_LIST_HEAD(&clp->cl_strhash);
383 INIT_LIST_HEAD(&clp->cl_openowners);
384 INIT_LIST_HEAD(&clp->cl_delegations);
385 INIT_LIST_HEAD(&clp->cl_lru);
386 out:
387 return clp;
390 static void
391 copy_verf(struct nfs4_client *target, nfs4_verifier *source) {
392 memcpy(target->cl_verifier.data, source->data, sizeof(target->cl_verifier.data));
395 static void
396 copy_clid(struct nfs4_client *target, struct nfs4_client *source) {
397 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
398 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
401 static void
402 copy_cred(struct svc_cred *target, struct svc_cred *source) {
404 target->cr_uid = source->cr_uid;
405 target->cr_gid = source->cr_gid;
406 target->cr_group_info = source->cr_group_info;
407 get_group_info(target->cr_group_info);
410 static inline int
411 same_name(const char *n1, const char *n2) {
412 return 0 == memcmp(n1, n2, HEXDIR_LEN);
415 static int
416 cmp_verf(nfs4_verifier *v1, nfs4_verifier *v2) {
417 return(!memcmp(v1->data,v2->data,sizeof(v1->data)));
420 static int
421 cmp_clid(clientid_t * cl1, clientid_t * cl2) {
422 return((cl1->cl_boot == cl2->cl_boot) &&
423 (cl1->cl_id == cl2->cl_id));
426 /* XXX what about NGROUP */
427 static int
428 cmp_creds(struct svc_cred *cr1, struct svc_cred *cr2){
429 return(cr1->cr_uid == cr2->cr_uid);
433 static void
434 gen_clid(struct nfs4_client *clp) {
435 clp->cl_clientid.cl_boot = boot_time;
436 clp->cl_clientid.cl_id = current_clientid++;
439 static void
440 gen_confirm(struct nfs4_client *clp) {
441 struct timespec tv;
442 u32 * p;
444 tv = CURRENT_TIME;
445 p = (u32 *)clp->cl_confirm.data;
446 *p++ = tv.tv_sec;
447 *p++ = tv.tv_nsec;
450 static int
451 check_name(struct xdr_netobj name) {
453 if (name.len == 0)
454 return 0;
455 if (name.len > NFS4_OPAQUE_LIMIT) {
456 printk("NFSD: check_name: name too long(%d)!\n", name.len);
457 return 0;
459 return 1;
462 static void
463 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
465 unsigned int idhashval;
467 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
468 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
469 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
470 list_add_tail(&clp->cl_lru, &client_lru);
471 clp->cl_time = get_seconds();
474 static void
475 move_to_confirmed(struct nfs4_client *clp)
477 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
478 unsigned int strhashval;
480 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
481 list_del_init(&clp->cl_strhash);
482 list_del_init(&clp->cl_idhash);
483 list_add(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
484 strhashval = clientstr_hashval(clp->cl_recdir);
485 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
486 renew_client(clp);
489 static struct nfs4_client *
490 find_confirmed_client(clientid_t *clid)
492 struct nfs4_client *clp;
493 unsigned int idhashval = clientid_hashval(clid->cl_id);
495 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
496 if (cmp_clid(&clp->cl_clientid, clid))
497 return clp;
499 return NULL;
502 static struct nfs4_client *
503 find_unconfirmed_client(clientid_t *clid)
505 struct nfs4_client *clp;
506 unsigned int idhashval = clientid_hashval(clid->cl_id);
508 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
509 if (cmp_clid(&clp->cl_clientid, clid))
510 return clp;
512 return NULL;
515 static struct nfs4_client *
516 find_confirmed_client_by_str(const char *dname, unsigned int hashval)
518 struct nfs4_client *clp;
520 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
521 if (same_name(clp->cl_recdir, dname))
522 return clp;
524 return NULL;
527 static struct nfs4_client *
528 find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
530 struct nfs4_client *clp;
532 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
533 if (same_name(clp->cl_recdir, dname))
534 return clp;
536 return NULL;
539 /* a helper function for parse_callback */
540 static int
541 parse_octet(unsigned int *lenp, char **addrp)
543 unsigned int len = *lenp;
544 char *p = *addrp;
545 int n = -1;
546 char c;
548 for (;;) {
549 if (!len)
550 break;
551 len--;
552 c = *p++;
553 if (c == '.')
554 break;
555 if ((c < '0') || (c > '9')) {
556 n = -1;
557 break;
559 if (n < 0)
560 n = 0;
561 n = (n * 10) + (c - '0');
562 if (n > 255) {
563 n = -1;
564 break;
567 *lenp = len;
568 *addrp = p;
569 return n;
572 /* parse and set the setclientid ipv4 callback address */
573 static int
574 parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
576 int temp = 0;
577 u32 cbaddr = 0;
578 u16 cbport = 0;
579 u32 addrlen = addr_len;
580 char *addr = addr_val;
581 int i, shift;
583 /* ipaddress */
584 shift = 24;
585 for(i = 4; i > 0 ; i--) {
586 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
587 return 0;
589 cbaddr |= (temp << shift);
590 if (shift > 0)
591 shift -= 8;
593 *cbaddrp = cbaddr;
595 /* port */
596 shift = 8;
597 for(i = 2; i > 0 ; i--) {
598 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
599 return 0;
601 cbport |= (temp << shift);
602 if (shift > 0)
603 shift -= 8;
605 *cbportp = cbport;
606 return 1;
609 static void
610 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
612 struct nfs4_callback *cb = &clp->cl_callback;
614 /* Currently, we only support tcp for the callback channel */
615 if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
616 goto out_err;
618 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
619 &cb->cb_addr, &cb->cb_port)))
620 goto out_err;
621 cb->cb_prog = se->se_callback_prog;
622 cb->cb_ident = se->se_callback_ident;
623 cb->cb_parsed = 1;
624 return;
625 out_err:
626 printk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
627 "will not receive delegations\n",
628 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
630 cb->cb_parsed = 0;
631 return;
635 * RFC 3010 has a complex implmentation description of processing a
636 * SETCLIENTID request consisting of 5 bullets, labeled as
637 * CASE0 - CASE4 below.
639 * NOTES:
640 * callback information will be processed in a future patch
642 * an unconfirmed record is added when:
643 * NORMAL (part of CASE 4): there is no confirmed nor unconfirmed record.
644 * CASE 1: confirmed record found with matching name, principal,
645 * verifier, and clientid.
646 * CASE 2: confirmed record found with matching name, principal,
647 * and there is no unconfirmed record with matching
648 * name and principal
650 * an unconfirmed record is replaced when:
651 * CASE 3: confirmed record found with matching name, principal,
652 * and an unconfirmed record is found with matching
653 * name, principal, and with clientid and
654 * confirm that does not match the confirmed record.
655 * CASE 4: there is no confirmed record with matching name and
656 * principal. there is an unconfirmed record with
657 * matching name, principal.
659 * an unconfirmed record is deleted when:
660 * CASE 1: an unconfirmed record that matches input name, verifier,
661 * and confirmed clientid.
662 * CASE 4: any unconfirmed records with matching name and principal
663 * that exist after an unconfirmed record has been replaced
664 * as described above.
668 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_setclientid *setclid)
670 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
671 struct xdr_netobj clname = {
672 .len = setclid->se_namelen,
673 .data = setclid->se_name,
675 nfs4_verifier clverifier = setclid->se_verf;
676 unsigned int strhashval;
677 struct nfs4_client *conf, *unconf, *new;
678 int status;
679 char dname[HEXDIR_LEN];
681 status = nfserr_inval;
682 if (!check_name(clname))
683 goto out;
685 status = nfs4_make_rec_clidname(dname, &clname);
686 if (status)
687 goto out;
690 * XXX The Duplicate Request Cache (DRC) has been checked (??)
691 * We get here on a DRC miss.
694 strhashval = clientstr_hashval(dname);
696 nfs4_lock_state();
697 conf = find_confirmed_client_by_str(dname, strhashval);
698 if (conf) {
700 * CASE 0:
701 * clname match, confirmed, different principal
702 * or different ip_address
704 status = nfserr_clid_inuse;
705 if (!cmp_creds(&conf->cl_cred, &rqstp->rq_cred)
706 || conf->cl_addr != ip_addr) {
707 printk("NFSD: setclientid: string in use by client"
708 "(clientid %08x/%08x)\n",
709 conf->cl_clientid.cl_boot, conf->cl_clientid.cl_id);
710 goto out;
713 unconf = find_unconfirmed_client_by_str(dname, strhashval);
714 status = nfserr_resource;
715 if (!conf) {
717 * CASE 4:
718 * placed first, because it is the normal case.
720 if (unconf)
721 expire_client(unconf);
722 new = create_client(clname, dname);
723 if (new == NULL)
724 goto out;
725 copy_verf(new, &clverifier);
726 new->cl_addr = ip_addr;
727 copy_cred(&new->cl_cred,&rqstp->rq_cred);
728 gen_clid(new);
729 gen_confirm(new);
730 gen_callback(new, setclid);
731 add_to_unconfirmed(new, strhashval);
732 } else if (cmp_verf(&conf->cl_verifier, &clverifier)) {
734 * CASE 1:
735 * cl_name match, confirmed, principal match
736 * verifier match: probable callback update
738 * remove any unconfirmed nfs4_client with
739 * matching cl_name, cl_verifier, and cl_clientid
741 * create and insert an unconfirmed nfs4_client with same
742 * cl_name, cl_verifier, and cl_clientid as existing
743 * nfs4_client, but with the new callback info and a
744 * new cl_confirm
746 if (unconf) {
747 /* Note this is removing unconfirmed {*x***},
748 * which is stronger than RFC recommended {vxc**}.
749 * This has the advantage that there is at most
750 * one {*x***} in either list at any time.
752 expire_client(unconf);
754 new = create_client(clname, dname);
755 if (new == NULL)
756 goto out;
757 copy_verf(new,&conf->cl_verifier);
758 new->cl_addr = ip_addr;
759 copy_cred(&new->cl_cred,&rqstp->rq_cred);
760 copy_clid(new, conf);
761 gen_confirm(new);
762 gen_callback(new, setclid);
763 add_to_unconfirmed(new,strhashval);
764 } else if (!unconf) {
766 * CASE 2:
767 * clname match, confirmed, principal match
768 * verfier does not match
769 * no unconfirmed. create a new unconfirmed nfs4_client
770 * using input clverifier, clname, and callback info
771 * and generate a new cl_clientid and cl_confirm.
773 new = create_client(clname, dname);
774 if (new == NULL)
775 goto out;
776 copy_verf(new,&clverifier);
777 new->cl_addr = ip_addr;
778 copy_cred(&new->cl_cred,&rqstp->rq_cred);
779 gen_clid(new);
780 gen_confirm(new);
781 gen_callback(new, setclid);
782 add_to_unconfirmed(new, strhashval);
783 } else if (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm)) {
785 * CASE3:
786 * confirmed found (name, principal match)
787 * confirmed verifier does not match input clverifier
789 * unconfirmed found (name match)
790 * confirmed->cl_confirm != unconfirmed->cl_confirm
792 * remove unconfirmed.
794 * create an unconfirmed nfs4_client
795 * with same cl_name as existing confirmed nfs4_client,
796 * but with new callback info, new cl_clientid,
797 * new cl_verifier and a new cl_confirm
799 expire_client(unconf);
800 new = create_client(clname, dname);
801 if (new == NULL)
802 goto out;
803 copy_verf(new,&clverifier);
804 new->cl_addr = ip_addr;
805 copy_cred(&new->cl_cred,&rqstp->rq_cred);
806 gen_clid(new);
807 gen_confirm(new);
808 gen_callback(new, setclid);
809 add_to_unconfirmed(new, strhashval);
810 } else {
811 /* No cases hit !!! */
812 status = nfserr_inval;
813 goto out;
816 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
817 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
818 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
819 status = nfs_ok;
820 out:
821 nfs4_unlock_state();
822 return status;
827 * RFC 3010 has a complex implmentation description of processing a
828 * SETCLIENTID_CONFIRM request consisting of 4 bullets describing
829 * processing on a DRC miss, labeled as CASE1 - CASE4 below.
831 * NOTE: callback information will be processed here in a future patch
834 nfsd4_setclientid_confirm(struct svc_rqst *rqstp, struct nfsd4_setclientid_confirm *setclientid_confirm)
836 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
837 struct nfs4_client *conf, *unconf;
838 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
839 clientid_t * clid = &setclientid_confirm->sc_clientid;
840 int status;
842 if (STALE_CLIENTID(clid))
843 return nfserr_stale_clientid;
845 * XXX The Duplicate Request Cache (DRC) has been checked (??)
846 * We get here on a DRC miss.
849 nfs4_lock_state();
851 conf = find_confirmed_client(clid);
852 unconf = find_unconfirmed_client(clid);
854 status = nfserr_clid_inuse;
855 if (conf && conf->cl_addr != ip_addr)
856 goto out;
857 if (unconf && unconf->cl_addr != ip_addr)
858 goto out;
860 if ((conf && unconf) &&
861 (cmp_verf(&unconf->cl_confirm, &confirm)) &&
862 (cmp_verf(&conf->cl_verifier, &unconf->cl_verifier)) &&
863 (same_name(conf->cl_recdir,unconf->cl_recdir)) &&
864 (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm))) {
865 /* CASE 1:
866 * unconf record that matches input clientid and input confirm.
867 * conf record that matches input clientid.
868 * conf and unconf records match names, verifiers
870 if (!cmp_creds(&conf->cl_cred, &unconf->cl_cred))
871 status = nfserr_clid_inuse;
872 else {
873 /* XXX: We just turn off callbacks until we can handle
874 * change request correctly. */
875 conf->cl_callback.cb_parsed = 0;
876 gen_confirm(conf);
877 expire_client(unconf);
878 status = nfs_ok;
881 } else if ((conf && !unconf) ||
882 ((conf && unconf) &&
883 (!cmp_verf(&conf->cl_verifier, &unconf->cl_verifier) ||
884 !same_name(conf->cl_recdir, unconf->cl_recdir)))) {
885 /* CASE 2:
886 * conf record that matches input clientid.
887 * if unconf record matches input clientid, then
888 * unconf->cl_name or unconf->cl_verifier don't match the
889 * conf record.
891 if (!cmp_creds(&conf->cl_cred,&rqstp->rq_cred))
892 status = nfserr_clid_inuse;
893 else
894 status = nfs_ok;
895 } else if (!conf && unconf
896 && cmp_verf(&unconf->cl_confirm, &confirm)) {
897 /* CASE 3:
898 * conf record not found.
899 * unconf record found.
900 * unconf->cl_confirm matches input confirm
902 if (!cmp_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
903 status = nfserr_clid_inuse;
904 } else {
905 unsigned int hash =
906 clientstr_hashval(unconf->cl_recdir);
907 conf = find_confirmed_client_by_str(unconf->cl_recdir,
908 hash);
909 if (conf) {
910 expire_client(conf);
912 move_to_confirmed(unconf);
913 conf = unconf;
914 status = nfs_ok;
916 } else if ((!conf || (conf && !cmp_verf(&conf->cl_confirm, &confirm)))
917 && (!unconf || (unconf && !cmp_verf(&unconf->cl_confirm,
918 &confirm)))) {
919 /* CASE 4:
920 * conf record not found, or if conf, conf->cl_confirm does not
921 * match input confirm.
922 * unconf record not found, or if unconf, unconf->cl_confirm
923 * does not match input confirm.
925 status = nfserr_stale_clientid;
926 } else {
927 /* check that we have hit one of the cases...*/
928 status = nfserr_clid_inuse;
930 out:
931 if (!status)
932 nfsd4_probe_callback(conf);
933 nfs4_unlock_state();
934 return status;
938 * Open owner state (share locks)
941 /* hash tables for nfs4_stateowner */
942 #define OWNER_HASH_BITS 8
943 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
944 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
946 #define ownerid_hashval(id) \
947 ((id) & OWNER_HASH_MASK)
948 #define ownerstr_hashval(clientid, ownername) \
949 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
951 static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
952 static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
954 /* hash table for nfs4_file */
955 #define FILE_HASH_BITS 8
956 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
957 #define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
958 /* hash table for (open)nfs4_stateid */
959 #define STATEID_HASH_BITS 10
960 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
961 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
963 #define file_hashval(x) \
964 hash_ptr(x, FILE_HASH_BITS)
965 #define stateid_hashval(owner_id, file_id) \
966 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
968 static struct list_head file_hashtbl[FILE_HASH_SIZE];
969 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
971 /* OPEN Share state helper functions */
972 static inline struct nfs4_file *
973 alloc_init_file(struct inode *ino)
975 struct nfs4_file *fp;
976 unsigned int hashval = file_hashval(ino);
978 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
979 if (fp) {
980 kref_init(&fp->fi_ref);
981 INIT_LIST_HEAD(&fp->fi_hash);
982 INIT_LIST_HEAD(&fp->fi_stateids);
983 INIT_LIST_HEAD(&fp->fi_delegations);
984 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
985 fp->fi_inode = igrab(ino);
986 fp->fi_id = current_fileid++;
987 return fp;
989 return NULL;
992 static void
993 nfsd4_free_slab(kmem_cache_t **slab)
995 int status;
997 if (*slab == NULL)
998 return;
999 status = kmem_cache_destroy(*slab);
1000 *slab = NULL;
1001 WARN_ON(status);
1004 static void
1005 nfsd4_free_slabs(void)
1007 nfsd4_free_slab(&stateowner_slab);
1008 nfsd4_free_slab(&file_slab);
1009 nfsd4_free_slab(&stateid_slab);
1010 nfsd4_free_slab(&deleg_slab);
1013 static int
1014 nfsd4_init_slabs(void)
1016 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
1017 sizeof(struct nfs4_stateowner), 0, 0, NULL, NULL);
1018 if (stateowner_slab == NULL)
1019 goto out_nomem;
1020 file_slab = kmem_cache_create("nfsd4_files",
1021 sizeof(struct nfs4_file), 0, 0, NULL, NULL);
1022 if (file_slab == NULL)
1023 goto out_nomem;
1024 stateid_slab = kmem_cache_create("nfsd4_stateids",
1025 sizeof(struct nfs4_stateid), 0, 0, NULL, NULL);
1026 if (stateid_slab == NULL)
1027 goto out_nomem;
1028 deleg_slab = kmem_cache_create("nfsd4_delegations",
1029 sizeof(struct nfs4_delegation), 0, 0, NULL, NULL);
1030 if (deleg_slab == NULL)
1031 goto out_nomem;
1032 return 0;
1033 out_nomem:
1034 nfsd4_free_slabs();
1035 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1036 return -ENOMEM;
1039 void
1040 nfs4_free_stateowner(struct kref *kref)
1042 struct nfs4_stateowner *sop =
1043 container_of(kref, struct nfs4_stateowner, so_ref);
1044 kfree(sop->so_owner.data);
1045 kmem_cache_free(stateowner_slab, sop);
1048 static inline struct nfs4_stateowner *
1049 alloc_stateowner(struct xdr_netobj *owner)
1051 struct nfs4_stateowner *sop;
1053 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1054 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1055 memcpy(sop->so_owner.data, owner->data, owner->len);
1056 sop->so_owner.len = owner->len;
1057 kref_init(&sop->so_ref);
1058 return sop;
1060 kmem_cache_free(stateowner_slab, sop);
1062 return NULL;
1065 static struct nfs4_stateowner *
1066 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1067 struct nfs4_stateowner *sop;
1068 struct nfs4_replay *rp;
1069 unsigned int idhashval;
1071 if (!(sop = alloc_stateowner(&open->op_owner)))
1072 return NULL;
1073 idhashval = ownerid_hashval(current_ownerid);
1074 INIT_LIST_HEAD(&sop->so_idhash);
1075 INIT_LIST_HEAD(&sop->so_strhash);
1076 INIT_LIST_HEAD(&sop->so_perclient);
1077 INIT_LIST_HEAD(&sop->so_stateids);
1078 INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
1079 INIT_LIST_HEAD(&sop->so_close_lru);
1080 sop->so_time = 0;
1081 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1082 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
1083 list_add(&sop->so_perclient, &clp->cl_openowners);
1084 sop->so_is_open_owner = 1;
1085 sop->so_id = current_ownerid++;
1086 sop->so_client = clp;
1087 sop->so_seqid = open->op_seqid;
1088 sop->so_confirmed = 0;
1089 rp = &sop->so_replay;
1090 rp->rp_status = NFSERR_SERVERFAULT;
1091 rp->rp_buflen = 0;
1092 rp->rp_buf = rp->rp_ibuf;
1093 return sop;
1096 static void
1097 release_stateid_lockowners(struct nfs4_stateid *open_stp)
1099 struct nfs4_stateowner *lock_sop;
1101 while (!list_empty(&open_stp->st_lockowners)) {
1102 lock_sop = list_entry(open_stp->st_lockowners.next,
1103 struct nfs4_stateowner, so_perstateid);
1104 /* list_del(&open_stp->st_lockowners); */
1105 BUG_ON(lock_sop->so_is_open_owner);
1106 release_stateowner(lock_sop);
1110 static void
1111 unhash_stateowner(struct nfs4_stateowner *sop)
1113 struct nfs4_stateid *stp;
1115 list_del(&sop->so_idhash);
1116 list_del(&sop->so_strhash);
1117 if (sop->so_is_open_owner)
1118 list_del(&sop->so_perclient);
1119 list_del(&sop->so_perstateid);
1120 while (!list_empty(&sop->so_stateids)) {
1121 stp = list_entry(sop->so_stateids.next,
1122 struct nfs4_stateid, st_perstateowner);
1123 if (sop->so_is_open_owner)
1124 release_stateid(stp, OPEN_STATE);
1125 else
1126 release_stateid(stp, LOCK_STATE);
1130 static void
1131 release_stateowner(struct nfs4_stateowner *sop)
1133 unhash_stateowner(sop);
1134 list_del(&sop->so_close_lru);
1135 nfs4_put_stateowner(sop);
1138 static inline void
1139 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1140 struct nfs4_stateowner *sop = open->op_stateowner;
1141 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1143 INIT_LIST_HEAD(&stp->st_hash);
1144 INIT_LIST_HEAD(&stp->st_perstateowner);
1145 INIT_LIST_HEAD(&stp->st_lockowners);
1146 INIT_LIST_HEAD(&stp->st_perfile);
1147 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
1148 list_add(&stp->st_perstateowner, &sop->so_stateids);
1149 list_add(&stp->st_perfile, &fp->fi_stateids);
1150 stp->st_stateowner = sop;
1151 get_nfs4_file(fp);
1152 stp->st_file = fp;
1153 stp->st_stateid.si_boot = boot_time;
1154 stp->st_stateid.si_stateownerid = sop->so_id;
1155 stp->st_stateid.si_fileid = fp->fi_id;
1156 stp->st_stateid.si_generation = 0;
1157 stp->st_access_bmap = 0;
1158 stp->st_deny_bmap = 0;
1159 __set_bit(open->op_share_access, &stp->st_access_bmap);
1160 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
1163 static void
1164 release_stateid(struct nfs4_stateid *stp, int flags)
1166 struct file *filp = stp->st_vfs_file;
1168 list_del(&stp->st_hash);
1169 list_del(&stp->st_perfile);
1170 list_del(&stp->st_perstateowner);
1171 if (flags & OPEN_STATE) {
1172 release_stateid_lockowners(stp);
1173 stp->st_vfs_file = NULL;
1174 nfsd_close(filp);
1175 } else if (flags & LOCK_STATE)
1176 locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
1177 put_nfs4_file(stp->st_file);
1178 kmem_cache_free(stateid_slab, stp);
1179 stp = NULL;
1182 static void
1183 move_to_close_lru(struct nfs4_stateowner *sop)
1185 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1187 unhash_stateowner(sop);
1188 list_add_tail(&sop->so_close_lru, &close_lru);
1189 sop->so_time = get_seconds();
1192 static void
1193 release_state_owner(struct nfs4_stateid *stp, int flag)
1195 struct nfs4_stateowner *sop = stp->st_stateowner;
1197 dprintk("NFSD: release_state_owner\n");
1198 release_stateid(stp, flag);
1200 /* place unused nfs4_stateowners on so_close_lru list to be
1201 * released by the laundromat service after the lease period
1202 * to enable us to handle CLOSE replay
1204 if (sop->so_confirmed && list_empty(&sop->so_stateids))
1205 move_to_close_lru(sop);
1208 static int
1209 cmp_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, clientid_t *clid) {
1210 return ((sop->so_owner.len == owner->len) &&
1211 !memcmp(sop->so_owner.data, owner->data, owner->len) &&
1212 (sop->so_client->cl_clientid.cl_id == clid->cl_id));
1215 static struct nfs4_stateowner *
1216 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1218 struct nfs4_stateowner *so = NULL;
1220 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
1221 if (cmp_owner_str(so, &open->op_owner, &open->op_clientid))
1222 return so;
1224 return NULL;
1227 /* search file_hashtbl[] for file */
1228 static struct nfs4_file *
1229 find_file(struct inode *ino)
1231 unsigned int hashval = file_hashval(ino);
1232 struct nfs4_file *fp;
1234 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
1235 if (fp->fi_inode == ino) {
1236 get_nfs4_file(fp);
1237 return fp;
1240 return NULL;
1243 #define TEST_ACCESS(x) ((x > 0 || x < 4)?1:0)
1244 #define TEST_DENY(x) ((x >= 0 || x < 5)?1:0)
1246 static void
1247 set_access(unsigned int *access, unsigned long bmap) {
1248 int i;
1250 *access = 0;
1251 for (i = 1; i < 4; i++) {
1252 if (test_bit(i, &bmap))
1253 *access |= i;
1257 static void
1258 set_deny(unsigned int *deny, unsigned long bmap) {
1259 int i;
1261 *deny = 0;
1262 for (i = 0; i < 4; i++) {
1263 if (test_bit(i, &bmap))
1264 *deny |= i ;
1268 static int
1269 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1270 unsigned int access, deny;
1272 set_access(&access, stp->st_access_bmap);
1273 set_deny(&deny, stp->st_deny_bmap);
1274 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1275 return 0;
1276 return 1;
1280 * Called to check deny when READ with all zero stateid or
1281 * WRITE with all zero or all one stateid
1283 static int
1284 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1286 struct inode *ino = current_fh->fh_dentry->d_inode;
1287 struct nfs4_file *fp;
1288 struct nfs4_stateid *stp;
1289 int ret;
1291 dprintk("NFSD: nfs4_share_conflict\n");
1293 fp = find_file(ino);
1294 if (!fp)
1295 return nfs_ok;
1296 ret = nfserr_share_denied;
1297 /* Search for conflicting share reservations */
1298 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1299 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1300 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1301 goto out;
1303 ret = nfs_ok;
1304 out:
1305 put_nfs4_file(fp);
1306 return ret;
1309 static inline void
1310 nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1312 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1313 put_write_access(filp->f_dentry->d_inode);
1314 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1319 * Recall a delegation
1321 static int
1322 do_recall(void *__dp)
1324 struct nfs4_delegation *dp = __dp;
1326 daemonize("nfsv4-recall");
1328 nfsd4_cb_recall(dp);
1329 return 0;
1333 * Spawn a thread to perform a recall on the delegation represented
1334 * by the lease (file_lock)
1336 * Called from break_lease() with lock_kernel() held.
1337 * Note: we assume break_lease will only call this *once* for any given
1338 * lease.
1340 static
1341 void nfsd_break_deleg_cb(struct file_lock *fl)
1343 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1344 struct task_struct *t;
1346 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1347 if (!dp)
1348 return;
1350 /* We're assuming the state code never drops its reference
1351 * without first removing the lease. Since we're in this lease
1352 * callback (and since the lease code is serialized by the kernel
1353 * lock) we know the server hasn't removed the lease yet, we know
1354 * it's safe to take a reference: */
1355 atomic_inc(&dp->dl_count);
1357 spin_lock(&recall_lock);
1358 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1359 spin_unlock(&recall_lock);
1361 /* only place dl_time is set. protected by lock_kernel*/
1362 dp->dl_time = get_seconds();
1364 /* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */
1365 fl->fl_break_time = jiffies + NFSD_LEASE_TIME * HZ;
1367 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1368 if (IS_ERR(t)) {
1369 struct nfs4_client *clp = dp->dl_client;
1371 printk(KERN_INFO "NFSD: Callback thread failed for "
1372 "for client (clientid %08x/%08x)\n",
1373 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1374 nfs4_put_delegation(dp);
1379 * The file_lock is being reapd.
1381 * Called by locks_free_lock() with lock_kernel() held.
1383 static
1384 void nfsd_release_deleg_cb(struct file_lock *fl)
1386 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1388 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1390 if (!(fl->fl_flags & FL_LEASE) || !dp)
1391 return;
1392 dp->dl_flock = NULL;
1396 * Set the delegation file_lock back pointer.
1398 * Called from __setlease() with lock_kernel() held.
1400 static
1401 void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1403 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1405 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1406 if (!dp)
1407 return;
1408 dp->dl_flock = new;
1412 * Called from __setlease() with lock_kernel() held
1414 static
1415 int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1417 struct nfs4_delegation *onlistd =
1418 (struct nfs4_delegation *)onlist->fl_owner;
1419 struct nfs4_delegation *tryd =
1420 (struct nfs4_delegation *)try->fl_owner;
1422 if (onlist->fl_lmops != try->fl_lmops)
1423 return 0;
1425 return onlistd->dl_client == tryd->dl_client;
1429 static
1430 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1432 if (arg & F_UNLCK)
1433 return lease_modify(onlist, arg);
1434 else
1435 return -EAGAIN;
1438 static struct lock_manager_operations nfsd_lease_mng_ops = {
1439 .fl_break = nfsd_break_deleg_cb,
1440 .fl_release_private = nfsd_release_deleg_cb,
1441 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1442 .fl_mylease = nfsd_same_client_deleg_cb,
1443 .fl_change = nfsd_change_deleg_cb,
1448 * nfsd4_process_open1()
1449 * lookup stateowner.
1450 * found:
1451 * check confirmed
1452 * confirmed:
1453 * check seqid
1454 * not confirmed:
1455 * delete owner
1456 * create new owner
1457 * notfound:
1458 * verify clientid
1459 * create new owner
1461 * called with nfs4_lock_state() held.
1464 nfsd4_process_open1(struct nfsd4_open *open)
1466 int status;
1467 clientid_t *clientid = &open->op_clientid;
1468 struct nfs4_client *clp = NULL;
1469 unsigned int strhashval;
1470 struct nfs4_stateowner *sop = NULL;
1472 status = nfserr_inval;
1473 if (!check_name(open->op_owner))
1474 goto out;
1476 if (STALE_CLIENTID(&open->op_clientid))
1477 return nfserr_stale_clientid;
1479 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1480 sop = find_openstateowner_str(strhashval, open);
1481 if (sop) {
1482 open->op_stateowner = sop;
1483 /* check for replay */
1484 if (open->op_seqid == sop->so_seqid){
1485 if (sop->so_replay.rp_buflen)
1486 return NFSERR_REPLAY_ME;
1487 else {
1488 /* The original OPEN failed so spectacularly
1489 * that we don't even have replay data saved!
1490 * Therefore, we have no choice but to continue
1491 * processing this OPEN; presumably, we'll
1492 * fail again for the same reason.
1494 dprintk("nfsd4_process_open1:"
1495 " replay with no replay cache\n");
1496 goto renew;
1498 } else if (sop->so_confirmed) {
1499 if (open->op_seqid == sop->so_seqid + 1)
1500 goto renew;
1501 status = nfserr_bad_seqid;
1502 goto out;
1503 } else {
1504 /* If we get here, we received an OPEN for an
1505 * unconfirmed nfs4_stateowner. Since the seqid's are
1506 * different, purge the existing nfs4_stateowner, and
1507 * instantiate a new one.
1509 clp = sop->so_client;
1510 release_stateowner(sop);
1512 } else {
1513 /* nfs4_stateowner not found.
1514 * Verify clientid and instantiate new nfs4_stateowner.
1515 * If verify fails this is presumably the result of the
1516 * client's lease expiring.
1518 status = nfserr_expired;
1519 clp = find_confirmed_client(clientid);
1520 if (clp == NULL)
1521 goto out;
1523 status = nfserr_resource;
1524 sop = alloc_init_open_stateowner(strhashval, clp, open);
1525 if (sop == NULL)
1526 goto out;
1527 open->op_stateowner = sop;
1528 renew:
1529 status = nfs_ok;
1530 renew_client(sop->so_client);
1531 out:
1532 if (status && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1533 status = nfserr_reclaim_bad;
1534 return status;
1537 static inline int
1538 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1540 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1541 return nfserr_openmode;
1542 else
1543 return nfs_ok;
1546 static struct nfs4_delegation *
1547 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1549 struct nfs4_delegation *dp;
1551 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) {
1552 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1553 return dp;
1555 return NULL;
1558 static int
1559 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1560 struct nfs4_delegation **dp)
1562 int flags;
1563 int status = nfserr_bad_stateid;
1565 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1566 if (*dp == NULL)
1567 goto out;
1568 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1569 RD_STATE : WR_STATE;
1570 status = nfs4_check_delegmode(*dp, flags);
1571 if (status)
1572 *dp = NULL;
1573 out:
1574 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1575 return nfs_ok;
1576 if (status)
1577 return status;
1578 open->op_stateowner->so_confirmed = 1;
1579 return nfs_ok;
1582 static int
1583 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1585 struct nfs4_stateid *local;
1586 int status = nfserr_share_denied;
1587 struct nfs4_stateowner *sop = open->op_stateowner;
1589 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
1590 /* ignore lock owners */
1591 if (local->st_stateowner->so_is_open_owner == 0)
1592 continue;
1593 /* remember if we have seen this open owner */
1594 if (local->st_stateowner == sop)
1595 *stpp = local;
1596 /* check for conflicting share reservations */
1597 if (!test_share(local, open))
1598 goto out;
1600 status = 0;
1601 out:
1602 return status;
1605 static inline struct nfs4_stateid *
1606 nfs4_alloc_stateid(void)
1608 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
1611 static int
1612 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
1613 struct nfs4_delegation *dp,
1614 struct svc_fh *cur_fh, int flags)
1616 struct nfs4_stateid *stp;
1618 stp = nfs4_alloc_stateid();
1619 if (stp == NULL)
1620 return nfserr_resource;
1622 if (dp) {
1623 get_file(dp->dl_vfs_file);
1624 stp->st_vfs_file = dp->dl_vfs_file;
1625 } else {
1626 int status;
1627 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1628 &stp->st_vfs_file);
1629 if (status) {
1630 if (status == nfserr_dropit)
1631 status = nfserr_jukebox;
1632 kmem_cache_free(stateid_slab, stp);
1633 return status;
1636 *stpp = stp;
1637 return 0;
1640 static inline int
1641 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1642 struct nfsd4_open *open)
1644 struct iattr iattr = {
1645 .ia_valid = ATTR_SIZE,
1646 .ia_size = 0,
1648 if (!open->op_truncate)
1649 return 0;
1650 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
1651 return -EINVAL;
1652 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1655 static int
1656 nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1658 struct file *filp = stp->st_vfs_file;
1659 struct inode *inode = filp->f_dentry->d_inode;
1660 unsigned int share_access;
1661 int status;
1663 set_access(&share_access, stp->st_access_bmap);
1664 share_access = ~share_access;
1665 share_access &= open->op_share_access;
1667 if (!(share_access & NFS4_SHARE_ACCESS_WRITE))
1668 return nfsd4_truncate(rqstp, cur_fh, open);
1670 status = get_write_access(inode);
1671 if (status)
1672 return nfserrno(status);
1673 status = nfsd4_truncate(rqstp, cur_fh, open);
1674 if (status) {
1675 put_write_access(inode);
1676 return status;
1678 /* remember the open */
1679 filp->f_mode = (filp->f_mode | FMODE_WRITE) & ~FMODE_READ;
1680 set_bit(open->op_share_access, &stp->st_access_bmap);
1681 set_bit(open->op_share_deny, &stp->st_deny_bmap);
1683 return nfs_ok;
1687 /* decrement seqid on successful reclaim, it will be bumped in encode_open */
1688 static void
1689 nfs4_set_claim_prev(struct nfsd4_open *open, int *status)
1691 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) {
1692 if (*status)
1693 *status = nfserr_reclaim_bad;
1694 else {
1695 open->op_stateowner->so_confirmed = 1;
1696 open->op_stateowner->so_seqid--;
1702 * Attempt to hand out a delegation.
1704 static void
1705 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1707 struct nfs4_delegation *dp;
1708 struct nfs4_stateowner *sop = stp->st_stateowner;
1709 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1710 struct file_lock fl, *flp = &fl;
1711 int status, flag = 0;
1713 flag = NFS4_OPEN_DELEGATE_NONE;
1714 open->op_recall = 0;
1715 switch (open->op_claim_type) {
1716 case NFS4_OPEN_CLAIM_PREVIOUS:
1717 if (!atomic_read(&cb->cb_set))
1718 open->op_recall = 1;
1719 flag = open->op_delegate_type;
1720 if (flag == NFS4_OPEN_DELEGATE_NONE)
1721 goto out;
1722 break;
1723 case NFS4_OPEN_CLAIM_NULL:
1724 /* Let's not give out any delegations till everyone's
1725 * had the chance to reclaim theirs.... */
1726 if (nfs4_in_grace())
1727 goto out;
1728 if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
1729 goto out;
1730 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1731 flag = NFS4_OPEN_DELEGATE_WRITE;
1732 else
1733 flag = NFS4_OPEN_DELEGATE_READ;
1734 break;
1735 default:
1736 goto out;
1739 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1740 if (dp == NULL) {
1741 flag = NFS4_OPEN_DELEGATE_NONE;
1742 goto out;
1744 locks_init_lock(&fl);
1745 fl.fl_lmops = &nfsd_lease_mng_ops;
1746 fl.fl_flags = FL_LEASE;
1747 fl.fl_end = OFFSET_MAX;
1748 fl.fl_owner = (fl_owner_t)dp;
1749 fl.fl_file = stp->st_vfs_file;
1750 fl.fl_pid = current->tgid;
1752 /* setlease checks to see if delegation should be handed out.
1753 * the lock_manager callbacks fl_mylease and fl_change are used
1755 if ((status = setlease(stp->st_vfs_file,
1756 flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) {
1757 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
1758 unhash_delegation(dp);
1759 flag = NFS4_OPEN_DELEGATE_NONE;
1760 goto out;
1763 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1765 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1766 dp->dl_stateid.si_boot,
1767 dp->dl_stateid.si_stateownerid,
1768 dp->dl_stateid.si_fileid,
1769 dp->dl_stateid.si_generation);
1770 out:
1771 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
1772 && flag == NFS4_OPEN_DELEGATE_NONE
1773 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
1774 printk("NFSD: WARNING: refusing delegation reclaim\n");
1775 open->op_delegate_type = flag;
1779 * called with nfs4_lock_state() held.
1782 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1784 struct nfs4_file *fp = NULL;
1785 struct inode *ino = current_fh->fh_dentry->d_inode;
1786 struct nfs4_stateid *stp = NULL;
1787 struct nfs4_delegation *dp = NULL;
1788 int status;
1790 status = nfserr_inval;
1791 if (!TEST_ACCESS(open->op_share_access) || !TEST_DENY(open->op_share_deny))
1792 goto out;
1794 * Lookup file; if found, lookup stateid and check open request,
1795 * and check for delegations in the process of being recalled.
1796 * If not found, create the nfs4_file struct
1798 fp = find_file(ino);
1799 if (fp) {
1800 if ((status = nfs4_check_open(fp, open, &stp)))
1801 goto out;
1802 status = nfs4_check_deleg(fp, open, &dp);
1803 if (status)
1804 goto out;
1805 } else {
1806 status = nfserr_bad_stateid;
1807 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1808 goto out;
1809 status = nfserr_resource;
1810 fp = alloc_init_file(ino);
1811 if (fp == NULL)
1812 goto out;
1816 * OPEN the file, or upgrade an existing OPEN.
1817 * If truncate fails, the OPEN fails.
1819 if (stp) {
1820 /* Stateid was found, this is an OPEN upgrade */
1821 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1822 if (status)
1823 goto out;
1824 } else {
1825 /* Stateid was not found, this is a new OPEN */
1826 int flags = 0;
1827 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1828 flags = MAY_WRITE;
1829 else
1830 flags = MAY_READ;
1831 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1832 if (status)
1833 goto out;
1834 init_stateid(stp, fp, open);
1835 status = nfsd4_truncate(rqstp, current_fh, open);
1836 if (status) {
1837 release_stateid(stp, OPEN_STATE);
1838 goto out;
1841 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1844 * Attempt to hand out a delegation. No error return, because the
1845 * OPEN succeeds even if we fail.
1847 nfs4_open_delegation(current_fh, open, stp);
1849 status = nfs_ok;
1851 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1852 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1853 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1854 out:
1855 if (fp)
1856 put_nfs4_file(fp);
1857 /* CLAIM_PREVIOUS has different error returns */
1858 nfs4_set_claim_prev(open, &status);
1860 * To finish the open response, we just need to set the rflags.
1862 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1863 if (!open->op_stateowner->so_confirmed)
1864 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1866 return status;
1869 static struct workqueue_struct *laundry_wq;
1870 static struct work_struct laundromat_work;
1871 static void laundromat_main(void *);
1872 static DECLARE_WORK(laundromat_work, laundromat_main, NULL);
1874 int
1875 nfsd4_renew(clientid_t *clid)
1877 struct nfs4_client *clp;
1878 int status;
1880 nfs4_lock_state();
1881 dprintk("process_renew(%08x/%08x): starting\n",
1882 clid->cl_boot, clid->cl_id);
1883 status = nfserr_stale_clientid;
1884 if (STALE_CLIENTID(clid))
1885 goto out;
1886 clp = find_confirmed_client(clid);
1887 status = nfserr_expired;
1888 if (clp == NULL) {
1889 /* We assume the client took too long to RENEW. */
1890 dprintk("nfsd4_renew: clientid not found!\n");
1891 goto out;
1893 renew_client(clp);
1894 status = nfserr_cb_path_down;
1895 if (!list_empty(&clp->cl_delegations)
1896 && !atomic_read(&clp->cl_callback.cb_set))
1897 goto out;
1898 status = nfs_ok;
1899 out:
1900 nfs4_unlock_state();
1901 return status;
1904 static void
1905 end_grace(void)
1907 dprintk("NFSD: end of grace period\n");
1908 in_grace = 0;
1911 static time_t
1912 nfs4_laundromat(void)
1914 struct nfs4_client *clp;
1915 struct nfs4_stateowner *sop;
1916 struct nfs4_delegation *dp;
1917 struct list_head *pos, *next, reaplist;
1918 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1919 time_t t, clientid_val = NFSD_LEASE_TIME;
1920 time_t u, test_val = NFSD_LEASE_TIME;
1922 nfs4_lock_state();
1924 dprintk("NFSD: laundromat service - starting\n");
1925 if (in_grace)
1926 end_grace();
1927 list_for_each_safe(pos, next, &client_lru) {
1928 clp = list_entry(pos, struct nfs4_client, cl_lru);
1929 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1930 t = clp->cl_time - cutoff;
1931 if (clientid_val > t)
1932 clientid_val = t;
1933 break;
1935 dprintk("NFSD: purging unused client (clientid %08x)\n",
1936 clp->cl_clientid.cl_id);
1937 expire_client(clp);
1939 INIT_LIST_HEAD(&reaplist);
1940 spin_lock(&recall_lock);
1941 list_for_each_safe(pos, next, &del_recall_lru) {
1942 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1943 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1944 u = dp->dl_time - cutoff;
1945 if (test_val > u)
1946 test_val = u;
1947 break;
1949 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1950 dp, dp->dl_flock);
1951 list_move(&dp->dl_recall_lru, &reaplist);
1953 spin_unlock(&recall_lock);
1954 list_for_each_safe(pos, next, &reaplist) {
1955 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1956 list_del_init(&dp->dl_recall_lru);
1957 unhash_delegation(dp);
1959 test_val = NFSD_LEASE_TIME;
1960 list_for_each_safe(pos, next, &close_lru) {
1961 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1962 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1963 u = sop->so_time - cutoff;
1964 if (test_val > u)
1965 test_val = u;
1966 break;
1968 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1969 sop->so_id);
1970 list_del(&sop->so_close_lru);
1971 nfs4_put_stateowner(sop);
1973 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1974 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1975 nfs4_unlock_state();
1976 return clientid_val;
1979 void
1980 laundromat_main(void *not_used)
1982 time_t t;
1984 t = nfs4_laundromat();
1985 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
1986 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
1989 /* search ownerid_hashtbl[] and close_lru for stateid owner
1990 * (stateid->si_stateownerid)
1992 static struct nfs4_stateowner *
1993 find_openstateowner_id(u32 st_id, int flags) {
1994 struct nfs4_stateowner *local = NULL;
1996 dprintk("NFSD: find_openstateowner_id %d\n", st_id);
1997 if (flags & CLOSE_STATE) {
1998 list_for_each_entry(local, &close_lru, so_close_lru) {
1999 if (local->so_id == st_id)
2000 return local;
2003 return NULL;
2006 static inline int
2007 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
2009 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_dentry->d_inode;
2012 static int
2013 STALE_STATEID(stateid_t *stateid)
2015 if (stateid->si_boot == boot_time)
2016 return 0;
2017 printk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
2018 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2019 stateid->si_generation);
2020 return 1;
2023 static inline int
2024 access_permit_read(unsigned long access_bmap)
2026 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
2027 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
2028 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
2031 static inline int
2032 access_permit_write(unsigned long access_bmap)
2034 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
2035 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
2038 static
2039 int nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
2041 int status = nfserr_openmode;
2043 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2044 goto out;
2045 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2046 goto out;
2047 status = nfs_ok;
2048 out:
2049 return status;
2052 static inline int
2053 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2055 /* Trying to call delegreturn with a special stateid? Yuch: */
2056 if (!(flags & (RD_STATE | WR_STATE)))
2057 return nfserr_bad_stateid;
2058 else if (ONE_STATEID(stateid) && (flags & RD_STATE))
2059 return nfs_ok;
2060 else if (nfs4_in_grace()) {
2061 /* Answer in remaining cases depends on existance of
2062 * conflicting state; so we must wait out the grace period. */
2063 return nfserr_grace;
2064 } else if (flags & WR_STATE)
2065 return nfs4_share_conflict(current_fh,
2066 NFS4_SHARE_DENY_WRITE);
2067 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2068 return nfs4_share_conflict(current_fh,
2069 NFS4_SHARE_DENY_READ);
2073 * Allow READ/WRITE during grace period on recovered state only for files
2074 * that are not able to provide mandatory locking.
2076 static inline int
2077 io_during_grace_disallowed(struct inode *inode, int flags)
2079 return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE))
2080 && MANDATORY_LOCK(inode);
2084 * Checks for stateid operations
2087 nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2089 struct nfs4_stateid *stp = NULL;
2090 struct nfs4_delegation *dp = NULL;
2091 stateid_t *stidp;
2092 struct inode *ino = current_fh->fh_dentry->d_inode;
2093 int status;
2095 dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2096 stateid->si_boot, stateid->si_stateownerid,
2097 stateid->si_fileid, stateid->si_generation);
2098 if (filpp)
2099 *filpp = NULL;
2101 if (io_during_grace_disallowed(ino, flags))
2102 return nfserr_grace;
2104 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2105 return check_special_stateids(current_fh, stateid, flags);
2107 /* STALE STATEID */
2108 status = nfserr_stale_stateid;
2109 if (STALE_STATEID(stateid))
2110 goto out;
2112 /* BAD STATEID */
2113 status = nfserr_bad_stateid;
2114 if (!stateid->si_fileid) { /* delegation stateid */
2115 if(!(dp = find_delegation_stateid(ino, stateid))) {
2116 dprintk("NFSD: delegation stateid not found\n");
2117 if (nfs4_in_grace())
2118 status = nfserr_grace;
2119 goto out;
2121 stidp = &dp->dl_stateid;
2122 } else { /* open or lock stateid */
2123 if (!(stp = find_stateid(stateid, flags))) {
2124 dprintk("NFSD: open or lock stateid not found\n");
2125 if (nfs4_in_grace())
2126 status = nfserr_grace;
2127 goto out;
2129 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
2130 goto out;
2131 if (!stp->st_stateowner->so_confirmed)
2132 goto out;
2133 stidp = &stp->st_stateid;
2135 if (stateid->si_generation > stidp->si_generation)
2136 goto out;
2138 /* OLD STATEID */
2139 status = nfserr_old_stateid;
2140 if (stateid->si_generation < stidp->si_generation)
2141 goto out;
2142 if (stp) {
2143 if ((status = nfs4_check_openmode(stp,flags)))
2144 goto out;
2145 renew_client(stp->st_stateowner->so_client);
2146 if (filpp)
2147 *filpp = stp->st_vfs_file;
2148 } else if (dp) {
2149 if ((status = nfs4_check_delegmode(dp, flags)))
2150 goto out;
2151 renew_client(dp->dl_client);
2152 if (flags & DELEG_RET)
2153 unhash_delegation(dp);
2154 if (filpp)
2155 *filpp = dp->dl_vfs_file;
2157 status = nfs_ok;
2158 out:
2159 return status;
2164 * Checks for sequence id mutating operations.
2166 static int
2167 nfs4_preprocess_seqid_op(struct svc_fh *current_fh, u32 seqid, stateid_t *stateid, int flags, struct nfs4_stateowner **sopp, struct nfs4_stateid **stpp, clientid_t *lockclid)
2169 int status;
2170 struct nfs4_stateid *stp;
2171 struct nfs4_stateowner *sop;
2173 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2174 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2175 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2176 stateid->si_generation);
2178 *stpp = NULL;
2179 *sopp = NULL;
2181 status = nfserr_bad_stateid;
2182 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2183 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
2184 goto out;
2187 status = nfserr_stale_stateid;
2188 if (STALE_STATEID(stateid))
2189 goto out;
2191 * We return BAD_STATEID if filehandle doesn't match stateid,
2192 * the confirmed flag is incorrecly set, or the generation
2193 * number is incorrect.
2194 * If there is no entry in the openfile table for this id,
2195 * we can't always return BAD_STATEID;
2196 * this might be a retransmitted CLOSE which has arrived after
2197 * the openfile has been released.
2199 if (!(stp = find_stateid(stateid, flags)))
2200 goto no_nfs4_stateid;
2202 status = nfserr_bad_stateid;
2204 /* for new lock stateowners:
2205 * check that the lock->v.new.open_stateid
2206 * refers to an open stateowner
2208 * check that the lockclid (nfs4_lock->v.new.clientid) is the same
2209 * as the open_stateid->st_stateowner->so_client->clientid
2211 if (lockclid) {
2212 struct nfs4_stateowner *sop = stp->st_stateowner;
2213 struct nfs4_client *clp = sop->so_client;
2215 if (!sop->so_is_open_owner)
2216 goto out;
2217 if (!cmp_clid(&clp->cl_clientid, lockclid))
2218 goto out;
2221 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
2222 printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
2223 goto out;
2226 *stpp = stp;
2227 *sopp = sop = stp->st_stateowner;
2230 * We now validate the seqid and stateid generation numbers.
2231 * For the moment, we ignore the possibility of
2232 * generation number wraparound.
2234 if (seqid != sop->so_seqid + 1)
2235 goto check_replay;
2237 if (sop->so_confirmed) {
2238 if (flags & CONFIRM) {
2239 printk("NFSD: preprocess_seqid_op: expected unconfirmed stateowner!\n");
2240 goto out;
2243 else {
2244 if (!(flags & CONFIRM)) {
2245 printk("NFSD: preprocess_seqid_op: stateowner not confirmed yet!\n");
2246 goto out;
2249 if (stateid->si_generation > stp->st_stateid.si_generation) {
2250 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
2251 goto out;
2254 status = nfserr_old_stateid;
2255 if (stateid->si_generation < stp->st_stateid.si_generation) {
2256 printk("NFSD: preprocess_seqid_op: old stateid!\n");
2257 goto out;
2259 /* XXX renew the client lease here */
2260 status = nfs_ok;
2262 out:
2263 return status;
2265 no_nfs4_stateid:
2268 * We determine whether this is a bad stateid or a replay,
2269 * starting by trying to look up the stateowner.
2270 * If stateowner is not found - stateid is bad.
2272 if (!(sop = find_openstateowner_id(stateid->si_stateownerid, flags))) {
2273 printk("NFSD: preprocess_seqid_op: no stateowner or nfs4_stateid!\n");
2274 status = nfserr_bad_stateid;
2275 goto out;
2277 *sopp = sop;
2279 check_replay:
2280 if (seqid == sop->so_seqid) {
2281 printk("NFSD: preprocess_seqid_op: retransmission?\n");
2282 /* indicate replay to calling function */
2283 status = NFSERR_REPLAY_ME;
2284 } else {
2285 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d\n", sop->so_seqid +1, seqid);
2287 *sopp = NULL;
2288 status = nfserr_bad_seqid;
2290 goto out;
2294 nfsd4_open_confirm(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_confirm *oc)
2296 int status;
2297 struct nfs4_stateowner *sop;
2298 struct nfs4_stateid *stp;
2300 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
2301 (int)current_fh->fh_dentry->d_name.len,
2302 current_fh->fh_dentry->d_name.name);
2304 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2305 goto out;
2307 nfs4_lock_state();
2309 if ((status = nfs4_preprocess_seqid_op(current_fh, oc->oc_seqid,
2310 &oc->oc_req_stateid,
2311 CHECK_FH | CONFIRM | OPEN_STATE,
2312 &oc->oc_stateowner, &stp, NULL)))
2313 goto out;
2315 sop = oc->oc_stateowner;
2316 sop->so_confirmed = 1;
2317 update_stateid(&stp->st_stateid);
2318 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2319 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2320 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2321 stp->st_stateid.si_boot,
2322 stp->st_stateid.si_stateownerid,
2323 stp->st_stateid.si_fileid,
2324 stp->st_stateid.si_generation);
2325 out:
2326 if (oc->oc_stateowner)
2327 nfs4_get_stateowner(oc->oc_stateowner);
2328 nfs4_unlock_state();
2329 return status;
2334 * unset all bits in union bitmap (bmap) that
2335 * do not exist in share (from successful OPEN_DOWNGRADE)
2337 static void
2338 reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2340 int i;
2341 for (i = 1; i < 4; i++) {
2342 if ((i & access) != i)
2343 __clear_bit(i, bmap);
2347 static void
2348 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2350 int i;
2351 for (i = 0; i < 4; i++) {
2352 if ((i & deny) != i)
2353 __clear_bit(i, bmap);
2358 nfsd4_open_downgrade(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_downgrade *od)
2360 int status;
2361 struct nfs4_stateid *stp;
2362 unsigned int share_access;
2364 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
2365 (int)current_fh->fh_dentry->d_name.len,
2366 current_fh->fh_dentry->d_name.name);
2368 if (!TEST_ACCESS(od->od_share_access) || !TEST_DENY(od->od_share_deny))
2369 return nfserr_inval;
2371 nfs4_lock_state();
2372 if ((status = nfs4_preprocess_seqid_op(current_fh, od->od_seqid,
2373 &od->od_stateid,
2374 CHECK_FH | OPEN_STATE,
2375 &od->od_stateowner, &stp, NULL)))
2376 goto out;
2378 status = nfserr_inval;
2379 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2380 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2381 stp->st_access_bmap, od->od_share_access);
2382 goto out;
2384 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2385 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2386 stp->st_deny_bmap, od->od_share_deny);
2387 goto out;
2389 set_access(&share_access, stp->st_access_bmap);
2390 nfs4_file_downgrade(stp->st_vfs_file,
2391 share_access & ~od->od_share_access);
2393 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2394 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2396 update_stateid(&stp->st_stateid);
2397 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2398 status = nfs_ok;
2399 out:
2400 if (od->od_stateowner)
2401 nfs4_get_stateowner(od->od_stateowner);
2402 nfs4_unlock_state();
2403 return status;
2407 * nfs4_unlock_state() called after encode
2410 nfsd4_close(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_close *close)
2412 int status;
2413 struct nfs4_stateid *stp;
2415 dprintk("NFSD: nfsd4_close on file %.*s\n",
2416 (int)current_fh->fh_dentry->d_name.len,
2417 current_fh->fh_dentry->d_name.name);
2419 nfs4_lock_state();
2420 /* check close_lru for replay */
2421 if ((status = nfs4_preprocess_seqid_op(current_fh, close->cl_seqid,
2422 &close->cl_stateid,
2423 CHECK_FH | OPEN_STATE | CLOSE_STATE,
2424 &close->cl_stateowner, &stp, NULL)))
2425 goto out;
2427 * Return success, but first update the stateid.
2429 status = nfs_ok;
2430 update_stateid(&stp->st_stateid);
2431 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2433 /* release_state_owner() calls nfsd_close() if needed */
2434 release_state_owner(stp, OPEN_STATE);
2435 out:
2436 if (close->cl_stateowner)
2437 nfs4_get_stateowner(close->cl_stateowner);
2438 nfs4_unlock_state();
2439 return status;
2443 nfsd4_delegreturn(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_delegreturn *dr)
2445 int status;
2447 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2448 goto out;
2450 nfs4_lock_state();
2451 status = nfs4_preprocess_stateid_op(current_fh, &dr->dr_stateid, DELEG_RET, NULL);
2452 nfs4_unlock_state();
2453 out:
2454 return status;
2459 * Lock owner state (byte-range locks)
2461 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2462 #define LOCK_HASH_BITS 8
2463 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2464 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2466 #define lockownerid_hashval(id) \
2467 ((id) & LOCK_HASH_MASK)
2469 static inline unsigned int
2470 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2471 struct xdr_netobj *ownername)
2473 return (file_hashval(inode) + cl_id
2474 + opaque_hashval(ownername->data, ownername->len))
2475 & LOCK_HASH_MASK;
2478 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2479 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2480 static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2482 static struct nfs4_stateid *
2483 find_stateid(stateid_t *stid, int flags)
2485 struct nfs4_stateid *local = NULL;
2486 u32 st_id = stid->si_stateownerid;
2487 u32 f_id = stid->si_fileid;
2488 unsigned int hashval;
2490 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
2491 if ((flags & LOCK_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2492 hashval = stateid_hashval(st_id, f_id);
2493 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2494 if ((local->st_stateid.si_stateownerid == st_id) &&
2495 (local->st_stateid.si_fileid == f_id))
2496 return local;
2499 if ((flags & OPEN_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2500 hashval = stateid_hashval(st_id, f_id);
2501 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2502 if ((local->st_stateid.si_stateownerid == st_id) &&
2503 (local->st_stateid.si_fileid == f_id))
2504 return local;
2506 } else
2507 printk("NFSD: find_stateid: ERROR: no state flag\n");
2508 return NULL;
2511 static struct nfs4_delegation *
2512 find_delegation_stateid(struct inode *ino, stateid_t *stid)
2514 struct nfs4_file *fp;
2515 struct nfs4_delegation *dl;
2517 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2518 stid->si_boot, stid->si_stateownerid,
2519 stid->si_fileid, stid->si_generation);
2521 fp = find_file(ino);
2522 if (!fp)
2523 return NULL;
2524 dl = find_delegation_file(fp, stid);
2525 put_nfs4_file(fp);
2526 return dl;
2530 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2531 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2532 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2533 * locking, this prevents us from being completely protocol-compliant. The
2534 * real solution to this problem is to start using unsigned file offsets in
2535 * the VFS, but this is a very deep change!
2537 static inline void
2538 nfs4_transform_lock_offset(struct file_lock *lock)
2540 if (lock->fl_start < 0)
2541 lock->fl_start = OFFSET_MAX;
2542 if (lock->fl_end < 0)
2543 lock->fl_end = OFFSET_MAX;
2546 static int
2547 nfs4_verify_lock_stateowner(struct nfs4_stateowner *sop, unsigned int hashval)
2549 struct nfs4_stateowner *local = NULL;
2550 int status = 0;
2552 if (hashval >= LOCK_HASH_SIZE)
2553 goto out;
2554 list_for_each_entry(local, &lock_ownerid_hashtbl[hashval], so_idhash) {
2555 if (local == sop) {
2556 status = 1;
2557 goto out;
2560 out:
2561 return status;
2565 static inline void
2566 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2568 struct nfs4_stateowner *sop = (struct nfs4_stateowner *) fl->fl_owner;
2569 unsigned int hval = lockownerid_hashval(sop->so_id);
2571 deny->ld_sop = NULL;
2572 if (nfs4_verify_lock_stateowner(sop, hval)) {
2573 kref_get(&sop->so_ref);
2574 deny->ld_sop = sop;
2575 deny->ld_clientid = sop->so_client->cl_clientid;
2577 deny->ld_start = fl->fl_start;
2578 deny->ld_length = ~(u64)0;
2579 if (fl->fl_end != ~(u64)0)
2580 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2581 deny->ld_type = NFS4_READ_LT;
2582 if (fl->fl_type != F_RDLCK)
2583 deny->ld_type = NFS4_WRITE_LT;
2586 static struct nfs4_stateowner *
2587 find_lockstateowner(struct xdr_netobj *owner, clientid_t *clid)
2589 struct nfs4_stateowner *local = NULL;
2590 int i;
2592 for (i = 0; i < LOCK_HASH_SIZE; i++) {
2593 list_for_each_entry(local, &lock_ownerid_hashtbl[i], so_idhash) {
2594 if (!cmp_owner_str(local, owner, clid))
2595 continue;
2596 return local;
2599 return NULL;
2602 static struct nfs4_stateowner *
2603 find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2604 struct xdr_netobj *owner)
2606 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2607 struct nfs4_stateowner *op;
2609 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
2610 if (cmp_owner_str(op, owner, clid))
2611 return op;
2613 return NULL;
2617 * Alloc a lock owner structure.
2618 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2619 * occured.
2621 * strhashval = lock_ownerstr_hashval
2622 * so_seqid = lock->lk_new_lock_seqid - 1: it gets bumped in encode
2625 static struct nfs4_stateowner *
2626 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2627 struct nfs4_stateowner *sop;
2628 struct nfs4_replay *rp;
2629 unsigned int idhashval;
2631 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2632 return NULL;
2633 idhashval = lockownerid_hashval(current_ownerid);
2634 INIT_LIST_HEAD(&sop->so_idhash);
2635 INIT_LIST_HEAD(&sop->so_strhash);
2636 INIT_LIST_HEAD(&sop->so_perclient);
2637 INIT_LIST_HEAD(&sop->so_stateids);
2638 INIT_LIST_HEAD(&sop->so_perstateid);
2639 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2640 sop->so_time = 0;
2641 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2642 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
2643 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
2644 sop->so_is_open_owner = 0;
2645 sop->so_id = current_ownerid++;
2646 sop->so_client = clp;
2647 sop->so_seqid = lock->lk_new_lock_seqid - 1;
2648 sop->so_confirmed = 1;
2649 rp = &sop->so_replay;
2650 rp->rp_status = NFSERR_SERVERFAULT;
2651 rp->rp_buflen = 0;
2652 rp->rp_buf = rp->rp_ibuf;
2653 return sop;
2656 static struct nfs4_stateid *
2657 alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2659 struct nfs4_stateid *stp;
2660 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2662 stp = nfs4_alloc_stateid();
2663 if (stp == NULL)
2664 goto out;
2665 INIT_LIST_HEAD(&stp->st_hash);
2666 INIT_LIST_HEAD(&stp->st_perfile);
2667 INIT_LIST_HEAD(&stp->st_perstateowner);
2668 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
2669 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
2670 list_add(&stp->st_perfile, &fp->fi_stateids);
2671 list_add(&stp->st_perstateowner, &sop->so_stateids);
2672 stp->st_stateowner = sop;
2673 get_nfs4_file(fp);
2674 stp->st_file = fp;
2675 stp->st_stateid.si_boot = boot_time;
2676 stp->st_stateid.si_stateownerid = sop->so_id;
2677 stp->st_stateid.si_fileid = fp->fi_id;
2678 stp->st_stateid.si_generation = 0;
2679 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2680 stp->st_access_bmap = open_stp->st_access_bmap;
2681 stp->st_deny_bmap = open_stp->st_deny_bmap;
2683 out:
2684 return stp;
2687 static int
2688 check_lock_length(u64 offset, u64 length)
2690 return ((length == 0) || ((length != ~(u64)0) &&
2691 LOFF_OVERFLOW(offset, length)));
2695 * LOCK operation
2698 nfsd4_lock(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lock *lock)
2700 struct nfs4_stateowner *lock_sop = NULL, *open_sop = NULL;
2701 struct nfs4_stateid *lock_stp;
2702 struct file *filp;
2703 struct file_lock file_lock;
2704 struct file_lock *conflock;
2705 int status = 0;
2706 unsigned int strhashval;
2708 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2709 (long long) lock->lk_offset,
2710 (long long) lock->lk_length);
2712 if (nfs4_in_grace() && !lock->lk_reclaim)
2713 return nfserr_grace;
2714 if (!nfs4_in_grace() && lock->lk_reclaim)
2715 return nfserr_no_grace;
2717 if (check_lock_length(lock->lk_offset, lock->lk_length))
2718 return nfserr_inval;
2720 nfs4_lock_state();
2722 if (lock->lk_is_new) {
2724 * Client indicates that this is a new lockowner.
2725 * Use open owner and open stateid to create lock owner and lock
2726 * stateid.
2728 struct nfs4_stateid *open_stp = NULL;
2729 struct nfs4_file *fp;
2731 status = nfserr_stale_clientid;
2732 if (STALE_CLIENTID(&lock->lk_new_clientid)) {
2733 printk("NFSD: nfsd4_lock: clientid is stale!\n");
2734 goto out;
2737 /* is the new lock seqid presented by the client zero? */
2738 status = nfserr_bad_seqid;
2739 if (lock->v.new.lock_seqid != 0)
2740 goto out;
2742 /* validate and update open stateid and open seqid */
2743 status = nfs4_preprocess_seqid_op(current_fh,
2744 lock->lk_new_open_seqid,
2745 &lock->lk_new_open_stateid,
2746 CHECK_FH | OPEN_STATE,
2747 &open_sop, &open_stp,
2748 &lock->v.new.clientid);
2749 if (status) {
2750 if (lock->lk_reclaim)
2751 status = nfserr_reclaim_bad;
2752 goto out;
2754 /* create lockowner and lock stateid */
2755 fp = open_stp->st_file;
2756 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2757 open_sop->so_client->cl_clientid.cl_id,
2758 &lock->v.new.owner);
2760 * If we already have this lock owner, the client is in
2761 * error (or our bookeeping is wrong!)
2762 * for asking for a 'new lock'.
2764 status = nfserr_bad_stateid;
2765 lock_sop = find_lockstateowner(&lock->v.new.owner,
2766 &lock->v.new.clientid);
2767 if (lock_sop)
2768 goto out;
2769 status = nfserr_resource;
2770 if (!(lock->lk_stateowner = alloc_init_lock_stateowner(strhashval, open_sop->so_client, open_stp, lock)))
2771 goto out;
2772 if ((lock_stp = alloc_init_lock_stateid(lock->lk_stateowner,
2773 fp, open_stp)) == NULL) {
2774 release_stateowner(lock->lk_stateowner);
2775 lock->lk_stateowner = NULL;
2776 goto out;
2778 /* bump the open seqid used to create the lock */
2779 open_sop->so_seqid++;
2780 } else {
2781 /* lock (lock owner + lock stateid) already exists */
2782 status = nfs4_preprocess_seqid_op(current_fh,
2783 lock->lk_old_lock_seqid,
2784 &lock->lk_old_lock_stateid,
2785 CHECK_FH | LOCK_STATE,
2786 &lock->lk_stateowner, &lock_stp, NULL);
2787 if (status)
2788 goto out;
2790 /* lock->lk_stateowner and lock_stp have been created or found */
2791 filp = lock_stp->st_vfs_file;
2793 if ((status = fh_verify(rqstp, current_fh, S_IFREG, MAY_LOCK))) {
2794 printk("NFSD: nfsd4_lock: permission denied!\n");
2795 goto out;
2798 locks_init_lock(&file_lock);
2799 switch (lock->lk_type) {
2800 case NFS4_READ_LT:
2801 case NFS4_READW_LT:
2802 file_lock.fl_type = F_RDLCK;
2803 break;
2804 case NFS4_WRITE_LT:
2805 case NFS4_WRITEW_LT:
2806 file_lock.fl_type = F_WRLCK;
2807 break;
2808 default:
2809 status = nfserr_inval;
2810 goto out;
2812 file_lock.fl_owner = (fl_owner_t) lock->lk_stateowner;
2813 file_lock.fl_pid = current->tgid;
2814 file_lock.fl_file = filp;
2815 file_lock.fl_flags = FL_POSIX;
2817 file_lock.fl_start = lock->lk_offset;
2818 if ((lock->lk_length == ~(u64)0) ||
2819 LOFF_OVERFLOW(lock->lk_offset, lock->lk_length))
2820 file_lock.fl_end = ~(u64)0;
2821 else
2822 file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
2823 nfs4_transform_lock_offset(&file_lock);
2826 * Try to lock the file in the VFS.
2827 * Note: locks.c uses the BKL to protect the inode's lock list.
2830 status = posix_lock_file(filp, &file_lock);
2831 if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
2832 file_lock.fl_ops->fl_release_private(&file_lock);
2833 dprintk("NFSD: nfsd4_lock: posix_lock_file status %d\n",status);
2834 switch (-status) {
2835 case 0: /* success! */
2836 update_stateid(&lock_stp->st_stateid);
2837 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2838 sizeof(stateid_t));
2839 goto out;
2840 case (EAGAIN):
2841 goto conflicting_lock;
2842 case (EDEADLK):
2843 status = nfserr_deadlock;
2844 default:
2845 dprintk("NFSD: nfsd4_lock: posix_lock_file() failed! status %d\n",status);
2846 goto out_destroy_new_stateid;
2849 conflicting_lock:
2850 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2851 status = nfserr_denied;
2852 /* XXX There is a race here. Future patch needed to provide
2853 * an atomic posix_lock_and_test_file
2855 if (!(conflock = posix_test_lock(filp, &file_lock))) {
2856 status = nfserr_serverfault;
2857 goto out;
2859 nfs4_set_lock_denied(conflock, &lock->lk_denied);
2861 out_destroy_new_stateid:
2862 if (lock->lk_is_new) {
2863 dprintk("NFSD: nfsd4_lock: destroy new stateid!\n");
2865 * An error encountered after instantiation of the new
2866 * stateid has forced us to destroy it.
2868 if (!seqid_mutating_err(status))
2869 open_sop->so_seqid--;
2871 release_state_owner(lock_stp, LOCK_STATE);
2873 out:
2874 if (lock->lk_stateowner)
2875 nfs4_get_stateowner(lock->lk_stateowner);
2876 nfs4_unlock_state();
2877 return status;
2881 * LOCKT operation
2884 nfsd4_lockt(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lockt *lockt)
2886 struct inode *inode;
2887 struct file file;
2888 struct file_lock file_lock;
2889 struct file_lock *conflicting_lock;
2890 int status;
2892 if (nfs4_in_grace())
2893 return nfserr_grace;
2895 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2896 return nfserr_inval;
2898 lockt->lt_stateowner = NULL;
2899 nfs4_lock_state();
2901 status = nfserr_stale_clientid;
2902 if (STALE_CLIENTID(&lockt->lt_clientid)) {
2903 printk("NFSD: nfsd4_lockt: clientid is stale!\n");
2904 goto out;
2907 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0))) {
2908 printk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
2909 if (status == nfserr_symlink)
2910 status = nfserr_inval;
2911 goto out;
2914 inode = current_fh->fh_dentry->d_inode;
2915 locks_init_lock(&file_lock);
2916 switch (lockt->lt_type) {
2917 case NFS4_READ_LT:
2918 case NFS4_READW_LT:
2919 file_lock.fl_type = F_RDLCK;
2920 break;
2921 case NFS4_WRITE_LT:
2922 case NFS4_WRITEW_LT:
2923 file_lock.fl_type = F_WRLCK;
2924 break;
2925 default:
2926 printk("NFSD: nfs4_lockt: bad lock type!\n");
2927 status = nfserr_inval;
2928 goto out;
2931 lockt->lt_stateowner = find_lockstateowner_str(inode,
2932 &lockt->lt_clientid, &lockt->lt_owner);
2933 if (lockt->lt_stateowner)
2934 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2935 file_lock.fl_pid = current->tgid;
2936 file_lock.fl_flags = FL_POSIX;
2938 file_lock.fl_start = lockt->lt_offset;
2939 if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length))
2940 file_lock.fl_end = ~(u64)0;
2941 else
2942 file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
2944 nfs4_transform_lock_offset(&file_lock);
2946 /* posix_test_lock uses the struct file _only_ to resolve the inode.
2947 * since LOCKT doesn't require an OPEN, and therefore a struct
2948 * file may not exist, pass posix_test_lock a struct file with
2949 * only the dentry:inode set.
2951 memset(&file, 0, sizeof (struct file));
2952 file.f_dentry = current_fh->fh_dentry;
2954 status = nfs_ok;
2955 conflicting_lock = posix_test_lock(&file, &file_lock);
2956 if (conflicting_lock) {
2957 status = nfserr_denied;
2958 nfs4_set_lock_denied(conflicting_lock, &lockt->lt_denied);
2960 out:
2961 nfs4_unlock_state();
2962 return status;
2966 nfsd4_locku(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_locku *locku)
2968 struct nfs4_stateid *stp;
2969 struct file *filp = NULL;
2970 struct file_lock file_lock;
2971 int status;
2973 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2974 (long long) locku->lu_offset,
2975 (long long) locku->lu_length);
2977 if (check_lock_length(locku->lu_offset, locku->lu_length))
2978 return nfserr_inval;
2980 nfs4_lock_state();
2982 if ((status = nfs4_preprocess_seqid_op(current_fh,
2983 locku->lu_seqid,
2984 &locku->lu_stateid,
2985 CHECK_FH | LOCK_STATE,
2986 &locku->lu_stateowner, &stp, NULL)))
2987 goto out;
2989 filp = stp->st_vfs_file;
2990 BUG_ON(!filp);
2991 locks_init_lock(&file_lock);
2992 file_lock.fl_type = F_UNLCK;
2993 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
2994 file_lock.fl_pid = current->tgid;
2995 file_lock.fl_file = filp;
2996 file_lock.fl_flags = FL_POSIX;
2997 file_lock.fl_start = locku->lu_offset;
2999 if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
3000 file_lock.fl_end = ~(u64)0;
3001 else
3002 file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
3003 nfs4_transform_lock_offset(&file_lock);
3006 * Try to unlock the file in the VFS.
3008 status = posix_lock_file(filp, &file_lock);
3009 if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
3010 file_lock.fl_ops->fl_release_private(&file_lock);
3011 if (status) {
3012 printk("NFSD: nfs4_locku: posix_lock_file failed!\n");
3013 goto out_nfserr;
3016 * OK, unlock succeeded; the only thing left to do is update the stateid.
3018 update_stateid(&stp->st_stateid);
3019 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
3021 out:
3022 if (locku->lu_stateowner)
3023 nfs4_get_stateowner(locku->lu_stateowner);
3024 nfs4_unlock_state();
3025 return status;
3027 out_nfserr:
3028 status = nfserrno(status);
3029 goto out;
3033 * returns
3034 * 1: locks held by lockowner
3035 * 0: no locks held by lockowner
3037 static int
3038 check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
3040 struct file_lock **flpp;
3041 struct inode *inode = filp->f_dentry->d_inode;
3042 int status = 0;
3044 lock_kernel();
3045 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
3046 if ((*flpp)->fl_owner == (fl_owner_t)lowner)
3047 status = 1;
3048 goto out;
3050 out:
3051 unlock_kernel();
3052 return status;
3056 nfsd4_release_lockowner(struct svc_rqst *rqstp, struct nfsd4_release_lockowner *rlockowner)
3058 clientid_t *clid = &rlockowner->rl_clientid;
3059 struct nfs4_stateowner *local = NULL;
3060 struct xdr_netobj *owner = &rlockowner->rl_owner;
3061 int status;
3063 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
3064 clid->cl_boot, clid->cl_id);
3066 /* XXX check for lease expiration */
3068 status = nfserr_stale_clientid;
3069 if (STALE_CLIENTID(clid)) {
3070 printk("NFSD: nfsd4_release_lockowner: clientid is stale!\n");
3071 return status;
3074 nfs4_lock_state();
3076 status = nfs_ok;
3077 local = find_lockstateowner(owner, clid);
3078 if (local) {
3079 struct nfs4_stateid *stp;
3081 /* check for any locks held by any stateid
3082 * associated with the (lock) stateowner */
3083 status = nfserr_locks_held;
3084 list_for_each_entry(stp, &local->so_stateids,
3085 st_perstateowner) {
3086 if (check_for_locks(stp->st_vfs_file, local))
3087 goto out;
3089 /* no locks held by (lock) stateowner */
3090 status = nfs_ok;
3091 release_stateowner(local);
3093 out:
3094 nfs4_unlock_state();
3095 return status;
3098 static inline struct nfs4_client_reclaim *
3099 alloc_reclaim(void)
3101 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
3105 * failure => all reset bets are off, nfserr_no_grace...
3107 static int
3108 nfs4_client_to_reclaim(char *name)
3110 unsigned int strhashval;
3111 struct nfs4_client_reclaim *crp = NULL;
3113 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
3114 crp = alloc_reclaim();
3115 if (!crp)
3116 return 0;
3117 strhashval = clientstr_hashval(name);
3118 INIT_LIST_HEAD(&crp->cr_strhash);
3119 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
3120 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
3121 reclaim_str_hashtbl_size++;
3122 return 1;
3125 static void
3126 nfs4_release_reclaim(void)
3128 struct nfs4_client_reclaim *crp = NULL;
3129 int i;
3131 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3132 while (!list_empty(&reclaim_str_hashtbl[i])) {
3133 crp = list_entry(reclaim_str_hashtbl[i].next,
3134 struct nfs4_client_reclaim, cr_strhash);
3135 list_del(&crp->cr_strhash);
3136 kfree(crp);
3137 reclaim_str_hashtbl_size--;
3140 BUG_ON(reclaim_str_hashtbl_size);
3144 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
3145 static struct nfs4_client_reclaim *
3146 nfs4_find_reclaim_client(clientid_t *clid)
3148 unsigned int strhashval;
3149 struct nfs4_client *clp;
3150 struct nfs4_client_reclaim *crp = NULL;
3153 /* find clientid in conf_id_hashtbl */
3154 clp = find_confirmed_client(clid);
3155 if (clp == NULL)
3156 return NULL;
3158 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3159 clp->cl_name.len, clp->cl_name.data,
3160 clp->cl_recdir);
3162 /* find clp->cl_name in reclaim_str_hashtbl */
3163 strhashval = clientstr_hashval(clp->cl_recdir);
3164 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
3165 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
3166 return crp;
3169 return NULL;
3173 * Called from OPEN. Look for clientid in reclaim list.
3176 nfs4_check_open_reclaim(clientid_t *clid)
3178 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
3181 /* initialization to perform at module load time: */
3183 void
3184 nfs4_state_init(void)
3186 int i;
3188 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3189 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3190 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3191 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3192 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3194 for (i = 0; i < FILE_HASH_SIZE; i++) {
3195 INIT_LIST_HEAD(&file_hashtbl[i]);
3197 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3198 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3199 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3201 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3202 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3203 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3205 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3206 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3207 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3209 memset(&onestateid, ~0, sizeof(stateid_t));
3210 INIT_LIST_HEAD(&close_lru);
3211 INIT_LIST_HEAD(&client_lru);
3212 INIT_LIST_HEAD(&del_recall_lru);
3213 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3214 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3215 reclaim_str_hashtbl_size = 0;
3218 /* initialization to perform when the nfsd service is started: */
3220 static void
3221 __nfs4_state_start(void)
3223 time_t grace_time;
3225 boot_time = get_seconds();
3226 grace_time = max(user_lease_time, lease_time);
3227 lease_time = user_lease_time;
3228 in_grace = 1;
3229 printk("NFSD: starting %ld-second grace period\n", grace_time);
3230 laundry_wq = create_singlethread_workqueue("nfsd4");
3231 queue_delayed_work(laundry_wq, &laundromat_work, grace_time*HZ);
3235 nfs4_state_start(void)
3237 int status;
3239 if (nfs4_init)
3240 return 0;
3241 status = nfsd4_init_slabs();
3242 if (status)
3243 return status;
3244 __nfs4_state_start();
3245 nfs4_init = 1;
3246 return 0;
3250 nfs4_in_grace(void)
3252 return in_grace;
3255 time_t
3256 nfs4_lease_time(void)
3258 return lease_time;
3261 static void
3262 __nfs4_state_shutdown(void)
3264 int i;
3265 struct nfs4_client *clp = NULL;
3266 struct nfs4_delegation *dp = NULL;
3267 struct nfs4_stateowner *sop = NULL;
3268 struct list_head *pos, *next, reaplist;
3270 list_for_each_safe(pos, next, &close_lru) {
3271 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
3272 list_del(&sop->so_close_lru);
3273 nfs4_put_stateowner(sop);
3276 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3277 while (!list_empty(&conf_id_hashtbl[i])) {
3278 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3279 expire_client(clp);
3281 while (!list_empty(&unconf_str_hashtbl[i])) {
3282 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3283 expire_client(clp);
3286 INIT_LIST_HEAD(&reaplist);
3287 spin_lock(&recall_lock);
3288 list_for_each_safe(pos, next, &del_recall_lru) {
3289 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3290 list_move(&dp->dl_recall_lru, &reaplist);
3292 spin_unlock(&recall_lock);
3293 list_for_each_safe(pos, next, &reaplist) {
3294 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3295 list_del_init(&dp->dl_recall_lru);
3296 unhash_delegation(dp);
3299 cancel_delayed_work(&laundromat_work);
3300 flush_workqueue(laundry_wq);
3301 destroy_workqueue(laundry_wq);
3302 nfs4_init = 0;
3305 void
3306 nfs4_state_shutdown(void)
3308 nfs4_lock_state();
3309 nfs4_release_reclaim();
3310 __nfs4_state_shutdown();
3311 nfsd4_free_slabs();
3312 nfs4_unlock_state();
3316 * Called when leasetime is changed.
3318 * The only way the protocol gives us to handle on-the-fly lease changes is to
3319 * simulate a reboot. Instead of doing that, we just wait till the next time
3320 * we start to register any changes in lease time. If the administrator
3321 * really wants to change the lease time *now*, they can go ahead and bring
3322 * nfsd down and then back up again after changing the lease time.
3324 void
3325 nfs4_reset_lease(time_t leasetime)
3327 lock_kernel();
3328 user_lease_time = leasetime;
3329 unlock_kernel();