summaryrefslogtreecommitdiff
path: root/usr/src/stand/lib/fs/nfs/mount.c
blob: 2c394b98abf18168b567c6416f19b0aaa63420b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1988 AT&T */
/*	All Rights Reserved */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <rpc/types.h>
#include <rpc/auth.h>
#include <sys/t_lock.h>
#include <netdb.h>
#include "clnt.h"
#include <rpc/xdr.h>
#include <rpc/rpc_msg.h>
#include <rpc/rpc.h>
#include "brpc.h"
#include "auth_inet.h"
#include "pmap.h"
#include <rpcsvc/nfs_prot.h>
#include <rpcsvc/nfs4_prot.h>
#include "nfs_inet.h"
#include <rpcsvc/bootparam.h>
#include <dhcp_impl.h>
#include <rpcsvc/mount.h>
#include <sys/promif.h>
#include <sys/salib.h>
#include "socket_inet.h"
#include "ipv4.h"
#include "mac.h"
#include <sys/bootdebug.h>
#include <errno.h>
#include "dhcpv4.h"
#include <sys/mntent.h>

/* ARP timeout in milliseconds for BOOTP/RARP */
#define	ARP_INETBOOT_TIMEOUT 1000

struct nfs_file		roothandle;			/* root file handle */
static char		root_hostname[SYS_NMLN];	/* server hostname */
static char		my_hostname[MAXHOSTNAMELEN];
static char		root_pathbuf[NFS_MAXPATHLEN];	/* the root's path */
static char		root_boot_file[NFS_MAXPATHLEN];	/* optional boot file */
static struct sockaddr_in root_to;			/* server sock ip */
							/* in network order */
CLIENT			*root_CLIENT = NULL;		/* CLIENT handle */
int dontroute = FALSE;	/* In case rarp/bootparams was selected */
char			rootopts[MAX_PATH_LEN];
static gid_t		fake_gids = 1;	/* fake gids list for auth_unix */

extern void set_default_filename(char *);	/* boot.c */

/*
 * xdr routines used by mount.
 */

bool_t
xdr_fhstatus(XDR *xdrs, struct fhstatus *fhsp)
{
	if (!xdr_int(xdrs, (int *)&fhsp->fhs_status))
		return (FALSE);
	if (fhsp->fhs_status == 0) {
		return (xdr_fhandle(xdrs, fhsp->fhstatus_u.fhs_fhandle));
	}
	return (TRUE);
}

bool_t
xdr_fhandle(XDR *xdrs, fhandle fhp)
{
	return (xdr_opaque(xdrs, (char *)fhp, NFS_FHSIZE));
}

bool_t
xdr_path(XDR *xdrs, char **pathp)
{
	return (xdr_string(xdrs, pathp, MNTPATHLEN));
}

bool_t
xdr_fhandle3(XDR *xdrs, fhandle3 *objp)
{
	return (xdr_bytes(xdrs, (char **)&objp->fhandle3_val,
				(uint_t *)&objp->fhandle3_len, FHSIZE3));
}

bool_t
xdr_mountstat3(XDR *xdrs, mountstat3 *objp)
{
	return (xdr_enum(xdrs, (enum_t *)objp));
}

bool_t
xdr_mountres3_ok(XDR *xdrs, mountres3_ok *objp)
{
	if (!xdr_fhandle3(xdrs, &objp->fhandle))
		return (FALSE);
	return (xdr_array(xdrs, (char **)&objp->auth_flavors.auth_flavors_val,
			(uint_t *)&objp->auth_flavors.auth_flavors_len, ~0,
			sizeof (int), (xdrproc_t)xdr_int));
}

bool_t
xdr_mountres3(XDR *xdrs, mountres3 *objp)
{
	if (!xdr_mountstat3(xdrs, &objp->fhs_status))
		return (FALSE);
	if (objp->fhs_status == MNT_OK)
		return (xdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo));
	return (TRUE);
}

