2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
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.
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.
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
22 * Email: <neilb@suse.de>
28 #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
29 #error no endian defined
35 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
38 int Grow_Add_device(char *devname
, int fd
, char *newdev
)
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
46 * This means that we need to *find* all other devices.
53 struct supertype
*st
= NULL
;
54 char *subarray
= NULL
;
56 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
57 fprintf(stderr
, Name
": cannot get array info for %s\n", devname
);
61 if (info
.array
.level
!= -1) {
62 fprintf(stderr
, Name
": can only add devices to linear arrays\n");
66 st
= super_by_fd(fd
, &subarray
);
68 fprintf(stderr
, Name
": cannot handle arrays with superblock version %d\n", info
.array
.major_version
);
73 fprintf(stderr
, Name
": Cannot grow linear sub-arrays yet\n");
78 nfd
= open(newdev
, O_RDWR
|O_EXCL
|O_DIRECT
);
80 fprintf(stderr
, Name
": cannot open %s\n", newdev
);
85 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
86 fprintf(stderr
, Name
": %s is not a block device!\n", newdev
);
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
++) {
96 st
->ss
->free_super(st
);
99 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
100 fprintf(stderr
, Name
": cannot get device detail for device %d\n",
106 dv
= map_dev(disk
.major
, disk
.minor
, 1);
108 fprintf(stderr
, Name
": cannot find device file for device %d\n",
114 fd2
= dev_open(dv
, O_RDWR
);
116 fprintf(stderr
, Name
": cannot open device file %s\n", dv
);
122 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
123 fprintf(stderr
, Name
": cannot find super block on %s\n", dv
);
131 /* Ok, looks good. Lets update the superblock and write it out to
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
,
143 if (st
->ss
->store_super(st
, nfd
)) {
144 fprintf(stderr
, Name
": Cannot store new superblock on %s\n",
151 if (ioctl(fd
, ADD_NEW_DISK
, &info
.disk
) != 0) {
152 fprintf(stderr
, Name
": Cannot add new disk to this array\n");
155 /* Well, that seems to have worked.
156 * Now go through and update all superblocks
159 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
160 fprintf(stderr
, Name
": cannot get array info for %s\n", devname
);
165 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
166 mdu_disk_info_t disk
;
170 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
171 fprintf(stderr
, Name
": cannot get device detail for device %d\n",
175 dv
= map_dev(disk
.major
, disk
.minor
, 1);
177 fprintf(stderr
, Name
": cannot find device file for device %d\n",
181 fd2
= dev_open(dv
, O_RDWR
);
183 fprintf(stderr
, Name
": cannot open device file %s\n", dv
);
186 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
187 fprintf(stderr
, Name
": cannot find super block on %s\n", dv
);
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;
196 st
->ss
->update_super(st
, &info
, "linear-grow-update", dv
,
199 if (st
->ss
->store_super(st
, fd2
)) {
200 fprintf(stderr
, Name
": Cannot store new superblock on %s\n", dv
);
210 int Grow_addbitmap(char *devname
, int fd
, char *file
, int chunk
, int delay
, int write_behind
, int force
)
213 * First check that array doesn't have a bitmap
214 * Then create the bitmap
217 * For internal bitmaps, we need to check the version,
218 * find all the active devices, and write the bitmap block
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
;
230 major
= BITMAP_MAJOR_HOSTENDIAN
;
232 fprintf(stderr
, Name
": Warning - bitmaps created on this kernel are not portable\n"
233 " between different architectured. Consider upgrading the Linux kernel.\n");
237 if (ioctl(fd
, GET_BITMAP_FILE
, &bmf
) != 0) {
239 fprintf(stderr
, Name
": Memory allocation failure.\n");
241 fprintf(stderr
, Name
": bitmaps not supported by this kernel.\n");
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",
253 fprintf(stderr
, Name
": %s already has a bitmap (%s)\n",
254 devname
, bmf
.pathname
);
257 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
258 fprintf(stderr
, Name
": cannot get array status for %s\n", devname
);
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");
270 fprintf(stderr
, Name
": Internal bitmap already present on %s\n",
275 if (strcmp(file
, "none") == 0) {
276 fprintf(stderr
, Name
": no bitmap found on %s\n", devname
);
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");
284 bitmapsize
= array
.size
;
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
291 bitmapsize
= get_component_size(fd
);
293 if (bitmapsize
== 0) {
294 fprintf(stderr
, Name
": Cannot reliably determine size of array to create bitmap - sorry.\n");
298 if (array
.level
== 10) {
299 int ncopies
= (array
.layout
&255)*((array
.layout
>>8)&255);
300 bitmapsize
= bitmapsize
* array
.raid_disks
/ ncopies
;
303 st
= super_by_fd(fd
, &subarray
);
305 fprintf(stderr
, Name
": Cannot understand version %d.%d\n",
306 array
.major_version
, array
.minor_version
);
310 fprintf(stderr
, Name
": Cannot add bitmaps to sub-arrays yet\n");
315 if (strcmp(file
, "internal") == 0) {
317 if (st
->ss
->add_internal_bitmap
== NULL
) {
318 fprintf(stderr
, Name
": Internal bitmaps not supported "
319 "with %s metadata\n", st
->ss
->name
);
322 for (d
=0; d
< st
->max_devs
; d
++) {
323 mdu_disk_info_t disk
;
326 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
328 if (disk
.major
== 0 &&
331 if ((disk
.state
& (1<<MD_DISK_SYNC
))==0)
333 dv
= map_dev(disk
.major
, disk
.minor
, 1);
335 int fd2
= dev_open(dv
, O_RDWR
);
338 if (st
->ss
->load_super(st
, fd2
, NULL
)==0) {
339 if (st
->ss
->add_internal_bitmap(
341 &chunk
, delay
, write_behind
,
342 bitmapsize
, 0, major
)
344 st
->ss
->write_bitmap(st
, fd2
);
346 fprintf(stderr
, Name
": failed to create internal bitmap - chunksize problem.\n");
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");
363 int max_devs
= st
->max_devs
;
365 /* try to load a superblock */
366 for (d
=0; d
<max_devs
; d
++) {
367 mdu_disk_info_t disk
;
371 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
373 if ((disk
.major
==0 && disk
.minor
==0) ||
374 (disk
.state
& (1<<MD_DISK_REMOVED
)))
376 dv
= map_dev(disk
.major
, disk
.minor
, 1);
378 fd2
= dev_open(dv
, O_RDONLY
);
380 st
->ss
->load_super(st
, fd2
, NULL
) == 0) {
382 st
->ss
->uuid_from_super(st
, uuid
);
388 fprintf(stderr
, Name
": cannot find UUID for array!\n");
391 if (CreateBitmap(file
, force
, (char*)uuid
, chunk
,
392 delay
, write_behind
, bitmapsize
, major
)) {
395 bitmap_fd
= open(file
, O_RDWR
);
397 fprintf(stderr
, Name
": weird: %s cannot be opened\n",
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
));
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
417 * It if written after the backup is complete.
418 * It has the following structure.
421 static struct mdp_backup_super
{
422 char magic
[16]; /* md_backup_data-1 or -2 */
425 /* start/sizes in 512byte sectors */
426 __u64 devstart
; /* address on backup device/file of data */
429 __u32 sb_csum
; /* csum of preceeding bytes. */
431 __u64 devstart2
; /* offset in to data of second section */
434 __u32 sb_csum2
; /* csum of preceeding bytes. */
436 } __attribute__((aligned(512))) bsb
, bsb2
;
438 __u32
bsb_csum(char *buf
, int len
)
442 for (i
=0; i
<len
; i
++)
443 csum
= (csum
<<3) + buf
[0];
444 return __cpu_to_le32(csum
);
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
);
461 int freeze_array(struct mdinfo
*sra
)
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.
469 if (sysfs_get_str(sra
, NULL
, "sync_action", buf
, 20) <= 0)
471 if (strcmp(buf
, "idle\n") != 0 &&
472 strcmp(buf
, "frozen\n") != 0)
474 if (sysfs_set_str(sra
, NULL
, "sync_action", "frozen") < 0)
479 void unfreeze_array(struct mdinfo
*sra
, int frozen
)
481 /* If 'frozen' is 1, unfreeze the array */
483 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
486 void wait_reshape(struct mdinfo
*sra
)
488 int fd
= sysfs_get_fd(sra
, NULL
, "sync_action");
495 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
497 if (sysfs_fd_get_str(fd
, action
, 20) < 0) {
501 } while (strncmp(action
, "reshape", 7) == 0);
505 int Grow_reshape(char *devname
, int fd
, int quiet
, char *backup_file
,
507 int level
, char *layout_str
, int chunksize
, int raid_disks
)
509 /* Make some changes in the shape of an array.
510 * The kernel must support the change.
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).
516 * - change size (i.e. component_size)
518 * - change layout/chunksize/ndisks
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)
526 struct mdu_array_info_s array
, orig
;
529 struct supertype
*st
;
530 char *subarray
= NULL
;
533 int nlayout
, olayout
;
535 unsigned int ndata
, odata
;
536 int orig_level
= UnSet
;
539 unsigned long long *offsets
;
544 unsigned long a
,b
, blocks
, stripes
;
546 unsigned long long array_size
;
553 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) < 0) {
554 fprintf(stderr
, Name
": %s is not an active md array - aborting\n",
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");
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");
575 sra
= sysfs_read(fd
, 0, GET_LEVEL
);
577 frozen
= freeze_array(sra
);
579 fprintf(stderr
, Name
": failed to read sysfs parameters for %s\n",
584 fprintf(stderr
, Name
": %s is performing resync/recovery and cannot"
585 " be reshaped\n", devname
);
589 /* ========= set size =============== */
590 if (size
>= 0 && (size
== 0 || size
!= array
.size
)) {
592 if (array
.size
!= size
) {
593 /* got truncated to 32bit, write to
594 * component_size instead
597 rv
= sysfs_set_num(sra
, NULL
,
598 "component_size", size
);
602 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
605 fprintf(stderr
, Name
": Cannot set device size for %s: %s\n",
606 devname
, strerror(err
));
608 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
609 fprintf(stderr
, " Bitmap must be removed before size can be changed\n");
613 ioctl(fd
, GET_ARRAY_INFO
, &array
);
614 size
= get_component_size(fd
)/2;
618 fprintf(stderr
, Name
": component size of %s has been set to %lluK\n",
622 size
= get_component_size(fd
)/2;
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
638 if ((array
.level
== 6 || array
.level
== 5) &&
639 (level
== 5 || level
== 4 || level
== 0)) {
640 /* Don't change level yet, but choose intermediate
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";
651 case ALGORITHM_LEFT_SYMMETRIC
:
652 case ALGORITHM_LEFT_SYMMETRIC_6
:
653 case ALGORITHM_ROTATING_N_CONTINUE
:
654 layout_str
= "left-symmetric-6";
656 case ALGORITHM_RIGHT_ASYMMETRIC
:
657 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
658 case ALGORITHM_ROTATING_ZERO_RESTART
:
659 layout_str
= "right-asymmetric-6";
661 case ALGORITHM_RIGHT_SYMMETRIC
:
662 case ALGORITHM_RIGHT_SYMMETRIC_6
:
663 layout_str
= "right-symmetric-6";
665 case ALGORITHM_PARITY_0
:
666 case ALGORITHM_PARITY_0_6
:
667 layout_str
= "parity-first-6";
669 case ALGORITHM_PARITY_N
:
670 layout_str
= "parity-last";
673 fprintf(stderr
, Name
": %s: cannot"
674 "convert layout to RAID5 equivalent\n",
680 int l
= map_name(r5layout
, layout_str
);
682 fprintf(stderr
, Name
": %s: layout '%s' not recognised\n",
683 devname
, layout_str
);
687 if (l
!= ALGORITHM_PARITY_N
) {
688 /* need the -6 version */
689 char *ls
= map_num(r5layout
, l
);
690 strcat(strcpy(alt_layout
, ls
),
692 layout_str
= alt_layout
;
696 /* The final raid6->raid5 conversion
697 * will reduce the number of disks,
698 * so now we need to aim higher
702 layout_str
= "parity-last";
704 c
= map_num(pers
, level
);
706 rv
= 1;/* not possible */
709 err
= sysfs_set_str(sra
, NULL
, "level", c
);
712 fprintf(stderr
, Name
": %s: could not set level to %s\n",
715 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
716 fprintf(stderr
, " Bitmap must be removed before level can be changed\n");
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
);
728 fprintf(stderr
, Name
" level of %s changed to %s\n",
734 /* ========= set shape (chunk_size / layout / ndisks) ============== */
735 /* Check if layout change is a no-op */
736 if (layout_str
) switch(array
.level
) {
738 if (array
.layout
== map_name(r5layout
, layout_str
))
742 if (layout_str
== NULL
&&
743 ((chunksize
&& chunksize
* 1024 != array
.chunk_size
) ||
744 (raid_disks
&& raid_disks
!= array
.raid_disks
)) &&
745 array
.layout
>= 16) {
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",
754 if (strcmp(layout_str
, "normalise") == 0 ||
755 strcmp(layout_str
, "normalize") == 0) {
757 strcpy(alt_layout
, map_num(r6layout
, array
.layout
));
758 hyphen
= strrchr(alt_layout
, '-');
759 if (hyphen
&& strcmp(hyphen
, "-6") == 0) {
761 layout_str
= alt_layout
;
765 if (array
.layout
== map_name(r6layout
, layout_str
))
767 if (layout_str
&& strcmp(layout_str
, "preserve") == 0)
771 if (layout_str
== NULL
772 && (chunksize
== 0 || chunksize
*1024 == array
.chunk_size
)
773 && (raid_disks
== 0 || raid_disks
== array
.raid_disks
)) {
775 if (level
!= UnSet
&& level
!= array
.level
) {
776 /* Looks like this level change doesn't need
777 * a reshape after all.
779 c
= map_num(pers
, level
);
781 rv
= sysfs_set_str(sra
, NULL
, "level", c
);
784 fprintf(stderr
, Name
": %s: could not set level to %s\n",
787 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
788 fprintf(stderr
, " Bitmap must be removed before level can be changed\n");
792 } else if (!changed
&& !quiet
)
793 fprintf(stderr
, Name
": %s: no change requested\n",
798 c
= map_num(pers
, array
.level
);
799 if (c
== NULL
) c
= "-unknown-";
800 switch(array
.level
) {
801 default: /* raid0, linear, multipath cannot be reconfigured */
802 fprintf(stderr
, Name
": %s array %s cannot be reshaped.\n",
807 case LEVEL_FAULTY
: /* only 'layout' change is permitted */
809 if (chunksize
|| raid_disks
) {
810 fprintf(stderr
, Name
": %s: Cannot change chunksize or disks of a 'faulty' array\n",
815 if (layout_str
== NULL
)
816 break; /* nothing to do.... */
818 array
.layout
= parse_layout_faulty(layout_str
);
819 if (array
.layout
< 0) {
820 fprintf(stderr
, Name
": %s: layout %s not understood for 'faulty' array\n",
821 devname
, layout_str
);
825 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
826 fprintf(stderr
, Name
": Cannot set layout for %s: %s\n",
827 devname
, strerror(errno
));
830 printf("layout for %s set to %d\n", devname
, array
.layout
);
833 case 1: /* only raid_disks can each be changed. */
835 if (chunksize
|| layout_str
!= NULL
) {
836 fprintf(stderr
, Name
": %s: Cannot change chunk size or layout for a RAID1 array.\n",
841 if (raid_disks
> 0) {
842 array
.raid_disks
= raid_disks
;
843 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
844 fprintf(stderr
, Name
": Cannot set raid-devices for %s: %s\n",
845 devname
, strerror(errno
));
856 * layout/chunksize/raid_disks can be changed
857 * though the kernel may not support it all.
859 st
= super_by_fd(fd
, &subarray
);
861 fprintf(stderr
, Name
": Cannot reshape subarrays yet\n");
866 * There are three possibilities.
867 * 1/ The array will shrink.
868 * We need to ensure the reshape will pause before reaching
869 * the 'critical section'. We also need to fork and wait for
870 * that to happen. When it does we
871 * suspend/backup/complete/unfreeze
873 * 2/ The array will not change size.
874 * This requires that we keep a backup of a sliding window
875 * so that we can restore data after a crash. So we need
876 * to fork and monitor progress.
878 * 3/ The array will grow. This is relatively easy.
879 * However the kernel's restripe routines will cheerfully
880 * overwrite some early data before it is safe. So we
881 * need to make a backup of the early parts of the array
882 * and be ready to restore it if rebuild aborts very early.
884 * We backup data by writing it to one spare, or to a
885 * file which was given on command line.
887 * [FOLLOWING IS OLD AND PARTLY WRONG]
888 * So: we enumerate the devices in the array and
889 * make sure we can open all of them.
890 * Then we freeze the early part of the array and
891 * backup to the various spares.
892 * Then we request changes and start the reshape.
893 * Monitor progress until it has passed the danger zone.
894 * and finally invalidate the copied data and unfreeze the
895 * start of the array.
897 * In each case, we first make sure that storage is available
898 * for the required backup.
900 * - request the shape change.
901 * - for to handle backup etc.
903 nchunk
= ochunk
= array
.chunk_size
;
904 nlayout
= olayout
= array
.layout
;
905 ndisks
= odisks
= array
.raid_disks
;
908 nchunk
= chunksize
* 1024;
909 if (size
% chunksize
) {
910 fprintf(stderr
, Name
": component size %lluK is not"
911 " a multiple of chunksize %dK\n",
916 if (layout_str
!= NULL
)
917 switch(array
.level
) {
918 case 4: /* ignore layout */
921 nlayout
= map_name(r5layout
, layout_str
);
922 if (nlayout
== UnSet
) {
923 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
931 nlayout
= map_name(r6layout
, layout_str
);
932 if (nlayout
== UnSet
) {
933 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
940 if (raid_disks
) ndisks
= raid_disks
;
944 if (array
.level
== 6) {
945 odata
--; /* number of data disks */
949 if (odata
== ndata
&&
950 get_linux_version() < 2006032) {
951 fprintf(stderr
, Name
": in-place reshape is not safe before 2.6.32, sorry.\n");
955 /* Check that we can hold all the data */
956 get_dev_size(fd
, NULL
, &array_size
);
957 if (ndata
* (unsigned long long)size
< (array_size
/1024)) {
958 fprintf(stderr
, Name
": this change will reduce the size of the array.\n"
959 " use --grow --array-size first to truncate array.\n"
960 " e.g. mdadm --grow %s --array-size %llu\n",
961 devname
, ndata
* size
);
966 /* So how much do we need to backup.
967 * We need an amount of data which is both a whole number of
968 * old stripes and a whole number of new stripes.
969 * So LCM for (chunksize*datadisks).
971 a
= (ochunk
/512) * odata
;
972 b
= (nchunk
/512) * ndata
;
980 /* LCM == product / GCD */
981 blocks
= (ochunk
/512) * (nchunk
/512) * odata
* ndata
/ a
;
984 sra
= sysfs_read(fd
, 0,
985 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
989 fprintf(stderr
, Name
": %s: Cannot get array details from sysfs\n",
995 if (ndata
== odata
) {
996 /* Make 'blocks' bigger for better throughput, but
997 * not so big that we reject it below.
998 * Try for 16 megabytes
1000 while (blocks
* 32 < sra
->component_size
&&
1004 fprintf(stderr
, Name
": Need to backup %luK of critical "
1005 "section..\n", blocks
/2);
1007 if (blocks
>= sra
->component_size
/2) {
1008 fprintf(stderr
, Name
": %s: Something wrong - reshape aborted\n",
1013 nrdisks
= array
.raid_disks
+ sra
->array
.spare_disks
;
1014 /* Now we need to open all these devices so we can read/write.
1016 fdlist
= malloc((1+nrdisks
) * sizeof(int));
1017 offsets
= malloc((1+nrdisks
) * sizeof(offsets
[0]));
1018 if (!fdlist
|| !offsets
) {
1019 fprintf(stderr
, Name
": malloc failed: grow aborted\n");
1023 for (d
=0; d
<= nrdisks
; d
++)
1025 d
= array
.raid_disks
;
1026 for (sd
= sra
->devs
; sd
; sd
=sd
->next
) {
1027 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1029 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
1030 char *dn
= map_dev(sd
->disk
.major
,
1032 fdlist
[sd
->disk
.raid_disk
]
1033 = dev_open(dn
, O_RDONLY
);
1034 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
1035 if (fdlist
[sd
->disk
.raid_disk
] < 0) {
1036 fprintf(stderr
, Name
": %s: cannot open component %s\n",
1037 devname
, dn
?dn
:"-unknown-");
1041 } else if (backup_file
== NULL
) {
1043 char *dn
= map_dev(sd
->disk
.major
,
1045 fdlist
[d
] = dev_open(dn
, O_RDWR
);
1046 offsets
[d
] = (sd
->data_offset
+ sra
->component_size
- blocks
- 8)*512;
1048 fprintf(stderr
, Name
": %s: cannot open component %s\n",
1049 devname
, dn
?dn
:"-unknown");
1056 if (backup_file
== NULL
) {
1057 if (ndata
<= odata
) {
1058 fprintf(stderr
, Name
": %s: Cannot grow - need backup-file\n",
1062 } else if (sra
->array
.spare_disks
== 0) {
1063 fprintf(stderr
, Name
": %s: Cannot grow - need a spare or "
1064 "backup-file to backup critical section\n",
1069 if (d
== array
.raid_disks
) {
1070 fprintf(stderr
, Name
": %s: No spare device for backup\n",
1076 /* need to check backup file is large enough */
1078 fdlist
[d
] = open(backup_file
, O_RDWR
|O_CREAT
|O_EXCL
,
1080 offsets
[d
] = 8 * 512;
1081 if (fdlist
[d
] < 0) {
1082 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1083 devname
, backup_file
, strerror(errno
));
1087 memset(buf
, 0, 512);
1088 for (i
=0; i
< (signed)blocks
+ 1 ; i
++) {
1089 if (write(fdlist
[d
], buf
, 512) != 512) {
1090 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1091 devname
, backup_file
, strerror(errno
));
1096 if (fsync(fdlist
[d
]) != 0) {
1097 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1098 devname
, backup_file
, strerror(errno
));
1105 /* lastly, check that the internal stripe cache is
1106 * large enough, or it won't work.
1109 cache
= (nchunk
< ochunk
) ? ochunk
: nchunk
;
1110 cache
= cache
* 4 / 4096;
1111 if (cache
< blocks
/ 8 / odisks
+ 16)
1112 /* Make it big enough to hold 'blocks' */
1113 cache
= blocks
/ 8 / odisks
+ 16;
1114 if (sra
->cache_size
< cache
)
1115 sysfs_set_num(sra
, NULL
, "stripe_cache_size",
1117 /* Right, everything seems fine. Let's kick things off.
1118 * If only changing raid_disks, use ioctl, else use
1121 if (ochunk
== nchunk
&& olayout
== nlayout
) {
1122 array
.raid_disks
= ndisks
;
1123 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
1126 fprintf(stderr
, Name
": Cannot set device shape for %s: %s\n",
1127 devname
, strerror(errno
));
1128 if (ndisks
< odisks
&&
1129 get_linux_version() < 2006030)
1130 fprintf(stderr
, Name
": linux 2.6.30 or later required\n");
1132 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1133 fprintf(stderr
, " Bitmap must be removed before shape can be changed\n");
1138 /* set them all just in case some old 'new_*' value
1139 * persists from some earlier problem
1141 int err
= err
; /* only used if rv==1, and always set if
1142 * rv==1, so initialisation not needed,
1143 * despite gcc warning
1145 if (sysfs_set_num(sra
, NULL
, "chunk_size", nchunk
) < 0)
1146 rv
= 1, err
= errno
;
1147 if (!rv
&& sysfs_set_num(sra
, NULL
, "layout", nlayout
) < 0)
1148 rv
= 1, err
= errno
;
1149 if (!rv
&& sysfs_set_num(sra
, NULL
, "raid_disks", ndisks
) < 0)
1150 rv
= 1, err
= errno
;
1152 fprintf(stderr
, Name
": Cannot set device shape for %s\n",
1154 if (get_linux_version() < 2006030)
1155 fprintf(stderr
, Name
": linux 2.6.30 or later required\n");
1157 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1158 fprintf(stderr
, " Bitmap must be removed before shape can be changed\n");
1163 if (ndisks
== 2 && odisks
== 2) {
1164 /* No reshape is needed in this trivial case */
1169 /* set up the backup-super-block. This requires the
1170 * uuid from the array.
1172 /* Find a superblock */
1173 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1177 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1179 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
1180 devfd
= dev_open(dn
, O_RDONLY
);
1183 ok
= st
->ss
->load_super(st
, devfd
, NULL
);
1189 fprintf(stderr
, Name
": %s: Cannot find a superblock\n",
1195 memset(&bsb
, 0, 512);
1196 memcpy(bsb
.magic
, "md_backup_data-1", 16);
1197 st
->ss
->uuid_from_super(st
, (int*)&bsb
.set_uuid
);
1198 bsb
.mtime
= __cpu_to_le64(time(0));
1199 bsb
.devstart2
= blocks
;
1200 stripes
= blocks
/ (ochunk
/512) / odata
;
1201 /* Now we just need to kick off the reshape and watch, while
1202 * handling backups of the data...
1203 * This is all done by a forked background process.
1208 if (check_env("MDADM_GROW_VERIFY"))
1209 fd
= open(devname
, O_RDONLY
| O_DIRECT
);
1212 mlockall(MCL_FUTURE
);
1215 done
= child_grow(fd
, sra
, stripes
,
1217 odisks
, ochunk
, array
.level
, olayout
, odata
,
1218 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1219 else if (odata
> ndata
)
1220 done
= child_shrink(fd
, sra
, stripes
,
1222 odisks
, ochunk
, array
.level
, olayout
, odata
,
1223 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1225 done
= child_same_size(fd
, sra
, stripes
,
1228 odisks
, ochunk
, array
.level
, olayout
, odata
,
1229 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1230 if (backup_file
&& done
)
1231 unlink(backup_file
);
1232 if (level
!= UnSet
&& level
!= array
.level
) {
1233 /* We need to wait for the reshape to finish
1234 * (which will have happened unless odata < ndata)
1235 * and then set the level
1238 c
= map_num(pers
, level
);
1240 exit(0);/* not possible */
1244 err
= sysfs_set_str(sra
, NULL
, "level", c
);
1246 fprintf(stderr
, Name
": %s: could not set level to %s\n",
1251 fprintf(stderr
, Name
": Cannot run child to monitor reshape: %s\n",
1256 /* The child will take care of unfreezing the array */
1265 if (rv
&& orig_level
!= UnSet
&& sra
) {
1266 c
= map_num(pers
, orig_level
);
1267 if (c
&& sysfs_set_str(sra
, NULL
, "level", c
) == 0)
1268 fprintf(stderr
, Name
": aborting level change\n");
1271 unfreeze_array(sra
, frozen
);
1276 * We run a child process in the background which performs the following
1278 * - wait for resync to reach a certain point
1279 * - suspend io to the following section
1280 * - backup that section
1281 * - allow resync to proceed further
1283 * - discard the backup.
1285 * When are combined in slightly different ways in the three cases.
1287 * - suspend/backup/allow/wait/resume/discard
1289 * - allow/wait/suspend/backup/allow/wait/resume/discard
1291 * - wait/resume/discard/suspend/backup/allow
1293 * suspend/backup/allow always come together
1294 * wait/resume/discard do too.
1295 * For the same-size case we have two backups to improve flow.
1299 /* FIXME return status is never checked */
1300 int grow_backup(struct mdinfo
*sra
,
1301 unsigned long long offset
, /* per device */
1302 unsigned long stripes
, /* per device */
1303 int *sources
, unsigned long long *offsets
,
1304 int disks
, int chunk
, int level
, int layout
,
1305 int dests
, int *destfd
, unsigned long long *destoffsets
,
1306 int part
, int *degraded
,
1309 /* Backup 'blocks' sectors at 'offset' on each device of the array,
1310 * to storage 'destfd' (offset 'destoffsets'), after first
1311 * suspending IO. Then allow resync to continue
1312 * over the suspended section.
1313 * Use part 'part' of the backup-super-block.
1318 unsigned long long ll
;
1320 //printf("offset %llu\n", offset);
1325 sysfs_set_num(sra
, NULL
, "suspend_hi", (offset
+ stripes
* (chunk
/512)) * odata
);
1326 /* Check that array hasn't become degraded, else we might backup the wrong data */
1327 sysfs_get_ll(sra
, NULL
, "degraded", &ll
);
1328 new_degraded
= (int)ll
;
1329 if (new_degraded
!= *degraded
) {
1330 /* check each device to ensure it is still working */
1332 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1333 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1335 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
1337 if (sysfs_get_str(sra
, sd
, "state", sbuf
, 20) < 0 ||
1338 strstr(sbuf
, "faulty") ||
1339 strstr(sbuf
, "in_sync") == NULL
) {
1340 /* this device is dead */
1341 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
1342 if (sd
->disk
.raid_disk
>= 0 &&
1343 sources
[sd
->disk
.raid_disk
] >= 0) {
1344 close(sources
[sd
->disk
.raid_disk
]);
1345 sources
[sd
->disk
.raid_disk
] = -1;
1350 *degraded
= new_degraded
;
1353 bsb
.arraystart2
= __cpu_to_le64(offset
* odata
);
1354 bsb
.length2
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
1356 bsb
.arraystart
= __cpu_to_le64(offset
* odata
);
1357 bsb
.length
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
1360 bsb
.magic
[15] = '2';
1361 for (i
= 0; i
< dests
; i
++)
1363 lseek64(destfd
[i
], destoffsets
[i
] + __le64_to_cpu(bsb
.devstart2
)*512, 0);
1365 lseek64(destfd
[i
], destoffsets
[i
], 0);
1367 rv
= save_stripes(sources
, offsets
,
1368 disks
, chunk
, level
, layout
,
1370 offset
*512*odata
, stripes
* chunk
* odata
,
1375 bsb
.mtime
= __cpu_to_le64(time(0));
1376 for (i
= 0; i
< dests
; i
++) {
1377 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
1379 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
1380 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
1381 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
1382 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
1385 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
] - 4096, 0)
1386 != destoffsets
[i
] - 4096)
1388 if (write(destfd
[i
], &bsb
, 512) != 512)
1390 if (destoffsets
[i
] > 4096) {
1391 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]+stripes
*chunk
*odata
, 0) !=
1392 destoffsets
[i
]+stripes
*chunk
*odata
)
1394 if (write(destfd
[i
], &bsb
, 512) != 512)
1404 /* in 2.6.30, the value reported by sync_completed can be
1405 * less that it should be by one stripe.
1406 * This only happens when reshape hits sync_max and pauses.
1407 * So allow wait_backup to either extent sync_max further
1408 * than strictly necessary, or return before the
1409 * sync has got quite as far as we would really like.
1410 * This is what 'blocks2' is for.
1411 * The various caller give appropriate values so that
1414 /* FIXME return value is often ignored */
1415 int wait_backup(struct mdinfo
*sra
,
1416 unsigned long long offset
, /* per device */
1417 unsigned long long blocks
, /* per device */
1418 unsigned long long blocks2
, /* per device - hack */
1419 int dests
, int *destfd
, unsigned long long *destoffsets
,
1422 /* Wait for resync to pass the section that was backed up
1423 * then erase the backup and allow IO
1425 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
1426 unsigned long long completed
;
1432 sysfs_set_num(sra
, NULL
, "sync_max", offset
+ blocks
+ blocks2
);
1434 sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
1440 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
1441 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
1445 if (sysfs_get_str(sra
, NULL
, "sync_action",
1447 strncmp(action
, "reshape", 7) != 0)
1449 } while (completed
< offset
+ blocks
);
1453 bsb
.arraystart2
= __cpu_to_le64(0);
1454 bsb
.length2
= __cpu_to_le64(0);
1456 bsb
.arraystart
= __cpu_to_le64(0);
1457 bsb
.length
= __cpu_to_le64(0);
1459 bsb
.mtime
= __cpu_to_le64(time(0));
1461 for (i
= 0; i
< dests
; i
++) {
1462 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
1463 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
1464 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
1465 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
1466 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
1467 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]-4096, 0) !=
1468 destoffsets
[i
]-4096)
1471 write(destfd
[i
], &bsb
, 512) != 512)
1478 static void fail(char *msg
)
1481 rv
= (write(2, msg
, strlen(msg
)) != (int)strlen(msg
));
1482 rv
|= (write(2, "\n", 1) != 1);
1486 static char *abuf
, *bbuf
;
1487 static unsigned long long abuflen
;
1488 static void validate(int afd
, int bfd
, unsigned long long offset
)
1490 /* check that the data in the backup against the array.
1491 * This is only used for regression testing and should not
1492 * be used while the array is active
1496 lseek64(bfd
, offset
- 4096, 0);
1497 if (read(bfd
, &bsb2
, 512) != 512)
1498 fail("cannot read bsb");
1499 if (bsb2
.sb_csum
!= bsb_csum((char*)&bsb2
,
1500 ((char*)&bsb2
.sb_csum
)-((char*)&bsb2
)))
1501 fail("first csum bad");
1502 if (memcmp(bsb2
.magic
, "md_backup_data", 14) != 0)
1503 fail("magic is bad");
1504 if (memcmp(bsb2
.magic
, "md_backup_data-2", 16) == 0 &&
1505 bsb2
.sb_csum2
!= bsb_csum((char*)&bsb2
,
1506 ((char*)&bsb2
.sb_csum2
)-((char*)&bsb2
)))
1507 fail("second csum bad");
1509 if (__le64_to_cpu(bsb2
.devstart
)*512 != offset
)
1510 fail("devstart is wrong");
1513 unsigned long long len
= __le64_to_cpu(bsb2
.length
)*512;
1515 if (abuflen
< len
) {
1519 if (posix_memalign((void**)&abuf
, 4096, abuflen
) ||
1520 posix_memalign((void**)&bbuf
, 4096, abuflen
)) {
1522 /* just stop validating on mem-alloc failure */
1527 lseek64(bfd
, offset
, 0);
1528 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
) {
1529 //printf("len %llu\n", len);
1530 fail("read first backup failed");
1532 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart
)*512, 0);
1533 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
1534 fail("read first from array failed");
1535 if (memcmp(bbuf
, abuf
, len
) != 0) {
1538 printf("offset=%llu len=%llu\n",
1539 (unsigned long long)__le64_to_cpu(bsb2
.arraystart
)*512, len
);
1540 for (i
=0; i
<len
; i
++)
1541 if (bbuf
[i
] != abuf
[i
]) {
1542 printf("first diff byte %d\n", i
);
1546 fail("data1 compare failed");
1550 unsigned long long len
= __le64_to_cpu(bsb2
.length2
)*512;
1552 if (abuflen
< len
) {
1556 abuf
= malloc(abuflen
);
1557 bbuf
= malloc(abuflen
);
1560 lseek64(bfd
, offset
+__le64_to_cpu(bsb2
.devstart2
)*512, 0);
1561 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
)
1562 fail("read second backup failed");
1563 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart2
)*512, 0);
1564 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
1565 fail("read second from array failed");
1566 if (memcmp(bbuf
, abuf
, len
) != 0)
1567 fail("data2 compare failed");
1571 static int child_grow(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1572 int *fds
, unsigned long long *offsets
,
1573 int disks
, int chunk
, int level
, int layout
, int data
,
1574 int dests
, int *destfd
, unsigned long long *destoffsets
)
1579 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
1580 /* Don't start the 'reshape' */
1582 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1583 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1584 grow_backup(sra
, 0, stripes
,
1585 fds
, offsets
, disks
, chunk
, level
, layout
,
1586 dests
, destfd
, destoffsets
,
1588 validate(afd
, destfd
[0], destoffsets
[0]);
1589 wait_backup(sra
, 0, stripes
* (chunk
/ 512), stripes
* (chunk
/ 512),
1590 dests
, destfd
, destoffsets
,
1592 sysfs_set_num(sra
, NULL
, "suspend_lo", (stripes
* (chunk
/512)) * data
);
1594 /* FIXME this should probably be numeric */
1595 sysfs_set_str(sra
, NULL
, "sync_max", "max");
1599 static int child_shrink(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1600 int *fds
, unsigned long long *offsets
,
1601 int disks
, int chunk
, int level
, int layout
, int data
,
1602 int dests
, int *destfd
, unsigned long long *destoffsets
)
1605 unsigned long long start
;
1609 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
1611 start
= sra
->component_size
- stripes
* (chunk
/512);
1612 sysfs_set_num(sra
, NULL
, "sync_max", start
);
1613 sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
1614 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1615 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1616 rv
= wait_backup(sra
, 0, start
- stripes
* (chunk
/512), stripes
* (chunk
/512),
1617 dests
, destfd
, destoffsets
, 0);
1620 grow_backup(sra
, 0, stripes
,
1622 disks
, chunk
, level
, layout
,
1623 dests
, destfd
, destoffsets
,
1625 validate(afd
, destfd
[0], destoffsets
[0]);
1626 wait_backup(sra
, start
, stripes
*(chunk
/512), 0,
1627 dests
, destfd
, destoffsets
, 0);
1628 sysfs_set_num(sra
, NULL
, "suspend_lo", (stripes
* (chunk
/512)) * data
);
1630 /* FIXME this should probably be numeric */
1631 sysfs_set_str(sra
, NULL
, "sync_max", "max");
1635 static int child_same_size(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1636 int *fds
, unsigned long long *offsets
,
1637 unsigned long long start
,
1638 int disks
, int chunk
, int level
, int layout
, int data
,
1639 int dests
, int *destfd
, unsigned long long *destoffsets
)
1641 unsigned long long size
;
1642 unsigned long tailstripes
= stripes
;
1645 unsigned long long speed
;
1649 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
1652 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1653 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1655 sysfs_get_ll(sra
, NULL
, "sync_speed_min", &speed
);
1656 sysfs_set_num(sra
, NULL
, "sync_speed_min", 200000);
1658 grow_backup(sra
, start
, stripes
,
1660 disks
, chunk
, level
, layout
,
1661 dests
, destfd
, destoffsets
,
1663 grow_backup(sra
, (start
+ stripes
) * (chunk
/512), stripes
,
1665 disks
, chunk
, level
, layout
,
1666 dests
, destfd
, destoffsets
,
1668 validate(afd
, destfd
[0], destoffsets
[0]);
1670 start
+= stripes
* 2; /* where to read next */
1671 size
= sra
->component_size
/ (chunk
/512);
1672 while (start
< size
) {
1673 if (wait_backup(sra
, (start
-stripes
*2)*(chunk
/512),
1674 stripes
*(chunk
/512), 0,
1675 dests
, destfd
, destoffsets
,
1678 sysfs_set_num(sra
, NULL
, "suspend_lo", start
*(chunk
/512) * data
);
1679 if (start
+ stripes
> size
)
1680 tailstripes
= (size
- start
);
1682 grow_backup(sra
, start
*(chunk
/512), tailstripes
,
1684 disks
, chunk
, level
, layout
,
1685 dests
, destfd
, destoffsets
,
1686 part
, °raded
, buf
);
1689 validate(afd
, destfd
[0], destoffsets
[0]);
1691 if (wait_backup(sra
, (start
-stripes
*2) * (chunk
/512), stripes
* (chunk
/512), 0,
1692 dests
, destfd
, destoffsets
,
1695 sysfs_set_num(sra
, NULL
, "suspend_lo", ((start
-stripes
)*(chunk
/512)) * data
);
1696 wait_backup(sra
, (start
-stripes
) * (chunk
/512), tailstripes
* (chunk
/512), 0,
1697 dests
, destfd
, destoffsets
,
1699 sysfs_set_num(sra
, NULL
, "suspend_lo", (size
*(chunk
/512)) * data
);
1700 sysfs_set_num(sra
, NULL
, "sync_speed_min", speed
);
1706 * If any spare contains md_back_data-1 which is recent wrt mtime,
1707 * write that data into the array and update the super blocks with
1708 * the new reshape_progress
1710 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
, int cnt
,
1711 char *backup_file
, int verbose
)
1715 unsigned long long *offsets
;
1716 unsigned long long nstripe
, ostripe
;
1719 if (info
->new_level
!= info
->array
.level
)
1720 return 1; /* Cannot handle level changes (they are instantaneous) */
1722 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
1723 if (info
->array
.level
== 6) odata
--; /* number of data disks */
1724 ndata
= info
->array
.raid_disks
- 1;
1725 if (info
->new_level
== 6) ndata
--;
1727 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
1729 if (info
->delta_disks
<= 0)
1730 /* Didn't grow, so the backup file must have
1734 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
1735 struct mdinfo dinfo
;
1738 char *devname
, namebuf
[20];
1740 /* This was a spare and may have some saved data on it.
1741 * Load the superblock, find and load the
1742 * backup_super_block.
1743 * If either fail, go on to next device.
1744 * If the backup contains no new info, just return
1745 * else restore data and update all superblocks
1747 if (i
== old_disks
-1) {
1748 fd
= open(backup_file
, O_RDONLY
);
1750 fprintf(stderr
, Name
": backup file %s inaccessible: %s\n",
1751 backup_file
, strerror(errno
));
1754 devname
= backup_file
;
1759 if (st
->ss
->load_super(st
, fd
, NULL
))
1762 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
1763 st
->ss
->free_super(st
);
1766 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
1768 fprintf(stderr
, Name
": Cannot seek on device %d\n", i
);
1769 continue; /* Cannot seek */
1771 sprintf(namebuf
, "device-%d", i
);
1774 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
)) {
1776 fprintf(stderr
, Name
": Cannot read from %s\n", devname
);
1777 continue; /* Cannot read */
1779 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0 &&
1780 memcmp(bsb
.magic
, "md_backup_data-2", 16) != 0) {
1782 fprintf(stderr
, Name
": No backup metadata on %s\n", devname
);
1785 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
))) {
1787 fprintf(stderr
, Name
": Bad backup-metadata checksum on %s\n", devname
);
1788 continue; /* bad checksum */
1790 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0 &&
1791 bsb
.sb_csum2
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum2
)-((char*)&bsb
))) {
1793 fprintf(stderr
, Name
": Bad backup-metadata checksum2 on %s\n", devname
);
1794 continue; /* Bad second checksum */
1796 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0) {
1798 fprintf(stderr
, Name
": Wrong uuid on backup-metadata on %s\n", devname
);
1799 continue; /* Wrong uuid */
1802 /* array utime and backup-mtime should be updated at much the same time, but it seems that
1803 * sometimes they aren't... So allow considerable flexability in matching, and allow
1804 * this test to be overridden by an environment variable.
1806 if (info
->array
.utime
> (int)__le64_to_cpu(bsb
.mtime
) + 2*60*60 ||
1807 info
->array
.utime
< (int)__le64_to_cpu(bsb
.mtime
) - 10*60) {
1808 if (check_env("MDADM_GROW_ALLOW_OLD")) {
1809 fprintf(stderr
, Name
": accepting backup with timestamp %lu "
1810 "for array with timestamp %lu\n",
1811 (unsigned long)__le64_to_cpu(bsb
.mtime
),
1812 (unsigned long)info
->array
.utime
);
1815 fprintf(stderr
, Name
": too-old timestamp on "
1816 "backup-metadata on %s\n", devname
);
1817 continue; /* time stamp is too bad */
1821 if (bsb
.magic
[15] == '1') {
1822 if (info
->delta_disks
>= 0) {
1823 /* reshape_progress is increasing */
1824 if (__le64_to_cpu(bsb
.arraystart
) + __le64_to_cpu(bsb
.length
) <
1825 info
->reshape_progress
) {
1828 fprintf(stderr
, Name
": backup-metadata found on %s but is not needed\n", devname
);
1829 continue; /* No new data here */
1832 /* reshape_progress is decreasing */
1833 if (__le64_to_cpu(bsb
.arraystart
) >=
1834 info
->reshape_progress
)
1835 goto nonew
; /* No new data here */
1838 if (info
->delta_disks
>= 0) {
1839 /* reshape_progress is increasing */
1840 if (__le64_to_cpu(bsb
.arraystart
) + __le64_to_cpu(bsb
.length
) <
1841 info
->reshape_progress
&&
1842 __le64_to_cpu(bsb
.arraystart2
) + __le64_to_cpu(bsb
.length2
) <
1843 info
->reshape_progress
)
1844 goto nonew
; /* No new data here */
1846 /* reshape_progress is decreasing */
1847 if (__le64_to_cpu(bsb
.arraystart
) >=
1848 info
->reshape_progress
&&
1849 __le64_to_cpu(bsb
.arraystart2
) >=
1850 info
->reshape_progress
)
1851 goto nonew
; /* No new data here */
1854 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0) {
1857 fprintf(stderr
, Name
": Failed to verify secondary backup-metadata block on %s\n",
1859 continue; /* Cannot seek */
1861 /* There should be a duplicate backup superblock 4k before here */
1862 if (lseek64(fd
, -4096, 1) < 0 ||
1863 read(fd
, &bsb2
, sizeof(bsb2
)) != sizeof(bsb2
))
1864 goto second_fail
; /* Cannot find leading superblock */
1865 if (bsb
.magic
[15] == '1')
1866 bsbsize
= offsetof(struct mdp_backup_super
, pad1
);
1868 bsbsize
= offsetof(struct mdp_backup_super
, pad
);
1869 if (memcmp(&bsb2
, &bsb
, bsbsize
) != 0)
1870 goto second_fail
; /* Cannot find leading superblock */
1872 /* Now need the data offsets for all devices. */
1873 offsets
= malloc(sizeof(*offsets
)*info
->array
.raid_disks
);
1874 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
1877 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
1878 /* FIXME should be this be an error */
1880 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
1881 st
->ss
->free_super(st
);
1882 offsets
[j
] = dinfo
.data_offset
* 512;
1884 printf(Name
": restoring critical section\n");
1886 if (restore_stripes(fdlist
, offsets
,
1887 info
->array
.raid_disks
,
1891 fd
, __le64_to_cpu(bsb
.devstart
)*512,
1892 __le64_to_cpu(bsb
.arraystart
)*512,
1893 __le64_to_cpu(bsb
.length
)*512)) {
1894 /* didn't succeed, so giveup */
1896 fprintf(stderr
, Name
": Error restoring backup from %s\n",
1901 if (bsb
.magic
[15] == '2' &&
1902 restore_stripes(fdlist
, offsets
,
1903 info
->array
.raid_disks
,
1907 fd
, __le64_to_cpu(bsb
.devstart
)*512 +
1908 __le64_to_cpu(bsb
.devstart2
)*512,
1909 __le64_to_cpu(bsb
.arraystart2
)*512,
1910 __le64_to_cpu(bsb
.length2
)*512)) {
1911 /* didn't succeed, so giveup */
1913 fprintf(stderr
, Name
": Error restoring second backup from %s\n",
1919 /* Ok, so the data is restored. Let's update those superblocks. */
1921 if (info
->delta_disks
>= 0) {
1922 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
) +
1923 __le64_to_cpu(bsb
.length
);
1924 if (bsb
.magic
[15] == '2') {
1925 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
) +
1926 __le64_to_cpu(bsb
.length2
);
1927 if (p2
> info
->reshape_progress
)
1928 info
->reshape_progress
= p2
;
1931 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
);
1932 if (bsb
.magic
[15] == '2') {
1933 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
);
1934 if (p2
< info
->reshape_progress
)
1935 info
->reshape_progress
= p2
;
1938 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
1939 if (fdlist
[j
] < 0) continue;
1940 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
1942 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
1943 dinfo
.reshape_progress
= info
->reshape_progress
;
1944 st
->ss
->update_super(st
, &dinfo
,
1945 "_reshape_progress",
1947 st
->ss
->store_super(st
, fdlist
[j
]);
1948 st
->ss
->free_super(st
);
1952 /* Didn't find any backup data, try to see if any
1955 if (info
->delta_disks
< 0) {
1956 /* When shrinking, the critical section is at the end.
1957 * So see if we are before the critical section.
1959 unsigned long long first_block
;
1960 nstripe
= ostripe
= 0;
1962 while (ostripe
>= nstripe
) {
1963 ostripe
+= info
->array
.chunk_size
/ 512;
1964 first_block
= ostripe
* odata
;
1965 nstripe
= first_block
/ ndata
/ (info
->new_chunk
/512) *
1966 (info
->new_chunk
/512);
1969 if (info
->reshape_progress
>= first_block
)
1972 if (info
->delta_disks
> 0) {
1973 /* See if we are beyond the critical section. */
1974 unsigned long long last_block
;
1975 nstripe
= ostripe
= 0;
1977 while (nstripe
>= ostripe
) {
1978 nstripe
+= info
->new_chunk
/ 512;
1979 last_block
= nstripe
* ndata
;
1980 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
1981 (info
->array
.chunk_size
/512);
1984 if (info
->reshape_progress
>= last_block
)
1987 /* needed to recover critical section! */
1989 fprintf(stderr
, Name
": Failed to find backup of critical section\n");
1993 int Grow_continue(int mdfd
, struct supertype
*st
, struct mdinfo
*info
,
1996 /* Array is assembled and ready to be started, but
1997 * monitoring is probably required.
2000 * - set upper bound for resync
2001 * - initialise the 'suspend' boundaries
2002 * - switch to read-write
2003 * - fork and continue monitoring
2007 unsigned long long backup_offsets
[1];
2008 int odisks
, ndisks
, ochunk
, nchunk
,odata
,ndata
;
2009 unsigned long a
,b
,blocks
,stripes
;
2012 unsigned long long *offsets
;
2014 struct mdinfo
*sra
, *sd
;
2016 unsigned long cache
;
2019 err
= sysfs_set_str(info
, NULL
, "array_state", "readonly");
2023 /* make sure reshape doesn't progress until we are ready */
2024 sysfs_set_str(info
, NULL
, "sync_max", "0");
2025 sysfs_set_str(info
, NULL
, "array_state", "active"); /* FIXME or clean */
2027 sra
= sysfs_read(-1, devname2devnum(info
->sys_name
),
2028 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
2033 /* ndisks is not growing, so raid_disks is old and +delta is new */
2034 odisks
= info
->array
.raid_disks
;
2035 ndisks
= odisks
+ info
->delta_disks
;
2038 if (info
->array
.level
== 6) {
2042 ochunk
= info
->array
.chunk_size
;
2043 nchunk
= info
->new_chunk
;
2045 a
= (ochunk
/512) * odata
;
2046 b
= (nchunk
/512) * ndata
;
2054 /* LCM == product / GCD */
2055 blocks
= (ochunk
/512) * (nchunk
/512) * odata
* ndata
/ a
;
2058 while (blocks
* 32 < sra
->component_size
&&
2061 stripes
= blocks
/ (info
->array
.chunk_size
/512) / odata
;
2063 /* check that the internal stripe cache is
2064 * large enough, or it won't work.
2066 cache
= (nchunk
< ochunk
) ? ochunk
: nchunk
;
2067 cache
= cache
* 4 / 4096;
2068 if (cache
< blocks
/ 8 / odisks
+ 16)
2069 /* Make it big enough to hold 'blocks' */
2070 cache
= blocks
/ 8 / odisks
+ 16;
2071 if (sra
->cache_size
< cache
)
2072 sysfs_set_num(sra
, NULL
, "stripe_cache_size",
2075 memset(&bsb
, 0, 512);
2076 memcpy(bsb
.magic
, "md_backup_data-1", 16);
2077 memcpy(&bsb
.set_uuid
, info
->uuid
, 16);
2078 bsb
.mtime
= __cpu_to_le64(time(0));
2079 bsb
.devstart2
= blocks
;
2081 backup_fd
= open(backup_file
, O_RDWR
|O_CREAT
, S_IRUSR
| S_IWUSR
);
2082 backup_list
[0] = backup_fd
;
2083 backup_offsets
[0] = 8 * 512;
2084 fds
= malloc(odisks
* sizeof(fds
[0]));
2085 offsets
= malloc(odisks
* sizeof(offsets
[0]));
2086 for (d
=0; d
<odisks
; d
++)
2089 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2090 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2092 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
2093 char *dn
= map_dev(sd
->disk
.major
,
2095 fds
[sd
->disk
.raid_disk
]
2096 = dev_open(dn
, O_RDONLY
);
2097 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
2098 if (fds
[sd
->disk
.raid_disk
] < 0) {
2099 fprintf(stderr
, Name
": %s: cannot open component %s\n",
2100 info
->sys_name
, dn
?dn
:"-unknown-");
2111 mlockall(MCL_FUTURE
);
2112 if (info
->delta_disks
< 0)
2113 done
= child_shrink(-1, info
, stripes
,
2115 info
->array
.raid_disks
,
2116 info
->array
.chunk_size
,
2117 info
->array
.level
, info
->array
.layout
,
2119 1, backup_list
, backup_offsets
);
2120 else if (info
->delta_disks
== 0) {
2121 /* The 'start' is a per-device stripe number.
2122 * reshape_progress is a per-array sector number.
2123 * So divide by ndata * chunk_size
2125 unsigned long long start
= info
->reshape_progress
/ ndata
;
2126 start
/= (info
->array
.chunk_size
/512);
2127 done
= child_same_size(-1, info
, stripes
,
2130 info
->array
.raid_disks
,
2131 info
->array
.chunk_size
,
2132 info
->array
.level
, info
->array
.layout
,
2134 1, backup_list
, backup_offsets
);
2136 if (backup_file
&& done
)
2137 unlink(backup_file
);
2138 /* FIXME should I intuit a level change */
2141 fprintf(stderr
, Name
": Cannot run child to continue monitoring reshape: %s\n",