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