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