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>
31 #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
32 #error no endian defined
38 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
41 int restore_backup(struct supertype
*st
,
42 struct mdinfo
*content
,
52 int disk_count
= next_spare
+ working_disks
;
53 char *backup_file
= *backup_filep
;
55 dprintf("Called restore_backup()\n");
56 fdlist
= xmalloc(sizeof(int) * disk_count
);
58 enable_fds(next_spare
);
59 for (i
= 0; i
< next_spare
; i
++)
61 for (dev
= content
->devs
; dev
; dev
= dev
->next
) {
67 fd
= dev_open(buf
, O_RDWR
);
69 if (dev
->disk
.raid_disk
>= 0)
70 fdlist
[dev
->disk
.raid_disk
] = fd
;
72 fdlist
[next_spare
++] = fd
;
76 backup_file
= locate_backup(content
->sys_name
);
77 *backup_filep
= backup_file
;
80 if (st
->ss
->external
&& st
->ss
->recover_backup
)
81 err
= st
->ss
->recover_backup(st
, content
);
83 err
= Grow_restart(st
, content
, fdlist
, next_spare
,
84 backup_file
, verbose
> 0);
86 while (next_spare
> 0) {
88 if (fdlist
[next_spare
] >= 0)
89 close(fdlist
[next_spare
]);
93 pr_err("Failed to restore critical"
94 " section for reshape - sorry.\n");
96 pr_err("Possibly you need"
97 " to specify a --backup-file\n");
101 dprintf("restore_backup() returns status OK.\n");
105 int Grow_Add_device(char *devname
, int fd
, char *newdev
)
107 /* Add a device to an active array.
108 * Currently, just extend a linear array.
109 * This requires writing a new superblock on the
110 * new device, calling the kernel to add the device,
111 * and if that succeeds, update the superblock on
113 * This means that we need to *find* all other devices.
120 struct supertype
*st
= NULL
;
121 char *subarray
= NULL
;
123 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
124 pr_err("cannot get array info for %s\n", devname
);
128 if (info
.array
.level
!= -1) {
129 pr_err("can only add devices to linear arrays\n");
133 st
= super_by_fd(fd
, &subarray
);
135 pr_err("cannot handle arrays with superblock version %d\n",
136 info
.array
.major_version
);
141 pr_err("Cannot grow linear sub-arrays yet\n");
147 nfd
= open(newdev
, O_RDWR
|O_EXCL
|O_DIRECT
);
149 pr_err("cannot open %s\n", newdev
);
154 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
155 pr_err("%s is not a block device!\n", newdev
);
160 /* now check out all the devices and make sure we can read the
162 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
163 mdu_disk_info_t disk
;
166 st
->ss
->free_super(st
);
169 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
170 pr_err("cannot get device detail for device %d\n",
176 dv
= map_dev(disk
.major
, disk
.minor
, 1);
178 pr_err("cannot find device file for device %d\n",
184 fd2
= dev_open(dv
, O_RDWR
);
186 pr_err("cannot open device file %s\n", dv
);
192 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
193 pr_err("cannot find super block on %s\n", dv
);
201 /* Ok, looks good. Lets update the superblock and write it out to
205 info
.disk
.number
= d
;
206 info
.disk
.major
= major(stb
.st_rdev
);
207 info
.disk
.minor
= minor(stb
.st_rdev
);
208 info
.disk
.raid_disk
= d
;
209 info
.disk
.state
= (1 << MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
);
210 st
->ss
->update_super(st
, &info
, "linear-grow-new", newdev
,
213 if (st
->ss
->store_super(st
, nfd
)) {
214 pr_err("Cannot store new superblock on %s\n",
221 if (ioctl(fd
, ADD_NEW_DISK
, &info
.disk
) != 0) {
222 pr_err("Cannot add new disk to this array\n");
225 /* Well, that seems to have worked.
226 * Now go through and update all superblocks
229 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
230 pr_err("cannot get array info for %s\n", devname
);
235 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
236 mdu_disk_info_t disk
;
240 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
241 pr_err("cannot get device detail for device %d\n",
245 dv
= map_dev(disk
.major
, disk
.minor
, 1);
247 pr_err("cannot find device file for device %d\n",
251 fd2
= dev_open(dv
, O_RDWR
);
253 pr_err("cannot open device file %s\n", dv
);
256 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
257 pr_err("cannot find super block on %s\n", dv
);
261 info
.array
.raid_disks
= nd
+1;
262 info
.array
.nr_disks
= nd
+1;
263 info
.array
.active_disks
= nd
+1;
264 info
.array
.working_disks
= nd
+1;
266 st
->ss
->update_super(st
, &info
, "linear-grow-update", dv
,
269 if (st
->ss
->store_super(st
, fd2
)) {
270 pr_err("Cannot store new superblock on %s\n", dv
);
280 int Grow_addbitmap(char *devname
, int fd
, struct context
*c
, struct shape
*s
)
283 * First check that array doesn't have a bitmap
284 * Then create the bitmap
287 * For internal bitmaps, we need to check the version,
288 * find all the active devices, and write the bitmap block
291 mdu_bitmap_file_t bmf
;
292 mdu_array_info_t array
;
293 struct supertype
*st
;
294 char *subarray
= NULL
;
295 int major
= BITMAP_MAJOR_HI
;
296 int vers
= md_get_version(fd
);
297 unsigned long long bitmapsize
, array_size
;
300 major
= BITMAP_MAJOR_HOSTENDIAN
;
301 pr_err("Warning - bitmaps created on this kernel"
302 " are not portable\n"
303 " between different architectures. Consider upgrading"
304 " the Linux kernel.\n");
307 if (ioctl(fd
, GET_BITMAP_FILE
, &bmf
) != 0) {
309 pr_err("Memory allocation failure.\n");
311 pr_err("bitmaps not supported by this kernel.\n");
314 if (bmf
.pathname
[0]) {
315 if (strcmp(s
->bitmap_file
,"none")==0) {
316 if (ioctl(fd
, SET_BITMAP_FILE
, -1)!= 0) {
317 pr_err("failed to remove bitmap %s\n",
323 pr_err("%s already has a bitmap (%s)\n",
324 devname
, bmf
.pathname
);
327 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
328 pr_err("cannot get array status for %s\n", devname
);
331 if (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)) {
332 if (strcmp(s
->bitmap_file
, "none")==0) {
333 array
.state
&= ~(1<<MD_SB_BITMAP_PRESENT
);
334 if (ioctl(fd
, SET_ARRAY_INFO
, &array
)!= 0) {
335 pr_err("failed to remove internal bitmap.\n");
340 pr_err("Internal bitmap already present on %s\n",
345 if (strcmp(s
->bitmap_file
, "none") == 0) {
346 pr_err("no bitmap found on %s\n", devname
);
349 if (array
.level
<= 0) {
350 pr_err("Bitmaps not meaningful with level %s\n",
351 map_num(pers
, array
.level
)?:"of this array");
354 bitmapsize
= array
.size
;
356 if (get_dev_size(fd
, NULL
, &array_size
) &&
357 array_size
> (0x7fffffffULL
<<9)) {
358 /* Array is big enough that we cannot trust array.size
359 * try other approaches
361 bitmapsize
= get_component_size(fd
);
363 if (bitmapsize
== 0) {
364 pr_err("Cannot reliably determine size of array to create bitmap - sorry.\n");
368 if (array
.level
== 10) {
369 int ncopies
= (array
.layout
&255)*((array
.layout
>>8)&255);
370 bitmapsize
= bitmapsize
* array
.raid_disks
/ ncopies
;
373 st
= super_by_fd(fd
, &subarray
);
375 pr_err("Cannot understand version %d.%d\n",
376 array
.major_version
, array
.minor_version
);
380 pr_err("Cannot add bitmaps to sub-arrays yet\n");
385 if (strcmp(s
->bitmap_file
, "internal") == 0) {
388 int offset_setable
= 0;
390 if (st
->ss
->add_internal_bitmap
== NULL
) {
391 pr_err("Internal bitmaps not supported "
392 "with %s metadata\n", st
->ss
->name
);
395 mdi
= sysfs_read(fd
, NULL
, GET_BITMAP_LOCATION
);
398 for (d
=0; d
< st
->max_devs
; d
++) {
399 mdu_disk_info_t disk
;
402 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
404 if (disk
.major
== 0 &&
407 if ((disk
.state
& (1<<MD_DISK_SYNC
))==0)
409 dv
= map_dev(disk
.major
, disk
.minor
, 1);
411 int fd2
= dev_open(dv
, O_RDWR
);
414 if (st
->ss
->load_super(st
, fd2
, NULL
)==0) {
415 if (st
->ss
->add_internal_bitmap(
417 &s
->bitmap_chunk
, c
->delay
, s
->write_behind
,
418 bitmapsize
, offset_setable
,
421 st
->ss
->write_bitmap(st
, fd2
);
423 pr_err("failed to create internal bitmap"
424 " - chunksize problem.\n");
432 if (offset_setable
) {
433 st
->ss
->getinfo_super(st
, mdi
, NULL
);
434 sysfs_init(mdi
, fd
, NULL
);
435 rv
= sysfs_set_num_signed(mdi
, NULL
, "bitmap/location",
438 array
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
439 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
443 pr_err("Cannot add bitmap while array is"
444 " resyncing or reshaping etc.\n");
445 pr_err("failed to set internal bitmap.\n");
452 int max_devs
= st
->max_devs
;
454 /* try to load a superblock */
455 for (d
= 0; d
< max_devs
; d
++) {
456 mdu_disk_info_t disk
;
460 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
462 if ((disk
.major
==0 && disk
.minor
==0) ||
463 (disk
.state
& (1<<MD_DISK_REMOVED
)))
465 dv
= map_dev(disk
.major
, disk
.minor
, 1);
468 fd2
= dev_open(dv
, O_RDONLY
);
470 if (st
->ss
->load_super(st
, fd2
, NULL
) == 0) {
472 st
->ss
->uuid_from_super(st
, uuid
);
479 pr_err("cannot find UUID for array!\n");
482 if (CreateBitmap(s
->bitmap_file
, c
->force
, (char*)uuid
, s
->bitmap_chunk
,
483 c
->delay
, s
->write_behind
, bitmapsize
, major
)) {
486 bitmap_fd
= open(s
->bitmap_file
, O_RDWR
);
488 pr_err("weird: %s cannot be opened\n",
492 if (ioctl(fd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
495 pr_err("Cannot add bitmap while array is"
496 " resyncing or reshaping etc.\n");
497 pr_err("Cannot set bitmap file for %s: %s\n",
498 devname
, strerror(err
));
507 * When reshaping an array we might need to backup some data.
508 * This is written to all spares with a 'super_block' describing it.
509 * The superblock goes 4K from the end of the used space on the
511 * It if written after the backup is complete.
512 * It has the following structure.
515 static struct mdp_backup_super
{
516 char magic
[16]; /* md_backup_data-1 or -2 */
519 /* start/sizes in 512byte sectors */
520 __u64 devstart
; /* address on backup device/file of data */
523 __u32 sb_csum
; /* csum of preceeding bytes. */
525 __u64 devstart2
; /* offset in to data of second section */
528 __u32 sb_csum2
; /* csum of preceeding bytes. */
530 } __attribute__((aligned(512))) bsb
, bsb2
;
532 static __u32
bsb_csum(char *buf
, int len
)
536 for (i
= 0; i
< len
; i
++)
537 csum
= (csum
<<3) + buf
[0];
538 return __cpu_to_le32(csum
);
541 static int check_idle(struct supertype
*st
)
543 /* Check that all member arrays for this container, or the
544 * container of this array, are idle
546 char *container
= (st
->container_devnm
[0]
547 ? st
->container_devnm
: st
->devnm
);
548 struct mdstat_ent
*ent
, *e
;
551 ent
= mdstat_read(0, 0);
552 for (e
= ent
; e
; e
= e
->next
) {
553 if (!is_container_member(e
, container
))
555 if (e
->percent
>= 0) {
564 static int freeze_container(struct supertype
*st
)
566 char *container
= (st
->container_devnm
[0]
567 ? st
->container_devnm
: st
->devnm
);
572 if (block_monitor(container
, 1)) {
573 pr_err("failed to freeze container\n");
580 static void unfreeze_container(struct supertype
*st
)
582 char *container
= (st
->container_devnm
[0]
583 ? st
->container_devnm
: st
->devnm
);
585 unblock_monitor(container
, 1);
588 static int freeze(struct supertype
*st
)
590 /* Try to freeze resync/rebuild on this array/container.
591 * Return -1 if the array is busy,
592 * return -2 container cannot be frozen,
593 * return 0 if this kernel doesn't support 'frozen'
594 * return 1 if it worked.
596 if (st
->ss
->external
)
597 return freeze_container(st
);
599 struct mdinfo
*sra
= sysfs_read(-1, st
->devnm
, GET_VERSION
);
605 /* Need to clear any 'read-auto' status */
606 if (sysfs_get_str(sra
, NULL
, "array_state", buf
, 20) > 0 &&
607 strncmp(buf
, "read-auto", 9) == 0)
608 sysfs_set_str(sra
, NULL
, "array_state", "clean");
610 err
= sysfs_freeze_array(sra
);
616 static void unfreeze(struct supertype
*st
)
618 if (st
->ss
->external
)
619 return unfreeze_container(st
);
621 struct mdinfo
*sra
= sysfs_read(-1, st
->devnm
, GET_VERSION
);
625 sysfs_get_str(sra
, NULL
, "sync_action", buf
, 20) > 0
626 && strcmp(buf
, "frozen\n") == 0) {
627 printf("unfreeze\n");
628 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
634 static void wait_reshape(struct mdinfo
*sra
)
636 int fd
= sysfs_get_fd(sra
, NULL
, "sync_action");
642 while (sysfs_fd_get_str(fd
, action
, 20) > 0 &&
643 strncmp(action
, "reshape", 7) == 0)
644 sysfs_wait(fd
, NULL
);
648 static int reshape_super(struct supertype
*st
, unsigned long long size
,
649 int level
, int layout
, int chunksize
, int raid_disks
,
650 int delta_disks
, char *backup_file
, char *dev
,
651 int direction
, int verbose
)
653 /* nothing extra to check in the native case */
654 if (!st
->ss
->external
)
656 if (!st
->ss
->reshape_super
||
657 !st
->ss
->manage_reshape
) {
658 pr_err("%s metadata does not support reshape\n",
663 return st
->ss
->reshape_super(st
, size
, level
, layout
, chunksize
,
664 raid_disks
, delta_disks
, backup_file
, dev
,
668 static void sync_metadata(struct supertype
*st
)
670 if (st
->ss
->external
) {
671 if (st
->update_tail
) {
672 flush_metadata_updates(st
);
673 st
->update_tail
= &st
->updates
;
675 st
->ss
->sync_metadata(st
);
679 static int subarray_set_num(char *container
, struct mdinfo
*sra
, char *name
, int n
)
681 /* when dealing with external metadata subarrays we need to be
682 * prepared to handle EAGAIN. The kernel may need to wait for
683 * mdmon to mark the array active so the kernel can handle
684 * allocations/writeback when preparing the reshape action
685 * (md_allow_write()). We temporarily disable safe_mode_delay
686 * to close a race with the array_state going clean before the
687 * next write to raid_disks / stripe_cache_size
692 /* only 'raid_disks' and 'stripe_cache_size' trigger md_allow_write */
694 (strcmp(name
, "raid_disks") != 0 &&
695 strcmp(name
, "stripe_cache_size") != 0))
696 return sysfs_set_num(sra
, NULL
, name
, n
);
698 rc
= sysfs_get_str(sra
, NULL
, "safe_mode_delay", safe
, sizeof(safe
));
701 sysfs_set_num(sra
, NULL
, "safe_mode_delay", 0);
702 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
703 if (rc
< 0 && errno
== EAGAIN
) {
704 ping_monitor(container
);
705 /* if we get EAGAIN here then the monitor is not active
708 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
710 sysfs_set_str(sra
, NULL
, "safe_mode_delay", safe
);
714 int start_reshape(struct mdinfo
*sra
, int already_running
,
715 int before_data_disks
, int data_disks
)
718 unsigned long long sync_max_to_set
;
720 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
721 err
= sysfs_set_num(sra
, NULL
, "suspend_hi", sra
->reshape_progress
);
722 err
= err
?: sysfs_set_num(sra
, NULL
, "suspend_lo",
723 sra
->reshape_progress
);
724 if (before_data_disks
<= data_disks
)
725 sync_max_to_set
= sra
->reshape_progress
/ data_disks
;
727 sync_max_to_set
= (sra
->component_size
* data_disks
728 - sra
->reshape_progress
) / data_disks
;
729 if (!already_running
)
730 sysfs_set_num(sra
, NULL
, "sync_min", sync_max_to_set
);
731 err
= err
?: sysfs_set_num(sra
, NULL
, "sync_max", sync_max_to_set
);
732 if (!already_running
)
733 err
= err
?: sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
738 void abort_reshape(struct mdinfo
*sra
)
740 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
741 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
742 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
743 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
744 sysfs_set_num(sra
, NULL
, "sync_min", 0);
745 // It isn't safe to reset sync_max as we aren't monitoring.
746 // Array really should be stopped at this point.
749 int remove_disks_for_takeover(struct supertype
*st
,
754 struct mdinfo
*remaining
;
757 if (sra
->array
.level
== 10)
758 nr_of_copies
= layout
& 0xff;
759 else if (sra
->array
.level
== 1)
760 nr_of_copies
= sra
->array
.raid_disks
;
764 remaining
= sra
->devs
;
766 /* for each 'copy', select one device and remove from the list. */
767 for (slot
= 0; slot
< sra
->array
.raid_disks
; slot
+= nr_of_copies
) {
768 struct mdinfo
**diskp
;
771 /* Find a working device to keep */
772 for (diskp
= &remaining
; *diskp
; diskp
= &(*diskp
)->next
) {
773 struct mdinfo
*disk
= *diskp
;
775 if (disk
->disk
.raid_disk
< slot
)
777 if (disk
->disk
.raid_disk
>= slot
+ nr_of_copies
)
779 if (disk
->disk
.state
& (1<<MD_DISK_REMOVED
))
781 if (disk
->disk
.state
& (1<<MD_DISK_FAULTY
))
783 if (!(disk
->disk
.state
& (1<<MD_DISK_SYNC
)))
786 /* We have found a good disk to use! */
788 disk
->next
= sra
->devs
;
797 if (slot
< sra
->array
.raid_disks
) {
798 /* didn't find all slots */
804 sra
->devs
= remaining
;
808 /* Remove all 'remaining' devices from the array */
810 struct mdinfo
*sd
= remaining
;
811 remaining
= sd
->next
;
813 sysfs_set_str(sra
, sd
, "state", "faulty");
814 sysfs_set_str(sra
, sd
, "slot", "none");
815 /* for external metadata disks should be removed in mdmon */
816 if (!st
->ss
->external
)
817 sysfs_set_str(sra
, sd
, "state", "remove");
818 sd
->disk
.state
|= (1<<MD_DISK_REMOVED
);
819 sd
->disk
.state
&= ~(1<<MD_DISK_SYNC
);
820 sd
->next
= sra
->devs
;
826 void reshape_free_fdlist(int *fdlist
,
827 unsigned long long *offsets
,
832 for (i
= 0; i
< size
; i
++)
840 int reshape_prepare_fdlist(char *devname
,
844 unsigned long blocks
,
847 unsigned long long *offsets
)
853 for (d
= 0; d
<= nrdisks
; d
++)
856 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
857 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
859 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
860 char *dn
= map_dev(sd
->disk
.major
,
862 fdlist
[sd
->disk
.raid_disk
]
863 = dev_open(dn
, O_RDONLY
);
864 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
865 if (fdlist
[sd
->disk
.raid_disk
] < 0) {
866 pr_err("%s: cannot open component %s\n",
867 devname
, dn
? dn
: "-unknown-");
871 } else if (backup_file
== NULL
) {
873 char *dn
= map_dev(sd
->disk
.major
,
875 fdlist
[d
] = dev_open(dn
, O_RDWR
);
876 offsets
[d
] = (sd
->data_offset
+ sra
->component_size
- blocks
- 8)*512;
878 pr_err("%s: cannot open component %s\n",
879 devname
, dn
? dn
: "-unknown-");
890 int reshape_open_backup_file(char *backup_file
,
895 unsigned long long *offsets
,
899 /* Return 1 on success, 0 on any form of failure */
900 /* need to check backup file is large enough */
906 *fdlist
= open(backup_file
, O_RDWR
|O_CREAT
|(restart
? O_TRUNC
: O_EXCL
),
910 pr_err("%s: cannot create backup file %s: %s\n",
911 devname
, backup_file
, strerror(errno
));
914 /* Guard against backup file being on array device.
915 * If array is partitioned or if LVM etc is in the
916 * way this will not notice, but it is better than
919 fstat(*fdlist
, &stb
);
922 if (stb
.st_rdev
== dev
) {
923 pr_err("backup file must NOT be"
924 " on the array being reshaped.\n");
930 for (i
=0; i
< blocks
+ 8 ; i
++) {
931 if (write(*fdlist
, buf
, 512) != 512) {
932 pr_err("%s: cannot create"
933 " backup file %s: %s\n",
934 devname
, backup_file
, strerror(errno
));
938 if (fsync(*fdlist
) != 0) {
939 pr_err("%s: cannot create backup file %s: %s\n",
940 devname
, backup_file
, strerror(errno
));
944 if (!restart
&& strncmp(backup_file
, MAP_DIR
, strlen(MAP_DIR
)) != 0) {
945 char *bu
= make_backup(sys_name
);
946 symlink(backup_file
, bu
);
953 unsigned long compute_backup_blocks(int nchunk
, int ochunk
,
954 unsigned int ndata
, unsigned int odata
)
956 unsigned long a
, b
, blocks
;
957 /* So how much do we need to backup.
958 * We need an amount of data which is both a whole number of
959 * old stripes and a whole number of new stripes.
960 * So LCM for (chunksize*datadisks).
962 a
= (ochunk
/512) * odata
;
963 b
= (nchunk
/512) * ndata
;
966 /* LCM == product / GCD */
967 blocks
= (ochunk
/512) * (nchunk
/512) * odata
* ndata
/ a
;
972 char *analyse_change(char *devname
, struct mdinfo
*info
, struct reshape
*re
)
974 /* Based on the current array state in info->array and
975 * the changes in info->new_* etc, determine:
976 * - whether the change is possible
977 * - Intermediate level/raid_disks/layout
978 * - whether a restriping reshape is needed
979 * - number of sectors in minimum change unit. This
980 * will cover a whole number of stripes in 'before' and
983 * Return message if the change should be rejected
984 * NULL if the change can be achieved
986 * This can be called as part of starting a reshape, or
987 * when assembling an array that is undergoing reshape.
989 int near
, far
, offset
, copies
;
991 int old_chunk
, new_chunk
;
992 /* delta_parity records change in number of devices
993 * caused by level change
995 int delta_parity
= 0;
997 memset(re
, 0, sizeof(*re
));
999 /* If a new level not explicitly given, we assume no-change */
1000 if (info
->new_level
== UnSet
)
1001 info
->new_level
= info
->array
.level
;
1003 if (info
->new_chunk
)
1004 switch (info
->new_level
) {
1010 /* chunk size is meaningful, must divide component_size
1013 if (info
->component_size
% (info
->new_chunk
/512)) {
1014 unsigned long long shrink
= info
->component_size
;
1015 shrink
&= ~(unsigned long long)(info
->new_chunk
/512-1);
1016 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk)\n",
1017 info
->new_chunk
/1024, info
->component_size
/2);
1018 pr_err("After shrinking any filesystem, \"mdadm --grow %s --size %llu\"\n",
1020 pr_err("will shrink the array so the given chunk size would work.\n");
1025 return "chunk size not meaningful for this level";
1028 info
->new_chunk
= info
->array
.chunk_size
;
1030 switch (info
->array
.level
) {
1032 return "Cannot understand this RAID level";
1034 /* RAID1 can convert to RAID1 with different disks, or
1035 * raid5 with 2 disks, or
1038 if (info
->new_level
> 1 &&
1039 (info
->component_size
& 7))
1040 return "Cannot convert RAID1 of this size - "
1041 "reduce size to multiple of 4K first.";
1042 if (info
->new_level
== 0) {
1043 if (info
->delta_disks
!= UnSet
&&
1044 info
->delta_disks
!= 0)
1045 return "Cannot change number of disks "
1046 "with RAID1->RAID0 conversion";
1048 re
->before
.data_disks
= 1;
1049 re
->after
.data_disks
= 1;
1052 if (info
->new_level
== 1) {
1053 if (info
->delta_disks
== UnSet
)
1054 /* Don't know what to do */
1055 return "no change requested for Growing RAID1";
1059 if (info
->array
.raid_disks
== 2 &&
1060 info
->new_level
== 5) {
1063 re
->before
.data_disks
= 1;
1064 if (info
->delta_disks
!= UnSet
&&
1065 info
->delta_disks
!= 0)
1066 re
->after
.data_disks
= 1 + info
->delta_disks
;
1068 re
->after
.data_disks
= 1;
1069 if (re
->after
.data_disks
< 1)
1070 return "Number of disks too small for RAID5";
1072 re
->before
.layout
= ALGORITHM_LEFT_SYMMETRIC
;
1073 info
->array
.chunk_size
= 65536;
1076 /* Could do some multi-stage conversions, but leave that to
1079 return "Impossibly level change request for RAID1";
1082 /* RAID10 can be converted from near mode to
1083 * RAID0 by removing some devices.
1084 * It can also be reshaped if the kernel supports
1087 switch (info
->new_level
) {
1089 if ((info
->array
.layout
& ~0xff) != 0x100)
1090 return "Cannot Grow RAID10 with far/offset layout";
1091 /* number of devices must be multiple of number of copies */
1092 if (info
->array
.raid_disks
% (info
->array
.layout
& 0xff))
1093 return "RAID10 layout too complex for Grow operation";
1095 new_disks
= (info
->array
.raid_disks
1096 / (info
->array
.layout
& 0xff));
1097 if (info
->delta_disks
== UnSet
)
1098 info
->delta_disks
= (new_disks
1099 - info
->array
.raid_disks
);
1101 if (info
->delta_disks
!= new_disks
- info
->array
.raid_disks
)
1102 return "New number of raid-devices impossible for RAID10";
1103 if (info
->new_chunk
&&
1104 info
->new_chunk
!= info
->array
.chunk_size
)
1105 return "Cannot change chunk-size with RAID10 Grow";
1109 re
->before
.data_disks
= new_disks
;
1110 re
->after
.data_disks
= re
->before
.data_disks
;
1114 near
= info
->array
.layout
& 0xff;
1115 far
= (info
->array
.layout
>> 8) & 0xff;
1116 offset
= info
->array
.layout
& 0x10000;
1117 if (far
> 1 && !offset
)
1118 return "Cannot reshape RAID10 in far-mode";
1119 copies
= near
* far
;
1121 old_chunk
= info
->array
.chunk_size
* far
;
1123 if (info
->new_layout
== UnSet
)
1124 info
->new_layout
= info
->array
.layout
;
1126 near
= info
->new_layout
& 0xff;
1127 far
= (info
->new_layout
>> 8) & 0xff;
1128 offset
= info
->new_layout
& 0x10000;
1129 if (far
> 1 && !offset
)
1130 return "Cannot reshape RAID10 to far-mode";
1131 if (near
* far
!= copies
)
1132 return "Cannot change number of copies"
1133 " when reshaping RAID10";
1135 if (info
->delta_disks
== UnSet
)
1136 info
->delta_disks
= 0;
1137 new_disks
= (info
->array
.raid_disks
+
1140 new_chunk
= info
->new_chunk
* far
;
1143 re
->before
.layout
= info
->array
.layout
;
1144 re
->before
.data_disks
= info
->array
.raid_disks
;
1145 re
->after
.layout
= info
->new_layout
;
1146 re
->after
.data_disks
= new_disks
;
1147 /* For RAID10 we don't do backup but do allow reshape,
1148 * so set backup_blocks to INVALID_SECTORS rather than
1150 * And there is no need to synchronise stripes on both
1151 * 'old' and 'new'. So the important
1152 * number is the minimum data_offset difference
1153 * which is the larger of (offset copies * chunk).
1155 re
->backup_blocks
= INVALID_SECTORS
;
1156 re
->min_offset_change
= max(old_chunk
, new_chunk
) / 512;
1157 if (new_disks
< re
->before
.data_disks
&&
1158 info
->space_after
< re
->min_offset_change
)
1159 /* Reduce component size by one chunk */
1160 re
->new_size
= (info
->component_size
-
1161 re
->min_offset_change
);
1163 re
->new_size
= info
->component_size
;
1164 re
->new_size
= re
->new_size
* new_disks
/ copies
;
1168 return "RAID10 can only be changed to RAID0";
1171 /* RAID0 can be converted to RAID10, or to RAID456 */
1172 if (info
->new_level
== 10) {
1173 if (info
->new_layout
== UnSet
&& info
->delta_disks
== UnSet
) {
1174 /* Assume near=2 layout */
1175 info
->new_layout
= 0x102;
1176 info
->delta_disks
= info
->array
.raid_disks
;
1178 if (info
->new_layout
== UnSet
) {
1179 int copies
= 1 + (info
->delta_disks
1180 / info
->array
.raid_disks
);
1181 if (info
->array
.raid_disks
* (copies
-1)
1182 != info
->delta_disks
)
1183 return "Impossible number of devices"
1184 " for RAID0->RAID10";
1185 info
->new_layout
= 0x100 + copies
;
1187 if (info
->delta_disks
== UnSet
) {
1188 int copies
= info
->new_layout
& 0xff;
1189 if (info
->new_layout
!= 0x100 + copies
)
1190 return "New layout impossible"
1191 " for RAID0->RAID10";;
1192 info
->delta_disks
= (copies
- 1) *
1193 info
->array
.raid_disks
;
1195 if (info
->new_chunk
&&
1196 info
->new_chunk
!= info
->array
.chunk_size
)
1197 return "Cannot change chunk-size with RAID0->RAID10";
1200 re
->before
.data_disks
= (info
->array
.raid_disks
+
1202 re
->after
.data_disks
= re
->before
.data_disks
;
1203 re
->before
.layout
= info
->new_layout
;
1207 /* RAID0 can also covert to RAID0/4/5/6 by first converting to
1208 * a raid4 style layout of the final level.
1210 switch (info
->new_level
) {
1215 re
->before
.layout
= 0;
1220 re
->before
.layout
= ALGORITHM_PARITY_N
;
1221 if (info
->new_layout
== UnSet
)
1222 info
->new_layout
= map_name(r5layout
, "default");
1227 re
->before
.layout
= ALGORITHM_PARITY_N
;
1228 if (info
->new_layout
== UnSet
)
1229 info
->new_layout
= map_name(r6layout
, "default");
1232 return "Impossible level change requested";
1234 re
->before
.data_disks
= info
->array
.raid_disks
;
1235 /* determining 'after' layout happens outside this 'switch' */
1239 info
->array
.layout
= ALGORITHM_PARITY_N
;
1241 switch (info
->new_level
) {
1245 re
->level
= info
->array
.level
;
1246 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1247 re
->before
.layout
= info
->array
.layout
;
1251 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1252 re
->before
.layout
= info
->array
.layout
;
1257 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1258 switch (info
->array
.layout
) {
1259 case ALGORITHM_LEFT_ASYMMETRIC
:
1260 re
->before
.layout
= ALGORITHM_LEFT_ASYMMETRIC_6
;
1262 case ALGORITHM_RIGHT_ASYMMETRIC
:
1263 re
->before
.layout
= ALGORITHM_RIGHT_ASYMMETRIC_6
;
1265 case ALGORITHM_LEFT_SYMMETRIC
:
1266 re
->before
.layout
= ALGORITHM_LEFT_SYMMETRIC_6
;
1268 case ALGORITHM_RIGHT_SYMMETRIC
:
1269 re
->before
.layout
= ALGORITHM_RIGHT_SYMMETRIC_6
;
1271 case ALGORITHM_PARITY_0
:
1272 re
->before
.layout
= ALGORITHM_PARITY_0_6
;
1274 case ALGORITHM_PARITY_N
:
1275 re
->before
.layout
= ALGORITHM_PARITY_N_6
;
1278 return "Cannot convert an array with this layout";
1282 if (info
->array
.raid_disks
!= 2)
1283 return "Can only convert a 2-device array to RAID1";
1284 if (info
->delta_disks
!= UnSet
&&
1285 info
->delta_disks
!= 0)
1286 return "Cannot set raid_disk when "
1287 "converting RAID5->RAID1";
1289 info
->new_chunk
= 0;
1292 return "Impossible level change requested";
1296 switch (info
->new_level
) {
1302 re
->before
.data_disks
= info
->array
.raid_disks
- 2;
1303 re
->before
.layout
= info
->array
.layout
;
1306 return "Impossible level change requested";
1311 /* If we reached here then it looks like a re-stripe is
1312 * happening. We have determined the intermediate level
1313 * and initial raid_disks/layout and stored these in 're'.
1315 * We need to deduce the final layout that can be atomically
1316 * converted to the end state.
1318 switch (info
->new_level
) {
1320 /* We can only get to RAID0 from RAID4 or RAID5
1321 * with appropriate layout and one extra device
1323 if (re
->level
!= 4 && re
->level
!= 5)
1324 return "Cannot covert to RAID0 from this level";
1326 switch (re
->level
) {
1328 re
->before
.layout
= 0;
1329 re
->after
.layout
= 0;
1332 re
->after
.layout
= ALGORITHM_PARITY_N
;
1338 /* We can only get to RAID4 from RAID5 */
1339 if (re
->level
!= 4 && re
->level
!= 5)
1340 return "Cannot convert to RAID4 from this level";
1342 switch (re
->level
) {
1344 re
->before
.layout
= 0;
1345 re
->after
.layout
= 0;
1348 re
->after
.layout
= ALGORITHM_PARITY_N
;
1354 /* We get to RAID5 from RAID5 or RAID6 */
1355 if (re
->level
!= 5 && re
->level
!= 6)
1356 return "Cannot convert to RAID5 from this level";
1358 switch (re
->level
) {
1360 if (info
->new_layout
== UnSet
)
1361 re
->after
.layout
= re
->before
.layout
;
1363 re
->after
.layout
= info
->new_layout
;
1366 if (info
->new_layout
== UnSet
)
1367 info
->new_layout
= re
->before
.layout
;
1369 /* after.layout needs to be raid6 version of new_layout */
1370 if (info
->new_layout
== ALGORITHM_PARITY_N
)
1371 re
->after
.layout
= ALGORITHM_PARITY_N
;
1374 char *ls
= map_num(r5layout
, info
->new_layout
);
1377 /* Current RAID6 layout has a RAID5
1380 strcat(strcpy(layout
, ls
), "-6");
1381 l
= map_name(r6layout
, layout
);
1383 return "Cannot find RAID6 layout"
1386 /* Current RAID6 has no equivalent.
1387 * If it is already a '-6' layout we
1388 * can leave it unchanged, else we must
1391 ls
= map_num(r6layout
, info
->new_layout
);
1393 strcmp(ls
+strlen(ls
)-2, "-6") != 0)
1394 return "Please specify new layout";
1395 l
= info
->new_layout
;
1397 re
->after
.layout
= l
;
1403 /* We must already be at level 6 */
1405 return "Impossible level change";
1406 if (info
->new_layout
== UnSet
)
1407 re
->after
.layout
= info
->array
.layout
;
1409 re
->after
.layout
= info
->new_layout
;
1412 return "Impossible level change requested";
1414 if (info
->delta_disks
== UnSet
)
1415 info
->delta_disks
= delta_parity
;
1417 re
->after
.data_disks
= (re
->before
.data_disks
1420 switch (re
->level
) {
1421 case 6: re
->parity
= 2;
1424 case 5: re
->parity
= 1;
1426 default: re
->parity
= 0;
1429 /* So we have a restripe operation, we need to calculate the number
1430 * of blocks per reshape operation.
1432 re
->new_size
= info
->component_size
* re
->before
.data_disks
;
1433 if (info
->new_chunk
== 0)
1434 info
->new_chunk
= info
->array
.chunk_size
;
1435 if (re
->after
.data_disks
== re
->before
.data_disks
&&
1436 re
->after
.layout
== re
->before
.layout
&&
1437 info
->new_chunk
== info
->array
.chunk_size
) {
1438 /* Nothing to change, can change level immediately. */
1439 re
->level
= info
->new_level
;
1440 re
->backup_blocks
= 0;
1443 if (re
->after
.data_disks
== 1 && re
->before
.data_disks
== 1) {
1444 /* chunk and layout changes make no difference */
1445 re
->level
= info
->new_level
;
1446 re
->backup_blocks
= 0;
1450 if (re
->after
.data_disks
== re
->before
.data_disks
&&
1451 get_linux_version() < 2006032)
1452 return "in-place reshape is not safe before 2.6.32 - sorry.";
1454 if (re
->after
.data_disks
< re
->before
.data_disks
&&
1455 get_linux_version() < 2006030)
1456 return "reshape to fewer devices is not supported before 2.6.30 - sorry.";
1458 re
->backup_blocks
= compute_backup_blocks(
1459 info
->new_chunk
, info
->array
.chunk_size
,
1460 re
->after
.data_disks
,
1461 re
->before
.data_disks
);
1462 re
->min_offset_change
= re
->backup_blocks
/ re
->before
.data_disks
;
1464 re
->new_size
= info
->component_size
* re
->after
.data_disks
;
1468 static int set_array_size(struct supertype
*st
, struct mdinfo
*sra
,
1471 struct mdinfo
*info
;
1475 if ((st
== NULL
) || (sra
== NULL
))
1478 if (text_version
== NULL
)
1479 text_version
= sra
->text_version
;
1480 subarray
= strchr(text_version
+1, '/')+1;
1481 info
= st
->ss
->container_content(st
, subarray
);
1483 unsigned long long current_size
= 0;
1484 unsigned long long new_size
=
1485 info
->custom_array_size
/2;
1487 if (sysfs_get_ll(sra
, NULL
, "array_size", ¤t_size
) == 0 &&
1488 new_size
> current_size
) {
1489 if (sysfs_set_num(sra
, NULL
, "array_size", new_size
)
1491 dprintf("Error: Cannot set array size");
1494 dprintf("Array size changed");
1496 dprintf(" from %llu to %llu.\n",
1497 current_size
, new_size
);
1501 dprintf("Error: set_array_size(): info pointer in NULL\n");
1506 static int reshape_array(char *container
, int fd
, char *devname
,
1507 struct supertype
*st
, struct mdinfo
*info
,
1508 int force
, struct mddev_dev
*devlist
,
1509 unsigned long long data_offset
,
1510 char *backup_file
, int verbose
, int forked
,
1511 int restart
, int freeze_reshape
);
1512 static int reshape_container(char *container
, char *devname
,
1514 struct supertype
*st
,
1515 struct mdinfo
*info
,
1518 int verbose
, int restart
, int freeze_reshape
);
1520 int Grow_reshape(char *devname
, int fd
,
1521 struct mddev_dev
*devlist
,
1522 unsigned long long data_offset
,
1523 struct context
*c
, struct shape
*s
)
1525 /* Make some changes in the shape of an array.
1526 * The kernel must support the change.
1528 * There are three different changes. Each can trigger
1529 * a resync or recovery so we freeze that until we have
1530 * requested everything (if kernel supports freezing - 2.6.30).
1532 * - change size (i.e. component_size)
1534 * - change layout/chunksize/ndisks
1536 * The last can require a reshape. It is different on different
1537 * levels so we need to check the level before actioning it.
1538 * Some times the level change needs to be requested after the
1539 * reshape (e.g. raid6->raid5, raid5->raid0)
1542 struct mdu_array_info_s array
;
1544 struct supertype
*st
;
1545 char *subarray
= NULL
;
1549 char *container
= NULL
;
1552 struct mddev_dev
*dv
;
1558 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) < 0) {
1559 pr_err("%s is not an active md array - aborting\n",
1563 if (data_offset
!= INVALID_SECTORS
&& array
.level
!= 10
1564 && (array
.level
< 4 || array
.level
> 6)) {
1565 pr_err("--grow --data-offset not yet supported\n");
1570 (s
->chunk
|| s
->level
!= UnSet
|| s
->layout_str
|| s
->raiddisks
)) {
1571 pr_err("cannot change component size at the same time "
1572 "as other changes.\n"
1573 " Change size first, then check data is intact before "
1574 "making other changes.\n");
1578 if (s
->raiddisks
&& s
->raiddisks
< array
.raid_disks
&& array
.level
> 1 &&
1579 get_linux_version() < 2006032 &&
1580 !check_env("MDADM_FORCE_FEWER")) {
1581 pr_err("reducing the number of devices is not safe before Linux 2.6.32\n"
1582 " Please use a newer kernel\n");
1586 st
= super_by_fd(fd
, &subarray
);
1588 pr_err("Unable to determine metadata format for %s\n", devname
);
1591 if (s
->raiddisks
> st
->max_devs
) {
1592 pr_err("Cannot increase raid-disks on this array"
1593 " beyond %d\n", st
->max_devs
);
1597 /* in the external case we need to check that the requested reshape is
1598 * supported, and perform an initial check that the container holds the
1599 * pre-requisite spare devices (mdmon owns final validation)
1601 if (st
->ss
->external
) {
1605 container
= st
->container_devnm
;
1606 cfd
= open_dev_excl(st
->container_devnm
);
1608 container
= st
->devnm
;
1610 cfd
= open_dev_excl(st
->devnm
);
1614 pr_err("Unable to open container for %s\n",
1620 rv
= st
->ss
->load_container(st
, cfd
, NULL
);
1623 pr_err("Cannot read superblock for %s\n",
1629 /* check if operation is supported for metadata handler */
1630 if (st
->ss
->container_content
) {
1631 struct mdinfo
*cc
= NULL
;
1632 struct mdinfo
*content
= NULL
;
1634 cc
= st
->ss
->container_content(st
, subarray
);
1635 for (content
= cc
; content
; content
= content
->next
) {
1636 int allow_reshape
= 1;
1638 /* check if reshape is allowed based on metadata
1639 * indications stored in content.array.status
1641 if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
))
1643 if (content
->array
.state
1644 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
))
1646 if (!allow_reshape
) {
1647 pr_err("cannot reshape arrays in"
1648 " container with unsupported"
1649 " metadata: %s(%s)\n",
1650 devname
, container
);
1658 if (mdmon_running(container
))
1659 st
->update_tail
= &st
->updates
;
1663 for (dv
= devlist
; dv
; dv
= dv
->next
)
1665 if (s
->raiddisks
> array
.raid_disks
&&
1666 array
.spare_disks
+added_disks
< (s
->raiddisks
- array
.raid_disks
) &&
1668 pr_err("Need %d spare%s to avoid degraded array,"
1669 " and only have %d.\n"
1670 " Use --force to over-ride this check.\n",
1671 s
->raiddisks
- array
.raid_disks
,
1672 s
->raiddisks
- array
.raid_disks
== 1 ? "" : "s",
1673 array
.spare_disks
+ added_disks
);
1677 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
| GET_DISKS
| GET_DEVS
1678 | GET_STATE
| GET_VERSION
);
1680 if (st
->ss
->external
&& subarray
== NULL
) {
1681 array
.level
= LEVEL_CONTAINER
;
1682 sra
->array
.level
= LEVEL_CONTAINER
;
1685 pr_err("failed to read sysfs parameters for %s\n",
1689 frozen
= freeze(st
);
1691 /* freeze() already spewed the reason */
1694 } else if (frozen
< 0) {
1695 pr_err("%s is performing resync/recovery and cannot"
1696 " be reshaped\n", devname
);
1701 /* ========= set size =============== */
1702 if (s
->size
> 0 && (s
->size
== MAX_SIZE
|| s
->size
!= (unsigned)array
.size
)) {
1703 unsigned long long orig_size
= get_component_size(fd
)/2;
1704 unsigned long long min_csize
;
1706 int raid0_takeover
= 0;
1709 orig_size
= (unsigned) array
.size
;
1711 if (orig_size
== 0) {
1712 pr_err("Cannot set device size in this type of array.\n");
1717 if (reshape_super(st
, s
->size
, UnSet
, UnSet
, 0, 0, UnSet
, NULL
,
1718 devname
, APPLY_METADATA_CHANGES
, c
->verbose
> 0)) {
1723 if (st
->ss
->external
) {
1724 /* metadata can have size limitation
1725 * update size value according to metadata information
1727 struct mdinfo
*sizeinfo
=
1728 st
->ss
->container_content(st
, subarray
);
1730 unsigned long long new_size
=
1731 sizeinfo
->custom_array_size
/2;
1732 int data_disks
= get_data_disks(
1733 sizeinfo
->array
.level
,
1734 sizeinfo
->array
.layout
,
1735 sizeinfo
->array
.raid_disks
);
1736 new_size
/= data_disks
;
1737 dprintf("Metadata size correction from %llu to "
1738 "%llu (%llu)\n", orig_size
, new_size
,
1739 new_size
* data_disks
);
1741 sysfs_free(sizeinfo
);
1745 /* Update the size of each member device in case
1746 * they have been resized. This will never reduce
1747 * below the current used-size. The "size" attribute
1748 * understands '0' to mean 'max'.
1752 for (mdi
= sra
->devs
; mdi
; mdi
= mdi
->next
) {
1753 if (sysfs_set_num(sra
, mdi
, "size",
1754 s
->size
== MAX_SIZE
? 0 : s
->size
) < 0) {
1755 /* Probably kernel refusing to let us
1756 * reduce the size - not an error.
1760 if (array
.not_persistent
== 0 &&
1761 array
.major_version
== 0 &&
1762 get_linux_version() < 3001000) {
1763 /* Dangerous to allow size to exceed 2TB */
1764 unsigned long long csize
;
1765 if (sysfs_get_ll(sra
, mdi
, "size", &csize
) == 0) {
1766 if (csize
>= 2ULL*1024*1024*1024)
1767 csize
= 2ULL*1024*1024*1024;
1768 if ((min_csize
== 0 || (min_csize
1775 pr_err("Cannot set size on "
1776 "array members.\n");
1777 goto size_change_error
;
1779 if (min_csize
&& s
->size
> min_csize
) {
1780 pr_err("Cannot safely make this array "
1781 "use more than 2TB per device on this kernel.\n");
1783 goto size_change_error
;
1785 if (min_csize
&& s
->size
== MAX_SIZE
) {
1786 /* Don't let the kernel choose a size - it will get
1789 pr_err("Limited v0.90 array to "
1790 "2TB per device\n");
1791 s
->size
= min_csize
;
1793 if (st
->ss
->external
) {
1794 if (sra
->array
.level
== 0) {
1795 rv
= sysfs_set_str(sra
, NULL
, "level",
1799 /* get array parametes after takeover
1800 * to chane one parameter at time only
1802 rv
= ioctl(fd
, GET_ARRAY_INFO
, &array
);
1805 /* make sure mdmon is
1806 * aware of the new level */
1807 if (!mdmon_running(st
->container_devnm
))
1808 start_mdmon(st
->container_devnm
);
1809 ping_monitor(container
);
1810 if (mdmon_running(st
->container_devnm
) &&
1811 st
->update_tail
== NULL
)
1812 st
->update_tail
= &st
->updates
;
1815 if (s
->size
== MAX_SIZE
)
1817 array
.size
= s
->size
;
1818 if ((unsigned)array
.size
!= s
->size
) {
1819 /* got truncated to 32bit, write to
1820 * component_size instead
1823 rv
= sysfs_set_num(sra
, NULL
,
1824 "component_size", s
->size
);
1828 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
1830 /* manage array size when it is managed externally
1832 if ((rv
== 0) && st
->ss
->external
)
1833 rv
= set_array_size(st
, sra
, sra
->text_version
);
1836 if (raid0_takeover
) {
1837 /* do not recync non-existing parity,
1838 * we will drop it anyway
1840 sysfs_set_str(sra
, NULL
, "sync_action", "frozen");
1841 /* go back to raid0, drop parity disk
1843 sysfs_set_str(sra
, NULL
, "level", "raid0");
1844 ioctl(fd
, GET_ARRAY_INFO
, &array
);
1851 /* restore metadata */
1852 if (reshape_super(st
, orig_size
, UnSet
, UnSet
, 0, 0,
1853 UnSet
, NULL
, devname
,
1854 ROLLBACK_METADATA_CHANGES
,
1857 pr_err("Cannot set device size for %s: %s\n",
1858 devname
, strerror(err
));
1860 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1861 cont_err("Bitmap must be removed before size can be changed\n");
1865 if (s
->assume_clean
) {
1866 /* This will fail on kernels older than 3.0 unless
1867 * a backport has been arranged.
1870 sysfs_set_str(sra
, NULL
, "resync_start", "none") < 0)
1871 pr_err("--assume-clean not supported with --grow on this kernel\n");
1873 ioctl(fd
, GET_ARRAY_INFO
, &array
);
1874 s
->size
= get_component_size(fd
)/2;
1876 s
->size
= array
.size
;
1877 if (c
->verbose
>= 0) {
1878 if (s
->size
== orig_size
)
1879 pr_err("component size of %s "
1880 "unchanged at %lluK\n",
1883 pr_err("component size of %s "
1884 "has been set to %lluK\n",
1888 } else if (array
.level
!= LEVEL_CONTAINER
) {
1889 s
->size
= get_component_size(fd
)/2;
1891 s
->size
= array
.size
;
1894 /* See if there is anything else to do */
1895 if ((s
->level
== UnSet
|| s
->level
== array
.level
) &&
1896 (s
->layout_str
== NULL
) &&
1897 (s
->chunk
== 0 || s
->chunk
== array
.chunk_size
) &&
1898 data_offset
== INVALID_SECTORS
&&
1899 (s
->raiddisks
== 0 || s
->raiddisks
== array
.raid_disks
)) {
1900 /* Nothing more to do */
1901 if (!changed
&& c
->verbose
>= 0)
1902 pr_err("%s: no change requested\n",
1907 /* ========= check for Raid10/Raid1 -> Raid0 conversion ===============
1908 * current implementation assumes that following conditions must be met:
1911 * - near_copies == 2
1913 if ((s
->level
== 0 && array
.level
== 10 && sra
&&
1914 array
.layout
== ((1 << 8) + 2) && !(array
.raid_disks
& 1)) ||
1915 (s
->level
== 0 && array
.level
== 1 && sra
)) {
1917 err
= remove_disks_for_takeover(st
, sra
, array
.layout
);
1919 dprintf(Name
": Array cannot be reshaped\n");
1925 /* Make sure mdmon has seen the device removal
1926 * and updated metadata before we continue with
1930 ping_monitor(container
);
1933 memset(&info
, 0, sizeof(info
));
1935 sysfs_init(&info
, fd
, NULL
);
1936 strcpy(info
.text_version
, sra
->text_version
);
1937 info
.component_size
= s
->size
*2;
1938 info
.new_level
= s
->level
;
1939 info
.new_chunk
= s
->chunk
* 1024;
1940 if (info
.array
.level
== LEVEL_CONTAINER
) {
1941 info
.delta_disks
= UnSet
;
1942 info
.array
.raid_disks
= s
->raiddisks
;
1943 } else if (s
->raiddisks
)
1944 info
.delta_disks
= s
->raiddisks
- info
.array
.raid_disks
;
1946 info
.delta_disks
= UnSet
;
1947 if (s
->layout_str
== NULL
) {
1948 info
.new_layout
= UnSet
;
1949 if (info
.array
.level
== 6 &&
1950 (info
.new_level
== 6 || info
.new_level
== UnSet
) &&
1951 info
.array
.layout
>= 16) {
1952 pr_err("%s has a non-standard layout. If you"
1953 " wish to preserve this\n", devname
);
1954 cont_err("during the reshape, please specify"
1955 " --layout=preserve\n");
1956 cont_err("If you want to change it, specify a"
1957 " layout or use --layout=normalise\n");
1961 } else if (strcmp(s
->layout_str
, "normalise") == 0 ||
1962 strcmp(s
->layout_str
, "normalize") == 0) {
1963 /* If we have a -6 RAID6 layout, remove the '-6'. */
1964 info
.new_layout
= UnSet
;
1965 if (info
.array
.level
== 6 && info
.new_level
== UnSet
) {
1967 strcpy(l
, map_num(r6layout
, info
.array
.layout
));
1968 h
= strrchr(l
, '-');
1969 if (h
&& strcmp(h
, "-6") == 0) {
1971 info
.new_layout
= map_name(r6layout
, l
);
1974 pr_err("%s is only meaningful when reshaping"
1975 " a RAID6 array.\n", s
->layout_str
);
1979 } else if (strcmp(s
->layout_str
, "preserve") == 0) {
1980 /* This means that a non-standard RAID6 layout
1983 * - When reshape a RAID6 (e.g. adding a device)
1984 * which is in a non-standard layout, it is OK
1985 * to preserve that layout.
1986 * - When converting a RAID5 to RAID6, leave it in
1987 * the XXX-6 layout, don't re-layout.
1989 if (info
.array
.level
== 6 && info
.new_level
== UnSet
)
1990 info
.new_layout
= info
.array
.layout
;
1991 else if (info
.array
.level
== 5 && info
.new_level
== 6) {
1993 strcpy(l
, map_num(r5layout
, info
.array
.layout
));
1995 info
.new_layout
= map_name(r6layout
, l
);
1997 pr_err("%s in only meaningful when reshaping"
1998 " to RAID6\n", s
->layout_str
);
2003 int l
= info
.new_level
;
2005 l
= info
.array
.level
;
2008 info
.new_layout
= map_name(r5layout
, s
->layout_str
);
2011 info
.new_layout
= map_name(r6layout
, s
->layout_str
);
2014 info
.new_layout
= parse_layout_10(s
->layout_str
);
2017 info
.new_layout
= parse_layout_faulty(s
->layout_str
);
2020 pr_err("layout not meaningful"
2021 " with this level\n");
2025 if (info
.new_layout
== UnSet
) {
2026 pr_err("layout %s not understood"
2027 " for this level\n",
2034 if (array
.level
== LEVEL_FAULTY
) {
2035 if (s
->level
!= UnSet
&& s
->level
!= array
.level
) {
2036 pr_err("cannot change level of Faulty device\n");
2040 pr_err("cannot set chunksize of Faulty device\n");
2043 if (s
->raiddisks
&& s
->raiddisks
!= 1) {
2044 pr_err("cannot set raid_disks of Faulty device\n");
2047 if (s
->layout_str
) {
2048 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
2049 dprintf("Cannot get array information.\n");
2052 array
.layout
= info
.new_layout
;
2053 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2054 pr_err("failed to set new layout\n");
2056 } else if (c
->verbose
>= 0)
2057 printf("layout for %s set to %d\n",
2058 devname
, array
.layout
);
2060 } else if (array
.level
== LEVEL_CONTAINER
) {
2061 /* This change is to be applied to every array in the
2062 * container. This is only needed when the metadata imposes
2063 * restraints of the various arrays in the container.
2064 * Currently we only know that IMSM requires all arrays
2065 * to have the same number of devices so changing the
2066 * number of devices (On-Line Capacity Expansion) must be
2067 * performed at the level of the container
2069 rv
= reshape_container(container
, devname
, -1, st
, &info
,
2070 c
->force
, c
->backup_file
, c
->verbose
, 0, 0);
2073 /* get spare devices from external metadata
2075 if (st
->ss
->external
) {
2076 struct mdinfo
*info2
;
2078 info2
= st
->ss
->container_content(st
, subarray
);
2080 info
.array
.spare_disks
=
2081 info2
->array
.spare_disks
;
2086 /* Impose these changes on a single array. First
2087 * check that the metadata is OK with the change. */
2089 if (reshape_super(st
, 0, info
.new_level
,
2090 info
.new_layout
, info
.new_chunk
,
2091 info
.array
.raid_disks
, info
.delta_disks
,
2092 c
->backup_file
, devname
, APPLY_METADATA_CHANGES
,
2098 rv
= reshape_array(container
, fd
, devname
, st
, &info
, c
->force
,
2099 devlist
, data_offset
, c
->backup_file
, c
->verbose
,
2110 /* verify_reshape_position()
2111 * Function checks if reshape position in metadata is not farther
2112 * than position in md.
2114 * 0 : not valid sysfs entry
2115 * it can be caused by not started reshape, it should be started
2116 * by reshape array or raid0 array is before takeover
2117 * -1 : error, reshape position is obviously wrong
2118 * 1 : success, reshape progress correct or updated
2120 static int verify_reshape_position(struct mdinfo
*info
, int level
)
2126 /* read sync_max, failure can mean raid0 array */
2127 rv
= sysfs_get_str(info
, NULL
, "sync_max", buf
, 40);
2131 unsigned long long position
= strtoull(buf
, &ep
, 0);
2133 dprintf(Name
": Read sync_max sysfs entry is: %s\n", buf
);
2134 if (!(ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))) {
2135 position
*= get_data_disks(level
,
2137 info
->array
.raid_disks
);
2138 if (info
->reshape_progress
< position
) {
2139 dprintf("Corrected reshape progress (%llu) to "
2140 "md position (%llu)\n",
2141 info
->reshape_progress
, position
);
2142 info
->reshape_progress
= position
;
2144 } else if (info
->reshape_progress
> position
) {
2145 pr_err("Fatal error: array "
2146 "reshape was not properly frozen "
2147 "(expected reshape position is %llu, "
2148 "but reshape progress is %llu.\n",
2149 position
, info
->reshape_progress
);
2152 dprintf("Reshape position in md and metadata "
2157 } else if (rv
== 0) {
2158 /* for valid sysfs entry, 0-length content
2159 * should be indicated as error
2167 static unsigned long long choose_offset(unsigned long long lo
,
2168 unsigned long long hi
,
2169 unsigned long long min
,
2170 unsigned long long max
)
2172 /* Choose a new offset between hi and lo.
2173 * It must be between min and max, but
2174 * we would prefer something near the middle of hi/lo, and also
2175 * prefer to be aligned to a big power of 2.
2177 * So we start with the middle, then for each bit,
2178 * starting at '1' and increasing, if it is set, we either
2179 * add it or subtract it if possible, preferring the option
2180 * which is furthest from the boundary.
2182 * We stop once we get a 1MB alignment. As units are in sectors,
2183 * 1MB = 2*1024 sectors.
2185 unsigned long long choice
= (lo
+ hi
) / 2;
2186 unsigned long long bit
= 1;
2188 for (bit
= 1; bit
< 2*1024; bit
= bit
<< 1) {
2189 unsigned long long bigger
, smaller
;
2190 if (! (bit
& choice
))
2192 bigger
= choice
+ bit
;
2193 smaller
= choice
- bit
;
2194 if (bigger
> max
&& smaller
< min
)
2198 else if (smaller
< min
)
2200 else if (hi
- bigger
> smaller
- lo
)
2208 static int set_new_data_offset(struct mdinfo
*sra
, struct supertype
*st
,
2209 char *devname
, int delta_disks
,
2210 unsigned long long data_offset
,
2211 unsigned long long min
,
2217 unsigned long long before
, after
;
2219 /* Need to find min space before and after so same is used
2222 before
= UINT64_MAX
;
2224 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2228 struct supertype
*st2
;
2229 struct mdinfo info2
;
2231 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2233 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2234 dfd
= dev_open(dn
, O_RDONLY
);
2236 pr_err("%s: cannot open component %s\n",
2237 devname
, dn
? dn
: "-unknown-");
2240 st2
= dup_super(st
);
2241 rv
= st2
->ss
->load_super(st2
,dfd
, NULL
);
2245 pr_err("%s: cannot get superblock from %s\n",
2249 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
2250 st2
->ss
->free_super(st2
);
2252 if (info2
.space_before
== 0 &&
2253 info2
.space_after
== 0) {
2254 /* Metadata doesn't support data_offset changes */
2257 if (before
> info2
.space_before
)
2258 before
= info2
.space_before
;
2259 if (after
> info2
.space_after
)
2260 after
= info2
.space_after
;
2262 if (data_offset
!= INVALID_SECTORS
) {
2264 if (info2
.data_offset
== data_offset
) {
2265 pr_err("%s: already has that data_offset\n",
2269 if (data_offset
< info2
.data_offset
)
2273 } else if ((data_offset
<= info2
.data_offset
&& dir
== 1) ||
2274 (data_offset
>= info2
.data_offset
&& dir
== -1)) {
2275 pr_err("%s: differing data offsets on devices make this --data-offset setting impossible\n",
2281 if (before
== UINT64_MAX
)
2282 /* impossible really, there must be no devices */
2285 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2286 char *dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2287 unsigned long long new_data_offset
;
2289 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2291 if (delta_disks
< 0) {
2292 /* Don't need any space as array is shrinking
2293 * just move data_offset up by min
2295 if (data_offset
== INVALID_SECTORS
)
2296 new_data_offset
= sd
->data_offset
+ min
;
2298 if (data_offset
< sd
->data_offset
+ min
) {
2299 pr_err("--data-offset too small for %s\n",
2303 new_data_offset
= data_offset
;
2305 } else if (delta_disks
> 0) {
2306 /* need space before */
2310 pr_err("Insufficient head-space for reshape on %s\n",
2314 if (data_offset
== INVALID_SECTORS
)
2315 new_data_offset
= sd
->data_offset
- min
;
2317 if (data_offset
> sd
->data_offset
- min
) {
2318 pr_err("--data-offset too large for %s\n",
2322 new_data_offset
= data_offset
;
2326 /* can move up or down. If 'data_offset'
2327 * was set we would have already decided,
2328 * so just choose direction with most space.
2335 sysfs_set_str(sra
, NULL
, "reshape_direction",
2336 dir
== 1 ? "backwards" : "forwards");
2338 /* Increase data offset */
2342 pr_err("Insufficient tail-space for reshape on %s\n",
2346 if (data_offset
!= INVALID_SECTORS
&&
2347 data_offset
< sd
->data_offset
+ min
) {
2348 pr_err("--data-offset too small on %s\n",
2352 if (data_offset
!= INVALID_SECTORS
)
2353 new_data_offset
= data_offset
;
2355 new_data_offset
= choose_offset(sd
->data_offset
,
2356 sd
->data_offset
+ after
,
2357 sd
->data_offset
+ min
,
2358 sd
->data_offset
+ after
);
2360 /* Decrease data offset */
2364 pr_err("insufficient head-room on %s\n",
2368 if (data_offset
!= INVALID_SECTORS
&&
2369 data_offset
< sd
->data_offset
- min
) {
2370 pr_err("--data-offset too small on %s\n",
2374 if (data_offset
!= INVALID_SECTORS
)
2375 new_data_offset
= data_offset
;
2377 new_data_offset
= choose_offset(sd
->data_offset
- before
,
2379 sd
->data_offset
- before
,
2380 sd
->data_offset
- min
);
2383 err
= sysfs_set_num(sra
, sd
, "new_offset", new_data_offset
);
2384 if (err
< 0 && errno
== E2BIG
) {
2385 /* try again after increasing data size to max */
2386 err
= sysfs_set_num(sra
, sd
, "size", 0);
2387 if (err
< 0 && errno
== EINVAL
&&
2388 !(sd
->disk
.state
& (1<<MD_DISK_SYNC
))) {
2389 /* some kernels have a bug where you cannot
2390 * use '0' on spare devices. */
2391 sysfs_set_num(sra
, sd
, "size",
2392 (sra
->component_size
+ after
)/2);
2394 err
= sysfs_set_num(sra
, sd
, "new_offset",
2398 if (errno
== E2BIG
&& data_offset
!= INVALID_SECTORS
) {
2399 pr_err("data-offset is too big for %s\n",
2403 if (sd
== sra
->devs
&&
2404 (errno
== ENOENT
|| errno
== E2BIG
))
2405 /* Early kernel, no 'new_offset' file,
2406 * or kernel doesn't like us.
2407 * For RAID5/6 this is not fatal
2410 pr_err("Cannot set new_offset for %s\n",
2419 /* Just use a backup file */
2423 static int raid10_reshape(char *container
, int fd
, char *devname
,
2424 struct supertype
*st
, struct mdinfo
*info
,
2425 struct reshape
*reshape
,
2426 unsigned long long data_offset
,
2427 int force
, int verbose
)
2429 /* Changing raid_disks, layout, chunksize or possibly
2430 * just data_offset for a RAID10.
2431 * We must always change data_offset. We change by at least
2432 * ->min_offset_change which is the largest of the old and new
2434 * If raid_disks is increasing, then data_offset must decrease
2435 * by at least this copy size.
2436 * If raid_disks is unchanged, data_offset must increase or
2437 * decrease by at least min_offset_change but preferably by much more.
2438 * We choose half of the available space.
2439 * If raid_disks is decreasing, data_offset must increase by
2440 * at least min_offset_change. To allow of this, component_size
2441 * must be decreased by the same amount.
2443 * So we calculate the required minimum and direction, possibly
2444 * reduce the component_size, then iterate through the devices
2445 * and set the new_data_offset.
2446 * If that all works, we set chunk_size, layout, raid_disks, and start
2450 unsigned long long min
;
2453 sra
= sysfs_read(fd
, NULL
,
2454 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
2457 pr_err("%s: Cannot get array details from sysfs\n",
2461 min
= reshape
->min_offset_change
;
2463 if (info
->delta_disks
)
2464 sysfs_set_str(sra
, NULL
, "reshape_direction",
2465 info
->delta_disks
< 0 ? "backwards" : "forwards");
2466 if (info
->delta_disks
< 0 &&
2467 info
->space_after
< min
) {
2468 int rv
= sysfs_set_num(sra
, NULL
, "component_size",
2469 (sra
->component_size
-
2472 pr_err("cannot reduce component size\n");
2476 err
= set_new_data_offset(sra
, st
, devname
, info
->delta_disks
, data_offset
,
2479 pr_err("Cannot set new_data_offset: RAID10 reshape not\n");
2480 cont_err("supported on this kernel\n");
2486 if (!err
&& sysfs_set_num(sra
, NULL
, "chunk_size", info
->new_chunk
) < 0)
2488 if (!err
&& sysfs_set_num(sra
, NULL
, "layout", reshape
->after
.layout
) < 0)
2490 if (!err
&& sysfs_set_num(sra
, NULL
, "raid_disks",
2491 info
->array
.raid_disks
+ info
->delta_disks
) < 0)
2493 if (!err
&& sysfs_set_str(sra
, NULL
, "sync_action", "reshape") < 0)
2496 pr_err("Cannot set array shape for %s\n",
2499 (info
->array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2500 cont_err(" Bitmap must be removed before"
2501 " shape can be changed\n");
2511 static void get_space_after(int fd
, struct supertype
*st
, struct mdinfo
*info
)
2513 struct mdinfo
*sra
, *sd
;
2514 /* Initialisation to silence compiler warning */
2515 unsigned long long min_space_before
= 0, min_space_after
= 0;
2518 sra
= sysfs_read(fd
, NULL
, GET_DEVS
);
2521 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2524 struct supertype
*st2
;
2525 struct mdinfo info2
;
2527 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2529 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2530 dfd
= dev_open(dn
, O_RDONLY
);
2533 st2
= dup_super(st
);
2534 if (st2
->ss
->load_super(st2
,dfd
, NULL
)) {
2540 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
2541 st2
->ss
->free_super(st2
);
2544 min_space_before
> info2
.space_before
)
2545 min_space_before
= info2
.space_before
;
2547 min_space_after
> info2
.space_after
)
2548 min_space_after
= info2
.space_after
;
2551 if (sd
== NULL
&& !first
) {
2552 info
->space_after
= min_space_after
;
2553 info
->space_before
= min_space_before
;
2558 static void update_cache_size(char *container
, struct mdinfo
*sra
,
2559 struct mdinfo
*info
,
2560 int disks
, unsigned long long blocks
)
2562 /* Check that the internal stripe cache is
2563 * large enough, or it won't work.
2564 * It must hold at least 4 stripes of the larger
2567 unsigned long cache
;
2568 cache
= max(info
->array
.chunk_size
, info
->new_chunk
);
2569 cache
*= 4; /* 4 stripes minimum */
2570 cache
/= 512; /* convert to sectors */
2571 /* make sure there is room for 'blocks' with a bit to spare */
2572 if (cache
< 16 + blocks
/ disks
)
2573 cache
= 16 + blocks
/ disks
;
2574 cache
/= (4096/512); /* Covert from sectors to pages */
2576 if (sra
->cache_size
< cache
)
2577 subarray_set_num(container
, sra
, "stripe_cache_size",
2581 static int impose_reshape(struct mdinfo
*sra
,
2582 struct mdinfo
*info
,
2583 struct supertype
*st
,
2586 char *devname
, char *container
,
2587 struct reshape
*reshape
)
2589 struct mdu_array_info_s array
;
2591 sra
->new_chunk
= info
->new_chunk
;
2594 /* for external metadata checkpoint saved by mdmon can be lost
2595 * or missed /due to e.g. crash/. Check if md is not during
2596 * restart farther than metadata points to.
2597 * If so, this means metadata information is obsolete.
2599 if (st
->ss
->external
)
2600 verify_reshape_position(info
, reshape
->level
);
2601 sra
->reshape_progress
= info
->reshape_progress
;
2603 sra
->reshape_progress
= 0;
2604 if (reshape
->after
.data_disks
< reshape
->before
.data_disks
)
2605 /* start from the end of the new array */
2606 sra
->reshape_progress
= (sra
->component_size
2607 * reshape
->after
.data_disks
);
2610 ioctl(fd
, GET_ARRAY_INFO
, &array
);
2611 if (info
->array
.chunk_size
== info
->new_chunk
&&
2612 reshape
->before
.layout
== reshape
->after
.layout
&&
2613 st
->ss
->external
== 0) {
2614 /* use SET_ARRAY_INFO but only if reshape hasn't started */
2615 array
.raid_disks
= reshape
->after
.data_disks
+ reshape
->parity
;
2617 ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2620 pr_err("Cannot set device shape for %s: %s\n",
2621 devname
, strerror(errno
));
2624 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2625 cont_err("Bitmap must be removed before"
2626 " shape can be changed\n");
2630 } else if (!restart
) {
2631 /* set them all just in case some old 'new_*' value
2632 * persists from some earlier problem.
2635 if (sysfs_set_num(sra
, NULL
, "chunk_size", info
->new_chunk
) < 0)
2637 if (!err
&& sysfs_set_num(sra
, NULL
, "layout",
2638 reshape
->after
.layout
) < 0)
2640 if (!err
&& subarray_set_num(container
, sra
, "raid_disks",
2641 reshape
->after
.data_disks
+
2642 reshape
->parity
) < 0)
2645 pr_err("Cannot set device shape for %s\n",
2649 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2650 cont_err("Bitmap must be removed before"
2651 " shape can be changed\n");
2660 static int impose_level(int fd
, int level
, char *devname
, int verbose
)
2663 struct mdu_array_info_s array
;
2665 sysfs_init(&info
, fd
, NULL
);
2667 ioctl(fd
, GET_ARRAY_INFO
, &array
);
2669 (array
.level
>= 4 && array
.level
<= 6)) {
2670 /* To convert to RAID0 we need to fail and
2671 * remove any non-data devices. */
2674 int data_disks
= array
.raid_disks
- 1;
2675 if (array
.level
== 6)
2677 if (array
.level
== 5 &&
2678 array
.layout
!= ALGORITHM_PARITY_N
)
2680 if (array
.level
== 6 &&
2681 array
.layout
!= ALGORITHM_PARITY_N_6
)
2683 sysfs_set_str(&info
, NULL
,"sync_action", "idle");
2684 /* First remove any spares so no recovery starts */
2685 for (d
= 0, found
= 0;
2686 d
< MAX_DISKS
&& found
< array
.nr_disks
;
2688 mdu_disk_info_t disk
;
2690 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
2692 if (disk
.major
== 0 && disk
.minor
== 0)
2695 if ((disk
.state
& (1 << MD_DISK_ACTIVE
))
2696 && disk
.raid_disk
< data_disks
)
2699 ioctl(fd
, HOT_REMOVE_DISK
,
2700 makedev(disk
.major
, disk
.minor
));
2702 /* Now fail anything left */
2703 ioctl(fd
, GET_ARRAY_INFO
, &array
);
2704 for (d
= 0, found
= 0;
2705 d
< MAX_DISKS
&& found
< array
.nr_disks
;
2708 mdu_disk_info_t disk
;
2710 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
2712 if (disk
.major
== 0 && disk
.minor
== 0)
2715 if ((disk
.state
& (1 << MD_DISK_ACTIVE
))
2716 && disk
.raid_disk
< data_disks
)
2719 ioctl(fd
, SET_DISK_FAULTY
,
2720 makedev(disk
.major
, disk
.minor
));
2722 while (ioctl(fd
, HOT_REMOVE_DISK
,
2723 makedev(disk
.major
, disk
.minor
)) < 0
2730 c
= map_num(pers
, level
);
2732 int err
= sysfs_set_str(&info
, NULL
, "level", c
);
2735 pr_err("%s: could not set level to %s\n",
2738 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2739 cont_err("Bitmap must be removed"
2740 " before level can be changed\n");
2744 pr_err("level of %s changed to %s\n",
2751 static void catch_term(int sig
)
2756 static int reshape_array(char *container
, int fd
, char *devname
,
2757 struct supertype
*st
, struct mdinfo
*info
,
2758 int force
, struct mddev_dev
*devlist
,
2759 unsigned long long data_offset
,
2760 char *backup_file
, int verbose
, int forked
,
2761 int restart
, int freeze_reshape
)
2763 struct reshape reshape
;
2766 int orig_level
= UnSet
;
2770 struct mdu_array_info_s array
;
2773 struct mddev_dev
*dv
;
2777 unsigned long long *offsets
= NULL
;
2781 unsigned long blocks
;
2782 unsigned long long array_size
;
2784 struct mdinfo
*sra
= NULL
;
2786 /* when reshaping a RAID0, the component_size might be zero.
2787 * So try to fix that up.
2789 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
2790 dprintf("Cannot get array information.\n");
2793 if (array
.level
== 0 && info
->component_size
== 0) {
2794 get_dev_size(fd
, NULL
, &array_size
);
2795 info
->component_size
= array_size
/ array
.raid_disks
;
2798 if (array
.level
== 10)
2799 /* Need space_after info */
2800 get_space_after(fd
, st
, info
);
2802 if (info
->reshape_active
) {
2803 int new_level
= info
->new_level
;
2804 info
->new_level
= UnSet
;
2805 if (info
->delta_disks
> 0)
2806 info
->array
.raid_disks
-= info
->delta_disks
;
2807 msg
= analyse_change(devname
, info
, &reshape
);
2808 info
->new_level
= new_level
;
2809 if (info
->delta_disks
> 0)
2810 info
->array
.raid_disks
+= info
->delta_disks
;
2812 /* Make sure the array isn't read-only */
2813 ioctl(fd
, RESTART_ARRAY_RW
, 0);
2815 msg
= analyse_change(devname
, info
, &reshape
);
2817 /* if msg == "", error has already been printed */
2819 pr_err("%s\n", msg
);
2823 (reshape
.level
!= info
->array
.level
||
2824 reshape
.before
.layout
!= info
->array
.layout
||
2825 reshape
.before
.data_disks
+ reshape
.parity
2826 != info
->array
.raid_disks
- max(0, info
->delta_disks
))) {
2827 pr_err("reshape info is not in native format -"
2828 " cannot continue.\n");
2832 if (st
->ss
->external
&& restart
&& (info
->reshape_progress
== 0)) {
2833 /* When reshape is restarted from '0', very begin of array
2834 * it is possible that for external metadata reshape and array
2835 * configuration doesn't happen.
2836 * Check if md has the same opinion, and reshape is restarted
2837 * from 0. If so, this is regular reshape start after reshape
2838 * switch in metadata to next array only.
2840 if ((verify_reshape_position(info
, reshape
.level
) >= 0) &&
2841 (info
->reshape_progress
== 0))
2845 /* reshape already started. just skip to monitoring the reshape */
2846 if (reshape
.backup_blocks
== 0)
2848 if (restart
& RESHAPE_NO_BACKUP
)
2851 /* Need 'sra' down at 'started:' */
2852 sra
= sysfs_read(fd
, NULL
,
2853 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
|
2856 pr_err("%s: Cannot get array details from sysfs\n",
2862 backup_file
= locate_backup(sra
->sys_name
);
2866 /* The container is frozen but the array may not be.
2867 * So freeze the array so spares don't get put to the wrong use
2868 * FIXME there should probably be a cleaner separation between
2869 * freeze_array and freeze_container.
2871 sysfs_freeze_array(info
);
2872 /* Check we have enough spares to not be degraded */
2874 for (dv
= devlist
; dv
; dv
=dv
->next
)
2876 spares_needed
= max(reshape
.before
.data_disks
,
2877 reshape
.after
.data_disks
)
2878 + reshape
.parity
- array
.raid_disks
;
2881 info
->new_level
> 1 && info
->array
.level
> 1 &&
2882 spares_needed
> info
->array
.spare_disks
+ added_disks
) {
2883 pr_err("Need %d spare%s to avoid degraded array,"
2884 " and only have %d.\n"
2885 " Use --force to over-ride this check.\n",
2887 spares_needed
== 1 ? "" : "s",
2888 info
->array
.spare_disks
+ added_disks
);
2891 /* Check we have enough spares to not fail */
2892 spares_needed
= max(reshape
.before
.data_disks
,
2893 reshape
.after
.data_disks
)
2895 if ((info
->new_level
> 1 || info
->new_level
== 0) &&
2896 spares_needed
> info
->array
.spare_disks
+added_disks
) {
2897 pr_err("Need %d spare%s to create working array,"
2898 " and only have %d.\n",
2900 spares_needed
== 1 ? "" : "s",
2901 info
->array
.spare_disks
+ added_disks
);
2905 if (reshape
.level
!= array
.level
) {
2906 int err
= impose_level(fd
, reshape
.level
, devname
, verbose
);
2909 info
->new_layout
= UnSet
; /* after level change,
2910 * layout is meaningless */
2911 orig_level
= array
.level
;
2912 sysfs_freeze_array(info
);
2914 if (reshape
.level
> 0 && st
->ss
->external
) {
2915 /* make sure mdmon is aware of the new level */
2916 if (mdmon_running(container
))
2917 flush_mdmon(container
);
2919 if (!mdmon_running(container
))
2920 start_mdmon(container
);
2921 ping_monitor(container
);
2922 if (mdmon_running(container
) &&
2923 st
->update_tail
== NULL
)
2924 st
->update_tail
= &st
->updates
;
2927 /* ->reshape_super might have chosen some spares from the
2928 * container that it wants to be part of the new array.
2929 * We can collect them with ->container_content and give
2930 * them to the kernel.
2932 if (st
->ss
->reshape_super
&& st
->ss
->container_content
) {
2933 char *subarray
= strchr(info
->text_version
+1, '/')+1;
2934 struct mdinfo
*info2
=
2935 st
->ss
->container_content(st
, subarray
);
2939 sysfs_init(info2
, fd
, st
->devnm
);
2940 /* When increasing number of devices, we need to set
2941 * new raid_disks before adding these, or they might
2944 if (reshape
.backup_blocks
&&
2945 reshape
.after
.data_disks
> reshape
.before
.data_disks
)
2946 subarray_set_num(container
, info2
, "raid_disks",
2947 reshape
.after
.data_disks
+
2949 for (d
= info2
->devs
; d
; d
= d
->next
) {
2950 if (d
->disk
.state
== 0 &&
2951 d
->disk
.raid_disk
>= 0) {
2952 /* This is a spare that wants to
2953 * be part of the array.
2955 add_disk(fd
, st
, info2
, d
);
2961 /* We might have been given some devices to add to the
2962 * array. Now that the array has been changed to the right
2963 * level and frozen, we can safely add them.
2966 Manage_subdevs(devname
, fd
, devlist
, verbose
,
2969 if (reshape
.backup_blocks
== 0 && data_offset
!= INVALID_SECTORS
)
2970 reshape
.backup_blocks
= reshape
.before
.data_disks
* info
->array
.chunk_size
/512;
2971 if (reshape
.backup_blocks
== 0) {
2972 /* No restriping needed, but we might need to impose
2973 * some more changes: layout, raid_disks, chunk_size
2975 /* read current array info */
2976 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
2977 dprintf("Cannot get array information.\n");
2980 /* compare current array info with new values and if
2981 * it is different update them to new */
2982 if (info
->new_layout
!= UnSet
&&
2983 info
->new_layout
!= array
.layout
) {
2984 array
.layout
= info
->new_layout
;
2985 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2986 pr_err("failed to set new layout\n");
2988 } else if (verbose
>= 0)
2989 printf("layout for %s set to %d\n",
2990 devname
, array
.layout
);
2992 if (info
->delta_disks
!= UnSet
&&
2993 info
->delta_disks
!= 0 &&
2994 array
.raid_disks
!= (info
->array
.raid_disks
+ info
->delta_disks
)) {
2995 array
.raid_disks
+= info
->delta_disks
;
2996 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2997 pr_err("failed to set raid disks\n");
2999 } else if (verbose
>= 0) {
3000 printf("raid_disks for %s set to %d\n",
3001 devname
, array
.raid_disks
);
3004 if (info
->new_chunk
!= 0 &&
3005 info
->new_chunk
!= array
.chunk_size
) {
3006 if (sysfs_set_num(info
, NULL
,
3007 "chunk_size", info
->new_chunk
) != 0) {
3008 pr_err("failed to set chunk size\n");
3010 } else if (verbose
>= 0)
3011 printf("chunk size for %s set to %d\n",
3012 devname
, array
.chunk_size
);
3019 * There are three possibilities.
3020 * 1/ The array will shrink.
3021 * We need to ensure the reshape will pause before reaching
3022 * the 'critical section'. We also need to fork and wait for
3023 * that to happen. When it does we
3024 * suspend/backup/complete/unfreeze
3026 * 2/ The array will not change size.
3027 * This requires that we keep a backup of a sliding window
3028 * so that we can restore data after a crash. So we need
3029 * to fork and monitor progress.
3030 * In future we will allow the data_offset to change, so
3031 * a sliding backup becomes unnecessary.
3033 * 3/ The array will grow. This is relatively easy.
3034 * However the kernel's restripe routines will cheerfully
3035 * overwrite some early data before it is safe. So we
3036 * need to make a backup of the early parts of the array
3037 * and be ready to restore it if rebuild aborts very early.
3038 * For externally managed metadata, we still need a forked
3039 * child to monitor the reshape and suspend IO over the region
3040 * that is being reshaped.
3042 * We backup data by writing it to one spare, or to a
3043 * file which was given on command line.
3045 * In each case, we first make sure that storage is available
3046 * for the required backup.
3048 * - request the shape change.
3049 * - fork to handle backup etc.
3051 /* Check that we can hold all the data */
3052 get_dev_size(fd
, NULL
, &array_size
);
3053 if (reshape
.new_size
< (array_size
/512)) {
3054 pr_err("this change will reduce the size of the array.\n"
3055 " use --grow --array-size first to truncate array.\n"
3056 " e.g. mdadm --grow %s --array-size %llu\n",
3057 devname
, reshape
.new_size
/2);
3061 if (array
.level
== 10) {
3062 /* Reshaping RAID10 does not require any data backup by
3063 * user-space. Instead it requires that the data_offset
3064 * is changed to avoid the need for backup.
3065 * So this is handled very separately
3068 /* Nothing to do. */
3070 return raid10_reshape(container
, fd
, devname
, st
, info
,
3071 &reshape
, data_offset
,
3074 sra
= sysfs_read(fd
, NULL
,
3075 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
|
3078 pr_err("%s: Cannot get array details from sysfs\n",
3084 switch(set_new_data_offset(sra
, st
, devname
,
3085 reshape
.after
.data_disks
- reshape
.before
.data_disks
,
3087 reshape
.min_offset_change
, 1)) {
3091 /* Updated data_offset, so it's easy now */
3092 update_cache_size(container
, sra
, info
,
3093 min(reshape
.before
.data_disks
,
3094 reshape
.after
.data_disks
),
3095 reshape
.backup_blocks
);
3097 /* Right, everything seems fine. Let's kick things off.
3101 if (impose_reshape(sra
, info
, st
, fd
, restart
,
3102 devname
, container
, &reshape
) < 0)
3104 if (sysfs_set_str(sra
, NULL
, "sync_action", "reshape") < 0) {
3105 pr_err("Failed to initiate reshape!\n");
3108 if (info
->new_level
== reshape
.level
)
3110 /* need to adjust level when reshape completes */
3112 case -1: /* ignore error, but don't wait */
3114 default: /* parent */
3122 fd
= open_dev(sra
->sys_name
);
3124 impose_level(fd
, info
->new_level
, devname
, verbose
);
3126 case 1: /* Couldn't set data_offset, try the old way */
3127 if (data_offset
!= INVALID_SECTORS
) {
3128 pr_err("Cannot update data_offset on this array\n");
3135 /* Decide how many blocks (sectors) for a reshape
3136 * unit. The number we have so far is just a minimum
3138 blocks
= reshape
.backup_blocks
;
3139 if (reshape
.before
.data_disks
==
3140 reshape
.after
.data_disks
) {
3141 /* Make 'blocks' bigger for better throughput, but
3142 * not so big that we reject it below.
3143 * Try for 16 megabytes
3145 while (blocks
* 32 < sra
->component_size
&&
3149 pr_err("Need to backup %luK of critical "
3150 "section..\n", blocks
/2);
3152 if (blocks
>= sra
->component_size
/2) {
3153 pr_err("%s: Something wrong"
3154 " - reshape aborted\n",
3159 /* Now we need to open all these devices so we can read/write.
3161 nrdisks
= max(reshape
.before
.data_disks
,
3162 reshape
.after
.data_disks
) + reshape
.parity
3163 + sra
->array
.spare_disks
;
3164 fdlist
= xcalloc((1+nrdisks
), sizeof(int));
3165 offsets
= xcalloc((1+nrdisks
), sizeof(offsets
[0]));
3167 odisks
= reshape
.before
.data_disks
+ reshape
.parity
;
3168 d
= reshape_prepare_fdlist(devname
, sra
, odisks
,
3169 nrdisks
, blocks
, backup_file
,
3174 if ((st
->ss
->manage_reshape
== NULL
) ||
3175 (st
->ss
->recover_backup
== NULL
)) {
3176 if (backup_file
== NULL
) {
3177 if (reshape
.after
.data_disks
<=
3178 reshape
.before
.data_disks
) {
3179 pr_err("%s: Cannot grow - need backup-file\n",
3181 pr_err(" Please provide one with \"--backup=...\"\n");
3183 } else if (sra
->array
.spare_disks
== 0) {
3184 pr_err("%s: Cannot grow - "
3185 "need a spare or backup-file to backup "
3186 "critical section\n", devname
);
3190 if (!reshape_open_backup_file(backup_file
, fd
, devname
,
3192 fdlist
+d
, offsets
+d
,
3201 update_cache_size(container
, sra
, info
,
3202 min(reshape
.before
.data_disks
, reshape
.after
.data_disks
),
3205 /* Right, everything seems fine. Let's kick things off.
3206 * If only changing raid_disks, use ioctl, else use
3211 if (impose_reshape(sra
, info
, st
, fd
, restart
,
3212 devname
, container
, &reshape
) < 0)
3215 err
= start_reshape(sra
, restart
, reshape
.before
.data_disks
,
3216 reshape
.after
.data_disks
);
3218 pr_err("Cannot %s reshape for %s\n",
3219 restart
? "continue" : "start",
3224 sysfs_set_str(sra
, NULL
, "array_state", "active");
3225 if (freeze_reshape
) {
3229 pr_err("Reshape has to be continued from"
3230 " location %llu when root filesystem has been mounted.\n",
3231 sra
->reshape_progress
);
3235 if (!forked
&& !check_env("MDADM_NO_SYSTEMCTL")) {
3236 int skipped
, i
, pid
, status
;
3239 /* In a systemd/udev world, it is best to get systemd to
3240 * run "mdadm --grow --continue" rather than running in the
3246 devnm
= sra
->sys_name
;
3249 /* FIXME yuk. CLOSE_EXEC?? */
3251 for (i
= 3; skipped
< 20; i
++)
3257 /* Don't want to see error messages from
3258 * systemctl. If the service doesn't exist,
3259 * we fork ourselves.
3262 open("/dev/null", O_WRONLY
);
3263 snprintf(pathbuf
, sizeof(pathbuf
), "mdadm-grow-continue@%s.service",
3265 status
= execl("/usr/bin/systemctl", "systemctl",
3268 status
= execl("/bin/systemctl", "systemctl", "start",
3271 case -1: /* Just do it ourselves. */
3273 default: /* parent - good */
3274 pid
= wait(&status
);
3275 if (pid
>= 0 && status
== 0) {
3284 /* Now we just need to kick off the reshape and watch, while
3285 * handling backups of the data...
3286 * This is all done by a forked background process.
3288 switch(forked
? 0 : fork()) {
3290 pr_err("Cannot run child to monitor reshape: %s\n",
3304 /* If another array on the same devices is busy, the
3305 * reshape will wait for them. This would mean that
3306 * the first section that we suspend will stay suspended
3307 * for a long time. So check on that possibility
3308 * by looking for "DELAYED" in /proc/mdstat, and if found,
3312 struct mdstat_ent
*mds
, *m
;
3314 mds
= mdstat_read(1, 0);
3315 for (m
= mds
; m
; m
= m
->next
)
3316 if (strcmp(m
->devnm
, sra
->sys_name
) == 0) {
3318 m
->percent
== RESYNC_DELAYED
)
3321 /* Haven't started the reshape thread
3328 if (delayed
== 1 && get_linux_version() < 3007000) {
3329 pr_err("Reshape is delayed, but cannot wait carefully with this kernel.\n"
3330 " You might experience problems until other reshapes complete.\n");
3334 mdstat_wait(30 - (delayed
-1) * 25);
3338 if (check_env("MDADM_GROW_VERIFY"))
3339 fd
= open(devname
, O_RDONLY
| O_DIRECT
);
3342 mlockall(MCL_FUTURE
);
3344 signal(SIGTERM
, catch_term
);
3346 if (st
->ss
->external
) {
3347 /* metadata handler takes it from here */
3348 done
= st
->ss
->manage_reshape(
3349 fd
, sra
, &reshape
, st
, blocks
,
3351 d
- odisks
, fdlist
+odisks
,
3354 done
= child_monitor(
3355 fd
, sra
, &reshape
, st
, blocks
,
3357 d
- odisks
, fdlist
+odisks
,
3363 if (backup_file
&& done
) {
3365 bul
= make_backup(sra
->sys_name
);
3368 int l
= readlink(bul
, buf
, sizeof(buf
));
3376 unlink(backup_file
);
3383 if (!st
->ss
->external
&&
3384 !(reshape
.before
.data_disks
!= reshape
.after
.data_disks
3385 && info
->custom_array_size
) &&
3386 info
->new_level
== reshape
.level
&&
3388 /* no need to wait for the reshape to finish as
3389 * there is nothing more to do.
3396 if (st
->ss
->external
) {
3397 /* Re-load the metadata as much could have changed */
3398 int cfd
= open_dev(st
->container_devnm
);
3400 flush_mdmon(container
);
3401 st
->ss
->free_super(st
);
3402 st
->ss
->load_container(st
, cfd
, container
);
3407 /* set new array size if required customer_array_size is used
3410 if (reshape
.before
.data_disks
!=
3411 reshape
.after
.data_disks
&&
3412 info
->custom_array_size
)
3413 set_array_size(st
, info
, info
->text_version
);
3415 if (info
->new_level
!= reshape
.level
) {
3417 fd
= open(devname
, O_RDONLY
);
3418 impose_level(fd
, info
->new_level
, devname
, verbose
);
3420 if (info
->new_level
== 0)
3421 st
->update_tail
= NULL
;
3433 if (orig_level
!= UnSet
&& sra
) {
3434 c
= map_num(pers
, orig_level
);
3435 if (c
&& sysfs_set_str(sra
, NULL
, "level", c
) == 0)
3436 pr_err("aborting level change\n");
3444 /* mdfd handle is passed to be closed in child process (after fork).
3446 int reshape_container(char *container
, char *devname
,
3448 struct supertype
*st
,
3449 struct mdinfo
*info
,
3452 int verbose
, int restart
, int freeze_reshape
)
3454 struct mdinfo
*cc
= NULL
;
3456 char last_devnm
[32] = "";
3458 /* component_size is not meaningful for a container,
3459 * so pass '0' meaning 'no change'
3462 reshape_super(st
, 0, info
->new_level
,
3463 info
->new_layout
, info
->new_chunk
,
3464 info
->array
.raid_disks
, info
->delta_disks
,
3465 backup_file
, devname
, APPLY_METADATA_CHANGES
,
3473 /* ping monitor to be sure that update is on disk
3475 ping_monitor(container
);
3478 case -1: /* error */
3479 perror("Cannot fork to complete reshape\n");
3482 default: /* parent */
3483 if (!freeze_reshape
)
3484 printf(Name
": multi-array reshape continues"
3485 " in background\n");
3492 /* close unused handle in child process
3498 /* For each member array with reshape_active,
3499 * we need to perform the reshape.
3500 * We pick the first array that needs reshaping and
3501 * reshape it. reshape_array() will re-read the metadata
3502 * so the next time through a different array should be
3503 * ready for reshape.
3504 * It is possible that the 'different' array will not
3505 * be assembled yet. In that case we simple exit.
3506 * When it is assembled, the mdadm which assembles it
3507 * will take over the reshape.
3509 struct mdinfo
*content
;
3511 struct mdstat_ent
*mdstat
;
3517 cc
= st
->ss
->container_content(st
, NULL
);
3519 for (content
= cc
; content
; content
= content
->next
) {
3521 if (!content
->reshape_active
)
3524 subarray
= strchr(content
->text_version
+1, '/')+1;
3525 mdstat
= mdstat_by_subdev(subarray
, container
);
3528 if (mdstat
->active
== 0) {
3529 pr_err("Skipping inactive array %s.\n",
3531 free_mdstat(mdstat
);
3540 devid
= devnm2devid(mdstat
->devnm
);
3541 adev
= map_dev(major(devid
), minor(devid
), 0);
3543 adev
= content
->text_version
;
3545 fd
= open_dev(mdstat
->devnm
);
3547 printf(Name
": Device %s cannot be opened for reshape.",
3552 if (strcmp(last_devnm
, mdstat
->devnm
) == 0) {
3553 /* Do not allow for multiple reshape_array() calls for
3555 * It can happen when reshape_array() returns without
3556 * error, when reshape is not finished (wrong reshape
3557 * starting/continuation conditions). Mdmon doesn't
3558 * switch to next array in container and reentry
3559 * conditions for the same array occur.
3560 * This is possibly interim until the behaviour of
3561 * reshape_array is resolved().
3563 printf(Name
": Multiple reshape execution detected for "
3564 "device %s.", adev
);
3568 strcpy(last_devnm
, mdstat
->devnm
);
3570 sysfs_init(content
, fd
, mdstat
->devnm
);
3572 if (mdmon_running(container
))
3573 flush_mdmon(container
);
3575 rv
= reshape_array(container
, fd
, adev
, st
,
3576 content
, force
, NULL
, INVALID_SECTORS
,
3577 backup_file
, verbose
, 1, restart
,
3581 if (freeze_reshape
) {
3590 if (mdmon_running(container
))
3591 flush_mdmon(container
);
3600 * We run a child process in the background which performs the following
3602 * - wait for resync to reach a certain point
3603 * - suspend io to the following section
3604 * - backup that section
3605 * - allow resync to proceed further
3607 * - discard the backup.
3609 * When are combined in slightly different ways in the three cases.
3611 * - suspend/backup/allow/wait/resume/discard
3613 * - allow/wait/suspend/backup/allow/wait/resume/discard
3615 * - wait/resume/discard/suspend/backup/allow
3617 * suspend/backup/allow always come together
3618 * wait/resume/discard do too.
3619 * For the same-size case we have two backups to improve flow.
3623 int progress_reshape(struct mdinfo
*info
, struct reshape
*reshape
,
3624 unsigned long long backup_point
,
3625 unsigned long long wait_point
,
3626 unsigned long long *suspend_point
,
3627 unsigned long long *reshape_completed
, int *frozen
)
3629 /* This function is called repeatedly by the reshape manager.
3630 * It determines how much progress can safely be made and allows
3632 * - 'info' identifies the array and particularly records in
3633 * ->reshape_progress the metadata's knowledge of progress
3634 * This is a sector offset from the start of the array
3635 * of the next array block to be relocated. This number
3636 * may increase from 0 or decrease from array_size, depending
3637 * on the type of reshape that is happening.
3638 * Note that in contrast, 'sync_completed' is a block count of the
3639 * reshape so far. It gives the distance between the start point
3640 * (head or tail of device) and the next place that data will be
3641 * written. It always increases.
3642 * - 'reshape' is the structure created by analyse_change
3643 * - 'backup_point' shows how much the metadata manager has backed-up
3644 * data. For reshapes with increasing progress, it is the next address
3645 * to be backed up, previous addresses have been backed-up. For
3646 * decreasing progress, it is the earliest address that has been
3647 * backed up - later address are also backed up.
3648 * So addresses between reshape_progress and backup_point are
3649 * backed up providing those are in the 'correct' order.
3650 * - 'wait_point' is an array address. When reshape_completed
3651 * passes this point, progress_reshape should return. It might
3652 * return earlier if it determines that ->reshape_progress needs
3653 * to be updated or further backup is needed.
3654 * - suspend_point is maintained by progress_reshape and the caller
3655 * should not touch it except to initialise to zero.
3656 * It is an array address and it only increases in 2.6.37 and earlier.
3657 * This makes it difficult to handle reducing reshapes with
3658 * external metadata.
3659 * However: it is similar to backup_point in that it records the
3660 * other end of a suspended region from reshape_progress.
3661 * it is moved to extend the region that is safe to backup and/or
3663 * - reshape_completed is read from sysfs and returned. The caller
3664 * should copy this into ->reshape_progress when it has reason to
3665 * believe that the metadata knows this, and any backup outside this
3669 * 1 if more data from backup_point - but only as far as suspend_point,
3670 * should be backed up
3671 * 0 if things are progressing smoothly
3672 * -1 if the reshape is finished because it is all done,
3673 * -2 if the reshape is finished due to an error.
3676 int advancing
= (reshape
->after
.data_disks
3677 >= reshape
->before
.data_disks
);
3678 unsigned long long need_backup
; /* All data between start of array and
3679 * here will at some point need to
3682 unsigned long long read_offset
, write_offset
;
3683 unsigned long long write_range
;
3684 unsigned long long max_progress
, target
, completed
;
3685 unsigned long long array_size
= (info
->component_size
3686 * reshape
->before
.data_disks
);
3690 /* First, we unsuspend any region that is now known to be safe.
3691 * If suspend_point is on the 'wrong' side of reshape_progress, then
3692 * we don't have or need suspension at the moment. This is true for
3693 * native metadata when we don't need to back-up.
3696 if (info
->reshape_progress
<= *suspend_point
)
3697 sysfs_set_num(info
, NULL
, "suspend_lo",
3698 info
->reshape_progress
);
3700 /* Note: this won't work in 2.6.37 and before.
3701 * Something somewhere should make sure we don't need it!
3703 if (info
->reshape_progress
>= *suspend_point
)
3704 sysfs_set_num(info
, NULL
, "suspend_hi",
3705 info
->reshape_progress
);
3708 /* Now work out how far it is safe to progress.
3709 * If the read_offset for ->reshape_progress is less than
3710 * 'blocks' beyond the write_offset, we can only progress as far
3712 * Otherwise we can progress until the write_offset for the new location
3713 * reaches (within 'blocks' of) the read_offset at the current location.
3714 * However that region must be suspended unless we are using native
3716 * If we need to suspend more, we limit it to 128M per device, which is
3717 * rather arbitrary and should be some time-based calculation.
3719 read_offset
= info
->reshape_progress
/ reshape
->before
.data_disks
;
3720 write_offset
= info
->reshape_progress
/ reshape
->after
.data_disks
;
3721 write_range
= info
->new_chunk
/512;
3722 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
3723 need_backup
= array_size
;
3725 need_backup
= reshape
->backup_blocks
;
3727 if (read_offset
< write_offset
+ write_range
)
3728 max_progress
= backup_point
;
3732 reshape
->after
.data_disks
;
3734 if (read_offset
> write_offset
- write_range
)
3735 /* Can only progress as far as has been backed up,
3736 * which must be suspended */
3737 max_progress
= backup_point
;
3738 else if (info
->reshape_progress
<= need_backup
)
3739 max_progress
= backup_point
;
3741 if (info
->array
.major_version
>= 0)
3742 /* Can progress until backup is needed */
3743 max_progress
= need_backup
;
3745 /* Can progress until metadata update is required */
3748 reshape
->after
.data_disks
;
3749 /* but data must be suspended */
3750 if (max_progress
< *suspend_point
)
3751 max_progress
= *suspend_point
;
3756 /* We know it is safe to progress to 'max_progress' providing
3757 * it is suspended or we are using native metadata.
3758 * Consider extending suspend_point 128M per device if it
3759 * is less than 64M per device beyond reshape_progress.
3760 * But always do a multiple of 'blocks'
3761 * FIXME this is too big - it takes to long to complete
3764 target
= 64*1024*2 * min(reshape
->before
.data_disks
,
3765 reshape
->after
.data_disks
);
3766 target
/= reshape
->backup_blocks
;
3769 target
*= reshape
->backup_blocks
;
3771 /* For externally managed metadata we always need to suspend IO to
3772 * the area being reshaped so we regularly push suspend_point forward.
3773 * For native metadata we only need the suspend if we are going to do
3777 if ((need_backup
> info
->reshape_progress
3778 || info
->array
.major_version
< 0) &&
3779 *suspend_point
< info
->reshape_progress
+ target
) {
3780 if (need_backup
< *suspend_point
+ 2 * target
)
3781 *suspend_point
= need_backup
;
3782 else if (*suspend_point
+ 2 * target
< array_size
)
3783 *suspend_point
+= 2 * target
;
3785 *suspend_point
= array_size
;
3786 sysfs_set_num(info
, NULL
, "suspend_hi", *suspend_point
);
3787 if (max_progress
> *suspend_point
)
3788 max_progress
= *suspend_point
;
3791 if (info
->array
.major_version
>= 0) {
3792 /* Only need to suspend when about to backup */
3793 if (info
->reshape_progress
< need_backup
* 2 &&
3794 *suspend_point
> 0) {
3796 sysfs_set_num(info
, NULL
, "suspend_lo", 0);
3797 sysfs_set_num(info
, NULL
, "suspend_hi", need_backup
);
3800 /* Need to suspend continually */
3801 if (info
->reshape_progress
< *suspend_point
)
3802 *suspend_point
= info
->reshape_progress
;
3803 if (*suspend_point
+ target
< info
->reshape_progress
)
3804 /* No need to move suspend region yet */;
3806 if (*suspend_point
>= 2 * target
)
3807 *suspend_point
-= 2 * target
;
3810 sysfs_set_num(info
, NULL
, "suspend_lo",
3813 if (max_progress
< *suspend_point
)
3814 max_progress
= *suspend_point
;
3818 /* now set sync_max to allow that progress. sync_max, like
3819 * sync_completed is a count of sectors written per device, so
3820 * we find the difference between max_progress and the start point,
3821 * and divide that by after.data_disks to get a sync_max
3823 * At the same time we convert wait_point to a similar number
3824 * for comparing against sync_completed.
3826 /* scale down max_progress to per_disk */
3827 max_progress
/= reshape
->after
.data_disks
;
3828 /* Round to chunk size as some kernels give an erroneously high number */
3829 max_progress
/= info
->new_chunk
/512;
3830 max_progress
*= info
->new_chunk
/512;
3831 /* And round to old chunk size as the kernel wants that */
3832 max_progress
/= info
->array
.chunk_size
/512;
3833 max_progress
*= info
->array
.chunk_size
/512;
3834 /* Limit progress to the whole device */
3835 if (max_progress
> info
->component_size
)
3836 max_progress
= info
->component_size
;
3837 wait_point
/= reshape
->after
.data_disks
;
3839 /* switch from 'device offset' to 'processed block count' */
3840 max_progress
= info
->component_size
- max_progress
;
3841 wait_point
= info
->component_size
- wait_point
;
3845 sysfs_set_num(info
, NULL
, "sync_max", max_progress
);
3847 /* Now wait. If we have already reached the point that we were
3848 * asked to wait to, don't wait at all, else wait for any change.
3849 * We need to select on 'sync_completed' as that is the place that
3850 * notifications happen, but we are really interested in
3851 * 'reshape_position'
3853 fd
= sysfs_get_fd(info
, NULL
, "sync_completed");
3855 goto check_progress
;
3857 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
3858 goto check_progress
;
3860 while (completed
< max_progress
&& completed
< wait_point
) {
3861 /* Check that sync_action is still 'reshape' to avoid
3862 * waiting forever on a dead array
3865 if (sysfs_get_str(info
, NULL
, "sync_action",
3867 strncmp(action
, "reshape", 7) != 0)
3869 /* Some kernels reset 'sync_completed' to zero
3870 * before setting 'sync_action' to 'idle'.
3871 * So we need these extra tests.
3873 if (completed
== 0 && advancing
3874 && info
->reshape_progress
> 0)
3876 if (completed
== 0 && !advancing
3877 && info
->reshape_progress
< (info
->component_size
3878 * reshape
->after
.data_disks
))
3880 sysfs_wait(fd
, NULL
);
3881 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
3882 goto check_progress
;
3884 /* Some kernels reset 'sync_completed' to zero,
3885 * we need to have real point we are in md
3888 completed
= max_progress
;
3890 /* some kernels can give an incorrectly high 'completed' number */
3891 completed
/= (info
->new_chunk
/512);
3892 completed
*= (info
->new_chunk
/512);
3893 /* Convert 'completed' back in to a 'progress' number */
3894 completed
*= reshape
->after
.data_disks
;
3896 completed
= info
->component_size
* reshape
->after
.data_disks
3899 *reshape_completed
= completed
;
3903 /* We return the need_backup flag. Caller will decide
3904 * how much - a multiple of ->backup_blocks up to *suspend_point
3907 return need_backup
> info
->reshape_progress
;
3909 return need_backup
>= info
->reshape_progress
;
3912 /* if we couldn't read a number from sync_completed, then
3913 * either the reshape did complete, or it aborted.
3914 * We can tell which by checking for 'none' in reshape_position.
3915 * If it did abort, then it might immediately restart if it
3916 * it was just a device failure that leaves us degraded but
3920 if (sysfs_get_str(info
, NULL
, "reshape_position", buf
, sizeof(buf
)) < 0
3921 || strncmp(buf
, "none", 4) != 0) {
3922 /* The abort might only be temporary. Wait up to 10
3923 * seconds for fd to contain a valid number again.
3927 unsigned long long new_sync_max
;
3928 while (fd
>= 0 && rv
< 0 && wait
> 0) {
3929 if (sysfs_wait(fd
, &wait
) != 1)
3931 switch (sysfs_fd_get_ll(fd
, &completed
)) {
3933 /* all good again */
3935 /* If "sync_max" is no longer max_progress
3936 * we need to freeze things
3938 sysfs_get_ll(info
, NULL
, "sync_max", &new_sync_max
);
3939 *frozen
= (new_sync_max
!= max_progress
);
3941 case -2: /* read error - abort */
3948 return rv
; /* abort */
3950 /* Maybe racing with array shutdown - check state */
3953 if (sysfs_get_str(info
, NULL
, "array_state", buf
, sizeof(buf
)) < 0
3954 || strncmp(buf
, "inactive", 8) == 0
3955 || strncmp(buf
, "clear",5) == 0)
3956 return -2; /* abort */
3957 return -1; /* complete */
3961 /* FIXME return status is never checked */
3962 static int grow_backup(struct mdinfo
*sra
,
3963 unsigned long long offset
, /* per device */
3964 unsigned long stripes
, /* per device, in old chunks */
3965 int *sources
, unsigned long long *offsets
,
3966 int disks
, int chunk
, int level
, int layout
,
3967 int dests
, int *destfd
, unsigned long long *destoffsets
,
3968 int part
, int *degraded
,
3971 /* Backup 'blocks' sectors at 'offset' on each device of the array,
3972 * to storage 'destfd' (offset 'destoffsets'), after first
3973 * suspending IO. Then allow resync to continue
3974 * over the suspended section.
3975 * Use part 'part' of the backup-super-block.
3980 unsigned long long ll
;
3982 //printf("offset %llu\n", offset);
3988 /* Check that array hasn't become degraded, else we might backup the wrong data */
3989 if (sysfs_get_ll(sra
, NULL
, "degraded", &ll
) < 0)
3990 return -1; /* FIXME this error is ignored */
3991 new_degraded
= (int)ll
;
3992 if (new_degraded
!= *degraded
) {
3993 /* check each device to ensure it is still working */
3995 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
3996 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
3998 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
4000 if (sysfs_get_str(sra
, sd
, "state", sbuf
, 20) < 0 ||
4001 strstr(sbuf
, "faulty") ||
4002 strstr(sbuf
, "in_sync") == NULL
) {
4003 /* this device is dead */
4004 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
4005 if (sd
->disk
.raid_disk
>= 0 &&
4006 sources
[sd
->disk
.raid_disk
] >= 0) {
4007 close(sources
[sd
->disk
.raid_disk
]);
4008 sources
[sd
->disk
.raid_disk
] = -1;
4013 *degraded
= new_degraded
;
4016 bsb
.arraystart2
= __cpu_to_le64(offset
* odata
);
4017 bsb
.length2
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
4019 bsb
.arraystart
= __cpu_to_le64(offset
* odata
);
4020 bsb
.length
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
4023 bsb
.magic
[15] = '2';
4024 for (i
= 0; i
< dests
; i
++)
4026 lseek64(destfd
[i
], destoffsets
[i
] + __le64_to_cpu(bsb
.devstart2
)*512, 0);
4028 lseek64(destfd
[i
], destoffsets
[i
], 0);
4030 rv
= save_stripes(sources
, offsets
,
4031 disks
, chunk
, level
, layout
,
4033 offset
*512*odata
, stripes
* chunk
* odata
,
4038 bsb
.mtime
= __cpu_to_le64(time(0));
4039 for (i
= 0; i
< dests
; i
++) {
4040 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
4042 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
4043 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
4044 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
4045 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
4048 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
] - 4096, 0)
4049 != destoffsets
[i
] - 4096)
4051 if (write(destfd
[i
], &bsb
, 512) != 512)
4053 if (destoffsets
[i
] > 4096) {
4054 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]+stripes
*chunk
*odata
, 0) !=
4055 destoffsets
[i
]+stripes
*chunk
*odata
)
4057 if (write(destfd
[i
], &bsb
, 512) != 512)
4067 /* in 2.6.30, the value reported by sync_completed can be
4068 * less that it should be by one stripe.
4069 * This only happens when reshape hits sync_max and pauses.
4070 * So allow wait_backup to either extent sync_max further
4071 * than strictly necessary, or return before the
4072 * sync has got quite as far as we would really like.
4073 * This is what 'blocks2' is for.
4074 * The various caller give appropriate values so that
4077 /* FIXME return value is often ignored */
4078 static int forget_backup(int dests
, int *destfd
,
4079 unsigned long long *destoffsets
,
4083 * Erase backup 'part' (which is 0 or 1)
4089 bsb
.arraystart2
= __cpu_to_le64(0);
4090 bsb
.length2
= __cpu_to_le64(0);
4092 bsb
.arraystart
= __cpu_to_le64(0);
4093 bsb
.length
= __cpu_to_le64(0);
4095 bsb
.mtime
= __cpu_to_le64(time(0));
4097 for (i
= 0; i
< dests
; i
++) {
4098 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
4099 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
4100 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
4101 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
4102 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
4103 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]-4096, 0) !=
4104 destoffsets
[i
]-4096)
4107 write(destfd
[i
], &bsb
, 512) != 512)
4114 static void fail(char *msg
)
4117 rv
= (write(2, msg
, strlen(msg
)) != (int)strlen(msg
));
4118 rv
|= (write(2, "\n", 1) != 1);
4122 static char *abuf
, *bbuf
;
4123 static unsigned long long abuflen
;
4124 static void validate(int afd
, int bfd
, unsigned long long offset
)
4126 /* check that the data in the backup against the array.
4127 * This is only used for regression testing and should not
4128 * be used while the array is active
4132 lseek64(bfd
, offset
- 4096, 0);
4133 if (read(bfd
, &bsb2
, 512) != 512)
4134 fail("cannot read bsb");
4135 if (bsb2
.sb_csum
!= bsb_csum((char*)&bsb2
,
4136 ((char*)&bsb2
.sb_csum
)-((char*)&bsb2
)))
4137 fail("first csum bad");
4138 if (memcmp(bsb2
.magic
, "md_backup_data", 14) != 0)
4139 fail("magic is bad");
4140 if (memcmp(bsb2
.magic
, "md_backup_data-2", 16) == 0 &&
4141 bsb2
.sb_csum2
!= bsb_csum((char*)&bsb2
,
4142 ((char*)&bsb2
.sb_csum2
)-((char*)&bsb2
)))
4143 fail("second csum bad");
4145 if (__le64_to_cpu(bsb2
.devstart
)*512 != offset
)
4146 fail("devstart is wrong");
4149 unsigned long long len
= __le64_to_cpu(bsb2
.length
)*512;
4151 if (abuflen
< len
) {
4155 if (posix_memalign((void**)&abuf
, 4096, abuflen
) ||
4156 posix_memalign((void**)&bbuf
, 4096, abuflen
)) {
4158 /* just stop validating on mem-alloc failure */
4163 lseek64(bfd
, offset
, 0);
4164 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
) {
4165 //printf("len %llu\n", len);
4166 fail("read first backup failed");
4168 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart
)*512, 0);
4169 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
4170 fail("read first from array failed");
4171 if (memcmp(bbuf
, abuf
, len
) != 0) {
4174 printf("offset=%llu len=%llu\n",
4175 (unsigned long long)__le64_to_cpu(bsb2
.arraystart
)*512, len
);
4176 for (i
=0; i
<len
; i
++)
4177 if (bbuf
[i
] != abuf
[i
]) {
4178 printf("first diff byte %d\n", i
);
4182 fail("data1 compare failed");
4186 unsigned long long len
= __le64_to_cpu(bsb2
.length2
)*512;
4188 if (abuflen
< len
) {
4192 abuf
= xmalloc(abuflen
);
4193 bbuf
= xmalloc(abuflen
);
4196 lseek64(bfd
, offset
+__le64_to_cpu(bsb2
.devstart2
)*512, 0);
4197 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
)
4198 fail("read second backup failed");
4199 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart2
)*512, 0);
4200 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
4201 fail("read second from array failed");
4202 if (memcmp(bbuf
, abuf
, len
) != 0)
4203 fail("data2 compare failed");
4207 int child_monitor(int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
4208 struct supertype
*st
, unsigned long blocks
,
4209 int *fds
, unsigned long long *offsets
,
4210 int dests
, int *destfd
, unsigned long long *destoffsets
)
4212 /* Monitor a reshape where backup is being performed using
4213 * 'native' mechanism - either to a backup file, or
4214 * to some space in a spare.
4218 unsigned long long speed
;
4219 unsigned long long suspend_point
, array_size
;
4220 unsigned long long backup_point
, wait_point
;
4221 unsigned long long reshape_completed
;
4223 int increasing
= reshape
->after
.data_disks
>= reshape
->before
.data_disks
;
4224 int part
= 0; /* The next part of the backup area to fill. It may already
4225 * be full, so we need to check */
4226 int level
= reshape
->level
;
4227 int layout
= reshape
->before
.layout
;
4228 int data
= reshape
->before
.data_disks
;
4229 int disks
= reshape
->before
.data_disks
+ reshape
->parity
;
4230 int chunk
= sra
->array
.chunk_size
;
4232 unsigned long stripes
;
4236 /* set up the backup-super-block. This requires the
4237 * uuid from the array.
4239 /* Find a superblock */
4240 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
4244 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
4246 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
4247 devfd
= dev_open(dn
, O_RDONLY
);
4250 ok
= st
->ss
->load_super(st
, devfd
, NULL
);
4256 pr_err("Cannot find a superblock\n");
4260 memset(&bsb
, 0, 512);
4261 memcpy(bsb
.magic
, "md_backup_data-1", 16);
4262 st
->ss
->uuid_from_super(st
, uuid
);
4263 memcpy(bsb
.set_uuid
, uuid
, 16);
4264 bsb
.mtime
= __cpu_to_le64(time(0));
4265 bsb
.devstart2
= blocks
;
4267 stripes
= blocks
/ (sra
->array
.chunk_size
/512) /
4268 reshape
->before
.data_disks
;
4270 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
4271 /* Don't start the 'reshape' */
4273 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
) {
4274 sysfs_get_ll(sra
, NULL
, "sync_speed_min", &speed
);
4275 sysfs_set_num(sra
, NULL
, "sync_speed_min", 200000);
4279 array_size
= sra
->component_size
* reshape
->after
.data_disks
;
4280 backup_point
= sra
->reshape_progress
;
4283 array_size
= sra
->component_size
* reshape
->before
.data_disks
;
4284 backup_point
= reshape
->backup_blocks
;
4285 suspend_point
= array_size
;
4291 /* Want to return as soon the oldest backup slot can
4292 * be released as that allows us to start backing up
4293 * some more, providing suspend_point has been
4294 * advanced, which it should have.
4297 wait_point
= array_size
;
4298 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
4299 wait_point
= (__le64_to_cpu(bsb
.arraystart
) +
4300 __le64_to_cpu(bsb
.length
));
4301 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
4302 wait_point
= (__le64_to_cpu(bsb
.arraystart2
) +
4303 __le64_to_cpu(bsb
.length2
));
4306 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
4307 wait_point
= __le64_to_cpu(bsb
.arraystart
);
4308 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
4309 wait_point
= __le64_to_cpu(bsb
.arraystart2
);
4312 reshape_completed
= sra
->reshape_progress
;
4313 rv
= progress_reshape(sra
, reshape
,
4314 backup_point
, wait_point
,
4315 &suspend_point
, &reshape_completed
,
4317 /* external metadata would need to ping_monitor here */
4318 sra
->reshape_progress
= reshape_completed
;
4320 /* Clear any backup region that is before 'here' */
4322 if (__le64_to_cpu(bsb
.length
) > 0 &&
4323 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart
) +
4324 __le64_to_cpu(bsb
.length
)))
4325 forget_backup(dests
, destfd
,
4327 if (__le64_to_cpu(bsb
.length2
) > 0 &&
4328 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart2
) +
4329 __le64_to_cpu(bsb
.length2
)))
4330 forget_backup(dests
, destfd
,
4333 if (__le64_to_cpu(bsb
.length
) > 0 &&
4334 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart
)))
4335 forget_backup(dests
, destfd
,
4337 if (__le64_to_cpu(bsb
.length2
) > 0 &&
4338 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart2
)))
4339 forget_backup(dests
, destfd
,
4349 if (rv
== 0 && increasing
&& !st
->ss
->external
) {
4350 /* No longer need to monitor this reshape */
4351 sysfs_set_str(sra
, NULL
, "sync_max", "max");
4357 unsigned long long offset
;
4358 unsigned long actual_stripes
;
4359 /* Need to backup some data.
4360 * If 'part' is not used and the desired
4361 * backup size is suspended, do a backup,
4362 * then consider the next part.
4364 /* Check that 'part' is unused */
4365 if (part
== 0 && __le64_to_cpu(bsb
.length
) != 0)
4367 if (part
== 1 && __le64_to_cpu(bsb
.length2
) != 0)
4370 offset
= backup_point
/ data
;
4371 actual_stripes
= stripes
;
4373 if (offset
+ actual_stripes
* (chunk
/512) >
4374 sra
->component_size
)
4375 actual_stripes
= ((sra
->component_size
- offset
)
4377 if (offset
+ actual_stripes
* (chunk
/512) >
4381 if (offset
< actual_stripes
* (chunk
/512))
4382 actual_stripes
= offset
/ (chunk
/512);
4383 offset
-= actual_stripes
* (chunk
/512);
4384 if (offset
< suspend_point
/data
)
4387 if (actual_stripes
== 0)
4389 grow_backup(sra
, offset
, actual_stripes
,
4391 disks
, chunk
, level
, layout
,
4392 dests
, destfd
, destoffsets
,
4393 part
, °raded
, buf
);
4394 validate(afd
, destfd
[0], destoffsets
[0]);
4395 /* record where 'part' is up to */
4398 backup_point
+= actual_stripes
* (chunk
/512) * data
;
4400 backup_point
-= actual_stripes
* (chunk
/512) * data
;
4404 /* FIXME maybe call progress_reshape one more time instead */
4405 /* remove any remaining suspension */
4406 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
4407 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
4408 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
4409 sysfs_set_num(sra
, NULL
, "sync_min", 0);
4411 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
4412 sysfs_set_num(sra
, NULL
, "sync_speed_min", speed
);
4418 * If any spare contains md_back_data-1 which is recent wrt mtime,
4419 * write that data into the array and update the super blocks with
4420 * the new reshape_progress
4422 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
, int cnt
,
4423 char *backup_file
, int verbose
)
4427 unsigned long long *offsets
;
4428 unsigned long long nstripe
, ostripe
;
4431 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
4432 if (info
->array
.level
== 6) odata
--; /* number of data disks */
4433 ndata
= info
->array
.raid_disks
- 1;
4434 if (info
->new_level
== 6) ndata
--;
4436 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
4438 if (info
->delta_disks
<= 0)
4439 /* Didn't grow, so the backup file must have
4443 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
4444 struct mdinfo dinfo
;
4447 char *devname
, namebuf
[20];
4448 unsigned long long lo
, hi
;
4450 /* This was a spare and may have some saved data on it.
4451 * Load the superblock, find and load the
4452 * backup_super_block.
4453 * If either fail, go on to next device.
4454 * If the backup contains no new info, just return
4455 * else restore data and update all superblocks
4457 if (i
== old_disks
-1) {
4458 fd
= open(backup_file
, O_RDONLY
);
4460 pr_err("backup file %s inaccessible: %s\n",
4461 backup_file
, strerror(errno
));
4464 devname
= backup_file
;
4469 if (st
->ss
->load_super(st
, fd
, NULL
))
4472 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4473 st
->ss
->free_super(st
);
4476 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
4478 pr_err("Cannot seek on device %d\n", i
);
4479 continue; /* Cannot seek */
4481 sprintf(namebuf
, "device-%d", i
);
4484 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
)) {
4486 pr_err("Cannot read from %s\n", devname
);
4487 continue; /* Cannot read */
4489 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0 &&
4490 memcmp(bsb
.magic
, "md_backup_data-2", 16) != 0) {
4492 pr_err("No backup metadata on %s\n", devname
);
4495 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
))) {
4497 pr_err("Bad backup-metadata checksum on %s\n", devname
);
4498 continue; /* bad checksum */
4500 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0 &&
4501 bsb
.sb_csum2
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum2
)-((char*)&bsb
))) {
4503 pr_err("Bad backup-metadata checksum2 on %s\n", devname
);
4504 continue; /* Bad second checksum */
4506 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0) {
4508 pr_err("Wrong uuid on backup-metadata on %s\n", devname
);
4509 continue; /* Wrong uuid */
4512 /* array utime and backup-mtime should be updated at much the same time, but it seems that
4513 * sometimes they aren't... So allow considerable flexability in matching, and allow
4514 * this test to be overridden by an environment variable.
4516 if (info
->array
.utime
> (int)__le64_to_cpu(bsb
.mtime
) + 2*60*60 ||
4517 info
->array
.utime
< (int)__le64_to_cpu(bsb
.mtime
) - 10*60) {
4518 if (check_env("MDADM_GROW_ALLOW_OLD")) {
4519 pr_err("accepting backup with timestamp %lu "
4520 "for array with timestamp %lu\n",
4521 (unsigned long)__le64_to_cpu(bsb
.mtime
),
4522 (unsigned long)info
->array
.utime
);
4524 pr_err("too-old timestamp on backup-metadata on %s\n", devname
);
4525 pr_err("If you think it is should be safe, try 'export MDADM_GROW_ALLOW_OLD=1'\n");
4526 continue; /* time stamp is too bad */
4530 if (bsb
.magic
[15] == '1') {
4531 if (bsb
.length
== 0)
4533 if (info
->delta_disks
>= 0) {
4534 /* reshape_progress is increasing */
4535 if (__le64_to_cpu(bsb
.arraystart
)
4536 + __le64_to_cpu(bsb
.length
)
4537 < info
->reshape_progress
) {
4540 pr_err("backup-metadata found on %s but is not needed\n", devname
);
4541 continue; /* No new data here */
4544 /* reshape_progress is decreasing */
4545 if (__le64_to_cpu(bsb
.arraystart
) >=
4546 info
->reshape_progress
)
4547 goto nonew
; /* No new data here */
4550 if (bsb
.length
== 0 && bsb
.length2
== 0)
4552 if (info
->delta_disks
>= 0) {
4553 /* reshape_progress is increasing */
4554 if ((__le64_to_cpu(bsb
.arraystart
)
4555 + __le64_to_cpu(bsb
.length
)
4556 < info
->reshape_progress
)
4558 (__le64_to_cpu(bsb
.arraystart2
)
4559 + __le64_to_cpu(bsb
.length2
)
4560 < info
->reshape_progress
))
4561 goto nonew
; /* No new data here */
4563 /* reshape_progress is decreasing */
4564 if (__le64_to_cpu(bsb
.arraystart
) >=
4565 info
->reshape_progress
&&
4566 __le64_to_cpu(bsb
.arraystart2
) >=
4567 info
->reshape_progress
)
4568 goto nonew
; /* No new data here */
4571 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0) {
4574 pr_err("Failed to verify secondary backup-metadata block on %s\n",
4576 continue; /* Cannot seek */
4578 /* There should be a duplicate backup superblock 4k before here */
4579 if (lseek64(fd
, -4096, 1) < 0 ||
4580 read(fd
, &bsb2
, sizeof(bsb2
)) != sizeof(bsb2
))
4581 goto second_fail
; /* Cannot find leading superblock */
4582 if (bsb
.magic
[15] == '1')
4583 bsbsize
= offsetof(struct mdp_backup_super
, pad1
);
4585 bsbsize
= offsetof(struct mdp_backup_super
, pad
);
4586 if (memcmp(&bsb2
, &bsb
, bsbsize
) != 0)
4587 goto second_fail
; /* Cannot find leading superblock */
4589 /* Now need the data offsets for all devices. */
4590 offsets
= xmalloc(sizeof(*offsets
)*info
->array
.raid_disks
);
4591 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
4594 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
4595 /* FIXME should be this be an error */
4597 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4598 st
->ss
->free_super(st
);
4599 offsets
[j
] = dinfo
.data_offset
* 512;
4601 printf(Name
": restoring critical section\n");
4603 if (restore_stripes(fdlist
, offsets
,
4604 info
->array
.raid_disks
,
4608 fd
, __le64_to_cpu(bsb
.devstart
)*512,
4609 __le64_to_cpu(bsb
.arraystart
)*512,
4610 __le64_to_cpu(bsb
.length
)*512, NULL
)) {
4611 /* didn't succeed, so giveup */
4613 pr_err("Error restoring backup from %s\n",
4619 if (bsb
.magic
[15] == '2' &&
4620 restore_stripes(fdlist
, offsets
,
4621 info
->array
.raid_disks
,
4625 fd
, __le64_to_cpu(bsb
.devstart
)*512 +
4626 __le64_to_cpu(bsb
.devstart2
)*512,
4627 __le64_to_cpu(bsb
.arraystart2
)*512,
4628 __le64_to_cpu(bsb
.length2
)*512, NULL
)) {
4629 /* didn't succeed, so giveup */
4631 pr_err("Error restoring second backup from %s\n",
4639 /* Ok, so the data is restored. Let's update those superblocks. */
4643 lo
= __le64_to_cpu(bsb
.arraystart
);
4644 hi
= lo
+ __le64_to_cpu(bsb
.length
);
4646 if (bsb
.magic
[15] == '2' && bsb
.length2
) {
4647 unsigned long long lo1
, hi1
;
4648 lo1
= __le64_to_cpu(bsb
.arraystart2
);
4649 hi1
= lo1
+ __le64_to_cpu(bsb
.length2
);
4653 } else if (lo
< lo1
)
4659 (info
->reshape_progress
< lo
||
4660 info
->reshape_progress
> hi
))
4661 /* backup does not affect reshape_progress*/ ;
4662 else if (info
->delta_disks
>= 0) {
4663 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
) +
4664 __le64_to_cpu(bsb
.length
);
4665 if (bsb
.magic
[15] == '2') {
4666 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
) +
4667 __le64_to_cpu(bsb
.length2
);
4668 if (p2
> info
->reshape_progress
)
4669 info
->reshape_progress
= p2
;
4672 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
);
4673 if (bsb
.magic
[15] == '2') {
4674 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
);
4675 if (p2
< info
->reshape_progress
)
4676 info
->reshape_progress
= p2
;
4679 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
4682 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
4684 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4685 dinfo
.reshape_progress
= info
->reshape_progress
;
4686 st
->ss
->update_super(st
, &dinfo
,
4687 "_reshape_progress",
4689 st
->ss
->store_super(st
, fdlist
[j
]);
4690 st
->ss
->free_super(st
);
4694 /* Didn't find any backup data, try to see if any
4697 if (info
->delta_disks
< 0) {
4698 /* When shrinking, the critical section is at the end.
4699 * So see if we are before the critical section.
4701 unsigned long long first_block
;
4702 nstripe
= ostripe
= 0;
4704 while (ostripe
>= nstripe
) {
4705 ostripe
+= info
->array
.chunk_size
/ 512;
4706 first_block
= ostripe
* odata
;
4707 nstripe
= first_block
/ ndata
/ (info
->new_chunk
/512) *
4708 (info
->new_chunk
/512);
4711 if (info
->reshape_progress
>= first_block
)
4714 if (info
->delta_disks
> 0) {
4715 /* See if we are beyond the critical section. */
4716 unsigned long long last_block
;
4717 nstripe
= ostripe
= 0;
4719 while (nstripe
>= ostripe
) {
4720 nstripe
+= info
->new_chunk
/ 512;
4721 last_block
= nstripe
* ndata
;
4722 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
4723 (info
->array
.chunk_size
/512);
4726 if (info
->reshape_progress
>= last_block
)
4729 /* needed to recover critical section! */
4731 pr_err("Failed to find backup of critical section\n");
4735 int Grow_continue_command(char *devname
, int fd
,
4736 char *backup_file
, int verbose
)
4739 struct supertype
*st
= NULL
;
4740 struct mdinfo
*content
= NULL
;
4741 struct mdinfo array
;
4742 char *subarray
= NULL
;
4743 struct mdinfo
*cc
= NULL
;
4744 struct mdstat_ent
*mdstat
= NULL
;
4748 dprintf("Grow continue from command line called for %s\n",
4751 st
= super_by_fd(fd
, &subarray
);
4752 if (!st
|| !st
->ss
) {
4753 pr_err("Unable to determine metadata format for %s\n",
4757 dprintf("Grow continue is run for ");
4758 if (st
->ss
->external
== 0) {
4760 dprintf("native array (%s)\n", devname
);
4761 if (ioctl(fd
, GET_ARRAY_INFO
, &array
.array
) < 0) {
4762 pr_err("%s is not an active md array -"
4763 " aborting\n", devname
);
4765 goto Grow_continue_command_exit
;
4768 /* Need to load a superblock.
4769 * FIXME we should really get what we need from
4772 for (d
= 0; d
< MAX_DISKS
; d
++) {
4773 mdu_disk_info_t disk
;
4777 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
4779 if (disk
.major
== 0 && disk
.minor
== 0)
4781 if ((disk
.state
& (1 << MD_DISK_ACTIVE
)) == 0)
4783 dv
= map_dev(disk
.major
, disk
.minor
, 1);
4786 fd2
= dev_open(dv
, O_RDONLY
);
4789 err
= st
->ss
->load_super(st
, fd2
, NULL
);
4791 /* invalidate fd2 to avoid possible double close() */
4797 if (d
== MAX_DISKS
) {
4798 pr_err("Unable to load metadata for %s\n",
4801 goto Grow_continue_command_exit
;
4803 st
->ss
->getinfo_super(st
, content
, NULL
);
4808 dprintf("subarray (%s)\n", subarray
);
4809 container
= st
->container_devnm
;
4810 cfd
= open_dev_excl(st
->container_devnm
);
4812 container
= st
->devnm
;
4814 cfd
= open_dev_excl(st
->devnm
);
4815 dprintf("container (%s)\n", container
);
4819 pr_err("Unable to open container "
4820 "for %s\n", devname
);
4822 goto Grow_continue_command_exit
;
4825 /* find in container array under reshape
4827 ret_val
= st
->ss
->load_container(st
, cfd
, NULL
);
4829 pr_err("Cannot read superblock for %s\n",
4832 goto Grow_continue_command_exit
;
4835 cc
= st
->ss
->container_content(st
, subarray
);
4836 for (content
= cc
; content
; content
= content
->next
) {
4838 int allow_reshape
= 1;
4840 if (content
->reshape_active
== 0)
4842 /* The decision about array or container wide
4843 * reshape is taken in Grow_continue based
4844 * content->reshape_active state, therefore we
4845 * need to check_reshape based on
4846 * reshape_active and subarray name
4848 if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
))
4850 if (content
->reshape_active
== CONTAINER_RESHAPE
&&
4851 (content
->array
.state
4852 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
)))
4855 if (!allow_reshape
) {
4856 pr_err("cannot continue reshape of an array"
4857 " in container with unsupported"
4858 " metadata: %s(%s)\n",
4859 devname
, container
);
4861 goto Grow_continue_command_exit
;
4864 array
= strchr(content
->text_version
+1, '/')+1;
4865 mdstat
= mdstat_by_subdev(array
, container
);
4868 if (mdstat
->active
== 0) {
4869 pr_err("Skipping inactive array %s.\n",
4871 free_mdstat(mdstat
);
4878 pr_err("Unable to determine reshaped "
4879 "array for %s\n", devname
);
4881 goto Grow_continue_command_exit
;
4883 fd2
= open_dev(mdstat
->devnm
);
4885 pr_err("cannot open (%s)\n", mdstat
->devnm
);
4887 goto Grow_continue_command_exit
;
4890 sysfs_init(content
, fd2
, mdstat
->devnm
);
4892 /* start mdmon in case it is not running
4894 if (!mdmon_running(container
))
4895 start_mdmon(container
);
4896 ping_monitor(container
);
4898 if (mdmon_running(container
))
4899 st
->update_tail
= &st
->updates
;
4901 pr_err("No mdmon found. "
4902 "Grow cannot continue.\n");
4904 goto Grow_continue_command_exit
;
4908 /* verify that array under reshape is started from
4911 if (verify_reshape_position(content
, content
->array
.level
) < 0) {
4913 goto Grow_continue_command_exit
;
4918 ret_val
= Grow_continue(fd
, st
, content
, backup_file
, 0);
4920 Grow_continue_command_exit
:
4925 st
->ss
->free_super(st
);
4926 free_mdstat(mdstat
);
4933 int Grow_continue(int mdfd
, struct supertype
*st
, struct mdinfo
*info
,
4934 char *backup_file
, int freeze_reshape
)
4938 if (!info
->reshape_active
)
4941 if (st
->ss
->external
) {
4942 int cfd
= open_dev(st
->container_devnm
);
4947 st
->ss
->load_container(st
, cfd
, st
->container_devnm
);
4949 ret_val
= reshape_container(st
->container_devnm
, NULL
, mdfd
,
4950 st
, info
, 0, backup_file
,
4952 1 | info
->reshape_active
,
4955 ret_val
= reshape_array(NULL
, mdfd
, "array", st
, info
, 1,
4956 NULL
, INVALID_SECTORS
,
4958 1 | info
->reshape_active
,
4964 char *make_backup(char *name
)
4966 char *base
= "backup_file-";
4970 len
= strlen(MAP_DIR
) + 1 + strlen(base
) + strlen(name
)+1;
4971 fname
= xmalloc(len
);
4972 sprintf(fname
, "%s/%s%s", MAP_DIR
, base
, name
);
4976 char *locate_backup(char *name
)
4978 char *fl
= make_backup(name
);
4981 if (stat(fl
, &stb
) == 0 &&
4982 S_ISREG(stb
.st_mode
))