[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Cluster-devel] [PATCH] cman/qdisk: fix bugzilla 362031 (reworked)



Hi Lon,

this is a rework of the patch to fix $subject as we discussed on IRC last Friday.

We change the scan of /proc/partitions into scanning directly /dev and filtering
out a few dirs that are used by udev.

It is much less intrusive than the previous patch and it does everything we need
for the device scanning. It does not address yet the issue of a device
disappearing or multipath, but that requires a major code rework that we should
address with another BZ.

Please ACK or apply as you prefer.

Fabio

-- 
I'm going to make him an offer he can't refuse.
Index: disk.h
===================================================================
RCS file: /cvs/cluster/cluster/cman/qdisk/disk.h,v
retrieving revision 1.7
diff -u -r1.7 disk.h
--- disk.h	21 Feb 2007 20:24:30 -0000	1.7
+++ disk.h	12 Nov 2007 06:59:15 -0000
@@ -277,7 +277,7 @@
 void qd_destroy(qd_ctx *ctx);
 
 /* proc.c */
-int find_partitions(const char *partfile, const char *label,
+int find_partitions(const char *devdir, const char *label,
 		    char *devname, size_t devlen, int print);
 int check_device(char *device, char *label, quorum_header_t *qh);
 
Index: main.c
===================================================================
RCS file: /cvs/cluster/cluster/cman/qdisk/main.c,v
retrieving revision 1.11
diff -u -r1.11 main.c
--- main.c	1 Nov 2007 00:25:28 -0000	1.11
+++ main.c	12 Nov 2007 06:59:16 -0000
@@ -1491,7 +1491,8 @@
 	}
 	
 	if (ctx.qc_label) {
-		if (find_partitions("/proc/partitions",
+		memset(device, 0, sizeof(device));
+		if (find_partitions("/dev",
 				    ctx.qc_label, device,
 				    sizeof(device), 0) != 0) {
 			clulog_and_print(LOG_CRIT, "Unable to match label"
Index: mkqdisk.c
===================================================================
RCS file: /cvs/cluster/cluster/cman/qdisk/mkqdisk.c,v
retrieving revision 1.4
diff -u -r1.4 mkqdisk.c
--- mkqdisk.c	21 Nov 2006 14:50:49 -0000	1.4
+++ mkqdisk.c	12 Nov 2007 06:59:16 -0000
@@ -39,19 +39,19 @@
 	char *newdev = NULL, *newlabel = NULL;
 	int rv;
 
-	printf("mkqdisk v0.5.1\n");
+	printf("mkqdisk v" RELEASE_VERSION "\n\n");
 
 	while ((rv = getopt(argc, argv, "Lf:c:l:h")) != EOF) {
 		switch (rv) {
 		case 'L':
 			/* List */
 			close(2);
-			return find_partitions("/proc/partitions",
+			return find_partitions("/dev",
 					       NULL, NULL, 0, 1);
 			break;
 		case 'f':
 			close(2);
-			return find_partitions("/proc/partitions",
+			return find_partitions("/dev",
 					       optarg, device,
 					       sizeof(device), 1);
 		case 'c':
Index: proc.c
===================================================================
RCS file: /cvs/cluster/cluster/cman/qdisk/proc.c,v
retrieving revision 1.3
diff -u -r1.3 proc.c
--- proc.c	11 Sep 2007 12:38:08 -0000	1.3
+++ proc.c	12 Nov 2007 06:59:16 -0000
@@ -26,10 +26,12 @@
 #include <disk.h>
 #include <errno.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <platform.h>
 #include <stdlib.h>
 #include <string.h>
-
+#include <unistd.h>
+#include <dirent.h>
 
 int
 check_device(char *device, char *label, quorum_header_t *qh)
@@ -68,62 +70,61 @@
 
 
 int
-find_partitions(const char *partfile, const char *label,
+find_partitions(const char *devdir, const char *label,
 	        char *devname, size_t devlen, int print)
 {
-	char line[4096];
-	FILE *fp;
-	int minor, major;
-	unsigned long long blkcnt;
-	char device[128];
-	char realdev[256];
+	struct dirent **namelist;
+	struct stat sb;
+	char newpath[256];
+	int n;
 	quorum_header_t qh;
 
-	fp = fopen(partfile, "r");
-	if (!fp)
+	n = scandir(devdir, &namelist, 0, alphasort);
+	if (n <= 0)
 		return -1;
 
-	while (fgets(line, sizeof(line), fp) != NULL) {
-		if (strlen(line) > 128 + (22) /* 5 + 5 + 11 + 1 */) {
-			/*printf("Line too long!\n");*/
-			continue;
-		}
-
-		/* This line is taken from 2.6.15.4's proc line */
-		sscanf(line, "%4d %4d %10llu %s", &major, &minor,
-		       &blkcnt, device);
-
-		if (strlen(device)) {
-			snprintf(realdev, sizeof(realdev),
-				 "/dev/%s", device);
-			if (check_device(realdev, (char *)label, &qh) != 0)
-				continue;
-
-			if (print) {
-				time_t timestamp = qh.qh_timestamp;
-				printf("%s:\n", realdev);
-				printf("\tMagic:   %08x\n", qh.qh_magic);
-				printf("\tLabel:   %s\n", qh.qh_cluster);
-				printf("\tCreated: %s",
-				       ctime((time_t *)&timestamp));
-				printf("\tHost:    %s\n\n", qh.qh_updatehost);
-			}
-
-			if (devname && devlen) {
-				/* Got it */
-				strncpy(devname, realdev, devlen);
-				fclose(fp);
-				return 0;
+	while (n--) {
+		/* filter out:
+		 * . and ..
+		 * .static and .udev that are typical udev dirs that we don't want to scan
+		 */
+		if (strcmp(namelist[n]->d_name, ".") &&
+		    strcmp(namelist[n]->d_name, "..") &&
+		    strcmp(namelist[n]->d_name, ".static") &&
+		    strcmp(namelist[n]->d_name, ".udev")) {
+			snprintf(newpath, sizeof(newpath), "%s/%s", devdir, namelist[n]->d_name);
+			if (!lstat(newpath, &sb)) {
+				/* dive into directories */
+				if (S_ISDIR(sb.st_mode)) {
+					if (!find_partitions(newpath, label, devname, devlen, print)) {
+						if (devname && (strlen(devname) > 0))
+							return 0;
+					}
+				}
+				/* check if it's a block device */
+				if (S_ISBLK(sb.st_mode)) {
+					if (!check_device(newpath, (char *)label, &qh)) {
+						if (print) {
+							time_t timestamp = qh.qh_timestamp;
+							printf("%s:\n", newpath);
+							printf("\tMagic:   %08x\n", qh.qh_magic);
+							printf("\tLabel:   %s\n", qh.qh_cluster);
+							printf("\tCreated: %s",
+								ctime((time_t *)&timestamp));
+							printf("\tHost:    %s\n\n", qh.qh_updatehost);
+						}
+
+						if (devname && devlen) {
+							strncpy(devname, newpath, devlen);
+							return 0;
+						}
+					}
+				}
 			}
 		}
+		free(namelist[n]);
 	}
 
-	fclose(fp);
-
-	if (print)
-		/* No errors if we're just printing stuff */
-		return 0;
-
 	errno = ENOENT;
 	return -1;
 }

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]