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