97374c0
From 929da557efea6c7d2340467d9a7fdae7fda6d2b1 Mon Sep 17 00:00:00 2001
97374c0
From: Juergen Gross <jgross@suse.com>
97374c0
Date: Tue, 13 Sep 2022 07:35:13 +0200
97374c0
Subject: tools/xenstore: make the internal memory data base the default
97374c0
97374c0
Having a file backed data base has the only advantage of being capable
97374c0
to dump the contents of it while Xenstore is running, and potentially
97374c0
using less swap space in case the data base can't be kept in memory.
97374c0
97374c0
It has the major disadvantage of a huge performance overhead: switching
97374c0
to keep the data base in memory only speeds up live update of xenstored
97374c0
with 120000 nodes from 20 minutes to 11 seconds. A complete tree walk
97374c0
of this configuration will be reduced from 7 seconds to 280 msecs
97374c0
(measured by "xenstore-control check").
97374c0
97374c0
So make the internal memory data base the default and enhance the
97374c0
"--internal-db" command line parameter to take an optional parameter
97374c0
allowing to switch the internal data base back to the file based one.
97374c0
97374c0
This is part of XSA-419.
97374c0
97374c0
Reported-by: Juergen Gross <jgross@suse.com>
97374c0
Signed-off-by: Juergen Gross <jgross@suse.com>
97374c0
Reviewed-by: Julien Grall <jgrall@amazon.com>
97374c0
97374c0
diff --git a/tools/helpers/init-xenstore-domain.c b/tools/helpers/init-xenstore-domain.c
97374c0
index 2d9ab6f1c583..04e351ca29a8 100644
97374c0
--- a/tools/helpers/init-xenstore-domain.c
97374c0
+++ b/tools/helpers/init-xenstore-domain.c
97374c0
@@ -222,9 +222,9 @@ static int build(xc_interface *xch)
97374c0
     }
97374c0
 
97374c0
     if ( param )
97374c0
-        snprintf(cmdline, 512, "--event %d --internal-db %s", rv, param);
97374c0
+        snprintf(cmdline, 512, "--event %d %s", rv, param);
97374c0
     else
97374c0
-        snprintf(cmdline, 512, "--event %d --internal-db", rv);
97374c0
+        snprintf(cmdline, 512, "--event %d", rv);
97374c0
 
97374c0
     dom->guest_domid = domid;
97374c0
     dom->cmdline = xc_dom_strdup(dom, cmdline);
97374c0
diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_core.c
97374c0
index 13e48aaa731c..36fb4a832834 100644
97374c0
--- a/tools/xenstore/xenstored_core.c
97374c0
+++ b/tools/xenstore/xenstored_core.c
97374c0
@@ -2308,7 +2308,7 @@ static void accept_connection(int sock)
97374c0
 }
97374c0
 #endif
97374c0
 
97374c0
-static int tdb_flags;
97374c0
+static int tdb_flags = TDB_INTERNAL | TDB_NOLOCK;
97374c0
 
97374c0
 /* We create initial nodes manually. */
97374c0
 static void manual_node(const char *name, const char *child)
97374c0
@@ -2618,7 +2618,8 @@ static void usage(void)
97374c0
 "                          watch-event: time a watch-event is kept pending\n"
97374c0
 "  -R, --no-recovery       to request that no recovery should be attempted when\n"
97374c0
 "                          the store is corrupted (debug only),\n"
97374c0
-"  -I, --internal-db       store database in memory, not on disk\n"
97374c0
+"  -I, --internal-db [on|off] store database in memory, not on disk, default is\n"
97374c0
+"                          memory, with \"--internal-db off\" it is on disk\n"
97374c0
 "  -K, --keep-orphans      don't delete nodes owned by a domain when the\n"
97374c0
 "                          domain is deleted (this is a security risk!)\n"
97374c0
 "  -V, --verbose           to request verbose execution.\n");
97374c0
@@ -2644,7 +2645,7 @@ static struct option options[] = {
97374c0
 	{ "quota-soft", 1, NULL, 'q' },
97374c0
 	{ "timeout", 1, NULL, 'w' },
97374c0
 	{ "no-recovery", 0, NULL, 'R' },
97374c0
-	{ "internal-db", 0, NULL, 'I' },
97374c0
+	{ "internal-db", 2, NULL, 'I' },
97374c0
 	{ "keep-orphans", 0, NULL, 'K' },
97374c0
 	{ "verbose", 0, NULL, 'V' },
97374c0
 	{ "watch-nb", 1, NULL, 'W' },
97374c0
@@ -2725,7 +2726,8 @@ int main(int argc, char *argv[])
97374c0
 	orig_argc = argc;
97374c0
 	orig_argv = argv;
97374c0
 
97374c0
-	while ((opt = getopt_long(argc, argv, "DE:F:HKNPS:t:A:M:Q:q:T:RVW:w:U",
97374c0
+	while ((opt = getopt_long(argc, argv,
97374c0
+				  "DE:F:HI::KNPS:t:A:M:Q:q:T:RVW:w:U",
97374c0
 				  options, NULL)) != -1) {
97374c0
 		switch (opt) {
97374c0
 		case 'D':
97374c0
@@ -2759,7 +2761,8 @@ int main(int argc, char *argv[])
97374c0
 			tracefile = optarg;
97374c0
 			break;
97374c0
 		case 'I':
97374c0
-			tdb_flags = TDB_INTERNAL|TDB_NOLOCK;
97374c0
+			if (optarg && !strcmp(optarg, "off"))
97374c0
+				tdb_flags = 0;
97374c0
 			break;
97374c0
 		case 'K':
97374c0
 			keep_orphans = true;