static int
nfsmountroot(char *path, struct nfs_file *filep)
{
	int		rexmit;
	int		resp_wait;
	enum clnt_stat	status;
	struct fhstatus	root_tmp;			/* to pass to rpc/xdr */

	/*
	 * Wait up to 16 secs for first response, retransmitting expon.
	 */
	rexmit = 0;	/* default retransmission interval */
	resp_wait = 16;

	do {
		status = brpc_call((rpcprog_t)MOUNTPROG, (rpcvers_t)MOUNTVERS,
		    (rpcproc_t)MOUNTPROC_MNT, xdr_path, (caddr_t)&path,
		    xdr_fhstatus, (caddr_t)&(root_tmp), rexmit, resp_wait,
		    &root_to, NULL, AUTH_UNIX);
		if (status == RPC_TIMEDOUT) {
			dprintf("boot: %s:%s mount server not responding.\n",
			    root_hostname, path);
		}
		rexmit = resp_wait;
		resp_wait = 0;	/* use default wait time. */
	} while (status == RPC_TIMEDOUT);

	if ((status != RPC_SUCCESS) || (root_tmp.fhs_status != 0)) {
		nfs_error(root_tmp.fhs_status);
		root_to.sin_port = 0;
		return (-1);
	}

	/*
	 * Since the mount succeeded, we'll mark the filep's
	 * status as NFS_OK, and its type as NFDIR. If these
	 * points aren't the case, then we wouldn't be here.
	 */
	bcopy(&root_tmp.fhstatus_u.fhs_fhandle, &filep->fh.fh2, FHSIZE);
	filep->ftype.type2 = NFDIR;
	filep->version = NFS_VERSION;
	nfs_readsize = nfs_readsize <  NFS_MAXDATA ? nfs_readsize : NFS_MAXDATA;
	/*
	 * Set a reasonable lower limit on readsize
	 */
	nfs_readsize = (nfs_readsize != 0 && nfs_readsize < 512) ?
							512 : nfs_readsize;
	return (0);
}

int
setup_root_vars(void)
{
	size_t		buflen;
	uint16_t	readsize;

	/*
	 * Root server name. Required.
	 */
	buflen = sizeof (root_hostname);
	if (dhcp_getinfo(DSYM_VENDOR, VS_NFSMNT_ROOTSRVR_NAME, 0,
	    root_hostname, &buflen)) {
		root_hostname[buflen] = '\0';
	} else {
		dprintf("BOUND: Missing Root Server Name Option\n");
		errno = EINVAL;
		return (-1);
	}

	/*
	 * Root server IP. Required.
	 */
	buflen = sizeof (root_to.sin_addr);
	if (!dhcp_getinfo(DSYM_VENDOR, VS_NFSMNT_ROOTSRVR_IP, 0,
	    &root_to.sin_addr, &buflen)) {
		dprintf("BOUND: Missing Root Server IP Option\n");
		errno = EINVAL;
		return (-1);
	}

	/*
	 * Root path Required.
	 */
	buflen = sizeof (root_pathbuf);
	if (dhcp_getinfo(DSYM_VENDOR, VS_NFSMNT_ROOTPATH, 0,
	    root_pathbuf, &buflen)) {
		root_pathbuf[buflen] = '\0';
	} else {
		dprintf("BOUND: Missing Root Path Option\n");
		errno = EINVAL;
		return (-1);
	}

	/*
	 * Optional Bootfile path.
	 */
	buflen = sizeof (root_boot_file);
	if (dhcp_getinfo(DSYM_VENDOR, VS_NFSMNT_BOOTFILE, 0,
		    root_boot_file, &buflen)) {
		root_boot_file[buflen] = '\0';
		dprintf("BOUND: Optional Boot File is: %s\n", root_boot_file);
	}

	/* if we got a boot file name, use it as the default */
	if (root_boot_file[0] != '\0')
		set_default_filename(root_boot_file);

	/*
	 * Set the NFS read size. The mount code will adjust it to
	 * the maximum size.
	 */
	buflen = sizeof (readsize);
	if (dhcp_getinfo(DSYM_VENDOR, VS_BOOT_NFS_READSIZE, 0,
	    &readsize, &buflen)) {
		nfs_readsize = ntohs(readsize);
		if (boothowto & RB_VERBOSE) {
			printf("Boot NFS read size: %d\n", nfs_readsize);
		}
	}

	/*
	 * Optional rootopts.
	 */
	buflen = sizeof (rootopts);
	if (dhcp_getinfo(DSYM_VENDOR, VS_NFSMNT_ROOTOPTS, 0,
	    rootopts, &buflen)) {
		rootopts[buflen] = '\0';
		dprintf("BOUND: Optional Rootopts is: %s\n", rootopts);
	}

	return (0);
}

