]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Grow.c
Merge branch 'master' into devel-3.1
[thirdparty/mdadm.git] / Grow.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
22 * Email: <neilb@suse.de>
23 */
24 #include "mdadm.h"
25 #include "dlink.h"
26 #include <sys/mman.h>
27
28 #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
29 #error no endian defined
30 #endif
31 #include "md_u.h"
32 #include "md_p.h"
33
34 #ifndef offsetof
35 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
36 #endif
37
38 int Grow_Add_device(char *devname, int fd, char *newdev)
39 {
40 /* Add a device to an active array.
41 * Currently, just extend a linear array.
42 * This requires writing a new superblock on the
43 * new device, calling the kernel to add the device,
44 * and if that succeeds, update the superblock on
45 * all other devices.
46 * This means that we need to *find* all other devices.
47 */
48 struct mdinfo info;
49
50 struct stat stb;
51 int nfd, fd2;
52 int d, nd;
53 struct supertype *st = NULL;
54
55
56 if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
57 fprintf(stderr, Name ": cannot get array info for %s\n", devname);
58 return 1;
59 }
60
61 st = super_by_fd(fd);
62 if (!st) {
63 fprintf(stderr, Name ": cannot handle arrays with superblock version %d\n", info.array.major_version);
64 return 1;
65 }
66
67 if (info.array.level != -1) {
68 fprintf(stderr, Name ": can only add devices to linear arrays\n");
69 return 1;
70 }
71
72 nfd = open(newdev, O_RDWR|O_EXCL|O_DIRECT);
73 if (nfd < 0) {
74 fprintf(stderr, Name ": cannot open %s\n", newdev);
75 return 1;
76 }
77 fstat(nfd, &stb);
78 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
79 fprintf(stderr, Name ": %s is not a block device!\n", newdev);
80 close(nfd);
81 return 1;
82 }
83 /* now check out all the devices and make sure we can read the superblock */
84 for (d=0 ; d < info.array.raid_disks ; d++) {
85 mdu_disk_info_t disk;
86 char *dv;
87
88 disk.number = d;
89 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
90 fprintf(stderr, Name ": cannot get device detail for device %d\n",
91 d);
92 return 1;
93 }
94 dv = map_dev(disk.major, disk.minor, 1);
95 if (!dv) {
96 fprintf(stderr, Name ": cannot find device file for device %d\n",
97 d);
98 return 1;
99 }
100 fd2 = dev_open(dv, O_RDWR);
101 if (!fd2) {
102 fprintf(stderr, Name ": cannot open device file %s\n", dv);
103 return 1;
104 }
105 st->ss->free_super(st);
106
107 if (st->ss->load_super(st, fd2, NULL)) {
108 fprintf(stderr, Name ": cannot find super block on %s\n", dv);
109 close(fd2);
110 return 1;
111 }
112 close(fd2);
113 }
114 /* Ok, looks good. Lets update the superblock and write it out to
115 * newdev.
116 */
117
118 info.disk.number = d;
119 info.disk.major = major(stb.st_rdev);
120 info.disk.minor = minor(stb.st_rdev);
121 info.disk.raid_disk = d;
122 info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE);
123 st->ss->update_super(st, &info, "linear-grow-new", newdev,
124 0, 0, NULL);
125
126 if (st->ss->store_super(st, nfd)) {
127 fprintf(stderr, Name ": Cannot store new superblock on %s\n",
128 newdev);
129 close(nfd);
130 return 1;
131 }
132 close(nfd);
133
134 if (ioctl(fd, ADD_NEW_DISK, &info.disk) != 0) {
135 fprintf(stderr, Name ": Cannot add new disk to this array\n");
136 return 1;
137 }
138 /* Well, that seems to have worked.
139 * Now go through and update all superblocks
140 */
141
142 if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
143 fprintf(stderr, Name ": cannot get array info for %s\n", devname);
144 return 1;
145 }
146
147 nd = d;
148 for (d=0 ; d < info.array.raid_disks ; d++) {
149 mdu_disk_info_t disk;
150 char *dv;
151
152 disk.number = d;
153 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
154 fprintf(stderr, Name ": cannot get device detail for device %d\n",
155 d);
156 return 1;
157 }
158 dv = map_dev(disk.major, disk.minor, 1);
159 if (!dv) {
160 fprintf(stderr, Name ": cannot find device file for device %d\n",
161 d);
162 return 1;
163 }
164 fd2 = dev_open(dv, O_RDWR);
165 if (fd2 < 0) {
166 fprintf(stderr, Name ": cannot open device file %s\n", dv);
167 return 1;
168 }
169 if (st->ss->load_super(st, fd2, NULL)) {
170 fprintf(stderr, Name ": cannot find super block on %s\n", dv);
171 close(fd);
172 return 1;
173 }
174 info.array.raid_disks = nd+1;
175 info.array.nr_disks = nd+1;
176 info.array.active_disks = nd+1;
177 info.array.working_disks = nd+1;
178
179 st->ss->update_super(st, &info, "linear-grow-update", dv,
180 0, 0, NULL);
181
182 if (st->ss->store_super(st, fd2)) {
183 fprintf(stderr, Name ": Cannot store new superblock on %s\n", dv);
184 close(fd2);
185 return 1;
186 }
187 close(fd2);
188 }
189
190 return 0;
191 }
192
193 int Grow_addbitmap(char *devname, int fd, char *file, int chunk, int delay, int write_behind, int force)
194 {
195 /*
196 * First check that array doesn't have a bitmap
197 * Then create the bitmap
198 * Then add it
199 *
200 * For internal bitmaps, we need to check the version,
201 * find all the active devices, and write the bitmap block
202 * to all devices
203 */
204 mdu_bitmap_file_t bmf;
205 mdu_array_info_t array;
206 struct supertype *st;
207 int major = BITMAP_MAJOR_HI;
208 int vers = md_get_version(fd);
209 unsigned long long bitmapsize, array_size;
210
211 if (vers < 9003) {
212 major = BITMAP_MAJOR_HOSTENDIAN;
213 #ifdef __BIG_ENDIAN
214 fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
215 " between different architectured. Consider upgrading the Linux kernel.\n");
216 #endif
217 }
218
219 if (ioctl(fd, GET_BITMAP_FILE, &bmf) != 0) {
220 if (errno == ENOMEM)
221 fprintf(stderr, Name ": Memory allocation failure.\n");
222 else
223 fprintf(stderr, Name ": bitmaps not supported by this kernel.\n");
224 return 1;
225 }
226 if (bmf.pathname[0]) {
227 if (strcmp(file,"none")==0) {
228 if (ioctl(fd, SET_BITMAP_FILE, -1)!= 0) {
229 fprintf(stderr, Name ": failed to remove bitmap %s\n",
230 bmf.pathname);
231 return 1;
232 }
233 return 0;
234 }
235 fprintf(stderr, Name ": %s already has a bitmap (%s)\n",
236 devname, bmf.pathname);
237 return 1;
238 }
239 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
240 fprintf(stderr, Name ": cannot get array status for %s\n", devname);
241 return 1;
242 }
243 if (array.state & (1<<MD_SB_BITMAP_PRESENT)) {
244 if (strcmp(file, "none")==0) {
245 array.state &= ~(1<<MD_SB_BITMAP_PRESENT);
246 if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
247 fprintf(stderr, Name ": failed to remove internal bitmap.\n");
248 return 1;
249 }
250 return 0;
251 }
252 fprintf(stderr, Name ": Internal bitmap already present on %s\n",
253 devname);
254 return 1;
255 }
256 if (array.level <= 0) {
257 fprintf(stderr, Name ": Bitmaps not meaningful with level %s\n",
258 map_num(pers, array.level)?:"of this array");
259 return 1;
260 }
261 bitmapsize = array.size;
262 bitmapsize <<= 1;
263 if (get_dev_size(fd, NULL, &array_size) &&
264 array_size > (0x7fffffffULL<<9)) {
265 /* Array is big enough that we cannot trust array.size
266 * try other approaches
267 */
268 bitmapsize = get_component_size(fd);
269 }
270 if (bitmapsize == 0) {
271 fprintf(stderr, Name ": Cannot reliably determine size of array to create bitmap - sorry.\n");
272 return 1;
273 }
274
275 if (array.level == 10) {
276 int ncopies = (array.layout&255)*((array.layout>>8)&255);
277 bitmapsize = bitmapsize * array.raid_disks / ncopies;
278 }
279
280 st = super_by_fd(fd);
281 if (!st) {
282 fprintf(stderr, Name ": Cannot understand version %d.%d\n",
283 array.major_version, array.minor_version);
284 return 1;
285 }
286 if (strcmp(file, "none") == 0) {
287 fprintf(stderr, Name ": no bitmap found on %s\n", devname);
288 return 1;
289 } else if (strcmp(file, "internal") == 0) {
290 int d;
291 for (d=0; d< st->max_devs; d++) {
292 mdu_disk_info_t disk;
293 char *dv;
294 disk.number = d;
295 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
296 continue;
297 if (disk.major == 0 &&
298 disk.minor == 0)
299 continue;
300 if ((disk.state & (1<<MD_DISK_SYNC))==0)
301 continue;
302 dv = map_dev(disk.major, disk.minor, 1);
303 if (dv) {
304 int fd2 = dev_open(dv, O_RDWR);
305 if (fd2 < 0)
306 continue;
307 if (st->ss->load_super(st, fd2, NULL)==0) {
308 if (st->ss->add_internal_bitmap(
309 st,
310 &chunk, delay, write_behind,
311 bitmapsize, 0, major)
312 )
313 st->ss->write_bitmap(st, fd2);
314 else {
315 fprintf(stderr, Name ": failed to create internal bitmap - chunksize problem.\n");
316 close(fd2);
317 return 1;
318 }
319 }
320 close(fd2);
321 }
322 }
323 array.state |= (1<<MD_SB_BITMAP_PRESENT);
324 if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
325 fprintf(stderr, Name ": failed to set internal bitmap.\n");
326 return 1;
327 }
328 } else {
329 int uuid[4];
330 int bitmap_fd;
331 int d;
332 int max_devs = st->max_devs;
333
334 /* try to load a superblock */
335 for (d=0; d<max_devs; d++) {
336 mdu_disk_info_t disk;
337 char *dv;
338 int fd2;
339 disk.number = d;
340 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
341 continue;
342 if ((disk.major==0 && disk.minor==0) ||
343 (disk.state & (1<<MD_DISK_REMOVED)))
344 continue;
345 dv = map_dev(disk.major, disk.minor, 1);
346 if (!dv) continue;
347 fd2 = dev_open(dv, O_RDONLY);
348 if (fd2 >= 0 &&
349 st->ss->load_super(st, fd2, NULL) == 0) {
350 close(fd2);
351 st->ss->uuid_from_super(st, uuid);
352 break;
353 }
354 close(fd2);
355 }
356 if (d == max_devs) {
357 fprintf(stderr, Name ": cannot find UUID for array!\n");
358 return 1;
359 }
360 if (CreateBitmap(file, force, (char*)uuid, chunk,
361 delay, write_behind, bitmapsize, major)) {
362 return 1;
363 }
364 bitmap_fd = open(file, O_RDWR);
365 if (bitmap_fd < 0) {
366 fprintf(stderr, Name ": weird: %s cannot be opened\n",
367 file);
368 return 1;
369 }
370 if (ioctl(fd, SET_BITMAP_FILE, bitmap_fd) < 0) {
371 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
372 devname, strerror(errno));
373 return 1;
374 }
375 }
376
377 return 0;
378 }
379
380
381 /*
382 * When reshaping an array we might need to backup some data.
383 * This is written to all spares with a 'super_block' describing it.
384 * The superblock goes 1K form the end of the used space on the
385 * device.
386 * It if written after the backup is complete.
387 * It has the following structure.
388 */
389
390 struct mdp_backup_super {
391 char magic[16]; /* md_backup_data-1 or -2 */
392 __u8 set_uuid[16];
393 __u64 mtime;
394 /* start/sizes in 512byte sectors */
395 __u64 devstart; /* address on backup device/file of data */
396 __u64 arraystart;
397 __u64 length;
398 __u32 sb_csum; /* csum of preceeding bytes. */
399 __u32 pad1;
400 __u64 devstart2; /* offset in to data of second section */
401 __u64 arraystart2;
402 __u64 length2;
403 __u32 sb_csum2; /* csum of preceeding bytes. */
404 __u8 pad[512-68-32];
405 } __attribute__((aligned(512))) bsb;
406
407 int bsb_csum(char *buf, int len)
408 {
409 int i;
410 int csum = 0;
411 for (i=0; i<len; i++)
412 csum = (csum<<3) + buf[0];
413 return __cpu_to_le32(csum);
414 }
415
416 static int child_grow(int afd, struct mdinfo *sra, unsigned long blocks,
417 int *fds, unsigned long long *offsets,
418 int disks, int chunk, int level, int layout, int data,
419 int dests, int *destfd, unsigned long long *destoffsets);
420 static int child_shrink(int afd, struct mdinfo *sra, unsigned long blocks,
421 int *fds, unsigned long long *offsets,
422 int disks, int chunk, int level, int layout, int data,
423 int dests, int *destfd, unsigned long long *destoffsets);
424 static int child_same_size(int afd, struct mdinfo *sra, unsigned long blocks,
425 int *fds, unsigned long long *offsets,
426 unsigned long long start,
427 int disks, int chunk, int level, int layout, int data,
428 int dests, int *destfd, unsigned long long *destoffsets);
429
430 int freeze_array(struct mdinfo *sra)
431 {
432 /* Try to freeze resync on this array.
433 * Return -1 if the array is busy,
434 * return 0 if this kernel doesn't support 'frozen'
435 * return 1 if it worked.
436 */
437 char buf[20];
438 if (sysfs_get_str(sra, NULL, "sync_action", buf, 20) <= 0)
439 return 0;
440 if (strcmp(buf, "idle\n") != 0 &&
441 strcmp(buf, "frozen\n") != 0)
442 return -1;
443 if (sysfs_set_str(sra, NULL, "sync_action", "frozen") < 0)
444 return 0;
445 return 1;
446 }
447
448 void unfreeze_array(struct mdinfo *sra, int frozen)
449 {
450 /* If 'frozen' is 1, unfreeze the array */
451 if (frozen > 0)
452 sysfs_set_str(sra, NULL, "sync_action", "idle");
453 }
454
455 void wait_reshape(struct mdinfo *sra)
456 {
457 int fd = sysfs_get_fd(sra, NULL, "sync_action");
458 char action[20];
459
460 do {
461 fd_set rfds;
462 FD_ZERO(&rfds);
463 FD_SET(fd, &rfds);
464 select(fd+1, NULL, NULL, &rfds, NULL);
465
466 if (sysfs_fd_get_str(fd, action, 20) < 0) {
467 close(fd);
468 return;
469 }
470 } while (strncmp(action, "reshape", 7) == 0);
471 }
472
473
474 int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
475 long long size,
476 int level, char *layout_str, int chunksize, int raid_disks)
477 {
478 /* Make some changes in the shape of an array.
479 * The kernel must support the change.
480 *
481 * There are three different changes. Each can trigger
482 * a resync or recovery so we freeze that until we have
483 * requested everything (if kernel supports freezing - 2.6.30).
484 * The steps are:
485 * - change size (i.e. component_size)
486 * - change level
487 * - change layout/chunksize/ndisks
488 *
489 * The last can require a reshape. It is different on different
490 * levels so we need to check the level before actioning it.
491 * Some times the level change needs to be requested after the
492 * reshape (e.g. raid6->raid5, raid5->raid0)
493 *
494 */
495 struct mdu_array_info_s array, orig;
496 char *c;
497 int rv = 0;
498 struct supertype *st;
499
500 int nchunk, ochunk;
501 int nlayout, olayout;
502 int ndisks, odisks;
503 int ndata, odata;
504 int orig_level = UnSet;
505 char alt_layout[40];
506 int *fdlist;
507 unsigned long long *offsets;
508 int d, i;
509 int nrdisks;
510 int err;
511 int frozen;
512 unsigned long a,b, blocks, stripes;
513 int cache;
514 unsigned long long array_size;
515 int changed = 0;
516 int done;
517
518 struct mdinfo *sra;
519 struct mdinfo *sd;
520
521 if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) {
522 fprintf(stderr, Name ": %s is not an active md array - aborting\n",
523 devname);
524 return 1;
525 }
526 sra = sysfs_read(fd, 0, GET_LEVEL);
527 frozen = freeze_array(sra);
528 if (frozen < 0) {
529 fprintf(stderr, Name ": %s is performing resync/recovery and cannot"
530 " be reshaped\n", devname);
531 return 1;
532 }
533
534 /* ========= set size =============== */
535 if (size >= 0 && (size == 0 || size != array.size)) {
536 array.size = size;
537 if (array.size != size) {
538 /* got truncated to 32bit, write to
539 * component_size instead
540 */
541 if (sra)
542 rv = sysfs_set_num(sra, NULL,
543 "component_size", size);
544 else
545 rv = -1;
546 } else
547 rv = ioctl(fd, SET_ARRAY_INFO, &array);
548 if (rv != 0) {
549 fprintf(stderr, Name ": Cannot set device size for %s: %s\n",
550 devname, strerror(errno));
551 rv = 1;
552 goto release;
553 }
554 ioctl(fd, GET_ARRAY_INFO, &array);
555 if (!quiet)
556 fprintf(stderr, Name ": component size of %s has been set to %dK\n",
557 devname, array.size);
558 changed = 1;
559 }
560
561 /* ======= set level =========== */
562 if (level != UnSet && level != array.level) {
563 /* Trying to change the level.
564 * We might need to change layout first and schedule a
565 * level change for later.
566 * Level changes that can happen immediately are:
567 * 0->4,5,6 1->5 4->5,6 5->1,6
568 * Level changes that need a layout change first are:
569 * 6->5,4,0 : need a -6 layout, or parity-last
570 * 5->4,0 : need parity-last
571 */
572 if ((array.level == 6 || array.level == 5) &&
573 (level == 5 || level == 4 || level == 0)) {
574 /* Don't change level yet, but choose intermediate
575 * layout
576 */
577 if (level == 5) {
578 if (layout_str == NULL)
579 switch (array.layout) {
580 case ALGORITHM_LEFT_ASYMMETRIC:
581 case ALGORITHM_LEFT_ASYMMETRIC_6:
582 case ALGORITHM_ROTATING_N_RESTART:
583 layout_str = "left-asymmetric-6";
584 break;
585 case ALGORITHM_LEFT_SYMMETRIC:
586 case ALGORITHM_LEFT_SYMMETRIC_6:
587 case ALGORITHM_ROTATING_N_CONTINUE:
588 layout_str = "left-symmetric-6";
589 break;
590 case ALGORITHM_RIGHT_ASYMMETRIC:
591 case ALGORITHM_RIGHT_ASYMMETRIC_6:
592 case ALGORITHM_ROTATING_ZERO_RESTART:
593 layout_str = "right-asymmetric-6";
594 break;
595 case ALGORITHM_RIGHT_SYMMETRIC:
596 case ALGORITHM_RIGHT_SYMMETRIC_6:
597 layout_str = "right-symmetric-6";
598 break;
599 case ALGORITHM_PARITY_0:
600 case ALGORITHM_PARITY_0_6:
601 layout_str = "parity-first-6";
602 break;
603 case ALGORITHM_PARITY_N:
604 layout_str = "parity-last";
605 break;
606 default:
607 fprintf(stderr, Name ": %s: cannot"
608 "convert layout to RAID5 equivalent\n",
609 devname);
610 rv = 1;
611 goto release;
612 }
613 else {
614 int l = map_name(r5layout, layout_str);
615 if (l == UnSet) {
616 fprintf(stderr, Name ": %s: layout '%s' not recognised\n",
617 devname, layout_str);
618 rv = 1;
619 goto release;
620 }
621 if (l != ALGORITHM_PARITY_N) {
622 /* need the -6 version */
623 char *ls = map_num(r5layout, l);
624 strcat(strcpy(alt_layout, ls),
625 "-6");
626 layout_str = alt_layout;
627 }
628 }
629 if (raid_disks)
630 /* The find raid6->raid5 conversion
631 * will reduce the number of disks,
632 * so now we need to aim higher
633 */
634 raid_disks++;
635 } else
636 layout_str = "parity-last";
637 } else {
638 c = map_num(pers, level);
639 if (c == NULL)
640 return 1;/* not possible */
641 err = sysfs_set_str(sra, NULL, "level", c);
642 if (err) {
643 fprintf(stderr, Name ": %s: could not set level to %s\n",
644 devname, c);
645 rv = 1;
646 goto release;
647 }
648 orig = array;
649 orig_level = orig.level;
650 ioctl(fd, GET_ARRAY_INFO, &array);
651 if (layout_str == NULL &&
652 orig.level == 5 && level == 6 &&
653 array.layout != orig.layout)
654 layout_str = map_num(r5layout, orig.layout);
655 if (!quiet)
656 fprintf(stderr, Name " level of %s changed to %s\n",
657 devname, c);
658 changed = 1;
659 }
660 }
661
662 /* ========= set shape (chunk_size / layout / ndisks) ============== */
663 /* Check if layout change is a no-op */
664 if (layout_str) switch(array.level) {
665 case 5:
666 if (array.layout == map_name(r5layout, layout_str))
667 layout_str = NULL;
668 break;
669 case 6:
670 if (layout_str == NULL &&
671 ((chunksize && chunksize * 1024 != array.chunk_size) ||
672 (raid_disks && raid_disks != array.raid_disks)) &&
673 array.layout >= 16) {
674 fprintf(stderr, Name
675 ": %s has a non-standard layout. If you wish to preserve this\n"
676 " during the reshape, please specify --layout=preserve\n"
677 " If you want to change it, specify a layout or use --layout=normalise\n",
678 devname);
679 rv = 1;
680 goto release;
681 }
682 if (strcmp(layout_str, "normalise") == 0 ||
683 strcmp(layout_str, "normalize") == 0) {
684 char *hyphen;
685 strcpy(alt_layout, map_num(r6layout, array.layout));
686 hyphen = strrchr(alt_layout, '-');
687 if (hyphen && strcmp(hyphen, "-6") == 0) {
688 *hyphen = 0;
689 layout_str = alt_layout;
690 }
691 }
692
693 if (array.layout == map_name(r6layout, layout_str))
694 layout_str = NULL;
695 if (layout_str && strcmp(layout_str, "preserve") == 0)
696 layout_str = NULL;
697 break;
698 }
699 if (layout_str == NULL
700 && (chunksize == 0 || chunksize*1024 == array.chunk_size)
701 && (raid_disks == 0 || raid_disks == array.raid_disks)) {
702 rv = 0;
703 if (level != UnSet && level != array.level) {
704 /* Looks like this level change doesn't need
705 * a reshape after all.
706 */
707 c = map_num(pers, level);
708 if (c) {
709 rv = sysfs_set_str(sra, NULL, "level", c);
710 if (rv)
711 fprintf(stderr, Name ": %s: could not set level to %s\n",
712 devname, c);
713 }
714 } else if (!changed && !quiet)
715 fprintf(stderr, Name ": %s: no change requested\n",
716 devname);
717 goto release;
718 }
719
720 c = map_num(pers, array.level);
721 if (c == NULL) c = "-unknown-";
722 switch(array.level) {
723 default: /* raid0, linear, multipath cannot be reconfigured */
724 fprintf(stderr, Name ": %s array %s cannot be reshaped.\n",
725 c, devname);
726 rv = 1;
727 break;
728
729 case LEVEL_FAULTY: /* only 'layout' change is permitted */
730
731 if (chunksize || raid_disks) {
732 fprintf(stderr, Name ": %s: Cannot change chunksize or disks of a 'faulty' array\n",
733 devname);
734 rv = 1;
735 break;
736 }
737 if (layout_str == NULL)
738 break; /* nothing to do.... */
739
740 array.layout = parse_layout_faulty(layout_str);
741 if (array.layout < 0) {
742 int rv;
743 fprintf(stderr, Name ": %s: layout %s not understood for 'faulty' array\n",
744 devname, layout_str);
745 rv = 1;
746 break;
747 }
748 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
749 fprintf(stderr, Name ": Cannot set layout for %s: %s\n",
750 devname, strerror(errno));
751 rv = 1;
752 } else if (!quiet)
753 printf("layout for %s set to %d\n", devname, array.layout);
754 break;
755
756 case 1: /* only raid_disks can each be changed. */
757
758 if (chunksize || layout_str != NULL) {
759 fprintf(stderr, Name ": %s: Cannot change chunk size or layout for a RAID1 array.\n",
760 devname);
761 rv = 1;
762 break;
763 }
764 if (raid_disks > 0) {
765 array.raid_disks = raid_disks;
766 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
767 fprintf(stderr, Name ": Cannot set raid-devices for %s: %s\n",
768 devname, strerror(errno));
769 rv = 1;
770 }
771 }
772 break;
773
774 case 4:
775 case 5:
776 case 6:
777
778 /*
779 * layout/chunksize/raid_disks can be changed
780 * though the kernel may not support it all.
781 */
782 st = super_by_fd(fd);
783
784 /*
785 * There are three possibilities.
786 * 1/ The array will shrink.
787 * We need to ensure the reshape will pause before reaching
788 * the 'critical section'. We also need to fork and wait for
789 * that to happen. When it does we
790 * suspend/backup/complete/unfreeze
791 *
792 * 2/ The array will not change size.
793 * This requires that we keep a backup of a sliding window
794 * so that we can restore data after a crash. So we need
795 * to fork and monitor progress.
796 *
797 * 3/ The array will grow. This is relatively easy.
798 * However the kernel's restripe routines will cheerfully
799 * overwrite some early data before it is safe. So we
800 * need to make a backup of the early parts of the array
801 * and be ready to restore it if rebuild aborts very early.
802 *
803 * We backup data by writing it to one spare, or to a
804 * file which was given on command line.
805 *
806 * [FOLLOWING IS OLD AND PARTLY WRONG]
807 * So: we enumerate the devices in the array and
808 * make sure we can open all of them.
809 * Then we freeze the early part of the array and
810 * backup to the various spares.
811 * Then we request changes and start the reshape.
812 * Monitor progress until it has passed the danger zone.
813 * and finally invalidate the copied data and unfreeze the
814 * start of the array.
815 *
816 * In each case, we first make sure that storage is available
817 * for the required backup.
818 * Then we:
819 * - request the shape change.
820 * - for to handle backup etc.
821 */
822 nchunk = ochunk = array.chunk_size;
823 nlayout = olayout = array.layout;
824 ndisks = odisks = array.raid_disks;
825
826 if (chunksize) {
827 nchunk = chunksize * 1024;
828 if (array.size % chunksize) {
829 fprintf(stderr, Name ": component size %dK is not"
830 " a multiple of chunksize %dK\n",
831 array.size, chunksize);
832 break;
833 }
834 }
835 if (layout_str != NULL)
836 switch(array.level) {
837 case 4: /* ignore layout */
838 break;
839 case 5:
840 nlayout = map_name(r5layout, layout_str);
841 if (nlayout == UnSet) {
842 fprintf(stderr, Name ": layout %s not understood for raid5.\n",
843 layout_str);
844 return 1;
845 }
846 break;
847
848 case 6:
849 nlayout = map_name(r6layout, layout_str);
850 if (nlayout == UnSet) {
851 fprintf(stderr, Name ": layout %s not understood for raid6.\n",
852 layout_str);
853 return 1;
854 }
855 break;
856 }
857 if (raid_disks) ndisks = raid_disks;
858
859 odata = odisks-1;
860 ndata = ndisks-1;
861 if (array.level == 6) {
862 odata--; /* number of data disks */
863 ndata--;
864 }
865
866 /* Check that we can hold all the data */
867 size = ndata * array.size;
868 get_dev_size(fd, NULL, &array_size);
869 if (size < (array_size/1024)) {
870 fprintf(stderr, Name ": this change will reduce the size of the array.\n"
871 " use --grow --array-size first to truncate array.\n"
872 " e.g. mdadm --grow %s --array-size %llu\n",
873 devname, size);
874 rv = 1;
875 break;
876 }
877
878 /* So how much do we need to backup.
879 * We need an amount of data which is both a whole number of
880 * old stripes and a whole number of new stripes.
881 * So LCM for (chunksize*datadisks).
882 */
883 a = ochunk/512 * odata;
884 b = nchunk/512 * ndata;
885 /* Find GCD */
886 while (a != b) {
887 if (a < b)
888 b -= a;
889 if (b < a)
890 a -= b;
891 }
892 /* LCM == product / GCD */
893 blocks = ochunk/512 * nchunk/512 * odata * ndata / a;
894
895 if (ndata == odata)
896 blocks *= 16;
897 else
898 fprintf(stderr, Name ": Need to backup %luK of critical "
899 "section..\n", blocks/2);
900
901 sysfs_free(sra);
902 sra = sysfs_read(fd, 0,
903 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|
904 GET_CACHE);
905 if (!sra) {
906 fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n",
907 devname);
908 rv = 1;
909 break;
910 }
911
912 if (blocks >= sra->component_size/2) {
913 fprintf(stderr, Name ": %s: Something wrong - reshape aborted\n",
914 devname);
915 rv = 1;
916 break;
917 }
918 nrdisks = array.nr_disks + sra->array.spare_disks;
919 /* Now we need to open all these devices so we can read/write.
920 */
921 fdlist = malloc((1+nrdisks) * sizeof(int));
922 offsets = malloc((1+nrdisks) * sizeof(offsets[0]));
923 if (!fdlist || !offsets) {
924 fprintf(stderr, Name ": malloc failed: grow aborted\n");
925 rv = 1;
926 break;
927 }
928 for (d=0; d <= nrdisks; d++)
929 fdlist[d] = -1;
930 d = array.raid_disks;
931 for (sd = sra->devs; sd; sd=sd->next) {
932 if (sd->disk.state & (1<<MD_DISK_FAULTY))
933 continue;
934 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
935 char *dn = map_dev(sd->disk.major,
936 sd->disk.minor, 1);
937 fdlist[sd->disk.raid_disk]
938 = dev_open(dn, O_RDONLY);
939 offsets[sd->disk.raid_disk] = sd->data_offset*512;
940 if (fdlist[sd->disk.raid_disk] < 0) {
941 fprintf(stderr, Name ": %s: cannot open component %s\n",
942 devname, dn?dn:"-unknown-");
943 rv = 1;
944 goto release;
945 }
946 } else if (backup_file == NULL) {
947 /* spare */
948 char *dn = map_dev(sd->disk.major,
949 sd->disk.minor, 1);
950 fdlist[d] = dev_open(dn, O_RDWR);
951 offsets[d] = (sra->component_size - blocks - 8)*512;
952 if (fdlist[d]<0) {
953 fprintf(stderr, Name ": %s: cannot open component %s\n",
954 devname, dn?dn:"-unknown");
955 rv = 1;
956 goto release;
957 }
958 d++;
959 }
960 }
961 if (backup_file == NULL) {
962 if (ndata <= odata) {
963 fprintf(stderr, Name ": %s: Cannot grow - need backup-file\n",
964 devname);
965 rv = 1;
966 break;
967 } else if (sra->array.spare_disks == 0) {
968 fprintf(stderr, Name ": %s: Cannot grow - need a spare or "
969 "backup-file to backup critical section\n",
970 devname);
971 rv = 1;
972 break;
973 }
974 if (d == array.raid_disks) {
975 fprintf(stderr, Name ": %s: No spare device for backup\n",
976 devname);
977 rv = 1;
978 break;
979 }
980 } else {
981 /* need to check backup file is large enough */
982 char buf[512];
983 fdlist[d] = open(backup_file, O_RDWR|O_CREAT|O_EXCL,
984 S_IRUSR | S_IWUSR);
985 offsets[d] = 8 * 512;
986 if (fdlist[d] < 0) {
987 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
988 devname, backup_file, strerror(errno));
989 rv = 1;
990 break;
991 }
992 memset(buf, 0, 512);
993 for (i=0; i < blocks + 1 ; i++) {
994 if (write(fdlist[d], buf, 512) != 512) {
995 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
996 devname, backup_file, strerror(errno));
997 rv = 1;
998 break;
999 }
1000 }
1001 if (fsync(fdlist[d]) != 0) {
1002 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
1003 devname, backup_file, strerror(errno));
1004 rv = 1;
1005 break;
1006 }
1007 d++;
1008 }
1009
1010 /* lastly, check that the internal stripe cache is
1011 * large enough, or it won't work.
1012 */
1013
1014 cache = (nchunk < ochunk) ? ochunk : nchunk;
1015 cache = cache * 4 / 4096;
1016 if (sra->cache_size < cache)
1017 sysfs_set_num(sra, NULL, "stripe_cache_size",
1018 cache+1);
1019 /* Right, everything seems fine. Let's kick things off.
1020 * If only changing raid_disks, use ioctl, else use
1021 * sysfs.
1022 */
1023 if (ochunk == nchunk && olayout == nlayout) {
1024 array.raid_disks = ndisks;
1025 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
1026 rv = 1;
1027 fprintf(stderr, Name ": Cannot set device shape for %s: %s\n",
1028 devname, strerror(errno));
1029 if (ndisks < odisks &&
1030 get_linux_version() < 2006030)
1031 fprintf(stderr, Name ": linux 2.6.30 or later required\n");
1032
1033 break;
1034 }
1035 } else {
1036 /* set them all just in case some old 'new_*' value
1037 * persists from some earlier problem
1038 */
1039 if (sysfs_set_num(sra, NULL, "chunk_size", nchunk) < 0)
1040 rv = 1;
1041 if (sysfs_set_num(sra, NULL, "layout", nlayout) < 0)
1042 rv = 1;
1043 if (sysfs_set_num(sra, NULL, "raid_disks", ndisks) < 0)
1044 rv = 1;
1045 if (rv) {
1046 fprintf(stderr, Name ": Cannot set device shape for %s\n",
1047 devname);
1048 if (get_linux_version() < 2006030)
1049 fprintf(stderr, Name ": linux 2.6.30 or later required\n");
1050 break;
1051 }
1052 }
1053
1054 if (ndisks == 2 && odisks == 2) {
1055 /* No reshape is needed in this trivial case */
1056 rv = 0;
1057 break;
1058 }
1059
1060 /* set up the backup-super-block. This requires the
1061 * uuid from the array.
1062 */
1063 /* Find a superblock */
1064 for (sd = sra->devs; sd; sd = sd->next) {
1065 char *dn;
1066 int devfd;
1067 int ok;
1068 if (sd->disk.state & (1<<MD_DISK_FAULTY))
1069 continue;
1070 dn = map_dev(sd->disk.major, sd->disk.minor, 1);
1071 devfd = dev_open(dn, O_RDONLY);
1072 if (devfd < 0)
1073 continue;
1074 ok = st->ss->load_super(st, devfd, NULL);
1075 close(devfd);
1076 if (ok >= 0)
1077 break;
1078 }
1079 if (!sd) {
1080 fprintf(stderr, Name ": %s: Cannot find a superblock\n",
1081 devname);
1082 rv = 1;
1083 break;
1084 }
1085
1086 memset(&bsb, 0, 512);
1087 memcpy(bsb.magic, "md_backup_data-1", 16);
1088 st->ss->uuid_from_super(st, (int*)&bsb.set_uuid);
1089 bsb.mtime = __cpu_to_le64(time(0));
1090 bsb.devstart2 = blocks;
1091 stripes = blocks / (ochunk/512) / odata;
1092 /* Now we just need to kick off the reshape and watch, while
1093 * handling backups of the data...
1094 * This is all done by a forked background process.
1095 */
1096 switch(fork()) {
1097 case 0:
1098 close(fd);
1099 if (check_env("MDADM_GROW_VERIFY"))
1100 fd = open(devname, O_RDONLY | O_DIRECT);
1101 else
1102 fd = -1;
1103 mlockall(MCL_FUTURE);
1104
1105 if (odata < ndata)
1106 done = child_grow(fd, sra, stripes,
1107 fdlist, offsets,
1108 odisks, ochunk, array.level, olayout, odata,
1109 d - odisks, fdlist+odisks, offsets+odisks);
1110 else if (odata > ndata)
1111 done = child_shrink(fd, sra, stripes,
1112 fdlist, offsets,
1113 odisks, ochunk, array.level, olayout, odata,
1114 d - odisks, fdlist+odisks, offsets+odisks);
1115 else
1116 done = child_same_size(fd, sra, stripes,
1117 fdlist, offsets,
1118 0,
1119 odisks, ochunk, array.level, olayout, odata,
1120 d - odisks, fdlist+odisks, offsets+odisks);
1121 if (backup_file && done)
1122 unlink(backup_file);
1123 if (level != UnSet && level != array.level) {
1124 /* We need to wait for the reshape to finish
1125 * (which will have happened unless odata < ndata)
1126 * and then set the level
1127 */
1128
1129 c = map_num(pers, level);
1130 if (c == NULL)
1131 exit(0);/* not possible */
1132
1133 if (odata < ndata)
1134 wait_reshape(sra);
1135 err = sysfs_set_str(sra, NULL, "level", c);
1136 if (err)
1137 fprintf(stderr, Name ": %s: could not set level to %s\n",
1138 devname, c);
1139 }
1140 exit(0);
1141 case -1:
1142 fprintf(stderr, Name ": Cannot run child to monitor reshape: %s\n",
1143 strerror(errno));
1144 rv = 1;
1145 break;
1146 default:
1147 /* The child will take care of unfreezing the array */
1148 frozen = 0;
1149 break;
1150 }
1151 break;
1152
1153 }
1154
1155 release:
1156 if (rv && orig_level != UnSet && sra) {
1157 c = map_num(pers, orig_level);
1158 if (c && sysfs_set_str(sra, NULL, "level", c) == 0)
1159 fprintf(stderr, Name ": aborting level change\n");
1160 }
1161 if (sra)
1162 unfreeze_array(sra, frozen);
1163 return rv;
1164 }
1165
1166 /*
1167 * We run a child process in the background which performs the following
1168 * steps:
1169 * - wait for resync to reach a certain point
1170 * - suspend io to the following section
1171 * - backup that section
1172 * - allow resync to proceed further
1173 * - resume io
1174 * - discard the backup.
1175 *
1176 * When are combined in slightly different ways in the three cases.
1177 * Grow:
1178 * - suspend/backup/allow/wait/resume/discard
1179 * Shrink:
1180 * - allow/wait/suspend/backup/allow/wait/resume/discard
1181 * same-size:
1182 * - wait/resume/discard/suspend/backup/allow
1183 *
1184 * suspend/backup/allow always come together
1185 * wait/resume/discard do too.
1186 * For the same-size case we have two backups to improve flow.
1187 *
1188 */
1189
1190 int grow_backup(struct mdinfo *sra,
1191 unsigned long long offset, /* per device */
1192 unsigned long stripes, /* per device */
1193 int *sources, unsigned long long *offsets,
1194 int disks, int chunk, int level, int layout,
1195 int dests, int *destfd, unsigned long long *destoffsets,
1196 int part,
1197 char *buf)
1198 {
1199 /* Backup 'blocks' sectors at 'offset' on each device of the array,
1200 * to storage 'destfd' (offset 'destoffsets'), after first
1201 * suspending IO. Then allow resync to continue
1202 * over the suspended section.
1203 * Use part 'part' of the backup-super-block.
1204 */
1205 int odata = disks;
1206 int rv = 0;
1207 int i;
1208 //printf("offset %llu\n", offset);
1209 if (level >= 4)
1210 odata--;
1211 if (level == 6)
1212 odata--;
1213 sysfs_set_num(sra, NULL, "suspend_hi", (offset + stripes * chunk/512) * odata);
1214 if (part) {
1215 bsb.arraystart2 = __cpu_to_le64(offset * odata);
1216 bsb.length2 = __cpu_to_le64(stripes * chunk/512 * odata);
1217 } else {
1218 bsb.arraystart = __cpu_to_le64(offset * odata);
1219 bsb.length = __cpu_to_le64(stripes * chunk/512 * odata);
1220 }
1221 if (part)
1222 bsb.magic[15] = '2';
1223 for (i = 0; i < dests; i++)
1224 if (part)
1225 lseek64(destfd[i], destoffsets[i] + __le64_to_cpu(bsb.devstart2)*512, 0);
1226 else
1227 lseek64(destfd[i], destoffsets[i], 0);
1228
1229 rv = save_stripes(sources, offsets,
1230 disks, chunk, level, layout,
1231 dests, destfd,
1232 offset*512*odata, stripes * chunk * odata,
1233 buf);
1234
1235 if (rv)
1236 return rv;
1237 for (i = 0; i < dests; i++) {
1238 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
1239
1240 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
1241 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
1242 bsb.sb_csum2 = bsb_csum((char*)&bsb,
1243 ((char*)&bsb.sb_csum2)-((char*)&bsb));
1244
1245 lseek64(destfd[i], destoffsets[i] - 4096, 0);
1246 write(destfd[i], &bsb, 512);
1247 fsync(destfd[i]);
1248 }
1249
1250 return 0;
1251 }
1252
1253 /* in 2.6.30, the value reported by sync_completed can be
1254 * less that it should be by one stripe.
1255 * This only happens when reshape hits sync_max and pauses.
1256 * So allow wait_backup to either extent sync_max further
1257 * than strictly necessary, or return before the
1258 * sync has got quite as far as we would really like.
1259 * This is what 'blocks2' is for.
1260 * The various caller give appropriate values so that
1261 * every works.
1262 */
1263 int wait_backup(struct mdinfo *sra,
1264 unsigned long long offset, /* per device */
1265 unsigned long long blocks, /* per device */
1266 unsigned long long blocks2, /* per device - hack */
1267 int dests, int *destfd, unsigned long long *destoffsets,
1268 int part)
1269 {
1270 /* Wait for resync to pass the section that was backed up
1271 * then erase the backup and allow IO
1272 */
1273 int fd = sysfs_get_fd(sra, NULL, "sync_completed");
1274 unsigned long long completed;
1275 int i;
1276
1277 if (fd < 0)
1278 return -1;
1279 sysfs_set_num(sra, NULL, "sync_max", offset + blocks + blocks2);
1280 if (offset == 0)
1281 sysfs_set_str(sra, NULL, "sync_action", "reshape");
1282 do {
1283 char action[20];
1284 fd_set rfds;
1285 FD_ZERO(&rfds);
1286 FD_SET(fd, &rfds);
1287 select(fd+1, NULL, NULL, &rfds, NULL);
1288 if (sysfs_fd_get_ll(fd, &completed) < 0) {
1289 close(fd);
1290 return -1;
1291 }
1292 if (sysfs_get_str(sra, NULL, "sync_action",
1293 action, 20) > 0 &&
1294 strncmp(action, "reshape", 7) != 0)
1295 break;
1296 } while (completed < offset + blocks);
1297 close(fd);
1298
1299 if (part) {
1300 bsb.arraystart2 = __cpu_to_le64(0);
1301 bsb.length2 = __cpu_to_le64(0);
1302 } else {
1303 bsb.arraystart = __cpu_to_le64(0);
1304 bsb.length = __cpu_to_le64(0);
1305 }
1306 for (i = 0; i < dests; i++) {
1307 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
1308 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
1309 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
1310 bsb.sb_csum2 = bsb_csum((char*)&bsb,
1311 ((char*)&bsb.sb_csum2)-((char*)&bsb));
1312 lseek64(destfd[i], destoffsets[i]-4096, 0);
1313 write(destfd[i], &bsb, 512);
1314 fsync(destfd[i]);
1315 }
1316 return 0;
1317 }
1318
1319 static void fail(char *msg)
1320 {
1321 write(2, msg, strlen(msg));
1322 write(2, "\n", 1);
1323 exit(1);
1324 }
1325
1326 static char *abuf, *bbuf;
1327 static int abuflen;
1328 static void validate(int afd, int bfd, unsigned long long offset)
1329 {
1330 /* check that the data in the backup against the array.
1331 * This is only used for regression testing and should not
1332 * be used while the array is active
1333 */
1334 struct mdp_backup_super bsb2;
1335 if (afd < 0)
1336 return;
1337 lseek64(bfd, offset - 4096, 0);
1338 if (read(bfd, &bsb2, 512) != 512)
1339 fail("cannot read bsb");
1340 if (bsb2.sb_csum != bsb_csum((char*)&bsb2,
1341 ((char*)&bsb2.sb_csum)-((char*)&bsb2)))
1342 fail("first csum bad");
1343 if (memcmp(bsb2.magic, "md_backup_data", 14) != 0)
1344 fail("magic is bad");
1345 if (memcmp(bsb2.magic, "md_backup_data-2", 16) == 0 &&
1346 bsb2.sb_csum2 != bsb_csum((char*)&bsb2,
1347 ((char*)&bsb2.sb_csum2)-((char*)&bsb2)))
1348 fail("second csum bad");
1349
1350 if (__le64_to_cpu(bsb2.devstart)*512 != offset)
1351 fail("devstart is wrong");
1352
1353 if (bsb2.length) {
1354 unsigned long long len = __le64_to_cpu(bsb2.length)*512;
1355
1356 if (abuflen < len) {
1357 free(abuf);
1358 free(bbuf);
1359 abuflen = len;
1360 posix_memalign((void**)&abuf, 4096, abuflen);
1361 posix_memalign((void**)&bbuf, 4096, abuflen);
1362 }
1363
1364 lseek64(bfd, offset, 0);
1365 if (read(bfd, bbuf, len) != len) {
1366 printf("len %llu\n", len);
1367 fail("read first backup failed");
1368 }
1369 lseek64(afd, __le64_to_cpu(bsb2.arraystart)*512, 0);
1370 if (read(afd, abuf, len) != len)
1371 fail("read first from array failed");
1372 if (memcmp(bbuf, abuf, len) != 0) {
1373 int i;
1374 printf("offset=%llu len=%llu\n",
1375 __le64_to_cpu(bsb2.arraystart)*512, len);
1376 for (i=0; i<len; i++)
1377 if (bbuf[i] != abuf[i]) {
1378 printf("first diff byte %d\n", i);
1379 break;
1380 }
1381 fail("data1 compare failed");
1382 }
1383 }
1384 if (bsb2.length2) {
1385 unsigned long long len = __le64_to_cpu(bsb2.length2)*512;
1386
1387 if (abuflen < len) {
1388 free(abuf);
1389 free(bbuf);
1390 abuflen = len;
1391 abuf = malloc(abuflen);
1392 bbuf = malloc(abuflen);
1393 }
1394
1395 lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0);
1396 if (read(bfd, bbuf, len) != len)
1397 fail("read second backup failed");
1398 lseek64(afd, __le64_to_cpu(bsb2.arraystart2)*512, 0);
1399 if (read(afd, abuf, len) != len)
1400 fail("read second from array failed");
1401 if (memcmp(bbuf, abuf, len) != 0)
1402 fail("data2 compare failed");
1403 }
1404 }
1405
1406 static int child_grow(int afd, struct mdinfo *sra, unsigned long stripes,
1407 int *fds, unsigned long long *offsets,
1408 int disks, int chunk, int level, int layout, int data,
1409 int dests, int *destfd, unsigned long long *destoffsets)
1410 {
1411 char *buf;
1412
1413 posix_memalign((void**)&buf, 4096, disks * chunk);
1414 sysfs_set_num(sra, NULL, "suspend_hi", 0);
1415 sysfs_set_num(sra, NULL, "suspend_lo", 0);
1416 grow_backup(sra, 0, stripes,
1417 fds, offsets, disks, chunk, level, layout,
1418 dests, destfd, destoffsets,
1419 0, buf);
1420 validate(afd, destfd[0], destoffsets[0]);
1421 if (wait_backup(sra, 0, stripes * chunk / 512, stripes * chunk / 512,
1422 dests, destfd, destoffsets,
1423 0) < 0)
1424 return 0;
1425 sysfs_set_num(sra, NULL, "suspend_lo", (stripes * chunk/512) * data);
1426 free(buf);
1427 /* FIXME this should probably be numeric */
1428 sysfs_set_str(sra, NULL, "sync_max", "max");
1429 return 1;
1430 }
1431
1432 static int child_shrink(int afd, struct mdinfo *sra, unsigned long stripes,
1433 int *fds, unsigned long long *offsets,
1434 int disks, int chunk, int level, int layout, int data,
1435 int dests, int *destfd, unsigned long long *destoffsets)
1436 {
1437 char *buf;
1438 unsigned long long start;
1439 int rv;
1440
1441 posix_memalign((void**)&buf, 4096, disks * chunk);
1442 start = sra->component_size - stripes * chunk/512;
1443 sysfs_set_num(sra, NULL, "sync_max", start);
1444 sysfs_set_str(sra, NULL, "sync_action", "reshape");
1445 sysfs_set_num(sra, NULL, "suspend_lo", 0);
1446 sysfs_set_num(sra, NULL, "suspend_hi", 0);
1447 rv = wait_backup(sra, 0, start - stripes * chunk/512, stripes * chunk/512,
1448 dests, destfd, destoffsets, 0);
1449 if (rv < 0)
1450 return 0;
1451 grow_backup(sra, 0, stripes,
1452 fds, offsets,
1453 disks, chunk, level, layout,
1454 dests, destfd, destoffsets,
1455 0, buf);
1456 validate(afd, destfd[0], destoffsets[0]);
1457 rv = wait_backup(sra, start, stripes*chunk/512, 0,
1458 dests, destfd, destoffsets, 0);
1459 if (rv < 0)
1460 return 0;
1461 sysfs_set_num(sra, NULL, "suspend_lo", (stripes * chunk/512) * data);
1462 free(buf);
1463 /* FIXME this should probably be numeric */
1464 sysfs_set_str(sra, NULL, "sync_max", "max");
1465 return 1;
1466 }
1467
1468 static int child_same_size(int afd, struct mdinfo *sra, unsigned long stripes,
1469 int *fds, unsigned long long *offsets,
1470 unsigned long long start,
1471 int disks, int chunk, int level, int layout, int data,
1472 int dests, int *destfd, unsigned long long *destoffsets)
1473 {
1474 unsigned long long size;
1475 unsigned long tailstripes = stripes;
1476 int part;
1477 char *buf;
1478 unsigned long long speed;
1479
1480
1481 posix_memalign((void**)&buf, 4096, disks * chunk);
1482
1483 sysfs_set_num(sra, NULL, "suspend_lo", 0);
1484 sysfs_set_num(sra, NULL, "suspend_hi", 0);
1485
1486 sysfs_get_ll(sra, NULL, "sync_speed_min", &speed);
1487 sysfs_set_num(sra, NULL, "sync_speed_min", 200000);
1488
1489 grow_backup(sra, start, stripes,
1490 fds, offsets,
1491 disks, chunk, level, layout,
1492 dests, destfd, destoffsets,
1493 0, buf);
1494 grow_backup(sra, (start + stripes) * chunk/512, stripes,
1495 fds, offsets,
1496 disks, chunk, level, layout,
1497 dests, destfd, destoffsets,
1498 1, buf);
1499 validate(afd, destfd[0], destoffsets[0]);
1500 part = 0;
1501 start += stripes * 2; /* where to read next */
1502 size = sra->component_size / (chunk/512);
1503 while (start < size) {
1504 if (wait_backup(sra, (start-stripes*2)*chunk/512,
1505 stripes*chunk/512, 0,
1506 dests, destfd, destoffsets,
1507 part) < 0)
1508 return 0;
1509 sysfs_set_num(sra, NULL, "suspend_lo", start*chunk/512 * data);
1510 if (start + stripes > size)
1511 tailstripes = (size - start);
1512
1513 grow_backup(sra, start*chunk/512, tailstripes,
1514 fds, offsets,
1515 disks, chunk, level, layout,
1516 dests, destfd, destoffsets,
1517 part, buf);
1518 start += stripes;
1519 part = 1 - part;
1520 validate(afd, destfd[0], destoffsets[0]);
1521 }
1522 if (wait_backup(sra, (start-stripes*2) * chunk/512, stripes * chunk/512, 0,
1523 dests, destfd, destoffsets,
1524 part) < 0)
1525 return 0;
1526 sysfs_set_num(sra, NULL, "suspend_lo", ((start-stripes)*chunk/512) * data);
1527 if (wait_backup(sra, (start-stripes) * chunk/512, tailstripes * chunk/512, 0,
1528 dests, destfd, destoffsets,
1529 1-part) < 0)
1530 return 0;
1531 sysfs_set_num(sra, NULL, "suspend_lo", (size*chunk/512) * data);
1532 sysfs_set_num(sra, NULL, "sync_speed_min", speed);
1533 free(buf);
1534 return 1;
1535 }
1536
1537 /*
1538 * If any spare contains md_back_data-1 which is recent wrt mtime,
1539 * write that data into the array and update the super blocks with
1540 * the new reshape_progress
1541 */
1542 int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt, char *backup_file)
1543 {
1544 int i, j;
1545 int old_disks;
1546 unsigned long long *offsets;
1547 unsigned long long nstripe, ostripe, last_block;
1548 int ndata, odata;
1549
1550 if (info->new_level != info->array.level)
1551 return 1; /* Cannot handle level changes (they are instantaneous) */
1552
1553 odata = info->array.raid_disks - info->delta_disks - 1;
1554 if (info->array.level == 6) odata--; /* number of data disks */
1555 ndata = info->array.raid_disks - 1;
1556 if (info->new_level == 6) ndata--;
1557
1558 old_disks = info->array.raid_disks - info->delta_disks;
1559
1560 if (info->delta_disks <= 0)
1561 /* Didn't grow, so the backup file must have
1562 * been used
1563 */
1564 old_disks = cnt;
1565 for (i=old_disks-(backup_file?1:0); i<cnt; i++) {
1566 struct mdinfo dinfo;
1567 char buf[4096];
1568 int fd;
1569 int bsbsize;
1570
1571 /* This was a spare and may have some saved data on it.
1572 * Load the superblock, find and load the
1573 * backup_super_block.
1574 * If either fail, go on to next device.
1575 * If the backup contains no new info, just return
1576 * else restore data and update all superblocks
1577 */
1578 if (i == old_disks-1) {
1579 fd = open(backup_file, O_RDONLY);
1580 if (fd<0) {
1581 fprintf(stderr, Name ": backup file %s inaccessible: %s\n",
1582 backup_file, strerror(errno));
1583 continue;
1584 }
1585 } else {
1586 fd = fdlist[i];
1587 if (fd < 0)
1588 continue;
1589 if (st->ss->load_super(st, fd, NULL))
1590 continue;
1591
1592 st->ss->getinfo_super(st, &dinfo);
1593 st->ss->free_super(st);
1594
1595 if (lseek64(fd,
1596 (dinfo.data_offset + dinfo.component_size - 8) <<9,
1597 0) < 0)
1598 continue; /* Cannot seek */
1599 }
1600 if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb))
1601 continue; /* Cannot read */
1602 if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0 &&
1603 memcmp(bsb.magic, "md_backup_data-2", 16) != 0)
1604 continue;
1605 if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb)))
1606 continue; /* bad checksum */
1607 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0 &&
1608 bsb.sb_csum2 != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum2)-((char*)&bsb)))
1609 if (memcmp(bsb.set_uuid,info->uuid, 16) != 0)
1610 continue; /* Wrong uuid */
1611
1612 if (info->array.utime > __le64_to_cpu(bsb.mtime) + 3600 ||
1613 info->array.utime < __le64_to_cpu(bsb.mtime))
1614 continue; /* time stamp is too bad */
1615
1616 if (bsb.magic[15] == '1') {
1617 if (info->delta_disks >= 0) {
1618 /* reshape_progress is increasing */
1619 if (__le64_to_cpu(bsb.arraystart) + __le64_to_cpu(bsb.length) <
1620 info->reshape_progress)
1621 continue; /* No new data here */
1622 } else {
1623 /* reshape_progress is decreasing */
1624 if (__le64_to_cpu(bsb.arraystart) >=
1625 info->reshape_progress)
1626 continue; /* No new data here */
1627 }
1628 } else {
1629 if (info->delta_disks >= 0) {
1630 /* reshape_progress is increasing */
1631 if (__le64_to_cpu(bsb.arraystart) + __le64_to_cpu(bsb.length) <
1632 info->reshape_progress &&
1633 __le64_to_cpu(bsb.arraystart2) + __le64_to_cpu(bsb.length2) <
1634 info->reshape_progress)
1635 continue; /* No new data here */
1636 } else {
1637 /* reshape_progress is decreasing */
1638 if (__le64_to_cpu(bsb.arraystart) >=
1639 info->reshape_progress &&
1640 __le64_to_cpu(bsb.arraystart2) >=
1641 info->reshape_progress)
1642 continue; /* No new data here */
1643 }
1644 }
1645 if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0)
1646 continue; /* Cannot seek */
1647 /* There should be a duplicate backup superblock 4k before here */
1648 if (lseek64(fd, -4096, 1) < 0 ||
1649 read(fd, buf, 4096) != 4096)
1650 continue; /* Cannot find leading superblock */
1651 if (bsb.magic[15] == '1')
1652 bsbsize = offsetof(struct mdp_backup_super, pad1);
1653 else
1654 bsbsize = offsetof(struct mdp_backup_super, pad);
1655 if (memcmp(buf, &bsb, bsbsize) != 0)
1656 continue; /* Cannot find leading superblock */
1657
1658 /* Now need the data offsets for all devices. */
1659 offsets = malloc(sizeof(*offsets)*info->array.raid_disks);
1660 for(j=0; j<info->array.raid_disks; j++) {
1661 if (fdlist[j] < 0)
1662 continue;
1663 if (st->ss->load_super(st, fdlist[j], NULL))
1664 /* FIXME should be this be an error */
1665 continue;
1666 st->ss->getinfo_super(st, &dinfo);
1667 st->ss->free_super(st);
1668 offsets[j] = dinfo.data_offset;
1669 }
1670 printf(Name ": restoring critical section\n");
1671
1672 if (restore_stripes(fdlist, offsets,
1673 info->array.raid_disks,
1674 info->new_chunk,
1675 info->new_level,
1676 info->new_layout,
1677 fd, __le64_to_cpu(bsb.devstart)*512,
1678 __le64_to_cpu(bsb.arraystart),
1679 __le64_to_cpu(bsb.length)*512)) {
1680 /* didn't succeed, so giveup */
1681 return 1;
1682 }
1683
1684 if (bsb.magic[15] == '2' &&
1685 restore_stripes(fdlist, offsets,
1686 info->array.raid_disks,
1687 info->new_chunk,
1688 info->new_level,
1689 info->new_layout,
1690 fd, __le64_to_cpu(bsb.devstart)*512 +
1691 __le64_to_cpu(bsb.devstart2)*512,
1692 __le64_to_cpu(bsb.arraystart2),
1693 __le64_to_cpu(bsb.length2)*512)) {
1694 /* didn't succeed, so giveup */
1695 return 1;
1696 }
1697
1698
1699 /* Ok, so the data is restored. Let's update those superblocks. */
1700
1701 if (info->delta_disks >= 0) {
1702 info->reshape_progress = __le64_to_cpu(bsb.arraystart) +
1703 __le64_to_cpu(bsb.length);
1704 if (bsb.magic[15] == '2') {
1705 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2) +
1706 __le64_to_cpu(bsb.length2);
1707 if (p2 > info->reshape_progress)
1708 info->reshape_progress = p2;
1709 }
1710 } else {
1711 info->reshape_progress = __le64_to_cpu(bsb.arraystart);
1712 if (bsb.magic[15] == '2') {
1713 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2);
1714 if (p2 < info->reshape_progress)
1715 info->reshape_progress = p2;
1716 }
1717 }
1718 for (j=0; j<info->array.raid_disks; j++) {
1719 if (fdlist[j] < 0) continue;
1720 if (st->ss->load_super(st, fdlist[j], NULL))
1721 continue;
1722 st->ss->getinfo_super(st, &dinfo);
1723 dinfo.reshape_progress = info->reshape_progress;
1724 st->ss->update_super(st, &dinfo,
1725 "_reshape_progress",
1726 NULL,0, 0, NULL);
1727 st->ss->store_super(st, fdlist[j]);
1728 st->ss->free_super(st);
1729 }
1730 return 0;
1731 }
1732 /* Didn't find any backup data, try to see if any
1733 * was needed.
1734 */
1735 if (info->delta_disks == 0)
1736 /* Alway need backup data when size doesn't change */
1737 return 1;
1738 nstripe = ostripe = 0;
1739 last_block = 0;
1740 while (nstripe >= ostripe) {
1741 nstripe += info->new_chunk / 512;
1742 last_block = nstripe * ndata;
1743 ostripe = last_block / odata / (info->array.chunk_size/512) *
1744 (info->array.chunk_size/512);
1745 }
1746
1747 if (info->reshape_progress >= last_block)
1748 return 0;
1749 /* needed to recover critical section! */
1750 return 1;
1751 }
1752
1753 int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
1754 char *backup_file)
1755 {
1756 /* Array is assembled and ready to be started, but
1757 * monitoring is probably required.
1758 * So:
1759 * - start read-only
1760 * - set upper bound for resync
1761 * - initialise the 'suspend' boundaries
1762 * - switch to read-write
1763 * - fork and continue monitoring
1764 */
1765 int err;
1766 int backup_list[1];
1767 unsigned long long backup_offsets[1];
1768 int odisks, ndisks, ochunk, nchunk,odata,ndata;
1769 unsigned long a,b,blocks,stripes;
1770 int backup_fd;
1771 int *fds;
1772 unsigned long long *offsets;
1773 int d;
1774 struct mdinfo *sra, *sd;
1775 int rv;
1776 int done = 0;
1777
1778 err = sysfs_set_str(info, NULL, "array_state", "readonly");
1779 if (err)
1780 return err;
1781
1782 /* make sure reshape doesn't progress until we are ready */
1783 sysfs_set_str(info, NULL, "sync_max", "0");
1784 sysfs_set_str(info, NULL, "array_state", "active"); /* FIXME or clean */
1785
1786 /* ndisks is not growing, so raid_disks is old and +delta is new */
1787 odisks = info->array.raid_disks;
1788 ndisks = odisks + info->delta_disks;
1789 odata = odisks - 1;
1790 ndata = ndisks - 1;
1791 if (info->array.level == 6) {
1792 odata--;
1793 ndata--;
1794 }
1795 ochunk = info->array.chunk_size;
1796 nchunk = info->new_chunk;
1797
1798
1799 a = ochunk/512 * odata;
1800 b = nchunk/512 * ndata;
1801 /* Find GCD */
1802 while (a != b) {
1803 if (a < b)
1804 b -= a;
1805 if (b < a)
1806 a -= b;
1807 }
1808 /* LCM == product / GCD */
1809 blocks = ochunk/512 * nchunk/512 * odata * ndata / a;
1810
1811 if (ndata == odata)
1812 blocks *= 16;
1813 stripes = blocks / (info->array.chunk_size/512) / odata;
1814
1815
1816 memset(&bsb, 0, 512);
1817 memcpy(bsb.magic, "md_backup_data-1", 16);
1818 memcpy(&bsb.set_uuid, info->uuid, 16);
1819 bsb.mtime = __cpu_to_le64(time(0));
1820 bsb.devstart2 = blocks;
1821
1822 backup_fd = open(backup_file, O_RDWR|O_CREAT, S_IRUSR | S_IWUSR);
1823 backup_list[0] = backup_fd;
1824 backup_offsets[0] = 8 * 512;
1825 fds = malloc(odisks * sizeof(fds[0]));
1826 offsets = malloc(odisks * sizeof(offsets[0]));
1827 for (d=0; d<odisks; d++)
1828 fds[d] = -1;
1829
1830 sra = sysfs_read(-1, devname2devnum(info->sys_name),
1831 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|
1832 GET_CACHE);
1833
1834 for (sd = sra->devs; sd; sd = sd->next) {
1835 if (sd->disk.state & (1<<MD_DISK_FAULTY))
1836 continue;
1837 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
1838 char *dn = map_dev(sd->disk.major,
1839 sd->disk.minor, 1);
1840 fds[sd->disk.raid_disk]
1841 = dev_open(dn, O_RDONLY);
1842 offsets[sd->disk.raid_disk] = sd->data_offset*512;
1843 if (fds[sd->disk.raid_disk] < 0) {
1844 fprintf(stderr, Name ": %s: cannot open component %s\n",
1845 info->sys_name, dn?dn:"-unknown-");
1846 rv = 1;
1847 goto release;
1848 }
1849 free(dn);
1850 }
1851 }
1852
1853 switch(fork()) {
1854 case 0:
1855 close(mdfd);
1856 mlockall(MCL_FUTURE);
1857 if (info->delta_disks < 0)
1858 done = child_shrink(-1, info, stripes,
1859 fds, offsets,
1860 info->array.raid_disks,
1861 info->array.chunk_size,
1862 info->array.level, info->array.layout,
1863 odata,
1864 1, backup_list, backup_offsets);
1865 else if (info->delta_disks == 0) {
1866 /* The 'start' is a per-device stripe number.
1867 * reshape_progress is a per-array sector number.
1868 * So divide by ndata * chunk_size
1869 */
1870 unsigned long long start = info->reshape_progress / ndata;
1871 start /= (info->array.chunk_size/512);
1872 done = child_same_size(-1, info, stripes,
1873 fds, offsets,
1874 start,
1875 info->array.raid_disks,
1876 info->array.chunk_size,
1877 info->array.level, info->array.layout,
1878 odata,
1879 1, backup_list, backup_offsets);
1880 }
1881 if (backup_file && done)
1882 unlink(backup_file);
1883 /* FIXME should I intuit a level change */
1884 exit(0);
1885 case -1:
1886 fprintf(stderr, Name ": Cannot run child to continue monitoring reshape: %s\n",
1887 strerror(errno));
1888 return 1;
1889 default:
1890 break;
1891 }
1892 release:
1893 return 0;
1894 }
1895
1896