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