xref: /titanic_44/usr/src/uts/common/fs/nfs/nfs3_vfsops.c (revision f6db9f272f0061301cfaa1c0001b7d636eae31f4)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  *	Copyright (c) 1983,1984,1985,1986,1987,1988,1989  AT&T.
29  *	All rights reserved.
30  */
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <sys/cred.h>
38 #include <sys/vfs.h>
39 #include <sys/vnode.h>
40 #include <sys/pathname.h>
41 #include <sys/sysmacros.h>
42 #include <sys/kmem.h>
43 #include <sys/mkdev.h>
44 #include <sys/mount.h>
45 #include <sys/mntent.h>
46 #include <sys/statvfs.h>
47 #include <sys/errno.h>
48 #include <sys/debug.h>
49 #include <sys/cmn_err.h>
50 #include <sys/utsname.h>
51 #include <sys/bootconf.h>
52 #include <sys/modctl.h>
53 #include <sys/acl.h>
54 #include <sys/flock.h>
55 #include <sys/policy.h>
56 #include <sys/zone.h>
57 #include <sys/class.h>
58 #include <sys/socket.h>
59 #include <sys/netconfig.h>
60 
61 #include <rpc/types.h>
62 #include <rpc/auth.h>
63 #include <rpc/clnt.h>
64 
65 #include <nfs/nfs.h>
66 #include <nfs/nfs_clnt.h>
67 #include <nfs/rnode.h>
68 #include <nfs/mount.h>
69 #include <nfs/nfs_acl.h>
70 
71 #include <fs/fs_subr.h>
72 
73 /*
74  * From rpcsec module (common/rpcsec).
75  */
76 extern int sec_clnt_loadinfo(struct sec_data *, struct sec_data **, model_t);
77 extern void sec_clnt_freeinfo(struct sec_data *);
78 
79 /*
80  * The order and contents of this structure must be kept in sync with that of
81  * rfsreqcnt_v3_tmpl in nfs_stats.c
82  */
83 static char *rfsnames_v3[] = {
84 	"null", "getattr", "setattr", "lookup", "access", "readlink", "read",
85 	"write", "create", "mkdir", "symlink", "mknod", "remove", "rmdir",
86 	"rename", "link", "readdir", "readdirplus", "fsstat", "fsinfo",
87 	"pathconf", "commit"
88 };
89 
90 /*
91  * This table maps from NFS protocol number into call type.
92  * Zero means a "Lookup" type call
93  * One  means a "Read" type call
94  * Two  means a "Write" type call
95  * This is used to select a default time-out.
96  */
97 static uchar_t call_type_v3[] = {
98 	0, 0, 1, 0, 0, 0, 1,
99 	2, 2, 2, 2, 2, 2, 2,
100 	2, 2, 1, 2, 0, 0, 0,
101 	2 };
102 
103 /*
104  * Similar table, but to determine which timer to use
105  * (only real reads and writes!)
106  */
107 static uchar_t timer_type_v3[] = {
108 	0, 0, 0, 0, 0, 0, 1,
109 	2, 0, 0, 0, 0, 0, 0,
110 	0, 0, 1, 1, 0, 0, 0,
111 	0 };
112 
113 /*
114  * This table maps from NFS protocol number into a call type
115  * for the semisoft mount option.
116  * Zero means do not repeat operation.
117  * One  means repeat.
118  */
119 static uchar_t ss_call_type_v3[] = {
120 	0, 0, 1, 0, 0, 0, 0,
121 	1, 1, 1, 1, 1, 1, 1,
122 	1, 1, 0, 0, 0, 0, 0,
123 	1 };
124 
125 /*
126  * nfs3 vfs operations.
127  */
128 static int	nfs3_mount(vfs_t *, vnode_t *, struct mounta *, cred_t *);
129 static int	nfs3_unmount(vfs_t *, int, cred_t *);
130 static int	nfs3_root(vfs_t *, vnode_t **);
131 static int	nfs3_statvfs(vfs_t *, struct statvfs64 *);
132 static int	nfs3_sync(vfs_t *, short, cred_t *);
133 static int	nfs3_vget(vfs_t *, vnode_t **, fid_t *);
134 static int	nfs3_mountroot(vfs_t *, whymountroot_t);
135 static void	nfs3_freevfs(vfs_t *);
136 
137 static int	nfs3rootvp(vnode_t **, vfs_t *, struct servinfo *,
138 		    int, cred_t *, zone_t *);
139 
140 /*
141  * Initialize the vfs structure
142  */
143 
144 static int nfs3fstyp;
145 vfsops_t *nfs3_vfsops;
146 
147 /*
148  * Debug variable to check for rdma based
149  * transport startup and cleanup. Controlled
150  * through /etc/system. Off by default.
151  */
152 extern int rdma_debug;
153 
154 int
155 nfs3init(int fstyp, char *name)
156 {
157 	static const fs_operation_def_t nfs3_vfsops_template[] = {
158 		VFSNAME_MOUNT, nfs3_mount,
159 		VFSNAME_UNMOUNT, nfs3_unmount,
160 		VFSNAME_ROOT, nfs3_root,
161 		VFSNAME_STATVFS, nfs3_statvfs,
162 		VFSNAME_SYNC, (fs_generic_func_p) nfs3_sync,
163 		VFSNAME_VGET, nfs3_vget,
164 		VFSNAME_MOUNTROOT, nfs3_mountroot,
165 		VFSNAME_FREEVFS, (fs_generic_func_p)nfs3_freevfs,
166 		NULL, NULL
167 	};
168 	int error;
169 
170 	error = vfs_setfsops(fstyp, nfs3_vfsops_template, &nfs3_vfsops);
171 	if (error != 0) {
172 		zcmn_err(GLOBAL_ZONEID, CE_WARN,
173 		    "nfs3init: bad vfs ops template");
174 		return (error);
175 	}
176 
177 	error = vn_make_ops(name, nfs3_vnodeops_template, &nfs3_vnodeops);
178 	if (error != 0) {
179 		(void) vfs_freevfsops_by_type(fstyp);
180 		zcmn_err(GLOBAL_ZONEID, CE_WARN,
181 		    "nfs3init: bad vnode ops template");
182 		return (error);
183 	}
184 
185 	nfs3fstyp = fstyp;
186 
187 	return (0);
188 }
189 
190 void
191 nfs3fini(void)
192 {
193 }
194 
195 /*
196  * nfs mount vfsop
197  * Set up mount info record and attach it to vfs struct.
198  */
199 static int
200 nfs3_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
201 {
202 	char *data = uap->dataptr;
203 	int error;
204 	vnode_t *rtvp;			/* the server's root */
205 	mntinfo_t *mi;			/* mount info, pointed at by vfs */
206 	size_t hlen;			/* length of hostname */
207 	size_t nlen;			/* length of netname */
208 	char netname[SYS_NMLN];		/* server's netname */
209 	struct netbuf addr;		/* server's address */
210 	struct netbuf syncaddr;		/* AUTH_DES time sync addr */
211 	struct knetconfig *knconf;	/* transport knetconfig structure */
212 	struct knetconfig *rdma_knconf;	/* rdma transport structure */
213 	rnode_t *rp;
214 	struct servinfo *svp;		/* nfs server info */
215 	struct servinfo *svp_tail = NULL; /* previous nfs server info */
216 	struct servinfo *svp_head;	/* first nfs server info */
217 	struct servinfo *svp_2ndlast;	/* 2nd last in server info list */
218 	struct sec_data *secdata;	/* security data */
219 	STRUCT_DECL(nfs_args, args);	/* nfs mount arguments */
220 	STRUCT_DECL(knetconfig, knconf_tmp);
221 	STRUCT_DECL(netbuf, addr_tmp);
222 	int flags, addr_type;
223 	char *p, *pf;
224 	zone_t *zone = nfs_zone();
225 
226 	if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
227 		return (EPERM);
228 
229 	if (mvp->v_type != VDIR)
230 		return (ENOTDIR);
231 
232 	/*
233 	 * get arguments
234 	 *
235 	 * nfs_args is now versioned and is extensible, so
236 	 * uap->datalen might be different from sizeof (args)
237 	 * in a compatible situation.
238 	 */
239 more:
240 	STRUCT_INIT(args, get_udatamodel());
241 	bzero(STRUCT_BUF(args), SIZEOF_STRUCT(nfs_args, DATAMODEL_NATIVE));
242 	if (copyin(data, STRUCT_BUF(args), MIN(uap->datalen,
243 	    STRUCT_SIZE(args))))
244 		return (EFAULT);
245 
246 	flags = STRUCT_FGET(args, flags);
247 
248 	if (uap->flags & MS_REMOUNT) {
249 		size_t n;
250 		char name[FSTYPSZ];
251 
252 		if (uap->flags & MS_SYSSPACE)
253 			error = copystr(uap->fstype, name, FSTYPSZ, &n);
254 		else
255 			error = copyinstr(uap->fstype, name, FSTYPSZ, &n);
256 
257 		if (error) {
258 			if (error == ENAMETOOLONG)
259 				return (EINVAL);
260 			return (error);
261 		}
262 
263 		/*
264 		 * This check is to ensure that the request is a
265 		 * genuine nfs remount request.
266 		 */
267 
268 		if (strncmp(name, "nfs", 3) != 0)
269 			return (EINVAL);
270 
271 		/*
272 		 * If the request changes the locking type, disallow the
273 		 * remount,
274 		 * because it's questionable whether we can transfer the
275 		 * locking state correctly.
276 		 */
277 
278 		if ((mi = VFTOMI(vfsp)) != NULL) {
279 			uint_t new_mi_llock;
280 			uint_t old_mi_llock;
281 
282 			new_mi_llock = (flags & NFSMNT_LLOCK) ? 1 : 0;
283 			old_mi_llock = (mi->mi_flags & MI_LLOCK) ? 1 : 0;
284 			if (old_mi_llock != new_mi_llock)
285 				return (EBUSY);
286 		}
287 		return (0);
288 	}
289 
290 	mutex_enter(&mvp->v_lock);
291 	if (!(uap->flags & MS_OVERLAY) &&
292 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
293 		mutex_exit(&mvp->v_lock);
294 		return (EBUSY);
295 	}
296 	mutex_exit(&mvp->v_lock);
297 
298 	/* make sure things are zeroed for errout: */
299 	rtvp = NULL;
300 	mi = NULL;
301 	addr.buf = NULL;
302 	syncaddr.buf = NULL;
303 	secdata = NULL;
304 
305 	/*
306 	 * A valid knetconfig structure is required.
307 	 */
308 	if (!(flags & NFSMNT_KNCONF))
309 		return (EINVAL);
310 
311 	/*
312 	 * Allocate a servinfo struct.
313 	 */
314 	svp = kmem_zalloc(sizeof (*svp), KM_SLEEP);
315 	mutex_init(&svp->sv_lock, NULL, MUTEX_DEFAULT, NULL);
316 	if (svp_tail) {
317 		svp_2ndlast = svp_tail;
318 		svp_tail->sv_next = svp;
319 	} else {
320 		svp_head = svp;
321 		svp_2ndlast = svp;
322 	}
323 
324 	svp_tail = svp;
325 
326 	/*
327 	 * Allocate space for a knetconfig structure and
328 	 * its strings and copy in from user-land.
329 	 */
330 	knconf = kmem_zalloc(sizeof (*knconf), KM_SLEEP);
331 	svp->sv_knconf = knconf;
332 	STRUCT_INIT(knconf_tmp, get_udatamodel());
333 	if (copyin(STRUCT_FGETP(args, knconf), STRUCT_BUF(knconf_tmp),
334 	    STRUCT_SIZE(knconf_tmp))) {
335 		sv_free(svp_head);
336 		return (EFAULT);
337 	}
338 
339 	knconf->knc_semantics = STRUCT_FGET(knconf_tmp, knc_semantics);
340 	knconf->knc_protofmly = STRUCT_FGETP(knconf_tmp, knc_protofmly);
341 	knconf->knc_proto = STRUCT_FGETP(knconf_tmp, knc_proto);
342 	if (get_udatamodel() != DATAMODEL_LP64) {
343 		knconf->knc_rdev = expldev(STRUCT_FGET(knconf_tmp, knc_rdev));
344 	} else {
345 		knconf->knc_rdev = STRUCT_FGET(knconf_tmp, knc_rdev);
346 	}
347 
348 	pf = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
349 	p = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
350 	error = copyinstr(knconf->knc_protofmly, pf, KNC_STRSIZE, NULL);
351 	if (error) {
352 		kmem_free(pf, KNC_STRSIZE);
353 		kmem_free(p, KNC_STRSIZE);
354 		sv_free(svp_head);
355 		return (error);
356 	}
357 	error = copyinstr(knconf->knc_proto, p, KNC_STRSIZE, NULL);
358 	if (error) {
359 		kmem_free(pf, KNC_STRSIZE);
360 		kmem_free(p, KNC_STRSIZE);
361 		sv_free(svp_head);
362 		return (error);
363 	}
364 	knconf->knc_protofmly = pf;
365 	knconf->knc_proto = p;
366 
367 	/*
368 	 * Get server address
369 	 */
370 	STRUCT_INIT(addr_tmp, get_udatamodel());
371 	if (copyin(STRUCT_FGETP(args, addr), STRUCT_BUF(addr_tmp),
372 	    STRUCT_SIZE(addr_tmp))) {
373 		addr.buf = NULL;
374 		error = EFAULT;
375 	} else {
376 		char *userbufptr;
377 
378 		userbufptr = addr.buf = STRUCT_FGETP(addr_tmp, buf);
379 		addr.len = STRUCT_FGET(addr_tmp, len);
380 		addr.buf = kmem_alloc(addr.len, KM_SLEEP);
381 		addr.maxlen = addr.len;
382 		if (copyin(userbufptr, addr.buf, addr.len))
383 			error = EFAULT;
384 	}
385 	svp->sv_addr = addr;
386 	if (error)
387 		goto errout;
388 
389 	/*
390 	 * Get the root fhandle
391 	 */
392 	if (copyin(STRUCT_FGETP(args, fh), &svp->sv_fhandle,
393 	    sizeof (svp->sv_fhandle))) {
394 		error = EFAULT;
395 		goto errout;
396 	}
397 
398 	/*
399 	 * Check the root fhandle length
400 	 */
401 	if (svp->sv_fhandle.fh_len > NFS3_FHSIZE ||
402 		svp->sv_fhandle.fh_len <= 0) {
403 		error = EINVAL;
404 #ifdef DEBUG
405 		zcmn_err(getzoneid(), CE_WARN,
406 		    "nfs3_mount: got an invalid fhandle. fh_len = %d",
407 		    svp->sv_fhandle.fh_len);
408 		svp->sv_fhandle.fh_len = NFS_FHANDLE_LEN;
409 		nfs_printfhandle(&svp->sv_fhandle);
410 #endif
411 		goto errout;
412 	}
413 
414 	/*
415 	 * Get server's hostname
416 	 */
417 	if (flags & NFSMNT_HOSTNAME) {
418 		error = copyinstr(STRUCT_FGETP(args, hostname),
419 		    netname, sizeof (netname), &hlen);
420 		if (error)
421 			goto errout;
422 	} else {
423 		char *p = "unknown-host";
424 		hlen = strlen(p) + 1;
425 		(void) strcpy(netname, p);
426 	}
427 	svp->sv_hostnamelen = hlen;
428 	svp->sv_hostname = kmem_alloc(svp->sv_hostnamelen, KM_SLEEP);
429 	(void) strcpy(svp->sv_hostname, netname);
430 
431 	/*
432 	 * RDMA MOUNT SUPPORT FOR NFS v3:
433 	 * Establish, is it possible to use RDMA, if so overload the
434 	 * knconf with rdma specific knconf and free the orignal.
435 	 */
436 	if ((flags & NFSMNT_TRYRDMA) || (flags & NFSMNT_DORDMA)) {
437 		/*
438 		 * Determine the addr type for RDMA, IPv4 or v6.
439 		 */
440 		if (strcmp(svp->sv_knconf->knc_protofmly, NC_INET) == 0)
441 			addr_type = AF_INET;
442 		else if (strcmp(svp->sv_knconf->knc_protofmly, NC_INET6) == 0)
443 			addr_type = AF_INET6;
444 
445 		if (rdma_reachable(addr_type, &svp->sv_addr,
446 			&rdma_knconf) == 0) {
447 			/*
448 			 * If successful, hijack the orignal knconf and
449 			 * replace with a new one, depending on the flags.
450 			 */
451 			svp->sv_origknconf = svp->sv_knconf;
452 			svp->sv_knconf = rdma_knconf;
453 			knconf = rdma_knconf;
454 		} else {
455 			if (flags & NFSMNT_TRYRDMA) {
456 #ifdef	DEBUG
457 				if (rdma_debug)
458 					zcmn_err(getzoneid(), CE_WARN,
459 					    "no RDMA onboard, revert\n");
460 #endif
461 			}
462 
463 			if (flags & NFSMNT_DORDMA) {
464 				/*
465 				 * If proto=rdma is specified and no RDMA
466 				 * path to this server is avialable then
467 				 * ditch this server.
468 				 * This is not included in the mountable
469 				 * server list or the replica list.
470 				 * Check if more servers are specified;
471 				 * Failover case, otherwise bail out of mount.
472 				 */
473 				if (STRUCT_FGET(args, nfs_args_ext) ==
474 				    NFS_ARGS_EXTB && STRUCT_FGETP(args,
475 					nfs_ext_u.nfs_extB.next) != NULL) {
476 					if (uap->flags & MS_RDONLY &&
477 					    !(flags & NFSMNT_SOFT)) {
478 						data = (char *)
479 						    STRUCT_FGETP(args,
480 						nfs_ext_u.nfs_extB.next);
481 						if (svp_head->sv_next == NULL) {
482 							svp_tail = NULL;
483 							svp_2ndlast = NULL;
484 							sv_free(svp_head);
485 							goto more;
486 						} else {
487 							svp_tail = svp_2ndlast;
488 							svp_2ndlast->sv_next =
489 							    NULL;
490 							sv_free(svp);
491 							goto more;
492 						}
493 					}
494 				} else {
495 					/*
496 					 * This is the last server specified
497 					 * in the nfs_args list passed down
498 					 * and its not rdma capable.
499 					 */
500 					if (svp_head->sv_next == NULL) {
501 						/*
502 						 * Is this the only one
503 						 */
504 						error = EINVAL;
505 #ifdef	DEBUG
506 						if (rdma_debug)
507 							zcmn_err(getzoneid(),
508 							    CE_WARN,
509 							    "No RDMA srv");
510 #endif
511 						goto errout;
512 					} else {
513 						/*
514 						 * There is list, since some
515 						 * servers specified before
516 						 * this passed all requirements
517 						 */
518 						svp_tail = svp_2ndlast;
519 						svp_2ndlast->sv_next = NULL;
520 						sv_free(svp);
521 						goto proceed;
522 					}
523 				}
524 			}
525 		}
526 	}
527 
528 	/*
529 	 * Get the extention data which has the new security data structure.
530 	 */
531 	if (flags & NFSMNT_NEWARGS) {
532 		switch (STRUCT_FGET(args, nfs_args_ext)) {
533 		case NFS_ARGS_EXTA:
534 		case NFS_ARGS_EXTB:
535 			/*
536 			 * Indicating the application is using the new
537 			 * sec_data structure to pass in the security
538 			 * data.
539 			 */
540 			if (STRUCT_FGETP(args,
541 			    nfs_ext_u.nfs_extA.secdata) == NULL) {
542 				error = EINVAL;
543 			} else {
544 				error = sec_clnt_loadinfo(
545 				    (struct sec_data *)STRUCT_FGETP(args,
546 					nfs_ext_u.nfs_extA.secdata),
547 				    &secdata, get_udatamodel());
548 			}
549 			break;
550 
551 		default:
552 			error = EINVAL;
553 			break;
554 		}
555 	} else if (flags & NFSMNT_SECURE) {
556 		/*
557 		 * Keep this for backward compatibility to support
558 		 * NFSMNT_SECURE/NFSMNT_RPCTIMESYNC flags.
559 		 */
560 		if (STRUCT_FGETP(args, syncaddr) == NULL) {
561 			error = EINVAL;
562 		} else {
563 			/*
564 			 * get time sync address.
565 			 */
566 			if (copyin(STRUCT_FGETP(args, syncaddr), &addr_tmp,
567 			    STRUCT_SIZE(addr_tmp))) {
568 				syncaddr.buf = NULL;
569 				error = EFAULT;
570 			} else {
571 				char *userbufptr;
572 
573 				userbufptr = syncaddr.buf =
574 				    STRUCT_FGETP(addr_tmp, buf);
575 				syncaddr.len =
576 				    STRUCT_FGET(addr_tmp, len);
577 				syncaddr.buf = kmem_alloc(syncaddr.len,
578 				    KM_SLEEP);
579 				syncaddr.maxlen = syncaddr.len;
580 
581 				if (copyin(userbufptr, syncaddr.buf,
582 				    syncaddr.len))
583 					error = EFAULT;
584 			}
585 
586 			/*
587 			 * get server's netname
588 			 */
589 			if (!error) {
590 				error = copyinstr(STRUCT_FGETP(args, netname),
591 				    netname, sizeof (netname), &nlen);
592 				netname[nlen] = '\0';
593 			}
594 
595 			if (error && syncaddr.buf != NULL) {
596 				kmem_free(syncaddr.buf, syncaddr.len);
597 				syncaddr.buf = NULL;
598 			}
599 		}
600 
601 		/*
602 		 * Move security related data to the sec_data structure.
603 		 */
604 		if (!error) {
605 			dh_k4_clntdata_t *data;
606 			char *pf, *p;
607 
608 			secdata = kmem_alloc(sizeof (*secdata), KM_SLEEP);
609 			if (flags & NFSMNT_RPCTIMESYNC)
610 				secdata->flags |= AUTH_F_RPCTIMESYNC;
611 			data = kmem_alloc(sizeof (*data), KM_SLEEP);
612 			data->syncaddr = syncaddr;
613 
614 			/*
615 			 * duplicate the knconf information for the
616 			 * new opaque data.
617 			 */
618 			data->knconf = kmem_alloc(sizeof (*knconf), KM_SLEEP);
619 			*data->knconf = *knconf;
620 			pf = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
621 			p = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
622 			bcopy(knconf->knc_protofmly, pf, KNC_STRSIZE);
623 			bcopy(knconf->knc_proto, pf, KNC_STRSIZE);
624 			data->knconf->knc_protofmly = pf;
625 			data->knconf->knc_proto = p;
626 
627 			/* move server netname to the sec_data structure */
628 			if (nlen != 0) {
629 				data->netname = kmem_alloc(nlen, KM_SLEEP);
630 				bcopy(netname, data->netname, nlen);
631 				data->netnamelen = (int)nlen;
632 			}
633 			secdata->secmod = secdata->rpcflavor = AUTH_DES;
634 			secdata->data = (caddr_t)data;
635 		}
636 	} else {
637 		secdata = kmem_alloc(sizeof (*secdata), KM_SLEEP);
638 		secdata->secmod = secdata->rpcflavor = AUTH_UNIX;
639 		secdata->data = NULL;
640 	}
641 	svp->sv_secdata = secdata;
642 	if (error)
643 		goto errout;
644 
645 	/*
646 	 * See bug 1180236.
647 	 * If mount secure failed, we will fall back to AUTH_NONE
648 	 * and try again.  nfs3rootvp() will turn this back off.
649 	 *
650 	 * The NFS Version 3 mount uses the FSINFO and GETATTR
651 	 * procedures.  The server should not care if these procedures
652 	 * have the proper security flavor, so if mount retries using
653 	 * AUTH_NONE that does not require a credential setup for root
654 	 * then the automounter would work without requiring root to be
655 	 * keylogged into AUTH_DES.
656 	 */
657 	if (secdata->rpcflavor != AUTH_UNIX &&
658 	    secdata->rpcflavor != AUTH_LOOPBACK)
659 		secdata->flags |= AUTH_F_TRYNONE;
660 
661 	/*
662 	 * Failover support:
663 	 *
664 	 * We may have a linked list of nfs_args structures,
665 	 * which means the user is looking for failover.  If
666 	 * the mount is either not "read-only" or "soft",
667 	 * we want to bail out with EINVAL.
668 	 */
669 	if (STRUCT_FGET(args, nfs_args_ext) == NFS_ARGS_EXTB &&
670 	    STRUCT_FGETP(args, nfs_ext_u.nfs_extB.next) != NULL) {
671 		if (uap->flags & MS_RDONLY && !(flags & NFSMNT_SOFT)) {
672 			data = (char *)STRUCT_FGETP(args,
673 			    nfs_ext_u.nfs_extB.next);
674 			goto more;
675 		}
676 		error = EINVAL;
677 		goto errout;
678 	}
679 
680 	/*
681 	 * Determine the zone we're being mounted into.
682 	 */
683 	if (getzoneid() == GLOBAL_ZONEID) {
684 		zone_t *mntzone;
685 
686 		mntzone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
687 		ASSERT(mntzone != NULL);
688 		zone_rele(mntzone);
689 		if (mntzone != zone) {
690 			error = EBUSY;
691 			goto errout;
692 		}
693 	}
694 
695 	/*
696 	 * Stop the mount from going any further if the zone is going away.
697 	 */
698 	if (zone_status_get(curproc->p_zone) >= ZONE_IS_SHUTTING_DOWN) {
699 		error = EBUSY;
700 		goto errout;
701 	}
702 
703 	/*
704 	 * Get root vnode.
705 	 */
706 proceed:
707 	error = nfs3rootvp(&rtvp, vfsp, svp_head, flags, cr, zone);
708 
709 	if (error)
710 		goto errout;
711 
712 	/*
713 	 * Set option fields in the mount info record
714 	 */
715 	mi = VTOMI(rtvp);
716 
717 	if (svp_head->sv_next)
718 		mi->mi_flags |= MI_LLOCK;
719 
720 	error = nfs_setopts(rtvp, get_udatamodel(), STRUCT_BUF(args));
721 
722 errout:
723 	if (error) {
724 		if (rtvp != NULL) {
725 			rp = VTOR(rtvp);
726 			if (rp->r_flags & RHASHED)
727 				rp_rmhash(rp);
728 		}
729 		sv_free(svp_head);
730 		if (mi != NULL) {
731 			nfs_async_stop(vfsp);
732 			nfs_async_manager_stop(vfsp);
733 			if (mi->mi_io_kstats) {
734 				kstat_delete(mi->mi_io_kstats);
735 				mi->mi_io_kstats = NULL;
736 			}
737 			if (mi->mi_ro_kstats) {
738 				kstat_delete(mi->mi_ro_kstats);
739 				mi->mi_ro_kstats = NULL;
740 			}
741 			nfs_free_mi(mi);
742 		}
743 	}
744 
745 	if (rtvp != NULL)
746 		VN_RELE(rtvp);
747 
748 	return (error);
749 }
750 
751 static int nfs3_dynamic = 0;	/* global variable to enable dynamic retrans. */
752 static ushort_t nfs3_max_threads = 8;	/* max number of active async threads */
753 static uint_t nfs3_bsize = 32 * 1024;	/* client `block' size */
754 static uint_t nfs3_async_clusters = 1;	/* # of reqs from each async queue */
755 static uint_t nfs3_cots_timeo = NFS_COTS_TIMEO;
756 
757 static int
758 nfs3rootvp(vnode_t **rtvpp, vfs_t *vfsp, struct servinfo *svp,
759 	int flags, cred_t *cr, zone_t *zone)
760 {
761 	vnode_t *rtvp;
762 	mntinfo_t *mi;
763 	dev_t nfs_dev;
764 	struct vattr va;
765 	struct FSINFO3args args;
766 	struct FSINFO3res res;
767 	int error;
768 	int douprintf;
769 	rnode_t *rp;
770 	int i;
771 	uint_t max_transfer_size;
772 	struct nfs_stats *nfsstatsp;
773 	cred_t *lcr = NULL, *tcr = cr;
774 
775 	nfsstatsp = zone_getspecific(nfsstat_zone_key, nfs_zone());
776 	ASSERT(nfsstatsp != NULL);
777 
778 	ASSERT(nfs_zone() == zone);
779 	/*
780 	 * Create a mount record and link it to the vfs struct.
781 	 */
782 	mi = kmem_zalloc(sizeof (*mi), KM_SLEEP);
783 	mutex_init(&mi->mi_lock, NULL, MUTEX_DEFAULT, NULL);
784 	mi->mi_flags = MI_ACL | MI_EXTATTR;
785 	if (!(flags & NFSMNT_SOFT))
786 		mi->mi_flags |= MI_HARD;
787 	if ((flags & NFSMNT_SEMISOFT))
788 		mi->mi_flags |= MI_SEMISOFT;
789 	if ((flags & NFSMNT_NOPRINT))
790 		mi->mi_flags |= MI_NOPRINT;
791 	if (flags & NFSMNT_INT)
792 		mi->mi_flags |= MI_INT;
793 	mi->mi_retrans = NFS_RETRIES;
794 	if (svp->sv_knconf->knc_semantics == NC_TPI_COTS_ORD ||
795 	    svp->sv_knconf->knc_semantics == NC_TPI_COTS)
796 		mi->mi_timeo = nfs3_cots_timeo;
797 	else
798 		mi->mi_timeo = NFS_TIMEO;
799 	mi->mi_prog = NFS_PROGRAM;
800 	mi->mi_vers = NFS_V3;
801 	mi->mi_rfsnames = rfsnames_v3;
802 	mi->mi_reqs = nfsstatsp->nfs_stats_v3.rfsreqcnt_ptr;
803 	mi->mi_call_type = call_type_v3;
804 	mi->mi_ss_call_type = ss_call_type_v3;
805 	mi->mi_timer_type = timer_type_v3;
806 	mi->mi_aclnames = aclnames_v3;
807 	mi->mi_aclreqs = nfsstatsp->nfs_stats_v3.aclreqcnt_ptr;
808 	mi->mi_acl_call_type = acl_call_type_v3;
809 	mi->mi_acl_ss_call_type = acl_ss_call_type_v3;
810 	mi->mi_acl_timer_type = acl_timer_type_v3;
811 	cv_init(&mi->mi_failover_cv, NULL, CV_DEFAULT, NULL);
812 	mi->mi_servers = svp;
813 	mi->mi_curr_serv = svp;
814 	mi->mi_acregmin = SEC2HR(ACREGMIN);
815 	mi->mi_acregmax = SEC2HR(ACREGMAX);
816 	mi->mi_acdirmin = SEC2HR(ACDIRMIN);
817 	mi->mi_acdirmax = SEC2HR(ACDIRMAX);
818 
819 	if (nfs3_dynamic)
820 		mi->mi_flags |= MI_DYNAMIC;
821 
822 	if (flags & NFSMNT_DIRECTIO)
823 		mi->mi_flags |= MI_DIRECTIO;
824 
825 	/*
826 	 * Make a vfs struct for nfs.  We do this here instead of below
827 	 * because rtvp needs a vfs before we can do a getattr on it.
828 	 *
829 	 * Assign a unique device id to the mount
830 	 */
831 	mutex_enter(&nfs_minor_lock);
832 	do {
833 		nfs_minor = (nfs_minor + 1) & MAXMIN32;
834 		nfs_dev = makedevice(nfs_major, nfs_minor);
835 	} while (vfs_devismounted(nfs_dev));
836 	mutex_exit(&nfs_minor_lock);
837 
838 	vfsp->vfs_dev = nfs_dev;
839 	vfs_make_fsid(&vfsp->vfs_fsid, nfs_dev, nfs3fstyp);
840 	vfsp->vfs_data = (caddr_t)mi;
841 	vfsp->vfs_fstype = nfsfstyp;
842 
843 	/*
844 	 * Verify that nfs3_bsize tuneable is set to an
845 	 * acceptable value.  It be a multiple of PAGESIZE or
846 	 * file corruption can occur.
847 	 */
848 	if (nfs3_bsize & PAGEOFFSET)
849 		nfs3_bsize &= PAGEMASK;
850 	if (nfs3_bsize < PAGESIZE)
851 		nfs3_bsize = PAGESIZE;
852 	vfsp->vfs_bsize = nfs3_bsize;
853 
854 	/*
855 	 * Initialize fields used to support async putpage operations.
856 	 */
857 	for (i = 0; i < NFS_ASYNC_TYPES; i++)
858 		mi->mi_async_clusters[i] = nfs3_async_clusters;
859 	mi->mi_async_init_clusters = nfs3_async_clusters;
860 	mi->mi_async_curr = &mi->mi_async_reqs[0];
861 	mi->mi_max_threads = nfs3_max_threads;
862 	mutex_init(&mi->mi_async_lock, NULL, MUTEX_DEFAULT, NULL);
863 	cv_init(&mi->mi_async_reqs_cv, NULL, CV_DEFAULT, NULL);
864 	cv_init(&mi->mi_async_work_cv, NULL, CV_DEFAULT, NULL);
865 	cv_init(&mi->mi_async_cv, NULL, CV_DEFAULT, NULL);
866 
867 	mi->mi_vfsp = vfsp;
868 	zone_hold(mi->mi_zone = zone);
869 	nfs_mi_zonelist_add(mi);
870 
871 	/*
872 	 * Make the root vnode, use it to get attributes,
873 	 * then remake it with the attributes.
874 	 */
875 	rtvp = makenfs3node((nfs_fh3 *)&svp->sv_fhandle,
876 	    NULL, vfsp, gethrtime(), cr, NULL, NULL);
877 
878 	/*
879 	 * Make the FSINFO calls, primarily at this point to
880 	 * determine the transfer size.  For client failover,
881 	 * we'll want this to be the minimum bid from any
882 	 * server, so that we don't overrun stated limits.
883 	 *
884 	 * While we're looping, we'll turn off AUTH_F_TRYNONE,
885 	 * which is only for the mount operation.
886 	 */
887 
888 	mi->mi_tsize = nfs3_tsize(svp->sv_knconf);
889 	mi->mi_stsize = mi->mi_tsize;
890 
891 	mi->mi_curread = nfs3_bsize;
892 	mi->mi_curwrite = mi->mi_curread;
893 
894 	/*
895 	 * If the uid is set then set the creds for secure mounts
896 	 * by proxy processes such as automountd.
897 	 */
898 	if (svp->sv_secdata->uid != 0 &&
899 	    svp->sv_secdata->rpcflavor == RPCSEC_GSS) {
900 		lcr = crdup(cr);
901 		(void) crsetugid(lcr, svp->sv_secdata->uid, crgetgid(cr));
902 		tcr = lcr;
903 	}
904 
905 	for (svp = mi->mi_servers; svp != NULL; svp = svp->sv_next) {
906 		douprintf = 1;
907 		mi->mi_curr_serv = svp;
908 		max_transfer_size = nfs3_tsize(svp->sv_knconf);
909 		mi->mi_tsize = MIN(max_transfer_size, mi->mi_tsize);
910 		mi->mi_stsize = MIN(max_transfer_size, mi->mi_stsize);
911 		mi->mi_curread = MIN(max_transfer_size, mi->mi_curread);
912 		mi->mi_curwrite = MIN(max_transfer_size, mi->mi_curwrite);
913 		args.fsroot = *(nfs_fh3 *)&svp->sv_fhandle;
914 
915 		error = rfs3call(mi, NFSPROC3_FSINFO,
916 		    xdr_nfs_fh3, (caddr_t)&args,
917 		    xdr_FSINFO3res, (caddr_t)&res, tcr,
918 		    &douprintf, &res.status, 0, NULL);
919 		if (error)
920 			goto bad;
921 		error = geterrno3(res.status);
922 		if (error)
923 			goto bad;
924 
925 		/* get type of root node */
926 		if (res.resok.obj_attributes.attributes) {
927 			if (res.resok.obj_attributes.attr.type < NF3REG ||
928 			    res.resok.obj_attributes.attr.type > NF3FIFO) {
929 #ifdef DEBUG
930 				zcmn_err(getzoneid(), CE_WARN,
931 			    "NFS3 server %s returned a bad file type for root",
932 				    svp->sv_hostname);
933 #else
934 				zcmn_err(getzoneid(), CE_WARN,
935 			    "NFS server %s returned a bad file type for root",
936 				    svp->sv_hostname);
937 #endif
938 				error = EINVAL;
939 				goto bad;
940 			} else {
941 				if (rtvp->v_type != VNON &&
942 		rtvp->v_type != nf3_to_vt[res.resok.obj_attributes.attr.type]) {
943 #ifdef DEBUG
944 					zcmn_err(getzoneid(), CE_WARN,
945 		"NFS3 server %s returned a different file type for root",
946 					    svp->sv_hostname);
947 #else
948 					zcmn_err(getzoneid(), CE_WARN,
949 		"NFS server %s returned a different file type for root",
950 					    svp->sv_hostname);
951 #endif
952 					error = EINVAL;
953 					goto bad;
954 				}
955 				rtvp->v_type =
956 				nf3_to_vt[res.resok.obj_attributes.attr.type];
957 			}
958 		}
959 
960 		if (res.resok.rtmax != 0) {
961 			mi->mi_tsize = MIN(res.resok.rtmax, mi->mi_tsize);
962 			if (res.resok.rtpref != 0) {
963 				mi->mi_curread = MIN(res.resok.rtpref,
964 						    mi->mi_curread);
965 			} else {
966 				mi->mi_curread = MIN(res.resok.rtmax,
967 						    mi->mi_curread);
968 			}
969 		} else if (res.resok.rtpref != 0) {
970 			mi->mi_tsize = MIN(res.resok.rtpref, mi->mi_tsize);
971 			mi->mi_curread = MIN(res.resok.rtpref, mi->mi_curread);
972 		} else {
973 #ifdef DEBUG
974 			zcmn_err(getzoneid(), CE_WARN,
975 			    "NFS3 server %s returned 0 for read transfer sizes",
976 			    svp->sv_hostname);
977 #else
978 			zcmn_err(getzoneid(), CE_WARN,
979 			    "NFS server %s returned 0 for read transfer sizes",
980 			    svp->sv_hostname);
981 #endif
982 			error = EIO;
983 			goto bad;
984 		}
985 		if (res.resok.wtmax != 0) {
986 			mi->mi_stsize = MIN(res.resok.wtmax, mi->mi_stsize);
987 			if (res.resok.wtpref != 0) {
988 				mi->mi_curwrite = MIN(res.resok.wtpref,
989 						    mi->mi_curwrite);
990 			} else {
991 				mi->mi_curwrite = MIN(res.resok.wtmax,
992 						    mi->mi_curwrite);
993 			}
994 		} else if (res.resok.wtpref != 0) {
995 			mi->mi_stsize = MIN(res.resok.wtpref, mi->mi_stsize);
996 			mi->mi_curwrite = MIN(res.resok.wtpref,
997 					    mi->mi_curwrite);
998 		} else {
999 #ifdef DEBUG
1000 			zcmn_err(getzoneid(), CE_WARN,
1001 			"NFS3 server %s returned 0 for write transfer sizes",
1002 			    svp->sv_hostname);
1003 #else
1004 			zcmn_err(getzoneid(), CE_WARN,
1005 			"NFS server %s returned 0 for write transfer sizes",
1006 			    svp->sv_hostname);
1007 #endif
1008 			error = EIO;
1009 			goto bad;
1010 		}
1011 
1012 		/*
1013 		 * These signal the ability of the server to create
1014 		 * hard links and symbolic links, so they really
1015 		 * aren't relevant if there is more than one server.
1016 		 * We'll set them here, though it probably looks odd.
1017 		 */
1018 		if (res.resok.properties & FSF3_LINK)
1019 			mi->mi_flags |= MI_LINK;
1020 		if (res.resok.properties & FSF3_SYMLINK)
1021 			mi->mi_flags |= MI_SYMLINK;
1022 
1023 		/* Pick up smallest non-zero maxfilesize value */
1024 		if (res.resok.maxfilesize) {
1025 			if (mi->mi_maxfilesize) {
1026 				mi->mi_maxfilesize = MIN(mi->mi_maxfilesize,
1027 							res.resok.maxfilesize);
1028 			} else
1029 				mi->mi_maxfilesize = res.resok.maxfilesize;
1030 		}
1031 
1032 		/*
1033 		 * AUTH_F_TRYNONE is only for the mount operation,
1034 		 * so turn it back off.
1035 		 */
1036 		svp->sv_secdata->flags &= ~AUTH_F_TRYNONE;
1037 	}
1038 	mi->mi_curr_serv = mi->mi_servers;
1039 
1040 	/*
1041 	 * Start the thread responsible for handling async worker threads.
1042 	 */
1043 	VFS_HOLD(vfsp);	/* add reference for thread */
1044 	mi->mi_manager_thread = zthread_create(NULL, 0, nfs_async_manager,
1045 					vfsp, 0, minclsyspri);
1046 	ASSERT(mi->mi_manager_thread != NULL);
1047 
1048 	/*
1049 	 * Initialize kstats
1050 	 */
1051 	nfs_mnt_kstat_init(vfsp);
1052 
1053 	/* If we didn't get a type, get one now */
1054 	if (rtvp->v_type == VNON) {
1055 		va.va_mask = AT_ALL;
1056 
1057 		error = nfs3getattr(rtvp, &va, tcr);
1058 		if (error)
1059 			goto bad;
1060 		rtvp->v_type = va.va_type;
1061 	}
1062 
1063 	mi->mi_type = rtvp->v_type;
1064 
1065 	*rtvpp = rtvp;
1066 	if (lcr != NULL)
1067 		crfree(lcr);
1068 
1069 	return (0);
1070 bad:
1071 	/*
1072 	 * An error occurred somewhere, need to clean up...
1073 	 * We need to release our reference to the root vnode and
1074 	 * destroy the mntinfo struct that we just created.
1075 	 */
1076 	if (lcr != NULL)
1077 		crfree(lcr);
1078 	rp = VTOR(rtvp);
1079 	if (rp->r_flags & RHASHED)
1080 		rp_rmhash(rp);
1081 	VN_RELE(rtvp);
1082 	nfs_async_stop(vfsp);
1083 	nfs_async_manager_stop(vfsp);
1084 	if (mi->mi_io_kstats) {
1085 		kstat_delete(mi->mi_io_kstats);
1086 		mi->mi_io_kstats = NULL;
1087 	}
1088 	if (mi->mi_ro_kstats) {
1089 		kstat_delete(mi->mi_ro_kstats);
1090 		mi->mi_ro_kstats = NULL;
1091 	}
1092 	nfs_free_mi(mi);
1093 	*rtvpp = NULL;
1094 	return (error);
1095 }
1096 
1097 /*
1098  * vfs operations
1099  */
1100 static int
1101 nfs3_unmount(vfs_t *vfsp, int flag, cred_t *cr)
1102 {
1103 	mntinfo_t *mi;
1104 	ushort_t omax;
1105 
1106 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
1107 		return (EPERM);
1108 
1109 	mi = VFTOMI(vfsp);
1110 	if (flag & MS_FORCE) {
1111 		vfsp->vfs_flag |= VFS_UNMOUNTED;
1112 		/*
1113 		 * We need to stop the manager thread explicitly; the worker
1114 		 * threads can time out and exit on their own.
1115 		 */
1116 		nfs_async_manager_stop(vfsp);
1117 		destroy_rtable(vfsp, cr);
1118 		if (mi->mi_io_kstats) {
1119 			kstat_delete(mi->mi_io_kstats);
1120 			mi->mi_io_kstats = NULL;
1121 		}
1122 		if (mi->mi_ro_kstats) {
1123 			kstat_delete(mi->mi_ro_kstats);
1124 			mi->mi_ro_kstats = NULL;
1125 		}
1126 		return (0);
1127 	}
1128 	/*
1129 	 * Wait until all asynchronous putpage operations on
1130 	 * this file system are complete before flushing rnodes
1131 	 * from the cache.
1132 	 */
1133 	omax = mi->mi_max_threads;
1134 	if (nfs_async_stop_sig(vfsp)) {
1135 		return (EINTR);
1136 	}
1137 	rflush(vfsp, cr);
1138 	/*
1139 	 * If there are any active vnodes on this file system,
1140 	 * then the file system is busy and can't be umounted.
1141 	 */
1142 	if (check_rtable(vfsp)) {
1143 		mutex_enter(&mi->mi_async_lock);
1144 		mi->mi_max_threads = omax;
1145 		mutex_exit(&mi->mi_async_lock);
1146 		return (EBUSY);
1147 	}
1148 	/*
1149 	 * The unmount can't fail from now on; stop the worker thread manager.
1150 	 */
1151 	nfs_async_manager_stop(vfsp);
1152 	/*
1153 	 * Destroy all rnodes belonging to this file system from the
1154 	 * rnode hash queues and purge any resources allocated to
1155 	 * them.
1156 	 */
1157 	destroy_rtable(vfsp, cr);
1158 	if (mi->mi_io_kstats) {
1159 		kstat_delete(mi->mi_io_kstats);
1160 		mi->mi_io_kstats = NULL;
1161 	}
1162 	if (mi->mi_ro_kstats) {
1163 		kstat_delete(mi->mi_ro_kstats);
1164 		mi->mi_ro_kstats = NULL;
1165 	}
1166 	return (0);
1167 }
1168 
1169 /*
1170  * find root of nfs
1171  */
1172 static int
1173 nfs3_root(vfs_t *vfsp, vnode_t **vpp)
1174 {
1175 	mntinfo_t *mi;
1176 	vnode_t *vp;
1177 	servinfo_t *svp;
1178 
1179 	mi = VFTOMI(vfsp);
1180 
1181 	if (nfs_zone() != mi->mi_zone)
1182 		return (EPERM);
1183 
1184 	svp = mi->mi_curr_serv;
1185 	if (svp && (svp->sv_flags & SV_ROOT_STALE)) {
1186 		mutex_enter(&svp->sv_lock);
1187 		svp->sv_flags &= ~SV_ROOT_STALE;
1188 		mutex_exit(&svp->sv_lock);
1189 		return (ENOENT);
1190 	}
1191 
1192 	vp = makenfs3node((nfs_fh3 *)&mi->mi_curr_serv->sv_fhandle,
1193 	    NULL, vfsp, gethrtime(), CRED(), NULL, NULL);
1194 
1195 	if (VTOR(vp)->r_flags & RSTALE) {
1196 		VN_RELE(vp);
1197 		return (ENOENT);
1198 	}
1199 
1200 	ASSERT(vp->v_type == VNON || vp->v_type == mi->mi_type);
1201 
1202 	vp->v_type = mi->mi_type;
1203 
1204 	*vpp = vp;
1205 
1206 	return (0);
1207 }
1208 
1209 /*
1210  * Get file system statistics.
1211  */
1212 static int
1213 nfs3_statvfs(vfs_t *vfsp, struct statvfs64 *sbp)
1214 {
1215 	int error;
1216 	struct mntinfo *mi;
1217 	struct FSSTAT3args args;
1218 	struct FSSTAT3res res;
1219 	int douprintf;
1220 	failinfo_t fi;
1221 	vnode_t *vp;
1222 	cred_t *cr;
1223 	hrtime_t t;
1224 
1225 	mi = VFTOMI(vfsp);
1226 	if (nfs_zone() != mi->mi_zone)
1227 		return (EPERM);
1228 	error = nfs3_root(vfsp, &vp);
1229 	if (error)
1230 		return (error);
1231 
1232 	cr = CRED();
1233 
1234 	args.fsroot = *VTOFH3(vp);
1235 	fi.vp = vp;
1236 	fi.fhp = (caddr_t)&args.fsroot;
1237 	fi.copyproc = nfs3copyfh;
1238 	fi.lookupproc = nfs3lookup;
1239 	fi.xattrdirproc = acl_getxattrdir3;
1240 
1241 	douprintf = 1;
1242 
1243 	t = gethrtime();
1244 
1245 	error = rfs3call(mi, NFSPROC3_FSSTAT,
1246 	    xdr_nfs_fh3, (caddr_t)&args,
1247 	    xdr_FSSTAT3res, (caddr_t)&res, cr,
1248 	    &douprintf, &res.status, 0, &fi);
1249 
1250 	if (error) {
1251 		VN_RELE(vp);
1252 		return (error);
1253 	}
1254 
1255 	error = geterrno3(res.status);
1256 	if (!error) {
1257 		nfs3_cache_post_op_attr(vp, &res.resok.obj_attributes, t, cr);
1258 		sbp->f_bsize = MAXBSIZE;
1259 		sbp->f_frsize = DEV_BSIZE;
1260 		/*
1261 		 * Allow -1 fields to pass through unconverted.  These
1262 		 * indicate "don't know" fields.
1263 		 */
1264 		if (res.resok.tbytes == (size3)-1)
1265 			sbp->f_blocks = (fsblkcnt64_t)res.resok.tbytes;
1266 		else {
1267 			sbp->f_blocks = (fsblkcnt64_t)
1268 			    (res.resok.tbytes / DEV_BSIZE);
1269 		}
1270 		if (res.resok.fbytes == (size3)-1)
1271 			sbp->f_bfree = (fsblkcnt64_t)res.resok.fbytes;
1272 		else {
1273 			sbp->f_bfree = (fsblkcnt64_t)
1274 			    (res.resok.fbytes / DEV_BSIZE);
1275 		}
1276 		if (res.resok.abytes == (size3)-1)
1277 			sbp->f_bavail = (fsblkcnt64_t)res.resok.abytes;
1278 		else {
1279 			sbp->f_bavail = (fsblkcnt64_t)
1280 			    (res.resok.abytes / DEV_BSIZE);
1281 		}
1282 		sbp->f_files = (fsfilcnt64_t)res.resok.tfiles;
1283 		sbp->f_ffree = (fsfilcnt64_t)res.resok.ffiles;
1284 		sbp->f_favail = (fsfilcnt64_t)res.resok.afiles;
1285 		sbp->f_fsid = (unsigned long)vfsp->vfs_fsid.val[0];
1286 		(void) strncpy(sbp->f_basetype,
1287 		    vfssw[vfsp->vfs_fstype].vsw_name, FSTYPSZ);
1288 		sbp->f_flag = vf_to_stf(vfsp->vfs_flag);
1289 		sbp->f_namemax = (ulong_t)-1;
1290 	} else {
1291 		nfs3_cache_post_op_attr(vp, &res.resfail.obj_attributes, t, cr);
1292 		PURGE_STALE_FH(error, vp, cr);
1293 	}
1294 
1295 	VN_RELE(vp);
1296 
1297 	return (error);
1298 }
1299 
1300 static kmutex_t nfs3_syncbusy;
1301 
1302 /*
1303  * Flush dirty nfs files for file system vfsp.
1304  * If vfsp == NULL, all nfs files are flushed.
1305  */
1306 /* ARGSUSED */
1307 static int
1308 nfs3_sync(vfs_t *vfsp, short flag, cred_t *cr)
1309 {
1310 	/*
1311 	 * Cross-zone calls are OK here, since this translates to a
1312 	 * VOP_PUTPAGE(B_ASYNC), which gets picked up by the right zone.
1313 	 */
1314 	if (!(flag & SYNC_ATTR) && mutex_tryenter(&nfs3_syncbusy) != 0) {
1315 		rflush(vfsp, cr);
1316 		mutex_exit(&nfs3_syncbusy);
1317 	}
1318 	return (0);
1319 }
1320 
1321 /* ARGSUSED */
1322 static int
1323 nfs3_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1324 {
1325 	int error;
1326 	nfs_fh3 fh;
1327 	vnode_t *vp;
1328 	struct vattr va;
1329 
1330 	if (fidp->fid_len > NFS3_FHSIZE) {
1331 		*vpp = NULL;
1332 		return (ESTALE);
1333 	}
1334 
1335 	if (nfs_zone() != VFTOMI(vfsp)->mi_zone)
1336 		return (EPERM);
1337 	fh.fh3_length = fidp->fid_len;
1338 	bcopy(fidp->fid_data, fh.fh3_u.data, fh.fh3_length);
1339 
1340 	vp = makenfs3node(&fh, NULL, vfsp, gethrtime(), CRED(), NULL, NULL);
1341 
1342 	if (VTOR(vp)->r_flags & RSTALE) {
1343 		VN_RELE(vp);
1344 		*vpp = NULL;
1345 		return (ENOENT);
1346 	}
1347 
1348 	if (vp->v_type == VNON) {
1349 		va.va_mask = AT_ALL;
1350 		error = nfs3getattr(vp, &va, CRED());
1351 		if (error) {
1352 			VN_RELE(vp);
1353 			*vpp = NULL;
1354 			return (error);
1355 		}
1356 		vp->v_type = va.va_type;
1357 	}
1358 
1359 	*vpp = vp;
1360 
1361 	return (0);
1362 }
1363 
1364 /* ARGSUSED */
1365 static int
1366 nfs3_mountroot(vfs_t *vfsp, whymountroot_t why)
1367 {
1368 	vnode_t *rtvp;
1369 	char root_hostname[SYS_NMLN+1];
1370 	struct servinfo *svp;
1371 	int error;
1372 	int vfsflags;
1373 	size_t size;
1374 	char *root_path;
1375 	struct pathname pn;
1376 	char *name;
1377 	cred_t *cr;
1378 	struct nfs_args args;		/* nfs mount arguments */
1379 	static char token[10];
1380 
1381 	bzero(&args, sizeof (args));
1382 
1383 	/* do this BEFORE getfile which causes xid stamps to be initialized */
1384 	clkset(-1L);		/* hack for now - until we get time svc? */
1385 
1386 	if (why == ROOT_REMOUNT) {
1387 		/*
1388 		 * Shouldn't happen.
1389 		 */
1390 		panic("nfs3_mountroot: why == ROOT_REMOUNT");
1391 	}
1392 
1393 	if (why == ROOT_UNMOUNT) {
1394 		/*
1395 		 * Nothing to do for NFS.
1396 		 */
1397 		return (0);
1398 	}
1399 
1400 	/*
1401 	 * why == ROOT_INIT
1402 	 */
1403 
1404 	name = token;
1405 	*name = 0;
1406 	getfsname("root", name, sizeof (token));
1407 
1408 	pn_alloc(&pn);
1409 	root_path = pn.pn_path;
1410 
1411 	svp = kmem_zalloc(sizeof (*svp), KM_SLEEP);
1412 	svp->sv_knconf = kmem_zalloc(sizeof (*svp->sv_knconf), KM_SLEEP);
1413 	svp->sv_knconf->knc_protofmly = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
1414 	svp->sv_knconf->knc_proto = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
1415 
1416 	/*
1417 	 * Get server address
1418 	 * Get the root fhandle
1419 	 * Get server's transport
1420 	 * Get server's hostname
1421 	 * Get options
1422 	 */
1423 	args.addr = &svp->sv_addr;
1424 	args.fh = (char *)&svp->sv_fhandle;
1425 	args.knconf = svp->sv_knconf;
1426 	args.hostname = root_hostname;
1427 	vfsflags = 0;
1428 	if (error = mount_root(*name ? name : "root", root_path, NFS_V3,
1429 	    &args, &vfsflags)) {
1430 		if (error == EPROTONOSUPPORT)
1431 			nfs_cmn_err(error, CE_WARN, "nfs3_mountroot: "
1432 			    "mount_root failed: server doesn't support NFS V3");
1433 		else
1434 			nfs_cmn_err(error, CE_WARN,
1435 			    "nfs3_mountroot: mount_root failed: %m");
1436 		sv_free(svp);
1437 		pn_free(&pn);
1438 		return (error);
1439 	}
1440 	svp->sv_hostnamelen = (int)(strlen(root_hostname) + 1);
1441 	svp->sv_hostname = kmem_alloc(svp->sv_hostnamelen, KM_SLEEP);
1442 	(void) strcpy(svp->sv_hostname, root_hostname);
1443 
1444 	/*
1445 	 * Force root partition to always be mounted with AUTH_UNIX for now
1446 	 */
1447 	svp->sv_secdata = kmem_alloc(sizeof (*svp->sv_secdata), KM_SLEEP);
1448 	svp->sv_secdata->secmod = AUTH_UNIX;
1449 	svp->sv_secdata->rpcflavor = AUTH_UNIX;
1450 	svp->sv_secdata->data = NULL;
1451 
1452 	cr = crgetcred();
1453 	rtvp = NULL;
1454 
1455 	error = nfs3rootvp(&rtvp, vfsp, svp, args.flags, cr, global_zone);
1456 
1457 	crfree(cr);
1458 
1459 	if (error) {
1460 		pn_free(&pn);
1461 		goto errout;
1462 	}
1463 
1464 	error = nfs_setopts(rtvp, DATAMODEL_NATIVE, &args);
1465 	if (error) {
1466 		nfs_cmn_err(error, CE_WARN,
1467 		    "nfs3_mountroot: invalid root mount options");
1468 		pn_free(&pn);
1469 		goto errout;
1470 	}
1471 
1472 	(void) vfs_lock_wait(vfsp);
1473 	vfs_add(NULL, vfsp, vfsflags);
1474 	vfs_unlock(vfsp);
1475 
1476 	size = strlen(svp->sv_hostname);
1477 	(void) strcpy(rootfs.bo_name, svp->sv_hostname);
1478 	rootfs.bo_name[size] = ':';
1479 	(void) strcpy(&rootfs.bo_name[size + 1], root_path);
1480 
1481 	pn_free(&pn);
1482 
1483 errout:
1484 	if (error) {
1485 		sv_free(svp);
1486 		nfs_async_stop(vfsp);
1487 		nfs_async_manager_stop(vfsp);
1488 	}
1489 
1490 	if (rtvp != NULL)
1491 		VN_RELE(rtvp);
1492 
1493 	return (error);
1494 }
1495 
1496 /*
1497  * Initialization routine for VFS routines.  Should only be called once
1498  */
1499 int
1500 nfs3_vfsinit(void)
1501 {
1502 	mutex_init(&nfs3_syncbusy, NULL, MUTEX_DEFAULT, NULL);
1503 	return (0);
1504 }
1505 
1506 void
1507 nfs3_vfsfini(void)
1508 {
1509 	mutex_destroy(&nfs3_syncbusy);
1510 }
1511 
1512 void
1513 nfs3_freevfs(vfs_t *vfsp)
1514 {
1515 	mntinfo_t *mi;
1516 	servinfo_t *svp;
1517 
1518 	/* free up the resources */
1519 	mi = VFTOMI(vfsp);
1520 	svp = mi->mi_servers;
1521 	mi->mi_servers = mi->mi_curr_serv = NULL;
1522 	sv_free(svp);
1523 
1524 	/*
1525 	 * By this time we should have already deleted the
1526 	 * mi kstats in the unmount code. If they are still around
1527 	 * somethings wrong
1528 	 */
1529 	ASSERT(mi->mi_io_kstats == NULL);
1530 	nfs_free_mi(mi);
1531 }
1532