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
;
54 char *subarray
= NULL
;
56 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
57 fprintf(stderr
, Name
": cannot get array info for %s\n", devname
);
61 if (info
.array
.level
!= -1) {
62 fprintf(stderr
, Name
": can only add devices to linear arrays\n");
66 st
= super_by_fd(fd
, &subarray
);
68 fprintf(stderr
, Name
": cannot handle arrays with superblock version %d\n", info
.array
.major_version
);
73 fprintf(stderr
, Name
": Cannot grow linear sub-arrays yet\n");
78 nfd
= open(newdev
, O_RDWR
|O_EXCL
|O_DIRECT
);
80 fprintf(stderr
, Name
": cannot open %s\n", newdev
);
85 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
86 fprintf(stderr
, Name
": %s is not a block device!\n", newdev
);
91 /* now check out all the devices and make sure we can read the superblock */
92 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
96 st
->ss
->free_super(st
);
99 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
100 fprintf(stderr
, Name
": cannot get device detail for device %d\n",
106 dv
= map_dev(disk
.major
, disk
.minor
, 1);
108 fprintf(stderr
, Name
": cannot find device file for device %d\n",
114 fd2
= dev_open(dv
, O_RDWR
);
116 fprintf(stderr
, Name
": cannot open device file %s\n", dv
);
122 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
123 fprintf(stderr
, Name
": cannot find super block on %s\n", dv
);
131 /* Ok, looks good. Lets update the superblock and write it out to
135 info
.disk
.number
= d
;
136 info
.disk
.major
= major(stb
.st_rdev
);
137 info
.disk
.minor
= minor(stb
.st_rdev
);
138 info
.disk
.raid_disk
= d
;
139 info
.disk
.state
= (1 << MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
);
140 st
->ss
->update_super(st
, &info
, "linear-grow-new", newdev
,
143 if (st
->ss
->store_super(st
, nfd
)) {
144 fprintf(stderr
, Name
": Cannot store new superblock on %s\n",
151 if (ioctl(fd
, ADD_NEW_DISK
, &info
.disk
) != 0) {
152 fprintf(stderr
, Name
": Cannot add new disk to this array\n");
155 /* Well, that seems to have worked.
156 * Now go through and update all superblocks
159 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
160 fprintf(stderr
, Name
": cannot get array info for %s\n", devname
);
165 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
166 mdu_disk_info_t disk
;
170 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
171 fprintf(stderr
, Name
": cannot get device detail for device %d\n",
175 dv
= map_dev(disk
.major
, disk
.minor
, 1);
177 fprintf(stderr
, Name
": cannot find device file for device %d\n",
181 fd2
= dev_open(dv
, O_RDWR
);
183 fprintf(stderr
, Name
": cannot open device file %s\n", dv
);
186 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
187 fprintf(stderr
, Name
": cannot find super block on %s\n", dv
);
191 info
.array
.raid_disks
= nd
+1;
192 info
.array
.nr_disks
= nd
+1;
193 info
.array
.active_disks
= nd
+1;
194 info
.array
.working_disks
= nd
+1;
196 st
->ss
->update_super(st
, &info
, "linear-grow-update", dv
,
199 if (st
->ss
->store_super(st
, fd2
)) {
200 fprintf(stderr
, Name
": Cannot store new superblock on %s\n", dv
);
210 int Grow_addbitmap(char *devname
, int fd
, char *file
, int chunk
, int delay
, int write_behind
, int force
)
213 * First check that array doesn't have a bitmap
214 * Then create the bitmap
217 * For internal bitmaps, we need to check the version,
218 * find all the active devices, and write the bitmap block
221 mdu_bitmap_file_t bmf
;
222 mdu_array_info_t array
;
223 struct supertype
*st
;
224 char *subarray
= NULL
;
225 int major
= BITMAP_MAJOR_HI
;
226 int vers
= md_get_version(fd
);
227 unsigned long long bitmapsize
, array_size
;
230 major
= BITMAP_MAJOR_HOSTENDIAN
;
231 fprintf(stderr
, Name
": Warning - bitmaps created on this kernel"
232 " are not portable\n"
233 " between different architectures. Consider upgrading"
234 " the Linux kernel.\n");
237 if (ioctl(fd
, GET_BITMAP_FILE
, &bmf
) != 0) {
239 fprintf(stderr
, Name
": Memory allocation failure.\n");
241 fprintf(stderr
, Name
": bitmaps not supported by this kernel.\n");
244 if (bmf
.pathname
[0]) {
245 if (strcmp(file
,"none")==0) {
246 if (ioctl(fd
, SET_BITMAP_FILE
, -1)!= 0) {
247 fprintf(stderr
, Name
": failed to remove bitmap %s\n",
253 fprintf(stderr
, Name
": %s already has a bitmap (%s)\n",
254 devname
, bmf
.pathname
);
257 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
258 fprintf(stderr
, Name
": cannot get array status for %s\n", devname
);
261 if (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)) {
262 if (strcmp(file
, "none")==0) {
263 array
.state
&= ~(1<<MD_SB_BITMAP_PRESENT
);
264 if (ioctl(fd
, SET_ARRAY_INFO
, &array
)!= 0) {
265 fprintf(stderr
, Name
": failed to remove internal bitmap.\n");
270 fprintf(stderr
, Name
": Internal bitmap already present on %s\n",
275 if (strcmp(file
, "none") == 0) {
276 fprintf(stderr
, Name
": no bitmap found on %s\n", devname
);
279 if (array
.level
<= 0) {
280 fprintf(stderr
, Name
": Bitmaps not meaningful with level %s\n",
281 map_num(pers
, array
.level
)?:"of this array");
284 bitmapsize
= array
.size
;
286 if (get_dev_size(fd
, NULL
, &array_size
) &&
287 array_size
> (0x7fffffffULL
<<9)) {
288 /* Array is big enough that we cannot trust array.size
289 * try other approaches
291 bitmapsize
= get_component_size(fd
);
293 if (bitmapsize
== 0) {
294 fprintf(stderr
, Name
": Cannot reliably determine size of array to create bitmap - sorry.\n");
298 if (array
.level
== 10) {
299 int ncopies
= (array
.layout
&255)*((array
.layout
>>8)&255);
300 bitmapsize
= bitmapsize
* array
.raid_disks
/ ncopies
;
303 st
= super_by_fd(fd
, &subarray
);
305 fprintf(stderr
, Name
": Cannot understand version %d.%d\n",
306 array
.major_version
, array
.minor_version
);
310 fprintf(stderr
, Name
": Cannot add bitmaps to sub-arrays yet\n");
315 if (strcmp(file
, "internal") == 0) {
317 if (st
->ss
->add_internal_bitmap
== NULL
) {
318 fprintf(stderr
, Name
": Internal bitmaps not supported "
319 "with %s metadata\n", st
->ss
->name
);
322 for (d
=0; d
< st
->max_devs
; d
++) {
323 mdu_disk_info_t disk
;
326 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
328 if (disk
.major
== 0 &&
331 if ((disk
.state
& (1<<MD_DISK_SYNC
))==0)
333 dv
= map_dev(disk
.major
, disk
.minor
, 1);
335 int fd2
= dev_open(dv
, O_RDWR
);
338 if (st
->ss
->load_super(st
, fd2
, NULL
)==0) {
339 if (st
->ss
->add_internal_bitmap(
341 &chunk
, delay
, write_behind
,
342 bitmapsize
, 0, major
)
344 st
->ss
->write_bitmap(st
, fd2
);
346 fprintf(stderr
, Name
": failed to create internal bitmap - chunksize problem.\n");
354 array
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
355 if (ioctl(fd
, SET_ARRAY_INFO
, &array
)!= 0) {
358 ": Cannot add bitmap while array is"
359 " resyncing or reshaping etc.\n");
360 fprintf(stderr
, Name
": failed to set internal bitmap.\n");
367 int max_devs
= st
->max_devs
;
369 /* try to load a superblock */
370 for (d
=0; d
<max_devs
; d
++) {
371 mdu_disk_info_t disk
;
375 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
377 if ((disk
.major
==0 && disk
.minor
==0) ||
378 (disk
.state
& (1<<MD_DISK_REMOVED
)))
380 dv
= map_dev(disk
.major
, disk
.minor
, 1);
382 fd2
= dev_open(dv
, O_RDONLY
);
384 st
->ss
->load_super(st
, fd2
, NULL
) == 0) {
386 st
->ss
->uuid_from_super(st
, uuid
);
392 fprintf(stderr
, Name
": cannot find UUID for array!\n");
395 if (CreateBitmap(file
, force
, (char*)uuid
, chunk
,
396 delay
, write_behind
, bitmapsize
, major
)) {
399 bitmap_fd
= open(file
, O_RDWR
);
401 fprintf(stderr
, Name
": weird: %s cannot be opened\n",
405 if (ioctl(fd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
409 ": Cannot add bitmap while array is"
410 " resyncing or reshaping etc.\n");
411 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
412 devname
, strerror(err
));
422 * When reshaping an array we might need to backup some data.
423 * This is written to all spares with a 'super_block' describing it.
424 * The superblock goes 4K from the end of the used space on the
426 * It if written after the backup is complete.
427 * It has the following structure.
430 static struct mdp_backup_super
{
431 char magic
[16]; /* md_backup_data-1 or -2 */
434 /* start/sizes in 512byte sectors */
435 __u64 devstart
; /* address on backup device/file of data */
438 __u32 sb_csum
; /* csum of preceeding bytes. */
440 __u64 devstart2
; /* offset in to data of second section */
443 __u32 sb_csum2
; /* csum of preceeding bytes. */
445 } __attribute__((aligned(512))) bsb
, bsb2
;
447 static __u32
bsb_csum(char *buf
, int len
)
451 for (i
=0; i
<len
; i
++)
452 csum
= (csum
<<3) + buf
[0];
453 return __cpu_to_le32(csum
);
456 static int child_grow(int afd
, struct mdinfo
*sra
, unsigned long blocks
,
457 int *fds
, unsigned long long *offsets
,
458 int disks
, int chunk
, int level
, int layout
, int data
,
459 int dests
, int *destfd
, unsigned long long *destoffsets
);
460 static int child_shrink(int afd
, struct mdinfo
*sra
, unsigned long blocks
,
461 int *fds
, unsigned long long *offsets
,
462 int disks
, int chunk
, int level
, int layout
, int data
,
463 int dests
, int *destfd
, unsigned long long *destoffsets
);
464 static int child_same_size(int afd
, struct mdinfo
*sra
, unsigned long blocks
,
465 int *fds
, unsigned long long *offsets
,
466 unsigned long long start
,
467 int disks
, int chunk
, int level
, int layout
, int data
,
468 int dests
, int *destfd
, unsigned long long *destoffsets
);
470 static int freeze_container(struct supertype
*st
)
472 int container_dev
= (st
->container_dev
!= NoMdDev
473 ? st
->container_dev
: st
->devnum
);
474 char *container
= devnum2devname(container_dev
);
478 ": could not determine container name, freeze aborted\n");
482 if (block_monitor(container
, 1)) {
483 fprintf(stderr
, Name
": failed to freeze container\n");
490 static void unfreeze_container(struct supertype
*st
)
492 int container_dev
= (st
->container_dev
!= NoMdDev
493 ? st
->container_dev
: st
->devnum
);
494 char *container
= devnum2devname(container_dev
);
498 ": could not determine container name, unfreeze aborted\n");
502 unblock_monitor(container
, 1);
505 static int freeze(struct supertype
*st
)
507 /* Try to freeze resync/rebuild on this array/container.
508 * Return -1 if the array is busy,
509 * return -2 container cannot be frozen,
510 * return 0 if this kernel doesn't support 'frozen'
511 * return 1 if it worked.
513 if (st
->ss
->external
)
514 return freeze_container(st
);
516 struct mdinfo
*sra
= sysfs_read(-1, st
->devnum
, GET_VERSION
);
521 err
= sysfs_freeze_array(sra
);
527 static void unfreeze(struct supertype
*st
, int frozen
)
529 /* If 'frozen' is 1, unfreeze the array */
533 if (st
->ss
->external
)
534 return unfreeze_container(st
);
536 struct mdinfo
*sra
= sysfs_read(-1, st
->devnum
, GET_VERSION
);
539 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
541 fprintf(stderr
, Name
": failed to unfreeze array\n");
546 static void wait_reshape(struct mdinfo
*sra
)
548 int fd
= sysfs_get_fd(sra
, NULL
, "sync_action");
555 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
557 if (sysfs_fd_get_str(fd
, action
, 20) < 0) {
561 } while (strncmp(action
, "reshape", 7) == 0);
564 static int reshape_super(struct supertype
*st
, long long size
, int level
,
565 int layout
, int chunksize
, int raid_disks
,
566 char *backup_file
, char *dev
, int verbose
)
568 /* nothing extra to check in the native case */
569 if (!st
->ss
->external
)
571 if (!st
->ss
->reshape_super
||
572 !st
->ss
->manage_reshape
) {
573 fprintf(stderr
, Name
": %s metadata does not support reshape\n",
578 return st
->ss
->reshape_super(st
, size
, level
, layout
, chunksize
,
579 raid_disks
, backup_file
, dev
, verbose
);
582 static void sync_metadata(struct supertype
*st
)
584 if (st
->ss
->external
) {
586 flush_metadata_updates(st
);
588 st
->ss
->sync_metadata(st
);
592 static int subarray_set_num(char *container
, struct mdinfo
*sra
, char *name
, int n
)
594 /* when dealing with external metadata subarrays we need to be
595 * prepared to handle EAGAIN. The kernel may need to wait for
596 * mdmon to mark the array active so the kernel can handle
597 * allocations/writeback when preparing the reshape action
598 * (md_allow_write()). We temporarily disable safe_mode_delay
599 * to close a race with the array_state going clean before the
600 * next write to raid_disks / stripe_cache_size
605 /* only 'raid_disks' and 'stripe_cache_size' trigger md_allow_write */
606 if (strcmp(name
, "raid_disks") != 0 &&
607 strcmp(name
, "stripe_cache_size") != 0)
608 return sysfs_set_num(sra
, NULL
, name
, n
);
610 rc
= sysfs_get_str(sra
, NULL
, "safe_mode_delay", safe
, sizeof(safe
));
613 sysfs_set_num(sra
, NULL
, "safe_mode_delay", 0);
614 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
615 if (rc
< 0 && errno
== EAGAIN
) {
616 ping_monitor(container
);
617 /* if we get EAGAIN here then the monitor is not active
620 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
622 sysfs_set_str(sra
, NULL
, "safe_mode_delay", safe
);
626 static int reshape_container_raid_disks(char *container
, int raid_disks
)
628 /* for each subarray switch to a raid level that can
629 * support the reshape, and set raid disks
631 struct mdstat_ent
*ent
, *e
;
632 int changed
= 0, rv
= 0, err
= 0;
634 ent
= mdstat_read(1, 0);
636 fprintf(stderr
, Name
": unable to read /proc/mdstat\n");
641 for (e
= ent
; e
; e
= e
->next
) {
644 int level
, takeover_delta
= 0;
646 if (!is_container_member(e
, container
))
649 level
= map_name(pers
, e
->level
);
651 sub
= sysfs_read(-1, e
->devnum
, GET_VERSION
);
654 /* metadata records 'orig_level' */
655 rv
= sysfs_set_num(sub
, NULL
, "level", 4);
660 /* we want spares to be used for capacity
661 * expansion, not rebuild
677 sub
= sysfs_read(-1, e
->devnum
, GET_CHUNK
|GET_CACHE
);
680 cache
= (sub
->array
.chunk_size
/ 4096) * 4;
681 if (cache
> sub
->cache_size
)
682 rv
= subarray_set_num(container
, sub
,
683 "stripe_cache_size", cache
);
691 sub
= sysfs_read(-1, e
->devnum
, GET_VERSION
);
695 rv
= subarray_set_num(container
, sub
, "raid_disks",
696 raid_disks
+ takeover_delta
);
710 ": failed to initiate container reshape%s%s\n",
711 err
? ": " : "", err
? strerror(err
) : "");
718 static void revert_container_raid_disks(struct supertype
*st
, int fd
, char *container
)
720 /* we failed to prepare all subarrays in the container for
721 * reshape, so cancel the changes and restore the nominal raid
724 struct mdstat_ent
*ent
, *e
;
726 ent
= mdstat_read(0, 0);
729 ": failed to read /proc/mdstat while aborting reshape\n");
733 if (st
->ss
->load_container(st
, fd
, NULL
)) {
735 ": failed read metadata while aborting reshape\n");
740 for (e
= ent
; e
; e
= e
->next
) {
741 int level_fixed
= 0, disks_fixed
= 0;
742 struct mdinfo
*sub
, *prev
;
745 if (!is_container_member(e
, container
))
748 subarray
= to_subarray(e
, container
);
749 prev
= st
->ss
->container_content(st
, subarray
);
751 /* changing level might change raid_disks so we do it
752 * first and then check if raid_disks still needs fixing
754 if (map_name(pers
, e
->level
) != prev
->array
.level
) {
755 sub
= sysfs_read(-1, e
->devnum
, GET_VERSION
);
757 !sysfs_set_num(sub
, NULL
, "level", prev
->array
.level
))
763 sub
= sysfs_read(-1, e
->devnum
, GET_DISKS
);
764 if (sub
&& sub
->array
.raid_disks
!= prev
->array
.raid_disks
) {
765 if (!subarray_set_num(container
, sub
, "raid_disks",
766 prev
->array
.raid_disks
))
772 if (!disks_fixed
|| !level_fixed
)
774 ": failed to restore %s to a %d-disk %s array\n",
775 e
->dev
, prev
->array
.raid_disks
,
776 map_num(pers
, prev
->array
.level
));
779 st
->ss
->free_super(st
);
783 int remove_disks_on_raid10_to_raid0_takeover(struct supertype
*st
,
788 struct mdinfo
*remaining
;
791 nr_of_copies
= layout
& 0xff;
793 remaining
= sra
->devs
;
795 /* for each 'copy', select one device and remove from the list. */
796 for (slot
= 0; slot
< sra
->array
.raid_disks
; slot
+= nr_of_copies
) {
797 struct mdinfo
**diskp
;
800 /* Find a working device to keep */
801 for (diskp
= &remaining
; *diskp
; diskp
= &(*diskp
)->next
) {
802 struct mdinfo
*disk
= *diskp
;
804 if (disk
->disk
.raid_disk
< slot
)
806 if (disk
->disk
.raid_disk
>= slot
+ nr_of_copies
)
808 if (disk
->disk
.state
& (1<<MD_DISK_REMOVED
))
810 if (disk
->disk
.state
& (1<<MD_DISK_FAULTY
))
812 if (!(disk
->disk
.state
& (1<<MD_DISK_SYNC
)))
815 /* We have found a good disk to use! */
817 disk
->next
= sra
->devs
;
826 if (slot
< sra
->array
.raid_disks
) {
827 /* didn't find all slots */
833 sra
->devs
= remaining
;
837 /* Remove all 'remaining' devices from the array */
839 struct mdinfo
*sd
= remaining
;
840 remaining
= sd
->next
;
842 sysfs_set_str(sra
, sd
, "state", "faulty");
843 sysfs_set_str(sra
, sd
, "slot", "none");
844 sysfs_set_str(sra
, sd
, "state", "remove");
845 sd
->disk
.state
|= (1<<MD_DISK_REMOVED
);
846 sd
->disk
.state
&= ~(1<<MD_DISK_SYNC
);
847 sd
->next
= sra
->devs
;
853 void reshape_free_fdlist(int *fdlist
,
854 unsigned long long *offsets
,
859 for (i
= 0; i
< size
; i
++)
867 int reshape_prepare_fdlist(char *devname
,
871 unsigned long blocks
,
874 unsigned long long *offsets
)
879 for (d
= 0; d
<= nrdisks
; d
++)
882 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
883 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
885 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
886 char *dn
= map_dev(sd
->disk
.major
,
888 fdlist
[sd
->disk
.raid_disk
]
889 = dev_open(dn
, O_RDONLY
);
890 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
891 if (fdlist
[sd
->disk
.raid_disk
] < 0) {
893 Name
": %s: cannot open component %s\n",
894 devname
, dn
? dn
: "-unknown-");
898 } else if (backup_file
== NULL
) {
900 char *dn
= map_dev(sd
->disk
.major
,
902 fdlist
[d
] = dev_open(dn
, O_RDWR
);
903 offsets
[d
] = (sd
->data_offset
+ sra
->component_size
- blocks
- 8)*512;
905 fprintf(stderr
, Name
": %s: cannot open component %s\n",
906 devname
, dn
? dn
: "-unknown-");
917 int reshape_open_backup_file(char *backup_file
,
922 unsigned long long *offsets
)
924 /* Return 1 on success, 0 on any form of failure */
925 /* need to check backup file is large enough */
931 *fdlist
= open(backup_file
, O_RDWR
|O_CREAT
|O_EXCL
,
935 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
936 devname
, backup_file
, strerror(errno
));
939 /* Guard against backup file being on array device.
940 * If array is partitioned or if LVM etc is in the
941 * way this will not notice, but it is better than
944 fstat(*fdlist
, &stb
);
947 if (stb
.st_rdev
== dev
) {
948 fprintf(stderr
, Name
": backup file must NOT be"
949 " on the array being reshaped.\n");
955 for (i
=0; i
< blocks
+ 1 ; i
++) {
956 if (write(*fdlist
, buf
, 512) != 512) {
957 fprintf(stderr
, Name
": %s: cannot create"
958 " backup file %s: %s\n",
959 devname
, backup_file
, strerror(errno
));
963 if (fsync(*fdlist
) != 0) {
964 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
965 devname
, backup_file
, strerror(errno
));
972 unsigned long compute_backup_blocks(int nchunk
, int ochunk
,
973 unsigned int ndata
, unsigned int odata
)
975 unsigned long a
, b
, blocks
;
976 /* So how much do we need to backup.
977 * We need an amount of data which is both a whole number of
978 * old stripes and a whole number of new stripes.
979 * So LCM for (chunksize*datadisks).
981 a
= (ochunk
/512) * odata
;
982 b
= (nchunk
/512) * ndata
;
990 /* LCM == product / GCD */
991 blocks
= (ochunk
/512) * (nchunk
/512) * odata
* ndata
/ a
;
997 int Grow_reshape(char *devname
, int fd
, int quiet
, char *backup_file
,
999 int level
, char *layout_str
, int chunksize
, int raid_disks
)
1001 /* Make some changes in the shape of an array.
1002 * The kernel must support the change.
1004 * There are three different changes. Each can trigger
1005 * a resync or recovery so we freeze that until we have
1006 * requested everything (if kernel supports freezing - 2.6.30).
1008 * - change size (i.e. component_size)
1010 * - change layout/chunksize/ndisks
1012 * The last can require a reshape. It is different on different
1013 * levels so we need to check the level before actioning it.
1014 * Some times the level change needs to be requested after the
1015 * reshape (e.g. raid6->raid5, raid5->raid0)
1018 struct mdu_array_info_s array
, orig
;
1021 struct supertype
*st
;
1022 char *subarray
= NULL
;
1025 int nlayout
, olayout
;
1027 unsigned int ndata
, odata
;
1028 int orig_level
= UnSet
;
1029 char alt_layout
[40];
1031 unsigned long long *offsets
;
1036 unsigned long blocks
, stripes
;
1037 unsigned long cache
;
1038 unsigned long long array_size
;
1040 char *container
= NULL
;
1047 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) < 0) {
1048 fprintf(stderr
, Name
": %s is not an active md array - aborting\n",
1054 (chunksize
|| level
!= UnSet
|| layout_str
|| raid_disks
)) {
1055 fprintf(stderr
, Name
": cannot change component size at the same time "
1056 "as other changes.\n"
1057 " Change size first, then check data is intact before "
1058 "making other changes.\n");
1062 if (raid_disks
&& raid_disks
< array
.raid_disks
&& array
.level
> 1 &&
1063 get_linux_version() < 2006032 &&
1064 !check_env("MDADM_FORCE_FEWER")) {
1065 fprintf(stderr
, Name
": reducing the number of devices is not safe before Linux 2.6.32\n"
1066 " Please use a newer kernel\n");
1070 st
= super_by_fd(fd
, &subarray
);
1072 fprintf(stderr
, Name
": Unable to determine metadata format for %s\n", devname
);
1075 if (raid_disks
> st
->max_devs
) {
1076 fprintf(stderr
, Name
": Cannot increase raid-disks on this array"
1077 " beyond %d\n", st
->max_devs
);
1081 /* in the external case we need to check that the requested reshape is
1082 * supported, and perform an initial check that the container holds the
1083 * pre-requisite spare devices (mdmon owns final validation)
1085 if (st
->ss
->external
) {
1090 container_dev
= st
->container_dev
;
1091 cfd
= open_dev_excl(st
->container_dev
);
1092 } else if (size
>= 0 || layout_str
!= NULL
|| chunksize
!= 0 ||
1095 Name
": %s is a container, only 'raid-devices' can be changed\n",
1099 container_dev
= st
->devnum
;
1101 cfd
= open_dev_excl(st
->devnum
);
1105 fprintf(stderr
, Name
": Unable to open container for %s\n",
1111 container
= devnum2devname(st
->devnum
);
1113 fprintf(stderr
, Name
": Could not determine container name\n");
1119 rv
= st
->ss
->load_container(st
, cfd
, NULL
);
1121 rv
= st
->ss
->load_super(st
, cfd
, NULL
);
1123 fprintf(stderr
, Name
": Cannot read superblock for %s\n",
1129 if (mdmon_running(container_dev
))
1130 st
->update_tail
= &st
->updates
;
1133 sra
= sysfs_read(fd
, 0, GET_LEVEL
| GET_DISKS
| GET_DEVS
| GET_STATE
);
1135 if (st
->ss
->external
&& subarray
== NULL
) {
1136 array
.level
= LEVEL_CONTAINER
;
1137 sra
->array
.level
= LEVEL_CONTAINER
;
1140 fprintf(stderr
, Name
": failed to read sysfs parameters for %s\n",
1144 frozen
= freeze(st
);
1146 /* freeze() already spewed the reason */
1148 } else if (frozen
< 0) {
1149 fprintf(stderr
, Name
": %s is performing resync/recovery and cannot"
1150 " be reshaped\n", devname
);
1154 /* ========= set size =============== */
1155 if (size
>= 0 && (size
== 0 || size
!= array
.size
)) {
1156 long long orig_size
= array
.size
;
1158 if (reshape_super(st
, size
, UnSet
, UnSet
, 0, 0, NULL
, devname
, !quiet
)) {
1164 if (array
.size
!= size
) {
1165 /* got truncated to 32bit, write to
1166 * component_size instead
1169 rv
= sysfs_set_num(sra
, NULL
,
1170 "component_size", size
);
1174 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
1178 /* restore metadata */
1179 if (reshape_super(st
, orig_size
, UnSet
, UnSet
, 0, 0,
1180 NULL
, devname
, !quiet
) == 0)
1182 fprintf(stderr
, Name
": Cannot set device size for %s: %s\n",
1183 devname
, strerror(err
));
1185 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1186 fprintf(stderr
, " Bitmap must be removed before size can be changed\n");
1190 ioctl(fd
, GET_ARRAY_INFO
, &array
);
1191 size
= get_component_size(fd
)/2;
1195 fprintf(stderr
, Name
": component size of %s has been set to %lluK\n",
1198 } else if (array
.level
!= LEVEL_CONTAINER
) {
1199 size
= get_component_size(fd
)/2;
1204 /* ========= check for Raid10 -> Raid0 conversion ===============
1205 * current implemenation assumes that following conditions must be met:
1207 * - near_copies == 2
1209 if (level
== 0 && array
.level
== 10 &&
1210 array
.layout
== ((1 << 8) + 2) && !(array
.raid_disks
& 1)) {
1212 err
= remove_disks_on_raid10_to_raid0_takeover(st
, sra
, array
.layout
);
1214 dprintf(Name
": Array cannot be reshaped\n");
1223 /* ======= set level =========== */
1224 if (level
!= UnSet
&& level
!= array
.level
) {
1225 /* Trying to change the level.
1226 * We might need to change layout first and schedule a
1227 * level change for later.
1228 * Level changes that can happen immediately are:
1229 * 0->4,5,6 1->5 4->5,6 5->1,6
1230 * Level changes that need a layout change first are:
1231 * 6->5,4,0 : need a -6 layout, or parity-last
1232 * 5->4,0 : need parity-last
1234 if ((array
.level
== 6 || array
.level
== 5) &&
1235 (level
== 5 || level
== 4 || level
== 0)) {
1236 /* Don't change level yet, but choose intermediate
1240 if (layout_str
== NULL
)
1241 switch (array
.layout
) {
1242 case ALGORITHM_LEFT_ASYMMETRIC
:
1243 case ALGORITHM_LEFT_ASYMMETRIC_6
:
1244 case ALGORITHM_ROTATING_N_RESTART
:
1245 layout_str
= "left-asymmetric-6";
1247 case ALGORITHM_LEFT_SYMMETRIC
:
1248 case ALGORITHM_LEFT_SYMMETRIC_6
:
1249 case ALGORITHM_ROTATING_N_CONTINUE
:
1250 layout_str
= "left-symmetric-6";
1252 case ALGORITHM_RIGHT_ASYMMETRIC
:
1253 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
1254 case ALGORITHM_ROTATING_ZERO_RESTART
:
1255 layout_str
= "right-asymmetric-6";
1257 case ALGORITHM_RIGHT_SYMMETRIC
:
1258 case ALGORITHM_RIGHT_SYMMETRIC_6
:
1259 layout_str
= "right-symmetric-6";
1261 case ALGORITHM_PARITY_0
:
1262 case ALGORITHM_PARITY_0_6
:
1263 layout_str
= "parity-first-6";
1265 case ALGORITHM_PARITY_N
:
1266 layout_str
= "parity-last";
1269 fprintf(stderr
, Name
": %s: cannot"
1270 "convert layout to RAID5 equivalent\n",
1276 int l
= map_name(r5layout
, layout_str
);
1278 fprintf(stderr
, Name
": %s: layout '%s' not recognised\n",
1279 devname
, layout_str
);
1283 if (l
!= ALGORITHM_PARITY_N
) {
1284 /* need the -6 version */
1285 char *ls
= map_num(r5layout
, l
);
1286 strcat(strcpy(alt_layout
, ls
),
1288 layout_str
= alt_layout
;
1292 /* The final raid6->raid5 conversion
1293 * will reduce the number of disks,
1294 * so now we need to aim higher
1298 layout_str
= "parity-last";
1300 /* Level change is a simple takeover. In the external
1301 * case we don't check with the metadata handler until
1302 * we establish what the final layout will be. If the
1303 * level change is disallowed we will revert to
1304 * orig_level without disturbing the metadata, otherwise
1305 * we will send an update.
1307 c
= map_num(pers
, level
);
1309 rv
= 1;/* not possible */
1312 err
= sysfs_set_str(sra
, NULL
, "level", c
);
1315 fprintf(stderr
, Name
": %s: could not set level to %s\n",
1318 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1319 fprintf(stderr
, " Bitmap must be removed before level can be changed\n");
1324 orig_level
= orig
.level
;
1325 ioctl(fd
, GET_ARRAY_INFO
, &array
);
1326 if (layout_str
== NULL
&&
1327 orig
.level
== 5 && level
== 6 &&
1328 array
.layout
!= orig
.layout
)
1329 layout_str
= map_num(r5layout
, orig
.layout
);
1331 fprintf(stderr
, Name
" level of %s changed to %s\n",
1337 /* ========= set shape (chunk_size / layout / ndisks) ============== */
1338 /* Check if layout change is a no-op */
1339 switch (array
.level
) {
1341 if (layout_str
&& array
.layout
== map_name(r5layout
, layout_str
))
1345 if (layout_str
== NULL
&&
1346 ((chunksize
&& chunksize
* 1024 != array
.chunk_size
) ||
1347 (raid_disks
&& raid_disks
!= array
.raid_disks
)) &&
1348 array
.layout
>= 16) {
1349 fprintf(stderr
, Name
1350 ": %s has a non-standard layout. If you wish to preserve this\n"
1351 " during the reshape, please specify --layout=preserve\n"
1352 " If you want to change it, specify a layout or use --layout=normalise\n",
1358 (strcmp(layout_str
, "normalise") == 0 ||
1359 strcmp(layout_str
, "normalize") == 0)) {
1361 strcpy(alt_layout
, map_num(r6layout
, array
.layout
));
1362 hyphen
= strrchr(alt_layout
, '-');
1363 if (hyphen
&& strcmp(hyphen
, "-6") == 0) {
1365 layout_str
= alt_layout
;
1369 if (layout_str
&& array
.layout
== map_name(r6layout
, layout_str
))
1371 if (layout_str
&& strcmp(layout_str
, "preserve") == 0)
1375 if (layout_str
== NULL
1376 && (chunksize
== 0 || chunksize
*1024 == array
.chunk_size
)
1377 && (raid_disks
== 0 || raid_disks
== array
.raid_disks
)) {
1378 if (reshape_super(st
, -1, level
, UnSet
, 0, 0, NULL
, devname
, !quiet
)) {
1384 if (level
!= UnSet
&& level
!= array
.level
) {
1385 /* Looks like this level change doesn't need
1386 * a reshape after all.
1388 c
= map_num(pers
, level
);
1390 rv
= sysfs_set_str(sra
, NULL
, "level", c
);
1393 fprintf(stderr
, Name
": %s: could not set level to %s\n",
1396 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1397 fprintf(stderr
, " Bitmap must be removed before level can be changed\n");
1401 } else if (!changed
&& !quiet
)
1402 fprintf(stderr
, Name
": %s: no change requested\n",
1405 if (st
->ss
->external
&& !mdmon_running(st
->container_dev
) &&
1407 start_mdmon(st
->container_dev
);
1408 ping_monitor(container
);
1413 c
= map_num(pers
, array
.level
);
1414 if (c
== NULL
) c
= "-unknown-";
1415 switch (array
.level
) {
1416 default: /* raid0, linear, multipath cannot be reconfigured */
1417 fprintf(stderr
, Name
": %s array %s cannot be reshaped.\n",
1419 /* TODO raid0 raiddisks can be reshaped via raid4 */
1422 case LEVEL_CONTAINER
: {
1425 /* double check that we are not changing anything but raid_disks */
1426 if (size
>= 0 || layout_str
!= NULL
|| chunksize
!= 0 || level
!= UnSet
) {
1428 Name
": %s is a container, only 'raid-devices' can be changed\n",
1434 st
->update_tail
= &st
->updates
;
1435 if (reshape_super(st
, -1, UnSet
, UnSet
, 0, raid_disks
,
1436 backup_file
, devname
, !quiet
)) {
1441 count
= reshape_container_raid_disks(container
, raid_disks
);
1443 revert_container_raid_disks(st
, fd
, container
);
1446 } else if (count
== 0) {
1448 fprintf(stderr
, Name
1449 ": no active subarrays to reshape\n");
1453 if (!mdmon_running(st
->devnum
)) {
1454 start_mdmon(st
->devnum
);
1455 ping_monitor(container
);
1459 /* give mdmon a chance to allocate spares */
1460 ping_manager(container
);
1462 /* manage_reshape takes care of releasing the array(s) */
1463 st
->ss
->manage_reshape(st
, backup_file
);
1467 case LEVEL_FAULTY
: /* only 'layout' change is permitted */
1469 if (chunksize
|| raid_disks
) {
1470 fprintf(stderr
, Name
": %s: Cannot change chunksize or disks of a 'faulty' array\n",
1475 if (layout_str
== NULL
)
1476 break; /* nothing to do.... */
1478 array
.layout
= parse_layout_faulty(layout_str
);
1479 if (array
.layout
< 0) {
1480 fprintf(stderr
, Name
": %s: layout %s not understood for 'faulty' array\n",
1481 devname
, layout_str
);
1485 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
1486 fprintf(stderr
, Name
": Cannot set layout for %s: %s\n",
1487 devname
, strerror(errno
));
1490 printf("layout for %s set to %d\n", devname
, array
.layout
);
1493 case 1: /* only raid_disks can each be changed. */
1495 if (chunksize
|| layout_str
!= NULL
) {
1496 fprintf(stderr
, Name
": %s: Cannot change chunk size or layout for a RAID1 array.\n",
1501 if (raid_disks
> 0) {
1502 if (reshape_super(st
, -1, UnSet
, UnSet
, 0, raid_disks
,
1503 NULL
, devname
, !quiet
)) {
1508 array
.raid_disks
= raid_disks
;
1509 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
1510 fprintf(stderr
, Name
": Cannot set raid-devices for %s: %s\n",
1511 devname
, strerror(errno
));
1522 * layout/chunksize/raid_disks can be changed
1523 * though the kernel may not support it all.
1526 fprintf(stderr
, Name
": Cannot reshape subarrays yet\n");
1531 * There are three possibilities.
1532 * 1/ The array will shrink.
1533 * We need to ensure the reshape will pause before reaching
1534 * the 'critical section'. We also need to fork and wait for
1535 * that to happen. When it does we
1536 * suspend/backup/complete/unfreeze
1538 * 2/ The array will not change size.
1539 * This requires that we keep a backup of a sliding window
1540 * so that we can restore data after a crash. So we need
1541 * to fork and monitor progress.
1543 * 3/ The array will grow. This is relatively easy.
1544 * However the kernel's restripe routines will cheerfully
1545 * overwrite some early data before it is safe. So we
1546 * need to make a backup of the early parts of the array
1547 * and be ready to restore it if rebuild aborts very early.
1549 * We backup data by writing it to one spare, or to a
1550 * file which was given on command line.
1552 * [FOLLOWING IS OLD AND PARTLY WRONG]
1553 * So: we enumerate the devices in the array and
1554 * make sure we can open all of them.
1555 * Then we freeze the early part of the array and
1556 * backup to the various spares.
1557 * Then we request changes and start the reshape.
1558 * Monitor progress until it has passed the danger zone.
1559 * and finally invalidate the copied data and unfreeze the
1560 * start of the array.
1562 * In each case, we first make sure that storage is available
1563 * for the required backup.
1565 * - request the shape change.
1566 * - for to handle backup etc.
1568 nchunk
= ochunk
= array
.chunk_size
;
1569 nlayout
= olayout
= array
.layout
;
1570 ndisks
= odisks
= array
.raid_disks
;
1573 nchunk
= chunksize
* 1024;
1574 if (size
% chunksize
) {
1575 fprintf(stderr
, Name
": component size %lluK is not"
1576 " a multiple of chunksize %dK\n",
1581 if (layout_str
!= NULL
)
1582 switch(array
.level
) {
1583 case 4: /* ignore layout */
1586 nlayout
= map_name(r5layout
, layout_str
);
1587 if (nlayout
== UnSet
) {
1588 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
1596 nlayout
= map_name(r6layout
, layout_str
);
1597 if (nlayout
== UnSet
) {
1598 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
1605 if (raid_disks
) ndisks
= raid_disks
;
1609 if (array
.level
== 6) {
1610 odata
--; /* number of data disks */
1614 if (odata
== ndata
&&
1615 get_linux_version() < 2006032) {
1616 fprintf(stderr
, Name
": in-place reshape is not safe before 2.6.32, sorry.\n");
1620 /* Check that we can hold all the data */
1621 get_dev_size(fd
, NULL
, &array_size
);
1622 if (ndata
* (unsigned long long)size
< (array_size
/1024)) {
1623 fprintf(stderr
, Name
": this change will reduce the size of the array.\n"
1624 " use --grow --array-size first to truncate array.\n"
1625 " e.g. mdadm --grow %s --array-size %llu\n",
1626 devname
, ndata
* size
);
1631 blocks
= compute_backup_blocks(nchunk
, ochunk
, ndata
, odata
);
1634 sra
= sysfs_read(fd
, 0,
1635 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
1639 fprintf(stderr
, Name
": %s: Cannot get array details from sysfs\n",
1645 if (ndata
== odata
) {
1646 /* Make 'blocks' bigger for better throughput, but
1647 * not so big that we reject it below.
1648 * Try for 16 megabytes
1650 while (blocks
* 32 < sra
->component_size
&&
1654 fprintf(stderr
, Name
": Need to backup %luK of critical "
1655 "section..\n", blocks
/2);
1657 if (blocks
>= sra
->component_size
/2) {
1658 fprintf(stderr
, Name
": %s: Something wrong - reshape aborted\n",
1663 nrdisks
= array
.raid_disks
+ sra
->array
.spare_disks
;
1664 /* Now we need to open all these devices so we can read/write.
1666 fdlist
= malloc((1+nrdisks
) * sizeof(int));
1667 offsets
= malloc((1+nrdisks
) * sizeof(offsets
[0]));
1668 if (!fdlist
|| !offsets
) {
1669 fprintf(stderr
, Name
": malloc failed: grow aborted\n");
1674 d
= reshape_prepare_fdlist(devname
, sra
, array
.raid_disks
,
1675 nrdisks
, blocks
, backup_file
,
1681 if (backup_file
== NULL
) {
1682 if (st
->ss
->external
&& !st
->ss
->manage_reshape
) {
1683 fprintf(stderr
, Name
": %s Grow operation not supported by %s metadata\n",
1684 devname
, st
->ss
->name
);
1688 if (ndata
<= odata
) {
1689 fprintf(stderr
, Name
": %s: Cannot grow - need backup-file\n",
1693 } else if (sra
->array
.spare_disks
== 0) {
1694 fprintf(stderr
, Name
": %s: Cannot grow - need a spare or "
1695 "backup-file to backup critical section\n",
1700 if (d
== array
.raid_disks
) {
1701 fprintf(stderr
, Name
": %s: No spare device for backup\n",
1707 if (!reshape_open_backup_file(backup_file
, fd
, devname
,
1709 fdlist
+d
, offsets
+d
)) {
1716 /* check that the operation is supported by the metadata */
1717 if (reshape_super(st
, -1, level
, nlayout
, nchunk
, ndisks
,
1718 backup_file
, devname
, !quiet
)) {
1723 /* lastly, check that the internal stripe cache is
1724 * large enough, or it won't work.
1727 cache
= (nchunk
< ochunk
) ? ochunk
: nchunk
;
1728 cache
= cache
* 4 / 4096;
1729 if (cache
< blocks
/ 8 / odisks
+ 16)
1730 /* Make it big enough to hold 'blocks' */
1731 cache
= blocks
/ 8 / odisks
+ 16;
1732 if (sra
->cache_size
< cache
)
1733 sysfs_set_num(sra
, NULL
, "stripe_cache_size",
1735 /* Right, everything seems fine. Let's kick things off.
1736 * If only changing raid_disks, use ioctl, else use
1740 if (ochunk
== nchunk
&& olayout
== nlayout
) {
1741 array
.raid_disks
= ndisks
;
1742 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
1745 fprintf(stderr
, Name
": Cannot set device shape for %s: %s\n",
1746 devname
, strerror(errno
));
1747 if (ndisks
< odisks
&&
1748 get_linux_version() < 2006030)
1749 fprintf(stderr
, Name
": linux 2.6.30 or later required\n");
1751 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1752 fprintf(stderr
, " Bitmap must be removed before shape can be changed\n");
1757 /* set them all just in case some old 'new_*' value
1758 * persists from some earlier problem
1760 int err
= err
; /* only used if rv==1, and always set if
1761 * rv==1, so initialisation not needed,
1762 * despite gcc warning
1764 if (sysfs_set_num(sra
, NULL
, "chunk_size", nchunk
) < 0)
1765 rv
= 1, err
= errno
;
1766 if (!rv
&& sysfs_set_num(sra
, NULL
, "layout", nlayout
) < 0)
1767 rv
= 1, err
= errno
;
1768 if (!rv
&& sysfs_set_num(sra
, NULL
, "raid_disks", ndisks
) < 0)
1769 rv
= 1, err
= errno
;
1771 fprintf(stderr
, Name
": Cannot set device shape for %s\n",
1773 if (get_linux_version() < 2006030)
1774 fprintf(stderr
, Name
": linux 2.6.30 or later required\n");
1776 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1777 fprintf(stderr
, " Bitmap must be removed before shape can be changed\n");
1782 if (ndisks
== 2 && odisks
== 2) {
1783 /* No reshape is needed in this trivial case */
1788 if (st
->ss
->external
) {
1789 /* metadata handler takes it from here */
1790 ping_manager(container
);
1791 st
->ss
->manage_reshape(st
, backup_file
);
1796 /* set up the backup-super-block. This requires the
1797 * uuid from the array.
1799 /* Find a superblock */
1800 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1804 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1806 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
1807 devfd
= dev_open(dn
, O_RDONLY
);
1810 ok
= st
->ss
->load_super(st
, devfd
, NULL
);
1816 fprintf(stderr
, Name
": %s: Cannot find a superblock\n",
1822 memset(&bsb
, 0, 512);
1823 memcpy(bsb
.magic
, "md_backup_data-1", 16);
1824 st
->ss
->uuid_from_super(st
, (int*)&bsb
.set_uuid
);
1825 bsb
.mtime
= __cpu_to_le64(time(0));
1826 bsb
.devstart2
= blocks
;
1827 stripes
= blocks
/ (ochunk
/512) / odata
;
1828 /* Now we just need to kick off the reshape and watch, while
1829 * handling backups of the data...
1830 * This is all done by a forked background process.
1835 if (check_env("MDADM_GROW_VERIFY"))
1836 fd
= open(devname
, O_RDONLY
| O_DIRECT
);
1839 mlockall(MCL_FUTURE
);
1842 done
= child_grow(fd
, sra
, stripes
,
1844 odisks
, ochunk
, array
.level
, olayout
, odata
,
1845 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1846 else if (odata
> ndata
)
1847 done
= child_shrink(fd
, sra
, stripes
,
1849 odisks
, ochunk
, array
.level
, olayout
, odata
,
1850 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1852 done
= child_same_size(fd
, sra
, stripes
,
1855 odisks
, ochunk
, array
.level
, olayout
, odata
,
1856 d
- odisks
, fdlist
+odisks
, offsets
+odisks
);
1857 if (backup_file
&& done
)
1858 unlink(backup_file
);
1859 if (level
!= UnSet
&& level
!= array
.level
) {
1860 /* We need to wait for the reshape to finish
1861 * (which will have happened unless odata < ndata)
1862 * and then set the level
1865 c
= map_num(pers
, level
);
1867 exit(0);/* not possible */
1871 err
= sysfs_set_str(sra
, NULL
, "level", c
);
1873 fprintf(stderr
, Name
": %s: could not set level to %s\n",
1878 fprintf(stderr
, Name
": Cannot run child to monitor reshape: %s\n",
1883 /* The child will take care of unfreezing the array */
1892 if (rv
&& orig_level
!= UnSet
&& sra
) {
1893 c
= map_num(pers
, orig_level
);
1894 if (c
&& sysfs_set_str(sra
, NULL
, "level", c
) == 0)
1895 fprintf(stderr
, Name
": aborting level change\n");
1897 unfreeze(st
, frozen
);
1902 * We run a child process in the background which performs the following
1904 * - wait for resync to reach a certain point
1905 * - suspend io to the following section
1906 * - backup that section
1907 * - allow resync to proceed further
1909 * - discard the backup.
1911 * When are combined in slightly different ways in the three cases.
1913 * - suspend/backup/allow/wait/resume/discard
1915 * - allow/wait/suspend/backup/allow/wait/resume/discard
1917 * - wait/resume/discard/suspend/backup/allow
1919 * suspend/backup/allow always come together
1920 * wait/resume/discard do too.
1921 * For the same-size case we have two backups to improve flow.
1925 /* FIXME return status is never checked */
1926 static int grow_backup(struct mdinfo
*sra
,
1927 unsigned long long offset
, /* per device */
1928 unsigned long stripes
, /* per device */
1929 int *sources
, unsigned long long *offsets
,
1930 int disks
, int chunk
, int level
, int layout
,
1931 int dests
, int *destfd
, unsigned long long *destoffsets
,
1932 int part
, int *degraded
,
1935 /* Backup 'blocks' sectors at 'offset' on each device of the array,
1936 * to storage 'destfd' (offset 'destoffsets'), after first
1937 * suspending IO. Then allow resync to continue
1938 * over the suspended section.
1939 * Use part 'part' of the backup-super-block.
1944 unsigned long long ll
;
1946 //printf("offset %llu\n", offset);
1951 sysfs_set_num(sra
, NULL
, "suspend_hi", (offset
+ stripes
* (chunk
/512)) * odata
);
1952 /* Check that array hasn't become degraded, else we might backup the wrong data */
1953 sysfs_get_ll(sra
, NULL
, "degraded", &ll
);
1954 new_degraded
= (int)ll
;
1955 if (new_degraded
!= *degraded
) {
1956 /* check each device to ensure it is still working */
1958 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1959 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1961 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
1963 if (sysfs_get_str(sra
, sd
, "state", sbuf
, 20) < 0 ||
1964 strstr(sbuf
, "faulty") ||
1965 strstr(sbuf
, "in_sync") == NULL
) {
1966 /* this device is dead */
1967 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
1968 if (sd
->disk
.raid_disk
>= 0 &&
1969 sources
[sd
->disk
.raid_disk
] >= 0) {
1970 close(sources
[sd
->disk
.raid_disk
]);
1971 sources
[sd
->disk
.raid_disk
] = -1;
1976 *degraded
= new_degraded
;
1979 bsb
.arraystart2
= __cpu_to_le64(offset
* odata
);
1980 bsb
.length2
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
1982 bsb
.arraystart
= __cpu_to_le64(offset
* odata
);
1983 bsb
.length
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
1986 bsb
.magic
[15] = '2';
1987 for (i
= 0; i
< dests
; i
++)
1989 lseek64(destfd
[i
], destoffsets
[i
] + __le64_to_cpu(bsb
.devstart2
)*512, 0);
1991 lseek64(destfd
[i
], destoffsets
[i
], 0);
1993 rv
= save_stripes(sources
, offsets
,
1994 disks
, chunk
, level
, layout
,
1996 offset
*512*odata
, stripes
* chunk
* odata
,
2001 bsb
.mtime
= __cpu_to_le64(time(0));
2002 for (i
= 0; i
< dests
; i
++) {
2003 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
2005 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
2006 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
2007 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
2008 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
2011 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
] - 4096, 0)
2012 != destoffsets
[i
] - 4096)
2014 if (write(destfd
[i
], &bsb
, 512) != 512)
2016 if (destoffsets
[i
] > 4096) {
2017 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]+stripes
*chunk
*odata
, 0) !=
2018 destoffsets
[i
]+stripes
*chunk
*odata
)
2020 if (write(destfd
[i
], &bsb
, 512) != 512)
2030 /* in 2.6.30, the value reported by sync_completed can be
2031 * less that it should be by one stripe.
2032 * This only happens when reshape hits sync_max and pauses.
2033 * So allow wait_backup to either extent sync_max further
2034 * than strictly necessary, or return before the
2035 * sync has got quite as far as we would really like.
2036 * This is what 'blocks2' is for.
2037 * The various caller give appropriate values so that
2040 /* FIXME return value is often ignored */
2041 static int wait_backup(struct mdinfo
*sra
,
2042 unsigned long long offset
, /* per device */
2043 unsigned long long blocks
, /* per device */
2044 unsigned long long blocks2
, /* per device - hack */
2045 int dests
, int *destfd
, unsigned long long *destoffsets
,
2048 /* Wait for resync to pass the section that was backed up
2049 * then erase the backup and allow IO
2051 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
2052 unsigned long long completed
;
2058 sysfs_set_num(sra
, NULL
, "sync_max", offset
+ blocks
+ blocks2
);
2060 sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
2066 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
2067 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
2071 if (sysfs_get_str(sra
, NULL
, "sync_action",
2073 strncmp(action
, "reshape", 7) != 0)
2075 } while (completed
< offset
+ blocks
);
2079 bsb
.arraystart2
= __cpu_to_le64(0);
2080 bsb
.length2
= __cpu_to_le64(0);
2082 bsb
.arraystart
= __cpu_to_le64(0);
2083 bsb
.length
= __cpu_to_le64(0);
2085 bsb
.mtime
= __cpu_to_le64(time(0));
2087 for (i
= 0; i
< dests
; i
++) {
2088 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
2089 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
2090 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
2091 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
2092 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
2093 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]-4096, 0) !=
2094 destoffsets
[i
]-4096)
2097 write(destfd
[i
], &bsb
, 512) != 512)
2104 static void fail(char *msg
)
2107 rv
= (write(2, msg
, strlen(msg
)) != (int)strlen(msg
));
2108 rv
|= (write(2, "\n", 1) != 1);
2112 static char *abuf
, *bbuf
;
2113 static unsigned long long abuflen
;
2114 static void validate(int afd
, int bfd
, unsigned long long offset
)
2116 /* check that the data in the backup against the array.
2117 * This is only used for regression testing and should not
2118 * be used while the array is active
2122 lseek64(bfd
, offset
- 4096, 0);
2123 if (read(bfd
, &bsb2
, 512) != 512)
2124 fail("cannot read bsb");
2125 if (bsb2
.sb_csum
!= bsb_csum((char*)&bsb2
,
2126 ((char*)&bsb2
.sb_csum
)-((char*)&bsb2
)))
2127 fail("first csum bad");
2128 if (memcmp(bsb2
.magic
, "md_backup_data", 14) != 0)
2129 fail("magic is bad");
2130 if (memcmp(bsb2
.magic
, "md_backup_data-2", 16) == 0 &&
2131 bsb2
.sb_csum2
!= bsb_csum((char*)&bsb2
,
2132 ((char*)&bsb2
.sb_csum2
)-((char*)&bsb2
)))
2133 fail("second csum bad");
2135 if (__le64_to_cpu(bsb2
.devstart
)*512 != offset
)
2136 fail("devstart is wrong");
2139 unsigned long long len
= __le64_to_cpu(bsb2
.length
)*512;
2141 if (abuflen
< len
) {
2145 if (posix_memalign((void**)&abuf
, 4096, abuflen
) ||
2146 posix_memalign((void**)&bbuf
, 4096, abuflen
)) {
2148 /* just stop validating on mem-alloc failure */
2153 lseek64(bfd
, offset
, 0);
2154 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
) {
2155 //printf("len %llu\n", len);
2156 fail("read first backup failed");
2158 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart
)*512, 0);
2159 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
2160 fail("read first from array failed");
2161 if (memcmp(bbuf
, abuf
, len
) != 0) {
2164 printf("offset=%llu len=%llu\n",
2165 (unsigned long long)__le64_to_cpu(bsb2
.arraystart
)*512, len
);
2166 for (i
=0; i
<len
; i
++)
2167 if (bbuf
[i
] != abuf
[i
]) {
2168 printf("first diff byte %d\n", i
);
2172 fail("data1 compare failed");
2176 unsigned long long len
= __le64_to_cpu(bsb2
.length2
)*512;
2178 if (abuflen
< len
) {
2182 abuf
= malloc(abuflen
);
2183 bbuf
= malloc(abuflen
);
2186 lseek64(bfd
, offset
+__le64_to_cpu(bsb2
.devstart2
)*512, 0);
2187 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
)
2188 fail("read second backup failed");
2189 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart2
)*512, 0);
2190 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
2191 fail("read second from array failed");
2192 if (memcmp(bbuf
, abuf
, len
) != 0)
2193 fail("data2 compare failed");
2197 static int child_grow(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
2198 int *fds
, unsigned long long *offsets
,
2199 int disks
, int chunk
, int level
, int layout
, int data
,
2200 int dests
, int *destfd
, unsigned long long *destoffsets
)
2205 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
2206 /* Don't start the 'reshape' */
2208 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
2209 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
2210 grow_backup(sra
, 0, stripes
,
2211 fds
, offsets
, disks
, chunk
, level
, layout
,
2212 dests
, destfd
, destoffsets
,
2214 validate(afd
, destfd
[0], destoffsets
[0]);
2215 wait_backup(sra
, 0, stripes
* (chunk
/ 512), stripes
* (chunk
/ 512),
2216 dests
, destfd
, destoffsets
,
2218 sysfs_set_num(sra
, NULL
, "suspend_lo", (stripes
* (chunk
/512)) * data
);
2220 /* FIXME this should probably be numeric */
2221 sysfs_set_str(sra
, NULL
, "sync_max", "max");
2225 static int child_shrink(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
2226 int *fds
, unsigned long long *offsets
,
2227 int disks
, int chunk
, int level
, int layout
, int data
,
2228 int dests
, int *destfd
, unsigned long long *destoffsets
)
2231 unsigned long long start
;
2235 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
2237 start
= sra
->component_size
- stripes
* (chunk
/512);
2238 sysfs_set_num(sra
, NULL
, "sync_max", start
);
2239 sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
2240 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
2241 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
2242 rv
= wait_backup(sra
, 0, start
- stripes
* (chunk
/512), stripes
* (chunk
/512),
2243 dests
, destfd
, destoffsets
, 0);
2246 grow_backup(sra
, 0, stripes
,
2248 disks
, chunk
, level
, layout
,
2249 dests
, destfd
, destoffsets
,
2251 validate(afd
, destfd
[0], destoffsets
[0]);
2252 wait_backup(sra
, start
, stripes
*(chunk
/512), 0,
2253 dests
, destfd
, destoffsets
, 0);
2254 sysfs_set_num(sra
, NULL
, "suspend_lo", (stripes
* (chunk
/512)) * data
);
2256 /* FIXME this should probably be numeric */
2257 sysfs_set_str(sra
, NULL
, "sync_max", "max");
2261 static int child_same_size(int afd
, struct mdinfo
*sra
, unsigned long stripes
,
2262 int *fds
, unsigned long long *offsets
,
2263 unsigned long long start
,
2264 int disks
, int chunk
, int level
, int layout
, int data
,
2265 int dests
, int *destfd
, unsigned long long *destoffsets
)
2267 unsigned long long size
;
2268 unsigned long tailstripes
= stripes
;
2271 unsigned long long speed
;
2275 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
2278 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
2279 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
2281 sysfs_get_ll(sra
, NULL
, "sync_speed_min", &speed
);
2282 sysfs_set_num(sra
, NULL
, "sync_speed_min", 200000);
2284 grow_backup(sra
, start
, stripes
,
2286 disks
, chunk
, level
, layout
,
2287 dests
, destfd
, destoffsets
,
2289 grow_backup(sra
, (start
+ stripes
) * (chunk
/512), stripes
,
2291 disks
, chunk
, level
, layout
,
2292 dests
, destfd
, destoffsets
,
2294 validate(afd
, destfd
[0], destoffsets
[0]);
2296 start
+= stripes
* 2; /* where to read next */
2297 size
= sra
->component_size
/ (chunk
/512);
2298 while (start
< size
) {
2299 if (wait_backup(sra
, (start
-stripes
*2)*(chunk
/512),
2300 stripes
*(chunk
/512), 0,
2301 dests
, destfd
, destoffsets
,
2304 sysfs_set_num(sra
, NULL
, "suspend_lo", start
*(chunk
/512) * data
);
2305 if (start
+ stripes
> size
)
2306 tailstripes
= (size
- start
);
2308 grow_backup(sra
, start
*(chunk
/512), tailstripes
,
2310 disks
, chunk
, level
, layout
,
2311 dests
, destfd
, destoffsets
,
2312 part
, °raded
, buf
);
2315 validate(afd
, destfd
[0], destoffsets
[0]);
2317 if (wait_backup(sra
, (start
-stripes
*2) * (chunk
/512), stripes
* (chunk
/512), 0,
2318 dests
, destfd
, destoffsets
,
2321 sysfs_set_num(sra
, NULL
, "suspend_lo", ((start
-stripes
)*(chunk
/512)) * data
);
2322 wait_backup(sra
, (start
-stripes
) * (chunk
/512), tailstripes
* (chunk
/512), 0,
2323 dests
, destfd
, destoffsets
,
2325 sysfs_set_num(sra
, NULL
, "suspend_lo", (size
*(chunk
/512)) * data
);
2326 sysfs_set_num(sra
, NULL
, "sync_speed_min", speed
);
2332 * If any spare contains md_back_data-1 which is recent wrt mtime,
2333 * write that data into the array and update the super blocks with
2334 * the new reshape_progress
2336 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
, int cnt
,
2337 char *backup_file
, int verbose
)
2341 unsigned long long *offsets
;
2342 unsigned long long nstripe
, ostripe
;
2345 if (info
->new_level
!= info
->array
.level
)
2346 return 1; /* Cannot handle level changes (they are instantaneous) */
2348 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
2349 if (info
->array
.level
== 6) odata
--; /* number of data disks */
2350 ndata
= info
->array
.raid_disks
- 1;
2351 if (info
->new_level
== 6) ndata
--;
2353 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
2355 if (info
->delta_disks
<= 0)
2356 /* Didn't grow, so the backup file must have
2360 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
2361 struct mdinfo dinfo
;
2364 char *devname
, namebuf
[20];
2366 /* This was a spare and may have some saved data on it.
2367 * Load the superblock, find and load the
2368 * backup_super_block.
2369 * If either fail, go on to next device.
2370 * If the backup contains no new info, just return
2371 * else restore data and update all superblocks
2373 if (i
== old_disks
-1) {
2374 fd
= open(backup_file
, O_RDONLY
);
2376 fprintf(stderr
, Name
": backup file %s inaccessible: %s\n",
2377 backup_file
, strerror(errno
));
2380 devname
= backup_file
;
2385 if (st
->ss
->load_super(st
, fd
, NULL
))
2388 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
2389 st
->ss
->free_super(st
);
2392 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
2394 fprintf(stderr
, Name
": Cannot seek on device %d\n", i
);
2395 continue; /* Cannot seek */
2397 sprintf(namebuf
, "device-%d", i
);
2400 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
)) {
2402 fprintf(stderr
, Name
": Cannot read from %s\n", devname
);
2403 continue; /* Cannot read */
2405 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0 &&
2406 memcmp(bsb
.magic
, "md_backup_data-2", 16) != 0) {
2408 fprintf(stderr
, Name
": No backup metadata on %s\n", devname
);
2411 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
))) {
2413 fprintf(stderr
, Name
": Bad backup-metadata checksum on %s\n", devname
);
2414 continue; /* bad checksum */
2416 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0 &&
2417 bsb
.sb_csum2
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum2
)-((char*)&bsb
))) {
2419 fprintf(stderr
, Name
": Bad backup-metadata checksum2 on %s\n", devname
);
2420 continue; /* Bad second checksum */
2422 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0) {
2424 fprintf(stderr
, Name
": Wrong uuid on backup-metadata on %s\n", devname
);
2425 continue; /* Wrong uuid */
2428 /* array utime and backup-mtime should be updated at much the same time, but it seems that
2429 * sometimes they aren't... So allow considerable flexability in matching, and allow
2430 * this test to be overridden by an environment variable.
2432 if (info
->array
.utime
> (int)__le64_to_cpu(bsb
.mtime
) + 2*60*60 ||
2433 info
->array
.utime
< (int)__le64_to_cpu(bsb
.mtime
) - 10*60) {
2434 if (check_env("MDADM_GROW_ALLOW_OLD")) {
2435 fprintf(stderr
, Name
": accepting backup with timestamp %lu "
2436 "for array with timestamp %lu\n",
2437 (unsigned long)__le64_to_cpu(bsb
.mtime
),
2438 (unsigned long)info
->array
.utime
);
2441 fprintf(stderr
, Name
": too-old timestamp on "
2442 "backup-metadata on %s\n", devname
);
2443 continue; /* time stamp is too bad */
2447 if (bsb
.magic
[15] == '1') {
2448 if (info
->delta_disks
>= 0) {
2449 /* reshape_progress is increasing */
2450 if (__le64_to_cpu(bsb
.arraystart
) + __le64_to_cpu(bsb
.length
) <
2451 info
->reshape_progress
) {
2454 fprintf(stderr
, Name
": backup-metadata found on %s but is not needed\n", devname
);
2455 continue; /* No new data here */
2458 /* reshape_progress is decreasing */
2459 if (__le64_to_cpu(bsb
.arraystart
) >=
2460 info
->reshape_progress
)
2461 goto nonew
; /* No new data here */
2464 if (info
->delta_disks
>= 0) {
2465 /* reshape_progress is increasing */
2466 if (__le64_to_cpu(bsb
.arraystart
) + __le64_to_cpu(bsb
.length
) <
2467 info
->reshape_progress
&&
2468 __le64_to_cpu(bsb
.arraystart2
) + __le64_to_cpu(bsb
.length2
) <
2469 info
->reshape_progress
)
2470 goto nonew
; /* No new data here */
2472 /* reshape_progress is decreasing */
2473 if (__le64_to_cpu(bsb
.arraystart
) >=
2474 info
->reshape_progress
&&
2475 __le64_to_cpu(bsb
.arraystart2
) >=
2476 info
->reshape_progress
)
2477 goto nonew
; /* No new data here */
2480 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0) {
2483 fprintf(stderr
, Name
": Failed to verify secondary backup-metadata block on %s\n",
2485 continue; /* Cannot seek */
2487 /* There should be a duplicate backup superblock 4k before here */
2488 if (lseek64(fd
, -4096, 1) < 0 ||
2489 read(fd
, &bsb2
, sizeof(bsb2
)) != sizeof(bsb2
))
2490 goto second_fail
; /* Cannot find leading superblock */
2491 if (bsb
.magic
[15] == '1')
2492 bsbsize
= offsetof(struct mdp_backup_super
, pad1
);
2494 bsbsize
= offsetof(struct mdp_backup_super
, pad
);
2495 if (memcmp(&bsb2
, &bsb
, bsbsize
) != 0)
2496 goto second_fail
; /* Cannot find leading superblock */
2498 /* Now need the data offsets for all devices. */
2499 offsets
= malloc(sizeof(*offsets
)*info
->array
.raid_disks
);
2500 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
2503 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
2504 /* FIXME should be this be an error */
2506 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
2507 st
->ss
->free_super(st
);
2508 offsets
[j
] = dinfo
.data_offset
* 512;
2510 printf(Name
": restoring critical section\n");
2512 if (restore_stripes(fdlist
, offsets
,
2513 info
->array
.raid_disks
,
2517 fd
, __le64_to_cpu(bsb
.devstart
)*512,
2518 __le64_to_cpu(bsb
.arraystart
)*512,
2519 __le64_to_cpu(bsb
.length
)*512)) {
2520 /* didn't succeed, so giveup */
2522 fprintf(stderr
, Name
": Error restoring backup from %s\n",
2527 if (bsb
.magic
[15] == '2' &&
2528 restore_stripes(fdlist
, offsets
,
2529 info
->array
.raid_disks
,
2533 fd
, __le64_to_cpu(bsb
.devstart
)*512 +
2534 __le64_to_cpu(bsb
.devstart2
)*512,
2535 __le64_to_cpu(bsb
.arraystart2
)*512,
2536 __le64_to_cpu(bsb
.length2
)*512)) {
2537 /* didn't succeed, so giveup */
2539 fprintf(stderr
, Name
": Error restoring second backup from %s\n",
2545 /* Ok, so the data is restored. Let's update those superblocks. */
2547 if (info
->delta_disks
>= 0) {
2548 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
) +
2549 __le64_to_cpu(bsb
.length
);
2550 if (bsb
.magic
[15] == '2') {
2551 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
) +
2552 __le64_to_cpu(bsb
.length2
);
2553 if (p2
> info
->reshape_progress
)
2554 info
->reshape_progress
= p2
;
2557 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
);
2558 if (bsb
.magic
[15] == '2') {
2559 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
);
2560 if (p2
< info
->reshape_progress
)
2561 info
->reshape_progress
= p2
;
2564 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
2565 if (fdlist
[j
] < 0) continue;
2566 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
2568 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
2569 dinfo
.reshape_progress
= info
->reshape_progress
;
2570 st
->ss
->update_super(st
, &dinfo
,
2571 "_reshape_progress",
2573 st
->ss
->store_super(st
, fdlist
[j
]);
2574 st
->ss
->free_super(st
);
2578 /* Didn't find any backup data, try to see if any
2581 if (info
->delta_disks
< 0) {
2582 /* When shrinking, the critical section is at the end.
2583 * So see if we are before the critical section.
2585 unsigned long long first_block
;
2586 nstripe
= ostripe
= 0;
2588 while (ostripe
>= nstripe
) {
2589 ostripe
+= info
->array
.chunk_size
/ 512;
2590 first_block
= ostripe
* odata
;
2591 nstripe
= first_block
/ ndata
/ (info
->new_chunk
/512) *
2592 (info
->new_chunk
/512);
2595 if (info
->reshape_progress
>= first_block
)
2598 if (info
->delta_disks
> 0) {
2599 /* See if we are beyond the critical section. */
2600 unsigned long long last_block
;
2601 nstripe
= ostripe
= 0;
2603 while (nstripe
>= ostripe
) {
2604 nstripe
+= info
->new_chunk
/ 512;
2605 last_block
= nstripe
* ndata
;
2606 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
2607 (info
->array
.chunk_size
/512);
2610 if (info
->reshape_progress
>= last_block
)
2613 /* needed to recover critical section! */
2615 fprintf(stderr
, Name
": Failed to find backup of critical section\n");
2619 int Grow_continue(int mdfd
, struct supertype
*st
, struct mdinfo
*info
,
2622 /* Array is assembled and ready to be started, but
2623 * monitoring is probably required.
2626 * - set upper bound for resync
2627 * - initialise the 'suspend' boundaries
2628 * - switch to read-write
2629 * - fork and continue monitoring
2633 unsigned long long backup_offsets
[1];
2634 int odisks
, ndisks
, ochunk
, nchunk
,odata
,ndata
;
2635 unsigned long a
,b
,blocks
,stripes
;
2638 unsigned long long *offsets
;
2640 struct mdinfo
*sra
, *sd
;
2642 unsigned long cache
;
2645 err
= sysfs_set_str(info
, NULL
, "array_state", "readonly");
2649 /* make sure reshape doesn't progress until we are ready */
2650 sysfs_set_str(info
, NULL
, "sync_max", "0");
2651 sysfs_set_str(info
, NULL
, "array_state", "active"); /* FIXME or clean */
2653 sra
= sysfs_read(-1, devname2devnum(info
->sys_name
),
2654 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
2659 /* ndisks is not growing, so raid_disks is old and +delta is new */
2660 odisks
= info
->array
.raid_disks
;
2661 ndisks
= odisks
+ info
->delta_disks
;
2664 if (info
->array
.level
== 6) {
2668 ochunk
= info
->array
.chunk_size
;
2669 nchunk
= info
->new_chunk
;
2671 a
= (ochunk
/512) * odata
;
2672 b
= (nchunk
/512) * ndata
;
2680 /* LCM == product / GCD */
2681 blocks
= (ochunk
/512) * (nchunk
/512) * odata
* ndata
/ a
;
2684 while (blocks
* 32 < sra
->component_size
&&
2687 stripes
= blocks
/ (info
->array
.chunk_size
/512) / odata
;
2689 /* check that the internal stripe cache is
2690 * large enough, or it won't work.
2692 cache
= (nchunk
< ochunk
) ? ochunk
: nchunk
;
2693 cache
= cache
* 4 / 4096;
2694 if (cache
< blocks
/ 8 / odisks
+ 16)
2695 /* Make it big enough to hold 'blocks' */
2696 cache
= blocks
/ 8 / odisks
+ 16;
2697 if (sra
->cache_size
< cache
)
2698 sysfs_set_num(sra
, NULL
, "stripe_cache_size",
2701 memset(&bsb
, 0, 512);
2702 memcpy(bsb
.magic
, "md_backup_data-1", 16);
2703 memcpy(&bsb
.set_uuid
, info
->uuid
, 16);
2704 bsb
.mtime
= __cpu_to_le64(time(0));
2705 bsb
.devstart2
= blocks
;
2707 backup_fd
= open(backup_file
, O_RDWR
|O_CREAT
, S_IRUSR
| S_IWUSR
);
2708 if (backup_fd
< 0) {
2709 fprintf(stderr
, Name
": Cannot open backup file %s\n",
2710 backup_file
?: "- no backup-file given");
2713 backup_list
[0] = backup_fd
;
2714 backup_offsets
[0] = 8 * 512;
2715 fds
= malloc(odisks
* sizeof(fds
[0]));
2716 offsets
= malloc(odisks
* sizeof(offsets
[0]));
2717 for (d
=0; d
<odisks
; d
++)
2720 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2721 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2723 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
2724 char *dn
= map_dev(sd
->disk
.major
,
2726 fds
[sd
->disk
.raid_disk
]
2727 = dev_open(dn
, O_RDONLY
);
2728 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
2729 if (fds
[sd
->disk
.raid_disk
] < 0) {
2730 fprintf(stderr
, Name
": %s: cannot open component %s\n",
2731 info
->sys_name
, dn
?dn
:"-unknown-");
2742 mlockall(MCL_FUTURE
);
2743 if (info
->delta_disks
< 0)
2744 done
= child_shrink(-1, info
, stripes
,
2746 info
->array
.raid_disks
,
2747 info
->array
.chunk_size
,
2748 info
->array
.level
, info
->array
.layout
,
2750 1, backup_list
, backup_offsets
);
2751 else if (info
->delta_disks
== 0) {
2752 /* The 'start' is a per-device stripe number.
2753 * reshape_progress is a per-array sector number.
2754 * So divide by ndata * chunk_size
2756 unsigned long long start
= info
->reshape_progress
/ ndata
;
2757 start
/= (info
->array
.chunk_size
/512);
2758 done
= child_same_size(-1, info
, stripes
,
2761 info
->array
.raid_disks
,
2762 info
->array
.chunk_size
,
2763 info
->array
.level
, info
->array
.layout
,
2765 1, backup_list
, backup_offsets
);
2767 if (backup_file
&& done
)
2768 unlink(backup_file
);
2769 /* FIXME should I intuit a level change */
2772 fprintf(stderr
, Name
": Cannot run child to continue monitoring reshape: %s\n",