2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2013 Neil Brown <neilb@suse.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Email: <neilb@suse.de>
32 #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
33 #error no endian defined
38 int restore_backup(struct supertype
*st
,
39 struct mdinfo
*content
,
49 int disk_count
= next_spare
+ working_disks
;
50 char *backup_file
= *backup_filep
;
52 dprintf("Called restore_backup()\n");
53 fdlist
= xmalloc(sizeof(int) * disk_count
);
55 enable_fds(next_spare
);
56 for (i
= 0; i
< next_spare
; i
++)
58 for (dev
= content
->devs
; dev
; dev
= dev
->next
) {
64 fd
= dev_open(buf
, O_RDWR
);
66 if (dev
->disk
.raid_disk
>= 0)
67 fdlist
[dev
->disk
.raid_disk
] = fd
;
69 fdlist
[next_spare
++] = fd
;
73 backup_file
= locate_backup(content
->sys_name
);
74 *backup_filep
= backup_file
;
77 if (st
->ss
->external
&& st
->ss
->recover_backup
)
78 err
= st
->ss
->recover_backup(st
, content
);
80 err
= Grow_restart(st
, content
, fdlist
, next_spare
,
81 backup_file
, verbose
> 0);
83 while (next_spare
> 0) {
85 if (fdlist
[next_spare
] >= 0)
86 close(fdlist
[next_spare
]);
90 pr_err("Failed to restore critical"
91 " section for reshape - sorry.\n");
93 pr_err("Possibly you need"
94 " to specify a --backup-file\n");
98 dprintf("restore_backup() returns status OK.\n");
102 int Grow_Add_device(char *devname
, int fd
, char *newdev
)
104 /* Add a device to an active array.
105 * Currently, just extend a linear array.
106 * This requires writing a new superblock on the
107 * new device, calling the kernel to add the device,
108 * and if that succeeds, update the superblock on
110 * This means that we need to *find* all other devices.
117 struct supertype
*st
= NULL
;
118 char *subarray
= NULL
;
120 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
121 pr_err("cannot get array info for %s\n", devname
);
125 if (info
.array
.level
!= -1) {
126 pr_err("can only add devices to linear arrays\n");
130 st
= super_by_fd(fd
, &subarray
);
132 pr_err("cannot handle arrays with superblock version %d\n",
133 info
.array
.major_version
);
138 pr_err("Cannot grow linear sub-arrays yet\n");
144 nfd
= open(newdev
, O_RDWR
|O_EXCL
|O_DIRECT
);
146 pr_err("cannot open %s\n", newdev
);
151 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
152 pr_err("%s is not a block device!\n", newdev
);
157 /* now check out all the devices and make sure we can read the
159 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
160 mdu_disk_info_t disk
;
163 st
->ss
->free_super(st
);
166 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
167 pr_err("cannot get device detail for device %d\n",
173 dv
= map_dev(disk
.major
, disk
.minor
, 1);
175 pr_err("cannot find device file for device %d\n",
181 fd2
= dev_open(dv
, O_RDWR
);
183 pr_err("cannot open device file %s\n", dv
);
189 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
190 pr_err("cannot find super block on %s\n", dv
);
198 /* Ok, looks good. Lets update the superblock and write it out to
202 info
.disk
.number
= d
;
203 info
.disk
.major
= major(stb
.st_rdev
);
204 info
.disk
.minor
= minor(stb
.st_rdev
);
205 info
.disk
.raid_disk
= d
;
206 info
.disk
.state
= (1 << MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
);
207 st
->ss
->update_super(st
, &info
, "linear-grow-new", newdev
,
210 if (st
->ss
->store_super(st
, nfd
)) {
211 pr_err("Cannot store new superblock on %s\n",
218 if (ioctl(fd
, ADD_NEW_DISK
, &info
.disk
) != 0) {
219 pr_err("Cannot add new disk to this array\n");
222 /* Well, that seems to have worked.
223 * Now go through and update all superblocks
226 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
227 pr_err("cannot get array info for %s\n", devname
);
232 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
233 mdu_disk_info_t disk
;
237 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
238 pr_err("cannot get device detail for device %d\n",
242 dv
= map_dev(disk
.major
, disk
.minor
, 1);
244 pr_err("cannot find device file for device %d\n",
248 fd2
= dev_open(dv
, O_RDWR
);
250 pr_err("cannot open device file %s\n", dv
);
253 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
254 pr_err("cannot find super block on %s\n", dv
);
258 info
.array
.raid_disks
= nd
+1;
259 info
.array
.nr_disks
= nd
+1;
260 info
.array
.active_disks
= nd
+1;
261 info
.array
.working_disks
= nd
+1;
263 st
->ss
->update_super(st
, &info
, "linear-grow-update", dv
,
266 if (st
->ss
->store_super(st
, fd2
)) {
267 pr_err("Cannot store new superblock on %s\n", dv
);
277 int Grow_addbitmap(char *devname
, int fd
, struct context
*c
, struct shape
*s
)
280 * First check that array doesn't have a bitmap
281 * Then create the bitmap
284 * For internal bitmaps, we need to check the version,
285 * find all the active devices, and write the bitmap block
288 mdu_bitmap_file_t bmf
;
289 mdu_array_info_t array
;
290 struct supertype
*st
;
291 char *subarray
= NULL
;
292 int major
= BITMAP_MAJOR_HI
;
293 int vers
= md_get_version(fd
);
294 unsigned long long bitmapsize
, array_size
;
297 major
= BITMAP_MAJOR_HOSTENDIAN
;
298 pr_err("Warning - bitmaps created on this kernel"
299 " are not portable\n"
300 " between different architectures. Consider upgrading"
301 " the Linux kernel.\n");
304 if (ioctl(fd
, GET_BITMAP_FILE
, &bmf
) != 0) {
306 pr_err("Memory allocation failure.\n");
308 pr_err("bitmaps not supported by this kernel.\n");
311 if (bmf
.pathname
[0]) {
312 if (strcmp(s
->bitmap_file
,"none")==0) {
313 if (ioctl(fd
, SET_BITMAP_FILE
, -1)!= 0) {
314 pr_err("failed to remove bitmap %s\n",
320 pr_err("%s already has a bitmap (%s)\n",
321 devname
, bmf
.pathname
);
324 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
325 pr_err("cannot get array status for %s\n", devname
);
328 if (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)) {
329 if (strcmp(s
->bitmap_file
, "none")==0) {
330 array
.state
&= ~(1<<MD_SB_BITMAP_PRESENT
);
331 if (ioctl(fd
, SET_ARRAY_INFO
, &array
)!= 0) {
332 pr_err("failed to remove internal bitmap.\n");
337 pr_err("Internal bitmap already present on %s\n",
342 if (strcmp(s
->bitmap_file
, "none") == 0) {
343 pr_err("no bitmap found on %s\n", devname
);
346 if (array
.level
<= 0) {
347 pr_err("Bitmaps not meaningful with level %s\n",
348 map_num(pers
, array
.level
)?:"of this array");
351 bitmapsize
= array
.size
;
353 if (get_dev_size(fd
, NULL
, &array_size
) &&
354 array_size
> (0x7fffffffULL
<<9)) {
355 /* Array is big enough that we cannot trust array.size
356 * try other approaches
358 bitmapsize
= get_component_size(fd
);
360 if (bitmapsize
== 0) {
361 pr_err("Cannot reliably determine size of array to create bitmap - sorry.\n");
365 if (array
.level
== 10) {
366 int ncopies
= (array
.layout
&255)*((array
.layout
>>8)&255);
367 bitmapsize
= bitmapsize
* array
.raid_disks
/ ncopies
;
370 st
= super_by_fd(fd
, &subarray
);
372 pr_err("Cannot understand version %d.%d\n",
373 array
.major_version
, array
.minor_version
);
377 pr_err("Cannot add bitmaps to sub-arrays yet\n");
382 if (strcmp(s
->bitmap_file
, "internal") == 0) {
385 int offset_setable
= 0;
387 if (st
->ss
->add_internal_bitmap
== NULL
) {
388 pr_err("Internal bitmaps not supported "
389 "with %s metadata\n", st
->ss
->name
);
392 mdi
= sysfs_read(fd
, NULL
, GET_BITMAP_LOCATION
);
395 for (d
=0; d
< st
->max_devs
; d
++) {
396 mdu_disk_info_t disk
;
399 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
401 if (disk
.major
== 0 &&
404 if ((disk
.state
& (1<<MD_DISK_SYNC
))==0)
406 dv
= map_dev(disk
.major
, disk
.minor
, 1);
408 int fd2
= dev_open(dv
, O_RDWR
);
411 if (st
->ss
->load_super(st
, fd2
, NULL
)==0) {
412 if (st
->ss
->add_internal_bitmap(
414 &s
->bitmap_chunk
, c
->delay
, s
->write_behind
,
415 bitmapsize
, offset_setable
,
418 st
->ss
->write_bitmap(st
, fd2
);
420 pr_err("failed to create internal bitmap"
421 " - chunksize problem.\n");
429 if (offset_setable
) {
430 st
->ss
->getinfo_super(st
, mdi
, NULL
);
431 sysfs_init(mdi
, fd
, NULL
);
432 rv
= sysfs_set_num_signed(mdi
, NULL
, "bitmap/location",
435 array
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
436 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
440 pr_err("Cannot add bitmap while array is"
441 " resyncing or reshaping etc.\n");
442 pr_err("failed to set internal bitmap.\n");
449 int max_devs
= st
->max_devs
;
451 /* try to load a superblock */
452 for (d
= 0; d
< max_devs
; d
++) {
453 mdu_disk_info_t disk
;
457 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
459 if ((disk
.major
==0 && disk
.minor
==0) ||
460 (disk
.state
& (1<<MD_DISK_REMOVED
)))
462 dv
= map_dev(disk
.major
, disk
.minor
, 1);
465 fd2
= dev_open(dv
, O_RDONLY
);
467 if (st
->ss
->load_super(st
, fd2
, NULL
) == 0) {
469 st
->ss
->uuid_from_super(st
, uuid
);
476 pr_err("cannot find UUID for array!\n");
479 if (CreateBitmap(s
->bitmap_file
, c
->force
, (char*)uuid
, s
->bitmap_chunk
,
480 c
->delay
, s
->write_behind
, bitmapsize
, major
)) {
483 bitmap_fd
= open(s
->bitmap_file
, O_RDWR
);
485 pr_err("weird: %s cannot be opened\n",
489 if (ioctl(fd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
492 pr_err("Cannot add bitmap while array is"
493 " resyncing or reshaping etc.\n");
494 pr_err("Cannot set bitmap file for %s: %s\n",
495 devname
, strerror(err
));
504 * When reshaping an array we might need to backup some data.
505 * This is written to all spares with a 'super_block' describing it.
506 * The superblock goes 4K from the end of the used space on the
508 * It if written after the backup is complete.
509 * It has the following structure.
512 static struct mdp_backup_super
{
513 char magic
[16]; /* md_backup_data-1 or -2 */
516 /* start/sizes in 512byte sectors */
517 __u64 devstart
; /* address on backup device/file of data */
520 __u32 sb_csum
; /* csum of preceeding bytes. */
522 __u64 devstart2
; /* offset in to data of second section */
525 __u32 sb_csum2
; /* csum of preceeding bytes. */
527 } __attribute__((aligned(512))) bsb
, bsb2
;
529 static __u32
bsb_csum(char *buf
, int len
)
533 for (i
= 0; i
< len
; i
++)
534 csum
= (csum
<<3) + buf
[0];
535 return __cpu_to_le32(csum
);
538 static int check_idle(struct supertype
*st
)
540 /* Check that all member arrays for this container, or the
541 * container of this array, are idle
543 char *container
= (st
->container_devnm
[0]
544 ? st
->container_devnm
: st
->devnm
);
545 struct mdstat_ent
*ent
, *e
;
548 ent
= mdstat_read(0, 0);
549 for (e
= ent
; e
; e
= e
->next
) {
550 if (!is_container_member(e
, container
))
552 if (e
->percent
>= 0) {
561 static int freeze_container(struct supertype
*st
)
563 char *container
= (st
->container_devnm
[0]
564 ? st
->container_devnm
: st
->devnm
);
569 if (block_monitor(container
, 1)) {
570 pr_err("failed to freeze container\n");
577 static void unfreeze_container(struct supertype
*st
)
579 char *container
= (st
->container_devnm
[0]
580 ? st
->container_devnm
: st
->devnm
);
582 unblock_monitor(container
, 1);
585 static int freeze(struct supertype
*st
)
587 /* Try to freeze resync/rebuild on this array/container.
588 * Return -1 if the array is busy,
589 * return -2 container cannot be frozen,
590 * return 0 if this kernel doesn't support 'frozen'
591 * return 1 if it worked.
593 if (st
->ss
->external
)
594 return freeze_container(st
);
596 struct mdinfo
*sra
= sysfs_read(-1, st
->devnm
, GET_VERSION
);
602 /* Need to clear any 'read-auto' status */
603 if (sysfs_get_str(sra
, NULL
, "array_state", buf
, 20) > 0 &&
604 strncmp(buf
, "read-auto", 9) == 0)
605 sysfs_set_str(sra
, NULL
, "array_state", "clean");
607 err
= sysfs_freeze_array(sra
);
613 static void unfreeze(struct supertype
*st
)
615 if (st
->ss
->external
)
616 return unfreeze_container(st
);
618 struct mdinfo
*sra
= sysfs_read(-1, st
->devnm
, GET_VERSION
);
622 sysfs_get_str(sra
, NULL
, "sync_action", buf
, 20) > 0
623 && strcmp(buf
, "frozen\n") == 0) {
624 printf("unfreeze\n");
625 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
631 static void wait_reshape(struct mdinfo
*sra
)
633 int fd
= sysfs_get_fd(sra
, NULL
, "sync_action");
639 while (sysfs_fd_get_str(fd
, action
, 20) > 0 &&
640 strncmp(action
, "reshape", 7) == 0)
641 sysfs_wait(fd
, NULL
);
645 static int reshape_super(struct supertype
*st
, unsigned long long size
,
646 int level
, int layout
, int chunksize
, int raid_disks
,
647 int delta_disks
, char *backup_file
, char *dev
,
648 int direction
, int verbose
)
650 /* nothing extra to check in the native case */
651 if (!st
->ss
->external
)
653 if (!st
->ss
->reshape_super
||
654 !st
->ss
->manage_reshape
) {
655 pr_err("%s metadata does not support reshape\n",
660 return st
->ss
->reshape_super(st
, size
, level
, layout
, chunksize
,
661 raid_disks
, delta_disks
, backup_file
, dev
,
665 static void sync_metadata(struct supertype
*st
)
667 if (st
->ss
->external
) {
668 if (st
->update_tail
) {
669 flush_metadata_updates(st
);
670 st
->update_tail
= &st
->updates
;
672 st
->ss
->sync_metadata(st
);
676 static int subarray_set_num(char *container
, struct mdinfo
*sra
, char *name
, int n
)
678 /* when dealing with external metadata subarrays we need to be
679 * prepared to handle EAGAIN. The kernel may need to wait for
680 * mdmon to mark the array active so the kernel can handle
681 * allocations/writeback when preparing the reshape action
682 * (md_allow_write()). We temporarily disable safe_mode_delay
683 * to close a race with the array_state going clean before the
684 * next write to raid_disks / stripe_cache_size
689 /* only 'raid_disks' and 'stripe_cache_size' trigger md_allow_write */
691 (strcmp(name
, "raid_disks") != 0 &&
692 strcmp(name
, "stripe_cache_size") != 0))
693 return sysfs_set_num(sra
, NULL
, name
, n
);
695 rc
= sysfs_get_str(sra
, NULL
, "safe_mode_delay", safe
, sizeof(safe
));
698 sysfs_set_num(sra
, NULL
, "safe_mode_delay", 0);
699 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
700 if (rc
< 0 && errno
== EAGAIN
) {
701 ping_monitor(container
);
702 /* if we get EAGAIN here then the monitor is not active
705 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
707 sysfs_set_str(sra
, NULL
, "safe_mode_delay", safe
);
711 int start_reshape(struct mdinfo
*sra
, int already_running
,
712 int before_data_disks
, int data_disks
)
715 unsigned long long sync_max_to_set
;
717 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
718 err
= sysfs_set_num(sra
, NULL
, "suspend_hi", sra
->reshape_progress
);
719 err
= err
?: sysfs_set_num(sra
, NULL
, "suspend_lo",
720 sra
->reshape_progress
);
721 if (before_data_disks
<= data_disks
)
722 sync_max_to_set
= sra
->reshape_progress
/ data_disks
;
724 sync_max_to_set
= (sra
->component_size
* data_disks
725 - sra
->reshape_progress
) / data_disks
;
726 if (!already_running
)
727 sysfs_set_num(sra
, NULL
, "sync_min", sync_max_to_set
);
728 err
= err
?: sysfs_set_num(sra
, NULL
, "sync_max", sync_max_to_set
);
729 if (!already_running
)
730 err
= err
?: sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
735 void abort_reshape(struct mdinfo
*sra
)
737 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
738 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
739 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
740 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
741 sysfs_set_num(sra
, NULL
, "sync_min", 0);
742 // It isn't safe to reset sync_max as we aren't monitoring.
743 // Array really should be stopped at this point.
746 int remove_disks_for_takeover(struct supertype
*st
,
751 struct mdinfo
*remaining
;
754 if (sra
->array
.level
== 10)
755 nr_of_copies
= layout
& 0xff;
756 else if (sra
->array
.level
== 1)
757 nr_of_copies
= sra
->array
.raid_disks
;
761 remaining
= sra
->devs
;
763 /* for each 'copy', select one device and remove from the list. */
764 for (slot
= 0; slot
< sra
->array
.raid_disks
; slot
+= nr_of_copies
) {
765 struct mdinfo
**diskp
;
768 /* Find a working device to keep */
769 for (diskp
= &remaining
; *diskp
; diskp
= &(*diskp
)->next
) {
770 struct mdinfo
*disk
= *diskp
;
772 if (disk
->disk
.raid_disk
< slot
)
774 if (disk
->disk
.raid_disk
>= slot
+ nr_of_copies
)
776 if (disk
->disk
.state
& (1<<MD_DISK_REMOVED
))
778 if (disk
->disk
.state
& (1<<MD_DISK_FAULTY
))
780 if (!(disk
->disk
.state
& (1<<MD_DISK_SYNC
)))
783 /* We have found a good disk to use! */
785 disk
->next
= sra
->devs
;
794 if (slot
< sra
->array
.raid_disks
) {
795 /* didn't find all slots */
801 sra
->devs
= remaining
;
805 /* Remove all 'remaining' devices from the array */
807 struct mdinfo
*sd
= remaining
;
808 remaining
= sd
->next
;
810 sysfs_set_str(sra
, sd
, "state", "faulty");
811 sysfs_set_str(sra
, sd
, "slot", "none");
812 /* for external metadata disks should be removed in mdmon */
813 if (!st
->ss
->external
)
814 sysfs_set_str(sra
, sd
, "state", "remove");
815 sd
->disk
.state
|= (1<<MD_DISK_REMOVED
);
816 sd
->disk
.state
&= ~(1<<MD_DISK_SYNC
);
817 sd
->next
= sra
->devs
;
823 void reshape_free_fdlist(int *fdlist
,
824 unsigned long long *offsets
,
829 for (i
= 0; i
< size
; i
++)
837 int reshape_prepare_fdlist(char *devname
,
841 unsigned long blocks
,
844 unsigned long long *offsets
)
850 for (d
= 0; d
<= nrdisks
; d
++)
853 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
854 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
856 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
857 char *dn
= map_dev(sd
->disk
.major
,
859 fdlist
[sd
->disk
.raid_disk
]
860 = dev_open(dn
, O_RDONLY
);
861 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
862 if (fdlist
[sd
->disk
.raid_disk
] < 0) {
863 pr_err("%s: cannot open component %s\n",
864 devname
, dn
? dn
: "-unknown-");
868 } else if (backup_file
== NULL
) {
870 char *dn
= map_dev(sd
->disk
.major
,
872 fdlist
[d
] = dev_open(dn
, O_RDWR
);
873 offsets
[d
] = (sd
->data_offset
+ sra
->component_size
- blocks
- 8)*512;
875 pr_err("%s: cannot open component %s\n",
876 devname
, dn
? dn
: "-unknown-");
887 int reshape_open_backup_file(char *backup_file
,
892 unsigned long long *offsets
,
896 /* Return 1 on success, 0 on any form of failure */
897 /* need to check backup file is large enough */
903 *fdlist
= open(backup_file
, O_RDWR
|O_CREAT
|(restart
? O_TRUNC
: O_EXCL
),
907 pr_err("%s: cannot create backup file %s: %s\n",
908 devname
, backup_file
, strerror(errno
));
911 /* Guard against backup file being on array device.
912 * If array is partitioned or if LVM etc is in the
913 * way this will not notice, but it is better than
916 fstat(*fdlist
, &stb
);
919 if (stb
.st_rdev
== dev
) {
920 pr_err("backup file must NOT be"
921 " on the array being reshaped.\n");
927 for (i
=0; i
< blocks
+ 8 ; i
++) {
928 if (write(*fdlist
, buf
, 512) != 512) {
929 pr_err("%s: cannot create"
930 " backup file %s: %s\n",
931 devname
, backup_file
, strerror(errno
));
935 if (fsync(*fdlist
) != 0) {
936 pr_err("%s: cannot create backup file %s: %s\n",
937 devname
, backup_file
, strerror(errno
));
941 if (!restart
&& strncmp(backup_file
, MAP_DIR
, strlen(MAP_DIR
)) != 0) {
942 char *bu
= make_backup(sys_name
);
943 symlink(backup_file
, bu
);
950 unsigned long compute_backup_blocks(int nchunk
, int ochunk
,
951 unsigned int ndata
, unsigned int odata
)
953 unsigned long a
, b
, blocks
;
954 /* So how much do we need to backup.
955 * We need an amount of data which is both a whole number of
956 * old stripes and a whole number of new stripes.
957 * So LCM for (chunksize*datadisks).
959 a
= (ochunk
/512) * odata
;
960 b
= (nchunk
/512) * ndata
;
963 /* LCM == product / GCD */
964 blocks
= (ochunk
/512) * (nchunk
/512) * odata
* ndata
/ a
;
969 char *analyse_change(char *devname
, struct mdinfo
*info
, struct reshape
*re
)
971 /* Based on the current array state in info->array and
972 * the changes in info->new_* etc, determine:
973 * - whether the change is possible
974 * - Intermediate level/raid_disks/layout
975 * - whether a restriping reshape is needed
976 * - number of sectors in minimum change unit. This
977 * will cover a whole number of stripes in 'before' and
980 * Return message if the change should be rejected
981 * NULL if the change can be achieved
983 * This can be called as part of starting a reshape, or
984 * when assembling an array that is undergoing reshape.
986 int near
, far
, offset
, copies
;
988 int old_chunk
, new_chunk
;
989 /* delta_parity records change in number of devices
990 * caused by level change
992 int delta_parity
= 0;
994 memset(re
, 0, sizeof(*re
));
996 /* If a new level not explicitly given, we assume no-change */
997 if (info
->new_level
== UnSet
)
998 info
->new_level
= info
->array
.level
;
1000 if (info
->new_chunk
)
1001 switch (info
->new_level
) {
1007 /* chunk size is meaningful, must divide component_size
1010 if (info
->component_size
% (info
->new_chunk
/512)) {
1011 unsigned long long shrink
= info
->component_size
;
1012 shrink
&= ~(unsigned long long)(info
->new_chunk
/512-1);
1013 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk)\n",
1014 info
->new_chunk
/1024, info
->component_size
/2);
1015 pr_err("After shrinking any filesystem, \"mdadm --grow %s --size %llu\"\n",
1017 pr_err("will shrink the array so the given chunk size would work.\n");
1022 return "chunk size not meaningful for this level";
1025 info
->new_chunk
= info
->array
.chunk_size
;
1027 switch (info
->array
.level
) {
1029 return "Cannot understand this RAID level";
1031 /* RAID1 can convert to RAID1 with different disks, or
1032 * raid5 with 2 disks, or
1035 if (info
->new_level
> 1 &&
1036 (info
->component_size
& 7))
1037 return "Cannot convert RAID1 of this size - "
1038 "reduce size to multiple of 4K first.";
1039 if (info
->new_level
== 0) {
1040 if (info
->delta_disks
!= UnSet
&&
1041 info
->delta_disks
!= 0)
1042 return "Cannot change number of disks "
1043 "with RAID1->RAID0 conversion";
1045 re
->before
.data_disks
= 1;
1046 re
->after
.data_disks
= 1;
1049 if (info
->new_level
== 1) {
1050 if (info
->delta_disks
== UnSet
)
1051 /* Don't know what to do */
1052 return "no change requested for Growing RAID1";
1056 if (info
->array
.raid_disks
== 2 &&
1057 info
->new_level
== 5) {
1060 re
->before
.data_disks
= 1;
1061 if (info
->delta_disks
!= UnSet
&&
1062 info
->delta_disks
!= 0)
1063 re
->after
.data_disks
= 1 + info
->delta_disks
;
1065 re
->after
.data_disks
= 1;
1066 if (re
->after
.data_disks
< 1)
1067 return "Number of disks too small for RAID5";
1069 re
->before
.layout
= ALGORITHM_LEFT_SYMMETRIC
;
1070 info
->array
.chunk_size
= 65536;
1073 /* Could do some multi-stage conversions, but leave that to
1076 return "Impossibly level change request for RAID1";
1079 /* RAID10 can be converted from near mode to
1080 * RAID0 by removing some devices.
1081 * It can also be reshaped if the kernel supports
1084 switch (info
->new_level
) {
1086 if ((info
->array
.layout
& ~0xff) != 0x100)
1087 return "Cannot Grow RAID10 with far/offset layout";
1088 /* number of devices must be multiple of number of copies */
1089 if (info
->array
.raid_disks
% (info
->array
.layout
& 0xff))
1090 return "RAID10 layout too complex for Grow operation";
1092 new_disks
= (info
->array
.raid_disks
1093 / (info
->array
.layout
& 0xff));
1094 if (info
->delta_disks
== UnSet
)
1095 info
->delta_disks
= (new_disks
1096 - info
->array
.raid_disks
);
1098 if (info
->delta_disks
!= new_disks
- info
->array
.raid_disks
)
1099 return "New number of raid-devices impossible for RAID10";
1100 if (info
->new_chunk
&&
1101 info
->new_chunk
!= info
->array
.chunk_size
)
1102 return "Cannot change chunk-size with RAID10 Grow";
1106 re
->before
.data_disks
= new_disks
;
1107 re
->after
.data_disks
= re
->before
.data_disks
;
1111 near
= info
->array
.layout
& 0xff;
1112 far
= (info
->array
.layout
>> 8) & 0xff;
1113 offset
= info
->array
.layout
& 0x10000;
1114 if (far
> 1 && !offset
)
1115 return "Cannot reshape RAID10 in far-mode";
1116 copies
= near
* far
;
1118 old_chunk
= info
->array
.chunk_size
* far
;
1120 if (info
->new_layout
== UnSet
)
1121 info
->new_layout
= info
->array
.layout
;
1123 near
= info
->new_layout
& 0xff;
1124 far
= (info
->new_layout
>> 8) & 0xff;
1125 offset
= info
->new_layout
& 0x10000;
1126 if (far
> 1 && !offset
)
1127 return "Cannot reshape RAID10 to far-mode";
1128 if (near
* far
!= copies
)
1129 return "Cannot change number of copies"
1130 " when reshaping RAID10";
1132 if (info
->delta_disks
== UnSet
)
1133 info
->delta_disks
= 0;
1134 new_disks
= (info
->array
.raid_disks
+
1137 new_chunk
= info
->new_chunk
* far
;
1140 re
->before
.layout
= info
->array
.layout
;
1141 re
->before
.data_disks
= info
->array
.raid_disks
;
1142 re
->after
.layout
= info
->new_layout
;
1143 re
->after
.data_disks
= new_disks
;
1144 /* For RAID10 we don't do backup but do allow reshape,
1145 * so set backup_blocks to INVALID_SECTORS rather than
1147 * And there is no need to synchronise stripes on both
1148 * 'old' and 'new'. So the important
1149 * number is the minimum data_offset difference
1150 * which is the larger of (offset copies * chunk).
1152 re
->backup_blocks
= INVALID_SECTORS
;
1153 re
->min_offset_change
= max(old_chunk
, new_chunk
) / 512;
1154 if (new_disks
< re
->before
.data_disks
&&
1155 info
->space_after
< re
->min_offset_change
)
1156 /* Reduce component size by one chunk */
1157 re
->new_size
= (info
->component_size
-
1158 re
->min_offset_change
);
1160 re
->new_size
= info
->component_size
;
1161 re
->new_size
= re
->new_size
* new_disks
/ copies
;
1165 return "RAID10 can only be changed to RAID0";
1168 /* RAID0 can be converted to RAID10, or to RAID456 */
1169 if (info
->new_level
== 10) {
1170 if (info
->new_layout
== UnSet
&& info
->delta_disks
== UnSet
) {
1171 /* Assume near=2 layout */
1172 info
->new_layout
= 0x102;
1173 info
->delta_disks
= info
->array
.raid_disks
;
1175 if (info
->new_layout
== UnSet
) {
1176 int copies
= 1 + (info
->delta_disks
1177 / info
->array
.raid_disks
);
1178 if (info
->array
.raid_disks
* (copies
-1)
1179 != info
->delta_disks
)
1180 return "Impossible number of devices"
1181 " for RAID0->RAID10";
1182 info
->new_layout
= 0x100 + copies
;
1184 if (info
->delta_disks
== UnSet
) {
1185 int copies
= info
->new_layout
& 0xff;
1186 if (info
->new_layout
!= 0x100 + copies
)
1187 return "New layout impossible"
1188 " for RAID0->RAID10";;
1189 info
->delta_disks
= (copies
- 1) *
1190 info
->array
.raid_disks
;
1192 if (info
->new_chunk
&&
1193 info
->new_chunk
!= info
->array
.chunk_size
)
1194 return "Cannot change chunk-size with RAID0->RAID10";
1197 re
->before
.data_disks
= (info
->array
.raid_disks
+
1199 re
->after
.data_disks
= re
->before
.data_disks
;
1200 re
->before
.layout
= info
->new_layout
;
1204 /* RAID0 can also covert to RAID0/4/5/6 by first converting to
1205 * a raid4 style layout of the final level.
1207 switch (info
->new_level
) {
1212 re
->before
.layout
= 0;
1217 re
->before
.layout
= ALGORITHM_PARITY_N
;
1218 if (info
->new_layout
== UnSet
)
1219 info
->new_layout
= map_name(r5layout
, "default");
1224 re
->before
.layout
= ALGORITHM_PARITY_N
;
1225 if (info
->new_layout
== UnSet
)
1226 info
->new_layout
= map_name(r6layout
, "default");
1229 return "Impossible level change requested";
1231 re
->before
.data_disks
= info
->array
.raid_disks
;
1232 /* determining 'after' layout happens outside this 'switch' */
1236 info
->array
.layout
= ALGORITHM_PARITY_N
;
1238 switch (info
->new_level
) {
1242 re
->level
= info
->array
.level
;
1243 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1244 re
->before
.layout
= info
->array
.layout
;
1248 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1249 re
->before
.layout
= info
->array
.layout
;
1254 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1255 switch (info
->array
.layout
) {
1256 case ALGORITHM_LEFT_ASYMMETRIC
:
1257 re
->before
.layout
= ALGORITHM_LEFT_ASYMMETRIC_6
;
1259 case ALGORITHM_RIGHT_ASYMMETRIC
:
1260 re
->before
.layout
= ALGORITHM_RIGHT_ASYMMETRIC_6
;
1262 case ALGORITHM_LEFT_SYMMETRIC
:
1263 re
->before
.layout
= ALGORITHM_LEFT_SYMMETRIC_6
;
1265 case ALGORITHM_RIGHT_SYMMETRIC
:
1266 re
->before
.layout
= ALGORITHM_RIGHT_SYMMETRIC_6
;
1268 case ALGORITHM_PARITY_0
:
1269 re
->before
.layout
= ALGORITHM_PARITY_0_6
;
1271 case ALGORITHM_PARITY_N
:
1272 re
->before
.layout
= ALGORITHM_PARITY_N_6
;
1275 return "Cannot convert an array with this layout";
1279 if (info
->array
.raid_disks
!= 2)
1280 return "Can only convert a 2-device array to RAID1";
1281 if (info
->delta_disks
!= UnSet
&&
1282 info
->delta_disks
!= 0)
1283 return "Cannot set raid_disk when "
1284 "converting RAID5->RAID1";
1286 info
->new_chunk
= 0;
1289 return "Impossible level change requested";
1293 switch (info
->new_level
) {
1299 re
->before
.data_disks
= info
->array
.raid_disks
- 2;
1300 re
->before
.layout
= info
->array
.layout
;
1303 return "Impossible level change requested";
1308 /* If we reached here then it looks like a re-stripe is
1309 * happening. We have determined the intermediate level
1310 * and initial raid_disks/layout and stored these in 're'.
1312 * We need to deduce the final layout that can be atomically
1313 * converted to the end state.
1315 switch (info
->new_level
) {
1317 /* We can only get to RAID0 from RAID4 or RAID5
1318 * with appropriate layout and one extra device
1320 if (re
->level
!= 4 && re
->level
!= 5)
1321 return "Cannot covert to RAID0 from this level";
1323 switch (re
->level
) {
1325 re
->after
.layout
= 0;
1328 re
->after
.layout
= ALGORITHM_PARITY_N
;
1334 /* We can only get to RAID4 from RAID5 */
1335 if (re
->level
!= 4 && re
->level
!= 5)
1336 return "Cannot convert to RAID4 from this level";
1338 switch (re
->level
) {
1340 re
->before
.layout
= 0;
1341 re
->after
.layout
= 0;
1344 re
->after
.layout
= ALGORITHM_PARITY_N
;
1350 /* We get to RAID5 from RAID5 or RAID6 */
1351 if (re
->level
!= 5 && re
->level
!= 6)
1352 return "Cannot convert to RAID5 from this level";
1354 switch (re
->level
) {
1356 if (info
->new_layout
== UnSet
)
1357 re
->after
.layout
= re
->before
.layout
;
1359 re
->after
.layout
= info
->new_layout
;
1362 if (info
->new_layout
== UnSet
)
1363 info
->new_layout
= re
->before
.layout
;
1365 /* after.layout needs to be raid6 version of new_layout */
1366 if (info
->new_layout
== ALGORITHM_PARITY_N
)
1367 re
->after
.layout
= ALGORITHM_PARITY_N
;
1370 char *ls
= map_num(r5layout
, info
->new_layout
);
1373 /* Current RAID6 layout has a RAID5
1376 strcat(strcpy(layout
, ls
), "-6");
1377 l
= map_name(r6layout
, layout
);
1379 return "Cannot find RAID6 layout"
1382 /* Current RAID6 has no equivalent.
1383 * If it is already a '-6' layout we
1384 * can leave it unchanged, else we must
1387 ls
= map_num(r6layout
, info
->new_layout
);
1389 strcmp(ls
+strlen(ls
)-2, "-6") != 0)
1390 return "Please specify new layout";
1391 l
= info
->new_layout
;
1393 re
->after
.layout
= l
;
1399 /* We must already be at level 6 */
1401 return "Impossible level change";
1402 if (info
->new_layout
== UnSet
)
1403 re
->after
.layout
= info
->array
.layout
;
1405 re
->after
.layout
= info
->new_layout
;
1408 return "Impossible level change requested";
1410 if (info
->delta_disks
== UnSet
)
1411 info
->delta_disks
= delta_parity
;
1413 re
->after
.data_disks
= (re
->before
.data_disks
1416 switch (re
->level
) {
1417 case 6: re
->parity
= 2;
1420 case 5: re
->parity
= 1;
1422 default: re
->parity
= 0;
1425 /* So we have a restripe operation, we need to calculate the number
1426 * of blocks per reshape operation.
1428 re
->new_size
= info
->component_size
* re
->before
.data_disks
;
1429 if (info
->new_chunk
== 0)
1430 info
->new_chunk
= info
->array
.chunk_size
;
1431 if (re
->after
.data_disks
== re
->before
.data_disks
&&
1432 re
->after
.layout
== re
->before
.layout
&&
1433 info
->new_chunk
== info
->array
.chunk_size
) {
1434 /* Nothing to change, can change level immediately. */
1435 re
->level
= info
->new_level
;
1436 re
->backup_blocks
= 0;
1439 if (re
->after
.data_disks
== 1 && re
->before
.data_disks
== 1) {
1440 /* chunk and layout changes make no difference */
1441 re
->level
= info
->new_level
;
1442 re
->backup_blocks
= 0;
1446 if (re
->after
.data_disks
== re
->before
.data_disks
&&
1447 get_linux_version() < 2006032)
1448 return "in-place reshape is not safe before 2.6.32 - sorry.";
1450 if (re
->after
.data_disks
< re
->before
.data_disks
&&
1451 get_linux_version() < 2006030)
1452 return "reshape to fewer devices is not supported before 2.6.30 - sorry.";
1454 re
->backup_blocks
= compute_backup_blocks(
1455 info
->new_chunk
, info
->array
.chunk_size
,
1456 re
->after
.data_disks
,
1457 re
->before
.data_disks
);
1458 re
->min_offset_change
= re
->backup_blocks
/ re
->before
.data_disks
;
1460 re
->new_size
= info
->component_size
* re
->after
.data_disks
;
1464 static int set_array_size(struct supertype
*st
, struct mdinfo
*sra
,
1467 struct mdinfo
*info
;
1471 if ((st
== NULL
) || (sra
== NULL
))
1474 if (text_version
== NULL
)
1475 text_version
= sra
->text_version
;
1476 subarray
= strchr(text_version
+1, '/')+1;
1477 info
= st
->ss
->container_content(st
, subarray
);
1479 unsigned long long current_size
= 0;
1480 unsigned long long new_size
=
1481 info
->custom_array_size
/2;
1483 if (sysfs_get_ll(sra
, NULL
, "array_size", ¤t_size
) == 0 &&
1484 new_size
> current_size
) {
1485 if (sysfs_set_num(sra
, NULL
, "array_size", new_size
)
1487 dprintf("Error: Cannot set array size");
1490 dprintf("Array size changed");
1492 dprintf(" from %llu to %llu.\n",
1493 current_size
, new_size
);
1497 dprintf("Error: set_array_size(): info pointer in NULL\n");
1502 static int reshape_array(char *container
, int fd
, char *devname
,
1503 struct supertype
*st
, struct mdinfo
*info
,
1504 int force
, struct mddev_dev
*devlist
,
1505 unsigned long long data_offset
,
1506 char *backup_file
, int verbose
, int forked
,
1507 int restart
, int freeze_reshape
);
1508 static int reshape_container(char *container
, char *devname
,
1510 struct supertype
*st
,
1511 struct mdinfo
*info
,
1513 char *backup_file
, int verbose
,
1514 int forked
, int restart
, int freeze_reshape
);
1516 int Grow_reshape(char *devname
, int fd
,
1517 struct mddev_dev
*devlist
,
1518 unsigned long long data_offset
,
1519 struct context
*c
, struct shape
*s
)
1521 /* Make some changes in the shape of an array.
1522 * The kernel must support the change.
1524 * There are three different changes. Each can trigger
1525 * a resync or recovery so we freeze that until we have
1526 * requested everything (if kernel supports freezing - 2.6.30).
1528 * - change size (i.e. component_size)
1530 * - change layout/chunksize/ndisks
1532 * The last can require a reshape. It is different on different
1533 * levels so we need to check the level before actioning it.
1534 * Some times the level change needs to be requested after the
1535 * reshape (e.g. raid6->raid5, raid5->raid0)
1538 struct mdu_array_info_s array
;
1540 struct supertype
*st
;
1541 char *subarray
= NULL
;
1545 char *container
= NULL
;
1548 struct mddev_dev
*dv
;
1554 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) < 0) {
1555 pr_err("%s is not an active md array - aborting\n",
1559 if (data_offset
!= INVALID_SECTORS
&& array
.level
!= 10
1560 && (array
.level
< 4 || array
.level
> 6)) {
1561 pr_err("--grow --data-offset not yet supported\n");
1566 (s
->chunk
|| s
->level
!= UnSet
|| s
->layout_str
|| s
->raiddisks
)) {
1567 pr_err("cannot change component size at the same time "
1568 "as other changes.\n"
1569 " Change size first, then check data is intact before "
1570 "making other changes.\n");
1574 if (s
->raiddisks
&& s
->raiddisks
< array
.raid_disks
&& array
.level
> 1 &&
1575 get_linux_version() < 2006032 &&
1576 !check_env("MDADM_FORCE_FEWER")) {
1577 pr_err("reducing the number of devices is not safe before Linux 2.6.32\n"
1578 " Please use a newer kernel\n");
1582 st
= super_by_fd(fd
, &subarray
);
1584 pr_err("Unable to determine metadata format for %s\n", devname
);
1587 if (s
->raiddisks
> st
->max_devs
) {
1588 pr_err("Cannot increase raid-disks on this array"
1589 " beyond %d\n", st
->max_devs
);
1593 /* in the external case we need to check that the requested reshape is
1594 * supported, and perform an initial check that the container holds the
1595 * pre-requisite spare devices (mdmon owns final validation)
1597 if (st
->ss
->external
) {
1601 container
= st
->container_devnm
;
1602 cfd
= open_dev_excl(st
->container_devnm
);
1604 container
= st
->devnm
;
1606 cfd
= open_dev_excl(st
->devnm
);
1610 pr_err("Unable to open container for %s\n",
1616 rv
= st
->ss
->load_container(st
, cfd
, NULL
);
1619 pr_err("Cannot read superblock for %s\n",
1625 /* check if operation is supported for metadata handler */
1626 if (st
->ss
->container_content
) {
1627 struct mdinfo
*cc
= NULL
;
1628 struct mdinfo
*content
= NULL
;
1630 cc
= st
->ss
->container_content(st
, subarray
);
1631 for (content
= cc
; content
; content
= content
->next
) {
1632 int allow_reshape
= 1;
1634 /* check if reshape is allowed based on metadata
1635 * indications stored in content.array.status
1637 if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
))
1639 if (content
->array
.state
1640 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
))
1642 if (!allow_reshape
) {
1643 pr_err("cannot reshape arrays in"
1644 " container with unsupported"
1645 " metadata: %s(%s)\n",
1646 devname
, container
);
1654 if (mdmon_running(container
))
1655 st
->update_tail
= &st
->updates
;
1659 for (dv
= devlist
; dv
; dv
= dv
->next
)
1661 if (s
->raiddisks
> array
.raid_disks
&&
1662 array
.spare_disks
+added_disks
< (s
->raiddisks
- array
.raid_disks
) &&
1664 pr_err("Need %d spare%s to avoid degraded array,"
1665 " and only have %d.\n"
1666 " Use --force to over-ride this check.\n",
1667 s
->raiddisks
- array
.raid_disks
,
1668 s
->raiddisks
- array
.raid_disks
== 1 ? "" : "s",
1669 array
.spare_disks
+ added_disks
);
1673 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
| GET_DISKS
| GET_DEVS
1674 | GET_STATE
| GET_VERSION
);
1676 if (st
->ss
->external
&& subarray
== NULL
) {
1677 array
.level
= LEVEL_CONTAINER
;
1678 sra
->array
.level
= LEVEL_CONTAINER
;
1681 pr_err("failed to read sysfs parameters for %s\n",
1685 frozen
= freeze(st
);
1687 /* freeze() already spewed the reason */
1690 } else if (frozen
< 0) {
1691 pr_err("%s is performing resync/recovery and cannot"
1692 " be reshaped\n", devname
);
1697 /* ========= set size =============== */
1698 if (s
->size
> 0 && (s
->size
== MAX_SIZE
|| s
->size
!= (unsigned)array
.size
)) {
1699 unsigned long long orig_size
= get_component_size(fd
)/2;
1700 unsigned long long min_csize
;
1702 int raid0_takeover
= 0;
1705 orig_size
= (unsigned) array
.size
;
1707 if (orig_size
== 0) {
1708 pr_err("Cannot set device size in this type of array.\n");
1713 if (reshape_super(st
, s
->size
, UnSet
, UnSet
, 0, 0, UnSet
, NULL
,
1714 devname
, APPLY_METADATA_CHANGES
, c
->verbose
> 0)) {
1719 if (st
->ss
->external
) {
1720 /* metadata can have size limitation
1721 * update size value according to metadata information
1723 struct mdinfo
*sizeinfo
=
1724 st
->ss
->container_content(st
, subarray
);
1726 unsigned long long new_size
=
1727 sizeinfo
->custom_array_size
/2;
1728 int data_disks
= get_data_disks(
1729 sizeinfo
->array
.level
,
1730 sizeinfo
->array
.layout
,
1731 sizeinfo
->array
.raid_disks
);
1732 new_size
/= data_disks
;
1733 dprintf("Metadata size correction from %llu to "
1734 "%llu (%llu)\n", orig_size
, new_size
,
1735 new_size
* data_disks
);
1737 sysfs_free(sizeinfo
);
1741 /* Update the size of each member device in case
1742 * they have been resized. This will never reduce
1743 * below the current used-size. The "size" attribute
1744 * understands '0' to mean 'max'.
1748 for (mdi
= sra
->devs
; mdi
; mdi
= mdi
->next
) {
1749 if (sysfs_set_num(sra
, mdi
, "size",
1750 s
->size
== MAX_SIZE
? 0 : s
->size
) < 0) {
1751 /* Probably kernel refusing to let us
1752 * reduce the size - not an error.
1756 if (array
.not_persistent
== 0 &&
1757 array
.major_version
== 0 &&
1758 get_linux_version() < 3001000) {
1759 /* Dangerous to allow size to exceed 2TB */
1760 unsigned long long csize
;
1761 if (sysfs_get_ll(sra
, mdi
, "size", &csize
) == 0) {
1762 if (csize
>= 2ULL*1024*1024*1024)
1763 csize
= 2ULL*1024*1024*1024;
1764 if ((min_csize
== 0 || (min_csize
1771 pr_err("Cannot set size on "
1772 "array members.\n");
1773 goto size_change_error
;
1775 if (min_csize
&& s
->size
> min_csize
) {
1776 pr_err("Cannot safely make this array "
1777 "use more than 2TB per device on this kernel.\n");
1779 goto size_change_error
;
1781 if (min_csize
&& s
->size
== MAX_SIZE
) {
1782 /* Don't let the kernel choose a size - it will get
1785 pr_err("Limited v0.90 array to "
1786 "2TB per device\n");
1787 s
->size
= min_csize
;
1789 if (st
->ss
->external
) {
1790 if (sra
->array
.level
== 0) {
1791 rv
= sysfs_set_str(sra
, NULL
, "level",
1795 /* get array parametes after takeover
1796 * to chane one parameter at time only
1798 rv
= ioctl(fd
, GET_ARRAY_INFO
, &array
);
1801 /* make sure mdmon is
1802 * aware of the new level */
1803 if (!mdmon_running(st
->container_devnm
))
1804 start_mdmon(st
->container_devnm
);
1805 ping_monitor(container
);
1806 if (mdmon_running(st
->container_devnm
) &&
1807 st
->update_tail
== NULL
)
1808 st
->update_tail
= &st
->updates
;
1811 if (s
->size
== MAX_SIZE
)
1813 array
.size
= s
->size
;
1814 if ((unsigned)array
.size
!= s
->size
) {
1815 /* got truncated to 32bit, write to
1816 * component_size instead
1819 rv
= sysfs_set_num(sra
, NULL
,
1820 "component_size", s
->size
);
1824 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
1826 /* manage array size when it is managed externally
1828 if ((rv
== 0) && st
->ss
->external
)
1829 rv
= set_array_size(st
, sra
, sra
->text_version
);
1832 if (raid0_takeover
) {
1833 /* do not recync non-existing parity,
1834 * we will drop it anyway
1836 sysfs_set_str(sra
, NULL
, "sync_action", "frozen");
1837 /* go back to raid0, drop parity disk
1839 sysfs_set_str(sra
, NULL
, "level", "raid0");
1840 ioctl(fd
, GET_ARRAY_INFO
, &array
);
1847 /* restore metadata */
1848 if (reshape_super(st
, orig_size
, UnSet
, UnSet
, 0, 0,
1849 UnSet
, NULL
, devname
,
1850 ROLLBACK_METADATA_CHANGES
,
1853 pr_err("Cannot set device size for %s: %s\n",
1854 devname
, strerror(err
));
1856 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1857 cont_err("Bitmap must be removed before size can be changed\n");
1861 if (s
->assume_clean
) {
1862 /* This will fail on kernels older than 3.0 unless
1863 * a backport has been arranged.
1866 sysfs_set_str(sra
, NULL
, "resync_start", "none") < 0)
1867 pr_err("--assume-clean not supported with --grow on this kernel\n");
1869 ioctl(fd
, GET_ARRAY_INFO
, &array
);
1870 s
->size
= get_component_size(fd
)/2;
1872 s
->size
= array
.size
;
1873 if (c
->verbose
>= 0) {
1874 if (s
->size
== orig_size
)
1875 pr_err("component size of %s "
1876 "unchanged at %lluK\n",
1879 pr_err("component size of %s "
1880 "has been set to %lluK\n",
1884 } else if (array
.level
!= LEVEL_CONTAINER
) {
1885 s
->size
= get_component_size(fd
)/2;
1887 s
->size
= array
.size
;
1890 /* See if there is anything else to do */
1891 if ((s
->level
== UnSet
|| s
->level
== array
.level
) &&
1892 (s
->layout_str
== NULL
) &&
1893 (s
->chunk
== 0 || s
->chunk
== array
.chunk_size
) &&
1894 data_offset
== INVALID_SECTORS
&&
1895 (s
->raiddisks
== 0 || s
->raiddisks
== array
.raid_disks
)) {
1896 /* Nothing more to do */
1897 if (!changed
&& c
->verbose
>= 0)
1898 pr_err("%s: no change requested\n",
1903 /* ========= check for Raid10/Raid1 -> Raid0 conversion ===============
1904 * current implementation assumes that following conditions must be met:
1907 * - near_copies == 2
1909 if ((s
->level
== 0 && array
.level
== 10 && sra
&&
1910 array
.layout
== ((1 << 8) + 2) && !(array
.raid_disks
& 1)) ||
1911 (s
->level
== 0 && array
.level
== 1 && sra
)) {
1913 err
= remove_disks_for_takeover(st
, sra
, array
.layout
);
1915 dprintf(Name
": Array cannot be reshaped\n");
1921 /* Make sure mdmon has seen the device removal
1922 * and updated metadata before we continue with
1926 ping_monitor(container
);
1929 memset(&info
, 0, sizeof(info
));
1931 sysfs_init(&info
, fd
, NULL
);
1932 strcpy(info
.text_version
, sra
->text_version
);
1933 info
.component_size
= s
->size
*2;
1934 info
.new_level
= s
->level
;
1935 info
.new_chunk
= s
->chunk
* 1024;
1936 if (info
.array
.level
== LEVEL_CONTAINER
) {
1937 info
.delta_disks
= UnSet
;
1938 info
.array
.raid_disks
= s
->raiddisks
;
1939 } else if (s
->raiddisks
)
1940 info
.delta_disks
= s
->raiddisks
- info
.array
.raid_disks
;
1942 info
.delta_disks
= UnSet
;
1943 if (s
->layout_str
== NULL
) {
1944 info
.new_layout
= UnSet
;
1945 if (info
.array
.level
== 6 &&
1946 (info
.new_level
== 6 || info
.new_level
== UnSet
) &&
1947 info
.array
.layout
>= 16) {
1948 pr_err("%s has a non-standard layout. If you"
1949 " wish to preserve this\n", devname
);
1950 cont_err("during the reshape, please specify"
1951 " --layout=preserve\n");
1952 cont_err("If you want to change it, specify a"
1953 " layout or use --layout=normalise\n");
1957 } else if (strcmp(s
->layout_str
, "normalise") == 0 ||
1958 strcmp(s
->layout_str
, "normalize") == 0) {
1959 /* If we have a -6 RAID6 layout, remove the '-6'. */
1960 info
.new_layout
= UnSet
;
1961 if (info
.array
.level
== 6 && info
.new_level
== UnSet
) {
1963 strcpy(l
, map_num(r6layout
, info
.array
.layout
));
1964 h
= strrchr(l
, '-');
1965 if (h
&& strcmp(h
, "-6") == 0) {
1967 info
.new_layout
= map_name(r6layout
, l
);
1970 pr_err("%s is only meaningful when reshaping"
1971 " a RAID6 array.\n", s
->layout_str
);
1975 } else if (strcmp(s
->layout_str
, "preserve") == 0) {
1976 /* This means that a non-standard RAID6 layout
1979 * - When reshape a RAID6 (e.g. adding a device)
1980 * which is in a non-standard layout, it is OK
1981 * to preserve that layout.
1982 * - When converting a RAID5 to RAID6, leave it in
1983 * the XXX-6 layout, don't re-layout.
1985 if (info
.array
.level
== 6 && info
.new_level
== UnSet
)
1986 info
.new_layout
= info
.array
.layout
;
1987 else if (info
.array
.level
== 5 && info
.new_level
== 6) {
1989 strcpy(l
, map_num(r5layout
, info
.array
.layout
));
1991 info
.new_layout
= map_name(r6layout
, l
);
1993 pr_err("%s in only meaningful when reshaping"
1994 " to RAID6\n", s
->layout_str
);
1999 int l
= info
.new_level
;
2001 l
= info
.array
.level
;
2004 info
.new_layout
= map_name(r5layout
, s
->layout_str
);
2007 info
.new_layout
= map_name(r6layout
, s
->layout_str
);
2010 info
.new_layout
= parse_layout_10(s
->layout_str
);
2013 info
.new_layout
= parse_layout_faulty(s
->layout_str
);
2016 pr_err("layout not meaningful"
2017 " with this level\n");
2021 if (info
.new_layout
== UnSet
) {
2022 pr_err("layout %s not understood"
2023 " for this level\n",
2030 if (array
.level
== LEVEL_FAULTY
) {
2031 if (s
->level
!= UnSet
&& s
->level
!= array
.level
) {
2032 pr_err("cannot change level of Faulty device\n");
2036 pr_err("cannot set chunksize of Faulty device\n");
2039 if (s
->raiddisks
&& s
->raiddisks
!= 1) {
2040 pr_err("cannot set raid_disks of Faulty device\n");
2043 if (s
->layout_str
) {
2044 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
2045 dprintf("Cannot get array information.\n");
2048 array
.layout
= info
.new_layout
;
2049 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2050 pr_err("failed to set new layout\n");
2052 } else if (c
->verbose
>= 0)
2053 printf("layout for %s set to %d\n",
2054 devname
, array
.layout
);
2056 } else if (array
.level
== LEVEL_CONTAINER
) {
2057 /* This change is to be applied to every array in the
2058 * container. This is only needed when the metadata imposes
2059 * restraints of the various arrays in the container.
2060 * Currently we only know that IMSM requires all arrays
2061 * to have the same number of devices so changing the
2062 * number of devices (On-Line Capacity Expansion) must be
2063 * performed at the level of the container
2065 rv
= reshape_container(container
, devname
, -1, st
, &info
,
2066 c
->force
, c
->backup_file
, c
->verbose
, 0, 0, 0);
2069 /* get spare devices from external metadata
2071 if (st
->ss
->external
) {
2072 struct mdinfo
*info2
;
2074 info2
= st
->ss
->container_content(st
, subarray
);
2076 info
.array
.spare_disks
=
2077 info2
->array
.spare_disks
;
2082 /* Impose these changes on a single array. First
2083 * check that the metadata is OK with the change. */
2085 if (reshape_super(st
, 0, info
.new_level
,
2086 info
.new_layout
, info
.new_chunk
,
2087 info
.array
.raid_disks
, info
.delta_disks
,
2088 c
->backup_file
, devname
, APPLY_METADATA_CHANGES
,
2094 rv
= reshape_array(container
, fd
, devname
, st
, &info
, c
->force
,
2095 devlist
, data_offset
, c
->backup_file
, c
->verbose
,
2106 /* verify_reshape_position()
2107 * Function checks if reshape position in metadata is not farther
2108 * than position in md.
2110 * 0 : not valid sysfs entry
2111 * it can be caused by not started reshape, it should be started
2112 * by reshape array or raid0 array is before takeover
2113 * -1 : error, reshape position is obviously wrong
2114 * 1 : success, reshape progress correct or updated
2116 static int verify_reshape_position(struct mdinfo
*info
, int level
)
2122 /* read sync_max, failure can mean raid0 array */
2123 rv
= sysfs_get_str(info
, NULL
, "sync_max", buf
, 40);
2127 unsigned long long position
= strtoull(buf
, &ep
, 0);
2129 dprintf(Name
": Read sync_max sysfs entry is: %s\n", buf
);
2130 if (!(ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))) {
2131 position
*= get_data_disks(level
,
2133 info
->array
.raid_disks
);
2134 if (info
->reshape_progress
< position
) {
2135 dprintf("Corrected reshape progress (%llu) to "
2136 "md position (%llu)\n",
2137 info
->reshape_progress
, position
);
2138 info
->reshape_progress
= position
;
2140 } else if (info
->reshape_progress
> position
) {
2141 pr_err("Fatal error: array "
2142 "reshape was not properly frozen "
2143 "(expected reshape position is %llu, "
2144 "but reshape progress is %llu.\n",
2145 position
, info
->reshape_progress
);
2148 dprintf("Reshape position in md and metadata "
2153 } else if (rv
== 0) {
2154 /* for valid sysfs entry, 0-length content
2155 * should be indicated as error
2163 static unsigned long long choose_offset(unsigned long long lo
,
2164 unsigned long long hi
,
2165 unsigned long long min
,
2166 unsigned long long max
)
2168 /* Choose a new offset between hi and lo.
2169 * It must be between min and max, but
2170 * we would prefer something near the middle of hi/lo, and also
2171 * prefer to be aligned to a big power of 2.
2173 * So we start with the middle, then for each bit,
2174 * starting at '1' and increasing, if it is set, we either
2175 * add it or subtract it if possible, preferring the option
2176 * which is furthest from the boundary.
2178 * We stop once we get a 1MB alignment. As units are in sectors,
2179 * 1MB = 2*1024 sectors.
2181 unsigned long long choice
= (lo
+ hi
) / 2;
2182 unsigned long long bit
= 1;
2184 for (bit
= 1; bit
< 2*1024; bit
= bit
<< 1) {
2185 unsigned long long bigger
, smaller
;
2186 if (! (bit
& choice
))
2188 bigger
= choice
+ bit
;
2189 smaller
= choice
- bit
;
2190 if (bigger
> max
&& smaller
< min
)
2194 else if (smaller
< min
)
2196 else if (hi
- bigger
> smaller
- lo
)
2204 static int set_new_data_offset(struct mdinfo
*sra
, struct supertype
*st
,
2205 char *devname
, int delta_disks
,
2206 unsigned long long data_offset
,
2207 unsigned long long min
,
2213 unsigned long long before
, after
;
2215 /* Need to find min space before and after so same is used
2218 before
= UINT64_MAX
;
2220 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2224 struct supertype
*st2
;
2225 struct mdinfo info2
;
2227 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2229 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2230 dfd
= dev_open(dn
, O_RDONLY
);
2232 pr_err("%s: cannot open component %s\n",
2233 devname
, dn
? dn
: "-unknown-");
2236 st2
= dup_super(st
);
2237 rv
= st2
->ss
->load_super(st2
,dfd
, NULL
);
2241 pr_err("%s: cannot get superblock from %s\n",
2245 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
2246 st2
->ss
->free_super(st2
);
2248 if (info2
.space_before
== 0 &&
2249 info2
.space_after
== 0) {
2250 /* Metadata doesn't support data_offset changes */
2253 if (before
> info2
.space_before
)
2254 before
= info2
.space_before
;
2255 if (after
> info2
.space_after
)
2256 after
= info2
.space_after
;
2258 if (data_offset
!= INVALID_SECTORS
) {
2260 if (info2
.data_offset
== data_offset
) {
2261 pr_err("%s: already has that data_offset\n",
2265 if (data_offset
< info2
.data_offset
)
2269 } else if ((data_offset
<= info2
.data_offset
&& dir
== 1) ||
2270 (data_offset
>= info2
.data_offset
&& dir
== -1)) {
2271 pr_err("%s: differing data offsets on devices make this --data-offset setting impossible\n",
2277 if (before
== UINT64_MAX
)
2278 /* impossible really, there must be no devices */
2281 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2282 char *dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2283 unsigned long long new_data_offset
;
2285 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2287 if (delta_disks
< 0) {
2288 /* Don't need any space as array is shrinking
2289 * just move data_offset up by min
2291 if (data_offset
== INVALID_SECTORS
)
2292 new_data_offset
= sd
->data_offset
+ min
;
2294 if (data_offset
< sd
->data_offset
+ min
) {
2295 pr_err("--data-offset too small for %s\n",
2299 new_data_offset
= data_offset
;
2301 } else if (delta_disks
> 0) {
2302 /* need space before */
2306 pr_err("Insufficient head-space for reshape on %s\n",
2310 if (data_offset
== INVALID_SECTORS
)
2311 new_data_offset
= sd
->data_offset
- min
;
2313 if (data_offset
> sd
->data_offset
- min
) {
2314 pr_err("--data-offset too large for %s\n",
2318 new_data_offset
= data_offset
;
2322 /* can move up or down. If 'data_offset'
2323 * was set we would have already decided,
2324 * so just choose direction with most space.
2331 sysfs_set_str(sra
, NULL
, "reshape_direction",
2332 dir
== 1 ? "backwards" : "forwards");
2334 /* Increase data offset */
2338 pr_err("Insufficient tail-space for reshape on %s\n",
2342 if (data_offset
!= INVALID_SECTORS
&&
2343 data_offset
< sd
->data_offset
+ min
) {
2344 pr_err("--data-offset too small on %s\n",
2348 if (data_offset
!= INVALID_SECTORS
)
2349 new_data_offset
= data_offset
;
2351 new_data_offset
= choose_offset(sd
->data_offset
,
2352 sd
->data_offset
+ after
,
2353 sd
->data_offset
+ min
,
2354 sd
->data_offset
+ after
);
2356 /* Decrease data offset */
2360 pr_err("insufficient head-room on %s\n",
2364 if (data_offset
!= INVALID_SECTORS
&&
2365 data_offset
< sd
->data_offset
- min
) {
2366 pr_err("--data-offset too small on %s\n",
2370 if (data_offset
!= INVALID_SECTORS
)
2371 new_data_offset
= data_offset
;
2373 new_data_offset
= choose_offset(sd
->data_offset
- before
,
2375 sd
->data_offset
- before
,
2376 sd
->data_offset
- min
);
2379 err
= sysfs_set_num(sra
, sd
, "new_offset", new_data_offset
);
2380 if (err
< 0 && errno
== E2BIG
) {
2381 /* try again after increasing data size to max */
2382 err
= sysfs_set_num(sra
, sd
, "size", 0);
2383 if (err
< 0 && errno
== EINVAL
&&
2384 !(sd
->disk
.state
& (1<<MD_DISK_SYNC
))) {
2385 /* some kernels have a bug where you cannot
2386 * use '0' on spare devices. */
2387 sysfs_set_num(sra
, sd
, "size",
2388 (sra
->component_size
+ after
)/2);
2390 err
= sysfs_set_num(sra
, sd
, "new_offset",
2394 if (errno
== E2BIG
&& data_offset
!= INVALID_SECTORS
) {
2395 pr_err("data-offset is too big for %s\n",
2399 if (sd
== sra
->devs
&&
2400 (errno
== ENOENT
|| errno
== E2BIG
))
2401 /* Early kernel, no 'new_offset' file,
2402 * or kernel doesn't like us.
2403 * For RAID5/6 this is not fatal
2406 pr_err("Cannot set new_offset for %s\n",
2415 /* Just use a backup file */
2419 static int raid10_reshape(char *container
, int fd
, char *devname
,
2420 struct supertype
*st
, struct mdinfo
*info
,
2421 struct reshape
*reshape
,
2422 unsigned long long data_offset
,
2423 int force
, int verbose
)
2425 /* Changing raid_disks, layout, chunksize or possibly
2426 * just data_offset for a RAID10.
2427 * We must always change data_offset. We change by at least
2428 * ->min_offset_change which is the largest of the old and new
2430 * If raid_disks is increasing, then data_offset must decrease
2431 * by at least this copy size.
2432 * If raid_disks is unchanged, data_offset must increase or
2433 * decrease by at least min_offset_change but preferably by much more.
2434 * We choose half of the available space.
2435 * If raid_disks is decreasing, data_offset must increase by
2436 * at least min_offset_change. To allow of this, component_size
2437 * must be decreased by the same amount.
2439 * So we calculate the required minimum and direction, possibly
2440 * reduce the component_size, then iterate through the devices
2441 * and set the new_data_offset.
2442 * If that all works, we set chunk_size, layout, raid_disks, and start
2446 unsigned long long min
;
2449 sra
= sysfs_read(fd
, NULL
,
2450 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
2453 pr_err("%s: Cannot get array details from sysfs\n",
2457 min
= reshape
->min_offset_change
;
2459 if (info
->delta_disks
)
2460 sysfs_set_str(sra
, NULL
, "reshape_direction",
2461 info
->delta_disks
< 0 ? "backwards" : "forwards");
2462 if (info
->delta_disks
< 0 &&
2463 info
->space_after
< min
) {
2464 int rv
= sysfs_set_num(sra
, NULL
, "component_size",
2465 (sra
->component_size
-
2468 pr_err("cannot reduce component size\n");
2472 err
= set_new_data_offset(sra
, st
, devname
, info
->delta_disks
, data_offset
,
2475 pr_err("Cannot set new_data_offset: RAID10 reshape not\n");
2476 cont_err("supported on this kernel\n");
2482 if (!err
&& sysfs_set_num(sra
, NULL
, "chunk_size", info
->new_chunk
) < 0)
2484 if (!err
&& sysfs_set_num(sra
, NULL
, "layout", reshape
->after
.layout
) < 0)
2486 if (!err
&& sysfs_set_num(sra
, NULL
, "raid_disks",
2487 info
->array
.raid_disks
+ info
->delta_disks
) < 0)
2489 if (!err
&& sysfs_set_str(sra
, NULL
, "sync_action", "reshape") < 0)
2492 pr_err("Cannot set array shape for %s\n",
2495 (info
->array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2496 cont_err(" Bitmap must be removed before"
2497 " shape can be changed\n");
2507 static void get_space_after(int fd
, struct supertype
*st
, struct mdinfo
*info
)
2509 struct mdinfo
*sra
, *sd
;
2510 /* Initialisation to silence compiler warning */
2511 unsigned long long min_space_before
= 0, min_space_after
= 0;
2514 sra
= sysfs_read(fd
, NULL
, GET_DEVS
);
2517 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2520 struct supertype
*st2
;
2521 struct mdinfo info2
;
2523 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2525 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2526 dfd
= dev_open(dn
, O_RDONLY
);
2529 st2
= dup_super(st
);
2530 if (st2
->ss
->load_super(st2
,dfd
, NULL
)) {
2536 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
2537 st2
->ss
->free_super(st2
);
2540 min_space_before
> info2
.space_before
)
2541 min_space_before
= info2
.space_before
;
2543 min_space_after
> info2
.space_after
)
2544 min_space_after
= info2
.space_after
;
2547 if (sd
== NULL
&& !first
) {
2548 info
->space_after
= min_space_after
;
2549 info
->space_before
= min_space_before
;
2554 static void update_cache_size(char *container
, struct mdinfo
*sra
,
2555 struct mdinfo
*info
,
2556 int disks
, unsigned long long blocks
)
2558 /* Check that the internal stripe cache is
2559 * large enough, or it won't work.
2560 * It must hold at least 4 stripes of the larger
2563 unsigned long cache
;
2564 cache
= max(info
->array
.chunk_size
, info
->new_chunk
);
2565 cache
*= 4; /* 4 stripes minimum */
2566 cache
/= 512; /* convert to sectors */
2567 /* make sure there is room for 'blocks' with a bit to spare */
2568 if (cache
< 16 + blocks
/ disks
)
2569 cache
= 16 + blocks
/ disks
;
2570 cache
/= (4096/512); /* Covert from sectors to pages */
2572 if (sra
->cache_size
< cache
)
2573 subarray_set_num(container
, sra
, "stripe_cache_size",
2577 static int impose_reshape(struct mdinfo
*sra
,
2578 struct mdinfo
*info
,
2579 struct supertype
*st
,
2582 char *devname
, char *container
,
2583 struct reshape
*reshape
)
2585 struct mdu_array_info_s array
;
2587 sra
->new_chunk
= info
->new_chunk
;
2590 /* for external metadata checkpoint saved by mdmon can be lost
2591 * or missed /due to e.g. crash/. Check if md is not during
2592 * restart farther than metadata points to.
2593 * If so, this means metadata information is obsolete.
2595 if (st
->ss
->external
)
2596 verify_reshape_position(info
, reshape
->level
);
2597 sra
->reshape_progress
= info
->reshape_progress
;
2599 sra
->reshape_progress
= 0;
2600 if (reshape
->after
.data_disks
< reshape
->before
.data_disks
)
2601 /* start from the end of the new array */
2602 sra
->reshape_progress
= (sra
->component_size
2603 * reshape
->after
.data_disks
);
2606 ioctl(fd
, GET_ARRAY_INFO
, &array
);
2607 if (info
->array
.chunk_size
== info
->new_chunk
&&
2608 reshape
->before
.layout
== reshape
->after
.layout
&&
2609 st
->ss
->external
== 0) {
2610 /* use SET_ARRAY_INFO but only if reshape hasn't started */
2611 array
.raid_disks
= reshape
->after
.data_disks
+ reshape
->parity
;
2613 ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2616 pr_err("Cannot set device shape for %s: %s\n",
2617 devname
, strerror(errno
));
2620 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2621 cont_err("Bitmap must be removed before"
2622 " shape can be changed\n");
2626 } else if (!restart
) {
2627 /* set them all just in case some old 'new_*' value
2628 * persists from some earlier problem.
2631 if (sysfs_set_num(sra
, NULL
, "chunk_size", info
->new_chunk
) < 0)
2633 if (!err
&& sysfs_set_num(sra
, NULL
, "layout",
2634 reshape
->after
.layout
) < 0)
2636 if (!err
&& subarray_set_num(container
, sra
, "raid_disks",
2637 reshape
->after
.data_disks
+
2638 reshape
->parity
) < 0)
2641 pr_err("Cannot set device shape for %s\n",
2645 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2646 cont_err("Bitmap must be removed before"
2647 " shape can be changed\n");
2656 static int impose_level(int fd
, int level
, char *devname
, int verbose
)
2659 struct mdu_array_info_s array
;
2661 sysfs_init(&info
, fd
, NULL
);
2663 ioctl(fd
, GET_ARRAY_INFO
, &array
);
2665 (array
.level
>= 4 && array
.level
<= 6)) {
2666 /* To convert to RAID0 we need to fail and
2667 * remove any non-data devices. */
2670 int data_disks
= array
.raid_disks
- 1;
2671 if (array
.level
== 6)
2673 if (array
.level
== 5 &&
2674 array
.layout
!= ALGORITHM_PARITY_N
)
2676 if (array
.level
== 6 &&
2677 array
.layout
!= ALGORITHM_PARITY_N_6
)
2679 sysfs_set_str(&info
, NULL
,"sync_action", "idle");
2680 /* First remove any spares so no recovery starts */
2681 for (d
= 0, found
= 0;
2682 d
< MAX_DISKS
&& found
< array
.nr_disks
;
2684 mdu_disk_info_t disk
;
2686 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
2688 if (disk
.major
== 0 && disk
.minor
== 0)
2691 if ((disk
.state
& (1 << MD_DISK_ACTIVE
))
2692 && disk
.raid_disk
< data_disks
)
2695 ioctl(fd
, HOT_REMOVE_DISK
,
2696 makedev(disk
.major
, disk
.minor
));
2698 /* Now fail anything left */
2699 ioctl(fd
, GET_ARRAY_INFO
, &array
);
2700 for (d
= 0, found
= 0;
2701 d
< MAX_DISKS
&& found
< array
.nr_disks
;
2704 mdu_disk_info_t disk
;
2706 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
2708 if (disk
.major
== 0 && disk
.minor
== 0)
2711 if ((disk
.state
& (1 << MD_DISK_ACTIVE
))
2712 && disk
.raid_disk
< data_disks
)
2715 ioctl(fd
, SET_DISK_FAULTY
,
2716 makedev(disk
.major
, disk
.minor
));
2718 while (ioctl(fd
, HOT_REMOVE_DISK
,
2719 makedev(disk
.major
, disk
.minor
)) < 0
2726 c
= map_num(pers
, level
);
2728 int err
= sysfs_set_str(&info
, NULL
, "level", c
);
2731 pr_err("%s: could not set level to %s\n",
2734 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2735 cont_err("Bitmap must be removed"
2736 " before level can be changed\n");
2740 pr_err("level of %s changed to %s\n",
2747 static void catch_term(int sig
)
2752 static int continue_via_systemd(char *devnm
)
2754 int skipped
, i
, pid
, status
;
2756 /* In a systemd/udev world, it is best to get systemd to
2757 * run "mdadm --grow --continue" rather than running in the
2762 /* FIXME yuk. CLOSE_EXEC?? */
2764 for (i
= 3; skipped
< 20; i
++)
2770 /* Don't want to see error messages from
2771 * systemctl. If the service doesn't exist,
2772 * we fork ourselves.
2775 open("/dev/null", O_WRONLY
);
2776 snprintf(pathbuf
, sizeof(pathbuf
), "mdadm-grow-continue@%s.service",
2778 status
= execl("/usr/bin/systemctl", "systemctl",
2781 status
= execl("/bin/systemctl", "systemctl", "start",
2784 case -1: /* Just do it ourselves. */
2786 default: /* parent - good */
2787 pid
= wait(&status
);
2788 if (pid
>= 0 && status
== 0)
2794 static int reshape_array(char *container
, int fd
, char *devname
,
2795 struct supertype
*st
, struct mdinfo
*info
,
2796 int force
, struct mddev_dev
*devlist
,
2797 unsigned long long data_offset
,
2798 char *backup_file
, int verbose
, int forked
,
2799 int restart
, int freeze_reshape
)
2801 struct reshape reshape
;
2804 int orig_level
= UnSet
;
2808 struct mdu_array_info_s array
;
2811 struct mddev_dev
*dv
;
2815 unsigned long long *offsets
= NULL
;
2819 unsigned long blocks
;
2820 unsigned long long array_size
;
2822 struct mdinfo
*sra
= NULL
;
2824 /* when reshaping a RAID0, the component_size might be zero.
2825 * So try to fix that up.
2827 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
2828 dprintf("Cannot get array information.\n");
2831 if (array
.level
== 0 && info
->component_size
== 0) {
2832 get_dev_size(fd
, NULL
, &array_size
);
2833 info
->component_size
= array_size
/ array
.raid_disks
;
2836 if (array
.level
== 10)
2837 /* Need space_after info */
2838 get_space_after(fd
, st
, info
);
2840 if (info
->reshape_active
) {
2841 int new_level
= info
->new_level
;
2842 info
->new_level
= UnSet
;
2843 if (info
->delta_disks
> 0)
2844 info
->array
.raid_disks
-= info
->delta_disks
;
2845 msg
= analyse_change(devname
, info
, &reshape
);
2846 info
->new_level
= new_level
;
2847 if (info
->delta_disks
> 0)
2848 info
->array
.raid_disks
+= info
->delta_disks
;
2850 /* Make sure the array isn't read-only */
2851 ioctl(fd
, RESTART_ARRAY_RW
, 0);
2853 msg
= analyse_change(devname
, info
, &reshape
);
2855 /* if msg == "", error has already been printed */
2857 pr_err("%s\n", msg
);
2861 (reshape
.level
!= info
->array
.level
||
2862 reshape
.before
.layout
!= info
->array
.layout
||
2863 reshape
.before
.data_disks
+ reshape
.parity
2864 != info
->array
.raid_disks
- max(0, info
->delta_disks
))) {
2865 pr_err("reshape info is not in native format -"
2866 " cannot continue.\n");
2870 if (st
->ss
->external
&& restart
&& (info
->reshape_progress
== 0)) {
2871 /* When reshape is restarted from '0', very begin of array
2872 * it is possible that for external metadata reshape and array
2873 * configuration doesn't happen.
2874 * Check if md has the same opinion, and reshape is restarted
2875 * from 0. If so, this is regular reshape start after reshape
2876 * switch in metadata to next array only.
2878 if ((verify_reshape_position(info
, reshape
.level
) >= 0) &&
2879 (info
->reshape_progress
== 0))
2883 /* reshape already started. just skip to monitoring the reshape */
2884 if (reshape
.backup_blocks
== 0)
2886 if (restart
& RESHAPE_NO_BACKUP
)
2889 /* Need 'sra' down at 'started:' */
2890 sra
= sysfs_read(fd
, NULL
,
2891 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
|
2894 pr_err("%s: Cannot get array details from sysfs\n",
2900 backup_file
= locate_backup(sra
->sys_name
);
2904 /* The container is frozen but the array may not be.
2905 * So freeze the array so spares don't get put to the wrong use
2906 * FIXME there should probably be a cleaner separation between
2907 * freeze_array and freeze_container.
2909 sysfs_freeze_array(info
);
2910 /* Check we have enough spares to not be degraded */
2912 for (dv
= devlist
; dv
; dv
=dv
->next
)
2914 spares_needed
= max(reshape
.before
.data_disks
,
2915 reshape
.after
.data_disks
)
2916 + reshape
.parity
- array
.raid_disks
;
2919 info
->new_level
> 1 && info
->array
.level
> 1 &&
2920 spares_needed
> info
->array
.spare_disks
+ added_disks
) {
2921 pr_err("Need %d spare%s to avoid degraded array,"
2922 " and only have %d.\n"
2923 " Use --force to over-ride this check.\n",
2925 spares_needed
== 1 ? "" : "s",
2926 info
->array
.spare_disks
+ added_disks
);
2929 /* Check we have enough spares to not fail */
2930 spares_needed
= max(reshape
.before
.data_disks
,
2931 reshape
.after
.data_disks
)
2933 if ((info
->new_level
> 1 || info
->new_level
== 0) &&
2934 spares_needed
> info
->array
.spare_disks
+added_disks
) {
2935 pr_err("Need %d spare%s to create working array,"
2936 " and only have %d.\n",
2938 spares_needed
== 1 ? "" : "s",
2939 info
->array
.spare_disks
+ added_disks
);
2943 if (reshape
.level
!= array
.level
) {
2944 int err
= impose_level(fd
, reshape
.level
, devname
, verbose
);
2947 info
->new_layout
= UnSet
; /* after level change,
2948 * layout is meaningless */
2949 orig_level
= array
.level
;
2950 sysfs_freeze_array(info
);
2952 if (reshape
.level
> 0 && st
->ss
->external
) {
2953 /* make sure mdmon is aware of the new level */
2954 if (mdmon_running(container
))
2955 flush_mdmon(container
);
2957 if (!mdmon_running(container
))
2958 start_mdmon(container
);
2959 ping_monitor(container
);
2960 if (mdmon_running(container
) &&
2961 st
->update_tail
== NULL
)
2962 st
->update_tail
= &st
->updates
;
2965 /* ->reshape_super might have chosen some spares from the
2966 * container that it wants to be part of the new array.
2967 * We can collect them with ->container_content and give
2968 * them to the kernel.
2970 if (st
->ss
->reshape_super
&& st
->ss
->container_content
) {
2971 char *subarray
= strchr(info
->text_version
+1, '/')+1;
2972 struct mdinfo
*info2
=
2973 st
->ss
->container_content(st
, subarray
);
2977 sysfs_init(info2
, fd
, st
->devnm
);
2978 /* When increasing number of devices, we need to set
2979 * new raid_disks before adding these, or they might
2982 if (reshape
.backup_blocks
&&
2983 reshape
.after
.data_disks
> reshape
.before
.data_disks
)
2984 subarray_set_num(container
, info2
, "raid_disks",
2985 reshape
.after
.data_disks
+
2987 for (d
= info2
->devs
; d
; d
= d
->next
) {
2988 if (d
->disk
.state
== 0 &&
2989 d
->disk
.raid_disk
>= 0) {
2990 /* This is a spare that wants to
2991 * be part of the array.
2993 add_disk(fd
, st
, info2
, d
);
2999 /* We might have been given some devices to add to the
3000 * array. Now that the array has been changed to the right
3001 * level and frozen, we can safely add them.
3004 Manage_subdevs(devname
, fd
, devlist
, verbose
,
3007 if (reshape
.backup_blocks
== 0 && data_offset
!= INVALID_SECTORS
)
3008 reshape
.backup_blocks
= reshape
.before
.data_disks
* info
->array
.chunk_size
/512;
3009 if (reshape
.backup_blocks
== 0) {
3010 /* No restriping needed, but we might need to impose
3011 * some more changes: layout, raid_disks, chunk_size
3013 /* read current array info */
3014 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
3015 dprintf("Cannot get array information.\n");
3018 /* compare current array info with new values and if
3019 * it is different update them to new */
3020 if (info
->new_layout
!= UnSet
&&
3021 info
->new_layout
!= array
.layout
) {
3022 array
.layout
= info
->new_layout
;
3023 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
3024 pr_err("failed to set new layout\n");
3026 } else if (verbose
>= 0)
3027 printf("layout for %s set to %d\n",
3028 devname
, array
.layout
);
3030 if (info
->delta_disks
!= UnSet
&&
3031 info
->delta_disks
!= 0 &&
3032 array
.raid_disks
!= (info
->array
.raid_disks
+ info
->delta_disks
)) {
3033 array
.raid_disks
+= info
->delta_disks
;
3034 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
3035 pr_err("failed to set raid disks\n");
3037 } else if (verbose
>= 0) {
3038 printf("raid_disks for %s set to %d\n",
3039 devname
, array
.raid_disks
);
3042 if (info
->new_chunk
!= 0 &&
3043 info
->new_chunk
!= array
.chunk_size
) {
3044 if (sysfs_set_num(info
, NULL
,
3045 "chunk_size", info
->new_chunk
) != 0) {
3046 pr_err("failed to set chunk size\n");
3048 } else if (verbose
>= 0)
3049 printf("chunk size for %s set to %d\n",
3050 devname
, array
.chunk_size
);
3057 * There are three possibilities.
3058 * 1/ The array will shrink.
3059 * We need to ensure the reshape will pause before reaching
3060 * the 'critical section'. We also need to fork and wait for
3061 * that to happen. When it does we
3062 * suspend/backup/complete/unfreeze
3064 * 2/ The array will not change size.
3065 * This requires that we keep a backup of a sliding window
3066 * so that we can restore data after a crash. So we need
3067 * to fork and monitor progress.
3068 * In future we will allow the data_offset to change, so
3069 * a sliding backup becomes unnecessary.
3071 * 3/ The array will grow. This is relatively easy.
3072 * However the kernel's restripe routines will cheerfully
3073 * overwrite some early data before it is safe. So we
3074 * need to make a backup of the early parts of the array
3075 * and be ready to restore it if rebuild aborts very early.
3076 * For externally managed metadata, we still need a forked
3077 * child to monitor the reshape and suspend IO over the region
3078 * that is being reshaped.
3080 * We backup data by writing it to one spare, or to a
3081 * file which was given on command line.
3083 * In each case, we first make sure that storage is available
3084 * for the required backup.
3086 * - request the shape change.
3087 * - fork to handle backup etc.
3089 /* Check that we can hold all the data */
3090 get_dev_size(fd
, NULL
, &array_size
);
3091 if (reshape
.new_size
< (array_size
/512)) {
3092 pr_err("this change will reduce the size of the array.\n"
3093 " use --grow --array-size first to truncate array.\n"
3094 " e.g. mdadm --grow %s --array-size %llu\n",
3095 devname
, reshape
.new_size
/2);
3099 if (array
.level
== 10) {
3100 /* Reshaping RAID10 does not require any data backup by
3101 * user-space. Instead it requires that the data_offset
3102 * is changed to avoid the need for backup.
3103 * So this is handled very separately
3106 /* Nothing to do. */
3108 return raid10_reshape(container
, fd
, devname
, st
, info
,
3109 &reshape
, data_offset
,
3112 sra
= sysfs_read(fd
, NULL
,
3113 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
|
3116 pr_err("%s: Cannot get array details from sysfs\n",
3122 switch(set_new_data_offset(sra
, st
, devname
,
3123 reshape
.after
.data_disks
- reshape
.before
.data_disks
,
3125 reshape
.min_offset_change
, 1)) {
3129 /* Updated data_offset, so it's easy now */
3130 update_cache_size(container
, sra
, info
,
3131 min(reshape
.before
.data_disks
,
3132 reshape
.after
.data_disks
),
3133 reshape
.backup_blocks
);
3135 /* Right, everything seems fine. Let's kick things off.
3139 if (impose_reshape(sra
, info
, st
, fd
, restart
,
3140 devname
, container
, &reshape
) < 0)
3142 if (sysfs_set_str(sra
, NULL
, "sync_action", "reshape") < 0) {
3143 pr_err("Failed to initiate reshape!\n");
3146 if (info
->new_level
== reshape
.level
)
3148 /* need to adjust level when reshape completes */
3150 case -1: /* ignore error, but don't wait */
3152 default: /* parent */
3160 fd
= open_dev(sra
->sys_name
);
3162 impose_level(fd
, info
->new_level
, devname
, verbose
);
3164 case 1: /* Couldn't set data_offset, try the old way */
3165 if (data_offset
!= INVALID_SECTORS
) {
3166 pr_err("Cannot update data_offset on this array\n");
3173 /* Decide how many blocks (sectors) for a reshape
3174 * unit. The number we have so far is just a minimum
3176 blocks
= reshape
.backup_blocks
;
3177 if (reshape
.before
.data_disks
==
3178 reshape
.after
.data_disks
) {
3179 /* Make 'blocks' bigger for better throughput, but
3180 * not so big that we reject it below.
3181 * Try for 16 megabytes
3183 while (blocks
* 32 < sra
->component_size
&&
3187 pr_err("Need to backup %luK of critical "
3188 "section..\n", blocks
/2);
3190 if (blocks
>= sra
->component_size
/2) {
3191 pr_err("%s: Something wrong"
3192 " - reshape aborted\n",
3197 /* Now we need to open all these devices so we can read/write.
3199 nrdisks
= max(reshape
.before
.data_disks
,
3200 reshape
.after
.data_disks
) + reshape
.parity
3201 + sra
->array
.spare_disks
;
3202 fdlist
= xcalloc((1+nrdisks
), sizeof(int));
3203 offsets
= xcalloc((1+nrdisks
), sizeof(offsets
[0]));
3205 odisks
= reshape
.before
.data_disks
+ reshape
.parity
;
3206 d
= reshape_prepare_fdlist(devname
, sra
, odisks
,
3207 nrdisks
, blocks
, backup_file
,
3212 if ((st
->ss
->manage_reshape
== NULL
) ||
3213 (st
->ss
->recover_backup
== NULL
)) {
3214 if (backup_file
== NULL
) {
3215 if (reshape
.after
.data_disks
<=
3216 reshape
.before
.data_disks
) {
3217 pr_err("%s: Cannot grow - need backup-file\n",
3219 pr_err(" Please provide one with \"--backup=...\"\n");
3221 } else if (sra
->array
.spare_disks
== 0) {
3222 pr_err("%s: Cannot grow - "
3223 "need a spare or backup-file to backup "
3224 "critical section\n", devname
);
3228 if (!reshape_open_backup_file(backup_file
, fd
, devname
,
3230 fdlist
+d
, offsets
+d
,
3239 update_cache_size(container
, sra
, info
,
3240 min(reshape
.before
.data_disks
, reshape
.after
.data_disks
),
3243 /* Right, everything seems fine. Let's kick things off.
3244 * If only changing raid_disks, use ioctl, else use
3249 if (impose_reshape(sra
, info
, st
, fd
, restart
,
3250 devname
, container
, &reshape
) < 0)
3253 err
= start_reshape(sra
, restart
, reshape
.before
.data_disks
,
3254 reshape
.after
.data_disks
);
3256 pr_err("Cannot %s reshape for %s\n",
3257 restart
? "continue" : "start",
3262 sysfs_set_str(sra
, NULL
, "array_state", "active");
3263 if (freeze_reshape
) {
3267 pr_err("Reshape has to be continued from"
3268 " location %llu when root filesystem has been mounted.\n",
3269 sra
->reshape_progress
);
3273 if (!forked
&& !check_env("MDADM_NO_SYSTEMCTL"))
3274 if (continue_via_systemd(container
?: sra
->sys_name
)) {
3281 /* Now we just need to kick off the reshape and watch, while
3282 * handling backups of the data...
3283 * This is all done by a forked background process.
3285 switch(forked
? 0 : fork()) {
3287 pr_err("Cannot run child to monitor reshape: %s\n",
3301 /* If another array on the same devices is busy, the
3302 * reshape will wait for them. This would mean that
3303 * the first section that we suspend will stay suspended
3304 * for a long time. So check on that possibility
3305 * by looking for "DELAYED" in /proc/mdstat, and if found,
3309 struct mdstat_ent
*mds
, *m
;
3311 mds
= mdstat_read(1, 0);
3312 for (m
= mds
; m
; m
= m
->next
)
3313 if (strcmp(m
->devnm
, sra
->sys_name
) == 0) {
3315 m
->percent
== RESYNC_DELAYED
)
3318 /* Haven't started the reshape thread
3325 if (delayed
== 1 && get_linux_version() < 3007000) {
3326 pr_err("Reshape is delayed, but cannot wait carefully with this kernel.\n"
3327 " You might experience problems until other reshapes complete.\n");
3331 mdstat_wait(30 - (delayed
-1) * 25);
3335 if (check_env("MDADM_GROW_VERIFY"))
3336 fd
= open(devname
, O_RDONLY
| O_DIRECT
);
3339 mlockall(MCL_FUTURE
);
3341 signal(SIGTERM
, catch_term
);
3343 if (st
->ss
->external
) {
3344 /* metadata handler takes it from here */
3345 done
= st
->ss
->manage_reshape(
3346 fd
, sra
, &reshape
, st
, blocks
,
3348 d
- odisks
, fdlist
+odisks
,
3351 done
= child_monitor(
3352 fd
, sra
, &reshape
, st
, blocks
,
3354 d
- odisks
, fdlist
+odisks
,
3360 if (backup_file
&& done
) {
3362 bul
= make_backup(sra
->sys_name
);
3365 int l
= readlink(bul
, buf
, sizeof(buf
));
3373 unlink(backup_file
);
3380 if (!st
->ss
->external
&&
3381 !(reshape
.before
.data_disks
!= reshape
.after
.data_disks
3382 && info
->custom_array_size
) &&
3383 info
->new_level
== reshape
.level
&&
3385 /* no need to wait for the reshape to finish as
3386 * there is nothing more to do.
3393 if (st
->ss
->external
) {
3394 /* Re-load the metadata as much could have changed */
3395 int cfd
= open_dev(st
->container_devnm
);
3397 flush_mdmon(container
);
3398 st
->ss
->free_super(st
);
3399 st
->ss
->load_container(st
, cfd
, container
);
3404 /* set new array size if required customer_array_size is used
3407 if (reshape
.before
.data_disks
!=
3408 reshape
.after
.data_disks
&&
3409 info
->custom_array_size
)
3410 set_array_size(st
, info
, info
->text_version
);
3412 if (info
->new_level
!= reshape
.level
) {
3414 fd
= open(devname
, O_RDONLY
);
3415 impose_level(fd
, info
->new_level
, devname
, verbose
);
3417 if (info
->new_level
== 0)
3418 st
->update_tail
= NULL
;
3430 if (orig_level
!= UnSet
&& sra
) {
3431 c
= map_num(pers
, orig_level
);
3432 if (c
&& sysfs_set_str(sra
, NULL
, "level", c
) == 0)
3433 pr_err("aborting level change\n");
3441 /* mdfd handle is passed to be closed in child process (after fork).
3443 int reshape_container(char *container
, char *devname
,
3445 struct supertype
*st
,
3446 struct mdinfo
*info
,
3448 char *backup_file
, int verbose
,
3449 int forked
, int restart
, int freeze_reshape
)
3451 struct mdinfo
*cc
= NULL
;
3453 char last_devnm
[32] = "";
3455 /* component_size is not meaningful for a container,
3456 * so pass '0' meaning 'no change'
3459 reshape_super(st
, 0, info
->new_level
,
3460 info
->new_layout
, info
->new_chunk
,
3461 info
->array
.raid_disks
, info
->delta_disks
,
3462 backup_file
, devname
, APPLY_METADATA_CHANGES
,
3470 /* ping monitor to be sure that update is on disk
3472 ping_monitor(container
);
3474 if (!forked
&& !check_env("MDADM_NO_SYSTEMCTL"))
3475 if (continue_via_systemd(container
))
3478 switch (forked
? 0 : fork()) {
3479 case -1: /* error */
3480 perror("Cannot fork to complete reshape\n");
3483 default: /* parent */
3484 if (!freeze_reshape
)
3485 printf(Name
": multi-array reshape continues"
3486 " in background\n");
3493 /* close unused handle in child process
3499 /* For each member array with reshape_active,
3500 * we need to perform the reshape.
3501 * We pick the first array that needs reshaping and
3502 * reshape it. reshape_array() will re-read the metadata
3503 * so the next time through a different array should be
3504 * ready for reshape.
3505 * It is possible that the 'different' array will not
3506 * be assembled yet. In that case we simple exit.
3507 * When it is assembled, the mdadm which assembles it
3508 * will take over the reshape.
3510 struct mdinfo
*content
;
3512 struct mdstat_ent
*mdstat
;
3518 cc
= st
->ss
->container_content(st
, NULL
);
3520 for (content
= cc
; content
; content
= content
->next
) {
3522 if (!content
->reshape_active
)
3525 subarray
= strchr(content
->text_version
+1, '/')+1;
3526 mdstat
= mdstat_by_subdev(subarray
, container
);
3529 if (mdstat
->active
== 0) {
3530 pr_err("Skipping inactive array %s.\n",
3532 free_mdstat(mdstat
);
3541 devid
= devnm2devid(mdstat
->devnm
);
3542 adev
= map_dev(major(devid
), minor(devid
), 0);
3544 adev
= content
->text_version
;
3546 fd
= open_dev(mdstat
->devnm
);
3548 printf(Name
": Device %s cannot be opened for reshape.",
3553 if (strcmp(last_devnm
, mdstat
->devnm
) == 0) {
3554 /* Do not allow for multiple reshape_array() calls for
3556 * It can happen when reshape_array() returns without
3557 * error, when reshape is not finished (wrong reshape
3558 * starting/continuation conditions). Mdmon doesn't
3559 * switch to next array in container and reentry
3560 * conditions for the same array occur.
3561 * This is possibly interim until the behaviour of
3562 * reshape_array is resolved().
3564 printf(Name
": Multiple reshape execution detected for "
3565 "device %s.", adev
);
3569 strcpy(last_devnm
, mdstat
->devnm
);
3571 sysfs_init(content
, fd
, mdstat
->devnm
);
3573 if (mdmon_running(container
))
3574 flush_mdmon(container
);
3576 rv
= reshape_array(container
, fd
, adev
, st
,
3577 content
, force
, NULL
, INVALID_SECTORS
,
3578 backup_file
, verbose
, 1, restart
,
3582 if (freeze_reshape
) {
3591 if (mdmon_running(container
))
3592 flush_mdmon(container
);
3601 * We run a child process in the background which performs the following
3603 * - wait for resync to reach a certain point
3604 * - suspend io to the following section
3605 * - backup that section
3606 * - allow resync to proceed further
3608 * - discard the backup.
3610 * When are combined in slightly different ways in the three cases.
3612 * - suspend/backup/allow/wait/resume/discard
3614 * - allow/wait/suspend/backup/allow/wait/resume/discard
3616 * - wait/resume/discard/suspend/backup/allow
3618 * suspend/backup/allow always come together
3619 * wait/resume/discard do too.
3620 * For the same-size case we have two backups to improve flow.
3624 int progress_reshape(struct mdinfo
*info
, struct reshape
*reshape
,
3625 unsigned long long backup_point
,
3626 unsigned long long wait_point
,
3627 unsigned long long *suspend_point
,
3628 unsigned long long *reshape_completed
, int *frozen
)
3630 /* This function is called repeatedly by the reshape manager.
3631 * It determines how much progress can safely be made and allows
3633 * - 'info' identifies the array and particularly records in
3634 * ->reshape_progress the metadata's knowledge of progress
3635 * This is a sector offset from the start of the array
3636 * of the next array block to be relocated. This number
3637 * may increase from 0 or decrease from array_size, depending
3638 * on the type of reshape that is happening.
3639 * Note that in contrast, 'sync_completed' is a block count of the
3640 * reshape so far. It gives the distance between the start point
3641 * (head or tail of device) and the next place that data will be
3642 * written. It always increases.
3643 * - 'reshape' is the structure created by analyse_change
3644 * - 'backup_point' shows how much the metadata manager has backed-up
3645 * data. For reshapes with increasing progress, it is the next address
3646 * to be backed up, previous addresses have been backed-up. For
3647 * decreasing progress, it is the earliest address that has been
3648 * backed up - later address are also backed up.
3649 * So addresses between reshape_progress and backup_point are
3650 * backed up providing those are in the 'correct' order.
3651 * - 'wait_point' is an array address. When reshape_completed
3652 * passes this point, progress_reshape should return. It might
3653 * return earlier if it determines that ->reshape_progress needs
3654 * to be updated or further backup is needed.
3655 * - suspend_point is maintained by progress_reshape and the caller
3656 * should not touch it except to initialise to zero.
3657 * It is an array address and it only increases in 2.6.37 and earlier.
3658 * This makes it difficult to handle reducing reshapes with
3659 * external metadata.
3660 * However: it is similar to backup_point in that it records the
3661 * other end of a suspended region from reshape_progress.
3662 * it is moved to extend the region that is safe to backup and/or
3664 * - reshape_completed is read from sysfs and returned. The caller
3665 * should copy this into ->reshape_progress when it has reason to
3666 * believe that the metadata knows this, and any backup outside this
3670 * 1 if more data from backup_point - but only as far as suspend_point,
3671 * should be backed up
3672 * 0 if things are progressing smoothly
3673 * -1 if the reshape is finished because it is all done,
3674 * -2 if the reshape is finished due to an error.
3677 int advancing
= (reshape
->after
.data_disks
3678 >= reshape
->before
.data_disks
);
3679 unsigned long long need_backup
; /* All data between start of array and
3680 * here will at some point need to
3683 unsigned long long read_offset
, write_offset
;
3684 unsigned long long write_range
;
3685 unsigned long long max_progress
, target
, completed
;
3686 unsigned long long array_size
= (info
->component_size
3687 * reshape
->before
.data_disks
);
3691 /* First, we unsuspend any region that is now known to be safe.
3692 * If suspend_point is on the 'wrong' side of reshape_progress, then
3693 * we don't have or need suspension at the moment. This is true for
3694 * native metadata when we don't need to back-up.
3697 if (info
->reshape_progress
<= *suspend_point
)
3698 sysfs_set_num(info
, NULL
, "suspend_lo",
3699 info
->reshape_progress
);
3701 /* Note: this won't work in 2.6.37 and before.
3702 * Something somewhere should make sure we don't need it!
3704 if (info
->reshape_progress
>= *suspend_point
)
3705 sysfs_set_num(info
, NULL
, "suspend_hi",
3706 info
->reshape_progress
);
3709 /* Now work out how far it is safe to progress.
3710 * If the read_offset for ->reshape_progress is less than
3711 * 'blocks' beyond the write_offset, we can only progress as far
3713 * Otherwise we can progress until the write_offset for the new location
3714 * reaches (within 'blocks' of) the read_offset at the current location.
3715 * However that region must be suspended unless we are using native
3717 * If we need to suspend more, we limit it to 128M per device, which is
3718 * rather arbitrary and should be some time-based calculation.
3720 read_offset
= info
->reshape_progress
/ reshape
->before
.data_disks
;
3721 write_offset
= info
->reshape_progress
/ reshape
->after
.data_disks
;
3722 write_range
= info
->new_chunk
/512;
3723 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
3724 need_backup
= array_size
;
3726 need_backup
= reshape
->backup_blocks
;
3728 if (read_offset
< write_offset
+ write_range
)
3729 max_progress
= backup_point
;
3733 reshape
->after
.data_disks
;
3735 if (read_offset
> write_offset
- write_range
)
3736 /* Can only progress as far as has been backed up,
3737 * which must be suspended */
3738 max_progress
= backup_point
;
3739 else if (info
->reshape_progress
<= need_backup
)
3740 max_progress
= backup_point
;
3742 if (info
->array
.major_version
>= 0)
3743 /* Can progress until backup is needed */
3744 max_progress
= need_backup
;
3746 /* Can progress until metadata update is required */
3749 reshape
->after
.data_disks
;
3750 /* but data must be suspended */
3751 if (max_progress
< *suspend_point
)
3752 max_progress
= *suspend_point
;
3757 /* We know it is safe to progress to 'max_progress' providing
3758 * it is suspended or we are using native metadata.
3759 * Consider extending suspend_point 128M per device if it
3760 * is less than 64M per device beyond reshape_progress.
3761 * But always do a multiple of 'blocks'
3762 * FIXME this is too big - it takes to long to complete
3765 target
= 64*1024*2 * min(reshape
->before
.data_disks
,
3766 reshape
->after
.data_disks
);
3767 target
/= reshape
->backup_blocks
;
3770 target
*= reshape
->backup_blocks
;
3772 /* For externally managed metadata we always need to suspend IO to
3773 * the area being reshaped so we regularly push suspend_point forward.
3774 * For native metadata we only need the suspend if we are going to do
3778 if ((need_backup
> info
->reshape_progress
3779 || info
->array
.major_version
< 0) &&
3780 *suspend_point
< info
->reshape_progress
+ target
) {
3781 if (need_backup
< *suspend_point
+ 2 * target
)
3782 *suspend_point
= need_backup
;
3783 else if (*suspend_point
+ 2 * target
< array_size
)
3784 *suspend_point
+= 2 * target
;
3786 *suspend_point
= array_size
;
3787 sysfs_set_num(info
, NULL
, "suspend_hi", *suspend_point
);
3788 if (max_progress
> *suspend_point
)
3789 max_progress
= *suspend_point
;
3792 if (info
->array
.major_version
>= 0) {
3793 /* Only need to suspend when about to backup */
3794 if (info
->reshape_progress
< need_backup
* 2 &&
3795 *suspend_point
> 0) {
3797 sysfs_set_num(info
, NULL
, "suspend_lo", 0);
3798 sysfs_set_num(info
, NULL
, "suspend_hi", need_backup
);
3801 /* Need to suspend continually */
3802 if (info
->reshape_progress
< *suspend_point
)
3803 *suspend_point
= info
->reshape_progress
;
3804 if (*suspend_point
+ target
< info
->reshape_progress
)
3805 /* No need to move suspend region yet */;
3807 if (*suspend_point
>= 2 * target
)
3808 *suspend_point
-= 2 * target
;
3811 sysfs_set_num(info
, NULL
, "suspend_lo",
3814 if (max_progress
< *suspend_point
)
3815 max_progress
= *suspend_point
;
3819 /* now set sync_max to allow that progress. sync_max, like
3820 * sync_completed is a count of sectors written per device, so
3821 * we find the difference between max_progress and the start point,
3822 * and divide that by after.data_disks to get a sync_max
3824 * At the same time we convert wait_point to a similar number
3825 * for comparing against sync_completed.
3827 /* scale down max_progress to per_disk */
3828 max_progress
/= reshape
->after
.data_disks
;
3829 /* Round to chunk size as some kernels give an erroneously high number */
3830 max_progress
/= info
->new_chunk
/512;
3831 max_progress
*= info
->new_chunk
/512;
3832 /* And round to old chunk size as the kernel wants that */
3833 max_progress
/= info
->array
.chunk_size
/512;
3834 max_progress
*= info
->array
.chunk_size
/512;
3835 /* Limit progress to the whole device */
3836 if (max_progress
> info
->component_size
)
3837 max_progress
= info
->component_size
;
3838 wait_point
/= reshape
->after
.data_disks
;
3840 /* switch from 'device offset' to 'processed block count' */
3841 max_progress
= info
->component_size
- max_progress
;
3842 wait_point
= info
->component_size
- wait_point
;
3846 sysfs_set_num(info
, NULL
, "sync_max", max_progress
);
3848 /* Now wait. If we have already reached the point that we were
3849 * asked to wait to, don't wait at all, else wait for any change.
3850 * We need to select on 'sync_completed' as that is the place that
3851 * notifications happen, but we are really interested in
3852 * 'reshape_position'
3854 fd
= sysfs_get_fd(info
, NULL
, "sync_completed");
3856 goto check_progress
;
3858 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
3859 goto check_progress
;
3861 while (completed
< max_progress
&& completed
< wait_point
) {
3862 /* Check that sync_action is still 'reshape' to avoid
3863 * waiting forever on a dead array
3866 if (sysfs_get_str(info
, NULL
, "sync_action",
3868 strncmp(action
, "reshape", 7) != 0)
3870 /* Some kernels reset 'sync_completed' to zero
3871 * before setting 'sync_action' to 'idle'.
3872 * So we need these extra tests.
3874 if (completed
== 0 && advancing
3875 && info
->reshape_progress
> 0)
3877 if (completed
== 0 && !advancing
3878 && info
->reshape_progress
< (info
->component_size
3879 * reshape
->after
.data_disks
))
3881 sysfs_wait(fd
, NULL
);
3882 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
3883 goto check_progress
;
3885 /* Some kernels reset 'sync_completed' to zero,
3886 * we need to have real point we are in md
3889 completed
= max_progress
;
3891 /* some kernels can give an incorrectly high 'completed' number */
3892 completed
/= (info
->new_chunk
/512);
3893 completed
*= (info
->new_chunk
/512);
3894 /* Convert 'completed' back in to a 'progress' number */
3895 completed
*= reshape
->after
.data_disks
;
3897 completed
= info
->component_size
* reshape
->after
.data_disks
3900 *reshape_completed
= completed
;
3904 /* We return the need_backup flag. Caller will decide
3905 * how much - a multiple of ->backup_blocks up to *suspend_point
3908 return need_backup
> info
->reshape_progress
;
3910 return need_backup
>= info
->reshape_progress
;
3913 /* if we couldn't read a number from sync_completed, then
3914 * either the reshape did complete, or it aborted.
3915 * We can tell which by checking for 'none' in reshape_position.
3916 * If it did abort, then it might immediately restart if it
3917 * it was just a device failure that leaves us degraded but
3921 if (sysfs_get_str(info
, NULL
, "reshape_position", buf
, sizeof(buf
)) < 0
3922 || strncmp(buf
, "none", 4) != 0) {
3923 /* The abort might only be temporary. Wait up to 10
3924 * seconds for fd to contain a valid number again.
3928 unsigned long long new_sync_max
;
3929 while (fd
>= 0 && rv
< 0 && wait
> 0) {
3930 if (sysfs_wait(fd
, &wait
) != 1)
3932 switch (sysfs_fd_get_ll(fd
, &completed
)) {
3934 /* all good again */
3936 /* If "sync_max" is no longer max_progress
3937 * we need to freeze things
3939 sysfs_get_ll(info
, NULL
, "sync_max", &new_sync_max
);
3940 *frozen
= (new_sync_max
!= max_progress
);
3942 case -2: /* read error - abort */
3949 return rv
; /* abort */
3951 /* Maybe racing with array shutdown - check state */
3954 if (sysfs_get_str(info
, NULL
, "array_state", buf
, sizeof(buf
)) < 0
3955 || strncmp(buf
, "inactive", 8) == 0
3956 || strncmp(buf
, "clear",5) == 0)
3957 return -2; /* abort */
3958 return -1; /* complete */
3962 /* FIXME return status is never checked */
3963 static int grow_backup(struct mdinfo
*sra
,
3964 unsigned long long offset
, /* per device */
3965 unsigned long stripes
, /* per device, in old chunks */
3966 int *sources
, unsigned long long *offsets
,
3967 int disks
, int chunk
, int level
, int layout
,
3968 int dests
, int *destfd
, unsigned long long *destoffsets
,
3969 int part
, int *degraded
,
3972 /* Backup 'blocks' sectors at 'offset' on each device of the array,
3973 * to storage 'destfd' (offset 'destoffsets'), after first
3974 * suspending IO. Then allow resync to continue
3975 * over the suspended section.
3976 * Use part 'part' of the backup-super-block.
3981 unsigned long long ll
;
3983 //printf("offset %llu\n", offset);
3989 /* Check that array hasn't become degraded, else we might backup the wrong data */
3990 if (sysfs_get_ll(sra
, NULL
, "degraded", &ll
) < 0)
3991 return -1; /* FIXME this error is ignored */
3992 new_degraded
= (int)ll
;
3993 if (new_degraded
!= *degraded
) {
3994 /* check each device to ensure it is still working */
3996 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
3997 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
3999 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
4001 if (sysfs_get_str(sra
, sd
, "state", sbuf
, 20) < 0 ||
4002 strstr(sbuf
, "faulty") ||
4003 strstr(sbuf
, "in_sync") == NULL
) {
4004 /* this device is dead */
4005 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
4006 if (sd
->disk
.raid_disk
>= 0 &&
4007 sources
[sd
->disk
.raid_disk
] >= 0) {
4008 close(sources
[sd
->disk
.raid_disk
]);
4009 sources
[sd
->disk
.raid_disk
] = -1;
4014 *degraded
= new_degraded
;
4017 bsb
.arraystart2
= __cpu_to_le64(offset
* odata
);
4018 bsb
.length2
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
4020 bsb
.arraystart
= __cpu_to_le64(offset
* odata
);
4021 bsb
.length
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
4024 bsb
.magic
[15] = '2';
4025 for (i
= 0; i
< dests
; i
++)
4027 lseek64(destfd
[i
], destoffsets
[i
] + __le64_to_cpu(bsb
.devstart2
)*512, 0);
4029 lseek64(destfd
[i
], destoffsets
[i
], 0);
4031 rv
= save_stripes(sources
, offsets
,
4032 disks
, chunk
, level
, layout
,
4034 offset
*512*odata
, stripes
* chunk
* odata
,
4039 bsb
.mtime
= __cpu_to_le64(time(0));
4040 for (i
= 0; i
< dests
; i
++) {
4041 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
4043 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
4044 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
4045 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
4046 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
4049 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
] - 4096, 0)
4050 != destoffsets
[i
] - 4096)
4052 if (write(destfd
[i
], &bsb
, 512) != 512)
4054 if (destoffsets
[i
] > 4096) {
4055 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]+stripes
*chunk
*odata
, 0) !=
4056 destoffsets
[i
]+stripes
*chunk
*odata
)
4058 if (write(destfd
[i
], &bsb
, 512) != 512)
4068 /* in 2.6.30, the value reported by sync_completed can be
4069 * less that it should be by one stripe.
4070 * This only happens when reshape hits sync_max and pauses.
4071 * So allow wait_backup to either extent sync_max further
4072 * than strictly necessary, or return before the
4073 * sync has got quite as far as we would really like.
4074 * This is what 'blocks2' is for.
4075 * The various caller give appropriate values so that
4078 /* FIXME return value is often ignored */
4079 static int forget_backup(int dests
, int *destfd
,
4080 unsigned long long *destoffsets
,
4084 * Erase backup 'part' (which is 0 or 1)
4090 bsb
.arraystart2
= __cpu_to_le64(0);
4091 bsb
.length2
= __cpu_to_le64(0);
4093 bsb
.arraystart
= __cpu_to_le64(0);
4094 bsb
.length
= __cpu_to_le64(0);
4096 bsb
.mtime
= __cpu_to_le64(time(0));
4098 for (i
= 0; i
< dests
; i
++) {
4099 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
4100 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
4101 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
4102 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
4103 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
4104 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]-4096, 0) !=
4105 destoffsets
[i
]-4096)
4108 write(destfd
[i
], &bsb
, 512) != 512)
4115 static void fail(char *msg
)
4118 rv
= (write(2, msg
, strlen(msg
)) != (int)strlen(msg
));
4119 rv
|= (write(2, "\n", 1) != 1);
4123 static char *abuf
, *bbuf
;
4124 static unsigned long long abuflen
;
4125 static void validate(int afd
, int bfd
, unsigned long long offset
)
4127 /* check that the data in the backup against the array.
4128 * This is only used for regression testing and should not
4129 * be used while the array is active
4133 lseek64(bfd
, offset
- 4096, 0);
4134 if (read(bfd
, &bsb2
, 512) != 512)
4135 fail("cannot read bsb");
4136 if (bsb2
.sb_csum
!= bsb_csum((char*)&bsb2
,
4137 ((char*)&bsb2
.sb_csum
)-((char*)&bsb2
)))
4138 fail("first csum bad");
4139 if (memcmp(bsb2
.magic
, "md_backup_data", 14) != 0)
4140 fail("magic is bad");
4141 if (memcmp(bsb2
.magic
, "md_backup_data-2", 16) == 0 &&
4142 bsb2
.sb_csum2
!= bsb_csum((char*)&bsb2
,
4143 ((char*)&bsb2
.sb_csum2
)-((char*)&bsb2
)))
4144 fail("second csum bad");
4146 if (__le64_to_cpu(bsb2
.devstart
)*512 != offset
)
4147 fail("devstart is wrong");
4150 unsigned long long len
= __le64_to_cpu(bsb2
.length
)*512;
4152 if (abuflen
< len
) {
4156 if (posix_memalign((void**)&abuf
, 4096, abuflen
) ||
4157 posix_memalign((void**)&bbuf
, 4096, abuflen
)) {
4159 /* just stop validating on mem-alloc failure */
4164 lseek64(bfd
, offset
, 0);
4165 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
) {
4166 //printf("len %llu\n", len);
4167 fail("read first backup failed");
4169 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart
)*512, 0);
4170 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
4171 fail("read first from array failed");
4172 if (memcmp(bbuf
, abuf
, len
) != 0) {
4175 printf("offset=%llu len=%llu\n",
4176 (unsigned long long)__le64_to_cpu(bsb2
.arraystart
)*512, len
);
4177 for (i
=0; i
<len
; i
++)
4178 if (bbuf
[i
] != abuf
[i
]) {
4179 printf("first diff byte %d\n", i
);
4183 fail("data1 compare failed");
4187 unsigned long long len
= __le64_to_cpu(bsb2
.length2
)*512;
4189 if (abuflen
< len
) {
4193 abuf
= xmalloc(abuflen
);
4194 bbuf
= xmalloc(abuflen
);
4197 lseek64(bfd
, offset
+__le64_to_cpu(bsb2
.devstart2
)*512, 0);
4198 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
)
4199 fail("read second backup failed");
4200 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart2
)*512, 0);
4201 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
4202 fail("read second from array failed");
4203 if (memcmp(bbuf
, abuf
, len
) != 0)
4204 fail("data2 compare failed");
4208 int child_monitor(int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
4209 struct supertype
*st
, unsigned long blocks
,
4210 int *fds
, unsigned long long *offsets
,
4211 int dests
, int *destfd
, unsigned long long *destoffsets
)
4213 /* Monitor a reshape where backup is being performed using
4214 * 'native' mechanism - either to a backup file, or
4215 * to some space in a spare.
4219 unsigned long long speed
;
4220 unsigned long long suspend_point
, array_size
;
4221 unsigned long long backup_point
, wait_point
;
4222 unsigned long long reshape_completed
;
4224 int increasing
= reshape
->after
.data_disks
>= reshape
->before
.data_disks
;
4225 int part
= 0; /* The next part of the backup area to fill. It may already
4226 * be full, so we need to check */
4227 int level
= reshape
->level
;
4228 int layout
= reshape
->before
.layout
;
4229 int data
= reshape
->before
.data_disks
;
4230 int disks
= reshape
->before
.data_disks
+ reshape
->parity
;
4231 int chunk
= sra
->array
.chunk_size
;
4233 unsigned long stripes
;
4237 /* set up the backup-super-block. This requires the
4238 * uuid from the array.
4240 /* Find a superblock */
4241 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
4245 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
4247 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
4248 devfd
= dev_open(dn
, O_RDONLY
);
4251 ok
= st
->ss
->load_super(st
, devfd
, NULL
);
4257 pr_err("Cannot find a superblock\n");
4261 memset(&bsb
, 0, 512);
4262 memcpy(bsb
.magic
, "md_backup_data-1", 16);
4263 st
->ss
->uuid_from_super(st
, uuid
);
4264 memcpy(bsb
.set_uuid
, uuid
, 16);
4265 bsb
.mtime
= __cpu_to_le64(time(0));
4266 bsb
.devstart2
= blocks
;
4268 stripes
= blocks
/ (sra
->array
.chunk_size
/512) /
4269 reshape
->before
.data_disks
;
4271 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
4272 /* Don't start the 'reshape' */
4274 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
) {
4275 sysfs_get_ll(sra
, NULL
, "sync_speed_min", &speed
);
4276 sysfs_set_num(sra
, NULL
, "sync_speed_min", 200000);
4280 array_size
= sra
->component_size
* reshape
->after
.data_disks
;
4281 backup_point
= sra
->reshape_progress
;
4284 array_size
= sra
->component_size
* reshape
->before
.data_disks
;
4285 backup_point
= reshape
->backup_blocks
;
4286 suspend_point
= array_size
;
4292 /* Want to return as soon the oldest backup slot can
4293 * be released as that allows us to start backing up
4294 * some more, providing suspend_point has been
4295 * advanced, which it should have.
4298 wait_point
= array_size
;
4299 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
4300 wait_point
= (__le64_to_cpu(bsb
.arraystart
) +
4301 __le64_to_cpu(bsb
.length
));
4302 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
4303 wait_point
= (__le64_to_cpu(bsb
.arraystart2
) +
4304 __le64_to_cpu(bsb
.length2
));
4307 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
4308 wait_point
= __le64_to_cpu(bsb
.arraystart
);
4309 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
4310 wait_point
= __le64_to_cpu(bsb
.arraystart2
);
4313 reshape_completed
= sra
->reshape_progress
;
4314 rv
= progress_reshape(sra
, reshape
,
4315 backup_point
, wait_point
,
4316 &suspend_point
, &reshape_completed
,
4318 /* external metadata would need to ping_monitor here */
4319 sra
->reshape_progress
= reshape_completed
;
4321 /* Clear any backup region that is before 'here' */
4323 if (__le64_to_cpu(bsb
.length
) > 0 &&
4324 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart
) +
4325 __le64_to_cpu(bsb
.length
)))
4326 forget_backup(dests
, destfd
,
4328 if (__le64_to_cpu(bsb
.length2
) > 0 &&
4329 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart2
) +
4330 __le64_to_cpu(bsb
.length2
)))
4331 forget_backup(dests
, destfd
,
4334 if (__le64_to_cpu(bsb
.length
) > 0 &&
4335 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart
)))
4336 forget_backup(dests
, destfd
,
4338 if (__le64_to_cpu(bsb
.length2
) > 0 &&
4339 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart2
)))
4340 forget_backup(dests
, destfd
,
4350 if (rv
== 0 && increasing
&& !st
->ss
->external
) {
4351 /* No longer need to monitor this reshape */
4352 sysfs_set_str(sra
, NULL
, "sync_max", "max");
4358 unsigned long long offset
;
4359 unsigned long actual_stripes
;
4360 /* Need to backup some data.
4361 * If 'part' is not used and the desired
4362 * backup size is suspended, do a backup,
4363 * then consider the next part.
4365 /* Check that 'part' is unused */
4366 if (part
== 0 && __le64_to_cpu(bsb
.length
) != 0)
4368 if (part
== 1 && __le64_to_cpu(bsb
.length2
) != 0)
4371 offset
= backup_point
/ data
;
4372 actual_stripes
= stripes
;
4374 if (offset
+ actual_stripes
* (chunk
/512) >
4375 sra
->component_size
)
4376 actual_stripes
= ((sra
->component_size
- offset
)
4378 if (offset
+ actual_stripes
* (chunk
/512) >
4382 if (offset
< actual_stripes
* (chunk
/512))
4383 actual_stripes
= offset
/ (chunk
/512);
4384 offset
-= actual_stripes
* (chunk
/512);
4385 if (offset
< suspend_point
/data
)
4388 if (actual_stripes
== 0)
4390 grow_backup(sra
, offset
, actual_stripes
,
4392 disks
, chunk
, level
, layout
,
4393 dests
, destfd
, destoffsets
,
4394 part
, °raded
, buf
);
4395 validate(afd
, destfd
[0], destoffsets
[0]);
4396 /* record where 'part' is up to */
4399 backup_point
+= actual_stripes
* (chunk
/512) * data
;
4401 backup_point
-= actual_stripes
* (chunk
/512) * data
;
4405 /* FIXME maybe call progress_reshape one more time instead */
4406 /* remove any remaining suspension */
4407 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
4408 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
4409 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
4410 sysfs_set_num(sra
, NULL
, "sync_min", 0);
4412 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
4413 sysfs_set_num(sra
, NULL
, "sync_speed_min", speed
);
4419 * If any spare contains md_back_data-1 which is recent wrt mtime,
4420 * write that data into the array and update the super blocks with
4421 * the new reshape_progress
4423 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
, int cnt
,
4424 char *backup_file
, int verbose
)
4428 unsigned long long *offsets
;
4429 unsigned long long nstripe
, ostripe
;
4432 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
4433 if (info
->array
.level
== 6) odata
--; /* number of data disks */
4434 ndata
= info
->array
.raid_disks
- 1;
4435 if (info
->new_level
== 6) ndata
--;
4437 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
4439 if (info
->delta_disks
<= 0)
4440 /* Didn't grow, so the backup file must have
4444 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
4445 struct mdinfo dinfo
;
4448 char *devname
, namebuf
[20];
4449 unsigned long long lo
, hi
;
4451 /* This was a spare and may have some saved data on it.
4452 * Load the superblock, find and load the
4453 * backup_super_block.
4454 * If either fail, go on to next device.
4455 * If the backup contains no new info, just return
4456 * else restore data and update all superblocks
4458 if (i
== old_disks
-1) {
4459 fd
= open(backup_file
, O_RDONLY
);
4461 pr_err("backup file %s inaccessible: %s\n",
4462 backup_file
, strerror(errno
));
4465 devname
= backup_file
;
4470 if (st
->ss
->load_super(st
, fd
, NULL
))
4473 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4474 st
->ss
->free_super(st
);
4477 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
4479 pr_err("Cannot seek on device %d\n", i
);
4480 continue; /* Cannot seek */
4482 sprintf(namebuf
, "device-%d", i
);
4485 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
)) {
4487 pr_err("Cannot read from %s\n", devname
);
4488 continue; /* Cannot read */
4490 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0 &&
4491 memcmp(bsb
.magic
, "md_backup_data-2", 16) != 0) {
4493 pr_err("No backup metadata on %s\n", devname
);
4496 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
))) {
4498 pr_err("Bad backup-metadata checksum on %s\n", devname
);
4499 continue; /* bad checksum */
4501 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0 &&
4502 bsb
.sb_csum2
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum2
)-((char*)&bsb
))) {
4504 pr_err("Bad backup-metadata checksum2 on %s\n", devname
);
4505 continue; /* Bad second checksum */
4507 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0) {
4509 pr_err("Wrong uuid on backup-metadata on %s\n", devname
);
4510 continue; /* Wrong uuid */
4513 /* array utime and backup-mtime should be updated at much the same time, but it seems that
4514 * sometimes they aren't... So allow considerable flexability in matching, and allow
4515 * this test to be overridden by an environment variable.
4517 if (info
->array
.utime
> (int)__le64_to_cpu(bsb
.mtime
) + 2*60*60 ||
4518 info
->array
.utime
< (int)__le64_to_cpu(bsb
.mtime
) - 10*60) {
4519 if (check_env("MDADM_GROW_ALLOW_OLD")) {
4520 pr_err("accepting backup with timestamp %lu "
4521 "for array with timestamp %lu\n",
4522 (unsigned long)__le64_to_cpu(bsb
.mtime
),
4523 (unsigned long)info
->array
.utime
);
4525 pr_err("too-old timestamp on backup-metadata on %s\n", devname
);
4526 pr_err("If you think it is should be safe, try 'export MDADM_GROW_ALLOW_OLD=1'\n");
4527 continue; /* time stamp is too bad */
4531 if (bsb
.magic
[15] == '1') {
4532 if (bsb
.length
== 0)
4534 if (info
->delta_disks
>= 0) {
4535 /* reshape_progress is increasing */
4536 if (__le64_to_cpu(bsb
.arraystart
)
4537 + __le64_to_cpu(bsb
.length
)
4538 < info
->reshape_progress
) {
4541 pr_err("backup-metadata found on %s but is not needed\n", devname
);
4542 continue; /* No new data here */
4545 /* reshape_progress is decreasing */
4546 if (__le64_to_cpu(bsb
.arraystart
) >=
4547 info
->reshape_progress
)
4548 goto nonew
; /* No new data here */
4551 if (bsb
.length
== 0 && bsb
.length2
== 0)
4553 if (info
->delta_disks
>= 0) {
4554 /* reshape_progress is increasing */
4555 if ((__le64_to_cpu(bsb
.arraystart
)
4556 + __le64_to_cpu(bsb
.length
)
4557 < info
->reshape_progress
)
4559 (__le64_to_cpu(bsb
.arraystart2
)
4560 + __le64_to_cpu(bsb
.length2
)
4561 < info
->reshape_progress
))
4562 goto nonew
; /* No new data here */
4564 /* reshape_progress is decreasing */
4565 if (__le64_to_cpu(bsb
.arraystart
) >=
4566 info
->reshape_progress
&&
4567 __le64_to_cpu(bsb
.arraystart2
) >=
4568 info
->reshape_progress
)
4569 goto nonew
; /* No new data here */
4572 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0) {
4575 pr_err("Failed to verify secondary backup-metadata block on %s\n",
4577 continue; /* Cannot seek */
4579 /* There should be a duplicate backup superblock 4k before here */
4580 if (lseek64(fd
, -4096, 1) < 0 ||
4581 read(fd
, &bsb2
, sizeof(bsb2
)) != sizeof(bsb2
))
4582 goto second_fail
; /* Cannot find leading superblock */
4583 if (bsb
.magic
[15] == '1')
4584 bsbsize
= offsetof(struct mdp_backup_super
, pad1
);
4586 bsbsize
= offsetof(struct mdp_backup_super
, pad
);
4587 if (memcmp(&bsb2
, &bsb
, bsbsize
) != 0)
4588 goto second_fail
; /* Cannot find leading superblock */
4590 /* Now need the data offsets for all devices. */
4591 offsets
= xmalloc(sizeof(*offsets
)*info
->array
.raid_disks
);
4592 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
4595 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
4596 /* FIXME should be this be an error */
4598 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4599 st
->ss
->free_super(st
);
4600 offsets
[j
] = dinfo
.data_offset
* 512;
4602 printf(Name
": restoring critical section\n");
4604 if (restore_stripes(fdlist
, offsets
,
4605 info
->array
.raid_disks
,
4609 fd
, __le64_to_cpu(bsb
.devstart
)*512,
4610 __le64_to_cpu(bsb
.arraystart
)*512,
4611 __le64_to_cpu(bsb
.length
)*512, NULL
)) {
4612 /* didn't succeed, so giveup */
4614 pr_err("Error restoring backup from %s\n",
4620 if (bsb
.magic
[15] == '2' &&
4621 restore_stripes(fdlist
, offsets
,
4622 info
->array
.raid_disks
,
4626 fd
, __le64_to_cpu(bsb
.devstart
)*512 +
4627 __le64_to_cpu(bsb
.devstart2
)*512,
4628 __le64_to_cpu(bsb
.arraystart2
)*512,
4629 __le64_to_cpu(bsb
.length2
)*512, NULL
)) {
4630 /* didn't succeed, so giveup */
4632 pr_err("Error restoring second backup from %s\n",
4640 /* Ok, so the data is restored. Let's update those superblocks. */
4644 lo
= __le64_to_cpu(bsb
.arraystart
);
4645 hi
= lo
+ __le64_to_cpu(bsb
.length
);
4647 if (bsb
.magic
[15] == '2' && bsb
.length2
) {
4648 unsigned long long lo1
, hi1
;
4649 lo1
= __le64_to_cpu(bsb
.arraystart2
);
4650 hi1
= lo1
+ __le64_to_cpu(bsb
.length2
);
4654 } else if (lo
< lo1
)
4660 (info
->reshape_progress
< lo
||
4661 info
->reshape_progress
> hi
))
4662 /* backup does not affect reshape_progress*/ ;
4663 else if (info
->delta_disks
>= 0) {
4664 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
) +
4665 __le64_to_cpu(bsb
.length
);
4666 if (bsb
.magic
[15] == '2') {
4667 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
) +
4668 __le64_to_cpu(bsb
.length2
);
4669 if (p2
> info
->reshape_progress
)
4670 info
->reshape_progress
= p2
;
4673 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
);
4674 if (bsb
.magic
[15] == '2') {
4675 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
);
4676 if (p2
< info
->reshape_progress
)
4677 info
->reshape_progress
= p2
;
4680 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
4683 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
4685 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4686 dinfo
.reshape_progress
= info
->reshape_progress
;
4687 st
->ss
->update_super(st
, &dinfo
,
4688 "_reshape_progress",
4690 st
->ss
->store_super(st
, fdlist
[j
]);
4691 st
->ss
->free_super(st
);
4695 /* Didn't find any backup data, try to see if any
4698 if (info
->delta_disks
< 0) {
4699 /* When shrinking, the critical section is at the end.
4700 * So see if we are before the critical section.
4702 unsigned long long first_block
;
4703 nstripe
= ostripe
= 0;
4705 while (ostripe
>= nstripe
) {
4706 ostripe
+= info
->array
.chunk_size
/ 512;
4707 first_block
= ostripe
* odata
;
4708 nstripe
= first_block
/ ndata
/ (info
->new_chunk
/512) *
4709 (info
->new_chunk
/512);
4712 if (info
->reshape_progress
>= first_block
)
4715 if (info
->delta_disks
> 0) {
4716 /* See if we are beyond the critical section. */
4717 unsigned long long last_block
;
4718 nstripe
= ostripe
= 0;
4720 while (nstripe
>= ostripe
) {
4721 nstripe
+= info
->new_chunk
/ 512;
4722 last_block
= nstripe
* ndata
;
4723 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
4724 (info
->array
.chunk_size
/512);
4727 if (info
->reshape_progress
>= last_block
)
4730 /* needed to recover critical section! */
4732 pr_err("Failed to find backup of critical section\n");
4736 int Grow_continue_command(char *devname
, int fd
,
4737 char *backup_file
, int verbose
)
4740 struct supertype
*st
= NULL
;
4741 struct mdinfo
*content
= NULL
;
4742 struct mdinfo array
;
4743 char *subarray
= NULL
;
4744 struct mdinfo
*cc
= NULL
;
4745 struct mdstat_ent
*mdstat
= NULL
;
4749 dprintf("Grow continue from command line called for %s\n",
4752 st
= super_by_fd(fd
, &subarray
);
4753 if (!st
|| !st
->ss
) {
4754 pr_err("Unable to determine metadata format for %s\n",
4758 dprintf("Grow continue is run for ");
4759 if (st
->ss
->external
== 0) {
4761 dprintf("native array (%s)\n", devname
);
4762 if (ioctl(fd
, GET_ARRAY_INFO
, &array
.array
) < 0) {
4763 pr_err("%s is not an active md array -"
4764 " aborting\n", devname
);
4766 goto Grow_continue_command_exit
;
4769 /* Need to load a superblock.
4770 * FIXME we should really get what we need from
4773 for (d
= 0; d
< MAX_DISKS
; d
++) {
4774 mdu_disk_info_t disk
;
4778 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
4780 if (disk
.major
== 0 && disk
.minor
== 0)
4782 if ((disk
.state
& (1 << MD_DISK_ACTIVE
)) == 0)
4784 dv
= map_dev(disk
.major
, disk
.minor
, 1);
4787 fd2
= dev_open(dv
, O_RDONLY
);
4790 err
= st
->ss
->load_super(st
, fd2
, NULL
);
4792 /* invalidate fd2 to avoid possible double close() */
4798 if (d
== MAX_DISKS
) {
4799 pr_err("Unable to load metadata for %s\n",
4802 goto Grow_continue_command_exit
;
4804 st
->ss
->getinfo_super(st
, content
, NULL
);
4809 dprintf("subarray (%s)\n", subarray
);
4810 container
= st
->container_devnm
;
4811 cfd
= open_dev_excl(st
->container_devnm
);
4813 container
= st
->devnm
;
4815 cfd
= open_dev_excl(st
->devnm
);
4816 dprintf("container (%s)\n", container
);
4820 pr_err("Unable to open container "
4821 "for %s\n", devname
);
4823 goto Grow_continue_command_exit
;
4826 /* find in container array under reshape
4828 ret_val
= st
->ss
->load_container(st
, cfd
, NULL
);
4830 pr_err("Cannot read superblock for %s\n",
4833 goto Grow_continue_command_exit
;
4836 cc
= st
->ss
->container_content(st
, subarray
);
4837 for (content
= cc
; content
; content
= content
->next
) {
4839 int allow_reshape
= 1;
4841 if (content
->reshape_active
== 0)
4843 /* The decision about array or container wide
4844 * reshape is taken in Grow_continue based
4845 * content->reshape_active state, therefore we
4846 * need to check_reshape based on
4847 * reshape_active and subarray name
4849 if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
))
4851 if (content
->reshape_active
== CONTAINER_RESHAPE
&&
4852 (content
->array
.state
4853 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
)))
4856 if (!allow_reshape
) {
4857 pr_err("cannot continue reshape of an array"
4858 " in container with unsupported"
4859 " metadata: %s(%s)\n",
4860 devname
, container
);
4862 goto Grow_continue_command_exit
;
4865 array
= strchr(content
->text_version
+1, '/')+1;
4866 mdstat
= mdstat_by_subdev(array
, container
);
4869 if (mdstat
->active
== 0) {
4870 pr_err("Skipping inactive array %s.\n",
4872 free_mdstat(mdstat
);
4879 pr_err("Unable to determine reshaped "
4880 "array for %s\n", devname
);
4882 goto Grow_continue_command_exit
;
4884 fd2
= open_dev(mdstat
->devnm
);
4886 pr_err("cannot open (%s)\n", mdstat
->devnm
);
4888 goto Grow_continue_command_exit
;
4891 sysfs_init(content
, fd2
, mdstat
->devnm
);
4893 /* start mdmon in case it is not running
4895 if (!mdmon_running(container
))
4896 start_mdmon(container
);
4897 ping_monitor(container
);
4899 if (mdmon_running(container
))
4900 st
->update_tail
= &st
->updates
;
4902 pr_err("No mdmon found. "
4903 "Grow cannot continue.\n");
4905 goto Grow_continue_command_exit
;
4909 /* verify that array under reshape is started from
4912 if (verify_reshape_position(content
, content
->array
.level
) < 0) {
4914 goto Grow_continue_command_exit
;
4919 ret_val
= Grow_continue(fd
, st
, content
, backup_file
, 1, 0);
4921 Grow_continue_command_exit
:
4926 st
->ss
->free_super(st
);
4927 free_mdstat(mdstat
);
4934 int Grow_continue(int mdfd
, struct supertype
*st
, struct mdinfo
*info
,
4935 char *backup_file
, int forked
, int freeze_reshape
)
4939 if (!info
->reshape_active
)
4942 if (st
->ss
->external
) {
4943 int cfd
= open_dev(st
->container_devnm
);
4948 st
->ss
->load_container(st
, cfd
, st
->container_devnm
);
4950 ret_val
= reshape_container(st
->container_devnm
, NULL
, mdfd
,
4951 st
, info
, 0, backup_file
,
4953 1 | info
->reshape_active
,
4956 ret_val
= reshape_array(NULL
, mdfd
, "array", st
, info
, 1,
4957 NULL
, INVALID_SECTORS
,
4958 backup_file
, 0, forked
,
4959 1 | info
->reshape_active
,
4965 char *make_backup(char *name
)
4967 char *base
= "backup_file-";
4971 len
= strlen(MAP_DIR
) + 1 + strlen(base
) + strlen(name
)+1;
4972 fname
= xmalloc(len
);
4973 sprintf(fname
, "%s/%s%s", MAP_DIR
, base
, name
);
4977 char *locate_backup(char *name
)
4979 char *fl
= make_backup(name
);
4982 if (stat(fl
, &stb
) == 0 &&
4983 S_ISREG(stb
.st_mode
))