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