]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Grow.c
Grow: don't make 'blocks' too large during in-place reshape.
[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;
ea329559 291 for (d=0; d< st->max_devs; d++) {
f5e166fe
NB
292 mdu_disk_info_t disk;
293 char *dv;
294 disk.number = d;
295 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
296 continue;
297 if (disk.major == 0 &&
298 disk.minor == 0)
299 continue;
300 if ((disk.state & (1<<MD_DISK_SYNC))==0)
301 continue;
16c6fa80 302 dv = map_dev(disk.major, disk.minor, 1);
f5e166fe 303 if (dv) {
16c6fa80 304 int fd2 = dev_open(dv, O_RDWR);
f5e166fe
NB
305 if (fd2 < 0)
306 continue;
3da92f27 307 if (st->ss->load_super(st, fd2, NULL)==0) {
199171a2 308 if (st->ss->add_internal_bitmap(
3da92f27 309 st,
199171a2
NB
310 &chunk, delay, write_behind,
311 bitmapsize, 0, major)
312 )
3da92f27 313 st->ss->write_bitmap(st, fd2);
21e92547
NB
314 else {
315 fprintf(stderr, Name ": failed to create internal bitmap - chunksize problem.\n");
316 close(fd2);
317 return 1;
318 }
f5e166fe
NB
319 }
320 close(fd2);
321 }
322 }
323 array.state |= (1<<MD_SB_BITMAP_PRESENT);
324 if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
325 fprintf(stderr, Name ": failed to set internal bitmap.\n");
326 return 1;
327 }
fe80f49b
NB
328 } else {
329 int uuid[4];
330 int bitmap_fd;
331 int d;
332 int max_devs = st->max_devs;
fe80f49b
NB
333
334 /* try to load a superblock */
335 for (d=0; d<max_devs; d++) {
336 mdu_disk_info_t disk;
337 char *dv;
338 int fd2;
339 disk.number = d;
340 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
341 continue;
342 if ((disk.major==0 && disk.minor==0) ||
343 (disk.state & (1<<MD_DISK_REMOVED)))
344 continue;
16c6fa80 345 dv = map_dev(disk.major, disk.minor, 1);
fe80f49b 346 if (!dv) continue;
16c6fa80 347 fd2 = dev_open(dv, O_RDONLY);
fe80f49b 348 if (fd2 >= 0 &&
3da92f27 349 st->ss->load_super(st, fd2, NULL) == 0) {
fe80f49b 350 close(fd2);
3da92f27 351 st->ss->uuid_from_super(st, uuid);
fe80f49b
NB
352 break;
353 }
354 close(fd2);
355 }
356 if (d == max_devs) {
357 fprintf(stderr, Name ": cannot find UUID for array!\n");
358 return 1;
359 }
8fac0577 360 if (CreateBitmap(file, force, (char*)uuid, chunk,
f9c25f1d 361 delay, write_behind, bitmapsize, major)) {
fe80f49b
NB
362 return 1;
363 }
364 bitmap_fd = open(file, O_RDWR);
365 if (bitmap_fd < 0) {
8fac0577 366 fprintf(stderr, Name ": weird: %s cannot be opened\n",
fe80f49b
NB
367 file);
368 return 1;
369 }
370 if (ioctl(fd, SET_BITMAP_FILE, bitmap_fd) < 0) {
371 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
372 devname, strerror(errno));
373 return 1;
374 }
375 }
f5e166fe
NB
376
377 return 0;
378}
379
e86c9dd6
NB
380
381/*
382 * When reshaping an array we might need to backup some data.
383 * This is written to all spares with a 'super_block' describing it.
384 * The superblock goes 1K form the end of the used space on the
385 * device.
386 * It if written after the backup is complete.
387 * It has the following structure.
388 */
389
5fdf37e3 390static struct mdp_backup_super {
7236ee7a 391 char magic[16]; /* md_backup_data-1 or -2 */
e86c9dd6
NB
392 __u8 set_uuid[16];
393 __u64 mtime;
394 /* start/sizes in 512byte sectors */
7236ee7a 395 __u64 devstart; /* address on backup device/file of data */
e86c9dd6
NB
396 __u64 arraystart;
397 __u64 length;
398 __u32 sb_csum; /* csum of preceeding bytes. */
7236ee7a
N
399 __u32 pad1;
400 __u64 devstart2; /* offset in to data of second section */
401 __u64 arraystart2;
402 __u64 length2;
403 __u32 sb_csum2; /* csum of preceeding bytes. */
404 __u8 pad[512-68-32];
5fdf37e3 405} __attribute__((aligned(512))) bsb, bsb2;
e86c9dd6
NB
406
407int bsb_csum(char *buf, int len)
408{
409 int i;
410 int csum = 0;
411 for (i=0; i<len; i++)
412 csum = (csum<<3) + buf[0];
413 return __cpu_to_le32(csum);
414}
415
7236ee7a
N
416static int child_grow(int afd, struct mdinfo *sra, unsigned long blocks,
417 int *fds, unsigned long long *offsets,
418 int disks, int chunk, int level, int layout, int data,
419 int dests, int *destfd, unsigned long long *destoffsets);
420static int child_shrink(int afd, struct mdinfo *sra, unsigned long blocks,
421 int *fds, unsigned long long *offsets,
422 int disks, int chunk, int level, int layout, int data,
423 int dests, int *destfd, unsigned long long *destoffsets);
424static int child_same_size(int afd, struct mdinfo *sra, unsigned long blocks,
425 int *fds, unsigned long long *offsets,
e9e43ec3 426 unsigned long long start,
7236ee7a
N
427 int disks, int chunk, int level, int layout, int data,
428 int dests, int *destfd, unsigned long long *destoffsets);
429
430int freeze_array(struct mdinfo *sra)
431{
432 /* Try to freeze resync on this array.
433 * Return -1 if the array is busy,
434 * return 0 if this kernel doesn't support 'frozen'
435 * return 1 if it worked.
436 */
437 char buf[20];
438 if (sysfs_get_str(sra, NULL, "sync_action", buf, 20) <= 0)
439 return 0;
440 if (strcmp(buf, "idle\n") != 0 &&
441 strcmp(buf, "frozen\n") != 0)
442 return -1;
443 if (sysfs_set_str(sra, NULL, "sync_action", "frozen") < 0)
444 return 0;
445 return 1;
446}
447
448void unfreeze_array(struct mdinfo *sra, int frozen)
449{
450 /* If 'frozen' is 1, unfreeze the array */
451 if (frozen > 0)
452 sysfs_set_str(sra, NULL, "sync_action", "idle");
453}
454
455void wait_reshape(struct mdinfo *sra)
456{
457 int fd = sysfs_get_fd(sra, NULL, "sync_action");
458 char action[20];
459
460 do {
461 fd_set rfds;
462 FD_ZERO(&rfds);
463 FD_SET(fd, &rfds);
464 select(fd+1, NULL, NULL, &rfds, NULL);
465
466 if (sysfs_fd_get_str(fd, action, 20) < 0) {
467 close(fd);
468 return;
469 }
470 } while (strncmp(action, "reshape", 7) == 0);
471}
472
473
06b0d786 474int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
e86c9dd6 475 long long size,
19678e53 476 int level, char *layout_str, int chunksize, int raid_disks)
e86c9dd6
NB
477{
478 /* Make some changes in the shape of an array.
479 * The kernel must support the change.
7236ee7a
N
480 *
481 * There are three different changes. Each can trigger
482 * a resync or recovery so we freeze that until we have
483 * requested everything (if kernel supports freezing - 2.6.30).
484 * The steps are:
485 * - change size (i.e. component_size)
486 * - change level
487 * - change layout/chunksize/ndisks
488 *
489 * The last can require a reshape. It is different on different
490 * levels so we need to check the level before actioning it.
491 * Some times the level change needs to be requested after the
492 * reshape (e.g. raid6->raid5, raid5->raid0)
493 *
e86c9dd6 494 */
7236ee7a 495 struct mdu_array_info_s array, orig;
e86c9dd6 496 char *c;
7236ee7a 497 int rv = 0;
e86c9dd6
NB
498 struct supertype *st;
499
e86c9dd6
NB
500 int nchunk, ochunk;
501 int nlayout, olayout;
502 int ndisks, odisks;
503 int ndata, odata;
7236ee7a
N
504 int orig_level = UnSet;
505 char alt_layout[40];
e86c9dd6
NB
506 int *fdlist;
507 unsigned long long *offsets;
7236ee7a 508 int d, i;
e86c9dd6
NB
509 int nrdisks;
510 int err;
7236ee7a
N
511 int frozen;
512 unsigned long a,b, blocks, stripes;
513 int cache;
514 unsigned long long array_size;
515 int changed = 0;
516 int done;
e86c9dd6 517
7e0f6979 518 struct mdinfo *sra;
06c7f68e 519 struct mdinfo *sd;
e86c9dd6
NB
520
521 if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) {
522 fprintf(stderr, Name ": %s is not an active md array - aborting\n",
523 devname);
524 return 1;
525 }
7236ee7a
N
526 sra = sysfs_read(fd, 0, GET_LEVEL);
527 frozen = freeze_array(sra);
528 if (frozen < 0) {
529 fprintf(stderr, Name ": %s is performing resync/recovery and cannot"
530 " be reshaped\n", devname);
531 return 1;
532 }
533
534 /* ========= set size =============== */
535 if (size >= 0 && (size == 0 || size != array.size)) {
536 array.size = size;
537 if (array.size != size) {
538 /* got truncated to 32bit, write to
539 * component_size instead
540 */
541 if (sra)
542 rv = sysfs_set_num(sra, NULL,
543 "component_size", size);
544 else
545 rv = -1;
546 } else
547 rv = ioctl(fd, SET_ARRAY_INFO, &array);
548 if (rv != 0) {
549 fprintf(stderr, Name ": Cannot set device size for %s: %s\n",
550 devname, strerror(errno));
551 rv = 1;
552 goto release;
553 }
554 ioctl(fd, GET_ARRAY_INFO, &array);
555 if (!quiet)
556 fprintf(stderr, Name ": component size of %s has been set to %dK\n",
557 devname, array.size);
558 changed = 1;
559 }
560
561 /* ======= set level =========== */
562 if (level != UnSet && level != array.level) {
563 /* Trying to change the level.
564 * We might need to change layout first and schedule a
565 * level change for later.
566 * Level changes that can happen immediately are:
567 * 0->4,5,6 1->5 4->5,6 5->1,6
568 * Level changes that need a layout change first are:
569 * 6->5,4,0 : need a -6 layout, or parity-last
570 * 5->4,0 : need parity-last
571 */
572 if ((array.level == 6 || array.level == 5) &&
573 (level == 5 || level == 4 || level == 0)) {
574 /* Don't change level yet, but choose intermediate
575 * layout
576 */
577 if (level == 5) {
578 if (layout_str == NULL)
579 switch (array.layout) {
580 case ALGORITHM_LEFT_ASYMMETRIC:
581 case ALGORITHM_LEFT_ASYMMETRIC_6:
582 case ALGORITHM_ROTATING_N_RESTART:
583 layout_str = "left-asymmetric-6";
584 break;
585 case ALGORITHM_LEFT_SYMMETRIC:
586 case ALGORITHM_LEFT_SYMMETRIC_6:
587 case ALGORITHM_ROTATING_N_CONTINUE:
588 layout_str = "left-symmetric-6";
589 break;
590 case ALGORITHM_RIGHT_ASYMMETRIC:
591 case ALGORITHM_RIGHT_ASYMMETRIC_6:
592 case ALGORITHM_ROTATING_ZERO_RESTART:
593 layout_str = "right-asymmetric-6";
594 break;
595 case ALGORITHM_RIGHT_SYMMETRIC:
596 case ALGORITHM_RIGHT_SYMMETRIC_6:
597 layout_str = "right-symmetric-6";
598 break;
599 case ALGORITHM_PARITY_0:
600 case ALGORITHM_PARITY_0_6:
601 layout_str = "parity-first-6";
602 break;
603 case ALGORITHM_PARITY_N:
604 layout_str = "parity-last";
605 break;
606 default:
607 fprintf(stderr, Name ": %s: cannot"
608 "convert layout to RAID5 equivalent\n",
609 devname);
610 rv = 1;
611 goto release;
612 }
613 else {
614 int l = map_name(r5layout, layout_str);
615 if (l == UnSet) {
616 fprintf(stderr, Name ": %s: layout '%s' not recognised\n",
617 devname, layout_str);
618 rv = 1;
619 goto release;
620 }
621 if (l != ALGORITHM_PARITY_N) {
622 /* need the -6 version */
623 char *ls = map_num(r5layout, l);
624 strcat(strcpy(alt_layout, ls),
625 "-6");
626 layout_str = alt_layout;
627 }
628 }
629 if (raid_disks)
630 /* The find raid6->raid5 conversion
631 * will reduce the number of disks,
632 * so now we need to aim higher
633 */
634 raid_disks++;
635 } else
636 layout_str = "parity-last";
637 } else {
638 c = map_num(pers, level);
639 if (c == NULL)
640 return 1;/* not possible */
641 err = sysfs_set_str(sra, NULL, "level", c);
642 if (err) {
643 fprintf(stderr, Name ": %s: could not set level to %s\n",
644 devname, c);
645 rv = 1;
646 goto release;
647 }
648 orig = array;
649 orig_level = orig.level;
650 ioctl(fd, GET_ARRAY_INFO, &array);
651 if (layout_str == NULL &&
652 orig.level == 5 && level == 6 &&
653 array.layout != orig.layout)
654 layout_str = map_num(r5layout, orig.layout);
655 if (!quiet)
656 fprintf(stderr, Name " level of %s changed to %s\n",
657 devname, c);
658 changed = 1;
659 }
660 }
661
662 /* ========= set shape (chunk_size / layout / ndisks) ============== */
663 /* Check if layout change is a no-op */
664 if (layout_str) switch(array.level) {
665 case 5:
666 if (array.layout == map_name(r5layout, layout_str))
667 layout_str = NULL;
668 break;
669 case 6:
670 if (layout_str == NULL &&
671 ((chunksize && chunksize * 1024 != array.chunk_size) ||
672 (raid_disks && raid_disks != array.raid_disks)) &&
673 array.layout >= 16) {
674 fprintf(stderr, Name
675 ": %s has a non-standard layout. If you wish to preserve this\n"
676 " during the reshape, please specify --layout=preserve\n"
677 " If you want to change it, specify a layout or use --layout=normalise\n",
678 devname);
679 rv = 1;
680 goto release;
681 }
682 if (strcmp(layout_str, "normalise") == 0 ||
683 strcmp(layout_str, "normalize") == 0) {
684 char *hyphen;
685 strcpy(alt_layout, map_num(r6layout, array.layout));
686 hyphen = strrchr(alt_layout, '-');
687 if (hyphen && strcmp(hyphen, "-6") == 0) {
688 *hyphen = 0;
689 layout_str = alt_layout;
690 }
691 }
692
693 if (array.layout == map_name(r6layout, layout_str))
694 layout_str = NULL;
695 if (layout_str && strcmp(layout_str, "preserve") == 0)
696 layout_str = NULL;
697 break;
698 }
699 if (layout_str == NULL
700 && (chunksize == 0 || chunksize*1024 == array.chunk_size)
701 && (raid_disks == 0 || raid_disks == array.raid_disks)) {
702 rv = 0;
703 if (level != UnSet && level != array.level) {
704 /* Looks like this level change doesn't need
705 * a reshape after all.
706 */
707 c = map_num(pers, level);
708 if (c) {
709 rv = sysfs_set_str(sra, NULL, "level", c);
710 if (rv)
711 fprintf(stderr, Name ": %s: could not set level to %s\n",
712 devname, c);
713 }
714 } else if (!changed && !quiet)
715 fprintf(stderr, Name ": %s: no change requested\n",
716 devname);
717 goto release;
718 }
719
e86c9dd6
NB
720 c = map_num(pers, array.level);
721 if (c == NULL) c = "-unknown-";
722 switch(array.level) {
723 default: /* raid0, linear, multipath cannot be reconfigured */
724 fprintf(stderr, Name ": %s array %s cannot be reshaped.\n",
725 c, devname);
7236ee7a
N
726 rv = 1;
727 break;
e86c9dd6
NB
728
729 case LEVEL_FAULTY: /* only 'layout' change is permitted */
730
e86c9dd6
NB
731 if (chunksize || raid_disks) {
732 fprintf(stderr, Name ": %s: Cannot change chunksize or disks of a 'faulty' array\n",
733 devname);
7236ee7a
N
734 rv = 1;
735 break;
e86c9dd6 736 }
19678e53 737 if (layout_str == NULL)
7236ee7a 738 break; /* nothing to do.... */
e86c9dd6 739
19678e53
N
740 array.layout = parse_layout_faulty(layout_str);
741 if (array.layout < 0) {
7236ee7a 742 int rv;
19678e53
N
743 fprintf(stderr, Name ": %s: layout %s not understood for 'faulty' array\n",
744 devname, layout_str);
7236ee7a
N
745 rv = 1;
746 break;
19678e53 747 }
e86c9dd6
NB
748 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
749 fprintf(stderr, Name ": Cannot set layout for %s: %s\n",
750 devname, strerror(errno));
7236ee7a
N
751 rv = 1;
752 } else if (!quiet)
e86c9dd6 753 printf("layout for %s set to %d\n", devname, array.layout);
7236ee7a 754 break;
e86c9dd6 755
7236ee7a 756 case 1: /* only raid_disks can each be changed. */
e86c9dd6 757
19678e53 758 if (chunksize || layout_str != NULL) {
7236ee7a 759 fprintf(stderr, Name ": %s: Cannot change chunk size or layout for a RAID1 array.\n",
e86c9dd6 760 devname);
7236ee7a
N
761 rv = 1;
762 break;
e86c9dd6 763 }
9860f271 764 if (raid_disks > 0) {
e86c9dd6 765 array.raid_disks = raid_disks;
9860f271
NB
766 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
767 fprintf(stderr, Name ": Cannot set raid-devices for %s: %s\n",
768 devname, strerror(errno));
7236ee7a 769 rv = 1;
9860f271 770 }
e86c9dd6 771 }
7236ee7a 772 break;
e86c9dd6
NB
773
774 case 4:
775 case 5:
776 case 6:
1686dc25 777
7236ee7a
N
778 /*
779 * layout/chunksize/raid_disks can be changed
e86c9dd6 780 * though the kernel may not support it all.
e86c9dd6 781 */
7236ee7a
N
782 st = super_by_fd(fd);
783
784 /*
785 * There are three possibilities.
786 * 1/ The array will shrink.
787 * We need to ensure the reshape will pause before reaching
788 * the 'critical section'. We also need to fork and wait for
789 * that to happen. When it does we
790 * suspend/backup/complete/unfreeze
791 *
792 * 2/ The array will not change size.
793 * This requires that we keep a backup of a sliding window
794 * so that we can restore data after a crash. So we need
795 * to fork and monitor progress.
796 *
797 * 3/ The array will grow. This is relatively easy.
e86c9dd6
NB
798 * However the kernel's restripe routines will cheerfully
799 * overwrite some early data before it is safe. So we
800 * need to make a backup of the early parts of the array
801 * and be ready to restore it if rebuild aborts very early.
802 *
7236ee7a
N
803 * We backup data by writing it to one spare, or to a
804 * file which was given on command line.
e86c9dd6 805 *
7236ee7a 806 * [FOLLOWING IS OLD AND PARTLY WRONG]
e86c9dd6
NB
807 * So: we enumerate the devices in the array and
808 * make sure we can open all of them.
809 * Then we freeze the early part of the array and
810 * backup to the various spares.
811 * Then we request changes and start the reshape.
812 * Monitor progress until it has passed the danger zone.
813 * and finally invalidate the copied data and unfreeze the
814 * start of the array.
815 *
7236ee7a
N
816 * In each case, we first make sure that storage is available
817 * for the required backup.
818 * Then we:
819 * - request the shape change.
820 * - for to handle backup etc.
e86c9dd6 821 */
e86c9dd6
NB
822 nchunk = ochunk = array.chunk_size;
823 nlayout = olayout = array.layout;
824 ndisks = odisks = array.raid_disks;
825
7236ee7a
N
826 if (chunksize) {
827 nchunk = chunksize * 1024;
828 if (array.size % chunksize) {
829 fprintf(stderr, Name ": component size %dK is not"
830 " a multiple of chunksize %dK\n",
831 array.size, chunksize);
832 break;
833 }
834 }
19678e53 835 if (layout_str != NULL)
7236ee7a 836 switch(array.level) {
19678e53
N
837 case 4: /* ignore layout */
838 break;
839 case 5:
840 nlayout = map_name(r5layout, layout_str);
841 if (nlayout == UnSet) {
842 fprintf(stderr, Name ": layout %s not understood for raid5.\n",
843 layout_str);
844 return 1;
845 }
846 break;
847
848 case 6:
849 nlayout = map_name(r6layout, layout_str);
850 if (nlayout == UnSet) {
851 fprintf(stderr, Name ": layout %s not understood for raid6.\n",
852 layout_str);
853 return 1;
854 }
855 break;
856 }
e86c9dd6
NB
857 if (raid_disks) ndisks = raid_disks;
858
859 odata = odisks-1;
e86c9dd6 860 ndata = ndisks-1;
7236ee7a
N
861 if (array.level == 6) {
862 odata--; /* number of data disks */
863 ndata--;
e86c9dd6 864 }
7236ee7a
N
865
866 /* Check that we can hold all the data */
867 size = ndata * array.size;
868 get_dev_size(fd, NULL, &array_size);
869 if (size < (array_size/1024)) {
870 fprintf(stderr, Name ": this change will reduce the size of the array.\n"
871 " use --grow --array-size first to truncate array.\n"
872 " e.g. mdadm --grow %s --array-size %llu\n",
873 devname, size);
874 rv = 1;
875 break;
e86c9dd6 876 }
7236ee7a
N
877
878 /* So how much do we need to backup.
879 * We need an amount of data which is both a whole number of
880 * old stripes and a whole number of new stripes.
881 * So LCM for (chunksize*datadisks).
e86c9dd6 882 */
7236ee7a
N
883 a = ochunk/512 * odata;
884 b = nchunk/512 * ndata;
885 /* Find GCD */
886 while (a != b) {
887 if (a < b)
888 b -= a;
889 if (b < a)
890 a -= b;
e86c9dd6 891 }
7236ee7a
N
892 /* LCM == product / GCD */
893 blocks = ochunk/512 * nchunk/512 * odata * ndata / a;
894
eba71529
N
895 if (ndata == odata) {
896 /* Make 'blocks' bigger for better throughput, but
897 * not so big that we reject it below.
898 */
899 if (blocks * 32 < sra->component_size)
900 blocks *= 16;
901 } else
7236ee7a
N
902 fprintf(stderr, Name ": Need to backup %luK of critical "
903 "section..\n", blocks/2);
e86c9dd6 904
7236ee7a 905 sysfs_free(sra);
e86c9dd6 906 sra = sysfs_read(fd, 0,
758d3a8e
NB
907 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|
908 GET_CACHE);
e86c9dd6
NB
909 if (!sra) {
910 fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n",
911 devname);
7236ee7a
N
912 rv = 1;
913 break;
e86c9dd6
NB
914 }
915
7236ee7a 916 if (blocks >= sra->component_size/2) {
e86c9dd6
NB
917 fprintf(stderr, Name ": %s: Something wrong - reshape aborted\n",
918 devname);
7236ee7a
N
919 rv = 1;
920 break;
353632d9 921 }
7e0f6979 922 nrdisks = array.nr_disks + sra->array.spare_disks;
e86c9dd6
NB
923 /* Now we need to open all these devices so we can read/write.
924 */
06b0d786
NB
925 fdlist = malloc((1+nrdisks) * sizeof(int));
926 offsets = malloc((1+nrdisks) * sizeof(offsets[0]));
e86c9dd6
NB
927 if (!fdlist || !offsets) {
928 fprintf(stderr, Name ": malloc failed: grow aborted\n");
7236ee7a
N
929 rv = 1;
930 break;
e86c9dd6 931 }
06b0d786 932 for (d=0; d <= nrdisks; d++)
e86c9dd6
NB
933 fdlist[d] = -1;
934 d = array.raid_disks;
935 for (sd = sra->devs; sd; sd=sd->next) {
06c7f68e 936 if (sd->disk.state & (1<<MD_DISK_FAULTY))
e86c9dd6 937 continue;
06c7f68e
NB
938 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
939 char *dn = map_dev(sd->disk.major,
940 sd->disk.minor, 1);
941 fdlist[sd->disk.raid_disk]
942 = dev_open(dn, O_RDONLY);
7236ee7a 943 offsets[sd->disk.raid_disk] = sd->data_offset*512;
06c7f68e 944 if (fdlist[sd->disk.raid_disk] < 0) {
e86c9dd6 945 fprintf(stderr, Name ": %s: cannot open component %s\n",
e81cdd9f 946 devname, dn?dn:"-unknown-");
7236ee7a
N
947 rv = 1;
948 goto release;
e86c9dd6 949 }
7236ee7a 950 } else if (backup_file == NULL) {
e86c9dd6 951 /* spare */
06c7f68e
NB
952 char *dn = map_dev(sd->disk.major,
953 sd->disk.minor, 1);
16c6fa80 954 fdlist[d] = dev_open(dn, O_RDWR);
7236ee7a 955 offsets[d] = (sra->component_size - blocks - 8)*512;
e86c9dd6
NB
956 if (fdlist[d]<0) {
957 fprintf(stderr, Name ": %s: cannot open component %s\n",
e81cdd9f 958 devname, dn?dn:"-unknown");
7236ee7a
N
959 rv = 1;
960 goto release;
e86c9dd6
NB
961 }
962 d++;
963 }
964 }
7236ee7a
N
965 if (backup_file == NULL) {
966 if (ndata <= odata) {
967 fprintf(stderr, Name ": %s: Cannot grow - need backup-file\n",
968 devname);
969 rv = 1;
970 break;
971 } else if (sra->array.spare_disks == 0) {
972 fprintf(stderr, Name ": %s: Cannot grow - need a spare or "
973 "backup-file to backup critical section\n",
974 devname);
975 rv = 1;
976 break;
977 }
978 if (d == array.raid_disks) {
979 fprintf(stderr, Name ": %s: No spare device for backup\n",
980 devname);
981 rv = 1;
982 break;
e86c9dd6 983 }
7236ee7a
N
984 } else {
985 /* need to check backup file is large enough */
986 char buf[512];
987 fdlist[d] = open(backup_file, O_RDWR|O_CREAT|O_EXCL,
988 S_IRUSR | S_IWUSR);
989 offsets[d] = 8 * 512;
06b0d786
NB
990 if (fdlist[d] < 0) {
991 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
992 devname, backup_file, strerror(errno));
7236ee7a
N
993 rv = 1;
994 break;
995 }
996 memset(buf, 0, 512);
997 for (i=0; i < blocks + 1 ; i++) {
998 if (write(fdlist[d], buf, 512) != 512) {
999 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
1000 devname, backup_file, strerror(errno));
1001 rv = 1;
1002 break;
1003 }
1004 }
1005 if (fsync(fdlist[d]) != 0) {
1006 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
1007 devname, backup_file, strerror(errno));
1008 rv = 1;
1009 break;
06b0d786 1010 }
06b0d786 1011 d++;
06b0d786 1012 }
7236ee7a
N
1013
1014 /* lastly, check that the internal stripe cache is
1015 * large enough, or it won't work.
1016 */
1017
1018 cache = (nchunk < ochunk) ? ochunk : nchunk;
1019 cache = cache * 4 / 4096;
1020 if (sra->cache_size < cache)
1021 sysfs_set_num(sra, NULL, "stripe_cache_size",
1022 cache+1);
1023 /* Right, everything seems fine. Let's kick things off.
1024 * If only changing raid_disks, use ioctl, else use
1025 * sysfs.
1026 */
1027 if (ochunk == nchunk && olayout == nlayout) {
1028 array.raid_disks = ndisks;
1029 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
1030 rv = 1;
1031 fprintf(stderr, Name ": Cannot set device shape for %s: %s\n",
1032 devname, strerror(errno));
1033 if (ndisks < odisks &&
1034 get_linux_version() < 2006030)
1035 fprintf(stderr, Name ": linux 2.6.30 or later required\n");
1036
1037 break;
1038 }
1039 } else {
1040 /* set them all just in case some old 'new_*' value
1041 * persists from some earlier problem
1042 */
1043 if (sysfs_set_num(sra, NULL, "chunk_size", nchunk) < 0)
1044 rv = 1;
1045 if (sysfs_set_num(sra, NULL, "layout", nlayout) < 0)
1046 rv = 1;
1047 if (sysfs_set_num(sra, NULL, "raid_disks", ndisks) < 0)
1048 rv = 1;
1049 if (rv) {
1050 fprintf(stderr, Name ": Cannot set device shape for %s\n",
1051 devname);
1052 if (get_linux_version() < 2006030)
1053 fprintf(stderr, Name ": linux 2.6.30 or later required\n");
1054 break;
1055 }
e86c9dd6
NB
1056 }
1057
7236ee7a
N
1058 if (ndisks == 2 && odisks == 2) {
1059 /* No reshape is needed in this trivial case */
1060 rv = 0;
1061 break;
1062 }
1063
1064 /* set up the backup-super-block. This requires the
1065 * uuid from the array.
1066 */
e86c9dd6 1067 /* Find a superblock */
7236ee7a
N
1068 for (sd = sra->devs; sd; sd = sd->next) {
1069 char *dn;
1070 int devfd;
1071 int ok;
1072 if (sd->disk.state & (1<<MD_DISK_FAULTY))
1073 continue;
1074 dn = map_dev(sd->disk.major, sd->disk.minor, 1);
1075 devfd = dev_open(dn, O_RDONLY);
1076 if (devfd < 0)
1077 continue;
1078 ok = st->ss->load_super(st, devfd, NULL);
1079 close(devfd);
1080 if (ok >= 0)
1081 break;
1082 }
1083 if (!sd) {
e86c9dd6
NB
1084 fprintf(stderr, Name ": %s: Cannot find a superblock\n",
1085 devname);
7236ee7a
N
1086 rv = 1;
1087 break;
e86c9dd6
NB
1088 }
1089
7236ee7a 1090 memset(&bsb, 0, 512);
2efedc7b 1091 memcpy(bsb.magic, "md_backup_data-1", 16);
3da92f27 1092 st->ss->uuid_from_super(st, (int*)&bsb.set_uuid);
2efedc7b 1093 bsb.mtime = __cpu_to_le64(time(0));
7236ee7a
N
1094 bsb.devstart2 = blocks;
1095 stripes = blocks / (ochunk/512) / odata;
1096 /* Now we just need to kick off the reshape and watch, while
1097 * handling backups of the data...
1098 * This is all done by a forked background process.
2efedc7b 1099 */
7236ee7a
N
1100 switch(fork()) {
1101 case 0:
1102 close(fd);
1103 if (check_env("MDADM_GROW_VERIFY"))
1104 fd = open(devname, O_RDONLY | O_DIRECT);
1105 else
1106 fd = -1;
1107 mlockall(MCL_FUTURE);
1108
1109 if (odata < ndata)
1110 done = child_grow(fd, sra, stripes,
1111 fdlist, offsets,
1112 odisks, ochunk, array.level, olayout, odata,
1113 d - odisks, fdlist+odisks, offsets+odisks);
1114 else if (odata > ndata)
1115 done = child_shrink(fd, sra, stripes,
1116 fdlist, offsets,
1117 odisks, ochunk, array.level, olayout, odata,
1118 d - odisks, fdlist+odisks, offsets+odisks);
1119 else
1120 done = child_same_size(fd, sra, stripes,
1121 fdlist, offsets,
e9e43ec3 1122 0,
7236ee7a
N
1123 odisks, ochunk, array.level, olayout, odata,
1124 d - odisks, fdlist+odisks, offsets+odisks);
1125 if (backup_file && done)
1126 unlink(backup_file);
1127 if (level != UnSet && level != array.level) {
1128 /* We need to wait for the reshape to finish
1129 * (which will have happened unless odata < ndata)
1130 * and then set the level
758d3a8e 1131 */
7236ee7a
N
1132
1133 c = map_num(pers, level);
1134 if (c == NULL)
1135 exit(0);/* not possible */
1136
1137 if (odata < ndata)
1138 wait_reshape(sra);
1139 err = sysfs_set_str(sra, NULL, "level", c);
1140 if (err)
1141 fprintf(stderr, Name ": %s: could not set level to %s\n",
1142 devname, c);
758d3a8e 1143 }
7236ee7a
N
1144 exit(0);
1145 case -1:
1146 fprintf(stderr, Name ": Cannot run child to monitor reshape: %s\n",
1147 strerror(errno));
1148 rv = 1;
1149 break;
1150 default:
1151 /* The child will take care of unfreezing the array */
1152 frozen = 0;
1153 break;
e86c9dd6 1154 }
7236ee7a 1155 break;
e86c9dd6 1156
7236ee7a 1157 }
e86c9dd6 1158
7236ee7a
N
1159 release:
1160 if (rv && orig_level != UnSet && sra) {
1161 c = map_num(pers, orig_level);
1162 if (c && sysfs_set_str(sra, NULL, "level", c) == 0)
1163 fprintf(stderr, Name ": aborting level change\n");
1164 }
1165 if (sra)
1166 unfreeze_array(sra, frozen);
1167 return rv;
1168}
e86c9dd6 1169
7236ee7a
N
1170/*
1171 * We run a child process in the background which performs the following
1172 * steps:
1173 * - wait for resync to reach a certain point
1174 * - suspend io to the following section
1175 * - backup that section
1176 * - allow resync to proceed further
1177 * - resume io
1178 * - discard the backup.
1179 *
1180 * When are combined in slightly different ways in the three cases.
1181 * Grow:
1182 * - suspend/backup/allow/wait/resume/discard
1183 * Shrink:
1184 * - allow/wait/suspend/backup/allow/wait/resume/discard
1185 * same-size:
1186 * - wait/resume/discard/suspend/backup/allow
1187 *
1188 * suspend/backup/allow always come together
1189 * wait/resume/discard do too.
1190 * For the same-size case we have two backups to improve flow.
1191 *
1192 */
e86c9dd6 1193
7236ee7a
N
1194int grow_backup(struct mdinfo *sra,
1195 unsigned long long offset, /* per device */
1196 unsigned long stripes, /* per device */
1197 int *sources, unsigned long long *offsets,
1198 int disks, int chunk, int level, int layout,
1199 int dests, int *destfd, unsigned long long *destoffsets,
1200 int part,
1201 char *buf)
1202{
1203 /* Backup 'blocks' sectors at 'offset' on each device of the array,
1204 * to storage 'destfd' (offset 'destoffsets'), after first
1205 * suspending IO. Then allow resync to continue
1206 * over the suspended section.
1207 * Use part 'part' of the backup-super-block.
1208 */
1209 int odata = disks;
1210 int rv = 0;
1211 int i;
1212 //printf("offset %llu\n", offset);
1213 if (level >= 4)
1214 odata--;
1215 if (level == 6)
1216 odata--;
1217 sysfs_set_num(sra, NULL, "suspend_hi", (offset + stripes * chunk/512) * odata);
1218 if (part) {
1219 bsb.arraystart2 = __cpu_to_le64(offset * odata);
1220 bsb.length2 = __cpu_to_le64(stripes * chunk/512 * odata);
1221 } else {
1222 bsb.arraystart = __cpu_to_le64(offset * odata);
1223 bsb.length = __cpu_to_le64(stripes * chunk/512 * odata);
1224 }
1225 if (part)
1226 bsb.magic[15] = '2';
1227 for (i = 0; i < dests; i++)
1228 if (part)
1229 lseek64(destfd[i], destoffsets[i] + __le64_to_cpu(bsb.devstart2)*512, 0);
1230 else
1231 lseek64(destfd[i], destoffsets[i], 0);
1232
1233 rv = save_stripes(sources, offsets,
1234 disks, chunk, level, layout,
1235 dests, destfd,
1236 offset*512*odata, stripes * chunk * odata,
1237 buf);
1238
1239 if (rv)
1240 return rv;
1241 for (i = 0; i < dests; i++) {
1242 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
1243
1244 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
1245 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
1246 bsb.sb_csum2 = bsb_csum((char*)&bsb,
1247 ((char*)&bsb.sb_csum2)-((char*)&bsb));
1248
1249 lseek64(destfd[i], destoffsets[i] - 4096, 0);
1250 write(destfd[i], &bsb, 512);
1251 fsync(destfd[i]);
1252 }
2efedc7b 1253
7236ee7a
N
1254 return 0;
1255}
1256
1257/* in 2.6.30, the value reported by sync_completed can be
1258 * less that it should be by one stripe.
1259 * This only happens when reshape hits sync_max and pauses.
1260 * So allow wait_backup to either extent sync_max further
1261 * than strictly necessary, or return before the
1262 * sync has got quite as far as we would really like.
1263 * This is what 'blocks2' is for.
1264 * The various caller give appropriate values so that
1265 * every works.
1266 */
1267int wait_backup(struct mdinfo *sra,
1268 unsigned long long offset, /* per device */
1269 unsigned long long blocks, /* per device */
1270 unsigned long long blocks2, /* per device - hack */
1271 int dests, int *destfd, unsigned long long *destoffsets,
1272 int part)
1273{
1274 /* Wait for resync to pass the section that was backed up
1275 * then erase the backup and allow IO
1276 */
1277 int fd = sysfs_get_fd(sra, NULL, "sync_completed");
1278 unsigned long long completed;
1279 int i;
1280
1281 if (fd < 0)
1282 return -1;
1283 sysfs_set_num(sra, NULL, "sync_max", offset + blocks + blocks2);
1284 if (offset == 0)
1285 sysfs_set_str(sra, NULL, "sync_action", "reshape");
1286 do {
1287 char action[20];
1288 fd_set rfds;
1289 FD_ZERO(&rfds);
1290 FD_SET(fd, &rfds);
1291 select(fd+1, NULL, NULL, &rfds, NULL);
1292 if (sysfs_fd_get_ll(fd, &completed) < 0) {
1293 close(fd);
1294 return -1;
e86c9dd6 1295 }
7236ee7a
N
1296 if (sysfs_get_str(sra, NULL, "sync_action",
1297 action, 20) > 0 &&
1298 strncmp(action, "reshape", 7) != 0)
1299 break;
1300 } while (completed < offset + blocks);
1301 close(fd);
1302
1303 if (part) {
1304 bsb.arraystart2 = __cpu_to_le64(0);
1305 bsb.length2 = __cpu_to_le64(0);
1306 } else {
1307 bsb.arraystart = __cpu_to_le64(0);
1308 bsb.length = __cpu_to_le64(0);
1309 }
1310 for (i = 0; i < dests; i++) {
1311 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
1312 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
1313 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
1314 bsb.sb_csum2 = bsb_csum((char*)&bsb,
1315 ((char*)&bsb.sb_csum2)-((char*)&bsb));
1316 lseek64(destfd[i], destoffsets[i]-4096, 0);
1317 write(destfd[i], &bsb, 512);
1318 fsync(destfd[i]);
1319 }
1320 return 0;
1321}
e86c9dd6 1322
7236ee7a
N
1323static void fail(char *msg)
1324{
1325 write(2, msg, strlen(msg));
1326 write(2, "\n", 1);
1327 exit(1);
1328}
1329
1330static char *abuf, *bbuf;
1331static int abuflen;
1332static void validate(int afd, int bfd, unsigned long long offset)
1333{
1334 /* check that the data in the backup against the array.
1335 * This is only used for regression testing and should not
1336 * be used while the array is active
1337 */
7236ee7a
N
1338 if (afd < 0)
1339 return;
1340 lseek64(bfd, offset - 4096, 0);
1341 if (read(bfd, &bsb2, 512) != 512)
1342 fail("cannot read bsb");
1343 if (bsb2.sb_csum != bsb_csum((char*)&bsb2,
1344 ((char*)&bsb2.sb_csum)-((char*)&bsb2)))
1345 fail("first csum bad");
1346 if (memcmp(bsb2.magic, "md_backup_data", 14) != 0)
1347 fail("magic is bad");
1348 if (memcmp(bsb2.magic, "md_backup_data-2", 16) == 0 &&
1349 bsb2.sb_csum2 != bsb_csum((char*)&bsb2,
1350 ((char*)&bsb2.sb_csum2)-((char*)&bsb2)))
1351 fail("second csum bad");
1352
1353 if (__le64_to_cpu(bsb2.devstart)*512 != offset)
1354 fail("devstart is wrong");
1355
1356 if (bsb2.length) {
1357 unsigned long long len = __le64_to_cpu(bsb2.length)*512;
1358
1359 if (abuflen < len) {
1360 free(abuf);
1361 free(bbuf);
1362 abuflen = len;
1363 posix_memalign((void**)&abuf, 4096, abuflen);
1364 posix_memalign((void**)&bbuf, 4096, abuflen);
e86c9dd6 1365 }
48924014 1366
7236ee7a
N
1367 lseek64(bfd, offset, 0);
1368 if (read(bfd, bbuf, len) != len) {
1369 printf("len %llu\n", len);
1370 fail("read first backup failed");
1371 }
1372 lseek64(afd, __le64_to_cpu(bsb2.arraystart)*512, 0);
1373 if (read(afd, abuf, len) != len)
1374 fail("read first from array failed");
1375 if (memcmp(bbuf, abuf, len) != 0) {
1376 int i;
1377 printf("offset=%llu len=%llu\n",
1378 __le64_to_cpu(bsb2.arraystart)*512, len);
1379 for (i=0; i<len; i++)
1380 if (bbuf[i] != abuf[i]) {
1381 printf("first diff byte %d\n", i);
93ecfa01 1382 break;
7236ee7a
N
1383 }
1384 fail("data1 compare failed");
e86c9dd6 1385 }
7236ee7a
N
1386 }
1387 if (bsb2.length2) {
1388 unsigned long long len = __le64_to_cpu(bsb2.length2)*512;
1389
1390 if (abuflen < len) {
1391 free(abuf);
1392 free(bbuf);
1393 abuflen = len;
1394 abuf = malloc(abuflen);
1395 bbuf = malloc(abuflen);
e86c9dd6
NB
1396 }
1397
7236ee7a
N
1398 lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0);
1399 if (read(bfd, bbuf, len) != len)
1400 fail("read second backup failed");
1401 lseek64(afd, __le64_to_cpu(bsb2.arraystart2)*512, 0);
1402 if (read(afd, abuf, len) != len)
1403 fail("read second from array failed");
1404 if (memcmp(bbuf, abuf, len) != 0)
1405 fail("data2 compare failed");
e86c9dd6 1406 }
7236ee7a 1407}
e86c9dd6 1408
7236ee7a
N
1409static int child_grow(int afd, struct mdinfo *sra, unsigned long stripes,
1410 int *fds, unsigned long long *offsets,
1411 int disks, int chunk, int level, int layout, int data,
1412 int dests, int *destfd, unsigned long long *destoffsets)
1413{
1414 char *buf;
e86c9dd6 1415
7236ee7a
N
1416 posix_memalign((void**)&buf, 4096, disks * chunk);
1417 sysfs_set_num(sra, NULL, "suspend_hi", 0);
1418 sysfs_set_num(sra, NULL, "suspend_lo", 0);
1419 grow_backup(sra, 0, stripes,
1420 fds, offsets, disks, chunk, level, layout,
1421 dests, destfd, destoffsets,
1422 0, buf);
1423 validate(afd, destfd[0], destoffsets[0]);
725cac4c
N
1424 wait_backup(sra, 0, stripes * chunk / 512, stripes * chunk / 512,
1425 dests, destfd, destoffsets,
1426 0);
7236ee7a
N
1427 sysfs_set_num(sra, NULL, "suspend_lo", (stripes * chunk/512) * data);
1428 free(buf);
1429 /* FIXME this should probably be numeric */
1430 sysfs_set_str(sra, NULL, "sync_max", "max");
e86c9dd6 1431 return 1;
7236ee7a 1432}
e86c9dd6 1433
7236ee7a
N
1434static int child_shrink(int afd, struct mdinfo *sra, unsigned long stripes,
1435 int *fds, unsigned long long *offsets,
1436 int disks, int chunk, int level, int layout, int data,
1437 int dests, int *destfd, unsigned long long *destoffsets)
1438{
1439 char *buf;
1440 unsigned long long start;
1441 int rv;
1442
1443 posix_memalign((void**)&buf, 4096, disks * chunk);
1444 start = sra->component_size - stripes * chunk/512;
1445 sysfs_set_num(sra, NULL, "sync_max", start);
1446 sysfs_set_str(sra, NULL, "sync_action", "reshape");
1447 sysfs_set_num(sra, NULL, "suspend_lo", 0);
1448 sysfs_set_num(sra, NULL, "suspend_hi", 0);
1449 rv = wait_backup(sra, 0, start - stripes * chunk/512, stripes * chunk/512,
1450 dests, destfd, destoffsets, 0);
1451 if (rv < 0)
1452 return 0;
1453 grow_backup(sra, 0, stripes,
1454 fds, offsets,
1455 disks, chunk, level, layout,
1456 dests, destfd, destoffsets,
1457 0, buf);
1458 validate(afd, destfd[0], destoffsets[0]);
725cac4c
N
1459 wait_backup(sra, start, stripes*chunk/512, 0,
1460 dests, destfd, destoffsets, 0);
7236ee7a
N
1461 sysfs_set_num(sra, NULL, "suspend_lo", (stripes * chunk/512) * data);
1462 free(buf);
1463 /* FIXME this should probably be numeric */
1464 sysfs_set_str(sra, NULL, "sync_max", "max");
1465 return 1;
1466}
1467
1468static int child_same_size(int afd, struct mdinfo *sra, unsigned long stripes,
1469 int *fds, unsigned long long *offsets,
e9e43ec3 1470 unsigned long long start,
7236ee7a
N
1471 int disks, int chunk, int level, int layout, int data,
1472 int dests, int *destfd, unsigned long long *destoffsets)
1473{
e9e43ec3 1474 unsigned long long size;
7236ee7a
N
1475 unsigned long tailstripes = stripes;
1476 int part;
1477 char *buf;
1478 unsigned long long speed;
1479
1480
1481 posix_memalign((void**)&buf, 4096, disks * chunk);
1482
1483 sysfs_set_num(sra, NULL, "suspend_lo", 0);
1484 sysfs_set_num(sra, NULL, "suspend_hi", 0);
1485
1486 sysfs_get_ll(sra, NULL, "sync_speed_min", &speed);
1487 sysfs_set_num(sra, NULL, "sync_speed_min", 200000);
1488
e9e43ec3 1489 grow_backup(sra, start, stripes,
7236ee7a
N
1490 fds, offsets,
1491 disks, chunk, level, layout,
1492 dests, destfd, destoffsets,
1493 0, buf);
e9e43ec3 1494 grow_backup(sra, (start + stripes) * chunk/512, stripes,
7236ee7a
N
1495 fds, offsets,
1496 disks, chunk, level, layout,
1497 dests, destfd, destoffsets,
1498 1, buf);
1499 validate(afd, destfd[0], destoffsets[0]);
1500 part = 0;
e9e43ec3 1501 start += stripes * 2; /* where to read next */
7236ee7a
N
1502 size = sra->component_size / (chunk/512);
1503 while (start < size) {
1504 if (wait_backup(sra, (start-stripes*2)*chunk/512,
1505 stripes*chunk/512, 0,
1506 dests, destfd, destoffsets,
1507 part) < 0)
1508 return 0;
1509 sysfs_set_num(sra, NULL, "suspend_lo", start*chunk/512 * data);
1510 if (start + stripes > size)
1511 tailstripes = (size - start);
1512
1513 grow_backup(sra, start*chunk/512, tailstripes,
1514 fds, offsets,
1515 disks, chunk, level, layout,
1516 dests, destfd, destoffsets,
1517 part, buf);
1518 start += stripes;
1519 part = 1 - part;
1520 validate(afd, destfd[0], destoffsets[0]);
1521 }
1522 if (wait_backup(sra, (start-stripes*2) * chunk/512, stripes * chunk/512, 0,
1523 dests, destfd, destoffsets,
1524 part) < 0)
1525 return 0;
1526 sysfs_set_num(sra, NULL, "suspend_lo", ((start-stripes)*chunk/512) * data);
725cac4c
N
1527 wait_backup(sra, (start-stripes) * chunk/512, tailstripes * chunk/512, 0,
1528 dests, destfd, destoffsets,
1529 1-part);
7236ee7a
N
1530 sysfs_set_num(sra, NULL, "suspend_lo", (size*chunk/512) * data);
1531 sysfs_set_num(sra, NULL, "sync_speed_min", speed);
1532 free(buf);
1533 return 1;
e86c9dd6 1534}
353632d9
NB
1535
1536/*
1537 * If any spare contains md_back_data-1 which is recent wrt mtime,
1538 * write that data into the array and update the super blocks with
1539 * the new reshape_progress
1540 */
06b0d786 1541int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt, char *backup_file)
353632d9
NB
1542{
1543 int i, j;
1544 int old_disks;
353632d9 1545 unsigned long long *offsets;
6e9eac4f
NB
1546 unsigned long long nstripe, ostripe, last_block;
1547 int ndata, odata;
353632d9 1548
e9e43ec3
N
1549 if (info->new_level != info->array.level)
1550 return 1; /* Cannot handle level changes (they are instantaneous) */
1551
1552 odata = info->array.raid_disks - info->delta_disks - 1;
1553 if (info->array.level == 6) odata--; /* number of data disks */
1554 ndata = info->array.raid_disks - 1;
1555 if (info->new_level == 6) ndata--;
353632d9
NB
1556
1557 old_disks = info->array.raid_disks - info->delta_disks;
1558
e9e43ec3
N
1559 if (info->delta_disks <= 0)
1560 /* Didn't grow, so the backup file must have
1561 * been used
1562 */
1563 old_disks = cnt;
06b0d786 1564 for (i=old_disks-(backup_file?1:0); i<cnt; i++) {
353632d9 1565 struct mdinfo dinfo;
2efedc7b 1566 char buf[4096];
06b0d786 1567 int fd;
e9e43ec3 1568 int bsbsize;
353632d9
NB
1569
1570 /* This was a spare and may have some saved data on it.
1571 * Load the superblock, find and load the
1572 * backup_super_block.
1573 * If either fail, go on to next device.
1574 * If the backup contains no new info, just return
206c5eae 1575 * else restore data and update all superblocks
353632d9 1576 */
06b0d786
NB
1577 if (i == old_disks-1) {
1578 fd = open(backup_file, O_RDONLY);
e9e43ec3
N
1579 if (fd<0) {
1580 fprintf(stderr, Name ": backup file %s inaccessible: %s\n",
1581 backup_file, strerror(errno));
06b0d786 1582 continue;
e9e43ec3 1583 }
06b0d786
NB
1584 } else {
1585 fd = fdlist[i];
1586 if (fd < 0)
1587 continue;
3da92f27 1588 if (st->ss->load_super(st, fd, NULL))
06b0d786 1589 continue;
353632d9 1590
3da92f27
NB
1591 st->ss->getinfo_super(st, &dinfo);
1592 st->ss->free_super(st);
1593
06b0d786
NB
1594 if (lseek64(fd,
1595 (dinfo.data_offset + dinfo.component_size - 8) <<9,
1596 0) < 0)
1597 continue; /* Cannot seek */
1598 }
1599 if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb))
353632d9 1600 continue; /* Cannot read */
e9e43ec3
N
1601 if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0 &&
1602 memcmp(bsb.magic, "md_backup_data-2", 16) != 0)
353632d9
NB
1603 continue;
1604 if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb)))
1605 continue; /* bad checksum */
e9e43ec3
N
1606 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0 &&
1607 bsb.sb_csum2 != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum2)-((char*)&bsb)))
353632d9
NB
1608 if (memcmp(bsb.set_uuid,info->uuid, 16) != 0)
1609 continue; /* Wrong uuid */
1610
1611 if (info->array.utime > __le64_to_cpu(bsb.mtime) + 3600 ||
1612 info->array.utime < __le64_to_cpu(bsb.mtime))
1613 continue; /* time stamp is too bad */
1614
e9e43ec3
N
1615 if (bsb.magic[15] == '1') {
1616 if (info->delta_disks >= 0) {
1617 /* reshape_progress is increasing */
1618 if (__le64_to_cpu(bsb.arraystart) + __le64_to_cpu(bsb.length) <
1619 info->reshape_progress)
1620 continue; /* No new data here */
1621 } else {
1622 /* reshape_progress is decreasing */
1623 if (__le64_to_cpu(bsb.arraystart) >=
1624 info->reshape_progress)
1625 continue; /* No new data here */
1626 }
1627 } else {
1628 if (info->delta_disks >= 0) {
1629 /* reshape_progress is increasing */
1630 if (__le64_to_cpu(bsb.arraystart) + __le64_to_cpu(bsb.length) <
1631 info->reshape_progress &&
1632 __le64_to_cpu(bsb.arraystart2) + __le64_to_cpu(bsb.length2) <
1633 info->reshape_progress)
1634 continue; /* No new data here */
1635 } else {
1636 /* reshape_progress is decreasing */
1637 if (__le64_to_cpu(bsb.arraystart) >=
1638 info->reshape_progress &&
1639 __le64_to_cpu(bsb.arraystart2) >=
1640 info->reshape_progress)
1641 continue; /* No new data here */
1642 }
1643 }
06b0d786 1644 if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0)
353632d9 1645 continue; /* Cannot seek */
2efedc7b 1646 /* There should be a duplicate backup superblock 4k before here */
06b0d786 1647 if (lseek64(fd, -4096, 1) < 0 ||
e9e43ec3
N
1648 read(fd, buf, 4096) != 4096)
1649 continue; /* Cannot find leading superblock */
1650 if (bsb.magic[15] == '1')
1651 bsbsize = offsetof(struct mdp_backup_super, pad1);
1652 else
1653 bsbsize = offsetof(struct mdp_backup_super, pad);
1654 if (memcmp(buf, &bsb, bsbsize) != 0)
2efedc7b
NB
1655 continue; /* Cannot find leading superblock */
1656
353632d9
NB
1657 /* Now need the data offsets for all devices. */
1658 offsets = malloc(sizeof(*offsets)*info->array.raid_disks);
1659 for(j=0; j<info->array.raid_disks; j++) {
1660 if (fdlist[j] < 0)
1661 continue;
3da92f27 1662 if (st->ss->load_super(st, fdlist[j], NULL))
353632d9
NB
1663 /* FIXME should be this be an error */
1664 continue;
3da92f27
NB
1665 st->ss->getinfo_super(st, &dinfo);
1666 st->ss->free_super(st);
353632d9
NB
1667 offsets[j] = dinfo.data_offset;
1668 }
1669 printf(Name ": restoring critical section\n");
1670
1671 if (restore_stripes(fdlist, offsets,
1672 info->array.raid_disks,
1673 info->new_chunk,
1674 info->new_level,
1675 info->new_layout,
06b0d786 1676 fd, __le64_to_cpu(bsb.devstart)*512,
e9e43ec3
N
1677 __le64_to_cpu(bsb.arraystart),
1678 __le64_to_cpu(bsb.length)*512)) {
1679 /* didn't succeed, so giveup */
1680 return 1;
1681 }
1682
1683 if (bsb.magic[15] == '2' &&
1684 restore_stripes(fdlist, offsets,
1685 info->array.raid_disks,
1686 info->new_chunk,
1687 info->new_level,
1688 info->new_layout,
1689 fd, __le64_to_cpu(bsb.devstart)*512 +
1690 __le64_to_cpu(bsb.devstart2)*512,
1691 __le64_to_cpu(bsb.arraystart2),
1692 __le64_to_cpu(bsb.length2)*512)) {
353632d9 1693 /* didn't succeed, so giveup */
2295250a 1694 return 1;
353632d9
NB
1695 }
1696
e9e43ec3 1697
353632d9
NB
1698 /* Ok, so the data is restored. Let's update those superblocks. */
1699
e9e43ec3
N
1700 if (info->delta_disks >= 0) {
1701 info->reshape_progress = __le64_to_cpu(bsb.arraystart) +
1702 __le64_to_cpu(bsb.length);
1703 if (bsb.magic[15] == '2') {
1704 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2) +
1705 __le64_to_cpu(bsb.length2);
1706 if (p2 > info->reshape_progress)
1707 info->reshape_progress = p2;
1708 }
1709 } else {
1710 info->reshape_progress = __le64_to_cpu(bsb.arraystart);
1711 if (bsb.magic[15] == '2') {
1712 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2);
1713 if (p2 < info->reshape_progress)
1714 info->reshape_progress = p2;
1715 }
1716 }
353632d9
NB
1717 for (j=0; j<info->array.raid_disks; j++) {
1718 if (fdlist[j] < 0) continue;
3da92f27 1719 if (st->ss->load_super(st, fdlist[j], NULL))
353632d9 1720 continue;
3da92f27 1721 st->ss->getinfo_super(st, &dinfo);
e9e43ec3 1722 dinfo.reshape_progress = info->reshape_progress;
3da92f27 1723 st->ss->update_super(st, &dinfo,
68c7d6d7
NB
1724 "_reshape_progress",
1725 NULL,0, 0, NULL);
3da92f27
NB
1726 st->ss->store_super(st, fdlist[j]);
1727 st->ss->free_super(st);
353632d9 1728 }
353632d9
NB
1729 return 0;
1730 }
6e9eac4f
NB
1731 /* Didn't find any backup data, try to see if any
1732 * was needed.
1733 */
e9e43ec3
N
1734 if (info->delta_disks == 0)
1735 /* Alway need backup data when size doesn't change */
1736 return 1;
6e9eac4f 1737 nstripe = ostripe = 0;
6e9eac4f
NB
1738 last_block = 0;
1739 while (nstripe >= ostripe) {
1740 nstripe += info->new_chunk / 512;
1741 last_block = nstripe * ndata;
1742 ostripe = last_block / odata / (info->array.chunk_size/512) *
1743 (info->array.chunk_size/512);
1744 }
1745
1746 if (info->reshape_progress >= last_block)
1747 return 0;
1748 /* needed to recover critical section! */
2295250a 1749 return 1;
353632d9 1750}
e9e43ec3
N
1751
1752int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
1753 char *backup_file)
1754{
1755 /* Array is assembled and ready to be started, but
1756 * monitoring is probably required.
1757 * So:
1758 * - start read-only
1759 * - set upper bound for resync
1760 * - initialise the 'suspend' boundaries
1761 * - switch to read-write
1762 * - fork and continue monitoring
1763 */
1764 int err;
1765 int backup_list[1];
1766 unsigned long long backup_offsets[1];
1767 int odisks, ndisks, ochunk, nchunk,odata,ndata;
1768 unsigned long a,b,blocks,stripes;
1769 int backup_fd;
1770 int *fds;
1771 unsigned long long *offsets;
1772 int d;
1773 struct mdinfo *sra, *sd;
1774 int rv;
1775 int done = 0;
1776
1777 err = sysfs_set_str(info, NULL, "array_state", "readonly");
1778 if (err)
1779 return err;
1780
1781 /* make sure reshape doesn't progress until we are ready */
1782 sysfs_set_str(info, NULL, "sync_max", "0");
1783 sysfs_set_str(info, NULL, "array_state", "active"); /* FIXME or clean */
1784
1785 /* ndisks is not growing, so raid_disks is old and +delta is new */
1786 odisks = info->array.raid_disks;
1787 ndisks = odisks + info->delta_disks;
1788 odata = odisks - 1;
1789 ndata = ndisks - 1;
1790 if (info->array.level == 6) {
1791 odata--;
1792 ndata--;
1793 }
1794 ochunk = info->array.chunk_size;
1795 nchunk = info->new_chunk;
1796
1797
1798 a = ochunk/512 * odata;
1799 b = nchunk/512 * ndata;
1800 /* Find GCD */
1801 while (a != b) {
1802 if (a < b)
1803 b -= a;
1804 if (b < a)
1805 a -= b;
1806 }
1807 /* LCM == product / GCD */
1808 blocks = ochunk/512 * nchunk/512 * odata * ndata / a;
1809
1810 if (ndata == odata)
1811 blocks *= 16;
1812 stripes = blocks / (info->array.chunk_size/512) / odata;
1813
1814
1815 memset(&bsb, 0, 512);
1816 memcpy(bsb.magic, "md_backup_data-1", 16);
1817 memcpy(&bsb.set_uuid, info->uuid, 16);
1818 bsb.mtime = __cpu_to_le64(time(0));
1819 bsb.devstart2 = blocks;
1820
1821 backup_fd = open(backup_file, O_RDWR|O_CREAT, S_IRUSR | S_IWUSR);
1822 backup_list[0] = backup_fd;
1823 backup_offsets[0] = 8 * 512;
1824 fds = malloc(odisks * sizeof(fds[0]));
1825 offsets = malloc(odisks * sizeof(offsets[0]));
1826 for (d=0; d<odisks; d++)
1827 fds[d] = -1;
1828
1829 sra = sysfs_read(-1, devname2devnum(info->sys_name),
1830 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|
1831 GET_CACHE);
1832
1833 for (sd = sra->devs; sd; sd = sd->next) {
1834 if (sd->disk.state & (1<<MD_DISK_FAULTY))
1835 continue;
1836 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
1837 char *dn = map_dev(sd->disk.major,
1838 sd->disk.minor, 1);
1839 fds[sd->disk.raid_disk]
1840 = dev_open(dn, O_RDONLY);
1841 offsets[sd->disk.raid_disk] = sd->data_offset*512;
1842 if (fds[sd->disk.raid_disk] < 0) {
1843 fprintf(stderr, Name ": %s: cannot open component %s\n",
1844 info->sys_name, dn?dn:"-unknown-");
1845 rv = 1;
1846 goto release;
1847 }
1848 free(dn);
1849 }
1850 }
1851
1852 switch(fork()) {
1853 case 0:
1854 close(mdfd);
1855 mlockall(MCL_FUTURE);
1856 if (info->delta_disks < 0)
1857 done = child_shrink(-1, info, stripes,
1858 fds, offsets,
1859 info->array.raid_disks,
1860 info->array.chunk_size,
1861 info->array.level, info->array.layout,
1862 odata,
1863 1, backup_list, backup_offsets);
1864 else if (info->delta_disks == 0) {
1865 /* The 'start' is a per-device stripe number.
1866 * reshape_progress is a per-array sector number.
1867 * So divide by ndata * chunk_size
1868 */
1869 unsigned long long start = info->reshape_progress / ndata;
1870 start /= (info->array.chunk_size/512);
1871 done = child_same_size(-1, info, stripes,
1872 fds, offsets,
1873 start,
1874 info->array.raid_disks,
1875 info->array.chunk_size,
1876 info->array.level, info->array.layout,
1877 odata,
1878 1, backup_list, backup_offsets);
1879 }
1880 if (backup_file && done)
1881 unlink(backup_file);
1882 /* FIXME should I intuit a level change */
1883 exit(0);
1884 case -1:
1885 fprintf(stderr, Name ": Cannot run child to continue monitoring reshape: %s\n",
1886 strerror(errno));
1887 return 1;
1888 default:
1889 break;
1890 }
1891release:
1892 return 0;
1893}
1894
1895