[dm-devel] [PATCH] kpartx: Properly support read-only files

Till Maas opensource at till.name
Mon Jun 10 16:40:58 UTC 2013


- Use only variable loopro instead of additionally variable ro: If the
  file can only be opened read-only, it does not make sense to create a
  writable mapping.
- Fall back to read-only mapping on EACCESS instead of only on EROFS,
  since immutable files causes EACCESS.
---
 kpartx/kpartx.c | 8 ++++----
 kpartx/lopart.c | 2 +-
 2 Dateien geändert, 5 Zeilen hinzugefügt(+), 5 Zeilen entfernt(-)

diff --git a/kpartx/kpartx.c b/kpartx/kpartx.c
index 98d88c0..82c3bb2 100644
--- a/kpartx/kpartx.c
+++ b/kpartx/kpartx.c
@@ -192,7 +192,7 @@ get_hotplug_device(void)
 
 int
 main(int argc, char **argv){
-	int i, j, m, n, op, off, arg, c, d, ro=0;
+	int i, j, m, n, op, off, arg, c, d;
 	int fd = -1;
 	struct slice all;
 	struct pt *ptp;
@@ -241,7 +241,7 @@ main(int argc, char **argv){
 
 	while ((arg = getopt(argc, argv, short_opts)) != EOF) switch(arg) {
 		case 'r':
-			ro=1;
+			loopro=1;
 			break;
 		case 'f':
 			force_devmap=1;
@@ -489,7 +489,7 @@ main(int argc, char **argv){
 					DM_DEVICE_RELOAD : DM_DEVICE_CREATE);
 
 				if (!dm_addmap(op, partname, DM_TARGET, params,
-					       slices[j].size, ro, uuid, j+1,
+					       slices[j].size, loopro, uuid, j+1,
 					       buf.st_mode & 0777, buf.st_uid,
 					       buf.st_gid, &cookie)) {
 					fprintf(stderr, "create/reload failed on %s\n",
@@ -552,7 +552,7 @@ main(int argc, char **argv){
 					      DM_DEVICE_RELOAD : DM_DEVICE_CREATE);
 
 					dm_addmap(op, partname, DM_TARGET, params,
-						  slices[j].size, ro, uuid, j+1,
+						  slices[j].size, loopro, uuid, j+1,
 						  buf.st_mode & 0777,
 						  buf.st_uid, buf.st_gid,
 						  &cookie);
diff --git a/kpartx/lopart.c b/kpartx/lopart.c
index 79a7593..9082ca8 100644
--- a/kpartx/lopart.c
+++ b/kpartx/lopart.c
@@ -230,7 +230,7 @@ set_loop (const char *device, const char *file, int offset, int *loopro)
 
 	if ((ffd = open (file, mode)) < 0) {
 
-		if (!*loopro && errno == EROFS)
+		if (!*loopro && (errno == EROFS || errno == EACCES))
 			ffd = open (file, mode = O_RDONLY);
 
 		if (ffd < 0) {
-- 
1.7.11.7




More information about the dm-devel mailing list