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;
105 static int minthreads_set;
106 static int maxthreads_set;
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' },
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 *);
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 *);
135 * Nfs server daemon mostly just a user context for nfssvc()
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
142 * For connectionless protocols, just pass the socket into the kernel via.
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().
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
157 main(int argc, char **argv)
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;
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;
173 int nfs_minvers = NFS_VER2;
174 size_t nfs_minvers_size;
176 char **bindhost = NULL;
178 struct nfsd_nfsd_args nfsdargs;
180 nfsdargs.mirrorcnt = 1;
181 nfsdargs.addr = NULL;
182 nfsdargs.addrlen = 0;
183 nfsdcnt = DEFNFSDCNT;
184 unregister = reregister = tcpflag = maxsock = 0;
185 bindanyflag = udpflag = connect_type_cnt = bindhostc = 0;
186 getopt_shortopts = "ah:n:rdtuep:m:";
189 " nfsd [-ardtue] [-h bindip]\n"
190 " [-n numservers] [--minthreads #] [--maxthreads #]\n"
191 " [-p/--pnfs dsserver0:/dsserver0-mounted-on-dir,...,"
192 "dsserverN:/dsserverN-mounted-on-dir] [-m mirrorlevel]\n";
193 while ((ch = getopt_long(argc, argv, getopt_shortopts, longopts,
200 set_nfsdcnt(atoi(optarg));
204 bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
205 if (bindhost == NULL)
206 errx(1, "Out of memory");
207 bindhost[bindhostc-1] = strdup(optarg);
208 if (bindhost[bindhostc-1] == NULL)
209 errx(1, "Out of memory");
224 /* now a no-op, since this is the default */
227 /* Parse out the DS server host names and mount pts. */
228 parse_dsserver(optarg, &nfsdargs);
231 /* Set the mirror level for a pNFS service. */
233 if (i < 2 || i > NFSDEV_MAXMIRRORS)
234 errx(1, "Mirror level out of range 2<-->%d",
236 nfsdargs.mirrorcnt = i;
239 lopt = longopts[longindex].name;
240 if (!strcmp(lopt, "minthreads")) {
241 minthreads = atoi(optarg);
242 } else if (!strcmp(lopt, "maxthreads")) {
243 maxthreads = atoi(optarg);
250 if (!tcpflag && !udpflag)
254 if (minthreads_set && maxthreads_set && minthreads > maxthreads)
256 "error: minthreads(%d) can't be greater than "
257 "maxthreads(%d)", minthreads, maxthreads);
261 * Backward compatibility, trailing number is the count of daemons.
266 set_nfsdcnt(atoi(argv[0]));
269 * Unless the "-o" option was specified, try and run "nfsd".
270 * If "-o" was specified, try and run "nfsserver".
272 if (modfind("nfsd") < 0) {
273 /* Not present in kernel, try loading it */
274 if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
275 errx(1, "NFS server is not available");
279 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
281 if (errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT)
284 } else if (getnetconfigent("udp6") == NULL ||
285 getnetconfigent("tcp6") == NULL) {
291 if (bindhostc == 0 || bindanyflag) {
293 bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
294 if (bindhost == NULL)
295 errx(1, "Out of memory");
296 bindhost[bindhostc-1] = strdup("*");
297 if (bindhost[bindhostc-1] == NULL)
298 errx(1, "Out of memory");
301 nfs_minvers_size = sizeof(nfs_minvers);
302 error = sysctlbyname("vfs.nfsd.server_min_nfsvers", &nfs_minvers,
303 &nfs_minvers_size, NULL, 0);
304 if (error != 0 || nfs_minvers < NFS_VER2 || nfs_minvers > NFS_VER4) {
305 warnx("sysctlbyname(vfs.nfsd.server_min_nfsvers) failed,"
306 " defaulting to NFSv2");
307 nfs_minvers = NFS_VER2;
316 memset(&hints, 0, sizeof hints);
317 hints.ai_flags = AI_PASSIVE;
318 hints.ai_family = AF_INET;
319 hints.ai_socktype = SOCK_DGRAM;
320 hints.ai_protocol = IPPROTO_UDP;
321 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
323 err(1, "getaddrinfo udp: %s", gai_strerror(ecode));
324 nconf_udp = getnetconfigent("udp");
325 if (nconf_udp == NULL)
326 err(1, "getnetconfigent udp failed");
327 nb_udp.buf = ai_udp->ai_addr;
328 nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
329 if (nfs_minvers == NFS_VER2)
330 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp,
332 err(1, "rpcb_set udp failed");
333 if (nfs_minvers <= NFS_VER3)
334 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp,
336 err(1, "rpcb_set udp failed");
337 freeaddrinfo(ai_udp);
339 if (udpflag && ip6flag) {
340 memset(&hints, 0, sizeof hints);
341 hints.ai_flags = AI_PASSIVE;
342 hints.ai_family = AF_INET6;
343 hints.ai_socktype = SOCK_DGRAM;
344 hints.ai_protocol = IPPROTO_UDP;
345 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
347 err(1, "getaddrinfo udp6: %s", gai_strerror(ecode));
348 nconf_udp6 = getnetconfigent("udp6");
349 if (nconf_udp6 == NULL)
350 err(1, "getnetconfigent udp6 failed");
351 nb_udp6.buf = ai_udp6->ai_addr;
352 nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
353 if (nfs_minvers == NFS_VER2)
354 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp6,
356 err(1, "rpcb_set udp6 failed");
357 if (nfs_minvers <= NFS_VER3)
358 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6,
360 err(1, "rpcb_set udp6 failed");
361 freeaddrinfo(ai_udp6);
364 memset(&hints, 0, sizeof hints);
365 hints.ai_flags = AI_PASSIVE;
366 hints.ai_family = AF_INET;
367 hints.ai_socktype = SOCK_STREAM;
368 hints.ai_protocol = IPPROTO_TCP;
369 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp);
371 err(1, "getaddrinfo tcp: %s", gai_strerror(ecode));
372 nconf_tcp = getnetconfigent("tcp");
373 if (nconf_tcp == NULL)
374 err(1, "getnetconfigent tcp failed");
375 nb_tcp.buf = ai_tcp->ai_addr;
376 nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
377 if (nfs_minvers == NFS_VER2)
378 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp,
380 err(1, "rpcb_set tcp failed");
381 if (nfs_minvers <= NFS_VER3)
382 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp,
384 err(1, "rpcb_set tcp failed");
385 freeaddrinfo(ai_tcp);
387 if (tcpflag && ip6flag) {
388 memset(&hints, 0, sizeof hints);
389 hints.ai_flags = AI_PASSIVE;
390 hints.ai_family = AF_INET6;
391 hints.ai_socktype = SOCK_STREAM;
392 hints.ai_protocol = IPPROTO_TCP;
393 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
395 err(1, "getaddrinfo tcp6: %s", gai_strerror(ecode));
396 nconf_tcp6 = getnetconfigent("tcp6");
397 if (nconf_tcp6 == NULL)
398 err(1, "getnetconfigent tcp6 failed");
399 nb_tcp6.buf = ai_tcp6->ai_addr;
400 nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
401 if (nfs_minvers == NFS_VER2)
402 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6,
404 err(1, "rpcb_set tcp6 failed");
405 if (nfs_minvers <= NFS_VER3)
406 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6,
408 err(1, "rpcb_set tcp6 failed");
409 freeaddrinfo(ai_tcp6);
415 (void)signal(SIGHUP, SIG_IGN);
416 (void)signal(SIGINT, SIG_IGN);
418 * nfsd sits in the kernel most of the time. It needs
419 * to ignore SIGTERM/SIGQUIT in order to stay alive as long
420 * as possible during a shutdown, otherwise loopback
421 * mounts will not be able to unmount.
423 (void)signal(SIGTERM, SIG_IGN);
424 (void)signal(SIGQUIT, SIG_IGN);
426 (void)signal(SIGSYS, nonfs);
427 (void)signal(SIGCHLD, reapchild);
428 (void)signal(SIGUSR2, backup_stable);
430 openlog("nfsd", LOG_PID | (debug ? LOG_PERROR : 0), LOG_DAEMON);
433 * For V4, we open the stablerestart file and call nfssvc()
434 * to get it loaded. This is done before the daemons do the
435 * regular nfssvc() call to service NFS requests.
436 * (This way the file remains open until the last nfsd is killed
438 * It and the backup copy will be created as empty files
439 * the first time this nfsd is started and should never be
440 * deleted/replaced if at all possible. It should live on a
441 * local, non-volatile storage device that does not do hardware
442 * level write-back caching. (See SCSI doc for more information
443 * on how to prevent write-back caching on SCSI disks.)
445 open_stable(&stablefd, &backupfd);
447 syslog(LOG_ERR, "Can't open %s: %m\n", NFSD_STABLERESTART);
450 /* This system call will fail for old kernels, but that's ok. */
451 nfssvc(NFSSVC_BACKUPSTABLE, NULL);
452 if (nfssvc(NFSSVC_STABLERESTART, (caddr_t)&stablefd) < 0) {
453 syslog(LOG_ERR, "Can't read stable storage file: %m\n");
456 nfssvc_addsock = NFSSVC_NFSDADDSOCK;
457 nfssvc_nfsd = NFSSVC_NFSDNFSD | NFSSVC_NEWSTRUCT;
461 * For TCP mode, we fork once to start the first
462 * kernel nfsd thread. The kernel will add more
465 masterpid = getpid();
468 syslog(LOG_ERR, "fork: %m");
474 (void)signal(SIGUSR1, child_cleanup);
475 setproctitle("server");
476 start_server(0, &nfsdargs);
480 (void)signal(SIGUSR1, cleanup);
484 /* Set up the socket for udp and rpcb register it. */
487 for (i = 0; i < bindhostc; i++) {
488 memset(&hints, 0, sizeof hints);
489 hints.ai_flags = AI_PASSIVE;
490 hints.ai_family = AF_INET;
491 hints.ai_socktype = SOCK_DGRAM;
492 hints.ai_protocol = IPPROTO_UDP;
493 if (setbindhost(&ai_udp, bindhost[i], hints) == 0) {
496 if ((sock = socket(ai_udp->ai_family,
498 ai_udp->ai_protocol)) < 0) {
500 "can't create udp socket");
503 if (bind(sock, ai_udp->ai_addr,
504 ai_udp->ai_addrlen) < 0) {
506 "can't bind udp addr %s: %m",
510 freeaddrinfo(ai_udp);
511 addsockargs.sock = sock;
512 addsockargs.name = NULL;
513 addsockargs.namelen = 0;
514 if (nfssvc(nfssvc_addsock, &addsockargs) < 0) {
515 syslog(LOG_ERR, "can't Add UDP socket");
522 memset(&hints, 0, sizeof hints);
523 hints.ai_flags = AI_PASSIVE;
524 hints.ai_family = AF_INET;
525 hints.ai_socktype = SOCK_DGRAM;
526 hints.ai_protocol = IPPROTO_UDP;
527 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
529 syslog(LOG_ERR, "getaddrinfo udp: %s",
530 gai_strerror(ecode));
533 nconf_udp = getnetconfigent("udp");
534 if (nconf_udp == NULL)
535 err(1, "getnetconfigent udp failed");
536 nb_udp.buf = ai_udp->ai_addr;
537 nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
538 if (nfs_minvers == NFS_VER2)
539 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp,
541 err(1, "rpcb_set udp failed");
542 if (nfs_minvers <= NFS_VER3)
543 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp,
545 err(1, "rpcb_set udp failed");
546 freeaddrinfo(ai_udp);
550 /* Set up the socket for udp6 and rpcb register it. */
551 if (udpflag && ip6flag) {
553 for (i = 0; i < bindhostc; i++) {
554 memset(&hints, 0, sizeof hints);
555 hints.ai_flags = AI_PASSIVE;
556 hints.ai_family = AF_INET6;
557 hints.ai_socktype = SOCK_DGRAM;
558 hints.ai_protocol = IPPROTO_UDP;
559 if (setbindhost(&ai_udp6, bindhost[i], hints) == 0) {
562 if ((sock = socket(ai_udp6->ai_family,
563 ai_udp6->ai_socktype,
564 ai_udp6->ai_protocol)) < 0) {
566 "can't create udp6 socket");
569 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
570 &on, sizeof on) < 0) {
572 "can't set v6-only binding for "
576 if (bind(sock, ai_udp6->ai_addr,
577 ai_udp6->ai_addrlen) < 0) {
579 "can't bind udp6 addr %s: %m",
583 freeaddrinfo(ai_udp6);
584 addsockargs.sock = sock;
585 addsockargs.name = NULL;
586 addsockargs.namelen = 0;
587 if (nfssvc(nfssvc_addsock, &addsockargs) < 0) {
589 "can't add UDP6 socket");
596 memset(&hints, 0, sizeof hints);
597 hints.ai_flags = AI_PASSIVE;
598 hints.ai_family = AF_INET6;
599 hints.ai_socktype = SOCK_DGRAM;
600 hints.ai_protocol = IPPROTO_UDP;
601 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
603 syslog(LOG_ERR, "getaddrinfo udp6: %s",
604 gai_strerror(ecode));
607 nconf_udp6 = getnetconfigent("udp6");
608 if (nconf_udp6 == NULL)
609 err(1, "getnetconfigent udp6 failed");
610 nb_udp6.buf = ai_udp6->ai_addr;
611 nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
612 if (nfs_minvers == NFS_VER2)
613 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp6,
616 "rpcb_set udp6 failed");
617 if (nfs_minvers <= NFS_VER3)
618 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6,
621 "rpcb_set udp6 failed");
622 freeaddrinfo(ai_udp6);
626 /* Set up the socket for tcp and rpcb register it. */
629 for (i = 0; i < bindhostc; i++) {
630 memset(&hints, 0, sizeof hints);
631 hints.ai_flags = AI_PASSIVE;
632 hints.ai_family = AF_INET;
633 hints.ai_socktype = SOCK_STREAM;
634 hints.ai_protocol = IPPROTO_TCP;
635 if (setbindhost(&ai_tcp, bindhost[i], hints) == 0) {
638 if ((tcpsock = socket(AF_INET, SOCK_STREAM,
641 "can't create tcp socket");
644 if (setsockopt(tcpsock, SOL_SOCKET,
646 (char *)&on, sizeof(on)) < 0)
648 "setsockopt SO_REUSEADDR: %m");
649 if (bind(tcpsock, ai_tcp->ai_addr,
650 ai_tcp->ai_addrlen) < 0) {
652 "can't bind tcp addr %s: %m",
656 if (listen(tcpsock, -1) < 0) {
657 syslog(LOG_ERR, "listen failed");
660 freeaddrinfo(ai_tcp);
661 FD_SET(tcpsock, &sockbits);
667 memset(&hints, 0, sizeof hints);
668 hints.ai_flags = AI_PASSIVE;
669 hints.ai_family = AF_INET;
670 hints.ai_socktype = SOCK_STREAM;
671 hints.ai_protocol = IPPROTO_TCP;
672 ecode = getaddrinfo(NULL, "nfs", &hints,
675 syslog(LOG_ERR, "getaddrinfo tcp: %s",
676 gai_strerror(ecode));
679 nconf_tcp = getnetconfigent("tcp");
680 if (nconf_tcp == NULL)
681 err(1, "getnetconfigent tcp failed");
682 nb_tcp.buf = ai_tcp->ai_addr;
683 nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
684 if (nfs_minvers == NFS_VER2)
685 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp,
687 err(1, "rpcb_set tcp failed");
688 if (nfs_minvers <= NFS_VER3)
689 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp,
691 err(1, "rpcb_set tcp failed");
692 freeaddrinfo(ai_tcp);
696 /* Set up the socket for tcp6 and rpcb register it. */
697 if (tcpflag && ip6flag) {
699 for (i = 0; i < bindhostc; i++) {
700 memset(&hints, 0, sizeof hints);
701 hints.ai_flags = AI_PASSIVE;
702 hints.ai_family = AF_INET6;
703 hints.ai_socktype = SOCK_STREAM;
704 hints.ai_protocol = IPPROTO_TCP;
705 if (setbindhost(&ai_tcp6, bindhost[i], hints) == 0) {
708 if ((tcp6sock = socket(ai_tcp6->ai_family,
709 ai_tcp6->ai_socktype,
710 ai_tcp6->ai_protocol)) < 0) {
712 "can't create tcp6 socket");
715 if (setsockopt(tcp6sock, SOL_SOCKET,
717 (char *)&on, sizeof(on)) < 0)
719 "setsockopt SO_REUSEADDR: %m");
720 if (setsockopt(tcp6sock, IPPROTO_IPV6,
721 IPV6_V6ONLY, &on, sizeof on) < 0) {
723 "can't set v6-only binding for tcp6 "
727 if (bind(tcp6sock, ai_tcp6->ai_addr,
728 ai_tcp6->ai_addrlen) < 0) {
730 "can't bind tcp6 addr %s: %m",
734 if (listen(tcp6sock, -1) < 0) {
735 syslog(LOG_ERR, "listen failed");
738 freeaddrinfo(ai_tcp6);
739 FD_SET(tcp6sock, &sockbits);
740 if (maxsock < tcp6sock)
746 memset(&hints, 0, sizeof hints);
747 hints.ai_flags = AI_PASSIVE;
748 hints.ai_family = AF_INET6;
749 hints.ai_socktype = SOCK_STREAM;
750 hints.ai_protocol = IPPROTO_TCP;
751 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
753 syslog(LOG_ERR, "getaddrinfo tcp6: %s",
754 gai_strerror(ecode));
757 nconf_tcp6 = getnetconfigent("tcp6");
758 if (nconf_tcp6 == NULL)
759 err(1, "getnetconfigent tcp6 failed");
760 nb_tcp6.buf = ai_tcp6->ai_addr;
761 nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
762 if (nfs_minvers == NFS_VER2)
763 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6,
765 err(1, "rpcb_set tcp6 failed");
766 if (nfs_minvers <= NFS_VER3)
767 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6,
769 err(1, "rpcb_set tcp6 failed");
770 freeaddrinfo(ai_tcp6);
774 if (rpcbregcnt == 0) {
775 syslog(LOG_ERR, "rpcb_set() failed, nothing to do: %m");
779 if (tcpflag && connect_type_cnt == 0) {
780 syslog(LOG_ERR, "tcp connects == 0, nothing to do: %m");
784 setproctitle("master");
786 * We always want a master to have a clean way to shut nfsd down
787 * (with unregistration): if the master is killed, it unregisters and
788 * kills all children. If we run for UDP only (and so do not have to
789 * loop waiting for accept), we instead make the parent
790 * a "server" too. start_server will not return.
793 start_server(1, &nfsdargs);
796 * Loop forever accepting connections and passing the sockets
797 * into the kernel for the mounts.
801 if (connect_type_cnt > 1) {
802 if (select(maxsock + 1,
803 &ready, NULL, NULL, NULL) < 1) {
807 syslog(LOG_ERR, "select failed: %m");
811 for (tcpsock = 0; tcpsock <= maxsock; tcpsock++) {
812 if (FD_ISSET(tcpsock, &ready)) {
814 if ((msgsock = accept(tcpsock,
815 (struct sockaddr *)&peer, &len)) < 0) {
817 syslog(LOG_ERR, "accept failed: %m");
818 if (error == ECONNABORTED ||
823 if (setsockopt(msgsock, SOL_SOCKET,
824 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
826 "setsockopt SO_KEEPALIVE: %m");
827 addsockargs.sock = msgsock;
828 addsockargs.name = (caddr_t)&peer;
829 addsockargs.namelen = len;
830 nfssvc(nfssvc_addsock, &addsockargs);
831 (void)close(msgsock);
838 setbindhost(struct addrinfo **ai, const char *bindhost, struct addrinfo hints)
841 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
844 if (bindhost == NULL || strcmp("*", bindhost) == 0)
849 if (hostptr != NULL) {
850 switch (hints.ai_family) {
852 if (inet_pton(AF_INET, hostptr, host_addr) == 1) {
853 hints.ai_flags = AI_NUMERICHOST;
855 if (inet_pton(AF_INET6, hostptr,
861 if (inet_pton(AF_INET6, hostptr, host_addr) == 1) {
862 hints.ai_flags = AI_NUMERICHOST;
864 if (inet_pton(AF_INET, hostptr,
874 ecode = getaddrinfo(hostptr, "nfs", &hints, ai);
876 syslog(LOG_ERR, "getaddrinfo %s: %s", bindhost,
877 gai_strerror(ecode));
884 set_nfsdcnt(int proposed)
888 warnx("nfsd count too low %d; reset to %d", proposed,
890 nfsdcnt = DEFNFSDCNT;
891 } else if (proposed > MAXNFSDCNT) {
892 warnx("nfsd count too high %d; truncated to %d", proposed,
894 nfsdcnt = MAXNFSDCNT;
903 (void)fprintf(stderr, "%s", getopt_usage);
908 nonfs(__unused int signo)
910 syslog(LOG_ERR, "missing system call: NFS not available");
914 reapchild(__unused int signo)
919 while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) {
920 for (i = 0; i < nfsdcnt; i++)
921 if (pid == children[i])
929 if ((!rpcb_unset(NFS_PROGRAM, 2, NULL)) ||
930 (!rpcb_unset(NFS_PROGRAM, 3, NULL)))
931 syslog(LOG_ERR, "rpcb_unset failed");
939 for (i = 0; i < nfsdcnt; i++) {
941 kill(children[i], SIGKILL);
946 * Cleanup master after SIGUSR1.
949 cleanup(__unused int signo)
955 * Cleanup child after SIGUSR1.
958 child_cleanup(__unused int signo)
964 nfsd_exit(int status)
972 get_tuned_nfsdcount(void)
974 int ncpu, error, tuned_nfsdcnt;
977 ncpu_size = sizeof(ncpu);
978 error = sysctlbyname("hw.ncpu", &ncpu, &ncpu_size, NULL, 0);
980 warnx("sysctlbyname(hw.ncpu) failed defaulting to %d nfs servers",
982 tuned_nfsdcnt = DEFNFSDCNT;
984 tuned_nfsdcnt = ncpu * 8;
986 return tuned_nfsdcnt;
990 start_server(int master, struct nfsd_nfsd_args *nfsdargp)
992 char principal[MAXHOSTNAMELEN + 5];
994 char hostname[MAXHOSTNAMELEN + 1], *cp;
995 struct addrinfo *aip, hints;
998 gethostname(hostname, sizeof (hostname));
999 snprintf(principal, sizeof (principal), "nfs@%s", hostname);
1000 if ((cp = strchr(hostname, '.')) == NULL ||
1001 *(cp + 1) == '\0') {
1002 /* If not fully qualified, try getaddrinfo() */
1003 memset((void *)&hints, 0, sizeof (hints));
1004 hints.ai_flags = AI_CANONNAME;
1005 error = getaddrinfo(hostname, NULL, &hints, &aip);
1007 if (aip->ai_canonname != NULL &&
1008 (cp = strchr(aip->ai_canonname, '.')) !=
1009 NULL && *(cp + 1) != '\0')
1010 snprintf(principal, sizeof (principal),
1011 "nfs@%s", aip->ai_canonname);
1015 nfsdargp->principal = principal;
1018 nfsdargp->minthreads = nfsdargp->maxthreads = nfsdcnt;
1020 nfsdargp->minthreads = minthreads_set ? minthreads : get_tuned_nfsdcount();
1021 nfsdargp->maxthreads = maxthreads_set ? maxthreads : nfsdargp->minthreads;
1022 if (nfsdargp->maxthreads < nfsdargp->minthreads)
1023 nfsdargp->maxthreads = nfsdargp->minthreads;
1025 error = nfssvc(nfssvc_nfsd, nfsdargp);
1026 if (error < 0 && errno == EAUTH) {
1028 * This indicates that it could not register the
1029 * rpcsec_gss credentials, usually because the
1030 * gssd daemon isn't running.
1031 * (only the experimental server with nfsv4)
1033 syslog(LOG_ERR, "No gssd, using AUTH_SYS only");
1034 principal[0] = '\0';
1035 error = nfssvc(nfssvc_nfsd, nfsdargp);
1038 if (errno == ENXIO) {
1039 syslog(LOG_ERR, "Bad -p option, cannot run");
1040 if (masterpid != 0 && master == 0)
1041 kill(masterpid, SIGUSR1);
1043 syslog(LOG_ERR, "nfssvc: %m");
1053 * Open the stable restart file and return the file descriptor for it.
1056 open_stable(int *stable_fdp, int *backup_fdp)
1058 int stable_fd, backup_fd = -1, ret;
1059 struct stat st, backup_st;
1061 /* Open and stat the stable restart file. */
1062 stable_fd = open(NFSD_STABLERESTART, O_RDWR, 0);
1064 stable_fd = open(NFSD_STABLERESTART, O_RDWR | O_CREAT, 0600);
1065 if (stable_fd >= 0) {
1066 ret = fstat(stable_fd, &st);
1073 /* Open and stat the backup stable restart file. */
1074 if (stable_fd >= 0) {
1075 backup_fd = open(NFSD_STABLEBACKUP, O_RDWR, 0);
1077 backup_fd = open(NFSD_STABLEBACKUP, O_RDWR | O_CREAT,
1079 if (backup_fd >= 0) {
1080 ret = fstat(backup_fd, &backup_st);
1086 if (backup_fd < 0) {
1092 *stable_fdp = stable_fd;
1093 *backup_fdp = backup_fd;
1097 /* Sync up the 2 files, as required. */
1099 copy_stable(stable_fd, backup_fd);
1100 else if (backup_st.st_size > 0)
1101 copy_stable(backup_fd, stable_fd);
1105 * Copy the stable restart file to the backup or vice versa.
1108 copy_stable(int from_fd, int to_fd)
1111 static char buf[1024];
1113 ret = lseek(from_fd, (off_t)0, SEEK_SET);
1115 ret = lseek(to_fd, (off_t)0, SEEK_SET);
1117 ret = ftruncate(to_fd, (off_t)0);
1120 cnt = read(from_fd, buf, 1024);
1122 ret = write(to_fd, buf, cnt);
1125 } while (cnt > 0 && ret >= 0);
1129 syslog(LOG_ERR, "stable restart copy failure: %m");
1133 * Back up the stable restart file when indicated by the kernel.
1136 backup_stable(__unused int signo)
1140 copy_stable(stablefd, backupfd);
1144 * Parse the pNFS string and extract the DS servers and ports numbers.
1147 parse_dsserver(const char *optionarg, struct nfsd_nfsd_args *nfsdargp)
1149 char *cp, *cp2, *dsaddr, *dshost, *dspath, *dsvol, nfsprt[9];
1150 char *mdspath, *mdsp, ip6[INET6_ADDRSTRLEN];
1153 u_int adsiz, dsaddrcnt, dshostcnt, dspathcnt, hostsiz, pathsiz;
1155 size_t dsaddrsiz, dshostsiz, dspathsiz, nfsprtsiz, mdspathsiz;
1156 struct addrinfo hints, *ai_tcp, *res;
1157 struct sockaddr_in sin;
1158 struct sockaddr_in6 sin6;
1160 cp = strdup(optionarg);
1162 errx(1, "Out of memory");
1164 /* Now, do the host names. */
1167 dspath = malloc(dspathsiz);
1169 errx(1, "Out of memory");
1172 dshost = malloc(dshostsiz);
1174 errx(1, "Out of memory");
1177 dsaddr = malloc(dsaddrsiz);
1179 errx(1, "Out of memory");
1182 mdspath = malloc(mdspathsiz);
1183 if (mdspath == NULL)
1184 errx(1, "Out of memory");
1186 /* Put the NFS port# in "." form. */
1187 snprintf(nfsprt, 9, ".%d.%d", 2049 >> 8, 2049 & 0xff);
1188 nfsprtsiz = strlen(nfsprt);
1191 /* Loop around for each DS server name. */
1193 cp2 = strchr(cp, ',');
1195 /* Not the last DS in the list. */
1201 dsvol = strchr(cp, ':');
1202 if (dsvol == NULL || *(dsvol + 1) == '\0')
1206 /* Optional path for MDS file system to be stored on DS. */
1207 mdsp = strchr(dsvol, '#');
1209 if (*(mdsp + 1) == '\0' || mdsp <= dsvol)
1214 /* Append this pathname to dspath. */
1215 pathsiz = strlen(dsvol);
1216 if (dspathcnt + pathsiz + 1 > dspathsiz) {
1218 dspath = realloc(dspath, dspathsiz);
1220 errx(1, "Out of memory");
1222 strcpy(&dspath[dspathcnt], dsvol);
1223 dspathcnt += pathsiz + 1;
1225 /* Append this pathname to mdspath. */
1227 pathsiz = strlen(mdsp);
1230 if (mdspathcnt + pathsiz + 1 > mdspathsiz) {
1232 mdspath = realloc(mdspath, mdspathsiz);
1233 if (mdspath == NULL)
1234 errx(1, "Out of memory");
1237 strcpy(&mdspath[mdspathcnt], mdsp);
1239 mdspath[mdspathcnt] = '\0';
1240 mdspathcnt += pathsiz + 1;
1243 freeaddrinfo(ai_tcp);
1245 /* Get the fully qualified domain name and IP address. */
1246 memset(&hints, 0, sizeof(hints));
1247 hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
1248 hints.ai_family = PF_UNSPEC;
1249 hints.ai_socktype = SOCK_STREAM;
1250 hints.ai_protocol = IPPROTO_TCP;
1251 ecode = getaddrinfo(cp, NULL, &hints, &ai_tcp);
1253 err(1, "getaddrinfo pnfs: %s %s", cp,
1254 gai_strerror(ecode));
1256 for (res = ai_tcp; res != NULL; res = res->ai_next) {
1257 if (res->ai_addr->sa_family == AF_INET) {
1258 if (res->ai_addrlen < sizeof(sin))
1259 err(1, "getaddrinfo() returned "
1260 "undersized IPv4 address");
1262 * Mips cares about sockaddr_in alignment,
1263 * so copy the address.
1265 memcpy(&sin, res->ai_addr, sizeof(sin));
1266 ad = inet_ntoa(sin.sin_addr);
1268 } else if (res->ai_family == AF_INET6) {
1269 if (res->ai_addrlen < sizeof(sin6))
1270 err(1, "getaddrinfo() returned "
1271 "undersized IPv6 address");
1273 * Mips cares about sockaddr_in6 alignment,
1274 * so copy the address.
1276 memcpy(&sin6, res->ai_addr, sizeof(sin6));
1277 ad = inet_ntop(AF_INET6, &sin6.sin6_addr, ip6,
1282 * Since a link local address will only
1283 * work if the client and DS are in the
1284 * same scope zone, only use it if it is
1288 !IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr))
1293 err(1, "No IP address for %s", cp);
1295 /* Append this address to dsaddr. */
1297 if (dsaddrcnt + adsiz + nfsprtsiz + 1 > dsaddrsiz) {
1299 dsaddr = realloc(dsaddr, dsaddrsiz);
1301 errx(1, "Out of memory");
1303 strcpy(&dsaddr[dsaddrcnt], ad);
1304 strcat(&dsaddr[dsaddrcnt], nfsprt);
1305 dsaddrcnt += adsiz + nfsprtsiz + 1;
1307 /* Append this hostname to dshost. */
1308 hostsiz = strlen(ai_tcp->ai_canonname);
1309 if (dshostcnt + hostsiz + 1 > dshostsiz) {
1311 dshost = realloc(dshost, dshostsiz);
1313 errx(1, "Out of memory");
1315 strcpy(&dshost[dshostcnt], ai_tcp->ai_canonname);
1316 dshostcnt += hostsiz + 1;
1319 } while (cp != NULL);
1321 nfsdargp->addr = dsaddr;
1322 nfsdargp->addrlen = dsaddrcnt;
1323 nfsdargp->dnshost = dshost;
1324 nfsdargp->dnshostlen = dshostcnt;
1325 nfsdargp->dspath = dspath;
1326 nfsdargp->dspathlen = dspathcnt;
1327 nfsdargp->mdspath = mdspath;
1328 nfsdargp->mdspathlen = mdspathcnt;
1329 freeaddrinfo(ai_tcp);