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_in inetpeer;
164 struct sockaddr_in6 inet6peer;
165 fd_set ready, sockbits;
166 fd_set v4bits, v6bits;
167 int ch, connect_type_cnt, i, maxsock, msgsock;
169 int on = 1, unregister, reregister, sock;
170 int tcp6sock, ip6flag, tcpflag, tcpsock;
171 int udpflag, ecode, error, s;
172 int bindhostc, bindanyflag, rpcbreg, rpcbregcnt;
175 int nfs_minvers = NFS_VER2;
176 size_t nfs_minvers_size;
178 char **bindhost = NULL;
180 struct nfsd_nfsd_args nfsdargs;
182 nfsdargs.mirrorcnt = 1;
183 nfsdargs.addr = NULL;
184 nfsdargs.addrlen = 0;
185 nfsdcnt = DEFNFSDCNT;
186 unregister = reregister = tcpflag = maxsock = 0;
187 bindanyflag = udpflag = connect_type_cnt = bindhostc = 0;
188 getopt_shortopts = "ah:n:rdtuep:m:";
191 " nfsd [-ardtue] [-h bindip]\n"
192 " [-n numservers] [--minthreads #] [--maxthreads #]\n"
193 " [-p/--pnfs dsserver0:/dsserver0-mounted-on-dir,...,"
194 "dsserverN:/dsserverN-mounted-on-dir] [-m mirrorlevel]\n";
195 while ((ch = getopt_long(argc, argv, getopt_shortopts, longopts,
202 set_nfsdcnt(atoi(optarg));
206 bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
207 if (bindhost == NULL)
208 errx(1, "Out of memory");
209 bindhost[bindhostc-1] = strdup(optarg);
210 if (bindhost[bindhostc-1] == NULL)
211 errx(1, "Out of memory");
226 /* now a no-op, since this is the default */
229 /* Parse out the DS server host names and mount pts. */
230 parse_dsserver(optarg, &nfsdargs);
233 /* Set the mirror level for a pNFS service. */
235 if (i < 2 || i > NFSDEV_MAXMIRRORS)
236 errx(1, "Mirror level out of range 2<-->%d",
238 nfsdargs.mirrorcnt = i;
241 lopt = longopts[longindex].name;
242 if (!strcmp(lopt, "minthreads")) {
243 minthreads = atoi(optarg);
244 } else if (!strcmp(lopt, "maxthreads")) {
245 maxthreads = atoi(optarg);
252 if (!tcpflag && !udpflag)
256 if (minthreads_set && maxthreads_set && minthreads > maxthreads)
258 "error: minthreads(%d) can't be greater than "
259 "maxthreads(%d)", minthreads, maxthreads);
263 * Backward compatibility, trailing number is the count of daemons.
268 set_nfsdcnt(atoi(argv[0]));
271 * Unless the "-o" option was specified, try and run "nfsd".
272 * If "-o" was specified, try and run "nfsserver".
274 if (modfind("nfsd") < 0) {
275 /* Not present in kernel, try loading it */
276 if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
277 errx(1, "NFS server is not available");
281 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
283 if (errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT)
286 } else if (getnetconfigent("udp6") == NULL ||
287 getnetconfigent("tcp6") == NULL) {
293 if (bindhostc == 0 || bindanyflag) {
295 bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
296 if (bindhost == NULL)
297 errx(1, "Out of memory");
298 bindhost[bindhostc-1] = strdup("*");
299 if (bindhost[bindhostc-1] == NULL)
300 errx(1, "Out of memory");
303 nfs_minvers_size = sizeof(nfs_minvers);
304 error = sysctlbyname("vfs.nfsd.server_min_nfsvers", &nfs_minvers,
305 &nfs_minvers_size, NULL, 0);
306 if (error != 0 || nfs_minvers < NFS_VER2 || nfs_minvers > NFS_VER4) {
307 warnx("sysctlbyname(vfs.nfsd.server_min_nfsvers) failed,"
308 " defaulting to NFSv2");
309 nfs_minvers = NFS_VER2;
318 memset(&hints, 0, sizeof hints);
319 hints.ai_flags = AI_PASSIVE;
320 hints.ai_family = AF_INET;
321 hints.ai_socktype = SOCK_DGRAM;
322 hints.ai_protocol = IPPROTO_UDP;
323 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
325 err(1, "getaddrinfo udp: %s", gai_strerror(ecode));
326 nconf_udp = getnetconfigent("udp");
327 if (nconf_udp == NULL)
328 err(1, "getnetconfigent udp failed");
329 nb_udp.buf = ai_udp->ai_addr;
330 nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
331 if (nfs_minvers == NFS_VER2)
332 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp,
334 err(1, "rpcb_set udp failed");
335 if (nfs_minvers <= NFS_VER3)
336 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp,
338 err(1, "rpcb_set udp failed");
339 freeaddrinfo(ai_udp);
341 if (udpflag && ip6flag) {
342 memset(&hints, 0, sizeof hints);
343 hints.ai_flags = AI_PASSIVE;
344 hints.ai_family = AF_INET6;
345 hints.ai_socktype = SOCK_DGRAM;
346 hints.ai_protocol = IPPROTO_UDP;
347 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
349 err(1, "getaddrinfo udp6: %s", gai_strerror(ecode));
350 nconf_udp6 = getnetconfigent("udp6");
351 if (nconf_udp6 == NULL)
352 err(1, "getnetconfigent udp6 failed");
353 nb_udp6.buf = ai_udp6->ai_addr;
354 nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
355 if (nfs_minvers == NFS_VER2)
356 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp6,
358 err(1, "rpcb_set udp6 failed");
359 if (nfs_minvers <= NFS_VER3)
360 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6,
362 err(1, "rpcb_set udp6 failed");
363 freeaddrinfo(ai_udp6);
366 memset(&hints, 0, sizeof hints);
367 hints.ai_flags = AI_PASSIVE;
368 hints.ai_family = AF_INET;
369 hints.ai_socktype = SOCK_STREAM;
370 hints.ai_protocol = IPPROTO_TCP;
371 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp);
373 err(1, "getaddrinfo tcp: %s", gai_strerror(ecode));
374 nconf_tcp = getnetconfigent("tcp");
375 if (nconf_tcp == NULL)
376 err(1, "getnetconfigent tcp failed");
377 nb_tcp.buf = ai_tcp->ai_addr;
378 nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
379 if (nfs_minvers == NFS_VER2)
380 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp,
382 err(1, "rpcb_set tcp failed");
383 if (nfs_minvers <= NFS_VER3)
384 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp,
386 err(1, "rpcb_set tcp failed");
387 freeaddrinfo(ai_tcp);
389 if (tcpflag && ip6flag) {
390 memset(&hints, 0, sizeof hints);
391 hints.ai_flags = AI_PASSIVE;
392 hints.ai_family = AF_INET6;
393 hints.ai_socktype = SOCK_STREAM;
394 hints.ai_protocol = IPPROTO_TCP;
395 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
397 err(1, "getaddrinfo tcp6: %s", gai_strerror(ecode));
398 nconf_tcp6 = getnetconfigent("tcp6");
399 if (nconf_tcp6 == NULL)
400 err(1, "getnetconfigent tcp6 failed");
401 nb_tcp6.buf = ai_tcp6->ai_addr;
402 nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
403 if (nfs_minvers == NFS_VER2)
404 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6,
406 err(1, "rpcb_set tcp6 failed");
407 if (nfs_minvers <= NFS_VER3)
408 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6,
410 err(1, "rpcb_set tcp6 failed");
411 freeaddrinfo(ai_tcp6);
417 (void)signal(SIGHUP, SIG_IGN);
418 (void)signal(SIGINT, SIG_IGN);
420 * nfsd sits in the kernel most of the time. It needs
421 * to ignore SIGTERM/SIGQUIT in order to stay alive as long
422 * as possible during a shutdown, otherwise loopback
423 * mounts will not be able to unmount.
425 (void)signal(SIGTERM, SIG_IGN);
426 (void)signal(SIGQUIT, SIG_IGN);
428 (void)signal(SIGSYS, nonfs);
429 (void)signal(SIGCHLD, reapchild);
430 (void)signal(SIGUSR2, backup_stable);
432 openlog("nfsd", LOG_PID | (debug ? LOG_PERROR : 0), LOG_DAEMON);
435 * For V4, we open the stablerestart file and call nfssvc()
436 * to get it loaded. This is done before the daemons do the
437 * regular nfssvc() call to service NFS requests.
438 * (This way the file remains open until the last nfsd is killed
440 * It and the backup copy will be created as empty files
441 * the first time this nfsd is started and should never be
442 * deleted/replaced if at all possible. It should live on a
443 * local, non-volatile storage device that does not do hardware
444 * level write-back caching. (See SCSI doc for more information
445 * on how to prevent write-back caching on SCSI disks.)
447 open_stable(&stablefd, &backupfd);
449 syslog(LOG_ERR, "Can't open %s: %m\n", NFSD_STABLERESTART);
452 /* This system call will fail for old kernels, but that's ok. */
453 nfssvc(NFSSVC_BACKUPSTABLE, NULL);
454 if (nfssvc(NFSSVC_STABLERESTART, (caddr_t)&stablefd) < 0) {
455 syslog(LOG_ERR, "Can't read stable storage file: %m\n");
458 nfssvc_addsock = NFSSVC_NFSDADDSOCK;
459 nfssvc_nfsd = NFSSVC_NFSDNFSD | NFSSVC_NEWSTRUCT;
463 * For TCP mode, we fork once to start the first
464 * kernel nfsd thread. The kernel will add more
467 masterpid = getpid();
470 syslog(LOG_ERR, "fork: %m");
476 (void)signal(SIGUSR1, child_cleanup);
477 setproctitle("server");
478 start_server(0, &nfsdargs);
482 (void)signal(SIGUSR1, cleanup);
488 /* Set up the socket for udp and rpcb register it. */
491 for (i = 0; i < bindhostc; i++) {
492 memset(&hints, 0, sizeof hints);
493 hints.ai_flags = AI_PASSIVE;
494 hints.ai_family = AF_INET;
495 hints.ai_socktype = SOCK_DGRAM;
496 hints.ai_protocol = IPPROTO_UDP;
497 if (setbindhost(&ai_udp, bindhost[i], hints) == 0) {
500 if ((sock = socket(ai_udp->ai_family,
502 ai_udp->ai_protocol)) < 0) {
504 "can't create udp socket");
507 if (bind(sock, ai_udp->ai_addr,
508 ai_udp->ai_addrlen) < 0) {
510 "can't bind udp addr %s: %m",
514 freeaddrinfo(ai_udp);
515 addsockargs.sock = sock;
516 addsockargs.name = NULL;
517 addsockargs.namelen = 0;
518 if (nfssvc(nfssvc_addsock, &addsockargs) < 0) {
519 syslog(LOG_ERR, "can't Add UDP socket");
526 memset(&hints, 0, sizeof hints);
527 hints.ai_flags = AI_PASSIVE;
528 hints.ai_family = AF_INET;
529 hints.ai_socktype = SOCK_DGRAM;
530 hints.ai_protocol = IPPROTO_UDP;
531 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
533 syslog(LOG_ERR, "getaddrinfo udp: %s",
534 gai_strerror(ecode));
537 nconf_udp = getnetconfigent("udp");
538 if (nconf_udp == NULL)
539 err(1, "getnetconfigent udp failed");
540 nb_udp.buf = ai_udp->ai_addr;
541 nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
542 if (nfs_minvers == NFS_VER2)
543 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp,
545 err(1, "rpcb_set udp failed");
546 if (nfs_minvers <= NFS_VER3)
547 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp,
549 err(1, "rpcb_set udp failed");
550 freeaddrinfo(ai_udp);
554 /* Set up the socket for udp6 and rpcb register it. */
555 if (udpflag && ip6flag) {
557 for (i = 0; i < bindhostc; i++) {
558 memset(&hints, 0, sizeof hints);
559 hints.ai_flags = AI_PASSIVE;
560 hints.ai_family = AF_INET6;
561 hints.ai_socktype = SOCK_DGRAM;
562 hints.ai_protocol = IPPROTO_UDP;
563 if (setbindhost(&ai_udp6, bindhost[i], hints) == 0) {
566 if ((sock = socket(ai_udp6->ai_family,
567 ai_udp6->ai_socktype,
568 ai_udp6->ai_protocol)) < 0) {
570 "can't create udp6 socket");
573 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
574 &on, sizeof on) < 0) {
576 "can't set v6-only binding for "
580 if (bind(sock, ai_udp6->ai_addr,
581 ai_udp6->ai_addrlen) < 0) {
583 "can't bind udp6 addr %s: %m",
587 freeaddrinfo(ai_udp6);
588 addsockargs.sock = sock;
589 addsockargs.name = NULL;
590 addsockargs.namelen = 0;
591 if (nfssvc(nfssvc_addsock, &addsockargs) < 0) {
593 "can't add UDP6 socket");
600 memset(&hints, 0, sizeof hints);
601 hints.ai_flags = AI_PASSIVE;
602 hints.ai_family = AF_INET6;
603 hints.ai_socktype = SOCK_DGRAM;
604 hints.ai_protocol = IPPROTO_UDP;
605 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
607 syslog(LOG_ERR, "getaddrinfo udp6: %s",
608 gai_strerror(ecode));
611 nconf_udp6 = getnetconfigent("udp6");
612 if (nconf_udp6 == NULL)
613 err(1, "getnetconfigent udp6 failed");
614 nb_udp6.buf = ai_udp6->ai_addr;
615 nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
616 if (nfs_minvers == NFS_VER2)
617 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp6,
620 "rpcb_set udp6 failed");
621 if (nfs_minvers <= NFS_VER3)
622 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6,
625 "rpcb_set udp6 failed");
626 freeaddrinfo(ai_udp6);
630 /* Set up the socket for tcp and rpcb register it. */
633 for (i = 0; i < bindhostc; i++) {
634 memset(&hints, 0, sizeof hints);
635 hints.ai_flags = AI_PASSIVE;
636 hints.ai_family = AF_INET;
637 hints.ai_socktype = SOCK_STREAM;
638 hints.ai_protocol = IPPROTO_TCP;
639 if (setbindhost(&ai_tcp, bindhost[i], hints) == 0) {
642 if ((tcpsock = socket(AF_INET, SOCK_STREAM,
645 "can't create tcp socket");
648 if (setsockopt(tcpsock, SOL_SOCKET,
650 (char *)&on, sizeof(on)) < 0)
652 "setsockopt SO_REUSEADDR: %m");
653 if (bind(tcpsock, ai_tcp->ai_addr,
654 ai_tcp->ai_addrlen) < 0) {
656 "can't bind tcp addr %s: %m",
660 if (listen(tcpsock, -1) < 0) {
661 syslog(LOG_ERR, "listen failed");
664 freeaddrinfo(ai_tcp);
665 FD_SET(tcpsock, &sockbits);
666 FD_SET(tcpsock, &v4bits);
672 memset(&hints, 0, sizeof hints);
673 hints.ai_flags = AI_PASSIVE;
674 hints.ai_family = AF_INET;
675 hints.ai_socktype = SOCK_STREAM;
676 hints.ai_protocol = IPPROTO_TCP;
677 ecode = getaddrinfo(NULL, "nfs", &hints,
680 syslog(LOG_ERR, "getaddrinfo tcp: %s",
681 gai_strerror(ecode));
684 nconf_tcp = getnetconfigent("tcp");
685 if (nconf_tcp == NULL)
686 err(1, "getnetconfigent tcp failed");
687 nb_tcp.buf = ai_tcp->ai_addr;
688 nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
689 if (nfs_minvers == NFS_VER2)
690 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp,
692 err(1, "rpcb_set tcp failed");
693 if (nfs_minvers <= NFS_VER3)
694 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp,
696 err(1, "rpcb_set tcp failed");
697 freeaddrinfo(ai_tcp);
701 /* Set up the socket for tcp6 and rpcb register it. */
702 if (tcpflag && ip6flag) {
704 for (i = 0; i < bindhostc; i++) {
705 memset(&hints, 0, sizeof hints);
706 hints.ai_flags = AI_PASSIVE;
707 hints.ai_family = AF_INET6;
708 hints.ai_socktype = SOCK_STREAM;
709 hints.ai_protocol = IPPROTO_TCP;
710 if (setbindhost(&ai_tcp6, bindhost[i], hints) == 0) {
713 if ((tcp6sock = socket(ai_tcp6->ai_family,
714 ai_tcp6->ai_socktype,
715 ai_tcp6->ai_protocol)) < 0) {
717 "can't create tcp6 socket");
720 if (setsockopt(tcp6sock, SOL_SOCKET,
722 (char *)&on, sizeof(on)) < 0)
724 "setsockopt SO_REUSEADDR: %m");
725 if (setsockopt(tcp6sock, IPPROTO_IPV6,
726 IPV6_V6ONLY, &on, sizeof on) < 0) {
728 "can't set v6-only binding for tcp6 "
732 if (bind(tcp6sock, ai_tcp6->ai_addr,
733 ai_tcp6->ai_addrlen) < 0) {
735 "can't bind tcp6 addr %s: %m",
739 if (listen(tcp6sock, -1) < 0) {
740 syslog(LOG_ERR, "listen failed");
743 freeaddrinfo(ai_tcp6);
744 FD_SET(tcp6sock, &sockbits);
745 FD_SET(tcp6sock, &v6bits);
746 if (maxsock < tcp6sock)
752 memset(&hints, 0, sizeof hints);
753 hints.ai_flags = AI_PASSIVE;
754 hints.ai_family = AF_INET6;
755 hints.ai_socktype = SOCK_STREAM;
756 hints.ai_protocol = IPPROTO_TCP;
757 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
759 syslog(LOG_ERR, "getaddrinfo tcp6: %s",
760 gai_strerror(ecode));
763 nconf_tcp6 = getnetconfigent("tcp6");
764 if (nconf_tcp6 == NULL)
765 err(1, "getnetconfigent tcp6 failed");
766 nb_tcp6.buf = ai_tcp6->ai_addr;
767 nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
768 if (nfs_minvers == NFS_VER2)
769 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6,
771 err(1, "rpcb_set tcp6 failed");
772 if (nfs_minvers <= NFS_VER3)
773 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6,
775 err(1, "rpcb_set tcp6 failed");
776 freeaddrinfo(ai_tcp6);
780 if (rpcbregcnt == 0) {
781 syslog(LOG_ERR, "rpcb_set() failed, nothing to do: %m");
785 if (tcpflag && connect_type_cnt == 0) {
786 syslog(LOG_ERR, "tcp connects == 0, nothing to do: %m");
790 setproctitle("master");
792 * We always want a master to have a clean way to shut nfsd down
793 * (with unregistration): if the master is killed, it unregisters and
794 * kills all children. If we run for UDP only (and so do not have to
795 * loop waiting for accept), we instead make the parent
796 * a "server" too. start_server will not return.
799 start_server(1, &nfsdargs);
802 * Loop forever accepting connections and passing the sockets
803 * into the kernel for the mounts.
807 if (connect_type_cnt > 1) {
808 if (select(maxsock + 1,
809 &ready, NULL, NULL, NULL) < 1) {
813 syslog(LOG_ERR, "select failed: %m");
817 for (tcpsock = 0; tcpsock <= maxsock; tcpsock++) {
818 if (FD_ISSET(tcpsock, &ready)) {
819 if (FD_ISSET(tcpsock, &v4bits)) {
820 len = sizeof(inetpeer);
821 if ((msgsock = accept(tcpsock,
822 (struct sockaddr *)&inetpeer, &len)) < 0) {
824 syslog(LOG_ERR, "accept failed: %m");
825 if (error == ECONNABORTED ||
830 memset(inetpeer.sin_zero, 0,
831 sizeof(inetpeer.sin_zero));
832 if (setsockopt(msgsock, SOL_SOCKET,
833 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
835 "setsockopt SO_KEEPALIVE: %m");
836 addsockargs.sock = msgsock;
837 addsockargs.name = (caddr_t)&inetpeer;
838 addsockargs.namelen = len;
839 nfssvc(nfssvc_addsock, &addsockargs);
840 (void)close(msgsock);
841 } else if (FD_ISSET(tcpsock, &v6bits)) {
842 len = sizeof(inet6peer);
843 if ((msgsock = accept(tcpsock,
844 (struct sockaddr *)&inet6peer,
848 "accept failed: %m");
849 if (error == ECONNABORTED ||
854 if (setsockopt(msgsock, SOL_SOCKET,
855 SO_KEEPALIVE, (char *)&on,
857 syslog(LOG_ERR, "setsockopt "
859 addsockargs.sock = msgsock;
860 addsockargs.name = (caddr_t)&inet6peer;
861 addsockargs.namelen = len;
862 nfssvc(nfssvc_addsock, &addsockargs);
863 (void)close(msgsock);
871 setbindhost(struct addrinfo **ai, const char *bindhost, struct addrinfo hints)
874 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
877 if (bindhost == NULL || strcmp("*", bindhost) == 0)
882 if (hostptr != NULL) {
883 switch (hints.ai_family) {
885 if (inet_pton(AF_INET, hostptr, host_addr) == 1) {
886 hints.ai_flags = AI_NUMERICHOST;
888 if (inet_pton(AF_INET6, hostptr,
894 if (inet_pton(AF_INET6, hostptr, host_addr) == 1) {
895 hints.ai_flags = AI_NUMERICHOST;
897 if (inet_pton(AF_INET, hostptr,
907 ecode = getaddrinfo(hostptr, "nfs", &hints, ai);
909 syslog(LOG_ERR, "getaddrinfo %s: %s", bindhost,
910 gai_strerror(ecode));
917 set_nfsdcnt(int proposed)
921 warnx("nfsd count too low %d; reset to %d", proposed,
923 nfsdcnt = DEFNFSDCNT;
924 } else if (proposed > MAXNFSDCNT) {
925 warnx("nfsd count too high %d; truncated to %d", proposed,
927 nfsdcnt = MAXNFSDCNT;
936 (void)fprintf(stderr, "%s", getopt_usage);
941 nonfs(__unused int signo)
943 syslog(LOG_ERR, "missing system call: NFS not available");
947 reapchild(__unused int signo)
952 while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) {
953 for (i = 0; i < nfsdcnt; i++)
954 if (pid == children[i])
962 if ((!rpcb_unset(NFS_PROGRAM, 2, NULL)) ||
963 (!rpcb_unset(NFS_PROGRAM, 3, NULL)))
964 syslog(LOG_ERR, "rpcb_unset failed");
972 for (i = 0; i < nfsdcnt; i++) {
974 kill(children[i], SIGKILL);
979 * Cleanup master after SIGUSR1.
982 cleanup(__unused int signo)
988 * Cleanup child after SIGUSR1.
991 child_cleanup(__unused int signo)
997 nfsd_exit(int status)
1005 get_tuned_nfsdcount(void)
1007 int ncpu, error, tuned_nfsdcnt;
1010 ncpu_size = sizeof(ncpu);
1011 error = sysctlbyname("hw.ncpu", &ncpu, &ncpu_size, NULL, 0);
1013 warnx("sysctlbyname(hw.ncpu) failed defaulting to %d nfs servers",
1015 tuned_nfsdcnt = DEFNFSDCNT;
1017 tuned_nfsdcnt = ncpu * 8;
1019 return tuned_nfsdcnt;
1023 start_server(int master, struct nfsd_nfsd_args *nfsdargp)
1025 char principal[MAXHOSTNAMELEN + 5];
1027 char hostname[MAXHOSTNAMELEN + 1], *cp;
1028 struct addrinfo *aip, hints;
1031 gethostname(hostname, sizeof (hostname));
1032 snprintf(principal, sizeof (principal), "nfs@%s", hostname);
1033 if ((cp = strchr(hostname, '.')) == NULL ||
1034 *(cp + 1) == '\0') {
1035 /* If not fully qualified, try getaddrinfo() */
1036 memset((void *)&hints, 0, sizeof (hints));
1037 hints.ai_flags = AI_CANONNAME;
1038 error = getaddrinfo(hostname, NULL, &hints, &aip);
1040 if (aip->ai_canonname != NULL &&
1041 (cp = strchr(aip->ai_canonname, '.')) !=
1042 NULL && *(cp + 1) != '\0')
1043 snprintf(principal, sizeof (principal),
1044 "nfs@%s", aip->ai_canonname);
1048 nfsdargp->principal = principal;
1051 nfsdargp->minthreads = nfsdargp->maxthreads = nfsdcnt;
1053 nfsdargp->minthreads = minthreads_set ? minthreads : get_tuned_nfsdcount();
1054 nfsdargp->maxthreads = maxthreads_set ? maxthreads : nfsdargp->minthreads;
1055 if (nfsdargp->maxthreads < nfsdargp->minthreads)
1056 nfsdargp->maxthreads = nfsdargp->minthreads;
1058 error = nfssvc(nfssvc_nfsd, nfsdargp);
1059 if (error < 0 && errno == EAUTH) {
1061 * This indicates that it could not register the
1062 * rpcsec_gss credentials, usually because the
1063 * gssd daemon isn't running.
1064 * (only the experimental server with nfsv4)
1066 syslog(LOG_ERR, "No gssd, using AUTH_SYS only");
1067 principal[0] = '\0';
1068 error = nfssvc(nfssvc_nfsd, nfsdargp);
1071 if (errno == ENXIO) {
1072 syslog(LOG_ERR, "Bad -p option, cannot run");
1073 if (masterpid != 0 && master == 0)
1074 kill(masterpid, SIGUSR1);
1076 syslog(LOG_ERR, "nfssvc: %m");
1086 * Open the stable restart file and return the file descriptor for it.
1089 open_stable(int *stable_fdp, int *backup_fdp)
1091 int stable_fd, backup_fd = -1, ret;
1092 struct stat st, backup_st;
1094 /* Open and stat the stable restart file. */
1095 stable_fd = open(NFSD_STABLERESTART, O_RDWR, 0);
1097 stable_fd = open(NFSD_STABLERESTART, O_RDWR | O_CREAT, 0600);
1098 if (stable_fd >= 0) {
1099 ret = fstat(stable_fd, &st);
1106 /* Open and stat the backup stable restart file. */
1107 if (stable_fd >= 0) {
1108 backup_fd = open(NFSD_STABLEBACKUP, O_RDWR, 0);
1110 backup_fd = open(NFSD_STABLEBACKUP, O_RDWR | O_CREAT,
1112 if (backup_fd >= 0) {
1113 ret = fstat(backup_fd, &backup_st);
1119 if (backup_fd < 0) {
1125 *stable_fdp = stable_fd;
1126 *backup_fdp = backup_fd;
1130 /* Sync up the 2 files, as required. */
1132 copy_stable(stable_fd, backup_fd);
1133 else if (backup_st.st_size > 0)
1134 copy_stable(backup_fd, stable_fd);
1138 * Copy the stable restart file to the backup or vice versa.
1141 copy_stable(int from_fd, int to_fd)
1144 static char buf[1024];
1146 ret = lseek(from_fd, (off_t)0, SEEK_SET);
1148 ret = lseek(to_fd, (off_t)0, SEEK_SET);
1150 ret = ftruncate(to_fd, (off_t)0);
1153 cnt = read(from_fd, buf, 1024);
1155 ret = write(to_fd, buf, cnt);
1158 } while (cnt > 0 && ret >= 0);
1162 syslog(LOG_ERR, "stable restart copy failure: %m");
1166 * Back up the stable restart file when indicated by the kernel.
1169 backup_stable(__unused int signo)
1173 copy_stable(stablefd, backupfd);
1177 * Parse the pNFS string and extract the DS servers and ports numbers.
1180 parse_dsserver(const char *optionarg, struct nfsd_nfsd_args *nfsdargp)
1182 char *cp, *cp2, *dsaddr, *dshost, *dspath, *dsvol, nfsprt[9];
1183 char *mdspath, *mdsp, ip6[INET6_ADDRSTRLEN];
1186 u_int adsiz, dsaddrcnt, dshostcnt, dspathcnt, hostsiz, pathsiz;
1188 size_t dsaddrsiz, dshostsiz, dspathsiz, nfsprtsiz, mdspathsiz;
1189 struct addrinfo hints, *ai_tcp, *res;
1190 struct sockaddr_in sin;
1191 struct sockaddr_in6 sin6;
1193 cp = strdup(optionarg);
1195 errx(1, "Out of memory");
1197 /* Now, do the host names. */
1200 dspath = malloc(dspathsiz);
1202 errx(1, "Out of memory");
1205 dshost = malloc(dshostsiz);
1207 errx(1, "Out of memory");
1210 dsaddr = malloc(dsaddrsiz);
1212 errx(1, "Out of memory");
1215 mdspath = malloc(mdspathsiz);
1216 if (mdspath == NULL)
1217 errx(1, "Out of memory");
1219 /* Put the NFS port# in "." form. */
1220 snprintf(nfsprt, 9, ".%d.%d", 2049 >> 8, 2049 & 0xff);
1221 nfsprtsiz = strlen(nfsprt);
1224 /* Loop around for each DS server name. */
1226 cp2 = strchr(cp, ',');
1228 /* Not the last DS in the list. */
1234 dsvol = strchr(cp, ':');
1235 if (dsvol == NULL || *(dsvol + 1) == '\0')
1239 /* Optional path for MDS file system to be stored on DS. */
1240 mdsp = strchr(dsvol, '#');
1242 if (*(mdsp + 1) == '\0' || mdsp <= dsvol)
1247 /* Append this pathname to dspath. */
1248 pathsiz = strlen(dsvol);
1249 if (dspathcnt + pathsiz + 1 > dspathsiz) {
1251 dspath = realloc(dspath, dspathsiz);
1253 errx(1, "Out of memory");
1255 strcpy(&dspath[dspathcnt], dsvol);
1256 dspathcnt += pathsiz + 1;
1258 /* Append this pathname to mdspath. */
1260 pathsiz = strlen(mdsp);
1263 if (mdspathcnt + pathsiz + 1 > mdspathsiz) {
1265 mdspath = realloc(mdspath, mdspathsiz);
1266 if (mdspath == NULL)
1267 errx(1, "Out of memory");
1270 strcpy(&mdspath[mdspathcnt], mdsp);
1272 mdspath[mdspathcnt] = '\0';
1273 mdspathcnt += pathsiz + 1;
1276 freeaddrinfo(ai_tcp);
1278 /* Get the fully qualified domain name and IP address. */
1279 memset(&hints, 0, sizeof(hints));
1280 hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
1281 hints.ai_family = PF_UNSPEC;
1282 hints.ai_socktype = SOCK_STREAM;
1283 hints.ai_protocol = IPPROTO_TCP;
1284 ecode = getaddrinfo(cp, NULL, &hints, &ai_tcp);
1286 err(1, "getaddrinfo pnfs: %s %s", cp,
1287 gai_strerror(ecode));
1289 for (res = ai_tcp; res != NULL; res = res->ai_next) {
1290 if (res->ai_addr->sa_family == AF_INET) {
1291 if (res->ai_addrlen < sizeof(sin))
1292 err(1, "getaddrinfo() returned "
1293 "undersized IPv4 address");
1295 * Mips cares about sockaddr_in alignment,
1296 * so copy the address.
1298 memcpy(&sin, res->ai_addr, sizeof(sin));
1299 ad = inet_ntoa(sin.sin_addr);
1301 } else if (res->ai_family == AF_INET6) {
1302 if (res->ai_addrlen < sizeof(sin6))
1303 err(1, "getaddrinfo() returned "
1304 "undersized IPv6 address");
1306 * Mips cares about sockaddr_in6 alignment,
1307 * so copy the address.
1309 memcpy(&sin6, res->ai_addr, sizeof(sin6));
1310 ad = inet_ntop(AF_INET6, &sin6.sin6_addr, ip6,
1315 * Since a link local address will only
1316 * work if the client and DS are in the
1317 * same scope zone, only use it if it is
1321 !IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr))
1326 err(1, "No IP address for %s", cp);
1328 /* Append this address to dsaddr. */
1330 if (dsaddrcnt + adsiz + nfsprtsiz + 1 > dsaddrsiz) {
1332 dsaddr = realloc(dsaddr, dsaddrsiz);
1334 errx(1, "Out of memory");
1336 strcpy(&dsaddr[dsaddrcnt], ad);
1337 strcat(&dsaddr[dsaddrcnt], nfsprt);
1338 dsaddrcnt += adsiz + nfsprtsiz + 1;
1340 /* Append this hostname to dshost. */
1341 hostsiz = strlen(ai_tcp->ai_canonname);
1342 if (dshostcnt + hostsiz + 1 > dshostsiz) {
1344 dshost = realloc(dshost, dshostsiz);
1346 errx(1, "Out of memory");
1348 strcpy(&dshost[dshostcnt], ai_tcp->ai_canonname);
1349 dshostcnt += hostsiz + 1;
1352 } while (cp != NULL);
1354 nfsdargp->addr = dsaddr;
1355 nfsdargp->addrlen = dsaddrcnt;
1356 nfsdargp->dnshost = dshost;
1357 nfsdargp->dnshostlen = dshostcnt;
1358 nfsdargp->dspath = dspath;
1359 nfsdargp->dspathlen = dspathcnt;
1360 nfsdargp->mdspath = mdspath;
1361 nfsdargp->mdspathlen = mdspathcnt;
1362 freeaddrinfo(ai_tcp);