xref: /titanic_52/usr/src/uts/common/fs/nfs/nfs4_subr.c (revision ba4e3c84e6b9390bbf7df80b5f1d11dec34cc525)
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 2006 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/cmn_err.h>
37 #include <sys/vtrace.h>
38 #include <sys/session.h>
39 #include <sys/thread.h>
40 #include <sys/dnlc.h>
41 #include <sys/cred.h>
42 #include <sys/priv.h>
43 #include <sys/list.h>
44 #include <sys/sdt.h>
45 #include <sys/policy.h>
46 
47 #include <rpc/types.h>
48 #include <rpc/xdr.h>
49 
50 #include <nfs/nfs.h>
51 
52 #include <nfs/nfs_clnt.h>
53 
54 #include <nfs/nfs4.h>
55 #include <nfs/rnode4.h>
56 #include <nfs/nfs4_clnt.h>
57 
58 /*
59  * client side statistics
60  */
61 static const struct clstat4 clstat4_tmpl = {
62 	{ "calls",	KSTAT_DATA_UINT64 },
63 	{ "badcalls",	KSTAT_DATA_UINT64 },
64 	{ "clgets",	KSTAT_DATA_UINT64 },
65 	{ "cltoomany",	KSTAT_DATA_UINT64 },
66 #ifdef DEBUG
67 	{ "clalloc",	KSTAT_DATA_UINT64 },
68 	{ "noresponse",	KSTAT_DATA_UINT64 },
69 	{ "failover",	KSTAT_DATA_UINT64 },
70 	{ "remap",	KSTAT_DATA_UINT64 },
71 #endif
72 };
73 
74 #ifdef DEBUG
75 struct clstat4_debug clstat4_debug = {
76 	{ "nrnode",	KSTAT_DATA_UINT64 },
77 	{ "access",	KSTAT_DATA_UINT64 },
78 	{ "dirent",	KSTAT_DATA_UINT64 },
79 	{ "dirents",	KSTAT_DATA_UINT64 },
80 	{ "reclaim",	KSTAT_DATA_UINT64 },
81 	{ "clreclaim",	KSTAT_DATA_UINT64 },
82 	{ "f_reclaim",	KSTAT_DATA_UINT64 },
83 	{ "a_reclaim",	KSTAT_DATA_UINT64 },
84 	{ "r_reclaim",	KSTAT_DATA_UINT64 },
85 	{ "r_path",	KSTAT_DATA_UINT64 },
86 };
87 #endif
88 
89 /*
90  * We keep a global list of per-zone client data, so we can clean up all zones
91  * if we get low on memory.
92  */
93 static list_t nfs4_clnt_list;
94 static kmutex_t nfs4_clnt_list_lock;
95 static zone_key_t nfs4clnt_zone_key;
96 
97 static struct kmem_cache *chtab4_cache;
98 
99 #ifdef DEBUG
100 static int nfs4_rfscall_debug;
101 static int nfs4_try_failover_any;
102 int nfs4_utf8_debug = 0;
103 #endif
104 
105 /*
106  * NFSv4 readdir cache implementation
107  */
108 typedef struct rddir4_cache_impl {
109 	rddir4_cache	rc;		/* readdir cache element */
110 	kmutex_t	lock;		/* lock protects count */
111 	uint_t		count;		/* reference count */
112 	avl_node_t	tree;		/* AVL tree link */
113 } rddir4_cache_impl;
114 
115 static int rddir4_cache_compar(const void *, const void *);
116 static void rddir4_cache_free(rddir4_cache_impl *);
117 static rddir4_cache *rddir4_cache_alloc(int);
118 static void rddir4_cache_hold(rddir4_cache *);
119 static int try_failover(enum clnt_stat);
120 
121 static int nfs4_readdir_cache_hits = 0;
122 static int nfs4_readdir_cache_waits = 0;
123 static int nfs4_readdir_cache_misses = 0;
124 
125 /*
126  * Shared nfs4 functions
127  */
128 
129 /*
130  * Copy an nfs_fh4.  The destination storage (to->nfs_fh4_val) must already
131  * be allocated.
132  */
133 
134 void
135 nfs_fh4_copy(nfs_fh4 *from, nfs_fh4 *to)
136 {
137 	to->nfs_fh4_len = from->nfs_fh4_len;
138 	bcopy(from->nfs_fh4_val, to->nfs_fh4_val, to->nfs_fh4_len);
139 }
140 
141 /*
142  * nfs4cmpfh - compare 2 filehandles.
143  * Returns 0 if the two nfsv4 filehandles are the same, -1 if the first is
144  * "less" than the second, +1 if the first is "greater" than the second.
145  */
146 
147 int
148 nfs4cmpfh(const nfs_fh4 *fh4p1, const nfs_fh4 *fh4p2)
149 {
150 	const char *c1, *c2;
151 
152 	if (fh4p1->nfs_fh4_len < fh4p2->nfs_fh4_len)
153 		return (-1);
154 	if (fh4p1->nfs_fh4_len > fh4p2->nfs_fh4_len)
155 		return (1);
156 	for (c1 = fh4p1->nfs_fh4_val, c2 = fh4p2->nfs_fh4_val;
157 	    c1 < fh4p1->nfs_fh4_val + fh4p1->nfs_fh4_len;
158 	    c1++, c2++) {
159 		if (*c1 < *c2)
160 			return (-1);
161 		if (*c1 > *c2)
162 			return (1);
163 	}
164 
165 	return (0);
166 }
167 
168 /*
169  * Compare two v4 filehandles.  Return zero if they're the same, non-zero
170  * if they're not.  Like nfs4cmpfh(), but different filehandle
171  * representation, and doesn't provide information about greater than or
172  * less than.
173  */
174 
175 int
176 nfs4cmpfhandle(nfs4_fhandle_t *fh1, nfs4_fhandle_t *fh2)
177 {
178 	if (fh1->fh_len == fh2->fh_len)
179 		return (bcmp(fh1->fh_buf, fh2->fh_buf, fh1->fh_len));
180 
181 	return (1);
182 }
183 
184 int
185 stateid4_cmp(stateid4 *s1, stateid4 *s2)
186 {
187 	if (bcmp(s1, s2, sizeof (stateid4)) == 0)
188 		return (1);
189 	else
190 		return (0);
191 }
192 
193 nfsstat4
194 puterrno4(int error)
195 {
196 	switch (error) {
197 	case 0:
198 		return (NFS4_OK);
199 	case EPERM:
200 		return (NFS4ERR_PERM);
201 	case ENOENT:
202 		return (NFS4ERR_NOENT);
203 	case EINTR:
204 		return (NFS4ERR_IO);
205 	case EIO:
206 		return (NFS4ERR_IO);
207 	case ENXIO:
208 		return (NFS4ERR_NXIO);
209 	case ENOMEM:
210 		return (NFS4ERR_RESOURCE);
211 	case EACCES:
212 		return (NFS4ERR_ACCESS);
213 	case EBUSY:
214 		return (NFS4ERR_IO);
215 	case EEXIST:
216 		return (NFS4ERR_EXIST);
217 	case EXDEV:
218 		return (NFS4ERR_XDEV);
219 	case ENODEV:
220 		return (NFS4ERR_IO);
221 	case ENOTDIR:
222 		return (NFS4ERR_NOTDIR);
223 	case EISDIR:
224 		return (NFS4ERR_ISDIR);
225 	case EINVAL:
226 		return (NFS4ERR_INVAL);
227 	case EMFILE:
228 		return (NFS4ERR_RESOURCE);
229 	case EFBIG:
230 		return (NFS4ERR_FBIG);
231 	case ENOSPC:
232 		return (NFS4ERR_NOSPC);
233 	case EROFS:
234 		return (NFS4ERR_ROFS);
235 	case EMLINK:
236 		return (NFS4ERR_MLINK);
237 	case EDEADLK:
238 		return (NFS4ERR_DEADLOCK);
239 	case ENOLCK:
240 		return (NFS4ERR_DENIED);
241 	case EREMOTE:
242 		return (NFS4ERR_SERVERFAULT);
243 	case ENOTSUP:
244 		return (NFS4ERR_NOTSUPP);
245 	case EDQUOT:
246 		return (NFS4ERR_DQUOT);
247 	case ENAMETOOLONG:
248 		return (NFS4ERR_NAMETOOLONG);
249 	case EOVERFLOW:
250 		return (NFS4ERR_INVAL);
251 	case ENOSYS:
252 		return (NFS4ERR_NOTSUPP);
253 	case ENOTEMPTY:
254 		return (NFS4ERR_NOTEMPTY);
255 	case EOPNOTSUPP:
256 		return (NFS4ERR_NOTSUPP);
257 	case ESTALE:
258 		return (NFS4ERR_STALE);
259 	case EAGAIN:
260 		if (curthread->t_flag & T_WOULDBLOCK) {
261 			curthread->t_flag &= ~T_WOULDBLOCK;
262 			return (NFS4ERR_DELAY);
263 		}
264 		return (NFS4ERR_LOCKED);
265 	default:
266 		return ((enum nfsstat4)error);
267 	}
268 }
269 
270 int
271 geterrno4(enum nfsstat4 status)
272 {
273 	switch (status) {
274 	case NFS4_OK:
275 		return (0);
276 	case NFS4ERR_PERM:
277 		return (EPERM);
278 	case NFS4ERR_NOENT:
279 		return (ENOENT);
280 	case NFS4ERR_IO:
281 		return (EIO);
282 	case NFS4ERR_NXIO:
283 		return (ENXIO);
284 	case NFS4ERR_ACCESS:
285 		return (EACCES);
286 	case NFS4ERR_EXIST:
287 		return (EEXIST);
288 	case NFS4ERR_XDEV:
289 		return (EXDEV);
290 	case NFS4ERR_NOTDIR:
291 		return (ENOTDIR);
292 	case NFS4ERR_ISDIR:
293 		return (EISDIR);
294 	case NFS4ERR_INVAL:
295 		return (EINVAL);
296 	case NFS4ERR_FBIG:
297 		return (EFBIG);
298 	case NFS4ERR_NOSPC:
299 		return (ENOSPC);
300 	case NFS4ERR_ROFS:
301 		return (EROFS);
302 	case NFS4ERR_MLINK:
303 		return (EMLINK);
304 	case NFS4ERR_NAMETOOLONG:
305 		return (ENAMETOOLONG);
306 	case NFS4ERR_NOTEMPTY:
307 		return (ENOTEMPTY);
308 	case NFS4ERR_DQUOT:
309 		return (EDQUOT);
310 	case NFS4ERR_STALE:
311 		return (ESTALE);
312 	case NFS4ERR_BADHANDLE:
313 		return (ESTALE);
314 	case NFS4ERR_BAD_COOKIE:
315 		return (EINVAL);
316 	case NFS4ERR_NOTSUPP:
317 		return (EOPNOTSUPP);
318 	case NFS4ERR_TOOSMALL:
319 		return (EINVAL);
320 	case NFS4ERR_SERVERFAULT:
321 		return (EIO);
322 	case NFS4ERR_BADTYPE:
323 		return (EINVAL);
324 	case NFS4ERR_DELAY:
325 		return (ENXIO);
326 	case NFS4ERR_SAME:
327 		return (EPROTO);
328 	case NFS4ERR_DENIED:
329 		return (ENOLCK);
330 	case NFS4ERR_EXPIRED:
331 		return (EPROTO);
332 	case NFS4ERR_LOCKED:
333 		return (EACCES);
334 	case NFS4ERR_GRACE:
335 		return (EAGAIN);
336 	case NFS4ERR_FHEXPIRED:	/* if got here, failed to get a new fh */
337 		return (ESTALE);
338 	case NFS4ERR_SHARE_DENIED:
339 		return (EACCES);
340 	case NFS4ERR_WRONGSEC:
341 		return (EPERM);
342 	case NFS4ERR_CLID_INUSE:
343 		return (EAGAIN);
344 	case NFS4ERR_RESOURCE:
345 		return (EAGAIN);
346 	case NFS4ERR_MOVED:
347 		return (EPROTO);
348 	case NFS4ERR_NOFILEHANDLE:
349 		return (EIO);
350 	case NFS4ERR_MINOR_VERS_MISMATCH:
351 		return (ENOTSUP);
352 	case NFS4ERR_STALE_CLIENTID:
353 		return (EIO);
354 	case NFS4ERR_STALE_STATEID:
355 		return (EIO);
356 	case NFS4ERR_OLD_STATEID:
357 		return (EIO);
358 	case NFS4ERR_BAD_STATEID:
359 		return (EIO);
360 	case NFS4ERR_BAD_SEQID:
361 		return (EIO);
362 	case NFS4ERR_NOT_SAME:
363 		return (EPROTO);
364 	case NFS4ERR_LOCK_RANGE:
365 		return (EPROTO);
366 	case NFS4ERR_SYMLINK:
367 		return (EPROTO);
368 	case NFS4ERR_RESTOREFH:
369 		return (EPROTO);
370 	case NFS4ERR_LEASE_MOVED:
371 		return (EPROTO);
372 	case NFS4ERR_ATTRNOTSUPP:
373 		return (ENOTSUP);
374 	case NFS4ERR_NO_GRACE:
375 		return (EPROTO);
376 	case NFS4ERR_RECLAIM_BAD:
377 		return (EPROTO);
378 	case NFS4ERR_RECLAIM_CONFLICT:
379 		return (EPROTO);
380 	case NFS4ERR_BADXDR:
381 		return (EINVAL);
382 	case NFS4ERR_LOCKS_HELD:
383 		return (EIO);
384 	case NFS4ERR_OPENMODE:
385 		return (EACCES);
386 	case NFS4ERR_BADOWNER:
387 		/*
388 		 * Client and server are in different DNS domains
389 		 * and the NFSMAPID_DOMAIN in /etc/default/nfs
390 		 * doesn't match.  No good answer here.  Return
391 		 * EACCESS, which translates to "permission denied".
392 		 */
393 		return (EACCES);
394 	case NFS4ERR_BADCHAR:
395 		return (EINVAL);
396 	case NFS4ERR_BADNAME:
397 		return (EINVAL);
398 	case NFS4ERR_BAD_RANGE:
399 		return (EIO);
400 	case NFS4ERR_LOCK_NOTSUPP:
401 		return (ENOTSUP);
402 	case NFS4ERR_OP_ILLEGAL:
403 		return (EINVAL);
404 	case NFS4ERR_DEADLOCK:
405 		return (EDEADLK);
406 	case NFS4ERR_FILE_OPEN:
407 		return (EACCES);
408 	case NFS4ERR_ADMIN_REVOKED:
409 		return (EPROTO);
410 	case NFS4ERR_CB_PATH_DOWN:
411 		return (EPROTO);
412 	default:
413 #ifdef DEBUG
414 		zcmn_err(getzoneid(), CE_WARN, "geterrno4: got status %d",
415 		    status);
416 #endif
417 		return ((int)status);
418 	}
419 }
420 
421 void
422 nfs4_log_badowner(mntinfo4_t *mi, nfs_opnum4 op)
423 {
424 	nfs4_server_t *server;
425 
426 	/*
427 	 * Return if already printed/queued a msg
428 	 * for this mount point.
429 	 */
430 	if (mi->mi_flags & MI4_BADOWNER_DEBUG)
431 		return;
432 	/*
433 	 * Happens once per client <-> server pair.
434 	 */
435 	if (nfs_rw_enter_sig(&mi->mi_recovlock, RW_READER,
436 		mi->mi_flags & MI4_INT))
437 		return;
438 
439 	server = find_nfs4_server(mi);
440 	if (server == NULL) {
441 		nfs_rw_exit(&mi->mi_recovlock);
442 		return;
443 	}
444 
445 	if (!(server->s_flags & N4S_BADOWNER_DEBUG)) {
446 		zcmn_err(mi->mi_zone->zone_id, CE_WARN,
447 		    "!NFSMAPID_DOMAIN does not match"
448 		    " the server: %s domain.\n"
449 		    "Please check configuration",
450 		    mi->mi_curr_serv->sv_hostname);
451 		server->s_flags |= N4S_BADOWNER_DEBUG;
452 	}
453 	mutex_exit(&server->s_lock);
454 	nfs4_server_rele(server);
455 	nfs_rw_exit(&mi->mi_recovlock);
456 
457 	/*
458 	 * Happens once per mntinfo4_t.
459 	 * This error is deemed as one of the recovery facts "RF_BADOWNER",
460 	 * queue this in the mesg queue for this mount_info. This message
461 	 * is not printed, meaning its absent from id_to_dump_solo_fact()
462 	 * but its there for inspection if the queue is ever dumped/inspected.
463 	 */
464 	mutex_enter(&mi->mi_lock);
465 	if (!(mi->mi_flags & MI4_BADOWNER_DEBUG)) {
466 		nfs4_queue_fact(RF_BADOWNER, mi, NFS4ERR_BADOWNER, 0, op,
467 		    FALSE, NULL, 0, NULL);
468 		mi->mi_flags |= MI4_BADOWNER_DEBUG;
469 	}
470 	mutex_exit(&mi->mi_lock);
471 }
472 
473 
474 
475 int
476 nfs4_time_ntov(nfstime4 *ntime, timestruc_t *vatime)
477 {
478 	int64_t sec;
479 	int32_t nsec;
480 
481 	/*
482 	 * Here check that the nfsv4 time is valid for the system.
483 	 * nfsv4 time value is a signed 64-bit, and the system time
484 	 * may be either int64_t or int32_t (depends on the kernel),
485 	 * so if the kernel is 32-bit, the nfsv4 time value may not fit.
486 	 */
487 #ifndef _LP64
488 	if (! NFS4_TIME_OK(ntime->seconds)) {
489 		return (EOVERFLOW);
490 	}
491 #endif
492 
493 	/* Invalid to specify 1 billion (or more) nsecs */
494 	if (ntime->nseconds >= 1000000000)
495 		return (EINVAL);
496 
497 	if (ntime->seconds < 0) {
498 		sec = ntime->seconds + 1;
499 		nsec = -1000000000 + ntime->nseconds;
500 	} else {
501 		sec = ntime->seconds;
502 		nsec = ntime->nseconds;
503 	}
504 
505 	vatime->tv_sec = sec;
506 	vatime->tv_nsec = nsec;
507 
508 	return (0);
509 }
510 
511 int
512 nfs4_time_vton(timestruc_t *vatime, nfstime4 *ntime)
513 {
514 	int64_t sec;
515 	uint32_t nsec;
516 
517 	/*
518 	 * nfsv4 time value is a signed 64-bit, and the system time
519 	 * may be either int64_t or int32_t (depends on the kernel),
520 	 * so all system time values will fit.
521 	 */
522 	if (vatime->tv_nsec >= 0) {
523 		sec = vatime->tv_sec;
524 		nsec = vatime->tv_nsec;
525 	} else {
526 		sec = vatime->tv_sec - 1;
527 		nsec = 1000000000 + vatime->tv_nsec;
528 	}
529 	ntime->seconds = sec;
530 	ntime->nseconds = nsec;
531 
532 	return (0);
533 }
534 
535 /*
536  * Converts a utf8 string to a valid null terminated filename string.
537  *
538  * XXX - Not actually translating the UTF-8 string as per RFC 2279.
539  *	 For now, just validate that the UTF-8 string off the wire
540  *	 does not have characters that will freak out UFS, and leave
541  *	 it at that.
542  */
543 char *
544 utf8_to_fn(utf8string *u8s, uint_t *lenp, char *s)
545 {
546 	ASSERT(lenp != NULL);
547 
548 	if (u8s == NULL || u8s->utf8string_len <= 0 ||
549 					u8s->utf8string_val == NULL)
550 		return (NULL);
551 
552 	/*
553 	 * Check for obvious illegal filename chars
554 	 */
555 	if (utf8_strchr(u8s, '/') != NULL) {
556 #ifdef DEBUG
557 		if (nfs4_utf8_debug) {
558 			char *path;
559 			int len = u8s->utf8string_len;
560 
561 			path = kmem_alloc(len + 1, KM_SLEEP);
562 			bcopy(u8s->utf8string_val, path, len);
563 			path[len] = '\0';
564 
565 			zcmn_err(getzoneid(), CE_WARN,
566 			    "Invalid UTF-8 filename: %s", path);
567 
568 			kmem_free(path, len + 1);
569 		}
570 #endif
571 		return (NULL);
572 	}
573 
574 	return (utf8_to_str(u8s, lenp, s));
575 }
576 
577 /*
578  * Converts a utf8 string to a C string.
579  * kmem_allocs a new string if not supplied
580  */
581 char *
582 utf8_to_str(utf8string *str, uint_t *lenp, char *s)
583 {
584 	char	*sp;
585 	char	*u8p;
586 	int	len;
587 	int	 i;
588 
589 	ASSERT(lenp != NULL);
590 
591 	if (str == NULL)
592 		return (NULL);
593 
594 	u8p = str->utf8string_val;
595 	len = str->utf8string_len;
596 	if (len <= 0 || u8p == NULL) {
597 		if (s)
598 			*s = '\0';
599 		return (NULL);
600 	}
601 
602 	sp = s;
603 	if (sp == NULL)
604 		sp = kmem_alloc(len + 1, KM_SLEEP);
605 
606 	/*
607 	 * At least check for embedded nulls
608 	 */
609 	for (i = 0; i < len; i++) {
610 		sp[i] = u8p[i];
611 		if (u8p[i] == '\0') {
612 #ifdef	DEBUG
613 			zcmn_err(getzoneid(), CE_WARN,
614 			    "Embedded NULL in UTF-8 string");
615 #endif
616 			if (s == NULL)
617 				kmem_free(sp, len + 1);
618 			return (NULL);
619 		}
620 	}
621 	sp[len] = '\0';
622 	*lenp = len + 1;
623 
624 	return (sp);
625 }
626 
627 /*
628  * str_to_utf8 - converts a null-terminated C string to a utf8 string
629  */
630 utf8string *
631 str_to_utf8(char *nm, utf8string *str)
632 {
633 	int len;
634 
635 	if (str == NULL)
636 		return (NULL);
637 
638 	if (nm == NULL || *nm == '\0') {
639 		str->utf8string_len = 0;
640 		str->utf8string_val = NULL;
641 	}
642 
643 	len = strlen(nm);
644 
645 	str->utf8string_val = kmem_alloc(len, KM_SLEEP);
646 	str->utf8string_len = len;
647 	bcopy(nm, str->utf8string_val, len);
648 
649 	return (str);
650 }
651 
652 utf8string *
653 utf8_copy(utf8string *src, utf8string *dest)
654 {
655 	if (src == NULL)
656 		return (NULL);
657 	if (dest == NULL)
658 		return (NULL);
659 
660 	if (src->utf8string_len > 0) {
661 		dest->utf8string_val = kmem_alloc(src->utf8string_len,
662 			KM_SLEEP);
663 		bcopy(src->utf8string_val, dest->utf8string_val,
664 			src->utf8string_len);
665 		dest->utf8string_len = src->utf8string_len;
666 	} else {
667 		dest->utf8string_val = NULL;
668 		dest->utf8string_len = 0;
669 	}
670 
671 	return (dest);
672 }
673 
674 int
675 utf8_compare(const utf8string *a, const utf8string *b)
676 {
677 	int mlen, cmp;
678 	int alen, blen;
679 	char *aval, *bval;
680 
681 	if ((a == NULL) && (b == NULL))
682 		return (0);
683 	else if (a == NULL)
684 		return (-1);
685 	else if (b == NULL)
686 		return (1);
687 
688 	alen = a->utf8string_len;
689 	blen = b->utf8string_len;
690 	aval = a->utf8string_val;
691 	bval = b->utf8string_val;
692 
693 	if (((alen == 0) || (aval == NULL)) &&
694 	    ((blen == 0) || (bval == NULL)))
695 		return (0);
696 	else if ((alen == 0) || (aval == NULL))
697 		return (-1);
698 	else if ((blen == 0) || (bval == NULL))
699 		return (1);
700 
701 	mlen = MIN(alen, blen);
702 	cmp = strncmp(aval, bval, mlen);
703 
704 	if ((cmp == 0) && (alen == blen))
705 		return (0);
706 	else if ((cmp == 0) && (alen < blen))
707 		return (-1);
708 	else if (cmp == 0)
709 		return (1);
710 	else if (cmp < 0)
711 		return (-1);
712 	return (1);
713 }
714 
715 /*
716  * utf8_dir_verify - checks that the utf8 string is valid
717  */
718 int
719 utf8_dir_verify(utf8string *str)
720 {
721 	char *nm;
722 	int len;
723 
724 	if (str == NULL)
725 		return (0);
726 
727 	nm = str->utf8string_val;
728 	len = str->utf8string_len;
729 	if (nm == NULL || len == 0) {
730 		return (0);
731 	}
732 
733 	if (len == 1 && nm[0] == '.')
734 		return (0);
735 	if (len == 2 && nm[0] == '.' && nm[1] == '.')
736 		return (0);
737 
738 	if (utf8_strchr(str, '/') != NULL)
739 		return (0);
740 
741 	if (utf8_strchr(str, '\0') != NULL)
742 		return (0);
743 
744 	return (1);
745 }
746 
747 /*
748  * from rpcsec module (common/rpcsec)
749  */
750 extern int sec_clnt_geth(CLIENT *, struct sec_data *, cred_t *, AUTH **);
751 extern void sec_clnt_freeh(AUTH *);
752 extern void sec_clnt_freeinfo(struct sec_data *);
753 
754 /*
755  * authget() gets an auth handle based on the security
756  * information from the servinfo in mountinfo.
757  * The auth handle is stored in ch_client->cl_auth.
758  *
759  * First security flavor of choice is to use sv_secdata
760  * which is initiated by the client. If that fails, get
761  * secinfo from the server and then select one from the
762  * server secinfo list .
763  *
764  * For RPCSEC_GSS flavor, upon success, a secure context is
765  * established between client and server.
766  */
767 int
768 authget(servinfo4_t *svp, CLIENT *ch_client, cred_t *cr)
769 {
770 	int error, i;
771 
772 	/*
773 	 * SV4_TRYSECINFO indicates to try the secinfo list from
774 	 * sv_secinfo until a successful one is reached. Point
775 	 * sv_currsec to the selected security mechanism for
776 	 * later sessions.
777 	 */
778 	(void) nfs_rw_enter_sig(&svp->sv_lock, RW_WRITER, 0);
779 	if ((svp->sv_flags & SV4_TRYSECINFO) && svp->sv_secinfo) {
780 		for (i = svp->sv_secinfo->index; i < svp->sv_secinfo->count;
781 								i++) {
782 			if (!(error = sec_clnt_geth(ch_client,
783 				&svp->sv_secinfo->sdata[i],
784 				cr, &ch_client->cl_auth))) {
785 
786 				svp->sv_currsec = &svp->sv_secinfo->sdata[i];
787 				svp->sv_secinfo->index = i;
788 				/* done */
789 				svp->sv_flags &= ~SV4_TRYSECINFO;
790 				break;
791 			}
792 
793 			/*
794 			 * Allow the caller retry with the security flavor
795 			 * pointed by svp->sv_secinfo->index when
796 			 * ETIMEDOUT/ECONNRESET occurs.
797 			 */
798 			if (error == ETIMEDOUT || error == ECONNRESET) {
799 				svp->sv_secinfo->index = i;
800 				break;
801 			}
802 		}
803 	} else {
804 		/* sv_currsec points to one of the entries in sv_secinfo */
805 		if (svp->sv_currsec) {
806 			error = sec_clnt_geth(ch_client, svp->sv_currsec, cr,
807 				&ch_client->cl_auth);
808 		} else {
809 			/* If it's null, use sv_secdata. */
810 			error = sec_clnt_geth(ch_client, svp->sv_secdata, cr,
811 				&ch_client->cl_auth);
812 		}
813 	}
814 	nfs_rw_exit(&svp->sv_lock);
815 
816 	return (error);
817 }
818 
819 /*
820  * Common handle get program for NFS, NFS ACL, and NFS AUTH client.
821  */
822 int
823 clget4(clinfo_t *ci, servinfo4_t *svp, cred_t *cr, CLIENT **newcl,
824     struct chtab **chp, struct nfs4_clnt *nfscl)
825 {
826 	struct chhead *ch, *newch;
827 	struct chhead **plistp;
828 	struct chtab *cp;
829 	int error;
830 	k_sigset_t smask;
831 
832 	if (newcl == NULL || chp == NULL || ci == NULL)
833 		return (EINVAL);
834 
835 	*newcl = NULL;
836 	*chp = NULL;
837 
838 	/*
839 	 * Find an unused handle or create one
840 	 */
841 	newch = NULL;
842 	nfscl->nfscl_stat.clgets.value.ui64++;
843 top:
844 	/*
845 	 * Find the correct entry in the cache to check for free
846 	 * client handles.  The search is based on the RPC program
847 	 * number, program version number, dev_t for the transport
848 	 * device, and the protocol family.
849 	 */
850 	mutex_enter(&nfscl->nfscl_chtable4_lock);
851 	plistp = &nfscl->nfscl_chtable4;
852 	for (ch = nfscl->nfscl_chtable4; ch != NULL; ch = ch->ch_next) {
853 		if (ch->ch_prog == ci->cl_prog &&
854 		    ch->ch_vers == ci->cl_vers &&
855 		    ch->ch_dev == svp->sv_knconf->knc_rdev &&
856 		    (strcmp(ch->ch_protofmly,
857 			svp->sv_knconf->knc_protofmly) == 0))
858 			break;
859 		plistp = &ch->ch_next;
860 	}
861 
862 	/*
863 	 * If we didn't find a cache entry for this quadruple, then
864 	 * create one.  If we don't have one already preallocated,
865 	 * then drop the cache lock, create one, and then start over.
866 	 * If we did have a preallocated entry, then just add it to
867 	 * the front of the list.
868 	 */
869 	if (ch == NULL) {
870 		if (newch == NULL) {
871 			mutex_exit(&nfscl->nfscl_chtable4_lock);
872 			newch = kmem_alloc(sizeof (*newch), KM_SLEEP);
873 			newch->ch_timesused = 0;
874 			newch->ch_prog = ci->cl_prog;
875 			newch->ch_vers = ci->cl_vers;
876 			newch->ch_dev = svp->sv_knconf->knc_rdev;
877 			newch->ch_protofmly = kmem_alloc(
878 			    strlen(svp->sv_knconf->knc_protofmly) + 1,
879 			    KM_SLEEP);
880 			(void) strcpy(newch->ch_protofmly,
881 			    svp->sv_knconf->knc_protofmly);
882 			newch->ch_list = NULL;
883 			goto top;
884 		}
885 		ch = newch;
886 		newch = NULL;
887 		ch->ch_next = nfscl->nfscl_chtable4;
888 		nfscl->nfscl_chtable4 = ch;
889 	/*
890 	 * We found a cache entry, but if it isn't on the front of the
891 	 * list, then move it to the front of the list to try to take
892 	 * advantage of locality of operations.
893 	 */
894 	} else if (ch != nfscl->nfscl_chtable4) {
895 		*plistp = ch->ch_next;
896 		ch->ch_next = nfscl->nfscl_chtable4;
897 		nfscl->nfscl_chtable4 = ch;
898 	}
899 
900 	/*
901 	 * If there was a free client handle cached, then remove it
902 	 * from the list, init it, and use it.
903 	 */
904 	if (ch->ch_list != NULL) {
905 		cp = ch->ch_list;
906 		ch->ch_list = cp->ch_list;
907 		mutex_exit(&nfscl->nfscl_chtable4_lock);
908 		if (newch != NULL) {
909 			kmem_free(newch->ch_protofmly,
910 			    strlen(newch->ch_protofmly) + 1);
911 			kmem_free(newch, sizeof (*newch));
912 		}
913 		(void) clnt_tli_kinit(cp->ch_client, svp->sv_knconf,
914 		    &svp->sv_addr, ci->cl_readsize, ci->cl_retrans, cr);
915 
916 		/*
917 		 * Get an auth handle.
918 		 */
919 		error = authget(svp, cp->ch_client, cr);
920 		if (error || cp->ch_client->cl_auth == NULL) {
921 			CLNT_DESTROY(cp->ch_client);
922 			kmem_cache_free(chtab4_cache, cp);
923 			return ((error != 0) ? error : EINTR);
924 		}
925 		ch->ch_timesused++;
926 		*newcl = cp->ch_client;
927 		*chp = cp;
928 		return (0);
929 	}
930 
931 	/*
932 	 * There weren't any free client handles which fit, so allocate
933 	 * a new one and use that.
934 	 */
935 #ifdef DEBUG
936 	atomic_add_64(&nfscl->nfscl_stat.clalloc.value.ui64, 1);
937 #endif
938 	mutex_exit(&nfscl->nfscl_chtable4_lock);
939 
940 	nfscl->nfscl_stat.cltoomany.value.ui64++;
941 	if (newch != NULL) {
942 		kmem_free(newch->ch_protofmly, strlen(newch->ch_protofmly) + 1);
943 		kmem_free(newch, sizeof (*newch));
944 	}
945 
946 	cp = kmem_cache_alloc(chtab4_cache, KM_SLEEP);
947 	cp->ch_head = ch;
948 
949 	sigintr(&smask, (int)ci->cl_flags & MI4_INT);
950 	error = clnt_tli_kcreate(svp->sv_knconf, &svp->sv_addr, ci->cl_prog,
951 	    ci->cl_vers, ci->cl_readsize, ci->cl_retrans, cr, &cp->ch_client);
952 	sigunintr(&smask);
953 
954 	if (error != 0) {
955 		kmem_cache_free(chtab4_cache, cp);
956 #ifdef DEBUG
957 		atomic_add_64(&nfscl->nfscl_stat.clalloc.value.ui64, -1);
958 #endif
959 		/*
960 		 * Warning is unnecessary if error is EINTR.
961 		 */
962 		if (error != EINTR) {
963 			nfs_cmn_err(error, CE_WARN,
964 			    "clget: couldn't create handle: %m\n");
965 		}
966 		return (error);
967 	}
968 	(void) CLNT_CONTROL(cp->ch_client, CLSET_PROGRESS, NULL);
969 	auth_destroy(cp->ch_client->cl_auth);
970 
971 	/*
972 	 * Get an auth handle.
973 	 */
974 	error = authget(svp, cp->ch_client, cr);
975 	if (error || cp->ch_client->cl_auth == NULL) {
976 		CLNT_DESTROY(cp->ch_client);
977 		kmem_cache_free(chtab4_cache, cp);
978 #ifdef DEBUG
979 		atomic_add_64(&nfscl->nfscl_stat.clalloc.value.ui64, -1);
980 #endif
981 		return ((error != 0) ? error : EINTR);
982 	}
983 	ch->ch_timesused++;
984 	*newcl = cp->ch_client;
985 	ASSERT(cp->ch_client->cl_nosignal == FALSE);
986 	*chp = cp;
987 	return (0);
988 }
989 
990 static int
991 nfs_clget4(mntinfo4_t *mi, servinfo4_t *svp, cred_t *cr, CLIENT **newcl,
992     struct chtab **chp, struct nfs4_clnt *nfscl)
993 {
994 	clinfo_t ci;
995 	bool_t is_recov;
996 	int firstcall, error = 0;
997 
998 	/*
999 	 * Set read buffer size to rsize
1000 	 * and add room for RPC headers.
1001 	 */
1002 	ci.cl_readsize = mi->mi_tsize;
1003 	if (ci.cl_readsize != 0)
1004 		ci.cl_readsize += (RPC_MAXDATASIZE - NFS_MAXDATA);
1005 
1006 	/*
1007 	 * If soft mount and server is down just try once.
1008 	 * meaning: do not retransmit.
1009 	 */
1010 	if (!(mi->mi_flags & MI4_HARD) && (mi->mi_flags & MI4_DOWN))
1011 		ci.cl_retrans = 0;
1012 	else
1013 		ci.cl_retrans = mi->mi_retrans;
1014 
1015 	ci.cl_prog = mi->mi_prog;
1016 	ci.cl_vers = mi->mi_vers;
1017 	ci.cl_flags = mi->mi_flags;
1018 
1019 	/*
1020 	 * clget4 calls authget() to get an auth handle. For RPCSEC_GSS
1021 	 * security flavor, the client tries to establish a security context
1022 	 * by contacting the server. If the connection is timed out or reset,
1023 	 * e.g. server reboot, we will try again.
1024 	 */
1025 	is_recov = (curthread == mi->mi_recovthread);
1026 	firstcall = 1;
1027 
1028 	do {
1029 		error = clget4(&ci, svp, cr, newcl, chp, nfscl);
1030 
1031 		if (error == 0)
1032 			break;
1033 
1034 		/*
1035 		 * For forced unmount and zone shutdown, bail out but
1036 		 * let the recovery thread do one more transmission.
1037 		 */
1038 		if ((FS_OR_ZONE_GONE4(mi->mi_vfsp)) &&
1039 		    (!is_recov || !firstcall)) {
1040 			error = EIO;
1041 			break;
1042 		}
1043 
1044 		/* do not retry for soft mount */
1045 		if (!(mi->mi_flags & MI4_HARD))
1046 			break;
1047 
1048 		/* let the caller deal with the failover case */
1049 		if (FAILOVER_MOUNT4(mi))
1050 			break;
1051 
1052 		firstcall = 0;
1053 
1054 	} while (error == ETIMEDOUT || error == ECONNRESET);
1055 
1056 	return (error);
1057 }
1058 
1059 void
1060 clfree4(CLIENT *cl, struct chtab *cp, struct nfs4_clnt *nfscl)
1061 {
1062 	if (cl->cl_auth != NULL) {
1063 		sec_clnt_freeh(cl->cl_auth);
1064 		cl->cl_auth = NULL;
1065 	}
1066 
1067 	/*
1068 	 * Timestamp this cache entry so that we know when it was last
1069 	 * used.
1070 	 */
1071 	cp->ch_freed = gethrestime_sec();
1072 
1073 	/*
1074 	 * Add the free client handle to the front of the list.
1075 	 * This way, the list will be sorted in youngest to oldest
1076 	 * order.
1077 	 */
1078 	mutex_enter(&nfscl->nfscl_chtable4_lock);
1079 	cp->ch_list = cp->ch_head->ch_list;
1080 	cp->ch_head->ch_list = cp;
1081 	mutex_exit(&nfscl->nfscl_chtable4_lock);
1082 }
1083 
1084 #define	CL_HOLDTIME	60	/* time to hold client handles */
1085 
1086 static void
1087 clreclaim4_zone(struct nfs4_clnt *nfscl, uint_t cl_holdtime)
1088 {
1089 	struct chhead *ch;
1090 	struct chtab *cp;	/* list of objects that can be reclaimed */
1091 	struct chtab *cpe;
1092 	struct chtab *cpl;
1093 	struct chtab **cpp;
1094 #ifdef DEBUG
1095 	int n = 0;
1096 	clstat4_debug.clreclaim.value.ui64++;
1097 #endif
1098 
1099 	/*
1100 	 * Need to reclaim some memory, so step through the cache
1101 	 * looking through the lists for entries which can be freed.
1102 	 */
1103 	cp = NULL;
1104 
1105 	mutex_enter(&nfscl->nfscl_chtable4_lock);
1106 
1107 	/*
1108 	 * Here we step through each non-NULL quadruple and start to
1109 	 * construct the reclaim list pointed to by cp.  Note that
1110 	 * cp will contain all eligible chtab entries.  When this traversal
1111 	 * completes, chtab entries from the last quadruple will be at the
1112 	 * front of cp and entries from previously inspected quadruples have
1113 	 * been appended to the rear of cp.
1114 	 */
1115 	for (ch = nfscl->nfscl_chtable4; ch != NULL; ch = ch->ch_next) {
1116 		if (ch->ch_list == NULL)
1117 			continue;
1118 		/*
1119 		 * Search each list for entries older then
1120 		 * cl_holdtime seconds.  The lists are maintained
1121 		 * in youngest to oldest order so that when the
1122 		 * first entry is found which is old enough, then
1123 		 * all of the rest of the entries on the list will
1124 		 * be old enough as well.
1125 		 */
1126 		cpl = ch->ch_list;
1127 		cpp = &ch->ch_list;
1128 		while (cpl != NULL &&
1129 			cpl->ch_freed + cl_holdtime > gethrestime_sec()) {
1130 			cpp = &cpl->ch_list;
1131 			cpl = cpl->ch_list;
1132 		}
1133 		if (cpl != NULL) {
1134 			*cpp = NULL;
1135 			if (cp != NULL) {
1136 				cpe = cpl;
1137 				while (cpe->ch_list != NULL)
1138 					cpe = cpe->ch_list;
1139 				cpe->ch_list = cp;
1140 			}
1141 			cp = cpl;
1142 		}
1143 	}
1144 
1145 	mutex_exit(&nfscl->nfscl_chtable4_lock);
1146 
1147 	/*
1148 	 * If cp is empty, then there is nothing to reclaim here.
1149 	 */
1150 	if (cp == NULL)
1151 		return;
1152 
1153 	/*
1154 	 * Step through the list of entries to free, destroying each client
1155 	 * handle and kmem_free'ing the memory for each entry.
1156 	 */
1157 	while (cp != NULL) {
1158 #ifdef DEBUG
1159 		n++;
1160 #endif
1161 		CLNT_DESTROY(cp->ch_client);
1162 		cpl = cp->ch_list;
1163 		kmem_cache_free(chtab4_cache, cp);
1164 		cp = cpl;
1165 	}
1166 
1167 #ifdef DEBUG
1168 	/*
1169 	 * Update clalloc so that nfsstat shows the current number
1170 	 * of allocated client handles.
1171 	 */
1172 	atomic_add_64(&nfscl->nfscl_stat.clalloc.value.ui64, -n);
1173 #endif
1174 }
1175 
1176 /* ARGSUSED */
1177 static void
1178 clreclaim4(void *all)
1179 {
1180 	struct nfs4_clnt *nfscl;
1181 
1182 	/*
1183 	 * The system is low on memory; go through and try to reclaim some from
1184 	 * every zone on the system.
1185 	 */
1186 	mutex_enter(&nfs4_clnt_list_lock);
1187 	nfscl = list_head(&nfs4_clnt_list);
1188 	for (; nfscl != NULL; nfscl = list_next(&nfs4_clnt_list, nfscl))
1189 		clreclaim4_zone(nfscl, CL_HOLDTIME);
1190 	mutex_exit(&nfs4_clnt_list_lock);
1191 }
1192 
1193 /*
1194  * Minimum time-out values indexed by call type
1195  * These units are in "eights" of a second to avoid multiplies
1196  */
1197 static unsigned int minimum_timeo[] = {
1198 	6, 7, 10
1199 };
1200 
1201 #define	SHORTWAIT	(NFS_COTS_TIMEO / 10)
1202 
1203 /*
1204  * Back off for retransmission timeout, MAXTIMO is in hz of a sec
1205  */
1206 #define	MAXTIMO	(20*hz)
1207 #define	backoff(tim)	(((tim) < MAXTIMO) ? dobackoff(tim) : (tim))
1208 #define	dobackoff(tim)	((((tim) << 1) > MAXTIMO) ? MAXTIMO : ((tim) << 1))
1209 
1210 static int
1211 nfs4_rfscall(mntinfo4_t *mi, rpcproc_t which, xdrproc_t xdrargs, caddr_t argsp,
1212     xdrproc_t xdrres, caddr_t resp, cred_t *icr, int *doqueue,
1213     enum clnt_stat *rpc_statusp, int flags, struct nfs4_clnt *nfscl)
1214 {
1215 	CLIENT *client;
1216 	struct chtab *ch;
1217 	cred_t *cr = icr;
1218 	struct rpc_err rpcerr;
1219 	enum clnt_stat status;
1220 	int error;
1221 	struct timeval wait;
1222 	int timeo;		/* in units of hz */
1223 	bool_t tryagain, is_recov;
1224 	bool_t cred_cloned = FALSE;
1225 	k_sigset_t smask;
1226 	servinfo4_t *svp;
1227 #ifdef DEBUG
1228 	char *bufp;
1229 #endif
1230 	int firstcall;
1231 
1232 	rpcerr.re_status = RPC_SUCCESS;
1233 
1234 	/*
1235 	 * If we know that we are rebooting then let's
1236 	 * not bother with doing any over the wireness.
1237 	 */
1238 	mutex_enter(&mi->mi_lock);
1239 	if (mi->mi_flags & MI4_SHUTDOWN) {
1240 		mutex_exit(&mi->mi_lock);
1241 		return (EIO);
1242 	}
1243 	mutex_exit(&mi->mi_lock);
1244 
1245 	/* For TSOL, use a new cred which has net_mac_aware flag */
1246 	if (!cred_cloned && is_system_labeled()) {
1247 		cred_cloned = TRUE;
1248 		cr = crdup(icr);
1249 		(void) setpflags(NET_MAC_AWARE, 1, cr);
1250 	}
1251 
1252 	/*
1253 	 * clget() calls clnt_tli_kinit() which clears the xid, so we
1254 	 * are guaranteed to reprocess the retry as a new request.
1255 	 */
1256 	svp = mi->mi_curr_serv;
1257 	rpcerr.re_errno = nfs_clget4(mi, svp, cr, &client, &ch, nfscl);
1258 	if (rpcerr.re_errno != 0)
1259 		return (rpcerr.re_errno);
1260 
1261 	timeo = (mi->mi_timeo * hz) / 10;
1262 
1263 	/*
1264 	 * If hard mounted fs, retry call forever unless hard error
1265 	 * occurs.
1266 	 *
1267 	 * For forced unmount, let the recovery thread through but return
1268 	 * an error for all others.  This is so that user processes can
1269 	 * exit quickly.  The recovery thread bails out after one
1270 	 * transmission so that it can tell if it needs to continue.
1271 	 *
1272 	 * For zone shutdown, behave as above to encourage quick
1273 	 * process exit, but also fail quickly when servers have
1274 	 * timed out before and reduce the timeouts.
1275 	 */
1276 	is_recov = (curthread == mi->mi_recovthread);
1277 	firstcall = 1;
1278 	do {
1279 		tryagain = FALSE;
1280 
1281 		NFS4_DEBUG(nfs4_rfscall_debug, (CE_NOTE,
1282 			"nfs4_rfscall: vfs_flag=0x%x, %s",
1283 			mi->mi_vfsp->vfs_flag,
1284 			is_recov ? "recov thread" : "not recov thread"));
1285 
1286 		/*
1287 		 * It's possible while we're retrying the admin
1288 		 * decided to reboot.
1289 		 */
1290 		mutex_enter(&mi->mi_lock);
1291 		if (mi->mi_flags & MI4_SHUTDOWN) {
1292 			mutex_exit(&mi->mi_lock);
1293 			clfree4(client, ch, nfscl);
1294 			if (cred_cloned)
1295 				crfree(cr);
1296 			return (EIO);
1297 		}
1298 		mutex_exit(&mi->mi_lock);
1299 
1300 		if ((mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) &&
1301 		    (!is_recov || !firstcall)) {
1302 			clfree4(client, ch, nfscl);
1303 			if (cred_cloned)
1304 				crfree(cr);
1305 			return (EIO);
1306 		}
1307 
1308 		if (zone_status_get(curproc->p_zone) >= ZONE_IS_SHUTTING_DOWN) {
1309 			mutex_enter(&mi->mi_lock);
1310 			if ((mi->mi_flags & MI4_TIMEDOUT) ||
1311 			    !is_recov || !firstcall) {
1312 				mutex_exit(&mi->mi_lock);
1313 				clfree4(client, ch, nfscl);
1314 				if (cred_cloned)
1315 					crfree(cr);
1316 				return (EIO);
1317 			}
1318 			mutex_exit(&mi->mi_lock);
1319 			timeo = (MIN(mi->mi_timeo, SHORTWAIT) * hz) / 10;
1320 		}
1321 
1322 		firstcall = 0;
1323 		TICK_TO_TIMEVAL(timeo, &wait);
1324 
1325 		/*
1326 		 * Mask out all signals except SIGHUP, SIGINT, SIGQUIT
1327 		 * and SIGTERM. (Preserving the existing masks).
1328 		 * Mask out SIGINT if mount option nointr is specified.
1329 		 */
1330 		sigintr(&smask, (int)mi->mi_flags & MI4_INT);
1331 		if (!(mi->mi_flags & MI4_INT))
1332 			client->cl_nosignal = TRUE;
1333 
1334 		/*
1335 		 * If there is a current signal, then don't bother
1336 		 * even trying to send out the request because we
1337 		 * won't be able to block waiting for the response.
1338 		 * Simply assume RPC_INTR and get on with it.
1339 		 */
1340 		if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING))
1341 			status = RPC_INTR;
1342 		else {
1343 			status = CLNT_CALL(client, which, xdrargs, argsp,
1344 			    xdrres, resp, wait);
1345 		}
1346 
1347 		if (!(mi->mi_flags & MI4_INT))
1348 			client->cl_nosignal = FALSE;
1349 		/*
1350 		 * restore original signal mask
1351 		 */
1352 		sigunintr(&smask);
1353 
1354 		switch (status) {
1355 		case RPC_SUCCESS:
1356 			break;
1357 
1358 		case RPC_INTR:
1359 			/*
1360 			 * There is no way to recover from this error,
1361 			 * even if mount option nointr is specified.
1362 			 * SIGKILL, for example, cannot be blocked.
1363 			 */
1364 			rpcerr.re_status = RPC_INTR;
1365 			rpcerr.re_errno = EINTR;
1366 			break;
1367 
1368 		case RPC_UDERROR:
1369 			/*
1370 			 * If the NFS server is local (vold) and
1371 			 * it goes away then we get RPC_UDERROR.
1372 			 * This is a retryable error, so we would
1373 			 * loop, so check to see if the specific
1374 			 * error was ECONNRESET, indicating that
1375 			 * target did not exist at all.  If so,
1376 			 * return with RPC_PROGUNAVAIL and
1377 			 * ECONNRESET to indicate why.
1378 			 */
1379 			CLNT_GETERR(client, &rpcerr);
1380 			if (rpcerr.re_errno == ECONNRESET) {
1381 				rpcerr.re_status = RPC_PROGUNAVAIL;
1382 				rpcerr.re_errno = ECONNRESET;
1383 				break;
1384 			}
1385 			/*FALLTHROUGH*/
1386 
1387 		default:		/* probably RPC_TIMEDOUT */
1388 
1389 			if (IS_UNRECOVERABLE_RPC(status))
1390 				break;
1391 
1392 			/*
1393 			 * increment server not responding count
1394 			 */
1395 			mutex_enter(&mi->mi_lock);
1396 			mi->mi_noresponse++;
1397 			mutex_exit(&mi->mi_lock);
1398 #ifdef DEBUG
1399 			nfscl->nfscl_stat.noresponse.value.ui64++;
1400 #endif
1401 			/*
1402 			 * On zone shutdown, mark server dead and move on.
1403 			 */
1404 			if (zone_status_get(curproc->p_zone) >=
1405 			    ZONE_IS_SHUTTING_DOWN) {
1406 				mutex_enter(&mi->mi_lock);
1407 				mi->mi_flags |= MI4_TIMEDOUT;
1408 				mutex_exit(&mi->mi_lock);
1409 				clfree4(client, ch, nfscl);
1410 				if (cred_cloned)
1411 					crfree(cr);
1412 				return (EIO);
1413 			}
1414 
1415 			/*
1416 			 * NFS client failover support:
1417 			 * return and let the caller take care of
1418 			 * failover.  We only return for failover mounts
1419 			 * because otherwise we want the "not responding"
1420 			 * message, the timer updates, etc.
1421 			 */
1422 			if (mi->mi_vers == 4 && FAILOVER_MOUNT4(mi) &&
1423 			    (error = try_failover(status)) != 0) {
1424 				clfree4(client, ch, nfscl);
1425 				if (cred_cloned)
1426 					crfree(cr);
1427 				*rpc_statusp = status;
1428 				return (error);
1429 			}
1430 
1431 			if (flags & RFSCALL_SOFT)
1432 				break;
1433 
1434 			tryagain = TRUE;
1435 
1436 			/*
1437 			 * The call is in progress (over COTS).
1438 			 * Try the CLNT_CALL again, but don't
1439 			 * print a noisy error message.
1440 			 */
1441 			if (status == RPC_INPROGRESS)
1442 				break;
1443 
1444 			timeo = backoff(timeo);
1445 			mutex_enter(&mi->mi_lock);
1446 			if (!(mi->mi_flags & MI4_PRINTED)) {
1447 				mi->mi_flags |= MI4_PRINTED;
1448 				mutex_exit(&mi->mi_lock);
1449 				nfs4_queue_fact(RF_SRV_NOT_RESPOND, mi, 0, 0, 0,
1450 				    FALSE, NULL, 0, NULL);
1451 			} else
1452 				mutex_exit(&mi->mi_lock);
1453 
1454 			if (*doqueue && curproc->p_sessp->s_vp != NULL) {
1455 				*doqueue = 0;
1456 				if (!(mi->mi_flags & MI4_NOPRINT))
1457 					nfs4_queue_fact(RF_SRV_NOT_RESPOND, mi,
1458 					    0, 0, 0, FALSE, NULL, 0, NULL);
1459 			}
1460 		}
1461 	} while (tryagain);
1462 
1463 	DTRACE_PROBE2(nfs4__rfscall_debug, enum clnt_stat, status,
1464 			int, rpcerr.re_errno);
1465 
1466 	if (status != RPC_SUCCESS) {
1467 		zoneid_t zoneid = mi->mi_zone->zone_id;
1468 
1469 		/*
1470 		 * Let soft mounts use the timed out message.
1471 		 */
1472 		if (status == RPC_INPROGRESS)
1473 			status = RPC_TIMEDOUT;
1474 		nfscl->nfscl_stat.badcalls.value.ui64++;
1475 		if (status != RPC_INTR) {
1476 			mutex_enter(&mi->mi_lock);
1477 			mi->mi_flags |= MI4_DOWN;
1478 			mutex_exit(&mi->mi_lock);
1479 			CLNT_GETERR(client, &rpcerr);
1480 #ifdef DEBUG
1481 			bufp = clnt_sperror(client, svp->sv_hostname);
1482 			zprintf(zoneid, "NFS%d %s failed for %s\n",
1483 			    mi->mi_vers, mi->mi_rfsnames[which], bufp);
1484 			if (curproc->p_sessp->s_vp != NULL) {
1485 				if (!(mi->mi_flags & MI4_NOPRINT)) {
1486 					uprintf("NFS%d %s failed for %s\n",
1487 					    mi->mi_vers, mi->mi_rfsnames[which],
1488 					    bufp);
1489 				}
1490 			}
1491 			kmem_free(bufp, MAXPATHLEN);
1492 #else
1493 			zprintf(zoneid,
1494 			    "NFS %s failed for server %s: error %d (%s)\n",
1495 			    mi->mi_rfsnames[which], svp->sv_hostname,
1496 			    status, clnt_sperrno(status));
1497 			if (curproc->p_sessp->s_vp != NULL) {
1498 				if (!(mi->mi_flags & MI4_NOPRINT)) {
1499 					uprintf(
1500 				"NFS %s failed for server %s: error %d (%s)\n",
1501 					    mi->mi_rfsnames[which],
1502 					    svp->sv_hostname, status,
1503 					    clnt_sperrno(status));
1504 				}
1505 			}
1506 #endif
1507 			/*
1508 			 * when CLNT_CALL() fails with RPC_AUTHERROR,
1509 			 * re_errno is set appropriately depending on
1510 			 * the authentication error
1511 			 */
1512 			if (status == RPC_VERSMISMATCH ||
1513 			    status == RPC_PROGVERSMISMATCH)
1514 				rpcerr.re_errno = EIO;
1515 		}
1516 	} else {
1517 		/*
1518 		 * Test the value of mi_down and mi_printed without
1519 		 * holding the mi_lock mutex.  If they are both zero,
1520 		 * then it is okay to skip the down and printed
1521 		 * processing.  This saves on a mutex_enter and
1522 		 * mutex_exit pair for a normal, successful RPC.
1523 		 * This was just complete overhead.
1524 		 */
1525 		if (mi->mi_flags & (MI4_DOWN | MI4_PRINTED)) {
1526 			mutex_enter(&mi->mi_lock);
1527 			mi->mi_flags &= ~MI4_DOWN;
1528 			if (mi->mi_flags & MI4_PRINTED) {
1529 				mi->mi_flags &= ~MI4_PRINTED;
1530 				mutex_exit(&mi->mi_lock);
1531 				if (!(mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED))
1532 					nfs4_queue_fact(RF_SRV_OK, mi, 0, 0,
1533 					    0, FALSE, NULL, 0, NULL);
1534 			} else
1535 				mutex_exit(&mi->mi_lock);
1536 		}
1537 
1538 		if (*doqueue == 0) {
1539 			if (!(mi->mi_flags & MI4_NOPRINT) &&
1540 			    !(mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED))
1541 				nfs4_queue_fact(RF_SRV_OK, mi, 0, 0, 0,
1542 				    FALSE, NULL, 0, NULL);
1543 
1544 			*doqueue = 1;
1545 		}
1546 	}
1547 
1548 	clfree4(client, ch, nfscl);
1549 	if (cred_cloned)
1550 		crfree(cr);
1551 
1552 	ASSERT(rpcerr.re_status == RPC_SUCCESS || rpcerr.re_errno != 0);
1553 
1554 	TRACE_1(TR_FAC_NFS, TR_RFSCALL_END, "nfs4_rfscall_end:errno %d",
1555 	    rpcerr.re_errno);
1556 
1557 	*rpc_statusp = status;
1558 	return (rpcerr.re_errno);
1559 }
1560 
1561 /*
1562  * rfs4call - general wrapper for RPC calls initiated by the client
1563  */
1564 void
1565 rfs4call(mntinfo4_t *mi, COMPOUND4args_clnt *argsp, COMPOUND4res_clnt *resp,
1566 	cred_t *cr, int *doqueue, int flags, nfs4_error_t *ep)
1567 {
1568 	int i, error;
1569 	enum clnt_stat rpc_status = NFS4_OK;
1570 	int num_resops;
1571 	struct nfs4_clnt *nfscl;
1572 
1573 	ASSERT(nfs_zone() == mi->mi_zone);
1574 	nfscl = zone_getspecific(nfs4clnt_zone_key, nfs_zone());
1575 	ASSERT(nfscl != NULL);
1576 
1577 	nfscl->nfscl_stat.calls.value.ui64++;
1578 	mi->mi_reqs[NFSPROC4_COMPOUND].value.ui64++;
1579 
1580 	/* Set up the results struct for XDR usage */
1581 	resp->argsp = argsp;
1582 	resp->array = NULL;
1583 	resp->status = 0;
1584 	resp->decode_len = 0;
1585 
1586 	error = nfs4_rfscall(mi, NFSPROC4_COMPOUND,
1587 		xdr_COMPOUND4args_clnt, (caddr_t)argsp,
1588 		xdr_COMPOUND4res_clnt, (caddr_t)resp, cr,
1589 		doqueue, &rpc_status, flags, nfscl);
1590 
1591 	/* Return now if it was an RPC error */
1592 	if (error) {
1593 		ep->error = error;
1594 		ep->stat = resp->status;
1595 		ep->rpc_status = rpc_status;
1596 		return;
1597 	}
1598 
1599 	/* else we'll count the processed operations */
1600 	num_resops = resp->decode_len;
1601 	for (i = 0; i < num_resops; i++) {
1602 		/*
1603 		 * Count the individual operations
1604 		 * processed by the server.
1605 		 */
1606 		if (resp->array[i].resop >= NFSPROC4_NULL &&
1607 		    resp->array[i].resop <= OP_WRITE)
1608 			mi->mi_reqs[resp->array[i].resop].value.ui64++;
1609 	}
1610 
1611 	ep->error = 0;
1612 	ep->stat = resp->status;
1613 	ep->rpc_status = rpc_status;
1614 }
1615 
1616 /*
1617  * nfs4rename_update - updates stored state after a rename.  Currently this
1618  * is the path of the object and anything under it, and the filehandle of
1619  * the renamed object.
1620  */
1621 void
1622 nfs4rename_update(vnode_t *renvp, vnode_t *ndvp, nfs_fh4 *nfh4p, char *nnm)
1623 {
1624 	sfh4_update(VTOR4(renvp)->r_fh, nfh4p);
1625 	fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name, nnm);
1626 }
1627 
1628 /*
1629  * Routine to look up the filehandle for the given path and rootvp.
1630  *
1631  * Return values:
1632  * - success: returns zero and *statp is set to NFS4_OK, and *fhp is
1633  *   updated.
1634  * - error: return value (errno value) and/or *statp is set appropriately.
1635  */
1636 #define	RML_ORDINARY	1
1637 #define	RML_NAMED_ATTR	2
1638 #define	RML_ATTRDIR	3
1639 
1640 static void
1641 remap_lookup(nfs4_fname_t *fname, vnode_t *rootvp,
1642 	int filetype, cred_t *cr,
1643 	nfs_fh4 *fhp, nfs4_ga_res_t *garp,	/* fh, attrs for object */
1644 	nfs_fh4 *pfhp, nfs4_ga_res_t *pgarp,	/* fh, attrs for parent */
1645 	nfs4_error_t *ep)
1646 {
1647 	COMPOUND4args_clnt args;
1648 	COMPOUND4res_clnt res;
1649 	nfs_argop4 *argop;
1650 	nfs_resop4 *resop;
1651 	int num_argops;
1652 	lookup4_param_t lookuparg;
1653 	nfs_fh4 *tmpfhp;
1654 	int doqueue = 1;
1655 	char *path;
1656 	mntinfo4_t *mi;
1657 
1658 	ASSERT(fname != NULL);
1659 	ASSERT(rootvp->v_type == VDIR);
1660 
1661 	mi = VTOMI4(rootvp);
1662 	path = fn_path(fname);
1663 	switch (filetype) {
1664 	case RML_NAMED_ATTR:
1665 		lookuparg.l4_getattrs = LKP4_LAST_NAMED_ATTR;
1666 		args.ctag = TAG_REMAP_LOOKUP_NA;
1667 		break;
1668 	case RML_ATTRDIR:
1669 		lookuparg.l4_getattrs = LKP4_LAST_ATTRDIR;
1670 		args.ctag = TAG_REMAP_LOOKUP_AD;
1671 		break;
1672 	case RML_ORDINARY:
1673 		lookuparg.l4_getattrs = LKP4_ALL_ATTRIBUTES;
1674 		args.ctag = TAG_REMAP_LOOKUP;
1675 		break;
1676 	default:
1677 		ep->error = EINVAL;
1678 		return;
1679 	}
1680 	lookuparg.argsp = &args;
1681 	lookuparg.resp = &res;
1682 	lookuparg.header_len = 1;	/* Putfh */
1683 	lookuparg.trailer_len = 0;
1684 	lookuparg.ga_bits = NFS4_VATTR_MASK;
1685 	lookuparg.mi = VTOMI4(rootvp);
1686 
1687 	(void) nfs4lookup_setup(path, &lookuparg, 1);
1688 
1689 	/* 0: putfh directory */
1690 	argop = args.array;
1691 	argop[0].argop = OP_CPUTFH;
1692 	argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(rootvp)->r_fh;
1693 
1694 	num_argops = args.array_len;
1695 
1696 	rfs4call(mi, &args, &res, cr, &doqueue, RFSCALL_SOFT, ep);
1697 
1698 	if (ep->error || res.status != NFS4_OK)
1699 		goto exit;
1700 
1701 	/* get the object filehandle */
1702 	resop = &res.array[res.array_len - 2];
1703 	if (resop->resop != OP_GETFH) {
1704 		nfs4_queue_event(RE_FAIL_REMAP_OP, mi, NULL,
1705 		    0, NULL, NULL, 0, NULL, 0, TAG_NONE, TAG_NONE, 0, 0);
1706 		ep->stat = NFS4ERR_SERVERFAULT;
1707 		goto exit;
1708 	}
1709 	tmpfhp = &resop->nfs_resop4_u.opgetfh.object;
1710 	if (tmpfhp->nfs_fh4_len > NFS4_FHSIZE) {
1711 		nfs4_queue_event(RE_FAIL_REMAP_LEN, mi, NULL,
1712 		    tmpfhp->nfs_fh4_len, NULL, NULL, 0, NULL, 0, TAG_NONE,
1713 		    TAG_NONE, 0, 0);
1714 		ep->stat = NFS4ERR_SERVERFAULT;
1715 		goto exit;
1716 	}
1717 	fhp->nfs_fh4_val = kmem_alloc(tmpfhp->nfs_fh4_len, KM_SLEEP);
1718 	nfs_fh4_copy(tmpfhp, fhp);
1719 
1720 	/* get the object attributes */
1721 	resop = &res.array[res.array_len - 1];
1722 	if (garp && resop->resop == OP_GETATTR)
1723 		*garp = resop->nfs_resop4_u.opgetattr.ga_res;
1724 
1725 	/* See if there are enough fields in the response for parent info */
1726 	if ((int)res.array_len - 5 <= 0)
1727 		goto exit;
1728 
1729 	/* get the parent filehandle */
1730 	resop = &res.array[res.array_len - 5];
1731 	if (resop->resop != OP_GETFH) {
1732 		nfs4_queue_event(RE_FAIL_REMAP_OP, mi, NULL,
1733 		    0, NULL, NULL, 0, NULL, 0, TAG_NONE, TAG_NONE, 0, 0);
1734 		ep->stat = NFS4ERR_SERVERFAULT;
1735 		goto exit;
1736 	}
1737 	tmpfhp = &resop->nfs_resop4_u.opgetfh.object;
1738 	if (tmpfhp->nfs_fh4_len > NFS4_FHSIZE) {
1739 		nfs4_queue_event(RE_FAIL_REMAP_LEN, mi, NULL,
1740 		    tmpfhp->nfs_fh4_len, NULL, NULL, 0, NULL, 0, TAG_NONE,
1741 		    TAG_NONE, 0, 0);
1742 		ep->stat = NFS4ERR_SERVERFAULT;
1743 		goto exit;
1744 	}
1745 	pfhp->nfs_fh4_val = kmem_alloc(tmpfhp->nfs_fh4_len, KM_SLEEP);
1746 	nfs_fh4_copy(tmpfhp, pfhp);
1747 
1748 	/* get the parent attributes */
1749 	resop = &res.array[res.array_len - 4];
1750 	if (pgarp && resop->resop == OP_GETATTR)
1751 		*pgarp = resop->nfs_resop4_u.opgetattr.ga_res;
1752 
1753 exit:
1754 	/*
1755 	 * It is too hard to remember where all the OP_LOOKUPs are
1756 	 */
1757 	nfs4args_lookup_free(argop, num_argops);
1758 	kmem_free(argop, lookuparg.arglen * sizeof (nfs_argop4));
1759 
1760 	if (!ep->error)
1761 		(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1762 	kmem_free(path, strlen(path)+1);
1763 }
1764 
1765 /*
1766  * NFS client failover / volatile filehandle support
1767  *
1768  * Recover the filehandle for the given rnode.
1769  *
1770  * Errors are returned via the nfs4_error_t parameter.
1771  */
1772 
1773 void
1774 nfs4_remap_file(mntinfo4_t *mi, vnode_t *vp, int flags, nfs4_error_t *ep)
1775 {
1776 	rnode4_t *rp = VTOR4(vp);
1777 	vnode_t *rootvp = NULL;
1778 	vnode_t *dvp = NULL;
1779 	cred_t *cr, *cred_otw;
1780 	nfs4_ga_res_t gar, pgar;
1781 	nfs_fh4 newfh = {0, NULL}, newpfh = {0, NULL};
1782 	int filetype = RML_ORDINARY;
1783 	nfs4_recov_state_t recov = {NULL, 0, 0};
1784 	int badfhcount = 0;
1785 	nfs4_open_stream_t *osp = NULL;
1786 	bool_t first_time = TRUE;	/* first time getting OTW cred */
1787 	bool_t last_time = FALSE;	/* last time getting OTW cred */
1788 
1789 	NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
1790 	    "nfs4_remap_file: remapping %s", rnode4info(rp)));
1791 	ASSERT(nfs4_consistent_type(vp));
1792 
1793 	if (vp->v_flag & VROOT) {
1794 		nfs4_remap_root(mi, ep, flags);
1795 		return;
1796 	}
1797 
1798 	/*
1799 	 * Given the root fh, use the path stored in
1800 	 * the rnode to find the fh for the new server.
1801 	 */
1802 	ep->error = VFS_ROOT(mi->mi_vfsp, &rootvp);
1803 	if (ep->error != 0)
1804 		return;
1805 
1806 	cr = curthread->t_cred;
1807 	ASSERT(cr != NULL);
1808 get_remap_cred:
1809 	/*
1810 	 * Releases the osp, if it is provided.
1811 	 * Puts a hold on the cred_otw and the new osp (if found).
1812 	 */
1813 	cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
1814 		&first_time, &last_time);
1815 	ASSERT(cred_otw != NULL);
1816 
1817 	if (rp->r_flags & R4ISXATTR) {
1818 		filetype = RML_NAMED_ATTR;
1819 		(void) vtodv(vp, &dvp, cred_otw, FALSE);
1820 	}
1821 
1822 	if (vp->v_flag & V_XATTRDIR) {
1823 		filetype = RML_ATTRDIR;
1824 	}
1825 
1826 	if (filetype == RML_ORDINARY && rootvp->v_type == VREG) {
1827 		/* file mount, doesn't need a remap */
1828 		goto done;
1829 	}
1830 
1831 again:
1832 	remap_lookup(rp->r_svnode.sv_name, rootvp, filetype, cred_otw,
1833 			&newfh, &gar, &newpfh, &pgar, ep);
1834 
1835 	NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
1836 	    "nfs4_remap_file: remap_lookup returned %d/%d",
1837 	    ep->error, ep->stat));
1838 
1839 	if (last_time == FALSE && ep->error == EACCES) {
1840 		crfree(cred_otw);
1841 		if (dvp != NULL)
1842 			VN_RELE(dvp);
1843 		goto get_remap_cred;
1844 	}
1845 	if (ep->error != 0)
1846 		goto done;
1847 
1848 	switch (ep->stat) {
1849 	case NFS4_OK:
1850 		badfhcount = 0;
1851 		if (recov.rs_flags & NFS4_RS_DELAY_MSG) {
1852 			mutex_enter(&rp->r_statelock);
1853 			rp->r_delay_interval = 0;
1854 			mutex_exit(&rp->r_statelock);
1855 			uprintf("NFS File Available..\n");
1856 		}
1857 		break;
1858 	case NFS4ERR_FHEXPIRED:
1859 	case NFS4ERR_BADHANDLE:
1860 		/*
1861 		 * If we ran into filehandle problems, we should try to
1862 		 * remap the root vnode first and hope life gets better.
1863 		 * But we need to avoid loops.
1864 		 */
1865 		if (badfhcount++ > 0)
1866 			goto done;
1867 		if (newfh.nfs_fh4_len != 0) {
1868 			kmem_free(newfh.nfs_fh4_val, newfh.nfs_fh4_len);
1869 			newfh.nfs_fh4_len = 0;
1870 		}
1871 		if (newpfh.nfs_fh4_len != 0) {
1872 			kmem_free(newpfh.nfs_fh4_val, newpfh.nfs_fh4_len);
1873 			newpfh.nfs_fh4_len = 0;
1874 		}
1875 		/* relative path - remap rootvp then retry */
1876 		VN_RELE(rootvp);
1877 		rootvp = NULL;
1878 		nfs4_remap_root(mi, ep, flags);
1879 		if (ep->error != 0 || ep->stat != NFS4_OK)
1880 			goto done;
1881 		ep->error = VFS_ROOT(mi->mi_vfsp, &rootvp);
1882 		if (ep->error != 0)
1883 			goto done;
1884 		goto again;
1885 	case NFS4ERR_DELAY:
1886 		badfhcount = 0;
1887 		nfs4_set_delay_wait(vp);
1888 		ep->error = nfs4_wait_for_delay(vp, &recov);
1889 		if (ep->error != 0)
1890 			goto done;
1891 		goto again;
1892 	case NFS4ERR_ACCESS:
1893 		/* get new cred, try again */
1894 		if (last_time == TRUE)
1895 			goto done;
1896 		if (dvp != NULL)
1897 			VN_RELE(dvp);
1898 		crfree(cred_otw);
1899 		goto get_remap_cred;
1900 	default:
1901 		goto done;
1902 	}
1903 
1904 	/*
1905 	 * Check on the new and old rnodes before updating;
1906 	 * if the vnode type or size changes, issue a warning
1907 	 * and mark the file dead.
1908 	 */
1909 	mutex_enter(&rp->r_statelock);
1910 	if (flags & NFS4_REMAP_CKATTRS) {
1911 		if (vp->v_type != gar.n4g_va.va_type ||
1912 			(vp->v_type != VDIR &&
1913 			rp->r_size != gar.n4g_va.va_size)) {
1914 			NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
1915 			    "nfs4_remap_file: size %d vs. %d, type %d vs. %d",
1916 			    (int)rp->r_size, (int)gar.n4g_va.va_size,
1917 			    vp->v_type, gar.n4g_va.va_type));
1918 			mutex_exit(&rp->r_statelock);
1919 			nfs4_queue_event(RE_FILE_DIFF, mi,
1920 			    rp->r_server->sv_hostname, 0, vp, NULL, 0, NULL, 0,
1921 			    TAG_NONE, TAG_NONE, 0, 0);
1922 			nfs4_fail_recov(vp, NULL, 0, NFS4_OK);
1923 			goto done;
1924 		}
1925 	}
1926 	ASSERT(gar.n4g_va.va_type != VNON);
1927 	rp->r_server = mi->mi_curr_serv;
1928 
1929 	if (gar.n4g_fsid_valid) {
1930 		(void) nfs_rw_enter_sig(&rp->r_server->sv_lock, RW_READER, 0);
1931 		rp->r_srv_fsid = gar.n4g_fsid;
1932 		if (FATTR4_FSID_EQ(&gar.n4g_fsid, &rp->r_server->sv_fsid))
1933 			rp->r_flags &= ~R4SRVSTUB;
1934 		else
1935 			rp->r_flags |= R4SRVSTUB;
1936 		nfs_rw_exit(&rp->r_server->sv_lock);
1937 #ifdef DEBUG
1938 	} else {
1939 		NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
1940 			"remap_file: fsid attr not provided by server.  rp=%p",
1941 			(void *)rp));
1942 #endif
1943 	}
1944 	mutex_exit(&rp->r_statelock);
1945 	nfs4_attrcache_noinval(vp, &gar, gethrtime()); /* force update */
1946 	sfh4_update(rp->r_fh, &newfh);
1947 	ASSERT(nfs4_consistent_type(vp));
1948 
1949 	/*
1950 	 * If we got parent info, use it to update the parent
1951 	 */
1952 	if (newpfh.nfs_fh4_len != 0) {
1953 		if (rp->r_svnode.sv_dfh != NULL)
1954 			sfh4_update(rp->r_svnode.sv_dfh, &newpfh);
1955 		if (dvp != NULL) {
1956 			/* force update of attrs */
1957 			nfs4_attrcache_noinval(dvp, &pgar, gethrtime());
1958 		}
1959 	}
1960 done:
1961 	if (newfh.nfs_fh4_len != 0)
1962 		kmem_free(newfh.nfs_fh4_val, newfh.nfs_fh4_len);
1963 	if (newpfh.nfs_fh4_len != 0)
1964 		kmem_free(newpfh.nfs_fh4_val, newpfh.nfs_fh4_len);
1965 	if (cred_otw != NULL)
1966 		crfree(cred_otw);
1967 	if (rootvp != NULL)
1968 		VN_RELE(rootvp);
1969 	if (dvp != NULL)
1970 		VN_RELE(dvp);
1971 	if (osp != NULL)
1972 		open_stream_rele(osp, rp);
1973 }
1974 
1975 /*
1976  * Client-side failover support: remap the filehandle for vp if it appears
1977  * necessary.  errors are returned via the nfs4_error_t parameter; though,
1978  * if there is a problem, we will just try again later.
1979  */
1980 
1981 void
1982 nfs4_check_remap(mntinfo4_t *mi, vnode_t *vp, int flags, nfs4_error_t *ep)
1983 {
1984 	if (vp == NULL)
1985 		return;
1986 
1987 	if (!(vp->v_vfsp->vfs_flag & VFS_RDONLY))
1988 		return;
1989 
1990 	if (VTOR4(vp)->r_server == mi->mi_curr_serv)
1991 		return;
1992 
1993 	nfs4_remap_file(mi, vp, flags, ep);
1994 }
1995 
1996 /*
1997  * nfs4_make_dotdot() - find or create a parent vnode of a non-root node.
1998  *
1999  * Our caller has a filehandle for ".." relative to a particular
2000  * directory object.  We want to find or create a parent vnode
2001  * with that filehandle and return it.  We can of course create
2002  * a vnode from this filehandle, but we need to also make sure
2003  * that if ".." is a regular file (i.e. dvp is a V_XATTRDIR)
2004  * that we have a parent FH for future reopens as well.  If
2005  * we have a remap failure, we won't be able to reopen this
2006  * file, but we won't treat that as fatal because a reopen
2007  * is at least unlikely.  Someday nfs4_reopen() should look
2008  * for a missing parent FH and try a remap to recover from it.
2009  *
2010  * need_start_op argument indicates whether this function should
2011  * do a start_op before calling remap_lookup().  This should
2012  * be FALSE, if you are the recovery thread or in an op; otherwise,
2013  * set it to TRUE.
2014  */
2015 int
2016 nfs4_make_dotdot(nfs4_sharedfh_t *fhp, hrtime_t t, vnode_t *dvp,
2017     cred_t *cr, vnode_t **vpp, int need_start_op)
2018 {
2019 	mntinfo4_t *mi = VTOMI4(dvp);
2020 	nfs4_fname_t *np = NULL, *pnp = NULL;
2021 	vnode_t *vp = NULL, *rootvp = NULL;
2022 	rnode4_t *rp;
2023 	nfs_fh4 newfh = {0, NULL}, newpfh = {0, NULL};
2024 	nfs4_ga_res_t gar, pgar;
2025 	vattr_t va, pva;
2026 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
2027 	nfs4_sharedfh_t *sfh = NULL, *psfh = NULL;
2028 	nfs4_recov_state_t recov_state;
2029 
2030 #ifdef DEBUG
2031 	/*
2032 	 * ensure need_start_op is correct
2033 	 */
2034 	{
2035 		int no_need_start_op = (tsd_get(nfs4_tsd_key) ||
2036 		    (curthread == mi->mi_recovthread));
2037 		/* C needs a ^^ operator! */
2038 		ASSERT(((need_start_op) && (!no_need_start_op)) ||
2039 		    ((! need_start_op) && (no_need_start_op)));
2040 	}
2041 #endif
2042 	ASSERT(VTOMI4(dvp)->mi_zone == nfs_zone());
2043 
2044 	NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE,
2045 	    "nfs4_make_dotdot: called with fhp %p, dvp %s", (void *)fhp,
2046 	    rnode4info(VTOR4(dvp))));
2047 
2048 	/*
2049 	 * rootvp might be needed eventually. Holding it now will
2050 	 * ensure that r4find_unlocked() will find it, if ".." is the root.
2051 	 */
2052 	e.error = VFS_ROOT(mi->mi_vfsp, &rootvp);
2053 	if (e.error != 0)
2054 		goto out;
2055 	rp = r4find_unlocked(fhp, mi->mi_vfsp);
2056 	if (rp != NULL) {
2057 		*vpp = RTOV4(rp);
2058 		VN_RELE(rootvp);
2059 		return (0);
2060 	}
2061 
2062 	/*
2063 	 * Since we don't have the rnode, we have to go over the wire.
2064 	 * remap_lookup() can get all of the filehandles and attributes
2065 	 * we need in one operation.
2066 	 */
2067 	np = fn_parent(VTOSV(dvp)->sv_name);
2068 	ASSERT(np != NULL);
2069 
2070 	recov_state.rs_flags = 0;
2071 	recov_state.rs_num_retry_despite_err = 0;
2072 recov_retry:
2073 	if (need_start_op) {
2074 		e.error = nfs4_start_fop(mi, rootvp, NULL, OH_LOOKUP,
2075 		    &recov_state, NULL);
2076 		if (e.error != 0) {
2077 			goto out;
2078 		}
2079 	}
2080 	va.va_type = VNON;
2081 	pva.va_type = VNON;
2082 	remap_lookup(np, rootvp, RML_ORDINARY, cr,
2083 	    &newfh, &gar, &newpfh, &pgar, &e);
2084 	if (nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp)) {
2085 		if (need_start_op) {
2086 			bool_t abort;
2087 
2088 			abort = nfs4_start_recovery(&e, mi,
2089 			    rootvp, NULL, NULL, NULL, OP_LOOKUP, NULL);
2090 			if (abort) {
2091 				nfs4_end_fop(mi, rootvp, NULL, OH_LOOKUP,
2092 				    &recov_state, FALSE);
2093 				if (e.error == 0)
2094 					e.error = EIO;
2095 				goto out;
2096 			}
2097 			nfs4_end_fop(mi, rootvp, NULL, OH_LOOKUP,
2098 			    &recov_state, TRUE);
2099 			goto recov_retry;
2100 		}
2101 		if (e.error == 0)
2102 			e.error = EIO;
2103 		goto out;
2104 	}
2105 
2106 	if (!e.error) {
2107 		va = gar.n4g_va;
2108 		pva = pgar.n4g_va;
2109 	}
2110 
2111 	if ((e.error != 0) ||
2112 	    (va.va_type != VDIR)) {
2113 		if (need_start_op)
2114 			nfs4_end_fop(mi, rootvp, NULL, OH_LOOKUP,
2115 			    &recov_state, FALSE);
2116 		if (e.error == 0)
2117 			e.error = EIO;
2118 		goto out;
2119 	}
2120 
2121 	if (e.stat != NFS4_OK) {
2122 		if (need_start_op)
2123 			nfs4_end_fop(mi, rootvp, NULL, OH_LOOKUP,
2124 			    &recov_state, FALSE);
2125 		e.error = EIO;
2126 		goto out;
2127 	}
2128 
2129 	/*
2130 	 * It is possible for remap_lookup() to return with no error,
2131 	 * but without providing the parent filehandle and attrs.
2132 	 */
2133 	if (pva.va_type != VDIR) {
2134 		/*
2135 		 * Call remap_lookup() again, this time with the
2136 		 * newpfh and pgar args in the first position.
2137 		 */
2138 		pnp = fn_parent(np);
2139 		if (pnp != NULL) {
2140 			remap_lookup(pnp, rootvp, RML_ORDINARY, cr,
2141 			    &newpfh, &pgar, NULL, NULL, &e);
2142 			if (nfs4_needs_recovery(&e, FALSE,
2143 			    mi->mi_vfsp)) {
2144 				if (need_start_op) {
2145 					bool_t abort;
2146 
2147 					abort = nfs4_start_recovery(&e, mi,
2148 					    rootvp, NULL, NULL, NULL,
2149 					    OP_LOOKUP, NULL);
2150 					if (abort) {
2151 						nfs4_end_fop(mi, rootvp, NULL,
2152 						    OH_LOOKUP, &recov_state,
2153 						    FALSE);
2154 						if (e.error == 0)
2155 							e.error = EIO;
2156 						goto out;
2157 					}
2158 					nfs4_end_fop(mi, rootvp, NULL,
2159 					    OH_LOOKUP, &recov_state, TRUE);
2160 					goto recov_retry;
2161 				}
2162 				if (e.error == 0)
2163 					e.error = EIO;
2164 				goto out;
2165 			}
2166 
2167 			if (e.stat != NFS4_OK) {
2168 				if (need_start_op)
2169 					nfs4_end_fop(mi, rootvp, NULL,
2170 					    OH_LOOKUP, &recov_state, FALSE);
2171 				e.error = EIO;
2172 				goto out;
2173 			}
2174 		}
2175 		if ((pnp == NULL) ||
2176 		    (e.error != 0) ||
2177 		    (pva.va_type == VNON)) {
2178 			if (need_start_op)
2179 				nfs4_end_fop(mi, rootvp, NULL, OH_LOOKUP,
2180 				    &recov_state, FALSE);
2181 			if (e.error == 0)
2182 				e.error = EIO;
2183 			goto out;
2184 		}
2185 	}
2186 	ASSERT(newpfh.nfs_fh4_len != 0);
2187 	if (need_start_op)
2188 		nfs4_end_fop(mi, rootvp, NULL, OH_LOOKUP, &recov_state, FALSE);
2189 	psfh = sfh4_get(&newpfh, mi);
2190 
2191 	sfh = sfh4_get(&newfh, mi);
2192 	vp = makenfs4node_by_fh(sfh, psfh, &np, &gar, mi, cr, t);
2193 
2194 out:
2195 	if (np != NULL)
2196 		fn_rele(&np);
2197 	if (pnp != NULL)
2198 		fn_rele(&pnp);
2199 	if (newfh.nfs_fh4_len != 0)
2200 		kmem_free(newfh.nfs_fh4_val, newfh.nfs_fh4_len);
2201 	if (newpfh.nfs_fh4_len != 0)
2202 		kmem_free(newpfh.nfs_fh4_val, newpfh.nfs_fh4_len);
2203 	if (sfh != NULL)
2204 		sfh4_rele(&sfh);
2205 	if (psfh != NULL)
2206 		sfh4_rele(&psfh);
2207 	if (rootvp != NULL)
2208 		VN_RELE(rootvp);
2209 	*vpp = vp;
2210 	return (e.error);
2211 }
2212 
2213 #ifdef DEBUG
2214 size_t r_path_memuse = 0;
2215 #endif
2216 
2217 /*
2218  * NFS client failover support
2219  *
2220  * sv4_free() frees the malloc'd portion of a "servinfo_t".
2221  */
2222 void
2223 sv4_free(servinfo4_t *svp)
2224 {
2225 	servinfo4_t *next;
2226 	struct knetconfig *knconf;
2227 
2228 	while (svp != NULL) {
2229 		next = svp->sv_next;
2230 		if (svp->sv_dhsec)
2231 			sec_clnt_freeinfo(svp->sv_dhsec);
2232 		if (svp->sv_secdata)
2233 			sec_clnt_freeinfo(svp->sv_secdata);
2234 		if (svp->sv_save_secinfo &&
2235 				svp->sv_save_secinfo != svp->sv_secinfo)
2236 			secinfo_free(svp->sv_save_secinfo);
2237 		if (svp->sv_secinfo)
2238 			secinfo_free(svp->sv_secinfo);
2239 		if (svp->sv_hostname && svp->sv_hostnamelen > 0)
2240 			kmem_free(svp->sv_hostname, svp->sv_hostnamelen);
2241 		knconf = svp->sv_knconf;
2242 		if (knconf != NULL) {
2243 			if (knconf->knc_protofmly != NULL)
2244 				kmem_free(knconf->knc_protofmly, KNC_STRSIZE);
2245 			if (knconf->knc_proto != NULL)
2246 				kmem_free(knconf->knc_proto, KNC_STRSIZE);
2247 			kmem_free(knconf, sizeof (*knconf));
2248 		}
2249 		knconf = svp->sv_origknconf;
2250 		if (knconf != NULL) {
2251 			if (knconf->knc_protofmly != NULL)
2252 				kmem_free(knconf->knc_protofmly, KNC_STRSIZE);
2253 			if (knconf->knc_proto != NULL)
2254 				kmem_free(knconf->knc_proto, KNC_STRSIZE);
2255 			kmem_free(knconf, sizeof (*knconf));
2256 		}
2257 		if (svp->sv_addr.buf != NULL && svp->sv_addr.maxlen != 0)
2258 			kmem_free(svp->sv_addr.buf, svp->sv_addr.maxlen);
2259 		if (svp->sv_path != NULL) {
2260 			kmem_free(svp->sv_path, svp->sv_pathlen);
2261 		}
2262 		nfs_rw_destroy(&svp->sv_lock);
2263 		kmem_free(svp, sizeof (*svp));
2264 		svp = next;
2265 	}
2266 }
2267 
2268 void
2269 nfs4_printfhandle(nfs4_fhandle_t *fhp)
2270 {
2271 	int *ip;
2272 	char *buf;
2273 	size_t bufsize;
2274 	char *cp;
2275 
2276 	/*
2277 	 * 13 == "(file handle:"
2278 	 * maximum of NFS_FHANDLE / sizeof (*ip) elements in fh_buf times
2279 	 *	1 == ' '
2280 	 *	8 == maximum strlen of "%x"
2281 	 * 3 == ")\n\0"
2282 	 */
2283 	bufsize = 13 + ((NFS_FHANDLE_LEN / sizeof (*ip)) * (1 + 8)) + 3;
2284 	buf = kmem_alloc(bufsize, KM_NOSLEEP);
2285 	if (buf == NULL)
2286 		return;
2287 
2288 	cp = buf;
2289 	(void) strcpy(cp, "(file handle:");
2290 	while (*cp != '\0')
2291 		cp++;
2292 	for (ip = (int *)fhp->fh_buf;
2293 	    ip < (int *)&fhp->fh_buf[fhp->fh_len];
2294 	    ip++) {
2295 		(void) sprintf(cp, " %x", *ip);
2296 		while (*cp != '\0')
2297 			cp++;
2298 	}
2299 	(void) strcpy(cp, ")\n");
2300 
2301 	zcmn_err(getzoneid(), CE_CONT, "%s", buf);
2302 
2303 	kmem_free(buf, bufsize);
2304 }
2305 
2306 /*
2307  * The NFSv4 readdir cache subsystem.
2308  *
2309  * We provide a set of interfaces to allow the rest of the system to utilize
2310  * a caching mechanism while encapsulating the details of the actual
2311  * implementation.  This should allow for better maintainability and
2312  * extensibilty by consolidating the implementation details in one location.
2313  */
2314 
2315 /*
2316  * Comparator used by AVL routines.
2317  */
2318 static int
2319 rddir4_cache_compar(const void *x, const void *y)
2320 {
2321 	rddir4_cache_impl *ai = (rddir4_cache_impl *)x;
2322 	rddir4_cache_impl *bi = (rddir4_cache_impl *)y;
2323 	rddir4_cache *a = &ai->rc;
2324 	rddir4_cache *b = &bi->rc;
2325 
2326 	if (a->nfs4_cookie == b->nfs4_cookie) {
2327 		if (a->buflen == b->buflen)
2328 			return (0);
2329 		if (a->buflen < b->buflen)
2330 			return (-1);
2331 		return (1);
2332 	}
2333 
2334 	if (a->nfs4_cookie < b->nfs4_cookie)
2335 			return (-1);
2336 
2337 	return (1);
2338 }
2339 
2340 /*
2341  * Allocate an opaque handle for the readdir cache.
2342  */
2343 void
2344 rddir4_cache_create(rnode4_t *rp)
2345 {
2346 	ASSERT(rp->r_dir == NULL);
2347 
2348 	rp->r_dir = kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
2349 
2350 	avl_create(rp->r_dir, rddir4_cache_compar, sizeof (rddir4_cache_impl),
2351 			offsetof(rddir4_cache_impl, tree));
2352 }
2353 
2354 /*
2355  *  Purge the cache of all cached readdir responses.
2356  */
2357 void
2358 rddir4_cache_purge(rnode4_t *rp)
2359 {
2360 	rddir4_cache_impl	*rdip;
2361 	rddir4_cache_impl	*nrdip;
2362 
2363 	ASSERT(MUTEX_HELD(&rp->r_statelock));
2364 
2365 	if (rp->r_dir == NULL)
2366 		return;
2367 
2368 	rdip = avl_first(rp->r_dir);
2369 
2370 	while (rdip != NULL) {
2371 		nrdip = AVL_NEXT(rp->r_dir, rdip);
2372 		avl_remove(rp->r_dir, rdip);
2373 		rdip->rc.flags &= ~RDDIRCACHED;
2374 		rddir4_cache_rele(rp, &rdip->rc);
2375 		rdip = nrdip;
2376 	}
2377 	ASSERT(avl_numnodes(rp->r_dir) == 0);
2378 }
2379 
2380 /*
2381  * Destroy the readdir cache.
2382  */
2383 void
2384 rddir4_cache_destroy(rnode4_t *rp)
2385 {
2386 	ASSERT(MUTEX_HELD(&rp->r_statelock));
2387 	if (rp->r_dir == NULL)
2388 		return;
2389 
2390 	rddir4_cache_purge(rp);
2391 	avl_destroy(rp->r_dir);
2392 	kmem_free(rp->r_dir, sizeof (avl_tree_t));
2393 	rp->r_dir = NULL;
2394 }
2395 
2396 /*
2397  * Locate a readdir response from the readdir cache.
2398  *
2399  * Return values:
2400  *
2401  * NULL - If there is an unrecoverable situation like the operation may have
2402  *	  been interrupted.
2403  *
2404  * rddir4_cache * - A pointer to a rddir4_cache is returned to the caller.
2405  *		    The flags are set approprately, such that the caller knows
2406  *		    what state the entry is in.
2407  */
2408 rddir4_cache *
2409 rddir4_cache_lookup(rnode4_t *rp, offset_t cookie, int count)
2410 {
2411 	rddir4_cache_impl	*rdip = NULL;
2412 	rddir4_cache_impl	srdip;
2413 	rddir4_cache		*srdc;
2414 	rddir4_cache		*rdc = NULL;
2415 	rddir4_cache		*nrdc = NULL;
2416 	avl_index_t		where;
2417 
2418 top:
2419 	ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
2420 	ASSERT(MUTEX_HELD(&rp->r_statelock));
2421 	/*
2422 	 * Check to see if the readdir cache has been disabled.  If so, then
2423 	 * simply allocate an rddir4_cache entry and return it, since caching
2424 	 * operations do not apply.
2425 	 */
2426 	if (rp->r_dir == NULL) {
2427 		if (nrdc == NULL) {
2428 			/*
2429 			 * Drop the lock because we are doing a sleeping
2430 			 * allocation.
2431 			 */
2432 			mutex_exit(&rp->r_statelock);
2433 			rdc = rddir4_cache_alloc(KM_SLEEP);
2434 			rdc->nfs4_cookie = cookie;
2435 			rdc->buflen = count;
2436 			mutex_enter(&rp->r_statelock);
2437 			return (rdc);
2438 		}
2439 		return (nrdc);
2440 	}
2441 
2442 	srdc = &srdip.rc;
2443 	srdc->nfs4_cookie = cookie;
2444 	srdc->buflen = count;
2445 
2446 	rdip = avl_find(rp->r_dir, &srdip, &where);
2447 
2448 	/*
2449 	 * If we didn't find an entry then create one and insert it
2450 	 * into the cache.
2451 	 */
2452 	if (rdip == NULL) {
2453 		/*
2454 		 * Check for the case where we have made a second pass through
2455 		 * the cache due to a lockless allocation.  If we find that no
2456 		 * thread has already inserted this entry, do the insert now
2457 		 * and return.
2458 		 */
2459 		if (nrdc != NULL) {
2460 			avl_insert(rp->r_dir, nrdc->data, where);
2461 			nrdc->flags |= RDDIRCACHED;
2462 			rddir4_cache_hold(nrdc);
2463 			return (nrdc);
2464 		}
2465 
2466 #ifdef DEBUG
2467 		nfs4_readdir_cache_misses++;
2468 #endif
2469 		/*
2470 		 * First, try to allocate an entry without sleeping.  If that
2471 		 * fails then drop the lock and do a sleeping allocation.
2472 		 */
2473 		nrdc = rddir4_cache_alloc(KM_NOSLEEP);
2474 		if (nrdc != NULL) {
2475 			nrdc->nfs4_cookie = cookie;
2476 			nrdc->buflen = count;
2477 			avl_insert(rp->r_dir, nrdc->data, where);
2478 			nrdc->flags |= RDDIRCACHED;
2479 			rddir4_cache_hold(nrdc);
2480 			return (nrdc);
2481 		}
2482 
2483 		/*
2484 		 * Drop the lock and do a sleeping allocation.	We incur
2485 		 * additional overhead by having to search the cache again,
2486 		 * but this case should be rare.
2487 		 */
2488 		mutex_exit(&rp->r_statelock);
2489 		nrdc = rddir4_cache_alloc(KM_SLEEP);
2490 		nrdc->nfs4_cookie = cookie;
2491 		nrdc->buflen = count;
2492 		mutex_enter(&rp->r_statelock);
2493 		/*
2494 		 * We need to take another pass through the cache
2495 		 * since we dropped our lock to perform the alloc.
2496 		 * Another thread may have come by and inserted the
2497 		 * entry we are interested in.
2498 		 */
2499 		goto top;
2500 	}
2501 
2502 	/*
2503 	 * Check to see if we need to free our entry.  This can happen if
2504 	 * another thread came along beat us to the insert.  We can
2505 	 * safely call rddir4_cache_free directly because no other thread
2506 	 * would have a reference to this entry.
2507 	 */
2508 	if (nrdc != NULL)
2509 		rddir4_cache_free((rddir4_cache_impl *)nrdc->data);
2510 
2511 #ifdef DEBUG
2512 	nfs4_readdir_cache_hits++;
2513 #endif
2514 	/*
2515 	 * Found something.  Make sure it's ready to return.
2516 	 */
2517 	rdc = &rdip->rc;
2518 	rddir4_cache_hold(rdc);
2519 	/*
2520 	 * If the cache entry is in the process of being filled in, wait
2521 	 * until this completes.  The RDDIRWAIT bit is set to indicate that
2522 	 * someone is waiting and when the thread currently filling the entry
2523 	 * is done, it should do a cv_broadcast to wakeup all of the threads
2524 	 * waiting for it to finish. If the thread wakes up to find that
2525 	 * someone new is now trying to complete the the entry, go back
2526 	 * to sleep.
2527 	 */
2528 	while (rdc->flags & RDDIR) {
2529 		/*
2530 		 * The entry is not complete.
2531 		 */
2532 		nfs_rw_exit(&rp->r_rwlock);
2533 		rdc->flags |= RDDIRWAIT;
2534 #ifdef DEBUG
2535 		nfs4_readdir_cache_waits++;
2536 #endif
2537 		while (rdc->flags & RDDIRWAIT) {
2538 			if (!cv_wait_sig(&rdc->cv, &rp->r_statelock)) {
2539 				/*
2540 				 * We got interrupted, probably the user
2541 				 * typed ^C or an alarm fired.  We free the
2542 				 * new entry if we allocated one.
2543 				 */
2544 				rddir4_cache_rele(rp, rdc);
2545 				mutex_exit(&rp->r_statelock);
2546 				(void) nfs_rw_enter_sig(&rp->r_rwlock,
2547 					RW_READER, FALSE);
2548 				mutex_enter(&rp->r_statelock);
2549 				return (NULL);
2550 			}
2551 		}
2552 		mutex_exit(&rp->r_statelock);
2553 		(void) nfs_rw_enter_sig(&rp->r_rwlock,
2554 			RW_READER, FALSE);
2555 		mutex_enter(&rp->r_statelock);
2556 	}
2557 
2558 	/*
2559 	 * The entry we were waiting on may have been purged from
2560 	 * the cache and should no longer be used, release it and
2561 	 * start over.
2562 	 */
2563 	if (!(rdc->flags & RDDIRCACHED)) {
2564 		rddir4_cache_rele(rp, rdc);
2565 		goto top;
2566 	}
2567 
2568 	/*
2569 	 * The entry is completed.  Return it.
2570 	 */
2571 	return (rdc);
2572 }
2573 
2574 /*
2575  * Allocate a cache element and return it.  Can return NULL if memory is
2576  * low.
2577  */
2578 static rddir4_cache *
2579 rddir4_cache_alloc(int flags)
2580 {
2581 	rddir4_cache_impl	*rdip = NULL;
2582 	rddir4_cache		*rc = NULL;
2583 
2584 	rdip = kmem_alloc(sizeof (rddir4_cache_impl), flags);
2585 
2586 	if (rdip != NULL) {
2587 		rc = &rdip->rc;
2588 		rc->data = (void *)rdip;
2589 		rc->nfs4_cookie = 0;
2590 		rc->nfs4_ncookie = 0;
2591 		rc->entries = NULL;
2592 		rc->eof = 0;
2593 		rc->entlen = 0;
2594 		rc->buflen = 0;
2595 		rc->actlen = 0;
2596 		/*
2597 		 * A readdir is required so set the flag.
2598 		 */
2599 		rc->flags = RDDIRREQ;
2600 		cv_init(&rc->cv, NULL, CV_DEFAULT, NULL);
2601 		rc->error = 0;
2602 		mutex_init(&rdip->lock, NULL, MUTEX_DEFAULT, NULL);
2603 		rdip->count = 1;
2604 #ifdef DEBUG
2605 		atomic_add_64(&clstat4_debug.dirent.value.ui64, 1);
2606 #endif
2607 	}
2608 	return (rc);
2609 }
2610 
2611 /*
2612  * Increment the reference count to this cache element.
2613  */
2614 static void
2615 rddir4_cache_hold(rddir4_cache *rc)
2616 {
2617 	rddir4_cache_impl *rdip = (rddir4_cache_impl *)rc->data;
2618 
2619 	mutex_enter(&rdip->lock);
2620 	rdip->count++;
2621 	mutex_exit(&rdip->lock);
2622 }
2623 
2624 /*
2625  * Release a reference to this cache element.  If the count is zero then
2626  * free the element.
2627  */
2628 void
2629 rddir4_cache_rele(rnode4_t *rp, rddir4_cache *rdc)
2630 {
2631 	rddir4_cache_impl *rdip = (rddir4_cache_impl *)rdc->data;
2632 
2633 	ASSERT(MUTEX_HELD(&rp->r_statelock));
2634 
2635 	/*
2636 	 * Check to see if we have any waiters.  If so, we can wake them
2637 	 * so that they can proceed.
2638 	 */
2639 	if (rdc->flags & RDDIRWAIT) {
2640 		rdc->flags &= ~RDDIRWAIT;
2641 		cv_broadcast(&rdc->cv);
2642 	}
2643 
2644 	mutex_enter(&rdip->lock);
2645 	ASSERT(rdip->count > 0);
2646 	if (--rdip->count == 0) {
2647 		mutex_exit(&rdip->lock);
2648 		rddir4_cache_free(rdip);
2649 	} else
2650 		mutex_exit(&rdip->lock);
2651 }
2652 
2653 /*
2654  * Free a cache element.
2655  */
2656 static void
2657 rddir4_cache_free(rddir4_cache_impl *rdip)
2658 {
2659 	rddir4_cache *rc = &rdip->rc;
2660 
2661 #ifdef DEBUG
2662 	atomic_add_64(&clstat4_debug.dirent.value.ui64, -1);
2663 #endif
2664 	if (rc->entries != NULL)
2665 		kmem_free(rc->entries, rc->buflen);
2666 	cv_destroy(&rc->cv);
2667 	mutex_destroy(&rdip->lock);
2668 	kmem_free(rdip, sizeof (*rdip));
2669 }
2670 
2671 /*
2672  * Snapshot callback for nfs:0:nfs4_client as registered with the kstat
2673  * framework.
2674  */
2675 static int
2676 cl4_snapshot(kstat_t *ksp, void *buf, int rw)
2677 {
2678 	ksp->ks_snaptime = gethrtime();
2679 	if (rw == KSTAT_WRITE) {
2680 		bcopy(buf, ksp->ks_private, sizeof (clstat4_tmpl));
2681 #ifdef DEBUG
2682 		/*
2683 		 * Currently only the global zone can write to kstats, but we
2684 		 * add the check just for paranoia.
2685 		 */
2686 		if (INGLOBALZONE(curproc))
2687 		    bcopy((char *)buf + sizeof (clstat4_tmpl), &clstat4_debug,
2688 			    sizeof (clstat4_debug));
2689 #endif
2690 	} else {
2691 		bcopy(ksp->ks_private, buf, sizeof (clstat4_tmpl));
2692 #ifdef DEBUG
2693 		/*
2694 		 * If we're displaying the "global" debug kstat values, we
2695 		 * display them as-is to all zones since in fact they apply to
2696 		 * the system as a whole.
2697 		 */
2698 		bcopy(&clstat4_debug, (char *)buf + sizeof (clstat4_tmpl),
2699 		    sizeof (clstat4_debug));
2700 #endif
2701 	}
2702 	return (0);
2703 }
2704 
2705 
2706 
2707 /*
2708  * Zone support
2709  */
2710 static void *
2711 clinit4_zone(zoneid_t zoneid)
2712 {
2713 	kstat_t *nfs4_client_kstat;
2714 	struct nfs4_clnt *nfscl;
2715 	uint_t ndata;
2716 
2717 	nfscl = kmem_alloc(sizeof (*nfscl), KM_SLEEP);
2718 	mutex_init(&nfscl->nfscl_chtable4_lock, NULL, MUTEX_DEFAULT, NULL);
2719 	nfscl->nfscl_chtable4 = NULL;
2720 	nfscl->nfscl_zoneid = zoneid;
2721 
2722 	bcopy(&clstat4_tmpl, &nfscl->nfscl_stat, sizeof (clstat4_tmpl));
2723 	ndata = sizeof (clstat4_tmpl) / sizeof (kstat_named_t);
2724 #ifdef DEBUG
2725 	ndata += sizeof (clstat4_debug) / sizeof (kstat_named_t);
2726 #endif
2727 	if ((nfs4_client_kstat = kstat_create_zone("nfs", 0, "nfs4_client",
2728 	    "misc", KSTAT_TYPE_NAMED, ndata,
2729 	    KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_WRITABLE, zoneid)) != NULL) {
2730 		nfs4_client_kstat->ks_private = &nfscl->nfscl_stat;
2731 		nfs4_client_kstat->ks_snapshot = cl4_snapshot;
2732 		kstat_install(nfs4_client_kstat);
2733 	}
2734 	mutex_enter(&nfs4_clnt_list_lock);
2735 	list_insert_head(&nfs4_clnt_list, nfscl);
2736 	mutex_exit(&nfs4_clnt_list_lock);
2737 	return (nfscl);
2738 }
2739 
2740 /*ARGSUSED*/
2741 static void
2742 clfini4_zone(zoneid_t zoneid, void *arg)
2743 {
2744 	struct nfs4_clnt *nfscl = arg;
2745 	chhead_t *chp, *next;
2746 
2747 	if (nfscl == NULL)
2748 		return;
2749 	mutex_enter(&nfs4_clnt_list_lock);
2750 	list_remove(&nfs4_clnt_list, nfscl);
2751 	mutex_exit(&nfs4_clnt_list_lock);
2752 	clreclaim4_zone(nfscl, 0);
2753 	for (chp = nfscl->nfscl_chtable4; chp != NULL; chp = next) {
2754 		ASSERT(chp->ch_list == NULL);
2755 		kmem_free(chp->ch_protofmly, strlen(chp->ch_protofmly) + 1);
2756 		next = chp->ch_next;
2757 		kmem_free(chp, sizeof (*chp));
2758 	}
2759 	kstat_delete_byname_zone("nfs", 0, "nfs4_client", zoneid);
2760 	mutex_destroy(&nfscl->nfscl_chtable4_lock);
2761 	kmem_free(nfscl, sizeof (*nfscl));
2762 }
2763 
2764 /*
2765  * Called by endpnt_destructor to make sure the client handles are
2766  * cleaned up before the RPC endpoints.  This becomes a no-op if
2767  * clfini_zone (above) is called first.  This function is needed
2768  * (rather than relying on clfini_zone to clean up) because the ZSD
2769  * callbacks have no ordering mechanism, so we have no way to ensure
2770  * that clfini_zone is called before endpnt_destructor.
2771  */
2772 void
2773 clcleanup4_zone(zoneid_t zoneid)
2774 {
2775 	struct nfs4_clnt *nfscl;
2776 
2777 	mutex_enter(&nfs4_clnt_list_lock);
2778 	nfscl = list_head(&nfs4_clnt_list);
2779 	for (; nfscl != NULL; nfscl = list_next(&nfs4_clnt_list, nfscl)) {
2780 		if (nfscl->nfscl_zoneid == zoneid) {
2781 			clreclaim4_zone(nfscl, 0);
2782 			break;
2783 		}
2784 	}
2785 	mutex_exit(&nfs4_clnt_list_lock);
2786 }
2787 
2788 int
2789 nfs4_subr_init(void)
2790 {
2791 	/*
2792 	 * Allocate and initialize the client handle cache
2793 	 */
2794 	chtab4_cache = kmem_cache_create("client_handle4_cache",
2795 		sizeof (struct chtab), 0, NULL, NULL, clreclaim4, NULL,
2796 		NULL, 0);
2797 
2798 	/*
2799 	 * Initialize the list of per-zone client handles (and associated data).
2800 	 * This needs to be done before we call zone_key_create().
2801 	 */
2802 	list_create(&nfs4_clnt_list, sizeof (struct nfs4_clnt),
2803 	    offsetof(struct nfs4_clnt, nfscl_node));
2804 
2805 	/*
2806 	 * Initialize the zone_key for per-zone client handle lists.
2807 	 */
2808 	zone_key_create(&nfs4clnt_zone_key, clinit4_zone, NULL, clfini4_zone);
2809 
2810 	if (nfs4err_delay_time == 0)
2811 		nfs4err_delay_time = NFS4ERR_DELAY_TIME;
2812 
2813 	return (0);
2814 }
2815 
2816 int
2817 nfs4_subr_fini(void)
2818 {
2819 	/*
2820 	 * Deallocate the client handle cache
2821 	 */
2822 	kmem_cache_destroy(chtab4_cache);
2823 
2824 	/*
2825 	 * Destroy the zone_key
2826 	 */
2827 	(void) zone_key_delete(nfs4clnt_zone_key);
2828 
2829 	return (0);
2830 }
2831 /*
2832  * Set or Clear direct I/O flag
2833  * VOP_RWLOCK() is held for write access to prevent a race condition
2834  * which would occur if a process is in the middle of a write when
2835  * directio flag gets set. It is possible that all pages may not get flushed.
2836  *
2837  * This is a copy of nfs_directio, changes here may need to be made
2838  * there and vice versa.
2839  */
2840 
2841 int
2842 nfs4_directio(vnode_t *vp, int cmd, cred_t *cr)
2843 {
2844 	int	error = 0;
2845 	rnode4_t *rp;
2846 
2847 	rp = VTOR4(vp);
2848 
2849 	if (cmd == DIRECTIO_ON) {
2850 
2851 		if (rp->r_flags & R4DIRECTIO)
2852 			return (0);
2853 
2854 		/*
2855 		 * Flush the page cache.
2856 		 */
2857 
2858 		(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
2859 
2860 		if (rp->r_flags & R4DIRECTIO) {
2861 			VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
2862 			return (0);
2863 		}
2864 
2865 		if (nfs4_has_pages(vp) &&
2866 		    ((rp->r_flags & R4DIRTY) || rp->r_awcount > 0)) {
2867 			error = VOP_PUTPAGE(vp, (offset_t)0, (uint_t)0,
2868 			    B_INVAL, cr);
2869 			if (error) {
2870 				if (error == ENOSPC || error == EDQUOT) {
2871 					mutex_enter(&rp->r_statelock);
2872 					if (!rp->r_error)
2873 						rp->r_error = error;
2874 					mutex_exit(&rp->r_statelock);
2875 				}
2876 				VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
2877 				return (error);
2878 			}
2879 		}
2880 
2881 		mutex_enter(&rp->r_statelock);
2882 		rp->r_flags |= R4DIRECTIO;
2883 		mutex_exit(&rp->r_statelock);
2884 		VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
2885 		return (0);
2886 	}
2887 
2888 	if (cmd == DIRECTIO_OFF) {
2889 		mutex_enter(&rp->r_statelock);
2890 		rp->r_flags &= ~R4DIRECTIO;	/* disable direct mode */
2891 		mutex_exit(&rp->r_statelock);
2892 		return (0);
2893 	}
2894 
2895 	return (EINVAL);
2896 }
2897 
2898 /*
2899  * Return TRUE if the file has any pages.  Always go back to
2900  * the master vnode to check v_pages since none of the shadows
2901  * can have pages.
2902  */
2903 
2904 bool_t
2905 nfs4_has_pages(vnode_t *vp)
2906 {
2907 	rnode4_t *rp;
2908 
2909 	rp = VTOR4(vp);
2910 	if (IS_SHADOW(vp, rp))
2911 		vp = RTOV4(rp);	/* RTOV4 always gives the master */
2912 
2913 	return (vn_has_cached_data(vp));
2914 }
2915 
2916 /*
2917  * This table is used to determine whether the client should attempt
2918  * failover based on the clnt_stat value returned by CLNT_CALL.  The
2919  * clnt_stat is used as an index into the table.  If
2920  * the error value that corresponds to the clnt_stat value in the
2921  * table is non-zero, then that is the error to be returned AND
2922  * that signals that failover should be attempted.
2923  *
2924  * Special note: If the RPC_ values change, then direct indexing of the
2925  * table is no longer valid, but having the RPC_ values in the table
2926  * allow the functions to detect the change and issue a warning.
2927  * In this case, the code will always attempt failover as a defensive
2928  * measure.
2929  */
2930 
2931 static struct try_failover_tab {
2932 	enum clnt_stat	cstat;
2933 	int		error;
2934 } try_failover_table [] = {
2935 
2936 	RPC_SUCCESS,		0,
2937 	RPC_CANTENCODEARGS,	0,
2938 	RPC_CANTDECODERES,	0,
2939 	RPC_CANTSEND,		ECOMM,
2940 	RPC_CANTRECV,		ECOMM,
2941 	RPC_TIMEDOUT,		ETIMEDOUT,
2942 	RPC_VERSMISMATCH,	0,
2943 	RPC_AUTHERROR,		0,
2944 	RPC_PROGUNAVAIL,	0,
2945 	RPC_PROGVERSMISMATCH,	0,
2946 	RPC_PROCUNAVAIL,	0,
2947 	RPC_CANTDECODEARGS,	0,
2948 	RPC_SYSTEMERROR,	ENOSR,
2949 	RPC_UNKNOWNHOST,	EHOSTUNREACH,
2950 	RPC_RPCBFAILURE,	ENETUNREACH,
2951 	RPC_PROGNOTREGISTERED,	ECONNREFUSED,
2952 	RPC_FAILED,		ETIMEDOUT,
2953 	RPC_UNKNOWNPROTO,	EHOSTUNREACH,
2954 	RPC_INTR,		0,
2955 	RPC_UNKNOWNADDR,	EHOSTUNREACH,
2956 	RPC_TLIERROR,		0,
2957 	RPC_NOBROADCAST,	EHOSTUNREACH,
2958 	RPC_N2AXLATEFAILURE,	ECONNREFUSED,
2959 	RPC_UDERROR,		0,
2960 	RPC_INPROGRESS,		0,
2961 	RPC_STALERACHANDLE,	EINVAL,
2962 	RPC_CANTCONNECT,	ECONNREFUSED,
2963 	RPC_XPRTFAILED,		ECONNABORTED,
2964 	RPC_CANTCREATESTREAM,	ECONNREFUSED,
2965 	RPC_CANTSTORE,		ENOBUFS
2966 };
2967 
2968 /*
2969  * nfs4_try_failover - determine whether the client should
2970  * attempt failover based on the values stored in the nfs4_error_t.
2971  */
2972 int
2973 nfs4_try_failover(nfs4_error_t *ep)
2974 {
2975 	if (ep->error == ETIMEDOUT || ep->stat == NFS4ERR_RESOURCE)
2976 		return (TRUE);
2977 
2978 	if (ep->error && ep->rpc_status != RPC_SUCCESS)
2979 		return (try_failover(ep->rpc_status) != 0 ? TRUE : FALSE);
2980 
2981 	return (FALSE);
2982 }
2983 
2984 /*
2985  * try_failover - internal version of nfs4_try_failover, called
2986  * only by rfscall and aclcall.  Determine if failover is warranted
2987  * based on the clnt_stat and return the error number if it is.
2988  */
2989 static int
2990 try_failover(enum clnt_stat rpc_status)
2991 {
2992 	int err = 0;
2993 
2994 	if (rpc_status == RPC_SUCCESS)
2995 		return (0);
2996 
2997 #ifdef	DEBUG
2998 	if (rpc_status != 0 && nfs4_try_failover_any) {
2999 		err = ETIMEDOUT;
3000 		goto done;
3001 	}
3002 #endif
3003 	/*
3004 	 * The rpc status is used as an index into the table.
3005 	 * If the rpc status is outside of the range of the
3006 	 * table or if the rpc error numbers have been changed
3007 	 * since the table was constructed, then print a warning
3008 	 * (DEBUG only) and try failover anyway.  Otherwise, just
3009 	 * grab the resulting error number out of the table.
3010 	 */
3011 	if (rpc_status < RPC_SUCCESS || rpc_status >=
3012 	    sizeof (try_failover_table)/sizeof (try_failover_table[0]) ||
3013 	    try_failover_table[rpc_status].cstat != rpc_status) {
3014 
3015 		err = ETIMEDOUT;
3016 #ifdef	DEBUG
3017 		cmn_err(CE_NOTE, "try_failover: unexpected rpc error %d",
3018 			rpc_status);
3019 #endif
3020 	} else
3021 		err = try_failover_table[rpc_status].error;
3022 
3023 done:
3024 	if (rpc_status)
3025 		NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
3026 			"nfs4_try_failover: %strying failover on error %d",
3027 			err ? "" : "NOT ", rpc_status));
3028 
3029 	return (err);
3030 }
3031 
3032 void
3033 nfs4_error_zinit(nfs4_error_t *ep)
3034 {
3035 	ep->error = 0;
3036 	ep->stat = NFS4_OK;
3037 	ep->rpc_status = RPC_SUCCESS;
3038 }
3039 
3040 void
3041 nfs4_error_init(nfs4_error_t *ep, int error)
3042 {
3043 	ep->error = error;
3044 	ep->stat = NFS4_OK;
3045 	ep->rpc_status = RPC_SUCCESS;
3046 }
3047 
3048 
3049 #ifdef DEBUG
3050 
3051 /*
3052  * Return a 16-bit hash for filehandle, stateid, clientid, owner.
3053  * use the same algorithm as for NFS v3.
3054  *
3055  */
3056 int
3057 hash16(void *p, int len)
3058 {
3059 	int i, rem;
3060 	uint_t *wp;
3061 	uint_t key = 0;
3062 
3063 	/* protect against non word aligned */
3064 	if ((rem = len & 3) != 0)
3065 		len &= ~3;
3066 
3067 	for (i = 0, wp = (uint_t *)p; i < len; i += 4, wp++) {
3068 		key ^= (*wp >> 16) ^ *wp;
3069 	}
3070 
3071 	/* hash left-over bytes */
3072 	for (i = 0; i < rem; i++)
3073 		key ^= *((uchar_t *)p + i);
3074 
3075 	return (key & 0xffff);
3076 }
3077 
3078 /*
3079  * rnode4info - return filehandle and path information for an rnode.
3080  * XXX MT issues: uses a single static buffer, no locking of path.
3081  */
3082 char *
3083 rnode4info(rnode4_t *rp)
3084 {
3085 	static char buf[80];
3086 	nfs4_fhandle_t fhandle;
3087 	char *path;
3088 	char *type;
3089 
3090 	if (rp == NULL)
3091 		return ("null");
3092 	if (rp->r_flags & R4ISXATTR)
3093 		type = "attr";
3094 	else if (RTOV4(rp)->v_flag & V_XATTRDIR)
3095 		type = "attrdir";
3096 	else if (RTOV4(rp)->v_flag & VROOT)
3097 		type = "root";
3098 	else if (RTOV4(rp)->v_type == VDIR)
3099 		type = "dir";
3100 	else if (RTOV4(rp)->v_type == VREG)
3101 		type = "file";
3102 	else
3103 		type = "other";
3104 	sfh4_copyval(rp->r_fh, &fhandle);
3105 	path = fn_path(rp->r_svnode.sv_name);
3106 	(void) snprintf(buf, 80, "$%p[%s], type=%s, flags=%04X, FH=%04X\n",
3107 	    (void *)rp, path, type, rp->r_flags,
3108 	    hash16((void *)&fhandle.fh_buf, fhandle.fh_len));
3109 	kmem_free(path, strlen(path)+1);
3110 	return (buf);
3111 }
3112 #endif
3113