2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2013 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>
32 #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
33 #error no endian defined
38 int restore_backup(struct supertype
*st
,
39 struct mdinfo
*content
,
49 int disk_count
= next_spare
+ working_disks
;
50 char *backup_file
= *backup_filep
;
52 dprintf("Called restore_backup()\n");
53 fdlist
= xmalloc(sizeof(int) * disk_count
);
55 enable_fds(next_spare
);
56 for (i
= 0; i
< next_spare
; i
++)
58 for (dev
= content
->devs
; dev
; dev
= dev
->next
) {
64 fd
= dev_open(buf
, O_RDWR
);
66 if (dev
->disk
.raid_disk
>= 0)
67 fdlist
[dev
->disk
.raid_disk
] = fd
;
69 fdlist
[next_spare
++] = fd
;
73 backup_file
= locate_backup(content
->sys_name
);
74 *backup_filep
= backup_file
;
77 if (st
->ss
->external
&& st
->ss
->recover_backup
)
78 err
= st
->ss
->recover_backup(st
, content
);
80 err
= Grow_restart(st
, content
, fdlist
, next_spare
,
81 backup_file
, verbose
> 0);
83 while (next_spare
> 0) {
85 if (fdlist
[next_spare
] >= 0)
86 close(fdlist
[next_spare
]);
90 pr_err("Failed to restore critical section for reshape - sorry.\n");
92 pr_err("Possibly you need to specify a --backup-file\n");
96 dprintf("restore_backup() returns status OK.\n");
100 int Grow_Add_device(char *devname
, int fd
, char *newdev
)
102 /* Add a device to an active array.
103 * Currently, just extend a linear array.
104 * This requires writing a new superblock on the
105 * new device, calling the kernel to add the device,
106 * and if that succeeds, update the superblock on
108 * This means that we need to *find* all other devices.
115 struct supertype
*st
= NULL
;
116 char *subarray
= NULL
;
118 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
119 pr_err("cannot get array info for %s\n", devname
);
123 if (info
.array
.level
!= -1) {
124 pr_err("can only add devices to linear arrays\n");
128 st
= super_by_fd(fd
, &subarray
);
130 pr_err("cannot handle arrays with superblock version %d\n",
131 info
.array
.major_version
);
136 pr_err("Cannot grow linear sub-arrays yet\n");
142 nfd
= open(newdev
, O_RDWR
|O_EXCL
|O_DIRECT
);
144 pr_err("cannot open %s\n", newdev
);
149 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
150 pr_err("%s is not a block device!\n", newdev
);
155 /* now check out all the devices and make sure we can read the
157 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
158 mdu_disk_info_t disk
;
161 st
->ss
->free_super(st
);
164 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
165 pr_err("cannot get device detail for device %d\n",
171 dv
= map_dev(disk
.major
, disk
.minor
, 1);
173 pr_err("cannot find device file for device %d\n",
179 fd2
= dev_open(dv
, O_RDWR
);
181 pr_err("cannot open device file %s\n", dv
);
187 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
188 pr_err("cannot find super block on %s\n", dv
);
196 /* Ok, looks good. Lets update the superblock and write it out to
200 info
.disk
.number
= d
;
201 info
.disk
.major
= major(stb
.st_rdev
);
202 info
.disk
.minor
= minor(stb
.st_rdev
);
203 info
.disk
.raid_disk
= d
;
204 info
.disk
.state
= (1 << MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
);
205 st
->ss
->update_super(st
, &info
, "linear-grow-new", newdev
,
208 if (st
->ss
->store_super(st
, nfd
)) {
209 pr_err("Cannot store new superblock on %s\n",
216 if (ioctl(fd
, ADD_NEW_DISK
, &info
.disk
) != 0) {
217 pr_err("Cannot add new disk to this array\n");
220 /* Well, that seems to have worked.
221 * Now go through and update all superblocks
224 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
225 pr_err("cannot get array info for %s\n", devname
);
230 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
231 mdu_disk_info_t disk
;
235 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
236 pr_err("cannot get device detail for device %d\n",
240 dv
= map_dev(disk
.major
, disk
.minor
, 1);
242 pr_err("cannot find device file for device %d\n",
246 fd2
= dev_open(dv
, O_RDWR
);
248 pr_err("cannot open device file %s\n", dv
);
251 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
252 pr_err("cannot find super block on %s\n", dv
);
256 info
.array
.raid_disks
= nd
+1;
257 info
.array
.nr_disks
= nd
+1;
258 info
.array
.active_disks
= nd
+1;
259 info
.array
.working_disks
= nd
+1;
261 st
->ss
->update_super(st
, &info
, "linear-grow-update", dv
,
264 if (st
->ss
->store_super(st
, fd2
)) {
265 pr_err("Cannot store new superblock on %s\n", dv
);
275 int Grow_addbitmap(char *devname
, int fd
, struct context
*c
, struct shape
*s
)
278 * First check that array doesn't have a bitmap
279 * Then create the bitmap
282 * For internal bitmaps, we need to check the version,
283 * find all the active devices, and write the bitmap block
286 mdu_bitmap_file_t bmf
;
287 mdu_array_info_t array
;
288 struct supertype
*st
;
289 char *subarray
= NULL
;
290 int major
= BITMAP_MAJOR_HI
;
291 int vers
= md_get_version(fd
);
292 unsigned long long bitmapsize
, array_size
;
295 major
= BITMAP_MAJOR_HOSTENDIAN
;
296 pr_err("Warning - bitmaps created on this kernel are not portable\n"
297 " between different architectures. Consider upgrading the Linux kernel.\n");
300 if (s
->bitmap_file
&& strcmp(s
->bitmap_file
, "clustered") == 0)
301 major
= BITMAP_MAJOR_CLUSTERED
;
303 if (ioctl(fd
, GET_BITMAP_FILE
, &bmf
) != 0) {
305 pr_err("Memory allocation failure.\n");
307 pr_err("bitmaps not supported by this kernel.\n");
310 if (bmf
.pathname
[0]) {
311 if (strcmp(s
->bitmap_file
,"none")==0) {
312 if (ioctl(fd
, SET_BITMAP_FILE
, -1)!= 0) {
313 pr_err("failed to remove bitmap %s\n",
319 pr_err("%s already has a bitmap (%s)\n",
320 devname
, bmf
.pathname
);
323 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
324 pr_err("cannot get array status for %s\n", devname
);
327 if (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)) {
328 if (strcmp(s
->bitmap_file
, "none")==0) {
329 array
.state
&= ~(1<<MD_SB_BITMAP_PRESENT
);
330 if (ioctl(fd
, SET_ARRAY_INFO
, &array
)!= 0) {
331 if (array
.state
& (1<<MD_SB_CLUSTERED
))
332 pr_err("failed to remove clustered bitmap.\n");
334 pr_err("failed to remove internal bitmap.\n");
339 pr_err("bitmap already present on %s\n", devname
);
343 if (strcmp(s
->bitmap_file
, "none") == 0) {
344 pr_err("no bitmap found on %s\n", devname
);
347 if (array
.level
<= 0) {
348 pr_err("Bitmaps not meaningful with level %s\n",
349 map_num(pers
, array
.level
)?:"of this array");
352 bitmapsize
= array
.size
;
354 if (get_dev_size(fd
, NULL
, &array_size
) &&
355 array_size
> (0x7fffffffULL
<<9)) {
356 /* Array is big enough that we cannot trust array.size
357 * try other approaches
359 bitmapsize
= get_component_size(fd
);
361 if (bitmapsize
== 0) {
362 pr_err("Cannot reliably determine size of array to create bitmap - sorry.\n");
366 if (array
.level
== 10) {
367 int ncopies
= (array
.layout
&255)*((array
.layout
>>8)&255);
368 bitmapsize
= bitmapsize
* array
.raid_disks
/ ncopies
;
371 st
= super_by_fd(fd
, &subarray
);
373 pr_err("Cannot understand version %d.%d\n",
374 array
.major_version
, array
.minor_version
);
378 pr_err("Cannot add bitmaps to sub-arrays yet\n");
383 if (strcmp(s
->bitmap_file
, "internal") == 0 ||
384 strcmp(s
->bitmap_file
, "clustered") == 0) {
387 int offset_setable
= 0;
389 if (st
->ss
->add_internal_bitmap
== NULL
) {
390 pr_err("Internal bitmaps not supported with %s metadata\n", st
->ss
->name
);
393 st
->nodes
= c
->nodes
;
394 st
->cluster_name
= c
->homecluster
;
395 mdi
= sysfs_read(fd
, NULL
, GET_BITMAP_LOCATION
);
398 for (d
=0; d
< st
->max_devs
; d
++) {
399 mdu_disk_info_t disk
;
402 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
404 if (disk
.major
== 0 &&
407 if ((disk
.state
& (1<<MD_DISK_SYNC
))==0)
409 dv
= map_dev(disk
.major
, disk
.minor
, 1);
411 int fd2
= dev_open(dv
, O_RDWR
);
414 if (st
->ss
->load_super(st
, fd2
, NULL
)==0) {
415 if (st
->ss
->add_internal_bitmap(
417 &s
->bitmap_chunk
, c
->delay
, s
->write_behind
,
418 bitmapsize
, offset_setable
,
421 st
->ss
->write_bitmap(st
, fd2
, NoUpdate
);
423 pr_err("failed to create internal bitmap - chunksize problem.\n");
431 if (offset_setable
) {
432 st
->ss
->getinfo_super(st
, mdi
, NULL
);
433 sysfs_init(mdi
, fd
, NULL
);
434 rv
= sysfs_set_num_signed(mdi
, NULL
, "bitmap/location",
437 if (strcmp(s
->bitmap_file
, "clustered") == 0)
438 array
.state
|= (1<<MD_SB_CLUSTERED
);
439 array
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
440 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
444 pr_err("Cannot add bitmap while array is resyncing or reshaping etc.\n");
445 pr_err("failed to set internal bitmap.\n");
452 int max_devs
= st
->max_devs
;
454 /* try to load a superblock */
455 for (d
= 0; d
< max_devs
; d
++) {
456 mdu_disk_info_t disk
;
460 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
462 if ((disk
.major
==0 && disk
.minor
==0) ||
463 (disk
.state
& (1<<MD_DISK_REMOVED
)))
465 dv
= map_dev(disk
.major
, disk
.minor
, 1);
468 fd2
= dev_open(dv
, O_RDONLY
);
470 if (st
->ss
->load_super(st
, fd2
, NULL
) == 0) {
472 st
->ss
->uuid_from_super(st
, uuid
);
479 pr_err("cannot find UUID for array!\n");
482 if (CreateBitmap(s
->bitmap_file
, c
->force
, (char*)uuid
, s
->bitmap_chunk
,
483 c
->delay
, s
->write_behind
, bitmapsize
, major
)) {
486 bitmap_fd
= open(s
->bitmap_file
, O_RDWR
);
488 pr_err("weird: %s cannot be opened\n",
492 if (ioctl(fd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
495 pr_err("Cannot add bitmap while array is resyncing or reshaping etc.\n");
496 pr_err("Cannot set bitmap file for %s: %s\n",
497 devname
, strerror(err
));
506 * When reshaping an array we might need to backup some data.
507 * This is written to all spares with a 'super_block' describing it.
508 * The superblock goes 4K from the end of the used space on the
510 * It if written after the backup is complete.
511 * It has the following structure.
514 static struct mdp_backup_super
{
515 char magic
[16]; /* md_backup_data-1 or -2 */
518 /* start/sizes in 512byte sectors */
519 __u64 devstart
; /* address on backup device/file of data */
522 __u32 sb_csum
; /* csum of preceeding bytes. */
524 __u64 devstart2
; /* offset in to data of second section */
527 __u32 sb_csum2
; /* csum of preceeding bytes. */
529 } __attribute__((aligned(512))) bsb
, bsb2
;
531 static __u32
bsb_csum(char *buf
, int len
)
535 for (i
= 0; i
< len
; i
++)
536 csum
= (csum
<<3) + buf
[0];
537 return __cpu_to_le32(csum
);
540 static int check_idle(struct supertype
*st
)
542 /* Check that all member arrays for this container, or the
543 * container of this array, are idle
545 char *container
= (st
->container_devnm
[0]
546 ? st
->container_devnm
: st
->devnm
);
547 struct mdstat_ent
*ent
, *e
;
550 ent
= mdstat_read(0, 0);
551 for (e
= ent
; e
; e
= e
->next
) {
552 if (!is_container_member(e
, container
))
554 if (e
->percent
>= 0) {
563 static int freeze_container(struct supertype
*st
)
565 char *container
= (st
->container_devnm
[0]
566 ? st
->container_devnm
: st
->devnm
);
571 if (block_monitor(container
, 1)) {
572 pr_err("failed to freeze container\n");
579 static void unfreeze_container(struct supertype
*st
)
581 char *container
= (st
->container_devnm
[0]
582 ? st
->container_devnm
: st
->devnm
);
584 unblock_monitor(container
, 1);
587 static int freeze(struct supertype
*st
)
589 /* Try to freeze resync/rebuild on this array/container.
590 * Return -1 if the array is busy,
591 * return -2 container cannot be frozen,
592 * return 0 if this kernel doesn't support 'frozen'
593 * return 1 if it worked.
595 if (st
->ss
->external
)
596 return freeze_container(st
);
598 struct mdinfo
*sra
= sysfs_read(-1, st
->devnm
, GET_VERSION
);
604 /* Need to clear any 'read-auto' status */
605 if (sysfs_get_str(sra
, NULL
, "array_state", buf
, 20) > 0 &&
606 strncmp(buf
, "read-auto", 9) == 0)
607 sysfs_set_str(sra
, NULL
, "array_state", "clean");
609 err
= sysfs_freeze_array(sra
);
615 static void unfreeze(struct supertype
*st
)
617 if (st
->ss
->external
)
618 return unfreeze_container(st
);
620 struct mdinfo
*sra
= sysfs_read(-1, st
->devnm
, GET_VERSION
);
624 sysfs_get_str(sra
, NULL
, "sync_action", buf
, 20) > 0
625 && strcmp(buf
, "frozen\n") == 0)
626 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
631 static void wait_reshape(struct mdinfo
*sra
)
633 int fd
= sysfs_get_fd(sra
, NULL
, "sync_action");
639 while (sysfs_fd_get_str(fd
, action
, 20) > 0 &&
640 strncmp(action
, "reshape", 7) == 0)
641 sysfs_wait(fd
, NULL
);
645 static int reshape_super(struct supertype
*st
, unsigned long long size
,
646 int level
, int layout
, int chunksize
, int raid_disks
,
647 int delta_disks
, char *backup_file
, char *dev
,
648 int direction
, int verbose
)
650 /* nothing extra to check in the native case */
651 if (!st
->ss
->external
)
653 if (!st
->ss
->reshape_super
||
654 !st
->ss
->manage_reshape
) {
655 pr_err("%s metadata does not support reshape\n",
660 return st
->ss
->reshape_super(st
, size
, level
, layout
, chunksize
,
661 raid_disks
, delta_disks
, backup_file
, dev
,
665 static void sync_metadata(struct supertype
*st
)
667 if (st
->ss
->external
) {
668 if (st
->update_tail
) {
669 flush_metadata_updates(st
);
670 st
->update_tail
= &st
->updates
;
672 st
->ss
->sync_metadata(st
);
676 static int subarray_set_num(char *container
, struct mdinfo
*sra
, char *name
, int n
)
678 /* when dealing with external metadata subarrays we need to be
679 * prepared to handle EAGAIN. The kernel may need to wait for
680 * mdmon to mark the array active so the kernel can handle
681 * allocations/writeback when preparing the reshape action
682 * (md_allow_write()). We temporarily disable safe_mode_delay
683 * to close a race with the array_state going clean before the
684 * next write to raid_disks / stripe_cache_size
689 /* only 'raid_disks' and 'stripe_cache_size' trigger md_allow_write */
691 (strcmp(name
, "raid_disks") != 0 &&
692 strcmp(name
, "stripe_cache_size") != 0))
693 return sysfs_set_num(sra
, NULL
, name
, n
);
695 rc
= sysfs_get_str(sra
, NULL
, "safe_mode_delay", safe
, sizeof(safe
));
698 sysfs_set_num(sra
, NULL
, "safe_mode_delay", 0);
699 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
700 if (rc
< 0 && errno
== EAGAIN
) {
701 ping_monitor(container
);
702 /* if we get EAGAIN here then the monitor is not active
705 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
707 sysfs_set_str(sra
, NULL
, "safe_mode_delay", safe
);
711 int start_reshape(struct mdinfo
*sra
, int already_running
,
712 int before_data_disks
, int data_disks
)
715 unsigned long long sync_max_to_set
;
717 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
718 err
= sysfs_set_num(sra
, NULL
, "suspend_hi", sra
->reshape_progress
);
719 err
= err
?: sysfs_set_num(sra
, NULL
, "suspend_lo",
720 sra
->reshape_progress
);
721 if (before_data_disks
<= data_disks
)
722 sync_max_to_set
= sra
->reshape_progress
/ data_disks
;
724 sync_max_to_set
= (sra
->component_size
* data_disks
725 - sra
->reshape_progress
) / data_disks
;
726 if (!already_running
)
727 sysfs_set_num(sra
, NULL
, "sync_min", sync_max_to_set
);
728 err
= err
?: sysfs_set_num(sra
, NULL
, "sync_max", sync_max_to_set
);
729 if (!already_running
&& err
== 0) {
732 err
= sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
735 } while (err
&& errno
== EBUSY
&& cnt
-- > 0);
740 void abort_reshape(struct mdinfo
*sra
)
742 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
743 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
744 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
745 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
746 sysfs_set_num(sra
, NULL
, "sync_min", 0);
747 // It isn't safe to reset sync_max as we aren't monitoring.
748 // Array really should be stopped at this point.
751 int remove_disks_for_takeover(struct supertype
*st
,
756 struct mdinfo
*remaining
;
759 if (sra
->array
.level
== 10)
760 nr_of_copies
= layout
& 0xff;
761 else if (sra
->array
.level
== 1)
762 nr_of_copies
= sra
->array
.raid_disks
;
766 remaining
= sra
->devs
;
768 /* for each 'copy', select one device and remove from the list. */
769 for (slot
= 0; slot
< sra
->array
.raid_disks
; slot
+= nr_of_copies
) {
770 struct mdinfo
**diskp
;
773 /* Find a working device to keep */
774 for (diskp
= &remaining
; *diskp
; diskp
= &(*diskp
)->next
) {
775 struct mdinfo
*disk
= *diskp
;
777 if (disk
->disk
.raid_disk
< slot
)
779 if (disk
->disk
.raid_disk
>= slot
+ nr_of_copies
)
781 if (disk
->disk
.state
& (1<<MD_DISK_REMOVED
))
783 if (disk
->disk
.state
& (1<<MD_DISK_FAULTY
))
785 if (!(disk
->disk
.state
& (1<<MD_DISK_SYNC
)))
788 /* We have found a good disk to use! */
790 disk
->next
= sra
->devs
;
799 if (slot
< sra
->array
.raid_disks
) {
800 /* didn't find all slots */
806 sra
->devs
= remaining
;
810 /* Remove all 'remaining' devices from the array */
812 struct mdinfo
*sd
= remaining
;
813 remaining
= sd
->next
;
815 sysfs_set_str(sra
, sd
, "state", "faulty");
816 sysfs_set_str(sra
, sd
, "slot", "none");
817 /* for external metadata disks should be removed in mdmon */
818 if (!st
->ss
->external
)
819 sysfs_set_str(sra
, sd
, "state", "remove");
820 sd
->disk
.state
|= (1<<MD_DISK_REMOVED
);
821 sd
->disk
.state
&= ~(1<<MD_DISK_SYNC
);
822 sd
->next
= sra
->devs
;
828 void reshape_free_fdlist(int *fdlist
,
829 unsigned long long *offsets
,
834 for (i
= 0; i
< size
; i
++)
842 int reshape_prepare_fdlist(char *devname
,
846 unsigned long blocks
,
849 unsigned long long *offsets
)
855 for (d
= 0; d
<= nrdisks
; d
++)
858 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
859 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
861 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
) &&
862 sd
->disk
.raid_disk
< raid_disks
) {
863 char *dn
= map_dev(sd
->disk
.major
,
865 fdlist
[sd
->disk
.raid_disk
]
866 = dev_open(dn
, O_RDONLY
);
867 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
868 if (fdlist
[sd
->disk
.raid_disk
] < 0) {
869 pr_err("%s: cannot open component %s\n",
870 devname
, dn
? dn
: "-unknown-");
874 } else if (backup_file
== NULL
) {
876 char *dn
= map_dev(sd
->disk
.major
,
878 fdlist
[d
] = dev_open(dn
, O_RDWR
);
879 offsets
[d
] = (sd
->data_offset
+ sra
->component_size
- blocks
- 8)*512;
881 pr_err("%s: cannot open component %s\n",
882 devname
, dn
? dn
: "-unknown-");
893 int reshape_open_backup_file(char *backup_file
,
898 unsigned long long *offsets
,
902 /* Return 1 on success, 0 on any form of failure */
903 /* need to check backup file is large enough */
909 *fdlist
= open(backup_file
, O_RDWR
|O_CREAT
|(restart
? O_TRUNC
: O_EXCL
),
913 pr_err("%s: cannot create backup file %s: %s\n",
914 devname
, backup_file
, strerror(errno
));
917 /* Guard against backup file being on array device.
918 * If array is partitioned or if LVM etc is in the
919 * way this will not notice, but it is better than
922 fstat(*fdlist
, &stb
);
925 if (stb
.st_rdev
== dev
) {
926 pr_err("backup file must NOT be on the array being reshaped.\n");
932 for (i
=0; i
< blocks
+ 8 ; i
++) {
933 if (write(*fdlist
, buf
, 512) != 512) {
934 pr_err("%s: cannot create backup file %s: %s\n",
935 devname
, backup_file
, strerror(errno
));
939 if (fsync(*fdlist
) != 0) {
940 pr_err("%s: cannot create backup file %s: %s\n",
941 devname
, backup_file
, strerror(errno
));
945 if (!restart
&& strncmp(backup_file
, MAP_DIR
, strlen(MAP_DIR
)) != 0) {
946 char *bu
= make_backup(sys_name
);
947 if (symlink(backup_file
, bu
))
948 pr_err("Recording backup file in " MAP_DIR
" failed: %s\n",
956 unsigned long compute_backup_blocks(int nchunk
, int ochunk
,
957 unsigned int ndata
, unsigned int odata
)
959 unsigned long a
, b
, blocks
;
960 /* So how much do we need to backup.
961 * We need an amount of data which is both a whole number of
962 * old stripes and a whole number of new stripes.
963 * So LCM for (chunksize*datadisks).
965 a
= (ochunk
/512) * odata
;
966 b
= (nchunk
/512) * ndata
;
969 /* LCM == product / GCD */
970 blocks
= (ochunk
/512) * (nchunk
/512) * odata
* ndata
/ a
;
975 char *analyse_change(char *devname
, struct mdinfo
*info
, struct reshape
*re
)
977 /* Based on the current array state in info->array and
978 * the changes in info->new_* etc, determine:
979 * - whether the change is possible
980 * - Intermediate level/raid_disks/layout
981 * - whether a restriping reshape is needed
982 * - number of sectors in minimum change unit. This
983 * will cover a whole number of stripes in 'before' and
986 * Return message if the change should be rejected
987 * NULL if the change can be achieved
989 * This can be called as part of starting a reshape, or
990 * when assembling an array that is undergoing reshape.
992 int near
, far
, offset
, copies
;
994 int old_chunk
, new_chunk
;
995 /* delta_parity records change in number of devices
996 * caused by level change
998 int delta_parity
= 0;
1000 memset(re
, 0, sizeof(*re
));
1002 /* If a new level not explicitly given, we assume no-change */
1003 if (info
->new_level
== UnSet
)
1004 info
->new_level
= info
->array
.level
;
1006 if (info
->new_chunk
)
1007 switch (info
->new_level
) {
1013 /* chunk size is meaningful, must divide component_size
1016 if (info
->component_size
% (info
->new_chunk
/512)) {
1017 unsigned long long shrink
= info
->component_size
;
1018 shrink
&= ~(unsigned long long)(info
->new_chunk
/512-1);
1019 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk)\n",
1020 info
->new_chunk
/1024, info
->component_size
/2);
1021 pr_err("After shrinking any filesystem, \"mdadm --grow %s --size %llu\"\n",
1023 pr_err("will shrink the array so the given chunk size would work.\n");
1028 return "chunk size not meaningful for this level";
1031 info
->new_chunk
= info
->array
.chunk_size
;
1033 switch (info
->array
.level
) {
1035 return "No reshape is possibly for this RAID level";
1037 if (info
->delta_disks
!= UnSet
)
1038 return "Only --add is supported for LINEAR, setting --raid-disks is not needed";
1040 return "Only --add is supported for LINEAR, other --grow options are not meaningful";
1042 /* RAID1 can convert to RAID1 with different disks, or
1043 * raid5 with 2 disks, or
1046 if (info
->new_level
> 1 &&
1047 (info
->component_size
& 7))
1048 return "Cannot convert RAID1 of this size - reduce size to multiple of 4K first.";
1049 if (info
->new_level
== 0) {
1050 if (info
->delta_disks
!= UnSet
&&
1051 info
->delta_disks
!= 0)
1052 return "Cannot change number of disks with RAID1->RAID0 conversion";
1054 re
->before
.data_disks
= 1;
1055 re
->after
.data_disks
= 1;
1058 if (info
->new_level
== 1) {
1059 if (info
->delta_disks
== UnSet
)
1060 /* Don't know what to do */
1061 return "no change requested for Growing RAID1";
1065 if (info
->array
.raid_disks
== 2 &&
1066 info
->new_level
== 5) {
1069 re
->before
.data_disks
= 1;
1070 if (info
->delta_disks
!= UnSet
&&
1071 info
->delta_disks
!= 0)
1072 re
->after
.data_disks
= 1 + info
->delta_disks
;
1074 re
->after
.data_disks
= 1;
1075 if (re
->after
.data_disks
< 1)
1076 return "Number of disks too small for RAID5";
1078 re
->before
.layout
= ALGORITHM_LEFT_SYMMETRIC
;
1079 info
->array
.chunk_size
= 65536;
1082 /* Could do some multi-stage conversions, but leave that to
1085 return "Impossibly level change request for RAID1";
1088 /* RAID10 can be converted from near mode to
1089 * RAID0 by removing some devices.
1090 * It can also be reshaped if the kernel supports
1093 switch (info
->new_level
) {
1095 if ((info
->array
.layout
& ~0xff) != 0x100)
1096 return "Cannot Grow RAID10 with far/offset layout";
1097 /* number of devices must be multiple of number of copies */
1098 if (info
->array
.raid_disks
% (info
->array
.layout
& 0xff))
1099 return "RAID10 layout too complex for Grow operation";
1101 new_disks
= (info
->array
.raid_disks
1102 / (info
->array
.layout
& 0xff));
1103 if (info
->delta_disks
== UnSet
)
1104 info
->delta_disks
= (new_disks
1105 - info
->array
.raid_disks
);
1107 if (info
->delta_disks
!= new_disks
- info
->array
.raid_disks
)
1108 return "New number of raid-devices impossible for RAID10";
1109 if (info
->new_chunk
&&
1110 info
->new_chunk
!= info
->array
.chunk_size
)
1111 return "Cannot change chunk-size with RAID10 Grow";
1115 re
->before
.data_disks
= new_disks
;
1116 re
->after
.data_disks
= re
->before
.data_disks
;
1120 near
= info
->array
.layout
& 0xff;
1121 far
= (info
->array
.layout
>> 8) & 0xff;
1122 offset
= info
->array
.layout
& 0x10000;
1123 if (far
> 1 && !offset
)
1124 return "Cannot reshape RAID10 in far-mode";
1125 copies
= near
* far
;
1127 old_chunk
= info
->array
.chunk_size
* far
;
1129 if (info
->new_layout
== UnSet
)
1130 info
->new_layout
= info
->array
.layout
;
1132 near
= info
->new_layout
& 0xff;
1133 far
= (info
->new_layout
>> 8) & 0xff;
1134 offset
= info
->new_layout
& 0x10000;
1135 if (far
> 1 && !offset
)
1136 return "Cannot reshape RAID10 to far-mode";
1137 if (near
* far
!= copies
)
1138 return "Cannot change number of copies when reshaping RAID10";
1140 if (info
->delta_disks
== UnSet
)
1141 info
->delta_disks
= 0;
1142 new_disks
= (info
->array
.raid_disks
+
1145 new_chunk
= info
->new_chunk
* far
;
1148 re
->before
.layout
= info
->array
.layout
;
1149 re
->before
.data_disks
= info
->array
.raid_disks
;
1150 re
->after
.layout
= info
->new_layout
;
1151 re
->after
.data_disks
= new_disks
;
1152 /* For RAID10 we don't do backup but do allow reshape,
1153 * so set backup_blocks to INVALID_SECTORS rather than
1155 * And there is no need to synchronise stripes on both
1156 * 'old' and 'new'. So the important
1157 * number is the minimum data_offset difference
1158 * which is the larger of (offset copies * chunk).
1160 re
->backup_blocks
= INVALID_SECTORS
;
1161 re
->min_offset_change
= max(old_chunk
, new_chunk
) / 512;
1162 if (new_disks
< re
->before
.data_disks
&&
1163 info
->space_after
< re
->min_offset_change
)
1164 /* Reduce component size by one chunk */
1165 re
->new_size
= (info
->component_size
-
1166 re
->min_offset_change
);
1168 re
->new_size
= info
->component_size
;
1169 re
->new_size
= re
->new_size
* new_disks
/ copies
;
1173 return "RAID10 can only be changed to RAID0";
1176 /* RAID0 can be converted to RAID10, or to RAID456 */
1177 if (info
->new_level
== 10) {
1178 if (info
->new_layout
== UnSet
&& info
->delta_disks
== UnSet
) {
1179 /* Assume near=2 layout */
1180 info
->new_layout
= 0x102;
1181 info
->delta_disks
= info
->array
.raid_disks
;
1183 if (info
->new_layout
== UnSet
) {
1184 int copies
= 1 + (info
->delta_disks
1185 / info
->array
.raid_disks
);
1186 if (info
->array
.raid_disks
* (copies
-1)
1187 != info
->delta_disks
)
1188 return "Impossible number of devices for RAID0->RAID10";
1189 info
->new_layout
= 0x100 + copies
;
1191 if (info
->delta_disks
== UnSet
) {
1192 int copies
= info
->new_layout
& 0xff;
1193 if (info
->new_layout
!= 0x100 + copies
)
1194 return "New layout impossible for RAID0->RAID10";;
1195 info
->delta_disks
= (copies
- 1) *
1196 info
->array
.raid_disks
;
1198 if (info
->new_chunk
&&
1199 info
->new_chunk
!= info
->array
.chunk_size
)
1200 return "Cannot change chunk-size with RAID0->RAID10";
1203 re
->before
.data_disks
= (info
->array
.raid_disks
+
1205 re
->after
.data_disks
= re
->before
.data_disks
;
1206 re
->before
.layout
= info
->new_layout
;
1210 /* RAID0 can also covert to RAID0/4/5/6 by first converting to
1211 * a raid4 style layout of the final level.
1213 switch (info
->new_level
) {
1218 re
->before
.layout
= 0;
1223 re
->before
.layout
= ALGORITHM_PARITY_N
;
1224 if (info
->new_layout
== UnSet
)
1225 info
->new_layout
= map_name(r5layout
, "default");
1230 re
->before
.layout
= ALGORITHM_PARITY_N
;
1231 if (info
->new_layout
== UnSet
)
1232 info
->new_layout
= map_name(r6layout
, "default");
1235 return "Impossible level change requested";
1237 re
->before
.data_disks
= info
->array
.raid_disks
;
1238 /* determining 'after' layout happens outside this 'switch' */
1242 info
->array
.layout
= ALGORITHM_PARITY_N
;
1244 switch (info
->new_level
) {
1248 re
->level
= info
->array
.level
;
1249 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1250 re
->before
.layout
= info
->array
.layout
;
1254 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1255 re
->before
.layout
= info
->array
.layout
;
1260 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1261 switch (info
->array
.layout
) {
1262 case ALGORITHM_LEFT_ASYMMETRIC
:
1263 re
->before
.layout
= ALGORITHM_LEFT_ASYMMETRIC_6
;
1265 case ALGORITHM_RIGHT_ASYMMETRIC
:
1266 re
->before
.layout
= ALGORITHM_RIGHT_ASYMMETRIC_6
;
1268 case ALGORITHM_LEFT_SYMMETRIC
:
1269 re
->before
.layout
= ALGORITHM_LEFT_SYMMETRIC_6
;
1271 case ALGORITHM_RIGHT_SYMMETRIC
:
1272 re
->before
.layout
= ALGORITHM_RIGHT_SYMMETRIC_6
;
1274 case ALGORITHM_PARITY_0
:
1275 re
->before
.layout
= ALGORITHM_PARITY_0_6
;
1277 case ALGORITHM_PARITY_N
:
1278 re
->before
.layout
= ALGORITHM_PARITY_N_6
;
1281 return "Cannot convert an array with this layout";
1285 if (info
->array
.raid_disks
!= 2)
1286 return "Can only convert a 2-device array to RAID1";
1287 if (info
->delta_disks
!= UnSet
&&
1288 info
->delta_disks
!= 0)
1289 return "Cannot set raid_disk when converting RAID5->RAID1";
1291 info
->new_chunk
= 0;
1294 return "Impossible level change requested";
1298 switch (info
->new_level
) {
1304 re
->before
.data_disks
= info
->array
.raid_disks
- 2;
1305 re
->before
.layout
= info
->array
.layout
;
1308 return "Impossible level change requested";
1313 /* If we reached here then it looks like a re-stripe is
1314 * happening. We have determined the intermediate level
1315 * and initial raid_disks/layout and stored these in 're'.
1317 * We need to deduce the final layout that can be atomically
1318 * converted to the end state.
1320 switch (info
->new_level
) {
1322 /* We can only get to RAID0 from RAID4 or RAID5
1323 * with appropriate layout and one extra device
1325 if (re
->level
!= 4 && re
->level
!= 5)
1326 return "Cannot covert to RAID0 from this level";
1328 switch (re
->level
) {
1330 re
->before
.layout
= 0;
1331 re
->after
.layout
= 0;
1334 re
->after
.layout
= ALGORITHM_PARITY_N
;
1340 /* We can only get to RAID4 from RAID5 */
1341 if (re
->level
!= 4 && re
->level
!= 5)
1342 return "Cannot convert to RAID4 from this level";
1344 switch (re
->level
) {
1346 re
->after
.layout
= 0;
1349 re
->after
.layout
= ALGORITHM_PARITY_N
;
1355 /* We get to RAID5 from RAID5 or RAID6 */
1356 if (re
->level
!= 5 && re
->level
!= 6)
1357 return "Cannot convert to RAID5 from this level";
1359 switch (re
->level
) {
1361 if (info
->new_layout
== UnSet
)
1362 re
->after
.layout
= re
->before
.layout
;
1364 re
->after
.layout
= info
->new_layout
;
1367 if (info
->new_layout
== UnSet
)
1368 info
->new_layout
= re
->before
.layout
;
1370 /* after.layout needs to be raid6 version of new_layout */
1371 if (info
->new_layout
== ALGORITHM_PARITY_N
)
1372 re
->after
.layout
= ALGORITHM_PARITY_N
;
1375 char *ls
= map_num(r5layout
, info
->new_layout
);
1378 /* Current RAID6 layout has a RAID5
1381 strcat(strcpy(layout
, ls
), "-6");
1382 l
= map_name(r6layout
, layout
);
1384 return "Cannot find RAID6 layout to convert to";
1386 /* Current RAID6 has no equivalent.
1387 * If it is already a '-6' layout we
1388 * can leave it unchanged, else we must
1391 ls
= map_num(r6layout
, info
->new_layout
);
1393 strcmp(ls
+strlen(ls
)-2, "-6") != 0)
1394 return "Please specify new layout";
1395 l
= info
->new_layout
;
1397 re
->after
.layout
= l
;
1403 /* We must already be at level 6 */
1405 return "Impossible level change";
1406 if (info
->new_layout
== UnSet
)
1407 re
->after
.layout
= info
->array
.layout
;
1409 re
->after
.layout
= info
->new_layout
;
1412 return "Impossible level change requested";
1414 if (info
->delta_disks
== UnSet
)
1415 info
->delta_disks
= delta_parity
;
1417 re
->after
.data_disks
= (re
->before
.data_disks
1420 switch (re
->level
) {
1421 case 6: re
->parity
= 2;
1424 case 5: re
->parity
= 1;
1426 default: re
->parity
= 0;
1429 /* So we have a restripe operation, we need to calculate the number
1430 * of blocks per reshape operation.
1432 re
->new_size
= info
->component_size
* re
->before
.data_disks
;
1433 if (info
->new_chunk
== 0)
1434 info
->new_chunk
= info
->array
.chunk_size
;
1435 if (re
->after
.data_disks
== re
->before
.data_disks
&&
1436 re
->after
.layout
== re
->before
.layout
&&
1437 info
->new_chunk
== info
->array
.chunk_size
) {
1438 /* Nothing to change, can change level immediately. */
1439 re
->level
= info
->new_level
;
1440 re
->backup_blocks
= 0;
1443 if (re
->after
.data_disks
== 1 && re
->before
.data_disks
== 1) {
1444 /* chunk and layout changes make no difference */
1445 re
->level
= info
->new_level
;
1446 re
->backup_blocks
= 0;
1450 if (re
->after
.data_disks
== re
->before
.data_disks
&&
1451 get_linux_version() < 2006032)
1452 return "in-place reshape is not safe before 2.6.32 - sorry.";
1454 if (re
->after
.data_disks
< re
->before
.data_disks
&&
1455 get_linux_version() < 2006030)
1456 return "reshape to fewer devices is not supported before 2.6.30 - sorry.";
1458 re
->backup_blocks
= compute_backup_blocks(
1459 info
->new_chunk
, info
->array
.chunk_size
,
1460 re
->after
.data_disks
,
1461 re
->before
.data_disks
);
1462 re
->min_offset_change
= re
->backup_blocks
/ re
->before
.data_disks
;
1464 re
->new_size
= info
->component_size
* re
->after
.data_disks
;
1468 static int set_array_size(struct supertype
*st
, struct mdinfo
*sra
,
1471 struct mdinfo
*info
;
1475 if ((st
== NULL
) || (sra
== NULL
))
1478 if (text_version
== NULL
)
1479 text_version
= sra
->text_version
;
1480 subarray
= strchr(text_version
+1, '/')+1;
1481 info
= st
->ss
->container_content(st
, subarray
);
1483 unsigned long long current_size
= 0;
1484 unsigned long long new_size
=
1485 info
->custom_array_size
/2;
1487 if (sysfs_get_ll(sra
, NULL
, "array_size", ¤t_size
) == 0 &&
1488 new_size
> current_size
) {
1489 if (sysfs_set_num(sra
, NULL
, "array_size", new_size
)
1491 dprintf("Error: Cannot set array size");
1494 dprintf("Array size changed");
1496 dprintf_cont(" from %llu to %llu.\n",
1497 current_size
, new_size
);
1501 dprintf("Error: set_array_size(): info pointer in NULL\n");
1506 static int reshape_array(char *container
, int fd
, char *devname
,
1507 struct supertype
*st
, struct mdinfo
*info
,
1508 int force
, struct mddev_dev
*devlist
,
1509 unsigned long long data_offset
,
1510 char *backup_file
, int verbose
, int forked
,
1511 int restart
, int freeze_reshape
);
1512 static int reshape_container(char *container
, char *devname
,
1514 struct supertype
*st
,
1515 struct mdinfo
*info
,
1517 char *backup_file
, int verbose
,
1518 int forked
, int restart
, int freeze_reshape
);
1520 int Grow_reshape(char *devname
, int fd
,
1521 struct mddev_dev
*devlist
,
1522 unsigned long long data_offset
,
1523 struct context
*c
, struct shape
*s
)
1525 /* Make some changes in the shape of an array.
1526 * The kernel must support the change.
1528 * There are three different changes. Each can trigger
1529 * a resync or recovery so we freeze that until we have
1530 * requested everything (if kernel supports freezing - 2.6.30).
1532 * - change size (i.e. component_size)
1534 * - change layout/chunksize/ndisks
1536 * The last can require a reshape. It is different on different
1537 * levels so we need to check the level before actioning it.
1538 * Some times the level change needs to be requested after the
1539 * reshape (e.g. raid6->raid5, raid5->raid0)
1542 struct mdu_array_info_s array
;
1544 struct supertype
*st
;
1545 char *subarray
= NULL
;
1549 char *container
= NULL
;
1552 struct mddev_dev
*dv
;
1558 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) < 0) {
1559 pr_err("%s is not an active md array - aborting\n",
1563 if (data_offset
!= INVALID_SECTORS
&& array
.level
!= 10
1564 && (array
.level
< 4 || array
.level
> 6)) {
1565 pr_err("--grow --data-offset not yet supported\n");
1570 (s
->chunk
|| s
->level
!= UnSet
|| s
->layout_str
|| s
->raiddisks
)) {
1571 pr_err("cannot change component size at the same time as other changes.\n"
1572 " Change size first, then check data is intact before making other changes.\n");
1576 if (s
->raiddisks
&& s
->raiddisks
< array
.raid_disks
&& array
.level
> 1 &&
1577 get_linux_version() < 2006032 &&
1578 !check_env("MDADM_FORCE_FEWER")) {
1579 pr_err("reducing the number of devices is not safe before Linux 2.6.32\n"
1580 " Please use a newer kernel\n");
1584 st
= super_by_fd(fd
, &subarray
);
1586 pr_err("Unable to determine metadata format for %s\n", devname
);
1589 if (s
->raiddisks
> st
->max_devs
) {
1590 pr_err("Cannot increase raid-disks on this array beyond %d\n", st
->max_devs
);
1594 /* in the external case we need to check that the requested reshape is
1595 * supported, and perform an initial check that the container holds the
1596 * pre-requisite spare devices (mdmon owns final validation)
1598 if (st
->ss
->external
) {
1602 container
= st
->container_devnm
;
1603 cfd
= open_dev_excl(st
->container_devnm
);
1605 container
= st
->devnm
;
1607 cfd
= open_dev_excl(st
->devnm
);
1611 pr_err("Unable to open container for %s\n",
1617 rv
= st
->ss
->load_container(st
, cfd
, NULL
);
1620 pr_err("Cannot read superblock for %s\n",
1626 /* check if operation is supported for metadata handler */
1627 if (st
->ss
->container_content
) {
1628 struct mdinfo
*cc
= NULL
;
1629 struct mdinfo
*content
= NULL
;
1631 cc
= st
->ss
->container_content(st
, subarray
);
1632 for (content
= cc
; content
; content
= content
->next
) {
1633 int allow_reshape
= 1;
1635 /* check if reshape is allowed based on metadata
1636 * indications stored in content.array.status
1638 if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
))
1640 if (content
->array
.state
1641 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
))
1643 if (!allow_reshape
) {
1644 pr_err("cannot reshape arrays in container with unsupported metadata: %s(%s)\n",
1645 devname
, container
);
1653 if (mdmon_running(container
))
1654 st
->update_tail
= &st
->updates
;
1658 for (dv
= devlist
; dv
; dv
= dv
->next
)
1660 if (s
->raiddisks
> array
.raid_disks
&&
1661 array
.spare_disks
+added_disks
< (s
->raiddisks
- array
.raid_disks
) &&
1663 pr_err("Need %d spare%s to avoid degraded array, and only have %d.\n"
1664 " Use --force to over-ride this check.\n",
1665 s
->raiddisks
- array
.raid_disks
,
1666 s
->raiddisks
- array
.raid_disks
== 1 ? "" : "s",
1667 array
.spare_disks
+ added_disks
);
1671 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
| GET_DISKS
| GET_DEVS
1672 | GET_STATE
| GET_VERSION
);
1674 if (st
->ss
->external
&& subarray
== NULL
) {
1675 array
.level
= LEVEL_CONTAINER
;
1676 sra
->array
.level
= LEVEL_CONTAINER
;
1679 pr_err("failed to read sysfs parameters for %s\n",
1683 frozen
= freeze(st
);
1685 /* freeze() already spewed the reason */
1688 } else if (frozen
< 0) {
1689 pr_err("%s is performing resync/recovery and cannot be reshaped\n", devname
);
1694 /* ========= set size =============== */
1695 if (s
->size
> 0 && (s
->size
== MAX_SIZE
|| s
->size
!= (unsigned)array
.size
)) {
1696 unsigned long long orig_size
= get_component_size(fd
)/2;
1697 unsigned long long min_csize
;
1699 int raid0_takeover
= 0;
1702 orig_size
= (unsigned) array
.size
;
1704 if (orig_size
== 0) {
1705 pr_err("Cannot set device size in this type of array.\n");
1710 if (reshape_super(st
, s
->size
, UnSet
, UnSet
, 0, 0, UnSet
, NULL
,
1711 devname
, APPLY_METADATA_CHANGES
, c
->verbose
> 0)) {
1716 if (st
->ss
->external
) {
1717 /* metadata can have size limitation
1718 * update size value according to metadata information
1720 struct mdinfo
*sizeinfo
=
1721 st
->ss
->container_content(st
, subarray
);
1723 unsigned long long new_size
=
1724 sizeinfo
->custom_array_size
/2;
1725 int data_disks
= get_data_disks(
1726 sizeinfo
->array
.level
,
1727 sizeinfo
->array
.layout
,
1728 sizeinfo
->array
.raid_disks
);
1729 new_size
/= data_disks
;
1730 dprintf("Metadata size correction from %llu to %llu (%llu)\n", orig_size
, new_size
,
1731 new_size
* data_disks
);
1733 sysfs_free(sizeinfo
);
1737 /* Update the size of each member device in case
1738 * they have been resized. This will never reduce
1739 * below the current used-size. The "size" attribute
1740 * understands '0' to mean 'max'.
1744 for (mdi
= sra
->devs
; mdi
; mdi
= mdi
->next
) {
1745 if (sysfs_set_num(sra
, mdi
, "size",
1746 s
->size
== MAX_SIZE
? 0 : s
->size
) < 0) {
1747 /* Probably kernel refusing to let us
1748 * reduce the size - not an error.
1752 if (array
.not_persistent
== 0 &&
1753 array
.major_version
== 0 &&
1754 get_linux_version() < 3001000) {
1755 /* Dangerous to allow size to exceed 2TB */
1756 unsigned long long csize
;
1757 if (sysfs_get_ll(sra
, mdi
, "size", &csize
) == 0) {
1758 if (csize
>= 2ULL*1024*1024*1024)
1759 csize
= 2ULL*1024*1024*1024;
1760 if ((min_csize
== 0 || (min_csize
1767 pr_err("Cannot set size on array members.\n");
1768 goto size_change_error
;
1770 if (min_csize
&& s
->size
> min_csize
) {
1771 pr_err("Cannot safely make this array use more than 2TB per device on this kernel.\n");
1773 goto size_change_error
;
1775 if (min_csize
&& s
->size
== MAX_SIZE
) {
1776 /* Don't let the kernel choose a size - it will get
1779 pr_err("Limited v0.90 array to 2TB per device\n");
1780 s
->size
= min_csize
;
1782 if (st
->ss
->external
) {
1783 if (sra
->array
.level
== 0) {
1784 rv
= sysfs_set_str(sra
, NULL
, "level",
1788 /* get array parameters after takeover
1789 * to change one parameter at time only
1791 rv
= ioctl(fd
, GET_ARRAY_INFO
, &array
);
1794 /* make sure mdmon is
1795 * aware of the new level */
1796 if (!mdmon_running(st
->container_devnm
))
1797 start_mdmon(st
->container_devnm
);
1798 ping_monitor(container
);
1799 if (mdmon_running(st
->container_devnm
) &&
1800 st
->update_tail
== NULL
)
1801 st
->update_tail
= &st
->updates
;
1804 if (s
->size
== MAX_SIZE
)
1806 array
.size
= s
->size
;
1807 if (s
->size
& ~INT32_MAX
) {
1808 /* got truncated to 32bit, write to
1809 * component_size instead
1812 rv
= sysfs_set_num(sra
, NULL
,
1813 "component_size", s
->size
);
1817 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
1819 /* manage array size when it is managed externally
1821 if ((rv
== 0) && st
->ss
->external
)
1822 rv
= set_array_size(st
, sra
, sra
->text_version
);
1825 if (raid0_takeover
) {
1826 /* do not recync non-existing parity,
1827 * we will drop it anyway
1829 sysfs_set_str(sra
, NULL
, "sync_action", "frozen");
1830 /* go back to raid0, drop parity disk
1832 sysfs_set_str(sra
, NULL
, "level", "raid0");
1833 ioctl(fd
, GET_ARRAY_INFO
, &array
);
1840 /* restore metadata */
1841 if (reshape_super(st
, orig_size
, UnSet
, UnSet
, 0, 0,
1842 UnSet
, NULL
, devname
,
1843 ROLLBACK_METADATA_CHANGES
,
1846 pr_err("Cannot set device size for %s: %s\n",
1847 devname
, strerror(err
));
1849 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1850 cont_err("Bitmap must be removed before size can be changed\n");
1854 if (s
->assume_clean
) {
1855 /* This will fail on kernels older than 3.0 unless
1856 * a backport has been arranged.
1859 sysfs_set_str(sra
, NULL
, "resync_start", "none") < 0)
1860 pr_err("--assume-clean not supported with --grow on this kernel\n");
1862 ioctl(fd
, GET_ARRAY_INFO
, &array
);
1863 s
->size
= get_component_size(fd
)/2;
1865 s
->size
= array
.size
;
1866 if (c
->verbose
>= 0) {
1867 if (s
->size
== orig_size
)
1868 pr_err("component size of %s unchanged at %lluK\n",
1871 pr_err("component size of %s has been set to %lluK\n",
1875 } else if (array
.level
!= LEVEL_CONTAINER
) {
1876 s
->size
= get_component_size(fd
)/2;
1878 s
->size
= array
.size
;
1881 /* See if there is anything else to do */
1882 if ((s
->level
== UnSet
|| s
->level
== array
.level
) &&
1883 (s
->layout_str
== NULL
) &&
1884 (s
->chunk
== 0 || s
->chunk
== array
.chunk_size
) &&
1885 data_offset
== INVALID_SECTORS
&&
1886 (s
->raiddisks
== 0 || s
->raiddisks
== array
.raid_disks
)) {
1887 /* Nothing more to do */
1888 if (!changed
&& c
->verbose
>= 0)
1889 pr_err("%s: no change requested\n",
1894 /* ========= check for Raid10/Raid1 -> Raid0 conversion ===============
1895 * current implementation assumes that following conditions must be met:
1898 * - near_copies == 2
1900 if ((s
->level
== 0 && array
.level
== 10 && sra
&&
1901 array
.layout
== ((1 << 8) + 2) && !(array
.raid_disks
& 1)) ||
1902 (s
->level
== 0 && array
.level
== 1 && sra
)) {
1904 err
= remove_disks_for_takeover(st
, sra
, array
.layout
);
1906 dprintf("Array cannot be reshaped\n");
1912 /* Make sure mdmon has seen the device removal
1913 * and updated metadata before we continue with
1917 ping_monitor(container
);
1920 memset(&info
, 0, sizeof(info
));
1922 sysfs_init(&info
, fd
, NULL
);
1923 strcpy(info
.text_version
, sra
->text_version
);
1924 info
.component_size
= s
->size
*2;
1925 info
.new_level
= s
->level
;
1926 info
.new_chunk
= s
->chunk
* 1024;
1927 if (info
.array
.level
== LEVEL_CONTAINER
) {
1928 info
.delta_disks
= UnSet
;
1929 info
.array
.raid_disks
= s
->raiddisks
;
1930 } else if (s
->raiddisks
)
1931 info
.delta_disks
= s
->raiddisks
- info
.array
.raid_disks
;
1933 info
.delta_disks
= UnSet
;
1934 if (s
->layout_str
== NULL
) {
1935 info
.new_layout
= UnSet
;
1936 if (info
.array
.level
== 6 &&
1937 (info
.new_level
== 6 || info
.new_level
== UnSet
) &&
1938 info
.array
.layout
>= 16) {
1939 pr_err("%s has a non-standard layout. If you wish to preserve this\n", devname
);
1940 cont_err("during the reshape, please specify --layout=preserve\n");
1941 cont_err("If you want to change it, specify a layout or use --layout=normalise\n");
1945 } else if (strcmp(s
->layout_str
, "normalise") == 0 ||
1946 strcmp(s
->layout_str
, "normalize") == 0) {
1947 /* If we have a -6 RAID6 layout, remove the '-6'. */
1948 info
.new_layout
= UnSet
;
1949 if (info
.array
.level
== 6 && info
.new_level
== UnSet
) {
1951 strcpy(l
, map_num(r6layout
, info
.array
.layout
));
1952 h
= strrchr(l
, '-');
1953 if (h
&& strcmp(h
, "-6") == 0) {
1955 info
.new_layout
= map_name(r6layout
, l
);
1958 pr_err("%s is only meaningful when reshaping a RAID6 array.\n", s
->layout_str
);
1962 } else if (strcmp(s
->layout_str
, "preserve") == 0) {
1963 /* This means that a non-standard RAID6 layout
1966 * - When reshape a RAID6 (e.g. adding a device)
1967 * which is in a non-standard layout, it is OK
1968 * to preserve that layout.
1969 * - When converting a RAID5 to RAID6, leave it in
1970 * the XXX-6 layout, don't re-layout.
1972 if (info
.array
.level
== 6 && info
.new_level
== UnSet
)
1973 info
.new_layout
= info
.array
.layout
;
1974 else if (info
.array
.level
== 5 && info
.new_level
== 6) {
1976 strcpy(l
, map_num(r5layout
, info
.array
.layout
));
1978 info
.new_layout
= map_name(r6layout
, l
);
1980 pr_err("%s in only meaningful when reshaping to RAID6\n", s
->layout_str
);
1985 int l
= info
.new_level
;
1987 l
= info
.array
.level
;
1990 info
.new_layout
= map_name(r5layout
, s
->layout_str
);
1993 info
.new_layout
= map_name(r6layout
, s
->layout_str
);
1996 info
.new_layout
= parse_layout_10(s
->layout_str
);
1999 info
.new_layout
= parse_layout_faulty(s
->layout_str
);
2002 pr_err("layout not meaningful with this level\n");
2006 if (info
.new_layout
== UnSet
) {
2007 pr_err("layout %s not understood for this level\n",
2014 if (array
.level
== LEVEL_FAULTY
) {
2015 if (s
->level
!= UnSet
&& s
->level
!= array
.level
) {
2016 pr_err("cannot change level of Faulty device\n");
2020 pr_err("cannot set chunksize of Faulty device\n");
2023 if (s
->raiddisks
&& s
->raiddisks
!= 1) {
2024 pr_err("cannot set raid_disks of Faulty device\n");
2027 if (s
->layout_str
) {
2028 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
2029 dprintf("Cannot get array information.\n");
2032 array
.layout
= info
.new_layout
;
2033 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2034 pr_err("failed to set new layout\n");
2036 } else if (c
->verbose
>= 0)
2037 printf("layout for %s set to %d\n",
2038 devname
, array
.layout
);
2040 } else if (array
.level
== LEVEL_CONTAINER
) {
2041 /* This change is to be applied to every array in the
2042 * container. This is only needed when the metadata imposes
2043 * restraints of the various arrays in the container.
2044 * Currently we only know that IMSM requires all arrays
2045 * to have the same number of devices so changing the
2046 * number of devices (On-Line Capacity Expansion) must be
2047 * performed at the level of the container
2049 rv
= reshape_container(container
, devname
, -1, st
, &info
,
2050 c
->force
, c
->backup_file
, c
->verbose
, 0, 0, 0);
2053 /* get spare devices from external metadata
2055 if (st
->ss
->external
) {
2056 struct mdinfo
*info2
;
2058 info2
= st
->ss
->container_content(st
, subarray
);
2060 info
.array
.spare_disks
=
2061 info2
->array
.spare_disks
;
2066 /* Impose these changes on a single array. First
2067 * check that the metadata is OK with the change. */
2069 if (reshape_super(st
, 0, info
.new_level
,
2070 info
.new_layout
, info
.new_chunk
,
2071 info
.array
.raid_disks
, info
.delta_disks
,
2072 c
->backup_file
, devname
, APPLY_METADATA_CHANGES
,
2078 rv
= reshape_array(container
, fd
, devname
, st
, &info
, c
->force
,
2079 devlist
, data_offset
, c
->backup_file
, c
->verbose
,
2090 /* verify_reshape_position()
2091 * Function checks if reshape position in metadata is not farther
2092 * than position in md.
2094 * 0 : not valid sysfs entry
2095 * it can be caused by not started reshape, it should be started
2096 * by reshape array or raid0 array is before takeover
2097 * -1 : error, reshape position is obviously wrong
2098 * 1 : success, reshape progress correct or updated
2100 static int verify_reshape_position(struct mdinfo
*info
, int level
)
2106 /* read sync_max, failure can mean raid0 array */
2107 rv
= sysfs_get_str(info
, NULL
, "sync_max", buf
, 40);
2111 unsigned long long position
= strtoull(buf
, &ep
, 0);
2113 dprintf("Read sync_max sysfs entry is: %s\n", buf
);
2114 if (!(ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))) {
2115 position
*= get_data_disks(level
,
2117 info
->array
.raid_disks
);
2118 if (info
->reshape_progress
< position
) {
2119 dprintf("Corrected reshape progress (%llu) to md position (%llu)\n",
2120 info
->reshape_progress
, position
);
2121 info
->reshape_progress
= position
;
2123 } else if (info
->reshape_progress
> position
) {
2124 pr_err("Fatal error: array reshape was not properly frozen (expected reshape position is %llu, but reshape progress is %llu.\n",
2125 position
, info
->reshape_progress
);
2128 dprintf("Reshape position in md and metadata are the same;");
2132 } else if (rv
== 0) {
2133 /* for valid sysfs entry, 0-length content
2134 * should be indicated as error
2142 static unsigned long long choose_offset(unsigned long long lo
,
2143 unsigned long long hi
,
2144 unsigned long long min
,
2145 unsigned long long max
)
2147 /* Choose a new offset between hi and lo.
2148 * It must be between min and max, but
2149 * we would prefer something near the middle of hi/lo, and also
2150 * prefer to be aligned to a big power of 2.
2152 * So we start with the middle, then for each bit,
2153 * starting at '1' and increasing, if it is set, we either
2154 * add it or subtract it if possible, preferring the option
2155 * which is furthest from the boundary.
2157 * We stop once we get a 1MB alignment. As units are in sectors,
2158 * 1MB = 2*1024 sectors.
2160 unsigned long long choice
= (lo
+ hi
) / 2;
2161 unsigned long long bit
= 1;
2163 for (bit
= 1; bit
< 2*1024; bit
= bit
<< 1) {
2164 unsigned long long bigger
, smaller
;
2165 if (! (bit
& choice
))
2167 bigger
= choice
+ bit
;
2168 smaller
= choice
- bit
;
2169 if (bigger
> max
&& smaller
< min
)
2173 else if (smaller
< min
)
2175 else if (hi
- bigger
> smaller
- lo
)
2183 static int set_new_data_offset(struct mdinfo
*sra
, struct supertype
*st
,
2184 char *devname
, int delta_disks
,
2185 unsigned long long data_offset
,
2186 unsigned long long min
,
2192 unsigned long long before
, after
;
2194 /* Need to find min space before and after so same is used
2197 before
= UINT64_MAX
;
2199 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2203 struct supertype
*st2
;
2204 struct mdinfo info2
;
2206 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2208 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2209 dfd
= dev_open(dn
, O_RDONLY
);
2211 pr_err("%s: cannot open component %s\n",
2212 devname
, dn
? dn
: "-unknown-");
2215 st2
= dup_super(st
);
2216 rv
= st2
->ss
->load_super(st2
,dfd
, NULL
);
2220 pr_err("%s: cannot get superblock from %s\n",
2224 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
2225 st2
->ss
->free_super(st2
);
2227 if (info2
.space_before
== 0 &&
2228 info2
.space_after
== 0) {
2229 /* Metadata doesn't support data_offset changes */
2231 pr_err("%s: Metadata version doesn't support data_offset changes\n",
2235 if (before
> info2
.space_before
)
2236 before
= info2
.space_before
;
2237 if (after
> info2
.space_after
)
2238 after
= info2
.space_after
;
2240 if (data_offset
!= INVALID_SECTORS
) {
2242 if (info2
.data_offset
== data_offset
) {
2243 pr_err("%s: already has that data_offset\n",
2247 if (data_offset
< info2
.data_offset
)
2251 } else if ((data_offset
<= info2
.data_offset
&& dir
== 1) ||
2252 (data_offset
>= info2
.data_offset
&& dir
== -1)) {
2253 pr_err("%s: differing data offsets on devices make this --data-offset setting impossible\n",
2259 if (before
== UINT64_MAX
)
2260 /* impossible really, there must be no devices */
2263 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2264 char *dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2265 unsigned long long new_data_offset
;
2267 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2269 if (delta_disks
< 0) {
2270 /* Don't need any space as array is shrinking
2271 * just move data_offset up by min
2273 if (data_offset
== INVALID_SECTORS
)
2274 new_data_offset
= sd
->data_offset
+ min
;
2276 if (data_offset
< sd
->data_offset
+ min
) {
2277 pr_err("--data-offset too small for %s\n",
2281 new_data_offset
= data_offset
;
2283 } else if (delta_disks
> 0) {
2284 /* need space before */
2288 pr_err("Insufficient head-space for reshape on %s\n",
2292 if (data_offset
== INVALID_SECTORS
)
2293 new_data_offset
= sd
->data_offset
- min
;
2295 if (data_offset
> sd
->data_offset
- min
) {
2296 pr_err("--data-offset too large for %s\n",
2300 new_data_offset
= data_offset
;
2304 /* can move up or down. If 'data_offset'
2305 * was set we would have already decided,
2306 * so just choose direction with most space.
2313 sysfs_set_str(sra
, NULL
, "reshape_direction",
2314 dir
== 1 ? "backwards" : "forwards");
2316 /* Increase data offset */
2320 pr_err("Insufficient tail-space for reshape on %s\n",
2324 if (data_offset
!= INVALID_SECTORS
&&
2325 data_offset
< sd
->data_offset
+ min
) {
2326 pr_err("--data-offset too small on %s\n",
2330 if (data_offset
!= INVALID_SECTORS
)
2331 new_data_offset
= data_offset
;
2333 new_data_offset
= choose_offset(sd
->data_offset
,
2334 sd
->data_offset
+ after
,
2335 sd
->data_offset
+ min
,
2336 sd
->data_offset
+ after
);
2338 /* Decrease data offset */
2342 pr_err("insufficient head-room on %s\n",
2346 if (data_offset
!= INVALID_SECTORS
&&
2347 data_offset
< sd
->data_offset
- min
) {
2348 pr_err("--data-offset too small on %s\n",
2352 if (data_offset
!= INVALID_SECTORS
)
2353 new_data_offset
= data_offset
;
2355 new_data_offset
= choose_offset(sd
->data_offset
- before
,
2357 sd
->data_offset
- before
,
2358 sd
->data_offset
- min
);
2361 err
= sysfs_set_num(sra
, sd
, "new_offset", new_data_offset
);
2362 if (err
< 0 && errno
== E2BIG
) {
2363 /* try again after increasing data size to max */
2364 err
= sysfs_set_num(sra
, sd
, "size", 0);
2365 if (err
< 0 && errno
== EINVAL
&&
2366 !(sd
->disk
.state
& (1<<MD_DISK_SYNC
))) {
2367 /* some kernels have a bug where you cannot
2368 * use '0' on spare devices. */
2369 sysfs_set_num(sra
, sd
, "size",
2370 (sra
->component_size
+ after
)/2);
2372 err
= sysfs_set_num(sra
, sd
, "new_offset",
2376 if (errno
== E2BIG
&& data_offset
!= INVALID_SECTORS
) {
2377 pr_err("data-offset is too big for %s\n",
2381 if (sd
== sra
->devs
&&
2382 (errno
== ENOENT
|| errno
== E2BIG
))
2383 /* Early kernel, no 'new_offset' file,
2384 * or kernel doesn't like us.
2385 * For RAID5/6 this is not fatal
2388 pr_err("Cannot set new_offset for %s\n",
2397 /* Just use a backup file */
2401 static int raid10_reshape(char *container
, int fd
, char *devname
,
2402 struct supertype
*st
, struct mdinfo
*info
,
2403 struct reshape
*reshape
,
2404 unsigned long long data_offset
,
2405 int force
, int verbose
)
2407 /* Changing raid_disks, layout, chunksize or possibly
2408 * just data_offset for a RAID10.
2409 * We must always change data_offset. We change by at least
2410 * ->min_offset_change which is the largest of the old and new
2412 * If raid_disks is increasing, then data_offset must decrease
2413 * by at least this copy size.
2414 * If raid_disks is unchanged, data_offset must increase or
2415 * decrease by at least min_offset_change but preferably by much more.
2416 * We choose half of the available space.
2417 * If raid_disks is decreasing, data_offset must increase by
2418 * at least min_offset_change. To allow of this, component_size
2419 * must be decreased by the same amount.
2421 * So we calculate the required minimum and direction, possibly
2422 * reduce the component_size, then iterate through the devices
2423 * and set the new_data_offset.
2424 * If that all works, we set chunk_size, layout, raid_disks, and start
2428 unsigned long long min
;
2431 sra
= sysfs_read(fd
, NULL
,
2432 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
2435 pr_err("%s: Cannot get array details from sysfs\n",
2439 min
= reshape
->min_offset_change
;
2441 if (info
->delta_disks
)
2442 sysfs_set_str(sra
, NULL
, "reshape_direction",
2443 info
->delta_disks
< 0 ? "backwards" : "forwards");
2444 if (info
->delta_disks
< 0 &&
2445 info
->space_after
< min
) {
2446 int rv
= sysfs_set_num(sra
, NULL
, "component_size",
2447 (sra
->component_size
-
2450 pr_err("cannot reduce component size\n");
2454 err
= set_new_data_offset(sra
, st
, devname
, info
->delta_disks
, data_offset
,
2457 pr_err("Cannot set new_data_offset: RAID10 reshape not\n");
2458 cont_err("supported on this kernel\n");
2464 if (!err
&& sysfs_set_num(sra
, NULL
, "chunk_size", info
->new_chunk
) < 0)
2466 if (!err
&& sysfs_set_num(sra
, NULL
, "layout", reshape
->after
.layout
) < 0)
2468 if (!err
&& sysfs_set_num(sra
, NULL
, "raid_disks",
2469 info
->array
.raid_disks
+ info
->delta_disks
) < 0)
2471 if (!err
&& sysfs_set_str(sra
, NULL
, "sync_action", "reshape") < 0)
2474 pr_err("Cannot set array shape for %s\n",
2477 (info
->array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2478 cont_err(" Bitmap must be removed before shape can be changed\n");
2488 static void get_space_after(int fd
, struct supertype
*st
, struct mdinfo
*info
)
2490 struct mdinfo
*sra
, *sd
;
2491 /* Initialisation to silence compiler warning */
2492 unsigned long long min_space_before
= 0, min_space_after
= 0;
2495 sra
= sysfs_read(fd
, NULL
, GET_DEVS
);
2498 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2501 struct supertype
*st2
;
2502 struct mdinfo info2
;
2504 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2506 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2507 dfd
= dev_open(dn
, O_RDONLY
);
2510 st2
= dup_super(st
);
2511 if (st2
->ss
->load_super(st2
,dfd
, NULL
)) {
2517 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
2518 st2
->ss
->free_super(st2
);
2521 min_space_before
> info2
.space_before
)
2522 min_space_before
= info2
.space_before
;
2524 min_space_after
> info2
.space_after
)
2525 min_space_after
= info2
.space_after
;
2528 if (sd
== NULL
&& !first
) {
2529 info
->space_after
= min_space_after
;
2530 info
->space_before
= min_space_before
;
2535 static void update_cache_size(char *container
, struct mdinfo
*sra
,
2536 struct mdinfo
*info
,
2537 int disks
, unsigned long long blocks
)
2539 /* Check that the internal stripe cache is
2540 * large enough, or it won't work.
2541 * It must hold at least 4 stripes of the larger
2544 unsigned long cache
;
2545 cache
= max(info
->array
.chunk_size
, info
->new_chunk
);
2546 cache
*= 4; /* 4 stripes minimum */
2547 cache
/= 512; /* convert to sectors */
2548 /* make sure there is room for 'blocks' with a bit to spare */
2549 if (cache
< 16 + blocks
/ disks
)
2550 cache
= 16 + blocks
/ disks
;
2551 cache
/= (4096/512); /* Convert from sectors to pages */
2553 if (sra
->cache_size
< cache
)
2554 subarray_set_num(container
, sra
, "stripe_cache_size",
2558 static int impose_reshape(struct mdinfo
*sra
,
2559 struct mdinfo
*info
,
2560 struct supertype
*st
,
2563 char *devname
, char *container
,
2564 struct reshape
*reshape
)
2566 struct mdu_array_info_s array
;
2568 sra
->new_chunk
= info
->new_chunk
;
2571 /* for external metadata checkpoint saved by mdmon can be lost
2572 * or missed /due to e.g. crash/. Check if md is not during
2573 * restart farther than metadata points to.
2574 * If so, this means metadata information is obsolete.
2576 if (st
->ss
->external
)
2577 verify_reshape_position(info
, reshape
->level
);
2578 sra
->reshape_progress
= info
->reshape_progress
;
2580 sra
->reshape_progress
= 0;
2581 if (reshape
->after
.data_disks
< reshape
->before
.data_disks
)
2582 /* start from the end of the new array */
2583 sra
->reshape_progress
= (sra
->component_size
2584 * reshape
->after
.data_disks
);
2587 ioctl(fd
, GET_ARRAY_INFO
, &array
);
2588 if (info
->array
.chunk_size
== info
->new_chunk
&&
2589 reshape
->before
.layout
== reshape
->after
.layout
&&
2590 st
->ss
->external
== 0) {
2591 /* use SET_ARRAY_INFO but only if reshape hasn't started */
2592 array
.raid_disks
= reshape
->after
.data_disks
+ reshape
->parity
;
2594 ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2597 pr_err("Cannot set device shape for %s: %s\n",
2598 devname
, strerror(errno
));
2601 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2602 cont_err("Bitmap must be removed before shape can be changed\n");
2606 } else if (!restart
) {
2607 /* set them all just in case some old 'new_*' value
2608 * persists from some earlier problem.
2611 if (sysfs_set_num(sra
, NULL
, "chunk_size", info
->new_chunk
) < 0)
2613 if (!err
&& sysfs_set_num(sra
, NULL
, "layout",
2614 reshape
->after
.layout
) < 0)
2616 if (!err
&& subarray_set_num(container
, sra
, "raid_disks",
2617 reshape
->after
.data_disks
+
2618 reshape
->parity
) < 0)
2621 pr_err("Cannot set device shape for %s\n",
2625 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2626 cont_err("Bitmap must be removed before shape can be changed\n");
2635 static int impose_level(int fd
, int level
, char *devname
, int verbose
)
2638 struct mdu_array_info_s array
;
2640 sysfs_init(&info
, fd
, NULL
);
2642 ioctl(fd
, GET_ARRAY_INFO
, &array
);
2644 (array
.level
>= 4 && array
.level
<= 6)) {
2645 /* To convert to RAID0 we need to fail and
2646 * remove any non-data devices. */
2649 int data_disks
= array
.raid_disks
- 1;
2650 if (array
.level
== 6)
2652 if (array
.level
== 5 &&
2653 array
.layout
!= ALGORITHM_PARITY_N
)
2655 if (array
.level
== 6 &&
2656 array
.layout
!= ALGORITHM_PARITY_N_6
)
2658 sysfs_set_str(&info
, NULL
,"sync_action", "idle");
2659 /* First remove any spares so no recovery starts */
2660 for (d
= 0, found
= 0;
2661 d
< MAX_DISKS
&& found
< array
.nr_disks
;
2663 mdu_disk_info_t disk
;
2665 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
2667 if (disk
.major
== 0 && disk
.minor
== 0)
2670 if ((disk
.state
& (1 << MD_DISK_ACTIVE
))
2671 && disk
.raid_disk
< data_disks
)
2674 ioctl(fd
, HOT_REMOVE_DISK
,
2675 makedev(disk
.major
, disk
.minor
));
2677 /* Now fail anything left */
2678 ioctl(fd
, GET_ARRAY_INFO
, &array
);
2679 for (d
= 0, found
= 0;
2680 d
< MAX_DISKS
&& found
< array
.nr_disks
;
2683 mdu_disk_info_t disk
;
2685 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
2687 if (disk
.major
== 0 && disk
.minor
== 0)
2690 if ((disk
.state
& (1 << MD_DISK_ACTIVE
))
2691 && disk
.raid_disk
< data_disks
)
2694 ioctl(fd
, SET_DISK_FAULTY
,
2695 makedev(disk
.major
, disk
.minor
));
2697 while (ioctl(fd
, HOT_REMOVE_DISK
,
2698 makedev(disk
.major
, disk
.minor
)) < 0
2705 c
= map_num(pers
, level
);
2707 int err
= sysfs_set_str(&info
, NULL
, "level", c
);
2710 pr_err("%s: could not set level to %s\n",
2713 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2714 cont_err("Bitmap must be removed before level can be changed\n");
2718 pr_err("level of %s changed to %s\n",
2725 static void catch_term(int sig
)
2730 static int continue_via_systemd(char *devnm
)
2732 int skipped
, i
, pid
, status
;
2734 /* In a systemd/udev world, it is best to get systemd to
2735 * run "mdadm --grow --continue" rather than running in the
2740 /* FIXME yuk. CLOSE_EXEC?? */
2742 for (i
= 3; skipped
< 20; i
++)
2748 /* Don't want to see error messages from
2749 * systemctl. If the service doesn't exist,
2750 * we fork ourselves.
2753 open("/dev/null", O_WRONLY
);
2754 snprintf(pathbuf
, sizeof(pathbuf
), "mdadm-grow-continue@%s.service",
2756 status
= execl("/usr/bin/systemctl", "systemctl",
2759 status
= execl("/bin/systemctl", "systemctl", "start",
2762 case -1: /* Just do it ourselves. */
2764 default: /* parent - good */
2765 pid
= wait(&status
);
2766 if (pid
>= 0 && status
== 0)
2772 static int reshape_array(char *container
, int fd
, char *devname
,
2773 struct supertype
*st
, struct mdinfo
*info
,
2774 int force
, struct mddev_dev
*devlist
,
2775 unsigned long long data_offset
,
2776 char *backup_file
, int verbose
, int forked
,
2777 int restart
, int freeze_reshape
)
2779 struct reshape reshape
;
2782 int orig_level
= UnSet
;
2786 struct mdu_array_info_s array
;
2789 struct mddev_dev
*dv
;
2793 unsigned long long *offsets
= NULL
;
2797 unsigned long blocks
;
2798 unsigned long long array_size
;
2800 struct mdinfo
*sra
= NULL
;
2803 /* when reshaping a RAID0, the component_size might be zero.
2804 * So try to fix that up.
2806 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
2807 dprintf("Cannot get array information.\n");
2810 if (array
.level
== 0 && info
->component_size
== 0) {
2811 get_dev_size(fd
, NULL
, &array_size
);
2812 info
->component_size
= array_size
/ array
.raid_disks
;
2815 if (array
.level
== 10)
2816 /* Need space_after info */
2817 get_space_after(fd
, st
, info
);
2819 if (info
->reshape_active
) {
2820 int new_level
= info
->new_level
;
2821 info
->new_level
= UnSet
;
2822 if (info
->delta_disks
> 0)
2823 info
->array
.raid_disks
-= info
->delta_disks
;
2824 msg
= analyse_change(devname
, info
, &reshape
);
2825 info
->new_level
= new_level
;
2826 if (info
->delta_disks
> 0)
2827 info
->array
.raid_disks
+= info
->delta_disks
;
2829 /* Make sure the array isn't read-only */
2830 ioctl(fd
, RESTART_ARRAY_RW
, 0);
2832 msg
= analyse_change(devname
, info
, &reshape
);
2834 /* if msg == "", error has already been printed */
2836 pr_err("%s\n", msg
);
2840 (reshape
.level
!= info
->array
.level
||
2841 reshape
.before
.layout
!= info
->array
.layout
||
2842 reshape
.before
.data_disks
+ reshape
.parity
2843 != info
->array
.raid_disks
- max(0, info
->delta_disks
))) {
2844 pr_err("reshape info is not in native format - cannot continue.\n");
2848 if (st
->ss
->external
&& restart
&& (info
->reshape_progress
== 0) &&
2849 !((sysfs_get_str(info
, NULL
, "sync_action", buf
, sizeof(buf
)) > 0) &&
2850 (strncmp(buf
, "reshape", 7) == 0))) {
2851 /* When reshape is restarted from '0', very begin of array
2852 * it is possible that for external metadata reshape and array
2853 * configuration doesn't happen.
2854 * Check if md has the same opinion, and reshape is restarted
2855 * from 0. If so, this is regular reshape start after reshape
2856 * switch in metadata to next array only.
2858 if ((verify_reshape_position(info
, reshape
.level
) >= 0) &&
2859 (info
->reshape_progress
== 0))
2863 /* reshape already started. just skip to monitoring the reshape */
2864 if (reshape
.backup_blocks
== 0)
2866 if (restart
& RESHAPE_NO_BACKUP
)
2869 /* Need 'sra' down at 'started:' */
2870 sra
= sysfs_read(fd
, NULL
,
2871 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
|
2874 pr_err("%s: Cannot get array details from sysfs\n",
2880 backup_file
= locate_backup(sra
->sys_name
);
2884 /* The container is frozen but the array may not be.
2885 * So freeze the array so spares don't get put to the wrong use
2886 * FIXME there should probably be a cleaner separation between
2887 * freeze_array and freeze_container.
2889 sysfs_freeze_array(info
);
2890 /* Check we have enough spares to not be degraded */
2892 for (dv
= devlist
; dv
; dv
=dv
->next
)
2894 spares_needed
= max(reshape
.before
.data_disks
,
2895 reshape
.after
.data_disks
)
2896 + reshape
.parity
- array
.raid_disks
;
2899 info
->new_level
> 1 && info
->array
.level
> 1 &&
2900 spares_needed
> info
->array
.spare_disks
+ added_disks
) {
2901 pr_err("Need %d spare%s to avoid degraded array, and only have %d.\n"
2902 " Use --force to over-ride this check.\n",
2904 spares_needed
== 1 ? "" : "s",
2905 info
->array
.spare_disks
+ added_disks
);
2908 /* Check we have enough spares to not fail */
2909 spares_needed
= max(reshape
.before
.data_disks
,
2910 reshape
.after
.data_disks
)
2912 if ((info
->new_level
> 1 || info
->new_level
== 0) &&
2913 spares_needed
> info
->array
.spare_disks
+added_disks
) {
2914 pr_err("Need %d spare%s to create working array, and only have %d.\n",
2916 spares_needed
== 1 ? "" : "s",
2917 info
->array
.spare_disks
+ added_disks
);
2921 if (reshape
.level
!= array
.level
) {
2922 int err
= impose_level(fd
, reshape
.level
, devname
, verbose
);
2925 info
->new_layout
= UnSet
; /* after level change,
2926 * layout is meaningless */
2927 orig_level
= array
.level
;
2928 sysfs_freeze_array(info
);
2930 if (reshape
.level
> 0 && st
->ss
->external
) {
2931 /* make sure mdmon is aware of the new level */
2932 if (mdmon_running(container
))
2933 flush_mdmon(container
);
2935 if (!mdmon_running(container
))
2936 start_mdmon(container
);
2937 ping_monitor(container
);
2938 if (mdmon_running(container
) &&
2939 st
->update_tail
== NULL
)
2940 st
->update_tail
= &st
->updates
;
2943 /* ->reshape_super might have chosen some spares from the
2944 * container that it wants to be part of the new array.
2945 * We can collect them with ->container_content and give
2946 * them to the kernel.
2948 if (st
->ss
->reshape_super
&& st
->ss
->container_content
) {
2949 char *subarray
= strchr(info
->text_version
+1, '/')+1;
2950 struct mdinfo
*info2
=
2951 st
->ss
->container_content(st
, subarray
);
2955 sysfs_init(info2
, fd
, st
->devnm
);
2956 /* When increasing number of devices, we need to set
2957 * new raid_disks before adding these, or they might
2960 if (reshape
.backup_blocks
&&
2961 reshape
.after
.data_disks
> reshape
.before
.data_disks
)
2962 subarray_set_num(container
, info2
, "raid_disks",
2963 reshape
.after
.data_disks
+
2965 for (d
= info2
->devs
; d
; d
= d
->next
) {
2966 if (d
->disk
.state
== 0 &&
2967 d
->disk
.raid_disk
>= 0) {
2968 /* This is a spare that wants to
2969 * be part of the array.
2971 add_disk(fd
, st
, info2
, d
);
2977 /* We might have been given some devices to add to the
2978 * array. Now that the array has been changed to the right
2979 * level and frozen, we can safely add them.
2982 Manage_subdevs(devname
, fd
, devlist
, verbose
,
2985 if (reshape
.backup_blocks
== 0 && data_offset
!= INVALID_SECTORS
)
2986 reshape
.backup_blocks
= reshape
.before
.data_disks
* info
->array
.chunk_size
/512;
2987 if (reshape
.backup_blocks
== 0) {
2988 /* No restriping needed, but we might need to impose
2989 * some more changes: layout, raid_disks, chunk_size
2991 /* read current array info */
2992 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
2993 dprintf("Cannot get array information.\n");
2996 /* compare current array info with new values and if
2997 * it is different update them to new */
2998 if (info
->new_layout
!= UnSet
&&
2999 info
->new_layout
!= array
.layout
) {
3000 array
.layout
= info
->new_layout
;
3001 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
3002 pr_err("failed to set new layout\n");
3004 } else if (verbose
>= 0)
3005 printf("layout for %s set to %d\n",
3006 devname
, array
.layout
);
3008 if (info
->delta_disks
!= UnSet
&&
3009 info
->delta_disks
!= 0 &&
3010 array
.raid_disks
!= (info
->array
.raid_disks
+ info
->delta_disks
)) {
3011 array
.raid_disks
+= info
->delta_disks
;
3012 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
3013 pr_err("failed to set raid disks\n");
3015 } else if (verbose
>= 0) {
3016 printf("raid_disks for %s set to %d\n",
3017 devname
, array
.raid_disks
);
3020 if (info
->new_chunk
!= 0 &&
3021 info
->new_chunk
!= array
.chunk_size
) {
3022 if (sysfs_set_num(info
, NULL
,
3023 "chunk_size", info
->new_chunk
) != 0) {
3024 pr_err("failed to set chunk size\n");
3026 } else if (verbose
>= 0)
3027 printf("chunk size for %s set to %d\n",
3028 devname
, array
.chunk_size
);
3035 * There are three possibilities.
3036 * 1/ The array will shrink.
3037 * We need to ensure the reshape will pause before reaching
3038 * the 'critical section'. We also need to fork and wait for
3039 * that to happen. When it does we
3040 * suspend/backup/complete/unfreeze
3042 * 2/ The array will not change size.
3043 * This requires that we keep a backup of a sliding window
3044 * so that we can restore data after a crash. So we need
3045 * to fork and monitor progress.
3046 * In future we will allow the data_offset to change, so
3047 * a sliding backup becomes unnecessary.
3049 * 3/ The array will grow. This is relatively easy.
3050 * However the kernel's restripe routines will cheerfully
3051 * overwrite some early data before it is safe. So we
3052 * need to make a backup of the early parts of the array
3053 * and be ready to restore it if rebuild aborts very early.
3054 * For externally managed metadata, we still need a forked
3055 * child to monitor the reshape and suspend IO over the region
3056 * that is being reshaped.
3058 * We backup data by writing it to one spare, or to a
3059 * file which was given on command line.
3061 * In each case, we first make sure that storage is available
3062 * for the required backup.
3064 * - request the shape change.
3065 * - fork to handle backup etc.
3067 /* Check that we can hold all the data */
3068 get_dev_size(fd
, NULL
, &array_size
);
3069 if (reshape
.new_size
< (array_size
/512)) {
3070 pr_err("this change will reduce the size of the array.\n"
3071 " use --grow --array-size first to truncate array.\n"
3072 " e.g. mdadm --grow %s --array-size %llu\n",
3073 devname
, reshape
.new_size
/2);
3077 if (array
.level
== 10) {
3078 /* Reshaping RAID10 does not require any data backup by
3079 * user-space. Instead it requires that the data_offset
3080 * is changed to avoid the need for backup.
3081 * So this is handled very separately
3084 /* Nothing to do. */
3086 return raid10_reshape(container
, fd
, devname
, st
, info
,
3087 &reshape
, data_offset
,
3090 sra
= sysfs_read(fd
, NULL
,
3091 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
|
3094 pr_err("%s: Cannot get array details from sysfs\n",
3100 switch(set_new_data_offset(sra
, st
, devname
,
3101 reshape
.after
.data_disks
- reshape
.before
.data_disks
,
3103 reshape
.min_offset_change
, 1)) {
3107 /* Updated data_offset, so it's easy now */
3108 update_cache_size(container
, sra
, info
,
3109 min(reshape
.before
.data_disks
,
3110 reshape
.after
.data_disks
),
3111 reshape
.backup_blocks
);
3113 /* Right, everything seems fine. Let's kick things off.
3117 if (impose_reshape(sra
, info
, st
, fd
, restart
,
3118 devname
, container
, &reshape
) < 0)
3120 if (sysfs_set_str(sra
, NULL
, "sync_action", "reshape") < 0) {
3122 if (errno
!= EINVAL
) {
3123 pr_err("Failed to initiate reshape!\n");
3126 /* revert data_offset and try the old way */
3127 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
3128 sysfs_set_num(sra
, sd
, "new_offset",
3130 sysfs_set_str(sra
, NULL
, "reshape_direction",
3135 if (info
->new_level
== reshape
.level
)
3137 /* need to adjust level when reshape completes */
3139 case -1: /* ignore error, but don't wait */
3141 default: /* parent */
3149 fd
= open_dev(sra
->sys_name
);
3151 impose_level(fd
, info
->new_level
, devname
, verbose
);
3153 case 1: /* Couldn't set data_offset, try the old way */
3154 if (data_offset
!= INVALID_SECTORS
) {
3155 pr_err("Cannot update data_offset on this array\n");
3162 /* Decide how many blocks (sectors) for a reshape
3163 * unit. The number we have so far is just a minimum
3165 blocks
= reshape
.backup_blocks
;
3166 if (reshape
.before
.data_disks
==
3167 reshape
.after
.data_disks
) {
3168 /* Make 'blocks' bigger for better throughput, but
3169 * not so big that we reject it below.
3170 * Try for 16 megabytes
3172 while (blocks
* 32 < sra
->component_size
&&
3176 pr_err("Need to backup %luK of critical section..\n", blocks
/2);
3178 if (blocks
>= sra
->component_size
/2) {
3179 pr_err("%s: Something wrong - reshape aborted\n",
3184 /* Now we need to open all these devices so we can read/write.
3186 nrdisks
= max(reshape
.before
.data_disks
,
3187 reshape
.after
.data_disks
) + reshape
.parity
3188 + sra
->array
.spare_disks
;
3189 fdlist
= xcalloc((1+nrdisks
), sizeof(int));
3190 offsets
= xcalloc((1+nrdisks
), sizeof(offsets
[0]));
3192 odisks
= reshape
.before
.data_disks
+ reshape
.parity
;
3193 d
= reshape_prepare_fdlist(devname
, sra
, odisks
,
3194 nrdisks
, blocks
, backup_file
,
3199 if ((st
->ss
->manage_reshape
== NULL
) ||
3200 (st
->ss
->recover_backup
== NULL
)) {
3201 if (backup_file
== NULL
) {
3202 if (reshape
.after
.data_disks
<=
3203 reshape
.before
.data_disks
) {
3204 pr_err("%s: Cannot grow - need backup-file\n",
3206 pr_err(" Please provide one with \"--backup=...\"\n");
3208 } else if (d
== odisks
) {
3209 pr_err("%s: Cannot grow - need a spare or backup-file to backup critical section\n", devname
);
3213 if (!reshape_open_backup_file(backup_file
, fd
, devname
,
3215 fdlist
+d
, offsets
+d
,
3224 update_cache_size(container
, sra
, info
,
3225 min(reshape
.before
.data_disks
, reshape
.after
.data_disks
),
3228 /* Right, everything seems fine. Let's kick things off.
3229 * If only changing raid_disks, use ioctl, else use
3234 if (impose_reshape(sra
, info
, st
, fd
, restart
,
3235 devname
, container
, &reshape
) < 0)
3238 err
= start_reshape(sra
, restart
, reshape
.before
.data_disks
,
3239 reshape
.after
.data_disks
);
3241 pr_err("Cannot %s reshape for %s\n",
3242 restart
? "continue" : "start",
3247 sysfs_set_str(sra
, NULL
, "array_state", "active");
3248 if (freeze_reshape
) {
3252 pr_err("Reshape has to be continued from location %llu when root filesystem has been mounted.\n",
3253 sra
->reshape_progress
);
3257 if (!forked
&& !check_env("MDADM_NO_SYSTEMCTL"))
3258 if (continue_via_systemd(container
?: sra
->sys_name
)) {
3265 /* Now we just need to kick off the reshape and watch, while
3266 * handling backups of the data...
3267 * This is all done by a forked background process.
3269 switch(forked
? 0 : fork()) {
3271 pr_err("Cannot run child to monitor reshape: %s\n",
3285 /* If another array on the same devices is busy, the
3286 * reshape will wait for them. This would mean that
3287 * the first section that we suspend will stay suspended
3288 * for a long time. So check on that possibility
3289 * by looking for "DELAYED" in /proc/mdstat, and if found,
3293 struct mdstat_ent
*mds
, *m
;
3295 mds
= mdstat_read(1, 0);
3296 for (m
= mds
; m
; m
= m
->next
)
3297 if (strcmp(m
->devnm
, sra
->sys_name
) == 0) {
3299 m
->percent
== RESYNC_DELAYED
)
3302 /* Haven't started the reshape thread
3309 if (delayed
== 1 && get_linux_version() < 3007000) {
3310 pr_err("Reshape is delayed, but cannot wait carefully with this kernel.\n"
3311 " You might experience problems until other reshapes complete.\n");
3315 mdstat_wait(30 - (delayed
-1) * 25);
3319 if (check_env("MDADM_GROW_VERIFY"))
3320 fd
= open(devname
, O_RDONLY
| O_DIRECT
);
3323 mlockall(MCL_FUTURE
);
3325 signal(SIGTERM
, catch_term
);
3327 if (st
->ss
->external
) {
3328 /* metadata handler takes it from here */
3329 done
= st
->ss
->manage_reshape(
3330 fd
, sra
, &reshape
, st
, blocks
,
3332 d
- odisks
, fdlist
+odisks
,
3335 done
= child_monitor(
3336 fd
, sra
, &reshape
, st
, blocks
,
3338 d
- odisks
, fdlist
+odisks
,
3344 if (backup_file
&& done
) {
3346 bul
= make_backup(sra
->sys_name
);
3349 int l
= readlink(bul
, buf
, sizeof(buf
) - 1);
3357 unlink(backup_file
);
3364 if (!st
->ss
->external
&&
3365 !(reshape
.before
.data_disks
!= reshape
.after
.data_disks
3366 && info
->custom_array_size
) &&
3367 info
->new_level
== reshape
.level
&&
3369 /* no need to wait for the reshape to finish as
3370 * there is nothing more to do.
3377 if (st
->ss
->external
) {
3378 /* Re-load the metadata as much could have changed */
3379 int cfd
= open_dev(st
->container_devnm
);
3381 flush_mdmon(container
);
3382 st
->ss
->free_super(st
);
3383 st
->ss
->load_container(st
, cfd
, container
);
3388 /* set new array size if required customer_array_size is used
3391 if (reshape
.before
.data_disks
!=
3392 reshape
.after
.data_disks
&&
3393 info
->custom_array_size
)
3394 set_array_size(st
, info
, info
->text_version
);
3396 if (info
->new_level
!= reshape
.level
) {
3398 fd
= open(devname
, O_RDONLY
);
3399 impose_level(fd
, info
->new_level
, devname
, verbose
);
3401 if (info
->new_level
== 0)
3402 st
->update_tail
= NULL
;
3414 if (orig_level
!= UnSet
&& sra
) {
3415 c
= map_num(pers
, orig_level
);
3416 if (c
&& sysfs_set_str(sra
, NULL
, "level", c
) == 0)
3417 pr_err("aborting level change\n");
3425 /* mdfd handle is passed to be closed in child process (after fork).
3427 int reshape_container(char *container
, char *devname
,
3429 struct supertype
*st
,
3430 struct mdinfo
*info
,
3432 char *backup_file
, int verbose
,
3433 int forked
, int restart
, int freeze_reshape
)
3435 struct mdinfo
*cc
= NULL
;
3437 char last_devnm
[32] = "";
3439 /* component_size is not meaningful for a container,
3440 * so pass '0' meaning 'no change'
3443 reshape_super(st
, 0, info
->new_level
,
3444 info
->new_layout
, info
->new_chunk
,
3445 info
->array
.raid_disks
, info
->delta_disks
,
3446 backup_file
, devname
, APPLY_METADATA_CHANGES
,
3454 /* ping monitor to be sure that update is on disk
3456 ping_monitor(container
);
3458 if (!forked
&& !freeze_reshape
&& !check_env("MDADM_NO_SYSTEMCTL"))
3459 if (continue_via_systemd(container
))
3462 switch (forked
? 0 : fork()) {
3463 case -1: /* error */
3464 perror("Cannot fork to complete reshape\n");
3467 default: /* parent */
3468 if (!freeze_reshape
)
3469 printf("%s: multi-array reshape continues in background\n", Name
);
3476 /* close unused handle in child process
3482 /* For each member array with reshape_active,
3483 * we need to perform the reshape.
3484 * We pick the first array that needs reshaping and
3485 * reshape it. reshape_array() will re-read the metadata
3486 * so the next time through a different array should be
3487 * ready for reshape.
3488 * It is possible that the 'different' array will not
3489 * be assembled yet. In that case we simple exit.
3490 * When it is assembled, the mdadm which assembles it
3491 * will take over the reshape.
3493 struct mdinfo
*content
;
3495 struct mdstat_ent
*mdstat
;
3501 cc
= st
->ss
->container_content(st
, NULL
);
3503 for (content
= cc
; content
; content
= content
->next
) {
3505 if (!content
->reshape_active
)
3508 subarray
= strchr(content
->text_version
+1, '/')+1;
3509 mdstat
= mdstat_by_subdev(subarray
, container
);
3512 if (mdstat
->active
== 0) {
3513 pr_err("Skipping inactive array %s.\n",
3515 free_mdstat(mdstat
);
3524 devid
= devnm2devid(mdstat
->devnm
);
3525 adev
= map_dev(major(devid
), minor(devid
), 0);
3527 adev
= content
->text_version
;
3529 fd
= open_dev(mdstat
->devnm
);
3531 pr_err("Device %s cannot be opened for reshape.\n", adev
);
3535 if (strcmp(last_devnm
, mdstat
->devnm
) == 0) {
3536 /* Do not allow for multiple reshape_array() calls for
3538 * It can happen when reshape_array() returns without
3539 * error, when reshape is not finished (wrong reshape
3540 * starting/continuation conditions). Mdmon doesn't
3541 * switch to next array in container and reentry
3542 * conditions for the same array occur.
3543 * This is possibly interim until the behaviour of
3544 * reshape_array is resolved().
3546 printf("%s: Multiple reshape execution detected for device %s.\n", Name
, adev
);
3550 strcpy(last_devnm
, mdstat
->devnm
);
3552 sysfs_init(content
, fd
, mdstat
->devnm
);
3554 if (mdmon_running(container
))
3555 flush_mdmon(container
);
3557 rv
= reshape_array(container
, fd
, adev
, st
,
3558 content
, force
, NULL
, INVALID_SECTORS
,
3559 backup_file
, verbose
, 1, restart
,
3563 if (freeze_reshape
) {
3572 if (mdmon_running(container
))
3573 flush_mdmon(container
);
3582 * We run a child process in the background which performs the following
3584 * - wait for resync to reach a certain point
3585 * - suspend io to the following section
3586 * - backup that section
3587 * - allow resync to proceed further
3589 * - discard the backup.
3591 * When are combined in slightly different ways in the three cases.
3593 * - suspend/backup/allow/wait/resume/discard
3595 * - allow/wait/suspend/backup/allow/wait/resume/discard
3597 * - wait/resume/discard/suspend/backup/allow
3599 * suspend/backup/allow always come together
3600 * wait/resume/discard do too.
3601 * For the same-size case we have two backups to improve flow.
3605 int progress_reshape(struct mdinfo
*info
, struct reshape
*reshape
,
3606 unsigned long long backup_point
,
3607 unsigned long long wait_point
,
3608 unsigned long long *suspend_point
,
3609 unsigned long long *reshape_completed
, int *frozen
)
3611 /* This function is called repeatedly by the reshape manager.
3612 * It determines how much progress can safely be made and allows
3614 * - 'info' identifies the array and particularly records in
3615 * ->reshape_progress the metadata's knowledge of progress
3616 * This is a sector offset from the start of the array
3617 * of the next array block to be relocated. This number
3618 * may increase from 0 or decrease from array_size, depending
3619 * on the type of reshape that is happening.
3620 * Note that in contrast, 'sync_completed' is a block count of the
3621 * reshape so far. It gives the distance between the start point
3622 * (head or tail of device) and the next place that data will be
3623 * written. It always increases.
3624 * - 'reshape' is the structure created by analyse_change
3625 * - 'backup_point' shows how much the metadata manager has backed-up
3626 * data. For reshapes with increasing progress, it is the next address
3627 * to be backed up, previous addresses have been backed-up. For
3628 * decreasing progress, it is the earliest address that has been
3629 * backed up - later address are also backed up.
3630 * So addresses between reshape_progress and backup_point are
3631 * backed up providing those are in the 'correct' order.
3632 * - 'wait_point' is an array address. When reshape_completed
3633 * passes this point, progress_reshape should return. It might
3634 * return earlier if it determines that ->reshape_progress needs
3635 * to be updated or further backup is needed.
3636 * - suspend_point is maintained by progress_reshape and the caller
3637 * should not touch it except to initialise to zero.
3638 * It is an array address and it only increases in 2.6.37 and earlier.
3639 * This makes it difficult to handle reducing reshapes with
3640 * external metadata.
3641 * However: it is similar to backup_point in that it records the
3642 * other end of a suspended region from reshape_progress.
3643 * it is moved to extend the region that is safe to backup and/or
3645 * - reshape_completed is read from sysfs and returned. The caller
3646 * should copy this into ->reshape_progress when it has reason to
3647 * believe that the metadata knows this, and any backup outside this
3651 * 1 if more data from backup_point - but only as far as suspend_point,
3652 * should be backed up
3653 * 0 if things are progressing smoothly
3654 * -1 if the reshape is finished because it is all done,
3655 * -2 if the reshape is finished due to an error.
3658 int advancing
= (reshape
->after
.data_disks
3659 >= reshape
->before
.data_disks
);
3660 unsigned long long need_backup
; /* All data between start of array and
3661 * here will at some point need to
3664 unsigned long long read_offset
, write_offset
;
3665 unsigned long long write_range
;
3666 unsigned long long max_progress
, target
, completed
;
3667 unsigned long long array_size
= (info
->component_size
3668 * reshape
->before
.data_disks
);
3672 /* First, we unsuspend any region that is now known to be safe.
3673 * If suspend_point is on the 'wrong' side of reshape_progress, then
3674 * we don't have or need suspension at the moment. This is true for
3675 * native metadata when we don't need to back-up.
3678 if (info
->reshape_progress
<= *suspend_point
)
3679 sysfs_set_num(info
, NULL
, "suspend_lo",
3680 info
->reshape_progress
);
3682 /* Note: this won't work in 2.6.37 and before.
3683 * Something somewhere should make sure we don't need it!
3685 if (info
->reshape_progress
>= *suspend_point
)
3686 sysfs_set_num(info
, NULL
, "suspend_hi",
3687 info
->reshape_progress
);
3690 /* Now work out how far it is safe to progress.
3691 * If the read_offset for ->reshape_progress is less than
3692 * 'blocks' beyond the write_offset, we can only progress as far
3694 * Otherwise we can progress until the write_offset for the new location
3695 * reaches (within 'blocks' of) the read_offset at the current location.
3696 * However that region must be suspended unless we are using native
3698 * If we need to suspend more, we limit it to 128M per device, which is
3699 * rather arbitrary and should be some time-based calculation.
3701 read_offset
= info
->reshape_progress
/ reshape
->before
.data_disks
;
3702 write_offset
= info
->reshape_progress
/ reshape
->after
.data_disks
;
3703 write_range
= info
->new_chunk
/512;
3704 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
3705 need_backup
= array_size
;
3707 need_backup
= reshape
->backup_blocks
;
3709 if (read_offset
< write_offset
+ write_range
)
3710 max_progress
= backup_point
;
3714 reshape
->after
.data_disks
;
3716 if (read_offset
> write_offset
- write_range
)
3717 /* Can only progress as far as has been backed up,
3718 * which must be suspended */
3719 max_progress
= backup_point
;
3720 else if (info
->reshape_progress
<= need_backup
)
3721 max_progress
= backup_point
;
3723 if (info
->array
.major_version
>= 0)
3724 /* Can progress until backup is needed */
3725 max_progress
= need_backup
;
3727 /* Can progress until metadata update is required */
3730 reshape
->after
.data_disks
;
3731 /* but data must be suspended */
3732 if (max_progress
< *suspend_point
)
3733 max_progress
= *suspend_point
;
3738 /* We know it is safe to progress to 'max_progress' providing
3739 * it is suspended or we are using native metadata.
3740 * Consider extending suspend_point 128M per device if it
3741 * is less than 64M per device beyond reshape_progress.
3742 * But always do a multiple of 'blocks'
3743 * FIXME this is too big - it takes to long to complete
3746 target
= 64*1024*2 * min(reshape
->before
.data_disks
,
3747 reshape
->after
.data_disks
);
3748 target
/= reshape
->backup_blocks
;
3751 target
*= reshape
->backup_blocks
;
3753 /* For externally managed metadata we always need to suspend IO to
3754 * the area being reshaped so we regularly push suspend_point forward.
3755 * For native metadata we only need the suspend if we are going to do
3759 if ((need_backup
> info
->reshape_progress
3760 || info
->array
.major_version
< 0) &&
3761 *suspend_point
< info
->reshape_progress
+ target
) {
3762 if (need_backup
< *suspend_point
+ 2 * target
)
3763 *suspend_point
= need_backup
;
3764 else if (*suspend_point
+ 2 * target
< array_size
)
3765 *suspend_point
+= 2 * target
;
3767 *suspend_point
= array_size
;
3768 sysfs_set_num(info
, NULL
, "suspend_hi", *suspend_point
);
3769 if (max_progress
> *suspend_point
)
3770 max_progress
= *suspend_point
;
3773 if (info
->array
.major_version
>= 0) {
3774 /* Only need to suspend when about to backup */
3775 if (info
->reshape_progress
< need_backup
* 2 &&
3776 *suspend_point
> 0) {
3778 sysfs_set_num(info
, NULL
, "suspend_lo", 0);
3779 sysfs_set_num(info
, NULL
, "suspend_hi", need_backup
);
3782 /* Need to suspend continually */
3783 if (info
->reshape_progress
< *suspend_point
)
3784 *suspend_point
= info
->reshape_progress
;
3785 if (*suspend_point
+ target
< info
->reshape_progress
)
3786 /* No need to move suspend region yet */;
3788 if (*suspend_point
>= 2 * target
)
3789 *suspend_point
-= 2 * target
;
3792 sysfs_set_num(info
, NULL
, "suspend_lo",
3795 if (max_progress
< *suspend_point
)
3796 max_progress
= *suspend_point
;
3800 /* now set sync_max to allow that progress. sync_max, like
3801 * sync_completed is a count of sectors written per device, so
3802 * we find the difference between max_progress and the start point,
3803 * and divide that by after.data_disks to get a sync_max
3805 * At the same time we convert wait_point to a similar number
3806 * for comparing against sync_completed.
3808 /* scale down max_progress to per_disk */
3809 max_progress
/= reshape
->after
.data_disks
;
3810 /* Round to chunk size as some kernels give an erroneously high number */
3811 max_progress
/= info
->new_chunk
/512;
3812 max_progress
*= info
->new_chunk
/512;
3813 /* And round to old chunk size as the kernel wants that */
3814 max_progress
/= info
->array
.chunk_size
/512;
3815 max_progress
*= info
->array
.chunk_size
/512;
3816 /* Limit progress to the whole device */
3817 if (max_progress
> info
->component_size
)
3818 max_progress
= info
->component_size
;
3819 wait_point
/= reshape
->after
.data_disks
;
3821 /* switch from 'device offset' to 'processed block count' */
3822 max_progress
= info
->component_size
- max_progress
;
3823 wait_point
= info
->component_size
- wait_point
;
3827 sysfs_set_num(info
, NULL
, "sync_max", max_progress
);
3829 /* Now wait. If we have already reached the point that we were
3830 * asked to wait to, don't wait at all, else wait for any change.
3831 * We need to select on 'sync_completed' as that is the place that
3832 * notifications happen, but we are really interested in
3833 * 'reshape_position'
3835 fd
= sysfs_get_fd(info
, NULL
, "sync_completed");
3837 goto check_progress
;
3839 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
3840 goto check_progress
;
3842 while (completed
< max_progress
&& completed
< wait_point
) {
3843 /* Check that sync_action is still 'reshape' to avoid
3844 * waiting forever on a dead array
3847 if (sysfs_get_str(info
, NULL
, "sync_action",
3849 strncmp(action
, "reshape", 7) != 0)
3851 /* Some kernels reset 'sync_completed' to zero
3852 * before setting 'sync_action' to 'idle'.
3853 * So we need these extra tests.
3855 if (completed
== 0 && advancing
3856 && strncmp(action
, "idle", 4) == 0
3857 && info
->reshape_progress
> 0)
3859 if (completed
== 0 && !advancing
3860 && strncmp(action
, "idle", 4) == 0
3861 && info
->reshape_progress
< (info
->component_size
3862 * reshape
->after
.data_disks
))
3864 sysfs_wait(fd
, NULL
);
3865 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
3866 goto check_progress
;
3868 /* Some kernels reset 'sync_completed' to zero,
3869 * we need to have real point we are in md.
3870 * So in that case, read 'reshape_position' from sysfs.
3872 if (completed
== 0) {
3873 unsigned long long reshapep
;
3875 if (sysfs_get_str(info
, NULL
, "sync_action",
3877 strncmp(action
, "idle", 4) == 0 &&
3878 sysfs_get_ll(info
, NULL
,
3879 "reshape_position", &reshapep
) == 0)
3880 *reshape_completed
= reshapep
;
3882 /* some kernels can give an incorrectly high
3883 * 'completed' number, so round down */
3884 completed
/= (info
->new_chunk
/512);
3885 completed
*= (info
->new_chunk
/512);
3886 /* Convert 'completed' back in to a 'progress' number */
3887 completed
*= reshape
->after
.data_disks
;
3889 completed
= (info
->component_size
3890 * reshape
->after
.data_disks
3892 *reshape_completed
= completed
;
3897 /* We return the need_backup flag. Caller will decide
3898 * how much - a multiple of ->backup_blocks up to *suspend_point
3901 return need_backup
> info
->reshape_progress
;
3903 return need_backup
>= info
->reshape_progress
;
3906 /* if we couldn't read a number from sync_completed, then
3907 * either the reshape did complete, or it aborted.
3908 * We can tell which by checking for 'none' in reshape_position.
3909 * If it did abort, then it might immediately restart if it
3910 * it was just a device failure that leaves us degraded but
3913 if (sysfs_get_str(info
, NULL
, "reshape_position", buf
, sizeof(buf
)) < 0
3914 || strncmp(buf
, "none", 4) != 0) {
3915 /* The abort might only be temporary. Wait up to 10
3916 * seconds for fd to contain a valid number again.
3920 unsigned long long new_sync_max
;
3921 while (fd
>= 0 && rv
< 0 && wait
> 0) {
3922 if (sysfs_wait(fd
, &wait
) != 1)
3924 switch (sysfs_fd_get_ll(fd
, &completed
)) {
3926 /* all good again */
3928 /* If "sync_max" is no longer max_progress
3929 * we need to freeze things
3931 sysfs_get_ll(info
, NULL
, "sync_max", &new_sync_max
);
3932 *frozen
= (new_sync_max
!= max_progress
);
3934 case -2: /* read error - abort */
3941 return rv
; /* abort */
3943 /* Maybe racing with array shutdown - check state */
3946 if (sysfs_get_str(info
, NULL
, "array_state", buf
, sizeof(buf
)) < 0
3947 || strncmp(buf
, "inactive", 8) == 0
3948 || strncmp(buf
, "clear",5) == 0)
3949 return -2; /* abort */
3950 return -1; /* complete */
3954 /* FIXME return status is never checked */
3955 static int grow_backup(struct mdinfo
*sra
,
3956 unsigned long long offset
, /* per device */
3957 unsigned long stripes
, /* per device, in old chunks */
3958 int *sources
, unsigned long long *offsets
,
3959 int disks
, int chunk
, int level
, int layout
,
3960 int dests
, int *destfd
, unsigned long long *destoffsets
,
3961 int part
, int *degraded
,
3964 /* Backup 'blocks' sectors at 'offset' on each device of the array,
3965 * to storage 'destfd' (offset 'destoffsets'), after first
3966 * suspending IO. Then allow resync to continue
3967 * over the suspended section.
3968 * Use part 'part' of the backup-super-block.
3973 unsigned long long ll
;
3975 //printf("offset %llu\n", offset);
3981 /* Check that array hasn't become degraded, else we might backup the wrong data */
3982 if (sysfs_get_ll(sra
, NULL
, "degraded", &ll
) < 0)
3983 return -1; /* FIXME this error is ignored */
3984 new_degraded
= (int)ll
;
3985 if (new_degraded
!= *degraded
) {
3986 /* check each device to ensure it is still working */
3988 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
3989 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
3991 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
3993 if (sysfs_get_str(sra
, sd
, "state", sbuf
, 20) < 0 ||
3994 strstr(sbuf
, "faulty") ||
3995 strstr(sbuf
, "in_sync") == NULL
) {
3996 /* this device is dead */
3997 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
3998 if (sd
->disk
.raid_disk
>= 0 &&
3999 sources
[sd
->disk
.raid_disk
] >= 0) {
4000 close(sources
[sd
->disk
.raid_disk
]);
4001 sources
[sd
->disk
.raid_disk
] = -1;
4006 *degraded
= new_degraded
;
4009 bsb
.arraystart2
= __cpu_to_le64(offset
* odata
);
4010 bsb
.length2
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
4012 bsb
.arraystart
= __cpu_to_le64(offset
* odata
);
4013 bsb
.length
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
4016 bsb
.magic
[15] = '2';
4017 for (i
= 0; i
< dests
; i
++)
4019 lseek64(destfd
[i
], destoffsets
[i
] + __le64_to_cpu(bsb
.devstart2
)*512, 0);
4021 lseek64(destfd
[i
], destoffsets
[i
], 0);
4023 rv
= save_stripes(sources
, offsets
,
4024 disks
, chunk
, level
, layout
,
4026 offset
*512*odata
, stripes
* chunk
* odata
,
4031 bsb
.mtime
= __cpu_to_le64(time(0));
4032 for (i
= 0; i
< dests
; i
++) {
4033 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
4035 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
4036 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
4037 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
4038 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
4041 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
] - 4096, 0)
4042 != destoffsets
[i
] - 4096)
4044 if (write(destfd
[i
], &bsb
, 512) != 512)
4046 if (destoffsets
[i
] > 4096) {
4047 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]+stripes
*chunk
*odata
, 0) !=
4048 destoffsets
[i
]+stripes
*chunk
*odata
)
4050 if (write(destfd
[i
], &bsb
, 512) != 512)
4060 /* in 2.6.30, the value reported by sync_completed can be
4061 * less that it should be by one stripe.
4062 * This only happens when reshape hits sync_max and pauses.
4063 * So allow wait_backup to either extent sync_max further
4064 * than strictly necessary, or return before the
4065 * sync has got quite as far as we would really like.
4066 * This is what 'blocks2' is for.
4067 * The various caller give appropriate values so that
4070 /* FIXME return value is often ignored */
4071 static int forget_backup(int dests
, int *destfd
,
4072 unsigned long long *destoffsets
,
4076 * Erase backup 'part' (which is 0 or 1)
4082 bsb
.arraystart2
= __cpu_to_le64(0);
4083 bsb
.length2
= __cpu_to_le64(0);
4085 bsb
.arraystart
= __cpu_to_le64(0);
4086 bsb
.length
= __cpu_to_le64(0);
4088 bsb
.mtime
= __cpu_to_le64(time(0));
4090 for (i
= 0; i
< dests
; i
++) {
4091 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
4092 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
4093 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
4094 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
4095 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
4096 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]-4096, 0) !=
4097 destoffsets
[i
]-4096)
4100 write(destfd
[i
], &bsb
, 512) != 512)
4107 static void fail(char *msg
)
4110 rv
= (write(2, msg
, strlen(msg
)) != (int)strlen(msg
));
4111 rv
|= (write(2, "\n", 1) != 1);
4115 static char *abuf
, *bbuf
;
4116 static unsigned long long abuflen
;
4117 static void validate(int afd
, int bfd
, unsigned long long offset
)
4119 /* check that the data in the backup against the array.
4120 * This is only used for regression testing and should not
4121 * be used while the array is active
4125 lseek64(bfd
, offset
- 4096, 0);
4126 if (read(bfd
, &bsb2
, 512) != 512)
4127 fail("cannot read bsb");
4128 if (bsb2
.sb_csum
!= bsb_csum((char*)&bsb2
,
4129 ((char*)&bsb2
.sb_csum
)-((char*)&bsb2
)))
4130 fail("first csum bad");
4131 if (memcmp(bsb2
.magic
, "md_backup_data", 14) != 0)
4132 fail("magic is bad");
4133 if (memcmp(bsb2
.magic
, "md_backup_data-2", 16) == 0 &&
4134 bsb2
.sb_csum2
!= bsb_csum((char*)&bsb2
,
4135 ((char*)&bsb2
.sb_csum2
)-((char*)&bsb2
)))
4136 fail("second csum bad");
4138 if (__le64_to_cpu(bsb2
.devstart
)*512 != offset
)
4139 fail("devstart is wrong");
4142 unsigned long long len
= __le64_to_cpu(bsb2
.length
)*512;
4144 if (abuflen
< len
) {
4148 if (posix_memalign((void**)&abuf
, 4096, abuflen
) ||
4149 posix_memalign((void**)&bbuf
, 4096, abuflen
)) {
4151 /* just stop validating on mem-alloc failure */
4156 lseek64(bfd
, offset
, 0);
4157 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
) {
4158 //printf("len %llu\n", len);
4159 fail("read first backup failed");
4161 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart
)*512, 0);
4162 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
4163 fail("read first from array failed");
4164 if (memcmp(bbuf
, abuf
, len
) != 0) {
4167 printf("offset=%llu len=%llu\n",
4168 (unsigned long long)__le64_to_cpu(bsb2
.arraystart
)*512, len
);
4169 for (i
=0; i
<len
; i
++)
4170 if (bbuf
[i
] != abuf
[i
]) {
4171 printf("first diff byte %d\n", i
);
4175 fail("data1 compare failed");
4179 unsigned long long len
= __le64_to_cpu(bsb2
.length2
)*512;
4181 if (abuflen
< len
) {
4185 abuf
= xmalloc(abuflen
);
4186 bbuf
= xmalloc(abuflen
);
4189 lseek64(bfd
, offset
+__le64_to_cpu(bsb2
.devstart2
)*512, 0);
4190 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
)
4191 fail("read second backup failed");
4192 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart2
)*512, 0);
4193 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
4194 fail("read second from array failed");
4195 if (memcmp(bbuf
, abuf
, len
) != 0)
4196 fail("data2 compare failed");
4200 int child_monitor(int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
4201 struct supertype
*st
, unsigned long blocks
,
4202 int *fds
, unsigned long long *offsets
,
4203 int dests
, int *destfd
, unsigned long long *destoffsets
)
4205 /* Monitor a reshape where backup is being performed using
4206 * 'native' mechanism - either to a backup file, or
4207 * to some space in a spare.
4211 unsigned long long speed
;
4212 unsigned long long suspend_point
, array_size
;
4213 unsigned long long backup_point
, wait_point
;
4214 unsigned long long reshape_completed
;
4216 int increasing
= reshape
->after
.data_disks
>= reshape
->before
.data_disks
;
4217 int part
= 0; /* The next part of the backup area to fill. It may already
4218 * be full, so we need to check */
4219 int level
= reshape
->level
;
4220 int layout
= reshape
->before
.layout
;
4221 int data
= reshape
->before
.data_disks
;
4222 int disks
= reshape
->before
.data_disks
+ reshape
->parity
;
4223 int chunk
= sra
->array
.chunk_size
;
4225 unsigned long stripes
;
4229 /* set up the backup-super-block. This requires the
4230 * uuid from the array.
4232 /* Find a superblock */
4233 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
4237 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
4239 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
4240 devfd
= dev_open(dn
, O_RDONLY
);
4243 ok
= st
->ss
->load_super(st
, devfd
, NULL
);
4249 pr_err("Cannot find a superblock\n");
4253 memset(&bsb
, 0, 512);
4254 memcpy(bsb
.magic
, "md_backup_data-1", 16);
4255 st
->ss
->uuid_from_super(st
, uuid
);
4256 memcpy(bsb
.set_uuid
, uuid
, 16);
4257 bsb
.mtime
= __cpu_to_le64(time(0));
4258 bsb
.devstart2
= blocks
;
4260 stripes
= blocks
/ (sra
->array
.chunk_size
/512) /
4261 reshape
->before
.data_disks
;
4263 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
4264 /* Don't start the 'reshape' */
4266 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
) {
4267 sysfs_get_ll(sra
, NULL
, "sync_speed_min", &speed
);
4268 sysfs_set_num(sra
, NULL
, "sync_speed_min", 200000);
4272 array_size
= sra
->component_size
* reshape
->after
.data_disks
;
4273 backup_point
= sra
->reshape_progress
;
4276 array_size
= sra
->component_size
* reshape
->before
.data_disks
;
4277 backup_point
= reshape
->backup_blocks
;
4278 suspend_point
= array_size
;
4284 /* Want to return as soon the oldest backup slot can
4285 * be released as that allows us to start backing up
4286 * some more, providing suspend_point has been
4287 * advanced, which it should have.
4290 wait_point
= array_size
;
4291 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
4292 wait_point
= (__le64_to_cpu(bsb
.arraystart
) +
4293 __le64_to_cpu(bsb
.length
));
4294 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
4295 wait_point
= (__le64_to_cpu(bsb
.arraystart2
) +
4296 __le64_to_cpu(bsb
.length2
));
4299 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
4300 wait_point
= __le64_to_cpu(bsb
.arraystart
);
4301 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
4302 wait_point
= __le64_to_cpu(bsb
.arraystart2
);
4305 reshape_completed
= sra
->reshape_progress
;
4306 rv
= progress_reshape(sra
, reshape
,
4307 backup_point
, wait_point
,
4308 &suspend_point
, &reshape_completed
,
4310 /* external metadata would need to ping_monitor here */
4311 sra
->reshape_progress
= reshape_completed
;
4313 /* Clear any backup region that is before 'here' */
4315 if (__le64_to_cpu(bsb
.length
) > 0 &&
4316 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart
) +
4317 __le64_to_cpu(bsb
.length
)))
4318 forget_backup(dests
, destfd
,
4320 if (__le64_to_cpu(bsb
.length2
) > 0 &&
4321 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart2
) +
4322 __le64_to_cpu(bsb
.length2
)))
4323 forget_backup(dests
, destfd
,
4326 if (__le64_to_cpu(bsb
.length
) > 0 &&
4327 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart
)))
4328 forget_backup(dests
, destfd
,
4330 if (__le64_to_cpu(bsb
.length2
) > 0 &&
4331 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart2
)))
4332 forget_backup(dests
, destfd
,
4342 if (rv
== 0 && increasing
&& !st
->ss
->external
) {
4343 /* No longer need to monitor this reshape */
4344 sysfs_set_str(sra
, NULL
, "sync_max", "max");
4350 unsigned long long offset
;
4351 unsigned long actual_stripes
;
4352 /* Need to backup some data.
4353 * If 'part' is not used and the desired
4354 * backup size is suspended, do a backup,
4355 * then consider the next part.
4357 /* Check that 'part' is unused */
4358 if (part
== 0 && __le64_to_cpu(bsb
.length
) != 0)
4360 if (part
== 1 && __le64_to_cpu(bsb
.length2
) != 0)
4363 offset
= backup_point
/ data
;
4364 actual_stripes
= stripes
;
4366 if (offset
+ actual_stripes
* (chunk
/512) >
4367 sra
->component_size
)
4368 actual_stripes
= ((sra
->component_size
- offset
)
4370 if (offset
+ actual_stripes
* (chunk
/512) >
4374 if (offset
< actual_stripes
* (chunk
/512))
4375 actual_stripes
= offset
/ (chunk
/512);
4376 offset
-= actual_stripes
* (chunk
/512);
4377 if (offset
< suspend_point
/data
)
4380 if (actual_stripes
== 0)
4382 grow_backup(sra
, offset
, actual_stripes
,
4384 disks
, chunk
, level
, layout
,
4385 dests
, destfd
, destoffsets
,
4386 part
, °raded
, buf
);
4387 validate(afd
, destfd
[0], destoffsets
[0]);
4388 /* record where 'part' is up to */
4391 backup_point
+= actual_stripes
* (chunk
/512) * data
;
4393 backup_point
-= actual_stripes
* (chunk
/512) * data
;
4397 /* FIXME maybe call progress_reshape one more time instead */
4398 /* remove any remaining suspension */
4399 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
4400 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
4401 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
4402 sysfs_set_num(sra
, NULL
, "sync_min", 0);
4404 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
4405 sysfs_set_num(sra
, NULL
, "sync_speed_min", speed
);
4411 * If any spare contains md_back_data-1 which is recent wrt mtime,
4412 * write that data into the array and update the super blocks with
4413 * the new reshape_progress
4415 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
, int cnt
,
4416 char *backup_file
, int verbose
)
4420 unsigned long long *offsets
;
4421 unsigned long long nstripe
, ostripe
;
4424 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
4425 if (info
->array
.level
== 6) odata
--; /* number of data disks */
4426 ndata
= info
->array
.raid_disks
- 1;
4427 if (info
->new_level
== 6) ndata
--;
4429 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
4431 if (info
->delta_disks
<= 0)
4432 /* Didn't grow, so the backup file must have
4436 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
4437 struct mdinfo dinfo
;
4440 char *devname
, namebuf
[20];
4441 unsigned long long lo
, hi
;
4443 /* This was a spare and may have some saved data on it.
4444 * Load the superblock, find and load the
4445 * backup_super_block.
4446 * If either fail, go on to next device.
4447 * If the backup contains no new info, just return
4448 * else restore data and update all superblocks
4450 if (i
== old_disks
-1) {
4451 fd
= open(backup_file
, O_RDONLY
);
4453 pr_err("backup file %s inaccessible: %s\n",
4454 backup_file
, strerror(errno
));
4457 devname
= backup_file
;
4462 if (st
->ss
->load_super(st
, fd
, NULL
))
4465 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4466 st
->ss
->free_super(st
);
4469 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
4471 pr_err("Cannot seek on device %d\n", i
);
4472 continue; /* Cannot seek */
4474 sprintf(namebuf
, "device-%d", i
);
4477 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
)) {
4479 pr_err("Cannot read from %s\n", devname
);
4480 continue; /* Cannot read */
4482 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0 &&
4483 memcmp(bsb
.magic
, "md_backup_data-2", 16) != 0) {
4485 pr_err("No backup metadata on %s\n", devname
);
4488 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
))) {
4490 pr_err("Bad backup-metadata checksum on %s\n", devname
);
4491 continue; /* bad checksum */
4493 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0 &&
4494 bsb
.sb_csum2
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum2
)-((char*)&bsb
))) {
4496 pr_err("Bad backup-metadata checksum2 on %s\n", devname
);
4497 continue; /* Bad second checksum */
4499 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0) {
4501 pr_err("Wrong uuid on backup-metadata on %s\n", devname
);
4502 continue; /* Wrong uuid */
4505 /* array utime and backup-mtime should be updated at much the same time, but it seems that
4506 * sometimes they aren't... So allow considerable flexability in matching, and allow
4507 * this test to be overridden by an environment variable.
4509 if(time_after(info
->array
.utime
, (unsigned int)__le64_to_cpu(bsb
.mtime
) + 2*60*60) ||
4510 time_before(info
->array
.utime
, (unsigned int)__le64_to_cpu(bsb
.mtime
) - 10*60)) {
4511 if (check_env("MDADM_GROW_ALLOW_OLD")) {
4512 pr_err("accepting backup with timestamp %lu for array with timestamp %lu\n",
4513 (unsigned long)__le64_to_cpu(bsb
.mtime
),
4514 (unsigned long)info
->array
.utime
);
4516 pr_err("too-old timestamp on backup-metadata on %s\n", devname
);
4517 pr_err("If you think it is should be safe, try 'export MDADM_GROW_ALLOW_OLD=1'\n");
4518 continue; /* time stamp is too bad */
4522 if (bsb
.magic
[15] == '1') {
4523 if (bsb
.length
== 0)
4525 if (info
->delta_disks
>= 0) {
4526 /* reshape_progress is increasing */
4527 if (__le64_to_cpu(bsb
.arraystart
)
4528 + __le64_to_cpu(bsb
.length
)
4529 < info
->reshape_progress
) {
4532 pr_err("backup-metadata found on %s but is not needed\n", devname
);
4533 continue; /* No new data here */
4536 /* reshape_progress is decreasing */
4537 if (__le64_to_cpu(bsb
.arraystart
) >=
4538 info
->reshape_progress
)
4539 goto nonew
; /* No new data here */
4542 if (bsb
.length
== 0 && bsb
.length2
== 0)
4544 if (info
->delta_disks
>= 0) {
4545 /* reshape_progress is increasing */
4546 if ((__le64_to_cpu(bsb
.arraystart
)
4547 + __le64_to_cpu(bsb
.length
)
4548 < info
->reshape_progress
)
4550 (__le64_to_cpu(bsb
.arraystart2
)
4551 + __le64_to_cpu(bsb
.length2
)
4552 < info
->reshape_progress
))
4553 goto nonew
; /* No new data here */
4555 /* reshape_progress is decreasing */
4556 if (__le64_to_cpu(bsb
.arraystart
) >=
4557 info
->reshape_progress
&&
4558 __le64_to_cpu(bsb
.arraystart2
) >=
4559 info
->reshape_progress
)
4560 goto nonew
; /* No new data here */
4563 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0) {
4566 pr_err("Failed to verify secondary backup-metadata block on %s\n",
4568 continue; /* Cannot seek */
4570 /* There should be a duplicate backup superblock 4k before here */
4571 if (lseek64(fd
, -4096, 1) < 0 ||
4572 read(fd
, &bsb2
, sizeof(bsb2
)) != sizeof(bsb2
))
4573 goto second_fail
; /* Cannot find leading superblock */
4574 if (bsb
.magic
[15] == '1')
4575 bsbsize
= offsetof(struct mdp_backup_super
, pad1
);
4577 bsbsize
= offsetof(struct mdp_backup_super
, pad
);
4578 if (memcmp(&bsb2
, &bsb
, bsbsize
) != 0)
4579 goto second_fail
; /* Cannot find leading superblock */
4581 /* Now need the data offsets for all devices. */
4582 offsets
= xmalloc(sizeof(*offsets
)*info
->array
.raid_disks
);
4583 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
4586 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
4587 /* FIXME should be this be an error */
4589 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4590 st
->ss
->free_super(st
);
4591 offsets
[j
] = dinfo
.data_offset
* 512;
4593 printf("%s: restoring critical section\n", Name
);
4595 if (restore_stripes(fdlist
, offsets
,
4596 info
->array
.raid_disks
,
4600 fd
, __le64_to_cpu(bsb
.devstart
)*512,
4601 __le64_to_cpu(bsb
.arraystart
)*512,
4602 __le64_to_cpu(bsb
.length
)*512, NULL
)) {
4603 /* didn't succeed, so giveup */
4605 pr_err("Error restoring backup from %s\n",
4611 if (bsb
.magic
[15] == '2' &&
4612 restore_stripes(fdlist
, offsets
,
4613 info
->array
.raid_disks
,
4617 fd
, __le64_to_cpu(bsb
.devstart
)*512 +
4618 __le64_to_cpu(bsb
.devstart2
)*512,
4619 __le64_to_cpu(bsb
.arraystart2
)*512,
4620 __le64_to_cpu(bsb
.length2
)*512, NULL
)) {
4621 /* didn't succeed, so giveup */
4623 pr_err("Error restoring second backup from %s\n",
4631 /* Ok, so the data is restored. Let's update those superblocks. */
4635 lo
= __le64_to_cpu(bsb
.arraystart
);
4636 hi
= lo
+ __le64_to_cpu(bsb
.length
);
4638 if (bsb
.magic
[15] == '2' && bsb
.length2
) {
4639 unsigned long long lo1
, hi1
;
4640 lo1
= __le64_to_cpu(bsb
.arraystart2
);
4641 hi1
= lo1
+ __le64_to_cpu(bsb
.length2
);
4645 } else if (lo
< lo1
)
4651 (info
->reshape_progress
< lo
||
4652 info
->reshape_progress
> hi
))
4653 /* backup does not affect reshape_progress*/ ;
4654 else if (info
->delta_disks
>= 0) {
4655 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
) +
4656 __le64_to_cpu(bsb
.length
);
4657 if (bsb
.magic
[15] == '2') {
4658 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
) +
4659 __le64_to_cpu(bsb
.length2
);
4660 if (p2
> info
->reshape_progress
)
4661 info
->reshape_progress
= p2
;
4664 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
);
4665 if (bsb
.magic
[15] == '2') {
4666 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
);
4667 if (p2
< info
->reshape_progress
)
4668 info
->reshape_progress
= p2
;
4671 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
4674 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
4676 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4677 dinfo
.reshape_progress
= info
->reshape_progress
;
4678 st
->ss
->update_super(st
, &dinfo
,
4679 "_reshape_progress",
4681 st
->ss
->store_super(st
, fdlist
[j
]);
4682 st
->ss
->free_super(st
);
4686 /* Didn't find any backup data, try to see if any
4689 if (info
->delta_disks
< 0) {
4690 /* When shrinking, the critical section is at the end.
4691 * So see if we are before the critical section.
4693 unsigned long long first_block
;
4694 nstripe
= ostripe
= 0;
4696 while (ostripe
>= nstripe
) {
4697 ostripe
+= info
->array
.chunk_size
/ 512;
4698 first_block
= ostripe
* odata
;
4699 nstripe
= first_block
/ ndata
/ (info
->new_chunk
/512) *
4700 (info
->new_chunk
/512);
4703 if (info
->reshape_progress
>= first_block
)
4706 if (info
->delta_disks
> 0) {
4707 /* See if we are beyond the critical section. */
4708 unsigned long long last_block
;
4709 nstripe
= ostripe
= 0;
4711 while (nstripe
>= ostripe
) {
4712 nstripe
+= info
->new_chunk
/ 512;
4713 last_block
= nstripe
* ndata
;
4714 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
4715 (info
->array
.chunk_size
/512);
4718 if (info
->reshape_progress
>= last_block
)
4721 /* needed to recover critical section! */
4723 pr_err("Failed to find backup of critical section\n");
4727 int Grow_continue_command(char *devname
, int fd
,
4728 char *backup_file
, int verbose
)
4731 struct supertype
*st
= NULL
;
4732 struct mdinfo
*content
= NULL
;
4733 struct mdinfo array
;
4734 char *subarray
= NULL
;
4735 struct mdinfo
*cc
= NULL
;
4736 struct mdstat_ent
*mdstat
= NULL
;
4740 dprintf("Grow continue from command line called for %s\n",
4743 st
= super_by_fd(fd
, &subarray
);
4744 if (!st
|| !st
->ss
) {
4745 pr_err("Unable to determine metadata format for %s\n",
4749 dprintf("Grow continue is run for ");
4750 if (st
->ss
->external
== 0) {
4752 dprintf_cont("native array (%s)\n", devname
);
4753 if (ioctl(fd
, GET_ARRAY_INFO
, &array
.array
) < 0) {
4754 pr_err("%s is not an active md array - aborting\n", devname
);
4756 goto Grow_continue_command_exit
;
4759 /* Need to load a superblock.
4760 * FIXME we should really get what we need from
4763 for (d
= 0; d
< MAX_DISKS
; d
++) {
4764 mdu_disk_info_t disk
;
4768 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
4770 if (disk
.major
== 0 && disk
.minor
== 0)
4772 if ((disk
.state
& (1 << MD_DISK_ACTIVE
)) == 0)
4774 dv
= map_dev(disk
.major
, disk
.minor
, 1);
4777 fd2
= dev_open(dv
, O_RDONLY
);
4780 err
= st
->ss
->load_super(st
, fd2
, NULL
);
4782 /* invalidate fd2 to avoid possible double close() */
4788 if (d
== MAX_DISKS
) {
4789 pr_err("Unable to load metadata for %s\n",
4792 goto Grow_continue_command_exit
;
4794 st
->ss
->getinfo_super(st
, content
, NULL
);
4799 dprintf_cont("subarray (%s)\n", subarray
);
4800 container
= st
->container_devnm
;
4801 cfd
= open_dev_excl(st
->container_devnm
);
4803 container
= st
->devnm
;
4805 cfd
= open_dev_excl(st
->devnm
);
4806 dprintf_cont("container (%s)\n", container
);
4810 pr_err("Unable to open container for %s\n", devname
);
4812 goto Grow_continue_command_exit
;
4815 /* find in container array under reshape
4817 ret_val
= st
->ss
->load_container(st
, cfd
, NULL
);
4819 pr_err("Cannot read superblock for %s\n",
4822 goto Grow_continue_command_exit
;
4825 cc
= st
->ss
->container_content(st
, subarray
);
4826 for (content
= cc
; content
; content
= content
->next
) {
4828 int allow_reshape
= 1;
4830 if (content
->reshape_active
== 0)
4832 /* The decision about array or container wide
4833 * reshape is taken in Grow_continue based
4834 * content->reshape_active state, therefore we
4835 * need to check_reshape based on
4836 * reshape_active and subarray name
4838 if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
))
4840 if (content
->reshape_active
== CONTAINER_RESHAPE
&&
4841 (content
->array
.state
4842 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
)))
4845 if (!allow_reshape
) {
4846 pr_err("cannot continue reshape of an array in container with unsupported metadata: %s(%s)\n",
4847 devname
, container
);
4849 goto Grow_continue_command_exit
;
4852 array
= strchr(content
->text_version
+1, '/')+1;
4853 mdstat
= mdstat_by_subdev(array
, container
);
4856 if (mdstat
->active
== 0) {
4857 pr_err("Skipping inactive array %s.\n",
4859 free_mdstat(mdstat
);
4866 pr_err("Unable to determine reshaped array for %s\n", devname
);
4868 goto Grow_continue_command_exit
;
4870 fd2
= open_dev(mdstat
->devnm
);
4872 pr_err("cannot open (%s)\n", mdstat
->devnm
);
4874 goto Grow_continue_command_exit
;
4877 sysfs_init(content
, fd2
, mdstat
->devnm
);
4879 /* start mdmon in case it is not running
4881 if (!mdmon_running(container
))
4882 start_mdmon(container
);
4883 ping_monitor(container
);
4885 if (mdmon_running(container
))
4886 st
->update_tail
= &st
->updates
;
4888 pr_err("No mdmon found. Grow cannot continue.\n");
4890 goto Grow_continue_command_exit
;
4894 /* verify that array under reshape is started from
4897 if (verify_reshape_position(content
, content
->array
.level
) < 0) {
4899 goto Grow_continue_command_exit
;
4904 ret_val
= Grow_continue(fd
, st
, content
, backup_file
, 1, 0);
4906 Grow_continue_command_exit
:
4911 st
->ss
->free_super(st
);
4912 free_mdstat(mdstat
);
4919 int Grow_continue(int mdfd
, struct supertype
*st
, struct mdinfo
*info
,
4920 char *backup_file
, int forked
, int freeze_reshape
)
4924 if (!info
->reshape_active
)
4927 if (st
->ss
->external
) {
4928 int cfd
= open_dev(st
->container_devnm
);
4933 st
->ss
->load_container(st
, cfd
, st
->container_devnm
);
4935 ret_val
= reshape_container(st
->container_devnm
, NULL
, mdfd
,
4936 st
, info
, 0, backup_file
,
4938 1 | info
->reshape_active
,
4941 ret_val
= reshape_array(NULL
, mdfd
, "array", st
, info
, 1,
4942 NULL
, INVALID_SECTORS
,
4943 backup_file
, 0, forked
,
4944 1 | info
->reshape_active
,
4950 char *make_backup(char *name
)
4952 char *base
= "backup_file-";
4956 len
= strlen(MAP_DIR
) + 1 + strlen(base
) + strlen(name
)+1;
4957 fname
= xmalloc(len
);
4958 sprintf(fname
, "%s/%s%s", MAP_DIR
, base
, name
);
4962 char *locate_backup(char *name
)
4964 char *fl
= make_backup(name
);
4967 if (stat(fl
, &stb
) == 0 &&
4968 S_ISREG(stb
.st_mode
))