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