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);
444 static void nfsd4_release_respages(struct page **respages, short resused);
447 free_session(struct kref *kref)
449 struct nfsd4_session *ses;
452 ses = container_of(kref, struct nfsd4_session, se_ref);
453 for (i = 0; i < ses->se_fnumslots; i++) {
454 struct nfsd4_cache_entry *e = &ses->se_slots[i].sl_cache_entry;
455 nfsd4_release_respages(e->ce_respages, e->ce_resused);
457 kfree(ses->se_slots);
462 renew_client(struct nfs4_client *clp)
465 * Move client to the end to the LRU list.
467 dprintk("renewing client (clientid %08x/%08x)\n",
468 clp->cl_clientid.cl_boot,
469 clp->cl_clientid.cl_id);
470 list_move_tail(&clp->cl_lru, &client_lru);
471 clp->cl_time = get_seconds();
474 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
476 STALE_CLIENTID(clientid_t *clid)
478 if (clid->cl_boot == boot_time)
480 dprintk("NFSD stale clientid (%08x/%08x)\n",
481 clid->cl_boot, clid->cl_id);
486 * XXX Should we use a slab cache ?
487 * This type of memory management is somewhat inefficient, but we use it
488 * anyway since SETCLIENTID is not a common operation.
490 static struct nfs4_client *alloc_client(struct xdr_netobj name)
492 struct nfs4_client *clp;
494 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
497 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
498 if (clp->cl_name.data == NULL) {
502 memcpy(clp->cl_name.data, name.data, name.len);
503 clp->cl_name.len = name.len;
508 shutdown_callback_client(struct nfs4_client *clp)
510 struct rpc_clnt *clnt = clp->cl_callback.cb_client;
514 * Callback threads take a reference on the client, so there
515 * should be no outstanding callbacks at this point.
517 clp->cl_callback.cb_client = NULL;
518 rpc_shutdown_client(clnt);
523 free_client(struct nfs4_client *clp)
525 shutdown_callback_client(clp);
526 if (clp->cl_cred.cr_group_info)
527 put_group_info(clp->cl_cred.cr_group_info);
528 kfree(clp->cl_principal);
529 kfree(clp->cl_name.data);
534 put_nfs4_client(struct nfs4_client *clp)
536 if (atomic_dec_and_test(&clp->cl_count))
541 expire_client(struct nfs4_client *clp)
543 struct nfs4_stateowner *sop;
544 struct nfs4_delegation *dp;
545 struct list_head reaplist;
547 dprintk("NFSD: expire_client cl_count %d\n",
548 atomic_read(&clp->cl_count));
550 INIT_LIST_HEAD(&reaplist);
551 spin_lock(&recall_lock);
552 while (!list_empty(&clp->cl_delegations)) {
553 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
554 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
556 list_del_init(&dp->dl_perclnt);
557 list_move(&dp->dl_recall_lru, &reaplist);
559 spin_unlock(&recall_lock);
560 while (!list_empty(&reaplist)) {
561 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
562 list_del_init(&dp->dl_recall_lru);
563 unhash_delegation(dp);
565 list_del(&clp->cl_idhash);
566 list_del(&clp->cl_strhash);
567 list_del(&clp->cl_lru);
568 while (!list_empty(&clp->cl_openowners)) {
569 sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
570 release_openowner(sop);
572 while (!list_empty(&clp->cl_sessions)) {
573 struct nfsd4_session *ses;
574 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
576 release_session(ses);
578 put_nfs4_client(clp);
581 static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir)
583 struct nfs4_client *clp;
585 clp = alloc_client(name);
588 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
589 atomic_set(&clp->cl_count, 1);
590 atomic_set(&clp->cl_callback.cb_set, 0);
591 INIT_LIST_HEAD(&clp->cl_idhash);
592 INIT_LIST_HEAD(&clp->cl_strhash);
593 INIT_LIST_HEAD(&clp->cl_openowners);
594 INIT_LIST_HEAD(&clp->cl_delegations);
595 INIT_LIST_HEAD(&clp->cl_sessions);
596 INIT_LIST_HEAD(&clp->cl_lru);
600 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
602 memcpy(target->cl_verifier.data, source->data,
603 sizeof(target->cl_verifier.data));
606 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
608 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
609 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
612 static void copy_cred(struct svc_cred *target, struct svc_cred *source)
614 target->cr_uid = source->cr_uid;
615 target->cr_gid = source->cr_gid;
616 target->cr_group_info = source->cr_group_info;
617 get_group_info(target->cr_group_info);
620 static int same_name(const char *n1, const char *n2)
622 return 0 == memcmp(n1, n2, HEXDIR_LEN);
626 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
628 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
632 same_clid(clientid_t *cl1, clientid_t *cl2)
634 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
637 /* XXX what about NGROUP */
639 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
641 return cr1->cr_uid == cr2->cr_uid;
644 static void gen_clid(struct nfs4_client *clp)
646 static u32 current_clientid = 1;
648 clp->cl_clientid.cl_boot = boot_time;
649 clp->cl_clientid.cl_id = current_clientid++;
652 static void gen_confirm(struct nfs4_client *clp)
657 p = (u32 *)clp->cl_confirm.data;
658 *p++ = get_seconds();
662 static int check_name(struct xdr_netobj name)
666 if (name.len > NFS4_OPAQUE_LIMIT) {
667 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
674 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
676 unsigned int idhashval;
678 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
679 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
680 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
681 list_add_tail(&clp->cl_lru, &client_lru);
682 clp->cl_time = get_seconds();
686 move_to_confirmed(struct nfs4_client *clp)
688 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
689 unsigned int strhashval;
691 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
692 list_del_init(&clp->cl_strhash);
693 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
694 strhashval = clientstr_hashval(clp->cl_recdir);
695 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
699 static struct nfs4_client *
700 find_confirmed_client(clientid_t *clid)
702 struct nfs4_client *clp;
703 unsigned int idhashval = clientid_hashval(clid->cl_id);
705 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
706 if (same_clid(&clp->cl_clientid, clid))
712 static struct nfs4_client *
713 find_unconfirmed_client(clientid_t *clid)
715 struct nfs4_client *clp;
716 unsigned int idhashval = clientid_hashval(clid->cl_id);
718 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
719 if (same_clid(&clp->cl_clientid, clid))
726 * Return 1 iff clp's clientid establishment method matches the use_exchange_id
727 * parameter. Matching is based on the fact the at least one of the
728 * EXCHGID4_FLAG_USE_{NON_PNFS,PNFS_MDS,PNFS_DS} flags must be set for v4.1
730 * FIXME: we need to unify the clientid namespaces for nfsv4.x
731 * and correctly deal with client upgrade/downgrade in EXCHANGE_ID
732 * and SET_CLIENTID{,_CONFIRM}
735 match_clientid_establishment(struct nfs4_client *clp, bool use_exchange_id)
737 bool has_exchange_flags = (clp->cl_exchange_flags != 0);
738 return use_exchange_id == has_exchange_flags;
741 static struct nfs4_client *
742 find_confirmed_client_by_str(const char *dname, unsigned int hashval,
743 bool use_exchange_id)
745 struct nfs4_client *clp;
747 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
748 if (same_name(clp->cl_recdir, dname) &&
749 match_clientid_establishment(clp, use_exchange_id))
755 static struct nfs4_client *
756 find_unconfirmed_client_by_str(const char *dname, unsigned int hashval,
757 bool use_exchange_id)
759 struct nfs4_client *clp;
761 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
762 if (same_name(clp->cl_recdir, dname) &&
763 match_clientid_establishment(clp, use_exchange_id))
769 /* a helper function for parse_callback */
771 parse_octet(unsigned int *lenp, char **addrp)
773 unsigned int len = *lenp;
785 if ((c < '0') || (c > '9')) {
791 n = (n * 10) + (c - '0');
802 /* parse and set the setclientid ipv4 callback address */
804 parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
809 u32 addrlen = addr_len;
810 char *addr = addr_val;
815 for(i = 4; i > 0 ; i--) {
816 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
819 cbaddr |= (temp << shift);
827 for(i = 2; i > 0 ; i--) {
828 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
831 cbport |= (temp << shift);
840 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
842 struct nfs4_callback *cb = &clp->cl_callback;
844 /* Currently, we only support tcp for the callback channel */
845 if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
848 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
849 &cb->cb_addr, &cb->cb_port)))
851 cb->cb_prog = se->se_callback_prog;
852 cb->cb_ident = se->se_callback_ident;
855 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
856 "will not receive delegations\n",
857 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
863 nfsd4_set_statp(struct svc_rqst *rqstp, __be32 *statp)
865 struct nfsd4_compoundres *resp = rqstp->rq_resp;
867 resp->cstate.statp = statp;
871 * Dereference the result pages.
874 nfsd4_release_respages(struct page **respages, short resused)
878 dprintk("--> %s\n", __func__);
879 for (i = 0; i < resused; i++) {
882 put_page(respages[i]);
888 nfsd4_copy_pages(struct page **topages, struct page **frompages, short count)
892 for (i = 0; i < count; i++) {
893 topages[i] = frompages[i];
896 get_page(topages[i]);
901 * Cache the reply pages up to NFSD_PAGES_PER_SLOT + 1, clearing the previous
902 * pages. We add a page to NFSD_PAGES_PER_SLOT for the case where the total
903 * length of the XDR response is less than se_fmaxresp_cached
904 * (NFSD_PAGES_PER_SLOT * PAGE_SIZE) but the xdr_buf pages is used for a
905 * of the reply (e.g. readdir).
907 * Store the base and length of the rq_req.head[0] page
908 * of the NFSv4.1 data, just past the rpc header.
911 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
913 struct nfsd4_cache_entry *entry = &resp->cstate.slot->sl_cache_entry;
914 struct svc_rqst *rqstp = resp->rqstp;
915 struct nfsd4_compoundargs *args = rqstp->rq_argp;
916 struct nfsd4_op *op = &args->ops[resp->opcnt];
917 struct kvec *resv = &rqstp->rq_res.head[0];
919 dprintk("--> %s entry %p\n", __func__, entry);
921 /* Don't cache a failed OP_SEQUENCE. */
922 if (resp->opcnt == 1 && op->opnum == OP_SEQUENCE && resp->cstate.status)
924 nfsd4_release_respages(entry->ce_respages, entry->ce_resused);
925 entry->ce_resused = rqstp->rq_resused;
926 if (entry->ce_resused > NFSD_PAGES_PER_SLOT + 1)
927 entry->ce_resused = NFSD_PAGES_PER_SLOT + 1;
928 nfsd4_copy_pages(entry->ce_respages, rqstp->rq_respages,
930 entry->ce_status = resp->cstate.status;
931 entry->ce_datav.iov_base = resp->cstate.statp;
932 entry->ce_datav.iov_len = resv->iov_len - ((char *)resp->cstate.statp -
933 (char *)page_address(rqstp->rq_respages[0]));
934 entry->ce_opcnt = resp->opcnt;
935 /* Current request rpc header length*/
936 entry->ce_rpchdrlen = (char *)resp->cstate.statp -
937 (char *)page_address(rqstp->rq_respages[0]);
941 * We keep the rpc header, but take the nfs reply from the replycache.
944 nfsd41_copy_replay_data(struct nfsd4_compoundres *resp,
945 struct nfsd4_cache_entry *entry)
947 struct svc_rqst *rqstp = resp->rqstp;
948 struct kvec *resv = &resp->rqstp->rq_res.head[0];
951 /* Current request rpc header length*/
952 len = (char *)resp->cstate.statp -
953 (char *)page_address(rqstp->rq_respages[0]);
954 if (entry->ce_datav.iov_len + len > PAGE_SIZE) {
955 dprintk("%s v41 cached reply too large (%Zd).\n", __func__,
956 entry->ce_datav.iov_len);
959 /* copy the cached reply nfsd data past the current rpc header */
960 memcpy((char *)resv->iov_base + len, entry->ce_datav.iov_base,
961 entry->ce_datav.iov_len);
962 resv->iov_len = len + entry->ce_datav.iov_len;
967 * Keep the first page of the replay. Copy the NFSv4.1 data from the first
968 * cached page. Replace any futher replay pages from the cache.
971 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp)
973 struct nfsd4_cache_entry *entry = &resp->cstate.slot->sl_cache_entry;
976 dprintk("--> %s entry %p\n", __func__, entry);
979 if (!nfsd41_copy_replay_data(resp, entry)) {
981 * Not enough room to use the replay rpc header, send the
982 * cached header. Release all the allocated result pages.
984 svc_free_res_pages(resp->rqstp);
985 nfsd4_copy_pages(resp->rqstp->rq_respages, entry->ce_respages,
988 /* Release all but the first allocated result page */
990 resp->rqstp->rq_resused--;
991 svc_free_res_pages(resp->rqstp);
993 nfsd4_copy_pages(&resp->rqstp->rq_respages[1],
994 &entry->ce_respages[1],
995 entry->ce_resused - 1);
998 resp->rqstp->rq_resused = entry->ce_resused;
999 resp->opcnt = entry->ce_opcnt;
1000 resp->cstate.iovlen = entry->ce_datav.iov_len + entry->ce_rpchdrlen;
1001 status = entry->ce_status;
1007 * Set the exchange_id flags returned by the server.
1010 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1012 /* pNFS is not supported */
1013 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1015 /* Referrals are supported, Migration is not. */
1016 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1018 /* set the wire flags to return to client. */
1019 clid->flags = new->cl_exchange_flags;
1023 nfsd4_exchange_id(struct svc_rqst *rqstp,
1024 struct nfsd4_compound_state *cstate,
1025 struct nfsd4_exchange_id *exid)
1027 struct nfs4_client *unconf, *conf, *new;
1029 unsigned int strhashval;
1030 char dname[HEXDIR_LEN];
1031 nfs4_verifier verf = exid->verifier;
1032 u32 ip_addr = svc_addr_in(rqstp)->sin_addr.s_addr;
1034 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1035 " ip_addr=%u flags %x, spa_how %d\n",
1036 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
1037 ip_addr, exid->flags, exid->spa_how);
1039 if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A))
1040 return nfserr_inval;
1042 /* Currently only support SP4_NONE */
1043 switch (exid->spa_how) {
1047 return nfserr_encr_alg_unsupp;
1049 BUG(); /* checked by xdr code */
1051 return nfserr_serverfault; /* no excuse :-/ */
1054 status = nfs4_make_rec_clidname(dname, &exid->clname);
1059 strhashval = clientstr_hashval(dname);
1064 conf = find_confirmed_client_by_str(dname, strhashval, true);
1066 if (!same_verf(&verf, &conf->cl_verifier)) {
1067 /* 18.35.4 case 8 */
1068 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1069 status = nfserr_not_same;
1072 /* Client reboot: destroy old state */
1073 expire_client(conf);
1076 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1077 /* 18.35.4 case 9 */
1078 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1079 status = nfserr_perm;
1082 expire_client(conf);
1085 if (ip_addr != conf->cl_addr &&
1086 !(exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A)) {
1087 /* Client collision. 18.35.4 case 3 */
1088 status = nfserr_clid_inuse;
1092 * Set bit when the owner id and verifier map to an already
1093 * confirmed client id (18.35.3).
1095 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1098 * Falling into 18.35.4 case 2, possible router replay.
1099 * Leave confirmed record intact and return same result.
1101 copy_verf(conf, &verf);
1105 /* 18.35.4 case 7 */
1106 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1107 status = nfserr_noent;
1112 unconf = find_unconfirmed_client_by_str(dname, strhashval, true);
1115 * Possible retry or client restart. Per 18.35.4 case 4,
1116 * a new unconfirmed record should be generated regardless
1117 * of whether any properties have changed.
1119 expire_client(unconf);
1124 new = create_client(exid->clname, dname);
1126 status = nfserr_resource;
1130 copy_verf(new, &verf);
1131 copy_cred(&new->cl_cred, &rqstp->rq_cred);
1132 new->cl_addr = ip_addr;
1135 add_to_unconfirmed(new, strhashval);
1137 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1138 exid->clientid.cl_id = new->cl_clientid.cl_id;
1140 new->cl_seqid = exid->seqid = 1;
1141 nfsd4_set_ex_flags(new, exid);
1143 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1144 new->cl_seqid, new->cl_exchange_flags);
1148 nfs4_unlock_state();
1150 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1155 check_slot_seqid(u32 seqid, struct nfsd4_slot *slot)
1157 dprintk("%s enter. seqid %d slot->sl_seqid %d\n", __func__, seqid,
1160 /* The slot is in use, and no response has been sent. */
1161 if (slot->sl_inuse) {
1162 if (seqid == slot->sl_seqid)
1163 return nfserr_jukebox;
1165 return nfserr_seq_misordered;
1168 if (likely(seqid == slot->sl_seqid + 1))
1171 if (seqid == slot->sl_seqid)
1172 return nfserr_replay_cache;
1174 if (seqid == 1 && (slot->sl_seqid + 1) == 0)
1176 /* Misordered replay or misordered new request */
1177 return nfserr_seq_misordered;
1181 nfsd4_create_session(struct svc_rqst *rqstp,
1182 struct nfsd4_compound_state *cstate,
1183 struct nfsd4_create_session *cr_ses)
1185 return -1; /* stub */
1189 nfsd4_destroy_session(struct svc_rqst *r,
1190 struct nfsd4_compound_state *cstate,
1191 struct nfsd4_destroy_session *sessionid)
1193 return -1; /* stub */
1197 nfsd4_sequence(struct svc_rqst *rqstp,
1198 struct nfsd4_compound_state *cstate,
1199 struct nfsd4_sequence *seq)
1201 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1202 struct nfsd4_session *session;
1203 struct nfsd4_slot *slot;
1206 if (resp->opcnt != 1)
1207 return nfserr_sequence_pos;
1209 spin_lock(&sessionid_lock);
1210 status = nfserr_badsession;
1211 session = find_in_sessionid_hashtbl(&seq->sessionid);
1215 status = nfserr_badslot;
1216 if (seq->slotid >= session->se_fnumslots)
1219 slot = &session->se_slots[seq->slotid];
1220 dprintk("%s: slotid %d\n", __func__, seq->slotid);
1222 status = check_slot_seqid(seq->seqid, slot);
1223 if (status == nfserr_replay_cache) {
1224 cstate->slot = slot;
1225 cstate->session = session;
1226 /* Return the cached reply status and set cstate->status
1227 * for nfsd4_svc_encode_compoundres processing*/
1228 status = nfsd4_replay_cache_entry(resp);
1229 cstate->status = nfserr_replay_cache;
1235 /* Success! bump slot seqid */
1236 slot->sl_inuse = true;
1237 slot->sl_seqid = seq->seqid;
1239 cstate->slot = slot;
1240 cstate->session = session;
1243 /* Renew the clientid on success and on replay.
1244 * Hold a session reference until done processing the compound:
1245 * nfsd4_put_session called only if the cstate slot is set.
1247 renew_client(session->se_client);
1248 nfsd4_get_session(session);
1250 spin_unlock(&sessionid_lock);
1251 dprintk("%s: return %d\n", __func__, ntohl(status));
1256 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1257 struct nfsd4_setclientid *setclid)
1259 struct sockaddr_in *sin = svc_addr_in(rqstp);
1260 struct xdr_netobj clname = {
1261 .len = setclid->se_namelen,
1262 .data = setclid->se_name,
1264 nfs4_verifier clverifier = setclid->se_verf;
1265 unsigned int strhashval;
1266 struct nfs4_client *conf, *unconf, *new;
1269 char dname[HEXDIR_LEN];
1271 if (!check_name(clname))
1272 return nfserr_inval;
1274 status = nfs4_make_rec_clidname(dname, &clname);
1279 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1280 * We get here on a DRC miss.
1283 strhashval = clientstr_hashval(dname);
1286 conf = find_confirmed_client_by_str(dname, strhashval, false);
1288 /* RFC 3530 14.2.33 CASE 0: */
1289 status = nfserr_clid_inuse;
1290 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1291 dprintk("NFSD: setclientid: string in use by client"
1292 " at %pI4\n", &conf->cl_addr);
1297 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
1298 * has a description of SETCLIENTID request processing consisting
1299 * of 5 bullet points, labeled as CASE0 - CASE4 below.
1301 unconf = find_unconfirmed_client_by_str(dname, strhashval, false);
1302 status = nfserr_resource;
1305 * RFC 3530 14.2.33 CASE 4:
1306 * placed first, because it is the normal case
1309 expire_client(unconf);
1310 new = create_client(clname, dname);
1314 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
1316 * RFC 3530 14.2.33 CASE 1:
1317 * probable callback update
1320 /* Note this is removing unconfirmed {*x***},
1321 * which is stronger than RFC recommended {vxc**}.
1322 * This has the advantage that there is at most
1323 * one {*x***} in either list at any time.
1325 expire_client(unconf);
1327 new = create_client(clname, dname);
1330 copy_clid(new, conf);
1331 } else if (!unconf) {
1333 * RFC 3530 14.2.33 CASE 2:
1334 * probable client reboot; state will be removed if
1337 new = create_client(clname, dname);
1343 * RFC 3530 14.2.33 CASE 3:
1344 * probable client reboot; state will be removed if
1347 expire_client(unconf);
1348 new = create_client(clname, dname);
1353 copy_verf(new, &clverifier);
1354 new->cl_addr = sin->sin_addr.s_addr;
1355 new->cl_flavor = rqstp->rq_flavor;
1356 princ = svc_gss_principal(rqstp);
1358 new->cl_principal = kstrdup(princ, GFP_KERNEL);
1359 if (new->cl_principal == NULL) {
1364 copy_cred(&new->cl_cred, &rqstp->rq_cred);
1366 gen_callback(new, setclid);
1367 add_to_unconfirmed(new, strhashval);
1368 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
1369 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
1370 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
1373 nfs4_unlock_state();
1379 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
1380 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
1381 * bullets, labeled as CASE1 - CASE4 below.
1384 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
1385 struct nfsd4_compound_state *cstate,
1386 struct nfsd4_setclientid_confirm *setclientid_confirm)
1388 struct sockaddr_in *sin = svc_addr_in(rqstp);
1389 struct nfs4_client *conf, *unconf;
1390 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
1391 clientid_t * clid = &setclientid_confirm->sc_clientid;
1394 if (STALE_CLIENTID(clid))
1395 return nfserr_stale_clientid;
1397 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1398 * We get here on a DRC miss.
1403 conf = find_confirmed_client(clid);
1404 unconf = find_unconfirmed_client(clid);
1406 status = nfserr_clid_inuse;
1407 if (conf && conf->cl_addr != sin->sin_addr.s_addr)
1409 if (unconf && unconf->cl_addr != sin->sin_addr.s_addr)
1413 * section 14.2.34 of RFC 3530 has a description of
1414 * SETCLIENTID_CONFIRM request processing consisting
1415 * of 4 bullet points, labeled as CASE1 - CASE4 below.
1417 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
1419 * RFC 3530 14.2.34 CASE 1:
1422 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
1423 status = nfserr_clid_inuse;
1425 /* XXX: We just turn off callbacks until we can handle
1426 * change request correctly. */
1427 atomic_set(&conf->cl_callback.cb_set, 0);
1429 nfsd4_remove_clid_dir(unconf);
1430 expire_client(unconf);
1434 } else if (conf && !unconf) {
1436 * RFC 3530 14.2.34 CASE 2:
1437 * probable retransmitted request; play it safe and
1440 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
1441 status = nfserr_clid_inuse;
1444 } else if (!conf && unconf
1445 && same_verf(&unconf->cl_confirm, &confirm)) {
1447 * RFC 3530 14.2.34 CASE 3:
1448 * Normal case; new or rebooted client:
1450 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
1451 status = nfserr_clid_inuse;
1454 clientstr_hashval(unconf->cl_recdir);
1455 conf = find_confirmed_client_by_str(unconf->cl_recdir,
1458 nfsd4_remove_clid_dir(conf);
1459 expire_client(conf);
1461 move_to_confirmed(unconf);
1463 nfsd4_probe_callback(conf);
1466 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
1467 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
1470 * RFC 3530 14.2.34 CASE 4:
1471 * Client probably hasn't noticed that we rebooted yet.
1473 status = nfserr_stale_clientid;
1475 /* check that we have hit one of the cases...*/
1476 status = nfserr_clid_inuse;
1479 nfs4_unlock_state();
1483 /* OPEN Share state helper functions */
1484 static inline struct nfs4_file *
1485 alloc_init_file(struct inode *ino)
1487 struct nfs4_file *fp;
1488 unsigned int hashval = file_hashval(ino);
1490 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
1492 atomic_set(&fp->fi_ref, 1);
1493 INIT_LIST_HEAD(&fp->fi_hash);
1494 INIT_LIST_HEAD(&fp->fi_stateids);
1495 INIT_LIST_HEAD(&fp->fi_delegations);
1496 spin_lock(&recall_lock);
1497 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
1498 spin_unlock(&recall_lock);
1499 fp->fi_inode = igrab(ino);
1500 fp->fi_id = current_fileid++;
1501 fp->fi_had_conflict = false;
1508 nfsd4_free_slab(struct kmem_cache **slab)
1512 kmem_cache_destroy(*slab);
1517 nfsd4_free_slabs(void)
1519 nfsd4_free_slab(&stateowner_slab);
1520 nfsd4_free_slab(&file_slab);
1521 nfsd4_free_slab(&stateid_slab);
1522 nfsd4_free_slab(&deleg_slab);
1526 nfsd4_init_slabs(void)
1528 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
1529 sizeof(struct nfs4_stateowner), 0, 0, NULL);
1530 if (stateowner_slab == NULL)
1532 file_slab = kmem_cache_create("nfsd4_files",
1533 sizeof(struct nfs4_file), 0, 0, NULL);
1534 if (file_slab == NULL)
1536 stateid_slab = kmem_cache_create("nfsd4_stateids",
1537 sizeof(struct nfs4_stateid), 0, 0, NULL);
1538 if (stateid_slab == NULL)
1540 deleg_slab = kmem_cache_create("nfsd4_delegations",
1541 sizeof(struct nfs4_delegation), 0, 0, NULL);
1542 if (deleg_slab == NULL)
1547 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1552 nfs4_free_stateowner(struct kref *kref)
1554 struct nfs4_stateowner *sop =
1555 container_of(kref, struct nfs4_stateowner, so_ref);
1556 kfree(sop->so_owner.data);
1557 kmem_cache_free(stateowner_slab, sop);
1560 static inline struct nfs4_stateowner *
1561 alloc_stateowner(struct xdr_netobj *owner)
1563 struct nfs4_stateowner *sop;
1565 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1566 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1567 memcpy(sop->so_owner.data, owner->data, owner->len);
1568 sop->so_owner.len = owner->len;
1569 kref_init(&sop->so_ref);
1572 kmem_cache_free(stateowner_slab, sop);
1577 static struct nfs4_stateowner *
1578 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1579 struct nfs4_stateowner *sop;
1580 struct nfs4_replay *rp;
1581 unsigned int idhashval;
1583 if (!(sop = alloc_stateowner(&open->op_owner)))
1585 idhashval = ownerid_hashval(current_ownerid);
1586 INIT_LIST_HEAD(&sop->so_idhash);
1587 INIT_LIST_HEAD(&sop->so_strhash);
1588 INIT_LIST_HEAD(&sop->so_perclient);
1589 INIT_LIST_HEAD(&sop->so_stateids);
1590 INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
1591 INIT_LIST_HEAD(&sop->so_close_lru);
1593 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1594 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
1595 list_add(&sop->so_perclient, &clp->cl_openowners);
1596 sop->so_is_open_owner = 1;
1597 sop->so_id = current_ownerid++;
1598 sop->so_client = clp;
1599 sop->so_seqid = open->op_seqid;
1600 sop->so_confirmed = 0;
1601 rp = &sop->so_replay;
1602 rp->rp_status = nfserr_serverfault;
1604 rp->rp_buf = rp->rp_ibuf;
1609 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1610 struct nfs4_stateowner *sop = open->op_stateowner;
1611 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1613 INIT_LIST_HEAD(&stp->st_hash);
1614 INIT_LIST_HEAD(&stp->st_perstateowner);
1615 INIT_LIST_HEAD(&stp->st_lockowners);
1616 INIT_LIST_HEAD(&stp->st_perfile);
1617 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
1618 list_add(&stp->st_perstateowner, &sop->so_stateids);
1619 list_add(&stp->st_perfile, &fp->fi_stateids);
1620 stp->st_stateowner = sop;
1623 stp->st_stateid.si_boot = boot_time;
1624 stp->st_stateid.si_stateownerid = sop->so_id;
1625 stp->st_stateid.si_fileid = fp->fi_id;
1626 stp->st_stateid.si_generation = 0;
1627 stp->st_access_bmap = 0;
1628 stp->st_deny_bmap = 0;
1629 __set_bit(open->op_share_access, &stp->st_access_bmap);
1630 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
1631 stp->st_openstp = NULL;
1635 move_to_close_lru(struct nfs4_stateowner *sop)
1637 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1639 list_move_tail(&sop->so_close_lru, &close_lru);
1640 sop->so_time = get_seconds();
1644 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
1647 return (sop->so_owner.len == owner->len) &&
1648 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
1649 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
1652 static struct nfs4_stateowner *
1653 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1655 struct nfs4_stateowner *so = NULL;
1657 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
1658 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
1664 /* search file_hashtbl[] for file */
1665 static struct nfs4_file *
1666 find_file(struct inode *ino)
1668 unsigned int hashval = file_hashval(ino);
1669 struct nfs4_file *fp;
1671 spin_lock(&recall_lock);
1672 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
1673 if (fp->fi_inode == ino) {
1675 spin_unlock(&recall_lock);
1679 spin_unlock(&recall_lock);
1683 static inline int access_valid(u32 x)
1685 if (x < NFS4_SHARE_ACCESS_READ)
1687 if (x > NFS4_SHARE_ACCESS_BOTH)
1692 static inline int deny_valid(u32 x)
1694 /* Note: unlike access bits, deny bits may be zero. */
1695 return x <= NFS4_SHARE_DENY_BOTH;
1699 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1700 * st_{access,deny}_bmap field of the stateid, in order to track not
1701 * only what share bits are currently in force, but also what
1702 * combinations of share bits previous opens have used. This allows us
1703 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1704 * return an error if the client attempt to downgrade to a combination
1705 * of share bits not explicable by closing some of its previous opens.
1707 * XXX: This enforcement is actually incomplete, since we don't keep
1708 * track of access/deny bit combinations; so, e.g., we allow:
1710 * OPEN allow read, deny write
1711 * OPEN allow both, deny none
1712 * DOWNGRADE allow read, deny none
1714 * which we should reject.
1717 set_access(unsigned int *access, unsigned long bmap) {
1721 for (i = 1; i < 4; i++) {
1722 if (test_bit(i, &bmap))
1728 set_deny(unsigned int *deny, unsigned long bmap) {
1732 for (i = 0; i < 4; i++) {
1733 if (test_bit(i, &bmap))
1739 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1740 unsigned int access, deny;
1742 set_access(&access, stp->st_access_bmap);
1743 set_deny(&deny, stp->st_deny_bmap);
1744 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1750 * Called to check deny when READ with all zero stateid or
1751 * WRITE with all zero or all one stateid
1754 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1756 struct inode *ino = current_fh->fh_dentry->d_inode;
1757 struct nfs4_file *fp;
1758 struct nfs4_stateid *stp;
1761 dprintk("NFSD: nfs4_share_conflict\n");
1763 fp = find_file(ino);
1766 ret = nfserr_locked;
1767 /* Search for conflicting share reservations */
1768 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1769 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1770 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1780 nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1782 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1783 drop_file_write_access(filp);
1784 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1789 * Recall a delegation
1792 do_recall(void *__dp)
1794 struct nfs4_delegation *dp = __dp;
1796 dp->dl_file->fi_had_conflict = true;
1797 nfsd4_cb_recall(dp);
1802 * Spawn a thread to perform a recall on the delegation represented
1803 * by the lease (file_lock)
1805 * Called from break_lease() with lock_kernel() held.
1806 * Note: we assume break_lease will only call this *once* for any given
1810 void nfsd_break_deleg_cb(struct file_lock *fl)
1812 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1813 struct task_struct *t;
1815 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1819 /* We're assuming the state code never drops its reference
1820 * without first removing the lease. Since we're in this lease
1821 * callback (and since the lease code is serialized by the kernel
1822 * lock) we know the server hasn't removed the lease yet, we know
1823 * it's safe to take a reference: */
1824 atomic_inc(&dp->dl_count);
1825 atomic_inc(&dp->dl_client->cl_count);
1827 spin_lock(&recall_lock);
1828 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1829 spin_unlock(&recall_lock);
1831 /* only place dl_time is set. protected by lock_kernel*/
1832 dp->dl_time = get_seconds();
1835 * We don't want the locks code to timeout the lease for us;
1836 * we'll remove it ourself if the delegation isn't returned
1839 fl->fl_break_time = 0;
1841 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1843 struct nfs4_client *clp = dp->dl_client;
1845 printk(KERN_INFO "NFSD: Callback thread failed for "
1846 "for client (clientid %08x/%08x)\n",
1847 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1848 put_nfs4_client(dp->dl_client);
1849 nfs4_put_delegation(dp);
1854 * The file_lock is being reapd.
1856 * Called by locks_free_lock() with lock_kernel() held.
1859 void nfsd_release_deleg_cb(struct file_lock *fl)
1861 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1863 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1865 if (!(fl->fl_flags & FL_LEASE) || !dp)
1867 dp->dl_flock = NULL;
1871 * Set the delegation file_lock back pointer.
1873 * Called from setlease() with lock_kernel() held.
1876 void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1878 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1880 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1887 * Called from setlease() with lock_kernel() held
1890 int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1892 struct nfs4_delegation *onlistd =
1893 (struct nfs4_delegation *)onlist->fl_owner;
1894 struct nfs4_delegation *tryd =
1895 (struct nfs4_delegation *)try->fl_owner;
1897 if (onlist->fl_lmops != try->fl_lmops)
1900 return onlistd->dl_client == tryd->dl_client;
1905 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1908 return lease_modify(onlist, arg);
1913 static struct lock_manager_operations nfsd_lease_mng_ops = {
1914 .fl_break = nfsd_break_deleg_cb,
1915 .fl_release_private = nfsd_release_deleg_cb,
1916 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1917 .fl_mylease = nfsd_same_client_deleg_cb,
1918 .fl_change = nfsd_change_deleg_cb,
1923 nfsd4_process_open1(struct nfsd4_open *open)
1925 clientid_t *clientid = &open->op_clientid;
1926 struct nfs4_client *clp = NULL;
1927 unsigned int strhashval;
1928 struct nfs4_stateowner *sop = NULL;
1930 if (!check_name(open->op_owner))
1931 return nfserr_inval;
1933 if (STALE_CLIENTID(&open->op_clientid))
1934 return nfserr_stale_clientid;
1936 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1937 sop = find_openstateowner_str(strhashval, open);
1938 open->op_stateowner = sop;
1940 /* Make sure the client's lease hasn't expired. */
1941 clp = find_confirmed_client(clientid);
1943 return nfserr_expired;
1946 if (!sop->so_confirmed) {
1947 /* Replace unconfirmed owners without checking for replay. */
1948 clp = sop->so_client;
1949 release_openowner(sop);
1950 open->op_stateowner = NULL;
1953 if (open->op_seqid == sop->so_seqid - 1) {
1954 if (sop->so_replay.rp_buflen)
1955 return nfserr_replay_me;
1956 /* The original OPEN failed so spectacularly
1957 * that we don't even have replay data saved!
1958 * Therefore, we have no choice but to continue
1959 * processing this OPEN; presumably, we'll
1960 * fail again for the same reason.
1962 dprintk("nfsd4_process_open1: replay with no replay cache\n");
1965 if (open->op_seqid != sop->so_seqid)
1966 return nfserr_bad_seqid;
1968 if (open->op_stateowner == NULL) {
1969 sop = alloc_init_open_stateowner(strhashval, clp, open);
1971 return nfserr_resource;
1972 open->op_stateowner = sop;
1974 list_del_init(&sop->so_close_lru);
1975 renew_client(sop->so_client);
1979 static inline __be32
1980 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1982 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1983 return nfserr_openmode;
1988 static struct nfs4_delegation *
1989 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1991 struct nfs4_delegation *dp;
1993 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) {
1994 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
2001 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
2002 struct nfs4_delegation **dp)
2005 __be32 status = nfserr_bad_stateid;
2007 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
2010 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
2011 RD_STATE : WR_STATE;
2012 status = nfs4_check_delegmode(*dp, flags);
2016 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
2020 open->op_stateowner->so_confirmed = 1;
2025 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
2027 struct nfs4_stateid *local;
2028 __be32 status = nfserr_share_denied;
2029 struct nfs4_stateowner *sop = open->op_stateowner;
2031 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
2032 /* ignore lock owners */
2033 if (local->st_stateowner->so_is_open_owner == 0)
2035 /* remember if we have seen this open owner */
2036 if (local->st_stateowner == sop)
2038 /* check for conflicting share reservations */
2039 if (!test_share(local, open))
2047 static inline struct nfs4_stateid *
2048 nfs4_alloc_stateid(void)
2050 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
2054 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
2055 struct nfs4_delegation *dp,
2056 struct svc_fh *cur_fh, int flags)
2058 struct nfs4_stateid *stp;
2060 stp = nfs4_alloc_stateid();
2062 return nfserr_resource;
2065 get_file(dp->dl_vfs_file);
2066 stp->st_vfs_file = dp->dl_vfs_file;
2069 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
2072 if (status == nfserr_dropit)
2073 status = nfserr_jukebox;
2074 kmem_cache_free(stateid_slab, stp);
2082 static inline __be32
2083 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2084 struct nfsd4_open *open)
2086 struct iattr iattr = {
2087 .ia_valid = ATTR_SIZE,
2090 if (!open->op_truncate)
2092 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
2093 return nfserr_inval;
2094 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2098 nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
2100 struct file *filp = stp->st_vfs_file;
2101 struct inode *inode = filp->f_path.dentry->d_inode;
2102 unsigned int share_access, new_writer;
2105 set_access(&share_access, stp->st_access_bmap);
2106 new_writer = (~share_access) & open->op_share_access
2107 & NFS4_SHARE_ACCESS_WRITE;
2110 int err = get_write_access(inode);
2112 return nfserrno(err);
2113 err = mnt_want_write(cur_fh->fh_export->ex_path.mnt);
2115 return nfserrno(err);
2116 file_take_write(filp);
2118 status = nfsd4_truncate(rqstp, cur_fh, open);
2121 put_write_access(inode);
2124 /* remember the open */
2125 filp->f_mode |= open->op_share_access;
2126 __set_bit(open->op_share_access, &stp->st_access_bmap);
2127 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
2134 nfs4_set_claim_prev(struct nfsd4_open *open)
2136 open->op_stateowner->so_confirmed = 1;
2137 open->op_stateowner->so_client->cl_firststate = 1;
2141 * Attempt to hand out a delegation.
2144 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
2146 struct nfs4_delegation *dp;
2147 struct nfs4_stateowner *sop = stp->st_stateowner;
2148 struct nfs4_callback *cb = &sop->so_client->cl_callback;
2149 struct file_lock fl, *flp = &fl;
2150 int status, flag = 0;
2152 flag = NFS4_OPEN_DELEGATE_NONE;
2153 open->op_recall = 0;
2154 switch (open->op_claim_type) {
2155 case NFS4_OPEN_CLAIM_PREVIOUS:
2156 if (!atomic_read(&cb->cb_set))
2157 open->op_recall = 1;
2158 flag = open->op_delegate_type;
2159 if (flag == NFS4_OPEN_DELEGATE_NONE)
2162 case NFS4_OPEN_CLAIM_NULL:
2163 /* Let's not give out any delegations till everyone's
2164 * had the chance to reclaim theirs.... */
2165 if (locks_in_grace())
2167 if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
2169 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2170 flag = NFS4_OPEN_DELEGATE_WRITE;
2172 flag = NFS4_OPEN_DELEGATE_READ;
2178 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
2180 flag = NFS4_OPEN_DELEGATE_NONE;
2183 locks_init_lock(&fl);
2184 fl.fl_lmops = &nfsd_lease_mng_ops;
2185 fl.fl_flags = FL_LEASE;
2186 fl.fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2187 fl.fl_end = OFFSET_MAX;
2188 fl.fl_owner = (fl_owner_t)dp;
2189 fl.fl_file = stp->st_vfs_file;
2190 fl.fl_pid = current->tgid;
2192 /* vfs_setlease checks to see if delegation should be handed out.
2193 * the lock_manager callbacks fl_mylease and fl_change are used
2195 if ((status = vfs_setlease(stp->st_vfs_file, fl.fl_type, &flp))) {
2196 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
2197 unhash_delegation(dp);
2198 flag = NFS4_OPEN_DELEGATE_NONE;
2202 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
2204 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
2205 dp->dl_stateid.si_boot,
2206 dp->dl_stateid.si_stateownerid,
2207 dp->dl_stateid.si_fileid,
2208 dp->dl_stateid.si_generation);
2210 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
2211 && flag == NFS4_OPEN_DELEGATE_NONE
2212 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
2213 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2214 open->op_delegate_type = flag;
2218 * called with nfs4_lock_state() held.
2221 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2223 struct nfs4_file *fp = NULL;
2224 struct inode *ino = current_fh->fh_dentry->d_inode;
2225 struct nfs4_stateid *stp = NULL;
2226 struct nfs4_delegation *dp = NULL;
2229 status = nfserr_inval;
2230 if (!access_valid(open->op_share_access)
2231 || !deny_valid(open->op_share_deny))
2234 * Lookup file; if found, lookup stateid and check open request,
2235 * and check for delegations in the process of being recalled.
2236 * If not found, create the nfs4_file struct
2238 fp = find_file(ino);
2240 if ((status = nfs4_check_open(fp, open, &stp)))
2242 status = nfs4_check_deleg(fp, open, &dp);
2246 status = nfserr_bad_stateid;
2247 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2249 status = nfserr_resource;
2250 fp = alloc_init_file(ino);
2256 * OPEN the file, or upgrade an existing OPEN.
2257 * If truncate fails, the OPEN fails.
2260 /* Stateid was found, this is an OPEN upgrade */
2261 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
2264 update_stateid(&stp->st_stateid);
2266 /* Stateid was not found, this is a new OPEN */
2268 if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
2269 flags |= NFSD_MAY_READ;
2270 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2271 flags |= NFSD_MAY_WRITE;
2272 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
2275 init_stateid(stp, fp, open);
2276 status = nfsd4_truncate(rqstp, current_fh, open);
2278 release_open_stateid(stp);
2282 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
2285 * Attempt to hand out a delegation. No error return, because the
2286 * OPEN succeeds even if we fail.
2288 nfs4_open_delegation(current_fh, open, stp);
2292 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
2293 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
2294 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
2298 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
2299 nfs4_set_claim_prev(open);
2301 * To finish the open response, we just need to set the rflags.
2303 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
2304 if (!open->op_stateowner->so_confirmed)
2305 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
2311 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2314 struct nfs4_client *clp;
2318 dprintk("process_renew(%08x/%08x): starting\n",
2319 clid->cl_boot, clid->cl_id);
2320 status = nfserr_stale_clientid;
2321 if (STALE_CLIENTID(clid))
2323 clp = find_confirmed_client(clid);
2324 status = nfserr_expired;
2326 /* We assume the client took too long to RENEW. */
2327 dprintk("nfsd4_renew: clientid not found!\n");
2331 status = nfserr_cb_path_down;
2332 if (!list_empty(&clp->cl_delegations)
2333 && !atomic_read(&clp->cl_callback.cb_set))
2337 nfs4_unlock_state();
2341 struct lock_manager nfsd4_manager = {
2345 nfsd4_end_grace(void)
2347 dprintk("NFSD: end of grace period\n");
2348 nfsd4_recdir_purge_old();
2349 locks_end_grace(&nfsd4_manager);
2353 nfs4_laundromat(void)
2355 struct nfs4_client *clp;
2356 struct nfs4_stateowner *sop;
2357 struct nfs4_delegation *dp;
2358 struct list_head *pos, *next, reaplist;
2359 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
2360 time_t t, clientid_val = NFSD_LEASE_TIME;
2361 time_t u, test_val = NFSD_LEASE_TIME;
2365 dprintk("NFSD: laundromat service - starting\n");
2366 if (locks_in_grace())
2368 list_for_each_safe(pos, next, &client_lru) {
2369 clp = list_entry(pos, struct nfs4_client, cl_lru);
2370 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
2371 t = clp->cl_time - cutoff;
2372 if (clientid_val > t)
2376 dprintk("NFSD: purging unused client (clientid %08x)\n",
2377 clp->cl_clientid.cl_id);
2378 nfsd4_remove_clid_dir(clp);
2381 INIT_LIST_HEAD(&reaplist);
2382 spin_lock(&recall_lock);
2383 list_for_each_safe(pos, next, &del_recall_lru) {
2384 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
2385 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
2386 u = dp->dl_time - cutoff;
2391 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
2393 list_move(&dp->dl_recall_lru, &reaplist);
2395 spin_unlock(&recall_lock);
2396 list_for_each_safe(pos, next, &reaplist) {
2397 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
2398 list_del_init(&dp->dl_recall_lru);
2399 unhash_delegation(dp);
2401 test_val = NFSD_LEASE_TIME;
2402 list_for_each_safe(pos, next, &close_lru) {
2403 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
2404 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
2405 u = sop->so_time - cutoff;
2410 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
2412 release_openowner(sop);
2414 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
2415 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
2416 nfs4_unlock_state();
2417 return clientid_val;
2420 static struct workqueue_struct *laundry_wq;
2421 static void laundromat_main(struct work_struct *);
2422 static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
2425 laundromat_main(struct work_struct *not_used)
2429 t = nfs4_laundromat();
2430 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
2431 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
2434 static struct nfs4_stateowner *
2435 search_close_lru(u32 st_id, int flags)
2437 struct nfs4_stateowner *local = NULL;
2439 if (flags & CLOSE_STATE) {
2440 list_for_each_entry(local, &close_lru, so_close_lru) {
2441 if (local->so_id == st_id)
2449 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
2451 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_path.dentry->d_inode;
2455 STALE_STATEID(stateid_t *stateid)
2457 if (stateid->si_boot == boot_time)
2459 dprintk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
2460 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2461 stateid->si_generation);
2466 access_permit_read(unsigned long access_bmap)
2468 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
2469 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
2470 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
2474 access_permit_write(unsigned long access_bmap)
2476 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
2477 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
2481 __be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
2483 __be32 status = nfserr_openmode;
2485 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2487 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2494 static inline __be32
2495 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2497 if (ONE_STATEID(stateid) && (flags & RD_STATE))
2499 else if (locks_in_grace()) {
2500 /* Answer in remaining cases depends on existance of
2501 * conflicting state; so we must wait out the grace period. */
2502 return nfserr_grace;
2503 } else if (flags & WR_STATE)
2504 return nfs4_share_conflict(current_fh,
2505 NFS4_SHARE_DENY_WRITE);
2506 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2507 return nfs4_share_conflict(current_fh,
2508 NFS4_SHARE_DENY_READ);
2512 * Allow READ/WRITE during grace period on recovered state only for files
2513 * that are not able to provide mandatory locking.
2516 grace_disallows_io(struct inode *inode)
2518 return locks_in_grace() && mandatory_lock(inode);
2521 static int check_stateid_generation(stateid_t *in, stateid_t *ref)
2523 /* If the client sends us a stateid from the future, it's buggy: */
2524 if (in->si_generation > ref->si_generation)
2525 return nfserr_bad_stateid;
2527 * The following, however, can happen. For example, if the
2528 * client sends an open and some IO at the same time, the open
2529 * may bump si_generation while the IO is still in flight.
2530 * Thanks to hard links and renames, the client never knows what
2531 * file an open will affect. So it could avoid that situation
2532 * only by serializing all opens and IO from the same open
2533 * owner. To recover from the old_stateid error, the client
2534 * will just have to retry the IO:
2536 if (in->si_generation < ref->si_generation)
2537 return nfserr_old_stateid;
2541 static int is_delegation_stateid(stateid_t *stateid)
2543 return stateid->si_fileid == 0;
2547 * Checks for stateid operations
2550 nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2552 struct nfs4_stateid *stp = NULL;
2553 struct nfs4_delegation *dp = NULL;
2554 struct inode *ino = current_fh->fh_dentry->d_inode;
2560 if (grace_disallows_io(ino))
2561 return nfserr_grace;
2563 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2564 return check_special_stateids(current_fh, stateid, flags);
2566 status = nfserr_stale_stateid;
2567 if (STALE_STATEID(stateid))
2570 status = nfserr_bad_stateid;
2571 if (is_delegation_stateid(stateid)) {
2572 dp = find_delegation_stateid(ino, stateid);
2575 status = check_stateid_generation(stateid, &dp->dl_stateid);
2578 status = nfs4_check_delegmode(dp, flags);
2581 renew_client(dp->dl_client);
2583 *filpp = dp->dl_vfs_file;
2584 } else { /* open or lock stateid */
2585 stp = find_stateid(stateid, flags);
2588 if (nfs4_check_fh(current_fh, stp))
2590 if (!stp->st_stateowner->so_confirmed)
2592 status = check_stateid_generation(stateid, &stp->st_stateid);
2595 status = nfs4_check_openmode(stp, flags);
2598 renew_client(stp->st_stateowner->so_client);
2600 *filpp = stp->st_vfs_file;
2610 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
2611 RD_STATE : WR_STATE;
2615 * Checks for sequence id mutating operations.
2618 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)
2620 struct nfs4_stateid *stp;
2621 struct nfs4_stateowner *sop;
2624 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2625 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2626 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2627 stateid->si_generation);
2632 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2633 dprintk("NFSD: preprocess_seqid_op: magic stateid!\n");
2634 return nfserr_bad_stateid;
2637 if (STALE_STATEID(stateid))
2638 return nfserr_stale_stateid;
2640 * We return BAD_STATEID if filehandle doesn't match stateid,
2641 * the confirmed flag is incorrecly set, or the generation
2642 * number is incorrect.
2644 stp = find_stateid(stateid, flags);
2647 * Also, we should make sure this isn't just the result of
2650 sop = search_close_lru(stateid->si_stateownerid, flags);
2652 return nfserr_bad_stateid;
2658 *sopp = sop = stp->st_stateowner;
2661 clientid_t *lockclid = &lock->v.new.clientid;
2662 struct nfs4_client *clp = sop->so_client;
2666 lkflg = setlkflg(lock->lk_type);
2668 if (lock->lk_is_new) {
2669 if (!sop->so_is_open_owner)
2670 return nfserr_bad_stateid;
2671 if (!same_clid(&clp->cl_clientid, lockclid))
2672 return nfserr_bad_stateid;
2673 /* stp is the open stateid */
2674 status = nfs4_check_openmode(stp, lkflg);
2678 /* stp is the lock stateid */
2679 status = nfs4_check_openmode(stp->st_openstp, lkflg);
2685 if (nfs4_check_fh(current_fh, stp)) {
2686 dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
2687 return nfserr_bad_stateid;
2691 * We now validate the seqid and stateid generation numbers.
2692 * For the moment, we ignore the possibility of
2693 * generation number wraparound.
2695 if (seqid != sop->so_seqid)
2698 if (sop->so_confirmed && flags & CONFIRM) {
2699 dprintk("NFSD: preprocess_seqid_op: expected"
2700 " unconfirmed stateowner!\n");
2701 return nfserr_bad_stateid;
2703 if (!sop->so_confirmed && !(flags & CONFIRM)) {
2704 dprintk("NFSD: preprocess_seqid_op: stateowner not"
2705 " confirmed yet!\n");
2706 return nfserr_bad_stateid;
2708 status = check_stateid_generation(stateid, &stp->st_stateid);
2711 renew_client(sop->so_client);
2715 if (seqid == sop->so_seqid - 1) {
2716 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
2717 /* indicate replay to calling function */
2718 return nfserr_replay_me;
2720 dprintk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
2721 sop->so_seqid, seqid);
2723 return nfserr_bad_seqid;
2727 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2728 struct nfsd4_open_confirm *oc)
2731 struct nfs4_stateowner *sop;
2732 struct nfs4_stateid *stp;
2734 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
2735 (int)cstate->current_fh.fh_dentry->d_name.len,
2736 cstate->current_fh.fh_dentry->d_name.name);
2738 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
2744 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2745 oc->oc_seqid, &oc->oc_req_stateid,
2746 CONFIRM | OPEN_STATE,
2747 &oc->oc_stateowner, &stp, NULL)))
2750 sop = oc->oc_stateowner;
2751 sop->so_confirmed = 1;
2752 update_stateid(&stp->st_stateid);
2753 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2754 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2755 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2756 stp->st_stateid.si_boot,
2757 stp->st_stateid.si_stateownerid,
2758 stp->st_stateid.si_fileid,
2759 stp->st_stateid.si_generation);
2761 nfsd4_create_clid_dir(sop->so_client);
2763 if (oc->oc_stateowner) {
2764 nfs4_get_stateowner(oc->oc_stateowner);
2765 cstate->replay_owner = oc->oc_stateowner;
2767 nfs4_unlock_state();
2773 * unset all bits in union bitmap (bmap) that
2774 * do not exist in share (from successful OPEN_DOWNGRADE)
2777 reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2780 for (i = 1; i < 4; i++) {
2781 if ((i & access) != i)
2782 __clear_bit(i, bmap);
2787 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2790 for (i = 0; i < 4; i++) {
2791 if ((i & deny) != i)
2792 __clear_bit(i, bmap);
2797 nfsd4_open_downgrade(struct svc_rqst *rqstp,
2798 struct nfsd4_compound_state *cstate,
2799 struct nfsd4_open_downgrade *od)
2802 struct nfs4_stateid *stp;
2803 unsigned int share_access;
2805 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
2806 (int)cstate->current_fh.fh_dentry->d_name.len,
2807 cstate->current_fh.fh_dentry->d_name.name);
2809 if (!access_valid(od->od_share_access)
2810 || !deny_valid(od->od_share_deny))
2811 return nfserr_inval;
2814 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2818 &od->od_stateowner, &stp, NULL)))
2821 status = nfserr_inval;
2822 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2823 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2824 stp->st_access_bmap, od->od_share_access);
2827 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2828 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2829 stp->st_deny_bmap, od->od_share_deny);
2832 set_access(&share_access, stp->st_access_bmap);
2833 nfs4_file_downgrade(stp->st_vfs_file,
2834 share_access & ~od->od_share_access);
2836 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2837 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2839 update_stateid(&stp->st_stateid);
2840 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2843 if (od->od_stateowner) {
2844 nfs4_get_stateowner(od->od_stateowner);
2845 cstate->replay_owner = od->od_stateowner;
2847 nfs4_unlock_state();
2852 * nfs4_unlock_state() called after encode
2855 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2856 struct nfsd4_close *close)
2859 struct nfs4_stateid *stp;
2861 dprintk("NFSD: nfsd4_close on file %.*s\n",
2862 (int)cstate->current_fh.fh_dentry->d_name.len,
2863 cstate->current_fh.fh_dentry->d_name.name);
2866 /* check close_lru for replay */
2867 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2870 OPEN_STATE | CLOSE_STATE,
2871 &close->cl_stateowner, &stp, NULL)))
2874 update_stateid(&stp->st_stateid);
2875 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2877 /* release_stateid() calls nfsd_close() if needed */
2878 release_open_stateid(stp);
2880 /* place unused nfs4_stateowners on so_close_lru list to be
2881 * released by the laundromat service after the lease period
2882 * to enable us to handle CLOSE replay
2884 if (list_empty(&close->cl_stateowner->so_stateids))
2885 move_to_close_lru(close->cl_stateowner);
2887 if (close->cl_stateowner) {
2888 nfs4_get_stateowner(close->cl_stateowner);
2889 cstate->replay_owner = close->cl_stateowner;
2891 nfs4_unlock_state();
2896 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2897 struct nfsd4_delegreturn *dr)
2899 struct nfs4_delegation *dp;
2900 stateid_t *stateid = &dr->dr_stateid;
2901 struct inode *inode;
2904 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
2906 inode = cstate->current_fh.fh_dentry->d_inode;
2909 status = nfserr_bad_stateid;
2910 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2912 status = nfserr_stale_stateid;
2913 if (STALE_STATEID(stateid))
2915 status = nfserr_bad_stateid;
2916 if (!is_delegation_stateid(stateid))
2918 dp = find_delegation_stateid(inode, stateid);
2921 status = check_stateid_generation(stateid, &dp->dl_stateid);
2924 renew_client(dp->dl_client);
2926 unhash_delegation(dp);
2928 nfs4_unlock_state();
2935 * Lock owner state (byte-range locks)
2937 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2938 #define LOCK_HASH_BITS 8
2939 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2940 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2943 end_offset(u64 start, u64 len)
2948 return end >= start ? end: NFS4_MAX_UINT64;
2951 /* last octet in a range */
2953 last_byte_offset(u64 start, u64 len)
2959 return end > start ? end - 1: NFS4_MAX_UINT64;
2962 #define lockownerid_hashval(id) \
2963 ((id) & LOCK_HASH_MASK)
2965 static inline unsigned int
2966 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2967 struct xdr_netobj *ownername)
2969 return (file_hashval(inode) + cl_id
2970 + opaque_hashval(ownername->data, ownername->len))
2974 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2975 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2976 static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2978 static struct nfs4_stateid *
2979 find_stateid(stateid_t *stid, int flags)
2981 struct nfs4_stateid *local;
2982 u32 st_id = stid->si_stateownerid;
2983 u32 f_id = stid->si_fileid;
2984 unsigned int hashval;
2986 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
2987 if (flags & (LOCK_STATE | RD_STATE | WR_STATE)) {
2988 hashval = stateid_hashval(st_id, f_id);
2989 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2990 if ((local->st_stateid.si_stateownerid == st_id) &&
2991 (local->st_stateid.si_fileid == f_id))
2996 if (flags & (OPEN_STATE | RD_STATE | WR_STATE)) {
2997 hashval = stateid_hashval(st_id, f_id);
2998 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2999 if ((local->st_stateid.si_stateownerid == st_id) &&
3000 (local->st_stateid.si_fileid == f_id))
3007 static struct nfs4_delegation *
3008 find_delegation_stateid(struct inode *ino, stateid_t *stid)
3010 struct nfs4_file *fp;
3011 struct nfs4_delegation *dl;
3013 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
3014 stid->si_boot, stid->si_stateownerid,
3015 stid->si_fileid, stid->si_generation);
3017 fp = find_file(ino);
3020 dl = find_delegation_file(fp, stid);
3026 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3027 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3028 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3029 * locking, this prevents us from being completely protocol-compliant. The
3030 * real solution to this problem is to start using unsigned file offsets in
3031 * the VFS, but this is a very deep change!
3034 nfs4_transform_lock_offset(struct file_lock *lock)
3036 if (lock->fl_start < 0)
3037 lock->fl_start = OFFSET_MAX;
3038 if (lock->fl_end < 0)
3039 lock->fl_end = OFFSET_MAX;
3042 /* Hack!: For now, we're defining this just so we can use a pointer to it
3043 * as a unique cookie to identify our (NFSv4's) posix locks. */
3044 static struct lock_manager_operations nfsd_posix_mng_ops = {
3048 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3050 struct nfs4_stateowner *sop;
3053 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
3054 sop = (struct nfs4_stateowner *) fl->fl_owner;
3055 hval = lockownerid_hashval(sop->so_id);
3056 kref_get(&sop->so_ref);
3058 deny->ld_clientid = sop->so_client->cl_clientid;
3060 deny->ld_sop = NULL;
3061 deny->ld_clientid.cl_boot = 0;
3062 deny->ld_clientid.cl_id = 0;
3064 deny->ld_start = fl->fl_start;
3065 deny->ld_length = NFS4_MAX_UINT64;
3066 if (fl->fl_end != NFS4_MAX_UINT64)
3067 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3068 deny->ld_type = NFS4_READ_LT;
3069 if (fl->fl_type != F_RDLCK)
3070 deny->ld_type = NFS4_WRITE_LT;
3073 static struct nfs4_stateowner *
3074 find_lockstateowner_str(struct inode *inode, clientid_t *clid,
3075 struct xdr_netobj *owner)
3077 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
3078 struct nfs4_stateowner *op;
3080 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
3081 if (same_owner_str(op, owner, clid))
3088 * Alloc a lock owner structure.
3089 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
3092 * strhashval = lock_ownerstr_hashval
3095 static struct nfs4_stateowner *
3096 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
3097 struct nfs4_stateowner *sop;
3098 struct nfs4_replay *rp;
3099 unsigned int idhashval;
3101 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
3103 idhashval = lockownerid_hashval(current_ownerid);
3104 INIT_LIST_HEAD(&sop->so_idhash);
3105 INIT_LIST_HEAD(&sop->so_strhash);
3106 INIT_LIST_HEAD(&sop->so_perclient);
3107 INIT_LIST_HEAD(&sop->so_stateids);
3108 INIT_LIST_HEAD(&sop->so_perstateid);
3109 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
3111 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
3112 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
3113 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
3114 sop->so_is_open_owner = 0;
3115 sop->so_id = current_ownerid++;
3116 sop->so_client = clp;
3117 /* It is the openowner seqid that will be incremented in encode in the
3118 * case of new lockowners; so increment the lock seqid manually: */
3119 sop->so_seqid = lock->lk_new_lock_seqid + 1;
3120 sop->so_confirmed = 1;
3121 rp = &sop->so_replay;
3122 rp->rp_status = nfserr_serverfault;
3124 rp->rp_buf = rp->rp_ibuf;
3128 static struct nfs4_stateid *
3129 alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
3131 struct nfs4_stateid *stp;
3132 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
3134 stp = nfs4_alloc_stateid();
3137 INIT_LIST_HEAD(&stp->st_hash);
3138 INIT_LIST_HEAD(&stp->st_perfile);
3139 INIT_LIST_HEAD(&stp->st_perstateowner);
3140 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
3141 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
3142 list_add(&stp->st_perfile, &fp->fi_stateids);
3143 list_add(&stp->st_perstateowner, &sop->so_stateids);
3144 stp->st_stateowner = sop;
3147 stp->st_stateid.si_boot = boot_time;
3148 stp->st_stateid.si_stateownerid = sop->so_id;
3149 stp->st_stateid.si_fileid = fp->fi_id;
3150 stp->st_stateid.si_generation = 0;
3151 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
3152 stp->st_access_bmap = open_stp->st_access_bmap;
3153 stp->st_deny_bmap = open_stp->st_deny_bmap;
3154 stp->st_openstp = open_stp;
3161 check_lock_length(u64 offset, u64 length)
3163 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
3164 LOFF_OVERFLOW(offset, length)));
3171 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3172 struct nfsd4_lock *lock)
3174 struct nfs4_stateowner *open_sop = NULL;
3175 struct nfs4_stateowner *lock_sop = NULL;
3176 struct nfs4_stateid *lock_stp;
3178 struct file_lock file_lock;
3179 struct file_lock conflock;
3181 unsigned int strhashval;
3185 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3186 (long long) lock->lk_offset,
3187 (long long) lock->lk_length);
3189 if (check_lock_length(lock->lk_offset, lock->lk_length))
3190 return nfserr_inval;
3192 if ((status = fh_verify(rqstp, &cstate->current_fh,
3193 S_IFREG, NFSD_MAY_LOCK))) {
3194 dprintk("NFSD: nfsd4_lock: permission denied!\n");
3200 if (lock->lk_is_new) {
3202 * Client indicates that this is a new lockowner.
3203 * Use open owner and open stateid to create lock owner and
3206 struct nfs4_stateid *open_stp = NULL;
3207 struct nfs4_file *fp;
3209 status = nfserr_stale_clientid;
3210 if (STALE_CLIENTID(&lock->lk_new_clientid))
3213 /* validate and update open stateid and open seqid */
3214 status = nfs4_preprocess_seqid_op(&cstate->current_fh,
3215 lock->lk_new_open_seqid,
3216 &lock->lk_new_open_stateid,
3218 &lock->lk_replay_owner, &open_stp,
3222 open_sop = lock->lk_replay_owner;
3223 /* create lockowner and lock stateid */
3224 fp = open_stp->st_file;
3225 strhashval = lock_ownerstr_hashval(fp->fi_inode,
3226 open_sop->so_client->cl_clientid.cl_id,
3227 &lock->v.new.owner);
3228 /* XXX: Do we need to check for duplicate stateowners on
3229 * the same file, or should they just be allowed (and
3230 * create new stateids)? */
3231 status = nfserr_resource;
3232 lock_sop = alloc_init_lock_stateowner(strhashval,
3233 open_sop->so_client, open_stp, lock);
3234 if (lock_sop == NULL)
3236 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
3237 if (lock_stp == NULL)
3240 /* lock (lock owner + lock stateid) already exists */
3241 status = nfs4_preprocess_seqid_op(&cstate->current_fh,
3242 lock->lk_old_lock_seqid,
3243 &lock->lk_old_lock_stateid,
3245 &lock->lk_replay_owner, &lock_stp, lock);
3248 lock_sop = lock->lk_replay_owner;
3250 /* lock->lk_replay_owner and lock_stp have been created or found */
3251 filp = lock_stp->st_vfs_file;
3253 status = nfserr_grace;
3254 if (locks_in_grace() && !lock->lk_reclaim)
3256 status = nfserr_no_grace;
3257 if (!locks_in_grace() && lock->lk_reclaim)
3260 locks_init_lock(&file_lock);
3261 switch (lock->lk_type) {
3264 file_lock.fl_type = F_RDLCK;
3268 case NFS4_WRITEW_LT:
3269 file_lock.fl_type = F_WRLCK;
3273 status = nfserr_inval;
3276 file_lock.fl_owner = (fl_owner_t)lock_sop;
3277 file_lock.fl_pid = current->tgid;
3278 file_lock.fl_file = filp;
3279 file_lock.fl_flags = FL_POSIX;
3280 file_lock.fl_lmops = &nfsd_posix_mng_ops;
3282 file_lock.fl_start = lock->lk_offset;
3283 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
3284 nfs4_transform_lock_offset(&file_lock);
3287 * Try to lock the file in the VFS.
3288 * Note: locks.c uses the BKL to protect the inode's lock list.
3291 err = vfs_lock_file(filp, cmd, &file_lock, &conflock);
3293 case 0: /* success! */
3294 update_stateid(&lock_stp->st_stateid);
3295 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
3299 case (EAGAIN): /* conflock holds conflicting lock */
3300 status = nfserr_denied;
3301 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
3302 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
3305 status = nfserr_deadlock;
3308 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
3309 status = nfserr_resource;
3313 if (status && lock->lk_is_new && lock_sop)
3314 release_lockowner(lock_sop);
3315 if (lock->lk_replay_owner) {
3316 nfs4_get_stateowner(lock->lk_replay_owner);
3317 cstate->replay_owner = lock->lk_replay_owner;
3319 nfs4_unlock_state();
3324 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
3325 * so we do a temporary open here just to get an open file to pass to
3326 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
3329 static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
3334 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
3337 err = vfs_test_lock(file, lock);
3346 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3347 struct nfsd4_lockt *lockt)
3349 struct inode *inode;
3350 struct file_lock file_lock;
3354 if (locks_in_grace())
3355 return nfserr_grace;
3357 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
3358 return nfserr_inval;
3360 lockt->lt_stateowner = NULL;
3363 status = nfserr_stale_clientid;
3364 if (STALE_CLIENTID(&lockt->lt_clientid))
3367 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) {
3368 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
3369 if (status == nfserr_symlink)
3370 status = nfserr_inval;
3374 inode = cstate->current_fh.fh_dentry->d_inode;
3375 locks_init_lock(&file_lock);
3376 switch (lockt->lt_type) {
3379 file_lock.fl_type = F_RDLCK;
3382 case NFS4_WRITEW_LT:
3383 file_lock.fl_type = F_WRLCK;
3386 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
3387 status = nfserr_inval;
3391 lockt->lt_stateowner = find_lockstateowner_str(inode,
3392 &lockt->lt_clientid, &lockt->lt_owner);
3393 if (lockt->lt_stateowner)
3394 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
3395 file_lock.fl_pid = current->tgid;
3396 file_lock.fl_flags = FL_POSIX;
3398 file_lock.fl_start = lockt->lt_offset;
3399 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
3401 nfs4_transform_lock_offset(&file_lock);
3404 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
3406 status = nfserrno(error);
3409 if (file_lock.fl_type != F_UNLCK) {
3410 status = nfserr_denied;
3411 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
3414 nfs4_unlock_state();
3419 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3420 struct nfsd4_locku *locku)
3422 struct nfs4_stateid *stp;
3423 struct file *filp = NULL;
3424 struct file_lock file_lock;
3428 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
3429 (long long) locku->lu_offset,
3430 (long long) locku->lu_length);
3432 if (check_lock_length(locku->lu_offset, locku->lu_length))
3433 return nfserr_inval;
3437 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
3441 &locku->lu_stateowner, &stp, NULL)))
3444 filp = stp->st_vfs_file;
3446 locks_init_lock(&file_lock);
3447 file_lock.fl_type = F_UNLCK;
3448 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
3449 file_lock.fl_pid = current->tgid;
3450 file_lock.fl_file = filp;
3451 file_lock.fl_flags = FL_POSIX;
3452 file_lock.fl_lmops = &nfsd_posix_mng_ops;
3453 file_lock.fl_start = locku->lu_offset;
3455 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
3456 nfs4_transform_lock_offset(&file_lock);
3459 * Try to unlock the file in the VFS.
3461 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
3463 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
3467 * OK, unlock succeeded; the only thing left to do is update the stateid.
3469 update_stateid(&stp->st_stateid);
3470 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
3473 if (locku->lu_stateowner) {
3474 nfs4_get_stateowner(locku->lu_stateowner);
3475 cstate->replay_owner = locku->lu_stateowner;
3477 nfs4_unlock_state();
3481 status = nfserrno(err);
3487 * 1: locks held by lockowner
3488 * 0: no locks held by lockowner
3491 check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
3493 struct file_lock **flpp;
3494 struct inode *inode = filp->f_path.dentry->d_inode;
3498 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
3499 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
3510 nfsd4_release_lockowner(struct svc_rqst *rqstp,
3511 struct nfsd4_compound_state *cstate,
3512 struct nfsd4_release_lockowner *rlockowner)
3514 clientid_t *clid = &rlockowner->rl_clientid;
3515 struct nfs4_stateowner *sop;
3516 struct nfs4_stateid *stp;
3517 struct xdr_netobj *owner = &rlockowner->rl_owner;
3518 struct list_head matches;
3522 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
3523 clid->cl_boot, clid->cl_id);
3525 /* XXX check for lease expiration */
3527 status = nfserr_stale_clientid;
3528 if (STALE_CLIENTID(clid))
3533 status = nfserr_locks_held;
3534 /* XXX: we're doing a linear search through all the lockowners.
3535 * Yipes! For now we'll just hope clients aren't really using
3536 * release_lockowner much, but eventually we have to fix these
3537 * data structures. */
3538 INIT_LIST_HEAD(&matches);
3539 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3540 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
3541 if (!same_owner_str(sop, owner, clid))
3543 list_for_each_entry(stp, &sop->so_stateids,
3545 if (check_for_locks(stp->st_vfs_file, sop))
3547 /* Note: so_perclient unused for lockowners,
3548 * so it's OK to fool with here. */
3549 list_add(&sop->so_perclient, &matches);
3553 /* Clients probably won't expect us to return with some (but not all)
3554 * of the lockowner state released; so don't release any until all
3555 * have been checked. */
3557 while (!list_empty(&matches)) {
3558 sop = list_entry(matches.next, struct nfs4_stateowner,
3560 /* unhash_stateowner deletes so_perclient only
3561 * for openowners. */
3562 list_del(&sop->so_perclient);
3563 release_lockowner(sop);
3566 nfs4_unlock_state();
3570 static inline struct nfs4_client_reclaim *
3573 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
3577 nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
3579 unsigned int strhashval = clientstr_hashval(name);
3580 struct nfs4_client *clp;
3582 clp = find_confirmed_client_by_str(name, strhashval, use_exchange_id);
3587 * failure => all reset bets are off, nfserr_no_grace...
3590 nfs4_client_to_reclaim(const char *name)
3592 unsigned int strhashval;
3593 struct nfs4_client_reclaim *crp = NULL;
3595 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
3596 crp = alloc_reclaim();
3599 strhashval = clientstr_hashval(name);
3600 INIT_LIST_HEAD(&crp->cr_strhash);
3601 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
3602 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
3603 reclaim_str_hashtbl_size++;
3608 nfs4_release_reclaim(void)
3610 struct nfs4_client_reclaim *crp = NULL;
3613 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3614 while (!list_empty(&reclaim_str_hashtbl[i])) {
3615 crp = list_entry(reclaim_str_hashtbl[i].next,
3616 struct nfs4_client_reclaim, cr_strhash);
3617 list_del(&crp->cr_strhash);
3619 reclaim_str_hashtbl_size--;
3622 BUG_ON(reclaim_str_hashtbl_size);
3626 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
3627 static struct nfs4_client_reclaim *
3628 nfs4_find_reclaim_client(clientid_t *clid)
3630 unsigned int strhashval;
3631 struct nfs4_client *clp;
3632 struct nfs4_client_reclaim *crp = NULL;
3635 /* find clientid in conf_id_hashtbl */
3636 clp = find_confirmed_client(clid);
3640 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3641 clp->cl_name.len, clp->cl_name.data,
3644 /* find clp->cl_name in reclaim_str_hashtbl */
3645 strhashval = clientstr_hashval(clp->cl_recdir);
3646 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
3647 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
3655 * Called from OPEN. Look for clientid in reclaim list.
3658 nfs4_check_open_reclaim(clientid_t *clid)
3660 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
3663 /* initialization to perform at module load time: */
3666 nfs4_state_init(void)
3670 status = nfsd4_init_slabs();
3673 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3674 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3675 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3676 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3677 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3679 for (i = 0; i < SESSION_HASH_SIZE; i++)
3680 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
3681 for (i = 0; i < FILE_HASH_SIZE; i++) {
3682 INIT_LIST_HEAD(&file_hashtbl[i]);
3684 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3685 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3686 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3688 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3689 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3690 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3692 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3693 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3694 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3696 memset(&onestateid, ~0, sizeof(stateid_t));
3697 INIT_LIST_HEAD(&close_lru);
3698 INIT_LIST_HEAD(&client_lru);
3699 INIT_LIST_HEAD(&del_recall_lru);
3700 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3701 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3702 reclaim_str_hashtbl_size = 0;
3707 nfsd4_load_reboot_recovery_data(void)
3712 nfsd4_init_recdir(user_recovery_dirname);
3713 status = nfsd4_recdir_load();
3714 nfs4_unlock_state();
3716 printk("NFSD: Failure reading reboot recovery data\n");
3720 get_nfs4_grace_period(void)
3722 return max(user_lease_time, lease_time) * HZ;
3726 * Since the lifetime of a delegation isn't limited to that of an open, a
3727 * client may quite reasonably hang on to a delegation as long as it has
3728 * the inode cached. This becomes an obvious problem the first time a
3729 * client's inode cache approaches the size of the server's total memory.
3731 * For now we avoid this problem by imposing a hard limit on the number
3732 * of delegations, which varies according to the server's memory size.
3735 set_max_delegations(void)
3738 * Allow at most 4 delegations per megabyte of RAM. Quick
3739 * estimates suggest that in the worst case (where every delegation
3740 * is for a different inode), a delegation could take about 1.5K,
3741 * giving a worst case usage of about 6% of memory.
3743 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
3746 /* initialization to perform when the nfsd service is started: */
3749 __nfs4_state_start(void)
3751 unsigned long grace_time;
3753 boot_time = get_seconds();
3754 grace_time = get_nfs4_grace_period();
3755 lease_time = user_lease_time;
3756 locks_start_grace(&nfsd4_manager);
3757 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
3759 laundry_wq = create_singlethread_workqueue("nfsd4");
3760 queue_delayed_work(laundry_wq, &laundromat_work, grace_time);
3761 set_max_delegations();
3765 nfs4_state_start(void)
3769 nfsd4_load_reboot_recovery_data();
3770 __nfs4_state_start();
3776 nfs4_lease_time(void)
3782 __nfs4_state_shutdown(void)
3785 struct nfs4_client *clp = NULL;
3786 struct nfs4_delegation *dp = NULL;
3787 struct list_head *pos, *next, reaplist;
3789 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3790 while (!list_empty(&conf_id_hashtbl[i])) {
3791 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3794 while (!list_empty(&unconf_str_hashtbl[i])) {
3795 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3799 INIT_LIST_HEAD(&reaplist);
3800 spin_lock(&recall_lock);
3801 list_for_each_safe(pos, next, &del_recall_lru) {
3802 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3803 list_move(&dp->dl_recall_lru, &reaplist);
3805 spin_unlock(&recall_lock);
3806 list_for_each_safe(pos, next, &reaplist) {
3807 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3808 list_del_init(&dp->dl_recall_lru);
3809 unhash_delegation(dp);
3812 nfsd4_shutdown_recdir();
3817 nfs4_state_shutdown(void)
3819 cancel_rearming_delayed_workqueue(laundry_wq, &laundromat_work);
3820 destroy_workqueue(laundry_wq);
3821 locks_end_grace(&nfsd4_manager);
3823 nfs4_release_reclaim();
3824 __nfs4_state_shutdown();
3825 nfs4_unlock_state();
3829 * user_recovery_dirname is protected by the nfsd_mutex since it's only
3830 * accessed when nfsd is starting.
3833 nfs4_set_recdir(char *recdir)
3835 strcpy(user_recovery_dirname, recdir);
3839 * Change the NFSv4 recovery directory to recdir.
3842 nfs4_reset_recoverydir(char *recdir)
3847 status = kern_path(recdir, LOOKUP_FOLLOW, &path);
3851 if (S_ISDIR(path.dentry->d_inode->i_mode)) {
3852 nfs4_set_recdir(recdir);
3860 nfs4_recoverydir(void)
3862 return user_recovery_dirname;
3866 * Called when leasetime is changed.
3868 * The only way the protocol gives us to handle on-the-fly lease changes is to
3869 * simulate a reboot. Instead of doing that, we just wait till the next time
3870 * we start to register any changes in lease time. If the administrator
3871 * really wants to change the lease time *now*, they can go ahead and bring
3872 * nfsd down and then back up again after changing the lease time.
3874 * user_lease_time is protected by nfsd_mutex since it's only really accessed
3875 * when nfsd is starting
3878 nfs4_reset_lease(time_t leasetime)
3880 user_lease_time = leasetime;