]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Grow.c
Be more careful checking why reshape has stopped.
[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;
524
525 if (!sra)
526 return -1;
527 err = sysfs_freeze_array(sra);
528 sysfs_free(sra);
529 return err;
530 }
7236ee7a
N
531}
532
9202b8eb 533static void unfreeze(struct supertype *st)
7236ee7a 534{
7f2ba464
DW
535 if (st->ss->external)
536 return unfreeze_container(st);
537 else {
538 struct mdinfo *sra = sysfs_read(-1, st->devnum, GET_VERSION);
539
540 if (sra)
541 sysfs_set_str(sra, NULL, "sync_action", "idle");
542 else
543 fprintf(stderr, Name ": failed to unfreeze array\n");
544 sysfs_free(sra);
545 }
7236ee7a
N
546}
547
4411fb17 548static void wait_reshape(struct mdinfo *sra)
7236ee7a
N
549{
550 int fd = sysfs_get_fd(sra, NULL, "sync_action");
551 char action[20];
552
92a19f1a
AK
553 if (fd < 0)
554 return;
555
556 while (sysfs_fd_get_str(fd, action, 20) > 0 &&
557 strncmp(action, "reshape", 7) == 0) {
7236ee7a
N
558 fd_set rfds;
559 FD_ZERO(&rfds);
560 FD_SET(fd, &rfds);
561 select(fd+1, NULL, NULL, &rfds, NULL);
92a19f1a
AK
562 }
563 close(fd);
7236ee7a 564}
7bc71196
DW
565
566static int reshape_super(struct supertype *st, long long size, int level,
567 int layout, int chunksize, int raid_disks,
568 char *backup_file, char *dev, int verbose)
569{
570 /* nothing extra to check in the native case */
571 if (!st->ss->external)
572 return 0;
573 if (!st->ss->reshape_super ||
574 !st->ss->manage_reshape) {
575 fprintf(stderr, Name ": %s metadata does not support reshape\n",
576 st->ss->name);
577 return 1;
578 }
579
580 return st->ss->reshape_super(st, size, level, layout, chunksize,
581 raid_disks, backup_file, dev, verbose);
582}
583
584static void sync_metadata(struct supertype *st)
585{
586 if (st->ss->external) {
76266030 587 if (st->update_tail) {
7bc71196 588 flush_metadata_updates(st);
76266030
N
589 st->update_tail = &st->updates;
590 } else
7bc71196
DW
591 st->ss->sync_metadata(st);
592 }
593}
594
595static int subarray_set_num(char *container, struct mdinfo *sra, char *name, int n)
596{
597 /* when dealing with external metadata subarrays we need to be
598 * prepared to handle EAGAIN. The kernel may need to wait for
599 * mdmon to mark the array active so the kernel can handle
600 * allocations/writeback when preparing the reshape action
601 * (md_allow_write()). We temporarily disable safe_mode_delay
602 * to close a race with the array_state going clean before the
603 * next write to raid_disks / stripe_cache_size
604 */
605 char safe[50];
606 int rc;
607
608 /* only 'raid_disks' and 'stripe_cache_size' trigger md_allow_write */
5da9ab98
N
609 if (!container ||
610 (strcmp(name, "raid_disks") != 0 &&
611 strcmp(name, "stripe_cache_size") != 0))
7bc71196
DW
612 return sysfs_set_num(sra, NULL, name, n);
613
614 rc = sysfs_get_str(sra, NULL, "safe_mode_delay", safe, sizeof(safe));
615 if (rc <= 0)
616 return -1;
617 sysfs_set_num(sra, NULL, "safe_mode_delay", 0);
618 rc = sysfs_set_num(sra, NULL, name, n);
619 if (rc < 0 && errno == EAGAIN) {
620 ping_monitor(container);
621 /* if we get EAGAIN here then the monitor is not active
622 * so stop trying
623 */
624 rc = sysfs_set_num(sra, NULL, name, n);
625 }
626 sysfs_set_str(sra, NULL, "safe_mode_delay", safe);
627 return rc;
628}
629
47eb4d5a
N
630int start_reshape(struct mdinfo *sra)
631{
632 int err;
0f28668b 633 sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
47eb4d5a
N
634 err = sysfs_set_num(sra, NULL, "suspend_hi", 0);
635 err = err ?: sysfs_set_num(sra, NULL, "suspend_lo", 0);
18eaf9e5
N
636 /* Setting sync_min can fail if the recovery is already 'running',
637 * which can happen when restarting an array which is reshaping.
638 * So don't worry about errors here */
639 sysfs_set_num(sra, NULL, "sync_min", 0);
47eb4d5a
N
640 err = err ?: sysfs_set_num(sra, NULL, "sync_max", 0);
641 err = err ?: sysfs_set_str(sra, NULL, "sync_action", "reshape");
642
643 return err;
644}
645
646void abort_reshape(struct mdinfo *sra)
647{
648 sysfs_set_str(sra, NULL, "sync_action", "idle");
649 sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
650 sysfs_set_num(sra, NULL, "suspend_hi", 0);
651 sysfs_set_num(sra, NULL, "suspend_lo", 0);
652 sysfs_set_num(sra, NULL, "sync_min", 0);
653 sysfs_set_str(sra, NULL, "sync_max", "max");
654}
655
62a48395
AK
656int remove_disks_on_raid10_to_raid0_takeover(struct supertype *st,
657 struct mdinfo *sra,
658 int layout)
659{
660 int nr_of_copies;
661 struct mdinfo *remaining;
662 int slot;
663
664 nr_of_copies = layout & 0xff;
665
666 remaining = sra->devs;
667 sra->devs = NULL;
668 /* for each 'copy', select one device and remove from the list. */
669 for (slot = 0; slot < sra->array.raid_disks; slot += nr_of_copies) {
670 struct mdinfo **diskp;
671 int found = 0;
672
673 /* Find a working device to keep */
674 for (diskp = &remaining; *diskp ; diskp = &(*diskp)->next) {
675 struct mdinfo *disk = *diskp;
676
677 if (disk->disk.raid_disk < slot)
678 continue;
679 if (disk->disk.raid_disk >= slot + nr_of_copies)
680 continue;
681 if (disk->disk.state & (1<<MD_DISK_REMOVED))
682 continue;
683 if (disk->disk.state & (1<<MD_DISK_FAULTY))
684 continue;
685 if (!(disk->disk.state & (1<<MD_DISK_SYNC)))
686 continue;
687
688 /* We have found a good disk to use! */
689 *diskp = disk->next;
690 disk->next = sra->devs;
691 sra->devs = disk;
692 found = 1;
693 break;
694 }
695 if (!found)
696 break;
697 }
698
699 if (slot < sra->array.raid_disks) {
700 /* didn't find all slots */
701 struct mdinfo **e;
702 e = &remaining;
703 while (*e)
704 e = &(*e)->next;
705 *e = sra->devs;
706 sra->devs = remaining;
707 return 1;
708 }
709
710 /* Remove all 'remaining' devices from the array */
711 while (remaining) {
712 struct mdinfo *sd = remaining;
713 remaining = sd->next;
714
715 sysfs_set_str(sra, sd, "state", "faulty");
716 sysfs_set_str(sra, sd, "slot", "none");
717 sysfs_set_str(sra, sd, "state", "remove");
718 sd->disk.state |= (1<<MD_DISK_REMOVED);
719 sd->disk.state &= ~(1<<MD_DISK_SYNC);
720 sd->next = sra->devs;
721 sra->devs = sd;
722 }
723 return 0;
724}
725
130994cb
AK
726void reshape_free_fdlist(int *fdlist,
727 unsigned long long *offsets,
728 int size)
729{
730 int i;
731
732 for (i = 0; i < size; i++)
733 if (fdlist[i] >= 0)
734 close(fdlist[i]);
735
736 free(fdlist);
737 free(offsets);
738}
739
740int reshape_prepare_fdlist(char *devname,
741 struct mdinfo *sra,
742 int raid_disks,
743 int nrdisks,
744 unsigned long blocks,
745 char *backup_file,
746 int *fdlist,
747 unsigned long long *offsets)
748{
749 int d = 0;
750 struct mdinfo *sd;
751
752 for (d = 0; d <= nrdisks; d++)
753 fdlist[d] = -1;
754 d = raid_disks;
755 for (sd = sra->devs; sd; sd = sd->next) {
756 if (sd->disk.state & (1<<MD_DISK_FAULTY))
757 continue;
758 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
759 char *dn = map_dev(sd->disk.major,
760 sd->disk.minor, 1);
761 fdlist[sd->disk.raid_disk]
762 = dev_open(dn, O_RDONLY);
763 offsets[sd->disk.raid_disk] = sd->data_offset*512;
764 if (fdlist[sd->disk.raid_disk] < 0) {
765 fprintf(stderr,
766 Name ": %s: cannot open component %s\n",
767 devname, dn ? dn : "-unknown-");
768 d = -1;
769 goto release;
770 }
771 } else if (backup_file == NULL) {
772 /* spare */
773 char *dn = map_dev(sd->disk.major,
774 sd->disk.minor, 1);
775 fdlist[d] = dev_open(dn, O_RDWR);
776 offsets[d] = (sd->data_offset + sra->component_size - blocks - 8)*512;
777 if (fdlist[d] < 0) {
778 fprintf(stderr, Name ": %s: cannot open component %s\n",
779 devname, dn ? dn : "-unknown-");
780 d = -1;
781 goto release;
782 }
783 d++;
784 }
785 }
786release:
787 return d;
788}
789
e6e9d47b
AK
790int reshape_open_backup_file(char *backup_file,
791 int fd,
792 char *devname,
793 long blocks,
794 int *fdlist,
795 unsigned long long *offsets)
796{
797 /* Return 1 on success, 0 on any form of failure */
798 /* need to check backup file is large enough */
799 char buf[512];
800 struct stat stb;
801 unsigned int dev;
802 int i;
803
804 *fdlist = open(backup_file, O_RDWR|O_CREAT|O_EXCL,
805 S_IRUSR | S_IWUSR);
806 *offsets = 8 * 512;
807 if (*fdlist < 0) {
808 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
809 devname, backup_file, strerror(errno));
810 return 0;
811 }
812 /* Guard against backup file being on array device.
813 * If array is partitioned or if LVM etc is in the
814 * way this will not notice, but it is better than
815 * nothing.
816 */
817 fstat(*fdlist, &stb);
818 dev = stb.st_dev;
819 fstat(fd, &stb);
820 if (stb.st_rdev == dev) {
821 fprintf(stderr, Name ": backup file must NOT be"
822 " on the array being reshaped.\n");
823 close(*fdlist);
824 return 0;
825 }
826
827 memset(buf, 0, 512);
828 for (i=0; i < blocks + 1 ; i++) {
829 if (write(*fdlist, buf, 512) != 512) {
830 fprintf(stderr, Name ": %s: cannot create"
831 " backup file %s: %s\n",
832 devname, backup_file, strerror(errno));
833 return 0;
834 }
835 }
836 if (fsync(*fdlist) != 0) {
837 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
838 devname, backup_file, strerror(errno));
839 return 0;
840 }
841
842 return 1;
843}
844
1c009fc2
AK
845unsigned long compute_backup_blocks(int nchunk, int ochunk,
846 unsigned int ndata, unsigned int odata)
847{
848 unsigned long a, b, blocks;
849 /* So how much do we need to backup.
850 * We need an amount of data which is both a whole number of
851 * old stripes and a whole number of new stripes.
852 * So LCM for (chunksize*datadisks).
853 */
854 a = (ochunk/512) * odata;
855 b = (nchunk/512) * ndata;
856 /* Find GCD */
857 while (a != b) {
858 if (a < b)
859 b -= a;
860 if (b < a)
861 a -= b;
862 }
863 /* LCM == product / GCD */
864 blocks = (ochunk/512) * (nchunk/512) * odata * ndata / a;
865
866 return blocks;
867}
868
5da9ab98
N
869char *analyse_change(struct mdinfo *info, struct reshape *re)
870{
871 /* Based on the current array state in info->array and
872 * the changes in info->new_* etc, determine:
873 * - whether the change is possible
874 * - Intermediate level/raid_disks/layout
875 * - whether a restriping reshape is needed
876 * - number of sectors in minimum change unit. This
877 * will cover a whole number of stripes in 'before' and
878 * 'after'.
879 *
880 * Return message if the change should be rejected
881 * NULL if the change can be achieved
882 *
883 * This can be called as part of starting a reshape, or
884 * when assembling an array that is undergoing reshape.
885 */
886 int new_disks;
887
888 /* If a new level not explicitly given, we assume no-change */
889 if (info->new_level == UnSet)
890 info->new_level = info->array.level;
891
892 if (info->new_chunk)
893 switch (info->new_level) {
894 case 0:
895 case 4:
896 case 5:
897 case 6:
898 case 10:
899 /* chunk size is meaningful, must divide component_size
900 * evenly
901 */
902 if (info->component_size % (info->new_chunk/512))
903 return "New chunk size does not"
904 " divide component size";
905 break;
906 default:
907 return "chunk size not meaningful for this level";
908 }
909 else
910 info->new_chunk = info->array.chunk_size;
911
912 switch (info->array.level) {
913 case 1:
914 /* RAID1 can convert to RAID1 with different disks, or
915 * raid5 with 2 disks
916 */
917 if (info->new_level == 1) {
918 if (info->delta_disks == UnSet)
919 /* Don't know what to do */
920 return "no change requested for Growing RAID1";
921 re->level = 1;
922 re->before.data_disks = (info->array.raid_disks +
923 info->delta_disks);
924 re->before.layout = 0;
b4f8e38b 925 re->backup_blocks = 0;
5da9ab98
N
926 re->parity = 0;
927 return NULL;
928 }
929 if (info->array.raid_disks == 2 &&
930 info->array.raid_disks == 5) {
931 /* simple in-place conversion */
932 re->level = 5;
933 re->parity = 1;
934 re->before.data_disks = 1;
935 re->before.layout = ALGORITHM_LEFT_SYMMETRIC;
b4f8e38b 936 re->backup_blocks = 0;
5da9ab98
N
937 return NULL;
938 }
939 /* Could do some multi-stage conversions, but leave that to
940 * later.
941 */
942 return "Impossibly level change request for RAID1";
943
944 case 10:
945 /* RAID10 can only be converted from near mode to
946 * RAID0 by removing some devices
947 */
948 if ((info->array.layout & ~0xff) != 0x100)
949 return "Cannot Grow RAID10 with far/offset layout";
950 /* number of devices must be multiple of number of copies */
951 if (info->array.raid_disks % (info->array.layout & 0xff))
952 return "RAID10 layout too complex for Grow operation";
953
954 if (info->new_level != 0)
955 return "RAID10 can only be changed to RAID0";
956 new_disks = (info->array.raid_disks
957 / (info->array.layout & 0xff));
5dad078d 958 if (info->delta_disks == UnSet) {
5da9ab98
N
959 info->delta_disks = (new_disks
960 - info->array.raid_disks);
961 }
962 if (info->delta_disks != new_disks - info->array.raid_disks)
963 return "New number of raid-devices impossible for RAID10";
964 if (info->new_chunk &&
965 info->new_chunk != info->array.chunk_size)
966 return "Cannot change chunk-size with RAID10 Grow";
967
968 /* looks good */
969 re->level = 0;
970 re->parity = 0;
971 re->before.data_disks = new_disks;
972 re->before.layout = 0;
b4f8e38b 973 re->backup_blocks = 0;
5da9ab98
N
974 return NULL;
975
976 case 0:
977 /* RAID0 can be converted to RAID10, or to RAID456 */
978 if (info->new_level == 10) {
979 if (info->new_layout == UnSet && info->delta_disks == UnSet) {
980 /* Assume near=2 layout */
981 info->new_layout = 0x102;
982 info->delta_disks = info->array.raid_disks;
983 }
984 if (info->new_layout == UnSet) {
985 int copies = 1 + (info->delta_disks
986 / info->array.raid_disks);
987 if (info->array.raid_disks * (copies-1)
988 != info->delta_disks)
989 return "Impossible number of devices"
990 " for RAID0->RAID10";
991 info->new_layout = 0x100 + copies;
992 }
993 if (info->delta_disks == UnSet) {
994 int copies = info->new_layout & 0xff;
995 if (info->new_layout != 0x100 + copies)
996 return "New layout impossible"
997 " for RAID0->RAID10";;
998 info->delta_disks = (copies - 1) *
999 info->array.raid_disks;
1000 }
1001 if (info->new_chunk &&
1002 info->new_chunk != info->array.chunk_size)
1003 return "Cannot change chunk-size with RAID0->RAID10";
1004 /* looks good */
1005 re->level = 10;
1006 re->parity = 0;
1007 re->before.data_disks = (info->array.raid_disks +
1008 info->delta_disks);
1009 re->before.layout = info->new_layout;
b4f8e38b 1010 re->backup_blocks = 0;
5da9ab98
N
1011 return NULL;
1012 }
1013
1014 /* RAID0 can also covert to RAID0/4/5/6 by first converting to
1015 * a raid4 style layout of the final level.
1016 */
1017 switch (info->new_level) {
1018 case 0:
1019 case 4:
1020 re->level = 4;
1021 re->before.layout = 0;
1022 break;
1023 case 5:
1024 re->level = 5;
1025 re->before.layout = ALGORITHM_PARITY_N;
1026 break;
1027 case 6:
1028 re->level = 6;
1029 re->before.layout = ALGORITHM_PARITY_N;
1030 break;
1031 default:
1032 return "Impossible level change requested";
1033 }
1034 re->before.data_disks = info->array.raid_disks;
1035 /* determining 'after' layout happens outside this 'switch' */
1036 break;
1037
1038 case 4:
1039 info->array.layout = ALGORITHM_PARITY_N;
1040 case 5:
1041 switch (info->new_level) {
1042 case 4:
1043 re->level = info->array.level;
1044 re->before.data_disks = info->array.raid_disks - 1;
1045 re->before.layout = info->array.layout;
1046 break;
1047 case 5:
1048 re->level = 5;
1049 re->before.data_disks = info->array.raid_disks - 1;
1050 re->before.layout = info->array.layout;
1051 break;
1052 case 6:
1053 re->level = 6;
1054 re->before.data_disks = info->array.raid_disks - 1;
1055 switch (info->array.layout) {
1056 case ALGORITHM_LEFT_ASYMMETRIC:
1057 re->before.layout = ALGORITHM_LEFT_ASYMMETRIC_6;
1058 break;
1059 case ALGORITHM_RIGHT_ASYMMETRIC:
1060 re->before.layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
1061 break;
1062 case ALGORITHM_LEFT_SYMMETRIC:
1063 re->before.layout = ALGORITHM_LEFT_SYMMETRIC_6;
1064 break;
1065 case ALGORITHM_RIGHT_SYMMETRIC:
1066 re->before.layout = ALGORITHM_RIGHT_SYMMETRIC_6;
1067 break;
1068 case ALGORITHM_PARITY_0:
1069 re->before.layout = ALGORITHM_PARITY_0_6;
1070 break;
1071 case ALGORITHM_PARITY_N:
1072 re->before.layout = ALGORITHM_PARITY_N_6;
1073 break;
1074 default:
1075 return "Cannot convert an array with this layout";
1076 }
1077 break;
1078 case 1:
1079 if (info->array.raid_disks != 2)
1080 return "Can only convert a 2-device array to RAID1";
1081 re->level = 1;
1082 re->before.data_disks = 2;
1083 re->before.layout = 0;
1084 break;
1085 default:
1086 return "Impossible level change requested";
1087 }
1088 break;
1089 case 6:
1090 switch (info->new_level) {
1091 case 4:
1092 case 5:
1093 case 6:
1094 re->level = 6;
1095 re->before.data_disks = info->array.raid_disks - 2;
1096 re->before.layout = info->array.layout;
1097 break;
1098 default:
1099 return "Impossible level change requested";
1100 }
1101 break;
1102 }
1103
1104 /* If we reached here then it looks like a re-stripe is
1105 * happening. We have determined the intermediate level
1106 * and initial raid_disks/layout and stored these in 're'.
1107 *
1108 * We need to deduce the final layout that can be atomically
1109 * converted to the end state.
1110 */
1111 switch (info->new_level) {
1112 case 0:
1113 /* We can only get to RAID0 from RAID4 or RAID5
1114 * with appropriate layout and one extra device
1115 */
1116 if (re->level != 4 && re->level != 5)
1117 return "Cannot covert to RAID0 from this level";
1118 if (info->delta_disks == UnSet)
1119 re->after.data_disks = re->before.data_disks;
1120 else
1121 re->after.data_disks =
1122 info->array.raid_disks + info->delta_disks;
1123 switch (re->level) {
1124 case 4:
1125 re->after.layout = 0 ; break;
1126 case 5:
1127 re->after.layout = ALGORITHM_PARITY_N; break;
1128 }
1129 break;
1130
1131 case 4:
1132 /* We can only get to RAID4 from RAID5 */
1133 if (re->level != 4 && re->level != 5)
1134 return "Cannot convert to RAID4 from this level";
1135 if (info->delta_disks == UnSet)
1136 re->after.data_disks = re->before.data_disks;
1137 else
1138 re->after.data_disks =
1139 re->before.data_disks + info->delta_disks;
1140 switch (re->level) {
1141 case 4:
1142 re->after.layout = 0 ; break;
1143 case 5:
1144 re->after.layout = ALGORITHM_PARITY_N; break;
1145 }
1146 break;
1147
1148 case 5:
1149 /* We get to RAID5 for RAID5 or RAID6 */
1150 if (re->level != 5 && re->level != 6)
1151 return "Cannot convert to RAID5 from this level";
1152 if (info->delta_disks == UnSet)
1153 re->after.data_disks = re->before.data_disks;
1154 else if (re->level == 5)
1155 re->after.data_disks =
1156 re->before.data_disks + info->delta_disks;
1157 else
1158 re->after.data_disks =
1159 info->array.raid_disks + info->delta_disks - 1;
1160 switch (re->level) {
1161 case 5:
1162 if (info->new_layout == UnSet)
1163 re->after.layout = re->before.layout;
1164 else
1165 re->after.layout = info->new_layout;
1166 break;
1167 case 6:
4a870364
N
1168 if (info->new_layout == UnSet)
1169 info->new_layout = re->before.layout;
1170
5da9ab98
N
1171 /* after.layout needs to be raid6 version of new_layout */
1172 if (info->new_layout == ALGORITHM_PARITY_N)
1173 re->after.layout = ALGORITHM_PARITY_N;
1174 else {
1175 char layout[40];
1176 char *ls = map_num(r5layout, info->new_layout);
1177 int l;
1178 strcat(strcpy(layout, ls), "-6");
1179 l = map_name(r6layout, layout);
1180 if (l == UnSet)
1181 return "Cannot find RAID6 layout"
1182 " to convert to";
1183 re->after.layout = l;
1184 }
1185 }
1186 break;
1187
1188 case 6:
1189 /* We must already be at level 6 */
1190 if (re->level != 6)
1191 return "Impossible level change";
1192 if (info->delta_disks == UnSet)
1193 re->after.data_disks = re->before.data_disks;
1194 else
1195 re->after.data_disks = (info->array.raid_disks +
1196 info->delta_disks) - 2;
1197 if (info->new_layout == UnSet)
4a870364 1198 re->after.layout = info->array.layout;
5da9ab98
N
1199 else
1200 re->after.layout = info->new_layout;
1201 break;
1202 default:
1203 return "Impossible level change requested";
1204 }
1205 switch (re->level) {
1206 case 6: re->parity = 2; break;
1207 case 4:
1208 case 5: re->parity = 1; break;
1209 default: re->parity = 0; break;
1210 }
1211 /* So we have a restripe operation, we need to calculate the number
1212 * of blocks per reshape operation.
1213 */
1214 if (info->new_chunk == 0)
1215 info->new_chunk = info->array.chunk_size;
1216 if (re->after.data_disks == re->before.data_disks &&
1217 re->after.layout == re->before.layout &&
1218 info->new_chunk == info->array.chunk_size) {
1219 /* Nothing to change */
b4f8e38b 1220 re->backup_blocks = 0;
5da9ab98
N
1221 return NULL;
1222 }
1223 if (re->after.data_disks == 1 && re->before.data_disks == 1) {
1224 /* chunks can layout changes make no difference */
b4f8e38b 1225 re->backup_blocks = 0;
5da9ab98
N
1226 return NULL;
1227 }
1228
1229 if (re->after.data_disks == re->before.data_disks &&
1230 get_linux_version() < 2006032)
1231 return "in-place reshape is not safe before 2.6.32 - sorry.";
1232
1233 if (re->after.data_disks < re->before.data_disks &&
1234 get_linux_version() < 2006030)
1235 return "reshape to fewer devices is not supported before 2.6.32 - sorry.";
1236
b4f8e38b 1237 re->backup_blocks = compute_backup_blocks(
5da9ab98
N
1238 info->new_chunk, info->array.chunk_size,
1239 re->after.data_disks,
1240 re->before.data_disks);
1241
1242 re->new_size = info->component_size * re->after.data_disks;
1243 return NULL;
1244}
1245
1246static int reshape_array(char *container, int fd, char *devname,
1247 struct supertype *st, struct mdinfo *info,
1248 int force, char *backup_file, int quiet, int forked);
1249static int reshape_container(char *container, int cfd, char *devname,
1250 struct supertype *st,
1251 struct mdinfo *info,
1252 int force,
1253 char *backup_file,
1254 int quiet);
1c009fc2 1255
06b0d786 1256int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
e86c9dd6 1257 long long size,
691a36b7
N
1258 int level, char *layout_str, int chunksize, int raid_disks,
1259 int force)
e86c9dd6
NB
1260{
1261 /* Make some changes in the shape of an array.
1262 * The kernel must support the change.
7236ee7a
N
1263 *
1264 * There are three different changes. Each can trigger
1265 * a resync or recovery so we freeze that until we have
1266 * requested everything (if kernel supports freezing - 2.6.30).
1267 * The steps are:
1268 * - change size (i.e. component_size)
1269 * - change level
1270 * - change layout/chunksize/ndisks
1271 *
1272 * The last can require a reshape. It is different on different
1273 * levels so we need to check the level before actioning it.
1274 * Some times the level change needs to be requested after the
1275 * reshape (e.g. raid6->raid5, raid5->raid0)
1276 *
e86c9dd6 1277 */
5da9ab98 1278 struct mdu_array_info_s array;
7236ee7a 1279 int rv = 0;
e86c9dd6 1280 struct supertype *st;
4725bc31 1281 char *subarray = NULL;
e86c9dd6 1282
7236ee7a 1283 int frozen;
7236ee7a 1284 int changed = 0;
7bc71196 1285 char *container = NULL;
5da9ab98 1286 char container_buf[20];
7bc71196 1287 int cfd = -1;
e86c9dd6 1288
5da9ab98 1289 struct mdinfo info;
7e0f6979 1290 struct mdinfo *sra;
e86c9dd6
NB
1291
1292 if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) {
1293 fprintf(stderr, Name ": %s is not an active md array - aborting\n",
1294 devname);
1295 return 1;
1296 }
24d40069 1297
9ce510be
N
1298 if (size >= 0 &&
1299 (chunksize || level!= UnSet || layout_str || raid_disks)) {
1300 fprintf(stderr, Name ": cannot change component size at the same time "
1301 "as other changes.\n"
1302 " Change size first, then check data is intact before "
1303 "making other changes.\n");
1304 return 1;
1305 }
1306
24d40069
N
1307 if (raid_disks && raid_disks < array.raid_disks && array.level > 1 &&
1308 get_linux_version() < 2006032 &&
1309 !check_env("MDADM_FORCE_FEWER")) {
1310 fprintf(stderr, Name ": reducing the number of devices is not safe before Linux 2.6.32\n"
1311 " Please use a newer kernel\n");
1312 return 1;
1313 }
7bc71196
DW
1314
1315 st = super_by_fd(fd, &subarray);
1316 if (!st) {
1317 fprintf(stderr, Name ": Unable to determine metadata format for %s\n", devname);
1318 return 1;
1319 }
acab7bb1
N
1320 if (raid_disks > st->max_devs) {
1321 fprintf(stderr, Name ": Cannot increase raid-disks on this array"
1322 " beyond %d\n", st->max_devs);
1323 return 1;
1324 }
7bc71196
DW
1325
1326 /* in the external case we need to check that the requested reshape is
1327 * supported, and perform an initial check that the container holds the
1328 * pre-requisite spare devices (mdmon owns final validation)
1329 */
1330 if (st->ss->external) {
1331 int container_dev;
7f2ba464 1332 int rv;
7bc71196
DW
1333
1334 if (subarray) {
1335 container_dev = st->container_dev;
1336 cfd = open_dev_excl(st->container_dev);
7bc71196
DW
1337 } else {
1338 container_dev = st->devnum;
1339 close(fd);
1340 cfd = open_dev_excl(st->devnum);
1341 fd = cfd;
1342 }
1343 if (cfd < 0) {
1344 fprintf(stderr, Name ": Unable to open container for %s\n",
1345 devname);
7f2ba464 1346 free(subarray);
7bc71196
DW
1347 return 1;
1348 }
1349
5da9ab98
N
1350 fmt_devname(container_buf, container_dev);
1351 container = container_buf;
7bc71196 1352
eb744788
AK
1353 rv = st->ss->load_container(st, cfd, NULL);
1354
7f2ba464 1355 if (rv) {
7bc71196
DW
1356 fprintf(stderr, Name ": Cannot read superblock for %s\n",
1357 devname);
7f2ba464 1358 free(subarray);
7bc71196
DW
1359 return 1;
1360 }
1361
1362 if (mdmon_running(container_dev))
1363 st->update_tail = &st->updates;
691a36b7
N
1364 }
1365
1366 if (raid_disks > array.raid_disks &&
1367 array.spare_disks < (raid_disks - array.raid_disks) &&
1368 !force) {
1369 fprintf(stderr,
1370 Name ": Need %d spare%s to avoid degraded array,"
1371 " and only have %d.\n"
1372 " Use --force to over-ride this check.\n",
1373 raid_disks - array.raid_disks,
1374 raid_disks - array.raid_disks == 1 ? "" : "s",
1375 array.spare_disks);
1376 return 1;
7bc71196
DW
1377 }
1378
3fa436ac
ML
1379 sra = sysfs_read(fd, 0, GET_LEVEL | GET_DISKS | GET_DEVS
1380 | GET_STATE | GET_VERSION);
1381 if (sra) {
7f2ba464 1382 if (st->ss->external && subarray == NULL) {
7bc71196
DW
1383 array.level = LEVEL_CONTAINER;
1384 sra->array.level = LEVEL_CONTAINER;
1385 }
7bc71196 1386 } else {
b526e52d
DW
1387 fprintf(stderr, Name ": failed to read sysfs parameters for %s\n",
1388 devname);
1389 return 1;
1390 }
7f2ba464
DW
1391 frozen = freeze(st);
1392 if (frozen < -1) {
1393 /* freeze() already spewed the reason */
1394 return 1;
1395 } else if (frozen < 0) {
7236ee7a
N
1396 fprintf(stderr, Name ": %s is performing resync/recovery and cannot"
1397 " be reshaped\n", devname);
1398 return 1;
1399 }
1400
1401 /* ========= set size =============== */
1402 if (size >= 0 && (size == 0 || size != array.size)) {
7bc71196
DW
1403 long long orig_size = array.size;
1404
1405 if (reshape_super(st, size, UnSet, UnSet, 0, 0, NULL, devname, !quiet)) {
1406 rv = 1;
1407 goto release;
1408 }
1409 sync_metadata(st);
7236ee7a
N
1410 array.size = size;
1411 if (array.size != size) {
1412 /* got truncated to 32bit, write to
1413 * component_size instead
1414 */
1415 if (sra)
1416 rv = sysfs_set_num(sra, NULL,
1417 "component_size", size);
1418 else
1419 rv = -1;
1420 } else
1421 rv = ioctl(fd, SET_ARRAY_INFO, &array);
1422 if (rv != 0) {
39bbb392 1423 int err = errno;
7bc71196
DW
1424
1425 /* restore metadata */
1426 if (reshape_super(st, orig_size, UnSet, UnSet, 0, 0,
1427 NULL, devname, !quiet) == 0)
1428 sync_metadata(st);
7236ee7a 1429 fprintf(stderr, Name ": Cannot set device size for %s: %s\n",
39bbb392
N
1430 devname, strerror(err));
1431 if (err == EBUSY &&
1432 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
1433 fprintf(stderr, " Bitmap must be removed before size can be changed\n");
7236ee7a
N
1434 rv = 1;
1435 goto release;
1436 }
1437 ioctl(fd, GET_ARRAY_INFO, &array);
be1cabbd 1438 size = get_component_size(fd)/2;
f98841b3
N
1439 if (size == 0)
1440 size = array.size;
7236ee7a 1441 if (!quiet)
f98841b3
N
1442 fprintf(stderr, Name ": component size of %s has been set to %lluK\n",
1443 devname, size);
7236ee7a 1444 changed = 1;
7bc71196 1445 } else if (array.level != LEVEL_CONTAINER) {
be1cabbd 1446 size = get_component_size(fd)/2;
f98841b3
N
1447 if (size == 0)
1448 size = array.size;
7236ee7a
N
1449 }
1450
62a48395 1451 /* ========= check for Raid10 -> Raid0 conversion ===============
5da9ab98 1452 * current implementation assumes that following conditions must be met:
62a48395
AK
1453 * - far_copies == 1
1454 * - near_copies == 2
1455 */
5da9ab98 1456 if (level == 0 && array.level == 10 && sra &&
62a48395
AK
1457 array.layout == ((1 << 8) + 2) && !(array.raid_disks & 1)) {
1458 int err;
1459 err = remove_disks_on_raid10_to_raid0_takeover(st, sra, array.layout);
1460 if (err) {
1461 dprintf(Name": Array cannot be reshaped\n");
62a48395
AK
1462 if (cfd > -1)
1463 close(cfd);
5da9ab98
N
1464 rv = 1;
1465 goto release;
7236ee7a
N
1466 }
1467 }
1468
5da9ab98 1469 info.array = array;
7443ee81 1470 sysfs_init(&info, fd, NoMdDev);
3fa436ac 1471 strcpy(info.text_version, sra->text_version);
7443ee81 1472 info.component_size = size*2;
5da9ab98
N
1473 info.new_level = level;
1474 info.new_chunk = chunksize * 1024;
1475 if (raid_disks)
1476 info.delta_disks = raid_disks - info.array.raid_disks;
1477 else
1478 info.delta_disks = UnSet;
1479 if (layout_str == NULL) {
1480 info.new_layout = UnSet;
1481 if (info.array.level == 6 &&
1482 (info.new_level == 6 || info.new_level == UnSet) &&
1483 info.array.layout >= 16) {
7236ee7a 1484 fprintf(stderr, Name
5da9ab98
N
1485 ": %s has a non-standard layout. If you"
1486 " wish to preserve this\n"
1487 " during the reshape, please specify"
1488 " --layout=preserve\n"
1489 " If you want to change it, specify a"
1490 " layout or use --layout=normalise\n",
7236ee7a
N
1491 devname);
1492 rv = 1;
1493 goto release;
1494 }
5da9ab98
N
1495 } else if (strcmp(layout_str, "normalise") == 0 ||
1496 strcmp(layout_str, "normalize") == 0) {
1497 /* If we have a -6 RAID6 layout, remove the '-6'. */
1498 info.new_layout = UnSet;
1499 if (info.array.level == 6 && info.new_level == UnSet) {
1500 char l[40], *h;
1501 strcpy(l, map_num(r6layout, info.array.layout));
1502 h = strrchr(l, '-');
1503 if (h && strcmp(h, "-6") == 0) {
1504 *h = 0;
1505 info.new_layout = map_name(r6layout, l);
7236ee7a
N
1506 }
1507 }
5da9ab98
N
1508 } else if (strcmp(layout_str, "preserve") == 0) {
1509 info.new_layout = UnSet;
1510 } else {
1511 int l = info.new_level;
1512 if (l == UnSet)
1513 l = info.array.level;
1514 switch (l) {
1515 case 5:
1516 info.new_layout = map_name(r5layout, layout_str);
1517 break;
1518 case 6:
1519 info.new_layout = map_name(r6layout, layout_str);
1520 break;
1521 case 10:
1522 info.new_layout = parse_layout_10(layout_str);
1523 break;
1524 case LEVEL_FAULTY:
1525 info.new_layout = parse_layout_faulty(layout_str);
1526 break;
1527 default:
1528 fprintf(stderr, Name ": layout not meaningful"
1529 " with this level\n");
7bc71196
DW
1530 rv = 1;
1531 goto release;
1532 }
5da9ab98
N
1533 if (info.new_layout == UnSet) {
1534 fprintf(stderr, Name ": layout %s not understood"
1535 " for this level\n",
1536 layout_str);
1537 rv = 1;
1538 goto release;
7bc71196 1539 }
7236ee7a
N
1540 }
1541
5da9ab98
N
1542 if (array.level == LEVEL_CONTAINER) {
1543 /* This change is to be applied to every array in the
1544 * container. This is only needed when the metadata imposes
1545 * restraints of the various arrays in the container.
1546 * Currently we only know that IMSM requires all arrays
1547 * to have the same number of devices so changing the
1548 * number of devices (On-Line Capacity Expansion) must be
1549 * performed at the level of the container
1550 */
1551 rv = reshape_container(container, fd, devname, st, &info,
1552 force, backup_file, quiet);
9202b8eb 1553 frozen = 0;
5da9ab98
N
1554 } else {
1555 /* Impose these changes on a single array. First
1556 * check that the metadata is OK with the change. */
7bc71196 1557
5da9ab98
N
1558 if (reshape_super(st, info.component_size, info.new_level,
1559 info.new_layout, info.new_chunk,
1560 info.array.raid_disks + info.delta_disks,
1561 backup_file, devname, quiet)) {
7bc71196
DW
1562 rv = 1;
1563 goto release;
1564 }
5da9ab98
N
1565 sync_metadata(st);
1566 rv = reshape_array(container, fd, devname, st, &info, force,
1567 backup_file, quiet, 0);
9202b8eb 1568 frozen = 0;
5da9ab98
N
1569 }
1570release:
9202b8eb
N
1571 if (frozen > 0)
1572 unfreeze(st);
5da9ab98
N
1573 return rv;
1574}
7bc71196 1575
5da9ab98
N
1576static int reshape_array(char *container, int fd, char *devname,
1577 struct supertype *st, struct mdinfo *info,
1578 int force,
1579 char *backup_file, int quiet, int forked)
1580{
1581 struct reshape reshape;
1582 int spares_needed;
1583 char *msg;
1584 int orig_level = UnSet;
1585 int disks, odisks;
7bc71196 1586
5da9ab98
N
1587 struct mdu_array_info_s array;
1588 char *c;
e86c9dd6 1589
5da9ab98
N
1590 int *fdlist;
1591 unsigned long long *offsets;
1592 int d;
1593 int nrdisks;
1594 int err;
da8100ca 1595 unsigned long blocks;
5da9ab98
N
1596 unsigned long cache;
1597 unsigned long long array_size;
1598 int done;
da8100ca 1599 struct mdinfo *sra;
7bc71196 1600
5da9ab98
N
1601 msg = analyse_change(info, &reshape);
1602 if (msg) {
1603 fprintf(stderr, Name ": %s\n", msg);
631d7405 1604 goto release;
5da9ab98 1605 }
9ff87c16
AK
1606 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
1607 dprintf("Canot get array information.\n");
631d7405 1608 goto release;
9ff87c16 1609 }
5da9ab98
N
1610 spares_needed = max(reshape.before.data_disks,
1611 reshape.after.data_disks)
1612 + reshape.parity - array.raid_disks;
7bc71196 1613
88c1a083
N
1614 if (!force &&
1615 info->new_level > 0 &&
1616 spares_needed > info->array.spare_disks) {
5da9ab98
N
1617 fprintf(stderr,
1618 Name ": Need %d spare%s to avoid degraded array,"
1619 " and only have %d.\n"
1620 " Use --force to over-ride this check.\n",
1621 spares_needed,
1622 spares_needed == 1 ? "" : "s",
1623 info->array.spare_disks);
631d7405 1624 goto release;
7bc71196 1625 }
e86c9dd6 1626
5da9ab98
N
1627 if (reshape.level != info->array.level) {
1628 char *c = map_num(pers, reshape.level);
1629 int err;
1630 if (c == NULL)
631d7405 1631 goto release;
e86c9dd6 1632
5da9ab98
N
1633 err = sysfs_set_str(info, NULL, "level", c);
1634 if (err) {
1635 err = errno;
1636 fprintf(stderr, Name ": %s: could not set level to %s\n",
1637 devname, c);
1638 if (err == EBUSY &&
1639 (info->array.state & (1<<MD_SB_BITMAP_PRESENT)))
1640 fprintf(stderr, " Bitmap must be removed"
1641 " before level can be changed\n");
631d7405 1642 goto release;
19678e53 1643 }
5da9ab98 1644 if (!quiet)
b6b95155 1645 fprintf(stderr, Name ": level of %s changed to %s\n",
5da9ab98
N
1646 devname, c);
1647 orig_level = info->array.level;
1648 }
e86c9dd6 1649
5da9ab98
N
1650 if (reshape.level > 0 && st->ss->external &&
1651 !mdmon_running(st->container_dev)) {
1652 start_mdmon(st->container_dev);
1653 ping_monitor(container);
1654 }
e86c9dd6 1655
5da9ab98
N
1656 /* ->reshape_super might have chosen some spares from the
1657 * container that it wants to be part of the new array.
1658 * We can collect them with ->container_content and give
1659 * them to the kernel.
1660 */
1661 if (st->ss->reshape_super && st->ss->container_content) {
1662 char *subarray = strchr(info->text_version+1, '/')+1;
1663 struct mdinfo *info2 =
1664 st->ss->container_content(st, subarray);
1665 struct mdinfo *d;
1666
2dd0d697
AK
1667 if (info2) {
1668 sysfs_init(info2, fd, st->devnum);
5da9ab98
N
1669 for (d = info2->devs; d; d = d->next) {
1670 if (d->disk.state == 0 &&
1671 d->disk.raid_disk >= 0) {
1672 /* This is a spare that wants to
1673 * be part of the array.
1674 */
1675 add_disk(fd, st, info2, d);
1676 }
9860f271 1677 }
2dd0d697
AK
1678 sysfs_free(info2);
1679 }
5da9ab98 1680 }
1686dc25 1681
b4f8e38b 1682 if (reshape.backup_blocks == 0) {
5da9ab98
N
1683 /* No restriping needed, but we might need to impose
1684 * some more changes: layout, raid_disks, chunk_size
e86c9dd6 1685 */
5da9ab98
N
1686 if (info->new_layout != UnSet &&
1687 info->new_layout != info->array.layout) {
1688 info->array.layout = info->new_layout;
1689 if (ioctl(fd, SET_ARRAY_INFO, &info->array) != 0) {
1690 fprintf(stderr, Name ": failed to set new layout\n");
631d7405 1691 goto release;
5da9ab98
N
1692 } else if (!quiet)
1693 printf("layout for %s set to %d\n",
1694 devname, info->array.layout);
1695 }
1696 if (info->delta_disks != UnSet &&
1697 info->delta_disks != 0) {
1698 info->array.raid_disks += info->delta_disks;
1699 if (ioctl(fd, SET_ARRAY_INFO, &info->array) != 0) {
1700 fprintf(stderr, Name ": failed to set raid disks\n");
631d7405 1701 goto release;
5da9ab98
N
1702 } else if (!quiet)
1703 printf("raid_disks for %s set to %d\n",
1704 devname, info->array.raid_disks);
1705 }
1706 if (info->new_chunk != 0 &&
1707 info->new_chunk != info->array.chunk_size) {
1708 if (sysfs_set_num(info, NULL,
1709 "chunk_size", info->new_chunk) != 0) {
1710 fprintf(stderr, Name ": failed to set chunk size\n");
631d7405 1711 goto release;
5da9ab98
N
1712 } else if (!quiet)
1713 printf("chunk size for %s set to %d\n",
1714 devname, info->array.chunk_size);
4725bc31 1715 }
7236ee7a 1716
631d7405
N
1717 if (!forked)
1718 unfreeze(st);
1719 return 0;
5da9ab98 1720 }
19678e53 1721
5da9ab98
N
1722 /*
1723 * There are three possibilities.
1724 * 1/ The array will shrink.
1725 * We need to ensure the reshape will pause before reaching
1726 * the 'critical section'. We also need to fork and wait for
1727 * that to happen. When it does we
1728 * suspend/backup/complete/unfreeze
1729 *
1730 * 2/ The array will not change size.
1731 * This requires that we keep a backup of a sliding window
1732 * so that we can restore data after a crash. So we need
1733 * to fork and monitor progress.
1734 * In future we will allow the data_offset to change, so
1735 * a sliding backup becomes unnecessary.
1736 *
1737 * 3/ The array will grow. This is relatively easy.
1738 * However the kernel's restripe routines will cheerfully
1739 * overwrite some early data before it is safe. So we
1740 * need to make a backup of the early parts of the array
1741 * and be ready to restore it if rebuild aborts very early.
1742 * For externally managed metadata, we still need a forked
1743 * child to monitor the reshape and suspend IO over the region
1744 * that is being reshaped.
1745 *
1746 * We backup data by writing it to one spare, or to a
1747 * file which was given on command line.
1748 *
1749 * In each case, we first make sure that storage is available
1750 * for the required backup.
1751 * Then we:
1752 * - request the shape change.
1753 * - fork to handle backup etc.
1754 */
e86c9dd6 1755
5da9ab98
N
1756 /* Check that we can hold all the data */
1757 get_dev_size(fd, NULL, &array_size);
1758 if (reshape.new_size < (array_size/512)) {
1759 fprintf(stderr,
1760 Name ": this change will reduce the size of the array.\n"
1761 " use --grow --array-size first to truncate array.\n"
1762 " e.g. mdadm --grow %s --array-size %llu\n",
1763 devname, reshape.new_size/2);
5da9ab98
N
1764 goto release;
1765 }
7236ee7a 1766
5da9ab98 1767 sra = sysfs_read(fd, 0,
3656edcd 1768 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK|
5da9ab98 1769 GET_CACHE);
d2505cff 1770
5da9ab98
N
1771 if (!sra) {
1772 fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n",
1773 devname);
5da9ab98
N
1774 goto release;
1775 }
7236ee7a 1776
5da9ab98
N
1777 /* Decide how many blocks (sectors) for a reshape
1778 * unit. The number we have so far is just a minimum
1779 */
b4f8e38b 1780 blocks = reshape.backup_blocks;
5da9ab98
N
1781 if (reshape.before.data_disks ==
1782 reshape.after.data_disks) {
1783 /* Make 'blocks' bigger for better throughput, but
1784 * not so big that we reject it below.
1785 * Try for 16 megabytes
1786 */
1787 while (blocks * 32 < sra->component_size &&
1788 blocks < 16*1024*2)
1789 blocks *= 2;
1790 } else
1791 fprintf(stderr, Name ": Need to backup %luK of critical "
1792 "section..\n", blocks/2);
1793
1794 if (blocks >= sra->component_size/2) {
1795 fprintf(stderr, Name ": %s: Something wrong"
1796 " - reshape aborted\n",
1797 devname);
5da9ab98
N
1798 goto release;
1799 }
7236ee7a 1800
5da9ab98
N
1801 /* Now we need to open all these devices so we can read/write.
1802 */
1803 nrdisks = array.raid_disks + sra->array.spare_disks;
1804 fdlist = malloc((1+nrdisks) * sizeof(int));
1805 offsets = malloc((1+nrdisks) * sizeof(offsets[0]));
1806 if (!fdlist || !offsets) {
1807 fprintf(stderr, Name ": malloc failed: grow aborted\n");
5da9ab98
N
1808 goto release;
1809 }
e380d3be 1810
5da9ab98
N
1811 d = reshape_prepare_fdlist(devname, sra, array.raid_disks,
1812 nrdisks, blocks, backup_file,
1813 fdlist, offsets);
1814 if (d < 0) {
5da9ab98
N
1815 goto release;
1816 }
1817 if (backup_file == NULL) {
374ba335 1818 if (reshape.after.data_disks <= reshape.before.data_disks) {
5da9ab98
N
1819 fprintf(stderr,
1820 Name ": %s: Cannot grow - need backup-file\n",
c03ef02d 1821 devname);
5da9ab98
N
1822 goto release;
1823 } else if (sra->array.spare_disks == 0) {
1824 fprintf(stderr, Name ": %s: Cannot grow - need a spare or "
1825 "backup-file to backup critical section\n",
e86c9dd6 1826 devname);
5da9ab98 1827 goto release;
e86c9dd6 1828 }
5da9ab98
N
1829 } else {
1830 if (!reshape_open_backup_file(backup_file, fd, devname,
1831 (signed)blocks,
1832 fdlist+d, offsets+d)) {
130994cb 1833 goto release;
e86c9dd6 1834 }
5da9ab98
N
1835 d++;
1836 }
7236ee7a 1837
5da9ab98
N
1838 /* lastly, check that the internal stripe cache is
1839 * large enough, or it won't work.
1840 * It must hold at least 4 stripes of the larger
1841 * chunk size
1842 */
1843 cache = max(info->array.chunk_size, info->new_chunk);
1844 cache *= 4; /* 4 stripes minimum */
1845 cache /= 512; /* convert to sectors */
1846 disks = min(reshape.before.data_disks, reshape.after.data_disks);
1847 /* make sure there is room for 'blocks' with a bit to spare */
1848 if (cache < 16 + blocks / disks)
1849 cache = 16 + blocks / disks;
1850 cache /= (4096/512); /* Covert from sectors to pages */
1851
1852 if (sra->cache_size < cache)
1853 subarray_set_num(container, sra, "stripe_cache_size",
1854 cache+1);
1855
1856 /* Right, everything seems fine. Let's kick things off.
1857 * If only changing raid_disks, use ioctl, else use
1858 * sysfs.
1859 */
1860 sync_metadata(st);
1861
b4f8e38b
N
1862 sra->new_chunk = info->new_chunk;
1863
5da9ab98 1864 if (info->array.chunk_size == info->new_chunk &&
7477d513
AK
1865 reshape.before.layout == reshape.after.layout &&
1866 st->ss->external == 0) {
5da9ab98
N
1867 array.raid_disks = reshape.after.data_disks + reshape.parity;
1868 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
1869 int err = errno;
631d7405 1870
5da9ab98
N
1871 fprintf(stderr,
1872 Name ": Cannot set device shape for %s: %s\n",
1873 devname, strerror(errno));
7bc71196 1874
5da9ab98
N
1875 if (err == EBUSY &&
1876 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
1877 fprintf(stderr,
1878 " Bitmap must be removed before"
1879 " shape can be changed\n");
b5420ef3 1880
5da9ab98
N
1881 goto release;
1882 }
1883 } else {
1884 /* set them all just in case some old 'new_*' value
1885 * persists from some earlier problem
7236ee7a 1886 */
631d7405 1887 int err = 0;
5da9ab98 1888 if (sysfs_set_num(sra, NULL, "chunk_size", info->new_chunk) < 0)
631d7405
N
1889 err = errno;
1890 if (!err && sysfs_set_num(sra, NULL, "layout",
5da9ab98 1891 reshape.after.layout) < 0)
631d7405
N
1892 err = errno;
1893 if (!err && subarray_set_num(container, sra, "raid_disks",
5da9ab98
N
1894 reshape.after.data_disks +
1895 reshape.parity) < 0)
631d7405
N
1896 err = errno;
1897 if (err) {
5da9ab98
N
1898 fprintf(stderr, Name ": Cannot set device shape for %s\n",
1899 devname);
7236ee7a 1900
5da9ab98
N
1901 if (err == EBUSY &&
1902 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
1903 fprintf(stderr,
1904 " Bitmap must be removed before"
1905 " shape can be changed\n");
1906 goto release;
e86c9dd6 1907 }
5da9ab98 1908 }
e86c9dd6 1909
5da9ab98 1910 start_reshape(sra);
5da9ab98
N
1911
1912 /* Now we just need to kick off the reshape and watch, while
1913 * handling backups of the data...
1914 * This is all done by a forked background process.
1915 */
1916 switch(forked ? 0 : fork()) {
6b2d630c
N
1917 case -1:
1918 fprintf(stderr, Name ": Cannot run child to monitor reshape: %s\n",
1919 strerror(errno));
6b2d630c 1920 abort_reshape(sra);
631d7405 1921 goto release;
6b2d630c
N
1922 default:
1923 return 0;
5da9ab98 1924 case 0:
6b2d630c
N
1925 break;
1926 }
5da9ab98 1927
6b2d630c
N
1928 close(fd);
1929 if (check_env("MDADM_GROW_VERIFY"))
1930 fd = open(devname, O_RDONLY | O_DIRECT);
1931 else
1932 fd = -1;
1933 mlockall(MCL_FUTURE);
5da9ab98 1934
6b2d630c 1935 odisks = reshape.before.data_disks + reshape.parity;
582496b2 1936
6b2d630c
N
1937 if (st->ss->external) {
1938 /* metadata handler takes it from here */
1939 done = st->ss->manage_reshape(
1940 fd, sra, &reshape, st, blocks,
1941 fdlist, offsets,
1942 d - odisks, fdlist+odisks,
1943 offsets+odisks);
1944 } else
1945 done = child_monitor(
1946 fd, sra, &reshape, st, blocks,
1947 fdlist, offsets,
1948 d - odisks, fdlist+odisks,
1949 offsets+odisks);
1950
1951 if (backup_file && done)
1952 unlink(backup_file);
1953 if (!done) {
1954 abort_reshape(sra);
1955 goto out;
1956 }
1cbc5680
N
1957
1958 if (!st->ss->external &&
1959 !(reshape.before.data_disks != reshape.after.data_disks
1960 && info->custom_array_size) &&
1961 info->new_level == reshape.level &&
1962 !forked) {
1963 /* no need to wait for the reshape to finish as
1964 * there is nothing more to do.
1965 */
1966 exit(0);
1967 }
1968 wait_reshape(sra);
1969
1970 if (st->ss->external) {
1971 /* Re-load the metadata as much could have changed */
1972 int cfd = open_dev(st->container_dev);
1973 if (cfd >= 0) {
1974 ping_monitor(container);
1975 st->ss->free_super(st);
1976 st->ss->load_container(st, cfd, container);
1977 close(cfd);
1978 }
1979 }
1980
6b2d630c
N
1981 /* set new array size if required customer_array_size is used
1982 * by this metadata.
1983 */
1984 if (reshape.before.data_disks !=
1985 reshape.after.data_disks &&
1986 info->custom_array_size) {
1987 struct mdinfo *info2;
1988 char *subarray = strchr(info->text_version+1, '/')+1;
582496b2 1989
6b2d630c
N
1990 info2 = st->ss->container_content(st, subarray);
1991 if (info2) {
1992 unsigned long long current_size = 0;
1993 unsigned long long new_size =
1994 info2->custom_array_size/2;
1995
1996 if (sysfs_get_ll(sra,
1997 NULL,
1998 "array_size",
1999 &current_size) == 0 &&
2000 new_size > current_size) {
2001 if (sysfs_set_num(sra, NULL,
2002 "array_size", new_size)
2003 < 0)
2004 dprintf("Error: Cannot"
2005 " set array size");
2006 else
2007 dprintf("Array size "
2008 "changed");
2009 dprintf(" from %llu to %llu.\n",
2010 current_size, new_size);
582496b2 2011 }
6b2d630c 2012 sysfs_free(info2);
582496b2 2013 }
6b2d630c 2014 }
582496b2 2015
6b2d630c 2016 if (info->new_level != reshape.level) {
582496b2 2017
6b2d630c 2018 c = map_num(pers, info->new_level);
1cbc5680
N
2019 if (c) {
2020 err = sysfs_set_str(sra, NULL, "level", c);
2021 if (err)
2022 fprintf(stderr, Name\
2023 ": %s: could not set level "
2024 "to %s\n", devname, c);
2025 }
7236ee7a 2026 }
6b2d630c
N
2027out:
2028 if (forked)
2029 return 0;
2030 exit(0);
e86c9dd6 2031
6b2d630c 2032release:
1cbc5680 2033 if (orig_level != UnSet && sra) {
7236ee7a
N
2034 c = map_num(pers, orig_level);
2035 if (c && sysfs_set_str(sra, NULL, "level", c) == 0)
2036 fprintf(stderr, Name ": aborting level change\n");
2037 }
9202b8eb
N
2038 if (!forked)
2039 unfreeze(st);
631d7405 2040 return 1;
7236ee7a 2041}
e86c9dd6 2042
5da9ab98
N
2043int reshape_container(char *container, int cfd, char *devname,
2044 struct supertype *st,
2045 struct mdinfo *info,
2046 int force,
2047 char *backup_file,
2048 int quiet)
2049{
1bb174ba
AK
2050 struct mdinfo *cc = NULL;
2051
d8eb27f7
KA
2052 /* component_size is not meaningful for a container,
2053 * so pass '-1' meaning 'no change'
2054 */
2055 if (reshape_super(st, -1, info->new_level,
5da9ab98
N
2056 info->new_layout, info->new_chunk,
2057 info->array.raid_disks + info->delta_disks,
2058 backup_file, devname, quiet))
2059 return 1;
2060
2061 sync_metadata(st);
2062
1bb174ba
AK
2063 /* ping monitor to be sure that update is on disk
2064 */
2065 ping_monitor(container);
5da9ab98
N
2066
2067 switch (fork()) {
2068 case -1: /* error */
2069 perror("Cannot fork to complete reshape\n");
2070 return 1;
2071 default: /* parent */
2072 printf(Name ": multi-array reshape continues in background\n");
2073 return 0;
2074 case 0: /* child */
2075 break;
2076 }
2077
1bb174ba
AK
2078 while(1) {
2079 /* For each member array with reshape_active,
2080 * we need to perform the reshape.
2081 * We pick the first array that needs reshaping and
2082 * reshape it. reshape_array() will re-read the metadata
2083 * so the next time through a different array should be
2084 * ready for reshape.
2085 */
2086 struct mdinfo *content;
5da9ab98
N
2087 int rv;
2088 int fd;
2089 struct mdstat_ent *mdstat;
5da9ab98
N
2090 char *adev;
2091
1bb174ba 2092 sysfs_free(cc);
5da9ab98 2093
1bb174ba
AK
2094 cc = st->ss->container_content(st, NULL);
2095
2096 for (content = cc; content ; content = content->next) {
2097 char *subarray;
2098 if (!content->reshape_active)
2099 continue;
2100
2101 subarray = strchr(content->text_version+1, '/')+1;
2102 mdstat = mdstat_by_subdev(subarray,
2103 devname2devnum(container));
2104 if (!mdstat)
2105 continue;
2106 break;
2107 }
2108 if (!content)
2109 break;
5da9ab98 2110
5da9ab98
N
2111 fd = open_dev_excl(mdstat->devnum);
2112 if (fd < 0)
2113 break;
2114 adev = map_dev(dev2major(mdstat->devnum),
2115 dev2minor(mdstat->devnum),
2116 0);
2117 if (!adev)
1bb174ba
AK
2118 adev = content->text_version;
2119
2120 sysfs_init(content, fd, mdstat->devnum);
5da9ab98 2121
1bb174ba
AK
2122 rv = reshape_array(container, fd, adev, st,
2123 content, force,
5da9ab98
N
2124 backup_file, quiet, 1);
2125 close(fd);
2126 if (rv)
2127 break;
2128 }
9202b8eb 2129 unfreeze(st);
1bb174ba 2130 sysfs_free(cc);
5da9ab98
N
2131 exit(0);
2132}
2133
7236ee7a
N
2134/*
2135 * We run a child process in the background which performs the following
2136 * steps:
2137 * - wait for resync to reach a certain point
2138 * - suspend io to the following section
2139 * - backup that section
2140 * - allow resync to proceed further
2141 * - resume io
2142 * - discard the backup.
2143 *
2144 * When are combined in slightly different ways in the three cases.
2145 * Grow:
2146 * - suspend/backup/allow/wait/resume/discard
2147 * Shrink:
2148 * - allow/wait/suspend/backup/allow/wait/resume/discard
2149 * same-size:
2150 * - wait/resume/discard/suspend/backup/allow
2151 *
2152 * suspend/backup/allow always come together
2153 * wait/resume/discard do too.
2154 * For the same-size case we have two backups to improve flow.
2155 *
2156 */
e86c9dd6 2157
7443ee81
N
2158int progress_reshape(struct mdinfo *info, struct reshape *reshape,
2159 unsigned long long backup_point,
2160 unsigned long long wait_point,
2161 unsigned long long *suspend_point,
2162 unsigned long long *reshape_completed)
2163{
2164 /* This function is called repeatedly by the reshape manager.
2165 * It determines how much progress can safely be made and allows
2166 * that progress.
2167 * - 'info' identifies the array and particularly records in
2168 * ->reshape_progress the metadata's knowledge of progress
2169 * This is a sector offset from the start of the array
2170 * of the next array block to be relocated. This number
2171 * may increase from 0 or decrease from array_size, depending
2172 * on the type of reshape that is happening.
2173 * Note that in contrast, 'sync_completed' is a block count of the
2174 * reshape so far. It gives the distance between the start point
2175 * (head or tail of device) and the next place that data will be
2176 * written. It always increases.
2177 * - 'reshape' is the structure created by analyse_change
2178 * - 'backup_point' shows how much the metadata manager has backed-up
2179 * data. For reshapes with increasing progress, it is the next address
2180 * to be backed up, previous addresses have been backed-up. For
2181 * decreasing progress, it is the earliest address that has been
2182 * backed up - later address are also backed up.
2183 * So addresses between reshape_progress and backup_point are
2184 * backed up providing those are in the 'correct' order.
2185 * - 'wait_point' is an array address. When reshape_completed
2186 * passes this point, progress_reshape should return. It might
2187 * return earlier if it determines that ->reshape_progress needs
2188 * to be updated or further backup is needed.
2189 * - suspend_point is maintained by progress_reshape and the caller
2190 * should not touch it except to initialise to zero.
2191 * It is an array address and it only increases in 2.6.37 and earlier.
b6b95155 2192 * This makes it difficult to handle reducing reshapes with
7443ee81
N
2193 * external metadata.
2194 * However: it is similar to backup_point in that it records the
2195 * other end of a suspended region from reshape_progress.
2196 * it is moved to extend the region that is safe to backup and/or
2197 * reshape
2198 * - reshape_completed is read from sysfs and returned. The caller
2199 * should copy this into ->reshape_progress when it has reason to
2200 * believe that the metadata knows this, and any backup outside this
2201 * has been erased.
2202 *
2203 * Return value is:
2204 * 1 if more data from backup_point - but only as far as suspend_point,
2205 * should be backed up
2206 * 0 if things are progressing smoothly
2207 * -1 if the reshape is finished, either because it is all done,
2208 * or due to an error.
2209 */
2210
2211 int advancing = (reshape->after.data_disks
2212 >= reshape->before.data_disks);
d0ab945e
N
2213 unsigned long long need_backup; /* need to eventually backup all the way
2214 * to here
2215 */
7443ee81 2216 unsigned long long read_offset, write_offset;
b4f8e38b 2217 unsigned long long write_range;
7443ee81 2218 unsigned long long max_progress, target, completed;
d0ab945e
N
2219 unsigned long long array_size = (info->component_size
2220 * reshape->before.data_disks);
7443ee81 2221 int fd;
77a73a17 2222 char buf[20];
7443ee81
N
2223
2224 /* First, we unsuspend any region that is now known to be safe.
2225 * If suspend_point is on the 'wrong' side of reshape_progress, then
2226 * we don't have or need suspension at the moment. This is true for
2227 * native metadata when we don't need to back-up.
2228 */
2229 if (advancing) {
49cd738b 2230 if (info->reshape_progress <= *suspend_point)
7443ee81
N
2231 sysfs_set_num(info, NULL, "suspend_lo",
2232 info->reshape_progress);
2233 } else {
2234 /* Note: this won't work in 2.6.37 and before.
2235 * Something somewhere should make sure we don't need it!
2236 */
49cd738b 2237 if (info->reshape_progress >= *suspend_point)
7443ee81
N
2238 sysfs_set_num(info, NULL, "suspend_hi",
2239 info->reshape_progress);
2240 }
2241
2242 /* Now work out how far it is safe to progress.
2243 * If the read_offset for ->reshape_progress is less than
2244 * 'blocks' beyond the write_offset, we can only progress as far
2245 * as a backup.
2246 * Otherwise we can progress until the write_offset for the new location
2247 * reaches (within 'blocks' of) the read_offset at the current location.
2248 * However that region must be suspended unless we are using native
2249 * metadata.
2250 * If we need to suspend more, we limit it to 128M per device, which is
2251 * rather arbitrary and should be some time-based calculation.
2252 */
ec757320
N
2253 read_offset = info->reshape_progress / reshape->before.data_disks;
2254 write_offset = info->reshape_progress / reshape->after.data_disks;
b4f8e38b 2255 write_range = info->new_chunk/512;
7443ee81 2256 if (advancing) {
d0ab945e 2257 need_backup = 0;
7443ee81
N
2258 if (read_offset < write_offset + write_range) {
2259 max_progress = backup_point;
d0ab945e
N
2260 if (reshape->before.data_disks == reshape->after.data_disks)
2261 need_backup = array_size;
2262 else
2263 need_backup = reshape->backup_blocks;
7443ee81
N
2264 } else {
2265 max_progress =
2f3dd5e4
N
2266 read_offset *
2267 reshape->after.data_disks;
7443ee81
N
2268 }
2269 } else {
d0ab945e 2270 need_backup = array_size;
7443ee81
N
2271 if (read_offset > write_offset - write_range) {
2272 max_progress = backup_point;
2273 if (max_progress >= info->reshape_progress)
d0ab945e 2274 need_backup = 0;
7443ee81
N
2275 } else {
2276 max_progress =
2f3dd5e4
N
2277 read_offset *
2278 reshape->after.data_disks;
7443ee81
N
2279 /* If we are using internal metadata, then we can
2280 * progress all the way to the suspend_point without
2281 * worrying about backing-up/suspending along the
2282 * way.
2283 */
2284 if (max_progress < *suspend_point &&
2285 info->array.major_version >= 0)
2286 max_progress = *suspend_point;
2287 }
2288 }
2289
2290 /* We know it is safe to progress to 'max_progress' providing
2291 * it is suspended or we are using native metadata.
2292 * Consider extending suspend_point 128M per device if it
2293 * is less than 64M per device beyond reshape_progress.
2294 * But always do a multiple of 'blocks'
832b2b9a
N
2295 * FIXME this is too big - it takes to long to complete
2296 * this much.
7443ee81
N
2297 */
2298 target = 64*1024*2 * min(reshape->before.data_disks,
2299 reshape->after.data_disks);
b4f8e38b 2300 target /= reshape->backup_blocks;
7443ee81
N
2301 if (target < 2)
2302 target = 2;
b4f8e38b 2303 target *= reshape->backup_blocks;
7443ee81
N
2304
2305 /* For externally managed metadata we always need to suspend IO to
2306 * the area being reshaped so we regularly push suspend_point forward.
2307 * For native metadata we only need the suspend if we are going to do
2308 * a backup.
2309 */
2310 if (advancing) {
d0ab945e
N
2311 if ((need_backup > info->reshape_progress
2312 || info->array.major_version < 0) &&
7443ee81 2313 *suspend_point < info->reshape_progress + target) {
d0ab945e
N
2314 if (need_backup < *suspend_point + 2 * target)
2315 *suspend_point = need_backup;
2316 else if (*suspend_point + 2 * target < array_size)
7443ee81 2317 *suspend_point += 2 * target;
d0ab945e
N
2318 else
2319 *suspend_point = array_size;
7443ee81 2320 sysfs_set_num(info, NULL, "suspend_hi", *suspend_point);
d0ab945e
N
2321 if (max_progress > *suspend_point)
2322 max_progress = *suspend_point;
7443ee81
N
2323 }
2324 } else {
d0ab945e
N
2325 if ((need_backup < info->reshape_progress
2326 || info->array.major_version < 0) &&
7443ee81 2327 *suspend_point > info->reshape_progress - target) {
d0ab945e
N
2328 if (need_backup > *suspend_point - 2 * target)
2329 *suspend_point = need_backup;
2330 else if (*suspend_point >= 2 * target)
7443ee81 2331 *suspend_point -= 2 * target;
d0ab945e
N
2332 else
2333 *suspend_point = 0;
7443ee81 2334 sysfs_set_num(info, NULL, "suspend_lo", *suspend_point);
d0ab945e
N
2335 if (max_progress < *suspend_point)
2336 max_progress = *suspend_point;
7443ee81
N
2337 }
2338 }
2339
2340 /* now set sync_max to allow that progress. sync_max, like
2341 * sync_completed is a count of sectors written per device, so
2342 * we find the difference between max_progress and the start point,
2343 * and divide that by after.data_disks to get a sync_max
2344 * number.
2345 * At the same time we convert wait_point to a similar number
2346 * for comparing against sync_completed.
2347 */
92d1991f 2348 /* scale down max_progress to per_disk */
7443ee81 2349 max_progress /= reshape->after.data_disks;
92d1991f
N
2350 /* Round to chunk size as some kernels give an erroneously high number */
2351 max_progress /= info->new_chunk/512;
2352 max_progress *= info->new_chunk/512;
2353 /* Limit progress to the whole device */
2354 if (max_progress > info->component_size)
2355 max_progress = info->component_size;
7443ee81 2356 wait_point /= reshape->after.data_disks;
92d1991f
N
2357 if (!advancing) {
2358 /* switch from 'device offset' to 'processed block count' */
2359 max_progress = info->component_size - max_progress;
2360 wait_point = info->component_size - wait_point;
2361 }
7443ee81
N
2362
2363 sysfs_set_num(info, NULL, "sync_max", max_progress);
2364
2365 /* Now wait. If we have already reached the point that we were
2366 * asked to wait to, don't wait at all, else wait for any change.
2367 * We need to select on 'sync_completed' as that is the place that
2368 * notifications happen, but we are really interested in
2369 * 'reshape_position'
2370 */
2371 fd = sysfs_get_fd(info, NULL, "sync_completed");
2372 if (fd < 0)
77a73a17 2373 goto check_progress;
7443ee81
N
2374
2375 if (sysfs_fd_get_ll(fd, &completed) < 0) {
2376 close(fd);
77a73a17 2377 goto check_progress;
7443ee81
N
2378 }
2379 while (completed < max_progress && completed < wait_point) {
2380 /* Check that sync_action is still 'reshape' to avoid
2381 * waiting forever on a dead array
2382 */
2383 char action[20];
2384 fd_set rfds;
2385 if (sysfs_get_str(info, NULL, "sync_action",
2386 action, 20) <= 0 ||
2387 strncmp(action, "reshape", 7) != 0)
2388 break;
2389 FD_ZERO(&rfds);
2390 FD_SET(fd, &rfds);
2391 select(fd+1, NULL, NULL, &rfds, NULL);
2392 if (sysfs_fd_get_ll(fd, &completed) < 0) {
2393 close(fd);
77a73a17 2394 goto check_progress;
7443ee81
N
2395 }
2396 }
92d1991f
N
2397 /* some kernels can give an incorrectly high 'completed' number */
2398 completed /= (info->new_chunk/512);
2399 completed *= (info->new_chunk/512);
7443ee81
N
2400 /* Convert 'completed' back in to a 'progress' number */
2401 completed *= reshape->after.data_disks;
2402 if (!advancing) {
2403 completed = info->component_size * reshape->after.data_disks
2404 - completed;
2405 }
2406 *reshape_completed = completed;
2407
2408 close(fd);
2409
2410 /* We return the need_backup flag. Caller will decide
d0ab945e 2411 * how much - a multiple of ->backup_blocks up to *suspend_point
7443ee81 2412 */
d0ab945e
N
2413 return advancing
2414 ? (need_backup > info->reshape_progress)
2415 : (need_backup < info->reshape_progress);
77a73a17
N
2416
2417check_progress:
2418 /* if we couldn't read a number from sync_completed, then
2419 * either the reshape did complete, or it aborted.
2420 * We can tell which by checking for 'none' in reshape_position.
2421 */
2422 strcpy(buf, "hi");
2423 if (sysfs_get_str(info, NULL, "reshape_position", buf, sizeof(buf)) < 0
2424 || strncmp(buf, "none", 4) != 0)
2425 return -2; /* abort */
6d5316f6
N
2426 else {
2427 /* Maybe racing with array shutdown - check state */
2428 if (sysfs_get_str(info, NULL, "array_state", buf, sizeof(buf)) < 0
2429 || strncmp(buf, "inactive", 8) == 0
2430 || strncmp(buf, "clear",5) == 0)
2431 return -2; /* abort */
77a73a17 2432 return -1; /* complete */
6d5316f6 2433 }
7443ee81
N
2434}
2435
2436
fcf57625 2437/* FIXME return status is never checked */
4411fb17 2438static int grow_backup(struct mdinfo *sra,
7236ee7a 2439 unsigned long long offset, /* per device */
7443ee81 2440 unsigned long stripes, /* per device, in old chunks */
7236ee7a
N
2441 int *sources, unsigned long long *offsets,
2442 int disks, int chunk, int level, int layout,
2443 int dests, int *destfd, unsigned long long *destoffsets,
d4445387 2444 int part, int *degraded,
7236ee7a
N
2445 char *buf)
2446{
2447 /* Backup 'blocks' sectors at 'offset' on each device of the array,
2448 * to storage 'destfd' (offset 'destoffsets'), after first
2449 * suspending IO. Then allow resync to continue
2450 * over the suspended section.
2451 * Use part 'part' of the backup-super-block.
2452 */
2453 int odata = disks;
2454 int rv = 0;
2455 int i;
f21e18ca
N
2456 unsigned long long ll;
2457 int new_degraded;
7236ee7a
N
2458 //printf("offset %llu\n", offset);
2459 if (level >= 4)
2460 odata--;
2461 if (level == 6)
2462 odata--;
7443ee81 2463
d4445387 2464 /* Check that array hasn't become degraded, else we might backup the wrong data */
f21e18ca
N
2465 sysfs_get_ll(sra, NULL, "degraded", &ll);
2466 new_degraded = (int)ll;
d4445387
N
2467 if (new_degraded != *degraded) {
2468 /* check each device to ensure it is still working */
2469 struct mdinfo *sd;
2470 for (sd = sra->devs ; sd ; sd = sd->next) {
2471 if (sd->disk.state & (1<<MD_DISK_FAULTY))
2472 continue;
2473 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
2474 char sbuf[20];
2475 if (sysfs_get_str(sra, sd, "state", sbuf, 20) < 0 ||
2476 strstr(sbuf, "faulty") ||
2477 strstr(sbuf, "in_sync") == NULL) {
2478 /* this device is dead */
2479 sd->disk.state = (1<<MD_DISK_FAULTY);
2480 if (sd->disk.raid_disk >= 0 &&
2481 sources[sd->disk.raid_disk] >= 0) {
2482 close(sources[sd->disk.raid_disk]);
2483 sources[sd->disk.raid_disk] = -1;
2484 }
2485 }
2486 }
2487 }
2488 *degraded = new_degraded;
2489 }
7236ee7a
N
2490 if (part) {
2491 bsb.arraystart2 = __cpu_to_le64(offset * odata);
200871ad 2492 bsb.length2 = __cpu_to_le64(stripes * (chunk/512) * odata);
7236ee7a
N
2493 } else {
2494 bsb.arraystart = __cpu_to_le64(offset * odata);
200871ad 2495 bsb.length = __cpu_to_le64(stripes * (chunk/512) * odata);
7236ee7a
N
2496 }
2497 if (part)
2498 bsb.magic[15] = '2';
2499 for (i = 0; i < dests; i++)
2500 if (part)
2501 lseek64(destfd[i], destoffsets[i] + __le64_to_cpu(bsb.devstart2)*512, 0);
2502 else
2503 lseek64(destfd[i], destoffsets[i], 0);
2504
2505 rv = save_stripes(sources, offsets,
2506 disks, chunk, level, layout,
2507 dests, destfd,
2508 offset*512*odata, stripes * chunk * odata,
2509 buf);
2510
2511 if (rv)
2512 return rv;
97396422 2513 bsb.mtime = __cpu_to_le64(time(0));
7236ee7a
N
2514 for (i = 0; i < dests; i++) {
2515 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
2516
2517 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
2518 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
2519 bsb.sb_csum2 = bsb_csum((char*)&bsb,
2520 ((char*)&bsb.sb_csum2)-((char*)&bsb));
2521
72044953 2522 rv = -1;
f21e18ca
N
2523 if ((unsigned long long)lseek64(destfd[i], destoffsets[i] - 4096, 0)
2524 != destoffsets[i] - 4096)
72044953
N
2525 break;
2526 if (write(destfd[i], &bsb, 512) != 512)
2527 break;
ff94fb86 2528 if (destoffsets[i] > 4096) {
f21e18ca 2529 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]+stripes*chunk*odata, 0) !=
a847575a 2530 destoffsets[i]+stripes*chunk*odata)
72044953
N
2531 break;
2532 if (write(destfd[i], &bsb, 512) != 512)
2533 break;
ff94fb86 2534 }
7236ee7a 2535 fsync(destfd[i]);
72044953 2536 rv = 0;
7236ee7a 2537 }
2efedc7b 2538
fcf57625 2539 return rv;
7236ee7a
N
2540}
2541
2542/* in 2.6.30, the value reported by sync_completed can be
2543 * less that it should be by one stripe.
2544 * This only happens when reshape hits sync_max and pauses.
2545 * So allow wait_backup to either extent sync_max further
2546 * than strictly necessary, or return before the
2547 * sync has got quite as far as we would really like.
2548 * This is what 'blocks2' is for.
2549 * The various caller give appropriate values so that
2550 * every works.
2551 */
fcf57625 2552/* FIXME return value is often ignored */
7443ee81 2553static int forget_backup(
7236ee7a
N
2554 int dests, int *destfd, unsigned long long *destoffsets,
2555 int part)
2556{
7443ee81
N
2557 /*
2558 * Erase backup 'part' (which is 0 or 1)
7236ee7a 2559 */
7236ee7a 2560 int i;
fcf57625 2561 int rv;
7236ee7a 2562
7236ee7a
N
2563 if (part) {
2564 bsb.arraystart2 = __cpu_to_le64(0);
2565 bsb.length2 = __cpu_to_le64(0);
2566 } else {
2567 bsb.arraystart = __cpu_to_le64(0);
2568 bsb.length = __cpu_to_le64(0);
2569 }
97396422 2570 bsb.mtime = __cpu_to_le64(time(0));
fcf57625 2571 rv = 0;
7236ee7a
N
2572 for (i = 0; i < dests; i++) {
2573 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
2574 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
2575 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
2576 bsb.sb_csum2 = bsb_csum((char*)&bsb,
2577 ((char*)&bsb.sb_csum2)-((char*)&bsb));
f21e18ca 2578 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]-4096, 0) !=
a847575a 2579 destoffsets[i]-4096)
72044953
N
2580 rv = -1;
2581 if (rv == 0 &&
2582 write(destfd[i], &bsb, 512) != 512)
2583 rv = -1;
7236ee7a
N
2584 fsync(destfd[i]);
2585 }
fcf57625 2586 return rv;
7236ee7a 2587}
e86c9dd6 2588
7236ee7a
N
2589static void fail(char *msg)
2590{
fcf57625 2591 int rv;
72044953
N
2592 rv = (write(2, msg, strlen(msg)) != (int)strlen(msg));
2593 rv |= (write(2, "\n", 1) != 1);
fcf57625 2594 exit(rv ? 1 : 2);
7236ee7a
N
2595}
2596
2597static char *abuf, *bbuf;
f21e18ca 2598static unsigned long long abuflen;
7236ee7a
N
2599static void validate(int afd, int bfd, unsigned long long offset)
2600{
2601 /* check that the data in the backup against the array.
2602 * This is only used for regression testing and should not
2603 * be used while the array is active
2604 */
7236ee7a
N
2605 if (afd < 0)
2606 return;
2607 lseek64(bfd, offset - 4096, 0);
2608 if (read(bfd, &bsb2, 512) != 512)
2609 fail("cannot read bsb");
2610 if (bsb2.sb_csum != bsb_csum((char*)&bsb2,
2611 ((char*)&bsb2.sb_csum)-((char*)&bsb2)))
2612 fail("first csum bad");
2613 if (memcmp(bsb2.magic, "md_backup_data", 14) != 0)
2614 fail("magic is bad");
2615 if (memcmp(bsb2.magic, "md_backup_data-2", 16) == 0 &&
2616 bsb2.sb_csum2 != bsb_csum((char*)&bsb2,
2617 ((char*)&bsb2.sb_csum2)-((char*)&bsb2)))
2618 fail("second csum bad");
2619
2620 if (__le64_to_cpu(bsb2.devstart)*512 != offset)
2621 fail("devstart is wrong");
2622
2623 if (bsb2.length) {
2624 unsigned long long len = __le64_to_cpu(bsb2.length)*512;
2625
2626 if (abuflen < len) {
2627 free(abuf);
2628 free(bbuf);
2629 abuflen = len;
fcf57625
N
2630 if (posix_memalign((void**)&abuf, 4096, abuflen) ||
2631 posix_memalign((void**)&bbuf, 4096, abuflen)) {
2632 abuflen = 0;
2633 /* just stop validating on mem-alloc failure */
2634 return;
2635 }
e86c9dd6 2636 }
48924014 2637
7236ee7a 2638 lseek64(bfd, offset, 0);
f21e18ca 2639 if ((unsigned long long)read(bfd, bbuf, len) != len) {
080fd005 2640 //printf("len %llu\n", len);
7236ee7a
N
2641 fail("read first backup failed");
2642 }
2643 lseek64(afd, __le64_to_cpu(bsb2.arraystart)*512, 0);
f21e18ca 2644 if ((unsigned long long)read(afd, abuf, len) != len)
7236ee7a
N
2645 fail("read first from array failed");
2646 if (memcmp(bbuf, abuf, len) != 0) {
080fd005 2647 #if 0
7236ee7a
N
2648 int i;
2649 printf("offset=%llu len=%llu\n",
080fd005 2650 (unsigned long long)__le64_to_cpu(bsb2.arraystart)*512, len);
7236ee7a
N
2651 for (i=0; i<len; i++)
2652 if (bbuf[i] != abuf[i]) {
2653 printf("first diff byte %d\n", i);
93ecfa01 2654 break;
7236ee7a 2655 }
080fd005 2656 #endif
7236ee7a 2657 fail("data1 compare failed");
e86c9dd6 2658 }
7236ee7a
N
2659 }
2660 if (bsb2.length2) {
2661 unsigned long long len = __le64_to_cpu(bsb2.length2)*512;
2662
2663 if (abuflen < len) {
2664 free(abuf);
2665 free(bbuf);
2666 abuflen = len;
2667 abuf = malloc(abuflen);
2668 bbuf = malloc(abuflen);
e86c9dd6
NB
2669 }
2670
7236ee7a 2671 lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0);
f21e18ca 2672 if ((unsigned long long)read(bfd, bbuf, len) != len)
7236ee7a
N
2673 fail("read second backup failed");
2674 lseek64(afd, __le64_to_cpu(bsb2.arraystart2)*512, 0);
f21e18ca 2675 if ((unsigned long long)read(afd, abuf, len) != len)
7236ee7a
N
2676 fail("read second from array failed");
2677 if (memcmp(bbuf, abuf, len) != 0)
2678 fail("data2 compare failed");
e86c9dd6 2679 }
7236ee7a 2680}
e86c9dd6 2681
999b4972
N
2682int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
2683 struct supertype *st, unsigned long blocks,
2684 int *fds, unsigned long long *offsets,
2685 int dests, int *destfd, unsigned long long *destoffsets)
7236ee7a 2686{
7443ee81
N
2687 /* Monitor a reshape where backup is being performed using
2688 * 'native' mechanism - either to a backup file, or
2689 * to some space in a spare.
2690 */
7236ee7a 2691 char *buf;
7443ee81
N
2692 int degraded = -1;
2693 unsigned long long speed;
2694 unsigned long long suspend_point, array_size;
2695 unsigned long long backup_point, wait_point;
2696 unsigned long long reshape_completed;
2697 int done = 0;
2698 int increasing = reshape->after.data_disks >= reshape->before.data_disks;
2699 int part = 0; /* The next part of the backup area to fill. It may already
2700 * be full, so we need to check */
2701 int level = reshape->level;
2702 int layout = reshape->before.layout;
2703 int data = reshape->before.data_disks;
2704 int disks = reshape->before.data_disks + reshape->parity;
2705 int chunk = sra->array.chunk_size;
da8100ca
N
2706 struct mdinfo *sd;
2707 unsigned long stripes;
2708
2709 /* set up the backup-super-block. This requires the
2710 * uuid from the array.
2711 */
2712 /* Find a superblock */
2713 for (sd = sra->devs; sd; sd = sd->next) {
2714 char *dn;
2715 int devfd;
2716 int ok;
2717 if (sd->disk.state & (1<<MD_DISK_FAULTY))
2718 continue;
2719 dn = map_dev(sd->disk.major, sd->disk.minor, 1);
2720 devfd = dev_open(dn, O_RDONLY);
2721 if (devfd < 0)
2722 continue;
2723 ok = st->ss->load_super(st, devfd, NULL);
2724 close(devfd);
2725 if (ok >= 0)
2726 break;
2727 }
2728 if (!sd) {
2729 fprintf(stderr, Name ": Cannot find a superblock\n");
2730 return 0;
2731 }
2732
2733 memset(&bsb, 0, 512);
2734 memcpy(bsb.magic, "md_backup_data-1", 16);
2735 st->ss->uuid_from_super(st, (int*)&bsb.set_uuid);
2736 bsb.mtime = __cpu_to_le64(time(0));
2737 bsb.devstart2 = blocks;
2738
2739 stripes = blocks / (sra->array.chunk_size/512) /
2740 reshape->before.data_disks;
e86c9dd6 2741
fcf57625
N
2742 if (posix_memalign((void**)&buf, 4096, disks * chunk))
2743 /* Don't start the 'reshape' */
2744 return 0;
7443ee81
N
2745 if (reshape->before.data_disks == reshape->after.data_disks) {
2746 sysfs_get_ll(sra, NULL, "sync_speed_min", &speed);
2747 sysfs_set_num(sra, NULL, "sync_speed_min", 200000);
2748 }
e86c9dd6 2749
7443ee81 2750 if (increasing) {
96533072 2751 array_size = sra->component_size * reshape->after.data_disks;
7443ee81
N
2752 backup_point = sra->reshape_progress;
2753 suspend_point = 0;
2754 } else {
96533072 2755 array_size = sra->component_size * reshape->before.data_disks;
7443ee81
N
2756 backup_point = array_size;
2757 suspend_point = array_size;
2758 }
7236ee7a 2759
7443ee81
N
2760 while (!done) {
2761 int rv;
7236ee7a 2762
7443ee81
N
2763 /* Want to return as soon the oldest backup slot can
2764 * be released as that allows us to start backing up
2765 * some more, providing suspend_point has been
b6b95155 2766 * advanced, which it should have.
7443ee81
N
2767 */
2768 if (increasing) {
2769 wait_point = array_size;
2770 if (part == 0 && __le64_to_cpu(bsb.length) > 0)
2771 wait_point = (__le64_to_cpu(bsb.arraystart) +
2772 __le64_to_cpu(bsb.length));
2773 if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
2774 wait_point = (__le64_to_cpu(bsb.arraystart2) +
2775 __le64_to_cpu(bsb.length2));
2776 } else {
2777 wait_point = 0;
2778 if (part == 0 && __le64_to_cpu(bsb.length) > 0)
2779 wait_point = __le64_to_cpu(bsb.arraystart);
2780 if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
2781 wait_point = __le64_to_cpu(bsb.arraystart2);
2782 }
2783
2784 rv = progress_reshape(sra, reshape,
2785 backup_point, wait_point,
2786 &suspend_point, &reshape_completed);
7443ee81
N
2787 /* external metadata would need to ping_monitor here */
2788 sra->reshape_progress = reshape_completed;
7236ee7a 2789
7443ee81
N
2790 /* Clear any backup region that is before 'here' */
2791 if (increasing) {
2792 if (reshape_completed >= (__le64_to_cpu(bsb.arraystart) +
2793 __le64_to_cpu(bsb.length)))
2794 forget_backup(dests, destfd,
2795 destoffsets, 0);
2796 if (reshape_completed >= (__le64_to_cpu(bsb.arraystart2) +
2797 __le64_to_cpu(bsb.length2)))
2798 forget_backup(dests, destfd,
2799 destoffsets, 1);
2800 } else {
2801 if (reshape_completed <= (__le64_to_cpu(bsb.arraystart)))
2802 forget_backup(dests, destfd,
2803 destoffsets, 0);
2804 if (reshape_completed <= (__le64_to_cpu(bsb.arraystart2)))
2805 forget_backup(dests, destfd,
2806 destoffsets, 1);
2807 }
7236ee7a 2808
223c5c99 2809 if (rv < 0) {
77a73a17
N
2810 if (rv == -1)
2811 done = 1;
223c5c99
N
2812 break;
2813 }
2814
60204e4e 2815 while (rv) {
7443ee81 2816 unsigned long long offset;
e97ca002 2817 unsigned long actual_stripes;
60204e4e
N
2818 /* Need to backup some data.
2819 * If 'part' is not used and the desired
2820 * backup size is suspended, do a backup,
2821 * then consider the next part.
2822 */
7443ee81
N
2823 /* Check that 'part' is unused */
2824 if (part == 0 && __le64_to_cpu(bsb.length) != 0)
60204e4e 2825 break;
7443ee81 2826 if (part == 1 && __le64_to_cpu(bsb.length2) != 0)
60204e4e 2827 break;
7443ee81
N
2828
2829 offset = backup_point / data;
e97ca002 2830 actual_stripes = stripes;
60204e4e 2831 if (increasing) {
e97ca002
N
2832 if (offset + actual_stripes * (chunk/512) >
2833 sra->component_size)
2834 actual_stripes = ((sra->component_size - offset)
2835 / (chunk/512));
2836 if (offset + actual_stripes * (chunk/512) >
60204e4e
N
2837 suspend_point/data)
2838 break;
2839 } else {
e97ca002
N
2840 if (offset < actual_stripes * (chunk/512))
2841 actual_stripes = offset / (chunk/512);
2842 offset -= actual_stripes * (chunk/512);
2843 if (offset < suspend_point/data)
60204e4e
N
2844 break;
2845 }
e97ca002 2846 grow_backup(sra, offset, actual_stripes,
7443ee81
N
2847 fds, offsets,
2848 disks, chunk, level, layout,
2849 dests, destfd, destoffsets,
2850 part, &degraded, buf);
2851 validate(afd, destfd[0], destoffsets[0]);
2852 /* record where 'part' is up to */
2853 part = !part;
2854 if (increasing)
e97ca002 2855 backup_point += actual_stripes * (chunk/512) * data;
7443ee81 2856 else
e97ca002 2857 backup_point -= actual_stripes * (chunk/512) * data;
7443ee81
N
2858 }
2859 }
2860
223c5c99
N
2861 /* FIXME maybe call progress_reshape one more time instead */
2862 abort_reshape(sra); /* remove any remaining suspension */
7443ee81
N
2863 if (reshape->before.data_disks == reshape->after.data_disks)
2864 sysfs_set_num(sra, NULL, "sync_speed_min", speed);
7236ee7a 2865 free(buf);
77a73a17 2866 return done;
e86c9dd6 2867}
353632d9
NB
2868
2869/*
2870 * If any spare contains md_back_data-1 which is recent wrt mtime,
2871 * write that data into the array and update the super blocks with
2872 * the new reshape_progress
2873 */
ea0ebe96
N
2874int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt,
2875 char *backup_file, int verbose)
353632d9
NB
2876{
2877 int i, j;
2878 int old_disks;
353632d9 2879 unsigned long long *offsets;
82f2d6ab 2880 unsigned long long nstripe, ostripe;
6e9eac4f 2881 int ndata, odata;
353632d9 2882
e9e43ec3
N
2883 if (info->new_level != info->array.level)
2884 return 1; /* Cannot handle level changes (they are instantaneous) */
2885
2886 odata = info->array.raid_disks - info->delta_disks - 1;
2887 if (info->array.level == 6) odata--; /* number of data disks */
2888 ndata = info->array.raid_disks - 1;
2889 if (info->new_level == 6) ndata--;
353632d9
NB
2890
2891 old_disks = info->array.raid_disks - info->delta_disks;
2892
e9e43ec3
N
2893 if (info->delta_disks <= 0)
2894 /* Didn't grow, so the backup file must have
2895 * been used
2896 */
2897 old_disks = cnt;
06b0d786 2898 for (i=old_disks-(backup_file?1:0); i<cnt; i++) {
353632d9 2899 struct mdinfo dinfo;
06b0d786 2900 int fd;
e9e43ec3 2901 int bsbsize;
ea0ebe96 2902 char *devname, namebuf[20];
353632d9
NB
2903
2904 /* This was a spare and may have some saved data on it.
2905 * Load the superblock, find and load the
2906 * backup_super_block.
2907 * If either fail, go on to next device.
2908 * If the backup contains no new info, just return
206c5eae 2909 * else restore data and update all superblocks
353632d9 2910 */
06b0d786
NB
2911 if (i == old_disks-1) {
2912 fd = open(backup_file, O_RDONLY);
e9e43ec3
N
2913 if (fd<0) {
2914 fprintf(stderr, Name ": backup file %s inaccessible: %s\n",
2915 backup_file, strerror(errno));
06b0d786 2916 continue;
e9e43ec3 2917 }
ea0ebe96 2918 devname = backup_file;
06b0d786
NB
2919 } else {
2920 fd = fdlist[i];
2921 if (fd < 0)
2922 continue;
3da92f27 2923 if (st->ss->load_super(st, fd, NULL))
06b0d786 2924 continue;
353632d9 2925
a5d85af7 2926 st->ss->getinfo_super(st, &dinfo, NULL);
3da92f27
NB
2927 st->ss->free_super(st);
2928
06b0d786
NB
2929 if (lseek64(fd,
2930 (dinfo.data_offset + dinfo.component_size - 8) <<9,
ea0ebe96
N
2931 0) < 0) {
2932 fprintf(stderr, Name ": Cannot seek on device %d\n", i);
06b0d786 2933 continue; /* Cannot seek */
ea0ebe96
N
2934 }
2935 sprintf(namebuf, "device-%d", i);
2936 devname = namebuf;
06b0d786 2937 }
ea0ebe96
N
2938 if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb)) {
2939 if (verbose)
2940 fprintf(stderr, Name ": Cannot read from %s\n", devname);
353632d9 2941 continue; /* Cannot read */
ea0ebe96 2942 }
e9e43ec3 2943 if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0 &&
ea0ebe96
N
2944 memcmp(bsb.magic, "md_backup_data-2", 16) != 0) {
2945 if (verbose)
2946 fprintf(stderr, Name ": No backup metadata on %s\n", devname);
353632d9 2947 continue;
ea0ebe96
N
2948 }
2949 if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb))) {
2950 if (verbose)
2951 fprintf(stderr, Name ": Bad backup-metadata checksum on %s\n", devname);
353632d9 2952 continue; /* bad checksum */
ea0ebe96 2953 }
e9e43ec3 2954 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0 &&
ea0ebe96
N
2955 bsb.sb_csum2 != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum2)-((char*)&bsb))) {
2956 if (verbose)
2957 fprintf(stderr, Name ": Bad backup-metadata checksum2 on %s\n", devname);
22e30516 2958 continue; /* Bad second checksum */
ea0ebe96
N
2959 }
2960 if (memcmp(bsb.set_uuid,info->uuid, 16) != 0) {
2961 if (verbose)
2962 fprintf(stderr, Name ": Wrong uuid on backup-metadata on %s\n", devname);
353632d9 2963 continue; /* Wrong uuid */
ea0ebe96 2964 }
353632d9 2965
097075b6
N
2966 /* array utime and backup-mtime should be updated at much the same time, but it seems that
2967 * sometimes they aren't... So allow considerable flexability in matching, and allow
2968 * this test to be overridden by an environment variable.
2969 */
f21e18ca
N
2970 if (info->array.utime > (int)__le64_to_cpu(bsb.mtime) + 2*60*60 ||
2971 info->array.utime < (int)__le64_to_cpu(bsb.mtime) - 10*60) {
097075b6
N
2972 if (check_env("MDADM_GROW_ALLOW_OLD")) {
2973 fprintf(stderr, Name ": accepting backup with timestamp %lu "
2974 "for array with timestamp %lu\n",
2975 (unsigned long)__le64_to_cpu(bsb.mtime),
2976 (unsigned long)info->array.utime);
2977 } else {
2978 if (verbose)
2979 fprintf(stderr, Name ": too-old timestamp on "
2980 "backup-metadata on %s\n", devname);
2981 continue; /* time stamp is too bad */
2982 }
ea0ebe96 2983 }
353632d9 2984
e9e43ec3
N
2985 if (bsb.magic[15] == '1') {
2986 if (info->delta_disks >= 0) {
2987 /* reshape_progress is increasing */
2988 if (__le64_to_cpu(bsb.arraystart) + __le64_to_cpu(bsb.length) <
ea0ebe96
N
2989 info->reshape_progress) {
2990 nonew:
2991 if (verbose)
2992 fprintf(stderr, Name ": backup-metadata found on %s but is not needed\n", devname);
e9e43ec3 2993 continue; /* No new data here */
ea0ebe96 2994 }
e9e43ec3
N
2995 } else {
2996 /* reshape_progress is decreasing */
2997 if (__le64_to_cpu(bsb.arraystart) >=
2998 info->reshape_progress)
ea0ebe96 2999 goto nonew; /* No new data here */
e9e43ec3
N
3000 }
3001 } else {
3002 if (info->delta_disks >= 0) {
3003 /* reshape_progress is increasing */
3004 if (__le64_to_cpu(bsb.arraystart) + __le64_to_cpu(bsb.length) <
3005 info->reshape_progress &&
3006 __le64_to_cpu(bsb.arraystart2) + __le64_to_cpu(bsb.length2) <
3007 info->reshape_progress)
ea0ebe96 3008 goto nonew; /* No new data here */
e9e43ec3
N
3009 } else {
3010 /* reshape_progress is decreasing */
3011 if (__le64_to_cpu(bsb.arraystart) >=
3012 info->reshape_progress &&
3013 __le64_to_cpu(bsb.arraystart2) >=
3014 info->reshape_progress)
ea0ebe96 3015 goto nonew; /* No new data here */
e9e43ec3
N
3016 }
3017 }
ea0ebe96
N
3018 if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0) {
3019 second_fail:
3020 if (verbose)
3021 fprintf(stderr, Name ": Failed to verify secondary backup-metadata block on %s\n",
3022 devname);
353632d9 3023 continue; /* Cannot seek */
ea0ebe96 3024 }
2efedc7b 3025 /* There should be a duplicate backup superblock 4k before here */
06b0d786 3026 if (lseek64(fd, -4096, 1) < 0 ||
0155af90 3027 read(fd, &bsb2, sizeof(bsb2)) != sizeof(bsb2))
ea0ebe96 3028 goto second_fail; /* Cannot find leading superblock */
e9e43ec3
N
3029 if (bsb.magic[15] == '1')
3030 bsbsize = offsetof(struct mdp_backup_super, pad1);
3031 else
3032 bsbsize = offsetof(struct mdp_backup_super, pad);
ff94fb86 3033 if (memcmp(&bsb2, &bsb, bsbsize) != 0)
ea0ebe96 3034 goto second_fail; /* Cannot find leading superblock */
2efedc7b 3035
353632d9
NB
3036 /* Now need the data offsets for all devices. */
3037 offsets = malloc(sizeof(*offsets)*info->array.raid_disks);
3038 for(j=0; j<info->array.raid_disks; j++) {
3039 if (fdlist[j] < 0)
3040 continue;
3da92f27 3041 if (st->ss->load_super(st, fdlist[j], NULL))
353632d9
NB
3042 /* FIXME should be this be an error */
3043 continue;
a5d85af7 3044 st->ss->getinfo_super(st, &dinfo, NULL);
3da92f27 3045 st->ss->free_super(st);
14e5b4d7 3046 offsets[j] = dinfo.data_offset * 512;
353632d9
NB
3047 }
3048 printf(Name ": restoring critical section\n");
3049
3050 if (restore_stripes(fdlist, offsets,
3051 info->array.raid_disks,
3052 info->new_chunk,
3053 info->new_level,
3054 info->new_layout,
06b0d786 3055 fd, __le64_to_cpu(bsb.devstart)*512,
92dcdf7c 3056 __le64_to_cpu(bsb.arraystart)*512,
e9e43ec3
N
3057 __le64_to_cpu(bsb.length)*512)) {
3058 /* didn't succeed, so giveup */
ea0ebe96
N
3059 if (verbose)
3060 fprintf(stderr, Name ": Error restoring backup from %s\n",
3061 devname);
e9e43ec3
N
3062 return 1;
3063 }
3064
3065 if (bsb.magic[15] == '2' &&
3066 restore_stripes(fdlist, offsets,
3067 info->array.raid_disks,
3068 info->new_chunk,
3069 info->new_level,
3070 info->new_layout,
3071 fd, __le64_to_cpu(bsb.devstart)*512 +
3072 __le64_to_cpu(bsb.devstart2)*512,
92dcdf7c 3073 __le64_to_cpu(bsb.arraystart2)*512,
e9e43ec3 3074 __le64_to_cpu(bsb.length2)*512)) {
353632d9 3075 /* didn't succeed, so giveup */
ea0ebe96
N
3076 if (verbose)
3077 fprintf(stderr, Name ": Error restoring second backup from %s\n",
3078 devname);
2295250a 3079 return 1;
353632d9
NB
3080 }
3081
e9e43ec3 3082
353632d9
NB
3083 /* Ok, so the data is restored. Let's update those superblocks. */
3084
e9e43ec3
N
3085 if (info->delta_disks >= 0) {
3086 info->reshape_progress = __le64_to_cpu(bsb.arraystart) +
3087 __le64_to_cpu(bsb.length);
3088 if (bsb.magic[15] == '2') {
3089 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2) +
3090 __le64_to_cpu(bsb.length2);
3091 if (p2 > info->reshape_progress)
3092 info->reshape_progress = p2;
3093 }
3094 } else {
3095 info->reshape_progress = __le64_to_cpu(bsb.arraystart);
3096 if (bsb.magic[15] == '2') {
3097 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2);
3098 if (p2 < info->reshape_progress)
3099 info->reshape_progress = p2;
3100 }
3101 }
353632d9
NB
3102 for (j=0; j<info->array.raid_disks; j++) {
3103 if (fdlist[j] < 0) continue;
3da92f27 3104 if (st->ss->load_super(st, fdlist[j], NULL))
353632d9 3105 continue;
a5d85af7 3106 st->ss->getinfo_super(st, &dinfo, NULL);
e9e43ec3 3107 dinfo.reshape_progress = info->reshape_progress;
3da92f27 3108 st->ss->update_super(st, &dinfo,
68c7d6d7
NB
3109 "_reshape_progress",
3110 NULL,0, 0, NULL);
3da92f27
NB
3111 st->ss->store_super(st, fdlist[j]);
3112 st->ss->free_super(st);
353632d9 3113 }
353632d9
NB
3114 return 0;
3115 }
6e9eac4f
NB
3116 /* Didn't find any backup data, try to see if any
3117 * was needed.
3118 */
82f2d6ab
N
3119 if (info->delta_disks < 0) {
3120 /* When shrinking, the critical section is at the end.
3121 * So see if we are before the critical section.
3122 */
3123 unsigned long long first_block;
3124 nstripe = ostripe = 0;
3125 first_block = 0;
3126 while (ostripe >= nstripe) {
3127 ostripe += info->array.chunk_size / 512;
3128 first_block = ostripe * odata;
3129 nstripe = first_block / ndata / (info->new_chunk/512) *
3130 (info->new_chunk/512);
3131 }
3132
3133 if (info->reshape_progress >= first_block)
3134 return 0;
6e9eac4f 3135 }
82f2d6ab
N
3136 if (info->delta_disks > 0) {
3137 /* See if we are beyond the critical section. */
3138 unsigned long long last_block;
3139 nstripe = ostripe = 0;
3140 last_block = 0;
3141 while (nstripe >= ostripe) {
3142 nstripe += info->new_chunk / 512;
3143 last_block = nstripe * ndata;
3144 ostripe = last_block / odata / (info->array.chunk_size/512) *
3145 (info->array.chunk_size/512);
3146 }
6e9eac4f 3147
82f2d6ab
N
3148 if (info->reshape_progress >= last_block)
3149 return 0;
3150 }
6e9eac4f 3151 /* needed to recover critical section! */
ea0ebe96
N
3152 if (verbose)
3153 fprintf(stderr, Name ": Failed to find backup of critical section\n");
2295250a 3154 return 1;
353632d9 3155}
e9e43ec3
N
3156
3157int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
3158 char *backup_file)
3159{
7443ee81 3160 int err = sysfs_set_str(info, NULL, "array_state", "readonly");
e9e43ec3
N
3161 if (err)
3162 return err;
7443ee81 3163 return reshape_array(NULL, mdfd, "array", st, info, 1, backup_file, 0, 0);
e9e43ec3
N
3164}
3165
3166