[PATCH] nfsd4: reboot recovery fix
[firewire-audio.git] / fs / nfsd / nfs4state.c
blob9f9db40b5666684be26b19c09d04e23820e21dc1
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>
51 #include <linux/namei.h>
53 #define NFSDDBG_FACILITY NFSDDBG_PROC
55 /* Globals */
56 static time_t lease_time = 90; /* default lease time */
57 static time_t user_lease_time = 90;
58 static time_t boot_time;
59 static int in_grace = 1;
60 static u32 current_clientid = 1;
61 static u32 current_ownerid = 1;
62 static u32 current_fileid = 1;
63 static u32 current_delegid = 1;
64 static u32 nfs4_init;
65 static stateid_t zerostateid; /* bits all 0 */
66 static stateid_t onestateid; /* bits all 1 */
68 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
69 #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
71 /* forward declarations */
72 static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
73 static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
74 static void release_stateid_lockowners(struct nfs4_stateid *open_stp);
75 static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
76 static void nfs4_set_recdir(char *recdir);
78 /* Locking:
80 * client_sema:
81 * protects clientid_hashtbl[], clientstr_hashtbl[],
82 * unconfstr_hashtbl[], uncofid_hashtbl[].
84 static DECLARE_MUTEX(client_sema);
86 static kmem_cache_t *stateowner_slab = NULL;
87 static kmem_cache_t *file_slab = NULL;
88 static kmem_cache_t *stateid_slab = NULL;
89 static kmem_cache_t *deleg_slab = NULL;
91 void
92 nfs4_lock_state(void)
94 down(&client_sema);
97 void
98 nfs4_unlock_state(void)
100 up(&client_sema);
103 static inline u32
104 opaque_hashval(const void *ptr, int nbytes)
106 unsigned char *cptr = (unsigned char *) ptr;
108 u32 x = 0;
109 while (nbytes--) {
110 x *= 37;
111 x += *cptr++;
113 return x;
116 /* forward declarations */
117 static void release_stateowner(struct nfs4_stateowner *sop);
118 static void release_stateid(struct nfs4_stateid *stp, int flags);
121 * Delegation state
124 /* recall_lock protects the del_recall_lru */
125 static spinlock_t recall_lock = SPIN_LOCK_UNLOCKED;
126 static struct list_head del_recall_lru;
128 static void
129 free_nfs4_file(struct kref *kref)
131 struct nfs4_file *fp = container_of(kref, struct nfs4_file, fi_ref);
132 list_del(&fp->fi_hash);
133 iput(fp->fi_inode);
134 kmem_cache_free(file_slab, fp);
137 static inline void
138 put_nfs4_file(struct nfs4_file *fi)
140 kref_put(&fi->fi_ref, free_nfs4_file);
143 static inline void
144 get_nfs4_file(struct nfs4_file *fi)
146 kref_get(&fi->fi_ref);
149 static struct nfs4_delegation *
150 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
152 struct nfs4_delegation *dp;
153 struct nfs4_file *fp = stp->st_file;
154 struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback;
156 dprintk("NFSD alloc_init_deleg\n");
157 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
158 if (dp == NULL)
159 return dp;
160 INIT_LIST_HEAD(&dp->dl_perfile);
161 INIT_LIST_HEAD(&dp->dl_perclnt);
162 INIT_LIST_HEAD(&dp->dl_recall_lru);
163 dp->dl_client = clp;
164 get_nfs4_file(fp);
165 dp->dl_file = fp;
166 dp->dl_flock = NULL;
167 get_file(stp->st_vfs_file);
168 dp->dl_vfs_file = stp->st_vfs_file;
169 dp->dl_type = type;
170 dp->dl_recall.cbr_dp = NULL;
171 dp->dl_recall.cbr_ident = cb->cb_ident;
172 dp->dl_recall.cbr_trunc = 0;
173 dp->dl_stateid.si_boot = boot_time;
174 dp->dl_stateid.si_stateownerid = current_delegid++;
175 dp->dl_stateid.si_fileid = 0;
176 dp->dl_stateid.si_generation = 0;
177 dp->dl_fhlen = current_fh->fh_handle.fh_size;
178 memcpy(dp->dl_fhval, &current_fh->fh_handle.fh_base,
179 current_fh->fh_handle.fh_size);
180 dp->dl_time = 0;
181 atomic_set(&dp->dl_count, 1);
182 list_add(&dp->dl_perfile, &fp->fi_delegations);
183 list_add(&dp->dl_perclnt, &clp->cl_delegations);
184 return dp;
187 void
188 nfs4_put_delegation(struct nfs4_delegation *dp)
190 if (atomic_dec_and_test(&dp->dl_count)) {
191 dprintk("NFSD: freeing dp %p\n",dp);
192 put_nfs4_file(dp->dl_file);
193 kmem_cache_free(deleg_slab, dp);
197 /* Remove the associated file_lock first, then remove the delegation.
198 * lease_modify() is called to remove the FS_LEASE file_lock from
199 * the i_flock list, eventually calling nfsd's lock_manager
200 * fl_release_callback.
202 static void
203 nfs4_close_delegation(struct nfs4_delegation *dp)
205 struct file *filp = dp->dl_vfs_file;
207 dprintk("NFSD: close_delegation dp %p\n",dp);
208 dp->dl_vfs_file = NULL;
209 /* The following nfsd_close may not actually close the file,
210 * but we want to remove the lease in any case. */
211 if (dp->dl_flock)
212 setlease(filp, F_UNLCK, &dp->dl_flock);
213 nfsd_close(filp);
216 /* Called under the state lock. */
217 static void
218 unhash_delegation(struct nfs4_delegation *dp)
220 list_del_init(&dp->dl_perfile);
221 list_del_init(&dp->dl_perclnt);
222 spin_lock(&recall_lock);
223 list_del_init(&dp->dl_recall_lru);
224 spin_unlock(&recall_lock);
225 nfs4_close_delegation(dp);
226 nfs4_put_delegation(dp);
230 * SETCLIENTID state
233 /* Hash tables for nfs4_clientid state */
234 #define CLIENT_HASH_BITS 4
235 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
236 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
238 #define clientid_hashval(id) \
239 ((id) & CLIENT_HASH_MASK)
240 #define clientstr_hashval(name) \
241 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
243 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
244 * used in reboot/reset lease grace period processing
246 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
247 * setclientid_confirmed info.
249 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
250 * setclientid info.
252 * client_lru holds client queue ordered by nfs4_client.cl_time
253 * for lease renewal.
255 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
256 * for last close replay.
258 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
259 static int reclaim_str_hashtbl_size = 0;
260 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
261 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
262 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
263 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
264 static struct list_head client_lru;
265 static struct list_head close_lru;
267 static inline void
268 renew_client(struct nfs4_client *clp)
271 * Move client to the end to the LRU list.
273 dprintk("renewing client (clientid %08x/%08x)\n",
274 clp->cl_clientid.cl_boot,
275 clp->cl_clientid.cl_id);
276 list_move_tail(&clp->cl_lru, &client_lru);
277 clp->cl_time = get_seconds();
280 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
281 static int
282 STALE_CLIENTID(clientid_t *clid)
284 if (clid->cl_boot == boot_time)
285 return 0;
286 dprintk("NFSD stale clientid (%08x/%08x)\n",
287 clid->cl_boot, clid->cl_id);
288 return 1;
292 * XXX Should we use a slab cache ?
293 * This type of memory management is somewhat inefficient, but we use it
294 * anyway since SETCLIENTID is not a common operation.
296 static inline struct nfs4_client *
297 alloc_client(struct xdr_netobj name)
299 struct nfs4_client *clp;
301 if ((clp = kmalloc(sizeof(struct nfs4_client), GFP_KERNEL))!= NULL) {
302 memset(clp, 0, sizeof(*clp));
303 if ((clp->cl_name.data = kmalloc(name.len, GFP_KERNEL)) != NULL) {
304 memcpy(clp->cl_name.data, name.data, name.len);
305 clp->cl_name.len = name.len;
307 else {
308 kfree(clp);
309 clp = NULL;
312 return clp;
315 static inline void
316 free_client(struct nfs4_client *clp)
318 if (clp->cl_cred.cr_group_info)
319 put_group_info(clp->cl_cred.cr_group_info);
320 kfree(clp->cl_name.data);
321 kfree(clp);
324 void
325 put_nfs4_client(struct nfs4_client *clp)
327 if (atomic_dec_and_test(&clp->cl_count))
328 free_client(clp);
331 static void
332 expire_client(struct nfs4_client *clp)
334 struct nfs4_stateowner *sop;
335 struct nfs4_delegation *dp;
336 struct nfs4_callback *cb = &clp->cl_callback;
337 struct rpc_clnt *clnt = clp->cl_callback.cb_client;
338 struct list_head reaplist;
340 dprintk("NFSD: expire_client cl_count %d\n",
341 atomic_read(&clp->cl_count));
343 /* shutdown rpc client, ending any outstanding recall rpcs */
344 if (atomic_read(&cb->cb_set) == 1 && clnt) {
345 rpc_shutdown_client(clnt);
346 clnt = clp->cl_callback.cb_client = NULL;
349 INIT_LIST_HEAD(&reaplist);
350 spin_lock(&recall_lock);
351 while (!list_empty(&clp->cl_delegations)) {
352 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
353 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
354 dp->dl_flock);
355 list_del_init(&dp->dl_perclnt);
356 list_move(&dp->dl_recall_lru, &reaplist);
358 spin_unlock(&recall_lock);
359 while (!list_empty(&reaplist)) {
360 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
361 list_del_init(&dp->dl_recall_lru);
362 unhash_delegation(dp);
364 list_del(&clp->cl_idhash);
365 list_del(&clp->cl_strhash);
366 list_del(&clp->cl_lru);
367 while (!list_empty(&clp->cl_openowners)) {
368 sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
369 release_stateowner(sop);
371 put_nfs4_client(clp);
374 static struct nfs4_client *
375 create_client(struct xdr_netobj name, char *recdir) {
376 struct nfs4_client *clp;
378 if (!(clp = alloc_client(name)))
379 goto out;
380 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
381 atomic_set(&clp->cl_count, 1);
382 atomic_set(&clp->cl_callback.cb_set, 0);
383 INIT_LIST_HEAD(&clp->cl_idhash);
384 INIT_LIST_HEAD(&clp->cl_strhash);
385 INIT_LIST_HEAD(&clp->cl_openowners);
386 INIT_LIST_HEAD(&clp->cl_delegations);
387 INIT_LIST_HEAD(&clp->cl_lru);
388 out:
389 return clp;
392 static void
393 copy_verf(struct nfs4_client *target, nfs4_verifier *source) {
394 memcpy(target->cl_verifier.data, source->data, sizeof(target->cl_verifier.data));
397 static void
398 copy_clid(struct nfs4_client *target, struct nfs4_client *source) {
399 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
400 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
403 static void
404 copy_cred(struct svc_cred *target, struct svc_cred *source) {
406 target->cr_uid = source->cr_uid;
407 target->cr_gid = source->cr_gid;
408 target->cr_group_info = source->cr_group_info;
409 get_group_info(target->cr_group_info);
412 static inline int
413 same_name(const char *n1, const char *n2) {
414 return 0 == memcmp(n1, n2, HEXDIR_LEN);
417 static int
418 cmp_verf(nfs4_verifier *v1, nfs4_verifier *v2) {
419 return(!memcmp(v1->data,v2->data,sizeof(v1->data)));
422 static int
423 cmp_clid(clientid_t * cl1, clientid_t * cl2) {
424 return((cl1->cl_boot == cl2->cl_boot) &&
425 (cl1->cl_id == cl2->cl_id));
428 /* XXX what about NGROUP */
429 static int
430 cmp_creds(struct svc_cred *cr1, struct svc_cred *cr2){
431 return(cr1->cr_uid == cr2->cr_uid);
435 static void
436 gen_clid(struct nfs4_client *clp) {
437 clp->cl_clientid.cl_boot = boot_time;
438 clp->cl_clientid.cl_id = current_clientid++;
441 static void
442 gen_confirm(struct nfs4_client *clp) {
443 struct timespec tv;
444 u32 * p;
446 tv = CURRENT_TIME;
447 p = (u32 *)clp->cl_confirm.data;
448 *p++ = tv.tv_sec;
449 *p++ = tv.tv_nsec;
452 static int
453 check_name(struct xdr_netobj name) {
455 if (name.len == 0)
456 return 0;
457 if (name.len > NFS4_OPAQUE_LIMIT) {
458 printk("NFSD: check_name: name too long(%d)!\n", name.len);
459 return 0;
461 return 1;
464 static void
465 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
467 unsigned int idhashval;
469 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
470 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
471 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
472 list_add_tail(&clp->cl_lru, &client_lru);
473 clp->cl_time = get_seconds();
476 static void
477 move_to_confirmed(struct nfs4_client *clp)
479 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
480 unsigned int strhashval;
482 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
483 list_del_init(&clp->cl_strhash);
484 list_del_init(&clp->cl_idhash);
485 list_add(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
486 strhashval = clientstr_hashval(clp->cl_recdir);
487 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
488 renew_client(clp);
491 static struct nfs4_client *
492 find_confirmed_client(clientid_t *clid)
494 struct nfs4_client *clp;
495 unsigned int idhashval = clientid_hashval(clid->cl_id);
497 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
498 if (cmp_clid(&clp->cl_clientid, clid))
499 return clp;
501 return NULL;
504 static struct nfs4_client *
505 find_unconfirmed_client(clientid_t *clid)
507 struct nfs4_client *clp;
508 unsigned int idhashval = clientid_hashval(clid->cl_id);
510 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
511 if (cmp_clid(&clp->cl_clientid, clid))
512 return clp;
514 return NULL;
517 static struct nfs4_client *
518 find_confirmed_client_by_str(const char *dname, unsigned int hashval)
520 struct nfs4_client *clp;
522 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
523 if (same_name(clp->cl_recdir, dname))
524 return clp;
526 return NULL;
529 static struct nfs4_client *
530 find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
532 struct nfs4_client *clp;
534 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
535 if (same_name(clp->cl_recdir, dname))
536 return clp;
538 return NULL;
541 /* a helper function for parse_callback */
542 static int
543 parse_octet(unsigned int *lenp, char **addrp)
545 unsigned int len = *lenp;
546 char *p = *addrp;
547 int n = -1;
548 char c;
550 for (;;) {
551 if (!len)
552 break;
553 len--;
554 c = *p++;
555 if (c == '.')
556 break;
557 if ((c < '0') || (c > '9')) {
558 n = -1;
559 break;
561 if (n < 0)
562 n = 0;
563 n = (n * 10) + (c - '0');
564 if (n > 255) {
565 n = -1;
566 break;
569 *lenp = len;
570 *addrp = p;
571 return n;
574 /* parse and set the setclientid ipv4 callback address */
575 static int
576 parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
578 int temp = 0;
579 u32 cbaddr = 0;
580 u16 cbport = 0;
581 u32 addrlen = addr_len;
582 char *addr = addr_val;
583 int i, shift;
585 /* ipaddress */
586 shift = 24;
587 for(i = 4; i > 0 ; i--) {
588 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
589 return 0;
591 cbaddr |= (temp << shift);
592 if (shift > 0)
593 shift -= 8;
595 *cbaddrp = cbaddr;
597 /* port */
598 shift = 8;
599 for(i = 2; i > 0 ; i--) {
600 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
601 return 0;
603 cbport |= (temp << shift);
604 if (shift > 0)
605 shift -= 8;
607 *cbportp = cbport;
608 return 1;
611 static void
612 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
614 struct nfs4_callback *cb = &clp->cl_callback;
616 /* Currently, we only support tcp for the callback channel */
617 if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
618 goto out_err;
620 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
621 &cb->cb_addr, &cb->cb_port)))
622 goto out_err;
623 cb->cb_prog = se->se_callback_prog;
624 cb->cb_ident = se->se_callback_ident;
625 return;
626 out_err:
627 printk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
628 "will not receive delegations\n",
629 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
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 atomic_set(&conf->cl_callback.cb_set, 0);
876 gen_confirm(conf);
877 nfsd4_remove_clid_dir(unconf);
878 expire_client(unconf);
879 status = nfs_ok;
882 } else if ((conf && !unconf) ||
883 ((conf && unconf) &&
884 (!cmp_verf(&conf->cl_verifier, &unconf->cl_verifier) ||
885 !same_name(conf->cl_recdir, unconf->cl_recdir)))) {
886 /* CASE 2:
887 * conf record that matches input clientid.
888 * if unconf record matches input clientid, then
889 * unconf->cl_name or unconf->cl_verifier don't match the
890 * conf record.
892 if (!cmp_creds(&conf->cl_cred,&rqstp->rq_cred))
893 status = nfserr_clid_inuse;
894 else
895 status = nfs_ok;
896 } else if (!conf && unconf
897 && cmp_verf(&unconf->cl_confirm, &confirm)) {
898 /* CASE 3:
899 * conf record not found.
900 * unconf record found.
901 * unconf->cl_confirm matches input confirm
903 if (!cmp_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
904 status = nfserr_clid_inuse;
905 } else {
906 unsigned int hash =
907 clientstr_hashval(unconf->cl_recdir);
908 conf = find_confirmed_client_by_str(unconf->cl_recdir,
909 hash);
910 if (conf) {
911 nfsd4_remove_clid_dir(conf);
912 expire_client(conf);
914 move_to_confirmed(unconf);
915 conf = unconf;
916 status = nfs_ok;
918 } else if ((!conf || (conf && !cmp_verf(&conf->cl_confirm, &confirm)))
919 && (!unconf || (unconf && !cmp_verf(&unconf->cl_confirm,
920 &confirm)))) {
921 /* CASE 4:
922 * conf record not found, or if conf, conf->cl_confirm does not
923 * match input confirm.
924 * unconf record not found, or if unconf, unconf->cl_confirm
925 * does not match input confirm.
927 status = nfserr_stale_clientid;
928 } else {
929 /* check that we have hit one of the cases...*/
930 status = nfserr_clid_inuse;
932 out:
933 if (!status)
934 nfsd4_probe_callback(conf);
935 nfs4_unlock_state();
936 return status;
940 * Open owner state (share locks)
943 /* hash tables for nfs4_stateowner */
944 #define OWNER_HASH_BITS 8
945 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
946 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
948 #define ownerid_hashval(id) \
949 ((id) & OWNER_HASH_MASK)
950 #define ownerstr_hashval(clientid, ownername) \
951 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
953 static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
954 static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
956 /* hash table for nfs4_file */
957 #define FILE_HASH_BITS 8
958 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
959 #define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
960 /* hash table for (open)nfs4_stateid */
961 #define STATEID_HASH_BITS 10
962 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
963 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
965 #define file_hashval(x) \
966 hash_ptr(x, FILE_HASH_BITS)
967 #define stateid_hashval(owner_id, file_id) \
968 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
970 static struct list_head file_hashtbl[FILE_HASH_SIZE];
971 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
973 /* OPEN Share state helper functions */
974 static inline struct nfs4_file *
975 alloc_init_file(struct inode *ino)
977 struct nfs4_file *fp;
978 unsigned int hashval = file_hashval(ino);
980 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
981 if (fp) {
982 kref_init(&fp->fi_ref);
983 INIT_LIST_HEAD(&fp->fi_hash);
984 INIT_LIST_HEAD(&fp->fi_stateids);
985 INIT_LIST_HEAD(&fp->fi_delegations);
986 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
987 fp->fi_inode = igrab(ino);
988 fp->fi_id = current_fileid++;
989 return fp;
991 return NULL;
994 static void
995 nfsd4_free_slab(kmem_cache_t **slab)
997 int status;
999 if (*slab == NULL)
1000 return;
1001 status = kmem_cache_destroy(*slab);
1002 *slab = NULL;
1003 WARN_ON(status);
1006 static void
1007 nfsd4_free_slabs(void)
1009 nfsd4_free_slab(&stateowner_slab);
1010 nfsd4_free_slab(&file_slab);
1011 nfsd4_free_slab(&stateid_slab);
1012 nfsd4_free_slab(&deleg_slab);
1015 static int
1016 nfsd4_init_slabs(void)
1018 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
1019 sizeof(struct nfs4_stateowner), 0, 0, NULL, NULL);
1020 if (stateowner_slab == NULL)
1021 goto out_nomem;
1022 file_slab = kmem_cache_create("nfsd4_files",
1023 sizeof(struct nfs4_file), 0, 0, NULL, NULL);
1024 if (file_slab == NULL)
1025 goto out_nomem;
1026 stateid_slab = kmem_cache_create("nfsd4_stateids",
1027 sizeof(struct nfs4_stateid), 0, 0, NULL, NULL);
1028 if (stateid_slab == NULL)
1029 goto out_nomem;
1030 deleg_slab = kmem_cache_create("nfsd4_delegations",
1031 sizeof(struct nfs4_delegation), 0, 0, NULL, NULL);
1032 if (deleg_slab == NULL)
1033 goto out_nomem;
1034 return 0;
1035 out_nomem:
1036 nfsd4_free_slabs();
1037 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1038 return -ENOMEM;
1041 void
1042 nfs4_free_stateowner(struct kref *kref)
1044 struct nfs4_stateowner *sop =
1045 container_of(kref, struct nfs4_stateowner, so_ref);
1046 kfree(sop->so_owner.data);
1047 kmem_cache_free(stateowner_slab, sop);
1050 static inline struct nfs4_stateowner *
1051 alloc_stateowner(struct xdr_netobj *owner)
1053 struct nfs4_stateowner *sop;
1055 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1056 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1057 memcpy(sop->so_owner.data, owner->data, owner->len);
1058 sop->so_owner.len = owner->len;
1059 kref_init(&sop->so_ref);
1060 return sop;
1062 kmem_cache_free(stateowner_slab, sop);
1064 return NULL;
1067 static struct nfs4_stateowner *
1068 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1069 struct nfs4_stateowner *sop;
1070 struct nfs4_replay *rp;
1071 unsigned int idhashval;
1073 if (!(sop = alloc_stateowner(&open->op_owner)))
1074 return NULL;
1075 idhashval = ownerid_hashval(current_ownerid);
1076 INIT_LIST_HEAD(&sop->so_idhash);
1077 INIT_LIST_HEAD(&sop->so_strhash);
1078 INIT_LIST_HEAD(&sop->so_perclient);
1079 INIT_LIST_HEAD(&sop->so_stateids);
1080 INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
1081 INIT_LIST_HEAD(&sop->so_close_lru);
1082 sop->so_time = 0;
1083 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1084 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
1085 list_add(&sop->so_perclient, &clp->cl_openowners);
1086 sop->so_is_open_owner = 1;
1087 sop->so_id = current_ownerid++;
1088 sop->so_client = clp;
1089 sop->so_seqid = open->op_seqid;
1090 sop->so_confirmed = 0;
1091 rp = &sop->so_replay;
1092 rp->rp_status = NFSERR_SERVERFAULT;
1093 rp->rp_buflen = 0;
1094 rp->rp_buf = rp->rp_ibuf;
1095 return sop;
1098 static void
1099 release_stateid_lockowners(struct nfs4_stateid *open_stp)
1101 struct nfs4_stateowner *lock_sop;
1103 while (!list_empty(&open_stp->st_lockowners)) {
1104 lock_sop = list_entry(open_stp->st_lockowners.next,
1105 struct nfs4_stateowner, so_perstateid);
1106 /* list_del(&open_stp->st_lockowners); */
1107 BUG_ON(lock_sop->so_is_open_owner);
1108 release_stateowner(lock_sop);
1112 static void
1113 unhash_stateowner(struct nfs4_stateowner *sop)
1115 struct nfs4_stateid *stp;
1117 list_del(&sop->so_idhash);
1118 list_del(&sop->so_strhash);
1119 if (sop->so_is_open_owner)
1120 list_del(&sop->so_perclient);
1121 list_del(&sop->so_perstateid);
1122 while (!list_empty(&sop->so_stateids)) {
1123 stp = list_entry(sop->so_stateids.next,
1124 struct nfs4_stateid, st_perstateowner);
1125 if (sop->so_is_open_owner)
1126 release_stateid(stp, OPEN_STATE);
1127 else
1128 release_stateid(stp, LOCK_STATE);
1132 static void
1133 release_stateowner(struct nfs4_stateowner *sop)
1135 unhash_stateowner(sop);
1136 list_del(&sop->so_close_lru);
1137 nfs4_put_stateowner(sop);
1140 static inline void
1141 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1142 struct nfs4_stateowner *sop = open->op_stateowner;
1143 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1145 INIT_LIST_HEAD(&stp->st_hash);
1146 INIT_LIST_HEAD(&stp->st_perstateowner);
1147 INIT_LIST_HEAD(&stp->st_lockowners);
1148 INIT_LIST_HEAD(&stp->st_perfile);
1149 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
1150 list_add(&stp->st_perstateowner, &sop->so_stateids);
1151 list_add(&stp->st_perfile, &fp->fi_stateids);
1152 stp->st_stateowner = sop;
1153 get_nfs4_file(fp);
1154 stp->st_file = fp;
1155 stp->st_stateid.si_boot = boot_time;
1156 stp->st_stateid.si_stateownerid = sop->so_id;
1157 stp->st_stateid.si_fileid = fp->fi_id;
1158 stp->st_stateid.si_generation = 0;
1159 stp->st_access_bmap = 0;
1160 stp->st_deny_bmap = 0;
1161 __set_bit(open->op_share_access, &stp->st_access_bmap);
1162 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
1165 static void
1166 release_stateid(struct nfs4_stateid *stp, int flags)
1168 struct file *filp = stp->st_vfs_file;
1170 list_del(&stp->st_hash);
1171 list_del(&stp->st_perfile);
1172 list_del(&stp->st_perstateowner);
1173 if (flags & OPEN_STATE) {
1174 release_stateid_lockowners(stp);
1175 stp->st_vfs_file = NULL;
1176 nfsd_close(filp);
1177 } else if (flags & LOCK_STATE)
1178 locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
1179 put_nfs4_file(stp->st_file);
1180 kmem_cache_free(stateid_slab, stp);
1181 stp = NULL;
1184 static void
1185 move_to_close_lru(struct nfs4_stateowner *sop)
1187 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1189 unhash_stateowner(sop);
1190 list_add_tail(&sop->so_close_lru, &close_lru);
1191 sop->so_time = get_seconds();
1194 static void
1195 release_state_owner(struct nfs4_stateid *stp, int flag)
1197 struct nfs4_stateowner *sop = stp->st_stateowner;
1199 dprintk("NFSD: release_state_owner\n");
1200 release_stateid(stp, flag);
1202 /* place unused nfs4_stateowners on so_close_lru list to be
1203 * released by the laundromat service after the lease period
1204 * to enable us to handle CLOSE replay
1206 if (sop->so_confirmed && list_empty(&sop->so_stateids))
1207 move_to_close_lru(sop);
1210 static int
1211 cmp_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, clientid_t *clid) {
1212 return ((sop->so_owner.len == owner->len) &&
1213 !memcmp(sop->so_owner.data, owner->data, owner->len) &&
1214 (sop->so_client->cl_clientid.cl_id == clid->cl_id));
1217 static struct nfs4_stateowner *
1218 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1220 struct nfs4_stateowner *so = NULL;
1222 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
1223 if (cmp_owner_str(so, &open->op_owner, &open->op_clientid))
1224 return so;
1226 return NULL;
1229 /* search file_hashtbl[] for file */
1230 static struct nfs4_file *
1231 find_file(struct inode *ino)
1233 unsigned int hashval = file_hashval(ino);
1234 struct nfs4_file *fp;
1236 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
1237 if (fp->fi_inode == ino) {
1238 get_nfs4_file(fp);
1239 return fp;
1242 return NULL;
1245 #define TEST_ACCESS(x) ((x > 0 || x < 4)?1:0)
1246 #define TEST_DENY(x) ((x >= 0 || x < 5)?1:0)
1248 static void
1249 set_access(unsigned int *access, unsigned long bmap) {
1250 int i;
1252 *access = 0;
1253 for (i = 1; i < 4; i++) {
1254 if (test_bit(i, &bmap))
1255 *access |= i;
1259 static void
1260 set_deny(unsigned int *deny, unsigned long bmap) {
1261 int i;
1263 *deny = 0;
1264 for (i = 0; i < 4; i++) {
1265 if (test_bit(i, &bmap))
1266 *deny |= i ;
1270 static int
1271 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1272 unsigned int access, deny;
1274 set_access(&access, stp->st_access_bmap);
1275 set_deny(&deny, stp->st_deny_bmap);
1276 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1277 return 0;
1278 return 1;
1282 * Called to check deny when READ with all zero stateid or
1283 * WRITE with all zero or all one stateid
1285 static int
1286 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1288 struct inode *ino = current_fh->fh_dentry->d_inode;
1289 struct nfs4_file *fp;
1290 struct nfs4_stateid *stp;
1291 int ret;
1293 dprintk("NFSD: nfs4_share_conflict\n");
1295 fp = find_file(ino);
1296 if (!fp)
1297 return nfs_ok;
1298 ret = nfserr_share_denied;
1299 /* Search for conflicting share reservations */
1300 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1301 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1302 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1303 goto out;
1305 ret = nfs_ok;
1306 out:
1307 put_nfs4_file(fp);
1308 return ret;
1311 static inline void
1312 nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1314 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1315 put_write_access(filp->f_dentry->d_inode);
1316 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1321 * Recall a delegation
1323 static int
1324 do_recall(void *__dp)
1326 struct nfs4_delegation *dp = __dp;
1328 daemonize("nfsv4-recall");
1330 nfsd4_cb_recall(dp);
1331 return 0;
1335 * Spawn a thread to perform a recall on the delegation represented
1336 * by the lease (file_lock)
1338 * Called from break_lease() with lock_kernel() held.
1339 * Note: we assume break_lease will only call this *once* for any given
1340 * lease.
1342 static
1343 void nfsd_break_deleg_cb(struct file_lock *fl)
1345 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1346 struct task_struct *t;
1348 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1349 if (!dp)
1350 return;
1352 /* We're assuming the state code never drops its reference
1353 * without first removing the lease. Since we're in this lease
1354 * callback (and since the lease code is serialized by the kernel
1355 * lock) we know the server hasn't removed the lease yet, we know
1356 * it's safe to take a reference: */
1357 atomic_inc(&dp->dl_count);
1359 spin_lock(&recall_lock);
1360 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1361 spin_unlock(&recall_lock);
1363 /* only place dl_time is set. protected by lock_kernel*/
1364 dp->dl_time = get_seconds();
1366 /* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */
1367 fl->fl_break_time = jiffies + NFSD_LEASE_TIME * HZ;
1369 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1370 if (IS_ERR(t)) {
1371 struct nfs4_client *clp = dp->dl_client;
1373 printk(KERN_INFO "NFSD: Callback thread failed for "
1374 "for client (clientid %08x/%08x)\n",
1375 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1376 nfs4_put_delegation(dp);
1381 * The file_lock is being reapd.
1383 * Called by locks_free_lock() with lock_kernel() held.
1385 static
1386 void nfsd_release_deleg_cb(struct file_lock *fl)
1388 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1390 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1392 if (!(fl->fl_flags & FL_LEASE) || !dp)
1393 return;
1394 dp->dl_flock = NULL;
1398 * Set the delegation file_lock back pointer.
1400 * Called from __setlease() with lock_kernel() held.
1402 static
1403 void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1405 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1407 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1408 if (!dp)
1409 return;
1410 dp->dl_flock = new;
1414 * Called from __setlease() with lock_kernel() held
1416 static
1417 int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1419 struct nfs4_delegation *onlistd =
1420 (struct nfs4_delegation *)onlist->fl_owner;
1421 struct nfs4_delegation *tryd =
1422 (struct nfs4_delegation *)try->fl_owner;
1424 if (onlist->fl_lmops != try->fl_lmops)
1425 return 0;
1427 return onlistd->dl_client == tryd->dl_client;
1431 static
1432 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1434 if (arg & F_UNLCK)
1435 return lease_modify(onlist, arg);
1436 else
1437 return -EAGAIN;
1440 static struct lock_manager_operations nfsd_lease_mng_ops = {
1441 .fl_break = nfsd_break_deleg_cb,
1442 .fl_release_private = nfsd_release_deleg_cb,
1443 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1444 .fl_mylease = nfsd_same_client_deleg_cb,
1445 .fl_change = nfsd_change_deleg_cb,
1450 * nfsd4_process_open1()
1451 * lookup stateowner.
1452 * found:
1453 * check confirmed
1454 * confirmed:
1455 * check seqid
1456 * not confirmed:
1457 * delete owner
1458 * create new owner
1459 * notfound:
1460 * verify clientid
1461 * create new owner
1463 * called with nfs4_lock_state() held.
1466 nfsd4_process_open1(struct nfsd4_open *open)
1468 int status;
1469 clientid_t *clientid = &open->op_clientid;
1470 struct nfs4_client *clp = NULL;
1471 unsigned int strhashval;
1472 struct nfs4_stateowner *sop = NULL;
1474 status = nfserr_inval;
1475 if (!check_name(open->op_owner))
1476 goto out;
1478 if (STALE_CLIENTID(&open->op_clientid))
1479 return nfserr_stale_clientid;
1481 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1482 sop = find_openstateowner_str(strhashval, open);
1483 if (sop) {
1484 open->op_stateowner = sop;
1485 /* check for replay */
1486 if (open->op_seqid == sop->so_seqid){
1487 if (sop->so_replay.rp_buflen)
1488 return NFSERR_REPLAY_ME;
1489 else {
1490 /* The original OPEN failed so spectacularly
1491 * that we don't even have replay data saved!
1492 * Therefore, we have no choice but to continue
1493 * processing this OPEN; presumably, we'll
1494 * fail again for the same reason.
1496 dprintk("nfsd4_process_open1:"
1497 " replay with no replay cache\n");
1498 goto renew;
1500 } else if (sop->so_confirmed) {
1501 if (open->op_seqid == sop->so_seqid + 1)
1502 goto renew;
1503 status = nfserr_bad_seqid;
1504 goto out;
1505 } else {
1506 /* If we get here, we received an OPEN for an
1507 * unconfirmed nfs4_stateowner. Since the seqid's are
1508 * different, purge the existing nfs4_stateowner, and
1509 * instantiate a new one.
1511 clp = sop->so_client;
1512 release_stateowner(sop);
1514 } else {
1515 /* nfs4_stateowner not found.
1516 * Verify clientid and instantiate new nfs4_stateowner.
1517 * If verify fails this is presumably the result of the
1518 * client's lease expiring.
1520 status = nfserr_expired;
1521 clp = find_confirmed_client(clientid);
1522 if (clp == NULL)
1523 goto out;
1525 status = nfserr_resource;
1526 sop = alloc_init_open_stateowner(strhashval, clp, open);
1527 if (sop == NULL)
1528 goto out;
1529 open->op_stateowner = sop;
1530 renew:
1531 status = nfs_ok;
1532 renew_client(sop->so_client);
1533 out:
1534 if (status && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1535 status = nfserr_reclaim_bad;
1536 return status;
1539 static inline int
1540 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1542 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1543 return nfserr_openmode;
1544 else
1545 return nfs_ok;
1548 static struct nfs4_delegation *
1549 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1551 struct nfs4_delegation *dp;
1553 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) {
1554 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1555 return dp;
1557 return NULL;
1560 static int
1561 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1562 struct nfs4_delegation **dp)
1564 int flags;
1565 int status = nfserr_bad_stateid;
1567 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1568 if (*dp == NULL)
1569 goto out;
1570 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1571 RD_STATE : WR_STATE;
1572 status = nfs4_check_delegmode(*dp, flags);
1573 if (status)
1574 *dp = NULL;
1575 out:
1576 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1577 return nfs_ok;
1578 if (status)
1579 return status;
1580 open->op_stateowner->so_confirmed = 1;
1581 return nfs_ok;
1584 static int
1585 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1587 struct nfs4_stateid *local;
1588 int status = nfserr_share_denied;
1589 struct nfs4_stateowner *sop = open->op_stateowner;
1591 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
1592 /* ignore lock owners */
1593 if (local->st_stateowner->so_is_open_owner == 0)
1594 continue;
1595 /* remember if we have seen this open owner */
1596 if (local->st_stateowner == sop)
1597 *stpp = local;
1598 /* check for conflicting share reservations */
1599 if (!test_share(local, open))
1600 goto out;
1602 status = 0;
1603 out:
1604 return status;
1607 static inline struct nfs4_stateid *
1608 nfs4_alloc_stateid(void)
1610 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
1613 static int
1614 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
1615 struct nfs4_delegation *dp,
1616 struct svc_fh *cur_fh, int flags)
1618 struct nfs4_stateid *stp;
1620 stp = nfs4_alloc_stateid();
1621 if (stp == NULL)
1622 return nfserr_resource;
1624 if (dp) {
1625 get_file(dp->dl_vfs_file);
1626 stp->st_vfs_file = dp->dl_vfs_file;
1627 } else {
1628 int status;
1629 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1630 &stp->st_vfs_file);
1631 if (status) {
1632 if (status == nfserr_dropit)
1633 status = nfserr_jukebox;
1634 kmem_cache_free(stateid_slab, stp);
1635 return status;
1638 *stpp = stp;
1639 return 0;
1642 static inline int
1643 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1644 struct nfsd4_open *open)
1646 struct iattr iattr = {
1647 .ia_valid = ATTR_SIZE,
1648 .ia_size = 0,
1650 if (!open->op_truncate)
1651 return 0;
1652 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
1653 return -EINVAL;
1654 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1657 static int
1658 nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1660 struct file *filp = stp->st_vfs_file;
1661 struct inode *inode = filp->f_dentry->d_inode;
1662 unsigned int share_access;
1663 int status;
1665 set_access(&share_access, stp->st_access_bmap);
1666 share_access = ~share_access;
1667 share_access &= open->op_share_access;
1669 if (!(share_access & NFS4_SHARE_ACCESS_WRITE))
1670 return nfsd4_truncate(rqstp, cur_fh, open);
1672 status = get_write_access(inode);
1673 if (status)
1674 return nfserrno(status);
1675 status = nfsd4_truncate(rqstp, cur_fh, open);
1676 if (status) {
1677 put_write_access(inode);
1678 return status;
1680 /* remember the open */
1681 filp->f_mode = (filp->f_mode | FMODE_WRITE) & ~FMODE_READ;
1682 set_bit(open->op_share_access, &stp->st_access_bmap);
1683 set_bit(open->op_share_deny, &stp->st_deny_bmap);
1685 return nfs_ok;
1689 /* decrement seqid on successful reclaim, it will be bumped in encode_open */
1690 static void
1691 nfs4_set_claim_prev(struct nfsd4_open *open, int *status)
1693 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) {
1694 if (*status)
1695 *status = nfserr_reclaim_bad;
1696 else {
1697 open->op_stateowner->so_confirmed = 1;
1698 open->op_stateowner->so_client->cl_firststate = 1;
1699 open->op_stateowner->so_seqid--;
1705 * Attempt to hand out a delegation.
1707 static void
1708 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1710 struct nfs4_delegation *dp;
1711 struct nfs4_stateowner *sop = stp->st_stateowner;
1712 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1713 struct file_lock fl, *flp = &fl;
1714 int status, flag = 0;
1716 flag = NFS4_OPEN_DELEGATE_NONE;
1717 open->op_recall = 0;
1718 switch (open->op_claim_type) {
1719 case NFS4_OPEN_CLAIM_PREVIOUS:
1720 if (!atomic_read(&cb->cb_set))
1721 open->op_recall = 1;
1722 flag = open->op_delegate_type;
1723 if (flag == NFS4_OPEN_DELEGATE_NONE)
1724 goto out;
1725 break;
1726 case NFS4_OPEN_CLAIM_NULL:
1727 /* Let's not give out any delegations till everyone's
1728 * had the chance to reclaim theirs.... */
1729 if (nfs4_in_grace())
1730 goto out;
1731 if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
1732 goto out;
1733 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1734 flag = NFS4_OPEN_DELEGATE_WRITE;
1735 else
1736 flag = NFS4_OPEN_DELEGATE_READ;
1737 break;
1738 default:
1739 goto out;
1742 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1743 if (dp == NULL) {
1744 flag = NFS4_OPEN_DELEGATE_NONE;
1745 goto out;
1747 locks_init_lock(&fl);
1748 fl.fl_lmops = &nfsd_lease_mng_ops;
1749 fl.fl_flags = FL_LEASE;
1750 fl.fl_end = OFFSET_MAX;
1751 fl.fl_owner = (fl_owner_t)dp;
1752 fl.fl_file = stp->st_vfs_file;
1753 fl.fl_pid = current->tgid;
1755 /* setlease checks to see if delegation should be handed out.
1756 * the lock_manager callbacks fl_mylease and fl_change are used
1758 if ((status = setlease(stp->st_vfs_file,
1759 flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) {
1760 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
1761 unhash_delegation(dp);
1762 flag = NFS4_OPEN_DELEGATE_NONE;
1763 goto out;
1766 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1768 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1769 dp->dl_stateid.si_boot,
1770 dp->dl_stateid.si_stateownerid,
1771 dp->dl_stateid.si_fileid,
1772 dp->dl_stateid.si_generation);
1773 out:
1774 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
1775 && flag == NFS4_OPEN_DELEGATE_NONE
1776 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
1777 printk("NFSD: WARNING: refusing delegation reclaim\n");
1778 open->op_delegate_type = flag;
1782 * called with nfs4_lock_state() held.
1785 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1787 struct nfs4_file *fp = NULL;
1788 struct inode *ino = current_fh->fh_dentry->d_inode;
1789 struct nfs4_stateid *stp = NULL;
1790 struct nfs4_delegation *dp = NULL;
1791 int status;
1793 status = nfserr_inval;
1794 if (!TEST_ACCESS(open->op_share_access) || !TEST_DENY(open->op_share_deny))
1795 goto out;
1797 * Lookup file; if found, lookup stateid and check open request,
1798 * and check for delegations in the process of being recalled.
1799 * If not found, create the nfs4_file struct
1801 fp = find_file(ino);
1802 if (fp) {
1803 if ((status = nfs4_check_open(fp, open, &stp)))
1804 goto out;
1805 status = nfs4_check_deleg(fp, open, &dp);
1806 if (status)
1807 goto out;
1808 } else {
1809 status = nfserr_bad_stateid;
1810 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1811 goto out;
1812 status = nfserr_resource;
1813 fp = alloc_init_file(ino);
1814 if (fp == NULL)
1815 goto out;
1819 * OPEN the file, or upgrade an existing OPEN.
1820 * If truncate fails, the OPEN fails.
1822 if (stp) {
1823 /* Stateid was found, this is an OPEN upgrade */
1824 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1825 if (status)
1826 goto out;
1827 } else {
1828 /* Stateid was not found, this is a new OPEN */
1829 int flags = 0;
1830 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1831 flags = MAY_WRITE;
1832 else
1833 flags = MAY_READ;
1834 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1835 if (status)
1836 goto out;
1837 init_stateid(stp, fp, open);
1838 status = nfsd4_truncate(rqstp, current_fh, open);
1839 if (status) {
1840 release_stateid(stp, OPEN_STATE);
1841 goto out;
1844 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1847 * Attempt to hand out a delegation. No error return, because the
1848 * OPEN succeeds even if we fail.
1850 nfs4_open_delegation(current_fh, open, stp);
1852 status = nfs_ok;
1854 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1855 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1856 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1857 out:
1858 if (fp)
1859 put_nfs4_file(fp);
1860 /* CLAIM_PREVIOUS has different error returns */
1861 nfs4_set_claim_prev(open, &status);
1863 * To finish the open response, we just need to set the rflags.
1865 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1866 if (!open->op_stateowner->so_confirmed)
1867 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1869 return status;
1872 static struct workqueue_struct *laundry_wq;
1873 static struct work_struct laundromat_work;
1874 static void laundromat_main(void *);
1875 static DECLARE_WORK(laundromat_work, laundromat_main, NULL);
1877 int
1878 nfsd4_renew(clientid_t *clid)
1880 struct nfs4_client *clp;
1881 int status;
1883 nfs4_lock_state();
1884 dprintk("process_renew(%08x/%08x): starting\n",
1885 clid->cl_boot, clid->cl_id);
1886 status = nfserr_stale_clientid;
1887 if (STALE_CLIENTID(clid))
1888 goto out;
1889 clp = find_confirmed_client(clid);
1890 status = nfserr_expired;
1891 if (clp == NULL) {
1892 /* We assume the client took too long to RENEW. */
1893 dprintk("nfsd4_renew: clientid not found!\n");
1894 goto out;
1896 renew_client(clp);
1897 status = nfserr_cb_path_down;
1898 if (!list_empty(&clp->cl_delegations)
1899 && !atomic_read(&clp->cl_callback.cb_set))
1900 goto out;
1901 status = nfs_ok;
1902 out:
1903 nfs4_unlock_state();
1904 return status;
1907 static void
1908 end_grace(void)
1910 dprintk("NFSD: end of grace period\n");
1911 nfsd4_recdir_purge_old();
1912 in_grace = 0;
1915 static time_t
1916 nfs4_laundromat(void)
1918 struct nfs4_client *clp;
1919 struct nfs4_stateowner *sop;
1920 struct nfs4_delegation *dp;
1921 struct list_head *pos, *next, reaplist;
1922 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1923 time_t t, clientid_val = NFSD_LEASE_TIME;
1924 time_t u, test_val = NFSD_LEASE_TIME;
1926 nfs4_lock_state();
1928 dprintk("NFSD: laundromat service - starting\n");
1929 if (in_grace)
1930 end_grace();
1931 list_for_each_safe(pos, next, &client_lru) {
1932 clp = list_entry(pos, struct nfs4_client, cl_lru);
1933 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1934 t = clp->cl_time - cutoff;
1935 if (clientid_val > t)
1936 clientid_val = t;
1937 break;
1939 dprintk("NFSD: purging unused client (clientid %08x)\n",
1940 clp->cl_clientid.cl_id);
1941 nfsd4_remove_clid_dir(clp);
1942 expire_client(clp);
1944 INIT_LIST_HEAD(&reaplist);
1945 spin_lock(&recall_lock);
1946 list_for_each_safe(pos, next, &del_recall_lru) {
1947 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1948 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1949 u = dp->dl_time - cutoff;
1950 if (test_val > u)
1951 test_val = u;
1952 break;
1954 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1955 dp, dp->dl_flock);
1956 list_move(&dp->dl_recall_lru, &reaplist);
1958 spin_unlock(&recall_lock);
1959 list_for_each_safe(pos, next, &reaplist) {
1960 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1961 list_del_init(&dp->dl_recall_lru);
1962 unhash_delegation(dp);
1964 test_val = NFSD_LEASE_TIME;
1965 list_for_each_safe(pos, next, &close_lru) {
1966 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1967 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1968 u = sop->so_time - cutoff;
1969 if (test_val > u)
1970 test_val = u;
1971 break;
1973 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1974 sop->so_id);
1975 list_del(&sop->so_close_lru);
1976 nfs4_put_stateowner(sop);
1978 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1979 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1980 nfs4_unlock_state();
1981 return clientid_val;
1984 void
1985 laundromat_main(void *not_used)
1987 time_t t;
1989 t = nfs4_laundromat();
1990 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
1991 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
1994 /* search ownerid_hashtbl[] and close_lru for stateid owner
1995 * (stateid->si_stateownerid)
1997 static struct nfs4_stateowner *
1998 find_openstateowner_id(u32 st_id, int flags) {
1999 struct nfs4_stateowner *local = NULL;
2001 dprintk("NFSD: find_openstateowner_id %d\n", st_id);
2002 if (flags & CLOSE_STATE) {
2003 list_for_each_entry(local, &close_lru, so_close_lru) {
2004 if (local->so_id == st_id)
2005 return local;
2008 return NULL;
2011 static inline int
2012 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
2014 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_dentry->d_inode;
2017 static int
2018 STALE_STATEID(stateid_t *stateid)
2020 if (stateid->si_boot == boot_time)
2021 return 0;
2022 printk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
2023 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2024 stateid->si_generation);
2025 return 1;
2028 static inline int
2029 access_permit_read(unsigned long access_bmap)
2031 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
2032 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
2033 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
2036 static inline int
2037 access_permit_write(unsigned long access_bmap)
2039 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
2040 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
2043 static
2044 int nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
2046 int status = nfserr_openmode;
2048 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2049 goto out;
2050 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2051 goto out;
2052 status = nfs_ok;
2053 out:
2054 return status;
2057 static inline int
2058 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2060 /* Trying to call delegreturn with a special stateid? Yuch: */
2061 if (!(flags & (RD_STATE | WR_STATE)))
2062 return nfserr_bad_stateid;
2063 else if (ONE_STATEID(stateid) && (flags & RD_STATE))
2064 return nfs_ok;
2065 else if (nfs4_in_grace()) {
2066 /* Answer in remaining cases depends on existance of
2067 * conflicting state; so we must wait out the grace period. */
2068 return nfserr_grace;
2069 } else if (flags & WR_STATE)
2070 return nfs4_share_conflict(current_fh,
2071 NFS4_SHARE_DENY_WRITE);
2072 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2073 return nfs4_share_conflict(current_fh,
2074 NFS4_SHARE_DENY_READ);
2078 * Allow READ/WRITE during grace period on recovered state only for files
2079 * that are not able to provide mandatory locking.
2081 static inline int
2082 io_during_grace_disallowed(struct inode *inode, int flags)
2084 return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE))
2085 && MANDATORY_LOCK(inode);
2089 * Checks for stateid operations
2092 nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2094 struct nfs4_stateid *stp = NULL;
2095 struct nfs4_delegation *dp = NULL;
2096 stateid_t *stidp;
2097 struct inode *ino = current_fh->fh_dentry->d_inode;
2098 int status;
2100 dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2101 stateid->si_boot, stateid->si_stateownerid,
2102 stateid->si_fileid, stateid->si_generation);
2103 if (filpp)
2104 *filpp = NULL;
2106 if (io_during_grace_disallowed(ino, flags))
2107 return nfserr_grace;
2109 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2110 return check_special_stateids(current_fh, stateid, flags);
2112 /* STALE STATEID */
2113 status = nfserr_stale_stateid;
2114 if (STALE_STATEID(stateid))
2115 goto out;
2117 /* BAD STATEID */
2118 status = nfserr_bad_stateid;
2119 if (!stateid->si_fileid) { /* delegation stateid */
2120 if(!(dp = find_delegation_stateid(ino, stateid))) {
2121 dprintk("NFSD: delegation stateid not found\n");
2122 if (nfs4_in_grace())
2123 status = nfserr_grace;
2124 goto out;
2126 stidp = &dp->dl_stateid;
2127 } else { /* open or lock stateid */
2128 if (!(stp = find_stateid(stateid, flags))) {
2129 dprintk("NFSD: open or lock stateid not found\n");
2130 if (nfs4_in_grace())
2131 status = nfserr_grace;
2132 goto out;
2134 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
2135 goto out;
2136 if (!stp->st_stateowner->so_confirmed)
2137 goto out;
2138 stidp = &stp->st_stateid;
2140 if (stateid->si_generation > stidp->si_generation)
2141 goto out;
2143 /* OLD STATEID */
2144 status = nfserr_old_stateid;
2145 if (stateid->si_generation < stidp->si_generation)
2146 goto out;
2147 if (stp) {
2148 if ((status = nfs4_check_openmode(stp,flags)))
2149 goto out;
2150 renew_client(stp->st_stateowner->so_client);
2151 if (filpp)
2152 *filpp = stp->st_vfs_file;
2153 } else if (dp) {
2154 if ((status = nfs4_check_delegmode(dp, flags)))
2155 goto out;
2156 renew_client(dp->dl_client);
2157 if (flags & DELEG_RET)
2158 unhash_delegation(dp);
2159 if (filpp)
2160 *filpp = dp->dl_vfs_file;
2162 status = nfs_ok;
2163 out:
2164 return status;
2169 * Checks for sequence id mutating operations.
2171 static int
2172 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)
2174 int status;
2175 struct nfs4_stateid *stp;
2176 struct nfs4_stateowner *sop;
2178 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2179 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2180 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2181 stateid->si_generation);
2183 *stpp = NULL;
2184 *sopp = NULL;
2186 status = nfserr_bad_stateid;
2187 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2188 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
2189 goto out;
2192 status = nfserr_stale_stateid;
2193 if (STALE_STATEID(stateid))
2194 goto out;
2196 * We return BAD_STATEID if filehandle doesn't match stateid,
2197 * the confirmed flag is incorrecly set, or the generation
2198 * number is incorrect.
2199 * If there is no entry in the openfile table for this id,
2200 * we can't always return BAD_STATEID;
2201 * this might be a retransmitted CLOSE which has arrived after
2202 * the openfile has been released.
2204 if (!(stp = find_stateid(stateid, flags)))
2205 goto no_nfs4_stateid;
2207 status = nfserr_bad_stateid;
2209 /* for new lock stateowners:
2210 * check that the lock->v.new.open_stateid
2211 * refers to an open stateowner
2213 * check that the lockclid (nfs4_lock->v.new.clientid) is the same
2214 * as the open_stateid->st_stateowner->so_client->clientid
2216 if (lockclid) {
2217 struct nfs4_stateowner *sop = stp->st_stateowner;
2218 struct nfs4_client *clp = sop->so_client;
2220 if (!sop->so_is_open_owner)
2221 goto out;
2222 if (!cmp_clid(&clp->cl_clientid, lockclid))
2223 goto out;
2226 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
2227 printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
2228 goto out;
2231 *stpp = stp;
2232 *sopp = sop = stp->st_stateowner;
2235 * We now validate the seqid and stateid generation numbers.
2236 * For the moment, we ignore the possibility of
2237 * generation number wraparound.
2239 if (seqid != sop->so_seqid + 1)
2240 goto check_replay;
2242 if (sop->so_confirmed) {
2243 if (flags & CONFIRM) {
2244 printk("NFSD: preprocess_seqid_op: expected unconfirmed stateowner!\n");
2245 goto out;
2248 else {
2249 if (!(flags & CONFIRM)) {
2250 printk("NFSD: preprocess_seqid_op: stateowner not confirmed yet!\n");
2251 goto out;
2254 if (stateid->si_generation > stp->st_stateid.si_generation) {
2255 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
2256 goto out;
2259 status = nfserr_old_stateid;
2260 if (stateid->si_generation < stp->st_stateid.si_generation) {
2261 printk("NFSD: preprocess_seqid_op: old stateid!\n");
2262 goto out;
2264 /* XXX renew the client lease here */
2265 status = nfs_ok;
2267 out:
2268 return status;
2270 no_nfs4_stateid:
2273 * We determine whether this is a bad stateid or a replay,
2274 * starting by trying to look up the stateowner.
2275 * If stateowner is not found - stateid is bad.
2277 if (!(sop = find_openstateowner_id(stateid->si_stateownerid, flags))) {
2278 printk("NFSD: preprocess_seqid_op: no stateowner or nfs4_stateid!\n");
2279 status = nfserr_bad_stateid;
2280 goto out;
2282 *sopp = sop;
2284 check_replay:
2285 if (seqid == sop->so_seqid) {
2286 printk("NFSD: preprocess_seqid_op: retransmission?\n");
2287 /* indicate replay to calling function */
2288 status = NFSERR_REPLAY_ME;
2289 } else {
2290 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d\n", sop->so_seqid +1, seqid);
2292 *sopp = NULL;
2293 status = nfserr_bad_seqid;
2295 goto out;
2299 nfsd4_open_confirm(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_confirm *oc)
2301 int status;
2302 struct nfs4_stateowner *sop;
2303 struct nfs4_stateid *stp;
2305 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
2306 (int)current_fh->fh_dentry->d_name.len,
2307 current_fh->fh_dentry->d_name.name);
2309 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2310 goto out;
2312 nfs4_lock_state();
2314 if ((status = nfs4_preprocess_seqid_op(current_fh, oc->oc_seqid,
2315 &oc->oc_req_stateid,
2316 CHECK_FH | CONFIRM | OPEN_STATE,
2317 &oc->oc_stateowner, &stp, NULL)))
2318 goto out;
2320 sop = oc->oc_stateowner;
2321 sop->so_confirmed = 1;
2322 update_stateid(&stp->st_stateid);
2323 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2324 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2325 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2326 stp->st_stateid.si_boot,
2327 stp->st_stateid.si_stateownerid,
2328 stp->st_stateid.si_fileid,
2329 stp->st_stateid.si_generation);
2331 nfsd4_create_clid_dir(sop->so_client);
2332 out:
2333 if (oc->oc_stateowner)
2334 nfs4_get_stateowner(oc->oc_stateowner);
2335 nfs4_unlock_state();
2336 return status;
2341 * unset all bits in union bitmap (bmap) that
2342 * do not exist in share (from successful OPEN_DOWNGRADE)
2344 static void
2345 reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2347 int i;
2348 for (i = 1; i < 4; i++) {
2349 if ((i & access) != i)
2350 __clear_bit(i, bmap);
2354 static void
2355 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2357 int i;
2358 for (i = 0; i < 4; i++) {
2359 if ((i & deny) != i)
2360 __clear_bit(i, bmap);
2365 nfsd4_open_downgrade(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_downgrade *od)
2367 int status;
2368 struct nfs4_stateid *stp;
2369 unsigned int share_access;
2371 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
2372 (int)current_fh->fh_dentry->d_name.len,
2373 current_fh->fh_dentry->d_name.name);
2375 if (!TEST_ACCESS(od->od_share_access) || !TEST_DENY(od->od_share_deny))
2376 return nfserr_inval;
2378 nfs4_lock_state();
2379 if ((status = nfs4_preprocess_seqid_op(current_fh, od->od_seqid,
2380 &od->od_stateid,
2381 CHECK_FH | OPEN_STATE,
2382 &od->od_stateowner, &stp, NULL)))
2383 goto out;
2385 status = nfserr_inval;
2386 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2387 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2388 stp->st_access_bmap, od->od_share_access);
2389 goto out;
2391 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2392 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2393 stp->st_deny_bmap, od->od_share_deny);
2394 goto out;
2396 set_access(&share_access, stp->st_access_bmap);
2397 nfs4_file_downgrade(stp->st_vfs_file,
2398 share_access & ~od->od_share_access);
2400 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2401 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2403 update_stateid(&stp->st_stateid);
2404 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2405 status = nfs_ok;
2406 out:
2407 if (od->od_stateowner)
2408 nfs4_get_stateowner(od->od_stateowner);
2409 nfs4_unlock_state();
2410 return status;
2414 * nfs4_unlock_state() called after encode
2417 nfsd4_close(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_close *close)
2419 int status;
2420 struct nfs4_stateid *stp;
2422 dprintk("NFSD: nfsd4_close on file %.*s\n",
2423 (int)current_fh->fh_dentry->d_name.len,
2424 current_fh->fh_dentry->d_name.name);
2426 nfs4_lock_state();
2427 /* check close_lru for replay */
2428 if ((status = nfs4_preprocess_seqid_op(current_fh, close->cl_seqid,
2429 &close->cl_stateid,
2430 CHECK_FH | OPEN_STATE | CLOSE_STATE,
2431 &close->cl_stateowner, &stp, NULL)))
2432 goto out;
2434 * Return success, but first update the stateid.
2436 status = nfs_ok;
2437 update_stateid(&stp->st_stateid);
2438 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2440 /* release_state_owner() calls nfsd_close() if needed */
2441 release_state_owner(stp, OPEN_STATE);
2442 out:
2443 if (close->cl_stateowner)
2444 nfs4_get_stateowner(close->cl_stateowner);
2445 nfs4_unlock_state();
2446 return status;
2450 nfsd4_delegreturn(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_delegreturn *dr)
2452 int status;
2454 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2455 goto out;
2457 nfs4_lock_state();
2458 status = nfs4_preprocess_stateid_op(current_fh, &dr->dr_stateid, DELEG_RET, NULL);
2459 nfs4_unlock_state();
2460 out:
2461 return status;
2466 * Lock owner state (byte-range locks)
2468 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2469 #define LOCK_HASH_BITS 8
2470 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2471 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2473 #define lockownerid_hashval(id) \
2474 ((id) & LOCK_HASH_MASK)
2476 static inline unsigned int
2477 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2478 struct xdr_netobj *ownername)
2480 return (file_hashval(inode) + cl_id
2481 + opaque_hashval(ownername->data, ownername->len))
2482 & LOCK_HASH_MASK;
2485 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2486 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2487 static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2489 static struct nfs4_stateid *
2490 find_stateid(stateid_t *stid, int flags)
2492 struct nfs4_stateid *local = NULL;
2493 u32 st_id = stid->si_stateownerid;
2494 u32 f_id = stid->si_fileid;
2495 unsigned int hashval;
2497 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
2498 if ((flags & LOCK_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2499 hashval = stateid_hashval(st_id, f_id);
2500 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2501 if ((local->st_stateid.si_stateownerid == st_id) &&
2502 (local->st_stateid.si_fileid == f_id))
2503 return local;
2506 if ((flags & OPEN_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2507 hashval = stateid_hashval(st_id, f_id);
2508 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2509 if ((local->st_stateid.si_stateownerid == st_id) &&
2510 (local->st_stateid.si_fileid == f_id))
2511 return local;
2513 } else
2514 printk("NFSD: find_stateid: ERROR: no state flag\n");
2515 return NULL;
2518 static struct nfs4_delegation *
2519 find_delegation_stateid(struct inode *ino, stateid_t *stid)
2521 struct nfs4_file *fp;
2522 struct nfs4_delegation *dl;
2524 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2525 stid->si_boot, stid->si_stateownerid,
2526 stid->si_fileid, stid->si_generation);
2528 fp = find_file(ino);
2529 if (!fp)
2530 return NULL;
2531 dl = find_delegation_file(fp, stid);
2532 put_nfs4_file(fp);
2533 return dl;
2537 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2538 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2539 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2540 * locking, this prevents us from being completely protocol-compliant. The
2541 * real solution to this problem is to start using unsigned file offsets in
2542 * the VFS, but this is a very deep change!
2544 static inline void
2545 nfs4_transform_lock_offset(struct file_lock *lock)
2547 if (lock->fl_start < 0)
2548 lock->fl_start = OFFSET_MAX;
2549 if (lock->fl_end < 0)
2550 lock->fl_end = OFFSET_MAX;
2553 static int
2554 nfs4_verify_lock_stateowner(struct nfs4_stateowner *sop, unsigned int hashval)
2556 struct nfs4_stateowner *local = NULL;
2557 int status = 0;
2559 if (hashval >= LOCK_HASH_SIZE)
2560 goto out;
2561 list_for_each_entry(local, &lock_ownerid_hashtbl[hashval], so_idhash) {
2562 if (local == sop) {
2563 status = 1;
2564 goto out;
2567 out:
2568 return status;
2572 static inline void
2573 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2575 struct nfs4_stateowner *sop = (struct nfs4_stateowner *) fl->fl_owner;
2576 unsigned int hval = lockownerid_hashval(sop->so_id);
2578 deny->ld_sop = NULL;
2579 if (nfs4_verify_lock_stateowner(sop, hval)) {
2580 kref_get(&sop->so_ref);
2581 deny->ld_sop = sop;
2582 deny->ld_clientid = sop->so_client->cl_clientid;
2584 deny->ld_start = fl->fl_start;
2585 deny->ld_length = ~(u64)0;
2586 if (fl->fl_end != ~(u64)0)
2587 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2588 deny->ld_type = NFS4_READ_LT;
2589 if (fl->fl_type != F_RDLCK)
2590 deny->ld_type = NFS4_WRITE_LT;
2593 static struct nfs4_stateowner *
2594 find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2595 struct xdr_netobj *owner)
2597 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2598 struct nfs4_stateowner *op;
2600 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
2601 if (cmp_owner_str(op, owner, clid))
2602 return op;
2604 return NULL;
2608 * Alloc a lock owner structure.
2609 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2610 * occured.
2612 * strhashval = lock_ownerstr_hashval
2613 * so_seqid = lock->lk_new_lock_seqid - 1: it gets bumped in encode
2616 static struct nfs4_stateowner *
2617 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2618 struct nfs4_stateowner *sop;
2619 struct nfs4_replay *rp;
2620 unsigned int idhashval;
2622 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2623 return NULL;
2624 idhashval = lockownerid_hashval(current_ownerid);
2625 INIT_LIST_HEAD(&sop->so_idhash);
2626 INIT_LIST_HEAD(&sop->so_strhash);
2627 INIT_LIST_HEAD(&sop->so_perclient);
2628 INIT_LIST_HEAD(&sop->so_stateids);
2629 INIT_LIST_HEAD(&sop->so_perstateid);
2630 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2631 sop->so_time = 0;
2632 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2633 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
2634 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
2635 sop->so_is_open_owner = 0;
2636 sop->so_id = current_ownerid++;
2637 sop->so_client = clp;
2638 sop->so_seqid = lock->lk_new_lock_seqid - 1;
2639 sop->so_confirmed = 1;
2640 rp = &sop->so_replay;
2641 rp->rp_status = NFSERR_SERVERFAULT;
2642 rp->rp_buflen = 0;
2643 rp->rp_buf = rp->rp_ibuf;
2644 return sop;
2647 static struct nfs4_stateid *
2648 alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2650 struct nfs4_stateid *stp;
2651 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2653 stp = nfs4_alloc_stateid();
2654 if (stp == NULL)
2655 goto out;
2656 INIT_LIST_HEAD(&stp->st_hash);
2657 INIT_LIST_HEAD(&stp->st_perfile);
2658 INIT_LIST_HEAD(&stp->st_perstateowner);
2659 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
2660 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
2661 list_add(&stp->st_perfile, &fp->fi_stateids);
2662 list_add(&stp->st_perstateowner, &sop->so_stateids);
2663 stp->st_stateowner = sop;
2664 get_nfs4_file(fp);
2665 stp->st_file = fp;
2666 stp->st_stateid.si_boot = boot_time;
2667 stp->st_stateid.si_stateownerid = sop->so_id;
2668 stp->st_stateid.si_fileid = fp->fi_id;
2669 stp->st_stateid.si_generation = 0;
2670 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2671 stp->st_access_bmap = open_stp->st_access_bmap;
2672 stp->st_deny_bmap = open_stp->st_deny_bmap;
2674 out:
2675 return stp;
2678 static int
2679 check_lock_length(u64 offset, u64 length)
2681 return ((length == 0) || ((length != ~(u64)0) &&
2682 LOFF_OVERFLOW(offset, length)));
2686 * LOCK operation
2689 nfsd4_lock(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lock *lock)
2691 struct nfs4_stateowner *open_sop = NULL;
2692 struct nfs4_stateid *lock_stp;
2693 struct file *filp;
2694 struct file_lock file_lock;
2695 struct file_lock *conflock;
2696 int status = 0;
2697 unsigned int strhashval;
2699 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2700 (long long) lock->lk_offset,
2701 (long long) lock->lk_length);
2703 if (nfs4_in_grace() && !lock->lk_reclaim)
2704 return nfserr_grace;
2705 if (!nfs4_in_grace() && lock->lk_reclaim)
2706 return nfserr_no_grace;
2708 if (check_lock_length(lock->lk_offset, lock->lk_length))
2709 return nfserr_inval;
2711 nfs4_lock_state();
2713 if (lock->lk_is_new) {
2715 * Client indicates that this is a new lockowner.
2716 * Use open owner and open stateid to create lock owner and lock
2717 * stateid.
2719 struct nfs4_stateid *open_stp = NULL;
2720 struct nfs4_file *fp;
2722 status = nfserr_stale_clientid;
2723 if (STALE_CLIENTID(&lock->lk_new_clientid)) {
2724 printk("NFSD: nfsd4_lock: clientid is stale!\n");
2725 goto out;
2728 /* is the new lock seqid presented by the client zero? */
2729 status = nfserr_bad_seqid;
2730 if (lock->v.new.lock_seqid != 0)
2731 goto out;
2733 /* validate and update open stateid and open seqid */
2734 status = nfs4_preprocess_seqid_op(current_fh,
2735 lock->lk_new_open_seqid,
2736 &lock->lk_new_open_stateid,
2737 CHECK_FH | OPEN_STATE,
2738 &open_sop, &open_stp,
2739 &lock->v.new.clientid);
2740 if (status) {
2741 if (lock->lk_reclaim)
2742 status = nfserr_reclaim_bad;
2743 goto out;
2745 /* create lockowner and lock stateid */
2746 fp = open_stp->st_file;
2747 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2748 open_sop->so_client->cl_clientid.cl_id,
2749 &lock->v.new.owner);
2750 /* XXX: Do we need to check for duplicate stateowners on
2751 * the same file, or should they just be allowed (and
2752 * create new stateids)? */
2753 status = nfserr_resource;
2754 if (!(lock->lk_stateowner = alloc_init_lock_stateowner(strhashval, open_sop->so_client, open_stp, lock)))
2755 goto out;
2756 if ((lock_stp = alloc_init_lock_stateid(lock->lk_stateowner,
2757 fp, open_stp)) == NULL) {
2758 release_stateowner(lock->lk_stateowner);
2759 lock->lk_stateowner = NULL;
2760 goto out;
2762 /* bump the open seqid used to create the lock */
2763 open_sop->so_seqid++;
2764 } else {
2765 /* lock (lock owner + lock stateid) already exists */
2766 status = nfs4_preprocess_seqid_op(current_fh,
2767 lock->lk_old_lock_seqid,
2768 &lock->lk_old_lock_stateid,
2769 CHECK_FH | LOCK_STATE,
2770 &lock->lk_stateowner, &lock_stp, NULL);
2771 if (status)
2772 goto out;
2774 /* lock->lk_stateowner and lock_stp have been created or found */
2775 filp = lock_stp->st_vfs_file;
2777 if ((status = fh_verify(rqstp, current_fh, S_IFREG, MAY_LOCK))) {
2778 printk("NFSD: nfsd4_lock: permission denied!\n");
2779 goto out;
2782 locks_init_lock(&file_lock);
2783 switch (lock->lk_type) {
2784 case NFS4_READ_LT:
2785 case NFS4_READW_LT:
2786 file_lock.fl_type = F_RDLCK;
2787 break;
2788 case NFS4_WRITE_LT:
2789 case NFS4_WRITEW_LT:
2790 file_lock.fl_type = F_WRLCK;
2791 break;
2792 default:
2793 status = nfserr_inval;
2794 goto out;
2796 file_lock.fl_owner = (fl_owner_t) lock->lk_stateowner;
2797 file_lock.fl_pid = current->tgid;
2798 file_lock.fl_file = filp;
2799 file_lock.fl_flags = FL_POSIX;
2801 file_lock.fl_start = lock->lk_offset;
2802 if ((lock->lk_length == ~(u64)0) ||
2803 LOFF_OVERFLOW(lock->lk_offset, lock->lk_length))
2804 file_lock.fl_end = ~(u64)0;
2805 else
2806 file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
2807 nfs4_transform_lock_offset(&file_lock);
2810 * Try to lock the file in the VFS.
2811 * Note: locks.c uses the BKL to protect the inode's lock list.
2814 status = posix_lock_file(filp, &file_lock);
2815 if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
2816 file_lock.fl_ops->fl_release_private(&file_lock);
2817 dprintk("NFSD: nfsd4_lock: posix_lock_file status %d\n",status);
2818 switch (-status) {
2819 case 0: /* success! */
2820 update_stateid(&lock_stp->st_stateid);
2821 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2822 sizeof(stateid_t));
2823 goto out;
2824 case (EAGAIN):
2825 goto conflicting_lock;
2826 case (EDEADLK):
2827 status = nfserr_deadlock;
2828 default:
2829 dprintk("NFSD: nfsd4_lock: posix_lock_file() failed! status %d\n",status);
2830 goto out_destroy_new_stateid;
2833 conflicting_lock:
2834 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2835 status = nfserr_denied;
2836 /* XXX There is a race here. Future patch needed to provide
2837 * an atomic posix_lock_and_test_file
2839 if (!(conflock = posix_test_lock(filp, &file_lock))) {
2840 status = nfserr_serverfault;
2841 goto out;
2843 nfs4_set_lock_denied(conflock, &lock->lk_denied);
2845 out_destroy_new_stateid:
2846 if (lock->lk_is_new) {
2847 dprintk("NFSD: nfsd4_lock: destroy new stateid!\n");
2849 * An error encountered after instantiation of the new
2850 * stateid has forced us to destroy it.
2852 if (!seqid_mutating_err(status))
2853 open_sop->so_seqid--;
2855 release_state_owner(lock_stp, LOCK_STATE);
2857 out:
2858 if (lock->lk_stateowner)
2859 nfs4_get_stateowner(lock->lk_stateowner);
2860 nfs4_unlock_state();
2861 return status;
2865 * LOCKT operation
2868 nfsd4_lockt(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lockt *lockt)
2870 struct inode *inode;
2871 struct file file;
2872 struct file_lock file_lock;
2873 struct file_lock *conflicting_lock;
2874 int status;
2876 if (nfs4_in_grace())
2877 return nfserr_grace;
2879 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2880 return nfserr_inval;
2882 lockt->lt_stateowner = NULL;
2883 nfs4_lock_state();
2885 status = nfserr_stale_clientid;
2886 if (STALE_CLIENTID(&lockt->lt_clientid)) {
2887 printk("NFSD: nfsd4_lockt: clientid is stale!\n");
2888 goto out;
2891 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0))) {
2892 printk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
2893 if (status == nfserr_symlink)
2894 status = nfserr_inval;
2895 goto out;
2898 inode = current_fh->fh_dentry->d_inode;
2899 locks_init_lock(&file_lock);
2900 switch (lockt->lt_type) {
2901 case NFS4_READ_LT:
2902 case NFS4_READW_LT:
2903 file_lock.fl_type = F_RDLCK;
2904 break;
2905 case NFS4_WRITE_LT:
2906 case NFS4_WRITEW_LT:
2907 file_lock.fl_type = F_WRLCK;
2908 break;
2909 default:
2910 printk("NFSD: nfs4_lockt: bad lock type!\n");
2911 status = nfserr_inval;
2912 goto out;
2915 lockt->lt_stateowner = find_lockstateowner_str(inode,
2916 &lockt->lt_clientid, &lockt->lt_owner);
2917 if (lockt->lt_stateowner)
2918 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2919 file_lock.fl_pid = current->tgid;
2920 file_lock.fl_flags = FL_POSIX;
2922 file_lock.fl_start = lockt->lt_offset;
2923 if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length))
2924 file_lock.fl_end = ~(u64)0;
2925 else
2926 file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
2928 nfs4_transform_lock_offset(&file_lock);
2930 /* posix_test_lock uses the struct file _only_ to resolve the inode.
2931 * since LOCKT doesn't require an OPEN, and therefore a struct
2932 * file may not exist, pass posix_test_lock a struct file with
2933 * only the dentry:inode set.
2935 memset(&file, 0, sizeof (struct file));
2936 file.f_dentry = current_fh->fh_dentry;
2938 status = nfs_ok;
2939 conflicting_lock = posix_test_lock(&file, &file_lock);
2940 if (conflicting_lock) {
2941 status = nfserr_denied;
2942 nfs4_set_lock_denied(conflicting_lock, &lockt->lt_denied);
2944 out:
2945 nfs4_unlock_state();
2946 return status;
2950 nfsd4_locku(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_locku *locku)
2952 struct nfs4_stateid *stp;
2953 struct file *filp = NULL;
2954 struct file_lock file_lock;
2955 int status;
2957 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2958 (long long) locku->lu_offset,
2959 (long long) locku->lu_length);
2961 if (check_lock_length(locku->lu_offset, locku->lu_length))
2962 return nfserr_inval;
2964 nfs4_lock_state();
2966 if ((status = nfs4_preprocess_seqid_op(current_fh,
2967 locku->lu_seqid,
2968 &locku->lu_stateid,
2969 CHECK_FH | LOCK_STATE,
2970 &locku->lu_stateowner, &stp, NULL)))
2971 goto out;
2973 filp = stp->st_vfs_file;
2974 BUG_ON(!filp);
2975 locks_init_lock(&file_lock);
2976 file_lock.fl_type = F_UNLCK;
2977 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
2978 file_lock.fl_pid = current->tgid;
2979 file_lock.fl_file = filp;
2980 file_lock.fl_flags = FL_POSIX;
2981 file_lock.fl_start = locku->lu_offset;
2983 if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
2984 file_lock.fl_end = ~(u64)0;
2985 else
2986 file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
2987 nfs4_transform_lock_offset(&file_lock);
2990 * Try to unlock the file in the VFS.
2992 status = posix_lock_file(filp, &file_lock);
2993 if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
2994 file_lock.fl_ops->fl_release_private(&file_lock);
2995 if (status) {
2996 printk("NFSD: nfs4_locku: posix_lock_file failed!\n");
2997 goto out_nfserr;
3000 * OK, unlock succeeded; the only thing left to do is update the stateid.
3002 update_stateid(&stp->st_stateid);
3003 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
3005 out:
3006 if (locku->lu_stateowner)
3007 nfs4_get_stateowner(locku->lu_stateowner);
3008 nfs4_unlock_state();
3009 return status;
3011 out_nfserr:
3012 status = nfserrno(status);
3013 goto out;
3017 * returns
3018 * 1: locks held by lockowner
3019 * 0: no locks held by lockowner
3021 static int
3022 check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
3024 struct file_lock **flpp;
3025 struct inode *inode = filp->f_dentry->d_inode;
3026 int status = 0;
3028 lock_kernel();
3029 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
3030 if ((*flpp)->fl_owner == (fl_owner_t)lowner)
3031 status = 1;
3032 goto out;
3034 out:
3035 unlock_kernel();
3036 return status;
3040 nfsd4_release_lockowner(struct svc_rqst *rqstp, struct nfsd4_release_lockowner *rlockowner)
3042 clientid_t *clid = &rlockowner->rl_clientid;
3043 struct nfs4_stateowner *sop;
3044 struct nfs4_stateid *stp;
3045 struct xdr_netobj *owner = &rlockowner->rl_owner;
3046 struct list_head matches;
3047 int i;
3048 int status;
3050 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
3051 clid->cl_boot, clid->cl_id);
3053 /* XXX check for lease expiration */
3055 status = nfserr_stale_clientid;
3056 if (STALE_CLIENTID(clid)) {
3057 printk("NFSD: nfsd4_release_lockowner: clientid is stale!\n");
3058 return status;
3061 nfs4_lock_state();
3063 status = nfserr_locks_held;
3064 /* XXX: we're doing a linear search through all the lockowners.
3065 * Yipes! For now we'll just hope clients aren't really using
3066 * release_lockowner much, but eventually we have to fix these
3067 * data structures. */
3068 INIT_LIST_HEAD(&matches);
3069 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3070 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
3071 if (!cmp_owner_str(sop, owner, clid))
3072 continue;
3073 list_for_each_entry(stp, &sop->so_stateids,
3074 st_perstateowner) {
3075 if (check_for_locks(stp->st_vfs_file, sop))
3076 goto out;
3077 /* Note: so_perclient unused for lockowners,
3078 * so it's OK to fool with here. */
3079 list_add(&sop->so_perclient, &matches);
3083 /* Clients probably won't expect us to return with some (but not all)
3084 * of the lockowner state released; so don't release any until all
3085 * have been checked. */
3086 status = nfs_ok;
3087 list_for_each_entry(sop, &matches, so_perclient) {
3088 release_stateowner(sop);
3090 out:
3091 nfs4_unlock_state();
3092 return status;
3095 static inline struct nfs4_client_reclaim *
3096 alloc_reclaim(void)
3098 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
3102 nfs4_has_reclaimed_state(const char *name)
3104 unsigned int strhashval = clientstr_hashval(name);
3105 struct nfs4_client *clp;
3107 clp = find_confirmed_client_by_str(name, strhashval);
3108 return clp ? 1 : 0;
3112 * failure => all reset bets are off, nfserr_no_grace...
3115 nfs4_client_to_reclaim(const char *name)
3117 unsigned int strhashval;
3118 struct nfs4_client_reclaim *crp = NULL;
3120 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
3121 crp = alloc_reclaim();
3122 if (!crp)
3123 return 0;
3124 strhashval = clientstr_hashval(name);
3125 INIT_LIST_HEAD(&crp->cr_strhash);
3126 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
3127 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
3128 reclaim_str_hashtbl_size++;
3129 return 1;
3132 static void
3133 nfs4_release_reclaim(void)
3135 struct nfs4_client_reclaim *crp = NULL;
3136 int i;
3138 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3139 while (!list_empty(&reclaim_str_hashtbl[i])) {
3140 crp = list_entry(reclaim_str_hashtbl[i].next,
3141 struct nfs4_client_reclaim, cr_strhash);
3142 list_del(&crp->cr_strhash);
3143 kfree(crp);
3144 reclaim_str_hashtbl_size--;
3147 BUG_ON(reclaim_str_hashtbl_size);
3151 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
3152 static struct nfs4_client_reclaim *
3153 nfs4_find_reclaim_client(clientid_t *clid)
3155 unsigned int strhashval;
3156 struct nfs4_client *clp;
3157 struct nfs4_client_reclaim *crp = NULL;
3160 /* find clientid in conf_id_hashtbl */
3161 clp = find_confirmed_client(clid);
3162 if (clp == NULL)
3163 return NULL;
3165 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3166 clp->cl_name.len, clp->cl_name.data,
3167 clp->cl_recdir);
3169 /* find clp->cl_name in reclaim_str_hashtbl */
3170 strhashval = clientstr_hashval(clp->cl_recdir);
3171 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
3172 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
3173 return crp;
3176 return NULL;
3180 * Called from OPEN. Look for clientid in reclaim list.
3183 nfs4_check_open_reclaim(clientid_t *clid)
3185 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
3188 /* initialization to perform at module load time: */
3190 void
3191 nfs4_state_init(void)
3193 int i;
3195 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3196 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3197 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3198 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3199 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3201 for (i = 0; i < FILE_HASH_SIZE; i++) {
3202 INIT_LIST_HEAD(&file_hashtbl[i]);
3204 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3205 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3206 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3208 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3209 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3210 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3212 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3213 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3214 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3216 memset(&onestateid, ~0, sizeof(stateid_t));
3217 INIT_LIST_HEAD(&close_lru);
3218 INIT_LIST_HEAD(&client_lru);
3219 INIT_LIST_HEAD(&del_recall_lru);
3220 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3221 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3222 reclaim_str_hashtbl_size = 0;
3225 static void
3226 nfsd4_load_reboot_recovery_data(void)
3228 int status;
3230 nfs4_lock_state();
3231 nfsd4_init_recdir(user_recovery_dirname);
3232 status = nfsd4_recdir_load();
3233 nfs4_unlock_state();
3234 if (status)
3235 printk("NFSD: Failure reading reboot recovery data\n");
3238 /* initialization to perform when the nfsd service is started: */
3240 static void
3241 __nfs4_state_start(void)
3243 time_t grace_time;
3245 boot_time = get_seconds();
3246 grace_time = max(user_lease_time, lease_time);
3247 lease_time = user_lease_time;
3248 in_grace = 1;
3249 printk("NFSD: starting %ld-second grace period\n", grace_time);
3250 laundry_wq = create_singlethread_workqueue("nfsd4");
3251 queue_delayed_work(laundry_wq, &laundromat_work, grace_time*HZ);
3255 nfs4_state_start(void)
3257 int status;
3259 if (nfs4_init)
3260 return 0;
3261 status = nfsd4_init_slabs();
3262 if (status)
3263 return status;
3264 nfsd4_load_reboot_recovery_data();
3265 __nfs4_state_start();
3266 nfs4_init = 1;
3267 return 0;
3271 nfs4_in_grace(void)
3273 return in_grace;
3276 time_t
3277 nfs4_lease_time(void)
3279 return lease_time;
3282 static void
3283 __nfs4_state_shutdown(void)
3285 int i;
3286 struct nfs4_client *clp = NULL;
3287 struct nfs4_delegation *dp = NULL;
3288 struct nfs4_stateowner *sop = NULL;
3289 struct list_head *pos, *next, reaplist;
3291 list_for_each_safe(pos, next, &close_lru) {
3292 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
3293 list_del(&sop->so_close_lru);
3294 nfs4_put_stateowner(sop);
3297 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3298 while (!list_empty(&conf_id_hashtbl[i])) {
3299 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3300 expire_client(clp);
3302 while (!list_empty(&unconf_str_hashtbl[i])) {
3303 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3304 expire_client(clp);
3307 INIT_LIST_HEAD(&reaplist);
3308 spin_lock(&recall_lock);
3309 list_for_each_safe(pos, next, &del_recall_lru) {
3310 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3311 list_move(&dp->dl_recall_lru, &reaplist);
3313 spin_unlock(&recall_lock);
3314 list_for_each_safe(pos, next, &reaplist) {
3315 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3316 list_del_init(&dp->dl_recall_lru);
3317 unhash_delegation(dp);
3320 cancel_delayed_work(&laundromat_work);
3321 flush_workqueue(laundry_wq);
3322 destroy_workqueue(laundry_wq);
3323 nfsd4_shutdown_recdir();
3324 nfs4_init = 0;
3327 void
3328 nfs4_state_shutdown(void)
3330 nfs4_lock_state();
3331 nfs4_release_reclaim();
3332 __nfs4_state_shutdown();
3333 nfsd4_free_slabs();
3334 nfs4_unlock_state();
3337 static void
3338 nfs4_set_recdir(char *recdir)
3340 nfs4_lock_state();
3341 strcpy(user_recovery_dirname, recdir);
3342 nfs4_unlock_state();
3346 * Change the NFSv4 recovery directory to recdir.
3349 nfs4_reset_recoverydir(char *recdir)
3351 int status;
3352 struct nameidata nd;
3354 status = path_lookup(recdir, LOOKUP_FOLLOW, &nd);
3355 if (status)
3356 return status;
3357 status = -ENOTDIR;
3358 if (S_ISDIR(nd.dentry->d_inode->i_mode)) {
3359 nfs4_set_recdir(recdir);
3360 status = 0;
3362 path_release(&nd);
3363 return status;
3367 * Called when leasetime is changed.
3369 * The only way the protocol gives us to handle on-the-fly lease changes is to
3370 * simulate a reboot. Instead of doing that, we just wait till the next time
3371 * we start to register any changes in lease time. If the administrator
3372 * really wants to change the lease time *now*, they can go ahead and bring
3373 * nfsd down and then back up again after changing the lease time.
3375 void
3376 nfs4_reset_lease(time_t leasetime)
3378 lock_kernel();
3379 user_lease_time = leasetime;
3380 unlock_kernel();