[PATCH] myri10ge: define some previously hardwired firmware constants
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfsd / nfs4state.c
blob9daa0b9feb8d027e438ef39c846daee95cf2d6cf
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 DEFINE_SPINLOCK(recall_lock);
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 int num_delegations;
153 * Open owner state (share locks)
156 /* hash tables for nfs4_stateowner */
157 #define OWNER_HASH_BITS 8
158 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
159 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
161 #define ownerid_hashval(id) \
162 ((id) & OWNER_HASH_MASK)
163 #define ownerstr_hashval(clientid, ownername) \
164 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
166 static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
167 static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
169 /* hash table for nfs4_file */
170 #define FILE_HASH_BITS 8
171 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
172 #define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
173 /* hash table for (open)nfs4_stateid */
174 #define STATEID_HASH_BITS 10
175 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
176 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
178 #define file_hashval(x) \
179 hash_ptr(x, FILE_HASH_BITS)
180 #define stateid_hashval(owner_id, file_id) \
181 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
183 static struct list_head file_hashtbl[FILE_HASH_SIZE];
184 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
186 static struct nfs4_delegation *
187 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
189 struct nfs4_delegation *dp;
190 struct nfs4_file *fp = stp->st_file;
191 struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback;
193 dprintk("NFSD alloc_init_deleg\n");
194 if (num_delegations > STATEID_HASH_SIZE * 4)
195 return NULL;
196 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
197 if (dp == NULL)
198 return dp;
199 num_delegations++;
200 INIT_LIST_HEAD(&dp->dl_perfile);
201 INIT_LIST_HEAD(&dp->dl_perclnt);
202 INIT_LIST_HEAD(&dp->dl_recall_lru);
203 dp->dl_client = clp;
204 get_nfs4_file(fp);
205 dp->dl_file = fp;
206 dp->dl_flock = NULL;
207 get_file(stp->st_vfs_file);
208 dp->dl_vfs_file = stp->st_vfs_file;
209 dp->dl_type = type;
210 dp->dl_recall.cbr_dp = NULL;
211 dp->dl_recall.cbr_ident = cb->cb_ident;
212 dp->dl_recall.cbr_trunc = 0;
213 dp->dl_stateid.si_boot = boot_time;
214 dp->dl_stateid.si_stateownerid = current_delegid++;
215 dp->dl_stateid.si_fileid = 0;
216 dp->dl_stateid.si_generation = 0;
217 dp->dl_fhlen = current_fh->fh_handle.fh_size;
218 memcpy(dp->dl_fhval, &current_fh->fh_handle.fh_base,
219 current_fh->fh_handle.fh_size);
220 dp->dl_time = 0;
221 atomic_set(&dp->dl_count, 1);
222 list_add(&dp->dl_perfile, &fp->fi_delegations);
223 list_add(&dp->dl_perclnt, &clp->cl_delegations);
224 return dp;
227 void
228 nfs4_put_delegation(struct nfs4_delegation *dp)
230 if (atomic_dec_and_test(&dp->dl_count)) {
231 dprintk("NFSD: freeing dp %p\n",dp);
232 put_nfs4_file(dp->dl_file);
233 kmem_cache_free(deleg_slab, dp);
234 num_delegations--;
238 /* Remove the associated file_lock first, then remove the delegation.
239 * lease_modify() is called to remove the FS_LEASE file_lock from
240 * the i_flock list, eventually calling nfsd's lock_manager
241 * fl_release_callback.
243 static void
244 nfs4_close_delegation(struct nfs4_delegation *dp)
246 struct file *filp = dp->dl_vfs_file;
248 dprintk("NFSD: close_delegation dp %p\n",dp);
249 dp->dl_vfs_file = NULL;
250 /* The following nfsd_close may not actually close the file,
251 * but we want to remove the lease in any case. */
252 if (dp->dl_flock)
253 setlease(filp, F_UNLCK, &dp->dl_flock);
254 nfsd_close(filp);
257 /* Called under the state lock. */
258 static void
259 unhash_delegation(struct nfs4_delegation *dp)
261 list_del_init(&dp->dl_perfile);
262 list_del_init(&dp->dl_perclnt);
263 spin_lock(&recall_lock);
264 list_del_init(&dp->dl_recall_lru);
265 spin_unlock(&recall_lock);
266 nfs4_close_delegation(dp);
267 nfs4_put_delegation(dp);
271 * SETCLIENTID state
274 /* Hash tables for nfs4_clientid state */
275 #define CLIENT_HASH_BITS 4
276 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
277 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
279 #define clientid_hashval(id) \
280 ((id) & CLIENT_HASH_MASK)
281 #define clientstr_hashval(name) \
282 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
284 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
285 * used in reboot/reset lease grace period processing
287 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
288 * setclientid_confirmed info.
290 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
291 * setclientid info.
293 * client_lru holds client queue ordered by nfs4_client.cl_time
294 * for lease renewal.
296 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
297 * for last close replay.
299 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
300 static int reclaim_str_hashtbl_size = 0;
301 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
302 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
303 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
304 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
305 static struct list_head client_lru;
306 static struct list_head close_lru;
308 static inline void
309 renew_client(struct nfs4_client *clp)
312 * Move client to the end to the LRU list.
314 dprintk("renewing client (clientid %08x/%08x)\n",
315 clp->cl_clientid.cl_boot,
316 clp->cl_clientid.cl_id);
317 list_move_tail(&clp->cl_lru, &client_lru);
318 clp->cl_time = get_seconds();
321 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
322 static int
323 STALE_CLIENTID(clientid_t *clid)
325 if (clid->cl_boot == boot_time)
326 return 0;
327 dprintk("NFSD stale clientid (%08x/%08x)\n",
328 clid->cl_boot, clid->cl_id);
329 return 1;
333 * XXX Should we use a slab cache ?
334 * This type of memory management is somewhat inefficient, but we use it
335 * anyway since SETCLIENTID is not a common operation.
337 static inline struct nfs4_client *
338 alloc_client(struct xdr_netobj name)
340 struct nfs4_client *clp;
342 if ((clp = kmalloc(sizeof(struct nfs4_client), GFP_KERNEL))!= NULL) {
343 memset(clp, 0, sizeof(*clp));
344 if ((clp->cl_name.data = kmalloc(name.len, GFP_KERNEL)) != NULL) {
345 memcpy(clp->cl_name.data, name.data, name.len);
346 clp->cl_name.len = name.len;
348 else {
349 kfree(clp);
350 clp = NULL;
353 return clp;
356 static inline void
357 free_client(struct nfs4_client *clp)
359 if (clp->cl_cred.cr_group_info)
360 put_group_info(clp->cl_cred.cr_group_info);
361 kfree(clp->cl_name.data);
362 kfree(clp);
365 void
366 put_nfs4_client(struct nfs4_client *clp)
368 if (atomic_dec_and_test(&clp->cl_count))
369 free_client(clp);
372 static void
373 shutdown_callback_client(struct nfs4_client *clp)
375 struct rpc_clnt *clnt = clp->cl_callback.cb_client;
377 /* shutdown rpc client, ending any outstanding recall rpcs */
378 if (clnt) {
379 clp->cl_callback.cb_client = NULL;
380 rpc_shutdown_client(clnt);
381 rpciod_down();
385 static void
386 expire_client(struct nfs4_client *clp)
388 struct nfs4_stateowner *sop;
389 struct nfs4_delegation *dp;
390 struct list_head reaplist;
392 dprintk("NFSD: expire_client cl_count %d\n",
393 atomic_read(&clp->cl_count));
395 shutdown_callback_client(clp);
397 INIT_LIST_HEAD(&reaplist);
398 spin_lock(&recall_lock);
399 while (!list_empty(&clp->cl_delegations)) {
400 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
401 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
402 dp->dl_flock);
403 list_del_init(&dp->dl_perclnt);
404 list_move(&dp->dl_recall_lru, &reaplist);
406 spin_unlock(&recall_lock);
407 while (!list_empty(&reaplist)) {
408 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
409 list_del_init(&dp->dl_recall_lru);
410 unhash_delegation(dp);
412 list_del(&clp->cl_idhash);
413 list_del(&clp->cl_strhash);
414 list_del(&clp->cl_lru);
415 while (!list_empty(&clp->cl_openowners)) {
416 sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
417 release_stateowner(sop);
419 put_nfs4_client(clp);
422 static struct nfs4_client *
423 create_client(struct xdr_netobj name, char *recdir) {
424 struct nfs4_client *clp;
426 if (!(clp = alloc_client(name)))
427 goto out;
428 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
429 atomic_set(&clp->cl_count, 1);
430 atomic_set(&clp->cl_callback.cb_set, 0);
431 INIT_LIST_HEAD(&clp->cl_idhash);
432 INIT_LIST_HEAD(&clp->cl_strhash);
433 INIT_LIST_HEAD(&clp->cl_openowners);
434 INIT_LIST_HEAD(&clp->cl_delegations);
435 INIT_LIST_HEAD(&clp->cl_lru);
436 out:
437 return clp;
440 static void
441 copy_verf(struct nfs4_client *target, nfs4_verifier *source) {
442 memcpy(target->cl_verifier.data, source->data, sizeof(target->cl_verifier.data));
445 static void
446 copy_clid(struct nfs4_client *target, struct nfs4_client *source) {
447 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
448 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
451 static void
452 copy_cred(struct svc_cred *target, struct svc_cred *source) {
454 target->cr_uid = source->cr_uid;
455 target->cr_gid = source->cr_gid;
456 target->cr_group_info = source->cr_group_info;
457 get_group_info(target->cr_group_info);
460 static inline int
461 same_name(const char *n1, const char *n2) {
462 return 0 == memcmp(n1, n2, HEXDIR_LEN);
465 static int
466 cmp_verf(nfs4_verifier *v1, nfs4_verifier *v2) {
467 return(!memcmp(v1->data,v2->data,sizeof(v1->data)));
470 static int
471 cmp_clid(clientid_t * cl1, clientid_t * cl2) {
472 return((cl1->cl_boot == cl2->cl_boot) &&
473 (cl1->cl_id == cl2->cl_id));
476 /* XXX what about NGROUP */
477 static int
478 cmp_creds(struct svc_cred *cr1, struct svc_cred *cr2){
479 return(cr1->cr_uid == cr2->cr_uid);
483 static void
484 gen_clid(struct nfs4_client *clp) {
485 clp->cl_clientid.cl_boot = boot_time;
486 clp->cl_clientid.cl_id = current_clientid++;
489 static void
490 gen_confirm(struct nfs4_client *clp) {
491 struct timespec tv;
492 u32 * p;
494 tv = CURRENT_TIME;
495 p = (u32 *)clp->cl_confirm.data;
496 *p++ = tv.tv_sec;
497 *p++ = tv.tv_nsec;
500 static int
501 check_name(struct xdr_netobj name) {
503 if (name.len == 0)
504 return 0;
505 if (name.len > NFS4_OPAQUE_LIMIT) {
506 printk("NFSD: check_name: name too long(%d)!\n", name.len);
507 return 0;
509 return 1;
512 static void
513 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
515 unsigned int idhashval;
517 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
518 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
519 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
520 list_add_tail(&clp->cl_lru, &client_lru);
521 clp->cl_time = get_seconds();
524 static void
525 move_to_confirmed(struct nfs4_client *clp)
527 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
528 unsigned int strhashval;
530 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
531 list_del_init(&clp->cl_strhash);
532 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
533 strhashval = clientstr_hashval(clp->cl_recdir);
534 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
535 renew_client(clp);
538 static struct nfs4_client *
539 find_confirmed_client(clientid_t *clid)
541 struct nfs4_client *clp;
542 unsigned int idhashval = clientid_hashval(clid->cl_id);
544 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
545 if (cmp_clid(&clp->cl_clientid, clid))
546 return clp;
548 return NULL;
551 static struct nfs4_client *
552 find_unconfirmed_client(clientid_t *clid)
554 struct nfs4_client *clp;
555 unsigned int idhashval = clientid_hashval(clid->cl_id);
557 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
558 if (cmp_clid(&clp->cl_clientid, clid))
559 return clp;
561 return NULL;
564 static struct nfs4_client *
565 find_confirmed_client_by_str(const char *dname, unsigned int hashval)
567 struct nfs4_client *clp;
569 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
570 if (same_name(clp->cl_recdir, dname))
571 return clp;
573 return NULL;
576 static struct nfs4_client *
577 find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
579 struct nfs4_client *clp;
581 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
582 if (same_name(clp->cl_recdir, dname))
583 return clp;
585 return NULL;
588 /* a helper function for parse_callback */
589 static int
590 parse_octet(unsigned int *lenp, char **addrp)
592 unsigned int len = *lenp;
593 char *p = *addrp;
594 int n = -1;
595 char c;
597 for (;;) {
598 if (!len)
599 break;
600 len--;
601 c = *p++;
602 if (c == '.')
603 break;
604 if ((c < '0') || (c > '9')) {
605 n = -1;
606 break;
608 if (n < 0)
609 n = 0;
610 n = (n * 10) + (c - '0');
611 if (n > 255) {
612 n = -1;
613 break;
616 *lenp = len;
617 *addrp = p;
618 return n;
621 /* parse and set the setclientid ipv4 callback address */
622 static int
623 parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
625 int temp = 0;
626 u32 cbaddr = 0;
627 u16 cbport = 0;
628 u32 addrlen = addr_len;
629 char *addr = addr_val;
630 int i, shift;
632 /* ipaddress */
633 shift = 24;
634 for(i = 4; i > 0 ; i--) {
635 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
636 return 0;
638 cbaddr |= (temp << shift);
639 if (shift > 0)
640 shift -= 8;
642 *cbaddrp = cbaddr;
644 /* port */
645 shift = 8;
646 for(i = 2; i > 0 ; i--) {
647 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
648 return 0;
650 cbport |= (temp << shift);
651 if (shift > 0)
652 shift -= 8;
654 *cbportp = cbport;
655 return 1;
658 static void
659 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
661 struct nfs4_callback *cb = &clp->cl_callback;
663 /* Currently, we only support tcp for the callback channel */
664 if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
665 goto out_err;
667 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
668 &cb->cb_addr, &cb->cb_port)))
669 goto out_err;
670 cb->cb_prog = se->se_callback_prog;
671 cb->cb_ident = se->se_callback_ident;
672 return;
673 out_err:
674 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
675 "will not receive delegations\n",
676 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
678 return;
682 * RFC 3010 has a complex implmentation description of processing a
683 * SETCLIENTID request consisting of 5 bullets, labeled as
684 * CASE0 - CASE4 below.
686 * NOTES:
687 * callback information will be processed in a future patch
689 * an unconfirmed record is added when:
690 * NORMAL (part of CASE 4): there is no confirmed nor unconfirmed record.
691 * CASE 1: confirmed record found with matching name, principal,
692 * verifier, and clientid.
693 * CASE 2: confirmed record found with matching name, principal,
694 * and there is no unconfirmed record with matching
695 * name and principal
697 * an unconfirmed record is replaced when:
698 * CASE 3: confirmed record found with matching name, principal,
699 * and an unconfirmed record is found with matching
700 * name, principal, and with clientid and
701 * confirm that does not match the confirmed record.
702 * CASE 4: there is no confirmed record with matching name and
703 * principal. there is an unconfirmed record with
704 * matching name, principal.
706 * an unconfirmed record is deleted when:
707 * CASE 1: an unconfirmed record that matches input name, verifier,
708 * and confirmed clientid.
709 * CASE 4: any unconfirmed records with matching name and principal
710 * that exist after an unconfirmed record has been replaced
711 * as described above.
715 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_setclientid *setclid)
717 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
718 struct xdr_netobj clname = {
719 .len = setclid->se_namelen,
720 .data = setclid->se_name,
722 nfs4_verifier clverifier = setclid->se_verf;
723 unsigned int strhashval;
724 struct nfs4_client *conf, *unconf, *new;
725 int status;
726 char dname[HEXDIR_LEN];
728 if (!check_name(clname))
729 return nfserr_inval;
731 status = nfs4_make_rec_clidname(dname, &clname);
732 if (status)
733 return status;
736 * XXX The Duplicate Request Cache (DRC) has been checked (??)
737 * We get here on a DRC miss.
740 strhashval = clientstr_hashval(dname);
742 nfs4_lock_state();
743 conf = find_confirmed_client_by_str(dname, strhashval);
744 if (conf) {
746 * CASE 0:
747 * clname match, confirmed, different principal
748 * or different ip_address
750 status = nfserr_clid_inuse;
751 if (!cmp_creds(&conf->cl_cred, &rqstp->rq_cred)
752 || conf->cl_addr != ip_addr) {
753 printk("NFSD: setclientid: string in use by client"
754 "(clientid %08x/%08x)\n",
755 conf->cl_clientid.cl_boot, conf->cl_clientid.cl_id);
756 goto out;
759 unconf = find_unconfirmed_client_by_str(dname, strhashval);
760 status = nfserr_resource;
761 if (!conf) {
763 * CASE 4:
764 * placed first, because it is the normal case.
766 if (unconf)
767 expire_client(unconf);
768 new = create_client(clname, dname);
769 if (new == NULL)
770 goto out;
771 copy_verf(new, &clverifier);
772 new->cl_addr = ip_addr;
773 copy_cred(&new->cl_cred,&rqstp->rq_cred);
774 gen_clid(new);
775 gen_confirm(new);
776 gen_callback(new, setclid);
777 add_to_unconfirmed(new, strhashval);
778 } else if (cmp_verf(&conf->cl_verifier, &clverifier)) {
780 * CASE 1:
781 * cl_name match, confirmed, principal match
782 * verifier match: probable callback update
784 * remove any unconfirmed nfs4_client with
785 * matching cl_name, cl_verifier, and cl_clientid
787 * create and insert an unconfirmed nfs4_client with same
788 * cl_name, cl_verifier, and cl_clientid as existing
789 * nfs4_client, but with the new callback info and a
790 * new cl_confirm
792 if (unconf) {
793 /* Note this is removing unconfirmed {*x***},
794 * which is stronger than RFC recommended {vxc**}.
795 * This has the advantage that there is at most
796 * one {*x***} in either list at any time.
798 expire_client(unconf);
800 new = create_client(clname, dname);
801 if (new == NULL)
802 goto out;
803 copy_verf(new,&conf->cl_verifier);
804 new->cl_addr = ip_addr;
805 copy_cred(&new->cl_cred,&rqstp->rq_cred);
806 copy_clid(new, conf);
807 gen_confirm(new);
808 gen_callback(new, setclid);
809 add_to_unconfirmed(new,strhashval);
810 } else if (!unconf) {
812 * CASE 2:
813 * clname match, confirmed, principal match
814 * verfier does not match
815 * no unconfirmed. create a new unconfirmed nfs4_client
816 * using input clverifier, clname, and callback info
817 * and generate a new cl_clientid and cl_confirm.
819 new = create_client(clname, dname);
820 if (new == NULL)
821 goto out;
822 copy_verf(new,&clverifier);
823 new->cl_addr = ip_addr;
824 copy_cred(&new->cl_cred,&rqstp->rq_cred);
825 gen_clid(new);
826 gen_confirm(new);
827 gen_callback(new, setclid);
828 add_to_unconfirmed(new, strhashval);
829 } else if (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm)) {
831 * CASE3:
832 * confirmed found (name, principal match)
833 * confirmed verifier does not match input clverifier
835 * unconfirmed found (name match)
836 * confirmed->cl_confirm != unconfirmed->cl_confirm
838 * remove unconfirmed.
840 * create an unconfirmed nfs4_client
841 * with same cl_name as existing confirmed nfs4_client,
842 * but with new callback info, new cl_clientid,
843 * new cl_verifier and a new cl_confirm
845 expire_client(unconf);
846 new = create_client(clname, dname);
847 if (new == NULL)
848 goto out;
849 copy_verf(new,&clverifier);
850 new->cl_addr = ip_addr;
851 copy_cred(&new->cl_cred,&rqstp->rq_cred);
852 gen_clid(new);
853 gen_confirm(new);
854 gen_callback(new, setclid);
855 add_to_unconfirmed(new, strhashval);
856 } else {
857 /* No cases hit !!! */
858 status = nfserr_inval;
859 goto out;
862 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
863 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
864 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
865 status = nfs_ok;
866 out:
867 nfs4_unlock_state();
868 return status;
873 * RFC 3010 has a complex implmentation description of processing a
874 * SETCLIENTID_CONFIRM request consisting of 4 bullets describing
875 * processing on a DRC miss, labeled as CASE1 - CASE4 below.
877 * NOTE: callback information will be processed here in a future patch
880 nfsd4_setclientid_confirm(struct svc_rqst *rqstp, struct nfsd4_setclientid_confirm *setclientid_confirm)
882 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
883 struct nfs4_client *conf, *unconf;
884 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
885 clientid_t * clid = &setclientid_confirm->sc_clientid;
886 int status;
888 if (STALE_CLIENTID(clid))
889 return nfserr_stale_clientid;
891 * XXX The Duplicate Request Cache (DRC) has been checked (??)
892 * We get here on a DRC miss.
895 nfs4_lock_state();
897 conf = find_confirmed_client(clid);
898 unconf = find_unconfirmed_client(clid);
900 status = nfserr_clid_inuse;
901 if (conf && conf->cl_addr != ip_addr)
902 goto out;
903 if (unconf && unconf->cl_addr != ip_addr)
904 goto out;
906 if ((conf && unconf) &&
907 (cmp_verf(&unconf->cl_confirm, &confirm)) &&
908 (cmp_verf(&conf->cl_verifier, &unconf->cl_verifier)) &&
909 (same_name(conf->cl_recdir,unconf->cl_recdir)) &&
910 (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm))) {
911 /* CASE 1:
912 * unconf record that matches input clientid and input confirm.
913 * conf record that matches input clientid.
914 * conf and unconf records match names, verifiers
916 if (!cmp_creds(&conf->cl_cred, &unconf->cl_cred))
917 status = nfserr_clid_inuse;
918 else {
919 /* XXX: We just turn off callbacks until we can handle
920 * change request correctly. */
921 atomic_set(&conf->cl_callback.cb_set, 0);
922 gen_confirm(conf);
923 nfsd4_remove_clid_dir(unconf);
924 expire_client(unconf);
925 status = nfs_ok;
928 } else if ((conf && !unconf) ||
929 ((conf && unconf) &&
930 (!cmp_verf(&conf->cl_verifier, &unconf->cl_verifier) ||
931 !same_name(conf->cl_recdir, unconf->cl_recdir)))) {
932 /* CASE 2:
933 * conf record that matches input clientid.
934 * if unconf record matches input clientid, then
935 * unconf->cl_name or unconf->cl_verifier don't match the
936 * conf record.
938 if (!cmp_creds(&conf->cl_cred,&rqstp->rq_cred))
939 status = nfserr_clid_inuse;
940 else
941 status = nfs_ok;
942 } else if (!conf && unconf
943 && cmp_verf(&unconf->cl_confirm, &confirm)) {
944 /* CASE 3:
945 * conf record not found.
946 * unconf record found.
947 * unconf->cl_confirm matches input confirm
949 if (!cmp_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
950 status = nfserr_clid_inuse;
951 } else {
952 unsigned int hash =
953 clientstr_hashval(unconf->cl_recdir);
954 conf = find_confirmed_client_by_str(unconf->cl_recdir,
955 hash);
956 if (conf) {
957 nfsd4_remove_clid_dir(conf);
958 expire_client(conf);
960 move_to_confirmed(unconf);
961 conf = unconf;
962 status = nfs_ok;
964 } else if ((!conf || (conf && !cmp_verf(&conf->cl_confirm, &confirm)))
965 && (!unconf || (unconf && !cmp_verf(&unconf->cl_confirm,
966 &confirm)))) {
967 /* CASE 4:
968 * conf record not found, or if conf, conf->cl_confirm does not
969 * match input confirm.
970 * unconf record not found, or if unconf, unconf->cl_confirm
971 * does not match input confirm.
973 status = nfserr_stale_clientid;
974 } else {
975 /* check that we have hit one of the cases...*/
976 status = nfserr_clid_inuse;
978 out:
979 if (!status)
980 nfsd4_probe_callback(conf);
981 nfs4_unlock_state();
982 return status;
985 /* OPEN Share state helper functions */
986 static inline struct nfs4_file *
987 alloc_init_file(struct inode *ino)
989 struct nfs4_file *fp;
990 unsigned int hashval = file_hashval(ino);
992 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
993 if (fp) {
994 kref_init(&fp->fi_ref);
995 INIT_LIST_HEAD(&fp->fi_hash);
996 INIT_LIST_HEAD(&fp->fi_stateids);
997 INIT_LIST_HEAD(&fp->fi_delegations);
998 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
999 fp->fi_inode = igrab(ino);
1000 fp->fi_id = current_fileid++;
1001 return fp;
1003 return NULL;
1006 static void
1007 nfsd4_free_slab(kmem_cache_t **slab)
1009 int status;
1011 if (*slab == NULL)
1012 return;
1013 status = kmem_cache_destroy(*slab);
1014 *slab = NULL;
1015 WARN_ON(status);
1018 static void
1019 nfsd4_free_slabs(void)
1021 nfsd4_free_slab(&stateowner_slab);
1022 nfsd4_free_slab(&file_slab);
1023 nfsd4_free_slab(&stateid_slab);
1024 nfsd4_free_slab(&deleg_slab);
1027 static int
1028 nfsd4_init_slabs(void)
1030 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
1031 sizeof(struct nfs4_stateowner), 0, 0, NULL, NULL);
1032 if (stateowner_slab == NULL)
1033 goto out_nomem;
1034 file_slab = kmem_cache_create("nfsd4_files",
1035 sizeof(struct nfs4_file), 0, 0, NULL, NULL);
1036 if (file_slab == NULL)
1037 goto out_nomem;
1038 stateid_slab = kmem_cache_create("nfsd4_stateids",
1039 sizeof(struct nfs4_stateid), 0, 0, NULL, NULL);
1040 if (stateid_slab == NULL)
1041 goto out_nomem;
1042 deleg_slab = kmem_cache_create("nfsd4_delegations",
1043 sizeof(struct nfs4_delegation), 0, 0, NULL, NULL);
1044 if (deleg_slab == NULL)
1045 goto out_nomem;
1046 return 0;
1047 out_nomem:
1048 nfsd4_free_slabs();
1049 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1050 return -ENOMEM;
1053 void
1054 nfs4_free_stateowner(struct kref *kref)
1056 struct nfs4_stateowner *sop =
1057 container_of(kref, struct nfs4_stateowner, so_ref);
1058 kfree(sop->so_owner.data);
1059 kmem_cache_free(stateowner_slab, sop);
1062 static inline struct nfs4_stateowner *
1063 alloc_stateowner(struct xdr_netobj *owner)
1065 struct nfs4_stateowner *sop;
1067 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1068 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1069 memcpy(sop->so_owner.data, owner->data, owner->len);
1070 sop->so_owner.len = owner->len;
1071 kref_init(&sop->so_ref);
1072 return sop;
1074 kmem_cache_free(stateowner_slab, sop);
1076 return NULL;
1079 static struct nfs4_stateowner *
1080 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1081 struct nfs4_stateowner *sop;
1082 struct nfs4_replay *rp;
1083 unsigned int idhashval;
1085 if (!(sop = alloc_stateowner(&open->op_owner)))
1086 return NULL;
1087 idhashval = ownerid_hashval(current_ownerid);
1088 INIT_LIST_HEAD(&sop->so_idhash);
1089 INIT_LIST_HEAD(&sop->so_strhash);
1090 INIT_LIST_HEAD(&sop->so_perclient);
1091 INIT_LIST_HEAD(&sop->so_stateids);
1092 INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
1093 INIT_LIST_HEAD(&sop->so_close_lru);
1094 sop->so_time = 0;
1095 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1096 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
1097 list_add(&sop->so_perclient, &clp->cl_openowners);
1098 sop->so_is_open_owner = 1;
1099 sop->so_id = current_ownerid++;
1100 sop->so_client = clp;
1101 sop->so_seqid = open->op_seqid;
1102 sop->so_confirmed = 0;
1103 rp = &sop->so_replay;
1104 rp->rp_status = nfserr_serverfault;
1105 rp->rp_buflen = 0;
1106 rp->rp_buf = rp->rp_ibuf;
1107 return sop;
1110 static void
1111 release_stateid_lockowners(struct nfs4_stateid *open_stp)
1113 struct nfs4_stateowner *lock_sop;
1115 while (!list_empty(&open_stp->st_lockowners)) {
1116 lock_sop = list_entry(open_stp->st_lockowners.next,
1117 struct nfs4_stateowner, so_perstateid);
1118 /* list_del(&open_stp->st_lockowners); */
1119 BUG_ON(lock_sop->so_is_open_owner);
1120 release_stateowner(lock_sop);
1124 static void
1125 unhash_stateowner(struct nfs4_stateowner *sop)
1127 struct nfs4_stateid *stp;
1129 list_del(&sop->so_idhash);
1130 list_del(&sop->so_strhash);
1131 if (sop->so_is_open_owner)
1132 list_del(&sop->so_perclient);
1133 list_del(&sop->so_perstateid);
1134 while (!list_empty(&sop->so_stateids)) {
1135 stp = list_entry(sop->so_stateids.next,
1136 struct nfs4_stateid, st_perstateowner);
1137 if (sop->so_is_open_owner)
1138 release_stateid(stp, OPEN_STATE);
1139 else
1140 release_stateid(stp, LOCK_STATE);
1144 static void
1145 release_stateowner(struct nfs4_stateowner *sop)
1147 unhash_stateowner(sop);
1148 list_del(&sop->so_close_lru);
1149 nfs4_put_stateowner(sop);
1152 static inline void
1153 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1154 struct nfs4_stateowner *sop = open->op_stateowner;
1155 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1157 INIT_LIST_HEAD(&stp->st_hash);
1158 INIT_LIST_HEAD(&stp->st_perstateowner);
1159 INIT_LIST_HEAD(&stp->st_lockowners);
1160 INIT_LIST_HEAD(&stp->st_perfile);
1161 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
1162 list_add(&stp->st_perstateowner, &sop->so_stateids);
1163 list_add(&stp->st_perfile, &fp->fi_stateids);
1164 stp->st_stateowner = sop;
1165 get_nfs4_file(fp);
1166 stp->st_file = fp;
1167 stp->st_stateid.si_boot = boot_time;
1168 stp->st_stateid.si_stateownerid = sop->so_id;
1169 stp->st_stateid.si_fileid = fp->fi_id;
1170 stp->st_stateid.si_generation = 0;
1171 stp->st_access_bmap = 0;
1172 stp->st_deny_bmap = 0;
1173 __set_bit(open->op_share_access, &stp->st_access_bmap);
1174 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
1175 stp->st_openstp = NULL;
1178 static void
1179 release_stateid(struct nfs4_stateid *stp, int flags)
1181 struct file *filp = stp->st_vfs_file;
1183 list_del(&stp->st_hash);
1184 list_del(&stp->st_perfile);
1185 list_del(&stp->st_perstateowner);
1186 if (flags & OPEN_STATE) {
1187 release_stateid_lockowners(stp);
1188 stp->st_vfs_file = NULL;
1189 nfsd_close(filp);
1190 } else if (flags & LOCK_STATE)
1191 locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
1192 put_nfs4_file(stp->st_file);
1193 kmem_cache_free(stateid_slab, stp);
1196 static void
1197 move_to_close_lru(struct nfs4_stateowner *sop)
1199 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1201 list_move_tail(&sop->so_close_lru, &close_lru);
1202 sop->so_time = get_seconds();
1205 static int
1206 cmp_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, clientid_t *clid) {
1207 return ((sop->so_owner.len == owner->len) &&
1208 !memcmp(sop->so_owner.data, owner->data, owner->len) &&
1209 (sop->so_client->cl_clientid.cl_id == clid->cl_id));
1212 static struct nfs4_stateowner *
1213 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1215 struct nfs4_stateowner *so = NULL;
1217 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
1218 if (cmp_owner_str(so, &open->op_owner, &open->op_clientid))
1219 return so;
1221 return NULL;
1224 /* search file_hashtbl[] for file */
1225 static struct nfs4_file *
1226 find_file(struct inode *ino)
1228 unsigned int hashval = file_hashval(ino);
1229 struct nfs4_file *fp;
1231 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
1232 if (fp->fi_inode == ino) {
1233 get_nfs4_file(fp);
1234 return fp;
1237 return NULL;
1240 static int access_valid(u32 x)
1242 return (x > 0 && x < 4);
1245 static int deny_valid(u32 x)
1247 return (x >= 0 && x < 5);
1250 static void
1251 set_access(unsigned int *access, unsigned long bmap) {
1252 int i;
1254 *access = 0;
1255 for (i = 1; i < 4; i++) {
1256 if (test_bit(i, &bmap))
1257 *access |= i;
1261 static void
1262 set_deny(unsigned int *deny, unsigned long bmap) {
1263 int i;
1265 *deny = 0;
1266 for (i = 0; i < 4; i++) {
1267 if (test_bit(i, &bmap))
1268 *deny |= i ;
1272 static int
1273 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1274 unsigned int access, deny;
1276 set_access(&access, stp->st_access_bmap);
1277 set_deny(&deny, stp->st_deny_bmap);
1278 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1279 return 0;
1280 return 1;
1284 * Called to check deny when READ with all zero stateid or
1285 * WRITE with all zero or all one stateid
1287 static int
1288 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1290 struct inode *ino = current_fh->fh_dentry->d_inode;
1291 struct nfs4_file *fp;
1292 struct nfs4_stateid *stp;
1293 int ret;
1295 dprintk("NFSD: nfs4_share_conflict\n");
1297 fp = find_file(ino);
1298 if (!fp)
1299 return nfs_ok;
1300 ret = nfserr_locked;
1301 /* Search for conflicting share reservations */
1302 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1303 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1304 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1305 goto out;
1307 ret = nfs_ok;
1308 out:
1309 put_nfs4_file(fp);
1310 return ret;
1313 static inline void
1314 nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1316 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1317 put_write_access(filp->f_dentry->d_inode);
1318 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1323 * Recall a delegation
1325 static int
1326 do_recall(void *__dp)
1328 struct nfs4_delegation *dp = __dp;
1330 daemonize("nfsv4-recall");
1332 nfsd4_cb_recall(dp);
1333 return 0;
1337 * Spawn a thread to perform a recall on the delegation represented
1338 * by the lease (file_lock)
1340 * Called from break_lease() with lock_kernel() held.
1341 * Note: we assume break_lease will only call this *once* for any given
1342 * lease.
1344 static
1345 void nfsd_break_deleg_cb(struct file_lock *fl)
1347 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1348 struct task_struct *t;
1350 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1351 if (!dp)
1352 return;
1354 /* We're assuming the state code never drops its reference
1355 * without first removing the lease. Since we're in this lease
1356 * callback (and since the lease code is serialized by the kernel
1357 * lock) we know the server hasn't removed the lease yet, we know
1358 * it's safe to take a reference: */
1359 atomic_inc(&dp->dl_count);
1361 spin_lock(&recall_lock);
1362 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1363 spin_unlock(&recall_lock);
1365 /* only place dl_time is set. protected by lock_kernel*/
1366 dp->dl_time = get_seconds();
1368 /* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */
1369 fl->fl_break_time = jiffies + NFSD_LEASE_TIME * HZ;
1371 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1372 if (IS_ERR(t)) {
1373 struct nfs4_client *clp = dp->dl_client;
1375 printk(KERN_INFO "NFSD: Callback thread failed for "
1376 "for client (clientid %08x/%08x)\n",
1377 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1378 nfs4_put_delegation(dp);
1383 * The file_lock is being reapd.
1385 * Called by locks_free_lock() with lock_kernel() held.
1387 static
1388 void nfsd_release_deleg_cb(struct file_lock *fl)
1390 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1392 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1394 if (!(fl->fl_flags & FL_LEASE) || !dp)
1395 return;
1396 dp->dl_flock = NULL;
1400 * Set the delegation file_lock back pointer.
1402 * Called from __setlease() with lock_kernel() held.
1404 static
1405 void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1407 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1409 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1410 if (!dp)
1411 return;
1412 dp->dl_flock = new;
1416 * Called from __setlease() with lock_kernel() held
1418 static
1419 int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1421 struct nfs4_delegation *onlistd =
1422 (struct nfs4_delegation *)onlist->fl_owner;
1423 struct nfs4_delegation *tryd =
1424 (struct nfs4_delegation *)try->fl_owner;
1426 if (onlist->fl_lmops != try->fl_lmops)
1427 return 0;
1429 return onlistd->dl_client == tryd->dl_client;
1433 static
1434 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1436 if (arg & F_UNLCK)
1437 return lease_modify(onlist, arg);
1438 else
1439 return -EAGAIN;
1442 static struct lock_manager_operations nfsd_lease_mng_ops = {
1443 .fl_break = nfsd_break_deleg_cb,
1444 .fl_release_private = nfsd_release_deleg_cb,
1445 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1446 .fl_mylease = nfsd_same_client_deleg_cb,
1447 .fl_change = nfsd_change_deleg_cb,
1452 nfsd4_process_open1(struct nfsd4_open *open)
1454 clientid_t *clientid = &open->op_clientid;
1455 struct nfs4_client *clp = NULL;
1456 unsigned int strhashval;
1457 struct nfs4_stateowner *sop = NULL;
1459 if (!check_name(open->op_owner))
1460 return nfserr_inval;
1462 if (STALE_CLIENTID(&open->op_clientid))
1463 return nfserr_stale_clientid;
1465 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1466 sop = find_openstateowner_str(strhashval, open);
1467 open->op_stateowner = sop;
1468 if (!sop) {
1469 /* Make sure the client's lease hasn't expired. */
1470 clp = find_confirmed_client(clientid);
1471 if (clp == NULL)
1472 return nfserr_expired;
1473 goto renew;
1475 if (!sop->so_confirmed) {
1476 /* Replace unconfirmed owners without checking for replay. */
1477 clp = sop->so_client;
1478 release_stateowner(sop);
1479 open->op_stateowner = NULL;
1480 goto renew;
1482 if (open->op_seqid == sop->so_seqid - 1) {
1483 if (sop->so_replay.rp_buflen)
1484 return NFSERR_REPLAY_ME;
1485 /* The original OPEN failed so spectacularly
1486 * that we don't even have replay data saved!
1487 * Therefore, we have no choice but to continue
1488 * processing this OPEN; presumably, we'll
1489 * fail again for the same reason.
1491 dprintk("nfsd4_process_open1: replay with no replay cache\n");
1492 goto renew;
1494 if (open->op_seqid != sop->so_seqid)
1495 return nfserr_bad_seqid;
1496 renew:
1497 if (open->op_stateowner == NULL) {
1498 sop = alloc_init_open_stateowner(strhashval, clp, open);
1499 if (sop == NULL)
1500 return nfserr_resource;
1501 open->op_stateowner = sop;
1503 list_del_init(&sop->so_close_lru);
1504 renew_client(sop->so_client);
1505 return nfs_ok;
1508 static inline int
1509 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1511 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1512 return nfserr_openmode;
1513 else
1514 return nfs_ok;
1517 static struct nfs4_delegation *
1518 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1520 struct nfs4_delegation *dp;
1522 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) {
1523 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1524 return dp;
1526 return NULL;
1529 static int
1530 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1531 struct nfs4_delegation **dp)
1533 int flags;
1534 int status = nfserr_bad_stateid;
1536 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1537 if (*dp == NULL)
1538 goto out;
1539 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1540 RD_STATE : WR_STATE;
1541 status = nfs4_check_delegmode(*dp, flags);
1542 if (status)
1543 *dp = NULL;
1544 out:
1545 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1546 return nfs_ok;
1547 if (status)
1548 return status;
1549 open->op_stateowner->so_confirmed = 1;
1550 return nfs_ok;
1553 static int
1554 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1556 struct nfs4_stateid *local;
1557 int status = nfserr_share_denied;
1558 struct nfs4_stateowner *sop = open->op_stateowner;
1560 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
1561 /* ignore lock owners */
1562 if (local->st_stateowner->so_is_open_owner == 0)
1563 continue;
1564 /* remember if we have seen this open owner */
1565 if (local->st_stateowner == sop)
1566 *stpp = local;
1567 /* check for conflicting share reservations */
1568 if (!test_share(local, open))
1569 goto out;
1571 status = 0;
1572 out:
1573 return status;
1576 static inline struct nfs4_stateid *
1577 nfs4_alloc_stateid(void)
1579 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
1582 static int
1583 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
1584 struct nfs4_delegation *dp,
1585 struct svc_fh *cur_fh, int flags)
1587 struct nfs4_stateid *stp;
1589 stp = nfs4_alloc_stateid();
1590 if (stp == NULL)
1591 return nfserr_resource;
1593 if (dp) {
1594 get_file(dp->dl_vfs_file);
1595 stp->st_vfs_file = dp->dl_vfs_file;
1596 } else {
1597 int status;
1598 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1599 &stp->st_vfs_file);
1600 if (status) {
1601 if (status == nfserr_dropit)
1602 status = nfserr_jukebox;
1603 kmem_cache_free(stateid_slab, stp);
1604 return status;
1607 *stpp = stp;
1608 return 0;
1611 static inline int
1612 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1613 struct nfsd4_open *open)
1615 struct iattr iattr = {
1616 .ia_valid = ATTR_SIZE,
1617 .ia_size = 0,
1619 if (!open->op_truncate)
1620 return 0;
1621 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
1622 return nfserr_inval;
1623 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1626 static int
1627 nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1629 struct file *filp = stp->st_vfs_file;
1630 struct inode *inode = filp->f_dentry->d_inode;
1631 unsigned int share_access, new_writer;
1632 int status;
1634 set_access(&share_access, stp->st_access_bmap);
1635 new_writer = (~share_access) & open->op_share_access
1636 & NFS4_SHARE_ACCESS_WRITE;
1638 if (new_writer) {
1639 status = get_write_access(inode);
1640 if (status)
1641 return nfserrno(status);
1643 status = nfsd4_truncate(rqstp, cur_fh, open);
1644 if (status) {
1645 if (new_writer)
1646 put_write_access(inode);
1647 return status;
1649 /* remember the open */
1650 filp->f_mode |= open->op_share_access;
1651 set_bit(open->op_share_access, &stp->st_access_bmap);
1652 set_bit(open->op_share_deny, &stp->st_deny_bmap);
1654 return nfs_ok;
1658 static void
1659 nfs4_set_claim_prev(struct nfsd4_open *open)
1661 open->op_stateowner->so_confirmed = 1;
1662 open->op_stateowner->so_client->cl_firststate = 1;
1666 * Attempt to hand out a delegation.
1668 static void
1669 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1671 struct nfs4_delegation *dp;
1672 struct nfs4_stateowner *sop = stp->st_stateowner;
1673 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1674 struct file_lock fl, *flp = &fl;
1675 int status, flag = 0;
1677 flag = NFS4_OPEN_DELEGATE_NONE;
1678 open->op_recall = 0;
1679 switch (open->op_claim_type) {
1680 case NFS4_OPEN_CLAIM_PREVIOUS:
1681 if (!atomic_read(&cb->cb_set))
1682 open->op_recall = 1;
1683 flag = open->op_delegate_type;
1684 if (flag == NFS4_OPEN_DELEGATE_NONE)
1685 goto out;
1686 break;
1687 case NFS4_OPEN_CLAIM_NULL:
1688 /* Let's not give out any delegations till everyone's
1689 * had the chance to reclaim theirs.... */
1690 if (nfs4_in_grace())
1691 goto out;
1692 if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
1693 goto out;
1694 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1695 flag = NFS4_OPEN_DELEGATE_WRITE;
1696 else
1697 flag = NFS4_OPEN_DELEGATE_READ;
1698 break;
1699 default:
1700 goto out;
1703 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1704 if (dp == NULL) {
1705 flag = NFS4_OPEN_DELEGATE_NONE;
1706 goto out;
1708 locks_init_lock(&fl);
1709 fl.fl_lmops = &nfsd_lease_mng_ops;
1710 fl.fl_flags = FL_LEASE;
1711 fl.fl_end = OFFSET_MAX;
1712 fl.fl_owner = (fl_owner_t)dp;
1713 fl.fl_file = stp->st_vfs_file;
1714 fl.fl_pid = current->tgid;
1716 /* setlease checks to see if delegation should be handed out.
1717 * the lock_manager callbacks fl_mylease and fl_change are used
1719 if ((status = setlease(stp->st_vfs_file,
1720 flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) {
1721 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
1722 unhash_delegation(dp);
1723 flag = NFS4_OPEN_DELEGATE_NONE;
1724 goto out;
1727 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1729 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1730 dp->dl_stateid.si_boot,
1731 dp->dl_stateid.si_stateownerid,
1732 dp->dl_stateid.si_fileid,
1733 dp->dl_stateid.si_generation);
1734 out:
1735 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
1736 && flag == NFS4_OPEN_DELEGATE_NONE
1737 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
1738 printk("NFSD: WARNING: refusing delegation reclaim\n");
1739 open->op_delegate_type = flag;
1743 * called with nfs4_lock_state() held.
1746 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1748 struct nfs4_file *fp = NULL;
1749 struct inode *ino = current_fh->fh_dentry->d_inode;
1750 struct nfs4_stateid *stp = NULL;
1751 struct nfs4_delegation *dp = NULL;
1752 int status;
1754 status = nfserr_inval;
1755 if (!access_valid(open->op_share_access)
1756 || !deny_valid(open->op_share_deny))
1757 goto out;
1759 * Lookup file; if found, lookup stateid and check open request,
1760 * and check for delegations in the process of being recalled.
1761 * If not found, create the nfs4_file struct
1763 fp = find_file(ino);
1764 if (fp) {
1765 if ((status = nfs4_check_open(fp, open, &stp)))
1766 goto out;
1767 status = nfs4_check_deleg(fp, open, &dp);
1768 if (status)
1769 goto out;
1770 } else {
1771 status = nfserr_bad_stateid;
1772 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1773 goto out;
1774 status = nfserr_resource;
1775 fp = alloc_init_file(ino);
1776 if (fp == NULL)
1777 goto out;
1781 * OPEN the file, or upgrade an existing OPEN.
1782 * If truncate fails, the OPEN fails.
1784 if (stp) {
1785 /* Stateid was found, this is an OPEN upgrade */
1786 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1787 if (status)
1788 goto out;
1789 update_stateid(&stp->st_stateid);
1790 } else {
1791 /* Stateid was not found, this is a new OPEN */
1792 int flags = 0;
1793 if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
1794 flags |= MAY_READ;
1795 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1796 flags |= MAY_WRITE;
1797 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1798 if (status)
1799 goto out;
1800 init_stateid(stp, fp, open);
1801 status = nfsd4_truncate(rqstp, current_fh, open);
1802 if (status) {
1803 release_stateid(stp, OPEN_STATE);
1804 goto out;
1807 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1810 * Attempt to hand out a delegation. No error return, because the
1811 * OPEN succeeds even if we fail.
1813 nfs4_open_delegation(current_fh, open, stp);
1815 status = nfs_ok;
1817 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1818 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1819 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1820 out:
1821 if (fp)
1822 put_nfs4_file(fp);
1823 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1824 nfs4_set_claim_prev(open);
1826 * To finish the open response, we just need to set the rflags.
1828 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1829 if (!open->op_stateowner->so_confirmed)
1830 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1832 return status;
1835 static struct workqueue_struct *laundry_wq;
1836 static struct work_struct laundromat_work;
1837 static void laundromat_main(void *);
1838 static DECLARE_WORK(laundromat_work, laundromat_main, NULL);
1840 int
1841 nfsd4_renew(clientid_t *clid)
1843 struct nfs4_client *clp;
1844 int status;
1846 nfs4_lock_state();
1847 dprintk("process_renew(%08x/%08x): starting\n",
1848 clid->cl_boot, clid->cl_id);
1849 status = nfserr_stale_clientid;
1850 if (STALE_CLIENTID(clid))
1851 goto out;
1852 clp = find_confirmed_client(clid);
1853 status = nfserr_expired;
1854 if (clp == NULL) {
1855 /* We assume the client took too long to RENEW. */
1856 dprintk("nfsd4_renew: clientid not found!\n");
1857 goto out;
1859 renew_client(clp);
1860 status = nfserr_cb_path_down;
1861 if (!list_empty(&clp->cl_delegations)
1862 && !atomic_read(&clp->cl_callback.cb_set))
1863 goto out;
1864 status = nfs_ok;
1865 out:
1866 nfs4_unlock_state();
1867 return status;
1870 static void
1871 end_grace(void)
1873 dprintk("NFSD: end of grace period\n");
1874 nfsd4_recdir_purge_old();
1875 in_grace = 0;
1878 static time_t
1879 nfs4_laundromat(void)
1881 struct nfs4_client *clp;
1882 struct nfs4_stateowner *sop;
1883 struct nfs4_delegation *dp;
1884 struct list_head *pos, *next, reaplist;
1885 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1886 time_t t, clientid_val = NFSD_LEASE_TIME;
1887 time_t u, test_val = NFSD_LEASE_TIME;
1889 nfs4_lock_state();
1891 dprintk("NFSD: laundromat service - starting\n");
1892 if (in_grace)
1893 end_grace();
1894 list_for_each_safe(pos, next, &client_lru) {
1895 clp = list_entry(pos, struct nfs4_client, cl_lru);
1896 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1897 t = clp->cl_time - cutoff;
1898 if (clientid_val > t)
1899 clientid_val = t;
1900 break;
1902 dprintk("NFSD: purging unused client (clientid %08x)\n",
1903 clp->cl_clientid.cl_id);
1904 nfsd4_remove_clid_dir(clp);
1905 expire_client(clp);
1907 INIT_LIST_HEAD(&reaplist);
1908 spin_lock(&recall_lock);
1909 list_for_each_safe(pos, next, &del_recall_lru) {
1910 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1911 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1912 u = dp->dl_time - cutoff;
1913 if (test_val > u)
1914 test_val = u;
1915 break;
1917 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1918 dp, dp->dl_flock);
1919 list_move(&dp->dl_recall_lru, &reaplist);
1921 spin_unlock(&recall_lock);
1922 list_for_each_safe(pos, next, &reaplist) {
1923 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1924 list_del_init(&dp->dl_recall_lru);
1925 unhash_delegation(dp);
1927 test_val = NFSD_LEASE_TIME;
1928 list_for_each_safe(pos, next, &close_lru) {
1929 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1930 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1931 u = sop->so_time - cutoff;
1932 if (test_val > u)
1933 test_val = u;
1934 break;
1936 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1937 sop->so_id);
1938 release_stateowner(sop);
1940 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1941 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1942 nfs4_unlock_state();
1943 return clientid_val;
1946 void
1947 laundromat_main(void *not_used)
1949 time_t t;
1951 t = nfs4_laundromat();
1952 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
1953 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
1956 static struct nfs4_stateowner *
1957 search_close_lru(u32 st_id, int flags)
1959 struct nfs4_stateowner *local = NULL;
1961 if (flags & CLOSE_STATE) {
1962 list_for_each_entry(local, &close_lru, so_close_lru) {
1963 if (local->so_id == st_id)
1964 return local;
1967 return NULL;
1970 static inline int
1971 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
1973 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_dentry->d_inode;
1976 static int
1977 STALE_STATEID(stateid_t *stateid)
1979 if (stateid->si_boot == boot_time)
1980 return 0;
1981 dprintk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
1982 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
1983 stateid->si_generation);
1984 return 1;
1987 static inline int
1988 access_permit_read(unsigned long access_bmap)
1990 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
1991 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
1992 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
1995 static inline int
1996 access_permit_write(unsigned long access_bmap)
1998 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
1999 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
2002 static
2003 int nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
2005 int status = nfserr_openmode;
2007 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2008 goto out;
2009 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2010 goto out;
2011 status = nfs_ok;
2012 out:
2013 return status;
2016 static inline int
2017 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2019 /* Trying to call delegreturn with a special stateid? Yuch: */
2020 if (!(flags & (RD_STATE | WR_STATE)))
2021 return nfserr_bad_stateid;
2022 else if (ONE_STATEID(stateid) && (flags & RD_STATE))
2023 return nfs_ok;
2024 else if (nfs4_in_grace()) {
2025 /* Answer in remaining cases depends on existance of
2026 * conflicting state; so we must wait out the grace period. */
2027 return nfserr_grace;
2028 } else if (flags & WR_STATE)
2029 return nfs4_share_conflict(current_fh,
2030 NFS4_SHARE_DENY_WRITE);
2031 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2032 return nfs4_share_conflict(current_fh,
2033 NFS4_SHARE_DENY_READ);
2037 * Allow READ/WRITE during grace period on recovered state only for files
2038 * that are not able to provide mandatory locking.
2040 static inline int
2041 io_during_grace_disallowed(struct inode *inode, int flags)
2043 return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE))
2044 && MANDATORY_LOCK(inode);
2048 * Checks for stateid operations
2051 nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2053 struct nfs4_stateid *stp = NULL;
2054 struct nfs4_delegation *dp = NULL;
2055 stateid_t *stidp;
2056 struct inode *ino = current_fh->fh_dentry->d_inode;
2057 int status;
2059 dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2060 stateid->si_boot, stateid->si_stateownerid,
2061 stateid->si_fileid, stateid->si_generation);
2062 if (filpp)
2063 *filpp = NULL;
2065 if (io_during_grace_disallowed(ino, flags))
2066 return nfserr_grace;
2068 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2069 return check_special_stateids(current_fh, stateid, flags);
2071 /* STALE STATEID */
2072 status = nfserr_stale_stateid;
2073 if (STALE_STATEID(stateid))
2074 goto out;
2076 /* BAD STATEID */
2077 status = nfserr_bad_stateid;
2078 if (!stateid->si_fileid) { /* delegation stateid */
2079 if(!(dp = find_delegation_stateid(ino, stateid))) {
2080 dprintk("NFSD: delegation stateid not found\n");
2081 goto out;
2083 stidp = &dp->dl_stateid;
2084 } else { /* open or lock stateid */
2085 if (!(stp = find_stateid(stateid, flags))) {
2086 dprintk("NFSD: open or lock stateid not found\n");
2087 goto out;
2089 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
2090 goto out;
2091 if (!stp->st_stateowner->so_confirmed)
2092 goto out;
2093 stidp = &stp->st_stateid;
2095 if (stateid->si_generation > stidp->si_generation)
2096 goto out;
2098 /* OLD STATEID */
2099 status = nfserr_old_stateid;
2100 if (stateid->si_generation < stidp->si_generation)
2101 goto out;
2102 if (stp) {
2103 if ((status = nfs4_check_openmode(stp,flags)))
2104 goto out;
2105 renew_client(stp->st_stateowner->so_client);
2106 if (filpp)
2107 *filpp = stp->st_vfs_file;
2108 } else if (dp) {
2109 if ((status = nfs4_check_delegmode(dp, flags)))
2110 goto out;
2111 renew_client(dp->dl_client);
2112 if (flags & DELEG_RET)
2113 unhash_delegation(dp);
2114 if (filpp)
2115 *filpp = dp->dl_vfs_file;
2117 status = nfs_ok;
2118 out:
2119 return status;
2122 static inline int
2123 setlkflg (int type)
2125 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
2126 RD_STATE : WR_STATE;
2130 * Checks for sequence id mutating operations.
2132 static int
2133 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)
2135 struct nfs4_stateid *stp;
2136 struct nfs4_stateowner *sop;
2138 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2139 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2140 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2141 stateid->si_generation);
2143 *stpp = NULL;
2144 *sopp = NULL;
2146 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2147 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
2148 return nfserr_bad_stateid;
2151 if (STALE_STATEID(stateid))
2152 return nfserr_stale_stateid;
2154 * We return BAD_STATEID if filehandle doesn't match stateid,
2155 * the confirmed flag is incorrecly set, or the generation
2156 * number is incorrect.
2158 stp = find_stateid(stateid, flags);
2159 if (stp == NULL) {
2161 * Also, we should make sure this isn't just the result of
2162 * a replayed close:
2164 sop = search_close_lru(stateid->si_stateownerid, flags);
2165 if (sop == NULL)
2166 return nfserr_bad_stateid;
2167 *sopp = sop;
2168 goto check_replay;
2171 if (lock) {
2172 struct nfs4_stateowner *sop = stp->st_stateowner;
2173 clientid_t *lockclid = &lock->v.new.clientid;
2174 struct nfs4_client *clp = sop->so_client;
2175 int lkflg = 0;
2176 int status;
2178 lkflg = setlkflg(lock->lk_type);
2180 if (lock->lk_is_new) {
2181 if (!sop->so_is_open_owner)
2182 return nfserr_bad_stateid;
2183 if (!cmp_clid(&clp->cl_clientid, lockclid))
2184 return nfserr_bad_stateid;
2185 /* stp is the open stateid */
2186 status = nfs4_check_openmode(stp, lkflg);
2187 if (status)
2188 return status;
2189 } else {
2190 /* stp is the lock stateid */
2191 status = nfs4_check_openmode(stp->st_openstp, lkflg);
2192 if (status)
2193 return status;
2198 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
2199 printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
2200 return nfserr_bad_stateid;
2203 *stpp = stp;
2204 *sopp = sop = stp->st_stateowner;
2207 * We now validate the seqid and stateid generation numbers.
2208 * For the moment, we ignore the possibility of
2209 * generation number wraparound.
2211 if (seqid != sop->so_seqid)
2212 goto check_replay;
2214 if (sop->so_confirmed && flags & CONFIRM) {
2215 printk("NFSD: preprocess_seqid_op: expected"
2216 " unconfirmed stateowner!\n");
2217 return nfserr_bad_stateid;
2219 if (!sop->so_confirmed && !(flags & CONFIRM)) {
2220 printk("NFSD: preprocess_seqid_op: stateowner not"
2221 " confirmed yet!\n");
2222 return nfserr_bad_stateid;
2224 if (stateid->si_generation > stp->st_stateid.si_generation) {
2225 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
2226 return nfserr_bad_stateid;
2229 if (stateid->si_generation < stp->st_stateid.si_generation) {
2230 printk("NFSD: preprocess_seqid_op: old stateid!\n");
2231 return nfserr_old_stateid;
2233 renew_client(sop->so_client);
2234 return nfs_ok;
2236 check_replay:
2237 if (seqid == sop->so_seqid - 1) {
2238 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
2239 /* indicate replay to calling function */
2240 return NFSERR_REPLAY_ME;
2242 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
2243 sop->so_seqid, seqid);
2244 *sopp = NULL;
2245 return nfserr_bad_seqid;
2249 nfsd4_open_confirm(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_confirm *oc, struct nfs4_stateowner **replay_owner)
2251 int status;
2252 struct nfs4_stateowner *sop;
2253 struct nfs4_stateid *stp;
2255 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
2256 (int)current_fh->fh_dentry->d_name.len,
2257 current_fh->fh_dentry->d_name.name);
2259 status = fh_verify(rqstp, current_fh, S_IFREG, 0);
2260 if (status)
2261 return status;
2263 nfs4_lock_state();
2265 if ((status = nfs4_preprocess_seqid_op(current_fh, oc->oc_seqid,
2266 &oc->oc_req_stateid,
2267 CHECK_FH | CONFIRM | OPEN_STATE,
2268 &oc->oc_stateowner, &stp, NULL)))
2269 goto out;
2271 sop = oc->oc_stateowner;
2272 sop->so_confirmed = 1;
2273 update_stateid(&stp->st_stateid);
2274 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2275 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2276 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2277 stp->st_stateid.si_boot,
2278 stp->st_stateid.si_stateownerid,
2279 stp->st_stateid.si_fileid,
2280 stp->st_stateid.si_generation);
2282 nfsd4_create_clid_dir(sop->so_client);
2283 out:
2284 if (oc->oc_stateowner) {
2285 nfs4_get_stateowner(oc->oc_stateowner);
2286 *replay_owner = oc->oc_stateowner;
2288 nfs4_unlock_state();
2289 return status;
2294 * unset all bits in union bitmap (bmap) that
2295 * do not exist in share (from successful OPEN_DOWNGRADE)
2297 static void
2298 reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2300 int i;
2301 for (i = 1; i < 4; i++) {
2302 if ((i & access) != i)
2303 __clear_bit(i, bmap);
2307 static void
2308 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2310 int i;
2311 for (i = 0; i < 4; i++) {
2312 if ((i & deny) != i)
2313 __clear_bit(i, bmap);
2318 nfsd4_open_downgrade(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_downgrade *od, struct nfs4_stateowner **replay_owner)
2320 int status;
2321 struct nfs4_stateid *stp;
2322 unsigned int share_access;
2324 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
2325 (int)current_fh->fh_dentry->d_name.len,
2326 current_fh->fh_dentry->d_name.name);
2328 if (!access_valid(od->od_share_access)
2329 || !deny_valid(od->od_share_deny))
2330 return nfserr_inval;
2332 nfs4_lock_state();
2333 if ((status = nfs4_preprocess_seqid_op(current_fh, od->od_seqid,
2334 &od->od_stateid,
2335 CHECK_FH | OPEN_STATE,
2336 &od->od_stateowner, &stp, NULL)))
2337 goto out;
2339 status = nfserr_inval;
2340 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2341 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2342 stp->st_access_bmap, od->od_share_access);
2343 goto out;
2345 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2346 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2347 stp->st_deny_bmap, od->od_share_deny);
2348 goto out;
2350 set_access(&share_access, stp->st_access_bmap);
2351 nfs4_file_downgrade(stp->st_vfs_file,
2352 share_access & ~od->od_share_access);
2354 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2355 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2357 update_stateid(&stp->st_stateid);
2358 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2359 status = nfs_ok;
2360 out:
2361 if (od->od_stateowner) {
2362 nfs4_get_stateowner(od->od_stateowner);
2363 *replay_owner = od->od_stateowner;
2365 nfs4_unlock_state();
2366 return status;
2370 * nfs4_unlock_state() called after encode
2373 nfsd4_close(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_close *close, struct nfs4_stateowner **replay_owner)
2375 int status;
2376 struct nfs4_stateid *stp;
2378 dprintk("NFSD: nfsd4_close on file %.*s\n",
2379 (int)current_fh->fh_dentry->d_name.len,
2380 current_fh->fh_dentry->d_name.name);
2382 nfs4_lock_state();
2383 /* check close_lru for replay */
2384 if ((status = nfs4_preprocess_seqid_op(current_fh, close->cl_seqid,
2385 &close->cl_stateid,
2386 CHECK_FH | OPEN_STATE | CLOSE_STATE,
2387 &close->cl_stateowner, &stp, NULL)))
2388 goto out;
2389 status = nfs_ok;
2390 update_stateid(&stp->st_stateid);
2391 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2393 /* release_stateid() calls nfsd_close() if needed */
2394 release_stateid(stp, OPEN_STATE);
2396 /* place unused nfs4_stateowners on so_close_lru list to be
2397 * released by the laundromat service after the lease period
2398 * to enable us to handle CLOSE replay
2400 if (list_empty(&close->cl_stateowner->so_stateids))
2401 move_to_close_lru(close->cl_stateowner);
2402 out:
2403 if (close->cl_stateowner) {
2404 nfs4_get_stateowner(close->cl_stateowner);
2405 *replay_owner = close->cl_stateowner;
2407 nfs4_unlock_state();
2408 return status;
2412 nfsd4_delegreturn(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_delegreturn *dr)
2414 int status;
2416 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2417 goto out;
2419 nfs4_lock_state();
2420 status = nfs4_preprocess_stateid_op(current_fh, &dr->dr_stateid, DELEG_RET, NULL);
2421 nfs4_unlock_state();
2422 out:
2423 return status;
2428 * Lock owner state (byte-range locks)
2430 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2431 #define LOCK_HASH_BITS 8
2432 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2433 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2435 #define lockownerid_hashval(id) \
2436 ((id) & LOCK_HASH_MASK)
2438 static inline unsigned int
2439 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2440 struct xdr_netobj *ownername)
2442 return (file_hashval(inode) + cl_id
2443 + opaque_hashval(ownername->data, ownername->len))
2444 & LOCK_HASH_MASK;
2447 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2448 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2449 static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2451 static struct nfs4_stateid *
2452 find_stateid(stateid_t *stid, int flags)
2454 struct nfs4_stateid *local = NULL;
2455 u32 st_id = stid->si_stateownerid;
2456 u32 f_id = stid->si_fileid;
2457 unsigned int hashval;
2459 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
2460 if ((flags & LOCK_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2461 hashval = stateid_hashval(st_id, f_id);
2462 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2463 if ((local->st_stateid.si_stateownerid == st_id) &&
2464 (local->st_stateid.si_fileid == f_id))
2465 return local;
2468 if ((flags & OPEN_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2469 hashval = stateid_hashval(st_id, f_id);
2470 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2471 if ((local->st_stateid.si_stateownerid == st_id) &&
2472 (local->st_stateid.si_fileid == f_id))
2473 return local;
2476 return NULL;
2479 static struct nfs4_delegation *
2480 find_delegation_stateid(struct inode *ino, stateid_t *stid)
2482 struct nfs4_file *fp;
2483 struct nfs4_delegation *dl;
2485 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2486 stid->si_boot, stid->si_stateownerid,
2487 stid->si_fileid, stid->si_generation);
2489 fp = find_file(ino);
2490 if (!fp)
2491 return NULL;
2492 dl = find_delegation_file(fp, stid);
2493 put_nfs4_file(fp);
2494 return dl;
2498 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2499 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2500 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2501 * locking, this prevents us from being completely protocol-compliant. The
2502 * real solution to this problem is to start using unsigned file offsets in
2503 * the VFS, but this is a very deep change!
2505 static inline void
2506 nfs4_transform_lock_offset(struct file_lock *lock)
2508 if (lock->fl_start < 0)
2509 lock->fl_start = OFFSET_MAX;
2510 if (lock->fl_end < 0)
2511 lock->fl_end = OFFSET_MAX;
2514 /* Hack!: For now, we're defining this just so we can use a pointer to it
2515 * as a unique cookie to identify our (NFSv4's) posix locks. */
2516 static struct lock_manager_operations nfsd_posix_mng_ops = {
2519 static inline void
2520 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2522 struct nfs4_stateowner *sop;
2523 unsigned int hval;
2525 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
2526 sop = (struct nfs4_stateowner *) fl->fl_owner;
2527 hval = lockownerid_hashval(sop->so_id);
2528 kref_get(&sop->so_ref);
2529 deny->ld_sop = sop;
2530 deny->ld_clientid = sop->so_client->cl_clientid;
2531 } else {
2532 deny->ld_sop = NULL;
2533 deny->ld_clientid.cl_boot = 0;
2534 deny->ld_clientid.cl_id = 0;
2536 deny->ld_start = fl->fl_start;
2537 deny->ld_length = ~(u64)0;
2538 if (fl->fl_end != ~(u64)0)
2539 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2540 deny->ld_type = NFS4_READ_LT;
2541 if (fl->fl_type != F_RDLCK)
2542 deny->ld_type = NFS4_WRITE_LT;
2545 static struct nfs4_stateowner *
2546 find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2547 struct xdr_netobj *owner)
2549 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2550 struct nfs4_stateowner *op;
2552 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
2553 if (cmp_owner_str(op, owner, clid))
2554 return op;
2556 return NULL;
2560 * Alloc a lock owner structure.
2561 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2562 * occured.
2564 * strhashval = lock_ownerstr_hashval
2567 static struct nfs4_stateowner *
2568 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2569 struct nfs4_stateowner *sop;
2570 struct nfs4_replay *rp;
2571 unsigned int idhashval;
2573 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2574 return NULL;
2575 idhashval = lockownerid_hashval(current_ownerid);
2576 INIT_LIST_HEAD(&sop->so_idhash);
2577 INIT_LIST_HEAD(&sop->so_strhash);
2578 INIT_LIST_HEAD(&sop->so_perclient);
2579 INIT_LIST_HEAD(&sop->so_stateids);
2580 INIT_LIST_HEAD(&sop->so_perstateid);
2581 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2582 sop->so_time = 0;
2583 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2584 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
2585 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
2586 sop->so_is_open_owner = 0;
2587 sop->so_id = current_ownerid++;
2588 sop->so_client = clp;
2589 /* It is the openowner seqid that will be incremented in encode in the
2590 * case of new lockowners; so increment the lock seqid manually: */
2591 sop->so_seqid = lock->lk_new_lock_seqid + 1;
2592 sop->so_confirmed = 1;
2593 rp = &sop->so_replay;
2594 rp->rp_status = nfserr_serverfault;
2595 rp->rp_buflen = 0;
2596 rp->rp_buf = rp->rp_ibuf;
2597 return sop;
2600 static struct nfs4_stateid *
2601 alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2603 struct nfs4_stateid *stp;
2604 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2606 stp = nfs4_alloc_stateid();
2607 if (stp == NULL)
2608 goto out;
2609 INIT_LIST_HEAD(&stp->st_hash);
2610 INIT_LIST_HEAD(&stp->st_perfile);
2611 INIT_LIST_HEAD(&stp->st_perstateowner);
2612 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
2613 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
2614 list_add(&stp->st_perfile, &fp->fi_stateids);
2615 list_add(&stp->st_perstateowner, &sop->so_stateids);
2616 stp->st_stateowner = sop;
2617 get_nfs4_file(fp);
2618 stp->st_file = fp;
2619 stp->st_stateid.si_boot = boot_time;
2620 stp->st_stateid.si_stateownerid = sop->so_id;
2621 stp->st_stateid.si_fileid = fp->fi_id;
2622 stp->st_stateid.si_generation = 0;
2623 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2624 stp->st_access_bmap = open_stp->st_access_bmap;
2625 stp->st_deny_bmap = open_stp->st_deny_bmap;
2626 stp->st_openstp = open_stp;
2628 out:
2629 return stp;
2632 static int
2633 check_lock_length(u64 offset, u64 length)
2635 return ((length == 0) || ((length != ~(u64)0) &&
2636 LOFF_OVERFLOW(offset, length)));
2640 * LOCK operation
2643 nfsd4_lock(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lock *lock, struct nfs4_stateowner **replay_owner)
2645 struct nfs4_stateowner *open_sop = NULL;
2646 struct nfs4_stateowner *lock_sop = NULL;
2647 struct nfs4_stateid *lock_stp;
2648 struct file *filp;
2649 struct file_lock file_lock;
2650 struct file_lock conflock;
2651 int status = 0;
2652 unsigned int strhashval;
2654 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2655 (long long) lock->lk_offset,
2656 (long long) lock->lk_length);
2658 if (check_lock_length(lock->lk_offset, lock->lk_length))
2659 return nfserr_inval;
2661 if ((status = fh_verify(rqstp, current_fh, S_IFREG, MAY_LOCK))) {
2662 dprintk("NFSD: nfsd4_lock: permission denied!\n");
2663 return status;
2666 nfs4_lock_state();
2668 if (lock->lk_is_new) {
2670 * Client indicates that this is a new lockowner.
2671 * Use open owner and open stateid to create lock owner and
2672 * lock stateid.
2674 struct nfs4_stateid *open_stp = NULL;
2675 struct nfs4_file *fp;
2677 status = nfserr_stale_clientid;
2678 if (STALE_CLIENTID(&lock->lk_new_clientid))
2679 goto out;
2681 /* validate and update open stateid and open seqid */
2682 status = nfs4_preprocess_seqid_op(current_fh,
2683 lock->lk_new_open_seqid,
2684 &lock->lk_new_open_stateid,
2685 CHECK_FH | OPEN_STATE,
2686 &lock->lk_replay_owner, &open_stp,
2687 lock);
2688 if (status)
2689 goto out;
2690 open_sop = lock->lk_replay_owner;
2691 /* create lockowner and lock stateid */
2692 fp = open_stp->st_file;
2693 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2694 open_sop->so_client->cl_clientid.cl_id,
2695 &lock->v.new.owner);
2696 /* XXX: Do we need to check for duplicate stateowners on
2697 * the same file, or should they just be allowed (and
2698 * create new stateids)? */
2699 status = nfserr_resource;
2700 lock_sop = alloc_init_lock_stateowner(strhashval,
2701 open_sop->so_client, open_stp, lock);
2702 if (lock_sop == NULL)
2703 goto out;
2704 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
2705 if (lock_stp == NULL)
2706 goto out;
2707 } else {
2708 /* lock (lock owner + lock stateid) already exists */
2709 status = nfs4_preprocess_seqid_op(current_fh,
2710 lock->lk_old_lock_seqid,
2711 &lock->lk_old_lock_stateid,
2712 CHECK_FH | LOCK_STATE,
2713 &lock->lk_replay_owner, &lock_stp, lock);
2714 if (status)
2715 goto out;
2716 lock_sop = lock->lk_replay_owner;
2718 /* lock->lk_replay_owner and lock_stp have been created or found */
2719 filp = lock_stp->st_vfs_file;
2721 status = nfserr_grace;
2722 if (nfs4_in_grace() && !lock->lk_reclaim)
2723 goto out;
2724 status = nfserr_no_grace;
2725 if (!nfs4_in_grace() && lock->lk_reclaim)
2726 goto out;
2728 locks_init_lock(&file_lock);
2729 switch (lock->lk_type) {
2730 case NFS4_READ_LT:
2731 case NFS4_READW_LT:
2732 file_lock.fl_type = F_RDLCK;
2733 break;
2734 case NFS4_WRITE_LT:
2735 case NFS4_WRITEW_LT:
2736 file_lock.fl_type = F_WRLCK;
2737 break;
2738 default:
2739 status = nfserr_inval;
2740 goto out;
2742 file_lock.fl_owner = (fl_owner_t)lock_sop;
2743 file_lock.fl_pid = current->tgid;
2744 file_lock.fl_file = filp;
2745 file_lock.fl_flags = FL_POSIX;
2746 file_lock.fl_lmops = &nfsd_posix_mng_ops;
2748 file_lock.fl_start = lock->lk_offset;
2749 if ((lock->lk_length == ~(u64)0) ||
2750 LOFF_OVERFLOW(lock->lk_offset, lock->lk_length))
2751 file_lock.fl_end = ~(u64)0;
2752 else
2753 file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
2754 nfs4_transform_lock_offset(&file_lock);
2757 * Try to lock the file in the VFS.
2758 * Note: locks.c uses the BKL to protect the inode's lock list.
2761 /* XXX?: Just to divert the locks_release_private at the start of
2762 * locks_copy_lock: */
2763 conflock.fl_ops = NULL;
2764 conflock.fl_lmops = NULL;
2765 status = posix_lock_file_conf(filp, &file_lock, &conflock);
2766 dprintk("NFSD: nfsd4_lock: posix_lock_file_conf status %d\n",status);
2767 switch (-status) {
2768 case 0: /* success! */
2769 update_stateid(&lock_stp->st_stateid);
2770 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2771 sizeof(stateid_t));
2772 break;
2773 case (EAGAIN): /* conflock holds conflicting lock */
2774 status = nfserr_denied;
2775 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2776 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
2777 break;
2778 case (EDEADLK):
2779 status = nfserr_deadlock;
2780 break;
2781 default:
2782 dprintk("NFSD: nfsd4_lock: posix_lock_file_conf() failed! status %d\n",status);
2783 status = nfserr_resource;
2784 break;
2786 out:
2787 if (status && lock->lk_is_new && lock_sop)
2788 release_stateowner(lock_sop);
2789 if (lock->lk_replay_owner) {
2790 nfs4_get_stateowner(lock->lk_replay_owner);
2791 *replay_owner = lock->lk_replay_owner;
2793 nfs4_unlock_state();
2794 return status;
2798 * LOCKT operation
2801 nfsd4_lockt(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lockt *lockt)
2803 struct inode *inode;
2804 struct file file;
2805 struct file_lock file_lock;
2806 struct file_lock conflock;
2807 int status;
2809 if (nfs4_in_grace())
2810 return nfserr_grace;
2812 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2813 return nfserr_inval;
2815 lockt->lt_stateowner = NULL;
2816 nfs4_lock_state();
2818 status = nfserr_stale_clientid;
2819 if (STALE_CLIENTID(&lockt->lt_clientid))
2820 goto out;
2822 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0))) {
2823 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
2824 if (status == nfserr_symlink)
2825 status = nfserr_inval;
2826 goto out;
2829 inode = current_fh->fh_dentry->d_inode;
2830 locks_init_lock(&file_lock);
2831 switch (lockt->lt_type) {
2832 case NFS4_READ_LT:
2833 case NFS4_READW_LT:
2834 file_lock.fl_type = F_RDLCK;
2835 break;
2836 case NFS4_WRITE_LT:
2837 case NFS4_WRITEW_LT:
2838 file_lock.fl_type = F_WRLCK;
2839 break;
2840 default:
2841 printk("NFSD: nfs4_lockt: bad lock type!\n");
2842 status = nfserr_inval;
2843 goto out;
2846 lockt->lt_stateowner = find_lockstateowner_str(inode,
2847 &lockt->lt_clientid, &lockt->lt_owner);
2848 if (lockt->lt_stateowner)
2849 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2850 file_lock.fl_pid = current->tgid;
2851 file_lock.fl_flags = FL_POSIX;
2852 file_lock.fl_lmops = &nfsd_posix_mng_ops;
2854 file_lock.fl_start = lockt->lt_offset;
2855 if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length))
2856 file_lock.fl_end = ~(u64)0;
2857 else
2858 file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
2860 nfs4_transform_lock_offset(&file_lock);
2862 /* posix_test_lock uses the struct file _only_ to resolve the inode.
2863 * since LOCKT doesn't require an OPEN, and therefore a struct
2864 * file may not exist, pass posix_test_lock a struct file with
2865 * only the dentry:inode set.
2867 memset(&file, 0, sizeof (struct file));
2868 file.f_dentry = current_fh->fh_dentry;
2870 status = nfs_ok;
2871 if (posix_test_lock(&file, &file_lock, &conflock)) {
2872 status = nfserr_denied;
2873 nfs4_set_lock_denied(&conflock, &lockt->lt_denied);
2875 out:
2876 nfs4_unlock_state();
2877 return status;
2881 nfsd4_locku(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_locku *locku, struct nfs4_stateowner **replay_owner)
2883 struct nfs4_stateid *stp;
2884 struct file *filp = NULL;
2885 struct file_lock file_lock;
2886 int status;
2888 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2889 (long long) locku->lu_offset,
2890 (long long) locku->lu_length);
2892 if (check_lock_length(locku->lu_offset, locku->lu_length))
2893 return nfserr_inval;
2895 nfs4_lock_state();
2897 if ((status = nfs4_preprocess_seqid_op(current_fh,
2898 locku->lu_seqid,
2899 &locku->lu_stateid,
2900 CHECK_FH | LOCK_STATE,
2901 &locku->lu_stateowner, &stp, NULL)))
2902 goto out;
2904 filp = stp->st_vfs_file;
2905 BUG_ON(!filp);
2906 locks_init_lock(&file_lock);
2907 file_lock.fl_type = F_UNLCK;
2908 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
2909 file_lock.fl_pid = current->tgid;
2910 file_lock.fl_file = filp;
2911 file_lock.fl_flags = FL_POSIX;
2912 file_lock.fl_lmops = &nfsd_posix_mng_ops;
2913 file_lock.fl_start = locku->lu_offset;
2915 if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
2916 file_lock.fl_end = ~(u64)0;
2917 else
2918 file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
2919 nfs4_transform_lock_offset(&file_lock);
2922 * Try to unlock the file in the VFS.
2924 status = posix_lock_file(filp, &file_lock);
2925 if (status) {
2926 dprintk("NFSD: nfs4_locku: posix_lock_file failed!\n");
2927 goto out_nfserr;
2930 * OK, unlock succeeded; the only thing left to do is update the stateid.
2932 update_stateid(&stp->st_stateid);
2933 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
2935 out:
2936 if (locku->lu_stateowner) {
2937 nfs4_get_stateowner(locku->lu_stateowner);
2938 *replay_owner = locku->lu_stateowner;
2940 nfs4_unlock_state();
2941 return status;
2943 out_nfserr:
2944 status = nfserrno(status);
2945 goto out;
2949 * returns
2950 * 1: locks held by lockowner
2951 * 0: no locks held by lockowner
2953 static int
2954 check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
2956 struct file_lock **flpp;
2957 struct inode *inode = filp->f_dentry->d_inode;
2958 int status = 0;
2960 lock_kernel();
2961 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
2962 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
2963 status = 1;
2964 goto out;
2967 out:
2968 unlock_kernel();
2969 return status;
2973 nfsd4_release_lockowner(struct svc_rqst *rqstp, struct nfsd4_release_lockowner *rlockowner)
2975 clientid_t *clid = &rlockowner->rl_clientid;
2976 struct nfs4_stateowner *sop;
2977 struct nfs4_stateid *stp;
2978 struct xdr_netobj *owner = &rlockowner->rl_owner;
2979 struct list_head matches;
2980 int i;
2981 int status;
2983 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
2984 clid->cl_boot, clid->cl_id);
2986 /* XXX check for lease expiration */
2988 status = nfserr_stale_clientid;
2989 if (STALE_CLIENTID(clid))
2990 return status;
2992 nfs4_lock_state();
2994 status = nfserr_locks_held;
2995 /* XXX: we're doing a linear search through all the lockowners.
2996 * Yipes! For now we'll just hope clients aren't really using
2997 * release_lockowner much, but eventually we have to fix these
2998 * data structures. */
2999 INIT_LIST_HEAD(&matches);
3000 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3001 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
3002 if (!cmp_owner_str(sop, owner, clid))
3003 continue;
3004 list_for_each_entry(stp, &sop->so_stateids,
3005 st_perstateowner) {
3006 if (check_for_locks(stp->st_vfs_file, sop))
3007 goto out;
3008 /* Note: so_perclient unused for lockowners,
3009 * so it's OK to fool with here. */
3010 list_add(&sop->so_perclient, &matches);
3014 /* Clients probably won't expect us to return with some (but not all)
3015 * of the lockowner state released; so don't release any until all
3016 * have been checked. */
3017 status = nfs_ok;
3018 while (!list_empty(&matches)) {
3019 sop = list_entry(matches.next, struct nfs4_stateowner,
3020 so_perclient);
3021 /* unhash_stateowner deletes so_perclient only
3022 * for openowners. */
3023 list_del(&sop->so_perclient);
3024 release_stateowner(sop);
3026 out:
3027 nfs4_unlock_state();
3028 return status;
3031 static inline struct nfs4_client_reclaim *
3032 alloc_reclaim(void)
3034 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
3038 nfs4_has_reclaimed_state(const char *name)
3040 unsigned int strhashval = clientstr_hashval(name);
3041 struct nfs4_client *clp;
3043 clp = find_confirmed_client_by_str(name, strhashval);
3044 return clp ? 1 : 0;
3048 * failure => all reset bets are off, nfserr_no_grace...
3051 nfs4_client_to_reclaim(const char *name)
3053 unsigned int strhashval;
3054 struct nfs4_client_reclaim *crp = NULL;
3056 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
3057 crp = alloc_reclaim();
3058 if (!crp)
3059 return 0;
3060 strhashval = clientstr_hashval(name);
3061 INIT_LIST_HEAD(&crp->cr_strhash);
3062 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
3063 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
3064 reclaim_str_hashtbl_size++;
3065 return 1;
3068 static void
3069 nfs4_release_reclaim(void)
3071 struct nfs4_client_reclaim *crp = NULL;
3072 int i;
3074 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3075 while (!list_empty(&reclaim_str_hashtbl[i])) {
3076 crp = list_entry(reclaim_str_hashtbl[i].next,
3077 struct nfs4_client_reclaim, cr_strhash);
3078 list_del(&crp->cr_strhash);
3079 kfree(crp);
3080 reclaim_str_hashtbl_size--;
3083 BUG_ON(reclaim_str_hashtbl_size);
3087 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
3088 static struct nfs4_client_reclaim *
3089 nfs4_find_reclaim_client(clientid_t *clid)
3091 unsigned int strhashval;
3092 struct nfs4_client *clp;
3093 struct nfs4_client_reclaim *crp = NULL;
3096 /* find clientid in conf_id_hashtbl */
3097 clp = find_confirmed_client(clid);
3098 if (clp == NULL)
3099 return NULL;
3101 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3102 clp->cl_name.len, clp->cl_name.data,
3103 clp->cl_recdir);
3105 /* find clp->cl_name in reclaim_str_hashtbl */
3106 strhashval = clientstr_hashval(clp->cl_recdir);
3107 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
3108 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
3109 return crp;
3112 return NULL;
3116 * Called from OPEN. Look for clientid in reclaim list.
3119 nfs4_check_open_reclaim(clientid_t *clid)
3121 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
3124 /* initialization to perform at module load time: */
3126 void
3127 nfs4_state_init(void)
3129 int i;
3131 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3132 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3133 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3134 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3135 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3137 for (i = 0; i < FILE_HASH_SIZE; i++) {
3138 INIT_LIST_HEAD(&file_hashtbl[i]);
3140 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3141 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3142 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3144 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3145 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3146 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3148 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3149 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3150 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3152 memset(&onestateid, ~0, sizeof(stateid_t));
3153 INIT_LIST_HEAD(&close_lru);
3154 INIT_LIST_HEAD(&client_lru);
3155 INIT_LIST_HEAD(&del_recall_lru);
3156 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3157 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3158 reclaim_str_hashtbl_size = 0;
3161 static void
3162 nfsd4_load_reboot_recovery_data(void)
3164 int status;
3166 nfs4_lock_state();
3167 nfsd4_init_recdir(user_recovery_dirname);
3168 status = nfsd4_recdir_load();
3169 nfs4_unlock_state();
3170 if (status)
3171 printk("NFSD: Failure reading reboot recovery data\n");
3174 /* initialization to perform when the nfsd service is started: */
3176 static void
3177 __nfs4_state_start(void)
3179 time_t grace_time;
3181 boot_time = get_seconds();
3182 grace_time = max(user_lease_time, lease_time);
3183 lease_time = user_lease_time;
3184 in_grace = 1;
3185 printk("NFSD: starting %ld-second grace period\n", grace_time);
3186 laundry_wq = create_singlethread_workqueue("nfsd4");
3187 queue_delayed_work(laundry_wq, &laundromat_work, grace_time*HZ);
3191 nfs4_state_start(void)
3193 int status;
3195 if (nfs4_init)
3196 return 0;
3197 status = nfsd4_init_slabs();
3198 if (status)
3199 return status;
3200 nfsd4_load_reboot_recovery_data();
3201 __nfs4_state_start();
3202 nfs4_init = 1;
3203 return 0;
3207 nfs4_in_grace(void)
3209 return in_grace;
3212 time_t
3213 nfs4_lease_time(void)
3215 return lease_time;
3218 static void
3219 __nfs4_state_shutdown(void)
3221 int i;
3222 struct nfs4_client *clp = NULL;
3223 struct nfs4_delegation *dp = NULL;
3224 struct list_head *pos, *next, reaplist;
3226 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3227 while (!list_empty(&conf_id_hashtbl[i])) {
3228 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3229 expire_client(clp);
3231 while (!list_empty(&unconf_str_hashtbl[i])) {
3232 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3233 expire_client(clp);
3236 INIT_LIST_HEAD(&reaplist);
3237 spin_lock(&recall_lock);
3238 list_for_each_safe(pos, next, &del_recall_lru) {
3239 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3240 list_move(&dp->dl_recall_lru, &reaplist);
3242 spin_unlock(&recall_lock);
3243 list_for_each_safe(pos, next, &reaplist) {
3244 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3245 list_del_init(&dp->dl_recall_lru);
3246 unhash_delegation(dp);
3249 cancel_delayed_work(&laundromat_work);
3250 nfsd4_shutdown_recdir();
3251 nfs4_init = 0;
3254 void
3255 nfs4_state_shutdown(void)
3257 cancel_rearming_delayed_workqueue(laundry_wq, &laundromat_work);
3258 destroy_workqueue(laundry_wq);
3259 nfs4_lock_state();
3260 nfs4_release_reclaim();
3261 __nfs4_state_shutdown();
3262 nfsd4_free_slabs();
3263 nfs4_unlock_state();
3266 static void
3267 nfs4_set_recdir(char *recdir)
3269 nfs4_lock_state();
3270 strcpy(user_recovery_dirname, recdir);
3271 nfs4_unlock_state();
3275 * Change the NFSv4 recovery directory to recdir.
3278 nfs4_reset_recoverydir(char *recdir)
3280 int status;
3281 struct nameidata nd;
3283 status = path_lookup(recdir, LOOKUP_FOLLOW, &nd);
3284 if (status)
3285 return status;
3286 status = -ENOTDIR;
3287 if (S_ISDIR(nd.dentry->d_inode->i_mode)) {
3288 nfs4_set_recdir(recdir);
3289 status = 0;
3291 path_release(&nd);
3292 return status;
3296 * Called when leasetime is changed.
3298 * The only way the protocol gives us to handle on-the-fly lease changes is to
3299 * simulate a reboot. Instead of doing that, we just wait till the next time
3300 * we start to register any changes in lease time. If the administrator
3301 * really wants to change the lease time *now*, they can go ahead and bring
3302 * nfsd down and then back up again after changing the lease time.
3304 void
3305 nfs4_reset_lease(time_t leasetime)
3307 lock_kernel();
3308 user_lease_time = leasetime;
3309 unlock_kernel();