xref: /titanic_52/usr/src/uts/common/fs/nfs/nfs_vfsops.c (revision f498645a3eecf2ddd304b4ea9c7f1b4c155ff79e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  *	Copyright (c) 1983,1984,1985,1986,1987,1988,1989  AT&T.
26  *	All rights reserved.
27  */
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/cred.h>
35 #include <sys/vfs.h>
36 #include <sys/vnode.h>
37 #include <sys/pathname.h>
38 #include <sys/sysmacros.h>
39 #include <sys/kmem.h>
40 #include <sys/mkdev.h>
41 #include <sys/mount.h>
42 #include <sys/mntent.h>
43 #include <sys/statvfs.h>
44 #include <sys/errno.h>
45 #include <sys/debug.h>
46 #include <sys/cmn_err.h>
47 #include <sys/utsname.h>
48 #include <sys/bootconf.h>
49 #include <sys/modctl.h>
50 #include <sys/acl.h>
51 #include <sys/flock.h>
52 #include <sys/policy.h>
53 #include <sys/zone.h>
54 #include <sys/class.h>
55 #include <sys/socket.h>
56 #include <sys/netconfig.h>
57 #include <sys/mntent.h>
58 #include <sys/tsol/label.h>
59 
60 #include <rpc/types.h>
61 #include <rpc/auth.h>
62 #include <rpc/clnt.h>
63 
64 #include <nfs/nfs.h>
65 #include <nfs/nfs_clnt.h>
66 #include <nfs/rnode.h>
67 #include <nfs/mount.h>
68 #include <nfs/nfs_acl.h>
69 
70 #include <fs/fs_subr.h>
71 
72 /*
73  * From rpcsec module (common/rpcsec).
74  */
75 extern int sec_clnt_loadinfo(struct sec_data *, struct sec_data **, model_t);
76 extern void sec_clnt_freeinfo(struct sec_data *);
77 
78 static int pathconf_copyin(struct nfs_args *, struct pathcnf *);
79 static int pathconf_get(struct mntinfo *, struct nfs_args *);
80 static void pathconf_rele(struct mntinfo *);
81 
82 /*
83  * The order and contents of this structure must be kept in sync with that of
84  * rfsreqcnt_v2_tmpl in nfs_stats.c
85  */
86 static char *rfsnames_v2[] = {
87 	"null", "getattr", "setattr", "unused", "lookup", "readlink", "read",
88 	"unused", "write", "create", "remove", "rename", "link", "symlink",
89 	"mkdir", "rmdir", "readdir", "fsstat"
90 };
91 
92 /*
93  * This table maps from NFS protocol number into call type.
94  * Zero means a "Lookup" type call
95  * One  means a "Read" type call
96  * Two  means a "Write" type call
97  * This is used to select a default time-out.
98  */
99 static uchar_t call_type_v2[] = {
100 	0, 0, 1, 0, 0, 0, 1,
101 	0, 2, 2, 2, 2, 2, 2,
102 	2, 2, 1, 0
103 };
104 
105 /*
106  * Similar table, but to determine which timer to use
107  * (only real reads and writes!)
108  */
109 static uchar_t timer_type_v2[] = {
110 	0, 0, 0, 0, 0, 0, 1,
111 	0, 2, 0, 0, 0, 0, 0,
112 	0, 0, 1, 0
113 };
114 
115 /*
116  * This table maps from NFS protocol number into a call type
117  * for the semisoft mount option.
118  * Zero means do not repeat operation.
119  * One  means repeat.
120  */
121 static uchar_t ss_call_type_v2[] = {
122 	0, 0, 1, 0, 0, 0, 0,
123 	0, 1, 1, 1, 1, 1, 1,
124 	1, 1, 0, 0
125 };
126 
127 /*
128  * nfs vfs operations.
129  */
130 static int	nfs_mount(vfs_t *, vnode_t *, struct mounta *, cred_t *);
131 static int	nfs_unmount(vfs_t *, int, cred_t *);
132 static int	nfs_root(vfs_t *, vnode_t **);
133 static int	nfs_statvfs(vfs_t *, struct statvfs64 *);
134 static int	nfs_sync(vfs_t *, short, cred_t *);
135 static int	nfs_vget(vfs_t *, vnode_t **, fid_t *);
136 static int	nfs_mountroot(vfs_t *, whymountroot_t);
137 static void	nfs_freevfs(vfs_t *);
138 
139 static int	nfsrootvp(vnode_t **, vfs_t *, struct servinfo *,
140 		    int, cred_t *, zone_t *);
141 
142 /*
143  * Initialize the vfs structure
144  */
145 
146 int nfsfstyp;
147 vfsops_t *nfs_vfsops;
148 
149 /*
150  * Debug variable to check for rdma based
151  * transport startup and cleanup. Controlled
152  * through /etc/system. Off by default.
153  */
154 int rdma_debug = 0;
155 
156 int
157 nfsinit(int fstyp, char *name)
158 {
159 	static const fs_operation_def_t nfs_vfsops_template[] = {
160 		VFSNAME_MOUNT, nfs_mount,
161 		VFSNAME_UNMOUNT, nfs_unmount,
162 		VFSNAME_ROOT, nfs_root,
163 		VFSNAME_STATVFS, nfs_statvfs,
164 		VFSNAME_SYNC, (fs_generic_func_p) nfs_sync,
165 		VFSNAME_VGET, nfs_vget,
166 		VFSNAME_MOUNTROOT, nfs_mountroot,
167 		VFSNAME_FREEVFS, (fs_generic_func_p)nfs_freevfs,
168 		NULL, NULL
169 	};
170 	int error;
171 
172 	error = vfs_setfsops(fstyp, nfs_vfsops_template, &nfs_vfsops);
173 	if (error != 0) {
174 		zcmn_err(GLOBAL_ZONEID, CE_WARN,
175 		    "nfsinit: bad vfs ops template");
176 		return (error);
177 	}
178 
179 	error = vn_make_ops(name, nfs_vnodeops_template, &nfs_vnodeops);
180 	if (error != 0) {
181 		(void) vfs_freevfsops_by_type(fstyp);
182 		zcmn_err(GLOBAL_ZONEID, CE_WARN,
183 		    "nfsinit: bad vnode ops template");
184 		return (error);
185 	}
186 
187 
188 	nfsfstyp = fstyp;
189 
190 	return (0);
191 }
192 
193 void
194 nfsfini(void)
195 {
196 }
197 
198 static void
199 nfs_free_args(struct nfs_args *nargs, nfs_fhandle *fh)
200 {
201 
202 	if (fh)
203 		kmem_free(fh, sizeof (*fh));
204 
205 	if (nargs->pathconf) {
206 		kmem_free(nargs->pathconf, sizeof (struct pathcnf));
207 		nargs->pathconf = NULL;
208 	}
209 
210 	if (nargs->knconf) {
211 		if (nargs->knconf->knc_protofmly)
212 			kmem_free(nargs->knconf->knc_protofmly,
213 				KNC_STRSIZE);
214 		if (nargs->knconf->knc_proto)
215 			kmem_free(nargs->knconf->knc_proto, KNC_STRSIZE);
216 		kmem_free(nargs->knconf, sizeof (*nargs->knconf));
217 		nargs->knconf = NULL;
218 	}
219 
220 	if (nargs->fh) {
221 		kmem_free(nargs->fh, strlen(nargs->fh) + 1);
222 		nargs->fh = NULL;
223 	}
224 
225 	if (nargs->hostname) {
226 		kmem_free(nargs->hostname, strlen(nargs->hostname) + 1);
227 		nargs->hostname = NULL;
228 	}
229 
230 	if (nargs->addr) {
231 		if (nargs->addr->buf) {
232 			ASSERT(nargs->addr->len);
233 			kmem_free(nargs->addr->buf, nargs->addr->len);
234 		}
235 		kmem_free(nargs->addr, sizeof (struct netbuf));
236 		nargs->addr = NULL;
237 	}
238 
239 	if (nargs->syncaddr) {
240 		ASSERT(nargs->syncaddr->len);
241 		if (nargs->syncaddr->buf) {
242 			ASSERT(nargs->syncaddr->len);
243 			kmem_free(nargs->syncaddr->buf, nargs->syncaddr->len);
244 		}
245 		kmem_free(nargs->syncaddr, sizeof (struct netbuf));
246 		nargs->syncaddr = NULL;
247 	}
248 
249 	if (nargs->netname) {
250 		kmem_free(nargs->netname, strlen(nargs->netname) + 1);
251 		nargs->netname = NULL;
252 	}
253 
254 	if (nargs->nfs_ext_u.nfs_extA.secdata) {
255 		sec_clnt_freeinfo(
256 			nargs->nfs_ext_u.nfs_extA.secdata);
257 		nargs->nfs_ext_u.nfs_extA.secdata = NULL;
258 	}
259 }
260 
261 static int
262 nfs_copyin(char *data, int datalen, struct nfs_args *nargs, nfs_fhandle *fh)
263 {
264 
265 	int error;
266 	size_t nlen;			/* length of netname */
267 	size_t hlen;			/* length of hostname */
268 	char netname[MAXNETNAMELEN+1];	/* server's netname */
269 	struct netbuf addr;		/* server's address */
270 	struct netbuf syncaddr;		/* AUTH_DES time sync addr */
271 	struct knetconfig *knconf;	/* transport knetconfig structure */
272 	struct sec_data *secdata = NULL;	/* security data */
273 	STRUCT_DECL(nfs_args, args);		/* nfs mount arguments */
274 	STRUCT_DECL(knetconfig, knconf_tmp);
275 	STRUCT_DECL(netbuf, addr_tmp);
276 	int flags;
277 	struct pathcnf	*pc;		/* Pathconf */
278 	char *p, *pf;
279 	char *userbufptr;
280 
281 
282 	bzero(nargs, sizeof (*nargs));
283 
284 	STRUCT_INIT(args, get_udatamodel());
285 	bzero(STRUCT_BUF(args), SIZEOF_STRUCT(nfs_args, DATAMODEL_NATIVE));
286 	if (copyin(data, STRUCT_BUF(args), MIN(datalen,
287 		STRUCT_SIZE(args))))
288 		return (EFAULT);
289 
290 	nargs->wsize = STRUCT_FGET(args, wsize);
291 	nargs->rsize = STRUCT_FGET(args, rsize);
292 	nargs->timeo = STRUCT_FGET(args, timeo);
293 	nargs->retrans = STRUCT_FGET(args, retrans);
294 	nargs->acregmin = STRUCT_FGET(args, acregmin);
295 	nargs->acregmax = STRUCT_FGET(args, acregmax);
296 	nargs->acdirmin = STRUCT_FGET(args, acdirmin);
297 	nargs->acdirmax = STRUCT_FGET(args, acdirmax);
298 
299 	flags = STRUCT_FGET(args, flags);
300 	nargs->flags = flags;
301 
302 
303 	addr.buf = NULL;
304 	syncaddr.buf = NULL;
305 
306 	/*
307 	 * Allocate space for a knetconfig structure and
308 	 * its strings and copy in from user-land.
309 	 */
310 	knconf = kmem_zalloc(sizeof (*knconf), KM_SLEEP);
311 	STRUCT_INIT(knconf_tmp, get_udatamodel());
312 	if (copyin(STRUCT_FGETP(args, knconf), STRUCT_BUF(knconf_tmp),
313 		STRUCT_SIZE(knconf_tmp))) {
314 		kmem_free(knconf, sizeof (*knconf));
315 		return (EFAULT);
316 	}
317 
318 	knconf->knc_semantics = STRUCT_FGET(knconf_tmp, knc_semantics);
319 	knconf->knc_protofmly = STRUCT_FGETP(knconf_tmp, knc_protofmly);
320 	knconf->knc_proto = STRUCT_FGETP(knconf_tmp, knc_proto);
321 	if (get_udatamodel() != DATAMODEL_LP64) {
322 		knconf->knc_rdev = expldev(STRUCT_FGET(knconf_tmp, knc_rdev));
323 	} else {
324 		knconf->knc_rdev = STRUCT_FGET(knconf_tmp, knc_rdev);
325 	}
326 
327 	pf = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
328 	p = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
329 	error = copyinstr(knconf->knc_protofmly, pf, KNC_STRSIZE, NULL);
330 	if (error) {
331 		kmem_free(pf, KNC_STRSIZE);
332 		kmem_free(p, KNC_STRSIZE);
333 		kmem_free(knconf, sizeof (*knconf));
334 		return (error);
335 	}
336 
337 	error = copyinstr(knconf->knc_proto, p, KNC_STRSIZE, NULL);
338 	if (error) {
339 		kmem_free(pf, KNC_STRSIZE);
340 		kmem_free(p, KNC_STRSIZE);
341 		kmem_free(knconf, sizeof (*knconf));
342 		return (error);
343 	}
344 
345 
346 	knconf->knc_protofmly = pf;
347 	knconf->knc_proto = p;
348 
349 	nargs->knconf = knconf;
350 
351 	/* Copyin pathconf if there is one */
352 	if (STRUCT_FGETP(args, pathconf) != NULL) {
353 		pc = kmem_alloc(sizeof (*pc), KM_SLEEP);
354 		error = pathconf_copyin(STRUCT_BUF(args), pc);
355 		nargs->pathconf = pc;
356 		if (error)
357 			goto errout;
358 	}
359 
360 	/*
361 	 * Get server address
362 	 */
363 	STRUCT_INIT(addr_tmp, get_udatamodel());
364 	if (copyin(STRUCT_FGETP(args, addr), STRUCT_BUF(addr_tmp),
365 		STRUCT_SIZE(addr_tmp))) {
366 		error = EFAULT;
367 		goto errout;
368 	}
369 	nargs->addr = kmem_alloc(sizeof (struct netbuf), KM_SLEEP);
370 	userbufptr = STRUCT_FGETP(addr_tmp, buf);
371 	addr.len = STRUCT_FGET(addr_tmp, len);
372 	addr.buf = kmem_alloc(addr.len, KM_SLEEP);
373 	addr.maxlen = addr.len;
374 	if (copyin(userbufptr, addr.buf, addr.len)) {
375 		kmem_free(addr.buf, addr.len);
376 		error = EFAULT;
377 		goto errout;
378 	}
379 	bcopy(&addr, nargs->addr, sizeof (struct netbuf));
380 
381 	/*
382 	 * Get the root fhandle
383 	 */
384 
385 	if (copyin(STRUCT_FGETP(args, fh), &fh->fh_buf, NFS_FHSIZE)) {
386 		error = EFAULT;
387 		goto errout;
388 	}
389 	fh->fh_len = NFS_FHSIZE;
390 
391 	/*
392 	 * Get server's hostname
393 	 */
394 	if (flags & NFSMNT_HOSTNAME) {
395 		error = copyinstr(STRUCT_FGETP(args, hostname),
396 			netname, sizeof (netname), &hlen);
397 		if (error)
398 			goto errout;
399 		nargs->hostname = kmem_zalloc(hlen, KM_SLEEP);
400 		(void) strcpy(nargs->hostname, netname);
401 
402 	} else {
403 		nargs->hostname = NULL;
404 	}
405 
406 
407 	/*
408 	 * If there are syncaddr and netname data, load them in. This is
409 	 * to support data needed for NFSV4 when AUTH_DH is the negotiated
410 	 * flavor via SECINFO. (instead of using MOUNT protocol in V3).
411 	 */
412 	netname[0] = '\0';
413 	if (flags & NFSMNT_SECURE) {
414 		if (STRUCT_FGETP(args, syncaddr) == NULL) {
415 			error = EINVAL;
416 			goto errout;
417 		}
418 		/* get syncaddr */
419 		STRUCT_INIT(addr_tmp, get_udatamodel());
420 		if (copyin(STRUCT_FGETP(args, syncaddr), STRUCT_BUF(addr_tmp),
421 			STRUCT_SIZE(addr_tmp))) {
422 			error = EINVAL;
423 			goto errout;
424 		}
425 		userbufptr = STRUCT_FGETP(addr_tmp, buf);
426 		syncaddr.len = STRUCT_FGET(addr_tmp, len);
427 		syncaddr.buf = kmem_alloc(syncaddr.len, KM_SLEEP);
428 		syncaddr.maxlen = syncaddr.len;
429 		if (copyin(userbufptr, syncaddr.buf, syncaddr.len)) {
430 			kmem_free(syncaddr.buf, syncaddr.len);
431 			error = EFAULT;
432 			goto errout;
433 		}
434 
435 		nargs->syncaddr = kmem_alloc(sizeof (struct netbuf), KM_SLEEP);
436 		bcopy(&syncaddr, nargs->syncaddr, sizeof (struct netbuf));
437 
438 		ASSERT(STRUCT_FGETP(args, netname));
439 		if (copyinstr(STRUCT_FGETP(args, netname), netname,
440 			sizeof (netname), &nlen)) {
441 			error = EFAULT;
442 			goto errout;
443 		}
444 
445 		netname[nlen] = '\0';
446 		nargs->netname = kmem_zalloc(nlen, KM_SLEEP);
447 		(void) strcpy(nargs->netname, netname);
448 	}
449 
450 	/*
451 	 * Get the extention data which has the security data structure.
452 	 * This includes data for AUTH_SYS as well.
453 	 */
454 	if (flags & NFSMNT_NEWARGS) {
455 		nargs->nfs_args_ext = STRUCT_FGET(args, nfs_args_ext);
456 		if (nargs->nfs_args_ext == NFS_ARGS_EXTA ||
457 			nargs->nfs_args_ext == NFS_ARGS_EXTB) {
458 			/*
459 			 * Indicating the application is using the new
460 			 * sec_data structure to pass in the security
461 			 * data.
462 			 */
463 			if (STRUCT_FGETP(args,
464 				nfs_ext_u.nfs_extA.secdata) != NULL) {
465 				error = sec_clnt_loadinfo(
466 					(struct sec_data *)STRUCT_FGETP(args,
467 						nfs_ext_u.nfs_extA.secdata),
468 						&secdata, get_udatamodel());
469 			}
470 			nargs->nfs_ext_u.nfs_extA.secdata = secdata;
471 		}
472 	}
473 
474 	if (error)
475 		goto errout;
476 
477 	/*
478 	 * Failover support:
479 	 *
480 	 * We may have a linked list of nfs_args structures,
481 	 * which means the user is looking for failover.  If
482 	 * the mount is either not "read-only" or "soft",
483 	 * we want to bail out with EINVAL.
484 	 */
485 	if (nargs->nfs_args_ext == NFS_ARGS_EXTB)
486 		nargs->nfs_ext_u.nfs_extB.next =
487 			STRUCT_FGETP(args, nfs_ext_u.nfs_extB.next);
488 
489 errout:
490 	if (error)
491 		nfs_free_args(nargs, fh);
492 
493 	return (error);
494 }
495 
496 
497 /*
498  * nfs mount vfsop
499  * Set up mount info record and attach it to vfs struct.
500  */
501 static int
502 nfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
503 {
504 	char *data = uap->dataptr;
505 	int error;
506 	vnode_t *rtvp;			/* the server's root */
507 	mntinfo_t *mi;			/* mount info, pointed at by vfs */
508 	size_t nlen;			/* length of netname */
509 	struct knetconfig *knconf;	/* transport knetconfig structure */
510 	struct knetconfig *rdma_knconf;	/* rdma transport structure */
511 	rnode_t *rp;
512 	struct servinfo *svp;		/* nfs server info */
513 	struct servinfo *svp_tail = NULL; /* previous nfs server info */
514 	struct servinfo *svp_head;	/* first nfs server info */
515 	struct servinfo *svp_2ndlast;	/* 2nd last in the server info list */
516 	struct sec_data *secdata;	/* security data */
517 	struct nfs_args	*args = NULL;
518 	int flags, addr_type;
519 	zone_t *zone = nfs_zone();
520 	zone_t *mntzone = NULL;
521 	nfs_fhandle	*fhandle = NULL;
522 
523 	if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
524 		return (error);
525 
526 	if (mvp->v_type != VDIR)
527 		return (ENOTDIR);
528 
529 	/*
530 	 * get arguments
531 	 *
532 	 * nfs_args is now versioned and is extensible, so
533 	 * uap->datalen might be different from sizeof (args)
534 	 * in a compatible situation.
535 	 */
536 more:
537 
538 	if (!(uap->flags & MS_SYSSPACE)) {
539 		if (args == NULL)
540 			args = kmem_alloc(sizeof (struct nfs_args), KM_SLEEP);
541 		else {
542 			nfs_free_args(args, fhandle);
543 			fhandle = NULL;
544 		}
545 		if (fhandle == NULL)
546 			fhandle = kmem_zalloc(sizeof (nfs_fhandle), KM_SLEEP);
547 		error = nfs_copyin(data, uap->datalen, args, fhandle);
548 		if (error)  {
549 			if (args)
550 				kmem_free(args, sizeof (*args));
551 			return (error);
552 		}
553 	} else {
554 		args = (struct nfs_args *)data;
555 		fhandle = (nfs_fhandle *)args->fh;
556 	}
557 
558 
559 	flags = args->flags;
560 
561 	if (uap->flags & MS_REMOUNT) {
562 		size_t n;
563 		char name[FSTYPSZ];
564 
565 
566 		if (uap->flags & MS_SYSSPACE) {
567 			error = copystr(uap->fstype, name, FSTYPSZ, &n);
568 		} else {
569 			nfs_free_args(args, fhandle);
570 			kmem_free(args, sizeof (*args));
571 			error = copyinstr(uap->fstype, name, FSTYPSZ, &n);
572 		}
573 
574 		if (error) {
575 			if (error == ENAMETOOLONG)
576 				return (EINVAL);
577 			return (error);
578 		}
579 
580 
581 		/*
582 		 * This check is to ensure that the request is a
583 		 * genuine nfs remount request.
584 		 */
585 
586 		if (strncmp(name, "nfs", 3) != 0)
587 			return (EINVAL);
588 
589 		/*
590 		 * If the request changes the locking type, disallow the
591 		 * remount,
592 		 * because it's questionable whether we can transfer the
593 		 * locking state correctly.
594 		 *
595 		 * Remounts need to save the pathconf information.
596 		 * Part of the infamous static kludge.
597 		 */
598 
599 		if ((mi = VFTOMI(vfsp)) != NULL) {
600 			uint_t new_mi_llock;
601 			uint_t old_mi_llock;
602 
603 			new_mi_llock = (flags & NFSMNT_LLOCK) ? 1 : 0;
604 			old_mi_llock = (mi->mi_flags & MI_LLOCK) ? 1 : 0;
605 			if (old_mi_llock != new_mi_llock)
606 				return (EBUSY);
607 		}
608 		error = pathconf_get((struct mntinfo *)vfsp->vfs_data, args);
609 
610 		return (error);
611 	}
612 
613 	mutex_enter(&mvp->v_lock);
614 	if (!(uap->flags & MS_OVERLAY) &&
615 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
616 		mutex_exit(&mvp->v_lock);
617 		if (!(uap->flags) & MS_SYSSPACE) {
618 			nfs_free_args(args, fhandle);
619 			kmem_free(args, sizeof (*args));
620 		}
621 		return (EBUSY);
622 	}
623 	mutex_exit(&mvp->v_lock);
624 
625 	/* make sure things are zeroed for errout: */
626 	rtvp = NULL;
627 	mi = NULL;
628 	secdata = NULL;
629 
630 	/*
631 	 * A valid knetconfig structure is required.
632 	 */
633 	if (!(flags & NFSMNT_KNCONF)) {
634 		if (!(uap->flags & MS_SYSSPACE)) {
635 			nfs_free_args(args, fhandle);
636 			kmem_free(args, sizeof (*args));
637 		}
638 		return (EINVAL);
639 	}
640 
641 	if ((strlen(args->knconf->knc_protofmly) >= KNC_STRSIZE) ||
642 		(strlen(args->knconf->knc_proto) >= KNC_STRSIZE)) {
643 		if (!(uap->flags & MS_SYSSPACE)) {
644 			nfs_free_args(args, fhandle);
645 			kmem_free(args, sizeof (*args));
646 		}
647 		return (EINVAL);
648 	}
649 
650 
651 	/*
652 	 * Allocate a servinfo struct.
653 	 */
654 	svp = kmem_zalloc(sizeof (*svp), KM_SLEEP);
655 	mutex_init(&svp->sv_lock, NULL, MUTEX_DEFAULT, NULL);
656 	if (svp_tail) {
657 		svp_2ndlast = svp_tail;
658 		svp_tail->sv_next = svp;
659 	} else {
660 		svp_head = svp;
661 		svp_2ndlast = svp;
662 	}
663 
664 	svp_tail = svp;
665 
666 	/*
667 	 * Get knetconfig and server address
668 	 */
669 	svp->sv_knconf = args->knconf;
670 	args->knconf = NULL;
671 
672 	if (args->addr == NULL || args->addr->buf == NULL) {
673 		error = EINVAL;
674 		goto errout;
675 	}
676 
677 	svp->sv_addr.maxlen = args->addr->maxlen;
678 	svp->sv_addr.len = args->addr->len;
679 	svp->sv_addr.buf = args->addr->buf;
680 	args->addr->buf = NULL;
681 
682 	/*
683 	 * Get the root fhandle
684 	 */
685 	ASSERT(fhandle);
686 
687 	bcopy(&fhandle->fh_buf, &svp->sv_fhandle.fh_buf, fhandle->fh_len);
688 	svp->sv_fhandle.fh_len = fhandle->fh_len;
689 
690 	/*
691 	 * Get server's hostname
692 	 */
693 	if (flags & NFSMNT_HOSTNAME) {
694 		if (args->hostname == NULL) {
695 			error = EINVAL;
696 			goto errout;
697 		}
698 		svp->sv_hostnamelen = strlen(args->hostname) + 1;
699 		svp->sv_hostname = args->hostname;
700 		args->hostname = NULL;
701 	} else {
702 		char *p = "unknown-host";
703 		svp->sv_hostnamelen = strlen(p) + 1;
704 		svp->sv_hostname = kmem_zalloc(svp->sv_hostnamelen, KM_SLEEP);
705 		(void) strcpy(svp->sv_hostname, p);
706 	}
707 
708 
709 	/*
710 	 * RDMA MOUNT SUPPORT FOR NFS v2:
711 	 * Establish, is it possible to use RDMA, if so overload the
712 	 * knconf with rdma specific knconf and free the orignal.
713 	 */
714 	if ((flags & NFSMNT_TRYRDMA) || (flags & NFSMNT_DORDMA)) {
715 		/*
716 		 * Determine the addr type for RDMA, IPv4 or v6.
717 		 */
718 		if (strcmp(svp->sv_knconf->knc_protofmly, NC_INET) == 0)
719 			addr_type = AF_INET;
720 		else if (strcmp(svp->sv_knconf->knc_protofmly, NC_INET6) == 0)
721 			addr_type = AF_INET6;
722 
723 		if (rdma_reachable(addr_type, &svp->sv_addr,
724 			&rdma_knconf) == 0) {
725 			/*
726 			 * If successful, hijack, the orignal knconf and
727 			 * replace with a new one, depending on the flags.
728 			 */
729 			svp->sv_origknconf = svp->sv_knconf;
730 			svp->sv_knconf = rdma_knconf;
731 			knconf = rdma_knconf;
732 		} else {
733 			if (flags & NFSMNT_TRYRDMA) {
734 #ifdef	DEBUG
735 				if (rdma_debug)
736 					zcmn_err(getzoneid(), CE_WARN,
737 					    "no RDMA onboard, revert\n");
738 #endif
739 			}
740 
741 			if (flags & NFSMNT_DORDMA) {
742 				/*
743 				 * If proto=rdma is specified and no RDMA
744 				 * path to this server is avialable then
745 				 * ditch this server.
746 				 * This is not included in the mountable
747 				 * server list or the replica list.
748 				 * Check if more servers are specified;
749 				 * Failover case, otherwise bail out of mount.
750 				 */
751 				if (args->nfs_args_ext ==
752 				    NFS_ARGS_EXTB &&
753 					args->nfs_ext_u.nfs_extB.next
754 					!= NULL) {
755 					data = (char *)
756 						args->nfs_ext_u.nfs_extB.next;
757 					if (uap->flags & MS_RDONLY &&
758 					    !(flags & NFSMNT_SOFT)) {
759 						if (svp_head->sv_next == NULL) {
760 							svp_tail = NULL;
761 							svp_2ndlast = NULL;
762 							sv_free(svp_head);
763 							goto more;
764 						} else {
765 							svp_tail = svp_2ndlast;
766 							svp_2ndlast->sv_next =
767 							    NULL;
768 							sv_free(svp);
769 							goto more;
770 						}
771 					}
772 				} else {
773 					/*
774 					 * This is the last server specified
775 					 * in the nfs_args list passed down
776 					 * and its not rdma capable.
777 					 */
778 					if (svp_head->sv_next == NULL) {
779 						/*
780 						 * Is this the only one
781 						 */
782 						error = EINVAL;
783 #ifdef	DEBUG
784 						if (rdma_debug)
785 							zcmn_err(getzoneid(),
786 							    CE_WARN,
787 							    "No RDMA srv");
788 #endif
789 						goto errout;
790 					} else {
791 						/*
792 						 * There is list, since some
793 						 * servers specified before
794 						 * this passed all requirements
795 						 */
796 						svp_tail = svp_2ndlast;
797 						svp_2ndlast->sv_next = NULL;
798 						sv_free(svp);
799 						goto proceed;
800 					}
801 				}
802 			}
803 		}
804 	}
805 
806 	/*
807 	 * Get the extention data which has the new security data structure.
808 	 */
809 	if (flags & NFSMNT_NEWARGS) {
810 		switch (args->nfs_args_ext) {
811 		case NFS_ARGS_EXTA:
812 		case NFS_ARGS_EXTB:
813 			/*
814 			 * Indicating the application is using the new
815 			 * sec_data structure to pass in the security
816 			 * data.
817 			 */
818 			secdata = args->nfs_ext_u.nfs_extA.secdata;
819 			if (secdata == NULL) {
820 				error = EINVAL;
821 			} else {
822 				/*
823 				 * Need to validate the flavor here if
824 				 * sysspace, userspace was already
825 				 * validate from the nfs_copyin function.
826 				 */
827 				switch (secdata->rpcflavor) {
828 					case AUTH_NONE:
829 					case AUTH_UNIX:
830 					case AUTH_LOOPBACK:
831 					case AUTH_DES:
832 					case RPCSEC_GSS:
833 						break;
834 					default:
835 						error = EINVAL;
836 						goto errout;
837 				}
838 			}
839 			args->nfs_ext_u.nfs_extA.secdata = NULL;
840 			break;
841 
842 		default:
843 			error = EINVAL;
844 			break;
845 		}
846 	} else if (flags & NFSMNT_SECURE) {
847 		/*
848 		 * Keep this for backward compatibility to support
849 		 * NFSMNT_SECURE/NFSMNT_RPCTIMESYNC flags.
850 		 */
851 		if (args->syncaddr == NULL || args->syncaddr->buf == NULL) {
852 			error = EINVAL;
853 			goto errout;
854 		}
855 
856 		/*
857 		 * get time sync address.
858 		 */
859 		if (args->syncaddr == NULL) {
860 			error = EFAULT;
861 			goto errout;
862 		}
863 
864 		/*
865 		 * Move security related data to the sec_data structure.
866 		 */
867 		{
868 			dh_k4_clntdata_t *data;
869 			char *pf, *p;
870 
871 			secdata = kmem_alloc(sizeof (*secdata), KM_SLEEP);
872 			if (flags & NFSMNT_RPCTIMESYNC)
873 				secdata->flags |= AUTH_F_RPCTIMESYNC;
874 			data = kmem_alloc(sizeof (*data), KM_SLEEP);
875 			bcopy(args->syncaddr, &data->syncaddr,
876 				sizeof (*args->syncaddr));
877 
878 
879 			/*
880 			 * duplicate the knconf information for the
881 			 * new opaque data.
882 			 */
883 			data->knconf = kmem_alloc(sizeof (*knconf), KM_SLEEP);
884 			*data->knconf = *knconf;
885 			pf = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
886 			p = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
887 			bcopy(knconf->knc_protofmly, pf, KNC_STRSIZE);
888 			bcopy(knconf->knc_proto, pf, KNC_STRSIZE);
889 			data->knconf->knc_protofmly = pf;
890 			data->knconf->knc_proto = p;
891 
892 			/* move server netname to the sec_data structure */
893 			nlen = strlen(args->hostname) + 1;
894 			if (nlen != 0) {
895 				data->netname = kmem_alloc(nlen, KM_SLEEP);
896 				bcopy(args->hostname, data->netname, nlen);
897 				data->netnamelen = (int)nlen;
898 			}
899 			secdata->secmod = secdata->rpcflavor = AUTH_DES;
900 			secdata->data = (caddr_t)data;
901 		}
902 	} else {
903 		secdata = kmem_alloc(sizeof (*secdata), KM_SLEEP);
904 		secdata->secmod = secdata->rpcflavor = AUTH_UNIX;
905 		secdata->data = NULL;
906 	}
907 	svp->sv_secdata = secdata;
908 
909 	/*
910 	 * See bug 1180236.
911 	 * If mount secure failed, we will fall back to AUTH_NONE
912 	 * and try again.  nfs3rootvp() will turn this back off.
913 	 *
914 	 * The NFS Version 2 mount uses GETATTR and STATFS procedures.
915 	 * The server does not care if these procedures have the proper
916 	 * authentication flavor, so if mount retries using AUTH_NONE
917 	 * that does not require a credential setup for root then the
918 	 * automounter would work without requiring root to be
919 	 * keylogged into AUTH_DES.
920 	 */
921 	if (secdata->rpcflavor != AUTH_UNIX &&
922 	    secdata->rpcflavor != AUTH_LOOPBACK)
923 		secdata->flags |= AUTH_F_TRYNONE;
924 
925 	/*
926 	 * Failover support:
927 	 *
928 	 * We may have a linked list of nfs_args structures,
929 	 * which means the user is looking for failover.  If
930 	 * the mount is either not "read-only" or "soft",
931 	 * we want to bail out with EINVAL.
932 	 */
933 	if (args->nfs_args_ext == NFS_ARGS_EXTB &&
934 	    args->nfs_ext_u.nfs_extB.next != NULL) {
935 		if (uap->flags & MS_RDONLY && !(flags & NFSMNT_SOFT)) {
936 			data = (char *)args->nfs_ext_u.nfs_extB.next;
937 			goto more;
938 		}
939 		error = EINVAL;
940 		goto errout;
941 	}
942 
943 	/*
944 	 * Determine the zone we're being mounted into.
945 	 */
946 	zone_hold(mntzone = zone);		/* start with this assumption */
947 	if (getzoneid() == GLOBAL_ZONEID) {
948 		zone_rele(mntzone);
949 		mntzone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
950 		ASSERT(mntzone != NULL);
951 		if (mntzone != zone) {
952 			error = EBUSY;
953 			goto errout;
954 		}
955 	}
956 
957 	if (is_system_labeled()) {
958 		error = nfs_mount_label_policy(vfsp, &svp->sv_addr,
959 		    svp->sv_knconf, cr);
960 
961 		if (error > 0)
962 			goto errout;
963 
964 		if (error == -1) {
965 			/* change mount to read-only to prevent write-down */
966 			vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
967 		}
968 	}
969 
970 	/*
971 	 * Stop the mount from going any further if the zone is going away.
972 	 */
973 	if (zone_status_get(mntzone) >= ZONE_IS_SHUTTING_DOWN) {
974 		error = EBUSY;
975 		goto errout;
976 	}
977 
978 	/*
979 	 * Get root vnode.
980 	 */
981 proceed:
982 	error = nfsrootvp(&rtvp, vfsp, svp_head, flags, cr, mntzone);
983 
984 	if (error)
985 		goto errout;
986 
987 	/*
988 	 * Set option fields in the mount info record
989 	 */
990 	mi = VTOMI(rtvp);
991 
992 	if (svp_head->sv_next)
993 		mi->mi_flags |= MI_LLOCK;
994 
995 	error = nfs_setopts(rtvp, DATAMODEL_NATIVE, args);
996 	if (!error) {
997 		/* static pathconf kludge */
998 		error = pathconf_get(mi, args);
999 	}
1000 
1001 errout:
1002 	if (error) {
1003 		if (rtvp != NULL) {
1004 			rp = VTOR(rtvp);
1005 			if (rp->r_flags & RHASHED)
1006 				rp_rmhash(rp);
1007 		}
1008 		sv_free(svp_head);
1009 		if (mi != NULL) {
1010 			nfs_async_stop(vfsp);
1011 			nfs_async_manager_stop(vfsp);
1012 			if (mi->mi_io_kstats) {
1013 				kstat_delete(mi->mi_io_kstats);
1014 				mi->mi_io_kstats = NULL;
1015 			}
1016 			if (mi->mi_ro_kstats) {
1017 				kstat_delete(mi->mi_ro_kstats);
1018 				mi->mi_ro_kstats = NULL;
1019 			}
1020 			nfs_free_mi(mi);
1021 		}
1022 	}
1023 
1024 	if (!(uap->flags & MS_SYSSPACE) && args) {
1025 		nfs_free_args(args, fhandle);
1026 		kmem_free(args, sizeof (*args));
1027 	}
1028 
1029 	if (rtvp != NULL)
1030 		VN_RELE(rtvp);
1031 
1032 	if (mntzone != NULL)
1033 		zone_rele(mntzone);
1034 
1035 	return (error);
1036 }
1037 
1038 /*
1039  * The pathconf information is kept on a linked list of kmem_alloc'ed
1040  * structs. We search the list & add a new struct iff there is no other
1041  * struct with the same information.
1042  * See sys/pathconf.h for ``the rest of the story.''
1043  */
1044 static struct pathcnf *allpc = NULL;
1045 
1046 static int
1047 pathconf_copyin(struct nfs_args *args, struct pathcnf *pc)
1048 {
1049 	STRUCT_DECL(pathcnf, pc_tmp);
1050 	STRUCT_HANDLE(nfs_args, ap);
1051 	int i;
1052 	model_t	model;
1053 
1054 	model = get_udatamodel();
1055 	STRUCT_INIT(pc_tmp, model);
1056 	STRUCT_SET_HANDLE(ap, model, args);
1057 
1058 	if ((STRUCT_FGET(ap, flags) & NFSMNT_POSIX) &&
1059 	    STRUCT_FGETP(ap, pathconf) != NULL) {
1060 		if (copyin(STRUCT_FGETP(ap, pathconf), STRUCT_BUF(pc_tmp),
1061 		    STRUCT_SIZE(pc_tmp)))
1062 			return (EFAULT);
1063 		if (_PC_ISSET(_PC_ERROR, STRUCT_FGET(pc_tmp, pc_mask)))
1064 			return (EINVAL);
1065 
1066 		pc->pc_link_max = STRUCT_FGET(pc_tmp, pc_link_max);
1067 		pc->pc_max_canon = STRUCT_FGET(pc_tmp, pc_max_canon);
1068 		pc->pc_max_input = STRUCT_FGET(pc_tmp, pc_max_input);
1069 		pc->pc_name_max = STRUCT_FGET(pc_tmp, pc_name_max);
1070 		pc->pc_path_max = STRUCT_FGET(pc_tmp, pc_path_max);
1071 		pc->pc_pipe_buf = STRUCT_FGET(pc_tmp, pc_pipe_buf);
1072 		pc->pc_vdisable = STRUCT_FGET(pc_tmp, pc_vdisable);
1073 		pc->pc_xxx = STRUCT_FGET(pc_tmp, pc_xxx);
1074 		for (i = 0; i < _PC_N; i++)
1075 			pc->pc_mask[i] = STRUCT_FGET(pc_tmp, pc_mask[i]);
1076 	}
1077 	return (0);
1078 }
1079 
1080 static int
1081 pathconf_get(struct mntinfo *mi, struct nfs_args *args)
1082 {
1083 	struct pathcnf *p, *pc;
1084 
1085 	pc = args->pathconf;
1086 	if (mi->mi_pathconf != NULL) {
1087 		pathconf_rele(mi);
1088 		mi->mi_pathconf = NULL;
1089 	}
1090 	if (args->flags & NFSMNT_POSIX &&
1091 		args->pathconf != NULL) {
1092 
1093 		if (_PC_ISSET(_PC_ERROR, pc->pc_mask))
1094 			return (EINVAL);
1095 
1096 		for (p = allpc; p != NULL; p = p->pc_next) {
1097 			if (PCCMP(p, pc) == 0)
1098 				break;
1099 		}
1100 		if (p != NULL) {
1101 			mi->mi_pathconf = p;
1102 			p->pc_refcnt++;
1103 		} else {
1104 			p = kmem_alloc(sizeof (*p), KM_SLEEP);
1105 			bcopy(pc, p, sizeof (struct pathcnf));
1106 			p->pc_next = allpc;
1107 			p->pc_refcnt = 1;
1108 			allpc = mi->mi_pathconf = p;
1109 		}
1110 	}
1111 	return (0);
1112 }
1113 
1114 /*
1115  * release the static pathconf information
1116  */
1117 static void
1118 pathconf_rele(struct mntinfo *mi)
1119 {
1120 	if (mi->mi_pathconf != NULL) {
1121 		if (--mi->mi_pathconf->pc_refcnt == 0) {
1122 			struct pathcnf *p;
1123 			struct pathcnf *p2;
1124 
1125 			p2 = p = allpc;
1126 			while (p != NULL && p != mi->mi_pathconf) {
1127 				p2 = p;
1128 				p = p->pc_next;
1129 			}
1130 			if (p == NULL) {
1131 				panic("mi->pathconf");
1132 				/*NOTREACHED*/
1133 			}
1134 			if (p == allpc)
1135 				allpc = p->pc_next;
1136 			else
1137 				p2->pc_next = p->pc_next;
1138 			kmem_free(p, sizeof (*p));
1139 			mi->mi_pathconf = NULL;
1140 		}
1141 	}
1142 }
1143 
1144 static int nfs_dynamic = 1;	/* global variable to enable dynamic retrans. */
1145 static ushort_t nfs_max_threads = 8;	/* max number of active async threads */
1146 static uint_t nfs_async_clusters = 1;	/* # of reqs from each async queue */
1147 static uint_t nfs_cots_timeo = NFS_COTS_TIMEO;
1148 
1149 static int
1150 nfsrootvp(vnode_t **rtvpp, vfs_t *vfsp, struct servinfo *svp,
1151 	int flags, cred_t *cr, zone_t *zone)
1152 {
1153 	vnode_t *rtvp;
1154 	mntinfo_t *mi;
1155 	dev_t nfs_dev;
1156 	struct vattr va;
1157 	int error;
1158 	rnode_t *rp;
1159 	int i;
1160 	struct nfs_stats *nfsstatsp;
1161 	cred_t *lcr = NULL, *tcr = cr;
1162 
1163 	nfsstatsp = zone_getspecific(nfsstat_zone_key, nfs_zone());
1164 	ASSERT(nfsstatsp != NULL);
1165 
1166 	/*
1167 	 * Create a mount record and link it to the vfs struct.
1168 	 */
1169 	mi = kmem_zalloc(sizeof (*mi), KM_SLEEP);
1170 	mutex_init(&mi->mi_lock, NULL, MUTEX_DEFAULT, NULL);
1171 	mutex_init(&mi->mi_remap_lock, NULL, MUTEX_DEFAULT, NULL);
1172 	mi->mi_flags = MI_ACL | MI_EXTATTR;
1173 	if (!(flags & NFSMNT_SOFT))
1174 		mi->mi_flags |= MI_HARD;
1175 	if ((flags & NFSMNT_SEMISOFT))
1176 		mi->mi_flags |= MI_SEMISOFT;
1177 	if ((flags & NFSMNT_NOPRINT))
1178 		mi->mi_flags |= MI_NOPRINT;
1179 	if (flags & NFSMNT_INT)
1180 		mi->mi_flags |= MI_INT;
1181 	mi->mi_retrans = NFS_RETRIES;
1182 	if (svp->sv_knconf->knc_semantics == NC_TPI_COTS_ORD ||
1183 	    svp->sv_knconf->knc_semantics == NC_TPI_COTS)
1184 		mi->mi_timeo = nfs_cots_timeo;
1185 	else
1186 		mi->mi_timeo = NFS_TIMEO;
1187 	mi->mi_prog = NFS_PROGRAM;
1188 	mi->mi_vers = NFS_VERSION;
1189 	mi->mi_rfsnames = rfsnames_v2;
1190 	mi->mi_reqs = nfsstatsp->nfs_stats_v2.rfsreqcnt_ptr;
1191 	mi->mi_call_type = call_type_v2;
1192 	mi->mi_ss_call_type = ss_call_type_v2;
1193 	mi->mi_timer_type = timer_type_v2;
1194 	mi->mi_aclnames = aclnames_v2;
1195 	mi->mi_aclreqs = nfsstatsp->nfs_stats_v2.aclreqcnt_ptr;
1196 	mi->mi_acl_call_type = acl_call_type_v2;
1197 	mi->mi_acl_ss_call_type = acl_ss_call_type_v2;
1198 	mi->mi_acl_timer_type = acl_timer_type_v2;
1199 	cv_init(&mi->mi_failover_cv, NULL, CV_DEFAULT, NULL);
1200 	mi->mi_servers = svp;
1201 	mi->mi_curr_serv = svp;
1202 	mi->mi_acregmin = SEC2HR(ACREGMIN);
1203 	mi->mi_acregmax = SEC2HR(ACREGMAX);
1204 	mi->mi_acdirmin = SEC2HR(ACDIRMIN);
1205 	mi->mi_acdirmax = SEC2HR(ACDIRMAX);
1206 
1207 	if (nfs_dynamic)
1208 		mi->mi_flags |= MI_DYNAMIC;
1209 
1210 	if (flags & NFSMNT_DIRECTIO)
1211 		mi->mi_flags |= MI_DIRECTIO;
1212 
1213 	/*
1214 	 * Make a vfs struct for nfs.  We do this here instead of below
1215 	 * because rtvp needs a vfs before we can do a getattr on it.
1216 	 *
1217 	 * Assign a unique device id to the mount
1218 	 */
1219 	mutex_enter(&nfs_minor_lock);
1220 	do {
1221 		nfs_minor = (nfs_minor + 1) & MAXMIN32;
1222 		nfs_dev = makedevice(nfs_major, nfs_minor);
1223 	} while (vfs_devismounted(nfs_dev));
1224 	mutex_exit(&nfs_minor_lock);
1225 
1226 	vfsp->vfs_dev = nfs_dev;
1227 	vfs_make_fsid(&vfsp->vfs_fsid, nfs_dev, nfsfstyp);
1228 	vfsp->vfs_data = (caddr_t)mi;
1229 	vfsp->vfs_fstype = nfsfstyp;
1230 	vfsp->vfs_bsize = NFS_MAXDATA;
1231 
1232 	/*
1233 	 * Initialize fields used to support async putpage operations.
1234 	 */
1235 	for (i = 0; i < NFS_ASYNC_TYPES; i++)
1236 		mi->mi_async_clusters[i] = nfs_async_clusters;
1237 	mi->mi_async_init_clusters = nfs_async_clusters;
1238 	mi->mi_async_curr = &mi->mi_async_reqs[0];
1239 	mi->mi_max_threads = nfs_max_threads;
1240 	mutex_init(&mi->mi_async_lock, NULL, MUTEX_DEFAULT, NULL);
1241 	cv_init(&mi->mi_async_reqs_cv, NULL, CV_DEFAULT, NULL);
1242 	cv_init(&mi->mi_async_work_cv, NULL, CV_DEFAULT, NULL);
1243 	cv_init(&mi->mi_async_cv, NULL, CV_DEFAULT, NULL);
1244 
1245 	mi->mi_vfsp = vfsp;
1246 	zone_hold(mi->mi_zone = zone);
1247 	nfs_mi_zonelist_add(mi);
1248 
1249 	/*
1250 	 * Make the root vnode, use it to get attributes,
1251 	 * then remake it with the attributes.
1252 	 */
1253 	rtvp = makenfsnode((fhandle_t *)svp->sv_fhandle.fh_buf,
1254 	    NULL, vfsp, gethrtime(), cr, NULL, NULL);
1255 
1256 	va.va_mask = AT_ALL;
1257 
1258 	/*
1259 	 * If the uid is set then set the creds for secure mounts
1260 	 * by proxy processes such as automountd.
1261 	 */
1262 	if (svp->sv_secdata->uid != 0 &&
1263 	    svp->sv_secdata->rpcflavor == RPCSEC_GSS) {
1264 		lcr = crdup(cr);
1265 		(void) crsetugid(lcr, svp->sv_secdata->uid, crgetgid(cr));
1266 		tcr = lcr;
1267 	}
1268 
1269 	error = nfsgetattr(rtvp, &va, tcr);
1270 	if (error)
1271 		goto bad;
1272 	rtvp->v_type = va.va_type;
1273 
1274 	/*
1275 	 * Poll every server to get the filesystem stats; we're
1276 	 * only interested in the server's transfer size, and we
1277 	 * want the minimum.
1278 	 *
1279 	 * While we're looping, we'll turn off AUTH_F_TRYNONE,
1280 	 * which is only for the mount operation.
1281 	 */
1282 
1283 	mi->mi_tsize = MIN(NFS_MAXDATA, nfstsize());
1284 	mi->mi_stsize = MIN(NFS_MAXDATA, nfstsize());
1285 
1286 	for (svp = mi->mi_servers; svp != NULL; svp = svp->sv_next) {
1287 		struct nfsstatfs fs;
1288 		int douprintf;
1289 
1290 		douprintf = 1;
1291 		mi->mi_curr_serv = svp;
1292 
1293 		error = rfs2call(mi, RFS_STATFS,
1294 			xdr_fhandle, (caddr_t)svp->sv_fhandle.fh_buf,
1295 			xdr_statfs, (caddr_t)&fs, tcr, &douprintf,
1296 			&fs.fs_status, 0, NULL);
1297 		if (error)
1298 			goto bad;
1299 		mi->mi_stsize = MIN(mi->mi_stsize, fs.fs_tsize);
1300 		svp->sv_secdata->flags &= ~AUTH_F_TRYNONE;
1301 	}
1302 	mi->mi_curr_serv = mi->mi_servers;
1303 	mi->mi_curread = mi->mi_tsize;
1304 	mi->mi_curwrite = mi->mi_stsize;
1305 
1306 	/*
1307 	 * Start the manager thread responsible for handling async worker
1308 	 * threads.
1309 	 */
1310 	VFS_HOLD(vfsp);	/* add reference for thread */
1311 	mi->mi_manager_thread = zthread_create(NULL, 0, nfs_async_manager,
1312 					vfsp, 0, minclsyspri);
1313 	ASSERT(mi->mi_manager_thread != NULL);
1314 
1315 	/*
1316 	 * Initialize kstats
1317 	 */
1318 	nfs_mnt_kstat_init(vfsp);
1319 
1320 	mi->mi_type = rtvp->v_type;
1321 
1322 	*rtvpp = rtvp;
1323 	if (lcr != NULL)
1324 		crfree(lcr);
1325 
1326 	return (0);
1327 bad:
1328 	/*
1329 	 * An error occurred somewhere, need to clean up...
1330 	 * We need to release our reference to the root vnode and
1331 	 * destroy the mntinfo struct that we just created.
1332 	 */
1333 	if (lcr != NULL)
1334 		crfree(lcr);
1335 	rp = VTOR(rtvp);
1336 	if (rp->r_flags & RHASHED)
1337 		rp_rmhash(rp);
1338 	VN_RELE(rtvp);
1339 	nfs_async_stop(vfsp);
1340 	nfs_async_manager_stop(vfsp);
1341 	if (mi->mi_io_kstats) {
1342 		kstat_delete(mi->mi_io_kstats);
1343 		mi->mi_io_kstats = NULL;
1344 	}
1345 	if (mi->mi_ro_kstats) {
1346 		kstat_delete(mi->mi_ro_kstats);
1347 		mi->mi_ro_kstats = NULL;
1348 	}
1349 	nfs_free_mi(mi);
1350 	*rtvpp = NULL;
1351 	return (error);
1352 }
1353 
1354 /*
1355  * vfs operations
1356  */
1357 static int
1358 nfs_unmount(vfs_t *vfsp, int flag, cred_t *cr)
1359 {
1360 	mntinfo_t *mi;
1361 	ushort_t omax;
1362 
1363 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
1364 		return (EPERM);
1365 
1366 	mi = VFTOMI(vfsp);
1367 	if (flag & MS_FORCE) {
1368 		vfsp->vfs_flag |= VFS_UNMOUNTED;
1369 		/*
1370 		 * We need to stop the manager thread explicitly; the worker
1371 		 * threads can time out and exit on their own.
1372 		 */
1373 		nfs_async_manager_stop(vfsp);
1374 		destroy_rtable(vfsp, cr);
1375 		if (mi->mi_io_kstats) {
1376 			kstat_delete(mi->mi_io_kstats);
1377 			mi->mi_io_kstats = NULL;
1378 		}
1379 		if (mi->mi_ro_kstats) {
1380 			kstat_delete(mi->mi_ro_kstats);
1381 			mi->mi_ro_kstats = NULL;
1382 		}
1383 		return (0);
1384 	}
1385 	/*
1386 	 * Wait until all asynchronous putpage operations on
1387 	 * this file system are complete before flushing rnodes
1388 	 * from the cache.
1389 	 */
1390 	omax = mi->mi_max_threads;
1391 	if (nfs_async_stop_sig(vfsp)) {
1392 		return (EINTR);
1393 	}
1394 	rflush(vfsp, cr);
1395 	/*
1396 	 * If there are any active vnodes on this file system,
1397 	 * then the file system is busy and can't be umounted.
1398 	 */
1399 	if (check_rtable(vfsp)) {
1400 		mutex_enter(&mi->mi_async_lock);
1401 		mi->mi_max_threads = omax;
1402 		mutex_exit(&mi->mi_async_lock);
1403 		return (EBUSY);
1404 	}
1405 	/*
1406 	 * The unmount can't fail from now on; stop the manager thread.
1407 	 */
1408 	nfs_async_manager_stop(vfsp);
1409 	/*
1410 	 * Destroy all rnodes belonging to this file system from the
1411 	 * rnode hash queues and purge any resources allocated to
1412 	 * them.
1413 	 */
1414 	destroy_rtable(vfsp, cr);
1415 	if (mi->mi_io_kstats) {
1416 		kstat_delete(mi->mi_io_kstats);
1417 		mi->mi_io_kstats = NULL;
1418 	}
1419 	if (mi->mi_ro_kstats) {
1420 		kstat_delete(mi->mi_ro_kstats);
1421 		mi->mi_ro_kstats = NULL;
1422 	}
1423 	return (0);
1424 }
1425 
1426 /*
1427  * find root of nfs
1428  */
1429 static int
1430 nfs_root(vfs_t *vfsp, vnode_t **vpp)
1431 {
1432 	mntinfo_t *mi;
1433 	vnode_t *vp;
1434 	servinfo_t *svp;
1435 
1436 	mi = VFTOMI(vfsp);
1437 
1438 	if (nfs_zone() != mi->mi_zone)
1439 		return (EPERM);
1440 
1441 	svp = mi->mi_curr_serv;
1442 	if (svp && (svp->sv_flags & SV_ROOT_STALE)) {
1443 		mutex_enter(&svp->sv_lock);
1444 		svp->sv_flags &= ~SV_ROOT_STALE;
1445 		mutex_exit(&svp->sv_lock);
1446 		return (ENOENT);
1447 	}
1448 
1449 	vp = makenfsnode((fhandle_t *)mi->mi_curr_serv->sv_fhandle.fh_buf,
1450 	    NULL, vfsp, gethrtime(), CRED(), NULL, NULL);
1451 
1452 	if (VTOR(vp)->r_flags & RSTALE) {
1453 		VN_RELE(vp);
1454 		return (ENOENT);
1455 	}
1456 
1457 	ASSERT(vp->v_type == VNON || vp->v_type == mi->mi_type);
1458 
1459 	vp->v_type = mi->mi_type;
1460 
1461 	*vpp = vp;
1462 
1463 	return (0);
1464 }
1465 
1466 /*
1467  * Get file system statistics.
1468  */
1469 static int
1470 nfs_statvfs(vfs_t *vfsp, struct statvfs64 *sbp)
1471 {
1472 	int error;
1473 	mntinfo_t *mi;
1474 	struct nfsstatfs fs;
1475 	int douprintf;
1476 	failinfo_t fi;
1477 	vnode_t *vp;
1478 
1479 	error = nfs_root(vfsp, &vp);
1480 	if (error)
1481 		return (error);
1482 
1483 	mi = VFTOMI(vfsp);
1484 	douprintf = 1;
1485 	fi.vp = vp;
1486 	fi.fhp = NULL;		/* no need to update, filehandle not copied */
1487 	fi.copyproc = nfscopyfh;
1488 	fi.lookupproc = nfslookup;
1489 	fi.xattrdirproc = acl_getxattrdir2;
1490 
1491 	error = rfs2call(mi, RFS_STATFS,
1492 			xdr_fhandle, (caddr_t)VTOFH(vp),
1493 			xdr_statfs, (caddr_t)&fs, CRED(), &douprintf,
1494 			&fs.fs_status, 0, &fi);
1495 
1496 	if (!error) {
1497 		error = geterrno(fs.fs_status);
1498 		if (!error) {
1499 			mutex_enter(&mi->mi_lock);
1500 			if (mi->mi_stsize) {
1501 				mi->mi_stsize = MIN(mi->mi_stsize, fs.fs_tsize);
1502 			} else {
1503 				mi->mi_stsize = fs.fs_tsize;
1504 				mi->mi_curwrite = mi->mi_stsize;
1505 			}
1506 			mutex_exit(&mi->mi_lock);
1507 			sbp->f_bsize = fs.fs_bsize;
1508 			sbp->f_frsize = fs.fs_bsize;
1509 			sbp->f_blocks = (fsblkcnt64_t)fs.fs_blocks;
1510 			sbp->f_bfree = (fsblkcnt64_t)fs.fs_bfree;
1511 			/*
1512 			 * Some servers may return negative available
1513 			 * block counts.  They may do this because they
1514 			 * calculate the number of available blocks by
1515 			 * subtracting the number of used blocks from
1516 			 * the total number of blocks modified by the
1517 			 * minimum free value.  For example, if the
1518 			 * minumum free percentage is 10 and the file
1519 			 * system is greater than 90 percent full, then
1520 			 * 90 percent of the total blocks minus the
1521 			 * actual number of used blocks may be a
1522 			 * negative number.
1523 			 *
1524 			 * In this case, we need to sign extend the
1525 			 * negative number through the assignment from
1526 			 * the 32 bit bavail count to the 64 bit bavail
1527 			 * count.
1528 			 *
1529 			 * We need to be able to discern between there
1530 			 * just being a lot of available blocks on the
1531 			 * file system and the case described above.
1532 			 * We are making the assumption that it does
1533 			 * not make sense to have more available blocks
1534 			 * than there are free blocks.  So, if there
1535 			 * are, then we treat the number as if it were
1536 			 * a negative number and arrange to have it
1537 			 * sign extended when it is converted from 32
1538 			 * bits to 64 bits.
1539 			 */
1540 			if (fs.fs_bavail <= fs.fs_bfree)
1541 				sbp->f_bavail = (fsblkcnt64_t)fs.fs_bavail;
1542 			else {
1543 				sbp->f_bavail =
1544 					(fsblkcnt64_t)((long)fs.fs_bavail);
1545 			}
1546 			sbp->f_files = (fsfilcnt64_t)-1;
1547 			sbp->f_ffree = (fsfilcnt64_t)-1;
1548 			sbp->f_favail = (fsfilcnt64_t)-1;
1549 			sbp->f_fsid = (unsigned long)vfsp->vfs_fsid.val[0];
1550 			(void) strncpy(sbp->f_basetype,
1551 				vfssw[vfsp->vfs_fstype].vsw_name, FSTYPSZ);
1552 			sbp->f_flag = vf_to_stf(vfsp->vfs_flag);
1553 			sbp->f_namemax = (uint32_t)-1;
1554 		} else {
1555 			PURGE_STALE_FH(error, vp, CRED());
1556 		}
1557 	}
1558 
1559 	VN_RELE(vp);
1560 
1561 	return (error);
1562 }
1563 
1564 static kmutex_t nfs_syncbusy;
1565 
1566 /*
1567  * Flush dirty nfs files for file system vfsp.
1568  * If vfsp == NULL, all nfs files are flushed.
1569  */
1570 /* ARGSUSED */
1571 static int
1572 nfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
1573 {
1574 	/*
1575 	 * Cross-zone calls are OK here, since this translates to a
1576 	 * VOP_PUTPAGE(B_ASYNC), which gets picked up by the right zone.
1577 	 */
1578 	if (!(flag & SYNC_ATTR) && mutex_tryenter(&nfs_syncbusy) != 0) {
1579 		rflush(vfsp, cr);
1580 		mutex_exit(&nfs_syncbusy);
1581 	}
1582 	return (0);
1583 }
1584 
1585 /* ARGSUSED */
1586 static int
1587 nfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1588 {
1589 	int error;
1590 	vnode_t *vp;
1591 	struct vattr va;
1592 	struct nfs_fid *nfsfidp = (struct nfs_fid *)fidp;
1593 	zoneid_t zoneid = VFTOMI(vfsp)->mi_zone->zone_id;
1594 
1595 	if (nfs_zone() != VFTOMI(vfsp)->mi_zone)
1596 		return (EPERM);
1597 	if (fidp->fid_len != (sizeof (*nfsfidp) - sizeof (short))) {
1598 #ifdef DEBUG
1599 		zcmn_err(zoneid, CE_WARN,
1600 		    "nfs_vget: bad fid len, %d/%d", fidp->fid_len,
1601 		    (int)(sizeof (*nfsfidp) - sizeof (short)));
1602 #endif
1603 		*vpp = NULL;
1604 		return (ESTALE);
1605 	}
1606 
1607 	vp = makenfsnode((fhandle_t *)(nfsfidp->nf_data), NULL, vfsp,
1608 	    gethrtime(), CRED(), NULL, NULL);
1609 
1610 	if (VTOR(vp)->r_flags & RSTALE) {
1611 		VN_RELE(vp);
1612 		*vpp = NULL;
1613 		return (ENOENT);
1614 	}
1615 
1616 	if (vp->v_type == VNON) {
1617 		va.va_mask = AT_ALL;
1618 		error = nfsgetattr(vp, &va, CRED());
1619 		if (error) {
1620 			VN_RELE(vp);
1621 			*vpp = NULL;
1622 			return (error);
1623 		}
1624 		vp->v_type = va.va_type;
1625 	}
1626 
1627 	*vpp = vp;
1628 
1629 	return (0);
1630 }
1631 
1632 /* ARGSUSED */
1633 static int
1634 nfs_mountroot(vfs_t *vfsp, whymountroot_t why)
1635 {
1636 	vnode_t *rtvp;
1637 	char root_hostname[SYS_NMLN+1];
1638 	struct servinfo *svp;
1639 	int error;
1640 	int vfsflags;
1641 	size_t size;
1642 	char *root_path;
1643 	struct pathname pn;
1644 	char *name;
1645 	cred_t *cr;
1646 	struct nfs_args args;		/* nfs mount arguments */
1647 	static char token[10];
1648 
1649 	bzero(&args, sizeof (args));
1650 
1651 	/* do this BEFORE getfile which causes xid stamps to be initialized */
1652 	clkset(-1L);		/* hack for now - until we get time svc? */
1653 
1654 	if (why == ROOT_REMOUNT) {
1655 		/*
1656 		 * Shouldn't happen.
1657 		 */
1658 		panic("nfs_mountroot: why == ROOT_REMOUNT");
1659 	}
1660 
1661 	if (why == ROOT_UNMOUNT) {
1662 		/*
1663 		 * Nothing to do for NFS.
1664 		 */
1665 		return (0);
1666 	}
1667 
1668 	/*
1669 	 * why == ROOT_INIT
1670 	 */
1671 
1672 	name = token;
1673 	*name = 0;
1674 	getfsname("root", name, sizeof (token));
1675 
1676 	pn_alloc(&pn);
1677 	root_path = pn.pn_path;
1678 
1679 	svp = kmem_zalloc(sizeof (*svp), KM_SLEEP);
1680 	svp->sv_knconf = kmem_zalloc(sizeof (*svp->sv_knconf), KM_SLEEP);
1681 	svp->sv_knconf->knc_protofmly = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
1682 	svp->sv_knconf->knc_proto = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
1683 
1684 	/*
1685 	 * Get server address
1686 	 * Get the root fhandle
1687 	 * Get server's transport
1688 	 * Get server's hostname
1689 	 * Get options
1690 	 */
1691 	args.addr = &svp->sv_addr;
1692 	args.fh = (char *)&svp->sv_fhandle.fh_buf;
1693 	args.knconf = svp->sv_knconf;
1694 	args.hostname = root_hostname;
1695 	vfsflags = 0;
1696 	if (error = mount_root(*name ? name : "root", root_path, NFS_VERSION,
1697 	    &args, &vfsflags)) {
1698 		nfs_cmn_err(error, CE_WARN,
1699 		    "nfs_mountroot: mount_root failed: %m");
1700 		sv_free(svp);
1701 		pn_free(&pn);
1702 		return (error);
1703 	}
1704 	svp->sv_fhandle.fh_len = NFS_FHSIZE;
1705 	svp->sv_hostnamelen = (int)(strlen(root_hostname) + 1);
1706 	svp->sv_hostname = kmem_alloc(svp->sv_hostnamelen, KM_SLEEP);
1707 	(void) strcpy(svp->sv_hostname, root_hostname);
1708 
1709 	/*
1710 	 * Force root partition to always be mounted with AUTH_UNIX for now
1711 	 */
1712 	svp->sv_secdata = kmem_alloc(sizeof (*svp->sv_secdata), KM_SLEEP);
1713 	svp->sv_secdata->secmod = AUTH_UNIX;
1714 	svp->sv_secdata->rpcflavor = AUTH_UNIX;
1715 	svp->sv_secdata->data = NULL;
1716 
1717 	cr = crgetcred();
1718 	rtvp = NULL;
1719 
1720 	error = nfsrootvp(&rtvp, vfsp, svp, args.flags, cr, global_zone);
1721 
1722 	crfree(cr);
1723 
1724 	if (error) {
1725 		pn_free(&pn);
1726 		goto errout;
1727 	}
1728 
1729 	error = nfs_setopts(rtvp, DATAMODEL_NATIVE, &args);
1730 	if (error) {
1731 		nfs_cmn_err(error, CE_WARN,
1732 		    "nfs_mountroot: invalid root mount options");
1733 		pn_free(&pn);
1734 		goto errout;
1735 	}
1736 
1737 	(void) vfs_lock_wait(vfsp);
1738 	vfs_add(NULL, vfsp, vfsflags);
1739 	vfs_unlock(vfsp);
1740 
1741 	size = strlen(svp->sv_hostname);
1742 	(void) strcpy(rootfs.bo_name, svp->sv_hostname);
1743 	rootfs.bo_name[size] = ':';
1744 	(void) strcpy(&rootfs.bo_name[size + 1], root_path);
1745 
1746 	pn_free(&pn);
1747 
1748 errout:
1749 	if (error) {
1750 		sv_free(svp);
1751 		nfs_async_stop(vfsp);
1752 		nfs_async_manager_stop(vfsp);
1753 	}
1754 
1755 	if (rtvp != NULL)
1756 		VN_RELE(rtvp);
1757 
1758 	return (error);
1759 }
1760 
1761 /*
1762  * Initialization routine for VFS routines.  Should only be called once
1763  */
1764 int
1765 nfs_vfsinit(void)
1766 {
1767 	mutex_init(&nfs_syncbusy, NULL, MUTEX_DEFAULT, NULL);
1768 	return (0);
1769 }
1770 
1771 void
1772 nfs_vfsfini(void)
1773 {
1774 	mutex_destroy(&nfs_syncbusy);
1775 }
1776 
1777 void
1778 nfs_freevfs(vfs_t *vfsp)
1779 {
1780 	mntinfo_t *mi;
1781 	servinfo_t *svp;
1782 
1783 	/* free up the resources */
1784 	mi = VFTOMI(vfsp);
1785 	pathconf_rele(mi);
1786 	svp = mi->mi_servers;
1787 	mi->mi_servers = mi->mi_curr_serv = NULL;
1788 	sv_free(svp);
1789 
1790 	/*
1791 	 * By this time we should have already deleted the
1792 	 * mi kstats in the unmount code. If they are still around
1793 	 * somethings wrong
1794 	 */
1795 	ASSERT(mi->mi_io_kstats == NULL);
1796 	nfs_free_mi(mi);
1797 }
1798