static void
mnt3_error(enum mountstat3 status)
{
	if (!(boothowto & RB_DEBUG))
		return;

	switch (status) {
	case MNT_OK:
		printf("Mount: No error.\n");
		break;
	case MNT3ERR_PERM:
		printf("Mount: Not owner.\n");
		break;
	case MNT3ERR_NOENT:
		printf("Mount: No such file or directory.\n");
		break;
	case MNT3ERR_IO:
		printf("Mount: I/O error.\n");
		break;
	case MNT3ERR_ACCES:
		printf("Mount: Permission denied.\n");
		break;
	case MNT3ERR_NOTDIR:
		printf("Mount: Not a directory.\n");
		break;
	case MNT3ERR_INVAL:
		printf("Mount: Invalid argument.\n");
		break;
	case MNT3ERR_NAMETOOLONG:
		printf("Mount: File name too long.\n");
		break;
	case MNT3ERR_NOTSUPP:
		printf("Mount: Operation not supported.\n");
		break;
	case MNT3ERR_SERVERFAULT:
		printf("Mount: Server fault.\n");
		break;
	default:
		printf("Mount: unknown error.\n");
		break;
	}
}

static int
nfs3mountroot(char *path, struct nfs_file *filep)
{
	int		rexmit;
	int		resp_wait;
	struct mountres3 res3;
	enum clnt_stat	status;

	/*
	 * Wait up to 16 secs for first response, retransmitting expon.
	 */
	rexmit = 0;	/* default retransmission interval */
	resp_wait = 16;

	/*
	 * Try to mount using V3
	 */
	do {
		bzero(&res3, sizeof (struct mountres3));

		status = brpc_call((rpcprog_t)MOUNTPROG, (rpcvers_t)MOUNTVERS3,
		    (rpcproc_t)MOUNTPROC_MNT, xdr_path, (caddr_t)&path,
		    xdr_mountres3, (caddr_t)&res3, rexmit, resp_wait,
		    &root_to, NULL, AUTH_UNIX);

		if (status != RPC_TIMEDOUT)
			break;

		dprintf("boot: %s:%s mount server not responding.\n",
			    root_hostname, path);

		rexmit = resp_wait;
		resp_wait = 0;	/* use default wait time. */

		xdr_free(xdr_mountres3, (caddr_t)&res3);
	} while (status == RPC_TIMEDOUT);

	if ((status != RPC_SUCCESS) || (res3.fhs_status != MNT_OK)) {
		mnt3_error(res3.fhs_status);
		root_to.sin_port = 0;
		return (-1);
	}

	/*
	 * Since the mount succeeded, we'll mark the filep's
	 * status as NFS_OK, and its type as NF3DIR. If these
	 * points aren't the case, then we wouldn't be here.
	 */
	filep->fh.fh3.len = res3.mountres3_u.mountinfo.fhandle.fhandle3_len;
	bcopy(res3.mountres3_u.mountinfo.fhandle.fhandle3_val,
			filep->fh.fh3.data,
			filep->fh.fh3.len);
	filep->ftype.type3 = NF3DIR;
	filep->version = NFS_V3;
	/*
	 * Hardwire in a known reasonable upper limit of 32K
	 */
	nfs_readsize = nfs_readsize <  32 * 1024 ? nfs_readsize : 32 * 1024;
	/*
	 * Set a reasonable lower limit on readsize
	 */
	nfs_readsize = (nfs_readsize != 0 && nfs_readsize < 512) ?
							512 : nfs_readsize;
	xdr_free(xdr_mountres3, (caddr_t)&res3);
	return (0);
}

/*
 * Setup v4 client for inetboot
 */
