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 st
->ss
->update_super(st
, &info
, "linear-grow-new", newdev
, 0, 0, NULL
);
202 if (st
->ss
->store_super(st
, nfd
)) {
203 pr_err("Cannot store new superblock on %s\n", newdev
);
209 if (ioctl(fd
, ADD_NEW_DISK
, &info
.disk
) != 0) {
210 pr_err("Cannot add new disk to this array\n");
213 /* Well, that seems to have worked.
214 * Now go through and update all superblocks
217 if (md_get_array_info(fd
, &info
.array
) < 0) {
218 pr_err("cannot get array info for %s\n", devname
);
223 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
224 mdu_disk_info_t disk
;
228 if (md_get_disk_info(fd
, &disk
) < 0) {
229 pr_err("cannot get device detail for device %d\n", d
);
232 dv
= map_dev(disk
.major
, disk
.minor
, 1);
234 pr_err("cannot find device file for device %d\n", d
);
237 fd2
= dev_open(dv
, O_RDWR
);
239 pr_err("cannot open device file %s\n", dv
);
242 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
243 pr_err("cannot find super block on %s\n", dv
);
248 info
.array
.raid_disks
= nd
+1;
249 info
.array
.nr_disks
= nd
+1;
250 info
.array
.active_disks
= nd
+1;
251 info
.array
.working_disks
= nd
+1;
253 st
->ss
->update_super(st
, &info
, "linear-grow-update", dv
,
256 if (st
->ss
->store_super(st
, fd2
)) {
257 pr_err("Cannot store new superblock on %s\n", dv
);
267 int Grow_addbitmap(char *devname
, int fd
, struct context
*c
, struct shape
*s
)
270 * First check that array doesn't have a bitmap
271 * Then create the bitmap
274 * For internal bitmaps, we need to check the version,
275 * find all the active devices, and write the bitmap block
278 mdu_bitmap_file_t bmf
;
279 mdu_array_info_t array
;
280 struct supertype
*st
;
281 char *subarray
= NULL
;
282 int major
= BITMAP_MAJOR_HI
;
283 unsigned long long bitmapsize
, array_size
;
287 * We only ever get called if s->bitmap_file is != NULL, so this check
288 * is just here to quiet down static code checkers.
293 if (strcmp(s
->bitmap_file
, "clustered") == 0)
294 major
= BITMAP_MAJOR_CLUSTERED
;
296 if (ioctl(fd
, GET_BITMAP_FILE
, &bmf
) != 0) {
298 pr_err("Memory allocation failure.\n");
300 pr_err("bitmaps not supported by this kernel.\n");
303 if (bmf
.pathname
[0]) {
304 if (strcmp(s
->bitmap_file
,"none") == 0) {
305 if (ioctl(fd
, SET_BITMAP_FILE
, -1) != 0) {
306 pr_err("failed to remove bitmap %s\n",
312 pr_err("%s already has a bitmap (%s)\n", devname
, bmf
.pathname
);
315 if (md_get_array_info(fd
, &array
) != 0) {
316 pr_err("cannot get array status for %s\n", devname
);
319 if (array
.state
& (1 << MD_SB_BITMAP_PRESENT
)) {
320 if (strcmp(s
->bitmap_file
, "none")==0) {
321 array
.state
&= ~(1 << MD_SB_BITMAP_PRESENT
);
322 if (md_set_array_info(fd
, &array
) != 0) {
323 if (array
.state
& (1 << MD_SB_CLUSTERED
))
324 pr_err("failed to remove clustered bitmap.\n");
326 pr_err("failed to remove internal bitmap.\n");
331 pr_err("bitmap already present on %s\n", devname
);
335 if (strcmp(s
->bitmap_file
, "none") == 0) {
336 pr_err("no bitmap found on %s\n", devname
);
339 if (array
.level
<= 0) {
340 pr_err("Bitmaps not meaningful with level %s\n",
341 map_num(pers
, array
.level
)?:"of this array");
344 bitmapsize
= array
.size
;
346 if (get_dev_size(fd
, NULL
, &array_size
) &&
347 array_size
> (0x7fffffffULL
<< 9)) {
348 /* Array is big enough that we cannot trust array.size
349 * try other approaches
351 bitmapsize
= get_component_size(fd
);
353 if (bitmapsize
== 0) {
354 pr_err("Cannot reliably determine size of array to create bitmap - sorry.\n");
358 if (array
.level
== 10) {
361 ncopies
= (array
.layout
& 255) * ((array
.layout
>> 8) & 255);
362 bitmapsize
= bitmapsize
* array
.raid_disks
/ ncopies
;
364 if (strcmp(s
->bitmap_file
, "clustered") == 0 &&
365 !is_near_layout_10(array
.layout
)) {
366 pr_err("only near layout is supported with clustered raid10\n");
371 st
= super_by_fd(fd
, &subarray
);
373 pr_err("Cannot understand version %d.%d\n",
374 array
.major_version
, array
.minor_version
);
378 pr_err("Cannot add bitmaps to sub-arrays yet\n");
384 mdi
= sysfs_read(fd
, NULL
, GET_CONSISTENCY_POLICY
);
386 if (mdi
->consistency_policy
== CONSISTENCY_POLICY_PPL
) {
387 pr_err("Cannot add bitmap to array with PPL\n");
395 if (strcmp(s
->bitmap_file
, "internal") == 0 ||
396 strcmp(s
->bitmap_file
, "clustered") == 0) {
399 int offset_setable
= 0;
400 if (st
->ss
->add_internal_bitmap
== NULL
) {
401 pr_err("Internal bitmaps not supported with %s metadata\n", st
->ss
->name
);
404 st
->nodes
= c
->nodes
;
405 st
->cluster_name
= c
->homecluster
;
406 mdi
= sysfs_read(fd
, NULL
, GET_BITMAP_LOCATION
);
409 for (d
= 0; d
< st
->max_devs
; d
++) {
410 mdu_disk_info_t disk
;
415 if (md_get_disk_info(fd
, &disk
) < 0)
417 if (disk
.major
== 0 && disk
.minor
== 0)
419 if ((disk
.state
& (1 << MD_DISK_SYNC
)) == 0)
421 dv
= map_dev(disk
.major
, disk
.minor
, 1);
424 fd2
= dev_open(dv
, O_RDWR
);
427 rv
= st
->ss
->load_super(st
, fd2
, NULL
);
429 rv
= st
->ss
->add_internal_bitmap(
430 st
, &s
->bitmap_chunk
, c
->delay
,
431 s
->write_behind
, bitmapsize
,
432 offset_setable
, major
);
434 st
->ss
->write_bitmap(st
, fd2
,
437 pr_err("failed to create internal bitmap - chunksize problem.\n");
440 pr_err("failed to load super-block.\n");
446 if (offset_setable
) {
447 st
->ss
->getinfo_super(st
, mdi
, NULL
);
448 if (sysfs_init(mdi
, fd
, NULL
)) {
449 pr_err("failed to initialize sysfs.\n");
452 rv
= sysfs_set_num_signed(mdi
, NULL
, "bitmap/location",
456 if (strcmp(s
->bitmap_file
, "clustered") == 0)
457 array
.state
|= (1 << MD_SB_CLUSTERED
);
458 array
.state
|= (1 << MD_SB_BITMAP_PRESENT
);
459 rv
= md_set_array_info(fd
, &array
);
463 pr_err("Cannot add bitmap while array is resyncing or reshaping etc.\n");
464 pr_err("failed to set internal bitmap.\n");
471 int max_devs
= st
->max_devs
;
473 /* try to load a superblock */
474 for (d
= 0; d
< max_devs
; d
++) {
475 mdu_disk_info_t disk
;
479 if (md_get_disk_info(fd
, &disk
) < 0)
481 if ((disk
.major
==0 && disk
.minor
== 0) ||
482 (disk
.state
& (1 << MD_DISK_REMOVED
)))
484 dv
= map_dev(disk
.major
, disk
.minor
, 1);
487 fd2
= dev_open(dv
, O_RDONLY
);
489 if (st
->ss
->load_super(st
, fd2
, NULL
) == 0) {
491 st
->ss
->uuid_from_super(st
, uuid
);
498 pr_err("cannot find UUID for array!\n");
501 if (CreateBitmap(s
->bitmap_file
, c
->force
, (char*)uuid
,
502 s
->bitmap_chunk
, c
->delay
, s
->write_behind
,
503 bitmapsize
, major
)) {
506 bitmap_fd
= open(s
->bitmap_file
, O_RDWR
);
508 pr_err("weird: %s cannot be opened\n", s
->bitmap_file
);
511 if (ioctl(fd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
514 pr_err("Cannot add bitmap while array is resyncing or reshaping etc.\n");
515 pr_err("Cannot set bitmap file for %s: %s\n",
516 devname
, strerror(err
));
524 int Grow_consistency_policy(char *devname
, int fd
, struct context
*c
, struct shape
*s
)
526 struct supertype
*st
;
529 char *subarray
= NULL
;
531 char container_dev
[PATH_MAX
];
534 if (s
->consistency_policy
!= CONSISTENCY_POLICY_RESYNC
&&
535 s
->consistency_policy
!= CONSISTENCY_POLICY_PPL
) {
536 pr_err("Operation not supported for consistency policy %s\n",
537 map_num(consistency_policies
, s
->consistency_policy
));
541 st
= super_by_fd(fd
, &subarray
);
545 sra
= sysfs_read(fd
, NULL
, GET_CONSISTENCY_POLICY
|GET_LEVEL
|
552 if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
553 !st
->ss
->write_init_ppl
) {
554 pr_err("%s metadata does not support PPL\n", st
->ss
->name
);
559 if (sra
->array
.level
!= 5) {
560 pr_err("Operation not supported for array level %d\n",
566 if (sra
->consistency_policy
== (unsigned)s
->consistency_policy
) {
567 pr_err("Consistency policy is already %s\n",
568 map_num(consistency_policies
, s
->consistency_policy
));
571 } else if (sra
->consistency_policy
!= CONSISTENCY_POLICY_RESYNC
&&
572 sra
->consistency_policy
!= CONSISTENCY_POLICY_PPL
) {
573 pr_err("Current consistency policy is %s, cannot change to %s\n",
574 map_num(consistency_policies
, sra
->consistency_policy
),
575 map_num(consistency_policies
, s
->consistency_policy
));
580 if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
) {
581 if (sysfs_get_str(sra
, NULL
, "sync_action", buf
, 20) <= 0) {
584 } else if (strcmp(buf
, "reshape\n") == 0) {
585 pr_err("PPL cannot be enabled when reshape is in progress\n");
594 if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
)
599 sprintf(container_dev
, "/dev/%s", st
->container_devnm
);
601 ret
= Update_subarray(container_dev
, subarray
, update
, NULL
,
607 if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
) {
614 cfd
= open(container_dev
, O_RDWR
|O_EXCL
);
616 pr_err("Failed to open %s\n", container_dev
);
621 ret
= st
->ss
->load_container(st
, cfd
, st
->container_devnm
);
625 pr_err("Cannot read superblock for %s\n",
630 mdi
= st
->ss
->container_content(st
, subarray
);
635 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
639 devpath
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
640 dfd
= dev_open(devpath
, O_RDWR
);
642 pr_err("Failed to open %s\n", devpath
);
648 ret
= st
->ss
->load_super(st
, dfd
, NULL
);
650 pr_err("Failed to load super-block.\n");
655 ret
= st
->ss
->update_super(st
, sra
, "ppl",
657 c
->verbose
, 0, NULL
);
660 st
->ss
->free_super(st
);
663 st
->ss
->getinfo_super(st
, &info
, NULL
);
666 ret
|= sysfs_set_num(sra
, sd
, "ppl_sector",
668 ret
|= sysfs_set_num(sra
, sd
, "ppl_size",
672 pr_err("Failed to set PPL attributes for %s\n",
675 st
->ss
->free_super(st
);
679 ret
= st
->ss
->write_init_ppl(st
, &info
, dfd
);
681 pr_err("Failed to write PPL\n");
686 st
->ss
->free_super(st
);
693 ret
= sysfs_set_str(sra
, NULL
, "consistency_policy",
694 map_num(consistency_policies
,
695 s
->consistency_policy
));
697 pr_err("Failed to change array consistency policy\n");
709 * When reshaping an array we might need to backup some data.
710 * This is written to all spares with a 'super_block' describing it.
711 * The superblock goes 4K from the end of the used space on the
713 * It if written after the backup is complete.
714 * It has the following structure.
717 static struct mdp_backup_super
{
718 char magic
[16]; /* md_backup_data-1 or -2 */
721 /* start/sizes in 512byte sectors */
722 __u64 devstart
; /* address on backup device/file of data */
725 __u32 sb_csum
; /* csum of preceeding bytes. */
727 __u64 devstart2
; /* offset in to data of second section */
730 __u32 sb_csum2
; /* csum of preceeding bytes. */
732 } __attribute__((aligned(512))) bsb
, bsb2
;
734 static __u32
bsb_csum(char *buf
, int len
)
738 for (i
= 0; i
< len
; i
++)
739 csum
= (csum
<<3) + buf
[0];
740 return __cpu_to_le32(csum
);
743 static int check_idle(struct supertype
*st
)
745 /* Check that all member arrays for this container, or the
746 * container of this array, are idle
748 char *container
= (st
->container_devnm
[0]
749 ? st
->container_devnm
: st
->devnm
);
750 struct mdstat_ent
*ent
, *e
;
753 ent
= mdstat_read(0, 0);
754 for (e
= ent
; e
; e
= e
->next
) {
755 if (!is_container_member(e
, container
))
757 /* frozen array is not idle*/
758 if (e
->percent
>= 0 || e
->metadata_version
[9] == '-') {
767 static int freeze_container(struct supertype
*st
)
769 char *container
= (st
->container_devnm
[0]
770 ? st
->container_devnm
: st
->devnm
);
775 if (block_monitor(container
, 1)) {
776 pr_err("failed to freeze container\n");
783 static void unfreeze_container(struct supertype
*st
)
785 char *container
= (st
->container_devnm
[0]
786 ? st
->container_devnm
: st
->devnm
);
788 unblock_monitor(container
, 1);
791 static int freeze(struct supertype
*st
)
793 /* Try to freeze resync/rebuild on this array/container.
794 * Return -1 if the array is busy,
795 * return -2 container cannot be frozen,
796 * return 0 if this kernel doesn't support 'frozen'
797 * return 1 if it worked.
799 if (st
->ss
->external
)
800 return freeze_container(st
);
802 struct mdinfo
*sra
= sysfs_read(-1, st
->devnm
, GET_VERSION
);
808 /* Need to clear any 'read-auto' status */
809 if (sysfs_get_str(sra
, NULL
, "array_state", buf
, 20) > 0 &&
810 strncmp(buf
, "read-auto", 9) == 0)
811 sysfs_set_str(sra
, NULL
, "array_state", "clean");
813 err
= sysfs_freeze_array(sra
);
819 static void unfreeze(struct supertype
*st
)
821 if (st
->ss
->external
)
822 return unfreeze_container(st
);
824 struct mdinfo
*sra
= sysfs_read(-1, st
->devnm
, GET_VERSION
);
828 sysfs_get_str(sra
, NULL
, "sync_action", buf
, 20) > 0 &&
829 strcmp(buf
, "frozen\n") == 0)
830 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
835 static void wait_reshape(struct mdinfo
*sra
)
837 int fd
= sysfs_get_fd(sra
, NULL
, "sync_action");
843 while (sysfs_fd_get_str(fd
, action
, 20) > 0 &&
844 strncmp(action
, "reshape", 7) == 0)
845 sysfs_wait(fd
, NULL
);
849 static int reshape_super(struct supertype
*st
, unsigned long long size
,
850 int level
, int layout
, int chunksize
, int raid_disks
,
851 int delta_disks
, char *backup_file
, char *dev
,
852 int direction
, int verbose
)
854 /* nothing extra to check in the native case */
855 if (!st
->ss
->external
)
857 if (!st
->ss
->reshape_super
|| !st
->ss
->manage_reshape
) {
858 pr_err("%s metadata does not support reshape\n",
863 return st
->ss
->reshape_super(st
, size
, level
, layout
, chunksize
,
864 raid_disks
, delta_disks
, backup_file
, dev
,
868 static void sync_metadata(struct supertype
*st
)
870 if (st
->ss
->external
) {
871 if (st
->update_tail
) {
872 flush_metadata_updates(st
);
873 st
->update_tail
= &st
->updates
;
875 st
->ss
->sync_metadata(st
);
879 static int subarray_set_num(char *container
, struct mdinfo
*sra
, char *name
, int n
)
881 /* when dealing with external metadata subarrays we need to be
882 * prepared to handle EAGAIN. The kernel may need to wait for
883 * mdmon to mark the array active so the kernel can handle
884 * allocations/writeback when preparing the reshape action
885 * (md_allow_write()). We temporarily disable safe_mode_delay
886 * to close a race with the array_state going clean before the
887 * next write to raid_disks / stripe_cache_size
892 /* only 'raid_disks' and 'stripe_cache_size' trigger md_allow_write */
894 (strcmp(name
, "raid_disks") != 0 &&
895 strcmp(name
, "stripe_cache_size") != 0))
896 return sysfs_set_num(sra
, NULL
, name
, n
);
898 rc
= sysfs_get_str(sra
, NULL
, "safe_mode_delay", safe
, sizeof(safe
));
901 sysfs_set_num(sra
, NULL
, "safe_mode_delay", 0);
902 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
903 if (rc
< 0 && errno
== EAGAIN
) {
904 ping_monitor(container
);
905 /* if we get EAGAIN here then the monitor is not active
908 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
910 sysfs_set_str(sra
, NULL
, "safe_mode_delay", safe
);
914 int start_reshape(struct mdinfo
*sra
, int already_running
,
915 int before_data_disks
, int data_disks
)
918 unsigned long long sync_max_to_set
;
920 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
921 err
= sysfs_set_num(sra
, NULL
, "suspend_hi", sra
->reshape_progress
);
922 err
= err
?: sysfs_set_num(sra
, NULL
, "suspend_lo",
923 sra
->reshape_progress
);
924 if (before_data_disks
<= data_disks
)
925 sync_max_to_set
= sra
->reshape_progress
/ data_disks
;
927 sync_max_to_set
= (sra
->component_size
* data_disks
928 - sra
->reshape_progress
) / data_disks
;
929 if (!already_running
)
930 sysfs_set_num(sra
, NULL
, "sync_min", sync_max_to_set
);
931 err
= err
?: sysfs_set_num(sra
, NULL
, "sync_max", sync_max_to_set
);
932 if (!already_running
&& err
== 0) {
936 err
= sysfs_set_str(sra
, NULL
, "sync_action",
938 err2
= sysfs_set_str(sra
, NULL
, "sync_max",
942 } while (err
&& err2
&& errno
== EBUSY
&& cnt
-- > 0);
947 void abort_reshape(struct mdinfo
*sra
)
949 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
951 * Prior to kernel commit: 23ddff3792f6 ("md: allow suspend_lo and
952 * suspend_hi to decrease as well as increase.")
953 * you could only increase suspend_{lo,hi} unless the region they
954 * covered was empty. So to reset to 0, you need to push suspend_lo
955 * up past suspend_hi first. So to maximize the chance of mdadm
956 * working on all kernels, we want to keep doing that.
958 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
959 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
960 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
961 sysfs_set_num(sra
, NULL
, "sync_min", 0);
962 // It isn't safe to reset sync_max as we aren't monitoring.
963 // Array really should be stopped at this point.
966 int remove_disks_for_takeover(struct supertype
*st
,
971 struct mdinfo
*remaining
;
974 if (st
->ss
->external
) {
976 struct mdinfo
*arrays
= st
->ss
->container_content(st
, NULL
);
978 * containter_content returns list of arrays in container
979 * If arrays->next is not NULL it means that there are
980 * 2 arrays in container and operation should be blocked
987 pr_err("Error. Cannot perform operation on /dev/%s\n", st
->devnm
);
988 pr_err("For this operation it MUST be single array in container\n");
994 if (sra
->array
.level
== 10)
995 nr_of_copies
= layout
& 0xff;
996 else if (sra
->array
.level
== 1)
997 nr_of_copies
= sra
->array
.raid_disks
;
1001 remaining
= sra
->devs
;
1003 /* for each 'copy', select one device and remove from the list. */
1004 for (slot
= 0; slot
< sra
->array
.raid_disks
; slot
+= nr_of_copies
) {
1005 struct mdinfo
**diskp
;
1008 /* Find a working device to keep */
1009 for (diskp
= &remaining
; *diskp
; diskp
= &(*diskp
)->next
) {
1010 struct mdinfo
*disk
= *diskp
;
1012 if (disk
->disk
.raid_disk
< slot
)
1014 if (disk
->disk
.raid_disk
>= slot
+ nr_of_copies
)
1016 if (disk
->disk
.state
& (1<<MD_DISK_REMOVED
))
1018 if (disk
->disk
.state
& (1<<MD_DISK_FAULTY
))
1020 if (!(disk
->disk
.state
& (1<<MD_DISK_SYNC
)))
1023 /* We have found a good disk to use! */
1024 *diskp
= disk
->next
;
1025 disk
->next
= sra
->devs
;
1034 if (slot
< sra
->array
.raid_disks
) {
1035 /* didn't find all slots */
1041 sra
->devs
= remaining
;
1045 /* Remove all 'remaining' devices from the array */
1047 struct mdinfo
*sd
= remaining
;
1048 remaining
= sd
->next
;
1050 sysfs_set_str(sra
, sd
, "state", "faulty");
1051 sysfs_set_str(sra
, sd
, "slot", "none");
1052 /* for external metadata disks should be removed in mdmon */
1053 if (!st
->ss
->external
)
1054 sysfs_set_str(sra
, sd
, "state", "remove");
1055 sd
->disk
.state
|= (1<<MD_DISK_REMOVED
);
1056 sd
->disk
.state
&= ~(1<<MD_DISK_SYNC
);
1057 sd
->next
= sra
->devs
;
1063 void reshape_free_fdlist(int *fdlist
,
1064 unsigned long long *offsets
,
1069 for (i
= 0; i
< size
; i
++)
1077 int reshape_prepare_fdlist(char *devname
,
1081 unsigned long blocks
,
1084 unsigned long long *offsets
)
1089 enable_fds(nrdisks
);
1090 for (d
= 0; d
<= nrdisks
; d
++)
1093 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1094 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
1096 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
) &&
1097 sd
->disk
.raid_disk
< raid_disks
) {
1098 char *dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
1099 fdlist
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDONLY
);
1100 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
1101 if (fdlist
[sd
->disk
.raid_disk
] < 0) {
1102 pr_err("%s: cannot open component %s\n",
1103 devname
, dn
? dn
: "-unknown-");
1107 } else if (backup_file
== NULL
) {
1109 char *dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
1110 fdlist
[d
] = dev_open(dn
, O_RDWR
);
1111 offsets
[d
] = (sd
->data_offset
+ sra
->component_size
- blocks
- 8)*512;
1112 if (fdlist
[d
] < 0) {
1113 pr_err("%s: cannot open component %s\n",
1114 devname
, dn
? dn
: "-unknown-");
1125 int reshape_open_backup_file(char *backup_file
,
1130 unsigned long long *offsets
,
1134 /* Return 1 on success, 0 on any form of failure */
1135 /* need to check backup file is large enough */
1141 *fdlist
= open(backup_file
, O_RDWR
|O_CREAT
|(restart
? O_TRUNC
: O_EXCL
),
1145 pr_err("%s: cannot create backup file %s: %s\n",
1146 devname
, backup_file
, strerror(errno
));
1149 /* Guard against backup file being on array device.
1150 * If array is partitioned or if LVM etc is in the
1151 * way this will not notice, but it is better than
1154 fstat(*fdlist
, &stb
);
1157 if (stb
.st_rdev
== dev
) {
1158 pr_err("backup file must NOT be on the array being reshaped.\n");
1163 memset(buf
, 0, 512);
1164 for (i
=0; i
< blocks
+ 8 ; i
++) {
1165 if (write(*fdlist
, buf
, 512) != 512) {
1166 pr_err("%s: cannot create backup file %s: %s\n",
1167 devname
, backup_file
, strerror(errno
));
1171 if (fsync(*fdlist
) != 0) {
1172 pr_err("%s: cannot create backup file %s: %s\n",
1173 devname
, backup_file
, strerror(errno
));
1177 if (!restart
&& strncmp(backup_file
, MAP_DIR
, strlen(MAP_DIR
)) != 0) {
1178 char *bu
= make_backup(sys_name
);
1179 if (symlink(backup_file
, bu
))
1180 pr_err("Recording backup file in " MAP_DIR
" failed: %s\n",
1188 unsigned long compute_backup_blocks(int nchunk
, int ochunk
,
1189 unsigned int ndata
, unsigned int odata
)
1191 unsigned long a
, b
, blocks
;
1192 /* So how much do we need to backup.
1193 * We need an amount of data which is both a whole number of
1194 * old stripes and a whole number of new stripes.
1195 * So LCM for (chunksize*datadisks).
1197 a
= (ochunk
/512) * odata
;
1198 b
= (nchunk
/512) * ndata
;
1201 /* LCM == product / GCD */
1202 blocks
= (unsigned long)(ochunk
/512) * (unsigned long)(nchunk
/512) *
1208 char *analyse_change(char *devname
, struct mdinfo
*info
, struct reshape
*re
)
1210 /* Based on the current array state in info->array and
1211 * the changes in info->new_* etc, determine:
1212 * - whether the change is possible
1213 * - Intermediate level/raid_disks/layout
1214 * - whether a restriping reshape is needed
1215 * - number of sectors in minimum change unit. This
1216 * will cover a whole number of stripes in 'before' and
1219 * Return message if the change should be rejected
1220 * NULL if the change can be achieved
1222 * This can be called as part of starting a reshape, or
1223 * when assembling an array that is undergoing reshape.
1225 int near
, far
, offset
, copies
;
1227 int old_chunk
, new_chunk
;
1228 /* delta_parity records change in number of devices
1229 * caused by level change
1231 int delta_parity
= 0;
1233 memset(re
, 0, sizeof(*re
));
1235 /* If a new level not explicitly given, we assume no-change */
1236 if (info
->new_level
== UnSet
)
1237 info
->new_level
= info
->array
.level
;
1239 if (info
->new_chunk
)
1240 switch (info
->new_level
) {
1246 /* chunk size is meaningful, must divide component_size
1249 if (info
->component_size
% (info
->new_chunk
/512)) {
1250 unsigned long long shrink
= info
->component_size
;
1251 shrink
&= ~(unsigned long long)(info
->new_chunk
/512-1);
1252 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk)\n",
1253 info
->new_chunk
/1024, info
->component_size
/2);
1254 pr_err("After shrinking any filesystem, \"mdadm --grow %s --size %llu\"\n",
1256 pr_err("will shrink the array so the given chunk size would work.\n");
1261 return "chunk size not meaningful for this level";
1264 info
->new_chunk
= info
->array
.chunk_size
;
1266 switch (info
->array
.level
) {
1268 return "No reshape is possibly for this RAID level";
1270 if (info
->delta_disks
!= UnSet
)
1271 return "Only --add is supported for LINEAR, setting --raid-disks is not needed";
1273 return "Only --add is supported for LINEAR, other --grow options are not meaningful";
1275 /* RAID1 can convert to RAID1 with different disks, or
1276 * raid5 with 2 disks, or
1279 if (info
->new_level
> 1 && (info
->component_size
& 7))
1280 return "Cannot convert RAID1 of this size - reduce size to multiple of 4K first.";
1281 if (info
->new_level
== 0) {
1282 if (info
->delta_disks
!= UnSet
&&
1283 info
->delta_disks
!= 0)
1284 return "Cannot change number of disks with RAID1->RAID0 conversion";
1286 re
->before
.data_disks
= 1;
1287 re
->after
.data_disks
= 1;
1290 if (info
->new_level
== 1) {
1291 if (info
->delta_disks
== UnSet
)
1292 /* Don't know what to do */
1293 return "no change requested for Growing RAID1";
1297 if (info
->array
.raid_disks
!= 2 && info
->new_level
== 5)
1298 return "Can only convert a 2-device array to RAID5";
1299 if (info
->array
.raid_disks
== 2 && info
->new_level
== 5) {
1301 re
->before
.data_disks
= 1;
1302 if (info
->delta_disks
!= UnSet
&&
1303 info
->delta_disks
!= 0)
1304 re
->after
.data_disks
= 1 + info
->delta_disks
;
1306 re
->after
.data_disks
= 1;
1307 if (re
->after
.data_disks
< 1)
1308 return "Number of disks too small for RAID5";
1310 re
->before
.layout
= ALGORITHM_LEFT_SYMMETRIC
;
1311 info
->array
.chunk_size
= 65536;
1314 /* Could do some multi-stage conversions, but leave that to
1317 return "Impossibly level change request for RAID1";
1320 /* RAID10 can be converted from near mode to
1321 * RAID0 by removing some devices.
1322 * It can also be reshaped if the kernel supports
1325 switch (info
->new_level
) {
1327 if ((info
->array
.layout
& ~0xff) != 0x100)
1328 return "Cannot Grow RAID10 with far/offset layout";
1330 * number of devices must be multiple of
1333 if (info
->array
.raid_disks
%
1334 (info
->array
.layout
& 0xff))
1335 return "RAID10 layout too complex for Grow operation";
1337 new_disks
= (info
->array
.raid_disks
/
1338 (info
->array
.layout
& 0xff));
1339 if (info
->delta_disks
== UnSet
)
1340 info
->delta_disks
= (new_disks
1341 - info
->array
.raid_disks
);
1343 if (info
->delta_disks
!=
1344 new_disks
- info
->array
.raid_disks
)
1345 return "New number of raid-devices impossible for RAID10";
1346 if (info
->new_chunk
&&
1347 info
->new_chunk
!= info
->array
.chunk_size
)
1348 return "Cannot change chunk-size with RAID10 Grow";
1352 re
->before
.data_disks
= new_disks
;
1353 re
->after
.data_disks
= re
->before
.data_disks
;
1357 near
= info
->array
.layout
& 0xff;
1358 far
= (info
->array
.layout
>> 8) & 0xff;
1359 offset
= info
->array
.layout
& 0x10000;
1360 if (far
> 1 && !offset
)
1361 return "Cannot reshape RAID10 in far-mode";
1362 copies
= near
* far
;
1364 old_chunk
= info
->array
.chunk_size
* far
;
1366 if (info
->new_layout
== UnSet
)
1367 info
->new_layout
= info
->array
.layout
;
1369 near
= info
->new_layout
& 0xff;
1370 far
= (info
->new_layout
>> 8) & 0xff;
1371 offset
= info
->new_layout
& 0x10000;
1372 if (far
> 1 && !offset
)
1373 return "Cannot reshape RAID10 to far-mode";
1374 if (near
* far
!= copies
)
1375 return "Cannot change number of copies when reshaping RAID10";
1377 if (info
->delta_disks
== UnSet
)
1378 info
->delta_disks
= 0;
1379 new_disks
= (info
->array
.raid_disks
+
1382 new_chunk
= info
->new_chunk
* far
;
1385 re
->before
.layout
= info
->array
.layout
;
1386 re
->before
.data_disks
= info
->array
.raid_disks
;
1387 re
->after
.layout
= info
->new_layout
;
1388 re
->after
.data_disks
= new_disks
;
1389 /* For RAID10 we don't do backup but do allow reshape,
1390 * so set backup_blocks to INVALID_SECTORS rather than
1392 * And there is no need to synchronise stripes on both
1393 * 'old' and 'new'. So the important
1394 * number is the minimum data_offset difference
1395 * which is the larger of (offset copies * chunk).
1397 re
->backup_blocks
= INVALID_SECTORS
;
1398 re
->min_offset_change
= max(old_chunk
, new_chunk
) / 512;
1399 if (new_disks
< re
->before
.data_disks
&&
1400 info
->space_after
< re
->min_offset_change
)
1401 /* Reduce component size by one chunk */
1402 re
->new_size
= (info
->component_size
-
1403 re
->min_offset_change
);
1405 re
->new_size
= info
->component_size
;
1406 re
->new_size
= re
->new_size
* new_disks
/ copies
;
1410 return "RAID10 can only be changed to RAID0";
1413 /* RAID0 can be converted to RAID10, or to RAID456 */
1414 if (info
->new_level
== 10) {
1415 if (info
->new_layout
== UnSet
&&
1416 info
->delta_disks
== UnSet
) {
1417 /* Assume near=2 layout */
1418 info
->new_layout
= 0x102;
1419 info
->delta_disks
= info
->array
.raid_disks
;
1421 if (info
->new_layout
== UnSet
) {
1422 int copies
= 1 + (info
->delta_disks
1423 / info
->array
.raid_disks
);
1424 if (info
->array
.raid_disks
* (copies
-1) !=
1426 return "Impossible number of devices for RAID0->RAID10";
1427 info
->new_layout
= 0x100 + copies
;
1429 if (info
->delta_disks
== UnSet
) {
1430 int copies
= info
->new_layout
& 0xff;
1431 if (info
->new_layout
!= 0x100 + copies
)
1432 return "New layout impossible for RAID0->RAID10";;
1433 info
->delta_disks
= (copies
- 1) *
1434 info
->array
.raid_disks
;
1436 if (info
->new_chunk
&&
1437 info
->new_chunk
!= info
->array
.chunk_size
)
1438 return "Cannot change chunk-size with RAID0->RAID10";
1441 re
->before
.data_disks
= (info
->array
.raid_disks
+
1443 re
->after
.data_disks
= re
->before
.data_disks
;
1444 re
->before
.layout
= info
->new_layout
;
1448 /* RAID0 can also covert to RAID0/4/5/6 by first converting to
1449 * a raid4 style layout of the final level.
1451 switch (info
->new_level
) {
1456 re
->before
.layout
= 0;
1461 re
->before
.layout
= ALGORITHM_PARITY_N
;
1462 if (info
->new_layout
== UnSet
)
1463 info
->new_layout
= map_name(r5layout
, "default");
1468 re
->before
.layout
= ALGORITHM_PARITY_N
;
1469 if (info
->new_layout
== UnSet
)
1470 info
->new_layout
= map_name(r6layout
, "default");
1473 return "Impossible level change requested";
1475 re
->before
.data_disks
= info
->array
.raid_disks
;
1476 /* determining 'after' layout happens outside this 'switch' */
1480 info
->array
.layout
= ALGORITHM_PARITY_N
;
1482 switch (info
->new_level
) {
1486 re
->level
= info
->array
.level
;
1487 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1488 re
->before
.layout
= info
->array
.layout
;
1492 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1493 re
->before
.layout
= info
->array
.layout
;
1498 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1499 switch (info
->array
.layout
) {
1500 case ALGORITHM_LEFT_ASYMMETRIC
:
1501 re
->before
.layout
= ALGORITHM_LEFT_ASYMMETRIC_6
;
1503 case ALGORITHM_RIGHT_ASYMMETRIC
:
1504 re
->before
.layout
= ALGORITHM_RIGHT_ASYMMETRIC_6
;
1506 case ALGORITHM_LEFT_SYMMETRIC
:
1507 re
->before
.layout
= ALGORITHM_LEFT_SYMMETRIC_6
;
1509 case ALGORITHM_RIGHT_SYMMETRIC
:
1510 re
->before
.layout
= ALGORITHM_RIGHT_SYMMETRIC_6
;
1512 case ALGORITHM_PARITY_0
:
1513 re
->before
.layout
= ALGORITHM_PARITY_0_6
;
1515 case ALGORITHM_PARITY_N
:
1516 re
->before
.layout
= ALGORITHM_PARITY_N_6
;
1519 return "Cannot convert an array with this layout";
1523 if (info
->array
.raid_disks
!= 2)
1524 return "Can only convert a 2-device array to RAID1";
1525 if (info
->delta_disks
!= UnSet
&&
1526 info
->delta_disks
!= 0)
1527 return "Cannot set raid_disk when converting RAID5->RAID1";
1529 info
->new_chunk
= 0;
1532 return "Impossible level change requested";
1536 switch (info
->new_level
) {
1542 re
->before
.data_disks
= info
->array
.raid_disks
- 2;
1543 re
->before
.layout
= info
->array
.layout
;
1546 return "Impossible level change requested";
1551 /* If we reached here then it looks like a re-stripe is
1552 * happening. We have determined the intermediate level
1553 * and initial raid_disks/layout and stored these in 're'.
1555 * We need to deduce the final layout that can be atomically
1556 * converted to the end state.
1558 switch (info
->new_level
) {
1560 /* We can only get to RAID0 from RAID4 or RAID5
1561 * with appropriate layout and one extra device
1563 if (re
->level
!= 4 && re
->level
!= 5)
1564 return "Cannot covert to RAID0 from this level";
1566 switch (re
->level
) {
1568 re
->before
.layout
= 0;
1569 re
->after
.layout
= 0;
1572 re
->after
.layout
= ALGORITHM_PARITY_N
;
1578 /* We can only get to RAID4 from RAID5 */
1579 if (re
->level
!= 4 && re
->level
!= 5)
1580 return "Cannot convert to RAID4 from this level";
1582 switch (re
->level
) {
1584 re
->after
.layout
= 0;
1587 re
->after
.layout
= ALGORITHM_PARITY_N
;
1593 /* We get to RAID5 from RAID5 or RAID6 */
1594 if (re
->level
!= 5 && re
->level
!= 6)
1595 return "Cannot convert to RAID5 from this level";
1597 switch (re
->level
) {
1599 if (info
->new_layout
== UnSet
)
1600 re
->after
.layout
= re
->before
.layout
;
1602 re
->after
.layout
= info
->new_layout
;
1605 if (info
->new_layout
== UnSet
)
1606 info
->new_layout
= re
->before
.layout
;
1608 /* after.layout needs to be raid6 version of new_layout */
1609 if (info
->new_layout
== ALGORITHM_PARITY_N
)
1610 re
->after
.layout
= ALGORITHM_PARITY_N
;
1613 char *ls
= map_num(r5layout
, info
->new_layout
);
1616 /* Current RAID6 layout has a RAID5
1619 strcat(strcpy(layout
, ls
), "-6");
1620 l
= map_name(r6layout
, layout
);
1622 return "Cannot find RAID6 layout to convert to";
1624 /* Current RAID6 has no equivalent.
1625 * If it is already a '-6' layout we
1626 * can leave it unchanged, else we must
1629 ls
= map_num(r6layout
,
1632 strcmp(ls
+strlen(ls
)-2, "-6") != 0)
1633 return "Please specify new layout";
1634 l
= info
->new_layout
;
1636 re
->after
.layout
= l
;
1642 /* We must already be at level 6 */
1644 return "Impossible level change";
1645 if (info
->new_layout
== UnSet
)
1646 re
->after
.layout
= info
->array
.layout
;
1648 re
->after
.layout
= info
->new_layout
;
1651 return "Impossible level change requested";
1653 if (info
->delta_disks
== UnSet
)
1654 info
->delta_disks
= delta_parity
;
1656 re
->after
.data_disks
=
1657 (re
->before
.data_disks
+ info
->delta_disks
- delta_parity
);
1659 switch (re
->level
) {
1671 /* So we have a restripe operation, we need to calculate the number
1672 * of blocks per reshape operation.
1674 re
->new_size
= info
->component_size
* re
->before
.data_disks
;
1675 if (info
->new_chunk
== 0)
1676 info
->new_chunk
= info
->array
.chunk_size
;
1677 if (re
->after
.data_disks
== re
->before
.data_disks
&&
1678 re
->after
.layout
== re
->before
.layout
&&
1679 info
->new_chunk
== info
->array
.chunk_size
) {
1680 /* Nothing to change, can change level immediately. */
1681 re
->level
= info
->new_level
;
1682 re
->backup_blocks
= 0;
1685 if (re
->after
.data_disks
== 1 && re
->before
.data_disks
== 1) {
1686 /* chunk and layout changes make no difference */
1687 re
->level
= info
->new_level
;
1688 re
->backup_blocks
= 0;
1692 if (re
->after
.data_disks
== re
->before
.data_disks
&&
1693 get_linux_version() < 2006032)
1694 return "in-place reshape is not safe before 2.6.32 - sorry.";
1696 if (re
->after
.data_disks
< re
->before
.data_disks
&&
1697 get_linux_version() < 2006030)
1698 return "reshape to fewer devices is not supported before 2.6.30 - sorry.";
1700 re
->backup_blocks
= compute_backup_blocks(
1701 info
->new_chunk
, info
->array
.chunk_size
,
1702 re
->after
.data_disks
, re
->before
.data_disks
);
1703 re
->min_offset_change
= re
->backup_blocks
/ re
->before
.data_disks
;
1705 re
->new_size
= info
->component_size
* re
->after
.data_disks
;
1709 static int set_array_size(struct supertype
*st
, struct mdinfo
*sra
,
1712 struct mdinfo
*info
;
1716 if ((st
== NULL
) || (sra
== NULL
))
1719 if (text_version
== NULL
)
1720 text_version
= sra
->text_version
;
1721 subarray
= strchr(text_version
+ 1, '/')+1;
1722 info
= st
->ss
->container_content(st
, subarray
);
1724 unsigned long long current_size
= 0;
1725 unsigned long long new_size
= info
->custom_array_size
/2;
1727 if (sysfs_get_ll(sra
, NULL
, "array_size", ¤t_size
) == 0 &&
1728 new_size
> current_size
) {
1729 if (sysfs_set_num(sra
, NULL
, "array_size", new_size
)
1731 dprintf("Error: Cannot set array size");
1734 dprintf("Array size changed");
1736 dprintf_cont(" from %llu to %llu.\n",
1737 current_size
, new_size
);
1741 dprintf("Error: set_array_size(): info pointer in NULL\n");
1746 static int reshape_array(char *container
, int fd
, char *devname
,
1747 struct supertype
*st
, struct mdinfo
*info
,
1748 int force
, struct mddev_dev
*devlist
,
1749 unsigned long long data_offset
,
1750 char *backup_file
, int verbose
, int forked
,
1751 int restart
, int freeze_reshape
);
1752 static int reshape_container(char *container
, char *devname
,
1754 struct supertype
*st
,
1755 struct mdinfo
*info
,
1757 char *backup_file
, int verbose
,
1758 int forked
, int restart
, int freeze_reshape
);
1760 int Grow_reshape(char *devname
, int fd
,
1761 struct mddev_dev
*devlist
,
1762 unsigned long long data_offset
,
1763 struct context
*c
, struct shape
*s
)
1765 /* Make some changes in the shape of an array.
1766 * The kernel must support the change.
1768 * There are three different changes. Each can trigger
1769 * a resync or recovery so we freeze that until we have
1770 * requested everything (if kernel supports freezing - 2.6.30).
1772 * - change size (i.e. component_size)
1774 * - change layout/chunksize/ndisks
1776 * The last can require a reshape. It is different on different
1777 * levels so we need to check the level before actioning it.
1778 * Some times the level change needs to be requested after the
1779 * reshape (e.g. raid6->raid5, raid5->raid0)
1782 struct mdu_array_info_s array
;
1784 struct supertype
*st
;
1785 char *subarray
= NULL
;
1789 char *container
= NULL
;
1792 struct mddev_dev
*dv
;
1798 if (md_get_array_info(fd
, &array
) < 0) {
1799 pr_err("%s is not an active md array - aborting\n",
1803 if (s
->level
!= UnSet
&& s
->chunk
) {
1804 pr_err("Cannot change array level in the same operation as changing chunk size.\n");
1808 if (data_offset
!= INVALID_SECTORS
&& array
.level
!= 10 &&
1809 (array
.level
< 4 || array
.level
> 6)) {
1810 pr_err("--grow --data-offset not yet supported\n");
1815 (s
->chunk
|| s
->level
!= UnSet
|| s
->layout_str
|| s
->raiddisks
)) {
1816 pr_err("cannot change component size at the same time as other changes.\n"
1817 " Change size first, then check data is intact before making other changes.\n");
1821 if (s
->raiddisks
&& s
->raiddisks
< array
.raid_disks
&&
1822 array
.level
> 1 && get_linux_version() < 2006032 &&
1823 !check_env("MDADM_FORCE_FEWER")) {
1824 pr_err("reducing the number of devices is not safe before Linux 2.6.32\n"
1825 " Please use a newer kernel\n");
1829 if (array
.level
> 1 && s
->size
> 1 &&
1830 (unsigned long long) (array
.chunk_size
/ 1024) > s
->size
) {
1831 pr_err("component size must be larger than chunk size.\n");
1835 st
= super_by_fd(fd
, &subarray
);
1837 pr_err("Unable to determine metadata format for %s\n", devname
);
1840 if (s
->raiddisks
> st
->max_devs
) {
1841 pr_err("Cannot increase raid-disks on this array beyond %d\n", st
->max_devs
);
1844 if (s
->level
== 0 &&
1845 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)) &&
1846 !(array
.state
& (1<<MD_SB_CLUSTERED
))) {
1847 array
.state
&= ~(1<<MD_SB_BITMAP_PRESENT
);
1848 if (md_set_array_info(fd
, &array
)!= 0) {
1849 pr_err("failed to remove internal bitmap.\n");
1854 /* in the external case we need to check that the requested reshape is
1855 * supported, and perform an initial check that the container holds the
1856 * pre-requisite spare devices (mdmon owns final validation)
1858 if (st
->ss
->external
) {
1862 container
= st
->container_devnm
;
1863 cfd
= open_dev_excl(st
->container_devnm
);
1865 container
= st
->devnm
;
1867 cfd
= open_dev_excl(st
->devnm
);
1871 pr_err("Unable to open container for %s\n", devname
);
1876 retval
= st
->ss
->load_container(st
, cfd
, NULL
);
1879 pr_err("Cannot read superblock for %s\n", devname
);
1884 /* check if operation is supported for metadata handler */
1885 if (st
->ss
->container_content
) {
1886 struct mdinfo
*cc
= NULL
;
1887 struct mdinfo
*content
= NULL
;
1889 cc
= st
->ss
->container_content(st
, subarray
);
1890 for (content
= cc
; content
; content
= content
->next
) {
1891 int allow_reshape
= 1;
1893 /* check if reshape is allowed based on metadata
1894 * indications stored in content.array.status
1896 if (content
->array
.state
&
1897 (1 << MD_SB_BLOCK_VOLUME
))
1899 if (content
->array
.state
&
1900 (1 << MD_SB_BLOCK_CONTAINER_RESHAPE
))
1902 if (!allow_reshape
) {
1903 pr_err("cannot reshape arrays in container with unsupported metadata: %s(%s)\n",
1904 devname
, container
);
1909 if (content
->consistency_policy
==
1910 CONSISTENCY_POLICY_PPL
) {
1911 pr_err("Operation not supported when ppl consistency policy is enabled\n");
1919 if (mdmon_running(container
))
1920 st
->update_tail
= &st
->updates
;
1924 for (dv
= devlist
; dv
; dv
= dv
->next
)
1926 if (s
->raiddisks
> array
.raid_disks
&&
1927 array
.spare_disks
+ added_disks
<
1928 (s
->raiddisks
- array
.raid_disks
) &&
1930 pr_err("Need %d spare%s to avoid degraded array, and only have %d.\n"
1931 " Use --force to over-ride this check.\n",
1932 s
->raiddisks
- array
.raid_disks
,
1933 s
->raiddisks
- array
.raid_disks
== 1 ? "" : "s",
1934 array
.spare_disks
+ added_disks
);
1938 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
| GET_DISKS
| GET_DEVS
|
1939 GET_STATE
| GET_VERSION
);
1941 if (st
->ss
->external
&& subarray
== NULL
) {
1942 array
.level
= LEVEL_CONTAINER
;
1943 sra
->array
.level
= LEVEL_CONTAINER
;
1946 pr_err("failed to read sysfs parameters for %s\n",
1950 frozen
= freeze(st
);
1952 /* freeze() already spewed the reason */
1955 } else if (frozen
< 0) {
1956 pr_err("%s is performing resync/recovery and cannot be reshaped\n", devname
);
1961 /* ========= set size =============== */
1963 (s
->size
== MAX_SIZE
|| s
->size
!= (unsigned)array
.size
)) {
1964 unsigned long long orig_size
= get_component_size(fd
)/2;
1965 unsigned long long min_csize
;
1967 int raid0_takeover
= 0;
1970 orig_size
= (unsigned) array
.size
;
1972 if (orig_size
== 0) {
1973 pr_err("Cannot set device size in this type of array.\n");
1978 if (reshape_super(st
, s
->size
, UnSet
, UnSet
, 0, 0, UnSet
, NULL
,
1979 devname
, APPLY_METADATA_CHANGES
,
1985 if (st
->ss
->external
) {
1986 /* metadata can have size limitation
1987 * update size value according to metadata information
1989 struct mdinfo
*sizeinfo
=
1990 st
->ss
->container_content(st
, subarray
);
1992 unsigned long long new_size
=
1993 sizeinfo
->custom_array_size
/2;
1994 int data_disks
= get_data_disks(
1995 sizeinfo
->array
.level
,
1996 sizeinfo
->array
.layout
,
1997 sizeinfo
->array
.raid_disks
);
1998 new_size
/= data_disks
;
1999 dprintf("Metadata size correction from %llu to %llu (%llu)\n",
2000 orig_size
, new_size
,
2001 new_size
* data_disks
);
2003 sysfs_free(sizeinfo
);
2007 /* Update the size of each member device in case
2008 * they have been resized. This will never reduce
2009 * below the current used-size. The "size" attribute
2010 * understands '0' to mean 'max'.
2013 for (mdi
= sra
->devs
; mdi
; mdi
= mdi
->next
) {
2014 sysfs_set_num(sra
, mdi
, "size",
2015 s
->size
== MAX_SIZE
? 0 : s
->size
);
2016 if (array
.not_persistent
== 0 &&
2017 array
.major_version
== 0 &&
2018 get_linux_version() < 3001000) {
2019 /* Dangerous to allow size to exceed 2TB */
2020 unsigned long long csize
;
2021 if (sysfs_get_ll(sra
, mdi
, "size",
2023 if (csize
>= 2ULL*1024*1024*1024)
2024 csize
= 2ULL*1024*1024*1024;
2025 if ((min_csize
== 0 ||
2026 (min_csize
> csize
)))
2031 if (min_csize
&& s
->size
> min_csize
) {
2032 pr_err("Cannot safely make this array use more than 2TB per device on this kernel.\n");
2034 goto size_change_error
;
2036 if (min_csize
&& s
->size
== MAX_SIZE
) {
2037 /* Don't let the kernel choose a size - it will get
2040 pr_err("Limited v0.90 array to 2TB per device\n");
2041 s
->size
= min_csize
;
2043 if (st
->ss
->external
) {
2044 if (sra
->array
.level
== 0) {
2045 rv
= sysfs_set_str(sra
, NULL
, "level", "raid5");
2048 /* get array parameters after takeover
2049 * to change one parameter at time only
2051 rv
= md_get_array_info(fd
, &array
);
2054 /* make sure mdmon is
2055 * aware of the new level */
2056 if (!mdmon_running(st
->container_devnm
))
2057 start_mdmon(st
->container_devnm
);
2058 ping_monitor(container
);
2059 if (mdmon_running(st
->container_devnm
) &&
2060 st
->update_tail
== NULL
)
2061 st
->update_tail
= &st
->updates
;
2064 if (s
->size
== MAX_SIZE
)
2066 array
.size
= s
->size
;
2067 if (s
->size
& ~INT32_MAX
) {
2068 /* got truncated to 32bit, write to
2069 * component_size instead
2072 rv
= sysfs_set_num(sra
, NULL
,
2073 "component_size", s
->size
);
2077 rv
= md_set_array_info(fd
, &array
);
2079 /* manage array size when it is managed externally
2081 if ((rv
== 0) && st
->ss
->external
)
2082 rv
= set_array_size(st
, sra
, sra
->text_version
);
2085 if (raid0_takeover
) {
2086 /* do not recync non-existing parity,
2087 * we will drop it anyway
2089 sysfs_set_str(sra
, NULL
, "sync_action", "frozen");
2090 /* go back to raid0, drop parity disk
2092 sysfs_set_str(sra
, NULL
, "level", "raid0");
2093 md_get_array_info(fd
, &array
);
2100 /* restore metadata */
2101 if (reshape_super(st
, orig_size
, UnSet
, UnSet
, 0, 0,
2102 UnSet
, NULL
, devname
,
2103 ROLLBACK_METADATA_CHANGES
,
2106 pr_err("Cannot set device size for %s: %s\n",
2107 devname
, strerror(err
));
2109 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2110 cont_err("Bitmap must be removed before size can be changed\n");
2114 if (s
->assume_clean
) {
2115 /* This will fail on kernels older than 3.0 unless
2116 * a backport has been arranged.
2119 sysfs_set_str(sra
, NULL
, "resync_start",
2121 pr_err("--assume-clean not supported with --grow on this kernel\n");
2123 md_get_array_info(fd
, &array
);
2124 s
->size
= get_component_size(fd
)/2;
2126 s
->size
= array
.size
;
2127 if (c
->verbose
>= 0) {
2128 if (s
->size
== orig_size
)
2129 pr_err("component size of %s unchanged at %lluK\n",
2132 pr_err("component size of %s has been set to %lluK\n",
2136 } else if (array
.level
!= LEVEL_CONTAINER
) {
2137 s
->size
= get_component_size(fd
)/2;
2139 s
->size
= array
.size
;
2142 /* See if there is anything else to do */
2143 if ((s
->level
== UnSet
|| s
->level
== array
.level
) &&
2144 (s
->layout_str
== NULL
) &&
2145 (s
->chunk
== 0 || s
->chunk
== array
.chunk_size
) &&
2146 data_offset
== INVALID_SECTORS
&&
2147 (s
->raiddisks
== 0 || s
->raiddisks
== array
.raid_disks
)) {
2148 /* Nothing more to do */
2149 if (!changed
&& c
->verbose
>= 0)
2150 pr_err("%s: no change requested\n", devname
);
2154 /* ========= check for Raid10/Raid1 -> Raid0 conversion ===============
2155 * current implementation assumes that following conditions must be met:
2158 * - near_copies == 2
2160 if ((s
->level
== 0 && array
.level
== 10 && sra
&&
2161 array
.layout
== ((1 << 8) + 2) && !(array
.raid_disks
& 1)) ||
2162 (s
->level
== 0 && array
.level
== 1 && sra
)) {
2165 err
= remove_disks_for_takeover(st
, sra
, array
.layout
);
2167 dprintf("Array cannot be reshaped\n");
2173 /* Make sure mdmon has seen the device removal
2174 * and updated metadata before we continue with
2178 ping_monitor(container
);
2181 memset(&info
, 0, sizeof(info
));
2183 if (sysfs_init(&info
, fd
, NULL
)) {
2184 pr_err("failed to initialize sysfs.\n");
2188 strcpy(info
.text_version
, sra
->text_version
);
2189 info
.component_size
= s
->size
*2;
2190 info
.new_level
= s
->level
;
2191 info
.new_chunk
= s
->chunk
* 1024;
2192 if (info
.array
.level
== LEVEL_CONTAINER
) {
2193 info
.delta_disks
= UnSet
;
2194 info
.array
.raid_disks
= s
->raiddisks
;
2195 } else if (s
->raiddisks
)
2196 info
.delta_disks
= s
->raiddisks
- info
.array
.raid_disks
;
2198 info
.delta_disks
= UnSet
;
2199 if (s
->layout_str
== NULL
) {
2200 info
.new_layout
= UnSet
;
2201 if (info
.array
.level
== 6 &&
2202 (info
.new_level
== 6 || info
.new_level
== UnSet
) &&
2203 info
.array
.layout
>= 16) {
2204 pr_err("%s has a non-standard layout. If you wish to preserve this\n", devname
);
2205 cont_err("during the reshape, please specify --layout=preserve\n");
2206 cont_err("If you want to change it, specify a layout or use --layout=normalise\n");
2210 } else if (strcmp(s
->layout_str
, "normalise") == 0 ||
2211 strcmp(s
->layout_str
, "normalize") == 0) {
2212 /* If we have a -6 RAID6 layout, remove the '-6'. */
2213 info
.new_layout
= UnSet
;
2214 if (info
.array
.level
== 6 && info
.new_level
== UnSet
) {
2216 strcpy(l
, map_num(r6layout
, info
.array
.layout
));
2217 h
= strrchr(l
, '-');
2218 if (h
&& strcmp(h
, "-6") == 0) {
2220 info
.new_layout
= map_name(r6layout
, l
);
2223 pr_err("%s is only meaningful when reshaping a RAID6 array.\n", s
->layout_str
);
2227 } else if (strcmp(s
->layout_str
, "preserve") == 0) {
2228 /* This means that a non-standard RAID6 layout
2231 * - When reshape a RAID6 (e.g. adding a device)
2232 * which is in a non-standard layout, it is OK
2233 * to preserve that layout.
2234 * - When converting a RAID5 to RAID6, leave it in
2235 * the XXX-6 layout, don't re-layout.
2237 if (info
.array
.level
== 6 && info
.new_level
== UnSet
)
2238 info
.new_layout
= info
.array
.layout
;
2239 else if (info
.array
.level
== 5 && info
.new_level
== 6) {
2241 strcpy(l
, map_num(r5layout
, info
.array
.layout
));
2243 info
.new_layout
= map_name(r6layout
, l
);
2245 pr_err("%s in only meaningful when reshaping to RAID6\n", s
->layout_str
);
2250 int l
= info
.new_level
;
2252 l
= info
.array
.level
;
2255 info
.new_layout
= map_name(r5layout
, s
->layout_str
);
2258 info
.new_layout
= map_name(r6layout
, s
->layout_str
);
2261 info
.new_layout
= parse_layout_10(s
->layout_str
);
2264 info
.new_layout
= parse_layout_faulty(s
->layout_str
);
2267 pr_err("layout not meaningful with this level\n");
2271 if (info
.new_layout
== UnSet
) {
2272 pr_err("layout %s not understood for this level\n",
2279 if (array
.level
== LEVEL_FAULTY
) {
2280 if (s
->level
!= UnSet
&& s
->level
!= array
.level
) {
2281 pr_err("cannot change level of Faulty device\n");
2285 pr_err("cannot set chunksize of Faulty device\n");
2288 if (s
->raiddisks
&& s
->raiddisks
!= 1) {
2289 pr_err("cannot set raid_disks of Faulty device\n");
2292 if (s
->layout_str
) {
2293 if (md_get_array_info(fd
, &array
) != 0) {
2294 dprintf("Cannot get array information.\n");
2297 array
.layout
= info
.new_layout
;
2298 if (md_set_array_info(fd
, &array
) != 0) {
2299 pr_err("failed to set new layout\n");
2301 } else if (c
->verbose
>= 0)
2302 printf("layout for %s set to %d\n",
2303 devname
, array
.layout
);
2305 } else if (array
.level
== LEVEL_CONTAINER
) {
2306 /* This change is to be applied to every array in the
2307 * container. This is only needed when the metadata imposes
2308 * restraints of the various arrays in the container.
2309 * Currently we only know that IMSM requires all arrays
2310 * to have the same number of devices so changing the
2311 * number of devices (On-Line Capacity Expansion) must be
2312 * performed at the level of the container
2318 rv
= reshape_container(container
, devname
, -1, st
, &info
,
2319 c
->force
, c
->backup_file
, c
->verbose
,
2323 /* get spare devices from external metadata
2325 if (st
->ss
->external
) {
2326 struct mdinfo
*info2
;
2328 info2
= st
->ss
->container_content(st
, subarray
);
2330 info
.array
.spare_disks
=
2331 info2
->array
.spare_disks
;
2336 /* Impose these changes on a single array. First
2337 * check that the metadata is OK with the change. */
2339 if (reshape_super(st
, 0, info
.new_level
,
2340 info
.new_layout
, info
.new_chunk
,
2341 info
.array
.raid_disks
, info
.delta_disks
,
2342 c
->backup_file
, devname
,
2343 APPLY_METADATA_CHANGES
, c
->verbose
)) {
2348 rv
= reshape_array(container
, fd
, devname
, st
, &info
, c
->force
,
2349 devlist
, data_offset
, c
->backup_file
,
2350 c
->verbose
, 0, 0, 0);
2360 /* verify_reshape_position()
2361 * Function checks if reshape position in metadata is not farther
2362 * than position in md.
2364 * 0 : not valid sysfs entry
2365 * it can be caused by not started reshape, it should be started
2366 * by reshape array or raid0 array is before takeover
2367 * -1 : error, reshape position is obviously wrong
2368 * 1 : success, reshape progress correct or updated
2370 static int verify_reshape_position(struct mdinfo
*info
, int level
)
2376 /* read sync_max, failure can mean raid0 array */
2377 rv
= sysfs_get_str(info
, NULL
, "sync_max", buf
, 40);
2381 unsigned long long position
= strtoull(buf
, &ep
, 0);
2383 dprintf("Read sync_max sysfs entry is: %s\n", buf
);
2384 if (!(ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))) {
2385 position
*= get_data_disks(level
,
2387 info
->array
.raid_disks
);
2388 if (info
->reshape_progress
< position
) {
2389 dprintf("Corrected reshape progress (%llu) to md position (%llu)\n",
2390 info
->reshape_progress
, position
);
2391 info
->reshape_progress
= position
;
2393 } else if (info
->reshape_progress
> position
) {
2394 pr_err("Fatal error: array reshape was not properly frozen (expected reshape position is %llu, but reshape progress is %llu.\n",
2395 position
, info
->reshape_progress
);
2398 dprintf("Reshape position in md and metadata are the same;");
2402 } else if (rv
== 0) {
2403 /* for valid sysfs entry, 0-length content
2404 * should be indicated as error
2412 static unsigned long long choose_offset(unsigned long long lo
,
2413 unsigned long long hi
,
2414 unsigned long long min
,
2415 unsigned long long max
)
2417 /* Choose a new offset between hi and lo.
2418 * It must be between min and max, but
2419 * we would prefer something near the middle of hi/lo, and also
2420 * prefer to be aligned to a big power of 2.
2422 * So we start with the middle, then for each bit,
2423 * starting at '1' and increasing, if it is set, we either
2424 * add it or subtract it if possible, preferring the option
2425 * which is furthest from the boundary.
2427 * We stop once we get a 1MB alignment. As units are in sectors,
2428 * 1MB = 2*1024 sectors.
2430 unsigned long long choice
= (lo
+ hi
) / 2;
2431 unsigned long long bit
= 1;
2433 for (bit
= 1; bit
< 2*1024; bit
= bit
<< 1) {
2434 unsigned long long bigger
, smaller
;
2435 if (! (bit
& choice
))
2437 bigger
= choice
+ bit
;
2438 smaller
= choice
- bit
;
2439 if (bigger
> max
&& smaller
< min
)
2443 else if (smaller
< min
)
2445 else if (hi
- bigger
> smaller
- lo
)
2453 static int set_new_data_offset(struct mdinfo
*sra
, struct supertype
*st
,
2454 char *devname
, int delta_disks
,
2455 unsigned long long data_offset
,
2456 unsigned long long min
,
2462 unsigned long long before
, after
;
2464 /* Need to find min space before and after so same is used
2467 before
= UINT64_MAX
;
2469 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2473 struct supertype
*st2
;
2474 struct mdinfo info2
;
2476 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2478 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2479 dfd
= dev_open(dn
, O_RDONLY
);
2481 pr_err("%s: cannot open component %s\n",
2482 devname
, dn
? dn
: "-unknown-");
2485 st2
= dup_super(st
);
2486 rv
= st2
->ss
->load_super(st2
,dfd
, NULL
);
2490 pr_err("%s: cannot get superblock from %s\n",
2494 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
2495 st2
->ss
->free_super(st2
);
2497 if (info2
.space_before
== 0 &&
2498 info2
.space_after
== 0) {
2499 /* Metadata doesn't support data_offset changes */
2501 pr_err("%s: Metadata version doesn't support data_offset changes\n",
2505 if (before
> info2
.space_before
)
2506 before
= info2
.space_before
;
2507 if (after
> info2
.space_after
)
2508 after
= info2
.space_after
;
2510 if (data_offset
!= INVALID_SECTORS
) {
2512 if (info2
.data_offset
== data_offset
) {
2513 pr_err("%s: already has that data_offset\n",
2517 if (data_offset
< info2
.data_offset
)
2521 } else if ((data_offset
<= info2
.data_offset
&&
2523 (data_offset
>= info2
.data_offset
&&
2525 pr_err("%s: differing data offsets on devices make this --data-offset setting impossible\n",
2531 if (before
== UINT64_MAX
)
2532 /* impossible really, there must be no devices */
2535 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2536 char *dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2537 unsigned long long new_data_offset
;
2539 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2541 if (delta_disks
< 0) {
2542 /* Don't need any space as array is shrinking
2543 * just move data_offset up by min
2545 if (data_offset
== INVALID_SECTORS
)
2546 new_data_offset
= sd
->data_offset
+ min
;
2548 if (data_offset
< sd
->data_offset
+ min
) {
2549 pr_err("--data-offset too small for %s\n",
2553 new_data_offset
= data_offset
;
2555 } else if (delta_disks
> 0) {
2556 /* need space before */
2560 pr_err("Insufficient head-space for reshape on %s\n",
2564 if (data_offset
== INVALID_SECTORS
)
2565 new_data_offset
= sd
->data_offset
- min
;
2567 if (data_offset
> sd
->data_offset
- min
) {
2568 pr_err("--data-offset too large for %s\n",
2572 new_data_offset
= data_offset
;
2576 /* can move up or down. If 'data_offset'
2577 * was set we would have already decided,
2578 * so just choose direction with most space.
2585 sysfs_set_str(sra
, NULL
, "reshape_direction",
2586 dir
== 1 ? "backwards" : "forwards");
2588 /* Increase data offset */
2592 pr_err("Insufficient tail-space for reshape on %s\n",
2596 if (data_offset
!= INVALID_SECTORS
&&
2597 data_offset
< sd
->data_offset
+ min
) {
2598 pr_err("--data-offset too small on %s\n",
2602 if (data_offset
!= INVALID_SECTORS
)
2603 new_data_offset
= data_offset
;
2605 new_data_offset
= choose_offset(sd
->data_offset
,
2606 sd
->data_offset
+ after
,
2607 sd
->data_offset
+ min
,
2608 sd
->data_offset
+ after
);
2610 /* Decrease data offset */
2614 pr_err("insufficient head-room on %s\n",
2618 if (data_offset
!= INVALID_SECTORS
&&
2619 data_offset
> sd
->data_offset
- min
) {
2620 pr_err("--data-offset too large on %s\n",
2624 if (data_offset
!= INVALID_SECTORS
)
2625 new_data_offset
= data_offset
;
2627 new_data_offset
= choose_offset(sd
->data_offset
- before
,
2629 sd
->data_offset
- before
,
2630 sd
->data_offset
- min
);
2633 err
= sysfs_set_num(sra
, sd
, "new_offset", new_data_offset
);
2634 if (err
< 0 && errno
== E2BIG
) {
2635 /* try again after increasing data size to max */
2636 err
= sysfs_set_num(sra
, sd
, "size", 0);
2637 if (err
< 0 && errno
== EINVAL
&&
2638 !(sd
->disk
.state
& (1<<MD_DISK_SYNC
))) {
2639 /* some kernels have a bug where you cannot
2640 * use '0' on spare devices. */
2641 sysfs_set_num(sra
, sd
, "size",
2642 (sra
->component_size
+ after
)/2);
2644 err
= sysfs_set_num(sra
, sd
, "new_offset",
2648 if (errno
== E2BIG
&& data_offset
!= INVALID_SECTORS
) {
2649 pr_err("data-offset is too big for %s\n", dn
);
2652 if (sd
== sra
->devs
&&
2653 (errno
== ENOENT
|| errno
== E2BIG
))
2654 /* Early kernel, no 'new_offset' file,
2655 * or kernel doesn't like us.
2656 * For RAID5/6 this is not fatal
2659 pr_err("Cannot set new_offset for %s\n", dn
);
2667 /* Just use a backup file */
2671 static int raid10_reshape(char *container
, int fd
, char *devname
,
2672 struct supertype
*st
, struct mdinfo
*info
,
2673 struct reshape
*reshape
,
2674 unsigned long long data_offset
,
2675 int force
, int verbose
)
2677 /* Changing raid_disks, layout, chunksize or possibly
2678 * just data_offset for a RAID10.
2679 * We must always change data_offset. We change by at least
2680 * ->min_offset_change which is the largest of the old and new
2682 * If raid_disks is increasing, then data_offset must decrease
2683 * by at least this copy size.
2684 * If raid_disks is unchanged, data_offset must increase or
2685 * decrease by at least min_offset_change but preferably by much more.
2686 * We choose half of the available space.
2687 * If raid_disks is decreasing, data_offset must increase by
2688 * at least min_offset_change. To allow of this, component_size
2689 * must be decreased by the same amount.
2691 * So we calculate the required minimum and direction, possibly
2692 * reduce the component_size, then iterate through the devices
2693 * and set the new_data_offset.
2694 * If that all works, we set chunk_size, layout, raid_disks, and start
2698 unsigned long long min
;
2701 sra
= sysfs_read(fd
, NULL
,
2702 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
2705 pr_err("%s: Cannot get array details from sysfs\n", devname
);
2708 min
= reshape
->min_offset_change
;
2710 if (info
->delta_disks
)
2711 sysfs_set_str(sra
, NULL
, "reshape_direction",
2712 info
->delta_disks
< 0 ? "backwards" : "forwards");
2713 if (info
->delta_disks
< 0 && info
->space_after
< min
) {
2714 int rv
= sysfs_set_num(sra
, NULL
, "component_size",
2715 (sra
->component_size
- min
)/2);
2717 pr_err("cannot reduce component size\n");
2721 err
= set_new_data_offset(sra
, st
, devname
, info
->delta_disks
,
2722 data_offset
, min
, 0);
2724 pr_err("Cannot set new_data_offset: RAID10 reshape not\n");
2725 cont_err("supported on this kernel\n");
2731 if (!err
&& sysfs_set_num(sra
, NULL
, "chunk_size", info
->new_chunk
) < 0)
2733 if (!err
&& sysfs_set_num(sra
, NULL
, "layout",
2734 reshape
->after
.layout
) < 0)
2737 sysfs_set_num(sra
, NULL
, "raid_disks",
2738 info
->array
.raid_disks
+ info
->delta_disks
) < 0)
2740 if (!err
&& sysfs_set_str(sra
, NULL
, "sync_action", "reshape") < 0)
2743 pr_err("Cannot set array shape for %s\n",
2746 (info
->array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2747 cont_err(" Bitmap must be removed before shape can be changed\n");
2757 static void get_space_after(int fd
, struct supertype
*st
, struct mdinfo
*info
)
2759 struct mdinfo
*sra
, *sd
;
2760 /* Initialisation to silence compiler warning */
2761 unsigned long long min_space_before
= 0, min_space_after
= 0;
2764 sra
= sysfs_read(fd
, NULL
, GET_DEVS
);
2767 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2770 struct supertype
*st2
;
2771 struct mdinfo info2
;
2773 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2775 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2776 dfd
= dev_open(dn
, O_RDONLY
);
2779 st2
= dup_super(st
);
2780 if (st2
->ss
->load_super(st2
,dfd
, NULL
)) {
2786 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
2787 st2
->ss
->free_super(st2
);
2790 min_space_before
> info2
.space_before
)
2791 min_space_before
= info2
.space_before
;
2793 min_space_after
> info2
.space_after
)
2794 min_space_after
= info2
.space_after
;
2797 if (sd
== NULL
&& !first
) {
2798 info
->space_after
= min_space_after
;
2799 info
->space_before
= min_space_before
;
2804 static void update_cache_size(char *container
, struct mdinfo
*sra
,
2805 struct mdinfo
*info
,
2806 int disks
, unsigned long long blocks
)
2808 /* Check that the internal stripe cache is
2809 * large enough, or it won't work.
2810 * It must hold at least 4 stripes of the larger
2813 unsigned long cache
;
2814 cache
= max(info
->array
.chunk_size
, info
->new_chunk
);
2815 cache
*= 4; /* 4 stripes minimum */
2816 cache
/= 512; /* convert to sectors */
2817 /* make sure there is room for 'blocks' with a bit to spare */
2818 if (cache
< 16 + blocks
/ disks
)
2819 cache
= 16 + blocks
/ disks
;
2820 cache
/= (4096/512); /* Convert from sectors to pages */
2822 if (sra
->cache_size
< cache
)
2823 subarray_set_num(container
, sra
, "stripe_cache_size",
2827 static int impose_reshape(struct mdinfo
*sra
,
2828 struct mdinfo
*info
,
2829 struct supertype
*st
,
2832 char *devname
, char *container
,
2833 struct reshape
*reshape
)
2835 struct mdu_array_info_s array
;
2837 sra
->new_chunk
= info
->new_chunk
;
2840 /* for external metadata checkpoint saved by mdmon can be lost
2841 * or missed /due to e.g. crash/. Check if md is not during
2842 * restart farther than metadata points to.
2843 * If so, this means metadata information is obsolete.
2845 if (st
->ss
->external
)
2846 verify_reshape_position(info
, reshape
->level
);
2847 sra
->reshape_progress
= info
->reshape_progress
;
2849 sra
->reshape_progress
= 0;
2850 if (reshape
->after
.data_disks
< reshape
->before
.data_disks
)
2851 /* start from the end of the new array */
2852 sra
->reshape_progress
= (sra
->component_size
2853 * reshape
->after
.data_disks
);
2856 md_get_array_info(fd
, &array
);
2857 if (info
->array
.chunk_size
== info
->new_chunk
&&
2858 reshape
->before
.layout
== reshape
->after
.layout
&&
2859 st
->ss
->external
== 0) {
2860 /* use SET_ARRAY_INFO but only if reshape hasn't started */
2861 array
.raid_disks
= reshape
->after
.data_disks
+ reshape
->parity
;
2862 if (!restart
&& md_set_array_info(fd
, &array
) != 0) {
2865 pr_err("Cannot set device shape for %s: %s\n",
2866 devname
, strerror(errno
));
2869 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2870 cont_err("Bitmap must be removed before shape can be changed\n");
2874 } else if (!restart
) {
2875 /* set them all just in case some old 'new_*' value
2876 * persists from some earlier problem.
2879 if (sysfs_set_num(sra
, NULL
, "chunk_size", info
->new_chunk
) < 0)
2881 if (!err
&& sysfs_set_num(sra
, NULL
, "layout",
2882 reshape
->after
.layout
) < 0)
2884 if (!err
&& subarray_set_num(container
, sra
, "raid_disks",
2885 reshape
->after
.data_disks
+
2886 reshape
->parity
) < 0)
2889 pr_err("Cannot set device shape for %s\n", devname
);
2892 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2893 cont_err("Bitmap must be removed before shape can be changed\n");
2902 static int impose_level(int fd
, int level
, char *devname
, int verbose
)
2905 struct mdu_array_info_s array
;
2908 if (sysfs_init(&info
, fd
, NULL
)) {
2909 pr_err("failed to initialize sysfs.\n");
2913 md_get_array_info(fd
, &array
);
2914 if (level
== 0 && (array
.level
>= 4 && array
.level
<= 6)) {
2915 /* To convert to RAID0 we need to fail and
2916 * remove any non-data devices. */
2919 int data_disks
= array
.raid_disks
- 1;
2920 if (array
.level
== 6)
2922 if (array
.level
== 5 && array
.layout
!= ALGORITHM_PARITY_N
)
2924 if (array
.level
== 6 && array
.layout
!= ALGORITHM_PARITY_N_6
)
2926 sysfs_set_str(&info
, NULL
,"sync_action", "idle");
2927 /* First remove any spares so no recovery starts */
2928 for (d
= 0, found
= 0;
2929 d
< MAX_DISKS
&& found
< array
.nr_disks
; d
++) {
2930 mdu_disk_info_t disk
;
2932 if (md_get_disk_info(fd
, &disk
) < 0)
2934 if (disk
.major
== 0 && disk
.minor
== 0)
2937 if ((disk
.state
& (1 << MD_DISK_ACTIVE
)) &&
2938 disk
.raid_disk
< data_disks
)
2941 ioctl(fd
, HOT_REMOVE_DISK
,
2942 makedev(disk
.major
, disk
.minor
));
2944 /* Now fail anything left */
2945 md_get_array_info(fd
, &array
);
2946 for (d
= 0, found
= 0;
2947 d
< MAX_DISKS
&& found
< array
.nr_disks
; d
++) {
2948 mdu_disk_info_t disk
;
2950 if (md_get_disk_info(fd
, &disk
) < 0)
2952 if (disk
.major
== 0 && disk
.minor
== 0)
2955 if ((disk
.state
& (1 << MD_DISK_ACTIVE
)) &&
2956 disk
.raid_disk
< data_disks
)
2959 ioctl(fd
, SET_DISK_FAULTY
,
2960 makedev(disk
.major
, disk
.minor
));
2961 hot_remove_disk(fd
, makedev(disk
.major
, disk
.minor
), 1);
2964 c
= map_num(pers
, level
);
2966 int err
= sysfs_set_str(&info
, NULL
, "level", c
);
2969 pr_err("%s: could not set level to %s\n",
2972 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2973 cont_err("Bitmap must be removed before level can be changed\n");
2977 pr_err("level of %s changed to %s\n", devname
, c
);
2983 static void catch_term(int sig
)
2988 static int reshape_array(char *container
, int fd
, char *devname
,
2989 struct supertype
*st
, struct mdinfo
*info
,
2990 int force
, struct mddev_dev
*devlist
,
2991 unsigned long long data_offset
,
2992 char *backup_file
, int verbose
, int forked
,
2993 int restart
, int freeze_reshape
)
2995 struct reshape reshape
;
2998 int orig_level
= UnSet
;
3002 struct mdu_array_info_s array
;
3005 struct mddev_dev
*dv
;
3009 unsigned long long *offsets
= NULL
;
3013 unsigned long blocks
;
3014 unsigned long long array_size
;
3016 struct mdinfo
*sra
= NULL
;
3019 /* when reshaping a RAID0, the component_size might be zero.
3020 * So try to fix that up.
3022 if (md_get_array_info(fd
, &array
) != 0) {
3023 dprintf("Cannot get array information.\n");
3026 if (array
.level
== 0 && info
->component_size
== 0) {
3027 get_dev_size(fd
, NULL
, &array_size
);
3028 info
->component_size
= array_size
/ array
.raid_disks
;
3031 if (array
.level
== 10)
3032 /* Need space_after info */
3033 get_space_after(fd
, st
, info
);
3035 if (info
->reshape_active
) {
3036 int new_level
= info
->new_level
;
3037 info
->new_level
= UnSet
;
3038 if (info
->delta_disks
> 0)
3039 info
->array
.raid_disks
-= info
->delta_disks
;
3040 msg
= analyse_change(devname
, info
, &reshape
);
3041 info
->new_level
= new_level
;
3042 if (info
->delta_disks
> 0)
3043 info
->array
.raid_disks
+= info
->delta_disks
;
3045 /* Make sure the array isn't read-only */
3046 ioctl(fd
, RESTART_ARRAY_RW
, 0);
3048 msg
= analyse_change(devname
, info
, &reshape
);
3050 /* if msg == "", error has already been printed */
3052 pr_err("%s\n", msg
);
3055 if (restart
&& (reshape
.level
!= info
->array
.level
||
3056 reshape
.before
.layout
!= info
->array
.layout
||
3057 reshape
.before
.data_disks
+ reshape
.parity
!=
3058 info
->array
.raid_disks
- max(0, info
->delta_disks
))) {
3059 pr_err("reshape info is not in native format - cannot continue.\n");
3063 if (st
->ss
->external
&& restart
&& (info
->reshape_progress
== 0) &&
3064 !((sysfs_get_str(info
, NULL
, "sync_action",
3065 buf
, sizeof(buf
)) > 0) &&
3066 (strncmp(buf
, "reshape", 7) == 0))) {
3067 /* When reshape is restarted from '0', very begin of array
3068 * it is possible that for external metadata reshape and array
3069 * configuration doesn't happen.
3070 * Check if md has the same opinion, and reshape is restarted
3071 * from 0. If so, this is regular reshape start after reshape
3072 * switch in metadata to next array only.
3074 if ((verify_reshape_position(info
, reshape
.level
) >= 0) &&
3075 (info
->reshape_progress
== 0))
3080 * reshape already started. just skip to monitoring
3083 if (reshape
.backup_blocks
== 0)
3085 if (restart
& RESHAPE_NO_BACKUP
)
3088 /* Need 'sra' down at 'started:' */
3089 sra
= sysfs_read(fd
, NULL
,
3090 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
3091 GET_CHUNK
|GET_CACHE
);
3093 pr_err("%s: Cannot get array details from sysfs\n",
3099 backup_file
= locate_backup(sra
->sys_name
);
3103 /* The container is frozen but the array may not be.
3104 * So freeze the array so spares don't get put to the wrong use
3105 * FIXME there should probably be a cleaner separation between
3106 * freeze_array and freeze_container.
3108 sysfs_freeze_array(info
);
3109 /* Check we have enough spares to not be degraded */
3111 for (dv
= devlist
; dv
; dv
=dv
->next
)
3113 spares_needed
= max(reshape
.before
.data_disks
,
3114 reshape
.after
.data_disks
) +
3115 reshape
.parity
- array
.raid_disks
;
3117 if (!force
&& info
->new_level
> 1 && info
->array
.level
> 1 &&
3118 spares_needed
> info
->array
.spare_disks
+ added_disks
) {
3119 pr_err("Need %d spare%s to avoid degraded array, and only have %d.\n"
3120 " Use --force to over-ride this check.\n",
3122 spares_needed
== 1 ? "" : "s",
3123 info
->array
.spare_disks
+ added_disks
);
3126 /* Check we have enough spares to not fail */
3127 spares_needed
= max(reshape
.before
.data_disks
,
3128 reshape
.after
.data_disks
)
3130 if ((info
->new_level
> 1 || info
->new_level
== 0) &&
3131 spares_needed
> info
->array
.spare_disks
+added_disks
) {
3132 pr_err("Need %d spare%s to create working array, and only have %d.\n",
3133 spares_needed
, spares_needed
== 1 ? "" : "s",
3134 info
->array
.spare_disks
+ added_disks
);
3138 if (reshape
.level
!= array
.level
) {
3139 int err
= impose_level(fd
, reshape
.level
, devname
, verbose
);
3142 info
->new_layout
= UnSet
; /* after level change,
3143 * layout is meaningless */
3144 orig_level
= array
.level
;
3145 sysfs_freeze_array(info
);
3147 if (reshape
.level
> 0 && st
->ss
->external
) {
3148 /* make sure mdmon is aware of the new level */
3149 if (mdmon_running(container
))
3150 flush_mdmon(container
);
3152 if (!mdmon_running(container
))
3153 start_mdmon(container
);
3154 ping_monitor(container
);
3155 if (mdmon_running(container
) && st
->update_tail
== NULL
)
3156 st
->update_tail
= &st
->updates
;
3159 /* ->reshape_super might have chosen some spares from the
3160 * container that it wants to be part of the new array.
3161 * We can collect them with ->container_content and give
3162 * them to the kernel.
3164 if (st
->ss
->reshape_super
&& st
->ss
->container_content
) {
3165 char *subarray
= strchr(info
->text_version
+1, '/')+1;
3166 struct mdinfo
*info2
=
3167 st
->ss
->container_content(st
, subarray
);
3171 if (sysfs_init(info2
, fd
, st
->devnm
)) {
3172 pr_err("unable to initialize sysfs for %s\n",
3177 /* When increasing number of devices, we need to set
3178 * new raid_disks before adding these, or they might
3181 if (reshape
.backup_blocks
&&
3182 reshape
.after
.data_disks
>
3183 reshape
.before
.data_disks
)
3184 subarray_set_num(container
, info2
, "raid_disks",
3185 reshape
.after
.data_disks
+
3187 for (d
= info2
->devs
; d
; d
= d
->next
) {
3188 if (d
->disk
.state
== 0 &&
3189 d
->disk
.raid_disk
>= 0) {
3190 /* This is a spare that wants to
3191 * be part of the array.
3193 add_disk(fd
, st
, info2
, d
);
3199 /* We might have been given some devices to add to the
3200 * array. Now that the array has been changed to the right
3201 * level and frozen, we can safely add them.
3204 if (Manage_subdevs(devname
, fd
, devlist
, verbose
, 0, NULL
, 0))
3208 if (reshape
.backup_blocks
== 0 && data_offset
!= INVALID_SECTORS
)
3209 reshape
.backup_blocks
= reshape
.before
.data_disks
* info
->array
.chunk_size
/512;
3210 if (reshape
.backup_blocks
== 0) {
3211 /* No restriping needed, but we might need to impose
3212 * some more changes: layout, raid_disks, chunk_size
3214 /* read current array info */
3215 if (md_get_array_info(fd
, &array
) != 0) {
3216 dprintf("Cannot get array information.\n");
3219 /* compare current array info with new values and if
3220 * it is different update them to new */
3221 if (info
->new_layout
!= UnSet
&&
3222 info
->new_layout
!= array
.layout
) {
3223 array
.layout
= info
->new_layout
;
3224 if (md_set_array_info(fd
, &array
) != 0) {
3225 pr_err("failed to set new layout\n");
3227 } else if (verbose
>= 0)
3228 printf("layout for %s set to %d\n",
3229 devname
, array
.layout
);
3231 if (info
->delta_disks
!= UnSet
&& info
->delta_disks
!= 0 &&
3233 (info
->array
.raid_disks
+ info
->delta_disks
)) {
3234 array
.raid_disks
+= info
->delta_disks
;
3235 if (md_set_array_info(fd
, &array
) != 0) {
3236 pr_err("failed to set raid disks\n");
3238 } else if (verbose
>= 0) {
3239 printf("raid_disks for %s set to %d\n",
3240 devname
, array
.raid_disks
);
3243 if (info
->new_chunk
!= 0 &&
3244 info
->new_chunk
!= array
.chunk_size
) {
3245 if (sysfs_set_num(info
, NULL
,
3246 "chunk_size", info
->new_chunk
) != 0) {
3247 pr_err("failed to set chunk size\n");
3249 } else if (verbose
>= 0)
3250 printf("chunk size for %s set to %d\n",
3251 devname
, info
->new_chunk
);
3258 * There are three possibilities.
3259 * 1/ The array will shrink.
3260 * We need to ensure the reshape will pause before reaching
3261 * the 'critical section'. We also need to fork and wait for
3262 * that to happen. When it does we
3263 * suspend/backup/complete/unfreeze
3265 * 2/ The array will not change size.
3266 * This requires that we keep a backup of a sliding window
3267 * so that we can restore data after a crash. So we need
3268 * to fork and monitor progress.
3269 * In future we will allow the data_offset to change, so
3270 * a sliding backup becomes unnecessary.
3272 * 3/ The array will grow. This is relatively easy.
3273 * However the kernel's restripe routines will cheerfully
3274 * overwrite some early data before it is safe. So we
3275 * need to make a backup of the early parts of the array
3276 * and be ready to restore it if rebuild aborts very early.
3277 * For externally managed metadata, we still need a forked
3278 * child to monitor the reshape and suspend IO over the region
3279 * that is being reshaped.
3281 * We backup data by writing it to one spare, or to a
3282 * file which was given on command line.
3284 * In each case, we first make sure that storage is available
3285 * for the required backup.
3287 * - request the shape change.
3288 * - fork to handle backup etc.
3290 /* Check that we can hold all the data */
3291 get_dev_size(fd
, NULL
, &array_size
);
3292 if (reshape
.new_size
< (array_size
/512)) {
3293 pr_err("this change will reduce the size of the array.\n"
3294 " use --grow --array-size first to truncate array.\n"
3295 " e.g. mdadm --grow %s --array-size %llu\n",
3296 devname
, reshape
.new_size
/2);
3300 if (array
.level
== 10) {
3301 /* Reshaping RAID10 does not require any data backup by
3302 * user-space. Instead it requires that the data_offset
3303 * is changed to avoid the need for backup.
3304 * So this is handled very separately
3307 /* Nothing to do. */
3309 return raid10_reshape(container
, fd
, devname
, st
, info
,
3310 &reshape
, data_offset
, force
, verbose
);
3312 sra
= sysfs_read(fd
, NULL
,
3313 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
|
3316 pr_err("%s: Cannot get array details from sysfs\n",
3322 switch(set_new_data_offset(sra
, st
, devname
,
3323 reshape
.after
.data_disks
- reshape
.before
.data_disks
,
3325 reshape
.min_offset_change
, 1)) {
3329 /* Updated data_offset, so it's easy now */
3330 update_cache_size(container
, sra
, info
,
3331 min(reshape
.before
.data_disks
,
3332 reshape
.after
.data_disks
),
3333 reshape
.backup_blocks
);
3335 /* Right, everything seems fine. Let's kick things off.
3339 if (impose_reshape(sra
, info
, st
, fd
, restart
,
3340 devname
, container
, &reshape
) < 0)
3342 if (sysfs_set_str(sra
, NULL
, "sync_action", "reshape") < 0) {
3344 if (errno
!= EINVAL
) {
3345 pr_err("Failed to initiate reshape!\n");
3348 /* revert data_offset and try the old way */
3349 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
3350 sysfs_set_num(sra
, sd
, "new_offset",
3352 sysfs_set_str(sra
, NULL
, "reshape_direction",
3357 if (info
->new_level
== reshape
.level
)
3359 /* need to adjust level when reshape completes */
3361 case -1: /* ignore error, but don't wait */
3363 default: /* parent */
3372 fd
= open_dev(sra
->sys_name
);
3374 impose_level(fd
, info
->new_level
, devname
, verbose
);
3376 case 1: /* Couldn't set data_offset, try the old way */
3377 if (data_offset
!= INVALID_SECTORS
) {
3378 pr_err("Cannot update data_offset on this array\n");
3385 /* Decide how many blocks (sectors) for a reshape
3386 * unit. The number we have so far is just a minimum
3388 blocks
= reshape
.backup_blocks
;
3389 if (reshape
.before
.data_disks
==
3390 reshape
.after
.data_disks
) {
3391 /* Make 'blocks' bigger for better throughput, but
3392 * not so big that we reject it below.
3393 * Try for 16 megabytes
3395 while (blocks
* 32 < sra
->component_size
&& blocks
< 16*1024*2)
3398 pr_err("Need to backup %luK of critical section..\n", blocks
/2);
3400 if (blocks
>= sra
->component_size
/2) {
3401 pr_err("%s: Something wrong - reshape aborted\n", devname
);
3405 /* Now we need to open all these devices so we can read/write.
3407 nrdisks
= max(reshape
.before
.data_disks
,
3408 reshape
.after
.data_disks
) + reshape
.parity
3409 + sra
->array
.spare_disks
;
3410 fdlist
= xcalloc((1+nrdisks
), sizeof(int));
3411 offsets
= xcalloc((1+nrdisks
), sizeof(offsets
[0]));
3413 odisks
= reshape
.before
.data_disks
+ reshape
.parity
;
3414 d
= reshape_prepare_fdlist(devname
, sra
, odisks
, nrdisks
, blocks
,
3415 backup_file
, fdlist
, offsets
);
3419 if ((st
->ss
->manage_reshape
== NULL
) ||
3420 (st
->ss
->recover_backup
== NULL
)) {
3421 if (backup_file
== NULL
) {
3422 if (reshape
.after
.data_disks
<=
3423 reshape
.before
.data_disks
) {
3424 pr_err("%s: Cannot grow - need backup-file\n",
3426 pr_err(" Please provide one with \"--backup=...\"\n");
3428 } else if (d
== odisks
) {
3429 pr_err("%s: Cannot grow - need a spare or backup-file to backup critical section\n", devname
);
3433 if (!reshape_open_backup_file(backup_file
, fd
, devname
,
3435 fdlist
+d
, offsets
+d
,
3436 sra
->sys_name
, restart
)) {
3443 update_cache_size(container
, sra
, info
,
3444 min(reshape
.before
.data_disks
,
3445 reshape
.after
.data_disks
), blocks
);
3447 /* Right, everything seems fine. Let's kick things off.
3448 * If only changing raid_disks, use ioctl, else use
3453 if (impose_reshape(sra
, info
, st
, fd
, restart
,
3454 devname
, container
, &reshape
) < 0)
3457 err
= start_reshape(sra
, restart
, reshape
.before
.data_disks
,
3458 reshape
.after
.data_disks
);
3460 pr_err("Cannot %s reshape for %s\n",
3461 restart
? "continue" : "start", devname
);
3465 sysfs_set_str(sra
, NULL
, "array_state", "active");
3466 if (freeze_reshape
) {
3470 pr_err("Reshape has to be continued from location %llu when root filesystem has been mounted.\n",
3471 sra
->reshape_progress
);
3476 if (continue_via_systemd(container
?: sra
->sys_name
,
3485 /* Now we just need to kick off the reshape and watch, while
3486 * handling backups of the data...
3487 * This is all done by a forked background process.
3489 switch(forked
? 0 : fork()) {
3491 pr_err("Cannot run child to monitor reshape: %s\n",
3505 /* If another array on the same devices is busy, the
3506 * reshape will wait for them. This would mean that
3507 * the first section that we suspend will stay suspended
3508 * for a long time. So check on that possibility
3509 * by looking for "DELAYED" in /proc/mdstat, and if found,
3513 struct mdstat_ent
*mds
, *m
;
3515 mds
= mdstat_read(1, 0);
3516 for (m
= mds
; m
; m
= m
->next
)
3517 if (strcmp(m
->devnm
, sra
->sys_name
) == 0) {
3518 if (m
->resync
&& m
->percent
== RESYNC_DELAYED
)
3521 /* Haven't started the reshape thread
3528 if (delayed
== 1 && get_linux_version() < 3007000) {
3529 pr_err("Reshape is delayed, but cannot wait carefully with this kernel.\n"
3530 " You might experience problems until other reshapes complete.\n");
3534 mdstat_wait(30 - (delayed
-1) * 25);
3537 if (check_env("MDADM_GROW_VERIFY"))
3538 fd
= open(devname
, O_RDONLY
| O_DIRECT
);
3541 mlockall(MCL_FUTURE
);
3543 signal(SIGTERM
, catch_term
);
3545 if (st
->ss
->external
) {
3546 /* metadata handler takes it from here */
3547 done
= st
->ss
->manage_reshape(
3548 fd
, sra
, &reshape
, st
, blocks
,
3549 fdlist
, offsets
, d
- odisks
, fdlist
+ odisks
,
3552 done
= child_monitor(
3553 fd
, sra
, &reshape
, st
, blocks
, fdlist
, offsets
,
3554 d
- odisks
, fdlist
+ odisks
, offsets
+ odisks
);
3559 if (backup_file
&& done
) {
3561 bul
= make_backup(sra
->sys_name
);
3564 int l
= readlink(bul
, buf
, sizeof(buf
) - 1);
3572 unlink(backup_file
);
3579 if (!st
->ss
->external
&&
3580 !(reshape
.before
.data_disks
!= reshape
.after
.data_disks
&&
3581 info
->custom_array_size
) && info
->new_level
== reshape
.level
&&
3583 /* no need to wait for the reshape to finish as
3584 * there is nothing more to do.
3591 if (st
->ss
->external
) {
3592 /* Re-load the metadata as much could have changed */
3593 int cfd
= open_dev(st
->container_devnm
);
3595 flush_mdmon(container
);
3596 st
->ss
->free_super(st
);
3597 st
->ss
->load_container(st
, cfd
, container
);
3602 /* set new array size if required customer_array_size is used
3605 if (reshape
.before
.data_disks
!= reshape
.after
.data_disks
&&
3606 info
->custom_array_size
)
3607 set_array_size(st
, info
, info
->text_version
);
3609 if (info
->new_level
!= reshape
.level
) {
3611 fd
= open(devname
, O_RDONLY
);
3612 impose_level(fd
, info
->new_level
, devname
, verbose
);
3614 if (info
->new_level
== 0)
3615 st
->update_tail
= NULL
;
3627 if (orig_level
!= UnSet
&& sra
) {
3628 c
= map_num(pers
, orig_level
);
3629 if (c
&& sysfs_set_str(sra
, NULL
, "level", c
) == 0)
3630 pr_err("aborting level change\n");
3638 /* mdfd handle is passed to be closed in child process (after fork).
3640 int reshape_container(char *container
, char *devname
,
3642 struct supertype
*st
,
3643 struct mdinfo
*info
,
3645 char *backup_file
, int verbose
,
3646 int forked
, int restart
, int freeze_reshape
)
3648 struct mdinfo
*cc
= NULL
;
3650 char last_devnm
[32] = "";
3652 /* component_size is not meaningful for a container,
3653 * so pass '0' meaning 'no change'
3656 reshape_super(st
, 0, info
->new_level
,
3657 info
->new_layout
, info
->new_chunk
,
3658 info
->array
.raid_disks
, info
->delta_disks
,
3659 backup_file
, devname
, APPLY_METADATA_CHANGES
,
3667 /* ping monitor to be sure that update is on disk
3669 ping_monitor(container
);
3671 if (!forked
&& !freeze_reshape
)
3672 if (continue_via_systemd(container
, GROW_SERVICE
))
3675 switch (forked
? 0 : fork()) {
3676 case -1: /* error */
3677 perror("Cannot fork to complete reshape\n");
3680 default: /* parent */
3681 if (!freeze_reshape
)
3682 printf("%s: multi-array reshape continues in background\n", Name
);
3690 /* close unused handle in child process
3696 /* For each member array with reshape_active,
3697 * we need to perform the reshape.
3698 * We pick the first array that needs reshaping and
3699 * reshape it. reshape_array() will re-read the metadata
3700 * so the next time through a different array should be
3701 * ready for reshape.
3702 * It is possible that the 'different' array will not
3703 * be assembled yet. In that case we simple exit.
3704 * When it is assembled, the mdadm which assembles it
3705 * will take over the reshape.
3707 struct mdinfo
*content
;
3709 struct mdstat_ent
*mdstat
;
3715 cc
= st
->ss
->container_content(st
, NULL
);
3717 for (content
= cc
; content
; content
= content
->next
) {
3719 if (!content
->reshape_active
)
3722 subarray
= strchr(content
->text_version
+1, '/')+1;
3723 mdstat
= mdstat_by_subdev(subarray
, container
);
3726 if (mdstat
->active
== 0) {
3727 pr_err("Skipping inactive array %s.\n",
3729 free_mdstat(mdstat
);
3738 devid
= devnm2devid(mdstat
->devnm
);
3739 adev
= map_dev(major(devid
), minor(devid
), 0);
3741 adev
= content
->text_version
;
3743 fd
= open_dev(mdstat
->devnm
);
3745 pr_err("Device %s cannot be opened for reshape.\n",
3750 if (strcmp(last_devnm
, mdstat
->devnm
) == 0) {
3751 /* Do not allow for multiple reshape_array() calls for
3753 * It can happen when reshape_array() returns without
3754 * error, when reshape is not finished (wrong reshape
3755 * starting/continuation conditions). Mdmon doesn't
3756 * switch to next array in container and reentry
3757 * conditions for the same array occur.
3758 * This is possibly interim until the behaviour of
3759 * reshape_array is resolved().
3761 printf("%s: Multiple reshape execution detected for device %s.\n", Name
, adev
);
3765 strcpy(last_devnm
, mdstat
->devnm
);
3767 if (sysfs_init(content
, fd
, mdstat
->devnm
)) {
3768 pr_err("Unable to initialize sysfs for %s\n",
3774 if (mdmon_running(container
))
3775 flush_mdmon(container
);
3777 rv
= reshape_array(container
, fd
, adev
, st
,
3778 content
, force
, NULL
, INVALID_SECTORS
,
3779 backup_file
, verbose
, 1, restart
,
3783 if (freeze_reshape
) {
3792 if (mdmon_running(container
))
3793 flush_mdmon(container
);
3802 * We run a child process in the background which performs the following
3804 * - wait for resync to reach a certain point
3805 * - suspend io to the following section
3806 * - backup that section
3807 * - allow resync to proceed further
3809 * - discard the backup.
3811 * When are combined in slightly different ways in the three cases.
3813 * - suspend/backup/allow/wait/resume/discard
3815 * - allow/wait/suspend/backup/allow/wait/resume/discard
3817 * - wait/resume/discard/suspend/backup/allow
3819 * suspend/backup/allow always come together
3820 * wait/resume/discard do too.
3821 * For the same-size case we have two backups to improve flow.
3825 int progress_reshape(struct mdinfo
*info
, struct reshape
*reshape
,
3826 unsigned long long backup_point
,
3827 unsigned long long wait_point
,
3828 unsigned long long *suspend_point
,
3829 unsigned long long *reshape_completed
, int *frozen
)
3831 /* This function is called repeatedly by the reshape manager.
3832 * It determines how much progress can safely be made and allows
3834 * - 'info' identifies the array and particularly records in
3835 * ->reshape_progress the metadata's knowledge of progress
3836 * This is a sector offset from the start of the array
3837 * of the next array block to be relocated. This number
3838 * may increase from 0 or decrease from array_size, depending
3839 * on the type of reshape that is happening.
3840 * Note that in contrast, 'sync_completed' is a block count of the
3841 * reshape so far. It gives the distance between the start point
3842 * (head or tail of device) and the next place that data will be
3843 * written. It always increases.
3844 * - 'reshape' is the structure created by analyse_change
3845 * - 'backup_point' shows how much the metadata manager has backed-up
3846 * data. For reshapes with increasing progress, it is the next address
3847 * to be backed up, previous addresses have been backed-up. For
3848 * decreasing progress, it is the earliest address that has been
3849 * backed up - later address are also backed up.
3850 * So addresses between reshape_progress and backup_point are
3851 * backed up providing those are in the 'correct' order.
3852 * - 'wait_point' is an array address. When reshape_completed
3853 * passes this point, progress_reshape should return. It might
3854 * return earlier if it determines that ->reshape_progress needs
3855 * to be updated or further backup is needed.
3856 * - suspend_point is maintained by progress_reshape and the caller
3857 * should not touch it except to initialise to zero.
3858 * It is an array address and it only increases in 2.6.37 and earlier.
3859 * This makes it difficult to handle reducing reshapes with
3860 * external metadata.
3861 * However: it is similar to backup_point in that it records the
3862 * other end of a suspended region from reshape_progress.
3863 * it is moved to extend the region that is safe to backup and/or
3865 * - reshape_completed is read from sysfs and returned. The caller
3866 * should copy this into ->reshape_progress when it has reason to
3867 * believe that the metadata knows this, and any backup outside this
3871 * 1 if more data from backup_point - but only as far as suspend_point,
3872 * should be backed up
3873 * 0 if things are progressing smoothly
3874 * -1 if the reshape is finished because it is all done,
3875 * -2 if the reshape is finished due to an error.
3878 int advancing
= (reshape
->after
.data_disks
3879 >= reshape
->before
.data_disks
);
3880 unsigned long long need_backup
; /* All data between start of array and
3881 * here will at some point need to
3884 unsigned long long read_offset
, write_offset
;
3885 unsigned long long write_range
;
3886 unsigned long long max_progress
, target
, completed
;
3887 unsigned long long array_size
= (info
->component_size
3888 * reshape
->before
.data_disks
);
3892 /* First, we unsuspend any region that is now known to be safe.
3893 * If suspend_point is on the 'wrong' side of reshape_progress, then
3894 * we don't have or need suspension at the moment. This is true for
3895 * native metadata when we don't need to back-up.
3898 if (info
->reshape_progress
<= *suspend_point
)
3899 sysfs_set_num(info
, NULL
, "suspend_lo",
3900 info
->reshape_progress
);
3902 /* Note: this won't work in 2.6.37 and before.
3903 * Something somewhere should make sure we don't need it!
3905 if (info
->reshape_progress
>= *suspend_point
)
3906 sysfs_set_num(info
, NULL
, "suspend_hi",
3907 info
->reshape_progress
);
3910 /* Now work out how far it is safe to progress.
3911 * If the read_offset for ->reshape_progress is less than
3912 * 'blocks' beyond the write_offset, we can only progress as far
3914 * Otherwise we can progress until the write_offset for the new location
3915 * reaches (within 'blocks' of) the read_offset at the current location.
3916 * However that region must be suspended unless we are using native
3918 * If we need to suspend more, we limit it to 128M per device, which is
3919 * rather arbitrary and should be some time-based calculation.
3921 read_offset
= info
->reshape_progress
/ reshape
->before
.data_disks
;
3922 write_offset
= info
->reshape_progress
/ reshape
->after
.data_disks
;
3923 write_range
= info
->new_chunk
/512;
3924 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
3925 need_backup
= array_size
;
3927 need_backup
= reshape
->backup_blocks
;
3929 if (read_offset
< write_offset
+ write_range
)
3930 max_progress
= backup_point
;
3933 read_offset
* reshape
->after
.data_disks
;
3935 if (read_offset
> write_offset
- write_range
)
3936 /* Can only progress as far as has been backed up,
3937 * which must be suspended */
3938 max_progress
= backup_point
;
3939 else if (info
->reshape_progress
<= need_backup
)
3940 max_progress
= backup_point
;
3942 if (info
->array
.major_version
>= 0)
3943 /* Can progress until backup is needed */
3944 max_progress
= need_backup
;
3946 /* Can progress until metadata update is required */
3948 read_offset
* reshape
->after
.data_disks
;
3949 /* but data must be suspended */
3950 if (max_progress
< *suspend_point
)
3951 max_progress
= *suspend_point
;
3956 /* We know it is safe to progress to 'max_progress' providing
3957 * it is suspended or we are using native metadata.
3958 * Consider extending suspend_point 128M per device if it
3959 * is less than 64M per device beyond reshape_progress.
3960 * But always do a multiple of 'blocks'
3961 * FIXME this is too big - it takes to long to complete
3964 target
= 64*1024*2 * min(reshape
->before
.data_disks
,
3965 reshape
->after
.data_disks
);
3966 target
/= reshape
->backup_blocks
;
3969 target
*= reshape
->backup_blocks
;
3971 /* For externally managed metadata we always need to suspend IO to
3972 * the area being reshaped so we regularly push suspend_point forward.
3973 * For native metadata we only need the suspend if we are going to do
3977 if ((need_backup
> info
->reshape_progress
||
3978 info
->array
.major_version
< 0) &&
3979 *suspend_point
< info
->reshape_progress
+ target
) {
3980 if (need_backup
< *suspend_point
+ 2 * target
)
3981 *suspend_point
= need_backup
;
3982 else if (*suspend_point
+ 2 * target
< array_size
)
3983 *suspend_point
+= 2 * target
;
3985 *suspend_point
= array_size
;
3986 sysfs_set_num(info
, NULL
, "suspend_hi", *suspend_point
);
3987 if (max_progress
> *suspend_point
)
3988 max_progress
= *suspend_point
;
3991 if (info
->array
.major_version
>= 0) {
3992 /* Only need to suspend when about to backup */
3993 if (info
->reshape_progress
< need_backup
* 2 &&
3994 *suspend_point
> 0) {
3996 sysfs_set_num(info
, NULL
, "suspend_lo", 0);
3997 sysfs_set_num(info
, NULL
, "suspend_hi",
4001 /* Need to suspend continually */
4002 if (info
->reshape_progress
< *suspend_point
)
4003 *suspend_point
= info
->reshape_progress
;
4004 if (*suspend_point
+ target
< info
->reshape_progress
)
4005 /* No need to move suspend region yet */;
4007 if (*suspend_point
>= 2 * target
)
4008 *suspend_point
-= 2 * target
;
4011 sysfs_set_num(info
, NULL
, "suspend_lo",
4014 if (max_progress
< *suspend_point
)
4015 max_progress
= *suspend_point
;
4019 /* now set sync_max to allow that progress. sync_max, like
4020 * sync_completed is a count of sectors written per device, so
4021 * we find the difference between max_progress and the start point,
4022 * and divide that by after.data_disks to get a sync_max
4024 * At the same time we convert wait_point to a similar number
4025 * for comparing against sync_completed.
4027 /* scale down max_progress to per_disk */
4028 max_progress
/= reshape
->after
.data_disks
;
4030 * Round to chunk size as some kernels give an erroneously
4033 max_progress
/= info
->new_chunk
/512;
4034 max_progress
*= info
->new_chunk
/512;
4035 /* And round to old chunk size as the kernel wants that */
4036 max_progress
/= info
->array
.chunk_size
/512;
4037 max_progress
*= info
->array
.chunk_size
/512;
4038 /* Limit progress to the whole device */
4039 if (max_progress
> info
->component_size
)
4040 max_progress
= info
->component_size
;
4041 wait_point
/= reshape
->after
.data_disks
;
4043 /* switch from 'device offset' to 'processed block count' */
4044 max_progress
= info
->component_size
- max_progress
;
4045 wait_point
= info
->component_size
- wait_point
;
4049 sysfs_set_num(info
, NULL
, "sync_max", max_progress
);
4051 /* Now wait. If we have already reached the point that we were
4052 * asked to wait to, don't wait at all, else wait for any change.
4053 * We need to select on 'sync_completed' as that is the place that
4054 * notifications happen, but we are really interested in
4055 * 'reshape_position'
4057 fd
= sysfs_get_fd(info
, NULL
, "sync_completed");
4059 goto check_progress
;
4061 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
4062 goto check_progress
;
4064 while (completed
< max_progress
&& completed
< wait_point
) {
4065 /* Check that sync_action is still 'reshape' to avoid
4066 * waiting forever on a dead array
4069 if (sysfs_get_str(info
, NULL
, "sync_action", action
, 20) <= 0 ||
4070 strncmp(action
, "reshape", 7) != 0)
4072 /* Some kernels reset 'sync_completed' to zero
4073 * before setting 'sync_action' to 'idle'.
4074 * So we need these extra tests.
4076 if (completed
== 0 && advancing
&&
4077 strncmp(action
, "idle", 4) == 0 &&
4078 info
->reshape_progress
> 0)
4080 if (completed
== 0 && !advancing
&&
4081 strncmp(action
, "idle", 4) == 0 &&
4082 info
->reshape_progress
<
4083 (info
->component_size
* reshape
->after
.data_disks
))
4085 sysfs_wait(fd
, NULL
);
4086 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
4087 goto check_progress
;
4089 /* Some kernels reset 'sync_completed' to zero,
4090 * we need to have real point we are in md.
4091 * So in that case, read 'reshape_position' from sysfs.
4093 if (completed
== 0) {
4094 unsigned long long reshapep
;
4096 if (sysfs_get_str(info
, NULL
, "sync_action", action
, 20) > 0 &&
4097 strncmp(action
, "idle", 4) == 0 &&
4098 sysfs_get_ll(info
, NULL
,
4099 "reshape_position", &reshapep
) == 0)
4100 *reshape_completed
= reshapep
;
4102 /* some kernels can give an incorrectly high
4103 * 'completed' number, so round down */
4104 completed
/= (info
->new_chunk
/512);
4105 completed
*= (info
->new_chunk
/512);
4106 /* Convert 'completed' back in to a 'progress' number */
4107 completed
*= reshape
->after
.data_disks
;
4109 completed
= (info
->component_size
4110 * reshape
->after
.data_disks
4112 *reshape_completed
= completed
;
4117 /* We return the need_backup flag. Caller will decide
4118 * how much - a multiple of ->backup_blocks up to *suspend_point
4121 return need_backup
> info
->reshape_progress
;
4123 return need_backup
>= info
->reshape_progress
;
4126 /* if we couldn't read a number from sync_completed, then
4127 * either the reshape did complete, or it aborted.
4128 * We can tell which by checking for 'none' in reshape_position.
4129 * If it did abort, then it might immediately restart if it
4130 * it was just a device failure that leaves us degraded but
4133 if (sysfs_get_str(info
, NULL
, "reshape_position", buf
,
4134 sizeof(buf
)) < 0 || strncmp(buf
, "none", 4) != 0) {
4135 /* The abort might only be temporary. Wait up to 10
4136 * seconds for fd to contain a valid number again.
4140 unsigned long long new_sync_max
;
4141 while (fd
>= 0 && rv
< 0 && wait
> 0) {
4142 if (sysfs_wait(fd
, &wait
) != 1)
4144 switch (sysfs_fd_get_ll(fd
, &completed
)) {
4146 /* all good again */
4148 /* If "sync_max" is no longer max_progress
4149 * we need to freeze things
4151 sysfs_get_ll(info
, NULL
, "sync_max",
4153 *frozen
= (new_sync_max
!= max_progress
);
4155 case -2: /* read error - abort */
4162 return rv
; /* abort */
4164 /* Maybe racing with array shutdown - check state */
4167 if (sysfs_get_str(info
, NULL
, "array_state", buf
,
4169 strncmp(buf
, "inactive", 8) == 0 ||
4170 strncmp(buf
, "clear",5) == 0)
4171 return -2; /* abort */
4172 return -1; /* complete */
4176 /* FIXME return status is never checked */
4177 static int grow_backup(struct mdinfo
*sra
,
4178 unsigned long long offset
, /* per device */
4179 unsigned long stripes
, /* per device, in old chunks */
4180 int *sources
, unsigned long long *offsets
,
4181 int disks
, int chunk
, int level
, int layout
,
4182 int dests
, int *destfd
, unsigned long long *destoffsets
,
4183 int part
, int *degraded
,
4186 /* Backup 'blocks' sectors at 'offset' on each device of the array,
4187 * to storage 'destfd' (offset 'destoffsets'), after first
4188 * suspending IO. Then allow resync to continue
4189 * over the suspended section.
4190 * Use part 'part' of the backup-super-block.
4195 unsigned long long ll
;
4197 //printf("offset %llu\n", offset);
4203 /* Check that array hasn't become degraded, else we might backup the wrong data */
4204 if (sysfs_get_ll(sra
, NULL
, "degraded", &ll
) < 0)
4205 return -1; /* FIXME this error is ignored */
4206 new_degraded
= (int)ll
;
4207 if (new_degraded
!= *degraded
) {
4208 /* check each device to ensure it is still working */
4210 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
4211 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
4213 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
4216 if (sysfs_get_str(sra
, sd
, "state",
4217 sbuf
, sizeof(sbuf
)) < 0 ||
4218 strstr(sbuf
, "faulty") ||
4219 strstr(sbuf
, "in_sync") == NULL
) {
4220 /* this device is dead */
4221 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
4222 if (sd
->disk
.raid_disk
>= 0 &&
4223 sources
[sd
->disk
.raid_disk
] >= 0) {
4224 close(sources
[sd
->disk
.raid_disk
]);
4225 sources
[sd
->disk
.raid_disk
] = -1;
4230 *degraded
= new_degraded
;
4233 bsb
.arraystart2
= __cpu_to_le64(offset
* odata
);
4234 bsb
.length2
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
4236 bsb
.arraystart
= __cpu_to_le64(offset
* odata
);
4237 bsb
.length
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
4240 bsb
.magic
[15] = '2';
4241 for (i
= 0; i
< dests
; i
++)
4243 lseek64(destfd
[i
], destoffsets
[i
] +
4244 __le64_to_cpu(bsb
.devstart2
)*512, 0);
4246 lseek64(destfd
[i
], destoffsets
[i
], 0);
4248 rv
= save_stripes(sources
, offsets
, disks
, chunk
, level
, layout
,
4249 dests
, destfd
, offset
* 512 * odata
,
4250 stripes
* chunk
* odata
, buf
);
4254 bsb
.mtime
= __cpu_to_le64(time(0));
4255 for (i
= 0; i
< dests
; i
++) {
4256 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
4258 bsb
.sb_csum
= bsb_csum((char*)&bsb
,
4259 ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
4260 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
4261 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
4262 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
4265 if ((unsigned long long)lseek64(destfd
[i
],
4266 destoffsets
[i
] - 4096, 0) !=
4267 destoffsets
[i
] - 4096)
4269 if (write(destfd
[i
], &bsb
, 512) != 512)
4271 if (destoffsets
[i
] > 4096) {
4272 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]+stripes
*chunk
*odata
, 0) !=
4273 destoffsets
[i
]+stripes
*chunk
*odata
)
4275 if (write(destfd
[i
], &bsb
, 512) != 512)
4285 /* in 2.6.30, the value reported by sync_completed can be
4286 * less that it should be by one stripe.
4287 * This only happens when reshape hits sync_max and pauses.
4288 * So allow wait_backup to either extent sync_max further
4289 * than strictly necessary, or return before the
4290 * sync has got quite as far as we would really like.
4291 * This is what 'blocks2' is for.
4292 * The various caller give appropriate values so that
4295 /* FIXME return value is often ignored */
4296 static int forget_backup(int dests
, int *destfd
,
4297 unsigned long long *destoffsets
,
4301 * Erase backup 'part' (which is 0 or 1)
4307 bsb
.arraystart2
= __cpu_to_le64(0);
4308 bsb
.length2
= __cpu_to_le64(0);
4310 bsb
.arraystart
= __cpu_to_le64(0);
4311 bsb
.length
= __cpu_to_le64(0);
4313 bsb
.mtime
= __cpu_to_le64(time(0));
4315 for (i
= 0; i
< dests
; i
++) {
4316 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
4317 bsb
.sb_csum
= bsb_csum((char*)&bsb
,
4318 ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
4319 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
4320 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
4321 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
4322 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]-4096, 0) !=
4323 destoffsets
[i
]-4096)
4325 if (rv
== 0 && write(destfd
[i
], &bsb
, 512) != 512)
4332 static void fail(char *msg
)
4335 rv
= (write(2, msg
, strlen(msg
)) != (int)strlen(msg
));
4336 rv
|= (write(2, "\n", 1) != 1);
4340 static char *abuf
, *bbuf
;
4341 static unsigned long long abuflen
;
4342 static void validate(int afd
, int bfd
, unsigned long long offset
)
4344 /* check that the data in the backup against the array.
4345 * This is only used for regression testing and should not
4346 * be used while the array is active
4350 lseek64(bfd
, offset
- 4096, 0);
4351 if (read(bfd
, &bsb2
, 512) != 512)
4352 fail("cannot read bsb");
4353 if (bsb2
.sb_csum
!= bsb_csum((char*)&bsb2
,
4354 ((char*)&bsb2
.sb_csum
)-((char*)&bsb2
)))
4355 fail("first csum bad");
4356 if (memcmp(bsb2
.magic
, "md_backup_data", 14) != 0)
4357 fail("magic is bad");
4358 if (memcmp(bsb2
.magic
, "md_backup_data-2", 16) == 0 &&
4359 bsb2
.sb_csum2
!= bsb_csum((char*)&bsb2
,
4360 ((char*)&bsb2
.sb_csum2
)-((char*)&bsb2
)))
4361 fail("second csum bad");
4363 if (__le64_to_cpu(bsb2
.devstart
)*512 != offset
)
4364 fail("devstart is wrong");
4367 unsigned long long len
= __le64_to_cpu(bsb2
.length
)*512;
4369 if (abuflen
< len
) {
4373 if (posix_memalign((void**)&abuf
, 4096, abuflen
) ||
4374 posix_memalign((void**)&bbuf
, 4096, abuflen
)) {
4376 /* just stop validating on mem-alloc failure */
4381 lseek64(bfd
, offset
, 0);
4382 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
) {
4383 //printf("len %llu\n", len);
4384 fail("read first backup failed");
4386 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart
)*512, 0);
4387 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
4388 fail("read first from array failed");
4389 if (memcmp(bbuf
, abuf
, len
) != 0) {
4392 printf("offset=%llu len=%llu\n",
4393 (unsigned long long)__le64_to_cpu(bsb2
.arraystart
)*512, len
);
4394 for (i
=0; i
<len
; i
++)
4395 if (bbuf
[i
] != abuf
[i
]) {
4396 printf("first diff byte %d\n", i
);
4400 fail("data1 compare failed");
4404 unsigned long long len
= __le64_to_cpu(bsb2
.length2
)*512;
4406 if (abuflen
< len
) {
4410 abuf
= xmalloc(abuflen
);
4411 bbuf
= xmalloc(abuflen
);
4414 lseek64(bfd
, offset
+__le64_to_cpu(bsb2
.devstart2
)*512, 0);
4415 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
)
4416 fail("read second backup failed");
4417 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart2
)*512, 0);
4418 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
4419 fail("read second from array failed");
4420 if (memcmp(bbuf
, abuf
, len
) != 0)
4421 fail("data2 compare failed");
4425 int child_monitor(int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
4426 struct supertype
*st
, unsigned long blocks
,
4427 int *fds
, unsigned long long *offsets
,
4428 int dests
, int *destfd
, unsigned long long *destoffsets
)
4430 /* Monitor a reshape where backup is being performed using
4431 * 'native' mechanism - either to a backup file, or
4432 * to some space in a spare.
4436 unsigned long long speed
;
4437 unsigned long long suspend_point
, array_size
;
4438 unsigned long long backup_point
, wait_point
;
4439 unsigned long long reshape_completed
;
4441 int increasing
= reshape
->after
.data_disks
>=
4442 reshape
->before
.data_disks
;
4443 int part
= 0; /* The next part of the backup area to fill. It
4444 * may already be full, so we need to check */
4445 int level
= reshape
->level
;
4446 int layout
= reshape
->before
.layout
;
4447 int data
= reshape
->before
.data_disks
;
4448 int disks
= reshape
->before
.data_disks
+ reshape
->parity
;
4449 int chunk
= sra
->array
.chunk_size
;
4451 unsigned long stripes
;
4455 /* set up the backup-super-block. This requires the
4456 * uuid from the array.
4458 /* Find a superblock */
4459 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
4463 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
4465 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
4466 devfd
= dev_open(dn
, O_RDONLY
);
4469 ok
= st
->ss
->load_super(st
, devfd
, NULL
);
4475 pr_err("Cannot find a superblock\n");
4479 memset(&bsb
, 0, 512);
4480 memcpy(bsb
.magic
, "md_backup_data-1", 16);
4481 st
->ss
->uuid_from_super(st
, uuid
);
4482 memcpy(bsb
.set_uuid
, uuid
, 16);
4483 bsb
.mtime
= __cpu_to_le64(time(0));
4484 bsb
.devstart2
= blocks
;
4486 stripes
= blocks
/ (sra
->array
.chunk_size
/512) /
4487 reshape
->before
.data_disks
;
4489 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
4490 /* Don't start the 'reshape' */
4492 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
) {
4493 sysfs_get_ll(sra
, NULL
, "sync_speed_min", &speed
);
4494 sysfs_set_num(sra
, NULL
, "sync_speed_min", 200000);
4498 array_size
= sra
->component_size
* reshape
->after
.data_disks
;
4499 backup_point
= sra
->reshape_progress
;
4502 array_size
= sra
->component_size
* reshape
->before
.data_disks
;
4503 backup_point
= reshape
->backup_blocks
;
4504 suspend_point
= array_size
;
4510 /* Want to return as soon the oldest backup slot can
4511 * be released as that allows us to start backing up
4512 * some more, providing suspend_point has been
4513 * advanced, which it should have.
4516 wait_point
= array_size
;
4517 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
4518 wait_point
= (__le64_to_cpu(bsb
.arraystart
) +
4519 __le64_to_cpu(bsb
.length
));
4520 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
4521 wait_point
= (__le64_to_cpu(bsb
.arraystart2
) +
4522 __le64_to_cpu(bsb
.length2
));
4525 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
4526 wait_point
= __le64_to_cpu(bsb
.arraystart
);
4527 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
4528 wait_point
= __le64_to_cpu(bsb
.arraystart2
);
4531 reshape_completed
= sra
->reshape_progress
;
4532 rv
= progress_reshape(sra
, reshape
,
4533 backup_point
, wait_point
,
4534 &suspend_point
, &reshape_completed
,
4536 /* external metadata would need to ping_monitor here */
4537 sra
->reshape_progress
= reshape_completed
;
4539 /* Clear any backup region that is before 'here' */
4541 if (__le64_to_cpu(bsb
.length
) > 0 &&
4542 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart
) +
4543 __le64_to_cpu(bsb
.length
)))
4544 forget_backup(dests
, destfd
,
4546 if (__le64_to_cpu(bsb
.length2
) > 0 &&
4547 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart2
) +
4548 __le64_to_cpu(bsb
.length2
)))
4549 forget_backup(dests
, destfd
,
4552 if (__le64_to_cpu(bsb
.length
) > 0 &&
4553 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart
)))
4554 forget_backup(dests
, destfd
,
4556 if (__le64_to_cpu(bsb
.length2
) > 0 &&
4557 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart2
)))
4558 forget_backup(dests
, destfd
,
4568 if (rv
== 0 && increasing
&& !st
->ss
->external
) {
4569 /* No longer need to monitor this reshape */
4570 sysfs_set_str(sra
, NULL
, "sync_max", "max");
4576 unsigned long long offset
;
4577 unsigned long actual_stripes
;
4578 /* Need to backup some data.
4579 * If 'part' is not used and the desired
4580 * backup size is suspended, do a backup,
4581 * then consider the next part.
4583 /* Check that 'part' is unused */
4584 if (part
== 0 && __le64_to_cpu(bsb
.length
) != 0)
4586 if (part
== 1 && __le64_to_cpu(bsb
.length2
) != 0)
4589 offset
= backup_point
/ data
;
4590 actual_stripes
= stripes
;
4592 if (offset
+ actual_stripes
* (chunk
/512) >
4593 sra
->component_size
)
4594 actual_stripes
= ((sra
->component_size
- offset
)
4596 if (offset
+ actual_stripes
* (chunk
/512) >
4600 if (offset
< actual_stripes
* (chunk
/512))
4601 actual_stripes
= offset
/ (chunk
/512);
4602 offset
-= actual_stripes
* (chunk
/512);
4603 if (offset
< suspend_point
/data
)
4606 if (actual_stripes
== 0)
4608 grow_backup(sra
, offset
, actual_stripes
, fds
, offsets
,
4609 disks
, chunk
, level
, layout
, dests
, destfd
,
4610 destoffsets
, part
, °raded
, buf
);
4611 validate(afd
, destfd
[0], destoffsets
[0]);
4612 /* record where 'part' is up to */
4615 backup_point
+= actual_stripes
* (chunk
/512) * data
;
4617 backup_point
-= actual_stripes
* (chunk
/512) * data
;
4621 /* FIXME maybe call progress_reshape one more time instead */
4622 /* remove any remaining suspension */
4623 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
4624 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
4625 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
4626 sysfs_set_num(sra
, NULL
, "sync_min", 0);
4628 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
4629 sysfs_set_num(sra
, NULL
, "sync_speed_min", speed
);
4635 * If any spare contains md_back_data-1 which is recent wrt mtime,
4636 * write that data into the array and update the super blocks with
4637 * the new reshape_progress
4639 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
,
4640 int cnt
, char *backup_file
, int verbose
)
4644 unsigned long long *offsets
;
4645 unsigned long long nstripe
, ostripe
;
4648 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
4649 if (info
->array
.level
== 6)
4650 odata
--; /* number of data disks */
4651 ndata
= info
->array
.raid_disks
- 1;
4652 if (info
->new_level
== 6)
4655 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
4657 if (info
->delta_disks
<= 0)
4658 /* Didn't grow, so the backup file must have
4662 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
4663 struct mdinfo dinfo
;
4666 char *devname
, namebuf
[20];
4667 unsigned long long lo
, hi
;
4669 /* This was a spare and may have some saved data on it.
4670 * Load the superblock, find and load the
4671 * backup_super_block.
4672 * If either fail, go on to next device.
4673 * If the backup contains no new info, just return
4674 * else restore data and update all superblocks
4676 if (i
== old_disks
-1) {
4677 fd
= open(backup_file
, O_RDONLY
);
4679 pr_err("backup file %s inaccessible: %s\n",
4680 backup_file
, strerror(errno
));
4683 devname
= backup_file
;
4688 if (st
->ss
->load_super(st
, fd
, NULL
))
4691 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4692 st
->ss
->free_super(st
);
4695 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
4697 pr_err("Cannot seek on device %d\n", i
);
4698 continue; /* Cannot seek */
4700 sprintf(namebuf
, "device-%d", i
);
4703 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
)) {
4705 pr_err("Cannot read from %s\n", devname
);
4706 continue; /* Cannot read */
4708 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0 &&
4709 memcmp(bsb
.magic
, "md_backup_data-2", 16) != 0) {
4711 pr_err("No backup metadata on %s\n", devname
);
4714 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
))) {
4716 pr_err("Bad backup-metadata checksum on %s\n",
4718 continue; /* bad checksum */
4720 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0 &&
4721 bsb
.sb_csum2
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum2
)-((char*)&bsb
))) {
4723 pr_err("Bad backup-metadata checksum2 on %s\n",
4725 continue; /* Bad second checksum */
4727 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0) {
4729 pr_err("Wrong uuid on backup-metadata on %s\n",
4731 continue; /* Wrong uuid */
4735 * array utime and backup-mtime should be updated at
4736 * much the same time, but it seems that sometimes
4737 * they aren't... So allow considerable flexability in
4738 * matching, and allow this test to be overridden by
4739 * an environment variable.
4741 if(time_after(info
->array
.utime
, (unsigned int)__le64_to_cpu(bsb
.mtime
) + 2*60*60) ||
4742 time_before(info
->array
.utime
, (unsigned int)__le64_to_cpu(bsb
.mtime
) - 10*60)) {
4743 if (check_env("MDADM_GROW_ALLOW_OLD")) {
4744 pr_err("accepting backup with timestamp %lu for array with timestamp %lu\n",
4745 (unsigned long)__le64_to_cpu(bsb
.mtime
),
4746 (unsigned long)info
->array
.utime
);
4748 pr_err("too-old timestamp on backup-metadata on %s\n", devname
);
4749 pr_err("If you think it is should be safe, try 'export MDADM_GROW_ALLOW_OLD=1'\n");
4750 continue; /* time stamp is too bad */
4754 if (bsb
.magic
[15] == '1') {
4755 if (bsb
.length
== 0)
4757 if (info
->delta_disks
>= 0) {
4758 /* reshape_progress is increasing */
4759 if (__le64_to_cpu(bsb
.arraystart
)
4760 + __le64_to_cpu(bsb
.length
)
4761 < info
->reshape_progress
) {
4764 pr_err("backup-metadata found on %s but is not needed\n", devname
);
4765 continue; /* No new data here */
4768 /* reshape_progress is decreasing */
4769 if (__le64_to_cpu(bsb
.arraystart
) >=
4770 info
->reshape_progress
)
4771 goto nonew
; /* No new data here */
4774 if (bsb
.length
== 0 && bsb
.length2
== 0)
4776 if (info
->delta_disks
>= 0) {
4777 /* reshape_progress is increasing */
4778 if ((__le64_to_cpu(bsb
.arraystart
)
4779 + __le64_to_cpu(bsb
.length
)
4780 < info
->reshape_progress
) &&
4781 (__le64_to_cpu(bsb
.arraystart2
)
4782 + __le64_to_cpu(bsb
.length2
)
4783 < info
->reshape_progress
))
4784 goto nonew
; /* No new data here */
4786 /* reshape_progress is decreasing */
4787 if (__le64_to_cpu(bsb
.arraystart
) >=
4788 info
->reshape_progress
&&
4789 __le64_to_cpu(bsb
.arraystart2
) >=
4790 info
->reshape_progress
)
4791 goto nonew
; /* No new data here */
4794 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0) {
4797 pr_err("Failed to verify secondary backup-metadata block on %s\n",
4799 continue; /* Cannot seek */
4801 /* There should be a duplicate backup superblock 4k before here */
4802 if (lseek64(fd
, -4096, 1) < 0 ||
4803 read(fd
, &bsb2
, sizeof(bsb2
)) != sizeof(bsb2
))
4804 goto second_fail
; /* Cannot find leading superblock */
4805 if (bsb
.magic
[15] == '1')
4806 bsbsize
= offsetof(struct mdp_backup_super
, pad1
);
4808 bsbsize
= offsetof(struct mdp_backup_super
, pad
);
4809 if (memcmp(&bsb2
, &bsb
, bsbsize
) != 0)
4810 goto second_fail
; /* Cannot find leading superblock */
4812 /* Now need the data offsets for all devices. */
4813 offsets
= xmalloc(sizeof(*offsets
)*info
->array
.raid_disks
);
4814 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
4817 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
4818 /* FIXME should be this be an error */
4820 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4821 st
->ss
->free_super(st
);
4822 offsets
[j
] = dinfo
.data_offset
* 512;
4824 printf("%s: restoring critical section\n", Name
);
4826 if (restore_stripes(fdlist
, offsets
, info
->array
.raid_disks
,
4827 info
->new_chunk
, info
->new_level
,
4828 info
->new_layout
, fd
,
4829 __le64_to_cpu(bsb
.devstart
)*512,
4830 __le64_to_cpu(bsb
.arraystart
)*512,
4831 __le64_to_cpu(bsb
.length
)*512, NULL
)) {
4832 /* didn't succeed, so giveup */
4834 pr_err("Error restoring backup from %s\n",
4840 if (bsb
.magic
[15] == '2' &&
4841 restore_stripes(fdlist
, offsets
, info
->array
.raid_disks
,
4842 info
->new_chunk
, info
->new_level
,
4843 info
->new_layout
, fd
,
4844 __le64_to_cpu(bsb
.devstart
)*512 +
4845 __le64_to_cpu(bsb
.devstart2
)*512,
4846 __le64_to_cpu(bsb
.arraystart2
)*512,
4847 __le64_to_cpu(bsb
.length2
)*512, NULL
)) {
4848 /* didn't succeed, so giveup */
4850 pr_err("Error restoring second backup from %s\n",
4858 /* Ok, so the data is restored. Let's update those superblocks. */
4862 lo
= __le64_to_cpu(bsb
.arraystart
);
4863 hi
= lo
+ __le64_to_cpu(bsb
.length
);
4865 if (bsb
.magic
[15] == '2' && bsb
.length2
) {
4866 unsigned long long lo1
, hi1
;
4867 lo1
= __le64_to_cpu(bsb
.arraystart2
);
4868 hi1
= lo1
+ __le64_to_cpu(bsb
.length2
);
4872 } else if (lo
< lo1
)
4877 if (lo
< hi
&& (info
->reshape_progress
< lo
||
4878 info
->reshape_progress
> hi
))
4879 /* backup does not affect reshape_progress*/ ;
4880 else if (info
->delta_disks
>= 0) {
4881 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
) +
4882 __le64_to_cpu(bsb
.length
);
4883 if (bsb
.magic
[15] == '2') {
4884 unsigned long long p2
;
4886 p2
= __le64_to_cpu(bsb
.arraystart2
) +
4887 __le64_to_cpu(bsb
.length2
);
4888 if (p2
> info
->reshape_progress
)
4889 info
->reshape_progress
= p2
;
4892 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
);
4893 if (bsb
.magic
[15] == '2') {
4894 unsigned long long p2
;
4896 p2
= __le64_to_cpu(bsb
.arraystart2
);
4897 if (p2
< info
->reshape_progress
)
4898 info
->reshape_progress
= p2
;
4901 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
4904 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
4906 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4907 dinfo
.reshape_progress
= info
->reshape_progress
;
4908 st
->ss
->update_super(st
, &dinfo
, "_reshape_progress",
4910 st
->ss
->store_super(st
, fdlist
[j
]);
4911 st
->ss
->free_super(st
);
4915 /* Didn't find any backup data, try to see if any
4918 if (info
->delta_disks
< 0) {
4919 /* When shrinking, the critical section is at the end.
4920 * So see if we are before the critical section.
4922 unsigned long long first_block
;
4923 nstripe
= ostripe
= 0;
4925 while (ostripe
>= nstripe
) {
4926 ostripe
+= info
->array
.chunk_size
/ 512;
4927 first_block
= ostripe
* odata
;
4928 nstripe
= first_block
/ ndata
/ (info
->new_chunk
/512) *
4929 (info
->new_chunk
/512);
4932 if (info
->reshape_progress
>= first_block
)
4935 if (info
->delta_disks
> 0) {
4936 /* See if we are beyond the critical section. */
4937 unsigned long long last_block
;
4938 nstripe
= ostripe
= 0;
4940 while (nstripe
>= ostripe
) {
4941 nstripe
+= info
->new_chunk
/ 512;
4942 last_block
= nstripe
* ndata
;
4943 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
4944 (info
->array
.chunk_size
/512);
4947 if (info
->reshape_progress
>= last_block
)
4950 /* needed to recover critical section! */
4952 pr_err("Failed to find backup of critical section\n");
4956 int Grow_continue_command(char *devname
, int fd
,
4957 char *backup_file
, int verbose
)
4960 struct supertype
*st
= NULL
;
4961 struct mdinfo
*content
= NULL
;
4962 struct mdinfo array
;
4963 char *subarray
= NULL
;
4964 struct mdinfo
*cc
= NULL
;
4965 struct mdstat_ent
*mdstat
= NULL
;
4969 dprintf("Grow continue from command line called for %s\n", devname
);
4971 st
= super_by_fd(fd
, &subarray
);
4972 if (!st
|| !st
->ss
) {
4973 pr_err("Unable to determine metadata format for %s\n", devname
);
4976 dprintf("Grow continue is run for ");
4977 if (st
->ss
->external
== 0) {
4980 dprintf_cont("native array (%s)\n", devname
);
4981 if (md_get_array_info(fd
, &array
.array
) < 0) {
4982 pr_err("%s is not an active md array - aborting\n",
4985 goto Grow_continue_command_exit
;
4988 sysfs_init(content
, fd
, NULL
);
4989 /* Need to load a superblock.
4990 * FIXME we should really get what we need from
4994 for (d
= 0; d
< MAX_DISKS
; d
++) {
4995 mdu_disk_info_t disk
;
4999 if (md_get_disk_info(fd
, &disk
) < 0)
5001 if (disk
.major
== 0 && disk
.minor
== 0)
5003 if ((disk
.state
& (1 << MD_DISK_ACTIVE
)) == 0)
5005 dv
= map_dev(disk
.major
, disk
.minor
, 1);
5008 fd2
= dev_open(dv
, O_RDONLY
);
5011 err
= st
->ss
->load_super(st
, fd2
, NULL
);
5017 if (d
== MAX_DISKS
) {
5018 pr_err("Unable to load metadata for %s\n",
5021 goto Grow_continue_command_exit
;
5023 st
->ss
->getinfo_super(st
, content
, NULL
);
5024 if (!content
->reshape_active
)
5028 } while (cnt
-- > 0);
5033 dprintf_cont("subarray (%s)\n", subarray
);
5034 container
= st
->container_devnm
;
5035 cfd
= open_dev_excl(st
->container_devnm
);
5037 container
= st
->devnm
;
5039 cfd
= open_dev_excl(st
->devnm
);
5040 dprintf_cont("container (%s)\n", container
);
5044 pr_err("Unable to open container for %s\n", devname
);
5046 goto Grow_continue_command_exit
;
5049 /* find in container array under reshape
5051 ret_val
= st
->ss
->load_container(st
, cfd
, NULL
);
5053 pr_err("Cannot read superblock for %s\n", devname
);
5055 goto Grow_continue_command_exit
;
5058 cc
= st
->ss
->container_content(st
, subarray
);
5059 for (content
= cc
; content
; content
= content
->next
) {
5061 int allow_reshape
= 1;
5063 if (content
->reshape_active
== 0)
5065 /* The decision about array or container wide
5066 * reshape is taken in Grow_continue based
5067 * content->reshape_active state, therefore we
5068 * need to check_reshape based on
5069 * reshape_active and subarray name
5071 if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
))
5073 if (content
->reshape_active
== CONTAINER_RESHAPE
&&
5074 (content
->array
.state
5075 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
)))
5078 if (!allow_reshape
) {
5079 pr_err("cannot continue reshape of an array in container with unsupported metadata: %s(%s)\n",
5080 devname
, container
);
5082 goto Grow_continue_command_exit
;
5085 array_name
= strchr(content
->text_version
+1, '/')+1;
5086 mdstat
= mdstat_by_subdev(array_name
, container
);
5089 if (mdstat
->active
== 0) {
5090 pr_err("Skipping inactive array %s.\n",
5092 free_mdstat(mdstat
);
5099 pr_err("Unable to determine reshaped array for %s\n", devname
);
5101 goto Grow_continue_command_exit
;
5103 fd2
= open_dev(mdstat
->devnm
);
5105 pr_err("cannot open (%s)\n", mdstat
->devnm
);
5107 goto Grow_continue_command_exit
;
5110 if (sysfs_init(content
, fd2
, mdstat
->devnm
)) {
5111 pr_err("Unable to initialize sysfs for %s, Grow cannot continue.\n",
5115 goto Grow_continue_command_exit
;
5120 /* start mdmon in case it is not running
5122 if (!mdmon_running(container
))
5123 start_mdmon(container
);
5124 ping_monitor(container
);
5126 if (mdmon_running(container
))
5127 st
->update_tail
= &st
->updates
;
5129 pr_err("No mdmon found. Grow cannot continue.\n");
5131 goto Grow_continue_command_exit
;
5135 /* verify that array under reshape is started from
5138 if (verify_reshape_position(content
, content
->array
.level
) < 0) {
5140 goto Grow_continue_command_exit
;
5145 ret_val
= Grow_continue(fd
, st
, content
, backup_file
, 1, 0);
5147 Grow_continue_command_exit
:
5150 st
->ss
->free_super(st
);
5151 free_mdstat(mdstat
);
5158 int Grow_continue(int mdfd
, struct supertype
*st
, struct mdinfo
*info
,
5159 char *backup_file
, int forked
, int freeze_reshape
)
5163 if (!info
->reshape_active
)
5166 if (st
->ss
->external
) {
5167 int cfd
= open_dev(st
->container_devnm
);
5172 st
->ss
->load_container(st
, cfd
, st
->container_devnm
);
5174 ret_val
= reshape_container(st
->container_devnm
, NULL
, mdfd
,
5175 st
, info
, 0, backup_file
, 0,
5176 forked
, 1 | info
->reshape_active
,
5179 ret_val
= reshape_array(NULL
, mdfd
, "array", st
, info
, 1,
5180 NULL
, INVALID_SECTORS
, backup_file
,
5181 0, forked
, 1 | info
->reshape_active
,
5187 char *make_backup(char *name
)
5189 char *base
= "backup_file-";
5193 len
= strlen(MAP_DIR
) + 1 + strlen(base
) + strlen(name
)+1;
5194 fname
= xmalloc(len
);
5195 sprintf(fname
, "%s/%s%s", MAP_DIR
, base
, name
);
5199 char *locate_backup(char *name
)
5201 char *fl
= make_backup(name
);
5204 if (stat(fl
, &stb
) == 0 && S_ISREG(stb
.st_mode
))