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 *, const char *vhost);
126 static void unregistration(void);
127 static void usage(void);
128 static void open_stable(int *, int *);
129 static void copy_stable(int, int);
130 static void backup_stable(int);
131 static void set_nfsdcnt(int);
132 static void parse_dsserver(const char *, struct nfsd_nfsd_args *);
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;
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,...,\n"
193 " [-V virtual_hostname]\n"
194 " dsserverN:/dsserverN-mounted-on-dir] [-m mirrorlevel]\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");
310 nfs_minvers_size = sizeof(nfs_minvers);
311 error = sysctlbyname("vfs.nfsd.server_min_nfsvers", &nfs_minvers,
312 &nfs_minvers_size, NULL, 0);
313 if (error != 0 || nfs_minvers < NFS_VER2 || nfs_minvers > NFS_VER4) {
314 warnx("sysctlbyname(vfs.nfsd.server_min_nfsvers) failed,"
315 " defaulting to NFSv2");
316 nfs_minvers = NFS_VER2;
325 memset(&hints, 0, sizeof hints);
326 hints.ai_flags = AI_PASSIVE;
327 hints.ai_family = AF_INET;
328 hints.ai_socktype = SOCK_DGRAM;
329 hints.ai_protocol = IPPROTO_UDP;
330 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
332 err(1, "getaddrinfo udp: %s", gai_strerror(ecode));
333 nconf_udp = getnetconfigent("udp");
334 if (nconf_udp == NULL)
335 err(1, "getnetconfigent udp failed");
336 nb_udp.buf = ai_udp->ai_addr;
337 nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
338 if (nfs_minvers == NFS_VER2)
339 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp,
341 err(1, "rpcb_set udp failed");
342 if (nfs_minvers <= NFS_VER3)
343 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp,
345 err(1, "rpcb_set udp failed");
346 freeaddrinfo(ai_udp);
348 if (udpflag && ip6flag) {
349 memset(&hints, 0, sizeof hints);
350 hints.ai_flags = AI_PASSIVE;
351 hints.ai_family = AF_INET6;
352 hints.ai_socktype = SOCK_DGRAM;
353 hints.ai_protocol = IPPROTO_UDP;
354 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
356 err(1, "getaddrinfo udp6: %s", gai_strerror(ecode));
357 nconf_udp6 = getnetconfigent("udp6");
358 if (nconf_udp6 == NULL)
359 err(1, "getnetconfigent udp6 failed");
360 nb_udp6.buf = ai_udp6->ai_addr;
361 nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
362 if (nfs_minvers == NFS_VER2)
363 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp6,
365 err(1, "rpcb_set udp6 failed");
366 if (nfs_minvers <= NFS_VER3)
367 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6,
369 err(1, "rpcb_set udp6 failed");
370 freeaddrinfo(ai_udp6);
373 memset(&hints, 0, sizeof hints);
374 hints.ai_flags = AI_PASSIVE;
375 hints.ai_family = AF_INET;
376 hints.ai_socktype = SOCK_STREAM;
377 hints.ai_protocol = IPPROTO_TCP;
378 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp);
380 err(1, "getaddrinfo tcp: %s", gai_strerror(ecode));
381 nconf_tcp = getnetconfigent("tcp");
382 if (nconf_tcp == NULL)
383 err(1, "getnetconfigent tcp failed");
384 nb_tcp.buf = ai_tcp->ai_addr;
385 nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
386 if (nfs_minvers == NFS_VER2)
387 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp,
389 err(1, "rpcb_set tcp failed");
390 if (nfs_minvers <= NFS_VER3)
391 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp,
393 err(1, "rpcb_set tcp failed");
394 freeaddrinfo(ai_tcp);
396 if (tcpflag && ip6flag) {
397 memset(&hints, 0, sizeof hints);
398 hints.ai_flags = AI_PASSIVE;
399 hints.ai_family = AF_INET6;
400 hints.ai_socktype = SOCK_STREAM;
401 hints.ai_protocol = IPPROTO_TCP;
402 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
404 err(1, "getaddrinfo tcp6: %s", gai_strerror(ecode));
405 nconf_tcp6 = getnetconfigent("tcp6");
406 if (nconf_tcp6 == NULL)
407 err(1, "getnetconfigent tcp6 failed");
408 nb_tcp6.buf = ai_tcp6->ai_addr;
409 nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
410 if (nfs_minvers == NFS_VER2)
411 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6,
413 err(1, "rpcb_set tcp6 failed");
414 if (nfs_minvers <= NFS_VER3)
415 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6,
417 err(1, "rpcb_set tcp6 failed");
418 freeaddrinfo(ai_tcp6);
424 (void)signal(SIGHUP, SIG_IGN);
425 (void)signal(SIGINT, SIG_IGN);
427 * nfsd sits in the kernel most of the time. It needs
428 * to ignore SIGTERM/SIGQUIT in order to stay alive as long
429 * as possible during a shutdown, otherwise loopback
430 * mounts will not be able to unmount.
432 (void)signal(SIGTERM, SIG_IGN);
433 (void)signal(SIGQUIT, SIG_IGN);
435 (void)signal(SIGSYS, nonfs);
436 (void)signal(SIGCHLD, reapchild);
437 (void)signal(SIGUSR2, backup_stable);
439 openlog("nfsd", LOG_PID | (debug ? LOG_PERROR : 0), LOG_DAEMON);
442 * For V4, we open the stablerestart file and call nfssvc()
443 * to get it loaded. This is done before the daemons do the
444 * regular nfssvc() call to service NFS requests.
445 * (This way the file remains open until the last nfsd is killed
447 * It and the backup copy will be created as empty files
448 * the first time this nfsd is started and should never be
449 * deleted/replaced if at all possible. It should live on a
450 * local, non-volatile storage device that does not do hardware
451 * level write-back caching. (See SCSI doc for more information
452 * on how to prevent write-back caching on SCSI disks.)
454 open_stable(&stablefd, &backupfd);
456 syslog(LOG_ERR, "Can't open %s: %m\n", NFSD_STABLERESTART);
459 /* This system call will fail for old kernels, but that's ok. */
460 nfssvc(NFSSVC_BACKUPSTABLE, NULL);
461 if (nfssvc(NFSSVC_STABLERESTART, (caddr_t)&stablefd) < 0) {
462 syslog(LOG_ERR, "Can't read stable storage file: %m\n");
465 nfssvc_addsock = NFSSVC_NFSDADDSOCK;
466 nfssvc_nfsd = NFSSVC_NFSDNFSD | NFSSVC_NEWSTRUCT;
470 * For TCP mode, we fork once to start the first
471 * kernel nfsd thread. The kernel will add more
474 masterpid = getpid();
477 syslog(LOG_ERR, "fork: %m");
483 (void)signal(SIGUSR1, child_cleanup);
484 setproctitle("server");
485 start_server(0, &nfsdargs, vhostname);
489 (void)signal(SIGUSR1, cleanup);
493 /* Set up the socket for udp and rpcb register it. */
496 for (i = 0; i < bindhostc; i++) {
497 memset(&hints, 0, sizeof hints);
498 hints.ai_flags = AI_PASSIVE;
499 hints.ai_family = AF_INET;
500 hints.ai_socktype = SOCK_DGRAM;
501 hints.ai_protocol = IPPROTO_UDP;
502 if (setbindhost(&ai_udp, bindhost[i], hints) == 0) {
505 if ((sock = socket(ai_udp->ai_family,
507 ai_udp->ai_protocol)) < 0) {
509 "can't create udp socket");
512 if (bind(sock, ai_udp->ai_addr,
513 ai_udp->ai_addrlen) < 0) {
515 "can't bind udp addr %s: %m",
519 freeaddrinfo(ai_udp);
520 addsockargs.sock = sock;
521 addsockargs.name = NULL;
522 addsockargs.namelen = 0;
523 if (nfssvc(nfssvc_addsock, &addsockargs) < 0) {
524 syslog(LOG_ERR, "can't Add UDP socket");
531 memset(&hints, 0, sizeof hints);
532 hints.ai_flags = AI_PASSIVE;
533 hints.ai_family = AF_INET;
534 hints.ai_socktype = SOCK_DGRAM;
535 hints.ai_protocol = IPPROTO_UDP;
536 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
538 syslog(LOG_ERR, "getaddrinfo udp: %s",
539 gai_strerror(ecode));
542 nconf_udp = getnetconfigent("udp");
543 if (nconf_udp == NULL)
544 err(1, "getnetconfigent udp failed");
545 nb_udp.buf = ai_udp->ai_addr;
546 nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
547 if (nfs_minvers == NFS_VER2)
548 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp,
550 err(1, "rpcb_set udp failed");
551 if (nfs_minvers <= NFS_VER3)
552 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp,
554 err(1, "rpcb_set udp failed");
555 freeaddrinfo(ai_udp);
559 /* Set up the socket for udp6 and rpcb register it. */
560 if (udpflag && ip6flag) {
562 for (i = 0; i < bindhostc; i++) {
563 memset(&hints, 0, sizeof hints);
564 hints.ai_flags = AI_PASSIVE;
565 hints.ai_family = AF_INET6;
566 hints.ai_socktype = SOCK_DGRAM;
567 hints.ai_protocol = IPPROTO_UDP;
568 if (setbindhost(&ai_udp6, bindhost[i], hints) == 0) {
571 if ((sock = socket(ai_udp6->ai_family,
572 ai_udp6->ai_socktype,
573 ai_udp6->ai_protocol)) < 0) {
575 "can't create udp6 socket");
578 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
579 &on, sizeof on) < 0) {
581 "can't set v6-only binding for "
585 if (bind(sock, ai_udp6->ai_addr,
586 ai_udp6->ai_addrlen) < 0) {
588 "can't bind udp6 addr %s: %m",
592 freeaddrinfo(ai_udp6);
593 addsockargs.sock = sock;
594 addsockargs.name = NULL;
595 addsockargs.namelen = 0;
596 if (nfssvc(nfssvc_addsock, &addsockargs) < 0) {
598 "can't add UDP6 socket");
605 memset(&hints, 0, sizeof hints);
606 hints.ai_flags = AI_PASSIVE;
607 hints.ai_family = AF_INET6;
608 hints.ai_socktype = SOCK_DGRAM;
609 hints.ai_protocol = IPPROTO_UDP;
610 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
612 syslog(LOG_ERR, "getaddrinfo udp6: %s",
613 gai_strerror(ecode));
616 nconf_udp6 = getnetconfigent("udp6");
617 if (nconf_udp6 == NULL)
618 err(1, "getnetconfigent udp6 failed");
619 nb_udp6.buf = ai_udp6->ai_addr;
620 nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
621 if (nfs_minvers == NFS_VER2)
622 if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp6,
625 "rpcb_set udp6 failed");
626 if (nfs_minvers <= NFS_VER3)
627 if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6,
630 "rpcb_set udp6 failed");
631 freeaddrinfo(ai_udp6);
635 /* Set up the socket for tcp and rpcb register it. */
638 for (i = 0; i < bindhostc; i++) {
639 memset(&hints, 0, sizeof hints);
640 hints.ai_flags = AI_PASSIVE;
641 hints.ai_family = AF_INET;
642 hints.ai_socktype = SOCK_STREAM;
643 hints.ai_protocol = IPPROTO_TCP;
644 if (setbindhost(&ai_tcp, bindhost[i], hints) == 0) {
647 if ((tcpsock = socket(AF_INET, SOCK_STREAM,
650 "can't create tcp socket");
653 if (setsockopt(tcpsock, SOL_SOCKET,
655 (char *)&on, sizeof(on)) < 0)
657 "setsockopt SO_REUSEADDR: %m");
658 if (bind(tcpsock, ai_tcp->ai_addr,
659 ai_tcp->ai_addrlen) < 0) {
661 "can't bind tcp addr %s: %m",
665 if (listen(tcpsock, -1) < 0) {
666 syslog(LOG_ERR, "listen failed");
669 freeaddrinfo(ai_tcp);
670 FD_SET(tcpsock, &sockbits);
676 memset(&hints, 0, sizeof hints);
677 hints.ai_flags = AI_PASSIVE;
678 hints.ai_family = AF_INET;
679 hints.ai_socktype = SOCK_STREAM;
680 hints.ai_protocol = IPPROTO_TCP;
681 ecode = getaddrinfo(NULL, "nfs", &hints,
684 syslog(LOG_ERR, "getaddrinfo tcp: %s",
685 gai_strerror(ecode));
688 nconf_tcp = getnetconfigent("tcp");
689 if (nconf_tcp == NULL)
690 err(1, "getnetconfigent tcp failed");
691 nb_tcp.buf = ai_tcp->ai_addr;
692 nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
693 if (nfs_minvers == NFS_VER2)
694 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp,
696 err(1, "rpcb_set tcp failed");
697 if (nfs_minvers <= NFS_VER3)
698 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp,
700 err(1, "rpcb_set tcp failed");
701 freeaddrinfo(ai_tcp);
705 /* Set up the socket for tcp6 and rpcb register it. */
706 if (tcpflag && ip6flag) {
708 for (i = 0; i < bindhostc; i++) {
709 memset(&hints, 0, sizeof hints);
710 hints.ai_flags = AI_PASSIVE;
711 hints.ai_family = AF_INET6;
712 hints.ai_socktype = SOCK_STREAM;
713 hints.ai_protocol = IPPROTO_TCP;
714 if (setbindhost(&ai_tcp6, bindhost[i], hints) == 0) {
717 if ((tcp6sock = socket(ai_tcp6->ai_family,
718 ai_tcp6->ai_socktype,
719 ai_tcp6->ai_protocol)) < 0) {
721 "can't create tcp6 socket");
724 if (setsockopt(tcp6sock, SOL_SOCKET,
726 (char *)&on, sizeof(on)) < 0)
728 "setsockopt SO_REUSEADDR: %m");
729 if (setsockopt(tcp6sock, IPPROTO_IPV6,
730 IPV6_V6ONLY, &on, sizeof on) < 0) {
732 "can't set v6-only binding for tcp6 "
736 if (bind(tcp6sock, ai_tcp6->ai_addr,
737 ai_tcp6->ai_addrlen) < 0) {
739 "can't bind tcp6 addr %s: %m",
743 if (listen(tcp6sock, -1) < 0) {
744 syslog(LOG_ERR, "listen failed");
747 freeaddrinfo(ai_tcp6);
748 FD_SET(tcp6sock, &sockbits);
749 if (maxsock < tcp6sock)
755 memset(&hints, 0, sizeof hints);
756 hints.ai_flags = AI_PASSIVE;
757 hints.ai_family = AF_INET6;
758 hints.ai_socktype = SOCK_STREAM;
759 hints.ai_protocol = IPPROTO_TCP;
760 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
762 syslog(LOG_ERR, "getaddrinfo tcp6: %s",
763 gai_strerror(ecode));
766 nconf_tcp6 = getnetconfigent("tcp6");
767 if (nconf_tcp6 == NULL)
768 err(1, "getnetconfigent tcp6 failed");
769 nb_tcp6.buf = ai_tcp6->ai_addr;
770 nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
771 if (nfs_minvers == NFS_VER2)
772 if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6,
774 err(1, "rpcb_set tcp6 failed");
775 if (nfs_minvers <= NFS_VER3)
776 if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6,
778 err(1, "rpcb_set tcp6 failed");
779 freeaddrinfo(ai_tcp6);
783 if (rpcbregcnt == 0) {
784 syslog(LOG_ERR, "rpcb_set() failed, nothing to do: %m");
788 if (tcpflag && connect_type_cnt == 0) {
789 syslog(LOG_ERR, "tcp connects == 0, nothing to do: %m");
793 setproctitle("master");
795 * We always want a master to have a clean way to shut nfsd down
796 * (with unregistration): if the master is killed, it unregisters and
797 * kills all children. If we run for UDP only (and so do not have to
798 * loop waiting for accept), we instead make the parent
799 * a "server" too. start_server will not return.
802 start_server(1, &nfsdargs, vhostname);
805 * Loop forever accepting connections and passing the sockets
806 * into the kernel for the mounts.
810 if (connect_type_cnt > 1) {
811 if (select(maxsock + 1,
812 &ready, NULL, NULL, NULL) < 1) {
816 syslog(LOG_ERR, "select failed: %m");
820 for (tcpsock = 0; tcpsock <= maxsock; tcpsock++) {
821 if (FD_ISSET(tcpsock, &ready)) {
823 if ((msgsock = accept(tcpsock,
824 (struct sockaddr *)&peer, &len)) < 0) {
826 syslog(LOG_ERR, "accept failed: %m");
827 if (error == ECONNABORTED ||
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)&peer;
838 addsockargs.namelen = len;
839 nfssvc(nfssvc_addsock, &addsockargs);
840 (void)close(msgsock);
847 setbindhost(struct addrinfo **ai, const char *bindhost, struct addrinfo hints)
850 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
853 if (bindhost == NULL || strcmp("*", bindhost) == 0)
858 if (hostptr != NULL) {
859 switch (hints.ai_family) {
861 if (inet_pton(AF_INET, hostptr, host_addr) == 1) {
862 hints.ai_flags = AI_NUMERICHOST;
864 if (inet_pton(AF_INET6, hostptr,
870 if (inet_pton(AF_INET6, hostptr, host_addr) == 1) {
871 hints.ai_flags = AI_NUMERICHOST;
873 if (inet_pton(AF_INET, hostptr,
883 ecode = getaddrinfo(hostptr, "nfs", &hints, ai);
885 syslog(LOG_ERR, "getaddrinfo %s: %s", bindhost,
886 gai_strerror(ecode));
893 set_nfsdcnt(int proposed)
897 warnx("nfsd count too low %d; reset to %d", proposed,
899 nfsdcnt = DEFNFSDCNT;
900 } else if (proposed > MAXNFSDCNT) {
901 warnx("nfsd count too high %d; truncated to %d", proposed,
903 nfsdcnt = MAXNFSDCNT;
912 (void)fprintf(stderr, "%s", getopt_usage);
917 nonfs(__unused int signo)
919 syslog(LOG_ERR, "missing system call: NFS not available");
923 reapchild(__unused int signo)
928 while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) {
929 for (i = 0; i < nfsdcnt; i++)
930 if (pid == children[i])
938 if ((!rpcb_unset(NFS_PROGRAM, 2, NULL)) ||
939 (!rpcb_unset(NFS_PROGRAM, 3, NULL)))
940 syslog(LOG_ERR, "rpcb_unset failed");
948 for (i = 0; i < nfsdcnt; i++) {
950 kill(children[i], SIGKILL);
955 * Cleanup master after SIGUSR1.
958 cleanup(__unused int signo)
964 * Cleanup child after SIGUSR1.
967 child_cleanup(__unused int signo)
973 nfsd_exit(int status)
981 get_tuned_nfsdcount(void)
983 int ncpu, error, tuned_nfsdcnt;
986 ncpu_size = sizeof(ncpu);
987 error = sysctlbyname("hw.ncpu", &ncpu, &ncpu_size, NULL, 0);
989 warnx("sysctlbyname(hw.ncpu) failed defaulting to %d nfs servers",
991 tuned_nfsdcnt = DEFNFSDCNT;
993 tuned_nfsdcnt = ncpu * 8;
995 return tuned_nfsdcnt;
999 start_server(int master, struct nfsd_nfsd_args *nfsdargp, const char *vhost)
1001 char principal[MAXHOSTNAMELEN + 5];
1003 char hostname[MAXHOSTNAMELEN + 1], *cp;
1004 struct addrinfo *aip, hints;
1008 gethostname(hostname, sizeof (hostname));
1010 strlcpy(hostname, vhost, sizeof (hostname));
1011 snprintf(principal, sizeof (principal), "nfs@%s", hostname);
1012 if ((cp = strchr(hostname, '.')) == NULL ||
1013 *(cp + 1) == '\0') {
1014 /* If not fully qualified, try getaddrinfo() */
1015 memset((void *)&hints, 0, sizeof (hints));
1016 hints.ai_flags = AI_CANONNAME;
1017 error = getaddrinfo(hostname, NULL, &hints, &aip);
1019 if (aip->ai_canonname != NULL &&
1020 (cp = strchr(aip->ai_canonname, '.')) !=
1021 NULL && *(cp + 1) != '\0')
1022 snprintf(principal, sizeof (principal),
1023 "nfs@%s", aip->ai_canonname);
1027 nfsdargp->principal = principal;
1030 nfsdargp->minthreads = nfsdargp->maxthreads = nfsdcnt;
1032 nfsdargp->minthreads = minthreads_set ? minthreads : get_tuned_nfsdcount();
1033 nfsdargp->maxthreads = maxthreads_set ? maxthreads : nfsdargp->minthreads;
1034 if (nfsdargp->maxthreads < nfsdargp->minthreads)
1035 nfsdargp->maxthreads = nfsdargp->minthreads;
1037 error = nfssvc(nfssvc_nfsd, nfsdargp);
1038 if (error < 0 && errno == EAUTH) {
1040 * This indicates that it could not register the
1041 * rpcsec_gss credentials, usually because the
1042 * gssd daemon isn't running.
1043 * (only the experimental server with nfsv4)
1045 syslog(LOG_ERR, "No gssd, using AUTH_SYS only");
1046 principal[0] = '\0';
1047 error = nfssvc(nfssvc_nfsd, nfsdargp);
1050 if (errno == ENXIO) {
1051 syslog(LOG_ERR, "Bad -p option, cannot run");
1052 if (masterpid != 0 && master == 0)
1053 kill(masterpid, SIGUSR1);
1055 syslog(LOG_ERR, "nfssvc: %m");
1065 * Open the stable restart file and return the file descriptor for it.
1068 open_stable(int *stable_fdp, int *backup_fdp)
1070 int stable_fd, backup_fd = -1, ret;
1071 struct stat st, backup_st;
1073 /* Open and stat the stable restart file. */
1074 stable_fd = open(NFSD_STABLERESTART, O_RDWR, 0);
1076 stable_fd = open(NFSD_STABLERESTART, O_RDWR | O_CREAT, 0600);
1077 if (stable_fd >= 0) {
1078 ret = fstat(stable_fd, &st);
1085 /* Open and stat the backup stable restart file. */
1086 if (stable_fd >= 0) {
1087 backup_fd = open(NFSD_STABLEBACKUP, O_RDWR, 0);
1089 backup_fd = open(NFSD_STABLEBACKUP, O_RDWR | O_CREAT,
1091 if (backup_fd >= 0) {
1092 ret = fstat(backup_fd, &backup_st);
1098 if (backup_fd < 0) {
1104 *stable_fdp = stable_fd;
1105 *backup_fdp = backup_fd;
1109 /* Sync up the 2 files, as required. */
1111 copy_stable(stable_fd, backup_fd);
1112 else if (backup_st.st_size > 0)
1113 copy_stable(backup_fd, stable_fd);
1117 * Copy the stable restart file to the backup or vice versa.
1120 copy_stable(int from_fd, int to_fd)
1123 static char buf[1024];
1125 ret = lseek(from_fd, (off_t)0, SEEK_SET);
1127 ret = lseek(to_fd, (off_t)0, SEEK_SET);
1129 ret = ftruncate(to_fd, (off_t)0);
1132 cnt = read(from_fd, buf, 1024);
1134 ret = write(to_fd, buf, cnt);
1137 } while (cnt > 0 && ret >= 0);
1141 syslog(LOG_ERR, "stable restart copy failure: %m");
1145 * Back up the stable restart file when indicated by the kernel.
1148 backup_stable(__unused int signo)
1152 copy_stable(stablefd, backupfd);
1156 * Parse the pNFS string and extract the DS servers and ports numbers.
1159 parse_dsserver(const char *optionarg, struct nfsd_nfsd_args *nfsdargp)
1161 char *cp, *cp2, *dsaddr, *dshost, *dspath, *dsvol, nfsprt[9];
1162 char *mdspath, *mdsp, ip6[INET6_ADDRSTRLEN];
1165 u_int adsiz, dsaddrcnt, dshostcnt, dspathcnt, hostsiz, pathsiz;
1167 size_t dsaddrsiz, dshostsiz, dspathsiz, nfsprtsiz, mdspathsiz;
1168 struct addrinfo hints, *ai_tcp, *res;
1169 struct sockaddr_in sin;
1170 struct sockaddr_in6 sin6;
1172 cp = strdup(optionarg);
1174 errx(1, "Out of memory");
1176 /* Now, do the host names. */
1179 dspath = malloc(dspathsiz);
1181 errx(1, "Out of memory");
1184 dshost = malloc(dshostsiz);
1186 errx(1, "Out of memory");
1189 dsaddr = malloc(dsaddrsiz);
1191 errx(1, "Out of memory");
1194 mdspath = malloc(mdspathsiz);
1195 if (mdspath == NULL)
1196 errx(1, "Out of memory");
1198 /* Put the NFS port# in "." form. */
1199 snprintf(nfsprt, 9, ".%d.%d", 2049 >> 8, 2049 & 0xff);
1200 nfsprtsiz = strlen(nfsprt);
1203 /* Loop around for each DS server name. */
1205 cp2 = strchr(cp, ',');
1207 /* Not the last DS in the list. */
1213 dsvol = strchr(cp, ':');
1214 if (dsvol == NULL || *(dsvol + 1) == '\0')
1218 /* Optional path for MDS file system to be stored on DS. */
1219 mdsp = strchr(dsvol, '#');
1221 if (*(mdsp + 1) == '\0' || mdsp <= dsvol)
1226 /* Append this pathname to dspath. */
1227 pathsiz = strlen(dsvol);
1228 if (dspathcnt + pathsiz + 1 > dspathsiz) {
1230 dspath = realloc(dspath, dspathsiz);
1232 errx(1, "Out of memory");
1234 strcpy(&dspath[dspathcnt], dsvol);
1235 dspathcnt += pathsiz + 1;
1237 /* Append this pathname to mdspath. */
1239 pathsiz = strlen(mdsp);
1242 if (mdspathcnt + pathsiz + 1 > mdspathsiz) {
1244 mdspath = realloc(mdspath, mdspathsiz);
1245 if (mdspath == NULL)
1246 errx(1, "Out of memory");
1249 strcpy(&mdspath[mdspathcnt], mdsp);
1251 mdspath[mdspathcnt] = '\0';
1252 mdspathcnt += pathsiz + 1;
1255 freeaddrinfo(ai_tcp);
1257 /* Get the fully qualified domain name and IP address. */
1258 memset(&hints, 0, sizeof(hints));
1259 hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
1260 hints.ai_family = PF_UNSPEC;
1261 hints.ai_socktype = SOCK_STREAM;
1262 hints.ai_protocol = IPPROTO_TCP;
1263 ecode = getaddrinfo(cp, NULL, &hints, &ai_tcp);
1265 err(1, "getaddrinfo pnfs: %s %s", cp,
1266 gai_strerror(ecode));
1268 for (res = ai_tcp; res != NULL; res = res->ai_next) {
1269 if (res->ai_addr->sa_family == AF_INET) {
1270 if (res->ai_addrlen < sizeof(sin))
1271 err(1, "getaddrinfo() returned "
1272 "undersized IPv4 address");
1274 * Mips cares about sockaddr_in alignment,
1275 * so copy the address.
1277 memcpy(&sin, res->ai_addr, sizeof(sin));
1278 ad = inet_ntoa(sin.sin_addr);
1280 } else if (res->ai_family == AF_INET6) {
1281 if (res->ai_addrlen < sizeof(sin6))
1282 err(1, "getaddrinfo() returned "
1283 "undersized IPv6 address");
1285 * Mips cares about sockaddr_in6 alignment,
1286 * so copy the address.
1288 memcpy(&sin6, res->ai_addr, sizeof(sin6));
1289 ad = inet_ntop(AF_INET6, &sin6.sin6_addr, ip6,
1294 * Since a link local address will only
1295 * work if the client and DS are in the
1296 * same scope zone, only use it if it is
1300 !IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr))
1305 err(1, "No IP address for %s", cp);
1307 /* Append this address to dsaddr. */
1309 if (dsaddrcnt + adsiz + nfsprtsiz + 1 > dsaddrsiz) {
1311 dsaddr = realloc(dsaddr, dsaddrsiz);
1313 errx(1, "Out of memory");
1315 strcpy(&dsaddr[dsaddrcnt], ad);
1316 strcat(&dsaddr[dsaddrcnt], nfsprt);
1317 dsaddrcnt += adsiz + nfsprtsiz + 1;
1319 /* Append this hostname to dshost. */
1320 hostsiz = strlen(ai_tcp->ai_canonname);
1321 if (dshostcnt + hostsiz + 1 > dshostsiz) {
1323 dshost = realloc(dshost, dshostsiz);
1325 errx(1, "Out of memory");
1327 strcpy(&dshost[dshostcnt], ai_tcp->ai_canonname);
1328 dshostcnt += hostsiz + 1;
1331 } while (cp != NULL);
1333 nfsdargp->addr = dsaddr;
1334 nfsdargp->addrlen = dsaddrcnt;
1335 nfsdargp->dnshost = dshost;
1336 nfsdargp->dnshostlen = dshostcnt;
1337 nfsdargp->dspath = dspath;
1338 nfsdargp->dspathlen = dspathcnt;
1339 nfsdargp->mdspath = mdspath;
1340 nfsdargp->mdspathlen = mdspathcnt;
1341 freeaddrinfo(ai_tcp);