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
);
566 fprintf(stderr
, Name
": Cannot set device size for %s: %s\n",
567 devname
, strerror(errno
));
571 ioctl(fd
, GET_ARRAY_INFO
, &array
);
573 fprintf(stderr
, Name
": component size of %s has been set to %dK\n",
574 devname
, array
.size
);
578 /* ======= set level =========== */
579 if (level
!= UnSet
&& level
!= array
.level
) {
580 /* Trying to change the level.
581 * We might need to change layout first and schedule a
582 * level change for later.
583 * Level changes that can happen immediately are:
584 * 0->4,5,6 1->5 4->5,6 5->1,6
585 * Level changes that need a layout change first are:
586 * 6->5,4,0 : need a -6 layout, or parity-last
587 * 5->4,0 : need parity-last
589 if ((array
.level
== 6 || array
.level
== 5) &&
590 (level
== 5 || level
== 4 || level
== 0)) {
591 /* Don't change level yet, but choose intermediate
595 if (layout_str
== NULL
)
596 switch (array
.layout
) {
597 case ALGORITHM_LEFT_ASYMMETRIC
:
598 case ALGORITHM_LEFT_ASYMMETRIC_6
:
599 case ALGORITHM_ROTATING_N_RESTART
:
600 layout_str
= "left-asymmetric-6";
602 case ALGORITHM_LEFT_SYMMETRIC
:
603 case ALGORITHM_LEFT_SYMMETRIC_6
:
604 case ALGORITHM_ROTATING_N_CONTINUE
:
605 layout_str
= "left-symmetric-6";
607 case ALGORITHM_RIGHT_ASYMMETRIC
:
608 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
609 case ALGORITHM_ROTATING_ZERO_RESTART
:
610 layout_str
= "right-asymmetric-6";
612 case ALGORITHM_RIGHT_SYMMETRIC
:
613 case ALGORITHM_RIGHT_SYMMETRIC_6
:
614 layout_str
= "right-symmetric-6";
616 case ALGORITHM_PARITY_0
:
617 case ALGORITHM_PARITY_0_6
:
618 layout_str
= "parity-first-6";
620 case ALGORITHM_PARITY_N
:
621 layout_str
= "parity-last";
624 fprintf(stderr
, Name
": %s: cannot"
625 "convert layout to RAID5 equivalent\n",
631 int l
= map_name(r5layout
, layout_str
);
633 fprintf(stderr
, Name
": %s: layout '%s' not recognised\n",
634 devname
, layout_str
);
638 if (l
!= ALGORITHM_PARITY_N
) {
639 /* need the -6 version */
640 char *ls
= map_num(r5layout
, l
);
641 strcat(strcpy(alt_layout
, ls
),
643 layout_str
= alt_layout
;
647 /* The final raid6->raid5 conversion
648 * will reduce the number of disks,
649 * so now we need to aim higher
653 layout_str
= "parity-last";
655 c
= map_num(pers
, level
);
657 rv
= 1;/* not possible */
660 err
= sysfs_set_str(sra
, NULL
, "level", c
);
662 fprintf(stderr
, Name
": %s: could not set level to %s\n",
668 orig_level
= orig
.level
;
669 ioctl(fd
, GET_ARRAY_INFO
, &array
);
670 if (layout_str
== NULL
&&
671 orig
.level
== 5 && level
== 6 &&
672 array
.layout
!= orig
.layout
)
673 layout_str
= map_num(r5layout
, orig
.layout
);
675 fprintf(stderr
, Name
" level of %s changed to %s\n",
681 /* ========= set shape (chunk_size / layout / ndisks) ============== */
682 /* Check if layout change is a no-op */
683 if (layout_str
) switch(array
.level
) {
685 if (array
.layout
== map_name(r5layout
, layout_str
))
689 if (layout_str
== NULL
&&
690 ((chunksize
&& chunksize
* 1024 != array
.chunk_size
) ||
691 (raid_disks
&& raid_disks
!= array
.raid_disks
)) &&
692 array
.layout
>= 16) {
694 ": %s has a non-standard layout. If you wish to preserve this\n"
695 " during the reshape, please specify --layout=preserve\n"
696 " If you want to change it, specify a layout or use --layout=normalise\n",
701 if (strcmp(layout_str
, "normalise") == 0 ||
702 strcmp(layout_str
, "normalize") == 0) {
704 strcpy(alt_layout
, map_num(r6layout
, array
.layout
));
705 hyphen
= strrchr(alt_layout
, '-');
706 if (hyphen
&& strcmp(hyphen
, "-6") == 0) {
708 layout_str
= alt_layout
;
712 if (array
.layout
== map_name(r6layout
, layout_str
))
714 if (layout_str
&& strcmp(layout_str
, "preserve") == 0)
718 if (layout_str
== NULL
719 && (chunksize
== 0 || chunksize
*1024 == array
.chunk_size
)
720 && (raid_disks
== 0 || raid_disks
== array
.raid_disks
)) {
722 if (level
!= UnSet
&& level
!= array
.level
) {
723 /* Looks like this level change doesn't need
724 * a reshape after all.
726 c
= map_num(pers
, level
);
728 rv
= sysfs_set_str(sra
, NULL
, "level", c
);
730 fprintf(stderr
, Name
": %s: could not set level to %s\n",
733 } else if (!changed
&& !quiet
)
734 fprintf(stderr
, Name
": %s: no change requested\n",
739 c
= map_num(pers
, array
.level
);
740 if (c
== NULL
) c
= "-unknown-";
741 switch(array
.level
) {
742 default: /* raid0, linear, multipath cannot be reconfigured */
743 fprintf(stderr
, Name
": %s array %s cannot be reshaped.\n",
748 case LEVEL_FAULTY
: /* only 'layout' change is permitted */
750 if (chunksize
|| raid_disks
) {
751 fprintf(stderr
, Name
": %s: Cannot change chunksize or disks of a 'faulty' array\n",
756 if (layout_str
== NULL
)
757 break; /* nothing to do.... */
759 array
.layout
= parse_layout_faulty(layout_str
);
760 if (array
.layout
< 0) {
762 fprintf(stderr
, Name
": %s: layout %s not understood for 'faulty' array\n",
763 devname
, layout_str
);
767 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
768 fprintf(stderr
, Name
": Cannot set layout for %s: %s\n",
769 devname
, strerror(errno
));
772 printf("layout for %s set to %d\n", devname
, array
.layout
);
775 case 1: /* only raid_disks can each be changed. */
777 if (chunksize
|| layout_str
!= NULL
) {
778 fprintf(stderr
, Name
": %s: Cannot change chunk size or layout for a RAID1 array.\n",
783 if (raid_disks
> 0) {
784 array
.raid_disks
= raid_disks
;
785 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
786 fprintf(stderr
, Name
": Cannot set raid-devices for %s: %s\n",
787 devname
, strerror(errno
));
798 * layout/chunksize/raid_disks can be changed
799 * though the kernel may not support it all.
801 st
= super_by_fd(fd
);
804 * There are three possibilities.
805 * 1/ The array will shrink.
806 * We need to ensure the reshape will pause before reaching
807 * the 'critical section'. We also need to fork and wait for
808 * that to happen. When it does we
809 * suspend/backup/complete/unfreeze
811 * 2/ The array will not change size.
812 * This requires that we keep a backup of a sliding window
813 * so that we can restore data after a crash. So we need
814 * to fork and monitor progress.
816 * 3/ The array will grow. This is relatively easy.
817 * However the kernel's restripe routines will cheerfully
818 * overwrite some early data before it is safe. So we
819 * need to make a backup of the early parts of the array
820 * and be ready to restore it if rebuild aborts very early.
822 * We backup data by writing it to one spare, or to a
823 * file which was given on command line.
825 * [FOLLOWING IS OLD AND PARTLY WRONG]
826 * So: we enumerate the devices in the array and
827 * make sure we can open all of them.
828 * Then we freeze the early part of the array and
829 * backup to the various spares.
830 * Then we request changes and start the reshape.
831 * Monitor progress until it has passed the danger zone.
832 * and finally invalidate the copied data and unfreeze the
833 * start of the array.
835 * In each case, we first make sure that storage is available
836 * for the required backup.
838 * - request the shape change.
839 * - for to handle backup etc.
841 nchunk
= ochunk
= array
.chunk_size
;
842 nlayout
= olayout
= array
.layout
;
843 ndisks
= odisks
= array
.raid_disks
;
846 nchunk
= chunksize
* 1024;
847 if (array
.size
% chunksize
) {
848 fprintf(stderr
, Name
": component size %dK is not"
849 " a multiple of chunksize %dK\n",
850 array
.size
, chunksize
);
854 if (layout_str
!= NULL
)
855 switch(array
.level
) {
856 case 4: /* ignore layout */
859 nlayout
= map_name(r5layout
, layout_str
);
860 if (nlayout
== UnSet
) {
861 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
869 nlayout
= map_name(r6layout
, layout_str
);
870 if (nlayout
== UnSet
) {
871 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
878 if (raid_disks
) ndisks
= raid_disks
;
882 if (array
.level
== 6) {
883 odata
--; /* number of data disks */
887 if (odata
== ndata
&&
888 get_linux_version() < 2006032) {
889 fprintf(stderr
, Name
": in-place reshape is not safe before 2.6.32, sorry.\n");
893 /* Check that we can hold all the data */
894 size
= ndata
* (long long)array
.size
;
895 get_dev_size(fd
, NULL
, &array_size
);
896 if (size
< (array_size
/1024)) {
897 fprintf(stderr
, Name
": this change will reduce the size of the array.\n"
898 " use --grow --array-size first to truncate array.\n"
899 " e.g. mdadm --grow %s --array-size %llu\n",
905 /* So how much do we need to backup.
906 * We need an amount of data which is both a whole number of
907 * old stripes and a whole number of new stripes.
908 * So LCM for (chunksize*datadisks).
910 a
= ochunk
/512 * odata
;
911 b
= nchunk
/512 * ndata
;
919 /* LCM == product / GCD */
920 blocks
= ochunk
/512 * nchunk
/512 * odata
* ndata
/ a
;
923 sra
= sysfs_read(fd
, 0,
924 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
927 if (ndata
== odata
) {
928 /* Make 'blocks' bigger for better throughput, but
929 * not so big that we reject it below.
930 * Try for 16 megabytes
932 while (blocks
* 32 < sra
->component_size
&&
936 fprintf(stderr
, Name
": Need to backup %luK of critical "
937 "section..\n", blocks
/2);
940 fprintf(stderr
, Name
": %s: Cannot get array details from sysfs\n",
946 if (blocks
>= sra
->component_size
/2) {
947 fprintf(stderr
, Name
": %s: Something wrong - reshape aborted\n",
952 nrdisks
= array
.nr_disks
+ sra
->array
.spare_disks
;
953 /* Now we need to open all these devices so we can read/write.
955 fdlist
= malloc((1+nrdisks
) * sizeof(int));
956 offsets
= malloc((1+nrdisks
) * sizeof(offsets
[0]));
957 if (!fdlist
|| !offsets
) {
958 fprintf(stderr
, Name
": malloc failed: grow aborted\n");
962 for (d
=0; d
<= nrdisks
; d
++)
964 d
= array
.raid_disks
;
965 for (sd
= sra
->devs
; sd
; sd
=sd
->next
) {
966 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
968 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
969 char *dn
= map_dev(sd
->disk
.major
,
971 fdlist
[sd
->disk
.raid_disk
]
972 = dev_open(dn
, O_RDONLY
);
973 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
974 if (fdlist
[sd
->disk
.raid_disk
] < 0) {
975 fprintf(stderr
, Name
": %s: cannot open component %s\n",
976 devname
, dn
?dn
:"-unknown-");
980 } else if (backup_file
== NULL
) {
982 char *dn
= map_dev(sd
->disk
.major
,
984 fdlist
[d
] = dev_open(dn
, O_RDWR
);
985 offsets
[d
] = (sd
->data_offset
+ sra
->component_size
- blocks
- 8)*512;
987 fprintf(stderr
, Name
": %s: cannot open component %s\n",
988 devname
, dn
?dn
:"-unknown");
995 if (backup_file
== NULL
) {
996 if (ndata
<= odata
) {
997 fprintf(stderr
, Name
": %s: Cannot grow - need backup-file\n",
1001 } else if (sra
->array
.spare_disks
== 0) {
1002 fprintf(stderr
, Name
": %s: Cannot grow - need a spare or "
1003 "backup-file to backup critical section\n",
1008 if (d
== array
.raid_disks
) {
1009 fprintf(stderr
, Name
": %s: No spare device for backup\n",
1015 /* need to check backup file is large enough */
1017 fdlist
[d
] = open(backup_file
, O_RDWR
|O_CREAT
|O_EXCL
,
1019 offsets
[d
] = 8 * 512;
1020 if (fdlist
[d
] < 0) {
1021 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1022 devname
, backup_file
, strerror(errno
));
1026 memset(buf
, 0, 512);
1027 for (i
=0; i
< blocks
+ 1 ; i
++) {
1028 if (write(fdlist
[d
], buf
, 512) != 512) {
1029 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1030 devname
, backup_file
, strerror(errno
));
1035 if (fsync(fdlist
[d
]) != 0) {
1036 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1037 devname
, backup_file
, strerror(errno
));
1044 /* lastly, check that the internal stripe cache is
1045 * large enough, or it won't work.
1048 cache
= (nchunk
< ochunk
) ? ochunk
: nchunk
;
1049 cache
= cache
* 4 / 4096;
1050 if (cache
< blocks
/ 8 / odisks
+ 16)
1051 /* Make it big enough to hold 'blocks' */
1052 cache
= blocks
/ 8 / odisks
+ 16;
1053 if (sra
->cache_size
< cache
)
1054 sysfs_set_num(sra
, NULL
, "stripe_cache_size",
1056 /* Right, everything seems fine. Let's kick things off.
1057 * If only changing raid_disks, use ioctl, else use
1060 if (ochunk
== nchunk
&& olayout
== nlayout
) {
1061 array
.raid_disks
= ndisks
;
1062 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
1064 fprintf(stderr
, Name
": Cannot set device shape for %s: %s\n",
1065 devname
, strerror(errno
));
1066 if (ndisks
< odisks
&&
1067 get_linux_version() < 2006030)
1068 fprintf(stderr
, Name
": linux 2.6.30 or later required\n");
1073 /* set them all just in case some old 'new_*' value
1074 * persists from some earlier problem
1076 if (sysfs_set_num(sra
, NULL
, "chunk_size", nchunk
) < 0)
1078 if (sysfs_set_num(sra
, NULL
, "layout", nlayout
) < 0)
1080 if (sysfs_set_num(sra
, NULL
, "raid_disks", ndisks
) < 0)
1083 fprintf(stderr
, Name
": Cannot set device shape for %s\n",
1085 if (get_linux_version() < 2006030)
1086 fprintf(stderr
, Name
": linux 2.6.30 or later required\n");
1091 if (ndisks
== 2 && odisks
== 2) {
1092 /* No reshape is needed in this trivial case */
1097 /* set up the backup-super-block. This requires the
1098 * uuid from the array.
1100 /* Find a superblock */
1101 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1105 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1107 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
1108 devfd
= dev_open(dn
, O_RDONLY
);
1111 ok
= st
->ss
->load_super(st
, devfd
, NULL
);
1117 fprintf(stderr
, Name
": %s: Cannot find a superblock\n",
1123 memset(&bsb
, 0, 512);
1124 memcpy(bsb
.magic
, "md_backup_data-1", 16);
1125 st
->ss
->uuid_from_super(st
, (int*)&bsb
.set_uuid
);
1126 bsb
.mtime
= __cpu_to_le64(time(0));
1127 bsb
.devstart2
= blocks
;
1128 stripes
= blocks
/ (ochunk
/512) / odata
;
1129 /* Now we just need to kick off the reshape and watch, while
1130 * handling backups of the data...
1131 * This is all done by a forked background process.
1136 if (check_env("MDADM_GROW_VERIFY"))
1137 fd
= open(devname
, O_RDONLY
| O_DIRECT
);
1140 mlockall(MCL_FUTURE
);
1143 done
= child_grow(fd
, sra
, stripes
,
1145 odisks
, ochunk
, array
.level
, olayout
, odata
,
1146 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1147 else if (odata
> ndata
)
1148 done
= child_shrink(fd
, sra
, stripes
,
1150 odisks
, ochunk
, array
.level
, olayout
, odata
,
1151 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1153 done
= child_same_size(fd
, sra
, stripes
,
1156 odisks
, ochunk
, array
.level
, olayout
, odata
,
1157 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1158 if (backup_file
&& done
)
1159 unlink(backup_file
);
1160 if (level
!= UnSet
&& level
!= array
.level
) {
1161 /* We need to wait for the reshape to finish
1162 * (which will have happened unless odata < ndata)
1163 * and then set the level
1166 c
= map_num(pers
, level
);
1168 exit(0);/* not possible */
1172 err
= sysfs_set_str(sra
, NULL
, "level", c
);
1174 fprintf(stderr
, Name
": %s: could not set level to %s\n",
1179 fprintf(stderr
, Name
": Cannot run child to monitor reshape: %s\n",
1184 /* The child will take care of unfreezing the array */
1193 if (rv
&& orig_level
!= UnSet
&& sra
) {
1194 c
= map_num(pers
, orig_level
);
1195 if (c
&& sysfs_set_str(sra
, NULL
, "level", c
) == 0)
1196 fprintf(stderr
, Name
": aborting level change\n");
1199 unfreeze_array(sra
, frozen
);
1204 * We run a child process in the background which performs the following
1206 * - wait for resync to reach a certain point
1207 * - suspend io to the following section
1208 * - backup that section
1209 * - allow resync to proceed further
1211 * - discard the backup.
1213 * When are combined in slightly different ways in the three cases.
1215 * - suspend/backup/allow/wait/resume/discard
1217 * - allow/wait/suspend/backup/allow/wait/resume/discard
1219 * - wait/resume/discard/suspend/backup/allow
1221 * suspend/backup/allow always come together
1222 * wait/resume/discard do too.
1223 * For the same-size case we have two backups to improve flow.
1227 int grow_backup(struct mdinfo
*sra
,
1228 unsigned long long offset
, /* per device */
1229 unsigned long stripes
, /* per device */
1230 int *sources
, unsigned long long *offsets
,
1231 int disks
, int chunk
, int level
, int layout
,
1232 int dests
, int *destfd
, unsigned long long *destoffsets
,
1233 int part
, int *degraded
,
1236 /* Backup 'blocks' sectors at 'offset' on each device of the array,
1237 * to storage 'destfd' (offset 'destoffsets'), after first
1238 * suspending IO. Then allow resync to continue
1239 * over the suspended section.
1240 * Use part 'part' of the backup-super-block.
1245 unsigned long long new_degraded
;
1246 //printf("offset %llu\n", offset);
1251 sysfs_set_num(sra
, NULL
, "suspend_hi", (offset
+ stripes
* chunk
/512) * odata
);
1252 /* Check that array hasn't become degraded, else we might backup the wrong data */
1253 sysfs_get_ll(sra
, NULL
, "degraded", &new_degraded
);
1254 if (new_degraded
!= *degraded
) {
1255 /* check each device to ensure it is still working */
1257 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1258 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1260 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
1262 if (sysfs_get_str(sra
, sd
, "state", sbuf
, 20) < 0 ||
1263 strstr(sbuf
, "faulty") ||
1264 strstr(sbuf
, "in_sync") == NULL
) {
1265 /* this device is dead */
1266 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
1267 if (sd
->disk
.raid_disk
>= 0 &&
1268 sources
[sd
->disk
.raid_disk
] >= 0) {
1269 close(sources
[sd
->disk
.raid_disk
]);
1270 sources
[sd
->disk
.raid_disk
] = -1;
1275 *degraded
= new_degraded
;
1278 bsb
.arraystart2
= __cpu_to_le64(offset
* odata
);
1279 bsb
.length2
= __cpu_to_le64(stripes
* chunk
/512 * odata
);
1281 bsb
.arraystart
= __cpu_to_le64(offset
* odata
);
1282 bsb
.length
= __cpu_to_le64(stripes
* chunk
/512 * odata
);
1285 bsb
.magic
[15] = '2';
1286 for (i
= 0; i
< dests
; i
++)
1288 lseek64(destfd
[i
], destoffsets
[i
] + __le64_to_cpu(bsb
.devstart2
)*512, 0);
1290 lseek64(destfd
[i
], destoffsets
[i
], 0);
1292 rv
= save_stripes(sources
, offsets
,
1293 disks
, chunk
, level
, layout
,
1295 offset
*512*odata
, stripes
* chunk
* odata
,
1300 bsb
.mtime
= __cpu_to_le64(time(0));
1301 for (i
= 0; i
< dests
; i
++) {
1302 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
1304 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
1305 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
1306 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
1307 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
1309 lseek64(destfd
[i
], destoffsets
[i
] - 4096, 0);
1310 write(destfd
[i
], &bsb
, 512);
1311 if (destoffsets
[i
] > 4096) {
1312 lseek64(destfd
[i
], destoffsets
[i
]+stripes
*chunk
*odata
, 0);
1313 write(destfd
[i
], &bsb
, 512);
1321 /* in 2.6.30, the value reported by sync_completed can be
1322 * less that it should be by one stripe.
1323 * This only happens when reshape hits sync_max and pauses.
1324 * So allow wait_backup to either extent sync_max further
1325 * than strictly necessary, or return before the
1326 * sync has got quite as far as we would really like.
1327 * This is what 'blocks2' is for.
1328 * The various caller give appropriate values so that
1331 int wait_backup(struct mdinfo
*sra
,
1332 unsigned long long offset
, /* per device */
1333 unsigned long long blocks
, /* per device */
1334 unsigned long long blocks2
, /* per device - hack */
1335 int dests
, int *destfd
, unsigned long long *destoffsets
,
1338 /* Wait for resync to pass the section that was backed up
1339 * then erase the backup and allow IO
1341 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
1342 unsigned long long completed
;
1347 sysfs_set_num(sra
, NULL
, "sync_max", offset
+ blocks
+ blocks2
);
1349 sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
1355 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
1356 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
1360 if (sysfs_get_str(sra
, NULL
, "sync_action",
1362 strncmp(action
, "reshape", 7) != 0)
1364 } while (completed
< offset
+ blocks
);
1368 bsb
.arraystart2
= __cpu_to_le64(0);
1369 bsb
.length2
= __cpu_to_le64(0);
1371 bsb
.arraystart
= __cpu_to_le64(0);
1372 bsb
.length
= __cpu_to_le64(0);
1374 bsb
.mtime
= __cpu_to_le64(time(0));
1375 for (i
= 0; i
< dests
; i
++) {
1376 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
1377 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
1378 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
1379 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
1380 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
1381 lseek64(destfd
[i
], destoffsets
[i
]-4096, 0);
1382 write(destfd
[i
], &bsb
, 512);
1388 static void fail(char *msg
)
1390 write(2, msg
, strlen(msg
));
1395 static char *abuf
, *bbuf
;
1397 static void validate(int afd
, int bfd
, unsigned long long offset
)
1399 /* check that the data in the backup against the array.
1400 * This is only used for regression testing and should not
1401 * be used while the array is active
1405 lseek64(bfd
, offset
- 4096, 0);
1406 if (read(bfd
, &bsb2
, 512) != 512)
1407 fail("cannot read bsb");
1408 if (bsb2
.sb_csum
!= bsb_csum((char*)&bsb2
,
1409 ((char*)&bsb2
.sb_csum
)-((char*)&bsb2
)))
1410 fail("first csum bad");
1411 if (memcmp(bsb2
.magic
, "md_backup_data", 14) != 0)
1412 fail("magic is bad");
1413 if (memcmp(bsb2
.magic
, "md_backup_data-2", 16) == 0 &&
1414 bsb2
.sb_csum2
!= bsb_csum((char*)&bsb2
,
1415 ((char*)&bsb2
.sb_csum2
)-((char*)&bsb2
)))
1416 fail("second csum bad");
1418 if (__le64_to_cpu(bsb2
.devstart
)*512 != offset
)
1419 fail("devstart is wrong");
1422 unsigned long long len
= __le64_to_cpu(bsb2
.length
)*512;
1424 if (abuflen
< len
) {
1428 posix_memalign((void**)&abuf
, 4096, abuflen
);
1429 posix_memalign((void**)&bbuf
, 4096, abuflen
);
1432 lseek64(bfd
, offset
, 0);
1433 if (read(bfd
, bbuf
, len
) != len
) {
1434 printf("len %llu\n", len
);
1435 fail("read first backup failed");
1437 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart
)*512, 0);
1438 if (read(afd
, abuf
, len
) != len
)
1439 fail("read first from array failed");
1440 if (memcmp(bbuf
, abuf
, len
) != 0) {
1442 printf("offset=%llu len=%llu\n",
1443 __le64_to_cpu(bsb2
.arraystart
)*512, len
);
1444 for (i
=0; i
<len
; i
++)
1445 if (bbuf
[i
] != abuf
[i
]) {
1446 printf("first diff byte %d\n", i
);
1449 fail("data1 compare failed");
1453 unsigned long long len
= __le64_to_cpu(bsb2
.length2
)*512;
1455 if (abuflen
< len
) {
1459 abuf
= malloc(abuflen
);
1460 bbuf
= malloc(abuflen
);
1463 lseek64(bfd
, offset
+__le64_to_cpu(bsb2
.devstart2
)*512, 0);
1464 if (read(bfd
, bbuf
, len
) != len
)
1465 fail("read second backup failed");
1466 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart2
)*512, 0);
1467 if (read(afd
, abuf
, len
) != len
)
1468 fail("read second from array failed");
1469 if (memcmp(bbuf
, abuf
, len
) != 0)
1470 fail("data2 compare failed");
1474 static int child_grow(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1475 int *fds
, unsigned long long *offsets
,
1476 int disks
, int chunk
, int level
, int layout
, int data
,
1477 int dests
, int *destfd
, unsigned long long *destoffsets
)
1482 posix_memalign((void**)&buf
, 4096, disks
* chunk
);
1483 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1484 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1485 grow_backup(sra
, 0, stripes
,
1486 fds
, offsets
, disks
, chunk
, level
, layout
,
1487 dests
, destfd
, destoffsets
,
1489 validate(afd
, destfd
[0], destoffsets
[0]);
1490 wait_backup(sra
, 0, stripes
* chunk
/ 512, stripes
* chunk
/ 512,
1491 dests
, destfd
, destoffsets
,
1493 sysfs_set_num(sra
, NULL
, "suspend_lo", (stripes
* chunk
/512) * data
);
1495 /* FIXME this should probably be numeric */
1496 sysfs_set_str(sra
, NULL
, "sync_max", "max");
1500 static int child_shrink(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1501 int *fds
, unsigned long long *offsets
,
1502 int disks
, int chunk
, int level
, int layout
, int data
,
1503 int dests
, int *destfd
, unsigned long long *destoffsets
)
1506 unsigned long long start
;
1510 posix_memalign((void**)&buf
, 4096, disks
* chunk
);
1511 start
= sra
->component_size
- stripes
* chunk
/512;
1512 sysfs_set_num(sra
, NULL
, "sync_max", start
);
1513 sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
1514 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1515 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1516 rv
= wait_backup(sra
, 0, start
- stripes
* chunk
/512, stripes
* chunk
/512,
1517 dests
, destfd
, destoffsets
, 0);
1520 grow_backup(sra
, 0, stripes
,
1522 disks
, chunk
, level
, layout
,
1523 dests
, destfd
, destoffsets
,
1525 validate(afd
, destfd
[0], destoffsets
[0]);
1526 wait_backup(sra
, start
, stripes
*chunk
/512, 0,
1527 dests
, destfd
, destoffsets
, 0);
1528 sysfs_set_num(sra
, NULL
, "suspend_lo", (stripes
* chunk
/512) * data
);
1530 /* FIXME this should probably be numeric */
1531 sysfs_set_str(sra
, NULL
, "sync_max", "max");
1535 static int child_same_size(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1536 int *fds
, unsigned long long *offsets
,
1537 unsigned long long start
,
1538 int disks
, int chunk
, int level
, int layout
, int data
,
1539 int dests
, int *destfd
, unsigned long long *destoffsets
)
1541 unsigned long long size
;
1542 unsigned long tailstripes
= stripes
;
1545 unsigned long long speed
;
1549 posix_memalign((void**)&buf
, 4096, disks
* chunk
);
1551 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1552 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1554 sysfs_get_ll(sra
, NULL
, "sync_speed_min", &speed
);
1555 sysfs_set_num(sra
, NULL
, "sync_speed_min", 200000);
1557 grow_backup(sra
, start
, stripes
,
1559 disks
, chunk
, level
, layout
,
1560 dests
, destfd
, destoffsets
,
1562 grow_backup(sra
, (start
+ stripes
) * chunk
/512, stripes
,
1564 disks
, chunk
, level
, layout
,
1565 dests
, destfd
, destoffsets
,
1567 validate(afd
, destfd
[0], destoffsets
[0]);
1569 start
+= stripes
* 2; /* where to read next */
1570 size
= sra
->component_size
/ (chunk
/512);
1571 while (start
< size
) {
1572 if (wait_backup(sra
, (start
-stripes
*2)*chunk
/512,
1573 stripes
*chunk
/512, 0,
1574 dests
, destfd
, destoffsets
,
1577 sysfs_set_num(sra
, NULL
, "suspend_lo", start
*chunk
/512 * data
);
1578 if (start
+ stripes
> size
)
1579 tailstripes
= (size
- start
);
1581 grow_backup(sra
, start
*chunk
/512, tailstripes
,
1583 disks
, chunk
, level
, layout
,
1584 dests
, destfd
, destoffsets
,
1585 part
, °raded
, buf
);
1588 validate(afd
, destfd
[0], destoffsets
[0]);
1590 if (wait_backup(sra
, (start
-stripes
*2) * chunk
/512, stripes
* chunk
/512, 0,
1591 dests
, destfd
, destoffsets
,
1594 sysfs_set_num(sra
, NULL
, "suspend_lo", ((start
-stripes
)*chunk
/512) * data
);
1595 wait_backup(sra
, (start
-stripes
) * chunk
/512, tailstripes
* chunk
/512, 0,
1596 dests
, destfd
, destoffsets
,
1598 sysfs_set_num(sra
, NULL
, "suspend_lo", (size
*chunk
/512) * data
);
1599 sysfs_set_num(sra
, NULL
, "sync_speed_min", speed
);
1605 * If any spare contains md_back_data-1 which is recent wrt mtime,
1606 * write that data into the array and update the super blocks with
1607 * the new reshape_progress
1609 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
, int cnt
,
1610 char *backup_file
, int verbose
)
1614 unsigned long long *offsets
;
1615 unsigned long long nstripe
, ostripe
;
1618 if (info
->new_level
!= info
->array
.level
)
1619 return 1; /* Cannot handle level changes (they are instantaneous) */
1621 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
1622 if (info
->array
.level
== 6) odata
--; /* number of data disks */
1623 ndata
= info
->array
.raid_disks
- 1;
1624 if (info
->new_level
== 6) ndata
--;
1626 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
1628 if (info
->delta_disks
<= 0)
1629 /* Didn't grow, so the backup file must have
1633 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
1634 struct mdinfo dinfo
;
1637 char *devname
, namebuf
[20];
1639 /* This was a spare and may have some saved data on it.
1640 * Load the superblock, find and load the
1641 * backup_super_block.
1642 * If either fail, go on to next device.
1643 * If the backup contains no new info, just return
1644 * else restore data and update all superblocks
1646 if (i
== old_disks
-1) {
1647 fd
= open(backup_file
, O_RDONLY
);
1649 fprintf(stderr
, Name
": backup file %s inaccessible: %s\n",
1650 backup_file
, strerror(errno
));
1653 devname
= backup_file
;
1658 if (st
->ss
->load_super(st
, fd
, NULL
))
1661 st
->ss
->getinfo_super(st
, &dinfo
);
1662 st
->ss
->free_super(st
);
1665 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
1667 fprintf(stderr
, Name
": Cannot seek on device %d\n", i
);
1668 continue; /* Cannot seek */
1670 sprintf(namebuf
, "device-%d", i
);
1673 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
)) {
1675 fprintf(stderr
, Name
": Cannot read from %s\n", devname
);
1676 continue; /* Cannot read */
1678 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0 &&
1679 memcmp(bsb
.magic
, "md_backup_data-2", 16) != 0) {
1681 fprintf(stderr
, Name
": No backup metadata on %s\n", devname
);
1684 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
))) {
1686 fprintf(stderr
, Name
": Bad backup-metadata checksum on %s\n", devname
);
1687 continue; /* bad checksum */
1689 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0 &&
1690 bsb
.sb_csum2
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum2
)-((char*)&bsb
))) {
1692 fprintf(stderr
, Name
": Bad backup-metadata checksum2 on %s\n", devname
);
1693 continue; /* Bad second checksum */
1695 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0) {
1697 fprintf(stderr
, Name
": Wrong uuid on backup-metadata on %s\n", devname
);
1698 continue; /* Wrong uuid */
1701 if (info
->array
.utime
> __le64_to_cpu(bsb
.mtime
) + 10*60 ||
1702 info
->array
.utime
< __le64_to_cpu(bsb
.mtime
) - 10*60) {
1704 fprintf(stderr
, Name
": too-old timestamp on backup-metadata on %s\n", devname
);
1705 continue; /* time stamp is too bad */
1708 if (bsb
.magic
[15] == '1') {
1709 if (info
->delta_disks
>= 0) {
1710 /* reshape_progress is increasing */
1711 if (__le64_to_cpu(bsb
.arraystart
) + __le64_to_cpu(bsb
.length
) <
1712 info
->reshape_progress
) {
1715 fprintf(stderr
, Name
": backup-metadata found on %s but is not needed\n", devname
);
1716 continue; /* No new data here */
1719 /* reshape_progress is decreasing */
1720 if (__le64_to_cpu(bsb
.arraystart
) >=
1721 info
->reshape_progress
)
1722 goto nonew
; /* No new data here */
1725 if (info
->delta_disks
>= 0) {
1726 /* reshape_progress is increasing */
1727 if (__le64_to_cpu(bsb
.arraystart
) + __le64_to_cpu(bsb
.length
) <
1728 info
->reshape_progress
&&
1729 __le64_to_cpu(bsb
.arraystart2
) + __le64_to_cpu(bsb
.length2
) <
1730 info
->reshape_progress
)
1731 goto nonew
; /* No new data here */
1733 /* reshape_progress is decreasing */
1734 if (__le64_to_cpu(bsb
.arraystart
) >=
1735 info
->reshape_progress
&&
1736 __le64_to_cpu(bsb
.arraystart2
) >=
1737 info
->reshape_progress
)
1738 goto nonew
; /* No new data here */
1741 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0) {
1744 fprintf(stderr
, Name
": Failed to verify secondary backup-metadata block on %s\n",
1746 continue; /* Cannot seek */
1748 /* There should be a duplicate backup superblock 4k before here */
1749 if (lseek64(fd
, -4096, 1) < 0 ||
1750 read(fd
, &bsb2
, 4096) != 4096)
1751 goto second_fail
; /* Cannot find leading superblock */
1752 if (bsb
.magic
[15] == '1')
1753 bsbsize
= offsetof(struct mdp_backup_super
, pad1
);
1755 bsbsize
= offsetof(struct mdp_backup_super
, pad
);
1756 if (memcmp(&bsb2
, &bsb
, bsbsize
) != 0)
1757 goto second_fail
; /* Cannot find leading superblock */
1759 /* Now need the data offsets for all devices. */
1760 offsets
= malloc(sizeof(*offsets
)*info
->array
.raid_disks
);
1761 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
1764 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
1765 /* FIXME should be this be an error */
1767 st
->ss
->getinfo_super(st
, &dinfo
);
1768 st
->ss
->free_super(st
);
1769 offsets
[j
] = dinfo
.data_offset
* 512;
1771 printf(Name
": restoring critical section\n");
1773 if (restore_stripes(fdlist
, offsets
,
1774 info
->array
.raid_disks
,
1778 fd
, __le64_to_cpu(bsb
.devstart
)*512,
1779 __le64_to_cpu(bsb
.arraystart
)*512,
1780 __le64_to_cpu(bsb
.length
)*512)) {
1781 /* didn't succeed, so giveup */
1783 fprintf(stderr
, Name
": Error restoring backup from %s\n",
1788 if (bsb
.magic
[15] == '2' &&
1789 restore_stripes(fdlist
, offsets
,
1790 info
->array
.raid_disks
,
1794 fd
, __le64_to_cpu(bsb
.devstart
)*512 +
1795 __le64_to_cpu(bsb
.devstart2
)*512,
1796 __le64_to_cpu(bsb
.arraystart2
)*512,
1797 __le64_to_cpu(bsb
.length2
)*512)) {
1798 /* didn't succeed, so giveup */
1800 fprintf(stderr
, Name
": Error restoring second backup from %s\n",
1806 /* Ok, so the data is restored. Let's update those superblocks. */
1808 if (info
->delta_disks
>= 0) {
1809 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
) +
1810 __le64_to_cpu(bsb
.length
);
1811 if (bsb
.magic
[15] == '2') {
1812 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
) +
1813 __le64_to_cpu(bsb
.length2
);
1814 if (p2
> info
->reshape_progress
)
1815 info
->reshape_progress
= p2
;
1818 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
);
1819 if (bsb
.magic
[15] == '2') {
1820 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
);
1821 if (p2
< info
->reshape_progress
)
1822 info
->reshape_progress
= p2
;
1825 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
1826 if (fdlist
[j
] < 0) continue;
1827 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
1829 st
->ss
->getinfo_super(st
, &dinfo
);
1830 dinfo
.reshape_progress
= info
->reshape_progress
;
1831 st
->ss
->update_super(st
, &dinfo
,
1832 "_reshape_progress",
1834 st
->ss
->store_super(st
, fdlist
[j
]);
1835 st
->ss
->free_super(st
);
1839 /* Didn't find any backup data, try to see if any
1842 if (info
->delta_disks
< 0) {
1843 /* When shrinking, the critical section is at the end.
1844 * So see if we are before the critical section.
1846 unsigned long long first_block
;
1847 nstripe
= ostripe
= 0;
1849 while (ostripe
>= nstripe
) {
1850 ostripe
+= info
->array
.chunk_size
/ 512;
1851 first_block
= ostripe
* odata
;
1852 nstripe
= first_block
/ ndata
/ (info
->new_chunk
/512) *
1853 (info
->new_chunk
/512);
1856 if (info
->reshape_progress
>= first_block
)
1859 if (info
->delta_disks
> 0) {
1860 /* See if we are beyond the critical section. */
1861 unsigned long long last_block
;
1862 nstripe
= ostripe
= 0;
1864 while (nstripe
>= ostripe
) {
1865 nstripe
+= info
->new_chunk
/ 512;
1866 last_block
= nstripe
* ndata
;
1867 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
1868 (info
->array
.chunk_size
/512);
1871 if (info
->reshape_progress
>= last_block
)
1874 /* needed to recover critical section! */
1876 fprintf(stderr
, Name
": Failed to find backup of critical section\n");
1880 int Grow_continue(int mdfd
, struct supertype
*st
, struct mdinfo
*info
,
1883 /* Array is assembled and ready to be started, but
1884 * monitoring is probably required.
1887 * - set upper bound for resync
1888 * - initialise the 'suspend' boundaries
1889 * - switch to read-write
1890 * - fork and continue monitoring
1894 unsigned long long backup_offsets
[1];
1895 int odisks
, ndisks
, ochunk
, nchunk
,odata
,ndata
;
1896 unsigned long a
,b
,blocks
,stripes
;
1899 unsigned long long *offsets
;
1901 struct mdinfo
*sra
, *sd
;
1906 err
= sysfs_set_str(info
, NULL
, "array_state", "readonly");
1910 /* make sure reshape doesn't progress until we are ready */
1911 sysfs_set_str(info
, NULL
, "sync_max", "0");
1912 sysfs_set_str(info
, NULL
, "array_state", "active"); /* FIXME or clean */
1914 /* ndisks is not growing, so raid_disks is old and +delta is new */
1915 odisks
= info
->array
.raid_disks
;
1916 ndisks
= odisks
+ info
->delta_disks
;
1919 if (info
->array
.level
== 6) {
1923 ochunk
= info
->array
.chunk_size
;
1924 nchunk
= info
->new_chunk
;
1927 a
= ochunk
/512 * odata
;
1928 b
= nchunk
/512 * ndata
;
1936 /* LCM == product / GCD */
1937 blocks
= ochunk
/512 * nchunk
/512 * odata
* ndata
/ a
;
1939 sra
= sysfs_read(-1, devname2devnum(info
->sys_name
),
1940 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
1945 while (blocks
* 32 < sra
->component_size
&&
1948 stripes
= blocks
/ (info
->array
.chunk_size
/512) / odata
;
1950 /* check that the internal stripe cache is
1951 * large enough, or it won't work.
1953 cache
= (nchunk
< ochunk
) ? ochunk
: nchunk
;
1954 cache
= cache
* 4 / 4096;
1955 if (cache
< blocks
/ 8 / odisks
+ 16)
1956 /* Make it big enough to hold 'blocks' */
1957 cache
= blocks
/ 8 / odisks
+ 16;
1958 if (sra
->cache_size
< cache
)
1959 sysfs_set_num(sra
, NULL
, "stripe_cache_size",
1962 memset(&bsb
, 0, 512);
1963 memcpy(bsb
.magic
, "md_backup_data-1", 16);
1964 memcpy(&bsb
.set_uuid
, info
->uuid
, 16);
1965 bsb
.mtime
= __cpu_to_le64(time(0));
1966 bsb
.devstart2
= blocks
;
1968 backup_fd
= open(backup_file
, O_RDWR
|O_CREAT
, S_IRUSR
| S_IWUSR
);
1969 backup_list
[0] = backup_fd
;
1970 backup_offsets
[0] = 8 * 512;
1971 fds
= malloc(odisks
* sizeof(fds
[0]));
1972 offsets
= malloc(odisks
* sizeof(offsets
[0]));
1973 for (d
=0; d
<odisks
; d
++)
1976 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1977 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1979 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
1980 char *dn
= map_dev(sd
->disk
.major
,
1982 fds
[sd
->disk
.raid_disk
]
1983 = dev_open(dn
, O_RDONLY
);
1984 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
1985 if (fds
[sd
->disk
.raid_disk
] < 0) {
1986 fprintf(stderr
, Name
": %s: cannot open component %s\n",
1987 info
->sys_name
, dn
?dn
:"-unknown-");
1998 mlockall(MCL_FUTURE
);
1999 if (info
->delta_disks
< 0)
2000 done
= child_shrink(-1, info
, stripes
,
2002 info
->array
.raid_disks
,
2003 info
->array
.chunk_size
,
2004 info
->array
.level
, info
->array
.layout
,
2006 1, backup_list
, backup_offsets
);
2007 else if (info
->delta_disks
== 0) {
2008 /* The 'start' is a per-device stripe number.
2009 * reshape_progress is a per-array sector number.
2010 * So divide by ndata * chunk_size
2012 unsigned long long start
= info
->reshape_progress
/ ndata
;
2013 start
/= (info
->array
.chunk_size
/512);
2014 done
= child_same_size(-1, info
, stripes
,
2017 info
->array
.raid_disks
,
2018 info
->array
.chunk_size
,
2019 info
->array
.level
, info
->array
.layout
,
2021 1, backup_list
, backup_offsets
);
2023 if (backup_file
&& done
)
2024 unlink(backup_file
);
2025 /* FIXME should I intuit a level change */
2028 fprintf(stderr
, Name
": Cannot run child to continue monitoring reshape: %s\n",