Skip to contentSkip to conversation

xfs_healer: update weakhandle mountpoint when reconnecting

PATCH In review Darrick J. Wong 3 participants 3 messages Apr 13 – Apr 15

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs_healer: update weakhandle mountpoint when reconnecting

From: Darrick J. Wong <[email protected]>

If we have to reconnect a weakhandle to a different mount point, we should update the internal mountpoint string to that new value so we don't have to keep reconnecting it.

---
 healer/weakhandle.c |   34 +++++++++++++++++++++++++++++++---
 1 file changed, 31 insertions(+), 3 deletions(-)
healer/weakhandle.c +31 -3
index 7b8cef0a63f971..d659cbd63b5176 100644
@@ -18,8 +18,8 @@
 #include "xfs_healer.h"
 
 struct weakhandle {
-	/* Shared reference to the user's mountpoint for logging */
-	const char		*mntpoint;
+	/* Owned reference to the user's mountpoint for logging */
+	char			*mntpoint;
 
 	/* Shared reference to the getmntent fsname for reconnecting */
 	const char		*fsname;
@@ -55,7 +55,9 @@ weakhandle_alloc(
 	if (!wh)
 		return -1;
 
-	wh->mntpoint = mountpoint;
+	wh->mntpoint = strdup(mountpoint);
+	if (!wh->mntpoint)
+		goto out_wh;
 	wh->mnt_id = mnt_id;
 	wh->fsname = fsname;
 
@@ -71,6 +73,28 @@ weakhandle_alloc(
 	return -1;
 }
 
+static void
+try_update_mntpoint(
+	struct weakhandle	*wh,
+	const char		*path)
+{
+	static pthread_mutex_t	lock = PTHREAD_MUTEX_INITIALIZER;
+	char			*s = strdup(path);
+
+	if (!s)
+		return;
+
+	pthread_mutex_lock(&lock);
+	if (path != wh->mntpoint) {
+		free(wh->mntpoint);
+		wh->mntpoint = s;
+		s = NULL;
+	}
+	pthread_mutex_unlock(&lock);
+
+	free(s);
+}
+
 /*
  * Reopen a file handle obtained via weak reference, using the given path to a
  * mount point.
@@ -108,6 +132,9 @@ weakhandle_reopen_from(
 		goto out_handle;
 	}
 
+	if (path != wh->mntpoint)
+		try_update_mntpoint(wh, path);
+
 	free_handle(hanp, hlen);
 	*fd = mnt_fd;
 	return 0;
@@ -193,6 +220,7 @@ weakhandle_free(
 
 	if (wh) {
 		free_handle(wh->hanp, wh->hlen);
+		free(wh->mntpoint);
 		free(wh);
 	}