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
) {
62 sprintf(buf
, "%d:%d", dev
->disk
.major
, dev
->disk
.minor
);
63 fd
= dev_open(buf
, O_RDWR
);
65 if (dev
->disk
.raid_disk
>= 0)
66 fdlist
[dev
->disk
.raid_disk
] = fd
;
68 fdlist
[next_spare
++] = fd
;
72 backup_file
= locate_backup(content
->sys_name
);
73 *backup_filep
= backup_file
;
76 if (st
->ss
->external
&& st
->ss
->recover_backup
)
77 err
= st
->ss
->recover_backup(st
, content
);
79 err
= Grow_restart(st
, content
, fdlist
, next_spare
,
80 backup_file
, verbose
> 0);
82 while (next_spare
> 0) {
84 if (fdlist
[next_spare
] >= 0)
85 close(fdlist
[next_spare
]);
89 pr_err("Failed to restore critical section for reshape - sorry.\n");
91 pr_err("Possibly you need to specify a --backup-file\n");
95 dprintf("restore_backup() returns status OK.\n");
99 int Grow_Add_device(char *devname
, int fd
, char *newdev
)
101 /* Add a device to an active array.
102 * Currently, just extend a linear array.
103 * This requires writing a new superblock on the
104 * new device, calling the kernel to add the device,
105 * and if that succeeds, update the superblock on
107 * This means that we need to *find* all other devices.
114 struct supertype
*st
= NULL
;
115 char *subarray
= NULL
;
117 if (md_get_array_info(fd
, &info
.array
) < 0) {
118 pr_err("cannot get array info for %s\n", devname
);
122 if (info
.array
.level
!= -1) {
123 pr_err("can only add devices to linear arrays\n");
127 st
= super_by_fd(fd
, &subarray
);
129 pr_err("cannot handle arrays with superblock version %d\n",
130 info
.array
.major_version
);
135 pr_err("Cannot grow linear sub-arrays yet\n");
141 nfd
= open(newdev
, O_RDWR
|O_EXCL
|O_DIRECT
);
143 pr_err("cannot open %s\n", newdev
);
147 if (!fstat_is_blkdev(nfd
, newdev
, &rdev
)) {
152 /* now check out all the devices and make sure we can read the
154 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
155 mdu_disk_info_t disk
;
158 st
->ss
->free_super(st
);
161 if (md_get_disk_info(fd
, &disk
) < 0) {
162 pr_err("cannot get device detail for device %d\n", d
);
167 dv
= map_dev(disk
.major
, disk
.minor
, 1);
169 pr_err("cannot find device file for device %d\n", d
);
174 fd2
= dev_open(dv
, O_RDWR
);
176 pr_err("cannot open device file %s\n", dv
);
182 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
183 pr_err("cannot find super block on %s\n", dv
);
191 /* Ok, looks good. Lets update the superblock and write it out to
195 info
.disk
.number
= d
;
196 info
.disk
.major
= major(rdev
);
197 info
.disk
.minor
= minor(rdev
);
198 info
.disk
.raid_disk
= d
;
199 info
.disk
.state
= (1 << MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
);
200 if (st
->ss
->update_super(st
, &info
, "linear-grow-new", newdev
,
202 pr_err("Preparing new metadata failed on %s\n", newdev
);
207 if (st
->ss
->store_super(st
, nfd
)) {
208 pr_err("Cannot store new superblock on %s\n", newdev
);
214 if (ioctl(fd
, ADD_NEW_DISK
, &info
.disk
) != 0) {
215 pr_err("Cannot add new disk to this array\n");
218 /* Well, that seems to have worked.
219 * Now go through and update all superblocks
222 if (md_get_array_info(fd
, &info
.array
) < 0) {
223 pr_err("cannot get array info for %s\n", devname
);
228 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
229 mdu_disk_info_t disk
;
233 if (md_get_disk_info(fd
, &disk
) < 0) {
234 pr_err("cannot get device detail for device %d\n", d
);
237 dv
= map_dev(disk
.major
, disk
.minor
, 1);
239 pr_err("cannot find device file for device %d\n", d
);
242 fd2
= dev_open(dv
, O_RDWR
);
244 pr_err("cannot open device file %s\n", dv
);
247 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
248 pr_err("cannot find super block on %s\n", dv
);
253 info
.array
.raid_disks
= nd
+1;
254 info
.array
.nr_disks
= nd
+1;
255 info
.array
.active_disks
= nd
+1;
256 info
.array
.working_disks
= nd
+1;
258 if (st
->ss
->update_super(st
, &info
, "linear-grow-update", dv
,
260 pr_err("Updating metadata failed on %s\n", dv
);
265 if (st
->ss
->store_super(st
, fd2
)) {
266 pr_err("Cannot store new superblock on %s\n", dv
);
276 int Grow_addbitmap(char *devname
, int fd
, struct context
*c
, struct shape
*s
)
279 * First check that array doesn't have a bitmap
280 * Then create the bitmap
283 * For internal bitmaps, we need to check the version,
284 * find all the active devices, and write the bitmap block
287 mdu_bitmap_file_t bmf
;
288 mdu_array_info_t array
;
289 struct supertype
*st
;
290 char *subarray
= NULL
;
291 int major
= BITMAP_MAJOR_HI
;
292 unsigned long long bitmapsize
, array_size
;
296 * We only ever get called if s->bitmap_file is != NULL, so this check
297 * is just here to quiet down static code checkers.
302 if (strcmp(s
->bitmap_file
, "clustered") == 0)
303 major
= BITMAP_MAJOR_CLUSTERED
;
305 if (ioctl(fd
, GET_BITMAP_FILE
, &bmf
) != 0) {
307 pr_err("Memory allocation failure.\n");
309 pr_err("bitmaps not supported by this kernel.\n");
312 if (bmf
.pathname
[0]) {
313 if (strcmp(s
->bitmap_file
,"none") == 0) {
314 if (ioctl(fd
, SET_BITMAP_FILE
, -1) != 0) {
315 pr_err("failed to remove bitmap %s\n",
321 pr_err("%s already has a bitmap (%s)\n", devname
, bmf
.pathname
);
324 if (md_get_array_info(fd
, &array
) != 0) {
325 pr_err("cannot get array status for %s\n", devname
);
328 if (array
.state
& (1 << MD_SB_BITMAP_PRESENT
)) {
329 if (strcmp(s
->bitmap_file
, "none")==0) {
330 array
.state
&= ~(1 << MD_SB_BITMAP_PRESENT
);
331 if (md_set_array_info(fd
, &array
) != 0) {
332 if (array
.state
& (1 << MD_SB_CLUSTERED
))
333 pr_err("failed to remove clustered bitmap.\n");
335 pr_err("failed to remove internal bitmap.\n");
340 pr_err("bitmap already present on %s\n", devname
);
344 if (strcmp(s
->bitmap_file
, "none") == 0) {
345 pr_err("no bitmap found on %s\n", devname
);
348 if (array
.level
<= 0) {
349 pr_err("Bitmaps not meaningful with level %s\n",
350 map_num(pers
, array
.level
)?:"of this array");
353 bitmapsize
= array
.size
;
355 if (get_dev_size(fd
, NULL
, &array_size
) &&
356 array_size
> (0x7fffffffULL
<< 9)) {
357 /* Array is big enough that we cannot trust array.size
358 * try other approaches
360 bitmapsize
= get_component_size(fd
);
362 if (bitmapsize
== 0) {
363 pr_err("Cannot reliably determine size of array to create bitmap - sorry.\n");
367 if (array
.level
== 10) {
370 ncopies
= (array
.layout
& 255) * ((array
.layout
>> 8) & 255);
371 bitmapsize
= bitmapsize
* array
.raid_disks
/ ncopies
;
373 if (strcmp(s
->bitmap_file
, "clustered") == 0 &&
374 !is_near_layout_10(array
.layout
)) {
375 pr_err("only near layout is supported with clustered raid10\n");
380 st
= super_by_fd(fd
, &subarray
);
382 pr_err("Cannot understand version %d.%d\n",
383 array
.major_version
, array
.minor_version
);
387 pr_err("Cannot add bitmaps to sub-arrays yet\n");
393 mdi
= sysfs_read(fd
, NULL
, GET_CONSISTENCY_POLICY
);
395 if (mdi
->consistency_policy
== CONSISTENCY_POLICY_PPL
) {
396 pr_err("Cannot add bitmap to array with PPL\n");
404 if (strcmp(s
->bitmap_file
, "internal") == 0 ||
405 strcmp(s
->bitmap_file
, "clustered") == 0) {
408 int offset_setable
= 0;
409 if (st
->ss
->add_internal_bitmap
== NULL
) {
410 pr_err("Internal bitmaps not supported with %s metadata\n", st
->ss
->name
);
413 st
->nodes
= c
->nodes
;
414 st
->cluster_name
= c
->homecluster
;
415 mdi
= sysfs_read(fd
, NULL
, GET_BITMAP_LOCATION
);
418 for (d
= 0; d
< st
->max_devs
; d
++) {
419 mdu_disk_info_t disk
;
424 if (md_get_disk_info(fd
, &disk
) < 0)
426 if (disk
.major
== 0 && disk
.minor
== 0)
428 if ((disk
.state
& (1 << MD_DISK_SYNC
)) == 0)
430 dv
= map_dev(disk
.major
, disk
.minor
, 1);
433 if (((disk
.state
& (1 << MD_DISK_WRITEMOSTLY
)) == 0) &&
434 (strcmp(s
->bitmap_file
, "clustered") == 0)) {
435 pr_err("%s disks marked write-mostly are not supported with clustered bitmap\n",devname
);
438 fd2
= dev_open(dv
, O_RDWR
);
441 rv
= st
->ss
->load_super(st
, fd2
, NULL
);
443 rv
= st
->ss
->add_internal_bitmap(
444 st
, &s
->bitmap_chunk
, c
->delay
,
445 s
->write_behind
, bitmapsize
,
446 offset_setable
, major
);
448 st
->ss
->write_bitmap(st
, fd2
,
451 pr_err("failed to create internal bitmap - chunksize problem.\n");
454 pr_err("failed to load super-block.\n");
460 if (offset_setable
) {
461 st
->ss
->getinfo_super(st
, mdi
, NULL
);
462 if (sysfs_init(mdi
, fd
, NULL
)) {
463 pr_err("failed to initialize sysfs.\n");
466 rv
= sysfs_set_num_signed(mdi
, NULL
, "bitmap/location",
470 if (strcmp(s
->bitmap_file
, "clustered") == 0)
471 array
.state
|= (1 << MD_SB_CLUSTERED
);
472 array
.state
|= (1 << MD_SB_BITMAP_PRESENT
);
473 rv
= md_set_array_info(fd
, &array
);
477 pr_err("Cannot add bitmap while array is resyncing or reshaping etc.\n");
478 pr_err("failed to set internal bitmap.\n");
485 int max_devs
= st
->max_devs
;
487 /* try to load a superblock */
488 for (d
= 0; d
< max_devs
; d
++) {
489 mdu_disk_info_t disk
;
493 if (md_get_disk_info(fd
, &disk
) < 0)
495 if ((disk
.major
==0 && disk
.minor
== 0) ||
496 (disk
.state
& (1 << MD_DISK_REMOVED
)))
498 dv
= map_dev(disk
.major
, disk
.minor
, 1);
501 fd2
= dev_open(dv
, O_RDONLY
);
503 if (st
->ss
->load_super(st
, fd2
, NULL
) == 0) {
505 st
->ss
->uuid_from_super(st
, uuid
);
512 pr_err("cannot find UUID for array!\n");
515 if (CreateBitmap(s
->bitmap_file
, c
->force
, (char*)uuid
,
516 s
->bitmap_chunk
, c
->delay
, s
->write_behind
,
517 bitmapsize
, major
)) {
520 bitmap_fd
= open(s
->bitmap_file
, O_RDWR
);
522 pr_err("weird: %s cannot be opened\n", s
->bitmap_file
);
525 if (ioctl(fd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
528 pr_err("Cannot add bitmap while array is resyncing or reshaping etc.\n");
529 pr_err("Cannot set bitmap file for %s: %s\n",
530 devname
, strerror(err
));
538 int Grow_consistency_policy(char *devname
, int fd
, struct context
*c
, struct shape
*s
)
540 struct supertype
*st
;
543 char *subarray
= NULL
;
545 char container_dev
[PATH_MAX
];
548 if (s
->consistency_policy
!= CONSISTENCY_POLICY_RESYNC
&&
549 s
->consistency_policy
!= CONSISTENCY_POLICY_PPL
) {
550 pr_err("Operation not supported for consistency policy %s\n",
551 map_num(consistency_policies
, s
->consistency_policy
));
555 st
= super_by_fd(fd
, &subarray
);
559 sra
= sysfs_read(fd
, NULL
, GET_CONSISTENCY_POLICY
|GET_LEVEL
|
566 if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
567 !st
->ss
->write_init_ppl
) {
568 pr_err("%s metadata does not support PPL\n", st
->ss
->name
);
573 if (sra
->array
.level
!= 5) {
574 pr_err("Operation not supported for array level %d\n",
580 if (sra
->consistency_policy
== (unsigned)s
->consistency_policy
) {
581 pr_err("Consistency policy is already %s\n",
582 map_num(consistency_policies
, s
->consistency_policy
));
585 } else if (sra
->consistency_policy
!= CONSISTENCY_POLICY_RESYNC
&&
586 sra
->consistency_policy
!= CONSISTENCY_POLICY_PPL
) {
587 pr_err("Current consistency policy is %s, cannot change to %s\n",
588 map_num(consistency_policies
, sra
->consistency_policy
),
589 map_num(consistency_policies
, s
->consistency_policy
));
594 if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
) {
595 if (sysfs_get_str(sra
, NULL
, "sync_action", buf
, 20) <= 0) {
598 } else if (strcmp(buf
, "reshape\n") == 0) {
599 pr_err("PPL cannot be enabled when reshape is in progress\n");
608 if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
)
613 sprintf(container_dev
, "/dev/%s", st
->container_devnm
);
615 ret
= Update_subarray(container_dev
, subarray
, update
, NULL
,
621 if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
) {
628 cfd
= open(container_dev
, O_RDWR
|O_EXCL
);
630 pr_err("Failed to open %s\n", container_dev
);
635 ret
= st
->ss
->load_container(st
, cfd
, st
->container_devnm
);
639 pr_err("Cannot read superblock for %s\n",
644 mdi
= st
->ss
->container_content(st
, subarray
);
649 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
653 devpath
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
654 dfd
= dev_open(devpath
, O_RDWR
);
656 pr_err("Failed to open %s\n", devpath
);
662 ret
= st
->ss
->load_super(st
, dfd
, NULL
);
664 pr_err("Failed to load super-block.\n");
669 ret
= st
->ss
->update_super(st
, sra
, "ppl",
671 c
->verbose
, 0, NULL
);
674 st
->ss
->free_super(st
);
677 st
->ss
->getinfo_super(st
, &info
, NULL
);
680 ret
|= sysfs_set_num(sra
, sd
, "ppl_sector",
682 ret
|= sysfs_set_num(sra
, sd
, "ppl_size",
686 pr_err("Failed to set PPL attributes for %s\n",
689 st
->ss
->free_super(st
);
693 ret
= st
->ss
->write_init_ppl(st
, &info
, dfd
);
695 pr_err("Failed to write PPL\n");
700 st
->ss
->free_super(st
);
707 ret
= sysfs_set_str(sra
, NULL
, "consistency_policy",
708 map_num(consistency_policies
,
709 s
->consistency_policy
));
711 pr_err("Failed to change array consistency policy\n");
723 * When reshaping an array we might need to backup some data.
724 * This is written to all spares with a 'super_block' describing it.
725 * The superblock goes 4K from the end of the used space on the
727 * It if written after the backup is complete.
728 * It has the following structure.
731 static struct mdp_backup_super
{
732 char magic
[16]; /* md_backup_data-1 or -2 */
735 /* start/sizes in 512byte sectors */
736 __u64 devstart
; /* address on backup device/file of data */
739 __u32 sb_csum
; /* csum of preceeding bytes. */
741 __u64 devstart2
; /* offset in to data of second section */
744 __u32 sb_csum2
; /* csum of preceeding bytes. */
746 } __attribute__((aligned(512))) bsb
, bsb2
;
748 static __u32
bsb_csum(char *buf
, int len
)
752 for (i
= 0; i
< len
; i
++)
753 csum
= (csum
<<3) + buf
[0];
754 return __cpu_to_le32(csum
);
757 static int check_idle(struct supertype
*st
)
759 /* Check that all member arrays for this container, or the
760 * container of this array, are idle
762 char *container
= (st
->container_devnm
[0]
763 ? st
->container_devnm
: st
->devnm
);
764 struct mdstat_ent
*ent
, *e
;
767 ent
= mdstat_read(0, 0);
768 for (e
= ent
; e
; e
= e
->next
) {
769 if (!is_container_member(e
, container
))
771 /* frozen array is not idle*/
772 if (e
->percent
>= 0 || e
->metadata_version
[9] == '-') {
781 static int freeze_container(struct supertype
*st
)
783 char *container
= (st
->container_devnm
[0]
784 ? st
->container_devnm
: st
->devnm
);
789 if (block_monitor(container
, 1)) {
790 pr_err("failed to freeze container\n");
797 static void unfreeze_container(struct supertype
*st
)
799 char *container
= (st
->container_devnm
[0]
800 ? st
->container_devnm
: st
->devnm
);
802 unblock_monitor(container
, 1);
805 static int freeze(struct supertype
*st
)
807 /* Try to freeze resync/rebuild on this array/container.
808 * Return -1 if the array is busy,
809 * return -2 container cannot be frozen,
810 * return 0 if this kernel doesn't support 'frozen'
811 * return 1 if it worked.
813 if (st
->ss
->external
)
814 return freeze_container(st
);
816 struct mdinfo
*sra
= sysfs_read(-1, st
->devnm
, GET_VERSION
);
822 /* Need to clear any 'read-auto' status */
823 if (sysfs_get_str(sra
, NULL
, "array_state", buf
, 20) > 0 &&
824 strncmp(buf
, "read-auto", 9) == 0)
825 sysfs_set_str(sra
, NULL
, "array_state", "clean");
827 err
= sysfs_freeze_array(sra
);
833 static void unfreeze(struct supertype
*st
)
835 if (st
->ss
->external
)
836 return unfreeze_container(st
);
838 struct mdinfo
*sra
= sysfs_read(-1, st
->devnm
, GET_VERSION
);
842 sysfs_get_str(sra
, NULL
, "sync_action", buf
, 20) > 0 &&
843 strcmp(buf
, "frozen\n") == 0)
844 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
849 static void wait_reshape(struct mdinfo
*sra
)
851 int fd
= sysfs_get_fd(sra
, NULL
, "sync_action");
857 while (sysfs_fd_get_str(fd
, action
, 20) > 0 &&
858 strncmp(action
, "reshape", 7) == 0)
859 sysfs_wait(fd
, NULL
);
863 static int reshape_super(struct supertype
*st
, unsigned long long size
,
864 int level
, int layout
, int chunksize
, int raid_disks
,
865 int delta_disks
, char *backup_file
, char *dev
,
866 int direction
, int verbose
)
868 /* nothing extra to check in the native case */
869 if (!st
->ss
->external
)
871 if (!st
->ss
->reshape_super
|| !st
->ss
->manage_reshape
) {
872 pr_err("%s metadata does not support reshape\n",
877 return st
->ss
->reshape_super(st
, size
, level
, layout
, chunksize
,
878 raid_disks
, delta_disks
, backup_file
, dev
,
882 static void sync_metadata(struct supertype
*st
)
884 if (st
->ss
->external
) {
885 if (st
->update_tail
) {
886 flush_metadata_updates(st
);
887 st
->update_tail
= &st
->updates
;
889 st
->ss
->sync_metadata(st
);
893 static int subarray_set_num(char *container
, struct mdinfo
*sra
, char *name
, int n
)
895 /* when dealing with external metadata subarrays we need to be
896 * prepared to handle EAGAIN. The kernel may need to wait for
897 * mdmon to mark the array active so the kernel can handle
898 * allocations/writeback when preparing the reshape action
899 * (md_allow_write()). We temporarily disable safe_mode_delay
900 * to close a race with the array_state going clean before the
901 * next write to raid_disks / stripe_cache_size
906 /* only 'raid_disks' and 'stripe_cache_size' trigger md_allow_write */
908 (strcmp(name
, "raid_disks") != 0 &&
909 strcmp(name
, "stripe_cache_size") != 0))
910 return sysfs_set_num(sra
, NULL
, name
, n
);
912 rc
= sysfs_get_str(sra
, NULL
, "safe_mode_delay", safe
, sizeof(safe
));
915 sysfs_set_num(sra
, NULL
, "safe_mode_delay", 0);
916 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
917 if (rc
< 0 && errno
== EAGAIN
) {
918 ping_monitor(container
);
919 /* if we get EAGAIN here then the monitor is not active
922 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
924 sysfs_set_str(sra
, NULL
, "safe_mode_delay", safe
);
928 int start_reshape(struct mdinfo
*sra
, int already_running
,
929 int before_data_disks
, int data_disks
, struct supertype
*st
)
932 unsigned long long sync_max_to_set
;
934 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
935 err
= sysfs_set_num(sra
, NULL
, "suspend_hi", sra
->reshape_progress
);
936 err
= err
?: sysfs_set_num(sra
, NULL
, "suspend_lo",
937 sra
->reshape_progress
);
938 if (before_data_disks
<= data_disks
)
939 sync_max_to_set
= sra
->reshape_progress
/ data_disks
;
941 sync_max_to_set
= (sra
->component_size
* data_disks
942 - sra
->reshape_progress
) / data_disks
;
944 if (!already_running
)
945 sysfs_set_num(sra
, NULL
, "sync_min", sync_max_to_set
);
947 if (st
->ss
->external
)
948 err
= err
?: sysfs_set_num(sra
, NULL
, "sync_max", sync_max_to_set
);
950 err
= err
?: sysfs_set_str(sra
, NULL
, "sync_max", "max");
952 if (!already_running
&& err
== 0) {
955 err
= sysfs_set_str(sra
, NULL
, "sync_action",
959 } while (err
&& errno
== EBUSY
&& cnt
-- > 0);
964 void abort_reshape(struct mdinfo
*sra
)
966 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
968 * Prior to kernel commit: 23ddff3792f6 ("md: allow suspend_lo and
969 * suspend_hi to decrease as well as increase.")
970 * you could only increase suspend_{lo,hi} unless the region they
971 * covered was empty. So to reset to 0, you need to push suspend_lo
972 * up past suspend_hi first. So to maximize the chance of mdadm
973 * working on all kernels, we want to keep doing that.
975 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
976 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
977 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
978 sysfs_set_num(sra
, NULL
, "sync_min", 0);
979 // It isn't safe to reset sync_max as we aren't monitoring.
980 // Array really should be stopped at this point.
983 int remove_disks_for_takeover(struct supertype
*st
,
988 struct mdinfo
*remaining
;
991 if (st
->ss
->external
) {
993 struct mdinfo
*arrays
= st
->ss
->container_content(st
, NULL
);
995 * containter_content returns list of arrays in container
996 * If arrays->next is not NULL it means that there are
997 * 2 arrays in container and operation should be blocked
1004 pr_err("Error. Cannot perform operation on %s- for this operation "
1005 "it MUST be single array in container\n", st
->devnm
);
1011 if (sra
->array
.level
== 10)
1012 nr_of_copies
= layout
& 0xff;
1013 else if (sra
->array
.level
== 1)
1014 nr_of_copies
= sra
->array
.raid_disks
;
1018 remaining
= sra
->devs
;
1020 /* for each 'copy', select one device and remove from the list. */
1021 for (slot
= 0; slot
< sra
->array
.raid_disks
; slot
+= nr_of_copies
) {
1022 struct mdinfo
**diskp
;
1025 /* Find a working device to keep */
1026 for (diskp
= &remaining
; *diskp
; diskp
= &(*diskp
)->next
) {
1027 struct mdinfo
*disk
= *diskp
;
1029 if (disk
->disk
.raid_disk
< slot
)
1031 if (disk
->disk
.raid_disk
>= slot
+ nr_of_copies
)
1033 if (disk
->disk
.state
& (1<<MD_DISK_REMOVED
))
1035 if (disk
->disk
.state
& (1<<MD_DISK_FAULTY
))
1037 if (!(disk
->disk
.state
& (1<<MD_DISK_SYNC
)))
1040 /* We have found a good disk to use! */
1041 *diskp
= disk
->next
;
1042 disk
->next
= sra
->devs
;
1051 if (slot
< sra
->array
.raid_disks
) {
1052 /* didn't find all slots */
1058 sra
->devs
= remaining
;
1062 /* Remove all 'remaining' devices from the array */
1064 struct mdinfo
*sd
= remaining
;
1065 remaining
= sd
->next
;
1067 sysfs_set_str(sra
, sd
, "state", "faulty");
1068 sysfs_set_str(sra
, sd
, "slot", "none");
1069 /* for external metadata disks should be removed in mdmon */
1070 if (!st
->ss
->external
)
1071 sysfs_set_str(sra
, sd
, "state", "remove");
1072 sd
->disk
.state
|= (1<<MD_DISK_REMOVED
);
1073 sd
->disk
.state
&= ~(1<<MD_DISK_SYNC
);
1074 sd
->next
= sra
->devs
;
1080 void reshape_free_fdlist(int *fdlist
,
1081 unsigned long long *offsets
,
1086 for (i
= 0; i
< size
; i
++)
1094 int reshape_prepare_fdlist(char *devname
,
1098 unsigned long blocks
,
1101 unsigned long long *offsets
)
1106 enable_fds(nrdisks
);
1107 for (d
= 0; d
<= nrdisks
; d
++)
1110 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1111 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1113 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
) &&
1114 sd
->disk
.raid_disk
< raid_disks
) {
1115 char *dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
1116 fdlist
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDONLY
);
1117 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
1118 if (fdlist
[sd
->disk
.raid_disk
] < 0) {
1119 pr_err("%s: cannot open component %s\n",
1120 devname
, dn
? dn
: "-unknown-");
1124 } else if (backup_file
== NULL
) {
1126 char *dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
1127 fdlist
[d
] = dev_open(dn
, O_RDWR
);
1128 offsets
[d
] = (sd
->data_offset
+ sra
->component_size
- blocks
- 8)*512;
1129 if (fdlist
[d
] < 0) {
1130 pr_err("%s: cannot open component %s\n",
1131 devname
, dn
? dn
: "-unknown-");
1142 int reshape_open_backup_file(char *backup_file
,
1147 unsigned long long *offsets
,
1151 /* Return 1 on success, 0 on any form of failure */
1152 /* need to check backup file is large enough */
1158 *fdlist
= open(backup_file
, O_RDWR
|O_CREAT
|(restart
? O_TRUNC
: O_EXCL
),
1162 pr_err("%s: cannot create backup file %s: %s\n",
1163 devname
, backup_file
, strerror(errno
));
1166 /* Guard against backup file being on array device.
1167 * If array is partitioned or if LVM etc is in the
1168 * way this will not notice, but it is better than
1171 fstat(*fdlist
, &stb
);
1174 if (stb
.st_rdev
== dev
) {
1175 pr_err("backup file must NOT be on the array being reshaped.\n");
1180 memset(buf
, 0, 512);
1181 for (i
=0; i
< blocks
+ 8 ; i
++) {
1182 if (write(*fdlist
, buf
, 512) != 512) {
1183 pr_err("%s: cannot create backup file %s: %s\n",
1184 devname
, backup_file
, strerror(errno
));
1188 if (fsync(*fdlist
) != 0) {
1189 pr_err("%s: cannot create backup file %s: %s\n",
1190 devname
, backup_file
, strerror(errno
));
1194 if (!restart
&& strncmp(backup_file
, MAP_DIR
, strlen(MAP_DIR
)) != 0) {
1195 char *bu
= make_backup(sys_name
);
1196 if (symlink(backup_file
, bu
))
1197 pr_err("Recording backup file in " MAP_DIR
" failed: %s\n",
1205 unsigned long compute_backup_blocks(int nchunk
, int ochunk
,
1206 unsigned int ndata
, unsigned int odata
)
1208 unsigned long a
, b
, blocks
;
1209 /* So how much do we need to backup.
1210 * We need an amount of data which is both a whole number of
1211 * old stripes and a whole number of new stripes.
1212 * So LCM for (chunksize*datadisks).
1214 a
= (ochunk
/512) * odata
;
1215 b
= (nchunk
/512) * ndata
;
1218 /* LCM == product / GCD */
1219 blocks
= (unsigned long)(ochunk
/512) * (unsigned long)(nchunk
/512) *
1225 char *analyse_change(char *devname
, struct mdinfo
*info
, struct reshape
*re
)
1227 /* Based on the current array state in info->array and
1228 * the changes in info->new_* etc, determine:
1229 * - whether the change is possible
1230 * - Intermediate level/raid_disks/layout
1231 * - whether a restriping reshape is needed
1232 * - number of sectors in minimum change unit. This
1233 * will cover a whole number of stripes in 'before' and
1236 * Return message if the change should be rejected
1237 * NULL if the change can be achieved
1239 * This can be called as part of starting a reshape, or
1240 * when assembling an array that is undergoing reshape.
1242 int near
, far
, offset
, copies
;
1244 int old_chunk
, new_chunk
;
1245 /* delta_parity records change in number of devices
1246 * caused by level change
1248 int delta_parity
= 0;
1250 memset(re
, 0, sizeof(*re
));
1252 /* If a new level not explicitly given, we assume no-change */
1253 if (info
->new_level
== UnSet
)
1254 info
->new_level
= info
->array
.level
;
1256 if (info
->new_chunk
)
1257 switch (info
->new_level
) {
1263 /* chunk size is meaningful, must divide component_size
1266 if (info
->component_size
% (info
->new_chunk
/512)) {
1267 unsigned long long shrink
= info
->component_size
;
1268 shrink
&= ~(unsigned long long)(info
->new_chunk
/512-1);
1269 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk)\n",
1270 info
->new_chunk
/1024, info
->component_size
/2);
1271 pr_err("After shrinking any filesystem, \"mdadm --grow %s --size %llu\"\n",
1273 pr_err("will shrink the array so the given chunk size would work.\n");
1278 return "chunk size not meaningful for this level";
1281 info
->new_chunk
= info
->array
.chunk_size
;
1283 switch (info
->array
.level
) {
1285 return "No reshape is possibly for this RAID level";
1287 if (info
->delta_disks
!= UnSet
)
1288 return "Only --add is supported for LINEAR, setting --raid-disks is not needed";
1290 return "Only --add is supported for LINEAR, other --grow options are not meaningful";
1292 /* RAID1 can convert to RAID1 with different disks, or
1293 * raid5 with 2 disks, or
1296 if (info
->new_level
> 1 && (info
->component_size
& 7))
1297 return "Cannot convert RAID1 of this size - reduce size to multiple of 4K first.";
1298 if (info
->new_level
== 0) {
1299 if (info
->delta_disks
!= UnSet
&&
1300 info
->delta_disks
!= 0)
1301 return "Cannot change number of disks with RAID1->RAID0 conversion";
1303 re
->before
.data_disks
= 1;
1304 re
->after
.data_disks
= 1;
1307 if (info
->new_level
== 1) {
1308 if (info
->delta_disks
== UnSet
)
1309 /* Don't know what to do */
1310 return "no change requested for Growing RAID1";
1314 if (info
->array
.raid_disks
!= 2 && info
->new_level
== 5)
1315 return "Can only convert a 2-device array to RAID5";
1316 if (info
->array
.raid_disks
== 2 && info
->new_level
== 5) {
1318 re
->before
.data_disks
= 1;
1319 if (info
->delta_disks
!= UnSet
&&
1320 info
->delta_disks
!= 0)
1321 re
->after
.data_disks
= 1 + info
->delta_disks
;
1323 re
->after
.data_disks
= 1;
1324 if (re
->after
.data_disks
< 1)
1325 return "Number of disks too small for RAID5";
1327 re
->before
.layout
= ALGORITHM_LEFT_SYMMETRIC
;
1328 info
->array
.chunk_size
= 65536;
1331 /* Could do some multi-stage conversions, but leave that to
1334 return "Impossibly level change request for RAID1";
1337 /* RAID10 can be converted from near mode to
1338 * RAID0 by removing some devices.
1339 * It can also be reshaped if the kernel supports
1342 switch (info
->new_level
) {
1344 if ((info
->array
.layout
& ~0xff) != 0x100)
1345 return "Cannot Grow RAID10 with far/offset layout";
1347 * number of devices must be multiple of
1350 if (info
->array
.raid_disks
%
1351 (info
->array
.layout
& 0xff))
1352 return "RAID10 layout too complex for Grow operation";
1354 new_disks
= (info
->array
.raid_disks
/
1355 (info
->array
.layout
& 0xff));
1356 if (info
->delta_disks
== UnSet
)
1357 info
->delta_disks
= (new_disks
1358 - info
->array
.raid_disks
);
1360 if (info
->delta_disks
!=
1361 new_disks
- info
->array
.raid_disks
)
1362 return "New number of raid-devices impossible for RAID10";
1363 if (info
->new_chunk
&&
1364 info
->new_chunk
!= info
->array
.chunk_size
)
1365 return "Cannot change chunk-size with RAID10 Grow";
1369 re
->before
.data_disks
= new_disks
;
1370 re
->after
.data_disks
= re
->before
.data_disks
;
1374 near
= info
->array
.layout
& 0xff;
1375 far
= (info
->array
.layout
>> 8) & 0xff;
1376 offset
= info
->array
.layout
& 0x10000;
1377 if (far
> 1 && !offset
)
1378 return "Cannot reshape RAID10 in far-mode";
1379 copies
= near
* far
;
1381 old_chunk
= info
->array
.chunk_size
* far
;
1383 if (info
->new_layout
== UnSet
)
1384 info
->new_layout
= info
->array
.layout
;
1386 near
= info
->new_layout
& 0xff;
1387 far
= (info
->new_layout
>> 8) & 0xff;
1388 offset
= info
->new_layout
& 0x10000;
1389 if (far
> 1 && !offset
)
1390 return "Cannot reshape RAID10 to far-mode";
1391 if (near
* far
!= copies
)
1392 return "Cannot change number of copies when reshaping RAID10";
1394 if (info
->delta_disks
== UnSet
)
1395 info
->delta_disks
= 0;
1396 new_disks
= (info
->array
.raid_disks
+
1399 new_chunk
= info
->new_chunk
* far
;
1402 re
->before
.layout
= info
->array
.layout
;
1403 re
->before
.data_disks
= info
->array
.raid_disks
;
1404 re
->after
.layout
= info
->new_layout
;
1405 re
->after
.data_disks
= new_disks
;
1406 /* For RAID10 we don't do backup but do allow reshape,
1407 * so set backup_blocks to INVALID_SECTORS rather than
1409 * And there is no need to synchronise stripes on both
1410 * 'old' and 'new'. So the important
1411 * number is the minimum data_offset difference
1412 * which is the larger of (offset copies * chunk).
1414 re
->backup_blocks
= INVALID_SECTORS
;
1415 re
->min_offset_change
= max(old_chunk
, new_chunk
) / 512;
1416 if (new_disks
< re
->before
.data_disks
&&
1417 info
->space_after
< re
->min_offset_change
)
1418 /* Reduce component size by one chunk */
1419 re
->new_size
= (info
->component_size
-
1420 re
->min_offset_change
);
1422 re
->new_size
= info
->component_size
;
1423 re
->new_size
= re
->new_size
* new_disks
/ copies
;
1427 return "RAID10 can only be changed to RAID0";
1430 /* RAID0 can be converted to RAID10, or to RAID456 */
1431 if (info
->new_level
== 10) {
1432 if (info
->new_layout
== UnSet
&&
1433 info
->delta_disks
== UnSet
) {
1434 /* Assume near=2 layout */
1435 info
->new_layout
= 0x102;
1436 info
->delta_disks
= info
->array
.raid_disks
;
1438 if (info
->new_layout
== UnSet
) {
1439 int copies
= 1 + (info
->delta_disks
1440 / info
->array
.raid_disks
);
1441 if (info
->array
.raid_disks
* (copies
-1) !=
1443 return "Impossible number of devices for RAID0->RAID10";
1444 info
->new_layout
= 0x100 + copies
;
1446 if (info
->delta_disks
== UnSet
) {
1447 int copies
= info
->new_layout
& 0xff;
1448 if (info
->new_layout
!= 0x100 + copies
)
1449 return "New layout impossible for RAID0->RAID10";;
1450 info
->delta_disks
= (copies
- 1) *
1451 info
->array
.raid_disks
;
1453 if (info
->new_chunk
&&
1454 info
->new_chunk
!= info
->array
.chunk_size
)
1455 return "Cannot change chunk-size with RAID0->RAID10";
1458 re
->before
.data_disks
= (info
->array
.raid_disks
+
1460 re
->after
.data_disks
= re
->before
.data_disks
;
1461 re
->before
.layout
= info
->new_layout
;
1465 /* RAID0 can also covert to RAID0/4/5/6 by first converting to
1466 * a raid4 style layout of the final level.
1468 switch (info
->new_level
) {
1473 re
->before
.layout
= 0;
1478 re
->before
.layout
= ALGORITHM_PARITY_N
;
1479 if (info
->new_layout
== UnSet
)
1480 info
->new_layout
= map_name(r5layout
, "default");
1485 re
->before
.layout
= ALGORITHM_PARITY_N
;
1486 if (info
->new_layout
== UnSet
)
1487 info
->new_layout
= map_name(r6layout
, "default");
1490 return "Impossible level change requested";
1492 re
->before
.data_disks
= info
->array
.raid_disks
;
1493 /* determining 'after' layout happens outside this 'switch' */
1497 info
->array
.layout
= ALGORITHM_PARITY_N
;
1499 switch (info
->new_level
) {
1503 re
->level
= info
->array
.level
;
1504 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1505 re
->before
.layout
= info
->array
.layout
;
1509 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1510 re
->before
.layout
= info
->array
.layout
;
1515 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1516 switch (info
->array
.layout
) {
1517 case ALGORITHM_LEFT_ASYMMETRIC
:
1518 re
->before
.layout
= ALGORITHM_LEFT_ASYMMETRIC_6
;
1520 case ALGORITHM_RIGHT_ASYMMETRIC
:
1521 re
->before
.layout
= ALGORITHM_RIGHT_ASYMMETRIC_6
;
1523 case ALGORITHM_LEFT_SYMMETRIC
:
1524 re
->before
.layout
= ALGORITHM_LEFT_SYMMETRIC_6
;
1526 case ALGORITHM_RIGHT_SYMMETRIC
:
1527 re
->before
.layout
= ALGORITHM_RIGHT_SYMMETRIC_6
;
1529 case ALGORITHM_PARITY_0
:
1530 re
->before
.layout
= ALGORITHM_PARITY_0_6
;
1532 case ALGORITHM_PARITY_N
:
1533 re
->before
.layout
= ALGORITHM_PARITY_N_6
;
1536 return "Cannot convert an array with this layout";
1540 if (info
->array
.raid_disks
!= 2)
1541 return "Can only convert a 2-device array to RAID1";
1542 if (info
->delta_disks
!= UnSet
&&
1543 info
->delta_disks
!= 0)
1544 return "Cannot set raid_disk when converting RAID5->RAID1";
1546 info
->new_chunk
= 0;
1549 return "Impossible level change requested";
1553 switch (info
->new_level
) {
1559 re
->before
.data_disks
= info
->array
.raid_disks
- 2;
1560 re
->before
.layout
= info
->array
.layout
;
1563 return "Impossible level change requested";
1568 /* If we reached here then it looks like a re-stripe is
1569 * happening. We have determined the intermediate level
1570 * and initial raid_disks/layout and stored these in 're'.
1572 * We need to deduce the final layout that can be atomically
1573 * converted to the end state.
1575 switch (info
->new_level
) {
1577 /* We can only get to RAID0 from RAID4 or RAID5
1578 * with appropriate layout and one extra device
1580 if (re
->level
!= 4 && re
->level
!= 5)
1581 return "Cannot covert to RAID0 from this level";
1583 switch (re
->level
) {
1585 re
->before
.layout
= 0;
1586 re
->after
.layout
= 0;
1589 re
->after
.layout
= ALGORITHM_PARITY_N
;
1595 /* We can only get to RAID4 from RAID5 */
1596 if (re
->level
!= 4 && re
->level
!= 5)
1597 return "Cannot convert to RAID4 from this level";
1599 switch (re
->level
) {
1601 re
->after
.layout
= 0;
1604 re
->after
.layout
= ALGORITHM_PARITY_N
;
1610 /* We get to RAID5 from RAID5 or RAID6 */
1611 if (re
->level
!= 5 && re
->level
!= 6)
1612 return "Cannot convert to RAID5 from this level";
1614 switch (re
->level
) {
1616 if (info
->new_layout
== UnSet
)
1617 re
->after
.layout
= re
->before
.layout
;
1619 re
->after
.layout
= info
->new_layout
;
1622 if (info
->new_layout
== UnSet
)
1623 info
->new_layout
= re
->before
.layout
;
1625 /* after.layout needs to be raid6 version of new_layout */
1626 if (info
->new_layout
== ALGORITHM_PARITY_N
)
1627 re
->after
.layout
= ALGORITHM_PARITY_N
;
1630 char *ls
= map_num(r5layout
, info
->new_layout
);
1633 /* Current RAID6 layout has a RAID5
1636 strcat(strcpy(layout
, ls
), "-6");
1637 l
= map_name(r6layout
, layout
);
1639 return "Cannot find RAID6 layout to convert to";
1641 /* Current RAID6 has no equivalent.
1642 * If it is already a '-6' layout we
1643 * can leave it unchanged, else we must
1646 ls
= map_num(r6layout
,
1649 strcmp(ls
+strlen(ls
)-2, "-6") != 0)
1650 return "Please specify new layout";
1651 l
= info
->new_layout
;
1653 re
->after
.layout
= l
;
1659 /* We must already be at level 6 */
1661 return "Impossible level change";
1662 if (info
->new_layout
== UnSet
)
1663 re
->after
.layout
= info
->array
.layout
;
1665 re
->after
.layout
= info
->new_layout
;
1668 return "Impossible level change requested";
1670 if (info
->delta_disks
== UnSet
)
1671 info
->delta_disks
= delta_parity
;
1673 re
->after
.data_disks
=
1674 (re
->before
.data_disks
+ info
->delta_disks
- delta_parity
);
1676 switch (re
->level
) {
1688 /* So we have a restripe operation, we need to calculate the number
1689 * of blocks per reshape operation.
1691 re
->new_size
= info
->component_size
* re
->before
.data_disks
;
1692 if (info
->new_chunk
== 0)
1693 info
->new_chunk
= info
->array
.chunk_size
;
1694 if (re
->after
.data_disks
== re
->before
.data_disks
&&
1695 re
->after
.layout
== re
->before
.layout
&&
1696 info
->new_chunk
== info
->array
.chunk_size
) {
1697 /* Nothing to change, can change level immediately. */
1698 re
->level
= info
->new_level
;
1699 re
->backup_blocks
= 0;
1702 if (re
->after
.data_disks
== 1 && re
->before
.data_disks
== 1) {
1703 /* chunk and layout changes make no difference */
1704 re
->level
= info
->new_level
;
1705 re
->backup_blocks
= 0;
1709 if (re
->after
.data_disks
== re
->before
.data_disks
&&
1710 get_linux_version() < 2006032)
1711 return "in-place reshape is not safe before 2.6.32 - sorry.";
1713 if (re
->after
.data_disks
< re
->before
.data_disks
&&
1714 get_linux_version() < 2006030)
1715 return "reshape to fewer devices is not supported before 2.6.30 - sorry.";
1717 re
->backup_blocks
= compute_backup_blocks(
1718 info
->new_chunk
, info
->array
.chunk_size
,
1719 re
->after
.data_disks
, re
->before
.data_disks
);
1720 re
->min_offset_change
= re
->backup_blocks
/ re
->before
.data_disks
;
1722 re
->new_size
= info
->component_size
* re
->after
.data_disks
;
1726 static int set_array_size(struct supertype
*st
, struct mdinfo
*sra
,
1729 struct mdinfo
*info
;
1733 if ((st
== NULL
) || (sra
== NULL
))
1736 if (text_version
== NULL
)
1737 text_version
= sra
->text_version
;
1738 subarray
= strchr(text_version
+ 1, '/')+1;
1739 info
= st
->ss
->container_content(st
, subarray
);
1741 unsigned long long current_size
= 0;
1742 unsigned long long new_size
= info
->custom_array_size
/2;
1744 if (sysfs_get_ll(sra
, NULL
, "array_size", ¤t_size
) == 0 &&
1745 new_size
> current_size
) {
1746 if (sysfs_set_num(sra
, NULL
, "array_size", new_size
)
1748 dprintf("Error: Cannot set array size");
1751 dprintf("Array size changed");
1753 dprintf_cont(" from %llu to %llu.\n",
1754 current_size
, new_size
);
1758 dprintf("Error: set_array_size(): info pointer in NULL\n");
1763 static int reshape_array(char *container
, int fd
, char *devname
,
1764 struct supertype
*st
, struct mdinfo
*info
,
1765 int force
, struct mddev_dev
*devlist
,
1766 unsigned long long data_offset
,
1767 char *backup_file
, int verbose
, int forked
,
1768 int restart
, int freeze_reshape
);
1769 static int reshape_container(char *container
, char *devname
,
1771 struct supertype
*st
,
1772 struct mdinfo
*info
,
1774 char *backup_file
, int verbose
,
1775 int forked
, int restart
, int freeze_reshape
);
1777 int Grow_reshape(char *devname
, int fd
,
1778 struct mddev_dev
*devlist
,
1779 unsigned long long data_offset
,
1780 struct context
*c
, struct shape
*s
)
1782 /* Make some changes in the shape of an array.
1783 * The kernel must support the change.
1785 * There are three different changes. Each can trigger
1786 * a resync or recovery so we freeze that until we have
1787 * requested everything (if kernel supports freezing - 2.6.30).
1789 * - change size (i.e. component_size)
1791 * - change layout/chunksize/ndisks
1793 * The last can require a reshape. It is different on different
1794 * levels so we need to check the level before actioning it.
1795 * Some times the level change needs to be requested after the
1796 * reshape (e.g. raid6->raid5, raid5->raid0)
1799 struct mdu_array_info_s array
;
1801 struct supertype
*st
;
1802 char *subarray
= NULL
;
1806 char *container
= NULL
;
1809 struct mddev_dev
*dv
;
1815 if (md_get_array_info(fd
, &array
) < 0) {
1816 pr_err("%s is not an active md array - aborting\n",
1820 if (s
->level
!= UnSet
&& s
->chunk
) {
1821 pr_err("Cannot change array level in the same operation as changing chunk size.\n");
1825 if (data_offset
!= INVALID_SECTORS
&& array
.level
!= 10 &&
1826 (array
.level
< 4 || array
.level
> 6)) {
1827 pr_err("--grow --data-offset not yet supported\n");
1832 (s
->chunk
|| s
->level
!= UnSet
|| s
->layout_str
|| s
->raiddisks
)) {
1833 pr_err("cannot change component size at the same time as other changes.\n"
1834 " Change size first, then check data is intact before making other changes.\n");
1838 if (s
->raiddisks
&& s
->raiddisks
< array
.raid_disks
&&
1839 array
.level
> 1 && get_linux_version() < 2006032 &&
1840 !check_env("MDADM_FORCE_FEWER")) {
1841 pr_err("reducing the number of devices is not safe before Linux 2.6.32\n"
1842 " Please use a newer kernel\n");
1846 if (array
.level
> 1 && s
->size
> 1 &&
1847 (unsigned long long) (array
.chunk_size
/ 1024) > s
->size
) {
1848 pr_err("component size must be larger than chunk size.\n");
1852 st
= super_by_fd(fd
, &subarray
);
1854 pr_err("Unable to determine metadata format for %s\n", devname
);
1857 if (s
->raiddisks
> st
->max_devs
) {
1858 pr_err("Cannot increase raid-disks on this array beyond %d\n", st
->max_devs
);
1861 if (s
->level
== 0 && (array
.state
& (1 << MD_SB_BITMAP_PRESENT
)) &&
1862 !(array
.state
& (1 << MD_SB_CLUSTERED
)) && !st
->ss
->external
) {
1863 array
.state
&= ~(1 << MD_SB_BITMAP_PRESENT
);
1864 if (md_set_array_info(fd
, &array
) != 0) {
1865 pr_err("failed to remove internal bitmap.\n");
1870 /* in the external case we need to check that the requested reshape is
1871 * supported, and perform an initial check that the container holds the
1872 * pre-requisite spare devices (mdmon owns final validation)
1874 if (st
->ss
->external
) {
1878 container
= st
->container_devnm
;
1879 cfd
= open_dev_excl(st
->container_devnm
);
1881 container
= st
->devnm
;
1883 cfd
= open_dev_excl(st
->devnm
);
1887 pr_err("Unable to open container for %s\n", devname
);
1892 retval
= st
->ss
->load_container(st
, cfd
, NULL
);
1895 pr_err("Cannot read superblock for %s\n", devname
);
1900 /* check if operation is supported for metadata handler */
1901 if (st
->ss
->container_content
) {
1902 struct mdinfo
*cc
= NULL
;
1903 struct mdinfo
*content
= NULL
;
1905 cc
= st
->ss
->container_content(st
, subarray
);
1906 for (content
= cc
; content
; content
= content
->next
) {
1907 int allow_reshape
= 1;
1909 /* check if reshape is allowed based on metadata
1910 * indications stored in content.array.status
1912 if (content
->array
.state
&
1913 (1 << MD_SB_BLOCK_VOLUME
))
1915 if (content
->array
.state
&
1916 (1 << MD_SB_BLOCK_CONTAINER_RESHAPE
))
1918 if (!allow_reshape
) {
1919 pr_err("cannot reshape arrays in container with unsupported metadata: %s(%s)\n",
1920 devname
, container
);
1925 if (content
->consistency_policy
==
1926 CONSISTENCY_POLICY_PPL
) {
1927 pr_err("Operation not supported when ppl consistency policy is enabled\n");
1932 if (content
->consistency_policy
==
1933 CONSISTENCY_POLICY_BITMAP
) {
1934 pr_err("Operation not supported when write-intent bitmap is enabled\n");
1942 if (mdmon_running(container
))
1943 st
->update_tail
= &st
->updates
;
1947 for (dv
= devlist
; dv
; dv
= dv
->next
)
1949 if (s
->raiddisks
> array
.raid_disks
&&
1950 array
.spare_disks
+ added_disks
<
1951 (s
->raiddisks
- array
.raid_disks
) &&
1953 pr_err("Need %d spare%s to avoid degraded array, and only have %d.\n"
1954 " Use --force to over-ride this check.\n",
1955 s
->raiddisks
- array
.raid_disks
,
1956 s
->raiddisks
- array
.raid_disks
== 1 ? "" : "s",
1957 array
.spare_disks
+ added_disks
);
1961 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
| GET_DISKS
| GET_DEVS
|
1962 GET_STATE
| GET_VERSION
);
1964 if (st
->ss
->external
&& subarray
== NULL
) {
1965 array
.level
= LEVEL_CONTAINER
;
1966 sra
->array
.level
= LEVEL_CONTAINER
;
1969 pr_err("failed to read sysfs parameters for %s\n",
1973 frozen
= freeze(st
);
1975 /* freeze() already spewed the reason */
1978 } else if (frozen
< 0) {
1979 pr_err("%s is performing resync/recovery and cannot be reshaped\n", devname
);
1984 /* ========= set size =============== */
1986 (s
->size
== MAX_SIZE
|| s
->size
!= (unsigned)array
.size
)) {
1987 unsigned long long orig_size
= get_component_size(fd
)/2;
1988 unsigned long long min_csize
;
1990 int raid0_takeover
= 0;
1993 orig_size
= (unsigned) array
.size
;
1995 if (orig_size
== 0) {
1996 pr_err("Cannot set device size in this type of array.\n");
2001 if (reshape_super(st
, s
->size
, UnSet
, UnSet
, 0, 0, UnSet
, NULL
,
2002 devname
, APPLY_METADATA_CHANGES
,
2008 if (st
->ss
->external
) {
2009 /* metadata can have size limitation
2010 * update size value according to metadata information
2012 struct mdinfo
*sizeinfo
=
2013 st
->ss
->container_content(st
, subarray
);
2015 unsigned long long new_size
=
2016 sizeinfo
->custom_array_size
/2;
2017 int data_disks
= get_data_disks(
2018 sizeinfo
->array
.level
,
2019 sizeinfo
->array
.layout
,
2020 sizeinfo
->array
.raid_disks
);
2021 new_size
/= data_disks
;
2022 dprintf("Metadata size correction from %llu to %llu (%llu)\n",
2023 orig_size
, new_size
,
2024 new_size
* data_disks
);
2026 sysfs_free(sizeinfo
);
2030 /* Update the size of each member device in case
2031 * they have been resized. This will never reduce
2032 * below the current used-size. The "size" attribute
2033 * understands '0' to mean 'max'.
2036 for (mdi
= sra
->devs
; mdi
; mdi
= mdi
->next
) {
2037 sysfs_set_num(sra
, mdi
, "size",
2038 s
->size
== MAX_SIZE
? 0 : s
->size
);
2039 if (array
.not_persistent
== 0 &&
2040 array
.major_version
== 0 &&
2041 get_linux_version() < 3001000) {
2042 /* Dangerous to allow size to exceed 2TB */
2043 unsigned long long csize
;
2044 if (sysfs_get_ll(sra
, mdi
, "size",
2046 if (csize
>= 2ULL*1024*1024*1024)
2047 csize
= 2ULL*1024*1024*1024;
2048 if ((min_csize
== 0 ||
2049 (min_csize
> csize
)))
2054 if (min_csize
&& s
->size
> min_csize
) {
2055 pr_err("Cannot safely make this array use more than 2TB per device on this kernel.\n");
2057 goto size_change_error
;
2059 if (min_csize
&& s
->size
== MAX_SIZE
) {
2060 /* Don't let the kernel choose a size - it will get
2063 pr_err("Limited v0.90 array to 2TB per device\n");
2064 s
->size
= min_csize
;
2066 if (st
->ss
->external
) {
2067 if (sra
->array
.level
== 0) {
2068 rv
= sysfs_set_str(sra
, NULL
, "level", "raid5");
2071 /* get array parameters after takeover
2072 * to change one parameter at time only
2074 rv
= md_get_array_info(fd
, &array
);
2077 /* make sure mdmon is
2078 * aware of the new level */
2079 if (!mdmon_running(st
->container_devnm
))
2080 start_mdmon(st
->container_devnm
);
2081 ping_monitor(container
);
2082 if (mdmon_running(st
->container_devnm
) &&
2083 st
->update_tail
== NULL
)
2084 st
->update_tail
= &st
->updates
;
2087 if (s
->size
== MAX_SIZE
)
2089 array
.size
= s
->size
;
2090 if (s
->size
& ~INT32_MAX
) {
2091 /* got truncated to 32bit, write to
2092 * component_size instead
2095 rv
= sysfs_set_num(sra
, NULL
,
2096 "component_size", s
->size
);
2100 rv
= md_set_array_info(fd
, &array
);
2102 /* manage array size when it is managed externally
2104 if ((rv
== 0) && st
->ss
->external
)
2105 rv
= set_array_size(st
, sra
, sra
->text_version
);
2108 if (raid0_takeover
) {
2109 /* do not recync non-existing parity,
2110 * we will drop it anyway
2112 sysfs_set_str(sra
, NULL
, "sync_action", "frozen");
2113 /* go back to raid0, drop parity disk
2115 sysfs_set_str(sra
, NULL
, "level", "raid0");
2116 md_get_array_info(fd
, &array
);
2123 /* restore metadata */
2124 if (reshape_super(st
, orig_size
, UnSet
, UnSet
, 0, 0,
2125 UnSet
, NULL
, devname
,
2126 ROLLBACK_METADATA_CHANGES
,
2129 pr_err("Cannot set device size for %s: %s\n",
2130 devname
, strerror(err
));
2132 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2133 cont_err("Bitmap must be removed before size can be changed\n");
2137 if (s
->assume_clean
) {
2138 /* This will fail on kernels older than 3.0 unless
2139 * a backport has been arranged.
2142 sysfs_set_str(sra
, NULL
, "resync_start",
2144 pr_err("--assume-clean not supported with --grow on this kernel\n");
2146 md_get_array_info(fd
, &array
);
2147 s
->size
= get_component_size(fd
)/2;
2149 s
->size
= array
.size
;
2150 if (c
->verbose
>= 0) {
2151 if (s
->size
== orig_size
)
2152 pr_err("component size of %s unchanged at %lluK\n",
2155 pr_err("component size of %s has been set to %lluK\n",
2159 } else if (array
.level
!= LEVEL_CONTAINER
) {
2160 s
->size
= get_component_size(fd
)/2;
2162 s
->size
= array
.size
;
2165 /* See if there is anything else to do */
2166 if ((s
->level
== UnSet
|| s
->level
== array
.level
) &&
2167 (s
->layout_str
== NULL
) &&
2168 (s
->chunk
== 0 || s
->chunk
== array
.chunk_size
) &&
2169 data_offset
== INVALID_SECTORS
&&
2170 (s
->raiddisks
== 0 || s
->raiddisks
== array
.raid_disks
)) {
2171 /* Nothing more to do */
2172 if (!changed
&& c
->verbose
>= 0)
2173 pr_err("%s: no change requested\n", devname
);
2177 /* ========= check for Raid10/Raid1 -> Raid0 conversion ===============
2178 * current implementation assumes that following conditions must be met:
2181 * - near_copies == 2
2183 if ((s
->level
== 0 && array
.level
== 10 && sra
&&
2184 array
.layout
== ((1 << 8) + 2) && !(array
.raid_disks
& 1)) ||
2185 (s
->level
== 0 && array
.level
== 1 && sra
)) {
2188 err
= remove_disks_for_takeover(st
, sra
, array
.layout
);
2190 dprintf("Array cannot be reshaped\n");
2196 /* Make sure mdmon has seen the device removal
2197 * and updated metadata before we continue with
2201 ping_monitor(container
);
2204 memset(&info
, 0, sizeof(info
));
2206 if (sysfs_init(&info
, fd
, NULL
)) {
2207 pr_err("failed to initialize sysfs.\n");
2211 strcpy(info
.text_version
, sra
->text_version
);
2212 info
.component_size
= s
->size
*2;
2213 info
.new_level
= s
->level
;
2214 info
.new_chunk
= s
->chunk
* 1024;
2215 if (info
.array
.level
== LEVEL_CONTAINER
) {
2216 info
.delta_disks
= UnSet
;
2217 info
.array
.raid_disks
= s
->raiddisks
;
2218 } else if (s
->raiddisks
)
2219 info
.delta_disks
= s
->raiddisks
- info
.array
.raid_disks
;
2221 info
.delta_disks
= UnSet
;
2222 if (s
->layout_str
== NULL
) {
2223 info
.new_layout
= UnSet
;
2224 if (info
.array
.level
== 6 &&
2225 (info
.new_level
== 6 || info
.new_level
== UnSet
) &&
2226 info
.array
.layout
>= 16) {
2227 pr_err("%s has a non-standard layout. If you wish to preserve this\n", devname
);
2228 cont_err("during the reshape, please specify --layout=preserve\n");
2229 cont_err("If you want to change it, specify a layout or use --layout=normalise\n");
2233 } else if (strcmp(s
->layout_str
, "normalise") == 0 ||
2234 strcmp(s
->layout_str
, "normalize") == 0) {
2235 /* If we have a -6 RAID6 layout, remove the '-6'. */
2236 info
.new_layout
= UnSet
;
2237 if (info
.array
.level
== 6 && info
.new_level
== UnSet
) {
2239 strcpy(l
, map_num(r6layout
, info
.array
.layout
));
2240 h
= strrchr(l
, '-');
2241 if (h
&& strcmp(h
, "-6") == 0) {
2243 info
.new_layout
= map_name(r6layout
, l
);
2246 pr_err("%s is only meaningful when reshaping a RAID6 array.\n", s
->layout_str
);
2250 } else if (strcmp(s
->layout_str
, "preserve") == 0) {
2251 /* This means that a non-standard RAID6 layout
2254 * - When reshape a RAID6 (e.g. adding a device)
2255 * which is in a non-standard layout, it is OK
2256 * to preserve that layout.
2257 * - When converting a RAID5 to RAID6, leave it in
2258 * the XXX-6 layout, don't re-layout.
2260 if (info
.array
.level
== 6 && info
.new_level
== UnSet
)
2261 info
.new_layout
= info
.array
.layout
;
2262 else if (info
.array
.level
== 5 && info
.new_level
== 6) {
2264 strcpy(l
, map_num(r5layout
, info
.array
.layout
));
2266 info
.new_layout
= map_name(r6layout
, l
);
2268 pr_err("%s in only meaningful when reshaping to RAID6\n", s
->layout_str
);
2273 int l
= info
.new_level
;
2275 l
= info
.array
.level
;
2278 info
.new_layout
= map_name(r5layout
, s
->layout_str
);
2281 info
.new_layout
= map_name(r6layout
, s
->layout_str
);
2284 info
.new_layout
= parse_layout_10(s
->layout_str
);
2287 info
.new_layout
= parse_layout_faulty(s
->layout_str
);
2290 pr_err("layout not meaningful with this level\n");
2294 if (info
.new_layout
== UnSet
) {
2295 pr_err("layout %s not understood for this level\n",
2302 if (array
.level
== LEVEL_FAULTY
) {
2303 if (s
->level
!= UnSet
&& s
->level
!= array
.level
) {
2304 pr_err("cannot change level of Faulty device\n");
2308 pr_err("cannot set chunksize of Faulty device\n");
2311 if (s
->raiddisks
&& s
->raiddisks
!= 1) {
2312 pr_err("cannot set raid_disks of Faulty device\n");
2315 if (s
->layout_str
) {
2316 if (md_get_array_info(fd
, &array
) != 0) {
2317 dprintf("Cannot get array information.\n");
2320 array
.layout
= info
.new_layout
;
2321 if (md_set_array_info(fd
, &array
) != 0) {
2322 pr_err("failed to set new layout\n");
2324 } else if (c
->verbose
>= 0)
2325 printf("layout for %s set to %d\n",
2326 devname
, array
.layout
);
2328 } else if (array
.level
== LEVEL_CONTAINER
) {
2329 /* This change is to be applied to every array in the
2330 * container. This is only needed when the metadata imposes
2331 * restraints of the various arrays in the container.
2332 * Currently we only know that IMSM requires all arrays
2333 * to have the same number of devices so changing the
2334 * number of devices (On-Line Capacity Expansion) must be
2335 * performed at the level of the container
2338 rv
= reshape_container(container
, devname
, -1, st
, &info
,
2339 c
->force
, c
->backup_file
, c
->verbose
,
2343 /* get spare devices from external metadata
2345 if (st
->ss
->external
) {
2346 struct mdinfo
*info2
;
2348 info2
= st
->ss
->container_content(st
, subarray
);
2350 info
.array
.spare_disks
=
2351 info2
->array
.spare_disks
;
2356 /* Impose these changes on a single array. First
2357 * check that the metadata is OK with the change. */
2359 if (reshape_super(st
, 0, info
.new_level
,
2360 info
.new_layout
, info
.new_chunk
,
2361 info
.array
.raid_disks
, info
.delta_disks
,
2362 c
->backup_file
, devname
,
2363 APPLY_METADATA_CHANGES
, c
->verbose
)) {
2368 rv
= reshape_array(container
, fd
, devname
, st
, &info
, c
->force
,
2369 devlist
, data_offset
, c
->backup_file
,
2370 c
->verbose
, 0, 0, 0);
2380 /* verify_reshape_position()
2381 * Function checks if reshape position in metadata is not farther
2382 * than position in md.
2384 * 0 : not valid sysfs entry
2385 * it can be caused by not started reshape, it should be started
2386 * by reshape array or raid0 array is before takeover
2387 * -1 : error, reshape position is obviously wrong
2388 * 1 : success, reshape progress correct or updated
2390 static int verify_reshape_position(struct mdinfo
*info
, int level
)
2396 /* read sync_max, failure can mean raid0 array */
2397 rv
= sysfs_get_str(info
, NULL
, "sync_max", buf
, 40);
2401 unsigned long long position
= strtoull(buf
, &ep
, 0);
2403 dprintf("Read sync_max sysfs entry is: %s\n", buf
);
2404 if (!(ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))) {
2405 position
*= get_data_disks(level
,
2407 info
->array
.raid_disks
);
2408 if (info
->reshape_progress
< position
) {
2409 dprintf("Corrected reshape progress (%llu) to md position (%llu)\n",
2410 info
->reshape_progress
, position
);
2411 info
->reshape_progress
= position
;
2413 } else if (info
->reshape_progress
> position
) {
2414 pr_err("Fatal error: array reshape was not properly frozen (expected reshape position is %llu, but reshape progress is %llu.\n",
2415 position
, info
->reshape_progress
);
2418 dprintf("Reshape position in md and metadata are the same;");
2422 } else if (rv
== 0) {
2423 /* for valid sysfs entry, 0-length content
2424 * should be indicated as error
2432 static unsigned long long choose_offset(unsigned long long lo
,
2433 unsigned long long hi
,
2434 unsigned long long min
,
2435 unsigned long long max
)
2437 /* Choose a new offset between hi and lo.
2438 * It must be between min and max, but
2439 * we would prefer something near the middle of hi/lo, and also
2440 * prefer to be aligned to a big power of 2.
2442 * So we start with the middle, then for each bit,
2443 * starting at '1' and increasing, if it is set, we either
2444 * add it or subtract it if possible, preferring the option
2445 * which is furthest from the boundary.
2447 * We stop once we get a 1MB alignment. As units are in sectors,
2448 * 1MB = 2*1024 sectors.
2450 unsigned long long choice
= (lo
+ hi
) / 2;
2451 unsigned long long bit
= 1;
2453 for (bit
= 1; bit
< 2*1024; bit
= bit
<< 1) {
2454 unsigned long long bigger
, smaller
;
2455 if (! (bit
& choice
))
2457 bigger
= choice
+ bit
;
2458 smaller
= choice
- bit
;
2459 if (bigger
> max
&& smaller
< min
)
2463 else if (smaller
< min
)
2465 else if (hi
- bigger
> smaller
- lo
)
2473 static int set_new_data_offset(struct mdinfo
*sra
, struct supertype
*st
,
2474 char *devname
, int delta_disks
,
2475 unsigned long long data_offset
,
2476 unsigned long long min
,
2482 unsigned long long before
, after
;
2484 /* Need to find min space before and after so same is used
2487 before
= UINT64_MAX
;
2489 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2493 struct supertype
*st2
;
2494 struct mdinfo info2
;
2496 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2498 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2499 dfd
= dev_open(dn
, O_RDONLY
);
2501 pr_err("%s: cannot open component %s\n",
2502 devname
, dn
? dn
: "-unknown-");
2505 st2
= dup_super(st
);
2506 rv
= st2
->ss
->load_super(st2
,dfd
, NULL
);
2510 pr_err("%s: cannot get superblock from %s\n",
2514 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
2515 st2
->ss
->free_super(st2
);
2517 if (info2
.space_before
== 0 &&
2518 info2
.space_after
== 0) {
2519 /* Metadata doesn't support data_offset changes */
2521 pr_err("%s: Metadata version doesn't support data_offset changes\n",
2525 if (before
> info2
.space_before
)
2526 before
= info2
.space_before
;
2527 if (after
> info2
.space_after
)
2528 after
= info2
.space_after
;
2530 if (data_offset
!= INVALID_SECTORS
) {
2532 if (info2
.data_offset
== data_offset
) {
2533 pr_err("%s: already has that data_offset\n",
2537 if (data_offset
< info2
.data_offset
)
2541 } else if ((data_offset
<= info2
.data_offset
&&
2543 (data_offset
>= info2
.data_offset
&&
2545 pr_err("%s: differing data offsets on devices make this --data-offset setting impossible\n",
2551 if (before
== UINT64_MAX
)
2552 /* impossible really, there must be no devices */
2555 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2556 char *dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2557 unsigned long long new_data_offset
;
2559 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2561 if (delta_disks
< 0) {
2562 /* Don't need any space as array is shrinking
2563 * just move data_offset up by min
2565 if (data_offset
== INVALID_SECTORS
)
2566 new_data_offset
= sd
->data_offset
+ min
;
2568 if (data_offset
< sd
->data_offset
+ min
) {
2569 pr_err("--data-offset too small for %s\n",
2573 new_data_offset
= data_offset
;
2575 } else if (delta_disks
> 0) {
2576 /* need space before */
2580 pr_err("Insufficient head-space for reshape on %s\n",
2584 if (data_offset
== INVALID_SECTORS
)
2585 new_data_offset
= sd
->data_offset
- min
;
2587 if (data_offset
> sd
->data_offset
- min
) {
2588 pr_err("--data-offset too large for %s\n",
2592 new_data_offset
= data_offset
;
2596 /* can move up or down. If 'data_offset'
2597 * was set we would have already decided,
2598 * so just choose direction with most space.
2605 sysfs_set_str(sra
, NULL
, "reshape_direction",
2606 dir
== 1 ? "backwards" : "forwards");
2608 /* Increase data offset */
2612 pr_err("Insufficient tail-space for reshape on %s\n",
2616 if (data_offset
!= INVALID_SECTORS
&&
2617 data_offset
< sd
->data_offset
+ min
) {
2618 pr_err("--data-offset too small on %s\n",
2622 if (data_offset
!= INVALID_SECTORS
)
2623 new_data_offset
= data_offset
;
2625 new_data_offset
= choose_offset(sd
->data_offset
,
2626 sd
->data_offset
+ after
,
2627 sd
->data_offset
+ min
,
2628 sd
->data_offset
+ after
);
2630 /* Decrease data offset */
2634 pr_err("insufficient head-room on %s\n",
2638 if (data_offset
!= INVALID_SECTORS
&&
2639 data_offset
> sd
->data_offset
- min
) {
2640 pr_err("--data-offset too large on %s\n",
2644 if (data_offset
!= INVALID_SECTORS
)
2645 new_data_offset
= data_offset
;
2647 new_data_offset
= choose_offset(sd
->data_offset
- before
,
2649 sd
->data_offset
- before
,
2650 sd
->data_offset
- min
);
2653 err
= sysfs_set_num(sra
, sd
, "new_offset", new_data_offset
);
2654 if (err
< 0 && errno
== E2BIG
) {
2655 /* try again after increasing data size to max */
2656 err
= sysfs_set_num(sra
, sd
, "size", 0);
2657 if (err
< 0 && errno
== EINVAL
&&
2658 !(sd
->disk
.state
& (1<<MD_DISK_SYNC
))) {
2659 /* some kernels have a bug where you cannot
2660 * use '0' on spare devices. */
2661 sysfs_set_num(sra
, sd
, "size",
2662 (sra
->component_size
+ after
)/2);
2664 err
= sysfs_set_num(sra
, sd
, "new_offset",
2668 if (errno
== E2BIG
&& data_offset
!= INVALID_SECTORS
) {
2669 pr_err("data-offset is too big for %s\n", dn
);
2672 if (sd
== sra
->devs
&&
2673 (errno
== ENOENT
|| errno
== E2BIG
))
2674 /* Early kernel, no 'new_offset' file,
2675 * or kernel doesn't like us.
2676 * For RAID5/6 this is not fatal
2679 pr_err("Cannot set new_offset for %s\n", dn
);
2687 /* Just use a backup file */
2691 static int raid10_reshape(char *container
, int fd
, char *devname
,
2692 struct supertype
*st
, struct mdinfo
*info
,
2693 struct reshape
*reshape
,
2694 unsigned long long data_offset
,
2695 int force
, int verbose
)
2697 /* Changing raid_disks, layout, chunksize or possibly
2698 * just data_offset for a RAID10.
2699 * We must always change data_offset. We change by at least
2700 * ->min_offset_change which is the largest of the old and new
2702 * If raid_disks is increasing, then data_offset must decrease
2703 * by at least this copy size.
2704 * If raid_disks is unchanged, data_offset must increase or
2705 * decrease by at least min_offset_change but preferably by much more.
2706 * We choose half of the available space.
2707 * If raid_disks is decreasing, data_offset must increase by
2708 * at least min_offset_change. To allow of this, component_size
2709 * must be decreased by the same amount.
2711 * So we calculate the required minimum and direction, possibly
2712 * reduce the component_size, then iterate through the devices
2713 * and set the new_data_offset.
2714 * If that all works, we set chunk_size, layout, raid_disks, and start
2718 unsigned long long min
;
2721 sra
= sysfs_read(fd
, NULL
,
2722 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
2725 pr_err("%s: Cannot get array details from sysfs\n", devname
);
2728 min
= reshape
->min_offset_change
;
2730 if (info
->delta_disks
)
2731 sysfs_set_str(sra
, NULL
, "reshape_direction",
2732 info
->delta_disks
< 0 ? "backwards" : "forwards");
2733 if (info
->delta_disks
< 0 && info
->space_after
< min
) {
2734 int rv
= sysfs_set_num(sra
, NULL
, "component_size",
2735 (sra
->component_size
- min
)/2);
2737 pr_err("cannot reduce component size\n");
2741 err
= set_new_data_offset(sra
, st
, devname
, info
->delta_disks
,
2742 data_offset
, min
, 0);
2744 pr_err("Cannot set new_data_offset: RAID10 reshape not\n");
2745 cont_err("supported on this kernel\n");
2751 if (!err
&& sysfs_set_num(sra
, NULL
, "chunk_size", info
->new_chunk
) < 0)
2753 if (!err
&& sysfs_set_num(sra
, NULL
, "layout",
2754 reshape
->after
.layout
) < 0)
2757 sysfs_set_num(sra
, NULL
, "raid_disks",
2758 info
->array
.raid_disks
+ info
->delta_disks
) < 0)
2760 if (!err
&& sysfs_set_str(sra
, NULL
, "sync_action", "reshape") < 0)
2763 pr_err("Cannot set array shape for %s\n",
2766 (info
->array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2767 cont_err(" Bitmap must be removed before shape can be changed\n");
2777 static void get_space_after(int fd
, struct supertype
*st
, struct mdinfo
*info
)
2779 struct mdinfo
*sra
, *sd
;
2780 /* Initialisation to silence compiler warning */
2781 unsigned long long min_space_before
= 0, min_space_after
= 0;
2784 sra
= sysfs_read(fd
, NULL
, GET_DEVS
);
2787 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2790 struct supertype
*st2
;
2791 struct mdinfo info2
;
2793 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2795 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2796 dfd
= dev_open(dn
, O_RDONLY
);
2799 st2
= dup_super(st
);
2800 if (st2
->ss
->load_super(st2
,dfd
, NULL
)) {
2806 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
2807 st2
->ss
->free_super(st2
);
2810 min_space_before
> info2
.space_before
)
2811 min_space_before
= info2
.space_before
;
2813 min_space_after
> info2
.space_after
)
2814 min_space_after
= info2
.space_after
;
2817 if (sd
== NULL
&& !first
) {
2818 info
->space_after
= min_space_after
;
2819 info
->space_before
= min_space_before
;
2824 static void update_cache_size(char *container
, struct mdinfo
*sra
,
2825 struct mdinfo
*info
,
2826 int disks
, unsigned long long blocks
)
2828 /* Check that the internal stripe cache is
2829 * large enough, or it won't work.
2830 * It must hold at least 4 stripes of the larger
2833 unsigned long cache
;
2834 cache
= max(info
->array
.chunk_size
, info
->new_chunk
);
2835 cache
*= 4; /* 4 stripes minimum */
2836 cache
/= 512; /* convert to sectors */
2837 /* make sure there is room for 'blocks' with a bit to spare */
2838 if (cache
< 16 + blocks
/ disks
)
2839 cache
= 16 + blocks
/ disks
;
2840 cache
/= (4096/512); /* Convert from sectors to pages */
2842 if (sra
->cache_size
< cache
)
2843 subarray_set_num(container
, sra
, "stripe_cache_size",
2847 static int impose_reshape(struct mdinfo
*sra
,
2848 struct mdinfo
*info
,
2849 struct supertype
*st
,
2852 char *devname
, char *container
,
2853 struct reshape
*reshape
)
2855 struct mdu_array_info_s array
;
2857 sra
->new_chunk
= info
->new_chunk
;
2860 /* for external metadata checkpoint saved by mdmon can be lost
2861 * or missed /due to e.g. crash/. Check if md is not during
2862 * restart farther than metadata points to.
2863 * If so, this means metadata information is obsolete.
2865 if (st
->ss
->external
)
2866 verify_reshape_position(info
, reshape
->level
);
2867 sra
->reshape_progress
= info
->reshape_progress
;
2869 sra
->reshape_progress
= 0;
2870 if (reshape
->after
.data_disks
< reshape
->before
.data_disks
)
2871 /* start from the end of the new array */
2872 sra
->reshape_progress
= (sra
->component_size
2873 * reshape
->after
.data_disks
);
2876 md_get_array_info(fd
, &array
);
2877 if (info
->array
.chunk_size
== info
->new_chunk
&&
2878 reshape
->before
.layout
== reshape
->after
.layout
&&
2879 st
->ss
->external
== 0) {
2880 /* use SET_ARRAY_INFO but only if reshape hasn't started */
2881 array
.raid_disks
= reshape
->after
.data_disks
+ reshape
->parity
;
2882 if (!restart
&& md_set_array_info(fd
, &array
) != 0) {
2885 pr_err("Cannot set device shape for %s: %s\n",
2886 devname
, strerror(errno
));
2889 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2890 cont_err("Bitmap must be removed before shape can be changed\n");
2894 } else if (!restart
) {
2895 /* set them all just in case some old 'new_*' value
2896 * persists from some earlier problem.
2899 if (sysfs_set_num(sra
, NULL
, "chunk_size", info
->new_chunk
) < 0)
2901 if (!err
&& sysfs_set_num(sra
, NULL
, "layout",
2902 reshape
->after
.layout
) < 0)
2904 if (!err
&& subarray_set_num(container
, sra
, "raid_disks",
2905 reshape
->after
.data_disks
+
2906 reshape
->parity
) < 0)
2909 pr_err("Cannot set device shape for %s\n", devname
);
2912 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2913 cont_err("Bitmap must be removed before shape can be changed\n");
2922 static int impose_level(int fd
, int level
, char *devname
, int verbose
)
2925 struct mdu_array_info_s array
;
2928 if (sysfs_init(&info
, fd
, NULL
)) {
2929 pr_err("failed to initialize sysfs.\n");
2933 md_get_array_info(fd
, &array
);
2934 if (level
== 0 && (array
.level
>= 4 && array
.level
<= 6)) {
2935 /* To convert to RAID0 we need to fail and
2936 * remove any non-data devices. */
2939 int data_disks
= array
.raid_disks
- 1;
2940 if (array
.level
== 6)
2942 if (array
.level
== 5 && array
.layout
!= ALGORITHM_PARITY_N
)
2944 if (array
.level
== 6 && array
.layout
!= ALGORITHM_PARITY_N_6
)
2946 sysfs_set_str(&info
, NULL
,"sync_action", "idle");
2947 /* First remove any spares so no recovery starts */
2948 for (d
= 0, found
= 0;
2949 d
< MAX_DISKS
&& found
< array
.nr_disks
; d
++) {
2950 mdu_disk_info_t disk
;
2952 if (md_get_disk_info(fd
, &disk
) < 0)
2954 if (disk
.major
== 0 && disk
.minor
== 0)
2957 if ((disk
.state
& (1 << MD_DISK_ACTIVE
)) &&
2958 disk
.raid_disk
< data_disks
)
2961 ioctl(fd
, HOT_REMOVE_DISK
,
2962 makedev(disk
.major
, disk
.minor
));
2964 /* Now fail anything left */
2965 md_get_array_info(fd
, &array
);
2966 for (d
= 0, found
= 0;
2967 d
< MAX_DISKS
&& found
< array
.nr_disks
; d
++) {
2968 mdu_disk_info_t disk
;
2970 if (md_get_disk_info(fd
, &disk
) < 0)
2972 if (disk
.major
== 0 && disk
.minor
== 0)
2975 if ((disk
.state
& (1 << MD_DISK_ACTIVE
)) &&
2976 disk
.raid_disk
< data_disks
)
2979 ioctl(fd
, SET_DISK_FAULTY
,
2980 makedev(disk
.major
, disk
.minor
));
2981 hot_remove_disk(fd
, makedev(disk
.major
, disk
.minor
), 1);
2984 c
= map_num(pers
, level
);
2986 int err
= sysfs_set_str(&info
, NULL
, "level", c
);
2989 pr_err("%s: could not set level to %s\n",
2992 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2993 cont_err("Bitmap must be removed before level can be changed\n");
2997 pr_err("level of %s changed to %s\n", devname
, c
);
3003 static void catch_term(int sig
)
3008 static int reshape_array(char *container
, int fd
, char *devname
,
3009 struct supertype
*st
, struct mdinfo
*info
,
3010 int force
, struct mddev_dev
*devlist
,
3011 unsigned long long data_offset
,
3012 char *backup_file
, int verbose
, int forked
,
3013 int restart
, int freeze_reshape
)
3015 struct reshape reshape
;
3018 int orig_level
= UnSet
;
3022 struct mdu_array_info_s array
;
3025 struct mddev_dev
*dv
;
3029 unsigned long long *offsets
= NULL
;
3033 unsigned long blocks
;
3034 unsigned long long array_size
;
3036 struct mdinfo
*sra
= NULL
;
3039 /* when reshaping a RAID0, the component_size might be zero.
3040 * So try to fix that up.
3042 if (md_get_array_info(fd
, &array
) != 0) {
3043 dprintf("Cannot get array information.\n");
3046 if (array
.level
== 0 && info
->component_size
== 0) {
3047 get_dev_size(fd
, NULL
, &array_size
);
3048 info
->component_size
= array_size
/ array
.raid_disks
;
3051 if (array
.level
== 10)
3052 /* Need space_after info */
3053 get_space_after(fd
, st
, info
);
3055 if (info
->reshape_active
) {
3056 int new_level
= info
->new_level
;
3057 info
->new_level
= UnSet
;
3058 if (info
->delta_disks
> 0)
3059 info
->array
.raid_disks
-= info
->delta_disks
;
3060 msg
= analyse_change(devname
, info
, &reshape
);
3061 info
->new_level
= new_level
;
3062 if (info
->delta_disks
> 0)
3063 info
->array
.raid_disks
+= info
->delta_disks
;
3065 /* Make sure the array isn't read-only */
3066 ioctl(fd
, RESTART_ARRAY_RW
, 0);
3068 msg
= analyse_change(devname
, info
, &reshape
);
3070 /* if msg == "", error has already been printed */
3072 pr_err("%s\n", msg
);
3075 if (restart
&& (reshape
.level
!= info
->array
.level
||
3076 reshape
.before
.layout
!= info
->array
.layout
||
3077 reshape
.before
.data_disks
+ reshape
.parity
!=
3078 info
->array
.raid_disks
- max(0, info
->delta_disks
))) {
3079 pr_err("reshape info is not in native format - cannot continue.\n");
3083 if (st
->ss
->external
&& restart
&& (info
->reshape_progress
== 0) &&
3084 !((sysfs_get_str(info
, NULL
, "sync_action",
3085 buf
, sizeof(buf
)) > 0) &&
3086 (strncmp(buf
, "reshape", 7) == 0))) {
3087 /* When reshape is restarted from '0', very begin of array
3088 * it is possible that for external metadata reshape and array
3089 * configuration doesn't happen.
3090 * Check if md has the same opinion, and reshape is restarted
3091 * from 0. If so, this is regular reshape start after reshape
3092 * switch in metadata to next array only.
3094 if ((verify_reshape_position(info
, reshape
.level
) >= 0) &&
3095 (info
->reshape_progress
== 0))
3100 * reshape already started. just skip to monitoring
3103 if (reshape
.backup_blocks
== 0)
3105 if (restart
& RESHAPE_NO_BACKUP
)
3108 /* Need 'sra' down at 'started:' */
3109 sra
= sysfs_read(fd
, NULL
,
3110 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
3111 GET_CHUNK
|GET_CACHE
);
3113 pr_err("%s: Cannot get array details from sysfs\n",
3119 backup_file
= locate_backup(sra
->sys_name
);
3123 /* The container is frozen but the array may not be.
3124 * So freeze the array so spares don't get put to the wrong use
3125 * FIXME there should probably be a cleaner separation between
3126 * freeze_array and freeze_container.
3128 sysfs_freeze_array(info
);
3129 /* Check we have enough spares to not be degraded */
3131 for (dv
= devlist
; dv
; dv
=dv
->next
)
3133 spares_needed
= max(reshape
.before
.data_disks
,
3134 reshape
.after
.data_disks
) +
3135 reshape
.parity
- array
.raid_disks
;
3137 if (!force
&& info
->new_level
> 1 && info
->array
.level
> 1 &&
3138 spares_needed
> info
->array
.spare_disks
+ added_disks
) {
3139 pr_err("Need %d spare%s to avoid degraded array, and only have %d.\n"
3140 " Use --force to over-ride this check.\n",
3142 spares_needed
== 1 ? "" : "s",
3143 info
->array
.spare_disks
+ added_disks
);
3146 /* Check we have enough spares to not fail */
3147 spares_needed
= max(reshape
.before
.data_disks
,
3148 reshape
.after
.data_disks
)
3150 if ((info
->new_level
> 1 || info
->new_level
== 0) &&
3151 spares_needed
> info
->array
.spare_disks
+added_disks
) {
3152 pr_err("Need %d spare%s to create working array, and only have %d.\n",
3153 spares_needed
, spares_needed
== 1 ? "" : "s",
3154 info
->array
.spare_disks
+ added_disks
);
3158 if (reshape
.level
!= array
.level
) {
3159 int err
= impose_level(fd
, reshape
.level
, devname
, verbose
);
3162 info
->new_layout
= UnSet
; /* after level change,
3163 * layout is meaningless */
3164 orig_level
= array
.level
;
3165 sysfs_freeze_array(info
);
3167 if (reshape
.level
> 0 && st
->ss
->external
) {
3168 /* make sure mdmon is aware of the new level */
3169 if (mdmon_running(container
))
3170 flush_mdmon(container
);
3172 if (!mdmon_running(container
))
3173 start_mdmon(container
);
3174 ping_monitor(container
);
3175 if (mdmon_running(container
) && st
->update_tail
== NULL
)
3176 st
->update_tail
= &st
->updates
;
3179 /* ->reshape_super might have chosen some spares from the
3180 * container that it wants to be part of the new array.
3181 * We can collect them with ->container_content and give
3182 * them to the kernel.
3184 if (st
->ss
->reshape_super
&& st
->ss
->container_content
) {
3185 char *subarray
= strchr(info
->text_version
+1, '/')+1;
3186 struct mdinfo
*info2
=
3187 st
->ss
->container_content(st
, subarray
);
3191 if (sysfs_init(info2
, fd
, st
->devnm
)) {
3192 pr_err("unable to initialize sysfs for %s\n",
3197 /* When increasing number of devices, we need to set
3198 * new raid_disks before adding these, or they might
3201 if (reshape
.backup_blocks
&&
3202 reshape
.after
.data_disks
>
3203 reshape
.before
.data_disks
)
3204 subarray_set_num(container
, info2
, "raid_disks",
3205 reshape
.after
.data_disks
+
3207 for (d
= info2
->devs
; d
; d
= d
->next
) {
3208 if (d
->disk
.state
== 0 &&
3209 d
->disk
.raid_disk
>= 0) {
3210 /* This is a spare that wants to
3211 * be part of the array.
3213 add_disk(fd
, st
, info2
, d
);
3219 /* We might have been given some devices to add to the
3220 * array. Now that the array has been changed to the right
3221 * level and frozen, we can safely add them.
3224 if (Manage_subdevs(devname
, fd
, devlist
, verbose
, 0, NULL
, 0))
3228 if (reshape
.backup_blocks
== 0 && data_offset
!= INVALID_SECTORS
)
3229 reshape
.backup_blocks
= reshape
.before
.data_disks
* info
->array
.chunk_size
/512;
3230 if (reshape
.backup_blocks
== 0) {
3231 /* No restriping needed, but we might need to impose
3232 * some more changes: layout, raid_disks, chunk_size
3234 /* read current array info */
3235 if (md_get_array_info(fd
, &array
) != 0) {
3236 dprintf("Cannot get array information.\n");
3239 /* compare current array info with new values and if
3240 * it is different update them to new */
3241 if (info
->new_layout
!= UnSet
&&
3242 info
->new_layout
!= array
.layout
) {
3243 array
.layout
= info
->new_layout
;
3244 if (md_set_array_info(fd
, &array
) != 0) {
3245 pr_err("failed to set new layout\n");
3247 } else if (verbose
>= 0)
3248 printf("layout for %s set to %d\n",
3249 devname
, array
.layout
);
3251 if (info
->delta_disks
!= UnSet
&& info
->delta_disks
!= 0 &&
3253 (info
->array
.raid_disks
+ info
->delta_disks
)) {
3254 array
.raid_disks
+= info
->delta_disks
;
3255 if (md_set_array_info(fd
, &array
) != 0) {
3256 pr_err("failed to set raid disks\n");
3258 } else if (verbose
>= 0) {
3259 printf("raid_disks for %s set to %d\n",
3260 devname
, array
.raid_disks
);
3263 if (info
->new_chunk
!= 0 &&
3264 info
->new_chunk
!= array
.chunk_size
) {
3265 if (sysfs_set_num(info
, NULL
,
3266 "chunk_size", info
->new_chunk
) != 0) {
3267 pr_err("failed to set chunk size\n");
3269 } else if (verbose
>= 0)
3270 printf("chunk size for %s set to %d\n",
3271 devname
, info
->new_chunk
);
3278 * There are three possibilities.
3279 * 1/ The array will shrink.
3280 * We need to ensure the reshape will pause before reaching
3281 * the 'critical section'. We also need to fork and wait for
3282 * that to happen. When it does we
3283 * suspend/backup/complete/unfreeze
3285 * 2/ The array will not change size.
3286 * This requires that we keep a backup of a sliding window
3287 * so that we can restore data after a crash. So we need
3288 * to fork and monitor progress.
3289 * In future we will allow the data_offset to change, so
3290 * a sliding backup becomes unnecessary.
3292 * 3/ The array will grow. This is relatively easy.
3293 * However the kernel's restripe routines will cheerfully
3294 * overwrite some early data before it is safe. So we
3295 * need to make a backup of the early parts of the array
3296 * and be ready to restore it if rebuild aborts very early.
3297 * For externally managed metadata, we still need a forked
3298 * child to monitor the reshape and suspend IO over the region
3299 * that is being reshaped.
3301 * We backup data by writing it to one spare, or to a
3302 * file which was given on command line.
3304 * In each case, we first make sure that storage is available
3305 * for the required backup.
3307 * - request the shape change.
3308 * - fork to handle backup etc.
3310 /* Check that we can hold all the data */
3311 get_dev_size(fd
, NULL
, &array_size
);
3312 if (reshape
.new_size
< (array_size
/512)) {
3313 pr_err("this change will reduce the size of the array.\n"
3314 " use --grow --array-size first to truncate array.\n"
3315 " e.g. mdadm --grow %s --array-size %llu\n",
3316 devname
, reshape
.new_size
/2);
3320 if (array
.level
== 10) {
3321 /* Reshaping RAID10 does not require any data backup by
3322 * user-space. Instead it requires that the data_offset
3323 * is changed to avoid the need for backup.
3324 * So this is handled very separately
3327 /* Nothing to do. */
3329 return raid10_reshape(container
, fd
, devname
, st
, info
,
3330 &reshape
, data_offset
, force
, verbose
);
3332 sra
= sysfs_read(fd
, NULL
,
3333 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
|
3336 pr_err("%s: Cannot get array details from sysfs\n",
3342 switch(set_new_data_offset(sra
, st
, devname
,
3343 reshape
.after
.data_disks
- reshape
.before
.data_disks
,
3345 reshape
.min_offset_change
, 1)) {
3349 /* Updated data_offset, so it's easy now */
3350 update_cache_size(container
, sra
, info
,
3351 min(reshape
.before
.data_disks
,
3352 reshape
.after
.data_disks
),
3353 reshape
.backup_blocks
);
3355 /* Right, everything seems fine. Let's kick things off.
3359 if (impose_reshape(sra
, info
, st
, fd
, restart
,
3360 devname
, container
, &reshape
) < 0)
3362 if (sysfs_set_str(sra
, NULL
, "sync_action", "reshape") < 0) {
3364 if (errno
!= EINVAL
) {
3365 pr_err("Failed to initiate reshape!\n");
3368 /* revert data_offset and try the old way */
3369 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
3370 sysfs_set_num(sra
, sd
, "new_offset",
3372 sysfs_set_str(sra
, NULL
, "reshape_direction",
3377 if (info
->new_level
== reshape
.level
)
3379 /* need to adjust level when reshape completes */
3381 case -1: /* ignore error, but don't wait */
3383 default: /* parent */
3392 fd
= open_dev(sra
->sys_name
);
3394 impose_level(fd
, info
->new_level
, devname
, verbose
);
3396 case 1: /* Couldn't set data_offset, try the old way */
3397 if (data_offset
!= INVALID_SECTORS
) {
3398 pr_err("Cannot update data_offset on this array\n");
3405 /* Decide how many blocks (sectors) for a reshape
3406 * unit. The number we have so far is just a minimum
3408 blocks
= reshape
.backup_blocks
;
3409 if (reshape
.before
.data_disks
==
3410 reshape
.after
.data_disks
) {
3411 /* Make 'blocks' bigger for better throughput, but
3412 * not so big that we reject it below.
3413 * Try for 16 megabytes
3415 while (blocks
* 32 < sra
->component_size
&& blocks
< 16*1024*2)
3418 pr_err("Need to backup %luK of critical section..\n", blocks
/2);
3420 if (blocks
>= sra
->component_size
/2) {
3421 pr_err("%s: Something wrong - reshape aborted\n", devname
);
3425 /* Now we need to open all these devices so we can read/write.
3427 nrdisks
= max(reshape
.before
.data_disks
,
3428 reshape
.after
.data_disks
) + reshape
.parity
3429 + sra
->array
.spare_disks
;
3430 fdlist
= xcalloc((1+nrdisks
), sizeof(int));
3431 offsets
= xcalloc((1+nrdisks
), sizeof(offsets
[0]));
3433 odisks
= reshape
.before
.data_disks
+ reshape
.parity
;
3434 d
= reshape_prepare_fdlist(devname
, sra
, odisks
, nrdisks
, blocks
,
3435 backup_file
, fdlist
, offsets
);
3439 if ((st
->ss
->manage_reshape
== NULL
) ||
3440 (st
->ss
->recover_backup
== NULL
)) {
3441 if (backup_file
== NULL
) {
3442 if (reshape
.after
.data_disks
<=
3443 reshape
.before
.data_disks
) {
3444 pr_err("%s: Cannot grow - need backup-file\n",
3446 pr_err(" Please provide one with \"--backup=...\"\n");
3448 } else if (d
== odisks
) {
3449 pr_err("%s: Cannot grow - need a spare or backup-file to backup critical section\n", devname
);
3453 if (!reshape_open_backup_file(backup_file
, fd
, devname
,
3455 fdlist
+d
, offsets
+d
,
3456 sra
->sys_name
, restart
)) {
3463 update_cache_size(container
, sra
, info
,
3464 min(reshape
.before
.data_disks
,
3465 reshape
.after
.data_disks
), blocks
);
3467 /* Right, everything seems fine. Let's kick things off.
3468 * If only changing raid_disks, use ioctl, else use
3473 if (impose_reshape(sra
, info
, st
, fd
, restart
,
3474 devname
, container
, &reshape
) < 0)
3477 err
= start_reshape(sra
, restart
, reshape
.before
.data_disks
,
3478 reshape
.after
.data_disks
, st
);
3480 pr_err("Cannot %s reshape for %s\n",
3481 restart
? "continue" : "start", devname
);
3485 sysfs_set_str(sra
, NULL
, "array_state", "active");
3486 if (freeze_reshape
) {
3490 pr_err("Reshape has to be continued from location %llu when root filesystem has been mounted.\n",
3491 sra
->reshape_progress
);
3496 if (continue_via_systemd(container
?: sra
->sys_name
,
3505 /* Now we just need to kick off the reshape and watch, while
3506 * handling backups of the data...
3507 * This is all done by a forked background process.
3509 switch(forked
? 0 : fork()) {
3511 pr_err("Cannot run child to monitor reshape: %s\n",
3525 /* If another array on the same devices is busy, the
3526 * reshape will wait for them. This would mean that
3527 * the first section that we suspend will stay suspended
3528 * for a long time. So check on that possibility
3529 * by looking for "DELAYED" in /proc/mdstat, and if found,
3533 struct mdstat_ent
*mds
, *m
;
3535 mds
= mdstat_read(1, 0);
3536 for (m
= mds
; m
; m
= m
->next
)
3537 if (strcmp(m
->devnm
, sra
->sys_name
) == 0) {
3538 if (m
->resync
&& m
->percent
== RESYNC_DELAYED
)
3541 /* Haven't started the reshape thread
3548 if (delayed
== 1 && get_linux_version() < 3007000) {
3549 pr_err("Reshape is delayed, but cannot wait carefully with this kernel.\n"
3550 " You might experience problems until other reshapes complete.\n");
3554 mdstat_wait(30 - (delayed
-1) * 25);
3557 if (check_env("MDADM_GROW_VERIFY"))
3558 fd
= open(devname
, O_RDONLY
| O_DIRECT
);
3561 mlockall(MCL_FUTURE
);
3563 signal(SIGTERM
, catch_term
);
3565 if (st
->ss
->external
) {
3566 /* metadata handler takes it from here */
3567 done
= st
->ss
->manage_reshape(
3568 fd
, sra
, &reshape
, st
, blocks
,
3569 fdlist
, offsets
, d
- odisks
, fdlist
+ odisks
,
3572 done
= child_monitor(
3573 fd
, sra
, &reshape
, st
, blocks
, fdlist
, offsets
,
3574 d
- odisks
, fdlist
+ odisks
, offsets
+ odisks
);
3579 if (backup_file
&& done
) {
3581 bul
= make_backup(sra
->sys_name
);
3584 int l
= readlink(bul
, buf
, sizeof(buf
) - 1);
3592 unlink(backup_file
);
3599 if (!st
->ss
->external
&&
3600 !(reshape
.before
.data_disks
!= reshape
.after
.data_disks
&&
3601 info
->custom_array_size
) && info
->new_level
== reshape
.level
&&
3603 /* no need to wait for the reshape to finish as
3604 * there is nothing more to do.
3611 if (st
->ss
->external
) {
3612 /* Re-load the metadata as much could have changed */
3613 int cfd
= open_dev(st
->container_devnm
);
3615 flush_mdmon(container
);
3616 st
->ss
->free_super(st
);
3617 st
->ss
->load_container(st
, cfd
, container
);
3622 /* set new array size if required customer_array_size is used
3625 if (reshape
.before
.data_disks
!= reshape
.after
.data_disks
&&
3626 info
->custom_array_size
)
3627 set_array_size(st
, info
, info
->text_version
);
3629 if (info
->new_level
!= reshape
.level
) {
3631 fd
= open(devname
, O_RDONLY
);
3632 impose_level(fd
, info
->new_level
, devname
, verbose
);
3634 if (info
->new_level
== 0)
3635 st
->update_tail
= NULL
;
3647 if (orig_level
!= UnSet
&& sra
) {
3648 c
= map_num(pers
, orig_level
);
3649 if (c
&& sysfs_set_str(sra
, NULL
, "level", c
) == 0)
3650 pr_err("aborting level change\n");
3658 /* mdfd handle is passed to be closed in child process (after fork).
3660 int reshape_container(char *container
, char *devname
,
3662 struct supertype
*st
,
3663 struct mdinfo
*info
,
3665 char *backup_file
, int verbose
,
3666 int forked
, int restart
, int freeze_reshape
)
3668 struct mdinfo
*cc
= NULL
;
3670 char last_devnm
[32] = "";
3672 /* component_size is not meaningful for a container,
3673 * so pass '0' meaning 'no change'
3676 reshape_super(st
, 0, info
->new_level
,
3677 info
->new_layout
, info
->new_chunk
,
3678 info
->array
.raid_disks
, info
->delta_disks
,
3679 backup_file
, devname
, APPLY_METADATA_CHANGES
,
3687 /* ping monitor to be sure that update is on disk
3689 ping_monitor(container
);
3691 if (!forked
&& !freeze_reshape
)
3692 if (continue_via_systemd(container
, GROW_SERVICE
))
3695 switch (forked
? 0 : fork()) {
3696 case -1: /* error */
3697 perror("Cannot fork to complete reshape\n");
3700 default: /* parent */
3701 if (!freeze_reshape
)
3702 printf("%s: multi-array reshape continues in background\n", Name
);
3710 /* close unused handle in child process
3716 /* For each member array with reshape_active,
3717 * we need to perform the reshape.
3718 * We pick the first array that needs reshaping and
3719 * reshape it. reshape_array() will re-read the metadata
3720 * so the next time through a different array should be
3721 * ready for reshape.
3722 * It is possible that the 'different' array will not
3723 * be assembled yet. In that case we simple exit.
3724 * When it is assembled, the mdadm which assembles it
3725 * will take over the reshape.
3727 struct mdinfo
*content
;
3729 struct mdstat_ent
*mdstat
;
3735 cc
= st
->ss
->container_content(st
, NULL
);
3737 for (content
= cc
; content
; content
= content
->next
) {
3739 if (!content
->reshape_active
)
3742 subarray
= strchr(content
->text_version
+1, '/')+1;
3743 mdstat
= mdstat_by_subdev(subarray
, container
);
3746 if (mdstat
->active
== 0) {
3747 pr_err("Skipping inactive array %s.\n",
3749 free_mdstat(mdstat
);
3758 devid
= devnm2devid(mdstat
->devnm
);
3759 adev
= map_dev(major(devid
), minor(devid
), 0);
3761 adev
= content
->text_version
;
3763 fd
= open_dev(mdstat
->devnm
);
3765 pr_err("Device %s cannot be opened for reshape.\n",
3770 if (strcmp(last_devnm
, mdstat
->devnm
) == 0) {
3771 /* Do not allow for multiple reshape_array() calls for
3773 * It can happen when reshape_array() returns without
3774 * error, when reshape is not finished (wrong reshape
3775 * starting/continuation conditions). Mdmon doesn't
3776 * switch to next array in container and reentry
3777 * conditions for the same array occur.
3778 * This is possibly interim until the behaviour of
3779 * reshape_array is resolved().
3781 printf("%s: Multiple reshape execution detected for device %s.\n", Name
, adev
);
3785 strcpy(last_devnm
, mdstat
->devnm
);
3787 if (sysfs_init(content
, fd
, mdstat
->devnm
)) {
3788 pr_err("Unable to initialize sysfs for %s\n",
3794 if (mdmon_running(container
))
3795 flush_mdmon(container
);
3797 rv
= reshape_array(container
, fd
, adev
, st
,
3798 content
, force
, NULL
, INVALID_SECTORS
,
3799 backup_file
, verbose
, 1, restart
,
3803 if (freeze_reshape
) {
3812 if (mdmon_running(container
))
3813 flush_mdmon(container
);
3822 * We run a child process in the background which performs the following
3824 * - wait for resync to reach a certain point
3825 * - suspend io to the following section
3826 * - backup that section
3827 * - allow resync to proceed further
3829 * - discard the backup.
3831 * When are combined in slightly different ways in the three cases.
3833 * - suspend/backup/allow/wait/resume/discard
3835 * - allow/wait/suspend/backup/allow/wait/resume/discard
3837 * - wait/resume/discard/suspend/backup/allow
3839 * suspend/backup/allow always come together
3840 * wait/resume/discard do too.
3841 * For the same-size case we have two backups to improve flow.
3845 int progress_reshape(struct mdinfo
*info
, struct reshape
*reshape
,
3846 unsigned long long backup_point
,
3847 unsigned long long wait_point
,
3848 unsigned long long *suspend_point
,
3849 unsigned long long *reshape_completed
, int *frozen
)
3851 /* This function is called repeatedly by the reshape manager.
3852 * It determines how much progress can safely be made and allows
3854 * - 'info' identifies the array and particularly records in
3855 * ->reshape_progress the metadata's knowledge of progress
3856 * This is a sector offset from the start of the array
3857 * of the next array block to be relocated. This number
3858 * may increase from 0 or decrease from array_size, depending
3859 * on the type of reshape that is happening.
3860 * Note that in contrast, 'sync_completed' is a block count of the
3861 * reshape so far. It gives the distance between the start point
3862 * (head or tail of device) and the next place that data will be
3863 * written. It always increases.
3864 * - 'reshape' is the structure created by analyse_change
3865 * - 'backup_point' shows how much the metadata manager has backed-up
3866 * data. For reshapes with increasing progress, it is the next address
3867 * to be backed up, previous addresses have been backed-up. For
3868 * decreasing progress, it is the earliest address that has been
3869 * backed up - later address are also backed up.
3870 * So addresses between reshape_progress and backup_point are
3871 * backed up providing those are in the 'correct' order.
3872 * - 'wait_point' is an array address. When reshape_completed
3873 * passes this point, progress_reshape should return. It might
3874 * return earlier if it determines that ->reshape_progress needs
3875 * to be updated or further backup is needed.
3876 * - suspend_point is maintained by progress_reshape and the caller
3877 * should not touch it except to initialise to zero.
3878 * It is an array address and it only increases in 2.6.37 and earlier.
3879 * This makes it difficult to handle reducing reshapes with
3880 * external metadata.
3881 * However: it is similar to backup_point in that it records the
3882 * other end of a suspended region from reshape_progress.
3883 * it is moved to extend the region that is safe to backup and/or
3885 * - reshape_completed is read from sysfs and returned. The caller
3886 * should copy this into ->reshape_progress when it has reason to
3887 * believe that the metadata knows this, and any backup outside this
3891 * 1 if more data from backup_point - but only as far as suspend_point,
3892 * should be backed up
3893 * 0 if things are progressing smoothly
3894 * -1 if the reshape is finished because it is all done,
3895 * -2 if the reshape is finished due to an error.
3898 int advancing
= (reshape
->after
.data_disks
3899 >= reshape
->before
.data_disks
);
3900 unsigned long long need_backup
; /* All data between start of array and
3901 * here will at some point need to
3904 unsigned long long read_offset
, write_offset
;
3905 unsigned long long write_range
;
3906 unsigned long long max_progress
, target
, completed
;
3907 unsigned long long array_size
= (info
->component_size
3908 * reshape
->before
.data_disks
);
3912 /* First, we unsuspend any region that is now known to be safe.
3913 * If suspend_point is on the 'wrong' side of reshape_progress, then
3914 * we don't have or need suspension at the moment. This is true for
3915 * native metadata when we don't need to back-up.
3918 if (info
->reshape_progress
<= *suspend_point
)
3919 sysfs_set_num(info
, NULL
, "suspend_lo",
3920 info
->reshape_progress
);
3922 /* Note: this won't work in 2.6.37 and before.
3923 * Something somewhere should make sure we don't need it!
3925 if (info
->reshape_progress
>= *suspend_point
)
3926 sysfs_set_num(info
, NULL
, "suspend_hi",
3927 info
->reshape_progress
);
3930 /* Now work out how far it is safe to progress.
3931 * If the read_offset for ->reshape_progress is less than
3932 * 'blocks' beyond the write_offset, we can only progress as far
3934 * Otherwise we can progress until the write_offset for the new location
3935 * reaches (within 'blocks' of) the read_offset at the current location.
3936 * However that region must be suspended unless we are using native
3938 * If we need to suspend more, we limit it to 128M per device, which is
3939 * rather arbitrary and should be some time-based calculation.
3941 read_offset
= info
->reshape_progress
/ reshape
->before
.data_disks
;
3942 write_offset
= info
->reshape_progress
/ reshape
->after
.data_disks
;
3943 write_range
= info
->new_chunk
/512;
3944 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
3945 need_backup
= array_size
;
3947 need_backup
= reshape
->backup_blocks
;
3949 if (read_offset
< write_offset
+ write_range
)
3950 max_progress
= backup_point
;
3953 read_offset
* reshape
->after
.data_disks
;
3955 if (read_offset
> write_offset
- write_range
)
3956 /* Can only progress as far as has been backed up,
3957 * which must be suspended */
3958 max_progress
= backup_point
;
3959 else if (info
->reshape_progress
<= need_backup
)
3960 max_progress
= backup_point
;
3962 if (info
->array
.major_version
>= 0)
3963 /* Can progress until backup is needed */
3964 max_progress
= need_backup
;
3966 /* Can progress until metadata update is required */
3968 read_offset
* reshape
->after
.data_disks
;
3969 /* but data must be suspended */
3970 if (max_progress
< *suspend_point
)
3971 max_progress
= *suspend_point
;
3976 /* We know it is safe to progress to 'max_progress' providing
3977 * it is suspended or we are using native metadata.
3978 * Consider extending suspend_point 128M per device if it
3979 * is less than 64M per device beyond reshape_progress.
3980 * But always do a multiple of 'blocks'
3981 * FIXME this is too big - it takes to long to complete
3984 target
= 64*1024*2 * min(reshape
->before
.data_disks
,
3985 reshape
->after
.data_disks
);
3986 target
/= reshape
->backup_blocks
;
3989 target
*= reshape
->backup_blocks
;
3991 /* For externally managed metadata we always need to suspend IO to
3992 * the area being reshaped so we regularly push suspend_point forward.
3993 * For native metadata we only need the suspend if we are going to do
3997 if ((need_backup
> info
->reshape_progress
||
3998 info
->array
.major_version
< 0) &&
3999 *suspend_point
< info
->reshape_progress
+ target
) {
4000 if (need_backup
< *suspend_point
+ 2 * target
)
4001 *suspend_point
= need_backup
;
4002 else if (*suspend_point
+ 2 * target
< array_size
)
4003 *suspend_point
+= 2 * target
;
4005 *suspend_point
= array_size
;
4006 sysfs_set_num(info
, NULL
, "suspend_hi", *suspend_point
);
4007 if (max_progress
> *suspend_point
)
4008 max_progress
= *suspend_point
;
4011 if (info
->array
.major_version
>= 0) {
4012 /* Only need to suspend when about to backup */
4013 if (info
->reshape_progress
< need_backup
* 2 &&
4014 *suspend_point
> 0) {
4016 sysfs_set_num(info
, NULL
, "suspend_lo", 0);
4017 sysfs_set_num(info
, NULL
, "suspend_hi",
4021 /* Need to suspend continually */
4022 if (info
->reshape_progress
< *suspend_point
)
4023 *suspend_point
= info
->reshape_progress
;
4024 if (*suspend_point
+ target
< info
->reshape_progress
)
4025 /* No need to move suspend region yet */;
4027 if (*suspend_point
>= 2 * target
)
4028 *suspend_point
-= 2 * target
;
4031 sysfs_set_num(info
, NULL
, "suspend_lo",
4034 if (max_progress
< *suspend_point
)
4035 max_progress
= *suspend_point
;
4039 /* now set sync_max to allow that progress. sync_max, like
4040 * sync_completed is a count of sectors written per device, so
4041 * we find the difference between max_progress and the start point,
4042 * and divide that by after.data_disks to get a sync_max
4044 * At the same time we convert wait_point to a similar number
4045 * for comparing against sync_completed.
4047 /* scale down max_progress to per_disk */
4048 max_progress
/= reshape
->after
.data_disks
;
4050 * Round to chunk size as some kernels give an erroneously
4053 max_progress
/= info
->new_chunk
/512;
4054 max_progress
*= info
->new_chunk
/512;
4055 /* And round to old chunk size as the kernel wants that */
4056 max_progress
/= info
->array
.chunk_size
/512;
4057 max_progress
*= info
->array
.chunk_size
/512;
4058 /* Limit progress to the whole device */
4059 if (max_progress
> info
->component_size
)
4060 max_progress
= info
->component_size
;
4061 wait_point
/= reshape
->after
.data_disks
;
4063 /* switch from 'device offset' to 'processed block count' */
4064 max_progress
= info
->component_size
- max_progress
;
4065 wait_point
= info
->component_size
- wait_point
;
4069 sysfs_set_num(info
, NULL
, "sync_max", max_progress
);
4071 /* Now wait. If we have already reached the point that we were
4072 * asked to wait to, don't wait at all, else wait for any change.
4073 * We need to select on 'sync_completed' as that is the place that
4074 * notifications happen, but we are really interested in
4075 * 'reshape_position'
4077 fd
= sysfs_get_fd(info
, NULL
, "sync_completed");
4079 goto check_progress
;
4081 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
4082 goto check_progress
;
4084 while (completed
< max_progress
&& completed
< wait_point
) {
4085 /* Check that sync_action is still 'reshape' to avoid
4086 * waiting forever on a dead array
4089 if (sysfs_get_str(info
, NULL
, "sync_action", action
, 20) <= 0 ||
4090 strncmp(action
, "reshape", 7) != 0)
4092 /* Some kernels reset 'sync_completed' to zero
4093 * before setting 'sync_action' to 'idle'.
4094 * So we need these extra tests.
4096 if (completed
== 0 && advancing
&&
4097 strncmp(action
, "idle", 4) == 0 &&
4098 info
->reshape_progress
> 0)
4100 if (completed
== 0 && !advancing
&&
4101 strncmp(action
, "idle", 4) == 0 &&
4102 info
->reshape_progress
<
4103 (info
->component_size
* reshape
->after
.data_disks
))
4105 sysfs_wait(fd
, NULL
);
4106 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
4107 goto check_progress
;
4109 /* Some kernels reset 'sync_completed' to zero,
4110 * we need to have real point we are in md.
4111 * So in that case, read 'reshape_position' from sysfs.
4113 if (completed
== 0) {
4114 unsigned long long reshapep
;
4116 if (sysfs_get_str(info
, NULL
, "sync_action", action
, 20) > 0 &&
4117 strncmp(action
, "idle", 4) == 0 &&
4118 sysfs_get_ll(info
, NULL
,
4119 "reshape_position", &reshapep
) == 0)
4120 *reshape_completed
= reshapep
;
4122 /* some kernels can give an incorrectly high
4123 * 'completed' number, so round down */
4124 completed
/= (info
->new_chunk
/512);
4125 completed
*= (info
->new_chunk
/512);
4126 /* Convert 'completed' back in to a 'progress' number */
4127 completed
*= reshape
->after
.data_disks
;
4129 completed
= (info
->component_size
4130 * reshape
->after
.data_disks
4132 *reshape_completed
= completed
;
4137 /* We return the need_backup flag. Caller will decide
4138 * how much - a multiple of ->backup_blocks up to *suspend_point
4141 return need_backup
> info
->reshape_progress
;
4143 return need_backup
>= info
->reshape_progress
;
4146 /* if we couldn't read a number from sync_completed, then
4147 * either the reshape did complete, or it aborted.
4148 * We can tell which by checking for 'none' in reshape_position.
4149 * If it did abort, then it might immediately restart if it
4150 * it was just a device failure that leaves us degraded but
4153 if (sysfs_get_str(info
, NULL
, "reshape_position", buf
,
4154 sizeof(buf
)) < 0 || strncmp(buf
, "none", 4) != 0) {
4155 /* The abort might only be temporary. Wait up to 10
4156 * seconds for fd to contain a valid number again.
4160 unsigned long long new_sync_max
;
4161 while (fd
>= 0 && rv
< 0 && wait
> 0) {
4162 if (sysfs_wait(fd
, &wait
) != 1)
4164 switch (sysfs_fd_get_ll(fd
, &completed
)) {
4166 /* all good again */
4168 /* If "sync_max" is no longer max_progress
4169 * we need to freeze things
4171 sysfs_get_ll(info
, NULL
, "sync_max",
4173 *frozen
= (new_sync_max
!= max_progress
);
4175 case -2: /* read error - abort */
4182 return rv
; /* abort */
4184 /* Maybe racing with array shutdown - check state */
4187 if (sysfs_get_str(info
, NULL
, "array_state", buf
,
4189 strncmp(buf
, "inactive", 8) == 0 ||
4190 strncmp(buf
, "clear",5) == 0)
4191 return -2; /* abort */
4192 return -1; /* complete */
4196 /* FIXME return status is never checked */
4197 static int grow_backup(struct mdinfo
*sra
,
4198 unsigned long long offset
, /* per device */
4199 unsigned long stripes
, /* per device, in old chunks */
4200 int *sources
, unsigned long long *offsets
,
4201 int disks
, int chunk
, int level
, int layout
,
4202 int dests
, int *destfd
, unsigned long long *destoffsets
,
4203 int part
, int *degraded
,
4206 /* Backup 'blocks' sectors at 'offset' on each device of the array,
4207 * to storage 'destfd' (offset 'destoffsets'), after first
4208 * suspending IO. Then allow resync to continue
4209 * over the suspended section.
4210 * Use part 'part' of the backup-super-block.
4215 unsigned long long ll
;
4217 //printf("offset %llu\n", offset);
4223 /* Check that array hasn't become degraded, else we might backup the wrong data */
4224 if (sysfs_get_ll(sra
, NULL
, "degraded", &ll
) < 0)
4225 return -1; /* FIXME this error is ignored */
4226 new_degraded
= (int)ll
;
4227 if (new_degraded
!= *degraded
) {
4228 /* check each device to ensure it is still working */
4230 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
4231 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
4233 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
4236 if (sysfs_get_str(sra
, sd
, "state",
4237 sbuf
, sizeof(sbuf
)) < 0 ||
4238 strstr(sbuf
, "faulty") ||
4239 strstr(sbuf
, "in_sync") == NULL
) {
4240 /* this device is dead */
4241 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
4242 if (sd
->disk
.raid_disk
>= 0 &&
4243 sources
[sd
->disk
.raid_disk
] >= 0) {
4244 close(sources
[sd
->disk
.raid_disk
]);
4245 sources
[sd
->disk
.raid_disk
] = -1;
4250 *degraded
= new_degraded
;
4253 bsb
.arraystart2
= __cpu_to_le64(offset
* odata
);
4254 bsb
.length2
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
4256 bsb
.arraystart
= __cpu_to_le64(offset
* odata
);
4257 bsb
.length
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
4260 bsb
.magic
[15] = '2';
4261 for (i
= 0; i
< dests
; i
++)
4263 lseek64(destfd
[i
], destoffsets
[i
] +
4264 __le64_to_cpu(bsb
.devstart2
)*512, 0);
4266 lseek64(destfd
[i
], destoffsets
[i
], 0);
4268 rv
= save_stripes(sources
, offsets
, disks
, chunk
, level
, layout
,
4269 dests
, destfd
, offset
* 512 * odata
,
4270 stripes
* chunk
* odata
, buf
);
4274 bsb
.mtime
= __cpu_to_le64(time(0));
4275 for (i
= 0; i
< dests
; i
++) {
4276 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
4278 bsb
.sb_csum
= bsb_csum((char*)&bsb
,
4279 ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
4280 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
4281 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
4282 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
4285 if ((unsigned long long)lseek64(destfd
[i
],
4286 destoffsets
[i
] - 4096, 0) !=
4287 destoffsets
[i
] - 4096)
4289 if (write(destfd
[i
], &bsb
, 512) != 512)
4291 if (destoffsets
[i
] > 4096) {
4292 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]+stripes
*chunk
*odata
, 0) !=
4293 destoffsets
[i
]+stripes
*chunk
*odata
)
4295 if (write(destfd
[i
], &bsb
, 512) != 512)
4305 /* in 2.6.30, the value reported by sync_completed can be
4306 * less that it should be by one stripe.
4307 * This only happens when reshape hits sync_max and pauses.
4308 * So allow wait_backup to either extent sync_max further
4309 * than strictly necessary, or return before the
4310 * sync has got quite as far as we would really like.
4311 * This is what 'blocks2' is for.
4312 * The various caller give appropriate values so that
4315 /* FIXME return value is often ignored */
4316 static int forget_backup(int dests
, int *destfd
,
4317 unsigned long long *destoffsets
,
4321 * Erase backup 'part' (which is 0 or 1)
4327 bsb
.arraystart2
= __cpu_to_le64(0);
4328 bsb
.length2
= __cpu_to_le64(0);
4330 bsb
.arraystart
= __cpu_to_le64(0);
4331 bsb
.length
= __cpu_to_le64(0);
4333 bsb
.mtime
= __cpu_to_le64(time(0));
4335 for (i
= 0; i
< dests
; i
++) {
4336 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
4337 bsb
.sb_csum
= bsb_csum((char*)&bsb
,
4338 ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
4339 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
4340 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
4341 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
4342 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]-4096, 0) !=
4343 destoffsets
[i
]-4096)
4345 if (rv
== 0 && write(destfd
[i
], &bsb
, 512) != 512)
4352 static void fail(char *msg
)
4355 rv
= (write(2, msg
, strlen(msg
)) != (int)strlen(msg
));
4356 rv
|= (write(2, "\n", 1) != 1);
4360 static char *abuf
, *bbuf
;
4361 static unsigned long long abuflen
;
4362 static void validate(int afd
, int bfd
, unsigned long long offset
)
4364 /* check that the data in the backup against the array.
4365 * This is only used for regression testing and should not
4366 * be used while the array is active
4370 lseek64(bfd
, offset
- 4096, 0);
4371 if (read(bfd
, &bsb2
, 512) != 512)
4372 fail("cannot read bsb");
4373 if (bsb2
.sb_csum
!= bsb_csum((char*)&bsb2
,
4374 ((char*)&bsb2
.sb_csum
)-((char*)&bsb2
)))
4375 fail("first csum bad");
4376 if (memcmp(bsb2
.magic
, "md_backup_data", 14) != 0)
4377 fail("magic is bad");
4378 if (memcmp(bsb2
.magic
, "md_backup_data-2", 16) == 0 &&
4379 bsb2
.sb_csum2
!= bsb_csum((char*)&bsb2
,
4380 ((char*)&bsb2
.sb_csum2
)-((char*)&bsb2
)))
4381 fail("second csum bad");
4383 if (__le64_to_cpu(bsb2
.devstart
)*512 != offset
)
4384 fail("devstart is wrong");
4387 unsigned long long len
= __le64_to_cpu(bsb2
.length
)*512;
4389 if (abuflen
< len
) {
4393 if (posix_memalign((void**)&abuf
, 4096, abuflen
) ||
4394 posix_memalign((void**)&bbuf
, 4096, abuflen
)) {
4396 /* just stop validating on mem-alloc failure */
4401 lseek64(bfd
, offset
, 0);
4402 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
) {
4403 //printf("len %llu\n", len);
4404 fail("read first backup failed");
4406 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart
)*512, 0);
4407 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
4408 fail("read first from array failed");
4409 if (memcmp(bbuf
, abuf
, len
) != 0) {
4412 printf("offset=%llu len=%llu\n",
4413 (unsigned long long)__le64_to_cpu(bsb2
.arraystart
)*512, len
);
4414 for (i
=0; i
<len
; i
++)
4415 if (bbuf
[i
] != abuf
[i
]) {
4416 printf("first diff byte %d\n", i
);
4420 fail("data1 compare failed");
4424 unsigned long long len
= __le64_to_cpu(bsb2
.length2
)*512;
4426 if (abuflen
< len
) {
4430 abuf
= xmalloc(abuflen
);
4431 bbuf
= xmalloc(abuflen
);
4434 lseek64(bfd
, offset
+__le64_to_cpu(bsb2
.devstart2
)*512, 0);
4435 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
)
4436 fail("read second backup failed");
4437 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart2
)*512, 0);
4438 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
4439 fail("read second from array failed");
4440 if (memcmp(bbuf
, abuf
, len
) != 0)
4441 fail("data2 compare failed");
4445 int child_monitor(int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
4446 struct supertype
*st
, unsigned long blocks
,
4447 int *fds
, unsigned long long *offsets
,
4448 int dests
, int *destfd
, unsigned long long *destoffsets
)
4450 /* Monitor a reshape where backup is being performed using
4451 * 'native' mechanism - either to a backup file, or
4452 * to some space in a spare.
4456 unsigned long long speed
;
4457 unsigned long long suspend_point
, array_size
;
4458 unsigned long long backup_point
, wait_point
;
4459 unsigned long long reshape_completed
;
4461 int increasing
= reshape
->after
.data_disks
>=
4462 reshape
->before
.data_disks
;
4463 int part
= 0; /* The next part of the backup area to fill. It
4464 * may already be full, so we need to check */
4465 int level
= reshape
->level
;
4466 int layout
= reshape
->before
.layout
;
4467 int data
= reshape
->before
.data_disks
;
4468 int disks
= reshape
->before
.data_disks
+ reshape
->parity
;
4469 int chunk
= sra
->array
.chunk_size
;
4471 unsigned long stripes
;
4475 /* set up the backup-super-block. This requires the
4476 * uuid from the array.
4478 /* Find a superblock */
4479 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
4483 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
4485 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
4486 devfd
= dev_open(dn
, O_RDONLY
);
4489 ok
= st
->ss
->load_super(st
, devfd
, NULL
);
4495 pr_err("Cannot find a superblock\n");
4499 memset(&bsb
, 0, 512);
4500 memcpy(bsb
.magic
, "md_backup_data-1", 16);
4501 st
->ss
->uuid_from_super(st
, uuid
);
4502 memcpy(bsb
.set_uuid
, uuid
, 16);
4503 bsb
.mtime
= __cpu_to_le64(time(0));
4504 bsb
.devstart2
= blocks
;
4506 stripes
= blocks
/ (sra
->array
.chunk_size
/512) /
4507 reshape
->before
.data_disks
;
4509 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
4510 /* Don't start the 'reshape' */
4512 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
) {
4513 sysfs_get_ll(sra
, NULL
, "sync_speed_min", &speed
);
4514 sysfs_set_num(sra
, NULL
, "sync_speed_min", 200000);
4518 array_size
= sra
->component_size
* reshape
->after
.data_disks
;
4519 backup_point
= sra
->reshape_progress
;
4522 array_size
= sra
->component_size
* reshape
->before
.data_disks
;
4523 backup_point
= reshape
->backup_blocks
;
4524 suspend_point
= array_size
;
4530 /* Want to return as soon the oldest backup slot can
4531 * be released as that allows us to start backing up
4532 * some more, providing suspend_point has been
4533 * advanced, which it should have.
4536 wait_point
= array_size
;
4537 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
4538 wait_point
= (__le64_to_cpu(bsb
.arraystart
) +
4539 __le64_to_cpu(bsb
.length
));
4540 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
4541 wait_point
= (__le64_to_cpu(bsb
.arraystart2
) +
4542 __le64_to_cpu(bsb
.length2
));
4545 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
4546 wait_point
= __le64_to_cpu(bsb
.arraystart
);
4547 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
4548 wait_point
= __le64_to_cpu(bsb
.arraystart2
);
4551 reshape_completed
= sra
->reshape_progress
;
4552 rv
= progress_reshape(sra
, reshape
,
4553 backup_point
, wait_point
,
4554 &suspend_point
, &reshape_completed
,
4556 /* external metadata would need to ping_monitor here */
4557 sra
->reshape_progress
= reshape_completed
;
4559 /* Clear any backup region that is before 'here' */
4561 if (__le64_to_cpu(bsb
.length
) > 0 &&
4562 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart
) +
4563 __le64_to_cpu(bsb
.length
)))
4564 forget_backup(dests
, destfd
,
4566 if (__le64_to_cpu(bsb
.length2
) > 0 &&
4567 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart2
) +
4568 __le64_to_cpu(bsb
.length2
)))
4569 forget_backup(dests
, destfd
,
4572 if (__le64_to_cpu(bsb
.length
) > 0 &&
4573 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart
)))
4574 forget_backup(dests
, destfd
,
4576 if (__le64_to_cpu(bsb
.length2
) > 0 &&
4577 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart2
)))
4578 forget_backup(dests
, destfd
,
4588 if (rv
== 0 && increasing
&& !st
->ss
->external
) {
4589 /* No longer need to monitor this reshape */
4590 sysfs_set_str(sra
, NULL
, "sync_max", "max");
4596 unsigned long long offset
;
4597 unsigned long actual_stripes
;
4598 /* Need to backup some data.
4599 * If 'part' is not used and the desired
4600 * backup size is suspended, do a backup,
4601 * then consider the next part.
4603 /* Check that 'part' is unused */
4604 if (part
== 0 && __le64_to_cpu(bsb
.length
) != 0)
4606 if (part
== 1 && __le64_to_cpu(bsb
.length2
) != 0)
4609 offset
= backup_point
/ data
;
4610 actual_stripes
= stripes
;
4612 if (offset
+ actual_stripes
* (chunk
/512) >
4613 sra
->component_size
)
4614 actual_stripes
= ((sra
->component_size
- offset
)
4616 if (offset
+ actual_stripes
* (chunk
/512) >
4620 if (offset
< actual_stripes
* (chunk
/512))
4621 actual_stripes
= offset
/ (chunk
/512);
4622 offset
-= actual_stripes
* (chunk
/512);
4623 if (offset
< suspend_point
/data
)
4626 if (actual_stripes
== 0)
4628 grow_backup(sra
, offset
, actual_stripes
, fds
, offsets
,
4629 disks
, chunk
, level
, layout
, dests
, destfd
,
4630 destoffsets
, part
, °raded
, buf
);
4631 validate(afd
, destfd
[0], destoffsets
[0]);
4632 /* record where 'part' is up to */
4635 backup_point
+= actual_stripes
* (chunk
/512) * data
;
4637 backup_point
-= actual_stripes
* (chunk
/512) * data
;
4641 /* FIXME maybe call progress_reshape one more time instead */
4642 /* remove any remaining suspension */
4643 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
4644 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
4645 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
4646 sysfs_set_num(sra
, NULL
, "sync_min", 0);
4648 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
4649 sysfs_set_num(sra
, NULL
, "sync_speed_min", speed
);
4655 * If any spare contains md_back_data-1 which is recent wrt mtime,
4656 * write that data into the array and update the super blocks with
4657 * the new reshape_progress
4659 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
,
4660 int cnt
, char *backup_file
, int verbose
)
4664 unsigned long long *offsets
;
4665 unsigned long long nstripe
, ostripe
;
4668 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
4669 if (info
->array
.level
== 6)
4670 odata
--; /* number of data disks */
4671 ndata
= info
->array
.raid_disks
- 1;
4672 if (info
->new_level
== 6)
4675 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
4677 if (info
->delta_disks
<= 0)
4678 /* Didn't grow, so the backup file must have
4682 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
4683 struct mdinfo dinfo
;
4686 char *devname
, namebuf
[20];
4687 unsigned long long lo
, hi
;
4689 /* This was a spare and may have some saved data on it.
4690 * Load the superblock, find and load the
4691 * backup_super_block.
4692 * If either fail, go on to next device.
4693 * If the backup contains no new info, just return
4694 * else restore data and update all superblocks
4696 if (i
== old_disks
-1) {
4697 fd
= open(backup_file
, O_RDONLY
);
4699 pr_err("backup file %s inaccessible: %s\n",
4700 backup_file
, strerror(errno
));
4703 devname
= backup_file
;
4708 if (st
->ss
->load_super(st
, fd
, NULL
))
4711 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4712 st
->ss
->free_super(st
);
4715 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
4717 pr_err("Cannot seek on device %d\n", i
);
4718 continue; /* Cannot seek */
4720 sprintf(namebuf
, "device-%d", i
);
4723 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
)) {
4725 pr_err("Cannot read from %s\n", devname
);
4726 continue; /* Cannot read */
4728 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0 &&
4729 memcmp(bsb
.magic
, "md_backup_data-2", 16) != 0) {
4731 pr_err("No backup metadata on %s\n", devname
);
4734 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
))) {
4736 pr_err("Bad backup-metadata checksum on %s\n",
4738 continue; /* bad checksum */
4740 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0 &&
4741 bsb
.sb_csum2
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum2
)-((char*)&bsb
))) {
4743 pr_err("Bad backup-metadata checksum2 on %s\n",
4745 continue; /* Bad second checksum */
4747 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0) {
4749 pr_err("Wrong uuid on backup-metadata on %s\n",
4751 continue; /* Wrong uuid */
4755 * array utime and backup-mtime should be updated at
4756 * much the same time, but it seems that sometimes
4757 * they aren't... So allow considerable flexability in
4758 * matching, and allow this test to be overridden by
4759 * an environment variable.
4761 if(time_after(info
->array
.utime
, (unsigned int)__le64_to_cpu(bsb
.mtime
) + 2*60*60) ||
4762 time_before(info
->array
.utime
, (unsigned int)__le64_to_cpu(bsb
.mtime
) - 10*60)) {
4763 if (check_env("MDADM_GROW_ALLOW_OLD")) {
4764 pr_err("accepting backup with timestamp %lu for array with timestamp %lu\n",
4765 (unsigned long)__le64_to_cpu(bsb
.mtime
),
4766 (unsigned long)info
->array
.utime
);
4768 pr_err("too-old timestamp on backup-metadata on %s\n", devname
);
4769 pr_err("If you think it is should be safe, try 'export MDADM_GROW_ALLOW_OLD=1'\n");
4770 continue; /* time stamp is too bad */
4774 if (bsb
.magic
[15] == '1') {
4775 if (bsb
.length
== 0)
4777 if (info
->delta_disks
>= 0) {
4778 /* reshape_progress is increasing */
4779 if (__le64_to_cpu(bsb
.arraystart
)
4780 + __le64_to_cpu(bsb
.length
)
4781 < info
->reshape_progress
) {
4784 pr_err("backup-metadata found on %s but is not needed\n", devname
);
4785 continue; /* No new data here */
4788 /* reshape_progress is decreasing */
4789 if (__le64_to_cpu(bsb
.arraystart
) >=
4790 info
->reshape_progress
)
4791 goto nonew
; /* No new data here */
4794 if (bsb
.length
== 0 && bsb
.length2
== 0)
4796 if (info
->delta_disks
>= 0) {
4797 /* reshape_progress is increasing */
4798 if ((__le64_to_cpu(bsb
.arraystart
)
4799 + __le64_to_cpu(bsb
.length
)
4800 < info
->reshape_progress
) &&
4801 (__le64_to_cpu(bsb
.arraystart2
)
4802 + __le64_to_cpu(bsb
.length2
)
4803 < info
->reshape_progress
))
4804 goto nonew
; /* No new data here */
4806 /* reshape_progress is decreasing */
4807 if (__le64_to_cpu(bsb
.arraystart
) >=
4808 info
->reshape_progress
&&
4809 __le64_to_cpu(bsb
.arraystart2
) >=
4810 info
->reshape_progress
)
4811 goto nonew
; /* No new data here */
4814 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0) {
4817 pr_err("Failed to verify secondary backup-metadata block on %s\n",
4819 continue; /* Cannot seek */
4821 /* There should be a duplicate backup superblock 4k before here */
4822 if (lseek64(fd
, -4096, 1) < 0 ||
4823 read(fd
, &bsb2
, sizeof(bsb2
)) != sizeof(bsb2
))
4824 goto second_fail
; /* Cannot find leading superblock */
4825 if (bsb
.magic
[15] == '1')
4826 bsbsize
= offsetof(struct mdp_backup_super
, pad1
);
4828 bsbsize
= offsetof(struct mdp_backup_super
, pad
);
4829 if (memcmp(&bsb2
, &bsb
, bsbsize
) != 0)
4830 goto second_fail
; /* Cannot find leading superblock */
4832 /* Now need the data offsets for all devices. */
4833 offsets
= xmalloc(sizeof(*offsets
)*info
->array
.raid_disks
);
4834 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
4837 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
4838 /* FIXME should be this be an error */
4840 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4841 st
->ss
->free_super(st
);
4842 offsets
[j
] = dinfo
.data_offset
* 512;
4844 printf("%s: restoring critical section\n", Name
);
4846 if (restore_stripes(fdlist
, offsets
, info
->array
.raid_disks
,
4847 info
->new_chunk
, info
->new_level
,
4848 info
->new_layout
, fd
,
4849 __le64_to_cpu(bsb
.devstart
)*512,
4850 __le64_to_cpu(bsb
.arraystart
)*512,
4851 __le64_to_cpu(bsb
.length
)*512, NULL
)) {
4852 /* didn't succeed, so giveup */
4854 pr_err("Error restoring backup from %s\n",
4860 if (bsb
.magic
[15] == '2' &&
4861 restore_stripes(fdlist
, offsets
, info
->array
.raid_disks
,
4862 info
->new_chunk
, info
->new_level
,
4863 info
->new_layout
, fd
,
4864 __le64_to_cpu(bsb
.devstart
)*512 +
4865 __le64_to_cpu(bsb
.devstart2
)*512,
4866 __le64_to_cpu(bsb
.arraystart2
)*512,
4867 __le64_to_cpu(bsb
.length2
)*512, NULL
)) {
4868 /* didn't succeed, so giveup */
4870 pr_err("Error restoring second backup from %s\n",
4878 /* Ok, so the data is restored. Let's update those superblocks. */
4882 lo
= __le64_to_cpu(bsb
.arraystart
);
4883 hi
= lo
+ __le64_to_cpu(bsb
.length
);
4885 if (bsb
.magic
[15] == '2' && bsb
.length2
) {
4886 unsigned long long lo1
, hi1
;
4887 lo1
= __le64_to_cpu(bsb
.arraystart2
);
4888 hi1
= lo1
+ __le64_to_cpu(bsb
.length2
);
4892 } else if (lo
< lo1
)
4897 if (lo
< hi
&& (info
->reshape_progress
< lo
||
4898 info
->reshape_progress
> hi
))
4899 /* backup does not affect reshape_progress*/ ;
4900 else if (info
->delta_disks
>= 0) {
4901 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
) +
4902 __le64_to_cpu(bsb
.length
);
4903 if (bsb
.magic
[15] == '2') {
4904 unsigned long long p2
;
4906 p2
= __le64_to_cpu(bsb
.arraystart2
) +
4907 __le64_to_cpu(bsb
.length2
);
4908 if (p2
> info
->reshape_progress
)
4909 info
->reshape_progress
= p2
;
4912 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
);
4913 if (bsb
.magic
[15] == '2') {
4914 unsigned long long p2
;
4916 p2
= __le64_to_cpu(bsb
.arraystart2
);
4917 if (p2
< info
->reshape_progress
)
4918 info
->reshape_progress
= p2
;
4921 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
4924 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
4926 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4927 dinfo
.reshape_progress
= info
->reshape_progress
;
4928 st
->ss
->update_super(st
, &dinfo
, "_reshape_progress",
4930 st
->ss
->store_super(st
, fdlist
[j
]);
4931 st
->ss
->free_super(st
);
4935 /* Didn't find any backup data, try to see if any
4938 if (info
->delta_disks
< 0) {
4939 /* When shrinking, the critical section is at the end.
4940 * So see if we are before the critical section.
4942 unsigned long long first_block
;
4943 nstripe
= ostripe
= 0;
4945 while (ostripe
>= nstripe
) {
4946 ostripe
+= info
->array
.chunk_size
/ 512;
4947 first_block
= ostripe
* odata
;
4948 nstripe
= first_block
/ ndata
/ (info
->new_chunk
/512) *
4949 (info
->new_chunk
/512);
4952 if (info
->reshape_progress
>= first_block
)
4955 if (info
->delta_disks
> 0) {
4956 /* See if we are beyond the critical section. */
4957 unsigned long long last_block
;
4958 nstripe
= ostripe
= 0;
4960 while (nstripe
>= ostripe
) {
4961 nstripe
+= info
->new_chunk
/ 512;
4962 last_block
= nstripe
* ndata
;
4963 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
4964 (info
->array
.chunk_size
/512);
4967 if (info
->reshape_progress
>= last_block
)
4970 /* needed to recover critical section! */
4972 pr_err("Failed to find backup of critical section\n");
4976 int Grow_continue_command(char *devname
, int fd
,
4977 char *backup_file
, int verbose
)
4980 struct supertype
*st
= NULL
;
4981 struct mdinfo
*content
= NULL
;
4982 struct mdinfo array
;
4983 char *subarray
= NULL
;
4984 struct mdinfo
*cc
= NULL
;
4985 struct mdstat_ent
*mdstat
= NULL
;
4989 dprintf("Grow continue from command line called for %s\n", devname
);
4991 st
= super_by_fd(fd
, &subarray
);
4992 if (!st
|| !st
->ss
) {
4993 pr_err("Unable to determine metadata format for %s\n", devname
);
4996 dprintf("Grow continue is run for ");
4997 if (st
->ss
->external
== 0) {
5000 dprintf_cont("native array (%s)\n", devname
);
5001 if (md_get_array_info(fd
, &array
.array
) < 0) {
5002 pr_err("%s is not an active md array - aborting\n",
5005 goto Grow_continue_command_exit
;
5008 sysfs_init(content
, fd
, NULL
);
5009 /* Need to load a superblock.
5010 * FIXME we should really get what we need from
5014 for (d
= 0; d
< MAX_DISKS
; d
++) {
5015 mdu_disk_info_t disk
;
5019 if (md_get_disk_info(fd
, &disk
) < 0)
5021 if (disk
.major
== 0 && disk
.minor
== 0)
5023 if ((disk
.state
& (1 << MD_DISK_ACTIVE
)) == 0)
5025 dv
= map_dev(disk
.major
, disk
.minor
, 1);
5028 fd2
= dev_open(dv
, O_RDONLY
);
5031 err
= st
->ss
->load_super(st
, fd2
, NULL
);
5037 if (d
== MAX_DISKS
) {
5038 pr_err("Unable to load metadata for %s\n",
5041 goto Grow_continue_command_exit
;
5043 st
->ss
->getinfo_super(st
, content
, NULL
);
5044 if (!content
->reshape_active
)
5048 } while (cnt
-- > 0);
5053 dprintf_cont("subarray (%s)\n", subarray
);
5054 container
= st
->container_devnm
;
5055 cfd
= open_dev_excl(st
->container_devnm
);
5057 container
= st
->devnm
;
5059 cfd
= open_dev_excl(st
->devnm
);
5060 dprintf_cont("container (%s)\n", container
);
5064 pr_err("Unable to open container for %s\n", devname
);
5066 goto Grow_continue_command_exit
;
5069 /* find in container array under reshape
5071 ret_val
= st
->ss
->load_container(st
, cfd
, NULL
);
5073 pr_err("Cannot read superblock for %s\n", devname
);
5075 goto Grow_continue_command_exit
;
5078 cc
= st
->ss
->container_content(st
, subarray
);
5079 for (content
= cc
; content
; content
= content
->next
) {
5081 int allow_reshape
= 1;
5083 if (content
->reshape_active
== 0)
5085 /* The decision about array or container wide
5086 * reshape is taken in Grow_continue based
5087 * content->reshape_active state, therefore we
5088 * need to check_reshape based on
5089 * reshape_active and subarray name
5091 if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
))
5093 if (content
->reshape_active
== CONTAINER_RESHAPE
&&
5094 (content
->array
.state
5095 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
)))
5098 if (!allow_reshape
) {
5099 pr_err("cannot continue reshape of an array in container with unsupported metadata: %s(%s)\n",
5100 devname
, container
);
5102 goto Grow_continue_command_exit
;
5105 array_name
= strchr(content
->text_version
+1, '/')+1;
5106 mdstat
= mdstat_by_subdev(array_name
, container
);
5109 if (mdstat
->active
== 0) {
5110 pr_err("Skipping inactive array %s.\n",
5112 free_mdstat(mdstat
);
5119 pr_err("Unable to determine reshaped array for %s\n", devname
);
5121 goto Grow_continue_command_exit
;
5123 fd2
= open_dev(mdstat
->devnm
);
5125 pr_err("cannot open (%s)\n", mdstat
->devnm
);
5127 goto Grow_continue_command_exit
;
5130 if (sysfs_init(content
, fd2
, mdstat
->devnm
)) {
5131 pr_err("Unable to initialize sysfs for %s, Grow cannot continue.\n",
5135 goto Grow_continue_command_exit
;
5140 /* start mdmon in case it is not running
5142 if (!mdmon_running(container
))
5143 start_mdmon(container
);
5144 ping_monitor(container
);
5146 if (mdmon_running(container
))
5147 st
->update_tail
= &st
->updates
;
5149 pr_err("No mdmon found. Grow cannot continue.\n");
5151 goto Grow_continue_command_exit
;
5155 /* verify that array under reshape is started from
5158 if (verify_reshape_position(content
, content
->array
.level
) < 0) {
5160 goto Grow_continue_command_exit
;
5165 ret_val
= Grow_continue(fd
, st
, content
, backup_file
, 1, 0);
5167 Grow_continue_command_exit
:
5170 st
->ss
->free_super(st
);
5171 free_mdstat(mdstat
);
5178 int Grow_continue(int mdfd
, struct supertype
*st
, struct mdinfo
*info
,
5179 char *backup_file
, int forked
, int freeze_reshape
)
5183 if (!info
->reshape_active
)
5186 if (st
->ss
->external
) {
5187 int cfd
= open_dev(st
->container_devnm
);
5192 st
->ss
->load_container(st
, cfd
, st
->container_devnm
);
5194 ret_val
= reshape_container(st
->container_devnm
, NULL
, mdfd
,
5195 st
, info
, 0, backup_file
, 0,
5196 forked
, 1 | info
->reshape_active
,
5199 ret_val
= reshape_array(NULL
, mdfd
, "array", st
, info
, 1,
5200 NULL
, INVALID_SECTORS
, backup_file
,
5201 0, forked
, 1 | info
->reshape_active
,
5207 char *make_backup(char *name
)
5209 char *base
= "backup_file-";
5213 len
= strlen(MAP_DIR
) + 1 + strlen(base
) + strlen(name
)+1;
5214 fname
= xmalloc(len
);
5215 sprintf(fname
, "%s/%s%s", MAP_DIR
, base
, name
);
5219 char *locate_backup(char *name
)
5221 char *fl
= make_backup(name
);
5224 if (stat(fl
, &stb
) == 0 && S_ISREG(stb
.st_mode
))