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