All of lore.kernel.org
 help / color / mirror / Atom feed
From: Steve Dickson <steved@redhat.com>
To: Linux NFS Mailing list <linux-nfs@vger.kernel.org>
Subject: [PATCH 3/7] exportd: multiple threads
Date: Fri, 19 Feb 2021 15:08:11 -0500	[thread overview]
Message-ID: <20210219200815.792667-4-steved@redhat.com> (raw)
In-Reply-To: <20210219200815.792667-1-steved@redhat.com>

Ported the multiple thread code from mountd (commit 11d34d11)

Signed-off-by: Steve Dickson <steved@redhat.com>
---
 nfs.conf                  |   3 +
 systemd/nfs.conf.man      |   9 +++
 utils/exportd/exportd.c   | 118 ++++++++++++++++++++++++++++++++++++--
 utils/exportd/exportd.man |   7 +++
 4 files changed, 133 insertions(+), 4 deletions(-)

diff --git a/nfs.conf b/nfs.conf
index 9fcf1bf..4b344fa 100644
--- a/nfs.conf
+++ b/nfs.conf
@@ -29,6 +29,9 @@
 # port=0
 # udp-port=0
 #
+[exportd]
+# debug="all|auth|call|general|parse"
+# threads=1
 [mountd]
 # debug="all|auth|call|general|parse"
 # manage-gids=n
diff --git a/systemd/nfs.conf.man b/systemd/nfs.conf.man
index 16e0ec4..a4379fd 100644
--- a/systemd/nfs.conf.man
+++ b/systemd/nfs.conf.man
@@ -128,6 +128,15 @@ then the client will be able to mount the path as
 but on the server, this will resolve to the path
 .BR /my/root/filesystem .
 
+.TP
+.B exportd
+Recognized values:
+.B threads
+
+See
+.BR exportd (8)
+for details.
+
 .TP
 .B nfsdcltrack
 Recognized values:
diff --git a/utils/exportd/exportd.c b/utils/exportd/exportd.c
index 2f67e3b..c814503 100644
--- a/utils/exportd/exportd.c
+++ b/utils/exportd/exportd.c
@@ -15,6 +15,8 @@
 #include <signal.h>
 #include <string.h>
 #include <getopt.h>
+#include <errno.h>
+#include <wait.h>
 
 #include "nfslib.h"
 #include "conffile.h"
@@ -26,6 +28,13 @@ extern void my_svc_run(void);
 struct state_paths etab;
 struct state_paths rmtab;
 
+/* Number of mountd threads to start.   Default is 1 and
+ * that's probably enough unless you need hundreds of
+ * clients to be able to mount at once.  */
+static int num_threads = 1;
+/* Arbitrary limit on number of threads */
+#define MAX_THREADS 64
+
 int manage_gids;
 int use_ipaddr = -1;
 
@@ -34,6 +43,7 @@ static struct option longopts[] =
 	{ "foreground", 0, 0, 'F' },
 	{ "debug", 1, 0, 'd' },
 	{ "help", 0, 0, 'h' },
+	{ "num-threads", 1, 0, 't' },
 	{ NULL, 0, 0, 0 }
 };
 
@@ -42,10 +52,85 @@ static struct option longopts[] =
  */
 inline static void set_signals(void);
 
+/* Wait for all worker child processes to exit and reap them */
+static void
+wait_for_workers (void)
+{
+	int status;
+	pid_t pid;
+
+	for (;;) {
+
+		pid = waitpid(0, &status, 0);
+
+		if (pid < 0) {
+			if (errno == ECHILD)
+				return; /* no more children */
+			xlog(L_FATAL, "mountd: can't wait: %s\n",
+					strerror(errno));
+		}
+
+		/* Note: because we SIG_IGN'd SIGCHLD earlier, this
+		 * does not happen on 2.6 kernels, and waitpid() blocks
+		 * until all the children are dead then returns with
+		 * -ECHILD.  But, we don't need to do anything on the
+		 * death of individual workers, so we don't care. */
+		xlog(L_NOTICE, "mountd: reaped child %d, status %d\n",
+				(int)pid, status);
+	}
+}
+
+/* Fork num_threads worker children and wait for them */
+static void
+fork_workers(void)
+{
+	int i;
+	pid_t pid;
+
+	xlog(L_NOTICE, "mountd: starting %d threads\n", num_threads);
+
+	for (i = 0 ; i < num_threads ; i++) {
+		pid = fork();
+		if (pid < 0) {
+			xlog(L_FATAL, "mountd: cannot fork: %s\n",
+					strerror(errno));
+		}
+		if (pid == 0) {
+			/* worker child */
+
+			/* Re-enable the default action on SIGTERM et al
+			 * so that workers die naturally when sent them.
+			 * Only the parent unregisters with pmap and
+			 * hence needs to do special SIGTERM handling. */
+			struct sigaction sa;
+			sa.sa_handler = SIG_DFL;
+			sa.sa_flags = 0;
+			sigemptyset(&sa.sa_mask);
+			sigaction(SIGHUP, &sa, NULL);
+			sigaction(SIGINT, &sa, NULL);
+			sigaction(SIGTERM, &sa, NULL);
+
+			/* fall into my_svc_run in caller */
+			return;
+		}
+	}
+
+	/* in parent */
+	wait_for_workers();
+	xlog(L_NOTICE, "exportd: no more workers, exiting\n");
+	exit(0);
+}
+
 static void 
 killer (int sig)
 {
+	if (num_threads > 1) {
+		/* play Kronos and eat our children */
+		kill(0, SIGTERM);
+		wait_for_workers();
+	}
 	xlog (L_NOTICE, "Caught signal %d, exiting.", sig);
+
 	exit(0);
 }
 static void
