linux-raid.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "John Stoffel" <john@stoffel.org>
To: John Stoffel <john@stoffel.org>
Cc: Eli Ben-Shoshan <eli@benshoshan.com>,
	Jes.Sorensen@gmail.com, linux-raid@vger.kernel.org
Subject: mdadm: Patch to restrict --size when shrinking unless forced
Date: Wed, 4 Oct 2017 14:00:56 -0400	[thread overview]
Message-ID: <22997.8664.67459.119616@quad.stoffel.home> (raw)


Since Eli had such a horrible experience where he shrunk the
individual component raid device size, instead of growing the overall
raid by adding a device, I came up with this hacky patch to warn you
when you are about to shoot yourself in the foot.

The idea is it will warn you and exit unless you pass in the --force
(or -f) switch when using the command.  For example, on a set of loop
devices:

    # cat /proc/mdstat
    Personalities : [linear] [raid0] [raid1] [raid10] [raid6] [raid5]
    [raid4] [multipath] [faulty]
    md99 : active raid6 loop4p1[4] loop3p1[3] loop2p1[2] loop1p1[1]
    loop0p1[0]
	  606720 blocks super 1.2 level 6, 512k chunk, algorithm 2 [5/5]
	  [UUUUU]

    # ./mdadm --grow /dev/md99 --size 128
    mdadm: Cannot set device size smaller than current component_size of /dev/md99 array.  Use -f to force change.

    # ./mdadm --grow /dev/md99 --size 128 -f
    mdadm: component size of /dev/md99 has been set to 0K


I suspect I could do a better job of showing the original component
size, so that you have a chance of recovering even then.

But the patch:

diff --git a/Grow.c b/Grow.c
index 455c5f9..701590f 100755
--- a/Grow.c
+++ b/Grow.c
@@ -1561,7 +1561,7 @@ static int reshape_container(char *container, char *devname,
 			     char *backup_file, int verbose,
 			     int forked, int restart, int freeze_reshape);
 
-int Grow_reshape(char *devname, int fd,
+int Grow_reshape(char *devname, int fd, int force,
 		 struct mddev_dev *devlist,
 		 unsigned long long data_offset,
 		 struct context *c, struct shape *s)
@@ -1574,6 +1574,7 @@ int Grow_reshape(char *devname, int fd,
 	 * requested everything (if kernel supports freezing - 2.6.30).
 	 * The steps are:
 	 *  - change size (i.e. component_size)
+         *    - when shrinking, you must force the change 
 	 *  - change level
 	 *  - change layout/chunksize/ndisks
 	 *
@@ -1617,6 +1618,11 @@ int Grow_reshape(char *devname, int fd,
 		return 1;
 	}
 
+	if ((s->size < (unsigned)array.size) && !force) {
+	    pr_err("Cannot set device size smaller than current component_size of %s array.  Use -f to force change.\n",devname);
+	    return 1;
+	}
+
 	if (s->raiddisks && s->raiddisks < array.raid_disks && array.level > 1 &&
 	    get_linux_version() < 2006032 &&
 	    !check_env("MDADM_FORCE_FEWER")) {
diff --git a/ReadMe.c b/ReadMe.c
index 50d3807..46988ae 100644
--- a/ReadMe.c
+++ b/ReadMe.c
@@ -203,6 +203,7 @@ struct option long_options[] = {
     {"invalid-backup",0,0,InvalidBackup},
     {"array-size", 1, 0, 'Z'},
     {"continue", 0, 0, Continue},
+    {"force",	  0, 0, Force},
 
     /* For Incremental */
     {"rebuild-map", 0, 0, RebuildMapOpt},
@@ -563,6 +564,7 @@ char Help_grow[] =
 "                      : This is useful if all devices have been replaced\n"
 "                      : with larger devices.   Value is in Kilobytes, or\n"
 "                      : the special word 'max' meaning 'as large as possible'.\n"
+"  --force       -f    : Override normal checks and be more forceful\n"
 "  --assume-clean      : When increasing the --size, this flag will avoid\n"
 "                      : a resync of the new space\n"
 "  --chunk=       -c   : Change the chunksize of the array\n"
diff --git a/mdadm.c b/mdadm.c
index c3a265b..821658a 100644
--- a/mdadm.c
+++ b/mdadm.c
@@ -1617,7 +1617,7 @@ int main(int argc, char *argv[])
 		else if (s.size > 0 || s.raiddisks || s.layout_str ||
 			 s.chunk != 0 || s.level != UnSet ||
 			 data_offset != INVALID_SECTORS) {
-			rv = Grow_reshape(devlist->devname, mdfd,
+		    rv = Grow_reshape(devlist->devname, mdfd, c.force,
 					  devlist->next,
 					  data_offset, &c, &s);
 		} else if (array_size == 0)
diff --git a/mdadm.h b/mdadm.h
index 71b8afb..9e00f05 100644
--- a/mdadm.h
+++ b/mdadm.h
@@ -1300,7 +1300,7 @@ extern int autodetect(void);
 extern int Grow_Add_device(char *devname, int fd, char *newdev);
 extern int Grow_addbitmap(char *devname, int fd,
 			  struct context *c, struct shape *s);
-extern int Grow_reshape(char *devname, int fd,
+extern int Grow_reshape(char *devname, int fd, int force,
 			struct mddev_dev *devlist,
 			unsigned long long data_offset,
 			struct context *c, struct shape *s);

             reply	other threads:[~2017-10-04 18:00 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-04 18:00 John Stoffel [this message]
2017-10-04 18:11 ` mdadm: Patch to restrict --size when shrinking unless forced Jes Sorensen
2017-10-04 19:15   ` John Stoffel
2017-10-04 19:23     ` Jes Sorensen
2017-10-04 19:33       ` John Stoffel
2017-10-04 21:50 ` NeilBrown
2017-10-05  1:26   ` John Stoffel
2017-10-07 22:06     ` Wols Lists
2017-10-07 22:17       ` John Stoffel
2017-10-07 22:37         ` Wols Lists
2017-10-07 22:46           ` John Stoffel
2017-10-08 20:57   ` John Stoffel
2017-10-08 22:52     ` NeilBrown
2017-10-09  1:18       ` John Stoffel
2017-10-09  1:36         ` NeilBrown
2017-10-09  1:22       ` John Stoffel
2017-10-09  4:10         ` NeilBrown
2017-10-09 20:04           ` Phil Turmel
2017-10-10  0:07             ` Wakko Warner
2017-10-10 13:12               ` Phil Turmel
2017-10-10 20:52               ` NeilBrown
2017-10-10 20:55                 ` Wakko Warner
2017-10-10  2:01             ` John Stoffel
2017-10-10 20:09             ` Jes Sorensen
2017-10-10 20:54               ` Wols Lists
2017-10-10 21:07                 ` Jes Sorensen
2017-10-10 20:48             ` NeilBrown
2017-10-10 20:58               ` Phil Turmel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=22997.8664.67459.119616@quad.stoffel.home \
    --to=john@stoffel.org \
    --cc=Jes.Sorensen@gmail.com \
    --cc=eli@benshoshan.com \
    --cc=linux-raid@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).