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