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 if (st
->ss
->add_internal_bitmap
== NULL
) {
292 fprintf(stderr
, Name
": Internal bitmaps not supported "
293 "with %s metadata\n", st
->ss
->name
);
296 for (d
=0; d
< st
->max_devs
; d
++) {
297 mdu_disk_info_t disk
;
300 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
302 if (disk
.major
== 0 &&
305 if ((disk
.state
& (1<<MD_DISK_SYNC
))==0)
307 dv
= map_dev(disk
.major
, disk
.minor
, 1);
309 int fd2
= dev_open(dv
, O_RDWR
);
312 if (st
->ss
->load_super(st
, fd2
, NULL
)==0) {
313 if (st
->ss
->add_internal_bitmap(
315 &chunk
, delay
, write_behind
,
316 bitmapsize
, 0, major
)
318 st
->ss
->write_bitmap(st
, fd2
);
320 fprintf(stderr
, Name
": failed to create internal bitmap - chunksize problem.\n");
328 array
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
329 if (ioctl(fd
, SET_ARRAY_INFO
, &array
)!= 0) {
330 fprintf(stderr
, Name
": failed to set internal bitmap.\n");
337 int max_devs
= st
->max_devs
;
339 /* try to load a superblock */
340 for (d
=0; d
<max_devs
; d
++) {
341 mdu_disk_info_t disk
;
345 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
347 if ((disk
.major
==0 && disk
.minor
==0) ||
348 (disk
.state
& (1<<MD_DISK_REMOVED
)))
350 dv
= map_dev(disk
.major
, disk
.minor
, 1);
352 fd2
= dev_open(dv
, O_RDONLY
);
354 st
->ss
->load_super(st
, fd2
, NULL
) == 0) {
356 st
->ss
->uuid_from_super(st
, uuid
);
362 fprintf(stderr
, Name
": cannot find UUID for array!\n");
365 if (CreateBitmap(file
, force
, (char*)uuid
, chunk
,
366 delay
, write_behind
, bitmapsize
, major
)) {
369 bitmap_fd
= open(file
, O_RDWR
);
371 fprintf(stderr
, Name
": weird: %s cannot be opened\n",
375 if (ioctl(fd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
376 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
377 devname
, strerror(errno
));
387 * When reshaping an array we might need to backup some data.
388 * This is written to all spares with a 'super_block' describing it.
389 * The superblock goes 4K from the end of the used space on the
391 * It if written after the backup is complete.
392 * It has the following structure.
395 static struct mdp_backup_super
{
396 char magic
[16]; /* md_backup_data-1 or -2 */
399 /* start/sizes in 512byte sectors */
400 __u64 devstart
; /* address on backup device/file of data */
403 __u32 sb_csum
; /* csum of preceeding bytes. */
405 __u64 devstart2
; /* offset in to data of second section */
408 __u32 sb_csum2
; /* csum of preceeding bytes. */
410 } __attribute__((aligned(512))) bsb
, bsb2
;
412 __u32
bsb_csum(char *buf
, int len
)
416 for (i
=0; i
<len
; i
++)
417 csum
= (csum
<<3) + buf
[0];
418 return __cpu_to_le32(csum
);
421 static int child_grow(int afd
, struct mdinfo
*sra
, unsigned long blocks
,
422 int *fds
, unsigned long long *offsets
,
423 int disks
, int chunk
, int level
, int layout
, int data
,
424 int dests
, int *destfd
, unsigned long long *destoffsets
);
425 static int child_shrink(int afd
, struct mdinfo
*sra
, unsigned long blocks
,
426 int *fds
, unsigned long long *offsets
,
427 int disks
, int chunk
, int level
, int layout
, int data
,
428 int dests
, int *destfd
, unsigned long long *destoffsets
);
429 static int child_same_size(int afd
, struct mdinfo
*sra
, unsigned long blocks
,
430 int *fds
, unsigned long long *offsets
,
431 unsigned long long start
,
432 int disks
, int chunk
, int level
, int layout
, int data
,
433 int dests
, int *destfd
, unsigned long long *destoffsets
);
435 int freeze_array(struct mdinfo
*sra
)
437 /* Try to freeze resync on this array.
438 * Return -1 if the array is busy,
439 * return 0 if this kernel doesn't support 'frozen'
440 * return 1 if it worked.
443 if (sysfs_get_str(sra
, NULL
, "sync_action", buf
, 20) <= 0)
445 if (strcmp(buf
, "idle\n") != 0 &&
446 strcmp(buf
, "frozen\n") != 0)
448 if (sysfs_set_str(sra
, NULL
, "sync_action", "frozen") < 0)
453 void unfreeze_array(struct mdinfo
*sra
, int frozen
)
455 /* If 'frozen' is 1, unfreeze the array */
457 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
460 void wait_reshape(struct mdinfo
*sra
)
462 int fd
= sysfs_get_fd(sra
, NULL
, "sync_action");
469 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
471 if (sysfs_fd_get_str(fd
, action
, 20) < 0) {
475 } while (strncmp(action
, "reshape", 7) == 0);
479 int Grow_reshape(char *devname
, int fd
, int quiet
, char *backup_file
,
481 int level
, char *layout_str
, int chunksize
, int raid_disks
)
483 /* Make some changes in the shape of an array.
484 * The kernel must support the change.
486 * There are three different changes. Each can trigger
487 * a resync or recovery so we freeze that until we have
488 * requested everything (if kernel supports freezing - 2.6.30).
490 * - change size (i.e. component_size)
492 * - change layout/chunksize/ndisks
494 * The last can require a reshape. It is different on different
495 * levels so we need to check the level before actioning it.
496 * Some times the level change needs to be requested after the
497 * reshape (e.g. raid6->raid5, raid5->raid0)
500 struct mdu_array_info_s array
, orig
;
503 struct supertype
*st
;
506 int nlayout
, olayout
;
508 unsigned int ndata
, odata
;
509 int orig_level
= UnSet
;
512 unsigned long long *offsets
;
517 unsigned long a
,b
, blocks
, stripes
;
519 unsigned long long array_size
;
526 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) < 0) {
527 fprintf(stderr
, Name
": %s is not an active md array - aborting\n",
533 (chunksize
|| level
!= UnSet
|| layout_str
|| raid_disks
)) {
534 fprintf(stderr
, Name
": cannot change component size at the same time "
535 "as other changes.\n"
536 " Change size first, then check data is intact before "
537 "making other changes.\n");
541 if (raid_disks
&& raid_disks
< array
.raid_disks
&& array
.level
> 1 &&
542 get_linux_version() < 2006032 &&
543 !check_env("MDADM_FORCE_FEWER")) {
544 fprintf(stderr
, Name
": reducing the number of devices is not safe before Linux 2.6.32\n"
545 " Please use a newer kernel\n");
548 sra
= sysfs_read(fd
, 0, GET_LEVEL
);
550 frozen
= freeze_array(sra
);
552 fprintf(stderr
, Name
": failed to read sysfs parameters for %s\n",
557 fprintf(stderr
, Name
": %s is performing resync/recovery and cannot"
558 " be reshaped\n", devname
);
562 /* ========= set size =============== */
563 if (size
>= 0 && (size
== 0 || size
!= array
.size
)) {
565 if (array
.size
!= size
) {
566 /* got truncated to 32bit, write to
567 * component_size instead
570 rv
= sysfs_set_num(sra
, NULL
,
571 "component_size", size
);
575 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
578 fprintf(stderr
, Name
": Cannot set device size for %s: %s\n",
579 devname
, strerror(err
));
581 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
582 fprintf(stderr
, " Bitmap must be removed before size can be changed\n");
586 ioctl(fd
, GET_ARRAY_INFO
, &array
);
587 size
= get_component_size(fd
)/2;
591 fprintf(stderr
, Name
": component size of %s has been set to %lluK\n",
595 size
= get_component_size(fd
)/2;
600 /* ======= set level =========== */
601 if (level
!= UnSet
&& level
!= array
.level
) {
602 /* Trying to change the level.
603 * We might need to change layout first and schedule a
604 * level change for later.
605 * Level changes that can happen immediately are:
606 * 0->4,5,6 1->5 4->5,6 5->1,6
607 * Level changes that need a layout change first are:
608 * 6->5,4,0 : need a -6 layout, or parity-last
609 * 5->4,0 : need parity-last
611 if ((array
.level
== 6 || array
.level
== 5) &&
612 (level
== 5 || level
== 4 || level
== 0)) {
613 /* Don't change level yet, but choose intermediate
617 if (layout_str
== NULL
)
618 switch (array
.layout
) {
619 case ALGORITHM_LEFT_ASYMMETRIC
:
620 case ALGORITHM_LEFT_ASYMMETRIC_6
:
621 case ALGORITHM_ROTATING_N_RESTART
:
622 layout_str
= "left-asymmetric-6";
624 case ALGORITHM_LEFT_SYMMETRIC
:
625 case ALGORITHM_LEFT_SYMMETRIC_6
:
626 case ALGORITHM_ROTATING_N_CONTINUE
:
627 layout_str
= "left-symmetric-6";
629 case ALGORITHM_RIGHT_ASYMMETRIC
:
630 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
631 case ALGORITHM_ROTATING_ZERO_RESTART
:
632 layout_str
= "right-asymmetric-6";
634 case ALGORITHM_RIGHT_SYMMETRIC
:
635 case ALGORITHM_RIGHT_SYMMETRIC_6
:
636 layout_str
= "right-symmetric-6";
638 case ALGORITHM_PARITY_0
:
639 case ALGORITHM_PARITY_0_6
:
640 layout_str
= "parity-first-6";
642 case ALGORITHM_PARITY_N
:
643 layout_str
= "parity-last";
646 fprintf(stderr
, Name
": %s: cannot"
647 "convert layout to RAID5 equivalent\n",
653 int l
= map_name(r5layout
, layout_str
);
655 fprintf(stderr
, Name
": %s: layout '%s' not recognised\n",
656 devname
, layout_str
);
660 if (l
!= ALGORITHM_PARITY_N
) {
661 /* need the -6 version */
662 char *ls
= map_num(r5layout
, l
);
663 strcat(strcpy(alt_layout
, ls
),
665 layout_str
= alt_layout
;
669 /* The final raid6->raid5 conversion
670 * will reduce the number of disks,
671 * so now we need to aim higher
675 layout_str
= "parity-last";
677 c
= map_num(pers
, level
);
679 rv
= 1;/* not possible */
682 err
= sysfs_set_str(sra
, NULL
, "level", c
);
685 fprintf(stderr
, Name
": %s: could not set level to %s\n",
688 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
689 fprintf(stderr
, " Bitmap must be removed before level can be changed\n");
694 orig_level
= orig
.level
;
695 ioctl(fd
, GET_ARRAY_INFO
, &array
);
696 if (layout_str
== NULL
&&
697 orig
.level
== 5 && level
== 6 &&
698 array
.layout
!= orig
.layout
)
699 layout_str
= map_num(r5layout
, orig
.layout
);
701 fprintf(stderr
, Name
" level of %s changed to %s\n",
707 /* ========= set shape (chunk_size / layout / ndisks) ============== */
708 /* Check if layout change is a no-op */
709 if (layout_str
) switch(array
.level
) {
711 if (array
.layout
== map_name(r5layout
, layout_str
))
715 if (layout_str
== NULL
&&
716 ((chunksize
&& chunksize
* 1024 != array
.chunk_size
) ||
717 (raid_disks
&& raid_disks
!= array
.raid_disks
)) &&
718 array
.layout
>= 16) {
720 ": %s has a non-standard layout. If you wish to preserve this\n"
721 " during the reshape, please specify --layout=preserve\n"
722 " If you want to change it, specify a layout or use --layout=normalise\n",
727 if (strcmp(layout_str
, "normalise") == 0 ||
728 strcmp(layout_str
, "normalize") == 0) {
730 strcpy(alt_layout
, map_num(r6layout
, array
.layout
));
731 hyphen
= strrchr(alt_layout
, '-');
732 if (hyphen
&& strcmp(hyphen
, "-6") == 0) {
734 layout_str
= alt_layout
;
738 if (array
.layout
== map_name(r6layout
, layout_str
))
740 if (layout_str
&& strcmp(layout_str
, "preserve") == 0)
744 if (layout_str
== NULL
745 && (chunksize
== 0 || chunksize
*1024 == array
.chunk_size
)
746 && (raid_disks
== 0 || raid_disks
== array
.raid_disks
)) {
748 if (level
!= UnSet
&& level
!= array
.level
) {
749 /* Looks like this level change doesn't need
750 * a reshape after all.
752 c
= map_num(pers
, level
);
754 rv
= sysfs_set_str(sra
, NULL
, "level", c
);
757 fprintf(stderr
, Name
": %s: could not set level to %s\n",
760 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
761 fprintf(stderr
, " Bitmap must be removed before level can be changed\n");
765 } else if (!changed
&& !quiet
)
766 fprintf(stderr
, Name
": %s: no change requested\n",
771 c
= map_num(pers
, array
.level
);
772 if (c
== NULL
) c
= "-unknown-";
773 switch(array
.level
) {
774 default: /* raid0, linear, multipath cannot be reconfigured */
775 fprintf(stderr
, Name
": %s array %s cannot be reshaped.\n",
780 case LEVEL_FAULTY
: /* only 'layout' change is permitted */
782 if (chunksize
|| raid_disks
) {
783 fprintf(stderr
, Name
": %s: Cannot change chunksize or disks of a 'faulty' array\n",
788 if (layout_str
== NULL
)
789 break; /* nothing to do.... */
791 array
.layout
= parse_layout_faulty(layout_str
);
792 if (array
.layout
< 0) {
793 fprintf(stderr
, Name
": %s: layout %s not understood for 'faulty' array\n",
794 devname
, layout_str
);
798 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
799 fprintf(stderr
, Name
": Cannot set layout for %s: %s\n",
800 devname
, strerror(errno
));
803 printf("layout for %s set to %d\n", devname
, array
.layout
);
806 case 1: /* only raid_disks can each be changed. */
808 if (chunksize
|| layout_str
!= NULL
) {
809 fprintf(stderr
, Name
": %s: Cannot change chunk size or layout for a RAID1 array.\n",
814 if (raid_disks
> 0) {
815 array
.raid_disks
= raid_disks
;
816 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
817 fprintf(stderr
, Name
": Cannot set raid-devices for %s: %s\n",
818 devname
, strerror(errno
));
829 * layout/chunksize/raid_disks can be changed
830 * though the kernel may not support it all.
832 st
= super_by_fd(fd
);
835 * There are three possibilities.
836 * 1/ The array will shrink.
837 * We need to ensure the reshape will pause before reaching
838 * the 'critical section'. We also need to fork and wait for
839 * that to happen. When it does we
840 * suspend/backup/complete/unfreeze
842 * 2/ The array will not change size.
843 * This requires that we keep a backup of a sliding window
844 * so that we can restore data after a crash. So we need
845 * to fork and monitor progress.
847 * 3/ The array will grow. This is relatively easy.
848 * However the kernel's restripe routines will cheerfully
849 * overwrite some early data before it is safe. So we
850 * need to make a backup of the early parts of the array
851 * and be ready to restore it if rebuild aborts very early.
853 * We backup data by writing it to one spare, or to a
854 * file which was given on command line.
856 * [FOLLOWING IS OLD AND PARTLY WRONG]
857 * So: we enumerate the devices in the array and
858 * make sure we can open all of them.
859 * Then we freeze the early part of the array and
860 * backup to the various spares.
861 * Then we request changes and start the reshape.
862 * Monitor progress until it has passed the danger zone.
863 * and finally invalidate the copied data and unfreeze the
864 * start of the array.
866 * In each case, we first make sure that storage is available
867 * for the required backup.
869 * - request the shape change.
870 * - for to handle backup etc.
872 nchunk
= ochunk
= array
.chunk_size
;
873 nlayout
= olayout
= array
.layout
;
874 ndisks
= odisks
= array
.raid_disks
;
877 nchunk
= chunksize
* 1024;
878 if (size
% chunksize
) {
879 fprintf(stderr
, Name
": component size %lluK is not"
880 " a multiple of chunksize %dK\n",
885 if (layout_str
!= NULL
)
886 switch(array
.level
) {
887 case 4: /* ignore layout */
890 nlayout
= map_name(r5layout
, layout_str
);
891 if (nlayout
== UnSet
) {
892 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
900 nlayout
= map_name(r6layout
, layout_str
);
901 if (nlayout
== UnSet
) {
902 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
909 if (raid_disks
) ndisks
= raid_disks
;
913 if (array
.level
== 6) {
914 odata
--; /* number of data disks */
918 if (odata
== ndata
&&
919 get_linux_version() < 2006032) {
920 fprintf(stderr
, Name
": in-place reshape is not safe before 2.6.32, sorry.\n");
924 /* Check that we can hold all the data */
925 get_dev_size(fd
, NULL
, &array_size
);
926 if (ndata
* (unsigned long long)size
< (array_size
/1024)) {
927 fprintf(stderr
, Name
": this change will reduce the size of the array.\n"
928 " use --grow --array-size first to truncate array.\n"
929 " e.g. mdadm --grow %s --array-size %llu\n",
930 devname
, ndata
* size
);
935 /* So how much do we need to backup.
936 * We need an amount of data which is both a whole number of
937 * old stripes and a whole number of new stripes.
938 * So LCM for (chunksize*datadisks).
940 a
= (ochunk
/512) * odata
;
941 b
= (nchunk
/512) * ndata
;
949 /* LCM == product / GCD */
950 blocks
= (ochunk
/512) * (nchunk
/512) * odata
* ndata
/ a
;
953 sra
= sysfs_read(fd
, 0,
954 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
958 fprintf(stderr
, Name
": %s: Cannot get array details from sysfs\n",
964 if (ndata
== odata
) {
965 /* Make 'blocks' bigger for better throughput, but
966 * not so big that we reject it below.
967 * Try for 16 megabytes
969 while (blocks
* 32 < sra
->component_size
&&
973 fprintf(stderr
, Name
": Need to backup %luK of critical "
974 "section..\n", blocks
/2);
976 if (blocks
>= sra
->component_size
/2) {
977 fprintf(stderr
, Name
": %s: Something wrong - reshape aborted\n",
982 nrdisks
= array
.raid_disks
+ sra
->array
.spare_disks
;
983 /* Now we need to open all these devices so we can read/write.
985 fdlist
= malloc((1+nrdisks
) * sizeof(int));
986 offsets
= malloc((1+nrdisks
) * sizeof(offsets
[0]));
987 if (!fdlist
|| !offsets
) {
988 fprintf(stderr
, Name
": malloc failed: grow aborted\n");
992 for (d
=0; d
<= nrdisks
; d
++)
994 d
= array
.raid_disks
;
995 for (sd
= sra
->devs
; sd
; sd
=sd
->next
) {
996 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
998 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
999 char *dn
= map_dev(sd
->disk
.major
,
1001 fdlist
[sd
->disk
.raid_disk
]
1002 = dev_open(dn
, O_RDONLY
);
1003 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
1004 if (fdlist
[sd
->disk
.raid_disk
] < 0) {
1005 fprintf(stderr
, Name
": %s: cannot open component %s\n",
1006 devname
, dn
?dn
:"-unknown-");
1010 } else if (backup_file
== NULL
) {
1012 char *dn
= map_dev(sd
->disk
.major
,
1014 fdlist
[d
] = dev_open(dn
, O_RDWR
);
1015 offsets
[d
] = (sd
->data_offset
+ sra
->component_size
- blocks
- 8)*512;
1017 fprintf(stderr
, Name
": %s: cannot open component %s\n",
1018 devname
, dn
?dn
:"-unknown");
1025 if (backup_file
== NULL
) {
1026 if (ndata
<= odata
) {
1027 fprintf(stderr
, Name
": %s: Cannot grow - need backup-file\n",
1031 } else if (sra
->array
.spare_disks
== 0) {
1032 fprintf(stderr
, Name
": %s: Cannot grow - need a spare or "
1033 "backup-file to backup critical section\n",
1038 if (d
== array
.raid_disks
) {
1039 fprintf(stderr
, Name
": %s: No spare device for backup\n",
1045 /* need to check backup file is large enough */
1047 fdlist
[d
] = open(backup_file
, O_RDWR
|O_CREAT
|O_EXCL
,
1049 offsets
[d
] = 8 * 512;
1050 if (fdlist
[d
] < 0) {
1051 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1052 devname
, backup_file
, strerror(errno
));
1056 memset(buf
, 0, 512);
1057 for (i
=0; i
< (signed)blocks
+ 8 ; i
++) {
1058 if (write(fdlist
[d
], buf
, 512) != 512) {
1059 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1060 devname
, backup_file
, strerror(errno
));
1065 if (fsync(fdlist
[d
]) != 0) {
1066 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1067 devname
, backup_file
, strerror(errno
));
1074 /* lastly, check that the internal stripe cache is
1075 * large enough, or it won't work.
1078 cache
= (nchunk
< ochunk
) ? ochunk
: nchunk
;
1079 cache
= cache
* 4 / 4096;
1080 if (cache
< blocks
/ 8 / odisks
+ 16)
1081 /* Make it big enough to hold 'blocks' */
1082 cache
= blocks
/ 8 / odisks
+ 16;
1083 if (sra
->cache_size
< cache
)
1084 sysfs_set_num(sra
, NULL
, "stripe_cache_size",
1086 /* Right, everything seems fine. Let's kick things off.
1087 * If only changing raid_disks, use ioctl, else use
1090 if (ochunk
== nchunk
&& olayout
== nlayout
) {
1091 array
.raid_disks
= ndisks
;
1092 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
1095 fprintf(stderr
, Name
": Cannot set device shape for %s: %s\n",
1096 devname
, strerror(errno
));
1097 if (ndisks
< odisks
&&
1098 get_linux_version() < 2006030)
1099 fprintf(stderr
, Name
": linux 2.6.30 or later required\n");
1101 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1102 fprintf(stderr
, " Bitmap must be removed before shape can be changed\n");
1107 /* set them all just in case some old 'new_*' value
1108 * persists from some earlier problem
1110 int err
= err
; /* only used if rv==1, and always set if
1111 * rv==1, so initialisation not needed,
1112 * despite gcc warning
1114 if (sysfs_set_num(sra
, NULL
, "chunk_size", nchunk
) < 0)
1115 rv
= 1, err
= errno
;
1116 if (!rv
&& sysfs_set_num(sra
, NULL
, "layout", nlayout
) < 0)
1117 rv
= 1, err
= errno
;
1118 if (!rv
&& sysfs_set_num(sra
, NULL
, "raid_disks", ndisks
) < 0)
1119 rv
= 1, err
= errno
;
1121 fprintf(stderr
, Name
": Cannot set device shape for %s\n",
1123 if (get_linux_version() < 2006030)
1124 fprintf(stderr
, Name
": linux 2.6.30 or later required\n");
1126 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1127 fprintf(stderr
, " Bitmap must be removed before shape can be changed\n");
1132 if (ndisks
== 2 && odisks
== 2) {
1133 /* No reshape is needed in this trivial case */
1138 /* set up the backup-super-block. This requires the
1139 * uuid from the array.
1141 /* Find a superblock */
1142 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1146 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1148 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
1149 devfd
= dev_open(dn
, O_RDONLY
);
1152 ok
= st
->ss
->load_super(st
, devfd
, NULL
);
1158 fprintf(stderr
, Name
": %s: Cannot find a superblock\n",
1164 memset(&bsb
, 0, 512);
1165 memcpy(bsb
.magic
, "md_backup_data-1", 16);
1166 st
->ss
->uuid_from_super(st
, (int*)&bsb
.set_uuid
);
1167 bsb
.mtime
= __cpu_to_le64(time(0));
1168 bsb
.devstart2
= blocks
;
1169 stripes
= blocks
/ (ochunk
/512) / odata
;
1170 /* Now we just need to kick off the reshape and watch, while
1171 * handling backups of the data...
1172 * This is all done by a forked background process.
1177 if (check_env("MDADM_GROW_VERIFY"))
1178 fd
= open(devname
, O_RDONLY
| O_DIRECT
);
1181 mlockall(MCL_FUTURE
);
1184 done
= child_grow(fd
, sra
, stripes
,
1186 odisks
, ochunk
, array
.level
, olayout
, odata
,
1187 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1188 else if (odata
> ndata
)
1189 done
= child_shrink(fd
, sra
, stripes
,
1191 odisks
, ochunk
, array
.level
, olayout
, odata
,
1192 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1194 done
= child_same_size(fd
, sra
, stripes
,
1197 odisks
, ochunk
, array
.level
, olayout
, odata
,
1198 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1199 if (backup_file
&& done
)
1200 unlink(backup_file
);
1201 if (level
!= UnSet
&& level
!= array
.level
) {
1202 /* We need to wait for the reshape to finish
1203 * (which will have happened unless odata < ndata)
1204 * and then set the level
1207 c
= map_num(pers
, level
);
1209 exit(0);/* not possible */
1213 err
= sysfs_set_str(sra
, NULL
, "level", c
);
1215 fprintf(stderr
, Name
": %s: could not set level to %s\n",
1220 fprintf(stderr
, Name
": Cannot run child to monitor reshape: %s\n",
1225 /* The child will take care of unfreezing the array */
1234 if (rv
&& orig_level
!= UnSet
&& sra
) {
1235 c
= map_num(pers
, orig_level
);
1236 if (c
&& sysfs_set_str(sra
, NULL
, "level", c
) == 0)
1237 fprintf(stderr
, Name
": aborting level change\n");
1240 unfreeze_array(sra
, frozen
);
1245 * We run a child process in the background which performs the following
1247 * - wait for resync to reach a certain point
1248 * - suspend io to the following section
1249 * - backup that section
1250 * - allow resync to proceed further
1252 * - discard the backup.
1254 * When are combined in slightly different ways in the three cases.
1256 * - suspend/backup/allow/wait/resume/discard
1258 * - allow/wait/suspend/backup/allow/wait/resume/discard
1260 * - wait/resume/discard/suspend/backup/allow
1262 * suspend/backup/allow always come together
1263 * wait/resume/discard do too.
1264 * For the same-size case we have two backups to improve flow.
1268 /* FIXME return status is never checked */
1269 int grow_backup(struct mdinfo
*sra
,
1270 unsigned long long offset
, /* per device */
1271 unsigned long stripes
, /* per device */
1272 int *sources
, unsigned long long *offsets
,
1273 int disks
, int chunk
, int level
, int layout
,
1274 int dests
, int *destfd
, unsigned long long *destoffsets
,
1275 int part
, int *degraded
,
1278 /* Backup 'blocks' sectors at 'offset' on each device of the array,
1279 * to storage 'destfd' (offset 'destoffsets'), after first
1280 * suspending IO. Then allow resync to continue
1281 * over the suspended section.
1282 * Use part 'part' of the backup-super-block.
1287 unsigned long long ll
;
1289 //printf("offset %llu\n", offset);
1294 sysfs_set_num(sra
, NULL
, "suspend_hi", (offset
+ stripes
* (chunk
/512)) * odata
);
1295 /* Check that array hasn't become degraded, else we might backup the wrong data */
1296 if (sysfs_get_ll(sra
, NULL
, "degraded", &ll
) < 0)
1297 return -1; /* FIXME this error is ignored */
1298 new_degraded
= (int)ll
;
1299 if (new_degraded
!= *degraded
) {
1300 /* check each device to ensure it is still working */
1302 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1303 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1305 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
1307 if (sysfs_get_str(sra
, sd
, "state", sbuf
, 20) < 0 ||
1308 strstr(sbuf
, "faulty") ||
1309 strstr(sbuf
, "in_sync") == NULL
) {
1310 /* this device is dead */
1311 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
1312 if (sd
->disk
.raid_disk
>= 0 &&
1313 sources
[sd
->disk
.raid_disk
] >= 0) {
1314 close(sources
[sd
->disk
.raid_disk
]);
1315 sources
[sd
->disk
.raid_disk
] = -1;
1320 *degraded
= new_degraded
;
1323 bsb
.arraystart2
= __cpu_to_le64(offset
* odata
);
1324 bsb
.length2
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
1326 bsb
.arraystart
= __cpu_to_le64(offset
* odata
);
1327 bsb
.length
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
1330 bsb
.magic
[15] = '2';
1331 for (i
= 0; i
< dests
; i
++)
1333 lseek64(destfd
[i
], destoffsets
[i
] + __le64_to_cpu(bsb
.devstart2
)*512, 0);
1335 lseek64(destfd
[i
], destoffsets
[i
], 0);
1337 rv
= save_stripes(sources
, offsets
,
1338 disks
, chunk
, level
, layout
,
1340 offset
*512*odata
, stripes
* chunk
* odata
,
1345 bsb
.mtime
= __cpu_to_le64(time(0));
1346 for (i
= 0; i
< dests
; i
++) {
1347 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
1349 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
1350 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
1351 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
1352 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
1355 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
] - 4096, 0)
1356 != destoffsets
[i
] - 4096)
1358 if (write(destfd
[i
], &bsb
, 512) != 512)
1360 if (destoffsets
[i
] > 4096) {
1361 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]+stripes
*chunk
*odata
, 0) !=
1362 destoffsets
[i
]+stripes
*chunk
*odata
)
1364 if (write(destfd
[i
], &bsb
, 512) != 512)
1374 /* in 2.6.30, the value reported by sync_completed can be
1375 * less that it should be by one stripe.
1376 * This only happens when reshape hits sync_max and pauses.
1377 * So allow wait_backup to either extent sync_max further
1378 * than strictly necessary, or return before the
1379 * sync has got quite as far as we would really like.
1380 * This is what 'blocks2' is for.
1381 * The various caller give appropriate values so that
1384 /* FIXME return value is often ignored */
1385 int wait_backup(struct mdinfo
*sra
,
1386 unsigned long long offset
, /* per device */
1387 unsigned long long blocks
, /* per device */
1388 unsigned long long blocks2
, /* per device - hack */
1389 int dests
, int *destfd
, unsigned long long *destoffsets
,
1392 /* Wait for resync to pass the section that was backed up
1393 * then erase the backup and allow IO
1395 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
1396 unsigned long long completed
;
1402 sysfs_set_num(sra
, NULL
, "sync_max", offset
+ blocks
+ blocks2
);
1404 sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
1410 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
1411 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
1415 if (sysfs_get_str(sra
, NULL
, "sync_action",
1417 strncmp(action
, "reshape", 7) != 0)
1419 } while (completed
< offset
+ blocks
);
1423 bsb
.arraystart2
= __cpu_to_le64(0);
1424 bsb
.length2
= __cpu_to_le64(0);
1426 bsb
.arraystart
= __cpu_to_le64(0);
1427 bsb
.length
= __cpu_to_le64(0);
1429 bsb
.mtime
= __cpu_to_le64(time(0));
1431 for (i
= 0; i
< dests
; i
++) {
1432 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
1433 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
1434 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
1435 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
1436 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
1437 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]-4096, 0) !=
1438 destoffsets
[i
]-4096)
1441 write(destfd
[i
], &bsb
, 512) != 512)
1448 static void fail(char *msg
)
1451 rv
= (write(2, msg
, strlen(msg
)) != (int)strlen(msg
));
1452 rv
|= (write(2, "\n", 1) != 1);
1456 static char *abuf
, *bbuf
;
1457 static unsigned long long abuflen
;
1458 static void validate(int afd
, int bfd
, unsigned long long offset
)
1460 /* check that the data in the backup against the array.
1461 * This is only used for regression testing and should not
1462 * be used while the array is active
1466 lseek64(bfd
, offset
- 4096, 0);
1467 if (read(bfd
, &bsb2
, 512) != 512)
1468 fail("cannot read bsb");
1469 if (bsb2
.sb_csum
!= bsb_csum((char*)&bsb2
,
1470 ((char*)&bsb2
.sb_csum
)-((char*)&bsb2
)))
1471 fail("first csum bad");
1472 if (memcmp(bsb2
.magic
, "md_backup_data", 14) != 0)
1473 fail("magic is bad");
1474 if (memcmp(bsb2
.magic
, "md_backup_data-2", 16) == 0 &&
1475 bsb2
.sb_csum2
!= bsb_csum((char*)&bsb2
,
1476 ((char*)&bsb2
.sb_csum2
)-((char*)&bsb2
)))
1477 fail("second csum bad");
1479 if (__le64_to_cpu(bsb2
.devstart
)*512 != offset
)
1480 fail("devstart is wrong");
1483 unsigned long long len
= __le64_to_cpu(bsb2
.length
)*512;
1485 if (abuflen
< len
) {
1489 if (posix_memalign((void**)&abuf
, 4096, abuflen
) ||
1490 posix_memalign((void**)&bbuf
, 4096, abuflen
)) {
1492 /* just stop validating on mem-alloc failure */
1497 lseek64(bfd
, offset
, 0);
1498 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
) {
1499 //printf("len %llu\n", len);
1500 fail("read first backup failed");
1502 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart
)*512, 0);
1503 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
1504 fail("read first from array failed");
1505 if (memcmp(bbuf
, abuf
, len
) != 0) {
1508 printf("offset=%llu len=%llu\n",
1509 (unsigned long long)__le64_to_cpu(bsb2
.arraystart
)*512, len
);
1510 for (i
=0; i
<len
; i
++)
1511 if (bbuf
[i
] != abuf
[i
]) {
1512 printf("first diff byte %d\n", i
);
1516 fail("data1 compare failed");
1520 unsigned long long len
= __le64_to_cpu(bsb2
.length2
)*512;
1522 if (abuflen
< len
) {
1526 abuf
= malloc(abuflen
);
1527 bbuf
= malloc(abuflen
);
1530 lseek64(bfd
, offset
+__le64_to_cpu(bsb2
.devstart2
)*512, 0);
1531 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
)
1532 fail("read second backup failed");
1533 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart2
)*512, 0);
1534 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
1535 fail("read second from array failed");
1536 if (memcmp(bbuf
, abuf
, len
) != 0)
1537 fail("data2 compare failed");
1541 static int child_grow(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1542 int *fds
, unsigned long long *offsets
,
1543 int disks
, int chunk
, int level
, int layout
, int data
,
1544 int dests
, int *destfd
, unsigned long long *destoffsets
)
1549 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
1550 /* Don't start the 'reshape' */
1552 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1553 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1554 grow_backup(sra
, 0, stripes
,
1555 fds
, offsets
, disks
, chunk
, level
, layout
,
1556 dests
, destfd
, destoffsets
,
1558 validate(afd
, destfd
[0], destoffsets
[0]);
1559 wait_backup(sra
, 0, stripes
* (chunk
/ 512), stripes
* (chunk
/ 512),
1560 dests
, destfd
, destoffsets
,
1562 sysfs_set_num(sra
, NULL
, "suspend_lo", (stripes
* (chunk
/512)) * data
);
1564 /* FIXME this should probably be numeric */
1565 sysfs_set_str(sra
, NULL
, "sync_max", "max");
1569 static int child_shrink(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1570 int *fds
, unsigned long long *offsets
,
1571 int disks
, int chunk
, int level
, int layout
, int data
,
1572 int dests
, int *destfd
, unsigned long long *destoffsets
)
1575 unsigned long long start
;
1579 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
1581 start
= sra
->component_size
- stripes
* (chunk
/512);
1582 sysfs_set_num(sra
, NULL
, "sync_max", start
);
1583 sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
1584 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1585 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1586 rv
= wait_backup(sra
, 0, start
- stripes
* (chunk
/512), stripes
* (chunk
/512),
1587 dests
, destfd
, destoffsets
, 0);
1590 grow_backup(sra
, 0, stripes
,
1592 disks
, chunk
, level
, layout
,
1593 dests
, destfd
, destoffsets
,
1595 validate(afd
, destfd
[0], destoffsets
[0]);
1596 wait_backup(sra
, start
, stripes
*(chunk
/512), 0,
1597 dests
, destfd
, destoffsets
, 0);
1598 sysfs_set_num(sra
, NULL
, "suspend_lo", (stripes
* (chunk
/512)) * data
);
1600 /* FIXME this should probably be numeric */
1601 sysfs_set_str(sra
, NULL
, "sync_max", "max");
1605 static int child_same_size(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1606 int *fds
, unsigned long long *offsets
,
1607 unsigned long long start
,
1608 int disks
, int chunk
, int level
, int layout
, int data
,
1609 int dests
, int *destfd
, unsigned long long *destoffsets
)
1611 unsigned long long size
;
1612 unsigned long tailstripes
= stripes
;
1615 unsigned long long speed
;
1619 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
1622 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1623 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1625 sysfs_get_ll(sra
, NULL
, "sync_speed_min", &speed
);
1626 sysfs_set_num(sra
, NULL
, "sync_speed_min", 200000);
1628 grow_backup(sra
, start
, stripes
,
1630 disks
, chunk
, level
, layout
,
1631 dests
, destfd
, destoffsets
,
1633 grow_backup(sra
, (start
+ stripes
) * (chunk
/512), stripes
,
1635 disks
, chunk
, level
, layout
,
1636 dests
, destfd
, destoffsets
,
1638 validate(afd
, destfd
[0], destoffsets
[0]);
1640 start
+= stripes
* 2; /* where to read next */
1641 size
= sra
->component_size
/ (chunk
/512);
1642 while (start
< size
) {
1643 if (wait_backup(sra
, (start
-stripes
*2)*(chunk
/512),
1644 stripes
*(chunk
/512), 0,
1645 dests
, destfd
, destoffsets
,
1648 sysfs_set_num(sra
, NULL
, "suspend_lo", start
*(chunk
/512) * data
);
1649 if (start
+ stripes
> size
)
1650 tailstripes
= (size
- start
);
1652 grow_backup(sra
, start
*(chunk
/512), tailstripes
,
1654 disks
, chunk
, level
, layout
,
1655 dests
, destfd
, destoffsets
,
1656 part
, °raded
, buf
);
1659 validate(afd
, destfd
[0], destoffsets
[0]);
1661 if (wait_backup(sra
, (start
-stripes
*2) * (chunk
/512), stripes
* (chunk
/512), 0,
1662 dests
, destfd
, destoffsets
,
1665 sysfs_set_num(sra
, NULL
, "suspend_lo", ((start
-stripes
)*(chunk
/512)) * data
);
1666 wait_backup(sra
, (start
-stripes
) * (chunk
/512), tailstripes
* (chunk
/512), 0,
1667 dests
, destfd
, destoffsets
,
1669 sysfs_set_num(sra
, NULL
, "suspend_lo", (size
*(chunk
/512)) * data
);
1670 sysfs_set_num(sra
, NULL
, "sync_speed_min", speed
);
1676 * If any spare contains md_back_data-1 which is recent wrt mtime,
1677 * write that data into the array and update the super blocks with
1678 * the new reshape_progress
1680 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
, int cnt
,
1681 char *backup_file
, int verbose
)
1685 unsigned long long *offsets
;
1686 unsigned long long nstripe
, ostripe
;
1689 if (info
->new_level
!= info
->array
.level
)
1690 return 1; /* Cannot handle level changes (they are instantaneous) */
1692 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
1693 if (info
->array
.level
== 6) odata
--; /* number of data disks */
1694 ndata
= info
->array
.raid_disks
- 1;
1695 if (info
->new_level
== 6) ndata
--;
1697 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
1699 if (info
->delta_disks
<= 0)
1700 /* Didn't grow, so the backup file must have
1704 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
1705 struct mdinfo dinfo
;
1708 char *devname
, namebuf
[20];
1710 /* This was a spare and may have some saved data on it.
1711 * Load the superblock, find and load the
1712 * backup_super_block.
1713 * If either fail, go on to next device.
1714 * If the backup contains no new info, just return
1715 * else restore data and update all superblocks
1717 if (i
== old_disks
-1) {
1718 fd
= open(backup_file
, O_RDONLY
);
1720 fprintf(stderr
, Name
": backup file %s inaccessible: %s\n",
1721 backup_file
, strerror(errno
));
1724 devname
= backup_file
;
1729 if (st
->ss
->load_super(st
, fd
, NULL
))
1732 st
->ss
->getinfo_super(st
, &dinfo
);
1733 st
->ss
->free_super(st
);
1736 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
1738 fprintf(stderr
, Name
": Cannot seek on device %d\n", i
);
1739 continue; /* Cannot seek */
1741 sprintf(namebuf
, "device-%d", i
);
1744 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
)) {
1746 fprintf(stderr
, Name
": Cannot read from %s\n", devname
);
1747 continue; /* Cannot read */
1749 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0 &&
1750 memcmp(bsb
.magic
, "md_backup_data-2", 16) != 0) {
1752 fprintf(stderr
, Name
": No backup metadata on %s\n", devname
);
1755 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
))) {
1757 fprintf(stderr
, Name
": Bad backup-metadata checksum on %s\n", devname
);
1758 continue; /* bad checksum */
1760 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0 &&
1761 bsb
.sb_csum2
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum2
)-((char*)&bsb
))) {
1763 fprintf(stderr
, Name
": Bad backup-metadata checksum2 on %s\n", devname
);
1764 continue; /* Bad second checksum */
1766 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0) {
1768 fprintf(stderr
, Name
": Wrong uuid on backup-metadata on %s\n", devname
);
1769 continue; /* Wrong uuid */
1772 /* array utime and backup-mtime should be updated at much the same time, but it seems that
1773 * sometimes they aren't... So allow considerable flexability in matching, and allow
1774 * this test to be overridden by an environment variable.
1776 if (info
->array
.utime
> (int)__le64_to_cpu(bsb
.mtime
) + 2*60*60 ||
1777 info
->array
.utime
< (int)__le64_to_cpu(bsb
.mtime
) - 10*60) {
1778 if (check_env("MDADM_GROW_ALLOW_OLD")) {
1779 fprintf(stderr
, Name
": accepting backup with timestamp %lu "
1780 "for array with timestamp %lu\n",
1781 (unsigned long)__le64_to_cpu(bsb
.mtime
),
1782 (unsigned long)info
->array
.utime
);
1785 fprintf(stderr
, Name
": too-old timestamp on "
1786 "backup-metadata on %s\n", devname
);
1787 continue; /* time stamp is too bad */
1791 if (bsb
.magic
[15] == '1') {
1792 if (info
->delta_disks
>= 0) {
1793 /* reshape_progress is increasing */
1794 if (__le64_to_cpu(bsb
.arraystart
) + __le64_to_cpu(bsb
.length
) <
1795 info
->reshape_progress
) {
1798 fprintf(stderr
, Name
": backup-metadata found on %s but is not needed\n", devname
);
1799 continue; /* No new data here */
1802 /* reshape_progress is decreasing */
1803 if (__le64_to_cpu(bsb
.arraystart
) >=
1804 info
->reshape_progress
)
1805 goto nonew
; /* No new data here */
1808 if (info
->delta_disks
>= 0) {
1809 /* reshape_progress is increasing */
1810 if (__le64_to_cpu(bsb
.arraystart
) + __le64_to_cpu(bsb
.length
) <
1811 info
->reshape_progress
&&
1812 __le64_to_cpu(bsb
.arraystart2
) + __le64_to_cpu(bsb
.length2
) <
1813 info
->reshape_progress
)
1814 goto nonew
; /* No new data here */
1816 /* reshape_progress is decreasing */
1817 if (__le64_to_cpu(bsb
.arraystart
) >=
1818 info
->reshape_progress
&&
1819 __le64_to_cpu(bsb
.arraystart2
) >=
1820 info
->reshape_progress
)
1821 goto nonew
; /* No new data here */
1824 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0) {
1827 fprintf(stderr
, Name
": Failed to verify secondary backup-metadata block on %s\n",
1829 continue; /* Cannot seek */
1831 /* There should be a duplicate backup superblock 4k before here */
1832 if (lseek64(fd
, -4096, 1) < 0 ||
1833 read(fd
, &bsb2
, sizeof(bsb2
)) != sizeof(bsb2
))
1834 goto second_fail
; /* Cannot find leading superblock */
1835 if (bsb
.magic
[15] == '1')
1836 bsbsize
= offsetof(struct mdp_backup_super
, pad1
);
1838 bsbsize
= offsetof(struct mdp_backup_super
, pad
);
1839 if (memcmp(&bsb2
, &bsb
, bsbsize
) != 0)
1840 goto second_fail
; /* Cannot find leading superblock */
1842 /* Now need the data offsets for all devices. */
1843 offsets
= malloc(sizeof(*offsets
)*info
->array
.raid_disks
);
1844 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
1847 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
1848 /* FIXME should be this be an error */
1850 st
->ss
->getinfo_super(st
, &dinfo
);
1851 st
->ss
->free_super(st
);
1852 offsets
[j
] = dinfo
.data_offset
* 512;
1854 printf(Name
": restoring critical section\n");
1856 if (restore_stripes(fdlist
, offsets
,
1857 info
->array
.raid_disks
,
1861 fd
, __le64_to_cpu(bsb
.devstart
)*512,
1862 __le64_to_cpu(bsb
.arraystart
)*512,
1863 __le64_to_cpu(bsb
.length
)*512)) {
1864 /* didn't succeed, so giveup */
1866 fprintf(stderr
, Name
": Error restoring backup from %s\n",
1871 if (bsb
.magic
[15] == '2' &&
1872 restore_stripes(fdlist
, offsets
,
1873 info
->array
.raid_disks
,
1877 fd
, __le64_to_cpu(bsb
.devstart
)*512 +
1878 __le64_to_cpu(bsb
.devstart2
)*512,
1879 __le64_to_cpu(bsb
.arraystart2
)*512,
1880 __le64_to_cpu(bsb
.length2
)*512)) {
1881 /* didn't succeed, so giveup */
1883 fprintf(stderr
, Name
": Error restoring second backup from %s\n",
1889 /* Ok, so the data is restored. Let's update those superblocks. */
1891 if (info
->delta_disks
>= 0) {
1892 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
) +
1893 __le64_to_cpu(bsb
.length
);
1894 if (bsb
.magic
[15] == '2') {
1895 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
) +
1896 __le64_to_cpu(bsb
.length2
);
1897 if (p2
> info
->reshape_progress
)
1898 info
->reshape_progress
= p2
;
1901 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
);
1902 if (bsb
.magic
[15] == '2') {
1903 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
);
1904 if (p2
< info
->reshape_progress
)
1905 info
->reshape_progress
= p2
;
1908 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
1909 if (fdlist
[j
] < 0) continue;
1910 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
1912 st
->ss
->getinfo_super(st
, &dinfo
);
1913 dinfo
.reshape_progress
= info
->reshape_progress
;
1914 st
->ss
->update_super(st
, &dinfo
,
1915 "_reshape_progress",
1917 st
->ss
->store_super(st
, fdlist
[j
]);
1918 st
->ss
->free_super(st
);
1922 /* Didn't find any backup data, try to see if any
1925 if (info
->delta_disks
< 0) {
1926 /* When shrinking, the critical section is at the end.
1927 * So see if we are before the critical section.
1929 unsigned long long first_block
;
1930 nstripe
= ostripe
= 0;
1932 while (ostripe
>= nstripe
) {
1933 ostripe
+= info
->array
.chunk_size
/ 512;
1934 first_block
= ostripe
* odata
;
1935 nstripe
= first_block
/ ndata
/ (info
->new_chunk
/512) *
1936 (info
->new_chunk
/512);
1939 if (info
->reshape_progress
>= first_block
)
1942 if (info
->delta_disks
> 0) {
1943 /* See if we are beyond the critical section. */
1944 unsigned long long last_block
;
1945 nstripe
= ostripe
= 0;
1947 while (nstripe
>= ostripe
) {
1948 nstripe
+= info
->new_chunk
/ 512;
1949 last_block
= nstripe
* ndata
;
1950 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
1951 (info
->array
.chunk_size
/512);
1954 if (info
->reshape_progress
>= last_block
)
1957 /* needed to recover critical section! */
1959 fprintf(stderr
, Name
": Failed to find backup of critical section\n");
1963 int Grow_continue(int mdfd
, struct supertype
*st
, struct mdinfo
*info
,
1966 /* Array is assembled and ready to be started, but
1967 * monitoring is probably required.
1970 * - set upper bound for resync
1971 * - initialise the 'suspend' boundaries
1972 * - switch to read-write
1973 * - fork and continue monitoring
1977 unsigned long long backup_offsets
[1];
1978 int odisks
, ndisks
, ochunk
, nchunk
,odata
,ndata
;
1979 unsigned long a
,b
,blocks
,stripes
;
1982 unsigned long long *offsets
;
1984 struct mdinfo
*sra
, *sd
;
1986 unsigned long cache
;
1989 err
= sysfs_set_str(info
, NULL
, "array_state", "readonly");
1993 /* make sure reshape doesn't progress until we are ready */
1994 sysfs_set_str(info
, NULL
, "sync_max", "0");
1995 sysfs_set_str(info
, NULL
, "array_state", "active"); /* FIXME or clean */
1997 sra
= sysfs_read(-1, devname2devnum(info
->sys_name
),
1998 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
2003 /* ndisks is not growing, so raid_disks is old and +delta is new */
2004 odisks
= info
->array
.raid_disks
;
2005 ndisks
= odisks
+ info
->delta_disks
;
2008 if (info
->array
.level
== 6) {
2012 ochunk
= info
->array
.chunk_size
;
2013 nchunk
= info
->new_chunk
;
2015 a
= (ochunk
/512) * odata
;
2016 b
= (nchunk
/512) * ndata
;
2024 /* LCM == product / GCD */
2025 blocks
= (ochunk
/512) * (nchunk
/512) * odata
* ndata
/ a
;
2028 while (blocks
* 32 < sra
->component_size
&&
2031 stripes
= blocks
/ (info
->array
.chunk_size
/512) / odata
;
2033 /* check that the internal stripe cache is
2034 * large enough, or it won't work.
2036 cache
= (nchunk
< ochunk
) ? ochunk
: nchunk
;
2037 cache
= cache
* 4 / 4096;
2038 if (cache
< blocks
/ 8 / odisks
+ 16)
2039 /* Make it big enough to hold 'blocks' */
2040 cache
= blocks
/ 8 / odisks
+ 16;
2041 if (sra
->cache_size
< cache
)
2042 sysfs_set_num(sra
, NULL
, "stripe_cache_size",
2045 memset(&bsb
, 0, 512);
2046 memcpy(bsb
.magic
, "md_backup_data-1", 16);
2047 memcpy(&bsb
.set_uuid
, info
->uuid
, 16);
2048 bsb
.mtime
= __cpu_to_le64(time(0));
2049 bsb
.devstart2
= blocks
;
2051 backup_fd
= open(backup_file
, O_RDWR
|O_CREAT
, S_IRUSR
| S_IWUSR
);
2052 backup_list
[0] = backup_fd
;
2053 backup_offsets
[0] = 8 * 512;
2054 fds
= malloc(odisks
* sizeof(fds
[0]));
2055 offsets
= malloc(odisks
* sizeof(offsets
[0]));
2056 for (d
=0; d
<odisks
; d
++)
2059 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2060 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2062 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
2063 char *dn
= map_dev(sd
->disk
.major
,
2065 fds
[sd
->disk
.raid_disk
]
2066 = dev_open(dn
, O_RDONLY
);
2067 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
2068 if (fds
[sd
->disk
.raid_disk
] < 0) {
2069 fprintf(stderr
, Name
": %s: cannot open component %s\n",
2070 info
->sys_name
, dn
?dn
:"-unknown-");
2081 mlockall(MCL_FUTURE
);
2082 if (info
->delta_disks
< 0)
2083 done
= child_shrink(-1, info
, stripes
,
2085 info
->array
.raid_disks
,
2086 info
->array
.chunk_size
,
2087 info
->array
.level
, info
->array
.layout
,
2089 1, backup_list
, backup_offsets
);
2090 else if (info
->delta_disks
== 0) {
2091 /* The 'start' is a per-device stripe number.
2092 * reshape_progress is a per-array sector number.
2093 * So divide by ndata * chunk_size
2095 unsigned long long start
= info
->reshape_progress
/ ndata
;
2096 start
/= (info
->array
.chunk_size
/512);
2097 done
= child_same_size(-1, info
, stripes
,
2100 info
->array
.raid_disks
,
2101 info
->array
.chunk_size
,
2102 info
->array
.level
, info
->array
.layout
,
2104 1, backup_list
, backup_offsets
);
2106 if (backup_file
&& done
)
2107 unlink(backup_file
);
2108 /* FIXME should I intuit a level change */
2111 fprintf(stderr
, Name
": Cannot run child to continue monitoring reshape: %s\n",