]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Grow.c
Incremental: Fix a merge error in recent patch
[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
81219e70
LM
1361/*
1362 * helper routine to check metadata reshape avalability
1363 * 1. Do not "grow" arrays with volume activation blocked
1364 * 2. do not reshape containers with container reshape blocked
1365 *
1366 * IN:
1367 * subarray - array name or NULL for container wide reshape
1368 * content - md device info from container_content
1369 * OUT:
1370 * 0 - block reshape
1371 */
1372static int check_reshape(char *subarray, struct mdinfo *content)
1373{
1374 char *ep;
1375 unsigned int idx;
1376
1377 if (!subarray) {
1378 if (content->array.state & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE))
1379 return 0;
1380 } else {
1381 /* do not "grow" arrays with volume activation blocked */
1382 idx = strtoul(subarray, &ep, 10);
1383 if (*ep == '\0'
1384 && content->container_member == (int) idx
1385 && (content->array.state & (1<<MD_SB_BLOCK_VOLUME)))
1386 return 0;
1387 }
1388 return 1;
1389}
1390
06b0d786 1391int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
e86c9dd6 1392 long long size,
691a36b7 1393 int level, char *layout_str, int chunksize, int raid_disks,
e2e53a2d 1394 struct mddev_dev *devlist,
ce52f92f 1395 int assume_clean, int force)
e86c9dd6
NB
1396{
1397 /* Make some changes in the shape of an array.
1398 * The kernel must support the change.
7236ee7a
N
1399 *
1400 * There are three different changes. Each can trigger
1401 * a resync or recovery so we freeze that until we have
1402 * requested everything (if kernel supports freezing - 2.6.30).
1403 * The steps are:
1404 * - change size (i.e. component_size)
1405 * - change level
1406 * - change layout/chunksize/ndisks
1407 *
1408 * The last can require a reshape. It is different on different
1409 * levels so we need to check the level before actioning it.
1410 * Some times the level change needs to be requested after the
1411 * reshape (e.g. raid6->raid5, raid5->raid0)
1412 *
e86c9dd6 1413 */
5da9ab98 1414 struct mdu_array_info_s array;
7236ee7a 1415 int rv = 0;
e86c9dd6 1416 struct supertype *st;
4725bc31 1417 char *subarray = NULL;
e86c9dd6 1418
7236ee7a 1419 int frozen;
7236ee7a 1420 int changed = 0;
7bc71196 1421 char *container = NULL;
5da9ab98 1422 char container_buf[20];
7bc71196 1423 int cfd = -1;
e86c9dd6 1424
e2e53a2d
N
1425 struct mddev_dev *dv;
1426 int added_disks;
1427
5da9ab98 1428 struct mdinfo info;
7e0f6979 1429 struct mdinfo *sra;
e86c9dd6
NB
1430
1431 if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) {
1432 fprintf(stderr, Name ": %s is not an active md array - aborting\n",
1433 devname);
1434 return 1;
1435 }
24d40069 1436
9ce510be
N
1437 if (size >= 0 &&
1438 (chunksize || level!= UnSet || layout_str || raid_disks)) {
1439 fprintf(stderr, Name ": cannot change component size at the same time "
1440 "as other changes.\n"
1441 " Change size first, then check data is intact before "
1442 "making other changes.\n");
1443 return 1;
1444 }
1445
24d40069
N
1446 if (raid_disks && raid_disks < array.raid_disks && array.level > 1 &&
1447 get_linux_version() < 2006032 &&
1448 !check_env("MDADM_FORCE_FEWER")) {
1449 fprintf(stderr, Name ": reducing the number of devices is not safe before Linux 2.6.32\n"
1450 " Please use a newer kernel\n");
1451 return 1;
1452 }
7bc71196
DW
1453
1454 st = super_by_fd(fd, &subarray);
1455 if (!st) {
1456 fprintf(stderr, Name ": Unable to determine metadata format for %s\n", devname);
1457 return 1;
1458 }
acab7bb1
N
1459 if (raid_disks > st->max_devs) {
1460 fprintf(stderr, Name ": Cannot increase raid-disks on this array"
1461 " beyond %d\n", st->max_devs);
1462 return 1;
1463 }
7bc71196
DW
1464
1465 /* in the external case we need to check that the requested reshape is
1466 * supported, and perform an initial check that the container holds the
1467 * pre-requisite spare devices (mdmon owns final validation)
1468 */
1469 if (st->ss->external) {
1470 int container_dev;
7f2ba464 1471 int rv;
7bc71196
DW
1472
1473 if (subarray) {
1474 container_dev = st->container_dev;
1475 cfd = open_dev_excl(st->container_dev);
7bc71196
DW
1476 } else {
1477 container_dev = st->devnum;
1478 close(fd);
1479 cfd = open_dev_excl(st->devnum);
1480 fd = cfd;
1481 }
1482 if (cfd < 0) {
1483 fprintf(stderr, Name ": Unable to open container for %s\n",
1484 devname);
7f2ba464 1485 free(subarray);
7bc71196
DW
1486 return 1;
1487 }
1488
5da9ab98
N
1489 fmt_devname(container_buf, container_dev);
1490 container = container_buf;
7bc71196 1491
eb744788
AK
1492 rv = st->ss->load_container(st, cfd, NULL);
1493
7f2ba464 1494 if (rv) {
7bc71196
DW
1495 fprintf(stderr, Name ": Cannot read superblock for %s\n",
1496 devname);
7f2ba464 1497 free(subarray);
7bc71196
DW
1498 return 1;
1499 }
1500
81219e70
LM
1501 /* check if operation is supported for metadata handler */
1502 if (st->ss->container_content) {
1503 struct mdinfo *cc = NULL;
1504 struct mdinfo *content = NULL;
1505
1506 cc = st->ss->container_content(st, subarray);
1507 for (content = cc; content ; content = content->next) {
1508 int allow_reshape;
1509
1510 /* check if reshape is allowed based on metadata
1511 * indications stored in content.array.status
1512 */
1513 allow_reshape = check_reshape(subarray, content);
1514 if (!allow_reshape) {
1515 fprintf(stderr, Name
1516 " cannot reshape arrays in"
1517 " container with unsupported"
1518 " metadata: %s(%s)\n",
1519 devname, container_buf);
1520 sysfs_free(cc);
1521 free(subarray);
1522 return 1;
1523 }
1524 }
1525 sysfs_free(cc);
1526 }
7bc71196
DW
1527 if (mdmon_running(container_dev))
1528 st->update_tail = &st->updates;
e2e53a2d 1529 }
691a36b7 1530
e2e53a2d
N
1531 added_disks = 0;
1532 for (dv = devlist; dv; dv = dv->next)
1533 added_disks++;
691a36b7 1534 if (raid_disks > array.raid_disks &&
e2e53a2d 1535 array.spare_disks +added_disks < (raid_disks - array.raid_disks) &&
691a36b7
N
1536 !force) {
1537 fprintf(stderr,
1538 Name ": Need %d spare%s to avoid degraded array,"
1539 " and only have %d.\n"
1540 " Use --force to over-ride this check.\n",
1541 raid_disks - array.raid_disks,
1542 raid_disks - array.raid_disks == 1 ? "" : "s",
e2e53a2d 1543 array.spare_disks + added_disks);
691a36b7 1544 return 1;
7bc71196
DW
1545 }
1546
3fa436ac
ML
1547 sra = sysfs_read(fd, 0, GET_LEVEL | GET_DISKS | GET_DEVS
1548 | GET_STATE | GET_VERSION);
1549 if (sra) {
7f2ba464 1550 if (st->ss->external && subarray == NULL) {
7bc71196
DW
1551 array.level = LEVEL_CONTAINER;
1552 sra->array.level = LEVEL_CONTAINER;
1553 }
7bc71196 1554 } else {
b526e52d
DW
1555 fprintf(stderr, Name ": failed to read sysfs parameters for %s\n",
1556 devname);
1557 return 1;
1558 }
7f2ba464
DW
1559 frozen = freeze(st);
1560 if (frozen < -1) {
1561 /* freeze() already spewed the reason */
1562 return 1;
1563 } else if (frozen < 0) {
7236ee7a
N
1564 fprintf(stderr, Name ": %s is performing resync/recovery and cannot"
1565 " be reshaped\n", devname);
1566 return 1;
1567 }
1568
1569 /* ========= set size =============== */
1570 if (size >= 0 && (size == 0 || size != array.size)) {
85f10287 1571 long long orig_size = get_component_size(fd)/2;
20a46756 1572 long long min_csize;
d1537ed1 1573 struct mdinfo *mdi;
7bc71196 1574
85f10287
N
1575 if (orig_size == 0)
1576 orig_size = array.size;
1577
41784c88
AK
1578 if (reshape_super(st, size, UnSet, UnSet, 0, 0, UnSet, NULL,
1579 devname, !quiet)) {
7bc71196
DW
1580 rv = 1;
1581 goto release;
1582 }
1583 sync_metadata(st);
d1537ed1
N
1584
1585 /* Update the size of each member device in case
1586 * they have been resized. This will never reduce
1587 * below the current used-size. The "size" attribute
20a46756 1588 * understands '0' to mean 'max'.
d1537ed1 1589 */
20a46756
N
1590 min_csize = 0;
1591 for (mdi = sra->devs; mdi; mdi = mdi->next) {
1592 if (sysfs_set_num(sra, mdi, "size", size) < 0)
1593 break;
1594 if (array.not_persistent == 0 &&
1595 array.major_version == 0 &&
1596 get_linux_version() < 3001000) {
1597 /* Dangerous to allow size to exceed 2TB */
1598 unsigned long long csize;
1599 if (sysfs_get_ll(sra, mdi, "size", &csize) == 0) {
1600 if (csize >= 2ULL*1024*1024*1024)
1601 csize = 2ULL*1024*1024*1024;
1602 if ((min_csize == 0 || (min_csize
1603 > (long long)csize)))
1604 min_csize = csize;
1605 }
1606 }
1607 }
1608 if (min_csize && size > min_csize) {
1609 fprintf(stderr, Name ": Cannot safely make this array "
1610 "use more than 2TB per device on this kernel.\n");
1611 rv = 1;
1612 goto release;
1613 }
1614 if (min_csize && size == 0) {
1615 /* Don't let the kernel choose a size - it will get
1616 * it wrong
1617 */
1618 fprintf(stderr, Name ": Limited v0.90 array to "
1619 "2TB per device\n");
1620 size = min_csize;
1621 }
d1537ed1 1622
7236ee7a
N
1623 array.size = size;
1624 if (array.size != size) {
1625 /* got truncated to 32bit, write to
1626 * component_size instead
1627 */
1628 if (sra)
1629 rv = sysfs_set_num(sra, NULL,
1630 "component_size", size);
1631 else
1632 rv = -1;
1633 } else
1634 rv = ioctl(fd, SET_ARRAY_INFO, &array);
1635 if (rv != 0) {
39bbb392 1636 int err = errno;
7bc71196
DW
1637
1638 /* restore metadata */
1639 if (reshape_super(st, orig_size, UnSet, UnSet, 0, 0,
41784c88 1640 UnSet, NULL, devname, !quiet) == 0)
7bc71196 1641 sync_metadata(st);
7236ee7a 1642 fprintf(stderr, Name ": Cannot set device size for %s: %s\n",
39bbb392
N
1643 devname, strerror(err));
1644 if (err == EBUSY &&
1645 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
1646 fprintf(stderr, " Bitmap must be removed before size can be changed\n");
7236ee7a
N
1647 rv = 1;
1648 goto release;
1649 }
ce52f92f 1650 if (assume_clean) {
6cbf8fb8 1651 /* This will fail on kernels newer than 3.0 unless
ce52f92f
N
1652 * a backport has been arranged.
1653 */
1654 if (sra == NULL ||
1655 sysfs_set_str(sra, NULL, "resync_start", "none") < 0)
1656 fprintf(stderr, Name ": --assume-clean not support with --grow on this kernel\n");
1657 }
7236ee7a 1658 ioctl(fd, GET_ARRAY_INFO, &array);
be1cabbd 1659 size = get_component_size(fd)/2;
f98841b3
N
1660 if (size == 0)
1661 size = array.size;
85f10287
N
1662 if (!quiet) {
1663 if (size == orig_size)
1664 fprintf(stderr, Name ": component size of %s "
1665 "unchanged at %lluK\n",
1666 devname, size);
1667 else
1668 fprintf(stderr, Name ": component size of %s "
1669 "has been set to %lluK\n",
1670 devname, size);
1671 }
7236ee7a 1672 changed = 1;
7bc71196 1673 } else if (array.level != LEVEL_CONTAINER) {
be1cabbd 1674 size = get_component_size(fd)/2;
f98841b3
N
1675 if (size == 0)
1676 size = array.size;
7236ee7a
N
1677 }
1678
d515d27f
N
1679 /* See if there is anything else to do */
1680 if ((level == UnSet || level == array.level) &&
1681 (layout_str == NULL) &&
1682 (chunksize == 0 || chunksize == array.chunk_size) &&
1683 (raid_disks == 0 || raid_disks == array.raid_disks)) {
1684 /* Nothing more to do */
1685 if (!changed && !quiet)
1686 fprintf(stderr, Name ": %s: no change requested\n",
1687 devname);
1688 goto release;
1689 }
1690
dfe77a9e 1691 /* ========= check for Raid10/Raid1 -> Raid0 conversion ===============
5da9ab98 1692 * current implementation assumes that following conditions must be met:
dfe77a9e
KW
1693 * - RAID10:
1694 * - far_copies == 1
1695 * - near_copies == 2
62a48395 1696 */
dfe77a9e
KW
1697 if ((level == 0 && array.level == 10 && sra &&
1698 array.layout == ((1 << 8) + 2) && !(array.raid_disks & 1)) ||
1699 (level == 0 && array.level == 1 && sra)) {
62a48395 1700 int err;
dfe77a9e 1701 err = remove_disks_for_takeover(st, sra, array.layout);
62a48395
AK
1702 if (err) {
1703 dprintf(Name": Array cannot be reshaped\n");
62a48395
AK
1704 if (cfd > -1)
1705 close(cfd);
5da9ab98
N
1706 rv = 1;
1707 goto release;
7236ee7a 1708 }
fde139b9
N
1709 /* Make sure mdmon has seen the device removal
1710 * and updated metadata before we continue with
1711 * level change
1712 */
1713 if (container)
1714 ping_monitor(container);
7236ee7a
N
1715 }
1716
8ff6d094 1717 memset(&info, 0, sizeof(info));
5da9ab98 1718 info.array = array;
7443ee81 1719 sysfs_init(&info, fd, NoMdDev);
3fa436ac 1720 strcpy(info.text_version, sra->text_version);
7443ee81 1721 info.component_size = size*2;
5da9ab98
N
1722 info.new_level = level;
1723 info.new_chunk = chunksize * 1024;
eff4954d
N
1724 if (info.array.level == LEVEL_CONTAINER) {
1725 info.delta_disks = UnSet;
1726 info.array.raid_disks = raid_disks;
1727 } else if (raid_disks)
5da9ab98
N
1728 info.delta_disks = raid_disks - info.array.raid_disks;
1729 else
1730 info.delta_disks = UnSet;
1731 if (layout_str == NULL) {
1732 info.new_layout = UnSet;
1733 if (info.array.level == 6 &&
1734 (info.new_level == 6 || info.new_level == UnSet) &&
1735 info.array.layout >= 16) {
7236ee7a 1736 fprintf(stderr, Name
5da9ab98
N
1737 ": %s has a non-standard layout. If you"
1738 " wish to preserve this\n"
1739 " during the reshape, please specify"
1740 " --layout=preserve\n"
1741 " If you want to change it, specify a"
1742 " layout or use --layout=normalise\n",
7236ee7a
N
1743 devname);
1744 rv = 1;
1745 goto release;
1746 }
5da9ab98
N
1747 } else if (strcmp(layout_str, "normalise") == 0 ||
1748 strcmp(layout_str, "normalize") == 0) {
1749 /* If we have a -6 RAID6 layout, remove the '-6'. */
1750 info.new_layout = UnSet;
1751 if (info.array.level == 6 && info.new_level == UnSet) {
1752 char l[40], *h;
1753 strcpy(l, map_num(r6layout, info.array.layout));
1754 h = strrchr(l, '-');
1755 if (h && strcmp(h, "-6") == 0) {
1756 *h = 0;
1757 info.new_layout = map_name(r6layout, l);
7236ee7a
N
1758 }
1759 }
5da9ab98
N
1760 } else if (strcmp(layout_str, "preserve") == 0) {
1761 info.new_layout = UnSet;
1762 } else {
1763 int l = info.new_level;
1764 if (l == UnSet)
1765 l = info.array.level;
1766 switch (l) {
1767 case 5:
1768 info.new_layout = map_name(r5layout, layout_str);
1769 break;
1770 case 6:
1771 info.new_layout = map_name(r6layout, layout_str);
1772 break;
1773 case 10:
1774 info.new_layout = parse_layout_10(layout_str);
1775 break;
1776 case LEVEL_FAULTY:
1777 info.new_layout = parse_layout_faulty(layout_str);
1778 break;
1779 default:
1780 fprintf(stderr, Name ": layout not meaningful"
1781 " with this level\n");
7bc71196
DW
1782 rv = 1;
1783 goto release;
1784 }
5da9ab98
N
1785 if (info.new_layout == UnSet) {
1786 fprintf(stderr, Name ": layout %s not understood"
1787 " for this level\n",
1788 layout_str);
1789 rv = 1;
1790 goto release;
7bc71196 1791 }
7236ee7a
N
1792 }
1793
907ea753
N
1794 if (array.level == LEVEL_FAULTY) {
1795 if (level != UnSet && level != array.level) {
1796 fprintf(stderr, Name ": cannot change level of Faulty device\n");
1797 rv =1 ;
1798 }
1799 if (chunksize) {
1800 fprintf(stderr, Name ": cannot set chunksize of Faulty device\n");
1801 rv =1 ;
1802 }
1803 if (raid_disks && raid_disks != 1) {
1804 fprintf(stderr, Name ": cannot set raid_disks of Faulty device\n");
1805 rv =1 ;
1806 }
1807 if (layout_str) {
1808 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
1809 dprintf("Cannot get array information.\n");
1810 goto release;
1811 }
1812 array.layout = info.new_layout;
1813 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
1814 fprintf(stderr, Name ": failed to set new layout\n");
1815 rv = 1;
1816 } else if (!quiet)
1817 printf("layout for %s set to %d\n",
1818 devname, array.layout);
1819 }
1820 } else if (array.level == LEVEL_CONTAINER) {
5da9ab98
N
1821 /* This change is to be applied to every array in the
1822 * container. This is only needed when the metadata imposes
1823 * restraints of the various arrays in the container.
1824 * Currently we only know that IMSM requires all arrays
1825 * to have the same number of devices so changing the
1826 * number of devices (On-Line Capacity Expansion) must be
1827 * performed at the level of the container
1828 */
9ad6f6e6 1829 rv = reshape_container(container, devname, -1, st, &info,
b76b30e0 1830 force, backup_file, quiet, 0, 0);
9202b8eb 1831 frozen = 0;
5da9ab98 1832 } else {
08f9e34b
AK
1833 /* get spare devices from external metadata
1834 */
1835 if (st->ss->external) {
1836 struct mdinfo *info2;
1837
1838 info2 = st->ss->container_content(st, subarray);
1839 if (info2) {
1840 info.array.spare_disks =
1841 info2->array.spare_disks;
1842 sysfs_free(info2);
1843 }
1844 }
1845
5da9ab98
N
1846 /* Impose these changes on a single array. First
1847 * check that the metadata is OK with the change. */
7bc71196 1848
5da9ab98
N
1849 if (reshape_super(st, info.component_size, info.new_level,
1850 info.new_layout, info.new_chunk,
41784c88 1851 info.array.raid_disks, info.delta_disks,
5da9ab98 1852 backup_file, devname, quiet)) {
7bc71196
DW
1853 rv = 1;
1854 goto release;
1855 }
5da9ab98
N
1856 sync_metadata(st);
1857 rv = reshape_array(container, fd, devname, st, &info, force,
b76b30e0 1858 devlist, backup_file, quiet, 0, 0, 0);
9202b8eb 1859 frozen = 0;
5da9ab98
N
1860 }
1861release:
9202b8eb
N
1862 if (frozen > 0)
1863 unfreeze(st);
5da9ab98
N
1864 return rv;
1865}
7bc71196 1866
5da9ab98
N
1867static int reshape_array(char *container, int fd, char *devname,
1868 struct supertype *st, struct mdinfo *info,
e2e53a2d 1869 int force, struct mddev_dev *devlist,
a93f87ee 1870 char *backup_file, int quiet, int forked,
b76b30e0 1871 int restart, int freeze_reshape)
5da9ab98
N
1872{
1873 struct reshape reshape;
1874 int spares_needed;
1875 char *msg;
1876 int orig_level = UnSet;
1877 int disks, odisks;
7bc71196 1878
5da9ab98
N
1879 struct mdu_array_info_s array;
1880 char *c;
e86c9dd6 1881
e2e53a2d
N
1882 struct mddev_dev *dv;
1883 int added_disks;
1884
d152f53e
JS
1885 int *fdlist = NULL;
1886 unsigned long long *offsets = NULL;
5da9ab98
N
1887 int d;
1888 int nrdisks;
1889 int err;
da8100ca 1890 unsigned long blocks;
5da9ab98
N
1891 unsigned long cache;
1892 unsigned long long array_size;
1893 int done;
cf6ac177 1894 struct mdinfo *sra = NULL;
7bc71196 1895
9468aeac
N
1896 /* when reshaping a RAID0, the component_size might be zero.
1897 * So try to fix that up.
1898 */
1899 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
1900 dprintf("Cannot get array information.\n");
1901 goto release;
1902 }
1903 if (array.level == 0 && info->component_size == 0) {
1904 get_dev_size(fd, NULL, &array_size);
1905 info->component_size = array_size / array.raid_disks;
1906 }
1907
3cb2aed2
N
1908 if (info->reshape_active) {
1909 int new_level = info->new_level;
1910 info->new_level = UnSet;
178b8f35 1911 info->array.raid_disks -= info->delta_disks;
3cb2aed2
N
1912 msg = analyse_change(info, &reshape);
1913 info->new_level = new_level;
178b8f35 1914 info->array.raid_disks += info->delta_disks;
fcdfb814
AK
1915 if (!restart)
1916 /* Make sure the array isn't read-only */
1917 ioctl(fd, RESTART_ARRAY_RW, 0);
3cb2aed2
N
1918 } else
1919 msg = analyse_change(info, &reshape);
5da9ab98
N
1920 if (msg) {
1921 fprintf(stderr, Name ": %s\n", msg);
631d7405 1922 goto release;
5da9ab98 1923 }
817ed7d6
N
1924 if (restart &&
1925 (reshape.level != info->array.level ||
1926 reshape.before.layout != info->array.layout ||
384e9be1
AK
1927 reshape.before.data_disks + reshape.parity
1928 != info->array.raid_disks - info->delta_disks)) {
20a40eca
N
1929 fprintf(stderr, Name ": reshape info is not in native format -"
1930 " cannot continue.\n");
1931 goto release;
1932 }
a93f87ee
N
1933
1934 if (restart) {
1935 /* reshape already started. just skip to monitoring the reshape */
1936 if (reshape.backup_blocks == 0)
1937 return 0;
1938 goto started;
1939 }
a9c3e78f
AK
1940 /* The container is frozen but the array may not be.
1941 * So freeze the array so spares don't get put to the wrong use
1942 * FIXME there should probably be a cleaner separation between
1943 * freeze_array and freeze_container.
1944 */
1945 sysfs_freeze_array(info);
e06c4e59 1946 /* Check we have enough spares to not be degraded */
e2e53a2d
N
1947 added_disks = 0;
1948 for (dv = devlist; dv ; dv=dv->next)
1949 added_disks++;
5da9ab98
N
1950 spares_needed = max(reshape.before.data_disks,
1951 reshape.after.data_disks)
1952 + reshape.parity - array.raid_disks;
7bc71196 1953
88c1a083 1954 if (!force &&
e2e53a2d
N
1955 info->new_level > 1 && info->array.level > 1 &&
1956 spares_needed > info->array.spare_disks + added_disks) {
5da9ab98
N
1957 fprintf(stderr,
1958 Name ": Need %d spare%s to avoid degraded array,"
1959 " and only have %d.\n"
1960 " Use --force to over-ride this check.\n",
1961 spares_needed,
1962 spares_needed == 1 ? "" : "s",
e2e53a2d 1963 info->array.spare_disks + added_disks);
631d7405 1964 goto release;
7bc71196 1965 }
e06c4e59
N
1966 /* Check we have enough spares to not fail */
1967 spares_needed = max(reshape.before.data_disks,
1968 reshape.after.data_disks)
1969 - array.raid_disks;
1970 if ((info->new_level > 1 || info->new_level == 0) &&
e2e53a2d 1971 spares_needed > info->array.spare_disks +added_disks) {
e06c4e59
N
1972 fprintf(stderr,
1973 Name ": Need %d spare%s to create working array,"
1974 " and only have %d.\n",
1975 spares_needed,
1976 spares_needed == 1 ? "" : "s",
e2e53a2d 1977 info->array.spare_disks + added_disks);
e06c4e59
N
1978 goto release;
1979 }
e86c9dd6 1980
eae35f5c 1981 if (reshape.level != array.level) {
5da9ab98
N
1982 char *c = map_num(pers, reshape.level);
1983 int err;
1984 if (c == NULL)
631d7405 1985 goto release;
e86c9dd6 1986
5da9ab98
N
1987 err = sysfs_set_str(info, NULL, "level", c);
1988 if (err) {
1989 err = errno;
1990 fprintf(stderr, Name ": %s: could not set level to %s\n",
1991 devname, c);
1992 if (err == EBUSY &&
1993 (info->array.state & (1<<MD_SB_BITMAP_PRESENT)))
1994 fprintf(stderr, " Bitmap must be removed"
1995 " before level can be changed\n");
631d7405 1996 goto release;
19678e53 1997 }
5da9ab98 1998 if (!quiet)
b6b95155 1999 fprintf(stderr, Name ": level of %s changed to %s\n",
5da9ab98 2000 devname, c);
eae35f5c 2001 orig_level = array.level;
3cd4e7c4 2002 sysfs_freeze_array(info);
e86c9dd6 2003
16d4d84e
AK
2004 if (reshape.level > 0 && st->ss->external) {
2005 /* make sure mdmon is aware of the new level */
2006 if (!mdmon_running(st->container_dev))
2007 start_mdmon(st->container_dev);
2008 ping_monitor(container);
e919fb0a
AK
2009 if (mdmon_running(st->container_dev) &&
2010 st->update_tail == NULL)
2011 st->update_tail = &st->updates;
16d4d84e 2012 }
5da9ab98 2013 }
5da9ab98
N
2014 /* ->reshape_super might have chosen some spares from the
2015 * container that it wants to be part of the new array.
2016 * We can collect them with ->container_content and give
2017 * them to the kernel.
2018 */
2019 if (st->ss->reshape_super && st->ss->container_content) {
2020 char *subarray = strchr(info->text_version+1, '/')+1;
2021 struct mdinfo *info2 =
2022 st->ss->container_content(st, subarray);
2023 struct mdinfo *d;
2024
2dd0d697
AK
2025 if (info2) {
2026 sysfs_init(info2, fd, st->devnum);
0d5ac3c6
N
2027 /* When increasing number of devices, we need to set
2028 * new raid_disks before adding these, or they might
2029 * be rejected.
2030 */
2031 if (reshape.backup_blocks &&
2032 reshape.after.data_disks > reshape.before.data_disks)
2033 subarray_set_num(container, info2, "raid_disks",
2034 reshape.after.data_disks +
2035 reshape.parity);
5da9ab98
N
2036 for (d = info2->devs; d; d = d->next) {
2037 if (d->disk.state == 0 &&
2038 d->disk.raid_disk >= 0) {
2039 /* This is a spare that wants to
2040 * be part of the array.
2041 */
2042 add_disk(fd, st, info2, d);
2043 }
9860f271 2044 }
2dd0d697
AK
2045 sysfs_free(info2);
2046 }
5da9ab98 2047 }
e2e53a2d
N
2048 /* We might have been given some devices to add to the
2049 * array. Now that the array has been changed to the right
2050 * level and frozen, we can safely add them.
2051 */
2052 if (devlist)
2053 Manage_subdevs(devname, fd, devlist, !quiet,
11b391ec 2054 0,NULL, 0);
1686dc25 2055
b4f8e38b 2056 if (reshape.backup_blocks == 0) {
5da9ab98
N
2057 /* No restriping needed, but we might need to impose
2058 * some more changes: layout, raid_disks, chunk_size
e86c9dd6 2059 */
24aebf3a
KW
2060 /* read current array info */
2061 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
83732c28 2062 dprintf("Cannot get array information.\n");
24aebf3a
KW
2063 goto release;
2064 }
2065 /* compare current array info with new values and if
2066 * it is different update them to new */
5da9ab98 2067 if (info->new_layout != UnSet &&
24aebf3a
KW
2068 info->new_layout != array.layout) {
2069 array.layout = info->new_layout;
2070 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
5da9ab98 2071 fprintf(stderr, Name ": failed to set new layout\n");
631d7405 2072 goto release;
5da9ab98
N
2073 } else if (!quiet)
2074 printf("layout for %s set to %d\n",
24aebf3a 2075 devname, array.layout);
5da9ab98
N
2076 }
2077 if (info->delta_disks != UnSet &&
24aebf3a
KW
2078 info->delta_disks != 0 &&
2079 array.raid_disks != (info->array.raid_disks + info->delta_disks)) {
2080 array.raid_disks += info->delta_disks;
2081 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
5da9ab98 2082 fprintf(stderr, Name ": failed to set raid disks\n");
631d7405 2083 goto release;
24aebf3a 2084 } else if (!quiet) {
5da9ab98 2085 printf("raid_disks for %s set to %d\n",
24aebf3a
KW
2086 devname, array.raid_disks);
2087 }
5da9ab98
N
2088 }
2089 if (info->new_chunk != 0 &&
24aebf3a 2090 info->new_chunk != array.chunk_size) {
5da9ab98
N
2091 if (sysfs_set_num(info, NULL,
2092 "chunk_size", info->new_chunk) != 0) {
2093 fprintf(stderr, Name ": failed to set chunk size\n");
631d7405 2094 goto release;
5da9ab98
N
2095 } else if (!quiet)
2096 printf("chunk size for %s set to %d\n",
24aebf3a 2097 devname, array.chunk_size);
4725bc31 2098 }
e35b189b 2099 unfreeze(st);
631d7405 2100 return 0;
5da9ab98 2101 }
19678e53 2102
5da9ab98
N
2103 /*
2104 * There are three possibilities.
2105 * 1/ The array will shrink.
2106 * We need to ensure the reshape will pause before reaching
2107 * the 'critical section'. We also need to fork and wait for
2108 * that to happen. When it does we
2109 * suspend/backup/complete/unfreeze
2110 *
2111 * 2/ The array will not change size.
2112 * This requires that we keep a backup of a sliding window
2113 * so that we can restore data after a crash. So we need
2114 * to fork and monitor progress.
2115 * In future we will allow the data_offset to change, so
2116 * a sliding backup becomes unnecessary.
2117 *
2118 * 3/ The array will grow. This is relatively easy.
2119 * However the kernel's restripe routines will cheerfully
2120 * overwrite some early data before it is safe. So we
2121 * need to make a backup of the early parts of the array
2122 * and be ready to restore it if rebuild aborts very early.
2123 * For externally managed metadata, we still need a forked
2124 * child to monitor the reshape and suspend IO over the region
2125 * that is being reshaped.
2126 *
2127 * We backup data by writing it to one spare, or to a
2128 * file which was given on command line.
2129 *
2130 * In each case, we first make sure that storage is available
2131 * for the required backup.
2132 * Then we:
2133 * - request the shape change.
2134 * - fork to handle backup etc.
2135 */
a93f87ee 2136started:
5da9ab98
N
2137 /* Check that we can hold all the data */
2138 get_dev_size(fd, NULL, &array_size);
2139 if (reshape.new_size < (array_size/512)) {
2140 fprintf(stderr,
2141 Name ": this change will reduce the size of the array.\n"
2142 " use --grow --array-size first to truncate array.\n"
2143 " e.g. mdadm --grow %s --array-size %llu\n",
2144 devname, reshape.new_size/2);
5da9ab98
N
2145 goto release;
2146 }
7236ee7a 2147
5da9ab98 2148 sra = sysfs_read(fd, 0,
3656edcd 2149 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK|
5da9ab98 2150 GET_CACHE);
5da9ab98
N
2151 if (!sra) {
2152 fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n",
2153 devname);
5da9ab98
N
2154 goto release;
2155 }
7236ee7a 2156
5da9ab98
N
2157 /* Decide how many blocks (sectors) for a reshape
2158 * unit. The number we have so far is just a minimum
2159 */
b4f8e38b 2160 blocks = reshape.backup_blocks;
5da9ab98
N
2161 if (reshape.before.data_disks ==
2162 reshape.after.data_disks) {
2163 /* Make 'blocks' bigger for better throughput, but
2164 * not so big that we reject it below.
2165 * Try for 16 megabytes
2166 */
2167 while (blocks * 32 < sra->component_size &&
2168 blocks < 16*1024*2)
2169 blocks *= 2;
2170 } else
2171 fprintf(stderr, Name ": Need to backup %luK of critical "
2172 "section..\n", blocks/2);
2173
2174 if (blocks >= sra->component_size/2) {
2175 fprintf(stderr, Name ": %s: Something wrong"
2176 " - reshape aborted\n",
2177 devname);
5da9ab98
N
2178 goto release;
2179 }
7236ee7a 2180
5da9ab98
N
2181 /* Now we need to open all these devices so we can read/write.
2182 */
b8b286a6
N
2183 nrdisks = max(reshape.before.data_disks,
2184 reshape.after.data_disks) + reshape.parity
2185 + sra->array.spare_disks;
5da9ab98
N
2186 fdlist = malloc((1+nrdisks) * sizeof(int));
2187 offsets = malloc((1+nrdisks) * sizeof(offsets[0]));
2188 if (!fdlist || !offsets) {
2189 fprintf(stderr, Name ": malloc failed: grow aborted\n");
5da9ab98
N
2190 goto release;
2191 }
e380d3be 2192
b8b286a6
N
2193 odisks = reshape.before.data_disks + reshape.parity;
2194 d = reshape_prepare_fdlist(devname, sra, odisks,
5da9ab98
N
2195 nrdisks, blocks, backup_file,
2196 fdlist, offsets);
2197 if (d < 0) {
5da9ab98
N
2198 goto release;
2199 }
13c37ad3
AK
2200 if ((st->ss->manage_reshape == NULL) ||
2201 (st->ss->recover_backup == NULL)) {
2202 if (backup_file == NULL) {
2203 if (reshape.after.data_disks <=
2204 reshape.before.data_disks) {
2205 fprintf(stderr, Name ": %s: Cannot grow - "
2206 "need backup-file\n", devname);
2207 goto release;
2208 } else if (sra->array.spare_disks == 0) {
2209 fprintf(stderr, Name ": %s: Cannot grow - "
2210 "need a spare or backup-file to backup "
2211 "critical section\n", devname);
2212 goto release;
2213 }
2214 } else {
2215 if (!reshape_open_backup_file(backup_file, fd, devname,
2216 (signed)blocks,
2217 fdlist+d, offsets+d,
2218 restart)) {
2219 goto release;
2220 }
2221 d++;
e86c9dd6 2222 }
5da9ab98 2223 }
7236ee7a 2224
5da9ab98
N
2225 /* lastly, check that the internal stripe cache is
2226 * large enough, or it won't work.
2227 * It must hold at least 4 stripes of the larger
2228 * chunk size
2229 */
2230 cache = max(info->array.chunk_size, info->new_chunk);
2231 cache *= 4; /* 4 stripes minimum */
2232 cache /= 512; /* convert to sectors */
2233 disks = min(reshape.before.data_disks, reshape.after.data_disks);
2234 /* make sure there is room for 'blocks' with a bit to spare */
2235 if (cache < 16 + blocks / disks)
2236 cache = 16 + blocks / disks;
2237 cache /= (4096/512); /* Covert from sectors to pages */
2238
2239 if (sra->cache_size < cache)
2240 subarray_set_num(container, sra, "stripe_cache_size",
2241 cache+1);
2242
2243 /* Right, everything seems fine. Let's kick things off.
2244 * If only changing raid_disks, use ioctl, else use
2245 * sysfs.
2246 */
2247 sync_metadata(st);
2248
b4f8e38b 2249 sra->new_chunk = info->new_chunk;
ddee071d 2250
20a40eca 2251 if (restart)
ddee071d 2252 sra->reshape_progress = info->reshape_progress;
f897078e
N
2253 else {
2254 sra->reshape_progress = 0;
2255 if (reshape.after.data_disks < reshape.before.data_disks)
2256 /* start from the end of the new array */
2257 sra->reshape_progress = (sra->component_size
2258 * reshape.after.data_disks);
2259 }
2260
2261 if (info->array.chunk_size == info->new_chunk &&
7477d513
AK
2262 reshape.before.layout == reshape.after.layout &&
2263 st->ss->external == 0) {
f897078e 2264 /* use SET_ARRAY_INFO but only if reshape hasn't started */
6ef421be 2265 ioctl(fd, GET_ARRAY_INFO, &array);
5da9ab98 2266 array.raid_disks = reshape.after.data_disks + reshape.parity;
20a40eca 2267 if (!restart &&
f897078e 2268 ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
5da9ab98 2269 int err = errno;
631d7405 2270
5da9ab98
N
2271 fprintf(stderr,
2272 Name ": Cannot set device shape for %s: %s\n",
2273 devname, strerror(errno));
7bc71196 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");
b5420ef3 2280
5da9ab98
N
2281 goto release;
2282 }
20a40eca 2283 } else if (!restart) {
5da9ab98 2284 /* set them all just in case some old 'new_*' value
f897078e 2285 * persists from some earlier problem.
7236ee7a 2286 */
631d7405 2287 int err = 0;
5da9ab98 2288 if (sysfs_set_num(sra, NULL, "chunk_size", info->new_chunk) < 0)
631d7405
N
2289 err = errno;
2290 if (!err && sysfs_set_num(sra, NULL, "layout",
5da9ab98 2291 reshape.after.layout) < 0)
631d7405
N
2292 err = errno;
2293 if (!err && subarray_set_num(container, sra, "raid_disks",
5da9ab98
N
2294 reshape.after.data_disks +
2295 reshape.parity) < 0)
631d7405
N
2296 err = errno;
2297 if (err) {
5da9ab98
N
2298 fprintf(stderr, Name ": Cannot set device shape for %s\n",
2299 devname);
7236ee7a 2300
5da9ab98
N
2301 if (err == EBUSY &&
2302 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
2303 fprintf(stderr,
2304 " Bitmap must be removed before"
2305 " shape can be changed\n");
2306 goto release;
e86c9dd6 2307 }
5da9ab98 2308 }
e86c9dd6 2309
6937e6d2
AK
2310 err = start_reshape(sra, restart,
2311 info->array.raid_disks - reshape.parity);
fab32c97 2312 if (err) {
20a40eca
N
2313 fprintf(stderr,
2314 Name ": Cannot %s reshape for %s\n",
2315 restart ? "continue" : "start",
fab32c97
AK
2316 devname);
2317 goto release;
2318 }
a93f87ee
N
2319 if (restart)
2320 sysfs_set_str(sra, NULL, "array_state", "active");
b76b30e0
AK
2321 if (freeze_reshape) {
2322 free(fdlist);
2323 free(offsets);
2324 sysfs_free(sra);
2325 fprintf(stderr, Name ": Reshape has to be continued from"
2326 " location %llu when root fileststem has been mounted\n",
2327 sra->reshape_progress);
2328 return 1;
2329 }
5da9ab98
N
2330
2331 /* Now we just need to kick off the reshape and watch, while
2332 * handling backups of the data...
2333 * This is all done by a forked background process.
2334 */
2335 switch(forked ? 0 : fork()) {
6b2d630c
N
2336 case -1:
2337 fprintf(stderr, Name ": Cannot run child to monitor reshape: %s\n",
2338 strerror(errno));
6b2d630c 2339 abort_reshape(sra);
631d7405 2340 goto release;
6b2d630c 2341 default:
d152f53e
JS
2342 free(fdlist);
2343 free(offsets);
2344 sysfs_free(sra);
6b2d630c 2345 return 0;
5da9ab98 2346 case 0:
cc700db3 2347 map_fork();
6b2d630c
N
2348 break;
2349 }
5da9ab98 2350
6b2d630c
N
2351 close(fd);
2352 if (check_env("MDADM_GROW_VERIFY"))
2353 fd = open(devname, O_RDONLY | O_DIRECT);
2354 else
2355 fd = -1;
2356 mlockall(MCL_FUTURE);
5da9ab98 2357
6b2d630c
N
2358 if (st->ss->external) {
2359 /* metadata handler takes it from here */
2360 done = st->ss->manage_reshape(
2361 fd, sra, &reshape, st, blocks,
2362 fdlist, offsets,
2363 d - odisks, fdlist+odisks,
2364 offsets+odisks);
2365 } else
2366 done = child_monitor(
2367 fd, sra, &reshape, st, blocks,
2368 fdlist, offsets,
2369 d - odisks, fdlist+odisks,
2370 offsets+odisks);
2371
d152f53e
JS
2372 free(fdlist);
2373 free(offsets);
2374
6b2d630c
N
2375 if (backup_file && done)
2376 unlink(backup_file);
2377 if (!done) {
2378 abort_reshape(sra);
2379 goto out;
2380 }
1cbc5680
N
2381
2382 if (!st->ss->external &&
2383 !(reshape.before.data_disks != reshape.after.data_disks
2384 && info->custom_array_size) &&
2385 info->new_level == reshape.level &&
2386 !forked) {
2387 /* no need to wait for the reshape to finish as
2388 * there is nothing more to do.
2389 */
d152f53e 2390 sysfs_free(sra);
1cbc5680
N
2391 exit(0);
2392 }
2393 wait_reshape(sra);
2394
2395 if (st->ss->external) {
2396 /* Re-load the metadata as much could have changed */
2397 int cfd = open_dev(st->container_dev);
2398 if (cfd >= 0) {
2399 ping_monitor(container);
2400 st->ss->free_super(st);
2401 st->ss->load_container(st, cfd, container);
2402 close(cfd);
2403 }
2404 }
2405
6b2d630c
N
2406 /* set new array size if required customer_array_size is used
2407 * by this metadata.
2408 */
2409 if (reshape.before.data_disks !=
2410 reshape.after.data_disks &&
2411 info->custom_array_size) {
2412 struct mdinfo *info2;
2413 char *subarray = strchr(info->text_version+1, '/')+1;
582496b2 2414
6b2d630c
N
2415 info2 = st->ss->container_content(st, subarray);
2416 if (info2) {
2417 unsigned long long current_size = 0;
2418 unsigned long long new_size =
2419 info2->custom_array_size/2;
2420
2421 if (sysfs_get_ll(sra,
2422 NULL,
2423 "array_size",
2424 &current_size) == 0 &&
2425 new_size > current_size) {
2426 if (sysfs_set_num(sra, NULL,
2427 "array_size", new_size)
2428 < 0)
2429 dprintf("Error: Cannot"
2430 " set array size");
2431 else
2432 dprintf("Array size "
2433 "changed");
2434 dprintf(" from %llu to %llu.\n",
2435 current_size, new_size);
582496b2 2436 }
6b2d630c 2437 sysfs_free(info2);
582496b2 2438 }
6b2d630c 2439 }
582496b2 2440
6b2d630c 2441 if (info->new_level != reshape.level) {
582496b2 2442
6b2d630c 2443 c = map_num(pers, info->new_level);
1cbc5680
N
2444 if (c) {
2445 err = sysfs_set_str(sra, NULL, "level", c);
2446 if (err)
2447 fprintf(stderr, Name\
2448 ": %s: could not set level "
2449 "to %s\n", devname, c);
2450 }
e919fb0a
AK
2451 if (info->new_level == 0)
2452 st->update_tail = NULL;
7236ee7a 2453 }
6b2d630c 2454out:
d152f53e 2455 sysfs_free(sra);
6b2d630c
N
2456 if (forked)
2457 return 0;
e84f2c00 2458 unfreeze(st);
6b2d630c 2459 exit(0);
e86c9dd6 2460
6b2d630c 2461release:
d152f53e
JS
2462 free(fdlist);
2463 free(offsets);
1cbc5680 2464 if (orig_level != UnSet && sra) {
7236ee7a
N
2465 c = map_num(pers, orig_level);
2466 if (c && sysfs_set_str(sra, NULL, "level", c) == 0)
2467 fprintf(stderr, Name ": aborting level change\n");
2468 }
d152f53e 2469 sysfs_free(sra);
9202b8eb
N
2470 if (!forked)
2471 unfreeze(st);
631d7405 2472 return 1;
7236ee7a 2473}
e86c9dd6 2474
9ad6f6e6
AK
2475/* mdfd handle is passed to be closed in child process (after fork).
2476 */
493f5dd6 2477int reshape_container(char *container, char *devname,
9ad6f6e6 2478 int mdfd,
5da9ab98
N
2479 struct supertype *st,
2480 struct mdinfo *info,
2481 int force,
2482 char *backup_file,
b76b30e0 2483 int quiet, int restart, int freeze_reshape)
5da9ab98 2484{
1bb174ba 2485 struct mdinfo *cc = NULL;
bcc9e9ed 2486 int rv = restart;
1bb174ba 2487
d8eb27f7
KA
2488 /* component_size is not meaningful for a container,
2489 * so pass '-1' meaning 'no change'
2490 */
493f5dd6
N
2491 if (!restart &&
2492 reshape_super(st, -1, info->new_level,
5da9ab98 2493 info->new_layout, info->new_chunk,
41784c88 2494 info->array.raid_disks, info->delta_disks,
9e325442
AK
2495 backup_file, devname, quiet)) {
2496 unfreeze(st);
5da9ab98 2497 return 1;
9e325442 2498 }
5da9ab98
N
2499
2500 sync_metadata(st);
2501
1bb174ba
AK
2502 /* ping monitor to be sure that update is on disk
2503 */
2504 ping_monitor(container);
5da9ab98
N
2505
2506 switch (fork()) {
2507 case -1: /* error */
2508 perror("Cannot fork to complete reshape\n");
9e325442 2509 unfreeze(st);
5da9ab98
N
2510 return 1;
2511 default: /* parent */
b76b30e0
AK
2512 if (!freeze_reshape)
2513 printf(Name ": multi-array reshape continues"
2514 " in background\n");
5da9ab98
N
2515 return 0;
2516 case 0: /* child */
cc700db3 2517 map_fork();
5da9ab98
N
2518 break;
2519 }
2520
9ad6f6e6
AK
2521 /* close unused handle in child process
2522 */
2523 if (mdfd > -1)
2524 close(mdfd);
2525
1bb174ba
AK
2526 while(1) {
2527 /* For each member array with reshape_active,
2528 * we need to perform the reshape.
2529 * We pick the first array that needs reshaping and
2530 * reshape it. reshape_array() will re-read the metadata
2531 * so the next time through a different array should be
2532 * ready for reshape.
493f5dd6
N
2533 * It is possible that the 'different' array will not
2534 * be assembled yet. In that case we simple exit.
2535 * When it is assembled, the mdadm which assembles it
2536 * will take over the reshape.
1bb174ba
AK
2537 */
2538 struct mdinfo *content;
5da9ab98
N
2539 int fd;
2540 struct mdstat_ent *mdstat;
5da9ab98
N
2541 char *adev;
2542
1bb174ba 2543 sysfs_free(cc);
5da9ab98 2544
1bb174ba
AK
2545 cc = st->ss->container_content(st, NULL);
2546
2547 for (content = cc; content ; content = content->next) {
2548 char *subarray;
2549 if (!content->reshape_active)
2550 continue;
2551
2552 subarray = strchr(content->text_version+1, '/')+1;
2553 mdstat = mdstat_by_subdev(subarray,
2554 devname2devnum(container));
2555 if (!mdstat)
2556 continue;
2557 break;
2558 }
2559 if (!content)
2560 break;
5da9ab98 2561
55f14721 2562 fd = open_dev(mdstat->devnum);
5da9ab98
N
2563 if (fd < 0)
2564 break;
2565 adev = map_dev(dev2major(mdstat->devnum),
2566 dev2minor(mdstat->devnum),
2567 0);
2568 if (!adev)
1bb174ba
AK
2569 adev = content->text_version;
2570
2571 sysfs_init(content, fd, mdstat->devnum);
5da9ab98 2572
1bb174ba 2573 rv = reshape_array(container, fd, adev, st,
e2e53a2d 2574 content, force, NULL,
b76b30e0
AK
2575 backup_file, quiet, 1, restart,
2576 freeze_reshape);
5da9ab98 2577 close(fd);
b76b30e0
AK
2578
2579 if (freeze_reshape) {
2580 sysfs_free(cc);
2581 exit(0);
2582 }
2583
493f5dd6 2584 restart = 0;
5da9ab98
N
2585 if (rv)
2586 break;
2587 }
bcc9e9ed
AK
2588 if (!rv)
2589 unfreeze(st);
1bb174ba 2590 sysfs_free(cc);
5da9ab98
N
2591 exit(0);
2592}
2593
7236ee7a
N
2594/*
2595 * We run a child process in the background which performs the following
2596 * steps:
2597 * - wait for resync to reach a certain point
2598 * - suspend io to the following section
2599 * - backup that section
2600 * - allow resync to proceed further
2601 * - resume io
2602 * - discard the backup.
2603 *
2604 * When are combined in slightly different ways in the three cases.
2605 * Grow:
2606 * - suspend/backup/allow/wait/resume/discard
2607 * Shrink:
2608 * - allow/wait/suspend/backup/allow/wait/resume/discard
2609 * same-size:
2610 * - wait/resume/discard/suspend/backup/allow
2611 *
2612 * suspend/backup/allow always come together
2613 * wait/resume/discard do too.
2614 * For the same-size case we have two backups to improve flow.
2615 *
2616 */
e86c9dd6 2617
7443ee81
N
2618int progress_reshape(struct mdinfo *info, struct reshape *reshape,
2619 unsigned long long backup_point,
2620 unsigned long long wait_point,
2621 unsigned long long *suspend_point,
2622 unsigned long long *reshape_completed)
2623{
2624 /* This function is called repeatedly by the reshape manager.
2625 * It determines how much progress can safely be made and allows
2626 * that progress.
2627 * - 'info' identifies the array and particularly records in
2628 * ->reshape_progress the metadata's knowledge of progress
2629 * This is a sector offset from the start of the array
2630 * of the next array block to be relocated. This number
2631 * may increase from 0 or decrease from array_size, depending
2632 * on the type of reshape that is happening.
2633 * Note that in contrast, 'sync_completed' is a block count of the
2634 * reshape so far. It gives the distance between the start point
2635 * (head or tail of device) and the next place that data will be
2636 * written. It always increases.
2637 * - 'reshape' is the structure created by analyse_change
2638 * - 'backup_point' shows how much the metadata manager has backed-up
2639 * data. For reshapes with increasing progress, it is the next address
2640 * to be backed up, previous addresses have been backed-up. For
2641 * decreasing progress, it is the earliest address that has been
2642 * backed up - later address are also backed up.
2643 * So addresses between reshape_progress and backup_point are
2644 * backed up providing those are in the 'correct' order.
2645 * - 'wait_point' is an array address. When reshape_completed
2646 * passes this point, progress_reshape should return. It might
2647 * return earlier if it determines that ->reshape_progress needs
2648 * to be updated or further backup is needed.
2649 * - suspend_point is maintained by progress_reshape and the caller
2650 * should not touch it except to initialise to zero.
2651 * It is an array address and it only increases in 2.6.37 and earlier.
b6b95155 2652 * This makes it difficult to handle reducing reshapes with
7443ee81
N
2653 * external metadata.
2654 * However: it is similar to backup_point in that it records the
2655 * other end of a suspended region from reshape_progress.
2656 * it is moved to extend the region that is safe to backup and/or
2657 * reshape
2658 * - reshape_completed is read from sysfs and returned. The caller
2659 * should copy this into ->reshape_progress when it has reason to
2660 * believe that the metadata knows this, and any backup outside this
2661 * has been erased.
2662 *
2663 * Return value is:
2664 * 1 if more data from backup_point - but only as far as suspend_point,
2665 * should be backed up
2666 * 0 if things are progressing smoothly
9e034f70
N
2667 * -1 if the reshape is finished because it is all done,
2668 * -2 if the reshape is finished due to an error.
7443ee81
N
2669 */
2670
2671 int advancing = (reshape->after.data_disks
2672 >= reshape->before.data_disks);
38dad34a
N
2673 unsigned long long need_backup; /* All data between start of array and
2674 * here will at some point need to
2675 * be backed up.
d0ab945e 2676 */
7443ee81 2677 unsigned long long read_offset, write_offset;
b4f8e38b 2678 unsigned long long write_range;
7443ee81 2679 unsigned long long max_progress, target, completed;
d0ab945e
N
2680 unsigned long long array_size = (info->component_size
2681 * reshape->before.data_disks);
7443ee81 2682 int fd;
77a73a17 2683 char buf[20];
7443ee81
N
2684
2685 /* First, we unsuspend any region that is now known to be safe.
2686 * If suspend_point is on the 'wrong' side of reshape_progress, then
2687 * we don't have or need suspension at the moment. This is true for
2688 * native metadata when we don't need to back-up.
2689 */
2690 if (advancing) {
49cd738b 2691 if (info->reshape_progress <= *suspend_point)
7443ee81
N
2692 sysfs_set_num(info, NULL, "suspend_lo",
2693 info->reshape_progress);
2694 } else {
2695 /* Note: this won't work in 2.6.37 and before.
2696 * Something somewhere should make sure we don't need it!
2697 */
49cd738b 2698 if (info->reshape_progress >= *suspend_point)
7443ee81
N
2699 sysfs_set_num(info, NULL, "suspend_hi",
2700 info->reshape_progress);
2701 }
2702
2703 /* Now work out how far it is safe to progress.
2704 * If the read_offset for ->reshape_progress is less than
2705 * 'blocks' beyond the write_offset, we can only progress as far
2706 * as a backup.
2707 * Otherwise we can progress until the write_offset for the new location
2708 * reaches (within 'blocks' of) the read_offset at the current location.
2709 * However that region must be suspended unless we are using native
2710 * metadata.
2711 * If we need to suspend more, we limit it to 128M per device, which is
2712 * rather arbitrary and should be some time-based calculation.
2713 */
ec757320
N
2714 read_offset = info->reshape_progress / reshape->before.data_disks;
2715 write_offset = info->reshape_progress / reshape->after.data_disks;
b4f8e38b 2716 write_range = info->new_chunk/512;
38dad34a
N
2717 if (reshape->before.data_disks == reshape->after.data_disks)
2718 need_backup = array_size;
2719 else
2720 need_backup = reshape->backup_blocks;
7443ee81 2721 if (advancing) {
38dad34a 2722 if (read_offset < write_offset + write_range)
7443ee81 2723 max_progress = backup_point;
ddee071d 2724 else
7443ee81 2725 max_progress =
2f3dd5e4
N
2726 read_offset *
2727 reshape->after.data_disks;
7443ee81 2728 } else {
38dad34a
N
2729 if (read_offset > write_offset - write_range)
2730 /* Can only progress as far as has been backed up,
2731 * which must be suspended */
7443ee81 2732 max_progress = backup_point;
38dad34a
N
2733 else if (info->reshape_progress <= need_backup)
2734 max_progress = backup_point;
2735 else {
2736 if (info->array.major_version >= 0)
2737 /* Can progress until backup is needed */
2738 max_progress = need_backup;
2739 else {
2740 /* Can progress until metadata update is required */
2741 max_progress =
2742 read_offset *
2743 reshape->after.data_disks;
2744 /* but data must be suspended */
2745 if (max_progress < *suspend_point)
2746 max_progress = *suspend_point;
2747 }
7443ee81
N
2748 }
2749 }
2750
2751 /* We know it is safe to progress to 'max_progress' providing
2752 * it is suspended or we are using native metadata.
2753 * Consider extending suspend_point 128M per device if it
2754 * is less than 64M per device beyond reshape_progress.
2755 * But always do a multiple of 'blocks'
832b2b9a
N
2756 * FIXME this is too big - it takes to long to complete
2757 * this much.
7443ee81
N
2758 */
2759 target = 64*1024*2 * min(reshape->before.data_disks,
2760 reshape->after.data_disks);
b4f8e38b 2761 target /= reshape->backup_blocks;
7443ee81
N
2762 if (target < 2)
2763 target = 2;
b4f8e38b 2764 target *= reshape->backup_blocks;
7443ee81
N
2765
2766 /* For externally managed metadata we always need to suspend IO to
2767 * the area being reshaped so we regularly push suspend_point forward.
2768 * For native metadata we only need the suspend if we are going to do
2769 * a backup.
2770 */
2771 if (advancing) {
d0ab945e
N
2772 if ((need_backup > info->reshape_progress
2773 || info->array.major_version < 0) &&
7443ee81 2774 *suspend_point < info->reshape_progress + target) {
d0ab945e
N
2775 if (need_backup < *suspend_point + 2 * target)
2776 *suspend_point = need_backup;
2777 else if (*suspend_point + 2 * target < array_size)
7443ee81 2778 *suspend_point += 2 * target;
d0ab945e
N
2779 else
2780 *suspend_point = array_size;
7443ee81 2781 sysfs_set_num(info, NULL, "suspend_hi", *suspend_point);
d0ab945e
N
2782 if (max_progress > *suspend_point)
2783 max_progress = *suspend_point;
7443ee81
N
2784 }
2785 } else {
38dad34a
N
2786 if (info->array.major_version >= 0) {
2787 /* Only need to suspend when about to backup */
2788 if (info->reshape_progress < need_backup * 2 &&
2789 *suspend_point > 0) {
d0ab945e 2790 *suspend_point = 0;
38dad34a
N
2791 sysfs_set_num(info, NULL, "suspend_lo", 0);
2792 sysfs_set_num(info, NULL, "suspend_hi", need_backup);
2793 }
2794 } else {
2795 /* Need to suspend continually */
2796 if (info->reshape_progress < *suspend_point)
2797 *suspend_point = info->reshape_progress;
2798 if (*suspend_point + target < info->reshape_progress)
2799 /* No need to move suspend region yet */;
2800 else {
2801 if (*suspend_point >= 2 * target)
2802 *suspend_point -= 2 * target;
2803 else
2804 *suspend_point = 0;
2805 sysfs_set_num(info, NULL, "suspend_lo",
2806 *suspend_point);
2807 }
d0ab945e
N
2808 if (max_progress < *suspend_point)
2809 max_progress = *suspend_point;
7443ee81
N
2810 }
2811 }
2812
2813 /* now set sync_max to allow that progress. sync_max, like
2814 * sync_completed is a count of sectors written per device, so
2815 * we find the difference between max_progress and the start point,
2816 * and divide that by after.data_disks to get a sync_max
2817 * number.
2818 * At the same time we convert wait_point to a similar number
2819 * for comparing against sync_completed.
2820 */
92d1991f 2821 /* scale down max_progress to per_disk */
7443ee81 2822 max_progress /= reshape->after.data_disks;
92d1991f
N
2823 /* Round to chunk size as some kernels give an erroneously high number */
2824 max_progress /= info->new_chunk/512;
2825 max_progress *= info->new_chunk/512;
7f913e9b
N
2826 /* And round to old chunk size as the kernel wants that */
2827 max_progress /= info->array.chunk_size/512;
2828 max_progress *= info->array.chunk_size/512;
92d1991f
N
2829 /* Limit progress to the whole device */
2830 if (max_progress > info->component_size)
2831 max_progress = info->component_size;
7443ee81 2832 wait_point /= reshape->after.data_disks;
92d1991f
N
2833 if (!advancing) {
2834 /* switch from 'device offset' to 'processed block count' */
2835 max_progress = info->component_size - max_progress;
2836 wait_point = info->component_size - wait_point;
2837 }
7443ee81
N
2838
2839 sysfs_set_num(info, NULL, "sync_max", max_progress);
2840
2841 /* Now wait. If we have already reached the point that we were
2842 * asked to wait to, don't wait at all, else wait for any change.
2843 * We need to select on 'sync_completed' as that is the place that
2844 * notifications happen, but we are really interested in
2845 * 'reshape_position'
2846 */
2847 fd = sysfs_get_fd(info, NULL, "sync_completed");
2848 if (fd < 0)
77a73a17 2849 goto check_progress;
7443ee81 2850
621ea11b 2851 if (sysfs_fd_get_ll(fd, &completed) < 0)
77a73a17 2852 goto check_progress;
621ea11b 2853
7443ee81
N
2854 while (completed < max_progress && completed < wait_point) {
2855 /* Check that sync_action is still 'reshape' to avoid
2856 * waiting forever on a dead array
2857 */
2858 char action[20];
2859 fd_set rfds;
2860 if (sysfs_get_str(info, NULL, "sync_action",
2861 action, 20) <= 0 ||
2862 strncmp(action, "reshape", 7) != 0)
2863 break;
26d6e157
AK
2864 /* Some kernels reset 'sync_completed' to zero
2865 * before setting 'sync_action' to 'idle'.
2866 * So we need these extra tests.
2867 */
2868 if (completed == 0 && advancing
2869 && info->reshape_progress > 0)
2870 break;
2871 if (completed == 0 && !advancing
2872 && info->reshape_progress < (info->component_size
2873 * reshape->after.data_disks))
2874 break;
7443ee81
N
2875 FD_ZERO(&rfds);
2876 FD_SET(fd, &rfds);
2877 select(fd+1, NULL, NULL, &rfds, NULL);
621ea11b 2878 if (sysfs_fd_get_ll(fd, &completed) < 0)
77a73a17 2879 goto check_progress;
7443ee81 2880 }
10d0d365
AK
2881 /* Some kernels reset 'sync_completed' to zero,
2882 * we need to have real point we are in md
2883 */
2884 if (completed == 0)
2885 completed = max_progress;
2886
92d1991f
N
2887 /* some kernels can give an incorrectly high 'completed' number */
2888 completed /= (info->new_chunk/512);
2889 completed *= (info->new_chunk/512);
7443ee81
N
2890 /* Convert 'completed' back in to a 'progress' number */
2891 completed *= reshape->after.data_disks;
2892 if (!advancing) {
2893 completed = info->component_size * reshape->after.data_disks
2894 - completed;
2895 }
2896 *reshape_completed = completed;
2897
2898 close(fd);
2899
2900 /* We return the need_backup flag. Caller will decide
d0ab945e 2901 * how much - a multiple of ->backup_blocks up to *suspend_point
7443ee81 2902 */
38dad34a
N
2903 if (advancing)
2904 return need_backup > info->reshape_progress;
2905 else
2906 return need_backup >= info->reshape_progress;
77a73a17
N
2907
2908check_progress:
2909 /* if we couldn't read a number from sync_completed, then
2910 * either the reshape did complete, or it aborted.
2911 * We can tell which by checking for 'none' in reshape_position.
621ea11b
N
2912 * If it did abort, then it might immediately restart if it
2913 * it was just a device failure that leaves us degraded but
2914 * functioning.
77a73a17
N
2915 */
2916 strcpy(buf, "hi");
2917 if (sysfs_get_str(info, NULL, "reshape_position", buf, sizeof(buf)) < 0
621ea11b
N
2918 || strncmp(buf, "none", 4) != 0) {
2919 /* The abort might only be temporary. Wait up to 10
2920 * seconds for fd to contain a valid number again.
2921 */
2922 struct timeval tv;
2923 int rv = -2;
2924 tv.tv_sec = 10;
2925 tv.tv_usec = 0;
6560987b 2926 while (fd >= 0 && rv < 0 && tv.tv_sec > 0) {
621ea11b
N
2927 fd_set rfds;
2928 FD_ZERO(&rfds);
2929 FD_SET(fd, &rfds);
2930 if (select(fd+1, NULL, NULL, &rfds, &tv) != 1)
2931 break;
6560987b
N
2932 switch (sysfs_fd_get_ll(fd, &completed)) {
2933 case 0:
621ea11b
N
2934 /* all good again */
2935 rv = 1;
6560987b
N
2936 break;
2937 case -2: /* read error - abort */
2938 tv.tv_sec = 0;
2939 break;
2940 }
621ea11b
N
2941 }
2942 if (fd >= 0)
2943 close(fd);
2944 return rv; /* abort */
2945 } else {
6d5316f6 2946 /* Maybe racing with array shutdown - check state */
621ea11b
N
2947 if (fd >= 0)
2948 close(fd);
6d5316f6
N
2949 if (sysfs_get_str(info, NULL, "array_state", buf, sizeof(buf)) < 0
2950 || strncmp(buf, "inactive", 8) == 0
2951 || strncmp(buf, "clear",5) == 0)
2952 return -2; /* abort */
77a73a17 2953 return -1; /* complete */
6d5316f6 2954 }
7443ee81
N
2955}
2956
2957
fcf57625 2958/* FIXME return status is never checked */
4411fb17 2959static int grow_backup(struct mdinfo *sra,
7236ee7a 2960 unsigned long long offset, /* per device */
7443ee81 2961 unsigned long stripes, /* per device, in old chunks */
7236ee7a
N
2962 int *sources, unsigned long long *offsets,
2963 int disks, int chunk, int level, int layout,
2964 int dests, int *destfd, unsigned long long *destoffsets,
d4445387 2965 int part, int *degraded,
7236ee7a
N
2966 char *buf)
2967{
2968 /* Backup 'blocks' sectors at 'offset' on each device of the array,
2969 * to storage 'destfd' (offset 'destoffsets'), after first
2970 * suspending IO. Then allow resync to continue
2971 * over the suspended section.
2972 * Use part 'part' of the backup-super-block.
2973 */
2974 int odata = disks;
2975 int rv = 0;
2976 int i;
f21e18ca
N
2977 unsigned long long ll;
2978 int new_degraded;
7236ee7a
N
2979 //printf("offset %llu\n", offset);
2980 if (level >= 4)
2981 odata--;
2982 if (level == 6)
2983 odata--;
7443ee81 2984
d4445387 2985 /* Check that array hasn't become degraded, else we might backup the wrong data */
2c6ac128
N
2986 if (sysfs_get_ll(sra, NULL, "degraded", &ll) < 0)
2987 return -1; /* FIXME this error is ignored */
f21e18ca 2988 new_degraded = (int)ll;
d4445387
N
2989 if (new_degraded != *degraded) {
2990 /* check each device to ensure it is still working */
2991 struct mdinfo *sd;
2992 for (sd = sra->devs ; sd ; sd = sd->next) {
2993 if (sd->disk.state & (1<<MD_DISK_FAULTY))
2994 continue;
2995 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
2996 char sbuf[20];
2997 if (sysfs_get_str(sra, sd, "state", sbuf, 20) < 0 ||
2998 strstr(sbuf, "faulty") ||
2999 strstr(sbuf, "in_sync") == NULL) {
3000 /* this device is dead */
3001 sd->disk.state = (1<<MD_DISK_FAULTY);
3002 if (sd->disk.raid_disk >= 0 &&
3003 sources[sd->disk.raid_disk] >= 0) {
3004 close(sources[sd->disk.raid_disk]);
3005 sources[sd->disk.raid_disk] = -1;
3006 }
3007 }
3008 }
3009 }
3010 *degraded = new_degraded;
3011 }
7236ee7a
N
3012 if (part) {
3013 bsb.arraystart2 = __cpu_to_le64(offset * odata);
200871ad 3014 bsb.length2 = __cpu_to_le64(stripes * (chunk/512) * odata);
7236ee7a
N
3015 } else {
3016 bsb.arraystart = __cpu_to_le64(offset * odata);
200871ad 3017 bsb.length = __cpu_to_le64(stripes * (chunk/512) * odata);
7236ee7a
N
3018 }
3019 if (part)
3020 bsb.magic[15] = '2';
3021 for (i = 0; i < dests; i++)
3022 if (part)
3023 lseek64(destfd[i], destoffsets[i] + __le64_to_cpu(bsb.devstart2)*512, 0);
3024 else
3025 lseek64(destfd[i], destoffsets[i], 0);
3026
3027 rv = save_stripes(sources, offsets,
3028 disks, chunk, level, layout,
3029 dests, destfd,
3030 offset*512*odata, stripes * chunk * odata,
3031 buf);
3032
3033 if (rv)
3034 return rv;
97396422 3035 bsb.mtime = __cpu_to_le64(time(0));
7236ee7a
N
3036 for (i = 0; i < dests; i++) {
3037 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
3038
3039 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
3040 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
3041 bsb.sb_csum2 = bsb_csum((char*)&bsb,
3042 ((char*)&bsb.sb_csum2)-((char*)&bsb));
3043
72044953 3044 rv = -1;
f21e18ca
N
3045 if ((unsigned long long)lseek64(destfd[i], destoffsets[i] - 4096, 0)
3046 != destoffsets[i] - 4096)
72044953
N
3047 break;
3048 if (write(destfd[i], &bsb, 512) != 512)
3049 break;
ff94fb86 3050 if (destoffsets[i] > 4096) {
f21e18ca 3051 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]+stripes*chunk*odata, 0) !=
a847575a 3052 destoffsets[i]+stripes*chunk*odata)
72044953
N
3053 break;
3054 if (write(destfd[i], &bsb, 512) != 512)
3055 break;
ff94fb86 3056 }
7236ee7a 3057 fsync(destfd[i]);
72044953 3058 rv = 0;
7236ee7a 3059 }
2efedc7b 3060
fcf57625 3061 return rv;
7236ee7a
N
3062}
3063
3064/* in 2.6.30, the value reported by sync_completed can be
3065 * less that it should be by one stripe.
3066 * This only happens when reshape hits sync_max and pauses.
3067 * So allow wait_backup to either extent sync_max further
3068 * than strictly necessary, or return before the
3069 * sync has got quite as far as we would really like.
3070 * This is what 'blocks2' is for.
3071 * The various caller give appropriate values so that
3072 * every works.
3073 */
fcf57625 3074/* FIXME return value is often ignored */
7443ee81 3075static int forget_backup(
7236ee7a
N
3076 int dests, int *destfd, unsigned long long *destoffsets,
3077 int part)
3078{
7443ee81
N
3079 /*
3080 * Erase backup 'part' (which is 0 or 1)
7236ee7a 3081 */
7236ee7a 3082 int i;
fcf57625 3083 int rv;
7236ee7a 3084
7236ee7a
N
3085 if (part) {
3086 bsb.arraystart2 = __cpu_to_le64(0);
3087 bsb.length2 = __cpu_to_le64(0);
3088 } else {
3089 bsb.arraystart = __cpu_to_le64(0);
3090 bsb.length = __cpu_to_le64(0);
3091 }
97396422 3092 bsb.mtime = __cpu_to_le64(time(0));
fcf57625 3093 rv = 0;
7236ee7a
N
3094 for (i = 0; i < dests; i++) {
3095 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
3096 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
3097 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
3098 bsb.sb_csum2 = bsb_csum((char*)&bsb,
3099 ((char*)&bsb.sb_csum2)-((char*)&bsb));
f21e18ca 3100 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]-4096, 0) !=
a847575a 3101 destoffsets[i]-4096)
72044953
N
3102 rv = -1;
3103 if (rv == 0 &&
3104 write(destfd[i], &bsb, 512) != 512)
3105 rv = -1;
7236ee7a
N
3106 fsync(destfd[i]);
3107 }
fcf57625 3108 return rv;
7236ee7a 3109}
e86c9dd6 3110
7236ee7a
N
3111static void fail(char *msg)
3112{
fcf57625 3113 int rv;
72044953
N
3114 rv = (write(2, msg, strlen(msg)) != (int)strlen(msg));
3115 rv |= (write(2, "\n", 1) != 1);
fcf57625 3116 exit(rv ? 1 : 2);
7236ee7a
N
3117}
3118
3119static char *abuf, *bbuf;
f21e18ca 3120static unsigned long long abuflen;
7236ee7a
N
3121static void validate(int afd, int bfd, unsigned long long offset)
3122{
3123 /* check that the data in the backup against the array.
3124 * This is only used for regression testing and should not
3125 * be used while the array is active
3126 */
7236ee7a
N
3127 if (afd < 0)
3128 return;
3129 lseek64(bfd, offset - 4096, 0);
3130 if (read(bfd, &bsb2, 512) != 512)
3131 fail("cannot read bsb");
3132 if (bsb2.sb_csum != bsb_csum((char*)&bsb2,
3133 ((char*)&bsb2.sb_csum)-((char*)&bsb2)))
3134 fail("first csum bad");
3135 if (memcmp(bsb2.magic, "md_backup_data", 14) != 0)
3136 fail("magic is bad");
3137 if (memcmp(bsb2.magic, "md_backup_data-2", 16) == 0 &&
3138 bsb2.sb_csum2 != bsb_csum((char*)&bsb2,
3139 ((char*)&bsb2.sb_csum2)-((char*)&bsb2)))
3140 fail("second csum bad");
3141
3142 if (__le64_to_cpu(bsb2.devstart)*512 != offset)
3143 fail("devstart is wrong");
3144
3145 if (bsb2.length) {
3146 unsigned long long len = __le64_to_cpu(bsb2.length)*512;
3147
3148 if (abuflen < len) {
3149 free(abuf);
3150 free(bbuf);
3151 abuflen = len;
fcf57625
N
3152 if (posix_memalign((void**)&abuf, 4096, abuflen) ||
3153 posix_memalign((void**)&bbuf, 4096, abuflen)) {
3154 abuflen = 0;
3155 /* just stop validating on mem-alloc failure */
3156 return;
3157 }
e86c9dd6 3158 }
48924014 3159
7236ee7a 3160 lseek64(bfd, offset, 0);
f21e18ca 3161 if ((unsigned long long)read(bfd, bbuf, len) != len) {
080fd005 3162 //printf("len %llu\n", len);
7236ee7a
N
3163 fail("read first backup failed");
3164 }
3165 lseek64(afd, __le64_to_cpu(bsb2.arraystart)*512, 0);
f21e18ca 3166 if ((unsigned long long)read(afd, abuf, len) != len)
7236ee7a
N
3167 fail("read first from array failed");
3168 if (memcmp(bbuf, abuf, len) != 0) {
080fd005 3169 #if 0
7236ee7a
N
3170 int i;
3171 printf("offset=%llu len=%llu\n",
080fd005 3172 (unsigned long long)__le64_to_cpu(bsb2.arraystart)*512, len);
7236ee7a
N
3173 for (i=0; i<len; i++)
3174 if (bbuf[i] != abuf[i]) {
3175 printf("first diff byte %d\n", i);
93ecfa01 3176 break;
7236ee7a 3177 }
080fd005 3178 #endif
7236ee7a 3179 fail("data1 compare failed");
e86c9dd6 3180 }
7236ee7a
N
3181 }
3182 if (bsb2.length2) {
3183 unsigned long long len = __le64_to_cpu(bsb2.length2)*512;
3184
3185 if (abuflen < len) {
3186 free(abuf);
3187 free(bbuf);
3188 abuflen = len;
3189 abuf = malloc(abuflen);
3190 bbuf = malloc(abuflen);
e86c9dd6
NB
3191 }
3192
7236ee7a 3193 lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0);
f21e18ca 3194 if ((unsigned long long)read(bfd, bbuf, len) != len)
7236ee7a
N
3195 fail("read second backup failed");
3196 lseek64(afd, __le64_to_cpu(bsb2.arraystart2)*512, 0);
f21e18ca 3197 if ((unsigned long long)read(afd, abuf, len) != len)
7236ee7a
N
3198 fail("read second from array failed");
3199 if (memcmp(bbuf, abuf, len) != 0)
3200 fail("data2 compare failed");
e86c9dd6 3201 }
7236ee7a 3202}
e86c9dd6 3203
999b4972
N
3204int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
3205 struct supertype *st, unsigned long blocks,
3206 int *fds, unsigned long long *offsets,
3207 int dests, int *destfd, unsigned long long *destoffsets)
7236ee7a 3208{
7443ee81
N
3209 /* Monitor a reshape where backup is being performed using
3210 * 'native' mechanism - either to a backup file, or
3211 * to some space in a spare.
3212 */
7236ee7a 3213 char *buf;
7443ee81
N
3214 int degraded = -1;
3215 unsigned long long speed;
3216 unsigned long long suspend_point, array_size;
3217 unsigned long long backup_point, wait_point;
3218 unsigned long long reshape_completed;
3219 int done = 0;
3220 int increasing = reshape->after.data_disks >= reshape->before.data_disks;
3221 int part = 0; /* The next part of the backup area to fill. It may already
3222 * be full, so we need to check */
3223 int level = reshape->level;
3224 int layout = reshape->before.layout;
3225 int data = reshape->before.data_disks;
3226 int disks = reshape->before.data_disks + reshape->parity;
3227 int chunk = sra->array.chunk_size;
da8100ca
N
3228 struct mdinfo *sd;
3229 unsigned long stripes;
3b7e9d0c 3230 int uuid[4];
da8100ca
N
3231
3232 /* set up the backup-super-block. This requires the
3233 * uuid from the array.
3234 */
3235 /* Find a superblock */
3236 for (sd = sra->devs; sd; sd = sd->next) {
3237 char *dn;
3238 int devfd;
3239 int ok;
3240 if (sd->disk.state & (1<<MD_DISK_FAULTY))
3241 continue;
3242 dn = map_dev(sd->disk.major, sd->disk.minor, 1);
3243 devfd = dev_open(dn, O_RDONLY);
3244 if (devfd < 0)
3245 continue;
3246 ok = st->ss->load_super(st, devfd, NULL);
3247 close(devfd);
77f8d358 3248 if (ok == 0)
da8100ca
N
3249 break;
3250 }
3251 if (!sd) {
3252 fprintf(stderr, Name ": Cannot find a superblock\n");
3253 return 0;
3254 }
3255
3256 memset(&bsb, 0, 512);
3257 memcpy(bsb.magic, "md_backup_data-1", 16);
3b7e9d0c
LB
3258 st->ss->uuid_from_super(st, uuid);
3259 memcpy(bsb.set_uuid, uuid, 16);
da8100ca
N
3260 bsb.mtime = __cpu_to_le64(time(0));
3261 bsb.devstart2 = blocks;
3262
3263 stripes = blocks / (sra->array.chunk_size/512) /
3264 reshape->before.data_disks;
e86c9dd6 3265
fcf57625
N
3266 if (posix_memalign((void**)&buf, 4096, disks * chunk))
3267 /* Don't start the 'reshape' */
3268 return 0;
7443ee81
N
3269 if (reshape->before.data_disks == reshape->after.data_disks) {
3270 sysfs_get_ll(sra, NULL, "sync_speed_min", &speed);
3271 sysfs_set_num(sra, NULL, "sync_speed_min", 200000);
3272 }
e86c9dd6 3273
7443ee81 3274 if (increasing) {
96533072 3275 array_size = sra->component_size * reshape->after.data_disks;
7443ee81
N
3276 backup_point = sra->reshape_progress;
3277 suspend_point = 0;
3278 } else {
96533072 3279 array_size = sra->component_size * reshape->before.data_disks;
25da62d9 3280 backup_point = reshape->backup_blocks;
7443ee81
N
3281 suspend_point = array_size;
3282 }
7236ee7a 3283
7443ee81
N
3284 while (!done) {
3285 int rv;
7236ee7a 3286
7443ee81
N
3287 /* Want to return as soon the oldest backup slot can
3288 * be released as that allows us to start backing up
3289 * some more, providing suspend_point has been
b6b95155 3290 * advanced, which it should have.
7443ee81
N
3291 */
3292 if (increasing) {
3293 wait_point = array_size;
3294 if (part == 0 && __le64_to_cpu(bsb.length) > 0)
3295 wait_point = (__le64_to_cpu(bsb.arraystart) +
3296 __le64_to_cpu(bsb.length));
3297 if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
3298 wait_point = (__le64_to_cpu(bsb.arraystart2) +
3299 __le64_to_cpu(bsb.length2));
3300 } else {
3301 wait_point = 0;
3302 if (part == 0 && __le64_to_cpu(bsb.length) > 0)
3303 wait_point = __le64_to_cpu(bsb.arraystart);
3304 if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
3305 wait_point = __le64_to_cpu(bsb.arraystart2);
3306 }
3307
3308 rv = progress_reshape(sra, reshape,
3309 backup_point, wait_point,
3310 &suspend_point, &reshape_completed);
7443ee81
N
3311 /* external metadata would need to ping_monitor here */
3312 sra->reshape_progress = reshape_completed;
7236ee7a 3313
7443ee81
N
3314 /* Clear any backup region that is before 'here' */
3315 if (increasing) {
b3cf095a
N
3316 if (__le64_to_cpu(bsb.length) > 0 &&
3317 reshape_completed >= (__le64_to_cpu(bsb.arraystart) +
7443ee81
N
3318 __le64_to_cpu(bsb.length)))
3319 forget_backup(dests, destfd,
3320 destoffsets, 0);
b3cf095a
N
3321 if (__le64_to_cpu(bsb.length2) > 0 &&
3322 reshape_completed >= (__le64_to_cpu(bsb.arraystart2) +
7443ee81
N
3323 __le64_to_cpu(bsb.length2)))
3324 forget_backup(dests, destfd,
3325 destoffsets, 1);
3326 } else {
b3cf095a
N
3327 if (__le64_to_cpu(bsb.length) > 0 &&
3328 reshape_completed <= (__le64_to_cpu(bsb.arraystart)))
7443ee81
N
3329 forget_backup(dests, destfd,
3330 destoffsets, 0);
b3cf095a
N
3331 if (__le64_to_cpu(bsb.length2) > 0 &&
3332 reshape_completed <= (__le64_to_cpu(bsb.arraystart2)))
7443ee81
N
3333 forget_backup(dests, destfd,
3334 destoffsets, 1);
3335 }
7236ee7a 3336
223c5c99 3337 if (rv < 0) {
77a73a17
N
3338 if (rv == -1)
3339 done = 1;
223c5c99
N
3340 break;
3341 }
2d4de5f9
N
3342 if (rv == 0 && increasing && !st->ss->external) {
3343 /* No longer need to monitor this reshape */
3344 done = 1;
3345 break;
3346 }
223c5c99 3347
60204e4e 3348 while (rv) {
7443ee81 3349 unsigned long long offset;
e97ca002 3350 unsigned long actual_stripes;
60204e4e
N
3351 /* Need to backup some data.
3352 * If 'part' is not used and the desired
3353 * backup size is suspended, do a backup,
3354 * then consider the next part.
3355 */
7443ee81
N
3356 /* Check that 'part' is unused */
3357 if (part == 0 && __le64_to_cpu(bsb.length) != 0)
60204e4e 3358 break;
7443ee81 3359 if (part == 1 && __le64_to_cpu(bsb.length2) != 0)
60204e4e 3360 break;
7443ee81
N
3361
3362 offset = backup_point / data;
e97ca002 3363 actual_stripes = stripes;
60204e4e 3364 if (increasing) {
e97ca002
N
3365 if (offset + actual_stripes * (chunk/512) >
3366 sra->component_size)
3367 actual_stripes = ((sra->component_size - offset)
3368 / (chunk/512));
3369 if (offset + actual_stripes * (chunk/512) >
60204e4e
N
3370 suspend_point/data)
3371 break;
3372 } else {
e97ca002
N
3373 if (offset < actual_stripes * (chunk/512))
3374 actual_stripes = offset / (chunk/512);
3375 offset -= actual_stripes * (chunk/512);
3376 if (offset < suspend_point/data)
60204e4e
N
3377 break;
3378 }
e4b11073
N
3379 if (actual_stripes == 0)
3380 break;
e97ca002 3381 grow_backup(sra, offset, actual_stripes,
7443ee81
N
3382 fds, offsets,
3383 disks, chunk, level, layout,
3384 dests, destfd, destoffsets,
3385 part, &degraded, buf);
3386 validate(afd, destfd[0], destoffsets[0]);
3387 /* record where 'part' is up to */
3388 part = !part;
3389 if (increasing)
e97ca002 3390 backup_point += actual_stripes * (chunk/512) * data;
7443ee81 3391 else
e97ca002 3392 backup_point -= actual_stripes * (chunk/512) * data;
7443ee81
N
3393 }
3394 }
3395
223c5c99
N
3396 /* FIXME maybe call progress_reshape one more time instead */
3397 abort_reshape(sra); /* remove any remaining suspension */
7443ee81
N
3398 if (reshape->before.data_disks == reshape->after.data_disks)
3399 sysfs_set_num(sra, NULL, "sync_speed_min", speed);
7236ee7a 3400 free(buf);
77a73a17 3401 return done;
e86c9dd6 3402}
353632d9
NB
3403
3404/*
3405 * If any spare contains md_back_data-1 which is recent wrt mtime,
3406 * write that data into the array and update the super blocks with
3407 * the new reshape_progress
3408 */
ea0ebe96
N
3409int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt,
3410 char *backup_file, int verbose)
353632d9
NB
3411{
3412 int i, j;
3413 int old_disks;
353632d9 3414 unsigned long long *offsets;
82f2d6ab 3415 unsigned long long nstripe, ostripe;
6e9eac4f 3416 int ndata, odata;
353632d9 3417
e9e43ec3
N
3418 odata = info->array.raid_disks - info->delta_disks - 1;
3419 if (info->array.level == 6) odata--; /* number of data disks */
3420 ndata = info->array.raid_disks - 1;
3421 if (info->new_level == 6) ndata--;
353632d9
NB
3422
3423 old_disks = info->array.raid_disks - info->delta_disks;
3424
e9e43ec3
N
3425 if (info->delta_disks <= 0)
3426 /* Didn't grow, so the backup file must have
3427 * been used
3428 */
3429 old_disks = cnt;
06b0d786 3430 for (i=old_disks-(backup_file?1:0); i<cnt; i++) {
353632d9 3431 struct mdinfo dinfo;
06b0d786 3432 int fd;
e9e43ec3 3433 int bsbsize;
ea0ebe96 3434 char *devname, namebuf[20];
10af14c4 3435 unsigned long long lo, hi;
353632d9
NB
3436
3437 /* This was a spare and may have some saved data on it.
3438 * Load the superblock, find and load the
3439 * backup_super_block.
3440 * If either fail, go on to next device.
3441 * If the backup contains no new info, just return
206c5eae 3442 * else restore data and update all superblocks
353632d9 3443 */
06b0d786
NB
3444 if (i == old_disks-1) {
3445 fd = open(backup_file, O_RDONLY);
e9e43ec3
N
3446 if (fd<0) {
3447 fprintf(stderr, Name ": backup file %s inaccessible: %s\n",
3448 backup_file, strerror(errno));
06b0d786 3449 continue;
e9e43ec3 3450 }
ea0ebe96 3451 devname = backup_file;
06b0d786
NB
3452 } else {
3453 fd = fdlist[i];
3454 if (fd < 0)
3455 continue;
3da92f27 3456 if (st->ss->load_super(st, fd, NULL))
06b0d786 3457 continue;
353632d9 3458
a5d85af7 3459 st->ss->getinfo_super(st, &dinfo, NULL);
3da92f27
NB
3460 st->ss->free_super(st);
3461
06b0d786
NB
3462 if (lseek64(fd,
3463 (dinfo.data_offset + dinfo.component_size - 8) <<9,
ea0ebe96
N
3464 0) < 0) {
3465 fprintf(stderr, Name ": Cannot seek on device %d\n", i);
06b0d786 3466 continue; /* Cannot seek */
ea0ebe96
N
3467 }
3468 sprintf(namebuf, "device-%d", i);
3469 devname = namebuf;
06b0d786 3470 }
ea0ebe96
N
3471 if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb)) {
3472 if (verbose)
3473 fprintf(stderr, Name ": Cannot read from %s\n", devname);
353632d9 3474 continue; /* Cannot read */
ea0ebe96 3475 }
e9e43ec3 3476 if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0 &&
ea0ebe96
N
3477 memcmp(bsb.magic, "md_backup_data-2", 16) != 0) {
3478 if (verbose)
3479 fprintf(stderr, Name ": No backup metadata on %s\n", devname);
353632d9 3480 continue;
ea0ebe96
N
3481 }
3482 if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb))) {
3483 if (verbose)
3484 fprintf(stderr, Name ": Bad backup-metadata checksum on %s\n", devname);
353632d9 3485 continue; /* bad checksum */
ea0ebe96 3486 }
e9e43ec3 3487 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0 &&
ea0ebe96
N
3488 bsb.sb_csum2 != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum2)-((char*)&bsb))) {
3489 if (verbose)
3490 fprintf(stderr, Name ": Bad backup-metadata checksum2 on %s\n", devname);
22e30516 3491 continue; /* Bad second checksum */
ea0ebe96
N
3492 }
3493 if (memcmp(bsb.set_uuid,info->uuid, 16) != 0) {
3494 if (verbose)
3495 fprintf(stderr, Name ": Wrong uuid on backup-metadata on %s\n", devname);
353632d9 3496 continue; /* Wrong uuid */
ea0ebe96 3497 }
353632d9 3498
097075b6
N
3499 /* array utime and backup-mtime should be updated at much the same time, but it seems that
3500 * sometimes they aren't... So allow considerable flexability in matching, and allow
3501 * this test to be overridden by an environment variable.
3502 */
f21e18ca
N
3503 if (info->array.utime > (int)__le64_to_cpu(bsb.mtime) + 2*60*60 ||
3504 info->array.utime < (int)__le64_to_cpu(bsb.mtime) - 10*60) {
097075b6
N
3505 if (check_env("MDADM_GROW_ALLOW_OLD")) {
3506 fprintf(stderr, Name ": accepting backup with timestamp %lu "
3507 "for array with timestamp %lu\n",
3508 (unsigned long)__le64_to_cpu(bsb.mtime),
3509 (unsigned long)info->array.utime);
3510 } else {
3511 if (verbose)
3512 fprintf(stderr, Name ": too-old timestamp on "
3513 "backup-metadata on %s\n", devname);
3514 continue; /* time stamp is too bad */
3515 }
ea0ebe96 3516 }
353632d9 3517
e9e43ec3 3518 if (bsb.magic[15] == '1') {
10af14c4
N
3519 if (bsb.length == 0)
3520 continue;
e7a71c6b
N
3521 if (info->delta_disks >= 0) {
3522 /* reshape_progress is increasing */
3523 if (__le64_to_cpu(bsb.arraystart)
3524 + __le64_to_cpu(bsb.length)
3525 < info->reshape_progress) {
3526 nonew:
3527 if (verbose)
3528 fprintf(stderr, Name
3529 ": backup-metadata found on %s but is not needed\n", devname);
3530 continue; /* No new data here */
3531 }
3532 } else {
3533 /* reshape_progress is decreasing */
3534 if (__le64_to_cpu(bsb.arraystart) >=
3535 info->reshape_progress)
3536 goto nonew; /* No new data here */
ea0ebe96 3537 }
e9e43ec3 3538 } else {
10af14c4
N
3539 if (bsb.length == 0 && bsb.length2 == 0)
3540 continue;
e7a71c6b
N
3541 if (info->delta_disks >= 0) {
3542 /* reshape_progress is increasing */
3543 if ((__le64_to_cpu(bsb.arraystart)
3544 + __le64_to_cpu(bsb.length)
3545 < info->reshape_progress)
3546 &&
3547 (__le64_to_cpu(bsb.arraystart2)
3548 + __le64_to_cpu(bsb.length2)
3549 < info->reshape_progress))
3550 goto nonew; /* No new data here */
3551 } else {
3552 /* reshape_progress is decreasing */
3553 if (__le64_to_cpu(bsb.arraystart) >=
3554 info->reshape_progress &&
3555 __le64_to_cpu(bsb.arraystart2) >=
3556 info->reshape_progress)
3557 goto nonew; /* No new data here */
3558 }
e9e43ec3 3559 }
ea0ebe96
N
3560 if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0) {
3561 second_fail:
3562 if (verbose)
e7a71c6b
N
3563 fprintf(stderr, Name
3564 ": Failed to verify secondary backup-metadata block on %s\n",
ea0ebe96 3565 devname);
353632d9 3566 continue; /* Cannot seek */
ea0ebe96 3567 }
2efedc7b 3568 /* There should be a duplicate backup superblock 4k before here */
06b0d786 3569 if (lseek64(fd, -4096, 1) < 0 ||
0155af90 3570 read(fd, &bsb2, sizeof(bsb2)) != sizeof(bsb2))
ea0ebe96 3571 goto second_fail; /* Cannot find leading superblock */
e9e43ec3
N
3572 if (bsb.magic[15] == '1')
3573 bsbsize = offsetof(struct mdp_backup_super, pad1);
3574 else
3575 bsbsize = offsetof(struct mdp_backup_super, pad);
ff94fb86 3576 if (memcmp(&bsb2, &bsb, bsbsize) != 0)
ea0ebe96 3577 goto second_fail; /* Cannot find leading superblock */
2efedc7b 3578
353632d9
NB
3579 /* Now need the data offsets for all devices. */
3580 offsets = malloc(sizeof(*offsets)*info->array.raid_disks);
3581 for(j=0; j<info->array.raid_disks; j++) {
3582 if (fdlist[j] < 0)
3583 continue;
3da92f27 3584 if (st->ss->load_super(st, fdlist[j], NULL))
353632d9
NB
3585 /* FIXME should be this be an error */
3586 continue;
a5d85af7 3587 st->ss->getinfo_super(st, &dinfo, NULL);
3da92f27 3588 st->ss->free_super(st);
14e5b4d7 3589 offsets[j] = dinfo.data_offset * 512;
353632d9
NB
3590 }
3591 printf(Name ": restoring critical section\n");
3592
3593 if (restore_stripes(fdlist, offsets,
3594 info->array.raid_disks,
3595 info->new_chunk,
3596 info->new_level,
3597 info->new_layout,
06b0d786 3598 fd, __le64_to_cpu(bsb.devstart)*512,
92dcdf7c 3599 __le64_to_cpu(bsb.arraystart)*512,
2fcb75ae 3600 __le64_to_cpu(bsb.length)*512, NULL)) {
e9e43ec3 3601 /* didn't succeed, so giveup */
ea0ebe96
N
3602 if (verbose)
3603 fprintf(stderr, Name ": Error restoring backup from %s\n",
3604 devname);
e9e43ec3
N
3605 return 1;
3606 }
3607
3608 if (bsb.magic[15] == '2' &&
3609 restore_stripes(fdlist, offsets,
3610 info->array.raid_disks,
3611 info->new_chunk,
3612 info->new_level,
3613 info->new_layout,
3614 fd, __le64_to_cpu(bsb.devstart)*512 +
3615 __le64_to_cpu(bsb.devstart2)*512,
92dcdf7c 3616 __le64_to_cpu(bsb.arraystart2)*512,
2fcb75ae 3617 __le64_to_cpu(bsb.length2)*512, NULL)) {
353632d9 3618 /* didn't succeed, so giveup */
ea0ebe96
N
3619 if (verbose)
3620 fprintf(stderr, Name ": Error restoring second backup from %s\n",
3621 devname);
2295250a 3622 return 1;
353632d9
NB
3623 }
3624
e9e43ec3 3625
353632d9
NB
3626 /* Ok, so the data is restored. Let's update those superblocks. */
3627
10af14c4
N
3628 lo = hi = 0;
3629 if (bsb.length) {
3630 lo = __le64_to_cpu(bsb.arraystart);
3631 hi = lo + __le64_to_cpu(bsb.length);
3632 }
3633 if (bsb.magic[15] == '2' && bsb.length2) {
3634 unsigned long long lo1, hi1;
3635 lo1 = __le64_to_cpu(bsb.arraystart2);
3636 hi1 = lo1 + __le64_to_cpu(bsb.length2);
3637 if (lo == hi) {
3638 lo = lo1;
3639 hi = hi1;
3640 } else if (lo < lo1)
3641 hi = hi1;
3642 else
3643 lo = lo1;
3644 }
3645 if (lo < hi &&
3646 (info->reshape_progress < lo ||
3647 info->reshape_progress > hi))
3648 /* backup does not affect reshape_progress*/ ;
3649 else if (info->delta_disks >= 0) {
e9e43ec3
N
3650 info->reshape_progress = __le64_to_cpu(bsb.arraystart) +
3651 __le64_to_cpu(bsb.length);
3652 if (bsb.magic[15] == '2') {
3653 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2) +
3654 __le64_to_cpu(bsb.length2);
3655 if (p2 > info->reshape_progress)
3656 info->reshape_progress = p2;
3657 }
3658 } else {
3659 info->reshape_progress = __le64_to_cpu(bsb.arraystart);
3660 if (bsb.magic[15] == '2') {
3661 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2);
3662 if (p2 < info->reshape_progress)
3663 info->reshape_progress = p2;
3664 }
3665 }
353632d9
NB
3666 for (j=0; j<info->array.raid_disks; j++) {
3667 if (fdlist[j] < 0) continue;
3da92f27 3668 if (st->ss->load_super(st, fdlist[j], NULL))
353632d9 3669 continue;
a5d85af7 3670 st->ss->getinfo_super(st, &dinfo, NULL);
e9e43ec3 3671 dinfo.reshape_progress = info->reshape_progress;
3da92f27 3672 st->ss->update_super(st, &dinfo,
68c7d6d7
NB
3673 "_reshape_progress",
3674 NULL,0, 0, NULL);
3da92f27
NB
3675 st->ss->store_super(st, fdlist[j]);
3676 st->ss->free_super(st);
353632d9 3677 }
353632d9
NB
3678 return 0;
3679 }
6e9eac4f
NB
3680 /* Didn't find any backup data, try to see if any
3681 * was needed.
3682 */
82f2d6ab
N
3683 if (info->delta_disks < 0) {
3684 /* When shrinking, the critical section is at the end.
3685 * So see if we are before the critical section.
3686 */
3687 unsigned long long first_block;
3688 nstripe = ostripe = 0;
3689 first_block = 0;
3690 while (ostripe >= nstripe) {
3691 ostripe += info->array.chunk_size / 512;
3692 first_block = ostripe * odata;
3693 nstripe = first_block / ndata / (info->new_chunk/512) *
3694 (info->new_chunk/512);
3695 }
3696
3697 if (info->reshape_progress >= first_block)
3698 return 0;
6e9eac4f 3699 }
82f2d6ab
N
3700 if (info->delta_disks > 0) {
3701 /* See if we are beyond the critical section. */
3702 unsigned long long last_block;
3703 nstripe = ostripe = 0;
3704 last_block = 0;
3705 while (nstripe >= ostripe) {
3706 nstripe += info->new_chunk / 512;
3707 last_block = nstripe * ndata;
3708 ostripe = last_block / odata / (info->array.chunk_size/512) *
3709 (info->array.chunk_size/512);
3710 }
6e9eac4f 3711
82f2d6ab
N
3712 if (info->reshape_progress >= last_block)
3713 return 0;
3714 }
6e9eac4f 3715 /* needed to recover critical section! */
ea0ebe96
N
3716 if (verbose)
3717 fprintf(stderr, Name ": Failed to find backup of critical section\n");
2295250a 3718 return 1;
353632d9 3719}
e9e43ec3 3720
2dddadb0
AK
3721int Grow_continue_command(char *devname, int fd,
3722 char *backup_file, int verbose)
3723{
3724 int ret_val = 0;
3725 struct supertype *st = NULL;
3726 struct mdinfo *content = NULL;
3727 struct mdinfo array;
3728 char *subarray = NULL;
3729 struct mdinfo *cc = NULL;
3730 struct mdstat_ent *mdstat = NULL;
3731 char buf[40];
3732 int cfd = -1;
3733 int fd2 = -1;
f1fe496b
AK
3734 char *ep;
3735 unsigned long long position;
2dddadb0
AK
3736
3737 dprintf("Grow continue from command line called for %s\n",
3738 devname);
3739
3740 st = super_by_fd(fd, &subarray);
3741 if (!st || !st->ss) {
3742 fprintf(stderr,
3743 Name ": Unable to determine metadata format for %s\n",
3744 devname);
3745 return 1;
3746 }
3747 dprintf("Grow continue is run for ");
3748 if (st->ss->external == 0) {
3749 dprintf("native array (%s)\n", devname);
3750 if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) {
3751 fprintf(stderr, Name ": %s is not an active md array -"
3752 " aborting\n", devname);
3753 ret_val = 1;
3754 goto Grow_continue_command_exit;
3755 }
3756 content = &array;
3757 sysfs_init(content, fd, st->devnum);
3758 } else {
3759 int container_dev;
3760
3761 if (subarray) {
3762 dprintf("subarray (%s)\n", subarray);
3763 container_dev = st->container_dev;
3764 cfd = open_dev_excl(st->container_dev);
3765 } else {
3766 container_dev = st->devnum;
3767 close(fd);
3768 cfd = open_dev_excl(st->devnum);
3769 dprintf("container (%i)\n", container_dev);
3770 fd = cfd;
3771 }
3772 if (cfd < 0) {
3773 fprintf(stderr, Name ": Unable to open container "
3774 "for %s\n", devname);
3775 ret_val = 1;
3776 goto Grow_continue_command_exit;
3777 }
3778 fmt_devname(buf, container_dev);
3779
3780 /* find in container array under reshape
3781 */
3782 ret_val = st->ss->load_container(st, cfd, NULL);
3783 if (ret_val) {
3784 fprintf(stderr,
3785 Name ": Cannot read superblock for %s\n",
3786 devname);
3787 ret_val = 1;
3788 goto Grow_continue_command_exit;
3789 }
3790
3791 cc = st->ss->container_content(st, NULL);
3792 for (content = cc; content ; content = content->next) {
3793 char *array;
81219e70 3794 int allow_reshape;
2dddadb0
AK
3795
3796 if (content->reshape_active == 0)
3797 continue;
81219e70
LM
3798 /* The decision about array or container wide
3799 * reshape is taken in Grow_continue based
3800 * content->reshape_active state, therefore we
3801 * need to check_reshape based on
3802 * reshape_active and subarray name
3803 */
3804 allow_reshape =
3805 check_reshape((content->reshape_active == CONTAINER_RESHAPE)? NULL : subarray,
3806 content);
3807 if (!allow_reshape) {
3808 fprintf(stderr, Name
3809 ": cannot continue reshape of an array"
3810 " in container with unsupported"
3811 " metadata: %s(%s)\n",
3812 devname, buf);
3813 ret_val = 1;
3814 goto Grow_continue_command_exit;
3815 }
2dddadb0
AK
3816
3817 array = strchr(content->text_version+1, '/')+1;
3818 mdstat = mdstat_by_subdev(array, container_dev);
3819 if (!mdstat)
3820 continue;
3821 break;
3822 }
3823 if (!content) {
3824 fprintf(stderr,
3825 Name ": Unable to determine reshaped "
3826 "array for %s\n", devname);
3827 ret_val = 1;
3828 goto Grow_continue_command_exit;
3829 }
3830 fd2 = open_dev(mdstat->devnum);
3831 if (fd2 < 0) {
3832 fprintf(stderr, Name ": cannot open (md%i)\n",
3833 mdstat->devnum);
3834 ret_val = 1;
3835 goto Grow_continue_command_exit;
3836 }
3837
3838 sysfs_init(content, fd2, mdstat->devnum);
3839
3840 /* start mdmon in case it is not running
3841 */
3842 if (!mdmon_running(container_dev))
3843 start_mdmon(container_dev);
3844 ping_monitor(buf);
3845
3846 if (mdmon_running(container_dev))
3847 st->update_tail = &st->updates;
3848 else {
3849 fprintf(stderr, Name ": No mdmon found. "
3850 "Grow cannot continue.\n");
3851 ret_val = 1;
3852 goto Grow_continue_command_exit;
3853 }
3854 }
3855
f1fe496b
AK
3856 /* verify that array under reshape is started from
3857 * correct position
3858 */
3859 ret_val = sysfs_get_str(content, NULL, "sync_max", buf, 40);
3860 if (ret_val <= 0) {
3861 fprintf(stderr, Name
3862 ": cannot open verify reshape progress for %s (%i)\n",
3863 content->sys_name, ret_val);
3864 ret_val = 1;
3865 goto Grow_continue_command_exit;
3866 }
3867 dprintf(Name ": Read sync_max sysfs entry is: %s\n", buf);
3868 position = strtoull(buf, &ep, 0);
3869 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) {
3870 fprintf(stderr, Name ": Fatal error: array reshape was"
3871 " not properly frozen\n");
3872 ret_val = 1;
3873 goto Grow_continue_command_exit;
3874 }
3875 position *= get_data_disks(map_name(pers, mdstat->level),
3876 content->new_layout,
3877 content->array.raid_disks);
3878 if (position != content->reshape_progress) {
3879 fprintf(stderr, Name ": Fatal error: array reshape was"
3880 " not properly frozen.\n");
3881 ret_val = 1;
3882 goto Grow_continue_command_exit;
3883 }
3884
2dddadb0
AK
3885 /* continue reshape
3886 */
3887 ret_val = Grow_continue(fd, st, content, backup_file, 0);
3888
3889Grow_continue_command_exit:
3890 if (fd2 > -1)
3891 close(fd2);
3892 if (cfd > -1)
3893 close(cfd);
3894 st->ss->free_super(st);
3895 free_mdstat(mdstat);
3896 sysfs_free(cc);
3897 free(subarray);
3898
3899 return ret_val;
3900}
3901
e9e43ec3 3902int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
b76b30e0 3903 char *backup_file, int freeze_reshape)
e9e43ec3 3904{
3bd58dc6
AK
3905 int ret_val = 2;
3906
3907 if (!info->reshape_active)
3908 return ret_val;
864a004f 3909
20a40eca 3910 if (st->ss->external) {
3bd58dc6
AK
3911 char container[40];
3912 int cfd = open_dev(st->container_dev);
3db2fdd8 3913
3bd58dc6
AK
3914 if (cfd < 0)
3915 return 1;
f362d22b 3916
3bd58dc6
AK
3917 fmt_devname(container, st->container_dev);
3918 st->ss->load_container(st, cfd, container);
3919 close(cfd);
9ad6f6e6 3920 ret_val = reshape_container(container, NULL, mdfd,
3bd58dc6
AK
3921 st, info, 0, backup_file,
3922 0, 1, freeze_reshape);
3923 } else
3924 ret_val = reshape_array(NULL, mdfd, "array", st, info, 1,
3925 NULL, backup_file, 0, 0, 1,
3926 freeze_reshape);
f362d22b 3927
3bd58dc6 3928 return ret_val;
e9e43ec3 3929}