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 check_idle(struct supertype
*st
)
458 /* Check that all member arrays for this container, or the
459 * container of this array, are idle
461 int container_dev
= (st
->container_dev
!= NoMdDev
462 ? st
->container_dev
: st
->devnum
);
464 struct mdstat_ent
*ent
, *e
;
467 fmt_devname(container
, container_dev
);
468 ent
= mdstat_read(0, 0);
469 for (e
= ent
; e
; e
= e
->next
) {
470 if (!is_container_member(e
, container
))
472 if (e
->percent
>= 0) {
481 static int freeze_container(struct supertype
*st
)
483 int container_dev
= (st
->container_dev
!= NoMdDev
484 ? st
->container_dev
: st
->devnum
);
490 fmt_devname(container
, container_dev
);
492 if (block_monitor(container
, 1)) {
493 fprintf(stderr
, Name
": failed to freeze container\n");
500 static void unfreeze_container(struct supertype
*st
)
502 int container_dev
= (st
->container_dev
!= NoMdDev
503 ? st
->container_dev
: st
->devnum
);
506 fmt_devname(container
, container_dev
);
508 unblock_monitor(container
, 1);
511 static int freeze(struct supertype
*st
)
513 /* Try to freeze resync/rebuild on this array/container.
514 * Return -1 if the array is busy,
515 * return -2 container cannot be frozen,
516 * return 0 if this kernel doesn't support 'frozen'
517 * return 1 if it worked.
519 if (st
->ss
->external
)
520 return freeze_container(st
);
522 struct mdinfo
*sra
= sysfs_read(-1, st
->devnum
, GET_VERSION
);
528 /* Need to clear any 'read-auto' status */
529 if (sysfs_get_str(sra
, NULL
, "array_state", buf
, 20) > 0 &&
530 strncmp(buf
, "read-auto", 9) == 0)
531 sysfs_set_str(sra
, NULL
, "array_state", "clean");
533 err
= sysfs_freeze_array(sra
);
539 static void unfreeze(struct supertype
*st
)
541 if (st
->ss
->external
)
542 return unfreeze_container(st
);
544 struct mdinfo
*sra
= sysfs_read(-1, st
->devnum
, GET_VERSION
);
547 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
549 fprintf(stderr
, Name
": failed to unfreeze array\n");
554 static void wait_reshape(struct mdinfo
*sra
)
556 int fd
= sysfs_get_fd(sra
, NULL
, "sync_action");
562 while (sysfs_fd_get_str(fd
, action
, 20) > 0 &&
563 strncmp(action
, "reshape", 7) == 0) {
567 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
572 static int reshape_super(struct supertype
*st
, long long size
, int level
,
573 int layout
, int chunksize
, int raid_disks
,
574 int delta_disks
, char *backup_file
, char *dev
,
577 /* nothing extra to check in the native case */
578 if (!st
->ss
->external
)
580 if (!st
->ss
->reshape_super
||
581 !st
->ss
->manage_reshape
) {
582 fprintf(stderr
, Name
": %s metadata does not support reshape\n",
587 return st
->ss
->reshape_super(st
, size
, level
, layout
, chunksize
,
588 raid_disks
, delta_disks
, backup_file
, dev
,
592 static void sync_metadata(struct supertype
*st
)
594 if (st
->ss
->external
) {
595 if (st
->update_tail
) {
596 flush_metadata_updates(st
);
597 st
->update_tail
= &st
->updates
;
599 st
->ss
->sync_metadata(st
);
603 static int subarray_set_num(char *container
, struct mdinfo
*sra
, char *name
, int n
)
605 /* when dealing with external metadata subarrays we need to be
606 * prepared to handle EAGAIN. The kernel may need to wait for
607 * mdmon to mark the array active so the kernel can handle
608 * allocations/writeback when preparing the reshape action
609 * (md_allow_write()). We temporarily disable safe_mode_delay
610 * to close a race with the array_state going clean before the
611 * next write to raid_disks / stripe_cache_size
616 /* only 'raid_disks' and 'stripe_cache_size' trigger md_allow_write */
618 (strcmp(name
, "raid_disks") != 0 &&
619 strcmp(name
, "stripe_cache_size") != 0))
620 return sysfs_set_num(sra
, NULL
, name
, n
);
622 rc
= sysfs_get_str(sra
, NULL
, "safe_mode_delay", safe
, sizeof(safe
));
625 sysfs_set_num(sra
, NULL
, "safe_mode_delay", 0);
626 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
627 if (rc
< 0 && errno
== EAGAIN
) {
628 ping_monitor(container
);
629 /* if we get EAGAIN here then the monitor is not active
632 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
634 sysfs_set_str(sra
, NULL
, "safe_mode_delay", safe
);
638 int start_reshape(struct mdinfo
*sra
, int already_running
)
641 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
642 err
= sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
643 err
= err
?: sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
644 if (!already_running
)
645 sysfs_set_num(sra
, NULL
, "sync_min", 0);
646 err
= err
?: sysfs_set_num(sra
, NULL
, "sync_max", 0);
647 if (!already_running
)
648 err
= err
?: sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
653 void abort_reshape(struct mdinfo
*sra
)
655 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
656 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
657 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
658 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
659 sysfs_set_num(sra
, NULL
, "sync_min", 0);
660 sysfs_set_str(sra
, NULL
, "sync_max", "max");
663 int remove_disks_for_takeover(struct supertype
*st
,
668 struct mdinfo
*remaining
;
671 if (sra
->array
.level
== 10)
672 nr_of_copies
= layout
& 0xff;
673 else if (sra
->array
.level
== 1)
674 nr_of_copies
= sra
->array
.raid_disks
;
678 remaining
= sra
->devs
;
680 /* for each 'copy', select one device and remove from the list. */
681 for (slot
= 0; slot
< sra
->array
.raid_disks
; slot
+= nr_of_copies
) {
682 struct mdinfo
**diskp
;
685 /* Find a working device to keep */
686 for (diskp
= &remaining
; *diskp
; diskp
= &(*diskp
)->next
) {
687 struct mdinfo
*disk
= *diskp
;
689 if (disk
->disk
.raid_disk
< slot
)
691 if (disk
->disk
.raid_disk
>= slot
+ nr_of_copies
)
693 if (disk
->disk
.state
& (1<<MD_DISK_REMOVED
))
695 if (disk
->disk
.state
& (1<<MD_DISK_FAULTY
))
697 if (!(disk
->disk
.state
& (1<<MD_DISK_SYNC
)))
700 /* We have found a good disk to use! */
702 disk
->next
= sra
->devs
;
711 if (slot
< sra
->array
.raid_disks
) {
712 /* didn't find all slots */
718 sra
->devs
= remaining
;
722 /* Remove all 'remaining' devices from the array */
724 struct mdinfo
*sd
= remaining
;
725 remaining
= sd
->next
;
727 sysfs_set_str(sra
, sd
, "state", "faulty");
728 sysfs_set_str(sra
, sd
, "slot", "none");
729 /* for external metadata disks should be removed in mdmon */
730 if (!st
->ss
->external
)
731 sysfs_set_str(sra
, sd
, "state", "remove");
732 sd
->disk
.state
|= (1<<MD_DISK_REMOVED
);
733 sd
->disk
.state
&= ~(1<<MD_DISK_SYNC
);
734 sd
->next
= sra
->devs
;
740 void reshape_free_fdlist(int *fdlist
,
741 unsigned long long *offsets
,
746 for (i
= 0; i
< size
; i
++)
754 int reshape_prepare_fdlist(char *devname
,
758 unsigned long blocks
,
761 unsigned long long *offsets
)
766 for (d
= 0; d
<= nrdisks
; d
++)
769 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
770 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
772 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
773 char *dn
= map_dev(sd
->disk
.major
,
775 fdlist
[sd
->disk
.raid_disk
]
776 = dev_open(dn
, O_RDONLY
);
777 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
778 if (fdlist
[sd
->disk
.raid_disk
] < 0) {
780 Name
": %s: cannot open component %s\n",
781 devname
, dn
? dn
: "-unknown-");
785 } else if (backup_file
== NULL
) {
787 char *dn
= map_dev(sd
->disk
.major
,
789 fdlist
[d
] = dev_open(dn
, O_RDWR
);
790 offsets
[d
] = (sd
->data_offset
+ sra
->component_size
- blocks
- 8)*512;
792 fprintf(stderr
, Name
": %s: cannot open component %s\n",
793 devname
, dn
? dn
: "-unknown-");
804 int reshape_open_backup_file(char *backup_file
,
809 unsigned long long *offsets
,
812 /* Return 1 on success, 0 on any form of failure */
813 /* need to check backup file is large enough */
819 *fdlist
= open(backup_file
, O_RDWR
|O_CREAT
|(restart
? O_TRUNC
: O_EXCL
),
823 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
824 devname
, backup_file
, strerror(errno
));
827 /* Guard against backup file being on array device.
828 * If array is partitioned or if LVM etc is in the
829 * way this will not notice, but it is better than
832 fstat(*fdlist
, &stb
);
835 if (stb
.st_rdev
== dev
) {
836 fprintf(stderr
, Name
": backup file must NOT be"
837 " on the array being reshaped.\n");
843 for (i
=0; i
< blocks
+ 8 ; i
++) {
844 if (write(*fdlist
, buf
, 512) != 512) {
845 fprintf(stderr
, Name
": %s: cannot create"
846 " backup file %s: %s\n",
847 devname
, backup_file
, strerror(errno
));
851 if (fsync(*fdlist
) != 0) {
852 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
853 devname
, backup_file
, strerror(errno
));
860 unsigned long compute_backup_blocks(int nchunk
, int ochunk
,
861 unsigned int ndata
, unsigned int odata
)
863 unsigned long a
, b
, blocks
;
864 /* So how much do we need to backup.
865 * We need an amount of data which is both a whole number of
866 * old stripes and a whole number of new stripes.
867 * So LCM for (chunksize*datadisks).
869 a
= (ochunk
/512) * odata
;
870 b
= (nchunk
/512) * ndata
;
878 /* LCM == product / GCD */
879 blocks
= (ochunk
/512) * (nchunk
/512) * odata
* ndata
/ a
;
884 char *analyse_change(struct mdinfo
*info
, struct reshape
*re
)
886 /* Based on the current array state in info->array and
887 * the changes in info->new_* etc, determine:
888 * - whether the change is possible
889 * - Intermediate level/raid_disks/layout
890 * - whether a restriping reshape is needed
891 * - number of sectors in minimum change unit. This
892 * will cover a whole number of stripes in 'before' and
895 * Return message if the change should be rejected
896 * NULL if the change can be achieved
898 * This can be called as part of starting a reshape, or
899 * when assembling an array that is undergoing reshape.
902 /* delta_parity records change in number of devices
903 * caused by level change
905 int delta_parity
= 0;
907 /* If a new level not explicitly given, we assume no-change */
908 if (info
->new_level
== UnSet
)
909 info
->new_level
= info
->array
.level
;
912 switch (info
->new_level
) {
918 /* chunk size is meaningful, must divide component_size
921 if (info
->component_size
% (info
->new_chunk
/512))
922 return "New chunk size does not"
923 " divide component size";
926 return "chunk size not meaningful for this level";
929 info
->new_chunk
= info
->array
.chunk_size
;
931 switch (info
->array
.level
) {
933 return "Cannot understand this RAID level";
935 /* RAID1 can convert to RAID1 with different disks, or
936 * raid5 with 2 disks, or
939 if (info
->new_level
== 0) {
940 if (info
->delta_disks
!= UnSet
&&
941 info
->delta_disks
!= 0)
942 return "Cannot change number of disks "
943 "with RAID1->RAID0 conversion";
945 re
->before
.data_disks
= 1;
946 re
->after
.data_disks
= 1;
947 re
->before
.layout
= 0;
948 re
->backup_blocks
= 0;
952 if (info
->new_level
== 1) {
953 if (info
->delta_disks
== UnSet
)
954 /* Don't know what to do */
955 return "no change requested for Growing RAID1";
957 re
->backup_blocks
= 0;
961 if (info
->array
.raid_disks
== 2 &&
962 info
->new_level
== 5) {
965 re
->before
.data_disks
= 1;
966 if (info
->delta_disks
!= UnSet
&&
967 info
->delta_disks
!= 0)
968 re
->after
.data_disks
= 1 + info
->delta_disks
;
970 re
->after
.data_disks
= 1;
971 if (re
->after
.data_disks
< 1)
972 return "Number of disks too small for RAID5";
974 re
->before
.layout
= ALGORITHM_LEFT_SYMMETRIC
;
975 info
->array
.chunk_size
= 65536;
978 /* Could do some multi-stage conversions, but leave that to
981 return "Impossibly level change request for RAID1";
984 /* RAID10 can only be converted from near mode to
985 * RAID0 by removing some devices
987 if ((info
->array
.layout
& ~0xff) != 0x100)
988 return "Cannot Grow RAID10 with far/offset layout";
989 /* number of devices must be multiple of number of copies */
990 if (info
->array
.raid_disks
% (info
->array
.layout
& 0xff))
991 return "RAID10 layout too complex for Grow operation";
993 if (info
->new_level
!= 0)
994 return "RAID10 can only be changed to RAID0";
995 new_disks
= (info
->array
.raid_disks
996 / (info
->array
.layout
& 0xff));
997 if (info
->delta_disks
== UnSet
)
998 info
->delta_disks
= (new_disks
999 - info
->array
.raid_disks
);
1001 if (info
->delta_disks
!= new_disks
- info
->array
.raid_disks
)
1002 return "New number of raid-devices impossible for RAID10";
1003 if (info
->new_chunk
&&
1004 info
->new_chunk
!= info
->array
.chunk_size
)
1005 return "Cannot change chunk-size with RAID10 Grow";
1010 re
->before
.data_disks
= new_disks
;
1011 re
->after
.data_disks
= re
->before
.data_disks
;
1012 re
->before
.layout
= 0;
1013 re
->backup_blocks
= 0;
1017 /* RAID0 can be converted to RAID10, or to RAID456 */
1018 if (info
->new_level
== 10) {
1019 if (info
->new_layout
== UnSet
&& info
->delta_disks
== UnSet
) {
1020 /* Assume near=2 layout */
1021 info
->new_layout
= 0x102;
1022 info
->delta_disks
= info
->array
.raid_disks
;
1024 if (info
->new_layout
== UnSet
) {
1025 int copies
= 1 + (info
->delta_disks
1026 / info
->array
.raid_disks
);
1027 if (info
->array
.raid_disks
* (copies
-1)
1028 != info
->delta_disks
)
1029 return "Impossible number of devices"
1030 " for RAID0->RAID10";
1031 info
->new_layout
= 0x100 + copies
;
1033 if (info
->delta_disks
== UnSet
) {
1034 int copies
= info
->new_layout
& 0xff;
1035 if (info
->new_layout
!= 0x100 + copies
)
1036 return "New layout impossible"
1037 " for RAID0->RAID10";;
1038 info
->delta_disks
= (copies
- 1) *
1039 info
->array
.raid_disks
;
1041 if (info
->new_chunk
&&
1042 info
->new_chunk
!= info
->array
.chunk_size
)
1043 return "Cannot change chunk-size with RAID0->RAID10";
1047 re
->before
.data_disks
= (info
->array
.raid_disks
+
1049 re
->after
.data_disks
= re
->before
.data_disks
;
1050 re
->before
.layout
= info
->new_layout
;
1051 re
->backup_blocks
= 0;
1055 /* RAID0 can also covert to RAID0/4/5/6 by first converting to
1056 * a raid4 style layout of the final level.
1058 switch (info
->new_level
) {
1063 re
->before
.layout
= 0;
1068 re
->before
.layout
= ALGORITHM_PARITY_N
;
1073 re
->before
.layout
= ALGORITHM_PARITY_N
;
1076 return "Impossible level change requested";
1078 re
->before
.data_disks
= info
->array
.raid_disks
;
1079 /* determining 'after' layout happens outside this 'switch' */
1083 info
->array
.layout
= ALGORITHM_PARITY_N
;
1085 switch (info
->new_level
) {
1089 re
->level
= info
->array
.level
;
1090 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1091 re
->before
.layout
= info
->array
.layout
;
1095 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1096 re
->before
.layout
= info
->array
.layout
;
1101 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1102 switch (info
->array
.layout
) {
1103 case ALGORITHM_LEFT_ASYMMETRIC
:
1104 re
->before
.layout
= ALGORITHM_LEFT_ASYMMETRIC_6
;
1106 case ALGORITHM_RIGHT_ASYMMETRIC
:
1107 re
->before
.layout
= ALGORITHM_RIGHT_ASYMMETRIC_6
;
1109 case ALGORITHM_LEFT_SYMMETRIC
:
1110 re
->before
.layout
= ALGORITHM_LEFT_SYMMETRIC_6
;
1112 case ALGORITHM_RIGHT_SYMMETRIC
:
1113 re
->before
.layout
= ALGORITHM_RIGHT_SYMMETRIC_6
;
1115 case ALGORITHM_PARITY_0
:
1116 re
->before
.layout
= ALGORITHM_PARITY_0_6
;
1118 case ALGORITHM_PARITY_N
:
1119 re
->before
.layout
= ALGORITHM_PARITY_N_6
;
1122 return "Cannot convert an array with this layout";
1126 if (info
->array
.raid_disks
!= 2)
1127 return "Can only convert a 2-device array to RAID1";
1128 if (info
->delta_disks
!= UnSet
&&
1129 info
->delta_disks
!= 0)
1130 return "Cannot set raid_disk when "
1131 "converting RAID5->RAID1";
1135 return "Impossible level change requested";
1139 switch (info
->new_level
) {
1145 re
->before
.data_disks
= info
->array
.raid_disks
- 2;
1146 re
->before
.layout
= info
->array
.layout
;
1149 return "Impossible level change requested";
1154 /* If we reached here then it looks like a re-stripe is
1155 * happening. We have determined the intermediate level
1156 * and initial raid_disks/layout and stored these in 're'.
1158 * We need to deduce the final layout that can be atomically
1159 * converted to the end state.
1161 switch (info
->new_level
) {
1163 /* We can only get to RAID0 from RAID4 or RAID5
1164 * with appropriate layout and one extra device
1166 if (re
->level
!= 4 && re
->level
!= 5)
1167 return "Cannot covert to RAID0 from this level";
1169 switch (re
->level
) {
1171 re
->after
.layout
= 0 ; break;
1173 re
->after
.layout
= ALGORITHM_PARITY_N
; break;
1178 /* We can only get to RAID4 from RAID5 */
1179 if (re
->level
!= 4 && re
->level
!= 5)
1180 return "Cannot convert to RAID4 from this level";
1182 switch (re
->level
) {
1184 re
->after
.layout
= 0 ; break;
1186 re
->after
.layout
= ALGORITHM_PARITY_N
; break;
1191 /* We get to RAID5 for RAID5 or RAID6 */
1192 if (re
->level
!= 5 && re
->level
!= 6)
1193 return "Cannot convert to RAID5 from this level";
1195 switch (re
->level
) {
1197 if (info
->new_layout
== UnSet
)
1198 re
->after
.layout
= re
->before
.layout
;
1200 re
->after
.layout
= info
->new_layout
;
1203 if (info
->new_layout
== UnSet
)
1204 info
->new_layout
= re
->before
.layout
;
1206 /* after.layout needs to be raid6 version of new_layout */
1207 if (info
->new_layout
== ALGORITHM_PARITY_N
)
1208 re
->after
.layout
= ALGORITHM_PARITY_N
;
1211 char *ls
= map_num(r5layout
, info
->new_layout
);
1213 strcat(strcpy(layout
, ls
), "-6");
1214 l
= map_name(r6layout
, layout
);
1216 return "Cannot find RAID6 layout"
1218 re
->after
.layout
= l
;
1224 /* We must already be at level 6 */
1226 return "Impossible level change";
1227 if (info
->new_layout
== UnSet
)
1228 re
->after
.layout
= info
->array
.layout
;
1230 re
->after
.layout
= info
->new_layout
;
1233 return "Impossible level change requested";
1235 if (info
->delta_disks
== UnSet
)
1236 info
->delta_disks
= delta_parity
;
1238 re
->after
.data_disks
= (re
->before
.data_disks
1241 switch (re
->level
) {
1242 case 6: re
->parity
= 2; break;
1244 case 5: re
->parity
= 1; break;
1245 default: re
->parity
= 0; break;
1247 /* So we have a restripe operation, we need to calculate the number
1248 * of blocks per reshape operation.
1250 if (info
->new_chunk
== 0)
1251 info
->new_chunk
= info
->array
.chunk_size
;
1252 if (re
->after
.data_disks
== re
->before
.data_disks
&&
1253 re
->after
.layout
== re
->before
.layout
&&
1254 info
->new_chunk
== info
->array
.chunk_size
) {
1255 /* Nothing to change */
1256 re
->backup_blocks
= 0;
1259 if (re
->after
.data_disks
== 1 && re
->before
.data_disks
== 1) {
1260 /* chunk and layout changes make no difference */
1261 re
->backup_blocks
= 0;
1265 if (re
->after
.data_disks
== re
->before
.data_disks
&&
1266 get_linux_version() < 2006032)
1267 return "in-place reshape is not safe before 2.6.32 - sorry.";
1269 if (re
->after
.data_disks
< re
->before
.data_disks
&&
1270 get_linux_version() < 2006030)
1271 return "reshape to fewer devices is not supported before 2.6.32 - sorry.";
1273 re
->backup_blocks
= compute_backup_blocks(
1274 info
->new_chunk
, info
->array
.chunk_size
,
1275 re
->after
.data_disks
,
1276 re
->before
.data_disks
);
1278 re
->new_size
= info
->component_size
* re
->after
.data_disks
;
1282 static int reshape_array(char *container
, int fd
, char *devname
,
1283 struct supertype
*st
, struct mdinfo
*info
,
1284 int force
, struct mddev_dev
*devlist
,
1285 char *backup_file
, int quiet
, int forked
,
1287 static int reshape_container(char *container
, char *devname
,
1288 struct supertype
*st
,
1289 struct mdinfo
*info
,
1292 int quiet
, int restart
);
1294 int Grow_reshape(char *devname
, int fd
, int quiet
, char *backup_file
,
1296 int level
, char *layout_str
, int chunksize
, int raid_disks
,
1297 struct mddev_dev
*devlist
,
1298 int assume_clean
, int force
)
1300 /* Make some changes in the shape of an array.
1301 * The kernel must support the change.
1303 * There are three different changes. Each can trigger
1304 * a resync or recovery so we freeze that until we have
1305 * requested everything (if kernel supports freezing - 2.6.30).
1307 * - change size (i.e. component_size)
1309 * - change layout/chunksize/ndisks
1311 * The last can require a reshape. It is different on different
1312 * levels so we need to check the level before actioning it.
1313 * Some times the level change needs to be requested after the
1314 * reshape (e.g. raid6->raid5, raid5->raid0)
1317 struct mdu_array_info_s array
;
1319 struct supertype
*st
;
1320 char *subarray
= NULL
;
1324 char *container
= NULL
;
1325 char container_buf
[20];
1328 struct mddev_dev
*dv
;
1334 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) < 0) {
1335 fprintf(stderr
, Name
": %s is not an active md array - aborting\n",
1341 (chunksize
|| level
!= UnSet
|| layout_str
|| raid_disks
)) {
1342 fprintf(stderr
, Name
": cannot change component size at the same time "
1343 "as other changes.\n"
1344 " Change size first, then check data is intact before "
1345 "making other changes.\n");
1349 if (raid_disks
&& raid_disks
< array
.raid_disks
&& array
.level
> 1 &&
1350 get_linux_version() < 2006032 &&
1351 !check_env("MDADM_FORCE_FEWER")) {
1352 fprintf(stderr
, Name
": reducing the number of devices is not safe before Linux 2.6.32\n"
1353 " Please use a newer kernel\n");
1357 st
= super_by_fd(fd
, &subarray
);
1359 fprintf(stderr
, Name
": Unable to determine metadata format for %s\n", devname
);
1362 if (raid_disks
> st
->max_devs
) {
1363 fprintf(stderr
, Name
": Cannot increase raid-disks on this array"
1364 " beyond %d\n", st
->max_devs
);
1368 /* in the external case we need to check that the requested reshape is
1369 * supported, and perform an initial check that the container holds the
1370 * pre-requisite spare devices (mdmon owns final validation)
1372 if (st
->ss
->external
) {
1377 container_dev
= st
->container_dev
;
1378 cfd
= open_dev_excl(st
->container_dev
);
1380 container_dev
= st
->devnum
;
1382 cfd
= open_dev_excl(st
->devnum
);
1386 fprintf(stderr
, Name
": Unable to open container for %s\n",
1392 fmt_devname(container_buf
, container_dev
);
1393 container
= container_buf
;
1395 rv
= st
->ss
->load_container(st
, cfd
, NULL
);
1398 fprintf(stderr
, Name
": Cannot read superblock for %s\n",
1404 if (mdmon_running(container_dev
))
1405 st
->update_tail
= &st
->updates
;
1409 for (dv
= devlist
; dv
; dv
= dv
->next
)
1411 if (raid_disks
> array
.raid_disks
&&
1412 array
.spare_disks
+added_disks
< (raid_disks
- array
.raid_disks
) &&
1415 Name
": Need %d spare%s to avoid degraded array,"
1416 " and only have %d.\n"
1417 " Use --force to over-ride this check.\n",
1418 raid_disks
- array
.raid_disks
,
1419 raid_disks
- array
.raid_disks
== 1 ? "" : "s",
1420 array
.spare_disks
+ added_disks
);
1424 sra
= sysfs_read(fd
, 0, GET_LEVEL
| GET_DISKS
| GET_DEVS
1425 | GET_STATE
| GET_VERSION
);
1427 if (st
->ss
->external
&& subarray
== NULL
) {
1428 array
.level
= LEVEL_CONTAINER
;
1429 sra
->array
.level
= LEVEL_CONTAINER
;
1432 fprintf(stderr
, Name
": failed to read sysfs parameters for %s\n",
1436 frozen
= freeze(st
);
1438 /* freeze() already spewed the reason */
1440 } else if (frozen
< 0) {
1441 fprintf(stderr
, Name
": %s is performing resync/recovery and cannot"
1442 " be reshaped\n", devname
);
1446 /* ========= set size =============== */
1447 if (size
>= 0 && (size
== 0 || size
!= array
.size
)) {
1448 long long orig_size
= get_component_size(fd
)/2;
1452 orig_size
= array
.size
;
1454 if (reshape_super(st
, size
, UnSet
, UnSet
, 0, 0, UnSet
, NULL
,
1461 /* Update the size of each member device in case
1462 * they have been resized. This will never reduce
1463 * below the current used-size. The "size" attribute
1464 * understand '0' to mean 'max'.
1466 for (mdi
= sra
->devs
; mdi
; mdi
= mdi
->next
)
1467 sysfs_set_num(sra
, mdi
, "size", size
);
1470 if (array
.size
!= size
) {
1471 /* got truncated to 32bit, write to
1472 * component_size instead
1475 rv
= sysfs_set_num(sra
, NULL
,
1476 "component_size", size
);
1480 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
1484 /* restore metadata */
1485 if (reshape_super(st
, orig_size
, UnSet
, UnSet
, 0, 0,
1486 UnSet
, NULL
, devname
, !quiet
) == 0)
1488 fprintf(stderr
, Name
": Cannot set device size for %s: %s\n",
1489 devname
, strerror(err
));
1491 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1492 fprintf(stderr
, " Bitmap must be removed before size can be changed\n");
1497 /* This will fail on kernels newer than 2.6.40 unless
1498 * a backport has been arranged.
1501 sysfs_set_str(sra
, NULL
, "resync_start", "none") < 0)
1502 fprintf(stderr
, Name
": --assume-clean not support with --grow on this kernel\n");
1504 ioctl(fd
, GET_ARRAY_INFO
, &array
);
1505 size
= get_component_size(fd
)/2;
1509 if (size
== orig_size
)
1510 fprintf(stderr
, Name
": component size of %s "
1511 "unchanged at %lluK\n",
1514 fprintf(stderr
, Name
": component size of %s "
1515 "has been set to %lluK\n",
1519 } else if (array
.level
!= LEVEL_CONTAINER
) {
1520 size
= get_component_size(fd
)/2;
1525 /* See if there is anything else to do */
1526 if ((level
== UnSet
|| level
== array
.level
) &&
1527 (layout_str
== NULL
) &&
1528 (chunksize
== 0 || chunksize
== array
.chunk_size
) &&
1529 (raid_disks
== 0 || raid_disks
== array
.raid_disks
)) {
1530 /* Nothing more to do */
1531 if (!changed
&& !quiet
)
1532 fprintf(stderr
, Name
": %s: no change requested\n",
1537 /* ========= check for Raid10/Raid1 -> Raid0 conversion ===============
1538 * current implementation assumes that following conditions must be met:
1541 * - near_copies == 2
1543 if ((level
== 0 && array
.level
== 10 && sra
&&
1544 array
.layout
== ((1 << 8) + 2) && !(array
.raid_disks
& 1)) ||
1545 (level
== 0 && array
.level
== 1 && sra
)) {
1547 err
= remove_disks_for_takeover(st
, sra
, array
.layout
);
1549 dprintf(Name
": Array cannot be reshaped\n");
1555 /* FIXME this is added with no justification - why is it here */
1556 ping_monitor(container
);
1559 memset(&info
, 0, sizeof(info
));
1561 sysfs_init(&info
, fd
, NoMdDev
);
1562 strcpy(info
.text_version
, sra
->text_version
);
1563 info
.component_size
= size
*2;
1564 info
.new_level
= level
;
1565 info
.new_chunk
= chunksize
* 1024;
1566 if (info
.array
.level
== LEVEL_CONTAINER
) {
1567 info
.delta_disks
= UnSet
;
1568 info
.array
.raid_disks
= raid_disks
;
1569 } else if (raid_disks
)
1570 info
.delta_disks
= raid_disks
- info
.array
.raid_disks
;
1572 info
.delta_disks
= UnSet
;
1573 if (layout_str
== NULL
) {
1574 info
.new_layout
= UnSet
;
1575 if (info
.array
.level
== 6 &&
1576 (info
.new_level
== 6 || info
.new_level
== UnSet
) &&
1577 info
.array
.layout
>= 16) {
1578 fprintf(stderr
, Name
1579 ": %s has a non-standard layout. If you"
1580 " wish to preserve this\n"
1581 " during the reshape, please specify"
1582 " --layout=preserve\n"
1583 " If you want to change it, specify a"
1584 " layout or use --layout=normalise\n",
1589 } else if (strcmp(layout_str
, "normalise") == 0 ||
1590 strcmp(layout_str
, "normalize") == 0) {
1591 /* If we have a -6 RAID6 layout, remove the '-6'. */
1592 info
.new_layout
= UnSet
;
1593 if (info
.array
.level
== 6 && info
.new_level
== UnSet
) {
1595 strcpy(l
, map_num(r6layout
, info
.array
.layout
));
1596 h
= strrchr(l
, '-');
1597 if (h
&& strcmp(h
, "-6") == 0) {
1599 info
.new_layout
= map_name(r6layout
, l
);
1602 } else if (strcmp(layout_str
, "preserve") == 0) {
1603 info
.new_layout
= UnSet
;
1605 int l
= info
.new_level
;
1607 l
= info
.array
.level
;
1610 info
.new_layout
= map_name(r5layout
, layout_str
);
1613 info
.new_layout
= map_name(r6layout
, layout_str
);
1616 info
.new_layout
= parse_layout_10(layout_str
);
1619 info
.new_layout
= parse_layout_faulty(layout_str
);
1622 fprintf(stderr
, Name
": layout not meaningful"
1623 " with this level\n");
1627 if (info
.new_layout
== UnSet
) {
1628 fprintf(stderr
, Name
": layout %s not understood"
1629 " for this level\n",
1636 if (array
.level
== LEVEL_FAULTY
) {
1637 if (level
!= UnSet
&& level
!= array
.level
) {
1638 fprintf(stderr
, Name
": cannot change level of Faulty device\n");
1642 fprintf(stderr
, Name
": cannot set chunksize of Faulty device\n");
1645 if (raid_disks
&& raid_disks
!= 1) {
1646 fprintf(stderr
, Name
": cannot set raid_disks of Faulty device\n");
1650 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
1651 dprintf("Cannot get array information.\n");
1654 array
.layout
= info
.new_layout
;
1655 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
1656 fprintf(stderr
, Name
": failed to set new layout\n");
1659 printf("layout for %s set to %d\n",
1660 devname
, array
.layout
);
1662 } else if (array
.level
== LEVEL_CONTAINER
) {
1663 /* This change is to be applied to every array in the
1664 * container. This is only needed when the metadata imposes
1665 * restraints of the various arrays in the container.
1666 * Currently we only know that IMSM requires all arrays
1667 * to have the same number of devices so changing the
1668 * number of devices (On-Line Capacity Expansion) must be
1669 * performed at the level of the container
1671 rv
= reshape_container(container
, devname
, st
, &info
,
1672 force
, backup_file
, quiet
, 0);
1675 /* get spare devices from external metadata
1677 if (st
->ss
->external
) {
1678 struct mdinfo
*info2
;
1680 info2
= st
->ss
->container_content(st
, subarray
);
1682 info
.array
.spare_disks
=
1683 info2
->array
.spare_disks
;
1688 /* Impose these changes on a single array. First
1689 * check that the metadata is OK with the change. */
1691 if (reshape_super(st
, info
.component_size
, info
.new_level
,
1692 info
.new_layout
, info
.new_chunk
,
1693 info
.array
.raid_disks
, info
.delta_disks
,
1694 backup_file
, devname
, quiet
)) {
1699 rv
= reshape_array(container
, fd
, devname
, st
, &info
, force
,
1700 devlist
, backup_file
, quiet
, 0, 0);
1709 static int reshape_array(char *container
, int fd
, char *devname
,
1710 struct supertype
*st
, struct mdinfo
*info
,
1711 int force
, struct mddev_dev
*devlist
,
1712 char *backup_file
, int quiet
, int forked
,
1715 struct reshape reshape
;
1718 int orig_level
= UnSet
;
1721 struct mdu_array_info_s array
;
1724 struct mddev_dev
*dv
;
1728 unsigned long long *offsets
;
1732 unsigned long blocks
;
1733 unsigned long cache
;
1734 unsigned long long array_size
;
1736 struct mdinfo
*sra
= NULL
;
1738 /* when reshaping a RAID0, the component_size might be zero.
1739 * So try to fix that up.
1741 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
1742 dprintf("Cannot get array information.\n");
1745 if (array
.level
== 0 && info
->component_size
== 0) {
1746 get_dev_size(fd
, NULL
, &array_size
);
1747 info
->component_size
= array_size
/ array
.raid_disks
;
1750 if (info
->reshape_active
) {
1751 int new_level
= info
->new_level
;
1752 info
->new_level
= UnSet
;
1753 info
->array
.raid_disks
-= info
->delta_disks
;
1754 msg
= analyse_change(info
, &reshape
);
1755 info
->new_level
= new_level
;
1756 info
->array
.raid_disks
+= info
->delta_disks
;
1758 /* Make sure the array isn't read-only */
1759 ioctl(fd
, RESTART_ARRAY_RW
, 0);
1761 msg
= analyse_change(info
, &reshape
);
1763 fprintf(stderr
, Name
": %s\n", msg
);
1767 (reshape
.level
!= info
->array
.level
||
1768 reshape
.before
.layout
!= info
->array
.layout
||
1769 reshape
.before
.data_disks
+ reshape
.parity
1770 != info
->array
.raid_disks
- info
->delta_disks
)) {
1771 fprintf(stderr
, Name
": reshape info is not in native format -"
1772 " cannot continue.\n");
1777 /* reshape already started. just skip to monitoring the reshape */
1778 if (reshape
.backup_blocks
== 0)
1782 /* The container is frozen but the array may not be.
1783 * So freeze the array so spares don't get put to the wrong use
1784 * FIXME there should probably be a cleaner separation between
1785 * freeze_array and freeze_container.
1787 sysfs_freeze_array(info
);
1788 /* Check we have enough spares to not be degraded */
1790 for (dv
= devlist
; dv
; dv
=dv
->next
)
1792 spares_needed
= max(reshape
.before
.data_disks
,
1793 reshape
.after
.data_disks
)
1794 + reshape
.parity
- array
.raid_disks
;
1797 info
->new_level
> 1 && info
->array
.level
> 1 &&
1798 spares_needed
> info
->array
.spare_disks
+ added_disks
) {
1800 Name
": Need %d spare%s to avoid degraded array,"
1801 " and only have %d.\n"
1802 " Use --force to over-ride this check.\n",
1804 spares_needed
== 1 ? "" : "s",
1805 info
->array
.spare_disks
+ added_disks
);
1808 /* Check we have enough spares to not fail */
1809 spares_needed
= max(reshape
.before
.data_disks
,
1810 reshape
.after
.data_disks
)
1812 if ((info
->new_level
> 1 || info
->new_level
== 0) &&
1813 spares_needed
> info
->array
.spare_disks
+added_disks
) {
1815 Name
": Need %d spare%s to create working array,"
1816 " and only have %d.\n",
1818 spares_needed
== 1 ? "" : "s",
1819 info
->array
.spare_disks
+ added_disks
);
1823 if (reshape
.level
!= array
.level
) {
1824 char *c
= map_num(pers
, reshape
.level
);
1829 err
= sysfs_set_str(info
, NULL
, "level", c
);
1832 fprintf(stderr
, Name
": %s: could not set level to %s\n",
1835 (info
->array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1836 fprintf(stderr
, " Bitmap must be removed"
1837 " before level can be changed\n");
1841 fprintf(stderr
, Name
": level of %s changed to %s\n",
1843 orig_level
= array
.level
;
1844 sysfs_freeze_array(info
);
1846 if (reshape
.level
> 0 && st
->ss
->external
) {
1847 /* make sure mdmon is aware of the new level */
1848 if (!mdmon_running(st
->container_dev
))
1849 start_mdmon(st
->container_dev
);
1850 ping_monitor(container
);
1853 /* ->reshape_super might have chosen some spares from the
1854 * container that it wants to be part of the new array.
1855 * We can collect them with ->container_content and give
1856 * them to the kernel.
1858 if (st
->ss
->reshape_super
&& st
->ss
->container_content
) {
1859 char *subarray
= strchr(info
->text_version
+1, '/')+1;
1860 struct mdinfo
*info2
=
1861 st
->ss
->container_content(st
, subarray
);
1865 sysfs_init(info2
, fd
, st
->devnum
);
1866 /* When increasing number of devices, we need to set
1867 * new raid_disks before adding these, or they might
1870 if (reshape
.backup_blocks
&&
1871 reshape
.after
.data_disks
> reshape
.before
.data_disks
)
1872 subarray_set_num(container
, info2
, "raid_disks",
1873 reshape
.after
.data_disks
+
1875 for (d
= info2
->devs
; d
; d
= d
->next
) {
1876 if (d
->disk
.state
== 0 &&
1877 d
->disk
.raid_disk
>= 0) {
1878 /* This is a spare that wants to
1879 * be part of the array.
1881 add_disk(fd
, st
, info2
, d
);
1887 /* We might have been given some devices to add to the
1888 * array. Now that the array has been changed to the right
1889 * level and frozen, we can safely add them.
1892 Manage_subdevs(devname
, fd
, devlist
, !quiet
,
1895 if (reshape
.backup_blocks
== 0) {
1896 /* No restriping needed, but we might need to impose
1897 * some more changes: layout, raid_disks, chunk_size
1899 /* read current array info */
1900 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
1901 dprintf("Cannot get array information.\n");
1904 /* compare current array info with new values and if
1905 * it is different update them to new */
1906 if (info
->new_layout
!= UnSet
&&
1907 info
->new_layout
!= array
.layout
) {
1908 array
.layout
= info
->new_layout
;
1909 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
1910 fprintf(stderr
, Name
": failed to set new layout\n");
1913 printf("layout for %s set to %d\n",
1914 devname
, array
.layout
);
1916 if (info
->delta_disks
!= UnSet
&&
1917 info
->delta_disks
!= 0 &&
1918 array
.raid_disks
!= (info
->array
.raid_disks
+ info
->delta_disks
)) {
1919 array
.raid_disks
+= info
->delta_disks
;
1920 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
1921 fprintf(stderr
, Name
": failed to set raid disks\n");
1923 } else if (!quiet
) {
1924 printf("raid_disks for %s set to %d\n",
1925 devname
, array
.raid_disks
);
1928 if (info
->new_chunk
!= 0 &&
1929 info
->new_chunk
!= array
.chunk_size
) {
1930 if (sysfs_set_num(info
, NULL
,
1931 "chunk_size", info
->new_chunk
) != 0) {
1932 fprintf(stderr
, Name
": failed to set chunk size\n");
1935 printf("chunk size for %s set to %d\n",
1936 devname
, array
.chunk_size
);
1943 * There are three possibilities.
1944 * 1/ The array will shrink.
1945 * We need to ensure the reshape will pause before reaching
1946 * the 'critical section'. We also need to fork and wait for
1947 * that to happen. When it does we
1948 * suspend/backup/complete/unfreeze
1950 * 2/ The array will not change size.
1951 * This requires that we keep a backup of a sliding window
1952 * so that we can restore data after a crash. So we need
1953 * to fork and monitor progress.
1954 * In future we will allow the data_offset to change, so
1955 * a sliding backup becomes unnecessary.
1957 * 3/ The array will grow. This is relatively easy.
1958 * However the kernel's restripe routines will cheerfully
1959 * overwrite some early data before it is safe. So we
1960 * need to make a backup of the early parts of the array
1961 * and be ready to restore it if rebuild aborts very early.
1962 * For externally managed metadata, we still need a forked
1963 * child to monitor the reshape and suspend IO over the region
1964 * that is being reshaped.
1966 * We backup data by writing it to one spare, or to a
1967 * file which was given on command line.
1969 * In each case, we first make sure that storage is available
1970 * for the required backup.
1972 * - request the shape change.
1973 * - fork to handle backup etc.
1976 /* Check that we can hold all the data */
1977 get_dev_size(fd
, NULL
, &array_size
);
1978 if (reshape
.new_size
< (array_size
/512)) {
1980 Name
": this change will reduce the size of the array.\n"
1981 " use --grow --array-size first to truncate array.\n"
1982 " e.g. mdadm --grow %s --array-size %llu\n",
1983 devname
, reshape
.new_size
/2);
1987 sra
= sysfs_read(fd
, 0,
1988 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
|
1991 fprintf(stderr
, Name
": %s: Cannot get array details from sysfs\n",
1996 /* Decide how many blocks (sectors) for a reshape
1997 * unit. The number we have so far is just a minimum
1999 blocks
= reshape
.backup_blocks
;
2000 if (reshape
.before
.data_disks
==
2001 reshape
.after
.data_disks
) {
2002 /* Make 'blocks' bigger for better throughput, but
2003 * not so big that we reject it below.
2004 * Try for 16 megabytes
2006 while (blocks
* 32 < sra
->component_size
&&
2010 fprintf(stderr
, Name
": Need to backup %luK of critical "
2011 "section..\n", blocks
/2);
2013 if (blocks
>= sra
->component_size
/2) {
2014 fprintf(stderr
, Name
": %s: Something wrong"
2015 " - reshape aborted\n",
2020 /* Now we need to open all these devices so we can read/write.
2022 nrdisks
= max(reshape
.before
.data_disks
,
2023 reshape
.after
.data_disks
) + reshape
.parity
2024 + sra
->array
.spare_disks
;
2025 fdlist
= malloc((1+nrdisks
) * sizeof(int));
2026 offsets
= malloc((1+nrdisks
) * sizeof(offsets
[0]));
2027 if (!fdlist
|| !offsets
) {
2028 fprintf(stderr
, Name
": malloc failed: grow aborted\n");
2032 odisks
= reshape
.before
.data_disks
+ reshape
.parity
;
2033 d
= reshape_prepare_fdlist(devname
, sra
, odisks
,
2034 nrdisks
, blocks
, backup_file
,
2039 if (backup_file
== NULL
) {
2040 if (reshape
.after
.data_disks
<= reshape
.before
.data_disks
) {
2042 Name
": %s: Cannot grow - need backup-file\n",
2045 } else if (sra
->array
.spare_disks
== 0) {
2046 fprintf(stderr
, Name
": %s: Cannot grow - need a spare or "
2047 "backup-file to backup critical section\n",
2052 if (!reshape_open_backup_file(backup_file
, fd
, devname
,
2054 fdlist
+d
, offsets
+d
, restart
)) {
2060 /* lastly, check that the internal stripe cache is
2061 * large enough, or it won't work.
2062 * It must hold at least 4 stripes of the larger
2065 cache
= max(info
->array
.chunk_size
, info
->new_chunk
);
2066 cache
*= 4; /* 4 stripes minimum */
2067 cache
/= 512; /* convert to sectors */
2068 disks
= min(reshape
.before
.data_disks
, reshape
.after
.data_disks
);
2069 /* make sure there is room for 'blocks' with a bit to spare */
2070 if (cache
< 16 + blocks
/ disks
)
2071 cache
= 16 + blocks
/ disks
;
2072 cache
/= (4096/512); /* Covert from sectors to pages */
2074 if (sra
->cache_size
< cache
)
2075 subarray_set_num(container
, sra
, "stripe_cache_size",
2078 /* Right, everything seems fine. Let's kick things off.
2079 * If only changing raid_disks, use ioctl, else use
2084 sra
->new_chunk
= info
->new_chunk
;
2087 sra
->reshape_progress
= info
->reshape_progress
;
2089 sra
->reshape_progress
= 0;
2090 if (reshape
.after
.data_disks
< reshape
.before
.data_disks
)
2091 /* start from the end of the new array */
2092 sra
->reshape_progress
= (sra
->component_size
2093 * reshape
.after
.data_disks
);
2096 if (info
->array
.chunk_size
== info
->new_chunk
&&
2097 reshape
.before
.layout
== reshape
.after
.layout
&&
2098 st
->ss
->external
== 0) {
2099 /* use SET_ARRAY_INFO but only if reshape hasn't started */
2100 ioctl(fd
, GET_ARRAY_INFO
, &array
);
2101 array
.raid_disks
= reshape
.after
.data_disks
+ reshape
.parity
;
2103 ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2107 Name
": Cannot set device shape for %s: %s\n",
2108 devname
, strerror(errno
));
2111 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2113 " Bitmap must be removed before"
2114 " shape can be changed\n");
2118 } else if (!restart
) {
2119 /* set them all just in case some old 'new_*' value
2120 * persists from some earlier problem.
2123 if (sysfs_set_num(sra
, NULL
, "chunk_size", info
->new_chunk
) < 0)
2125 if (!err
&& sysfs_set_num(sra
, NULL
, "layout",
2126 reshape
.after
.layout
) < 0)
2128 if (!err
&& subarray_set_num(container
, sra
, "raid_disks",
2129 reshape
.after
.data_disks
+
2130 reshape
.parity
) < 0)
2133 fprintf(stderr
, Name
": Cannot set device shape for %s\n",
2137 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2139 " Bitmap must be removed before"
2140 " shape can be changed\n");
2145 err
= start_reshape(sra
, restart
);
2148 Name
": Cannot %s reshape for %s\n",
2149 restart
? "continue" : "start",
2154 sysfs_set_str(sra
, NULL
, "array_state", "active");
2156 /* Now we just need to kick off the reshape and watch, while
2157 * handling backups of the data...
2158 * This is all done by a forked background process.
2160 switch(forked
? 0 : fork()) {
2162 fprintf(stderr
, Name
": Cannot run child to monitor reshape: %s\n",
2173 if (check_env("MDADM_GROW_VERIFY"))
2174 fd
= open(devname
, O_RDONLY
| O_DIRECT
);
2177 mlockall(MCL_FUTURE
);
2179 if (st
->ss
->external
) {
2180 /* metadata handler takes it from here */
2181 done
= st
->ss
->manage_reshape(
2182 fd
, sra
, &reshape
, st
, blocks
,
2184 d
- odisks
, fdlist
+odisks
,
2187 done
= child_monitor(
2188 fd
, sra
, &reshape
, st
, blocks
,
2190 d
- odisks
, fdlist
+odisks
,
2193 if (backup_file
&& done
)
2194 unlink(backup_file
);
2200 if (!st
->ss
->external
&&
2201 !(reshape
.before
.data_disks
!= reshape
.after
.data_disks
2202 && info
->custom_array_size
) &&
2203 info
->new_level
== reshape
.level
&&
2205 /* no need to wait for the reshape to finish as
2206 * there is nothing more to do.
2212 if (st
->ss
->external
) {
2213 /* Re-load the metadata as much could have changed */
2214 int cfd
= open_dev(st
->container_dev
);
2216 ping_monitor(container
);
2217 st
->ss
->free_super(st
);
2218 st
->ss
->load_container(st
, cfd
, container
);
2223 /* set new array size if required customer_array_size is used
2226 if (reshape
.before
.data_disks
!=
2227 reshape
.after
.data_disks
&&
2228 info
->custom_array_size
) {
2229 struct mdinfo
*info2
;
2230 char *subarray
= strchr(info
->text_version
+1, '/')+1;
2232 info2
= st
->ss
->container_content(st
, subarray
);
2234 unsigned long long current_size
= 0;
2235 unsigned long long new_size
=
2236 info2
->custom_array_size
/2;
2238 if (sysfs_get_ll(sra
,
2241 ¤t_size
) == 0 &&
2242 new_size
> current_size
) {
2243 if (sysfs_set_num(sra
, NULL
,
2244 "array_size", new_size
)
2246 dprintf("Error: Cannot"
2249 dprintf("Array size "
2251 dprintf(" from %llu to %llu.\n",
2252 current_size
, new_size
);
2258 if (info
->new_level
!= reshape
.level
) {
2260 c
= map_num(pers
, info
->new_level
);
2262 err
= sysfs_set_str(sra
, NULL
, "level", c
);
2264 fprintf(stderr
, Name\
2265 ": %s: could not set level "
2266 "to %s\n", devname
, c
);
2276 if (orig_level
!= UnSet
&& sra
) {
2277 c
= map_num(pers
, orig_level
);
2278 if (c
&& sysfs_set_str(sra
, NULL
, "level", c
) == 0)
2279 fprintf(stderr
, Name
": aborting level change\n");
2286 int reshape_container(char *container
, char *devname
,
2287 struct supertype
*st
,
2288 struct mdinfo
*info
,
2291 int quiet
, int restart
)
2293 struct mdinfo
*cc
= NULL
;
2296 /* component_size is not meaningful for a container,
2297 * so pass '-1' meaning 'no change'
2300 reshape_super(st
, -1, info
->new_level
,
2301 info
->new_layout
, info
->new_chunk
,
2302 info
->array
.raid_disks
, info
->delta_disks
,
2303 backup_file
, devname
, quiet
)) {
2310 /* ping monitor to be sure that update is on disk
2312 ping_monitor(container
);
2315 case -1: /* error */
2316 perror("Cannot fork to complete reshape\n");
2319 default: /* parent */
2320 printf(Name
": multi-array reshape continues in background\n");
2327 /* For each member array with reshape_active,
2328 * we need to perform the reshape.
2329 * We pick the first array that needs reshaping and
2330 * reshape it. reshape_array() will re-read the metadata
2331 * so the next time through a different array should be
2332 * ready for reshape.
2333 * It is possible that the 'different' array will not
2334 * be assembled yet. In that case we simple exit.
2335 * When it is assembled, the mdadm which assembles it
2336 * will take over the reshape.
2338 struct mdinfo
*content
;
2340 struct mdstat_ent
*mdstat
;
2345 cc
= st
->ss
->container_content(st
, NULL
);
2347 for (content
= cc
; content
; content
= content
->next
) {
2349 if (!content
->reshape_active
)
2352 subarray
= strchr(content
->text_version
+1, '/')+1;
2353 mdstat
= mdstat_by_subdev(subarray
,
2354 devname2devnum(container
));
2362 fd
= open_dev(mdstat
->devnum
);
2365 adev
= map_dev(dev2major(mdstat
->devnum
),
2366 dev2minor(mdstat
->devnum
),
2369 adev
= content
->text_version
;
2371 sysfs_init(content
, fd
, mdstat
->devnum
);
2373 rv
= reshape_array(container
, fd
, adev
, st
,
2374 content
, force
, NULL
,
2375 backup_file
, quiet
, 1, restart
);
2388 * We run a child process in the background which performs the following
2390 * - wait for resync to reach a certain point
2391 * - suspend io to the following section
2392 * - backup that section
2393 * - allow resync to proceed further
2395 * - discard the backup.
2397 * When are combined in slightly different ways in the three cases.
2399 * - suspend/backup/allow/wait/resume/discard
2401 * - allow/wait/suspend/backup/allow/wait/resume/discard
2403 * - wait/resume/discard/suspend/backup/allow
2405 * suspend/backup/allow always come together
2406 * wait/resume/discard do too.
2407 * For the same-size case we have two backups to improve flow.
2411 int progress_reshape(struct mdinfo
*info
, struct reshape
*reshape
,
2412 unsigned long long backup_point
,
2413 unsigned long long wait_point
,
2414 unsigned long long *suspend_point
,
2415 unsigned long long *reshape_completed
)
2417 /* This function is called repeatedly by the reshape manager.
2418 * It determines how much progress can safely be made and allows
2420 * - 'info' identifies the array and particularly records in
2421 * ->reshape_progress the metadata's knowledge of progress
2422 * This is a sector offset from the start of the array
2423 * of the next array block to be relocated. This number
2424 * may increase from 0 or decrease from array_size, depending
2425 * on the type of reshape that is happening.
2426 * Note that in contrast, 'sync_completed' is a block count of the
2427 * reshape so far. It gives the distance between the start point
2428 * (head or tail of device) and the next place that data will be
2429 * written. It always increases.
2430 * - 'reshape' is the structure created by analyse_change
2431 * - 'backup_point' shows how much the metadata manager has backed-up
2432 * data. For reshapes with increasing progress, it is the next address
2433 * to be backed up, previous addresses have been backed-up. For
2434 * decreasing progress, it is the earliest address that has been
2435 * backed up - later address are also backed up.
2436 * So addresses between reshape_progress and backup_point are
2437 * backed up providing those are in the 'correct' order.
2438 * - 'wait_point' is an array address. When reshape_completed
2439 * passes this point, progress_reshape should return. It might
2440 * return earlier if it determines that ->reshape_progress needs
2441 * to be updated or further backup is needed.
2442 * - suspend_point is maintained by progress_reshape and the caller
2443 * should not touch it except to initialise to zero.
2444 * It is an array address and it only increases in 2.6.37 and earlier.
2445 * This makes it difficult to handle reducing reshapes with
2446 * external metadata.
2447 * However: it is similar to backup_point in that it records the
2448 * other end of a suspended region from reshape_progress.
2449 * it is moved to extend the region that is safe to backup and/or
2451 * - reshape_completed is read from sysfs and returned. The caller
2452 * should copy this into ->reshape_progress when it has reason to
2453 * believe that the metadata knows this, and any backup outside this
2457 * 1 if more data from backup_point - but only as far as suspend_point,
2458 * should be backed up
2459 * 0 if things are progressing smoothly
2460 * -1 if the reshape is finished because it is all done,
2461 * -2 if the reshape is finished due to an error.
2464 int advancing
= (reshape
->after
.data_disks
2465 >= reshape
->before
.data_disks
);
2466 unsigned long long need_backup
; /* All data between start of array and
2467 * here will at some point need to
2470 unsigned long long read_offset
, write_offset
;
2471 unsigned long long write_range
;
2472 unsigned long long max_progress
, target
, completed
;
2473 unsigned long long array_size
= (info
->component_size
2474 * reshape
->before
.data_disks
);
2478 /* First, we unsuspend any region that is now known to be safe.
2479 * If suspend_point is on the 'wrong' side of reshape_progress, then
2480 * we don't have or need suspension at the moment. This is true for
2481 * native metadata when we don't need to back-up.
2484 if (info
->reshape_progress
<= *suspend_point
)
2485 sysfs_set_num(info
, NULL
, "suspend_lo",
2486 info
->reshape_progress
);
2488 /* Note: this won't work in 2.6.37 and before.
2489 * Something somewhere should make sure we don't need it!
2491 if (info
->reshape_progress
>= *suspend_point
)
2492 sysfs_set_num(info
, NULL
, "suspend_hi",
2493 info
->reshape_progress
);
2496 /* Now work out how far it is safe to progress.
2497 * If the read_offset for ->reshape_progress is less than
2498 * 'blocks' beyond the write_offset, we can only progress as far
2500 * Otherwise we can progress until the write_offset for the new location
2501 * reaches (within 'blocks' of) the read_offset at the current location.
2502 * However that region must be suspended unless we are using native
2504 * If we need to suspend more, we limit it to 128M per device, which is
2505 * rather arbitrary and should be some time-based calculation.
2507 read_offset
= info
->reshape_progress
/ reshape
->before
.data_disks
;
2508 write_offset
= info
->reshape_progress
/ reshape
->after
.data_disks
;
2509 write_range
= info
->new_chunk
/512;
2510 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
2511 need_backup
= array_size
;
2513 need_backup
= reshape
->backup_blocks
;
2515 if (read_offset
< write_offset
+ write_range
)
2516 max_progress
= backup_point
;
2520 reshape
->after
.data_disks
;
2522 if (read_offset
> write_offset
- write_range
)
2523 /* Can only progress as far as has been backed up,
2524 * which must be suspended */
2525 max_progress
= backup_point
;
2526 else if (info
->reshape_progress
<= need_backup
)
2527 max_progress
= backup_point
;
2529 if (info
->array
.major_version
>= 0)
2530 /* Can progress until backup is needed */
2531 max_progress
= need_backup
;
2533 /* Can progress until metadata update is required */
2536 reshape
->after
.data_disks
;
2537 /* but data must be suspended */
2538 if (max_progress
< *suspend_point
)
2539 max_progress
= *suspend_point
;
2544 /* We know it is safe to progress to 'max_progress' providing
2545 * it is suspended or we are using native metadata.
2546 * Consider extending suspend_point 128M per device if it
2547 * is less than 64M per device beyond reshape_progress.
2548 * But always do a multiple of 'blocks'
2549 * FIXME this is too big - it takes to long to complete
2552 target
= 64*1024*2 * min(reshape
->before
.data_disks
,
2553 reshape
->after
.data_disks
);
2554 target
/= reshape
->backup_blocks
;
2557 target
*= reshape
->backup_blocks
;
2559 /* For externally managed metadata we always need to suspend IO to
2560 * the area being reshaped so we regularly push suspend_point forward.
2561 * For native metadata we only need the suspend if we are going to do
2565 if ((need_backup
> info
->reshape_progress
2566 || info
->array
.major_version
< 0) &&
2567 *suspend_point
< info
->reshape_progress
+ target
) {
2568 if (need_backup
< *suspend_point
+ 2 * target
)
2569 *suspend_point
= need_backup
;
2570 else if (*suspend_point
+ 2 * target
< array_size
)
2571 *suspend_point
+= 2 * target
;
2573 *suspend_point
= array_size
;
2574 sysfs_set_num(info
, NULL
, "suspend_hi", *suspend_point
);
2575 if (max_progress
> *suspend_point
)
2576 max_progress
= *suspend_point
;
2579 if (info
->array
.major_version
>= 0) {
2580 /* Only need to suspend when about to backup */
2581 if (info
->reshape_progress
< need_backup
* 2 &&
2582 *suspend_point
> 0) {
2584 sysfs_set_num(info
, NULL
, "suspend_lo", 0);
2585 sysfs_set_num(info
, NULL
, "suspend_hi", need_backup
);
2588 /* Need to suspend continually */
2589 if (info
->reshape_progress
< *suspend_point
)
2590 *suspend_point
= info
->reshape_progress
;
2591 if (*suspend_point
+ target
< info
->reshape_progress
)
2592 /* No need to move suspend region yet */;
2594 if (*suspend_point
>= 2 * target
)
2595 *suspend_point
-= 2 * target
;
2598 sysfs_set_num(info
, NULL
, "suspend_lo",
2601 if (max_progress
< *suspend_point
)
2602 max_progress
= *suspend_point
;
2606 /* now set sync_max to allow that progress. sync_max, like
2607 * sync_completed is a count of sectors written per device, so
2608 * we find the difference between max_progress and the start point,
2609 * and divide that by after.data_disks to get a sync_max
2611 * At the same time we convert wait_point to a similar number
2612 * for comparing against sync_completed.
2614 /* scale down max_progress to per_disk */
2615 max_progress
/= reshape
->after
.data_disks
;
2616 /* Round to chunk size as some kernels give an erroneously high number */
2617 max_progress
/= info
->new_chunk
/512;
2618 max_progress
*= info
->new_chunk
/512;
2619 /* And round to old chunk size as the kernel wants that */
2620 max_progress
/= info
->array
.chunk_size
/512;
2621 max_progress
*= info
->array
.chunk_size
/512;
2622 /* Limit progress to the whole device */
2623 if (max_progress
> info
->component_size
)
2624 max_progress
= info
->component_size
;
2625 wait_point
/= reshape
->after
.data_disks
;
2627 /* switch from 'device offset' to 'processed block count' */
2628 max_progress
= info
->component_size
- max_progress
;
2629 wait_point
= info
->component_size
- wait_point
;
2632 sysfs_set_num(info
, NULL
, "sync_max", max_progress
);
2634 /* Now wait. If we have already reached the point that we were
2635 * asked to wait to, don't wait at all, else wait for any change.
2636 * We need to select on 'sync_completed' as that is the place that
2637 * notifications happen, but we are really interested in
2638 * 'reshape_position'
2640 fd
= sysfs_get_fd(info
, NULL
, "sync_completed");
2642 goto check_progress
;
2644 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
2645 goto check_progress
;
2647 while (completed
< max_progress
&& completed
< wait_point
) {
2648 /* Check that sync_action is still 'reshape' to avoid
2649 * waiting forever on a dead array
2653 if (sysfs_get_str(info
, NULL
, "sync_action",
2655 strncmp(action
, "reshape", 7) != 0)
2657 /* Some kernels reset 'sync_completed' to zero
2658 * before setting 'sync_action' to 'idle'.
2659 * So we need these extra tests.
2661 if (completed
== 0 && advancing
2662 && info
->reshape_progress
> 0)
2664 if (completed
== 0 && !advancing
2665 && info
->reshape_progress
< (info
->component_size
2666 * reshape
->after
.data_disks
))
2670 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
2671 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
2672 goto check_progress
;
2674 /* Some kernels reset 'sync_completed' to zero,
2675 * we need to have real point we are in md
2678 completed
= max_progress
;
2680 /* some kernels can give an incorrectly high 'completed' number */
2681 completed
/= (info
->new_chunk
/512);
2682 completed
*= (info
->new_chunk
/512);
2683 /* Convert 'completed' back in to a 'progress' number */
2684 completed
*= reshape
->after
.data_disks
;
2686 completed
= info
->component_size
* reshape
->after
.data_disks
2689 *reshape_completed
= completed
;
2693 /* We return the need_backup flag. Caller will decide
2694 * how much - a multiple of ->backup_blocks up to *suspend_point
2697 return need_backup
> info
->reshape_progress
;
2699 return need_backup
>= info
->reshape_progress
;
2702 /* if we couldn't read a number from sync_completed, then
2703 * either the reshape did complete, or it aborted.
2704 * We can tell which by checking for 'none' in reshape_position.
2705 * If it did abort, then it might immediately restart if it
2706 * it was just a device failure that leaves us degraded but
2710 if (sysfs_get_str(info
, NULL
, "reshape_position", buf
, sizeof(buf
)) < 0
2711 || strncmp(buf
, "none", 4) != 0) {
2712 /* The abort might only be temporary. Wait up to 10
2713 * seconds for fd to contain a valid number again.
2719 while (fd
>= 0 && rv
< 0) {
2723 if (select(fd
+1, NULL
, NULL
, &rfds
, &tv
) != 1)
2725 if (sysfs_fd_get_ll(fd
, &completed
) >= 0)
2726 /* all good again */
2731 return rv
; /* abort */
2733 /* Maybe racing with array shutdown - check state */
2736 if (sysfs_get_str(info
, NULL
, "array_state", buf
, sizeof(buf
)) < 0
2737 || strncmp(buf
, "inactive", 8) == 0
2738 || strncmp(buf
, "clear",5) == 0)
2739 return -2; /* abort */
2740 return -1; /* complete */
2745 /* FIXME return status is never checked */
2746 static int grow_backup(struct mdinfo
*sra
,
2747 unsigned long long offset
, /* per device */
2748 unsigned long stripes
, /* per device, in old chunks */
2749 int *sources
, unsigned long long *offsets
,
2750 int disks
, int chunk
, int level
, int layout
,
2751 int dests
, int *destfd
, unsigned long long *destoffsets
,
2752 int part
, int *degraded
,
2755 /* Backup 'blocks' sectors at 'offset' on each device of the array,
2756 * to storage 'destfd' (offset 'destoffsets'), after first
2757 * suspending IO. Then allow resync to continue
2758 * over the suspended section.
2759 * Use part 'part' of the backup-super-block.
2764 unsigned long long ll
;
2766 //printf("offset %llu\n", offset);
2772 /* Check that array hasn't become degraded, else we might backup the wrong data */
2773 if (sysfs_get_ll(sra
, NULL
, "degraded", &ll
) < 0)
2774 return -1; /* FIXME this error is ignored */
2775 new_degraded
= (int)ll
;
2776 if (new_degraded
!= *degraded
) {
2777 /* check each device to ensure it is still working */
2779 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2780 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2782 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
2784 if (sysfs_get_str(sra
, sd
, "state", sbuf
, 20) < 0 ||
2785 strstr(sbuf
, "faulty") ||
2786 strstr(sbuf
, "in_sync") == NULL
) {
2787 /* this device is dead */
2788 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
2789 if (sd
->disk
.raid_disk
>= 0 &&
2790 sources
[sd
->disk
.raid_disk
] >= 0) {
2791 close(sources
[sd
->disk
.raid_disk
]);
2792 sources
[sd
->disk
.raid_disk
] = -1;
2797 *degraded
= new_degraded
;
2800 bsb
.arraystart2
= __cpu_to_le64(offset
* odata
);
2801 bsb
.length2
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
2803 bsb
.arraystart
= __cpu_to_le64(offset
* odata
);
2804 bsb
.length
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
2807 bsb
.magic
[15] = '2';
2808 for (i
= 0; i
< dests
; i
++)
2810 lseek64(destfd
[i
], destoffsets
[i
] + __le64_to_cpu(bsb
.devstart2
)*512, 0);
2812 lseek64(destfd
[i
], destoffsets
[i
], 0);
2814 rv
= save_stripes(sources
, offsets
,
2815 disks
, chunk
, level
, layout
,
2817 offset
*512*odata
, stripes
* chunk
* odata
,
2822 bsb
.mtime
= __cpu_to_le64(time(0));
2823 for (i
= 0; i
< dests
; i
++) {
2824 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
2826 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
2827 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
2828 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
2829 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
2832 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
] - 4096, 0)
2833 != destoffsets
[i
] - 4096)
2835 if (write(destfd
[i
], &bsb
, 512) != 512)
2837 if (destoffsets
[i
] > 4096) {
2838 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]+stripes
*chunk
*odata
, 0) !=
2839 destoffsets
[i
]+stripes
*chunk
*odata
)
2841 if (write(destfd
[i
], &bsb
, 512) != 512)
2851 /* in 2.6.30, the value reported by sync_completed can be
2852 * less that it should be by one stripe.
2853 * This only happens when reshape hits sync_max and pauses.
2854 * So allow wait_backup to either extent sync_max further
2855 * than strictly necessary, or return before the
2856 * sync has got quite as far as we would really like.
2857 * This is what 'blocks2' is for.
2858 * The various caller give appropriate values so that
2861 /* FIXME return value is often ignored */
2862 static int forget_backup(
2863 int dests
, int *destfd
, unsigned long long *destoffsets
,
2867 * Erase backup 'part' (which is 0 or 1)
2873 bsb
.arraystart2
= __cpu_to_le64(0);
2874 bsb
.length2
= __cpu_to_le64(0);
2876 bsb
.arraystart
= __cpu_to_le64(0);
2877 bsb
.length
= __cpu_to_le64(0);
2879 bsb
.mtime
= __cpu_to_le64(time(0));
2881 for (i
= 0; i
< dests
; i
++) {
2882 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
2883 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
2884 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
2885 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
2886 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
2887 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]-4096, 0) !=
2888 destoffsets
[i
]-4096)
2891 write(destfd
[i
], &bsb
, 512) != 512)
2898 static void fail(char *msg
)
2901 rv
= (write(2, msg
, strlen(msg
)) != (int)strlen(msg
));
2902 rv
|= (write(2, "\n", 1) != 1);
2906 static char *abuf
, *bbuf
;
2907 static unsigned long long abuflen
;
2908 static void validate(int afd
, int bfd
, unsigned long long offset
)
2910 /* check that the data in the backup against the array.
2911 * This is only used for regression testing and should not
2912 * be used while the array is active
2916 lseek64(bfd
, offset
- 4096, 0);
2917 if (read(bfd
, &bsb2
, 512) != 512)
2918 fail("cannot read bsb");
2919 if (bsb2
.sb_csum
!= bsb_csum((char*)&bsb2
,
2920 ((char*)&bsb2
.sb_csum
)-((char*)&bsb2
)))
2921 fail("first csum bad");
2922 if (memcmp(bsb2
.magic
, "md_backup_data", 14) != 0)
2923 fail("magic is bad");
2924 if (memcmp(bsb2
.magic
, "md_backup_data-2", 16) == 0 &&
2925 bsb2
.sb_csum2
!= bsb_csum((char*)&bsb2
,
2926 ((char*)&bsb2
.sb_csum2
)-((char*)&bsb2
)))
2927 fail("second csum bad");
2929 if (__le64_to_cpu(bsb2
.devstart
)*512 != offset
)
2930 fail("devstart is wrong");
2933 unsigned long long len
= __le64_to_cpu(bsb2
.length
)*512;
2935 if (abuflen
< len
) {
2939 if (posix_memalign((void**)&abuf
, 4096, abuflen
) ||
2940 posix_memalign((void**)&bbuf
, 4096, abuflen
)) {
2942 /* just stop validating on mem-alloc failure */
2947 lseek64(bfd
, offset
, 0);
2948 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
) {
2949 //printf("len %llu\n", len);
2950 fail("read first backup failed");
2952 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart
)*512, 0);
2953 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
2954 fail("read first from array failed");
2955 if (memcmp(bbuf
, abuf
, len
) != 0) {
2958 printf("offset=%llu len=%llu\n",
2959 (unsigned long long)__le64_to_cpu(bsb2
.arraystart
)*512, len
);
2960 for (i
=0; i
<len
; i
++)
2961 if (bbuf
[i
] != abuf
[i
]) {
2962 printf("first diff byte %d\n", i
);
2966 fail("data1 compare failed");
2970 unsigned long long len
= __le64_to_cpu(bsb2
.length2
)*512;
2972 if (abuflen
< len
) {
2976 abuf
= malloc(abuflen
);
2977 bbuf
= malloc(abuflen
);
2980 lseek64(bfd
, offset
+__le64_to_cpu(bsb2
.devstart2
)*512, 0);
2981 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
)
2982 fail("read second backup failed");
2983 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart2
)*512, 0);
2984 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
2985 fail("read second from array failed");
2986 if (memcmp(bbuf
, abuf
, len
) != 0)
2987 fail("data2 compare failed");
2991 int child_monitor(int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
2992 struct supertype
*st
, unsigned long blocks
,
2993 int *fds
, unsigned long long *offsets
,
2994 int dests
, int *destfd
, unsigned long long *destoffsets
)
2996 /* Monitor a reshape where backup is being performed using
2997 * 'native' mechanism - either to a backup file, or
2998 * to some space in a spare.
3002 unsigned long long speed
;
3003 unsigned long long suspend_point
, array_size
;
3004 unsigned long long backup_point
, wait_point
;
3005 unsigned long long reshape_completed
;
3007 int increasing
= reshape
->after
.data_disks
>= reshape
->before
.data_disks
;
3008 int part
= 0; /* The next part of the backup area to fill. It may already
3009 * be full, so we need to check */
3010 int level
= reshape
->level
;
3011 int layout
= reshape
->before
.layout
;
3012 int data
= reshape
->before
.data_disks
;
3013 int disks
= reshape
->before
.data_disks
+ reshape
->parity
;
3014 int chunk
= sra
->array
.chunk_size
;
3016 unsigned long stripes
;
3018 /* set up the backup-super-block. This requires the
3019 * uuid from the array.
3021 /* Find a superblock */
3022 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
3026 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
3028 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
3029 devfd
= dev_open(dn
, O_RDONLY
);
3032 ok
= st
->ss
->load_super(st
, devfd
, NULL
);
3038 fprintf(stderr
, Name
": Cannot find a superblock\n");
3042 memset(&bsb
, 0, 512);
3043 memcpy(bsb
.magic
, "md_backup_data-1", 16);
3044 st
->ss
->uuid_from_super(st
, (int*)&bsb
.set_uuid
);
3045 bsb
.mtime
= __cpu_to_le64(time(0));
3046 bsb
.devstart2
= blocks
;
3048 stripes
= blocks
/ (sra
->array
.chunk_size
/512) /
3049 reshape
->before
.data_disks
;
3051 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
3052 /* Don't start the 'reshape' */
3054 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
) {
3055 sysfs_get_ll(sra
, NULL
, "sync_speed_min", &speed
);
3056 sysfs_set_num(sra
, NULL
, "sync_speed_min", 200000);
3060 array_size
= sra
->component_size
* reshape
->after
.data_disks
;
3061 backup_point
= sra
->reshape_progress
;
3064 array_size
= sra
->component_size
* reshape
->before
.data_disks
;
3065 backup_point
= reshape
->backup_blocks
;
3066 suspend_point
= array_size
;
3072 /* Want to return as soon the oldest backup slot can
3073 * be released as that allows us to start backing up
3074 * some more, providing suspend_point has been
3075 * advanced, which it should have.
3078 wait_point
= array_size
;
3079 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
3080 wait_point
= (__le64_to_cpu(bsb
.arraystart
) +
3081 __le64_to_cpu(bsb
.length
));
3082 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
3083 wait_point
= (__le64_to_cpu(bsb
.arraystart2
) +
3084 __le64_to_cpu(bsb
.length2
));
3087 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
3088 wait_point
= __le64_to_cpu(bsb
.arraystart
);
3089 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
3090 wait_point
= __le64_to_cpu(bsb
.arraystart2
);
3093 rv
= progress_reshape(sra
, reshape
,
3094 backup_point
, wait_point
,
3095 &suspend_point
, &reshape_completed
);
3096 /* external metadata would need to ping_monitor here */
3097 sra
->reshape_progress
= reshape_completed
;
3099 /* Clear any backup region that is before 'here' */
3101 if (__le64_to_cpu(bsb
.length
) > 0 &&
3102 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart
) +
3103 __le64_to_cpu(bsb
.length
)))
3104 forget_backup(dests
, destfd
,
3106 if (__le64_to_cpu(bsb
.length2
) > 0 &&
3107 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart2
) +
3108 __le64_to_cpu(bsb
.length2
)))
3109 forget_backup(dests
, destfd
,
3112 if (__le64_to_cpu(bsb
.length
) > 0 &&
3113 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart
)))
3114 forget_backup(dests
, destfd
,
3116 if (__le64_to_cpu(bsb
.length2
) > 0 &&
3117 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart2
)))
3118 forget_backup(dests
, destfd
,
3127 if (rv
== 0 && increasing
&& !st
->ss
->external
) {
3128 /* No longer need to monitor this reshape */
3134 unsigned long long offset
;
3135 unsigned long actual_stripes
;
3136 /* Need to backup some data.
3137 * If 'part' is not used and the desired
3138 * backup size is suspended, do a backup,
3139 * then consider the next part.
3141 /* Check that 'part' is unused */
3142 if (part
== 0 && __le64_to_cpu(bsb
.length
) != 0)
3144 if (part
== 1 && __le64_to_cpu(bsb
.length2
) != 0)
3147 offset
= backup_point
/ data
;
3148 actual_stripes
= stripes
;
3150 if (offset
+ actual_stripes
* (chunk
/512) >
3151 sra
->component_size
)
3152 actual_stripes
= ((sra
->component_size
- offset
)
3154 if (offset
+ actual_stripes
* (chunk
/512) >
3158 if (offset
< actual_stripes
* (chunk
/512))
3159 actual_stripes
= offset
/ (chunk
/512);
3160 offset
-= actual_stripes
* (chunk
/512);
3161 if (offset
< suspend_point
/data
)
3164 if (actual_stripes
== 0)
3166 grow_backup(sra
, offset
, actual_stripes
,
3168 disks
, chunk
, level
, layout
,
3169 dests
, destfd
, destoffsets
,
3170 part
, °raded
, buf
);
3171 validate(afd
, destfd
[0], destoffsets
[0]);
3172 /* record where 'part' is up to */
3175 backup_point
+= actual_stripes
* (chunk
/512) * data
;
3177 backup_point
-= actual_stripes
* (chunk
/512) * data
;
3181 /* FIXME maybe call progress_reshape one more time instead */
3182 abort_reshape(sra
); /* remove any remaining suspension */
3183 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
3184 sysfs_set_num(sra
, NULL
, "sync_speed_min", speed
);
3190 * If any spare contains md_back_data-1 which is recent wrt mtime,
3191 * write that data into the array and update the super blocks with
3192 * the new reshape_progress
3194 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
, int cnt
,
3195 char *backup_file
, int verbose
)
3199 unsigned long long *offsets
;
3200 unsigned long long nstripe
, ostripe
;
3203 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
3204 if (info
->array
.level
== 6) odata
--; /* number of data disks */
3205 ndata
= info
->array
.raid_disks
- 1;
3206 if (info
->new_level
== 6) ndata
--;
3208 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
3210 if (info
->delta_disks
<= 0)
3211 /* Didn't grow, so the backup file must have
3215 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
3216 struct mdinfo dinfo
;
3219 char *devname
, namebuf
[20];
3220 unsigned long long lo
, hi
;
3222 /* This was a spare and may have some saved data on it.
3223 * Load the superblock, find and load the
3224 * backup_super_block.
3225 * If either fail, go on to next device.
3226 * If the backup contains no new info, just return
3227 * else restore data and update all superblocks
3229 if (i
== old_disks
-1) {
3230 fd
= open(backup_file
, O_RDONLY
);
3232 fprintf(stderr
, Name
": backup file %s inaccessible: %s\n",
3233 backup_file
, strerror(errno
));
3236 devname
= backup_file
;
3241 if (st
->ss
->load_super(st
, fd
, NULL
))
3244 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
3245 st
->ss
->free_super(st
);
3248 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
3250 fprintf(stderr
, Name
": Cannot seek on device %d\n", i
);
3251 continue; /* Cannot seek */
3253 sprintf(namebuf
, "device-%d", i
);
3256 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
)) {
3258 fprintf(stderr
, Name
": Cannot read from %s\n", devname
);
3259 continue; /* Cannot read */
3261 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0 &&
3262 memcmp(bsb
.magic
, "md_backup_data-2", 16) != 0) {
3264 fprintf(stderr
, Name
": No backup metadata on %s\n", devname
);
3267 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
))) {
3269 fprintf(stderr
, Name
": Bad backup-metadata checksum on %s\n", devname
);
3270 continue; /* bad checksum */
3272 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0 &&
3273 bsb
.sb_csum2
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum2
)-((char*)&bsb
))) {
3275 fprintf(stderr
, Name
": Bad backup-metadata checksum2 on %s\n", devname
);
3276 continue; /* Bad second checksum */
3278 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0) {
3280 fprintf(stderr
, Name
": Wrong uuid on backup-metadata on %s\n", devname
);
3281 continue; /* Wrong uuid */
3284 /* array utime and backup-mtime should be updated at much the same time, but it seems that
3285 * sometimes they aren't... So allow considerable flexability in matching, and allow
3286 * this test to be overridden by an environment variable.
3288 if (info
->array
.utime
> (int)__le64_to_cpu(bsb
.mtime
) + 2*60*60 ||
3289 info
->array
.utime
< (int)__le64_to_cpu(bsb
.mtime
) - 10*60) {
3290 if (check_env("MDADM_GROW_ALLOW_OLD")) {
3291 fprintf(stderr
, Name
": accepting backup with timestamp %lu "
3292 "for array with timestamp %lu\n",
3293 (unsigned long)__le64_to_cpu(bsb
.mtime
),
3294 (unsigned long)info
->array
.utime
);
3297 fprintf(stderr
, Name
": too-old timestamp on "
3298 "backup-metadata on %s\n", devname
);
3299 continue; /* time stamp is too bad */
3303 if (bsb
.magic
[15] == '1') {
3304 if (bsb
.length
== 0)
3306 if (info
->delta_disks
>= 0) {
3307 /* reshape_progress is increasing */
3308 if (__le64_to_cpu(bsb
.arraystart
)
3309 + __le64_to_cpu(bsb
.length
)
3310 < info
->reshape_progress
) {
3313 fprintf(stderr
, Name
3314 ": backup-metadata found on %s but is not needed\n", devname
);
3315 continue; /* No new data here */
3318 /* reshape_progress is decreasing */
3319 if (__le64_to_cpu(bsb
.arraystart
) >=
3320 info
->reshape_progress
)
3321 goto nonew
; /* No new data here */
3324 if (bsb
.length
== 0 && bsb
.length2
== 0)
3326 if (info
->delta_disks
>= 0) {
3327 /* reshape_progress is increasing */
3328 if ((__le64_to_cpu(bsb
.arraystart
)
3329 + __le64_to_cpu(bsb
.length
)
3330 < info
->reshape_progress
)
3332 (__le64_to_cpu(bsb
.arraystart2
)
3333 + __le64_to_cpu(bsb
.length2
)
3334 < info
->reshape_progress
))
3335 goto nonew
; /* No new data here */
3337 /* reshape_progress is decreasing */
3338 if (__le64_to_cpu(bsb
.arraystart
) >=
3339 info
->reshape_progress
&&
3340 __le64_to_cpu(bsb
.arraystart2
) >=
3341 info
->reshape_progress
)
3342 goto nonew
; /* No new data here */
3345 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0) {
3348 fprintf(stderr
, Name
3349 ": Failed to verify secondary backup-metadata block on %s\n",
3351 continue; /* Cannot seek */
3353 /* There should be a duplicate backup superblock 4k before here */
3354 if (lseek64(fd
, -4096, 1) < 0 ||
3355 read(fd
, &bsb2
, sizeof(bsb2
)) != sizeof(bsb2
))
3356 goto second_fail
; /* Cannot find leading superblock */
3357 if (bsb
.magic
[15] == '1')
3358 bsbsize
= offsetof(struct mdp_backup_super
, pad1
);
3360 bsbsize
= offsetof(struct mdp_backup_super
, pad
);
3361 if (memcmp(&bsb2
, &bsb
, bsbsize
) != 0)
3362 goto second_fail
; /* Cannot find leading superblock */
3364 /* Now need the data offsets for all devices. */
3365 offsets
= malloc(sizeof(*offsets
)*info
->array
.raid_disks
);
3366 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
3369 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
3370 /* FIXME should be this be an error */
3372 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
3373 st
->ss
->free_super(st
);
3374 offsets
[j
] = dinfo
.data_offset
* 512;
3376 printf(Name
": restoring critical section\n");
3378 if (restore_stripes(fdlist
, offsets
,
3379 info
->array
.raid_disks
,
3383 fd
, __le64_to_cpu(bsb
.devstart
)*512,
3384 __le64_to_cpu(bsb
.arraystart
)*512,
3385 __le64_to_cpu(bsb
.length
)*512, NULL
)) {
3386 /* didn't succeed, so giveup */
3388 fprintf(stderr
, Name
": Error restoring backup from %s\n",
3393 if (bsb
.magic
[15] == '2' &&
3394 restore_stripes(fdlist
, offsets
,
3395 info
->array
.raid_disks
,
3399 fd
, __le64_to_cpu(bsb
.devstart
)*512 +
3400 __le64_to_cpu(bsb
.devstart2
)*512,
3401 __le64_to_cpu(bsb
.arraystart2
)*512,
3402 __le64_to_cpu(bsb
.length2
)*512, NULL
)) {
3403 /* didn't succeed, so giveup */
3405 fprintf(stderr
, Name
": Error restoring second backup from %s\n",
3411 /* Ok, so the data is restored. Let's update those superblocks. */
3415 lo
= __le64_to_cpu(bsb
.arraystart
);
3416 hi
= lo
+ __le64_to_cpu(bsb
.length
);
3418 if (bsb
.magic
[15] == '2' && bsb
.length2
) {
3419 unsigned long long lo1
, hi1
;
3420 lo1
= __le64_to_cpu(bsb
.arraystart2
);
3421 hi1
= lo1
+ __le64_to_cpu(bsb
.length2
);
3425 } else if (lo
< lo1
)
3431 (info
->reshape_progress
< lo
||
3432 info
->reshape_progress
> hi
))
3433 /* backup does not affect reshape_progress*/ ;
3434 else if (info
->delta_disks
>= 0) {
3435 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
) +
3436 __le64_to_cpu(bsb
.length
);
3437 if (bsb
.magic
[15] == '2') {
3438 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
) +
3439 __le64_to_cpu(bsb
.length2
);
3440 if (p2
> info
->reshape_progress
)
3441 info
->reshape_progress
= p2
;
3444 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
);
3445 if (bsb
.magic
[15] == '2') {
3446 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
);
3447 if (p2
< info
->reshape_progress
)
3448 info
->reshape_progress
= p2
;
3451 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
3452 if (fdlist
[j
] < 0) continue;
3453 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
3455 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
3456 dinfo
.reshape_progress
= info
->reshape_progress
;
3457 st
->ss
->update_super(st
, &dinfo
,
3458 "_reshape_progress",
3460 st
->ss
->store_super(st
, fdlist
[j
]);
3461 st
->ss
->free_super(st
);
3465 /* Didn't find any backup data, try to see if any
3468 if (info
->delta_disks
< 0) {
3469 /* When shrinking, the critical section is at the end.
3470 * So see if we are before the critical section.
3472 unsigned long long first_block
;
3473 nstripe
= ostripe
= 0;
3475 while (ostripe
>= nstripe
) {
3476 ostripe
+= info
->array
.chunk_size
/ 512;
3477 first_block
= ostripe
* odata
;
3478 nstripe
= first_block
/ ndata
/ (info
->new_chunk
/512) *
3479 (info
->new_chunk
/512);
3482 if (info
->reshape_progress
>= first_block
)
3485 if (info
->delta_disks
> 0) {
3486 /* See if we are beyond the critical section. */
3487 unsigned long long last_block
;
3488 nstripe
= ostripe
= 0;
3490 while (nstripe
>= ostripe
) {
3491 nstripe
+= info
->new_chunk
/ 512;
3492 last_block
= nstripe
* ndata
;
3493 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
3494 (info
->array
.chunk_size
/512);
3497 if (info
->reshape_progress
>= last_block
)
3500 /* needed to recover critical section! */
3502 fprintf(stderr
, Name
": Failed to find backup of critical section\n");
3506 int Grow_continue(int mdfd
, struct supertype
*st
, struct mdinfo
*info
,
3510 char *container
= NULL
;
3513 err
= sysfs_set_str(info
, NULL
, "array_state", "readonly");
3516 if (st
->ss
->external
) {
3517 fmt_devname(buf
, st
->container_dev
);
3521 if (!mdmon_running(st
->container_dev
))
3522 start_mdmon(st
->container_dev
);
3523 ping_monitor_by_id(st
->container_dev
);
3526 if (info
->reshape_active
== 2) {
3527 int cfd
= open_dev(st
->container_dev
);
3530 st
->ss
->load_container(st
, cfd
, container
);
3532 return reshape_container(container
, NULL
,
3533 st
, info
, 0, backup_file
,
3537 return reshape_array(container
, mdfd
, "array", st
, info
, 1,
3538 NULL
, backup_file
, 0, 0, 1);