[Ocfs2-tools-devel] [PATCH 3/3] debugfs: Code cleanup

Sunil Mushran sunil.mushran at oracle.com
Mon Feb 25 12:27:05 PST 2008


Consolidated code dealing with the debugfs fs to utils.c.

Signed-off-by: Sunil Mushran <sunil.mushran at oracle.com>
---
 debugfs.ocfs2/dump_dlm_locks.c |   74 ++-------------------------------------
 debugfs.ocfs2/dump_fs_locks.c  |   74 ++-------------------------------------
 debugfs.ocfs2/include/utils.h  |    7 +++-
 debugfs.ocfs2/utils.c          |   70 ++++++++++++++++++++++++++++++++++++-
 4 files changed, 81 insertions(+), 144 deletions(-)

diff --git a/debugfs.ocfs2/dump_dlm_locks.c b/debugfs.ocfs2/dump_dlm_locks.c
index c7b964a..9b6e620 100644
--- a/debugfs.ocfs2/dump_dlm_locks.c
+++ b/debugfs.ocfs2/dump_dlm_locks.c
@@ -416,91 +416,25 @@ static int get_next_dlm_lockname(FILE *file, char *name, int len)
 	return 0;
 }
 
-#define DEBUGFS_MAGIC   0x64626720
-static errcode_t try_debugfs_path(const char *path)
-{
-	errcode_t ret;
-	struct stat64 stat_buf;
-	struct statfs64 statfs_buf;
-
-	ret = stat64(path, &stat_buf);
-	if (ret || !S_ISDIR(stat_buf.st_mode))
-		return O2CB_ET_SERVICE_UNAVAILABLE;
-	ret = statfs64(path, &statfs_buf);
-	if (ret || (statfs_buf.f_type != DEBUGFS_MAGIC))
-		return O2CB_ET_SERVICE_UNAVAILABLE;
-
-	return 0;
-}
-
-#define DLM_LOCKING_STATE_FORMAT_PATH "%s/o2dlm/%s/locking_state"
-static errcode_t open_dlm_locking_state(const char *debugfs_path,
-					const char *uuid_str,
-					FILE **state_file)
-{
-	errcode_t ret = 0;
-	char path[PATH_MAX];
-
-	ret = snprintf(path, PATH_MAX - 1, DLM_LOCKING_STATE_FORMAT_PATH,
-		       debugfs_path, uuid_str);
-	if ((ret <= 0) || (ret == (PATH_MAX - 1)))
-		return O2CB_ET_INTERNAL_FAILURE;
-
-	*state_file = fopen(path, "r");
-	if (!*state_file) {
-		switch (errno) {
-			default:
-				ret = O2CB_ET_INTERNAL_FAILURE;
-				break;
-
-			case ENOTDIR:
-			case ENOENT:
-			case EISDIR:
-				ret = O2CB_ET_SERVICE_UNAVAILABLE;
-				break;
-
-			case EACCES:
-			case EPERM:
-			case EROFS:
-				ret = O2CB_ET_PERMISSION_DENIED;
-				break;
-		}
-		goto out;
-	}
-
-	ret = 0;
-out:
-	return ret;
-}
-
-#define SYSFS_BASE		"/sys/kernel/"
-#define DEBUGFS_PATH		SYSFS_BASE "debug"
-#define DEBUGFS_ALTERNATE_PATH	"/debug"
-
 void dump_dlm_locks(char *uuid, FILE *out, int dump_lvbs,
 		    struct list_head *locklist)
 {
 	errcode_t ret;
-	int err;
-	const char *debugfs_path = DEBUGFS_PATH;
-	struct stat64 stat_buf;
+	char debugfs_path[PATH_MAX];
 	FILE *file;
 	char name[OCFS2_LOCK_ID_MAX_LEN];
 	struct lockres res;
 	int show_all_locks = 0;
 
-	err = stat64(SYSFS_BASE, &stat_buf);
-	if (err)
-		debugfs_path = DEBUGFS_ALTERNATE_PATH;
-
-	ret = try_debugfs_path(debugfs_path);
+	ret = get_debugfs_path(debugfs_path, sizeof(debugfs_path));
 	if (ret) {
 		fprintf(stderr, "Could not locate debugfs file system. "
 			"Perhaps it is not mounted?\n");
 		return;
 	}
 
-	ret = open_dlm_locking_state(debugfs_path, uuid, &file);
+	ret = open_debugfs_file(debugfs_path, "o2dlm", uuid, "locking_state",
+				&file);
 	if (ret) {
 		fprintf(stderr, "Could not open debug state for \"%s\".\n"
 			"Perhaps that OCFS2 file system is not mounted?\n",
diff --git a/debugfs.ocfs2/dump_fs_locks.c b/debugfs.ocfs2/dump_fs_locks.c
index 28059d0..3f05cde 100644
--- a/debugfs.ocfs2/dump_fs_locks.c
+++ b/debugfs.ocfs2/dump_fs_locks.c
@@ -343,87 +343,21 @@ static int dump_one_lockres(FILE *file, FILE *out, int lvbs, int only_busy)
 	return ret;
 }
 
-#define DEBUGFS_MAGIC   0x64626720
-static errcode_t try_debugfs_path(const char *path)
-{
-	errcode_t ret;
-	struct stat64 stat_buf;
-	struct statfs64 statfs_buf;
-
-	ret = stat64(path, &stat_buf);
-	if (ret || !S_ISDIR(stat_buf.st_mode))
-		return O2CB_ET_SERVICE_UNAVAILABLE;
-	ret = statfs64(path, &statfs_buf);
-	if (ret || (statfs_buf.f_type != DEBUGFS_MAGIC))
-		return O2CB_ET_SERVICE_UNAVAILABLE;
-
-	return 0;
-}
-
-#define LOCKING_STATE_FORMAT_PATH "%s/ocfs2/%s/locking_state"
-static errcode_t open_locking_state(const char *debugfs_path,
-				    const char *uuid_str,
-				    FILE **state_file)
-{
-	errcode_t ret = 0;
-	char path[PATH_MAX];
-
-	ret = snprintf(path, PATH_MAX - 1, LOCKING_STATE_FORMAT_PATH,
-		       debugfs_path, uuid_str);
-	if ((ret <= 0) || (ret == (PATH_MAX - 1)))
-		return O2CB_ET_INTERNAL_FAILURE;
-
-	*state_file = fopen(path, "r");
-	if (!*state_file) {
-		switch (errno) {
-			default:
-				ret = O2CB_ET_INTERNAL_FAILURE;
-				break;
-
-			case ENOTDIR:
-			case ENOENT:
-			case EISDIR:
-				ret = O2CB_ET_SERVICE_UNAVAILABLE;
-				break;
-
-			case EACCES:
-			case EPERM:
-			case EROFS:
-				ret = O2CB_ET_PERMISSION_DENIED;
-				break;
-		}
-		goto out;
-	}
-
-	ret = 0;
-out:
-	return ret;
-}
-
-#define SYSFS_BASE		"/sys/kernel/"
-#define DEBUGFS_PATH		SYSFS_BASE "debug"
-#define DEBUGFS_ALTERNATE_PATH	"/debug"
-
 void dump_fs_locks(char *uuid_str, FILE *out, int dump_lvbs, int only_busy)
 {
 	errcode_t ret;
-	int err;
-	const char *debugfs_path = DEBUGFS_PATH;
-	struct stat64 stat_buf;
+	char debugfs_path[PATH_MAX];
 	FILE *file;
 
-	err = stat64(SYSFS_BASE, &stat_buf);
-	if (err)
-		debugfs_path = DEBUGFS_ALTERNATE_PATH;
-
-	ret = try_debugfs_path(debugfs_path);
+	ret = get_debugfs_path(debugfs_path, sizeof(debugfs_path));
 	if (ret) {
 		fprintf(stderr, "Could not locate debugfs file system. "
 			"Perhaps it is not mounted?\n");
 		return;
 	}
 
-	ret = open_locking_state(debugfs_path, uuid_str, &file);
+	ret = open_debugfs_file(debugfs_path, "ocfs2", uuid_str,
+				"locking_state", &file);
 	if (ret) {
 		fprintf(stderr, "Could not open debug state for \"%s\".\n"
 			"Perhaps that OCFS2 file system is not mounted?\n",
diff --git a/debugfs.ocfs2/include/utils.h b/debugfs.ocfs2/include/utils.h
index b5f0c0c..1d71c11 100644
--- a/debugfs.ocfs2/include/utils.h
+++ b/debugfs.ocfs2/include/utils.h
@@ -3,7 +3,7 @@
  *
  * Function prototypes, macros, etc. for related 'C' files
  *
- * Copyright (C) 2004 Oracle.  All rights reserved.
+ * Copyright (C) 2004, 2008 Oracle.  All rights reserved.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public
@@ -20,7 +20,6 @@
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 021110-1307, USA.
  *
- * Authors: Sunil Mushran
  */
 
 #ifndef __UTILS_H__
@@ -57,4 +56,8 @@ errcode_t rdump_inode(ocfs2_filesys *fs, uint64_t blkno, const char *name,
 void crunch_strsplit(char **args);
 void find_max_contig_free_bits(struct ocfs2_group_desc *gd, int *max_contig_free_bits);
 
+errcode_t get_debugfs_path(char *debugfs_path, int len);
+errcode_t open_debugfs_file(const char *debugfs_path, const char *dirname,
+			    const char *uuid, const char *filename, FILE **fd);
+
 #endif		/* __UTILS_H__ */
diff --git a/debugfs.ocfs2/utils.c b/debugfs.ocfs2/utils.c
index a980362..21d32f0 100644
--- a/debugfs.ocfs2/utils.c
+++ b/debugfs.ocfs2/utils.c
@@ -3,7 +3,7 @@
  *
  * utility functions
  *
- * Copyright (C) 2004 Oracle.  All rights reserved.
+ * Copyright (C) 2004, 2008 Oracle.  All rights reserved.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public
@@ -20,7 +20,6 @@
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 021110-1307, USA.
  *
- * Authors: Sunil Mushran
  */
 
 #include "main.h"
@@ -850,3 +849,70 @@ void find_max_contig_free_bits(struct ocfs2_group_desc *gd, int *max_contig_free
 			*max_contig_free_bits = free_bits;
 	}
 }
+
+#define SYSFS_BASE		"/sys/kernel/"
+#define DEBUGFS_PATH		SYSFS_BASE "debug"
+#define DEBUGFS_ALTERNATE_PATH	"/debug"
+#define DEBUGFS_MAGIC		0x64626720
+errcode_t get_debugfs_path(char *debugfs_path, int len)
+{
+	errcode_t ret;
+	int err;
+	struct stat64 stat_buf;
+	struct statfs64 statfs_buf;
+	char *path = DEBUGFS_PATH;
+
+	err = stat64(SYSFS_BASE, &stat_buf);
+	if (err)
+		path = DEBUGFS_ALTERNATE_PATH;
+
+	ret = stat64(path, &stat_buf);
+	if (ret || !S_ISDIR(stat_buf.st_mode))
+		return O2CB_ET_SERVICE_UNAVAILABLE;
+
+	ret = statfs64(path, &statfs_buf);
+	if (ret || (statfs_buf.f_type != DEBUGFS_MAGIC))
+		return O2CB_ET_SERVICE_UNAVAILABLE;
+
+	strncpy(debugfs_path, path, len);
+
+	return 0;
+}
+
+errcode_t open_debugfs_file(const char *debugfs_path, const char *dirname,
+			    const char *uuid, const char *filename, FILE **fd)
+{
+	errcode_t ret = 0;
+	char path[PATH_MAX];
+
+	ret = snprintf(path, PATH_MAX - 1, "%s/%s/%s/%s",
+		       debugfs_path, dirname, uuid, filename);
+	if ((ret <= 0) || (ret == (PATH_MAX - 1)))
+		return O2CB_ET_INTERNAL_FAILURE;
+
+	*fd = fopen(path, "r");
+	if (!*fd) {
+		switch (errno) {
+			default:
+				ret = O2CB_ET_INTERNAL_FAILURE;
+				break;
+
+			case ENOTDIR:
+			case ENOENT:
+			case EISDIR:
+				ret = O2CB_ET_SERVICE_UNAVAILABLE;
+				break;
+
+			case EACCES:
+			case EPERM:
+			case EROFS:
+				ret = O2CB_ET_PERMISSION_DENIED;
+				break;
+		}
+		goto out;
+	}
+
+	ret = 0;
+out:
+	return ret;
+}
-- 
1.5.2.5




More information about the Ocfs2-tools-devel mailing list