]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Grow.c
udevadm settle in autodetect test
[thirdparty/mdadm.git] / Grow.c
CommitLineData
e5329c37
NB
1/*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
e736b623 4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
e5329c37
NB
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
e736b623 22 * Email: <neilb@suse.de>
e5329c37
NB
23 */
24#include "mdadm.h"
25#include "dlink.h"
7236ee7a 26#include <sys/mman.h>
e5329c37
NB
27
28#if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
29#error no endian defined
30#endif
31#include "md_u.h"
32#include "md_p.h"
33
e9e43ec3
N
34#ifndef offsetof
35#define offsetof(t,f) ((size_t)&(((t*)0)->f))
36#endif
37
e5329c37
NB
38int Grow_Add_device(char *devname, int fd, char *newdev)
39{
40 /* Add a device to an active array.
41 * Currently, just extend a linear array.
42 * This requires writing a new superblock on the
43 * new device, calling the kernel to add the device,
44 * and if that succeeds, update the superblock on
45 * all other devices.
46 * This means that we need to *find* all other devices.
47 */
4b1ac34b
NB
48 struct mdinfo info;
49
e5329c37
NB
50 struct stat stb;
51 int nfd, fd2;
52 int d, nd;
82d9eba6 53 struct supertype *st = NULL;
aba69144 54
e5329c37 55
4b1ac34b 56 if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
e5329c37
NB
57 fprintf(stderr, Name ": cannot get array info for %s\n", devname);
58 return 1;
59 }
60
1686dc25 61 st = super_by_fd(fd);
82d9eba6 62 if (!st) {
f9ce90ba
NB
63 fprintf(stderr, Name ": cannot handle arrays with superblock version %d\n", info.array.major_version);
64 return 1;
65 }
66
4b1ac34b 67 if (info.array.level != -1) {
e5329c37
NB
68 fprintf(stderr, Name ": can only add devices to linear arrays\n");
69 return 1;
70 }
71
6416d527 72 nfd = open(newdev, O_RDWR|O_EXCL|O_DIRECT);
e5329c37
NB
73 if (nfd < 0) {
74 fprintf(stderr, Name ": cannot open %s\n", newdev);
75 return 1;
76 }
77 fstat(nfd, &stb);
78 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
79 fprintf(stderr, Name ": %s is not a block device!\n", newdev);
80 close(nfd);
81 return 1;
82 }
83 /* now check out all the devices and make sure we can read the superblock */
4b1ac34b 84 for (d=0 ; d < info.array.raid_disks ; d++) {
e5329c37
NB
85 mdu_disk_info_t disk;
86 char *dv;
87
88 disk.number = d;
89 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
90 fprintf(stderr, Name ": cannot get device detail for device %d\n",
91 d);
92 return 1;
93 }
16c6fa80 94 dv = map_dev(disk.major, disk.minor, 1);
e5329c37
NB
95 if (!dv) {
96 fprintf(stderr, Name ": cannot find device file for device %d\n",
97 d);
98 return 1;
99 }
16c6fa80 100 fd2 = dev_open(dv, O_RDWR);
e5329c37
NB
101 if (!fd2) {
102 fprintf(stderr, Name ": cannot open device file %s\n", dv);
103 return 1;
104 }
3da92f27
NB
105 st->ss->free_super(st);
106
107 if (st->ss->load_super(st, fd2, NULL)) {
e5329c37
NB
108 fprintf(stderr, Name ": cannot find super block on %s\n", dv);
109 close(fd2);
110 return 1;
111 }
112 close(fd2);
113 }
114 /* Ok, looks good. Lets update the superblock and write it out to
115 * newdev.
116 */
aba69144 117
4b1ac34b
NB
118 info.disk.number = d;
119 info.disk.major = major(stb.st_rdev);
120 info.disk.minor = minor(stb.st_rdev);
121 info.disk.raid_disk = d;
122 info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE);
3da92f27 123 st->ss->update_super(st, &info, "linear-grow-new", newdev,
f752781f 124 0, 0, NULL);
e5329c37 125
3da92f27 126 if (st->ss->store_super(st, nfd)) {
f752781f
NB
127 fprintf(stderr, Name ": Cannot store new superblock on %s\n",
128 newdev);
e5329c37
NB
129 close(nfd);
130 return 1;
131 }
e5329c37 132 close(nfd);
4b1ac34b
NB
133
134 if (ioctl(fd, ADD_NEW_DISK, &info.disk) != 0) {
e5329c37
NB
135 fprintf(stderr, Name ": Cannot add new disk to this array\n");
136 return 1;
137 }
138 /* Well, that seems to have worked.
139 * Now go through and update all superblocks
140 */
141
4b1ac34b 142 if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
e5329c37
NB
143 fprintf(stderr, Name ": cannot get array info for %s\n", devname);
144 return 1;
145 }
146
147 nd = d;
4b1ac34b 148 for (d=0 ; d < info.array.raid_disks ; d++) {
e5329c37
NB
149 mdu_disk_info_t disk;
150 char *dv;
151
152 disk.number = d;
153 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
154 fprintf(stderr, Name ": cannot get device detail for device %d\n",
155 d);
156 return 1;
157 }
16c6fa80 158 dv = map_dev(disk.major, disk.minor, 1);
e5329c37
NB
159 if (!dv) {
160 fprintf(stderr, Name ": cannot find device file for device %d\n",
161 d);
162 return 1;
163 }
16c6fa80 164 fd2 = dev_open(dv, O_RDWR);
e5329c37
NB
165 if (fd2 < 0) {
166 fprintf(stderr, Name ": cannot open device file %s\n", dv);
167 return 1;
168 }
3da92f27 169 if (st->ss->load_super(st, fd2, NULL)) {
e5329c37
NB
170 fprintf(stderr, Name ": cannot find super block on %s\n", dv);
171 close(fd);
172 return 1;
173 }
4b1ac34b
NB
174 info.array.raid_disks = nd+1;
175 info.array.nr_disks = nd+1;
176 info.array.active_disks = nd+1;
177 info.array.working_disks = nd+1;
f752781f 178
3da92f27 179 st->ss->update_super(st, &info, "linear-grow-update", dv,
f752781f 180 0, 0, NULL);
aba69144 181
3da92f27 182 if (st->ss->store_super(st, fd2)) {
e5329c37
NB
183 fprintf(stderr, Name ": Cannot store new superblock on %s\n", dv);
184 close(fd2);
185 return 1;
186 }
187 close(fd2);
188 }
189
190 return 0;
191}
f5e166fe 192
8fac0577 193int Grow_addbitmap(char *devname, int fd, char *file, int chunk, int delay, int write_behind, int force)
f5e166fe
NB
194{
195 /*
196 * First check that array doesn't have a bitmap
197 * Then create the bitmap
198 * Then add it
199 *
200 * For internal bitmaps, we need to check the version,
201 * find all the active devices, and write the bitmap block
202 * to all devices
203 */
204 mdu_bitmap_file_t bmf;
205 mdu_array_info_t array;
206 struct supertype *st;
dcec9ee5
NB
207 int major = BITMAP_MAJOR_HI;
208 int vers = md_get_version(fd);
8fac0577 209 unsigned long long bitmapsize, array_size;
dcec9ee5
NB
210
211 if (vers < 9003) {
212 major = BITMAP_MAJOR_HOSTENDIAN;
213#ifdef __BIG_ENDIAN
214 fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
215 " between different architectured. Consider upgrading the Linux kernel.\n");
216#endif
217 }
f5e166fe
NB
218
219 if (ioctl(fd, GET_BITMAP_FILE, &bmf) != 0) {
353632d9 220 if (errno == ENOMEM)
f5e166fe
NB
221 fprintf(stderr, Name ": Memory allocation failure.\n");
222 else
223 fprintf(stderr, Name ": bitmaps not supported by this kernel.\n");
224 return 1;
225 }
226 if (bmf.pathname[0]) {
fe80f49b
NB
227 if (strcmp(file,"none")==0) {
228 if (ioctl(fd, SET_BITMAP_FILE, -1)!= 0) {
229 fprintf(stderr, Name ": failed to remove bitmap %s\n",
230 bmf.pathname);
231 return 1;
232 }
233 return 0;
234 }
f5e166fe
NB
235 fprintf(stderr, Name ": %s already has a bitmap (%s)\n",
236 devname, bmf.pathname);
237 return 1;
238 }
239 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
240 fprintf(stderr, Name ": cannot get array status for %s\n", devname);
241 return 1;
242 }
243 if (array.state & (1<<MD_SB_BITMAP_PRESENT)) {
fe80f49b
NB
244 if (strcmp(file, "none")==0) {
245 array.state &= ~(1<<MD_SB_BITMAP_PRESENT);
246 if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
247 fprintf(stderr, Name ": failed to remove internal bitmap.\n");
248 return 1;
249 }
250 return 0;
251 }
f5e166fe
NB
252 fprintf(stderr, Name ": Internal bitmap already present on %s\n",
253 devname);
254 return 1;
255 }
5b28bd56
NB
256 if (array.level <= 0) {
257 fprintf(stderr, Name ": Bitmaps not meaningful with level %s\n",
258 map_num(pers, array.level)?:"of this array");
259 return 1;
260 }
8fac0577
NB
261 bitmapsize = array.size;
262 bitmapsize <<= 1;
beae1dfe 263 if (get_dev_size(fd, NULL, &array_size) &&
8fac0577
NB
264 array_size > (0x7fffffffULL<<9)) {
265 /* Array is big enough that we cannot trust array.size
266 * try other approaches
267 */
268 bitmapsize = get_component_size(fd);
269 }
8fac0577
NB
270 if (bitmapsize == 0) {
271 fprintf(stderr, Name ": Cannot reliably determine size of array to create bitmap - sorry.\n");
272 return 1;
273 }
274
f9c25f1d 275 if (array.level == 10) {
8686f3ed 276 int ncopies = (array.layout&255)*((array.layout>>8)&255);
f9c25f1d
NB
277 bitmapsize = bitmapsize * array.raid_disks / ncopies;
278 }
279
1686dc25 280 st = super_by_fd(fd);
f5e166fe
NB
281 if (!st) {
282 fprintf(stderr, Name ": Cannot understand version %d.%d\n",
283 array.major_version, array.minor_version);
284 return 1;
285 }
fe80f49b
NB
286 if (strcmp(file, "none") == 0) {
287 fprintf(stderr, Name ": no bitmap found on %s\n", devname);
288 return 1;
289 } else if (strcmp(file, "internal") == 0) {
f5e166fe 290 int d;
ebeb3663
N
291 if (st->ss->add_internal_bitmap == NULL) {
292 fprintf(stderr, Name ": Internal bitmaps not supported "
293 "with %s metadata\n", st->ss->name);
294 return 1;
295 }
ea329559 296 for (d=0; d< st->max_devs; d++) {
f5e166fe
NB
297 mdu_disk_info_t disk;
298 char *dv;
299 disk.number = d;
300 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
301 continue;
302 if (disk.major == 0 &&
303 disk.minor == 0)
304 continue;
305 if ((disk.state & (1<<MD_DISK_SYNC))==0)
306 continue;
16c6fa80 307 dv = map_dev(disk.major, disk.minor, 1);
f5e166fe 308 if (dv) {
16c6fa80 309 int fd2 = dev_open(dv, O_RDWR);
f5e166fe
NB
310 if (fd2 < 0)
311 continue;
3da92f27 312 if (st->ss->load_super(st, fd2, NULL)==0) {
199171a2 313 if (st->ss->add_internal_bitmap(
3da92f27 314 st,
199171a2
NB
315 &chunk, delay, write_behind,
316 bitmapsize, 0, major)
317 )
3da92f27 318 st->ss->write_bitmap(st, fd2);
21e92547
NB
319 else {
320 fprintf(stderr, Name ": failed to create internal bitmap - chunksize problem.\n");
321 close(fd2);
322 return 1;
323 }
f5e166fe
NB
324 }
325 close(fd2);
326 }
327 }
328 array.state |= (1<<MD_SB_BITMAP_PRESENT);
329 if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
330 fprintf(stderr, Name ": failed to set internal bitmap.\n");
331 return 1;
332 }
fe80f49b
NB
333 } else {
334 int uuid[4];
335 int bitmap_fd;
336 int d;
337 int max_devs = st->max_devs;
fe80f49b
NB
338
339 /* try to load a superblock */
340 for (d=0; d<max_devs; d++) {
341 mdu_disk_info_t disk;
342 char *dv;
343 int fd2;
344 disk.number = d;
345 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
346 continue;
347 if ((disk.major==0 && disk.minor==0) ||
348 (disk.state & (1<<MD_DISK_REMOVED)))
349 continue;
16c6fa80 350 dv = map_dev(disk.major, disk.minor, 1);
fe80f49b 351 if (!dv) continue;
16c6fa80 352 fd2 = dev_open(dv, O_RDONLY);
fe80f49b 353 if (fd2 >= 0 &&
3da92f27 354 st->ss->load_super(st, fd2, NULL) == 0) {
fe80f49b 355 close(fd2);
3da92f27 356 st->ss->uuid_from_super(st, uuid);
fe80f49b
NB
357 break;
358 }
359 close(fd2);
360 }
361 if (d == max_devs) {
362 fprintf(stderr, Name ": cannot find UUID for array!\n");
363 return 1;
364 }
8fac0577 365 if (CreateBitmap(file, force, (char*)uuid, chunk,
f9c25f1d 366 delay, write_behind, bitmapsize, major)) {
fe80f49b
NB
367 return 1;
368 }
369 bitmap_fd = open(file, O_RDWR);
370 if (bitmap_fd < 0) {
8fac0577 371 fprintf(stderr, Name ": weird: %s cannot be opened\n",
fe80f49b
NB
372 file);
373 return 1;
374 }
375 if (ioctl(fd, SET_BITMAP_FILE, bitmap_fd) < 0) {
376 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
377 devname, strerror(errno));
378 return 1;
379 }
380 }
f5e166fe
NB
381
382 return 0;
383}
384
e86c9dd6
NB
385
386/*
387 * When reshaping an array we might need to backup some data.
388 * This is written to all spares with a 'super_block' describing it.
ff94fb86 389 * The superblock goes 4K from the end of the used space on the
e86c9dd6
NB
390 * device.
391 * It if written after the backup is complete.
392 * It has the following structure.
393 */
394
5fdf37e3 395static struct mdp_backup_super {
7236ee7a 396 char magic[16]; /* md_backup_data-1 or -2 */
e86c9dd6
NB
397 __u8 set_uuid[16];
398 __u64 mtime;
399 /* start/sizes in 512byte sectors */
7236ee7a 400 __u64 devstart; /* address on backup device/file of data */
e86c9dd6
NB
401 __u64 arraystart;
402 __u64 length;
403 __u32 sb_csum; /* csum of preceeding bytes. */
7236ee7a
N
404 __u32 pad1;
405 __u64 devstart2; /* offset in to data of second section */
406 __u64 arraystart2;
407 __u64 length2;
408 __u32 sb_csum2; /* csum of preceeding bytes. */
409 __u8 pad[512-68-32];
5fdf37e3 410} __attribute__((aligned(512))) bsb, bsb2;
e86c9dd6 411
f21e18ca 412__u32 bsb_csum(char *buf, int len)
e86c9dd6
NB
413{
414 int i;
415 int csum = 0;
416 for (i=0; i<len; i++)
417 csum = (csum<<3) + buf[0];
418 return __cpu_to_le32(csum);
419}
420
7236ee7a
N
421static int child_grow(int afd, struct mdinfo *sra, unsigned long blocks,
422 int *fds, unsigned long long *offsets,
423 int disks, int chunk, int level, int layout, int data,
424 int dests, int *destfd, unsigned long long *destoffsets);
425static int child_shrink(int afd, struct mdinfo *sra, unsigned long blocks,
426 int *fds, unsigned long long *offsets,
427 int disks, int chunk, int level, int layout, int data,
428 int dests, int *destfd, unsigned long long *destoffsets);
429static int child_same_size(int afd, struct mdinfo *sra, unsigned long blocks,
430 int *fds, unsigned long long *offsets,
e9e43ec3 431 unsigned long long start,
7236ee7a
N
432 int disks, int chunk, int level, int layout, int data,
433 int dests, int *destfd, unsigned long long *destoffsets);
434
435int freeze_array(struct mdinfo *sra)
436{
437 /* Try to freeze resync on this array.
438 * Return -1 if the array is busy,
439 * return 0 if this kernel doesn't support 'frozen'
440 * return 1 if it worked.
441 */
442 char buf[20];
443 if (sysfs_get_str(sra, NULL, "sync_action", buf, 20) <= 0)
444 return 0;
445 if (strcmp(buf, "idle\n") != 0 &&
446 strcmp(buf, "frozen\n") != 0)
447 return -1;
448 if (sysfs_set_str(sra, NULL, "sync_action", "frozen") < 0)
449 return 0;
450 return 1;
451}
452
453void unfreeze_array(struct mdinfo *sra, int frozen)
454{
455 /* If 'frozen' is 1, unfreeze the array */
456 if (frozen > 0)
457 sysfs_set_str(sra, NULL, "sync_action", "idle");
458}
459
460void wait_reshape(struct mdinfo *sra)
461{
462 int fd = sysfs_get_fd(sra, NULL, "sync_action");
463 char action[20];
464
465 do {
466 fd_set rfds;
467 FD_ZERO(&rfds);
468 FD_SET(fd, &rfds);
469 select(fd+1, NULL, NULL, &rfds, NULL);
470
471 if (sysfs_fd_get_str(fd, action, 20) < 0) {
472 close(fd);
473 return;
474 }
475 } while (strncmp(action, "reshape", 7) == 0);
476}
477
478
06b0d786 479int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
e86c9dd6 480 long long size,
19678e53 481 int level, char *layout_str, int chunksize, int raid_disks)
e86c9dd6
NB
482{
483 /* Make some changes in the shape of an array.
484 * The kernel must support the change.
7236ee7a
N
485 *
486 * There are three different changes. Each can trigger
487 * a resync or recovery so we freeze that until we have
488 * requested everything (if kernel supports freezing - 2.6.30).
489 * The steps are:
490 * - change size (i.e. component_size)
491 * - change level
492 * - change layout/chunksize/ndisks
493 *
494 * The last can require a reshape. It is different on different
495 * levels so we need to check the level before actioning it.
496 * Some times the level change needs to be requested after the
497 * reshape (e.g. raid6->raid5, raid5->raid0)
498 *
e86c9dd6 499 */
7236ee7a 500 struct mdu_array_info_s array, orig;
e86c9dd6 501 char *c;
7236ee7a 502 int rv = 0;
e86c9dd6
NB
503 struct supertype *st;
504
e86c9dd6
NB
505 int nchunk, ochunk;
506 int nlayout, olayout;
507 int ndisks, odisks;
f21e18ca 508 unsigned int ndata, odata;
7236ee7a
N
509 int orig_level = UnSet;
510 char alt_layout[40];
e86c9dd6
NB
511 int *fdlist;
512 unsigned long long *offsets;
7236ee7a 513 int d, i;
e86c9dd6
NB
514 int nrdisks;
515 int err;
7236ee7a
N
516 int frozen;
517 unsigned long a,b, blocks, stripes;
f21e18ca 518 unsigned long cache;
7236ee7a
N
519 unsigned long long array_size;
520 int changed = 0;
521 int done;
e86c9dd6 522
7e0f6979 523 struct mdinfo *sra;
06c7f68e 524 struct mdinfo *sd;
e86c9dd6
NB
525
526 if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) {
527 fprintf(stderr, Name ": %s is not an active md array - aborting\n",
528 devname);
529 return 1;
530 }
24d40069 531
9ce510be
N
532 if (size >= 0 &&
533 (chunksize || level!= UnSet || layout_str || raid_disks)) {
534 fprintf(stderr, Name ": cannot change component size at the same time "
535 "as other changes.\n"
536 " Change size first, then check data is intact before "
537 "making other changes.\n");
538 return 1;
539 }
540
24d40069
N
541 if (raid_disks && raid_disks < array.raid_disks && array.level > 1 &&
542 get_linux_version() < 2006032 &&
543 !check_env("MDADM_FORCE_FEWER")) {
544 fprintf(stderr, Name ": reducing the number of devices is not safe before Linux 2.6.32\n"
545 " Please use a newer kernel\n");
546 return 1;
547 }
7236ee7a 548 sra = sysfs_read(fd, 0, GET_LEVEL);
b526e52d
DW
549 if (sra)
550 frozen = freeze_array(sra);
551 else {
552 fprintf(stderr, Name ": failed to read sysfs parameters for %s\n",
553 devname);
554 return 1;
555 }
7236ee7a
N
556 if (frozen < 0) {
557 fprintf(stderr, Name ": %s is performing resync/recovery and cannot"
558 " be reshaped\n", devname);
559 return 1;
560 }
561
562 /* ========= set size =============== */
563 if (size >= 0 && (size == 0 || size != array.size)) {
564 array.size = size;
565 if (array.size != size) {
566 /* got truncated to 32bit, write to
567 * component_size instead
568 */
569 if (sra)
570 rv = sysfs_set_num(sra, NULL,
571 "component_size", size);
572 else
573 rv = -1;
574 } else
575 rv = ioctl(fd, SET_ARRAY_INFO, &array);
576 if (rv != 0) {
39bbb392 577 int err = errno;
7236ee7a 578 fprintf(stderr, Name ": Cannot set device size for %s: %s\n",
39bbb392
N
579 devname, strerror(err));
580 if (err == EBUSY &&
581 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
582 fprintf(stderr, " Bitmap must be removed before size can be changed\n");
7236ee7a
N
583 rv = 1;
584 goto release;
585 }
586 ioctl(fd, GET_ARRAY_INFO, &array);
be1cabbd 587 size = get_component_size(fd)/2;
f98841b3
N
588 if (size == 0)
589 size = array.size;
7236ee7a 590 if (!quiet)
f98841b3
N
591 fprintf(stderr, Name ": component size of %s has been set to %lluK\n",
592 devname, size);
7236ee7a 593 changed = 1;
f98841b3 594 } else {
be1cabbd 595 size = get_component_size(fd)/2;
f98841b3
N
596 if (size == 0)
597 size = array.size;
7236ee7a
N
598 }
599
600 /* ======= set level =========== */
601 if (level != UnSet && level != array.level) {
602 /* Trying to change the level.
603 * We might need to change layout first and schedule a
604 * level change for later.
605 * Level changes that can happen immediately are:
606 * 0->4,5,6 1->5 4->5,6 5->1,6
607 * Level changes that need a layout change first are:
608 * 6->5,4,0 : need a -6 layout, or parity-last
609 * 5->4,0 : need parity-last
610 */
611 if ((array.level == 6 || array.level == 5) &&
612 (level == 5 || level == 4 || level == 0)) {
613 /* Don't change level yet, but choose intermediate
614 * layout
615 */
616 if (level == 5) {
617 if (layout_str == NULL)
618 switch (array.layout) {
619 case ALGORITHM_LEFT_ASYMMETRIC:
620 case ALGORITHM_LEFT_ASYMMETRIC_6:
621 case ALGORITHM_ROTATING_N_RESTART:
622 layout_str = "left-asymmetric-6";
623 break;
624 case ALGORITHM_LEFT_SYMMETRIC:
625 case ALGORITHM_LEFT_SYMMETRIC_6:
626 case ALGORITHM_ROTATING_N_CONTINUE:
627 layout_str = "left-symmetric-6";
628 break;
629 case ALGORITHM_RIGHT_ASYMMETRIC:
630 case ALGORITHM_RIGHT_ASYMMETRIC_6:
631 case ALGORITHM_ROTATING_ZERO_RESTART:
632 layout_str = "right-asymmetric-6";
633 break;
634 case ALGORITHM_RIGHT_SYMMETRIC:
635 case ALGORITHM_RIGHT_SYMMETRIC_6:
636 layout_str = "right-symmetric-6";
637 break;
638 case ALGORITHM_PARITY_0:
639 case ALGORITHM_PARITY_0_6:
640 layout_str = "parity-first-6";
641 break;
642 case ALGORITHM_PARITY_N:
643 layout_str = "parity-last";
644 break;
645 default:
646 fprintf(stderr, Name ": %s: cannot"
647 "convert layout to RAID5 equivalent\n",
648 devname);
649 rv = 1;
650 goto release;
651 }
652 else {
653 int l = map_name(r5layout, layout_str);
654 if (l == UnSet) {
655 fprintf(stderr, Name ": %s: layout '%s' not recognised\n",
656 devname, layout_str);
657 rv = 1;
658 goto release;
659 }
660 if (l != ALGORITHM_PARITY_N) {
661 /* need the -6 version */
662 char *ls = map_num(r5layout, l);
663 strcat(strcpy(alt_layout, ls),
664 "-6");
665 layout_str = alt_layout;
666 }
667 }
668 if (raid_disks)
24d40069 669 /* The final raid6->raid5 conversion
7236ee7a
N
670 * will reduce the number of disks,
671 * so now we need to aim higher
672 */
673 raid_disks++;
674 } else
675 layout_str = "parity-last";
676 } else {
677 c = map_num(pers, level);
b5ea446a
N
678 if (c == NULL) {
679 rv = 1;/* not possible */
680 goto release;
681 }
7236ee7a
N
682 err = sysfs_set_str(sra, NULL, "level", c);
683 if (err) {
39bbb392 684 err = errno;
7236ee7a
N
685 fprintf(stderr, Name ": %s: could not set level to %s\n",
686 devname, c);
39bbb392
N
687 if (err == EBUSY &&
688 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
689 fprintf(stderr, " Bitmap must be removed before level can be changed\n");
7236ee7a
N
690 rv = 1;
691 goto release;
692 }
693 orig = array;
694 orig_level = orig.level;
695 ioctl(fd, GET_ARRAY_INFO, &array);
696 if (layout_str == NULL &&
697 orig.level == 5 && level == 6 &&
698 array.layout != orig.layout)
699 layout_str = map_num(r5layout, orig.layout);
700 if (!quiet)
701 fprintf(stderr, Name " level of %s changed to %s\n",
702 devname, c);
703 changed = 1;
704 }
705 }
706
707 /* ========= set shape (chunk_size / layout / ndisks) ============== */
708 /* Check if layout change is a no-op */
709 if (layout_str) switch(array.level) {
710 case 5:
711 if (array.layout == map_name(r5layout, layout_str))
712 layout_str = NULL;
713 break;
714 case 6:
715 if (layout_str == NULL &&
716 ((chunksize && chunksize * 1024 != array.chunk_size) ||
717 (raid_disks && raid_disks != array.raid_disks)) &&
718 array.layout >= 16) {
719 fprintf(stderr, Name
720 ": %s has a non-standard layout. If you wish to preserve this\n"
721 " during the reshape, please specify --layout=preserve\n"
722 " If you want to change it, specify a layout or use --layout=normalise\n",
723 devname);
724 rv = 1;
725 goto release;
726 }
727 if (strcmp(layout_str, "normalise") == 0 ||
728 strcmp(layout_str, "normalize") == 0) {
729 char *hyphen;
730 strcpy(alt_layout, map_num(r6layout, array.layout));
731 hyphen = strrchr(alt_layout, '-');
732 if (hyphen && strcmp(hyphen, "-6") == 0) {
733 *hyphen = 0;
734 layout_str = alt_layout;
735 }
736 }
737
738 if (array.layout == map_name(r6layout, layout_str))
739 layout_str = NULL;
740 if (layout_str && strcmp(layout_str, "preserve") == 0)
741 layout_str = NULL;
742 break;
743 }
744 if (layout_str == NULL
745 && (chunksize == 0 || chunksize*1024 == array.chunk_size)
746 && (raid_disks == 0 || raid_disks == array.raid_disks)) {
747 rv = 0;
748 if (level != UnSet && level != array.level) {
749 /* Looks like this level change doesn't need
750 * a reshape after all.
751 */
752 c = map_num(pers, level);
753 if (c) {
754 rv = sysfs_set_str(sra, NULL, "level", c);
39bbb392
N
755 if (rv) {
756 int err = errno;
7236ee7a
N
757 fprintf(stderr, Name ": %s: could not set level to %s\n",
758 devname, c);
39bbb392
N
759 if (err == EBUSY &&
760 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
761 fprintf(stderr, " Bitmap must be removed before level can be changed\n");
b7e734fc 762 rv = 1;
39bbb392 763 }
7236ee7a
N
764 }
765 } else if (!changed && !quiet)
766 fprintf(stderr, Name ": %s: no change requested\n",
767 devname);
768 goto release;
769 }
770
e86c9dd6
NB
771 c = map_num(pers, array.level);
772 if (c == NULL) c = "-unknown-";
773 switch(array.level) {
774 default: /* raid0, linear, multipath cannot be reconfigured */
775 fprintf(stderr, Name ": %s array %s cannot be reshaped.\n",
776 c, devname);
7236ee7a
N
777 rv = 1;
778 break;
e86c9dd6
NB
779
780 case LEVEL_FAULTY: /* only 'layout' change is permitted */
781
e86c9dd6
NB
782 if (chunksize || raid_disks) {
783 fprintf(stderr, Name ": %s: Cannot change chunksize or disks of a 'faulty' array\n",
784 devname);
7236ee7a
N
785 rv = 1;
786 break;
e86c9dd6 787 }
19678e53 788 if (layout_str == NULL)
7236ee7a 789 break; /* nothing to do.... */
e86c9dd6 790
19678e53
N
791 array.layout = parse_layout_faulty(layout_str);
792 if (array.layout < 0) {
793 fprintf(stderr, Name ": %s: layout %s not understood for 'faulty' array\n",
794 devname, layout_str);
7236ee7a
N
795 rv = 1;
796 break;
19678e53 797 }
e86c9dd6
NB
798 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
799 fprintf(stderr, Name ": Cannot set layout for %s: %s\n",
800 devname, strerror(errno));
7236ee7a
N
801 rv = 1;
802 } else if (!quiet)
e86c9dd6 803 printf("layout for %s set to %d\n", devname, array.layout);
7236ee7a 804 break;
e86c9dd6 805
7236ee7a 806 case 1: /* only raid_disks can each be changed. */
e86c9dd6 807
19678e53 808 if (chunksize || layout_str != NULL) {
7236ee7a 809 fprintf(stderr, Name ": %s: Cannot change chunk size or layout for a RAID1 array.\n",
e86c9dd6 810 devname);
7236ee7a
N
811 rv = 1;
812 break;
e86c9dd6 813 }
9860f271 814 if (raid_disks > 0) {
e86c9dd6 815 array.raid_disks = raid_disks;
9860f271
NB
816 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
817 fprintf(stderr, Name ": Cannot set raid-devices for %s: %s\n",
818 devname, strerror(errno));
7236ee7a 819 rv = 1;
9860f271 820 }
e86c9dd6 821 }
7236ee7a 822 break;
e86c9dd6
NB
823
824 case 4:
825 case 5:
826 case 6:
1686dc25 827
7236ee7a
N
828 /*
829 * layout/chunksize/raid_disks can be changed
e86c9dd6 830 * though the kernel may not support it all.
e86c9dd6 831 */
7236ee7a
N
832 st = super_by_fd(fd);
833
834 /*
835 * There are three possibilities.
836 * 1/ The array will shrink.
837 * We need to ensure the reshape will pause before reaching
838 * the 'critical section'. We also need to fork and wait for
839 * that to happen. When it does we
840 * suspend/backup/complete/unfreeze
841 *
842 * 2/ The array will not change size.
843 * This requires that we keep a backup of a sliding window
844 * so that we can restore data after a crash. So we need
845 * to fork and monitor progress.
846 *
847 * 3/ The array will grow. This is relatively easy.
e86c9dd6
NB
848 * However the kernel's restripe routines will cheerfully
849 * overwrite some early data before it is safe. So we
850 * need to make a backup of the early parts of the array
851 * and be ready to restore it if rebuild aborts very early.
852 *
7236ee7a
N
853 * We backup data by writing it to one spare, or to a
854 * file which was given on command line.
e86c9dd6 855 *
7236ee7a 856 * [FOLLOWING IS OLD AND PARTLY WRONG]
e86c9dd6
NB
857 * So: we enumerate the devices in the array and
858 * make sure we can open all of them.
859 * Then we freeze the early part of the array and
860 * backup to the various spares.
861 * Then we request changes and start the reshape.
862 * Monitor progress until it has passed the danger zone.
863 * and finally invalidate the copied data and unfreeze the
864 * start of the array.
865 *
7236ee7a
N
866 * In each case, we first make sure that storage is available
867 * for the required backup.
868 * Then we:
869 * - request the shape change.
870 * - for to handle backup etc.
e86c9dd6 871 */
e86c9dd6
NB
872 nchunk = ochunk = array.chunk_size;
873 nlayout = olayout = array.layout;
874 ndisks = odisks = array.raid_disks;
875
7236ee7a
N
876 if (chunksize) {
877 nchunk = chunksize * 1024;
f98841b3
N
878 if (size % chunksize) {
879 fprintf(stderr, Name ": component size %lluK is not"
7236ee7a 880 " a multiple of chunksize %dK\n",
f98841b3 881 size, chunksize);
7236ee7a
N
882 break;
883 }
884 }
19678e53 885 if (layout_str != NULL)
7236ee7a 886 switch(array.level) {
19678e53
N
887 case 4: /* ignore layout */
888 break;
889 case 5:
890 nlayout = map_name(r5layout, layout_str);
891 if (nlayout == UnSet) {
892 fprintf(stderr, Name ": layout %s not understood for raid5.\n",
893 layout_str);
b5ea446a
N
894 rv = 1;
895 goto release;
19678e53
N
896 }
897 break;
898
899 case 6:
900 nlayout = map_name(r6layout, layout_str);
901 if (nlayout == UnSet) {
902 fprintf(stderr, Name ": layout %s not understood for raid6.\n",
903 layout_str);
b5ea446a
N
904 rv = 1;
905 goto release;
19678e53
N
906 }
907 break;
908 }
e86c9dd6
NB
909 if (raid_disks) ndisks = raid_disks;
910
911 odata = odisks-1;
e86c9dd6 912 ndata = ndisks-1;
7236ee7a
N
913 if (array.level == 6) {
914 odata--; /* number of data disks */
915 ndata--;
e86c9dd6 916 }
7236ee7a 917
d2505cff
N
918 if (odata == ndata &&
919 get_linux_version() < 2006032) {
920 fprintf(stderr, Name ": in-place reshape is not safe before 2.6.32, sorry.\n");
921 break;
922 }
923
7236ee7a 924 /* Check that we can hold all the data */
7236ee7a 925 get_dev_size(fd, NULL, &array_size);
f21e18ca 926 if (ndata * (unsigned long long)size < (array_size/1024)) {
7236ee7a
N
927 fprintf(stderr, Name ": this change will reduce the size of the array.\n"
928 " use --grow --array-size first to truncate array.\n"
929 " e.g. mdadm --grow %s --array-size %llu\n",
f98841b3 930 devname, ndata * size);
7236ee7a
N
931 rv = 1;
932 break;
e86c9dd6 933 }
7236ee7a
N
934
935 /* So how much do we need to backup.
936 * We need an amount of data which is both a whole number of
937 * old stripes and a whole number of new stripes.
938 * So LCM for (chunksize*datadisks).
e86c9dd6 939 */
200871ad
N
940 a = (ochunk/512) * odata;
941 b = (nchunk/512) * ndata;
7236ee7a
N
942 /* Find GCD */
943 while (a != b) {
944 if (a < b)
945 b -= a;
946 if (b < a)
947 a -= b;
e86c9dd6 948 }
7236ee7a 949 /* LCM == product / GCD */
200871ad 950 blocks = (ochunk/512) * (nchunk/512) * odata * ndata / a;
7236ee7a 951
e380d3be
N
952 sysfs_free(sra);
953 sra = sysfs_read(fd, 0,
954 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|
955 GET_CACHE);
956
c03ef02d
N
957 if (!sra) {
958 fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n",
959 devname);
960 rv = 1;
961 break;
962 }
963
eba71529
N
964 if (ndata == odata) {
965 /* Make 'blocks' bigger for better throughput, but
966 * not so big that we reject it below.
1b13faf7 967 * Try for 16 megabytes
eba71529 968 */
1b13faf7
N
969 while (blocks * 32 < sra->component_size &&
970 blocks < 16*1024*2)
971 blocks *= 2;
eba71529 972 } else
7236ee7a
N
973 fprintf(stderr, Name ": Need to backup %luK of critical "
974 "section..\n", blocks/2);
e86c9dd6 975
7236ee7a 976 if (blocks >= sra->component_size/2) {
e86c9dd6
NB
977 fprintf(stderr, Name ": %s: Something wrong - reshape aborted\n",
978 devname);
7236ee7a
N
979 rv = 1;
980 break;
353632d9 981 }
7e0f6979 982 nrdisks = array.nr_disks + sra->array.spare_disks;
e86c9dd6
NB
983 /* Now we need to open all these devices so we can read/write.
984 */
06b0d786
NB
985 fdlist = malloc((1+nrdisks) * sizeof(int));
986 offsets = malloc((1+nrdisks) * sizeof(offsets[0]));
e86c9dd6
NB
987 if (!fdlist || !offsets) {
988 fprintf(stderr, Name ": malloc failed: grow aborted\n");
7236ee7a
N
989 rv = 1;
990 break;
e86c9dd6 991 }
06b0d786 992 for (d=0; d <= nrdisks; d++)
e86c9dd6
NB
993 fdlist[d] = -1;
994 d = array.raid_disks;
995 for (sd = sra->devs; sd; sd=sd->next) {
06c7f68e 996 if (sd->disk.state & (1<<MD_DISK_FAULTY))
e86c9dd6 997 continue;
06c7f68e
NB
998 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
999 char *dn = map_dev(sd->disk.major,
1000 sd->disk.minor, 1);
1001 fdlist[sd->disk.raid_disk]
1002 = dev_open(dn, O_RDONLY);
7236ee7a 1003 offsets[sd->disk.raid_disk] = sd->data_offset*512;
06c7f68e 1004 if (fdlist[sd->disk.raid_disk] < 0) {
e86c9dd6 1005 fprintf(stderr, Name ": %s: cannot open component %s\n",
e81cdd9f 1006 devname, dn?dn:"-unknown-");
7236ee7a
N
1007 rv = 1;
1008 goto release;
e86c9dd6 1009 }
7236ee7a 1010 } else if (backup_file == NULL) {
e86c9dd6 1011 /* spare */
06c7f68e
NB
1012 char *dn = map_dev(sd->disk.major,
1013 sd->disk.minor, 1);
16c6fa80 1014 fdlist[d] = dev_open(dn, O_RDWR);
ff94fb86 1015 offsets[d] = (sd->data_offset + sra->component_size - blocks - 8)*512;
e86c9dd6
NB
1016 if (fdlist[d]<0) {
1017 fprintf(stderr, Name ": %s: cannot open component %s\n",
e81cdd9f 1018 devname, dn?dn:"-unknown");
7236ee7a
N
1019 rv = 1;
1020 goto release;
e86c9dd6
NB
1021 }
1022 d++;
1023 }
1024 }
7236ee7a
N
1025 if (backup_file == NULL) {
1026 if (ndata <= odata) {
1027 fprintf(stderr, Name ": %s: Cannot grow - need backup-file\n",
1028 devname);
1029 rv = 1;
1030 break;
1031 } else if (sra->array.spare_disks == 0) {
1032 fprintf(stderr, Name ": %s: Cannot grow - need a spare or "
1033 "backup-file to backup critical section\n",
1034 devname);
1035 rv = 1;
1036 break;
1037 }
1038 if (d == array.raid_disks) {
1039 fprintf(stderr, Name ": %s: No spare device for backup\n",
1040 devname);
1041 rv = 1;
1042 break;
e86c9dd6 1043 }
7236ee7a
N
1044 } else {
1045 /* need to check backup file is large enough */
1046 char buf[512];
1047 fdlist[d] = open(backup_file, O_RDWR|O_CREAT|O_EXCL,
1048 S_IRUSR | S_IWUSR);
1049 offsets[d] = 8 * 512;
06b0d786
NB
1050 if (fdlist[d] < 0) {
1051 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
1052 devname, backup_file, strerror(errno));
7236ee7a
N
1053 rv = 1;
1054 break;
1055 }
1056 memset(buf, 0, 512);
f21e18ca 1057 for (i=0; i < (signed)blocks + 1 ; i++) {
7236ee7a
N
1058 if (write(fdlist[d], buf, 512) != 512) {
1059 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
1060 devname, backup_file, strerror(errno));
1061 rv = 1;
1062 break;
1063 }
1064 }
1065 if (fsync(fdlist[d]) != 0) {
1066 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
1067 devname, backup_file, strerror(errno));
1068 rv = 1;
1069 break;
06b0d786 1070 }
06b0d786 1071 d++;
06b0d786 1072 }
7236ee7a
N
1073
1074 /* lastly, check that the internal stripe cache is
1075 * large enough, or it won't work.
1076 */
1077
1078 cache = (nchunk < ochunk) ? ochunk : nchunk;
1079 cache = cache * 4 / 4096;
1b13faf7
N
1080 if (cache < blocks / 8 / odisks + 16)
1081 /* Make it big enough to hold 'blocks' */
1082 cache = blocks / 8 / odisks + 16;
7236ee7a
N
1083 if (sra->cache_size < cache)
1084 sysfs_set_num(sra, NULL, "stripe_cache_size",
1085 cache+1);
1086 /* Right, everything seems fine. Let's kick things off.
1087 * If only changing raid_disks, use ioctl, else use
1088 * sysfs.
1089 */
1090 if (ochunk == nchunk && olayout == nlayout) {
1091 array.raid_disks = ndisks;
1092 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
39bbb392 1093 int err = errno;
7236ee7a
N
1094 rv = 1;
1095 fprintf(stderr, Name ": Cannot set device shape for %s: %s\n",
1096 devname, strerror(errno));
1097 if (ndisks < odisks &&
1098 get_linux_version() < 2006030)
1099 fprintf(stderr, Name ": linux 2.6.30 or later required\n");
39bbb392
N
1100 if (err == EBUSY &&
1101 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
1102 fprintf(stderr, " Bitmap must be removed before shape can be changed\n");
7236ee7a
N
1103
1104 break;
1105 }
1106 } else {
1107 /* set them all just in case some old 'new_*' value
1108 * persists from some earlier problem
1109 */
53f50353
N
1110 int err = err; /* only used if rv==1, and always set if
1111 * rv==1, so initialisation not needed,
1112 * despite gcc warning
1113 */
7236ee7a 1114 if (sysfs_set_num(sra, NULL, "chunk_size", nchunk) < 0)
39bbb392
N
1115 rv = 1, err = errno;
1116 if (!rv && sysfs_set_num(sra, NULL, "layout", nlayout) < 0)
1117 rv = 1, err = errno;
1118 if (!rv && sysfs_set_num(sra, NULL, "raid_disks", ndisks) < 0)
1119 rv = 1, err = errno;
7236ee7a
N
1120 if (rv) {
1121 fprintf(stderr, Name ": Cannot set device shape for %s\n",
1122 devname);
1123 if (get_linux_version() < 2006030)
1124 fprintf(stderr, Name ": linux 2.6.30 or later required\n");
39bbb392
N
1125 if (err == EBUSY &&
1126 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
1127 fprintf(stderr, " Bitmap must be removed before shape can be changed\n");
7236ee7a
N
1128 break;
1129 }
e86c9dd6
NB
1130 }
1131
7236ee7a
N
1132 if (ndisks == 2 && odisks == 2) {
1133 /* No reshape is needed in this trivial case */
1134 rv = 0;
1135 break;
1136 }
1137
1138 /* set up the backup-super-block. This requires the
1139 * uuid from the array.
1140 */
e86c9dd6 1141 /* Find a superblock */
7236ee7a
N
1142 for (sd = sra->devs; sd; sd = sd->next) {
1143 char *dn;
1144 int devfd;
1145 int ok;
1146 if (sd->disk.state & (1<<MD_DISK_FAULTY))
1147 continue;
1148 dn = map_dev(sd->disk.major, sd->disk.minor, 1);
1149 devfd = dev_open(dn, O_RDONLY);
1150 if (devfd < 0)
1151 continue;
1152 ok = st->ss->load_super(st, devfd, NULL);
1153 close(devfd);
1154 if (ok >= 0)
1155 break;
1156 }
1157 if (!sd) {
e86c9dd6
NB
1158 fprintf(stderr, Name ": %s: Cannot find a superblock\n",
1159 devname);
7236ee7a
N
1160 rv = 1;
1161 break;
e86c9dd6
NB
1162 }
1163
7236ee7a 1164 memset(&bsb, 0, 512);
2efedc7b 1165 memcpy(bsb.magic, "md_backup_data-1", 16);
3da92f27 1166 st->ss->uuid_from_super(st, (int*)&bsb.set_uuid);
2efedc7b 1167 bsb.mtime = __cpu_to_le64(time(0));
7236ee7a
N
1168 bsb.devstart2 = blocks;
1169 stripes = blocks / (ochunk/512) / odata;
1170 /* Now we just need to kick off the reshape and watch, while
1171 * handling backups of the data...
1172 * This is all done by a forked background process.
2efedc7b 1173 */
7236ee7a
N
1174 switch(fork()) {
1175 case 0:
1176 close(fd);
1177 if (check_env("MDADM_GROW_VERIFY"))
1178 fd = open(devname, O_RDONLY | O_DIRECT);
1179 else
1180 fd = -1;
1181 mlockall(MCL_FUTURE);
1182
1183 if (odata < ndata)
1184 done = child_grow(fd, sra, stripes,
1185 fdlist, offsets,
1186 odisks, ochunk, array.level, olayout, odata,
1187 d - odisks, fdlist+odisks, offsets+odisks);
1188 else if (odata > ndata)
1189 done = child_shrink(fd, sra, stripes,
1190 fdlist, offsets,
1191 odisks, ochunk, array.level, olayout, odata,
1192 d - odisks, fdlist+odisks, offsets+odisks);
1193 else
1194 done = child_same_size(fd, sra, stripes,
1195 fdlist, offsets,
e9e43ec3 1196 0,
7236ee7a
N
1197 odisks, ochunk, array.level, olayout, odata,
1198 d - odisks, fdlist+odisks, offsets+odisks);
1199 if (backup_file && done)
1200 unlink(backup_file);
1201 if (level != UnSet && level != array.level) {
1202 /* We need to wait for the reshape to finish
1203 * (which will have happened unless odata < ndata)
1204 * and then set the level
758d3a8e 1205 */
7236ee7a
N
1206
1207 c = map_num(pers, level);
1208 if (c == NULL)
1209 exit(0);/* not possible */
1210
1211 if (odata < ndata)
1212 wait_reshape(sra);
1213 err = sysfs_set_str(sra, NULL, "level", c);
1214 if (err)
1215 fprintf(stderr, Name ": %s: could not set level to %s\n",
1216 devname, c);
758d3a8e 1217 }
7236ee7a
N
1218 exit(0);
1219 case -1:
1220 fprintf(stderr, Name ": Cannot run child to monitor reshape: %s\n",
1221 strerror(errno));
1222 rv = 1;
1223 break;
1224 default:
1225 /* The child will take care of unfreezing the array */
1226 frozen = 0;
1227 break;
e86c9dd6 1228 }
7236ee7a 1229 break;
e86c9dd6 1230
7236ee7a 1231 }
e86c9dd6 1232
7236ee7a
N
1233 release:
1234 if (rv && orig_level != UnSet && sra) {
1235 c = map_num(pers, orig_level);
1236 if (c && sysfs_set_str(sra, NULL, "level", c) == 0)
1237 fprintf(stderr, Name ": aborting level change\n");
1238 }
1239 if (sra)
1240 unfreeze_array(sra, frozen);
1241 return rv;
1242}
e86c9dd6 1243
7236ee7a
N
1244/*
1245 * We run a child process in the background which performs the following
1246 * steps:
1247 * - wait for resync to reach a certain point
1248 * - suspend io to the following section
1249 * - backup that section
1250 * - allow resync to proceed further
1251 * - resume io
1252 * - discard the backup.
1253 *
1254 * When are combined in slightly different ways in the three cases.
1255 * Grow:
1256 * - suspend/backup/allow/wait/resume/discard
1257 * Shrink:
1258 * - allow/wait/suspend/backup/allow/wait/resume/discard
1259 * same-size:
1260 * - wait/resume/discard/suspend/backup/allow
1261 *
1262 * suspend/backup/allow always come together
1263 * wait/resume/discard do too.
1264 * For the same-size case we have two backups to improve flow.
1265 *
1266 */
e86c9dd6 1267
fcf57625 1268/* FIXME return status is never checked */
7236ee7a
N
1269int grow_backup(struct mdinfo *sra,
1270 unsigned long long offset, /* per device */
1271 unsigned long stripes, /* per device */
1272 int *sources, unsigned long long *offsets,
1273 int disks, int chunk, int level, int layout,
1274 int dests, int *destfd, unsigned long long *destoffsets,
d4445387 1275 int part, int *degraded,
7236ee7a
N
1276 char *buf)
1277{
1278 /* Backup 'blocks' sectors at 'offset' on each device of the array,
1279 * to storage 'destfd' (offset 'destoffsets'), after first
1280 * suspending IO. Then allow resync to continue
1281 * over the suspended section.
1282 * Use part 'part' of the backup-super-block.
1283 */
1284 int odata = disks;
1285 int rv = 0;
1286 int i;
f21e18ca
N
1287 unsigned long long ll;
1288 int new_degraded;
7236ee7a
N
1289 //printf("offset %llu\n", offset);
1290 if (level >= 4)
1291 odata--;
1292 if (level == 6)
1293 odata--;
200871ad 1294 sysfs_set_num(sra, NULL, "suspend_hi", (offset + stripes * (chunk/512)) * odata);
d4445387 1295 /* Check that array hasn't become degraded, else we might backup the wrong data */
f21e18ca
N
1296 sysfs_get_ll(sra, NULL, "degraded", &ll);
1297 new_degraded = (int)ll;
d4445387
N
1298 if (new_degraded != *degraded) {
1299 /* check each device to ensure it is still working */
1300 struct mdinfo *sd;
1301 for (sd = sra->devs ; sd ; sd = sd->next) {
1302 if (sd->disk.state & (1<<MD_DISK_FAULTY))
1303 continue;
1304 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
1305 char sbuf[20];
1306 if (sysfs_get_str(sra, sd, "state", sbuf, 20) < 0 ||
1307 strstr(sbuf, "faulty") ||
1308 strstr(sbuf, "in_sync") == NULL) {
1309 /* this device is dead */
1310 sd->disk.state = (1<<MD_DISK_FAULTY);
1311 if (sd->disk.raid_disk >= 0 &&
1312 sources[sd->disk.raid_disk] >= 0) {
1313 close(sources[sd->disk.raid_disk]);
1314 sources[sd->disk.raid_disk] = -1;
1315 }
1316 }
1317 }
1318 }
1319 *degraded = new_degraded;
1320 }
7236ee7a
N
1321 if (part) {
1322 bsb.arraystart2 = __cpu_to_le64(offset * odata);
200871ad 1323 bsb.length2 = __cpu_to_le64(stripes * (chunk/512) * odata);
7236ee7a
N
1324 } else {
1325 bsb.arraystart = __cpu_to_le64(offset * odata);
200871ad 1326 bsb.length = __cpu_to_le64(stripes * (chunk/512) * odata);
7236ee7a
N
1327 }
1328 if (part)
1329 bsb.magic[15] = '2';
1330 for (i = 0; i < dests; i++)
1331 if (part)
1332 lseek64(destfd[i], destoffsets[i] + __le64_to_cpu(bsb.devstart2)*512, 0);
1333 else
1334 lseek64(destfd[i], destoffsets[i], 0);
1335
1336 rv = save_stripes(sources, offsets,
1337 disks, chunk, level, layout,
1338 dests, destfd,
1339 offset*512*odata, stripes * chunk * odata,
1340 buf);
1341
1342 if (rv)
1343 return rv;
97396422 1344 bsb.mtime = __cpu_to_le64(time(0));
7236ee7a
N
1345 for (i = 0; i < dests; i++) {
1346 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
1347
1348 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
1349 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
1350 bsb.sb_csum2 = bsb_csum((char*)&bsb,
1351 ((char*)&bsb.sb_csum2)-((char*)&bsb));
1352
f21e18ca
N
1353 if ((unsigned long long)lseek64(destfd[i], destoffsets[i] - 4096, 0)
1354 != destoffsets[i] - 4096)
a847575a 1355 rv = 1;
fcf57625 1356 rv = rv ?: write(destfd[i], &bsb, 512);
ff94fb86 1357 if (destoffsets[i] > 4096) {
f21e18ca 1358 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]+stripes*chunk*odata, 0) !=
a847575a
N
1359 destoffsets[i]+stripes*chunk*odata)
1360 rv = 1;
fcf57625 1361 rv = rv ?: write(destfd[i], &bsb, 512);
ff94fb86 1362 }
7236ee7a
N
1363 fsync(destfd[i]);
1364 }
2efedc7b 1365
fcf57625 1366 return rv;
7236ee7a
N
1367}
1368
1369/* in 2.6.30, the value reported by sync_completed can be
1370 * less that it should be by one stripe.
1371 * This only happens when reshape hits sync_max and pauses.
1372 * So allow wait_backup to either extent sync_max further
1373 * than strictly necessary, or return before the
1374 * sync has got quite as far as we would really like.
1375 * This is what 'blocks2' is for.
1376 * The various caller give appropriate values so that
1377 * every works.
1378 */
fcf57625 1379/* FIXME return value is often ignored */
7236ee7a
N
1380int wait_backup(struct mdinfo *sra,
1381 unsigned long long offset, /* per device */
1382 unsigned long long blocks, /* per device */
1383 unsigned long long blocks2, /* per device - hack */
1384 int dests, int *destfd, unsigned long long *destoffsets,
1385 int part)
1386{
1387 /* Wait for resync to pass the section that was backed up
1388 * then erase the backup and allow IO
1389 */
1390 int fd = sysfs_get_fd(sra, NULL, "sync_completed");
1391 unsigned long long completed;
1392 int i;
fcf57625 1393 int rv;
7236ee7a
N
1394
1395 if (fd < 0)
1396 return -1;
1397 sysfs_set_num(sra, NULL, "sync_max", offset + blocks + blocks2);
1398 if (offset == 0)
1399 sysfs_set_str(sra, NULL, "sync_action", "reshape");
1400 do {
1401 char action[20];
1402 fd_set rfds;
1403 FD_ZERO(&rfds);
1404 FD_SET(fd, &rfds);
1405 select(fd+1, NULL, NULL, &rfds, NULL);
1406 if (sysfs_fd_get_ll(fd, &completed) < 0) {
1407 close(fd);
1408 return -1;
e86c9dd6 1409 }
7236ee7a
N
1410 if (sysfs_get_str(sra, NULL, "sync_action",
1411 action, 20) > 0 &&
1412 strncmp(action, "reshape", 7) != 0)
1413 break;
1414 } while (completed < offset + blocks);
1415 close(fd);
1416
1417 if (part) {
1418 bsb.arraystart2 = __cpu_to_le64(0);
1419 bsb.length2 = __cpu_to_le64(0);
1420 } else {
1421 bsb.arraystart = __cpu_to_le64(0);
1422 bsb.length = __cpu_to_le64(0);
1423 }
97396422 1424 bsb.mtime = __cpu_to_le64(time(0));
fcf57625 1425 rv = 0;
7236ee7a
N
1426 for (i = 0; i < dests; i++) {
1427 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
1428 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
1429 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
1430 bsb.sb_csum2 = bsb_csum((char*)&bsb,
1431 ((char*)&bsb.sb_csum2)-((char*)&bsb));
f21e18ca 1432 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]-4096, 0) !=
a847575a
N
1433 destoffsets[i]-4096)
1434 rv = 1;
fcf57625 1435 rv = rv ?: write(destfd[i], &bsb, 512);
7236ee7a
N
1436 fsync(destfd[i]);
1437 }
fcf57625 1438 return rv;
7236ee7a 1439}
e86c9dd6 1440
7236ee7a
N
1441static void fail(char *msg)
1442{
fcf57625
N
1443 int rv;
1444 rv = write(2, msg, strlen(msg));
1445 rv |= write(2, "\n", 1);
1446 exit(rv ? 1 : 2);
7236ee7a
N
1447}
1448
1449static char *abuf, *bbuf;
f21e18ca 1450static unsigned long long abuflen;
7236ee7a
N
1451static void validate(int afd, int bfd, unsigned long long offset)
1452{
1453 /* check that the data in the backup against the array.
1454 * This is only used for regression testing and should not
1455 * be used while the array is active
1456 */
7236ee7a
N
1457 if (afd < 0)
1458 return;
1459 lseek64(bfd, offset - 4096, 0);
1460 if (read(bfd, &bsb2, 512) != 512)
1461 fail("cannot read bsb");
1462 if (bsb2.sb_csum != bsb_csum((char*)&bsb2,
1463 ((char*)&bsb2.sb_csum)-((char*)&bsb2)))
1464 fail("first csum bad");
1465 if (memcmp(bsb2.magic, "md_backup_data", 14) != 0)
1466 fail("magic is bad");
1467 if (memcmp(bsb2.magic, "md_backup_data-2", 16) == 0 &&
1468 bsb2.sb_csum2 != bsb_csum((char*)&bsb2,
1469 ((char*)&bsb2.sb_csum2)-((char*)&bsb2)))
1470 fail("second csum bad");
1471
1472 if (__le64_to_cpu(bsb2.devstart)*512 != offset)
1473 fail("devstart is wrong");
1474
1475 if (bsb2.length) {
1476 unsigned long long len = __le64_to_cpu(bsb2.length)*512;
1477
1478 if (abuflen < len) {
1479 free(abuf);
1480 free(bbuf);
1481 abuflen = len;
fcf57625
N
1482 if (posix_memalign((void**)&abuf, 4096, abuflen) ||
1483 posix_memalign((void**)&bbuf, 4096, abuflen)) {
1484 abuflen = 0;
1485 /* just stop validating on mem-alloc failure */
1486 return;
1487 }
e86c9dd6 1488 }
48924014 1489
7236ee7a 1490 lseek64(bfd, offset, 0);
f21e18ca 1491 if ((unsigned long long)read(bfd, bbuf, len) != len) {
080fd005 1492 //printf("len %llu\n", len);
7236ee7a
N
1493 fail("read first backup failed");
1494 }
1495 lseek64(afd, __le64_to_cpu(bsb2.arraystart)*512, 0);
f21e18ca 1496 if ((unsigned long long)read(afd, abuf, len) != len)
7236ee7a
N
1497 fail("read first from array failed");
1498 if (memcmp(bbuf, abuf, len) != 0) {
080fd005 1499 #if 0
7236ee7a
N
1500 int i;
1501 printf("offset=%llu len=%llu\n",
080fd005 1502 (unsigned long long)__le64_to_cpu(bsb2.arraystart)*512, len);
7236ee7a
N
1503 for (i=0; i<len; i++)
1504 if (bbuf[i] != abuf[i]) {
1505 printf("first diff byte %d\n", i);
93ecfa01 1506 break;
7236ee7a 1507 }
080fd005 1508 #endif
7236ee7a 1509 fail("data1 compare failed");
e86c9dd6 1510 }
7236ee7a
N
1511 }
1512 if (bsb2.length2) {
1513 unsigned long long len = __le64_to_cpu(bsb2.length2)*512;
1514
1515 if (abuflen < len) {
1516 free(abuf);
1517 free(bbuf);
1518 abuflen = len;
1519 abuf = malloc(abuflen);
1520 bbuf = malloc(abuflen);
e86c9dd6
NB
1521 }
1522
7236ee7a 1523 lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0);
f21e18ca 1524 if ((unsigned long long)read(bfd, bbuf, len) != len)
7236ee7a
N
1525 fail("read second backup failed");
1526 lseek64(afd, __le64_to_cpu(bsb2.arraystart2)*512, 0);
f21e18ca 1527 if ((unsigned long long)read(afd, abuf, len) != len)
7236ee7a
N
1528 fail("read second from array failed");
1529 if (memcmp(bbuf, abuf, len) != 0)
1530 fail("data2 compare failed");
e86c9dd6 1531 }
7236ee7a 1532}
e86c9dd6 1533
7236ee7a
N
1534static int child_grow(int afd, struct mdinfo *sra, unsigned long stripes,
1535 int *fds, unsigned long long *offsets,
1536 int disks, int chunk, int level, int layout, int data,
1537 int dests, int *destfd, unsigned long long *destoffsets)
1538{
1539 char *buf;
d4445387 1540 int degraded = 0;
e86c9dd6 1541
fcf57625
N
1542 if (posix_memalign((void**)&buf, 4096, disks * chunk))
1543 /* Don't start the 'reshape' */
1544 return 0;
7236ee7a
N
1545 sysfs_set_num(sra, NULL, "suspend_hi", 0);
1546 sysfs_set_num(sra, NULL, "suspend_lo", 0);
1547 grow_backup(sra, 0, stripes,
1548 fds, offsets, disks, chunk, level, layout,
1549 dests, destfd, destoffsets,
d4445387 1550 0, &degraded, buf);
7236ee7a 1551 validate(afd, destfd[0], destoffsets[0]);
200871ad 1552 wait_backup(sra, 0, stripes * (chunk / 512), stripes * (chunk / 512),
725cac4c
N
1553 dests, destfd, destoffsets,
1554 0);
200871ad 1555 sysfs_set_num(sra, NULL, "suspend_lo", (stripes * (chunk/512)) * data);
7236ee7a
N
1556 free(buf);
1557 /* FIXME this should probably be numeric */
1558 sysfs_set_str(sra, NULL, "sync_max", "max");
e86c9dd6 1559 return 1;
7236ee7a 1560}
e86c9dd6 1561
7236ee7a
N
1562static int child_shrink(int afd, struct mdinfo *sra, unsigned long stripes,
1563 int *fds, unsigned long long *offsets,
1564 int disks, int chunk, int level, int layout, int data,
1565 int dests, int *destfd, unsigned long long *destoffsets)
1566{
1567 char *buf;
1568 unsigned long long start;
1569 int rv;
d4445387 1570 int degraded = 0;
7236ee7a 1571
fcf57625
N
1572 if (posix_memalign((void**)&buf, 4096, disks * chunk))
1573 return 0;
200871ad 1574 start = sra->component_size - stripes * (chunk/512);
7236ee7a
N
1575 sysfs_set_num(sra, NULL, "sync_max", start);
1576 sysfs_set_str(sra, NULL, "sync_action", "reshape");
1577 sysfs_set_num(sra, NULL, "suspend_lo", 0);
1578 sysfs_set_num(sra, NULL, "suspend_hi", 0);
200871ad 1579 rv = wait_backup(sra, 0, start - stripes * (chunk/512), stripes * (chunk/512),
7236ee7a
N
1580 dests, destfd, destoffsets, 0);
1581 if (rv < 0)
1582 return 0;
1583 grow_backup(sra, 0, stripes,
1584 fds, offsets,
1585 disks, chunk, level, layout,
1586 dests, destfd, destoffsets,
d4445387 1587 0, &degraded, buf);
7236ee7a 1588 validate(afd, destfd[0], destoffsets[0]);
200871ad 1589 wait_backup(sra, start, stripes*(chunk/512), 0,
725cac4c 1590 dests, destfd, destoffsets, 0);
200871ad 1591 sysfs_set_num(sra, NULL, "suspend_lo", (stripes * (chunk/512)) * data);
7236ee7a
N
1592 free(buf);
1593 /* FIXME this should probably be numeric */
1594 sysfs_set_str(sra, NULL, "sync_max", "max");
1595 return 1;
1596}
1597
1598static int child_same_size(int afd, struct mdinfo *sra, unsigned long stripes,
1599 int *fds, unsigned long long *offsets,
e9e43ec3 1600 unsigned long long start,
7236ee7a
N
1601 int disks, int chunk, int level, int layout, int data,
1602 int dests, int *destfd, unsigned long long *destoffsets)
1603{
e9e43ec3 1604 unsigned long long size;
7236ee7a
N
1605 unsigned long tailstripes = stripes;
1606 int part;
1607 char *buf;
1608 unsigned long long speed;
d4445387 1609 int degraded = 0;
7236ee7a
N
1610
1611
fcf57625
N
1612 if (posix_memalign((void**)&buf, 4096, disks * chunk))
1613 return 0;
7236ee7a
N
1614
1615 sysfs_set_num(sra, NULL, "suspend_lo", 0);
1616 sysfs_set_num(sra, NULL, "suspend_hi", 0);
1617
1618 sysfs_get_ll(sra, NULL, "sync_speed_min", &speed);
1619 sysfs_set_num(sra, NULL, "sync_speed_min", 200000);
1620
e9e43ec3 1621 grow_backup(sra, start, stripes,
7236ee7a
N
1622 fds, offsets,
1623 disks, chunk, level, layout,
1624 dests, destfd, destoffsets,
d4445387 1625 0, &degraded, buf);
200871ad 1626 grow_backup(sra, (start + stripes) * (chunk/512), stripes,
7236ee7a
N
1627 fds, offsets,
1628 disks, chunk, level, layout,
1629 dests, destfd, destoffsets,
d4445387 1630 1, &degraded, buf);
7236ee7a
N
1631 validate(afd, destfd[0], destoffsets[0]);
1632 part = 0;
e9e43ec3 1633 start += stripes * 2; /* where to read next */
7236ee7a
N
1634 size = sra->component_size / (chunk/512);
1635 while (start < size) {
200871ad
N
1636 if (wait_backup(sra, (start-stripes*2)*(chunk/512),
1637 stripes*(chunk/512), 0,
7236ee7a
N
1638 dests, destfd, destoffsets,
1639 part) < 0)
1640 return 0;
200871ad 1641 sysfs_set_num(sra, NULL, "suspend_lo", start*(chunk/512) * data);
7236ee7a
N
1642 if (start + stripes > size)
1643 tailstripes = (size - start);
1644
200871ad 1645 grow_backup(sra, start*(chunk/512), tailstripes,
7236ee7a
N
1646 fds, offsets,
1647 disks, chunk, level, layout,
1648 dests, destfd, destoffsets,
d4445387 1649 part, &degraded, buf);
7236ee7a
N
1650 start += stripes;
1651 part = 1 - part;
1652 validate(afd, destfd[0], destoffsets[0]);
1653 }
200871ad 1654 if (wait_backup(sra, (start-stripes*2) * (chunk/512), stripes * (chunk/512), 0,
7236ee7a
N
1655 dests, destfd, destoffsets,
1656 part) < 0)
1657 return 0;
200871ad
N
1658 sysfs_set_num(sra, NULL, "suspend_lo", ((start-stripes)*(chunk/512)) * data);
1659 wait_backup(sra, (start-stripes) * (chunk/512), tailstripes * (chunk/512), 0,
725cac4c
N
1660 dests, destfd, destoffsets,
1661 1-part);
200871ad 1662 sysfs_set_num(sra, NULL, "suspend_lo", (size*(chunk/512)) * data);
7236ee7a
N
1663 sysfs_set_num(sra, NULL, "sync_speed_min", speed);
1664 free(buf);
1665 return 1;
e86c9dd6 1666}
353632d9
NB
1667
1668/*
1669 * If any spare contains md_back_data-1 which is recent wrt mtime,
1670 * write that data into the array and update the super blocks with
1671 * the new reshape_progress
1672 */
ea0ebe96
N
1673int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt,
1674 char *backup_file, int verbose)
353632d9
NB
1675{
1676 int i, j;
1677 int old_disks;
353632d9 1678 unsigned long long *offsets;
82f2d6ab 1679 unsigned long long nstripe, ostripe;
6e9eac4f 1680 int ndata, odata;
353632d9 1681
e9e43ec3
N
1682 if (info->new_level != info->array.level)
1683 return 1; /* Cannot handle level changes (they are instantaneous) */
1684
1685 odata = info->array.raid_disks - info->delta_disks - 1;
1686 if (info->array.level == 6) odata--; /* number of data disks */
1687 ndata = info->array.raid_disks - 1;
1688 if (info->new_level == 6) ndata--;
353632d9
NB
1689
1690 old_disks = info->array.raid_disks - info->delta_disks;
1691
e9e43ec3
N
1692 if (info->delta_disks <= 0)
1693 /* Didn't grow, so the backup file must have
1694 * been used
1695 */
1696 old_disks = cnt;
06b0d786 1697 for (i=old_disks-(backup_file?1:0); i<cnt; i++) {
353632d9 1698 struct mdinfo dinfo;
06b0d786 1699 int fd;
e9e43ec3 1700 int bsbsize;
ea0ebe96 1701 char *devname, namebuf[20];
353632d9
NB
1702
1703 /* This was a spare and may have some saved data on it.
1704 * Load the superblock, find and load the
1705 * backup_super_block.
1706 * If either fail, go on to next device.
1707 * If the backup contains no new info, just return
206c5eae 1708 * else restore data and update all superblocks
353632d9 1709 */
06b0d786
NB
1710 if (i == old_disks-1) {
1711 fd = open(backup_file, O_RDONLY);
e9e43ec3
N
1712 if (fd<0) {
1713 fprintf(stderr, Name ": backup file %s inaccessible: %s\n",
1714 backup_file, strerror(errno));
06b0d786 1715 continue;
e9e43ec3 1716 }
ea0ebe96 1717 devname = backup_file;
06b0d786
NB
1718 } else {
1719 fd = fdlist[i];
1720 if (fd < 0)
1721 continue;
3da92f27 1722 if (st->ss->load_super(st, fd, NULL))
06b0d786 1723 continue;
353632d9 1724
3da92f27
NB
1725 st->ss->getinfo_super(st, &dinfo);
1726 st->ss->free_super(st);
1727
06b0d786
NB
1728 if (lseek64(fd,
1729 (dinfo.data_offset + dinfo.component_size - 8) <<9,
ea0ebe96
N
1730 0) < 0) {
1731 fprintf(stderr, Name ": Cannot seek on device %d\n", i);
06b0d786 1732 continue; /* Cannot seek */
ea0ebe96
N
1733 }
1734 sprintf(namebuf, "device-%d", i);
1735 devname = namebuf;
06b0d786 1736 }
ea0ebe96
N
1737 if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb)) {
1738 if (verbose)
1739 fprintf(stderr, Name ": Cannot read from %s\n", devname);
353632d9 1740 continue; /* Cannot read */
ea0ebe96 1741 }
e9e43ec3 1742 if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0 &&
ea0ebe96
N
1743 memcmp(bsb.magic, "md_backup_data-2", 16) != 0) {
1744 if (verbose)
1745 fprintf(stderr, Name ": No backup metadata on %s\n", devname);
353632d9 1746 continue;
ea0ebe96
N
1747 }
1748 if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb))) {
1749 if (verbose)
1750 fprintf(stderr, Name ": Bad backup-metadata checksum on %s\n", devname);
353632d9 1751 continue; /* bad checksum */
ea0ebe96 1752 }
e9e43ec3 1753 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0 &&
ea0ebe96
N
1754 bsb.sb_csum2 != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum2)-((char*)&bsb))) {
1755 if (verbose)
1756 fprintf(stderr, Name ": Bad backup-metadata checksum2 on %s\n", devname);
22e30516 1757 continue; /* Bad second checksum */
ea0ebe96
N
1758 }
1759 if (memcmp(bsb.set_uuid,info->uuid, 16) != 0) {
1760 if (verbose)
1761 fprintf(stderr, Name ": Wrong uuid on backup-metadata on %s\n", devname);
353632d9 1762 continue; /* Wrong uuid */
ea0ebe96 1763 }
353632d9 1764
097075b6
N
1765 /* array utime and backup-mtime should be updated at much the same time, but it seems that
1766 * sometimes they aren't... So allow considerable flexability in matching, and allow
1767 * this test to be overridden by an environment variable.
1768 */
f21e18ca
N
1769 if (info->array.utime > (int)__le64_to_cpu(bsb.mtime) + 2*60*60 ||
1770 info->array.utime < (int)__le64_to_cpu(bsb.mtime) - 10*60) {
097075b6
N
1771 if (check_env("MDADM_GROW_ALLOW_OLD")) {
1772 fprintf(stderr, Name ": accepting backup with timestamp %lu "
1773 "for array with timestamp %lu\n",
1774 (unsigned long)__le64_to_cpu(bsb.mtime),
1775 (unsigned long)info->array.utime);
1776 } else {
1777 if (verbose)
1778 fprintf(stderr, Name ": too-old timestamp on "
1779 "backup-metadata on %s\n", devname);
1780 continue; /* time stamp is too bad */
1781 }
ea0ebe96 1782 }
353632d9 1783
e9e43ec3
N
1784 if (bsb.magic[15] == '1') {
1785 if (info->delta_disks >= 0) {
1786 /* reshape_progress is increasing */
1787 if (__le64_to_cpu(bsb.arraystart) + __le64_to_cpu(bsb.length) <
ea0ebe96
N
1788 info->reshape_progress) {
1789 nonew:
1790 if (verbose)
1791 fprintf(stderr, Name ": backup-metadata found on %s but is not needed\n", devname);
e9e43ec3 1792 continue; /* No new data here */
ea0ebe96 1793 }
e9e43ec3
N
1794 } else {
1795 /* reshape_progress is decreasing */
1796 if (__le64_to_cpu(bsb.arraystart) >=
1797 info->reshape_progress)
ea0ebe96 1798 goto nonew; /* No new data here */
e9e43ec3
N
1799 }
1800 } else {
1801 if (info->delta_disks >= 0) {
1802 /* reshape_progress is increasing */
1803 if (__le64_to_cpu(bsb.arraystart) + __le64_to_cpu(bsb.length) <
1804 info->reshape_progress &&
1805 __le64_to_cpu(bsb.arraystart2) + __le64_to_cpu(bsb.length2) <
1806 info->reshape_progress)
ea0ebe96 1807 goto nonew; /* No new data here */
e9e43ec3
N
1808 } else {
1809 /* reshape_progress is decreasing */
1810 if (__le64_to_cpu(bsb.arraystart) >=
1811 info->reshape_progress &&
1812 __le64_to_cpu(bsb.arraystart2) >=
1813 info->reshape_progress)
ea0ebe96 1814 goto nonew; /* No new data here */
e9e43ec3
N
1815 }
1816 }
ea0ebe96
N
1817 if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0) {
1818 second_fail:
1819 if (verbose)
1820 fprintf(stderr, Name ": Failed to verify secondary backup-metadata block on %s\n",
1821 devname);
353632d9 1822 continue; /* Cannot seek */
ea0ebe96 1823 }
2efedc7b 1824 /* There should be a duplicate backup superblock 4k before here */
06b0d786 1825 if (lseek64(fd, -4096, 1) < 0 ||
0155af90 1826 read(fd, &bsb2, sizeof(bsb2)) != sizeof(bsb2))
ea0ebe96 1827 goto second_fail; /* Cannot find leading superblock */
e9e43ec3
N
1828 if (bsb.magic[15] == '1')
1829 bsbsize = offsetof(struct mdp_backup_super, pad1);
1830 else
1831 bsbsize = offsetof(struct mdp_backup_super, pad);
ff94fb86 1832 if (memcmp(&bsb2, &bsb, bsbsize) != 0)
ea0ebe96 1833 goto second_fail; /* Cannot find leading superblock */
2efedc7b 1834
353632d9
NB
1835 /* Now need the data offsets for all devices. */
1836 offsets = malloc(sizeof(*offsets)*info->array.raid_disks);
1837 for(j=0; j<info->array.raid_disks; j++) {
1838 if (fdlist[j] < 0)
1839 continue;
3da92f27 1840 if (st->ss->load_super(st, fdlist[j], NULL))
353632d9
NB
1841 /* FIXME should be this be an error */
1842 continue;
3da92f27
NB
1843 st->ss->getinfo_super(st, &dinfo);
1844 st->ss->free_super(st);
14e5b4d7 1845 offsets[j] = dinfo.data_offset * 512;
353632d9
NB
1846 }
1847 printf(Name ": restoring critical section\n");
1848
1849 if (restore_stripes(fdlist, offsets,
1850 info->array.raid_disks,
1851 info->new_chunk,
1852 info->new_level,
1853 info->new_layout,
06b0d786 1854 fd, __le64_to_cpu(bsb.devstart)*512,
92dcdf7c 1855 __le64_to_cpu(bsb.arraystart)*512,
e9e43ec3
N
1856 __le64_to_cpu(bsb.length)*512)) {
1857 /* didn't succeed, so giveup */
ea0ebe96
N
1858 if (verbose)
1859 fprintf(stderr, Name ": Error restoring backup from %s\n",
1860 devname);
e9e43ec3
N
1861 return 1;
1862 }
1863
1864 if (bsb.magic[15] == '2' &&
1865 restore_stripes(fdlist, offsets,
1866 info->array.raid_disks,
1867 info->new_chunk,
1868 info->new_level,
1869 info->new_layout,
1870 fd, __le64_to_cpu(bsb.devstart)*512 +
1871 __le64_to_cpu(bsb.devstart2)*512,
92dcdf7c 1872 __le64_to_cpu(bsb.arraystart2)*512,
e9e43ec3 1873 __le64_to_cpu(bsb.length2)*512)) {
353632d9 1874 /* didn't succeed, so giveup */
ea0ebe96
N
1875 if (verbose)
1876 fprintf(stderr, Name ": Error restoring second backup from %s\n",
1877 devname);
2295250a 1878 return 1;
353632d9
NB
1879 }
1880
e9e43ec3 1881
353632d9
NB
1882 /* Ok, so the data is restored. Let's update those superblocks. */
1883
e9e43ec3
N
1884 if (info->delta_disks >= 0) {
1885 info->reshape_progress = __le64_to_cpu(bsb.arraystart) +
1886 __le64_to_cpu(bsb.length);
1887 if (bsb.magic[15] == '2') {
1888 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2) +
1889 __le64_to_cpu(bsb.length2);
1890 if (p2 > info->reshape_progress)
1891 info->reshape_progress = p2;
1892 }
1893 } else {
1894 info->reshape_progress = __le64_to_cpu(bsb.arraystart);
1895 if (bsb.magic[15] == '2') {
1896 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2);
1897 if (p2 < info->reshape_progress)
1898 info->reshape_progress = p2;
1899 }
1900 }
353632d9
NB
1901 for (j=0; j<info->array.raid_disks; j++) {
1902 if (fdlist[j] < 0) continue;
3da92f27 1903 if (st->ss->load_super(st, fdlist[j], NULL))
353632d9 1904 continue;
3da92f27 1905 st->ss->getinfo_super(st, &dinfo);
e9e43ec3 1906 dinfo.reshape_progress = info->reshape_progress;
3da92f27 1907 st->ss->update_super(st, &dinfo,
68c7d6d7
NB
1908 "_reshape_progress",
1909 NULL,0, 0, NULL);
3da92f27
NB
1910 st->ss->store_super(st, fdlist[j]);
1911 st->ss->free_super(st);
353632d9 1912 }
353632d9
NB
1913 return 0;
1914 }
6e9eac4f
NB
1915 /* Didn't find any backup data, try to see if any
1916 * was needed.
1917 */
82f2d6ab
N
1918 if (info->delta_disks < 0) {
1919 /* When shrinking, the critical section is at the end.
1920 * So see if we are before the critical section.
1921 */
1922 unsigned long long first_block;
1923 nstripe = ostripe = 0;
1924 first_block = 0;
1925 while (ostripe >= nstripe) {
1926 ostripe += info->array.chunk_size / 512;
1927 first_block = ostripe * odata;
1928 nstripe = first_block / ndata / (info->new_chunk/512) *
1929 (info->new_chunk/512);
1930 }
1931
1932 if (info->reshape_progress >= first_block)
1933 return 0;
6e9eac4f 1934 }
82f2d6ab
N
1935 if (info->delta_disks > 0) {
1936 /* See if we are beyond the critical section. */
1937 unsigned long long last_block;
1938 nstripe = ostripe = 0;
1939 last_block = 0;
1940 while (nstripe >= ostripe) {
1941 nstripe += info->new_chunk / 512;
1942 last_block = nstripe * ndata;
1943 ostripe = last_block / odata / (info->array.chunk_size/512) *
1944 (info->array.chunk_size/512);
1945 }
6e9eac4f 1946
82f2d6ab
N
1947 if (info->reshape_progress >= last_block)
1948 return 0;
1949 }
6e9eac4f 1950 /* needed to recover critical section! */
ea0ebe96
N
1951 if (verbose)
1952 fprintf(stderr, Name ": Failed to find backup of critical section\n");
2295250a 1953 return 1;
353632d9 1954}
e9e43ec3
N
1955
1956int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
1957 char *backup_file)
1958{
1959 /* Array is assembled and ready to be started, but
1960 * monitoring is probably required.
1961 * So:
1962 * - start read-only
1963 * - set upper bound for resync
1964 * - initialise the 'suspend' boundaries
1965 * - switch to read-write
1966 * - fork and continue monitoring
1967 */
1968 int err;
1969 int backup_list[1];
1970 unsigned long long backup_offsets[1];
1971 int odisks, ndisks, ochunk, nchunk,odata,ndata;
1972 unsigned long a,b,blocks,stripes;
1973 int backup_fd;
1974 int *fds;
1975 unsigned long long *offsets;
1976 int d;
1977 struct mdinfo *sra, *sd;
1978 int rv;
f21e18ca 1979 unsigned long cache;
e9e43ec3
N
1980 int done = 0;
1981
1982 err = sysfs_set_str(info, NULL, "array_state", "readonly");
1983 if (err)
1984 return err;
1985
1986 /* make sure reshape doesn't progress until we are ready */
1987 sysfs_set_str(info, NULL, "sync_max", "0");
1988 sysfs_set_str(info, NULL, "array_state", "active"); /* FIXME or clean */
5f6ca90a
N
1989
1990 sra = sysfs_read(-1, devname2devnum(info->sys_name),
1991 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|
1992 GET_CACHE);
1993 if (!sra)
1994 return 1;
1995
e9e43ec3
N
1996 /* ndisks is not growing, so raid_disks is old and +delta is new */
1997 odisks = info->array.raid_disks;
1998 ndisks = odisks + info->delta_disks;
1999 odata = odisks - 1;
2000 ndata = ndisks - 1;
2001 if (info->array.level == 6) {
2002 odata--;
2003 ndata--;
2004 }
2005 ochunk = info->array.chunk_size;
2006 nchunk = info->new_chunk;
2007
200871ad
N
2008 a = (ochunk/512) * odata;
2009 b = (nchunk/512) * ndata;
e9e43ec3
N
2010 /* Find GCD */
2011 while (a != b) {
2012 if (a < b)
2013 b -= a;
2014 if (b < a)
2015 a -= b;
2016 }
2017 /* LCM == product / GCD */
200871ad 2018 blocks = (ochunk/512) * (nchunk/512) * odata * ndata / a;
e9e43ec3
N
2019
2020 if (ndata == odata)
1b13faf7
N
2021 while (blocks * 32 < sra->component_size &&
2022 blocks < 16*1024*2)
2023 blocks *= 2;
e9e43ec3
N
2024 stripes = blocks / (info->array.chunk_size/512) / odata;
2025
1b13faf7
N
2026 /* check that the internal stripe cache is
2027 * large enough, or it won't work.
2028 */
2029 cache = (nchunk < ochunk) ? ochunk : nchunk;
2030 cache = cache * 4 / 4096;
2031 if (cache < blocks / 8 / odisks + 16)
2032 /* Make it big enough to hold 'blocks' */
2033 cache = blocks / 8 / odisks + 16;
2034 if (sra->cache_size < cache)
2035 sysfs_set_num(sra, NULL, "stripe_cache_size",
2036 cache+1);
e9e43ec3
N
2037
2038 memset(&bsb, 0, 512);
2039 memcpy(bsb.magic, "md_backup_data-1", 16);
2040 memcpy(&bsb.set_uuid, info->uuid, 16);
2041 bsb.mtime = __cpu_to_le64(time(0));
2042 bsb.devstart2 = blocks;
2043
2044 backup_fd = open(backup_file, O_RDWR|O_CREAT, S_IRUSR | S_IWUSR);
2045 backup_list[0] = backup_fd;
2046 backup_offsets[0] = 8 * 512;
2047 fds = malloc(odisks * sizeof(fds[0]));
2048 offsets = malloc(odisks * sizeof(offsets[0]));
2049 for (d=0; d<odisks; d++)
2050 fds[d] = -1;
2051
e9e43ec3
N
2052 for (sd = sra->devs; sd; sd = sd->next) {
2053 if (sd->disk.state & (1<<MD_DISK_FAULTY))
2054 continue;
2055 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
2056 char *dn = map_dev(sd->disk.major,
2057 sd->disk.minor, 1);
2058 fds[sd->disk.raid_disk]
2059 = dev_open(dn, O_RDONLY);
2060 offsets[sd->disk.raid_disk] = sd->data_offset*512;
2061 if (fds[sd->disk.raid_disk] < 0) {
2062 fprintf(stderr, Name ": %s: cannot open component %s\n",
2063 info->sys_name, dn?dn:"-unknown-");
2064 rv = 1;
2065 goto release;
2066 }
2067 free(dn);
2068 }
2069 }
2070
2071 switch(fork()) {
2072 case 0:
2073 close(mdfd);
2074 mlockall(MCL_FUTURE);
2075 if (info->delta_disks < 0)
2076 done = child_shrink(-1, info, stripes,
2077 fds, offsets,
2078 info->array.raid_disks,
2079 info->array.chunk_size,
2080 info->array.level, info->array.layout,
2081 odata,
2082 1, backup_list, backup_offsets);
2083 else if (info->delta_disks == 0) {
2084 /* The 'start' is a per-device stripe number.
2085 * reshape_progress is a per-array sector number.
2086 * So divide by ndata * chunk_size
2087 */
2088 unsigned long long start = info->reshape_progress / ndata;
2089 start /= (info->array.chunk_size/512);
2090 done = child_same_size(-1, info, stripes,
2091 fds, offsets,
2092 start,
2093 info->array.raid_disks,
2094 info->array.chunk_size,
2095 info->array.level, info->array.layout,
2096 odata,
2097 1, backup_list, backup_offsets);
2098 }
2099 if (backup_file && done)
2100 unlink(backup_file);
2101 /* FIXME should I intuit a level change */
2102 exit(0);
2103 case -1:
2104 fprintf(stderr, Name ": Cannot run child to continue monitoring reshape: %s\n",
2105 strerror(errno));
2106 return 1;
2107 default:
2108 break;
2109 }
2110release:
2111 return 0;
2112}
2113
2114