]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Grow.c
Grow: disallow --size changes on RAID0 and Linear.
[thirdparty/mdadm.git] / Grow.c
CommitLineData
e5329c37
NB
1/*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
ca3b6696 4 * Copyright (C) 2001-2012 Neil Brown <neilb@suse.de>
e5329c37
NB
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
e736b623 22 * Email: <neilb@suse.de>
e5329c37
NB
23 */
24#include "mdadm.h"
25#include "dlink.h"
7236ee7a 26#include <sys/mman.h>
e5329c37
NB
27
28#if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
29#error no endian defined
30#endif
31#include "md_u.h"
32#include "md_p.h"
33
e9e43ec3
N
34#ifndef offsetof
35#define offsetof(t,f) ((size_t)&(((t*)0)->f))
36#endif
37
3f54bd62
AK
38int restore_backup(struct supertype *st,
39 struct mdinfo *content,
40 int working_disks,
41 int next_spare,
42 char *backup_file,
43 int verbose)
44{
45 int i;
46 int *fdlist;
47 struct mdinfo *dev;
48 int err;
49 int disk_count = next_spare + working_disks;
50
51 dprintf("Called restore_backup()\n");
503975b9
N
52 fdlist = xmalloc(sizeof(int) * disk_count);
53
3f54bd62
AK
54 for (i = 0; i < next_spare; i++)
55 fdlist[i] = -1;
56 for (dev = content->devs; dev; dev = dev->next) {
57 char buf[22];
58 int fd;
59 sprintf(buf, "%d:%d",
60 dev->disk.major,
61 dev->disk.minor);
62 fd = dev_open(buf, O_RDWR);
63
64 if (dev->disk.raid_disk >= 0)
65 fdlist[dev->disk.raid_disk] = fd;
66 else
67 fdlist[next_spare++] = fd;
68 }
69
70 if (st->ss->external && st->ss->recover_backup)
71 err = st->ss->recover_backup(st, content);
72 else
73 err = Grow_restart(st, content, fdlist, next_spare,
74 backup_file, verbose > 0);
75
76 while (next_spare > 0) {
cc7f63e5
N
77 next_spare--;
78 if (fdlist[next_spare] >= 0)
79 close(fdlist[next_spare]);
3f54bd62
AK
80 }
81 free(fdlist);
82 if (err) {
e7b84f9d
N
83 pr_err("Failed to restore critical"
84 " section for reshape - sorry.\n");
3f54bd62 85 if (!backup_file)
e7b84f9d 86 pr_err("Possibly you need"
3f54bd62
AK
87 " to specify a --backup-file\n");
88 return 1;
89 }
90
91 dprintf("restore_backup() returns status OK.\n");
92 return 0;
93}
94
e5329c37
NB
95int Grow_Add_device(char *devname, int fd, char *newdev)
96{
97 /* Add a device to an active array.
98 * Currently, just extend a linear array.
99 * This requires writing a new superblock on the
100 * new device, calling the kernel to add the device,
101 * and if that succeeds, update the superblock on
102 * all other devices.
103 * This means that we need to *find* all other devices.
104 */
4b1ac34b
NB
105 struct mdinfo info;
106
e5329c37
NB
107 struct stat stb;
108 int nfd, fd2;
109 int d, nd;
82d9eba6 110 struct supertype *st = NULL;
4725bc31 111 char *subarray = NULL;
e5329c37 112
4b1ac34b 113 if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
e7b84f9d 114 pr_err("cannot get array info for %s\n", devname);
e5329c37
NB
115 return 1;
116 }
117
4725bc31 118 if (info.array.level != -1) {
e7b84f9d 119 pr_err("can only add devices to linear arrays\n");
4725bc31
N
120 return 1;
121 }
122
123 st = super_by_fd(fd, &subarray);
82d9eba6 124 if (!st) {
ca3b6696
N
125 pr_err("cannot handle arrays with superblock version %d\n",
126 info.array.major_version);
f9ce90ba
NB
127 return 1;
128 }
129
4725bc31 130 if (subarray) {
e7b84f9d 131 pr_err("Cannot grow linear sub-arrays yet\n");
4725bc31
N
132 free(subarray);
133 free(st);
2641101b 134 return 1;
e5329c37
NB
135 }
136
6416d527 137 nfd = open(newdev, O_RDWR|O_EXCL|O_DIRECT);
e5329c37 138 if (nfd < 0) {
e7b84f9d 139 pr_err("cannot open %s\n", newdev);
4725bc31 140 free(st);
e5329c37
NB
141 return 1;
142 }
143 fstat(nfd, &stb);
144 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
e7b84f9d 145 pr_err("%s is not a block device!\n", newdev);
e5329c37 146 close(nfd);
4725bc31 147 free(st);
e5329c37
NB
148 return 1;
149 }
ca3b6696
N
150 /* now check out all the devices and make sure we can read the
151 * superblock */
4b1ac34b 152 for (d=0 ; d < info.array.raid_disks ; d++) {
e5329c37
NB
153 mdu_disk_info_t disk;
154 char *dv;
155
4725bc31
N
156 st->ss->free_super(st);
157
e5329c37
NB
158 disk.number = d;
159 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
e7b84f9d 160 pr_err("cannot get device detail for device %d\n",
e5329c37 161 d);
4725bc31
N
162 close(nfd);
163 free(st);
e5329c37
NB
164 return 1;
165 }
16c6fa80 166 dv = map_dev(disk.major, disk.minor, 1);
e5329c37 167 if (!dv) {
e7b84f9d 168 pr_err("cannot find device file for device %d\n",
e5329c37 169 d);
4725bc31
N
170 close(nfd);
171 free(st);
e5329c37
NB
172 return 1;
173 }
16c6fa80 174 fd2 = dev_open(dv, O_RDWR);
68fe8c6e 175 if (fd2 < 0) {
e7b84f9d 176 pr_err("cannot open device file %s\n", dv);
4725bc31
N
177 close(nfd);
178 free(st);
e5329c37
NB
179 return 1;
180 }
3da92f27
NB
181
182 if (st->ss->load_super(st, fd2, NULL)) {
e7b84f9d 183 pr_err("cannot find super block on %s\n", dv);
4725bc31 184 close(nfd);
e5329c37 185 close(fd2);
4725bc31 186 free(st);
e5329c37
NB
187 return 1;
188 }
189 close(fd2);
190 }
191 /* Ok, looks good. Lets update the superblock and write it out to
192 * newdev.
193 */
aba69144 194
4b1ac34b
NB
195 info.disk.number = d;
196 info.disk.major = major(stb.st_rdev);
197 info.disk.minor = minor(stb.st_rdev);
198 info.disk.raid_disk = d;
199 info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE);
3da92f27 200 st->ss->update_super(st, &info, "linear-grow-new", newdev,
f752781f 201 0, 0, NULL);
e5329c37 202
3da92f27 203 if (st->ss->store_super(st, nfd)) {
e7b84f9d 204 pr_err("Cannot store new superblock on %s\n",
f752781f 205 newdev);
e5329c37
NB
206 close(nfd);
207 return 1;
208 }
e5329c37 209 close(nfd);
4b1ac34b
NB
210
211 if (ioctl(fd, ADD_NEW_DISK, &info.disk) != 0) {
e7b84f9d 212 pr_err("Cannot add new disk to this array\n");
e5329c37
NB
213 return 1;
214 }
215 /* Well, that seems to have worked.
216 * Now go through and update all superblocks
217 */
218
4b1ac34b 219 if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
e7b84f9d 220 pr_err("cannot get array info for %s\n", devname);
e5329c37
NB
221 return 1;
222 }
223
224 nd = d;
4b1ac34b 225 for (d=0 ; d < info.array.raid_disks ; d++) {
e5329c37
NB
226 mdu_disk_info_t disk;
227 char *dv;
228
229 disk.number = d;
230 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
e7b84f9d 231 pr_err("cannot get device detail for device %d\n",
e5329c37
NB
232 d);
233 return 1;
234 }
16c6fa80 235 dv = map_dev(disk.major, disk.minor, 1);
e5329c37 236 if (!dv) {
e7b84f9d 237 pr_err("cannot find device file for device %d\n",
e5329c37
NB
238 d);
239 return 1;
240 }
16c6fa80 241 fd2 = dev_open(dv, O_RDWR);
e5329c37 242 if (fd2 < 0) {
e7b84f9d 243 pr_err("cannot open device file %s\n", dv);
e5329c37
NB
244 return 1;
245 }
3da92f27 246 if (st->ss->load_super(st, fd2, NULL)) {
e7b84f9d 247 pr_err("cannot find super block on %s\n", dv);
e5329c37
NB
248 close(fd);
249 return 1;
250 }
4b1ac34b
NB
251 info.array.raid_disks = nd+1;
252 info.array.nr_disks = nd+1;
253 info.array.active_disks = nd+1;
254 info.array.working_disks = nd+1;
f752781f 255
3da92f27 256 st->ss->update_super(st, &info, "linear-grow-update", dv,
f752781f 257 0, 0, NULL);
aba69144 258
3da92f27 259 if (st->ss->store_super(st, fd2)) {
e7b84f9d 260 pr_err("Cannot store new superblock on %s\n", dv);
e5329c37
NB
261 close(fd2);
262 return 1;
263 }
264 close(fd2);
265 }
266
267 return 0;
268}
f5e166fe 269
50f01ba5 270int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
f5e166fe
NB
271{
272 /*
273 * First check that array doesn't have a bitmap
274 * Then create the bitmap
275 * Then add it
276 *
277 * For internal bitmaps, we need to check the version,
278 * find all the active devices, and write the bitmap block
279 * to all devices
280 */
281 mdu_bitmap_file_t bmf;
282 mdu_array_info_t array;
283 struct supertype *st;
4725bc31 284 char *subarray = NULL;
dcec9ee5
NB
285 int major = BITMAP_MAJOR_HI;
286 int vers = md_get_version(fd);
8fac0577 287 unsigned long long bitmapsize, array_size;
dcec9ee5
NB
288
289 if (vers < 9003) {
290 major = BITMAP_MAJOR_HOSTENDIAN;
e7b84f9d 291 pr_err("Warning - bitmaps created on this kernel"
b3bd581b
N
292 " are not portable\n"
293 " between different architectures. Consider upgrading"
294 " the Linux kernel.\n");
dcec9ee5 295 }
f5e166fe
NB
296
297 if (ioctl(fd, GET_BITMAP_FILE, &bmf) != 0) {
353632d9 298 if (errno == ENOMEM)
e7b84f9d 299 pr_err("Memory allocation failure.\n");
f5e166fe 300 else
e7b84f9d 301 pr_err("bitmaps not supported by this kernel.\n");
f5e166fe
NB
302 return 1;
303 }
304 if (bmf.pathname[0]) {
50f01ba5 305 if (strcmp(s->bitmap_file,"none")==0) {
fe80f49b 306 if (ioctl(fd, SET_BITMAP_FILE, -1)!= 0) {
e7b84f9d 307 pr_err("failed to remove bitmap %s\n",
fe80f49b
NB
308 bmf.pathname);
309 return 1;
310 }
311 return 0;
312 }
e7b84f9d 313 pr_err("%s already has a bitmap (%s)\n",
f5e166fe
NB
314 devname, bmf.pathname);
315 return 1;
316 }
317 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
e7b84f9d 318 pr_err("cannot get array status for %s\n", devname);
f5e166fe
NB
319 return 1;
320 }
321 if (array.state & (1<<MD_SB_BITMAP_PRESENT)) {
50f01ba5 322 if (strcmp(s->bitmap_file, "none")==0) {
fe80f49b
NB
323 array.state &= ~(1<<MD_SB_BITMAP_PRESENT);
324 if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
e7b84f9d 325 pr_err("failed to remove internal bitmap.\n");
fe80f49b
NB
326 return 1;
327 }
328 return 0;
329 }
e7b84f9d 330 pr_err("Internal bitmap already present on %s\n",
f5e166fe
NB
331 devname);
332 return 1;
333 }
4725bc31 334
50f01ba5 335 if (strcmp(s->bitmap_file, "none") == 0) {
e7b84f9d 336 pr_err("no bitmap found on %s\n", devname);
4725bc31
N
337 return 1;
338 }
5b28bd56 339 if (array.level <= 0) {
e7b84f9d 340 pr_err("Bitmaps not meaningful with level %s\n",
5b28bd56
NB
341 map_num(pers, array.level)?:"of this array");
342 return 1;
343 }
8fac0577
NB
344 bitmapsize = array.size;
345 bitmapsize <<= 1;
beae1dfe 346 if (get_dev_size(fd, NULL, &array_size) &&
8fac0577
NB
347 array_size > (0x7fffffffULL<<9)) {
348 /* Array is big enough that we cannot trust array.size
349 * try other approaches
350 */
351 bitmapsize = get_component_size(fd);
352 }
8fac0577 353 if (bitmapsize == 0) {
e7b84f9d 354 pr_err("Cannot reliably determine size of array to create bitmap - sorry.\n");
8fac0577
NB
355 return 1;
356 }
357
f9c25f1d 358 if (array.level == 10) {
8686f3ed 359 int ncopies = (array.layout&255)*((array.layout>>8)&255);
f9c25f1d
NB
360 bitmapsize = bitmapsize * array.raid_disks / ncopies;
361 }
362
4725bc31 363 st = super_by_fd(fd, &subarray);
f5e166fe 364 if (!st) {
e7b84f9d 365 pr_err("Cannot understand version %d.%d\n",
f5e166fe
NB
366 array.major_version, array.minor_version);
367 return 1;
368 }
4725bc31 369 if (subarray) {
e7b84f9d 370 pr_err("Cannot add bitmaps to sub-arrays yet\n");
4725bc31
N
371 free(subarray);
372 free(st);
fe80f49b 373 return 1;
4725bc31 374 }
50f01ba5 375 if (strcmp(s->bitmap_file, "internal") == 0) {
c0c1acd6 376 int rv;
f5e166fe 377 int d;
c0c1acd6
N
378 int offset_setable = 0;
379 struct mdinfo *mdi;
ebeb3663 380 if (st->ss->add_internal_bitmap == NULL) {
e7b84f9d 381 pr_err("Internal bitmaps not supported "
ebeb3663
N
382 "with %s metadata\n", st->ss->name);
383 return 1;
384 }
c0c1acd6
N
385 mdi = sysfs_read(fd, -1, GET_BITMAP_LOCATION);
386 if (mdi)
387 offset_setable = 1;
ea329559 388 for (d=0; d< st->max_devs; d++) {
f5e166fe
NB
389 mdu_disk_info_t disk;
390 char *dv;
391 disk.number = d;
392 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
393 continue;
394 if (disk.major == 0 &&
395 disk.minor == 0)
396 continue;
397 if ((disk.state & (1<<MD_DISK_SYNC))==0)
398 continue;
16c6fa80 399 dv = map_dev(disk.major, disk.minor, 1);
f5e166fe 400 if (dv) {
16c6fa80 401 int fd2 = dev_open(dv, O_RDWR);
f5e166fe
NB
402 if (fd2 < 0)
403 continue;
3da92f27 404 if (st->ss->load_super(st, fd2, NULL)==0) {
199171a2 405 if (st->ss->add_internal_bitmap(
3da92f27 406 st,
50f01ba5 407 &s->bitmap_chunk, c->delay, s->write_behind,
c0c1acd6
N
408 bitmapsize, offset_setable,
409 major)
199171a2 410 )
3da92f27 411 st->ss->write_bitmap(st, fd2);
21e92547 412 else {
ca3b6696
N
413 pr_err("failed to create internal bitmap"
414 " - chunksize problem.\n");
21e92547
NB
415 close(fd2);
416 return 1;
417 }
f5e166fe
NB
418 }
419 close(fd2);
420 }
421 }
c0c1acd6
N
422 if (offset_setable) {
423 st->ss->getinfo_super(st, mdi, NULL);
424 sysfs_init(mdi, fd, -1);
012a8641
JS
425 rv = sysfs_set_num_signed(mdi, NULL, "bitmap/location",
426 mdi->bitmap_offset);
c0c1acd6
N
427 } else {
428 array.state |= (1<<MD_SB_BITMAP_PRESENT);
429 rv = ioctl(fd, SET_ARRAY_INFO, &array);
430 }
431 if (rv < 0) {
ff634064 432 if (errno == EBUSY)
e7b84f9d
N
433 pr_err("Cannot add bitmap while array is"
434 " resyncing or reshaping etc.\n");
435 pr_err("failed to set internal bitmap.\n");
f5e166fe
NB
436 return 1;
437 }
fe80f49b
NB
438 } else {
439 int uuid[4];
440 int bitmap_fd;
441 int d;
442 int max_devs = st->max_devs;
fe80f49b
NB
443
444 /* try to load a superblock */
ca3b6696 445 for (d = 0; d < max_devs; d++) {
fe80f49b
NB
446 mdu_disk_info_t disk;
447 char *dv;
448 int fd2;
449 disk.number = d;
450 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
451 continue;
452 if ((disk.major==0 && disk.minor==0) ||
453 (disk.state & (1<<MD_DISK_REMOVED)))
454 continue;
16c6fa80 455 dv = map_dev(disk.major, disk.minor, 1);
ca3b6696
N
456 if (!dv)
457 continue;
16c6fa80 458 fd2 = dev_open(dv, O_RDONLY);
e7344e90
JS
459 if (fd2 >= 0) {
460 if (st->ss->load_super(st, fd2, NULL) == 0) {
461 close(fd2);
462 st->ss->uuid_from_super(st, uuid);
463 break;
464 }
fe80f49b 465 close(fd2);
fe80f49b 466 }
fe80f49b
NB
467 }
468 if (d == max_devs) {
e7b84f9d 469 pr_err("cannot find UUID for array!\n");
fe80f49b
NB
470 return 1;
471 }
50f01ba5
N
472 if (CreateBitmap(s->bitmap_file, c->force, (char*)uuid, s->bitmap_chunk,
473 c->delay, s->write_behind, bitmapsize, major)) {
fe80f49b
NB
474 return 1;
475 }
50f01ba5 476 bitmap_fd = open(s->bitmap_file, O_RDWR);
fe80f49b 477 if (bitmap_fd < 0) {
e7b84f9d 478 pr_err("weird: %s cannot be opened\n",
50f01ba5 479 s->bitmap_file);
fe80f49b
NB
480 return 1;
481 }
482 if (ioctl(fd, SET_BITMAP_FILE, bitmap_fd) < 0) {
ff634064
N
483 int err = errno;
484 if (errno == EBUSY)
e7b84f9d
N
485 pr_err("Cannot add bitmap while array is"
486 " resyncing or reshaping etc.\n");
487 pr_err("Cannot set bitmap file for %s: %s\n",
ff634064 488 devname, strerror(err));
fe80f49b
NB
489 return 1;
490 }
491 }
f5e166fe
NB
492
493 return 0;
494}
495
e86c9dd6
NB
496/*
497 * When reshaping an array we might need to backup some data.
498 * This is written to all spares with a 'super_block' describing it.
ff94fb86 499 * The superblock goes 4K from the end of the used space on the
e86c9dd6
NB
500 * device.
501 * It if written after the backup is complete.
502 * It has the following structure.
503 */
504
5fdf37e3 505static struct mdp_backup_super {
7236ee7a 506 char magic[16]; /* md_backup_data-1 or -2 */
e86c9dd6
NB
507 __u8 set_uuid[16];
508 __u64 mtime;
509 /* start/sizes in 512byte sectors */
7236ee7a 510 __u64 devstart; /* address on backup device/file of data */
e86c9dd6
NB
511 __u64 arraystart;
512 __u64 length;
513 __u32 sb_csum; /* csum of preceeding bytes. */
7236ee7a
N
514 __u32 pad1;
515 __u64 devstart2; /* offset in to data of second section */
516 __u64 arraystart2;
517 __u64 length2;
518 __u32 sb_csum2; /* csum of preceeding bytes. */
519 __u8 pad[512-68-32];
5fdf37e3 520} __attribute__((aligned(512))) bsb, bsb2;
e86c9dd6 521
4411fb17 522static __u32 bsb_csum(char *buf, int len)
e86c9dd6
NB
523{
524 int i;
525 int csum = 0;
ca3b6696 526 for (i = 0; i < len; i++)
e86c9dd6
NB
527 csum = (csum<<3) + buf[0];
528 return __cpu_to_le32(csum);
529}
530
d7ca196c
N
531static int check_idle(struct supertype *st)
532{
533 /* Check that all member arrays for this container, or the
534 * container of this array, are idle
535 */
536 int container_dev = (st->container_dev != NoMdDev
537 ? st->container_dev : st->devnum);
538 char container[40];
539 struct mdstat_ent *ent, *e;
540 int is_idle = 1;
24daa16f 541
d7ca196c
N
542 fmt_devname(container, container_dev);
543 ent = mdstat_read(0, 0);
544 for (e = ent ; e; e = e->next) {
545 if (!is_container_member(e, container))
546 continue;
547 if (e->percent >= 0) {
548 is_idle = 0;
549 break;
550 }
551 }
552 free_mdstat(ent);
553 return is_idle;
554}
555
7f2ba464 556static int freeze_container(struct supertype *st)
7236ee7a 557{
7f2ba464
DW
558 int container_dev = (st->container_dev != NoMdDev
559 ? st->container_dev : st->devnum);
d7ca196c 560 char container[40];
7f2ba464 561
d7ca196c
N
562 if (!check_idle(st))
563 return -1;
24daa16f 564
d7ca196c 565 fmt_devname(container, container_dev);
7f2ba464
DW
566
567 if (block_monitor(container, 1)) {
e7b84f9d 568 pr_err("failed to freeze container\n");
7f2ba464
DW
569 return -2;
570 }
571
572 return 1;
573}
574
575static void unfreeze_container(struct supertype *st)
576{
577 int container_dev = (st->container_dev != NoMdDev
578 ? st->container_dev : st->devnum);
d7ca196c 579 char container[40];
24daa16f 580
d7ca196c 581 fmt_devname(container, container_dev);
7f2ba464
DW
582
583 unblock_monitor(container, 1);
584}
585
586static int freeze(struct supertype *st)
587{
588 /* Try to freeze resync/rebuild on this array/container.
7236ee7a 589 * Return -1 if the array is busy,
7f2ba464 590 * return -2 container cannot be frozen,
7236ee7a
N
591 * return 0 if this kernel doesn't support 'frozen'
592 * return 1 if it worked.
593 */
7f2ba464
DW
594 if (st->ss->external)
595 return freeze_container(st);
596 else {
597 struct mdinfo *sra = sysfs_read(-1, st->devnum, GET_VERSION);
598 int err;
815c8a7e 599 char buf[20];
7f2ba464
DW
600
601 if (!sra)
602 return -1;
815c8a7e
N
603 /* Need to clear any 'read-auto' status */
604 if (sysfs_get_str(sra, NULL, "array_state", buf, 20) > 0 &&
605 strncmp(buf, "read-auto", 9) == 0)
606 sysfs_set_str(sra, NULL, "array_state", "clean");
607
7f2ba464
DW
608 err = sysfs_freeze_array(sra);
609 sysfs_free(sra);
610 return err;
611 }
7236ee7a
N
612}
613
9202b8eb 614static void unfreeze(struct supertype *st)
7236ee7a 615{
7f2ba464
DW
616 if (st->ss->external)
617 return unfreeze_container(st);
618 else {
619 struct mdinfo *sra = sysfs_read(-1, st->devnum, GET_VERSION);
620
621 if (sra)
622 sysfs_set_str(sra, NULL, "sync_action", "idle");
7f2ba464
DW
623 sysfs_free(sra);
624 }
7236ee7a
N
625}
626
4411fb17 627static void wait_reshape(struct mdinfo *sra)
7236ee7a
N
628{
629 int fd = sysfs_get_fd(sra, NULL, "sync_action");
630 char action[20];
631
92a19f1a
AK
632 if (fd < 0)
633 return;
634
635 while (sysfs_fd_get_str(fd, action, 20) > 0 &&
636 strncmp(action, "reshape", 7) == 0) {
7236ee7a
N
637 fd_set rfds;
638 FD_ZERO(&rfds);
639 FD_SET(fd, &rfds);
640 select(fd+1, NULL, NULL, &rfds, NULL);
92a19f1a
AK
641 }
642 close(fd);
7236ee7a 643}
7bc71196 644
7ec0996c
LD
645static int reshape_super(struct supertype *st, unsigned long long size,
646 int level, int layout, int chunksize, int raid_disks,
41784c88 647 int delta_disks, char *backup_file, char *dev,
016e00f5 648 int direction, int verbose)
7bc71196
DW
649{
650 /* nothing extra to check in the native case */
651 if (!st->ss->external)
652 return 0;
653 if (!st->ss->reshape_super ||
654 !st->ss->manage_reshape) {
e7b84f9d 655 pr_err("%s metadata does not support reshape\n",
7bc71196
DW
656 st->ss->name);
657 return 1;
658 }
659
660 return st->ss->reshape_super(st, size, level, layout, chunksize,
41784c88 661 raid_disks, delta_disks, backup_file, dev,
016e00f5 662 direction, verbose);
7bc71196
DW
663}
664
665static void sync_metadata(struct supertype *st)
666{
667 if (st->ss->external) {
76266030 668 if (st->update_tail) {
7bc71196 669 flush_metadata_updates(st);
76266030
N
670 st->update_tail = &st->updates;
671 } else
7bc71196
DW
672 st->ss->sync_metadata(st);
673 }
674}
675
676static int subarray_set_num(char *container, struct mdinfo *sra, char *name, int n)
677{
678 /* when dealing with external metadata subarrays we need to be
679 * prepared to handle EAGAIN. The kernel may need to wait for
680 * mdmon to mark the array active so the kernel can handle
681 * allocations/writeback when preparing the reshape action
682 * (md_allow_write()). We temporarily disable safe_mode_delay
683 * to close a race with the array_state going clean before the
684 * next write to raid_disks / stripe_cache_size
685 */
686 char safe[50];
687 int rc;
688
689 /* only 'raid_disks' and 'stripe_cache_size' trigger md_allow_write */
5da9ab98
N
690 if (!container ||
691 (strcmp(name, "raid_disks") != 0 &&
692 strcmp(name, "stripe_cache_size") != 0))
7bc71196
DW
693 return sysfs_set_num(sra, NULL, name, n);
694
695 rc = sysfs_get_str(sra, NULL, "safe_mode_delay", safe, sizeof(safe));
696 if (rc <= 0)
697 return -1;
698 sysfs_set_num(sra, NULL, "safe_mode_delay", 0);
699 rc = sysfs_set_num(sra, NULL, name, n);
700 if (rc < 0 && errno == EAGAIN) {
701 ping_monitor(container);
702 /* if we get EAGAIN here then the monitor is not active
703 * so stop trying
704 */
705 rc = sysfs_set_num(sra, NULL, name, n);
706 }
707 sysfs_set_str(sra, NULL, "safe_mode_delay", safe);
708 return rc;
709}
710
27a1e5b5
N
711int start_reshape(struct mdinfo *sra, int already_running,
712 int before_data_disks, int data_disks)
47eb4d5a
N
713{
714 int err;
6937e6d2
AK
715 unsigned long long sync_max_to_set;
716
0f28668b 717 sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
6937e6d2
AK
718 err = sysfs_set_num(sra, NULL, "suspend_hi", sra->reshape_progress);
719 err = err ?: sysfs_set_num(sra, NULL, "suspend_lo",
720 sra->reshape_progress);
27a1e5b5
N
721 if (before_data_disks <= data_disks)
722 sync_max_to_set = sra->reshape_progress / data_disks;
723 else
724 sync_max_to_set = (sra->component_size * data_disks
725 - sra->reshape_progress) / data_disks;
1e971e71 726 if (!already_running)
6937e6d2
AK
727 sysfs_set_num(sra, NULL, "sync_min", sync_max_to_set);
728 err = err ?: sysfs_set_num(sra, NULL, "sync_max", sync_max_to_set);
1e971e71
N
729 if (!already_running)
730 err = err ?: sysfs_set_str(sra, NULL, "sync_action", "reshape");
47eb4d5a
N
731
732 return err;
733}
734
735void abort_reshape(struct mdinfo *sra)
736{
737 sysfs_set_str(sra, NULL, "sync_action", "idle");
738 sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
739 sysfs_set_num(sra, NULL, "suspend_hi", 0);
740 sysfs_set_num(sra, NULL, "suspend_lo", 0);
741 sysfs_set_num(sra, NULL, "sync_min", 0);
742 sysfs_set_str(sra, NULL, "sync_max", "max");
743}
744
dfe77a9e
KW
745int remove_disks_for_takeover(struct supertype *st,
746 struct mdinfo *sra,
747 int layout)
62a48395
AK
748{
749 int nr_of_copies;
750 struct mdinfo *remaining;
751 int slot;
752
dfe77a9e
KW
753 if (sra->array.level == 10)
754 nr_of_copies = layout & 0xff;
755 else if (sra->array.level == 1)
756 nr_of_copies = sra->array.raid_disks;
757 else
758 return 1;
62a48395
AK
759
760 remaining = sra->devs;
761 sra->devs = NULL;
762 /* for each 'copy', select one device and remove from the list. */
763 for (slot = 0; slot < sra->array.raid_disks; slot += nr_of_copies) {
764 struct mdinfo **diskp;
765 int found = 0;
766
767 /* Find a working device to keep */
768 for (diskp = &remaining; *diskp ; diskp = &(*diskp)->next) {
769 struct mdinfo *disk = *diskp;
770
771 if (disk->disk.raid_disk < slot)
772 continue;
773 if (disk->disk.raid_disk >= slot + nr_of_copies)
774 continue;
775 if (disk->disk.state & (1<<MD_DISK_REMOVED))
776 continue;
777 if (disk->disk.state & (1<<MD_DISK_FAULTY))
778 continue;
779 if (!(disk->disk.state & (1<<MD_DISK_SYNC)))
780 continue;
781
782 /* We have found a good disk to use! */
783 *diskp = disk->next;
784 disk->next = sra->devs;
785 sra->devs = disk;
786 found = 1;
787 break;
788 }
789 if (!found)
790 break;
791 }
792
793 if (slot < sra->array.raid_disks) {
794 /* didn't find all slots */
795 struct mdinfo **e;
796 e = &remaining;
797 while (*e)
798 e = &(*e)->next;
799 *e = sra->devs;
800 sra->devs = remaining;
801 return 1;
802 }
803
804 /* Remove all 'remaining' devices from the array */
805 while (remaining) {
806 struct mdinfo *sd = remaining;
807 remaining = sd->next;
808
809 sysfs_set_str(sra, sd, "state", "faulty");
810 sysfs_set_str(sra, sd, "slot", "none");
d5ca4a23
KW
811 /* for external metadata disks should be removed in mdmon */
812 if (!st->ss->external)
813 sysfs_set_str(sra, sd, "state", "remove");
62a48395
AK
814 sd->disk.state |= (1<<MD_DISK_REMOVED);
815 sd->disk.state &= ~(1<<MD_DISK_SYNC);
816 sd->next = sra->devs;
817 sra->devs = sd;
818 }
819 return 0;
820}
821
130994cb
AK
822void reshape_free_fdlist(int *fdlist,
823 unsigned long long *offsets,
824 int size)
825{
826 int i;
827
828 for (i = 0; i < size; i++)
829 if (fdlist[i] >= 0)
830 close(fdlist[i]);
831
832 free(fdlist);
833 free(offsets);
834}
835
836int reshape_prepare_fdlist(char *devname,
837 struct mdinfo *sra,
838 int raid_disks,
839 int nrdisks,
840 unsigned long blocks,
841 char *backup_file,
842 int *fdlist,
843 unsigned long long *offsets)
844{
845 int d = 0;
846 struct mdinfo *sd;
847
848 for (d = 0; d <= nrdisks; d++)
849 fdlist[d] = -1;
850 d = raid_disks;
851 for (sd = sra->devs; sd; sd = sd->next) {
852 if (sd->disk.state & (1<<MD_DISK_FAULTY))
853 continue;
854 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
855 char *dn = map_dev(sd->disk.major,
856 sd->disk.minor, 1);
857 fdlist[sd->disk.raid_disk]
858 = dev_open(dn, O_RDONLY);
859 offsets[sd->disk.raid_disk] = sd->data_offset*512;
860 if (fdlist[sd->disk.raid_disk] < 0) {
e7b84f9d
N
861 pr_err("%s: cannot open component %s\n",
862 devname, dn ? dn : "-unknown-");
130994cb
AK
863 d = -1;
864 goto release;
865 }
866 } else if (backup_file == NULL) {
867 /* spare */
868 char *dn = map_dev(sd->disk.major,
869 sd->disk.minor, 1);
870 fdlist[d] = dev_open(dn, O_RDWR);
871 offsets[d] = (sd->data_offset + sra->component_size - blocks - 8)*512;
872 if (fdlist[d] < 0) {
e7b84f9d 873 pr_err("%s: cannot open component %s\n",
130994cb
AK
874 devname, dn ? dn : "-unknown-");
875 d = -1;
876 goto release;
877 }
878 d++;
879 }
880 }
881release:
882 return d;
883}
884
e6e9d47b
AK
885int reshape_open_backup_file(char *backup_file,
886 int fd,
887 char *devname,
888 long blocks,
889 int *fdlist,
a93f87ee
N
890 unsigned long long *offsets,
891 int restart)
e6e9d47b
AK
892{
893 /* Return 1 on success, 0 on any form of failure */
894 /* need to check backup file is large enough */
895 char buf[512];
896 struct stat stb;
897 unsigned int dev;
898 int i;
899
a93f87ee 900 *fdlist = open(backup_file, O_RDWR|O_CREAT|(restart ? O_TRUNC : O_EXCL),
e6e9d47b
AK
901 S_IRUSR | S_IWUSR);
902 *offsets = 8 * 512;
903 if (*fdlist < 0) {
e7b84f9d 904 pr_err("%s: cannot create backup file %s: %s\n",
e6e9d47b
AK
905 devname, backup_file, strerror(errno));
906 return 0;
907 }
908 /* Guard against backup file being on array device.
909 * If array is partitioned or if LVM etc is in the
910 * way this will not notice, but it is better than
911 * nothing.
912 */
913 fstat(*fdlist, &stb);
914 dev = stb.st_dev;
915 fstat(fd, &stb);
916 if (stb.st_rdev == dev) {
e7b84f9d 917 pr_err("backup file must NOT be"
e6e9d47b
AK
918 " on the array being reshaped.\n");
919 close(*fdlist);
920 return 0;
921 }
922
923 memset(buf, 0, 512);
ca4fe0bf 924 for (i=0; i < blocks + 8 ; i++) {
e6e9d47b 925 if (write(*fdlist, buf, 512) != 512) {
e7b84f9d 926 pr_err("%s: cannot create"
e6e9d47b
AK
927 " backup file %s: %s\n",
928 devname, backup_file, strerror(errno));
929 return 0;
930 }
931 }
932 if (fsync(*fdlist) != 0) {
e7b84f9d 933 pr_err("%s: cannot create backup file %s: %s\n",
e6e9d47b
AK
934 devname, backup_file, strerror(errno));
935 return 0;
936 }
937
938 return 1;
939}
940
b48e2e25
N
941unsigned long GCD(unsigned long a, unsigned long b)
942{
943 while (a != b) {
944 if (a < b)
945 b -= a;
946 if (b < a)
947 a -= b;
948 }
949 return a;
950}
951
1c009fc2
AK
952unsigned long compute_backup_blocks(int nchunk, int ochunk,
953 unsigned int ndata, unsigned int odata)
954{
955 unsigned long a, b, blocks;
956 /* So how much do we need to backup.
957 * We need an amount of data which is both a whole number of
958 * old stripes and a whole number of new stripes.
959 * So LCM for (chunksize*datadisks).
960 */
961 a = (ochunk/512) * odata;
962 b = (nchunk/512) * ndata;
963 /* Find GCD */
b48e2e25 964 a = GCD(a, b);
1c009fc2
AK
965 /* LCM == product / GCD */
966 blocks = (ochunk/512) * (nchunk/512) * odata * ndata / a;
967
968 return blocks;
969}
970
5da9ab98
N
971char *analyse_change(struct mdinfo *info, struct reshape *re)
972{
973 /* Based on the current array state in info->array and
974 * the changes in info->new_* etc, determine:
975 * - whether the change is possible
976 * - Intermediate level/raid_disks/layout
977 * - whether a restriping reshape is needed
978 * - number of sectors in minimum change unit. This
979 * will cover a whole number of stripes in 'before' and
980 * 'after'.
981 *
982 * Return message if the change should be rejected
983 * NULL if the change can be achieved
984 *
985 * This can be called as part of starting a reshape, or
986 * when assembling an array that is undergoing reshape.
987 */
19ceb16d 988 int near, far, offset, copies;
5da9ab98 989 int new_disks;
19ceb16d 990 int old_chunk, new_chunk;
b545e14a
N
991 /* delta_parity records change in number of devices
992 * caused by level change
993 */
994 int delta_parity = 0;
5da9ab98
N
995
996 /* If a new level not explicitly given, we assume no-change */
997 if (info->new_level == UnSet)
998 info->new_level = info->array.level;
999
1000 if (info->new_chunk)
1001 switch (info->new_level) {
1002 case 0:
1003 case 4:
1004 case 5:
1005 case 6:
1006 case 10:
1007 /* chunk size is meaningful, must divide component_size
1008 * evenly
1009 */
1010 if (info->component_size % (info->new_chunk/512))
1011 return "New chunk size does not"
1012 " divide component size";
1013 break;
1014 default:
1015 return "chunk size not meaningful for this level";
1016 }
1017 else
1018 info->new_chunk = info->array.chunk_size;
1019
1020 switch (info->array.level) {
90b60dfa
N
1021 default:
1022 return "Cannot understand this RAID level";
5da9ab98
N
1023 case 1:
1024 /* RAID1 can convert to RAID1 with different disks, or
dfe77a9e
KW
1025 * raid5 with 2 disks, or
1026 * raid0 with 1 disk
5da9ab98 1027 */
5ca3a902
N
1028 if (info->new_level > 1 &&
1029 (info->component_size & 7))
1030 return "Cannot convert RAID1 of this size - "
1031 "reduce size to multiple of 4K first.";
dfe77a9e 1032 if (info->new_level == 0) {
b545e14a
N
1033 if (info->delta_disks != UnSet &&
1034 info->delta_disks != 0)
1035 return "Cannot change number of disks "
1036 "with RAID1->RAID0 conversion";
dfe77a9e
KW
1037 re->level = 0;
1038 re->before.data_disks = 1;
1039 re->after.data_disks = 1;
1040 re->before.layout = 0;
1041 re->backup_blocks = 0;
1042 re->parity = 0;
1043 return NULL;
1044 }
5da9ab98
N
1045 if (info->new_level == 1) {
1046 if (info->delta_disks == UnSet)
1047 /* Don't know what to do */
1048 return "no change requested for Growing RAID1";
1049 re->level = 1;
b4f8e38b 1050 re->backup_blocks = 0;
5da9ab98
N
1051 re->parity = 0;
1052 return NULL;
1053 }
1054 if (info->array.raid_disks == 2 &&
446d2a5a 1055 info->new_level == 5) {
5652f2d9 1056
5da9ab98 1057 re->level = 5;
5da9ab98 1058 re->before.data_disks = 1;
5652f2d9
N
1059 if (info->delta_disks != UnSet &&
1060 info->delta_disks != 0)
1061 re->after.data_disks = 1 + info->delta_disks;
1062 else
1063 re->after.data_disks = 1;
1064 if (re->after.data_disks < 1)
1065 return "Number of disks too small for RAID5";
1066
5da9ab98 1067 re->before.layout = ALGORITHM_LEFT_SYMMETRIC;
446d2a5a
N
1068 info->array.chunk_size = 65536;
1069 break;
5da9ab98
N
1070 }
1071 /* Could do some multi-stage conversions, but leave that to
1072 * later.
1073 */
1074 return "Impossibly level change request for RAID1";
1075
1076 case 10:
19ceb16d
N
1077 /* RAID10 can be converted from near mode to
1078 * RAID0 by removing some devices.
1079 * It can also be reshaped if the kernel supports
1080 * new_data_offset.
5da9ab98 1081 */
19ceb16d
N
1082 switch (info->new_level) {
1083 case 0:
1084 if ((info->array.layout & ~0xff) != 0x100)
1085 return "Cannot Grow RAID10 with far/offset layout";
1086 /* number of devices must be multiple of number of copies */
1087 if (info->array.raid_disks % (info->array.layout & 0xff))
1088 return "RAID10 layout too complex for Grow operation";
1089
1090 new_disks = (info->array.raid_disks
1091 / (info->array.layout & 0xff));
1092 if (info->delta_disks == UnSet)
1093 info->delta_disks = (new_disks
1094 - info->array.raid_disks);
5da9ab98 1095
19ceb16d
N
1096 if (info->delta_disks != new_disks - info->array.raid_disks)
1097 return "New number of raid-devices impossible for RAID10";
1098 if (info->new_chunk &&
1099 info->new_chunk != info->array.chunk_size)
1100 return "Cannot change chunk-size with RAID10 Grow";
1101
1102 /* looks good */
1103 re->level = 0;
1104 re->parity = 0;
1105 re->before.data_disks = new_disks;
1106 re->after.data_disks = re->before.data_disks;
1107 re->before.layout = 0;
1108 re->backup_blocks = 0;
1109 return NULL;
1110
1111 case 10:
1112 near = info->array.layout & 0xff;
1113 far = (info->array.layout >> 8) & 0xff;
1114 offset = info->array.layout & 0x10000;
1115 if (far > 1 && !offset)
1116 return "Cannot reshape RAID10 in far-mode";
1117 copies = near * far;
1118
1119 old_chunk = info->array.chunk_size * far;
1120
1121 if (info->new_layout == UnSet)
1122 info->new_layout = info->array.layout;
1123 else {
1124 near = info->new_layout & 0xff;
1125 far = (info->new_layout >> 8) & 0xff;
1126 offset = info->new_layout & 0x10000;
1127 if (far > 1 && !offset)
1128 return "Cannot reshape RAID10 to far-mode";
1129 if (near * far != copies)
1130 return "Cannot change number of copies"
1131 " when reshaping RAID10";
1132 }
1133 if (info->delta_disks == UnSet)
1134 info->delta_disks = 0;
1135 new_disks = (info->array.raid_disks +
1136 info->delta_disks);
1137
1138 new_chunk = info->new_chunk * far;
1139
1140 re->level = 10;
1141 re->parity = 0;
1142 re->before.layout = info->array.layout;
1143 re->before.data_disks = info->array.raid_disks;
1144 re->after.layout = info->new_layout;
1145 re->after.data_disks = new_disks;
1146 /* For RAID10 we don't do backup, and there is
1147 * no need to synchronise stripes on both
1148 * 'old' and 'new'. So the important
1149 * number is the minimum data_offset difference
1150 * which is the larger of (offset copies * chunk).
1151 */
1152
1153 re->backup_blocks = max(old_chunk, new_chunk) / 512;
ee2429e0
N
1154 if (new_disks < re->before.data_disks &&
1155 info->space_after < re->backup_blocks)
1156 /* Reduce component size by one chunk */
1157 re->new_size = (info->component_size -
1158 re->backup_blocks);
1159 else
1160 re->new_size = info->component_size;
1161 re->new_size = re->new_size * new_disks / copies;
19ceb16d 1162 return NULL;
5da9ab98 1163
19ceb16d
N
1164 default:
1165 return "RAID10 can only be changed to RAID0";
1166 }
5da9ab98
N
1167 case 0:
1168 /* RAID0 can be converted to RAID10, or to RAID456 */
1169 if (info->new_level == 10) {
1170 if (info->new_layout == UnSet && info->delta_disks == UnSet) {
1171 /* Assume near=2 layout */
1172 info->new_layout = 0x102;
1173 info->delta_disks = info->array.raid_disks;
1174 }
1175 if (info->new_layout == UnSet) {
1176 int copies = 1 + (info->delta_disks
1177 / info->array.raid_disks);
1178 if (info->array.raid_disks * (copies-1)
1179 != info->delta_disks)
1180 return "Impossible number of devices"
1181 " for RAID0->RAID10";
1182 info->new_layout = 0x100 + copies;
1183 }
1184 if (info->delta_disks == UnSet) {
1185 int copies = info->new_layout & 0xff;
1186 if (info->new_layout != 0x100 + copies)
1187 return "New layout impossible"
1188 " for RAID0->RAID10";;
1189 info->delta_disks = (copies - 1) *
1190 info->array.raid_disks;
1191 }
1192 if (info->new_chunk &&
1193 info->new_chunk != info->array.chunk_size)
1194 return "Cannot change chunk-size with RAID0->RAID10";
1195 /* looks good */
1196 re->level = 10;
1197 re->parity = 0;
1198 re->before.data_disks = (info->array.raid_disks +
1199 info->delta_disks);
031d445c 1200 re->after.data_disks = re->before.data_disks;
5da9ab98 1201 re->before.layout = info->new_layout;
b4f8e38b 1202 re->backup_blocks = 0;
5da9ab98
N
1203 return NULL;
1204 }
1205
1206 /* RAID0 can also covert to RAID0/4/5/6 by first converting to
1207 * a raid4 style layout of the final level.
1208 */
1209 switch (info->new_level) {
5da9ab98 1210 case 4:
b545e14a
N
1211 delta_parity = 1;
1212 case 0:
5da9ab98
N
1213 re->level = 4;
1214 re->before.layout = 0;
1215 break;
1216 case 5:
b545e14a 1217 delta_parity = 1;
5da9ab98
N
1218 re->level = 5;
1219 re->before.layout = ALGORITHM_PARITY_N;
1220 break;
1221 case 6:
b545e14a 1222 delta_parity = 2;
5da9ab98
N
1223 re->level = 6;
1224 re->before.layout = ALGORITHM_PARITY_N;
1225 break;
1226 default:
1227 return "Impossible level change requested";
1228 }
1229 re->before.data_disks = info->array.raid_disks;
1230 /* determining 'after' layout happens outside this 'switch' */
1231 break;
1232
1233 case 4:
1234 info->array.layout = ALGORITHM_PARITY_N;
1235 case 5:
1236 switch (info->new_level) {
f0cce442 1237 case 0:
b545e14a 1238 delta_parity = -1;
5da9ab98
N
1239 case 4:
1240 re->level = info->array.level;
1241 re->before.data_disks = info->array.raid_disks - 1;
1242 re->before.layout = info->array.layout;
1243 break;
1244 case 5:
1245 re->level = 5;
1246 re->before.data_disks = info->array.raid_disks - 1;
1247 re->before.layout = info->array.layout;
1248 break;
1249 case 6:
b545e14a 1250 delta_parity = 1;
5da9ab98
N
1251 re->level = 6;
1252 re->before.data_disks = info->array.raid_disks - 1;
1253 switch (info->array.layout) {
1254 case ALGORITHM_LEFT_ASYMMETRIC:
1255 re->before.layout = ALGORITHM_LEFT_ASYMMETRIC_6;
1256 break;
1257 case ALGORITHM_RIGHT_ASYMMETRIC:
1258 re->before.layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
1259 break;
1260 case ALGORITHM_LEFT_SYMMETRIC:
1261 re->before.layout = ALGORITHM_LEFT_SYMMETRIC_6;
1262 break;
1263 case ALGORITHM_RIGHT_SYMMETRIC:
1264 re->before.layout = ALGORITHM_RIGHT_SYMMETRIC_6;
1265 break;
1266 case ALGORITHM_PARITY_0:
1267 re->before.layout = ALGORITHM_PARITY_0_6;
1268 break;
1269 case ALGORITHM_PARITY_N:
1270 re->before.layout = ALGORITHM_PARITY_N_6;
1271 break;
1272 default:
1273 return "Cannot convert an array with this layout";
1274 }
1275 break;
1276 case 1:
1277 if (info->array.raid_disks != 2)
1278 return "Can only convert a 2-device array to RAID1";
b545e14a
N
1279 if (info->delta_disks != UnSet &&
1280 info->delta_disks != 0)
1281 return "Cannot set raid_disk when "
1282 "converting RAID5->RAID1";
5da9ab98 1283 re->level = 1;
6a67848a
N
1284 re->backup_blocks = 0;
1285 info->new_chunk = 0;
1286 return NULL;
5da9ab98
N
1287 default:
1288 return "Impossible level change requested";
1289 }
1290 break;
1291 case 6:
1292 switch (info->new_level) {
1293 case 4:
1294 case 5:
b545e14a 1295 delta_parity = -1;
5da9ab98
N
1296 case 6:
1297 re->level = 6;
1298 re->before.data_disks = info->array.raid_disks - 2;
1299 re->before.layout = info->array.layout;
1300 break;
1301 default:
1302 return "Impossible level change requested";
1303 }
1304 break;
1305 }
1306
1307 /* If we reached here then it looks like a re-stripe is
1308 * happening. We have determined the intermediate level
1309 * and initial raid_disks/layout and stored these in 're'.
1310 *
1311 * We need to deduce the final layout that can be atomically
1312 * converted to the end state.
1313 */
1314 switch (info->new_level) {
1315 case 0:
1316 /* We can only get to RAID0 from RAID4 or RAID5
1317 * with appropriate layout and one extra device
1318 */
1319 if (re->level != 4 && re->level != 5)
1320 return "Cannot covert to RAID0 from this level";
b545e14a 1321
5da9ab98
N
1322 switch (re->level) {
1323 case 4:
ca3b6696
N
1324 re->after.layout = 0;
1325 break;
5da9ab98 1326 case 5:
ca3b6696
N
1327 re->after.layout = ALGORITHM_PARITY_N;
1328 break;
5da9ab98
N
1329 }
1330 break;
1331
1332 case 4:
1333 /* We can only get to RAID4 from RAID5 */
1334 if (re->level != 4 && re->level != 5)
1335 return "Cannot convert to RAID4 from this level";
b545e14a 1336
5da9ab98
N
1337 switch (re->level) {
1338 case 4:
ca3b6696
N
1339 re->after.layout = 0;
1340 break;
5da9ab98 1341 case 5:
ca3b6696
N
1342 re->after.layout = ALGORITHM_PARITY_N;
1343 break;
5da9ab98
N
1344 }
1345 break;
1346
1347 case 5:
0073a6e1 1348 /* We get to RAID5 from RAID5 or RAID6 */
5da9ab98
N
1349 if (re->level != 5 && re->level != 6)
1350 return "Cannot convert to RAID5 from this level";
b545e14a 1351
5da9ab98
N
1352 switch (re->level) {
1353 case 5:
1354 if (info->new_layout == UnSet)
1355 re->after.layout = re->before.layout;
1356 else
1357 re->after.layout = info->new_layout;
1358 break;
1359 case 6:
4a870364
N
1360 if (info->new_layout == UnSet)
1361 info->new_layout = re->before.layout;
1362
5da9ab98
N
1363 /* after.layout needs to be raid6 version of new_layout */
1364 if (info->new_layout == ALGORITHM_PARITY_N)
1365 re->after.layout = ALGORITHM_PARITY_N;
1366 else {
1367 char layout[40];
1368 char *ls = map_num(r5layout, info->new_layout);
1369 int l;
0073a6e1
N
1370 if (ls) {
1371 /* Current RAID6 layout has a RAID5
1372 * equivalent - good
1373 */
1374 strcat(strcpy(layout, ls), "-6");
1375 l = map_name(r6layout, layout);
1376 if (l == UnSet)
1377 return "Cannot find RAID6 layout"
1378 " to convert to";
1379 } else {
1380 /* Current RAID6 has no equivalent.
1381 * If it is already a '-6' layout we
1382 * can leave it unchanged, else we must
1383 * fail
1384 */
1385 ls = map_num(r6layout, info->new_layout);
1386 if (!ls ||
1387 strcmp(ls+strlen(ls)-2, "-6") != 0)
1388 return "Please specify new layout";
1389 l = info->new_layout;
1390 }
5da9ab98
N
1391 re->after.layout = l;
1392 }
1393 }
1394 break;
1395
1396 case 6:
1397 /* We must already be at level 6 */
1398 if (re->level != 6)
1399 return "Impossible level change";
5da9ab98 1400 if (info->new_layout == UnSet)
4a870364 1401 re->after.layout = info->array.layout;
5da9ab98
N
1402 else
1403 re->after.layout = info->new_layout;
1404 break;
1405 default:
1406 return "Impossible level change requested";
1407 }
b545e14a
N
1408 if (info->delta_disks == UnSet)
1409 info->delta_disks = delta_parity;
1410
1411 re->after.data_disks = (re->before.data_disks
1412 + info->delta_disks
1413 - delta_parity);
5da9ab98 1414 switch (re->level) {
ca3b6696
N
1415 case 6: re->parity = 2;
1416 break;
5da9ab98 1417 case 4:
ca3b6696
N
1418 case 5: re->parity = 1;
1419 break;
1420 default: re->parity = 0;
1421 break;
5da9ab98
N
1422 }
1423 /* So we have a restripe operation, we need to calculate the number
1424 * of blocks per reshape operation.
1425 */
1426 if (info->new_chunk == 0)
1427 info->new_chunk = info->array.chunk_size;
1428 if (re->after.data_disks == re->before.data_disks &&
1429 re->after.layout == re->before.layout &&
1430 info->new_chunk == info->array.chunk_size) {
1431 /* Nothing to change */
b4f8e38b 1432 re->backup_blocks = 0;
5da9ab98
N
1433 return NULL;
1434 }
1435 if (re->after.data_disks == 1 && re->before.data_disks == 1) {
446d2a5a 1436 /* chunk and layout changes make no difference */
b4f8e38b 1437 re->backup_blocks = 0;
5da9ab98
N
1438 return NULL;
1439 }
1440
1441 if (re->after.data_disks == re->before.data_disks &&
1442 get_linux_version() < 2006032)
1443 return "in-place reshape is not safe before 2.6.32 - sorry.";
1444
1445 if (re->after.data_disks < re->before.data_disks &&
1446 get_linux_version() < 2006030)
508ede86 1447 return "reshape to fewer devices is not supported before 2.6.30 - sorry.";
5da9ab98 1448
b4f8e38b 1449 re->backup_blocks = compute_backup_blocks(
5da9ab98
N
1450 info->new_chunk, info->array.chunk_size,
1451 re->after.data_disks,
1452 re->before.data_disks);
1453
1454 re->new_size = info->component_size * re->after.data_disks;
1455 return NULL;
1456}
1457
54397ed9
AK
1458static int set_array_size(struct supertype *st, struct mdinfo *sra,
1459 char *text_version)
1460{
1461 struct mdinfo *info;
1462 char *subarray;
1463 int ret_val = -1;
1464
1465 if ((st == NULL) || (sra == NULL))
1466 return ret_val;
1467
1468 if (text_version == NULL)
1469 text_version = sra->text_version;
1470 subarray = strchr(text_version+1, '/')+1;
1471 info = st->ss->container_content(st, subarray);
1472 if (info) {
1473 unsigned long long current_size = 0;
1474 unsigned long long new_size =
1475 info->custom_array_size/2;
1476
1477 if (sysfs_get_ll(sra, NULL, "array_size", &current_size) == 0 &&
1478 new_size > current_size) {
1479 if (sysfs_set_num(sra, NULL, "array_size", new_size)
1480 < 0)
1481 dprintf("Error: Cannot set array size");
1482 else {
1483 ret_val = 0;
1484 dprintf("Array size changed");
1485 }
1486 dprintf(" from %llu to %llu.\n",
1487 current_size, new_size);
1488 }
1489 sysfs_free(info);
1490 } else
1491 dprintf("Error: set_array_size(): info pointer in NULL\n");
1492
1493 return ret_val;
1494}
1495
5da9ab98
N
1496static int reshape_array(char *container, int fd, char *devname,
1497 struct supertype *st, struct mdinfo *info,
e2e53a2d 1498 int force, struct mddev_dev *devlist,
19ceb16d 1499 unsigned long long data_offset,
ba728be7 1500 char *backup_file, int verbose, int forked,
b76b30e0 1501 int restart, int freeze_reshape);
493f5dd6 1502static int reshape_container(char *container, char *devname,
9ad6f6e6 1503 int mdfd,
24daa16f 1504 struct supertype *st,
5da9ab98
N
1505 struct mdinfo *info,
1506 int force,
1507 char *backup_file,
ba728be7 1508 int verbose, int restart, int freeze_reshape);
1c009fc2 1509
32754b7d 1510int Grow_reshape(char *devname, int fd,
e2e53a2d 1511 struct mddev_dev *devlist,
40c9a66a 1512 unsigned long long data_offset,
32754b7d 1513 struct context *c, struct shape *s)
e86c9dd6
NB
1514{
1515 /* Make some changes in the shape of an array.
1516 * The kernel must support the change.
7236ee7a
N
1517 *
1518 * There are three different changes. Each can trigger
1519 * a resync or recovery so we freeze that until we have
1520 * requested everything (if kernel supports freezing - 2.6.30).
1521 * The steps are:
1522 * - change size (i.e. component_size)
1523 * - change level
1524 * - change layout/chunksize/ndisks
1525 *
1526 * The last can require a reshape. It is different on different
1527 * levels so we need to check the level before actioning it.
1528 * Some times the level change needs to be requested after the
1529 * reshape (e.g. raid6->raid5, raid5->raid0)
1530 *
e86c9dd6 1531 */
5da9ab98 1532 struct mdu_array_info_s array;
7236ee7a 1533 int rv = 0;
e86c9dd6 1534 struct supertype *st;
4725bc31 1535 char *subarray = NULL;
e86c9dd6 1536
7236ee7a 1537 int frozen;
7236ee7a 1538 int changed = 0;
7bc71196 1539 char *container = NULL;
5da9ab98 1540 char container_buf[20];
7bc71196 1541 int cfd = -1;
e86c9dd6 1542
e2e53a2d
N
1543 struct mddev_dev *dv;
1544 int added_disks;
1545
5da9ab98 1546 struct mdinfo info;
7e0f6979 1547 struct mdinfo *sra;
e86c9dd6 1548
40c9a66a 1549
e86c9dd6 1550 if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) {
19ceb16d 1551 fprintf(stderr, Name ": %s is not an active md array - aborting\n",
e86c9dd6
NB
1552 devname);
1553 return 1;
1554 }
19ceb16d
N
1555 if (data_offset != INVALID_SECTORS && array.level != 10) {
1556 pr_err("--grow --data-offset not yet supported\n");
1557 return 1;
1558 }
24d40069 1559
32754b7d
N
1560 if (s->size > 0 &&
1561 (s->chunk || s->level!= UnSet || s->layout_str || s->raiddisks)) {
e7b84f9d 1562 pr_err("cannot change component size at the same time "
9ce510be
N
1563 "as other changes.\n"
1564 " Change size first, then check data is intact before "
1565 "making other changes.\n");
1566 return 1;
1567 }
1568
32754b7d 1569 if (s->raiddisks && s->raiddisks < array.raid_disks && array.level > 1 &&
24d40069
N
1570 get_linux_version() < 2006032 &&
1571 !check_env("MDADM_FORCE_FEWER")) {
e7b84f9d 1572 pr_err("reducing the number of devices is not safe before Linux 2.6.32\n"
24d40069
N
1573 " Please use a newer kernel\n");
1574 return 1;
1575 }
7bc71196
DW
1576
1577 st = super_by_fd(fd, &subarray);
1578 if (!st) {
e7b84f9d 1579 pr_err("Unable to determine metadata format for %s\n", devname);
7bc71196
DW
1580 return 1;
1581 }
32754b7d 1582 if (s->raiddisks > st->max_devs) {
e7b84f9d 1583 pr_err("Cannot increase raid-disks on this array"
acab7bb1
N
1584 " beyond %d\n", st->max_devs);
1585 return 1;
1586 }
7bc71196
DW
1587
1588 /* in the external case we need to check that the requested reshape is
1589 * supported, and perform an initial check that the container holds the
1590 * pre-requisite spare devices (mdmon owns final validation)
1591 */
1592 if (st->ss->external) {
1593 int container_dev;
7f2ba464 1594 int rv;
7bc71196
DW
1595
1596 if (subarray) {
1597 container_dev = st->container_dev;
1598 cfd = open_dev_excl(st->container_dev);
7bc71196
DW
1599 } else {
1600 container_dev = st->devnum;
1601 close(fd);
1602 cfd = open_dev_excl(st->devnum);
1603 fd = cfd;
1604 }
1605 if (cfd < 0) {
e7b84f9d 1606 pr_err("Unable to open container for %s\n",
7bc71196 1607 devname);
7f2ba464 1608 free(subarray);
7bc71196
DW
1609 return 1;
1610 }
1611
5da9ab98
N
1612 fmt_devname(container_buf, container_dev);
1613 container = container_buf;
7bc71196 1614
eb744788
AK
1615 rv = st->ss->load_container(st, cfd, NULL);
1616
7f2ba464 1617 if (rv) {
e7b84f9d 1618 pr_err("Cannot read superblock for %s\n",
7bc71196 1619 devname);
7f2ba464 1620 free(subarray);
7bc71196
DW
1621 return 1;
1622 }
1623
81219e70
LM
1624 /* check if operation is supported for metadata handler */
1625 if (st->ss->container_content) {
1626 struct mdinfo *cc = NULL;
1627 struct mdinfo *content = NULL;
1628
1629 cc = st->ss->container_content(st, subarray);
1630 for (content = cc; content ; content = content->next) {
446894ea 1631 int allow_reshape = 1;
81219e70
LM
1632
1633 /* check if reshape is allowed based on metadata
1634 * indications stored in content.array.status
1635 */
446894ea
N
1636 if (content->array.state & (1<<MD_SB_BLOCK_VOLUME))
1637 allow_reshape = 0;
1638 if (content->array.state
1639 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE))
1640 allow_reshape = 0;
81219e70 1641 if (!allow_reshape) {
e7b84f9d
N
1642 pr_err("cannot reshape arrays in"
1643 " container with unsupported"
1644 " metadata: %s(%s)\n",
1645 devname, container_buf);
81219e70
LM
1646 sysfs_free(cc);
1647 free(subarray);
1648 return 1;
1649 }
1650 }
1651 sysfs_free(cc);
1652 }
7bc71196
DW
1653 if (mdmon_running(container_dev))
1654 st->update_tail = &st->updates;
e2e53a2d 1655 }
691a36b7 1656
e2e53a2d
N
1657 added_disks = 0;
1658 for (dv = devlist; dv; dv = dv->next)
1659 added_disks++;
32754b7d
N
1660 if (s->raiddisks > array.raid_disks &&
1661 array.spare_disks +added_disks < (s->raiddisks - array.raid_disks) &&
1662 !c->force) {
e7b84f9d
N
1663 pr_err("Need %d spare%s to avoid degraded array,"
1664 " and only have %d.\n"
1665 " Use --force to over-ride this check.\n",
32754b7d
N
1666 s->raiddisks - array.raid_disks,
1667 s->raiddisks - array.raid_disks == 1 ? "" : "s",
e7b84f9d 1668 array.spare_disks + added_disks);
691a36b7 1669 return 1;
7bc71196
DW
1670 }
1671
3fa436ac
ML
1672 sra = sysfs_read(fd, 0, GET_LEVEL | GET_DISKS | GET_DEVS
1673 | GET_STATE | GET_VERSION);
24daa16f 1674 if (sra) {
7f2ba464 1675 if (st->ss->external && subarray == NULL) {
7bc71196
DW
1676 array.level = LEVEL_CONTAINER;
1677 sra->array.level = LEVEL_CONTAINER;
1678 }
7bc71196 1679 } else {
e7b84f9d 1680 pr_err("failed to read sysfs parameters for %s\n",
b526e52d
DW
1681 devname);
1682 return 1;
1683 }
7f2ba464
DW
1684 frozen = freeze(st);
1685 if (frozen < -1) {
1686 /* freeze() already spewed the reason */
8e61e0d7 1687 sysfs_free(sra);
7f2ba464
DW
1688 return 1;
1689 } else if (frozen < 0) {
e7b84f9d 1690 pr_err("%s is performing resync/recovery and cannot"
7236ee7a 1691 " be reshaped\n", devname);
8e61e0d7 1692 sysfs_free(sra);
7236ee7a
N
1693 return 1;
1694 }
1695
1696 /* ========= set size =============== */
887a7a9e 1697 if (s->size > 0 && (s->size == MAX_SIZE || s->size != (unsigned)array.size)) {
d04f65f4
N
1698 unsigned long long orig_size = get_component_size(fd)/2;
1699 unsigned long long min_csize;
d1537ed1 1700 struct mdinfo *mdi;
44f6f181 1701 int raid0_takeover = 0;
7bc71196 1702
85f10287 1703 if (orig_size == 0)
d04f65f4 1704 orig_size = (unsigned) array.size;
85f10287 1705
3920235e
N
1706 if (orig_size == 0) {
1707 pr_err("Cannot set device size in this type of array.\n");
1708 rv = 1;
1709 goto release;
1710 }
1711
32754b7d
N
1712 if (reshape_super(st, s->size, UnSet, UnSet, 0, 0, UnSet, NULL,
1713 devname, APPLY_METADATA_CHANGES, c->verbose > 0)) {
7bc71196
DW
1714 rv = 1;
1715 goto release;
1716 }
1717 sync_metadata(st);
7e7e9a4d
AK
1718 if (st->ss->external) {
1719 /* metadata can have size limitation
1720 * update size value according to metadata information
1721 */
1722 struct mdinfo *sizeinfo =
1723 st->ss->container_content(st, subarray);
1724 if (sizeinfo) {
1725 unsigned long long new_size =
1726 sizeinfo->custom_array_size/2;
1727 int data_disks = get_data_disks(
1728 sizeinfo->array.level,
1729 sizeinfo->array.layout,
1730 sizeinfo->array.raid_disks);
1731 new_size /= data_disks;
1732 dprintf("Metadata size correction from %llu to "
1733 "%llu (%llu)\n", orig_size, new_size,
1734 new_size * data_disks);
32754b7d 1735 s->size = new_size;
7e7e9a4d
AK
1736 sysfs_free(sizeinfo);
1737 }
1738 }
d1537ed1
N
1739
1740 /* Update the size of each member device in case
1741 * they have been resized. This will never reduce
1742 * below the current used-size. The "size" attribute
20a46756 1743 * understands '0' to mean 'max'.
d1537ed1 1744 */
20a46756 1745 min_csize = 0;
65a9798b 1746 rv = 0;
20a46756 1747 for (mdi = sra->devs; mdi; mdi = mdi->next) {
d04f65f4 1748 if (sysfs_set_num(sra, mdi, "size",
32754b7d 1749 s->size == MAX_SIZE ? 0 : s->size) < 0) {
b0a658ff
N
1750 /* Probably kernel refusing to let us
1751 * reduce the size - not an error.
1752 */
20a46756 1753 break;
65a9798b 1754 }
20a46756
N
1755 if (array.not_persistent == 0 &&
1756 array.major_version == 0 &&
1757 get_linux_version() < 3001000) {
1758 /* Dangerous to allow size to exceed 2TB */
1759 unsigned long long csize;
1760 if (sysfs_get_ll(sra, mdi, "size", &csize) == 0) {
1761 if (csize >= 2ULL*1024*1024*1024)
1762 csize = 2ULL*1024*1024*1024;
1763 if ((min_csize == 0 || (min_csize
d04f65f4 1764 > csize)))
20a46756
N
1765 min_csize = csize;
1766 }
1767 }
1768 }
65a9798b 1769 if (rv) {
e7b84f9d 1770 pr_err("Cannot set size on "
65a9798b
AK
1771 "array members.\n");
1772 goto size_change_error;
1773 }
32754b7d 1774 if (min_csize && s->size > min_csize) {
e7b84f9d 1775 pr_err("Cannot safely make this array "
20a46756
N
1776 "use more than 2TB per device on this kernel.\n");
1777 rv = 1;
65a9798b 1778 goto size_change_error;
20a46756 1779 }
32754b7d 1780 if (min_csize && s->size == MAX_SIZE) {
20a46756
N
1781 /* Don't let the kernel choose a size - it will get
1782 * it wrong
1783 */
e7b84f9d
N
1784 pr_err("Limited v0.90 array to "
1785 "2TB per device\n");
32754b7d 1786 s->size = min_csize;
20a46756 1787 }
44f6f181
AK
1788 if (st->ss->external) {
1789 if (sra->array.level == 0) {
1790 rv = sysfs_set_str(sra, NULL, "level",
1791 "raid5");
1792 if (!rv) {
1793 raid0_takeover = 1;
1794 /* get array parametes after takeover
1795 * to chane one parameter at time only
1796 */
1797 rv = ioctl(fd, GET_ARRAY_INFO, &array);
1798 }
1799 }
1800 /* make sure mdmon is
1801 * aware of the new level */
1802 if (!mdmon_running(st->container_dev))
1803 start_mdmon(st->container_dev);
1804 ping_monitor(container);
1805 if (mdmon_running(st->container_dev) &&
1806 st->update_tail == NULL)
1807 st->update_tail = &st->updates;
1808 }
1809
0a8b92a6
N
1810 if (s->size == MAX_SIZE)
1811 s->size = 0;
1812 array.size = s->size;
32754b7d 1813 if ((unsigned)array.size != s->size) {
7236ee7a
N
1814 /* got truncated to 32bit, write to
1815 * component_size instead
1816 */
1817 if (sra)
1818 rv = sysfs_set_num(sra, NULL,
32754b7d 1819 "component_size", s->size);
7236ee7a
N
1820 else
1821 rv = -1;
54397ed9 1822 } else {
7236ee7a 1823 rv = ioctl(fd, SET_ARRAY_INFO, &array);
44f6f181 1824
54397ed9
AK
1825 /* manage array size when it is managed externally
1826 */
1827 if ((rv == 0) && st->ss->external)
1828 rv = set_array_size(st, sra, sra->text_version);
1829 }
1830
44f6f181
AK
1831 if (raid0_takeover) {
1832 /* do not recync non-existing parity,
1833 * we will drop it anyway
1834 */
b51702b8 1835 sysfs_set_str(sra, NULL, "sync_action", "frozen");
44f6f181
AK
1836 /* go back to raid0, drop parity disk
1837 */
1838 sysfs_set_str(sra, NULL, "level", "raid0");
1839 ioctl(fd, GET_ARRAY_INFO, &array);
1840 }
1841
65a9798b 1842size_change_error:
7236ee7a 1843 if (rv != 0) {
39bbb392 1844 int err = errno;
7bc71196
DW
1845
1846 /* restore metadata */
1847 if (reshape_super(st, orig_size, UnSet, UnSet, 0, 0,
016e00f5
AK
1848 UnSet, NULL, devname,
1849 ROLLBACK_METADATA_CHANGES,
32754b7d 1850 c->verbose) == 0)
7bc71196 1851 sync_metadata(st);
e7b84f9d 1852 pr_err("Cannot set device size for %s: %s\n",
39bbb392 1853 devname, strerror(err));
24daa16f 1854 if (err == EBUSY &&
39bbb392 1855 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
e7b84f9d 1856 cont_err("Bitmap must be removed before size can be changed\n");
7236ee7a
N
1857 rv = 1;
1858 goto release;
1859 }
32754b7d 1860 if (s->assume_clean) {
86952387 1861 /* This will fail on kernels older than 3.0 unless
ce52f92f
N
1862 * a backport has been arranged.
1863 */
1864 if (sra == NULL ||
1865 sysfs_set_str(sra, NULL, "resync_start", "none") < 0)
86952387 1866 pr_err("--assume-clean not supported with --grow on this kernel\n");
ce52f92f 1867 }
7236ee7a 1868 ioctl(fd, GET_ARRAY_INFO, &array);
32754b7d
N
1869 s->size = get_component_size(fd)/2;
1870 if (s->size == 0)
1871 s->size = array.size;
1872 if (c->verbose >= 0) {
1873 if (s->size == orig_size)
e7b84f9d 1874 pr_err("component size of %s "
85f10287 1875 "unchanged at %lluK\n",
32754b7d 1876 devname, s->size);
85f10287 1877 else
e7b84f9d 1878 pr_err("component size of %s "
85f10287 1879 "has been set to %lluK\n",
32754b7d 1880 devname, s->size);
85f10287 1881 }
7236ee7a 1882 changed = 1;
7bc71196 1883 } else if (array.level != LEVEL_CONTAINER) {
32754b7d
N
1884 s->size = get_component_size(fd)/2;
1885 if (s->size == 0)
1886 s->size = array.size;
7236ee7a
N
1887 }
1888
d515d27f 1889 /* See if there is anything else to do */
32754b7d
N
1890 if ((s->level == UnSet || s->level == array.level) &&
1891 (s->layout_str == NULL) &&
1892 (s->chunk == 0 || s->chunk == array.chunk_size) &&
1893 (s->raiddisks == 0 || s->raiddisks == array.raid_disks)) {
d515d27f 1894 /* Nothing more to do */
32754b7d 1895 if (!changed && c->verbose >= 0)
e7b84f9d 1896 pr_err("%s: no change requested\n",
d515d27f
N
1897 devname);
1898 goto release;
1899 }
1900
dfe77a9e 1901 /* ========= check for Raid10/Raid1 -> Raid0 conversion ===============
5da9ab98 1902 * current implementation assumes that following conditions must be met:
dfe77a9e 1903 * - RAID10:
24daa16f
N
1904 * - far_copies == 1
1905 * - near_copies == 2
62a48395 1906 */
32754b7d 1907 if ((s->level == 0 && array.level == 10 && sra &&
24daa16f 1908 array.layout == ((1 << 8) + 2) && !(array.raid_disks & 1)) ||
32754b7d 1909 (s->level == 0 && array.level == 1 && sra)) {
62a48395 1910 int err;
dfe77a9e 1911 err = remove_disks_for_takeover(st, sra, array.layout);
62a48395
AK
1912 if (err) {
1913 dprintf(Name": Array cannot be reshaped\n");
62a48395
AK
1914 if (cfd > -1)
1915 close(cfd);
5da9ab98
N
1916 rv = 1;
1917 goto release;
7236ee7a 1918 }
fde139b9
N
1919 /* Make sure mdmon has seen the device removal
1920 * and updated metadata before we continue with
1921 * level change
1922 */
1923 if (container)
1924 ping_monitor(container);
7236ee7a
N
1925 }
1926
8ff6d094 1927 memset(&info, 0, sizeof(info));
5da9ab98 1928 info.array = array;
7443ee81 1929 sysfs_init(&info, fd, NoMdDev);
3fa436ac 1930 strcpy(info.text_version, sra->text_version);
32754b7d
N
1931 info.component_size = s->size*2;
1932 info.new_level = s->level;
1933 info.new_chunk = s->chunk * 1024;
eff4954d
N
1934 if (info.array.level == LEVEL_CONTAINER) {
1935 info.delta_disks = UnSet;
32754b7d
N
1936 info.array.raid_disks = s->raiddisks;
1937 } else if (s->raiddisks)
1938 info.delta_disks = s->raiddisks - info.array.raid_disks;
5da9ab98
N
1939 else
1940 info.delta_disks = UnSet;
32754b7d 1941 if (s->layout_str == NULL) {
5da9ab98
N
1942 info.new_layout = UnSet;
1943 if (info.array.level == 6 &&
1944 (info.new_level == 6 || info.new_level == UnSet) &&
1945 info.array.layout >= 16) {
e7b84f9d
N
1946 pr_err("%s has a non-standard layout. If you"
1947 " wish to preserve this\n", devname);
1948 cont_err("during the reshape, please specify"
1949 " --layout=preserve\n");
1950 cont_err("If you want to change it, specify a"
1951 " layout or use --layout=normalise\n");
7236ee7a
N
1952 rv = 1;
1953 goto release;
1954 }
32754b7d
N
1955 } else if (strcmp(s->layout_str, "normalise") == 0 ||
1956 strcmp(s->layout_str, "normalize") == 0) {
5da9ab98
N
1957 /* If we have a -6 RAID6 layout, remove the '-6'. */
1958 info.new_layout = UnSet;
1959 if (info.array.level == 6 && info.new_level == UnSet) {
1960 char l[40], *h;
1961 strcpy(l, map_num(r6layout, info.array.layout));
1962 h = strrchr(l, '-');
1963 if (h && strcmp(h, "-6") == 0) {
1964 *h = 0;
1965 info.new_layout = map_name(r6layout, l);
7236ee7a 1966 }
385167f3 1967 } else {
e7b84f9d 1968 pr_err("%s is only meaningful when reshaping"
32754b7d 1969 " a RAID6 array.\n", s->layout_str);
385167f3
N
1970 rv = 1;
1971 goto release;
7236ee7a 1972 }
32754b7d 1973 } else if (strcmp(s->layout_str, "preserve") == 0) {
385167f3
N
1974 /* This means that a non-standard RAID6 layout
1975 * is OK.
1976 * In particular:
1977 * - When reshape a RAID6 (e.g. adding a device)
1978 * which is in a non-standard layout, it is OK
1979 * to preserve that layout.
1980 * - When converting a RAID5 to RAID6, leave it in
1981 * the XXX-6 layout, don't re-layout.
1982 */
1983 if (info.array.level == 6 && info.new_level == UnSet)
1984 info.new_layout = info.array.layout;
1985 else if (info.array.level == 5 && info.new_level == 6) {
1986 char l[40];
1987 strcpy(l, map_num(r5layout, info.array.layout));
1988 strcat(l, "-6");
1989 info.new_layout = map_name(r6layout, l);
1990 } else {
e7b84f9d 1991 pr_err("%s in only meaningful when reshaping"
32754b7d 1992 " to RAID6\n", s->layout_str);
385167f3
N
1993 rv = 1;
1994 goto release;
1995 }
5da9ab98
N
1996 } else {
1997 int l = info.new_level;
1998 if (l == UnSet)
1999 l = info.array.level;
2000 switch (l) {
2001 case 5:
32754b7d 2002 info.new_layout = map_name(r5layout, s->layout_str);
5da9ab98
N
2003 break;
2004 case 6:
32754b7d 2005 info.new_layout = map_name(r6layout, s->layout_str);
5da9ab98
N
2006 break;
2007 case 10:
32754b7d 2008 info.new_layout = parse_layout_10(s->layout_str);
5da9ab98
N
2009 break;
2010 case LEVEL_FAULTY:
32754b7d 2011 info.new_layout = parse_layout_faulty(s->layout_str);
5da9ab98
N
2012 break;
2013 default:
e7b84f9d 2014 pr_err("layout not meaningful"
5da9ab98 2015 " with this level\n");
7bc71196
DW
2016 rv = 1;
2017 goto release;
2018 }
5da9ab98 2019 if (info.new_layout == UnSet) {
e7b84f9d 2020 pr_err("layout %s not understood"
5da9ab98 2021 " for this level\n",
32754b7d 2022 s->layout_str);
5da9ab98
N
2023 rv = 1;
2024 goto release;
7bc71196 2025 }
7236ee7a
N
2026 }
2027
907ea753 2028 if (array.level == LEVEL_FAULTY) {
32754b7d 2029 if (s->level != UnSet && s->level != array.level) {
e7b84f9d 2030 pr_err("cannot change level of Faulty device\n");
907ea753
N
2031 rv =1 ;
2032 }
32754b7d 2033 if (s->chunk) {
e7b84f9d 2034 pr_err("cannot set chunksize of Faulty device\n");
907ea753
N
2035 rv =1 ;
2036 }
32754b7d 2037 if (s->raiddisks && s->raiddisks != 1) {
e7b84f9d 2038 pr_err("cannot set raid_disks of Faulty device\n");
907ea753
N
2039 rv =1 ;
2040 }
32754b7d 2041 if (s->layout_str) {
907ea753
N
2042 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
2043 dprintf("Cannot get array information.\n");
2044 goto release;
2045 }
2046 array.layout = info.new_layout;
2047 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
e7b84f9d 2048 pr_err("failed to set new layout\n");
907ea753 2049 rv = 1;
32754b7d 2050 } else if (c->verbose >= 0)
907ea753
N
2051 printf("layout for %s set to %d\n",
2052 devname, array.layout);
2053 }
2054 } else if (array.level == LEVEL_CONTAINER) {
5da9ab98
N
2055 /* This change is to be applied to every array in the
2056 * container. This is only needed when the metadata imposes
2057 * restraints of the various arrays in the container.
2058 * Currently we only know that IMSM requires all arrays
2059 * to have the same number of devices so changing the
2060 * number of devices (On-Line Capacity Expansion) must be
2061 * performed at the level of the container
2062 */
9ad6f6e6 2063 rv = reshape_container(container, devname, -1, st, &info,
32754b7d 2064 c->force, c->backup_file, c->verbose, 0, 0);
9202b8eb 2065 frozen = 0;
5da9ab98 2066 } else {
08f9e34b
AK
2067 /* get spare devices from external metadata
2068 */
2069 if (st->ss->external) {
2070 struct mdinfo *info2;
2071
2072 info2 = st->ss->container_content(st, subarray);
2073 if (info2) {
2074 info.array.spare_disks =
2075 info2->array.spare_disks;
2076 sysfs_free(info2);
2077 }
2078 }
2079
5da9ab98
N
2080 /* Impose these changes on a single array. First
2081 * check that the metadata is OK with the change. */
7bc71196 2082
7ec0996c 2083 if (reshape_super(st, 0, info.new_level,
5da9ab98 2084 info.new_layout, info.new_chunk,
41784c88 2085 info.array.raid_disks, info.delta_disks,
32754b7d
N
2086 c->backup_file, devname, APPLY_METADATA_CHANGES,
2087 c->verbose)) {
7bc71196
DW
2088 rv = 1;
2089 goto release;
2090 }
5da9ab98 2091 sync_metadata(st);
32754b7d 2092 rv = reshape_array(container, fd, devname, st, &info, c->force,
19ceb16d
N
2093 devlist, data_offset, c->backup_file, c->verbose,
2094 0, 0, 0);
9202b8eb 2095 frozen = 0;
5da9ab98
N
2096 }
2097release:
8e61e0d7 2098 sysfs_free(sra);
9202b8eb
N
2099 if (frozen > 0)
2100 unfreeze(st);
5da9ab98
N
2101 return rv;
2102}
7bc71196 2103
f93346ef
AK
2104/* verify_reshape_position()
2105 * Function checks if reshape position in metadata is not farther
2106 * than position in md.
2107 * Return value:
2108 * 0 : not valid sysfs entry
2109 * it can be caused by not started reshape, it should be started
2110 * by reshape array or raid0 array is before takeover
2111 * -1 : error, reshape position is obviously wrong
2112 * 1 : success, reshape progress correct or updated
2113*/
2114static int verify_reshape_position(struct mdinfo *info, int level)
2115{
2116 int ret_val = 0;
2117 char buf[40];
178950ea 2118 int rv;
f93346ef
AK
2119
2120 /* read sync_max, failure can mean raid0 array */
178950ea
AK
2121 rv = sysfs_get_str(info, NULL, "sync_max", buf, 40);
2122
2123 if (rv > 0) {
f93346ef
AK
2124 char *ep;
2125 unsigned long long position = strtoull(buf, &ep, 0);
2126
2127 dprintf(Name": Read sync_max sysfs entry is: %s\n", buf);
2128 if (!(ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))) {
2129 position *= get_data_disks(level,
2130 info->new_layout,
2131 info->array.raid_disks);
2132 if (info->reshape_progress < position) {
2133 dprintf("Corrected reshape progress (%llu) to "
2134 "md position (%llu)\n",
2135 info->reshape_progress, position);
2136 info->reshape_progress = position;
2137 ret_val = 1;
2138 } else if (info->reshape_progress > position) {
e7b84f9d
N
2139 pr_err("Fatal error: array "
2140 "reshape was not properly frozen "
2141 "(expected reshape position is %llu, "
2142 "but reshape progress is %llu.\n",
2143 position, info->reshape_progress);
f93346ef
AK
2144 ret_val = -1;
2145 } else {
2146 dprintf("Reshape position in md and metadata "
2147 "are the same;");
2148 ret_val = 1;
2149 }
2150 }
178950ea
AK
2151 } else if (rv == 0) {
2152 /* for valid sysfs entry, 0-length content
2153 * should be indicated as error
2154 */
2155 ret_val = -1;
f93346ef
AK
2156 }
2157
2158 return ret_val;
2159}
2160
19ceb16d
N
2161static int raid10_reshape(char *container, int fd, char *devname,
2162 struct supertype *st, struct mdinfo *info,
2163 struct reshape *reshape,
2164 unsigned long long data_offset,
2165 int force, int verbose)
2166{
2167 /* Changing raid_disks, layout, chunksize or possibly
2168 * just data_offset for a RAID10.
ee2429e0
N
2169 * We must always change data_offset. We change by at least
2170 * ->backup_blocks which is the largest of the old and new
2171 * chunk sizes.
19ceb16d
N
2172 * If raid_disks is increasing, then data_offset must decrease
2173 * by at least this copy size.
2174 * If raid_disks is unchanged, data_offset must increase or
ee2429e0 2175 * decrease by at least backup_blocks but preferably by much more.
19ceb16d
N
2176 * We choose half of the available space.
2177 * If raid_disks is decreasing, data_offset must increase by
ee2429e0
N
2178 * at least backup_blocks. To allow of this, component_size
2179 * must be decreased by the same amount.
19ceb16d 2180 *
ee2429e0
N
2181 * So we calculate the required minimum and direction, possibly
2182 * reduce the component_size, then iterate through the devices
2183 * and set the new_data_offset.
19ceb16d
N
2184 * If that all works, we set chunk_size, layout, raid_disks, and start
2185 * 'reshape'
2186 */
2187 struct mdinfo *sra, *sd;
2188 unsigned long long min;
2189 int dir = 0;
2190 int err = 0;
2191
2192 sra = sysfs_read(fd, 0,
2193 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK
2194 );
2195 if (!sra) {
2196 fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n",
2197 devname);
2198 goto release;
2199 }
2200 min = reshape->backup_blocks;
2201
2202 if (info->delta_disks)
2203 sysfs_set_str(sra, NULL, "reshape_direction",
2204 info->delta_disks < 0 ? "backwards" : "forwards");
ee2429e0
N
2205 if (info->delta_disks < 0 &&
2206 info->space_after < reshape->backup_blocks) {
2207 int rv = sysfs_set_num(sra, NULL, "component_size",
2208 (sra->component_size -
2209 reshape->backup_blocks)/2);
2210 if (rv) {
2211 fprintf(stderr, Name ": cannot reduce component size\n");
2212 goto release;
2213 }
2214 }
19ceb16d
N
2215 for (sd = sra->devs; sd; sd = sd->next) {
2216 char *dn;
2217 int dfd;
2218 int rv;
2219 struct supertype *st2;
2220 struct mdinfo info2;
2221
2222 if (sd->disk.state & (1<<MD_DISK_FAULTY))
2223 continue;
2224 dn = map_dev(sd->disk.major, sd->disk.minor, 0);
2225 dfd = dev_open(dn, O_RDONLY);
2226 if (dfd < 0) {
2227 fprintf(stderr,
2228 Name ": %s: cannot open component %s\n",
2229 devname, dn ? dn : "-unknown-");
2230 rv = -1;
2231 goto release;
2232 }
2233 st2 = dup_super(st);
2234 rv = st2->ss->load_super(st2,dfd, NULL);
2235 close(dfd);
2236 if (rv) {
2237 free(st2);
2238 fprintf(stderr, ": %s: cannot get superblock from %s\n",
2239 devname, dn);
2240 goto release;
2241 }
2242 st2->ss->getinfo_super(st2, &info2, NULL);
2243 st2->ss->free_super(st2);
2244 free(st2);
2245 if (info->delta_disks < 0) {
2246 /* Don't need any space as array is shrinking
2247 * just move data_offset up by min
2248 */
2249 if (data_offset == 1)
2250 info2.new_data_offset = info2.data_offset + min;
2251 else {
2252 if ((unsigned long long)data_offset
2253 < info2.data_offset + min) {
2254 fprintf(stderr, Name ": --data-offset too small for %s\n",
2255 dn);
2256 goto release;
2257 }
2258 info2.new_data_offset = data_offset;
2259 }
2260 } else if (info->delta_disks > 0) {
2261 /* need space before */
2262 if (info2.space_before < min) {
2263 fprintf(stderr, Name ": Insufficient head-space for reshape on %s\n",
2264 dn);
2265 goto release;
2266 }
2267 if (data_offset == 1)
2268 info2.new_data_offset = info2.data_offset - min;
2269 else {
2270 if ((unsigned long long)data_offset
2271 > info2.data_offset - min) {
2272 fprintf(stderr, Name ": --data-offset too large for %s\n",
2273 dn);
2274 goto release;
2275 }
2276 info2.new_data_offset = data_offset;
2277 }
2278 } else {
2279 if (dir == 0) {
2280 /* can move up or down. 'data_offset'
2281 * might guide us, otherwise choose
2282 * direction with most space
2283 */
2284 if (data_offset == 1) {
2285 if (info2.space_before > info2.space_after)
2286 dir = -1;
2287 else
2288 dir = 1;
2289 } else if (data_offset < info2.data_offset)
2290 dir = -1;
2291 else
2292 dir = 1;
2293 sysfs_set_str(sra, NULL, "reshape_direction",
2294 dir == 1 ? "backwards" : "forwards");
2295 }
2296 switch (dir) {
2297 case 1: /* Increase data offset */
2298 if (info2.space_after < min) {
2299 fprintf(stderr, Name ": Insufficient tail-space for reshape on %s\n",
2300 dn);
2301 goto release;
2302 }
2303 if (data_offset != 1 &&
2304 data_offset < info2.data_offset + min) {
2305 fprintf(stderr, Name ": --data-offset too small on %s\n",
2306 dn);
2307 goto release;
2308 }
2309 if (data_offset != 1)
2310 info2.new_data_offset = data_offset;
2311 else {
2312 unsigned long long off =
2313 info2.space_after / 2;
2314 off &= ~7ULL;
2315 if (off < min)
2316 off = min;
2317 info2.new_data_offset =
2318 info2.data_offset + off;
2319 }
2320 break;
2321 case -1: /* Decrease data offset */
2322 if (info2.space_before < min) {
2323 fprintf(stderr, Name ": insufficient head-room on %s\n",
2324 dn);
2325 goto release;
2326 }
2327 if (data_offset != 1 &&
2328 data_offset < info2.data_offset - min) {
2329 fprintf(stderr, Name ": --data-offset too small on %s\n",
2330 dn);
2331 goto release;
2332 }
2333 if (data_offset != 1)
2334 info2.new_data_offset = data_offset;
2335 else {
2336 unsigned long long off =
2337 info2.space_before / 2;
2338 off &= ~7ULL;
2339 if (off < min)
2340 off = min;
2341 info2.new_data_offset =
2342 info2.data_offset - off;
2343 }
2344 break;
2345 }
2346 }
2347 if (sysfs_set_num(sra, sd, "new_offset",
2348 info2.new_data_offset) < 0) {
2349 err = errno;
2350 fprintf(stderr, Name ": Cannot set new_offset for %s\n",
2351 dn);
2352 break;
2353 }
2354 }
ee2429e0 2355 if (!err && sysfs_set_num(sra, NULL, "chunk_size", info->new_chunk) < 0)
19ceb16d
N
2356 err = errno;
2357 if (!err && sysfs_set_num(sra, NULL, "layout", reshape->after.layout) < 0)
2358 err = errno;
2359 if (!err && sysfs_set_num(sra, NULL, "raid_disks",
2360 info->array.raid_disks + info->delta_disks) < 0)
2361 err = errno;
2362 if (!err && sysfs_set_str(sra, NULL, "sync_action", "reshape") < 0)
2363 err = errno;
2364 if (err) {
2365 fprintf(stderr, Name ": Cannot set array shape for %s\n",
2366 devname);
2367 if (err == EBUSY &&
2368 (info->array.state & (1<<MD_SB_BITMAP_PRESENT)))
2369 fprintf(stderr,
2370 " Bitmap must be removed before"
2371 " shape can be changed\n");
2372 goto release;
2373 }
2374 sysfs_free(sra);
2375 return 0;
2376release:
2377 sysfs_free(sra);
2378 return 1;
2379}
2380
ee2429e0
N
2381static void get_space_after(int fd, struct supertype *st, struct mdinfo *info)
2382{
2383 struct mdinfo *sra, *sd;
f3f09a52
LD
2384 /* Initialisation to silence compiler warning */
2385 unsigned long long min_space_before = 0, min_space_after = 0;
ee2429e0
N
2386 int first = 1;
2387
2388 sra = sysfs_read(fd, 0, GET_DEVS);
2389 if (!sra)
2390 return;
2391 for (sd = sra->devs; sd; sd = sd->next) {
2392 char *dn;
2393 int dfd;
2394 struct supertype *st2;
2395 struct mdinfo info2;
2396
2397 if (sd->disk.state & (1<<MD_DISK_FAULTY))
2398 continue;
2399 dn = map_dev(sd->disk.major, sd->disk.minor, 0);
2400 dfd = dev_open(dn, O_RDONLY);
2401 if (dfd < 0)
2402 break;
2403 st2 = dup_super(st);
2404 if (st2->ss->load_super(st2,dfd, NULL)) {
2405 close(dfd);
2406 free(st2);
2407 break;
2408 }
2409 close(dfd);
2410 st2->ss->getinfo_super(st2, &info2, NULL);
2411 st2->ss->free_super(st2);
2412 free(st2);
2413 if (first ||
2414 min_space_before > info2.space_before)
2415 min_space_before = info2.space_before;
2416 if (first ||
2417 min_space_after > info2.space_after)
2418 min_space_after = info2.space_after;
2419 first = 0;
2420 }
2421 if (sd == NULL && !first) {
2422 info->space_after = min_space_after;
2423 info->space_before = min_space_before;
2424 }
2425 sysfs_free(sra);
2426}
2427
5da9ab98
N
2428static int reshape_array(char *container, int fd, char *devname,
2429 struct supertype *st, struct mdinfo *info,
e2e53a2d 2430 int force, struct mddev_dev *devlist,
19ceb16d 2431 unsigned long long data_offset,
ba728be7 2432 char *backup_file, int verbose, int forked,
b76b30e0 2433 int restart, int freeze_reshape)
5da9ab98
N
2434{
2435 struct reshape reshape;
2436 int spares_needed;
2437 char *msg;
2438 int orig_level = UnSet;
2439 int disks, odisks;
1f9b0e28 2440 int delayed;
7bc71196 2441
5da9ab98
N
2442 struct mdu_array_info_s array;
2443 char *c;
e86c9dd6 2444
e2e53a2d
N
2445 struct mddev_dev *dv;
2446 int added_disks;
2447
d152f53e
JS
2448 int *fdlist = NULL;
2449 unsigned long long *offsets = NULL;
5da9ab98
N
2450 int d;
2451 int nrdisks;
2452 int err;
da8100ca 2453 unsigned long blocks;
5da9ab98
N
2454 unsigned long cache;
2455 unsigned long long array_size;
2456 int done;
cf6ac177 2457 struct mdinfo *sra = NULL;
7bc71196 2458
9468aeac
N
2459 /* when reshaping a RAID0, the component_size might be zero.
2460 * So try to fix that up.
2461 */
2462 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
2463 dprintf("Cannot get array information.\n");
2464 goto release;
2465 }
2466 if (array.level == 0 && info->component_size == 0) {
2467 get_dev_size(fd, NULL, &array_size);
2468 info->component_size = array_size / array.raid_disks;
2469 }
2470
ee2429e0
N
2471 if (array.level == 10)
2472 /* Need space_after info */
2473 get_space_after(fd, st, info);
2474
3cb2aed2
N
2475 if (info->reshape_active) {
2476 int new_level = info->new_level;
2477 info->new_level = UnSet;
ce4783d3
N
2478 if (info->delta_disks > 0)
2479 info->array.raid_disks -= info->delta_disks;
3cb2aed2
N
2480 msg = analyse_change(info, &reshape);
2481 info->new_level = new_level;
ce4783d3
N
2482 if (info->delta_disks > 0)
2483 info->array.raid_disks += info->delta_disks;
fcdfb814
AK
2484 if (!restart)
2485 /* Make sure the array isn't read-only */
2486 ioctl(fd, RESTART_ARRAY_RW, 0);
3cb2aed2
N
2487 } else
2488 msg = analyse_change(info, &reshape);
5da9ab98 2489 if (msg) {
e7b84f9d 2490 pr_err("%s\n", msg);
631d7405 2491 goto release;
5da9ab98 2492 }
817ed7d6
N
2493 if (restart &&
2494 (reshape.level != info->array.level ||
2495 reshape.before.layout != info->array.layout ||
384e9be1 2496 reshape.before.data_disks + reshape.parity
ce4783d3 2497 != info->array.raid_disks - max(0, info->delta_disks))) {
e7b84f9d 2498 pr_err("reshape info is not in native format -"
20a40eca
N
2499 " cannot continue.\n");
2500 goto release;
2501 }
a93f87ee 2502
e1dd332a
AK
2503 if (st->ss->external && restart && (info->reshape_progress == 0)) {
2504 /* When reshape is restarted from '0', very begin of array
2505 * it is possible that for external metadata reshape and array
2506 * configuration doesn't happen.
2507 * Check if md has the same opinion, and reshape is restarted
2508 * from 0. If so, this is regular reshape start after reshape
2509 * switch in metadata to next array only.
2510 */
2511 if ((verify_reshape_position(info, reshape.level) >= 0) &&
2512 (info->reshape_progress == 0))
2513 restart = 0;
2514 }
a93f87ee
N
2515 if (restart) {
2516 /* reshape already started. just skip to monitoring the reshape */
2517 if (reshape.backup_blocks == 0)
2518 return 0;
2519 goto started;
2520 }
a9c3e78f
AK
2521 /* The container is frozen but the array may not be.
2522 * So freeze the array so spares don't get put to the wrong use
2523 * FIXME there should probably be a cleaner separation between
2524 * freeze_array and freeze_container.
2525 */
2526 sysfs_freeze_array(info);
e06c4e59 2527 /* Check we have enough spares to not be degraded */
e2e53a2d
N
2528 added_disks = 0;
2529 for (dv = devlist; dv ; dv=dv->next)
2530 added_disks++;
5da9ab98
N
2531 spares_needed = max(reshape.before.data_disks,
2532 reshape.after.data_disks)
2533 + reshape.parity - array.raid_disks;
7bc71196 2534
88c1a083 2535 if (!force &&
e2e53a2d
N
2536 info->new_level > 1 && info->array.level > 1 &&
2537 spares_needed > info->array.spare_disks + added_disks) {
e7b84f9d
N
2538 pr_err("Need %d spare%s to avoid degraded array,"
2539 " and only have %d.\n"
2540 " Use --force to over-ride this check.\n",
2541 spares_needed,
2542 spares_needed == 1 ? "" : "s",
2543 info->array.spare_disks + added_disks);
631d7405 2544 goto release;
7bc71196 2545 }
e06c4e59
N
2546 /* Check we have enough spares to not fail */
2547 spares_needed = max(reshape.before.data_disks,
2548 reshape.after.data_disks)
2549 - array.raid_disks;
2550 if ((info->new_level > 1 || info->new_level == 0) &&
e2e53a2d 2551 spares_needed > info->array.spare_disks +added_disks) {
e7b84f9d
N
2552 pr_err("Need %d spare%s to create working array,"
2553 " and only have %d.\n",
2554 spares_needed,
2555 spares_needed == 1 ? "" : "s",
2556 info->array.spare_disks + added_disks);
e06c4e59
N
2557 goto release;
2558 }
e86c9dd6 2559
eae35f5c 2560 if (reshape.level != array.level) {
5da9ab98
N
2561 char *c = map_num(pers, reshape.level);
2562 int err;
2563 if (c == NULL)
631d7405 2564 goto release;
e86c9dd6 2565
5da9ab98
N
2566 err = sysfs_set_str(info, NULL, "level", c);
2567 if (err) {
2568 err = errno;
e7b84f9d 2569 pr_err("%s: could not set level to %s\n",
5da9ab98 2570 devname, c);
24daa16f 2571 if (err == EBUSY &&
5da9ab98 2572 (info->array.state & (1<<MD_SB_BITMAP_PRESENT)))
e7b84f9d
N
2573 cont_err("Bitmap must be removed"
2574 " before level can be changed\n");
631d7405 2575 goto release;
19678e53 2576 }
ba728be7 2577 if (verbose >= 0)
e7b84f9d 2578 pr_err("level of %s changed to %s\n",
24daa16f 2579 devname, c);
eae35f5c 2580 orig_level = array.level;
3cd4e7c4 2581 sysfs_freeze_array(info);
e86c9dd6 2582
16d4d84e
AK
2583 if (reshape.level > 0 && st->ss->external) {
2584 /* make sure mdmon is aware of the new level */
78340e26
AK
2585 if (mdmon_running(st->container_dev))
2586 flush_mdmon(container);
2587
16d4d84e
AK
2588 if (!mdmon_running(st->container_dev))
2589 start_mdmon(st->container_dev);
2590 ping_monitor(container);
e919fb0a
AK
2591 if (mdmon_running(st->container_dev) &&
2592 st->update_tail == NULL)
2593 st->update_tail = &st->updates;
16d4d84e 2594 }
5da9ab98 2595 }
5da9ab98
N
2596 /* ->reshape_super might have chosen some spares from the
2597 * container that it wants to be part of the new array.
2598 * We can collect them with ->container_content and give
2599 * them to the kernel.
2600 */
2601 if (st->ss->reshape_super && st->ss->container_content) {
2602 char *subarray = strchr(info->text_version+1, '/')+1;
2603 struct mdinfo *info2 =
2604 st->ss->container_content(st, subarray);
2605 struct mdinfo *d;
2606
2dd0d697
AK
2607 if (info2) {
2608 sysfs_init(info2, fd, st->devnum);
0d5ac3c6
N
2609 /* When increasing number of devices, we need to set
2610 * new raid_disks before adding these, or they might
2611 * be rejected.
2612 */
2613 if (reshape.backup_blocks &&
2614 reshape.after.data_disks > reshape.before.data_disks)
2615 subarray_set_num(container, info2, "raid_disks",
2616 reshape.after.data_disks +
2617 reshape.parity);
5da9ab98
N
2618 for (d = info2->devs; d; d = d->next) {
2619 if (d->disk.state == 0 &&
2620 d->disk.raid_disk >= 0) {
2621 /* This is a spare that wants to
2622 * be part of the array.
2623 */
2624 add_disk(fd, st, info2, d);
2625 }
9860f271 2626 }
2dd0d697
AK
2627 sysfs_free(info2);
2628 }
5da9ab98 2629 }
e2e53a2d
N
2630 /* We might have been given some devices to add to the
2631 * array. Now that the array has been changed to the right
2632 * level and frozen, we can safely add them.
2633 */
2634 if (devlist)
ba728be7 2635 Manage_subdevs(devname, fd, devlist, verbose,
11b391ec 2636 0,NULL, 0);
1686dc25 2637
b4f8e38b 2638 if (reshape.backup_blocks == 0) {
5da9ab98
N
2639 /* No restriping needed, but we might need to impose
2640 * some more changes: layout, raid_disks, chunk_size
e86c9dd6 2641 */
24aebf3a
KW
2642 /* read current array info */
2643 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
83732c28 2644 dprintf("Cannot get array information.\n");
24aebf3a
KW
2645 goto release;
2646 }
2647 /* compare current array info with new values and if
2648 * it is different update them to new */
5da9ab98 2649 if (info->new_layout != UnSet &&
24aebf3a
KW
2650 info->new_layout != array.layout) {
2651 array.layout = info->new_layout;
2652 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
e7b84f9d 2653 pr_err("failed to set new layout\n");
631d7405 2654 goto release;
ba728be7 2655 } else if (verbose >= 0)
5da9ab98 2656 printf("layout for %s set to %d\n",
24aebf3a 2657 devname, array.layout);
5da9ab98
N
2658 }
2659 if (info->delta_disks != UnSet &&
24aebf3a
KW
2660 info->delta_disks != 0 &&
2661 array.raid_disks != (info->array.raid_disks + info->delta_disks)) {
2662 array.raid_disks += info->delta_disks;
2663 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
e7b84f9d 2664 pr_err("failed to set raid disks\n");
631d7405 2665 goto release;
ba728be7 2666 } else if (verbose >= 0) {
5da9ab98 2667 printf("raid_disks for %s set to %d\n",
24aebf3a
KW
2668 devname, array.raid_disks);
2669 }
5da9ab98
N
2670 }
2671 if (info->new_chunk != 0 &&
24aebf3a 2672 info->new_chunk != array.chunk_size) {
5da9ab98
N
2673 if (sysfs_set_num(info, NULL,
2674 "chunk_size", info->new_chunk) != 0) {
e7b84f9d 2675 pr_err("failed to set chunk size\n");
631d7405 2676 goto release;
ba728be7 2677 } else if (verbose >= 0)
5da9ab98 2678 printf("chunk size for %s set to %d\n",
24aebf3a 2679 devname, array.chunk_size);
4725bc31 2680 }
e35b189b 2681 unfreeze(st);
631d7405 2682 return 0;
5da9ab98 2683 }
19678e53 2684
5da9ab98
N
2685 /*
2686 * There are three possibilities.
2687 * 1/ The array will shrink.
2688 * We need to ensure the reshape will pause before reaching
2689 * the 'critical section'. We also need to fork and wait for
24daa16f 2690 * that to happen. When it does we
5da9ab98
N
2691 * suspend/backup/complete/unfreeze
2692 *
2693 * 2/ The array will not change size.
2694 * This requires that we keep a backup of a sliding window
2695 * so that we can restore data after a crash. So we need
2696 * to fork and monitor progress.
2697 * In future we will allow the data_offset to change, so
2698 * a sliding backup becomes unnecessary.
2699 *
2700 * 3/ The array will grow. This is relatively easy.
2701 * However the kernel's restripe routines will cheerfully
2702 * overwrite some early data before it is safe. So we
2703 * need to make a backup of the early parts of the array
2704 * and be ready to restore it if rebuild aborts very early.
2705 * For externally managed metadata, we still need a forked
2706 * child to monitor the reshape and suspend IO over the region
2707 * that is being reshaped.
2708 *
2709 * We backup data by writing it to one spare, or to a
2710 * file which was given on command line.
2711 *
2712 * In each case, we first make sure that storage is available
2713 * for the required backup.
2714 * Then we:
2715 * - request the shape change.
2716 * - fork to handle backup etc.
2717 */
5da9ab98
N
2718 /* Check that we can hold all the data */
2719 get_dev_size(fd, NULL, &array_size);
2720 if (reshape.new_size < (array_size/512)) {
e7b84f9d
N
2721 pr_err("this change will reduce the size of the array.\n"
2722 " use --grow --array-size first to truncate array.\n"
2723 " e.g. mdadm --grow %s --array-size %llu\n",
2724 devname, reshape.new_size/2);
5da9ab98
N
2725 goto release;
2726 }
7236ee7a 2727
19ceb16d
N
2728started:
2729
2730 if (array.level == 10) {
2731 /* Reshaping RAID10 does not require and data backup by
2732 * user-space. Instead it requires that the data_offset
2733 * is changed to avoid the need for backup.
2734 * So this is handled very separately
2735 */
2736 if (restart)
2737 /* Nothing to do. */
2738 return 0;
2739 return raid10_reshape(container, fd, devname, st, info,
2740 &reshape, data_offset,
2741 force, verbose);
2742 }
5da9ab98 2743 sra = sysfs_read(fd, 0,
3656edcd 2744 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK|
5da9ab98 2745 GET_CACHE);
5da9ab98 2746 if (!sra) {
e7b84f9d 2747 pr_err("%s: Cannot get array details from sysfs\n",
5da9ab98 2748 devname);
5da9ab98
N
2749 goto release;
2750 }
7236ee7a 2751
5da9ab98
N
2752 /* Decide how many blocks (sectors) for a reshape
2753 * unit. The number we have so far is just a minimum
2754 */
b4f8e38b 2755 blocks = reshape.backup_blocks;
24daa16f 2756 if (reshape.before.data_disks ==
5da9ab98
N
2757 reshape.after.data_disks) {
2758 /* Make 'blocks' bigger for better throughput, but
2759 * not so big that we reject it below.
2760 * Try for 16 megabytes
2761 */
2762 while (blocks * 32 < sra->component_size &&
2763 blocks < 16*1024*2)
2764 blocks *= 2;
2765 } else
e7b84f9d 2766 pr_err("Need to backup %luK of critical "
5da9ab98
N
2767 "section..\n", blocks/2);
2768
2769 if (blocks >= sra->component_size/2) {
e7b84f9d 2770 pr_err("%s: Something wrong"
5da9ab98
N
2771 " - reshape aborted\n",
2772 devname);
5da9ab98
N
2773 goto release;
2774 }
7236ee7a 2775
5da9ab98
N
2776 /* Now we need to open all these devices so we can read/write.
2777 */
b8b286a6
N
2778 nrdisks = max(reshape.before.data_disks,
2779 reshape.after.data_disks) + reshape.parity
2780 + sra->array.spare_disks;
503975b9
N
2781 fdlist = xcalloc((1+nrdisks), sizeof(int));
2782 offsets = xcalloc((1+nrdisks), sizeof(offsets[0]));
e380d3be 2783
b8b286a6
N
2784 odisks = reshape.before.data_disks + reshape.parity;
2785 d = reshape_prepare_fdlist(devname, sra, odisks,
5da9ab98
N
2786 nrdisks, blocks, backup_file,
2787 fdlist, offsets);
2788 if (d < 0) {
5da9ab98
N
2789 goto release;
2790 }
13c37ad3
AK
2791 if ((st->ss->manage_reshape == NULL) ||
2792 (st->ss->recover_backup == NULL)) {
2793 if (backup_file == NULL) {
2794 if (reshape.after.data_disks <=
2795 reshape.before.data_disks) {
e7b84f9d 2796 pr_err("%s: Cannot grow - "
13c37ad3
AK
2797 "need backup-file\n", devname);
2798 goto release;
2799 } else if (sra->array.spare_disks == 0) {
e7b84f9d 2800 pr_err("%s: Cannot grow - "
13c37ad3
AK
2801 "need a spare or backup-file to backup "
2802 "critical section\n", devname);
2803 goto release;
2804 }
2805 } else {
2806 if (!reshape_open_backup_file(backup_file, fd, devname,
2807 (signed)blocks,
2808 fdlist+d, offsets+d,
2809 restart)) {
2810 goto release;
2811 }
2812 d++;
e86c9dd6 2813 }
5da9ab98 2814 }
7236ee7a 2815
5da9ab98
N
2816 /* lastly, check that the internal stripe cache is
2817 * large enough, or it won't work.
2818 * It must hold at least 4 stripes of the larger
2819 * chunk size
2820 */
2821 cache = max(info->array.chunk_size, info->new_chunk);
2822 cache *= 4; /* 4 stripes minimum */
2823 cache /= 512; /* convert to sectors */
2824 disks = min(reshape.before.data_disks, reshape.after.data_disks);
2825 /* make sure there is room for 'blocks' with a bit to spare */
2826 if (cache < 16 + blocks / disks)
2827 cache = 16 + blocks / disks;
2828 cache /= (4096/512); /* Covert from sectors to pages */
2829
2830 if (sra->cache_size < cache)
2831 subarray_set_num(container, sra, "stripe_cache_size",
2832 cache+1);
2833
2834 /* Right, everything seems fine. Let's kick things off.
2835 * If only changing raid_disks, use ioctl, else use
2836 * sysfs.
2837 */
2838 sync_metadata(st);
2839
b4f8e38b 2840 sra->new_chunk = info->new_chunk;
ddee071d 2841
f93346ef
AK
2842 if (restart) {
2843 /* for external metadata checkpoint saved by mdmon can be lost
2844 * or missed /due to e.g. crash/. Check if md is not during
2845 * restart farther than metadata points to.
2846 * If so, this means metadata information is obsolete.
2847 */
2848 if (st->ss->external)
2849 verify_reshape_position(info, reshape.level);
ddee071d 2850 sra->reshape_progress = info->reshape_progress;
f93346ef 2851 } else {
f897078e
N
2852 sra->reshape_progress = 0;
2853 if (reshape.after.data_disks < reshape.before.data_disks)
2854 /* start from the end of the new array */
2855 sra->reshape_progress = (sra->component_size
2856 * reshape.after.data_disks);
2857 }
2858
2859 if (info->array.chunk_size == info->new_chunk &&
7477d513
AK
2860 reshape.before.layout == reshape.after.layout &&
2861 st->ss->external == 0) {
f897078e 2862 /* use SET_ARRAY_INFO but only if reshape hasn't started */
6ef421be 2863 ioctl(fd, GET_ARRAY_INFO, &array);
5da9ab98 2864 array.raid_disks = reshape.after.data_disks + reshape.parity;
20a40eca 2865 if (!restart &&
f897078e 2866 ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
5da9ab98 2867 int err = errno;
631d7405 2868
e7b84f9d
N
2869 pr_err("Cannot set device shape for %s: %s\n",
2870 devname, strerror(errno));
7bc71196 2871
24daa16f 2872 if (err == EBUSY &&
5da9ab98 2873 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
e7b84f9d
N
2874 cont_err("Bitmap must be removed before"
2875 " shape can be changed\n");
b5420ef3 2876
5da9ab98
N
2877 goto release;
2878 }
20a40eca 2879 } else if (!restart) {
5da9ab98 2880 /* set them all just in case some old 'new_*' value
f897078e 2881 * persists from some earlier problem.
7236ee7a 2882 */
631d7405 2883 int err = 0;
5da9ab98 2884 if (sysfs_set_num(sra, NULL, "chunk_size", info->new_chunk) < 0)
631d7405 2885 err = errno;
24daa16f
N
2886 if (!err && sysfs_set_num(sra, NULL, "layout",
2887 reshape.after.layout) < 0)
631d7405
N
2888 err = errno;
2889 if (!err && subarray_set_num(container, sra, "raid_disks",
24daa16f
N
2890 reshape.after.data_disks +
2891 reshape.parity) < 0)
631d7405
N
2892 err = errno;
2893 if (err) {
e7b84f9d 2894 pr_err("Cannot set device shape for %s\n",
5da9ab98 2895 devname);
7236ee7a 2896
24daa16f 2897 if (err == EBUSY &&
5da9ab98 2898 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
e7b84f9d
N
2899 cont_err("Bitmap must be removed before"
2900 " shape can be changed\n");
5da9ab98 2901 goto release;
e86c9dd6 2902 }
5da9ab98 2903 }
e86c9dd6 2904
27a1e5b5
N
2905 err = start_reshape(sra, restart, reshape.before.data_disks,
2906 reshape.after.data_disks);
fab32c97 2907 if (err) {
e7b84f9d
N
2908 pr_err("Cannot %s reshape for %s\n",
2909 restart ? "continue" : "start",
2910 devname);
fab32c97
AK
2911 goto release;
2912 }
a93f87ee
N
2913 if (restart)
2914 sysfs_set_str(sra, NULL, "array_state", "active");
b76b30e0
AK
2915 if (freeze_reshape) {
2916 free(fdlist);
2917 free(offsets);
2918 sysfs_free(sra);
e7b84f9d 2919 pr_err("Reshape has to be continued from"
59ab9f54 2920 " location %llu when root filesystem has been mounted.\n",
b76b30e0
AK
2921 sra->reshape_progress);
2922 return 1;
2923 }
5da9ab98
N
2924
2925 /* Now we just need to kick off the reshape and watch, while
2926 * handling backups of the data...
2927 * This is all done by a forked background process.
2928 */
2929 switch(forked ? 0 : fork()) {
6b2d630c 2930 case -1:
e7b84f9d 2931 pr_err("Cannot run child to monitor reshape: %s\n",
6b2d630c 2932 strerror(errno));
6b2d630c 2933 abort_reshape(sra);
631d7405 2934 goto release;
6b2d630c 2935 default:
d152f53e
JS
2936 free(fdlist);
2937 free(offsets);
2938 sysfs_free(sra);
6b2d630c 2939 return 0;
5da9ab98 2940 case 0:
cc700db3 2941 map_fork();
6b2d630c
N
2942 break;
2943 }
5da9ab98 2944
1f9b0e28
N
2945 /* If another array on the same devices is busy, the
2946 * reshape will wait for them. This would mean that
2947 * the first section that we suspend will stay suspended
2948 * for a long time. So check on that possibility
2949 * by looking for "DELAYED" in /proc/mdstat, and if found,
2950 * wait a while
2951 */
2952 do {
2953 struct mdstat_ent *mds, *m;
2954 delayed = 0;
2955 mds = mdstat_read(0, 0);
ae0dcfbd 2956 for (m = mds; m; m = m->next)
1f9b0e28
N
2957 if (m->devnum == devname2devnum(sra->sys_name)) {
2958 if (m->resync &&
2959 m->percent == RESYNC_DELAYED)
2960 delayed = 1;
2961 if (m->resync == 0)
2962 /* Haven't started the reshape thread
2963 * yet, wait a bit
2964 */
2965 delayed = 2;
2966 break;
2967 }
2968 free_mdstat(mds);
2969 if (delayed == 1 && get_linux_version() < 3007000) {
2970 pr_err("Reshape is delayed, but cannot wait carefully with this kernel.\n"
2971 " You might experience problems until other reshapes complete.\n");
2972 delayed = 0;
2973 }
2974 if (delayed)
2975 sleep(30 - (delayed-1) * 25);
2976 } while (delayed);
2977
6b2d630c
N
2978 close(fd);
2979 if (check_env("MDADM_GROW_VERIFY"))
2980 fd = open(devname, O_RDONLY | O_DIRECT);
2981 else
2982 fd = -1;
2983 mlockall(MCL_FUTURE);
5da9ab98 2984
6b2d630c
N
2985 if (st->ss->external) {
2986 /* metadata handler takes it from here */
2987 done = st->ss->manage_reshape(
2988 fd, sra, &reshape, st, blocks,
2989 fdlist, offsets,
2990 d - odisks, fdlist+odisks,
2991 offsets+odisks);
2992 } else
2993 done = child_monitor(
2994 fd, sra, &reshape, st, blocks,
2995 fdlist, offsets,
2996 d - odisks, fdlist+odisks,
2997 offsets+odisks);
2998
d152f53e
JS
2999 free(fdlist);
3000 free(offsets);
3001
6b2d630c
N
3002 if (backup_file && done)
3003 unlink(backup_file);
3004 if (!done) {
3005 abort_reshape(sra);
3006 goto out;
3007 }
1cbc5680
N
3008
3009 if (!st->ss->external &&
3010 !(reshape.before.data_disks != reshape.after.data_disks
3011 && info->custom_array_size) &&
3012 info->new_level == reshape.level &&
3013 !forked) {
3014 /* no need to wait for the reshape to finish as
3015 * there is nothing more to do.
3016 */
d152f53e 3017 sysfs_free(sra);
1cbc5680
N
3018 exit(0);
3019 }
3020 wait_reshape(sra);
3021
3022 if (st->ss->external) {
3023 /* Re-load the metadata as much could have changed */
3024 int cfd = open_dev(st->container_dev);
3025 if (cfd >= 0) {
78340e26 3026 flush_mdmon(container);
1cbc5680
N
3027 st->ss->free_super(st);
3028 st->ss->load_container(st, cfd, container);
3029 close(cfd);
3030 }
3031 }
3032
6b2d630c
N
3033 /* set new array size if required customer_array_size is used
3034 * by this metadata.
3035 */
3036 if (reshape.before.data_disks !=
3037 reshape.after.data_disks &&
54397ed9
AK
3038 info->custom_array_size)
3039 set_array_size(st, info, info->text_version);
582496b2 3040
6b2d630c 3041 if (info->new_level != reshape.level) {
582496b2 3042
6b2d630c 3043 c = map_num(pers, info->new_level);
1cbc5680
N
3044 if (c) {
3045 err = sysfs_set_str(sra, NULL, "level", c);
3046 if (err)
e7b84f9d
N
3047 pr_err("%s: could not set level "
3048 "to %s\n", devname, c);
1cbc5680 3049 }
e919fb0a
AK
3050 if (info->new_level == 0)
3051 st->update_tail = NULL;
7236ee7a 3052 }
6b2d630c 3053out:
d152f53e 3054 sysfs_free(sra);
6b2d630c
N
3055 if (forked)
3056 return 0;
e84f2c00 3057 unfreeze(st);
6b2d630c 3058 exit(0);
e86c9dd6 3059
6b2d630c 3060release:
d152f53e
JS
3061 free(fdlist);
3062 free(offsets);
1cbc5680 3063 if (orig_level != UnSet && sra) {
7236ee7a
N
3064 c = map_num(pers, orig_level);
3065 if (c && sysfs_set_str(sra, NULL, "level", c) == 0)
e7b84f9d 3066 pr_err("aborting level change\n");
7236ee7a 3067 }
d152f53e 3068 sysfs_free(sra);
9202b8eb
N
3069 if (!forked)
3070 unfreeze(st);
631d7405 3071 return 1;
7236ee7a 3072}
e86c9dd6 3073
9ad6f6e6
AK
3074/* mdfd handle is passed to be closed in child process (after fork).
3075 */
493f5dd6 3076int reshape_container(char *container, char *devname,
9ad6f6e6 3077 int mdfd,
24daa16f 3078 struct supertype *st,
5da9ab98
N
3079 struct mdinfo *info,
3080 int force,
3081 char *backup_file,
ba728be7 3082 int verbose, int restart, int freeze_reshape)
5da9ab98 3083{
1bb174ba 3084 struct mdinfo *cc = NULL;
bcc9e9ed 3085 int rv = restart;
2d04d7e5 3086 int last_devnum = -1;
1bb174ba 3087
d8eb27f7 3088 /* component_size is not meaningful for a container,
7ec0996c 3089 * so pass '0' meaning 'no change'
d8eb27f7 3090 */
493f5dd6 3091 if (!restart &&
7ec0996c 3092 reshape_super(st, 0, info->new_level,
5da9ab98 3093 info->new_layout, info->new_chunk,
41784c88 3094 info->array.raid_disks, info->delta_disks,
016e00f5 3095 backup_file, devname, APPLY_METADATA_CHANGES,
ba728be7 3096 verbose)) {
9e325442 3097 unfreeze(st);
5da9ab98 3098 return 1;
9e325442 3099 }
5da9ab98
N
3100
3101 sync_metadata(st);
3102
1bb174ba
AK
3103 /* ping monitor to be sure that update is on disk
3104 */
3105 ping_monitor(container);
5da9ab98
N
3106
3107 switch (fork()) {
3108 case -1: /* error */
3109 perror("Cannot fork to complete reshape\n");
9e325442 3110 unfreeze(st);
5da9ab98
N
3111 return 1;
3112 default: /* parent */
b76b30e0
AK
3113 if (!freeze_reshape)
3114 printf(Name ": multi-array reshape continues"
3115 " in background\n");
5da9ab98
N
3116 return 0;
3117 case 0: /* child */
cc700db3 3118 map_fork();
5da9ab98
N
3119 break;
3120 }
3121
9ad6f6e6
AK
3122 /* close unused handle in child process
3123 */
3124 if (mdfd > -1)
3125 close(mdfd);
3126
1bb174ba
AK
3127 while(1) {
3128 /* For each member array with reshape_active,
3129 * we need to perform the reshape.
3130 * We pick the first array that needs reshaping and
3131 * reshape it. reshape_array() will re-read the metadata
3132 * so the next time through a different array should be
3133 * ready for reshape.
493f5dd6
N
3134 * It is possible that the 'different' array will not
3135 * be assembled yet. In that case we simple exit.
3136 * When it is assembled, the mdadm which assembles it
3137 * will take over the reshape.
1bb174ba
AK
3138 */
3139 struct mdinfo *content;
5da9ab98
N
3140 int fd;
3141 struct mdstat_ent *mdstat;
5da9ab98
N
3142 char *adev;
3143
1bb174ba 3144 sysfs_free(cc);
5da9ab98 3145
1bb174ba
AK
3146 cc = st->ss->container_content(st, NULL);
3147
3148 for (content = cc; content ; content = content->next) {
3149 char *subarray;
3150 if (!content->reshape_active)
3151 continue;
3152
3153 subarray = strchr(content->text_version+1, '/')+1;
3154 mdstat = mdstat_by_subdev(subarray,
3155 devname2devnum(container));
3156 if (!mdstat)
3157 continue;
1ca90aa6 3158 if (mdstat->active == 0) {
e7b84f9d 3159 pr_err("Skipping inactive "
1ca90aa6
AK
3160 "array md%i.\n", mdstat->devnum);
3161 free_mdstat(mdstat);
3162 mdstat = NULL;
3163 continue;
3164 }
1bb174ba
AK
3165 break;
3166 }
3167 if (!content)
3168 break;
5da9ab98 3169
5da9ab98
N
3170 adev = map_dev(dev2major(mdstat->devnum),
3171 dev2minor(mdstat->devnum),
3172 0);
3173 if (!adev)
1bb174ba
AK
3174 adev = content->text_version;
3175
97a3490c
AK
3176 fd = open_dev(mdstat->devnum);
3177 if (fd < 0) {
3178 printf(Name ": Device %s cannot be opened for reshape.",
3179 adev);
3180 break;
3181 }
3182
2d04d7e5
AK
3183 if (last_devnum == mdstat->devnum) {
3184 /* Do not allow for multiple reshape_array() calls for
3185 * the same array.
3186 * It can happen when reshape_array() returns without
3187 * error, when reshape is not finished (wrong reshape
3188 * starting/continuation conditions). Mdmon doesn't
3189 * switch to next array in container and reentry
3190 * conditions for the same array occur.
3191 * This is possibly interim until the behaviour of
3192 * reshape_array is resolved().
3193 */
3194 printf(Name ": Multiple reshape execution detected for "
3195 "device %s.", adev);
3196 close(fd);
3197 break;
3198 }
3199 last_devnum = mdstat->devnum;
3200
1bb174ba 3201 sysfs_init(content, fd, mdstat->devnum);
5da9ab98 3202
78340e26
AK
3203 if (mdmon_running(devname2devnum(container)))
3204 flush_mdmon(container);
3205
1bb174ba 3206 rv = reshape_array(container, fd, adev, st,
19ceb16d 3207 content, force, NULL, 0ULL,
ba728be7 3208 backup_file, verbose, 1, restart,
b76b30e0 3209 freeze_reshape);
5da9ab98 3210 close(fd);
b76b30e0
AK
3211
3212 if (freeze_reshape) {
3213 sysfs_free(cc);
3214 exit(0);
3215 }
3216
493f5dd6 3217 restart = 0;
5da9ab98
N
3218 if (rv)
3219 break;
78340e26
AK
3220
3221 if (mdmon_running(devname2devnum(container)))
3222 flush_mdmon(container);
5da9ab98 3223 }
bcc9e9ed
AK
3224 if (!rv)
3225 unfreeze(st);
1bb174ba 3226 sysfs_free(cc);
5da9ab98
N
3227 exit(0);
3228}
3229
7236ee7a
N
3230/*
3231 * We run a child process in the background which performs the following
3232 * steps:
3233 * - wait for resync to reach a certain point
3234 * - suspend io to the following section
3235 * - backup that section
3236 * - allow resync to proceed further
3237 * - resume io
3238 * - discard the backup.
3239 *
3240 * When are combined in slightly different ways in the three cases.
3241 * Grow:
3242 * - suspend/backup/allow/wait/resume/discard
3243 * Shrink:
3244 * - allow/wait/suspend/backup/allow/wait/resume/discard
3245 * same-size:
3246 * - wait/resume/discard/suspend/backup/allow
3247 *
3248 * suspend/backup/allow always come together
3249 * wait/resume/discard do too.
3250 * For the same-size case we have two backups to improve flow.
24daa16f 3251 *
7236ee7a 3252 */
e86c9dd6 3253
7443ee81
N
3254int progress_reshape(struct mdinfo *info, struct reshape *reshape,
3255 unsigned long long backup_point,
3256 unsigned long long wait_point,
3257 unsigned long long *suspend_point,
3258 unsigned long long *reshape_completed)
3259{
3260 /* This function is called repeatedly by the reshape manager.
3261 * It determines how much progress can safely be made and allows
3262 * that progress.
3263 * - 'info' identifies the array and particularly records in
3264 * ->reshape_progress the metadata's knowledge of progress
3265 * This is a sector offset from the start of the array
3266 * of the next array block to be relocated. This number
3267 * may increase from 0 or decrease from array_size, depending
3268 * on the type of reshape that is happening.
3269 * Note that in contrast, 'sync_completed' is a block count of the
3270 * reshape so far. It gives the distance between the start point
3271 * (head or tail of device) and the next place that data will be
3272 * written. It always increases.
3273 * - 'reshape' is the structure created by analyse_change
3274 * - 'backup_point' shows how much the metadata manager has backed-up
3275 * data. For reshapes with increasing progress, it is the next address
3276 * to be backed up, previous addresses have been backed-up. For
3277 * decreasing progress, it is the earliest address that has been
3278 * backed up - later address are also backed up.
3279 * So addresses between reshape_progress and backup_point are
3280 * backed up providing those are in the 'correct' order.
3281 * - 'wait_point' is an array address. When reshape_completed
3282 * passes this point, progress_reshape should return. It might
3283 * return earlier if it determines that ->reshape_progress needs
3284 * to be updated or further backup is needed.
3285 * - suspend_point is maintained by progress_reshape and the caller
3286 * should not touch it except to initialise to zero.
3287 * It is an array address and it only increases in 2.6.37 and earlier.
b6b95155 3288 * This makes it difficult to handle reducing reshapes with
7443ee81
N
3289 * external metadata.
3290 * However: it is similar to backup_point in that it records the
3291 * other end of a suspended region from reshape_progress.
3292 * it is moved to extend the region that is safe to backup and/or
3293 * reshape
3294 * - reshape_completed is read from sysfs and returned. The caller
3295 * should copy this into ->reshape_progress when it has reason to
3296 * believe that the metadata knows this, and any backup outside this
3297 * has been erased.
3298 *
3299 * Return value is:
3300 * 1 if more data from backup_point - but only as far as suspend_point,
3301 * should be backed up
3302 * 0 if things are progressing smoothly
9e034f70
N
3303 * -1 if the reshape is finished because it is all done,
3304 * -2 if the reshape is finished due to an error.
7443ee81
N
3305 */
3306
3307 int advancing = (reshape->after.data_disks
3308 >= reshape->before.data_disks);
38dad34a
N
3309 unsigned long long need_backup; /* All data between start of array and
3310 * here will at some point need to
3311 * be backed up.
d0ab945e 3312 */
7443ee81 3313 unsigned long long read_offset, write_offset;
b4f8e38b 3314 unsigned long long write_range;
7443ee81 3315 unsigned long long max_progress, target, completed;
d0ab945e
N
3316 unsigned long long array_size = (info->component_size
3317 * reshape->before.data_disks);
7443ee81 3318 int fd;
77a73a17 3319 char buf[20];
7443ee81
N
3320
3321 /* First, we unsuspend any region that is now known to be safe.
3322 * If suspend_point is on the 'wrong' side of reshape_progress, then
3323 * we don't have or need suspension at the moment. This is true for
3324 * native metadata when we don't need to back-up.
3325 */
3326 if (advancing) {
49cd738b 3327 if (info->reshape_progress <= *suspend_point)
7443ee81
N
3328 sysfs_set_num(info, NULL, "suspend_lo",
3329 info->reshape_progress);
3330 } else {
3331 /* Note: this won't work in 2.6.37 and before.
3332 * Something somewhere should make sure we don't need it!
3333 */
49cd738b 3334 if (info->reshape_progress >= *suspend_point)
7443ee81
N
3335 sysfs_set_num(info, NULL, "suspend_hi",
3336 info->reshape_progress);
3337 }
3338
3339 /* Now work out how far it is safe to progress.
3340 * If the read_offset for ->reshape_progress is less than
3341 * 'blocks' beyond the write_offset, we can only progress as far
3342 * as a backup.
3343 * Otherwise we can progress until the write_offset for the new location
3344 * reaches (within 'blocks' of) the read_offset at the current location.
3345 * However that region must be suspended unless we are using native
3346 * metadata.
3347 * If we need to suspend more, we limit it to 128M per device, which is
3348 * rather arbitrary and should be some time-based calculation.
3349 */
ec757320
N
3350 read_offset = info->reshape_progress / reshape->before.data_disks;
3351 write_offset = info->reshape_progress / reshape->after.data_disks;
b4f8e38b 3352 write_range = info->new_chunk/512;
38dad34a
N
3353 if (reshape->before.data_disks == reshape->after.data_disks)
3354 need_backup = array_size;
3355 else
3356 need_backup = reshape->backup_blocks;
7443ee81 3357 if (advancing) {
38dad34a 3358 if (read_offset < write_offset + write_range)
7443ee81 3359 max_progress = backup_point;
ddee071d 3360 else
7443ee81 3361 max_progress =
2f3dd5e4
N
3362 read_offset *
3363 reshape->after.data_disks;
7443ee81 3364 } else {
38dad34a
N
3365 if (read_offset > write_offset - write_range)
3366 /* Can only progress as far as has been backed up,
3367 * which must be suspended */
7443ee81 3368 max_progress = backup_point;
38dad34a
N
3369 else if (info->reshape_progress <= need_backup)
3370 max_progress = backup_point;
3371 else {
3372 if (info->array.major_version >= 0)
3373 /* Can progress until backup is needed */
3374 max_progress = need_backup;
3375 else {
3376 /* Can progress until metadata update is required */
3377 max_progress =
3378 read_offset *
3379 reshape->after.data_disks;
3380 /* but data must be suspended */
3381 if (max_progress < *suspend_point)
3382 max_progress = *suspend_point;
3383 }
7443ee81
N
3384 }
3385 }
3386
3387 /* We know it is safe to progress to 'max_progress' providing
3388 * it is suspended or we are using native metadata.
3389 * Consider extending suspend_point 128M per device if it
3390 * is less than 64M per device beyond reshape_progress.
3391 * But always do a multiple of 'blocks'
832b2b9a
N
3392 * FIXME this is too big - it takes to long to complete
3393 * this much.
7443ee81
N
3394 */
3395 target = 64*1024*2 * min(reshape->before.data_disks,
24daa16f 3396 reshape->after.data_disks);
b4f8e38b 3397 target /= reshape->backup_blocks;
7443ee81
N
3398 if (target < 2)
3399 target = 2;
b4f8e38b 3400 target *= reshape->backup_blocks;
7443ee81
N
3401
3402 /* For externally managed metadata we always need to suspend IO to
3403 * the area being reshaped so we regularly push suspend_point forward.
3404 * For native metadata we only need the suspend if we are going to do
3405 * a backup.
3406 */
3407 if (advancing) {
d0ab945e
N
3408 if ((need_backup > info->reshape_progress
3409 || info->array.major_version < 0) &&
7443ee81 3410 *suspend_point < info->reshape_progress + target) {
d0ab945e
N
3411 if (need_backup < *suspend_point + 2 * target)
3412 *suspend_point = need_backup;
3413 else if (*suspend_point + 2 * target < array_size)
7443ee81 3414 *suspend_point += 2 * target;
d0ab945e
N
3415 else
3416 *suspend_point = array_size;
7443ee81 3417 sysfs_set_num(info, NULL, "suspend_hi", *suspend_point);
d0ab945e
N
3418 if (max_progress > *suspend_point)
3419 max_progress = *suspend_point;
7443ee81
N
3420 }
3421 } else {
38dad34a
N
3422 if (info->array.major_version >= 0) {
3423 /* Only need to suspend when about to backup */
3424 if (info->reshape_progress < need_backup * 2 &&
3425 *suspend_point > 0) {
d0ab945e 3426 *suspend_point = 0;
38dad34a
N
3427 sysfs_set_num(info, NULL, "suspend_lo", 0);
3428 sysfs_set_num(info, NULL, "suspend_hi", need_backup);
3429 }
3430 } else {
3431 /* Need to suspend continually */
3432 if (info->reshape_progress < *suspend_point)
3433 *suspend_point = info->reshape_progress;
3434 if (*suspend_point + target < info->reshape_progress)
3435 /* No need to move suspend region yet */;
3436 else {
3437 if (*suspend_point >= 2 * target)
3438 *suspend_point -= 2 * target;
3439 else
3440 *suspend_point = 0;
3441 sysfs_set_num(info, NULL, "suspend_lo",
3442 *suspend_point);
3443 }
d0ab945e
N
3444 if (max_progress < *suspend_point)
3445 max_progress = *suspend_point;
7443ee81
N
3446 }
3447 }
3448
3449 /* now set sync_max to allow that progress. sync_max, like
3450 * sync_completed is a count of sectors written per device, so
3451 * we find the difference between max_progress and the start point,
3452 * and divide that by after.data_disks to get a sync_max
3453 * number.
3454 * At the same time we convert wait_point to a similar number
3455 * for comparing against sync_completed.
3456 */
92d1991f 3457 /* scale down max_progress to per_disk */
7443ee81 3458 max_progress /= reshape->after.data_disks;
92d1991f
N
3459 /* Round to chunk size as some kernels give an erroneously high number */
3460 max_progress /= info->new_chunk/512;
3461 max_progress *= info->new_chunk/512;
7f913e9b
N
3462 /* And round to old chunk size as the kernel wants that */
3463 max_progress /= info->array.chunk_size/512;
3464 max_progress *= info->array.chunk_size/512;
92d1991f
N
3465 /* Limit progress to the whole device */
3466 if (max_progress > info->component_size)
3467 max_progress = info->component_size;
7443ee81 3468 wait_point /= reshape->after.data_disks;
92d1991f
N
3469 if (!advancing) {
3470 /* switch from 'device offset' to 'processed block count' */
3471 max_progress = info->component_size - max_progress;
3472 wait_point = info->component_size - wait_point;
3473 }
7443ee81
N
3474
3475 sysfs_set_num(info, NULL, "sync_max", max_progress);
3476
3477 /* Now wait. If we have already reached the point that we were
3478 * asked to wait to, don't wait at all, else wait for any change.
3479 * We need to select on 'sync_completed' as that is the place that
3480 * notifications happen, but we are really interested in
3481 * 'reshape_position'
3482 */
3483 fd = sysfs_get_fd(info, NULL, "sync_completed");
3484 if (fd < 0)
77a73a17 3485 goto check_progress;
7443ee81 3486
621ea11b 3487 if (sysfs_fd_get_ll(fd, &completed) < 0)
77a73a17 3488 goto check_progress;
621ea11b 3489
7443ee81
N
3490 while (completed < max_progress && completed < wait_point) {
3491 /* Check that sync_action is still 'reshape' to avoid
3492 * waiting forever on a dead array
3493 */
3494 char action[20];
3495 fd_set rfds;
3496 if (sysfs_get_str(info, NULL, "sync_action",
3497 action, 20) <= 0 ||
3498 strncmp(action, "reshape", 7) != 0)
3499 break;
26d6e157
AK
3500 /* Some kernels reset 'sync_completed' to zero
3501 * before setting 'sync_action' to 'idle'.
3502 * So we need these extra tests.
3503 */
3504 if (completed == 0 && advancing
3505 && info->reshape_progress > 0)
3506 break;
3507 if (completed == 0 && !advancing
3508 && info->reshape_progress < (info->component_size
3509 * reshape->after.data_disks))
3510 break;
7443ee81
N
3511 FD_ZERO(&rfds);
3512 FD_SET(fd, &rfds);
3513 select(fd+1, NULL, NULL, &rfds, NULL);
621ea11b 3514 if (sysfs_fd_get_ll(fd, &completed) < 0)
77a73a17 3515 goto check_progress;
7443ee81 3516 }
10d0d365
AK
3517 /* Some kernels reset 'sync_completed' to zero,
3518 * we need to have real point we are in md
3519 */
3520 if (completed == 0)
3521 completed = max_progress;
3522
92d1991f
N
3523 /* some kernels can give an incorrectly high 'completed' number */
3524 completed /= (info->new_chunk/512);
3525 completed *= (info->new_chunk/512);
7443ee81
N
3526 /* Convert 'completed' back in to a 'progress' number */
3527 completed *= reshape->after.data_disks;
3528 if (!advancing) {
3529 completed = info->component_size * reshape->after.data_disks
3530 - completed;
3531 }
3532 *reshape_completed = completed;
24daa16f 3533
7443ee81
N
3534 close(fd);
3535
3536 /* We return the need_backup flag. Caller will decide
d0ab945e 3537 * how much - a multiple of ->backup_blocks up to *suspend_point
7443ee81 3538 */
38dad34a
N
3539 if (advancing)
3540 return need_backup > info->reshape_progress;
3541 else
3542 return need_backup >= info->reshape_progress;
77a73a17
N
3543
3544check_progress:
3545 /* if we couldn't read a number from sync_completed, then
3546 * either the reshape did complete, or it aborted.
3547 * We can tell which by checking for 'none' in reshape_position.
621ea11b
N
3548 * If it did abort, then it might immediately restart if it
3549 * it was just a device failure that leaves us degraded but
3550 * functioning.
77a73a17
N
3551 */
3552 strcpy(buf, "hi");
3553 if (sysfs_get_str(info, NULL, "reshape_position", buf, sizeof(buf)) < 0
621ea11b
N
3554 || strncmp(buf, "none", 4) != 0) {
3555 /* The abort might only be temporary. Wait up to 10
3556 * seconds for fd to contain a valid number again.
3557 */
3558 struct timeval tv;
3559 int rv = -2;
3560 tv.tv_sec = 10;
3561 tv.tv_usec = 0;
6560987b 3562 while (fd >= 0 && rv < 0 && tv.tv_sec > 0) {
621ea11b
N
3563 fd_set rfds;
3564 FD_ZERO(&rfds);
3565 FD_SET(fd, &rfds);
3566 if (select(fd+1, NULL, NULL, &rfds, &tv) != 1)
3567 break;
6560987b
N
3568 switch (sysfs_fd_get_ll(fd, &completed)) {
3569 case 0:
621ea11b
N
3570 /* all good again */
3571 rv = 1;
6560987b
N
3572 break;
3573 case -2: /* read error - abort */
3574 tv.tv_sec = 0;
3575 break;
3576 }
621ea11b
N
3577 }
3578 if (fd >= 0)
3579 close(fd);
3580 return rv; /* abort */
3581 } else {
6d5316f6 3582 /* Maybe racing with array shutdown - check state */
621ea11b
N
3583 if (fd >= 0)
3584 close(fd);
6d5316f6
N
3585 if (sysfs_get_str(info, NULL, "array_state", buf, sizeof(buf)) < 0
3586 || strncmp(buf, "inactive", 8) == 0
3587 || strncmp(buf, "clear",5) == 0)
3588 return -2; /* abort */
77a73a17 3589 return -1; /* complete */
6d5316f6 3590 }
7443ee81
N
3591}
3592
fcf57625 3593/* FIXME return status is never checked */
4411fb17 3594static int grow_backup(struct mdinfo *sra,
7236ee7a 3595 unsigned long long offset, /* per device */
7443ee81 3596 unsigned long stripes, /* per device, in old chunks */
7236ee7a
N
3597 int *sources, unsigned long long *offsets,
3598 int disks, int chunk, int level, int layout,
3599 int dests, int *destfd, unsigned long long *destoffsets,
d4445387 3600 int part, int *degraded,
7236ee7a
N
3601 char *buf)
3602{
3603 /* Backup 'blocks' sectors at 'offset' on each device of the array,
3604 * to storage 'destfd' (offset 'destoffsets'), after first
3605 * suspending IO. Then allow resync to continue
3606 * over the suspended section.
3607 * Use part 'part' of the backup-super-block.
3608 */
3609 int odata = disks;
3610 int rv = 0;
3611 int i;
f21e18ca
N
3612 unsigned long long ll;
3613 int new_degraded;
7236ee7a
N
3614 //printf("offset %llu\n", offset);
3615 if (level >= 4)
3616 odata--;
3617 if (level == 6)
3618 odata--;
7443ee81 3619
d4445387 3620 /* Check that array hasn't become degraded, else we might backup the wrong data */
2c6ac128
N
3621 if (sysfs_get_ll(sra, NULL, "degraded", &ll) < 0)
3622 return -1; /* FIXME this error is ignored */
f21e18ca 3623 new_degraded = (int)ll;
d4445387
N
3624 if (new_degraded != *degraded) {
3625 /* check each device to ensure it is still working */
3626 struct mdinfo *sd;
3627 for (sd = sra->devs ; sd ; sd = sd->next) {
3628 if (sd->disk.state & (1<<MD_DISK_FAULTY))
3629 continue;
3630 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
3631 char sbuf[20];
3632 if (sysfs_get_str(sra, sd, "state", sbuf, 20) < 0 ||
3633 strstr(sbuf, "faulty") ||
3634 strstr(sbuf, "in_sync") == NULL) {
3635 /* this device is dead */
3636 sd->disk.state = (1<<MD_DISK_FAULTY);
3637 if (sd->disk.raid_disk >= 0 &&
3638 sources[sd->disk.raid_disk] >= 0) {
3639 close(sources[sd->disk.raid_disk]);
3640 sources[sd->disk.raid_disk] = -1;
3641 }
3642 }
3643 }
3644 }
3645 *degraded = new_degraded;
3646 }
7236ee7a
N
3647 if (part) {
3648 bsb.arraystart2 = __cpu_to_le64(offset * odata);
200871ad 3649 bsb.length2 = __cpu_to_le64(stripes * (chunk/512) * odata);
7236ee7a
N
3650 } else {
3651 bsb.arraystart = __cpu_to_le64(offset * odata);
200871ad 3652 bsb.length = __cpu_to_le64(stripes * (chunk/512) * odata);
7236ee7a
N
3653 }
3654 if (part)
3655 bsb.magic[15] = '2';
3656 for (i = 0; i < dests; i++)
3657 if (part)
3658 lseek64(destfd[i], destoffsets[i] + __le64_to_cpu(bsb.devstart2)*512, 0);
3659 else
3660 lseek64(destfd[i], destoffsets[i], 0);
3661
24daa16f 3662 rv = save_stripes(sources, offsets,
7236ee7a
N
3663 disks, chunk, level, layout,
3664 dests, destfd,
3665 offset*512*odata, stripes * chunk * odata,
3666 buf);
3667
3668 if (rv)
3669 return rv;
97396422 3670 bsb.mtime = __cpu_to_le64(time(0));
7236ee7a
N
3671 for (i = 0; i < dests; i++) {
3672 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
3673
3674 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
3675 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
3676 bsb.sb_csum2 = bsb_csum((char*)&bsb,
3677 ((char*)&bsb.sb_csum2)-((char*)&bsb));
3678
72044953 3679 rv = -1;
f21e18ca
N
3680 if ((unsigned long long)lseek64(destfd[i], destoffsets[i] - 4096, 0)
3681 != destoffsets[i] - 4096)
72044953
N
3682 break;
3683 if (write(destfd[i], &bsb, 512) != 512)
3684 break;
ff94fb86 3685 if (destoffsets[i] > 4096) {
f21e18ca 3686 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]+stripes*chunk*odata, 0) !=
a847575a 3687 destoffsets[i]+stripes*chunk*odata)
72044953
N
3688 break;
3689 if (write(destfd[i], &bsb, 512) != 512)
3690 break;
ff94fb86 3691 }
7236ee7a 3692 fsync(destfd[i]);
72044953 3693 rv = 0;
7236ee7a 3694 }
2efedc7b 3695
fcf57625 3696 return rv;
7236ee7a
N
3697}
3698
3699/* in 2.6.30, the value reported by sync_completed can be
3700 * less that it should be by one stripe.
3701 * This only happens when reshape hits sync_max and pauses.
3702 * So allow wait_backup to either extent sync_max further
3703 * than strictly necessary, or return before the
3704 * sync has got quite as far as we would really like.
3705 * This is what 'blocks2' is for.
3706 * The various caller give appropriate values so that
3707 * every works.
3708 */
fcf57625 3709/* FIXME return value is often ignored */
24daa16f
N
3710static int forget_backup(int dests, int *destfd,
3711 unsigned long long *destoffsets,
3712 int part)
7236ee7a 3713{
24daa16f 3714 /*
7443ee81 3715 * Erase backup 'part' (which is 0 or 1)
7236ee7a 3716 */
7236ee7a 3717 int i;
fcf57625 3718 int rv;
7236ee7a 3719
7236ee7a
N
3720 if (part) {
3721 bsb.arraystart2 = __cpu_to_le64(0);
3722 bsb.length2 = __cpu_to_le64(0);
3723 } else {
3724 bsb.arraystart = __cpu_to_le64(0);
3725 bsb.length = __cpu_to_le64(0);
3726 }
97396422 3727 bsb.mtime = __cpu_to_le64(time(0));
fcf57625 3728 rv = 0;
7236ee7a
N
3729 for (i = 0; i < dests; i++) {
3730 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
3731 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
3732 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
3733 bsb.sb_csum2 = bsb_csum((char*)&bsb,
3734 ((char*)&bsb.sb_csum2)-((char*)&bsb));
f21e18ca 3735 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]-4096, 0) !=
a847575a 3736 destoffsets[i]-4096)
72044953 3737 rv = -1;
24daa16f 3738 if (rv == 0 &&
72044953
N
3739 write(destfd[i], &bsb, 512) != 512)
3740 rv = -1;
7236ee7a
N
3741 fsync(destfd[i]);
3742 }
fcf57625 3743 return rv;
7236ee7a 3744}
e86c9dd6 3745
7236ee7a
N
3746static void fail(char *msg)
3747{
fcf57625 3748 int rv;
72044953
N
3749 rv = (write(2, msg, strlen(msg)) != (int)strlen(msg));
3750 rv |= (write(2, "\n", 1) != 1);
fcf57625 3751 exit(rv ? 1 : 2);
7236ee7a
N
3752}
3753
3754static char *abuf, *bbuf;
f21e18ca 3755static unsigned long long abuflen;
7236ee7a
N
3756static void validate(int afd, int bfd, unsigned long long offset)
3757{
3758 /* check that the data in the backup against the array.
3759 * This is only used for regression testing and should not
3760 * be used while the array is active
3761 */
7236ee7a
N
3762 if (afd < 0)
3763 return;
3764 lseek64(bfd, offset - 4096, 0);
3765 if (read(bfd, &bsb2, 512) != 512)
3766 fail("cannot read bsb");
3767 if (bsb2.sb_csum != bsb_csum((char*)&bsb2,
3768 ((char*)&bsb2.sb_csum)-((char*)&bsb2)))
3769 fail("first csum bad");
3770 if (memcmp(bsb2.magic, "md_backup_data", 14) != 0)
3771 fail("magic is bad");
3772 if (memcmp(bsb2.magic, "md_backup_data-2", 16) == 0 &&
3773 bsb2.sb_csum2 != bsb_csum((char*)&bsb2,
24daa16f 3774 ((char*)&bsb2.sb_csum2)-((char*)&bsb2)))
7236ee7a
N
3775 fail("second csum bad");
3776
3777 if (__le64_to_cpu(bsb2.devstart)*512 != offset)
3778 fail("devstart is wrong");
3779
3780 if (bsb2.length) {
3781 unsigned long long len = __le64_to_cpu(bsb2.length)*512;
3782
3783 if (abuflen < len) {
3784 free(abuf);
3785 free(bbuf);
3786 abuflen = len;
fcf57625
N
3787 if (posix_memalign((void**)&abuf, 4096, abuflen) ||
3788 posix_memalign((void**)&bbuf, 4096, abuflen)) {
3789 abuflen = 0;
3790 /* just stop validating on mem-alloc failure */
3791 return;
3792 }
e86c9dd6 3793 }
48924014 3794
7236ee7a 3795 lseek64(bfd, offset, 0);
f21e18ca 3796 if ((unsigned long long)read(bfd, bbuf, len) != len) {
080fd005 3797 //printf("len %llu\n", len);
7236ee7a
N
3798 fail("read first backup failed");
3799 }
3800 lseek64(afd, __le64_to_cpu(bsb2.arraystart)*512, 0);
f21e18ca 3801 if ((unsigned long long)read(afd, abuf, len) != len)
7236ee7a
N
3802 fail("read first from array failed");
3803 if (memcmp(bbuf, abuf, len) != 0) {
24daa16f 3804#if 0
7236ee7a
N
3805 int i;
3806 printf("offset=%llu len=%llu\n",
080fd005 3807 (unsigned long long)__le64_to_cpu(bsb2.arraystart)*512, len);
7236ee7a
N
3808 for (i=0; i<len; i++)
3809 if (bbuf[i] != abuf[i]) {
3810 printf("first diff byte %d\n", i);
93ecfa01 3811 break;
7236ee7a 3812 }
24daa16f 3813#endif
7236ee7a 3814 fail("data1 compare failed");
e86c9dd6 3815 }
7236ee7a
N
3816 }
3817 if (bsb2.length2) {
3818 unsigned long long len = __le64_to_cpu(bsb2.length2)*512;
3819
3820 if (abuflen < len) {
3821 free(abuf);
3822 free(bbuf);
3823 abuflen = len;
503975b9
N
3824 abuf = xmalloc(abuflen);
3825 bbuf = xmalloc(abuflen);
e86c9dd6
NB
3826 }
3827
7236ee7a 3828 lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0);
f21e18ca 3829 if ((unsigned long long)read(bfd, bbuf, len) != len)
7236ee7a
N
3830 fail("read second backup failed");
3831 lseek64(afd, __le64_to_cpu(bsb2.arraystart2)*512, 0);
f21e18ca 3832 if ((unsigned long long)read(afd, abuf, len) != len)
7236ee7a
N
3833 fail("read second from array failed");
3834 if (memcmp(bbuf, abuf, len) != 0)
3835 fail("data2 compare failed");
e86c9dd6 3836 }
7236ee7a 3837}
e86c9dd6 3838
999b4972
N
3839int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
3840 struct supertype *st, unsigned long blocks,
3841 int *fds, unsigned long long *offsets,
3842 int dests, int *destfd, unsigned long long *destoffsets)
7236ee7a 3843{
7443ee81
N
3844 /* Monitor a reshape where backup is being performed using
3845 * 'native' mechanism - either to a backup file, or
3846 * to some space in a spare.
3847 */
7236ee7a 3848 char *buf;
7443ee81
N
3849 int degraded = -1;
3850 unsigned long long speed;
3851 unsigned long long suspend_point, array_size;
3852 unsigned long long backup_point, wait_point;
3853 unsigned long long reshape_completed;
3854 int done = 0;
3855 int increasing = reshape->after.data_disks >= reshape->before.data_disks;
3856 int part = 0; /* The next part of the backup area to fill. It may already
3857 * be full, so we need to check */
3858 int level = reshape->level;
3859 int layout = reshape->before.layout;
3860 int data = reshape->before.data_disks;
3861 int disks = reshape->before.data_disks + reshape->parity;
3862 int chunk = sra->array.chunk_size;
da8100ca
N
3863 struct mdinfo *sd;
3864 unsigned long stripes;
3b7e9d0c 3865 int uuid[4];
da8100ca
N
3866
3867 /* set up the backup-super-block. This requires the
3868 * uuid from the array.
3869 */
3870 /* Find a superblock */
3871 for (sd = sra->devs; sd; sd = sd->next) {
3872 char *dn;
3873 int devfd;
3874 int ok;
3875 if (sd->disk.state & (1<<MD_DISK_FAULTY))
3876 continue;
3877 dn = map_dev(sd->disk.major, sd->disk.minor, 1);
3878 devfd = dev_open(dn, O_RDONLY);
3879 if (devfd < 0)
3880 continue;
3881 ok = st->ss->load_super(st, devfd, NULL);
3882 close(devfd);
77f8d358 3883 if (ok == 0)
da8100ca
N
3884 break;
3885 }
3886 if (!sd) {
e7b84f9d 3887 pr_err("Cannot find a superblock\n");
da8100ca
N
3888 return 0;
3889 }
3890
3891 memset(&bsb, 0, 512);
3892 memcpy(bsb.magic, "md_backup_data-1", 16);
3b7e9d0c
LB
3893 st->ss->uuid_from_super(st, uuid);
3894 memcpy(bsb.set_uuid, uuid, 16);
da8100ca
N
3895 bsb.mtime = __cpu_to_le64(time(0));
3896 bsb.devstart2 = blocks;
3897
3898 stripes = blocks / (sra->array.chunk_size/512) /
3899 reshape->before.data_disks;
e86c9dd6 3900
fcf57625
N
3901 if (posix_memalign((void**)&buf, 4096, disks * chunk))
3902 /* Don't start the 'reshape' */
3903 return 0;
7443ee81
N
3904 if (reshape->before.data_disks == reshape->after.data_disks) {
3905 sysfs_get_ll(sra, NULL, "sync_speed_min", &speed);
3906 sysfs_set_num(sra, NULL, "sync_speed_min", 200000);
3907 }
e86c9dd6 3908
7443ee81 3909 if (increasing) {
96533072 3910 array_size = sra->component_size * reshape->after.data_disks;
7443ee81
N
3911 backup_point = sra->reshape_progress;
3912 suspend_point = 0;
3913 } else {
96533072 3914 array_size = sra->component_size * reshape->before.data_disks;
25da62d9 3915 backup_point = reshape->backup_blocks;
7443ee81
N
3916 suspend_point = array_size;
3917 }
7236ee7a 3918
7443ee81
N
3919 while (!done) {
3920 int rv;
7236ee7a 3921
7443ee81
N
3922 /* Want to return as soon the oldest backup slot can
3923 * be released as that allows us to start backing up
3924 * some more, providing suspend_point has been
b6b95155 3925 * advanced, which it should have.
7443ee81
N
3926 */
3927 if (increasing) {
3928 wait_point = array_size;
3929 if (part == 0 && __le64_to_cpu(bsb.length) > 0)
3930 wait_point = (__le64_to_cpu(bsb.arraystart) +
3931 __le64_to_cpu(bsb.length));
3932 if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
3933 wait_point = (__le64_to_cpu(bsb.arraystart2) +
3934 __le64_to_cpu(bsb.length2));
3935 } else {
3936 wait_point = 0;
3937 if (part == 0 && __le64_to_cpu(bsb.length) > 0)
3938 wait_point = __le64_to_cpu(bsb.arraystart);
3939 if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
3940 wait_point = __le64_to_cpu(bsb.arraystart2);
3941 }
3942
3943 rv = progress_reshape(sra, reshape,
3944 backup_point, wait_point,
3945 &suspend_point, &reshape_completed);
7443ee81
N
3946 /* external metadata would need to ping_monitor here */
3947 sra->reshape_progress = reshape_completed;
7236ee7a 3948
7443ee81
N
3949 /* Clear any backup region that is before 'here' */
3950 if (increasing) {
b3cf095a
N
3951 if (__le64_to_cpu(bsb.length) > 0 &&
3952 reshape_completed >= (__le64_to_cpu(bsb.arraystart) +
7443ee81
N
3953 __le64_to_cpu(bsb.length)))
3954 forget_backup(dests, destfd,
3955 destoffsets, 0);
b3cf095a
N
3956 if (__le64_to_cpu(bsb.length2) > 0 &&
3957 reshape_completed >= (__le64_to_cpu(bsb.arraystart2) +
7443ee81
N
3958 __le64_to_cpu(bsb.length2)))
3959 forget_backup(dests, destfd,
3960 destoffsets, 1);
3961 } else {
b3cf095a
N
3962 if (__le64_to_cpu(bsb.length) > 0 &&
3963 reshape_completed <= (__le64_to_cpu(bsb.arraystart)))
7443ee81
N
3964 forget_backup(dests, destfd,
3965 destoffsets, 0);
b3cf095a
N
3966 if (__le64_to_cpu(bsb.length2) > 0 &&
3967 reshape_completed <= (__le64_to_cpu(bsb.arraystart2)))
7443ee81
N
3968 forget_backup(dests, destfd,
3969 destoffsets, 1);
3970 }
7236ee7a 3971
223c5c99 3972 if (rv < 0) {
77a73a17
N
3973 if (rv == -1)
3974 done = 1;
223c5c99
N
3975 break;
3976 }
2d4de5f9
N
3977 if (rv == 0 && increasing && !st->ss->external) {
3978 /* No longer need to monitor this reshape */
3979 done = 1;
3980 break;
3981 }
223c5c99 3982
60204e4e 3983 while (rv) {
7443ee81 3984 unsigned long long offset;
e97ca002 3985 unsigned long actual_stripes;
60204e4e
N
3986 /* Need to backup some data.
3987 * If 'part' is not used and the desired
3988 * backup size is suspended, do a backup,
3989 * then consider the next part.
3990 */
7443ee81
N
3991 /* Check that 'part' is unused */
3992 if (part == 0 && __le64_to_cpu(bsb.length) != 0)
60204e4e 3993 break;
7443ee81 3994 if (part == 1 && __le64_to_cpu(bsb.length2) != 0)
60204e4e 3995 break;
7443ee81
N
3996
3997 offset = backup_point / data;
e97ca002 3998 actual_stripes = stripes;
60204e4e 3999 if (increasing) {
e97ca002
N
4000 if (offset + actual_stripes * (chunk/512) >
4001 sra->component_size)
4002 actual_stripes = ((sra->component_size - offset)
4003 / (chunk/512));
4004 if (offset + actual_stripes * (chunk/512) >
60204e4e
N
4005 suspend_point/data)
4006 break;
4007 } else {
e97ca002
N
4008 if (offset < actual_stripes * (chunk/512))
4009 actual_stripes = offset / (chunk/512);
4010 offset -= actual_stripes * (chunk/512);
4011 if (offset < suspend_point/data)
60204e4e
N
4012 break;
4013 }
e4b11073
N
4014 if (actual_stripes == 0)
4015 break;
e97ca002 4016 grow_backup(sra, offset, actual_stripes,
7443ee81
N
4017 fds, offsets,
4018 disks, chunk, level, layout,
4019 dests, destfd, destoffsets,
4020 part, &degraded, buf);
4021 validate(afd, destfd[0], destoffsets[0]);
4022 /* record where 'part' is up to */
4023 part = !part;
4024 if (increasing)
e97ca002 4025 backup_point += actual_stripes * (chunk/512) * data;
7443ee81 4026 else
e97ca002 4027 backup_point -= actual_stripes * (chunk/512) * data;
7443ee81
N
4028 }
4029 }
4030
223c5c99
N
4031 /* FIXME maybe call progress_reshape one more time instead */
4032 abort_reshape(sra); /* remove any remaining suspension */
7443ee81
N
4033 if (reshape->before.data_disks == reshape->after.data_disks)
4034 sysfs_set_num(sra, NULL, "sync_speed_min", speed);
7236ee7a 4035 free(buf);
77a73a17 4036 return done;
e86c9dd6 4037}
353632d9
NB
4038
4039/*
4040 * If any spare contains md_back_data-1 which is recent wrt mtime,
4041 * write that data into the array and update the super blocks with
4042 * the new reshape_progress
4043 */
ea0ebe96
N
4044int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt,
4045 char *backup_file, int verbose)
353632d9
NB
4046{
4047 int i, j;
4048 int old_disks;
353632d9 4049 unsigned long long *offsets;
82f2d6ab 4050 unsigned long long nstripe, ostripe;
6e9eac4f 4051 int ndata, odata;
353632d9 4052
e9e43ec3
N
4053 odata = info->array.raid_disks - info->delta_disks - 1;
4054 if (info->array.level == 6) odata--; /* number of data disks */
4055 ndata = info->array.raid_disks - 1;
4056 if (info->new_level == 6) ndata--;
353632d9
NB
4057
4058 old_disks = info->array.raid_disks - info->delta_disks;
4059
e9e43ec3
N
4060 if (info->delta_disks <= 0)
4061 /* Didn't grow, so the backup file must have
4062 * been used
4063 */
4064 old_disks = cnt;
06b0d786 4065 for (i=old_disks-(backup_file?1:0); i<cnt; i++) {
353632d9 4066 struct mdinfo dinfo;
06b0d786 4067 int fd;
e9e43ec3 4068 int bsbsize;
ea0ebe96 4069 char *devname, namebuf[20];
10af14c4 4070 unsigned long long lo, hi;
353632d9
NB
4071
4072 /* This was a spare and may have some saved data on it.
4073 * Load the superblock, find and load the
4074 * backup_super_block.
4075 * If either fail, go on to next device.
4076 * If the backup contains no new info, just return
206c5eae 4077 * else restore data and update all superblocks
353632d9 4078 */
06b0d786
NB
4079 if (i == old_disks-1) {
4080 fd = open(backup_file, O_RDONLY);
e9e43ec3 4081 if (fd<0) {
e7b84f9d 4082 pr_err("backup file %s inaccessible: %s\n",
e9e43ec3 4083 backup_file, strerror(errno));
06b0d786 4084 continue;
e9e43ec3 4085 }
ea0ebe96 4086 devname = backup_file;
06b0d786
NB
4087 } else {
4088 fd = fdlist[i];
4089 if (fd < 0)
4090 continue;
3da92f27 4091 if (st->ss->load_super(st, fd, NULL))
06b0d786 4092 continue;
353632d9 4093
a5d85af7 4094 st->ss->getinfo_super(st, &dinfo, NULL);
3da92f27
NB
4095 st->ss->free_super(st);
4096
06b0d786
NB
4097 if (lseek64(fd,
4098 (dinfo.data_offset + dinfo.component_size - 8) <<9,
ea0ebe96 4099 0) < 0) {
e7b84f9d 4100 pr_err("Cannot seek on device %d\n", i);
06b0d786 4101 continue; /* Cannot seek */
ea0ebe96
N
4102 }
4103 sprintf(namebuf, "device-%d", i);
4104 devname = namebuf;
06b0d786 4105 }
ea0ebe96
N
4106 if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb)) {
4107 if (verbose)
e7b84f9d 4108 pr_err("Cannot read from %s\n", devname);
353632d9 4109 continue; /* Cannot read */
ea0ebe96 4110 }
e9e43ec3 4111 if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0 &&
ea0ebe96
N
4112 memcmp(bsb.magic, "md_backup_data-2", 16) != 0) {
4113 if (verbose)
e7b84f9d 4114 pr_err("No backup metadata on %s\n", devname);
353632d9 4115 continue;
ea0ebe96
N
4116 }
4117 if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb))) {
4118 if (verbose)
e7b84f9d 4119 pr_err("Bad backup-metadata checksum on %s\n", devname);
353632d9 4120 continue; /* bad checksum */
ea0ebe96 4121 }
e9e43ec3 4122 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0 &&
ea0ebe96
N
4123 bsb.sb_csum2 != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum2)-((char*)&bsb))) {
4124 if (verbose)
e7b84f9d 4125 pr_err("Bad backup-metadata checksum2 on %s\n", devname);
22e30516 4126 continue; /* Bad second checksum */
ea0ebe96
N
4127 }
4128 if (memcmp(bsb.set_uuid,info->uuid, 16) != 0) {
4129 if (verbose)
e7b84f9d 4130 pr_err("Wrong uuid on backup-metadata on %s\n", devname);
353632d9 4131 continue; /* Wrong uuid */
ea0ebe96 4132 }
353632d9 4133
097075b6
N
4134 /* array utime and backup-mtime should be updated at much the same time, but it seems that
4135 * sometimes they aren't... So allow considerable flexability in matching, and allow
4136 * this test to be overridden by an environment variable.
4137 */
f21e18ca
N
4138 if (info->array.utime > (int)__le64_to_cpu(bsb.mtime) + 2*60*60 ||
4139 info->array.utime < (int)__le64_to_cpu(bsb.mtime) - 10*60) {
097075b6 4140 if (check_env("MDADM_GROW_ALLOW_OLD")) {
e7b84f9d 4141 pr_err("accepting backup with timestamp %lu "
097075b6
N
4142 "for array with timestamp %lu\n",
4143 (unsigned long)__le64_to_cpu(bsb.mtime),
4144 (unsigned long)info->array.utime);
4145 } else {
676ab312
N
4146 pr_err("too-old timestamp on backup-metadata on %s\n", devname);
4147 pr_err("If you think it is should be safe, try 'export MDADM_GROW_ALLOW_OLD=1'\n");
097075b6
N
4148 continue; /* time stamp is too bad */
4149 }
ea0ebe96 4150 }
353632d9 4151
e9e43ec3 4152 if (bsb.magic[15] == '1') {
10af14c4
N
4153 if (bsb.length == 0)
4154 continue;
e7a71c6b
N
4155 if (info->delta_disks >= 0) {
4156 /* reshape_progress is increasing */
4157 if (__le64_to_cpu(bsb.arraystart)
4158 + __le64_to_cpu(bsb.length)
4159 < info->reshape_progress) {
4160 nonew:
4161 if (verbose)
e7b84f9d 4162 pr_err("backup-metadata found on %s but is not needed\n", devname);
e7a71c6b
N
4163 continue; /* No new data here */
4164 }
4165 } else {
4166 /* reshape_progress is decreasing */
4167 if (__le64_to_cpu(bsb.arraystart) >=
4168 info->reshape_progress)
4169 goto nonew; /* No new data here */
ea0ebe96 4170 }
e9e43ec3 4171 } else {
10af14c4
N
4172 if (bsb.length == 0 && bsb.length2 == 0)
4173 continue;
e7a71c6b
N
4174 if (info->delta_disks >= 0) {
4175 /* reshape_progress is increasing */
4176 if ((__le64_to_cpu(bsb.arraystart)
4177 + __le64_to_cpu(bsb.length)
4178 < info->reshape_progress)
4179 &&
4180 (__le64_to_cpu(bsb.arraystart2)
4181 + __le64_to_cpu(bsb.length2)
4182 < info->reshape_progress))
4183 goto nonew; /* No new data here */
4184 } else {
4185 /* reshape_progress is decreasing */
4186 if (__le64_to_cpu(bsb.arraystart) >=
4187 info->reshape_progress &&
4188 __le64_to_cpu(bsb.arraystart2) >=
4189 info->reshape_progress)
4190 goto nonew; /* No new data here */
4191 }
e9e43ec3 4192 }
ea0ebe96
N
4193 if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0) {
4194 second_fail:
4195 if (verbose)
e7b84f9d
N
4196 pr_err("Failed to verify secondary backup-metadata block on %s\n",
4197 devname);
353632d9 4198 continue; /* Cannot seek */
ea0ebe96 4199 }
2efedc7b 4200 /* There should be a duplicate backup superblock 4k before here */
06b0d786 4201 if (lseek64(fd, -4096, 1) < 0 ||
0155af90 4202 read(fd, &bsb2, sizeof(bsb2)) != sizeof(bsb2))
ea0ebe96 4203 goto second_fail; /* Cannot find leading superblock */
e9e43ec3
N
4204 if (bsb.magic[15] == '1')
4205 bsbsize = offsetof(struct mdp_backup_super, pad1);
4206 else
4207 bsbsize = offsetof(struct mdp_backup_super, pad);
ff94fb86 4208 if (memcmp(&bsb2, &bsb, bsbsize) != 0)
ea0ebe96 4209 goto second_fail; /* Cannot find leading superblock */
2efedc7b 4210
353632d9 4211 /* Now need the data offsets for all devices. */
503975b9 4212 offsets = xmalloc(sizeof(*offsets)*info->array.raid_disks);
353632d9
NB
4213 for(j=0; j<info->array.raid_disks; j++) {
4214 if (fdlist[j] < 0)
4215 continue;
3da92f27 4216 if (st->ss->load_super(st, fdlist[j], NULL))
353632d9
NB
4217 /* FIXME should be this be an error */
4218 continue;
a5d85af7 4219 st->ss->getinfo_super(st, &dinfo, NULL);
3da92f27 4220 st->ss->free_super(st);
14e5b4d7 4221 offsets[j] = dinfo.data_offset * 512;
353632d9
NB
4222 }
4223 printf(Name ": restoring critical section\n");
4224
4225 if (restore_stripes(fdlist, offsets,
4226 info->array.raid_disks,
4227 info->new_chunk,
4228 info->new_level,
4229 info->new_layout,
06b0d786 4230 fd, __le64_to_cpu(bsb.devstart)*512,
92dcdf7c 4231 __le64_to_cpu(bsb.arraystart)*512,
2fcb75ae 4232 __le64_to_cpu(bsb.length)*512, NULL)) {
e9e43ec3 4233 /* didn't succeed, so giveup */
ea0ebe96 4234 if (verbose)
e7b84f9d 4235 pr_err("Error restoring backup from %s\n",
ea0ebe96 4236 devname);
730ae51f 4237 free(offsets);
e9e43ec3
N
4238 return 1;
4239 }
24daa16f 4240
e9e43ec3
N
4241 if (bsb.magic[15] == '2' &&
4242 restore_stripes(fdlist, offsets,
4243 info->array.raid_disks,
4244 info->new_chunk,
4245 info->new_level,
4246 info->new_layout,
4247 fd, __le64_to_cpu(bsb.devstart)*512 +
4248 __le64_to_cpu(bsb.devstart2)*512,
92dcdf7c 4249 __le64_to_cpu(bsb.arraystart2)*512,
2fcb75ae 4250 __le64_to_cpu(bsb.length2)*512, NULL)) {
353632d9 4251 /* didn't succeed, so giveup */
ea0ebe96 4252 if (verbose)
e7b84f9d 4253 pr_err("Error restoring second backup from %s\n",
ea0ebe96 4254 devname);
730ae51f 4255 free(offsets);
2295250a 4256 return 1;
353632d9
NB
4257 }
4258
730ae51f 4259 free(offsets);
e9e43ec3 4260
353632d9
NB
4261 /* Ok, so the data is restored. Let's update those superblocks. */
4262
10af14c4
N
4263 lo = hi = 0;
4264 if (bsb.length) {
4265 lo = __le64_to_cpu(bsb.arraystart);
4266 hi = lo + __le64_to_cpu(bsb.length);
4267 }
4268 if (bsb.magic[15] == '2' && bsb.length2) {
4269 unsigned long long lo1, hi1;
4270 lo1 = __le64_to_cpu(bsb.arraystart2);
4271 hi1 = lo1 + __le64_to_cpu(bsb.length2);
4272 if (lo == hi) {
4273 lo = lo1;
4274 hi = hi1;
4275 } else if (lo < lo1)
4276 hi = hi1;
4277 else
4278 lo = lo1;
4279 }
4280 if (lo < hi &&
4281 (info->reshape_progress < lo ||
4282 info->reshape_progress > hi))
4283 /* backup does not affect reshape_progress*/ ;
4284 else if (info->delta_disks >= 0) {
e9e43ec3
N
4285 info->reshape_progress = __le64_to_cpu(bsb.arraystart) +
4286 __le64_to_cpu(bsb.length);
4287 if (bsb.magic[15] == '2') {
4288 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2) +
4289 __le64_to_cpu(bsb.length2);
4290 if (p2 > info->reshape_progress)
4291 info->reshape_progress = p2;
4292 }
4293 } else {
4294 info->reshape_progress = __le64_to_cpu(bsb.arraystart);
4295 if (bsb.magic[15] == '2') {
4296 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2);
4297 if (p2 < info->reshape_progress)
4298 info->reshape_progress = p2;
4299 }
4300 }
353632d9 4301 for (j=0; j<info->array.raid_disks; j++) {
ca3b6696
N
4302 if (fdlist[j] < 0)
4303 continue;
3da92f27 4304 if (st->ss->load_super(st, fdlist[j], NULL))
353632d9 4305 continue;
a5d85af7 4306 st->ss->getinfo_super(st, &dinfo, NULL);
e9e43ec3 4307 dinfo.reshape_progress = info->reshape_progress;
3da92f27 4308 st->ss->update_super(st, &dinfo,
68c7d6d7
NB
4309 "_reshape_progress",
4310 NULL,0, 0, NULL);
3da92f27
NB
4311 st->ss->store_super(st, fdlist[j]);
4312 st->ss->free_super(st);
353632d9 4313 }
353632d9
NB
4314 return 0;
4315 }
6e9eac4f
NB
4316 /* Didn't find any backup data, try to see if any
4317 * was needed.
4318 */
82f2d6ab
N
4319 if (info->delta_disks < 0) {
4320 /* When shrinking, the critical section is at the end.
4321 * So see if we are before the critical section.
4322 */
4323 unsigned long long first_block;
4324 nstripe = ostripe = 0;
4325 first_block = 0;
4326 while (ostripe >= nstripe) {
4327 ostripe += info->array.chunk_size / 512;
4328 first_block = ostripe * odata;
4329 nstripe = first_block / ndata / (info->new_chunk/512) *
4330 (info->new_chunk/512);
4331 }
4332
4333 if (info->reshape_progress >= first_block)
4334 return 0;
6e9eac4f 4335 }
82f2d6ab
N
4336 if (info->delta_disks > 0) {
4337 /* See if we are beyond the critical section. */
4338 unsigned long long last_block;
4339 nstripe = ostripe = 0;
4340 last_block = 0;
4341 while (nstripe >= ostripe) {
4342 nstripe += info->new_chunk / 512;
4343 last_block = nstripe * ndata;
4344 ostripe = last_block / odata / (info->array.chunk_size/512) *
4345 (info->array.chunk_size/512);
4346 }
6e9eac4f 4347
82f2d6ab
N
4348 if (info->reshape_progress >= last_block)
4349 return 0;
4350 }
6e9eac4f 4351 /* needed to recover critical section! */
ea0ebe96 4352 if (verbose)
e7b84f9d 4353 pr_err("Failed to find backup of critical section\n");
2295250a 4354 return 1;
353632d9 4355}
e9e43ec3 4356
2dddadb0
AK
4357int Grow_continue_command(char *devname, int fd,
4358 char *backup_file, int verbose)
4359{
4360 int ret_val = 0;
4361 struct supertype *st = NULL;
4362 struct mdinfo *content = NULL;
4363 struct mdinfo array;
4364 char *subarray = NULL;
4365 struct mdinfo *cc = NULL;
4366 struct mdstat_ent *mdstat = NULL;
4367 char buf[40];
4368 int cfd = -1;
4369 int fd2 = -1;
4370
4371 dprintf("Grow continue from command line called for %s\n",
4372 devname);
4373
4374 st = super_by_fd(fd, &subarray);
4375 if (!st || !st->ss) {
e7b84f9d
N
4376 pr_err("Unable to determine metadata format for %s\n",
4377 devname);
2dddadb0
AK
4378 return 1;
4379 }
4380 dprintf("Grow continue is run for ");
4381 if (st->ss->external == 0) {
e0ab37a3 4382 int d;
2dddadb0 4383 dprintf("native array (%s)\n", devname);
e0ab37a3 4384 if (ioctl(fd, GET_ARRAY_INFO, &array.array) < 0) {
e7b84f9d 4385 pr_err("%s is not an active md array -"
2dddadb0
AK
4386 " aborting\n", devname);
4387 ret_val = 1;
4388 goto Grow_continue_command_exit;
4389 }
4390 content = &array;
e0ab37a3
N
4391 /* Need to load a superblock.
4392 * FIXME we should really get what we need from
4393 * sysfs
4394 */
4395 for (d = 0; d < MAX_DISKS; d++) {
4396 mdu_disk_info_t disk;
4397 char *dv;
4398 int err;
4399 disk.number = d;
4400 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
4401 continue;
4402 if (disk.major == 0 && disk.minor == 0)
4403 continue;
4404 if ((disk.state & (1 << MD_DISK_ACTIVE)) == 0)
4405 continue;
4406 dv = map_dev(disk.major, disk.minor, 1);
4407 if (!dv)
4408 continue;
4409 fd2 = dev_open(dv, O_RDONLY);
4410 if (fd2 < 0)
4411 continue;
4412 err = st->ss->load_super(st, fd2, NULL);
4413 close(fd2);
4414 if (err)
4415 continue;
4416 break;
4417 }
4418 if (d == MAX_DISKS) {
4419 pr_err("Unable to load metadata for %s\n",
4420 devname);
4421 ret_val = 1;
4422 goto Grow_continue_command_exit;
4423 }
4424 st->ss->getinfo_super(st, content, NULL);
2dddadb0
AK
4425 } else {
4426 int container_dev;
4427
4428 if (subarray) {
4429 dprintf("subarray (%s)\n", subarray);
4430 container_dev = st->container_dev;
4431 cfd = open_dev_excl(st->container_dev);
4432 } else {
4433 container_dev = st->devnum;
4434 close(fd);
4435 cfd = open_dev_excl(st->devnum);
4436 dprintf("container (%i)\n", container_dev);
4437 fd = cfd;
4438 }
4439 if (cfd < 0) {
e7b84f9d 4440 pr_err("Unable to open container "
2dddadb0
AK
4441 "for %s\n", devname);
4442 ret_val = 1;
4443 goto Grow_continue_command_exit;
4444 }
4445 fmt_devname(buf, container_dev);
4446
4447 /* find in container array under reshape
4448 */
4449 ret_val = st->ss->load_container(st, cfd, NULL);
4450 if (ret_val) {
e7b84f9d
N
4451 pr_err("Cannot read superblock for %s\n",
4452 devname);
2dddadb0
AK
4453 ret_val = 1;
4454 goto Grow_continue_command_exit;
4455 }
4456
446894ea 4457 cc = st->ss->container_content(st, subarray);
2dddadb0
AK
4458 for (content = cc; content ; content = content->next) {
4459 char *array;
446894ea 4460 int allow_reshape = 1;
2dddadb0
AK
4461
4462 if (content->reshape_active == 0)
4463 continue;
81219e70
LM
4464 /* The decision about array or container wide
4465 * reshape is taken in Grow_continue based
4466 * content->reshape_active state, therefore we
4467 * need to check_reshape based on
4468 * reshape_active and subarray name
446894ea
N
4469 */
4470 if (content->array.state & (1<<MD_SB_BLOCK_VOLUME))
4471 allow_reshape = 0;
4472 if (content->reshape_active == CONTAINER_RESHAPE &&
4473 (content->array.state
4474 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE)))
4475 allow_reshape = 0;
4476
81219e70 4477 if (!allow_reshape) {
e7b84f9d
N
4478 pr_err("cannot continue reshape of an array"
4479 " in container with unsupported"
4480 " metadata: %s(%s)\n",
4481 devname, buf);
81219e70
LM
4482 ret_val = 1;
4483 goto Grow_continue_command_exit;
4484 }
2dddadb0
AK
4485
4486 array = strchr(content->text_version+1, '/')+1;
4487 mdstat = mdstat_by_subdev(array, container_dev);
4488 if (!mdstat)
4489 continue;
1ca90aa6 4490 if (mdstat->active == 0) {
e7b84f9d 4491 pr_err("Skipping inactive "
1ca90aa6
AK
4492 "array md%i.\n", mdstat->devnum);
4493 free_mdstat(mdstat);
4494 mdstat = NULL;
4495 continue;
4496 }
2dddadb0
AK
4497 break;
4498 }
4499 if (!content) {
e7b84f9d
N
4500 pr_err("Unable to determine reshaped "
4501 "array for %s\n", devname);
2dddadb0
AK
4502 ret_val = 1;
4503 goto Grow_continue_command_exit;
4504 }
4505 fd2 = open_dev(mdstat->devnum);
4506 if (fd2 < 0) {
e7b84f9d 4507 pr_err("cannot open (md%i)\n",
2dddadb0
AK
4508 mdstat->devnum);
4509 ret_val = 1;
4510 goto Grow_continue_command_exit;
4511 }
4512
4513 sysfs_init(content, fd2, mdstat->devnum);
4514
4515 /* start mdmon in case it is not running
4516 */
4517 if (!mdmon_running(container_dev))
4518 start_mdmon(container_dev);
4519 ping_monitor(buf);
4520
4521 if (mdmon_running(container_dev))
4522 st->update_tail = &st->updates;
4523 else {
e7b84f9d 4524 pr_err("No mdmon found. "
2dddadb0
AK
4525 "Grow cannot continue.\n");
4526 ret_val = 1;
4527 goto Grow_continue_command_exit;
4528 }
4529 }
4530
f1fe496b
AK
4531 /* verify that array under reshape is started from
4532 * correct position
4533 */
e0ab37a3 4534 if (verify_reshape_position(content, content->array.level) < 0) {
f1fe496b
AK
4535 ret_val = 1;
4536 goto Grow_continue_command_exit;
4537 }
4538
2dddadb0
AK
4539 /* continue reshape
4540 */
4541 ret_val = Grow_continue(fd, st, content, backup_file, 0);
4542
4543Grow_continue_command_exit:
4544 if (fd2 > -1)
4545 close(fd2);
4546 if (cfd > -1)
4547 close(cfd);
4548 st->ss->free_super(st);
4549 free_mdstat(mdstat);
4550 sysfs_free(cc);
4551 free(subarray);
4552
4553 return ret_val;
4554}
4555
e9e43ec3 4556int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
b76b30e0 4557 char *backup_file, int freeze_reshape)
e9e43ec3 4558{
3bd58dc6
AK
4559 int ret_val = 2;
4560
4561 if (!info->reshape_active)
4562 return ret_val;
864a004f 4563
20a40eca 4564 if (st->ss->external) {
3bd58dc6
AK
4565 char container[40];
4566 int cfd = open_dev(st->container_dev);
3db2fdd8 4567
3bd58dc6
AK
4568 if (cfd < 0)
4569 return 1;
f362d22b 4570
3bd58dc6
AK
4571 fmt_devname(container, st->container_dev);
4572 st->ss->load_container(st, cfd, container);
4573 close(cfd);
9ad6f6e6 4574 ret_val = reshape_container(container, NULL, mdfd,
3bd58dc6
AK
4575 st, info, 0, backup_file,
4576 0, 1, freeze_reshape);
4577 } else
4578 ret_val = reshape_array(NULL, mdfd, "array", st, info, 1,
19ceb16d 4579 NULL, 0ULL, backup_file, 0, 0, 1,
3bd58dc6 4580 freeze_reshape);
f362d22b 4581
3bd58dc6 4582 return ret_val;
e9e43ec3 4583}