xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs4_rnode.c (revision d7dc20313645bcb9c9960a816baebe924d60bcf3)
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 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  *  	Copyright (c) 1983,1984,1985,1986,1987,1988,1989  AT&T.
28  *	All Rights Reserved
29  */
30 
31 /*
32  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
33  * Copyright (c) 2017 by Delphix. All rights reserved.
34  */
35 
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/cred.h>
40 #include <sys/proc.h>
41 #include <sys/user.h>
42 #include <sys/time.h>
43 #include <sys/buf.h>
44 #include <sys/vfs.h>
45 #include <sys/vnode.h>
46 #include <sys/socket.h>
47 #include <sys/uio.h>
48 #include <sys/tiuser.h>
49 #include <sys/swap.h>
50 #include <sys/errno.h>
51 #include <sys/debug.h>
52 #include <sys/kmem.h>
53 #include <sys/kstat.h>
54 #include <sys/cmn_err.h>
55 #include <sys/vtrace.h>
56 #include <sys/session.h>
57 #include <sys/dnlc.h>
58 #include <sys/bitmap.h>
59 #include <sys/acl.h>
60 #include <sys/ddi.h>
61 #include <sys/pathname.h>
62 #include <sys/flock.h>
63 #include <sys/dirent.h>
64 #include <sys/flock.h>
65 #include <sys/callb.h>
66 #include <sys/sdt.h>
67 
68 #include <vm/pvn.h>
69 
70 #include <rpc/types.h>
71 #include <rpc/xdr.h>
72 #include <rpc/auth.h>
73 #include <rpc/rpcsec_gss.h>
74 #include <rpc/clnt.h>
75 
76 #include <nfs/nfs.h>
77 #include <nfs/nfs_clnt.h>
78 #include <nfs/nfs_acl.h>
79 
80 #include <nfs/nfs4.h>
81 #include <nfs/rnode4.h>
82 #include <nfs/nfs4_clnt.h>
83 
84 /*
85  * The hash queues for the access to active and cached rnodes
86  * are organized as doubly linked lists.  A reader/writer lock
87  * for each hash bucket is used to control access and to synchronize
88  * lookups, additions, and deletions from the hash queue.
89  *
90  * The rnode freelist is organized as a doubly linked list with
91  * a head pointer.  Additions and deletions are synchronized via
92  * a single mutex.
93  *
94  * In order to add an rnode to the free list, it must be hashed into
95  * a hash queue and the exclusive lock to the hash queue be held.
96  * If an rnode is not hashed into a hash queue, then it is destroyed
97  * because it represents no valuable information that can be reused
98  * about the file.  The exclusive lock to the hash queue must be
99  * held in order to prevent a lookup in the hash queue from finding
100  * the rnode and using it and assuming that the rnode is not on the
101  * freelist.  The lookup in the hash queue will have the hash queue
102  * locked, either exclusive or shared.
103  *
104  * The vnode reference count for each rnode is not allowed to drop
105  * below 1.  This prevents external entities, such as the VM
106  * subsystem, from acquiring references to vnodes already on the
107  * freelist and then trying to place them back on the freelist
108  * when their reference is released.  This means that the when an
109  * rnode is looked up in the hash queues, then either the rnode
110  * is removed from the freelist and that reference is transferred to
111  * the new reference or the vnode reference count must be incremented
112  * accordingly.  The mutex for the freelist must be held in order to
113  * accurately test to see if the rnode is on the freelist or not.
114  * The hash queue lock might be held shared and it is possible that
115  * two different threads may race to remove the rnode from the
116  * freelist.  This race can be resolved by holding the mutex for the
117  * freelist.  Please note that the mutex for the freelist does not
118  * need to be held if the rnode is not on the freelist.  It can not be
119  * placed on the freelist due to the requirement that the thread
120  * putting the rnode on the freelist must hold the exclusive lock
121  * to the hash queue and the thread doing the lookup in the hash
122  * queue is holding either a shared or exclusive lock to the hash
123  * queue.
124  *
125  * The lock ordering is:
126  *
127  *	hash bucket lock -> vnode lock
128  *	hash bucket lock -> freelist lock -> r_statelock
129  */
130 r4hashq_t *rtable4;
131 
132 static kmutex_t rp4freelist_lock;
133 static rnode4_t *rp4freelist = NULL;
134 static long rnode4_new = 0;
135 int rtable4size;
136 static int rtable4mask;
137 static struct kmem_cache *rnode4_cache;
138 static int rnode4_hashlen = 4;
139 
140 static void	r4inactive(rnode4_t *, cred_t *);
141 static vnode_t	*make_rnode4(nfs4_sharedfh_t *, r4hashq_t *, struct vfs *,
142 		    struct vnodeops *,
143 		    int (*)(vnode_t *, page_t *, u_offset_t *, size_t *, int,
144 		    cred_t *),
145 		    int *, cred_t *);
146 static void	rp4_rmfree(rnode4_t *);
147 int		nfs4_free_data_reclaim(rnode4_t *);
148 static int	nfs4_active_data_reclaim(rnode4_t *);
149 static int	nfs4_free_reclaim(void);
150 static int	nfs4_active_reclaim(void);
151 static int	nfs4_rnode_reclaim(void);
152 static void	nfs4_reclaim(void *);
153 static int	isrootfh(nfs4_sharedfh_t *, rnode4_t *);
154 static void	uninit_rnode4(rnode4_t *);
155 static void	destroy_rnode4(rnode4_t *);
156 static void	r4_stub_set(rnode4_t *, nfs4_stub_type_t);
157 
158 #ifdef DEBUG
159 static int r4_check_for_dups = 0; /* Flag to enable dup rnode detection. */
160 static int nfs4_rnode_debug = 0;
161 /* if nonzero, kmem_cache_free() rnodes rather than place on freelist */
162 static int nfs4_rnode_nofreelist = 0;
163 /* give messages on colliding shared filehandles */
164 static void	r4_dup_check(rnode4_t *, vfs_t *);
165 #endif
166 
167 /*
168  * If the vnode has pages, run the list and check for any that are
169  * still dangling.  We call this routine before putting an rnode on
170  * the free list.
171  */
172 static int
173 nfs4_dross_pages(vnode_t *vp)
174 {
175 	page_t *pp;
176 	kmutex_t *vphm;
177 
178 	vphm = page_vnode_mutex(vp);
179 	mutex_enter(vphm);
180 	if ((pp = vp->v_pages) != NULL) {
181 		do {
182 			if (pp->p_hash != PVN_VPLIST_HASH_TAG &&
183 			    pp->p_fsdata != C_NOCOMMIT) {
184 				mutex_exit(vphm);
185 				return (1);
186 			}
187 		} while ((pp = pp->p_vpnext) != vp->v_pages);
188 	}
189 	mutex_exit(vphm);
190 
191 	return (0);
192 }
193 
194 /*
195  * Flush any pages left on this rnode.
196  */
197 static void
198 r4flushpages(rnode4_t *rp, cred_t *cr)
199 {
200 	vnode_t *vp;
201 	int error;
202 
203 	/*
204 	 * Before freeing anything, wait until all asynchronous
205 	 * activity is done on this rnode.  This will allow all
206 	 * asynchronous read ahead and write behind i/o's to
207 	 * finish.
208 	 */
209 	mutex_enter(&rp->r_statelock);
210 	while (rp->r_count > 0)
211 		cv_wait(&rp->r_cv, &rp->r_statelock);
212 	mutex_exit(&rp->r_statelock);
213 
214 	/*
215 	 * Flush and invalidate all pages associated with the vnode.
216 	 */
217 	vp = RTOV4(rp);
218 	if (nfs4_has_pages(vp)) {
219 		ASSERT(vp->v_type != VCHR);
220 		if ((rp->r_flags & R4DIRTY) && !rp->r_error) {
221 			error = VOP_PUTPAGE(vp, (u_offset_t)0, 0, 0, cr, NULL);
222 			if (error && (error == ENOSPC || error == EDQUOT)) {
223 				mutex_enter(&rp->r_statelock);
224 				if (!rp->r_error)
225 					rp->r_error = error;
226 				mutex_exit(&rp->r_statelock);
227 			}
228 		}
229 		nfs4_invalidate_pages(vp, (u_offset_t)0, cr);
230 	}
231 }
232 
233 /*
234  * Free the resources associated with an rnode.
235  */
236 static void
237 r4inactive(rnode4_t *rp, cred_t *cr)
238 {
239 	vnode_t *vp;
240 	char *contents;
241 	int size;
242 	vsecattr_t *vsp;
243 	vnode_t *xattr;
244 
245 	r4flushpages(rp, cr);
246 
247 	vp = RTOV4(rp);
248 
249 	/*
250 	 * Free any held caches which may be
251 	 * associated with this rnode.
252 	 */
253 	mutex_enter(&rp->r_statelock);
254 	contents = rp->r_symlink.contents;
255 	size = rp->r_symlink.size;
256 	rp->r_symlink.contents = NULL;
257 	vsp = rp->r_secattr;
258 	rp->r_secattr = NULL;
259 	xattr = rp->r_xattr_dir;
260 	rp->r_xattr_dir = NULL;
261 	mutex_exit(&rp->r_statelock);
262 
263 	/*
264 	 * Free the access cache entries.
265 	 */
266 	(void) nfs4_access_purge_rp(rp);
267 
268 	/*
269 	 * Free the readdir cache entries.
270 	 */
271 	nfs4_purge_rddir_cache(vp);
272 
273 	/*
274 	 * Free the symbolic link cache.
275 	 */
276 	if (contents != NULL) {
277 
278 		kmem_free((void *)contents, size);
279 	}
280 
281 	/*
282 	 * Free any cached ACL.
283 	 */
284 	if (vsp != NULL)
285 		nfs4_acl_free_cache(vsp);
286 
287 	/*
288 	 * Release the cached xattr_dir
289 	 */
290 	if (xattr != NULL)
291 		VN_RELE(xattr);
292 }
293 
294 /*
295  * We have seen a case that the fh passed in is for "." which
296  * should be a VROOT node, however, the fh is different from the
297  * root fh stored in the mntinfo4_t. The invalid fh might be
298  * from a misbehaved server and will panic the client system at
299  * a later time. To avoid the panic, we drop the bad fh, use
300  * the root fh from mntinfo4_t, and print an error message
301  * for attention.
302  */
303 nfs4_sharedfh_t *
304 badrootfh_check(nfs4_sharedfh_t *fh, nfs4_fname_t *nm, mntinfo4_t *mi,
305     int *wasbad)
306 {
307 	char *s;
308 
309 	*wasbad = 0;
310 	s = fn_name(nm);
311 	ASSERT(strcmp(s, "..") != 0);
312 
313 	if ((s[0] == '.' && s[1] == '\0') && fh &&
314 	    !SFH4_SAME(mi->mi_rootfh, fh)) {
315 #ifdef DEBUG
316 		nfs4_fhandle_t fhandle;
317 
318 		zcmn_err(mi->mi_zone->zone_id, CE_WARN,
319 		    "Server %s returns a different "
320 		    "root filehandle for the path %s:",
321 		    mi->mi_curr_serv->sv_hostname,
322 		    mi->mi_curr_serv->sv_path);
323 
324 		/* print the bad fh */
325 		fhandle.fh_len = fh->sfh_fh.nfs_fh4_len;
326 		bcopy(fh->sfh_fh.nfs_fh4_val, fhandle.fh_buf,
327 		    fhandle.fh_len);
328 		nfs4_printfhandle(&fhandle);
329 
330 		/* print mi_rootfh */
331 		fhandle.fh_len = mi->mi_rootfh->sfh_fh.nfs_fh4_len;
332 		bcopy(mi->mi_rootfh->sfh_fh.nfs_fh4_val, fhandle.fh_buf,
333 		    fhandle.fh_len);
334 		nfs4_printfhandle(&fhandle);
335 #endif
336 		/* use mi_rootfh instead; fh will be rele by the caller */
337 		fh = mi->mi_rootfh;
338 		*wasbad = 1;
339 	}
340 
341 	kmem_free(s, MAXNAMELEN);
342 	return (fh);
343 }
344 
345 void
346 r4_do_attrcache(vnode_t *vp, nfs4_ga_res_t *garp, int newnode,
347     hrtime_t t, cred_t *cr, int index)
348 {
349 	int is_stub;
350 	vattr_t *attr;
351 	/*
352 	 * Don't add to attrcache if time overflow, but
353 	 * no need to check because either attr is null or the time
354 	 * values in it were processed by nfs4_time_ntov(), which checks
355 	 * for time overflows.
356 	 */
357 	attr = garp ? &garp->n4g_va : NULL;
358 
359 	if (attr) {
360 		if (!newnode) {
361 			rw_exit(&rtable4[index].r_lock);
362 #ifdef DEBUG
363 			if (vp->v_type != attr->va_type &&
364 			    vp->v_type != VNON && attr->va_type != VNON) {
365 				zcmn_err(VTOMI4(vp)->mi_zone->zone_id, CE_WARN,
366 				    "makenfs4node: type (%d) doesn't "
367 				    "match type of found node at %p (%d)",
368 				    attr->va_type, (void *)vp, vp->v_type);
369 			}
370 #endif
371 			nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL);
372 		} else {
373 			rnode4_t *rp = VTOR4(vp);
374 
375 			vp->v_type = attr->va_type;
376 			vp->v_rdev = attr->va_rdev;
377 
378 			/*
379 			 * Turn this object into a "stub" object if we
380 			 * crossed an underlying server fs boundary.
381 			 * To make this check, during mount we save the
382 			 * fsid of the server object being mounted.
383 			 * Here we compare this object's server fsid
384 			 * with the fsid we saved at mount.  If they
385 			 * are different, we crossed server fs boundary.
386 			 *
387 			 * The stub type is set (or not) at rnode
388 			 * creation time and it never changes for life
389 			 * of the rnode.
390 			 *
391 			 * This stub will be for a mirror-mount, rather than
392 			 * a referral (the latter also sets R4SRVSTUB).
393 			 *
394 			 * The stub type is also set during RO failover,
395 			 * nfs4_remap_file().
396 			 *
397 			 * We don't bother with taking r_state_lock to
398 			 * set the stub type because this is a new rnode
399 			 * and we're holding the hash bucket r_lock RW_WRITER.
400 			 * No other thread could have obtained access
401 			 * to this rnode.
402 			 */
403 			is_stub = 0;
404 			if (garp->n4g_fsid_valid) {
405 				fattr4_fsid ga_fsid = garp->n4g_fsid;
406 				servinfo4_t *svp = rp->r_server;
407 
408 				rp->r_srv_fsid = ga_fsid;
409 
410 				(void) nfs_rw_enter_sig(&svp->sv_lock,
411 				    RW_READER, 0);
412 				if (!FATTR4_FSID_EQ(&ga_fsid, &svp->sv_fsid))
413 					is_stub = 1;
414 				nfs_rw_exit(&svp->sv_lock);
415 			}
416 
417 			if (is_stub)
418 				r4_stub_mirrormount(rp);
419 			else
420 				r4_stub_none(rp);
421 
422 			/* Can not cache partial attr */
423 			if (attr->va_mask == AT_ALL)
424 				nfs4_attrcache_noinval(vp, garp, t);
425 			else
426 				PURGE_ATTRCACHE4(vp);
427 
428 			rw_exit(&rtable4[index].r_lock);
429 		}
430 	} else {
431 		if (newnode) {
432 			PURGE_ATTRCACHE4(vp);
433 		}
434 		rw_exit(&rtable4[index].r_lock);
435 	}
436 }
437 
438 /*
439  * Find or create an rnode based primarily on filehandle.  To be
440  * used when dvp (vnode for parent directory) is not available;
441  * otherwise, makenfs4node() should be used.
442  *
443  * The nfs4_fname_t argument *npp is consumed and nulled out.
444  */
445 
446 vnode_t *
447 makenfs4node_by_fh(nfs4_sharedfh_t *sfh, nfs4_sharedfh_t *psfh,
448     nfs4_fname_t **npp, nfs4_ga_res_t *garp,
449     mntinfo4_t *mi, cred_t *cr, hrtime_t t)
450 {
451 	vfs_t *vfsp = mi->mi_vfsp;
452 	int newnode = 0;
453 	vnode_t *vp;
454 	rnode4_t *rp;
455 	svnode_t *svp;
456 	nfs4_fname_t *name, *svpname;
457 	int index;
458 
459 	ASSERT(npp && *npp);
460 	name = *npp;
461 	*npp = NULL;
462 
463 	index = rtable4hash(sfh);
464 	rw_enter(&rtable4[index].r_lock, RW_READER);
465 
466 	vp = make_rnode4(sfh, &rtable4[index], vfsp,
467 	    nfs4_vnodeops, nfs4_putapage, &newnode, cr);
468 
469 	svp = VTOSV(vp);
470 	rp = VTOR4(vp);
471 	if (newnode) {
472 		svp->sv_forw = svp->sv_back = svp;
473 		svp->sv_name = name;
474 		if (psfh != NULL)
475 			sfh4_hold(psfh);
476 		svp->sv_dfh = psfh;
477 	} else {
478 		/*
479 		 * It is possible that due to a server
480 		 * side rename fnames have changed.
481 		 * update the fname here.
482 		 */
483 		mutex_enter(&rp->r_svlock);
484 		svpname = svp->sv_name;
485 		if (svp->sv_name != name) {
486 			svp->sv_name = name;
487 			mutex_exit(&rp->r_svlock);
488 			fn_rele(&svpname);
489 		} else {
490 			mutex_exit(&rp->r_svlock);
491 			fn_rele(&name);
492 		}
493 	}
494 
495 	ASSERT(RW_LOCK_HELD(&rtable4[index].r_lock));
496 	r4_do_attrcache(vp, garp, newnode, t, cr, index);
497 	ASSERT(rw_owner(&rtable4[index].r_lock) != curthread);
498 
499 	return (vp);
500 }
501 
502 /*
503  * Find or create a vnode for the given filehandle, filesystem, parent, and
504  * name.  The reference to nm is consumed, so the caller must first do an
505  * fn_hold() if it wants to continue using nm after this call.
506  */
507 vnode_t *
508 makenfs4node(nfs4_sharedfh_t *fh, nfs4_ga_res_t *garp, struct vfs *vfsp,
509     hrtime_t t, cred_t *cr, vnode_t *dvp, nfs4_fname_t *nm)
510 {
511 	vnode_t *vp;
512 	int newnode;
513 	int index;
514 	mntinfo4_t *mi = VFTOMI4(vfsp);
515 	int had_badfh = 0;
516 	rnode4_t *rp;
517 
518 	ASSERT(dvp != NULL);
519 
520 	fh = badrootfh_check(fh, nm, mi, &had_badfh);
521 
522 	index = rtable4hash(fh);
523 	rw_enter(&rtable4[index].r_lock, RW_READER);
524 
525 	/*
526 	 * Note: make_rnode4() may upgrade the hash bucket lock to exclusive.
527 	 */
528 	vp = make_rnode4(fh, &rtable4[index], vfsp, nfs4_vnodeops,
529 	    nfs4_putapage, &newnode, cr);
530 
531 	rp = VTOR4(vp);
532 	sv_activate(&vp, dvp, &nm, newnode);
533 	if (dvp->v_flag & V_XATTRDIR) {
534 		mutex_enter(&rp->r_statelock);
535 		rp->r_flags |= R4ISXATTR;
536 		mutex_exit(&rp->r_statelock);
537 	}
538 
539 	/* if getting a bad file handle, do not cache the attributes. */
540 	if (had_badfh) {
541 		rw_exit(&rtable4[index].r_lock);
542 		return (vp);
543 	}
544 
545 	ASSERT(RW_LOCK_HELD(&rtable4[index].r_lock));
546 	r4_do_attrcache(vp, garp, newnode, t, cr, index);
547 	ASSERT(rw_owner(&rtable4[index].r_lock) != curthread);
548 
549 	return (vp);
550 }
551 
552 /*
553  * Hash on address of filehandle object.
554  * XXX totally untuned.
555  */
556 
557 int
558 rtable4hash(nfs4_sharedfh_t *fh)
559 {
560 	return (((uintptr_t)fh / sizeof (*fh)) & rtable4mask);
561 }
562 
563 /*
564  * Find or create the vnode for the given filehandle and filesystem.
565  * *newnode is set to zero if the vnode already existed; non-zero if it had
566  * to be created.
567  *
568  * Note: make_rnode4() may upgrade the hash bucket lock to exclusive.
569  */
570 
571 static vnode_t *
572 make_rnode4(nfs4_sharedfh_t *fh, r4hashq_t *rhtp, struct vfs *vfsp,
573     struct vnodeops *vops,
574     int (*putapage)(vnode_t *, page_t *, u_offset_t *, size_t *, int, cred_t *),
575     int *newnode, cred_t *cr)
576 {
577 	rnode4_t *rp;
578 	rnode4_t *trp;
579 	vnode_t *vp;
580 	mntinfo4_t *mi;
581 
582 	ASSERT(RW_READ_HELD(&rhtp->r_lock));
583 
584 	mi = VFTOMI4(vfsp);
585 
586 start:
587 	if ((rp = r4find(rhtp, fh, vfsp)) != NULL) {
588 		vp = RTOV4(rp);
589 		*newnode = 0;
590 		return (vp);
591 	}
592 	rw_exit(&rhtp->r_lock);
593 
594 	mutex_enter(&rp4freelist_lock);
595 
596 	if (rp4freelist != NULL && rnode4_new >= nrnode) {
597 		rp = rp4freelist;
598 		rp4_rmfree(rp);
599 		mutex_exit(&rp4freelist_lock);
600 
601 		vp = RTOV4(rp);
602 
603 		if (rp->r_flags & R4HASHED) {
604 			rw_enter(&rp->r_hashq->r_lock, RW_WRITER);
605 			mutex_enter(&vp->v_lock);
606 			if (vp->v_count > 1) {
607 				VN_RELE_LOCKED(vp);
608 				mutex_exit(&vp->v_lock);
609 				rw_exit(&rp->r_hashq->r_lock);
610 				rw_enter(&rhtp->r_lock, RW_READER);
611 				goto start;
612 			}
613 			mutex_exit(&vp->v_lock);
614 			rp4_rmhash_locked(rp);
615 			rw_exit(&rp->r_hashq->r_lock);
616 		}
617 
618 		r4inactive(rp, cr);
619 
620 		mutex_enter(&vp->v_lock);
621 		if (vp->v_count > 1) {
622 			VN_RELE_LOCKED(vp);
623 			mutex_exit(&vp->v_lock);
624 			rw_enter(&rhtp->r_lock, RW_READER);
625 			goto start;
626 		}
627 		mutex_exit(&vp->v_lock);
628 		vn_invalid(vp);
629 
630 		/*
631 		 * destroy old locks before bzero'ing and
632 		 * recreating the locks below.
633 		 */
634 		uninit_rnode4(rp);
635 
636 		/*
637 		 * Make sure that if rnode is recycled then
638 		 * VFS count is decremented properly before
639 		 * reuse.
640 		 */
641 		VFS_RELE(vp->v_vfsp);
642 		vn_reinit(vp);
643 	} else {
644 		vnode_t *new_vp;
645 
646 		mutex_exit(&rp4freelist_lock);
647 
648 		rp = kmem_cache_alloc(rnode4_cache, KM_SLEEP);
649 		new_vp = vn_alloc(KM_SLEEP);
650 
651 		atomic_inc_ulong((ulong_t *)&rnode4_new);
652 #ifdef DEBUG
653 		clstat4_debug.nrnode.value.ui64++;
654 #endif
655 		vp = new_vp;
656 	}
657 
658 	bzero(rp, sizeof (*rp));
659 	rp->r_vnode = vp;
660 	nfs_rw_init(&rp->r_rwlock, NULL, RW_DEFAULT, NULL);
661 	nfs_rw_init(&rp->r_lkserlock, NULL, RW_DEFAULT, NULL);
662 	mutex_init(&rp->r_svlock, NULL, MUTEX_DEFAULT, NULL);
663 	mutex_init(&rp->r_statelock, NULL, MUTEX_DEFAULT, NULL);
664 	mutex_init(&rp->r_statev4_lock, NULL, MUTEX_DEFAULT, NULL);
665 	mutex_init(&rp->r_os_lock, NULL, MUTEX_DEFAULT, NULL);
666 	rp->created_v4 = 0;
667 	list_create(&rp->r_open_streams, sizeof (nfs4_open_stream_t),
668 	    offsetof(nfs4_open_stream_t, os_node));
669 	rp->r_lo_head.lo_prev_rnode = &rp->r_lo_head;
670 	rp->r_lo_head.lo_next_rnode = &rp->r_lo_head;
671 	cv_init(&rp->r_cv, NULL, CV_DEFAULT, NULL);
672 	cv_init(&rp->r_commit.c_cv, NULL, CV_DEFAULT, NULL);
673 	rp->r_flags = R4READDIRWATTR;
674 	rp->r_fh = fh;
675 	rp->r_hashq = rhtp;
676 	sfh4_hold(rp->r_fh);
677 	rp->r_server = mi->mi_curr_serv;
678 	rp->r_deleg_type = OPEN_DELEGATE_NONE;
679 	rp->r_deleg_needs_recovery = OPEN_DELEGATE_NONE;
680 	nfs_rw_init(&rp->r_deleg_recall_lock, NULL, RW_DEFAULT, NULL);
681 
682 	rddir4_cache_create(rp);
683 	rp->r_putapage = putapage;
684 	vn_setops(vp, vops);
685 	vp->v_data = (caddr_t)rp;
686 	vp->v_vfsp = vfsp;
687 	VFS_HOLD(vfsp);
688 	vp->v_type = VNON;
689 	vp->v_flag |= VMODSORT;
690 	if (isrootfh(fh, rp))
691 		vp->v_flag = VROOT;
692 	vn_exists(vp);
693 
694 	/*
695 	 * There is a race condition if someone else
696 	 * alloc's the rnode while no locks are held, so we
697 	 * check again and recover if found.
698 	 */
699 	rw_enter(&rhtp->r_lock, RW_WRITER);
700 	if ((trp = r4find(rhtp, fh, vfsp)) != NULL) {
701 		vp = RTOV4(trp);
702 		*newnode = 0;
703 		rw_exit(&rhtp->r_lock);
704 		rp4_addfree(rp, cr);
705 		rw_enter(&rhtp->r_lock, RW_READER);
706 		return (vp);
707 	}
708 	rp4_addhash(rp);
709 	*newnode = 1;
710 	return (vp);
711 }
712 
713 static void
714 uninit_rnode4(rnode4_t *rp)
715 {
716 	vnode_t *vp = RTOV4(rp);
717 
718 	ASSERT(rp != NULL);
719 	ASSERT(vp != NULL);
720 	ASSERT(vp->v_count == 1);
721 	ASSERT(rp->r_count == 0);
722 	ASSERT(rp->r_mapcnt == 0);
723 	if (rp->r_flags & R4LODANGLERS) {
724 		nfs4_flush_lock_owners(rp);
725 	}
726 	ASSERT(rp->r_lo_head.lo_next_rnode == &rp->r_lo_head);
727 	ASSERT(rp->r_lo_head.lo_prev_rnode == &rp->r_lo_head);
728 	ASSERT(!(rp->r_flags & R4HASHED));
729 	ASSERT(rp->r_freef == NULL && rp->r_freeb == NULL);
730 	nfs4_clear_open_streams(rp);
731 	list_destroy(&rp->r_open_streams);
732 
733 	/*
734 	 * Destroy the rddir cache first since we need to grab the r_statelock.
735 	 */
736 	mutex_enter(&rp->r_statelock);
737 	rddir4_cache_destroy(rp);
738 	mutex_exit(&rp->r_statelock);
739 	sv_uninit(&rp->r_svnode);
740 	sfh4_rele(&rp->r_fh);
741 	nfs_rw_destroy(&rp->r_rwlock);
742 	nfs_rw_destroy(&rp->r_lkserlock);
743 	mutex_destroy(&rp->r_statelock);
744 	mutex_destroy(&rp->r_statev4_lock);
745 	mutex_destroy(&rp->r_os_lock);
746 	cv_destroy(&rp->r_cv);
747 	cv_destroy(&rp->r_commit.c_cv);
748 	nfs_rw_destroy(&rp->r_deleg_recall_lock);
749 	if (rp->r_flags & R4DELMAPLIST)
750 		list_destroy(&rp->r_indelmap);
751 }
752 
753 /*
754  * Put an rnode on the free list.
755  *
756  * Rnodes which were allocated above and beyond the normal limit
757  * are immediately freed.
758  */
759 void
760 rp4_addfree(rnode4_t *rp, cred_t *cr)
761 {
762 	vnode_t *vp;
763 	vnode_t *xattr;
764 	struct vfs *vfsp;
765 
766 	vp = RTOV4(rp);
767 	ASSERT(vp->v_count >= 1);
768 	ASSERT(rp->r_freef == NULL && rp->r_freeb == NULL);
769 
770 	/*
771 	 * If we have too many rnodes allocated and there are no
772 	 * references to this rnode, or if the rnode is no longer
773 	 * accessible by it does not reside in the hash queues,
774 	 * or if an i/o error occurred while writing to the file,
775 	 * then just free it instead of putting it on the rnode
776 	 * freelist.
777 	 */
778 	vfsp = vp->v_vfsp;
779 	if (((rnode4_new > nrnode || !(rp->r_flags & R4HASHED) ||
780 #ifdef DEBUG
781 	    (nfs4_rnode_nofreelist != 0) ||
782 #endif
783 	    rp->r_error || (rp->r_flags & R4RECOVERR) ||
784 	    (vfsp->vfs_flag & VFS_UNMOUNTED)) && rp->r_count == 0)) {
785 		if (rp->r_flags & R4HASHED) {
786 			rw_enter(&rp->r_hashq->r_lock, RW_WRITER);
787 			mutex_enter(&vp->v_lock);
788 			if (vp->v_count > 1) {
789 				VN_RELE_LOCKED(vp);
790 				mutex_exit(&vp->v_lock);
791 				rw_exit(&rp->r_hashq->r_lock);
792 				return;
793 			}
794 			mutex_exit(&vp->v_lock);
795 			rp4_rmhash_locked(rp);
796 			rw_exit(&rp->r_hashq->r_lock);
797 		}
798 
799 		/*
800 		 * Make sure we don't have a delegation on this rnode
801 		 * before destroying it.
802 		 */
803 		if (rp->r_deleg_type != OPEN_DELEGATE_NONE) {
804 			(void) nfs4delegreturn(rp,
805 			    NFS4_DR_FORCE|NFS4_DR_PUSH|NFS4_DR_REOPEN);
806 		}
807 
808 		r4inactive(rp, cr);
809 
810 		/*
811 		 * Recheck the vnode reference count.  We need to
812 		 * make sure that another reference has not been
813 		 * acquired while we were not holding v_lock.  The
814 		 * rnode is not in the rnode hash queues; one
815 		 * way for a reference to have been acquired
816 		 * is for a VOP_PUTPAGE because the rnode was marked
817 		 * with R4DIRTY or for a modified page.  This
818 		 * reference may have been acquired before our call
819 		 * to r4inactive.  The i/o may have been completed,
820 		 * thus allowing r4inactive to complete, but the
821 		 * reference to the vnode may not have been released
822 		 * yet.  In any case, the rnode can not be destroyed
823 		 * until the other references to this vnode have been
824 		 * released.  The other references will take care of
825 		 * either destroying the rnode or placing it on the
826 		 * rnode freelist.  If there are no other references,
827 		 * then the rnode may be safely destroyed.
828 		 */
829 		mutex_enter(&vp->v_lock);
830 		if (vp->v_count > 1) {
831 			VN_RELE_LOCKED(vp);
832 			mutex_exit(&vp->v_lock);
833 			return;
834 		}
835 		mutex_exit(&vp->v_lock);
836 
837 		destroy_rnode4(rp);
838 		return;
839 	}
840 
841 	/*
842 	 * Lock the hash queue and then recheck the reference count
843 	 * to ensure that no other threads have acquired a reference
844 	 * to indicate that the rnode should not be placed on the
845 	 * freelist.  If another reference has been acquired, then
846 	 * just release this one and let the other thread complete
847 	 * the processing of adding this rnode to the freelist.
848 	 */
849 again:
850 	rw_enter(&rp->r_hashq->r_lock, RW_WRITER);
851 
852 	mutex_enter(&vp->v_lock);
853 	if (vp->v_count > 1) {
854 		VN_RELE_LOCKED(vp);
855 		mutex_exit(&vp->v_lock);
856 		rw_exit(&rp->r_hashq->r_lock);
857 		return;
858 	}
859 	mutex_exit(&vp->v_lock);
860 
861 	/*
862 	 * Make sure we don't put an rnode with a delegation
863 	 * on the free list.
864 	 */
865 	if (rp->r_deleg_type != OPEN_DELEGATE_NONE) {
866 		rw_exit(&rp->r_hashq->r_lock);
867 		(void) nfs4delegreturn(rp,
868 		    NFS4_DR_FORCE|NFS4_DR_PUSH|NFS4_DR_REOPEN);
869 		goto again;
870 	}
871 
872 	/*
873 	 * Now that we have the hash queue lock, and we know there
874 	 * are not anymore references on the vnode, check to make
875 	 * sure there aren't any open streams still on the rnode.
876 	 * If so, drop the hash queue lock, remove the open streams,
877 	 * and recheck the v_count.
878 	 */
879 	mutex_enter(&rp->r_os_lock);
880 	if (list_head(&rp->r_open_streams) != NULL) {
881 		mutex_exit(&rp->r_os_lock);
882 		rw_exit(&rp->r_hashq->r_lock);
883 		if (nfs_zone() != VTOMI4(vp)->mi_zone)
884 			nfs4_clear_open_streams(rp);
885 		else
886 			(void) nfs4close_all(vp, cr);
887 		goto again;
888 	}
889 	mutex_exit(&rp->r_os_lock);
890 
891 	/*
892 	 * Before we put it on the freelist, make sure there are no pages.
893 	 * If there are, flush and commit of all of the dirty and
894 	 * uncommitted pages, assuming the file system isn't read only.
895 	 */
896 	if (!(vp->v_vfsp->vfs_flag & VFS_RDONLY) && nfs4_dross_pages(vp)) {
897 		rw_exit(&rp->r_hashq->r_lock);
898 		r4flushpages(rp, cr);
899 		goto again;
900 	}
901 
902 	/*
903 	 * Before we put it on the freelist, make sure there is no
904 	 * active xattr directory cached, the freelist will not
905 	 * have its entries r4inactive'd if there is still an active
906 	 * rnode, thus nothing in the freelist can hold another
907 	 * rnode active.
908 	 */
909 	xattr = rp->r_xattr_dir;
910 	rp->r_xattr_dir = NULL;
911 
912 	/*
913 	 * If there is no cached data or metadata for this file, then
914 	 * put the rnode on the front of the freelist so that it will
915 	 * be reused before other rnodes which may have cached data or
916 	 * metadata associated with them.
917 	 */
918 	mutex_enter(&rp4freelist_lock);
919 	if (rp4freelist == NULL) {
920 		rp->r_freef = rp;
921 		rp->r_freeb = rp;
922 		rp4freelist = rp;
923 	} else {
924 		rp->r_freef = rp4freelist;
925 		rp->r_freeb = rp4freelist->r_freeb;
926 		rp4freelist->r_freeb->r_freef = rp;
927 		rp4freelist->r_freeb = rp;
928 		if (!nfs4_has_pages(vp) && rp->r_dir == NULL &&
929 		    rp->r_symlink.contents == NULL && rp->r_secattr == NULL)
930 			rp4freelist = rp;
931 	}
932 	mutex_exit(&rp4freelist_lock);
933 
934 	rw_exit(&rp->r_hashq->r_lock);
935 
936 	if (xattr)
937 		VN_RELE(xattr);
938 }
939 
940 /*
941  * Remove an rnode from the free list.
942  *
943  * The caller must be holding rp4freelist_lock and the rnode
944  * must be on the freelist.
945  */
946 static void
947 rp4_rmfree(rnode4_t *rp)
948 {
949 
950 	ASSERT(MUTEX_HELD(&rp4freelist_lock));
951 	ASSERT(rp->r_freef != NULL && rp->r_freeb != NULL);
952 
953 	if (rp == rp4freelist) {
954 		rp4freelist = rp->r_freef;
955 		if (rp == rp4freelist)
956 			rp4freelist = NULL;
957 	}
958 	rp->r_freeb->r_freef = rp->r_freef;
959 	rp->r_freef->r_freeb = rp->r_freeb;
960 
961 	rp->r_freef = rp->r_freeb = NULL;
962 }
963 
964 /*
965  * Put a rnode in the hash table.
966  *
967  * The caller must be holding the exclusive hash queue lock
968  */
969 void
970 rp4_addhash(rnode4_t *rp)
971 {
972 	ASSERT(RW_WRITE_HELD(&rp->r_hashq->r_lock));
973 	ASSERT(!(rp->r_flags & R4HASHED));
974 
975 #ifdef DEBUG
976 	r4_dup_check(rp, RTOV4(rp)->v_vfsp);
977 #endif
978 
979 	rp->r_hashf = rp->r_hashq->r_hashf;
980 	rp->r_hashq->r_hashf = rp;
981 	rp->r_hashb = (rnode4_t *)rp->r_hashq;
982 	rp->r_hashf->r_hashb = rp;
983 
984 	mutex_enter(&rp->r_statelock);
985 	rp->r_flags |= R4HASHED;
986 	mutex_exit(&rp->r_statelock);
987 }
988 
989 /*
990  * Remove a rnode from the hash table.
991  *
992  * The caller must be holding the hash queue lock.
993  */
994 void
995 rp4_rmhash_locked(rnode4_t *rp)
996 {
997 	ASSERT(RW_WRITE_HELD(&rp->r_hashq->r_lock));
998 	ASSERT(rp->r_flags & R4HASHED);
999 
1000 	rp->r_hashb->r_hashf = rp->r_hashf;
1001 	rp->r_hashf->r_hashb = rp->r_hashb;
1002 
1003 	mutex_enter(&rp->r_statelock);
1004 	rp->r_flags &= ~R4HASHED;
1005 	mutex_exit(&rp->r_statelock);
1006 }
1007 
1008 /*
1009  * Remove a rnode from the hash table.
1010  *
1011  * The caller must not be holding the hash queue lock.
1012  */
1013 void
1014 rp4_rmhash(rnode4_t *rp)
1015 {
1016 	rw_enter(&rp->r_hashq->r_lock, RW_WRITER);
1017 	rp4_rmhash_locked(rp);
1018 	rw_exit(&rp->r_hashq->r_lock);
1019 }
1020 
1021 /*
1022  * Lookup a rnode by fhandle.  Ignores rnodes that had failed recovery.
1023  * Returns NULL if no match.  If an rnode is returned, the reference count
1024  * on the master vnode is incremented.
1025  *
1026  * The caller must be holding the hash queue lock, either shared or exclusive.
1027  */
1028 rnode4_t *
1029 r4find(r4hashq_t *rhtp, nfs4_sharedfh_t *fh, struct vfs *vfsp)
1030 {
1031 	rnode4_t *rp;
1032 	vnode_t *vp;
1033 
1034 	ASSERT(RW_LOCK_HELD(&rhtp->r_lock));
1035 
1036 	for (rp = rhtp->r_hashf; rp != (rnode4_t *)rhtp; rp = rp->r_hashf) {
1037 		vp = RTOV4(rp);
1038 		if (vp->v_vfsp == vfsp && SFH4_SAME(rp->r_fh, fh)) {
1039 
1040 			mutex_enter(&rp->r_statelock);
1041 			if (rp->r_flags & R4RECOVERR) {
1042 				mutex_exit(&rp->r_statelock);
1043 				continue;
1044 			}
1045 			mutex_exit(&rp->r_statelock);
1046 #ifdef DEBUG
1047 			r4_dup_check(rp, vfsp);
1048 #endif
1049 			if (rp->r_freef != NULL) {
1050 				mutex_enter(&rp4freelist_lock);
1051 				/*
1052 				 * If the rnode is on the freelist,
1053 				 * then remove it and use that reference
1054 				 * as the new reference.  Otherwise,
1055 				 * need to increment the reference count.
1056 				 */
1057 				if (rp->r_freef != NULL) {
1058 					rp4_rmfree(rp);
1059 					mutex_exit(&rp4freelist_lock);
1060 				} else {
1061 					mutex_exit(&rp4freelist_lock);
1062 					VN_HOLD(vp);
1063 				}
1064 			} else
1065 				VN_HOLD(vp);
1066 
1067 			/*
1068 			 * if root vnode, set v_flag to indicate that
1069 			 */
1070 			if (isrootfh(fh, rp)) {
1071 				if (!(vp->v_flag & VROOT)) {
1072 					mutex_enter(&vp->v_lock);
1073 					vp->v_flag |= VROOT;
1074 					mutex_exit(&vp->v_lock);
1075 				}
1076 			}
1077 			return (rp);
1078 		}
1079 	}
1080 	return (NULL);
1081 }
1082 
1083 /*
1084  * Lookup an rnode by fhandle. Just a wrapper for r4find()
1085  * that assumes the caller hasn't already got the lock
1086  * on the hash bucket.
1087  */
1088 rnode4_t *
1089 r4find_unlocked(nfs4_sharedfh_t *fh, struct vfs *vfsp)
1090 {
1091 	rnode4_t *rp;
1092 	int index;
1093 
1094 	index = rtable4hash(fh);
1095 	rw_enter(&rtable4[index].r_lock, RW_READER);
1096 	rp = r4find(&rtable4[index], fh, vfsp);
1097 	rw_exit(&rtable4[index].r_lock);
1098 
1099 	return (rp);
1100 }
1101 
1102 /*
1103  * Return >0 if there is a active vnode belonging to this vfs in the
1104  * rtable4 cache.
1105  *
1106  * Several of these checks are done without holding the usual
1107  * locks.  This is safe because destroy_rtable(), rp_addfree(),
1108  * etc. will redo the necessary checks before actually destroying
1109  * any rnodes.
1110  */
1111 int
1112 check_rtable4(struct vfs *vfsp)
1113 {
1114 	rnode4_t *rp;
1115 	vnode_t *vp;
1116 	int busy = NFSV4_RTABLE4_OK;
1117 	int index;
1118 
1119 	for (index = 0; index < rtable4size; index++) {
1120 		rw_enter(&rtable4[index].r_lock, RW_READER);
1121 
1122 		for (rp = rtable4[index].r_hashf;
1123 		    rp != (rnode4_t *)(&rtable4[index]);
1124 		    rp = rp->r_hashf) {
1125 
1126 			vp = RTOV4(rp);
1127 			if (vp->v_vfsp == vfsp) {
1128 				if (rp->r_freef == NULL) {
1129 					busy = NFSV4_RTABLE4_NOT_FREE_LIST;
1130 				} else if (nfs4_has_pages(vp) &&
1131 				    (rp->r_flags & R4DIRTY)) {
1132 					busy = NFSV4_RTABLE4_DIRTY_PAGES;
1133 				} else if (rp->r_count > 0) {
1134 					busy = NFSV4_RTABLE4_POS_R_COUNT;
1135 				}
1136 
1137 				if (busy != NFSV4_RTABLE4_OK) {
1138 #ifdef DEBUG
1139 					char *path;
1140 
1141 					path = fn_path(rp->r_svnode.sv_name);
1142 					DTRACE_NFSV4_3(rnode__e__debug,
1143 					    int, busy, char *, path,
1144 					    rnode4_t *, rp);
1145 					kmem_free(path, strlen(path)+1);
1146 #endif
1147 					rw_exit(&rtable4[index].r_lock);
1148 					return (busy);
1149 				}
1150 			}
1151 		}
1152 		rw_exit(&rtable4[index].r_lock);
1153 	}
1154 	return (busy);
1155 }
1156 
1157 /*
1158  * Destroy inactive vnodes from the hash queues which
1159  * belong to this vfs. All of the vnodes should be inactive.
1160  * It is essential that we destroy all rnodes in case of
1161  * forced unmount as well as in normal unmount case.
1162  */
1163 
1164 void
1165 destroy_rtable4(struct vfs *vfsp, cred_t *cr)
1166 {
1167 	int index;
1168 	vnode_t *vp;
1169 	rnode4_t *rp, *r_hashf, *rlist;
1170 
1171 	rlist = NULL;
1172 
1173 	for (index = 0; index < rtable4size; index++) {
1174 		rw_enter(&rtable4[index].r_lock, RW_WRITER);
1175 		for (rp = rtable4[index].r_hashf;
1176 		    rp != (rnode4_t *)(&rtable4[index]);
1177 		    rp = r_hashf) {
1178 			/* save the hash pointer before destroying */
1179 			r_hashf = rp->r_hashf;
1180 
1181 			vp = RTOV4(rp);
1182 			if (vp->v_vfsp == vfsp) {
1183 				mutex_enter(&rp4freelist_lock);
1184 				if (rp->r_freef != NULL) {
1185 					rp4_rmfree(rp);
1186 					mutex_exit(&rp4freelist_lock);
1187 					rp4_rmhash_locked(rp);
1188 					rp->r_hashf = rlist;
1189 					rlist = rp;
1190 				} else
1191 					mutex_exit(&rp4freelist_lock);
1192 			}
1193 		}
1194 		rw_exit(&rtable4[index].r_lock);
1195 	}
1196 
1197 	for (rp = rlist; rp != NULL; rp = r_hashf) {
1198 		r_hashf = rp->r_hashf;
1199 		/*
1200 		 * This call to rp4_addfree will end up destroying the
1201 		 * rnode, but in a safe way with the appropriate set
1202 		 * of checks done.
1203 		 */
1204 		rp4_addfree(rp, cr);
1205 	}
1206 }
1207 
1208 /*
1209  * This routine destroys all the resources of an rnode
1210  * and finally the rnode itself.
1211  */
1212 static void
1213 destroy_rnode4(rnode4_t *rp)
1214 {
1215 	vnode_t *vp;
1216 	vfs_t *vfsp;
1217 
1218 	ASSERT(rp->r_deleg_type == OPEN_DELEGATE_NONE);
1219 
1220 	vp = RTOV4(rp);
1221 	vfsp = vp->v_vfsp;
1222 
1223 	uninit_rnode4(rp);
1224 	atomic_dec_ulong((ulong_t *)&rnode4_new);
1225 #ifdef DEBUG
1226 	clstat4_debug.nrnode.value.ui64--;
1227 #endif
1228 	kmem_cache_free(rnode4_cache, rp);
1229 	vn_invalid(vp);
1230 	vn_free(vp);
1231 	VFS_RELE(vfsp);
1232 }
1233 
1234 /*
1235  * Invalidate the attributes on all rnodes forcing the next getattr
1236  * to go over the wire.  Used to flush stale uid and gid mappings.
1237  * Maybe done on a per vfsp, or all rnodes (vfsp == NULL)
1238  */
1239 void
1240 nfs4_rnode_invalidate(struct vfs *vfsp)
1241 {
1242 	int index;
1243 	rnode4_t *rp;
1244 	vnode_t *vp;
1245 
1246 	/*
1247 	 * Walk the hash queues looking for rnodes.
1248 	 */
1249 	for (index = 0; index < rtable4size; index++) {
1250 		rw_enter(&rtable4[index].r_lock, RW_READER);
1251 		for (rp = rtable4[index].r_hashf;
1252 		    rp != (rnode4_t *)(&rtable4[index]);
1253 		    rp = rp->r_hashf) {
1254 			vp = RTOV4(rp);
1255 			if (vfsp != NULL && vp->v_vfsp != vfsp)
1256 				continue;
1257 
1258 			if (!mutex_tryenter(&rp->r_statelock))
1259 				continue;
1260 
1261 			/*
1262 			 * Expire the attributes by resetting the change
1263 			 * and attr timeout.
1264 			 */
1265 			rp->r_change = 0;
1266 			PURGE_ATTRCACHE4_LOCKED(rp);
1267 			mutex_exit(&rp->r_statelock);
1268 		}
1269 		rw_exit(&rtable4[index].r_lock);
1270 	}
1271 }
1272 
1273 /*
1274  * Flush all vnodes in this (or every) vfs.
1275  * Used by nfs_sync and by nfs_unmount.
1276  */
1277 void
1278 r4flush(struct vfs *vfsp, cred_t *cr)
1279 {
1280 	int index;
1281 	rnode4_t *rp;
1282 	vnode_t *vp, **vplist;
1283 	long num, cnt;
1284 
1285 	/*
1286 	 * Check to see whether there is anything to do.
1287 	 */
1288 	num = rnode4_new;
1289 	if (num == 0)
1290 		return;
1291 
1292 	/*
1293 	 * Allocate a slot for all currently active rnodes on the
1294 	 * supposition that they all may need flushing.
1295 	 */
1296 	vplist = kmem_alloc(num * sizeof (*vplist), KM_SLEEP);
1297 	cnt = 0;
1298 
1299 	/*
1300 	 * Walk the hash queues looking for rnodes with page
1301 	 * lists associated with them.  Make a list of these
1302 	 * files.
1303 	 */
1304 	for (index = 0; index < rtable4size; index++) {
1305 		rw_enter(&rtable4[index].r_lock, RW_READER);
1306 		for (rp = rtable4[index].r_hashf;
1307 		    rp != (rnode4_t *)(&rtable4[index]);
1308 		    rp = rp->r_hashf) {
1309 			vp = RTOV4(rp);
1310 			/*
1311 			 * Don't bother sync'ing a vp if it
1312 			 * is part of virtual swap device or
1313 			 * if VFS is read-only
1314 			 */
1315 			if (IS_SWAPVP(vp) || vn_is_readonly(vp))
1316 				continue;
1317 			/*
1318 			 * If flushing all mounted file systems or
1319 			 * the vnode belongs to this vfs, has pages
1320 			 * and is marked as either dirty or mmap'd,
1321 			 * hold and add this vnode to the list of
1322 			 * vnodes to flush.
1323 			 */
1324 			if ((vfsp == NULL || vp->v_vfsp == vfsp) &&
1325 			    nfs4_has_pages(vp) &&
1326 			    ((rp->r_flags & R4DIRTY) || rp->r_mapcnt > 0)) {
1327 				VN_HOLD(vp);
1328 				vplist[cnt++] = vp;
1329 				if (cnt == num) {
1330 					rw_exit(&rtable4[index].r_lock);
1331 					goto toomany;
1332 				}
1333 			}
1334 		}
1335 		rw_exit(&rtable4[index].r_lock);
1336 	}
1337 toomany:
1338 
1339 	/*
1340 	 * Flush and release all of the files on the list.
1341 	 */
1342 	while (cnt-- > 0) {
1343 		vp = vplist[cnt];
1344 		(void) VOP_PUTPAGE(vp, (u_offset_t)0, 0, B_ASYNC, cr, NULL);
1345 		VN_RELE(vp);
1346 	}
1347 
1348 	/*
1349 	 * Free the space allocated to hold the list.
1350 	 */
1351 	kmem_free(vplist, num * sizeof (*vplist));
1352 }
1353 
1354 int
1355 nfs4_free_data_reclaim(rnode4_t *rp)
1356 {
1357 	char *contents;
1358 	vnode_t *xattr;
1359 	int size;
1360 	vsecattr_t *vsp;
1361 	int freed;
1362 	bool_t rdc = FALSE;
1363 
1364 	/*
1365 	 * Free any held caches which may
1366 	 * be associated with this rnode.
1367 	 */
1368 	mutex_enter(&rp->r_statelock);
1369 	if (rp->r_dir != NULL)
1370 		rdc = TRUE;
1371 	contents = rp->r_symlink.contents;
1372 	size = rp->r_symlink.size;
1373 	rp->r_symlink.contents = NULL;
1374 	vsp = rp->r_secattr;
1375 	rp->r_secattr = NULL;
1376 	xattr = rp->r_xattr_dir;
1377 	rp->r_xattr_dir = NULL;
1378 	mutex_exit(&rp->r_statelock);
1379 
1380 	/*
1381 	 * Free the access cache entries.
1382 	 */
1383 	freed = nfs4_access_purge_rp(rp);
1384 
1385 	if (rdc == FALSE && contents == NULL && vsp == NULL && xattr == NULL)
1386 		return (freed);
1387 
1388 	/*
1389 	 * Free the readdir cache entries, incompletely if we can't block.
1390 	 */
1391 	nfs4_purge_rddir_cache(RTOV4(rp));
1392 
1393 	/*
1394 	 * Free the symbolic link cache.
1395 	 */
1396 	if (contents != NULL) {
1397 
1398 		kmem_free((void *)contents, size);
1399 	}
1400 
1401 	/*
1402 	 * Free any cached ACL.
1403 	 */
1404 	if (vsp != NULL)
1405 		nfs4_acl_free_cache(vsp);
1406 
1407 	/*
1408 	 * Release the xattr directory vnode
1409 	 */
1410 	if (xattr != NULL)
1411 		VN_RELE(xattr);
1412 
1413 	return (1);
1414 }
1415 
1416 static int
1417 nfs4_active_data_reclaim(rnode4_t *rp)
1418 {
1419 	char *contents;
1420 	vnode_t *xattr = NULL;
1421 	int size;
1422 	vsecattr_t *vsp;
1423 	int freed;
1424 	bool_t rdc = FALSE;
1425 
1426 	/*
1427 	 * Free any held credentials and caches which
1428 	 * may be associated with this rnode.
1429 	 */
1430 	if (!mutex_tryenter(&rp->r_statelock))
1431 		return (0);
1432 	contents = rp->r_symlink.contents;
1433 	size = rp->r_symlink.size;
1434 	rp->r_symlink.contents = NULL;
1435 	vsp = rp->r_secattr;
1436 	rp->r_secattr = NULL;
1437 	if (rp->r_dir != NULL)
1438 		rdc = TRUE;
1439 	/*
1440 	 * To avoid a deadlock, do not free r_xattr_dir cache if it is hashed
1441 	 * on the same r_hashq queue. We are not mandated to free all caches.
1442 	 * VN_RELE(rp->r_xattr_dir) will be done sometime later - e.g. when the
1443 	 * rnode 'rp' is freed or put on the free list.
1444 	 *
1445 	 * We will retain NFS4_XATTR_DIR_NOTSUPP because:
1446 	 * - it has no associated rnode4_t (its v_data is NULL),
1447 	 * - it is preallocated statically and will never go away,
1448 	 * so we cannot save anything by releasing it.
1449 	 */
1450 	if (rp->r_xattr_dir && rp->r_xattr_dir != NFS4_XATTR_DIR_NOTSUPP &&
1451 	    VTOR4(rp->r_xattr_dir)->r_hashq != rp->r_hashq) {
1452 		xattr = rp->r_xattr_dir;
1453 		rp->r_xattr_dir = NULL;
1454 	}
1455 	mutex_exit(&rp->r_statelock);
1456 
1457 	/*
1458 	 * Free the access cache entries.
1459 	 */
1460 	freed = nfs4_access_purge_rp(rp);
1461 
1462 	if (contents == NULL && vsp == NULL && rdc == FALSE && xattr == NULL)
1463 		return (freed);
1464 
1465 	/*
1466 	 * Free the symbolic link cache.
1467 	 */
1468 	if (contents != NULL) {
1469 
1470 		kmem_free((void *)contents, size);
1471 	}
1472 
1473 	/*
1474 	 * Free any cached ACL.
1475 	 */
1476 	if (vsp != NULL)
1477 		nfs4_acl_free_cache(vsp);
1478 
1479 	nfs4_purge_rddir_cache(RTOV4(rp));
1480 
1481 	/*
1482 	 * Release the xattr directory vnode
1483 	 */
1484 	if (xattr != NULL)
1485 		VN_RELE(xattr);
1486 
1487 	return (1);
1488 }
1489 
1490 static int
1491 nfs4_free_reclaim(void)
1492 {
1493 	int freed;
1494 	rnode4_t *rp;
1495 
1496 #ifdef DEBUG
1497 	clstat4_debug.f_reclaim.value.ui64++;
1498 #endif
1499 	freed = 0;
1500 	mutex_enter(&rp4freelist_lock);
1501 	rp = rp4freelist;
1502 	if (rp != NULL) {
1503 		do {
1504 			if (nfs4_free_data_reclaim(rp))
1505 				freed = 1;
1506 		} while ((rp = rp->r_freef) != rp4freelist);
1507 	}
1508 	mutex_exit(&rp4freelist_lock);
1509 	return (freed);
1510 }
1511 
1512 static int
1513 nfs4_active_reclaim(void)
1514 {
1515 	int freed;
1516 	int index;
1517 	rnode4_t *rp;
1518 
1519 #ifdef DEBUG
1520 	clstat4_debug.a_reclaim.value.ui64++;
1521 #endif
1522 	freed = 0;
1523 	for (index = 0; index < rtable4size; index++) {
1524 		rw_enter(&rtable4[index].r_lock, RW_READER);
1525 		for (rp = rtable4[index].r_hashf;
1526 		    rp != (rnode4_t *)(&rtable4[index]);
1527 		    rp = rp->r_hashf) {
1528 			if (nfs4_active_data_reclaim(rp))
1529 				freed = 1;
1530 		}
1531 		rw_exit(&rtable4[index].r_lock);
1532 	}
1533 	return (freed);
1534 }
1535 
1536 static int
1537 nfs4_rnode_reclaim(void)
1538 {
1539 	int freed;
1540 	rnode4_t *rp;
1541 	vnode_t *vp;
1542 
1543 #ifdef DEBUG
1544 	clstat4_debug.r_reclaim.value.ui64++;
1545 #endif
1546 	freed = 0;
1547 	mutex_enter(&rp4freelist_lock);
1548 	while ((rp = rp4freelist) != NULL) {
1549 		rp4_rmfree(rp);
1550 		mutex_exit(&rp4freelist_lock);
1551 		if (rp->r_flags & R4HASHED) {
1552 			vp = RTOV4(rp);
1553 			rw_enter(&rp->r_hashq->r_lock, RW_WRITER);
1554 			mutex_enter(&vp->v_lock);
1555 			if (vp->v_count > 1) {
1556 				VN_RELE_LOCKED(vp);
1557 				mutex_exit(&vp->v_lock);
1558 				rw_exit(&rp->r_hashq->r_lock);
1559 				mutex_enter(&rp4freelist_lock);
1560 				continue;
1561 			}
1562 			mutex_exit(&vp->v_lock);
1563 			rp4_rmhash_locked(rp);
1564 			rw_exit(&rp->r_hashq->r_lock);
1565 		}
1566 		/*
1567 		 * This call to rp_addfree will end up destroying the
1568 		 * rnode, but in a safe way with the appropriate set
1569 		 * of checks done.
1570 		 */
1571 		rp4_addfree(rp, CRED());
1572 		mutex_enter(&rp4freelist_lock);
1573 	}
1574 	mutex_exit(&rp4freelist_lock);
1575 	return (freed);
1576 }
1577 
1578 /*ARGSUSED*/
1579 static void
1580 nfs4_reclaim(void *cdrarg)
1581 {
1582 #ifdef DEBUG
1583 	clstat4_debug.reclaim.value.ui64++;
1584 #endif
1585 	if (nfs4_free_reclaim())
1586 		return;
1587 
1588 	if (nfs4_active_reclaim())
1589 		return;
1590 
1591 	(void) nfs4_rnode_reclaim();
1592 }
1593 
1594 /*
1595  * Returns the clientid4 to use for the given mntinfo4.  Note that the
1596  * clientid can change if the caller drops mi_recovlock.
1597  */
1598 
1599 clientid4
1600 mi2clientid(mntinfo4_t *mi)
1601 {
1602 	nfs4_server_t	*sp;
1603 	clientid4	clientid = 0;
1604 
1605 	/* this locks down sp if it is found */
1606 	sp = find_nfs4_server(mi);
1607 	if (sp != NULL) {
1608 		clientid = sp->clientid;
1609 		mutex_exit(&sp->s_lock);
1610 		nfs4_server_rele(sp);
1611 	}
1612 	return (clientid);
1613 }
1614 
1615 /*
1616  * Return the current lease time for the server associated with the given
1617  * file.  Note that the lease time could change immediately after this
1618  * call.
1619  */
1620 
1621 time_t
1622 r2lease_time(rnode4_t *rp)
1623 {
1624 	nfs4_server_t	*sp;
1625 	time_t		lease_time;
1626 	mntinfo4_t	*mi = VTOMI4(RTOV4(rp));
1627 
1628 	(void) nfs_rw_enter_sig(&mi->mi_recovlock, RW_READER, 0);
1629 
1630 	/* this locks down sp if it is found */
1631 	sp = find_nfs4_server(VTOMI4(RTOV4(rp)));
1632 
1633 	if (VTOMI4(RTOV4(rp))->mi_vfsp->vfs_flag & VFS_UNMOUNTED) {
1634 		if (sp != NULL) {
1635 			mutex_exit(&sp->s_lock);
1636 			nfs4_server_rele(sp);
1637 		}
1638 		nfs_rw_exit(&mi->mi_recovlock);
1639 		return (1);		/* 1 second */
1640 	}
1641 
1642 	ASSERT(sp != NULL);
1643 
1644 	lease_time = sp->s_lease_time;
1645 
1646 	mutex_exit(&sp->s_lock);
1647 	nfs4_server_rele(sp);
1648 	nfs_rw_exit(&mi->mi_recovlock);
1649 
1650 	return (lease_time);
1651 }
1652 
1653 /*
1654  * Return a list with information about all the known open instances for
1655  * a filesystem. The caller must call r4releopenlist() when done with the
1656  * list.
1657  *
1658  * We are safe at looking at os_valid and os_pending_close across dropping
1659  * the 'os_sync_lock' to count up the number of open streams and then
1660  * allocate memory for the osp list due to:
1661  *	-Looking at os_pending_close is safe since this routine is
1662  *	only called via recovery, and os_pending_close can only be set via
1663  *	a non-recovery operation (which are all blocked when recovery
1664  *	is active).
1665  *
1666  *	-Examining os_valid is safe since non-recovery operations, which
1667  *	could potentially switch os_valid to 0, are blocked (via
1668  *	nfs4_start_fop) and recovery is single-threaded per mntinfo4_t
1669  *	(which means we are the only recovery thread potentially acting
1670  *	on this open stream).
1671  */
1672 
1673 nfs4_opinst_t *
1674 r4mkopenlist(mntinfo4_t *mi)
1675 {
1676 	nfs4_opinst_t *reopenlist, *rep;
1677 	rnode4_t *rp;
1678 	vnode_t *vp;
1679 	vfs_t *vfsp = mi->mi_vfsp;
1680 	int numosp;
1681 	nfs4_open_stream_t *osp;
1682 	int index;
1683 	open_delegation_type4 dtype;
1684 	int hold_vnode;
1685 
1686 	reopenlist = NULL;
1687 
1688 	for (index = 0; index < rtable4size; index++) {
1689 		rw_enter(&rtable4[index].r_lock, RW_READER);
1690 		for (rp = rtable4[index].r_hashf;
1691 		    rp != (rnode4_t *)(&rtable4[index]);
1692 		    rp = rp->r_hashf) {
1693 
1694 			vp = RTOV4(rp);
1695 			if (vp->v_vfsp != vfsp)
1696 				continue;
1697 			hold_vnode = 0;
1698 
1699 			mutex_enter(&rp->r_os_lock);
1700 
1701 			/* Count the number of valid open_streams of the file */
1702 			numosp = 0;
1703 			for (osp = list_head(&rp->r_open_streams); osp != NULL;
1704 			    osp = list_next(&rp->r_open_streams, osp)) {
1705 				mutex_enter(&osp->os_sync_lock);
1706 				if (osp->os_valid && !osp->os_pending_close)
1707 					numosp++;
1708 				mutex_exit(&osp->os_sync_lock);
1709 			}
1710 
1711 			/* Fill in the valid open streams per vp */
1712 			if (numosp > 0) {
1713 				int j;
1714 
1715 				hold_vnode = 1;
1716 
1717 				/*
1718 				 * Add a new open instance to the list
1719 				 */
1720 				rep = kmem_zalloc(sizeof (*reopenlist),
1721 				    KM_SLEEP);
1722 				rep->re_next = reopenlist;
1723 				reopenlist = rep;
1724 
1725 				rep->re_vp = vp;
1726 				rep->re_osp = kmem_zalloc(
1727 				    numosp * sizeof (*(rep->re_osp)),
1728 				    KM_SLEEP);
1729 				rep->re_numosp = numosp;
1730 
1731 				j = 0;
1732 				for (osp = list_head(&rp->r_open_streams);
1733 				    osp != NULL;
1734 				    osp = list_next(&rp->r_open_streams, osp)) {
1735 
1736 					mutex_enter(&osp->os_sync_lock);
1737 					if (osp->os_valid &&
1738 					    !osp->os_pending_close) {
1739 						osp->os_ref_count++;
1740 						rep->re_osp[j] = osp;
1741 						j++;
1742 					}
1743 					mutex_exit(&osp->os_sync_lock);
1744 				}
1745 				/*
1746 				 * Assuming valid osp(s) stays valid between
1747 				 * the time obtaining j and numosp.
1748 				 */
1749 				ASSERT(j == numosp);
1750 			}
1751 
1752 			mutex_exit(&rp->r_os_lock);
1753 			/* do this here to keep v_lock > r_os_lock */
1754 			if (hold_vnode)
1755 				VN_HOLD(vp);
1756 			mutex_enter(&rp->r_statev4_lock);
1757 			if (rp->r_deleg_type != OPEN_DELEGATE_NONE) {
1758 				/*
1759 				 * If this rnode holds a delegation,
1760 				 * but if there are no valid open streams,
1761 				 * then just discard the delegation
1762 				 * without doing delegreturn.
1763 				 */
1764 				if (numosp > 0)
1765 					rp->r_deleg_needs_recovery =
1766 					    rp->r_deleg_type;
1767 			}
1768 			/* Save the delegation type for use outside the lock */
1769 			dtype = rp->r_deleg_type;
1770 			mutex_exit(&rp->r_statev4_lock);
1771 
1772 			/*
1773 			 * If we have a delegation then get rid of it.
1774 			 * We've set rp->r_deleg_needs_recovery so we have
1775 			 * enough information to recover.
1776 			 */
1777 			if (dtype != OPEN_DELEGATE_NONE) {
1778 				(void) nfs4delegreturn(rp, NFS4_DR_DISCARD);
1779 			}
1780 		}
1781 		rw_exit(&rtable4[index].r_lock);
1782 	}
1783 	return (reopenlist);
1784 }
1785 
1786 /*
1787  * Given a filesystem id, check to see if any rnodes
1788  * within this fsid reside in the rnode cache, other
1789  * than one we know about.
1790  *
1791  * Return 1 if an rnode is found, 0 otherwise
1792  */
1793 int
1794 r4find_by_fsid(mntinfo4_t *mi, fattr4_fsid *moved_fsid)
1795 {
1796 	rnode4_t *rp;
1797 	vnode_t *vp;
1798 	vfs_t *vfsp = mi->mi_vfsp;
1799 	fattr4_fsid *fsid;
1800 	int index, found = 0;
1801 
1802 	for (index = 0; index < rtable4size; index++) {
1803 		rw_enter(&rtable4[index].r_lock, RW_READER);
1804 		for (rp = rtable4[index].r_hashf;
1805 		    rp != (rnode4_t *)(&rtable4[index]);
1806 		    rp = rp->r_hashf) {
1807 
1808 			vp = RTOV4(rp);
1809 			if (vp->v_vfsp != vfsp)
1810 				continue;
1811 
1812 			/*
1813 			 * XXX there might be a case where a
1814 			 * replicated fs may have the same fsid
1815 			 * across two different servers. This
1816 			 * check isn't good enough in that case
1817 			 */
1818 			fsid = &rp->r_srv_fsid;
1819 			if (FATTR4_FSID_EQ(moved_fsid, fsid)) {
1820 				found = 1;
1821 				break;
1822 			}
1823 		}
1824 		rw_exit(&rtable4[index].r_lock);
1825 
1826 		if (found)
1827 			break;
1828 	}
1829 	return (found);
1830 }
1831 
1832 /*
1833  * Release the list of open instance references.
1834  */
1835 
1836 void
1837 r4releopenlist(nfs4_opinst_t *reopenp)
1838 {
1839 	nfs4_opinst_t *rep, *next;
1840 	int i;
1841 
1842 	for (rep = reopenp; rep; rep = next) {
1843 		next = rep->re_next;
1844 
1845 		for (i = 0; i < rep->re_numosp; i++)
1846 			open_stream_rele(rep->re_osp[i], VTOR4(rep->re_vp));
1847 
1848 		VN_RELE(rep->re_vp);
1849 		kmem_free(rep->re_osp,
1850 		    rep->re_numosp * sizeof (*(rep->re_osp)));
1851 
1852 		kmem_free(rep, sizeof (*rep));
1853 	}
1854 }
1855 
1856 int
1857 nfs4_rnode_init(void)
1858 {
1859 	ulong_t nrnode4_max;
1860 	int i;
1861 
1862 	/*
1863 	 * Compute the size of the rnode4 hash table
1864 	 */
1865 	if (nrnode <= 0)
1866 		nrnode = ncsize;
1867 	nrnode4_max =
1868 	    (ulong_t)((kmem_maxavail() >> 2) / sizeof (struct rnode4));
1869 	if (nrnode > nrnode4_max || (nrnode == 0 && ncsize == 0)) {
1870 		zcmn_err(GLOBAL_ZONEID, CE_NOTE,
1871 		    "!setting nrnode to max value of %ld", nrnode4_max);
1872 		nrnode = nrnode4_max;
1873 	}
1874 	rtable4size = 1 << highbit(nrnode / rnode4_hashlen);
1875 	rtable4mask = rtable4size - 1;
1876 
1877 	/*
1878 	 * Allocate and initialize the hash buckets
1879 	 */
1880 	rtable4 = kmem_alloc(rtable4size * sizeof (*rtable4), KM_SLEEP);
1881 	for (i = 0; i < rtable4size; i++) {
1882 		rtable4[i].r_hashf = (rnode4_t *)(&rtable4[i]);
1883 		rtable4[i].r_hashb = (rnode4_t *)(&rtable4[i]);
1884 		rw_init(&rtable4[i].r_lock, NULL, RW_DEFAULT, NULL);
1885 	}
1886 
1887 	rnode4_cache = kmem_cache_create("rnode4_cache", sizeof (rnode4_t),
1888 	    0, NULL, NULL, nfs4_reclaim, NULL, NULL, 0);
1889 
1890 	return (0);
1891 }
1892 
1893 int
1894 nfs4_rnode_fini(void)
1895 {
1896 	int i;
1897 
1898 	/*
1899 	 * Deallocate the rnode hash queues
1900 	 */
1901 	kmem_cache_destroy(rnode4_cache);
1902 
1903 	for (i = 0; i < rtable4size; i++)
1904 		rw_destroy(&rtable4[i].r_lock);
1905 
1906 	kmem_free(rtable4, rtable4size * sizeof (*rtable4));
1907 
1908 	return (0);
1909 }
1910 
1911 /*
1912  * Return non-zero if the given filehandle refers to the root filehandle
1913  * for the given rnode.
1914  */
1915 
1916 static int
1917 isrootfh(nfs4_sharedfh_t *fh, rnode4_t *rp)
1918 {
1919 	int isroot;
1920 
1921 	isroot = 0;
1922 	if (SFH4_SAME(VTOMI4(RTOV4(rp))->mi_rootfh, fh))
1923 		isroot = 1;
1924 
1925 	return (isroot);
1926 }
1927 
1928 /*
1929  * The r4_stub_* routines assume that the rnode is newly activated, and
1930  * that the caller either holds the hash bucket r_lock for this rnode as
1931  * RW_WRITER, or holds r_statelock.
1932  */
1933 static void
1934 r4_stub_set(rnode4_t *rp, nfs4_stub_type_t type)
1935 {
1936 	vnode_t *vp = RTOV4(rp);
1937 	krwlock_t *hash_lock = &rp->r_hashq->r_lock;
1938 
1939 	ASSERT(RW_WRITE_HELD(hash_lock) || MUTEX_HELD(&rp->r_statelock));
1940 
1941 	rp->r_stub_type = type;
1942 
1943 	/*
1944 	 * Safely switch this vnode to the trigger vnodeops.
1945 	 *
1946 	 * Currently, we don't ever switch a trigger vnode back to using
1947 	 * "regular" v4 vnodeops. NFS4_STUB_NONE is only used to note that
1948 	 * a new v4 object is not a trigger, and it will already have the
1949 	 * correct v4 vnodeops by default. So, no "else" case required here.
1950 	 */
1951 	if (type != NFS4_STUB_NONE)
1952 		vn_setops(vp, nfs4_trigger_vnodeops);
1953 }
1954 
1955 void
1956 r4_stub_mirrormount(rnode4_t *rp)
1957 {
1958 	r4_stub_set(rp, NFS4_STUB_MIRRORMOUNT);
1959 }
1960 
1961 void
1962 r4_stub_referral(rnode4_t *rp)
1963 {
1964 	DTRACE_PROBE1(nfs4clnt__func__referral__moved,
1965 	    vnode_t *, RTOV4(rp));
1966 	r4_stub_set(rp, NFS4_STUB_REFERRAL);
1967 }
1968 
1969 void
1970 r4_stub_none(rnode4_t *rp)
1971 {
1972 	r4_stub_set(rp, NFS4_STUB_NONE);
1973 }
1974 
1975 #ifdef DEBUG
1976 
1977 /*
1978  * Look in the rnode table for other rnodes that have the same filehandle.
1979  * Assume the lock is held for the hash chain of checkrp
1980  */
1981 
1982 static void
1983 r4_dup_check(rnode4_t *checkrp, vfs_t *vfsp)
1984 {
1985 	rnode4_t *rp;
1986 	vnode_t *tvp;
1987 	nfs4_fhandle_t fh, fh2;
1988 	int index;
1989 
1990 	if (!r4_check_for_dups)
1991 		return;
1992 
1993 	ASSERT(RW_LOCK_HELD(&checkrp->r_hashq->r_lock));
1994 
1995 	sfh4_copyval(checkrp->r_fh, &fh);
1996 
1997 	for (index = 0; index < rtable4size; index++) {
1998 
1999 		if (&rtable4[index] != checkrp->r_hashq)
2000 			rw_enter(&rtable4[index].r_lock, RW_READER);
2001 
2002 		for (rp = rtable4[index].r_hashf;
2003 		    rp != (rnode4_t *)(&rtable4[index]);
2004 		    rp = rp->r_hashf) {
2005 
2006 			if (rp == checkrp)
2007 				continue;
2008 
2009 			tvp = RTOV4(rp);
2010 			if (tvp->v_vfsp != vfsp)
2011 				continue;
2012 
2013 			sfh4_copyval(rp->r_fh, &fh2);
2014 			if (nfs4cmpfhandle(&fh, &fh2) == 0) {
2015 				cmn_err(CE_PANIC, "rnodes with same fs, fh "
2016 				    "(%p, %p)", (void *)checkrp, (void *)rp);
2017 			}
2018 		}
2019 
2020 		if (&rtable4[index] != checkrp->r_hashq)
2021 			rw_exit(&rtable4[index].r_lock);
2022 	}
2023 }
2024 
2025 #endif /* DEBUG */
2026