xref: /titanic_52/usr/src/uts/common/fs/nfs/nfs4_state.c (revision 59d2da88ef75ee90d89de8d98edf0521bea61f8d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/systm.h>
27 #include <sys/kmem.h>
28 #include <sys/cmn_err.h>
29 #include <sys/atomic.h>
30 #include <sys/clconf.h>
31 #include <sys/cladm.h>
32 #include <sys/flock.h>
33 #include <nfs/export.h>
34 #include <nfs/nfs.h>
35 #include <nfs/nfs4.h>
36 #include <nfs/nfssys.h>
37 #include <nfs/lm.h>
38 #include <sys/pathname.h>
39 #include <sys/sdt.h>
40 #include <sys/nvpair.h>
41 
42 extern u_longlong_t nfs4_srv_caller_id;
43 
44 extern time_t rfs4_start_time;
45 extern uint_t nfs4_srv_vkey;
46 
47 stateid4 special0 = {
48 	0,
49 	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
50 };
51 
52 stateid4 special1 = {
53 	0xffffffff,
54 	{
55 		(char)0xff, (char)0xff, (char)0xff, (char)0xff,
56 		(char)0xff, (char)0xff, (char)0xff, (char)0xff,
57 		(char)0xff, (char)0xff, (char)0xff, (char)0xff
58 	}
59 };
60 
61 
62 #define	ISSPECIAL(id)  (stateid4_cmp(id, &special0) || \
63 			stateid4_cmp(id, &special1))
64 
65 /* For embedding the cluster nodeid into our clientid */
66 #define	CLUSTER_NODEID_SHIFT	24
67 #define	CLUSTER_MAX_NODEID	255
68 
69 #ifdef DEBUG
70 int rfs4_debug;
71 #endif
72 
73 static uint32_t rfs4_database_debug = 0x00;
74 
75 static void rfs4_ss_clid_write(rfs4_client_t *cp, char *leaf);
76 static void rfs4_ss_clid_write_one(rfs4_client_t *cp, char *dir, char *leaf);
77 static void rfs4_dss_clear_oldstate(rfs4_servinst_t *sip);
78 static void rfs4_ss_chkclid_sip(rfs4_client_t *cp, rfs4_servinst_t *sip);
79 
80 /*
81  * Couple of simple init/destroy functions for a general waiter
82  */
83 void
84 rfs4_sw_init(rfs4_state_wait_t *swp)
85 {
86 	mutex_init(swp->sw_cv_lock, NULL, MUTEX_DEFAULT, NULL);
87 	cv_init(swp->sw_cv, NULL, CV_DEFAULT, NULL);
88 	swp->sw_active = FALSE;
89 	swp->sw_wait_count = 0;
90 }
91 
92 void
93 rfs4_sw_destroy(rfs4_state_wait_t *swp)
94 {
95 	mutex_destroy(swp->sw_cv_lock);
96 	cv_destroy(swp->sw_cv);
97 }
98 
99 void
100 rfs4_sw_enter(rfs4_state_wait_t *swp)
101 {
102 	mutex_enter(swp->sw_cv_lock);
103 	while (swp->sw_active) {
104 		swp->sw_wait_count++;
105 		cv_wait(swp->sw_cv, swp->sw_cv_lock);
106 		swp->sw_wait_count--;
107 	}
108 	ASSERT(swp->sw_active == FALSE);
109 	swp->sw_active = TRUE;
110 	mutex_exit(swp->sw_cv_lock);
111 }
112 
113 void
114 rfs4_sw_exit(rfs4_state_wait_t *swp)
115 {
116 	mutex_enter(swp->sw_cv_lock);
117 	ASSERT(swp->sw_active == TRUE);
118 	swp->sw_active = FALSE;
119 	if (swp->sw_wait_count != 0)
120 		cv_broadcast(swp->sw_cv);
121 	mutex_exit(swp->sw_cv_lock);
122 }
123 
124 /*
125  * CPR callback id -- not related to v4 callbacks
126  */
127 static callb_id_t cpr_id = 0;
128 
129 static void
130 deep_lock_copy(LOCK4res *dres, LOCK4res *sres)
131 {
132 	lock_owner4 *slo = &sres->LOCK4res_u.denied.owner;
133 	lock_owner4 *dlo = &dres->LOCK4res_u.denied.owner;
134 
135 	if (sres->status == NFS4ERR_DENIED) {
136 		dlo->owner_val = kmem_alloc(slo->owner_len, KM_SLEEP);
137 		bcopy(slo->owner_val, dlo->owner_val, slo->owner_len);
138 	}
139 }
140 
141 static void
142 deep_lock_free(LOCK4res *res)
143 {
144 	lock_owner4 *lo = &res->LOCK4res_u.denied.owner;
145 
146 	if (res->status == NFS4ERR_DENIED)
147 		kmem_free(lo->owner_val, lo->owner_len);
148 }
149 
150 static void
151 deep_open_copy(OPEN4res *dres, OPEN4res *sres)
152 {
153 	nfsace4 *sacep, *dacep;
154 
155 	if (sres->status != NFS4_OK) {
156 		return;
157 	}
158 
159 	dres->attrset = sres->attrset;
160 
161 	switch (sres->delegation.delegation_type) {
162 	case OPEN_DELEGATE_NONE:
163 		return;
164 	case OPEN_DELEGATE_READ:
165 		sacep = &sres->delegation.open_delegation4_u.read.permissions;
166 		dacep = &dres->delegation.open_delegation4_u.read.permissions;
167 		break;
168 	case OPEN_DELEGATE_WRITE:
169 		sacep = &sres->delegation.open_delegation4_u.write.permissions;
170 		dacep = &dres->delegation.open_delegation4_u.write.permissions;
171 		break;
172 	}
173 	dacep->who.utf8string_val =
174 	    kmem_alloc(sacep->who.utf8string_len, KM_SLEEP);
175 	bcopy(sacep->who.utf8string_val, dacep->who.utf8string_val,
176 	    sacep->who.utf8string_len);
177 }
178 
179 static void
180 deep_open_free(OPEN4res *res)
181 {
182 	nfsace4 *acep;
183 	if (res->status != NFS4_OK)
184 		return;
185 
186 	switch (res->delegation.delegation_type) {
187 	case OPEN_DELEGATE_NONE:
188 		return;
189 	case OPEN_DELEGATE_READ:
190 		acep = &res->delegation.open_delegation4_u.read.permissions;
191 		break;
192 	case OPEN_DELEGATE_WRITE:
193 		acep = &res->delegation.open_delegation4_u.write.permissions;
194 		break;
195 	}
196 
197 	if (acep->who.utf8string_val) {
198 		kmem_free(acep->who.utf8string_val, acep->who.utf8string_len);
199 		acep->who.utf8string_val = NULL;
200 	}
201 }
202 
203 void
204 rfs4_free_reply(nfs_resop4 *rp)
205 {
206 	switch (rp->resop) {
207 	case OP_LOCK:
208 		deep_lock_free(&rp->nfs_resop4_u.oplock);
209 		break;
210 	case OP_OPEN:
211 		deep_open_free(&rp->nfs_resop4_u.opopen);
212 	default:
213 		break;
214 	}
215 }
216 
217 void
218 rfs4_copy_reply(nfs_resop4 *dst, nfs_resop4 *src)
219 {
220 	*dst = *src;
221 
222 	/* Handle responses that need deep copy */
223 	switch (src->resop) {
224 	case OP_LOCK:
225 		deep_lock_copy(&dst->nfs_resop4_u.oplock,
226 		    &src->nfs_resop4_u.oplock);
227 		break;
228 	case OP_OPEN:
229 		deep_open_copy(&dst->nfs_resop4_u.opopen,
230 		    &src->nfs_resop4_u.opopen);
231 		break;
232 	default:
233 		break;
234 	};
235 }
236 
237 /*
238  * This is the implementation of the underlying state engine. The
239  * public interface to this engine is described by
240  * nfs4_state.h. Callers to the engine should hold no state engine
241  * locks when they call in to it. If the protocol needs to lock data
242  * structures it should do so after acquiring all references to them
243  * first and then follow the following lock order:
244  *
245  *	client > openowner > state > lo_state > lockowner > file.
246  *
247  * Internally we only allow a thread to hold one hash bucket lock at a
248  * time and the lock is higher in the lock order (must be acquired
249  * first) than the data structure that is on that hash list.
250  *
251  * If a new reference was acquired by the caller, that reference needs
252  * to be released after releasing all acquired locks with the
253  * corresponding rfs4_*_rele routine.
254  */
255 
256 /*
257  * This code is some what prototypical for now. Its purpose currently is to
258  * implement the interfaces sufficiently to finish the higher protocol
259  * elements. This will be replaced by a dynamically resizeable tables
260  * backed by kmem_cache allocator. However synchronization is handled
261  * correctly (I hope) and will not change by much.  The mutexes for
262  * the hash buckets that can be used to create new instances of data
263  * structures  might be good candidates to evolve into reader writer
264  * locks. If it has to do a creation, it would be holding the
265  * mutex across a kmem_alloc with KM_SLEEP specified.
266  */
267 
268 #ifdef DEBUG
269 #define	TABSIZE 17
270 #else
271 #define	TABSIZE 2047
272 #endif
273 
274 #define	ADDRHASH(key) ((unsigned long)(key) >> 3)
275 
276 /* Used to serialize create/destroy of rfs4_server_state database */
277 kmutex_t	rfs4_state_lock;
278 static rfs4_database_t *rfs4_server_state = NULL;
279 
280 /* Used to serialize lookups of clientids */
281 static	krwlock_t	rfs4_findclient_lock;
282 
283 /*
284  * For now this "table" is exposed so that the CPR callback
285  * function can tromp through it..
286  */
287 rfs4_table_t *rfs4_client_tab;
288 
289 static rfs4_index_t *rfs4_clientid_idx;
290 static rfs4_index_t *rfs4_nfsclnt_idx;
291 static rfs4_table_t *rfs4_openowner_tab;
292 static rfs4_index_t *rfs4_openowner_idx;
293 static rfs4_table_t *rfs4_state_tab;
294 static rfs4_index_t *rfs4_state_idx;
295 static rfs4_index_t *rfs4_state_owner_file_idx;
296 static rfs4_index_t *rfs4_state_file_idx;
297 static rfs4_table_t *rfs4_lo_state_tab;
298 static rfs4_index_t *rfs4_lo_state_idx;
299 static rfs4_index_t *rfs4_lo_state_owner_idx;
300 static rfs4_table_t *rfs4_lockowner_tab;
301 static rfs4_index_t *rfs4_lockowner_idx;
302 static rfs4_index_t *rfs4_lockowner_pid_idx;
303 static rfs4_table_t *rfs4_file_tab;
304 static rfs4_index_t *rfs4_file_idx;
305 static rfs4_table_t *rfs4_deleg_state_tab;
306 static rfs4_index_t *rfs4_deleg_idx;
307 static rfs4_index_t *rfs4_deleg_state_idx;
308 
309 #define	MAXTABSZ 1024*1024
310 
311 /* The values below are rfs4_lease_time units */
312 
313 #ifdef DEBUG
314 #define	CLIENT_CACHE_TIME 1
315 #define	OPENOWNER_CACHE_TIME 1
316 #define	STATE_CACHE_TIME 1
317 #define	LO_STATE_CACHE_TIME 1
318 #define	LOCKOWNER_CACHE_TIME 1
319 #define	FILE_CACHE_TIME 3
320 #define	DELEG_STATE_CACHE_TIME 1
321 #else
322 #define	CLIENT_CACHE_TIME 10
323 #define	OPENOWNER_CACHE_TIME 5
324 #define	STATE_CACHE_TIME 1
325 #define	LO_STATE_CACHE_TIME 1
326 #define	LOCKOWNER_CACHE_TIME 3
327 #define	FILE_CACHE_TIME 40
328 #define	DELEG_STATE_CACHE_TIME 1
329 #endif
330 
331 
332 static time_t rfs4_client_cache_time = 0;
333 static time_t rfs4_openowner_cache_time = 0;
334 static time_t rfs4_state_cache_time = 0;
335 static time_t rfs4_lo_state_cache_time = 0;
336 static time_t rfs4_lockowner_cache_time = 0;
337 static time_t rfs4_file_cache_time = 0;
338 static time_t rfs4_deleg_state_cache_time = 0;
339 
340 static bool_t rfs4_client_create(rfs4_entry_t, void *);
341 static void rfs4_dss_remove_cpleaf(rfs4_client_t *);
342 static void rfs4_dss_remove_leaf(rfs4_servinst_t *, char *, char *);
343 static void rfs4_client_destroy(rfs4_entry_t);
344 static bool_t rfs4_client_expiry(rfs4_entry_t);
345 static uint32_t clientid_hash(void *);
346 static bool_t clientid_compare(rfs4_entry_t, void *);
347 static void *clientid_mkkey(rfs4_entry_t);
348 static uint32_t nfsclnt_hash(void *);
349 static bool_t nfsclnt_compare(rfs4_entry_t, void *);
350 static void *nfsclnt_mkkey(rfs4_entry_t);
351 static bool_t rfs4_openowner_create(rfs4_entry_t, void *);
352 static void rfs4_openowner_destroy(rfs4_entry_t);
353 static bool_t rfs4_openowner_expiry(rfs4_entry_t);
354 static uint32_t openowner_hash(void *);
355 static bool_t openowner_compare(rfs4_entry_t, void *);
356 static void *openowner_mkkey(rfs4_entry_t);
357 static bool_t rfs4_state_create(rfs4_entry_t, void *);
358 static void rfs4_state_destroy(rfs4_entry_t);
359 static bool_t rfs4_state_expiry(rfs4_entry_t);
360 static uint32_t state_hash(void *);
361 static bool_t state_compare(rfs4_entry_t, void *);
362 static void *state_mkkey(rfs4_entry_t);
363 static uint32_t state_owner_file_hash(void *);
364 static bool_t state_owner_file_compare(rfs4_entry_t, void *);
365 static void *state_owner_file_mkkey(rfs4_entry_t);
366 static uint32_t state_file_hash(void *);
367 static bool_t state_file_compare(rfs4_entry_t, void *);
368 static void *state_file_mkkey(rfs4_entry_t);
369 static bool_t rfs4_lo_state_create(rfs4_entry_t, void *);
370 static void rfs4_lo_state_destroy(rfs4_entry_t);
371 static bool_t rfs4_lo_state_expiry(rfs4_entry_t);
372 static uint32_t lo_state_hash(void *);
373 static bool_t lo_state_compare(rfs4_entry_t, void *);
374 static void *lo_state_mkkey(rfs4_entry_t);
375 static uint32_t lo_state_lo_hash(void *);
376 static bool_t lo_state_lo_compare(rfs4_entry_t, void *);
377 static void *lo_state_lo_mkkey(rfs4_entry_t);
378 static bool_t rfs4_lockowner_create(rfs4_entry_t, void *);
379 static void rfs4_lockowner_destroy(rfs4_entry_t);
380 static bool_t rfs4_lockowner_expiry(rfs4_entry_t);
381 static uint32_t lockowner_hash(void *);
382 static bool_t lockowner_compare(rfs4_entry_t, void *);
383 static void *lockowner_mkkey(rfs4_entry_t);
384 static uint32_t pid_hash(void *);
385 static bool_t pid_compare(rfs4_entry_t, void *);
386 static void *pid_mkkey(rfs4_entry_t);
387 static bool_t rfs4_file_create(rfs4_entry_t, void *);
388 static void rfs4_file_destroy(rfs4_entry_t);
389 static uint32_t file_hash(void *);
390 static bool_t file_compare(rfs4_entry_t, void *);
391 static void *file_mkkey(rfs4_entry_t);
392 static bool_t rfs4_deleg_state_create(rfs4_entry_t, void *);
393 static void rfs4_deleg_state_destroy(rfs4_entry_t);
394 static bool_t rfs4_deleg_state_expiry(rfs4_entry_t);
395 static uint32_t deleg_hash(void *);
396 static bool_t deleg_compare(rfs4_entry_t, void *);
397 static void *deleg_mkkey(rfs4_entry_t);
398 static uint32_t deleg_state_hash(void *);
399 static bool_t deleg_state_compare(rfs4_entry_t, void *);
400 static void *deleg_state_mkkey(rfs4_entry_t);
401 
402 static void rfs4_state_rele_nounlock(rfs4_state_t *);
403 
404 static int rfs4_ss_enabled = 0;
405 
406 extern void (*rfs4_client_clrst)(struct nfs4clrst_args *);
407 
408 void
409 rfs4_ss_pnfree(rfs4_ss_pn_t *ss_pn)
410 {
411 	kmem_free(ss_pn, sizeof (rfs4_ss_pn_t));
412 }
413 
414 static rfs4_ss_pn_t *
415 rfs4_ss_pnalloc(char *dir, char *leaf)
416 {
417 	rfs4_ss_pn_t *ss_pn;
418 	int 	dir_len, leaf_len;
419 
420 	/*
421 	 * validate we have a resonable path
422 	 * (account for the '/' and trailing null)
423 	 */
424 	if ((dir_len = strlen(dir)) > MAXPATHLEN ||
425 	    (leaf_len = strlen(leaf)) > MAXNAMELEN ||
426 	    (dir_len + leaf_len + 2) > MAXPATHLEN) {
427 		return (NULL);
428 	}
429 
430 	ss_pn = kmem_alloc(sizeof (rfs4_ss_pn_t), KM_SLEEP);
431 
432 	(void) snprintf(ss_pn->pn, MAXPATHLEN, "%s/%s", dir, leaf);
433 	/* Handy pointer to just the leaf name */
434 	ss_pn->leaf = ss_pn->pn + dir_len + 1;
435 	return (ss_pn);
436 }
437 
438 
439 /*
440  * Move the "leaf" filename from "sdir" directory
441  * to the "ddir" directory. Return the pathname of
442  * the destination unless the rename fails in which
443  * case we need to return the source pathname.
444  */
445 static rfs4_ss_pn_t *
446 rfs4_ss_movestate(char *sdir, char *ddir, char *leaf)
447 {
448 	rfs4_ss_pn_t *src, *dst;
449 
450 	if ((src = rfs4_ss_pnalloc(sdir, leaf)) == NULL)
451 		return (NULL);
452 
453 	if ((dst = rfs4_ss_pnalloc(ddir, leaf)) == NULL) {
454 		rfs4_ss_pnfree(src);
455 		return (NULL);
456 	}
457 
458 	/*
459 	 * If the rename fails we shall return the src
460 	 * pathname and free the dst. Otherwise we need
461 	 * to free the src and return the dst pathanme.
462 	 */
463 	if (vn_rename(src->pn, dst->pn, UIO_SYSSPACE)) {
464 		rfs4_ss_pnfree(dst);
465 		return (src);
466 	}
467 	rfs4_ss_pnfree(src);
468 	return (dst);
469 }
470 
471 
472 static rfs4_oldstate_t *
473 rfs4_ss_getstate(vnode_t *dvp, rfs4_ss_pn_t *ss_pn)
474 {
475 	struct uio uio;
476 	struct iovec iov[3];
477 
478 	rfs4_oldstate_t *cl_ss = NULL;
479 	vnode_t *vp;
480 	vattr_t va;
481 	uint_t id_len;
482 	int err, kill_file, file_vers;
483 
484 	if (ss_pn == NULL)
485 		return (NULL);
486 
487 	/*
488 	 * open the state file.
489 	 */
490 	if (vn_open(ss_pn->pn, UIO_SYSSPACE, FREAD, 0, &vp, 0, 0) != 0) {
491 		return (NULL);
492 	}
493 
494 	if (vp->v_type != VREG) {
495 		(void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED(), NULL);
496 		VN_RELE(vp);
497 		return (NULL);
498 	}
499 
500 	err = VOP_ACCESS(vp, VREAD, 0, CRED(), NULL);
501 	if (err) {
502 		/*
503 		 * We don't have read access? better get the heck out.
504 		 */
505 		(void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED(), NULL);
506 		VN_RELE(vp);
507 		return (NULL);
508 	}
509 
510 	(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, NULL);
511 	/*
512 	 * get the file size to do some basic validation
513 	 */
514 	va.va_mask = AT_SIZE;
515 	err = VOP_GETATTR(vp, &va, 0, CRED(), NULL);
516 
517 	kill_file = (va.va_size == 0 || va.va_size <
518 	    (NFS4_VERIFIER_SIZE + sizeof (uint_t)+1));
519 
520 	if (err || kill_file) {
521 		VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
522 		(void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED(), NULL);
523 		VN_RELE(vp);
524 		if (kill_file) {
525 			(void) VOP_REMOVE(dvp, ss_pn->leaf, CRED(), NULL, 0);
526 		}
527 		return (NULL);
528 	}
529 
530 	cl_ss = kmem_alloc(sizeof (rfs4_oldstate_t), KM_SLEEP);
531 
532 	/*
533 	 * build iovecs to read in the file_version, verifier and id_len
534 	 */
535 	iov[0].iov_base = (caddr_t)&file_vers;
536 	iov[0].iov_len = sizeof (int);
537 	iov[1].iov_base = (caddr_t)&cl_ss->cl_id4.verifier;
538 	iov[1].iov_len = NFS4_VERIFIER_SIZE;
539 	iov[2].iov_base = (caddr_t)&id_len;
540 	iov[2].iov_len = sizeof (uint_t);
541 
542 	uio.uio_iov = iov;
543 	uio.uio_iovcnt = 3;
544 	uio.uio_segflg = UIO_SYSSPACE;
545 	uio.uio_loffset = 0;
546 	uio.uio_resid = sizeof (int) + NFS4_VERIFIER_SIZE + sizeof (uint_t);
547 
548 	if (err = VOP_READ(vp, &uio, FREAD, CRED(), NULL)) {
549 		VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
550 		(void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED(), NULL);
551 		VN_RELE(vp);
552 		kmem_free(cl_ss, sizeof (rfs4_oldstate_t));
553 		return (NULL);
554 	}
555 
556 	/*
557 	 * if the file_version doesn't match or if the
558 	 * id_len is zero or the combination of the verifier,
559 	 * id_len and id_val is bigger than the file we have
560 	 * a problem. If so ditch the file.
561 	 */
562 	kill_file = (file_vers != NFS4_SS_VERSION || id_len == 0 ||
563 	    (id_len + NFS4_VERIFIER_SIZE + sizeof (uint_t)) > va.va_size);
564 
565 	if (err || kill_file) {
566 		VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
567 		(void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED(), NULL);
568 		VN_RELE(vp);
569 		kmem_free(cl_ss, sizeof (rfs4_oldstate_t));
570 		if (kill_file) {
571 			(void) VOP_REMOVE(dvp, ss_pn->leaf, CRED(), NULL, 0);
572 		}
573 		return (NULL);
574 	}
575 
576 	/*
577 	 * now get the client id value
578 	 */
579 	cl_ss->cl_id4.id_val = kmem_alloc(id_len, KM_SLEEP);
580 	iov[0].iov_base = cl_ss->cl_id4.id_val;
581 	iov[0].iov_len = id_len;
582 
583 	uio.uio_iov = iov;
584 	uio.uio_iovcnt = 1;
585 	uio.uio_segflg = UIO_SYSSPACE;
586 	uio.uio_resid = cl_ss->cl_id4.id_len = id_len;
587 
588 	if (err = VOP_READ(vp, &uio, FREAD, CRED(), NULL)) {
589 		VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
590 		(void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED(), NULL);
591 		VN_RELE(vp);
592 		kmem_free(cl_ss->cl_id4.id_val, id_len);
593 		kmem_free(cl_ss, sizeof (rfs4_oldstate_t));
594 		return (NULL);
595 	}
596 
597 	VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
598 	(void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED(), NULL);
599 	VN_RELE(vp);
600 	return (cl_ss);
601 }
602 
603 #ifdef	nextdp
604 #undef nextdp
605 #endif
606 #define	nextdp(dp)	((struct dirent64 *)((char *)(dp) + (dp)->d_reclen))
607 
608 /*
609  * Add entries from statedir to supplied oldstate list.
610  * Optionally, move all entries from statedir -> destdir.
611  */
612 void
613 rfs4_ss_oldstate(rfs4_oldstate_t *oldstate, char *statedir, char *destdir)
614 {
615 	rfs4_ss_pn_t *ss_pn;
616 	rfs4_oldstate_t *cl_ss = NULL;
617 	char	*dirt = NULL;
618 	int	err, dir_eof = 0, size = 0;
619 	vnode_t *dvp;
620 	struct iovec iov;
621 	struct uio uio;
622 	struct dirent64 *dep;
623 	offset_t dirchunk_offset = 0;
624 
625 	/*
626 	 * open the state directory
627 	 */
628 	if (vn_open(statedir, UIO_SYSSPACE, FREAD, 0, &dvp, 0, 0))
629 		return;
630 
631 	if (dvp->v_type != VDIR || VOP_ACCESS(dvp, VREAD, 0, CRED(), NULL))
632 		goto out;
633 
634 	dirt = kmem_alloc(RFS4_SS_DIRSIZE, KM_SLEEP);
635 
636 	/*
637 	 * Get and process the directory entries
638 	 */
639 	while (!dir_eof) {
640 		(void) VOP_RWLOCK(dvp, V_WRITELOCK_FALSE, NULL);
641 		iov.iov_base = dirt;
642 		iov.iov_len = RFS4_SS_DIRSIZE;
643 		uio.uio_iov = &iov;
644 		uio.uio_iovcnt = 1;
645 		uio.uio_segflg = UIO_SYSSPACE;
646 		uio.uio_loffset = dirchunk_offset;
647 		uio.uio_resid = RFS4_SS_DIRSIZE;
648 
649 		err = VOP_READDIR(dvp, &uio, CRED(), &dir_eof, NULL, 0);
650 		VOP_RWUNLOCK(dvp, V_WRITELOCK_FALSE, NULL);
651 		if (err)
652 			goto out;
653 
654 		size = RFS4_SS_DIRSIZE - uio.uio_resid;
655 
656 		/*
657 		 * Process all the directory entries in this
658 		 * readdir chunk
659 		 */
660 		for (dep = (struct dirent64 *)dirt; size > 0;
661 		    dep = nextdp(dep)) {
662 
663 			size -= dep->d_reclen;
664 			dirchunk_offset = dep->d_off;
665 
666 			/*
667 			 * Skip '.' and '..'
668 			 */
669 			if (NFS_IS_DOTNAME(dep->d_name))
670 				continue;
671 
672 			ss_pn = rfs4_ss_pnalloc(statedir, dep->d_name);
673 			if (ss_pn == NULL)
674 				continue;
675 
676 			if (cl_ss = rfs4_ss_getstate(dvp, ss_pn)) {
677 				if (destdir != NULL) {
678 					rfs4_ss_pnfree(ss_pn);
679 					cl_ss->ss_pn = rfs4_ss_movestate(
680 					    statedir, destdir, dep->d_name);
681 				} else {
682 					cl_ss->ss_pn = ss_pn;
683 				}
684 				insque(cl_ss, oldstate);
685 			} else {
686 				rfs4_ss_pnfree(ss_pn);
687 			}
688 		}
689 	}
690 
691 out:
692 	(void) VOP_CLOSE(dvp, FREAD, 1, (offset_t)0, CRED(), NULL);
693 	VN_RELE(dvp);
694 	if (dirt)
695 		kmem_free((caddr_t)dirt, RFS4_SS_DIRSIZE);
696 }
697 
698 static void
699 rfs4_ss_init(void)
700 {
701 	int npaths = 1;
702 	char *default_dss_path = NFS4_DSS_VAR_DIR;
703 
704 	/* read the default stable storage state */
705 	rfs4_dss_readstate(npaths, &default_dss_path);
706 
707 	rfs4_ss_enabled = 1;
708 }
709 
710 static void
711 rfs4_ss_fini(void)
712 {
713 	rfs4_servinst_t *sip;
714 
715 	mutex_enter(&rfs4_servinst_lock);
716 	sip = rfs4_cur_servinst;
717 	while (sip != NULL) {
718 		rfs4_dss_clear_oldstate(sip);
719 		sip = sip->next;
720 	}
721 	mutex_exit(&rfs4_servinst_lock);
722 }
723 
724 /*
725  * Remove all oldstate files referenced by this servinst.
726  */
727 static void
728 rfs4_dss_clear_oldstate(rfs4_servinst_t *sip)
729 {
730 	rfs4_oldstate_t *os_head, *osp;
731 
732 	rw_enter(&sip->oldstate_lock, RW_WRITER);
733 	os_head = sip->oldstate;
734 
735 	if (os_head == NULL)
736 		return;
737 
738 	/* skip dummy entry */
739 	osp = os_head->next;
740 	while (osp != os_head) {
741 		char *leaf = osp->ss_pn->leaf;
742 		rfs4_oldstate_t *os_next;
743 
744 		rfs4_dss_remove_leaf(sip, NFS4_DSS_OLDSTATE_LEAF, leaf);
745 
746 		if (osp->cl_id4.id_val)
747 			kmem_free(osp->cl_id4.id_val, osp->cl_id4.id_len);
748 		if (osp->ss_pn)
749 			kmem_free(osp->ss_pn, sizeof (rfs4_ss_pn_t));
750 
751 		os_next = osp->next;
752 		remque(osp);
753 		kmem_free(osp, sizeof (rfs4_oldstate_t));
754 		osp = os_next;
755 	}
756 
757 	/* free dummy entry */
758 	kmem_free(osp, sizeof (rfs4_oldstate_t));
759 
760 	sip->oldstate = NULL;
761 
762 	rw_exit(&sip->oldstate_lock);
763 }
764 
765 /*
766  * Form the state and oldstate paths, and read in the stable storage files.
767  */
768 void
769 rfs4_dss_readstate(int npaths, char **paths)
770 {
771 	int i;
772 	char *state, *oldstate;
773 
774 	state = kmem_alloc(MAXPATHLEN, KM_SLEEP);
775 	oldstate = kmem_alloc(MAXPATHLEN, KM_SLEEP);
776 
777 	for (i = 0; i < npaths; i++) {
778 		char *path = paths[i];
779 
780 		(void) sprintf(state, "%s/%s", path, NFS4_DSS_STATE_LEAF);
781 		(void) sprintf(oldstate, "%s/%s", path, NFS4_DSS_OLDSTATE_LEAF);
782 
783 		/*
784 		 * Populate the current server instance's oldstate list.
785 		 *
786 		 * 1. Read stable storage data from old state directory,
787 		 *    leaving its contents alone.
788 		 *
789 		 * 2. Read stable storage data from state directory,
790 		 *    and move the latter's contents to old state
791 		 *    directory.
792 		 */
793 		rfs4_ss_oldstate(rfs4_cur_servinst->oldstate, oldstate, NULL);
794 		rfs4_ss_oldstate(rfs4_cur_servinst->oldstate, state, oldstate);
795 	}
796 
797 	kmem_free(state, MAXPATHLEN);
798 	kmem_free(oldstate, MAXPATHLEN);
799 }
800 
801 
802 /*
803  * Check if we are still in grace and if the client can be
804  * granted permission to perform reclaims.
805  */
806 void
807 rfs4_ss_chkclid(rfs4_client_t *cp)
808 {
809 	rfs4_servinst_t *sip;
810 
811 	/*
812 	 * It should be sufficient to check the oldstate data for just
813 	 * this client's instance. However, since our per-instance
814 	 * client grouping is solely temporal, HA-NFSv4 RG failover
815 	 * might result in clients of the same RG being partitioned into
816 	 * separate instances.
817 	 *
818 	 * Until the client grouping is improved, we must check the
819 	 * oldstate data for all instances with an active grace period.
820 	 *
821 	 * This also serves as the mechanism to remove stale oldstate data.
822 	 * The first time we check an instance after its grace period has
823 	 * expired, the oldstate data should be cleared.
824 	 *
825 	 * Start at the current instance, and walk the list backwards
826 	 * to the first.
827 	 */
828 	mutex_enter(&rfs4_servinst_lock);
829 	for (sip = rfs4_cur_servinst; sip != NULL; sip = sip->prev) {
830 		rfs4_ss_chkclid_sip(cp, sip);
831 
832 		/* if the above check found this client, we're done */
833 		if (cp->can_reclaim)
834 			break;
835 	}
836 	mutex_exit(&rfs4_servinst_lock);
837 }
838 
839 static void
840 rfs4_ss_chkclid_sip(rfs4_client_t *cp, rfs4_servinst_t *sip)
841 {
842 	rfs4_oldstate_t *osp, *os_head;
843 
844 	/* short circuit everything if this server instance has no oldstate */
845 	rw_enter(&sip->oldstate_lock, RW_READER);
846 	os_head = sip->oldstate;
847 	rw_exit(&sip->oldstate_lock);
848 	if (os_head == NULL)
849 		return;
850 
851 	/*
852 	 * If this server instance is no longer in a grace period then
853 	 * the client won't be able to reclaim. No further need for this
854 	 * instance's oldstate data, so it can be cleared.
855 	 */
856 	if (!rfs4_servinst_in_grace(sip))
857 		return;
858 
859 	/* this instance is still in grace; search for the clientid */
860 
861 	rw_enter(&sip->oldstate_lock, RW_READER);
862 
863 	os_head = sip->oldstate;
864 	/* skip dummy entry */
865 	osp = os_head->next;
866 	while (osp != os_head) {
867 		if (osp->cl_id4.id_len == cp->nfs_client.id_len) {
868 			if (bcmp(osp->cl_id4.id_val, cp->nfs_client.id_val,
869 			    osp->cl_id4.id_len) == 0) {
870 				cp->can_reclaim = 1;
871 				break;
872 			}
873 		}
874 		osp = osp->next;
875 	}
876 
877 	rw_exit(&sip->oldstate_lock);
878 }
879 
880 /*
881  * Place client information into stable storage: 1/3.
882  * First, generate the leaf filename, from the client's IP address and
883  * the server-generated short-hand clientid.
884  */
885 void
886 rfs4_ss_clid(rfs4_client_t *cp, struct svc_req *req)
887 {
888 	const char *kinet_ntop6(uchar_t *, char *, size_t);
889 	char leaf[MAXNAMELEN], buf[INET6_ADDRSTRLEN];
890 	struct sockaddr *ca;
891 	uchar_t *b;
892 
893 	if (rfs4_ss_enabled == 0) {
894 		return;
895 	}
896 
897 	buf[0] = 0;
898 
899 
900 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
901 	if (ca == NULL) {
902 		return;
903 	}
904 
905 	/*
906 	 * Convert the caller's IP address to a dotted string
907 	 */
908 	if (ca->sa_family == AF_INET) {
909 
910 		bcopy(svc_getrpccaller(req->rq_xprt)->buf, &cp->cl_addr,
911 		    sizeof (struct sockaddr_in));
912 		b = (uchar_t *)&((struct sockaddr_in *)ca)->sin_addr;
913 		(void) sprintf(buf, "%03d.%03d.%03d.%03d", b[0] & 0xFF,
914 		    b[1] & 0xFF, b[2] & 0xFF, b[3] & 0xFF);
915 	} else if (ca->sa_family == AF_INET6) {
916 		struct sockaddr_in6 *sin6;
917 
918 		sin6 = (struct sockaddr_in6 *)ca;
919 		bcopy(svc_getrpccaller(req->rq_xprt)->buf, &cp->cl_addr,
920 		    sizeof (struct sockaddr_in6));
921 		(void) kinet_ntop6((uchar_t *)&sin6->sin6_addr,
922 		    buf, INET6_ADDRSTRLEN);
923 	}
924 
925 	(void) snprintf(leaf, MAXNAMELEN, "%s-%llx", buf,
926 	    (longlong_t)cp->clientid);
927 	rfs4_ss_clid_write(cp, leaf);
928 }
929 
930 /*
931  * Place client information into stable storage: 2/3.
932  * DSS: distributed stable storage: the file may need to be written to
933  * multiple directories.
934  */
935 static void
936 rfs4_ss_clid_write(rfs4_client_t *cp, char *leaf)
937 {
938 	rfs4_servinst_t *sip;
939 
940 	/*
941 	 * It should be sufficient to write the leaf file to (all) DSS paths
942 	 * associated with just this client's instance. However, since our
943 	 * per-instance client grouping is solely temporal, HA-NFSv4 RG
944 	 * failover might result in us losing DSS data.
945 	 *
946 	 * Until the client grouping is improved, we must write the DSS data
947 	 * to all instances' paths. Start at the current instance, and
948 	 * walk the list backwards to the first.
949 	 */
950 	mutex_enter(&rfs4_servinst_lock);
951 	for (sip = rfs4_cur_servinst; sip != NULL; sip = sip->prev) {
952 		int i, npaths = sip->dss_npaths;
953 
954 		/* write the leaf file to all DSS paths */
955 		for (i = 0; i < npaths; i++) {
956 			rfs4_dss_path_t *dss_path = sip->dss_paths[i];
957 
958 			/* HA-NFSv4 path might have been failed-away from us */
959 			if (dss_path == NULL)
960 				continue;
961 
962 			rfs4_ss_clid_write_one(cp, dss_path->path, leaf);
963 		}
964 	}
965 	mutex_exit(&rfs4_servinst_lock);
966 }
967 
968 /*
969  * Place client information into stable storage: 3/3.
970  * Write the stable storage data to the requested file.
971  */
972 static void
973 rfs4_ss_clid_write_one(rfs4_client_t *cp, char *dss_path, char *leaf)
974 {
975 	int ioflag;
976 	int file_vers = NFS4_SS_VERSION;
977 	size_t dirlen;
978 	struct uio uio;
979 	struct iovec iov[4];
980 	char *dir;
981 	rfs4_ss_pn_t *ss_pn;
982 	vnode_t *vp;
983 	nfs_client_id4 *cl_id4 = &(cp->nfs_client);
984 
985 	/* allow 2 extra bytes for '/' & NUL */
986 	dirlen = strlen(dss_path) + strlen(NFS4_DSS_STATE_LEAF) + 2;
987 	dir = kmem_alloc(dirlen, KM_SLEEP);
988 	(void) sprintf(dir, "%s/%s", dss_path, NFS4_DSS_STATE_LEAF);
989 
990 	ss_pn = rfs4_ss_pnalloc(dir, leaf);
991 	/* rfs4_ss_pnalloc takes its own copy */
992 	kmem_free(dir, dirlen);
993 	if (ss_pn == NULL)
994 		return;
995 
996 	if (vn_open(ss_pn->pn, UIO_SYSSPACE, FCREAT|FWRITE, 0600, &vp,
997 	    CRCREAT, 0)) {
998 		rfs4_ss_pnfree(ss_pn);
999 		return;
1000 	}
1001 
1002 	/*
1003 	 * We need to record leaf - i.e. the filename - so that we know
1004 	 * what to remove, in the future. However, the dir part of cp->ss_pn
1005 	 * should never be referenced directly, since it's potentially only
1006 	 * one of several paths with this leaf in it.
1007 	 */
1008 	if (cp->ss_pn != NULL) {
1009 		if (strcmp(cp->ss_pn->leaf, leaf) == 0) {
1010 			/* we've already recorded *this* leaf */
1011 			rfs4_ss_pnfree(ss_pn);
1012 		} else {
1013 			/* replace with this leaf */
1014 			rfs4_ss_pnfree(cp->ss_pn);
1015 			cp->ss_pn = ss_pn;
1016 		}
1017 	} else {
1018 		cp->ss_pn = ss_pn;
1019 	}
1020 
1021 	/*
1022 	 * Build a scatter list that points to the nfs_client_id4
1023 	 */
1024 	iov[0].iov_base = (caddr_t)&file_vers;
1025 	iov[0].iov_len = sizeof (int);
1026 	iov[1].iov_base = (caddr_t)&(cl_id4->verifier);
1027 	iov[1].iov_len = NFS4_VERIFIER_SIZE;
1028 	iov[2].iov_base = (caddr_t)&(cl_id4->id_len);
1029 	iov[2].iov_len = sizeof (uint_t);
1030 	iov[3].iov_base = (caddr_t)cl_id4->id_val;
1031 	iov[3].iov_len = cl_id4->id_len;
1032 
1033 	uio.uio_iov = iov;
1034 	uio.uio_iovcnt = 4;
1035 	uio.uio_loffset = 0;
1036 	uio.uio_segflg = UIO_SYSSPACE;
1037 	uio.uio_llimit = (rlim64_t)MAXOFFSET_T;
1038 	uio.uio_resid = cl_id4->id_len + sizeof (int) +
1039 	    NFS4_VERIFIER_SIZE + sizeof (uint_t);
1040 
1041 	ioflag = uio.uio_fmode = (FWRITE|FSYNC);
1042 	uio.uio_extflg = UIO_COPY_DEFAULT;
1043 
1044 	(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
1045 	/* write the full client id to the file. */
1046 	(void) VOP_WRITE(vp, &uio, ioflag, CRED(), NULL);
1047 	VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
1048 
1049 	(void) VOP_CLOSE(vp, FWRITE, 1, (offset_t)0, CRED(), NULL);
1050 	VN_RELE(vp);
1051 }
1052 
1053 /*
1054  * DSS: distributed stable storage.
1055  * Unpack the list of paths passed by nfsd.
1056  * Use nvlist_alloc(9F) to manage the data.
1057  * The caller is responsible for allocating and freeing the buffer.
1058  */
1059 int
1060 rfs4_dss_setpaths(char *buf, size_t buflen)
1061 {
1062 	int error;
1063 
1064 	/*
1065 	 * If this is a "warm start", i.e. we previously had DSS paths,
1066 	 * preserve the old paths.
1067 	 */
1068 	if (rfs4_dss_paths != NULL) {
1069 		/*
1070 		 * Before we lose the ptr, destroy the nvlist and pathnames
1071 		 * array from the warm start before this one.
1072 		 */
1073 		if (rfs4_dss_oldpaths)
1074 			nvlist_free(rfs4_dss_oldpaths);
1075 		rfs4_dss_oldpaths = rfs4_dss_paths;
1076 	}
1077 
1078 	/* unpack the buffer into a searchable nvlist */
1079 	error = nvlist_unpack(buf, buflen, &rfs4_dss_paths, KM_SLEEP);
1080 	if (error)
1081 		return (error);
1082 
1083 	/*
1084 	 * Search the nvlist for the pathnames nvpair (which is the only nvpair
1085 	 * in the list, and record its location.
1086 	 */
1087 	error = nvlist_lookup_string_array(rfs4_dss_paths, NFS4_DSS_NVPAIR_NAME,
1088 	    &rfs4_dss_newpaths, &rfs4_dss_numnewpaths);
1089 	return (error);
1090 }
1091 
1092 /*
1093  * Ultimately the nfssys() call NFS4_CLR_STATE endsup here
1094  * to find and mark the client for forced expire.
1095  */
1096 static void
1097 rfs4_client_scrub(rfs4_entry_t ent, void *arg)
1098 {
1099 	rfs4_client_t *cp = (rfs4_client_t *)ent;
1100 	struct nfs4clrst_args *clr = arg;
1101 	struct sockaddr_in6 *ent_sin6;
1102 	struct in6_addr  clr_in6;
1103 	struct sockaddr_in  *ent_sin;
1104 	struct in_addr   clr_in;
1105 
1106 	if (clr->addr_type != cp->cl_addr.ss_family) {
1107 		return;
1108 	}
1109 
1110 	switch (clr->addr_type) {
1111 
1112 	case AF_INET6:
1113 		/* copyin the address from user space */
1114 		if (copyin(clr->ap, &clr_in6, sizeof (clr_in6))) {
1115 			break;
1116 		}
1117 
1118 		ent_sin6 = (struct sockaddr_in6 *)&cp->cl_addr;
1119 
1120 		/*
1121 		 * now compare, and if equivalent mark entry
1122 		 * for forced expiration
1123 		 */
1124 		if (IN6_ARE_ADDR_EQUAL(&ent_sin6->sin6_addr, &clr_in6)) {
1125 			cp->forced_expire = 1;
1126 		}
1127 		break;
1128 
1129 	case AF_INET:
1130 		/* copyin the address from user space */
1131 		if (copyin(clr->ap, &clr_in, sizeof (clr_in))) {
1132 			break;
1133 		}
1134 
1135 		ent_sin = (struct sockaddr_in *)&cp->cl_addr;
1136 
1137 		/*
1138 		 * now compare, and if equivalent mark entry
1139 		 * for forced expiration
1140 		 */
1141 		if (ent_sin->sin_addr.s_addr == clr_in.s_addr) {
1142 			cp->forced_expire = 1;
1143 		}
1144 		break;
1145 
1146 	default:
1147 		/* force this assert to fail */
1148 		ASSERT(clr->addr_type != clr->addr_type);
1149 	}
1150 }
1151 
1152 /*
1153  * This is called from nfssys() in order to clear server state
1154  * for the specified client IP Address.
1155  */
1156 void
1157 rfs4_clear_client_state(struct nfs4clrst_args *clr)
1158 {
1159 	(void) rfs4_dbe_walk(rfs4_client_tab, rfs4_client_scrub, clr);
1160 }
1161 
1162 /*
1163  * Used to initialize the NFSv4 server's state or database.  All of
1164  * the tables are created and timers are set. Only called when NFSv4
1165  * service is provided.
1166  */
1167 void
1168 rfs4_state_init()
1169 {
1170 	int start_grace;
1171 	extern boolean_t rfs4_cpr_callb(void *, int);
1172 	char *dss_path = NFS4_DSS_VAR_DIR;
1173 
1174 	mutex_enter(&rfs4_state_lock);
1175 
1176 	/*
1177 	 * If the server state database has already been initialized,
1178 	 * skip it
1179 	 */
1180 	if (rfs4_server_state != NULL) {
1181 		mutex_exit(&rfs4_state_lock);
1182 		return;
1183 	}
1184 
1185 	rw_init(&rfs4_findclient_lock, NULL, RW_DEFAULT, NULL);
1186 
1187 	/*
1188 	 * Set the boot time.  If the server
1189 	 * has been restarted quickly and has had the opportunity to
1190 	 * service clients, then the start_time needs to be bumped
1191 	 * regardless.  A small window but it exists...
1192 	 */
1193 	if (rfs4_start_time != gethrestime_sec())
1194 		rfs4_start_time = gethrestime_sec();
1195 	else
1196 		rfs4_start_time++;
1197 
1198 	/* DSS: distributed stable storage: initialise served paths list */
1199 	rfs4_dss_pathlist = NULL;
1200 
1201 	/*
1202 	 * Create the first server instance, or a new one if the server has
1203 	 * been restarted; see above comments on rfs4_start_time. Don't
1204 	 * start its grace period; that will be done later, to maximise the
1205 	 * clients' recovery window.
1206 	 */
1207 	start_grace = 0;
1208 	rfs4_servinst_create(start_grace, 1, &dss_path);
1209 
1210 	/* reset the "first NFSv4 request" status */
1211 	rfs4_seen_first_compound = 0;
1212 
1213 	/*
1214 	 * Add a CPR callback so that we can update client
1215 	 * access times to extend the lease after a suspend
1216 	 * and resume (using the same class as rpcmod/connmgr)
1217 	 */
1218 	cpr_id = callb_add(rfs4_cpr_callb, 0, CB_CL_CPR_RPC, "rfs4");
1219 
1220 	/* set the various cache timers for table creation */
1221 	if (rfs4_client_cache_time == 0)
1222 		rfs4_client_cache_time = CLIENT_CACHE_TIME;
1223 	if (rfs4_openowner_cache_time == 0)
1224 		rfs4_openowner_cache_time = OPENOWNER_CACHE_TIME;
1225 	if (rfs4_state_cache_time == 0)
1226 		rfs4_state_cache_time = STATE_CACHE_TIME;
1227 	if (rfs4_lo_state_cache_time == 0)
1228 		rfs4_lo_state_cache_time = LO_STATE_CACHE_TIME;
1229 	if (rfs4_lockowner_cache_time == 0)
1230 		rfs4_lockowner_cache_time = LOCKOWNER_CACHE_TIME;
1231 	if (rfs4_file_cache_time == 0)
1232 		rfs4_file_cache_time = FILE_CACHE_TIME;
1233 	if (rfs4_deleg_state_cache_time == 0)
1234 		rfs4_deleg_state_cache_time = DELEG_STATE_CACHE_TIME;
1235 
1236 	/* Create the overall database to hold all server state */
1237 	rfs4_server_state = rfs4_database_create(rfs4_database_debug);
1238 
1239 	/* Now create the individual tables */
1240 	rfs4_client_cache_time *= rfs4_lease_time;
1241 	rfs4_client_tab = rfs4_table_create(rfs4_server_state,
1242 	    "Client",
1243 	    rfs4_client_cache_time,
1244 	    2,
1245 	    rfs4_client_create,
1246 	    rfs4_client_destroy,
1247 	    rfs4_client_expiry,
1248 	    sizeof (rfs4_client_t),
1249 	    TABSIZE,
1250 	    MAXTABSZ/8, 100);
1251 	rfs4_nfsclnt_idx = rfs4_index_create(rfs4_client_tab,
1252 	    "nfs_client_id4", nfsclnt_hash,
1253 	    nfsclnt_compare, nfsclnt_mkkey,
1254 	    TRUE);
1255 	rfs4_clientid_idx = rfs4_index_create(rfs4_client_tab,
1256 	    "client_id", clientid_hash,
1257 	    clientid_compare, clientid_mkkey,
1258 	    FALSE);
1259 
1260 	rfs4_openowner_cache_time *= rfs4_lease_time;
1261 	rfs4_openowner_tab = rfs4_table_create(rfs4_server_state,
1262 	    "OpenOwner",
1263 	    rfs4_openowner_cache_time,
1264 	    1,
1265 	    rfs4_openowner_create,
1266 	    rfs4_openowner_destroy,
1267 	    rfs4_openowner_expiry,
1268 	    sizeof (rfs4_openowner_t),
1269 	    TABSIZE,
1270 	    MAXTABSZ, 100);
1271 	rfs4_openowner_idx = rfs4_index_create(rfs4_openowner_tab,
1272 	    "open_owner4", openowner_hash,
1273 	    openowner_compare,
1274 	    openowner_mkkey, TRUE);
1275 
1276 	rfs4_state_cache_time *= rfs4_lease_time;
1277 	rfs4_state_tab = rfs4_table_create(rfs4_server_state,
1278 	    "OpenStateID",
1279 	    rfs4_state_cache_time,
1280 	    3,
1281 	    rfs4_state_create,
1282 	    rfs4_state_destroy,
1283 	    rfs4_state_expiry,
1284 	    sizeof (rfs4_state_t),
1285 	    TABSIZE,
1286 	    MAXTABSZ, 100);
1287 
1288 	rfs4_state_owner_file_idx = rfs4_index_create(rfs4_state_tab,
1289 	    "Openowner-File",
1290 	    state_owner_file_hash,
1291 	    state_owner_file_compare,
1292 	    state_owner_file_mkkey, TRUE);
1293 
1294 	rfs4_state_idx = rfs4_index_create(rfs4_state_tab,
1295 	    "State-id", state_hash,
1296 	    state_compare, state_mkkey, FALSE);
1297 
1298 	rfs4_state_file_idx = rfs4_index_create(rfs4_state_tab,
1299 	    "File", state_file_hash,
1300 	    state_file_compare, state_file_mkkey,
1301 	    FALSE);
1302 
1303 	rfs4_lo_state_cache_time *= rfs4_lease_time;
1304 	rfs4_lo_state_tab = rfs4_table_create(rfs4_server_state,
1305 	    "LockStateID",
1306 	    rfs4_lo_state_cache_time,
1307 	    2,
1308 	    rfs4_lo_state_create,
1309 	    rfs4_lo_state_destroy,
1310 	    rfs4_lo_state_expiry,
1311 	    sizeof (rfs4_lo_state_t),
1312 	    TABSIZE,
1313 	    MAXTABSZ, 100);
1314 
1315 	rfs4_lo_state_owner_idx = rfs4_index_create(rfs4_lo_state_tab,
1316 	    "lockownerxstate",
1317 	    lo_state_lo_hash,
1318 	    lo_state_lo_compare,
1319 	    lo_state_lo_mkkey, TRUE);
1320 
1321 	rfs4_lo_state_idx = rfs4_index_create(rfs4_lo_state_tab,
1322 	    "State-id",
1323 	    lo_state_hash, lo_state_compare,
1324 	    lo_state_mkkey, FALSE);
1325 
1326 	rfs4_lockowner_cache_time *= rfs4_lease_time;
1327 
1328 	rfs4_lockowner_tab = rfs4_table_create(rfs4_server_state,
1329 	    "Lockowner",
1330 	    rfs4_lockowner_cache_time,
1331 	    2,
1332 	    rfs4_lockowner_create,
1333 	    rfs4_lockowner_destroy,
1334 	    rfs4_lockowner_expiry,
1335 	    sizeof (rfs4_lockowner_t),
1336 	    TABSIZE,
1337 	    MAXTABSZ, 100);
1338 
1339 	rfs4_lockowner_idx = rfs4_index_create(rfs4_lockowner_tab,
1340 	    "lock_owner4", lockowner_hash,
1341 	    lockowner_compare,
1342 	    lockowner_mkkey, TRUE);
1343 
1344 	rfs4_lockowner_pid_idx = rfs4_index_create(rfs4_lockowner_tab,
1345 	    "pid", pid_hash,
1346 	    pid_compare, pid_mkkey,
1347 	    FALSE);
1348 
1349 	rfs4_file_cache_time *= rfs4_lease_time;
1350 	rfs4_file_tab = rfs4_table_create(rfs4_server_state,
1351 	    "File",
1352 	    rfs4_file_cache_time,
1353 	    1,
1354 	    rfs4_file_create,
1355 	    rfs4_file_destroy,
1356 	    NULL,
1357 	    sizeof (rfs4_file_t),
1358 	    TABSIZE,
1359 	    MAXTABSZ, -1);
1360 
1361 	rfs4_file_idx = rfs4_index_create(rfs4_file_tab,
1362 	    "Filehandle", file_hash,
1363 	    file_compare, file_mkkey, TRUE);
1364 
1365 	rfs4_deleg_state_cache_time *= rfs4_lease_time;
1366 	rfs4_deleg_state_tab = rfs4_table_create(rfs4_server_state,
1367 	    "DelegStateID",
1368 	    rfs4_deleg_state_cache_time,
1369 	    2,
1370 	    rfs4_deleg_state_create,
1371 	    rfs4_deleg_state_destroy,
1372 	    rfs4_deleg_state_expiry,
1373 	    sizeof (rfs4_deleg_state_t),
1374 	    TABSIZE,
1375 	    MAXTABSZ, 100);
1376 	rfs4_deleg_idx = rfs4_index_create(rfs4_deleg_state_tab,
1377 	    "DelegByFileClient",
1378 	    deleg_hash,
1379 	    deleg_compare,
1380 	    deleg_mkkey, TRUE);
1381 
1382 	rfs4_deleg_state_idx = rfs4_index_create(rfs4_deleg_state_tab,
1383 	    "DelegState",
1384 	    deleg_state_hash,
1385 	    deleg_state_compare,
1386 	    deleg_state_mkkey, FALSE);
1387 
1388 	/*
1389 	 * Init the stable storage.
1390 	 */
1391 	rfs4_ss_init();
1392 
1393 	rfs4_client_clrst = rfs4_clear_client_state;
1394 
1395 	mutex_exit(&rfs4_state_lock);
1396 }
1397 
1398 
1399 /*
1400  * Used at server shutdown to cleanup all of the NFSv4 server's structures
1401  * and other state.
1402  */
1403 void
1404 rfs4_state_fini()
1405 {
1406 	rfs4_database_t *dbp;
1407 
1408 	mutex_enter(&rfs4_state_lock);
1409 
1410 	if (rfs4_server_state == NULL) {
1411 		mutex_exit(&rfs4_state_lock);
1412 		return;
1413 	}
1414 
1415 	rfs4_client_clrst = NULL;
1416 
1417 	rfs4_set_deleg_policy(SRV_NEVER_DELEGATE);
1418 	dbp = rfs4_server_state;
1419 	rfs4_server_state = NULL;
1420 
1421 	/*
1422 	 * Cleanup the CPR callback.
1423 	 */
1424 	if (cpr_id)
1425 		(void) callb_delete(cpr_id);
1426 
1427 	rw_destroy(&rfs4_findclient_lock);
1428 
1429 	/* First stop all of the reaper threads in the database */
1430 	rfs4_database_shutdown(dbp);
1431 	/* clean up any dangling stable storage structures */
1432 	rfs4_ss_fini();
1433 	/* Now actually destroy/release the database and its tables */
1434 	rfs4_database_destroy(dbp);
1435 
1436 	/* Reset the cache timers for next time */
1437 	rfs4_client_cache_time = 0;
1438 	rfs4_openowner_cache_time = 0;
1439 	rfs4_state_cache_time = 0;
1440 	rfs4_lo_state_cache_time = 0;
1441 	rfs4_lockowner_cache_time = 0;
1442 	rfs4_file_cache_time = 0;
1443 	rfs4_deleg_state_cache_time = 0;
1444 
1445 	mutex_exit(&rfs4_state_lock);
1446 
1447 	/* destroy server instances and current instance ptr */
1448 	rfs4_servinst_destroy_all();
1449 
1450 	/* reset the "first NFSv4 request" status */
1451 	rfs4_seen_first_compound = 0;
1452 
1453 	/* DSS: distributed stable storage */
1454 	if (rfs4_dss_oldpaths)
1455 		nvlist_free(rfs4_dss_oldpaths);
1456 	if (rfs4_dss_paths)
1457 		nvlist_free(rfs4_dss_paths);
1458 	rfs4_dss_paths = rfs4_dss_oldpaths = NULL;
1459 }
1460 
1461 typedef union {
1462 	struct {
1463 		uint32_t start_time;
1464 		uint32_t c_id;
1465 	} impl_id;
1466 	clientid4 id4;
1467 } cid;
1468 
1469 static int foreign_stateid(stateid_t *id);
1470 static int foreign_clientid(cid *cidp);
1471 static void embed_nodeid(cid *cidp);
1472 
1473 typedef union {
1474 	struct {
1475 		uint32_t c_id;
1476 		uint32_t gen_num;
1477 	} cv_impl;
1478 	verifier4	confirm_verf;
1479 } scid_confirm_verf;
1480 
1481 static uint32_t
1482 clientid_hash(void *key)
1483 {
1484 	cid *idp = key;
1485 
1486 	return (idp->impl_id.c_id);
1487 }
1488 
1489 static bool_t
1490 clientid_compare(rfs4_entry_t entry, void *key)
1491 {
1492 	rfs4_client_t *client = (rfs4_client_t *)entry;
1493 	clientid4 *idp = key;
1494 
1495 	return (*idp == client->clientid);
1496 }
1497 
1498 static void *
1499 clientid_mkkey(rfs4_entry_t entry)
1500 {
1501 	rfs4_client_t *client = (rfs4_client_t *)entry;
1502 
1503 	return (&client->clientid);
1504 }
1505 
1506 static uint32_t
1507 nfsclnt_hash(void *key)
1508 {
1509 	nfs_client_id4 *client = key;
1510 	int i;
1511 	uint32_t hash = 0;
1512 
1513 	for (i = 0; i < client->id_len; i++) {
1514 		hash <<= 1;
1515 		hash += (uint_t)client->id_val[i];
1516 	}
1517 	return (hash);
1518 }
1519 
1520 
1521 static bool_t
1522 nfsclnt_compare(rfs4_entry_t entry, void *key)
1523 {
1524 	rfs4_client_t *client = (rfs4_client_t *)entry;
1525 	nfs_client_id4 *nfs_client = key;
1526 
1527 	if (client->nfs_client.id_len != nfs_client->id_len)
1528 		return (FALSE);
1529 
1530 	return (bcmp(client->nfs_client.id_val, nfs_client->id_val,
1531 	    nfs_client->id_len) == 0);
1532 }
1533 
1534 static void *
1535 nfsclnt_mkkey(rfs4_entry_t entry)
1536 {
1537 	rfs4_client_t *client = (rfs4_client_t *)entry;
1538 
1539 	return (&client->nfs_client);
1540 }
1541 
1542 static bool_t
1543 rfs4_client_expiry(rfs4_entry_t u_entry)
1544 {
1545 	rfs4_client_t *cp = (rfs4_client_t *)u_entry;
1546 	bool_t cp_expired;
1547 
1548 	if (rfs4_dbe_is_invalid(cp->dbe))
1549 		return (TRUE);
1550 	/*
1551 	 * If the sysadmin has used clear_locks for this
1552 	 * entry then forced_expire will be set and we
1553 	 * want this entry to be reaped. Or the entry
1554 	 * has exceeded its lease period.
1555 	 */
1556 	cp_expired = (cp->forced_expire ||
1557 	    (gethrestime_sec() - cp->last_access
1558 	    > rfs4_lease_time));
1559 
1560 	if (!cp->ss_remove && cp_expired)
1561 		cp->ss_remove = 1;
1562 	return (cp_expired);
1563 }
1564 
1565 /*
1566  * Remove the leaf file from all distributed stable storage paths.
1567  */
1568 static void
1569 rfs4_dss_remove_cpleaf(rfs4_client_t *cp)
1570 {
1571 	char *leaf = cp->ss_pn->leaf;
1572 
1573 	rfs4_dss_remove_leaf(cp->server_instance, NFS4_DSS_STATE_LEAF, leaf);
1574 }
1575 
1576 static void
1577 rfs4_dss_remove_leaf(rfs4_servinst_t *sip, char *dir_leaf, char *leaf)
1578 {
1579 	int i, npaths = sip->dss_npaths;
1580 
1581 	for (i = 0; i < npaths; i++) {
1582 		rfs4_dss_path_t *dss_path = sip->dss_paths[i];
1583 		char *path, *dir;
1584 		size_t pathlen;
1585 
1586 		/* the HA-NFSv4 path might have been failed-over away from us */
1587 		if (dss_path == NULL)
1588 			continue;
1589 
1590 		dir = dss_path->path;
1591 
1592 		/* allow 3 extra bytes for two '/' & a NUL */
1593 		pathlen = strlen(dir) + strlen(dir_leaf) + strlen(leaf) + 3;
1594 		path = kmem_alloc(pathlen, KM_SLEEP);
1595 		(void) sprintf(path, "%s/%s/%s", dir, dir_leaf, leaf);
1596 
1597 		(void) vn_remove(path, UIO_SYSSPACE, RMFILE);
1598 
1599 		kmem_free(path, pathlen);
1600 	}
1601 }
1602 
1603 static void
1604 rfs4_client_destroy(rfs4_entry_t u_entry)
1605 {
1606 	rfs4_client_t *cp = (rfs4_client_t *)u_entry;
1607 
1608 	mutex_destroy(cp->cbinfo.cb_lock);
1609 	cv_destroy(cp->cbinfo.cb_cv);
1610 	cv_destroy(cp->cbinfo.cb_cv_nullcaller);
1611 
1612 	/* free callback info */
1613 	rfs4_cbinfo_free(&cp->cbinfo);
1614 
1615 	if (cp->cp_confirmed)
1616 		rfs4_client_rele(cp->cp_confirmed);
1617 
1618 	if (cp->ss_pn) {
1619 		/* check if the stable storage files need to be removed */
1620 		if (cp->ss_remove)
1621 			rfs4_dss_remove_cpleaf(cp);
1622 		rfs4_ss_pnfree(cp->ss_pn);
1623 	}
1624 
1625 	/* Free the client supplied client id */
1626 	kmem_free(cp->nfs_client.id_val, cp->nfs_client.id_len);
1627 
1628 	if (cp->sysidt != LM_NOSYSID)
1629 		lm_free_sysidt(cp->sysidt);
1630 }
1631 
1632 static bool_t
1633 rfs4_client_create(rfs4_entry_t u_entry, void *arg)
1634 {
1635 	rfs4_client_t *cp = (rfs4_client_t *)u_entry;
1636 	nfs_client_id4 *client = (nfs_client_id4 *)arg;
1637 	cid *cidp;
1638 	scid_confirm_verf *scvp;
1639 
1640 	/* Get a clientid to give to the client */
1641 	cidp = (cid *)&cp->clientid;
1642 	cidp->impl_id.start_time = rfs4_start_time;
1643 	cidp->impl_id.c_id = (uint32_t)rfs4_dbe_getid(cp->dbe);
1644 
1645 	/* If we are booted as a cluster node, embed our nodeid */
1646 	if (cluster_bootflags & CLUSTER_BOOTED)
1647 		embed_nodeid(cidp);
1648 
1649 	/* Allocate and copy client's client id value */
1650 	cp->nfs_client.id_val = kmem_alloc(client->id_len, KM_SLEEP);
1651 	cp->nfs_client.id_len = client->id_len;
1652 	bcopy(client->id_val, cp->nfs_client.id_val, client->id_len);
1653 	cp->nfs_client.verifier = client->verifier;
1654 
1655 	/* Init the value for the SETCLIENTID_CONFIRM verifier */
1656 	scvp = (scid_confirm_verf *)&cp->confirm_verf;
1657 	scvp->cv_impl.c_id = cidp->impl_id.c_id;
1658 	scvp->cv_impl.gen_num = 0;
1659 
1660 	/* An F_UNLKSYS has been done for this client */
1661 	cp->unlksys_completed = FALSE;
1662 
1663 	/* We need the client to ack us */
1664 	cp->need_confirm = TRUE;
1665 	cp->cp_confirmed = NULL;
1666 
1667 	/* TRUE all the time until the callback path actually fails */
1668 	cp->cbinfo.cb_notified_of_cb_path_down = TRUE;
1669 
1670 	/* Initialize the access time to now */
1671 	cp->last_access = gethrestime_sec();
1672 
1673 	cp->cr_set = NULL;
1674 	/* Initialize list for insque/remque */
1675 	cp->openownerlist.next = cp->openownerlist.prev = &cp->openownerlist;
1676 	cp->openownerlist.oop = NULL; /* This is not an openowner */
1677 
1678 	cp->sysidt = LM_NOSYSID;
1679 
1680 	cp->clientdeleglist.next = cp->clientdeleglist.prev =
1681 	    &cp->clientdeleglist;
1682 	cp->clientdeleglist.dsp = NULL;
1683 
1684 	/* set up the callback control structure */
1685 	cp->cbinfo.cb_state = CB_UNINIT;
1686 	mutex_init(cp->cbinfo.cb_lock, NULL, MUTEX_DEFAULT, NULL);
1687 	cv_init(cp->cbinfo.cb_cv, NULL, CV_DEFAULT, NULL);
1688 	cv_init(cp->cbinfo.cb_cv_nullcaller, NULL, CV_DEFAULT, NULL);
1689 
1690 	/*
1691 	 * Associate the client_t with the current server instance.
1692 	 * The hold is solely to satisfy the calling requirement of
1693 	 * rfs4_servinst_assign(). In this case it's not strictly necessary.
1694 	 */
1695 	rfs4_dbe_hold(cp->dbe);
1696 	rfs4_servinst_assign(cp, rfs4_cur_servinst);
1697 	rfs4_dbe_rele(cp->dbe);
1698 
1699 	return (TRUE);
1700 }
1701 
1702 /*
1703  * Caller wants to generate/update the setclientid_confirm verifier
1704  * associated with a client.  This is done during the SETCLIENTID
1705  * processing.
1706  */
1707 void
1708 rfs4_client_scv_next(rfs4_client_t *cp)
1709 {
1710 	scid_confirm_verf *scvp;
1711 
1712 	/* Init the value for the SETCLIENTID_CONFIRM verifier */
1713 	scvp = (scid_confirm_verf *)&cp->confirm_verf;
1714 	scvp->cv_impl.gen_num++;
1715 }
1716 
1717 void
1718 rfs4_client_rele(rfs4_client_t *cp)
1719 {
1720 	rfs4_dbe_rele(cp->dbe);
1721 }
1722 
1723 rfs4_client_t *
1724 rfs4_findclient(nfs_client_id4 *client, bool_t *create,	rfs4_client_t *oldcp)
1725 {
1726 	rfs4_client_t *cp;
1727 
1728 
1729 	if (oldcp) {
1730 		rw_enter(&rfs4_findclient_lock, RW_WRITER);
1731 		rfs4_dbe_hide(oldcp->dbe);
1732 	} else {
1733 		rw_enter(&rfs4_findclient_lock, RW_READER);
1734 	}
1735 
1736 	cp = (rfs4_client_t *)rfs4_dbsearch(rfs4_nfsclnt_idx, client,
1737 	    create, (void *)client, RFS4_DBS_VALID);
1738 
1739 	if (oldcp)
1740 		rfs4_dbe_unhide(oldcp->dbe);
1741 
1742 	rw_exit(&rfs4_findclient_lock);
1743 
1744 	return (cp);
1745 }
1746 
1747 rfs4_client_t *
1748 rfs4_findclient_by_id(clientid4 clientid, bool_t find_unconfirmed)
1749 {
1750 	rfs4_client_t *cp;
1751 	bool_t create = FALSE;
1752 	cid *cidp = (cid *)&clientid;
1753 
1754 	/* If we're a cluster and the nodeid isn't right, short-circuit */
1755 	if (cluster_bootflags & CLUSTER_BOOTED && foreign_clientid(cidp))
1756 		return (NULL);
1757 
1758 	rw_enter(&rfs4_findclient_lock, RW_READER);
1759 
1760 	cp = (rfs4_client_t *)rfs4_dbsearch(rfs4_clientid_idx, &clientid,
1761 	    &create, NULL, RFS4_DBS_VALID);
1762 
1763 	rw_exit(&rfs4_findclient_lock);
1764 
1765 	if (cp && cp->need_confirm && find_unconfirmed == FALSE) {
1766 		rfs4_client_rele(cp);
1767 		return (NULL);
1768 	} else {
1769 		return (cp);
1770 	}
1771 }
1772 
1773 bool_t
1774 rfs4_lease_expired(rfs4_client_t *cp)
1775 {
1776 	bool_t rc;
1777 
1778 	rfs4_dbe_lock(cp->dbe);
1779 
1780 	/*
1781 	 * If the admin has executed clear_locks for this
1782 	 * client id, force expire will be set, so no need
1783 	 * to calculate anything because it's "outa here".
1784 	 */
1785 	if (cp->forced_expire) {
1786 		rc = TRUE;
1787 	} else {
1788 		rc = (gethrestime_sec() - cp->last_access > rfs4_lease_time);
1789 	}
1790 
1791 	/*
1792 	 * If the lease has expired we will also want
1793 	 * to remove any stable storage state data. So
1794 	 * mark the client id accordingly.
1795 	 */
1796 	if (!cp->ss_remove)
1797 		cp->ss_remove = (rc == TRUE);
1798 
1799 	rfs4_dbe_unlock(cp->dbe);
1800 
1801 	return (rc);
1802 }
1803 
1804 void
1805 rfs4_update_lease(rfs4_client_t *cp)
1806 {
1807 	rfs4_dbe_lock(cp->dbe);
1808 	if (!cp->forced_expire)
1809 		cp->last_access = gethrestime_sec();
1810 	rfs4_dbe_unlock(cp->dbe);
1811 }
1812 
1813 
1814 static bool_t
1815 EQOPENOWNER(open_owner4 *a, open_owner4 *b)
1816 {
1817 	bool_t rc;
1818 
1819 	if (a->clientid != b->clientid)
1820 		return (FALSE);
1821 
1822 	if (a->owner_len != b->owner_len)
1823 		return (FALSE);
1824 
1825 	rc = (bcmp(a->owner_val, b->owner_val, a->owner_len) == 0);
1826 
1827 	return (rc);
1828 }
1829 
1830 static uint_t
1831 openowner_hash(void *key)
1832 {
1833 	int i;
1834 	open_owner4 *openowner = key;
1835 	uint_t hash = 0;
1836 
1837 	for (i = 0; i < openowner->owner_len; i++) {
1838 		hash <<= 4;
1839 		hash += (uint_t)openowner->owner_val[i];
1840 	}
1841 	hash += (uint_t)openowner->clientid;
1842 	hash |= (openowner->clientid >> 32);
1843 
1844 	return (hash);
1845 }
1846 
1847 static bool_t
1848 openowner_compare(rfs4_entry_t u_entry, void *key)
1849 {
1850 	rfs4_openowner_t *op = (rfs4_openowner_t *)u_entry;
1851 	open_owner4 *arg = key;
1852 
1853 	return (EQOPENOWNER(&op->owner, arg));
1854 }
1855 
1856 void *
1857 openowner_mkkey(rfs4_entry_t u_entry)
1858 {
1859 	rfs4_openowner_t *op = (rfs4_openowner_t *)u_entry;
1860 
1861 	return (&op->owner);
1862 }
1863 
1864 static bool_t
1865 rfs4_openowner_expiry(rfs4_entry_t u_entry)
1866 {
1867 	rfs4_openowner_t *op = (rfs4_openowner_t *)u_entry;
1868 
1869 	if (rfs4_dbe_is_invalid(op->dbe))
1870 		return (TRUE);
1871 	return ((gethrestime_sec() - op->client->last_access
1872 	    > rfs4_lease_time));
1873 }
1874 
1875 static void
1876 rfs4_openowner_destroy(rfs4_entry_t u_entry)
1877 {
1878 	rfs4_openowner_t *op = (rfs4_openowner_t *)u_entry;
1879 
1880 	rfs4_sw_destroy(&op->oo_sw);
1881 
1882 	/* Remove open owner from client's lists of open owners */
1883 	rfs4_dbe_lock(op->client->dbe);
1884 
1885 	remque(&op->openownerlist);
1886 	op->openownerlist.next = op->openownerlist.prev = &op->openownerlist;
1887 
1888 	rfs4_dbe_unlock(op->client->dbe);
1889 
1890 	/* One less reference to the client */
1891 	rfs4_client_rele(op->client);
1892 	op->client = NULL;
1893 
1894 	/* Free the last reply for this lock owner */
1895 	rfs4_free_reply(op->reply);
1896 
1897 	if (op->reply_fh.nfs_fh4_val) {
1898 		kmem_free(op->reply_fh.nfs_fh4_val, op->reply_fh.nfs_fh4_len);
1899 		op->reply_fh.nfs_fh4_val = NULL;
1900 		op->reply_fh.nfs_fh4_len = 0;
1901 	}
1902 
1903 	/* Free the lock owner id */
1904 	kmem_free(op->owner.owner_val, op->owner.owner_len);
1905 }
1906 
1907 void
1908 rfs4_openowner_rele(rfs4_openowner_t *op)
1909 {
1910 	rfs4_dbe_rele(op->dbe);
1911 }
1912 
1913 static bool_t
1914 rfs4_openowner_create(rfs4_entry_t u_entry, void *arg)
1915 {
1916 	rfs4_openowner_t *op = (rfs4_openowner_t *)u_entry;
1917 	rfs4_openowner_t *argp = (rfs4_openowner_t *)arg;
1918 	open_owner4 *openowner = &argp->owner;
1919 	seqid4 seqid = argp->open_seqid;
1920 	rfs4_client_t *cp;
1921 	bool_t create = FALSE;
1922 
1923 	rw_enter(&rfs4_findclient_lock, RW_READER);
1924 
1925 	cp = (rfs4_client_t *)rfs4_dbsearch(rfs4_clientid_idx,
1926 	    &openowner->clientid,
1927 	    &create, NULL, RFS4_DBS_VALID);
1928 
1929 	rw_exit(&rfs4_findclient_lock);
1930 
1931 	if (cp == NULL)
1932 		return (FALSE);
1933 
1934 	op->reply_fh.nfs_fh4_len = 0;
1935 	op->reply_fh.nfs_fh4_val = NULL;
1936 
1937 	op->owner.clientid = openowner->clientid;
1938 	op->owner.owner_val =
1939 	    kmem_alloc(openowner->owner_len, KM_SLEEP);
1940 
1941 	bcopy(openowner->owner_val,
1942 	    op->owner.owner_val, openowner->owner_len);
1943 
1944 	op->owner.owner_len = openowner->owner_len;
1945 
1946 	op->need_confirm = TRUE;
1947 
1948 	rfs4_sw_init(&op->oo_sw);
1949 
1950 	op->open_seqid = seqid;
1951 	bzero(op->reply, sizeof (nfs_resop4));
1952 	op->client = cp;
1953 	op->cr_set = NULL;
1954 	/* Init lists for remque/insque */
1955 	op->ownerstateids.next = op->ownerstateids.prev = &op->ownerstateids;
1956 	op->ownerstateids.sp = NULL; /* NULL since this is the state list */
1957 	op->openownerlist.next = op->openownerlist.prev = &op->openownerlist;
1958 	op->openownerlist.oop = op; /* ourselves */
1959 
1960 	/* Insert openowner into client's open owner list */
1961 	rfs4_dbe_lock(cp->dbe);
1962 
1963 	insque(&op->openownerlist, cp->openownerlist.prev);
1964 
1965 	rfs4_dbe_unlock(cp->dbe);
1966 
1967 	return (TRUE);
1968 }
1969 
1970 rfs4_openowner_t *
1971 rfs4_findopenowner(open_owner4 *openowner, bool_t *create, seqid4 seqid)
1972 {
1973 	rfs4_openowner_t *op;
1974 	rfs4_openowner_t arg;
1975 
1976 	arg.owner = *openowner;
1977 	arg.open_seqid = seqid;
1978 	op = (rfs4_openowner_t *)rfs4_dbsearch(rfs4_openowner_idx, openowner,
1979 	    create, &arg, RFS4_DBS_VALID);
1980 
1981 	return (op);
1982 }
1983 
1984 void
1985 rfs4_update_open_sequence(rfs4_openowner_t *op)
1986 {
1987 
1988 	rfs4_dbe_lock(op->dbe);
1989 
1990 	op->open_seqid++;
1991 
1992 	rfs4_dbe_unlock(op->dbe);
1993 }
1994 
1995 void
1996 rfs4_update_open_resp(rfs4_openowner_t *op, nfs_resop4 *resp, nfs_fh4 *fh)
1997 {
1998 
1999 	rfs4_dbe_lock(op->dbe);
2000 
2001 	rfs4_free_reply(op->reply);
2002 
2003 	rfs4_copy_reply(op->reply, resp);
2004 
2005 	/* Save the filehandle if provided and free if not used */
2006 	if (resp->nfs_resop4_u.opopen.status == NFS4_OK &&
2007 	    fh && fh->nfs_fh4_len) {
2008 		if (op->reply_fh.nfs_fh4_val == NULL)
2009 			op->reply_fh.nfs_fh4_val =
2010 			    kmem_alloc(fh->nfs_fh4_len, KM_SLEEP);
2011 		nfs_fh4_copy(fh, &op->reply_fh);
2012 	} else {
2013 		if (op->reply_fh.nfs_fh4_val) {
2014 			kmem_free(op->reply_fh.nfs_fh4_val,
2015 			    op->reply_fh.nfs_fh4_len);
2016 			op->reply_fh.nfs_fh4_val = NULL;
2017 			op->reply_fh.nfs_fh4_len = 0;
2018 		}
2019 	}
2020 
2021 	rfs4_dbe_unlock(op->dbe);
2022 }
2023 
2024 static bool_t
2025 lockowner_compare(rfs4_entry_t u_entry, void *key)
2026 {
2027 	rfs4_lockowner_t *lo = (rfs4_lockowner_t *)u_entry;
2028 	lock_owner4 *b = (lock_owner4 *)key;
2029 
2030 	if (lo->owner.clientid != b->clientid)
2031 		return (FALSE);
2032 
2033 	if (lo->owner.owner_len != b->owner_len)
2034 		return (FALSE);
2035 
2036 	return (bcmp(lo->owner.owner_val, b->owner_val,
2037 	    lo->owner.owner_len) == 0);
2038 }
2039 
2040 void *
2041 lockowner_mkkey(rfs4_entry_t u_entry)
2042 {
2043 	rfs4_lockowner_t *lo = (rfs4_lockowner_t *)u_entry;
2044 
2045 	return (&lo->owner);
2046 }
2047 
2048 static uint32_t
2049 lockowner_hash(void *key)
2050 {
2051 	int i;
2052 	lock_owner4 *lockowner = key;
2053 	uint_t hash = 0;
2054 
2055 	for (i = 0; i < lockowner->owner_len; i++) {
2056 		hash <<= 4;
2057 		hash += (uint_t)lockowner->owner_val[i];
2058 	}
2059 	hash += (uint_t)lockowner->clientid;
2060 	hash |= (lockowner->clientid >> 32);
2061 
2062 	return (hash);
2063 }
2064 
2065 static uint32_t
2066 pid_hash(void *key)
2067 {
2068 	return ((uint32_t)(uintptr_t)key);
2069 }
2070 
2071 static void *
2072 pid_mkkey(rfs4_entry_t u_entry)
2073 {
2074 	rfs4_lockowner_t *lo = (rfs4_lockowner_t *)u_entry;
2075 
2076 	return ((void *)(uintptr_t)lo->pid);
2077 }
2078 
2079 static bool_t
2080 pid_compare(rfs4_entry_t u_entry, void *key)
2081 {
2082 	rfs4_lockowner_t *lo = (rfs4_lockowner_t *)u_entry;
2083 
2084 	return (lo->pid == (pid_t)(uintptr_t)key);
2085 }
2086 
2087 static void
2088 rfs4_lockowner_destroy(rfs4_entry_t u_entry)
2089 {
2090 	rfs4_lockowner_t *lo = (rfs4_lockowner_t *)u_entry;
2091 
2092 	/* Free the lock owner id */
2093 	kmem_free(lo->owner.owner_val, lo->owner.owner_len);
2094 	rfs4_client_rele(lo->client);
2095 }
2096 
2097 void
2098 rfs4_lockowner_rele(rfs4_lockowner_t *lo)
2099 {
2100 	rfs4_dbe_rele(lo->dbe);
2101 }
2102 
2103 /* ARGSUSED */
2104 static bool_t
2105 rfs4_lockowner_expiry(rfs4_entry_t u_entry)
2106 {
2107 	/*
2108 	 * Since expiry is called with no other references on
2109 	 * this struct, go ahead and have it removed.
2110 	 */
2111 	return (TRUE);
2112 }
2113 
2114 static bool_t
2115 rfs4_lockowner_create(rfs4_entry_t u_entry, void *arg)
2116 {
2117 	rfs4_lockowner_t *lo = (rfs4_lockowner_t *)u_entry;
2118 	lock_owner4 *lockowner = (lock_owner4 *)arg;
2119 	rfs4_client_t *cp;
2120 	bool_t create = FALSE;
2121 
2122 	rw_enter(&rfs4_findclient_lock, RW_READER);
2123 
2124 	cp = (rfs4_client_t *)rfs4_dbsearch(rfs4_clientid_idx,
2125 	    &lockowner->clientid,
2126 	    &create, NULL, RFS4_DBS_VALID);
2127 
2128 	rw_exit(&rfs4_findclient_lock);
2129 
2130 	if (cp == NULL)
2131 		return (FALSE);
2132 
2133 	/* Reference client */
2134 	lo->client = cp;
2135 	lo->owner.clientid = lockowner->clientid;
2136 	lo->owner.owner_val = kmem_alloc(lockowner->owner_len, KM_SLEEP);
2137 	bcopy(lockowner->owner_val, lo->owner.owner_val, lockowner->owner_len);
2138 	lo->owner.owner_len = lockowner->owner_len;
2139 	lo->pid = rfs4_dbe_getid(lo->dbe);
2140 
2141 	return (TRUE);
2142 }
2143 
2144 rfs4_lockowner_t *
2145 rfs4_findlockowner(lock_owner4 *lockowner, bool_t *create)
2146 {
2147 	rfs4_lockowner_t *lo;
2148 
2149 	lo = (rfs4_lockowner_t *)rfs4_dbsearch(rfs4_lockowner_idx, lockowner,
2150 	    create, lockowner, RFS4_DBS_VALID);
2151 
2152 	return (lo);
2153 }
2154 
2155 rfs4_lockowner_t *
2156 rfs4_findlockowner_by_pid(pid_t pid)
2157 {
2158 	rfs4_lockowner_t *lo;
2159 	bool_t create = FALSE;
2160 
2161 	lo = (rfs4_lockowner_t *)rfs4_dbsearch(rfs4_lockowner_pid_idx,
2162 	    (void *)(uintptr_t)pid, &create, NULL, RFS4_DBS_VALID);
2163 
2164 	return (lo);
2165 }
2166 
2167 
2168 static uint32_t
2169 file_hash(void *key)
2170 {
2171 	return (ADDRHASH(key));
2172 }
2173 
2174 static void *
2175 file_mkkey(rfs4_entry_t u_entry)
2176 {
2177 	rfs4_file_t *fp = (rfs4_file_t *)u_entry;
2178 
2179 	return (fp->vp);
2180 }
2181 
2182 static bool_t
2183 file_compare(rfs4_entry_t u_entry, void *key)
2184 {
2185 	rfs4_file_t *fp = (rfs4_file_t *)u_entry;
2186 
2187 	return (fp->vp == (vnode_t *)key);
2188 }
2189 
2190 static void
2191 rfs4_file_destroy(rfs4_entry_t u_entry)
2192 {
2193 	rfs4_file_t *fp = (rfs4_file_t *)u_entry;
2194 
2195 	ASSERT(fp->delegationlist.next == &fp->delegationlist);
2196 	if (fp->filehandle.nfs_fh4_val)
2197 		kmem_free(fp->filehandle.nfs_fh4_val,
2198 		    fp->filehandle.nfs_fh4_len);
2199 	cv_destroy(fp->dinfo->recall_cv);
2200 	if (fp->vp) {
2201 		vnode_t *vp = fp->vp;
2202 
2203 		mutex_enter(&vp->v_lock);
2204 		(void) vsd_set(vp, nfs4_srv_vkey, NULL);
2205 		mutex_exit(&vp->v_lock);
2206 		VN_RELE(vp);
2207 		fp->vp = NULL;
2208 	}
2209 	rw_destroy(&fp->file_rwlock);
2210 }
2211 
2212 /*
2213  * Used to unlock the underlying dbe struct only
2214  */
2215 void
2216 rfs4_file_rele(rfs4_file_t *fp)
2217 {
2218 	rfs4_dbe_rele(fp->dbe);
2219 }
2220 
2221 /*
2222  * Used to unlock the file rw lock and the file's dbe entry
2223  * Only used to pair with rfs4_findfile_withlock()
2224  */
2225 void
2226 rfs4_file_rele_withunlock(rfs4_file_t *fp)
2227 {
2228 	rw_exit(&fp->file_rwlock);
2229 	rfs4_dbe_rele(fp->dbe);
2230 }
2231 
2232 typedef struct {
2233     vnode_t *vp;
2234     nfs_fh4 *fh;
2235 } rfs4_fcreate_arg;
2236 
2237 static bool_t
2238 rfs4_file_create(rfs4_entry_t u_entry, void *arg)
2239 {
2240 	rfs4_file_t *fp = (rfs4_file_t *)u_entry;
2241 	rfs4_fcreate_arg *ap = (rfs4_fcreate_arg *)arg;
2242 	vnode_t *vp = ap->vp;
2243 	nfs_fh4 *fh = ap->fh;
2244 
2245 	VN_HOLD(vp);
2246 
2247 	fp->filehandle.nfs_fh4_len = 0;
2248 	fp->filehandle.nfs_fh4_val = NULL;
2249 	ASSERT(fh && fh->nfs_fh4_len);
2250 	if (fh && fh->nfs_fh4_len) {
2251 		fp->filehandle.nfs_fh4_val =
2252 		    kmem_alloc(fh->nfs_fh4_len, KM_SLEEP);
2253 		nfs_fh4_copy(fh, &fp->filehandle);
2254 	}
2255 	fp->vp = vp;
2256 
2257 	/* Init list for remque/insque */
2258 	fp->delegationlist.next = fp->delegationlist.prev =
2259 	    &fp->delegationlist;
2260 	fp->delegationlist.dsp = NULL; /* NULL since this is state list */
2261 
2262 	fp->share_deny = fp->share_access = fp->access_read = 0;
2263 	fp->access_write = fp->deny_read = fp->deny_write = 0;
2264 
2265 	mutex_init(fp->dinfo->recall_lock, NULL, MUTEX_DEFAULT, NULL);
2266 	cv_init(fp->dinfo->recall_cv, NULL, CV_DEFAULT, NULL);
2267 
2268 	fp->dinfo->dtype = OPEN_DELEGATE_NONE;
2269 
2270 	rw_init(&fp->file_rwlock, NULL, RW_DEFAULT, NULL);
2271 
2272 	mutex_enter(&vp->v_lock);
2273 	if (vsd_set(vp, nfs4_srv_vkey, (void *)fp)) {
2274 		ASSERT(FALSE);
2275 		cmn_err(CE_WARN, "rfs4_file_create: vsd_set failed.");
2276 	}
2277 	mutex_exit(&vp->v_lock);
2278 
2279 	return (TRUE);
2280 }
2281 
2282 rfs4_file_t *
2283 rfs4_findfile(vnode_t *vp, nfs_fh4 *fh, bool_t *create)
2284 {
2285 	rfs4_file_t *fp;
2286 	rfs4_fcreate_arg arg;
2287 
2288 	arg.vp = vp;
2289 	arg.fh = fh;
2290 
2291 	if (*create == TRUE)
2292 		fp = (rfs4_file_t *)rfs4_dbsearch(rfs4_file_idx, vp, create,
2293 		    &arg, RFS4_DBS_VALID);
2294 	else {
2295 		mutex_enter(&vp->v_lock);
2296 		fp = (rfs4_file_t *)vsd_get(vp, nfs4_srv_vkey);
2297 		mutex_exit(&vp->v_lock);
2298 		if (fp) {
2299 			rfs4_dbe_lock(fp->dbe);
2300 			if (rfs4_dbe_is_invalid(fp->dbe) ||
2301 			    (rfs4_dbe_refcnt(fp->dbe) == 0)) {
2302 				rfs4_dbe_unlock(fp->dbe);
2303 				fp = NULL;
2304 			} else {
2305 				rfs4_dbe_hold(fp->dbe);
2306 				rfs4_dbe_unlock(fp->dbe);
2307 			}
2308 		}
2309 	}
2310 	return (fp);
2311 }
2312 
2313 /*
2314  * Find a file in the db and once it is located, take the rw lock.
2315  * Need to check the vnode pointer and if it does not exist (it was
2316  * removed between the db location and check) redo the find.  This
2317  * assumes that a file struct that has a NULL vnode pointer is marked
2318  * at 'invalid' and will not be found in the db the second time
2319  * around.
2320  */
2321 rfs4_file_t *
2322 rfs4_findfile_withlock(vnode_t *vp, nfs_fh4 *fh, bool_t *create)
2323 {
2324 	rfs4_file_t *fp;
2325 	rfs4_fcreate_arg arg;
2326 	bool_t screate = *create;
2327 
2328 	if (screate == FALSE) {
2329 		mutex_enter(&vp->v_lock);
2330 		fp = (rfs4_file_t *)vsd_get(vp, nfs4_srv_vkey);
2331 		mutex_exit(&vp->v_lock);
2332 		if (fp) {
2333 			rfs4_dbe_lock(fp->dbe);
2334 			if (rfs4_dbe_is_invalid(fp->dbe) ||
2335 			    (rfs4_dbe_refcnt(fp->dbe) == 0)) {
2336 				rfs4_dbe_unlock(fp->dbe);
2337 				fp = NULL;
2338 			} else {
2339 				rfs4_dbe_hold(fp->dbe);
2340 				rfs4_dbe_unlock(fp->dbe);
2341 				rw_enter(&fp->file_rwlock, RW_WRITER);
2342 				if (fp->vp == NULL) {
2343 					rw_exit(&fp->file_rwlock);
2344 					rfs4_file_rele(fp);
2345 					fp = NULL;
2346 				}
2347 			}
2348 		}
2349 	} else {
2350 retry:
2351 		arg.vp = vp;
2352 		arg.fh = fh;
2353 
2354 		fp = (rfs4_file_t *)rfs4_dbsearch(rfs4_file_idx, vp, create,
2355 		    &arg, RFS4_DBS_VALID);
2356 		if (fp != NULL) {
2357 			rw_enter(&fp->file_rwlock, RW_WRITER);
2358 			if (fp->vp == NULL) {
2359 				rw_exit(&fp->file_rwlock);
2360 				rfs4_file_rele(fp);
2361 				*create = screate;
2362 				goto retry;
2363 			}
2364 		}
2365 	}
2366 
2367 	return (fp);
2368 }
2369 
2370 static uint32_t
2371 lo_state_hash(void *key)
2372 {
2373 	stateid_t *id = key;
2374 
2375 	return (id->bits.ident+id->bits.pid);
2376 }
2377 
2378 static bool_t
2379 lo_state_compare(rfs4_entry_t u_entry, void *key)
2380 {
2381 	rfs4_lo_state_t *lop = (rfs4_lo_state_t *)u_entry;
2382 	stateid_t *id = key;
2383 	bool_t rc;
2384 
2385 	rc = (lop->lockid.bits.boottime == id->bits.boottime &&
2386 	    lop->lockid.bits.type == id->bits.type &&
2387 	    lop->lockid.bits.ident == id->bits.ident &&
2388 	    lop->lockid.bits.pid == id->bits.pid);
2389 
2390 	return (rc);
2391 }
2392 
2393 static void *
2394 lo_state_mkkey(rfs4_entry_t u_entry)
2395 {
2396 	rfs4_lo_state_t *lsp = (rfs4_lo_state_t *)u_entry;
2397 
2398 	return (&lsp->lockid);
2399 }
2400 
2401 static bool_t
2402 rfs4_lo_state_expiry(rfs4_entry_t u_entry)
2403 {
2404 	rfs4_lo_state_t *lsp = (rfs4_lo_state_t *)u_entry;
2405 
2406 	if (rfs4_dbe_is_invalid(lsp->dbe))
2407 		return (TRUE);
2408 	if (lsp->state->closed)
2409 		return (TRUE);
2410 	return ((gethrestime_sec() - lsp->state->owner->client->last_access
2411 	    > rfs4_lease_time));
2412 }
2413 
2414 static void
2415 rfs4_lo_state_destroy(rfs4_entry_t u_entry)
2416 {
2417 	rfs4_lo_state_t *lsp = (rfs4_lo_state_t *)u_entry;
2418 
2419 	rfs4_sw_destroy(&lsp->ls_sw);
2420 
2421 	/* Make sure to release the file locks */
2422 	if (lsp->locks_cleaned == FALSE) {
2423 		lsp->locks_cleaned = TRUE;
2424 		if (lsp->locker->client->sysidt != LM_NOSYSID) {
2425 			/* Is the PxFS kernel module loaded? */
2426 			if (lm_remove_file_locks != NULL) {
2427 				int new_sysid;
2428 
2429 				/* Encode the cluster nodeid in new sysid */
2430 				new_sysid = lsp->locker->client->sysidt;
2431 				lm_set_nlmid_flk(&new_sysid);
2432 
2433 				/*
2434 				 * This PxFS routine removes file locks for a
2435 				 * client over all nodes of a cluster.
2436 				 */
2437 				DTRACE_PROBE1(nfss_i_clust_rm_lck,
2438 				    int, new_sysid);
2439 				(*lm_remove_file_locks)(new_sysid);
2440 			} else {
2441 				(void) cleanlocks(lsp->state->finfo->vp,
2442 				    lsp->locker->pid,
2443 				    lsp->locker->client->sysidt);
2444 			}
2445 		}
2446 	}
2447 
2448 	rfs4_dbe_lock(lsp->state->dbe);
2449 
2450 	remque(&lsp->lockownerlist);
2451 	lsp->lockownerlist.next = lsp->lockownerlist.prev =
2452 	    &lsp->lockownerlist;
2453 
2454 	rfs4_dbe_unlock(lsp->state->dbe);
2455 
2456 	/* Free the last reply for this state */
2457 	rfs4_free_reply(lsp->reply);
2458 
2459 	rfs4_lockowner_rele(lsp->locker);
2460 	lsp->locker = NULL;
2461 
2462 	rfs4_state_rele_nounlock(lsp->state);
2463 	lsp->state = NULL;
2464 }
2465 
2466 static bool_t
2467 rfs4_lo_state_create(rfs4_entry_t u_entry, void *arg)
2468 {
2469 	rfs4_lo_state_t *lsp = (rfs4_lo_state_t *)u_entry;
2470 	rfs4_lo_state_t *argp = (rfs4_lo_state_t *)arg;
2471 	rfs4_lockowner_t *lo = argp->locker;
2472 	rfs4_state_t *sp = argp->state;
2473 
2474 	lsp->state = sp;
2475 
2476 	lsp->lockid = sp->stateid;
2477 	lsp->lockid.bits.type = LOCKID;
2478 	lsp->lockid.bits.chgseq = 0;
2479 	lsp->lockid.bits.pid = lo->pid;
2480 
2481 	lsp->locks_cleaned = FALSE;
2482 	lsp->lock_completed = FALSE;
2483 
2484 	rfs4_sw_init(&lsp->ls_sw);
2485 
2486 	/* Attached the supplied lock owner */
2487 	rfs4_dbe_hold(lo->dbe);
2488 	lsp->locker = lo;
2489 
2490 	lsp->lockownerlist.next = lsp->lockownerlist.prev =
2491 	    &lsp->lockownerlist;
2492 	lsp->lockownerlist.lsp = lsp;
2493 
2494 	rfs4_dbe_lock(sp->dbe);
2495 
2496 	insque(&lsp->lockownerlist, sp->lockownerlist.prev);
2497 
2498 	rfs4_dbe_hold(sp->dbe);
2499 
2500 	rfs4_dbe_unlock(sp->dbe);
2501 
2502 	return (TRUE);
2503 }
2504 
2505 void
2506 rfs4_lo_state_rele(rfs4_lo_state_t *lsp, bool_t unlock_fp)
2507 {
2508 	if (unlock_fp == TRUE)
2509 		rw_exit(&lsp->state->finfo->file_rwlock);
2510 	rfs4_dbe_rele(lsp->dbe);
2511 }
2512 
2513 static rfs4_lo_state_t *
2514 rfs4_findlo_state(stateid_t *id, bool_t lock_fp)
2515 {
2516 	rfs4_lo_state_t *lsp;
2517 	bool_t create = FALSE;
2518 
2519 	lsp = (rfs4_lo_state_t *)rfs4_dbsearch(rfs4_lo_state_idx, id,
2520 	    &create, NULL, RFS4_DBS_VALID);
2521 	if (lock_fp == TRUE && lsp != NULL)
2522 		rw_enter(&lsp->state->finfo->file_rwlock, RW_READER);
2523 
2524 	return (lsp);
2525 }
2526 
2527 
2528 static uint32_t
2529 lo_state_lo_hash(void *key)
2530 {
2531 	rfs4_lo_state_t *lop = key;
2532 
2533 	return (ADDRHASH(lop->locker) ^ ADDRHASH(lop->state));
2534 }
2535 
2536 static bool_t
2537 lo_state_lo_compare(rfs4_entry_t u_entry, void *key)
2538 {
2539 	rfs4_lo_state_t *lop = (rfs4_lo_state_t *)u_entry;
2540 	rfs4_lo_state_t *keyp = key;
2541 
2542 	return (keyp->locker == lop->locker && keyp->state == lop->state);
2543 }
2544 
2545 static void *
2546 lo_state_lo_mkkey(rfs4_entry_t u_entry)
2547 {
2548 	return (u_entry);
2549 }
2550 
2551 rfs4_lo_state_t *
2552 rfs4_findlo_state_by_owner(rfs4_lockowner_t *lo,
2553 			rfs4_state_t *sp, bool_t *create)
2554 {
2555 	rfs4_lo_state_t *lsp;
2556 	rfs4_lo_state_t arg;
2557 
2558 	arg.locker = lo;
2559 	arg.state = sp;
2560 
2561 	lsp = (rfs4_lo_state_t *)rfs4_dbsearch(rfs4_lo_state_owner_idx, &arg,
2562 	    create, &arg, RFS4_DBS_VALID);
2563 
2564 	return (lsp);
2565 }
2566 
2567 static stateid_t
2568 get_stateid(id_t eid)
2569 {
2570 	stateid_t id;
2571 
2572 	id.bits.boottime = rfs4_start_time;
2573 	id.bits.ident = eid;
2574 	id.bits.chgseq = 0;
2575 	id.bits.type = 0;
2576 	id.bits.pid = 0;
2577 
2578 	/*
2579 	 * If we are booted as a cluster node, embed our nodeid.
2580 	 * We've already done sanity checks in rfs4_client_create() so no
2581 	 * need to repeat them here.
2582 	 */
2583 	id.bits.clnodeid = (cluster_bootflags & CLUSTER_BOOTED) ?
2584 	    clconf_get_nodeid() : 0;
2585 
2586 	return (id);
2587 }
2588 
2589 /*
2590  * For use only when booted as a cluster node.
2591  * Returns TRUE if the embedded nodeid indicates that this stateid was
2592  * generated on another node.
2593  */
2594 static int
2595 foreign_stateid(stateid_t *id)
2596 {
2597 	ASSERT(cluster_bootflags & CLUSTER_BOOTED);
2598 	return (id->bits.clnodeid != (uint32_t)clconf_get_nodeid());
2599 }
2600 
2601 /*
2602  * For use only when booted as a cluster node.
2603  * Returns TRUE if the embedded nodeid indicates that this clientid was
2604  * generated on another node.
2605  */
2606 static int
2607 foreign_clientid(cid *cidp)
2608 {
2609 	ASSERT(cluster_bootflags & CLUSTER_BOOTED);
2610 	return (cidp->impl_id.c_id >> CLUSTER_NODEID_SHIFT !=
2611 	    (uint32_t)clconf_get_nodeid());
2612 }
2613 
2614 /*
2615  * For use only when booted as a cluster node.
2616  * Embed our cluster nodeid into the clientid.
2617  */
2618 static void
2619 embed_nodeid(cid *cidp)
2620 {
2621 	int clnodeid;
2622 	/*
2623 	 * Currently, our state tables are small enough that their
2624 	 * ids will leave enough bits free for the nodeid. If the
2625 	 * tables become larger, we mustn't overwrite the id.
2626 	 * Equally, we only have room for so many bits of nodeid, so
2627 	 * must check that too.
2628 	 */
2629 	ASSERT(cluster_bootflags & CLUSTER_BOOTED);
2630 	ASSERT(cidp->impl_id.c_id >> CLUSTER_NODEID_SHIFT == 0);
2631 	clnodeid = clconf_get_nodeid();
2632 	ASSERT(clnodeid <= CLUSTER_MAX_NODEID);
2633 	ASSERT(clnodeid != NODEID_UNKNOWN);
2634 	cidp->impl_id.c_id |= (clnodeid << CLUSTER_NODEID_SHIFT);
2635 }
2636 
2637 static uint32_t
2638 state_hash(void *key)
2639 {
2640 	stateid_t *ip = (stateid_t *)key;
2641 
2642 	return (ip->bits.ident);
2643 }
2644 
2645 static bool_t
2646 state_compare(rfs4_entry_t u_entry, void *key)
2647 {
2648 	rfs4_state_t *sp = (rfs4_state_t *)u_entry;
2649 	stateid_t *id = (stateid_t *)key;
2650 	bool_t rc;
2651 
2652 	rc = (sp->stateid.bits.boottime == id->bits.boottime &&
2653 	    sp->stateid.bits.ident == id->bits.ident);
2654 
2655 	return (rc);
2656 }
2657 
2658 static void *
2659 state_mkkey(rfs4_entry_t u_entry)
2660 {
2661 	rfs4_state_t *sp = (rfs4_state_t *)u_entry;
2662 
2663 	return (&sp->stateid);
2664 }
2665 
2666 static void
2667 rfs4_state_destroy(rfs4_entry_t u_entry)
2668 {
2669 	rfs4_state_t *sp = (rfs4_state_t *)u_entry;
2670 
2671 	ASSERT(&sp->lockownerlist == sp->lockownerlist.next);
2672 
2673 	/* release any share locks for this stateid if it's still open */
2674 	if (!sp->closed)
2675 		rfs4_unshare(sp);
2676 
2677 	/* Were done with the file */
2678 	rfs4_file_rele(sp->finfo);
2679 	sp->finfo = NULL;
2680 
2681 	/* And now with the openowner */
2682 	rfs4_dbe_lock(sp->owner->dbe);
2683 
2684 	remque(&sp->ownerstateids);
2685 	sp->ownerstateids.next = sp->ownerstateids.prev = &sp->ownerstateids;
2686 
2687 	rfs4_dbe_unlock(sp->owner->dbe);
2688 
2689 	rfs4_openowner_rele(sp->owner);
2690 	sp->owner = NULL;
2691 }
2692 
2693 static void
2694 rfs4_state_rele_nounlock(rfs4_state_t *sp)
2695 {
2696 	rfs4_dbe_rele(sp->dbe);
2697 }
2698 
2699 void
2700 rfs4_state_rele(rfs4_state_t *sp)
2701 {
2702 	rw_exit(&sp->finfo->file_rwlock);
2703 	rfs4_dbe_rele(sp->dbe);
2704 }
2705 
2706 static uint32_t
2707 deleg_hash(void *key)
2708 {
2709 	rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)key;
2710 
2711 	return (ADDRHASH(dsp->client) ^ ADDRHASH(dsp->finfo));
2712 }
2713 
2714 static bool_t
2715 deleg_compare(rfs4_entry_t u_entry, void *key)
2716 {
2717 	rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)u_entry;
2718 	rfs4_deleg_state_t *kdsp = (rfs4_deleg_state_t *)key;
2719 
2720 	return (dsp->client == kdsp->client && dsp->finfo == kdsp->finfo);
2721 }
2722 
2723 static void *
2724 deleg_mkkey(rfs4_entry_t u_entry)
2725 {
2726 	return (u_entry);
2727 }
2728 
2729 static uint32_t
2730 deleg_state_hash(void *key)
2731 {
2732 	stateid_t *ip = (stateid_t *)key;
2733 
2734 	return (ip->bits.ident);
2735 }
2736 
2737 static bool_t
2738 deleg_state_compare(rfs4_entry_t u_entry, void *key)
2739 {
2740 	rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)u_entry;
2741 	stateid_t *id = (stateid_t *)key;
2742 	bool_t rc;
2743 
2744 	if (id->bits.type != DELEGID)
2745 		return (FALSE);
2746 
2747 	rc = (dsp->delegid.bits.boottime == id->bits.boottime &&
2748 	    dsp->delegid.bits.ident == id->bits.ident);
2749 
2750 	return (rc);
2751 }
2752 
2753 static void *
2754 deleg_state_mkkey(rfs4_entry_t u_entry)
2755 {
2756 	rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)u_entry;
2757 
2758 	return (&dsp->delegid);
2759 }
2760 
2761 static bool_t
2762 rfs4_deleg_state_expiry(rfs4_entry_t u_entry)
2763 {
2764 	rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)u_entry;
2765 
2766 	if (rfs4_dbe_is_invalid(dsp->dbe))
2767 		return (TRUE);
2768 
2769 	if ((gethrestime_sec() - dsp->client->last_access
2770 	    > rfs4_lease_time)) {
2771 		rfs4_dbe_invalidate(dsp->dbe);
2772 		return (TRUE);
2773 	}
2774 
2775 	return (FALSE);
2776 }
2777 
2778 static bool_t
2779 rfs4_deleg_state_create(rfs4_entry_t u_entry, void *argp)
2780 {
2781 	rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)u_entry;
2782 	rfs4_file_t *fp = ((rfs4_deleg_state_t *)argp)->finfo;
2783 	rfs4_client_t *cp = ((rfs4_deleg_state_t *)argp)->client;
2784 
2785 	rfs4_dbe_hold(fp->dbe);
2786 	rfs4_dbe_hold(cp->dbe);
2787 
2788 	dsp->delegid = get_stateid(rfs4_dbe_getid(dsp->dbe));
2789 	dsp->delegid.bits.type = DELEGID;
2790 	dsp->finfo = fp;
2791 	dsp->client = cp;
2792 	dsp->dtype = OPEN_DELEGATE_NONE;
2793 
2794 	dsp->time_granted = gethrestime_sec();	/* observability */
2795 	dsp->time_revoked = 0;
2796 
2797 	/* Init lists for remque/insque */
2798 	dsp->delegationlist.next = dsp->delegationlist.prev =
2799 	    &dsp->delegationlist;
2800 	dsp->delegationlist.dsp = dsp;
2801 
2802 	dsp->clientdeleglist.next = dsp->clientdeleglist.prev =
2803 	    &dsp->clientdeleglist;
2804 	dsp->clientdeleglist.dsp = dsp;
2805 
2806 	/* Insert state on per open owner's list */
2807 	rfs4_dbe_lock(cp->dbe);
2808 
2809 	insque(&dsp->clientdeleglist, cp->clientdeleglist.prev);
2810 
2811 	rfs4_dbe_unlock(cp->dbe);
2812 
2813 	return (TRUE);
2814 }
2815 
2816 static void
2817 rfs4_deleg_state_destroy(rfs4_entry_t u_entry)
2818 {
2819 	rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)u_entry;
2820 
2821 	if (&dsp->delegationlist != dsp->delegationlist.next)
2822 		rfs4_return_deleg(dsp, FALSE);
2823 
2824 	/* Were done with the file */
2825 	rfs4_file_rele(dsp->finfo);
2826 	dsp->finfo = NULL;
2827 
2828 	/* And now with the openowner */
2829 	rfs4_dbe_lock(dsp->client->dbe);
2830 
2831 	remque(&dsp->clientdeleglist);
2832 	dsp->clientdeleglist.next = dsp->clientdeleglist.prev =
2833 	    &dsp->clientdeleglist;
2834 
2835 	rfs4_dbe_unlock(dsp->client->dbe);
2836 
2837 	rfs4_client_rele(dsp->client);
2838 	dsp->client = NULL;
2839 }
2840 
2841 rfs4_deleg_state_t *
2842 rfs4_finddeleg(rfs4_state_t *sp, bool_t *create)
2843 {
2844 	rfs4_deleg_state_t ds, *dsp;
2845 
2846 	ds.client = sp->owner->client;
2847 	ds.finfo = sp->finfo;
2848 
2849 	dsp = (rfs4_deleg_state_t *)rfs4_dbsearch(rfs4_deleg_idx, &ds,
2850 	    create, &ds, RFS4_DBS_VALID);
2851 
2852 	return (dsp);
2853 }
2854 
2855 rfs4_deleg_state_t *
2856 rfs4_finddelegstate(stateid_t *id)
2857 {
2858 	rfs4_deleg_state_t *dsp;
2859 	bool_t create = FALSE;
2860 
2861 	dsp = (rfs4_deleg_state_t *)rfs4_dbsearch(rfs4_deleg_state_idx, id,
2862 	    &create, NULL, RFS4_DBS_VALID);
2863 
2864 	return (dsp);
2865 }
2866 
2867 void
2868 rfs4_deleg_state_rele(rfs4_deleg_state_t *dsp)
2869 {
2870 	rfs4_dbe_rele(dsp->dbe);
2871 }
2872 
2873 void
2874 rfs4_update_lock_sequence(rfs4_lo_state_t *lsp)
2875 {
2876 
2877 	rfs4_dbe_lock(lsp->dbe);
2878 
2879 	/*
2880 	 * If we are skipping sequence id checking, this means that
2881 	 * this is the first lock request and therefore the sequence
2882 	 * id does not need to be updated.  This only happens on the
2883 	 * first lock request for a lockowner
2884 	 */
2885 	if (!lsp->skip_seqid_check)
2886 		lsp->seqid++;
2887 
2888 	rfs4_dbe_unlock(lsp->dbe);
2889 }
2890 
2891 void
2892 rfs4_update_lock_resp(rfs4_lo_state_t *lsp, nfs_resop4 *resp)
2893 {
2894 
2895 	rfs4_dbe_lock(lsp->dbe);
2896 
2897 	rfs4_free_reply(lsp->reply);
2898 
2899 	rfs4_copy_reply(lsp->reply, resp);
2900 
2901 	rfs4_dbe_unlock(lsp->dbe);
2902 }
2903 
2904 void
2905 rfs4_free_opens(rfs4_openowner_t *op, bool_t invalidate,
2906 	bool_t close_of_client)
2907 {
2908 	rfs4_state_t *sp;
2909 
2910 	rfs4_dbe_lock(op->dbe);
2911 
2912 	for (sp = op->ownerstateids.next->sp; sp != NULL;
2913 	    sp = sp->ownerstateids.next->sp) {
2914 		rfs4_state_close(sp, FALSE, close_of_client, CRED());
2915 		if (invalidate == TRUE)
2916 			rfs4_dbe_invalidate(sp->dbe);
2917 	}
2918 
2919 	rfs4_dbe_unlock(op->dbe);
2920 	rfs4_dbe_invalidate(op->dbe);
2921 }
2922 
2923 static uint32_t
2924 state_owner_file_hash(void *key)
2925 {
2926 	rfs4_state_t *sp = key;
2927 
2928 	return (ADDRHASH(sp->owner) ^ ADDRHASH(sp->finfo));
2929 }
2930 
2931 static bool_t
2932 state_owner_file_compare(rfs4_entry_t u_entry, void *key)
2933 {
2934 	rfs4_state_t *sp = (rfs4_state_t *)u_entry;
2935 	rfs4_state_t *arg = key;
2936 
2937 	if (sp->closed == TRUE)
2938 		return (FALSE);
2939 
2940 	return (arg->owner == sp->owner && arg->finfo == sp->finfo);
2941 }
2942 
2943 static void *
2944 state_owner_file_mkkey(rfs4_entry_t u_entry)
2945 {
2946 	return (u_entry);
2947 }
2948 
2949 static uint32_t
2950 state_file_hash(void *key)
2951 {
2952 	return (ADDRHASH(key));
2953 }
2954 
2955 static bool_t
2956 state_file_compare(rfs4_entry_t u_entry, void *key)
2957 {
2958 	rfs4_state_t *sp = (rfs4_state_t *)u_entry;
2959 	rfs4_file_t *fp = key;
2960 
2961 	if (sp->closed == TRUE)
2962 		return (FALSE);
2963 
2964 	return (fp == sp->finfo);
2965 }
2966 
2967 static void *
2968 state_file_mkkey(rfs4_entry_t u_entry)
2969 {
2970 	rfs4_state_t *sp = (rfs4_state_t *)u_entry;
2971 
2972 	return (sp->finfo);
2973 }
2974 
2975 rfs4_state_t *
2976 rfs4_findstate_by_owner_file(rfs4_openowner_t *op, rfs4_file_t *file,
2977 	bool_t *create)
2978 {
2979 	rfs4_state_t *sp;
2980 	rfs4_state_t key;
2981 
2982 	key.owner = op;
2983 	key.finfo = file;
2984 
2985 	sp = (rfs4_state_t *)rfs4_dbsearch(rfs4_state_owner_file_idx, &key,
2986 	    create, &key, RFS4_DBS_VALID);
2987 
2988 	return (sp);
2989 }
2990 
2991 /* This returns ANY state struct that refers to this file */
2992 static rfs4_state_t *
2993 rfs4_findstate_by_file(rfs4_file_t *fp)
2994 {
2995 	bool_t create = FALSE;
2996 
2997 	return ((rfs4_state_t *)rfs4_dbsearch(rfs4_state_file_idx, fp,
2998 	    &create, fp, RFS4_DBS_VALID));
2999 }
3000 
3001 static bool_t
3002 rfs4_state_expiry(rfs4_entry_t u_entry)
3003 {
3004 	rfs4_state_t *sp = (rfs4_state_t *)u_entry;
3005 
3006 	if (rfs4_dbe_is_invalid(sp->dbe))
3007 		return (TRUE);
3008 
3009 	if (sp->closed == TRUE &&
3010 	    ((gethrestime_sec() - rfs4_dbe_get_timerele(sp->dbe))
3011 	    > rfs4_lease_time))
3012 		return (TRUE);
3013 
3014 	return ((gethrestime_sec() - sp->owner->client->last_access
3015 	    > rfs4_lease_time));
3016 }
3017 
3018 static bool_t
3019 rfs4_state_create(rfs4_entry_t u_entry, void *argp)
3020 {
3021 	rfs4_state_t *sp = (rfs4_state_t *)u_entry;
3022 	rfs4_file_t *fp = ((rfs4_state_t *)argp)->finfo;
3023 	rfs4_openowner_t *op = ((rfs4_state_t *)argp)->owner;
3024 
3025 	rfs4_dbe_hold(fp->dbe);
3026 	rfs4_dbe_hold(op->dbe);
3027 	sp->stateid = get_stateid(rfs4_dbe_getid(sp->dbe));
3028 	sp->stateid.bits.type = OPENID;
3029 	sp->owner = op;
3030 	sp->finfo = fp;
3031 
3032 	/* Init lists for remque/insque */
3033 	sp->ownerstateids.next = sp->ownerstateids.prev = &sp->ownerstateids;
3034 	sp->ownerstateids.sp = sp;
3035 	sp->lockownerlist.next = sp->lockownerlist.prev = &sp->lockownerlist;
3036 	sp->lockownerlist.lsp = NULL;
3037 
3038 	/* Insert state on per open owner's list */
3039 	rfs4_dbe_lock(op->dbe);
3040 
3041 	insque(&sp->ownerstateids, op->ownerstateids.prev);
3042 
3043 	rfs4_dbe_unlock(op->dbe);
3044 
3045 	return (TRUE);
3046 }
3047 
3048 static rfs4_state_t *
3049 rfs4_findstate(stateid_t *id, rfs4_dbsearch_type_t find_invalid,
3050 		bool_t lock_fp)
3051 {
3052 	rfs4_state_t *sp;
3053 	bool_t create = FALSE;
3054 
3055 	sp = (rfs4_state_t *)rfs4_dbsearch(rfs4_state_idx, id,
3056 	    &create, NULL, find_invalid);
3057 	if (lock_fp == TRUE && sp != NULL)
3058 		rw_enter(&sp->finfo->file_rwlock, RW_READER);
3059 
3060 	return (sp);
3061 }
3062 
3063 void
3064 rfs4_state_close(rfs4_state_t *sp, bool_t lock_held,
3065 			bool_t close_of_client, cred_t *cr)
3066 {
3067 	/* Remove the associated lo_state owners */
3068 	if (!lock_held)
3069 		rfs4_dbe_lock(sp->dbe);
3070 
3071 	/*
3072 	 * If refcnt == 0, the dbe is about to be destroyed.
3073 	 * lock state will be released by the reaper thread.
3074 	 */
3075 
3076 	if (rfs4_dbe_refcnt(sp->dbe) > 0) {
3077 		if (sp->closed == FALSE) {
3078 			sp->closed = TRUE;
3079 
3080 			rfs4_release_share_lock_state(sp, cr, close_of_client);
3081 		}
3082 	}
3083 
3084 	if (!lock_held)
3085 		rfs4_dbe_unlock(sp->dbe);
3086 }
3087 
3088 /*
3089  * Remove all state associated with the given client.
3090  */
3091 void
3092 rfs4_client_state_remove(rfs4_client_t *cp)
3093 {
3094 	rfs4_openowner_t *oop;
3095 
3096 	rfs4_dbe_lock(cp->dbe);
3097 
3098 	for (oop = cp->openownerlist.next->oop;  oop != NULL;
3099 	    oop = oop->openownerlist.next->oop) {
3100 		rfs4_free_opens(oop, TRUE, TRUE);
3101 	}
3102 
3103 	rfs4_dbe_unlock(cp->dbe);
3104 }
3105 
3106 void
3107 rfs4_client_close(rfs4_client_t *cp)
3108 {
3109 	/* Mark client as going away. */
3110 	rfs4_dbe_lock(cp->dbe);
3111 	rfs4_dbe_invalidate(cp->dbe);
3112 	rfs4_dbe_unlock(cp->dbe);
3113 
3114 	rfs4_client_state_remove(cp);
3115 
3116 	/* Release the client */
3117 	rfs4_client_rele(cp);
3118 }
3119 
3120 nfsstat4
3121 rfs4_check_clientid(clientid4 *cp, int setclid_confirm)
3122 {
3123 	cid *cidp = (cid *) cp;
3124 
3125 	/*
3126 	 * If we are booted as a cluster node, check the embedded nodeid.
3127 	 * If it indicates that this clientid was generated on another node,
3128 	 * inform the client accordingly.
3129 	 */
3130 	if (cluster_bootflags & CLUSTER_BOOTED && foreign_clientid(cidp))
3131 		return (NFS4ERR_STALE_CLIENTID);
3132 
3133 	/*
3134 	 * If the server start time matches the time provided
3135 	 * by the client (via the clientid) and this is NOT a
3136 	 * setclientid_confirm then return EXPIRED.
3137 	 */
3138 	if (!setclid_confirm && cidp->impl_id.start_time == rfs4_start_time)
3139 		return (NFS4ERR_EXPIRED);
3140 
3141 	return (NFS4ERR_STALE_CLIENTID);
3142 }
3143 
3144 /*
3145  * This is used when a stateid has not been found amongst the
3146  * current server's state.  Check the stateid to see if it
3147  * was from this server instantiation or not.
3148  */
3149 static nfsstat4
3150 what_stateid_error(stateid_t *id, stateid_type_t type)
3151 {
3152 	/* If we are booted as a cluster node, was stateid locally generated? */
3153 	if ((cluster_bootflags & CLUSTER_BOOTED) && foreign_stateid(id))
3154 		return (NFS4ERR_STALE_STATEID);
3155 
3156 	/* If types don't match then no use checking further */
3157 	if (type != id->bits.type)
3158 		return (NFS4ERR_BAD_STATEID);
3159 
3160 	/* From a previous server instantiation, return STALE */
3161 	if (id->bits.boottime < rfs4_start_time)
3162 		return (NFS4ERR_STALE_STATEID);
3163 
3164 	/*
3165 	 * From this server but the state is most likely beyond lease
3166 	 * timeout: return NFS4ERR_EXPIRED.  However, there is the
3167 	 * case of a delegation stateid.  For delegations, there is a
3168 	 * case where the state can be removed without the client's
3169 	 * knowledge/consent: revocation.  In the case of delegation
3170 	 * revocation, the delegation state will be removed and will
3171 	 * not be found.  If the client does something like a
3172 	 * DELEGRETURN or even a READ/WRITE with a delegatoin stateid
3173 	 * that has been revoked, the server should return BAD_STATEID
3174 	 * instead of the more common EXPIRED error.
3175 	 */
3176 	if (id->bits.boottime == rfs4_start_time) {
3177 		if (type == DELEGID)
3178 			return (NFS4ERR_BAD_STATEID);
3179 		else
3180 			return (NFS4ERR_EXPIRED);
3181 	}
3182 
3183 	return (NFS4ERR_BAD_STATEID);
3184 }
3185 
3186 /*
3187  * Used later on to find the various state structs.  When called from
3188  * rfs4_check_stateid()->rfs4_get_all_state(), no file struct lock is
3189  * taken (it is not needed) and helps on the read/write path with
3190  * respect to performance.
3191  */
3192 static nfsstat4
3193 rfs4_get_state_lockit(stateid4 *stateid, rfs4_state_t **spp,
3194 		rfs4_dbsearch_type_t find_invalid, bool_t lock_fp)
3195 {
3196 	stateid_t *id = (stateid_t *)stateid;
3197 	rfs4_state_t *sp;
3198 
3199 	*spp = NULL;
3200 
3201 	/* If we are booted as a cluster node, was stateid locally generated? */
3202 	if ((cluster_bootflags & CLUSTER_BOOTED) && foreign_stateid(id))
3203 		return (NFS4ERR_STALE_STATEID);
3204 
3205 	sp = rfs4_findstate(id, find_invalid, lock_fp);
3206 	if (sp == NULL) {
3207 		return (what_stateid_error(id, OPENID));
3208 	}
3209 
3210 	if (rfs4_lease_expired(sp->owner->client)) {
3211 		if (lock_fp == TRUE)
3212 			rfs4_state_rele(sp);
3213 		else
3214 			rfs4_state_rele_nounlock(sp);
3215 		return (NFS4ERR_EXPIRED);
3216 	}
3217 
3218 	*spp = sp;
3219 
3220 	return (NFS4_OK);
3221 }
3222 
3223 nfsstat4
3224 rfs4_get_state(stateid4 *stateid, rfs4_state_t **spp,
3225 		rfs4_dbsearch_type_t find_invalid)
3226 {
3227 	return (rfs4_get_state_lockit(stateid, spp, find_invalid, TRUE));
3228 }
3229 
3230 int
3231 rfs4_check_stateid_seqid(rfs4_state_t *sp, stateid4 *stateid)
3232 {
3233 	stateid_t *id = (stateid_t *)stateid;
3234 
3235 	if (rfs4_lease_expired(sp->owner->client))
3236 		return (NFS4_CHECK_STATEID_EXPIRED);
3237 
3238 	/* Stateid is some time in the future - that's bad */
3239 	if (sp->stateid.bits.chgseq < id->bits.chgseq)
3240 		return (NFS4_CHECK_STATEID_BAD);
3241 
3242 	if (sp->stateid.bits.chgseq == id->bits.chgseq + 1)
3243 		return (NFS4_CHECK_STATEID_REPLAY);
3244 
3245 	/* Stateid is some time in the past - that's old */
3246 	if (sp->stateid.bits.chgseq > id->bits.chgseq)
3247 		return (NFS4_CHECK_STATEID_OLD);
3248 
3249 	/* Caller needs to know about confirmation before closure */
3250 	if (sp->owner->need_confirm)
3251 		return (NFS4_CHECK_STATEID_UNCONFIRMED);
3252 
3253 	if (sp->closed == TRUE)
3254 		return (NFS4_CHECK_STATEID_CLOSED);
3255 
3256 	return (NFS4_CHECK_STATEID_OKAY);
3257 }
3258 
3259 int
3260 rfs4_check_lo_stateid_seqid(rfs4_lo_state_t *lsp, stateid4 *stateid)
3261 {
3262 	stateid_t *id = (stateid_t *)stateid;
3263 
3264 	if (rfs4_lease_expired(lsp->state->owner->client))
3265 		return (NFS4_CHECK_STATEID_EXPIRED);
3266 
3267 	/* Stateid is some time in the future - that's bad */
3268 	if (lsp->lockid.bits.chgseq < id->bits.chgseq)
3269 		return (NFS4_CHECK_STATEID_BAD);
3270 
3271 	if (lsp->lockid.bits.chgseq == id->bits.chgseq + 1)
3272 		return (NFS4_CHECK_STATEID_REPLAY);
3273 
3274 	/* Stateid is some time in the past - that's old */
3275 	if (lsp->lockid.bits.chgseq > id->bits.chgseq)
3276 		return (NFS4_CHECK_STATEID_OLD);
3277 
3278 	return (NFS4_CHECK_STATEID_OKAY);
3279 }
3280 
3281 nfsstat4
3282 rfs4_get_deleg_state(stateid4 *stateid, rfs4_deleg_state_t **dspp)
3283 {
3284 	stateid_t *id = (stateid_t *)stateid;
3285 	rfs4_deleg_state_t *dsp;
3286 
3287 	*dspp = NULL;
3288 
3289 	/* If we are booted as a cluster node, was stateid locally generated? */
3290 	if ((cluster_bootflags & CLUSTER_BOOTED) && foreign_stateid(id))
3291 		return (NFS4ERR_STALE_STATEID);
3292 
3293 	dsp = rfs4_finddelegstate(id);
3294 	if (dsp == NULL) {
3295 		return (what_stateid_error(id, DELEGID));
3296 	}
3297 
3298 	if (rfs4_lease_expired(dsp->client)) {
3299 		rfs4_deleg_state_rele(dsp);
3300 		return (NFS4ERR_EXPIRED);
3301 	}
3302 
3303 	*dspp = dsp;
3304 
3305 	return (NFS4_OK);
3306 }
3307 
3308 nfsstat4
3309 rfs4_get_lo_state(stateid4 *stateid, rfs4_lo_state_t **lspp, bool_t lock_fp)
3310 {
3311 	stateid_t *id = (stateid_t *)stateid;
3312 	rfs4_lo_state_t *lsp;
3313 
3314 	*lspp = NULL;
3315 
3316 	/* If we are booted as a cluster node, was stateid locally generated? */
3317 	if ((cluster_bootflags & CLUSTER_BOOTED) && foreign_stateid(id))
3318 		return (NFS4ERR_STALE_STATEID);
3319 
3320 	lsp = rfs4_findlo_state(id, lock_fp);
3321 	if (lsp == NULL) {
3322 		return (what_stateid_error(id, LOCKID));
3323 	}
3324 
3325 	if (rfs4_lease_expired(lsp->state->owner->client)) {
3326 		rfs4_lo_state_rele(lsp, lock_fp);
3327 		return (NFS4ERR_EXPIRED);
3328 	}
3329 
3330 	*lspp = lsp;
3331 
3332 	return (NFS4_OK);
3333 }
3334 
3335 static nfsstat4
3336 rfs4_get_all_state(stateid4 *sid, rfs4_state_t **spp,
3337 	rfs4_deleg_state_t **dspp, rfs4_lo_state_t **lospp)
3338 {
3339 	rfs4_state_t *sp = NULL;
3340 	rfs4_deleg_state_t *dsp = NULL;
3341 	rfs4_lo_state_t *losp = NULL;
3342 	stateid_t *id;
3343 	nfsstat4 status;
3344 
3345 	*spp = NULL; *dspp = NULL; *lospp = NULL;
3346 
3347 	id = (stateid_t *)sid;
3348 	switch (id->bits.type) {
3349 	case OPENID:
3350 		status = rfs4_get_state_lockit(sid, &sp, FALSE, FALSE);
3351 		break;
3352 	case DELEGID:
3353 		status = rfs4_get_deleg_state(sid, &dsp);
3354 		break;
3355 	case LOCKID:
3356 		status = rfs4_get_lo_state(sid, &losp, FALSE);
3357 		if (status == NFS4_OK) {
3358 			sp = losp->state;
3359 			rfs4_dbe_hold(sp->dbe);
3360 		}
3361 		break;
3362 	default:
3363 		status = NFS4ERR_BAD_STATEID;
3364 	}
3365 
3366 	if (status == NFS4_OK) {
3367 		*spp = sp;
3368 		*dspp = dsp;
3369 		*lospp = losp;
3370 	}
3371 
3372 	return (status);
3373 }
3374 
3375 /*
3376  * Given the I/O mode (FREAD or FWRITE), this checks whether the
3377  * rfs4_state_t struct has access to do this operation and if so
3378  * return NFS4_OK; otherwise the proper NFSv4 error is returned.
3379  */
3380 nfsstat4
3381 rfs4_state_has_access(rfs4_state_t *sp, int mode, vnode_t *vp)
3382 {
3383 	nfsstat4 stat = NFS4_OK;
3384 	rfs4_file_t *fp;
3385 	bool_t create = FALSE;
3386 
3387 	rfs4_dbe_lock(sp->dbe);
3388 	if (mode == FWRITE) {
3389 		if (!(sp->share_access & OPEN4_SHARE_ACCESS_WRITE)) {
3390 			stat = NFS4ERR_OPENMODE;
3391 		}
3392 	} else if (mode == FREAD) {
3393 		if (!(sp->share_access & OPEN4_SHARE_ACCESS_READ)) {
3394 			/*
3395 			 * If we have OPENed the file with DENYing access
3396 			 * to both READ and WRITE then no one else could
3397 			 * have OPENed the file, hence no conflicting READ
3398 			 * deny.  This check is merely an optimization.
3399 			 */
3400 			if (sp->share_deny == OPEN4_SHARE_DENY_BOTH)
3401 				goto out;
3402 
3403 			/* Check against file struct's DENY mode */
3404 			fp = rfs4_findfile(vp, NULL, &create);
3405 			if (fp != NULL) {
3406 				int deny_read = 0;
3407 				rfs4_dbe_lock(fp->dbe);
3408 				/*
3409 				 * Check if any other open owner has the file
3410 				 * OPENed with deny READ.
3411 				 */
3412 				if (sp->share_deny & OPEN4_SHARE_DENY_READ)
3413 					deny_read = 1;
3414 				ASSERT(fp->deny_read - deny_read >= 0);
3415 				if (fp->deny_read - deny_read > 0)
3416 					stat = NFS4ERR_OPENMODE;
3417 				rfs4_dbe_unlock(fp->dbe);
3418 				rfs4_file_rele(fp);
3419 			}
3420 		}
3421 	} else {
3422 		/* Illegal I/O mode */
3423 		stat = NFS4ERR_INVAL;
3424 	}
3425 out:
3426 	rfs4_dbe_unlock(sp->dbe);
3427 	return (stat);
3428 }
3429 
3430 /*
3431  * Given the I/O mode (FREAD or FWRITE), the vnode, the stateid and whether
3432  * the file is being truncated, return NFS4_OK if allowed or appropriate
3433  * V4 error if not. Note NFS4ERR_DELAY will be returned and a recall on
3434  * the associated file will be done if the I/O is not consistent with any
3435  * delegation in effect on the file. Should be holding VOP_RWLOCK, either
3436  * as reader or writer as appropriate. rfs4_op_open will acquire the
3437  * VOP_RWLOCK as writer when setting up delegation. If the stateid is bad
3438  * this routine will return NFS4ERR_BAD_STATEID. In addition, through the
3439  * deleg parameter, we will return whether a write delegation is held by
3440  * the client associated with this stateid.
3441  * If the server instance associated with the relevant client is in its
3442  * grace period, return NFS4ERR_GRACE.
3443  */
3444 
3445 nfsstat4
3446 rfs4_check_stateid(int mode, vnode_t *vp,
3447 		stateid4 *stateid, bool_t trunc, bool_t *deleg,
3448 		bool_t do_access, caller_context_t *ct)
3449 {
3450 	rfs4_file_t *fp;
3451 	bool_t create = FALSE;
3452 	rfs4_state_t *sp;
3453 	rfs4_deleg_state_t *dsp;
3454 	rfs4_lo_state_t *lsp;
3455 	stateid_t *id = (stateid_t *)stateid;
3456 	nfsstat4 stat = NFS4_OK;
3457 
3458 	if (ct != NULL) {
3459 		ct->cc_sysid = 0;
3460 		ct->cc_pid = 0;
3461 		ct->cc_caller_id = nfs4_srv_caller_id;
3462 		ct->cc_flags = CC_DONTBLOCK;
3463 	}
3464 
3465 	if (ISSPECIAL(stateid)) {
3466 		fp = rfs4_findfile(vp, NULL, &create);
3467 		if (fp == NULL)
3468 			return (NFS4_OK);
3469 		if (fp->dinfo->dtype == OPEN_DELEGATE_NONE) {
3470 			rfs4_file_rele(fp);
3471 			return (NFS4_OK);
3472 		}
3473 		if (mode == FWRITE ||
3474 		    fp->dinfo->dtype == OPEN_DELEGATE_WRITE) {
3475 			rfs4_recall_deleg(fp, trunc, NULL);
3476 			rfs4_file_rele(fp);
3477 			return (NFS4ERR_DELAY);
3478 		}
3479 		rfs4_file_rele(fp);
3480 		return (NFS4_OK);
3481 	} else {
3482 		stat = rfs4_get_all_state(stateid, &sp, &dsp, &lsp);
3483 		if (stat != NFS4_OK)
3484 			return (stat);
3485 		if (lsp != NULL) {
3486 			/* Is associated server instance in its grace period? */
3487 			if (rfs4_clnt_in_grace(lsp->locker->client)) {
3488 				rfs4_lo_state_rele(lsp, FALSE);
3489 				if (sp != NULL)
3490 					rfs4_state_rele_nounlock(sp);
3491 				return (NFS4ERR_GRACE);
3492 			}
3493 			if (id->bits.type == LOCKID) {
3494 				/* Seqid in the future? - that's bad */
3495 				if (lsp->lockid.bits.chgseq <
3496 				    id->bits.chgseq) {
3497 					rfs4_lo_state_rele(lsp, FALSE);
3498 					if (sp != NULL)
3499 						rfs4_state_rele_nounlock(sp);
3500 					return (NFS4ERR_BAD_STATEID);
3501 				}
3502 				/* Seqid in the past? - that's old */
3503 				if (lsp->lockid.bits.chgseq >
3504 				    id->bits.chgseq) {
3505 					rfs4_lo_state_rele(lsp, FALSE);
3506 					if (sp != NULL)
3507 						rfs4_state_rele_nounlock(sp);
3508 					return (NFS4ERR_OLD_STATEID);
3509 				}
3510 				/* Ensure specified filehandle matches */
3511 				if (lsp->state->finfo->vp != vp) {
3512 					rfs4_lo_state_rele(lsp, FALSE);
3513 					if (sp != NULL)
3514 						rfs4_state_rele_nounlock(sp);
3515 					return (NFS4ERR_BAD_STATEID);
3516 				}
3517 			}
3518 			if (ct != NULL) {
3519 				ct->cc_sysid = lsp->locker->client->sysidt;
3520 				ct->cc_pid = lsp->locker->pid;
3521 			}
3522 			rfs4_lo_state_rele(lsp, FALSE);
3523 		}
3524 
3525 		/* Stateid provided was an "open" stateid */
3526 		if (sp != NULL) {
3527 			/* Is associated server instance in its grace period? */
3528 			if (rfs4_clnt_in_grace(sp->owner->client)) {
3529 				rfs4_state_rele_nounlock(sp);
3530 				return (NFS4ERR_GRACE);
3531 			}
3532 			if (id->bits.type == OPENID) {
3533 				/* Seqid in the future? - that's bad */
3534 				if (sp->stateid.bits.chgseq <
3535 				    id->bits.chgseq) {
3536 					rfs4_state_rele_nounlock(sp);
3537 					return (NFS4ERR_BAD_STATEID);
3538 				}
3539 				/* Seqid in the past - that's old */
3540 				if (sp->stateid.bits.chgseq >
3541 				    id->bits.chgseq) {
3542 					rfs4_state_rele_nounlock(sp);
3543 					return (NFS4ERR_OLD_STATEID);
3544 				}
3545 			}
3546 			/* Ensure specified filehandle matches */
3547 			if (sp->finfo->vp != vp) {
3548 				rfs4_state_rele_nounlock(sp);
3549 				return (NFS4ERR_BAD_STATEID);
3550 			}
3551 
3552 			if (sp->owner->need_confirm) {
3553 				rfs4_state_rele_nounlock(sp);
3554 				return (NFS4ERR_BAD_STATEID);
3555 			}
3556 
3557 			if (sp->closed == TRUE) {
3558 				rfs4_state_rele_nounlock(sp);
3559 				return (NFS4ERR_OLD_STATEID);
3560 			}
3561 
3562 			if (do_access)
3563 				stat = rfs4_state_has_access(sp, mode, vp);
3564 			else
3565 				stat = NFS4_OK;
3566 
3567 			/*
3568 			 * Return whether this state has write
3569 			 * delegation if desired
3570 			 */
3571 			if (deleg &&
3572 			    (sp->finfo->dinfo->dtype == OPEN_DELEGATE_WRITE))
3573 				*deleg = TRUE;
3574 
3575 			/*
3576 			 * We got a valid stateid, so we update the
3577 			 * lease on the client. Ideally we would like
3578 			 * to do this after the calling op succeeds,
3579 			 * but for now this will be good
3580 			 * enough. Callers of this routine are
3581 			 * currently insulated from the state stuff.
3582 			 */
3583 			rfs4_update_lease(sp->owner->client);
3584 
3585 			/*
3586 			 * If a delegation is present on this file and
3587 			 * this is a WRITE, then update the lastwrite
3588 			 * time to indicate that activity is present.
3589 			 */
3590 			if (sp->finfo->dinfo->dtype == OPEN_DELEGATE_WRITE &&
3591 			    mode == FWRITE) {
3592 				sp->finfo->dinfo->time_lastwrite =
3593 				    gethrestime_sec();
3594 			}
3595 
3596 			rfs4_state_rele_nounlock(sp);
3597 
3598 			return (stat);
3599 		}
3600 
3601 		if (dsp != NULL) {
3602 			/* Is associated server instance in its grace period? */
3603 			if (rfs4_clnt_in_grace(dsp->client)) {
3604 				rfs4_deleg_state_rele(dsp);
3605 				return (NFS4ERR_GRACE);
3606 			}
3607 			if (dsp->delegid.bits.chgseq !=	id->bits.chgseq) {
3608 				rfs4_deleg_state_rele(dsp);
3609 				return (NFS4ERR_BAD_STATEID);
3610 			}
3611 
3612 			/* Ensure specified filehandle matches */
3613 			if (dsp->finfo->vp != vp) {
3614 				rfs4_deleg_state_rele(dsp);
3615 				return (NFS4ERR_BAD_STATEID);
3616 			}
3617 			/*
3618 			 * Return whether this state has write
3619 			 * delegation if desired
3620 			 */
3621 			if (deleg &&
3622 			    (dsp->finfo->dinfo->dtype == OPEN_DELEGATE_WRITE))
3623 				*deleg = TRUE;
3624 
3625 			rfs4_update_lease(dsp->client);
3626 
3627 			/*
3628 			 * If a delegation is present on this file and
3629 			 * this is a WRITE, then update the lastwrite
3630 			 * time to indicate that activity is present.
3631 			 */
3632 			if (dsp->finfo->dinfo->dtype == OPEN_DELEGATE_WRITE &&
3633 			    mode == FWRITE) {
3634 				dsp->finfo->dinfo->time_lastwrite =
3635 				    gethrestime_sec();
3636 			}
3637 
3638 			/*
3639 			 * XXX - what happens if this is a WRITE and the
3640 			 * delegation type of for READ.
3641 			 */
3642 			rfs4_deleg_state_rele(dsp);
3643 
3644 			return (stat);
3645 		}
3646 		/*
3647 		 * If we got this far, something bad happened
3648 		 */
3649 		return (NFS4ERR_BAD_STATEID);
3650 	}
3651 }
3652 
3653 
3654 /*
3655  * This is a special function in that for the file struct provided the
3656  * server wants to remove/close all current state associated with the
3657  * file.  The prime use of this would be with OP_REMOVE to force the
3658  * release of state and particularly of file locks.
3659  *
3660  * There is an assumption that there is no delegations outstanding on
3661  * this file at this point.  The caller should have waited for those
3662  * to be returned or revoked.
3663  */
3664 void
3665 rfs4_close_all_state(rfs4_file_t *fp)
3666 {
3667 	rfs4_state_t *sp;
3668 
3669 	rfs4_dbe_lock(fp->dbe);
3670 
3671 #ifdef DEBUG
3672 	/* only applies when server is handing out delegations */
3673 	if (rfs4_deleg_policy != SRV_NEVER_DELEGATE)
3674 		ASSERT(fp->dinfo->hold_grant > 0);
3675 #endif
3676 
3677 	/* No delegations for this file */
3678 	ASSERT(fp->delegationlist.next == &fp->delegationlist);
3679 
3680 	/* Make sure that it can not be found */
3681 	rfs4_dbe_invalidate(fp->dbe);
3682 
3683 	if (fp->vp == NULL) {
3684 		rfs4_dbe_unlock(fp->dbe);
3685 		return;
3686 	}
3687 	rfs4_dbe_unlock(fp->dbe);
3688 
3689 	/*
3690 	 * Hold as writer to prevent other server threads from
3691 	 * processing requests related to the file while all state is
3692 	 * being removed.
3693 	 */
3694 	rw_enter(&fp->file_rwlock, RW_WRITER);
3695 
3696 	/* Remove ALL state from the file */
3697 	while (sp = rfs4_findstate_by_file(fp)) {
3698 		rfs4_state_close(sp, FALSE, FALSE, CRED());
3699 		rfs4_state_rele_nounlock(sp);
3700 	}
3701 
3702 	/*
3703 	 * This is only safe since there are no further references to
3704 	 * the file.
3705 	 */
3706 	rfs4_dbe_lock(fp->dbe);
3707 	if (fp->vp) {
3708 		vnode_t *vp = fp->vp;
3709 
3710 		mutex_enter(&vp->v_lock);
3711 		(void) vsd_set(vp, nfs4_srv_vkey, NULL);
3712 		mutex_exit(&vp->v_lock);
3713 		VN_RELE(vp);
3714 		fp->vp = NULL;
3715 	}
3716 	rfs4_dbe_unlock(fp->dbe);
3717 
3718 	/* Finally let other references to proceed */
3719 	rw_exit(&fp->file_rwlock);
3720 }
3721 
3722 /*
3723  * This function is used as a target for the rfs4_dbe_walk() call
3724  * below.  The purpose of this function is to see if the
3725  * lockowner_state refers to a file that resides within the exportinfo
3726  * export.  If so, then remove the lock_owner state (file locks and
3727  * share "locks") for this object since the intent is the server is
3728  * unexporting the specified directory.  Be sure to invalidate the
3729  * object after the state has been released
3730  */
3731 static void
3732 rfs4_lo_state_walk_callout(rfs4_entry_t u_entry, void *e)
3733 {
3734 	rfs4_lo_state_t *lsp = (rfs4_lo_state_t *)u_entry;
3735 	struct exportinfo *exi = (struct exportinfo *)e;
3736 	nfs_fh4_fmt_t   fhfmt4, *exi_fhp, *finfo_fhp;
3737 	fhandle_t *efhp;
3738 
3739 	efhp = (fhandle_t *)&exi->exi_fh;
3740 	exi_fhp = (nfs_fh4_fmt_t *)&fhfmt4;
3741 
3742 	FH_TO_FMT4(efhp, exi_fhp);
3743 
3744 	finfo_fhp =
3745 	    (nfs_fh4_fmt_t *)lsp->state->finfo->filehandle.nfs_fh4_val;
3746 
3747 	if (EQFSID(&finfo_fhp->fh4_fsid, &exi_fhp->fh4_fsid) &&
3748 	    bcmp(&finfo_fhp->fh4_xdata, &exi_fhp->fh4_xdata,
3749 	    exi_fhp->fh4_xlen) == 0) {
3750 		rfs4_state_close(lsp->state, FALSE, FALSE, CRED());
3751 		rfs4_dbe_invalidate(lsp->dbe);
3752 		rfs4_dbe_invalidate(lsp->state->dbe);
3753 	}
3754 }
3755 
3756 /*
3757  * This function is used as a target for the rfs4_dbe_walk() call
3758  * below.  The purpose of this function is to see if the state refers
3759  * to a file that resides within the exportinfo export.  If so, then
3760  * remove the open state for this object since the intent is the
3761  * server is unexporting the specified directory.  The main result for
3762  * this type of entry is to invalidate it such it will not be found in
3763  * the future.
3764  */
3765 static void
3766 rfs4_state_walk_callout(rfs4_entry_t u_entry, void *e)
3767 {
3768 	rfs4_state_t *sp = (rfs4_state_t *)u_entry;
3769 	struct exportinfo *exi = (struct exportinfo *)e;
3770 	nfs_fh4_fmt_t   fhfmt4, *exi_fhp, *finfo_fhp;
3771 	fhandle_t *efhp;
3772 
3773 	efhp = (fhandle_t *)&exi->exi_fh;
3774 	exi_fhp = (nfs_fh4_fmt_t *)&fhfmt4;
3775 
3776 	FH_TO_FMT4(efhp, exi_fhp);
3777 
3778 	finfo_fhp =
3779 	    (nfs_fh4_fmt_t *)sp->finfo->filehandle.nfs_fh4_val;
3780 
3781 	if (EQFSID(&finfo_fhp->fh4_fsid, &exi_fhp->fh4_fsid) &&
3782 	    bcmp(&finfo_fhp->fh4_xdata, &exi_fhp->fh4_xdata,
3783 	    exi_fhp->fh4_xlen) == 0) {
3784 		rfs4_state_close(sp, TRUE, FALSE, CRED());
3785 		rfs4_dbe_invalidate(sp->dbe);
3786 	}
3787 }
3788 
3789 /*
3790  * This function is used as a target for the rfs4_dbe_walk() call
3791  * below.  The purpose of this function is to see if the state refers
3792  * to a file that resides within the exportinfo export.  If so, then
3793  * remove the deleg state for this object since the intent is the
3794  * server is unexporting the specified directory.  The main result for
3795  * this type of entry is to invalidate it such it will not be found in
3796  * the future.
3797  */
3798 static void
3799 rfs4_deleg_state_walk_callout(rfs4_entry_t u_entry, void *e)
3800 {
3801 	rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)u_entry;
3802 	struct exportinfo *exi = (struct exportinfo *)e;
3803 	nfs_fh4_fmt_t   fhfmt4, *exi_fhp, *finfo_fhp;
3804 	fhandle_t *efhp;
3805 
3806 	efhp = (fhandle_t *)&exi->exi_fh;
3807 	exi_fhp = (nfs_fh4_fmt_t *)&fhfmt4;
3808 
3809 	FH_TO_FMT4(efhp, exi_fhp);
3810 
3811 	finfo_fhp =
3812 	    (nfs_fh4_fmt_t *)dsp->finfo->filehandle.nfs_fh4_val;
3813 
3814 	if (EQFSID(&finfo_fhp->fh4_fsid, &exi_fhp->fh4_fsid) &&
3815 	    bcmp(&finfo_fhp->fh4_xdata, &exi_fhp->fh4_xdata,
3816 	    exi_fhp->fh4_xlen) == 0) {
3817 		rfs4_dbe_invalidate(dsp->dbe);
3818 	}
3819 }
3820 
3821 /*
3822  * This function is used as a target for the rfs4_dbe_walk() call
3823  * below.  The purpose of this function is to see if the state refers
3824  * to a file that resides within the exportinfo export.  If so, then
3825  * release vnode hold for this object since the intent is the server
3826  * is unexporting the specified directory.  Invalidation will prevent
3827  * this struct from being found in the future.
3828  */
3829 static void
3830 rfs4_file_walk_callout(rfs4_entry_t u_entry, void *e)
3831 {
3832 	rfs4_file_t *fp = (rfs4_file_t *)u_entry;
3833 	struct exportinfo *exi = (struct exportinfo *)e;
3834 	nfs_fh4_fmt_t   fhfmt4, *exi_fhp, *finfo_fhp;
3835 	fhandle_t *efhp;
3836 
3837 	efhp = (fhandle_t *)&exi->exi_fh;
3838 	exi_fhp = (nfs_fh4_fmt_t *)&fhfmt4;
3839 
3840 	FH_TO_FMT4(efhp, exi_fhp);
3841 
3842 	finfo_fhp = (nfs_fh4_fmt_t *)fp->filehandle.nfs_fh4_val;
3843 
3844 	if (EQFSID(&finfo_fhp->fh4_fsid, &exi_fhp->fh4_fsid) &&
3845 	    bcmp(&finfo_fhp->fh4_xdata, &exi_fhp->fh4_xdata,
3846 	    exi_fhp->fh4_xlen) == 0) {
3847 		if (fp->vp) {
3848 			vnode_t *vp = fp->vp;
3849 
3850 			/*
3851 			 * don't leak monitors and remove the reference
3852 			 * put on the vnode when the delegation was granted.
3853 			 */
3854 			if (fp->dinfo->dtype == OPEN_DELEGATE_READ) {
3855 				(void) fem_uninstall(vp, deleg_rdops,
3856 				    (void *)fp);
3857 				vn_open_downgrade(vp, FREAD);
3858 			} else if (fp->dinfo->dtype == OPEN_DELEGATE_WRITE) {
3859 				(void) fem_uninstall(vp, deleg_wrops,
3860 				    (void *)fp);
3861 				vn_open_downgrade(vp, FREAD|FWRITE);
3862 			}
3863 			mutex_enter(&vp->v_lock);
3864 			(void) vsd_set(vp, nfs4_srv_vkey, NULL);
3865 			mutex_exit(&vp->v_lock);
3866 			VN_RELE(vp);
3867 			fp->vp = NULL;
3868 		}
3869 		rfs4_dbe_invalidate(fp->dbe);
3870 	}
3871 }
3872 
3873 /*
3874  * Given a directory that is being unexported, cleanup/release all
3875  * state in the server that refers to objects residing underneath this
3876  * particular export.  The ordering of the release is important.
3877  * Lock_owner, then state and then file.
3878  */
3879 void
3880 rfs4_clean_state_exi(struct exportinfo *exi)
3881 {
3882 	mutex_enter(&rfs4_state_lock);
3883 
3884 	if (rfs4_server_state == NULL) {
3885 		mutex_exit(&rfs4_state_lock);
3886 		return;
3887 	}
3888 
3889 	rfs4_dbe_walk(rfs4_lo_state_tab, rfs4_lo_state_walk_callout, exi);
3890 	rfs4_dbe_walk(rfs4_state_tab, rfs4_state_walk_callout, exi);
3891 	rfs4_dbe_walk(rfs4_deleg_state_tab, rfs4_deleg_state_walk_callout, exi);
3892 	rfs4_dbe_walk(rfs4_file_tab, rfs4_file_walk_callout, exi);
3893 
3894 	mutex_exit(&rfs4_state_lock);
3895 }
3896