xref: /titanic_44/usr/src/uts/common/fs/nfs/nfs4_state.c (revision 9d0d62ad2e60e8f742a2e723d06e88352ee6a1f3)
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->rc_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->rc_nfs_client.id_len) {
868 			if (bcmp(osp->cl_id4.id_val, cp->rc_nfs_client.id_val,
869 			    osp->cl_id4.id_len) == 0) {
870 				cp->rc_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->rc_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->rc_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->rc_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->rc_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->rc_ss_pn != NULL) {
1009 		if (strcmp(cp->rc_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->rc_ss_pn);
1015 			cp->rc_ss_pn = ss_pn;
1016 		}
1017 	} else {
1018 		cp->rc_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->rc_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->rc_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->rc_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->rc_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->rc_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 *cp = (rfs4_client_t *)entry;
1493 	clientid4 *idp = key;
1494 
1495 	return (*idp == cp->rc_clientid);
1496 }
1497 
1498 static void *
1499 clientid_mkkey(rfs4_entry_t entry)
1500 {
1501 	rfs4_client_t *cp = (rfs4_client_t *)entry;
1502 
1503 	return (&cp->rc_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 *cp = (rfs4_client_t *)entry;
1525 	nfs_client_id4 *nfs_client = key;
1526 
1527 	if (cp->rc_nfs_client.id_len != nfs_client->id_len)
1528 		return (FALSE);
1529 
1530 	return (bcmp(cp->rc_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 *cp = (rfs4_client_t *)entry;
1538 
1539 	return (&cp->rc_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->rc_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->rc_forced_expire ||
1557 	    (gethrestime_sec() - cp->rc_last_access
1558 	    > rfs4_lease_time));
1559 
1560 	if (!cp->rc_ss_remove && cp_expired)
1561 		cp->rc_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->rc_ss_pn->leaf;
1572 
1573 	rfs4_dss_remove_leaf(cp->rc_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->rc_cbinfo.cb_lock);
1609 	cv_destroy(cp->rc_cbinfo.cb_cv);
1610 	cv_destroy(cp->rc_cbinfo.cb_cv_nullcaller);
1611 	list_destroy(&cp->rc_openownerlist);
1612 
1613 	/* free callback info */
1614 	rfs4_cbinfo_free(&cp->rc_cbinfo);
1615 
1616 	if (cp->rc_cp_confirmed)
1617 		rfs4_client_rele(cp->rc_cp_confirmed);
1618 
1619 	if (cp->rc_ss_pn) {
1620 		/* check if the stable storage files need to be removed */
1621 		if (cp->rc_ss_remove)
1622 			rfs4_dss_remove_cpleaf(cp);
1623 		rfs4_ss_pnfree(cp->rc_ss_pn);
1624 	}
1625 
1626 	/* Free the client supplied client id */
1627 	kmem_free(cp->rc_nfs_client.id_val, cp->rc_nfs_client.id_len);
1628 
1629 	if (cp->rc_sysidt != LM_NOSYSID)
1630 		lm_free_sysidt(cp->rc_sysidt);
1631 }
1632 
1633 static bool_t
1634 rfs4_client_create(rfs4_entry_t u_entry, void *arg)
1635 {
1636 	rfs4_client_t *cp = (rfs4_client_t *)u_entry;
1637 	nfs_client_id4 *client = (nfs_client_id4 *)arg;
1638 	cid *cidp;
1639 	scid_confirm_verf *scvp;
1640 
1641 	/* Get a clientid to give to the client */
1642 	cidp = (cid *)&cp->rc_clientid;
1643 	cidp->impl_id.start_time = rfs4_start_time;
1644 	cidp->impl_id.c_id = (uint32_t)rfs4_dbe_getid(cp->rc_dbe);
1645 
1646 	/* If we are booted as a cluster node, embed our nodeid */
1647 	if (cluster_bootflags & CLUSTER_BOOTED)
1648 		embed_nodeid(cidp);
1649 
1650 	/* Allocate and copy client's client id value */
1651 	cp->rc_nfs_client.id_val = kmem_alloc(client->id_len, KM_SLEEP);
1652 	cp->rc_nfs_client.id_len = client->id_len;
1653 	bcopy(client->id_val, cp->rc_nfs_client.id_val, client->id_len);
1654 	cp->rc_nfs_client.verifier = client->verifier;
1655 
1656 	/* Init the value for the SETCLIENTID_CONFIRM verifier */
1657 	scvp = (scid_confirm_verf *)&cp->rc_confirm_verf;
1658 	scvp->cv_impl.c_id = cidp->impl_id.c_id;
1659 	scvp->cv_impl.gen_num = 0;
1660 
1661 	/* An F_UNLKSYS has been done for this client */
1662 	cp->rc_unlksys_completed = FALSE;
1663 
1664 	/* We need the client to ack us */
1665 	cp->rc_need_confirm = TRUE;
1666 	cp->rc_cp_confirmed = NULL;
1667 
1668 	/* TRUE all the time until the callback path actually fails */
1669 	cp->rc_cbinfo.cb_notified_of_cb_path_down = TRUE;
1670 
1671 	/* Initialize the access time to now */
1672 	cp->rc_last_access = gethrestime_sec();
1673 
1674 	cp->rc_cr_set = NULL;
1675 
1676 	cp->rc_sysidt = LM_NOSYSID;
1677 
1678 	list_create(&cp->rc_openownerlist, sizeof (rfs4_openowner_t),
1679 	    offsetof(rfs4_openowner_t, ro_node));
1680 
1681 	/* set up the callback control structure */
1682 	cp->rc_cbinfo.cb_state = CB_UNINIT;
1683 	mutex_init(cp->rc_cbinfo.cb_lock, NULL, MUTEX_DEFAULT, NULL);
1684 	cv_init(cp->rc_cbinfo.cb_cv, NULL, CV_DEFAULT, NULL);
1685 	cv_init(cp->rc_cbinfo.cb_cv_nullcaller, NULL, CV_DEFAULT, NULL);
1686 
1687 	/*
1688 	 * Associate the client_t with the current server instance.
1689 	 * The hold is solely to satisfy the calling requirement of
1690 	 * rfs4_servinst_assign(). In this case it's not strictly necessary.
1691 	 */
1692 	rfs4_dbe_hold(cp->rc_dbe);
1693 	rfs4_servinst_assign(cp, rfs4_cur_servinst);
1694 	rfs4_dbe_rele(cp->rc_dbe);
1695 
1696 	return (TRUE);
1697 }
1698 
1699 /*
1700  * Caller wants to generate/update the setclientid_confirm verifier
1701  * associated with a client.  This is done during the SETCLIENTID
1702  * processing.
1703  */
1704 void
1705 rfs4_client_scv_next(rfs4_client_t *cp)
1706 {
1707 	scid_confirm_verf *scvp;
1708 
1709 	/* Init the value for the SETCLIENTID_CONFIRM verifier */
1710 	scvp = (scid_confirm_verf *)&cp->rc_confirm_verf;
1711 	scvp->cv_impl.gen_num++;
1712 }
1713 
1714 void
1715 rfs4_client_rele(rfs4_client_t *cp)
1716 {
1717 	rfs4_dbe_rele(cp->rc_dbe);
1718 }
1719 
1720 rfs4_client_t *
1721 rfs4_findclient(nfs_client_id4 *client, bool_t *create,	rfs4_client_t *oldcp)
1722 {
1723 	rfs4_client_t *cp;
1724 
1725 
1726 	if (oldcp) {
1727 		rw_enter(&rfs4_findclient_lock, RW_WRITER);
1728 		rfs4_dbe_hide(oldcp->rc_dbe);
1729 	} else {
1730 		rw_enter(&rfs4_findclient_lock, RW_READER);
1731 	}
1732 
1733 	cp = (rfs4_client_t *)rfs4_dbsearch(rfs4_nfsclnt_idx, client,
1734 	    create, (void *)client, RFS4_DBS_VALID);
1735 
1736 	if (oldcp)
1737 		rfs4_dbe_unhide(oldcp->rc_dbe);
1738 
1739 	rw_exit(&rfs4_findclient_lock);
1740 
1741 	return (cp);
1742 }
1743 
1744 rfs4_client_t *
1745 rfs4_findclient_by_id(clientid4 clientid, bool_t find_unconfirmed)
1746 {
1747 	rfs4_client_t *cp;
1748 	bool_t create = FALSE;
1749 	cid *cidp = (cid *)&clientid;
1750 
1751 	/* If we're a cluster and the nodeid isn't right, short-circuit */
1752 	if (cluster_bootflags & CLUSTER_BOOTED && foreign_clientid(cidp))
1753 		return (NULL);
1754 
1755 	rw_enter(&rfs4_findclient_lock, RW_READER);
1756 
1757 	cp = (rfs4_client_t *)rfs4_dbsearch(rfs4_clientid_idx, &clientid,
1758 	    &create, NULL, RFS4_DBS_VALID);
1759 
1760 	rw_exit(&rfs4_findclient_lock);
1761 
1762 	if (cp && cp->rc_need_confirm && find_unconfirmed == FALSE) {
1763 		rfs4_client_rele(cp);
1764 		return (NULL);
1765 	} else {
1766 		return (cp);
1767 	}
1768 }
1769 
1770 bool_t
1771 rfs4_lease_expired(rfs4_client_t *cp)
1772 {
1773 	bool_t rc;
1774 
1775 	rfs4_dbe_lock(cp->rc_dbe);
1776 
1777 	/*
1778 	 * If the admin has executed clear_locks for this
1779 	 * client id, force expire will be set, so no need
1780 	 * to calculate anything because it's "outa here".
1781 	 */
1782 	if (cp->rc_forced_expire) {
1783 		rc = TRUE;
1784 	} else {
1785 		rc = (gethrestime_sec() - cp->rc_last_access > rfs4_lease_time);
1786 	}
1787 
1788 	/*
1789 	 * If the lease has expired we will also want
1790 	 * to remove any stable storage state data. So
1791 	 * mark the client id accordingly.
1792 	 */
1793 	if (!cp->rc_ss_remove)
1794 		cp->rc_ss_remove = (rc == TRUE);
1795 
1796 	rfs4_dbe_unlock(cp->rc_dbe);
1797 
1798 	return (rc);
1799 }
1800 
1801 void
1802 rfs4_update_lease(rfs4_client_t *cp)
1803 {
1804 	rfs4_dbe_lock(cp->rc_dbe);
1805 	if (!cp->rc_forced_expire)
1806 		cp->rc_last_access = gethrestime_sec();
1807 	rfs4_dbe_unlock(cp->rc_dbe);
1808 }
1809 
1810 
1811 static bool_t
1812 EQOPENOWNER(open_owner4 *a, open_owner4 *b)
1813 {
1814 	bool_t rc;
1815 
1816 	if (a->clientid != b->clientid)
1817 		return (FALSE);
1818 
1819 	if (a->owner_len != b->owner_len)
1820 		return (FALSE);
1821 
1822 	rc = (bcmp(a->owner_val, b->owner_val, a->owner_len) == 0);
1823 
1824 	return (rc);
1825 }
1826 
1827 static uint_t
1828 openowner_hash(void *key)
1829 {
1830 	int i;
1831 	open_owner4 *openowner = key;
1832 	uint_t hash = 0;
1833 
1834 	for (i = 0; i < openowner->owner_len; i++) {
1835 		hash <<= 4;
1836 		hash += (uint_t)openowner->owner_val[i];
1837 	}
1838 	hash += (uint_t)openowner->clientid;
1839 	hash |= (openowner->clientid >> 32);
1840 
1841 	return (hash);
1842 }
1843 
1844 static bool_t
1845 openowner_compare(rfs4_entry_t u_entry, void *key)
1846 {
1847 	rfs4_openowner_t *oo = (rfs4_openowner_t *)u_entry;
1848 	open_owner4 *arg = key;
1849 
1850 	return (EQOPENOWNER(&oo->ro_owner, arg));
1851 }
1852 
1853 void *
1854 openowner_mkkey(rfs4_entry_t u_entry)
1855 {
1856 	rfs4_openowner_t *oo = (rfs4_openowner_t *)u_entry;
1857 
1858 	return (&oo->ro_owner);
1859 }
1860 
1861 static bool_t
1862 rfs4_openowner_expiry(rfs4_entry_t u_entry)
1863 {
1864 	rfs4_openowner_t *oo = (rfs4_openowner_t *)u_entry;
1865 
1866 	if (rfs4_dbe_is_invalid(oo->ro_dbe))
1867 		return (TRUE);
1868 	return ((gethrestime_sec() - oo->ro_client->rc_last_access
1869 	    > rfs4_lease_time));
1870 }
1871 
1872 static void
1873 rfs4_openowner_destroy(rfs4_entry_t u_entry)
1874 {
1875 	rfs4_openowner_t *oo = (rfs4_openowner_t *)u_entry;
1876 
1877 	/* Remove open owner from client's lists of open owners */
1878 	rfs4_dbe_lock(oo->ro_client->rc_dbe);
1879 	list_remove(&oo->ro_client->rc_openownerlist, oo);
1880 	rfs4_dbe_unlock(oo->ro_client->rc_dbe);
1881 
1882 	/* One less reference to the client */
1883 	rfs4_client_rele(oo->ro_client);
1884 	oo->ro_client = NULL;
1885 
1886 	/* Free the last reply for this lock owner */
1887 	rfs4_free_reply(&oo->ro_reply);
1888 
1889 	if (oo->ro_reply_fh.nfs_fh4_val) {
1890 		kmem_free(oo->ro_reply_fh.nfs_fh4_val,
1891 		    oo->ro_reply_fh.nfs_fh4_len);
1892 		oo->ro_reply_fh.nfs_fh4_val = NULL;
1893 		oo->ro_reply_fh.nfs_fh4_len = 0;
1894 	}
1895 
1896 	rfs4_sw_destroy(&oo->ro_sw);
1897 	list_destroy(&oo->ro_statelist);
1898 
1899 	/* Free the lock owner id */
1900 	kmem_free(oo->ro_owner.owner_val, oo->ro_owner.owner_len);
1901 }
1902 
1903 void
1904 rfs4_openowner_rele(rfs4_openowner_t *oo)
1905 {
1906 	rfs4_dbe_rele(oo->ro_dbe);
1907 }
1908 
1909 static bool_t
1910 rfs4_openowner_create(rfs4_entry_t u_entry, void *arg)
1911 {
1912 	rfs4_openowner_t *oo = (rfs4_openowner_t *)u_entry;
1913 	rfs4_openowner_t *argp = (rfs4_openowner_t *)arg;
1914 	open_owner4 *openowner = &argp->ro_owner;
1915 	seqid4 seqid = argp->ro_open_seqid;
1916 	rfs4_client_t *cp;
1917 	bool_t create = FALSE;
1918 
1919 	rw_enter(&rfs4_findclient_lock, RW_READER);
1920 
1921 	cp = (rfs4_client_t *)rfs4_dbsearch(rfs4_clientid_idx,
1922 	    &openowner->clientid,
1923 	    &create, NULL, RFS4_DBS_VALID);
1924 
1925 	rw_exit(&rfs4_findclient_lock);
1926 
1927 	if (cp == NULL)
1928 		return (FALSE);
1929 
1930 	oo->ro_reply_fh.nfs_fh4_len = 0;
1931 	oo->ro_reply_fh.nfs_fh4_val = NULL;
1932 
1933 	oo->ro_owner.clientid = openowner->clientid;
1934 	oo->ro_owner.owner_val =
1935 	    kmem_alloc(openowner->owner_len, KM_SLEEP);
1936 
1937 	bcopy(openowner->owner_val,
1938 	    oo->ro_owner.owner_val, openowner->owner_len);
1939 
1940 	oo->ro_owner.owner_len = openowner->owner_len;
1941 
1942 	oo->ro_need_confirm = TRUE;
1943 
1944 	rfs4_sw_init(&oo->ro_sw);
1945 
1946 	oo->ro_open_seqid = seqid;
1947 	bzero(&oo->ro_reply, sizeof (nfs_resop4));
1948 	oo->ro_client = cp;
1949 	oo->ro_cr_set = NULL;
1950 
1951 	list_create(&oo->ro_statelist, sizeof (rfs4_state_t),
1952 	    offsetof(rfs4_state_t, rs_node));
1953 
1954 	/* Insert openowner into client's open owner list */
1955 	rfs4_dbe_lock(cp->rc_dbe);
1956 	list_insert_tail(&cp->rc_openownerlist, oo);
1957 	rfs4_dbe_unlock(cp->rc_dbe);
1958 
1959 	return (TRUE);
1960 }
1961 
1962 rfs4_openowner_t *
1963 rfs4_findopenowner(open_owner4 *openowner, bool_t *create, seqid4 seqid)
1964 {
1965 	rfs4_openowner_t *oo;
1966 	rfs4_openowner_t arg;
1967 
1968 	arg.ro_owner = *openowner;
1969 	arg.ro_open_seqid = seqid;
1970 	oo = (rfs4_openowner_t *)rfs4_dbsearch(rfs4_openowner_idx, openowner,
1971 	    create, &arg, RFS4_DBS_VALID);
1972 
1973 	return (oo);
1974 }
1975 
1976 void
1977 rfs4_update_open_sequence(rfs4_openowner_t *oo)
1978 {
1979 
1980 	rfs4_dbe_lock(oo->ro_dbe);
1981 
1982 	oo->ro_open_seqid++;
1983 
1984 	rfs4_dbe_unlock(oo->ro_dbe);
1985 }
1986 
1987 void
1988 rfs4_update_open_resp(rfs4_openowner_t *oo, nfs_resop4 *resp, nfs_fh4 *fh)
1989 {
1990 
1991 	rfs4_dbe_lock(oo->ro_dbe);
1992 
1993 	rfs4_free_reply(&oo->ro_reply);
1994 
1995 	rfs4_copy_reply(&oo->ro_reply, resp);
1996 
1997 	/* Save the filehandle if provided and free if not used */
1998 	if (resp->nfs_resop4_u.opopen.status == NFS4_OK &&
1999 	    fh && fh->nfs_fh4_len) {
2000 		if (oo->ro_reply_fh.nfs_fh4_val == NULL)
2001 			oo->ro_reply_fh.nfs_fh4_val =
2002 			    kmem_alloc(fh->nfs_fh4_len, KM_SLEEP);
2003 		nfs_fh4_copy(fh, &oo->ro_reply_fh);
2004 	} else {
2005 		if (oo->ro_reply_fh.nfs_fh4_val) {
2006 			kmem_free(oo->ro_reply_fh.nfs_fh4_val,
2007 			    oo->ro_reply_fh.nfs_fh4_len);
2008 			oo->ro_reply_fh.nfs_fh4_val = NULL;
2009 			oo->ro_reply_fh.nfs_fh4_len = 0;
2010 		}
2011 	}
2012 
2013 	rfs4_dbe_unlock(oo->ro_dbe);
2014 }
2015 
2016 static bool_t
2017 lockowner_compare(rfs4_entry_t u_entry, void *key)
2018 {
2019 	rfs4_lockowner_t *lo = (rfs4_lockowner_t *)u_entry;
2020 	lock_owner4 *b = (lock_owner4 *)key;
2021 
2022 	if (lo->rl_owner.clientid != b->clientid)
2023 		return (FALSE);
2024 
2025 	if (lo->rl_owner.owner_len != b->owner_len)
2026 		return (FALSE);
2027 
2028 	return (bcmp(lo->rl_owner.owner_val, b->owner_val,
2029 	    lo->rl_owner.owner_len) == 0);
2030 }
2031 
2032 void *
2033 lockowner_mkkey(rfs4_entry_t u_entry)
2034 {
2035 	rfs4_lockowner_t *lo = (rfs4_lockowner_t *)u_entry;
2036 
2037 	return (&lo->rl_owner);
2038 }
2039 
2040 static uint32_t
2041 lockowner_hash(void *key)
2042 {
2043 	int i;
2044 	lock_owner4 *lockowner = key;
2045 	uint_t hash = 0;
2046 
2047 	for (i = 0; i < lockowner->owner_len; i++) {
2048 		hash <<= 4;
2049 		hash += (uint_t)lockowner->owner_val[i];
2050 	}
2051 	hash += (uint_t)lockowner->clientid;
2052 	hash |= (lockowner->clientid >> 32);
2053 
2054 	return (hash);
2055 }
2056 
2057 static uint32_t
2058 pid_hash(void *key)
2059 {
2060 	return ((uint32_t)(uintptr_t)key);
2061 }
2062 
2063 static void *
2064 pid_mkkey(rfs4_entry_t u_entry)
2065 {
2066 	rfs4_lockowner_t *lo = (rfs4_lockowner_t *)u_entry;
2067 
2068 	return ((void *)(uintptr_t)lo->rl_pid);
2069 }
2070 
2071 static bool_t
2072 pid_compare(rfs4_entry_t u_entry, void *key)
2073 {
2074 	rfs4_lockowner_t *lo = (rfs4_lockowner_t *)u_entry;
2075 
2076 	return (lo->rl_pid == (pid_t)(uintptr_t)key);
2077 }
2078 
2079 static void
2080 rfs4_lockowner_destroy(rfs4_entry_t u_entry)
2081 {
2082 	rfs4_lockowner_t *lo = (rfs4_lockowner_t *)u_entry;
2083 
2084 	/* Free the lock owner id */
2085 	kmem_free(lo->rl_owner.owner_val, lo->rl_owner.owner_len);
2086 	rfs4_client_rele(lo->rl_client);
2087 }
2088 
2089 void
2090 rfs4_lockowner_rele(rfs4_lockowner_t *lo)
2091 {
2092 	rfs4_dbe_rele(lo->rl_dbe);
2093 }
2094 
2095 /* ARGSUSED */
2096 static bool_t
2097 rfs4_lockowner_expiry(rfs4_entry_t u_entry)
2098 {
2099 	/*
2100 	 * Since expiry is called with no other references on
2101 	 * this struct, go ahead and have it removed.
2102 	 */
2103 	return (TRUE);
2104 }
2105 
2106 static bool_t
2107 rfs4_lockowner_create(rfs4_entry_t u_entry, void *arg)
2108 {
2109 	rfs4_lockowner_t *lo = (rfs4_lockowner_t *)u_entry;
2110 	lock_owner4 *lockowner = (lock_owner4 *)arg;
2111 	rfs4_client_t *cp;
2112 	bool_t create = FALSE;
2113 
2114 	rw_enter(&rfs4_findclient_lock, RW_READER);
2115 
2116 	cp = (rfs4_client_t *)rfs4_dbsearch(rfs4_clientid_idx,
2117 	    &lockowner->clientid,
2118 	    &create, NULL, RFS4_DBS_VALID);
2119 
2120 	rw_exit(&rfs4_findclient_lock);
2121 
2122 	if (cp == NULL)
2123 		return (FALSE);
2124 
2125 	/* Reference client */
2126 	lo->rl_client = cp;
2127 	lo->rl_owner.clientid = lockowner->clientid;
2128 	lo->rl_owner.owner_val = kmem_alloc(lockowner->owner_len, KM_SLEEP);
2129 	bcopy(lockowner->owner_val, lo->rl_owner.owner_val,
2130 	    lockowner->owner_len);
2131 	lo->rl_owner.owner_len = lockowner->owner_len;
2132 	lo->rl_pid = rfs4_dbe_getid(lo->rl_dbe);
2133 
2134 	return (TRUE);
2135 }
2136 
2137 rfs4_lockowner_t *
2138 rfs4_findlockowner(lock_owner4 *lockowner, bool_t *create)
2139 {
2140 	rfs4_lockowner_t *lo;
2141 
2142 	lo = (rfs4_lockowner_t *)rfs4_dbsearch(rfs4_lockowner_idx, lockowner,
2143 	    create, lockowner, RFS4_DBS_VALID);
2144 
2145 	return (lo);
2146 }
2147 
2148 rfs4_lockowner_t *
2149 rfs4_findlockowner_by_pid(pid_t pid)
2150 {
2151 	rfs4_lockowner_t *lo;
2152 	bool_t create = FALSE;
2153 
2154 	lo = (rfs4_lockowner_t *)rfs4_dbsearch(rfs4_lockowner_pid_idx,
2155 	    (void *)(uintptr_t)pid, &create, NULL, RFS4_DBS_VALID);
2156 
2157 	return (lo);
2158 }
2159 
2160 
2161 static uint32_t
2162 file_hash(void *key)
2163 {
2164 	return (ADDRHASH(key));
2165 }
2166 
2167 static void *
2168 file_mkkey(rfs4_entry_t u_entry)
2169 {
2170 	rfs4_file_t *fp = (rfs4_file_t *)u_entry;
2171 
2172 	return (fp->rf_vp);
2173 }
2174 
2175 static bool_t
2176 file_compare(rfs4_entry_t u_entry, void *key)
2177 {
2178 	rfs4_file_t *fp = (rfs4_file_t *)u_entry;
2179 
2180 	return (fp->rf_vp == (vnode_t *)key);
2181 }
2182 
2183 static void
2184 rfs4_file_destroy(rfs4_entry_t u_entry)
2185 {
2186 	rfs4_file_t *fp = (rfs4_file_t *)u_entry;
2187 
2188 	list_destroy(&fp->rf_delegstatelist);
2189 
2190 	if (fp->rf_filehandle.nfs_fh4_val)
2191 		kmem_free(fp->rf_filehandle.nfs_fh4_val,
2192 		    fp->rf_filehandle.nfs_fh4_len);
2193 	cv_destroy(fp->rf_dinfo.rd_recall_cv);
2194 	if (fp->rf_vp) {
2195 		vnode_t *vp = fp->rf_vp;
2196 
2197 		mutex_enter(&vp->v_vsd_lock);
2198 		(void) vsd_set(vp, nfs4_srv_vkey, NULL);
2199 		mutex_exit(&vp->v_vsd_lock);
2200 		VN_RELE(vp);
2201 		fp->rf_vp = NULL;
2202 	}
2203 	rw_destroy(&fp->rf_file_rwlock);
2204 }
2205 
2206 /*
2207  * Used to unlock the underlying dbe struct only
2208  */
2209 void
2210 rfs4_file_rele(rfs4_file_t *fp)
2211 {
2212 	rfs4_dbe_rele(fp->rf_dbe);
2213 }
2214 
2215 typedef struct {
2216     vnode_t *vp;
2217     nfs_fh4 *fh;
2218 } rfs4_fcreate_arg;
2219 
2220 static bool_t
2221 rfs4_file_create(rfs4_entry_t u_entry, void *arg)
2222 {
2223 	rfs4_file_t *fp = (rfs4_file_t *)u_entry;
2224 	rfs4_fcreate_arg *ap = (rfs4_fcreate_arg *)arg;
2225 	vnode_t *vp = ap->vp;
2226 	nfs_fh4 *fh = ap->fh;
2227 
2228 	VN_HOLD(vp);
2229 
2230 	fp->rf_filehandle.nfs_fh4_len = 0;
2231 	fp->rf_filehandle.nfs_fh4_val = NULL;
2232 	ASSERT(fh && fh->nfs_fh4_len);
2233 	if (fh && fh->nfs_fh4_len) {
2234 		fp->rf_filehandle.nfs_fh4_val =
2235 		    kmem_alloc(fh->nfs_fh4_len, KM_SLEEP);
2236 		nfs_fh4_copy(fh, &fp->rf_filehandle);
2237 	}
2238 	fp->rf_vp = vp;
2239 
2240 	list_create(&fp->rf_delegstatelist, sizeof (rfs4_deleg_state_t),
2241 	    offsetof(rfs4_deleg_state_t, rds_node));
2242 
2243 	fp->rf_share_deny = fp->rf_share_access = fp->rf_access_read = 0;
2244 	fp->rf_access_write = fp->rf_deny_read = fp->rf_deny_write = 0;
2245 
2246 	mutex_init(fp->rf_dinfo.rd_recall_lock, NULL, MUTEX_DEFAULT, NULL);
2247 	cv_init(fp->rf_dinfo.rd_recall_cv, NULL, CV_DEFAULT, NULL);
2248 
2249 	fp->rf_dinfo.rd_dtype = OPEN_DELEGATE_NONE;
2250 
2251 	rw_init(&fp->rf_file_rwlock, NULL, RW_DEFAULT, NULL);
2252 
2253 	mutex_enter(&vp->v_vsd_lock);
2254 	VERIFY(vsd_set(vp, nfs4_srv_vkey, (void *)fp) == 0);
2255 	mutex_exit(&vp->v_vsd_lock);
2256 
2257 	return (TRUE);
2258 }
2259 
2260 rfs4_file_t *
2261 rfs4_findfile(vnode_t *vp, nfs_fh4 *fh, bool_t *create)
2262 {
2263 	rfs4_file_t *fp;
2264 	rfs4_fcreate_arg arg;
2265 
2266 	arg.vp = vp;
2267 	arg.fh = fh;
2268 
2269 	if (*create == TRUE)
2270 		fp = (rfs4_file_t *)rfs4_dbsearch(rfs4_file_idx, vp, create,
2271 		    &arg, RFS4_DBS_VALID);
2272 	else {
2273 		mutex_enter(&vp->v_vsd_lock);
2274 		fp = (rfs4_file_t *)vsd_get(vp, nfs4_srv_vkey);
2275 		if (fp) {
2276 			rfs4_dbe_lock(fp->rf_dbe);
2277 			if (rfs4_dbe_is_invalid(fp->rf_dbe) ||
2278 			    (rfs4_dbe_refcnt(fp->rf_dbe) == 0)) {
2279 				rfs4_dbe_unlock(fp->rf_dbe);
2280 				fp = NULL;
2281 			} else {
2282 				rfs4_dbe_hold(fp->rf_dbe);
2283 				rfs4_dbe_unlock(fp->rf_dbe);
2284 			}
2285 		}
2286 		mutex_exit(&vp->v_vsd_lock);
2287 	}
2288 	return (fp);
2289 }
2290 
2291 /*
2292  * Find a file in the db and once it is located, take the rw lock.
2293  * Need to check the vnode pointer and if it does not exist (it was
2294  * removed between the db location and check) redo the find.  This
2295  * assumes that a file struct that has a NULL vnode pointer is marked
2296  * at 'invalid' and will not be found in the db the second time
2297  * around.
2298  */
2299 rfs4_file_t *
2300 rfs4_findfile_withlock(vnode_t *vp, nfs_fh4 *fh, bool_t *create)
2301 {
2302 	rfs4_file_t *fp;
2303 	rfs4_fcreate_arg arg;
2304 	bool_t screate = *create;
2305 
2306 	if (screate == FALSE) {
2307 		mutex_enter(&vp->v_vsd_lock);
2308 		fp = (rfs4_file_t *)vsd_get(vp, nfs4_srv_vkey);
2309 		if (fp) {
2310 			rfs4_dbe_lock(fp->rf_dbe);
2311 			if (rfs4_dbe_is_invalid(fp->rf_dbe) ||
2312 			    (rfs4_dbe_refcnt(fp->rf_dbe) == 0)) {
2313 				rfs4_dbe_unlock(fp->rf_dbe);
2314 				mutex_exit(&vp->v_vsd_lock);
2315 				fp = NULL;
2316 			} else {
2317 				rfs4_dbe_hold(fp->rf_dbe);
2318 				rfs4_dbe_unlock(fp->rf_dbe);
2319 				mutex_exit(&vp->v_vsd_lock);
2320 				rw_enter(&fp->rf_file_rwlock, RW_WRITER);
2321 				if (fp->rf_vp == NULL) {
2322 					rw_exit(&fp->rf_file_rwlock);
2323 					rfs4_file_rele(fp);
2324 					fp = NULL;
2325 				}
2326 			}
2327 		} else {
2328 			mutex_exit(&vp->v_vsd_lock);
2329 		}
2330 	} else {
2331 retry:
2332 		arg.vp = vp;
2333 		arg.fh = fh;
2334 
2335 		fp = (rfs4_file_t *)rfs4_dbsearch(rfs4_file_idx, vp, create,
2336 		    &arg, RFS4_DBS_VALID);
2337 		if (fp != NULL) {
2338 			rw_enter(&fp->rf_file_rwlock, RW_WRITER);
2339 			if (fp->rf_vp == NULL) {
2340 				rw_exit(&fp->rf_file_rwlock);
2341 				rfs4_file_rele(fp);
2342 				*create = screate;
2343 				goto retry;
2344 			}
2345 		}
2346 	}
2347 
2348 	return (fp);
2349 }
2350 
2351 static uint32_t
2352 lo_state_hash(void *key)
2353 {
2354 	stateid_t *id = key;
2355 
2356 	return (id->bits.ident+id->bits.pid);
2357 }
2358 
2359 static bool_t
2360 lo_state_compare(rfs4_entry_t u_entry, void *key)
2361 {
2362 	rfs4_lo_state_t *lsp = (rfs4_lo_state_t *)u_entry;
2363 	stateid_t *id = key;
2364 	bool_t rc;
2365 
2366 	rc = (lsp->rls_lockid.bits.boottime == id->bits.boottime &&
2367 	    lsp->rls_lockid.bits.type == id->bits.type &&
2368 	    lsp->rls_lockid.bits.ident == id->bits.ident &&
2369 	    lsp->rls_lockid.bits.pid == id->bits.pid);
2370 
2371 	return (rc);
2372 }
2373 
2374 static void *
2375 lo_state_mkkey(rfs4_entry_t u_entry)
2376 {
2377 	rfs4_lo_state_t *lsp = (rfs4_lo_state_t *)u_entry;
2378 
2379 	return (&lsp->rls_lockid);
2380 }
2381 
2382 static bool_t
2383 rfs4_lo_state_expiry(rfs4_entry_t u_entry)
2384 {
2385 	rfs4_lo_state_t *lsp = (rfs4_lo_state_t *)u_entry;
2386 
2387 	if (rfs4_dbe_is_invalid(lsp->rls_dbe))
2388 		return (TRUE);
2389 	if (lsp->rls_state->rs_closed)
2390 		return (TRUE);
2391 	return ((gethrestime_sec() -
2392 	    lsp->rls_state->rs_owner->ro_client->rc_last_access
2393 	    > rfs4_lease_time));
2394 }
2395 
2396 static void
2397 rfs4_lo_state_destroy(rfs4_entry_t u_entry)
2398 {
2399 	rfs4_lo_state_t *lsp = (rfs4_lo_state_t *)u_entry;
2400 
2401 	rfs4_dbe_lock(lsp->rls_state->rs_dbe);
2402 	list_remove(&lsp->rls_state->rs_lostatelist, lsp);
2403 	rfs4_dbe_unlock(lsp->rls_state->rs_dbe);
2404 
2405 	rfs4_sw_destroy(&lsp->rls_sw);
2406 
2407 	/* Make sure to release the file locks */
2408 	if (lsp->rls_locks_cleaned == FALSE) {
2409 		lsp->rls_locks_cleaned = TRUE;
2410 		if (lsp->rls_locker->rl_client->rc_sysidt != LM_NOSYSID) {
2411 			/* Is the PxFS kernel module loaded? */
2412 			if (lm_remove_file_locks != NULL) {
2413 				int new_sysid;
2414 
2415 				/* Encode the cluster nodeid in new sysid */
2416 				new_sysid =
2417 				    lsp->rls_locker->rl_client->rc_sysidt;
2418 				lm_set_nlmid_flk(&new_sysid);
2419 
2420 				/*
2421 				 * This PxFS routine removes file locks for a
2422 				 * client over all nodes of a cluster.
2423 				 */
2424 				DTRACE_PROBE1(nfss_i_clust_rm_lck,
2425 				    int, new_sysid);
2426 				(*lm_remove_file_locks)(new_sysid);
2427 			} else {
2428 				(void) cleanlocks(
2429 				    lsp->rls_state->rs_finfo->rf_vp,
2430 				    lsp->rls_locker->rl_pid,
2431 				    lsp->rls_locker->rl_client->rc_sysidt);
2432 			}
2433 		}
2434 	}
2435 
2436 	/* Free the last reply for this state */
2437 	rfs4_free_reply(&lsp->rls_reply);
2438 
2439 	rfs4_lockowner_rele(lsp->rls_locker);
2440 	lsp->rls_locker = NULL;
2441 
2442 	rfs4_state_rele_nounlock(lsp->rls_state);
2443 	lsp->rls_state = NULL;
2444 }
2445 
2446 static bool_t
2447 rfs4_lo_state_create(rfs4_entry_t u_entry, void *arg)
2448 {
2449 	rfs4_lo_state_t *lsp = (rfs4_lo_state_t *)u_entry;
2450 	rfs4_lo_state_t *argp = (rfs4_lo_state_t *)arg;
2451 	rfs4_lockowner_t *lo = argp->rls_locker;
2452 	rfs4_state_t *sp = argp->rls_state;
2453 
2454 	lsp->rls_state = sp;
2455 
2456 	lsp->rls_lockid = sp->rs_stateid;
2457 	lsp->rls_lockid.bits.type = LOCKID;
2458 	lsp->rls_lockid.bits.chgseq = 0;
2459 	lsp->rls_lockid.bits.pid = lo->rl_pid;
2460 
2461 	lsp->rls_locks_cleaned = FALSE;
2462 	lsp->rls_lock_completed = FALSE;
2463 
2464 	rfs4_sw_init(&lsp->rls_sw);
2465 
2466 	/* Attached the supplied lock owner */
2467 	rfs4_dbe_hold(lo->rl_dbe);
2468 	lsp->rls_locker = lo;
2469 
2470 	rfs4_dbe_lock(sp->rs_dbe);
2471 	list_insert_tail(&sp->rs_lostatelist, lsp);
2472 	rfs4_dbe_hold(sp->rs_dbe);
2473 	rfs4_dbe_unlock(sp->rs_dbe);
2474 
2475 	return (TRUE);
2476 }
2477 
2478 void
2479 rfs4_lo_state_rele(rfs4_lo_state_t *lsp, bool_t unlock_fp)
2480 {
2481 	if (unlock_fp == TRUE)
2482 		rw_exit(&lsp->rls_state->rs_finfo->rf_file_rwlock);
2483 	rfs4_dbe_rele(lsp->rls_dbe);
2484 }
2485 
2486 static rfs4_lo_state_t *
2487 rfs4_findlo_state(stateid_t *id, bool_t lock_fp)
2488 {
2489 	rfs4_lo_state_t *lsp;
2490 	bool_t create = FALSE;
2491 
2492 	lsp = (rfs4_lo_state_t *)rfs4_dbsearch(rfs4_lo_state_idx, id,
2493 	    &create, NULL, RFS4_DBS_VALID);
2494 	if (lock_fp == TRUE && lsp != NULL)
2495 		rw_enter(&lsp->rls_state->rs_finfo->rf_file_rwlock, RW_READER);
2496 
2497 	return (lsp);
2498 }
2499 
2500 
2501 static uint32_t
2502 lo_state_lo_hash(void *key)
2503 {
2504 	rfs4_lo_state_t *lsp = key;
2505 
2506 	return (ADDRHASH(lsp->rls_locker) ^ ADDRHASH(lsp->rls_state));
2507 }
2508 
2509 static bool_t
2510 lo_state_lo_compare(rfs4_entry_t u_entry, void *key)
2511 {
2512 	rfs4_lo_state_t *lsp = (rfs4_lo_state_t *)u_entry;
2513 	rfs4_lo_state_t *keyp = key;
2514 
2515 	return (keyp->rls_locker == lsp->rls_locker &&
2516 	    keyp->rls_state == lsp->rls_state);
2517 }
2518 
2519 static void *
2520 lo_state_lo_mkkey(rfs4_entry_t u_entry)
2521 {
2522 	return (u_entry);
2523 }
2524 
2525 rfs4_lo_state_t *
2526 rfs4_findlo_state_by_owner(rfs4_lockowner_t *lo, rfs4_state_t *sp,
2527     bool_t *create)
2528 {
2529 	rfs4_lo_state_t *lsp;
2530 	rfs4_lo_state_t arg;
2531 
2532 	arg.rls_locker = lo;
2533 	arg.rls_state = sp;
2534 
2535 	lsp = (rfs4_lo_state_t *)rfs4_dbsearch(rfs4_lo_state_owner_idx, &arg,
2536 	    create, &arg, RFS4_DBS_VALID);
2537 
2538 	return (lsp);
2539 }
2540 
2541 static stateid_t
2542 get_stateid(id_t eid)
2543 {
2544 	stateid_t id;
2545 
2546 	id.bits.boottime = rfs4_start_time;
2547 	id.bits.ident = eid;
2548 	id.bits.chgseq = 0;
2549 	id.bits.type = 0;
2550 	id.bits.pid = 0;
2551 
2552 	/*
2553 	 * If we are booted as a cluster node, embed our nodeid.
2554 	 * We've already done sanity checks in rfs4_client_create() so no
2555 	 * need to repeat them here.
2556 	 */
2557 	id.bits.clnodeid = (cluster_bootflags & CLUSTER_BOOTED) ?
2558 	    clconf_get_nodeid() : 0;
2559 
2560 	return (id);
2561 }
2562 
2563 /*
2564  * For use only when booted as a cluster node.
2565  * Returns TRUE if the embedded nodeid indicates that this stateid was
2566  * generated on another node.
2567  */
2568 static int
2569 foreign_stateid(stateid_t *id)
2570 {
2571 	ASSERT(cluster_bootflags & CLUSTER_BOOTED);
2572 	return (id->bits.clnodeid != (uint32_t)clconf_get_nodeid());
2573 }
2574 
2575 /*
2576  * For use only when booted as a cluster node.
2577  * Returns TRUE if the embedded nodeid indicates that this clientid was
2578  * generated on another node.
2579  */
2580 static int
2581 foreign_clientid(cid *cidp)
2582 {
2583 	ASSERT(cluster_bootflags & CLUSTER_BOOTED);
2584 	return (cidp->impl_id.c_id >> CLUSTER_NODEID_SHIFT !=
2585 	    (uint32_t)clconf_get_nodeid());
2586 }
2587 
2588 /*
2589  * For use only when booted as a cluster node.
2590  * Embed our cluster nodeid into the clientid.
2591  */
2592 static void
2593 embed_nodeid(cid *cidp)
2594 {
2595 	int clnodeid;
2596 	/*
2597 	 * Currently, our state tables are small enough that their
2598 	 * ids will leave enough bits free for the nodeid. If the
2599 	 * tables become larger, we mustn't overwrite the id.
2600 	 * Equally, we only have room for so many bits of nodeid, so
2601 	 * must check that too.
2602 	 */
2603 	ASSERT(cluster_bootflags & CLUSTER_BOOTED);
2604 	ASSERT(cidp->impl_id.c_id >> CLUSTER_NODEID_SHIFT == 0);
2605 	clnodeid = clconf_get_nodeid();
2606 	ASSERT(clnodeid <= CLUSTER_MAX_NODEID);
2607 	ASSERT(clnodeid != NODEID_UNKNOWN);
2608 	cidp->impl_id.c_id |= (clnodeid << CLUSTER_NODEID_SHIFT);
2609 }
2610 
2611 static uint32_t
2612 state_hash(void *key)
2613 {
2614 	stateid_t *ip = (stateid_t *)key;
2615 
2616 	return (ip->bits.ident);
2617 }
2618 
2619 static bool_t
2620 state_compare(rfs4_entry_t u_entry, void *key)
2621 {
2622 	rfs4_state_t *sp = (rfs4_state_t *)u_entry;
2623 	stateid_t *id = (stateid_t *)key;
2624 	bool_t rc;
2625 
2626 	rc = (sp->rs_stateid.bits.boottime == id->bits.boottime &&
2627 	    sp->rs_stateid.bits.ident == id->bits.ident);
2628 
2629 	return (rc);
2630 }
2631 
2632 static void *
2633 state_mkkey(rfs4_entry_t u_entry)
2634 {
2635 	rfs4_state_t *sp = (rfs4_state_t *)u_entry;
2636 
2637 	return (&sp->rs_stateid);
2638 }
2639 
2640 static void
2641 rfs4_state_destroy(rfs4_entry_t u_entry)
2642 {
2643 	rfs4_state_t *sp = (rfs4_state_t *)u_entry;
2644 
2645 	/* remove from openowner list */
2646 	rfs4_dbe_lock(sp->rs_owner->ro_dbe);
2647 	list_remove(&sp->rs_owner->ro_statelist, sp);
2648 	rfs4_dbe_unlock(sp->rs_owner->ro_dbe);
2649 
2650 	list_destroy(&sp->rs_lostatelist);
2651 
2652 	/* release any share locks for this stateid if it's still open */
2653 	if (!sp->rs_closed) {
2654 		rfs4_dbe_lock(sp->rs_dbe);
2655 		(void) rfs4_unshare(sp);
2656 		rfs4_dbe_unlock(sp->rs_dbe);
2657 	}
2658 
2659 	/* Were done with the file */
2660 	rfs4_file_rele(sp->rs_finfo);
2661 	sp->rs_finfo = NULL;
2662 
2663 	/* And now with the openowner */
2664 	rfs4_openowner_rele(sp->rs_owner);
2665 	sp->rs_owner = NULL;
2666 }
2667 
2668 static void
2669 rfs4_state_rele_nounlock(rfs4_state_t *sp)
2670 {
2671 	rfs4_dbe_rele(sp->rs_dbe);
2672 }
2673 
2674 void
2675 rfs4_state_rele(rfs4_state_t *sp)
2676 {
2677 	rw_exit(&sp->rs_finfo->rf_file_rwlock);
2678 	rfs4_dbe_rele(sp->rs_dbe);
2679 }
2680 
2681 static uint32_t
2682 deleg_hash(void *key)
2683 {
2684 	rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)key;
2685 
2686 	return (ADDRHASH(dsp->rds_client) ^ ADDRHASH(dsp->rds_finfo));
2687 }
2688 
2689 static bool_t
2690 deleg_compare(rfs4_entry_t u_entry, void *key)
2691 {
2692 	rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)u_entry;
2693 	rfs4_deleg_state_t *kdsp = (rfs4_deleg_state_t *)key;
2694 
2695 	return (dsp->rds_client == kdsp->rds_client &&
2696 	    dsp->rds_finfo == kdsp->rds_finfo);
2697 }
2698 
2699 static void *
2700 deleg_mkkey(rfs4_entry_t u_entry)
2701 {
2702 	return (u_entry);
2703 }
2704 
2705 static uint32_t
2706 deleg_state_hash(void *key)
2707 {
2708 	stateid_t *ip = (stateid_t *)key;
2709 
2710 	return (ip->bits.ident);
2711 }
2712 
2713 static bool_t
2714 deleg_state_compare(rfs4_entry_t u_entry, void *key)
2715 {
2716 	rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)u_entry;
2717 	stateid_t *id = (stateid_t *)key;
2718 	bool_t rc;
2719 
2720 	if (id->bits.type != DELEGID)
2721 		return (FALSE);
2722 
2723 	rc = (dsp->rds_delegid.bits.boottime == id->bits.boottime &&
2724 	    dsp->rds_delegid.bits.ident == id->bits.ident);
2725 
2726 	return (rc);
2727 }
2728 
2729 static void *
2730 deleg_state_mkkey(rfs4_entry_t u_entry)
2731 {
2732 	rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)u_entry;
2733 
2734 	return (&dsp->rds_delegid);
2735 }
2736 
2737 static bool_t
2738 rfs4_deleg_state_expiry(rfs4_entry_t u_entry)
2739 {
2740 	rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)u_entry;
2741 
2742 	if (rfs4_dbe_is_invalid(dsp->rds_dbe))
2743 		return (TRUE);
2744 
2745 	if ((gethrestime_sec() - dsp->rds_client->rc_last_access
2746 	    > rfs4_lease_time)) {
2747 		rfs4_dbe_invalidate(dsp->rds_dbe);
2748 		return (TRUE);
2749 	}
2750 
2751 	return (FALSE);
2752 }
2753 
2754 static bool_t
2755 rfs4_deleg_state_create(rfs4_entry_t u_entry, void *argp)
2756 {
2757 	rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)u_entry;
2758 	rfs4_file_t *fp = ((rfs4_deleg_state_t *)argp)->rds_finfo;
2759 	rfs4_client_t *cp = ((rfs4_deleg_state_t *)argp)->rds_client;
2760 
2761 	rfs4_dbe_hold(fp->rf_dbe);
2762 	rfs4_dbe_hold(cp->rc_dbe);
2763 
2764 	dsp->rds_delegid = get_stateid(rfs4_dbe_getid(dsp->rds_dbe));
2765 	dsp->rds_delegid.bits.type = DELEGID;
2766 	dsp->rds_finfo = fp;
2767 	dsp->rds_client = cp;
2768 	dsp->rds_dtype = OPEN_DELEGATE_NONE;
2769 
2770 	dsp->rds_time_granted = gethrestime_sec();	/* observability */
2771 	dsp->rds_time_revoked = 0;
2772 
2773 	list_link_init(&dsp->rds_node);
2774 
2775 	return (TRUE);
2776 }
2777 
2778 static void
2779 rfs4_deleg_state_destroy(rfs4_entry_t u_entry)
2780 {
2781 	rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)u_entry;
2782 
2783 	/* return delegation if necessary */
2784 	rfs4_return_deleg(dsp, FALSE);
2785 
2786 	/* Were done with the file */
2787 	rfs4_file_rele(dsp->rds_finfo);
2788 	dsp->rds_finfo = NULL;
2789 
2790 	/* And now with the openowner */
2791 	rfs4_client_rele(dsp->rds_client);
2792 	dsp->rds_client = NULL;
2793 }
2794 
2795 rfs4_deleg_state_t *
2796 rfs4_finddeleg(rfs4_state_t *sp, bool_t *create)
2797 {
2798 	rfs4_deleg_state_t ds, *dsp;
2799 
2800 	ds.rds_client = sp->rs_owner->ro_client;
2801 	ds.rds_finfo = sp->rs_finfo;
2802 
2803 	dsp = (rfs4_deleg_state_t *)rfs4_dbsearch(rfs4_deleg_idx, &ds,
2804 	    create, &ds, RFS4_DBS_VALID);
2805 
2806 	return (dsp);
2807 }
2808 
2809 rfs4_deleg_state_t *
2810 rfs4_finddelegstate(stateid_t *id)
2811 {
2812 	rfs4_deleg_state_t *dsp;
2813 	bool_t create = FALSE;
2814 
2815 	dsp = (rfs4_deleg_state_t *)rfs4_dbsearch(rfs4_deleg_state_idx, id,
2816 	    &create, NULL, RFS4_DBS_VALID);
2817 
2818 	return (dsp);
2819 }
2820 
2821 void
2822 rfs4_deleg_state_rele(rfs4_deleg_state_t *dsp)
2823 {
2824 	rfs4_dbe_rele(dsp->rds_dbe);
2825 }
2826 
2827 void
2828 rfs4_update_lock_sequence(rfs4_lo_state_t *lsp)
2829 {
2830 
2831 	rfs4_dbe_lock(lsp->rls_dbe);
2832 
2833 	/*
2834 	 * If we are skipping sequence id checking, this means that
2835 	 * this is the first lock request and therefore the sequence
2836 	 * id does not need to be updated.  This only happens on the
2837 	 * first lock request for a lockowner
2838 	 */
2839 	if (!lsp->rls_skip_seqid_check)
2840 		lsp->rls_seqid++;
2841 
2842 	rfs4_dbe_unlock(lsp->rls_dbe);
2843 }
2844 
2845 void
2846 rfs4_update_lock_resp(rfs4_lo_state_t *lsp, nfs_resop4 *resp)
2847 {
2848 
2849 	rfs4_dbe_lock(lsp->rls_dbe);
2850 
2851 	rfs4_free_reply(&lsp->rls_reply);
2852 
2853 	rfs4_copy_reply(&lsp->rls_reply, resp);
2854 
2855 	rfs4_dbe_unlock(lsp->rls_dbe);
2856 }
2857 
2858 void
2859 rfs4_free_opens(rfs4_openowner_t *oo, bool_t invalidate,
2860     bool_t close_of_client)
2861 {
2862 	rfs4_state_t *sp;
2863 
2864 	rfs4_dbe_lock(oo->ro_dbe);
2865 
2866 	for (sp = list_head(&oo->ro_statelist); sp != NULL;
2867 	    sp = list_next(&oo->ro_statelist, sp)) {
2868 		rfs4_state_close(sp, FALSE, close_of_client, CRED());
2869 		if (invalidate == TRUE)
2870 			rfs4_dbe_invalidate(sp->rs_dbe);
2871 	}
2872 
2873 	rfs4_dbe_invalidate(oo->ro_dbe);
2874 	rfs4_dbe_unlock(oo->ro_dbe);
2875 }
2876 
2877 static uint32_t
2878 state_owner_file_hash(void *key)
2879 {
2880 	rfs4_state_t *sp = key;
2881 
2882 	return (ADDRHASH(sp->rs_owner) ^ ADDRHASH(sp->rs_finfo));
2883 }
2884 
2885 static bool_t
2886 state_owner_file_compare(rfs4_entry_t u_entry, void *key)
2887 {
2888 	rfs4_state_t *sp = (rfs4_state_t *)u_entry;
2889 	rfs4_state_t *arg = key;
2890 
2891 	if (sp->rs_closed == TRUE)
2892 		return (FALSE);
2893 
2894 	return (arg->rs_owner == sp->rs_owner && arg->rs_finfo == sp->rs_finfo);
2895 }
2896 
2897 static void *
2898 state_owner_file_mkkey(rfs4_entry_t u_entry)
2899 {
2900 	return (u_entry);
2901 }
2902 
2903 static uint32_t
2904 state_file_hash(void *key)
2905 {
2906 	return (ADDRHASH(key));
2907 }
2908 
2909 static bool_t
2910 state_file_compare(rfs4_entry_t u_entry, void *key)
2911 {
2912 	rfs4_state_t *sp = (rfs4_state_t *)u_entry;
2913 	rfs4_file_t *fp = key;
2914 
2915 	if (sp->rs_closed == TRUE)
2916 		return (FALSE);
2917 
2918 	return (fp == sp->rs_finfo);
2919 }
2920 
2921 static void *
2922 state_file_mkkey(rfs4_entry_t u_entry)
2923 {
2924 	rfs4_state_t *sp = (rfs4_state_t *)u_entry;
2925 
2926 	return (sp->rs_finfo);
2927 }
2928 
2929 rfs4_state_t *
2930 rfs4_findstate_by_owner_file(rfs4_openowner_t *oo, rfs4_file_t *fp,
2931 	bool_t *create)
2932 {
2933 	rfs4_state_t *sp;
2934 	rfs4_state_t key;
2935 
2936 	key.rs_owner = oo;
2937 	key.rs_finfo = fp;
2938 
2939 	sp = (rfs4_state_t *)rfs4_dbsearch(rfs4_state_owner_file_idx, &key,
2940 	    create, &key, RFS4_DBS_VALID);
2941 
2942 	return (sp);
2943 }
2944 
2945 /* This returns ANY state struct that refers to this file */
2946 static rfs4_state_t *
2947 rfs4_findstate_by_file(rfs4_file_t *fp)
2948 {
2949 	bool_t create = FALSE;
2950 
2951 	return ((rfs4_state_t *)rfs4_dbsearch(rfs4_state_file_idx, fp,
2952 	    &create, fp, RFS4_DBS_VALID));
2953 }
2954 
2955 static bool_t
2956 rfs4_state_expiry(rfs4_entry_t u_entry)
2957 {
2958 	rfs4_state_t *sp = (rfs4_state_t *)u_entry;
2959 
2960 	if (rfs4_dbe_is_invalid(sp->rs_dbe))
2961 		return (TRUE);
2962 
2963 	if (sp->rs_closed == TRUE &&
2964 	    ((gethrestime_sec() - rfs4_dbe_get_timerele(sp->rs_dbe))
2965 	    > rfs4_lease_time))
2966 		return (TRUE);
2967 
2968 	return ((gethrestime_sec() - sp->rs_owner->ro_client->rc_last_access
2969 	    > rfs4_lease_time));
2970 }
2971 
2972 static bool_t
2973 rfs4_state_create(rfs4_entry_t u_entry, void *argp)
2974 {
2975 	rfs4_state_t *sp = (rfs4_state_t *)u_entry;
2976 	rfs4_file_t *fp = ((rfs4_state_t *)argp)->rs_finfo;
2977 	rfs4_openowner_t *oo = ((rfs4_state_t *)argp)->rs_owner;
2978 
2979 	rfs4_dbe_hold(fp->rf_dbe);
2980 	rfs4_dbe_hold(oo->ro_dbe);
2981 	sp->rs_stateid = get_stateid(rfs4_dbe_getid(sp->rs_dbe));
2982 	sp->rs_stateid.bits.type = OPENID;
2983 	sp->rs_owner = oo;
2984 	sp->rs_finfo = fp;
2985 
2986 	list_create(&sp->rs_lostatelist, sizeof (rfs4_lo_state_t),
2987 	    offsetof(rfs4_lo_state_t, rls_node));
2988 
2989 	/* Insert state on per open owner's list */
2990 	rfs4_dbe_lock(oo->ro_dbe);
2991 	list_insert_tail(&oo->ro_statelist, sp);
2992 	rfs4_dbe_unlock(oo->ro_dbe);
2993 
2994 	return (TRUE);
2995 }
2996 
2997 static rfs4_state_t *
2998 rfs4_findstate(stateid_t *id, rfs4_dbsearch_type_t find_invalid, bool_t lock_fp)
2999 {
3000 	rfs4_state_t *sp;
3001 	bool_t create = FALSE;
3002 
3003 	sp = (rfs4_state_t *)rfs4_dbsearch(rfs4_state_idx, id,
3004 	    &create, NULL, find_invalid);
3005 	if (lock_fp == TRUE && sp != NULL)
3006 		rw_enter(&sp->rs_finfo->rf_file_rwlock, RW_READER);
3007 
3008 	return (sp);
3009 }
3010 
3011 void
3012 rfs4_state_close(rfs4_state_t *sp, bool_t lock_held, bool_t close_of_client,
3013     cred_t *cr)
3014 {
3015 	/* Remove the associated lo_state owners */
3016 	if (!lock_held)
3017 		rfs4_dbe_lock(sp->rs_dbe);
3018 
3019 	/*
3020 	 * If refcnt == 0, the dbe is about to be destroyed.
3021 	 * lock state will be released by the reaper thread.
3022 	 */
3023 
3024 	if (rfs4_dbe_refcnt(sp->rs_dbe) > 0) {
3025 		if (sp->rs_closed == FALSE) {
3026 			rfs4_release_share_lock_state(sp, cr, close_of_client);
3027 			sp->rs_closed = TRUE;
3028 		}
3029 	}
3030 
3031 	if (!lock_held)
3032 		rfs4_dbe_unlock(sp->rs_dbe);
3033 }
3034 
3035 /*
3036  * Remove all state associated with the given client.
3037  */
3038 void
3039 rfs4_client_state_remove(rfs4_client_t *cp)
3040 {
3041 	rfs4_openowner_t *oo;
3042 
3043 	rfs4_dbe_lock(cp->rc_dbe);
3044 
3045 	for (oo = list_head(&cp->rc_openownerlist); oo != NULL;
3046 	    oo = list_next(&cp->rc_openownerlist, oo)) {
3047 		rfs4_free_opens(oo, TRUE, TRUE);
3048 	}
3049 
3050 	rfs4_dbe_unlock(cp->rc_dbe);
3051 }
3052 
3053 void
3054 rfs4_client_close(rfs4_client_t *cp)
3055 {
3056 	/* Mark client as going away. */
3057 	rfs4_dbe_lock(cp->rc_dbe);
3058 	rfs4_dbe_invalidate(cp->rc_dbe);
3059 	rfs4_dbe_unlock(cp->rc_dbe);
3060 
3061 	rfs4_client_state_remove(cp);
3062 
3063 	/* Release the client */
3064 	rfs4_client_rele(cp);
3065 }
3066 
3067 nfsstat4
3068 rfs4_check_clientid(clientid4 *cp, int setclid_confirm)
3069 {
3070 	cid *cidp = (cid *) cp;
3071 
3072 	/*
3073 	 * If we are booted as a cluster node, check the embedded nodeid.
3074 	 * If it indicates that this clientid was generated on another node,
3075 	 * inform the client accordingly.
3076 	 */
3077 	if (cluster_bootflags & CLUSTER_BOOTED && foreign_clientid(cidp))
3078 		return (NFS4ERR_STALE_CLIENTID);
3079 
3080 	/*
3081 	 * If the server start time matches the time provided
3082 	 * by the client (via the clientid) and this is NOT a
3083 	 * setclientid_confirm then return EXPIRED.
3084 	 */
3085 	if (!setclid_confirm && cidp->impl_id.start_time == rfs4_start_time)
3086 		return (NFS4ERR_EXPIRED);
3087 
3088 	return (NFS4ERR_STALE_CLIENTID);
3089 }
3090 
3091 /*
3092  * This is used when a stateid has not been found amongst the
3093  * current server's state.  Check the stateid to see if it
3094  * was from this server instantiation or not.
3095  */
3096 static nfsstat4
3097 what_stateid_error(stateid_t *id, stateid_type_t type)
3098 {
3099 	/* If we are booted as a cluster node, was stateid locally generated? */
3100 	if ((cluster_bootflags & CLUSTER_BOOTED) && foreign_stateid(id))
3101 		return (NFS4ERR_STALE_STATEID);
3102 
3103 	/* If types don't match then no use checking further */
3104 	if (type != id->bits.type)
3105 		return (NFS4ERR_BAD_STATEID);
3106 
3107 	/* From a previous server instantiation, return STALE */
3108 	if (id->bits.boottime < rfs4_start_time)
3109 		return (NFS4ERR_STALE_STATEID);
3110 
3111 	/*
3112 	 * From this server but the state is most likely beyond lease
3113 	 * timeout: return NFS4ERR_EXPIRED.  However, there is the
3114 	 * case of a delegation stateid.  For delegations, there is a
3115 	 * case where the state can be removed without the client's
3116 	 * knowledge/consent: revocation.  In the case of delegation
3117 	 * revocation, the delegation state will be removed and will
3118 	 * not be found.  If the client does something like a
3119 	 * DELEGRETURN or even a READ/WRITE with a delegatoin stateid
3120 	 * that has been revoked, the server should return BAD_STATEID
3121 	 * instead of the more common EXPIRED error.
3122 	 */
3123 	if (id->bits.boottime == rfs4_start_time) {
3124 		if (type == DELEGID)
3125 			return (NFS4ERR_BAD_STATEID);
3126 		else
3127 			return (NFS4ERR_EXPIRED);
3128 	}
3129 
3130 	return (NFS4ERR_BAD_STATEID);
3131 }
3132 
3133 /*
3134  * Used later on to find the various state structs.  When called from
3135  * rfs4_check_stateid()->rfs4_get_all_state(), no file struct lock is
3136  * taken (it is not needed) and helps on the read/write path with
3137  * respect to performance.
3138  */
3139 static nfsstat4
3140 rfs4_get_state_lockit(stateid4 *stateid, rfs4_state_t **spp,
3141     rfs4_dbsearch_type_t find_invalid, bool_t lock_fp)
3142 {
3143 	stateid_t *id = (stateid_t *)stateid;
3144 	rfs4_state_t *sp;
3145 
3146 	*spp = NULL;
3147 
3148 	/* If we are booted as a cluster node, was stateid locally generated? */
3149 	if ((cluster_bootflags & CLUSTER_BOOTED) && foreign_stateid(id))
3150 		return (NFS4ERR_STALE_STATEID);
3151 
3152 	sp = rfs4_findstate(id, find_invalid, lock_fp);
3153 	if (sp == NULL) {
3154 		return (what_stateid_error(id, OPENID));
3155 	}
3156 
3157 	if (rfs4_lease_expired(sp->rs_owner->ro_client)) {
3158 		if (lock_fp == TRUE)
3159 			rfs4_state_rele(sp);
3160 		else
3161 			rfs4_state_rele_nounlock(sp);
3162 		return (NFS4ERR_EXPIRED);
3163 	}
3164 
3165 	*spp = sp;
3166 
3167 	return (NFS4_OK);
3168 }
3169 
3170 nfsstat4
3171 rfs4_get_state(stateid4 *stateid, rfs4_state_t **spp,
3172     rfs4_dbsearch_type_t find_invalid)
3173 {
3174 	return (rfs4_get_state_lockit(stateid, spp, find_invalid, TRUE));
3175 }
3176 
3177 int
3178 rfs4_check_stateid_seqid(rfs4_state_t *sp, stateid4 *stateid)
3179 {
3180 	stateid_t *id = (stateid_t *)stateid;
3181 
3182 	if (rfs4_lease_expired(sp->rs_owner->ro_client))
3183 		return (NFS4_CHECK_STATEID_EXPIRED);
3184 
3185 	/* Stateid is some time in the future - that's bad */
3186 	if (sp->rs_stateid.bits.chgseq < id->bits.chgseq)
3187 		return (NFS4_CHECK_STATEID_BAD);
3188 
3189 	if (sp->rs_stateid.bits.chgseq == id->bits.chgseq + 1)
3190 		return (NFS4_CHECK_STATEID_REPLAY);
3191 
3192 	/* Stateid is some time in the past - that's old */
3193 	if (sp->rs_stateid.bits.chgseq > id->bits.chgseq)
3194 		return (NFS4_CHECK_STATEID_OLD);
3195 
3196 	/* Caller needs to know about confirmation before closure */
3197 	if (sp->rs_owner->ro_need_confirm)
3198 		return (NFS4_CHECK_STATEID_UNCONFIRMED);
3199 
3200 	if (sp->rs_closed == TRUE)
3201 		return (NFS4_CHECK_STATEID_CLOSED);
3202 
3203 	return (NFS4_CHECK_STATEID_OKAY);
3204 }
3205 
3206 int
3207 rfs4_check_lo_stateid_seqid(rfs4_lo_state_t *lsp, stateid4 *stateid)
3208 {
3209 	stateid_t *id = (stateid_t *)stateid;
3210 
3211 	if (rfs4_lease_expired(lsp->rls_state->rs_owner->ro_client))
3212 		return (NFS4_CHECK_STATEID_EXPIRED);
3213 
3214 	/* Stateid is some time in the future - that's bad */
3215 	if (lsp->rls_lockid.bits.chgseq < id->bits.chgseq)
3216 		return (NFS4_CHECK_STATEID_BAD);
3217 
3218 	if (lsp->rls_lockid.bits.chgseq == id->bits.chgseq + 1)
3219 		return (NFS4_CHECK_STATEID_REPLAY);
3220 
3221 	/* Stateid is some time in the past - that's old */
3222 	if (lsp->rls_lockid.bits.chgseq > id->bits.chgseq)
3223 		return (NFS4_CHECK_STATEID_OLD);
3224 
3225 	if (lsp->rls_state->rs_closed == TRUE)
3226 		return (NFS4_CHECK_STATEID_CLOSED);
3227 
3228 	return (NFS4_CHECK_STATEID_OKAY);
3229 }
3230 
3231 nfsstat4
3232 rfs4_get_deleg_state(stateid4 *stateid, rfs4_deleg_state_t **dspp)
3233 {
3234 	stateid_t *id = (stateid_t *)stateid;
3235 	rfs4_deleg_state_t *dsp;
3236 
3237 	*dspp = NULL;
3238 
3239 	/* If we are booted as a cluster node, was stateid locally generated? */
3240 	if ((cluster_bootflags & CLUSTER_BOOTED) && foreign_stateid(id))
3241 		return (NFS4ERR_STALE_STATEID);
3242 
3243 	dsp = rfs4_finddelegstate(id);
3244 	if (dsp == NULL) {
3245 		return (what_stateid_error(id, DELEGID));
3246 	}
3247 
3248 	if (rfs4_lease_expired(dsp->rds_client)) {
3249 		rfs4_deleg_state_rele(dsp);
3250 		return (NFS4ERR_EXPIRED);
3251 	}
3252 
3253 	*dspp = dsp;
3254 
3255 	return (NFS4_OK);
3256 }
3257 
3258 nfsstat4
3259 rfs4_get_lo_state(stateid4 *stateid, rfs4_lo_state_t **lspp, bool_t lock_fp)
3260 {
3261 	stateid_t *id = (stateid_t *)stateid;
3262 	rfs4_lo_state_t *lsp;
3263 
3264 	*lspp = NULL;
3265 
3266 	/* If we are booted as a cluster node, was stateid locally generated? */
3267 	if ((cluster_bootflags & CLUSTER_BOOTED) && foreign_stateid(id))
3268 		return (NFS4ERR_STALE_STATEID);
3269 
3270 	lsp = rfs4_findlo_state(id, lock_fp);
3271 	if (lsp == NULL) {
3272 		return (what_stateid_error(id, LOCKID));
3273 	}
3274 
3275 	if (rfs4_lease_expired(lsp->rls_state->rs_owner->ro_client)) {
3276 		rfs4_lo_state_rele(lsp, lock_fp);
3277 		return (NFS4ERR_EXPIRED);
3278 	}
3279 
3280 	*lspp = lsp;
3281 
3282 	return (NFS4_OK);
3283 }
3284 
3285 static nfsstat4
3286 rfs4_get_all_state(stateid4 *sid, rfs4_state_t **spp,
3287     rfs4_deleg_state_t **dspp, rfs4_lo_state_t **lspp)
3288 {
3289 	rfs4_state_t *sp = NULL;
3290 	rfs4_deleg_state_t *dsp = NULL;
3291 	rfs4_lo_state_t *lsp = NULL;
3292 	stateid_t *id;
3293 	nfsstat4 status;
3294 
3295 	*spp = NULL; *dspp = NULL; *lspp = NULL;
3296 
3297 	id = (stateid_t *)sid;
3298 	switch (id->bits.type) {
3299 	case OPENID:
3300 		status = rfs4_get_state_lockit(sid, &sp, FALSE, FALSE);
3301 		break;
3302 	case DELEGID:
3303 		status = rfs4_get_deleg_state(sid, &dsp);
3304 		break;
3305 	case LOCKID:
3306 		status = rfs4_get_lo_state(sid, &lsp, FALSE);
3307 		if (status == NFS4_OK) {
3308 			sp = lsp->rls_state;
3309 			rfs4_dbe_hold(sp->rs_dbe);
3310 		}
3311 		break;
3312 	default:
3313 		status = NFS4ERR_BAD_STATEID;
3314 	}
3315 
3316 	if (status == NFS4_OK) {
3317 		*spp = sp;
3318 		*dspp = dsp;
3319 		*lspp = lsp;
3320 	}
3321 
3322 	return (status);
3323 }
3324 
3325 /*
3326  * Given the I/O mode (FREAD or FWRITE), this checks whether the
3327  * rfs4_state_t struct has access to do this operation and if so
3328  * return NFS4_OK; otherwise the proper NFSv4 error is returned.
3329  */
3330 nfsstat4
3331 rfs4_state_has_access(rfs4_state_t *sp, int mode, vnode_t *vp)
3332 {
3333 	nfsstat4 stat = NFS4_OK;
3334 	rfs4_file_t *fp;
3335 	bool_t create = FALSE;
3336 
3337 	rfs4_dbe_lock(sp->rs_dbe);
3338 	if (mode == FWRITE) {
3339 		if (!(sp->rs_share_access & OPEN4_SHARE_ACCESS_WRITE)) {
3340 			stat = NFS4ERR_OPENMODE;
3341 		}
3342 	} else if (mode == FREAD) {
3343 		if (!(sp->rs_share_access & OPEN4_SHARE_ACCESS_READ)) {
3344 			/*
3345 			 * If we have OPENed the file with DENYing access
3346 			 * to both READ and WRITE then no one else could
3347 			 * have OPENed the file, hence no conflicting READ
3348 			 * deny.  This check is merely an optimization.
3349 			 */
3350 			if (sp->rs_share_deny == OPEN4_SHARE_DENY_BOTH)
3351 				goto out;
3352 
3353 			/* Check against file struct's DENY mode */
3354 			fp = rfs4_findfile(vp, NULL, &create);
3355 			if (fp != NULL) {
3356 				int deny_read = 0;
3357 				rfs4_dbe_lock(fp->rf_dbe);
3358 				/*
3359 				 * Check if any other open owner has the file
3360 				 * OPENed with deny READ.
3361 				 */
3362 				if (sp->rs_share_deny & OPEN4_SHARE_DENY_READ)
3363 					deny_read = 1;
3364 				ASSERT(fp->rf_deny_read - deny_read >= 0);
3365 				if (fp->rf_deny_read - deny_read > 0)
3366 					stat = NFS4ERR_OPENMODE;
3367 				rfs4_dbe_unlock(fp->rf_dbe);
3368 				rfs4_file_rele(fp);
3369 			}
3370 		}
3371 	} else {
3372 		/* Illegal I/O mode */
3373 		stat = NFS4ERR_INVAL;
3374 	}
3375 out:
3376 	rfs4_dbe_unlock(sp->rs_dbe);
3377 	return (stat);
3378 }
3379 
3380 /*
3381  * Given the I/O mode (FREAD or FWRITE), the vnode, the stateid and whether
3382  * the file is being truncated, return NFS4_OK if allowed or appropriate
3383  * V4 error if not. Note NFS4ERR_DELAY will be returned and a recall on
3384  * the associated file will be done if the I/O is not consistent with any
3385  * delegation in effect on the file. Should be holding VOP_RWLOCK, either
3386  * as reader or writer as appropriate. rfs4_op_open will acquire the
3387  * VOP_RWLOCK as writer when setting up delegation. If the stateid is bad
3388  * this routine will return NFS4ERR_BAD_STATEID. In addition, through the
3389  * deleg parameter, we will return whether a write delegation is held by
3390  * the client associated with this stateid.
3391  * If the server instance associated with the relevant client is in its
3392  * grace period, return NFS4ERR_GRACE.
3393  */
3394 
3395 nfsstat4
3396 rfs4_check_stateid(int mode, vnode_t *vp,
3397     stateid4 *stateid, bool_t trunc, bool_t *deleg,
3398     bool_t do_access, caller_context_t *ct)
3399 {
3400 	rfs4_file_t *fp;
3401 	bool_t create = FALSE;
3402 	rfs4_state_t *sp;
3403 	rfs4_deleg_state_t *dsp;
3404 	rfs4_lo_state_t *lsp;
3405 	stateid_t *id = (stateid_t *)stateid;
3406 	nfsstat4 stat = NFS4_OK;
3407 
3408 	if (ct != NULL) {
3409 		ct->cc_sysid = 0;
3410 		ct->cc_pid = 0;
3411 		ct->cc_caller_id = nfs4_srv_caller_id;
3412 		ct->cc_flags = CC_DONTBLOCK;
3413 	}
3414 
3415 	if (ISSPECIAL(stateid)) {
3416 		fp = rfs4_findfile(vp, NULL, &create);
3417 		if (fp == NULL)
3418 			return (NFS4_OK);
3419 		if (fp->rf_dinfo.rd_dtype == OPEN_DELEGATE_NONE) {
3420 			rfs4_file_rele(fp);
3421 			return (NFS4_OK);
3422 		}
3423 		if (mode == FWRITE ||
3424 		    fp->rf_dinfo.rd_dtype == OPEN_DELEGATE_WRITE) {
3425 			rfs4_recall_deleg(fp, trunc, NULL);
3426 			rfs4_file_rele(fp);
3427 			return (NFS4ERR_DELAY);
3428 		}
3429 		rfs4_file_rele(fp);
3430 		return (NFS4_OK);
3431 	} else {
3432 		stat = rfs4_get_all_state(stateid, &sp, &dsp, &lsp);
3433 		if (stat != NFS4_OK)
3434 			return (stat);
3435 		if (lsp != NULL) {
3436 			/* Is associated server instance in its grace period? */
3437 			if (rfs4_clnt_in_grace(lsp->rls_locker->rl_client)) {
3438 				rfs4_lo_state_rele(lsp, FALSE);
3439 				if (sp != NULL)
3440 					rfs4_state_rele_nounlock(sp);
3441 				return (NFS4ERR_GRACE);
3442 			}
3443 			if (id->bits.type == LOCKID) {
3444 				/* Seqid in the future? - that's bad */
3445 				if (lsp->rls_lockid.bits.chgseq <
3446 				    id->bits.chgseq) {
3447 					rfs4_lo_state_rele(lsp, FALSE);
3448 					if (sp != NULL)
3449 						rfs4_state_rele_nounlock(sp);
3450 					return (NFS4ERR_BAD_STATEID);
3451 				}
3452 				/* Seqid in the past? - that's old */
3453 				if (lsp->rls_lockid.bits.chgseq >
3454 				    id->bits.chgseq) {
3455 					rfs4_lo_state_rele(lsp, FALSE);
3456 					if (sp != NULL)
3457 						rfs4_state_rele_nounlock(sp);
3458 					return (NFS4ERR_OLD_STATEID);
3459 				}
3460 				/* Ensure specified filehandle matches */
3461 				if (lsp->rls_state->rs_finfo->rf_vp != vp) {
3462 					rfs4_lo_state_rele(lsp, FALSE);
3463 					if (sp != NULL)
3464 						rfs4_state_rele_nounlock(sp);
3465 					return (NFS4ERR_BAD_STATEID);
3466 				}
3467 			}
3468 			if (ct != NULL) {
3469 				ct->cc_sysid =
3470 				    lsp->rls_locker->rl_client->rc_sysidt;
3471 				ct->cc_pid = lsp->rls_locker->rl_pid;
3472 			}
3473 			rfs4_lo_state_rele(lsp, FALSE);
3474 		}
3475 
3476 		/* Stateid provided was an "open" stateid */
3477 		if (sp != NULL) {
3478 			/* Is associated server instance in its grace period? */
3479 			if (rfs4_clnt_in_grace(sp->rs_owner->ro_client)) {
3480 				rfs4_state_rele_nounlock(sp);
3481 				return (NFS4ERR_GRACE);
3482 			}
3483 			if (id->bits.type == OPENID) {
3484 				/* Seqid in the future? - that's bad */
3485 				if (sp->rs_stateid.bits.chgseq <
3486 				    id->bits.chgseq) {
3487 					rfs4_state_rele_nounlock(sp);
3488 					return (NFS4ERR_BAD_STATEID);
3489 				}
3490 				/* Seqid in the past - that's old */
3491 				if (sp->rs_stateid.bits.chgseq >
3492 				    id->bits.chgseq) {
3493 					rfs4_state_rele_nounlock(sp);
3494 					return (NFS4ERR_OLD_STATEID);
3495 				}
3496 			}
3497 			/* Ensure specified filehandle matches */
3498 			if (sp->rs_finfo->rf_vp != vp) {
3499 				rfs4_state_rele_nounlock(sp);
3500 				return (NFS4ERR_BAD_STATEID);
3501 			}
3502 
3503 			if (sp->rs_owner->ro_need_confirm) {
3504 				rfs4_state_rele_nounlock(sp);
3505 				return (NFS4ERR_BAD_STATEID);
3506 			}
3507 
3508 			if (sp->rs_closed == TRUE) {
3509 				rfs4_state_rele_nounlock(sp);
3510 				return (NFS4ERR_OLD_STATEID);
3511 			}
3512 
3513 			if (do_access)
3514 				stat = rfs4_state_has_access(sp, mode, vp);
3515 			else
3516 				stat = NFS4_OK;
3517 
3518 			/*
3519 			 * Return whether this state has write
3520 			 * delegation if desired
3521 			 */
3522 			if (deleg && (sp->rs_finfo->rf_dinfo.rd_dtype ==
3523 			    OPEN_DELEGATE_WRITE))
3524 				*deleg = TRUE;
3525 
3526 			/*
3527 			 * We got a valid stateid, so we update the
3528 			 * lease on the client. Ideally we would like
3529 			 * to do this after the calling op succeeds,
3530 			 * but for now this will be good
3531 			 * enough. Callers of this routine are
3532 			 * currently insulated from the state stuff.
3533 			 */
3534 			rfs4_update_lease(sp->rs_owner->ro_client);
3535 
3536 			/*
3537 			 * If a delegation is present on this file and
3538 			 * this is a WRITE, then update the lastwrite
3539 			 * time to indicate that activity is present.
3540 			 */
3541 			if (sp->rs_finfo->rf_dinfo.rd_dtype ==
3542 			    OPEN_DELEGATE_WRITE &&
3543 			    mode == FWRITE) {
3544 				sp->rs_finfo->rf_dinfo.rd_time_lastwrite =
3545 				    gethrestime_sec();
3546 			}
3547 
3548 			rfs4_state_rele_nounlock(sp);
3549 
3550 			return (stat);
3551 		}
3552 
3553 		if (dsp != NULL) {
3554 			/* Is associated server instance in its grace period? */
3555 			if (rfs4_clnt_in_grace(dsp->rds_client)) {
3556 				rfs4_deleg_state_rele(dsp);
3557 				return (NFS4ERR_GRACE);
3558 			}
3559 			if (dsp->rds_delegid.bits.chgseq != id->bits.chgseq) {
3560 				rfs4_deleg_state_rele(dsp);
3561 				return (NFS4ERR_BAD_STATEID);
3562 			}
3563 
3564 			/* Ensure specified filehandle matches */
3565 			if (dsp->rds_finfo->rf_vp != vp) {
3566 				rfs4_deleg_state_rele(dsp);
3567 				return (NFS4ERR_BAD_STATEID);
3568 			}
3569 			/*
3570 			 * Return whether this state has write
3571 			 * delegation if desired
3572 			 */
3573 			if (deleg && (dsp->rds_finfo->rf_dinfo.rd_dtype ==
3574 			    OPEN_DELEGATE_WRITE))
3575 				*deleg = TRUE;
3576 
3577 			rfs4_update_lease(dsp->rds_client);
3578 
3579 			/*
3580 			 * If a delegation is present on this file and
3581 			 * this is a WRITE, then update the lastwrite
3582 			 * time to indicate that activity is present.
3583 			 */
3584 			if (dsp->rds_finfo->rf_dinfo.rd_dtype ==
3585 			    OPEN_DELEGATE_WRITE && mode == FWRITE) {
3586 				dsp->rds_finfo->rf_dinfo.rd_time_lastwrite =
3587 				    gethrestime_sec();
3588 			}
3589 
3590 			/*
3591 			 * XXX - what happens if this is a WRITE and the
3592 			 * delegation type of for READ.
3593 			 */
3594 			rfs4_deleg_state_rele(dsp);
3595 
3596 			return (stat);
3597 		}
3598 		/*
3599 		 * If we got this far, something bad happened
3600 		 */
3601 		return (NFS4ERR_BAD_STATEID);
3602 	}
3603 }
3604 
3605 
3606 /*
3607  * This is a special function in that for the file struct provided the
3608  * server wants to remove/close all current state associated with the
3609  * file.  The prime use of this would be with OP_REMOVE to force the
3610  * release of state and particularly of file locks.
3611  *
3612  * There is an assumption that there is no delegations outstanding on
3613  * this file at this point.  The caller should have waited for those
3614  * to be returned or revoked.
3615  */
3616 void
3617 rfs4_close_all_state(rfs4_file_t *fp)
3618 {
3619 	rfs4_state_t *sp;
3620 
3621 	rfs4_dbe_lock(fp->rf_dbe);
3622 
3623 #ifdef DEBUG
3624 	/* only applies when server is handing out delegations */
3625 	if (rfs4_deleg_policy != SRV_NEVER_DELEGATE)
3626 		ASSERT(fp->rf_dinfo.rd_hold_grant > 0);
3627 #endif
3628 
3629 	/* No delegations for this file */
3630 	ASSERT(list_is_empty(&fp->rf_delegstatelist));
3631 
3632 	/* Make sure that it can not be found */
3633 	rfs4_dbe_invalidate(fp->rf_dbe);
3634 
3635 	if (fp->rf_vp == NULL) {
3636 		rfs4_dbe_unlock(fp->rf_dbe);
3637 		return;
3638 	}
3639 	rfs4_dbe_unlock(fp->rf_dbe);
3640 
3641 	/*
3642 	 * Hold as writer to prevent other server threads from
3643 	 * processing requests related to the file while all state is
3644 	 * being removed.
3645 	 */
3646 	rw_enter(&fp->rf_file_rwlock, RW_WRITER);
3647 
3648 	/* Remove ALL state from the file */
3649 	while (sp = rfs4_findstate_by_file(fp)) {
3650 		rfs4_state_close(sp, FALSE, FALSE, CRED());
3651 		rfs4_state_rele_nounlock(sp);
3652 	}
3653 
3654 	/*
3655 	 * This is only safe since there are no further references to
3656 	 * the file.
3657 	 */
3658 	rfs4_dbe_lock(fp->rf_dbe);
3659 	if (fp->rf_vp) {
3660 		vnode_t *vp = fp->rf_vp;
3661 
3662 		mutex_enter(&vp->v_vsd_lock);
3663 		(void) vsd_set(vp, nfs4_srv_vkey, NULL);
3664 		mutex_exit(&vp->v_vsd_lock);
3665 		VN_RELE(vp);
3666 		fp->rf_vp = NULL;
3667 	}
3668 	rfs4_dbe_unlock(fp->rf_dbe);
3669 
3670 	/* Finally let other references to proceed */
3671 	rw_exit(&fp->rf_file_rwlock);
3672 }
3673 
3674 /*
3675  * This function is used as a target for the rfs4_dbe_walk() call
3676  * below.  The purpose of this function is to see if the
3677  * lockowner_state refers to a file that resides within the exportinfo
3678  * export.  If so, then remove the lock_owner state (file locks and
3679  * share "locks") for this object since the intent is the server is
3680  * unexporting the specified directory.  Be sure to invalidate the
3681  * object after the state has been released
3682  */
3683 static void
3684 rfs4_lo_state_walk_callout(rfs4_entry_t u_entry, void *e)
3685 {
3686 	rfs4_lo_state_t *lsp = (rfs4_lo_state_t *)u_entry;
3687 	struct exportinfo *exi = (struct exportinfo *)e;
3688 	nfs_fh4_fmt_t   fhfmt4, *exi_fhp, *finfo_fhp;
3689 	fhandle_t *efhp;
3690 
3691 	efhp = (fhandle_t *)&exi->exi_fh;
3692 	exi_fhp = (nfs_fh4_fmt_t *)&fhfmt4;
3693 
3694 	FH_TO_FMT4(efhp, exi_fhp);
3695 
3696 	finfo_fhp = (nfs_fh4_fmt_t *)lsp->rls_state->rs_finfo->
3697 	    rf_filehandle.nfs_fh4_val;
3698 
3699 	if (EQFSID(&finfo_fhp->fh4_fsid, &exi_fhp->fh4_fsid) &&
3700 	    bcmp(&finfo_fhp->fh4_xdata, &exi_fhp->fh4_xdata,
3701 	    exi_fhp->fh4_xlen) == 0) {
3702 		rfs4_state_close(lsp->rls_state, FALSE, FALSE, CRED());
3703 		rfs4_dbe_invalidate(lsp->rls_dbe);
3704 		rfs4_dbe_invalidate(lsp->rls_state->rs_dbe);
3705 	}
3706 }
3707 
3708 /*
3709  * This function is used as a target for the rfs4_dbe_walk() call
3710  * below.  The purpose of this function is to see if the state refers
3711  * to a file that resides within the exportinfo export.  If so, then
3712  * remove the open state for this object since the intent is the
3713  * server is unexporting the specified directory.  The main result for
3714  * this type of entry is to invalidate it such it will not be found in
3715  * the future.
3716  */
3717 static void
3718 rfs4_state_walk_callout(rfs4_entry_t u_entry, void *e)
3719 {
3720 	rfs4_state_t *sp = (rfs4_state_t *)u_entry;
3721 	struct exportinfo *exi = (struct exportinfo *)e;
3722 	nfs_fh4_fmt_t   fhfmt4, *exi_fhp, *finfo_fhp;
3723 	fhandle_t *efhp;
3724 
3725 	efhp = (fhandle_t *)&exi->exi_fh;
3726 	exi_fhp = (nfs_fh4_fmt_t *)&fhfmt4;
3727 
3728 	FH_TO_FMT4(efhp, exi_fhp);
3729 
3730 	finfo_fhp =
3731 	    (nfs_fh4_fmt_t *)sp->rs_finfo->rf_filehandle.nfs_fh4_val;
3732 
3733 	if (EQFSID(&finfo_fhp->fh4_fsid, &exi_fhp->fh4_fsid) &&
3734 	    bcmp(&finfo_fhp->fh4_xdata, &exi_fhp->fh4_xdata,
3735 	    exi_fhp->fh4_xlen) == 0) {
3736 		rfs4_state_close(sp, TRUE, FALSE, CRED());
3737 		rfs4_dbe_invalidate(sp->rs_dbe);
3738 	}
3739 }
3740 
3741 /*
3742  * This function is used as a target for the rfs4_dbe_walk() call
3743  * below.  The purpose of this function is to see if the state refers
3744  * to a file that resides within the exportinfo export.  If so, then
3745  * remove the deleg state for this object since the intent is the
3746  * server is unexporting the specified directory.  The main result for
3747  * this type of entry is to invalidate it such it will not be found in
3748  * the future.
3749  */
3750 static void
3751 rfs4_deleg_state_walk_callout(rfs4_entry_t u_entry, void *e)
3752 {
3753 	rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)u_entry;
3754 	struct exportinfo *exi = (struct exportinfo *)e;
3755 	nfs_fh4_fmt_t   fhfmt4, *exi_fhp, *finfo_fhp;
3756 	fhandle_t *efhp;
3757 
3758 	efhp = (fhandle_t *)&exi->exi_fh;
3759 	exi_fhp = (nfs_fh4_fmt_t *)&fhfmt4;
3760 
3761 	FH_TO_FMT4(efhp, exi_fhp);
3762 
3763 	finfo_fhp =
3764 	    (nfs_fh4_fmt_t *)dsp->rds_finfo->rf_filehandle.nfs_fh4_val;
3765 
3766 	if (EQFSID(&finfo_fhp->fh4_fsid, &exi_fhp->fh4_fsid) &&
3767 	    bcmp(&finfo_fhp->fh4_xdata, &exi_fhp->fh4_xdata,
3768 	    exi_fhp->fh4_xlen) == 0) {
3769 		rfs4_dbe_invalidate(dsp->rds_dbe);
3770 	}
3771 }
3772 
3773 /*
3774  * This function is used as a target for the rfs4_dbe_walk() call
3775  * below.  The purpose of this function is to see if the state refers
3776  * to a file that resides within the exportinfo export.  If so, then
3777  * release vnode hold for this object since the intent is the server
3778  * is unexporting the specified directory.  Invalidation will prevent
3779  * this struct from being found in the future.
3780  */
3781 static void
3782 rfs4_file_walk_callout(rfs4_entry_t u_entry, void *e)
3783 {
3784 	rfs4_file_t *fp = (rfs4_file_t *)u_entry;
3785 	struct exportinfo *exi = (struct exportinfo *)e;
3786 	nfs_fh4_fmt_t   fhfmt4, *exi_fhp, *finfo_fhp;
3787 	fhandle_t *efhp;
3788 
3789 	efhp = (fhandle_t *)&exi->exi_fh;
3790 	exi_fhp = (nfs_fh4_fmt_t *)&fhfmt4;
3791 
3792 	FH_TO_FMT4(efhp, exi_fhp);
3793 
3794 	finfo_fhp = (nfs_fh4_fmt_t *)fp->rf_filehandle.nfs_fh4_val;
3795 
3796 	if (EQFSID(&finfo_fhp->fh4_fsid, &exi_fhp->fh4_fsid) &&
3797 	    bcmp(&finfo_fhp->fh4_xdata, &exi_fhp->fh4_xdata,
3798 	    exi_fhp->fh4_xlen) == 0) {
3799 		if (fp->rf_vp) {
3800 			vnode_t *vp = fp->rf_vp;
3801 
3802 			/*
3803 			 * don't leak monitors and remove the reference
3804 			 * put on the vnode when the delegation was granted.
3805 			 */
3806 			if (fp->rf_dinfo.rd_dtype == OPEN_DELEGATE_READ) {
3807 				(void) fem_uninstall(vp, deleg_rdops,
3808 				    (void *)fp);
3809 				vn_open_downgrade(vp, FREAD);
3810 			} else if (fp->rf_dinfo.rd_dtype ==
3811 			    OPEN_DELEGATE_WRITE) {
3812 				(void) fem_uninstall(vp, deleg_wrops,
3813 				    (void *)fp);
3814 				vn_open_downgrade(vp, FREAD|FWRITE);
3815 			}
3816 			mutex_enter(&vp->v_vsd_lock);
3817 			(void) vsd_set(vp, nfs4_srv_vkey, NULL);
3818 			mutex_exit(&vp->v_vsd_lock);
3819 			VN_RELE(vp);
3820 			fp->rf_vp = NULL;
3821 		}
3822 		rfs4_dbe_invalidate(fp->rf_dbe);
3823 	}
3824 }
3825 
3826 /*
3827  * Given a directory that is being unexported, cleanup/release all
3828  * state in the server that refers to objects residing underneath this
3829  * particular export.  The ordering of the release is important.
3830  * Lock_owner, then state and then file.
3831  */
3832 void
3833 rfs4_clean_state_exi(struct exportinfo *exi)
3834 {
3835 	mutex_enter(&rfs4_state_lock);
3836 
3837 	if (rfs4_server_state == NULL) {
3838 		mutex_exit(&rfs4_state_lock);
3839 		return;
3840 	}
3841 
3842 	rfs4_dbe_walk(rfs4_lo_state_tab, rfs4_lo_state_walk_callout, exi);
3843 	rfs4_dbe_walk(rfs4_state_tab, rfs4_state_walk_callout, exi);
3844 	rfs4_dbe_walk(rfs4_deleg_state_tab, rfs4_deleg_state_walk_callout, exi);
3845 	rfs4_dbe_walk(rfs4_file_tab, rfs4_file_walk_callout, exi);
3846 
3847 	mutex_exit(&rfs4_state_lock);
3848 }
3849