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