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 int 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
;
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
);
549 frozen
= freeze_array(sra
);
551 fprintf(stderr
, Name
": %s is performing resync/recovery and cannot"
552 " be reshaped\n", devname
);
556 /* ========= set size =============== */
557 if (size
>= 0 && (size
== 0 || size
!= array
.size
)) {
559 if (array
.size
!= size
) {
560 /* got truncated to 32bit, write to
561 * component_size instead
564 rv
= sysfs_set_num(sra
, NULL
,
565 "component_size", size
);
569 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
572 fprintf(stderr
, Name
": Cannot set device size for %s: %s\n",
573 devname
, strerror(err
));
575 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
576 fprintf(stderr
, " Bitmap must be removed before size can be changed\n");
580 ioctl(fd
, GET_ARRAY_INFO
, &array
);
581 size
= get_component_size(fd
)/2;
585 fprintf(stderr
, Name
": component size of %s has been set to %lluK\n",
589 size
= get_component_size(fd
)/2;
594 /* ======= set level =========== */
595 if (level
!= UnSet
&& level
!= array
.level
) {
596 /* Trying to change the level.
597 * We might need to change layout first and schedule a
598 * level change for later.
599 * Level changes that can happen immediately are:
600 * 0->4,5,6 1->5 4->5,6 5->1,6
601 * Level changes that need a layout change first are:
602 * 6->5,4,0 : need a -6 layout, or parity-last
603 * 5->4,0 : need parity-last
605 if ((array
.level
== 6 || array
.level
== 5) &&
606 (level
== 5 || level
== 4 || level
== 0)) {
607 /* Don't change level yet, but choose intermediate
611 if (layout_str
== NULL
)
612 switch (array
.layout
) {
613 case ALGORITHM_LEFT_ASYMMETRIC
:
614 case ALGORITHM_LEFT_ASYMMETRIC_6
:
615 case ALGORITHM_ROTATING_N_RESTART
:
616 layout_str
= "left-asymmetric-6";
618 case ALGORITHM_LEFT_SYMMETRIC
:
619 case ALGORITHM_LEFT_SYMMETRIC_6
:
620 case ALGORITHM_ROTATING_N_CONTINUE
:
621 layout_str
= "left-symmetric-6";
623 case ALGORITHM_RIGHT_ASYMMETRIC
:
624 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
625 case ALGORITHM_ROTATING_ZERO_RESTART
:
626 layout_str
= "right-asymmetric-6";
628 case ALGORITHM_RIGHT_SYMMETRIC
:
629 case ALGORITHM_RIGHT_SYMMETRIC_6
:
630 layout_str
= "right-symmetric-6";
632 case ALGORITHM_PARITY_0
:
633 case ALGORITHM_PARITY_0_6
:
634 layout_str
= "parity-first-6";
636 case ALGORITHM_PARITY_N
:
637 layout_str
= "parity-last";
640 fprintf(stderr
, Name
": %s: cannot"
641 "convert layout to RAID5 equivalent\n",
647 int l
= map_name(r5layout
, layout_str
);
649 fprintf(stderr
, Name
": %s: layout '%s' not recognised\n",
650 devname
, layout_str
);
654 if (l
!= ALGORITHM_PARITY_N
) {
655 /* need the -6 version */
656 char *ls
= map_num(r5layout
, l
);
657 strcat(strcpy(alt_layout
, ls
),
659 layout_str
= alt_layout
;
663 /* The final raid6->raid5 conversion
664 * will reduce the number of disks,
665 * so now we need to aim higher
669 layout_str
= "parity-last";
671 c
= map_num(pers
, level
);
673 rv
= 1;/* not possible */
676 err
= sysfs_set_str(sra
, NULL
, "level", c
);
679 fprintf(stderr
, Name
": %s: could not set level to %s\n",
682 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
683 fprintf(stderr
, " Bitmap must be removed before level can be changed\n");
688 orig_level
= orig
.level
;
689 ioctl(fd
, GET_ARRAY_INFO
, &array
);
690 if (layout_str
== NULL
&&
691 orig
.level
== 5 && level
== 6 &&
692 array
.layout
!= orig
.layout
)
693 layout_str
= map_num(r5layout
, orig
.layout
);
695 fprintf(stderr
, Name
" level of %s changed to %s\n",
701 /* ========= set shape (chunk_size / layout / ndisks) ============== */
702 /* Check if layout change is a no-op */
703 if (layout_str
) switch(array
.level
) {
705 if (array
.layout
== map_name(r5layout
, layout_str
))
709 if (layout_str
== NULL
&&
710 ((chunksize
&& chunksize
* 1024 != array
.chunk_size
) ||
711 (raid_disks
&& raid_disks
!= array
.raid_disks
)) &&
712 array
.layout
>= 16) {
714 ": %s has a non-standard layout. If you wish to preserve this\n"
715 " during the reshape, please specify --layout=preserve\n"
716 " If you want to change it, specify a layout or use --layout=normalise\n",
721 if (strcmp(layout_str
, "normalise") == 0 ||
722 strcmp(layout_str
, "normalize") == 0) {
724 strcpy(alt_layout
, map_num(r6layout
, array
.layout
));
725 hyphen
= strrchr(alt_layout
, '-');
726 if (hyphen
&& strcmp(hyphen
, "-6") == 0) {
728 layout_str
= alt_layout
;
732 if (array
.layout
== map_name(r6layout
, layout_str
))
734 if (layout_str
&& strcmp(layout_str
, "preserve") == 0)
738 if (layout_str
== NULL
739 && (chunksize
== 0 || chunksize
*1024 == array
.chunk_size
)
740 && (raid_disks
== 0 || raid_disks
== array
.raid_disks
)) {
742 if (level
!= UnSet
&& level
!= array
.level
) {
743 /* Looks like this level change doesn't need
744 * a reshape after all.
746 c
= map_num(pers
, level
);
748 rv
= sysfs_set_str(sra
, NULL
, "level", c
);
751 fprintf(stderr
, Name
": %s: could not set level to %s\n",
754 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
755 fprintf(stderr
, " Bitmap must be removed before level can be changed\n");
758 } else if (!changed
&& !quiet
)
759 fprintf(stderr
, Name
": %s: no change requested\n",
764 c
= map_num(pers
, array
.level
);
765 if (c
== NULL
) c
= "-unknown-";
766 switch(array
.level
) {
767 default: /* raid0, linear, multipath cannot be reconfigured */
768 fprintf(stderr
, Name
": %s array %s cannot be reshaped.\n",
773 case LEVEL_FAULTY
: /* only 'layout' change is permitted */
775 if (chunksize
|| raid_disks
) {
776 fprintf(stderr
, Name
": %s: Cannot change chunksize or disks of a 'faulty' array\n",
781 if (layout_str
== NULL
)
782 break; /* nothing to do.... */
784 array
.layout
= parse_layout_faulty(layout_str
);
785 if (array
.layout
< 0) {
787 fprintf(stderr
, Name
": %s: layout %s not understood for 'faulty' array\n",
788 devname
, layout_str
);
792 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
793 fprintf(stderr
, Name
": Cannot set layout for %s: %s\n",
794 devname
, strerror(errno
));
797 printf("layout for %s set to %d\n", devname
, array
.layout
);
800 case 1: /* only raid_disks can each be changed. */
802 if (chunksize
|| layout_str
!= NULL
) {
803 fprintf(stderr
, Name
": %s: Cannot change chunk size or layout for a RAID1 array.\n",
808 if (raid_disks
> 0) {
809 array
.raid_disks
= raid_disks
;
810 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
811 fprintf(stderr
, Name
": Cannot set raid-devices for %s: %s\n",
812 devname
, strerror(errno
));
823 * layout/chunksize/raid_disks can be changed
824 * though the kernel may not support it all.
826 st
= super_by_fd(fd
);
829 * There are three possibilities.
830 * 1/ The array will shrink.
831 * We need to ensure the reshape will pause before reaching
832 * the 'critical section'. We also need to fork and wait for
833 * that to happen. When it does we
834 * suspend/backup/complete/unfreeze
836 * 2/ The array will not change size.
837 * This requires that we keep a backup of a sliding window
838 * so that we can restore data after a crash. So we need
839 * to fork and monitor progress.
841 * 3/ The array will grow. This is relatively easy.
842 * However the kernel's restripe routines will cheerfully
843 * overwrite some early data before it is safe. So we
844 * need to make a backup of the early parts of the array
845 * and be ready to restore it if rebuild aborts very early.
847 * We backup data by writing it to one spare, or to a
848 * file which was given on command line.
850 * [FOLLOWING IS OLD AND PARTLY WRONG]
851 * So: we enumerate the devices in the array and
852 * make sure we can open all of them.
853 * Then we freeze the early part of the array and
854 * backup to the various spares.
855 * Then we request changes and start the reshape.
856 * Monitor progress until it has passed the danger zone.
857 * and finally invalidate the copied data and unfreeze the
858 * start of the array.
860 * In each case, we first make sure that storage is available
861 * for the required backup.
863 * - request the shape change.
864 * - for to handle backup etc.
866 nchunk
= ochunk
= array
.chunk_size
;
867 nlayout
= olayout
= array
.layout
;
868 ndisks
= odisks
= array
.raid_disks
;
871 nchunk
= chunksize
* 1024;
872 if (size
% chunksize
) {
873 fprintf(stderr
, Name
": component size %lluK is not"
874 " a multiple of chunksize %dK\n",
879 if (layout_str
!= NULL
)
880 switch(array
.level
) {
881 case 4: /* ignore layout */
884 nlayout
= map_name(r5layout
, layout_str
);
885 if (nlayout
== UnSet
) {
886 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
894 nlayout
= map_name(r6layout
, layout_str
);
895 if (nlayout
== UnSet
) {
896 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
903 if (raid_disks
) ndisks
= raid_disks
;
907 if (array
.level
== 6) {
908 odata
--; /* number of data disks */
912 if (odata
== ndata
&&
913 get_linux_version() < 2006032) {
914 fprintf(stderr
, Name
": in-place reshape is not safe before 2.6.32, sorry.\n");
918 /* Check that we can hold all the data */
919 get_dev_size(fd
, NULL
, &array_size
);
920 if (ndata
* size
< (array_size
/1024)) {
921 fprintf(stderr
, Name
": this change will reduce the size of the array.\n"
922 " use --grow --array-size first to truncate array.\n"
923 " e.g. mdadm --grow %s --array-size %llu\n",
924 devname
, ndata
* size
);
929 /* So how much do we need to backup.
930 * We need an amount of data which is both a whole number of
931 * old stripes and a whole number of new stripes.
932 * So LCM for (chunksize*datadisks).
934 a
= ochunk
/512 * odata
;
935 b
= nchunk
/512 * ndata
;
943 /* LCM == product / GCD */
944 blocks
= ochunk
/512 * nchunk
/512 * odata
* ndata
/ a
;
947 sra
= sysfs_read(fd
, 0,
948 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
951 if (ndata
== odata
) {
952 /* Make 'blocks' bigger for better throughput, but
953 * not so big that we reject it below.
954 * Try for 16 megabytes
956 while (blocks
* 32 < sra
->component_size
&&
960 fprintf(stderr
, Name
": Need to backup %luK of critical "
961 "section..\n", blocks
/2);
964 fprintf(stderr
, Name
": %s: Cannot get array details from sysfs\n",
970 if (blocks
>= sra
->component_size
/2) {
971 fprintf(stderr
, Name
": %s: Something wrong - reshape aborted\n",
976 nrdisks
= array
.nr_disks
+ sra
->array
.spare_disks
;
977 /* Now we need to open all these devices so we can read/write.
979 fdlist
= malloc((1+nrdisks
) * sizeof(int));
980 offsets
= malloc((1+nrdisks
) * sizeof(offsets
[0]));
981 if (!fdlist
|| !offsets
) {
982 fprintf(stderr
, Name
": malloc failed: grow aborted\n");
986 for (d
=0; d
<= nrdisks
; d
++)
988 d
= array
.raid_disks
;
989 for (sd
= sra
->devs
; sd
; sd
=sd
->next
) {
990 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
992 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
993 char *dn
= map_dev(sd
->disk
.major
,
995 fdlist
[sd
->disk
.raid_disk
]
996 = dev_open(dn
, O_RDONLY
);
997 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
998 if (fdlist
[sd
->disk
.raid_disk
] < 0) {
999 fprintf(stderr
, Name
": %s: cannot open component %s\n",
1000 devname
, dn
?dn
:"-unknown-");
1004 } else if (backup_file
== NULL
) {
1006 char *dn
= map_dev(sd
->disk
.major
,
1008 fdlist
[d
] = dev_open(dn
, O_RDWR
);
1009 offsets
[d
] = (sd
->data_offset
+ sra
->component_size
- blocks
- 8)*512;
1011 fprintf(stderr
, Name
": %s: cannot open component %s\n",
1012 devname
, dn
?dn
:"-unknown");
1019 if (backup_file
== NULL
) {
1020 if (ndata
<= odata
) {
1021 fprintf(stderr
, Name
": %s: Cannot grow - need backup-file\n",
1025 } else if (sra
->array
.spare_disks
== 0) {
1026 fprintf(stderr
, Name
": %s: Cannot grow - need a spare or "
1027 "backup-file to backup critical section\n",
1032 if (d
== array
.raid_disks
) {
1033 fprintf(stderr
, Name
": %s: No spare device for backup\n",
1039 /* need to check backup file is large enough */
1041 fdlist
[d
] = open(backup_file
, O_RDWR
|O_CREAT
|O_EXCL
,
1043 offsets
[d
] = 8 * 512;
1044 if (fdlist
[d
] < 0) {
1045 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1046 devname
, backup_file
, strerror(errno
));
1050 memset(buf
, 0, 512);
1051 for (i
=0; i
< blocks
+ 1 ; i
++) {
1052 if (write(fdlist
[d
], buf
, 512) != 512) {
1053 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1054 devname
, backup_file
, strerror(errno
));
1059 if (fsync(fdlist
[d
]) != 0) {
1060 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1061 devname
, backup_file
, strerror(errno
));
1068 /* lastly, check that the internal stripe cache is
1069 * large enough, or it won't work.
1072 cache
= (nchunk
< ochunk
) ? ochunk
: nchunk
;
1073 cache
= cache
* 4 / 4096;
1074 if (cache
< blocks
/ 8 / odisks
+ 16)
1075 /* Make it big enough to hold 'blocks' */
1076 cache
= blocks
/ 8 / odisks
+ 16;
1077 if (sra
->cache_size
< cache
)
1078 sysfs_set_num(sra
, NULL
, "stripe_cache_size",
1080 /* Right, everything seems fine. Let's kick things off.
1081 * If only changing raid_disks, use ioctl, else use
1084 if (ochunk
== nchunk
&& olayout
== nlayout
) {
1085 array
.raid_disks
= ndisks
;
1086 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
1089 fprintf(stderr
, Name
": Cannot set device shape for %s: %s\n",
1090 devname
, strerror(errno
));
1091 if (ndisks
< odisks
&&
1092 get_linux_version() < 2006030)
1093 fprintf(stderr
, Name
": linux 2.6.30 or later required\n");
1095 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1096 fprintf(stderr
, " Bitmap must be removed before shape can be changed\n");
1101 /* set them all just in case some old 'new_*' value
1102 * persists from some earlier problem
1104 int err
= err
; /* only used if rv==1, and always set if
1105 * rv==1, so initialisation not needed,
1106 * despite gcc warning
1108 if (sysfs_set_num(sra
, NULL
, "chunk_size", nchunk
) < 0)
1109 rv
= 1, err
= errno
;
1110 if (!rv
&& sysfs_set_num(sra
, NULL
, "layout", nlayout
) < 0)
1111 rv
= 1, err
= errno
;
1112 if (!rv
&& sysfs_set_num(sra
, NULL
, "raid_disks", ndisks
) < 0)
1113 rv
= 1, err
= errno
;
1115 fprintf(stderr
, Name
": Cannot set device shape for %s\n",
1117 if (get_linux_version() < 2006030)
1118 fprintf(stderr
, Name
": linux 2.6.30 or later required\n");
1120 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1121 fprintf(stderr
, " Bitmap must be removed before shape can be changed\n");
1126 if (ndisks
== 2 && odisks
== 2) {
1127 /* No reshape is needed in this trivial case */
1132 /* set up the backup-super-block. This requires the
1133 * uuid from the array.
1135 /* Find a superblock */
1136 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1140 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1142 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
1143 devfd
= dev_open(dn
, O_RDONLY
);
1146 ok
= st
->ss
->load_super(st
, devfd
, NULL
);
1152 fprintf(stderr
, Name
": %s: Cannot find a superblock\n",
1158 memset(&bsb
, 0, 512);
1159 memcpy(bsb
.magic
, "md_backup_data-1", 16);
1160 st
->ss
->uuid_from_super(st
, (int*)&bsb
.set_uuid
);
1161 bsb
.mtime
= __cpu_to_le64(time(0));
1162 bsb
.devstart2
= blocks
;
1163 stripes
= blocks
/ (ochunk
/512) / odata
;
1164 /* Now we just need to kick off the reshape and watch, while
1165 * handling backups of the data...
1166 * This is all done by a forked background process.
1171 if (check_env("MDADM_GROW_VERIFY"))
1172 fd
= open(devname
, O_RDONLY
| O_DIRECT
);
1175 mlockall(MCL_FUTURE
);
1178 done
= child_grow(fd
, sra
, stripes
,
1180 odisks
, ochunk
, array
.level
, olayout
, odata
,
1181 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1182 else if (odata
> ndata
)
1183 done
= child_shrink(fd
, sra
, stripes
,
1185 odisks
, ochunk
, array
.level
, olayout
, odata
,
1186 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1188 done
= child_same_size(fd
, sra
, stripes
,
1191 odisks
, ochunk
, array
.level
, olayout
, odata
,
1192 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1193 if (backup_file
&& done
)
1194 unlink(backup_file
);
1195 if (level
!= UnSet
&& level
!= array
.level
) {
1196 /* We need to wait for the reshape to finish
1197 * (which will have happened unless odata < ndata)
1198 * and then set the level
1201 c
= map_num(pers
, level
);
1203 exit(0);/* not possible */
1207 err
= sysfs_set_str(sra
, NULL
, "level", c
);
1209 fprintf(stderr
, Name
": %s: could not set level to %s\n",
1214 fprintf(stderr
, Name
": Cannot run child to monitor reshape: %s\n",
1219 /* The child will take care of unfreezing the array */
1228 if (rv
&& orig_level
!= UnSet
&& sra
) {
1229 c
= map_num(pers
, orig_level
);
1230 if (c
&& sysfs_set_str(sra
, NULL
, "level", c
) == 0)
1231 fprintf(stderr
, Name
": aborting level change\n");
1234 unfreeze_array(sra
, frozen
);
1239 * We run a child process in the background which performs the following
1241 * - wait for resync to reach a certain point
1242 * - suspend io to the following section
1243 * - backup that section
1244 * - allow resync to proceed further
1246 * - discard the backup.
1248 * When are combined in slightly different ways in the three cases.
1250 * - suspend/backup/allow/wait/resume/discard
1252 * - allow/wait/suspend/backup/allow/wait/resume/discard
1254 * - wait/resume/discard/suspend/backup/allow
1256 * suspend/backup/allow always come together
1257 * wait/resume/discard do too.
1258 * For the same-size case we have two backups to improve flow.
1262 /* FIXME return status is never checked */
1263 int grow_backup(struct mdinfo
*sra
,
1264 unsigned long long offset
, /* per device */
1265 unsigned long stripes
, /* per device */
1266 int *sources
, unsigned long long *offsets
,
1267 int disks
, int chunk
, int level
, int layout
,
1268 int dests
, int *destfd
, unsigned long long *destoffsets
,
1269 int part
, int *degraded
,
1272 /* Backup 'blocks' sectors at 'offset' on each device of the array,
1273 * to storage 'destfd' (offset 'destoffsets'), after first
1274 * suspending IO. Then allow resync to continue
1275 * over the suspended section.
1276 * Use part 'part' of the backup-super-block.
1281 unsigned long long new_degraded
;
1282 //printf("offset %llu\n", offset);
1287 sysfs_set_num(sra
, NULL
, "suspend_hi", (offset
+ stripes
* chunk
/512) * odata
);
1288 /* Check that array hasn't become degraded, else we might backup the wrong data */
1289 sysfs_get_ll(sra
, NULL
, "degraded", &new_degraded
);
1290 if (new_degraded
!= *degraded
) {
1291 /* check each device to ensure it is still working */
1293 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1294 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1296 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
1298 if (sysfs_get_str(sra
, sd
, "state", sbuf
, 20) < 0 ||
1299 strstr(sbuf
, "faulty") ||
1300 strstr(sbuf
, "in_sync") == NULL
) {
1301 /* this device is dead */
1302 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
1303 if (sd
->disk
.raid_disk
>= 0 &&
1304 sources
[sd
->disk
.raid_disk
] >= 0) {
1305 close(sources
[sd
->disk
.raid_disk
]);
1306 sources
[sd
->disk
.raid_disk
] = -1;
1311 *degraded
= new_degraded
;
1314 bsb
.arraystart2
= __cpu_to_le64(offset
* odata
);
1315 bsb
.length2
= __cpu_to_le64(stripes
* chunk
/512 * odata
);
1317 bsb
.arraystart
= __cpu_to_le64(offset
* odata
);
1318 bsb
.length
= __cpu_to_le64(stripes
* chunk
/512 * odata
);
1321 bsb
.magic
[15] = '2';
1322 for (i
= 0; i
< dests
; i
++)
1324 lseek64(destfd
[i
], destoffsets
[i
] + __le64_to_cpu(bsb
.devstart2
)*512, 0);
1326 lseek64(destfd
[i
], destoffsets
[i
], 0);
1328 rv
= save_stripes(sources
, offsets
,
1329 disks
, chunk
, level
, layout
,
1331 offset
*512*odata
, stripes
* chunk
* odata
,
1336 bsb
.mtime
= __cpu_to_le64(time(0));
1337 for (i
= 0; i
< dests
; i
++) {
1338 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
1340 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
1341 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
1342 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
1343 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
1345 if (lseek64(destfd
[i
], destoffsets
[i
] - 4096, 0) != destoffsets
[i
] - 4096)
1347 rv
= rv
?: write(destfd
[i
], &bsb
, 512);
1348 if (destoffsets
[i
] > 4096) {
1349 if (lseek64(destfd
[i
], destoffsets
[i
]+stripes
*chunk
*odata
, 0) !=
1350 destoffsets
[i
]+stripes
*chunk
*odata
)
1352 rv
= rv
?: write(destfd
[i
], &bsb
, 512);
1360 /* in 2.6.30, the value reported by sync_completed can be
1361 * less that it should be by one stripe.
1362 * This only happens when reshape hits sync_max and pauses.
1363 * So allow wait_backup to either extent sync_max further
1364 * than strictly necessary, or return before the
1365 * sync has got quite as far as we would really like.
1366 * This is what 'blocks2' is for.
1367 * The various caller give appropriate values so that
1370 /* FIXME return value is often ignored */
1371 int wait_backup(struct mdinfo
*sra
,
1372 unsigned long long offset
, /* per device */
1373 unsigned long long blocks
, /* per device */
1374 unsigned long long blocks2
, /* per device - hack */
1375 int dests
, int *destfd
, unsigned long long *destoffsets
,
1378 /* Wait for resync to pass the section that was backed up
1379 * then erase the backup and allow IO
1381 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
1382 unsigned long long completed
;
1388 sysfs_set_num(sra
, NULL
, "sync_max", offset
+ blocks
+ blocks2
);
1390 sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
1396 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
1397 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
1401 if (sysfs_get_str(sra
, NULL
, "sync_action",
1403 strncmp(action
, "reshape", 7) != 0)
1405 } while (completed
< offset
+ blocks
);
1409 bsb
.arraystart2
= __cpu_to_le64(0);
1410 bsb
.length2
= __cpu_to_le64(0);
1412 bsb
.arraystart
= __cpu_to_le64(0);
1413 bsb
.length
= __cpu_to_le64(0);
1415 bsb
.mtime
= __cpu_to_le64(time(0));
1417 for (i
= 0; i
< dests
; i
++) {
1418 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
1419 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
1420 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
1421 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
1422 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
1423 if (lseek64(destfd
[i
], destoffsets
[i
]-4096, 0) !=
1424 destoffsets
[i
]-4096)
1426 rv
= rv
?: write(destfd
[i
], &bsb
, 512);
1432 static void fail(char *msg
)
1435 rv
= write(2, msg
, strlen(msg
));
1436 rv
|= write(2, "\n", 1);
1440 static char *abuf
, *bbuf
;
1442 static void validate(int afd
, int bfd
, unsigned long long offset
)
1444 /* check that the data in the backup against the array.
1445 * This is only used for regression testing and should not
1446 * be used while the array is active
1450 lseek64(bfd
, offset
- 4096, 0);
1451 if (read(bfd
, &bsb2
, 512) != 512)
1452 fail("cannot read bsb");
1453 if (bsb2
.sb_csum
!= bsb_csum((char*)&bsb2
,
1454 ((char*)&bsb2
.sb_csum
)-((char*)&bsb2
)))
1455 fail("first csum bad");
1456 if (memcmp(bsb2
.magic
, "md_backup_data", 14) != 0)
1457 fail("magic is bad");
1458 if (memcmp(bsb2
.magic
, "md_backup_data-2", 16) == 0 &&
1459 bsb2
.sb_csum2
!= bsb_csum((char*)&bsb2
,
1460 ((char*)&bsb2
.sb_csum2
)-((char*)&bsb2
)))
1461 fail("second csum bad");
1463 if (__le64_to_cpu(bsb2
.devstart
)*512 != offset
)
1464 fail("devstart is wrong");
1467 unsigned long long len
= __le64_to_cpu(bsb2
.length
)*512;
1469 if (abuflen
< len
) {
1473 if (posix_memalign((void**)&abuf
, 4096, abuflen
) ||
1474 posix_memalign((void**)&bbuf
, 4096, abuflen
)) {
1476 /* just stop validating on mem-alloc failure */
1481 lseek64(bfd
, offset
, 0);
1482 if (read(bfd
, bbuf
, len
) != len
) {
1483 //printf("len %llu\n", len);
1484 fail("read first backup failed");
1486 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart
)*512, 0);
1487 if (read(afd
, abuf
, len
) != len
)
1488 fail("read first from array failed");
1489 if (memcmp(bbuf
, abuf
, len
) != 0) {
1492 printf("offset=%llu len=%llu\n",
1493 (unsigned long long)__le64_to_cpu(bsb2
.arraystart
)*512, len
);
1494 for (i
=0; i
<len
; i
++)
1495 if (bbuf
[i
] != abuf
[i
]) {
1496 printf("first diff byte %d\n", i
);
1500 fail("data1 compare failed");
1504 unsigned long long len
= __le64_to_cpu(bsb2
.length2
)*512;
1506 if (abuflen
< len
) {
1510 abuf
= malloc(abuflen
);
1511 bbuf
= malloc(abuflen
);
1514 lseek64(bfd
, offset
+__le64_to_cpu(bsb2
.devstart2
)*512, 0);
1515 if (read(bfd
, bbuf
, len
) != len
)
1516 fail("read second backup failed");
1517 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart2
)*512, 0);
1518 if (read(afd
, abuf
, len
) != len
)
1519 fail("read second from array failed");
1520 if (memcmp(bbuf
, abuf
, len
) != 0)
1521 fail("data2 compare failed");
1525 static int child_grow(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1526 int *fds
, unsigned long long *offsets
,
1527 int disks
, int chunk
, int level
, int layout
, int data
,
1528 int dests
, int *destfd
, unsigned long long *destoffsets
)
1533 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
1534 /* Don't start the 'reshape' */
1536 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1537 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1538 grow_backup(sra
, 0, stripes
,
1539 fds
, offsets
, disks
, chunk
, level
, layout
,
1540 dests
, destfd
, destoffsets
,
1542 validate(afd
, destfd
[0], destoffsets
[0]);
1543 wait_backup(sra
, 0, stripes
* chunk
/ 512, stripes
* chunk
/ 512,
1544 dests
, destfd
, destoffsets
,
1546 sysfs_set_num(sra
, NULL
, "suspend_lo", (stripes
* chunk
/512) * data
);
1548 /* FIXME this should probably be numeric */
1549 sysfs_set_str(sra
, NULL
, "sync_max", "max");
1553 static int child_shrink(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1554 int *fds
, unsigned long long *offsets
,
1555 int disks
, int chunk
, int level
, int layout
, int data
,
1556 int dests
, int *destfd
, unsigned long long *destoffsets
)
1559 unsigned long long start
;
1563 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
1565 start
= sra
->component_size
- stripes
* chunk
/512;
1566 sysfs_set_num(sra
, NULL
, "sync_max", start
);
1567 sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
1568 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1569 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1570 rv
= wait_backup(sra
, 0, start
- stripes
* chunk
/512, stripes
* chunk
/512,
1571 dests
, destfd
, destoffsets
, 0);
1574 grow_backup(sra
, 0, stripes
,
1576 disks
, chunk
, level
, layout
,
1577 dests
, destfd
, destoffsets
,
1579 validate(afd
, destfd
[0], destoffsets
[0]);
1580 wait_backup(sra
, start
, stripes
*chunk
/512, 0,
1581 dests
, destfd
, destoffsets
, 0);
1582 sysfs_set_num(sra
, NULL
, "suspend_lo", (stripes
* chunk
/512) * data
);
1584 /* FIXME this should probably be numeric */
1585 sysfs_set_str(sra
, NULL
, "sync_max", "max");
1589 static int child_same_size(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1590 int *fds
, unsigned long long *offsets
,
1591 unsigned long long start
,
1592 int disks
, int chunk
, int level
, int layout
, int data
,
1593 int dests
, int *destfd
, unsigned long long *destoffsets
)
1595 unsigned long long size
;
1596 unsigned long tailstripes
= stripes
;
1599 unsigned long long speed
;
1603 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
1606 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1607 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1609 sysfs_get_ll(sra
, NULL
, "sync_speed_min", &speed
);
1610 sysfs_set_num(sra
, NULL
, "sync_speed_min", 200000);
1612 grow_backup(sra
, start
, stripes
,
1614 disks
, chunk
, level
, layout
,
1615 dests
, destfd
, destoffsets
,
1617 grow_backup(sra
, (start
+ stripes
) * chunk
/512, stripes
,
1619 disks
, chunk
, level
, layout
,
1620 dests
, destfd
, destoffsets
,
1622 validate(afd
, destfd
[0], destoffsets
[0]);
1624 start
+= stripes
* 2; /* where to read next */
1625 size
= sra
->component_size
/ (chunk
/512);
1626 while (start
< size
) {
1627 if (wait_backup(sra
, (start
-stripes
*2)*chunk
/512,
1628 stripes
*chunk
/512, 0,
1629 dests
, destfd
, destoffsets
,
1632 sysfs_set_num(sra
, NULL
, "suspend_lo", start
*chunk
/512 * data
);
1633 if (start
+ stripes
> size
)
1634 tailstripes
= (size
- start
);
1636 grow_backup(sra
, start
*chunk
/512, tailstripes
,
1638 disks
, chunk
, level
, layout
,
1639 dests
, destfd
, destoffsets
,
1640 part
, °raded
, buf
);
1643 validate(afd
, destfd
[0], destoffsets
[0]);
1645 if (wait_backup(sra
, (start
-stripes
*2) * chunk
/512, stripes
* chunk
/512, 0,
1646 dests
, destfd
, destoffsets
,
1649 sysfs_set_num(sra
, NULL
, "suspend_lo", ((start
-stripes
)*chunk
/512) * data
);
1650 wait_backup(sra
, (start
-stripes
) * chunk
/512, tailstripes
* chunk
/512, 0,
1651 dests
, destfd
, destoffsets
,
1653 sysfs_set_num(sra
, NULL
, "suspend_lo", (size
*chunk
/512) * data
);
1654 sysfs_set_num(sra
, NULL
, "sync_speed_min", speed
);
1660 * If any spare contains md_back_data-1 which is recent wrt mtime,
1661 * write that data into the array and update the super blocks with
1662 * the new reshape_progress
1664 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
, int cnt
,
1665 char *backup_file
, int verbose
)
1669 unsigned long long *offsets
;
1670 unsigned long long nstripe
, ostripe
;
1673 if (info
->new_level
!= info
->array
.level
)
1674 return 1; /* Cannot handle level changes (they are instantaneous) */
1676 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
1677 if (info
->array
.level
== 6) odata
--; /* number of data disks */
1678 ndata
= info
->array
.raid_disks
- 1;
1679 if (info
->new_level
== 6) ndata
--;
1681 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
1683 if (info
->delta_disks
<= 0)
1684 /* Didn't grow, so the backup file must have
1688 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
1689 struct mdinfo dinfo
;
1692 char *devname
, namebuf
[20];
1694 /* This was a spare and may have some saved data on it.
1695 * Load the superblock, find and load the
1696 * backup_super_block.
1697 * If either fail, go on to next device.
1698 * If the backup contains no new info, just return
1699 * else restore data and update all superblocks
1701 if (i
== old_disks
-1) {
1702 fd
= open(backup_file
, O_RDONLY
);
1704 fprintf(stderr
, Name
": backup file %s inaccessible: %s\n",
1705 backup_file
, strerror(errno
));
1708 devname
= backup_file
;
1713 if (st
->ss
->load_super(st
, fd
, NULL
))
1716 st
->ss
->getinfo_super(st
, &dinfo
);
1717 st
->ss
->free_super(st
);
1720 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
1722 fprintf(stderr
, Name
": Cannot seek on device %d\n", i
);
1723 continue; /* Cannot seek */
1725 sprintf(namebuf
, "device-%d", i
);
1728 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
)) {
1730 fprintf(stderr
, Name
": Cannot read from %s\n", devname
);
1731 continue; /* Cannot read */
1733 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0 &&
1734 memcmp(bsb
.magic
, "md_backup_data-2", 16) != 0) {
1736 fprintf(stderr
, Name
": No backup metadata on %s\n", devname
);
1739 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
))) {
1741 fprintf(stderr
, Name
": Bad backup-metadata checksum on %s\n", devname
);
1742 continue; /* bad checksum */
1744 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0 &&
1745 bsb
.sb_csum2
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum2
)-((char*)&bsb
))) {
1747 fprintf(stderr
, Name
": Bad backup-metadata checksum2 on %s\n", devname
);
1748 continue; /* Bad second checksum */
1750 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0) {
1752 fprintf(stderr
, Name
": Wrong uuid on backup-metadata on %s\n", devname
);
1753 continue; /* Wrong uuid */
1756 /* array utime and backup-mtime should be updated at much the same time, but it seems that
1757 * sometimes they aren't... So allow considerable flexability in matching, and allow
1758 * this test to be overridden by an environment variable.
1760 if (info
->array
.utime
> __le64_to_cpu(bsb
.mtime
) + 2*60*60 ||
1761 info
->array
.utime
< __le64_to_cpu(bsb
.mtime
) - 10*60) {
1762 if (check_env("MDADM_GROW_ALLOW_OLD")) {
1763 fprintf(stderr
, Name
": accepting backup with timestamp %lu "
1764 "for array with timestamp %lu\n",
1765 (unsigned long)__le64_to_cpu(bsb
.mtime
),
1766 (unsigned long)info
->array
.utime
);
1769 fprintf(stderr
, Name
": too-old timestamp on "
1770 "backup-metadata on %s\n", devname
);
1771 continue; /* time stamp is too bad */
1775 if (bsb
.magic
[15] == '1') {
1776 if (info
->delta_disks
>= 0) {
1777 /* reshape_progress is increasing */
1778 if (__le64_to_cpu(bsb
.arraystart
) + __le64_to_cpu(bsb
.length
) <
1779 info
->reshape_progress
) {
1782 fprintf(stderr
, Name
": backup-metadata found on %s but is not needed\n", devname
);
1783 continue; /* No new data here */
1786 /* reshape_progress is decreasing */
1787 if (__le64_to_cpu(bsb
.arraystart
) >=
1788 info
->reshape_progress
)
1789 goto nonew
; /* No new data here */
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
&&
1796 __le64_to_cpu(bsb
.arraystart2
) + __le64_to_cpu(bsb
.length2
) <
1797 info
->reshape_progress
)
1798 goto nonew
; /* No new data here */
1800 /* reshape_progress is decreasing */
1801 if (__le64_to_cpu(bsb
.arraystart
) >=
1802 info
->reshape_progress
&&
1803 __le64_to_cpu(bsb
.arraystart2
) >=
1804 info
->reshape_progress
)
1805 goto nonew
; /* No new data here */
1808 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0) {
1811 fprintf(stderr
, Name
": Failed to verify secondary backup-metadata block on %s\n",
1813 continue; /* Cannot seek */
1815 /* There should be a duplicate backup superblock 4k before here */
1816 if (lseek64(fd
, -4096, 1) < 0 ||
1817 read(fd
, &bsb2
, 4096) != 4096)
1818 goto second_fail
; /* Cannot find leading superblock */
1819 if (bsb
.magic
[15] == '1')
1820 bsbsize
= offsetof(struct mdp_backup_super
, pad1
);
1822 bsbsize
= offsetof(struct mdp_backup_super
, pad
);
1823 if (memcmp(&bsb2
, &bsb
, bsbsize
) != 0)
1824 goto second_fail
; /* Cannot find leading superblock */
1826 /* Now need the data offsets for all devices. */
1827 offsets
= malloc(sizeof(*offsets
)*info
->array
.raid_disks
);
1828 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
1831 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
1832 /* FIXME should be this be an error */
1834 st
->ss
->getinfo_super(st
, &dinfo
);
1835 st
->ss
->free_super(st
);
1836 offsets
[j
] = dinfo
.data_offset
* 512;
1838 printf(Name
": restoring critical section\n");
1840 if (restore_stripes(fdlist
, offsets
,
1841 info
->array
.raid_disks
,
1845 fd
, __le64_to_cpu(bsb
.devstart
)*512,
1846 __le64_to_cpu(bsb
.arraystart
)*512,
1847 __le64_to_cpu(bsb
.length
)*512)) {
1848 /* didn't succeed, so giveup */
1850 fprintf(stderr
, Name
": Error restoring backup from %s\n",
1855 if (bsb
.magic
[15] == '2' &&
1856 restore_stripes(fdlist
, offsets
,
1857 info
->array
.raid_disks
,
1861 fd
, __le64_to_cpu(bsb
.devstart
)*512 +
1862 __le64_to_cpu(bsb
.devstart2
)*512,
1863 __le64_to_cpu(bsb
.arraystart2
)*512,
1864 __le64_to_cpu(bsb
.length2
)*512)) {
1865 /* didn't succeed, so giveup */
1867 fprintf(stderr
, Name
": Error restoring second backup from %s\n",
1873 /* Ok, so the data is restored. Let's update those superblocks. */
1875 if (info
->delta_disks
>= 0) {
1876 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
) +
1877 __le64_to_cpu(bsb
.length
);
1878 if (bsb
.magic
[15] == '2') {
1879 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
) +
1880 __le64_to_cpu(bsb
.length2
);
1881 if (p2
> info
->reshape_progress
)
1882 info
->reshape_progress
= p2
;
1885 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
);
1886 if (bsb
.magic
[15] == '2') {
1887 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
);
1888 if (p2
< info
->reshape_progress
)
1889 info
->reshape_progress
= p2
;
1892 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
1893 if (fdlist
[j
] < 0) continue;
1894 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
1896 st
->ss
->getinfo_super(st
, &dinfo
);
1897 dinfo
.reshape_progress
= info
->reshape_progress
;
1898 st
->ss
->update_super(st
, &dinfo
,
1899 "_reshape_progress",
1901 st
->ss
->store_super(st
, fdlist
[j
]);
1902 st
->ss
->free_super(st
);
1906 /* Didn't find any backup data, try to see if any
1909 if (info
->delta_disks
< 0) {
1910 /* When shrinking, the critical section is at the end.
1911 * So see if we are before the critical section.
1913 unsigned long long first_block
;
1914 nstripe
= ostripe
= 0;
1916 while (ostripe
>= nstripe
) {
1917 ostripe
+= info
->array
.chunk_size
/ 512;
1918 first_block
= ostripe
* odata
;
1919 nstripe
= first_block
/ ndata
/ (info
->new_chunk
/512) *
1920 (info
->new_chunk
/512);
1923 if (info
->reshape_progress
>= first_block
)
1926 if (info
->delta_disks
> 0) {
1927 /* See if we are beyond the critical section. */
1928 unsigned long long last_block
;
1929 nstripe
= ostripe
= 0;
1931 while (nstripe
>= ostripe
) {
1932 nstripe
+= info
->new_chunk
/ 512;
1933 last_block
= nstripe
* ndata
;
1934 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
1935 (info
->array
.chunk_size
/512);
1938 if (info
->reshape_progress
>= last_block
)
1941 /* needed to recover critical section! */
1943 fprintf(stderr
, Name
": Failed to find backup of critical section\n");
1947 int Grow_continue(int mdfd
, struct supertype
*st
, struct mdinfo
*info
,
1950 /* Array is assembled and ready to be started, but
1951 * monitoring is probably required.
1954 * - set upper bound for resync
1955 * - initialise the 'suspend' boundaries
1956 * - switch to read-write
1957 * - fork and continue monitoring
1961 unsigned long long backup_offsets
[1];
1962 int odisks
, ndisks
, ochunk
, nchunk
,odata
,ndata
;
1963 unsigned long a
,b
,blocks
,stripes
;
1966 unsigned long long *offsets
;
1968 struct mdinfo
*sra
, *sd
;
1973 err
= sysfs_set_str(info
, NULL
, "array_state", "readonly");
1977 /* make sure reshape doesn't progress until we are ready */
1978 sysfs_set_str(info
, NULL
, "sync_max", "0");
1979 sysfs_set_str(info
, NULL
, "array_state", "active"); /* FIXME or clean */
1981 /* ndisks is not growing, so raid_disks is old and +delta is new */
1982 odisks
= info
->array
.raid_disks
;
1983 ndisks
= odisks
+ info
->delta_disks
;
1986 if (info
->array
.level
== 6) {
1990 ochunk
= info
->array
.chunk_size
;
1991 nchunk
= info
->new_chunk
;
1994 a
= ochunk
/512 * odata
;
1995 b
= nchunk
/512 * ndata
;
2003 /* LCM == product / GCD */
2004 blocks
= ochunk
/512 * nchunk
/512 * odata
* ndata
/ a
;
2006 sra
= sysfs_read(-1, devname2devnum(info
->sys_name
),
2007 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
2012 while (blocks
* 32 < sra
->component_size
&&
2015 stripes
= blocks
/ (info
->array
.chunk_size
/512) / odata
;
2017 /* check that the internal stripe cache is
2018 * large enough, or it won't work.
2020 cache
= (nchunk
< ochunk
) ? ochunk
: nchunk
;
2021 cache
= cache
* 4 / 4096;
2022 if (cache
< blocks
/ 8 / odisks
+ 16)
2023 /* Make it big enough to hold 'blocks' */
2024 cache
= blocks
/ 8 / odisks
+ 16;
2025 if (sra
->cache_size
< cache
)
2026 sysfs_set_num(sra
, NULL
, "stripe_cache_size",
2029 memset(&bsb
, 0, 512);
2030 memcpy(bsb
.magic
, "md_backup_data-1", 16);
2031 memcpy(&bsb
.set_uuid
, info
->uuid
, 16);
2032 bsb
.mtime
= __cpu_to_le64(time(0));
2033 bsb
.devstart2
= blocks
;
2035 backup_fd
= open(backup_file
, O_RDWR
|O_CREAT
, S_IRUSR
| S_IWUSR
);
2036 backup_list
[0] = backup_fd
;
2037 backup_offsets
[0] = 8 * 512;
2038 fds
= malloc(odisks
* sizeof(fds
[0]));
2039 offsets
= malloc(odisks
* sizeof(offsets
[0]));
2040 for (d
=0; d
<odisks
; d
++)
2043 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2044 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2046 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
2047 char *dn
= map_dev(sd
->disk
.major
,
2049 fds
[sd
->disk
.raid_disk
]
2050 = dev_open(dn
, O_RDONLY
);
2051 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
2052 if (fds
[sd
->disk
.raid_disk
] < 0) {
2053 fprintf(stderr
, Name
": %s: cannot open component %s\n",
2054 info
->sys_name
, dn
?dn
:"-unknown-");
2065 mlockall(MCL_FUTURE
);
2066 if (info
->delta_disks
< 0)
2067 done
= child_shrink(-1, info
, stripes
,
2069 info
->array
.raid_disks
,
2070 info
->array
.chunk_size
,
2071 info
->array
.level
, info
->array
.layout
,
2073 1, backup_list
, backup_offsets
);
2074 else if (info
->delta_disks
== 0) {
2075 /* The 'start' is a per-device stripe number.
2076 * reshape_progress is a per-array sector number.
2077 * So divide by ndata * chunk_size
2079 unsigned long long start
= info
->reshape_progress
/ ndata
;
2080 start
/= (info
->array
.chunk_size
/512);
2081 done
= child_same_size(-1, info
, stripes
,
2084 info
->array
.raid_disks
,
2085 info
->array
.chunk_size
,
2086 info
->array
.level
, info
->array
.layout
,
2088 1, backup_list
, backup_offsets
);
2090 if (backup_file
&& done
)
2091 unlink(backup_file
);
2092 /* FIXME should I intuit a level change */
2095 fprintf(stderr
, Name
": Cannot run child to continue monitoring reshape: %s\n",