xref: /illumos-gate/usr/src/cmd/fs.d/nfs/nfsd/nfsd.c (revision e8279403)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22fdd1ecaeSmaheshvs  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T		*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /* LINTLIBRARY */
407c478bd9Sstevel@tonic-gate /* PROTOLIB1 */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /* NFS server */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include <sys/param.h>
477c478bd9Sstevel@tonic-gate #include <sys/types.h>
48cee86682Scalum #include <sys/stat.h>
497c478bd9Sstevel@tonic-gate #include <syslog.h>
507c478bd9Sstevel@tonic-gate #include <tiuser.h>
517c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
527c478bd9Sstevel@tonic-gate #include <errno.h>
537c478bd9Sstevel@tonic-gate #include <thread.h>
547c478bd9Sstevel@tonic-gate #include <sys/resource.h>
557c478bd9Sstevel@tonic-gate #include <sys/time.h>
567c478bd9Sstevel@tonic-gate #include <sys/file.h>
577c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
587c478bd9Sstevel@tonic-gate #include <nfs/nfs_acl.h>
597c478bd9Sstevel@tonic-gate #include <nfs/nfssys.h>
607c478bd9Sstevel@tonic-gate #include <stdio.h>
61004388ebScasper #include <stdio_ext.h>
627c478bd9Sstevel@tonic-gate #include <stdlib.h>
637c478bd9Sstevel@tonic-gate #include <signal.h>
647c478bd9Sstevel@tonic-gate #include <netconfig.h>
657c478bd9Sstevel@tonic-gate #include <netdir.h>
667c478bd9Sstevel@tonic-gate #include <string.h>
677c478bd9Sstevel@tonic-gate #include <unistd.h>
687c478bd9Sstevel@tonic-gate #include <stropts.h>
697c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
707c478bd9Sstevel@tonic-gate #include <poll.h>
717c478bd9Sstevel@tonic-gate #include <priv_utils.h>
727c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
737c478bd9Sstevel@tonic-gate #include <netinet/tcp.h>
747c478bd9Sstevel@tonic-gate #include <deflt.h>
757c478bd9Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h>
767c478bd9Sstevel@tonic-gate #include <rpcsvc/nfs4_prot.h>
77cee86682Scalum #include <libnvpair.h>
787c478bd9Sstevel@tonic-gate #include "nfs_tbind.h"
797c478bd9Sstevel@tonic-gate #include "thrpool.h"
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /* quiesce requests will be ignored if nfs_server_vers_max < QUIESCE_VERSMIN */
827c478bd9Sstevel@tonic-gate #define	QUIESCE_VERSMIN	4
83cee86682Scalum /* DSS: distributed stable storage */
84cee86682Scalum #define	DSS_VERSMIN	4
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static	int	nfssvc(int, struct netbuf, struct netconfig *);
877c478bd9Sstevel@tonic-gate static	int	nfssvcpool(int maxservers);
88cee86682Scalum static	int	dss_init(uint_t npaths, char **pathnames);
89cee86682Scalum static	void	dss_mkleafdirs(uint_t npaths, char **pathnames);
90cee86682Scalum static	void	dss_mkleafdir(char *dir, char *leaf, char *path);
917c478bd9Sstevel@tonic-gate static	void	usage(void);
92cee86682Scalum int		qstrcmp(const void *s1, const void *s2);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate extern	int	_nfssys(int, void *);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /* signal handlers */
977c478bd9Sstevel@tonic-gate static void sigflush(int);
987c478bd9Sstevel@tonic-gate static void quiesce(int);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static	char	*MyName;
1017c478bd9Sstevel@tonic-gate static	NETSELDECL(defaultproviders)[] = { "/dev/tcp6", "/dev/tcp", "/dev/udp",
1027c478bd9Sstevel@tonic-gate 					    "/dev/udp6", NULL };
1037c478bd9Sstevel@tonic-gate /* static	NETSELDECL(defaultprotos)[] =	{ NC_UDP, NC_TCP, NULL }; */
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * The following are all globals used by routines in nfs_tbind.c.
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate size_t	end_listen_fds;		/* used by conn_close_oldest() */
1087c478bd9Sstevel@tonic-gate size_t	num_fds = 0;		/* used by multiple routines */
1097c478bd9Sstevel@tonic-gate int	listen_backlog = 32;	/* used by bind_to_{provider,proto}() */
1107c478bd9Sstevel@tonic-gate int	num_servers;		/* used by cots_listen_event() */
1117c478bd9Sstevel@tonic-gate int	(*Mysvc)(int, struct netbuf, struct netconfig *) = nfssvc;
1127c478bd9Sstevel@tonic-gate 				/* used by cots_listen_event() */
1137c478bd9Sstevel@tonic-gate int	max_conns_allowed = -1;	/* used by cots_listen_event() */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  * Keep track of min/max versions of NFS protocol to be started.
1177c478bd9Sstevel@tonic-gate  * Start with the defaults (min == 2, max == 3).  We have the
1187c478bd9Sstevel@tonic-gate  * capability of starting vers=4 but only if the user requests it.
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate int	nfs_server_vers_min = NFS_VERSMIN_DEFAULT;
1217c478bd9Sstevel@tonic-gate int	nfs_server_vers_max = NFS_VERSMAX_DEFAULT;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate  * Set the default for server delegation enablement and set per
1257c478bd9Sstevel@tonic-gate  * /etc/default/nfs configuration (if present).
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate int	nfs_server_delegation = NFS_SERVER_DELEGATION_DEFAULT;
1287c478bd9Sstevel@tonic-gate 
12911606941Sjwahlig int
13011606941Sjwahlig main(int ac, char *av[])
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	char *dir = "/";
1337c478bd9Sstevel@tonic-gate 	int allflag = 0;
1347c478bd9Sstevel@tonic-gate 	int df_allflag = 0;
1357c478bd9Sstevel@tonic-gate 	int opt_cnt = 0;
1367c478bd9Sstevel@tonic-gate 	int maxservers = 1;	/* zero allows inifinte number of threads */
1377c478bd9Sstevel@tonic-gate 	int maxservers_set = 0;
1387c478bd9Sstevel@tonic-gate 	int logmaxservers = 0;
1397c478bd9Sstevel@tonic-gate 	int pid;
1407c478bd9Sstevel@tonic-gate 	int i;
1417c478bd9Sstevel@tonic-gate 	char *provider = (char *)NULL;
1427c478bd9Sstevel@tonic-gate 	char *df_provider = (char *)NULL;
1437c478bd9Sstevel@tonic-gate 	struct protob *protobp0, *protobp;
1447c478bd9Sstevel@tonic-gate 	NETSELDECL(proto) = NULL;
1457c478bd9Sstevel@tonic-gate 	NETSELDECL(df_proto) = NULL;
1467c478bd9Sstevel@tonic-gate 	NETSELPDECL(providerp);
1477c478bd9Sstevel@tonic-gate 	char *defval;
14845916cd2Sjpk 	boolean_t can_do_mlp;
149cee86682Scalum 	uint_t dss_npaths = 0;
150cee86682Scalum 	char **dss_pathnames = NULL;
151*e8279403Smaheshvs 	sigset_t sgset;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	MyName = *av;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/*
1567c478bd9Sstevel@tonic-gate 	 * Initializations that require more privileges than we need to run.
1577c478bd9Sstevel@tonic-gate 	 */
1587c478bd9Sstevel@tonic-gate 	(void) _create_daemon_lock(NFSD, DAEMON_UID, DAEMON_GID);
1597c478bd9Sstevel@tonic-gate 	svcsetprio();
1607c478bd9Sstevel@tonic-gate 
16145916cd2Sjpk 	can_do_mlp = priv_ineffect(PRIV_NET_BINDMLP);
1627c478bd9Sstevel@tonic-gate 	if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET,
16345916cd2Sjpk 	    DAEMON_UID, DAEMON_GID, PRIV_SYS_NFS,
16445916cd2Sjpk 	    can_do_mlp ? PRIV_NET_BINDMLP : NULL, NULL) == -1) {
1657c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s should be run with"
1667c478bd9Sstevel@tonic-gate 		    " sufficient privileges\n", av[0]);
1677c478bd9Sstevel@tonic-gate 		exit(1);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
170004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
171004388ebScasper 
1727c478bd9Sstevel@tonic-gate 	/*
1737c478bd9Sstevel@tonic-gate 	 * Read in the values from config file first before we check
1747c478bd9Sstevel@tonic-gate 	 * commandline options so the options override the file.
1757c478bd9Sstevel@tonic-gate 	 */
1767c478bd9Sstevel@tonic-gate 	if ((defopen(NFSADMIN)) == 0) {
1777c478bd9Sstevel@tonic-gate 		if ((defval = defread("NFSD_MAX_CONNECTIONS=")) != NULL) {
1787c478bd9Sstevel@tonic-gate 			errno = 0;
1797c478bd9Sstevel@tonic-gate 			max_conns_allowed = strtol(defval, (char **)NULL, 10);
1807c478bd9Sstevel@tonic-gate 			if (errno != 0) {
1817c478bd9Sstevel@tonic-gate 				max_conns_allowed = -1;
1827c478bd9Sstevel@tonic-gate 			}
1837c478bd9Sstevel@tonic-gate 		}
1847c478bd9Sstevel@tonic-gate 		if ((defval = defread("NFSD_LISTEN_BACKLOG=")) != NULL) {
1857c478bd9Sstevel@tonic-gate 			errno = 0;
1867c478bd9Sstevel@tonic-gate 			listen_backlog = strtol(defval, (char **)NULL, 10);
1877c478bd9Sstevel@tonic-gate 			if (errno != 0) {
1887c478bd9Sstevel@tonic-gate 				listen_backlog = 32;
1897c478bd9Sstevel@tonic-gate 			}
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 		if ((defval = defread("NFSD_PROTOCOL=")) != NULL) {
1927c478bd9Sstevel@tonic-gate 			df_proto = strdup(defval);
1937c478bd9Sstevel@tonic-gate 			opt_cnt++;
1947c478bd9Sstevel@tonic-gate 			if (strncasecmp("ALL", defval, 3) == 0) {
1957c478bd9Sstevel@tonic-gate 				free(df_proto);
1967c478bd9Sstevel@tonic-gate 				df_proto = NULL;
1977c478bd9Sstevel@tonic-gate 				df_allflag = 1;
1987c478bd9Sstevel@tonic-gate 			}
1997c478bd9Sstevel@tonic-gate 		}
2007c478bd9Sstevel@tonic-gate 		if ((defval = defread("NFSD_DEVICE=")) != NULL) {
2017c478bd9Sstevel@tonic-gate 			df_provider = strdup(defval);
2027c478bd9Sstevel@tonic-gate 			opt_cnt++;
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 		if ((defval = defread("NFSD_SERVERS=")) != NULL) {
2057c478bd9Sstevel@tonic-gate 			errno = 0;
2067c478bd9Sstevel@tonic-gate 			maxservers = strtol(defval, (char **)NULL, 10);
2077c478bd9Sstevel@tonic-gate 			if (errno != 0) {
2087c478bd9Sstevel@tonic-gate 				maxservers = 1;
2097c478bd9Sstevel@tonic-gate 			} else {
2107c478bd9Sstevel@tonic-gate 				maxservers_set = 1;
2117c478bd9Sstevel@tonic-gate 			}
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 		if ((defval = defread("NFS_SERVER_VERSMIN=")) != NULL) {
2147c478bd9Sstevel@tonic-gate 			errno = 0;
2157c478bd9Sstevel@tonic-gate 			nfs_server_vers_min =
2167c478bd9Sstevel@tonic-gate 			    strtol(defval, (char **)NULL, 10);
2177c478bd9Sstevel@tonic-gate 			if (errno != 0) {
2187c478bd9Sstevel@tonic-gate 				nfs_server_vers_min = NFS_VERSMIN_DEFAULT;
2197c478bd9Sstevel@tonic-gate 			}
2207c478bd9Sstevel@tonic-gate 		}
2217c478bd9Sstevel@tonic-gate 		if ((defval = defread("NFS_SERVER_VERSMAX=")) != NULL) {
2227c478bd9Sstevel@tonic-gate 			errno = 0;
2237c478bd9Sstevel@tonic-gate 			nfs_server_vers_max =
2247c478bd9Sstevel@tonic-gate 			    strtol(defval, (char **)NULL, 10);
2257c478bd9Sstevel@tonic-gate 			if (errno != 0) {
2267c478bd9Sstevel@tonic-gate 				nfs_server_vers_max = NFS_VERSMAX_DEFAULT;
2277c478bd9Sstevel@tonic-gate 			}
2287c478bd9Sstevel@tonic-gate 		}
2297c478bd9Sstevel@tonic-gate 		if ((defval = defread("NFS_SERVER_DELEGATION=")) != NULL) {
2307c478bd9Sstevel@tonic-gate 			if (strcmp(defval, "off") == 0) {
2317c478bd9Sstevel@tonic-gate 				nfs_server_delegation = FALSE;
2327c478bd9Sstevel@tonic-gate 			}
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 		/* close defaults file */
2367c478bd9Sstevel@tonic-gate 		defopen(NULL);
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	/*
2407c478bd9Sstevel@tonic-gate 	 * Conflict options error messages.
2417c478bd9Sstevel@tonic-gate 	 */
2427c478bd9Sstevel@tonic-gate 	if (opt_cnt > 1) {
2437c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\nConflicting options, only one of "
2447c478bd9Sstevel@tonic-gate 		    "the following options can be specified\n"
2457c478bd9Sstevel@tonic-gate 		    "in " NFSADMIN ":\n"
2467c478bd9Sstevel@tonic-gate 		    "\tNFSD_PROTOCOL=ALL\n"
2477c478bd9Sstevel@tonic-gate 		    "\tNFSD_PROTOCOL=protocol\n"
2487c478bd9Sstevel@tonic-gate 		    "\tNFSD_DEVICE=device\n\n");
2497c478bd9Sstevel@tonic-gate 		usage();
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 	opt_cnt = 0;
2527c478bd9Sstevel@tonic-gate 
253cee86682Scalum 	while ((i = getopt(ac, av, "ac:p:s:t:l:")) != EOF) {
2547c478bd9Sstevel@tonic-gate 		switch (i) {
2557c478bd9Sstevel@tonic-gate 		case 'a':
2567c478bd9Sstevel@tonic-gate 			free(df_proto);
2577c478bd9Sstevel@tonic-gate 			df_proto = NULL;
2587c478bd9Sstevel@tonic-gate 			free(df_provider);
2597c478bd9Sstevel@tonic-gate 			df_provider = NULL;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 			allflag = 1;
2627c478bd9Sstevel@tonic-gate 			opt_cnt++;
2637c478bd9Sstevel@tonic-gate 			break;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 		case 'c':
2667c478bd9Sstevel@tonic-gate 			max_conns_allowed = atoi(optarg);
2677c478bd9Sstevel@tonic-gate 			break;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 		case 'p':
2707c478bd9Sstevel@tonic-gate 			proto = optarg;
2717c478bd9Sstevel@tonic-gate 			df_allflag = 0;
2727c478bd9Sstevel@tonic-gate 			opt_cnt++;
2737c478bd9Sstevel@tonic-gate 			break;
2747c478bd9Sstevel@tonic-gate 
275cee86682Scalum 		/*
276cee86682Scalum 		 * DSS: NFSv4 distributed stable storage.
277cee86682Scalum 		 *
278cee86682Scalum 		 * This is a Contracted Project Private interface, for
279cee86682Scalum 		 * the sole use of Sun Cluster HA-NFS. See PSARC/2006/313.
280cee86682Scalum 		 */
281cee86682Scalum 		case 's':
282cee86682Scalum 			if (strlen(optarg) < MAXPATHLEN) {
283cee86682Scalum 				/* first "-s" option encountered? */
284cee86682Scalum 				if (dss_pathnames == NULL) {
285cee86682Scalum 					/*
286cee86682Scalum 					 * Allocate maximum possible space
287cee86682Scalum 					 * required given cmdline arg count;
288cee86682Scalum 					 * "-s <path>" consumes two args.
289cee86682Scalum 					 */
290cee86682Scalum 					size_t sz = (ac / 2) * sizeof (char *);
291cee86682Scalum 					dss_pathnames = (char **)malloc(sz);
292cee86682Scalum 					if (dss_pathnames == NULL) {
293cee86682Scalum 						(void) fprintf(stderr, "%s: "
294cee86682Scalum 						    "dss paths malloc failed\n",
295cee86682Scalum 						    av[0]);
296cee86682Scalum 						exit(1);
297cee86682Scalum 					}
298cee86682Scalum 					(void) memset(dss_pathnames, 0, sz);
299cee86682Scalum 				}
300cee86682Scalum 				dss_pathnames[dss_npaths] = optarg;
301cee86682Scalum 				dss_npaths++;
302cee86682Scalum 			} else {
303cee86682Scalum 				(void) fprintf(stderr,
304cee86682Scalum 				    "%s: -s pathname too long.\n", av[0]);
305cee86682Scalum 			}
306cee86682Scalum 			break;
307cee86682Scalum 
3087c478bd9Sstevel@tonic-gate 		case 't':
3097c478bd9Sstevel@tonic-gate 			provider = optarg;
3107c478bd9Sstevel@tonic-gate 			df_allflag = 0;
3117c478bd9Sstevel@tonic-gate 			opt_cnt++;
3127c478bd9Sstevel@tonic-gate 			break;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 		case 'l':
3157c478bd9Sstevel@tonic-gate 			listen_backlog = atoi(optarg);
3167c478bd9Sstevel@tonic-gate 			break;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 		case '?':
3197c478bd9Sstevel@tonic-gate 			usage();
3207c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
3217c478bd9Sstevel@tonic-gate 		}
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	allflag = df_allflag;
3257c478bd9Sstevel@tonic-gate 	if (proto == NULL)
3267c478bd9Sstevel@tonic-gate 		proto = df_proto;
3277c478bd9Sstevel@tonic-gate 	if (provider == NULL)
3287c478bd9Sstevel@tonic-gate 		provider = df_provider;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	/*
3317c478bd9Sstevel@tonic-gate 	 * Conflict options error messages.
3327c478bd9Sstevel@tonic-gate 	 */
3337c478bd9Sstevel@tonic-gate 	if (opt_cnt > 1) {
3347c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\nConflicting options, only one of "
3357c478bd9Sstevel@tonic-gate 		    "the following options can be specified\n"
3367c478bd9Sstevel@tonic-gate 		    "on the command line:\n"
3377c478bd9Sstevel@tonic-gate 		    "\t-a\n"
3387c478bd9Sstevel@tonic-gate 		    "\t-p protocol\n"
3397c478bd9Sstevel@tonic-gate 		    "\t-t transport\n\n");
3407c478bd9Sstevel@tonic-gate 		usage();
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	if (proto != NULL &&
3447c478bd9Sstevel@tonic-gate 	    strncasecmp(proto, NC_UDP, strlen(NC_UDP)) == 0) {
3457c478bd9Sstevel@tonic-gate 		if (nfs_server_vers_max == NFS_V4) {
3467c478bd9Sstevel@tonic-gate 			if (nfs_server_vers_min == NFS_V4) {
3477c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
3487c478bd9Sstevel@tonic-gate 				    "NFS version 4 is not supported "
3497c478bd9Sstevel@tonic-gate 				    "with the UDP protocol.  Exiting\n");
3507c478bd9Sstevel@tonic-gate 				fprintf(stderr,
3517c478bd9Sstevel@tonic-gate 				    "NFS version 4 is not supported "
3527c478bd9Sstevel@tonic-gate 				    "with the UDP protocol.  Exiting\n");
3537c478bd9Sstevel@tonic-gate 				exit(3);
3547c478bd9Sstevel@tonic-gate 			} else {
3557c478bd9Sstevel@tonic-gate 				fprintf(stderr,
3567c478bd9Sstevel@tonic-gate 				    "NFS version 4 is not supported "
3577c478bd9Sstevel@tonic-gate 				    "with the UDP protocol.\n");
3587c478bd9Sstevel@tonic-gate 			}
3597c478bd9Sstevel@tonic-gate 		}
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	/*
3637c478bd9Sstevel@tonic-gate 	 * If there is exactly one more argument, it is the number of
3647c478bd9Sstevel@tonic-gate 	 * servers.
3657c478bd9Sstevel@tonic-gate 	 */
3667c478bd9Sstevel@tonic-gate 	if (optind == ac - 1) {
3677c478bd9Sstevel@tonic-gate 		maxservers = atoi(av[optind]);
3687c478bd9Sstevel@tonic-gate 		maxservers_set = 1;
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 	/*
3717c478bd9Sstevel@tonic-gate 	 * If there are two or more arguments, then this is a usage error.
3727c478bd9Sstevel@tonic-gate 	 */
3737c478bd9Sstevel@tonic-gate 	else if (optind < ac - 1)
3747c478bd9Sstevel@tonic-gate 		usage();
3757c478bd9Sstevel@tonic-gate 	/*
3767c478bd9Sstevel@tonic-gate 	 * Check the ranges for min/max version specified
3777c478bd9Sstevel@tonic-gate 	 */
3787c478bd9Sstevel@tonic-gate 	else if ((nfs_server_vers_min > nfs_server_vers_max) ||
3797c478bd9Sstevel@tonic-gate 	    (nfs_server_vers_min < NFS_VERSMIN) ||
3807c478bd9Sstevel@tonic-gate 	    (nfs_server_vers_max > NFS_VERSMAX))
3817c478bd9Sstevel@tonic-gate 		usage();
3827c478bd9Sstevel@tonic-gate 	/*
3837c478bd9Sstevel@tonic-gate 	 * There are no additional arguments, and we haven't set maxservers
3847c478bd9Sstevel@tonic-gate 	 * explicitly via the config file, we use a default number of
3857c478bd9Sstevel@tonic-gate 	 * servers.  We will log this.
3867c478bd9Sstevel@tonic-gate 	 */
3877c478bd9Sstevel@tonic-gate 	else if (maxservers_set == 0)
3887c478bd9Sstevel@tonic-gate 		logmaxservers = 1;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	/*
3917c478bd9Sstevel@tonic-gate 	 * Basic Sanity checks on options
3927c478bd9Sstevel@tonic-gate 	 *
3937c478bd9Sstevel@tonic-gate 	 * max_conns_allowed must be positive, except for the special
3947c478bd9Sstevel@tonic-gate 	 * value of -1 which is used internally to mean unlimited, -1 isn't
3957c478bd9Sstevel@tonic-gate 	 * documented but we allow it anyway.
3967c478bd9Sstevel@tonic-gate 	 *
3977c478bd9Sstevel@tonic-gate 	 * maxservers must be positive
3987c478bd9Sstevel@tonic-gate 	 * listen_backlog must be positive or zero
3997c478bd9Sstevel@tonic-gate 	 */
4007c478bd9Sstevel@tonic-gate 	if (((max_conns_allowed != -1) && (max_conns_allowed <= 0)) ||
4017c478bd9Sstevel@tonic-gate 	    (listen_backlog < 0) || (maxservers <= 0)) {
4027c478bd9Sstevel@tonic-gate 		usage();
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	/*
4067c478bd9Sstevel@tonic-gate 	 * Set current dir to server root
4077c478bd9Sstevel@tonic-gate 	 */
4087c478bd9Sstevel@tonic-gate 	if (chdir(dir) < 0) {
4097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s:  ", MyName);
4107c478bd9Sstevel@tonic-gate 		perror(dir);
4117c478bd9Sstevel@tonic-gate 		exit(1);
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate #ifndef DEBUG
4157c478bd9Sstevel@tonic-gate 	/*
4167c478bd9Sstevel@tonic-gate 	 * Background
4177c478bd9Sstevel@tonic-gate 	 */
4187c478bd9Sstevel@tonic-gate 	pid = fork();
4197c478bd9Sstevel@tonic-gate 	if (pid < 0) {
4207c478bd9Sstevel@tonic-gate 		perror("nfsd: fork");
4217c478bd9Sstevel@tonic-gate 		exit(1);
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 	if (pid != 0)
4247c478bd9Sstevel@tonic-gate 		exit(0);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	/*
4277c478bd9Sstevel@tonic-gate 	 * Close existing file descriptors, open "/dev/null" as
4287c478bd9Sstevel@tonic-gate 	 * standard input, output, and error, and detach from
4297c478bd9Sstevel@tonic-gate 	 * controlling terminal.
4307c478bd9Sstevel@tonic-gate 	 */
4317c478bd9Sstevel@tonic-gate 	closefrom(0);
4327c478bd9Sstevel@tonic-gate 	(void) open("/dev/null", O_RDONLY);
4337c478bd9Sstevel@tonic-gate 	(void) open("/dev/null", O_WRONLY);
4347c478bd9Sstevel@tonic-gate 	(void) dup(1);
4357c478bd9Sstevel@tonic-gate 	(void) setsid();
4367c478bd9Sstevel@tonic-gate #endif
4377c478bd9Sstevel@tonic-gate 	openlog(MyName, LOG_PID | LOG_NDELAY, LOG_DAEMON);
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	/*
4407c478bd9Sstevel@tonic-gate 	 * establish our lock on the lock file and write our pid to it.
4417c478bd9Sstevel@tonic-gate 	 * exit if some other process holds the lock, or if there's any
4427c478bd9Sstevel@tonic-gate 	 * error in writing/locking the file.
4437c478bd9Sstevel@tonic-gate 	 */
4447c478bd9Sstevel@tonic-gate 	pid = _enter_daemon_lock(NFSD);
4457c478bd9Sstevel@tonic-gate 	switch (pid) {
4467c478bd9Sstevel@tonic-gate 	case 0:
4477c478bd9Sstevel@tonic-gate 		break;
4487c478bd9Sstevel@tonic-gate 	case -1:
4497c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "error locking for %s: %s", NFSD,
4507c478bd9Sstevel@tonic-gate 		    strerror(errno));
4517c478bd9Sstevel@tonic-gate 		exit(2);
4527c478bd9Sstevel@tonic-gate 	default:
4537c478bd9Sstevel@tonic-gate 		/* daemon was already running */
4547c478bd9Sstevel@tonic-gate 		exit(0);
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 
457cee86682Scalum 	/*
458cee86682Scalum 	 * If we've been given a list of paths to be used for distributed
459cee86682Scalum 	 * stable storage, and provided we're going to run a version
460cee86682Scalum 	 * that supports it, setup the DSS paths.
461cee86682Scalum 	 */
462cee86682Scalum 	if (dss_pathnames != NULL && nfs_server_vers_max >= DSS_VERSMIN) {
463cee86682Scalum 		if (dss_init(dss_npaths, dss_pathnames) != 0) {
464cee86682Scalum 			syslog(LOG_ERR, "dss_init failed. Exiting.");
465cee86682Scalum 			exit(1);
466cee86682Scalum 		}
467cee86682Scalum 	}
468cee86682Scalum 
469*e8279403Smaheshvs 	/*
470*e8279403Smaheshvs 	 * Block all signals till we spawn other
471*e8279403Smaheshvs 	 * threads.
472*e8279403Smaheshvs 	 */
473*e8279403Smaheshvs 	(void) sigfillset(&sgset);
474*e8279403Smaheshvs 	(void) thr_sigsetmask(SIG_BLOCK, &sgset, NULL);
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	if (logmaxservers) {
4777c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_INFO,
4787c478bd9Sstevel@tonic-gate 		    "Number of servers not specified. Using default of %d.",
4797c478bd9Sstevel@tonic-gate 		    maxservers);
4807c478bd9Sstevel@tonic-gate 	}
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	/*
4837c478bd9Sstevel@tonic-gate 	 * Make sure to unregister any previous versions in case the
4847c478bd9Sstevel@tonic-gate 	 * user is reconfiguring the server in interesting ways.
4857c478bd9Sstevel@tonic-gate 	 */
4867c478bd9Sstevel@tonic-gate 	svc_unreg(NFS_PROGRAM, NFS_VERSION);
4877c478bd9Sstevel@tonic-gate 	svc_unreg(NFS_PROGRAM, NFS_V3);
4887c478bd9Sstevel@tonic-gate 	svc_unreg(NFS_PROGRAM, NFS_V4);
4897c478bd9Sstevel@tonic-gate 	svc_unreg(NFS_ACL_PROGRAM, NFS_ACL_V2);
4907c478bd9Sstevel@tonic-gate 	svc_unreg(NFS_ACL_PROGRAM, NFS_ACL_V3);
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	/*
4937c478bd9Sstevel@tonic-gate 	 * Set up kernel RPC thread pool for the NFS server.
4947c478bd9Sstevel@tonic-gate 	 */
4957c478bd9Sstevel@tonic-gate 	if (nfssvcpool(maxservers)) {
4967c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_ERR,
4977c478bd9Sstevel@tonic-gate 		    "Can't set up kernel NFS service: %m. Exiting");
4987c478bd9Sstevel@tonic-gate 		exit(1);
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	/*
5037c478bd9Sstevel@tonic-gate 	 * Set up blocked thread to do LWP creation on behalf of the kernel.
5047c478bd9Sstevel@tonic-gate 	 */
5057c478bd9Sstevel@tonic-gate 	if (svcwait(NFS_SVCPOOL_ID)) {
5067c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_ERR,
5077c478bd9Sstevel@tonic-gate 		    "Can't set up NFS pool creator: %m, Exiting");
5087c478bd9Sstevel@tonic-gate 		exit(1);
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	/*
5127c478bd9Sstevel@tonic-gate 	 * RDMA start and stop thread.
5137c478bd9Sstevel@tonic-gate 	 * Per pool RDMA listener creation and
5147c478bd9Sstevel@tonic-gate 	 * destructor thread.
5157c478bd9Sstevel@tonic-gate 	 *
5167c478bd9Sstevel@tonic-gate 	 * start rdma services and block in the kernel.
5177c478bd9Sstevel@tonic-gate 	 */
5187c478bd9Sstevel@tonic-gate 	if (svcrdma(NFS_SVCPOOL_ID, nfs_server_vers_min, nfs_server_vers_max,
5197c478bd9Sstevel@tonic-gate 	    nfs_server_delegation)) {
5207c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_ERR,
5217c478bd9Sstevel@tonic-gate 		    "Can't set up RDMA creator thread : %m.");
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	/*
525*e8279403Smaheshvs 	 * Now open up for signal delivery
526*e8279403Smaheshvs 	 */
527*e8279403Smaheshvs 
528*e8279403Smaheshvs 	(void) thr_sigsetmask(SIG_UNBLOCK, &sgset, NULL);
529*e8279403Smaheshvs 	sigset(SIGTERM, sigflush);
530*e8279403Smaheshvs 	sigset(SIGUSR1, quiesce);
531*e8279403Smaheshvs 
532*e8279403Smaheshvs 	/*
5337c478bd9Sstevel@tonic-gate 	 * Build a protocol block list for registration.
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 	protobp0 = protobp = (struct protob *)malloc(sizeof (struct protob));
5367c478bd9Sstevel@tonic-gate 	protobp->serv = "NFS";
5377c478bd9Sstevel@tonic-gate 	protobp->versmin = nfs_server_vers_min;
5387c478bd9Sstevel@tonic-gate 	protobp->versmax = nfs_server_vers_max;
5397c478bd9Sstevel@tonic-gate 	protobp->program = NFS_PROGRAM;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	protobp->next = (struct protob *)malloc(sizeof (struct protob));
5427c478bd9Sstevel@tonic-gate 	protobp = protobp->next;
5437c478bd9Sstevel@tonic-gate 	protobp->serv = "NFS_ACL";		/* not used */
5447c478bd9Sstevel@tonic-gate 	protobp->versmin = nfs_server_vers_min;
5457c478bd9Sstevel@tonic-gate 	/* XXX - this needs work to get the version just right */
5467c478bd9Sstevel@tonic-gate 	protobp->versmax = (nfs_server_vers_max > NFS_ACL_V3) ?
5477c478bd9Sstevel@tonic-gate 	    NFS_ACL_V3 : nfs_server_vers_max;
5487c478bd9Sstevel@tonic-gate 	protobp->program = NFS_ACL_PROGRAM;
5497c478bd9Sstevel@tonic-gate 	protobp->next = (struct protob *)NULL;
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	if (allflag) {
5529acbbeafSnn35248 		if (do_all(protobp0, nfssvc, 0) == -1)
5537c478bd9Sstevel@tonic-gate 			exit(1);
5547c478bd9Sstevel@tonic-gate 	} else if (proto) {
5557c478bd9Sstevel@tonic-gate 		/* there's more than one match for the same protocol */
5567c478bd9Sstevel@tonic-gate 		struct netconfig *nconf;
5577c478bd9Sstevel@tonic-gate 		NCONF_HANDLE *nc;
5587c478bd9Sstevel@tonic-gate 		bool_t	protoFound = FALSE;
5597c478bd9Sstevel@tonic-gate 		if ((nc = setnetconfig()) == (NCONF_HANDLE *) NULL) {
5607c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "setnetconfig failed: %m");
5617c478bd9Sstevel@tonic-gate 			goto done;
5627c478bd9Sstevel@tonic-gate 		}
5637c478bd9Sstevel@tonic-gate 		while (nconf = getnetconfig(nc)) {
5647c478bd9Sstevel@tonic-gate 			if (strcmp(nconf->nc_proto, proto) == 0) {
5657c478bd9Sstevel@tonic-gate 				protoFound = TRUE;
5667c478bd9Sstevel@tonic-gate 				do_one(nconf->nc_device, NULL,
5679acbbeafSnn35248 				    protobp0, nfssvc, 0);
5687c478bd9Sstevel@tonic-gate 			}
5697c478bd9Sstevel@tonic-gate 		}
5707c478bd9Sstevel@tonic-gate 		(void) endnetconfig(nc);
5717c478bd9Sstevel@tonic-gate 		if (protoFound == FALSE)
5727c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "couldn't find netconfig entry \
5737c478bd9Sstevel@tonic-gate 			    for protocol %s", proto);
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	} else if (provider)
5769acbbeafSnn35248 		do_one(provider, proto, protobp0, nfssvc, 0);
5777c478bd9Sstevel@tonic-gate 	else {
5787c478bd9Sstevel@tonic-gate 		for (providerp = defaultproviders;
5797c478bd9Sstevel@tonic-gate 		    *providerp != NULL; providerp++) {
5807c478bd9Sstevel@tonic-gate 			provider = *providerp;
5819acbbeafSnn35248 			do_one(provider, NULL, protobp0, nfssvc, 0);
5827c478bd9Sstevel@tonic-gate 		}
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate done:
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	free(protobp);
5877c478bd9Sstevel@tonic-gate 	free(protobp0);
5887c478bd9Sstevel@tonic-gate 
589*e8279403Smaheshvs 
5907c478bd9Sstevel@tonic-gate 	if (num_fds == 0) {
5917c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_ERR,
592cee86682Scalum 		"Could not start NFS service for any protocol. Exiting");
5937c478bd9Sstevel@tonic-gate 		exit(1);
5947c478bd9Sstevel@tonic-gate 	}
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	end_listen_fds = num_fds;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	/*
5997c478bd9Sstevel@tonic-gate 	 * Get rid of unneeded privileges.
6007c478bd9Sstevel@tonic-gate 	 */
6017c478bd9Sstevel@tonic-gate 	__fini_daemon_priv(PRIV_PROC_FORK, PRIV_PROC_EXEC, PRIV_PROC_SESSION,
6027c478bd9Sstevel@tonic-gate 	    PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL);
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	/*
6057c478bd9Sstevel@tonic-gate 	 * Poll for non-data control events on the transport descriptors.
6067c478bd9Sstevel@tonic-gate 	 */
6077c478bd9Sstevel@tonic-gate 	poll_for_action();
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	/*
6107c478bd9Sstevel@tonic-gate 	 * If we get here, something failed in poll_for_action().
6117c478bd9Sstevel@tonic-gate 	 */
6127c478bd9Sstevel@tonic-gate 	return (1);
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate static int
6167c478bd9Sstevel@tonic-gate nfssvcpool(int maxservers)
6177c478bd9Sstevel@tonic-gate {
6187c478bd9Sstevel@tonic-gate 	struct svcpool_args npa;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	npa.id = NFS_SVCPOOL_ID;
6217c478bd9Sstevel@tonic-gate 	npa.maxthreads = maxservers;
6227c478bd9Sstevel@tonic-gate 	npa.redline = 0;
6237c478bd9Sstevel@tonic-gate 	npa.qsize = 0;
6247c478bd9Sstevel@tonic-gate 	npa.timeout = 0;
6257c478bd9Sstevel@tonic-gate 	npa.stksize = 0;
6267c478bd9Sstevel@tonic-gate 	npa.max_same_xprt = 0;
6277c478bd9Sstevel@tonic-gate 	return (_nfssys(SVCPOOL_CREATE, &npa));
6287c478bd9Sstevel@tonic-gate }
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate /*
6317c478bd9Sstevel@tonic-gate  * Establish NFS service thread.
6327c478bd9Sstevel@tonic-gate  */
6337c478bd9Sstevel@tonic-gate static int
6347c478bd9Sstevel@tonic-gate nfssvc(int fd, struct netbuf addrmask, struct netconfig *nconf)
6357c478bd9Sstevel@tonic-gate {
6367c478bd9Sstevel@tonic-gate 	struct nfs_svc_args nsa;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	nsa.fd = fd;
6397c478bd9Sstevel@tonic-gate 	nsa.netid = nconf->nc_netid;
6407c478bd9Sstevel@tonic-gate 	nsa.addrmask = addrmask;
6417c478bd9Sstevel@tonic-gate 	if (strncasecmp(nconf->nc_proto, NC_UDP, strlen(NC_UDP)) == 0) {
6427c478bd9Sstevel@tonic-gate 		nsa.versmax = (nfs_server_vers_max > NFS_V3) ?
6437c478bd9Sstevel@tonic-gate 		    NFS_V3 : nfs_server_vers_max;
6447c478bd9Sstevel@tonic-gate 		nsa.versmin = nfs_server_vers_min;
6457c478bd9Sstevel@tonic-gate 		/*
6467c478bd9Sstevel@tonic-gate 		 * If no version left, silently do nothing, previous
6477c478bd9Sstevel@tonic-gate 		 * checks will have assured at least TCP is available.
6487c478bd9Sstevel@tonic-gate 		 */
6497c478bd9Sstevel@tonic-gate 		if (nsa.versmin > nsa.versmax)
6507c478bd9Sstevel@tonic-gate 			return (0);
6517c478bd9Sstevel@tonic-gate 	} else {
6527c478bd9Sstevel@tonic-gate 		nsa.versmax = nfs_server_vers_max;
6537c478bd9Sstevel@tonic-gate 		nsa.versmin = nfs_server_vers_min;
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 	nsa.delegation = nfs_server_delegation;
6567c478bd9Sstevel@tonic-gate 	return (_nfssys(NFS_SVC, &nsa));
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate static void
6607c478bd9Sstevel@tonic-gate usage(void)
6617c478bd9Sstevel@tonic-gate {
6627c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
6637c478bd9Sstevel@tonic-gate "usage: %s [ -a ] [ -c max_conns ] [ -p protocol ] [ -t transport ] ", MyName);
6647c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n[ -l listen_backlog ] [ nservers ]\n");
6657c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
6667c478bd9Sstevel@tonic-gate "\twhere -a causes <nservers> to be started on each appropriate transport,\n");
6677c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
6687c478bd9Sstevel@tonic-gate "\tmax_conns is the maximum number of concurrent connections allowed,\n");
6697c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t\tand max_conns must be a decimal number");
6707c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "> zero,\n");
6717c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\tprotocol is a protocol identifier,\n");
6727c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
6737c478bd9Sstevel@tonic-gate 	    "\ttransport is a transport provider name (i.e. device),\n");
6747c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
6757c478bd9Sstevel@tonic-gate 	    "\tlisten_backlog is the TCP listen backlog,\n");
6767c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
6777c478bd9Sstevel@tonic-gate 	    "\tand <nservers> must be a decimal number > zero.\n");
6787c478bd9Sstevel@tonic-gate 	exit(1);
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate /*
6827c478bd9Sstevel@tonic-gate  * Issue nfssys system call to flush all logging buffers asynchronously.
6837c478bd9Sstevel@tonic-gate  *
6847c478bd9Sstevel@tonic-gate  * NOTICE: It is extremely important to flush NFS logging buffers when
6857c478bd9Sstevel@tonic-gate  *	   nfsd exits. When the system is halted or rebooted nfslogd
6867c478bd9Sstevel@tonic-gate  *	   may not have an opportunity to flush the buffers.
6877c478bd9Sstevel@tonic-gate  */
6887c478bd9Sstevel@tonic-gate static void
6897c478bd9Sstevel@tonic-gate nfsl_flush()
6907c478bd9Sstevel@tonic-gate {
6917c478bd9Sstevel@tonic-gate 	struct nfsl_flush_args nfa;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	memset((void *)&nfa, 0, sizeof (nfa));
6947c478bd9Sstevel@tonic-gate 	nfa.version = NFSL_FLUSH_ARGS_VERS;
6957c478bd9Sstevel@tonic-gate 	nfa.directive = NFSL_ALL;	/* flush all asynchronously */
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	if (_nfssys(LOG_FLUSH, &nfa) < 0)
6987c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "_nfssys(LOG_FLUSH) failed: %s\n",
6997c478bd9Sstevel@tonic-gate 		    strerror(errno));
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate /*
7037c478bd9Sstevel@tonic-gate  * SIGTERM handler.
7047c478bd9Sstevel@tonic-gate  * Flush logging buffers and exit.
7057c478bd9Sstevel@tonic-gate  */
7067c478bd9Sstevel@tonic-gate static void
7077c478bd9Sstevel@tonic-gate sigflush(int sig)
7087c478bd9Sstevel@tonic-gate {
7097c478bd9Sstevel@tonic-gate 	nfsl_flush();
710*e8279403Smaheshvs 	_exit(0);
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate /*
7147c478bd9Sstevel@tonic-gate  * SIGUSR1 handler.
715cee86682Scalum  *
716cee86682Scalum  * Request that server quiesce, then (nfsd) exit. For subsequent warm start.
717cee86682Scalum  *
718cee86682Scalum  * This is a Contracted Project Private interface, for the sole use
719cee86682Scalum  * of Sun Cluster HA-NFS. See PSARC/2004/497.
720cee86682Scalum  *
7217c478bd9Sstevel@tonic-gate  * Equivalent to SIGTERM handler if nfs_server_vers_max < QUIESCE_VERSMIN.
7227c478bd9Sstevel@tonic-gate  */
7237c478bd9Sstevel@tonic-gate static void
7247c478bd9Sstevel@tonic-gate quiesce(int sig)
7257c478bd9Sstevel@tonic-gate {
7267c478bd9Sstevel@tonic-gate 	int error;
7277c478bd9Sstevel@tonic-gate 	int id = NFS_SVCPOOL_ID;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	if (nfs_server_vers_max >= QUIESCE_VERSMIN) {
7307c478bd9Sstevel@tonic-gate 		/* Request server quiesce at next shutdown */
731cee86682Scalum 		error = _nfssys(NFS4_SVC_REQUEST_QUIESCE, &id);
732fdd1ecaeSmaheshvs 
733fdd1ecaeSmaheshvs 		/*
734fdd1ecaeSmaheshvs 		 * ENOENT is returned if there is no matching SVC pool
735fdd1ecaeSmaheshvs 		 * for the id. Possibly because the pool is not yet setup.
736fdd1ecaeSmaheshvs 		 * In this case, just exit as if no error. For all other errors,
737fdd1ecaeSmaheshvs 		 * just return and allow caller to retry.
738fdd1ecaeSmaheshvs 		 */
739*e8279403Smaheshvs 		if (error && errno != ENOENT) {
7407c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
741cee86682Scalum 			    "_nfssys(NFS4_SVC_REQUEST_QUIESCE) failed: %s",
7427c478bd9Sstevel@tonic-gate 			    strerror(errno));
7437c478bd9Sstevel@tonic-gate 			return;
7447c478bd9Sstevel@tonic-gate 		}
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	/* Flush logging buffers */
7487c478bd9Sstevel@tonic-gate 	nfsl_flush();
7497c478bd9Sstevel@tonic-gate 
750*e8279403Smaheshvs 	_exit(0);
7517c478bd9Sstevel@tonic-gate }
752cee86682Scalum 
753cee86682Scalum /*
754cee86682Scalum  * DSS: distributed stable storage.
755cee86682Scalum  * Create leaf directories as required, keeping an eye on path
756cee86682Scalum  * lengths. Calls exit(1) on failure.
757cee86682Scalum  * The pathnames passed in must already exist, and must be writeable by nfsd.
758cee86682Scalum  * Note: the leaf directories under NFS4_VAR_DIR are not created here;
759cee86682Scalum  * they're created at pkg install.
760cee86682Scalum  */
761cee86682Scalum static void
762cee86682Scalum dss_mkleafdirs(uint_t npaths, char **pathnames)
763cee86682Scalum {
764cee86682Scalum 	int i;
765cee86682Scalum 	char *tmppath = NULL;
766cee86682Scalum 
767cee86682Scalum 	/*
768cee86682Scalum 	 * Create the temporary storage used by dss_mkleafdir() here,
769cee86682Scalum 	 * rather than in that function, so that it only needs to be
770cee86682Scalum 	 * done once, rather than once for each call. Too big to put
771cee86682Scalum 	 * on the function's stack.
772cee86682Scalum 	 */
773cee86682Scalum 	tmppath = (char *)malloc(MAXPATHLEN);
774cee86682Scalum 	if (tmppath == NULL) {
775cee86682Scalum 		syslog(LOG_ERR, "tmppath malloc failed. Exiting");
776cee86682Scalum 		exit(1);
777cee86682Scalum 	}
778cee86682Scalum 
779cee86682Scalum 	for (i = 0; i < npaths; i++) {
780cee86682Scalum 		char *p = pathnames[i];
781cee86682Scalum 
782cee86682Scalum 		dss_mkleafdir(p, NFS4_DSS_STATE_LEAF, tmppath);
783cee86682Scalum 		dss_mkleafdir(p, NFS4_DSS_OLDSTATE_LEAF, tmppath);
784cee86682Scalum 	}
785cee86682Scalum 
786cee86682Scalum 	free(tmppath);
787cee86682Scalum }
788cee86682Scalum 
789cee86682Scalum /*
790cee86682Scalum  * Create "leaf" in "dir" (which must already exist).
791cee86682Scalum  * leaf: should start with a '/'
792cee86682Scalum  */
793cee86682Scalum static void
794cee86682Scalum dss_mkleafdir(char *dir, char *leaf, char *tmppath)
795cee86682Scalum {
796cee86682Scalum 	/* MAXPATHLEN includes the terminating NUL */
797cee86682Scalum 	if (strlen(dir) + strlen(leaf) > MAXPATHLEN - 1) {
798cee86682Scalum 		syslog(LOG_ERR, "stable storage path too long: %s%s. Exiting",
799cee86682Scalum 		    dir, leaf);
800cee86682Scalum 		exit(1);
801cee86682Scalum 	}
802cee86682Scalum 
803cee86682Scalum 	(void) snprintf(tmppath, MAXPATHLEN, "%s/%s", dir, leaf);
804cee86682Scalum 
805cee86682Scalum 	/* the directory may already exist: that's OK */
806cee86682Scalum 	if (mkdir(tmppath, NFS4_DSS_DIR_MODE) == -1 && errno != EEXIST) {
807cee86682Scalum 		syslog(LOG_ERR, "error creating stable storage directory: "
808cee86682Scalum 		    "%s: %s. Exiting", strerror(errno), tmppath);
809cee86682Scalum 		exit(1);
810cee86682Scalum 	}
811cee86682Scalum }
812cee86682Scalum 
813cee86682Scalum /*
814cee86682Scalum  * Create the storage dirs, and pass the path list to the kernel.
815cee86682Scalum  * This requires the nfssrv module to be loaded; the _nfssys() syscall
816cee86682Scalum  * will fail ENOTSUP if it is not.
817cee86682Scalum  * Use libnvpair(3LIB) to pass the data to the kernel.
818cee86682Scalum  */
819cee86682Scalum static int
820cee86682Scalum dss_init(uint_t npaths, char **pathnames)
821cee86682Scalum {
822cee86682Scalum 	int i, j, nskipped, error;
823cee86682Scalum 	char *bufp;
824cee86682Scalum 	uint32_t bufsize;
825cee86682Scalum 	size_t buflen;
826cee86682Scalum 	nvlist_t *nvl;
827cee86682Scalum 
828cee86682Scalum 	if (npaths > 1) {
829cee86682Scalum 		/*
830cee86682Scalum 		 * We need to remove duplicate paths; this might be user error
831cee86682Scalum 		 * in the general case, but HA-NFSv4 can also cause this.
832cee86682Scalum 		 * Sort the pathnames array, and NULL out duplicates,
833cee86682Scalum 		 * then write the non-NULL entries to a new array.
834cee86682Scalum 		 * Sorting will also allow the kernel to optimise its searches.
835cee86682Scalum 		 */
836cee86682Scalum 
837cee86682Scalum 		qsort(pathnames, npaths, sizeof (char *), qstrcmp);
838cee86682Scalum 
839cee86682Scalum 		/* now NULL out any duplicates */
840cee86682Scalum 		i = 0; j = 1; nskipped = 0;
841cee86682Scalum 		while (j < npaths) {
842cee86682Scalum 			if (strcmp(pathnames[i], pathnames[j]) == NULL) {
843cee86682Scalum 				pathnames[j] = NULL;
844cee86682Scalum 				j++;
845cee86682Scalum 				nskipped++;
846cee86682Scalum 				continue;
847cee86682Scalum 			}
848cee86682Scalum 
849cee86682Scalum 			/* skip i over any of its NULLed duplicates */
850cee86682Scalum 			i = j++;
851cee86682Scalum 		}
852cee86682Scalum 
853cee86682Scalum 		/* finally, write the non-NULL entries to a new array */
854cee86682Scalum 		if (nskipped > 0) {
855cee86682Scalum 			int nreal;
856cee86682Scalum 			size_t sz;
857cee86682Scalum 			char **tmp_pathnames;
858cee86682Scalum 
859cee86682Scalum 			nreal = npaths - nskipped;
860cee86682Scalum 
861cee86682Scalum 			sz = nreal * sizeof (char *);
862cee86682Scalum 			tmp_pathnames = (char **)malloc(sz);
863cee86682Scalum 			if (tmp_pathnames == NULL) {
864cee86682Scalum 				syslog(LOG_ERR, "tmp_pathnames malloc failed");
865cee86682Scalum 				exit(1);
866cee86682Scalum 			}
867cee86682Scalum 
868cee86682Scalum 			for (i = 0, j = 0; i < npaths; i++)
869cee86682Scalum 				if (pathnames[i] != NULL)
870cee86682Scalum 					tmp_pathnames[j++] = pathnames[i];
871cee86682Scalum 			free(pathnames);
872cee86682Scalum 			pathnames = tmp_pathnames;
873cee86682Scalum 			npaths = nreal;
874cee86682Scalum 		}
875cee86682Scalum 
876cee86682Scalum 	}
877cee86682Scalum 
878cee86682Scalum 	/* Create directories to store the distributed state files */
879cee86682Scalum 	dss_mkleafdirs(npaths, pathnames);
880cee86682Scalum 
881cee86682Scalum 	/* Create the name-value pair list */
882cee86682Scalum 	error = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0);
883cee86682Scalum 	if (error) {
884cee86682Scalum 		syslog(LOG_ERR, "nvlist_alloc failed: %s.", strerror(errno));
885cee86682Scalum 		return (1);
886cee86682Scalum 	}
887cee86682Scalum 
888cee86682Scalum 	/* Add the pathnames array as a single name-value pair */
889cee86682Scalum 	error = nvlist_add_string_array(nvl, NFS4_DSS_NVPAIR_NAME,
890cee86682Scalum 	    pathnames, npaths);
891cee86682Scalum 	if (error) {
892cee86682Scalum 		syslog(LOG_ERR, "nvlist_add_string_array failed: %s.",
893cee86682Scalum 		    strerror(errno));
894cee86682Scalum 		nvlist_free(nvl);
895cee86682Scalum 		return (1);
896cee86682Scalum 	}
897cee86682Scalum 
898cee86682Scalum 	/*
899cee86682Scalum 	 * Pack list into contiguous memory, for passing to kernel.
900cee86682Scalum 	 * nvlist_pack() will allocate the memory for the buffer,
901cee86682Scalum 	 * which we should free() when no longer needed.
902cee86682Scalum 	 * NV_ENCODE_XDR for safety across ILP32/LP64 kernel boundary.
903cee86682Scalum 	 */
904cee86682Scalum 	bufp = NULL;
905cee86682Scalum 	error = nvlist_pack(nvl, &bufp, &buflen, NV_ENCODE_XDR, 0);
906cee86682Scalum 	if (error) {
907cee86682Scalum 		syslog(LOG_ERR, "nvlist_pack failed: %s.", strerror(errno));
908cee86682Scalum 		nvlist_free(nvl);
909cee86682Scalum 		return (1);
910cee86682Scalum 	}
911cee86682Scalum 
912cee86682Scalum 	/* Now we have the packed buffer, we no longer need the list */
913cee86682Scalum 	nvlist_free(nvl);
914cee86682Scalum 
915cee86682Scalum 	/*
916cee86682Scalum 	 * Let the kernel know in advance how big the buffer is.
917cee86682Scalum 	 * NOTE: we cannot just pass buflen, since size_t is a long, and
918cee86682Scalum 	 * thus a different size between ILP32 userland and LP64 kernel.
919cee86682Scalum 	 * Use an int for the transfer, since that should be big enough;
920cee86682Scalum 	 * this is a no-op at the moment, here, since nfsd is 32-bit, but
921cee86682Scalum 	 * that could change.
922cee86682Scalum 	 */
923cee86682Scalum 	bufsize = (uint32_t)buflen;
924cee86682Scalum 	error = _nfssys(NFS4_DSS_SETPATHS_SIZE, &bufsize);
925cee86682Scalum 	if (error) {
926cee86682Scalum 		syslog(LOG_ERR,
927cee86682Scalum 		    "_nfssys(NFS4_DSS_SETPATHS_SIZE) failed: %s. ",
928cee86682Scalum 		    strerror(errno));
929cee86682Scalum 		free(bufp);
930cee86682Scalum 		return (1);
931cee86682Scalum 	}
932cee86682Scalum 
933cee86682Scalum 	/* Pass the packed buffer to the kernel */
934cee86682Scalum 	error = _nfssys(NFS4_DSS_SETPATHS, bufp);
935cee86682Scalum 	if (error) {
936cee86682Scalum 		syslog(LOG_ERR,
937cee86682Scalum 		    "_nfssys(NFS4_DSS_SETPATHS) failed: %s. ", strerror(errno));
938cee86682Scalum 		free(bufp);
939cee86682Scalum 		return (1);
940cee86682Scalum 	}
941cee86682Scalum 
942cee86682Scalum 	/*
943cee86682Scalum 	 * The kernel has now unpacked the buffer and extracted the
944cee86682Scalum 	 * pathnames array, we no longer need the buffer.
945cee86682Scalum 	 */
946cee86682Scalum 	free(bufp);
947cee86682Scalum 
948cee86682Scalum 	return (0);
949cee86682Scalum }
950cee86682Scalum 
951cee86682Scalum /*
952cee86682Scalum  * Quick sort string compare routine, for qsort.
953cee86682Scalum  * Needed to make arg types correct.
954cee86682Scalum  */
955cee86682Scalum int
956cee86682Scalum qstrcmp(const void *p1, const void *p2)
957cee86682Scalum {
958cee86682Scalum 	char *s1 = *((char **)p1);
959cee86682Scalum 	char *s2 = *((char **)p2);
960cee86682Scalum 
961cee86682Scalum 	return (strcmp(s1, s2));
962cee86682Scalum }
963