2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Email: <neilb@suse.de>
28 #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
29 #error no endian defined
35 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
38 int Grow_Add_device(char *devname
, int fd
, char *newdev
)
40 /* Add a device to an active array.
41 * Currently, just extend a linear array.
42 * This requires writing a new superblock on the
43 * new device, calling the kernel to add the device,
44 * and if that succeeds, update the superblock on
46 * This means that we need to *find* all other devices.
53 struct supertype
*st
= NULL
;
56 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
57 fprintf(stderr
, Name
": cannot get array info for %s\n", devname
);
63 fprintf(stderr
, Name
": cannot handle arrays with superblock version %d\n", info
.array
.major_version
);
67 if (info
.array
.level
!= -1) {
68 fprintf(stderr
, Name
": can only add devices to linear arrays\n");
72 nfd
= open(newdev
, O_RDWR
|O_EXCL
|O_DIRECT
);
74 fprintf(stderr
, Name
": cannot open %s\n", newdev
);
78 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
79 fprintf(stderr
, Name
": %s is not a block device!\n", newdev
);
83 /* now check out all the devices and make sure we can read the superblock */
84 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
89 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
90 fprintf(stderr
, Name
": cannot get device detail for device %d\n",
94 dv
= map_dev(disk
.major
, disk
.minor
, 1);
96 fprintf(stderr
, Name
": cannot find device file for device %d\n",
100 fd2
= dev_open(dv
, O_RDWR
);
102 fprintf(stderr
, Name
": cannot open device file %s\n", dv
);
105 st
->ss
->free_super(st
);
107 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
108 fprintf(stderr
, Name
": cannot find super block on %s\n", dv
);
114 /* Ok, looks good. Lets update the superblock and write it out to
118 info
.disk
.number
= d
;
119 info
.disk
.major
= major(stb
.st_rdev
);
120 info
.disk
.minor
= minor(stb
.st_rdev
);
121 info
.disk
.raid_disk
= d
;
122 info
.disk
.state
= (1 << MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
);
123 st
->ss
->update_super(st
, &info
, "linear-grow-new", newdev
,
126 if (st
->ss
->store_super(st
, nfd
)) {
127 fprintf(stderr
, Name
": Cannot store new superblock on %s\n",
134 if (ioctl(fd
, ADD_NEW_DISK
, &info
.disk
) != 0) {
135 fprintf(stderr
, Name
": Cannot add new disk to this array\n");
138 /* Well, that seems to have worked.
139 * Now go through and update all superblocks
142 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
143 fprintf(stderr
, Name
": cannot get array info for %s\n", devname
);
148 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
149 mdu_disk_info_t disk
;
153 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
154 fprintf(stderr
, Name
": cannot get device detail for device %d\n",
158 dv
= map_dev(disk
.major
, disk
.minor
, 1);
160 fprintf(stderr
, Name
": cannot find device file for device %d\n",
164 fd2
= dev_open(dv
, O_RDWR
);
166 fprintf(stderr
, Name
": cannot open device file %s\n", dv
);
169 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
170 fprintf(stderr
, Name
": cannot find super block on %s\n", dv
);
174 info
.array
.raid_disks
= nd
+1;
175 info
.array
.nr_disks
= nd
+1;
176 info
.array
.active_disks
= nd
+1;
177 info
.array
.working_disks
= nd
+1;
179 st
->ss
->update_super(st
, &info
, "linear-grow-update", dv
,
182 if (st
->ss
->store_super(st
, fd2
)) {
183 fprintf(stderr
, Name
": Cannot store new superblock on %s\n", dv
);
193 int Grow_addbitmap(char *devname
, int fd
, char *file
, int chunk
, int delay
, int write_behind
, int force
)
196 * First check that array doesn't have a bitmap
197 * Then create the bitmap
200 * For internal bitmaps, we need to check the version,
201 * find all the active devices, and write the bitmap block
204 mdu_bitmap_file_t bmf
;
205 mdu_array_info_t array
;
206 struct supertype
*st
;
207 int major
= BITMAP_MAJOR_HI
;
208 int vers
= md_get_version(fd
);
209 unsigned long long bitmapsize
, array_size
;
212 major
= BITMAP_MAJOR_HOSTENDIAN
;
214 fprintf(stderr
, Name
": Warning - bitmaps created on this kernel are not portable\n"
215 " between different architectured. Consider upgrading the Linux kernel.\n");
219 if (ioctl(fd
, GET_BITMAP_FILE
, &bmf
) != 0) {
221 fprintf(stderr
, Name
": Memory allocation failure.\n");
223 fprintf(stderr
, Name
": bitmaps not supported by this kernel.\n");
226 if (bmf
.pathname
[0]) {
227 if (strcmp(file
,"none")==0) {
228 if (ioctl(fd
, SET_BITMAP_FILE
, -1)!= 0) {
229 fprintf(stderr
, Name
": failed to remove bitmap %s\n",
235 fprintf(stderr
, Name
": %s already has a bitmap (%s)\n",
236 devname
, bmf
.pathname
);
239 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
240 fprintf(stderr
, Name
": cannot get array status for %s\n", devname
);
243 if (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)) {
244 if (strcmp(file
, "none")==0) {
245 array
.state
&= ~(1<<MD_SB_BITMAP_PRESENT
);
246 if (ioctl(fd
, SET_ARRAY_INFO
, &array
)!= 0) {
247 fprintf(stderr
, Name
": failed to remove internal bitmap.\n");
252 fprintf(stderr
, Name
": Internal bitmap already present on %s\n",
256 if (array
.level
<= 0) {
257 fprintf(stderr
, Name
": Bitmaps not meaningful with level %s\n",
258 map_num(pers
, array
.level
)?:"of this array");
261 bitmapsize
= array
.size
;
263 if (get_dev_size(fd
, NULL
, &array_size
) &&
264 array_size
> (0x7fffffffULL
<<9)) {
265 /* Array is big enough that we cannot trust array.size
266 * try other approaches
268 bitmapsize
= get_component_size(fd
);
270 if (bitmapsize
== 0) {
271 fprintf(stderr
, Name
": Cannot reliably determine size of array to create bitmap - sorry.\n");
275 if (array
.level
== 10) {
276 int ncopies
= (array
.layout
&255)*((array
.layout
>>8)&255);
277 bitmapsize
= bitmapsize
* array
.raid_disks
/ ncopies
;
280 st
= super_by_fd(fd
);
282 fprintf(stderr
, Name
": Cannot understand version %d.%d\n",
283 array
.major_version
, array
.minor_version
);
286 if (strcmp(file
, "none") == 0) {
287 fprintf(stderr
, Name
": no bitmap found on %s\n", devname
);
289 } else if (strcmp(file
, "internal") == 0) {
291 for (d
=0; d
< st
->max_devs
; d
++) {
292 mdu_disk_info_t disk
;
295 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
297 if (disk
.major
== 0 &&
300 if ((disk
.state
& (1<<MD_DISK_SYNC
))==0)
302 dv
= map_dev(disk
.major
, disk
.minor
, 1);
304 int fd2
= dev_open(dv
, O_RDWR
);
307 if (st
->ss
->load_super(st
, fd2
, NULL
)==0) {
308 if (st
->ss
->add_internal_bitmap(
310 &chunk
, delay
, write_behind
,
311 bitmapsize
, 0, major
)
313 st
->ss
->write_bitmap(st
, fd2
);
315 fprintf(stderr
, Name
": failed to create internal bitmap - chunksize problem.\n");
323 array
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
324 if (ioctl(fd
, SET_ARRAY_INFO
, &array
)!= 0) {
325 fprintf(stderr
, Name
": failed to set internal bitmap.\n");
332 int max_devs
= st
->max_devs
;
334 /* try to load a superblock */
335 for (d
=0; d
<max_devs
; d
++) {
336 mdu_disk_info_t disk
;
340 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
342 if ((disk
.major
==0 && disk
.minor
==0) ||
343 (disk
.state
& (1<<MD_DISK_REMOVED
)))
345 dv
= map_dev(disk
.major
, disk
.minor
, 1);
347 fd2
= dev_open(dv
, O_RDONLY
);
349 st
->ss
->load_super(st
, fd2
, NULL
) == 0) {
351 st
->ss
->uuid_from_super(st
, uuid
);
357 fprintf(stderr
, Name
": cannot find UUID for array!\n");
360 if (CreateBitmap(file
, force
, (char*)uuid
, chunk
,
361 delay
, write_behind
, bitmapsize
, major
)) {
364 bitmap_fd
= open(file
, O_RDWR
);
366 fprintf(stderr
, Name
": weird: %s cannot be opened\n",
370 if (ioctl(fd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
371 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
372 devname
, strerror(errno
));
382 * When reshaping an array we might need to backup some data.
383 * This is written to all spares with a 'super_block' describing it.
384 * The superblock goes 4K from the end of the used space on the
386 * It if written after the backup is complete.
387 * It has the following structure.
390 static struct mdp_backup_super
{
391 char magic
[16]; /* md_backup_data-1 or -2 */
394 /* start/sizes in 512byte sectors */
395 __u64 devstart
; /* address on backup device/file of data */
398 __u32 sb_csum
; /* csum of preceeding bytes. */
400 __u64 devstart2
; /* offset in to data of second section */
403 __u32 sb_csum2
; /* csum of preceeding bytes. */
405 } __attribute__((aligned(512))) bsb
, bsb2
;
407 int bsb_csum(char *buf
, int len
)
411 for (i
=0; i
<len
; i
++)
412 csum
= (csum
<<3) + buf
[0];
413 return __cpu_to_le32(csum
);
416 static int child_grow(int afd
, struct mdinfo
*sra
, unsigned long blocks
,
417 int *fds
, unsigned long long *offsets
,
418 int disks
, int chunk
, int level
, int layout
, int data
,
419 int dests
, int *destfd
, unsigned long long *destoffsets
);
420 static int child_shrink(int afd
, struct mdinfo
*sra
, unsigned long blocks
,
421 int *fds
, unsigned long long *offsets
,
422 int disks
, int chunk
, int level
, int layout
, int data
,
423 int dests
, int *destfd
, unsigned long long *destoffsets
);
424 static int child_same_size(int afd
, struct mdinfo
*sra
, unsigned long blocks
,
425 int *fds
, unsigned long long *offsets
,
426 unsigned long long start
,
427 int disks
, int chunk
, int level
, int layout
, int data
,
428 int dests
, int *destfd
, unsigned long long *destoffsets
);
430 int freeze_array(struct mdinfo
*sra
)
432 /* Try to freeze resync on this array.
433 * Return -1 if the array is busy,
434 * return 0 if this kernel doesn't support 'frozen'
435 * return 1 if it worked.
438 if (sysfs_get_str(sra
, NULL
, "sync_action", buf
, 20) <= 0)
440 if (strcmp(buf
, "idle\n") != 0 &&
441 strcmp(buf
, "frozen\n") != 0)
443 if (sysfs_set_str(sra
, NULL
, "sync_action", "frozen") < 0)
448 void unfreeze_array(struct mdinfo
*sra
, int frozen
)
450 /* If 'frozen' is 1, unfreeze the array */
452 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
455 void wait_reshape(struct mdinfo
*sra
)
457 int fd
= sysfs_get_fd(sra
, NULL
, "sync_action");
464 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
466 if (sysfs_fd_get_str(fd
, action
, 20) < 0) {
470 } while (strncmp(action
, "reshape", 7) == 0);
474 int Grow_reshape(char *devname
, int fd
, int quiet
, char *backup_file
,
476 int level
, char *layout_str
, int chunksize
, int raid_disks
)
478 /* Make some changes in the shape of an array.
479 * The kernel must support the change.
481 * There are three different changes. Each can trigger
482 * a resync or recovery so we freeze that until we have
483 * requested everything (if kernel supports freezing - 2.6.30).
485 * - change size (i.e. component_size)
487 * - change layout/chunksize/ndisks
489 * The last can require a reshape. It is different on different
490 * levels so we need to check the level before actioning it.
491 * Some times the level change needs to be requested after the
492 * reshape (e.g. raid6->raid5, raid5->raid0)
495 struct mdu_array_info_s array
, orig
;
498 struct supertype
*st
;
501 int nlayout
, olayout
;
504 int orig_level
= UnSet
;
507 unsigned long long *offsets
;
512 unsigned long a
,b
, blocks
, stripes
;
514 unsigned long long array_size
;
521 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) < 0) {
522 fprintf(stderr
, Name
": %s is not an active md array - aborting\n",
528 (chunksize
|| level
!= UnSet
|| layout_str
|| raid_disks
)) {
529 fprintf(stderr
, Name
": cannot change component size at the same time "
530 "as other changes.\n"
531 " Change size first, then check data is intact before "
532 "making other changes.\n");
536 if (raid_disks
&& raid_disks
< array
.raid_disks
&& array
.level
> 1 &&
537 get_linux_version() < 2006032 &&
538 !check_env("MDADM_FORCE_FEWER")) {
539 fprintf(stderr
, Name
": reducing the number of devices is not safe before Linux 2.6.32\n"
540 " Please use a newer kernel\n");
543 sra
= sysfs_read(fd
, 0, GET_LEVEL
);
544 frozen
= freeze_array(sra
);
546 fprintf(stderr
, Name
": %s is performing resync/recovery and cannot"
547 " be reshaped\n", devname
);
551 /* ========= set size =============== */
552 if (size
>= 0 && (size
== 0 || size
!= array
.size
)) {
554 if (array
.size
!= size
) {
555 /* got truncated to 32bit, write to
556 * component_size instead
559 rv
= sysfs_set_num(sra
, NULL
,
560 "component_size", size
);
564 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
566 fprintf(stderr
, Name
": Cannot set device size for %s: %s\n",
567 devname
, strerror(errno
));
571 ioctl(fd
, GET_ARRAY_INFO
, &array
);
572 size
= get_component_size(fd
);
576 fprintf(stderr
, Name
": component size of %s has been set to %lluK\n",
580 size
= get_component_size(fd
);
585 /* ======= set level =========== */
586 if (level
!= UnSet
&& level
!= array
.level
) {
587 /* Trying to change the level.
588 * We might need to change layout first and schedule a
589 * level change for later.
590 * Level changes that can happen immediately are:
591 * 0->4,5,6 1->5 4->5,6 5->1,6
592 * Level changes that need a layout change first are:
593 * 6->5,4,0 : need a -6 layout, or parity-last
594 * 5->4,0 : need parity-last
596 if ((array
.level
== 6 || array
.level
== 5) &&
597 (level
== 5 || level
== 4 || level
== 0)) {
598 /* Don't change level yet, but choose intermediate
602 if (layout_str
== NULL
)
603 switch (array
.layout
) {
604 case ALGORITHM_LEFT_ASYMMETRIC
:
605 case ALGORITHM_LEFT_ASYMMETRIC_6
:
606 case ALGORITHM_ROTATING_N_RESTART
:
607 layout_str
= "left-asymmetric-6";
609 case ALGORITHM_LEFT_SYMMETRIC
:
610 case ALGORITHM_LEFT_SYMMETRIC_6
:
611 case ALGORITHM_ROTATING_N_CONTINUE
:
612 layout_str
= "left-symmetric-6";
614 case ALGORITHM_RIGHT_ASYMMETRIC
:
615 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
616 case ALGORITHM_ROTATING_ZERO_RESTART
:
617 layout_str
= "right-asymmetric-6";
619 case ALGORITHM_RIGHT_SYMMETRIC
:
620 case ALGORITHM_RIGHT_SYMMETRIC_6
:
621 layout_str
= "right-symmetric-6";
623 case ALGORITHM_PARITY_0
:
624 case ALGORITHM_PARITY_0_6
:
625 layout_str
= "parity-first-6";
627 case ALGORITHM_PARITY_N
:
628 layout_str
= "parity-last";
631 fprintf(stderr
, Name
": %s: cannot"
632 "convert layout to RAID5 equivalent\n",
638 int l
= map_name(r5layout
, layout_str
);
640 fprintf(stderr
, Name
": %s: layout '%s' not recognised\n",
641 devname
, layout_str
);
645 if (l
!= ALGORITHM_PARITY_N
) {
646 /* need the -6 version */
647 char *ls
= map_num(r5layout
, l
);
648 strcat(strcpy(alt_layout
, ls
),
650 layout_str
= alt_layout
;
654 /* The final raid6->raid5 conversion
655 * will reduce the number of disks,
656 * so now we need to aim higher
660 layout_str
= "parity-last";
662 c
= map_num(pers
, level
);
664 rv
= 1;/* not possible */
667 err
= sysfs_set_str(sra
, NULL
, "level", c
);
669 fprintf(stderr
, Name
": %s: could not set level to %s\n",
675 orig_level
= orig
.level
;
676 ioctl(fd
, GET_ARRAY_INFO
, &array
);
677 if (layout_str
== NULL
&&
678 orig
.level
== 5 && level
== 6 &&
679 array
.layout
!= orig
.layout
)
680 layout_str
= map_num(r5layout
, orig
.layout
);
682 fprintf(stderr
, Name
" level of %s changed to %s\n",
688 /* ========= set shape (chunk_size / layout / ndisks) ============== */
689 /* Check if layout change is a no-op */
690 if (layout_str
) switch(array
.level
) {
692 if (array
.layout
== map_name(r5layout
, layout_str
))
696 if (layout_str
== NULL
&&
697 ((chunksize
&& chunksize
* 1024 != array
.chunk_size
) ||
698 (raid_disks
&& raid_disks
!= array
.raid_disks
)) &&
699 array
.layout
>= 16) {
701 ": %s has a non-standard layout. If you wish to preserve this\n"
702 " during the reshape, please specify --layout=preserve\n"
703 " If you want to change it, specify a layout or use --layout=normalise\n",
708 if (strcmp(layout_str
, "normalise") == 0 ||
709 strcmp(layout_str
, "normalize") == 0) {
711 strcpy(alt_layout
, map_num(r6layout
, array
.layout
));
712 hyphen
= strrchr(alt_layout
, '-');
713 if (hyphen
&& strcmp(hyphen
, "-6") == 0) {
715 layout_str
= alt_layout
;
719 if (array
.layout
== map_name(r6layout
, layout_str
))
721 if (layout_str
&& strcmp(layout_str
, "preserve") == 0)
725 if (layout_str
== NULL
726 && (chunksize
== 0 || chunksize
*1024 == array
.chunk_size
)
727 && (raid_disks
== 0 || raid_disks
== array
.raid_disks
)) {
729 if (level
!= UnSet
&& level
!= array
.level
) {
730 /* Looks like this level change doesn't need
731 * a reshape after all.
733 c
= map_num(pers
, level
);
735 rv
= sysfs_set_str(sra
, NULL
, "level", c
);
737 fprintf(stderr
, Name
": %s: could not set level to %s\n",
740 } else if (!changed
&& !quiet
)
741 fprintf(stderr
, Name
": %s: no change requested\n",
746 c
= map_num(pers
, array
.level
);
747 if (c
== NULL
) c
= "-unknown-";
748 switch(array
.level
) {
749 default: /* raid0, linear, multipath cannot be reconfigured */
750 fprintf(stderr
, Name
": %s array %s cannot be reshaped.\n",
755 case LEVEL_FAULTY
: /* only 'layout' change is permitted */
757 if (chunksize
|| raid_disks
) {
758 fprintf(stderr
, Name
": %s: Cannot change chunksize or disks of a 'faulty' array\n",
763 if (layout_str
== NULL
)
764 break; /* nothing to do.... */
766 array
.layout
= parse_layout_faulty(layout_str
);
767 if (array
.layout
< 0) {
769 fprintf(stderr
, Name
": %s: layout %s not understood for 'faulty' array\n",
770 devname
, layout_str
);
774 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
775 fprintf(stderr
, Name
": Cannot set layout for %s: %s\n",
776 devname
, strerror(errno
));
779 printf("layout for %s set to %d\n", devname
, array
.layout
);
782 case 1: /* only raid_disks can each be changed. */
784 if (chunksize
|| layout_str
!= NULL
) {
785 fprintf(stderr
, Name
": %s: Cannot change chunk size or layout for a RAID1 array.\n",
790 if (raid_disks
> 0) {
791 array
.raid_disks
= raid_disks
;
792 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
793 fprintf(stderr
, Name
": Cannot set raid-devices for %s: %s\n",
794 devname
, strerror(errno
));
805 * layout/chunksize/raid_disks can be changed
806 * though the kernel may not support it all.
808 st
= super_by_fd(fd
);
811 * There are three possibilities.
812 * 1/ The array will shrink.
813 * We need to ensure the reshape will pause before reaching
814 * the 'critical section'. We also need to fork and wait for
815 * that to happen. When it does we
816 * suspend/backup/complete/unfreeze
818 * 2/ The array will not change size.
819 * This requires that we keep a backup of a sliding window
820 * so that we can restore data after a crash. So we need
821 * to fork and monitor progress.
823 * 3/ The array will grow. This is relatively easy.
824 * However the kernel's restripe routines will cheerfully
825 * overwrite some early data before it is safe. So we
826 * need to make a backup of the early parts of the array
827 * and be ready to restore it if rebuild aborts very early.
829 * We backup data by writing it to one spare, or to a
830 * file which was given on command line.
832 * [FOLLOWING IS OLD AND PARTLY WRONG]
833 * So: we enumerate the devices in the array and
834 * make sure we can open all of them.
835 * Then we freeze the early part of the array and
836 * backup to the various spares.
837 * Then we request changes and start the reshape.
838 * Monitor progress until it has passed the danger zone.
839 * and finally invalidate the copied data and unfreeze the
840 * start of the array.
842 * In each case, we first make sure that storage is available
843 * for the required backup.
845 * - request the shape change.
846 * - for to handle backup etc.
848 nchunk
= ochunk
= array
.chunk_size
;
849 nlayout
= olayout
= array
.layout
;
850 ndisks
= odisks
= array
.raid_disks
;
853 nchunk
= chunksize
* 1024;
854 if (size
% chunksize
) {
855 fprintf(stderr
, Name
": component size %lluK is not"
856 " a multiple of chunksize %dK\n",
861 if (layout_str
!= NULL
)
862 switch(array
.level
) {
863 case 4: /* ignore layout */
866 nlayout
= map_name(r5layout
, layout_str
);
867 if (nlayout
== UnSet
) {
868 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
876 nlayout
= map_name(r6layout
, layout_str
);
877 if (nlayout
== UnSet
) {
878 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
885 if (raid_disks
) ndisks
= raid_disks
;
889 if (array
.level
== 6) {
890 odata
--; /* number of data disks */
894 if (odata
== ndata
&&
895 get_linux_version() < 2006032) {
896 fprintf(stderr
, Name
": in-place reshape is not safe before 2.6.32, sorry.\n");
900 /* Check that we can hold all the data */
901 get_dev_size(fd
, NULL
, &array_size
);
902 if (ndata
* size
< (array_size
/1024)) {
903 fprintf(stderr
, Name
": this change will reduce the size of the array.\n"
904 " use --grow --array-size first to truncate array.\n"
905 " e.g. mdadm --grow %s --array-size %llu\n",
906 devname
, ndata
* size
);
911 /* So how much do we need to backup.
912 * We need an amount of data which is both a whole number of
913 * old stripes and a whole number of new stripes.
914 * So LCM for (chunksize*datadisks).
916 a
= ochunk
/512 * odata
;
917 b
= nchunk
/512 * ndata
;
925 /* LCM == product / GCD */
926 blocks
= ochunk
/512 * nchunk
/512 * odata
* ndata
/ a
;
929 sra
= sysfs_read(fd
, 0,
930 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
933 if (ndata
== odata
) {
934 /* Make 'blocks' bigger for better throughput, but
935 * not so big that we reject it below.
936 * Try for 16 megabytes
938 while (blocks
* 32 < sra
->component_size
&&
942 fprintf(stderr
, Name
": Need to backup %luK of critical "
943 "section..\n", blocks
/2);
946 fprintf(stderr
, Name
": %s: Cannot get array details from sysfs\n",
952 if (blocks
>= sra
->component_size
/2) {
953 fprintf(stderr
, Name
": %s: Something wrong - reshape aborted\n",
958 nrdisks
= array
.nr_disks
+ sra
->array
.spare_disks
;
959 /* Now we need to open all these devices so we can read/write.
961 fdlist
= malloc((1+nrdisks
) * sizeof(int));
962 offsets
= malloc((1+nrdisks
) * sizeof(offsets
[0]));
963 if (!fdlist
|| !offsets
) {
964 fprintf(stderr
, Name
": malloc failed: grow aborted\n");
968 for (d
=0; d
<= nrdisks
; d
++)
970 d
= array
.raid_disks
;
971 for (sd
= sra
->devs
; sd
; sd
=sd
->next
) {
972 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
974 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
975 char *dn
= map_dev(sd
->disk
.major
,
977 fdlist
[sd
->disk
.raid_disk
]
978 = dev_open(dn
, O_RDONLY
);
979 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
980 if (fdlist
[sd
->disk
.raid_disk
] < 0) {
981 fprintf(stderr
, Name
": %s: cannot open component %s\n",
982 devname
, dn
?dn
:"-unknown-");
986 } else if (backup_file
== NULL
) {
988 char *dn
= map_dev(sd
->disk
.major
,
990 fdlist
[d
] = dev_open(dn
, O_RDWR
);
991 offsets
[d
] = (sd
->data_offset
+ sra
->component_size
- blocks
- 8)*512;
993 fprintf(stderr
, Name
": %s: cannot open component %s\n",
994 devname
, dn
?dn
:"-unknown");
1001 if (backup_file
== NULL
) {
1002 if (ndata
<= odata
) {
1003 fprintf(stderr
, Name
": %s: Cannot grow - need backup-file\n",
1007 } else if (sra
->array
.spare_disks
== 0) {
1008 fprintf(stderr
, Name
": %s: Cannot grow - need a spare or "
1009 "backup-file to backup critical section\n",
1014 if (d
== array
.raid_disks
) {
1015 fprintf(stderr
, Name
": %s: No spare device for backup\n",
1021 /* need to check backup file is large enough */
1023 fdlist
[d
] = open(backup_file
, O_RDWR
|O_CREAT
|O_EXCL
,
1025 offsets
[d
] = 8 * 512;
1026 if (fdlist
[d
] < 0) {
1027 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1028 devname
, backup_file
, strerror(errno
));
1032 memset(buf
, 0, 512);
1033 for (i
=0; i
< blocks
+ 1 ; i
++) {
1034 if (write(fdlist
[d
], buf
, 512) != 512) {
1035 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1036 devname
, backup_file
, strerror(errno
));
1041 if (fsync(fdlist
[d
]) != 0) {
1042 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1043 devname
, backup_file
, strerror(errno
));
1050 /* lastly, check that the internal stripe cache is
1051 * large enough, or it won't work.
1054 cache
= (nchunk
< ochunk
) ? ochunk
: nchunk
;
1055 cache
= cache
* 4 / 4096;
1056 if (cache
< blocks
/ 8 / odisks
+ 16)
1057 /* Make it big enough to hold 'blocks' */
1058 cache
= blocks
/ 8 / odisks
+ 16;
1059 if (sra
->cache_size
< cache
)
1060 sysfs_set_num(sra
, NULL
, "stripe_cache_size",
1062 /* Right, everything seems fine. Let's kick things off.
1063 * If only changing raid_disks, use ioctl, else use
1066 if (ochunk
== nchunk
&& olayout
== nlayout
) {
1067 array
.raid_disks
= ndisks
;
1068 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
1070 fprintf(stderr
, Name
": Cannot set device shape for %s: %s\n",
1071 devname
, strerror(errno
));
1072 if (ndisks
< odisks
&&
1073 get_linux_version() < 2006030)
1074 fprintf(stderr
, Name
": linux 2.6.30 or later required\n");
1079 /* set them all just in case some old 'new_*' value
1080 * persists from some earlier problem
1082 if (sysfs_set_num(sra
, NULL
, "chunk_size", nchunk
) < 0)
1084 if (sysfs_set_num(sra
, NULL
, "layout", nlayout
) < 0)
1086 if (sysfs_set_num(sra
, NULL
, "raid_disks", ndisks
) < 0)
1089 fprintf(stderr
, Name
": Cannot set device shape for %s\n",
1091 if (get_linux_version() < 2006030)
1092 fprintf(stderr
, Name
": linux 2.6.30 or later required\n");
1097 if (ndisks
== 2 && odisks
== 2) {
1098 /* No reshape is needed in this trivial case */
1103 /* set up the backup-super-block. This requires the
1104 * uuid from the array.
1106 /* Find a superblock */
1107 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1111 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1113 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
1114 devfd
= dev_open(dn
, O_RDONLY
);
1117 ok
= st
->ss
->load_super(st
, devfd
, NULL
);
1123 fprintf(stderr
, Name
": %s: Cannot find a superblock\n",
1129 memset(&bsb
, 0, 512);
1130 memcpy(bsb
.magic
, "md_backup_data-1", 16);
1131 st
->ss
->uuid_from_super(st
, (int*)&bsb
.set_uuid
);
1132 bsb
.mtime
= __cpu_to_le64(time(0));
1133 bsb
.devstart2
= blocks
;
1134 stripes
= blocks
/ (ochunk
/512) / odata
;
1135 /* Now we just need to kick off the reshape and watch, while
1136 * handling backups of the data...
1137 * This is all done by a forked background process.
1142 if (check_env("MDADM_GROW_VERIFY"))
1143 fd
= open(devname
, O_RDONLY
| O_DIRECT
);
1146 mlockall(MCL_FUTURE
);
1149 done
= child_grow(fd
, sra
, stripes
,
1151 odisks
, ochunk
, array
.level
, olayout
, odata
,
1152 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1153 else if (odata
> ndata
)
1154 done
= child_shrink(fd
, sra
, stripes
,
1156 odisks
, ochunk
, array
.level
, olayout
, odata
,
1157 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1159 done
= child_same_size(fd
, sra
, stripes
,
1162 odisks
, ochunk
, array
.level
, olayout
, odata
,
1163 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1164 if (backup_file
&& done
)
1165 unlink(backup_file
);
1166 if (level
!= UnSet
&& level
!= array
.level
) {
1167 /* We need to wait for the reshape to finish
1168 * (which will have happened unless odata < ndata)
1169 * and then set the level
1172 c
= map_num(pers
, level
);
1174 exit(0);/* not possible */
1178 err
= sysfs_set_str(sra
, NULL
, "level", c
);
1180 fprintf(stderr
, Name
": %s: could not set level to %s\n",
1185 fprintf(stderr
, Name
": Cannot run child to monitor reshape: %s\n",
1190 /* The child will take care of unfreezing the array */
1199 if (rv
&& orig_level
!= UnSet
&& sra
) {
1200 c
= map_num(pers
, orig_level
);
1201 if (c
&& sysfs_set_str(sra
, NULL
, "level", c
) == 0)
1202 fprintf(stderr
, Name
": aborting level change\n");
1205 unfreeze_array(sra
, frozen
);
1210 * We run a child process in the background which performs the following
1212 * - wait for resync to reach a certain point
1213 * - suspend io to the following section
1214 * - backup that section
1215 * - allow resync to proceed further
1217 * - discard the backup.
1219 * When are combined in slightly different ways in the three cases.
1221 * - suspend/backup/allow/wait/resume/discard
1223 * - allow/wait/suspend/backup/allow/wait/resume/discard
1225 * - wait/resume/discard/suspend/backup/allow
1227 * suspend/backup/allow always come together
1228 * wait/resume/discard do too.
1229 * For the same-size case we have two backups to improve flow.
1233 int grow_backup(struct mdinfo
*sra
,
1234 unsigned long long offset
, /* per device */
1235 unsigned long stripes
, /* per device */
1236 int *sources
, unsigned long long *offsets
,
1237 int disks
, int chunk
, int level
, int layout
,
1238 int dests
, int *destfd
, unsigned long long *destoffsets
,
1239 int part
, int *degraded
,
1242 /* Backup 'blocks' sectors at 'offset' on each device of the array,
1243 * to storage 'destfd' (offset 'destoffsets'), after first
1244 * suspending IO. Then allow resync to continue
1245 * over the suspended section.
1246 * Use part 'part' of the backup-super-block.
1251 unsigned long long new_degraded
;
1252 //printf("offset %llu\n", offset);
1257 sysfs_set_num(sra
, NULL
, "suspend_hi", (offset
+ stripes
* chunk
/512) * odata
);
1258 /* Check that array hasn't become degraded, else we might backup the wrong data */
1259 sysfs_get_ll(sra
, NULL
, "degraded", &new_degraded
);
1260 if (new_degraded
!= *degraded
) {
1261 /* check each device to ensure it is still working */
1263 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1264 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1266 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
1268 if (sysfs_get_str(sra
, sd
, "state", sbuf
, 20) < 0 ||
1269 strstr(sbuf
, "faulty") ||
1270 strstr(sbuf
, "in_sync") == NULL
) {
1271 /* this device is dead */
1272 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
1273 if (sd
->disk
.raid_disk
>= 0 &&
1274 sources
[sd
->disk
.raid_disk
] >= 0) {
1275 close(sources
[sd
->disk
.raid_disk
]);
1276 sources
[sd
->disk
.raid_disk
] = -1;
1281 *degraded
= new_degraded
;
1284 bsb
.arraystart2
= __cpu_to_le64(offset
* odata
);
1285 bsb
.length2
= __cpu_to_le64(stripes
* chunk
/512 * odata
);
1287 bsb
.arraystart
= __cpu_to_le64(offset
* odata
);
1288 bsb
.length
= __cpu_to_le64(stripes
* chunk
/512 * odata
);
1291 bsb
.magic
[15] = '2';
1292 for (i
= 0; i
< dests
; i
++)
1294 lseek64(destfd
[i
], destoffsets
[i
] + __le64_to_cpu(bsb
.devstart2
)*512, 0);
1296 lseek64(destfd
[i
], destoffsets
[i
], 0);
1298 rv
= save_stripes(sources
, offsets
,
1299 disks
, chunk
, level
, layout
,
1301 offset
*512*odata
, stripes
* chunk
* odata
,
1306 bsb
.mtime
= __cpu_to_le64(time(0));
1307 for (i
= 0; i
< dests
; i
++) {
1308 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
1310 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
1311 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
1312 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
1313 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
1315 lseek64(destfd
[i
], destoffsets
[i
] - 4096, 0);
1316 write(destfd
[i
], &bsb
, 512);
1317 if (destoffsets
[i
] > 4096) {
1318 lseek64(destfd
[i
], destoffsets
[i
]+stripes
*chunk
*odata
, 0);
1319 write(destfd
[i
], &bsb
, 512);
1327 /* in 2.6.30, the value reported by sync_completed can be
1328 * less that it should be by one stripe.
1329 * This only happens when reshape hits sync_max and pauses.
1330 * So allow wait_backup to either extent sync_max further
1331 * than strictly necessary, or return before the
1332 * sync has got quite as far as we would really like.
1333 * This is what 'blocks2' is for.
1334 * The various caller give appropriate values so that
1337 int wait_backup(struct mdinfo
*sra
,
1338 unsigned long long offset
, /* per device */
1339 unsigned long long blocks
, /* per device */
1340 unsigned long long blocks2
, /* per device - hack */
1341 int dests
, int *destfd
, unsigned long long *destoffsets
,
1344 /* Wait for resync to pass the section that was backed up
1345 * then erase the backup and allow IO
1347 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
1348 unsigned long long completed
;
1353 sysfs_set_num(sra
, NULL
, "sync_max", offset
+ blocks
+ blocks2
);
1355 sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
1361 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
1362 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
1366 if (sysfs_get_str(sra
, NULL
, "sync_action",
1368 strncmp(action
, "reshape", 7) != 0)
1370 } while (completed
< offset
+ blocks
);
1374 bsb
.arraystart2
= __cpu_to_le64(0);
1375 bsb
.length2
= __cpu_to_le64(0);
1377 bsb
.arraystart
= __cpu_to_le64(0);
1378 bsb
.length
= __cpu_to_le64(0);
1380 bsb
.mtime
= __cpu_to_le64(time(0));
1381 for (i
= 0; i
< dests
; i
++) {
1382 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
));
1387 lseek64(destfd
[i
], destoffsets
[i
]-4096, 0);
1388 write(destfd
[i
], &bsb
, 512);
1394 static void fail(char *msg
)
1396 write(2, msg
, strlen(msg
));
1401 static char *abuf
, *bbuf
;
1403 static void validate(int afd
, int bfd
, unsigned long long offset
)
1405 /* check that the data in the backup against the array.
1406 * This is only used for regression testing and should not
1407 * be used while the array is active
1411 lseek64(bfd
, offset
- 4096, 0);
1412 if (read(bfd
, &bsb2
, 512) != 512)
1413 fail("cannot read bsb");
1414 if (bsb2
.sb_csum
!= bsb_csum((char*)&bsb2
,
1415 ((char*)&bsb2
.sb_csum
)-((char*)&bsb2
)))
1416 fail("first csum bad");
1417 if (memcmp(bsb2
.magic
, "md_backup_data", 14) != 0)
1418 fail("magic is bad");
1419 if (memcmp(bsb2
.magic
, "md_backup_data-2", 16) == 0 &&
1420 bsb2
.sb_csum2
!= bsb_csum((char*)&bsb2
,
1421 ((char*)&bsb2
.sb_csum2
)-((char*)&bsb2
)))
1422 fail("second csum bad");
1424 if (__le64_to_cpu(bsb2
.devstart
)*512 != offset
)
1425 fail("devstart is wrong");
1428 unsigned long long len
= __le64_to_cpu(bsb2
.length
)*512;
1430 if (abuflen
< len
) {
1434 posix_memalign((void**)&abuf
, 4096, abuflen
);
1435 posix_memalign((void**)&bbuf
, 4096, abuflen
);
1438 lseek64(bfd
, offset
, 0);
1439 if (read(bfd
, bbuf
, len
) != len
) {
1440 printf("len %llu\n", len
);
1441 fail("read first backup failed");
1443 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart
)*512, 0);
1444 if (read(afd
, abuf
, len
) != len
)
1445 fail("read first from array failed");
1446 if (memcmp(bbuf
, abuf
, len
) != 0) {
1448 printf("offset=%llu len=%llu\n",
1449 __le64_to_cpu(bsb2
.arraystart
)*512, len
);
1450 for (i
=0; i
<len
; i
++)
1451 if (bbuf
[i
] != abuf
[i
]) {
1452 printf("first diff byte %d\n", i
);
1455 fail("data1 compare failed");
1459 unsigned long long len
= __le64_to_cpu(bsb2
.length2
)*512;
1461 if (abuflen
< len
) {
1465 abuf
= malloc(abuflen
);
1466 bbuf
= malloc(abuflen
);
1469 lseek64(bfd
, offset
+__le64_to_cpu(bsb2
.devstart2
)*512, 0);
1470 if (read(bfd
, bbuf
, len
) != len
)
1471 fail("read second backup failed");
1472 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart2
)*512, 0);
1473 if (read(afd
, abuf
, len
) != len
)
1474 fail("read second from array failed");
1475 if (memcmp(bbuf
, abuf
, len
) != 0)
1476 fail("data2 compare failed");
1480 static int child_grow(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1481 int *fds
, unsigned long long *offsets
,
1482 int disks
, int chunk
, int level
, int layout
, int data
,
1483 int dests
, int *destfd
, unsigned long long *destoffsets
)
1488 posix_memalign((void**)&buf
, 4096, disks
* chunk
);
1489 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1490 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1491 grow_backup(sra
, 0, stripes
,
1492 fds
, offsets
, disks
, chunk
, level
, layout
,
1493 dests
, destfd
, destoffsets
,
1495 validate(afd
, destfd
[0], destoffsets
[0]);
1496 wait_backup(sra
, 0, stripes
* chunk
/ 512, stripes
* chunk
/ 512,
1497 dests
, destfd
, destoffsets
,
1499 sysfs_set_num(sra
, NULL
, "suspend_lo", (stripes
* chunk
/512) * data
);
1501 /* FIXME this should probably be numeric */
1502 sysfs_set_str(sra
, NULL
, "sync_max", "max");
1506 static int child_shrink(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1507 int *fds
, unsigned long long *offsets
,
1508 int disks
, int chunk
, int level
, int layout
, int data
,
1509 int dests
, int *destfd
, unsigned long long *destoffsets
)
1512 unsigned long long start
;
1516 posix_memalign((void**)&buf
, 4096, disks
* chunk
);
1517 start
= sra
->component_size
- stripes
* chunk
/512;
1518 sysfs_set_num(sra
, NULL
, "sync_max", start
);
1519 sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
1520 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1521 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1522 rv
= wait_backup(sra
, 0, start
- stripes
* chunk
/512, stripes
* chunk
/512,
1523 dests
, destfd
, destoffsets
, 0);
1526 grow_backup(sra
, 0, stripes
,
1528 disks
, chunk
, level
, layout
,
1529 dests
, destfd
, destoffsets
,
1531 validate(afd
, destfd
[0], destoffsets
[0]);
1532 wait_backup(sra
, start
, stripes
*chunk
/512, 0,
1533 dests
, destfd
, destoffsets
, 0);
1534 sysfs_set_num(sra
, NULL
, "suspend_lo", (stripes
* chunk
/512) * data
);
1536 /* FIXME this should probably be numeric */
1537 sysfs_set_str(sra
, NULL
, "sync_max", "max");
1541 static int child_same_size(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1542 int *fds
, unsigned long long *offsets
,
1543 unsigned long long start
,
1544 int disks
, int chunk
, int level
, int layout
, int data
,
1545 int dests
, int *destfd
, unsigned long long *destoffsets
)
1547 unsigned long long size
;
1548 unsigned long tailstripes
= stripes
;
1551 unsigned long long speed
;
1555 posix_memalign((void**)&buf
, 4096, disks
* chunk
);
1557 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1558 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1560 sysfs_get_ll(sra
, NULL
, "sync_speed_min", &speed
);
1561 sysfs_set_num(sra
, NULL
, "sync_speed_min", 200000);
1563 grow_backup(sra
, start
, stripes
,
1565 disks
, chunk
, level
, layout
,
1566 dests
, destfd
, destoffsets
,
1568 grow_backup(sra
, (start
+ stripes
) * chunk
/512, stripes
,
1570 disks
, chunk
, level
, layout
,
1571 dests
, destfd
, destoffsets
,
1573 validate(afd
, destfd
[0], destoffsets
[0]);
1575 start
+= stripes
* 2; /* where to read next */
1576 size
= sra
->component_size
/ (chunk
/512);
1577 while (start
< size
) {
1578 if (wait_backup(sra
, (start
-stripes
*2)*chunk
/512,
1579 stripes
*chunk
/512, 0,
1580 dests
, destfd
, destoffsets
,
1583 sysfs_set_num(sra
, NULL
, "suspend_lo", start
*chunk
/512 * data
);
1584 if (start
+ stripes
> size
)
1585 tailstripes
= (size
- start
);
1587 grow_backup(sra
, start
*chunk
/512, tailstripes
,
1589 disks
, chunk
, level
, layout
,
1590 dests
, destfd
, destoffsets
,
1591 part
, °raded
, buf
);
1594 validate(afd
, destfd
[0], destoffsets
[0]);
1596 if (wait_backup(sra
, (start
-stripes
*2) * chunk
/512, stripes
* chunk
/512, 0,
1597 dests
, destfd
, destoffsets
,
1600 sysfs_set_num(sra
, NULL
, "suspend_lo", ((start
-stripes
)*chunk
/512) * data
);
1601 wait_backup(sra
, (start
-stripes
) * chunk
/512, tailstripes
* chunk
/512, 0,
1602 dests
, destfd
, destoffsets
,
1604 sysfs_set_num(sra
, NULL
, "suspend_lo", (size
*chunk
/512) * data
);
1605 sysfs_set_num(sra
, NULL
, "sync_speed_min", speed
);
1611 * If any spare contains md_back_data-1 which is recent wrt mtime,
1612 * write that data into the array and update the super blocks with
1613 * the new reshape_progress
1615 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
, int cnt
,
1616 char *backup_file
, int verbose
)
1620 unsigned long long *offsets
;
1621 unsigned long long nstripe
, ostripe
;
1624 if (info
->new_level
!= info
->array
.level
)
1625 return 1; /* Cannot handle level changes (they are instantaneous) */
1627 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
1628 if (info
->array
.level
== 6) odata
--; /* number of data disks */
1629 ndata
= info
->array
.raid_disks
- 1;
1630 if (info
->new_level
== 6) ndata
--;
1632 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
1634 if (info
->delta_disks
<= 0)
1635 /* Didn't grow, so the backup file must have
1639 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
1640 struct mdinfo dinfo
;
1643 char *devname
, namebuf
[20];
1645 /* This was a spare and may have some saved data on it.
1646 * Load the superblock, find and load the
1647 * backup_super_block.
1648 * If either fail, go on to next device.
1649 * If the backup contains no new info, just return
1650 * else restore data and update all superblocks
1652 if (i
== old_disks
-1) {
1653 fd
= open(backup_file
, O_RDONLY
);
1655 fprintf(stderr
, Name
": backup file %s inaccessible: %s\n",
1656 backup_file
, strerror(errno
));
1659 devname
= backup_file
;
1664 if (st
->ss
->load_super(st
, fd
, NULL
))
1667 st
->ss
->getinfo_super(st
, &dinfo
);
1668 st
->ss
->free_super(st
);
1671 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
1673 fprintf(stderr
, Name
": Cannot seek on device %d\n", i
);
1674 continue; /* Cannot seek */
1676 sprintf(namebuf
, "device-%d", i
);
1679 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
)) {
1681 fprintf(stderr
, Name
": Cannot read from %s\n", devname
);
1682 continue; /* Cannot read */
1684 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0 &&
1685 memcmp(bsb
.magic
, "md_backup_data-2", 16) != 0) {
1687 fprintf(stderr
, Name
": No backup metadata on %s\n", devname
);
1690 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
))) {
1692 fprintf(stderr
, Name
": Bad backup-metadata checksum on %s\n", devname
);
1693 continue; /* bad checksum */
1695 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0 &&
1696 bsb
.sb_csum2
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum2
)-((char*)&bsb
))) {
1698 fprintf(stderr
, Name
": Bad backup-metadata checksum2 on %s\n", devname
);
1699 continue; /* Bad second checksum */
1701 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0) {
1703 fprintf(stderr
, Name
": Wrong uuid on backup-metadata on %s\n", devname
);
1704 continue; /* Wrong uuid */
1707 if (info
->array
.utime
> __le64_to_cpu(bsb
.mtime
) + 10*60 ||
1708 info
->array
.utime
< __le64_to_cpu(bsb
.mtime
) - 10*60) {
1710 fprintf(stderr
, Name
": too-old timestamp on backup-metadata on %s\n", devname
);
1711 continue; /* time stamp is too bad */
1714 if (bsb
.magic
[15] == '1') {
1715 if (info
->delta_disks
>= 0) {
1716 /* reshape_progress is increasing */
1717 if (__le64_to_cpu(bsb
.arraystart
) + __le64_to_cpu(bsb
.length
) <
1718 info
->reshape_progress
) {
1721 fprintf(stderr
, Name
": backup-metadata found on %s but is not needed\n", devname
);
1722 continue; /* No new data here */
1725 /* reshape_progress is decreasing */
1726 if (__le64_to_cpu(bsb
.arraystart
) >=
1727 info
->reshape_progress
)
1728 goto nonew
; /* No new data here */
1731 if (info
->delta_disks
>= 0) {
1732 /* reshape_progress is increasing */
1733 if (__le64_to_cpu(bsb
.arraystart
) + __le64_to_cpu(bsb
.length
) <
1734 info
->reshape_progress
&&
1735 __le64_to_cpu(bsb
.arraystart2
) + __le64_to_cpu(bsb
.length2
) <
1736 info
->reshape_progress
)
1737 goto nonew
; /* No new data here */
1739 /* reshape_progress is decreasing */
1740 if (__le64_to_cpu(bsb
.arraystart
) >=
1741 info
->reshape_progress
&&
1742 __le64_to_cpu(bsb
.arraystart2
) >=
1743 info
->reshape_progress
)
1744 goto nonew
; /* No new data here */
1747 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0) {
1750 fprintf(stderr
, Name
": Failed to verify secondary backup-metadata block on %s\n",
1752 continue; /* Cannot seek */
1754 /* There should be a duplicate backup superblock 4k before here */
1755 if (lseek64(fd
, -4096, 1) < 0 ||
1756 read(fd
, &bsb2
, 4096) != 4096)
1757 goto second_fail
; /* Cannot find leading superblock */
1758 if (bsb
.magic
[15] == '1')
1759 bsbsize
= offsetof(struct mdp_backup_super
, pad1
);
1761 bsbsize
= offsetof(struct mdp_backup_super
, pad
);
1762 if (memcmp(&bsb2
, &bsb
, bsbsize
) != 0)
1763 goto second_fail
; /* Cannot find leading superblock */
1765 /* Now need the data offsets for all devices. */
1766 offsets
= malloc(sizeof(*offsets
)*info
->array
.raid_disks
);
1767 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
1770 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
1771 /* FIXME should be this be an error */
1773 st
->ss
->getinfo_super(st
, &dinfo
);
1774 st
->ss
->free_super(st
);
1775 offsets
[j
] = dinfo
.data_offset
* 512;
1777 printf(Name
": restoring critical section\n");
1779 if (restore_stripes(fdlist
, offsets
,
1780 info
->array
.raid_disks
,
1784 fd
, __le64_to_cpu(bsb
.devstart
)*512,
1785 __le64_to_cpu(bsb
.arraystart
)*512,
1786 __le64_to_cpu(bsb
.length
)*512)) {
1787 /* didn't succeed, so giveup */
1789 fprintf(stderr
, Name
": Error restoring backup from %s\n",
1794 if (bsb
.magic
[15] == '2' &&
1795 restore_stripes(fdlist
, offsets
,
1796 info
->array
.raid_disks
,
1800 fd
, __le64_to_cpu(bsb
.devstart
)*512 +
1801 __le64_to_cpu(bsb
.devstart2
)*512,
1802 __le64_to_cpu(bsb
.arraystart2
)*512,
1803 __le64_to_cpu(bsb
.length2
)*512)) {
1804 /* didn't succeed, so giveup */
1806 fprintf(stderr
, Name
": Error restoring second backup from %s\n",
1812 /* Ok, so the data is restored. Let's update those superblocks. */
1814 if (info
->delta_disks
>= 0) {
1815 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
) +
1816 __le64_to_cpu(bsb
.length
);
1817 if (bsb
.magic
[15] == '2') {
1818 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
) +
1819 __le64_to_cpu(bsb
.length2
);
1820 if (p2
> info
->reshape_progress
)
1821 info
->reshape_progress
= p2
;
1824 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
);
1825 if (bsb
.magic
[15] == '2') {
1826 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
);
1827 if (p2
< info
->reshape_progress
)
1828 info
->reshape_progress
= p2
;
1831 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
1832 if (fdlist
[j
] < 0) continue;
1833 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
1835 st
->ss
->getinfo_super(st
, &dinfo
);
1836 dinfo
.reshape_progress
= info
->reshape_progress
;
1837 st
->ss
->update_super(st
, &dinfo
,
1838 "_reshape_progress",
1840 st
->ss
->store_super(st
, fdlist
[j
]);
1841 st
->ss
->free_super(st
);
1845 /* Didn't find any backup data, try to see if any
1848 if (info
->delta_disks
< 0) {
1849 /* When shrinking, the critical section is at the end.
1850 * So see if we are before the critical section.
1852 unsigned long long first_block
;
1853 nstripe
= ostripe
= 0;
1855 while (ostripe
>= nstripe
) {
1856 ostripe
+= info
->array
.chunk_size
/ 512;
1857 first_block
= ostripe
* odata
;
1858 nstripe
= first_block
/ ndata
/ (info
->new_chunk
/512) *
1859 (info
->new_chunk
/512);
1862 if (info
->reshape_progress
>= first_block
)
1865 if (info
->delta_disks
> 0) {
1866 /* See if we are beyond the critical section. */
1867 unsigned long long last_block
;
1868 nstripe
= ostripe
= 0;
1870 while (nstripe
>= ostripe
) {
1871 nstripe
+= info
->new_chunk
/ 512;
1872 last_block
= nstripe
* ndata
;
1873 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
1874 (info
->array
.chunk_size
/512);
1877 if (info
->reshape_progress
>= last_block
)
1880 /* needed to recover critical section! */
1882 fprintf(stderr
, Name
": Failed to find backup of critical section\n");
1886 int Grow_continue(int mdfd
, struct supertype
*st
, struct mdinfo
*info
,
1889 /* Array is assembled and ready to be started, but
1890 * monitoring is probably required.
1893 * - set upper bound for resync
1894 * - initialise the 'suspend' boundaries
1895 * - switch to read-write
1896 * - fork and continue monitoring
1900 unsigned long long backup_offsets
[1];
1901 int odisks
, ndisks
, ochunk
, nchunk
,odata
,ndata
;
1902 unsigned long a
,b
,blocks
,stripes
;
1905 unsigned long long *offsets
;
1907 struct mdinfo
*sra
, *sd
;
1912 err
= sysfs_set_str(info
, NULL
, "array_state", "readonly");
1916 /* make sure reshape doesn't progress until we are ready */
1917 sysfs_set_str(info
, NULL
, "sync_max", "0");
1918 sysfs_set_str(info
, NULL
, "array_state", "active"); /* FIXME or clean */
1920 /* ndisks is not growing, so raid_disks is old and +delta is new */
1921 odisks
= info
->array
.raid_disks
;
1922 ndisks
= odisks
+ info
->delta_disks
;
1925 if (info
->array
.level
== 6) {
1929 ochunk
= info
->array
.chunk_size
;
1930 nchunk
= info
->new_chunk
;
1933 a
= ochunk
/512 * odata
;
1934 b
= nchunk
/512 * ndata
;
1942 /* LCM == product / GCD */
1943 blocks
= ochunk
/512 * nchunk
/512 * odata
* ndata
/ a
;
1945 sra
= sysfs_read(-1, devname2devnum(info
->sys_name
),
1946 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
1951 while (blocks
* 32 < sra
->component_size
&&
1954 stripes
= blocks
/ (info
->array
.chunk_size
/512) / odata
;
1956 /* check that the internal stripe cache is
1957 * large enough, or it won't work.
1959 cache
= (nchunk
< ochunk
) ? ochunk
: nchunk
;
1960 cache
= cache
* 4 / 4096;
1961 if (cache
< blocks
/ 8 / odisks
+ 16)
1962 /* Make it big enough to hold 'blocks' */
1963 cache
= blocks
/ 8 / odisks
+ 16;
1964 if (sra
->cache_size
< cache
)
1965 sysfs_set_num(sra
, NULL
, "stripe_cache_size",
1968 memset(&bsb
, 0, 512);
1969 memcpy(bsb
.magic
, "md_backup_data-1", 16);
1970 memcpy(&bsb
.set_uuid
, info
->uuid
, 16);
1971 bsb
.mtime
= __cpu_to_le64(time(0));
1972 bsb
.devstart2
= blocks
;
1974 backup_fd
= open(backup_file
, O_RDWR
|O_CREAT
, S_IRUSR
| S_IWUSR
);
1975 backup_list
[0] = backup_fd
;
1976 backup_offsets
[0] = 8 * 512;
1977 fds
= malloc(odisks
* sizeof(fds
[0]));
1978 offsets
= malloc(odisks
* sizeof(offsets
[0]));
1979 for (d
=0; d
<odisks
; d
++)
1982 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1983 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1985 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
1986 char *dn
= map_dev(sd
->disk
.major
,
1988 fds
[sd
->disk
.raid_disk
]
1989 = dev_open(dn
, O_RDONLY
);
1990 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
1991 if (fds
[sd
->disk
.raid_disk
] < 0) {
1992 fprintf(stderr
, Name
": %s: cannot open component %s\n",
1993 info
->sys_name
, dn
?dn
:"-unknown-");
2004 mlockall(MCL_FUTURE
);
2005 if (info
->delta_disks
< 0)
2006 done
= child_shrink(-1, info
, stripes
,
2008 info
->array
.raid_disks
,
2009 info
->array
.chunk_size
,
2010 info
->array
.level
, info
->array
.layout
,
2012 1, backup_list
, backup_offsets
);
2013 else if (info
->delta_disks
== 0) {
2014 /* The 'start' is a per-device stripe number.
2015 * reshape_progress is a per-array sector number.
2016 * So divide by ndata * chunk_size
2018 unsigned long long start
= info
->reshape_progress
/ ndata
;
2019 start
/= (info
->array
.chunk_size
/512);
2020 done
= child_same_size(-1, info
, stripes
,
2023 info
->array
.raid_disks
,
2024 info
->array
.chunk_size
,
2025 info
->array
.level
, info
->array
.layout
,
2027 1, backup_list
, backup_offsets
);
2029 if (backup_file
&& done
)
2030 unlink(backup_file
);
2031 /* FIXME should I intuit a level change */
2034 fprintf(stderr
, Name
": Cannot run child to continue monitoring reshape: %s\n",