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