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