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
);
1593 if (s
->level
== 0 &&
1594 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)) &&
1595 !(array
.state
& (1<<MD_SB_CLUSTERED
))) {
1596 array
.state
&= ~(1<<MD_SB_BITMAP_PRESENT
);
1597 if (ioctl(fd
, SET_ARRAY_INFO
, &array
)!= 0) {
1598 pr_err("failed to remove internal bitmap.\n");
1603 /* in the external case we need to check that the requested reshape is
1604 * supported, and perform an initial check that the container holds the
1605 * pre-requisite spare devices (mdmon owns final validation)
1607 if (st
->ss
->external
) {
1611 container
= st
->container_devnm
;
1612 cfd
= open_dev_excl(st
->container_devnm
);
1614 container
= st
->devnm
;
1616 cfd
= open_dev_excl(st
->devnm
);
1620 pr_err("Unable to open container for %s\n",
1626 rv
= st
->ss
->load_container(st
, cfd
, NULL
);
1629 pr_err("Cannot read superblock for %s\n",
1635 /* check if operation is supported for metadata handler */
1636 if (st
->ss
->container_content
) {
1637 struct mdinfo
*cc
= NULL
;
1638 struct mdinfo
*content
= NULL
;
1640 cc
= st
->ss
->container_content(st
, subarray
);
1641 for (content
= cc
; content
; content
= content
->next
) {
1642 int allow_reshape
= 1;
1644 /* check if reshape is allowed based on metadata
1645 * indications stored in content.array.status
1647 if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
))
1649 if (content
->array
.state
1650 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
))
1652 if (!allow_reshape
) {
1653 pr_err("cannot reshape arrays in container with unsupported metadata: %s(%s)\n",
1654 devname
, container
);
1662 if (mdmon_running(container
))
1663 st
->update_tail
= &st
->updates
;
1667 for (dv
= devlist
; dv
; dv
= dv
->next
)
1669 if (s
->raiddisks
> array
.raid_disks
&&
1670 array
.spare_disks
+added_disks
< (s
->raiddisks
- array
.raid_disks
) &&
1672 pr_err("Need %d spare%s to avoid degraded array, and only have %d.\n"
1673 " Use --force to over-ride this check.\n",
1674 s
->raiddisks
- array
.raid_disks
,
1675 s
->raiddisks
- array
.raid_disks
== 1 ? "" : "s",
1676 array
.spare_disks
+ added_disks
);
1680 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
| GET_DISKS
| GET_DEVS
1681 | GET_STATE
| GET_VERSION
);
1683 if (st
->ss
->external
&& subarray
== NULL
) {
1684 array
.level
= LEVEL_CONTAINER
;
1685 sra
->array
.level
= LEVEL_CONTAINER
;
1688 pr_err("failed to read sysfs parameters for %s\n",
1692 frozen
= freeze(st
);
1694 /* freeze() already spewed the reason */
1697 } else if (frozen
< 0) {
1698 pr_err("%s is performing resync/recovery and cannot be reshaped\n", devname
);
1703 /* ========= set size =============== */
1704 if (s
->size
> 0 && (s
->size
== MAX_SIZE
|| s
->size
!= (unsigned)array
.size
)) {
1705 unsigned long long orig_size
= get_component_size(fd
)/2;
1706 unsigned long long min_csize
;
1708 int raid0_takeover
= 0;
1711 orig_size
= (unsigned) array
.size
;
1713 if (orig_size
== 0) {
1714 pr_err("Cannot set device size in this type of array.\n");
1719 if (reshape_super(st
, s
->size
, UnSet
, UnSet
, 0, 0, UnSet
, NULL
,
1720 devname
, APPLY_METADATA_CHANGES
, c
->verbose
> 0)) {
1725 if (st
->ss
->external
) {
1726 /* metadata can have size limitation
1727 * update size value according to metadata information
1729 struct mdinfo
*sizeinfo
=
1730 st
->ss
->container_content(st
, subarray
);
1732 unsigned long long new_size
=
1733 sizeinfo
->custom_array_size
/2;
1734 int data_disks
= get_data_disks(
1735 sizeinfo
->array
.level
,
1736 sizeinfo
->array
.layout
,
1737 sizeinfo
->array
.raid_disks
);
1738 new_size
/= data_disks
;
1739 dprintf("Metadata size correction from %llu to %llu (%llu)\n", orig_size
, new_size
,
1740 new_size
* data_disks
);
1742 sysfs_free(sizeinfo
);
1746 /* Update the size of each member device in case
1747 * they have been resized. This will never reduce
1748 * below the current used-size. The "size" attribute
1749 * understands '0' to mean 'max'.
1753 for (mdi
= sra
->devs
; mdi
; mdi
= mdi
->next
) {
1754 if (sysfs_set_num(sra
, mdi
, "size",
1755 s
->size
== MAX_SIZE
? 0 : s
->size
) < 0) {
1756 /* Probably kernel refusing to let us
1757 * reduce the size - not an error.
1761 if (array
.not_persistent
== 0 &&
1762 array
.major_version
== 0 &&
1763 get_linux_version() < 3001000) {
1764 /* Dangerous to allow size to exceed 2TB */
1765 unsigned long long csize
;
1766 if (sysfs_get_ll(sra
, mdi
, "size", &csize
) == 0) {
1767 if (csize
>= 2ULL*1024*1024*1024)
1768 csize
= 2ULL*1024*1024*1024;
1769 if ((min_csize
== 0 || (min_csize
1776 pr_err("Cannot set size on array members.\n");
1777 goto size_change_error
;
1779 if (min_csize
&& s
->size
> min_csize
) {
1780 pr_err("Cannot safely make this array use more than 2TB per device on this kernel.\n");
1782 goto size_change_error
;
1784 if (min_csize
&& s
->size
== MAX_SIZE
) {
1785 /* Don't let the kernel choose a size - it will get
1788 pr_err("Limited v0.90 array to 2TB per device\n");
1789 s
->size
= min_csize
;
1791 if (st
->ss
->external
) {
1792 if (sra
->array
.level
== 0) {
1793 rv
= sysfs_set_str(sra
, NULL
, "level",
1797 /* get array parameters after takeover
1798 * to change one parameter at time only
1800 rv
= ioctl(fd
, GET_ARRAY_INFO
, &array
);
1803 /* make sure mdmon is
1804 * aware of the new level */
1805 if (!mdmon_running(st
->container_devnm
))
1806 start_mdmon(st
->container_devnm
);
1807 ping_monitor(container
);
1808 if (mdmon_running(st
->container_devnm
) &&
1809 st
->update_tail
== NULL
)
1810 st
->update_tail
= &st
->updates
;
1813 if (s
->size
== MAX_SIZE
)
1815 array
.size
= s
->size
;
1816 if (s
->size
& ~INT32_MAX
) {
1817 /* got truncated to 32bit, write to
1818 * component_size instead
1821 rv
= sysfs_set_num(sra
, NULL
,
1822 "component_size", s
->size
);
1826 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
1828 /* manage array size when it is managed externally
1830 if ((rv
== 0) && st
->ss
->external
)
1831 rv
= set_array_size(st
, sra
, sra
->text_version
);
1834 if (raid0_takeover
) {
1835 /* do not recync non-existing parity,
1836 * we will drop it anyway
1838 sysfs_set_str(sra
, NULL
, "sync_action", "frozen");
1839 /* go back to raid0, drop parity disk
1841 sysfs_set_str(sra
, NULL
, "level", "raid0");
1842 ioctl(fd
, GET_ARRAY_INFO
, &array
);
1849 /* restore metadata */
1850 if (reshape_super(st
, orig_size
, UnSet
, UnSet
, 0, 0,
1851 UnSet
, NULL
, devname
,
1852 ROLLBACK_METADATA_CHANGES
,
1855 pr_err("Cannot set device size for %s: %s\n",
1856 devname
, strerror(err
));
1858 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1859 cont_err("Bitmap must be removed before size can be changed\n");
1863 if (s
->assume_clean
) {
1864 /* This will fail on kernels older than 3.0 unless
1865 * a backport has been arranged.
1868 sysfs_set_str(sra
, NULL
, "resync_start", "none") < 0)
1869 pr_err("--assume-clean not supported with --grow on this kernel\n");
1871 ioctl(fd
, GET_ARRAY_INFO
, &array
);
1872 s
->size
= get_component_size(fd
)/2;
1874 s
->size
= array
.size
;
1875 if (c
->verbose
>= 0) {
1876 if (s
->size
== orig_size
)
1877 pr_err("component size of %s unchanged at %lluK\n",
1880 pr_err("component size of %s has been set to %lluK\n",
1884 } else if (array
.level
!= LEVEL_CONTAINER
) {
1885 s
->size
= get_component_size(fd
)/2;
1887 s
->size
= array
.size
;
1890 /* See if there is anything else to do */
1891 if ((s
->level
== UnSet
|| s
->level
== array
.level
) &&
1892 (s
->layout_str
== NULL
) &&
1893 (s
->chunk
== 0 || s
->chunk
== array
.chunk_size
) &&
1894 data_offset
== INVALID_SECTORS
&&
1895 (s
->raiddisks
== 0 || s
->raiddisks
== array
.raid_disks
)) {
1896 /* Nothing more to do */
1897 if (!changed
&& c
->verbose
>= 0)
1898 pr_err("%s: no change requested\n",
1903 /* ========= check for Raid10/Raid1 -> Raid0 conversion ===============
1904 * current implementation assumes that following conditions must be met:
1907 * - near_copies == 2
1909 if ((s
->level
== 0 && array
.level
== 10 && sra
&&
1910 array
.layout
== ((1 << 8) + 2) && !(array
.raid_disks
& 1)) ||
1911 (s
->level
== 0 && array
.level
== 1 && sra
)) {
1913 err
= remove_disks_for_takeover(st
, sra
, array
.layout
);
1915 dprintf("Array cannot be reshaped\n");
1921 /* Make sure mdmon has seen the device removal
1922 * and updated metadata before we continue with
1926 ping_monitor(container
);
1929 memset(&info
, 0, sizeof(info
));
1931 sysfs_init(&info
, fd
, NULL
);
1932 strcpy(info
.text_version
, sra
->text_version
);
1933 info
.component_size
= s
->size
*2;
1934 info
.new_level
= s
->level
;
1935 info
.new_chunk
= s
->chunk
* 1024;
1936 if (info
.array
.level
== LEVEL_CONTAINER
) {
1937 info
.delta_disks
= UnSet
;
1938 info
.array
.raid_disks
= s
->raiddisks
;
1939 } else if (s
->raiddisks
)
1940 info
.delta_disks
= s
->raiddisks
- info
.array
.raid_disks
;
1942 info
.delta_disks
= UnSet
;
1943 if (s
->layout_str
== NULL
) {
1944 info
.new_layout
= UnSet
;
1945 if (info
.array
.level
== 6 &&
1946 (info
.new_level
== 6 || info
.new_level
== UnSet
) &&
1947 info
.array
.layout
>= 16) {
1948 pr_err("%s has a non-standard layout. If you wish to preserve this\n", devname
);
1949 cont_err("during the reshape, please specify --layout=preserve\n");
1950 cont_err("If you want to change it, specify a layout or use --layout=normalise\n");
1954 } else if (strcmp(s
->layout_str
, "normalise") == 0 ||
1955 strcmp(s
->layout_str
, "normalize") == 0) {
1956 /* If we have a -6 RAID6 layout, remove the '-6'. */
1957 info
.new_layout
= UnSet
;
1958 if (info
.array
.level
== 6 && info
.new_level
== UnSet
) {
1960 strcpy(l
, map_num(r6layout
, info
.array
.layout
));
1961 h
= strrchr(l
, '-');
1962 if (h
&& strcmp(h
, "-6") == 0) {
1964 info
.new_layout
= map_name(r6layout
, l
);
1967 pr_err("%s is only meaningful when reshaping a RAID6 array.\n", s
->layout_str
);
1971 } else if (strcmp(s
->layout_str
, "preserve") == 0) {
1972 /* This means that a non-standard RAID6 layout
1975 * - When reshape a RAID6 (e.g. adding a device)
1976 * which is in a non-standard layout, it is OK
1977 * to preserve that layout.
1978 * - When converting a RAID5 to RAID6, leave it in
1979 * the XXX-6 layout, don't re-layout.
1981 if (info
.array
.level
== 6 && info
.new_level
== UnSet
)
1982 info
.new_layout
= info
.array
.layout
;
1983 else if (info
.array
.level
== 5 && info
.new_level
== 6) {
1985 strcpy(l
, map_num(r5layout
, info
.array
.layout
));
1987 info
.new_layout
= map_name(r6layout
, l
);
1989 pr_err("%s in only meaningful when reshaping to RAID6\n", s
->layout_str
);
1994 int l
= info
.new_level
;
1996 l
= info
.array
.level
;
1999 info
.new_layout
= map_name(r5layout
, s
->layout_str
);
2002 info
.new_layout
= map_name(r6layout
, s
->layout_str
);
2005 info
.new_layout
= parse_layout_10(s
->layout_str
);
2008 info
.new_layout
= parse_layout_faulty(s
->layout_str
);
2011 pr_err("layout not meaningful with this level\n");
2015 if (info
.new_layout
== UnSet
) {
2016 pr_err("layout %s not understood for this level\n",
2023 if (array
.level
== LEVEL_FAULTY
) {
2024 if (s
->level
!= UnSet
&& s
->level
!= array
.level
) {
2025 pr_err("cannot change level of Faulty device\n");
2029 pr_err("cannot set chunksize of Faulty device\n");
2032 if (s
->raiddisks
&& s
->raiddisks
!= 1) {
2033 pr_err("cannot set raid_disks of Faulty device\n");
2036 if (s
->layout_str
) {
2037 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
2038 dprintf("Cannot get array information.\n");
2041 array
.layout
= info
.new_layout
;
2042 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2043 pr_err("failed to set new layout\n");
2045 } else if (c
->verbose
>= 0)
2046 printf("layout for %s set to %d\n",
2047 devname
, array
.layout
);
2049 } else if (array
.level
== LEVEL_CONTAINER
) {
2050 /* This change is to be applied to every array in the
2051 * container. This is only needed when the metadata imposes
2052 * restraints of the various arrays in the container.
2053 * Currently we only know that IMSM requires all arrays
2054 * to have the same number of devices so changing the
2055 * number of devices (On-Line Capacity Expansion) must be
2056 * performed at the level of the container
2058 rv
= reshape_container(container
, devname
, -1, st
, &info
,
2059 c
->force
, c
->backup_file
, c
->verbose
, 0, 0, 0);
2062 /* get spare devices from external metadata
2064 if (st
->ss
->external
) {
2065 struct mdinfo
*info2
;
2067 info2
= st
->ss
->container_content(st
, subarray
);
2069 info
.array
.spare_disks
=
2070 info2
->array
.spare_disks
;
2075 /* Impose these changes on a single array. First
2076 * check that the metadata is OK with the change. */
2078 if (reshape_super(st
, 0, info
.new_level
,
2079 info
.new_layout
, info
.new_chunk
,
2080 info
.array
.raid_disks
, info
.delta_disks
,
2081 c
->backup_file
, devname
, APPLY_METADATA_CHANGES
,
2087 rv
= reshape_array(container
, fd
, devname
, st
, &info
, c
->force
,
2088 devlist
, data_offset
, c
->backup_file
, c
->verbose
,
2099 /* verify_reshape_position()
2100 * Function checks if reshape position in metadata is not farther
2101 * than position in md.
2103 * 0 : not valid sysfs entry
2104 * it can be caused by not started reshape, it should be started
2105 * by reshape array or raid0 array is before takeover
2106 * -1 : error, reshape position is obviously wrong
2107 * 1 : success, reshape progress correct or updated
2109 static int verify_reshape_position(struct mdinfo
*info
, int level
)
2115 /* read sync_max, failure can mean raid0 array */
2116 rv
= sysfs_get_str(info
, NULL
, "sync_max", buf
, 40);
2120 unsigned long long position
= strtoull(buf
, &ep
, 0);
2122 dprintf("Read sync_max sysfs entry is: %s\n", buf
);
2123 if (!(ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))) {
2124 position
*= get_data_disks(level
,
2126 info
->array
.raid_disks
);
2127 if (info
->reshape_progress
< position
) {
2128 dprintf("Corrected reshape progress (%llu) to md position (%llu)\n",
2129 info
->reshape_progress
, position
);
2130 info
->reshape_progress
= position
;
2132 } else if (info
->reshape_progress
> position
) {
2133 pr_err("Fatal error: array reshape was not properly frozen (expected reshape position is %llu, but reshape progress is %llu.\n",
2134 position
, info
->reshape_progress
);
2137 dprintf("Reshape position in md and metadata are the same;");
2141 } else if (rv
== 0) {
2142 /* for valid sysfs entry, 0-length content
2143 * should be indicated as error
2151 static unsigned long long choose_offset(unsigned long long lo
,
2152 unsigned long long hi
,
2153 unsigned long long min
,
2154 unsigned long long max
)
2156 /* Choose a new offset between hi and lo.
2157 * It must be between min and max, but
2158 * we would prefer something near the middle of hi/lo, and also
2159 * prefer to be aligned to a big power of 2.
2161 * So we start with the middle, then for each bit,
2162 * starting at '1' and increasing, if it is set, we either
2163 * add it or subtract it if possible, preferring the option
2164 * which is furthest from the boundary.
2166 * We stop once we get a 1MB alignment. As units are in sectors,
2167 * 1MB = 2*1024 sectors.
2169 unsigned long long choice
= (lo
+ hi
) / 2;
2170 unsigned long long bit
= 1;
2172 for (bit
= 1; bit
< 2*1024; bit
= bit
<< 1) {
2173 unsigned long long bigger
, smaller
;
2174 if (! (bit
& choice
))
2176 bigger
= choice
+ bit
;
2177 smaller
= choice
- bit
;
2178 if (bigger
> max
&& smaller
< min
)
2182 else if (smaller
< min
)
2184 else if (hi
- bigger
> smaller
- lo
)
2192 static int set_new_data_offset(struct mdinfo
*sra
, struct supertype
*st
,
2193 char *devname
, int delta_disks
,
2194 unsigned long long data_offset
,
2195 unsigned long long min
,
2201 unsigned long long before
, after
;
2203 /* Need to find min space before and after so same is used
2206 before
= UINT64_MAX
;
2208 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2212 struct supertype
*st2
;
2213 struct mdinfo info2
;
2215 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2217 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2218 dfd
= dev_open(dn
, O_RDONLY
);
2220 pr_err("%s: cannot open component %s\n",
2221 devname
, dn
? dn
: "-unknown-");
2224 st2
= dup_super(st
);
2225 rv
= st2
->ss
->load_super(st2
,dfd
, NULL
);
2229 pr_err("%s: cannot get superblock from %s\n",
2233 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
2234 st2
->ss
->free_super(st2
);
2236 if (info2
.space_before
== 0 &&
2237 info2
.space_after
== 0) {
2238 /* Metadata doesn't support data_offset changes */
2240 pr_err("%s: Metadata version doesn't support data_offset changes\n",
2244 if (before
> info2
.space_before
)
2245 before
= info2
.space_before
;
2246 if (after
> info2
.space_after
)
2247 after
= info2
.space_after
;
2249 if (data_offset
!= INVALID_SECTORS
) {
2251 if (info2
.data_offset
== data_offset
) {
2252 pr_err("%s: already has that data_offset\n",
2256 if (data_offset
< info2
.data_offset
)
2260 } else if ((data_offset
<= info2
.data_offset
&& dir
== 1) ||
2261 (data_offset
>= info2
.data_offset
&& dir
== -1)) {
2262 pr_err("%s: differing data offsets on devices make this --data-offset setting impossible\n",
2268 if (before
== UINT64_MAX
)
2269 /* impossible really, there must be no devices */
2272 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2273 char *dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2274 unsigned long long new_data_offset
;
2276 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2278 if (delta_disks
< 0) {
2279 /* Don't need any space as array is shrinking
2280 * just move data_offset up by min
2282 if (data_offset
== INVALID_SECTORS
)
2283 new_data_offset
= sd
->data_offset
+ min
;
2285 if (data_offset
< sd
->data_offset
+ min
) {
2286 pr_err("--data-offset too small for %s\n",
2290 new_data_offset
= data_offset
;
2292 } else if (delta_disks
> 0) {
2293 /* need space before */
2297 pr_err("Insufficient head-space for reshape on %s\n",
2301 if (data_offset
== INVALID_SECTORS
)
2302 new_data_offset
= sd
->data_offset
- min
;
2304 if (data_offset
> sd
->data_offset
- min
) {
2305 pr_err("--data-offset too large for %s\n",
2309 new_data_offset
= data_offset
;
2313 /* can move up or down. If 'data_offset'
2314 * was set we would have already decided,
2315 * so just choose direction with most space.
2322 sysfs_set_str(sra
, NULL
, "reshape_direction",
2323 dir
== 1 ? "backwards" : "forwards");
2325 /* Increase data offset */
2329 pr_err("Insufficient tail-space for reshape on %s\n",
2333 if (data_offset
!= INVALID_SECTORS
&&
2334 data_offset
< sd
->data_offset
+ min
) {
2335 pr_err("--data-offset too small on %s\n",
2339 if (data_offset
!= INVALID_SECTORS
)
2340 new_data_offset
= data_offset
;
2342 new_data_offset
= choose_offset(sd
->data_offset
,
2343 sd
->data_offset
+ after
,
2344 sd
->data_offset
+ min
,
2345 sd
->data_offset
+ after
);
2347 /* Decrease data offset */
2351 pr_err("insufficient head-room on %s\n",
2355 if (data_offset
!= INVALID_SECTORS
&&
2356 data_offset
< sd
->data_offset
- min
) {
2357 pr_err("--data-offset too small on %s\n",
2361 if (data_offset
!= INVALID_SECTORS
)
2362 new_data_offset
= data_offset
;
2364 new_data_offset
= choose_offset(sd
->data_offset
- before
,
2366 sd
->data_offset
- before
,
2367 sd
->data_offset
- min
);
2370 err
= sysfs_set_num(sra
, sd
, "new_offset", new_data_offset
);
2371 if (err
< 0 && errno
== E2BIG
) {
2372 /* try again after increasing data size to max */
2373 err
= sysfs_set_num(sra
, sd
, "size", 0);
2374 if (err
< 0 && errno
== EINVAL
&&
2375 !(sd
->disk
.state
& (1<<MD_DISK_SYNC
))) {
2376 /* some kernels have a bug where you cannot
2377 * use '0' on spare devices. */
2378 sysfs_set_num(sra
, sd
, "size",
2379 (sra
->component_size
+ after
)/2);
2381 err
= sysfs_set_num(sra
, sd
, "new_offset",
2385 if (errno
== E2BIG
&& data_offset
!= INVALID_SECTORS
) {
2386 pr_err("data-offset is too big for %s\n",
2390 if (sd
== sra
->devs
&&
2391 (errno
== ENOENT
|| errno
== E2BIG
))
2392 /* Early kernel, no 'new_offset' file,
2393 * or kernel doesn't like us.
2394 * For RAID5/6 this is not fatal
2397 pr_err("Cannot set new_offset for %s\n",
2406 /* Just use a backup file */
2410 static int raid10_reshape(char *container
, int fd
, char *devname
,
2411 struct supertype
*st
, struct mdinfo
*info
,
2412 struct reshape
*reshape
,
2413 unsigned long long data_offset
,
2414 int force
, int verbose
)
2416 /* Changing raid_disks, layout, chunksize or possibly
2417 * just data_offset for a RAID10.
2418 * We must always change data_offset. We change by at least
2419 * ->min_offset_change which is the largest of the old and new
2421 * If raid_disks is increasing, then data_offset must decrease
2422 * by at least this copy size.
2423 * If raid_disks is unchanged, data_offset must increase or
2424 * decrease by at least min_offset_change but preferably by much more.
2425 * We choose half of the available space.
2426 * If raid_disks is decreasing, data_offset must increase by
2427 * at least min_offset_change. To allow of this, component_size
2428 * must be decreased by the same amount.
2430 * So we calculate the required minimum and direction, possibly
2431 * reduce the component_size, then iterate through the devices
2432 * and set the new_data_offset.
2433 * If that all works, we set chunk_size, layout, raid_disks, and start
2437 unsigned long long min
;
2440 sra
= sysfs_read(fd
, NULL
,
2441 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
2444 pr_err("%s: Cannot get array details from sysfs\n",
2448 min
= reshape
->min_offset_change
;
2450 if (info
->delta_disks
)
2451 sysfs_set_str(sra
, NULL
, "reshape_direction",
2452 info
->delta_disks
< 0 ? "backwards" : "forwards");
2453 if (info
->delta_disks
< 0 &&
2454 info
->space_after
< min
) {
2455 int rv
= sysfs_set_num(sra
, NULL
, "component_size",
2456 (sra
->component_size
-
2459 pr_err("cannot reduce component size\n");
2463 err
= set_new_data_offset(sra
, st
, devname
, info
->delta_disks
, data_offset
,
2466 pr_err("Cannot set new_data_offset: RAID10 reshape not\n");
2467 cont_err("supported on this kernel\n");
2473 if (!err
&& sysfs_set_num(sra
, NULL
, "chunk_size", info
->new_chunk
) < 0)
2475 if (!err
&& sysfs_set_num(sra
, NULL
, "layout", reshape
->after
.layout
) < 0)
2477 if (!err
&& sysfs_set_num(sra
, NULL
, "raid_disks",
2478 info
->array
.raid_disks
+ info
->delta_disks
) < 0)
2480 if (!err
&& sysfs_set_str(sra
, NULL
, "sync_action", "reshape") < 0)
2483 pr_err("Cannot set array shape for %s\n",
2486 (info
->array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2487 cont_err(" Bitmap must be removed before shape can be changed\n");
2497 static void get_space_after(int fd
, struct supertype
*st
, struct mdinfo
*info
)
2499 struct mdinfo
*sra
, *sd
;
2500 /* Initialisation to silence compiler warning */
2501 unsigned long long min_space_before
= 0, min_space_after
= 0;
2504 sra
= sysfs_read(fd
, NULL
, GET_DEVS
);
2507 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2510 struct supertype
*st2
;
2511 struct mdinfo info2
;
2513 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2515 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2516 dfd
= dev_open(dn
, O_RDONLY
);
2519 st2
= dup_super(st
);
2520 if (st2
->ss
->load_super(st2
,dfd
, NULL
)) {
2526 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
2527 st2
->ss
->free_super(st2
);
2530 min_space_before
> info2
.space_before
)
2531 min_space_before
= info2
.space_before
;
2533 min_space_after
> info2
.space_after
)
2534 min_space_after
= info2
.space_after
;
2537 if (sd
== NULL
&& !first
) {
2538 info
->space_after
= min_space_after
;
2539 info
->space_before
= min_space_before
;
2544 static void update_cache_size(char *container
, struct mdinfo
*sra
,
2545 struct mdinfo
*info
,
2546 int disks
, unsigned long long blocks
)
2548 /* Check that the internal stripe cache is
2549 * large enough, or it won't work.
2550 * It must hold at least 4 stripes of the larger
2553 unsigned long cache
;
2554 cache
= max(info
->array
.chunk_size
, info
->new_chunk
);
2555 cache
*= 4; /* 4 stripes minimum */
2556 cache
/= 512; /* convert to sectors */
2557 /* make sure there is room for 'blocks' with a bit to spare */
2558 if (cache
< 16 + blocks
/ disks
)
2559 cache
= 16 + blocks
/ disks
;
2560 cache
/= (4096/512); /* Convert from sectors to pages */
2562 if (sra
->cache_size
< cache
)
2563 subarray_set_num(container
, sra
, "stripe_cache_size",
2567 static int impose_reshape(struct mdinfo
*sra
,
2568 struct mdinfo
*info
,
2569 struct supertype
*st
,
2572 char *devname
, char *container
,
2573 struct reshape
*reshape
)
2575 struct mdu_array_info_s array
;
2577 sra
->new_chunk
= info
->new_chunk
;
2580 /* for external metadata checkpoint saved by mdmon can be lost
2581 * or missed /due to e.g. crash/. Check if md is not during
2582 * restart farther than metadata points to.
2583 * If so, this means metadata information is obsolete.
2585 if (st
->ss
->external
)
2586 verify_reshape_position(info
, reshape
->level
);
2587 sra
->reshape_progress
= info
->reshape_progress
;
2589 sra
->reshape_progress
= 0;
2590 if (reshape
->after
.data_disks
< reshape
->before
.data_disks
)
2591 /* start from the end of the new array */
2592 sra
->reshape_progress
= (sra
->component_size
2593 * reshape
->after
.data_disks
);
2596 ioctl(fd
, GET_ARRAY_INFO
, &array
);
2597 if (info
->array
.chunk_size
== info
->new_chunk
&&
2598 reshape
->before
.layout
== reshape
->after
.layout
&&
2599 st
->ss
->external
== 0) {
2600 /* use SET_ARRAY_INFO but only if reshape hasn't started */
2601 array
.raid_disks
= reshape
->after
.data_disks
+ reshape
->parity
;
2603 ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2606 pr_err("Cannot set device shape for %s: %s\n",
2607 devname
, strerror(errno
));
2610 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2611 cont_err("Bitmap must be removed before shape can be changed\n");
2615 } else if (!restart
) {
2616 /* set them all just in case some old 'new_*' value
2617 * persists from some earlier problem.
2620 if (sysfs_set_num(sra
, NULL
, "chunk_size", info
->new_chunk
) < 0)
2622 if (!err
&& sysfs_set_num(sra
, NULL
, "layout",
2623 reshape
->after
.layout
) < 0)
2625 if (!err
&& subarray_set_num(container
, sra
, "raid_disks",
2626 reshape
->after
.data_disks
+
2627 reshape
->parity
) < 0)
2630 pr_err("Cannot set device shape for %s\n",
2634 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2635 cont_err("Bitmap must be removed before shape can be changed\n");
2644 static int impose_level(int fd
, int level
, char *devname
, int verbose
)
2647 struct mdu_array_info_s array
;
2649 sysfs_init(&info
, fd
, NULL
);
2651 ioctl(fd
, GET_ARRAY_INFO
, &array
);
2653 (array
.level
>= 4 && array
.level
<= 6)) {
2654 /* To convert to RAID0 we need to fail and
2655 * remove any non-data devices. */
2658 int data_disks
= array
.raid_disks
- 1;
2659 if (array
.level
== 6)
2661 if (array
.level
== 5 &&
2662 array
.layout
!= ALGORITHM_PARITY_N
)
2664 if (array
.level
== 6 &&
2665 array
.layout
!= ALGORITHM_PARITY_N_6
)
2667 sysfs_set_str(&info
, NULL
,"sync_action", "idle");
2668 /* First remove any spares so no recovery starts */
2669 for (d
= 0, found
= 0;
2670 d
< MAX_DISKS
&& found
< array
.nr_disks
;
2672 mdu_disk_info_t disk
;
2674 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
2676 if (disk
.major
== 0 && disk
.minor
== 0)
2679 if ((disk
.state
& (1 << MD_DISK_ACTIVE
))
2680 && disk
.raid_disk
< data_disks
)
2683 ioctl(fd
, HOT_REMOVE_DISK
,
2684 makedev(disk
.major
, disk
.minor
));
2686 /* Now fail anything left */
2687 ioctl(fd
, GET_ARRAY_INFO
, &array
);
2688 for (d
= 0, found
= 0;
2689 d
< MAX_DISKS
&& found
< array
.nr_disks
;
2692 mdu_disk_info_t disk
;
2694 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
2696 if (disk
.major
== 0 && disk
.minor
== 0)
2699 if ((disk
.state
& (1 << MD_DISK_ACTIVE
))
2700 && disk
.raid_disk
< data_disks
)
2703 ioctl(fd
, SET_DISK_FAULTY
,
2704 makedev(disk
.major
, disk
.minor
));
2706 while (ioctl(fd
, HOT_REMOVE_DISK
,
2707 makedev(disk
.major
, disk
.minor
)) < 0
2714 c
= map_num(pers
, level
);
2716 int err
= sysfs_set_str(&info
, NULL
, "level", c
);
2719 pr_err("%s: could not set level to %s\n",
2722 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2723 cont_err("Bitmap must be removed before level can be changed\n");
2727 pr_err("level of %s changed to %s\n",
2734 static void catch_term(int sig
)
2739 static int continue_via_systemd(char *devnm
)
2741 int skipped
, i
, pid
, status
;
2743 /* In a systemd/udev world, it is best to get systemd to
2744 * run "mdadm --grow --continue" rather than running in the
2749 /* FIXME yuk. CLOSE_EXEC?? */
2751 for (i
= 3; skipped
< 20; i
++)
2757 /* Don't want to see error messages from
2758 * systemctl. If the service doesn't exist,
2759 * we fork ourselves.
2762 open("/dev/null", O_WRONLY
);
2763 snprintf(pathbuf
, sizeof(pathbuf
), "mdadm-grow-continue@%s.service",
2765 status
= execl("/usr/bin/systemctl", "systemctl",
2768 status
= execl("/bin/systemctl", "systemctl", "start",
2771 case -1: /* Just do it ourselves. */
2773 default: /* parent - good */
2774 pid
= wait(&status
);
2775 if (pid
>= 0 && status
== 0)
2781 static int reshape_array(char *container
, int fd
, char *devname
,
2782 struct supertype
*st
, struct mdinfo
*info
,
2783 int force
, struct mddev_dev
*devlist
,
2784 unsigned long long data_offset
,
2785 char *backup_file
, int verbose
, int forked
,
2786 int restart
, int freeze_reshape
)
2788 struct reshape reshape
;
2791 int orig_level
= UnSet
;
2795 struct mdu_array_info_s array
;
2798 struct mddev_dev
*dv
;
2802 unsigned long long *offsets
= NULL
;
2806 unsigned long blocks
;
2807 unsigned long long array_size
;
2809 struct mdinfo
*sra
= NULL
;
2812 /* when reshaping a RAID0, the component_size might be zero.
2813 * So try to fix that up.
2815 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
2816 dprintf("Cannot get array information.\n");
2819 if (array
.level
== 0 && info
->component_size
== 0) {
2820 get_dev_size(fd
, NULL
, &array_size
);
2821 info
->component_size
= array_size
/ array
.raid_disks
;
2824 if (array
.level
== 10)
2825 /* Need space_after info */
2826 get_space_after(fd
, st
, info
);
2828 if (info
->reshape_active
) {
2829 int new_level
= info
->new_level
;
2830 info
->new_level
= UnSet
;
2831 if (info
->delta_disks
> 0)
2832 info
->array
.raid_disks
-= info
->delta_disks
;
2833 msg
= analyse_change(devname
, info
, &reshape
);
2834 info
->new_level
= new_level
;
2835 if (info
->delta_disks
> 0)
2836 info
->array
.raid_disks
+= info
->delta_disks
;
2838 /* Make sure the array isn't read-only */
2839 ioctl(fd
, RESTART_ARRAY_RW
, 0);
2841 msg
= analyse_change(devname
, info
, &reshape
);
2843 /* if msg == "", error has already been printed */
2845 pr_err("%s\n", msg
);
2849 (reshape
.level
!= info
->array
.level
||
2850 reshape
.before
.layout
!= info
->array
.layout
||
2851 reshape
.before
.data_disks
+ reshape
.parity
2852 != info
->array
.raid_disks
- max(0, info
->delta_disks
))) {
2853 pr_err("reshape info is not in native format - cannot continue.\n");
2857 if (st
->ss
->external
&& restart
&& (info
->reshape_progress
== 0) &&
2858 !((sysfs_get_str(info
, NULL
, "sync_action", buf
, sizeof(buf
)) > 0) &&
2859 (strncmp(buf
, "reshape", 7) == 0))) {
2860 /* When reshape is restarted from '0', very begin of array
2861 * it is possible that for external metadata reshape and array
2862 * configuration doesn't happen.
2863 * Check if md has the same opinion, and reshape is restarted
2864 * from 0. If so, this is regular reshape start after reshape
2865 * switch in metadata to next array only.
2867 if ((verify_reshape_position(info
, reshape
.level
) >= 0) &&
2868 (info
->reshape_progress
== 0))
2872 /* reshape already started. just skip to monitoring the reshape */
2873 if (reshape
.backup_blocks
== 0)
2875 if (restart
& RESHAPE_NO_BACKUP
)
2878 /* Need 'sra' down at 'started:' */
2879 sra
= sysfs_read(fd
, NULL
,
2880 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
|
2883 pr_err("%s: Cannot get array details from sysfs\n",
2889 backup_file
= locate_backup(sra
->sys_name
);
2893 /* The container is frozen but the array may not be.
2894 * So freeze the array so spares don't get put to the wrong use
2895 * FIXME there should probably be a cleaner separation between
2896 * freeze_array and freeze_container.
2898 sysfs_freeze_array(info
);
2899 /* Check we have enough spares to not be degraded */
2901 for (dv
= devlist
; dv
; dv
=dv
->next
)
2903 spares_needed
= max(reshape
.before
.data_disks
,
2904 reshape
.after
.data_disks
)
2905 + reshape
.parity
- array
.raid_disks
;
2908 info
->new_level
> 1 && info
->array
.level
> 1 &&
2909 spares_needed
> info
->array
.spare_disks
+ added_disks
) {
2910 pr_err("Need %d spare%s to avoid degraded array, and only have %d.\n"
2911 " Use --force to over-ride this check.\n",
2913 spares_needed
== 1 ? "" : "s",
2914 info
->array
.spare_disks
+ added_disks
);
2917 /* Check we have enough spares to not fail */
2918 spares_needed
= max(reshape
.before
.data_disks
,
2919 reshape
.after
.data_disks
)
2921 if ((info
->new_level
> 1 || info
->new_level
== 0) &&
2922 spares_needed
> info
->array
.spare_disks
+added_disks
) {
2923 pr_err("Need %d spare%s to create working array, and only have %d.\n",
2925 spares_needed
== 1 ? "" : "s",
2926 info
->array
.spare_disks
+ added_disks
);
2930 if (reshape
.level
!= array
.level
) {
2931 int err
= impose_level(fd
, reshape
.level
, devname
, verbose
);
2934 info
->new_layout
= UnSet
; /* after level change,
2935 * layout is meaningless */
2936 orig_level
= array
.level
;
2937 sysfs_freeze_array(info
);
2939 if (reshape
.level
> 0 && st
->ss
->external
) {
2940 /* make sure mdmon is aware of the new level */
2941 if (mdmon_running(container
))
2942 flush_mdmon(container
);
2944 if (!mdmon_running(container
))
2945 start_mdmon(container
);
2946 ping_monitor(container
);
2947 if (mdmon_running(container
) &&
2948 st
->update_tail
== NULL
)
2949 st
->update_tail
= &st
->updates
;
2952 /* ->reshape_super might have chosen some spares from the
2953 * container that it wants to be part of the new array.
2954 * We can collect them with ->container_content and give
2955 * them to the kernel.
2957 if (st
->ss
->reshape_super
&& st
->ss
->container_content
) {
2958 char *subarray
= strchr(info
->text_version
+1, '/')+1;
2959 struct mdinfo
*info2
=
2960 st
->ss
->container_content(st
, subarray
);
2964 sysfs_init(info2
, fd
, st
->devnm
);
2965 /* When increasing number of devices, we need to set
2966 * new raid_disks before adding these, or they might
2969 if (reshape
.backup_blocks
&&
2970 reshape
.after
.data_disks
> reshape
.before
.data_disks
)
2971 subarray_set_num(container
, info2
, "raid_disks",
2972 reshape
.after
.data_disks
+
2974 for (d
= info2
->devs
; d
; d
= d
->next
) {
2975 if (d
->disk
.state
== 0 &&
2976 d
->disk
.raid_disk
>= 0) {
2977 /* This is a spare that wants to
2978 * be part of the array.
2980 add_disk(fd
, st
, info2
, d
);
2986 /* We might have been given some devices to add to the
2987 * array. Now that the array has been changed to the right
2988 * level and frozen, we can safely add them.
2991 Manage_subdevs(devname
, fd
, devlist
, verbose
,
2994 if (reshape
.backup_blocks
== 0 && data_offset
!= INVALID_SECTORS
)
2995 reshape
.backup_blocks
= reshape
.before
.data_disks
* info
->array
.chunk_size
/512;
2996 if (reshape
.backup_blocks
== 0) {
2997 /* No restriping needed, but we might need to impose
2998 * some more changes: layout, raid_disks, chunk_size
3000 /* read current array info */
3001 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
3002 dprintf("Cannot get array information.\n");
3005 /* compare current array info with new values and if
3006 * it is different update them to new */
3007 if (info
->new_layout
!= UnSet
&&
3008 info
->new_layout
!= array
.layout
) {
3009 array
.layout
= info
->new_layout
;
3010 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
3011 pr_err("failed to set new layout\n");
3013 } else if (verbose
>= 0)
3014 printf("layout for %s set to %d\n",
3015 devname
, array
.layout
);
3017 if (info
->delta_disks
!= UnSet
&&
3018 info
->delta_disks
!= 0 &&
3019 array
.raid_disks
!= (info
->array
.raid_disks
+ info
->delta_disks
)) {
3020 array
.raid_disks
+= info
->delta_disks
;
3021 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
3022 pr_err("failed to set raid disks\n");
3024 } else if (verbose
>= 0) {
3025 printf("raid_disks for %s set to %d\n",
3026 devname
, array
.raid_disks
);
3029 if (info
->new_chunk
!= 0 &&
3030 info
->new_chunk
!= array
.chunk_size
) {
3031 if (sysfs_set_num(info
, NULL
,
3032 "chunk_size", info
->new_chunk
) != 0) {
3033 pr_err("failed to set chunk size\n");
3035 } else if (verbose
>= 0)
3036 printf("chunk size for %s set to %d\n",
3037 devname
, array
.chunk_size
);
3044 * There are three possibilities.
3045 * 1/ The array will shrink.
3046 * We need to ensure the reshape will pause before reaching
3047 * the 'critical section'. We also need to fork and wait for
3048 * that to happen. When it does we
3049 * suspend/backup/complete/unfreeze
3051 * 2/ The array will not change size.
3052 * This requires that we keep a backup of a sliding window
3053 * so that we can restore data after a crash. So we need
3054 * to fork and monitor progress.
3055 * In future we will allow the data_offset to change, so
3056 * a sliding backup becomes unnecessary.
3058 * 3/ The array will grow. This is relatively easy.
3059 * However the kernel's restripe routines will cheerfully
3060 * overwrite some early data before it is safe. So we
3061 * need to make a backup of the early parts of the array
3062 * and be ready to restore it if rebuild aborts very early.
3063 * For externally managed metadata, we still need a forked
3064 * child to monitor the reshape and suspend IO over the region
3065 * that is being reshaped.
3067 * We backup data by writing it to one spare, or to a
3068 * file which was given on command line.
3070 * In each case, we first make sure that storage is available
3071 * for the required backup.
3073 * - request the shape change.
3074 * - fork to handle backup etc.
3076 /* Check that we can hold all the data */
3077 get_dev_size(fd
, NULL
, &array_size
);
3078 if (reshape
.new_size
< (array_size
/512)) {
3079 pr_err("this change will reduce the size of the array.\n"
3080 " use --grow --array-size first to truncate array.\n"
3081 " e.g. mdadm --grow %s --array-size %llu\n",
3082 devname
, reshape
.new_size
/2);
3086 if (array
.level
== 10) {
3087 /* Reshaping RAID10 does not require any data backup by
3088 * user-space. Instead it requires that the data_offset
3089 * is changed to avoid the need for backup.
3090 * So this is handled very separately
3093 /* Nothing to do. */
3095 return raid10_reshape(container
, fd
, devname
, st
, info
,
3096 &reshape
, data_offset
,
3099 sra
= sysfs_read(fd
, NULL
,
3100 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
|
3103 pr_err("%s: Cannot get array details from sysfs\n",
3109 switch(set_new_data_offset(sra
, st
, devname
,
3110 reshape
.after
.data_disks
- reshape
.before
.data_disks
,
3112 reshape
.min_offset_change
, 1)) {
3116 /* Updated data_offset, so it's easy now */
3117 update_cache_size(container
, sra
, info
,
3118 min(reshape
.before
.data_disks
,
3119 reshape
.after
.data_disks
),
3120 reshape
.backup_blocks
);
3122 /* Right, everything seems fine. Let's kick things off.
3126 if (impose_reshape(sra
, info
, st
, fd
, restart
,
3127 devname
, container
, &reshape
) < 0)
3129 if (sysfs_set_str(sra
, NULL
, "sync_action", "reshape") < 0) {
3131 if (errno
!= EINVAL
) {
3132 pr_err("Failed to initiate reshape!\n");
3135 /* revert data_offset and try the old way */
3136 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
3137 sysfs_set_num(sra
, sd
, "new_offset",
3139 sysfs_set_str(sra
, NULL
, "reshape_direction",
3144 if (info
->new_level
== reshape
.level
)
3146 /* need to adjust level when reshape completes */
3148 case -1: /* ignore error, but don't wait */
3150 default: /* parent */
3158 fd
= open_dev(sra
->sys_name
);
3160 impose_level(fd
, info
->new_level
, devname
, verbose
);
3162 case 1: /* Couldn't set data_offset, try the old way */
3163 if (data_offset
!= INVALID_SECTORS
) {
3164 pr_err("Cannot update data_offset on this array\n");
3171 /* Decide how many blocks (sectors) for a reshape
3172 * unit. The number we have so far is just a minimum
3174 blocks
= reshape
.backup_blocks
;
3175 if (reshape
.before
.data_disks
==
3176 reshape
.after
.data_disks
) {
3177 /* Make 'blocks' bigger for better throughput, but
3178 * not so big that we reject it below.
3179 * Try for 16 megabytes
3181 while (blocks
* 32 < sra
->component_size
&&
3185 pr_err("Need to backup %luK of critical section..\n", blocks
/2);
3187 if (blocks
>= sra
->component_size
/2) {
3188 pr_err("%s: Something wrong - reshape aborted\n",
3193 /* Now we need to open all these devices so we can read/write.
3195 nrdisks
= max(reshape
.before
.data_disks
,
3196 reshape
.after
.data_disks
) + reshape
.parity
3197 + sra
->array
.spare_disks
;
3198 fdlist
= xcalloc((1+nrdisks
), sizeof(int));
3199 offsets
= xcalloc((1+nrdisks
), sizeof(offsets
[0]));
3201 odisks
= reshape
.before
.data_disks
+ reshape
.parity
;
3202 d
= reshape_prepare_fdlist(devname
, sra
, odisks
,
3203 nrdisks
, blocks
, backup_file
,
3208 if ((st
->ss
->manage_reshape
== NULL
) ||
3209 (st
->ss
->recover_backup
== NULL
)) {
3210 if (backup_file
== NULL
) {
3211 if (reshape
.after
.data_disks
<=
3212 reshape
.before
.data_disks
) {
3213 pr_err("%s: Cannot grow - need backup-file\n",
3215 pr_err(" Please provide one with \"--backup=...\"\n");
3217 } else if (d
== odisks
) {
3218 pr_err("%s: Cannot grow - need a spare or backup-file to backup critical section\n", devname
);
3222 if (!reshape_open_backup_file(backup_file
, fd
, devname
,
3224 fdlist
+d
, offsets
+d
,
3233 update_cache_size(container
, sra
, info
,
3234 min(reshape
.before
.data_disks
, reshape
.after
.data_disks
),
3237 /* Right, everything seems fine. Let's kick things off.
3238 * If only changing raid_disks, use ioctl, else use
3243 if (impose_reshape(sra
, info
, st
, fd
, restart
,
3244 devname
, container
, &reshape
) < 0)
3247 err
= start_reshape(sra
, restart
, reshape
.before
.data_disks
,
3248 reshape
.after
.data_disks
);
3250 pr_err("Cannot %s reshape for %s\n",
3251 restart
? "continue" : "start",
3256 sysfs_set_str(sra
, NULL
, "array_state", "active");
3257 if (freeze_reshape
) {
3261 pr_err("Reshape has to be continued from location %llu when root filesystem has been mounted.\n",
3262 sra
->reshape_progress
);
3266 if (!forked
&& !check_env("MDADM_NO_SYSTEMCTL"))
3267 if (continue_via_systemd(container
?: sra
->sys_name
)) {
3274 /* Now we just need to kick off the reshape and watch, while
3275 * handling backups of the data...
3276 * This is all done by a forked background process.
3278 switch(forked
? 0 : fork()) {
3280 pr_err("Cannot run child to monitor reshape: %s\n",
3294 /* If another array on the same devices is busy, the
3295 * reshape will wait for them. This would mean that
3296 * the first section that we suspend will stay suspended
3297 * for a long time. So check on that possibility
3298 * by looking for "DELAYED" in /proc/mdstat, and if found,
3302 struct mdstat_ent
*mds
, *m
;
3304 mds
= mdstat_read(1, 0);
3305 for (m
= mds
; m
; m
= m
->next
)
3306 if (strcmp(m
->devnm
, sra
->sys_name
) == 0) {
3308 m
->percent
== RESYNC_DELAYED
)
3311 /* Haven't started the reshape thread
3318 if (delayed
== 1 && get_linux_version() < 3007000) {
3319 pr_err("Reshape is delayed, but cannot wait carefully with this kernel.\n"
3320 " You might experience problems until other reshapes complete.\n");
3324 mdstat_wait(30 - (delayed
-1) * 25);
3328 if (check_env("MDADM_GROW_VERIFY"))
3329 fd
= open(devname
, O_RDONLY
| O_DIRECT
);
3332 mlockall(MCL_FUTURE
);
3334 signal(SIGTERM
, catch_term
);
3336 if (st
->ss
->external
) {
3337 /* metadata handler takes it from here */
3338 done
= st
->ss
->manage_reshape(
3339 fd
, sra
, &reshape
, st
, blocks
,
3341 d
- odisks
, fdlist
+odisks
,
3344 done
= child_monitor(
3345 fd
, sra
, &reshape
, st
, blocks
,
3347 d
- odisks
, fdlist
+odisks
,
3353 if (backup_file
&& done
) {
3355 bul
= make_backup(sra
->sys_name
);
3358 int l
= readlink(bul
, buf
, sizeof(buf
) - 1);
3366 unlink(backup_file
);
3373 if (!st
->ss
->external
&&
3374 !(reshape
.before
.data_disks
!= reshape
.after
.data_disks
3375 && info
->custom_array_size
) &&
3376 info
->new_level
== reshape
.level
&&
3378 /* no need to wait for the reshape to finish as
3379 * there is nothing more to do.
3386 if (st
->ss
->external
) {
3387 /* Re-load the metadata as much could have changed */
3388 int cfd
= open_dev(st
->container_devnm
);
3390 flush_mdmon(container
);
3391 st
->ss
->free_super(st
);
3392 st
->ss
->load_container(st
, cfd
, container
);
3397 /* set new array size if required customer_array_size is used
3400 if (reshape
.before
.data_disks
!=
3401 reshape
.after
.data_disks
&&
3402 info
->custom_array_size
)
3403 set_array_size(st
, info
, info
->text_version
);
3405 if (info
->new_level
!= reshape
.level
) {
3407 fd
= open(devname
, O_RDONLY
);
3408 impose_level(fd
, info
->new_level
, devname
, verbose
);
3410 if (info
->new_level
== 0)
3411 st
->update_tail
= NULL
;
3423 if (orig_level
!= UnSet
&& sra
) {
3424 c
= map_num(pers
, orig_level
);
3425 if (c
&& sysfs_set_str(sra
, NULL
, "level", c
) == 0)
3426 pr_err("aborting level change\n");
3434 /* mdfd handle is passed to be closed in child process (after fork).
3436 int reshape_container(char *container
, char *devname
,
3438 struct supertype
*st
,
3439 struct mdinfo
*info
,
3441 char *backup_file
, int verbose
,
3442 int forked
, int restart
, int freeze_reshape
)
3444 struct mdinfo
*cc
= NULL
;
3446 char last_devnm
[32] = "";
3448 /* component_size is not meaningful for a container,
3449 * so pass '0' meaning 'no change'
3452 reshape_super(st
, 0, info
->new_level
,
3453 info
->new_layout
, info
->new_chunk
,
3454 info
->array
.raid_disks
, info
->delta_disks
,
3455 backup_file
, devname
, APPLY_METADATA_CHANGES
,
3463 /* ping monitor to be sure that update is on disk
3465 ping_monitor(container
);
3467 if (!forked
&& !freeze_reshape
&& !check_env("MDADM_NO_SYSTEMCTL"))
3468 if (continue_via_systemd(container
))
3471 switch (forked
? 0 : fork()) {
3472 case -1: /* error */
3473 perror("Cannot fork to complete reshape\n");
3476 default: /* parent */
3477 if (!freeze_reshape
)
3478 printf("%s: multi-array reshape continues in background\n", Name
);
3485 /* close unused handle in child process
3491 /* For each member array with reshape_active,
3492 * we need to perform the reshape.
3493 * We pick the first array that needs reshaping and
3494 * reshape it. reshape_array() will re-read the metadata
3495 * so the next time through a different array should be
3496 * ready for reshape.
3497 * It is possible that the 'different' array will not
3498 * be assembled yet. In that case we simple exit.
3499 * When it is assembled, the mdadm which assembles it
3500 * will take over the reshape.
3502 struct mdinfo
*content
;
3504 struct mdstat_ent
*mdstat
;
3510 cc
= st
->ss
->container_content(st
, NULL
);
3512 for (content
= cc
; content
; content
= content
->next
) {
3514 if (!content
->reshape_active
)
3517 subarray
= strchr(content
->text_version
+1, '/')+1;
3518 mdstat
= mdstat_by_subdev(subarray
, container
);
3521 if (mdstat
->active
== 0) {
3522 pr_err("Skipping inactive array %s.\n",
3524 free_mdstat(mdstat
);
3533 devid
= devnm2devid(mdstat
->devnm
);
3534 adev
= map_dev(major(devid
), minor(devid
), 0);
3536 adev
= content
->text_version
;
3538 fd
= open_dev(mdstat
->devnm
);
3540 pr_err("Device %s cannot be opened for reshape.\n", adev
);
3544 if (strcmp(last_devnm
, mdstat
->devnm
) == 0) {
3545 /* Do not allow for multiple reshape_array() calls for
3547 * It can happen when reshape_array() returns without
3548 * error, when reshape is not finished (wrong reshape
3549 * starting/continuation conditions). Mdmon doesn't
3550 * switch to next array in container and reentry
3551 * conditions for the same array occur.
3552 * This is possibly interim until the behaviour of
3553 * reshape_array is resolved().
3555 printf("%s: Multiple reshape execution detected for device %s.\n", Name
, adev
);
3559 strcpy(last_devnm
, mdstat
->devnm
);
3561 sysfs_init(content
, fd
, mdstat
->devnm
);
3563 if (mdmon_running(container
))
3564 flush_mdmon(container
);
3566 rv
= reshape_array(container
, fd
, adev
, st
,
3567 content
, force
, NULL
, INVALID_SECTORS
,
3568 backup_file
, verbose
, 1, restart
,
3572 if (freeze_reshape
) {
3581 if (mdmon_running(container
))
3582 flush_mdmon(container
);
3591 * We run a child process in the background which performs the following
3593 * - wait for resync to reach a certain point
3594 * - suspend io to the following section
3595 * - backup that section
3596 * - allow resync to proceed further
3598 * - discard the backup.
3600 * When are combined in slightly different ways in the three cases.
3602 * - suspend/backup/allow/wait/resume/discard
3604 * - allow/wait/suspend/backup/allow/wait/resume/discard
3606 * - wait/resume/discard/suspend/backup/allow
3608 * suspend/backup/allow always come together
3609 * wait/resume/discard do too.
3610 * For the same-size case we have two backups to improve flow.
3614 int progress_reshape(struct mdinfo
*info
, struct reshape
*reshape
,
3615 unsigned long long backup_point
,
3616 unsigned long long wait_point
,
3617 unsigned long long *suspend_point
,
3618 unsigned long long *reshape_completed
, int *frozen
)
3620 /* This function is called repeatedly by the reshape manager.
3621 * It determines how much progress can safely be made and allows
3623 * - 'info' identifies the array and particularly records in
3624 * ->reshape_progress the metadata's knowledge of progress
3625 * This is a sector offset from the start of the array
3626 * of the next array block to be relocated. This number
3627 * may increase from 0 or decrease from array_size, depending
3628 * on the type of reshape that is happening.
3629 * Note that in contrast, 'sync_completed' is a block count of the
3630 * reshape so far. It gives the distance between the start point
3631 * (head or tail of device) and the next place that data will be
3632 * written. It always increases.
3633 * - 'reshape' is the structure created by analyse_change
3634 * - 'backup_point' shows how much the metadata manager has backed-up
3635 * data. For reshapes with increasing progress, it is the next address
3636 * to be backed up, previous addresses have been backed-up. For
3637 * decreasing progress, it is the earliest address that has been
3638 * backed up - later address are also backed up.
3639 * So addresses between reshape_progress and backup_point are
3640 * backed up providing those are in the 'correct' order.
3641 * - 'wait_point' is an array address. When reshape_completed
3642 * passes this point, progress_reshape should return. It might
3643 * return earlier if it determines that ->reshape_progress needs
3644 * to be updated or further backup is needed.
3645 * - suspend_point is maintained by progress_reshape and the caller
3646 * should not touch it except to initialise to zero.
3647 * It is an array address and it only increases in 2.6.37 and earlier.
3648 * This makes it difficult to handle reducing reshapes with
3649 * external metadata.
3650 * However: it is similar to backup_point in that it records the
3651 * other end of a suspended region from reshape_progress.
3652 * it is moved to extend the region that is safe to backup and/or
3654 * - reshape_completed is read from sysfs and returned. The caller
3655 * should copy this into ->reshape_progress when it has reason to
3656 * believe that the metadata knows this, and any backup outside this
3660 * 1 if more data from backup_point - but only as far as suspend_point,
3661 * should be backed up
3662 * 0 if things are progressing smoothly
3663 * -1 if the reshape is finished because it is all done,
3664 * -2 if the reshape is finished due to an error.
3667 int advancing
= (reshape
->after
.data_disks
3668 >= reshape
->before
.data_disks
);
3669 unsigned long long need_backup
; /* All data between start of array and
3670 * here will at some point need to
3673 unsigned long long read_offset
, write_offset
;
3674 unsigned long long write_range
;
3675 unsigned long long max_progress
, target
, completed
;
3676 unsigned long long array_size
= (info
->component_size
3677 * reshape
->before
.data_disks
);
3681 /* First, we unsuspend any region that is now known to be safe.
3682 * If suspend_point is on the 'wrong' side of reshape_progress, then
3683 * we don't have or need suspension at the moment. This is true for
3684 * native metadata when we don't need to back-up.
3687 if (info
->reshape_progress
<= *suspend_point
)
3688 sysfs_set_num(info
, NULL
, "suspend_lo",
3689 info
->reshape_progress
);
3691 /* Note: this won't work in 2.6.37 and before.
3692 * Something somewhere should make sure we don't need it!
3694 if (info
->reshape_progress
>= *suspend_point
)
3695 sysfs_set_num(info
, NULL
, "suspend_hi",
3696 info
->reshape_progress
);
3699 /* Now work out how far it is safe to progress.
3700 * If the read_offset for ->reshape_progress is less than
3701 * 'blocks' beyond the write_offset, we can only progress as far
3703 * Otherwise we can progress until the write_offset for the new location
3704 * reaches (within 'blocks' of) the read_offset at the current location.
3705 * However that region must be suspended unless we are using native
3707 * If we need to suspend more, we limit it to 128M per device, which is
3708 * rather arbitrary and should be some time-based calculation.
3710 read_offset
= info
->reshape_progress
/ reshape
->before
.data_disks
;
3711 write_offset
= info
->reshape_progress
/ reshape
->after
.data_disks
;
3712 write_range
= info
->new_chunk
/512;
3713 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
3714 need_backup
= array_size
;
3716 need_backup
= reshape
->backup_blocks
;
3718 if (read_offset
< write_offset
+ write_range
)
3719 max_progress
= backup_point
;
3723 reshape
->after
.data_disks
;
3725 if (read_offset
> write_offset
- write_range
)
3726 /* Can only progress as far as has been backed up,
3727 * which must be suspended */
3728 max_progress
= backup_point
;
3729 else if (info
->reshape_progress
<= need_backup
)
3730 max_progress
= backup_point
;
3732 if (info
->array
.major_version
>= 0)
3733 /* Can progress until backup is needed */
3734 max_progress
= need_backup
;
3736 /* Can progress until metadata update is required */
3739 reshape
->after
.data_disks
;
3740 /* but data must be suspended */
3741 if (max_progress
< *suspend_point
)
3742 max_progress
= *suspend_point
;
3747 /* We know it is safe to progress to 'max_progress' providing
3748 * it is suspended or we are using native metadata.
3749 * Consider extending suspend_point 128M per device if it
3750 * is less than 64M per device beyond reshape_progress.
3751 * But always do a multiple of 'blocks'
3752 * FIXME this is too big - it takes to long to complete
3755 target
= 64*1024*2 * min(reshape
->before
.data_disks
,
3756 reshape
->after
.data_disks
);
3757 target
/= reshape
->backup_blocks
;
3760 target
*= reshape
->backup_blocks
;
3762 /* For externally managed metadata we always need to suspend IO to
3763 * the area being reshaped so we regularly push suspend_point forward.
3764 * For native metadata we only need the suspend if we are going to do
3768 if ((need_backup
> info
->reshape_progress
3769 || info
->array
.major_version
< 0) &&
3770 *suspend_point
< info
->reshape_progress
+ target
) {
3771 if (need_backup
< *suspend_point
+ 2 * target
)
3772 *suspend_point
= need_backup
;
3773 else if (*suspend_point
+ 2 * target
< array_size
)
3774 *suspend_point
+= 2 * target
;
3776 *suspend_point
= array_size
;
3777 sysfs_set_num(info
, NULL
, "suspend_hi", *suspend_point
);
3778 if (max_progress
> *suspend_point
)
3779 max_progress
= *suspend_point
;
3782 if (info
->array
.major_version
>= 0) {
3783 /* Only need to suspend when about to backup */
3784 if (info
->reshape_progress
< need_backup
* 2 &&
3785 *suspend_point
> 0) {
3787 sysfs_set_num(info
, NULL
, "suspend_lo", 0);
3788 sysfs_set_num(info
, NULL
, "suspend_hi", need_backup
);
3791 /* Need to suspend continually */
3792 if (info
->reshape_progress
< *suspend_point
)
3793 *suspend_point
= info
->reshape_progress
;
3794 if (*suspend_point
+ target
< info
->reshape_progress
)
3795 /* No need to move suspend region yet */;
3797 if (*suspend_point
>= 2 * target
)
3798 *suspend_point
-= 2 * target
;
3801 sysfs_set_num(info
, NULL
, "suspend_lo",
3804 if (max_progress
< *suspend_point
)
3805 max_progress
= *suspend_point
;
3809 /* now set sync_max to allow that progress. sync_max, like
3810 * sync_completed is a count of sectors written per device, so
3811 * we find the difference between max_progress and the start point,
3812 * and divide that by after.data_disks to get a sync_max
3814 * At the same time we convert wait_point to a similar number
3815 * for comparing against sync_completed.
3817 /* scale down max_progress to per_disk */
3818 max_progress
/= reshape
->after
.data_disks
;
3819 /* Round to chunk size as some kernels give an erroneously high number */
3820 max_progress
/= info
->new_chunk
/512;
3821 max_progress
*= info
->new_chunk
/512;
3822 /* And round to old chunk size as the kernel wants that */
3823 max_progress
/= info
->array
.chunk_size
/512;
3824 max_progress
*= info
->array
.chunk_size
/512;
3825 /* Limit progress to the whole device */
3826 if (max_progress
> info
->component_size
)
3827 max_progress
= info
->component_size
;
3828 wait_point
/= reshape
->after
.data_disks
;
3830 /* switch from 'device offset' to 'processed block count' */
3831 max_progress
= info
->component_size
- max_progress
;
3832 wait_point
= info
->component_size
- wait_point
;
3836 sysfs_set_num(info
, NULL
, "sync_max", max_progress
);
3838 /* Now wait. If we have already reached the point that we were
3839 * asked to wait to, don't wait at all, else wait for any change.
3840 * We need to select on 'sync_completed' as that is the place that
3841 * notifications happen, but we are really interested in
3842 * 'reshape_position'
3844 fd
= sysfs_get_fd(info
, NULL
, "sync_completed");
3846 goto check_progress
;
3848 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
3849 goto check_progress
;
3851 while (completed
< max_progress
&& completed
< wait_point
) {
3852 /* Check that sync_action is still 'reshape' to avoid
3853 * waiting forever on a dead array
3856 if (sysfs_get_str(info
, NULL
, "sync_action",
3858 strncmp(action
, "reshape", 7) != 0)
3860 /* Some kernels reset 'sync_completed' to zero
3861 * before setting 'sync_action' to 'idle'.
3862 * So we need these extra tests.
3864 if (completed
== 0 && advancing
3865 && strncmp(action
, "idle", 4) == 0
3866 && info
->reshape_progress
> 0)
3868 if (completed
== 0 && !advancing
3869 && strncmp(action
, "idle", 4) == 0
3870 && info
->reshape_progress
< (info
->component_size
3871 * reshape
->after
.data_disks
))
3873 sysfs_wait(fd
, NULL
);
3874 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
3875 goto check_progress
;
3877 /* Some kernels reset 'sync_completed' to zero,
3878 * we need to have real point we are in md.
3879 * So in that case, read 'reshape_position' from sysfs.
3881 if (completed
== 0) {
3882 unsigned long long reshapep
;
3884 if (sysfs_get_str(info
, NULL
, "sync_action",
3886 strncmp(action
, "idle", 4) == 0 &&
3887 sysfs_get_ll(info
, NULL
,
3888 "reshape_position", &reshapep
) == 0)
3889 *reshape_completed
= reshapep
;
3891 /* some kernels can give an incorrectly high
3892 * 'completed' number, so round down */
3893 completed
/= (info
->new_chunk
/512);
3894 completed
*= (info
->new_chunk
/512);
3895 /* Convert 'completed' back in to a 'progress' number */
3896 completed
*= reshape
->after
.data_disks
;
3898 completed
= (info
->component_size
3899 * reshape
->after
.data_disks
3901 *reshape_completed
= completed
;
3906 /* We return the need_backup flag. Caller will decide
3907 * how much - a multiple of ->backup_blocks up to *suspend_point
3910 return need_backup
> info
->reshape_progress
;
3912 return need_backup
>= info
->reshape_progress
;
3915 /* if we couldn't read a number from sync_completed, then
3916 * either the reshape did complete, or it aborted.
3917 * We can tell which by checking for 'none' in reshape_position.
3918 * If it did abort, then it might immediately restart if it
3919 * it was just a device failure that leaves us degraded but
3922 if (sysfs_get_str(info
, NULL
, "reshape_position", buf
, sizeof(buf
)) < 0
3923 || strncmp(buf
, "none", 4) != 0) {
3924 /* The abort might only be temporary. Wait up to 10
3925 * seconds for fd to contain a valid number again.
3929 unsigned long long new_sync_max
;
3930 while (fd
>= 0 && rv
< 0 && wait
> 0) {
3931 if (sysfs_wait(fd
, &wait
) != 1)
3933 switch (sysfs_fd_get_ll(fd
, &completed
)) {
3935 /* all good again */
3937 /* If "sync_max" is no longer max_progress
3938 * we need to freeze things
3940 sysfs_get_ll(info
, NULL
, "sync_max", &new_sync_max
);
3941 *frozen
= (new_sync_max
!= max_progress
);
3943 case -2: /* read error - abort */
3950 return rv
; /* abort */
3952 /* Maybe racing with array shutdown - check state */
3955 if (sysfs_get_str(info
, NULL
, "array_state", buf
, sizeof(buf
)) < 0
3956 || strncmp(buf
, "inactive", 8) == 0
3957 || strncmp(buf
, "clear",5) == 0)
3958 return -2; /* abort */
3959 return -1; /* complete */
3963 /* FIXME return status is never checked */
3964 static int grow_backup(struct mdinfo
*sra
,
3965 unsigned long long offset
, /* per device */
3966 unsigned long stripes
, /* per device, in old chunks */
3967 int *sources
, unsigned long long *offsets
,
3968 int disks
, int chunk
, int level
, int layout
,
3969 int dests
, int *destfd
, unsigned long long *destoffsets
,
3970 int part
, int *degraded
,
3973 /* Backup 'blocks' sectors at 'offset' on each device of the array,
3974 * to storage 'destfd' (offset 'destoffsets'), after first
3975 * suspending IO. Then allow resync to continue
3976 * over the suspended section.
3977 * Use part 'part' of the backup-super-block.
3982 unsigned long long ll
;
3984 //printf("offset %llu\n", offset);
3990 /* Check that array hasn't become degraded, else we might backup the wrong data */
3991 if (sysfs_get_ll(sra
, NULL
, "degraded", &ll
) < 0)
3992 return -1; /* FIXME this error is ignored */
3993 new_degraded
= (int)ll
;
3994 if (new_degraded
!= *degraded
) {
3995 /* check each device to ensure it is still working */
3997 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
3998 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
4000 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
4002 if (sysfs_get_str(sra
, sd
, "state", sbuf
, 20) < 0 ||
4003 strstr(sbuf
, "faulty") ||
4004 strstr(sbuf
, "in_sync") == NULL
) {
4005 /* this device is dead */
4006 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
4007 if (sd
->disk
.raid_disk
>= 0 &&
4008 sources
[sd
->disk
.raid_disk
] >= 0) {
4009 close(sources
[sd
->disk
.raid_disk
]);
4010 sources
[sd
->disk
.raid_disk
] = -1;
4015 *degraded
= new_degraded
;
4018 bsb
.arraystart2
= __cpu_to_le64(offset
* odata
);
4019 bsb
.length2
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
4021 bsb
.arraystart
= __cpu_to_le64(offset
* odata
);
4022 bsb
.length
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
4025 bsb
.magic
[15] = '2';
4026 for (i
= 0; i
< dests
; i
++)
4028 lseek64(destfd
[i
], destoffsets
[i
] + __le64_to_cpu(bsb
.devstart2
)*512, 0);
4030 lseek64(destfd
[i
], destoffsets
[i
], 0);
4032 rv
= save_stripes(sources
, offsets
,
4033 disks
, chunk
, level
, layout
,
4035 offset
*512*odata
, stripes
* chunk
* odata
,
4040 bsb
.mtime
= __cpu_to_le64(time(0));
4041 for (i
= 0; i
< dests
; i
++) {
4042 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
4044 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
4045 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
4046 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
4047 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
4050 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
] - 4096, 0)
4051 != destoffsets
[i
] - 4096)
4053 if (write(destfd
[i
], &bsb
, 512) != 512)
4055 if (destoffsets
[i
] > 4096) {
4056 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]+stripes
*chunk
*odata
, 0) !=
4057 destoffsets
[i
]+stripes
*chunk
*odata
)
4059 if (write(destfd
[i
], &bsb
, 512) != 512)
4069 /* in 2.6.30, the value reported by sync_completed can be
4070 * less that it should be by one stripe.
4071 * This only happens when reshape hits sync_max and pauses.
4072 * So allow wait_backup to either extent sync_max further
4073 * than strictly necessary, or return before the
4074 * sync has got quite as far as we would really like.
4075 * This is what 'blocks2' is for.
4076 * The various caller give appropriate values so that
4079 /* FIXME return value is often ignored */
4080 static int forget_backup(int dests
, int *destfd
,
4081 unsigned long long *destoffsets
,
4085 * Erase backup 'part' (which is 0 or 1)
4091 bsb
.arraystart2
= __cpu_to_le64(0);
4092 bsb
.length2
= __cpu_to_le64(0);
4094 bsb
.arraystart
= __cpu_to_le64(0);
4095 bsb
.length
= __cpu_to_le64(0);
4097 bsb
.mtime
= __cpu_to_le64(time(0));
4099 for (i
= 0; i
< dests
; i
++) {
4100 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
4101 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
4102 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
4103 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
4104 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
4105 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]-4096, 0) !=
4106 destoffsets
[i
]-4096)
4109 write(destfd
[i
], &bsb
, 512) != 512)
4116 static void fail(char *msg
)
4119 rv
= (write(2, msg
, strlen(msg
)) != (int)strlen(msg
));
4120 rv
|= (write(2, "\n", 1) != 1);
4124 static char *abuf
, *bbuf
;
4125 static unsigned long long abuflen
;
4126 static void validate(int afd
, int bfd
, unsigned long long offset
)
4128 /* check that the data in the backup against the array.
4129 * This is only used for regression testing and should not
4130 * be used while the array is active
4134 lseek64(bfd
, offset
- 4096, 0);
4135 if (read(bfd
, &bsb2
, 512) != 512)
4136 fail("cannot read bsb");
4137 if (bsb2
.sb_csum
!= bsb_csum((char*)&bsb2
,
4138 ((char*)&bsb2
.sb_csum
)-((char*)&bsb2
)))
4139 fail("first csum bad");
4140 if (memcmp(bsb2
.magic
, "md_backup_data", 14) != 0)
4141 fail("magic is bad");
4142 if (memcmp(bsb2
.magic
, "md_backup_data-2", 16) == 0 &&
4143 bsb2
.sb_csum2
!= bsb_csum((char*)&bsb2
,
4144 ((char*)&bsb2
.sb_csum2
)-((char*)&bsb2
)))
4145 fail("second csum bad");
4147 if (__le64_to_cpu(bsb2
.devstart
)*512 != offset
)
4148 fail("devstart is wrong");
4151 unsigned long long len
= __le64_to_cpu(bsb2
.length
)*512;
4153 if (abuflen
< len
) {
4157 if (posix_memalign((void**)&abuf
, 4096, abuflen
) ||
4158 posix_memalign((void**)&bbuf
, 4096, abuflen
)) {
4160 /* just stop validating on mem-alloc failure */
4165 lseek64(bfd
, offset
, 0);
4166 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
) {
4167 //printf("len %llu\n", len);
4168 fail("read first backup failed");
4170 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart
)*512, 0);
4171 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
4172 fail("read first from array failed");
4173 if (memcmp(bbuf
, abuf
, len
) != 0) {
4176 printf("offset=%llu len=%llu\n",
4177 (unsigned long long)__le64_to_cpu(bsb2
.arraystart
)*512, len
);
4178 for (i
=0; i
<len
; i
++)
4179 if (bbuf
[i
] != abuf
[i
]) {
4180 printf("first diff byte %d\n", i
);
4184 fail("data1 compare failed");
4188 unsigned long long len
= __le64_to_cpu(bsb2
.length2
)*512;
4190 if (abuflen
< len
) {
4194 abuf
= xmalloc(abuflen
);
4195 bbuf
= xmalloc(abuflen
);
4198 lseek64(bfd
, offset
+__le64_to_cpu(bsb2
.devstart2
)*512, 0);
4199 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
)
4200 fail("read second backup failed");
4201 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart2
)*512, 0);
4202 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
4203 fail("read second from array failed");
4204 if (memcmp(bbuf
, abuf
, len
) != 0)
4205 fail("data2 compare failed");
4209 int child_monitor(int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
4210 struct supertype
*st
, unsigned long blocks
,
4211 int *fds
, unsigned long long *offsets
,
4212 int dests
, int *destfd
, unsigned long long *destoffsets
)
4214 /* Monitor a reshape where backup is being performed using
4215 * 'native' mechanism - either to a backup file, or
4216 * to some space in a spare.
4220 unsigned long long speed
;
4221 unsigned long long suspend_point
, array_size
;
4222 unsigned long long backup_point
, wait_point
;
4223 unsigned long long reshape_completed
;
4225 int increasing
= reshape
->after
.data_disks
>= reshape
->before
.data_disks
;
4226 int part
= 0; /* The next part of the backup area to fill. It may already
4227 * be full, so we need to check */
4228 int level
= reshape
->level
;
4229 int layout
= reshape
->before
.layout
;
4230 int data
= reshape
->before
.data_disks
;
4231 int disks
= reshape
->before
.data_disks
+ reshape
->parity
;
4232 int chunk
= sra
->array
.chunk_size
;
4234 unsigned long stripes
;
4238 /* set up the backup-super-block. This requires the
4239 * uuid from the array.
4241 /* Find a superblock */
4242 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
4246 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
4248 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
4249 devfd
= dev_open(dn
, O_RDONLY
);
4252 ok
= st
->ss
->load_super(st
, devfd
, NULL
);
4258 pr_err("Cannot find a superblock\n");
4262 memset(&bsb
, 0, 512);
4263 memcpy(bsb
.magic
, "md_backup_data-1", 16);
4264 st
->ss
->uuid_from_super(st
, uuid
);
4265 memcpy(bsb
.set_uuid
, uuid
, 16);
4266 bsb
.mtime
= __cpu_to_le64(time(0));
4267 bsb
.devstart2
= blocks
;
4269 stripes
= blocks
/ (sra
->array
.chunk_size
/512) /
4270 reshape
->before
.data_disks
;
4272 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
4273 /* Don't start the 'reshape' */
4275 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
) {
4276 sysfs_get_ll(sra
, NULL
, "sync_speed_min", &speed
);
4277 sysfs_set_num(sra
, NULL
, "sync_speed_min", 200000);
4281 array_size
= sra
->component_size
* reshape
->after
.data_disks
;
4282 backup_point
= sra
->reshape_progress
;
4285 array_size
= sra
->component_size
* reshape
->before
.data_disks
;
4286 backup_point
= reshape
->backup_blocks
;
4287 suspend_point
= array_size
;
4293 /* Want to return as soon the oldest backup slot can
4294 * be released as that allows us to start backing up
4295 * some more, providing suspend_point has been
4296 * advanced, which it should have.
4299 wait_point
= array_size
;
4300 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
4301 wait_point
= (__le64_to_cpu(bsb
.arraystart
) +
4302 __le64_to_cpu(bsb
.length
));
4303 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
4304 wait_point
= (__le64_to_cpu(bsb
.arraystart2
) +
4305 __le64_to_cpu(bsb
.length2
));
4308 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
4309 wait_point
= __le64_to_cpu(bsb
.arraystart
);
4310 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
4311 wait_point
= __le64_to_cpu(bsb
.arraystart2
);
4314 reshape_completed
= sra
->reshape_progress
;
4315 rv
= progress_reshape(sra
, reshape
,
4316 backup_point
, wait_point
,
4317 &suspend_point
, &reshape_completed
,
4319 /* external metadata would need to ping_monitor here */
4320 sra
->reshape_progress
= reshape_completed
;
4322 /* Clear any backup region that is before 'here' */
4324 if (__le64_to_cpu(bsb
.length
) > 0 &&
4325 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart
) +
4326 __le64_to_cpu(bsb
.length
)))
4327 forget_backup(dests
, destfd
,
4329 if (__le64_to_cpu(bsb
.length2
) > 0 &&
4330 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart2
) +
4331 __le64_to_cpu(bsb
.length2
)))
4332 forget_backup(dests
, destfd
,
4335 if (__le64_to_cpu(bsb
.length
) > 0 &&
4336 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart
)))
4337 forget_backup(dests
, destfd
,
4339 if (__le64_to_cpu(bsb
.length2
) > 0 &&
4340 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart2
)))
4341 forget_backup(dests
, destfd
,
4351 if (rv
== 0 && increasing
&& !st
->ss
->external
) {
4352 /* No longer need to monitor this reshape */
4353 sysfs_set_str(sra
, NULL
, "sync_max", "max");
4359 unsigned long long offset
;
4360 unsigned long actual_stripes
;
4361 /* Need to backup some data.
4362 * If 'part' is not used and the desired
4363 * backup size is suspended, do a backup,
4364 * then consider the next part.
4366 /* Check that 'part' is unused */
4367 if (part
== 0 && __le64_to_cpu(bsb
.length
) != 0)
4369 if (part
== 1 && __le64_to_cpu(bsb
.length2
) != 0)
4372 offset
= backup_point
/ data
;
4373 actual_stripes
= stripes
;
4375 if (offset
+ actual_stripes
* (chunk
/512) >
4376 sra
->component_size
)
4377 actual_stripes
= ((sra
->component_size
- offset
)
4379 if (offset
+ actual_stripes
* (chunk
/512) >
4383 if (offset
< actual_stripes
* (chunk
/512))
4384 actual_stripes
= offset
/ (chunk
/512);
4385 offset
-= actual_stripes
* (chunk
/512);
4386 if (offset
< suspend_point
/data
)
4389 if (actual_stripes
== 0)
4391 grow_backup(sra
, offset
, actual_stripes
,
4393 disks
, chunk
, level
, layout
,
4394 dests
, destfd
, destoffsets
,
4395 part
, °raded
, buf
);
4396 validate(afd
, destfd
[0], destoffsets
[0]);
4397 /* record where 'part' is up to */
4400 backup_point
+= actual_stripes
* (chunk
/512) * data
;
4402 backup_point
-= actual_stripes
* (chunk
/512) * data
;
4406 /* FIXME maybe call progress_reshape one more time instead */
4407 /* remove any remaining suspension */
4408 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
4409 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
4410 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
4411 sysfs_set_num(sra
, NULL
, "sync_min", 0);
4413 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
4414 sysfs_set_num(sra
, NULL
, "sync_speed_min", speed
);
4420 * If any spare contains md_back_data-1 which is recent wrt mtime,
4421 * write that data into the array and update the super blocks with
4422 * the new reshape_progress
4424 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
, int cnt
,
4425 char *backup_file
, int verbose
)
4429 unsigned long long *offsets
;
4430 unsigned long long nstripe
, ostripe
;
4433 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
4434 if (info
->array
.level
== 6) odata
--; /* number of data disks */
4435 ndata
= info
->array
.raid_disks
- 1;
4436 if (info
->new_level
== 6) ndata
--;
4438 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
4440 if (info
->delta_disks
<= 0)
4441 /* Didn't grow, so the backup file must have
4445 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
4446 struct mdinfo dinfo
;
4449 char *devname
, namebuf
[20];
4450 unsigned long long lo
, hi
;
4452 /* This was a spare and may have some saved data on it.
4453 * Load the superblock, find and load the
4454 * backup_super_block.
4455 * If either fail, go on to next device.
4456 * If the backup contains no new info, just return
4457 * else restore data and update all superblocks
4459 if (i
== old_disks
-1) {
4460 fd
= open(backup_file
, O_RDONLY
);
4462 pr_err("backup file %s inaccessible: %s\n",
4463 backup_file
, strerror(errno
));
4466 devname
= backup_file
;
4471 if (st
->ss
->load_super(st
, fd
, NULL
))
4474 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4475 st
->ss
->free_super(st
);
4478 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
4480 pr_err("Cannot seek on device %d\n", i
);
4481 continue; /* Cannot seek */
4483 sprintf(namebuf
, "device-%d", i
);
4486 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
)) {
4488 pr_err("Cannot read from %s\n", devname
);
4489 continue; /* Cannot read */
4491 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0 &&
4492 memcmp(bsb
.magic
, "md_backup_data-2", 16) != 0) {
4494 pr_err("No backup metadata on %s\n", devname
);
4497 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
))) {
4499 pr_err("Bad backup-metadata checksum on %s\n", devname
);
4500 continue; /* bad checksum */
4502 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0 &&
4503 bsb
.sb_csum2
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum2
)-((char*)&bsb
))) {
4505 pr_err("Bad backup-metadata checksum2 on %s\n", devname
);
4506 continue; /* Bad second checksum */
4508 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0) {
4510 pr_err("Wrong uuid on backup-metadata on %s\n", devname
);
4511 continue; /* Wrong uuid */
4514 /* array utime and backup-mtime should be updated at much the same time, but it seems that
4515 * sometimes they aren't... So allow considerable flexability in matching, and allow
4516 * this test to be overridden by an environment variable.
4518 if(time_after(info
->array
.utime
, (unsigned int)__le64_to_cpu(bsb
.mtime
) + 2*60*60) ||
4519 time_before(info
->array
.utime
, (unsigned int)__le64_to_cpu(bsb
.mtime
) - 10*60)) {
4520 if (check_env("MDADM_GROW_ALLOW_OLD")) {
4521 pr_err("accepting backup with timestamp %lu for array with timestamp %lu\n",
4522 (unsigned long)__le64_to_cpu(bsb
.mtime
),
4523 (unsigned long)info
->array
.utime
);
4525 pr_err("too-old timestamp on backup-metadata on %s\n", devname
);
4526 pr_err("If you think it is should be safe, try 'export MDADM_GROW_ALLOW_OLD=1'\n");
4527 continue; /* time stamp is too bad */
4531 if (bsb
.magic
[15] == '1') {
4532 if (bsb
.length
== 0)
4534 if (info
->delta_disks
>= 0) {
4535 /* reshape_progress is increasing */
4536 if (__le64_to_cpu(bsb
.arraystart
)
4537 + __le64_to_cpu(bsb
.length
)
4538 < info
->reshape_progress
) {
4541 pr_err("backup-metadata found on %s but is not needed\n", devname
);
4542 continue; /* No new data here */
4545 /* reshape_progress is decreasing */
4546 if (__le64_to_cpu(bsb
.arraystart
) >=
4547 info
->reshape_progress
)
4548 goto nonew
; /* No new data here */
4551 if (bsb
.length
== 0 && bsb
.length2
== 0)
4553 if (info
->delta_disks
>= 0) {
4554 /* reshape_progress is increasing */
4555 if ((__le64_to_cpu(bsb
.arraystart
)
4556 + __le64_to_cpu(bsb
.length
)
4557 < info
->reshape_progress
)
4559 (__le64_to_cpu(bsb
.arraystart2
)
4560 + __le64_to_cpu(bsb
.length2
)
4561 < info
->reshape_progress
))
4562 goto nonew
; /* No new data here */
4564 /* reshape_progress is decreasing */
4565 if (__le64_to_cpu(bsb
.arraystart
) >=
4566 info
->reshape_progress
&&
4567 __le64_to_cpu(bsb
.arraystart2
) >=
4568 info
->reshape_progress
)
4569 goto nonew
; /* No new data here */
4572 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0) {
4575 pr_err("Failed to verify secondary backup-metadata block on %s\n",
4577 continue; /* Cannot seek */
4579 /* There should be a duplicate backup superblock 4k before here */
4580 if (lseek64(fd
, -4096, 1) < 0 ||
4581 read(fd
, &bsb2
, sizeof(bsb2
)) != sizeof(bsb2
))
4582 goto second_fail
; /* Cannot find leading superblock */
4583 if (bsb
.magic
[15] == '1')
4584 bsbsize
= offsetof(struct mdp_backup_super
, pad1
);
4586 bsbsize
= offsetof(struct mdp_backup_super
, pad
);
4587 if (memcmp(&bsb2
, &bsb
, bsbsize
) != 0)
4588 goto second_fail
; /* Cannot find leading superblock */
4590 /* Now need the data offsets for all devices. */
4591 offsets
= xmalloc(sizeof(*offsets
)*info
->array
.raid_disks
);
4592 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
4595 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
4596 /* FIXME should be this be an error */
4598 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4599 st
->ss
->free_super(st
);
4600 offsets
[j
] = dinfo
.data_offset
* 512;
4602 printf("%s: restoring critical section\n", Name
);
4604 if (restore_stripes(fdlist
, offsets
,
4605 info
->array
.raid_disks
,
4609 fd
, __le64_to_cpu(bsb
.devstart
)*512,
4610 __le64_to_cpu(bsb
.arraystart
)*512,
4611 __le64_to_cpu(bsb
.length
)*512, NULL
)) {
4612 /* didn't succeed, so giveup */
4614 pr_err("Error restoring backup from %s\n",
4620 if (bsb
.magic
[15] == '2' &&
4621 restore_stripes(fdlist
, offsets
,
4622 info
->array
.raid_disks
,
4626 fd
, __le64_to_cpu(bsb
.devstart
)*512 +
4627 __le64_to_cpu(bsb
.devstart2
)*512,
4628 __le64_to_cpu(bsb
.arraystart2
)*512,
4629 __le64_to_cpu(bsb
.length2
)*512, NULL
)) {
4630 /* didn't succeed, so giveup */
4632 pr_err("Error restoring second backup from %s\n",
4640 /* Ok, so the data is restored. Let's update those superblocks. */
4644 lo
= __le64_to_cpu(bsb
.arraystart
);
4645 hi
= lo
+ __le64_to_cpu(bsb
.length
);
4647 if (bsb
.magic
[15] == '2' && bsb
.length2
) {
4648 unsigned long long lo1
, hi1
;
4649 lo1
= __le64_to_cpu(bsb
.arraystart2
);
4650 hi1
= lo1
+ __le64_to_cpu(bsb
.length2
);
4654 } else if (lo
< lo1
)
4660 (info
->reshape_progress
< lo
||
4661 info
->reshape_progress
> hi
))
4662 /* backup does not affect reshape_progress*/ ;
4663 else if (info
->delta_disks
>= 0) {
4664 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
) +
4665 __le64_to_cpu(bsb
.length
);
4666 if (bsb
.magic
[15] == '2') {
4667 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
) +
4668 __le64_to_cpu(bsb
.length2
);
4669 if (p2
> info
->reshape_progress
)
4670 info
->reshape_progress
= p2
;
4673 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
);
4674 if (bsb
.magic
[15] == '2') {
4675 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
);
4676 if (p2
< info
->reshape_progress
)
4677 info
->reshape_progress
= p2
;
4680 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
4683 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
4685 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4686 dinfo
.reshape_progress
= info
->reshape_progress
;
4687 st
->ss
->update_super(st
, &dinfo
,
4688 "_reshape_progress",
4690 st
->ss
->store_super(st
, fdlist
[j
]);
4691 st
->ss
->free_super(st
);
4695 /* Didn't find any backup data, try to see if any
4698 if (info
->delta_disks
< 0) {
4699 /* When shrinking, the critical section is at the end.
4700 * So see if we are before the critical section.
4702 unsigned long long first_block
;
4703 nstripe
= ostripe
= 0;
4705 while (ostripe
>= nstripe
) {
4706 ostripe
+= info
->array
.chunk_size
/ 512;
4707 first_block
= ostripe
* odata
;
4708 nstripe
= first_block
/ ndata
/ (info
->new_chunk
/512) *
4709 (info
->new_chunk
/512);
4712 if (info
->reshape_progress
>= first_block
)
4715 if (info
->delta_disks
> 0) {
4716 /* See if we are beyond the critical section. */
4717 unsigned long long last_block
;
4718 nstripe
= ostripe
= 0;
4720 while (nstripe
>= ostripe
) {
4721 nstripe
+= info
->new_chunk
/ 512;
4722 last_block
= nstripe
* ndata
;
4723 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
4724 (info
->array
.chunk_size
/512);
4727 if (info
->reshape_progress
>= last_block
)
4730 /* needed to recover critical section! */
4732 pr_err("Failed to find backup of critical section\n");
4736 int Grow_continue_command(char *devname
, int fd
,
4737 char *backup_file
, int verbose
)
4740 struct supertype
*st
= NULL
;
4741 struct mdinfo
*content
= NULL
;
4742 struct mdinfo array
;
4743 char *subarray
= NULL
;
4744 struct mdinfo
*cc
= NULL
;
4745 struct mdstat_ent
*mdstat
= NULL
;
4749 dprintf("Grow continue from command line called for %s\n",
4752 st
= super_by_fd(fd
, &subarray
);
4753 if (!st
|| !st
->ss
) {
4754 pr_err("Unable to determine metadata format for %s\n",
4758 dprintf("Grow continue is run for ");
4759 if (st
->ss
->external
== 0) {
4761 dprintf_cont("native array (%s)\n", devname
);
4762 if (ioctl(fd
, GET_ARRAY_INFO
, &array
.array
) < 0) {
4763 pr_err("%s is not an active md array - aborting\n", devname
);
4765 goto Grow_continue_command_exit
;
4768 /* Need to load a superblock.
4769 * FIXME we should really get what we need from
4772 for (d
= 0; d
< MAX_DISKS
; d
++) {
4773 mdu_disk_info_t disk
;
4777 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
4779 if (disk
.major
== 0 && disk
.minor
== 0)
4781 if ((disk
.state
& (1 << MD_DISK_ACTIVE
)) == 0)
4783 dv
= map_dev(disk
.major
, disk
.minor
, 1);
4786 fd2
= dev_open(dv
, O_RDONLY
);
4789 err
= st
->ss
->load_super(st
, fd2
, NULL
);
4791 /* invalidate fd2 to avoid possible double close() */
4797 if (d
== MAX_DISKS
) {
4798 pr_err("Unable to load metadata for %s\n",
4801 goto Grow_continue_command_exit
;
4803 st
->ss
->getinfo_super(st
, content
, NULL
);
4808 dprintf_cont("subarray (%s)\n", subarray
);
4809 container
= st
->container_devnm
;
4810 cfd
= open_dev_excl(st
->container_devnm
);
4812 container
= st
->devnm
;
4814 cfd
= open_dev_excl(st
->devnm
);
4815 dprintf_cont("container (%s)\n", container
);
4819 pr_err("Unable to open container for %s\n", devname
);
4821 goto Grow_continue_command_exit
;
4824 /* find in container array under reshape
4826 ret_val
= st
->ss
->load_container(st
, cfd
, NULL
);
4828 pr_err("Cannot read superblock for %s\n",
4831 goto Grow_continue_command_exit
;
4834 cc
= st
->ss
->container_content(st
, subarray
);
4835 for (content
= cc
; content
; content
= content
->next
) {
4837 int allow_reshape
= 1;
4839 if (content
->reshape_active
== 0)
4841 /* The decision about array or container wide
4842 * reshape is taken in Grow_continue based
4843 * content->reshape_active state, therefore we
4844 * need to check_reshape based on
4845 * reshape_active and subarray name
4847 if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
))
4849 if (content
->reshape_active
== CONTAINER_RESHAPE
&&
4850 (content
->array
.state
4851 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
)))
4854 if (!allow_reshape
) {
4855 pr_err("cannot continue reshape of an array in container with unsupported metadata: %s(%s)\n",
4856 devname
, container
);
4858 goto Grow_continue_command_exit
;
4861 array
= strchr(content
->text_version
+1, '/')+1;
4862 mdstat
= mdstat_by_subdev(array
, container
);
4865 if (mdstat
->active
== 0) {
4866 pr_err("Skipping inactive array %s.\n",
4868 free_mdstat(mdstat
);
4875 pr_err("Unable to determine reshaped array for %s\n", devname
);
4877 goto Grow_continue_command_exit
;
4879 fd2
= open_dev(mdstat
->devnm
);
4881 pr_err("cannot open (%s)\n", mdstat
->devnm
);
4883 goto Grow_continue_command_exit
;
4886 sysfs_init(content
, fd2
, mdstat
->devnm
);
4891 /* start mdmon in case it is not running
4893 if (!mdmon_running(container
))
4894 start_mdmon(container
);
4895 ping_monitor(container
);
4897 if (mdmon_running(container
))
4898 st
->update_tail
= &st
->updates
;
4900 pr_err("No mdmon found. Grow cannot continue.\n");
4902 goto Grow_continue_command_exit
;
4906 /* verify that array under reshape is started from
4909 if (verify_reshape_position(content
, content
->array
.level
) < 0) {
4911 goto Grow_continue_command_exit
;
4916 ret_val
= Grow_continue(fd
, st
, content
, backup_file
, 1, 0);
4918 Grow_continue_command_exit
:
4923 st
->ss
->free_super(st
);
4924 free_mdstat(mdstat
);
4931 int Grow_continue(int mdfd
, struct supertype
*st
, struct mdinfo
*info
,
4932 char *backup_file
, int forked
, int freeze_reshape
)
4936 if (!info
->reshape_active
)
4939 if (st
->ss
->external
) {
4940 int cfd
= open_dev(st
->container_devnm
);
4945 st
->ss
->load_container(st
, cfd
, st
->container_devnm
);
4947 ret_val
= reshape_container(st
->container_devnm
, NULL
, mdfd
,
4948 st
, info
, 0, backup_file
,
4950 1 | info
->reshape_active
,
4953 ret_val
= reshape_array(NULL
, mdfd
, "array", st
, info
, 1,
4954 NULL
, INVALID_SECTORS
,
4955 backup_file
, 0, forked
,
4956 1 | info
->reshape_active
,
4962 char *make_backup(char *name
)
4964 char *base
= "backup_file-";
4968 len
= strlen(MAP_DIR
) + 1 + strlen(base
) + strlen(name
)+1;
4969 fname
= xmalloc(len
);
4970 sprintf(fname
, "%s/%s%s", MAP_DIR
, base
, name
);
4974 char *locate_backup(char *name
)
4976 char *fl
= make_backup(name
);
4979 if (stat(fl
, &stb
) == 0 &&
4980 S_ISREG(stb
.st_mode
))