static int
nfs4init(char *path, uint16_t nfs_port)
{
	struct timeval	wait;
	int		fd = -1;
	int		error = 0;
	enum clnt_stat	rpc_stat;
	struct nfs_file	rootpath;

	wait.tv_sec = RPC_RCVWAIT_MSEC / 1000;
	wait.tv_usec = 0;

	/*
	 * If we haven't explicitly set the port number, set to the standard
	 * 2049 and don't cause a rpcbind request.
	 */
	if (nfs_port == 0)
		nfs_port = 2049;

	root_to.sin_port = htons(nfs_port);

	/*
	 * Support TCP only
	 */
	root_CLIENT = clntbtcp_create(&root_to, NFS_PROGRAM,
					NFS_V4, wait, &fd,
					NFS4BUF_SIZE, NFS4BUF_SIZE);

	if (root_CLIENT == NULL) {
		root_to.sin_port = 0;
		return (-1);
	}

	root_CLIENT->cl_auth =
			authunix_create(my_hostname, 0, 1, 1, &fake_gids);

	/*
	 * Send NULL proc the server first to see if V4 exists
	 */
	rpc_stat = CLNT_CALL(root_CLIENT, NFSPROC4_NULL, xdr_void, NULL,
				xdr_void, NULL, wait);

	if (rpc_stat != RPC_SUCCESS) {
		dprintf("boot: NULL proc failed NFSv4 service not available\n");
		AUTH_DESTROY(root_CLIENT->cl_auth);
		CLNT_DESTROY(root_CLIENT);
		root_to.sin_port = 0;
		return (-1);
	}

	/*
	 * Do a lookup to get to the root_path.  This is nice since it can
	 * handle multicomponent lookups.
	 */
	roothandle.version = NFS_V4;
	roothandle.ftype.type4 = NF4DIR;
	roothandle.fh.fh4.len = 0;		/* Force a PUTROOTFH */
	roothandle.offset = (uint_t)0;		/* it's a directory! */
	error = lookup(path, &rootpath, TRUE);

	if (error) {
		printf("boot: lookup %s failed\n", path);
		return (-1);
	}
	roothandle = rootpath;	/* structure copy */

	/*
	 * Hardwire in a known reasonable upper limit of 32K
	 */
	nfs_readsize = nfs_readsize <  32 * 1024 ? nfs_readsize : 32 * 1024;
	/*
	 * Set a reasonable lower limit on readsize
	 */
	nfs_readsize = (nfs_readsize != 0 && nfs_readsize < 512) ?
							512 : nfs_readsize;

	return (0);
}

static int
atoi(const char *p)
{
	int n;
	int c, neg = 0;

	if (!isdigit(c = *p)) {
		while (c == ' ' || c == '\t' || c == '\n')
			c = *++p;
		switch (c) {
		case '-':
			neg++;
			/* FALLTHROUGH */
		case '+':
			c = *++p;
		}
		if (!isdigit(c))
			return (0);
	}
	for (n = '0' - c; isdigit(c = *++p); ) {
		n *= 10; /* two steps to avoid unnecessary overflow */
		n += '0' - c; /* accum neg to avoid surprises at MAX */
	}
	return (neg ? n : -n);
}

/*
 * Parse suboptions from a string.
 * Same as getsubopt(3C).
 */
static int
getsubopt(char **optionsp, char * const *tokens, char **valuep)
{
	char *s = *optionsp, *p;
	int i;
	size_t optlen;

	*valuep = NULL;
	if (*s == '\0')
		return (-1);
	p = strchr(s, ',');		/* find next option */
	if (p == NULL) {
		p = s + strlen(s);
	} else {
		*p++ = '\0';		/* mark end and point to next */
	}
	*optionsp = p;			/* point to next option */
	p = strchr(s, '=');		/* find value */
	if (p == NULL) {
		optlen = strlen(s);
		*valuep = NULL;
	} else {
		optlen = p - s;
		*valuep = ++p;
	}
	for (i = 0; tokens[i] != NULL; i++) {
		if ((optlen == strlen(tokens[i])) &&
		    (strncmp(s, tokens[i], optlen) == 0))
			return (i);
	}
	/* no match, point value at option and return error */
	*valuep = s;
	return (-1);
}

/*
 * The only interesting NFS mount options for initiating the kernel
 * all others are ignored.
 */
static char *optlist[] = {
#define	OPT_RSIZE	0
	MNTOPT_RSIZE,
#define	OPT_TIMEO	1
	MNTOPT_TIMEO,
#define	OPT_VERS	2
	MNTOPT_VERS,
#define	OPT_PROTO	3
	MNTOPT_PROTO,
#define	OPT_PORT	4
	MNTOPT_PORT,
	NULL
};

/*
 * This routine will open a device as it is known by the V2 OBP. It
 * then goes thru the stuff necessary to initialize the network device,
 * get our network parameters, (using DHCP or rarp/bootparams), and
 * finally actually go and get the root filehandle. Sound like fun?
 * Suuurrrree. Take a look.
 *
 * Returns 0 if things worked. -1 if we crashed and burned.
 */
