Blame SOURCES/0269-RHBZ-1804420-remove-kpartx-limit.patch

958de5
---
958de5
 kpartx/lopart.c |  206 ++++++++++++++++++++++++++++++++++----------------------
958de5
 1 file changed, 127 insertions(+), 79 deletions(-)
958de5
958de5
Index: multipath-tools-130222/kpartx/lopart.c
958de5
===================================================================
958de5
--- multipath-tools-130222.orig/kpartx/lopart.c
958de5
+++ multipath-tools-130222/kpartx/lopart.c
958de5
@@ -25,8 +25,9 @@
958de5
 #include <sys/ioctl.h>
958de5
 #include <sys/stat.h>
958de5
 #include <sys/mman.h>
958de5
+#include <sys/types.h>
958de5
+#include <dirent.h>
958de5
 #include <sysmacros.h>
958de5
-#include <asm/posix_types.h>
958de5
 #include <linux/loop.h>
958de5
 
958de5
 #include "lopart.h"
958de5
@@ -96,97 +97,149 @@ is_loop_device (const char *device)
958de5
 #define SIZE(a) (sizeof(a)/sizeof(a[0]))
958de5
 
958de5
 extern char *
958de5
-find_loop_by_file (const char * filename)
958de5
+find_loop_by_file(const char *filename)
958de5
 {
958de5
-	char dev[64];
958de5
-	char *loop_formats[] = { "/dev/loop%d", "/dev/loop/%d" };
958de5
-	int i, j, fd;
958de5
+	DIR *dir;
958de5
+	struct dirent *dent;
958de5
+	char dev[64], *found = NULL, *p;
958de5
+	int fd, bytes_read;
958de5
 	struct stat statbuf;
958de5
 	struct loop_info loopinfo;
958de5
+	const char VIRT_BLOCK[] = "/sys/devices/virtual/block";
958de5
+	char path[PATH_MAX];
958de5
+	char bf_path[PATH_MAX];
958de5
+	char backing_file[PATH_MAX];
958de5
 
958de5
-	for (j = 0; j < SIZE(loop_formats); j++) {
958de5
+	dir = opendir(VIRT_BLOCK);
958de5
+	if (!dir)
958de5
+		return NULL;
958de5
 
958de5
-		for (i = 0; i < 256; i++) {
958de5
-			sprintf (dev, loop_formats[j], i);
958de5
+	while ((dent = readdir(dir)) != NULL) {
958de5
+		if (strncmp(dent->d_name,"loop",4))
958de5
+			continue;
958de5
 
958de5
-			if (stat (dev, &statbuf) != 0 ||
958de5
-			    !S_ISBLK(statbuf.st_mode))
958de5
-				continue;
958de5
+		if (snprintf(path, PATH_MAX, "%s/%s/dev", VIRT_BLOCK,
958de5
+					dent->d_name) >= PATH_MAX)
958de5
+			continue;
958de5
 
958de5
-			fd = open (dev, O_RDONLY);
958de5
+		fd = open(path, O_RDONLY);
958de5
+		if (fd < 0)
958de5
+			continue;
958de5
 
958de5
-			if (fd < 0)
958de5
-				break;
958de5
+		bytes_read = read(fd, dev, sizeof(dev) - 1);
958de5
+		if (bytes_read <= 0) {
958de5
+			close(fd);
958de5
+			continue;
958de5
+		}
958de5
 
958de5
-			if (ioctl (fd, LOOP_GET_STATUS, &loopinfo) != 0) {
958de5
-				close (fd);
958de5
-				continue;
958de5
-			}
958de5
+		close(fd);
958de5
 
958de5
-			if (0 == strcmp(filename, loopinfo.lo_name)) {
958de5
-				close (fd);
958de5
-				return xstrdup(dev); /*found */
958de5
-			}
958de5
+		dev[bytes_read] = '\0';
958de5
+		p = strchr(dev, '\n');
958de5
+		if (p != NULL)
958de5
+			*p = '\0';
958de5
+		if (snprintf(path, PATH_MAX, "/dev/block/%s", dev) >= PATH_MAX)
958de5
+			continue;
958de5
 
958de5
+		fd = open (path, O_RDONLY);
958de5
+		if (fd < 0)
958de5
+			continue;
958de5
+
958de5
+		if (fstat (fd, &statbuf) != 0 ||
958de5
+		    !S_ISBLK(statbuf.st_mode)) {
958de5
+			close (fd);
958de5
+			continue;
958de5
+		}
958de5
+
958de5
+		if (ioctl (fd, LOOP_GET_STATUS, &loopinfo) != 0) {
958de5
 			close (fd);
958de5
 			continue;
958de5
 		}
958de5
+
958de5
+		close (fd);
958de5
+
958de5
+		if (0 == strcmp(filename, loopinfo.lo_name)) {
958de5
+			found = realpath(path, NULL);
958de5
+			break;
958de5
+		}
958de5
+
958de5
+		/*
958de5
+		 * filename is a realpath, while loopinfo.lo_name may hold just the
958de5
+		 * basename.  If that's the case, try to match filename against the
958de5
+		 * backing_file entry for this loop entry
958de5
+		 */
958de5
+		if (snprintf(bf_path, PATH_MAX, "%s/%s/loop/backing_file",
958de5
+			     VIRT_BLOCK, dent->d_name) >= PATH_MAX)
958de5
+			continue;
958de5
+
958de5
+		fd = open(bf_path, O_RDONLY);
958de5
+		if (fd < 0)
958de5
+			continue;
958de5
+
958de5
+		bytes_read = read(fd, backing_file, sizeof(backing_file) - 1);
958de5
+		if (bytes_read <= 0) {
958de5
+			close(fd);
958de5
+			continue;
958de5
+		}
958de5
+
958de5
+		close(fd);
958de5
+
958de5
+		backing_file[bytes_read-1] = '\0';
958de5
+
958de5
+		if (0 == strcmp(filename, backing_file)) {
958de5
+			found = realpath(path, NULL);
958de5
+			break;
958de5
+		}
958de5
 	}
958de5
-	return NULL;
958de5
+	closedir(dir);
958de5
+	return found;
958de5
 }
958de5
 
958de5
 extern char *
958de5
-find_unused_loop_device (void)
958de5
+find_unused_loop_device(void)
958de5
 {
958de5
-	/* Just creating a device, say in /tmp, is probably a bad idea -
958de5
-	   people might have problems with backup or so.
958de5
-	   So, we just try /dev/loop[0-7]. */
958de5
-
958de5
-	char dev[20];
958de5
-	char *loop_formats[] = { "/dev/loop%d", "/dev/loop/%d" };
958de5
-	int i, j, fd, first = 0, somedev = 0, someloop = 0, loop_known = 0;
958de5
+	char dev[20], *next_loop_dev = NULL;
958de5
+	int fd, next_loop = 0, somedev = 0, someloop = 0, loop_known = 0;
958de5
 	struct stat statbuf;
958de5
 	struct loop_info loopinfo;
958de5
 	FILE *procdev;
958de5
 
958de5
-	if (stat("/dev/loop-control", &statbuf) == 0 &&
958de5
-	    S_ISCHR(statbuf.st_mode)) {
958de5
-		fd = open("/dev/loop-control", O_RDWR);
958de5
-		if (fd >= 0) {
958de5
-			first = ioctl(fd, LOOP_CTL_GET_FREE);
958de5
-			close(fd);
958de5
+	while (next_loop_dev == NULL) {
958de5
+		if (stat("/dev/loop-control", &statbuf) == 0 &&
958de5
+		    S_ISCHR(statbuf.st_mode)) {
958de5
+			int next_loop_fd;
958de5
+
958de5
+			next_loop_fd = open("/dev/loop-control", O_RDWR);
958de5
+			if (next_loop_fd < 0)
958de5
+				return NULL;
958de5
+			next_loop = ioctl(next_loop_fd, LOOP_CTL_GET_FREE);
958de5
+			close(next_loop_fd);
958de5
+			if (next_loop < 0)
958de5
+				return NULL;
958de5
 		}
958de5
-		if (first < 0)
958de5
-			first = 0;
958de5
-	}
958de5
-	for (j = 0; j < SIZE(loop_formats); j++) {
958de5
-
958de5
-	    for(i = first; i < 256; i++) {
958de5
-		sprintf(dev, loop_formats[j], i);
958de5
-
958de5
-		if (stat (dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
958de5
-			somedev++;
958de5
-			fd = open (dev, O_RDONLY);
958de5
 
958de5
-			if (fd >= 0) {
958de5
+		sprintf(dev, "/dev/loop%d", next_loop);
958de5
 
958de5
+		fd = open (dev, O_RDONLY);
958de5
+		if (fd >= 0) {
958de5
+			if (fstat (fd, &statbuf) == 0 &&
958de5
+			    S_ISBLK(statbuf.st_mode)) {
958de5
+				somedev++;
958de5
 				if(ioctl (fd, LOOP_GET_STATUS, &loopinfo) == 0)
958de5
-					someloop++;		/* in use */
958de5
-
958de5
-				else if (errno == ENXIO) {
958de5
-					close (fd);
958de5
-					return xstrdup(dev);/* probably free */
958de5
-				}
958de5
+					someloop++;             /* in use */
958de5
+				else if (errno == ENXIO)
958de5
+					next_loop_dev = xstrdup(dev);
958de5
 
958de5
-				close (fd);
958de5
 			}
958de5
-			
958de5
+			close (fd);
958de5
+
958de5
 			/* continue trying as long as devices exist */
958de5
 			continue;
958de5
 		}
958de5
 		break;
958de5
-	    }
958de5
 	}
958de5
+	if (next_loop_dev)
958de5
+		return next_loop_dev;
958de5
 
958de5
 	/* Nothing found. Why not? */
958de5
 	if ((procdev = fopen(PROC_DEVICES, "r")) != NULL) {
958de5
@@ -209,29 +262,24 @@ find_unused_loop_device (void)
958de5
 		fprintf(stderr, "mount: could not find any device /dev/loop#");
958de5
 
958de5
 	else if (!someloop) {
958de5
-
958de5
-	    if (loop_known == 1)
958de5
-		fprintf(stderr,
958de5
-		    "mount: Could not find any loop device.\n"
958de5
-		    "       Maybe /dev/loop# has a wrong major number?");
958de5
-	    
958de5
-	    else if (loop_known == -1)
958de5
-		fprintf(stderr,
958de5
-		    "mount: Could not find any loop device, and, according to %s,\n"
958de5
-		    "       this kernel does not know about the loop device.\n"
958de5
-		    "       (If so, then recompile or `modprobe loop'.)",
958de5
-		      PROC_DEVICES);
958de5
-
958de5
-	    else
958de5
-		fprintf(stderr,
958de5
-		    "mount: Could not find any loop device. Maybe this kernel does not know\n"
958de5
-		    "       about the loop device (then recompile or `modprobe loop'), or\n"
958de5
-		    "       maybe /dev/loop# has the wrong major number?");
958de5
-
958de5
+		if (loop_known == 1)
958de5
+			fprintf(stderr,
958de5
+				"mount: Could not find any loop device.\n"
958de5
+				"       Maybe /dev/loop# has a wrong major number?");
958de5
+		else if (loop_known == -1)
958de5
+			fprintf(stderr,
958de5
+				"mount: Could not find any loop device, and, according to %s,\n"
958de5
+				"       this kernel does not know about the loop device.\n"
958de5
+				"       (If so, then recompile or `modprobe loop'.)",
958de5
+				PROC_DEVICES);
958de5
+		else
958de5
+			fprintf(stderr,
958de5
+				"mount: Could not find any loop device. Maybe this kernel does not know\n"
958de5
+				"       about the loop device (then recompile or `modprobe loop'), or\n"
958de5
+				"       maybe /dev/loop# has the wrong major number?");
958de5
 	} else
958de5
 		fprintf(stderr, "mount: could not find any free loop device");
958de5
-	
958de5
-	return 0;
958de5
+	return NULL;
958de5
 }
958de5
 
958de5
 extern int