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