@@ -78,10 +163,20 @@ static void
 usage(const char *prog, int n)
 {
 	fprintf(stderr,
-		"Usage: %s [-f|--foreground] [-h|--help] [-d kind|--debug kind]\n", prog);
+		"Usage: %s [-f|--foreground] [-h|--help] [-d kind|--debug kind]\n"
+"	[-t num|--num-threads=num]\n", prog);
 	exit(n);
 }
 
+inline static void 
+read_exportd_conf(char *progname)
+{
+	conf_init_file(NFS_CONFFILE);
+
+	xlog_set_debug(progname);
+
+	num_threads = conf_get_num("exportd", "threads", num_threads);
+}
 int
 main(int argc, char **argv)
 {
@@ -98,10 +193,10 @@ main(int argc, char **argv)
 	/* Initialize logging. */
 	xlog_open(progname);
 
-	conf_init_file(NFS_CONFFILE);
-	xlog_set_debug(progname);
+	/* Read in config setting */
+	read_exportd_conf(progname);
 
-	while ((c = getopt_long(argc, argv, "d:fh", longopts, NULL)) != EOF) {
+	while ((c = getopt_long(argc, argv, "d:fht:", longopts, NULL)) != EOF) {
 		switch (c) {
 		case 'd':
 			xlog_sconfig(optarg, 1);
@@ -112,6 +207,9 @@ main(int argc, char **argv)
 		case 'h':
 			usage(progname, 0);
 			break;
+		case 't':
+			num_threads = atoi (optarg);
+			break;
 		case '?':
 		default:
 			usage(progname, 1);
@@ -132,6 +230,18 @@ main(int argc, char **argv)
 	set_signals();
 	daemon_ready();
 
+	/* silently bounds check num_threads */
+	if (foreground)
+		num_threads = 1;
+	else if (num_threads < 1)
+		num_threads = 1;
+	else if (num_threads > MAX_THREADS)
+		num_threads = MAX_THREADS;
+
+	if (num_threads > 1)
+		fork_workers();
+
+
 	/* Open files now to avoid sharing descriptors among forked processes */
 	cache_open();
 
diff --git a/utils/exportd/exportd.man b/utils/exportd/exportd.man
index d786e57..1d65b5e 100644
--- a/utils/exportd/exportd.man
+++ b/utils/exportd/exportd.man
@@ -44,6 +44,13 @@ Run in foreground (do not daemonize)
 .TP
 .B \-h " or " \-\-help
 Display usage message.
+.TP
+.BR "\-t N" " or " "\-\-num\-threads=N " or  " \-\-num\-threads N "
+This option specifies the number of worker threads that rpc.mountd
+spawns.  The default is 1 thread, which is probably enough.  More
+threads are usually only needed for NFS servers which need to handle
+mount storms of hundreds of NFS mounts in a few seconds, or when
+your DNS server is slow or unreliable.
 .SH CONFIGURATION FILE
 Many of the options that can be set on the command line can also be
 controlled through values set in the
-- 
2.29.2


  parent reply	other threads:[~2021-02-19 20:08 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-19 20:08 [PATCH 0/7 V4] The NFSv4 only mounting daemon Steve Dickson
2021-02-19 20:08 ` [PATCH 1/7] exportd: the initial shell of the v4 export support Steve Dickson
2021-02-19 20:08 ` [PATCH 2/7] exportd: Moved cache upcalls routines into libexport.a Steve Dickson
2021-02-23 16:13   ` [PATCH] exportd: server-side gid management Daniel Kobras
2021-03-04 21:28     ` Steve Dickson
2021-02-19 20:08 ` Steve Dickson [this message]
2021-02-19 20:08 ` [PATCH 4/7] exportd/exportfs: Add the state-directory-path option Steve Dickson
2021-02-19 20:08 ` [PATCH 5/7] exportd: Enabled junction support Steve Dickson
2021-02-19 20:08 ` [PATCH 6/7] exportd: systemd unit files Steve Dickson
2021-02-19 20:08 ` [PATCH 7/7] exportd: Added config variable to compile in the NFSv4 only server Steve Dickson
2021-02-20 16:33 ` [PATCH 0/7 V4] The NFSv4 only mounting daemon Steve Dickson
2021-02-24 20:30 ` J. Bruce Fields
2021-03-02 22:33   ` Steve Dickson
2021-03-03 15:23     ` J. Bruce Fields
2021-03-03 21:22       ` Steve Dickson
2021-03-03 21:54         ` J. Bruce Fields
2021-03-03 22:07           ` Steve Dickson
2021-03-03 22:17             ` J. Bruce Fields
2021-03-04 13:57               ` Steve Dickson
2021-03-04 14:06                 ` J. Bruce Fields
2021-03-04 16:31                   ` Steve Dickson
2021-03-05 14:36                     ` J. Bruce Fields
2021-03-05 15:53                       ` Chuck Lever
2021-03-04 13:42           ` Steve Dickson
2021-03-04 14:01             ` J. Bruce Fields
2021-03-04 16:47               ` Steve Dickson
2021-03-04 21:31               ` Patrick Goetz
2021-03-04 13:34       ` Steve Dickson
2021-03-04 14:24         ` J. Bruce Fields
2021-03-04 16:20           ` Steve Dickson
2021-02-24 20:49 ` J. Bruce Fields
2021-03-02 22:39   ` Steve Dickson
2021-03-03 18:10     ` Chuck Lever
2021-03-03 21:24       ` Steve Dickson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210219200815.792667-4-steved@redhat.com \
    --to=steved@redhat.com \
    --cc=linux-nfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.