Skip to contentSkip to conversation

mdadm:checking level once mode has been set

PATCH In review Zhilong Liu 3 participants 15 messages Mar 8, 2017 – Mar 17, 2017 MD RAID

lore
Share & tools
lore
#0 lore raw

cover mdadm:checking level once mode has been set

mdadm: it would be better to check --level ealier, because it would fall to different prompt if user forgets to specify the --level. such as:
./mdadm -CR /dev/md0 -b internal -n2 -x1 /dev/loop[0-2]

Create.c +0 -4
index 9a951b0..beec29f 100644
@@ -125,10 +125,6 @@ int Create(struct supertype *st, char *mddev,
 	memset(&info, 0, sizeof(info));
 	if (s->level == UnSet && st && st->ss->default_geometry)
 		st->ss->default_geometry(st, &s->level, NULL, NULL);
-	if (s->level == UnSet) {
-		pr_err("a RAID level is needed to create an array.\n");
-		return 1;
-	}
 	if (s->raiddisks < 4 && s->level == 6) {
 		pr_err("at least 4 raid-devices needed for level 6\n");
 		return 1;
mdadm.c +6 -0
index 19a06db..ad24bdf 100644
@@ -350,6 +350,12 @@ int main(int argc, char *argv[])
 				pr_err("Must give -a/--add for devices to add: %s\n", optarg);
 				exit(2);
 			}
+			if (devs_found > 0 && s.level == UnSet && !devmode) {
+				if (mode == CREATE || mode == BUILD) {
+					pr_err("a RAID level is needed to create or build an array.\n");
+					exit(2);
+				}
+			}
 			dv = xmalloc(sizeof(*dv));
 			dv->devname = optarg;
 			dv->disposition = devmode;
Signature · 1 line
2.10.2
#1 lore raw

PATCH 1/4 mdadm:bitmap cannot be set twice

mdadm:it doesn't make sense to set bitmap twice.

mdadm.c +4 -0
index b5ac061..eb9a4e9 100644
@@ -1137,6 +1137,10 @@ int main(int argc, char *argv[])
 		case O(CREATE,Bitmap): /* here we create the bitmap */
 		case O(GROW,'b'):
 		case O(GROW,Bitmap):
+			if (s.bitmap_file) {
+				pr_err("bitmap cannot be set twice. Second value: %s.\n", optarg);
+				exit(2);
+			}
 			if (strcmp(optarg, "internal") == 0 ||
 			    strcmp(optarg, "none") == 0 ||
 			    strchr(optarg, '/') != NULL) {
Signature · 1 line
2.10.2
#2 lore raw

PATCH 5/5 mdadm:checking level once mode has been set

mdadm: it would be better to check --level ealier, because it would fall to different prompt if user forgets to specify the --level parameter. such as:
./mdadm -CR /dev/md0 -b internal -n2 -x1 /dev/loop[0-2]

Create.c +0 -4
index 2721884..50ec85e 100644
@@ -125,10 +125,6 @@ int Create(struct supertype *st, char *mddev,
 	memset(&info, 0, sizeof(info));
 	if (s->level == UnSet && st && st->ss->default_geometry)
 		st->ss->default_geometry(st, &s->level, NULL, NULL);
-	if (s->level == UnSet) {
-		pr_err("a RAID level is needed to create an array.\n");
-		return 1;
-	}
 	if (s->raiddisks < 4 && s->level == 6) {
 		pr_err("at least 4 raid-devices needed for level 6\n");
 		return 1;
mdadm.c +6 -0
index d6ad8dc..fcb33d1 100644
@@ -349,6 +349,12 @@ int main(int argc, char *argv[])
 				pr_err("Must give -a/--add for devices to add: %s\n", optarg);
 				exit(2);
 			}
+			if (devs_found > 0 && s.level == UnSet && !devmode) {
+				if (mode == CREATE || mode == BUILD) {
+					pr_err("a RAID level is needed to create or build an array.\n");
+					exit(2);
+				}
+			}
 			dv = xmalloc(sizeof(*dv));
 			dv->devname = optarg;
 			dv->disposition = devmode;
Signature · 1 line
2.10.2
#3 lore raw

PATCH 2/4 mdadm:external bitmap only supports ext filesystem

In reply to Zhilong Liu

mdadm: ensure that the external bitmap_file is stored by ext[2-4] file system, because bmap() of linux/driver/md/bitmap.c exits directly when the bitmap_file isn't suitable. mdadm should make users aware of this scenario and give a prompt.

Create.c +0 -5
index 2721884..9a951b0 100644
@@ -831,11 +831,6 @@ int Create(struct supertype *st, char *mddev,
 			goto abort_locked;
 		}
 		bitmap_fd = open(s->bitmap_file, O_RDWR);
-		if (bitmap_fd < 0) {
-			pr_err("weird: %s cannot be openned\n",
-				s->bitmap_file);
-			goto abort_locked;
-		}
 		if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
 			pr_err("Cannot set bitmap file for %s: %s\n",
 				mddev, strerror(errno));
mdadm.c +16 -0
index d6ad8dc..19a06db 100644
@@ -28,6 +28,7 @@
 #include "mdadm.h"
 #include "md_p.h"
 #include <ctype.h>
+#include <sys/vfs.h>
 
 static int scan_assemble(struct supertype *ss,
 			 struct context *c,
@@ -1143,6 +1144,21 @@ int main(int argc, char *argv[])
 			    strcmp(optarg, "none") == 0 ||
 			    strchr(optarg, '/') != NULL) {
 				s.bitmap_file = optarg;
+				if (strchr(s.bitmap_file, '/') != NULL) {
+					bitmap_fd = open(s.bitmap_file, O_RDWR);
+					if (bitmap_fd < 0) {
+						pr_err("weird: %s cannot be openned\n", s.bitmap_file);
+						exit(2);
+					}
+					close(bitmap_fd);
+					struct statfs ext_bitmap;
+					statfs(s.bitmap_file, &ext_bitmap);
+					if (ext_bitmap.f_type != 0xEF53){
+						pr_err("external bitmap only supports ext[2-4] filesystem, %s.\n",
+							s.bitmap_file);
+						exit(2);
+					}
+				}
 				continue;
 			}
 			if (strcmp(optarg, "clustered") == 0) {
Signature · 1 line
2.10.2
#4 lore raw

Re: [PATCH 2/4] mdadm:external bitmap only supports ext filesystem

as this patch's purpose, just wanna improve the prompt when using the
external bitmap mode, and this patch maybe a little redundant.
For the errno rule, RUN_ARRAY returned EINVAL indeed and the man page
has indicated that external bitmap only works with ext[2-4] file system.
I think it would be more user-friendly if prints one prompt and
returned
EINVAL at the same time when the bmap() got failure.
Such as:

drivers/md/bitmap.c +1 -0
index 9fb2cca..0bff96b 100644
@@ -381,6 +381,7 @@ static int read_page(struct file *file, unsigned long index,
                        bh->b_blocknr = bmap(inode, block);
                        if (bh->b_blocknr == 0) {
                                /* Cannot use this file! */
+                               pr_err("The external bitmap only works with ext[2-4] filesystem.\n");
                                ret = -EINVAL;
                                goto out;
                        }

Thanks,
-Zhilong

Quoted 58 lines · Zhilong Liu

mdadm: ensure that the external bitmap_file is stored by ext[2-4] file system, because bmap() of linux/driver/md/bitmap.c exits directly when the bitmap_file isn't suitable. mdadm should make users aware of this scenario and give a prompt.

Create.c +0 -5
index 2721884..9a951b0 100644
@@ -831,11 +831,6 @@ int Create(struct supertype *st, char *mddev,
  			goto abort_locked;
  		}
  		bitmap_fd = open(s->bitmap_file, O_RDWR);
-		if (bitmap_fd < 0) {
-			pr_err("weird: %s cannot be openned\n",
-				s->bitmap_file);
-			goto abort_locked;
-		}
  		if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
  			pr_err("Cannot set bitmap file for %s: %s\n",
  				mddev, strerror(errno));
mdadm.c +16 -0
index d6ad8dc..19a06db 100644
@@ -28,6 +28,7 @@
  #include "mdadm.h"
  #include "md_p.h"
  #include <ctype.h>
+#include <sys/vfs.h>
 
  static int scan_assemble(struct supertype *ss,
  			 struct context *c,
@@ -1143,6 +1144,21 @@ int main(int argc, char *argv[])
  			    strcmp(optarg, "none") == 0 ||
  			    strchr(optarg, '/') != NULL) {
  				s.bitmap_file = optarg;
+				if (strchr(s.bitmap_file, '/') != NULL) {
+					bitmap_fd = open(s.bitmap_file, O_RDWR);
+					if (bitmap_fd < 0) {
+						pr_err("weird: %s cannot be openned\n", s.bitmap_file);
+						exit(2);
+					}
+					close(bitmap_fd);
+					struct statfs ext_bitmap;
+					statfs(s.bitmap_file, &ext_bitmap);
+					if (ext_bitmap.f_type != 0xEF53){
+						pr_err("external bitmap only supports ext[2-4] filesystem, %s.\n",
+							s.bitmap_file);
+						exit(2);
+					}
+				}
  				continue;
  			}
  			if (strcmp(optarg, "clustered") == 0) {
author
#5 lore raw

Re: [PATCH 2/4] mdadm:external bitmap only supports ext filesystem

In reply to Zhilong Liu

as the purpose to improve the prompt when using the external bitmap
mode, maybe push this patch to mdadm is a little redundant.
For errno rule, RUN_ARRAY returned EINVAL indeed and the man-page
has indicated that external bitmap only works with ext[2-4] file system.
I think it would be more user-friendly if prints one prompt and
returned
EINVAL at the same time when the bmap() got failure, so that user knows where the EINVAL comes.
Such as:

drivers/md/bitmap.c +1 -0
index 9fb2cca..0bff96b 100644
@@ -381,6 +381,7 @@ static int read_page(struct file *file, unsigned long index,
                        bh->b_blocknr = bmap(inode, block);
                        if (bh->b_blocknr == 0) {
                                /* Cannot use this file! */
+                               pr_err("writing external bitmap only supports a file under ext[2-4] filesystem.\n");
                                ret = -EINVAL;
                                goto out;
                        }

Thanks,
-Zhilong

Quoted 58 lines · Zhilong Liu

mdadm: ensure that the external bitmap_file is stored by ext[2-4] file system, because bmap() of linux/driver/md/bitmap.c exits directly when the bitmap_file isn't suitable. mdadm should make users aware of this scenario and give a prompt.

Create.c +0 -5
index 2721884..9a951b0 100644
@@ -831,11 +831,6 @@ int Create(struct supertype *st, char *mddev,
  			goto abort_locked;
  		}
  		bitmap_fd = open(s->bitmap_file, O_RDWR);
-		if (bitmap_fd < 0) {
-			pr_err("weird: %s cannot be openned\n",
-				s->bitmap_file);
-			goto abort_locked;
-		}
  		if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
  			pr_err("Cannot set bitmap file for %s: %s\n",
  				mddev, strerror(errno));
mdadm.c +16 -0
index d6ad8dc..19a06db 100644
@@ -28,6 +28,7 @@
  #include "mdadm.h"
  #include "md_p.h"
  #include <ctype.h>
+#include <sys/vfs.h>
 
  static int scan_assemble(struct supertype *ss,
  			 struct context *c,
@@ -1143,6 +1144,21 @@ int main(int argc, char *argv[])
  			    strcmp(optarg, "none") == 0 ||
  			    strchr(optarg, '/') != NULL) {
  				s.bitmap_file = optarg;
+				if (strchr(s.bitmap_file, '/') != NULL) {
+					bitmap_fd = open(s.bitmap_file, O_RDWR);
+					if (bitmap_fd < 0) {
+						pr_err("weird: %s cannot be openned\n", s.bitmap_file);
+						exit(2);
+					}
+					close(bitmap_fd);
+					struct statfs ext_bitmap;
+					statfs(s.bitmap_file, &ext_bitmap);
+					if (ext_bitmap.f_type != 0xEF53){
+						pr_err("external bitmap only supports ext[2-4] filesystem, %s.\n",
+							s.bitmap_file);
+						exit(2);
+					}
+				}
  				continue;
  			}
  			if (strcmp(optarg, "clustered") == 0) {
#6 lore raw

PATCH 3/4 mdadm:triggers core dump when stat2devnm return NULL

In reply to Zhilong Liu

ensure that the device should be a block device when uses --wait parameter, such as the 'f' and 'd' type file would be triggered core dumped.
./mdadm --wait /dev/md/, happened core dump.

Monitor.c +4 -0
index 802a9d9..1900db3 100644
@@ -1002,6 +1002,10 @@ int Wait(char *dev)
 			strerror(errno));
 		return 2;
 	}
+	if ((S_IFMT & stb.st_mode) != S_IFBLK) {
+		pr_err("%s is not a block device.\n", dev);
+		return 2;
+	}
 	strcpy(devnm, stat2devnm(&stb));
 
 	while(1) {
lib.c +0 -3
index b640634..7116298 100644
@@ -89,9 +89,6 @@ char *devid2kname(int devid)
 
 char *stat2kname(struct stat *st)
 {
-	if ((S_IFMT & st->st_mode) != S_IFBLK)
-		return NULL;
-
 	return devid2kname(st->st_rdev);
 }
 
Signature · 1 line
2.10.2
#7 lore raw

Re: [PATCH 3/4] mdadm:triggers core dump when stat2devnm return NULL

Quoted 12 lines · Zhilong Liu

ensure that the device should be a block device when uses --wait parameter, such as the 'f' and 'd' type file would be triggered core dumped.
./mdadm --wait /dev/md/, happened core dump.

Monitor.c +0 -0
index 802a9d9..1900db3 100644

Monitor.c int Wait(char *dev) · line 1009

Quoted 8 lines
Monitor.c +4 -0
@@ -1002,6 +1002,10 @@ int Wait(char *dev)
 			strerror(errno));
 		return 2;
 	}
+	if ((S_IFMT & stb.st_mode) != S_IFBLK) {
+		pr_err("%s is not a block device.\n", dev);
+		return 2;
+	}
 	strcpy(devnm, stat2devnm(&stb));

Surely it would be cleaner to do something like:

  tmp = stat2devnm(&stb);
  if (!tmp) {
      pr_err("%s is not a block device.\n", dev);
      return 2;
  }
  strcpy(devnm, tmp);

This makes it more obvious how you have fixed the crash.

Quoted 7 lines
Monitor.c +0 -0
 	while(1) {
lib.c +0 -0
index b640634..7116298 100644

lib.c char *devid2kname(int devid) · line 93

Quoted 8 lines
lib.c +0 -3
@@ -89,9 +89,6 @@ char *devid2kname(int devid)
 
 char *stat2kname(struct stat *st)
 {
-	if ((S_IFMT & st->st_mode) != S_IFBLK)
-		return NULL;
-
 	return devid2kname(st->st_rdev);
 }

Why are you removing this test? It has nothing to do with the other part of the patch.

NeilBrown

author
#8 lore raw

Re: [PATCH 3/4] mdadm:triggers core dump when stat2devnm return NULL

Quoted 24 lines · NeilBrown
Quoted 12 lines · Zhilong Liu

ensure that the device should be a block device when uses --wait parameter, such as the 'f' and 'd' type file would be triggered core dumped.
./mdadm --wait /dev/md/, happened core dump.

Monitor.c +0 -0
index 802a9d9..1900db3 100644

Monitor.c int Wait(char *dev) · line 1009

Quoted 8 lines
Monitor.c +4 -0
@@ -1002,6 +1002,10 @@ int Wait(char *dev)
  			strerror(errno));
  		return 2;
  	}
+	if ((S_IFMT & stb.st_mode) != S_IFBLK) {
+		pr_err("%s is not a block device.\n", dev);
+		return 2;
+	}
  	strcpy(devnm, stat2devnm(&stb));

Surely it would be cleaner to do something like:

Quoted 8 lines
   tmp = stat2devnm(&stb);
   if (!tmp) {
       pr_err("%s is not a block device.\n", dev);
       return 2;
   }
   strcpy(devnm, tmp);

This makes it more obvious how you have fixed the crash.

Yes, this method is much better than I did. Great thanks for your improvement.

Quoted 8 lines
Quoted 8 lines
Monitor.c +0 -0
  	while(1) {
lib.c +0 -0
index b640634..7116298 100644
@@ -89,9 +89,6 @@ char *devid2kname(int devid)
 
Quoted 9 lines

lib.c char *devid2kname(int devid) · line 93

Quoted 7 lines
lib.c +0 -3
  char *stat2kname(struct stat *st)
  {
-	if ((S_IFMT & st->st_mode) != S_IFBLK)
-		return NULL;
-
  	return devid2kname(st->st_rdev);
  }

Why are you removing this test? It has nothing to do with the other part of the patch.

Yes, I would remove this part in next revision.

Thanks,
-Zhilong

Quoted 1 line

NeilBrown

#9 lore raw

PATCH 3/4 mdadm:triggers core dump when stat2devnm return NULL

In reply to NeilBrown

monitor: ensure that the device should be a block device when uses --wait parameter, such as the 'f' and 'd' type file would be triggered core dumped. ./mdadm --wait /dev/md/, happened core dump.

---
 Monitor.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
Monitor.c +6 -1
index 802a9d9..f8850d3 100644
@@ -1002,7 +1002,12 @@ int Wait(char *dev)
 			strerror(errno));
 		return 2;
 	}
-	strcpy(devnm, stat2devnm(&stb));
+	char *tmp = stat2devnm(&stb);
+	if (!tmp) {
+		pr_err("%s is not a block device.\n", dev);
+		return 2;
+	}
+	strcpy(devnm, tmp);
 
 	while(1) {
 		struct mdstat_ent *ms = mdstat_read(1, 0);
Signature · 1 line
2.6.6
#10 lore raw
In reply to Zhilong Liu
Quoted 4 lines · Zhilong Liu

mdadm: it would be better to check --level ealier, because it would fall to different prompt if user forgets to specify the --level. such as:
./mdadm -CR /dev/md0 -b internal -n2 -x1 /dev/loop[0-2]

When I run that command I get:

mdadm: a RAID level is needed to create an array.

What do you get?

NeilBrown

Quoted 42 lines
Create.c +0 -4
index 9a951b0..beec29f 100644
@@ -125,10 +125,6 @@ int Create(struct supertype *st, char *mddev,
 	memset(&info, 0, sizeof(info));
 	if (s->level == UnSet && st && st->ss->default_geometry)
 		st->ss->default_geometry(st, &s->level, NULL, NULL);
-	if (s->level == UnSet) {
-		pr_err("a RAID level is needed to create an array.\n");
-		return 1;
-	}
 	if (s->raiddisks < 4 && s->level == 6) {
 		pr_err("at least 4 raid-devices needed for level 6\n");
 		return 1;
mdadm.c +6 -0
index 19a06db..ad24bdf 100644
@@ -350,6 +350,12 @@ int main(int argc, char *argv[])
 				pr_err("Must give -a/--add for devices to add: %s\n", optarg);
 				exit(2);
 			}
+			if (devs_found > 0 && s.level == UnSet && !devmode) {
+				if (mode == CREATE || mode == BUILD) {
+					pr_err("a RAID level is needed to create or build an array.\n");
+					exit(2);
+				}
+			}
 			dv = xmalloc(sizeof(*dv));
 			dv->devname = optarg;
 			dv->disposition = devmode;
Signature · 6 lines
2.10.2

--
To unsubscribe from this list: send the line "unsubscribe linux-raid" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author
#11 lore raw
Quoted 6 lines · NeilBrown
Quoted 4 lines · Zhilong Liu

mdadm: it would be better to check --level ealier, because it would fall to different prompt if user forgets to specify the --level. such as:
./mdadm -CR /dev/md0 -b internal -n2 -x1 /dev/loop[0-2]

Quoted 6 lines

When I run that command I get:

mdadm: a RAID level is needed to create an array.

What do you get?

I'm sorry I have provided the wrong command, for this scenario, issues "mdadm --build /dev/md0 -n2 /dev/loop[0-1]" should be proper. it's the purpose to check --level earlier.

If this patch is useful, I would send the v1 patch for it, correct the comments.

Thanks,
-Zhilong

Quoted 44 lines

NeilBrown

Quoted 41 lines
Create.c +0 -4
index 9a951b0..beec29f 100644
@@ -125,10 +125,6 @@ int Create(struct supertype *st, char *mddev,
  	memset(&info, 0, sizeof(info));
  	if (s->level == UnSet && st && st->ss->default_geometry)
  		st->ss->default_geometry(st, &s->level, NULL, NULL);
-	if (s->level == UnSet) {
-		pr_err("a RAID level is needed to create an array.\n");
-		return 1;
-	}
  	if (s->raiddisks < 4 && s->level == 6) {
  		pr_err("at least 4 raid-devices needed for level 6\n");
  		return 1;
mdadm.c +6 -0
index 19a06db..ad24bdf 100644
@@ -350,6 +350,12 @@ int main(int argc, char *argv[])
  				pr_err("Must give -a/--add for devices to add: %s\n", optarg);
  				exit(2);
  			}
+			if (devs_found > 0 && s.level == UnSet && !devmode) {
+				if (mode == CREATE || mode == BUILD) {
+					pr_err("a RAID level is needed to create or build an array.\n");
+					exit(2);
+				}
+			}
  			dv = xmalloc(sizeof(*dv));
  			dv->devname = optarg;
  			dv->disposition = devmode;
Signature · 6 lines
2.10.2

--
To unsubscribe from this list: send the line "unsubscribe linux-raid" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#12 lore raw
Quoted 9 lines · zhilong
Quoted 8 lines · NeilBrown
Quoted 4 lines · Zhilong Liu

mdadm: it would be better to check --level ealier, because it would fall to different prompt if user forgets to specify the --level. such as:
./mdadm -CR /dev/md0 -b internal -n2 -x1 /dev/loop[0-2]

When I run that command I get:

Quoted 8 lines
Quoted 4 lines

mdadm: a RAID level is needed to create an array.

What do you get?

I'm sorry I have provided the wrong command, for this scenario, issues "mdadm --build /dev/md0 -n2 /dev/loop[0-1]" should be proper. it's the purpose to check --level earlier.

OK, that makes sense.

Quoted 3 lines

If this patch is useful, I would send the v1 patch for it, correct the comments.

My preference would be to have the check in Build.c, but Jes might have different ideas.

NeilBrown

author
#13 lore raw
Quoted 18 lines · NeilBrown
Quoted 9 lines · zhilong
Quoted 8 lines · NeilBrown
Quoted 4 lines · Zhilong Liu

mdadm: it would be better to check --level ealier, because it would fall to different prompt if user forgets to specify the --level. such as:
./mdadm -CR /dev/md0 -b internal -n2 -x1 /dev/loop[0-2]

When I run that command I get:

Quoted 7 lines
Quoted 4 lines

mdadm: a RAID level is needed to create an array.

What do you get?

I'm sorry I have provided the wrong command, for this scenario, issues "mdadm --build /dev/md0 -n2 /dev/loop[0-1]" should be proper. it's the purpose to check --level earlier.

Quoted 6 lines

OK, that makes sense.

Quoted 2 lines

If this patch is useful, I would send the v1 patch for it, correct the comments.

My preference would be to have the check in Build.c, but Jes might have different ideas.

copy that, thanks very much.

Best regards,
-Zhilong

Quoted 1 line

NeilBrown

#14 lore raw
In reply to Zhilong Liu
Quoted 6 lines · Zhilong Liu

mdadm: it would be better to check --level ealier, because it would fall to different prompt if user forgets to specify the --level. such as:
./mdadm -CR /dev/md0 -b internal -n2 -x1 /dev/loop[0-2]

This patchset is completely messed up - 0/4 contains actual code instead of being a cover letter. 5/5 is a reply to 1/4 and you posted a v1 of something which I assume needs to be the v2 given the v1 is the original posting.

Would you mind sending out a clean set of these patches?

Thanks,
Jes