xref: /freebsd/usr.sbin/nfsd/nfsd.c (revision 6829dae12bb055451fa467da4589c43bd03b1e64)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Rick Macklem at The University of Guelph.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #ifndef lint
36 static const char copyright[] =
37 "@(#) Copyright (c) 1989, 1993, 1994\n\
38 	The Regents of the University of California.  All rights reserved.\n";
39 #endif /* not lint */
40 
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)nfsd.c	8.9 (Berkeley) 3/29/95";
44 #endif
45 static const char rcsid[] =
46   "$FreeBSD$";
47 #endif /* not lint */
48 
49 #include <sys/param.h>
50 #include <sys/syslog.h>
51 #include <sys/wait.h>
52 #include <sys/mount.h>
53 #include <sys/fcntl.h>
54 #include <sys/linker.h>
55 #include <sys/module.h>
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #include <sys/sysctl.h>
59 #include <sys/ucred.h>
60 
61 #include <rpc/rpc.h>
62 #include <rpc/pmap_clnt.h>
63 #include <rpcsvc/nfs_prot.h>
64 
65 #include <netdb.h>
66 #include <arpa/inet.h>
67 #include <nfs/nfssvc.h>
68 
69 #include <fs/nfs/nfsproto.h>
70 #include <fs/nfs/nfskpiport.h>
71 #include <fs/nfs/nfs.h>
72 
73 #include <err.h>
74 #include <errno.h>
75 #include <signal.h>
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <string.h>
79 #include <unistd.h>
80 #include <sysexits.h>
81 
82 #include <getopt.h>
83 
84 static int	debug = 0;
85 
86 #define	NFSD_STABLERESTART	"/var/db/nfs-stablerestart"
87 #define	NFSD_STABLEBACKUP	"/var/db/nfs-stablerestart.bak"
88 #define	MAXNFSDCNT	256
89 #define	DEFNFSDCNT	 4
90 #define	NFS_VER2	 2
91 #define NFS_VER3	 3
92 #define NFS_VER4	 4
93 static pid_t children[MAXNFSDCNT]; /* PIDs of children */
94 static pid_t masterpid;		   /* PID of master/parent */
95 static int nfsdcnt;		/* number of children */
96 static int nfsdcnt_set;
97 static int minthreads;
98 static int maxthreads;
99 static int nfssvc_nfsd;		/* Set to correct NFSSVC_xxx flag */
100 static int stablefd = -1;	/* Fd for the stable restart file */
101 static int backupfd;		/* Fd for the backup stable restart file */
102 static const char *getopt_shortopts;
103 static const char *getopt_usage;
104 
105 static int minthreads_set;
106 static int maxthreads_set;
107 
108 static struct option longopts[] = {
109 	{ "debug", no_argument, &debug, 1 },
110 	{ "minthreads", required_argument, &minthreads_set, 1 },
111 	{ "maxthreads", required_argument, &maxthreads_set, 1 },
112 	{ "pnfs", required_argument, NULL, 'p' },
113 	{ "mirror", required_argument, NULL, 'm' },
114 	{ NULL, 0, NULL, 0}
115 };
116 
117 static void	cleanup(int);
118 static void	child_cleanup(int);
119 static void	killchildren(void);
120 static void	nfsd_exit(int);
121 static void	nonfs(int);
122 static void	reapchild(int);
123 static int	setbindhost(struct addrinfo **ia, const char *bindhost,
124 		    struct addrinfo hints);
125 static void	start_server(int, struct nfsd_nfsd_args *, const char *vhost);
126 static void	unregistration(void);
127 static void	usage(void);
128 static void	open_stable(int *, int *);
129 static void	copy_stable(int, int);
130 static void	backup_stable(int);
131 static void	set_nfsdcnt(int);
132 static void	parse_dsserver(const char *, struct nfsd_nfsd_args *);
133 
134 /*
135  * Nfs server daemon mostly just a user context for nfssvc()
136  *
137  * 1 - do file descriptor and signal cleanup
138  * 2 - fork the nfsd(s)
139  * 3 - create server socket(s)
140  * 4 - register socket with rpcbind
141  *
142  * For connectionless protocols, just pass the socket into the kernel via.
143  * nfssvc().
144  * For connection based sockets, loop doing accepts. When you get a new
145  * socket from accept, pass the msgsock into the kernel via. nfssvc().
146  * The arguments are:
147  *	-r - reregister with rpcbind
148  *	-d - unregister with rpcbind
149  *	-t - support tcp nfs clients
150  *	-u - support udp nfs clients
151  *	-e - forces it to run a server that supports nfsv4
152  *	-p - enable a pNFS service
153  *	-m - set the mirroring level for a pNFS service
154  * followed by "n" which is the number of nfsds' to fork off
155  */
156 int
157 main(int argc, char **argv)
158 {
159 	struct nfsd_addsock_args addsockargs;
160 	struct addrinfo *ai_udp, *ai_tcp, *ai_udp6, *ai_tcp6, hints;
161 	struct netconfig *nconf_udp, *nconf_tcp, *nconf_udp6, *nconf_tcp6;
162 	struct netbuf nb_udp, nb_tcp, nb_udp6, nb_tcp6;
163 	struct sockaddr_storage peer;
164 	fd_set ready, sockbits;
165 	int ch, connect_type_cnt, i, maxsock, msgsock;
166 	socklen_t len;
167 	int on = 1, unregister, reregister, sock;
168 	int tcp6sock, ip6flag, tcpflag, tcpsock;
169 	int udpflag, ecode, error, s;
170 	int bindhostc, bindanyflag, rpcbreg, rpcbregcnt;
171 	int nfssvc_addsock;
172 	int longindex = 0;
173 	int nfs_minvers = NFS_VER2;
174 	size_t nfs_minvers_size;
175 	const char *lopt;
176 	char **bindhost = NULL;
177 	pid_t pid;
178 	struct nfsd_nfsd_args nfsdargs;
179 	const char *vhostname = NULL;
180 
181 	nfsdargs.mirrorcnt = 1;
182 	nfsdargs.addr = NULL;
183 	nfsdargs.addrlen = 0;
184 	nfsdcnt = DEFNFSDCNT;
185 	unregister = reregister = tcpflag = maxsock = 0;
186 	bindanyflag = udpflag = connect_type_cnt = bindhostc = 0;
187 	getopt_shortopts = "ah:n:rdtuep:m:V:";
188 	getopt_usage =
189 	    "usage:\n"
190 	    "  nfsd [-ardtue] [-h bindip]\n"
191 	    "       [-n numservers] [--minthreads #] [--maxthreads #]\n"
192 	    "       [-p/--pnfs dsserver0:/dsserver0-mounted-on-dir,...,"
193 	    "dsserverN:/dsserverN-mounted-on-dir] [-m mirrorlevel]\n"
194 	    "       [-V virtual_hostname]\n";
195 	while ((ch = getopt_long(argc, argv, getopt_shortopts, longopts,
196 		    &longindex)) != -1)
197 		switch (ch) {
198 		case 'V':
199 			if (strlen(optarg) <= MAXHOSTNAMELEN)
200 				vhostname = optarg;
201 			else
202 				warnx("Virtual host name (%s) is too long",
203 				    optarg);
204 			break;
205 		case 'a':
206 			bindanyflag = 1;
207 			break;
208 		case 'n':
209 			set_nfsdcnt(atoi(optarg));
210 			break;
211 		case 'h':
212 			bindhostc++;
213 			bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
214 			if (bindhost == NULL)
215 				errx(1, "Out of memory");
216 			bindhost[bindhostc-1] = strdup(optarg);
217 			if (bindhost[bindhostc-1] == NULL)
218 				errx(1, "Out of memory");
219 			break;
220 		case 'r':
221 			reregister = 1;
222 			break;
223 		case 'd':
224 			unregister = 1;
225 			break;
226 		case 't':
227 			tcpflag = 1;
228 			break;
229 		case 'u':
230 			udpflag = 1;
231 			break;
232 		case 'e':
233 			/* now a no-op, since this is the default */
234 			break;
235 		case 'p':
236 			/* Parse out the DS server host names and mount pts. */
237 			parse_dsserver(optarg, &nfsdargs);
238 			break;
239 		case 'm':
240 			/* Set the mirror level for a pNFS service. */
241 			i = atoi(optarg);
242 			if (i < 2 || i > NFSDEV_MAXMIRRORS)
243 				errx(1, "Mirror level out of range 2<-->%d",
244 				    NFSDEV_MAXMIRRORS);
245 			nfsdargs.mirrorcnt = i;
246 			break;
247 		case 0:
248 			lopt = longopts[longindex].name;
249 			if (!strcmp(lopt, "minthreads")) {
250 				minthreads = atoi(optarg);
251 			} else if (!strcmp(lopt, "maxthreads")) {
252 				maxthreads = atoi(optarg);
253 			}
254 			break;
255 		default:
256 		case '?':
257 			usage();
258 		}
259 	if (!tcpflag && !udpflag)
260 		udpflag = 1;
261 	argv += optind;
262 	argc -= optind;
263 	if (minthreads_set && maxthreads_set && minthreads > maxthreads)
264 		errx(EX_USAGE,
265 		    "error: minthreads(%d) can't be greater than "
266 		    "maxthreads(%d)", minthreads, maxthreads);
267 
268 	/*
269 	 * XXX
270 	 * Backward compatibility, trailing number is the count of daemons.
271 	 */
272 	if (argc > 1)
273 		usage();
274 	if (argc == 1)
275 		set_nfsdcnt(atoi(argv[0]));
276 
277 	/*
278 	 * Unless the "-o" option was specified, try and run "nfsd".
279 	 * If "-o" was specified, try and run "nfsserver".
280 	 */
281 	if (modfind("nfsd") < 0) {
282 		/* Not present in kernel, try loading it */
283 		if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
284 			errx(1, "NFS server is not available");
285 	}
286 
287 	ip6flag = 1;
288 	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
289 	if (s == -1) {
290 		if (errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT)
291 			err(1, "socket");
292 		ip6flag = 0;
293 	} else if (getnetconfigent("udp6") == NULL ||
294 		getnetconfigent("tcp6") == NULL) {
295 		ip6flag = 0;
296 	}
297 	if (s != -1)
298 		close(s);
299 
300 	if (bindhostc == 0 || bindanyflag) {
301 		bindhostc++;
302 		bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
303 		if (bindhost == NULL)
304 			errx(1, "Out of memory");
305 		bindhost[bindhostc-1] = strdup("*");
306 		if (bindhost[bindhostc-1] == NULL)
307 			errx(1, "Out of memory");
308 	}
309 
310 	nfs_minvers_size = sizeof(nfs_minvers);
311 	error = sysctlbyname("vfs.nfsd.server_min_nfsvers", &nfs_minvers,
312 	    &nfs_minvers_size, NULL, 0);
313 	if (error != 0 || nfs_minvers < NFS_VER2 || nfs_minvers > NFS_VER4) {
314 		warnx("sysctlbyname(vfs.nfsd.server_min_nfsvers) failed,"
315 		    " defaulting to NFSv2");
316 		nfs_minvers = NFS_VER2;
317 	}
318 
319 	if (unregister) {
320 		unregistration();
321 		exit (0);
322 	}
323 	if (reregister) {
324 		if (udpflag) {
325 			memset(&hints, 0, sizeof hints);
326 			hints.ai_flags = AI_PASSIVE;
327 			hints.ai_family = AF_INET;
328 			hints.ai_socktype = SOCK_DGRAM;
329 			hints.ai_protocol = IPPROTO_UDP;
330 			ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
331 			if (ecode != 0)
332 				err(1, "getaddrinfo udp: %s", gai_strerror(ecode));
333 			nconf_udp = getnetconfigent("udp");
334 			if (nconf_udp == NULL)
335 				err(1, "getnetconfigent udp failed");
336 			nb_udp.buf = ai_udp->ai_addr;
337 			nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
338 			if (nfs_minvers == NFS_VER2)
339 				if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp,
340 				    &nb_udp))
341 					err(1, "rpcb_set udp failed");
342 			if (nfs_minvers <= NFS_VER3)
343 				if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp,
344 				    &nb_udp))
345 					err(1, "rpcb_set udp failed");
346 			freeaddrinfo(ai_udp);
347 		}
348 		if (udpflag && ip6flag) {
349 			memset(&hints, 0, sizeof hints);
350 			hints.ai_flags = AI_PASSIVE;
351 			hints.ai_family = AF_INET6;
352 			hints.ai_socktype = SOCK_DGRAM;
353 			hints.ai_protocol = IPPROTO_UDP;
354 			ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
355 			if (ecode != 0)
356 				err(1, "getaddrinfo udp6: %s", gai_strerror(ecode));
357 			nconf_udp6 = getnetconfigent("udp6");
358 			if (nconf_udp6 == NULL)
359 				err(1, "getnetconfigent udp6 failed");
360 			nb_udp6.buf = ai_udp6->ai_addr;
361 			nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
362 			if (nfs_minvers == NFS_VER2)
363 				if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp6,
364 				    &nb_udp6))
365 					err(1, "rpcb_set udp6 failed");
366 			if (nfs_minvers <= NFS_VER3)
367 				if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6,
368 				    &nb_udp6))
369 					err(1, "rpcb_set udp6 failed");
370 			freeaddrinfo(ai_udp6);
371 		}
372 		if (tcpflag) {
373 			memset(&hints, 0, sizeof hints);
374 			hints.ai_flags = AI_PASSIVE;
375 			hints.ai_family = AF_INET;
376 			hints.ai_socktype = SOCK_STREAM;
377 			hints.ai_protocol = IPPROTO_TCP;
378 			ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp);
379 			if (ecode != 0)
380 				err(1, "getaddrinfo tcp: %s", gai_strerror(ecode));
381 			nconf_tcp = getnetconfigent("tcp");
382 			if (nconf_tcp == NULL)
383 				err(1, "getnetconfigent tcp failed");
384 			nb_tcp.buf = ai_tcp->ai_addr;
385 			nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
386 			if (nfs_minvers == NFS_VER2)
387 				if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp,
388 				    &nb_tcp))
389 					err(1, "rpcb_set tcp failed");
390 			if (nfs_minvers <= NFS_VER3)
391 				if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp,
392 				    &nb_tcp))
393 					err(1, "rpcb_set tcp failed");
394 			freeaddrinfo(ai_tcp);
395 		}
396 		if (tcpflag && ip6flag) {
397 			memset(&hints, 0, sizeof hints);
398 			hints.ai_flags = AI_PASSIVE;
399 			hints.ai_family = AF_INET6;
400 			hints.ai_socktype = SOCK_STREAM;
401 			hints.ai_protocol = IPPROTO_TCP;
402 			ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
403 			if (ecode != 0)
404 				err(1, "getaddrinfo tcp6: %s", gai_strerror(ecode));
405 			nconf_tcp6 = getnetconfigent("tcp6");
406 			if (nconf_tcp6 == NULL)
407 				err(1, "getnetconfigent tcp6 failed");
408 			nb_tcp6.buf = ai_tcp6->ai_addr;
409 			nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
410 			if (nfs_minvers == NFS_VER2)
411 				if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6,
412 				    &nb_tcp6))
413 					err(1, "rpcb_set tcp6 failed");
414 			if (nfs_minvers <= NFS_VER3)
415 				if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6,
416 				   &nb_tcp6))
417 					err(1, "rpcb_set tcp6 failed");
418 			freeaddrinfo(ai_tcp6);
419 		}
420 		exit (0);
421 	}
422 	if (debug == 0) {
423 		daemon(0, 0);
424 		(void)signal(SIGHUP, SIG_IGN);
425 		(void)signal(SIGINT, SIG_IGN);
426 		/*
427 		 * nfsd sits in the kernel most of the time.  It needs
428 		 * to ignore SIGTERM/SIGQUIT in order to stay alive as long
429 		 * as possible during a shutdown, otherwise loopback
430 		 * mounts will not be able to unmount.
431 		 */
432 		(void)signal(SIGTERM, SIG_IGN);
433 		(void)signal(SIGQUIT, SIG_IGN);
434 	}
435 	(void)signal(SIGSYS, nonfs);
436 	(void)signal(SIGCHLD, reapchild);
437 	(void)signal(SIGUSR2, backup_stable);
438 
439 	openlog("nfsd", LOG_PID | (debug ? LOG_PERROR : 0), LOG_DAEMON);
440 
441 	/*
442 	 * For V4, we open the stablerestart file and call nfssvc()
443 	 * to get it loaded. This is done before the daemons do the
444 	 * regular nfssvc() call to service NFS requests.
445 	 * (This way the file remains open until the last nfsd is killed
446 	 *  off.)
447 	 * It and the backup copy will be created as empty files
448 	 * the first time this nfsd is started and should never be
449 	 * deleted/replaced if at all possible. It should live on a
450 	 * local, non-volatile storage device that does not do hardware
451 	 * level write-back caching. (See SCSI doc for more information
452 	 * on how to prevent write-back caching on SCSI disks.)
453 	 */
454 	open_stable(&stablefd, &backupfd);
455 	if (stablefd < 0) {
456 		syslog(LOG_ERR, "Can't open %s: %m\n", NFSD_STABLERESTART);
457 		exit(1);
458 	}
459 	/* This system call will fail for old kernels, but that's ok. */
460 	nfssvc(NFSSVC_BACKUPSTABLE, NULL);
461 	if (nfssvc(NFSSVC_STABLERESTART, (caddr_t)&stablefd) < 0) {
462 		syslog(LOG_ERR, "Can't read stable storage file: %m\n");
463 		exit(1);
464 	}
465 	nfssvc_addsock = NFSSVC_NFSDADDSOCK;
466 	nfssvc_nfsd = NFSSVC_NFSDNFSD | NFSSVC_NEWSTRUCT;
467 
468 	if (tcpflag) {
469 		/*
470 		 * For TCP mode, we fork once to start the first
471 		 * kernel nfsd thread. The kernel will add more
472 		 * threads as needed.
473 		 */
474 		masterpid = getpid();
475 		pid = fork();
476 		if (pid == -1) {
477 			syslog(LOG_ERR, "fork: %m");
478 			nfsd_exit(1);
479 		}
480 		if (pid) {
481 			children[0] = pid;
482 		} else {
483 			(void)signal(SIGUSR1, child_cleanup);
484 			setproctitle("server");
485 			start_server(0, &nfsdargs, vhostname);
486 		}
487 	}
488 
489 	(void)signal(SIGUSR1, cleanup);
490 	FD_ZERO(&sockbits);
491 
492 	rpcbregcnt = 0;
493 	/* Set up the socket for udp and rpcb register it. */
494 	if (udpflag) {
495 		rpcbreg = 0;
496 		for (i = 0; i < bindhostc; i++) {
497 			memset(&hints, 0, sizeof hints);
498 			hints.ai_flags = AI_PASSIVE;
499 			hints.ai_family = AF_INET;
500 			hints.ai_socktype = SOCK_DGRAM;
501 			hints.ai_protocol = IPPROTO_UDP;
502 			if (setbindhost(&ai_udp, bindhost[i], hints) == 0) {
503 				rpcbreg = 1;
504 				rpcbregcnt++;
505 				if ((sock = socket(ai_udp->ai_family,
506 				    ai_udp->ai_socktype,
507 				    ai_udp->ai_protocol)) < 0) {
508 					syslog(LOG_ERR,
509 					    "can't create udp socket");
510 					nfsd_exit(1);
511 				}
512 				if (bind(sock, ai_udp->ai_addr,
513 				    ai_udp->ai_addrlen) < 0) {
514 					syslog(LOG_ERR,
515 					    "can't bind udp addr %s: %m",
516 					    bindhost[i]);
517 					nfsd_exit(1);
518 				}
519 				freeaddrinfo(ai_udp);
520 				addsockargs.sock = sock;
521 				addsockargs.name = NULL;
522 				addsockargs.namelen = 0;
523 				if (nfssvc(nfssvc_addsock, &addsockargs) < 0) {
524 					syslog(LOG_ERR, "can't Add UDP socket");
525 					nfsd_exit(1);
526 				}
527 				(void)close(sock);
528 			}
529 		}
530 		if (rpcbreg == 1) {
531 			memset(&hints, 0, sizeof hints);
532 			hints.ai_flags = AI_PASSIVE;
533 			hints.ai_family = AF_INET;
534 			hints.ai_socktype = SOCK_DGRAM;
535 			hints.ai_protocol = IPPROTO_UDP;
536 			ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
537 			if (ecode != 0) {
538 				syslog(LOG_ERR, "getaddrinfo udp: %s",
539 				   gai_strerror(ecode));
540 				nfsd_exit(1);
541 			}
542 			nconf_udp = getnetconfigent("udp");
543 			if (nconf_udp == NULL)
544 				err(1, "getnetconfigent udp failed");
545 			nb_udp.buf = ai_udp->ai_addr;
546 			nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
547 			if (nfs_minvers == NFS_VER2)
548 				if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp,
549 				    &nb_udp))
550 					err(1, "rpcb_set udp failed");
551 			if (nfs_minvers <= NFS_VER3)
552 				if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp,
553 				    &nb_udp))
554 					err(1, "rpcb_set udp failed");
555 			freeaddrinfo(ai_udp);
556 		}
557 	}
558 
559 	/* Set up the socket for udp6 and rpcb register it. */
560 	if (udpflag && ip6flag) {
561 		rpcbreg = 0;
562 		for (i = 0; i < bindhostc; i++) {
563 			memset(&hints, 0, sizeof hints);
564 			hints.ai_flags = AI_PASSIVE;
565 			hints.ai_family = AF_INET6;
566 			hints.ai_socktype = SOCK_DGRAM;
567 			hints.ai_protocol = IPPROTO_UDP;
568 			if (setbindhost(&ai_udp6, bindhost[i], hints) == 0) {
569 				rpcbreg = 1;
570 				rpcbregcnt++;
571 				if ((sock = socket(ai_udp6->ai_family,
572 				    ai_udp6->ai_socktype,
573 				    ai_udp6->ai_protocol)) < 0) {
574 					syslog(LOG_ERR,
575 						"can't create udp6 socket");
576 					nfsd_exit(1);
577 				}
578 				if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
579 				    &on, sizeof on) < 0) {
580 					syslog(LOG_ERR,
581 					    "can't set v6-only binding for "
582 					    "udp6 socket: %m");
583 					nfsd_exit(1);
584 				}
585 				if (bind(sock, ai_udp6->ai_addr,
586 				    ai_udp6->ai_addrlen) < 0) {
587 					syslog(LOG_ERR,
588 					    "can't bind udp6 addr %s: %m",
589 					    bindhost[i]);
590 					nfsd_exit(1);
591 				}
592 				freeaddrinfo(ai_udp6);
593 				addsockargs.sock = sock;
594 				addsockargs.name = NULL;
595 				addsockargs.namelen = 0;
596 				if (nfssvc(nfssvc_addsock, &addsockargs) < 0) {
597 					syslog(LOG_ERR,
598 					    "can't add UDP6 socket");
599 					nfsd_exit(1);
600 				}
601 				(void)close(sock);
602 			}
603 		}
604 		if (rpcbreg == 1) {
605 			memset(&hints, 0, sizeof hints);
606 			hints.ai_flags = AI_PASSIVE;
607 			hints.ai_family = AF_INET6;
608 			hints.ai_socktype = SOCK_DGRAM;
609 			hints.ai_protocol = IPPROTO_UDP;
610 			ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
611 			if (ecode != 0) {
612 				syslog(LOG_ERR, "getaddrinfo udp6: %s",
613 				   gai_strerror(ecode));
614 				nfsd_exit(1);
615 			}
616 			nconf_udp6 = getnetconfigent("udp6");
617 			if (nconf_udp6 == NULL)
618 				err(1, "getnetconfigent udp6 failed");
619 			nb_udp6.buf = ai_udp6->ai_addr;
620 			nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
621 			if (nfs_minvers == NFS_VER2)
622 				if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp6,
623 				    &nb_udp6))
624 					err(1,
625 					    "rpcb_set udp6 failed");
626 			if (nfs_minvers <= NFS_VER3)
627 				if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6,
628 				    &nb_udp6))
629 					err(1,
630 					    "rpcb_set udp6 failed");
631 			freeaddrinfo(ai_udp6);
632 		}
633 	}
634 
635 	/* Set up the socket for tcp and rpcb register it. */
636 	if (tcpflag) {
637 		rpcbreg = 0;
638 		for (i = 0; i < bindhostc; i++) {
639 			memset(&hints, 0, sizeof hints);
640 			hints.ai_flags = AI_PASSIVE;
641 			hints.ai_family = AF_INET;
642 			hints.ai_socktype = SOCK_STREAM;
643 			hints.ai_protocol = IPPROTO_TCP;
644 			if (setbindhost(&ai_tcp, bindhost[i], hints) == 0) {
645 				rpcbreg = 1;
646 				rpcbregcnt++;
647 				if ((tcpsock = socket(AF_INET, SOCK_STREAM,
648 				    0)) < 0) {
649 					syslog(LOG_ERR,
650 					    "can't create tcp socket");
651 					nfsd_exit(1);
652 				}
653 				if (setsockopt(tcpsock, SOL_SOCKET,
654 				    SO_REUSEADDR,
655 				    (char *)&on, sizeof(on)) < 0)
656 					syslog(LOG_ERR,
657 					     "setsockopt SO_REUSEADDR: %m");
658 				if (bind(tcpsock, ai_tcp->ai_addr,
659 				    ai_tcp->ai_addrlen) < 0) {
660 					syslog(LOG_ERR,
661 					    "can't bind tcp addr %s: %m",
662 					    bindhost[i]);
663 					nfsd_exit(1);
664 				}
665 				if (listen(tcpsock, -1) < 0) {
666 					syslog(LOG_ERR, "listen failed");
667 					nfsd_exit(1);
668 				}
669 				freeaddrinfo(ai_tcp);
670 				FD_SET(tcpsock, &sockbits);
671 				maxsock = tcpsock;
672 				connect_type_cnt++;
673 			}
674 		}
675 		if (rpcbreg == 1) {
676 			memset(&hints, 0, sizeof hints);
677 			hints.ai_flags = AI_PASSIVE;
678 			hints.ai_family = AF_INET;
679 			hints.ai_socktype = SOCK_STREAM;
680 			hints.ai_protocol = IPPROTO_TCP;
681 			ecode = getaddrinfo(NULL, "nfs", &hints,
682 			     &ai_tcp);
683 			if (ecode != 0) {
684 				syslog(LOG_ERR, "getaddrinfo tcp: %s",
685 				   gai_strerror(ecode));
686 				nfsd_exit(1);
687 			}
688 			nconf_tcp = getnetconfigent("tcp");
689 			if (nconf_tcp == NULL)
690 				err(1, "getnetconfigent tcp failed");
691 			nb_tcp.buf = ai_tcp->ai_addr;
692 			nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
693 			if (nfs_minvers == NFS_VER2)
694 				if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp,
695 				    &nb_tcp))
696 					err(1, "rpcb_set tcp failed");
697 			if (nfs_minvers <= NFS_VER3)
698 				if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp,
699 				    &nb_tcp))
700 					err(1, "rpcb_set tcp failed");
701 			freeaddrinfo(ai_tcp);
702 		}
703 	}
704 
705 	/* Set up the socket for tcp6 and rpcb register it. */
706 	if (tcpflag && ip6flag) {
707 		rpcbreg = 0;
708 		for (i = 0; i < bindhostc; i++) {
709 			memset(&hints, 0, sizeof hints);
710 			hints.ai_flags = AI_PASSIVE;
711 			hints.ai_family = AF_INET6;
712 			hints.ai_socktype = SOCK_STREAM;
713 			hints.ai_protocol = IPPROTO_TCP;
714 			if (setbindhost(&ai_tcp6, bindhost[i], hints) == 0) {
715 				rpcbreg = 1;
716 				rpcbregcnt++;
717 				if ((tcp6sock = socket(ai_tcp6->ai_family,
718 				    ai_tcp6->ai_socktype,
719 				    ai_tcp6->ai_protocol)) < 0) {
720 					syslog(LOG_ERR,
721 					    "can't create tcp6 socket");
722 					nfsd_exit(1);
723 				}
724 				if (setsockopt(tcp6sock, SOL_SOCKET,
725 				    SO_REUSEADDR,
726 				    (char *)&on, sizeof(on)) < 0)
727 					syslog(LOG_ERR,
728 					    "setsockopt SO_REUSEADDR: %m");
729 				if (setsockopt(tcp6sock, IPPROTO_IPV6,
730 				    IPV6_V6ONLY, &on, sizeof on) < 0) {
731 					syslog(LOG_ERR,
732 					"can't set v6-only binding for tcp6 "
733 					    "socket: %m");
734 					nfsd_exit(1);
735 				}
736 				if (bind(tcp6sock, ai_tcp6->ai_addr,
737 				    ai_tcp6->ai_addrlen) < 0) {
738 					syslog(LOG_ERR,
739 					    "can't bind tcp6 addr %s: %m",
740 					    bindhost[i]);
741 					nfsd_exit(1);
742 				}
743 				if (listen(tcp6sock, -1) < 0) {
744 					syslog(LOG_ERR, "listen failed");
745 					nfsd_exit(1);
746 				}
747 				freeaddrinfo(ai_tcp6);
748 				FD_SET(tcp6sock, &sockbits);
749 				if (maxsock < tcp6sock)
750 					maxsock = tcp6sock;
751 				connect_type_cnt++;
752 			}
753 		}
754 		if (rpcbreg == 1) {
755 			memset(&hints, 0, sizeof hints);
756 			hints.ai_flags = AI_PASSIVE;
757 			hints.ai_family = AF_INET6;
758 			hints.ai_socktype = SOCK_STREAM;
759 			hints.ai_protocol = IPPROTO_TCP;
760 			ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
761 			if (ecode != 0) {
762 				syslog(LOG_ERR, "getaddrinfo tcp6: %s",
763 				   gai_strerror(ecode));
764 				nfsd_exit(1);
765 			}
766 			nconf_tcp6 = getnetconfigent("tcp6");
767 			if (nconf_tcp6 == NULL)
768 				err(1, "getnetconfigent tcp6 failed");
769 			nb_tcp6.buf = ai_tcp6->ai_addr;
770 			nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
771 			if (nfs_minvers == NFS_VER2)
772 				if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6,
773 				    &nb_tcp6))
774 					err(1, "rpcb_set tcp6 failed");
775 			if (nfs_minvers <= NFS_VER3)
776 				if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6,
777 				    &nb_tcp6))
778 					err(1, "rpcb_set tcp6 failed");
779 			freeaddrinfo(ai_tcp6);
780 		}
781 	}
782 
783 	if (rpcbregcnt == 0) {
784 		syslog(LOG_ERR, "rpcb_set() failed, nothing to do: %m");
785 		nfsd_exit(1);
786 	}
787 
788 	if (tcpflag && connect_type_cnt == 0) {
789 		syslog(LOG_ERR, "tcp connects == 0, nothing to do: %m");
790 		nfsd_exit(1);
791 	}
792 
793 	setproctitle("master");
794 	/*
795 	 * We always want a master to have a clean way to shut nfsd down
796 	 * (with unregistration): if the master is killed, it unregisters and
797 	 * kills all children. If we run for UDP only (and so do not have to
798 	 * loop waiting for accept), we instead make the parent
799 	 * a "server" too. start_server will not return.
800 	 */
801 	if (!tcpflag)
802 		start_server(1, &nfsdargs, vhostname);
803 
804 	/*
805 	 * Loop forever accepting connections and passing the sockets
806 	 * into the kernel for the mounts.
807 	 */
808 	for (;;) {
809 		ready = sockbits;
810 		if (connect_type_cnt > 1) {
811 			if (select(maxsock + 1,
812 			    &ready, NULL, NULL, NULL) < 1) {
813 				error = errno;
814 				if (error == EINTR)
815 					continue;
816 				syslog(LOG_ERR, "select failed: %m");
817 				nfsd_exit(1);
818 			}
819 		}
820 		for (tcpsock = 0; tcpsock <= maxsock; tcpsock++) {
821 			if (FD_ISSET(tcpsock, &ready)) {
822 				len = sizeof(peer);
823 				if ((msgsock = accept(tcpsock,
824 				    (struct sockaddr *)&peer, &len)) < 0) {
825 					error = errno;
826 					syslog(LOG_ERR, "accept failed: %m");
827 					if (error == ECONNABORTED ||
828 					    error == EINTR)
829 						continue;
830 					nfsd_exit(1);
831 				}
832 				if (setsockopt(msgsock, SOL_SOCKET,
833 				    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
834 					syslog(LOG_ERR,
835 					    "setsockopt SO_KEEPALIVE: %m");
836 				addsockargs.sock = msgsock;
837 				addsockargs.name = (caddr_t)&peer;
838 				addsockargs.namelen = len;
839 				nfssvc(nfssvc_addsock, &addsockargs);
840 				(void)close(msgsock);
841 			}
842 		}
843 	}
844 }
845 
846 static int
847 setbindhost(struct addrinfo **ai, const char *bindhost, struct addrinfo hints)
848 {
849 	int ecode;
850 	u_int32_t host_addr[4];  /* IPv4 or IPv6 */
851 	const char *hostptr;
852 
853 	if (bindhost == NULL || strcmp("*", bindhost) == 0)
854 		hostptr = NULL;
855 	else
856 		hostptr = bindhost;
857 
858 	if (hostptr != NULL) {
859 		switch (hints.ai_family) {
860 		case AF_INET:
861 			if (inet_pton(AF_INET, hostptr, host_addr) == 1) {
862 				hints.ai_flags = AI_NUMERICHOST;
863 			} else {
864 				if (inet_pton(AF_INET6, hostptr,
865 				    host_addr) == 1)
866 					return (1);
867 			}
868 			break;
869 		case AF_INET6:
870 			if (inet_pton(AF_INET6, hostptr, host_addr) == 1) {
871 				hints.ai_flags = AI_NUMERICHOST;
872 			} else {
873 				if (inet_pton(AF_INET, hostptr,
874 				    host_addr) == 1)
875 					return (1);
876 			}
877 			break;
878 		default:
879 			break;
880 		}
881 	}
882 
883 	ecode = getaddrinfo(hostptr, "nfs", &hints, ai);
884 	if (ecode != 0) {
885 		syslog(LOG_ERR, "getaddrinfo %s: %s", bindhost,
886 		    gai_strerror(ecode));
887 		return (1);
888 	}
889 	return (0);
890 }
891 
892 static void
893 set_nfsdcnt(int proposed)
894 {
895 
896 	if (proposed < 1) {
897 		warnx("nfsd count too low %d; reset to %d", proposed,
898 		    DEFNFSDCNT);
899 		nfsdcnt = DEFNFSDCNT;
900 	} else if (proposed > MAXNFSDCNT) {
901 		warnx("nfsd count too high %d; truncated to %d", proposed,
902 		    MAXNFSDCNT);
903 		nfsdcnt = MAXNFSDCNT;
904 	} else
905 		nfsdcnt = proposed;
906 	nfsdcnt_set = 1;
907 }
908 
909 static void
910 usage(void)
911 {
912 	(void)fprintf(stderr, "%s", getopt_usage);
913 	exit(1);
914 }
915 
916 static void
917 nonfs(__unused int signo)
918 {
919 	syslog(LOG_ERR, "missing system call: NFS not available");
920 }
921 
922 static void
923 reapchild(__unused int signo)
924 {
925 	pid_t pid;
926 	int i;
927 
928 	while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) {
929 		for (i = 0; i < nfsdcnt; i++)
930 			if (pid == children[i])
931 				children[i] = -1;
932 	}
933 }
934 
935 static void
936 unregistration(void)
937 {
938 	if ((!rpcb_unset(NFS_PROGRAM, 2, NULL)) ||
939 	    (!rpcb_unset(NFS_PROGRAM, 3, NULL)))
940 		syslog(LOG_ERR, "rpcb_unset failed");
941 }
942 
943 static void
944 killchildren(void)
945 {
946 	int i;
947 
948 	for (i = 0; i < nfsdcnt; i++) {
949 		if (children[i] > 0)
950 			kill(children[i], SIGKILL);
951 	}
952 }
953 
954 /*
955  * Cleanup master after SIGUSR1.
956  */
957 static void
958 cleanup(__unused int signo)
959 {
960 	nfsd_exit(0);
961 }
962 
963 /*
964  * Cleanup child after SIGUSR1.
965  */
966 static void
967 child_cleanup(__unused int signo)
968 {
969 	exit(0);
970 }
971 
972 static void
973 nfsd_exit(int status)
974 {
975 	killchildren();
976 	unregistration();
977 	exit(status);
978 }
979 
980 static int
981 get_tuned_nfsdcount(void)
982 {
983 	int ncpu, error, tuned_nfsdcnt;
984 	size_t ncpu_size;
985 
986 	ncpu_size = sizeof(ncpu);
987 	error = sysctlbyname("hw.ncpu", &ncpu, &ncpu_size, NULL, 0);
988 	if (error) {
989 		warnx("sysctlbyname(hw.ncpu) failed defaulting to %d nfs servers",
990 		    DEFNFSDCNT);
991 		tuned_nfsdcnt = DEFNFSDCNT;
992 	} else {
993 		tuned_nfsdcnt = ncpu * 8;
994 	}
995 	return tuned_nfsdcnt;
996 }
997 
998 static void
999 start_server(int master, struct nfsd_nfsd_args *nfsdargp, const char *vhost)
1000 {
1001 	char principal[MAXHOSTNAMELEN + 5];
1002 	int status, error;
1003 	char hostname[MAXHOSTNAMELEN + 1], *cp;
1004 	struct addrinfo *aip, hints;
1005 
1006 	status = 0;
1007 	if (vhost == NULL)
1008 		gethostname(hostname, sizeof (hostname));
1009 	else
1010 		strlcpy(hostname, vhost, sizeof (hostname));
1011 	snprintf(principal, sizeof (principal), "nfs@%s", hostname);
1012 	if ((cp = strchr(hostname, '.')) == NULL ||
1013 	    *(cp + 1) == '\0') {
1014 		/* If not fully qualified, try getaddrinfo() */
1015 		memset((void *)&hints, 0, sizeof (hints));
1016 		hints.ai_flags = AI_CANONNAME;
1017 		error = getaddrinfo(hostname, NULL, &hints, &aip);
1018 		if (error == 0) {
1019 			if (aip->ai_canonname != NULL &&
1020 			    (cp = strchr(aip->ai_canonname, '.')) !=
1021 			    NULL && *(cp + 1) != '\0')
1022 				snprintf(principal, sizeof (principal),
1023 				    "nfs@%s", aip->ai_canonname);
1024 			freeaddrinfo(aip);
1025 		}
1026 	}
1027 	nfsdargp->principal = principal;
1028 
1029 	if (nfsdcnt_set)
1030 		nfsdargp->minthreads = nfsdargp->maxthreads = nfsdcnt;
1031 	else {
1032 		nfsdargp->minthreads = minthreads_set ? minthreads : get_tuned_nfsdcount();
1033 		nfsdargp->maxthreads = maxthreads_set ? maxthreads : nfsdargp->minthreads;
1034 		if (nfsdargp->maxthreads < nfsdargp->minthreads)
1035 			nfsdargp->maxthreads = nfsdargp->minthreads;
1036 	}
1037 	error = nfssvc(nfssvc_nfsd, nfsdargp);
1038 	if (error < 0 && errno == EAUTH) {
1039 		/*
1040 		 * This indicates that it could not register the
1041 		 * rpcsec_gss credentials, usually because the
1042 		 * gssd daemon isn't running.
1043 		 * (only the experimental server with nfsv4)
1044 		 */
1045 		syslog(LOG_ERR, "No gssd, using AUTH_SYS only");
1046 		principal[0] = '\0';
1047 		error = nfssvc(nfssvc_nfsd, nfsdargp);
1048 	}
1049 	if (error < 0) {
1050 		if (errno == ENXIO) {
1051 			syslog(LOG_ERR, "Bad -p option, cannot run");
1052 			if (masterpid != 0 && master == 0)
1053 				kill(masterpid, SIGUSR1);
1054 		} else
1055 			syslog(LOG_ERR, "nfssvc: %m");
1056 		status = 1;
1057 	}
1058 	if (master)
1059 		nfsd_exit(status);
1060 	else
1061 		exit(status);
1062 }
1063 
1064 /*
1065  * Open the stable restart file and return the file descriptor for it.
1066  */
1067 static void
1068 open_stable(int *stable_fdp, int *backup_fdp)
1069 {
1070 	int stable_fd, backup_fd = -1, ret;
1071 	struct stat st, backup_st;
1072 
1073 	/* Open and stat the stable restart file. */
1074 	stable_fd = open(NFSD_STABLERESTART, O_RDWR, 0);
1075 	if (stable_fd < 0)
1076 		stable_fd = open(NFSD_STABLERESTART, O_RDWR | O_CREAT, 0600);
1077 	if (stable_fd >= 0) {
1078 		ret = fstat(stable_fd, &st);
1079 		if (ret < 0) {
1080 			close(stable_fd);
1081 			stable_fd = -1;
1082 		}
1083 	}
1084 
1085 	/* Open and stat the backup stable restart file. */
1086 	if (stable_fd >= 0) {
1087 		backup_fd = open(NFSD_STABLEBACKUP, O_RDWR, 0);
1088 		if (backup_fd < 0)
1089 			backup_fd = open(NFSD_STABLEBACKUP, O_RDWR | O_CREAT,
1090 			    0600);
1091 		if (backup_fd >= 0) {
1092 			ret = fstat(backup_fd, &backup_st);
1093 			if (ret < 0) {
1094 				close(backup_fd);
1095 				backup_fd = -1;
1096 			}
1097 		}
1098 		if (backup_fd < 0) {
1099 			close(stable_fd);
1100 			stable_fd = -1;
1101 		}
1102 	}
1103 
1104 	*stable_fdp = stable_fd;
1105 	*backup_fdp = backup_fd;
1106 	if (stable_fd < 0)
1107 		return;
1108 
1109 	/* Sync up the 2 files, as required. */
1110 	if (st.st_size > 0)
1111 		copy_stable(stable_fd, backup_fd);
1112 	else if (backup_st.st_size > 0)
1113 		copy_stable(backup_fd, stable_fd);
1114 }
1115 
1116 /*
1117  * Copy the stable restart file to the backup or vice versa.
1118  */
1119 static void
1120 copy_stable(int from_fd, int to_fd)
1121 {
1122 	int cnt, ret;
1123 	static char buf[1024];
1124 
1125 	ret = lseek(from_fd, (off_t)0, SEEK_SET);
1126 	if (ret >= 0)
1127 		ret = lseek(to_fd, (off_t)0, SEEK_SET);
1128 	if (ret >= 0)
1129 		ret = ftruncate(to_fd, (off_t)0);
1130 	if (ret >= 0)
1131 		do {
1132 			cnt = read(from_fd, buf, 1024);
1133 			if (cnt > 0)
1134 				ret = write(to_fd, buf, cnt);
1135 			else if (cnt < 0)
1136 				ret = cnt;
1137 		} while (cnt > 0 && ret >= 0);
1138 	if (ret >= 0)
1139 		ret = fsync(to_fd);
1140 	if (ret < 0)
1141 		syslog(LOG_ERR, "stable restart copy failure: %m");
1142 }
1143 
1144 /*
1145  * Back up the stable restart file when indicated by the kernel.
1146  */
1147 static void
1148 backup_stable(__unused int signo)
1149 {
1150 
1151 	if (stablefd >= 0)
1152 		copy_stable(stablefd, backupfd);
1153 }
1154 
1155 /*
1156  * Parse the pNFS string and extract the DS servers and ports numbers.
1157  */
1158 static void
1159 parse_dsserver(const char *optionarg, struct nfsd_nfsd_args *nfsdargp)
1160 {
1161 	char *cp, *cp2, *dsaddr, *dshost, *dspath, *dsvol, nfsprt[9];
1162 	char *mdspath, *mdsp, ip6[INET6_ADDRSTRLEN];
1163 	const char *ad;
1164 	int ecode;
1165 	u_int adsiz, dsaddrcnt, dshostcnt, dspathcnt, hostsiz, pathsiz;
1166 	u_int mdspathcnt;
1167 	size_t dsaddrsiz, dshostsiz, dspathsiz, nfsprtsiz, mdspathsiz;
1168 	struct addrinfo hints, *ai_tcp, *res;
1169 	struct sockaddr_in sin;
1170 	struct sockaddr_in6 sin6;
1171 
1172 	cp = strdup(optionarg);
1173 	if (cp == NULL)
1174 		errx(1, "Out of memory");
1175 
1176 	/* Now, do the host names. */
1177 	dspathsiz = 1024;
1178 	dspathcnt = 0;
1179 	dspath = malloc(dspathsiz);
1180 	if (dspath == NULL)
1181 		errx(1, "Out of memory");
1182 	dshostsiz = 1024;
1183 	dshostcnt = 0;
1184 	dshost = malloc(dshostsiz);
1185 	if (dshost == NULL)
1186 		errx(1, "Out of memory");
1187 	dsaddrsiz = 1024;
1188 	dsaddrcnt = 0;
1189 	dsaddr = malloc(dsaddrsiz);
1190 	if (dsaddr == NULL)
1191 		errx(1, "Out of memory");
1192 	mdspathsiz = 1024;
1193 	mdspathcnt = 0;
1194 	mdspath = malloc(mdspathsiz);
1195 	if (mdspath == NULL)
1196 		errx(1, "Out of memory");
1197 
1198 	/* Put the NFS port# in "." form. */
1199 	snprintf(nfsprt, 9, ".%d.%d", 2049 >> 8, 2049 & 0xff);
1200 	nfsprtsiz = strlen(nfsprt);
1201 
1202 	ai_tcp = NULL;
1203 	/* Loop around for each DS server name. */
1204 	do {
1205 		cp2 = strchr(cp, ',');
1206 		if (cp2 != NULL) {
1207 			/* Not the last DS in the list. */
1208 			*cp2++ = '\0';
1209 			if (*cp2 == '\0')
1210 				usage();
1211 		}
1212 
1213 		dsvol = strchr(cp, ':');
1214 		if (dsvol == NULL || *(dsvol + 1) == '\0')
1215 			usage();
1216 		*dsvol++ = '\0';
1217 
1218 		/* Optional path for MDS file system to be stored on DS. */
1219 		mdsp = strchr(dsvol, '#');
1220 		if (mdsp != NULL) {
1221 			if (*(mdsp + 1) == '\0' || mdsp <= dsvol)
1222 				usage();
1223 			*mdsp++ = '\0';
1224 		}
1225 
1226 		/* Append this pathname to dspath. */
1227 		pathsiz = strlen(dsvol);
1228 		if (dspathcnt + pathsiz + 1 > dspathsiz) {
1229 			dspathsiz *= 2;
1230 			dspath = realloc(dspath, dspathsiz);
1231 			if (dspath == NULL)
1232 				errx(1, "Out of memory");
1233 		}
1234 		strcpy(&dspath[dspathcnt], dsvol);
1235 		dspathcnt += pathsiz + 1;
1236 
1237 		/* Append this pathname to mdspath. */
1238 		if (mdsp != NULL)
1239 			pathsiz = strlen(mdsp);
1240 		else
1241 			pathsiz = 0;
1242 		if (mdspathcnt + pathsiz + 1 > mdspathsiz) {
1243 			mdspathsiz *= 2;
1244 			mdspath = realloc(mdspath, mdspathsiz);
1245 			if (mdspath == NULL)
1246 				errx(1, "Out of memory");
1247 		}
1248 		if (mdsp != NULL)
1249 			strcpy(&mdspath[mdspathcnt], mdsp);
1250 		else
1251 			mdspath[mdspathcnt] = '\0';
1252 		mdspathcnt += pathsiz + 1;
1253 
1254 		if (ai_tcp != NULL)
1255 			freeaddrinfo(ai_tcp);
1256 
1257 		/* Get the fully qualified domain name and IP address. */
1258 		memset(&hints, 0, sizeof(hints));
1259 		hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
1260 		hints.ai_family = PF_UNSPEC;
1261 		hints.ai_socktype = SOCK_STREAM;
1262 		hints.ai_protocol = IPPROTO_TCP;
1263 		ecode = getaddrinfo(cp, NULL, &hints, &ai_tcp);
1264 		if (ecode != 0)
1265 			err(1, "getaddrinfo pnfs: %s %s", cp,
1266 			    gai_strerror(ecode));
1267 		ad = NULL;
1268 		for (res = ai_tcp; res != NULL; res = res->ai_next) {
1269 			if (res->ai_addr->sa_family == AF_INET) {
1270 				if (res->ai_addrlen < sizeof(sin))
1271 					err(1, "getaddrinfo() returned "
1272 					    "undersized IPv4 address");
1273 				/*
1274 				 * Mips cares about sockaddr_in alignment,
1275 				 * so copy the address.
1276 				 */
1277 				memcpy(&sin, res->ai_addr, sizeof(sin));
1278 				ad = inet_ntoa(sin.sin_addr);
1279 				break;
1280 			} else if (res->ai_family == AF_INET6) {
1281 				if (res->ai_addrlen < sizeof(sin6))
1282 					err(1, "getaddrinfo() returned "
1283 					    "undersized IPv6 address");
1284 				/*
1285 				 * Mips cares about sockaddr_in6 alignment,
1286 				 * so copy the address.
1287 				 */
1288 				memcpy(&sin6, res->ai_addr, sizeof(sin6));
1289 				ad = inet_ntop(AF_INET6, &sin6.sin6_addr, ip6,
1290 				    sizeof(ip6));
1291 
1292 				/*
1293 				 * XXX
1294 				 * Since a link local address will only
1295 				 * work if the client and DS are in the
1296 				 * same scope zone, only use it if it is
1297 				 * the only address.
1298 				 */
1299 				if (ad != NULL &&
1300 				    !IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr))
1301 					break;
1302 			}
1303 		}
1304 		if (ad == NULL)
1305 			err(1, "No IP address for %s", cp);
1306 
1307 		/* Append this address to dsaddr. */
1308 		adsiz = strlen(ad);
1309 		if (dsaddrcnt + adsiz + nfsprtsiz + 1 > dsaddrsiz) {
1310 			dsaddrsiz *= 2;
1311 			dsaddr = realloc(dsaddr, dsaddrsiz);
1312 			if (dsaddr == NULL)
1313 				errx(1, "Out of memory");
1314 		}
1315 		strcpy(&dsaddr[dsaddrcnt], ad);
1316 		strcat(&dsaddr[dsaddrcnt], nfsprt);
1317 		dsaddrcnt += adsiz + nfsprtsiz + 1;
1318 
1319 		/* Append this hostname to dshost. */
1320 		hostsiz = strlen(ai_tcp->ai_canonname);
1321 		if (dshostcnt + hostsiz + 1 > dshostsiz) {
1322 			dshostsiz *= 2;
1323 			dshost = realloc(dshost, dshostsiz);
1324 			if (dshost == NULL)
1325 				errx(1, "Out of memory");
1326 		}
1327 		strcpy(&dshost[dshostcnt], ai_tcp->ai_canonname);
1328 		dshostcnt += hostsiz + 1;
1329 
1330 		cp = cp2;
1331 	} while (cp != NULL);
1332 
1333 	nfsdargp->addr = dsaddr;
1334 	nfsdargp->addrlen = dsaddrcnt;
1335 	nfsdargp->dnshost = dshost;
1336 	nfsdargp->dnshostlen = dshostcnt;
1337 	nfsdargp->dspath = dspath;
1338 	nfsdargp->dspathlen = dspathcnt;
1339 	nfsdargp->mdspath = mdspath;
1340 	nfsdargp->mdspathlen = mdspathcnt;
1341 	freeaddrinfo(ai_tcp);
1342 }
1343 
1344