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