int
boot_nfs_mountroot(char *str)
{
	int		status;
	enum clnt_stat	rpc_stat;
	char		*root_path = &root_pathbuf[0];	/* to make XDR happy */
	struct timeval	wait;
	int		fd;
	int		bufsize;
	char		*opts, *val;
	int		nfs_version = 0;
	int		istcp = 1;
	int		nfs_port = 0;	/* Cause pmap to get port */
	struct sockaddr_in tmp_addr;	/* throw away */

	if (root_CLIENT != NULL) {
		AUTH_DESTROY(root_CLIENT->cl_auth);
		CLNT_DESTROY(root_CLIENT);
		root_CLIENT = NULL;
	}

	root_to.sin_family = AF_INET;
	root_to.sin_addr.s_addr = htonl(INADDR_ANY);
	root_to.sin_port = htons(0);

	mac_init(str);

	(void) ipv4_setpromiscuous(TRUE);

	if (get_netconfig_strategy() == NCT_BOOTP_DHCP) {
		if (boothowto & RB_VERBOSE)
			printf("Using BOOTP/DHCP...\n");
		if (dhcp() != 0 || setup_root_vars() != 0) {
			(void) ipv4_setpromiscuous(FALSE);
			if (boothowto & RB_VERBOSE)
				printf("BOOTP/DHCP configuration failed!\n");
			return (-1);
		}

		/* now that we have an IP address, turn off promiscuous mode */
		(void) ipv4_setpromiscuous(FALSE);
	} else {
		/* Use RARP/BOOTPARAMS. RARP will try forever... */
		if (boothowto & RB_VERBOSE)
			printf("Using RARP/BOOTPARAMS...\n");
		mac_call_rarp();

		/*
		 * Since there is no way to determine our netmask, and therefore
		 * figure out if the router we got is useful, we assume all
		 * services are local. Use DHCP if this bothers you.
		 */
		dontroute = TRUE;
		/*
		 * We are trying to keep the ARP response
		 * timeout on the lower side with BOOTP/RARP.
		 * We are doing this for BOOTP/RARP where policy
		 * doesn't allow to route the packets outside
		 * the subnet as it has no idea about the
		 * netmask. By doing so, we are reducing
		 * ARP response timeout for any packet destined
		 * for outside booting clients subnet. Client can
		 * not expect such ARP replies and will finally
		 * timeout after a long delay. This would cause
		 * booting client to get stalled for a longer
		 * time. We can not avoid accepting any outside
		 * subnet packets accidentally destined for the
		 * booting client.
		 */
		mac_set_arp_timeout(ARP_INETBOOT_TIMEOUT);

		/* now that we have an IP address, turn off promiscuous mode */
		(void) ipv4_setpromiscuous(FALSE);

		/* get our hostname */
		if (whoami() == FALSE)
			return (-1);

		/* get our bootparams. */
		if (getfile("root", root_hostname, &root_to.sin_addr,
		    root_pathbuf) == FALSE)
			return (-1);

		/* get our rootopts. */
		(void) getfile("rootopts", root_hostname, &tmp_addr.sin_addr,
		    rootopts);
	}

	/* mount root */
	if (boothowto & RB_VERBOSE) {
		printf("root server: %s (%s)\n", root_hostname,
		    inet_ntoa(root_to.sin_addr));
		printf("root directory: %s\n", root_pathbuf);
	}

	/*
	 * Assumes we've configured the stack and thus know our
	 * IP address/hostname, either by using DHCP or rarp/bootparams.
	 */
	gethostname(my_hostname, sizeof (my_hostname));

	wait.tv_sec = RPC_RCVWAIT_MSEC / 1000;
	wait.tv_usec = 0;

	/*
	 * Parse out the interesting root options, if an invalid
	 * or unknown option is provided, silently ignore it and
	 * use the defaults.
	 */
	opts = rootopts;
	while (*opts) {
		int ival;
		switch (getsubopt(&opts, optlist, &val)) {
		case OPT_RSIZE:
			if (val == NULL || !isdigit(*val))
				break;
			nfs_readsize = atoi(val);
			break;
		case OPT_TIMEO:
			if (val == NULL || !isdigit(*val))
				break;
			ival = atoi(val);
			wait.tv_sec = ival / 10;
			wait.tv_usec = (ival % 10) * 100000;
			break;
		case OPT_VERS:
			if (val == NULL || !isdigit(*val))
				break;
			nfs_version = atoi(val);
			break;
		case OPT_PROTO:
			if (val == NULL || isdigit(*val))
				break;
			if ((strncmp(val, "udp", 3) == 0))
				istcp = 0;
			else
				istcp = 1;	/* must be tcp */
			break;
		case OPT_PORT:
			if (val == NULL || !isdigit(*val))
				break;
			nfs_port = atoi(val);

			/*
			 * Currently nfs_dlinet.c doesn't support setting
			 * the root NFS port. Delete this when it does.
			 */
			nfs_port = 0;
			break;
		default:
			/*
			 * Unknown options are silently ignored
			 */
			break;
		}
	}

	/*
	 * If version is set, then try that version first.
	 */
	switch (nfs_version) {
	case NFS_VERSION:
		if (nfsmountroot(root_path, &roothandle) == 0)
			goto domount;
		break;
	case NFS_V3:
		if (nfs3mountroot(root_path, &roothandle) == 0)
			goto domount;
		break;
	case NFS_V4:
		/*
		 * With v4 we skip the mount and go straight to
		 * setting the root filehandle.  Because of this we
		 * do things slightly differently and obtain our
		 * client handle first.
		 */
		if (istcp && nfs4init(root_path, nfs_port) == 0) {
			/*
			 * If v4 init succeeded then we are done.  Just return.
			 */
			return (0);
		}
	}

	/*
	 * If there was no chosen version or the chosen version failed
	 * try all versions in order, this may still fail to boot
	 * at the kernel level if the options are not right, but be
	 * generous at this early stage.
	 */
	if (istcp && nfs4init(root_path, nfs_port) == 0) {
		/*
		 * If v4 init succeeded then we are done.  Just return.
		 */
		return (0);
	}

	if (nfs3mountroot(root_path, &roothandle) == 0)
		goto domount;

	if ((status = nfsmountroot(root_path, &roothandle)) != 0)
		return (status);

domount:
	/*
	 * Only v2 and v3 go on from here.
	 */
	roothandle.offset = (uint_t)0;		/* it's a directory! */
	root_to.sin_port = htons(nfs_port);	/* NFS is next after mount */

	/*
	 * Create the CLIENT handle for NFS operations
	 */
	if (roothandle.version == NFS_VERSION)
		bufsize = NFSBUF_SIZE;
	else
		bufsize = NFS3BUF_SIZE;

	/*
	 * First try TCP then UDP (unless UDP asked for explicitly), if mountd
	 * alows this version but neither transport is available we are stuck.
	 */
	if (istcp) {
		fd = -1;
		root_CLIENT = clntbtcp_create(&root_to, NFS_PROGRAM,
			roothandle.version, wait, &fd, bufsize, bufsize);
		if (root_CLIENT != NULL) {
			root_CLIENT->cl_auth =
			    authunix_create(my_hostname, 0, 1, 1, &fake_gids);
			/*
			 * Send NULL proc, check if the server really exists
			 */
			rpc_stat = CLNT_CALL(root_CLIENT, 0,
					xdr_void, NULL, xdr_void, NULL, wait);

			if (rpc_stat == RPC_SUCCESS)
				return (0);

			AUTH_DESTROY(root_CLIENT->cl_auth);
			CLNT_DESTROY(root_CLIENT);
			root_CLIENT = NULL;
		}
		/* Fall through to UDP case */
	}

	fd = -1;
	root_CLIENT = clntbudp_bufcreate(&root_to, NFS_PROGRAM,
			roothandle.version, wait, &fd, bufsize, bufsize);
	if (root_CLIENT == NULL)
		return (-1);

	root_CLIENT->cl_auth =
			    authunix_create(my_hostname, 0, 1, 1, &fake_gids);
	/*
	 * Send NULL proc, check if the server really exists
	 */
	rpc_stat = CLNT_CALL(root_CLIENT, 0,
				xdr_void, NULL, xdr_void, NULL, wait);

	if (rpc_stat == RPC_SUCCESS)
		return (0);

	AUTH_DESTROY(root_CLIENT->cl_auth);
	CLNT_DESTROY(root_CLIENT);
	root_CLIENT = NULL;
	return (-1);
}