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