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
;
213 fprintf(stderr
, Name
": Warning - bitmaps created on this kernel"
214 " are not portable\n"
215 " between different architectures. Consider upgrading"
216 " 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) {
332 ": Cannot add bitmap while array is"
333 " resyncing or reshaping etc.\n");
334 fprintf(stderr
, Name
": failed to set internal bitmap.\n");
341 int max_devs
= st
->max_devs
;
343 /* try to load a superblock */
344 for (d
=0; d
<max_devs
; d
++) {
345 mdu_disk_info_t disk
;
349 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
351 if ((disk
.major
==0 && disk
.minor
==0) ||
352 (disk
.state
& (1<<MD_DISK_REMOVED
)))
354 dv
= map_dev(disk
.major
, disk
.minor
, 1);
356 fd2
= dev_open(dv
, O_RDONLY
);
358 st
->ss
->load_super(st
, fd2
, NULL
) == 0) {
360 st
->ss
->uuid_from_super(st
, uuid
);
366 fprintf(stderr
, Name
": cannot find UUID for array!\n");
369 if (CreateBitmap(file
, force
, (char*)uuid
, chunk
,
370 delay
, write_behind
, bitmapsize
, major
)) {
373 bitmap_fd
= open(file
, O_RDWR
);
375 fprintf(stderr
, Name
": weird: %s cannot be opened\n",
379 if (ioctl(fd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
383 ": Cannot add bitmap while array is"
384 " resyncing or reshaping etc.\n");
385 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
386 devname
, strerror(err
));
396 * When reshaping an array we might need to backup some data.
397 * This is written to all spares with a 'super_block' describing it.
398 * The superblock goes 4K from the end of the used space on the
400 * It if written after the backup is complete.
401 * It has the following structure.
404 static struct mdp_backup_super
{
405 char magic
[16]; /* md_backup_data-1 or -2 */
408 /* start/sizes in 512byte sectors */
409 __u64 devstart
; /* address on backup device/file of data */
412 __u32 sb_csum
; /* csum of preceeding bytes. */
414 __u64 devstart2
; /* offset in to data of second section */
417 __u32 sb_csum2
; /* csum of preceeding bytes. */
419 } __attribute__((aligned(512))) bsb
, bsb2
;
421 __u32
bsb_csum(char *buf
, int len
)
425 for (i
=0; i
<len
; i
++)
426 csum
= (csum
<<3) + buf
[0];
427 return __cpu_to_le32(csum
);
430 static int child_grow(int afd
, struct mdinfo
*sra
, unsigned long blocks
,
431 int *fds
, unsigned long long *offsets
,
432 int disks
, int chunk
, int level
, int layout
, int data
,
433 int dests
, int *destfd
, unsigned long long *destoffsets
);
434 static int child_shrink(int afd
, struct mdinfo
*sra
, unsigned long blocks
,
435 int *fds
, unsigned long long *offsets
,
436 int disks
, int chunk
, int level
, int layout
, int data
,
437 int dests
, int *destfd
, unsigned long long *destoffsets
);
438 static int child_same_size(int afd
, struct mdinfo
*sra
, unsigned long blocks
,
439 int *fds
, unsigned long long *offsets
,
440 unsigned long long start
,
441 int disks
, int chunk
, int level
, int layout
, int data
,
442 int dests
, int *destfd
, unsigned long long *destoffsets
);
444 int freeze_array(struct mdinfo
*sra
)
446 /* Try to freeze resync on this array.
447 * Return -1 if the array is busy,
448 * return 0 if this kernel doesn't support 'frozen'
449 * return 1 if it worked.
452 if (sysfs_get_str(sra
, NULL
, "sync_action", buf
, 20) <= 0)
454 if (strcmp(buf
, "idle\n") != 0 &&
455 strcmp(buf
, "frozen\n") != 0)
457 if (sysfs_set_str(sra
, NULL
, "sync_action", "frozen") < 0)
462 void unfreeze_array(struct mdinfo
*sra
, int frozen
)
464 /* If 'frozen' is 1, unfreeze the array */
466 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
469 void wait_reshape(struct mdinfo
*sra
)
471 int fd
= sysfs_get_fd(sra
, NULL
, "sync_action");
478 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
480 if (sysfs_fd_get_str(fd
, action
, 20) < 0) {
484 } while (strncmp(action
, "reshape", 7) == 0);
488 int Grow_reshape(char *devname
, int fd
, int quiet
, char *backup_file
,
490 int level
, char *layout_str
, int chunksize
, int raid_disks
)
492 /* Make some changes in the shape of an array.
493 * The kernel must support the change.
495 * There are three different changes. Each can trigger
496 * a resync or recovery so we freeze that until we have
497 * requested everything (if kernel supports freezing - 2.6.30).
499 * - change size (i.e. component_size)
501 * - change layout/chunksize/ndisks
503 * The last can require a reshape. It is different on different
504 * levels so we need to check the level before actioning it.
505 * Some times the level change needs to be requested after the
506 * reshape (e.g. raid6->raid5, raid5->raid0)
509 struct mdu_array_info_s array
, orig
;
512 struct supertype
*st
;
515 int nlayout
, olayout
;
517 unsigned int ndata
, odata
;
518 int orig_level
= UnSet
;
521 unsigned long long *offsets
;
526 unsigned long a
,b
, blocks
, stripes
;
528 unsigned long long array_size
;
535 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) < 0) {
536 fprintf(stderr
, Name
": %s is not an active md array - aborting\n",
542 (chunksize
|| level
!= UnSet
|| layout_str
|| raid_disks
)) {
543 fprintf(stderr
, Name
": cannot change component size at the same time "
544 "as other changes.\n"
545 " Change size first, then check data is intact before "
546 "making other changes.\n");
550 if (raid_disks
&& raid_disks
< array
.raid_disks
&& array
.level
> 1 &&
551 get_linux_version() < 2006032 &&
552 !check_env("MDADM_FORCE_FEWER")) {
553 fprintf(stderr
, Name
": reducing the number of devices is not safe before Linux 2.6.32\n"
554 " Please use a newer kernel\n");
557 sra
= sysfs_read(fd
, 0, GET_LEVEL
);
559 frozen
= freeze_array(sra
);
561 fprintf(stderr
, Name
": failed to read sysfs parameters for %s\n",
566 fprintf(stderr
, Name
": %s is performing resync/recovery and cannot"
567 " be reshaped\n", devname
);
571 /* ========= set size =============== */
572 if (size
>= 0 && (size
== 0 || size
!= array
.size
)) {
574 if (array
.size
!= size
) {
575 /* got truncated to 32bit, write to
576 * component_size instead
579 rv
= sysfs_set_num(sra
, NULL
,
580 "component_size", size
);
584 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
587 fprintf(stderr
, Name
": Cannot set device size for %s: %s\n",
588 devname
, strerror(err
));
590 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
591 fprintf(stderr
, " Bitmap must be removed before size can be changed\n");
595 ioctl(fd
, GET_ARRAY_INFO
, &array
);
596 size
= get_component_size(fd
)/2;
600 fprintf(stderr
, Name
": component size of %s has been set to %lluK\n",
604 size
= get_component_size(fd
)/2;
609 /* ======= set level =========== */
610 if (level
!= UnSet
&& level
!= array
.level
) {
611 /* Trying to change the level.
612 * We might need to change layout first and schedule a
613 * level change for later.
614 * Level changes that can happen immediately are:
615 * 0->4,5,6 1->5 4->5,6 5->1,6
616 * Level changes that need a layout change first are:
617 * 6->5,4,0 : need a -6 layout, or parity-last
618 * 5->4,0 : need parity-last
620 if ((array
.level
== 6 || array
.level
== 5) &&
621 (level
== 5 || level
== 4 || level
== 0)) {
622 /* Don't change level yet, but choose intermediate
626 if (layout_str
== NULL
)
627 switch (array
.layout
) {
628 case ALGORITHM_LEFT_ASYMMETRIC
:
629 case ALGORITHM_LEFT_ASYMMETRIC_6
:
630 case ALGORITHM_ROTATING_N_RESTART
:
631 layout_str
= "left-asymmetric-6";
633 case ALGORITHM_LEFT_SYMMETRIC
:
634 case ALGORITHM_LEFT_SYMMETRIC_6
:
635 case ALGORITHM_ROTATING_N_CONTINUE
:
636 layout_str
= "left-symmetric-6";
638 case ALGORITHM_RIGHT_ASYMMETRIC
:
639 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
640 case ALGORITHM_ROTATING_ZERO_RESTART
:
641 layout_str
= "right-asymmetric-6";
643 case ALGORITHM_RIGHT_SYMMETRIC
:
644 case ALGORITHM_RIGHT_SYMMETRIC_6
:
645 layout_str
= "right-symmetric-6";
647 case ALGORITHM_PARITY_0
:
648 case ALGORITHM_PARITY_0_6
:
649 layout_str
= "parity-first-6";
651 case ALGORITHM_PARITY_N
:
652 layout_str
= "parity-last";
655 fprintf(stderr
, Name
": %s: cannot"
656 "convert layout to RAID5 equivalent\n",
662 int l
= map_name(r5layout
, layout_str
);
664 fprintf(stderr
, Name
": %s: layout '%s' not recognised\n",
665 devname
, layout_str
);
669 if (l
!= ALGORITHM_PARITY_N
) {
670 /* need the -6 version */
671 char *ls
= map_num(r5layout
, l
);
672 strcat(strcpy(alt_layout
, ls
),
674 layout_str
= alt_layout
;
678 /* The final raid6->raid5 conversion
679 * will reduce the number of disks,
680 * so now we need to aim higher
684 layout_str
= "parity-last";
686 c
= map_num(pers
, level
);
688 rv
= 1;/* not possible */
691 err
= sysfs_set_str(sra
, NULL
, "level", c
);
694 fprintf(stderr
, Name
": %s: could not set level to %s\n",
697 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
698 fprintf(stderr
, " Bitmap must be removed before level can be changed\n");
703 orig_level
= orig
.level
;
704 ioctl(fd
, GET_ARRAY_INFO
, &array
);
705 if (layout_str
== NULL
&&
706 orig
.level
== 5 && level
== 6 &&
707 array
.layout
!= orig
.layout
)
708 layout_str
= map_num(r5layout
, orig
.layout
);
710 fprintf(stderr
, Name
" level of %s changed to %s\n",
716 /* ========= set shape (chunk_size / layout / ndisks) ============== */
717 /* Check if layout change is a no-op */
718 if (layout_str
) switch(array
.level
) {
720 if (array
.layout
== map_name(r5layout
, layout_str
))
724 if (layout_str
== NULL
&&
725 ((chunksize
&& chunksize
* 1024 != array
.chunk_size
) ||
726 (raid_disks
&& raid_disks
!= array
.raid_disks
)) &&
727 array
.layout
>= 16) {
729 ": %s has a non-standard layout. If you wish to preserve this\n"
730 " during the reshape, please specify --layout=preserve\n"
731 " If you want to change it, specify a layout or use --layout=normalise\n",
736 if (strcmp(layout_str
, "normalise") == 0 ||
737 strcmp(layout_str
, "normalize") == 0) {
739 strcpy(alt_layout
, map_num(r6layout
, array
.layout
));
740 hyphen
= strrchr(alt_layout
, '-');
741 if (hyphen
&& strcmp(hyphen
, "-6") == 0) {
743 layout_str
= alt_layout
;
747 if (array
.layout
== map_name(r6layout
, layout_str
))
749 if (layout_str
&& strcmp(layout_str
, "preserve") == 0)
753 if (layout_str
== NULL
754 && (chunksize
== 0 || chunksize
*1024 == array
.chunk_size
)
755 && (raid_disks
== 0 || raid_disks
== array
.raid_disks
)) {
757 if (level
!= UnSet
&& level
!= array
.level
) {
758 /* Looks like this level change doesn't need
759 * a reshape after all.
761 c
= map_num(pers
, level
);
763 rv
= sysfs_set_str(sra
, NULL
, "level", c
);
766 fprintf(stderr
, Name
": %s: could not set level to %s\n",
769 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
770 fprintf(stderr
, " Bitmap must be removed before level can be changed\n");
774 } else if (!changed
&& !quiet
)
775 fprintf(stderr
, Name
": %s: no change requested\n",
780 c
= map_num(pers
, array
.level
);
781 if (c
== NULL
) c
= "-unknown-";
782 switch(array
.level
) {
783 default: /* raid0, linear, multipath cannot be reconfigured */
784 fprintf(stderr
, Name
": %s array %s cannot be reshaped.\n",
789 case LEVEL_FAULTY
: /* only 'layout' change is permitted */
791 if (chunksize
|| raid_disks
) {
792 fprintf(stderr
, Name
": %s: Cannot change chunksize or disks of a 'faulty' array\n",
797 if (layout_str
== NULL
)
798 break; /* nothing to do.... */
800 array
.layout
= parse_layout_faulty(layout_str
);
801 if (array
.layout
< 0) {
802 fprintf(stderr
, Name
": %s: layout %s not understood for 'faulty' array\n",
803 devname
, layout_str
);
807 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
808 fprintf(stderr
, Name
": Cannot set layout for %s: %s\n",
809 devname
, strerror(errno
));
812 printf("layout for %s set to %d\n", devname
, array
.layout
);
815 case 1: /* only raid_disks can each be changed. */
817 if (chunksize
|| layout_str
!= NULL
) {
818 fprintf(stderr
, Name
": %s: Cannot change chunk size or layout for a RAID1 array.\n",
823 if (raid_disks
> 0) {
824 array
.raid_disks
= raid_disks
;
825 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
826 fprintf(stderr
, Name
": Cannot set raid-devices for %s: %s\n",
827 devname
, strerror(errno
));
838 * layout/chunksize/raid_disks can be changed
839 * though the kernel may not support it all.
841 st
= super_by_fd(fd
);
843 if (raid_disks
> st
->max_devs
) {
844 fprintf(stderr
, Name
": Cannot increase raid-disks on "
845 "this array beyond %d\n", st
->max_devs
);
851 * There are three possibilities.
852 * 1/ The array will shrink.
853 * We need to ensure the reshape will pause before reaching
854 * the 'critical section'. We also need to fork and wait for
855 * that to happen. When it does we
856 * suspend/backup/complete/unfreeze
858 * 2/ The array will not change size.
859 * This requires that we keep a backup of a sliding window
860 * so that we can restore data after a crash. So we need
861 * to fork and monitor progress.
863 * 3/ The array will grow. This is relatively easy.
864 * However the kernel's restripe routines will cheerfully
865 * overwrite some early data before it is safe. So we
866 * need to make a backup of the early parts of the array
867 * and be ready to restore it if rebuild aborts very early.
869 * We backup data by writing it to one spare, or to a
870 * file which was given on command line.
872 * [FOLLOWING IS OLD AND PARTLY WRONG]
873 * So: we enumerate the devices in the array and
874 * make sure we can open all of them.
875 * Then we freeze the early part of the array and
876 * backup to the various spares.
877 * Then we request changes and start the reshape.
878 * Monitor progress until it has passed the danger zone.
879 * and finally invalidate the copied data and unfreeze the
880 * start of the array.
882 * In each case, we first make sure that storage is available
883 * for the required backup.
885 * - request the shape change.
886 * - for to handle backup etc.
888 nchunk
= ochunk
= array
.chunk_size
;
889 nlayout
= olayout
= array
.layout
;
890 ndisks
= odisks
= array
.raid_disks
;
893 nchunk
= chunksize
* 1024;
894 if (size
% chunksize
) {
895 fprintf(stderr
, Name
": component size %lluK is not"
896 " a multiple of chunksize %dK\n",
901 if (layout_str
!= NULL
)
902 switch(array
.level
) {
903 case 4: /* ignore layout */
906 nlayout
= map_name(r5layout
, layout_str
);
907 if (nlayout
== UnSet
) {
908 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
916 nlayout
= map_name(r6layout
, layout_str
);
917 if (nlayout
== UnSet
) {
918 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
925 if (raid_disks
) ndisks
= raid_disks
;
929 if (array
.level
== 6) {
930 odata
--; /* number of data disks */
934 if (odata
== ndata
&&
935 get_linux_version() < 2006032) {
936 fprintf(stderr
, Name
": in-place reshape is not safe before 2.6.32, sorry.\n");
940 /* Check that we can hold all the data */
941 get_dev_size(fd
, NULL
, &array_size
);
942 if (ndata
* (unsigned long long)size
< (array_size
/1024)) {
943 fprintf(stderr
, Name
": this change will reduce the size of the array.\n"
944 " use --grow --array-size first to truncate array.\n"
945 " e.g. mdadm --grow %s --array-size %llu\n",
946 devname
, ndata
* size
);
951 /* So how much do we need to backup.
952 * We need an amount of data which is both a whole number of
953 * old stripes and a whole number of new stripes.
954 * So LCM for (chunksize*datadisks).
956 a
= (ochunk
/512) * odata
;
957 b
= (nchunk
/512) * ndata
;
965 /* LCM == product / GCD */
966 blocks
= (ochunk
/512) * (nchunk
/512) * odata
* ndata
/ a
;
969 sra
= sysfs_read(fd
, 0,
970 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
974 fprintf(stderr
, Name
": %s: Cannot get array details from sysfs\n",
980 if (ndata
== odata
) {
981 /* Make 'blocks' bigger for better throughput, but
982 * not so big that we reject it below.
983 * Try for 16 megabytes
985 while (blocks
* 32 < sra
->component_size
&&
989 fprintf(stderr
, Name
": Need to backup %luK of critical "
990 "section..\n", blocks
/2);
992 if (blocks
>= sra
->component_size
/2) {
993 fprintf(stderr
, Name
": %s: Something wrong - reshape aborted\n",
998 nrdisks
= array
.raid_disks
+ sra
->array
.spare_disks
;
999 /* Now we need to open all these devices so we can read/write.
1001 fdlist
= malloc((1+nrdisks
) * sizeof(int));
1002 offsets
= malloc((1+nrdisks
) * sizeof(offsets
[0]));
1003 if (!fdlist
|| !offsets
) {
1004 fprintf(stderr
, Name
": malloc failed: grow aborted\n");
1008 for (d
=0; d
<= nrdisks
; d
++)
1010 d
= array
.raid_disks
;
1011 for (sd
= sra
->devs
; sd
; sd
=sd
->next
) {
1012 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1014 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
1015 char *dn
= map_dev(sd
->disk
.major
,
1017 fdlist
[sd
->disk
.raid_disk
]
1018 = dev_open(dn
, O_RDONLY
);
1019 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
1020 if (fdlist
[sd
->disk
.raid_disk
] < 0) {
1021 fprintf(stderr
, Name
": %s: cannot open component %s\n",
1022 devname
, dn
?dn
:"-unknown-");
1026 } else if (backup_file
== NULL
) {
1028 char *dn
= map_dev(sd
->disk
.major
,
1030 fdlist
[d
] = dev_open(dn
, O_RDWR
);
1031 offsets
[d
] = (sd
->data_offset
+ sra
->component_size
- blocks
- 8)*512;
1033 fprintf(stderr
, Name
": %s: cannot open component %s\n",
1034 devname
, dn
?dn
:"-unknown");
1041 if (backup_file
== NULL
) {
1042 if (ndata
<= odata
) {
1043 fprintf(stderr
, Name
": %s: Cannot grow - need backup-file\n",
1047 } else if (sra
->array
.spare_disks
== 0) {
1048 fprintf(stderr
, Name
": %s: Cannot grow - need a spare or "
1049 "backup-file to backup critical section\n",
1054 if (d
== array
.raid_disks
) {
1055 fprintf(stderr
, Name
": %s: No spare device for backup\n",
1061 /* need to check backup file is large enough */
1065 fdlist
[d
] = open(backup_file
, O_RDWR
|O_CREAT
|O_EXCL
,
1067 offsets
[d
] = 8 * 512;
1068 if (fdlist
[d
] < 0) {
1069 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1070 devname
, backup_file
, strerror(errno
));
1074 /* Guard against backup file being on array device.
1075 * If array is partitioned or if LVM etc is in the
1076 * way this will not notice, but it is better than
1079 fstat(fdlist
[d
], &stb
);
1082 if (stb
.st_rdev
== dev
) {
1083 fprintf(stderr
, Name
": backup file must NOT be"
1084 " on the array being reshaped.\n");
1090 memset(buf
, 0, 512);
1091 for (i
=0; i
< (signed)blocks
+ 8 ; i
++) {
1092 if (write(fdlist
[d
], buf
, 512) != 512) {
1093 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1094 devname
, backup_file
, strerror(errno
));
1099 if (fsync(fdlist
[d
]) != 0) {
1100 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1101 devname
, backup_file
, strerror(errno
));
1108 /* lastly, check that the internal stripe cache is
1109 * large enough, or it won't work.
1112 cache
= (nchunk
< ochunk
) ? ochunk
: nchunk
;
1113 cache
= cache
* 4 / 4096;
1114 if (cache
< blocks
/ 8 / odisks
+ 16)
1115 /* Make it big enough to hold 'blocks' */
1116 cache
= blocks
/ 8 / odisks
+ 16;
1117 if (sra
->cache_size
< cache
)
1118 sysfs_set_num(sra
, NULL
, "stripe_cache_size",
1120 /* Right, everything seems fine. Let's kick things off.
1121 * If only changing raid_disks, use ioctl, else use
1124 if (ochunk
== nchunk
&& olayout
== nlayout
) {
1125 array
.raid_disks
= ndisks
;
1126 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
1129 fprintf(stderr
, Name
": Cannot set device shape for %s: %s\n",
1130 devname
, strerror(errno
));
1131 if (ndisks
< odisks
&&
1132 get_linux_version() < 2006030)
1133 fprintf(stderr
, Name
": linux 2.6.30 or later required\n");
1135 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1136 fprintf(stderr
, " Bitmap must be removed before shape can be changed\n");
1141 /* set them all just in case some old 'new_*' value
1142 * persists from some earlier problem
1144 int err
= err
; /* only used if rv==1, and always set if
1145 * rv==1, so initialisation not needed,
1146 * despite gcc warning
1148 if (sysfs_set_num(sra
, NULL
, "chunk_size", nchunk
) < 0)
1149 rv
= 1, err
= errno
;
1150 if (!rv
&& sysfs_set_num(sra
, NULL
, "layout", nlayout
) < 0)
1151 rv
= 1, err
= errno
;
1152 if (!rv
&& sysfs_set_num(sra
, NULL
, "raid_disks", ndisks
) < 0)
1153 rv
= 1, err
= errno
;
1155 fprintf(stderr
, Name
": Cannot set device shape for %s\n",
1157 if (get_linux_version() < 2006030)
1158 fprintf(stderr
, Name
": linux 2.6.30 or later required\n");
1160 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1161 fprintf(stderr
, " Bitmap must be removed before shape can be changed\n");
1166 if (ndisks
== 2 && odisks
== 2) {
1167 /* No reshape is needed in this trivial case */
1172 /* set up the backup-super-block. This requires the
1173 * uuid from the array.
1175 /* Find a superblock */
1176 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1180 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1182 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
1183 devfd
= dev_open(dn
, O_RDONLY
);
1186 ok
= st
->ss
->load_super(st
, devfd
, NULL
);
1192 fprintf(stderr
, Name
": %s: Cannot find a superblock\n",
1198 memset(&bsb
, 0, 512);
1199 memcpy(bsb
.magic
, "md_backup_data-1", 16);
1200 st
->ss
->uuid_from_super(st
, (int*)&bsb
.set_uuid
);
1201 bsb
.mtime
= __cpu_to_le64(time(0));
1202 bsb
.devstart2
= blocks
;
1203 stripes
= blocks
/ (ochunk
/512) / odata
;
1204 /* Now we just need to kick off the reshape and watch, while
1205 * handling backups of the data...
1206 * This is all done by a forked background process.
1211 if (check_env("MDADM_GROW_VERIFY"))
1212 fd
= open(devname
, O_RDONLY
| O_DIRECT
);
1215 mlockall(MCL_FUTURE
);
1218 done
= child_grow(fd
, sra
, stripes
,
1220 odisks
, ochunk
, array
.level
, olayout
, odata
,
1221 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1222 else if (odata
> ndata
)
1223 done
= child_shrink(fd
, sra
, stripes
,
1225 odisks
, ochunk
, array
.level
, olayout
, odata
,
1226 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1228 done
= child_same_size(fd
, sra
, stripes
,
1231 odisks
, ochunk
, array
.level
, olayout
, odata
,
1232 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1233 if (backup_file
&& done
)
1234 unlink(backup_file
);
1235 if (level
!= UnSet
&& level
!= array
.level
) {
1236 /* We need to wait for the reshape to finish
1237 * (which will have happened unless odata < ndata)
1238 * and then set the level
1241 c
= map_num(pers
, level
);
1243 exit(0);/* not possible */
1247 err
= sysfs_set_str(sra
, NULL
, "level", c
);
1249 fprintf(stderr
, Name
": %s: could not set level to %s\n",
1254 fprintf(stderr
, Name
": Cannot run child to monitor reshape: %s\n",
1259 /* The child will take care of unfreezing the array */
1268 if (rv
&& orig_level
!= UnSet
&& sra
) {
1269 c
= map_num(pers
, orig_level
);
1270 if (c
&& sysfs_set_str(sra
, NULL
, "level", c
) == 0)
1271 fprintf(stderr
, Name
": aborting level change\n");
1274 unfreeze_array(sra
, frozen
);
1279 * We run a child process in the background which performs the following
1281 * - wait for resync to reach a certain point
1282 * - suspend io to the following section
1283 * - backup that section
1284 * - allow resync to proceed further
1286 * - discard the backup.
1288 * When are combined in slightly different ways in the three cases.
1290 * - suspend/backup/allow/wait/resume/discard
1292 * - allow/wait/suspend/backup/allow/wait/resume/discard
1294 * - wait/resume/discard/suspend/backup/allow
1296 * suspend/backup/allow always come together
1297 * wait/resume/discard do too.
1298 * For the same-size case we have two backups to improve flow.
1302 /* FIXME return status is never checked */
1303 int grow_backup(struct mdinfo
*sra
,
1304 unsigned long long offset
, /* per device */
1305 unsigned long stripes
, /* per device */
1306 int *sources
, unsigned long long *offsets
,
1307 int disks
, int chunk
, int level
, int layout
,
1308 int dests
, int *destfd
, unsigned long long *destoffsets
,
1309 int part
, int *degraded
,
1312 /* Backup 'blocks' sectors at 'offset' on each device of the array,
1313 * to storage 'destfd' (offset 'destoffsets'), after first
1314 * suspending IO. Then allow resync to continue
1315 * over the suspended section.
1316 * Use part 'part' of the backup-super-block.
1321 unsigned long long ll
;
1323 //printf("offset %llu\n", offset);
1328 sysfs_set_num(sra
, NULL
, "suspend_hi", (offset
+ stripes
* (chunk
/512)) * odata
);
1329 /* Check that array hasn't become degraded, else we might backup the wrong data */
1330 if (sysfs_get_ll(sra
, NULL
, "degraded", &ll
) < 0)
1331 return -1; /* FIXME this error is ignored */
1332 new_degraded
= (int)ll
;
1333 if (new_degraded
!= *degraded
) {
1334 /* check each device to ensure it is still working */
1336 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1337 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1339 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
1341 if (sysfs_get_str(sra
, sd
, "state", sbuf
, 20) < 0 ||
1342 strstr(sbuf
, "faulty") ||
1343 strstr(sbuf
, "in_sync") == NULL
) {
1344 /* this device is dead */
1345 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
1346 if (sd
->disk
.raid_disk
>= 0 &&
1347 sources
[sd
->disk
.raid_disk
] >= 0) {
1348 close(sources
[sd
->disk
.raid_disk
]);
1349 sources
[sd
->disk
.raid_disk
] = -1;
1354 *degraded
= new_degraded
;
1357 bsb
.arraystart2
= __cpu_to_le64(offset
* odata
);
1358 bsb
.length2
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
1360 bsb
.arraystart
= __cpu_to_le64(offset
* odata
);
1361 bsb
.length
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
1364 bsb
.magic
[15] = '2';
1365 for (i
= 0; i
< dests
; i
++)
1367 lseek64(destfd
[i
], destoffsets
[i
] + __le64_to_cpu(bsb
.devstart2
)*512, 0);
1369 lseek64(destfd
[i
], destoffsets
[i
], 0);
1371 rv
= save_stripes(sources
, offsets
,
1372 disks
, chunk
, level
, layout
,
1374 offset
*512*odata
, stripes
* chunk
* odata
,
1379 bsb
.mtime
= __cpu_to_le64(time(0));
1380 for (i
= 0; i
< dests
; i
++) {
1381 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
1383 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
1384 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
1385 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
1386 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
1389 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
] - 4096, 0)
1390 != destoffsets
[i
] - 4096)
1392 if (write(destfd
[i
], &bsb
, 512) != 512)
1394 if (destoffsets
[i
] > 4096) {
1395 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]+stripes
*chunk
*odata
, 0) !=
1396 destoffsets
[i
]+stripes
*chunk
*odata
)
1398 if (write(destfd
[i
], &bsb
, 512) != 512)
1408 /* in 2.6.30, the value reported by sync_completed can be
1409 * less that it should be by one stripe.
1410 * This only happens when reshape hits sync_max and pauses.
1411 * So allow wait_backup to either extent sync_max further
1412 * than strictly necessary, or return before the
1413 * sync has got quite as far as we would really like.
1414 * This is what 'blocks2' is for.
1415 * The various caller give appropriate values so that
1418 /* FIXME return value is often ignored */
1419 int wait_backup(struct mdinfo
*sra
,
1420 unsigned long long offset
, /* per device */
1421 unsigned long long blocks
, /* per device */
1422 unsigned long long blocks2
, /* per device - hack */
1423 int dests
, int *destfd
, unsigned long long *destoffsets
,
1426 /* Wait for resync to pass the section that was backed up
1427 * then erase the backup and allow IO
1429 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
1430 unsigned long long completed
;
1436 sysfs_set_num(sra
, NULL
, "sync_max", offset
+ blocks
+ blocks2
);
1438 sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
1444 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
1445 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
1449 if (sysfs_get_str(sra
, NULL
, "sync_action",
1451 strncmp(action
, "reshape", 7) != 0)
1453 } while (completed
< offset
+ blocks
);
1457 bsb
.arraystart2
= __cpu_to_le64(0);
1458 bsb
.length2
= __cpu_to_le64(0);
1460 bsb
.arraystart
= __cpu_to_le64(0);
1461 bsb
.length
= __cpu_to_le64(0);
1463 bsb
.mtime
= __cpu_to_le64(time(0));
1465 for (i
= 0; i
< dests
; i
++) {
1466 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
1467 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
1468 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
1469 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
1470 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
1471 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]-4096, 0) !=
1472 destoffsets
[i
]-4096)
1475 write(destfd
[i
], &bsb
, 512) != 512)
1482 static void fail(char *msg
)
1485 rv
= (write(2, msg
, strlen(msg
)) != (int)strlen(msg
));
1486 rv
|= (write(2, "\n", 1) != 1);
1490 static char *abuf
, *bbuf
;
1491 static unsigned long long abuflen
;
1492 static void validate(int afd
, int bfd
, unsigned long long offset
)
1494 /* check that the data in the backup against the array.
1495 * This is only used for regression testing and should not
1496 * be used while the array is active
1500 lseek64(bfd
, offset
- 4096, 0);
1501 if (read(bfd
, &bsb2
, 512) != 512)
1502 fail("cannot read bsb");
1503 if (bsb2
.sb_csum
!= bsb_csum((char*)&bsb2
,
1504 ((char*)&bsb2
.sb_csum
)-((char*)&bsb2
)))
1505 fail("first csum bad");
1506 if (memcmp(bsb2
.magic
, "md_backup_data", 14) != 0)
1507 fail("magic is bad");
1508 if (memcmp(bsb2
.magic
, "md_backup_data-2", 16) == 0 &&
1509 bsb2
.sb_csum2
!= bsb_csum((char*)&bsb2
,
1510 ((char*)&bsb2
.sb_csum2
)-((char*)&bsb2
)))
1511 fail("second csum bad");
1513 if (__le64_to_cpu(bsb2
.devstart
)*512 != offset
)
1514 fail("devstart is wrong");
1517 unsigned long long len
= __le64_to_cpu(bsb2
.length
)*512;
1519 if (abuflen
< len
) {
1523 if (posix_memalign((void**)&abuf
, 4096, abuflen
) ||
1524 posix_memalign((void**)&bbuf
, 4096, abuflen
)) {
1526 /* just stop validating on mem-alloc failure */
1531 lseek64(bfd
, offset
, 0);
1532 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
) {
1533 //printf("len %llu\n", len);
1534 fail("read first backup failed");
1536 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart
)*512, 0);
1537 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
1538 fail("read first from array failed");
1539 if (memcmp(bbuf
, abuf
, len
) != 0) {
1542 printf("offset=%llu len=%llu\n",
1543 (unsigned long long)__le64_to_cpu(bsb2
.arraystart
)*512, len
);
1544 for (i
=0; i
<len
; i
++)
1545 if (bbuf
[i
] != abuf
[i
]) {
1546 printf("first diff byte %d\n", i
);
1550 fail("data1 compare failed");
1554 unsigned long long len
= __le64_to_cpu(bsb2
.length2
)*512;
1556 if (abuflen
< len
) {
1560 abuf
= malloc(abuflen
);
1561 bbuf
= malloc(abuflen
);
1564 lseek64(bfd
, offset
+__le64_to_cpu(bsb2
.devstart2
)*512, 0);
1565 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
)
1566 fail("read second backup failed");
1567 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart2
)*512, 0);
1568 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
1569 fail("read second from array failed");
1570 if (memcmp(bbuf
, abuf
, len
) != 0)
1571 fail("data2 compare failed");
1575 static int child_grow(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1576 int *fds
, unsigned long long *offsets
,
1577 int disks
, int chunk
, int level
, int layout
, int data
,
1578 int dests
, int *destfd
, unsigned long long *destoffsets
)
1583 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
1584 /* Don't start the 'reshape' */
1586 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1587 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1588 grow_backup(sra
, 0, stripes
,
1589 fds
, offsets
, disks
, chunk
, level
, layout
,
1590 dests
, destfd
, destoffsets
,
1592 validate(afd
, destfd
[0], destoffsets
[0]);
1593 wait_backup(sra
, 0, stripes
* (chunk
/ 512), stripes
* (chunk
/ 512),
1594 dests
, destfd
, destoffsets
,
1596 sysfs_set_num(sra
, NULL
, "suspend_lo", (stripes
* (chunk
/512)) * data
);
1598 /* FIXME this should probably be numeric */
1599 sysfs_set_str(sra
, NULL
, "sync_max", "max");
1603 static int child_shrink(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1604 int *fds
, unsigned long long *offsets
,
1605 int disks
, int chunk
, int level
, int layout
, int data
,
1606 int dests
, int *destfd
, unsigned long long *destoffsets
)
1609 unsigned long long start
;
1613 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
1615 start
= sra
->component_size
- stripes
* (chunk
/512);
1616 sysfs_set_num(sra
, NULL
, "sync_max", start
);
1617 sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
1618 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1619 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1620 rv
= wait_backup(sra
, 0, start
- stripes
* (chunk
/512), stripes
* (chunk
/512),
1621 dests
, destfd
, destoffsets
, 0);
1624 grow_backup(sra
, 0, stripes
,
1626 disks
, chunk
, level
, layout
,
1627 dests
, destfd
, destoffsets
,
1629 validate(afd
, destfd
[0], destoffsets
[0]);
1630 wait_backup(sra
, start
, stripes
*(chunk
/512), 0,
1631 dests
, destfd
, destoffsets
, 0);
1632 sysfs_set_num(sra
, NULL
, "suspend_lo", (stripes
* (chunk
/512)) * data
);
1634 /* FIXME this should probably be numeric */
1635 sysfs_set_str(sra
, NULL
, "sync_max", "max");
1639 static int child_same_size(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1640 int *fds
, unsigned long long *offsets
,
1641 unsigned long long start
,
1642 int disks
, int chunk
, int level
, int layout
, int data
,
1643 int dests
, int *destfd
, unsigned long long *destoffsets
)
1645 unsigned long long size
;
1646 unsigned long tailstripes
= stripes
;
1649 unsigned long long speed
;
1653 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
1656 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1657 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1659 sysfs_get_ll(sra
, NULL
, "sync_speed_min", &speed
);
1660 sysfs_set_num(sra
, NULL
, "sync_speed_min", 200000);
1662 grow_backup(sra
, start
, stripes
,
1664 disks
, chunk
, level
, layout
,
1665 dests
, destfd
, destoffsets
,
1667 grow_backup(sra
, (start
+ stripes
) * (chunk
/512), stripes
,
1669 disks
, chunk
, level
, layout
,
1670 dests
, destfd
, destoffsets
,
1672 validate(afd
, destfd
[0], destoffsets
[0]);
1674 start
+= stripes
* 2; /* where to read next */
1675 size
= sra
->component_size
/ (chunk
/512);
1676 while (start
< size
) {
1677 if (wait_backup(sra
, (start
-stripes
*2)*(chunk
/512),
1678 stripes
*(chunk
/512), 0,
1679 dests
, destfd
, destoffsets
,
1682 sysfs_set_num(sra
, NULL
, "suspend_lo", start
*(chunk
/512) * data
);
1683 if (start
+ stripes
> size
)
1684 tailstripes
= (size
- start
);
1686 grow_backup(sra
, start
*(chunk
/512), tailstripes
,
1688 disks
, chunk
, level
, layout
,
1689 dests
, destfd
, destoffsets
,
1690 part
, °raded
, buf
);
1693 validate(afd
, destfd
[0], destoffsets
[0]);
1695 if (wait_backup(sra
, (start
-stripes
*2) * (chunk
/512), stripes
* (chunk
/512), 0,
1696 dests
, destfd
, destoffsets
,
1699 sysfs_set_num(sra
, NULL
, "suspend_lo", ((start
-stripes
)*(chunk
/512)) * data
);
1700 wait_backup(sra
, (start
-stripes
) * (chunk
/512), tailstripes
* (chunk
/512), 0,
1701 dests
, destfd
, destoffsets
,
1703 sysfs_set_num(sra
, NULL
, "suspend_lo", (size
*(chunk
/512)) * data
);
1704 sysfs_set_num(sra
, NULL
, "sync_speed_min", speed
);
1710 * If any spare contains md_back_data-1 which is recent wrt mtime,
1711 * write that data into the array and update the super blocks with
1712 * the new reshape_progress
1714 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
, int cnt
,
1715 char *backup_file
, int verbose
)
1719 unsigned long long *offsets
;
1720 unsigned long long nstripe
, ostripe
;
1723 if (info
->new_level
!= info
->array
.level
)
1724 return 1; /* Cannot handle level changes (they are instantaneous) */
1726 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
1727 if (info
->array
.level
== 6) odata
--; /* number of data disks */
1728 ndata
= info
->array
.raid_disks
- 1;
1729 if (info
->new_level
== 6) ndata
--;
1731 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
1733 if (info
->delta_disks
<= 0)
1734 /* Didn't grow, so the backup file must have
1738 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
1739 struct mdinfo dinfo
;
1742 char *devname
, namebuf
[20];
1744 /* This was a spare and may have some saved data on it.
1745 * Load the superblock, find and load the
1746 * backup_super_block.
1747 * If either fail, go on to next device.
1748 * If the backup contains no new info, just return
1749 * else restore data and update all superblocks
1751 if (i
== old_disks
-1) {
1752 fd
= open(backup_file
, O_RDONLY
);
1754 fprintf(stderr
, Name
": backup file %s inaccessible: %s\n",
1755 backup_file
, strerror(errno
));
1758 devname
= backup_file
;
1763 if (st
->ss
->load_super(st
, fd
, NULL
))
1766 st
->ss
->getinfo_super(st
, &dinfo
);
1767 st
->ss
->free_super(st
);
1770 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
1772 fprintf(stderr
, Name
": Cannot seek on device %d\n", i
);
1773 continue; /* Cannot seek */
1775 sprintf(namebuf
, "device-%d", i
);
1778 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
)) {
1780 fprintf(stderr
, Name
": Cannot read from %s\n", devname
);
1781 continue; /* Cannot read */
1783 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0 &&
1784 memcmp(bsb
.magic
, "md_backup_data-2", 16) != 0) {
1786 fprintf(stderr
, Name
": No backup metadata on %s\n", devname
);
1789 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
))) {
1791 fprintf(stderr
, Name
": Bad backup-metadata checksum on %s\n", devname
);
1792 continue; /* bad checksum */
1794 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0 &&
1795 bsb
.sb_csum2
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum2
)-((char*)&bsb
))) {
1797 fprintf(stderr
, Name
": Bad backup-metadata checksum2 on %s\n", devname
);
1798 continue; /* Bad second checksum */
1800 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0) {
1802 fprintf(stderr
, Name
": Wrong uuid on backup-metadata on %s\n", devname
);
1803 continue; /* Wrong uuid */
1806 /* array utime and backup-mtime should be updated at much the same time, but it seems that
1807 * sometimes they aren't... So allow considerable flexability in matching, and allow
1808 * this test to be overridden by an environment variable.
1810 if (info
->array
.utime
> (int)__le64_to_cpu(bsb
.mtime
) + 2*60*60 ||
1811 info
->array
.utime
< (int)__le64_to_cpu(bsb
.mtime
) - 10*60) {
1812 if (check_env("MDADM_GROW_ALLOW_OLD")) {
1813 fprintf(stderr
, Name
": accepting backup with timestamp %lu "
1814 "for array with timestamp %lu\n",
1815 (unsigned long)__le64_to_cpu(bsb
.mtime
),
1816 (unsigned long)info
->array
.utime
);
1819 fprintf(stderr
, Name
": too-old timestamp on "
1820 "backup-metadata on %s\n", devname
);
1821 continue; /* time stamp is too bad */
1825 if (bsb
.magic
[15] == '1') {
1826 if (info
->delta_disks
>= 0) {
1827 /* reshape_progress is increasing */
1828 if (__le64_to_cpu(bsb
.arraystart
) + __le64_to_cpu(bsb
.length
) <
1829 info
->reshape_progress
) {
1832 fprintf(stderr
, Name
": backup-metadata found on %s but is not needed\n", devname
);
1833 continue; /* No new data here */
1836 /* reshape_progress is decreasing */
1837 if (__le64_to_cpu(bsb
.arraystart
) >=
1838 info
->reshape_progress
)
1839 goto nonew
; /* No new data here */
1842 if (info
->delta_disks
>= 0) {
1843 /* reshape_progress is increasing */
1844 if (__le64_to_cpu(bsb
.arraystart
) + __le64_to_cpu(bsb
.length
) <
1845 info
->reshape_progress
&&
1846 __le64_to_cpu(bsb
.arraystart2
) + __le64_to_cpu(bsb
.length2
) <
1847 info
->reshape_progress
)
1848 goto nonew
; /* No new data here */
1850 /* reshape_progress is decreasing */
1851 if (__le64_to_cpu(bsb
.arraystart
) >=
1852 info
->reshape_progress
&&
1853 __le64_to_cpu(bsb
.arraystart2
) >=
1854 info
->reshape_progress
)
1855 goto nonew
; /* No new data here */
1858 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0) {
1861 fprintf(stderr
, Name
": Failed to verify secondary backup-metadata block on %s\n",
1863 continue; /* Cannot seek */
1865 /* There should be a duplicate backup superblock 4k before here */
1866 if (lseek64(fd
, -4096, 1) < 0 ||
1867 read(fd
, &bsb2
, sizeof(bsb2
)) != sizeof(bsb2
))
1868 goto second_fail
; /* Cannot find leading superblock */
1869 if (bsb
.magic
[15] == '1')
1870 bsbsize
= offsetof(struct mdp_backup_super
, pad1
);
1872 bsbsize
= offsetof(struct mdp_backup_super
, pad
);
1873 if (memcmp(&bsb2
, &bsb
, bsbsize
) != 0)
1874 goto second_fail
; /* Cannot find leading superblock */
1876 /* Now need the data offsets for all devices. */
1877 offsets
= malloc(sizeof(*offsets
)*info
->array
.raid_disks
);
1878 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
1881 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
1882 /* FIXME should be this be an error */
1884 st
->ss
->getinfo_super(st
, &dinfo
);
1885 st
->ss
->free_super(st
);
1886 offsets
[j
] = dinfo
.data_offset
* 512;
1888 printf(Name
": restoring critical section\n");
1890 if (restore_stripes(fdlist
, offsets
,
1891 info
->array
.raid_disks
,
1895 fd
, __le64_to_cpu(bsb
.devstart
)*512,
1896 __le64_to_cpu(bsb
.arraystart
)*512,
1897 __le64_to_cpu(bsb
.length
)*512)) {
1898 /* didn't succeed, so giveup */
1900 fprintf(stderr
, Name
": Error restoring backup from %s\n",
1905 if (bsb
.magic
[15] == '2' &&
1906 restore_stripes(fdlist
, offsets
,
1907 info
->array
.raid_disks
,
1911 fd
, __le64_to_cpu(bsb
.devstart
)*512 +
1912 __le64_to_cpu(bsb
.devstart2
)*512,
1913 __le64_to_cpu(bsb
.arraystart2
)*512,
1914 __le64_to_cpu(bsb
.length2
)*512)) {
1915 /* didn't succeed, so giveup */
1917 fprintf(stderr
, Name
": Error restoring second backup from %s\n",
1923 /* Ok, so the data is restored. Let's update those superblocks. */
1925 if (info
->delta_disks
>= 0) {
1926 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
) +
1927 __le64_to_cpu(bsb
.length
);
1928 if (bsb
.magic
[15] == '2') {
1929 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
) +
1930 __le64_to_cpu(bsb
.length2
);
1931 if (p2
> info
->reshape_progress
)
1932 info
->reshape_progress
= p2
;
1935 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
);
1936 if (bsb
.magic
[15] == '2') {
1937 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
);
1938 if (p2
< info
->reshape_progress
)
1939 info
->reshape_progress
= p2
;
1942 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
1943 if (fdlist
[j
] < 0) continue;
1944 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
1946 st
->ss
->getinfo_super(st
, &dinfo
);
1947 dinfo
.reshape_progress
= info
->reshape_progress
;
1948 st
->ss
->update_super(st
, &dinfo
,
1949 "_reshape_progress",
1951 st
->ss
->store_super(st
, fdlist
[j
]);
1952 st
->ss
->free_super(st
);
1956 /* Didn't find any backup data, try to see if any
1959 if (info
->delta_disks
< 0) {
1960 /* When shrinking, the critical section is at the end.
1961 * So see if we are before the critical section.
1963 unsigned long long first_block
;
1964 nstripe
= ostripe
= 0;
1966 while (ostripe
>= nstripe
) {
1967 ostripe
+= info
->array
.chunk_size
/ 512;
1968 first_block
= ostripe
* odata
;
1969 nstripe
= first_block
/ ndata
/ (info
->new_chunk
/512) *
1970 (info
->new_chunk
/512);
1973 if (info
->reshape_progress
>= first_block
)
1976 if (info
->delta_disks
> 0) {
1977 /* See if we are beyond the critical section. */
1978 unsigned long long last_block
;
1979 nstripe
= ostripe
= 0;
1981 while (nstripe
>= ostripe
) {
1982 nstripe
+= info
->new_chunk
/ 512;
1983 last_block
= nstripe
* ndata
;
1984 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
1985 (info
->array
.chunk_size
/512);
1988 if (info
->reshape_progress
>= last_block
)
1991 /* needed to recover critical section! */
1993 fprintf(stderr
, Name
": Failed to find backup of critical section\n");
1997 int Grow_continue(int mdfd
, struct supertype
*st
, struct mdinfo
*info
,
2000 /* Array is assembled and ready to be started, but
2001 * monitoring is probably required.
2004 * - set upper bound for resync
2005 * - initialise the 'suspend' boundaries
2006 * - switch to read-write
2007 * - fork and continue monitoring
2011 unsigned long long backup_offsets
[1];
2012 int odisks
, ndisks
, ochunk
, nchunk
,odata
,ndata
;
2013 unsigned long a
,b
,blocks
,stripes
;
2016 unsigned long long *offsets
;
2018 struct mdinfo
*sra
, *sd
;
2020 unsigned long cache
;
2023 err
= sysfs_set_str(info
, NULL
, "array_state", "readonly");
2027 /* make sure reshape doesn't progress until we are ready */
2028 sysfs_set_str(info
, NULL
, "sync_max", "0");
2029 sysfs_set_str(info
, NULL
, "array_state", "active"); /* FIXME or clean */
2031 sra
= sysfs_read(-1, devname2devnum(info
->sys_name
),
2032 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
2037 /* ndisks is not growing, so raid_disks is old and +delta is new */
2038 odisks
= info
->array
.raid_disks
;
2039 ndisks
= odisks
+ info
->delta_disks
;
2042 if (info
->array
.level
== 6) {
2046 ochunk
= info
->array
.chunk_size
;
2047 nchunk
= info
->new_chunk
;
2049 a
= (ochunk
/512) * odata
;
2050 b
= (nchunk
/512) * ndata
;
2058 /* LCM == product / GCD */
2059 blocks
= (ochunk
/512) * (nchunk
/512) * odata
* ndata
/ a
;
2062 while (blocks
* 32 < sra
->component_size
&&
2065 stripes
= blocks
/ (info
->array
.chunk_size
/512) / odata
;
2067 /* check that the internal stripe cache is
2068 * large enough, or it won't work.
2070 cache
= (nchunk
< ochunk
) ? ochunk
: nchunk
;
2071 cache
= cache
* 4 / 4096;
2072 if (cache
< blocks
/ 8 / odisks
+ 16)
2073 /* Make it big enough to hold 'blocks' */
2074 cache
= blocks
/ 8 / odisks
+ 16;
2075 if (sra
->cache_size
< cache
)
2076 sysfs_set_num(sra
, NULL
, "stripe_cache_size",
2079 memset(&bsb
, 0, 512);
2080 memcpy(bsb
.magic
, "md_backup_data-1", 16);
2081 memcpy(&bsb
.set_uuid
, info
->uuid
, 16);
2082 bsb
.mtime
= __cpu_to_le64(time(0));
2083 bsb
.devstart2
= blocks
;
2085 backup_fd
= open(backup_file
, O_RDWR
|O_CREAT
, S_IRUSR
| S_IWUSR
);
2086 backup_list
[0] = backup_fd
;
2087 backup_offsets
[0] = 8 * 512;
2088 fds
= malloc(odisks
* sizeof(fds
[0]));
2089 offsets
= malloc(odisks
* sizeof(offsets
[0]));
2090 for (d
=0; d
<odisks
; d
++)
2093 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2094 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2096 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
2097 char *dn
= map_dev(sd
->disk
.major
,
2099 fds
[sd
->disk
.raid_disk
]
2100 = dev_open(dn
, O_RDONLY
);
2101 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
2102 if (fds
[sd
->disk
.raid_disk
] < 0) {
2103 fprintf(stderr
, Name
": %s: cannot open component %s\n",
2104 info
->sys_name
, dn
?dn
:"-unknown-");
2115 mlockall(MCL_FUTURE
);
2116 if (info
->delta_disks
< 0)
2117 done
= child_shrink(-1, info
, stripes
,
2119 info
->array
.raid_disks
,
2120 info
->array
.chunk_size
,
2121 info
->array
.level
, info
->array
.layout
,
2123 1, backup_list
, backup_offsets
);
2124 else if (info
->delta_disks
== 0) {
2125 /* The 'start' is a per-device stripe number.
2126 * reshape_progress is a per-array sector number.
2127 * So divide by ndata * chunk_size
2129 unsigned long long start
= info
->reshape_progress
/ ndata
;
2130 start
/= (info
->array
.chunk_size
/512);
2131 done
= child_same_size(-1, info
, stripes
,
2134 info
->array
.raid_disks
,
2135 info
->array
.chunk_size
,
2136 info
->array
.level
, info
->array
.layout
,
2138 1, backup_list
, backup_offsets
);
2140 if (backup_file
&& done
)
2141 unlink(backup_file
);
2142 /* FIXME should I intuit a level change */
2145 fprintf(stderr
, Name
": Cannot run child to continue monitoring reshape: %s\n",