Blame SOURCES/0059-Ticket-49298-force-sync-on-shutdown.patch

b69e47
From ba30cc562f5ebd58955502a19edbf9720a45b655 Mon Sep 17 00:00:00 2001
b69e47
From: Mark Reynolds <mreynolds@redhat.com>
b69e47
Date: Tue, 8 Aug 2017 13:02:53 -0400
b69e47
Subject: [PATCH] Ticket 49298 - force sync() on shutdown
b69e47
b69e47
        Bug Description:  During shutdown on xfs we would occasionally
b69e47
        see a broke dse.ldif (specifically, empty). This happens due to
b69e47
        a bug in xfs where the directory isn't synced on rename().
b69e47
b69e47
        Fix Description:  As we shutdown call sync() to force all our
b69e47
        writes to disk - dse.ldif, logs, db, all of it.
b69e47
b69e47
        https://pagure.io/389-ds-base/issue/49298
b69e47
---
b69e47
 ldap/servers/slapd/dse.c  | 59 +++++++++++++++++++++++++++++------------------
b69e47
 ldap/servers/slapd/main.c |  9 ++++----
b69e47
 2 files changed, 42 insertions(+), 26 deletions(-)
b69e47
b69e47
diff --git a/ldap/servers/slapd/dse.c b/ldap/servers/slapd/dse.c
b69e47
index 5715c83..fa1aacc 100644
b69e47
--- a/ldap/servers/slapd/dse.c
b69e47
+++ b/ldap/servers/slapd/dse.c
b69e47
@@ -40,6 +40,8 @@
b69e47
 #include "slap.h"
b69e47
 #include <pwd.h>
b69e47
 
b69e47
+#include <unistd.h> /* provides fsync/close */
b69e47
+
b69e47
 /* #define SLAPI_DSE_DEBUG */ 	/* define this to force trace log	*/
b69e47
 								/* messages to always be logged		*/
b69e47
 
b69e47
@@ -72,11 +74,11 @@
b69e47
 struct dse_callback
b69e47
 {
b69e47
     int operation;
b69e47
-	int flags;
b69e47
-	Slapi_DN *base;
b69e47
-	int scope;
b69e47
-	char *filter;				/* NULL means match all entries */
b69e47
-    Slapi_Filter *slapifilter;	/* NULL means match all entries */
b69e47
+    int flags;
b69e47
+    Slapi_DN *base;
b69e47
+    int scope;
b69e47
+    char *filter;               /* NULL means match all entries */
b69e47
+    Slapi_Filter *slapifilter;  /* NULL means match all entries */
b69e47
     int (*fn)(Slapi_PBlock *,Slapi_Entry *,Slapi_Entry *,int*,char*,void *);
b69e47
     void *fn_arg;
b69e47
     struct slapdplugin *plugin;
b69e47
@@ -89,13 +91,14 @@ struct dse
b69e47
     char *dse_tmpfile; /* and written to when changes are made via LDAP */
b69e47
     char *dse_fileback; /* contain the latest info, just before a new change */
b69e47
     char *dse_filestartOK; /* contain the latest info with which the server has successfully started */
b69e47
+    char *dse_configdir; /* The location of config files - allows us to fsync the dir post rename */
b69e47
     Avlnode *dse_tree;
b69e47
     struct dse_callback *dse_callback;
b69e47
     Slapi_RWLock *dse_rwlock; /* a read-write lock to protect the whole dse backend */
b69e47
-	char **dse_filelist; /* these are additional read only files used to */
b69e47
-						 /* initialize the dse */
b69e47
-	int  dse_is_updateable; /* if non-zero, this DSE can be written to */
b69e47
-	int  dse_readonly_error_reported; /* used to ensure that read-only errors are logged only once */
b69e47
+    char **dse_filelist; /* these are additional read only files used to */
b69e47
+                         /* initialize the dse */
b69e47
+    int  dse_is_updateable; /* if non-zero, this DSE can be written to */
b69e47
+    int  dse_readonly_error_reported; /* used to ensure that read-only errors are logged only once */
b69e47
 };
b69e47
 
b69e47
 struct dse_node
b69e47
@@ -361,37 +364,39 @@ dse_new( char *filename, char *tmpfilename, char *backfilename, char *startokfil
b69e47
 			if (!strstr(filename, realconfigdir))
b69e47
 			{
b69e47
 				pdse->dse_filename = slapi_ch_smprintf("%s/%s", realconfigdir, filename );
b69e47
-			}
b69e47
-			else
b69e47
+			} else {
b69e47
 				pdse->dse_filename = slapi_ch_strdup(filename);
b69e47
+			}
b69e47
 
