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
;
56 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
57 fprintf(stderr
, Name
": cannot get array info for %s\n", devname
);
63 fprintf(stderr
, Name
": cannot handle arrays with superblock version %d\n", info
.array
.major_version
);
67 if (info
.array
.level
!= -1) {
68 fprintf(stderr
, Name
": can only add devices to linear arrays\n");
72 nfd
= open(newdev
, O_RDWR
|O_EXCL
|O_DIRECT
);
74 fprintf(stderr
, Name
": cannot open %s\n", newdev
);
78 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
79 fprintf(stderr
, Name
": %s is not a block device!\n", newdev
);
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
++) {
89 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
90 fprintf(stderr
, Name
": cannot get device detail for device %d\n",
94 dv
= map_dev(disk
.major
, disk
.minor
, 1);
96 fprintf(stderr
, Name
": cannot find device file for device %d\n",
100 fd2
= dev_open(dv
, O_RDWR
);
102 fprintf(stderr
, Name
": cannot open device file %s\n", dv
);
105 st
->ss
->free_super(st
);
107 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
108 fprintf(stderr
, Name
": cannot find super block on %s\n", dv
);
114 /* Ok, looks good. Lets update the superblock and write it out to
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
,
126 if (st
->ss
->store_super(st
, nfd
)) {
127 fprintf(stderr
, Name
": Cannot store new superblock on %s\n",
134 if (ioctl(fd
, ADD_NEW_DISK
, &info
.disk
) != 0) {
135 fprintf(stderr
, Name
": Cannot add new disk to this array\n");
138 /* Well, that seems to have worked.
139 * Now go through and update all superblocks
142 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
143 fprintf(stderr
, Name
": cannot get array info for %s\n", devname
);
148 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
149 mdu_disk_info_t disk
;
153 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
154 fprintf(stderr
, Name
": cannot get device detail for device %d\n",
158 dv
= map_dev(disk
.major
, disk
.minor
, 1);
160 fprintf(stderr
, Name
": cannot find device file for device %d\n",
164 fd2
= dev_open(dv
, O_RDWR
);
166 fprintf(stderr
, Name
": cannot open device file %s\n", dv
);
169 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
170 fprintf(stderr
, Name
": cannot find super block on %s\n", dv
);
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;
179 st
->ss
->update_super(st
, &info
, "linear-grow-update", dv
,
182 if (st
->ss
->store_super(st
, fd2
)) {
183 fprintf(stderr
, Name
": Cannot store new superblock on %s\n", dv
);
193 int Grow_addbitmap(char *devname
, int fd
, char *file
, int chunk
, int delay
, int write_behind
, int force
)
196 * First check that array doesn't have a bitmap
197 * Then create the bitmap
200 * For internal bitmaps, we need to check the version,
201 * find all the active devices, and write the bitmap block
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
;
212 major
= BITMAP_MAJOR_HOSTENDIAN
;
214 fprintf(stderr
, Name
": Warning - bitmaps created on this kernel are not portable\n"
215 " between different architectured. Consider upgrading the Linux kernel.\n");
219 if (ioctl(fd
, GET_BITMAP_FILE
, &bmf
) != 0) {
221 fprintf(stderr
, Name
": Memory allocation failure.\n");
223 fprintf(stderr
, Name
": bitmaps not supported by this kernel.\n");
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",
235 fprintf(stderr
, Name
": %s already has a bitmap (%s)\n",
236 devname
, bmf
.pathname
);
239 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
240 fprintf(stderr
, Name
": cannot get array status for %s\n", devname
);
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");
252 fprintf(stderr
, Name
": Internal bitmap already present on %s\n",
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");
261 bitmapsize
= array
.size
;
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
268 bitmapsize
= get_component_size(fd
);
270 if (bitmapsize
== 0) {
271 fprintf(stderr
, Name
": Cannot reliably determine size of array to create bitmap - sorry.\n");
275 if (array
.level
== 10) {
276 int ncopies
= (array
.layout
&255)*((array
.layout
>>8)&255);
277 bitmapsize
= bitmapsize
* array
.raid_disks
/ ncopies
;
280 st
= super_by_fd(fd
);
282 fprintf(stderr
, Name
": Cannot understand version %d.%d\n",
283 array
.major_version
, array
.minor_version
);
286 if (strcmp(file
, "none") == 0) {
287 fprintf(stderr
, Name
": no bitmap found on %s\n", devname
);
289 } else if (strcmp(file
, "internal") == 0) {
291 for (d
=0; d
< st
->max_devs
; d
++) {
292 mdu_disk_info_t disk
;
295 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
297 if (disk
.major
== 0 &&
300 if ((disk
.state
& (1<<MD_DISK_SYNC
))==0)
302 dv
= map_dev(disk
.major
, disk
.minor
, 1);
304 int fd2
= dev_open(dv
, O_RDWR
);
307 if (st
->ss
->load_super(st
, fd2
, NULL
)==0) {
308 if (st
->ss
->add_internal_bitmap(
310 &chunk
, delay
, write_behind
,
311 bitmapsize
, 0, major
)
313 st
->ss
->write_bitmap(st
, fd2
);
315 fprintf(stderr
, Name
": failed to create internal bitmap - chunksize problem.\n");
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");
332 int max_devs
= st
->max_devs
;
334 /* try to load a superblock */
335 for (d
=0; d
<max_devs
; d
++) {
336 mdu_disk_info_t disk
;
340 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
342 if ((disk
.major
==0 && disk
.minor
==0) ||
343 (disk
.state
& (1<<MD_DISK_REMOVED
)))
345 dv
= map_dev(disk
.major
, disk
.minor
, 1);
347 fd2
= dev_open(dv
, O_RDONLY
);
349 st
->ss
->load_super(st
, fd2
, NULL
) == 0) {
351 st
->ss
->uuid_from_super(st
, uuid
);
357 fprintf(stderr
, Name
": cannot find UUID for array!\n");
360 if (CreateBitmap(file
, force
, (char*)uuid
, chunk
,
361 delay
, write_behind
, bitmapsize
, major
)) {
364 bitmap_fd
= open(file
, O_RDWR
);
366 fprintf(stderr
, Name
": weird: %s cannot be opened\n",
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
));
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 4K from the end of the used space on the
386 * It if written after the backup is complete.
387 * It has the following structure.
390 static struct mdp_backup_super
{
391 char magic
[16]; /* md_backup_data-1 or -2 */
394 /* start/sizes in 512byte sectors */
395 __u64 devstart
; /* address on backup device/file of data */
398 __u32 sb_csum
; /* csum of preceeding bytes. */
400 __u64 devstart2
; /* offset in to data of second section */
403 __u32 sb_csum2
; /* csum of preceeding bytes. */
405 } __attribute__((aligned(512))) bsb
, bsb2
;
407 int bsb_csum(char *buf
, int len
)
411 for (i
=0; i
<len
; i
++)
412 csum
= (csum
<<3) + buf
[0];
413 return __cpu_to_le32(csum
);
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
);
430 int freeze_array(struct mdinfo
*sra
)
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.
438 if (sysfs_get_str(sra
, NULL
, "sync_action", buf
, 20) <= 0)
440 if (strcmp(buf
, "idle\n") != 0 &&
441 strcmp(buf
, "frozen\n") != 0)
443 if (sysfs_set_str(sra
, NULL
, "sync_action", "frozen") < 0)
448 void unfreeze_array(struct mdinfo
*sra
, int frozen
)
450 /* If 'frozen' is 1, unfreeze the array */
452 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
455 void wait_reshape(struct mdinfo
*sra
)
457 int fd
= sysfs_get_fd(sra
, NULL
, "sync_action");
464 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
466 if (sysfs_fd_get_str(fd
, action
, 20) < 0) {
470 } while (strncmp(action
, "reshape", 7) == 0);
474 int Grow_reshape(char *devname
, int fd
, int quiet
, char *backup_file
,
476 int level
, char *layout_str
, int chunksize
, int raid_disks
)
478 /* Make some changes in the shape of an array.
479 * The kernel must support the change.
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).
485 * - change size (i.e. component_size)
487 * - change layout/chunksize/ndisks
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)
495 struct mdu_array_info_s array
, orig
;
498 struct supertype
*st
;
501 int nlayout
, olayout
;
504 int orig_level
= UnSet
;
507 unsigned long long *offsets
;
512 unsigned long a
,b
, blocks
, stripes
;
514 unsigned long long array_size
;
521 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) < 0) {
522 fprintf(stderr
, Name
": %s is not an active md array - aborting\n",
528 (chunksize
|| level
!= UnSet
|| layout_str
|| raid_disks
)) {
529 fprintf(stderr
, Name
": cannot change component size at the same time "
530 "as other changes.\n"
531 " Change size first, then check data is intact before "
532 "making other changes.\n");
536 if (raid_disks
&& raid_disks
< array
.raid_disks
&& array
.level
> 1 &&
537 get_linux_version() < 2006032 &&
538 !check_env("MDADM_FORCE_FEWER")) {
539 fprintf(stderr
, Name
": reducing the number of devices is not safe before Linux 2.6.32\n"
540 " Please use a newer kernel\n");
543 sra
= sysfs_read(fd
, 0, GET_LEVEL
);
544 frozen
= freeze_array(sra
);
546 fprintf(stderr
, Name
": %s is performing resync/recovery and cannot"
547 " be reshaped\n", devname
);
551 /* ========= set size =============== */
552 if (size
>= 0 && (size
== 0 || size
!= array
.size
)) {
554 if (array
.size
!= size
) {
555 /* got truncated to 32bit, write to
556 * component_size instead
559 rv
= sysfs_set_num(sra
, NULL
,
560 "component_size", size
);
564 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
567 fprintf(stderr
, Name
": Cannot set device size for %s: %s\n",
568 devname
, strerror(err
));
570 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
571 fprintf(stderr
, " Bitmap must be removed before size can be changed\n");
575 ioctl(fd
, GET_ARRAY_INFO
, &array
);
576 size
= get_component_size(fd
)/2;
580 fprintf(stderr
, Name
": component size of %s has been set to %lluK\n",
584 size
= get_component_size(fd
)/2;
589 /* ======= set level =========== */
590 if (level
!= UnSet
&& level
!= array
.level
) {
591 /* Trying to change the level.
592 * We might need to change layout first and schedule a
593 * level change for later.
594 * Level changes that can happen immediately are:
595 * 0->4,5,6 1->5 4->5,6 5->1,6
596 * Level changes that need a layout change first are:
597 * 6->5,4,0 : need a -6 layout, or parity-last
598 * 5->4,0 : need parity-last
600 if ((array
.level
== 6 || array
.level
== 5) &&
601 (level
== 5 || level
== 4 || level
== 0)) {
602 /* Don't change level yet, but choose intermediate
606 if (layout_str
== NULL
)
607 switch (array
.layout
) {
608 case ALGORITHM_LEFT_ASYMMETRIC
:
609 case ALGORITHM_LEFT_ASYMMETRIC_6
:
610 case ALGORITHM_ROTATING_N_RESTART
:
611 layout_str
= "left-asymmetric-6";
613 case ALGORITHM_LEFT_SYMMETRIC
:
614 case ALGORITHM_LEFT_SYMMETRIC_6
:
615 case ALGORITHM_ROTATING_N_CONTINUE
:
616 layout_str
= "left-symmetric-6";
618 case ALGORITHM_RIGHT_ASYMMETRIC
:
619 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
620 case ALGORITHM_ROTATING_ZERO_RESTART
:
621 layout_str
= "right-asymmetric-6";
623 case ALGORITHM_RIGHT_SYMMETRIC
:
624 case ALGORITHM_RIGHT_SYMMETRIC_6
:
625 layout_str
= "right-symmetric-6";
627 case ALGORITHM_PARITY_0
:
628 case ALGORITHM_PARITY_0_6
:
629 layout_str
= "parity-first-6";
631 case ALGORITHM_PARITY_N
:
632 layout_str
= "parity-last";
635 fprintf(stderr
, Name
": %s: cannot"
636 "convert layout to RAID5 equivalent\n",
642 int l
= map_name(r5layout
, layout_str
);
644 fprintf(stderr
, Name
": %s: layout '%s' not recognised\n",
645 devname
, layout_str
);
649 if (l
!= ALGORITHM_PARITY_N
) {
650 /* need the -6 version */
651 char *ls
= map_num(r5layout
, l
);
652 strcat(strcpy(alt_layout
, ls
),
654 layout_str
= alt_layout
;
658 /* The final raid6->raid5 conversion
659 * will reduce the number of disks,
660 * so now we need to aim higher
664 layout_str
= "parity-last";
666 c
= map_num(pers
, level
);
668 rv
= 1;/* not possible */
671 err
= sysfs_set_str(sra
, NULL
, "level", c
);
674 fprintf(stderr
, Name
": %s: could not set level to %s\n",
677 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
678 fprintf(stderr
, " Bitmap must be removed before level can be changed\n");
683 orig_level
= orig
.level
;
684 ioctl(fd
, GET_ARRAY_INFO
, &array
);
685 if (layout_str
== NULL
&&
686 orig
.level
== 5 && level
== 6 &&
687 array
.layout
!= orig
.layout
)
688 layout_str
= map_num(r5layout
, orig
.layout
);
690 fprintf(stderr
, Name
" level of %s changed to %s\n",
696 /* ========= set shape (chunk_size / layout / ndisks) ============== */
697 /* Check if layout change is a no-op */
698 if (layout_str
) switch(array
.level
) {
700 if (array
.layout
== map_name(r5layout
, layout_str
))
704 if (layout_str
== NULL
&&
705 ((chunksize
&& chunksize
* 1024 != array
.chunk_size
) ||
706 (raid_disks
&& raid_disks
!= array
.raid_disks
)) &&
707 array
.layout
>= 16) {
709 ": %s has a non-standard layout. If you wish to preserve this\n"
710 " during the reshape, please specify --layout=preserve\n"
711 " If you want to change it, specify a layout or use --layout=normalise\n",
716 if (strcmp(layout_str
, "normalise") == 0 ||
717 strcmp(layout_str
, "normalize") == 0) {
719 strcpy(alt_layout
, map_num(r6layout
, array
.layout
));
720 hyphen
= strrchr(alt_layout
, '-');
721 if (hyphen
&& strcmp(hyphen
, "-6") == 0) {
723 layout_str
= alt_layout
;
727 if (array
.layout
== map_name(r6layout
, layout_str
))
729 if (layout_str
&& strcmp(layout_str
, "preserve") == 0)
733 if (layout_str
== NULL
734 && (chunksize
== 0 || chunksize
*1024 == array
.chunk_size
)
735 && (raid_disks
== 0 || raid_disks
== array
.raid_disks
)) {
737 if (level
!= UnSet
&& level
!= array
.level
) {
738 /* Looks like this level change doesn't need
739 * a reshape after all.
741 c
= map_num(pers
, level
);
743 rv
= sysfs_set_str(sra
, NULL
, "level", c
);
746 fprintf(stderr
, Name
": %s: could not set level to %s\n",
749 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
750 fprintf(stderr
, " Bitmap must be removed before level can be changed\n");
753 } else if (!changed
&& !quiet
)
754 fprintf(stderr
, Name
": %s: no change requested\n",
759 c
= map_num(pers
, array
.level
);
760 if (c
== NULL
) c
= "-unknown-";
761 switch(array
.level
) {
762 default: /* raid0, linear, multipath cannot be reconfigured */
763 fprintf(stderr
, Name
": %s array %s cannot be reshaped.\n",
768 case LEVEL_FAULTY
: /* only 'layout' change is permitted */
770 if (chunksize
|| raid_disks
) {
771 fprintf(stderr
, Name
": %s: Cannot change chunksize or disks of a 'faulty' array\n",
776 if (layout_str
== NULL
)
777 break; /* nothing to do.... */
779 array
.layout
= parse_layout_faulty(layout_str
);
780 if (array
.layout
< 0) {
782 fprintf(stderr
, Name
": %s: layout %s not understood for 'faulty' array\n",
783 devname
, layout_str
);
787 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
788 fprintf(stderr
, Name
": Cannot set layout for %s: %s\n",
789 devname
, strerror(errno
));
792 printf("layout for %s set to %d\n", devname
, array
.layout
);
795 case 1: /* only raid_disks can each be changed. */
797 if (chunksize
|| layout_str
!= NULL
) {
798 fprintf(stderr
, Name
": %s: Cannot change chunk size or layout for a RAID1 array.\n",
803 if (raid_disks
> 0) {
804 array
.raid_disks
= raid_disks
;
805 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
806 fprintf(stderr
, Name
": Cannot set raid-devices for %s: %s\n",
807 devname
, strerror(errno
));
818 * layout/chunksize/raid_disks can be changed
819 * though the kernel may not support it all.
821 st
= super_by_fd(fd
);
824 * There are three possibilities.
825 * 1/ The array will shrink.
826 * We need to ensure the reshape will pause before reaching
827 * the 'critical section'. We also need to fork and wait for
828 * that to happen. When it does we
829 * suspend/backup/complete/unfreeze
831 * 2/ The array will not change size.
832 * This requires that we keep a backup of a sliding window
833 * so that we can restore data after a crash. So we need
834 * to fork and monitor progress.
836 * 3/ The array will grow. This is relatively easy.
837 * However the kernel's restripe routines will cheerfully
838 * overwrite some early data before it is safe. So we
839 * need to make a backup of the early parts of the array
840 * and be ready to restore it if rebuild aborts very early.
842 * We backup data by writing it to one spare, or to a
843 * file which was given on command line.
845 * [FOLLOWING IS OLD AND PARTLY WRONG]
846 * So: we enumerate the devices in the array and
847 * make sure we can open all of them.
848 * Then we freeze the early part of the array and
849 * backup to the various spares.
850 * Then we request changes and start the reshape.
851 * Monitor progress until it has passed the danger zone.
852 * and finally invalidate the copied data and unfreeze the
853 * start of the array.
855 * In each case, we first make sure that storage is available
856 * for the required backup.
858 * - request the shape change.
859 * - for to handle backup etc.
861 nchunk
= ochunk
= array
.chunk_size
;
862 nlayout
= olayout
= array
.layout
;
863 ndisks
= odisks
= array
.raid_disks
;
866 nchunk
= chunksize
* 1024;
867 if (size
% chunksize
) {
868 fprintf(stderr
, Name
": component size %lluK is not"
869 " a multiple of chunksize %dK\n",
874 if (layout_str
!= NULL
)
875 switch(array
.level
) {
876 case 4: /* ignore layout */
879 nlayout
= map_name(r5layout
, layout_str
);
880 if (nlayout
== UnSet
) {
881 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
889 nlayout
= map_name(r6layout
, layout_str
);
890 if (nlayout
== UnSet
) {
891 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
898 if (raid_disks
) ndisks
= raid_disks
;
902 if (array
.level
== 6) {
903 odata
--; /* number of data disks */
907 if (odata
== ndata
&&
908 get_linux_version() < 2006032) {
909 fprintf(stderr
, Name
": in-place reshape is not safe before 2.6.32, sorry.\n");
913 /* Check that we can hold all the data */
914 get_dev_size(fd
, NULL
, &array_size
);
915 if (ndata
* size
< (array_size
/1024)) {
916 fprintf(stderr
, Name
": this change will reduce the size of the array.\n"
917 " use --grow --array-size first to truncate array.\n"
918 " e.g. mdadm --grow %s --array-size %llu\n",
919 devname
, ndata
* size
);
924 /* So how much do we need to backup.
925 * We need an amount of data which is both a whole number of
926 * old stripes and a whole number of new stripes.
927 * So LCM for (chunksize*datadisks).
929 a
= ochunk
/512 * odata
;
930 b
= nchunk
/512 * ndata
;
938 /* LCM == product / GCD */
939 blocks
= ochunk
/512 * nchunk
/512 * odata
* ndata
/ a
;
942 sra
= sysfs_read(fd
, 0,
943 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
946 if (ndata
== odata
) {
947 /* Make 'blocks' bigger for better throughput, but
948 * not so big that we reject it below.
949 * Try for 16 megabytes
951 while (blocks
* 32 < sra
->component_size
&&
955 fprintf(stderr
, Name
": Need to backup %luK of critical "
956 "section..\n", blocks
/2);
959 fprintf(stderr
, Name
": %s: Cannot get array details from sysfs\n",
965 if (blocks
>= sra
->component_size
/2) {
966 fprintf(stderr
, Name
": %s: Something wrong - reshape aborted\n",
971 nrdisks
= array
.nr_disks
+ sra
->array
.spare_disks
;
972 /* Now we need to open all these devices so we can read/write.
974 fdlist
= malloc((1+nrdisks
) * sizeof(int));
975 offsets
= malloc((1+nrdisks
) * sizeof(offsets
[0]));
976 if (!fdlist
|| !offsets
) {
977 fprintf(stderr
, Name
": malloc failed: grow aborted\n");
981 for (d
=0; d
<= nrdisks
; d
++)
983 d
= array
.raid_disks
;
984 for (sd
= sra
->devs
; sd
; sd
=sd
->next
) {
985 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
987 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
988 char *dn
= map_dev(sd
->disk
.major
,
990 fdlist
[sd
->disk
.raid_disk
]
991 = dev_open(dn
, O_RDONLY
);
992 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
993 if (fdlist
[sd
->disk
.raid_disk
] < 0) {
994 fprintf(stderr
, Name
": %s: cannot open component %s\n",
995 devname
, dn
?dn
:"-unknown-");
999 } else if (backup_file
== NULL
) {
1001 char *dn
= map_dev(sd
->disk
.major
,
1003 fdlist
[d
] = dev_open(dn
, O_RDWR
);
1004 offsets
[d
] = (sd
->data_offset
+ sra
->component_size
- blocks
- 8)*512;
1006 fprintf(stderr
, Name
": %s: cannot open component %s\n",
1007 devname
, dn
?dn
:"-unknown");
1014 if (backup_file
== NULL
) {
1015 if (ndata
<= odata
) {
1016 fprintf(stderr
, Name
": %s: Cannot grow - need backup-file\n",
1020 } else if (sra
->array
.spare_disks
== 0) {
1021 fprintf(stderr
, Name
": %s: Cannot grow - need a spare or "
1022 "backup-file to backup critical section\n",
1027 if (d
== array
.raid_disks
) {
1028 fprintf(stderr
, Name
": %s: No spare device for backup\n",
1034 /* need to check backup file is large enough */
1036 fdlist
[d
] = open(backup_file
, O_RDWR
|O_CREAT
|O_EXCL
,
1038 offsets
[d
] = 8 * 512;
1039 if (fdlist
[d
] < 0) {
1040 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1041 devname
, backup_file
, strerror(errno
));
1045 memset(buf
, 0, 512);
1046 for (i
=0; i
< blocks
+ 1 ; i
++) {
1047 if (write(fdlist
[d
], buf
, 512) != 512) {
1048 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1049 devname
, backup_file
, strerror(errno
));
1054 if (fsync(fdlist
[d
]) != 0) {
1055 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1056 devname
, backup_file
, strerror(errno
));
1063 /* lastly, check that the internal stripe cache is
1064 * large enough, or it won't work.
1067 cache
= (nchunk
< ochunk
) ? ochunk
: nchunk
;
1068 cache
= cache
* 4 / 4096;
1069 if (cache
< blocks
/ 8 / odisks
+ 16)
1070 /* Make it big enough to hold 'blocks' */
1071 cache
= blocks
/ 8 / odisks
+ 16;
1072 if (sra
->cache_size
< cache
)
1073 sysfs_set_num(sra
, NULL
, "stripe_cache_size",
1075 /* Right, everything seems fine. Let's kick things off.
1076 * If only changing raid_disks, use ioctl, else use
1079 if (ochunk
== nchunk
&& olayout
== nlayout
) {
1080 array
.raid_disks
= ndisks
;
1081 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
1084 fprintf(stderr
, Name
": Cannot set device shape for %s: %s\n",
1085 devname
, strerror(errno
));
1086 if (ndisks
< odisks
&&
1087 get_linux_version() < 2006030)
1088 fprintf(stderr
, Name
": linux 2.6.30 or later required\n");
1090 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1091 fprintf(stderr
, " Bitmap must be removed before shape can be changed\n");
1096 /* set them all just in case some old 'new_*' value
1097 * persists from some earlier problem
1099 int err
= err
; /* only used if rv==1, and always set if
1100 * rv==1, so initialisation not needed,
1101 * despite gcc warning
1103 if (sysfs_set_num(sra
, NULL
, "chunk_size", nchunk
) < 0)
1104 rv
= 1, err
= errno
;
1105 if (!rv
&& sysfs_set_num(sra
, NULL
, "layout", nlayout
) < 0)
1106 rv
= 1, err
= errno
;
1107 if (!rv
&& sysfs_set_num(sra
, NULL
, "raid_disks", ndisks
) < 0)
1108 rv
= 1, err
= errno
;
1110 fprintf(stderr
, Name
": Cannot set device shape for %s\n",
1112 if (get_linux_version() < 2006030)
1113 fprintf(stderr
, Name
": linux 2.6.30 or later required\n");
1115 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1116 fprintf(stderr
, " Bitmap must be removed before shape can be changed\n");
1121 if (ndisks
== 2 && odisks
== 2) {
1122 /* No reshape is needed in this trivial case */
1127 /* set up the backup-super-block. This requires the
1128 * uuid from the array.
1130 /* Find a superblock */
1131 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1135 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1137 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
1138 devfd
= dev_open(dn
, O_RDONLY
);
1141 ok
= st
->ss
->load_super(st
, devfd
, NULL
);
1147 fprintf(stderr
, Name
": %s: Cannot find a superblock\n",
1153 memset(&bsb
, 0, 512);
1154 memcpy(bsb
.magic
, "md_backup_data-1", 16);
1155 st
->ss
->uuid_from_super(st
, (int*)&bsb
.set_uuid
);
1156 bsb
.mtime
= __cpu_to_le64(time(0));
1157 bsb
.devstart2
= blocks
;
1158 stripes
= blocks
/ (ochunk
/512) / odata
;
1159 /* Now we just need to kick off the reshape and watch, while
1160 * handling backups of the data...
1161 * This is all done by a forked background process.
1166 if (check_env("MDADM_GROW_VERIFY"))
1167 fd
= open(devname
, O_RDONLY
| O_DIRECT
);
1170 mlockall(MCL_FUTURE
);
1173 done
= child_grow(fd
, sra
, stripes
,
1175 odisks
, ochunk
, array
.level
, olayout
, odata
,
1176 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1177 else if (odata
> ndata
)
1178 done
= child_shrink(fd
, sra
, stripes
,
1180 odisks
, ochunk
, array
.level
, olayout
, odata
,
1181 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1183 done
= child_same_size(fd
, sra
, stripes
,
1186 odisks
, ochunk
, array
.level
, olayout
, odata
,
1187 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1188 if (backup_file
&& done
)
1189 unlink(backup_file
);
1190 if (level
!= UnSet
&& level
!= array
.level
) {
1191 /* We need to wait for the reshape to finish
1192 * (which will have happened unless odata < ndata)
1193 * and then set the level
1196 c
= map_num(pers
, level
);
1198 exit(0);/* not possible */
1202 err
= sysfs_set_str(sra
, NULL
, "level", c
);
1204 fprintf(stderr
, Name
": %s: could not set level to %s\n",
1209 fprintf(stderr
, Name
": Cannot run child to monitor reshape: %s\n",
1214 /* The child will take care of unfreezing the array */
1223 if (rv
&& orig_level
!= UnSet
&& sra
) {
1224 c
= map_num(pers
, orig_level
);
1225 if (c
&& sysfs_set_str(sra
, NULL
, "level", c
) == 0)
1226 fprintf(stderr
, Name
": aborting level change\n");
1229 unfreeze_array(sra
, frozen
);
1234 * We run a child process in the background which performs the following
1236 * - wait for resync to reach a certain point
1237 * - suspend io to the following section
1238 * - backup that section
1239 * - allow resync to proceed further
1241 * - discard the backup.
1243 * When are combined in slightly different ways in the three cases.
1245 * - suspend/backup/allow/wait/resume/discard
1247 * - allow/wait/suspend/backup/allow/wait/resume/discard
1249 * - wait/resume/discard/suspend/backup/allow
1251 * suspend/backup/allow always come together
1252 * wait/resume/discard do too.
1253 * For the same-size case we have two backups to improve flow.
1257 /* FIXME return status is never checked */
1258 int grow_backup(struct mdinfo
*sra
,
1259 unsigned long long offset
, /* per device */
1260 unsigned long stripes
, /* per device */
1261 int *sources
, unsigned long long *offsets
,
1262 int disks
, int chunk
, int level
, int layout
,
1263 int dests
, int *destfd
, unsigned long long *destoffsets
,
1264 int part
, int *degraded
,
1267 /* Backup 'blocks' sectors at 'offset' on each device of the array,
1268 * to storage 'destfd' (offset 'destoffsets'), after first
1269 * suspending IO. Then allow resync to continue
1270 * over the suspended section.
1271 * Use part 'part' of the backup-super-block.
1276 unsigned long long new_degraded
;
1277 //printf("offset %llu\n", offset);
1282 sysfs_set_num(sra
, NULL
, "suspend_hi", (offset
+ stripes
* chunk
/512) * odata
);
1283 /* Check that array hasn't become degraded, else we might backup the wrong data */
1284 sysfs_get_ll(sra
, NULL
, "degraded", &new_degraded
);
1285 if (new_degraded
!= *degraded
) {
1286 /* check each device to ensure it is still working */
1288 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1289 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1291 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
1293 if (sysfs_get_str(sra
, sd
, "state", sbuf
, 20) < 0 ||
1294 strstr(sbuf
, "faulty") ||
1295 strstr(sbuf
, "in_sync") == NULL
) {
1296 /* this device is dead */
1297 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
1298 if (sd
->disk
.raid_disk
>= 0 &&
1299 sources
[sd
->disk
.raid_disk
] >= 0) {
1300 close(sources
[sd
->disk
.raid_disk
]);
1301 sources
[sd
->disk
.raid_disk
] = -1;
1306 *degraded
= new_degraded
;
1309 bsb
.arraystart2
= __cpu_to_le64(offset
* odata
);
1310 bsb
.length2
= __cpu_to_le64(stripes
* chunk
/512 * odata
);
1312 bsb
.arraystart
= __cpu_to_le64(offset
* odata
);
1313 bsb
.length
= __cpu_to_le64(stripes
* chunk
/512 * odata
);
1316 bsb
.magic
[15] = '2';
1317 for (i
= 0; i
< dests
; i
++)
1319 lseek64(destfd
[i
], destoffsets
[i
] + __le64_to_cpu(bsb
.devstart2
)*512, 0);
1321 lseek64(destfd
[i
], destoffsets
[i
], 0);
1323 rv
= save_stripes(sources
, offsets
,
1324 disks
, chunk
, level
, layout
,
1326 offset
*512*odata
, stripes
* chunk
* odata
,
1331 bsb
.mtime
= __cpu_to_le64(time(0));
1332 for (i
= 0; i
< dests
; i
++) {
1333 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
1335 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
1336 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
1337 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
1338 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
1340 if (lseek64(destfd
[i
], destoffsets
[i
] - 4096, 0) != destoffsets
[i
] - 4096)
1342 rv
= rv
?: write(destfd
[i
], &bsb
, 512);
1343 if (destoffsets
[i
] > 4096) {
1344 if (lseek64(destfd
[i
], destoffsets
[i
]+stripes
*chunk
*odata
, 0) !=
1345 destoffsets
[i
]+stripes
*chunk
*odata
)
1347 rv
= rv
?: write(destfd
[i
], &bsb
, 512);
1355 /* in 2.6.30, the value reported by sync_completed can be
1356 * less that it should be by one stripe.
1357 * This only happens when reshape hits sync_max and pauses.
1358 * So allow wait_backup to either extent sync_max further
1359 * than strictly necessary, or return before the
1360 * sync has got quite as far as we would really like.
1361 * This is what 'blocks2' is for.
1362 * The various caller give appropriate values so that
1365 /* FIXME return value is often ignored */
1366 int wait_backup(struct mdinfo
*sra
,
1367 unsigned long long offset
, /* per device */
1368 unsigned long long blocks
, /* per device */
1369 unsigned long long blocks2
, /* per device - hack */
1370 int dests
, int *destfd
, unsigned long long *destoffsets
,
1373 /* Wait for resync to pass the section that was backed up
1374 * then erase the backup and allow IO
1376 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
1377 unsigned long long completed
;
1383 sysfs_set_num(sra
, NULL
, "sync_max", offset
+ blocks
+ blocks2
);
1385 sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
1391 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
1392 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
1396 if (sysfs_get_str(sra
, NULL
, "sync_action",
1398 strncmp(action
, "reshape", 7) != 0)
1400 } while (completed
< offset
+ blocks
);
1404 bsb
.arraystart2
= __cpu_to_le64(0);
1405 bsb
.length2
= __cpu_to_le64(0);
1407 bsb
.arraystart
= __cpu_to_le64(0);
1408 bsb
.length
= __cpu_to_le64(0);
1410 bsb
.mtime
= __cpu_to_le64(time(0));
1412 for (i
= 0; i
< dests
; i
++) {
1413 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
1414 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
1415 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
1416 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
1417 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
1418 if (lseek64(destfd
[i
], destoffsets
[i
]-4096, 0) !=
1419 destoffsets
[i
]-4096)
1421 rv
= rv
?: write(destfd
[i
], &bsb
, 512);
1427 static void fail(char *msg
)
1430 rv
= write(2, msg
, strlen(msg
));
1431 rv
|= write(2, "\n", 1);
1435 static char *abuf
, *bbuf
;
1437 static void validate(int afd
, int bfd
, unsigned long long offset
)
1439 /* check that the data in the backup against the array.
1440 * This is only used for regression testing and should not
1441 * be used while the array is active
1445 lseek64(bfd
, offset
- 4096, 0);
1446 if (read(bfd
, &bsb2
, 512) != 512)
1447 fail("cannot read bsb");
1448 if (bsb2
.sb_csum
!= bsb_csum((char*)&bsb2
,
1449 ((char*)&bsb2
.sb_csum
)-((char*)&bsb2
)))
1450 fail("first csum bad");
1451 if (memcmp(bsb2
.magic
, "md_backup_data", 14) != 0)
1452 fail("magic is bad");
1453 if (memcmp(bsb2
.magic
, "md_backup_data-2", 16) == 0 &&
1454 bsb2
.sb_csum2
!= bsb_csum((char*)&bsb2
,
1455 ((char*)&bsb2
.sb_csum2
)-((char*)&bsb2
)))
1456 fail("second csum bad");
1458 if (__le64_to_cpu(bsb2
.devstart
)*512 != offset
)
1459 fail("devstart is wrong");
1462 unsigned long long len
= __le64_to_cpu(bsb2
.length
)*512;
1464 if (abuflen
< len
) {
1468 if (posix_memalign((void**)&abuf
, 4096, abuflen
) ||
1469 posix_memalign((void**)&bbuf
, 4096, abuflen
)) {
1471 /* just stop validating on mem-alloc failure */
1476 lseek64(bfd
, offset
, 0);
1477 if (read(bfd
, bbuf
, len
) != len
) {
1478 //printf("len %llu\n", len);
1479 fail("read first backup failed");
1481 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart
)*512, 0);
1482 if (read(afd
, abuf
, len
) != len
)
1483 fail("read first from array failed");
1484 if (memcmp(bbuf
, abuf
, len
) != 0) {
1487 printf("offset=%llu len=%llu\n",
1488 (unsigned long long)__le64_to_cpu(bsb2
.arraystart
)*512, len
);
1489 for (i
=0; i
<len
; i
++)
1490 if (bbuf
[i
] != abuf
[i
]) {
1491 printf("first diff byte %d\n", i
);
1495 fail("data1 compare failed");
1499 unsigned long long len
= __le64_to_cpu(bsb2
.length2
)*512;
1501 if (abuflen
< len
) {
1505 abuf
= malloc(abuflen
);
1506 bbuf
= malloc(abuflen
);
1509 lseek64(bfd
, offset
+__le64_to_cpu(bsb2
.devstart2
)*512, 0);
1510 if (read(bfd
, bbuf
, len
) != len
)
1511 fail("read second backup failed");
1512 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart2
)*512, 0);
1513 if (read(afd
, abuf
, len
) != len
)
1514 fail("read second from array failed");
1515 if (memcmp(bbuf
, abuf
, len
) != 0)
1516 fail("data2 compare failed");
1520 static int child_grow(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1521 int *fds
, unsigned long long *offsets
,
1522 int disks
, int chunk
, int level
, int layout
, int data
,
1523 int dests
, int *destfd
, unsigned long long *destoffsets
)
1528 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
1529 /* Don't start the 'reshape' */
1531 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1532 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1533 grow_backup(sra
, 0, stripes
,
1534 fds
, offsets
, disks
, chunk
, level
, layout
,
1535 dests
, destfd
, destoffsets
,
1537 validate(afd
, destfd
[0], destoffsets
[0]);
1538 wait_backup(sra
, 0, stripes
* chunk
/ 512, stripes
* chunk
/ 512,
1539 dests
, destfd
, destoffsets
,
1541 sysfs_set_num(sra
, NULL
, "suspend_lo", (stripes
* chunk
/512) * data
);
1543 /* FIXME this should probably be numeric */
1544 sysfs_set_str(sra
, NULL
, "sync_max", "max");
1548 static int child_shrink(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1549 int *fds
, unsigned long long *offsets
,
1550 int disks
, int chunk
, int level
, int layout
, int data
,
1551 int dests
, int *destfd
, unsigned long long *destoffsets
)
1554 unsigned long long start
;
1558 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
1560 start
= sra
->component_size
- stripes
* chunk
/512;
1561 sysfs_set_num(sra
, NULL
, "sync_max", start
);
1562 sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
1563 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1564 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1565 rv
= wait_backup(sra
, 0, start
- stripes
* chunk
/512, stripes
* chunk
/512,
1566 dests
, destfd
, destoffsets
, 0);
1569 grow_backup(sra
, 0, stripes
,
1571 disks
, chunk
, level
, layout
,
1572 dests
, destfd
, destoffsets
,
1574 validate(afd
, destfd
[0], destoffsets
[0]);
1575 wait_backup(sra
, start
, stripes
*chunk
/512, 0,
1576 dests
, destfd
, destoffsets
, 0);
1577 sysfs_set_num(sra
, NULL
, "suspend_lo", (stripes
* chunk
/512) * data
);
1579 /* FIXME this should probably be numeric */
1580 sysfs_set_str(sra
, NULL
, "sync_max", "max");
1584 static int child_same_size(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1585 int *fds
, unsigned long long *offsets
,
1586 unsigned long long start
,
1587 int disks
, int chunk
, int level
, int layout
, int data
,
1588 int dests
, int *destfd
, unsigned long long *destoffsets
)
1590 unsigned long long size
;
1591 unsigned long tailstripes
= stripes
;
1594 unsigned long long speed
;
1598 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
1601 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1602 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1604 sysfs_get_ll(sra
, NULL
, "sync_speed_min", &speed
);
1605 sysfs_set_num(sra
, NULL
, "sync_speed_min", 200000);
1607 grow_backup(sra
, start
, stripes
,
1609 disks
, chunk
, level
, layout
,
1610 dests
, destfd
, destoffsets
,
1612 grow_backup(sra
, (start
+ stripes
) * chunk
/512, stripes
,
1614 disks
, chunk
, level
, layout
,
1615 dests
, destfd
, destoffsets
,
1617 validate(afd
, destfd
[0], destoffsets
[0]);
1619 start
+= stripes
* 2; /* where to read next */
1620 size
= sra
->component_size
/ (chunk
/512);
1621 while (start
< size
) {
1622 if (wait_backup(sra
, (start
-stripes
*2)*chunk
/512,
1623 stripes
*chunk
/512, 0,
1624 dests
, destfd
, destoffsets
,
1627 sysfs_set_num(sra
, NULL
, "suspend_lo", start
*chunk
/512 * data
);
1628 if (start
+ stripes
> size
)
1629 tailstripes
= (size
- start
);
1631 grow_backup(sra
, start
*chunk
/512, tailstripes
,
1633 disks
, chunk
, level
, layout
,
1634 dests
, destfd
, destoffsets
,
1635 part
, °raded
, buf
);
1638 validate(afd
, destfd
[0], destoffsets
[0]);
1640 if (wait_backup(sra
, (start
-stripes
*2) * chunk
/512, stripes
* chunk
/512, 0,
1641 dests
, destfd
, destoffsets
,
1644 sysfs_set_num(sra
, NULL
, "suspend_lo", ((start
-stripes
)*chunk
/512) * data
);
1645 wait_backup(sra
, (start
-stripes
) * chunk
/512, tailstripes
* chunk
/512, 0,
1646 dests
, destfd
, destoffsets
,
1648 sysfs_set_num(sra
, NULL
, "suspend_lo", (size
*chunk
/512) * data
);
1649 sysfs_set_num(sra
, NULL
, "sync_speed_min", speed
);
1655 * If any spare contains md_back_data-1 which is recent wrt mtime,
1656 * write that data into the array and update the super blocks with
1657 * the new reshape_progress
1659 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
, int cnt
,
1660 char *backup_file
, int verbose
)
1664 unsigned long long *offsets
;
1665 unsigned long long nstripe
, ostripe
;
1668 if (info
->new_level
!= info
->array
.level
)
1669 return 1; /* Cannot handle level changes (they are instantaneous) */
1671 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
1672 if (info
->array
.level
== 6) odata
--; /* number of data disks */
1673 ndata
= info
->array
.raid_disks
- 1;
1674 if (info
->new_level
== 6) ndata
--;
1676 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
1678 if (info
->delta_disks
<= 0)
1679 /* Didn't grow, so the backup file must have
1683 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
1684 struct mdinfo dinfo
;
1687 char *devname
, namebuf
[20];
1689 /* This was a spare and may have some saved data on it.
1690 * Load the superblock, find and load the
1691 * backup_super_block.
1692 * If either fail, go on to next device.
1693 * If the backup contains no new info, just return
1694 * else restore data and update all superblocks
1696 if (i
== old_disks
-1) {
1697 fd
= open(backup_file
, O_RDONLY
);
1699 fprintf(stderr
, Name
": backup file %s inaccessible: %s\n",
1700 backup_file
, strerror(errno
));
1703 devname
= backup_file
;
1708 if (st
->ss
->load_super(st
, fd
, NULL
))
1711 st
->ss
->getinfo_super(st
, &dinfo
);
1712 st
->ss
->free_super(st
);
1715 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
1717 fprintf(stderr
, Name
": Cannot seek on device %d\n", i
);
1718 continue; /* Cannot seek */
1720 sprintf(namebuf
, "device-%d", i
);
1723 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
)) {
1725 fprintf(stderr
, Name
": Cannot read from %s\n", devname
);
1726 continue; /* Cannot read */
1728 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0 &&
1729 memcmp(bsb
.magic
, "md_backup_data-2", 16) != 0) {
1731 fprintf(stderr
, Name
": No backup metadata on %s\n", devname
);
1734 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
))) {
1736 fprintf(stderr
, Name
": Bad backup-metadata checksum on %s\n", devname
);
1737 continue; /* bad checksum */
1739 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0 &&
1740 bsb
.sb_csum2
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum2
)-((char*)&bsb
))) {
1742 fprintf(stderr
, Name
": Bad backup-metadata checksum2 on %s\n", devname
);
1743 continue; /* Bad second checksum */
1745 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0) {
1747 fprintf(stderr
, Name
": Wrong uuid on backup-metadata on %s\n", devname
);
1748 continue; /* Wrong uuid */
1751 /* array utime and backup-mtime should be updated at much the same time, but it seems that
1752 * sometimes they aren't... So allow considerable flexability in matching, and allow
1753 * this test to be overridden by an environment variable.
1755 if (info
->array
.utime
> __le64_to_cpu(bsb
.mtime
) + 2*60*60 ||
1756 info
->array
.utime
< __le64_to_cpu(bsb
.mtime
) - 10*60) {
1757 if (check_env("MDADM_GROW_ALLOW_OLD")) {
1758 fprintf(stderr
, Name
": accepting backup with timestamp %lu "
1759 "for array with timestamp %lu\n",
1760 (unsigned long)__le64_to_cpu(bsb
.mtime
),
1761 (unsigned long)info
->array
.utime
);
1764 fprintf(stderr
, Name
": too-old timestamp on "
1765 "backup-metadata on %s\n", devname
);
1766 continue; /* time stamp is too bad */
1770 if (bsb
.magic
[15] == '1') {
1771 if (info
->delta_disks
>= 0) {
1772 /* reshape_progress is increasing */
1773 if (__le64_to_cpu(bsb
.arraystart
) + __le64_to_cpu(bsb
.length
) <
1774 info
->reshape_progress
) {
1777 fprintf(stderr
, Name
": backup-metadata found on %s but is not needed\n", devname
);
1778 continue; /* No new data here */
1781 /* reshape_progress is decreasing */
1782 if (__le64_to_cpu(bsb
.arraystart
) >=
1783 info
->reshape_progress
)
1784 goto nonew
; /* No new data here */
1787 if (info
->delta_disks
>= 0) {
1788 /* reshape_progress is increasing */
1789 if (__le64_to_cpu(bsb
.arraystart
) + __le64_to_cpu(bsb
.length
) <
1790 info
->reshape_progress
&&
1791 __le64_to_cpu(bsb
.arraystart2
) + __le64_to_cpu(bsb
.length2
) <
1792 info
->reshape_progress
)
1793 goto nonew
; /* No new data here */
1795 /* reshape_progress is decreasing */
1796 if (__le64_to_cpu(bsb
.arraystart
) >=
1797 info
->reshape_progress
&&
1798 __le64_to_cpu(bsb
.arraystart2
) >=
1799 info
->reshape_progress
)
1800 goto nonew
; /* No new data here */
1803 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0) {
1806 fprintf(stderr
, Name
": Failed to verify secondary backup-metadata block on %s\n",
1808 continue; /* Cannot seek */
1810 /* There should be a duplicate backup superblock 4k before here */
1811 if (lseek64(fd
, -4096, 1) < 0 ||
1812 read(fd
, &bsb2
, 4096) != 4096)
1813 goto second_fail
; /* Cannot find leading superblock */
1814 if (bsb
.magic
[15] == '1')
1815 bsbsize
= offsetof(struct mdp_backup_super
, pad1
);
1817 bsbsize
= offsetof(struct mdp_backup_super
, pad
);
1818 if (memcmp(&bsb2
, &bsb
, bsbsize
) != 0)
1819 goto second_fail
; /* Cannot find leading superblock */
1821 /* Now need the data offsets for all devices. */
1822 offsets
= malloc(sizeof(*offsets
)*info
->array
.raid_disks
);
1823 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
1826 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
1827 /* FIXME should be this be an error */
1829 st
->ss
->getinfo_super(st
, &dinfo
);
1830 st
->ss
->free_super(st
);
1831 offsets
[j
] = dinfo
.data_offset
* 512;
1833 printf(Name
": restoring critical section\n");
1835 if (restore_stripes(fdlist
, offsets
,
1836 info
->array
.raid_disks
,
1840 fd
, __le64_to_cpu(bsb
.devstart
)*512,
1841 __le64_to_cpu(bsb
.arraystart
)*512,
1842 __le64_to_cpu(bsb
.length
)*512)) {
1843 /* didn't succeed, so giveup */
1845 fprintf(stderr
, Name
": Error restoring backup from %s\n",
1850 if (bsb
.magic
[15] == '2' &&
1851 restore_stripes(fdlist
, offsets
,
1852 info
->array
.raid_disks
,
1856 fd
, __le64_to_cpu(bsb
.devstart
)*512 +
1857 __le64_to_cpu(bsb
.devstart2
)*512,
1858 __le64_to_cpu(bsb
.arraystart2
)*512,
1859 __le64_to_cpu(bsb
.length2
)*512)) {
1860 /* didn't succeed, so giveup */
1862 fprintf(stderr
, Name
": Error restoring second backup from %s\n",
1868 /* Ok, so the data is restored. Let's update those superblocks. */
1870 if (info
->delta_disks
>= 0) {
1871 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
) +
1872 __le64_to_cpu(bsb
.length
);
1873 if (bsb
.magic
[15] == '2') {
1874 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
) +
1875 __le64_to_cpu(bsb
.length2
);
1876 if (p2
> info
->reshape_progress
)
1877 info
->reshape_progress
= p2
;
1880 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
);
1881 if (bsb
.magic
[15] == '2') {
1882 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
);
1883 if (p2
< info
->reshape_progress
)
1884 info
->reshape_progress
= p2
;
1887 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
1888 if (fdlist
[j
] < 0) continue;
1889 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
1891 st
->ss
->getinfo_super(st
, &dinfo
);
1892 dinfo
.reshape_progress
= info
->reshape_progress
;
1893 st
->ss
->update_super(st
, &dinfo
,
1894 "_reshape_progress",
1896 st
->ss
->store_super(st
, fdlist
[j
]);
1897 st
->ss
->free_super(st
);
1901 /* Didn't find any backup data, try to see if any
1904 if (info
->delta_disks
< 0) {
1905 /* When shrinking, the critical section is at the end.
1906 * So see if we are before the critical section.
1908 unsigned long long first_block
;
1909 nstripe
= ostripe
= 0;
1911 while (ostripe
>= nstripe
) {
1912 ostripe
+= info
->array
.chunk_size
/ 512;
1913 first_block
= ostripe
* odata
;
1914 nstripe
= first_block
/ ndata
/ (info
->new_chunk
/512) *
1915 (info
->new_chunk
/512);
1918 if (info
->reshape_progress
>= first_block
)
1921 if (info
->delta_disks
> 0) {
1922 /* See if we are beyond the critical section. */
1923 unsigned long long last_block
;
1924 nstripe
= ostripe
= 0;
1926 while (nstripe
>= ostripe
) {
1927 nstripe
+= info
->new_chunk
/ 512;
1928 last_block
= nstripe
* ndata
;
1929 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
1930 (info
->array
.chunk_size
/512);
1933 if (info
->reshape_progress
>= last_block
)
1936 /* needed to recover critical section! */
1938 fprintf(stderr
, Name
": Failed to find backup of critical section\n");
1942 int Grow_continue(int mdfd
, struct supertype
*st
, struct mdinfo
*info
,
1945 /* Array is assembled and ready to be started, but
1946 * monitoring is probably required.
1949 * - set upper bound for resync
1950 * - initialise the 'suspend' boundaries
1951 * - switch to read-write
1952 * - fork and continue monitoring
1956 unsigned long long backup_offsets
[1];
1957 int odisks
, ndisks
, ochunk
, nchunk
,odata
,ndata
;
1958 unsigned long a
,b
,blocks
,stripes
;
1961 unsigned long long *offsets
;
1963 struct mdinfo
*sra
, *sd
;
1968 err
= sysfs_set_str(info
, NULL
, "array_state", "readonly");
1972 /* make sure reshape doesn't progress until we are ready */
1973 sysfs_set_str(info
, NULL
, "sync_max", "0");
1974 sysfs_set_str(info
, NULL
, "array_state", "active"); /* FIXME or clean */
1976 /* ndisks is not growing, so raid_disks is old and +delta is new */
1977 odisks
= info
->array
.raid_disks
;
1978 ndisks
= odisks
+ info
->delta_disks
;
1981 if (info
->array
.level
== 6) {
1985 ochunk
= info
->array
.chunk_size
;
1986 nchunk
= info
->new_chunk
;
1989 a
= ochunk
/512 * odata
;
1990 b
= nchunk
/512 * ndata
;
1998 /* LCM == product / GCD */
1999 blocks
= ochunk
/512 * nchunk
/512 * odata
* ndata
/ a
;
2001 sra
= sysfs_read(-1, devname2devnum(info
->sys_name
),
2002 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
2007 while (blocks
* 32 < sra
->component_size
&&
2010 stripes
= blocks
/ (info
->array
.chunk_size
/512) / odata
;
2012 /* check that the internal stripe cache is
2013 * large enough, or it won't work.
2015 cache
= (nchunk
< ochunk
) ? ochunk
: nchunk
;
2016 cache
= cache
* 4 / 4096;
2017 if (cache
< blocks
/ 8 / odisks
+ 16)
2018 /* Make it big enough to hold 'blocks' */
2019 cache
= blocks
/ 8 / odisks
+ 16;
2020 if (sra
->cache_size
< cache
)
2021 sysfs_set_num(sra
, NULL
, "stripe_cache_size",
2024 memset(&bsb
, 0, 512);
2025 memcpy(bsb
.magic
, "md_backup_data-1", 16);
2026 memcpy(&bsb
.set_uuid
, info
->uuid
, 16);
2027 bsb
.mtime
= __cpu_to_le64(time(0));
2028 bsb
.devstart2
= blocks
;
2030 backup_fd
= open(backup_file
, O_RDWR
|O_CREAT
, S_IRUSR
| S_IWUSR
);
2031 backup_list
[0] = backup_fd
;
2032 backup_offsets
[0] = 8 * 512;
2033 fds
= malloc(odisks
* sizeof(fds
[0]));
2034 offsets
= malloc(odisks
* sizeof(offsets
[0]));
2035 for (d
=0; d
<odisks
; d
++)
2038 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2039 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2041 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
2042 char *dn
= map_dev(sd
->disk
.major
,
2044 fds
[sd
->disk
.raid_disk
]
2045 = dev_open(dn
, O_RDONLY
);
2046 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
2047 if (fds
[sd
->disk
.raid_disk
] < 0) {
2048 fprintf(stderr
, Name
": %s: cannot open component %s\n",
2049 info
->sys_name
, dn
?dn
:"-unknown-");
2060 mlockall(MCL_FUTURE
);
2061 if (info
->delta_disks
< 0)
2062 done
= child_shrink(-1, info
, stripes
,
2064 info
->array
.raid_disks
,
2065 info
->array
.chunk_size
,
2066 info
->array
.level
, info
->array
.layout
,
2068 1, backup_list
, backup_offsets
);
2069 else if (info
->delta_disks
== 0) {
2070 /* The 'start' is a per-device stripe number.
2071 * reshape_progress is a per-array sector number.
2072 * So divide by ndata * chunk_size
2074 unsigned long long start
= info
->reshape_progress
/ ndata
;
2075 start
/= (info
->array
.chunk_size
/512);
2076 done
= child_same_size(-1, info
, stripes
,
2079 info
->array
.raid_disks
,
2080 info
->array
.chunk_size
,
2081 info
->array
.level
, info
->array
.layout
,
2083 1, backup_list
, backup_offsets
);
2085 if (backup_file
&& done
)
2086 unlink(backup_file
);
2087 /* FIXME should I intuit a level change */
2090 fprintf(stderr
, Name
": Cannot run child to continue monitoring reshape: %s\n",