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