b69e47
 			if (!strstr(tmpfilename, realconfigdir)) {
b69e47
 				pdse->dse_tmpfile = slapi_ch_smprintf("%s/%s", realconfigdir, tmpfilename );
b69e47
-			}
b69e47
-			else
b69e47
+			} else {
b69e47
 				pdse->dse_tmpfile = slapi_ch_strdup(tmpfilename);
b69e47
+			}
b69e47
+
b69e47
+			pdse->dse_configdir = slapi_ch_strdup(realconfigdir);
b69e47
 
b69e47
 			if ( backfilename != NULL )
b69e47
 			{
b69e47
 				if (!strstr(backfilename, realconfigdir)) {
b69e47
 					pdse->dse_fileback = slapi_ch_smprintf("%s/%s", realconfigdir, backfilename );
b69e47
-				}
b69e47
-				else
b69e47
+				} else {
b69e47
 					pdse->dse_fileback = slapi_ch_strdup(backfilename);
b69e47
-			}
b69e47
-			else
b69e47
+				}
b69e47
+			} else {
b69e47
 				pdse->dse_fileback = NULL;
b69e47
+            }
b69e47
 
b69e47
 			if ( startokfilename != NULL )
b69e47
 			{
b69e47
 				if (!strstr(startokfilename, realconfigdir)) {
b69e47
 					pdse->dse_filestartOK = slapi_ch_smprintf("%s/%s", realconfigdir, startokfilename );
b69e47
-				}
b69e47
-				else
b69e47
+				} else {
b69e47
 					pdse->dse_filestartOK = slapi_ch_strdup(startokfilename);
b69e47
-			}
b69e47
-			else
b69e47
+				}
b69e47
+			} else {
b69e47
 				pdse->dse_filestartOK = NULL;
b69e47
+			}
b69e47
 
b69e47
 			pdse->dse_tree= NULL;
b69e47
 			pdse->dse_callback= NULL;
b69e47
@@ -440,6 +445,7 @@ dse_destroy(struct dse *pdse)
b69e47
     slapi_ch_free((void **)&(pdse->dse_tmpfile));
b69e47
     slapi_ch_free((void **)&(pdse->dse_fileback));
b69e47
     slapi_ch_free((void **)&(pdse->dse_filestartOK));
b69e47
+    slapi_ch_free((void **)&(pdse->dse_configdir));
b69e47
     dse_callback_deletelist(&pdse->dse_callback);
b69e47
     charray_free(pdse->dse_filelist);
b69e47
     nentries = avl_free(pdse->dse_tree, dse_internal_delete_entry);
b69e47
@@ -991,8 +997,9 @@ dse_write_file_nolock(struct dse* pdse)
b69e47
     FPWrapper	fpw;
b69e47
     int rc = 0;
b69e47
 
b69e47
-	if (dont_ever_write_dse_files)
b69e47
+	if (dont_ever_write_dse_files) {
b69e47
 		return rc;
b69e47
+	}
b69e47
 
b69e47
     fpw.fpw_rc = 0;
b69e47
 	fpw.fpw_prfd = NULL;
b69e47
@@ -1042,6 +1049,14 @@ dse_write_file_nolock(struct dse* pdse)
b69e47
 							pdse->dse_tmpfile, pdse->dse_filename,
b69e47
 							rc, slapd_system_strerror( rc ));
b69e47
                 }
b69e47
+				/*
b69e47
+				 * We have now written to the tmp location, and renamed it
b69e47
+				 * we need to open and fsync the dir to make the rename stick.
b69e47
+				 */
b69e47
+				int fp_configdir = open(pdse->dse_configdir, O_PATH | O_DIRECTORY);
b69e47
+				fsync(fp_configdir);
b69e47
+				close(fp_configdir);
b69e47
+
b69e47
             }
b69e47
         }
b69e47
 		if (fpw.fpw_prfd)
b69e47
diff --git a/ldap/servers/slapd/main.c b/ldap/servers/slapd/main.c
b69e47
index ba1f5e8..3351464 100644
b69e47
--- a/ldap/servers/slapd/main.c
b69e47
+++ b/ldap/servers/slapd/main.c
b69e47
@@ -1154,11 +1154,12 @@ cleanup:
b69e47
 	ndn_cache_destroy();
b69e47
 	NSS_Shutdown();
b69e47
 	PR_Cleanup();
b69e47
-#if defined( hpux )
b69e47
-	exit( return_value );
b69e47
-#else
b69e47
+	/*
b69e47
+	 * Server has stopped, lets force everything to disk: logs
b69e47
+	 * db, dse.ldif, all of it.
b69e47
+	 */
b69e47
+	sync();
b69e47
 	return return_value;
b69e47
-#endif
b69e47
 }
b69e47
 
b69e47
 
b69e47
-- 
b69e47
2.9.4
b69e47