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