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