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