2 * linux/fs/nfsd/nfs4state.c
4 * Copyright (c) 2001 The Regents of the University of Michigan.
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
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/file.h>
45 #include <linux/mount.h>
46 #include <linux/workqueue.h>
47 #include <linux/smp_lock.h>
48 #include <linux/kthread.h>
49 #include <linux/nfs4.h>
50 #include <linux/nfsd/state.h>
51 #include <linux/nfsd/xdr4.h>
52 #include <linux/namei.h>
53 #include <linux/swap.h>
54 #include <linux/mutex.h>
55 #include <linux/lockd/bind.h>
56 #include <linux/module.h>
57 #include <linux/sunrpc/svcauth_gss.h>
59 #define NFSDDBG_FACILITY NFSDDBG_PROC
62 static time_t lease_time = 90; /* default lease time */
63 static time_t user_lease_time = 90;
64 static time_t boot_time;
65 static u32 current_ownerid = 1;
66 static u32 current_fileid = 1;
67 static u32 current_delegid = 1;
69 static stateid_t zerostateid; /* bits all 0 */
70 static stateid_t onestateid; /* bits all 1 */
72 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
73 #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
75 /* forward declarations */
76 static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
77 static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
78 static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
79 static void nfs4_set_recdir(char *recdir);
83 /* Currently used for almost all code touching nfsv4 state: */
84 static DEFINE_MUTEX(client_mutex);
87 * Currently used for the del_recall_lru and file hash table. In an
88 * effort to decrease the scope of the client_mutex, this spinlock may
89 * eventually cover more:
91 static DEFINE_SPINLOCK(recall_lock);
93 static struct kmem_cache *stateowner_slab = NULL;
94 static struct kmem_cache *file_slab = NULL;
95 static struct kmem_cache *stateid_slab = NULL;
96 static struct kmem_cache *deleg_slab = NULL;
101 mutex_lock(&client_mutex);
105 nfs4_unlock_state(void)
107 mutex_unlock(&client_mutex);
111 opaque_hashval(const void *ptr, int nbytes)
113 unsigned char *cptr = (unsigned char *) ptr;
123 static struct list_head del_recall_lru;
126 put_nfs4_file(struct nfs4_file *fi)
128 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
129 list_del(&fi->fi_hash);
130 spin_unlock(&recall_lock);
132 kmem_cache_free(file_slab, fi);
137 get_nfs4_file(struct nfs4_file *fi)
139 atomic_inc(&fi->fi_ref);
142 static int num_delegations;
143 unsigned int max_delegations;
146 * Open owner state (share locks)
149 /* hash tables for nfs4_stateowner */
150 #define OWNER_HASH_BITS 8
151 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
152 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
154 #define ownerid_hashval(id) \
155 ((id) & OWNER_HASH_MASK)
156 #define ownerstr_hashval(clientid, ownername) \
157 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
159 static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
160 static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
162 /* hash table for nfs4_file */
163 #define FILE_HASH_BITS 8
164 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
165 #define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
166 /* hash table for (open)nfs4_stateid */
167 #define STATEID_HASH_BITS 10
168 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
169 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
171 #define file_hashval(x) \
172 hash_ptr(x, FILE_HASH_BITS)
173 #define stateid_hashval(owner_id, file_id) \
174 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
176 static struct list_head file_hashtbl[FILE_HASH_SIZE];
177 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
179 static struct nfs4_delegation *
180 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
182 struct nfs4_delegation *dp;
183 struct nfs4_file *fp = stp->st_file;
184 struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback;
186 dprintk("NFSD alloc_init_deleg\n");
187 if (fp->fi_had_conflict)
189 if (num_delegations > max_delegations)
191 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
195 INIT_LIST_HEAD(&dp->dl_perfile);
196 INIT_LIST_HEAD(&dp->dl_perclnt);
197 INIT_LIST_HEAD(&dp->dl_recall_lru);
202 get_file(stp->st_vfs_file);
203 dp->dl_vfs_file = stp->st_vfs_file;
205 dp->dl_recall.cbr_dp = NULL;
206 dp->dl_recall.cbr_ident = cb->cb_ident;
207 dp->dl_recall.cbr_trunc = 0;
208 dp->dl_stateid.si_boot = boot_time;
209 dp->dl_stateid.si_stateownerid = current_delegid++;
210 dp->dl_stateid.si_fileid = 0;
211 dp->dl_stateid.si_generation = 0;
212 fh_copy_shallow(&dp->dl_fh, ¤t_fh->fh_handle);
214 atomic_set(&dp->dl_count, 1);
215 list_add(&dp->dl_perfile, &fp->fi_delegations);
216 list_add(&dp->dl_perclnt, &clp->cl_delegations);
221 nfs4_put_delegation(struct nfs4_delegation *dp)
223 if (atomic_dec_and_test(&dp->dl_count)) {
224 dprintk("NFSD: freeing dp %p\n",dp);
225 put_nfs4_file(dp->dl_file);
226 kmem_cache_free(deleg_slab, dp);
231 /* Remove the associated file_lock first, then remove the delegation.
232 * lease_modify() is called to remove the FS_LEASE file_lock from
233 * the i_flock list, eventually calling nfsd's lock_manager
234 * fl_release_callback.
237 nfs4_close_delegation(struct nfs4_delegation *dp)
239 struct file *filp = dp->dl_vfs_file;
241 dprintk("NFSD: close_delegation dp %p\n",dp);
242 dp->dl_vfs_file = NULL;
243 /* The following nfsd_close may not actually close the file,
244 * but we want to remove the lease in any case. */
246 vfs_setlease(filp, F_UNLCK, &dp->dl_flock);
250 /* Called under the state lock. */
252 unhash_delegation(struct nfs4_delegation *dp)
254 list_del_init(&dp->dl_perfile);
255 list_del_init(&dp->dl_perclnt);
256 spin_lock(&recall_lock);
257 list_del_init(&dp->dl_recall_lru);
258 spin_unlock(&recall_lock);
259 nfs4_close_delegation(dp);
260 nfs4_put_delegation(dp);
267 /* Hash tables for nfs4_clientid state */
268 #define CLIENT_HASH_BITS 4
269 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
270 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
272 #define clientid_hashval(id) \
273 ((id) & CLIENT_HASH_MASK)
274 #define clientstr_hashval(name) \
275 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
277 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
278 * used in reboot/reset lease grace period processing
280 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
281 * setclientid_confirmed info.
283 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
286 * client_lru holds client queue ordered by nfs4_client.cl_time
289 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
290 * for last close replay.
292 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
293 static int reclaim_str_hashtbl_size = 0;
294 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
295 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
296 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
297 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
298 static struct list_head client_lru;
299 static struct list_head close_lru;
301 static void unhash_generic_stateid(struct nfs4_stateid *stp)
303 list_del(&stp->st_hash);
304 list_del(&stp->st_perfile);
305 list_del(&stp->st_perstateowner);
308 static void free_generic_stateid(struct nfs4_stateid *stp)
310 put_nfs4_file(stp->st_file);
311 kmem_cache_free(stateid_slab, stp);
314 static void release_lock_stateid(struct nfs4_stateid *stp)
316 unhash_generic_stateid(stp);
317 locks_remove_posix(stp->st_vfs_file, (fl_owner_t)stp->st_stateowner);
318 free_generic_stateid(stp);
321 static void unhash_lockowner(struct nfs4_stateowner *sop)
323 struct nfs4_stateid *stp;
325 list_del(&sop->so_idhash);
326 list_del(&sop->so_strhash);
327 list_del(&sop->so_perstateid);
328 while (!list_empty(&sop->so_stateids)) {
329 stp = list_first_entry(&sop->so_stateids,
330 struct nfs4_stateid, st_perstateowner);
331 release_lock_stateid(stp);
335 static void release_lockowner(struct nfs4_stateowner *sop)
337 unhash_lockowner(sop);
338 nfs4_put_stateowner(sop);
342 release_stateid_lockowners(struct nfs4_stateid *open_stp)
344 struct nfs4_stateowner *lock_sop;
346 while (!list_empty(&open_stp->st_lockowners)) {
347 lock_sop = list_entry(open_stp->st_lockowners.next,
348 struct nfs4_stateowner, so_perstateid);
349 /* list_del(&open_stp->st_lockowners); */
350 BUG_ON(lock_sop->so_is_open_owner);
351 release_lockowner(lock_sop);
355 static void release_open_stateid(struct nfs4_stateid *stp)
357 unhash_generic_stateid(stp);
358 release_stateid_lockowners(stp);
359 nfsd_close(stp->st_vfs_file);
360 free_generic_stateid(stp);
363 static void unhash_openowner(struct nfs4_stateowner *sop)
365 struct nfs4_stateid *stp;
367 list_del(&sop->so_idhash);
368 list_del(&sop->so_strhash);
369 list_del(&sop->so_perclient);
370 list_del(&sop->so_perstateid); /* XXX: necessary? */
371 while (!list_empty(&sop->so_stateids)) {
372 stp = list_first_entry(&sop->so_stateids,
373 struct nfs4_stateid, st_perstateowner);
374 release_open_stateid(stp);
378 static void release_openowner(struct nfs4_stateowner *sop)
380 unhash_openowner(sop);
381 list_del(&sop->so_close_lru);
382 nfs4_put_stateowner(sop);
385 static DEFINE_SPINLOCK(sessionid_lock);
386 #define SESSION_HASH_SIZE 512
387 static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
390 hash_sessionid(struct nfs4_sessionid *sessionid)
392 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
394 return sid->sequence % SESSION_HASH_SIZE;
398 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
400 u32 *ptr = (u32 *)(&sessionid->data[0]);
401 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
404 /* caller must hold sessionid_lock */
405 static struct nfsd4_session *
406 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
408 struct nfsd4_session *elem;
411 dump_sessionid(__func__, sessionid);
412 idx = hash_sessionid(sessionid);
413 dprintk("%s: idx is %d\n", __func__, idx);
414 /* Search in the appropriate list */
415 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
416 dump_sessionid("list traversal", &elem->se_sessionid);
417 if (!memcmp(elem->se_sessionid.data, sessionid->data,
418 NFS4_MAX_SESSIONID_LEN)) {
423 dprintk("%s: session not found\n", __func__);
427 /* caller must hold sessionid_lock */
429 unhash_session(struct nfsd4_session *ses)
431 list_del(&ses->se_hash);
432 list_del(&ses->se_perclnt);
436 release_session(struct nfsd4_session *ses)
438 spin_lock(&sessionid_lock);
440 spin_unlock(&sessionid_lock);
441 nfsd4_put_session(ses);
445 free_session(struct kref *kref)
447 struct nfsd4_session *ses;
449 ses = container_of(kref, struct nfsd4_session, se_ref);
450 kfree(ses->se_slots);
455 renew_client(struct nfs4_client *clp)
458 * Move client to the end to the LRU list.
460 dprintk("renewing client (clientid %08x/%08x)\n",
461 clp->cl_clientid.cl_boot,
462 clp->cl_clientid.cl_id);
463 list_move_tail(&clp->cl_lru, &client_lru);
464 clp->cl_time = get_seconds();
467 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
469 STALE_CLIENTID(clientid_t *clid)
471 if (clid->cl_boot == boot_time)
473 dprintk("NFSD stale clientid (%08x/%08x)\n",
474 clid->cl_boot, clid->cl_id);
479 * XXX Should we use a slab cache ?
480 * This type of memory management is somewhat inefficient, but we use it
481 * anyway since SETCLIENTID is not a common operation.
483 static struct nfs4_client *alloc_client(struct xdr_netobj name)
485 struct nfs4_client *clp;
487 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
490 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
491 if (clp->cl_name.data == NULL) {
495 memcpy(clp->cl_name.data, name.data, name.len);
496 clp->cl_name.len = name.len;
501 shutdown_callback_client(struct nfs4_client *clp)
503 struct rpc_clnt *clnt = clp->cl_callback.cb_client;
507 * Callback threads take a reference on the client, so there
508 * should be no outstanding callbacks at this point.
510 clp->cl_callback.cb_client = NULL;
511 rpc_shutdown_client(clnt);
516 free_client(struct nfs4_client *clp)
518 shutdown_callback_client(clp);
519 if (clp->cl_cred.cr_group_info)
520 put_group_info(clp->cl_cred.cr_group_info);
521 kfree(clp->cl_principal);
522 kfree(clp->cl_name.data);
527 put_nfs4_client(struct nfs4_client *clp)
529 if (atomic_dec_and_test(&clp->cl_count))
534 expire_client(struct nfs4_client *clp)
536 struct nfs4_stateowner *sop;
537 struct nfs4_delegation *dp;
538 struct list_head reaplist;
540 dprintk("NFSD: expire_client cl_count %d\n",
541 atomic_read(&clp->cl_count));
543 INIT_LIST_HEAD(&reaplist);
544 spin_lock(&recall_lock);
545 while (!list_empty(&clp->cl_delegations)) {
546 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
547 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
549 list_del_init(&dp->dl_perclnt);
550 list_move(&dp->dl_recall_lru, &reaplist);
552 spin_unlock(&recall_lock);
553 while (!list_empty(&reaplist)) {
554 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
555 list_del_init(&dp->dl_recall_lru);
556 unhash_delegation(dp);
558 list_del(&clp->cl_idhash);
559 list_del(&clp->cl_strhash);
560 list_del(&clp->cl_lru);
561 while (!list_empty(&clp->cl_openowners)) {
562 sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
563 release_openowner(sop);
565 while (!list_empty(&clp->cl_sessions)) {
566 struct nfsd4_session *ses;
567 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
569 release_session(ses);
571 put_nfs4_client(clp);
574 static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir)
576 struct nfs4_client *clp;
578 clp = alloc_client(name);
581 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
582 atomic_set(&clp->cl_count, 1);
583 atomic_set(&clp->cl_callback.cb_set, 0);
584 INIT_LIST_HEAD(&clp->cl_idhash);
585 INIT_LIST_HEAD(&clp->cl_strhash);
586 INIT_LIST_HEAD(&clp->cl_openowners);
587 INIT_LIST_HEAD(&clp->cl_delegations);
588 INIT_LIST_HEAD(&clp->cl_sessions);
589 INIT_LIST_HEAD(&clp->cl_lru);
593 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
595 memcpy(target->cl_verifier.data, source->data,
596 sizeof(target->cl_verifier.data));
599 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
601 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
602 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
605 static void copy_cred(struct svc_cred *target, struct svc_cred *source)
607 target->cr_uid = source->cr_uid;
608 target->cr_gid = source->cr_gid;
609 target->cr_group_info = source->cr_group_info;
610 get_group_info(target->cr_group_info);
613 static int same_name(const char *n1, const char *n2)
615 return 0 == memcmp(n1, n2, HEXDIR_LEN);
619 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
621 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
625 same_clid(clientid_t *cl1, clientid_t *cl2)
627 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
630 /* XXX what about NGROUP */
632 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
634 return cr1->cr_uid == cr2->cr_uid;
637 static void gen_clid(struct nfs4_client *clp)
639 static u32 current_clientid = 1;
641 clp->cl_clientid.cl_boot = boot_time;
642 clp->cl_clientid.cl_id = current_clientid++;
645 static void gen_confirm(struct nfs4_client *clp)
650 p = (u32 *)clp->cl_confirm.data;
651 *p++ = get_seconds();
655 static int check_name(struct xdr_netobj name)
659 if (name.len > NFS4_OPAQUE_LIMIT) {
660 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
667 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
669 unsigned int idhashval;
671 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
672 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
673 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
674 list_add_tail(&clp->cl_lru, &client_lru);
675 clp->cl_time = get_seconds();
679 move_to_confirmed(struct nfs4_client *clp)
681 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
682 unsigned int strhashval;
684 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
685 list_del_init(&clp->cl_strhash);
686 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
687 strhashval = clientstr_hashval(clp->cl_recdir);
688 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
692 static struct nfs4_client *
693 find_confirmed_client(clientid_t *clid)
695 struct nfs4_client *clp;
696 unsigned int idhashval = clientid_hashval(clid->cl_id);
698 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
699 if (same_clid(&clp->cl_clientid, clid))
705 static struct nfs4_client *
706 find_unconfirmed_client(clientid_t *clid)
708 struct nfs4_client *clp;
709 unsigned int idhashval = clientid_hashval(clid->cl_id);
711 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
712 if (same_clid(&clp->cl_clientid, clid))
718 static struct nfs4_client *
719 find_confirmed_client_by_str(const char *dname, unsigned int hashval)
721 struct nfs4_client *clp;
723 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
724 if (same_name(clp->cl_recdir, dname))
730 static struct nfs4_client *
731 find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
733 struct nfs4_client *clp;
735 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
736 if (same_name(clp->cl_recdir, dname))
742 /* a helper function for parse_callback */
744 parse_octet(unsigned int *lenp, char **addrp)
746 unsigned int len = *lenp;
758 if ((c < '0') || (c > '9')) {
764 n = (n * 10) + (c - '0');
775 /* parse and set the setclientid ipv4 callback address */
777 parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
782 u32 addrlen = addr_len;
783 char *addr = addr_val;
788 for(i = 4; i > 0 ; i--) {
789 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
792 cbaddr |= (temp << shift);
800 for(i = 2; i > 0 ; i--) {
801 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
804 cbport |= (temp << shift);
813 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
815 struct nfs4_callback *cb = &clp->cl_callback;
817 /* Currently, we only support tcp for the callback channel */
818 if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
821 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
822 &cb->cb_addr, &cb->cb_port)))
824 cb->cb_prog = se->se_callback_prog;
825 cb->cb_ident = se->se_callback_ident;
828 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
829 "will not receive delegations\n",
830 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
836 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
837 struct nfsd4_setclientid *setclid)
839 struct sockaddr_in *sin = svc_addr_in(rqstp);
840 struct xdr_netobj clname = {
841 .len = setclid->se_namelen,
842 .data = setclid->se_name,
844 nfs4_verifier clverifier = setclid->se_verf;
845 unsigned int strhashval;
846 struct nfs4_client *conf, *unconf, *new;
849 char dname[HEXDIR_LEN];
851 if (!check_name(clname))
854 status = nfs4_make_rec_clidname(dname, &clname);
859 * XXX The Duplicate Request Cache (DRC) has been checked (??)
860 * We get here on a DRC miss.
863 strhashval = clientstr_hashval(dname);
866 conf = find_confirmed_client_by_str(dname, strhashval);
868 /* RFC 3530 14.2.33 CASE 0: */
869 status = nfserr_clid_inuse;
870 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
871 dprintk("NFSD: setclientid: string in use by client"
872 " at %pI4\n", &conf->cl_addr);
877 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
878 * has a description of SETCLIENTID request processing consisting
879 * of 5 bullet points, labeled as CASE0 - CASE4 below.
881 unconf = find_unconfirmed_client_by_str(dname, strhashval);
882 status = nfserr_resource;
885 * RFC 3530 14.2.33 CASE 4:
886 * placed first, because it is the normal case
889 expire_client(unconf);
890 new = create_client(clname, dname);
894 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
896 * RFC 3530 14.2.33 CASE 1:
897 * probable callback update
900 /* Note this is removing unconfirmed {*x***},
901 * which is stronger than RFC recommended {vxc**}.
902 * This has the advantage that there is at most
903 * one {*x***} in either list at any time.
905 expire_client(unconf);
907 new = create_client(clname, dname);
910 copy_clid(new, conf);
911 } else if (!unconf) {
913 * RFC 3530 14.2.33 CASE 2:
914 * probable client reboot; state will be removed if
917 new = create_client(clname, dname);
923 * RFC 3530 14.2.33 CASE 3:
924 * probable client reboot; state will be removed if
927 expire_client(unconf);
928 new = create_client(clname, dname);
933 copy_verf(new, &clverifier);
934 new->cl_addr = sin->sin_addr.s_addr;
935 new->cl_flavor = rqstp->rq_flavor;
936 princ = svc_gss_principal(rqstp);
938 new->cl_principal = kstrdup(princ, GFP_KERNEL);
939 if (new->cl_principal == NULL) {
944 copy_cred(&new->cl_cred, &rqstp->rq_cred);
946 gen_callback(new, setclid);
947 add_to_unconfirmed(new, strhashval);
948 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
949 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
950 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
959 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
960 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
961 * bullets, labeled as CASE1 - CASE4 below.
964 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
965 struct nfsd4_compound_state *cstate,
966 struct nfsd4_setclientid_confirm *setclientid_confirm)
968 struct sockaddr_in *sin = svc_addr_in(rqstp);
969 struct nfs4_client *conf, *unconf;
970 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
971 clientid_t * clid = &setclientid_confirm->sc_clientid;
974 if (STALE_CLIENTID(clid))
975 return nfserr_stale_clientid;
977 * XXX The Duplicate Request Cache (DRC) has been checked (??)
978 * We get here on a DRC miss.
983 conf = find_confirmed_client(clid);
984 unconf = find_unconfirmed_client(clid);
986 status = nfserr_clid_inuse;
987 if (conf && conf->cl_addr != sin->sin_addr.s_addr)
989 if (unconf && unconf->cl_addr != sin->sin_addr.s_addr)
993 * section 14.2.34 of RFC 3530 has a description of
994 * SETCLIENTID_CONFIRM request processing consisting
995 * of 4 bullet points, labeled as CASE1 - CASE4 below.
997 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
999 * RFC 3530 14.2.34 CASE 1:
1002 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
1003 status = nfserr_clid_inuse;
1005 /* XXX: We just turn off callbacks until we can handle
1006 * change request correctly. */
1007 atomic_set(&conf->cl_callback.cb_set, 0);
1009 nfsd4_remove_clid_dir(unconf);
1010 expire_client(unconf);
1014 } else if (conf && !unconf) {
1016 * RFC 3530 14.2.34 CASE 2:
1017 * probable retransmitted request; play it safe and
1020 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
1021 status = nfserr_clid_inuse;
1024 } else if (!conf && unconf
1025 && same_verf(&unconf->cl_confirm, &confirm)) {
1027 * RFC 3530 14.2.34 CASE 3:
1028 * Normal case; new or rebooted client:
1030 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
1031 status = nfserr_clid_inuse;
1034 clientstr_hashval(unconf->cl_recdir);
1035 conf = find_confirmed_client_by_str(unconf->cl_recdir,
1038 nfsd4_remove_clid_dir(conf);
1039 expire_client(conf);
1041 move_to_confirmed(unconf);
1043 nfsd4_probe_callback(conf);
1046 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
1047 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
1050 * RFC 3530 14.2.34 CASE 4:
1051 * Client probably hasn't noticed that we rebooted yet.
1053 status = nfserr_stale_clientid;
1055 /* check that we have hit one of the cases...*/
1056 status = nfserr_clid_inuse;
1059 nfs4_unlock_state();
1063 /* OPEN Share state helper functions */
1064 static inline struct nfs4_file *
1065 alloc_init_file(struct inode *ino)
1067 struct nfs4_file *fp;
1068 unsigned int hashval = file_hashval(ino);
1070 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
1072 atomic_set(&fp->fi_ref, 1);
1073 INIT_LIST_HEAD(&fp->fi_hash);
1074 INIT_LIST_HEAD(&fp->fi_stateids);
1075 INIT_LIST_HEAD(&fp->fi_delegations);
1076 spin_lock(&recall_lock);
1077 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
1078 spin_unlock(&recall_lock);
1079 fp->fi_inode = igrab(ino);
1080 fp->fi_id = current_fileid++;
1081 fp->fi_had_conflict = false;
1088 nfsd4_free_slab(struct kmem_cache **slab)
1092 kmem_cache_destroy(*slab);
1097 nfsd4_free_slabs(void)
1099 nfsd4_free_slab(&stateowner_slab);
1100 nfsd4_free_slab(&file_slab);
1101 nfsd4_free_slab(&stateid_slab);
1102 nfsd4_free_slab(&deleg_slab);
1106 nfsd4_init_slabs(void)
1108 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
1109 sizeof(struct nfs4_stateowner), 0, 0, NULL);
1110 if (stateowner_slab == NULL)
1112 file_slab = kmem_cache_create("nfsd4_files",
1113 sizeof(struct nfs4_file), 0, 0, NULL);
1114 if (file_slab == NULL)
1116 stateid_slab = kmem_cache_create("nfsd4_stateids",
1117 sizeof(struct nfs4_stateid), 0, 0, NULL);
1118 if (stateid_slab == NULL)
1120 deleg_slab = kmem_cache_create("nfsd4_delegations",
1121 sizeof(struct nfs4_delegation), 0, 0, NULL);
1122 if (deleg_slab == NULL)
1127 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1132 nfs4_free_stateowner(struct kref *kref)
1134 struct nfs4_stateowner *sop =
1135 container_of(kref, struct nfs4_stateowner, so_ref);
1136 kfree(sop->so_owner.data);
1137 kmem_cache_free(stateowner_slab, sop);
1140 static inline struct nfs4_stateowner *
1141 alloc_stateowner(struct xdr_netobj *owner)
1143 struct nfs4_stateowner *sop;
1145 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1146 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1147 memcpy(sop->so_owner.data, owner->data, owner->len);
1148 sop->so_owner.len = owner->len;
1149 kref_init(&sop->so_ref);
1152 kmem_cache_free(stateowner_slab, sop);
1157 static struct nfs4_stateowner *
1158 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1159 struct nfs4_stateowner *sop;
1160 struct nfs4_replay *rp;
1161 unsigned int idhashval;
1163 if (!(sop = alloc_stateowner(&open->op_owner)))
1165 idhashval = ownerid_hashval(current_ownerid);
1166 INIT_LIST_HEAD(&sop->so_idhash);
1167 INIT_LIST_HEAD(&sop->so_strhash);
1168 INIT_LIST_HEAD(&sop->so_perclient);
1169 INIT_LIST_HEAD(&sop->so_stateids);
1170 INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
1171 INIT_LIST_HEAD(&sop->so_close_lru);
1173 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1174 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
1175 list_add(&sop->so_perclient, &clp->cl_openowners);
1176 sop->so_is_open_owner = 1;
1177 sop->so_id = current_ownerid++;
1178 sop->so_client = clp;
1179 sop->so_seqid = open->op_seqid;
1180 sop->so_confirmed = 0;
1181 rp = &sop->so_replay;
1182 rp->rp_status = nfserr_serverfault;
1184 rp->rp_buf = rp->rp_ibuf;
1189 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1190 struct nfs4_stateowner *sop = open->op_stateowner;
1191 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1193 INIT_LIST_HEAD(&stp->st_hash);
1194 INIT_LIST_HEAD(&stp->st_perstateowner);
1195 INIT_LIST_HEAD(&stp->st_lockowners);
1196 INIT_LIST_HEAD(&stp->st_perfile);
1197 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
1198 list_add(&stp->st_perstateowner, &sop->so_stateids);
1199 list_add(&stp->st_perfile, &fp->fi_stateids);
1200 stp->st_stateowner = sop;
1203 stp->st_stateid.si_boot = boot_time;
1204 stp->st_stateid.si_stateownerid = sop->so_id;
1205 stp->st_stateid.si_fileid = fp->fi_id;
1206 stp->st_stateid.si_generation = 0;
1207 stp->st_access_bmap = 0;
1208 stp->st_deny_bmap = 0;
1209 __set_bit(open->op_share_access, &stp->st_access_bmap);
1210 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
1211 stp->st_openstp = NULL;
1215 move_to_close_lru(struct nfs4_stateowner *sop)
1217 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1219 list_move_tail(&sop->so_close_lru, &close_lru);
1220 sop->so_time = get_seconds();
1224 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
1227 return (sop->so_owner.len == owner->len) &&
1228 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
1229 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
1232 static struct nfs4_stateowner *
1233 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1235 struct nfs4_stateowner *so = NULL;
1237 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
1238 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
1244 /* search file_hashtbl[] for file */
1245 static struct nfs4_file *
1246 find_file(struct inode *ino)
1248 unsigned int hashval = file_hashval(ino);
1249 struct nfs4_file *fp;
1251 spin_lock(&recall_lock);
1252 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
1253 if (fp->fi_inode == ino) {
1255 spin_unlock(&recall_lock);
1259 spin_unlock(&recall_lock);
1263 static inline int access_valid(u32 x)
1265 if (x < NFS4_SHARE_ACCESS_READ)
1267 if (x > NFS4_SHARE_ACCESS_BOTH)
1272 static inline int deny_valid(u32 x)
1274 /* Note: unlike access bits, deny bits may be zero. */
1275 return x <= NFS4_SHARE_DENY_BOTH;
1279 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1280 * st_{access,deny}_bmap field of the stateid, in order to track not
1281 * only what share bits are currently in force, but also what
1282 * combinations of share bits previous opens have used. This allows us
1283 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1284 * return an error if the client attempt to downgrade to a combination
1285 * of share bits not explicable by closing some of its previous opens.
1287 * XXX: This enforcement is actually incomplete, since we don't keep
1288 * track of access/deny bit combinations; so, e.g., we allow:
1290 * OPEN allow read, deny write
1291 * OPEN allow both, deny none
1292 * DOWNGRADE allow read, deny none
1294 * which we should reject.
1297 set_access(unsigned int *access, unsigned long bmap) {
1301 for (i = 1; i < 4; i++) {
1302 if (test_bit(i, &bmap))
1308 set_deny(unsigned int *deny, unsigned long bmap) {
1312 for (i = 0; i < 4; i++) {
1313 if (test_bit(i, &bmap))
1319 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1320 unsigned int access, deny;
1322 set_access(&access, stp->st_access_bmap);
1323 set_deny(&deny, stp->st_deny_bmap);
1324 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1330 * Called to check deny when READ with all zero stateid or
1331 * WRITE with all zero or all one stateid
1334 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1336 struct inode *ino = current_fh->fh_dentry->d_inode;
1337 struct nfs4_file *fp;
1338 struct nfs4_stateid *stp;
1341 dprintk("NFSD: nfs4_share_conflict\n");
1343 fp = find_file(ino);
1346 ret = nfserr_locked;
1347 /* Search for conflicting share reservations */
1348 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1349 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1350 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1360 nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1362 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1363 drop_file_write_access(filp);
1364 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1369 * Recall a delegation
1372 do_recall(void *__dp)
1374 struct nfs4_delegation *dp = __dp;
1376 dp->dl_file->fi_had_conflict = true;
1377 nfsd4_cb_recall(dp);
1382 * Spawn a thread to perform a recall on the delegation represented
1383 * by the lease (file_lock)
1385 * Called from break_lease() with lock_kernel() held.
1386 * Note: we assume break_lease will only call this *once* for any given
1390 void nfsd_break_deleg_cb(struct file_lock *fl)
1392 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1393 struct task_struct *t;
1395 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1399 /* We're assuming the state code never drops its reference
1400 * without first removing the lease. Since we're in this lease
1401 * callback (and since the lease code is serialized by the kernel
1402 * lock) we know the server hasn't removed the lease yet, we know
1403 * it's safe to take a reference: */
1404 atomic_inc(&dp->dl_count);
1405 atomic_inc(&dp->dl_client->cl_count);
1407 spin_lock(&recall_lock);
1408 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1409 spin_unlock(&recall_lock);
1411 /* only place dl_time is set. protected by lock_kernel*/
1412 dp->dl_time = get_seconds();
1415 * We don't want the locks code to timeout the lease for us;
1416 * we'll remove it ourself if the delegation isn't returned
1419 fl->fl_break_time = 0;
1421 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1423 struct nfs4_client *clp = dp->dl_client;
1425 printk(KERN_INFO "NFSD: Callback thread failed for "
1426 "for client (clientid %08x/%08x)\n",
1427 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1428 put_nfs4_client(dp->dl_client);
1429 nfs4_put_delegation(dp);
1434 * The file_lock is being reapd.
1436 * Called by locks_free_lock() with lock_kernel() held.
1439 void nfsd_release_deleg_cb(struct file_lock *fl)
1441 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1443 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1445 if (!(fl->fl_flags & FL_LEASE) || !dp)
1447 dp->dl_flock = NULL;
1451 * Set the delegation file_lock back pointer.
1453 * Called from setlease() with lock_kernel() held.
1456 void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1458 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1460 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1467 * Called from setlease() with lock_kernel() held
1470 int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1472 struct nfs4_delegation *onlistd =
1473 (struct nfs4_delegation *)onlist->fl_owner;
1474 struct nfs4_delegation *tryd =
1475 (struct nfs4_delegation *)try->fl_owner;
1477 if (onlist->fl_lmops != try->fl_lmops)
1480 return onlistd->dl_client == tryd->dl_client;
1485 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1488 return lease_modify(onlist, arg);
1493 static struct lock_manager_operations nfsd_lease_mng_ops = {
1494 .fl_break = nfsd_break_deleg_cb,
1495 .fl_release_private = nfsd_release_deleg_cb,
1496 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1497 .fl_mylease = nfsd_same_client_deleg_cb,
1498 .fl_change = nfsd_change_deleg_cb,
1503 nfsd4_process_open1(struct nfsd4_open *open)
1505 clientid_t *clientid = &open->op_clientid;
1506 struct nfs4_client *clp = NULL;
1507 unsigned int strhashval;
1508 struct nfs4_stateowner *sop = NULL;
1510 if (!check_name(open->op_owner))
1511 return nfserr_inval;
1513 if (STALE_CLIENTID(&open->op_clientid))
1514 return nfserr_stale_clientid;
1516 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1517 sop = find_openstateowner_str(strhashval, open);
1518 open->op_stateowner = sop;
1520 /* Make sure the client's lease hasn't expired. */
1521 clp = find_confirmed_client(clientid);
1523 return nfserr_expired;
1526 if (!sop->so_confirmed) {
1527 /* Replace unconfirmed owners without checking for replay. */
1528 clp = sop->so_client;
1529 release_openowner(sop);
1530 open->op_stateowner = NULL;
1533 if (open->op_seqid == sop->so_seqid - 1) {
1534 if (sop->so_replay.rp_buflen)
1535 return nfserr_replay_me;
1536 /* The original OPEN failed so spectacularly
1537 * that we don't even have replay data saved!
1538 * Therefore, we have no choice but to continue
1539 * processing this OPEN; presumably, we'll
1540 * fail again for the same reason.
1542 dprintk("nfsd4_process_open1: replay with no replay cache\n");
1545 if (open->op_seqid != sop->so_seqid)
1546 return nfserr_bad_seqid;
1548 if (open->op_stateowner == NULL) {
1549 sop = alloc_init_open_stateowner(strhashval, clp, open);
1551 return nfserr_resource;
1552 open->op_stateowner = sop;
1554 list_del_init(&sop->so_close_lru);
1555 renew_client(sop->so_client);
1559 static inline __be32
1560 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1562 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1563 return nfserr_openmode;
1568 static struct nfs4_delegation *
1569 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1571 struct nfs4_delegation *dp;
1573 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) {
1574 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1581 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1582 struct nfs4_delegation **dp)
1585 __be32 status = nfserr_bad_stateid;
1587 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1590 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1591 RD_STATE : WR_STATE;
1592 status = nfs4_check_delegmode(*dp, flags);
1596 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1600 open->op_stateowner->so_confirmed = 1;
1605 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1607 struct nfs4_stateid *local;
1608 __be32 status = nfserr_share_denied;
1609 struct nfs4_stateowner *sop = open->op_stateowner;
1611 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
1612 /* ignore lock owners */
1613 if (local->st_stateowner->so_is_open_owner == 0)
1615 /* remember if we have seen this open owner */
1616 if (local->st_stateowner == sop)
1618 /* check for conflicting share reservations */
1619 if (!test_share(local, open))
1627 static inline struct nfs4_stateid *
1628 nfs4_alloc_stateid(void)
1630 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
1634 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
1635 struct nfs4_delegation *dp,
1636 struct svc_fh *cur_fh, int flags)
1638 struct nfs4_stateid *stp;
1640 stp = nfs4_alloc_stateid();
1642 return nfserr_resource;
1645 get_file(dp->dl_vfs_file);
1646 stp->st_vfs_file = dp->dl_vfs_file;
1649 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1652 if (status == nfserr_dropit)
1653 status = nfserr_jukebox;
1654 kmem_cache_free(stateid_slab, stp);
1662 static inline __be32
1663 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1664 struct nfsd4_open *open)
1666 struct iattr iattr = {
1667 .ia_valid = ATTR_SIZE,
1670 if (!open->op_truncate)
1672 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
1673 return nfserr_inval;
1674 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1678 nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1680 struct file *filp = stp->st_vfs_file;
1681 struct inode *inode = filp->f_path.dentry->d_inode;
1682 unsigned int share_access, new_writer;
1685 set_access(&share_access, stp->st_access_bmap);
1686 new_writer = (~share_access) & open->op_share_access
1687 & NFS4_SHARE_ACCESS_WRITE;
1690 int err = get_write_access(inode);
1692 return nfserrno(err);
1693 err = mnt_want_write(cur_fh->fh_export->ex_path.mnt);
1695 return nfserrno(err);
1696 file_take_write(filp);
1698 status = nfsd4_truncate(rqstp, cur_fh, open);
1701 put_write_access(inode);
1704 /* remember the open */
1705 filp->f_mode |= open->op_share_access;
1706 __set_bit(open->op_share_access, &stp->st_access_bmap);
1707 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
1714 nfs4_set_claim_prev(struct nfsd4_open *open)
1716 open->op_stateowner->so_confirmed = 1;
1717 open->op_stateowner->so_client->cl_firststate = 1;
1721 * Attempt to hand out a delegation.
1724 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1726 struct nfs4_delegation *dp;
1727 struct nfs4_stateowner *sop = stp->st_stateowner;
1728 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1729 struct file_lock fl, *flp = &fl;
1730 int status, flag = 0;
1732 flag = NFS4_OPEN_DELEGATE_NONE;
1733 open->op_recall = 0;
1734 switch (open->op_claim_type) {
1735 case NFS4_OPEN_CLAIM_PREVIOUS:
1736 if (!atomic_read(&cb->cb_set))
1737 open->op_recall = 1;
1738 flag = open->op_delegate_type;
1739 if (flag == NFS4_OPEN_DELEGATE_NONE)
1742 case NFS4_OPEN_CLAIM_NULL:
1743 /* Let's not give out any delegations till everyone's
1744 * had the chance to reclaim theirs.... */
1745 if (locks_in_grace())
1747 if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
1749 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1750 flag = NFS4_OPEN_DELEGATE_WRITE;
1752 flag = NFS4_OPEN_DELEGATE_READ;
1758 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1760 flag = NFS4_OPEN_DELEGATE_NONE;
1763 locks_init_lock(&fl);
1764 fl.fl_lmops = &nfsd_lease_mng_ops;
1765 fl.fl_flags = FL_LEASE;
1766 fl.fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
1767 fl.fl_end = OFFSET_MAX;
1768 fl.fl_owner = (fl_owner_t)dp;
1769 fl.fl_file = stp->st_vfs_file;
1770 fl.fl_pid = current->tgid;
1772 /* vfs_setlease checks to see if delegation should be handed out.
1773 * the lock_manager callbacks fl_mylease and fl_change are used
1775 if ((status = vfs_setlease(stp->st_vfs_file, fl.fl_type, &flp))) {
1776 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
1777 unhash_delegation(dp);
1778 flag = NFS4_OPEN_DELEGATE_NONE;
1782 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1784 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1785 dp->dl_stateid.si_boot,
1786 dp->dl_stateid.si_stateownerid,
1787 dp->dl_stateid.si_fileid,
1788 dp->dl_stateid.si_generation);
1790 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
1791 && flag == NFS4_OPEN_DELEGATE_NONE
1792 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
1793 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
1794 open->op_delegate_type = flag;
1798 * called with nfs4_lock_state() held.
1801 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1803 struct nfs4_file *fp = NULL;
1804 struct inode *ino = current_fh->fh_dentry->d_inode;
1805 struct nfs4_stateid *stp = NULL;
1806 struct nfs4_delegation *dp = NULL;
1809 status = nfserr_inval;
1810 if (!access_valid(open->op_share_access)
1811 || !deny_valid(open->op_share_deny))
1814 * Lookup file; if found, lookup stateid and check open request,
1815 * and check for delegations in the process of being recalled.
1816 * If not found, create the nfs4_file struct
1818 fp = find_file(ino);
1820 if ((status = nfs4_check_open(fp, open, &stp)))
1822 status = nfs4_check_deleg(fp, open, &dp);
1826 status = nfserr_bad_stateid;
1827 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1829 status = nfserr_resource;
1830 fp = alloc_init_file(ino);
1836 * OPEN the file, or upgrade an existing OPEN.
1837 * If truncate fails, the OPEN fails.
1840 /* Stateid was found, this is an OPEN upgrade */
1841 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1844 update_stateid(&stp->st_stateid);
1846 /* Stateid was not found, this is a new OPEN */
1848 if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
1849 flags |= NFSD_MAY_READ;
1850 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1851 flags |= NFSD_MAY_WRITE;
1852 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1855 init_stateid(stp, fp, open);
1856 status = nfsd4_truncate(rqstp, current_fh, open);
1858 release_open_stateid(stp);
1862 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1865 * Attempt to hand out a delegation. No error return, because the
1866 * OPEN succeeds even if we fail.
1868 nfs4_open_delegation(current_fh, open, stp);
1872 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1873 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1874 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1878 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1879 nfs4_set_claim_prev(open);
1881 * To finish the open response, we just need to set the rflags.
1883 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1884 if (!open->op_stateowner->so_confirmed)
1885 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1891 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1894 struct nfs4_client *clp;
1898 dprintk("process_renew(%08x/%08x): starting\n",
1899 clid->cl_boot, clid->cl_id);
1900 status = nfserr_stale_clientid;
1901 if (STALE_CLIENTID(clid))
1903 clp = find_confirmed_client(clid);
1904 status = nfserr_expired;
1906 /* We assume the client took too long to RENEW. */
1907 dprintk("nfsd4_renew: clientid not found!\n");
1911 status = nfserr_cb_path_down;
1912 if (!list_empty(&clp->cl_delegations)
1913 && !atomic_read(&clp->cl_callback.cb_set))
1917 nfs4_unlock_state();
1921 struct lock_manager nfsd4_manager = {
1925 nfsd4_end_grace(void)
1927 dprintk("NFSD: end of grace period\n");
1928 nfsd4_recdir_purge_old();
1929 locks_end_grace(&nfsd4_manager);
1933 nfs4_laundromat(void)
1935 struct nfs4_client *clp;
1936 struct nfs4_stateowner *sop;
1937 struct nfs4_delegation *dp;
1938 struct list_head *pos, *next, reaplist;
1939 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1940 time_t t, clientid_val = NFSD_LEASE_TIME;
1941 time_t u, test_val = NFSD_LEASE_TIME;
1945 dprintk("NFSD: laundromat service - starting\n");
1946 if (locks_in_grace())
1948 list_for_each_safe(pos, next, &client_lru) {
1949 clp = list_entry(pos, struct nfs4_client, cl_lru);
1950 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1951 t = clp->cl_time - cutoff;
1952 if (clientid_val > t)
1956 dprintk("NFSD: purging unused client (clientid %08x)\n",
1957 clp->cl_clientid.cl_id);
1958 nfsd4_remove_clid_dir(clp);
1961 INIT_LIST_HEAD(&reaplist);
1962 spin_lock(&recall_lock);
1963 list_for_each_safe(pos, next, &del_recall_lru) {
1964 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1965 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1966 u = dp->dl_time - cutoff;
1971 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1973 list_move(&dp->dl_recall_lru, &reaplist);
1975 spin_unlock(&recall_lock);
1976 list_for_each_safe(pos, next, &reaplist) {
1977 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1978 list_del_init(&dp->dl_recall_lru);
1979 unhash_delegation(dp);
1981 test_val = NFSD_LEASE_TIME;
1982 list_for_each_safe(pos, next, &close_lru) {
1983 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1984 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1985 u = sop->so_time - cutoff;
1990 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1992 release_openowner(sop);
1994 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1995 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1996 nfs4_unlock_state();
1997 return clientid_val;
2000 static struct workqueue_struct *laundry_wq;
2001 static void laundromat_main(struct work_struct *);
2002 static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
2005 laundromat_main(struct work_struct *not_used)
2009 t = nfs4_laundromat();
2010 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
2011 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
2014 static struct nfs4_stateowner *
2015 search_close_lru(u32 st_id, int flags)
2017 struct nfs4_stateowner *local = NULL;
2019 if (flags & CLOSE_STATE) {
2020 list_for_each_entry(local, &close_lru, so_close_lru) {
2021 if (local->so_id == st_id)
2029 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
2031 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_path.dentry->d_inode;
2035 STALE_STATEID(stateid_t *stateid)
2037 if (stateid->si_boot == boot_time)
2039 dprintk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
2040 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2041 stateid->si_generation);
2046 access_permit_read(unsigned long access_bmap)
2048 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
2049 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
2050 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
2054 access_permit_write(unsigned long access_bmap)
2056 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
2057 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
2061 __be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
2063 __be32 status = nfserr_openmode;
2065 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2067 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2074 static inline __be32
2075 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2077 if (ONE_STATEID(stateid) && (flags & RD_STATE))
2079 else if (locks_in_grace()) {
2080 /* Answer in remaining cases depends on existance of
2081 * conflicting state; so we must wait out the grace period. */
2082 return nfserr_grace;
2083 } else if (flags & WR_STATE)
2084 return nfs4_share_conflict(current_fh,
2085 NFS4_SHARE_DENY_WRITE);
2086 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2087 return nfs4_share_conflict(current_fh,
2088 NFS4_SHARE_DENY_READ);
2092 * Allow READ/WRITE during grace period on recovered state only for files
2093 * that are not able to provide mandatory locking.
2096 grace_disallows_io(struct inode *inode)
2098 return locks_in_grace() && mandatory_lock(inode);
2101 static int check_stateid_generation(stateid_t *in, stateid_t *ref)
2103 /* If the client sends us a stateid from the future, it's buggy: */
2104 if (in->si_generation > ref->si_generation)
2105 return nfserr_bad_stateid;
2107 * The following, however, can happen. For example, if the
2108 * client sends an open and some IO at the same time, the open
2109 * may bump si_generation while the IO is still in flight.
2110 * Thanks to hard links and renames, the client never knows what
2111 * file an open will affect. So it could avoid that situation
2112 * only by serializing all opens and IO from the same open
2113 * owner. To recover from the old_stateid error, the client
2114 * will just have to retry the IO:
2116 if (in->si_generation < ref->si_generation)
2117 return nfserr_old_stateid;
2121 static int is_delegation_stateid(stateid_t *stateid)
2123 return stateid->si_fileid == 0;
2127 * Checks for stateid operations
2130 nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2132 struct nfs4_stateid *stp = NULL;
2133 struct nfs4_delegation *dp = NULL;
2134 struct inode *ino = current_fh->fh_dentry->d_inode;
2140 if (grace_disallows_io(ino))
2141 return nfserr_grace;
2143 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2144 return check_special_stateids(current_fh, stateid, flags);
2146 status = nfserr_stale_stateid;
2147 if (STALE_STATEID(stateid))
2150 status = nfserr_bad_stateid;
2151 if (is_delegation_stateid(stateid)) {
2152 dp = find_delegation_stateid(ino, stateid);
2155 status = check_stateid_generation(stateid, &dp->dl_stateid);
2158 status = nfs4_check_delegmode(dp, flags);
2161 renew_client(dp->dl_client);
2163 *filpp = dp->dl_vfs_file;
2164 } else { /* open or lock stateid */
2165 stp = find_stateid(stateid, flags);
2168 if (nfs4_check_fh(current_fh, stp))
2170 if (!stp->st_stateowner->so_confirmed)
2172 status = check_stateid_generation(stateid, &stp->st_stateid);
2175 status = nfs4_check_openmode(stp, flags);
2178 renew_client(stp->st_stateowner->so_client);
2180 *filpp = stp->st_vfs_file;
2190 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
2191 RD_STATE : WR_STATE;
2195 * Checks for sequence id mutating operations.
2198 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)
2200 struct nfs4_stateid *stp;
2201 struct nfs4_stateowner *sop;
2204 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2205 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2206 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2207 stateid->si_generation);
2212 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2213 dprintk("NFSD: preprocess_seqid_op: magic stateid!\n");
2214 return nfserr_bad_stateid;
2217 if (STALE_STATEID(stateid))
2218 return nfserr_stale_stateid;
2220 * We return BAD_STATEID if filehandle doesn't match stateid,
2221 * the confirmed flag is incorrecly set, or the generation
2222 * number is incorrect.
2224 stp = find_stateid(stateid, flags);
2227 * Also, we should make sure this isn't just the result of
2230 sop = search_close_lru(stateid->si_stateownerid, flags);
2232 return nfserr_bad_stateid;
2238 *sopp = sop = stp->st_stateowner;
2241 clientid_t *lockclid = &lock->v.new.clientid;
2242 struct nfs4_client *clp = sop->so_client;
2246 lkflg = setlkflg(lock->lk_type);
2248 if (lock->lk_is_new) {
2249 if (!sop->so_is_open_owner)
2250 return nfserr_bad_stateid;
2251 if (!same_clid(&clp->cl_clientid, lockclid))
2252 return nfserr_bad_stateid;
2253 /* stp is the open stateid */
2254 status = nfs4_check_openmode(stp, lkflg);
2258 /* stp is the lock stateid */
2259 status = nfs4_check_openmode(stp->st_openstp, lkflg);
2265 if (nfs4_check_fh(current_fh, stp)) {
2266 dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
2267 return nfserr_bad_stateid;
2271 * We now validate the seqid and stateid generation numbers.
2272 * For the moment, we ignore the possibility of
2273 * generation number wraparound.
2275 if (seqid != sop->so_seqid)
2278 if (sop->so_confirmed && flags & CONFIRM) {
2279 dprintk("NFSD: preprocess_seqid_op: expected"
2280 " unconfirmed stateowner!\n");
2281 return nfserr_bad_stateid;
2283 if (!sop->so_confirmed && !(flags & CONFIRM)) {
2284 dprintk("NFSD: preprocess_seqid_op: stateowner not"
2285 " confirmed yet!\n");
2286 return nfserr_bad_stateid;
2288 status = check_stateid_generation(stateid, &stp->st_stateid);
2291 renew_client(sop->so_client);
2295 if (seqid == sop->so_seqid - 1) {
2296 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
2297 /* indicate replay to calling function */
2298 return nfserr_replay_me;
2300 dprintk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
2301 sop->so_seqid, seqid);
2303 return nfserr_bad_seqid;
2307 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2308 struct nfsd4_open_confirm *oc)
2311 struct nfs4_stateowner *sop;
2312 struct nfs4_stateid *stp;
2314 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
2315 (int)cstate->current_fh.fh_dentry->d_name.len,
2316 cstate->current_fh.fh_dentry->d_name.name);
2318 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
2324 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2325 oc->oc_seqid, &oc->oc_req_stateid,
2326 CONFIRM | OPEN_STATE,
2327 &oc->oc_stateowner, &stp, NULL)))
2330 sop = oc->oc_stateowner;
2331 sop->so_confirmed = 1;
2332 update_stateid(&stp->st_stateid);
2333 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2334 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2335 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2336 stp->st_stateid.si_boot,
2337 stp->st_stateid.si_stateownerid,
2338 stp->st_stateid.si_fileid,
2339 stp->st_stateid.si_generation);
2341 nfsd4_create_clid_dir(sop->so_client);
2343 if (oc->oc_stateowner) {
2344 nfs4_get_stateowner(oc->oc_stateowner);
2345 cstate->replay_owner = oc->oc_stateowner;
2347 nfs4_unlock_state();
2353 * unset all bits in union bitmap (bmap) that
2354 * do not exist in share (from successful OPEN_DOWNGRADE)
2357 reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2360 for (i = 1; i < 4; i++) {
2361 if ((i & access) != i)
2362 __clear_bit(i, bmap);
2367 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2370 for (i = 0; i < 4; i++) {
2371 if ((i & deny) != i)
2372 __clear_bit(i, bmap);
2377 nfsd4_open_downgrade(struct svc_rqst *rqstp,
2378 struct nfsd4_compound_state *cstate,
2379 struct nfsd4_open_downgrade *od)
2382 struct nfs4_stateid *stp;
2383 unsigned int share_access;
2385 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
2386 (int)cstate->current_fh.fh_dentry->d_name.len,
2387 cstate->current_fh.fh_dentry->d_name.name);
2389 if (!access_valid(od->od_share_access)
2390 || !deny_valid(od->od_share_deny))
2391 return nfserr_inval;
2394 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2398 &od->od_stateowner, &stp, NULL)))
2401 status = nfserr_inval;
2402 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2403 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2404 stp->st_access_bmap, od->od_share_access);
2407 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2408 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2409 stp->st_deny_bmap, od->od_share_deny);
2412 set_access(&share_access, stp->st_access_bmap);
2413 nfs4_file_downgrade(stp->st_vfs_file,
2414 share_access & ~od->od_share_access);
2416 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2417 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2419 update_stateid(&stp->st_stateid);
2420 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2423 if (od->od_stateowner) {
2424 nfs4_get_stateowner(od->od_stateowner);
2425 cstate->replay_owner = od->od_stateowner;
2427 nfs4_unlock_state();
2432 * nfs4_unlock_state() called after encode
2435 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2436 struct nfsd4_close *close)
2439 struct nfs4_stateid *stp;
2441 dprintk("NFSD: nfsd4_close on file %.*s\n",
2442 (int)cstate->current_fh.fh_dentry->d_name.len,
2443 cstate->current_fh.fh_dentry->d_name.name);
2446 /* check close_lru for replay */
2447 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2450 OPEN_STATE | CLOSE_STATE,
2451 &close->cl_stateowner, &stp, NULL)))
2454 update_stateid(&stp->st_stateid);
2455 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2457 /* release_stateid() calls nfsd_close() if needed */
2458 release_open_stateid(stp);
2460 /* place unused nfs4_stateowners on so_close_lru list to be
2461 * released by the laundromat service after the lease period
2462 * to enable us to handle CLOSE replay
2464 if (list_empty(&close->cl_stateowner->so_stateids))
2465 move_to_close_lru(close->cl_stateowner);
2467 if (close->cl_stateowner) {
2468 nfs4_get_stateowner(close->cl_stateowner);
2469 cstate->replay_owner = close->cl_stateowner;
2471 nfs4_unlock_state();
2476 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2477 struct nfsd4_delegreturn *dr)
2479 struct nfs4_delegation *dp;
2480 stateid_t *stateid = &dr->dr_stateid;
2481 struct inode *inode;
2484 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
2486 inode = cstate->current_fh.fh_dentry->d_inode;
2489 status = nfserr_bad_stateid;
2490 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2492 status = nfserr_stale_stateid;
2493 if (STALE_STATEID(stateid))
2495 status = nfserr_bad_stateid;
2496 if (!is_delegation_stateid(stateid))
2498 dp = find_delegation_stateid(inode, stateid);
2501 status = check_stateid_generation(stateid, &dp->dl_stateid);
2504 renew_client(dp->dl_client);
2506 unhash_delegation(dp);
2508 nfs4_unlock_state();
2515 * Lock owner state (byte-range locks)
2517 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2518 #define LOCK_HASH_BITS 8
2519 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2520 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2523 end_offset(u64 start, u64 len)
2528 return end >= start ? end: NFS4_MAX_UINT64;
2531 /* last octet in a range */
2533 last_byte_offset(u64 start, u64 len)
2539 return end > start ? end - 1: NFS4_MAX_UINT64;
2542 #define lockownerid_hashval(id) \
2543 ((id) & LOCK_HASH_MASK)
2545 static inline unsigned int
2546 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2547 struct xdr_netobj *ownername)
2549 return (file_hashval(inode) + cl_id
2550 + opaque_hashval(ownername->data, ownername->len))
2554 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2555 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2556 static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2558 static struct nfs4_stateid *
2559 find_stateid(stateid_t *stid, int flags)
2561 struct nfs4_stateid *local;
2562 u32 st_id = stid->si_stateownerid;
2563 u32 f_id = stid->si_fileid;
2564 unsigned int hashval;
2566 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
2567 if (flags & (LOCK_STATE | RD_STATE | WR_STATE)) {
2568 hashval = stateid_hashval(st_id, f_id);
2569 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2570 if ((local->st_stateid.si_stateownerid == st_id) &&
2571 (local->st_stateid.si_fileid == f_id))
2576 if (flags & (OPEN_STATE | RD_STATE | WR_STATE)) {
2577 hashval = stateid_hashval(st_id, f_id);
2578 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2579 if ((local->st_stateid.si_stateownerid == st_id) &&
2580 (local->st_stateid.si_fileid == f_id))
2587 static struct nfs4_delegation *
2588 find_delegation_stateid(struct inode *ino, stateid_t *stid)
2590 struct nfs4_file *fp;
2591 struct nfs4_delegation *dl;
2593 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2594 stid->si_boot, stid->si_stateownerid,
2595 stid->si_fileid, stid->si_generation);
2597 fp = find_file(ino);
2600 dl = find_delegation_file(fp, stid);
2606 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2607 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2608 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2609 * locking, this prevents us from being completely protocol-compliant. The
2610 * real solution to this problem is to start using unsigned file offsets in
2611 * the VFS, but this is a very deep change!
2614 nfs4_transform_lock_offset(struct file_lock *lock)
2616 if (lock->fl_start < 0)
2617 lock->fl_start = OFFSET_MAX;
2618 if (lock->fl_end < 0)
2619 lock->fl_end = OFFSET_MAX;
2622 /* Hack!: For now, we're defining this just so we can use a pointer to it
2623 * as a unique cookie to identify our (NFSv4's) posix locks. */
2624 static struct lock_manager_operations nfsd_posix_mng_ops = {
2628 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2630 struct nfs4_stateowner *sop;
2633 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
2634 sop = (struct nfs4_stateowner *) fl->fl_owner;
2635 hval = lockownerid_hashval(sop->so_id);
2636 kref_get(&sop->so_ref);
2638 deny->ld_clientid = sop->so_client->cl_clientid;
2640 deny->ld_sop = NULL;
2641 deny->ld_clientid.cl_boot = 0;
2642 deny->ld_clientid.cl_id = 0;
2644 deny->ld_start = fl->fl_start;
2645 deny->ld_length = NFS4_MAX_UINT64;
2646 if (fl->fl_end != NFS4_MAX_UINT64)
2647 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2648 deny->ld_type = NFS4_READ_LT;
2649 if (fl->fl_type != F_RDLCK)
2650 deny->ld_type = NFS4_WRITE_LT;
2653 static struct nfs4_stateowner *
2654 find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2655 struct xdr_netobj *owner)
2657 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2658 struct nfs4_stateowner *op;
2660 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
2661 if (same_owner_str(op, owner, clid))
2668 * Alloc a lock owner structure.
2669 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2672 * strhashval = lock_ownerstr_hashval
2675 static struct nfs4_stateowner *
2676 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2677 struct nfs4_stateowner *sop;
2678 struct nfs4_replay *rp;
2679 unsigned int idhashval;
2681 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2683 idhashval = lockownerid_hashval(current_ownerid);
2684 INIT_LIST_HEAD(&sop->so_idhash);
2685 INIT_LIST_HEAD(&sop->so_strhash);
2686 INIT_LIST_HEAD(&sop->so_perclient);
2687 INIT_LIST_HEAD(&sop->so_stateids);
2688 INIT_LIST_HEAD(&sop->so_perstateid);
2689 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2691 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2692 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
2693 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
2694 sop->so_is_open_owner = 0;
2695 sop->so_id = current_ownerid++;
2696 sop->so_client = clp;
2697 /* It is the openowner seqid that will be incremented in encode in the
2698 * case of new lockowners; so increment the lock seqid manually: */
2699 sop->so_seqid = lock->lk_new_lock_seqid + 1;
2700 sop->so_confirmed = 1;
2701 rp = &sop->so_replay;
2702 rp->rp_status = nfserr_serverfault;
2704 rp->rp_buf = rp->rp_ibuf;
2708 static struct nfs4_stateid *
2709 alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2711 struct nfs4_stateid *stp;
2712 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2714 stp = nfs4_alloc_stateid();
2717 INIT_LIST_HEAD(&stp->st_hash);
2718 INIT_LIST_HEAD(&stp->st_perfile);
2719 INIT_LIST_HEAD(&stp->st_perstateowner);
2720 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
2721 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
2722 list_add(&stp->st_perfile, &fp->fi_stateids);
2723 list_add(&stp->st_perstateowner, &sop->so_stateids);
2724 stp->st_stateowner = sop;
2727 stp->st_stateid.si_boot = boot_time;
2728 stp->st_stateid.si_stateownerid = sop->so_id;
2729 stp->st_stateid.si_fileid = fp->fi_id;
2730 stp->st_stateid.si_generation = 0;
2731 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2732 stp->st_access_bmap = open_stp->st_access_bmap;
2733 stp->st_deny_bmap = open_stp->st_deny_bmap;
2734 stp->st_openstp = open_stp;
2741 check_lock_length(u64 offset, u64 length)
2743 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
2744 LOFF_OVERFLOW(offset, length)));
2751 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2752 struct nfsd4_lock *lock)
2754 struct nfs4_stateowner *open_sop = NULL;
2755 struct nfs4_stateowner *lock_sop = NULL;
2756 struct nfs4_stateid *lock_stp;
2758 struct file_lock file_lock;
2759 struct file_lock conflock;
2761 unsigned int strhashval;
2765 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2766 (long long) lock->lk_offset,
2767 (long long) lock->lk_length);
2769 if (check_lock_length(lock->lk_offset, lock->lk_length))
2770 return nfserr_inval;
2772 if ((status = fh_verify(rqstp, &cstate->current_fh,
2773 S_IFREG, NFSD_MAY_LOCK))) {
2774 dprintk("NFSD: nfsd4_lock: permission denied!\n");
2780 if (lock->lk_is_new) {
2782 * Client indicates that this is a new lockowner.
2783 * Use open owner and open stateid to create lock owner and
2786 struct nfs4_stateid *open_stp = NULL;
2787 struct nfs4_file *fp;
2789 status = nfserr_stale_clientid;
2790 if (STALE_CLIENTID(&lock->lk_new_clientid))
2793 /* validate and update open stateid and open seqid */
2794 status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2795 lock->lk_new_open_seqid,
2796 &lock->lk_new_open_stateid,
2798 &lock->lk_replay_owner, &open_stp,
2802 open_sop = lock->lk_replay_owner;
2803 /* create lockowner and lock stateid */
2804 fp = open_stp->st_file;
2805 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2806 open_sop->so_client->cl_clientid.cl_id,
2807 &lock->v.new.owner);
2808 /* XXX: Do we need to check for duplicate stateowners on
2809 * the same file, or should they just be allowed (and
2810 * create new stateids)? */
2811 status = nfserr_resource;
2812 lock_sop = alloc_init_lock_stateowner(strhashval,
2813 open_sop->so_client, open_stp, lock);
2814 if (lock_sop == NULL)
2816 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
2817 if (lock_stp == NULL)
2820 /* lock (lock owner + lock stateid) already exists */
2821 status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2822 lock->lk_old_lock_seqid,
2823 &lock->lk_old_lock_stateid,
2825 &lock->lk_replay_owner, &lock_stp, lock);
2828 lock_sop = lock->lk_replay_owner;
2830 /* lock->lk_replay_owner and lock_stp have been created or found */
2831 filp = lock_stp->st_vfs_file;
2833 status = nfserr_grace;
2834 if (locks_in_grace() && !lock->lk_reclaim)
2836 status = nfserr_no_grace;
2837 if (!locks_in_grace() && lock->lk_reclaim)
2840 locks_init_lock(&file_lock);
2841 switch (lock->lk_type) {
2844 file_lock.fl_type = F_RDLCK;
2848 case NFS4_WRITEW_LT:
2849 file_lock.fl_type = F_WRLCK;
2853 status = nfserr_inval;
2856 file_lock.fl_owner = (fl_owner_t)lock_sop;
2857 file_lock.fl_pid = current->tgid;
2858 file_lock.fl_file = filp;
2859 file_lock.fl_flags = FL_POSIX;
2860 file_lock.fl_lmops = &nfsd_posix_mng_ops;
2862 file_lock.fl_start = lock->lk_offset;
2863 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
2864 nfs4_transform_lock_offset(&file_lock);
2867 * Try to lock the file in the VFS.
2868 * Note: locks.c uses the BKL to protect the inode's lock list.
2871 err = vfs_lock_file(filp, cmd, &file_lock, &conflock);
2873 case 0: /* success! */
2874 update_stateid(&lock_stp->st_stateid);
2875 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2879 case (EAGAIN): /* conflock holds conflicting lock */
2880 status = nfserr_denied;
2881 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2882 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
2885 status = nfserr_deadlock;
2888 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
2889 status = nfserr_resource;
2893 if (status && lock->lk_is_new && lock_sop)
2894 release_lockowner(lock_sop);
2895 if (lock->lk_replay_owner) {
2896 nfs4_get_stateowner(lock->lk_replay_owner);
2897 cstate->replay_owner = lock->lk_replay_owner;
2899 nfs4_unlock_state();
2904 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
2905 * so we do a temporary open here just to get an open file to pass to
2906 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
2909 static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
2914 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
2917 err = vfs_test_lock(file, lock);
2926 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2927 struct nfsd4_lockt *lockt)
2929 struct inode *inode;
2930 struct file_lock file_lock;
2934 if (locks_in_grace())
2935 return nfserr_grace;
2937 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2938 return nfserr_inval;
2940 lockt->lt_stateowner = NULL;
2943 status = nfserr_stale_clientid;
2944 if (STALE_CLIENTID(&lockt->lt_clientid))
2947 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) {
2948 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
2949 if (status == nfserr_symlink)
2950 status = nfserr_inval;
2954 inode = cstate->current_fh.fh_dentry->d_inode;
2955 locks_init_lock(&file_lock);
2956 switch (lockt->lt_type) {
2959 file_lock.fl_type = F_RDLCK;
2962 case NFS4_WRITEW_LT:
2963 file_lock.fl_type = F_WRLCK;
2966 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
2967 status = nfserr_inval;
2971 lockt->lt_stateowner = find_lockstateowner_str(inode,
2972 &lockt->lt_clientid, &lockt->lt_owner);
2973 if (lockt->lt_stateowner)
2974 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2975 file_lock.fl_pid = current->tgid;
2976 file_lock.fl_flags = FL_POSIX;
2978 file_lock.fl_start = lockt->lt_offset;
2979 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
2981 nfs4_transform_lock_offset(&file_lock);
2984 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
2986 status = nfserrno(error);
2989 if (file_lock.fl_type != F_UNLCK) {
2990 status = nfserr_denied;
2991 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
2994 nfs4_unlock_state();
2999 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3000 struct nfsd4_locku *locku)
3002 struct nfs4_stateid *stp;
3003 struct file *filp = NULL;
3004 struct file_lock file_lock;
3008 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
3009 (long long) locku->lu_offset,
3010 (long long) locku->lu_length);
3012 if (check_lock_length(locku->lu_offset, locku->lu_length))
3013 return nfserr_inval;
3017 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
3021 &locku->lu_stateowner, &stp, NULL)))
3024 filp = stp->st_vfs_file;
3026 locks_init_lock(&file_lock);
3027 file_lock.fl_type = F_UNLCK;
3028 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
3029 file_lock.fl_pid = current->tgid;
3030 file_lock.fl_file = filp;
3031 file_lock.fl_flags = FL_POSIX;
3032 file_lock.fl_lmops = &nfsd_posix_mng_ops;
3033 file_lock.fl_start = locku->lu_offset;
3035 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
3036 nfs4_transform_lock_offset(&file_lock);
3039 * Try to unlock the file in the VFS.
3041 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
3043 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
3047 * OK, unlock succeeded; the only thing left to do is update the stateid.
3049 update_stateid(&stp->st_stateid);
3050 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
3053 if (locku->lu_stateowner) {
3054 nfs4_get_stateowner(locku->lu_stateowner);
3055 cstate->replay_owner = locku->lu_stateowner;
3057 nfs4_unlock_state();
3061 status = nfserrno(err);
3067 * 1: locks held by lockowner
3068 * 0: no locks held by lockowner
3071 check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
3073 struct file_lock **flpp;
3074 struct inode *inode = filp->f_path.dentry->d_inode;
3078 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
3079 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
3090 nfsd4_release_lockowner(struct svc_rqst *rqstp,
3091 struct nfsd4_compound_state *cstate,
3092 struct nfsd4_release_lockowner *rlockowner)
3094 clientid_t *clid = &rlockowner->rl_clientid;
3095 struct nfs4_stateowner *sop;
3096 struct nfs4_stateid *stp;
3097 struct xdr_netobj *owner = &rlockowner->rl_owner;
3098 struct list_head matches;
3102 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
3103 clid->cl_boot, clid->cl_id);
3105 /* XXX check for lease expiration */
3107 status = nfserr_stale_clientid;
3108 if (STALE_CLIENTID(clid))
3113 status = nfserr_locks_held;
3114 /* XXX: we're doing a linear search through all the lockowners.
3115 * Yipes! For now we'll just hope clients aren't really using
3116 * release_lockowner much, but eventually we have to fix these
3117 * data structures. */
3118 INIT_LIST_HEAD(&matches);
3119 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3120 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
3121 if (!same_owner_str(sop, owner, clid))
3123 list_for_each_entry(stp, &sop->so_stateids,
3125 if (check_for_locks(stp->st_vfs_file, sop))
3127 /* Note: so_perclient unused for lockowners,
3128 * so it's OK to fool with here. */
3129 list_add(&sop->so_perclient, &matches);
3133 /* Clients probably won't expect us to return with some (but not all)
3134 * of the lockowner state released; so don't release any until all
3135 * have been checked. */
3137 while (!list_empty(&matches)) {
3138 sop = list_entry(matches.next, struct nfs4_stateowner,
3140 /* unhash_stateowner deletes so_perclient only
3141 * for openowners. */
3142 list_del(&sop->so_perclient);
3143 release_lockowner(sop);
3146 nfs4_unlock_state();
3150 static inline struct nfs4_client_reclaim *
3153 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
3157 nfs4_has_reclaimed_state(const char *name)
3159 unsigned int strhashval = clientstr_hashval(name);
3160 struct nfs4_client *clp;
3162 clp = find_confirmed_client_by_str(name, strhashval);
3167 * failure => all reset bets are off, nfserr_no_grace...
3170 nfs4_client_to_reclaim(const char *name)
3172 unsigned int strhashval;
3173 struct nfs4_client_reclaim *crp = NULL;
3175 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
3176 crp = alloc_reclaim();
3179 strhashval = clientstr_hashval(name);
3180 INIT_LIST_HEAD(&crp->cr_strhash);
3181 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
3182 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
3183 reclaim_str_hashtbl_size++;
3188 nfs4_release_reclaim(void)
3190 struct nfs4_client_reclaim *crp = NULL;
3193 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3194 while (!list_empty(&reclaim_str_hashtbl[i])) {
3195 crp = list_entry(reclaim_str_hashtbl[i].next,
3196 struct nfs4_client_reclaim, cr_strhash);
3197 list_del(&crp->cr_strhash);
3199 reclaim_str_hashtbl_size--;
3202 BUG_ON(reclaim_str_hashtbl_size);
3206 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
3207 static struct nfs4_client_reclaim *
3208 nfs4_find_reclaim_client(clientid_t *clid)
3210 unsigned int strhashval;
3211 struct nfs4_client *clp;
3212 struct nfs4_client_reclaim *crp = NULL;
3215 /* find clientid in conf_id_hashtbl */
3216 clp = find_confirmed_client(clid);
3220 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3221 clp->cl_name.len, clp->cl_name.data,
3224 /* find clp->cl_name in reclaim_str_hashtbl */
3225 strhashval = clientstr_hashval(clp->cl_recdir);
3226 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
3227 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
3235 * Called from OPEN. Look for clientid in reclaim list.
3238 nfs4_check_open_reclaim(clientid_t *clid)
3240 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
3243 /* initialization to perform at module load time: */
3246 nfs4_state_init(void)
3250 status = nfsd4_init_slabs();
3253 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3254 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3255 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3256 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3257 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3259 for (i = 0; i < SESSION_HASH_SIZE; i++)
3260 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
3261 for (i = 0; i < FILE_HASH_SIZE; i++) {
3262 INIT_LIST_HEAD(&file_hashtbl[i]);
3264 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3265 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3266 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3268 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3269 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3270 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3272 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3273 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3274 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3276 memset(&onestateid, ~0, sizeof(stateid_t));
3277 INIT_LIST_HEAD(&close_lru);
3278 INIT_LIST_HEAD(&client_lru);
3279 INIT_LIST_HEAD(&del_recall_lru);
3280 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3281 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3282 reclaim_str_hashtbl_size = 0;
3287 nfsd4_load_reboot_recovery_data(void)
3292 nfsd4_init_recdir(user_recovery_dirname);
3293 status = nfsd4_recdir_load();
3294 nfs4_unlock_state();
3296 printk("NFSD: Failure reading reboot recovery data\n");
3300 get_nfs4_grace_period(void)
3302 return max(user_lease_time, lease_time) * HZ;
3306 * Since the lifetime of a delegation isn't limited to that of an open, a
3307 * client may quite reasonably hang on to a delegation as long as it has
3308 * the inode cached. This becomes an obvious problem the first time a
3309 * client's inode cache approaches the size of the server's total memory.
3311 * For now we avoid this problem by imposing a hard limit on the number
3312 * of delegations, which varies according to the server's memory size.
3315 set_max_delegations(void)
3318 * Allow at most 4 delegations per megabyte of RAM. Quick
3319 * estimates suggest that in the worst case (where every delegation
3320 * is for a different inode), a delegation could take about 1.5K,
3321 * giving a worst case usage of about 6% of memory.
3323 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
3326 /* initialization to perform when the nfsd service is started: */
3329 __nfs4_state_start(void)
3331 unsigned long grace_time;
3333 boot_time = get_seconds();
3334 grace_time = get_nfs4_grace_period();
3335 lease_time = user_lease_time;
3336 locks_start_grace(&nfsd4_manager);
3337 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
3339 laundry_wq = create_singlethread_workqueue("nfsd4");
3340 queue_delayed_work(laundry_wq, &laundromat_work, grace_time);
3341 set_max_delegations();
3345 nfs4_state_start(void)
3349 nfsd4_load_reboot_recovery_data();
3350 __nfs4_state_start();
3356 nfs4_lease_time(void)
3362 __nfs4_state_shutdown(void)
3365 struct nfs4_client *clp = NULL;
3366 struct nfs4_delegation *dp = NULL;
3367 struct list_head *pos, *next, reaplist;
3369 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3370 while (!list_empty(&conf_id_hashtbl[i])) {
3371 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3374 while (!list_empty(&unconf_str_hashtbl[i])) {
3375 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3379 INIT_LIST_HEAD(&reaplist);
3380 spin_lock(&recall_lock);
3381 list_for_each_safe(pos, next, &del_recall_lru) {
3382 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3383 list_move(&dp->dl_recall_lru, &reaplist);
3385 spin_unlock(&recall_lock);
3386 list_for_each_safe(pos, next, &reaplist) {
3387 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3388 list_del_init(&dp->dl_recall_lru);
3389 unhash_delegation(dp);
3392 nfsd4_shutdown_recdir();
3397 nfs4_state_shutdown(void)
3399 cancel_rearming_delayed_workqueue(laundry_wq, &laundromat_work);
3400 destroy_workqueue(laundry_wq);
3401 locks_end_grace(&nfsd4_manager);
3403 nfs4_release_reclaim();
3404 __nfs4_state_shutdown();
3405 nfs4_unlock_state();
3409 * user_recovery_dirname is protected by the nfsd_mutex since it's only
3410 * accessed when nfsd is starting.
3413 nfs4_set_recdir(char *recdir)
3415 strcpy(user_recovery_dirname, recdir);
3419 * Change the NFSv4 recovery directory to recdir.
3422 nfs4_reset_recoverydir(char *recdir)
3427 status = kern_path(recdir, LOOKUP_FOLLOW, &path);
3431 if (S_ISDIR(path.dentry->d_inode->i_mode)) {
3432 nfs4_set_recdir(recdir);
3440 nfs4_recoverydir(void)
3442 return user_recovery_dirname;
3446 * Called when leasetime is changed.
3448 * The only way the protocol gives us to handle on-the-fly lease changes is to
3449 * simulate a reboot. Instead of doing that, we just wait till the next time
3450 * we start to register any changes in lease time. If the administrator
3451 * really wants to change the lease time *now*, they can go ahead and bring
3452 * nfsd down and then back up again after changing the lease time.
3454 * user_lease_time is protected by nfsd_mutex since it's only really accessed
3455 * when nfsd is starting
3458 nfs4_reset_lease(time_t leasetime)
3460 user_lease_time = leasetime;