IB/ipath: core device driver
[linux-2.6/zen-sources.git] / fs / nfsd / nfs4state.c
blob47ec112b266cf7a7c819babd881eb5176b7b4364
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>
52 #include <linux/mutex.h>
54 #define NFSDDBG_FACILITY NFSDDBG_PROC
56 /* Globals */
57 static time_t lease_time = 90; /* default lease time */
58 static time_t user_lease_time = 90;
59 static time_t boot_time;
60 static int in_grace = 1;
61 static u32 current_clientid = 1;
62 static u32 current_ownerid = 1;
63 static u32 current_fileid = 1;
64 static u32 current_delegid = 1;
65 static u32 nfs4_init;
66 static stateid_t zerostateid; /* bits all 0 */
67 static stateid_t onestateid; /* bits all 1 */
69 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
70 #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
72 /* forward declarations */
73 static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
74 static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
75 static void release_stateid_lockowners(struct nfs4_stateid *open_stp);
76 static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
77 static void nfs4_set_recdir(char *recdir);
79 /* Locking:
81 * client_mutex:
82 * protects clientid_hashtbl[], clientstr_hashtbl[],
83 * unconfstr_hashtbl[], uncofid_hashtbl[].
85 static DEFINE_MUTEX(client_mutex);
87 static kmem_cache_t *stateowner_slab = NULL;
88 static kmem_cache_t *file_slab = NULL;
89 static kmem_cache_t *stateid_slab = NULL;
90 static kmem_cache_t *deleg_slab = NULL;
92 void
93 nfs4_lock_state(void)
95 mutex_lock(&client_mutex);
98 void
99 nfs4_unlock_state(void)
101 mutex_unlock(&client_mutex);
104 static inline u32
105 opaque_hashval(const void *ptr, int nbytes)
107 unsigned char *cptr = (unsigned char *) ptr;
109 u32 x = 0;
110 while (nbytes--) {
111 x *= 37;
112 x += *cptr++;
114 return x;
117 /* forward declarations */
118 static void release_stateowner(struct nfs4_stateowner *sop);
119 static void release_stateid(struct nfs4_stateid *stp, int flags);
122 * Delegation state
125 /* recall_lock protects the del_recall_lru */
126 static spinlock_t recall_lock = SPIN_LOCK_UNLOCKED;
127 static struct list_head del_recall_lru;
129 static void
130 free_nfs4_file(struct kref *kref)
132 struct nfs4_file *fp = container_of(kref, struct nfs4_file, fi_ref);
133 list_del(&fp->fi_hash);
134 iput(fp->fi_inode);
135 kmem_cache_free(file_slab, fp);
138 static inline void
139 put_nfs4_file(struct nfs4_file *fi)
141 kref_put(&fi->fi_ref, free_nfs4_file);
144 static inline void
145 get_nfs4_file(struct nfs4_file *fi)
147 kref_get(&fi->fi_ref);
150 static struct nfs4_delegation *
151 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
153 struct nfs4_delegation *dp;
154 struct nfs4_file *fp = stp->st_file;
155 struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback;
157 dprintk("NFSD alloc_init_deleg\n");
158 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
159 if (dp == NULL)
160 return dp;
161 INIT_LIST_HEAD(&dp->dl_perfile);
162 INIT_LIST_HEAD(&dp->dl_perclnt);
163 INIT_LIST_HEAD(&dp->dl_recall_lru);
164 dp->dl_client = clp;
165 get_nfs4_file(fp);
166 dp->dl_file = fp;
167 dp->dl_flock = NULL;
168 get_file(stp->st_vfs_file);
169 dp->dl_vfs_file = stp->st_vfs_file;
170 dp->dl_type = type;
171 dp->dl_recall.cbr_dp = NULL;
172 dp->dl_recall.cbr_ident = cb->cb_ident;
173 dp->dl_recall.cbr_trunc = 0;
174 dp->dl_stateid.si_boot = boot_time;
175 dp->dl_stateid.si_stateownerid = current_delegid++;
176 dp->dl_stateid.si_fileid = 0;
177 dp->dl_stateid.si_generation = 0;
178 dp->dl_fhlen = current_fh->fh_handle.fh_size;
179 memcpy(dp->dl_fhval, &current_fh->fh_handle.fh_base,
180 current_fh->fh_handle.fh_size);
181 dp->dl_time = 0;
182 atomic_set(&dp->dl_count, 1);
183 list_add(&dp->dl_perfile, &fp->fi_delegations);
184 list_add(&dp->dl_perclnt, &clp->cl_delegations);
185 return dp;
188 void
189 nfs4_put_delegation(struct nfs4_delegation *dp)
191 if (atomic_dec_and_test(&dp->dl_count)) {
192 dprintk("NFSD: freeing dp %p\n",dp);
193 put_nfs4_file(dp->dl_file);
194 kmem_cache_free(deleg_slab, dp);
198 /* Remove the associated file_lock first, then remove the delegation.
199 * lease_modify() is called to remove the FS_LEASE file_lock from
200 * the i_flock list, eventually calling nfsd's lock_manager
201 * fl_release_callback.
203 static void
204 nfs4_close_delegation(struct nfs4_delegation *dp)
206 struct file *filp = dp->dl_vfs_file;
208 dprintk("NFSD: close_delegation dp %p\n",dp);
209 dp->dl_vfs_file = NULL;
210 /* The following nfsd_close may not actually close the file,
211 * but we want to remove the lease in any case. */
212 if (dp->dl_flock)
213 setlease(filp, F_UNLCK, &dp->dl_flock);
214 nfsd_close(filp);
217 /* Called under the state lock. */
218 static void
219 unhash_delegation(struct nfs4_delegation *dp)
221 list_del_init(&dp->dl_perfile);
222 list_del_init(&dp->dl_perclnt);
223 spin_lock(&recall_lock);
224 list_del_init(&dp->dl_recall_lru);
225 spin_unlock(&recall_lock);
226 nfs4_close_delegation(dp);
227 nfs4_put_delegation(dp);
231 * SETCLIENTID state
234 /* Hash tables for nfs4_clientid state */
235 #define CLIENT_HASH_BITS 4
236 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
237 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
239 #define clientid_hashval(id) \
240 ((id) & CLIENT_HASH_MASK)
241 #define clientstr_hashval(name) \
242 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
244 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
245 * used in reboot/reset lease grace period processing
247 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
248 * setclientid_confirmed info.
250 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
251 * setclientid info.
253 * client_lru holds client queue ordered by nfs4_client.cl_time
254 * for lease renewal.
256 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
257 * for last close replay.
259 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
260 static int reclaim_str_hashtbl_size = 0;
261 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
262 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
263 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
264 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
265 static struct list_head client_lru;
266 static struct list_head close_lru;
268 static inline void
269 renew_client(struct nfs4_client *clp)
272 * Move client to the end to the LRU list.
274 dprintk("renewing client (clientid %08x/%08x)\n",
275 clp->cl_clientid.cl_boot,
276 clp->cl_clientid.cl_id);
277 list_move_tail(&clp->cl_lru, &client_lru);
278 clp->cl_time = get_seconds();
281 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
282 static int
283 STALE_CLIENTID(clientid_t *clid)
285 if (clid->cl_boot == boot_time)
286 return 0;
287 dprintk("NFSD stale clientid (%08x/%08x)\n",
288 clid->cl_boot, clid->cl_id);
289 return 1;
293 * XXX Should we use a slab cache ?
294 * This type of memory management is somewhat inefficient, but we use it
295 * anyway since SETCLIENTID is not a common operation.
297 static inline struct nfs4_client *
298 alloc_client(struct xdr_netobj name)
300 struct nfs4_client *clp;
302 if ((clp = kmalloc(sizeof(struct nfs4_client), GFP_KERNEL))!= NULL) {
303 memset(clp, 0, sizeof(*clp));
304 if ((clp->cl_name.data = kmalloc(name.len, GFP_KERNEL)) != NULL) {
305 memcpy(clp->cl_name.data, name.data, name.len);
306 clp->cl_name.len = name.len;
308 else {
309 kfree(clp);
310 clp = NULL;
313 return clp;
316 static inline void
317 free_client(struct nfs4_client *clp)
319 if (clp->cl_cred.cr_group_info)
320 put_group_info(clp->cl_cred.cr_group_info);
321 kfree(clp->cl_name.data);
322 kfree(clp);
325 void
326 put_nfs4_client(struct nfs4_client *clp)
328 if (atomic_dec_and_test(&clp->cl_count))
329 free_client(clp);
332 static void
333 expire_client(struct nfs4_client *clp)
335 struct nfs4_stateowner *sop;
336 struct nfs4_delegation *dp;
337 struct nfs4_callback *cb = &clp->cl_callback;
338 struct rpc_clnt *clnt = clp->cl_callback.cb_client;
339 struct list_head reaplist;
341 dprintk("NFSD: expire_client cl_count %d\n",
342 atomic_read(&clp->cl_count));
344 /* shutdown rpc client, ending any outstanding recall rpcs */
345 if (atomic_read(&cb->cb_set) == 1 && clnt) {
346 rpc_shutdown_client(clnt);
347 clnt = clp->cl_callback.cb_client = NULL;
350 INIT_LIST_HEAD(&reaplist);
351 spin_lock(&recall_lock);
352 while (!list_empty(&clp->cl_delegations)) {
353 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
354 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
355 dp->dl_flock);
356 list_del_init(&dp->dl_perclnt);
357 list_move(&dp->dl_recall_lru, &reaplist);
359 spin_unlock(&recall_lock);
360 while (!list_empty(&reaplist)) {
361 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
362 list_del_init(&dp->dl_recall_lru);
363 unhash_delegation(dp);
365 list_del(&clp->cl_idhash);
366 list_del(&clp->cl_strhash);
367 list_del(&clp->cl_lru);
368 while (!list_empty(&clp->cl_openowners)) {
369 sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
370 release_stateowner(sop);
372 put_nfs4_client(clp);
375 static struct nfs4_client *
376 create_client(struct xdr_netobj name, char *recdir) {
377 struct nfs4_client *clp;
379 if (!(clp = alloc_client(name)))
380 goto out;
381 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
382 atomic_set(&clp->cl_count, 1);
383 atomic_set(&clp->cl_callback.cb_set, 0);
384 INIT_LIST_HEAD(&clp->cl_idhash);
385 INIT_LIST_HEAD(&clp->cl_strhash);
386 INIT_LIST_HEAD(&clp->cl_openowners);
387 INIT_LIST_HEAD(&clp->cl_delegations);
388 INIT_LIST_HEAD(&clp->cl_lru);
389 out:
390 return clp;
393 static void
394 copy_verf(struct nfs4_client *target, nfs4_verifier *source) {
395 memcpy(target->cl_verifier.data, source->data, sizeof(target->cl_verifier.data));
398 static void
399 copy_clid(struct nfs4_client *target, struct nfs4_client *source) {
400 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
401 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
404 static void
405 copy_cred(struct svc_cred *target, struct svc_cred *source) {
407 target->cr_uid = source->cr_uid;
408 target->cr_gid = source->cr_gid;
409 target->cr_group_info = source->cr_group_info;
410 get_group_info(target->cr_group_info);
413 static inline int
414 same_name(const char *n1, const char *n2) {
415 return 0 == memcmp(n1, n2, HEXDIR_LEN);
418 static int
419 cmp_verf(nfs4_verifier *v1, nfs4_verifier *v2) {
420 return(!memcmp(v1->data,v2->data,sizeof(v1->data)));
423 static int
424 cmp_clid(clientid_t * cl1, clientid_t * cl2) {
425 return((cl1->cl_boot == cl2->cl_boot) &&
426 (cl1->cl_id == cl2->cl_id));
429 /* XXX what about NGROUP */
430 static int
431 cmp_creds(struct svc_cred *cr1, struct svc_cred *cr2){
432 return(cr1->cr_uid == cr2->cr_uid);
436 static void
437 gen_clid(struct nfs4_client *clp) {
438 clp->cl_clientid.cl_boot = boot_time;
439 clp->cl_clientid.cl_id = current_clientid++;
442 static void
443 gen_confirm(struct nfs4_client *clp) {
444 struct timespec tv;
445 u32 * p;
447 tv = CURRENT_TIME;
448 p = (u32 *)clp->cl_confirm.data;
449 *p++ = tv.tv_sec;
450 *p++ = tv.tv_nsec;
453 static int
454 check_name(struct xdr_netobj name) {
456 if (name.len == 0)
457 return 0;
458 if (name.len > NFS4_OPAQUE_LIMIT) {
459 printk("NFSD: check_name: name too long(%d)!\n", name.len);
460 return 0;
462 return 1;
465 static void
466 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
468 unsigned int idhashval;
470 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
471 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
472 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
473 list_add_tail(&clp->cl_lru, &client_lru);
474 clp->cl_time = get_seconds();
477 static void
478 move_to_confirmed(struct nfs4_client *clp)
480 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
481 unsigned int strhashval;
483 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
484 list_del_init(&clp->cl_strhash);
485 list_del_init(&clp->cl_idhash);
486 list_add(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
487 strhashval = clientstr_hashval(clp->cl_recdir);
488 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
489 renew_client(clp);
492 static struct nfs4_client *
493 find_confirmed_client(clientid_t *clid)
495 struct nfs4_client *clp;
496 unsigned int idhashval = clientid_hashval(clid->cl_id);
498 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
499 if (cmp_clid(&clp->cl_clientid, clid))
500 return clp;
502 return NULL;
505 static struct nfs4_client *
506 find_unconfirmed_client(clientid_t *clid)
508 struct nfs4_client *clp;
509 unsigned int idhashval = clientid_hashval(clid->cl_id);
511 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
512 if (cmp_clid(&clp->cl_clientid, clid))
513 return clp;
515 return NULL;
518 static struct nfs4_client *
519 find_confirmed_client_by_str(const char *dname, unsigned int hashval)
521 struct nfs4_client *clp;
523 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
524 if (same_name(clp->cl_recdir, dname))
525 return clp;
527 return NULL;
530 static struct nfs4_client *
531 find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
533 struct nfs4_client *clp;
535 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
536 if (same_name(clp->cl_recdir, dname))
537 return clp;
539 return NULL;
542 /* a helper function for parse_callback */
543 static int
544 parse_octet(unsigned int *lenp, char **addrp)
546 unsigned int len = *lenp;
547 char *p = *addrp;
548 int n = -1;
549 char c;
551 for (;;) {
552 if (!len)
553 break;
554 len--;
555 c = *p++;
556 if (c == '.')
557 break;
558 if ((c < '0') || (c > '9')) {
559 n = -1;
560 break;
562 if (n < 0)
563 n = 0;
564 n = (n * 10) + (c - '0');
565 if (n > 255) {
566 n = -1;
567 break;
570 *lenp = len;
571 *addrp = p;
572 return n;
575 /* parse and set the setclientid ipv4 callback address */
576 static int
577 parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
579 int temp = 0;
580 u32 cbaddr = 0;
581 u16 cbport = 0;
582 u32 addrlen = addr_len;
583 char *addr = addr_val;
584 int i, shift;
586 /* ipaddress */
587 shift = 24;
588 for(i = 4; i > 0 ; i--) {
589 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
590 return 0;
592 cbaddr |= (temp << shift);
593 if (shift > 0)
594 shift -= 8;
596 *cbaddrp = cbaddr;
598 /* port */
599 shift = 8;
600 for(i = 2; i > 0 ; i--) {
601 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
602 return 0;
604 cbport |= (temp << shift);
605 if (shift > 0)
606 shift -= 8;
608 *cbportp = cbport;
609 return 1;
612 static void
613 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
615 struct nfs4_callback *cb = &clp->cl_callback;
617 /* Currently, we only support tcp for the callback channel */
618 if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
619 goto out_err;
621 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
622 &cb->cb_addr, &cb->cb_port)))
623 goto out_err;
624 cb->cb_prog = se->se_callback_prog;
625 cb->cb_ident = se->se_callback_ident;
626 return;
627 out_err:
628 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
629 "will not receive delegations\n",
630 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
632 return;
636 * RFC 3010 has a complex implmentation description of processing a
637 * SETCLIENTID request consisting of 5 bullets, labeled as
638 * CASE0 - CASE4 below.
640 * NOTES:
641 * callback information will be processed in a future patch
643 * an unconfirmed record is added when:
644 * NORMAL (part of CASE 4): there is no confirmed nor unconfirmed record.
645 * CASE 1: confirmed record found with matching name, principal,
646 * verifier, and clientid.
647 * CASE 2: confirmed record found with matching name, principal,
648 * and there is no unconfirmed record with matching
649 * name and principal
651 * an unconfirmed record is replaced when:
652 * CASE 3: confirmed record found with matching name, principal,
653 * and an unconfirmed record is found with matching
654 * name, principal, and with clientid and
655 * confirm that does not match the confirmed record.
656 * CASE 4: there is no confirmed record with matching name and
657 * principal. there is an unconfirmed record with
658 * matching name, principal.
660 * an unconfirmed record is deleted when:
661 * CASE 1: an unconfirmed record that matches input name, verifier,
662 * and confirmed clientid.
663 * CASE 4: any unconfirmed records with matching name and principal
664 * that exist after an unconfirmed record has been replaced
665 * as described above.
669 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_setclientid *setclid)
671 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
672 struct xdr_netobj clname = {
673 .len = setclid->se_namelen,
674 .data = setclid->se_name,
676 nfs4_verifier clverifier = setclid->se_verf;
677 unsigned int strhashval;
678 struct nfs4_client *conf, *unconf, *new;
679 int status;
680 char dname[HEXDIR_LEN];
682 if (!check_name(clname))
683 return nfserr_inval;
685 status = nfs4_make_rec_clidname(dname, &clname);
686 if (status)
687 return status;
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);
1163 stp->st_openstp = NULL;
1166 static void
1167 release_stateid(struct nfs4_stateid *stp, int flags)
1169 struct file *filp = stp->st_vfs_file;
1171 list_del(&stp->st_hash);
1172 list_del(&stp->st_perfile);
1173 list_del(&stp->st_perstateowner);
1174 if (flags & OPEN_STATE) {
1175 release_stateid_lockowners(stp);
1176 stp->st_vfs_file = NULL;
1177 nfsd_close(filp);
1178 } else if (flags & LOCK_STATE)
1179 locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
1180 put_nfs4_file(stp->st_file);
1181 kmem_cache_free(stateid_slab, stp);
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 int
1195 cmp_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, clientid_t *clid) {
1196 return ((sop->so_owner.len == owner->len) &&
1197 !memcmp(sop->so_owner.data, owner->data, owner->len) &&
1198 (sop->so_client->cl_clientid.cl_id == clid->cl_id));
1201 static struct nfs4_stateowner *
1202 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1204 struct nfs4_stateowner *so = NULL;
1206 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
1207 if (cmp_owner_str(so, &open->op_owner, &open->op_clientid))
1208 return so;
1210 return NULL;
1213 /* search file_hashtbl[] for file */
1214 static struct nfs4_file *
1215 find_file(struct inode *ino)
1217 unsigned int hashval = file_hashval(ino);
1218 struct nfs4_file *fp;
1220 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
1221 if (fp->fi_inode == ino) {
1222 get_nfs4_file(fp);
1223 return fp;
1226 return NULL;
1229 #define TEST_ACCESS(x) ((x > 0 || x < 4)?1:0)
1230 #define TEST_DENY(x) ((x >= 0 || x < 5)?1:0)
1232 static void
1233 set_access(unsigned int *access, unsigned long bmap) {
1234 int i;
1236 *access = 0;
1237 for (i = 1; i < 4; i++) {
1238 if (test_bit(i, &bmap))
1239 *access |= i;
1243 static void
1244 set_deny(unsigned int *deny, unsigned long bmap) {
1245 int i;
1247 *deny = 0;
1248 for (i = 0; i < 4; i++) {
1249 if (test_bit(i, &bmap))
1250 *deny |= i ;
1254 static int
1255 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1256 unsigned int access, deny;
1258 set_access(&access, stp->st_access_bmap);
1259 set_deny(&deny, stp->st_deny_bmap);
1260 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1261 return 0;
1262 return 1;
1266 * Called to check deny when READ with all zero stateid or
1267 * WRITE with all zero or all one stateid
1269 static int
1270 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1272 struct inode *ino = current_fh->fh_dentry->d_inode;
1273 struct nfs4_file *fp;
1274 struct nfs4_stateid *stp;
1275 int ret;
1277 dprintk("NFSD: nfs4_share_conflict\n");
1279 fp = find_file(ino);
1280 if (!fp)
1281 return nfs_ok;
1282 ret = nfserr_locked;
1283 /* Search for conflicting share reservations */
1284 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1285 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1286 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1287 goto out;
1289 ret = nfs_ok;
1290 out:
1291 put_nfs4_file(fp);
1292 return ret;
1295 static inline void
1296 nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1298 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1299 put_write_access(filp->f_dentry->d_inode);
1300 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1305 * Recall a delegation
1307 static int
1308 do_recall(void *__dp)
1310 struct nfs4_delegation *dp = __dp;
1312 daemonize("nfsv4-recall");
1314 nfsd4_cb_recall(dp);
1315 return 0;
1319 * Spawn a thread to perform a recall on the delegation represented
1320 * by the lease (file_lock)
1322 * Called from break_lease() with lock_kernel() held.
1323 * Note: we assume break_lease will only call this *once* for any given
1324 * lease.
1326 static
1327 void nfsd_break_deleg_cb(struct file_lock *fl)
1329 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1330 struct task_struct *t;
1332 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1333 if (!dp)
1334 return;
1336 /* We're assuming the state code never drops its reference
1337 * without first removing the lease. Since we're in this lease
1338 * callback (and since the lease code is serialized by the kernel
1339 * lock) we know the server hasn't removed the lease yet, we know
1340 * it's safe to take a reference: */
1341 atomic_inc(&dp->dl_count);
1343 spin_lock(&recall_lock);
1344 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1345 spin_unlock(&recall_lock);
1347 /* only place dl_time is set. protected by lock_kernel*/
1348 dp->dl_time = get_seconds();
1350 /* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */
1351 fl->fl_break_time = jiffies + NFSD_LEASE_TIME * HZ;
1353 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1354 if (IS_ERR(t)) {
1355 struct nfs4_client *clp = dp->dl_client;
1357 printk(KERN_INFO "NFSD: Callback thread failed for "
1358 "for client (clientid %08x/%08x)\n",
1359 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1360 nfs4_put_delegation(dp);
1365 * The file_lock is being reapd.
1367 * Called by locks_free_lock() with lock_kernel() held.
1369 static
1370 void nfsd_release_deleg_cb(struct file_lock *fl)
1372 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1374 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1376 if (!(fl->fl_flags & FL_LEASE) || !dp)
1377 return;
1378 dp->dl_flock = NULL;
1382 * Set the delegation file_lock back pointer.
1384 * Called from __setlease() with lock_kernel() held.
1386 static
1387 void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1389 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1391 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1392 if (!dp)
1393 return;
1394 dp->dl_flock = new;
1398 * Called from __setlease() with lock_kernel() held
1400 static
1401 int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1403 struct nfs4_delegation *onlistd =
1404 (struct nfs4_delegation *)onlist->fl_owner;
1405 struct nfs4_delegation *tryd =
1406 (struct nfs4_delegation *)try->fl_owner;
1408 if (onlist->fl_lmops != try->fl_lmops)
1409 return 0;
1411 return onlistd->dl_client == tryd->dl_client;
1415 static
1416 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1418 if (arg & F_UNLCK)
1419 return lease_modify(onlist, arg);
1420 else
1421 return -EAGAIN;
1424 static struct lock_manager_operations nfsd_lease_mng_ops = {
1425 .fl_break = nfsd_break_deleg_cb,
1426 .fl_release_private = nfsd_release_deleg_cb,
1427 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1428 .fl_mylease = nfsd_same_client_deleg_cb,
1429 .fl_change = nfsd_change_deleg_cb,
1434 nfsd4_process_open1(struct nfsd4_open *open)
1436 clientid_t *clientid = &open->op_clientid;
1437 struct nfs4_client *clp = NULL;
1438 unsigned int strhashval;
1439 struct nfs4_stateowner *sop = NULL;
1441 if (!check_name(open->op_owner))
1442 return nfserr_inval;
1444 if (STALE_CLIENTID(&open->op_clientid))
1445 return nfserr_stale_clientid;
1447 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1448 sop = find_openstateowner_str(strhashval, open);
1449 open->op_stateowner = sop;
1450 if (!sop) {
1451 /* Make sure the client's lease hasn't expired. */
1452 clp = find_confirmed_client(clientid);
1453 if (clp == NULL)
1454 return nfserr_expired;
1455 goto renew;
1457 if (!sop->so_confirmed) {
1458 /* Replace unconfirmed owners without checking for replay. */
1459 clp = sop->so_client;
1460 release_stateowner(sop);
1461 open->op_stateowner = NULL;
1462 goto renew;
1464 if (open->op_seqid == sop->so_seqid - 1) {
1465 if (sop->so_replay.rp_buflen)
1466 return NFSERR_REPLAY_ME;
1467 /* The original OPEN failed so spectacularly
1468 * that we don't even have replay data saved!
1469 * Therefore, we have no choice but to continue
1470 * processing this OPEN; presumably, we'll
1471 * fail again for the same reason.
1473 dprintk("nfsd4_process_open1: replay with no replay cache\n");
1474 goto renew;
1476 if (open->op_seqid != sop->so_seqid)
1477 return nfserr_bad_seqid;
1478 renew:
1479 if (open->op_stateowner == NULL) {
1480 sop = alloc_init_open_stateowner(strhashval, clp, open);
1481 if (sop == NULL)
1482 return nfserr_resource;
1483 open->op_stateowner = sop;
1485 list_del_init(&sop->so_close_lru);
1486 renew_client(sop->so_client);
1487 return nfs_ok;
1490 static inline int
1491 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1493 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1494 return nfserr_openmode;
1495 else
1496 return nfs_ok;
1499 static struct nfs4_delegation *
1500 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1502 struct nfs4_delegation *dp;
1504 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) {
1505 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1506 return dp;
1508 return NULL;
1511 static int
1512 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1513 struct nfs4_delegation **dp)
1515 int flags;
1516 int status = nfserr_bad_stateid;
1518 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1519 if (*dp == NULL)
1520 goto out;
1521 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1522 RD_STATE : WR_STATE;
1523 status = nfs4_check_delegmode(*dp, flags);
1524 if (status)
1525 *dp = NULL;
1526 out:
1527 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1528 return nfs_ok;
1529 if (status)
1530 return status;
1531 open->op_stateowner->so_confirmed = 1;
1532 return nfs_ok;
1535 static int
1536 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1538 struct nfs4_stateid *local;
1539 int status = nfserr_share_denied;
1540 struct nfs4_stateowner *sop = open->op_stateowner;
1542 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
1543 /* ignore lock owners */
1544 if (local->st_stateowner->so_is_open_owner == 0)
1545 continue;
1546 /* remember if we have seen this open owner */
1547 if (local->st_stateowner == sop)
1548 *stpp = local;
1549 /* check for conflicting share reservations */
1550 if (!test_share(local, open))
1551 goto out;
1553 status = 0;
1554 out:
1555 return status;
1558 static inline struct nfs4_stateid *
1559 nfs4_alloc_stateid(void)
1561 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
1564 static int
1565 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
1566 struct nfs4_delegation *dp,
1567 struct svc_fh *cur_fh, int flags)
1569 struct nfs4_stateid *stp;
1571 stp = nfs4_alloc_stateid();
1572 if (stp == NULL)
1573 return nfserr_resource;
1575 if (dp) {
1576 get_file(dp->dl_vfs_file);
1577 stp->st_vfs_file = dp->dl_vfs_file;
1578 } else {
1579 int status;
1580 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1581 &stp->st_vfs_file);
1582 if (status) {
1583 if (status == nfserr_dropit)
1584 status = nfserr_jukebox;
1585 kmem_cache_free(stateid_slab, stp);
1586 return status;
1589 *stpp = stp;
1590 return 0;
1593 static inline int
1594 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1595 struct nfsd4_open *open)
1597 struct iattr iattr = {
1598 .ia_valid = ATTR_SIZE,
1599 .ia_size = 0,
1601 if (!open->op_truncate)
1602 return 0;
1603 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
1604 return nfserr_inval;
1605 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1608 static int
1609 nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1611 struct file *filp = stp->st_vfs_file;
1612 struct inode *inode = filp->f_dentry->d_inode;
1613 unsigned int share_access, new_writer;
1614 int status;
1616 set_access(&share_access, stp->st_access_bmap);
1617 new_writer = (~share_access) & open->op_share_access
1618 & NFS4_SHARE_ACCESS_WRITE;
1620 if (new_writer) {
1621 status = get_write_access(inode);
1622 if (status)
1623 return nfserrno(status);
1625 status = nfsd4_truncate(rqstp, cur_fh, open);
1626 if (status) {
1627 if (new_writer)
1628 put_write_access(inode);
1629 return status;
1631 /* remember the open */
1632 filp->f_mode |= open->op_share_access;
1633 set_bit(open->op_share_access, &stp->st_access_bmap);
1634 set_bit(open->op_share_deny, &stp->st_deny_bmap);
1636 return nfs_ok;
1640 static void
1641 nfs4_set_claim_prev(struct nfsd4_open *open)
1643 open->op_stateowner->so_confirmed = 1;
1644 open->op_stateowner->so_client->cl_firststate = 1;
1648 * Attempt to hand out a delegation.
1650 static void
1651 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1653 struct nfs4_delegation *dp;
1654 struct nfs4_stateowner *sop = stp->st_stateowner;
1655 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1656 struct file_lock fl, *flp = &fl;
1657 int status, flag = 0;
1659 flag = NFS4_OPEN_DELEGATE_NONE;
1660 open->op_recall = 0;
1661 switch (open->op_claim_type) {
1662 case NFS4_OPEN_CLAIM_PREVIOUS:
1663 if (!atomic_read(&cb->cb_set))
1664 open->op_recall = 1;
1665 flag = open->op_delegate_type;
1666 if (flag == NFS4_OPEN_DELEGATE_NONE)
1667 goto out;
1668 break;
1669 case NFS4_OPEN_CLAIM_NULL:
1670 /* Let's not give out any delegations till everyone's
1671 * had the chance to reclaim theirs.... */
1672 if (nfs4_in_grace())
1673 goto out;
1674 if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
1675 goto out;
1676 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1677 flag = NFS4_OPEN_DELEGATE_WRITE;
1678 else
1679 flag = NFS4_OPEN_DELEGATE_READ;
1680 break;
1681 default:
1682 goto out;
1685 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1686 if (dp == NULL) {
1687 flag = NFS4_OPEN_DELEGATE_NONE;
1688 goto out;
1690 locks_init_lock(&fl);
1691 fl.fl_lmops = &nfsd_lease_mng_ops;
1692 fl.fl_flags = FL_LEASE;
1693 fl.fl_end = OFFSET_MAX;
1694 fl.fl_owner = (fl_owner_t)dp;
1695 fl.fl_file = stp->st_vfs_file;
1696 fl.fl_pid = current->tgid;
1698 /* setlease checks to see if delegation should be handed out.
1699 * the lock_manager callbacks fl_mylease and fl_change are used
1701 if ((status = setlease(stp->st_vfs_file,
1702 flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) {
1703 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
1704 unhash_delegation(dp);
1705 flag = NFS4_OPEN_DELEGATE_NONE;
1706 goto out;
1709 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1711 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1712 dp->dl_stateid.si_boot,
1713 dp->dl_stateid.si_stateownerid,
1714 dp->dl_stateid.si_fileid,
1715 dp->dl_stateid.si_generation);
1716 out:
1717 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
1718 && flag == NFS4_OPEN_DELEGATE_NONE
1719 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
1720 printk("NFSD: WARNING: refusing delegation reclaim\n");
1721 open->op_delegate_type = flag;
1725 * called with nfs4_lock_state() held.
1728 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1730 struct nfs4_file *fp = NULL;
1731 struct inode *ino = current_fh->fh_dentry->d_inode;
1732 struct nfs4_stateid *stp = NULL;
1733 struct nfs4_delegation *dp = NULL;
1734 int status;
1736 status = nfserr_inval;
1737 if (!TEST_ACCESS(open->op_share_access) || !TEST_DENY(open->op_share_deny))
1738 goto out;
1740 * Lookup file; if found, lookup stateid and check open request,
1741 * and check for delegations in the process of being recalled.
1742 * If not found, create the nfs4_file struct
1744 fp = find_file(ino);
1745 if (fp) {
1746 if ((status = nfs4_check_open(fp, open, &stp)))
1747 goto out;
1748 status = nfs4_check_deleg(fp, open, &dp);
1749 if (status)
1750 goto out;
1751 } else {
1752 status = nfserr_bad_stateid;
1753 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1754 goto out;
1755 status = nfserr_resource;
1756 fp = alloc_init_file(ino);
1757 if (fp == NULL)
1758 goto out;
1762 * OPEN the file, or upgrade an existing OPEN.
1763 * If truncate fails, the OPEN fails.
1765 if (stp) {
1766 /* Stateid was found, this is an OPEN upgrade */
1767 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1768 if (status)
1769 goto out;
1770 update_stateid(&stp->st_stateid);
1771 } else {
1772 /* Stateid was not found, this is a new OPEN */
1773 int flags = 0;
1774 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1775 flags = MAY_WRITE;
1776 else
1777 flags = MAY_READ;
1778 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1779 if (status)
1780 goto out;
1781 init_stateid(stp, fp, open);
1782 status = nfsd4_truncate(rqstp, current_fh, open);
1783 if (status) {
1784 release_stateid(stp, OPEN_STATE);
1785 goto out;
1788 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1791 * Attempt to hand out a delegation. No error return, because the
1792 * OPEN succeeds even if we fail.
1794 nfs4_open_delegation(current_fh, open, stp);
1796 status = nfs_ok;
1798 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1799 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1800 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1801 out:
1802 if (fp)
1803 put_nfs4_file(fp);
1804 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1805 nfs4_set_claim_prev(open);
1807 * To finish the open response, we just need to set the rflags.
1809 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1810 if (!open->op_stateowner->so_confirmed)
1811 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1813 return status;
1816 static struct workqueue_struct *laundry_wq;
1817 static struct work_struct laundromat_work;
1818 static void laundromat_main(void *);
1819 static DECLARE_WORK(laundromat_work, laundromat_main, NULL);
1821 int
1822 nfsd4_renew(clientid_t *clid)
1824 struct nfs4_client *clp;
1825 int status;
1827 nfs4_lock_state();
1828 dprintk("process_renew(%08x/%08x): starting\n",
1829 clid->cl_boot, clid->cl_id);
1830 status = nfserr_stale_clientid;
1831 if (STALE_CLIENTID(clid))
1832 goto out;
1833 clp = find_confirmed_client(clid);
1834 status = nfserr_expired;
1835 if (clp == NULL) {
1836 /* We assume the client took too long to RENEW. */
1837 dprintk("nfsd4_renew: clientid not found!\n");
1838 goto out;
1840 renew_client(clp);
1841 status = nfserr_cb_path_down;
1842 if (!list_empty(&clp->cl_delegations)
1843 && !atomic_read(&clp->cl_callback.cb_set))
1844 goto out;
1845 status = nfs_ok;
1846 out:
1847 nfs4_unlock_state();
1848 return status;
1851 static void
1852 end_grace(void)
1854 dprintk("NFSD: end of grace period\n");
1855 nfsd4_recdir_purge_old();
1856 in_grace = 0;
1859 static time_t
1860 nfs4_laundromat(void)
1862 struct nfs4_client *clp;
1863 struct nfs4_stateowner *sop;
1864 struct nfs4_delegation *dp;
1865 struct list_head *pos, *next, reaplist;
1866 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1867 time_t t, clientid_val = NFSD_LEASE_TIME;
1868 time_t u, test_val = NFSD_LEASE_TIME;
1870 nfs4_lock_state();
1872 dprintk("NFSD: laundromat service - starting\n");
1873 if (in_grace)
1874 end_grace();
1875 list_for_each_safe(pos, next, &client_lru) {
1876 clp = list_entry(pos, struct nfs4_client, cl_lru);
1877 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1878 t = clp->cl_time - cutoff;
1879 if (clientid_val > t)
1880 clientid_val = t;
1881 break;
1883 dprintk("NFSD: purging unused client (clientid %08x)\n",
1884 clp->cl_clientid.cl_id);
1885 nfsd4_remove_clid_dir(clp);
1886 expire_client(clp);
1888 INIT_LIST_HEAD(&reaplist);
1889 spin_lock(&recall_lock);
1890 list_for_each_safe(pos, next, &del_recall_lru) {
1891 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1892 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1893 u = dp->dl_time - cutoff;
1894 if (test_val > u)
1895 test_val = u;
1896 break;
1898 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1899 dp, dp->dl_flock);
1900 list_move(&dp->dl_recall_lru, &reaplist);
1902 spin_unlock(&recall_lock);
1903 list_for_each_safe(pos, next, &reaplist) {
1904 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1905 list_del_init(&dp->dl_recall_lru);
1906 unhash_delegation(dp);
1908 test_val = NFSD_LEASE_TIME;
1909 list_for_each_safe(pos, next, &close_lru) {
1910 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1911 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1912 u = sop->so_time - cutoff;
1913 if (test_val > u)
1914 test_val = u;
1915 break;
1917 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1918 sop->so_id);
1919 list_del(&sop->so_close_lru);
1920 nfs4_put_stateowner(sop);
1922 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1923 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1924 nfs4_unlock_state();
1925 return clientid_val;
1928 void
1929 laundromat_main(void *not_used)
1931 time_t t;
1933 t = nfs4_laundromat();
1934 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
1935 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
1938 static struct nfs4_stateowner *
1939 search_close_lru(u32 st_id, int flags)
1941 struct nfs4_stateowner *local = NULL;
1943 if (flags & CLOSE_STATE) {
1944 list_for_each_entry(local, &close_lru, so_close_lru) {
1945 if (local->so_id == st_id)
1946 return local;
1949 return NULL;
1952 static inline int
1953 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
1955 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_dentry->d_inode;
1958 static int
1959 STALE_STATEID(stateid_t *stateid)
1961 if (stateid->si_boot == boot_time)
1962 return 0;
1963 dprintk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
1964 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
1965 stateid->si_generation);
1966 return 1;
1969 static inline int
1970 access_permit_read(unsigned long access_bmap)
1972 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
1973 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
1974 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
1977 static inline int
1978 access_permit_write(unsigned long access_bmap)
1980 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
1981 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
1984 static
1985 int nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
1987 int status = nfserr_openmode;
1989 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
1990 goto out;
1991 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
1992 goto out;
1993 status = nfs_ok;
1994 out:
1995 return status;
1998 static inline int
1999 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2001 /* Trying to call delegreturn with a special stateid? Yuch: */
2002 if (!(flags & (RD_STATE | WR_STATE)))
2003 return nfserr_bad_stateid;
2004 else if (ONE_STATEID(stateid) && (flags & RD_STATE))
2005 return nfs_ok;
2006 else if (nfs4_in_grace()) {
2007 /* Answer in remaining cases depends on existance of
2008 * conflicting state; so we must wait out the grace period. */
2009 return nfserr_grace;
2010 } else if (flags & WR_STATE)
2011 return nfs4_share_conflict(current_fh,
2012 NFS4_SHARE_DENY_WRITE);
2013 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2014 return nfs4_share_conflict(current_fh,
2015 NFS4_SHARE_DENY_READ);
2019 * Allow READ/WRITE during grace period on recovered state only for files
2020 * that are not able to provide mandatory locking.
2022 static inline int
2023 io_during_grace_disallowed(struct inode *inode, int flags)
2025 return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE))
2026 && MANDATORY_LOCK(inode);
2030 * Checks for stateid operations
2033 nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2035 struct nfs4_stateid *stp = NULL;
2036 struct nfs4_delegation *dp = NULL;
2037 stateid_t *stidp;
2038 struct inode *ino = current_fh->fh_dentry->d_inode;
2039 int status;
2041 dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2042 stateid->si_boot, stateid->si_stateownerid,
2043 stateid->si_fileid, stateid->si_generation);
2044 if (filpp)
2045 *filpp = NULL;
2047 if (io_during_grace_disallowed(ino, flags))
2048 return nfserr_grace;
2050 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2051 return check_special_stateids(current_fh, stateid, flags);
2053 /* STALE STATEID */
2054 status = nfserr_stale_stateid;
2055 if (STALE_STATEID(stateid))
2056 goto out;
2058 /* BAD STATEID */
2059 status = nfserr_bad_stateid;
2060 if (!stateid->si_fileid) { /* delegation stateid */
2061 if(!(dp = find_delegation_stateid(ino, stateid))) {
2062 dprintk("NFSD: delegation stateid not found\n");
2063 if (nfs4_in_grace())
2064 status = nfserr_grace;
2065 goto out;
2067 stidp = &dp->dl_stateid;
2068 } else { /* open or lock stateid */
2069 if (!(stp = find_stateid(stateid, flags))) {
2070 dprintk("NFSD: open or lock stateid not found\n");
2071 if (nfs4_in_grace())
2072 status = nfserr_grace;
2073 goto out;
2075 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
2076 goto out;
2077 if (!stp->st_stateowner->so_confirmed)
2078 goto out;
2079 stidp = &stp->st_stateid;
2081 if (stateid->si_generation > stidp->si_generation)
2082 goto out;
2084 /* OLD STATEID */
2085 status = nfserr_old_stateid;
2086 if (stateid->si_generation < stidp->si_generation)
2087 goto out;
2088 if (stp) {
2089 if ((status = nfs4_check_openmode(stp,flags)))
2090 goto out;
2091 renew_client(stp->st_stateowner->so_client);
2092 if (filpp)
2093 *filpp = stp->st_vfs_file;
2094 } else if (dp) {
2095 if ((status = nfs4_check_delegmode(dp, flags)))
2096 goto out;
2097 renew_client(dp->dl_client);
2098 if (flags & DELEG_RET)
2099 unhash_delegation(dp);
2100 if (filpp)
2101 *filpp = dp->dl_vfs_file;
2103 status = nfs_ok;
2104 out:
2105 return status;
2108 static inline int
2109 setlkflg (int type)
2111 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
2112 RD_STATE : WR_STATE;
2116 * Checks for sequence id mutating operations.
2118 static int
2119 nfs4_preprocess_seqid_op(struct svc_fh *current_fh, u32 seqid, stateid_t *stateid, int flags, struct nfs4_stateowner **sopp, struct nfs4_stateid **stpp, struct nfsd4_lock *lock)
2121 struct nfs4_stateid *stp;
2122 struct nfs4_stateowner *sop;
2124 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2125 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2126 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2127 stateid->si_generation);
2129 *stpp = NULL;
2130 *sopp = NULL;
2132 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2133 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
2134 return nfserr_bad_stateid;
2137 if (STALE_STATEID(stateid))
2138 return nfserr_stale_stateid;
2140 * We return BAD_STATEID if filehandle doesn't match stateid,
2141 * the confirmed flag is incorrecly set, or the generation
2142 * number is incorrect.
2144 stp = find_stateid(stateid, flags);
2145 if (stp == NULL) {
2147 * Also, we should make sure this isn't just the result of
2148 * a replayed close:
2150 sop = search_close_lru(stateid->si_stateownerid, flags);
2151 if (sop == NULL)
2152 return nfserr_bad_stateid;
2153 *sopp = sop;
2154 goto check_replay;
2157 if (lock) {
2158 struct nfs4_stateowner *sop = stp->st_stateowner;
2159 clientid_t *lockclid = &lock->v.new.clientid;
2160 struct nfs4_client *clp = sop->so_client;
2161 int lkflg = 0;
2162 int status;
2164 lkflg = setlkflg(lock->lk_type);
2166 if (lock->lk_is_new) {
2167 if (!sop->so_is_open_owner)
2168 return nfserr_bad_stateid;
2169 if (!cmp_clid(&clp->cl_clientid, lockclid))
2170 return nfserr_bad_stateid;
2171 /* stp is the open stateid */
2172 status = nfs4_check_openmode(stp, lkflg);
2173 if (status)
2174 return status;
2175 } else {
2176 /* stp is the lock stateid */
2177 status = nfs4_check_openmode(stp->st_openstp, lkflg);
2178 if (status)
2179 return status;
2184 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
2185 printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
2186 return nfserr_bad_stateid;
2189 *stpp = stp;
2190 *sopp = sop = stp->st_stateowner;
2193 * We now validate the seqid and stateid generation numbers.
2194 * For the moment, we ignore the possibility of
2195 * generation number wraparound.
2197 if (seqid != sop->so_seqid)
2198 goto check_replay;
2200 if (sop->so_confirmed && flags & CONFIRM) {
2201 printk("NFSD: preprocess_seqid_op: expected"
2202 " unconfirmed stateowner!\n");
2203 return nfserr_bad_stateid;
2205 if (!sop->so_confirmed && !(flags & CONFIRM)) {
2206 printk("NFSD: preprocess_seqid_op: stateowner not"
2207 " confirmed yet!\n");
2208 return nfserr_bad_stateid;
2210 if (stateid->si_generation > stp->st_stateid.si_generation) {
2211 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
2212 return nfserr_bad_stateid;
2215 if (stateid->si_generation < stp->st_stateid.si_generation) {
2216 printk("NFSD: preprocess_seqid_op: old stateid!\n");
2217 return nfserr_old_stateid;
2219 renew_client(sop->so_client);
2220 return nfs_ok;
2222 check_replay:
2223 if (seqid == sop->so_seqid - 1) {
2224 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
2225 /* indicate replay to calling function */
2226 return NFSERR_REPLAY_ME;
2228 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
2229 sop->so_seqid, seqid);
2230 *sopp = NULL;
2231 return nfserr_bad_seqid;
2235 nfsd4_open_confirm(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_confirm *oc, struct nfs4_stateowner **replay_owner)
2237 int status;
2238 struct nfs4_stateowner *sop;
2239 struct nfs4_stateid *stp;
2241 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
2242 (int)current_fh->fh_dentry->d_name.len,
2243 current_fh->fh_dentry->d_name.name);
2245 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2246 goto out;
2248 nfs4_lock_state();
2250 if ((status = nfs4_preprocess_seqid_op(current_fh, oc->oc_seqid,
2251 &oc->oc_req_stateid,
2252 CHECK_FH | CONFIRM | OPEN_STATE,
2253 &oc->oc_stateowner, &stp, NULL)))
2254 goto out;
2256 sop = oc->oc_stateowner;
2257 sop->so_confirmed = 1;
2258 update_stateid(&stp->st_stateid);
2259 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2260 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2261 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2262 stp->st_stateid.si_boot,
2263 stp->st_stateid.si_stateownerid,
2264 stp->st_stateid.si_fileid,
2265 stp->st_stateid.si_generation);
2267 nfsd4_create_clid_dir(sop->so_client);
2268 out:
2269 if (oc->oc_stateowner) {
2270 nfs4_get_stateowner(oc->oc_stateowner);
2271 *replay_owner = oc->oc_stateowner;
2273 nfs4_unlock_state();
2274 return status;
2279 * unset all bits in union bitmap (bmap) that
2280 * do not exist in share (from successful OPEN_DOWNGRADE)
2282 static void
2283 reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2285 int i;
2286 for (i = 1; i < 4; i++) {
2287 if ((i & access) != i)
2288 __clear_bit(i, bmap);
2292 static void
2293 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2295 int i;
2296 for (i = 0; i < 4; i++) {
2297 if ((i & deny) != i)
2298 __clear_bit(i, bmap);
2303 nfsd4_open_downgrade(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_downgrade *od, struct nfs4_stateowner **replay_owner)
2305 int status;
2306 struct nfs4_stateid *stp;
2307 unsigned int share_access;
2309 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
2310 (int)current_fh->fh_dentry->d_name.len,
2311 current_fh->fh_dentry->d_name.name);
2313 if (!TEST_ACCESS(od->od_share_access) || !TEST_DENY(od->od_share_deny))
2314 return nfserr_inval;
2316 nfs4_lock_state();
2317 if ((status = nfs4_preprocess_seqid_op(current_fh, od->od_seqid,
2318 &od->od_stateid,
2319 CHECK_FH | OPEN_STATE,
2320 &od->od_stateowner, &stp, NULL)))
2321 goto out;
2323 status = nfserr_inval;
2324 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2325 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2326 stp->st_access_bmap, od->od_share_access);
2327 goto out;
2329 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2330 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2331 stp->st_deny_bmap, od->od_share_deny);
2332 goto out;
2334 set_access(&share_access, stp->st_access_bmap);
2335 nfs4_file_downgrade(stp->st_vfs_file,
2336 share_access & ~od->od_share_access);
2338 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2339 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2341 update_stateid(&stp->st_stateid);
2342 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2343 status = nfs_ok;
2344 out:
2345 if (od->od_stateowner) {
2346 nfs4_get_stateowner(od->od_stateowner);
2347 *replay_owner = od->od_stateowner;
2349 nfs4_unlock_state();
2350 return status;
2354 * nfs4_unlock_state() called after encode
2357 nfsd4_close(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_close *close, struct nfs4_stateowner **replay_owner)
2359 int status;
2360 struct nfs4_stateid *stp;
2362 dprintk("NFSD: nfsd4_close on file %.*s\n",
2363 (int)current_fh->fh_dentry->d_name.len,
2364 current_fh->fh_dentry->d_name.name);
2366 nfs4_lock_state();
2367 /* check close_lru for replay */
2368 if ((status = nfs4_preprocess_seqid_op(current_fh, close->cl_seqid,
2369 &close->cl_stateid,
2370 CHECK_FH | OPEN_STATE | CLOSE_STATE,
2371 &close->cl_stateowner, &stp, NULL)))
2372 goto out;
2373 status = nfs_ok;
2374 update_stateid(&stp->st_stateid);
2375 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2377 /* release_stateid() calls nfsd_close() if needed */
2378 release_stateid(stp, OPEN_STATE);
2380 /* place unused nfs4_stateowners on so_close_lru list to be
2381 * released by the laundromat service after the lease period
2382 * to enable us to handle CLOSE replay
2384 if (list_empty(&close->cl_stateowner->so_stateids))
2385 move_to_close_lru(close->cl_stateowner);
2386 out:
2387 if (close->cl_stateowner) {
2388 nfs4_get_stateowner(close->cl_stateowner);
2389 *replay_owner = close->cl_stateowner;
2391 nfs4_unlock_state();
2392 return status;
2396 nfsd4_delegreturn(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_delegreturn *dr)
2398 int status;
2400 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2401 goto out;
2403 nfs4_lock_state();
2404 status = nfs4_preprocess_stateid_op(current_fh, &dr->dr_stateid, DELEG_RET, NULL);
2405 nfs4_unlock_state();
2406 out:
2407 return status;
2412 * Lock owner state (byte-range locks)
2414 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2415 #define LOCK_HASH_BITS 8
2416 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2417 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2419 #define lockownerid_hashval(id) \
2420 ((id) & LOCK_HASH_MASK)
2422 static inline unsigned int
2423 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2424 struct xdr_netobj *ownername)
2426 return (file_hashval(inode) + cl_id
2427 + opaque_hashval(ownername->data, ownername->len))
2428 & LOCK_HASH_MASK;
2431 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2432 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2433 static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2435 static struct nfs4_stateid *
2436 find_stateid(stateid_t *stid, int flags)
2438 struct nfs4_stateid *local = NULL;
2439 u32 st_id = stid->si_stateownerid;
2440 u32 f_id = stid->si_fileid;
2441 unsigned int hashval;
2443 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
2444 if ((flags & LOCK_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2445 hashval = stateid_hashval(st_id, f_id);
2446 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2447 if ((local->st_stateid.si_stateownerid == st_id) &&
2448 (local->st_stateid.si_fileid == f_id))
2449 return local;
2452 if ((flags & OPEN_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2453 hashval = stateid_hashval(st_id, f_id);
2454 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2455 if ((local->st_stateid.si_stateownerid == st_id) &&
2456 (local->st_stateid.si_fileid == f_id))
2457 return local;
2460 return NULL;
2463 static struct nfs4_delegation *
2464 find_delegation_stateid(struct inode *ino, stateid_t *stid)
2466 struct nfs4_file *fp;
2467 struct nfs4_delegation *dl;
2469 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2470 stid->si_boot, stid->si_stateownerid,
2471 stid->si_fileid, stid->si_generation);
2473 fp = find_file(ino);
2474 if (!fp)
2475 return NULL;
2476 dl = find_delegation_file(fp, stid);
2477 put_nfs4_file(fp);
2478 return dl;
2482 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2483 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2484 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2485 * locking, this prevents us from being completely protocol-compliant. The
2486 * real solution to this problem is to start using unsigned file offsets in
2487 * the VFS, but this is a very deep change!
2489 static inline void
2490 nfs4_transform_lock_offset(struct file_lock *lock)
2492 if (lock->fl_start < 0)
2493 lock->fl_start = OFFSET_MAX;
2494 if (lock->fl_end < 0)
2495 lock->fl_end = OFFSET_MAX;
2498 static int
2499 nfs4_verify_lock_stateowner(struct nfs4_stateowner *sop, unsigned int hashval)
2501 struct nfs4_stateowner *local = NULL;
2502 int status = 0;
2504 if (hashval >= LOCK_HASH_SIZE)
2505 goto out;
2506 list_for_each_entry(local, &lock_ownerid_hashtbl[hashval], so_idhash) {
2507 if (local == sop) {
2508 status = 1;
2509 goto out;
2512 out:
2513 return status;
2517 static inline void
2518 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2520 struct nfs4_stateowner *sop = (struct nfs4_stateowner *) fl->fl_owner;
2521 unsigned int hval = lockownerid_hashval(sop->so_id);
2523 deny->ld_sop = NULL;
2524 if (nfs4_verify_lock_stateowner(sop, hval)) {
2525 kref_get(&sop->so_ref);
2526 deny->ld_sop = sop;
2527 deny->ld_clientid = sop->so_client->cl_clientid;
2529 deny->ld_start = fl->fl_start;
2530 deny->ld_length = ~(u64)0;
2531 if (fl->fl_end != ~(u64)0)
2532 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2533 deny->ld_type = NFS4_READ_LT;
2534 if (fl->fl_type != F_RDLCK)
2535 deny->ld_type = NFS4_WRITE_LT;
2538 static struct nfs4_stateowner *
2539 find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2540 struct xdr_netobj *owner)
2542 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2543 struct nfs4_stateowner *op;
2545 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
2546 if (cmp_owner_str(op, owner, clid))
2547 return op;
2549 return NULL;
2553 * Alloc a lock owner structure.
2554 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2555 * occured.
2557 * strhashval = lock_ownerstr_hashval
2560 static struct nfs4_stateowner *
2561 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2562 struct nfs4_stateowner *sop;
2563 struct nfs4_replay *rp;
2564 unsigned int idhashval;
2566 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2567 return NULL;
2568 idhashval = lockownerid_hashval(current_ownerid);
2569 INIT_LIST_HEAD(&sop->so_idhash);
2570 INIT_LIST_HEAD(&sop->so_strhash);
2571 INIT_LIST_HEAD(&sop->so_perclient);
2572 INIT_LIST_HEAD(&sop->so_stateids);
2573 INIT_LIST_HEAD(&sop->so_perstateid);
2574 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2575 sop->so_time = 0;
2576 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2577 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
2578 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
2579 sop->so_is_open_owner = 0;
2580 sop->so_id = current_ownerid++;
2581 sop->so_client = clp;
2582 /* It is the openowner seqid that will be incremented in encode in the
2583 * case of new lockowners; so increment the lock seqid manually: */
2584 sop->so_seqid = lock->lk_new_lock_seqid + 1;
2585 sop->so_confirmed = 1;
2586 rp = &sop->so_replay;
2587 rp->rp_status = nfserr_serverfault;
2588 rp->rp_buflen = 0;
2589 rp->rp_buf = rp->rp_ibuf;
2590 return sop;
2593 static struct nfs4_stateid *
2594 alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2596 struct nfs4_stateid *stp;
2597 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2599 stp = nfs4_alloc_stateid();
2600 if (stp == NULL)
2601 goto out;
2602 INIT_LIST_HEAD(&stp->st_hash);
2603 INIT_LIST_HEAD(&stp->st_perfile);
2604 INIT_LIST_HEAD(&stp->st_perstateowner);
2605 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
2606 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
2607 list_add(&stp->st_perfile, &fp->fi_stateids);
2608 list_add(&stp->st_perstateowner, &sop->so_stateids);
2609 stp->st_stateowner = sop;
2610 get_nfs4_file(fp);
2611 stp->st_file = fp;
2612 stp->st_stateid.si_boot = boot_time;
2613 stp->st_stateid.si_stateownerid = sop->so_id;
2614 stp->st_stateid.si_fileid = fp->fi_id;
2615 stp->st_stateid.si_generation = 0;
2616 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2617 stp->st_access_bmap = open_stp->st_access_bmap;
2618 stp->st_deny_bmap = open_stp->st_deny_bmap;
2619 stp->st_openstp = open_stp;
2621 out:
2622 return stp;
2625 static int
2626 check_lock_length(u64 offset, u64 length)
2628 return ((length == 0) || ((length != ~(u64)0) &&
2629 LOFF_OVERFLOW(offset, length)));
2633 * LOCK operation
2636 nfsd4_lock(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lock *lock, struct nfs4_stateowner **replay_owner)
2638 struct nfs4_stateowner *open_sop = NULL;
2639 struct nfs4_stateowner *lock_sop = NULL;
2640 struct nfs4_stateid *lock_stp;
2641 struct file *filp;
2642 struct file_lock file_lock;
2643 struct file_lock conflock;
2644 int status = 0;
2645 unsigned int strhashval;
2647 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2648 (long long) lock->lk_offset,
2649 (long long) lock->lk_length);
2651 if (check_lock_length(lock->lk_offset, lock->lk_length))
2652 return nfserr_inval;
2654 if ((status = fh_verify(rqstp, current_fh, S_IFREG, MAY_LOCK))) {
2655 dprintk("NFSD: nfsd4_lock: permission denied!\n");
2656 return status;
2659 nfs4_lock_state();
2661 if (lock->lk_is_new) {
2663 * Client indicates that this is a new lockowner.
2664 * Use open owner and open stateid to create lock owner and
2665 * lock stateid.
2667 struct nfs4_stateid *open_stp = NULL;
2668 struct nfs4_file *fp;
2670 status = nfserr_stale_clientid;
2671 if (STALE_CLIENTID(&lock->lk_new_clientid))
2672 goto out;
2674 /* validate and update open stateid and open seqid */
2675 status = nfs4_preprocess_seqid_op(current_fh,
2676 lock->lk_new_open_seqid,
2677 &lock->lk_new_open_stateid,
2678 CHECK_FH | OPEN_STATE,
2679 &lock->lk_replay_owner, &open_stp,
2680 lock);
2681 if (status)
2682 goto out;
2683 open_sop = lock->lk_replay_owner;
2684 /* create lockowner and lock stateid */
2685 fp = open_stp->st_file;
2686 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2687 open_sop->so_client->cl_clientid.cl_id,
2688 &lock->v.new.owner);
2689 /* XXX: Do we need to check for duplicate stateowners on
2690 * the same file, or should they just be allowed (and
2691 * create new stateids)? */
2692 status = nfserr_resource;
2693 lock_sop = alloc_init_lock_stateowner(strhashval,
2694 open_sop->so_client, open_stp, lock);
2695 if (lock_sop == NULL)
2696 goto out;
2697 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
2698 if (lock_stp == NULL)
2699 goto out;
2700 } else {
2701 /* lock (lock owner + lock stateid) already exists */
2702 status = nfs4_preprocess_seqid_op(current_fh,
2703 lock->lk_old_lock_seqid,
2704 &lock->lk_old_lock_stateid,
2705 CHECK_FH | LOCK_STATE,
2706 &lock->lk_replay_owner, &lock_stp, lock);
2707 if (status)
2708 goto out;
2709 lock_sop = lock->lk_replay_owner;
2711 /* lock->lk_replay_owner and lock_stp have been created or found */
2712 filp = lock_stp->st_vfs_file;
2714 status = nfserr_grace;
2715 if (nfs4_in_grace() && !lock->lk_reclaim)
2716 goto out;
2717 status = nfserr_no_grace;
2718 if (!nfs4_in_grace() && lock->lk_reclaim)
2719 goto out;
2721 locks_init_lock(&file_lock);
2722 switch (lock->lk_type) {
2723 case NFS4_READ_LT:
2724 case NFS4_READW_LT:
2725 file_lock.fl_type = F_RDLCK;
2726 break;
2727 case NFS4_WRITE_LT:
2728 case NFS4_WRITEW_LT:
2729 file_lock.fl_type = F_WRLCK;
2730 break;
2731 default:
2732 status = nfserr_inval;
2733 goto out;
2735 file_lock.fl_owner = (fl_owner_t)lock_sop;
2736 file_lock.fl_pid = current->tgid;
2737 file_lock.fl_file = filp;
2738 file_lock.fl_flags = FL_POSIX;
2740 file_lock.fl_start = lock->lk_offset;
2741 if ((lock->lk_length == ~(u64)0) ||
2742 LOFF_OVERFLOW(lock->lk_offset, lock->lk_length))
2743 file_lock.fl_end = ~(u64)0;
2744 else
2745 file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
2746 nfs4_transform_lock_offset(&file_lock);
2749 * Try to lock the file in the VFS.
2750 * Note: locks.c uses the BKL to protect the inode's lock list.
2753 /* XXX?: Just to divert the locks_release_private at the start of
2754 * locks_copy_lock: */
2755 conflock.fl_ops = NULL;
2756 conflock.fl_lmops = NULL;
2757 status = posix_lock_file_conf(filp, &file_lock, &conflock);
2758 dprintk("NFSD: nfsd4_lock: posix_lock_file_conf status %d\n",status);
2759 switch (-status) {
2760 case 0: /* success! */
2761 update_stateid(&lock_stp->st_stateid);
2762 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2763 sizeof(stateid_t));
2764 break;
2765 case (EAGAIN): /* conflock holds conflicting lock */
2766 status = nfserr_denied;
2767 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2768 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
2769 break;
2770 case (EDEADLK):
2771 status = nfserr_deadlock;
2772 break;
2773 default:
2774 dprintk("NFSD: nfsd4_lock: posix_lock_file_conf() failed! status %d\n",status);
2775 status = nfserr_resource;
2776 break;
2778 out:
2779 if (status && lock->lk_is_new && lock_sop)
2780 release_stateowner(lock_sop);
2781 if (lock->lk_replay_owner) {
2782 nfs4_get_stateowner(lock->lk_replay_owner);
2783 *replay_owner = lock->lk_replay_owner;
2785 nfs4_unlock_state();
2786 return status;
2790 * LOCKT operation
2793 nfsd4_lockt(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lockt *lockt)
2795 struct inode *inode;
2796 struct file file;
2797 struct file_lock file_lock;
2798 struct file_lock conflock;
2799 int status;
2801 if (nfs4_in_grace())
2802 return nfserr_grace;
2804 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2805 return nfserr_inval;
2807 lockt->lt_stateowner = NULL;
2808 nfs4_lock_state();
2810 status = nfserr_stale_clientid;
2811 if (STALE_CLIENTID(&lockt->lt_clientid))
2812 goto out;
2814 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0))) {
2815 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
2816 if (status == nfserr_symlink)
2817 status = nfserr_inval;
2818 goto out;
2821 inode = current_fh->fh_dentry->d_inode;
2822 locks_init_lock(&file_lock);
2823 switch (lockt->lt_type) {
2824 case NFS4_READ_LT:
2825 case NFS4_READW_LT:
2826 file_lock.fl_type = F_RDLCK;
2827 break;
2828 case NFS4_WRITE_LT:
2829 case NFS4_WRITEW_LT:
2830 file_lock.fl_type = F_WRLCK;
2831 break;
2832 default:
2833 printk("NFSD: nfs4_lockt: bad lock type!\n");
2834 status = nfserr_inval;
2835 goto out;
2838 lockt->lt_stateowner = find_lockstateowner_str(inode,
2839 &lockt->lt_clientid, &lockt->lt_owner);
2840 if (lockt->lt_stateowner)
2841 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2842 file_lock.fl_pid = current->tgid;
2843 file_lock.fl_flags = FL_POSIX;
2845 file_lock.fl_start = lockt->lt_offset;
2846 if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length))
2847 file_lock.fl_end = ~(u64)0;
2848 else
2849 file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
2851 nfs4_transform_lock_offset(&file_lock);
2853 /* posix_test_lock uses the struct file _only_ to resolve the inode.
2854 * since LOCKT doesn't require an OPEN, and therefore a struct
2855 * file may not exist, pass posix_test_lock a struct file with
2856 * only the dentry:inode set.
2858 memset(&file, 0, sizeof (struct file));
2859 file.f_dentry = current_fh->fh_dentry;
2861 status = nfs_ok;
2862 if (posix_test_lock(&file, &file_lock, &conflock)) {
2863 status = nfserr_denied;
2864 nfs4_set_lock_denied(&conflock, &lockt->lt_denied);
2866 out:
2867 nfs4_unlock_state();
2868 return status;
2872 nfsd4_locku(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_locku *locku, struct nfs4_stateowner **replay_owner)
2874 struct nfs4_stateid *stp;
2875 struct file *filp = NULL;
2876 struct file_lock file_lock;
2877 int status;
2879 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2880 (long long) locku->lu_offset,
2881 (long long) locku->lu_length);
2883 if (check_lock_length(locku->lu_offset, locku->lu_length))
2884 return nfserr_inval;
2886 nfs4_lock_state();
2888 if ((status = nfs4_preprocess_seqid_op(current_fh,
2889 locku->lu_seqid,
2890 &locku->lu_stateid,
2891 CHECK_FH | LOCK_STATE,
2892 &locku->lu_stateowner, &stp, NULL)))
2893 goto out;
2895 filp = stp->st_vfs_file;
2896 BUG_ON(!filp);
2897 locks_init_lock(&file_lock);
2898 file_lock.fl_type = F_UNLCK;
2899 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
2900 file_lock.fl_pid = current->tgid;
2901 file_lock.fl_file = filp;
2902 file_lock.fl_flags = FL_POSIX;
2903 file_lock.fl_start = locku->lu_offset;
2905 if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
2906 file_lock.fl_end = ~(u64)0;
2907 else
2908 file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
2909 nfs4_transform_lock_offset(&file_lock);
2912 * Try to unlock the file in the VFS.
2914 status = posix_lock_file(filp, &file_lock);
2915 if (status) {
2916 dprintk("NFSD: nfs4_locku: posix_lock_file failed!\n");
2917 goto out_nfserr;
2920 * OK, unlock succeeded; the only thing left to do is update the stateid.
2922 update_stateid(&stp->st_stateid);
2923 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
2925 out:
2926 if (locku->lu_stateowner) {
2927 nfs4_get_stateowner(locku->lu_stateowner);
2928 *replay_owner = locku->lu_stateowner;
2930 nfs4_unlock_state();
2931 return status;
2933 out_nfserr:
2934 status = nfserrno(status);
2935 goto out;
2939 * returns
2940 * 1: locks held by lockowner
2941 * 0: no locks held by lockowner
2943 static int
2944 check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
2946 struct file_lock **flpp;
2947 struct inode *inode = filp->f_dentry->d_inode;
2948 int status = 0;
2950 lock_kernel();
2951 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
2952 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
2953 status = 1;
2954 goto out;
2957 out:
2958 unlock_kernel();
2959 return status;
2963 nfsd4_release_lockowner(struct svc_rqst *rqstp, struct nfsd4_release_lockowner *rlockowner)
2965 clientid_t *clid = &rlockowner->rl_clientid;
2966 struct nfs4_stateowner *sop;
2967 struct nfs4_stateid *stp;
2968 struct xdr_netobj *owner = &rlockowner->rl_owner;
2969 struct list_head matches;
2970 int i;
2971 int status;
2973 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
2974 clid->cl_boot, clid->cl_id);
2976 /* XXX check for lease expiration */
2978 status = nfserr_stale_clientid;
2979 if (STALE_CLIENTID(clid))
2980 return status;
2982 nfs4_lock_state();
2984 status = nfserr_locks_held;
2985 /* XXX: we're doing a linear search through all the lockowners.
2986 * Yipes! For now we'll just hope clients aren't really using
2987 * release_lockowner much, but eventually we have to fix these
2988 * data structures. */
2989 INIT_LIST_HEAD(&matches);
2990 for (i = 0; i < LOCK_HASH_SIZE; i++) {
2991 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
2992 if (!cmp_owner_str(sop, owner, clid))
2993 continue;
2994 list_for_each_entry(stp, &sop->so_stateids,
2995 st_perstateowner) {
2996 if (check_for_locks(stp->st_vfs_file, sop))
2997 goto out;
2998 /* Note: so_perclient unused for lockowners,
2999 * so it's OK to fool with here. */
3000 list_add(&sop->so_perclient, &matches);
3004 /* Clients probably won't expect us to return with some (but not all)
3005 * of the lockowner state released; so don't release any until all
3006 * have been checked. */
3007 status = nfs_ok;
3008 while (!list_empty(&matches)) {
3009 sop = list_entry(matches.next, struct nfs4_stateowner,
3010 so_perclient);
3011 /* unhash_stateowner deletes so_perclient only
3012 * for openowners. */
3013 list_del(&sop->so_perclient);
3014 release_stateowner(sop);
3016 out:
3017 nfs4_unlock_state();
3018 return status;
3021 static inline struct nfs4_client_reclaim *
3022 alloc_reclaim(void)
3024 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
3028 nfs4_has_reclaimed_state(const char *name)
3030 unsigned int strhashval = clientstr_hashval(name);
3031 struct nfs4_client *clp;
3033 clp = find_confirmed_client_by_str(name, strhashval);
3034 return clp ? 1 : 0;
3038 * failure => all reset bets are off, nfserr_no_grace...
3041 nfs4_client_to_reclaim(const char *name)
3043 unsigned int strhashval;
3044 struct nfs4_client_reclaim *crp = NULL;
3046 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
3047 crp = alloc_reclaim();
3048 if (!crp)
3049 return 0;
3050 strhashval = clientstr_hashval(name);
3051 INIT_LIST_HEAD(&crp->cr_strhash);
3052 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
3053 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
3054 reclaim_str_hashtbl_size++;
3055 return 1;
3058 static void
3059 nfs4_release_reclaim(void)
3061 struct nfs4_client_reclaim *crp = NULL;
3062 int i;
3064 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3065 while (!list_empty(&reclaim_str_hashtbl[i])) {
3066 crp = list_entry(reclaim_str_hashtbl[i].next,
3067 struct nfs4_client_reclaim, cr_strhash);
3068 list_del(&crp->cr_strhash);
3069 kfree(crp);
3070 reclaim_str_hashtbl_size--;
3073 BUG_ON(reclaim_str_hashtbl_size);
3077 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
3078 static struct nfs4_client_reclaim *
3079 nfs4_find_reclaim_client(clientid_t *clid)
3081 unsigned int strhashval;
3082 struct nfs4_client *clp;
3083 struct nfs4_client_reclaim *crp = NULL;
3086 /* find clientid in conf_id_hashtbl */
3087 clp = find_confirmed_client(clid);
3088 if (clp == NULL)
3089 return NULL;
3091 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3092 clp->cl_name.len, clp->cl_name.data,
3093 clp->cl_recdir);
3095 /* find clp->cl_name in reclaim_str_hashtbl */
3096 strhashval = clientstr_hashval(clp->cl_recdir);
3097 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
3098 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
3099 return crp;
3102 return NULL;
3106 * Called from OPEN. Look for clientid in reclaim list.
3109 nfs4_check_open_reclaim(clientid_t *clid)
3111 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
3114 /* initialization to perform at module load time: */
3116 void
3117 nfs4_state_init(void)
3119 int i;
3121 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3122 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3123 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3124 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3125 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3127 for (i = 0; i < FILE_HASH_SIZE; i++) {
3128 INIT_LIST_HEAD(&file_hashtbl[i]);
3130 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3131 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3132 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3134 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3135 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3136 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3138 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3139 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3140 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3142 memset(&onestateid, ~0, sizeof(stateid_t));
3143 INIT_LIST_HEAD(&close_lru);
3144 INIT_LIST_HEAD(&client_lru);
3145 INIT_LIST_HEAD(&del_recall_lru);
3146 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3147 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3148 reclaim_str_hashtbl_size = 0;
3151 static void
3152 nfsd4_load_reboot_recovery_data(void)
3154 int status;
3156 nfs4_lock_state();
3157 nfsd4_init_recdir(user_recovery_dirname);
3158 status = nfsd4_recdir_load();
3159 nfs4_unlock_state();
3160 if (status)
3161 printk("NFSD: Failure reading reboot recovery data\n");
3164 /* initialization to perform when the nfsd service is started: */
3166 static void
3167 __nfs4_state_start(void)
3169 time_t grace_time;
3171 boot_time = get_seconds();
3172 grace_time = max(user_lease_time, lease_time);
3173 lease_time = user_lease_time;
3174 in_grace = 1;
3175 printk("NFSD: starting %ld-second grace period\n", grace_time);
3176 laundry_wq = create_singlethread_workqueue("nfsd4");
3177 queue_delayed_work(laundry_wq, &laundromat_work, grace_time*HZ);
3181 nfs4_state_start(void)
3183 int status;
3185 if (nfs4_init)
3186 return 0;
3187 status = nfsd4_init_slabs();
3188 if (status)
3189 return status;
3190 nfsd4_load_reboot_recovery_data();
3191 __nfs4_state_start();
3192 nfs4_init = 1;
3193 return 0;
3197 nfs4_in_grace(void)
3199 return in_grace;
3202 time_t
3203 nfs4_lease_time(void)
3205 return lease_time;
3208 static void
3209 __nfs4_state_shutdown(void)
3211 int i;
3212 struct nfs4_client *clp = NULL;
3213 struct nfs4_delegation *dp = NULL;
3214 struct nfs4_stateowner *sop = NULL;
3215 struct list_head *pos, *next, reaplist;
3217 list_for_each_safe(pos, next, &close_lru) {
3218 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
3219 list_del(&sop->so_close_lru);
3220 nfs4_put_stateowner(sop);
3223 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3224 while (!list_empty(&conf_id_hashtbl[i])) {
3225 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3226 expire_client(clp);
3228 while (!list_empty(&unconf_str_hashtbl[i])) {
3229 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3230 expire_client(clp);
3233 INIT_LIST_HEAD(&reaplist);
3234 spin_lock(&recall_lock);
3235 list_for_each_safe(pos, next, &del_recall_lru) {
3236 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3237 list_move(&dp->dl_recall_lru, &reaplist);
3239 spin_unlock(&recall_lock);
3240 list_for_each_safe(pos, next, &reaplist) {
3241 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3242 list_del_init(&dp->dl_recall_lru);
3243 unhash_delegation(dp);
3246 cancel_delayed_work(&laundromat_work);
3247 flush_workqueue(laundry_wq);
3248 destroy_workqueue(laundry_wq);
3249 nfsd4_shutdown_recdir();
3250 nfs4_init = 0;
3253 void
3254 nfs4_state_shutdown(void)
3256 nfs4_lock_state();
3257 nfs4_release_reclaim();
3258 __nfs4_state_shutdown();
3259 nfsd4_free_slabs();
3260 nfs4_unlock_state();
3263 static void
3264 nfs4_set_recdir(char *recdir)
3266 nfs4_lock_state();
3267 strcpy(user_recovery_dirname, recdir);
3268 nfs4_unlock_state();
3272 * Change the NFSv4 recovery directory to recdir.
3275 nfs4_reset_recoverydir(char *recdir)
3277 int status;
3278 struct nameidata nd;
3280 status = path_lookup(recdir, LOOKUP_FOLLOW, &nd);
3281 if (status)
3282 return status;
3283 status = -ENOTDIR;
3284 if (S_ISDIR(nd.dentry->d_inode->i_mode)) {
3285 nfs4_set_recdir(recdir);
3286 status = 0;
3288 path_release(&nd);
3289 return status;
3293 * Called when leasetime is changed.
3295 * The only way the protocol gives us to handle on-the-fly lease changes is to
3296 * simulate a reboot. Instead of doing that, we just wait till the next time
3297 * we start to register any changes in lease time. If the administrator
3298 * really wants to change the lease time *now*, they can go ahead and bring
3299 * nfsd down and then back up again after changing the lease time.
3301 void
3302 nfs4_reset_lease(time_t leasetime)
3304 lock_kernel();
3305 user_lease_time = leasetime;
3306 unlock_kernel();