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