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";
45 static const char rcsid[] =
49 #include <sys/param.h>
50 #include <sys/syslog.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>
58 #include <sys/sysctl.h>
59 #include <sys/ucred.h>
62 #include <rpc/pmap_clnt.h>
63 #include <rpcsvc/nfs_prot.h>
66 #include <arpa/inet.h>
67 #include <nfs/nfssvc.h>
69 #include <fs/nfs/nfsproto.h>
70 #include <fs/nfs/nfskpiport.h>
71 #include <fs/nfs/nfs.h>
86 #define NFSD_STABLERESTART "/var/db/nfs-stablerestart"
87 #define NFSD_STABLEBACKUP "/var/db/nfs-stablerestart.bak"
88 #define MAXNFSDCNT 256
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 static int nfs_minvers = NFS_VER2;
106 static int minthreads_set;
107 static int maxthreads_set;
109 static struct option longopts[] = {
110 { "debug", no_argument, &debug, 1 },
111 { "minthreads", required_argument, &minthreads_set, 1 },
112 { "maxthreads", required_argument, &maxthreads_set, 1 },
113 { "pnfs", required_argument, NULL, 'p' },
114 { "mirror", required_argument, NULL, 'm' },
118 static void cleanup(int);
119 static void child_cleanup(int);
120 static void killchildren(void);
121 static void nfsd_exit(int);
122 static void nonfs(int);
123 static void reapchild(int);
124 static int setbindhost(struct addrinfo **ia, const char *bindhost,
125 struct addrinfo hints);
126 static void start_server(int, struct nfsd_nfsd_args *, const char *vhost);
127 static void unregistration(void);
128 static void usage(void);
129 static void open_stable(int *, int *);
130 static void copy_stable(int, int);
131 static void backup_stable(int);
132 static void set_nfsdcnt(int);
133 static void parse_dsserver(const char *, struct nfsd_nfsd_args *);
136 * Nfs server daemon mostly just a user context for nfssvc()
138 * 1 - do file descriptor and signal cleanup
139 * 2 - fork the nfsd(s)
140 * 3 - create server socket(s)
141 * 4 - register socket with rpcbind
143 * For connectionless protocols, just pass the socket into the kernel via.
145 * For connection based sockets, loop doing accepts. When you get a new
146 * socket from accept, pass the msgsock into the kernel via. nfssvc().
148 * -r - reregister with rpcbind
149 * -d - unregister with rpcbind
150 * -t - support tcp nfs clients
151 * -u - support udp nfs clients
152 * -e - forces it to run a server that supports nfsv4
153 * -p - enable a pNFS service
154 * -m - set the mirroring level for a pNFS service
155 * followed by "n" which is the number of nfsds' to fork off
158 main(int argc, char **argv)
160 struct nfsd_addsock_args addsockargs;
161 struct addrinfo *ai_udp, *ai_tcp, *ai_udp6, *ai_tcp6, hints;
162 struct netconfig *nconf_udp, *nconf_tcp, *nconf_udp6, *nconf_tcp6;
163 struct netbuf nb_udp, nb_tcp, nb_udp6, nb_tcp6;
164 struct sockaddr_storage peer;
165 fd_set ready, sockbits;
166 int ch, connect_type_cnt, i, maxsock, msgsock;
168 int on = 1, unregister, reregister, sock;
169 int tcp6sock, ip6flag, tcpflag, tcpsock;
170 int udpflag, ecode, error, s;
171 int bindhostc, bindanyflag, rpcbreg, rpcbregcnt;
173 int jailed, longindex = 0;
174 size_t jailed_size, nfs_minvers_size;
176 char **bindhost = NULL;
178 struct nfsd_nfsd_args nfsdargs;
179 const char *vhostname = NULL;
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:";
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,
199 if (strlen(optarg) <= MAXHOSTNAMELEN)
202 warnx("Virtual host name (%s) is too long",
209 set_nfsdcnt(atoi(optarg));
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");
233 /* now a no-op, since this is the default */
236 /* Parse out the DS server host names and mount pts. */
237 parse_dsserver(optarg, &nfsdargs);
240 /* Set the mirror level for a pNFS service. */
242 if (i < 2 || i > NFSDEV_MAXMIRRORS)
243 errx(1, "Mirror level out of range 2<-->%d",
245 nfsdargs.mirrorcnt = i;
248 lopt = longopts[longindex].name;
249 if (!strcmp(lopt, "minthreads")) {
250 minthreads = atoi(optarg);
251 } else if (!strcmp(lopt, "maxthreads")) {
252 maxthreads = atoi(optarg);
259 if (!tcpflag && !udpflag)
263 if (minthreads_set && maxthreads_set && minthreads > maxthreads)
265 "error: minthreads(%d) can't be greater than "
266 "maxthreads(%d)", minthreads, maxthreads);
270 * Backward compatibility, trailing number is the count of daemons.
275 set_nfsdcnt(atoi(argv[0]));
278 * Unless the "-o" option was specified, try and run "nfsd".
279 * If "-o" was specified, try and run "nfsserver".
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");
288 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
290 if (errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT)
293 } else if (getnetconfigent("udp6") == NULL ||
294 getnetconfigent("tcp6") == NULL) {
300 if (bindhostc == 0 || bindanyflag) {
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");
312 * Unregister before setting nfs_minvers, in case the
313 * value of vfs.nfsd.server_min_nfsvers has changed
314 * since registering with rpcbind.
320 nfs_minvers_size = sizeof(nfs_minvers);
321 error = sysctlbyname("vfs.nfsd.server_min_nfsvers", &nfs_minvers,
322 &nfs_minvers_size, NULL, 0);
323 if (error != 0 || nfs_minvers < NFS_VER2 || nfs_minvers > NFS_VER4) {
324 warnx("sysctlbyname(vfs.nfsd.server_min_nfsvers) failed,"
325 " defaulting to NFSv2");
326 nfs_minvers = NFS_VER2;
331 memset(&hints, 0, sizeof hints);
332 hints.ai_flags = AI_PASSIVE;
333 hints.ai_family = AF_INET;
334 hints.ai_socktype = SOCK_DGRAM;
335 hints.ai_protocol = IPPROTO_UDP;
336 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
338 err(1, "getaddrinfo udp: %s", gai_strerror(ecode));
339 nconf_udp = getnetconfigent("udp");
340 if (nconf_udp == NULL)
341 err(1, "getnetconfigent udp failed");
342 nb_udp.buf = ai_udp->ai_addr;
343 nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
344 if (nfs_minvers == NFS_VER2)
345 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp,
347 err(1, "rpcb_set udp failed");
348 if (nfs_minvers <= NFS_VER3)
349 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp,
351 err(1, "rpcb_set udp failed");
352 freeaddrinfo(ai_udp);
354 if (udpflag && ip6flag) {
355 memset(&hints, 0, sizeof hints);
356 hints.ai_flags = AI_PASSIVE;
357 hints.ai_family = AF_INET6;
358 hints.ai_socktype = SOCK_DGRAM;
359 hints.ai_protocol = IPPROTO_UDP;
360 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
362 err(1, "getaddrinfo udp6: %s", gai_strerror(ecode));
363 nconf_udp6 = getnetconfigent("udp6");
364 if (nconf_udp6 == NULL)
365 err(1, "getnetconfigent udp6 failed");
366 nb_udp6.buf = ai_udp6->ai_addr;
367 nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
368 if (nfs_minvers == NFS_VER2)
369 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp6,
371 err(1, "rpcb_set udp6 failed");
372 if (nfs_minvers <= NFS_VER3)
373 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6,
375 err(1, "rpcb_set udp6 failed");
376 freeaddrinfo(ai_udp6);
379 memset(&hints, 0, sizeof hints);
380 hints.ai_flags = AI_PASSIVE;
381 hints.ai_family = AF_INET;
382 hints.ai_socktype = SOCK_STREAM;
383 hints.ai_protocol = IPPROTO_TCP;
384 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp);
386 err(1, "getaddrinfo tcp: %s", gai_strerror(ecode));
387 nconf_tcp = getnetconfigent("tcp");
388 if (nconf_tcp == NULL)
389 err(1, "getnetconfigent tcp failed");
390 nb_tcp.buf = ai_tcp->ai_addr;
391 nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
392 if (nfs_minvers == NFS_VER2)
393 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp,
395 err(1, "rpcb_set tcp failed");
396 if (nfs_minvers <= NFS_VER3)
397 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp,
399 err(1, "rpcb_set tcp failed");
400 freeaddrinfo(ai_tcp);
402 if (tcpflag && ip6flag) {
403 memset(&hints, 0, sizeof hints);
404 hints.ai_flags = AI_PASSIVE;
405 hints.ai_family = AF_INET6;
406 hints.ai_socktype = SOCK_STREAM;
407 hints.ai_protocol = IPPROTO_TCP;
408 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
410 err(1, "getaddrinfo tcp6: %s", gai_strerror(ecode));
411 nconf_tcp6 = getnetconfigent("tcp6");
412 if (nconf_tcp6 == NULL)
413 err(1, "getnetconfigent tcp6 failed");
414 nb_tcp6.buf = ai_tcp6->ai_addr;
415 nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
416 if (nfs_minvers == NFS_VER2)
417 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6,
419 err(1, "rpcb_set tcp6 failed");
420 if (nfs_minvers <= NFS_VER3)
421 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6,
423 err(1, "rpcb_set tcp6 failed");
424 freeaddrinfo(ai_tcp6);
430 (void)signal(SIGHUP, SIG_IGN);
431 (void)signal(SIGINT, SIG_IGN);
433 * nfsd sits in the kernel most of the time. It needs
434 * to ignore SIGTERM/SIGQUIT in order to stay alive as long
435 * as possible during a shutdown, otherwise loopback
436 * mounts will not be able to unmount.
438 (void)signal(SIGTERM, SIG_IGN);
439 (void)signal(SIGQUIT, SIG_IGN);
441 (void)signal(SIGSYS, nonfs);
442 (void)signal(SIGCHLD, reapchild);
443 (void)signal(SIGUSR2, backup_stable);
445 openlog("nfsd", LOG_PID | (debug ? LOG_PERROR : 0), LOG_DAEMON);
448 * For V4, we open the stablerestart file and call nfssvc()
449 * to get it loaded. This is done before the daemons do the
450 * regular nfssvc() call to service NFS requests.
451 * (This way the file remains open until the last nfsd is killed
453 * It and the backup copy will be created as empty files
454 * the first time this nfsd is started and should never be
455 * deleted/replaced if at all possible. It should live on a
456 * local, non-volatile storage device that does not do hardware
457 * level write-back caching. (See SCSI doc for more information
458 * on how to prevent write-back caching on SCSI disks.)
460 open_stable(&stablefd, &backupfd);
462 syslog(LOG_ERR, "Can't open %s: %m\n", NFSD_STABLERESTART);
465 /* This system call will fail for old kernels, but that's ok. */
466 nfssvc(NFSSVC_BACKUPSTABLE, NULL);
467 if (nfssvc(NFSSVC_STABLERESTART, (caddr_t)&stablefd) < 0) {
468 if (errno == EPERM) {
470 jailed_size = sizeof(jailed);
471 sysctlbyname("security.jail.jailed", &jailed,
472 &jailed_size, NULL, 0);
474 syslog(LOG_ERR, "nfssvc stablerestart failed: "
475 "allow.nfsd might not be configured");
477 syslog(LOG_ERR, "nfssvc stablerestart failed");
478 } else if (errno == ENXIO)
479 syslog(LOG_ERR, "nfssvc stablerestart failed: is nfsd "
482 syslog(LOG_ERR, "Can't read stable storage file: %m\n");
485 nfssvc_addsock = NFSSVC_NFSDADDSOCK;
486 nfssvc_nfsd = NFSSVC_NFSDNFSD | NFSSVC_NEWSTRUCT;
490 * For TCP mode, we fork once to start the first
491 * kernel nfsd thread. The kernel will add more
494 masterpid = getpid();
497 syslog(LOG_ERR, "fork: %m");
503 (void)signal(SIGUSR1, child_cleanup);
504 setproctitle("server");
505 start_server(0, &nfsdargs, vhostname);
509 (void)signal(SIGUSR1, cleanup);
513 /* Set up the socket for udp and rpcb register it. */
516 for (i = 0; i < bindhostc; i++) {
517 memset(&hints, 0, sizeof hints);
518 hints.ai_flags = AI_PASSIVE;
519 hints.ai_family = AF_INET;
520 hints.ai_socktype = SOCK_DGRAM;
521 hints.ai_protocol = IPPROTO_UDP;
522 if (setbindhost(&ai_udp, bindhost[i], hints) == 0) {
525 if ((sock = socket(ai_udp->ai_family,
527 ai_udp->ai_protocol)) < 0) {
529 "can't create udp socket");
532 if (bind(sock, ai_udp->ai_addr,
533 ai_udp->ai_addrlen) < 0) {
535 "can't bind udp addr %s: %m",
539 freeaddrinfo(ai_udp);
540 addsockargs.sock = sock;
541 addsockargs.name = NULL;
542 addsockargs.namelen = 0;
543 if (nfssvc(nfssvc_addsock, &addsockargs) < 0) {
544 syslog(LOG_ERR, "can't Add UDP socket");
551 memset(&hints, 0, sizeof hints);
552 hints.ai_flags = AI_PASSIVE;
553 hints.ai_family = AF_INET;
554 hints.ai_socktype = SOCK_DGRAM;
555 hints.ai_protocol = IPPROTO_UDP;
556 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
558 syslog(LOG_ERR, "getaddrinfo udp: %s",
559 gai_strerror(ecode));
562 nconf_udp = getnetconfigent("udp");
563 if (nconf_udp == NULL)
564 err(1, "getnetconfigent udp failed");
565 nb_udp.buf = ai_udp->ai_addr;
566 nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
567 if (nfs_minvers == NFS_VER2)
568 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp,
570 err(1, "rpcb_set udp failed");
571 if (nfs_minvers <= NFS_VER3)
572 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp,
574 err(1, "rpcb_set udp failed");
575 freeaddrinfo(ai_udp);
579 /* Set up the socket for udp6 and rpcb register it. */
580 if (udpflag && ip6flag) {
582 for (i = 0; i < bindhostc; i++) {
583 memset(&hints, 0, sizeof hints);
584 hints.ai_flags = AI_PASSIVE;
585 hints.ai_family = AF_INET6;
586 hints.ai_socktype = SOCK_DGRAM;
587 hints.ai_protocol = IPPROTO_UDP;
588 if (setbindhost(&ai_udp6, bindhost[i], hints) == 0) {
591 if ((sock = socket(ai_udp6->ai_family,
592 ai_udp6->ai_socktype,
593 ai_udp6->ai_protocol)) < 0) {
595 "can't create udp6 socket");
598 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
599 &on, sizeof on) < 0) {
601 "can't set v6-only binding for "
605 if (bind(sock, ai_udp6->ai_addr,
606 ai_udp6->ai_addrlen) < 0) {
608 "can't bind udp6 addr %s: %m",
612 freeaddrinfo(ai_udp6);
613 addsockargs.sock = sock;
614 addsockargs.name = NULL;
615 addsockargs.namelen = 0;
616 if (nfssvc(nfssvc_addsock, &addsockargs) < 0) {
618 "can't add UDP6 socket");
625 memset(&hints, 0, sizeof hints);
626 hints.ai_flags = AI_PASSIVE;
627 hints.ai_family = AF_INET6;
628 hints.ai_socktype = SOCK_DGRAM;
629 hints.ai_protocol = IPPROTO_UDP;
630 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
632 syslog(LOG_ERR, "getaddrinfo udp6: %s",
633 gai_strerror(ecode));
636 nconf_udp6 = getnetconfigent("udp6");
637 if (nconf_udp6 == NULL)
638 err(1, "getnetconfigent udp6 failed");
639 nb_udp6.buf = ai_udp6->ai_addr;
640 nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
641 if (nfs_minvers == NFS_VER2)
642 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp6,
645 "rpcb_set udp6 failed");
646 if (nfs_minvers <= NFS_VER3)
647 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6,
650 "rpcb_set udp6 failed");
651 freeaddrinfo(ai_udp6);
655 /* Set up the socket for tcp and rpcb register it. */
658 for (i = 0; i < bindhostc; i++) {
659 memset(&hints, 0, sizeof hints);
660 hints.ai_flags = AI_PASSIVE;
661 hints.ai_family = AF_INET;
662 hints.ai_socktype = SOCK_STREAM;
663 hints.ai_protocol = IPPROTO_TCP;
664 if (setbindhost(&ai_tcp, bindhost[i], hints) == 0) {
667 if ((tcpsock = socket(AF_INET, SOCK_STREAM,
670 "can't create tcp socket");
673 if (setsockopt(tcpsock, SOL_SOCKET,
675 (char *)&on, sizeof(on)) < 0)
677 "setsockopt SO_REUSEADDR: %m");
678 if (bind(tcpsock, ai_tcp->ai_addr,
679 ai_tcp->ai_addrlen) < 0) {
681 "can't bind tcp addr %s: %m",
685 if (listen(tcpsock, -1) < 0) {
686 syslog(LOG_ERR, "listen failed");
689 freeaddrinfo(ai_tcp);
690 FD_SET(tcpsock, &sockbits);
696 memset(&hints, 0, sizeof hints);
697 hints.ai_flags = AI_PASSIVE;
698 hints.ai_family = AF_INET;
699 hints.ai_socktype = SOCK_STREAM;
700 hints.ai_protocol = IPPROTO_TCP;
701 ecode = getaddrinfo(NULL, "nfs", &hints,
704 syslog(LOG_ERR, "getaddrinfo tcp: %s",
705 gai_strerror(ecode));
708 nconf_tcp = getnetconfigent("tcp");
709 if (nconf_tcp == NULL)
710 err(1, "getnetconfigent tcp failed");
711 nb_tcp.buf = ai_tcp->ai_addr;
712 nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
713 if (nfs_minvers == NFS_VER2)
714 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp,
716 err(1, "rpcb_set tcp failed");
717 if (nfs_minvers <= NFS_VER3)
718 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp,
720 err(1, "rpcb_set tcp failed");
721 freeaddrinfo(ai_tcp);
725 /* Set up the socket for tcp6 and rpcb register it. */
726 if (tcpflag && ip6flag) {
728 for (i = 0; i < bindhostc; i++) {
729 memset(&hints, 0, sizeof hints);
730 hints.ai_flags = AI_PASSIVE;
731 hints.ai_family = AF_INET6;
732 hints.ai_socktype = SOCK_STREAM;
733 hints.ai_protocol = IPPROTO_TCP;
734 if (setbindhost(&ai_tcp6, bindhost[i], hints) == 0) {
737 if ((tcp6sock = socket(ai_tcp6->ai_family,
738 ai_tcp6->ai_socktype,
739 ai_tcp6->ai_protocol)) < 0) {
741 "can't create tcp6 socket");
744 if (setsockopt(tcp6sock, SOL_SOCKET,
746 (char *)&on, sizeof(on)) < 0)
748 "setsockopt SO_REUSEADDR: %m");
749 if (setsockopt(tcp6sock, IPPROTO_IPV6,
750 IPV6_V6ONLY, &on, sizeof on) < 0) {
752 "can't set v6-only binding for tcp6 "
756 if (bind(tcp6sock, ai_tcp6->ai_addr,
757 ai_tcp6->ai_addrlen) < 0) {
759 "can't bind tcp6 addr %s: %m",
763 if (listen(tcp6sock, -1) < 0) {
764 syslog(LOG_ERR, "listen failed");
767 freeaddrinfo(ai_tcp6);
768 FD_SET(tcp6sock, &sockbits);
769 if (maxsock < tcp6sock)
775 memset(&hints, 0, sizeof hints);
776 hints.ai_flags = AI_PASSIVE;
777 hints.ai_family = AF_INET6;
778 hints.ai_socktype = SOCK_STREAM;
779 hints.ai_protocol = IPPROTO_TCP;
780 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
782 syslog(LOG_ERR, "getaddrinfo tcp6: %s",
783 gai_strerror(ecode));
786 nconf_tcp6 = getnetconfigent("tcp6");
787 if (nconf_tcp6 == NULL)
788 err(1, "getnetconfigent tcp6 failed");
789 nb_tcp6.buf = ai_tcp6->ai_addr;
790 nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
791 if (nfs_minvers == NFS_VER2)
792 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6,
794 err(1, "rpcb_set tcp6 failed");
795 if (nfs_minvers <= NFS_VER3)
796 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6,
798 err(1, "rpcb_set tcp6 failed");
799 freeaddrinfo(ai_tcp6);
803 if (rpcbregcnt == 0) {
804 syslog(LOG_ERR, "rpcb_set() failed, nothing to do: %m");
808 if (tcpflag && connect_type_cnt == 0) {
809 syslog(LOG_ERR, "tcp connects == 0, nothing to do: %m");
813 setproctitle("master");
815 * We always want a master to have a clean way to shut nfsd down
816 * (with unregistration): if the master is killed, it unregisters and
817 * kills all children. If we run for UDP only (and so do not have to
818 * loop waiting for accept), we instead make the parent
819 * a "server" too. start_server will not return.
822 start_server(1, &nfsdargs, vhostname);
825 * Loop forever accepting connections and passing the sockets
826 * into the kernel for the mounts.
830 if (connect_type_cnt > 1) {
831 if (select(maxsock + 1,
832 &ready, NULL, NULL, NULL) < 1) {
836 syslog(LOG_ERR, "select failed: %m");
840 for (tcpsock = 0; tcpsock <= maxsock; tcpsock++) {
841 if (FD_ISSET(tcpsock, &ready)) {
843 if ((msgsock = accept(tcpsock,
844 (struct sockaddr *)&peer, &len)) < 0) {
846 syslog(LOG_ERR, "accept failed: %m");
847 if (error == ECONNABORTED ||
852 if (setsockopt(msgsock, SOL_SOCKET,
853 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
855 "setsockopt SO_KEEPALIVE: %m");
856 addsockargs.sock = msgsock;
857 addsockargs.name = (caddr_t)&peer;
858 addsockargs.namelen = len;
859 nfssvc(nfssvc_addsock, &addsockargs);
860 (void)close(msgsock);
867 setbindhost(struct addrinfo **ai, const char *bindhost, struct addrinfo hints)
870 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
873 if (bindhost == NULL || strcmp("*", bindhost) == 0)
878 if (hostptr != NULL) {
879 switch (hints.ai_family) {
881 if (inet_pton(AF_INET, hostptr, host_addr) == 1) {
882 hints.ai_flags = AI_NUMERICHOST;
884 if (inet_pton(AF_INET6, hostptr,
890 if (inet_pton(AF_INET6, hostptr, host_addr) == 1) {
891 hints.ai_flags = AI_NUMERICHOST;
893 if (inet_pton(AF_INET, hostptr,
903 ecode = getaddrinfo(hostptr, "nfs", &hints, ai);
905 syslog(LOG_ERR, "getaddrinfo %s: %s", bindhost,
906 gai_strerror(ecode));
913 set_nfsdcnt(int proposed)
917 warnx("nfsd count too low %d; reset to %d", proposed,
919 nfsdcnt = DEFNFSDCNT;
920 } else if (proposed > MAXNFSDCNT) {
921 warnx("nfsd count too high %d; truncated to %d", proposed,
923 nfsdcnt = MAXNFSDCNT;
932 (void)fprintf(stderr, "%s", getopt_usage);
937 nonfs(__unused int signo)
939 syslog(LOG_ERR, "missing system call: NFS not available");
943 reapchild(__unused int signo)
948 while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) {
949 for (i = 0; i < nfsdcnt; i++)
950 if (pid == children[i])
958 if ((nfs_minvers == NFS_VER2 && !rpcb_unset(NFS_PROGRAM, 2, NULL)) ||
959 (nfs_minvers <= NFS_VER3 && !rpcb_unset(NFS_PROGRAM, 3, NULL)))
960 syslog(LOG_ERR, "rpcb_unset failed");
968 for (i = 0; i < nfsdcnt; i++) {
970 kill(children[i], SIGKILL);
975 * Cleanup master after SIGUSR1.
978 cleanup(__unused int signo)
984 * Cleanup child after SIGUSR1.
987 child_cleanup(__unused int signo)
993 nfsd_exit(int status)
1001 get_tuned_nfsdcount(void)
1003 int ncpu, error, tuned_nfsdcnt;
1006 ncpu_size = sizeof(ncpu);
1007 error = sysctlbyname("hw.ncpu", &ncpu, &ncpu_size, NULL, 0);
1009 warnx("sysctlbyname(hw.ncpu) failed defaulting to %d nfs servers",
1011 tuned_nfsdcnt = DEFNFSDCNT;
1013 tuned_nfsdcnt = ncpu * 8;
1015 return tuned_nfsdcnt;
1019 start_server(int master, struct nfsd_nfsd_args *nfsdargp, const char *vhost)
1021 char principal[MAXHOSTNAMELEN + 5];
1023 char hostname[MAXHOSTNAMELEN + 1], *cp;
1024 struct addrinfo *aip, hints;
1028 gethostname(hostname, sizeof (hostname));
1030 strlcpy(hostname, vhost, sizeof (hostname));
1031 snprintf(principal, sizeof (principal), "nfs@%s", hostname);
1032 if ((cp = strchr(hostname, '.')) == NULL ||
1033 *(cp + 1) == '\0') {
1034 /* If not fully qualified, try getaddrinfo() */
1035 memset((void *)&hints, 0, sizeof (hints));
1036 hints.ai_flags = AI_CANONNAME;
1037 error = getaddrinfo(hostname, NULL, &hints, &aip);
1039 if (aip->ai_canonname != NULL &&
1040 (cp = strchr(aip->ai_canonname, '.')) !=
1041 NULL && *(cp + 1) != '\0')
1042 snprintf(principal, sizeof (principal),
1043 "nfs@%s", aip->ai_canonname);
1047 nfsdargp->principal = principal;
1050 nfsdargp->minthreads = nfsdargp->maxthreads = nfsdcnt;
1052 nfsdargp->minthreads = minthreads_set ? minthreads : get_tuned_nfsdcount();
1053 nfsdargp->maxthreads = maxthreads_set ? maxthreads : nfsdargp->minthreads;
1054 if (nfsdargp->maxthreads < nfsdargp->minthreads)
1055 nfsdargp->maxthreads = nfsdargp->minthreads;
1057 error = nfssvc(nfssvc_nfsd, nfsdargp);
1058 if (error < 0 && errno == EAUTH) {
1060 * This indicates that it could not register the
1061 * rpcsec_gss credentials, usually because the
1062 * gssd daemon isn't running.
1063 * (only the experimental server with nfsv4)
1065 syslog(LOG_ERR, "No gssd, using AUTH_SYS only");
1066 principal[0] = '\0';
1067 error = nfssvc(nfssvc_nfsd, nfsdargp);
1070 if (errno == ENXIO) {
1071 syslog(LOG_ERR, "Bad -p option, cannot run");
1072 if (masterpid != 0 && master == 0)
1073 kill(masterpid, SIGUSR1);
1075 syslog(LOG_ERR, "nfssvc: %m");
1085 * Open the stable restart file and return the file descriptor for it.
1088 open_stable(int *stable_fdp, int *backup_fdp)
1090 int stable_fd, backup_fd = -1, ret;
1091 struct stat st, backup_st;
1093 /* Open and stat the stable restart file. */
1094 stable_fd = open(NFSD_STABLERESTART, O_RDWR, 0);
1096 stable_fd = open(NFSD_STABLERESTART, O_RDWR | O_CREAT, 0600);
1097 if (stable_fd >= 0) {
1098 ret = fstat(stable_fd, &st);
1105 /* Open and stat the backup stable restart file. */
1106 if (stable_fd >= 0) {
1107 backup_fd = open(NFSD_STABLEBACKUP, O_RDWR, 0);
1109 backup_fd = open(NFSD_STABLEBACKUP, O_RDWR | O_CREAT,
1111 if (backup_fd >= 0) {
1112 ret = fstat(backup_fd, &backup_st);
1118 if (backup_fd < 0) {
1124 *stable_fdp = stable_fd;
1125 *backup_fdp = backup_fd;
1129 /* Sync up the 2 files, as required. */
1131 copy_stable(stable_fd, backup_fd);
1132 else if (backup_st.st_size > 0)
1133 copy_stable(backup_fd, stable_fd);
1137 * Copy the stable restart file to the backup or vice versa.
1140 copy_stable(int from_fd, int to_fd)
1143 static char buf[1024];
1145 ret = lseek(from_fd, (off_t)0, SEEK_SET);
1147 ret = lseek(to_fd, (off_t)0, SEEK_SET);
1149 ret = ftruncate(to_fd, (off_t)0);
1152 cnt = read(from_fd, buf, 1024);
1154 ret = write(to_fd, buf, cnt);
1157 } while (cnt > 0 && ret >= 0);
1161 syslog(LOG_ERR, "stable restart copy failure: %m");
1165 * Back up the stable restart file when indicated by the kernel.
1168 backup_stable(__unused int signo)
1172 copy_stable(stablefd, backupfd);
1176 * Parse the pNFS string and extract the DS servers and ports numbers.
1179 parse_dsserver(const char *optionarg, struct nfsd_nfsd_args *nfsdargp)
1181 char *cp, *cp2, *dsaddr, *dshost, *dspath, *dsvol, nfsprt[9];
1182 char *mdspath, *mdsp, ip6[INET6_ADDRSTRLEN];
1185 u_int adsiz, dsaddrcnt, dshostcnt, dspathcnt, hostsiz, pathsiz;
1187 size_t dsaddrsiz, dshostsiz, dspathsiz, nfsprtsiz, mdspathsiz;
1188 struct addrinfo hints, *ai_tcp, *res;
1189 struct sockaddr_in sin;
1190 struct sockaddr_in6 sin6;
1192 cp = strdup(optionarg);
1194 errx(1, "Out of memory");
1196 /* Now, do the host names. */
1199 dspath = malloc(dspathsiz);
1201 errx(1, "Out of memory");
1204 dshost = malloc(dshostsiz);
1206 errx(1, "Out of memory");
1209 dsaddr = malloc(dsaddrsiz);
1211 errx(1, "Out of memory");
1214 mdspath = malloc(mdspathsiz);
1215 if (mdspath == NULL)
1216 errx(1, "Out of memory");
1218 /* Put the NFS port# in "." form. */
1219 snprintf(nfsprt, 9, ".%d.%d", 2049 >> 8, 2049 & 0xff);
1220 nfsprtsiz = strlen(nfsprt);
1223 /* Loop around for each DS server name. */
1225 cp2 = strchr(cp, ',');
1227 /* Not the last DS in the list. */
1233 dsvol = strchr(cp, ':');
1234 if (dsvol == NULL || *(dsvol + 1) == '\0')
1238 /* Optional path for MDS file system to be stored on DS. */
1239 mdsp = strchr(dsvol, '#');
1241 if (*(mdsp + 1) == '\0' || mdsp <= dsvol)
1246 /* Append this pathname to dspath. */
1247 pathsiz = strlen(dsvol);
1248 if (dspathcnt + pathsiz + 1 > dspathsiz) {
1250 dspath = realloc(dspath, dspathsiz);
1252 errx(1, "Out of memory");
1254 strcpy(&dspath[dspathcnt], dsvol);
1255 dspathcnt += pathsiz + 1;
1257 /* Append this pathname to mdspath. */
1259 pathsiz = strlen(mdsp);
1262 if (mdspathcnt + pathsiz + 1 > mdspathsiz) {
1264 mdspath = realloc(mdspath, mdspathsiz);
1265 if (mdspath == NULL)
1266 errx(1, "Out of memory");
1269 strcpy(&mdspath[mdspathcnt], mdsp);
1271 mdspath[mdspathcnt] = '\0';
1272 mdspathcnt += pathsiz + 1;
1275 freeaddrinfo(ai_tcp);
1277 /* Get the fully qualified domain name and IP address. */
1278 memset(&hints, 0, sizeof(hints));
1279 hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
1280 hints.ai_family = PF_UNSPEC;
1281 hints.ai_socktype = SOCK_STREAM;
1282 hints.ai_protocol = IPPROTO_TCP;
1283 ecode = getaddrinfo(cp, NULL, &hints, &ai_tcp);
1285 err(1, "getaddrinfo pnfs: %s %s", cp,
1286 gai_strerror(ecode));
1288 for (res = ai_tcp; res != NULL; res = res->ai_next) {
1289 if (res->ai_addr->sa_family == AF_INET) {
1290 if (res->ai_addrlen < sizeof(sin))
1291 err(1, "getaddrinfo() returned "
1292 "undersized IPv4 address");
1294 * Mips cares about sockaddr_in alignment,
1295 * so copy the address.
1297 memcpy(&sin, res->ai_addr, sizeof(sin));
1298 ad = inet_ntoa(sin.sin_addr);
1300 } else if (res->ai_family == AF_INET6) {
1301 if (res->ai_addrlen < sizeof(sin6))
1302 err(1, "getaddrinfo() returned "
1303 "undersized IPv6 address");
1305 * Mips cares about sockaddr_in6 alignment,
1306 * so copy the address.
1308 memcpy(&sin6, res->ai_addr, sizeof(sin6));
1309 ad = inet_ntop(AF_INET6, &sin6.sin6_addr, ip6,
1314 * Since a link local address will only
1315 * work if the client and DS are in the
1316 * same scope zone, only use it if it is
1320 !IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr))
1325 err(1, "No IP address for %s", cp);
1327 /* Append this address to dsaddr. */
1329 if (dsaddrcnt + adsiz + nfsprtsiz + 1 > dsaddrsiz) {
1331 dsaddr = realloc(dsaddr, dsaddrsiz);
1333 errx(1, "Out of memory");
1335 strcpy(&dsaddr[dsaddrcnt], ad);
1336 strcat(&dsaddr[dsaddrcnt], nfsprt);
1337 dsaddrcnt += adsiz + nfsprtsiz + 1;
1339 /* Append this hostname to dshost. */
1340 hostsiz = strlen(ai_tcp->ai_canonname);
1341 if (dshostcnt + hostsiz + 1 > dshostsiz) {
1343 dshost = realloc(dshost, dshostsiz);
1345 errx(1, "Out of memory");
1347 strcpy(&dshost[dshostcnt], ai_tcp->ai_canonname);
1348 dshostcnt += hostsiz + 1;
1351 } while (cp != NULL);
1353 nfsdargp->addr = dsaddr;
1354 nfsdargp->addrlen = dsaddrcnt;
1355 nfsdargp->dnshost = dshost;
1356 nfsdargp->dnshostlen = dshostcnt;
1357 nfsdargp->dspath = dspath;
1358 nfsdargp->dspathlen = dspathcnt;
1359 nfsdargp->mdspath = mdspath;
1360 nfsdargp->mdspathlen = mdspathcnt;
1361 freeaddrinfo(ai_tcp);