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

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