]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Grow.c
8322d2d74f96baedab477c121761c46a72f9b26f
[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 char *subarray = NULL;
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 if (info.array.level != -1) {
62 fprintf(stderr, Name ": can only add devices to linear arrays\n");
63 return 1;
64 }
65
66 st = super_by_fd(fd, &subarray);
67 if (!st) {
68 fprintf(stderr, Name ": cannot handle arrays with superblock version %d\n", info.array.major_version);
69 return 1;
70 }
71
72 if (subarray) {
73 fprintf(stderr, Name ": Cannot grow linear sub-arrays yet\n");
74 free(subarray);
75 free(st);
76 }
77
78 nfd = open(newdev, O_RDWR|O_EXCL|O_DIRECT);
79 if (nfd < 0) {
80 fprintf(stderr, Name ": cannot open %s\n", newdev);
81 free(st);
82 return 1;
83 }
84 fstat(nfd, &stb);
85 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
86 fprintf(stderr, Name ": %s is not a block device!\n", newdev);
87 close(nfd);
88 free(st);
89 return 1;
90 }
91 /* now check out all the devices and make sure we can read the superblock */
92 for (d=0 ; d < info.array.raid_disks ; d++) {
93 mdu_disk_info_t disk;
94 char *dv;
95
96 st->ss->free_super(st);
97
98 disk.number = d;
99 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
100 fprintf(stderr, Name ": cannot get device detail for device %d\n",
101 d);
102 close(nfd);
103 free(st);
104 return 1;
105 }
106 dv = map_dev(disk.major, disk.minor, 1);
107 if (!dv) {
108 fprintf(stderr, Name ": cannot find device file for device %d\n",
109 d);
110 close(nfd);
111 free(st);
112 return 1;
113 }
114 fd2 = dev_open(dv, O_RDWR);
115 if (!fd2) {
116 fprintf(stderr, Name ": cannot open device file %s\n", dv);
117 close(nfd);
118 free(st);
119 return 1;
120 }
121
122 if (st->ss->load_super(st, fd2, NULL)) {
123 fprintf(stderr, Name ": cannot find super block on %s\n", dv);
124 close(nfd);
125 close(fd2);
126 free(st);
127 return 1;
128 }
129 close(fd2);
130 }
131 /* Ok, looks good. Lets update the superblock and write it out to
132 * newdev.
133 */
134
135 info.disk.number = d;
136 info.disk.major = major(stb.st_rdev);
137 info.disk.minor = minor(stb.st_rdev);
138 info.disk.raid_disk = d;
139 info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE);
140 st->ss->update_super(st, &info, "linear-grow-new", newdev,
141 0, 0, NULL);
142
143 if (st->ss->store_super(st, nfd)) {
144 fprintf(stderr, Name ": Cannot store new superblock on %s\n",
145 newdev);
146 close(nfd);
147 return 1;
148 }
149 close(nfd);
150
151 if (ioctl(fd, ADD_NEW_DISK, &info.disk) != 0) {
152 fprintf(stderr, Name ": Cannot add new disk to this array\n");
153 return 1;
154 }
155 /* Well, that seems to have worked.
156 * Now go through and update all superblocks
157 */
158
159 if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
160 fprintf(stderr, Name ": cannot get array info for %s\n", devname);
161 return 1;
162 }
163
164 nd = d;
165 for (d=0 ; d < info.array.raid_disks ; d++) {
166 mdu_disk_info_t disk;
167 char *dv;
168
169 disk.number = d;
170 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
171 fprintf(stderr, Name ": cannot get device detail for device %d\n",
172 d);
173 return 1;
174 }
175 dv = map_dev(disk.major, disk.minor, 1);
176 if (!dv) {
177 fprintf(stderr, Name ": cannot find device file for device %d\n",
178 d);
179 return 1;
180 }
181 fd2 = dev_open(dv, O_RDWR);
182 if (fd2 < 0) {
183 fprintf(stderr, Name ": cannot open device file %s\n", dv);
184 return 1;
185 }
186 if (st->ss->load_super(st, fd2, NULL)) {
187 fprintf(stderr, Name ": cannot find super block on %s\n", dv);
188 close(fd);
189 return 1;
190 }
191 info.array.raid_disks = nd+1;
192 info.array.nr_disks = nd+1;
193 info.array.active_disks = nd+1;
194 info.array.working_disks = nd+1;
195
196 st->ss->update_super(st, &info, "linear-grow-update", dv,
197 0, 0, NULL);
198
199 if (st->ss->store_super(st, fd2)) {
200 fprintf(stderr, Name ": Cannot store new superblock on %s\n", dv);
201 close(fd2);
202 return 1;
203 }
204 close(fd2);
205 }
206
207 return 0;
208 }
209
210 int Grow_addbitmap(char *devname, int fd, char *file, int chunk, int delay, int write_behind, int force)
211 {
212 /*
213 * First check that array doesn't have a bitmap
214 * Then create the bitmap
215 * Then add it
216 *
217 * For internal bitmaps, we need to check the version,
218 * find all the active devices, and write the bitmap block
219 * to all devices
220 */
221 mdu_bitmap_file_t bmf;
222 mdu_array_info_t array;
223 struct supertype *st;
224 char *subarray = NULL;
225 int major = BITMAP_MAJOR_HI;
226 int vers = md_get_version(fd);
227 unsigned long long bitmapsize, array_size;
228
229 if (vers < 9003) {
230 major = BITMAP_MAJOR_HOSTENDIAN;
231 fprintf(stderr, Name ": Warning - bitmaps created on this kernel"
232 " are not portable\n"
233 " between different architectures. Consider upgrading"
234 " the Linux kernel.\n");
235 }
236
237 if (ioctl(fd, GET_BITMAP_FILE, &bmf) != 0) {
238 if (errno == ENOMEM)
239 fprintf(stderr, Name ": Memory allocation failure.\n");
240 else
241 fprintf(stderr, Name ": bitmaps not supported by this kernel.\n");
242 return 1;
243 }
244 if (bmf.pathname[0]) {
245 if (strcmp(file,"none")==0) {
246 if (ioctl(fd, SET_BITMAP_FILE, -1)!= 0) {
247 fprintf(stderr, Name ": failed to remove bitmap %s\n",
248 bmf.pathname);
249 return 1;
250 }
251 return 0;
252 }
253 fprintf(stderr, Name ": %s already has a bitmap (%s)\n",
254 devname, bmf.pathname);
255 return 1;
256 }
257 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
258 fprintf(stderr, Name ": cannot get array status for %s\n", devname);
259 return 1;
260 }
261 if (array.state & (1<<MD_SB_BITMAP_PRESENT)) {
262 if (strcmp(file, "none")==0) {
263 array.state &= ~(1<<MD_SB_BITMAP_PRESENT);
264 if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
265 fprintf(stderr, Name ": failed to remove internal bitmap.\n");
266 return 1;
267 }
268 return 0;
269 }
270 fprintf(stderr, Name ": Internal bitmap already present on %s\n",
271 devname);
272 return 1;
273 }
274
275 if (strcmp(file, "none") == 0) {
276 fprintf(stderr, Name ": no bitmap found on %s\n", devname);
277 return 1;
278 }
279 if (array.level <= 0) {
280 fprintf(stderr, Name ": Bitmaps not meaningful with level %s\n",
281 map_num(pers, array.level)?:"of this array");
282 return 1;
283 }
284 bitmapsize = array.size;
285 bitmapsize <<= 1;
286 if (get_dev_size(fd, NULL, &array_size) &&
287 array_size > (0x7fffffffULL<<9)) {
288 /* Array is big enough that we cannot trust array.size
289 * try other approaches
290 */
291 bitmapsize = get_component_size(fd);
292 }
293 if (bitmapsize == 0) {
294 fprintf(stderr, Name ": Cannot reliably determine size of array to create bitmap - sorry.\n");
295 return 1;
296 }
297
298 if (array.level == 10) {
299 int ncopies = (array.layout&255)*((array.layout>>8)&255);
300 bitmapsize = bitmapsize * array.raid_disks / ncopies;
301 }
302
303 st = super_by_fd(fd, &subarray);
304 if (!st) {
305 fprintf(stderr, Name ": Cannot understand version %d.%d\n",
306 array.major_version, array.minor_version);
307 return 1;
308 }
309 if (subarray) {
310 fprintf(stderr, Name ": Cannot add bitmaps to sub-arrays yet\n");
311 free(subarray);
312 free(st);
313 return 1;
314 }
315 if (strcmp(file, "internal") == 0) {
316 int d;
317 if (st->ss->add_internal_bitmap == NULL) {
318 fprintf(stderr, Name ": Internal bitmaps not supported "
319 "with %s metadata\n", st->ss->name);
320 return 1;
321 }
322 for (d=0; d< st->max_devs; d++) {
323 mdu_disk_info_t disk;
324 char *dv;
325 disk.number = d;
326 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
327 continue;
328 if (disk.major == 0 &&
329 disk.minor == 0)
330 continue;
331 if ((disk.state & (1<<MD_DISK_SYNC))==0)
332 continue;
333 dv = map_dev(disk.major, disk.minor, 1);
334 if (dv) {
335 int fd2 = dev_open(dv, O_RDWR);
336 if (fd2 < 0)
337 continue;
338 if (st->ss->load_super(st, fd2, NULL)==0) {
339 if (st->ss->add_internal_bitmap(
340 st,
341 &chunk, delay, write_behind,
342 bitmapsize, 0, major)
343 )
344 st->ss->write_bitmap(st, fd2);
345 else {
346 fprintf(stderr, Name ": failed to create internal bitmap - chunksize problem.\n");
347 close(fd2);
348 return 1;
349 }
350 }
351 close(fd2);
352 }
353 }
354 array.state |= (1<<MD_SB_BITMAP_PRESENT);
355 if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
356 if (errno == EBUSY)
357 fprintf(stderr, Name
358 ": Cannot add bitmap while array is"
359 " resyncing or reshaping etc.\n");
360 fprintf(stderr, Name ": failed to set internal bitmap.\n");
361 return 1;
362 }
363 } else {
364 int uuid[4];
365 int bitmap_fd;
366 int d;
367 int max_devs = st->max_devs;
368
369 /* try to load a superblock */
370 for (d=0; d<max_devs; d++) {
371 mdu_disk_info_t disk;
372 char *dv;
373 int fd2;
374 disk.number = d;
375 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
376 continue;
377 if ((disk.major==0 && disk.minor==0) ||
378 (disk.state & (1<<MD_DISK_REMOVED)))
379 continue;
380 dv = map_dev(disk.major, disk.minor, 1);
381 if (!dv) continue;
382 fd2 = dev_open(dv, O_RDONLY);
383 if (fd2 >= 0 &&
384 st->ss->load_super(st, fd2, NULL) == 0) {
385 close(fd2);
386 st->ss->uuid_from_super(st, uuid);
387 break;
388 }
389 close(fd2);
390 }
391 if (d == max_devs) {
392 fprintf(stderr, Name ": cannot find UUID for array!\n");
393 return 1;
394 }
395 if (CreateBitmap(file, force, (char*)uuid, chunk,
396 delay, write_behind, bitmapsize, major)) {
397 return 1;
398 }
399 bitmap_fd = open(file, O_RDWR);
400 if (bitmap_fd < 0) {
401 fprintf(stderr, Name ": weird: %s cannot be opened\n",
402 file);
403 return 1;
404 }
405 if (ioctl(fd, SET_BITMAP_FILE, bitmap_fd) < 0) {
406 int err = errno;
407 if (errno == EBUSY)
408 fprintf(stderr, Name
409 ": Cannot add bitmap while array is"
410 " resyncing or reshaping etc.\n");
411 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
412 devname, strerror(err));
413 return 1;
414 }
415 }
416
417 return 0;
418 }
419
420
421 /*
422 * When reshaping an array we might need to backup some data.
423 * This is written to all spares with a 'super_block' describing it.
424 * The superblock goes 4K from the end of the used space on the
425 * device.
426 * It if written after the backup is complete.
427 * It has the following structure.
428 */
429
430 static struct mdp_backup_super {
431 char magic[16]; /* md_backup_data-1 or -2 */
432 __u8 set_uuid[16];
433 __u64 mtime;
434 /* start/sizes in 512byte sectors */
435 __u64 devstart; /* address on backup device/file of data */
436 __u64 arraystart;
437 __u64 length;
438 __u32 sb_csum; /* csum of preceeding bytes. */
439 __u32 pad1;
440 __u64 devstart2; /* offset in to data of second section */
441 __u64 arraystart2;
442 __u64 length2;
443 __u32 sb_csum2; /* csum of preceeding bytes. */
444 __u8 pad[512-68-32];
445 } __attribute__((aligned(512))) bsb, bsb2;
446
447 static __u32 bsb_csum(char *buf, int len)
448 {
449 int i;
450 int csum = 0;
451 for (i=0; i<len; i++)
452 csum = (csum<<3) + buf[0];
453 return __cpu_to_le32(csum);
454 }
455
456 static int child_grow(int afd, struct mdinfo *sra, unsigned long blocks,
457 int *fds, unsigned long long *offsets,
458 int disks, int chunk, int level, int layout, int data,
459 int dests, int *destfd, unsigned long long *destoffsets);
460 static int child_shrink(int afd, struct mdinfo *sra, unsigned long blocks,
461 int *fds, unsigned long long *offsets,
462 int disks, int chunk, int level, int layout, int data,
463 int dests, int *destfd, unsigned long long *destoffsets);
464 static int child_same_size(int afd, struct mdinfo *sra, unsigned long blocks,
465 int *fds, unsigned long long *offsets,
466 unsigned long long start,
467 int disks, int chunk, int level, int layout, int data,
468 int dests, int *destfd, unsigned long long *destoffsets);
469
470 static int check_idle(struct supertype *st)
471 {
472 /* Check that all member arrays for this container, or the
473 * container of this array, are idle
474 */
475 int container_dev = (st->container_dev != NoMdDev
476 ? st->container_dev : st->devnum);
477 char container[40];
478 struct mdstat_ent *ent, *e;
479 int is_idle = 1;
480
481 fmt_devname(container, container_dev);
482 ent = mdstat_read(0, 0);
483 for (e = ent ; e; e = e->next) {
484 if (!is_container_member(e, container))
485 continue;
486 if (e->percent >= 0) {
487 is_idle = 0;
488 break;
489 }
490 }
491 free_mdstat(ent);
492 return is_idle;
493 }
494
495 static int freeze_container(struct supertype *st)
496 {
497 int container_dev = (st->container_dev != NoMdDev
498 ? st->container_dev : st->devnum);
499 char container[40];
500
501 if (!check_idle(st))
502 return -1;
503
504 fmt_devname(container, container_dev);
505
506 if (block_monitor(container, 1)) {
507 fprintf(stderr, Name ": failed to freeze container\n");
508 return -2;
509 }
510
511 return 1;
512 }
513
514 static void unfreeze_container(struct supertype *st)
515 {
516 int container_dev = (st->container_dev != NoMdDev
517 ? st->container_dev : st->devnum);
518 char container[40];
519
520 fmt_devname(container, container_dev);
521
522 unblock_monitor(container, 1);
523 }
524
525 static int freeze(struct supertype *st)
526 {
527 /* Try to freeze resync/rebuild on this array/container.
528 * Return -1 if the array is busy,
529 * return -2 container cannot be frozen,
530 * return 0 if this kernel doesn't support 'frozen'
531 * return 1 if it worked.
532 */
533 if (st->ss->external)
534 return freeze_container(st);
535 else {
536 struct mdinfo *sra = sysfs_read(-1, st->devnum, GET_VERSION);
537 int err;
538
539 if (!sra)
540 return -1;
541 err = sysfs_freeze_array(sra);
542 sysfs_free(sra);
543 return err;
544 }
545 }
546
547 static void unfreeze(struct supertype *st, int frozen)
548 {
549 /* If 'frozen' is 1, unfreeze the array */
550 if (frozen <= 0)
551 return;
552
553 if (st->ss->external)
554 return unfreeze_container(st);
555 else {
556 struct mdinfo *sra = sysfs_read(-1, st->devnum, GET_VERSION);
557
558 if (sra)
559 sysfs_set_str(sra, NULL, "sync_action", "idle");
560 else
561 fprintf(stderr, Name ": failed to unfreeze array\n");
562 sysfs_free(sra);
563 }
564 }
565
566 static void wait_reshape(struct mdinfo *sra)
567 {
568 int fd = sysfs_get_fd(sra, NULL, "sync_action");
569 char action[20];
570
571 if (fd < 0)
572 return;
573
574 while (sysfs_fd_get_str(fd, action, 20) > 0 &&
575 strncmp(action, "reshape", 7) == 0) {
576 fd_set rfds;
577 FD_ZERO(&rfds);
578 FD_SET(fd, &rfds);
579 select(fd+1, NULL, NULL, &rfds, NULL);
580 }
581 close(fd);
582 }
583
584 static int reshape_super(struct supertype *st, long long size, int level,
585 int layout, int chunksize, int raid_disks,
586 char *backup_file, char *dev, int verbose)
587 {
588 /* nothing extra to check in the native case */
589 if (!st->ss->external)
590 return 0;
591 if (!st->ss->reshape_super ||
592 !st->ss->manage_reshape) {
593 fprintf(stderr, Name ": %s metadata does not support reshape\n",
594 st->ss->name);
595 return 1;
596 }
597
598 return st->ss->reshape_super(st, size, level, layout, chunksize,
599 raid_disks, backup_file, dev, verbose);
600 }
601
602 static void sync_metadata(struct supertype *st)
603 {
604 if (st->ss->external) {
605 if (st->update_tail) {
606 flush_metadata_updates(st);
607 st->update_tail = &st->updates;
608 } else
609 st->ss->sync_metadata(st);
610 }
611 }
612
613 static int subarray_set_num(char *container, struct mdinfo *sra, char *name, int n)
614 {
615 /* when dealing with external metadata subarrays we need to be
616 * prepared to handle EAGAIN. The kernel may need to wait for
617 * mdmon to mark the array active so the kernel can handle
618 * allocations/writeback when preparing the reshape action
619 * (md_allow_write()). We temporarily disable safe_mode_delay
620 * to close a race with the array_state going clean before the
621 * next write to raid_disks / stripe_cache_size
622 */
623 char safe[50];
624 int rc;
625
626 /* only 'raid_disks' and 'stripe_cache_size' trigger md_allow_write */
627 if (strcmp(name, "raid_disks") != 0 &&
628 strcmp(name, "stripe_cache_size") != 0)
629 return sysfs_set_num(sra, NULL, name, n);
630
631 rc = sysfs_get_str(sra, NULL, "safe_mode_delay", safe, sizeof(safe));
632 if (rc <= 0)
633 return -1;
634 sysfs_set_num(sra, NULL, "safe_mode_delay", 0);
635 rc = sysfs_set_num(sra, NULL, name, n);
636 if (rc < 0 && errno == EAGAIN) {
637 ping_monitor(container);
638 /* if we get EAGAIN here then the monitor is not active
639 * so stop trying
640 */
641 rc = sysfs_set_num(sra, NULL, name, n);
642 }
643 sysfs_set_str(sra, NULL, "safe_mode_delay", safe);
644 return rc;
645 }
646
647 static int reshape_container_raid_disks(char *container, int raid_disks)
648 {
649 /* for each subarray switch to a raid level that can
650 * support the reshape, and set raid disks
651 */
652 struct mdstat_ent *ent, *e;
653 int changed = 0, rv = 0, err = 0;
654
655 ent = mdstat_read(1, 0);
656 if (!ent) {
657 fprintf(stderr, Name ": unable to read /proc/mdstat\n");
658 return -1;
659 }
660
661 changed = 0;
662 for (e = ent; e; e = e->next) {
663 struct mdinfo *sub;
664 unsigned int cache;
665 int level, takeover_delta = 0;
666
667 if (!is_container_member(e, container))
668 continue;
669
670 level = map_name(pers, e->level);
671 if (level == 0) {
672 sub = sysfs_read(-1, e->devnum, GET_VERSION);
673 if (!sub)
674 break;
675 /* metadata records 'orig_level' */
676 rv = sysfs_set_num(sub, NULL, "level", 4);
677 if (rv < 0) {
678 err = errno;
679 break;
680 }
681 /* we want spares to be used for capacity
682 * expansion, not rebuild
683 */
684 takeover_delta = 1;
685
686 sysfs_free(sub);
687 level = 4;
688 }
689
690 sub = NULL;
691 switch (level) {
692 default:
693 rv = -1;
694 break;
695 case 4:
696 case 5:
697 case 6:
698 sub = sysfs_read(-1, e->devnum, GET_CHUNK|GET_CACHE);
699 if (!sub)
700 break;
701 cache = (sub->array.chunk_size / 4096) * 4;
702 if (cache > sub->cache_size)
703 rv = subarray_set_num(container, sub,
704 "stripe_cache_size", cache);
705 if (rv) {
706 err = errno;
707 break;
708 }
709 /* fall through */
710 case 1:
711 if (!sub)
712 sub = sysfs_read(-1, e->devnum, GET_VERSION);
713 if (!sub)
714 break;
715
716 rv = subarray_set_num(container, sub, "raid_disks",
717 raid_disks + takeover_delta);
718 if (rv)
719 err = errno;
720 else
721 changed++;
722 break;
723 }
724 sysfs_free(sub);
725 if (rv)
726 break;
727 }
728 free_mdstat(ent);
729 if (rv) {
730 fprintf(stderr, Name
731 ": failed to initiate container reshape%s%s\n",
732 err ? ": " : "", err ? strerror(err) : "");
733 return rv;
734 }
735
736 return changed;
737 }
738
739 static void revert_container_raid_disks(struct supertype *st, int fd, char *container)
740 {
741 /* we failed to prepare all subarrays in the container for
742 * reshape, so cancel the changes and restore the nominal raid
743 * level
744 */
745 struct mdstat_ent *ent, *e;
746
747 ent = mdstat_read(0, 0);
748 if (!ent) {
749 fprintf(stderr, Name
750 ": failed to read /proc/mdstat while aborting reshape\n");
751 return;
752 }
753
754 if (st->ss->load_container(st, fd, NULL)) {
755 fprintf(stderr, Name
756 ": failed read metadata while aborting reshape\n");
757 return ;
758 }
759
760
761 for (e = ent; e; e = e->next) {
762 int level_fixed = 0, disks_fixed = 0;
763 struct mdinfo *sub, *prev;
764 char *subarray;
765
766 if (!is_container_member(e, container))
767 continue;
768
769 subarray = to_subarray(e, container);
770 prev = st->ss->container_content(st, subarray);
771
772 /* changing level might change raid_disks so we do it
773 * first and then check if raid_disks still needs fixing
774 */
775 if (map_name(pers, e->level) != prev->array.level) {
776 sub = sysfs_read(-1, e->devnum, GET_VERSION);
777 if (sub &&
778 !sysfs_set_num(sub, NULL, "level", prev->array.level))
779 level_fixed = 1;
780 sysfs_free(sub);
781 } else
782 level_fixed = 1;
783
784 sub = sysfs_read(-1, e->devnum, GET_DISKS);
785 if (sub && sub->array.raid_disks != prev->array.raid_disks) {
786 if (!subarray_set_num(container, sub, "raid_disks",
787 prev->array.raid_disks))
788 disks_fixed = 1;
789 } else if (sub)
790 disks_fixed = 1;
791 sysfs_free(sub);
792
793 if (!disks_fixed || !level_fixed)
794 fprintf(stderr, Name
795 ": failed to restore %s to a %d-disk %s array\n",
796 e->dev, prev->array.raid_disks,
797 map_num(pers, prev->array.level));
798 free(prev);
799 }
800 st->ss->free_super(st);
801 free_mdstat(ent);
802 }
803
804 int remove_disks_on_raid10_to_raid0_takeover(struct supertype *st,
805 struct mdinfo *sra,
806 int layout)
807 {
808 int nr_of_copies;
809 struct mdinfo *remaining;
810 int slot;
811
812 nr_of_copies = layout & 0xff;
813
814 remaining = sra->devs;
815 sra->devs = NULL;
816 /* for each 'copy', select one device and remove from the list. */
817 for (slot = 0; slot < sra->array.raid_disks; slot += nr_of_copies) {
818 struct mdinfo **diskp;
819 int found = 0;
820
821 /* Find a working device to keep */
822 for (diskp = &remaining; *diskp ; diskp = &(*diskp)->next) {
823 struct mdinfo *disk = *diskp;
824
825 if (disk->disk.raid_disk < slot)
826 continue;
827 if (disk->disk.raid_disk >= slot + nr_of_copies)
828 continue;
829 if (disk->disk.state & (1<<MD_DISK_REMOVED))
830 continue;
831 if (disk->disk.state & (1<<MD_DISK_FAULTY))
832 continue;
833 if (!(disk->disk.state & (1<<MD_DISK_SYNC)))
834 continue;
835
836 /* We have found a good disk to use! */
837 *diskp = disk->next;
838 disk->next = sra->devs;
839 sra->devs = disk;
840 found = 1;
841 break;
842 }
843 if (!found)
844 break;
845 }
846
847 if (slot < sra->array.raid_disks) {
848 /* didn't find all slots */
849 struct mdinfo **e;
850 e = &remaining;
851 while (*e)
852 e = &(*e)->next;
853 *e = sra->devs;
854 sra->devs = remaining;
855 return 1;
856 }
857
858 /* Remove all 'remaining' devices from the array */
859 while (remaining) {
860 struct mdinfo *sd = remaining;
861 remaining = sd->next;
862
863 sysfs_set_str(sra, sd, "state", "faulty");
864 sysfs_set_str(sra, sd, "slot", "none");
865 sysfs_set_str(sra, sd, "state", "remove");
866 sd->disk.state |= (1<<MD_DISK_REMOVED);
867 sd->disk.state &= ~(1<<MD_DISK_SYNC);
868 sd->next = sra->devs;
869 sra->devs = sd;
870 }
871 return 0;
872 }
873
874 void reshape_free_fdlist(int *fdlist,
875 unsigned long long *offsets,
876 int size)
877 {
878 int i;
879
880 for (i = 0; i < size; i++)
881 if (fdlist[i] >= 0)
882 close(fdlist[i]);
883
884 free(fdlist);
885 free(offsets);
886 }
887
888 int reshape_prepare_fdlist(char *devname,
889 struct mdinfo *sra,
890 int raid_disks,
891 int nrdisks,
892 unsigned long blocks,
893 char *backup_file,
894 int *fdlist,
895 unsigned long long *offsets)
896 {
897 int d = 0;
898 struct mdinfo *sd;
899
900 for (d = 0; d <= nrdisks; d++)
901 fdlist[d] = -1;
902 d = raid_disks;
903 for (sd = sra->devs; sd; sd = sd->next) {
904 if (sd->disk.state & (1<<MD_DISK_FAULTY))
905 continue;
906 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
907 char *dn = map_dev(sd->disk.major,
908 sd->disk.minor, 1);
909 fdlist[sd->disk.raid_disk]
910 = dev_open(dn, O_RDONLY);
911 offsets[sd->disk.raid_disk] = sd->data_offset*512;
912 if (fdlist[sd->disk.raid_disk] < 0) {
913 fprintf(stderr,
914 Name ": %s: cannot open component %s\n",
915 devname, dn ? dn : "-unknown-");
916 d = -1;
917 goto release;
918 }
919 } else if (backup_file == NULL) {
920 /* spare */
921 char *dn = map_dev(sd->disk.major,
922 sd->disk.minor, 1);
923 fdlist[d] = dev_open(dn, O_RDWR);
924 offsets[d] = (sd->data_offset + sra->component_size - blocks - 8)*512;
925 if (fdlist[d] < 0) {
926 fprintf(stderr, Name ": %s: cannot open component %s\n",
927 devname, dn ? dn : "-unknown-");
928 d = -1;
929 goto release;
930 }
931 d++;
932 }
933 }
934 release:
935 return d;
936 }
937
938 int reshape_open_backup_file(char *backup_file,
939 int fd,
940 char *devname,
941 long blocks,
942 int *fdlist,
943 unsigned long long *offsets)
944 {
945 /* Return 1 on success, 0 on any form of failure */
946 /* need to check backup file is large enough */
947 char buf[512];
948 struct stat stb;
949 unsigned int dev;
950 int i;
951
952 *fdlist = open(backup_file, O_RDWR|O_CREAT|O_EXCL,
953 S_IRUSR | S_IWUSR);
954 *offsets = 8 * 512;
955 if (*fdlist < 0) {
956 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
957 devname, backup_file, strerror(errno));
958 return 0;
959 }
960 /* Guard against backup file being on array device.
961 * If array is partitioned or if LVM etc is in the
962 * way this will not notice, but it is better than
963 * nothing.
964 */
965 fstat(*fdlist, &stb);
966 dev = stb.st_dev;
967 fstat(fd, &stb);
968 if (stb.st_rdev == dev) {
969 fprintf(stderr, Name ": backup file must NOT be"
970 " on the array being reshaped.\n");
971 close(*fdlist);
972 return 0;
973 }
974
975 memset(buf, 0, 512);
976 for (i=0; i < blocks + 1 ; i++) {
977 if (write(*fdlist, buf, 512) != 512) {
978 fprintf(stderr, Name ": %s: cannot create"
979 " backup file %s: %s\n",
980 devname, backup_file, strerror(errno));
981 return 0;
982 }
983 }
984 if (fsync(*fdlist) != 0) {
985 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
986 devname, backup_file, strerror(errno));
987 return 0;
988 }
989
990 return 1;
991 }
992
993 unsigned long compute_backup_blocks(int nchunk, int ochunk,
994 unsigned int ndata, unsigned int odata)
995 {
996 unsigned long a, b, blocks;
997 /* So how much do we need to backup.
998 * We need an amount of data which is both a whole number of
999 * old stripes and a whole number of new stripes.
1000 * So LCM for (chunksize*datadisks).
1001 */
1002 a = (ochunk/512) * odata;
1003 b = (nchunk/512) * ndata;
1004 /* Find GCD */
1005 while (a != b) {
1006 if (a < b)
1007 b -= a;
1008 if (b < a)
1009 a -= b;
1010 }
1011 /* LCM == product / GCD */
1012 blocks = (ochunk/512) * (nchunk/512) * odata * ndata / a;
1013
1014 return blocks;
1015 }
1016
1017
1018 int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
1019 long long size,
1020 int level, char *layout_str, int chunksize, int raid_disks,
1021 int force)
1022 {
1023 /* Make some changes in the shape of an array.
1024 * The kernel must support the change.
1025 *
1026 * There are three different changes. Each can trigger
1027 * a resync or recovery so we freeze that until we have
1028 * requested everything (if kernel supports freezing - 2.6.30).
1029 * The steps are:
1030 * - change size (i.e. component_size)
1031 * - change level
1032 * - change layout/chunksize/ndisks
1033 *
1034 * The last can require a reshape. It is different on different
1035 * levels so we need to check the level before actioning it.
1036 * Some times the level change needs to be requested after the
1037 * reshape (e.g. raid6->raid5, raid5->raid0)
1038 *
1039 */
1040 struct mdu_array_info_s array, orig;
1041 char *c;
1042 int rv = 0;
1043 struct supertype *st;
1044 char *subarray = NULL;
1045
1046 int nchunk, ochunk;
1047 int nlayout, olayout;
1048 int ndisks, odisks;
1049 unsigned int ndata, odata;
1050 int orig_level = UnSet;
1051 char alt_layout[40];
1052 int *fdlist;
1053 unsigned long long *offsets;
1054 int d;
1055 int nrdisks;
1056 int err;
1057 int frozen;
1058 unsigned long blocks, stripes;
1059 unsigned long cache;
1060 unsigned long long array_size;
1061 int changed = 0;
1062 char *container = NULL;
1063 int cfd = -1;
1064 int done;
1065
1066 struct mdinfo *sra;
1067 struct mdinfo *sd;
1068
1069 if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) {
1070 fprintf(stderr, Name ": %s is not an active md array - aborting\n",
1071 devname);
1072 return 1;
1073 }
1074
1075 if (size >= 0 &&
1076 (chunksize || level!= UnSet || layout_str || raid_disks)) {
1077 fprintf(stderr, Name ": cannot change component size at the same time "
1078 "as other changes.\n"
1079 " Change size first, then check data is intact before "
1080 "making other changes.\n");
1081 return 1;
1082 }
1083
1084 if (raid_disks && raid_disks < array.raid_disks && array.level > 1 &&
1085 get_linux_version() < 2006032 &&
1086 !check_env("MDADM_FORCE_FEWER")) {
1087 fprintf(stderr, Name ": reducing the number of devices is not safe before Linux 2.6.32\n"
1088 " Please use a newer kernel\n");
1089 return 1;
1090 }
1091
1092 st = super_by_fd(fd, &subarray);
1093 if (!st) {
1094 fprintf(stderr, Name ": Unable to determine metadata format for %s\n", devname);
1095 return 1;
1096 }
1097 if (raid_disks > st->max_devs) {
1098 fprintf(stderr, Name ": Cannot increase raid-disks on this array"
1099 " beyond %d\n", st->max_devs);
1100 return 1;
1101 }
1102
1103 /* in the external case we need to check that the requested reshape is
1104 * supported, and perform an initial check that the container holds the
1105 * pre-requisite spare devices (mdmon owns final validation)
1106 */
1107 if (st->ss->external) {
1108 int container_dev;
1109 int rv;
1110
1111 if (subarray) {
1112 container_dev = st->container_dev;
1113 cfd = open_dev_excl(st->container_dev);
1114 } else if (size >= 0 || layout_str != NULL || chunksize != 0 ||
1115 level != UnSet) {
1116 fprintf(stderr,
1117 Name ": %s is a container, only 'raid-devices' can be changed\n",
1118 devname);
1119 return 1;
1120 } else {
1121 container_dev = st->devnum;
1122 close(fd);
1123 cfd = open_dev_excl(st->devnum);
1124 fd = cfd;
1125 }
1126 if (cfd < 0) {
1127 fprintf(stderr, Name ": Unable to open container for %s\n",
1128 devname);
1129 free(subarray);
1130 return 1;
1131 }
1132
1133 container = devnum2devname(st->devnum);
1134 if (!container) {
1135 fprintf(stderr, Name ": Could not determine container name\n");
1136 free(subarray);
1137 return 1;
1138 }
1139
1140 if (subarray)
1141 rv = st->ss->load_container(st, cfd, NULL);
1142 else
1143 rv = st->ss->load_super(st, cfd, NULL);
1144 if (rv) {
1145 fprintf(stderr, Name ": Cannot read superblock for %s\n",
1146 devname);
1147 free(subarray);
1148 return 1;
1149 }
1150
1151 if (mdmon_running(container_dev))
1152 st->update_tail = &st->updates;
1153 }
1154
1155 if (raid_disks > array.raid_disks &&
1156 array.spare_disks < (raid_disks - array.raid_disks) &&
1157 !force) {
1158 fprintf(stderr,
1159 Name ": Need %d spare%s to avoid degraded array,"
1160 " and only have %d.\n"
1161 " Use --force to over-ride this check.\n",
1162 raid_disks - array.raid_disks,
1163 raid_disks - array.raid_disks == 1 ? "" : "s",
1164 array.spare_disks);
1165 return 1;
1166 }
1167
1168 sra = sysfs_read(fd, 0, GET_LEVEL | GET_DISKS | GET_DEVS | GET_STATE);
1169 if (sra) {
1170 if (st->ss->external && subarray == NULL) {
1171 array.level = LEVEL_CONTAINER;
1172 sra->array.level = LEVEL_CONTAINER;
1173 }
1174 } else {
1175 fprintf(stderr, Name ": failed to read sysfs parameters for %s\n",
1176 devname);
1177 return 1;
1178 }
1179 frozen = freeze(st);
1180 if (frozen < -1) {
1181 /* freeze() already spewed the reason */
1182 return 1;
1183 } else if (frozen < 0) {
1184 fprintf(stderr, Name ": %s is performing resync/recovery and cannot"
1185 " be reshaped\n", devname);
1186 return 1;
1187 }
1188
1189 /* ========= set size =============== */
1190 if (size >= 0 && (size == 0 || size != array.size)) {
1191 long long orig_size = array.size;
1192
1193 if (reshape_super(st, size, UnSet, UnSet, 0, 0, NULL, devname, !quiet)) {
1194 rv = 1;
1195 goto release;
1196 }
1197 sync_metadata(st);
1198 array.size = size;
1199 if (array.size != size) {
1200 /* got truncated to 32bit, write to
1201 * component_size instead
1202 */
1203 if (sra)
1204 rv = sysfs_set_num(sra, NULL,
1205 "component_size", size);
1206 else
1207 rv = -1;
1208 } else
1209 rv = ioctl(fd, SET_ARRAY_INFO, &array);
1210 if (rv != 0) {
1211 int err = errno;
1212
1213 /* restore metadata */
1214 if (reshape_super(st, orig_size, UnSet, UnSet, 0, 0,
1215 NULL, devname, !quiet) == 0)
1216 sync_metadata(st);
1217 fprintf(stderr, Name ": Cannot set device size for %s: %s\n",
1218 devname, strerror(err));
1219 if (err == EBUSY &&
1220 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
1221 fprintf(stderr, " Bitmap must be removed before size can be changed\n");
1222 rv = 1;
1223 goto release;
1224 }
1225 ioctl(fd, GET_ARRAY_INFO, &array);
1226 size = get_component_size(fd)/2;
1227 if (size == 0)
1228 size = array.size;
1229 if (!quiet)
1230 fprintf(stderr, Name ": component size of %s has been set to %lluK\n",
1231 devname, size);
1232 changed = 1;
1233 } else if (array.level != LEVEL_CONTAINER) {
1234 size = get_component_size(fd)/2;
1235 if (size == 0)
1236 size = array.size;
1237 }
1238
1239 /* ========= check for Raid10 -> Raid0 conversion ===============
1240 * current implemenation assumes that following conditions must be met:
1241 * - far_copies == 1
1242 * - near_copies == 2
1243 */
1244 if (level == 0 && array.level == 10 &&
1245 array.layout == ((1 << 8) + 2) && !(array.raid_disks & 1)) {
1246 int err;
1247 err = remove_disks_on_raid10_to_raid0_takeover(st, sra, array.layout);
1248 if (err) {
1249 dprintf(Name": Array cannot be reshaped\n");
1250 if (container)
1251 free(container);
1252 if (cfd > -1)
1253 close(cfd);
1254 return 1;
1255 }
1256 }
1257
1258 /* ======= set level =========== */
1259 if (level != UnSet && level != array.level) {
1260 /* Trying to change the level.
1261 * We might need to change layout first and schedule a
1262 * level change for later.
1263 * Level changes that can happen immediately are:
1264 * 0->4,5,6 1->5 4->5,6 5->1,6
1265 * Level changes that need a layout change first are:
1266 * 6->5,4,0 : need a -6 layout, or parity-last
1267 * 5->4,0 : need parity-last
1268 */
1269 if ((array.level == 6 || array.level == 5) &&
1270 (level == 5 || level == 4 || level == 0)) {
1271 /* Don't change level yet, but choose intermediate
1272 * layout
1273 */
1274 if (level == 5) {
1275 if (layout_str == NULL)
1276 switch (array.layout) {
1277 case ALGORITHM_LEFT_ASYMMETRIC:
1278 case ALGORITHM_LEFT_ASYMMETRIC_6:
1279 case ALGORITHM_ROTATING_N_RESTART:
1280 layout_str = "left-asymmetric-6";
1281 break;
1282 case ALGORITHM_LEFT_SYMMETRIC:
1283 case ALGORITHM_LEFT_SYMMETRIC_6:
1284 case ALGORITHM_ROTATING_N_CONTINUE:
1285 layout_str = "left-symmetric-6";
1286 break;
1287 case ALGORITHM_RIGHT_ASYMMETRIC:
1288 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1289 case ALGORITHM_ROTATING_ZERO_RESTART:
1290 layout_str = "right-asymmetric-6";
1291 break;
1292 case ALGORITHM_RIGHT_SYMMETRIC:
1293 case ALGORITHM_RIGHT_SYMMETRIC_6:
1294 layout_str = "right-symmetric-6";
1295 break;
1296 case ALGORITHM_PARITY_0:
1297 case ALGORITHM_PARITY_0_6:
1298 layout_str = "parity-first-6";
1299 break;
1300 case ALGORITHM_PARITY_N:
1301 layout_str = "parity-last";
1302 break;
1303 default:
1304 fprintf(stderr, Name ": %s: cannot"
1305 "convert layout to RAID5 equivalent\n",
1306 devname);
1307 rv = 1;
1308 goto release;
1309 }
1310 else {
1311 int l = map_name(r5layout, layout_str);
1312 if (l == UnSet) {
1313 fprintf(stderr, Name ": %s: layout '%s' not recognised\n",
1314 devname, layout_str);
1315 rv = 1;
1316 goto release;
1317 }
1318 if (l != ALGORITHM_PARITY_N) {
1319 /* need the -6 version */
1320 char *ls = map_num(r5layout, l);
1321 strcat(strcpy(alt_layout, ls),
1322 "-6");
1323 layout_str = alt_layout;
1324 }
1325 }
1326 if (raid_disks)
1327 /* The final raid6->raid5 conversion
1328 * will reduce the number of disks,
1329 * so now we need to aim higher
1330 */
1331 raid_disks++;
1332 } else
1333 layout_str = "parity-last";
1334 } else {
1335 /* Level change is a simple takeover. In the external
1336 * case we don't check with the metadata handler until
1337 * we establish what the final layout will be. If the
1338 * level change is disallowed we will revert to
1339 * orig_level without disturbing the metadata, otherwise
1340 * we will send an update.
1341 */
1342 c = map_num(pers, level);
1343 if (c == NULL) {
1344 rv = 1;/* not possible */
1345 goto release;
1346 }
1347 if (!force) {
1348 /* Need to check there are enough spares */
1349 int spares_needed = 0;
1350 switch (array.level * 16 + level) {
1351 case 0x05:
1352 spares_needed = 1; break;
1353 case 0x06:
1354 spares_needed = 2; break;
1355 case 0x15:
1356 spares_needed = 1; break;
1357 case 0x16:
1358 spares_needed = 2; break;
1359 case 0x56:
1360 spares_needed = 1; break;
1361 }
1362 if (raid_disks > array.raid_disks)
1363 spares_needed += raid_disks-array.raid_disks;
1364 if (spares_needed > array.spare_disks) {
1365 fprintf(stderr,
1366 Name ": Need %d spare%s to avoid"
1367 " degraded array, and only have %d.\n"
1368 " Use --force to over-ride"
1369 " this check.\n",
1370 spares_needed,
1371 spares_needed == 1 ? "" : "s",
1372 array.spare_disks);
1373 rv = 1;
1374 goto release;
1375 }
1376 }
1377 err = sysfs_set_str(sra, NULL, "level", c);
1378 if (err) {
1379 err = errno;
1380 fprintf(stderr, Name ": %s: could not set level to %s\n",
1381 devname, c);
1382 if (err == EBUSY &&
1383 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
1384 fprintf(stderr, " Bitmap must be removed before level can be changed\n");
1385 rv = 1;
1386 goto release;
1387 }
1388 orig = array;
1389 orig_level = orig.level;
1390 ioctl(fd, GET_ARRAY_INFO, &array);
1391 if (layout_str == NULL &&
1392 orig.level == 5 && level == 6 &&
1393 array.layout != orig.layout)
1394 layout_str = map_num(r5layout, orig.layout);
1395 if (!quiet)
1396 fprintf(stderr, Name " level of %s changed to %s\n",
1397 devname, c);
1398 changed = 1;
1399 }
1400 }
1401
1402 /* ========= set shape (chunk_size / layout / ndisks) ============== */
1403 /* Check if layout change is a no-op */
1404 switch (array.level) {
1405 case 5:
1406 if (layout_str && array.layout == map_name(r5layout, layout_str))
1407 layout_str = NULL;
1408 break;
1409 case 6:
1410 if (layout_str == NULL &&
1411 ((chunksize && chunksize * 1024 != array.chunk_size) ||
1412 (raid_disks && raid_disks != array.raid_disks)) &&
1413 array.layout >= 16) {
1414 fprintf(stderr, Name
1415 ": %s has a non-standard layout. If you wish to preserve this\n"
1416 " during the reshape, please specify --layout=preserve\n"
1417 " If you want to change it, specify a layout or use --layout=normalise\n",
1418 devname);
1419 rv = 1;
1420 goto release;
1421 }
1422 if (layout_str &&
1423 (strcmp(layout_str, "normalise") == 0 ||
1424 strcmp(layout_str, "normalize") == 0)) {
1425 char *hyphen;
1426 strcpy(alt_layout, map_num(r6layout, array.layout));
1427 hyphen = strrchr(alt_layout, '-');
1428 if (hyphen && strcmp(hyphen, "-6") == 0) {
1429 *hyphen = 0;
1430 layout_str = alt_layout;
1431 }
1432 }
1433
1434 if (layout_str && array.layout == map_name(r6layout, layout_str))
1435 layout_str = NULL;
1436 if (layout_str && strcmp(layout_str, "preserve") == 0)
1437 layout_str = NULL;
1438 break;
1439 }
1440 if (layout_str == NULL
1441 && (chunksize == 0 || chunksize*1024 == array.chunk_size)
1442 && (raid_disks == 0 || raid_disks == array.raid_disks)) {
1443 if (reshape_super(st, -1, level, UnSet, 0, 0, NULL, devname, !quiet)) {
1444 rv = 1;
1445 goto release;
1446 }
1447 sync_metadata(st);
1448 rv = 0;
1449 if (level != UnSet && level != array.level) {
1450 /* Looks like this level change doesn't need
1451 * a reshape after all.
1452 */
1453 c = map_num(pers, level);
1454 if (c) {
1455 rv = sysfs_set_str(sra, NULL, "level", c);
1456 if (rv) {
1457 int err = errno;
1458 fprintf(stderr, Name ": %s: could not set level to %s\n",
1459 devname, c);
1460 if (err == EBUSY &&
1461 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
1462 fprintf(stderr, " Bitmap must be removed before level can be changed\n");
1463 rv = 1;
1464 }
1465 }
1466 } else if (!changed && !quiet)
1467 fprintf(stderr, Name ": %s: no change requested\n",
1468 devname);
1469
1470 if (st->ss->external && !mdmon_running(st->container_dev) &&
1471 level > 0) {
1472 start_mdmon(st->container_dev);
1473 ping_monitor(container);
1474 }
1475 goto release;
1476 }
1477
1478 c = map_num(pers, array.level);
1479 if (c == NULL) c = "-unknown-";
1480 switch (array.level) {
1481 default: /* raid0, linear, multipath cannot be reconfigured */
1482 fprintf(stderr, Name ": %s array %s cannot be reshaped.\n",
1483 c, devname);
1484 /* TODO raid0 raiddisks can be reshaped via raid4 */
1485 rv = 1;
1486 break;
1487 case LEVEL_CONTAINER: {
1488 int count;
1489
1490 /* double check that we are not changing anything but raid_disks */
1491 if (size >= 0 || layout_str != NULL || chunksize != 0 || level != UnSet) {
1492 fprintf(stderr,
1493 Name ": %s is a container, only 'raid-devices' can be changed\n",
1494 devname);
1495 rv = 1;
1496 goto release;
1497 }
1498
1499 if (reshape_super(st, -1, UnSet, UnSet, 0, raid_disks,
1500 backup_file, devname, !quiet)) {
1501 rv = 1;
1502 goto release;
1503 }
1504
1505 count = reshape_container_raid_disks(container, raid_disks);
1506 if (count < 0) {
1507 revert_container_raid_disks(st, fd, container);
1508 rv = 1;
1509 goto release;
1510 } else if (count == 0) {
1511 if (!quiet)
1512 fprintf(stderr, Name
1513 ": no active subarrays to reshape\n");
1514 goto release;
1515 }
1516
1517 sync_metadata(st);
1518 if (!mdmon_running(st->devnum)) {
1519 start_mdmon(st->devnum);
1520 ping_monitor(container);
1521 st->update_tail = &st->updates;
1522 }
1523
1524 /* give mdmon a chance to allocate spares */
1525 ping_manager(container);
1526
1527 /* manage_reshape takes care of releasing the array(s) */
1528 st->ss->manage_reshape(st, backup_file);
1529 frozen = 0;
1530 goto release;
1531 }
1532 case LEVEL_FAULTY: /* only 'layout' change is permitted */
1533
1534 if (chunksize || raid_disks) {
1535 fprintf(stderr, Name ": %s: Cannot change chunksize or disks of a 'faulty' array\n",
1536 devname);
1537 rv = 1;
1538 break;
1539 }
1540 if (layout_str == NULL)
1541 break; /* nothing to do.... */
1542
1543 array.layout = parse_layout_faulty(layout_str);
1544 if (array.layout < 0) {
1545 fprintf(stderr, Name ": %s: layout %s not understood for 'faulty' array\n",
1546 devname, layout_str);
1547 rv = 1;
1548 break;
1549 }
1550 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
1551 fprintf(stderr, Name ": Cannot set layout for %s: %s\n",
1552 devname, strerror(errno));
1553 rv = 1;
1554 } else if (!quiet)
1555 printf("layout for %s set to %d\n", devname, array.layout);
1556 break;
1557
1558 case 1: /* only raid_disks can each be changed. */
1559
1560 if (chunksize || layout_str != NULL) {
1561 fprintf(stderr, Name ": %s: Cannot change chunk size or layout for a RAID1 array.\n",
1562 devname);
1563 rv = 1;
1564 break;
1565 }
1566 if (raid_disks > 0) {
1567 if (reshape_super(st, -1, UnSet, UnSet, 0, raid_disks,
1568 NULL, devname, !quiet)) {
1569 rv = 1;
1570 goto release;
1571 }
1572 sync_metadata(st);
1573 array.raid_disks = raid_disks;
1574 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
1575 fprintf(stderr, Name ": Cannot set raid-devices for %s: %s\n",
1576 devname, strerror(errno));
1577 rv = 1;
1578 }
1579 }
1580 break;
1581
1582 case 4:
1583 case 5:
1584 case 6:
1585
1586 /*
1587 * layout/chunksize/raid_disks can be changed
1588 * though the kernel may not support it all.
1589 */
1590 if (subarray) {
1591 fprintf(stderr, Name ": Cannot reshape subarrays yet\n");
1592 break;
1593 }
1594
1595 /*
1596 * There are three possibilities.
1597 * 1/ The array will shrink.
1598 * We need to ensure the reshape will pause before reaching
1599 * the 'critical section'. We also need to fork and wait for
1600 * that to happen. When it does we
1601 * suspend/backup/complete/unfreeze
1602 *
1603 * 2/ The array will not change size.
1604 * This requires that we keep a backup of a sliding window
1605 * so that we can restore data after a crash. So we need
1606 * to fork and monitor progress.
1607 *
1608 * 3/ The array will grow. This is relatively easy.
1609 * However the kernel's restripe routines will cheerfully
1610 * overwrite some early data before it is safe. So we
1611 * need to make a backup of the early parts of the array
1612 * and be ready to restore it if rebuild aborts very early.
1613 *
1614 * We backup data by writing it to one spare, or to a
1615 * file which was given on command line.
1616 *
1617 * [FOLLOWING IS OLD AND PARTLY WRONG]
1618 * So: we enumerate the devices in the array and
1619 * make sure we can open all of them.
1620 * Then we freeze the early part of the array and
1621 * backup to the various spares.
1622 * Then we request changes and start the reshape.
1623 * Monitor progress until it has passed the danger zone.
1624 * and finally invalidate the copied data and unfreeze the
1625 * start of the array.
1626 *
1627 * In each case, we first make sure that storage is available
1628 * for the required backup.
1629 * Then we:
1630 * - request the shape change.
1631 * - for to handle backup etc.
1632 */
1633 nchunk = ochunk = array.chunk_size;
1634 nlayout = olayout = array.layout;
1635 ndisks = odisks = array.raid_disks;
1636
1637 if (chunksize) {
1638 nchunk = chunksize * 1024;
1639 if (size % chunksize) {
1640 fprintf(stderr, Name ": component size %lluK is not"
1641 " a multiple of chunksize %dK\n",
1642 size, chunksize);
1643 break;
1644 }
1645 }
1646 if (layout_str != NULL)
1647 switch(array.level) {
1648 case 4: /* ignore layout */
1649 break;
1650 case 5:
1651 nlayout = map_name(r5layout, layout_str);
1652 if (nlayout == UnSet) {
1653 fprintf(stderr, Name ": layout %s not understood for raid5.\n",
1654 layout_str);
1655 rv = 1;
1656 goto release;
1657 }
1658 break;
1659
1660 case 6:
1661 nlayout = map_name(r6layout, layout_str);
1662 if (nlayout == UnSet) {
1663 fprintf(stderr, Name ": layout %s not understood for raid6.\n",
1664 layout_str);
1665 rv = 1;
1666 goto release;
1667 }
1668 break;
1669 }
1670 if (raid_disks) ndisks = raid_disks;
1671
1672 odata = odisks-1;
1673 ndata = ndisks-1;
1674 if (array.level == 6) {
1675 odata--; /* number of data disks */
1676 ndata--;
1677 }
1678
1679 if (odata == ndata &&
1680 get_linux_version() < 2006032) {
1681 fprintf(stderr, Name ": in-place reshape is not safe before 2.6.32, sorry.\n");
1682 break;
1683 }
1684
1685 /* Check that we can hold all the data */
1686 get_dev_size(fd, NULL, &array_size);
1687 if (ndata * (unsigned long long)size < (array_size/1024)) {
1688 fprintf(stderr, Name ": this change will reduce the size of the array.\n"
1689 " use --grow --array-size first to truncate array.\n"
1690 " e.g. mdadm --grow %s --array-size %llu\n",
1691 devname, ndata * size);
1692 rv = 1;
1693 break;
1694 }
1695
1696 blocks = compute_backup_blocks(nchunk, ochunk, ndata, odata);
1697
1698 sysfs_free(sra);
1699 sra = sysfs_read(fd, 0,
1700 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|
1701 GET_CACHE);
1702
1703 if (!sra) {
1704 fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n",
1705 devname);
1706 rv = 1;
1707 break;
1708 }
1709
1710 if (ndata == odata) {
1711 /* Make 'blocks' bigger for better throughput, but
1712 * not so big that we reject it below.
1713 * Try for 16 megabytes
1714 */
1715 while (blocks * 32 < sra->component_size &&
1716 blocks < 16*1024*2)
1717 blocks *= 2;
1718 } else
1719 fprintf(stderr, Name ": Need to backup %luK of critical "
1720 "section..\n", blocks/2);
1721
1722 if (blocks >= sra->component_size/2) {
1723 fprintf(stderr, Name ": %s: Something wrong - reshape aborted\n",
1724 devname);
1725 rv = 1;
1726 break;
1727 }
1728 nrdisks = array.raid_disks + sra->array.spare_disks;
1729 /* Now we need to open all these devices so we can read/write.
1730 */
1731 fdlist = malloc((1+nrdisks) * sizeof(int));
1732 offsets = malloc((1+nrdisks) * sizeof(offsets[0]));
1733 if (!fdlist || !offsets) {
1734 fprintf(stderr, Name ": malloc failed: grow aborted\n");
1735 rv = 1;
1736 break;
1737 }
1738
1739 d = reshape_prepare_fdlist(devname, sra, array.raid_disks,
1740 nrdisks, blocks, backup_file,
1741 fdlist, offsets);
1742 if (d < 0) {
1743 rv = 1;
1744 goto release;
1745 }
1746 if (backup_file == NULL) {
1747 if (st->ss->external && !st->ss->manage_reshape) {
1748 fprintf(stderr, Name ": %s Grow operation not supported by %s metadata\n",
1749 devname, st->ss->name);
1750 rv = 1;
1751 break;
1752 }
1753 if (ndata <= odata) {
1754 fprintf(stderr, Name ": %s: Cannot grow - need backup-file\n",
1755 devname);
1756 rv = 1;
1757 break;
1758 } else if (sra->array.spare_disks == 0) {
1759 fprintf(stderr, Name ": %s: Cannot grow - need a spare or "
1760 "backup-file to backup critical section\n",
1761 devname);
1762 rv = 1;
1763 break;
1764 }
1765 if (d == array.raid_disks) {
1766 fprintf(stderr, Name ": %s: No spare device for backup\n",
1767 devname);
1768 rv = 1;
1769 break;
1770 }
1771 } else {
1772 if (!reshape_open_backup_file(backup_file, fd, devname,
1773 (signed)blocks,
1774 fdlist+d, offsets+d)) {
1775 rv = 1;
1776 break;
1777 }
1778 d++;
1779 }
1780
1781 /* check that the operation is supported by the metadata */
1782 if (reshape_super(st, -1, level, nlayout, nchunk, ndisks,
1783 backup_file, devname, !quiet)) {
1784 rv = 1;
1785 break;
1786 }
1787
1788 /* lastly, check that the internal stripe cache is
1789 * large enough, or it won't work.
1790 */
1791
1792 cache = (nchunk < ochunk) ? ochunk : nchunk;
1793 cache = cache * 4 / 4096;
1794 if (cache < blocks / 8 / odisks + 16)
1795 /* Make it big enough to hold 'blocks' */
1796 cache = blocks / 8 / odisks + 16;
1797 if (sra->cache_size < cache)
1798 sysfs_set_num(sra, NULL, "stripe_cache_size",
1799 cache+1);
1800 /* Right, everything seems fine. Let's kick things off.
1801 * If only changing raid_disks, use ioctl, else use
1802 * sysfs.
1803 */
1804 sync_metadata(st);
1805 if (ochunk == nchunk && olayout == nlayout) {
1806 array.raid_disks = ndisks;
1807 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
1808 int err = errno;
1809 rv = 1;
1810 fprintf(stderr, Name ": Cannot set device shape for %s: %s\n",
1811 devname, strerror(errno));
1812 if (ndisks < odisks &&
1813 get_linux_version() < 2006030)
1814 fprintf(stderr, Name ": linux 2.6.30 or later required\n");
1815 if (err == EBUSY &&
1816 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
1817 fprintf(stderr, " Bitmap must be removed before shape can be changed\n");
1818
1819 break;
1820 }
1821 } else {
1822 /* set them all just in case some old 'new_*' value
1823 * persists from some earlier problem
1824 */
1825 int err = err; /* only used if rv==1, and always set if
1826 * rv==1, so initialisation not needed,
1827 * despite gcc warning
1828 */
1829 if (sysfs_set_num(sra, NULL, "chunk_size", nchunk) < 0)
1830 rv = 1, err = errno;
1831 if (!rv && sysfs_set_num(sra, NULL, "layout", nlayout) < 0)
1832 rv = 1, err = errno;
1833 if (!rv && sysfs_set_num(sra, NULL, "raid_disks", ndisks) < 0)
1834 rv = 1, err = errno;
1835 if (rv) {
1836 fprintf(stderr, Name ": Cannot set device shape for %s\n",
1837 devname);
1838 if (get_linux_version() < 2006030)
1839 fprintf(stderr, Name ": linux 2.6.30 or later required\n");
1840 if (err == EBUSY &&
1841 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
1842 fprintf(stderr, " Bitmap must be removed before shape can be changed\n");
1843 break;
1844 }
1845 }
1846
1847 if (ndisks == 2 && odisks == 2) {
1848 /* No reshape is needed in this trivial case */
1849 rv = 0;
1850 break;
1851 }
1852
1853 if (st->ss->external) {
1854 /* metadata handler takes it from here */
1855 ping_manager(container);
1856 st->ss->manage_reshape(st, backup_file);
1857 frozen = 0;
1858 break;
1859 }
1860
1861 /* set up the backup-super-block. This requires the
1862 * uuid from the array.
1863 */
1864 /* Find a superblock */
1865 for (sd = sra->devs; sd; sd = sd->next) {
1866 char *dn;
1867 int devfd;
1868 int ok;
1869 if (sd->disk.state & (1<<MD_DISK_FAULTY))
1870 continue;
1871 dn = map_dev(sd->disk.major, sd->disk.minor, 1);
1872 devfd = dev_open(dn, O_RDONLY);
1873 if (devfd < 0)
1874 continue;
1875 ok = st->ss->load_super(st, devfd, NULL);
1876 close(devfd);
1877 if (ok >= 0)
1878 break;
1879 }
1880 if (!sd) {
1881 fprintf(stderr, Name ": %s: Cannot find a superblock\n",
1882 devname);
1883 rv = 1;
1884 break;
1885 }
1886
1887 memset(&bsb, 0, 512);
1888 memcpy(bsb.magic, "md_backup_data-1", 16);
1889 st->ss->uuid_from_super(st, (int*)&bsb.set_uuid);
1890 bsb.mtime = __cpu_to_le64(time(0));
1891 bsb.devstart2 = blocks;
1892 stripes = blocks / (ochunk/512) / odata;
1893 /* Now we just need to kick off the reshape and watch, while
1894 * handling backups of the data...
1895 * This is all done by a forked background process.
1896 */
1897 switch(fork()) {
1898 case 0:
1899 close(fd);
1900 if (check_env("MDADM_GROW_VERIFY"))
1901 fd = open(devname, O_RDONLY | O_DIRECT);
1902 else
1903 fd = -1;
1904 mlockall(MCL_FUTURE);
1905
1906 if (odata < ndata)
1907 done = child_grow(fd, sra, stripes,
1908 fdlist, offsets,
1909 odisks, ochunk, array.level, olayout, odata,
1910 d - odisks, fdlist+odisks, offsets+odisks);
1911 else if (odata > ndata)
1912 done = child_shrink(fd, sra, stripes,
1913 fdlist, offsets,
1914 odisks, ochunk, array.level, olayout, odata,
1915 d - odisks, fdlist+odisks, offsets+odisks);
1916 else
1917 done = child_same_size(fd, sra, stripes,
1918 fdlist, offsets,
1919 0,
1920 odisks, ochunk, array.level, olayout, odata,
1921 d - odisks, fdlist+odisks, offsets+odisks);
1922 if (backup_file && done)
1923 unlink(backup_file);
1924 if (level != UnSet && level != array.level) {
1925 /* We need to wait for the reshape to finish
1926 * (which will have happened unless odata < ndata)
1927 * and then set the level
1928 */
1929
1930 c = map_num(pers, level);
1931 if (c == NULL)
1932 exit(0);/* not possible */
1933
1934 if (odata < ndata)
1935 wait_reshape(sra);
1936 err = sysfs_set_str(sra, NULL, "level", c);
1937 if (err)
1938 fprintf(stderr, Name ": %s: could not set level to %s\n",
1939 devname, c);
1940 }
1941 exit(0);
1942 case -1:
1943 fprintf(stderr, Name ": Cannot run child to monitor reshape: %s\n",
1944 strerror(errno));
1945 rv = 1;
1946 break;
1947 default:
1948 /* The child will take care of unfreezing the array */
1949 frozen = 0;
1950 break;
1951 }
1952 break;
1953
1954 }
1955
1956 release:
1957 if (rv && orig_level != UnSet && sra) {
1958 c = map_num(pers, orig_level);
1959 if (c && sysfs_set_str(sra, NULL, "level", c) == 0)
1960 fprintf(stderr, Name ": aborting level change\n");
1961 }
1962 unfreeze(st, frozen);
1963 return rv;
1964 }
1965
1966 /*
1967 * We run a child process in the background which performs the following
1968 * steps:
1969 * - wait for resync to reach a certain point
1970 * - suspend io to the following section
1971 * - backup that section
1972 * - allow resync to proceed further
1973 * - resume io
1974 * - discard the backup.
1975 *
1976 * When are combined in slightly different ways in the three cases.
1977 * Grow:
1978 * - suspend/backup/allow/wait/resume/discard
1979 * Shrink:
1980 * - allow/wait/suspend/backup/allow/wait/resume/discard
1981 * same-size:
1982 * - wait/resume/discard/suspend/backup/allow
1983 *
1984 * suspend/backup/allow always come together
1985 * wait/resume/discard do too.
1986 * For the same-size case we have two backups to improve flow.
1987 *
1988 */
1989
1990 /* FIXME return status is never checked */
1991 static int grow_backup(struct mdinfo *sra,
1992 unsigned long long offset, /* per device */
1993 unsigned long stripes, /* per device */
1994 int *sources, unsigned long long *offsets,
1995 int disks, int chunk, int level, int layout,
1996 int dests, int *destfd, unsigned long long *destoffsets,
1997 int part, int *degraded,
1998 char *buf)
1999 {
2000 /* Backup 'blocks' sectors at 'offset' on each device of the array,
2001 * to storage 'destfd' (offset 'destoffsets'), after first
2002 * suspending IO. Then allow resync to continue
2003 * over the suspended section.
2004 * Use part 'part' of the backup-super-block.
2005 */
2006 int odata = disks;
2007 int rv = 0;
2008 int i;
2009 unsigned long long ll;
2010 int new_degraded;
2011 //printf("offset %llu\n", offset);
2012 if (level >= 4)
2013 odata--;
2014 if (level == 6)
2015 odata--;
2016 sysfs_set_num(sra, NULL, "suspend_hi", (offset + stripes * (chunk/512)) * odata);
2017 /* Check that array hasn't become degraded, else we might backup the wrong data */
2018 sysfs_get_ll(sra, NULL, "degraded", &ll);
2019 new_degraded = (int)ll;
2020 if (new_degraded != *degraded) {
2021 /* check each device to ensure it is still working */
2022 struct mdinfo *sd;
2023 for (sd = sra->devs ; sd ; sd = sd->next) {
2024 if (sd->disk.state & (1<<MD_DISK_FAULTY))
2025 continue;
2026 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
2027 char sbuf[20];
2028 if (sysfs_get_str(sra, sd, "state", sbuf, 20) < 0 ||
2029 strstr(sbuf, "faulty") ||
2030 strstr(sbuf, "in_sync") == NULL) {
2031 /* this device is dead */
2032 sd->disk.state = (1<<MD_DISK_FAULTY);
2033 if (sd->disk.raid_disk >= 0 &&
2034 sources[sd->disk.raid_disk] >= 0) {
2035 close(sources[sd->disk.raid_disk]);
2036 sources[sd->disk.raid_disk] = -1;
2037 }
2038 }
2039 }
2040 }
2041 *degraded = new_degraded;
2042 }
2043 if (part) {
2044 bsb.arraystart2 = __cpu_to_le64(offset * odata);
2045 bsb.length2 = __cpu_to_le64(stripes * (chunk/512) * odata);
2046 } else {
2047 bsb.arraystart = __cpu_to_le64(offset * odata);
2048 bsb.length = __cpu_to_le64(stripes * (chunk/512) * odata);
2049 }
2050 if (part)
2051 bsb.magic[15] = '2';
2052 for (i = 0; i < dests; i++)
2053 if (part)
2054 lseek64(destfd[i], destoffsets[i] + __le64_to_cpu(bsb.devstart2)*512, 0);
2055 else
2056 lseek64(destfd[i], destoffsets[i], 0);
2057
2058 rv = save_stripes(sources, offsets,
2059 disks, chunk, level, layout,
2060 dests, destfd,
2061 offset*512*odata, stripes * chunk * odata,
2062 buf);
2063
2064 if (rv)
2065 return rv;
2066 bsb.mtime = __cpu_to_le64(time(0));
2067 for (i = 0; i < dests; i++) {
2068 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
2069
2070 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
2071 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
2072 bsb.sb_csum2 = bsb_csum((char*)&bsb,
2073 ((char*)&bsb.sb_csum2)-((char*)&bsb));
2074
2075 rv = -1;
2076 if ((unsigned long long)lseek64(destfd[i], destoffsets[i] - 4096, 0)
2077 != destoffsets[i] - 4096)
2078 break;
2079 if (write(destfd[i], &bsb, 512) != 512)
2080 break;
2081 if (destoffsets[i] > 4096) {
2082 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]+stripes*chunk*odata, 0) !=
2083 destoffsets[i]+stripes*chunk*odata)
2084 break;
2085 if (write(destfd[i], &bsb, 512) != 512)
2086 break;
2087 }
2088 fsync(destfd[i]);
2089 rv = 0;
2090 }
2091
2092 return rv;
2093 }
2094
2095 /* in 2.6.30, the value reported by sync_completed can be
2096 * less that it should be by one stripe.
2097 * This only happens when reshape hits sync_max and pauses.
2098 * So allow wait_backup to either extent sync_max further
2099 * than strictly necessary, or return before the
2100 * sync has got quite as far as we would really like.
2101 * This is what 'blocks2' is for.
2102 * The various caller give appropriate values so that
2103 * every works.
2104 */
2105 /* FIXME return value is often ignored */
2106 static int wait_backup(struct mdinfo *sra,
2107 unsigned long long offset, /* per device */
2108 unsigned long long blocks, /* per device */
2109 unsigned long long blocks2, /* per device - hack */
2110 int dests, int *destfd, unsigned long long *destoffsets,
2111 int part)
2112 {
2113 /* Wait for resync to pass the section that was backed up
2114 * then erase the backup and allow IO
2115 */
2116 int fd = sysfs_get_fd(sra, NULL, "sync_completed");
2117 unsigned long long completed;
2118 int i;
2119 int rv;
2120
2121 if (fd < 0)
2122 return -1;
2123 sysfs_set_num(sra, NULL, "sync_max", offset + blocks + blocks2);
2124 if (offset == 0)
2125 sysfs_set_str(sra, NULL, "sync_action", "reshape");
2126
2127 if (sysfs_fd_get_ll(fd, &completed) < 0) {
2128 close(fd);
2129 return -1;
2130 }
2131 while (completed < offset + blocks) {
2132 char action[20];
2133 fd_set rfds;
2134 FD_ZERO(&rfds);
2135 FD_SET(fd, &rfds);
2136 select(fd+1, NULL, NULL, &rfds, NULL);
2137 if (sysfs_fd_get_ll(fd, &completed) < 0) {
2138 close(fd);
2139 return -1;
2140 }
2141 if (sysfs_get_str(sra, NULL, "sync_action",
2142 action, 20) > 0 &&
2143 strncmp(action, "reshape", 7) != 0)
2144 break;
2145 }
2146 close(fd);
2147
2148 if (part) {
2149 bsb.arraystart2 = __cpu_to_le64(0);
2150 bsb.length2 = __cpu_to_le64(0);
2151 } else {
2152 bsb.arraystart = __cpu_to_le64(0);
2153 bsb.length = __cpu_to_le64(0);
2154 }
2155 bsb.mtime = __cpu_to_le64(time(0));
2156 rv = 0;
2157 for (i = 0; i < dests; i++) {
2158 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
2159 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
2160 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
2161 bsb.sb_csum2 = bsb_csum((char*)&bsb,
2162 ((char*)&bsb.sb_csum2)-((char*)&bsb));
2163 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]-4096, 0) !=
2164 destoffsets[i]-4096)
2165 rv = -1;
2166 if (rv == 0 &&
2167 write(destfd[i], &bsb, 512) != 512)
2168 rv = -1;
2169 fsync(destfd[i]);
2170 }
2171 return rv;
2172 }
2173
2174 static void fail(char *msg)
2175 {
2176 int rv;
2177 rv = (write(2, msg, strlen(msg)) != (int)strlen(msg));
2178 rv |= (write(2, "\n", 1) != 1);
2179 exit(rv ? 1 : 2);
2180 }
2181
2182 static char *abuf, *bbuf;
2183 static unsigned long long abuflen;
2184 static void validate(int afd, int bfd, unsigned long long offset)
2185 {
2186 /* check that the data in the backup against the array.
2187 * This is only used for regression testing and should not
2188 * be used while the array is active
2189 */
2190 if (afd < 0)
2191 return;
2192 lseek64(bfd, offset - 4096, 0);
2193 if (read(bfd, &bsb2, 512) != 512)
2194 fail("cannot read bsb");
2195 if (bsb2.sb_csum != bsb_csum((char*)&bsb2,
2196 ((char*)&bsb2.sb_csum)-((char*)&bsb2)))
2197 fail("first csum bad");
2198 if (memcmp(bsb2.magic, "md_backup_data", 14) != 0)
2199 fail("magic is bad");
2200 if (memcmp(bsb2.magic, "md_backup_data-2", 16) == 0 &&
2201 bsb2.sb_csum2 != bsb_csum((char*)&bsb2,
2202 ((char*)&bsb2.sb_csum2)-((char*)&bsb2)))
2203 fail("second csum bad");
2204
2205 if (__le64_to_cpu(bsb2.devstart)*512 != offset)
2206 fail("devstart is wrong");
2207
2208 if (bsb2.length) {
2209 unsigned long long len = __le64_to_cpu(bsb2.length)*512;
2210
2211 if (abuflen < len) {
2212 free(abuf);
2213 free(bbuf);
2214 abuflen = len;
2215 if (posix_memalign((void**)&abuf, 4096, abuflen) ||
2216 posix_memalign((void**)&bbuf, 4096, abuflen)) {
2217 abuflen = 0;
2218 /* just stop validating on mem-alloc failure */
2219 return;
2220 }
2221 }
2222
2223 lseek64(bfd, offset, 0);
2224 if ((unsigned long long)read(bfd, bbuf, len) != len) {
2225 //printf("len %llu\n", len);
2226 fail("read first backup failed");
2227 }
2228 lseek64(afd, __le64_to_cpu(bsb2.arraystart)*512, 0);
2229 if ((unsigned long long)read(afd, abuf, len) != len)
2230 fail("read first from array failed");
2231 if (memcmp(bbuf, abuf, len) != 0) {
2232 #if 0
2233 int i;
2234 printf("offset=%llu len=%llu\n",
2235 (unsigned long long)__le64_to_cpu(bsb2.arraystart)*512, len);
2236 for (i=0; i<len; i++)
2237 if (bbuf[i] != abuf[i]) {
2238 printf("first diff byte %d\n", i);
2239 break;
2240 }
2241 #endif
2242 fail("data1 compare failed");
2243 }
2244 }
2245 if (bsb2.length2) {
2246 unsigned long long len = __le64_to_cpu(bsb2.length2)*512;
2247
2248 if (abuflen < len) {
2249 free(abuf);
2250 free(bbuf);
2251 abuflen = len;
2252 abuf = malloc(abuflen);
2253 bbuf = malloc(abuflen);
2254 }
2255
2256 lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0);
2257 if ((unsigned long long)read(bfd, bbuf, len) != len)
2258 fail("read second backup failed");
2259 lseek64(afd, __le64_to_cpu(bsb2.arraystart2)*512, 0);
2260 if ((unsigned long long)read(afd, abuf, len) != len)
2261 fail("read second from array failed");
2262 if (memcmp(bbuf, abuf, len) != 0)
2263 fail("data2 compare failed");
2264 }
2265 }
2266
2267 static int child_grow(int afd, struct mdinfo *sra, unsigned long stripes,
2268 int *fds, unsigned long long *offsets,
2269 int disks, int chunk, int level, int layout, int data,
2270 int dests, int *destfd, unsigned long long *destoffsets)
2271 {
2272 char *buf;
2273 int degraded = 0;
2274
2275 if (posix_memalign((void**)&buf, 4096, disks * chunk))
2276 /* Don't start the 'reshape' */
2277 return 0;
2278 sysfs_set_num(sra, NULL, "suspend_hi", 0);
2279 sysfs_set_num(sra, NULL, "suspend_lo", 0);
2280 grow_backup(sra, 0, stripes,
2281 fds, offsets, disks, chunk, level, layout,
2282 dests, destfd, destoffsets,
2283 0, &degraded, buf);
2284 validate(afd, destfd[0], destoffsets[0]);
2285 wait_backup(sra, 0, stripes * (chunk / 512), stripes * (chunk / 512),
2286 dests, destfd, destoffsets,
2287 0);
2288 sysfs_set_num(sra, NULL, "suspend_lo", (stripes * (chunk/512)) * data);
2289 free(buf);
2290 /* FIXME this should probably be numeric */
2291 sysfs_set_str(sra, NULL, "sync_max", "max");
2292 return 1;
2293 }
2294
2295 static int child_shrink(int afd, struct mdinfo *sra, unsigned long stripes,
2296 int *fds, unsigned long long *offsets,
2297 int disks, int chunk, int level, int layout, int data,
2298 int dests, int *destfd, unsigned long long *destoffsets)
2299 {
2300 char *buf;
2301 unsigned long long start;
2302 int rv;
2303 int degraded = 0;
2304
2305 if (posix_memalign((void**)&buf, 4096, disks * chunk))
2306 return 0;
2307 start = sra->component_size - stripes * (chunk/512);
2308 sysfs_set_num(sra, NULL, "sync_max", start);
2309 sysfs_set_str(sra, NULL, "sync_action", "reshape");
2310 sysfs_set_num(sra, NULL, "suspend_lo", 0);
2311 sysfs_set_num(sra, NULL, "suspend_hi", 0);
2312 rv = wait_backup(sra, 0, start - stripes * (chunk/512), stripes * (chunk/512),
2313 dests, destfd, destoffsets, 0);
2314 if (rv < 0)
2315 return 0;
2316 grow_backup(sra, 0, stripes,
2317 fds, offsets,
2318 disks, chunk, level, layout,
2319 dests, destfd, destoffsets,
2320 0, &degraded, buf);
2321 validate(afd, destfd[0], destoffsets[0]);
2322 wait_backup(sra, start, stripes*(chunk/512), 0,
2323 dests, destfd, destoffsets, 0);
2324 sysfs_set_num(sra, NULL, "suspend_lo", (stripes * (chunk/512)) * data);
2325 free(buf);
2326 /* FIXME this should probably be numeric */
2327 sysfs_set_str(sra, NULL, "sync_max", "max");
2328 return 1;
2329 }
2330
2331 static int child_same_size(int afd, struct mdinfo *sra, unsigned long stripes,
2332 int *fds, unsigned long long *offsets,
2333 unsigned long long start,
2334 int disks, int chunk, int level, int layout, int data,
2335 int dests, int *destfd, unsigned long long *destoffsets)
2336 {
2337 unsigned long long size;
2338 unsigned long tailstripes = stripes;
2339 int part;
2340 char *buf;
2341 unsigned long long speed;
2342 int degraded = 0;
2343
2344
2345 if (posix_memalign((void**)&buf, 4096, disks * chunk))
2346 return 0;
2347
2348 sysfs_set_num(sra, NULL, "suspend_lo", 0);
2349 sysfs_set_num(sra, NULL, "suspend_hi", 0);
2350
2351 sysfs_get_ll(sra, NULL, "sync_speed_min", &speed);
2352 sysfs_set_num(sra, NULL, "sync_speed_min", 200000);
2353
2354 grow_backup(sra, start, stripes,
2355 fds, offsets,
2356 disks, chunk, level, layout,
2357 dests, destfd, destoffsets,
2358 0, &degraded, buf);
2359 grow_backup(sra, (start + stripes) * (chunk/512), stripes,
2360 fds, offsets,
2361 disks, chunk, level, layout,
2362 dests, destfd, destoffsets,
2363 1, &degraded, buf);
2364 validate(afd, destfd[0], destoffsets[0]);
2365 part = 0;
2366 start += stripes * 2; /* where to read next */
2367 size = sra->component_size / (chunk/512);
2368 while (start < size) {
2369 if (wait_backup(sra, (start-stripes*2)*(chunk/512),
2370 stripes*(chunk/512), 0,
2371 dests, destfd, destoffsets,
2372 part) < 0)
2373 return 0;
2374 sysfs_set_num(sra, NULL, "suspend_lo", start*(chunk/512) * data);
2375 if (start + stripes > size)
2376 tailstripes = (size - start);
2377
2378 grow_backup(sra, start*(chunk/512), tailstripes,
2379 fds, offsets,
2380 disks, chunk, level, layout,
2381 dests, destfd, destoffsets,
2382 part, &degraded, buf);
2383 start += stripes;
2384 part = 1 - part;
2385 validate(afd, destfd[0], destoffsets[0]);
2386 }
2387 if (wait_backup(sra, (start-stripes*2) * (chunk/512), stripes * (chunk/512), 0,
2388 dests, destfd, destoffsets,
2389 part) < 0)
2390 return 0;
2391 sysfs_set_num(sra, NULL, "suspend_lo", ((start-stripes)*(chunk/512)) * data);
2392 wait_backup(sra, (start-stripes) * (chunk/512), tailstripes * (chunk/512), 0,
2393 dests, destfd, destoffsets,
2394 1-part);
2395 sysfs_set_num(sra, NULL, "suspend_lo", (size*(chunk/512)) * data);
2396 sysfs_set_num(sra, NULL, "sync_speed_min", speed);
2397 free(buf);
2398 return 1;
2399 }
2400
2401 /*
2402 * If any spare contains md_back_data-1 which is recent wrt mtime,
2403 * write that data into the array and update the super blocks with
2404 * the new reshape_progress
2405 */
2406 int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt,
2407 char *backup_file, int verbose)
2408 {
2409 int i, j;
2410 int old_disks;
2411 unsigned long long *offsets;
2412 unsigned long long nstripe, ostripe;
2413 int ndata, odata;
2414
2415 if (info->new_level != info->array.level)
2416 return 1; /* Cannot handle level changes (they are instantaneous) */
2417
2418 odata = info->array.raid_disks - info->delta_disks - 1;
2419 if (info->array.level == 6) odata--; /* number of data disks */
2420 ndata = info->array.raid_disks - 1;
2421 if (info->new_level == 6) ndata--;
2422
2423 old_disks = info->array.raid_disks - info->delta_disks;
2424
2425 if (info->delta_disks <= 0)
2426 /* Didn't grow, so the backup file must have
2427 * been used
2428 */
2429 old_disks = cnt;
2430 for (i=old_disks-(backup_file?1:0); i<cnt; i++) {
2431 struct mdinfo dinfo;
2432 int fd;
2433 int bsbsize;
2434 char *devname, namebuf[20];
2435
2436 /* This was a spare and may have some saved data on it.
2437 * Load the superblock, find and load the
2438 * backup_super_block.
2439 * If either fail, go on to next device.
2440 * If the backup contains no new info, just return
2441 * else restore data and update all superblocks
2442 */
2443 if (i == old_disks-1) {
2444 fd = open(backup_file, O_RDONLY);
2445 if (fd<0) {
2446 fprintf(stderr, Name ": backup file %s inaccessible: %s\n",
2447 backup_file, strerror(errno));
2448 continue;
2449 }
2450 devname = backup_file;
2451 } else {
2452 fd = fdlist[i];
2453 if (fd < 0)
2454 continue;
2455 if (st->ss->load_super(st, fd, NULL))
2456 continue;
2457
2458 st->ss->getinfo_super(st, &dinfo, NULL);
2459 st->ss->free_super(st);
2460
2461 if (lseek64(fd,
2462 (dinfo.data_offset + dinfo.component_size - 8) <<9,
2463 0) < 0) {
2464 fprintf(stderr, Name ": Cannot seek on device %d\n", i);
2465 continue; /* Cannot seek */
2466 }
2467 sprintf(namebuf, "device-%d", i);
2468 devname = namebuf;
2469 }
2470 if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb)) {
2471 if (verbose)
2472 fprintf(stderr, Name ": Cannot read from %s\n", devname);
2473 continue; /* Cannot read */
2474 }
2475 if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0 &&
2476 memcmp(bsb.magic, "md_backup_data-2", 16) != 0) {
2477 if (verbose)
2478 fprintf(stderr, Name ": No backup metadata on %s\n", devname);
2479 continue;
2480 }
2481 if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb))) {
2482 if (verbose)
2483 fprintf(stderr, Name ": Bad backup-metadata checksum on %s\n", devname);
2484 continue; /* bad checksum */
2485 }
2486 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0 &&
2487 bsb.sb_csum2 != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum2)-((char*)&bsb))) {
2488 if (verbose)
2489 fprintf(stderr, Name ": Bad backup-metadata checksum2 on %s\n", devname);
2490 continue; /* Bad second checksum */
2491 }
2492 if (memcmp(bsb.set_uuid,info->uuid, 16) != 0) {
2493 if (verbose)
2494 fprintf(stderr, Name ": Wrong uuid on backup-metadata on %s\n", devname);
2495 continue; /* Wrong uuid */
2496 }
2497
2498 /* array utime and backup-mtime should be updated at much the same time, but it seems that
2499 * sometimes they aren't... So allow considerable flexability in matching, and allow
2500 * this test to be overridden by an environment variable.
2501 */
2502 if (info->array.utime > (int)__le64_to_cpu(bsb.mtime) + 2*60*60 ||
2503 info->array.utime < (int)__le64_to_cpu(bsb.mtime) - 10*60) {
2504 if (check_env("MDADM_GROW_ALLOW_OLD")) {
2505 fprintf(stderr, Name ": accepting backup with timestamp %lu "
2506 "for array with timestamp %lu\n",
2507 (unsigned long)__le64_to_cpu(bsb.mtime),
2508 (unsigned long)info->array.utime);
2509 } else {
2510 if (verbose)
2511 fprintf(stderr, Name ": too-old timestamp on "
2512 "backup-metadata on %s\n", devname);
2513 continue; /* time stamp is too bad */
2514 }
2515 }
2516
2517 if (bsb.magic[15] == '1') {
2518 if (info->delta_disks >= 0) {
2519 /* reshape_progress is increasing */
2520 if (__le64_to_cpu(bsb.arraystart) + __le64_to_cpu(bsb.length) <
2521 info->reshape_progress) {
2522 nonew:
2523 if (verbose)
2524 fprintf(stderr, Name ": backup-metadata found on %s but is not needed\n", devname);
2525 continue; /* No new data here */
2526 }
2527 } else {
2528 /* reshape_progress is decreasing */
2529 if (__le64_to_cpu(bsb.arraystart) >=
2530 info->reshape_progress)
2531 goto nonew; /* No new data here */
2532 }
2533 } else {
2534 if (info->delta_disks >= 0) {
2535 /* reshape_progress is increasing */
2536 if (__le64_to_cpu(bsb.arraystart) + __le64_to_cpu(bsb.length) <
2537 info->reshape_progress &&
2538 __le64_to_cpu(bsb.arraystart2) + __le64_to_cpu(bsb.length2) <
2539 info->reshape_progress)
2540 goto nonew; /* No new data here */
2541 } else {
2542 /* reshape_progress is decreasing */
2543 if (__le64_to_cpu(bsb.arraystart) >=
2544 info->reshape_progress &&
2545 __le64_to_cpu(bsb.arraystart2) >=
2546 info->reshape_progress)
2547 goto nonew; /* No new data here */
2548 }
2549 }
2550 if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0) {
2551 second_fail:
2552 if (verbose)
2553 fprintf(stderr, Name ": Failed to verify secondary backup-metadata block on %s\n",
2554 devname);
2555 continue; /* Cannot seek */
2556 }
2557 /* There should be a duplicate backup superblock 4k before here */
2558 if (lseek64(fd, -4096, 1) < 0 ||
2559 read(fd, &bsb2, sizeof(bsb2)) != sizeof(bsb2))
2560 goto second_fail; /* Cannot find leading superblock */
2561 if (bsb.magic[15] == '1')
2562 bsbsize = offsetof(struct mdp_backup_super, pad1);
2563 else
2564 bsbsize = offsetof(struct mdp_backup_super, pad);
2565 if (memcmp(&bsb2, &bsb, bsbsize) != 0)
2566 goto second_fail; /* Cannot find leading superblock */
2567
2568 /* Now need the data offsets for all devices. */
2569 offsets = malloc(sizeof(*offsets)*info->array.raid_disks);
2570 for(j=0; j<info->array.raid_disks; j++) {
2571 if (fdlist[j] < 0)
2572 continue;
2573 if (st->ss->load_super(st, fdlist[j], NULL))
2574 /* FIXME should be this be an error */
2575 continue;
2576 st->ss->getinfo_super(st, &dinfo, NULL);
2577 st->ss->free_super(st);
2578 offsets[j] = dinfo.data_offset * 512;
2579 }
2580 printf(Name ": restoring critical section\n");
2581
2582 if (restore_stripes(fdlist, offsets,
2583 info->array.raid_disks,
2584 info->new_chunk,
2585 info->new_level,
2586 info->new_layout,
2587 fd, __le64_to_cpu(bsb.devstart)*512,
2588 __le64_to_cpu(bsb.arraystart)*512,
2589 __le64_to_cpu(bsb.length)*512)) {
2590 /* didn't succeed, so giveup */
2591 if (verbose)
2592 fprintf(stderr, Name ": Error restoring backup from %s\n",
2593 devname);
2594 return 1;
2595 }
2596
2597 if (bsb.magic[15] == '2' &&
2598 restore_stripes(fdlist, offsets,
2599 info->array.raid_disks,
2600 info->new_chunk,
2601 info->new_level,
2602 info->new_layout,
2603 fd, __le64_to_cpu(bsb.devstart)*512 +
2604 __le64_to_cpu(bsb.devstart2)*512,
2605 __le64_to_cpu(bsb.arraystart2)*512,
2606 __le64_to_cpu(bsb.length2)*512)) {
2607 /* didn't succeed, so giveup */
2608 if (verbose)
2609 fprintf(stderr, Name ": Error restoring second backup from %s\n",
2610 devname);
2611 return 1;
2612 }
2613
2614
2615 /* Ok, so the data is restored. Let's update those superblocks. */
2616
2617 if (info->delta_disks >= 0) {
2618 info->reshape_progress = __le64_to_cpu(bsb.arraystart) +
2619 __le64_to_cpu(bsb.length);
2620 if (bsb.magic[15] == '2') {
2621 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2) +
2622 __le64_to_cpu(bsb.length2);
2623 if (p2 > info->reshape_progress)
2624 info->reshape_progress = p2;
2625 }
2626 } else {
2627 info->reshape_progress = __le64_to_cpu(bsb.arraystart);
2628 if (bsb.magic[15] == '2') {
2629 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2);
2630 if (p2 < info->reshape_progress)
2631 info->reshape_progress = p2;
2632 }
2633 }
2634 for (j=0; j<info->array.raid_disks; j++) {
2635 if (fdlist[j] < 0) continue;
2636 if (st->ss->load_super(st, fdlist[j], NULL))
2637 continue;
2638 st->ss->getinfo_super(st, &dinfo, NULL);
2639 dinfo.reshape_progress = info->reshape_progress;
2640 st->ss->update_super(st, &dinfo,
2641 "_reshape_progress",
2642 NULL,0, 0, NULL);
2643 st->ss->store_super(st, fdlist[j]);
2644 st->ss->free_super(st);
2645 }
2646 return 0;
2647 }
2648 /* Didn't find any backup data, try to see if any
2649 * was needed.
2650 */
2651 if (info->delta_disks < 0) {
2652 /* When shrinking, the critical section is at the end.
2653 * So see if we are before the critical section.
2654 */
2655 unsigned long long first_block;
2656 nstripe = ostripe = 0;
2657 first_block = 0;
2658 while (ostripe >= nstripe) {
2659 ostripe += info->array.chunk_size / 512;
2660 first_block = ostripe * odata;
2661 nstripe = first_block / ndata / (info->new_chunk/512) *
2662 (info->new_chunk/512);
2663 }
2664
2665 if (info->reshape_progress >= first_block)
2666 return 0;
2667 }
2668 if (info->delta_disks > 0) {
2669 /* See if we are beyond the critical section. */
2670 unsigned long long last_block;
2671 nstripe = ostripe = 0;
2672 last_block = 0;
2673 while (nstripe >= ostripe) {
2674 nstripe += info->new_chunk / 512;
2675 last_block = nstripe * ndata;
2676 ostripe = last_block / odata / (info->array.chunk_size/512) *
2677 (info->array.chunk_size/512);
2678 }
2679
2680 if (info->reshape_progress >= last_block)
2681 return 0;
2682 }
2683 /* needed to recover critical section! */
2684 if (verbose)
2685 fprintf(stderr, Name ": Failed to find backup of critical section\n");
2686 return 1;
2687 }
2688
2689 int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
2690 char *backup_file)
2691 {
2692 /* Array is assembled and ready to be started, but
2693 * monitoring is probably required.
2694 * So:
2695 * - start read-only
2696 * - set upper bound for resync
2697 * - initialise the 'suspend' boundaries
2698 * - switch to read-write
2699 * - fork and continue monitoring
2700 */
2701 int err;
2702 int backup_list[1];
2703 unsigned long long backup_offsets[1];
2704 int odisks, ndisks, ochunk, nchunk,odata,ndata;
2705 unsigned long a,b,blocks,stripes;
2706 int backup_fd;
2707 int *fds;
2708 unsigned long long *offsets;
2709 int d;
2710 struct mdinfo *sra, *sd;
2711 int rv;
2712 unsigned long cache;
2713 int done = 0;
2714
2715 err = sysfs_set_str(info, NULL, "array_state", "readonly");
2716 if (err)
2717 return err;
2718
2719 /* make sure reshape doesn't progress until we are ready */
2720 sysfs_set_str(info, NULL, "sync_max", "0");
2721 sysfs_set_str(info, NULL, "array_state", "active"); /* FIXME or clean */
2722
2723 sra = sysfs_read(-1, devname2devnum(info->sys_name),
2724 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|
2725 GET_CACHE);
2726 if (!sra)
2727 return 1;
2728
2729 /* ndisks is not growing, so raid_disks is old and +delta is new */
2730 odisks = info->array.raid_disks;
2731 ndisks = odisks + info->delta_disks;
2732 odata = odisks - 1;
2733 ndata = ndisks - 1;
2734 if (info->array.level == 6) {
2735 odata--;
2736 ndata--;
2737 }
2738 ochunk = info->array.chunk_size;
2739 nchunk = info->new_chunk;
2740
2741 a = (ochunk/512) * odata;
2742 b = (nchunk/512) * ndata;
2743 /* Find GCD */
2744 while (a != b) {
2745 if (a < b)
2746 b -= a;
2747 if (b < a)
2748 a -= b;
2749 }
2750 /* LCM == product / GCD */
2751 blocks = (ochunk/512) * (nchunk/512) * odata * ndata / a;
2752
2753 if (ndata == odata)
2754 while (blocks * 32 < sra->component_size &&
2755 blocks < 16*1024*2)
2756 blocks *= 2;
2757 stripes = blocks / (info->array.chunk_size/512) / odata;
2758
2759 /* check that the internal stripe cache is
2760 * large enough, or it won't work.
2761 */
2762 cache = (nchunk < ochunk) ? ochunk : nchunk;
2763 cache = cache * 4 / 4096;
2764 if (cache < blocks / 8 / odisks + 16)
2765 /* Make it big enough to hold 'blocks' */
2766 cache = blocks / 8 / odisks + 16;
2767 if (sra->cache_size < cache)
2768 sysfs_set_num(sra, NULL, "stripe_cache_size",
2769 cache+1);
2770
2771 memset(&bsb, 0, 512);
2772 memcpy(bsb.magic, "md_backup_data-1", 16);
2773 memcpy(&bsb.set_uuid, info->uuid, 16);
2774 bsb.mtime = __cpu_to_le64(time(0));
2775 bsb.devstart2 = blocks;
2776
2777 backup_fd = open(backup_file, O_RDWR|O_CREAT, S_IRUSR | S_IWUSR);
2778 if (backup_fd < 0) {
2779 fprintf(stderr, Name ": Cannot open backup file %s\n",
2780 backup_file ?: "- no backup-file given");
2781 return 1;
2782 }
2783 backup_list[0] = backup_fd;
2784 backup_offsets[0] = 8 * 512;
2785 fds = malloc(odisks * sizeof(fds[0]));
2786 offsets = malloc(odisks * sizeof(offsets[0]));
2787 for (d=0; d<odisks; d++)
2788 fds[d] = -1;
2789
2790 for (sd = sra->devs; sd; sd = sd->next) {
2791 if (sd->disk.state & (1<<MD_DISK_FAULTY))
2792 continue;
2793 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
2794 char *dn = map_dev(sd->disk.major,
2795 sd->disk.minor, 1);
2796 fds[sd->disk.raid_disk]
2797 = dev_open(dn, O_RDONLY);
2798 offsets[sd->disk.raid_disk] = sd->data_offset*512;
2799 if (fds[sd->disk.raid_disk] < 0) {
2800 fprintf(stderr, Name ": %s: cannot open component %s\n",
2801 info->sys_name, dn?dn:"-unknown-");
2802 rv = 1;
2803 goto release;
2804 }
2805 free(dn);
2806 }
2807 }
2808
2809 switch(fork()) {
2810 case 0:
2811 close(mdfd);
2812 mlockall(MCL_FUTURE);
2813 if (info->delta_disks < 0)
2814 done = child_shrink(-1, info, stripes,
2815 fds, offsets,
2816 info->array.raid_disks,
2817 info->array.chunk_size,
2818 info->array.level, info->array.layout,
2819 odata,
2820 1, backup_list, backup_offsets);
2821 else if (info->delta_disks == 0) {
2822 /* The 'start' is a per-device stripe number.
2823 * reshape_progress is a per-array sector number.
2824 * So divide by ndata * chunk_size
2825 */
2826 unsigned long long start = info->reshape_progress / ndata;
2827 start /= (info->array.chunk_size/512);
2828 done = child_same_size(-1, info, stripes,
2829 fds, offsets,
2830 start,
2831 info->array.raid_disks,
2832 info->array.chunk_size,
2833 info->array.level, info->array.layout,
2834 odata,
2835 1, backup_list, backup_offsets);
2836 }
2837 if (backup_file && done)
2838 unlink(backup_file);
2839 /* FIXME should I intuit a level change */
2840 exit(0);
2841 case -1:
2842 fprintf(stderr, Name ": Cannot run child to continue monitoring reshape: %s\n",
2843 strerror(errno));
2844 return 1;
2845 default:
2846 break;
2847 }
2848 release:
2849 return 0;
2850 }
2851
2852