2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
36 static const char copyright[] =
37 "@(#) Copyright (c) 1989, 1993, 1994\n\
38 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)nfsd.c 8.9 (Berkeley) 3/29/95";
47 #include <sys/param.h>
48 #include <sys/syslog.h>
50 #include <sys/mount.h>
51 #include <sys/fcntl.h>
52 #include <sys/linker.h>
53 #include <sys/module.h>
54 #include <sys/types.h>
56 #include <sys/sysctl.h>
57 #include <sys/ucred.h>
60 #include <rpc/pmap_clnt.h>
61 #include <rpcsvc/nfs_prot.h>
64 #include <arpa/inet.h>
65 #include <nfs/nfssvc.h>
67 #include <fs/nfs/nfsproto.h>
68 #include <fs/nfs/nfskpiport.h>
69 #include <fs/nfs/nfs.h>
84 #define NFSD_STABLERESTART "/var/db/nfs-stablerestart"
85 #define NFSD_STABLEBACKUP "/var/db/nfs-stablerestart.bak"
86 #define MAXNFSDCNT 256
91 static pid_t children[MAXNFSDCNT]; /* PIDs of children */
92 static pid_t masterpid; /* PID of master/parent */
93 static int nfsdcnt; /* number of children */
94 static int nfsdcnt_set;
95 static int minthreads;
96 static int maxthreads;
97 static int nfssvc_nfsd; /* Set to correct NFSSVC_xxx flag */
98 static int stablefd = -1; /* Fd for the stable restart file */
99 static int backupfd; /* Fd for the backup stable restart file */
100 static const char *getopt_shortopts;
101 static const char *getopt_usage;
102 static int nfs_minvers = NFS_VER2;
104 static int minthreads_set;
105 static int maxthreads_set;
107 static struct option longopts[] = {
108 { "debug", no_argument, &debug, 1 },
109 { "minthreads", required_argument, &minthreads_set, 1 },
110 { "maxthreads", required_argument, &maxthreads_set, 1 },
111 { "pnfs", required_argument, NULL, 'p' },
112 { "mirror", required_argument, NULL, 'm' },
116 static void cleanup(int);
117 static void child_cleanup(int);
118 static void killchildren(void);
119 static void nfsd_exit(int);
120 static void nonfs(int);
121 static void reapchild(int);
122 static int setbindhost(struct addrinfo **ia, const char *bindhost,
123 struct addrinfo hints);
124 static void start_server(int, struct nfsd_nfsd_args *, const char *vhost);
125 static void unregistration(void);
126 static void usage(void);
127 static void open_stable(int *, int *);
128 static void copy_stable(int, int);
129 static void backup_stable(int);
130 static void set_nfsdcnt(int);
131 static void parse_dsserver(const char *, struct nfsd_nfsd_args *);
134 * Nfs server daemon mostly just a user context for nfssvc()
136 * 1 - do file descriptor and signal cleanup
137 * 2 - fork the nfsd(s)
138 * 3 - create server socket(s)
139 * 4 - register socket with rpcbind
141 * For connectionless protocols, just pass the socket into the kernel via.
143 * For connection based sockets, loop doing accepts. When you get a new
144 * socket from accept, pass the msgsock into the kernel via. nfssvc().
146 * -r - reregister with rpcbind
147 * -d - unregister with rpcbind
148 * -t - support tcp nfs clients
149 * -u - support udp nfs clients
150 * -e - forces it to run a server that supports nfsv4
151 * -p - enable a pNFS service
152 * -m - set the mirroring level for a pNFS service
153 * followed by "n" which is the number of nfsds' to fork off
156 main(int argc, char **argv)
158 struct nfsd_addsock_args addsockargs;
159 struct addrinfo *ai_udp, *ai_tcp, *ai_udp6, *ai_tcp6, hints;
160 struct netconfig *nconf_udp, *nconf_tcp, *nconf_udp6, *nconf_tcp6;
161 struct netbuf nb_udp, nb_tcp, nb_udp6, nb_tcp6;
162 struct sockaddr_storage peer;
163 fd_set ready, sockbits;
164 int ch, connect_type_cnt, i, maxsock, msgsock;
166 int on = 1, unregister, reregister, sock;
167 int tcp6sock, ip6flag, tcpflag, tcpsock;
168 int udpflag, ecode, error, s;
169 int bindhostc, bindanyflag, rpcbreg, rpcbregcnt;
171 int jailed, longindex = 0;
172 size_t jailed_size, nfs_minvers_size;
174 char **bindhost = NULL;
176 struct nfsd_nfsd_args nfsdargs;
177 const char *vhostname = NULL;
179 nfsdargs.mirrorcnt = 1;
180 nfsdargs.addr = NULL;
181 nfsdargs.addrlen = 0;
182 nfsdcnt = DEFNFSDCNT;
183 unregister = reregister = tcpflag = maxsock = 0;
184 bindanyflag = udpflag = connect_type_cnt = bindhostc = 0;
185 getopt_shortopts = "ah:n:rdtuep:m:V:";
188 " nfsd [-ardtue] [-h bindip]\n"
189 " [-n numservers] [--minthreads #] [--maxthreads #]\n"
190 " [-p/--pnfs dsserver0:/dsserver0-mounted-on-dir,...,"
191 "dsserverN:/dsserverN-mounted-on-dir] [-m mirrorlevel]\n"
192 " [-V virtual_hostname]\n";
193 while ((ch = getopt_long(argc, argv, getopt_shortopts, longopts,
197 if (strlen(optarg) <= MAXHOSTNAMELEN)
200 warnx("Virtual host name (%s) is too long",
207 set_nfsdcnt(atoi(optarg));
211 bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
212 if (bindhost == NULL)
213 errx(1, "Out of memory");
214 bindhost[bindhostc-1] = strdup(optarg);
215 if (bindhost[bindhostc-1] == NULL)
216 errx(1, "Out of memory");
231 /* now a no-op, since this is the default */
234 /* Parse out the DS server host names and mount pts. */
235 parse_dsserver(optarg, &nfsdargs);
238 /* Set the mirror level for a pNFS service. */
240 if (i < 2 || i > NFSDEV_MAXMIRRORS)
241 errx(1, "Mirror level out of range 2<-->%d",
243 nfsdargs.mirrorcnt = i;
246 lopt = longopts[longindex].name;
247 if (!strcmp(lopt, "minthreads")) {
248 minthreads = atoi(optarg);
249 } else if (!strcmp(lopt, "maxthreads")) {
250 maxthreads = atoi(optarg);
257 if (!tcpflag && !udpflag)
261 if (minthreads_set && maxthreads_set && minthreads > maxthreads)
263 "error: minthreads(%d) can't be greater than "
264 "maxthreads(%d)", minthreads, maxthreads);
268 * Backward compatibility, trailing number is the count of daemons.
273 set_nfsdcnt(atoi(argv[0]));
276 * Unless the "-o" option was specified, try and run "nfsd".
277 * If "-o" was specified, try and run "nfsserver".
279 if (modfind("nfsd") < 0) {
280 /* Not present in kernel, try loading it */
281 if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
282 errx(1, "NFS server is not available");
286 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
288 if (errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT)
291 } else if (getnetconfigent("udp6") == NULL ||
292 getnetconfigent("tcp6") == NULL) {
298 if (bindhostc == 0 || bindanyflag) {
300 bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
301 if (bindhost == NULL)
302 errx(1, "Out of memory");
303 bindhost[bindhostc-1] = strdup("*");
304 if (bindhost[bindhostc-1] == NULL)
305 errx(1, "Out of memory");
310 * Unregister before setting nfs_minvers, in case the
311 * value of vfs.nfsd.server_min_nfsvers has changed
312 * since registering with rpcbind.
318 nfs_minvers_size = sizeof(nfs_minvers);
319 error = sysctlbyname("vfs.nfsd.server_min_nfsvers", &nfs_minvers,
320 &nfs_minvers_size, NULL, 0);
321 if (error != 0 || nfs_minvers < NFS_VER2 || nfs_minvers > NFS_VER4) {
322 warnx("sysctlbyname(vfs.nfsd.server_min_nfsvers) failed,"
323 " defaulting to NFSv2");
324 nfs_minvers = NFS_VER2;
329 memset(&hints, 0, sizeof hints);
330 hints.ai_flags = AI_PASSIVE;
331 hints.ai_family = AF_INET;
332 hints.ai_socktype = SOCK_DGRAM;
333 hints.ai_protocol = IPPROTO_UDP;
334 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
336 err(1, "getaddrinfo udp: %s", gai_strerror(ecode));
337 nconf_udp = getnetconfigent("udp");
338 if (nconf_udp == NULL)
339 err(1, "getnetconfigent udp failed");
340 nb_udp.buf = ai_udp->ai_addr;
341 nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
342 if (nfs_minvers == NFS_VER2)
343 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp,
345 err(1, "rpcb_set udp failed");
346 if (nfs_minvers <= NFS_VER3)
347 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp,
349 err(1, "rpcb_set udp failed");
350 freeaddrinfo(ai_udp);
352 if (udpflag && ip6flag) {
353 memset(&hints, 0, sizeof hints);
354 hints.ai_flags = AI_PASSIVE;
355 hints.ai_family = AF_INET6;
356 hints.ai_socktype = SOCK_DGRAM;
357 hints.ai_protocol = IPPROTO_UDP;
358 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
360 err(1, "getaddrinfo udp6: %s", gai_strerror(ecode));
361 nconf_udp6 = getnetconfigent("udp6");
362 if (nconf_udp6 == NULL)
363 err(1, "getnetconfigent udp6 failed");
364 nb_udp6.buf = ai_udp6->ai_addr;
365 nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
366 if (nfs_minvers == NFS_VER2)
367 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp6,
369 err(1, "rpcb_set udp6 failed");
370 if (nfs_minvers <= NFS_VER3)
371 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6,
373 err(1, "rpcb_set udp6 failed");
374 freeaddrinfo(ai_udp6);
377 memset(&hints, 0, sizeof hints);
378 hints.ai_flags = AI_PASSIVE;
379 hints.ai_family = AF_INET;
380 hints.ai_socktype = SOCK_STREAM;
381 hints.ai_protocol = IPPROTO_TCP;
382 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp);
384 err(1, "getaddrinfo tcp: %s", gai_strerror(ecode));
385 nconf_tcp = getnetconfigent("tcp");
386 if (nconf_tcp == NULL)
387 err(1, "getnetconfigent tcp failed");
388 nb_tcp.buf = ai_tcp->ai_addr;
389 nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
390 if (nfs_minvers == NFS_VER2)
391 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp,
393 err(1, "rpcb_set tcp failed");
394 if (nfs_minvers <= NFS_VER3)
395 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp,
397 err(1, "rpcb_set tcp failed");
398 freeaddrinfo(ai_tcp);
400 if (tcpflag && ip6flag) {
401 memset(&hints, 0, sizeof hints);
402 hints.ai_flags = AI_PASSIVE;
403 hints.ai_family = AF_INET6;
404 hints.ai_socktype = SOCK_STREAM;
405 hints.ai_protocol = IPPROTO_TCP;
406 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
408 err(1, "getaddrinfo tcp6: %s", gai_strerror(ecode));
409 nconf_tcp6 = getnetconfigent("tcp6");
410 if (nconf_tcp6 == NULL)
411 err(1, "getnetconfigent tcp6 failed");
412 nb_tcp6.buf = ai_tcp6->ai_addr;
413 nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
414 if (nfs_minvers == NFS_VER2)
415 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6,
417 err(1, "rpcb_set tcp6 failed");
418 if (nfs_minvers <= NFS_VER3)
419 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6,
421 err(1, "rpcb_set tcp6 failed");
422 freeaddrinfo(ai_tcp6);
428 (void)signal(SIGHUP, SIG_IGN);
429 (void)signal(SIGINT, SIG_IGN);
431 * nfsd sits in the kernel most of the time. It needs
432 * to ignore SIGTERM/SIGQUIT in order to stay alive as long
433 * as possible during a shutdown, otherwise loopback
434 * mounts will not be able to unmount.
436 (void)signal(SIGTERM, SIG_IGN);
437 (void)signal(SIGQUIT, SIG_IGN);
439 (void)signal(SIGSYS, nonfs);
440 (void)signal(SIGCHLD, reapchild);
441 (void)signal(SIGUSR2, backup_stable);
443 openlog("nfsd", LOG_PID | (debug ? LOG_PERROR : 0), LOG_DAEMON);
446 * For V4, we open the stablerestart file and call nfssvc()
447 * to get it loaded. This is done before the daemons do the
448 * regular nfssvc() call to service NFS requests.
449 * (This way the file remains open until the last nfsd is killed
451 * It and the backup copy will be created as empty files
452 * the first time this nfsd is started and should never be
453 * deleted/replaced if at all possible. It should live on a
454 * local, non-volatile storage device that does not do hardware
455 * level write-back caching. (See SCSI doc for more information
456 * on how to prevent write-back caching on SCSI disks.)
458 open_stable(&stablefd, &backupfd);
460 syslog(LOG_ERR, "Can't open %s: %m\n", NFSD_STABLERESTART);
463 /* This system call will fail for old kernels, but that's ok. */
464 nfssvc(NFSSVC_BACKUPSTABLE, NULL);
465 if (nfssvc(NFSSVC_STABLERESTART, (caddr_t)&stablefd) < 0) {
466 if (errno == EPERM) {
468 jailed_size = sizeof(jailed);
469 sysctlbyname("security.jail.jailed", &jailed,
470 &jailed_size, NULL, 0);
472 syslog(LOG_ERR, "nfssvc stablerestart failed: "
473 "allow.nfsd might not be configured");
475 syslog(LOG_ERR, "nfssvc stablerestart failed");
476 } else if (errno == ENXIO)
477 syslog(LOG_ERR, "nfssvc stablerestart failed: is nfsd "
480 syslog(LOG_ERR, "Can't read stable storage file: %m\n");
483 nfssvc_addsock = NFSSVC_NFSDADDSOCK;
484 nfssvc_nfsd = NFSSVC_NFSDNFSD | NFSSVC_NEWSTRUCT;
488 * For TCP mode, we fork once to start the first
489 * kernel nfsd thread. The kernel will add more
492 masterpid = getpid();
495 syslog(LOG_ERR, "fork: %m");
501 (void)signal(SIGUSR1, child_cleanup);
502 setproctitle("server");
503 start_server(0, &nfsdargs, vhostname);
507 (void)signal(SIGUSR1, cleanup);
511 /* Set up the socket for udp and rpcb register it. */
514 for (i = 0; i < bindhostc; i++) {
515 memset(&hints, 0, sizeof hints);
516 hints.ai_flags = AI_PASSIVE;
517 hints.ai_family = AF_INET;
518 hints.ai_socktype = SOCK_DGRAM;
519 hints.ai_protocol = IPPROTO_UDP;
520 if (setbindhost(&ai_udp, bindhost[i], hints) == 0) {
523 if ((sock = socket(ai_udp->ai_family,
525 ai_udp->ai_protocol)) < 0) {
527 "can't create udp socket");
530 if (bind(sock, ai_udp->ai_addr,
531 ai_udp->ai_addrlen) < 0) {
533 "can't bind udp addr %s: %m",
537 freeaddrinfo(ai_udp);
538 addsockargs.sock = sock;
539 addsockargs.name = NULL;
540 addsockargs.namelen = 0;
541 if (nfssvc(nfssvc_addsock, &addsockargs) < 0) {
542 syslog(LOG_ERR, "can't Add UDP socket");
549 memset(&hints, 0, sizeof hints);
550 hints.ai_flags = AI_PASSIVE;
551 hints.ai_family = AF_INET;
552 hints.ai_socktype = SOCK_DGRAM;
553 hints.ai_protocol = IPPROTO_UDP;
554 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
556 syslog(LOG_ERR, "getaddrinfo udp: %s",
557 gai_strerror(ecode));
560 nconf_udp = getnetconfigent("udp");
561 if (nconf_udp == NULL)
562 err(1, "getnetconfigent udp failed");
563 nb_udp.buf = ai_udp->ai_addr;
564 nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
565 if (nfs_minvers == NFS_VER2)
566 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp,
568 err(1, "rpcb_set udp failed");
569 if (nfs_minvers <= NFS_VER3)
570 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp,
572 err(1, "rpcb_set udp failed");
573 freeaddrinfo(ai_udp);
577 /* Set up the socket for udp6 and rpcb register it. */
578 if (udpflag && ip6flag) {
580 for (i = 0; i < bindhostc; i++) {
581 memset(&hints, 0, sizeof hints);
582 hints.ai_flags = AI_PASSIVE;
583 hints.ai_family = AF_INET6;
584 hints.ai_socktype = SOCK_DGRAM;
585 hints.ai_protocol = IPPROTO_UDP;
586 if (setbindhost(&ai_udp6, bindhost[i], hints) == 0) {
589 if ((sock = socket(ai_udp6->ai_family,
590 ai_udp6->ai_socktype,
591 ai_udp6->ai_protocol)) < 0) {
593 "can't create udp6 socket");
596 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
597 &on, sizeof on) < 0) {
599 "can't set v6-only binding for "
603 if (bind(sock, ai_udp6->ai_addr,
604 ai_udp6->ai_addrlen) < 0) {
606 "can't bind udp6 addr %s: %m",
610 freeaddrinfo(ai_udp6);
611 addsockargs.sock = sock;
612 addsockargs.name = NULL;
613 addsockargs.namelen = 0;
614 if (nfssvc(nfssvc_addsock, &addsockargs) < 0) {
616 "can't add UDP6 socket");
623 memset(&hints, 0, sizeof hints);
624 hints.ai_flags = AI_PASSIVE;
625 hints.ai_family = AF_INET6;
626 hints.ai_socktype = SOCK_DGRAM;
627 hints.ai_protocol = IPPROTO_UDP;
628 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
630 syslog(LOG_ERR, "getaddrinfo udp6: %s",
631 gai_strerror(ecode));
634 nconf_udp6 = getnetconfigent("udp6");
635 if (nconf_udp6 == NULL)
636 err(1, "getnetconfigent udp6 failed");
637 nb_udp6.buf = ai_udp6->ai_addr;
638 nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
639 if (nfs_minvers == NFS_VER2)
640 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp6,
643 "rpcb_set udp6 failed");
644 if (nfs_minvers <= NFS_VER3)
645 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6,
648 "rpcb_set udp6 failed");
649 freeaddrinfo(ai_udp6);
653 /* Set up the socket for tcp and rpcb register it. */
656 for (i = 0; i < bindhostc; i++) {
657 memset(&hints, 0, sizeof hints);
658 hints.ai_flags = AI_PASSIVE;
659 hints.ai_family = AF_INET;
660 hints.ai_socktype = SOCK_STREAM;
661 hints.ai_protocol = IPPROTO_TCP;
662 if (setbindhost(&ai_tcp, bindhost[i], hints) == 0) {
665 if ((tcpsock = socket(AF_INET, SOCK_STREAM,
668 "can't create tcp socket");
671 if (setsockopt(tcpsock, SOL_SOCKET,
673 (char *)&on, sizeof(on)) < 0)
675 "setsockopt SO_REUSEADDR: %m");
676 if (bind(tcpsock, ai_tcp->ai_addr,
677 ai_tcp->ai_addrlen) < 0) {
679 "can't bind tcp addr %s: %m",
683 if (listen(tcpsock, -1) < 0) {
684 syslog(LOG_ERR, "listen failed");
687 freeaddrinfo(ai_tcp);
688 FD_SET(tcpsock, &sockbits);
694 memset(&hints, 0, sizeof hints);
695 hints.ai_flags = AI_PASSIVE;
696 hints.ai_family = AF_INET;
697 hints.ai_socktype = SOCK_STREAM;
698 hints.ai_protocol = IPPROTO_TCP;
699 ecode = getaddrinfo(NULL, "nfs", &hints,
702 syslog(LOG_ERR, "getaddrinfo tcp: %s",
703 gai_strerror(ecode));
706 nconf_tcp = getnetconfigent("tcp");
707 if (nconf_tcp == NULL)
708 err(1, "getnetconfigent tcp failed");
709 nb_tcp.buf = ai_tcp->ai_addr;
710 nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
711 if (nfs_minvers == NFS_VER2)
712 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp,
714 err(1, "rpcb_set tcp failed");
715 if (nfs_minvers <= NFS_VER3)
716 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp,
718 err(1, "rpcb_set tcp failed");
719 freeaddrinfo(ai_tcp);
723 /* Set up the socket for tcp6 and rpcb register it. */
724 if (tcpflag && ip6flag) {
726 for (i = 0; i < bindhostc; i++) {
727 memset(&hints, 0, sizeof hints);
728 hints.ai_flags = AI_PASSIVE;
729 hints.ai_family = AF_INET6;
730 hints.ai_socktype = SOCK_STREAM;
731 hints.ai_protocol = IPPROTO_TCP;
732 if (setbindhost(&ai_tcp6, bindhost[i], hints) == 0) {
735 if ((tcp6sock = socket(ai_tcp6->ai_family,
736 ai_tcp6->ai_socktype,
737 ai_tcp6->ai_protocol)) < 0) {
739 "can't create tcp6 socket");
742 if (setsockopt(tcp6sock, SOL_SOCKET,
744 (char *)&on, sizeof(on)) < 0)
746 "setsockopt SO_REUSEADDR: %m");
747 if (setsockopt(tcp6sock, IPPROTO_IPV6,
748 IPV6_V6ONLY, &on, sizeof on) < 0) {
750 "can't set v6-only binding for tcp6 "
754 if (bind(tcp6sock, ai_tcp6->ai_addr,
755 ai_tcp6->ai_addrlen) < 0) {
757 "can't bind tcp6 addr %s: %m",
761 if (listen(tcp6sock, -1) < 0) {
762 syslog(LOG_ERR, "listen failed");
765 freeaddrinfo(ai_tcp6);
766 FD_SET(tcp6sock, &sockbits);
767 if (maxsock < tcp6sock)
773 memset(&hints, 0, sizeof hints);
774 hints.ai_flags = AI_PASSIVE;
775 hints.ai_family = AF_INET6;
776 hints.ai_socktype = SOCK_STREAM;
777 hints.ai_protocol = IPPROTO_TCP;
778 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
780 syslog(LOG_ERR, "getaddrinfo tcp6: %s",
781 gai_strerror(ecode));
784 nconf_tcp6 = getnetconfigent("tcp6");
785 if (nconf_tcp6 == NULL)
786 err(1, "getnetconfigent tcp6 failed");
787 nb_tcp6.buf = ai_tcp6->ai_addr;
788 nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
789 if (nfs_minvers == NFS_VER2)
790 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6,
792 err(1, "rpcb_set tcp6 failed");
793 if (nfs_minvers <= NFS_VER3)
794 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6,
796 err(1, "rpcb_set tcp6 failed");
797 freeaddrinfo(ai_tcp6);
801 if (rpcbregcnt == 0) {
802 syslog(LOG_ERR, "rpcb_set() failed, nothing to do: %m");
806 if (tcpflag && connect_type_cnt == 0) {
807 syslog(LOG_ERR, "tcp connects == 0, nothing to do: %m");
811 setproctitle("master");
813 * We always want a master to have a clean way to shut nfsd down
814 * (with unregistration): if the master is killed, it unregisters and
815 * kills all children. If we run for UDP only (and so do not have to
816 * loop waiting for accept), we instead make the parent
817 * a "server" too. start_server will not return.
820 start_server(1, &nfsdargs, vhostname);
823 * Loop forever accepting connections and passing the sockets
824 * into the kernel for the mounts.
828 if (connect_type_cnt > 1) {
829 if (select(maxsock + 1,
830 &ready, NULL, NULL, NULL) < 1) {
834 syslog(LOG_ERR, "select failed: %m");
838 for (tcpsock = 0; tcpsock <= maxsock; tcpsock++) {
839 if (FD_ISSET(tcpsock, &ready)) {
841 if ((msgsock = accept(tcpsock,
842 (struct sockaddr *)&peer, &len)) < 0) {
844 syslog(LOG_ERR, "accept failed: %m");
845 if (error == ECONNABORTED ||
850 if (setsockopt(msgsock, SOL_SOCKET,
851 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
853 "setsockopt SO_KEEPALIVE: %m");
854 addsockargs.sock = msgsock;
855 addsockargs.name = (caddr_t)&peer;
856 addsockargs.namelen = len;
857 nfssvc(nfssvc_addsock, &addsockargs);
858 (void)close(msgsock);
865 setbindhost(struct addrinfo **ai, const char *bindhost, struct addrinfo hints)
868 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
871 if (bindhost == NULL || strcmp("*", bindhost) == 0)
876 if (hostptr != NULL) {
877 switch (hints.ai_family) {
879 if (inet_pton(AF_INET, hostptr, host_addr) == 1) {
880 hints.ai_flags = AI_NUMERICHOST;
882 if (inet_pton(AF_INET6, hostptr,
888 if (inet_pton(AF_INET6, hostptr, host_addr) == 1) {
889 hints.ai_flags = AI_NUMERICHOST;
891 if (inet_pton(AF_INET, hostptr,
901 ecode = getaddrinfo(hostptr, "nfs", &hints, ai);
903 syslog(LOG_ERR, "getaddrinfo %s: %s", bindhost,
904 gai_strerror(ecode));
911 set_nfsdcnt(int proposed)
915 warnx("nfsd count too low %d; reset to %d", proposed,
917 nfsdcnt = DEFNFSDCNT;
918 } else if (proposed > MAXNFSDCNT) {
919 warnx("nfsd count too high %d; truncated to %d", proposed,
921 nfsdcnt = MAXNFSDCNT;
930 (void)fprintf(stderr, "%s", getopt_usage);
935 nonfs(__unused int signo)
937 syslog(LOG_ERR, "missing system call: NFS not available");
941 reapchild(__unused int signo)
946 while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) {
947 for (i = 0; i < nfsdcnt; i++)
948 if (pid == children[i])
956 if ((nfs_minvers == NFS_VER2 && !rpcb_unset(NFS_PROGRAM, 2, NULL)) ||
957 (nfs_minvers <= NFS_VER3 && !rpcb_unset(NFS_PROGRAM, 3, NULL)))
958 syslog(LOG_ERR, "rpcb_unset failed");
966 for (i = 0; i < nfsdcnt; i++) {
968 kill(children[i], SIGKILL);
973 * Cleanup master after SIGUSR1.
976 cleanup(__unused int signo)
982 * Cleanup child after SIGUSR1.
985 child_cleanup(__unused int signo)
991 nfsd_exit(int status)
999 get_tuned_nfsdcount(void)
1001 int ncpu, error, tuned_nfsdcnt;
1004 ncpu_size = sizeof(ncpu);
1005 error = sysctlbyname("hw.ncpu", &ncpu, &ncpu_size, NULL, 0);
1007 warnx("sysctlbyname(hw.ncpu) failed defaulting to %d nfs servers",
1009 tuned_nfsdcnt = DEFNFSDCNT;
1011 tuned_nfsdcnt = ncpu * 8;
1013 return tuned_nfsdcnt;
1017 start_server(int master, struct nfsd_nfsd_args *nfsdargp, const char *vhost)
1019 char principal[MAXHOSTNAMELEN + 5];
1021 char hostname[MAXHOSTNAMELEN + 1], *cp;
1022 struct addrinfo *aip, hints;
1026 gethostname(hostname, sizeof (hostname));
1028 strlcpy(hostname, vhost, sizeof (hostname));
1029 snprintf(principal, sizeof (principal), "nfs@%s", hostname);
1030 if ((cp = strchr(hostname, '.')) == NULL ||
1031 *(cp + 1) == '\0') {
1032 /* If not fully qualified, try getaddrinfo() */
1033 memset((void *)&hints, 0, sizeof (hints));
1034 hints.ai_flags = AI_CANONNAME;
1035 error = getaddrinfo(hostname, NULL, &hints, &aip);
1037 if (aip->ai_canonname != NULL &&
1038 (cp = strchr(aip->ai_canonname, '.')) !=
1039 NULL && *(cp + 1) != '\0')
1040 snprintf(principal, sizeof (principal),
1041 "nfs@%s", aip->ai_canonname);
1045 nfsdargp->principal = principal;
1048 nfsdargp->minthreads = nfsdargp->maxthreads = nfsdcnt;
1050 nfsdargp->minthreads = minthreads_set ? minthreads : get_tuned_nfsdcount();
1051 nfsdargp->maxthreads = maxthreads_set ? maxthreads : nfsdargp->minthreads;
1052 if (nfsdargp->maxthreads < nfsdargp->minthreads)
1053 nfsdargp->maxthreads = nfsdargp->minthreads;
1055 error = nfssvc(nfssvc_nfsd, nfsdargp);
1056 if (error < 0 && errno == EAUTH) {
1058 * This indicates that it could not register the
1059 * rpcsec_gss credentials, usually because the
1060 * gssd daemon isn't running.
1061 * (only the experimental server with nfsv4)
1063 syslog(LOG_ERR, "No gssd, using AUTH_SYS only");
1064 principal[0] = '\0';
1065 error = nfssvc(nfssvc_nfsd, nfsdargp);
1068 if (errno == ENXIO) {
1069 syslog(LOG_ERR, "Bad -p option, cannot run");
1070 if (masterpid != 0 && master == 0)
1071 kill(masterpid, SIGUSR1);
1073 syslog(LOG_ERR, "nfssvc: %m");
1083 * Open the stable restart file and return the file descriptor for it.
1086 open_stable(int *stable_fdp, int *backup_fdp)
1088 int stable_fd, backup_fd = -1, ret;
1089 struct stat st, backup_st;
1091 /* Open and stat the stable restart file. */
1092 stable_fd = open(NFSD_STABLERESTART, O_RDWR, 0);
1094 stable_fd = open(NFSD_STABLERESTART, O_RDWR | O_CREAT, 0600);
1095 if (stable_fd >= 0) {
1096 ret = fstat(stable_fd, &st);
1103 /* Open and stat the backup stable restart file. */
1104 if (stable_fd >= 0) {
1105 backup_fd = open(NFSD_STABLEBACKUP, O_RDWR, 0);
1107 backup_fd = open(NFSD_STABLEBACKUP, O_RDWR | O_CREAT,
1109 if (backup_fd >= 0) {
1110 ret = fstat(backup_fd, &backup_st);
1116 if (backup_fd < 0) {
1122 *stable_fdp = stable_fd;
1123 *backup_fdp = backup_fd;
1127 /* Sync up the 2 files, as required. */
1129 copy_stable(stable_fd, backup_fd);
1130 else if (backup_st.st_size > 0)
1131 copy_stable(backup_fd, stable_fd);
1135 * Copy the stable restart file to the backup or vice versa.
1138 copy_stable(int from_fd, int to_fd)
1141 static char buf[1024];
1143 ret = lseek(from_fd, (off_t)0, SEEK_SET);
1145 ret = lseek(to_fd, (off_t)0, SEEK_SET);
1147 ret = ftruncate(to_fd, (off_t)0);
1150 cnt = read(from_fd, buf, 1024);
1152 ret = write(to_fd, buf, cnt);
1155 } while (cnt > 0 && ret >= 0);
1159 syslog(LOG_ERR, "stable restart copy failure: %m");
1163 * Back up the stable restart file when indicated by the kernel.
1166 backup_stable(__unused int signo)
1170 copy_stable(stablefd, backupfd);
1174 * Parse the pNFS string and extract the DS servers and ports numbers.
1177 parse_dsserver(const char *optionarg, struct nfsd_nfsd_args *nfsdargp)
1179 char *cp, *cp2, *dsaddr, *dshost, *dspath, *dsvol, nfsprt[9];
1180 char *mdspath, *mdsp, ip6[INET6_ADDRSTRLEN];
1183 u_int adsiz, dsaddrcnt, dshostcnt, dspathcnt, hostsiz, pathsiz;
1185 size_t dsaddrsiz, dshostsiz, dspathsiz, nfsprtsiz, mdspathsiz;
1186 struct addrinfo hints, *ai_tcp, *res;
1187 struct sockaddr_in sin;
1188 struct sockaddr_in6 sin6;
1190 cp = strdup(optionarg);
1192 errx(1, "Out of memory");
1194 /* Now, do the host names. */
1197 dspath = malloc(dspathsiz);
1199 errx(1, "Out of memory");
1202 dshost = malloc(dshostsiz);
1204 errx(1, "Out of memory");
1207 dsaddr = malloc(dsaddrsiz);
1209 errx(1, "Out of memory");
1212 mdspath = malloc(mdspathsiz);
1213 if (mdspath == NULL)
1214 errx(1, "Out of memory");
1216 /* Put the NFS port# in "." form. */
1217 snprintf(nfsprt, 9, ".%d.%d", 2049 >> 8, 2049 & 0xff);
1218 nfsprtsiz = strlen(nfsprt);
1221 /* Loop around for each DS server name. */
1223 cp2 = strchr(cp, ',');
1225 /* Not the last DS in the list. */
1231 dsvol = strchr(cp, ':');
1232 if (dsvol == NULL || *(dsvol + 1) == '\0')
1236 /* Optional path for MDS file system to be stored on DS. */
1237 mdsp = strchr(dsvol, '#');
1239 if (*(mdsp + 1) == '\0' || mdsp <= dsvol)
1244 /* Append this pathname to dspath. */
1245 pathsiz = strlen(dsvol);
1246 if (dspathcnt + pathsiz + 1 > dspathsiz) {
1248 dspath = realloc(dspath, dspathsiz);
1250 errx(1, "Out of memory");
1252 strcpy(&dspath[dspathcnt], dsvol);
1253 dspathcnt += pathsiz + 1;
1255 /* Append this pathname to mdspath. */
1257 pathsiz = strlen(mdsp);
1260 if (mdspathcnt + pathsiz + 1 > mdspathsiz) {
1262 mdspath = realloc(mdspath, mdspathsiz);
1263 if (mdspath == NULL)
1264 errx(1, "Out of memory");
1267 strcpy(&mdspath[mdspathcnt], mdsp);
1269 mdspath[mdspathcnt] = '\0';
1270 mdspathcnt += pathsiz + 1;
1273 freeaddrinfo(ai_tcp);
1275 /* Get the fully qualified domain name and IP address. */
1276 memset(&hints, 0, sizeof(hints));
1277 hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
1278 hints.ai_family = PF_UNSPEC;
1279 hints.ai_socktype = SOCK_STREAM;
1280 hints.ai_protocol = IPPROTO_TCP;
1281 ecode = getaddrinfo(cp, NULL, &hints, &ai_tcp);
1283 err(1, "getaddrinfo pnfs: %s %s", cp,
1284 gai_strerror(ecode));
1286 for (res = ai_tcp; res != NULL; res = res->ai_next) {
1287 if (res->ai_addr->sa_family == AF_INET) {
1288 if (res->ai_addrlen < sizeof(sin))
1289 err(1, "getaddrinfo() returned "
1290 "undersized IPv4 address");
1292 * Mips cares about sockaddr_in alignment,
1293 * so copy the address.
1295 memcpy(&sin, res->ai_addr, sizeof(sin));
1296 ad = inet_ntoa(sin.sin_addr);
1298 } else if (res->ai_family == AF_INET6) {
1299 if (res->ai_addrlen < sizeof(sin6))
1300 err(1, "getaddrinfo() returned "
1301 "undersized IPv6 address");
1303 * Mips cares about sockaddr_in6 alignment,
1304 * so copy the address.
1306 memcpy(&sin6, res->ai_addr, sizeof(sin6));
1307 ad = inet_ntop(AF_INET6, &sin6.sin6_addr, ip6,
1312 * Since a link local address will only
1313 * work if the client and DS are in the
1314 * same scope zone, only use it if it is
1318 !IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr))
1323 err(1, "No IP address for %s", cp);
1325 /* Append this address to dsaddr. */
1327 if (dsaddrcnt + adsiz + nfsprtsiz + 1 > dsaddrsiz) {
1329 dsaddr = realloc(dsaddr, dsaddrsiz);
1331 errx(1, "Out of memory");
1333 strcpy(&dsaddr[dsaddrcnt], ad);
1334 strcat(&dsaddr[dsaddrcnt], nfsprt);
1335 dsaddrcnt += adsiz + nfsprtsiz + 1;
1337 /* Append this hostname to dshost. */
1338 hostsiz = strlen(ai_tcp->ai_canonname);
1339 if (dshostcnt + hostsiz + 1 > dshostsiz) {
1341 dshost = realloc(dshost, dshostsiz);
1343 errx(1, "Out of memory");
1345 strcpy(&dshost[dshostcnt], ai_tcp->ai_canonname);
1346 dshostcnt += hostsiz + 1;
1349 } while (cp != NULL);
1351 nfsdargp->addr = dsaddr;
1352 nfsdargp->addrlen = dsaddrcnt;
1353 nfsdargp->dnshost = dshost;
1354 nfsdargp->dnshostlen = dshostcnt;
1355 nfsdargp->dspath = dspath;
1356 nfsdargp->dspathlen = dspathcnt;
1357 nfsdargp->mdspath = mdspath;
1358 nfsdargp->mdspathlen = mdspathcnt;
1359 freeaddrinfo(ai_tcp);