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