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 1K form 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",
527 if (raid_disks
&& raid_disks
< array
.raid_disks
&& array
.level
> 1 &&
528 get_linux_version() < 2006032 &&
529 !check_env("MDADM_FORCE_FEWER")) {
530 fprintf(stderr
, Name
": reducing the number of devices is not safe before Linux 2.6.32\n"
531 " Please use a newer kernel\n");
534 sra
= sysfs_read(fd
, 0, GET_LEVEL
);
535 frozen
= freeze_array(sra
);
537 fprintf(stderr
, Name
": %s is performing resync/recovery and cannot"
538 " be reshaped\n", devname
);
542 /* ========= set size =============== */
543 if (size
>= 0 && (size
== 0 || size
!= array
.size
)) {
545 if (array
.size
!= size
) {
546 /* got truncated to 32bit, write to
547 * component_size instead
550 rv
= sysfs_set_num(sra
, NULL
,
551 "component_size", size
);
555 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
557 fprintf(stderr
, Name
": Cannot set device size for %s: %s\n",
558 devname
, strerror(errno
));
562 ioctl(fd
, GET_ARRAY_INFO
, &array
);
564 fprintf(stderr
, Name
": component size of %s has been set to %dK\n",
565 devname
, array
.size
);
569 /* ======= set level =========== */
570 if (level
!= UnSet
&& level
!= array
.level
) {
571 /* Trying to change the level.
572 * We might need to change layout first and schedule a
573 * level change for later.
574 * Level changes that can happen immediately are:
575 * 0->4,5,6 1->5 4->5,6 5->1,6
576 * Level changes that need a layout change first are:
577 * 6->5,4,0 : need a -6 layout, or parity-last
578 * 5->4,0 : need parity-last
580 if ((array
.level
== 6 || array
.level
== 5) &&
581 (level
== 5 || level
== 4 || level
== 0)) {
582 /* Don't change level yet, but choose intermediate
586 if (layout_str
== NULL
)
587 switch (array
.layout
) {
588 case ALGORITHM_LEFT_ASYMMETRIC
:
589 case ALGORITHM_LEFT_ASYMMETRIC_6
:
590 case ALGORITHM_ROTATING_N_RESTART
:
591 layout_str
= "left-asymmetric-6";
593 case ALGORITHM_LEFT_SYMMETRIC
:
594 case ALGORITHM_LEFT_SYMMETRIC_6
:
595 case ALGORITHM_ROTATING_N_CONTINUE
:
596 layout_str
= "left-symmetric-6";
598 case ALGORITHM_RIGHT_ASYMMETRIC
:
599 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
600 case ALGORITHM_ROTATING_ZERO_RESTART
:
601 layout_str
= "right-asymmetric-6";
603 case ALGORITHM_RIGHT_SYMMETRIC
:
604 case ALGORITHM_RIGHT_SYMMETRIC_6
:
605 layout_str
= "right-symmetric-6";
607 case ALGORITHM_PARITY_0
:
608 case ALGORITHM_PARITY_0_6
:
609 layout_str
= "parity-first-6";
611 case ALGORITHM_PARITY_N
:
612 layout_str
= "parity-last";
615 fprintf(stderr
, Name
": %s: cannot"
616 "convert layout to RAID5 equivalent\n",
622 int l
= map_name(r5layout
, layout_str
);
624 fprintf(stderr
, Name
": %s: layout '%s' not recognised\n",
625 devname
, layout_str
);
629 if (l
!= ALGORITHM_PARITY_N
) {
630 /* need the -6 version */
631 char *ls
= map_num(r5layout
, l
);
632 strcat(strcpy(alt_layout
, ls
),
634 layout_str
= alt_layout
;
638 /* The final raid6->raid5 conversion
639 * will reduce the number of disks,
640 * so now we need to aim higher
644 layout_str
= "parity-last";
646 c
= map_num(pers
, level
);
648 return 1;/* not possible */
649 err
= sysfs_set_str(sra
, NULL
, "level", c
);
651 fprintf(stderr
, Name
": %s: could not set level to %s\n",
657 orig_level
= orig
.level
;
658 ioctl(fd
, GET_ARRAY_INFO
, &array
);
659 if (layout_str
== NULL
&&
660 orig
.level
== 5 && level
== 6 &&
661 array
.layout
!= orig
.layout
)
662 layout_str
= map_num(r5layout
, orig
.layout
);
664 fprintf(stderr
, Name
" level of %s changed to %s\n",
670 /* ========= set shape (chunk_size / layout / ndisks) ============== */
671 /* Check if layout change is a no-op */
672 if (layout_str
) switch(array
.level
) {
674 if (array
.layout
== map_name(r5layout
, layout_str
))
678 if (layout_str
== NULL
&&
679 ((chunksize
&& chunksize
* 1024 != array
.chunk_size
) ||
680 (raid_disks
&& raid_disks
!= array
.raid_disks
)) &&
681 array
.layout
>= 16) {
683 ": %s has a non-standard layout. If you wish to preserve this\n"
684 " during the reshape, please specify --layout=preserve\n"
685 " If you want to change it, specify a layout or use --layout=normalise\n",
690 if (strcmp(layout_str
, "normalise") == 0 ||
691 strcmp(layout_str
, "normalize") == 0) {
693 strcpy(alt_layout
, map_num(r6layout
, array
.layout
));
694 hyphen
= strrchr(alt_layout
, '-');
695 if (hyphen
&& strcmp(hyphen
, "-6") == 0) {
697 layout_str
= alt_layout
;
701 if (array
.layout
== map_name(r6layout
, layout_str
))
703 if (layout_str
&& strcmp(layout_str
, "preserve") == 0)
707 if (layout_str
== NULL
708 && (chunksize
== 0 || chunksize
*1024 == array
.chunk_size
)
709 && (raid_disks
== 0 || raid_disks
== array
.raid_disks
)) {
711 if (level
!= UnSet
&& level
!= array
.level
) {
712 /* Looks like this level change doesn't need
713 * a reshape after all.
715 c
= map_num(pers
, level
);
717 rv
= sysfs_set_str(sra
, NULL
, "level", c
);
719 fprintf(stderr
, Name
": %s: could not set level to %s\n",
722 } else if (!changed
&& !quiet
)
723 fprintf(stderr
, Name
": %s: no change requested\n",
728 c
= map_num(pers
, array
.level
);
729 if (c
== NULL
) c
= "-unknown-";
730 switch(array
.level
) {
731 default: /* raid0, linear, multipath cannot be reconfigured */
732 fprintf(stderr
, Name
": %s array %s cannot be reshaped.\n",
737 case LEVEL_FAULTY
: /* only 'layout' change is permitted */
739 if (chunksize
|| raid_disks
) {
740 fprintf(stderr
, Name
": %s: Cannot change chunksize or disks of a 'faulty' array\n",
745 if (layout_str
== NULL
)
746 break; /* nothing to do.... */
748 array
.layout
= parse_layout_faulty(layout_str
);
749 if (array
.layout
< 0) {
751 fprintf(stderr
, Name
": %s: layout %s not understood for 'faulty' array\n",
752 devname
, layout_str
);
756 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
757 fprintf(stderr
, Name
": Cannot set layout for %s: %s\n",
758 devname
, strerror(errno
));
761 printf("layout for %s set to %d\n", devname
, array
.layout
);
764 case 1: /* only raid_disks can each be changed. */
766 if (chunksize
|| layout_str
!= NULL
) {
767 fprintf(stderr
, Name
": %s: Cannot change chunk size or layout for a RAID1 array.\n",
772 if (raid_disks
> 0) {
773 array
.raid_disks
= raid_disks
;
774 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
775 fprintf(stderr
, Name
": Cannot set raid-devices for %s: %s\n",
776 devname
, strerror(errno
));
787 * layout/chunksize/raid_disks can be changed
788 * though the kernel may not support it all.
790 st
= super_by_fd(fd
);
793 * There are three possibilities.
794 * 1/ The array will shrink.
795 * We need to ensure the reshape will pause before reaching
796 * the 'critical section'. We also need to fork and wait for
797 * that to happen. When it does we
798 * suspend/backup/complete/unfreeze
800 * 2/ The array will not change size.
801 * This requires that we keep a backup of a sliding window
802 * so that we can restore data after a crash. So we need
803 * to fork and monitor progress.
805 * 3/ The array will grow. This is relatively easy.
806 * However the kernel's restripe routines will cheerfully
807 * overwrite some early data before it is safe. So we
808 * need to make a backup of the early parts of the array
809 * and be ready to restore it if rebuild aborts very early.
811 * We backup data by writing it to one spare, or to a
812 * file which was given on command line.
814 * [FOLLOWING IS OLD AND PARTLY WRONG]
815 * So: we enumerate the devices in the array and
816 * make sure we can open all of them.
817 * Then we freeze the early part of the array and
818 * backup to the various spares.
819 * Then we request changes and start the reshape.
820 * Monitor progress until it has passed the danger zone.
821 * and finally invalidate the copied data and unfreeze the
822 * start of the array.
824 * In each case, we first make sure that storage is available
825 * for the required backup.
827 * - request the shape change.
828 * - for to handle backup etc.
830 nchunk
= ochunk
= array
.chunk_size
;
831 nlayout
= olayout
= array
.layout
;
832 ndisks
= odisks
= array
.raid_disks
;
835 nchunk
= chunksize
* 1024;
836 if (array
.size
% chunksize
) {
837 fprintf(stderr
, Name
": component size %dK is not"
838 " a multiple of chunksize %dK\n",
839 array
.size
, chunksize
);
843 if (layout_str
!= NULL
)
844 switch(array
.level
) {
845 case 4: /* ignore layout */
848 nlayout
= map_name(r5layout
, layout_str
);
849 if (nlayout
== UnSet
) {
850 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
857 nlayout
= map_name(r6layout
, layout_str
);
858 if (nlayout
== UnSet
) {
859 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
865 if (raid_disks
) ndisks
= raid_disks
;
869 if (array
.level
== 6) {
870 odata
--; /* number of data disks */
874 /* Check that we can hold all the data */
875 size
= ndata
* array
.size
;
876 get_dev_size(fd
, NULL
, &array_size
);
877 if (size
< (array_size
/1024)) {
878 fprintf(stderr
, Name
": this change will reduce the size of the array.\n"
879 " use --grow --array-size first to truncate array.\n"
880 " e.g. mdadm --grow %s --array-size %llu\n",
886 /* So how much do we need to backup.
887 * We need an amount of data which is both a whole number of
888 * old stripes and a whole number of new stripes.
889 * So LCM for (chunksize*datadisks).
891 a
= ochunk
/512 * odata
;
892 b
= nchunk
/512 * ndata
;
900 /* LCM == product / GCD */
901 blocks
= ochunk
/512 * nchunk
/512 * odata
* ndata
/ a
;
904 sra
= sysfs_read(fd
, 0,
905 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
908 if (ndata
== odata
) {
909 /* Make 'blocks' bigger for better throughput, but
910 * not so big that we reject it below.
911 * Try for 16 megabytes
913 while (blocks
* 32 < sra
->component_size
&&
917 fprintf(stderr
, Name
": Need to backup %luK of critical "
918 "section..\n", blocks
/2);
921 fprintf(stderr
, Name
": %s: Cannot get array details from sysfs\n",
927 if (blocks
>= sra
->component_size
/2) {
928 fprintf(stderr
, Name
": %s: Something wrong - reshape aborted\n",
933 nrdisks
= array
.nr_disks
+ sra
->array
.spare_disks
;
934 /* Now we need to open all these devices so we can read/write.
936 fdlist
= malloc((1+nrdisks
) * sizeof(int));
937 offsets
= malloc((1+nrdisks
) * sizeof(offsets
[0]));
938 if (!fdlist
|| !offsets
) {
939 fprintf(stderr
, Name
": malloc failed: grow aborted\n");
943 for (d
=0; d
<= nrdisks
; d
++)
945 d
= array
.raid_disks
;
946 for (sd
= sra
->devs
; sd
; sd
=sd
->next
) {
947 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
949 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
950 char *dn
= map_dev(sd
->disk
.major
,
952 fdlist
[sd
->disk
.raid_disk
]
953 = dev_open(dn
, O_RDONLY
);
954 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
955 if (fdlist
[sd
->disk
.raid_disk
] < 0) {
956 fprintf(stderr
, Name
": %s: cannot open component %s\n",
957 devname
, dn
?dn
:"-unknown-");
961 } else if (backup_file
== NULL
) {
963 char *dn
= map_dev(sd
->disk
.major
,
965 fdlist
[d
] = dev_open(dn
, O_RDWR
);
966 offsets
[d
] = (sra
->component_size
- blocks
- 8)*512;
968 fprintf(stderr
, Name
": %s: cannot open component %s\n",
969 devname
, dn
?dn
:"-unknown");
976 if (backup_file
== NULL
) {
977 if (ndata
<= odata
) {
978 fprintf(stderr
, Name
": %s: Cannot grow - need backup-file\n",
982 } else if (sra
->array
.spare_disks
== 0) {
983 fprintf(stderr
, Name
": %s: Cannot grow - need a spare or "
984 "backup-file to backup critical section\n",
989 if (d
== array
.raid_disks
) {
990 fprintf(stderr
, Name
": %s: No spare device for backup\n",
996 /* need to check backup file is large enough */
998 fdlist
[d
] = open(backup_file
, O_RDWR
|O_CREAT
|O_EXCL
,
1000 offsets
[d
] = 8 * 512;
1001 if (fdlist
[d
] < 0) {
1002 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1003 devname
, backup_file
, strerror(errno
));
1007 memset(buf
, 0, 512);
1008 for (i
=0; i
< blocks
+ 1 ; i
++) {
1009 if (write(fdlist
[d
], buf
, 512) != 512) {
1010 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1011 devname
, backup_file
, strerror(errno
));
1016 if (fsync(fdlist
[d
]) != 0) {
1017 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
1018 devname
, backup_file
, strerror(errno
));
1025 /* lastly, check that the internal stripe cache is
1026 * large enough, or it won't work.
1029 cache
= (nchunk
< ochunk
) ? ochunk
: nchunk
;
1030 cache
= cache
* 4 / 4096;
1031 if (cache
< blocks
/ 8 / odisks
+ 16)
1032 /* Make it big enough to hold 'blocks' */
1033 cache
= blocks
/ 8 / odisks
+ 16;
1034 if (sra
->cache_size
< cache
)
1035 sysfs_set_num(sra
, NULL
, "stripe_cache_size",
1037 /* Right, everything seems fine. Let's kick things off.
1038 * If only changing raid_disks, use ioctl, else use
1041 if (ochunk
== nchunk
&& olayout
== nlayout
) {
1042 array
.raid_disks
= ndisks
;
1043 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
1045 fprintf(stderr
, Name
": Cannot set device shape for %s: %s\n",
1046 devname
, strerror(errno
));
1047 if (ndisks
< odisks
&&
1048 get_linux_version() < 2006030)
1049 fprintf(stderr
, Name
": linux 2.6.30 or later required\n");
1054 /* set them all just in case some old 'new_*' value
1055 * persists from some earlier problem
1057 if (sysfs_set_num(sra
, NULL
, "chunk_size", nchunk
) < 0)
1059 if (sysfs_set_num(sra
, NULL
, "layout", nlayout
) < 0)
1061 if (sysfs_set_num(sra
, NULL
, "raid_disks", ndisks
) < 0)
1064 fprintf(stderr
, Name
": Cannot set device shape for %s\n",
1066 if (get_linux_version() < 2006030)
1067 fprintf(stderr
, Name
": linux 2.6.30 or later required\n");
1072 if (ndisks
== 2 && odisks
== 2) {
1073 /* No reshape is needed in this trivial case */
1078 /* set up the backup-super-block. This requires the
1079 * uuid from the array.
1081 /* Find a superblock */
1082 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1086 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1088 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
1089 devfd
= dev_open(dn
, O_RDONLY
);
1092 ok
= st
->ss
->load_super(st
, devfd
, NULL
);
1098 fprintf(stderr
, Name
": %s: Cannot find a superblock\n",
1104 memset(&bsb
, 0, 512);
1105 memcpy(bsb
.magic
, "md_backup_data-1", 16);
1106 st
->ss
->uuid_from_super(st
, (int*)&bsb
.set_uuid
);
1107 bsb
.mtime
= __cpu_to_le64(time(0));
1108 bsb
.devstart2
= blocks
;
1109 stripes
= blocks
/ (ochunk
/512) / odata
;
1110 /* Now we just need to kick off the reshape and watch, while
1111 * handling backups of the data...
1112 * This is all done by a forked background process.
1117 if (check_env("MDADM_GROW_VERIFY"))
1118 fd
= open(devname
, O_RDONLY
| O_DIRECT
);
1121 mlockall(MCL_FUTURE
);
1124 done
= child_grow(fd
, sra
, stripes
,
1126 odisks
, ochunk
, array
.level
, olayout
, odata
,
1127 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1128 else if (odata
> ndata
)
1129 done
= child_shrink(fd
, sra
, stripes
,
1131 odisks
, ochunk
, array
.level
, olayout
, odata
,
1132 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1134 done
= child_same_size(fd
, sra
, stripes
,
1137 odisks
, ochunk
, array
.level
, olayout
, odata
,
1138 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1139 if (backup_file
&& done
)
1140 unlink(backup_file
);
1141 if (level
!= UnSet
&& level
!= array
.level
) {
1142 /* We need to wait for the reshape to finish
1143 * (which will have happened unless odata < ndata)
1144 * and then set the level
1147 c
= map_num(pers
, level
);
1149 exit(0);/* not possible */
1153 err
= sysfs_set_str(sra
, NULL
, "level", c
);
1155 fprintf(stderr
, Name
": %s: could not set level to %s\n",
1160 fprintf(stderr
, Name
": Cannot run child to monitor reshape: %s\n",
1165 /* The child will take care of unfreezing the array */
1174 if (rv
&& orig_level
!= UnSet
&& sra
) {
1175 c
= map_num(pers
, orig_level
);
1176 if (c
&& sysfs_set_str(sra
, NULL
, "level", c
) == 0)
1177 fprintf(stderr
, Name
": aborting level change\n");
1180 unfreeze_array(sra
, frozen
);
1185 * We run a child process in the background which performs the following
1187 * - wait for resync to reach a certain point
1188 * - suspend io to the following section
1189 * - backup that section
1190 * - allow resync to proceed further
1192 * - discard the backup.
1194 * When are combined in slightly different ways in the three cases.
1196 * - suspend/backup/allow/wait/resume/discard
1198 * - allow/wait/suspend/backup/allow/wait/resume/discard
1200 * - wait/resume/discard/suspend/backup/allow
1202 * suspend/backup/allow always come together
1203 * wait/resume/discard do too.
1204 * For the same-size case we have two backups to improve flow.
1208 int grow_backup(struct mdinfo
*sra
,
1209 unsigned long long offset
, /* per device */
1210 unsigned long stripes
, /* per device */
1211 int *sources
, unsigned long long *offsets
,
1212 int disks
, int chunk
, int level
, int layout
,
1213 int dests
, int *destfd
, unsigned long long *destoffsets
,
1214 int part
, int *degraded
,
1217 /* Backup 'blocks' sectors at 'offset' on each device of the array,
1218 * to storage 'destfd' (offset 'destoffsets'), after first
1219 * suspending IO. Then allow resync to continue
1220 * over the suspended section.
1221 * Use part 'part' of the backup-super-block.
1226 unsigned long long new_degraded
;
1227 //printf("offset %llu\n", offset);
1232 sysfs_set_num(sra
, NULL
, "suspend_hi", (offset
+ stripes
* chunk
/512) * odata
);
1233 /* Check that array hasn't become degraded, else we might backup the wrong data */
1234 sysfs_get_ll(sra
, NULL
, "degraded", &new_degraded
);
1235 if (new_degraded
!= *degraded
) {
1236 /* check each device to ensure it is still working */
1238 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1239 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1241 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
1243 if (sysfs_get_str(sra
, sd
, "state", sbuf
, 20) < 0 ||
1244 strstr(sbuf
, "faulty") ||
1245 strstr(sbuf
, "in_sync") == NULL
) {
1246 /* this device is dead */
1247 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
1248 if (sd
->disk
.raid_disk
>= 0 &&
1249 sources
[sd
->disk
.raid_disk
] >= 0) {
1250 close(sources
[sd
->disk
.raid_disk
]);
1251 sources
[sd
->disk
.raid_disk
] = -1;
1256 *degraded
= new_degraded
;
1259 bsb
.arraystart2
= __cpu_to_le64(offset
* odata
);
1260 bsb
.length2
= __cpu_to_le64(stripes
* chunk
/512 * odata
);
1262 bsb
.arraystart
= __cpu_to_le64(offset
* odata
);
1263 bsb
.length
= __cpu_to_le64(stripes
* chunk
/512 * odata
);
1266 bsb
.magic
[15] = '2';
1267 for (i
= 0; i
< dests
; i
++)
1269 lseek64(destfd
[i
], destoffsets
[i
] + __le64_to_cpu(bsb
.devstart2
)*512, 0);
1271 lseek64(destfd
[i
], destoffsets
[i
], 0);
1273 rv
= save_stripes(sources
, offsets
,
1274 disks
, chunk
, level
, layout
,
1276 offset
*512*odata
, stripes
* chunk
* odata
,
1281 bsb
.mtime
= __cpu_to_le64(time(0));
1282 for (i
= 0; i
< dests
; i
++) {
1283 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
1285 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
1286 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
1287 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
1288 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
1290 lseek64(destfd
[i
], destoffsets
[i
] - 4096, 0);
1291 write(destfd
[i
], &bsb
, 512);
1298 /* in 2.6.30, the value reported by sync_completed can be
1299 * less that it should be by one stripe.
1300 * This only happens when reshape hits sync_max and pauses.
1301 * So allow wait_backup to either extent sync_max further
1302 * than strictly necessary, or return before the
1303 * sync has got quite as far as we would really like.
1304 * This is what 'blocks2' is for.
1305 * The various caller give appropriate values so that
1308 int wait_backup(struct mdinfo
*sra
,
1309 unsigned long long offset
, /* per device */
1310 unsigned long long blocks
, /* per device */
1311 unsigned long long blocks2
, /* per device - hack */
1312 int dests
, int *destfd
, unsigned long long *destoffsets
,
1315 /* Wait for resync to pass the section that was backed up
1316 * then erase the backup and allow IO
1318 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
1319 unsigned long long completed
;
1324 sysfs_set_num(sra
, NULL
, "sync_max", offset
+ blocks
+ blocks2
);
1326 sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
1332 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
1333 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
1337 if (sysfs_get_str(sra
, NULL
, "sync_action",
1339 strncmp(action
, "reshape", 7) != 0)
1341 } while (completed
< offset
+ blocks
);
1345 bsb
.arraystart2
= __cpu_to_le64(0);
1346 bsb
.length2
= __cpu_to_le64(0);
1348 bsb
.arraystart
= __cpu_to_le64(0);
1349 bsb
.length
= __cpu_to_le64(0);
1351 bsb
.mtime
= __cpu_to_le64(time(0));
1352 for (i
= 0; i
< dests
; i
++) {
1353 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
1354 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
1355 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
1356 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
1357 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
1358 lseek64(destfd
[i
], destoffsets
[i
]-4096, 0);
1359 write(destfd
[i
], &bsb
, 512);
1365 static void fail(char *msg
)
1367 write(2, msg
, strlen(msg
));
1372 static char *abuf
, *bbuf
;
1374 static void validate(int afd
, int bfd
, unsigned long long offset
)
1376 /* check that the data in the backup against the array.
1377 * This is only used for regression testing and should not
1378 * be used while the array is active
1382 lseek64(bfd
, offset
- 4096, 0);
1383 if (read(bfd
, &bsb2
, 512) != 512)
1384 fail("cannot read bsb");
1385 if (bsb2
.sb_csum
!= bsb_csum((char*)&bsb2
,
1386 ((char*)&bsb2
.sb_csum
)-((char*)&bsb2
)))
1387 fail("first csum bad");
1388 if (memcmp(bsb2
.magic
, "md_backup_data", 14) != 0)
1389 fail("magic is bad");
1390 if (memcmp(bsb2
.magic
, "md_backup_data-2", 16) == 0 &&
1391 bsb2
.sb_csum2
!= bsb_csum((char*)&bsb2
,
1392 ((char*)&bsb2
.sb_csum2
)-((char*)&bsb2
)))
1393 fail("second csum bad");
1395 if (__le64_to_cpu(bsb2
.devstart
)*512 != offset
)
1396 fail("devstart is wrong");
1399 unsigned long long len
= __le64_to_cpu(bsb2
.length
)*512;
1401 if (abuflen
< len
) {
1405 posix_memalign((void**)&abuf
, 4096, abuflen
);
1406 posix_memalign((void**)&bbuf
, 4096, abuflen
);
1409 lseek64(bfd
, offset
, 0);
1410 if (read(bfd
, bbuf
, len
) != len
) {
1411 printf("len %llu\n", len
);
1412 fail("read first backup failed");
1414 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart
)*512, 0);
1415 if (read(afd
, abuf
, len
) != len
)
1416 fail("read first from array failed");
1417 if (memcmp(bbuf
, abuf
, len
) != 0) {
1419 printf("offset=%llu len=%llu\n",
1420 __le64_to_cpu(bsb2
.arraystart
)*512, len
);
1421 for (i
=0; i
<len
; i
++)
1422 if (bbuf
[i
] != abuf
[i
]) {
1423 printf("first diff byte %d\n", i
);
1426 fail("data1 compare failed");
1430 unsigned long long len
= __le64_to_cpu(bsb2
.length2
)*512;
1432 if (abuflen
< len
) {
1436 abuf
= malloc(abuflen
);
1437 bbuf
= malloc(abuflen
);
1440 lseek64(bfd
, offset
+__le64_to_cpu(bsb2
.devstart2
)*512, 0);
1441 if (read(bfd
, bbuf
, len
) != len
)
1442 fail("read second backup failed");
1443 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart2
)*512, 0);
1444 if (read(afd
, abuf
, len
) != len
)
1445 fail("read second from array failed");
1446 if (memcmp(bbuf
, abuf
, len
) != 0)
1447 fail("data2 compare failed");
1451 static int child_grow(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1452 int *fds
, unsigned long long *offsets
,
1453 int disks
, int chunk
, int level
, int layout
, int data
,
1454 int dests
, int *destfd
, unsigned long long *destoffsets
)
1459 posix_memalign((void**)&buf
, 4096, disks
* chunk
);
1460 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1461 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1462 grow_backup(sra
, 0, stripes
,
1463 fds
, offsets
, disks
, chunk
, level
, layout
,
1464 dests
, destfd
, destoffsets
,
1466 validate(afd
, destfd
[0], destoffsets
[0]);
1467 wait_backup(sra
, 0, stripes
* chunk
/ 512, stripes
* chunk
/ 512,
1468 dests
, destfd
, destoffsets
,
1470 sysfs_set_num(sra
, NULL
, "suspend_lo", (stripes
* chunk
/512) * data
);
1472 /* FIXME this should probably be numeric */
1473 sysfs_set_str(sra
, NULL
, "sync_max", "max");
1477 static int child_shrink(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1478 int *fds
, unsigned long long *offsets
,
1479 int disks
, int chunk
, int level
, int layout
, int data
,
1480 int dests
, int *destfd
, unsigned long long *destoffsets
)
1483 unsigned long long start
;
1487 posix_memalign((void**)&buf
, 4096, disks
* chunk
);
1488 start
= sra
->component_size
- stripes
* chunk
/512;
1489 sysfs_set_num(sra
, NULL
, "sync_max", start
);
1490 sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
1491 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1492 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1493 rv
= wait_backup(sra
, 0, start
- stripes
* chunk
/512, stripes
* chunk
/512,
1494 dests
, destfd
, destoffsets
, 0);
1497 grow_backup(sra
, 0, stripes
,
1499 disks
, chunk
, level
, layout
,
1500 dests
, destfd
, destoffsets
,
1502 validate(afd
, destfd
[0], destoffsets
[0]);
1503 wait_backup(sra
, start
, stripes
*chunk
/512, 0,
1504 dests
, destfd
, destoffsets
, 0);
1505 sysfs_set_num(sra
, NULL
, "suspend_lo", (stripes
* chunk
/512) * data
);
1507 /* FIXME this should probably be numeric */
1508 sysfs_set_str(sra
, NULL
, "sync_max", "max");
1512 static int child_same_size(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
1513 int *fds
, unsigned long long *offsets
,
1514 unsigned long long start
,
1515 int disks
, int chunk
, int level
, int layout
, int data
,
1516 int dests
, int *destfd
, unsigned long long *destoffsets
)
1518 unsigned long long size
;
1519 unsigned long tailstripes
= stripes
;
1522 unsigned long long speed
;
1526 posix_memalign((void**)&buf
, 4096, disks
* chunk
);
1528 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
1529 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
1531 sysfs_get_ll(sra
, NULL
, "sync_speed_min", &speed
);
1532 sysfs_set_num(sra
, NULL
, "sync_speed_min", 200000);
1534 grow_backup(sra
, start
, stripes
,
1536 disks
, chunk
, level
, layout
,
1537 dests
, destfd
, destoffsets
,
1539 grow_backup(sra
, (start
+ stripes
) * chunk
/512, stripes
,
1541 disks
, chunk
, level
, layout
,
1542 dests
, destfd
, destoffsets
,
1544 validate(afd
, destfd
[0], destoffsets
[0]);
1546 start
+= stripes
* 2; /* where to read next */
1547 size
= sra
->component_size
/ (chunk
/512);
1548 while (start
< size
) {
1549 if (wait_backup(sra
, (start
-stripes
*2)*chunk
/512,
1550 stripes
*chunk
/512, 0,
1551 dests
, destfd
, destoffsets
,
1554 sysfs_set_num(sra
, NULL
, "suspend_lo", start
*chunk
/512 * data
);
1555 if (start
+ stripes
> size
)
1556 tailstripes
= (size
- start
);
1558 grow_backup(sra
, start
*chunk
/512, tailstripes
,
1560 disks
, chunk
, level
, layout
,
1561 dests
, destfd
, destoffsets
,
1562 part
, °raded
, buf
);
1565 validate(afd
, destfd
[0], destoffsets
[0]);
1567 if (wait_backup(sra
, (start
-stripes
*2) * chunk
/512, stripes
* chunk
/512, 0,
1568 dests
, destfd
, destoffsets
,
1571 sysfs_set_num(sra
, NULL
, "suspend_lo", ((start
-stripes
)*chunk
/512) * data
);
1572 wait_backup(sra
, (start
-stripes
) * chunk
/512, tailstripes
* chunk
/512, 0,
1573 dests
, destfd
, destoffsets
,
1575 sysfs_set_num(sra
, NULL
, "suspend_lo", (size
*chunk
/512) * data
);
1576 sysfs_set_num(sra
, NULL
, "sync_speed_min", speed
);
1582 * If any spare contains md_back_data-1 which is recent wrt mtime,
1583 * write that data into the array and update the super blocks with
1584 * the new reshape_progress
1586 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
, int cnt
,
1587 char *backup_file
, int verbose
)
1591 unsigned long long *offsets
;
1592 unsigned long long nstripe
, ostripe
;
1595 if (info
->new_level
!= info
->array
.level
)
1596 return 1; /* Cannot handle level changes (they are instantaneous) */
1598 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
1599 if (info
->array
.level
== 6) odata
--; /* number of data disks */
1600 ndata
= info
->array
.raid_disks
- 1;
1601 if (info
->new_level
== 6) ndata
--;
1603 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
1605 if (info
->delta_disks
<= 0)
1606 /* Didn't grow, so the backup file must have
1610 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
1611 struct mdinfo dinfo
;
1615 char *devname
, namebuf
[20];
1617 /* This was a spare and may have some saved data on it.
1618 * Load the superblock, find and load the
1619 * backup_super_block.
1620 * If either fail, go on to next device.
1621 * If the backup contains no new info, just return
1622 * else restore data and update all superblocks
1624 if (i
== old_disks
-1) {
1625 fd
= open(backup_file
, O_RDONLY
);
1627 fprintf(stderr
, Name
": backup file %s inaccessible: %s\n",
1628 backup_file
, strerror(errno
));
1631 devname
= backup_file
;
1636 if (st
->ss
->load_super(st
, fd
, NULL
))
1639 st
->ss
->getinfo_super(st
, &dinfo
);
1640 st
->ss
->free_super(st
);
1643 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
1645 fprintf(stderr
, Name
": Cannot seek on device %d\n", i
);
1646 continue; /* Cannot seek */
1648 sprintf(namebuf
, "device-%d", i
);
1651 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
)) {
1653 fprintf(stderr
, Name
": Cannot read from %s\n", devname
);
1654 continue; /* Cannot read */
1656 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0 &&
1657 memcmp(bsb
.magic
, "md_backup_data-2", 16) != 0) {
1659 fprintf(stderr
, Name
": No backup metadata on %s\n", devname
);
1662 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
))) {
1664 fprintf(stderr
, Name
": Bad backup-metadata checksum on %s\n", devname
);
1665 continue; /* bad checksum */
1667 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0 &&
1668 bsb
.sb_csum2
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum2
)-((char*)&bsb
))) {
1670 fprintf(stderr
, Name
": Bad backup-metadata checksum2 on %s\n", devname
);
1671 continue; /* Bad second checksum */
1673 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0) {
1675 fprintf(stderr
, Name
": Wrong uuid on backup-metadata on %s\n", devname
);
1676 continue; /* Wrong uuid */
1679 if (info
->array
.utime
> __le64_to_cpu(bsb
.mtime
) + 10*60 ||
1680 info
->array
.utime
< __le64_to_cpu(bsb
.mtime
) - 10*60) {
1682 fprintf(stderr
, Name
": too-old timestamp on backup-metadata on %s\n", devname
);
1683 continue; /* time stamp is too bad */
1686 if (bsb
.magic
[15] == '1') {
1687 if (info
->delta_disks
>= 0) {
1688 /* reshape_progress is increasing */
1689 if (__le64_to_cpu(bsb
.arraystart
) + __le64_to_cpu(bsb
.length
) <
1690 info
->reshape_progress
) {
1693 fprintf(stderr
, Name
": backup-metadata found on %s but is not needed\n", devname
);
1694 continue; /* No new data here */
1697 /* reshape_progress is decreasing */
1698 if (__le64_to_cpu(bsb
.arraystart
) >=
1699 info
->reshape_progress
)
1700 goto nonew
; /* No new data here */
1703 if (info
->delta_disks
>= 0) {
1704 /* reshape_progress is increasing */
1705 if (__le64_to_cpu(bsb
.arraystart
) + __le64_to_cpu(bsb
.length
) <
1706 info
->reshape_progress
&&
1707 __le64_to_cpu(bsb
.arraystart2
) + __le64_to_cpu(bsb
.length2
) <
1708 info
->reshape_progress
)
1709 goto nonew
; /* No new data here */
1711 /* reshape_progress is decreasing */
1712 if (__le64_to_cpu(bsb
.arraystart
) >=
1713 info
->reshape_progress
&&
1714 __le64_to_cpu(bsb
.arraystart2
) >=
1715 info
->reshape_progress
)
1716 goto nonew
; /* No new data here */
1719 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0) {
1722 fprintf(stderr
, Name
": Failed to verify secondary backup-metadata block on %s\n",
1724 continue; /* Cannot seek */
1726 /* There should be a duplicate backup superblock 4k before here */
1727 if (lseek64(fd
, -4096, 1) < 0 ||
1728 read(fd
, buf
, 4096) != 4096)
1729 goto second_fail
; /* Cannot find leading superblock */
1730 if (bsb
.magic
[15] == '1')
1731 bsbsize
= offsetof(struct mdp_backup_super
, pad1
);
1733 bsbsize
= offsetof(struct mdp_backup_super
, pad
);
1734 if (memcmp(buf
, &bsb
, bsbsize
) != 0)
1735 goto second_fail
; /* Cannot find leading superblock */
1737 /* Now need the data offsets for all devices. */
1738 offsets
= malloc(sizeof(*offsets
)*info
->array
.raid_disks
);
1739 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
1742 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
1743 /* FIXME should be this be an error */
1745 st
->ss
->getinfo_super(st
, &dinfo
);
1746 st
->ss
->free_super(st
);
1747 offsets
[j
] = dinfo
.data_offset
;
1749 printf(Name
": restoring critical section\n");
1751 if (restore_stripes(fdlist
, offsets
,
1752 info
->array
.raid_disks
,
1756 fd
, __le64_to_cpu(bsb
.devstart
)*512,
1757 __le64_to_cpu(bsb
.arraystart
)*512,
1758 __le64_to_cpu(bsb
.length
)*512)) {
1759 /* didn't succeed, so giveup */
1761 fprintf(stderr
, Name
": Error restoring backup from %s\n",
1766 if (bsb
.magic
[15] == '2' &&
1767 restore_stripes(fdlist
, offsets
,
1768 info
->array
.raid_disks
,
1772 fd
, __le64_to_cpu(bsb
.devstart
)*512 +
1773 __le64_to_cpu(bsb
.devstart2
)*512,
1774 __le64_to_cpu(bsb
.arraystart2
)*512,
1775 __le64_to_cpu(bsb
.length2
)*512)) {
1776 /* didn't succeed, so giveup */
1778 fprintf(stderr
, Name
": Error restoring second backup from %s\n",
1784 /* Ok, so the data is restored. Let's update those superblocks. */
1786 if (info
->delta_disks
>= 0) {
1787 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
) +
1788 __le64_to_cpu(bsb
.length
);
1789 if (bsb
.magic
[15] == '2') {
1790 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
) +
1791 __le64_to_cpu(bsb
.length2
);
1792 if (p2
> info
->reshape_progress
)
1793 info
->reshape_progress
= p2
;
1796 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
);
1797 if (bsb
.magic
[15] == '2') {
1798 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
);
1799 if (p2
< info
->reshape_progress
)
1800 info
->reshape_progress
= p2
;
1803 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
1804 if (fdlist
[j
] < 0) continue;
1805 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
1807 st
->ss
->getinfo_super(st
, &dinfo
);
1808 dinfo
.reshape_progress
= info
->reshape_progress
;
1809 st
->ss
->update_super(st
, &dinfo
,
1810 "_reshape_progress",
1812 st
->ss
->store_super(st
, fdlist
[j
]);
1813 st
->ss
->free_super(st
);
1817 /* Didn't find any backup data, try to see if any
1820 if (info
->delta_disks
< 0) {
1821 /* When shrinking, the critical section is at the end.
1822 * So see if we are before the critical section.
1824 unsigned long long first_block
;
1825 nstripe
= ostripe
= 0;
1827 while (ostripe
>= nstripe
) {
1828 ostripe
+= info
->array
.chunk_size
/ 512;
1829 first_block
= ostripe
* odata
;
1830 nstripe
= first_block
/ ndata
/ (info
->new_chunk
/512) *
1831 (info
->new_chunk
/512);
1834 if (info
->reshape_progress
>= first_block
)
1837 if (info
->delta_disks
> 0) {
1838 /* See if we are beyond the critical section. */
1839 unsigned long long last_block
;
1840 nstripe
= ostripe
= 0;
1842 while (nstripe
>= ostripe
) {
1843 nstripe
+= info
->new_chunk
/ 512;
1844 last_block
= nstripe
* ndata
;
1845 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
1846 (info
->array
.chunk_size
/512);
1849 if (info
->reshape_progress
>= last_block
)
1852 /* needed to recover critical section! */
1854 fprintf(stderr
, Name
": Failed to find backup of critical section\n");
1858 int Grow_continue(int mdfd
, struct supertype
*st
, struct mdinfo
*info
,
1861 /* Array is assembled and ready to be started, but
1862 * monitoring is probably required.
1865 * - set upper bound for resync
1866 * - initialise the 'suspend' boundaries
1867 * - switch to read-write
1868 * - fork and continue monitoring
1872 unsigned long long backup_offsets
[1];
1873 int odisks
, ndisks
, ochunk
, nchunk
,odata
,ndata
;
1874 unsigned long a
,b
,blocks
,stripes
;
1877 unsigned long long *offsets
;
1879 struct mdinfo
*sra
, *sd
;
1884 err
= sysfs_set_str(info
, NULL
, "array_state", "readonly");
1888 /* make sure reshape doesn't progress until we are ready */
1889 sysfs_set_str(info
, NULL
, "sync_max", "0");
1890 sysfs_set_str(info
, NULL
, "array_state", "active"); /* FIXME or clean */
1892 /* ndisks is not growing, so raid_disks is old and +delta is new */
1893 odisks
= info
->array
.raid_disks
;
1894 ndisks
= odisks
+ info
->delta_disks
;
1897 if (info
->array
.level
== 6) {
1901 ochunk
= info
->array
.chunk_size
;
1902 nchunk
= info
->new_chunk
;
1905 a
= ochunk
/512 * odata
;
1906 b
= nchunk
/512 * ndata
;
1914 /* LCM == product / GCD */
1915 blocks
= ochunk
/512 * nchunk
/512 * odata
* ndata
/ a
;
1917 sra
= sysfs_read(-1, devname2devnum(info
->sys_name
),
1918 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
1923 while (blocks
* 32 < sra
->component_size
&&
1926 stripes
= blocks
/ (info
->array
.chunk_size
/512) / odata
;
1928 /* check that the internal stripe cache is
1929 * large enough, or it won't work.
1931 cache
= (nchunk
< ochunk
) ? ochunk
: nchunk
;
1932 cache
= cache
* 4 / 4096;
1933 if (cache
< blocks
/ 8 / odisks
+ 16)
1934 /* Make it big enough to hold 'blocks' */
1935 cache
= blocks
/ 8 / odisks
+ 16;
1936 if (sra
->cache_size
< cache
)
1937 sysfs_set_num(sra
, NULL
, "stripe_cache_size",
1940 memset(&bsb
, 0, 512);
1941 memcpy(bsb
.magic
, "md_backup_data-1", 16);
1942 memcpy(&bsb
.set_uuid
, info
->uuid
, 16);
1943 bsb
.mtime
= __cpu_to_le64(time(0));
1944 bsb
.devstart2
= blocks
;
1946 backup_fd
= open(backup_file
, O_RDWR
|O_CREAT
, S_IRUSR
| S_IWUSR
);
1947 backup_list
[0] = backup_fd
;
1948 backup_offsets
[0] = 8 * 512;
1949 fds
= malloc(odisks
* sizeof(fds
[0]));
1950 offsets
= malloc(odisks
* sizeof(offsets
[0]));
1951 for (d
=0; d
<odisks
; d
++)
1954 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1955 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1957 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
1958 char *dn
= map_dev(sd
->disk
.major
,
1960 fds
[sd
->disk
.raid_disk
]
1961 = dev_open(dn
, O_RDONLY
);
1962 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
1963 if (fds
[sd
->disk
.raid_disk
] < 0) {
1964 fprintf(stderr
, Name
": %s: cannot open component %s\n",
1965 info
->sys_name
, dn
?dn
:"-unknown-");
1976 mlockall(MCL_FUTURE
);
1977 if (info
->delta_disks
< 0)
1978 done
= child_shrink(-1, info
, stripes
,
1980 info
->array
.raid_disks
,
1981 info
->array
.chunk_size
,
1982 info
->array
.level
, info
->array
.layout
,
1984 1, backup_list
, backup_offsets
);
1985 else if (info
->delta_disks
== 0) {
1986 /* The 'start' is a per-device stripe number.
1987 * reshape_progress is a per-array sector number.
1988 * So divide by ndata * chunk_size
1990 unsigned long long start
= info
->reshape_progress
/ ndata
;
1991 start
/= (info
->array
.chunk_size
/512);
1992 done
= child_same_size(-1, info
, stripes
,
1995 info
->array
.raid_disks
,
1996 info
->array
.chunk_size
,
1997 info
->array
.level
, info
->array
.layout
,
1999 1, backup_list
, backup_offsets
);
2001 if (backup_file
&& done
)
2002 unlink(backup_file
);
2003 /* FIXME should I intuit a level change */
2006 fprintf(stderr
, Name
": Cannot run child to continue monitoring reshape: %s\n",