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