2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 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>
28 #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
29 #error no endian defined
35 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
38 int restore_backup(struct supertype
*st
,
39 struct mdinfo
*content
,
49 int disk_count
= next_spare
+ working_disks
;
51 dprintf("Called restore_backup()\n");
52 fdlist
= malloc(sizeof(int) * disk_count
);
55 Name
": cannot allocate memory for disk list\n");
58 for (i
= 0; i
< next_spare
; i
++)
60 for (dev
= content
->devs
; dev
; dev
= dev
->next
) {
66 fd
= dev_open(buf
, O_RDWR
);
68 if (dev
->disk
.raid_disk
>= 0)
69 fdlist
[dev
->disk
.raid_disk
] = fd
;
71 fdlist
[next_spare
++] = fd
;
74 if (st
->ss
->external
&& st
->ss
->recover_backup
)
75 err
= st
->ss
->recover_backup(st
, content
);
77 err
= Grow_restart(st
, content
, fdlist
, next_spare
,
78 backup_file
, verbose
> 0);
80 while (next_spare
> 0) {
82 if (fdlist
[next_spare
] >= 0)
83 close(fdlist
[next_spare
]);
87 fprintf(stderr
, Name
": Failed to restore critical"
88 " section for reshape - sorry.\n");
90 fprintf(stderr
, Name
": Possibly you need"
91 " to specify a --backup-file\n");
95 dprintf("restore_backup() returns status OK.\n");
99 int Grow_Add_device(char *devname
, int fd
, char *newdev
)
101 /* Add a device to an active array.
102 * Currently, just extend a linear array.
103 * This requires writing a new superblock on the
104 * new device, calling the kernel to add the device,
105 * and if that succeeds, update the superblock on
107 * This means that we need to *find* all other devices.
114 struct supertype
*st
= NULL
;
115 char *subarray
= NULL
;
117 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
118 fprintf(stderr
, Name
": cannot get array info for %s\n", devname
);
122 if (info
.array
.level
!= -1) {
123 fprintf(stderr
, Name
": can only add devices to linear arrays\n");
127 st
= super_by_fd(fd
, &subarray
);
129 fprintf(stderr
, Name
": cannot handle arrays with superblock version %d\n", info
.array
.major_version
);
134 fprintf(stderr
, Name
": Cannot grow linear sub-arrays yet\n");
140 nfd
= open(newdev
, O_RDWR
|O_EXCL
|O_DIRECT
);
142 fprintf(stderr
, Name
": cannot open %s\n", newdev
);
147 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
148 fprintf(stderr
, Name
": %s is not a block device!\n", newdev
);
153 /* now check out all the devices and make sure we can read the superblock */
154 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
155 mdu_disk_info_t disk
;
158 st
->ss
->free_super(st
);
161 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
162 fprintf(stderr
, Name
": cannot get device detail for device %d\n",
168 dv
= map_dev(disk
.major
, disk
.minor
, 1);
170 fprintf(stderr
, Name
": cannot find device file for device %d\n",
176 fd2
= dev_open(dv
, O_RDWR
);
178 fprintf(stderr
, Name
": cannot open device file %s\n", dv
);
184 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
185 fprintf(stderr
, Name
": cannot find super block on %s\n", dv
);
193 /* Ok, looks good. Lets update the superblock and write it out to
197 info
.disk
.number
= d
;
198 info
.disk
.major
= major(stb
.st_rdev
);
199 info
.disk
.minor
= minor(stb
.st_rdev
);
200 info
.disk
.raid_disk
= d
;
201 info
.disk
.state
= (1 << MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
);
202 st
->ss
->update_super(st
, &info
, "linear-grow-new", newdev
,
205 if (st
->ss
->store_super(st
, nfd
)) {
206 fprintf(stderr
, Name
": Cannot store new superblock on %s\n",
213 if (ioctl(fd
, ADD_NEW_DISK
, &info
.disk
) != 0) {
214 fprintf(stderr
, Name
": Cannot add new disk to this array\n");
217 /* Well, that seems to have worked.
218 * Now go through and update all superblocks
221 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
222 fprintf(stderr
, Name
": cannot get array info for %s\n", devname
);
227 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
228 mdu_disk_info_t disk
;
232 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
233 fprintf(stderr
, Name
": cannot get device detail for device %d\n",
237 dv
= map_dev(disk
.major
, disk
.minor
, 1);
239 fprintf(stderr
, Name
": cannot find device file for device %d\n",
243 fd2
= dev_open(dv
, O_RDWR
);
245 fprintf(stderr
, Name
": cannot open device file %s\n", dv
);
248 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
249 fprintf(stderr
, Name
": cannot find super block on %s\n", dv
);
253 info
.array
.raid_disks
= nd
+1;
254 info
.array
.nr_disks
= nd
+1;
255 info
.array
.active_disks
= nd
+1;
256 info
.array
.working_disks
= nd
+1;
258 st
->ss
->update_super(st
, &info
, "linear-grow-update", dv
,
261 if (st
->ss
->store_super(st
, fd2
)) {
262 fprintf(stderr
, Name
": Cannot store new superblock on %s\n", dv
);
272 int Grow_addbitmap(char *devname
, int fd
, char *file
, int chunk
, int delay
, int write_behind
, int force
)
275 * First check that array doesn't have a bitmap
276 * Then create the bitmap
279 * For internal bitmaps, we need to check the version,
280 * find all the active devices, and write the bitmap block
283 mdu_bitmap_file_t bmf
;
284 mdu_array_info_t array
;
285 struct supertype
*st
;
286 char *subarray
= NULL
;
287 int major
= BITMAP_MAJOR_HI
;
288 int vers
= md_get_version(fd
);
289 unsigned long long bitmapsize
, array_size
;
292 major
= BITMAP_MAJOR_HOSTENDIAN
;
293 fprintf(stderr
, Name
": Warning - bitmaps created on this kernel"
294 " are not portable\n"
295 " between different architectures. Consider upgrading"
296 " the Linux kernel.\n");
299 if (ioctl(fd
, GET_BITMAP_FILE
, &bmf
) != 0) {
301 fprintf(stderr
, Name
": Memory allocation failure.\n");
303 fprintf(stderr
, Name
": bitmaps not supported by this kernel.\n");
306 if (bmf
.pathname
[0]) {
307 if (strcmp(file
,"none")==0) {
308 if (ioctl(fd
, SET_BITMAP_FILE
, -1)!= 0) {
309 fprintf(stderr
, Name
": failed to remove bitmap %s\n",
315 fprintf(stderr
, Name
": %s already has a bitmap (%s)\n",
316 devname
, bmf
.pathname
);
319 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
320 fprintf(stderr
, Name
": cannot get array status for %s\n", devname
);
323 if (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)) {
324 if (strcmp(file
, "none")==0) {
325 array
.state
&= ~(1<<MD_SB_BITMAP_PRESENT
);
326 if (ioctl(fd
, SET_ARRAY_INFO
, &array
)!= 0) {
327 fprintf(stderr
, Name
": failed to remove internal bitmap.\n");
332 fprintf(stderr
, Name
": Internal bitmap already present on %s\n",
337 if (strcmp(file
, "none") == 0) {
338 fprintf(stderr
, Name
": no bitmap found on %s\n", devname
);
341 if (array
.level
<= 0) {
342 fprintf(stderr
, Name
": Bitmaps not meaningful with level %s\n",
343 map_num(pers
, array
.level
)?:"of this array");
346 bitmapsize
= array
.size
;
348 if (get_dev_size(fd
, NULL
, &array_size
) &&
349 array_size
> (0x7fffffffULL
<<9)) {
350 /* Array is big enough that we cannot trust array.size
351 * try other approaches
353 bitmapsize
= get_component_size(fd
);
355 if (bitmapsize
== 0) {
356 fprintf(stderr
, Name
": Cannot reliably determine size of array to create bitmap - sorry.\n");
360 if (array
.level
== 10) {
361 int ncopies
= (array
.layout
&255)*((array
.layout
>>8)&255);
362 bitmapsize
= bitmapsize
* array
.raid_disks
/ ncopies
;
365 st
= super_by_fd(fd
, &subarray
);
367 fprintf(stderr
, Name
": Cannot understand version %d.%d\n",
368 array
.major_version
, array
.minor_version
);
372 fprintf(stderr
, Name
": Cannot add bitmaps to sub-arrays yet\n");
377 if (strcmp(file
, "internal") == 0) {
380 int offset_setable
= 0;
382 if (st
->ss
->add_internal_bitmap
== NULL
) {
383 fprintf(stderr
, Name
": Internal bitmaps not supported "
384 "with %s metadata\n", st
->ss
->name
);
387 mdi
= sysfs_read(fd
, -1, GET_BITMAP_LOCATION
);
390 for (d
=0; d
< st
->max_devs
; d
++) {
391 mdu_disk_info_t disk
;
394 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
396 if (disk
.major
== 0 &&
399 if ((disk
.state
& (1<<MD_DISK_SYNC
))==0)
401 dv
= map_dev(disk
.major
, disk
.minor
, 1);
403 int fd2
= dev_open(dv
, O_RDWR
);
406 if (st
->ss
->load_super(st
, fd2
, NULL
)==0) {
407 if (st
->ss
->add_internal_bitmap(
409 &chunk
, delay
, write_behind
,
410 bitmapsize
, offset_setable
,
413 st
->ss
->write_bitmap(st
, fd2
);
415 fprintf(stderr
, Name
": failed "
416 "to create internal bitmap - chunksize problem.\n");
424 if (offset_setable
) {
425 st
->ss
->getinfo_super(st
, mdi
, NULL
);
426 sysfs_init(mdi
, fd
, -1);
427 rv
= sysfs_set_num(mdi
, NULL
, "bitmap/location",
430 array
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
431 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
436 ": Cannot add bitmap while array is"
437 " resyncing or reshaping etc.\n");
438 fprintf(stderr
, Name
": failed to set internal bitmap.\n");
445 int max_devs
= st
->max_devs
;
447 /* try to load a superblock */
448 for (d
=0; d
<max_devs
; d
++) {
449 mdu_disk_info_t disk
;
453 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
455 if ((disk
.major
==0 && disk
.minor
==0) ||
456 (disk
.state
& (1<<MD_DISK_REMOVED
)))
458 dv
= map_dev(disk
.major
, disk
.minor
, 1);
460 fd2
= dev_open(dv
, O_RDONLY
);
462 if (st
->ss
->load_super(st
, fd2
, NULL
) == 0) {
464 st
->ss
->uuid_from_super(st
, uuid
);
471 fprintf(stderr
, Name
": cannot find UUID for array!\n");
474 if (CreateBitmap(file
, force
, (char*)uuid
, chunk
,
475 delay
, write_behind
, bitmapsize
, major
)) {
478 bitmap_fd
= open(file
, O_RDWR
);
480 fprintf(stderr
, Name
": weird: %s cannot be opened\n",
484 if (ioctl(fd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
488 ": Cannot add bitmap while array is"
489 " resyncing or reshaping etc.\n");
490 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
491 devname
, strerror(err
));
500 * When reshaping an array we might need to backup some data.
501 * This is written to all spares with a 'super_block' describing it.
502 * The superblock goes 4K from the end of the used space on the
504 * It if written after the backup is complete.
505 * It has the following structure.
508 static struct mdp_backup_super
{
509 char magic
[16]; /* md_backup_data-1 or -2 */
512 /* start/sizes in 512byte sectors */
513 __u64 devstart
; /* address on backup device/file of data */
516 __u32 sb_csum
; /* csum of preceeding bytes. */
518 __u64 devstart2
; /* offset in to data of second section */
521 __u32 sb_csum2
; /* csum of preceeding bytes. */
523 } __attribute__((aligned(512))) bsb
, bsb2
;
525 static __u32
bsb_csum(char *buf
, int len
)
529 for (i
=0; i
<len
; i
++)
530 csum
= (csum
<<3) + buf
[0];
531 return __cpu_to_le32(csum
);
534 static int check_idle(struct supertype
*st
)
536 /* Check that all member arrays for this container, or the
537 * container of this array, are idle
539 int container_dev
= (st
->container_dev
!= NoMdDev
540 ? st
->container_dev
: st
->devnum
);
542 struct mdstat_ent
*ent
, *e
;
545 fmt_devname(container
, container_dev
);
546 ent
= mdstat_read(0, 0);
547 for (e
= ent
; e
; e
= e
->next
) {
548 if (!is_container_member(e
, container
))
550 if (e
->percent
>= 0) {
559 static int freeze_container(struct supertype
*st
)
561 int container_dev
= (st
->container_dev
!= NoMdDev
562 ? st
->container_dev
: st
->devnum
);
568 fmt_devname(container
, container_dev
);
570 if (block_monitor(container
, 1)) {
571 fprintf(stderr
, Name
": failed to freeze container\n");
578 static void unfreeze_container(struct supertype
*st
)
580 int container_dev
= (st
->container_dev
!= NoMdDev
581 ? st
->container_dev
: st
->devnum
);
584 fmt_devname(container
, container_dev
);
586 unblock_monitor(container
, 1);
589 static int freeze(struct supertype
*st
)
591 /* Try to freeze resync/rebuild on this array/container.
592 * Return -1 if the array is busy,
593 * return -2 container cannot be frozen,
594 * return 0 if this kernel doesn't support 'frozen'
595 * return 1 if it worked.
597 if (st
->ss
->external
)
598 return freeze_container(st
);
600 struct mdinfo
*sra
= sysfs_read(-1, st
->devnum
, GET_VERSION
);
606 /* Need to clear any 'read-auto' status */
607 if (sysfs_get_str(sra
, NULL
, "array_state", buf
, 20) > 0 &&
608 strncmp(buf
, "read-auto", 9) == 0)
609 sysfs_set_str(sra
, NULL
, "array_state", "clean");
611 err
= sysfs_freeze_array(sra
);
617 static void unfreeze(struct supertype
*st
)
619 if (st
->ss
->external
)
620 return unfreeze_container(st
);
622 struct mdinfo
*sra
= sysfs_read(-1, st
->devnum
, GET_VERSION
);
625 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
627 fprintf(stderr
, Name
": failed to unfreeze array\n");
632 static void wait_reshape(struct mdinfo
*sra
)
634 int fd
= sysfs_get_fd(sra
, NULL
, "sync_action");
640 while (sysfs_fd_get_str(fd
, action
, 20) > 0 &&
641 strncmp(action
, "reshape", 7) == 0) {
645 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
650 static int reshape_super(struct supertype
*st
, long long size
, int level
,
651 int layout
, int chunksize
, int raid_disks
,
652 int delta_disks
, char *backup_file
, char *dev
,
655 /* nothing extra to check in the native case */
656 if (!st
->ss
->external
)
658 if (!st
->ss
->reshape_super
||
659 !st
->ss
->manage_reshape
) {
660 fprintf(stderr
, Name
": %s metadata does not support reshape\n",
665 return st
->ss
->reshape_super(st
, size
, level
, layout
, chunksize
,
666 raid_disks
, delta_disks
, backup_file
, dev
,
670 static void sync_metadata(struct supertype
*st
)
672 if (st
->ss
->external
) {
673 if (st
->update_tail
) {
674 flush_metadata_updates(st
);
675 st
->update_tail
= &st
->updates
;
677 st
->ss
->sync_metadata(st
);
681 static int subarray_set_num(char *container
, struct mdinfo
*sra
, char *name
, int n
)
683 /* when dealing with external metadata subarrays we need to be
684 * prepared to handle EAGAIN. The kernel may need to wait for
685 * mdmon to mark the array active so the kernel can handle
686 * allocations/writeback when preparing the reshape action
687 * (md_allow_write()). We temporarily disable safe_mode_delay
688 * to close a race with the array_state going clean before the
689 * next write to raid_disks / stripe_cache_size
694 /* only 'raid_disks' and 'stripe_cache_size' trigger md_allow_write */
696 (strcmp(name
, "raid_disks") != 0 &&
697 strcmp(name
, "stripe_cache_size") != 0))
698 return sysfs_set_num(sra
, NULL
, name
, n
);
700 rc
= sysfs_get_str(sra
, NULL
, "safe_mode_delay", safe
, sizeof(safe
));
703 sysfs_set_num(sra
, NULL
, "safe_mode_delay", 0);
704 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
705 if (rc
< 0 && errno
== EAGAIN
) {
706 ping_monitor(container
);
707 /* if we get EAGAIN here then the monitor is not active
710 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
712 sysfs_set_str(sra
, NULL
, "safe_mode_delay", safe
);
716 int start_reshape(struct mdinfo
*sra
, int already_running
,
717 int before_data_disks
, int data_disks
)
720 unsigned long long sync_max_to_set
;
722 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
723 err
= sysfs_set_num(sra
, NULL
, "suspend_hi", sra
->reshape_progress
);
724 err
= err
?: sysfs_set_num(sra
, NULL
, "suspend_lo",
725 sra
->reshape_progress
);
726 if (before_data_disks
<= data_disks
)
727 sync_max_to_set
= sra
->reshape_progress
/ data_disks
;
729 sync_max_to_set
= (sra
->component_size
* data_disks
730 - sra
->reshape_progress
) / data_disks
;
731 if (!already_running
)
732 sysfs_set_num(sra
, NULL
, "sync_min", sync_max_to_set
);
733 err
= err
?: sysfs_set_num(sra
, NULL
, "sync_max", sync_max_to_set
);
734 if (!already_running
)
735 err
= err
?: sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
740 void abort_reshape(struct mdinfo
*sra
)
742 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
743 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
744 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
745 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
746 sysfs_set_num(sra
, NULL
, "sync_min", 0);
747 sysfs_set_str(sra
, NULL
, "sync_max", "max");
750 int remove_disks_for_takeover(struct supertype
*st
,
755 struct mdinfo
*remaining
;
758 if (sra
->array
.level
== 10)
759 nr_of_copies
= layout
& 0xff;
760 else if (sra
->array
.level
== 1)
761 nr_of_copies
= sra
->array
.raid_disks
;
765 remaining
= sra
->devs
;
767 /* for each 'copy', select one device and remove from the list. */
768 for (slot
= 0; slot
< sra
->array
.raid_disks
; slot
+= nr_of_copies
) {
769 struct mdinfo
**diskp
;
772 /* Find a working device to keep */
773 for (diskp
= &remaining
; *diskp
; diskp
= &(*diskp
)->next
) {
774 struct mdinfo
*disk
= *diskp
;
776 if (disk
->disk
.raid_disk
< slot
)
778 if (disk
->disk
.raid_disk
>= slot
+ nr_of_copies
)
780 if (disk
->disk
.state
& (1<<MD_DISK_REMOVED
))
782 if (disk
->disk
.state
& (1<<MD_DISK_FAULTY
))
784 if (!(disk
->disk
.state
& (1<<MD_DISK_SYNC
)))
787 /* We have found a good disk to use! */
789 disk
->next
= sra
->devs
;
798 if (slot
< sra
->array
.raid_disks
) {
799 /* didn't find all slots */
805 sra
->devs
= remaining
;
809 /* Remove all 'remaining' devices from the array */
811 struct mdinfo
*sd
= remaining
;
812 remaining
= sd
->next
;
814 sysfs_set_str(sra
, sd
, "state", "faulty");
815 sysfs_set_str(sra
, sd
, "slot", "none");
816 /* for external metadata disks should be removed in mdmon */
817 if (!st
->ss
->external
)
818 sysfs_set_str(sra
, sd
, "state", "remove");
819 sd
->disk
.state
|= (1<<MD_DISK_REMOVED
);
820 sd
->disk
.state
&= ~(1<<MD_DISK_SYNC
);
821 sd
->next
= sra
->devs
;
827 void reshape_free_fdlist(int *fdlist
,
828 unsigned long long *offsets
,
833 for (i
= 0; i
< size
; i
++)
841 int reshape_prepare_fdlist(char *devname
,
845 unsigned long blocks
,
848 unsigned long long *offsets
)
853 for (d
= 0; d
<= nrdisks
; d
++)
856 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
857 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
859 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
860 char *dn
= map_dev(sd
->disk
.major
,
862 fdlist
[sd
->disk
.raid_disk
]
863 = dev_open(dn
, O_RDONLY
);
864 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
865 if (fdlist
[sd
->disk
.raid_disk
] < 0) {
867 Name
": %s: cannot open component %s\n",
868 devname
, dn
? dn
: "-unknown-");
872 } else if (backup_file
== NULL
) {
874 char *dn
= map_dev(sd
->disk
.major
,
876 fdlist
[d
] = dev_open(dn
, O_RDWR
);
877 offsets
[d
] = (sd
->data_offset
+ sra
->component_size
- blocks
- 8)*512;
879 fprintf(stderr
, Name
": %s: cannot open component %s\n",
880 devname
, dn
? dn
: "-unknown-");
891 int reshape_open_backup_file(char *backup_file
,
896 unsigned long long *offsets
,
899 /* Return 1 on success, 0 on any form of failure */
900 /* need to check backup file is large enough */
906 *fdlist
= open(backup_file
, O_RDWR
|O_CREAT
|(restart
? O_TRUNC
: O_EXCL
),
910 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
911 devname
, backup_file
, strerror(errno
));
914 /* Guard against backup file being on array device.
915 * If array is partitioned or if LVM etc is in the
916 * way this will not notice, but it is better than
919 fstat(*fdlist
, &stb
);
922 if (stb
.st_rdev
== dev
) {
923 fprintf(stderr
, Name
": backup file must NOT be"
924 " on the array being reshaped.\n");
930 for (i
=0; i
< blocks
+ 8 ; i
++) {
931 if (write(*fdlist
, buf
, 512) != 512) {
932 fprintf(stderr
, Name
": %s: cannot create"
933 " backup file %s: %s\n",
934 devname
, backup_file
, strerror(errno
));
938 if (fsync(*fdlist
) != 0) {
939 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
940 devname
, backup_file
, strerror(errno
));
947 unsigned long compute_backup_blocks(int nchunk
, int ochunk
,
948 unsigned int ndata
, unsigned int odata
)
950 unsigned long a
, b
, blocks
;
951 /* So how much do we need to backup.
952 * We need an amount of data which is both a whole number of
953 * old stripes and a whole number of new stripes.
954 * So LCM for (chunksize*datadisks).
956 a
= (ochunk
/512) * odata
;
957 b
= (nchunk
/512) * ndata
;
965 /* LCM == product / GCD */
966 blocks
= (ochunk
/512) * (nchunk
/512) * odata
* ndata
/ a
;
971 char *analyse_change(struct mdinfo
*info
, struct reshape
*re
)
973 /* Based on the current array state in info->array and
974 * the changes in info->new_* etc, determine:
975 * - whether the change is possible
976 * - Intermediate level/raid_disks/layout
977 * - whether a restriping reshape is needed
978 * - number of sectors in minimum change unit. This
979 * will cover a whole number of stripes in 'before' and
982 * Return message if the change should be rejected
983 * NULL if the change can be achieved
985 * This can be called as part of starting a reshape, or
986 * when assembling an array that is undergoing reshape.
989 /* delta_parity records change in number of devices
990 * caused by level change
992 int delta_parity
= 0;
994 /* If a new level not explicitly given, we assume no-change */
995 if (info
->new_level
== UnSet
)
996 info
->new_level
= info
->array
.level
;
999 switch (info
->new_level
) {
1005 /* chunk size is meaningful, must divide component_size
1008 if (info
->component_size
% (info
->new_chunk
/512))
1009 return "New chunk size does not"
1010 " divide component size";
1013 return "chunk size not meaningful for this level";
1016 info
->new_chunk
= info
->array
.chunk_size
;
1018 switch (info
->array
.level
) {
1020 return "Cannot understand this RAID level";
1022 /* RAID1 can convert to RAID1 with different disks, or
1023 * raid5 with 2 disks, or
1026 if (info
->new_level
== 0) {
1027 if (info
->delta_disks
!= UnSet
&&
1028 info
->delta_disks
!= 0)
1029 return "Cannot change number of disks "
1030 "with RAID1->RAID0 conversion";
1032 re
->before
.data_disks
= 1;
1033 re
->after
.data_disks
= 1;
1034 re
->before
.layout
= 0;
1035 re
->backup_blocks
= 0;
1039 if (info
->new_level
== 1) {
1040 if (info
->delta_disks
== UnSet
)
1041 /* Don't know what to do */
1042 return "no change requested for Growing RAID1";
1044 re
->backup_blocks
= 0;
1048 if (info
->array
.raid_disks
== 2 &&
1049 info
->new_level
== 5) {
1052 re
->before
.data_disks
= 1;
1053 if (info
->delta_disks
!= UnSet
&&
1054 info
->delta_disks
!= 0)
1055 re
->after
.data_disks
= 1 + info
->delta_disks
;
1057 re
->after
.data_disks
= 1;
1058 if (re
->after
.data_disks
< 1)
1059 return "Number of disks too small for RAID5";
1061 re
->before
.layout
= ALGORITHM_LEFT_SYMMETRIC
;
1062 info
->array
.chunk_size
= 65536;
1065 /* Could do some multi-stage conversions, but leave that to
1068 return "Impossibly level change request for RAID1";
1071 /* RAID10 can only be converted from near mode to
1072 * RAID0 by removing some devices
1074 if ((info
->array
.layout
& ~0xff) != 0x100)
1075 return "Cannot Grow RAID10 with far/offset layout";
1076 /* number of devices must be multiple of number of copies */
1077 if (info
->array
.raid_disks
% (info
->array
.layout
& 0xff))
1078 return "RAID10 layout too complex for Grow operation";
1080 if (info
->new_level
!= 0)
1081 return "RAID10 can only be changed to RAID0";
1082 new_disks
= (info
->array
.raid_disks
1083 / (info
->array
.layout
& 0xff));
1084 if (info
->delta_disks
== UnSet
)
1085 info
->delta_disks
= (new_disks
1086 - info
->array
.raid_disks
);
1088 if (info
->delta_disks
!= new_disks
- info
->array
.raid_disks
)
1089 return "New number of raid-devices impossible for RAID10";
1090 if (info
->new_chunk
&&
1091 info
->new_chunk
!= info
->array
.chunk_size
)
1092 return "Cannot change chunk-size with RAID10 Grow";
1097 re
->before
.data_disks
= new_disks
;
1098 re
->after
.data_disks
= re
->before
.data_disks
;
1099 re
->before
.layout
= 0;
1100 re
->backup_blocks
= 0;
1104 /* RAID0 can be converted to RAID10, or to RAID456 */
1105 if (info
->new_level
== 10) {
1106 if (info
->new_layout
== UnSet
&& info
->delta_disks
== UnSet
) {
1107 /* Assume near=2 layout */
1108 info
->new_layout
= 0x102;
1109 info
->delta_disks
= info
->array
.raid_disks
;
1111 if (info
->new_layout
== UnSet
) {
1112 int copies
= 1 + (info
->delta_disks
1113 / info
->array
.raid_disks
);
1114 if (info
->array
.raid_disks
* (copies
-1)
1115 != info
->delta_disks
)
1116 return "Impossible number of devices"
1117 " for RAID0->RAID10";
1118 info
->new_layout
= 0x100 + copies
;
1120 if (info
->delta_disks
== UnSet
) {
1121 int copies
= info
->new_layout
& 0xff;
1122 if (info
->new_layout
!= 0x100 + copies
)
1123 return "New layout impossible"
1124 " for RAID0->RAID10";;
1125 info
->delta_disks
= (copies
- 1) *
1126 info
->array
.raid_disks
;
1128 if (info
->new_chunk
&&
1129 info
->new_chunk
!= info
->array
.chunk_size
)
1130 return "Cannot change chunk-size with RAID0->RAID10";
1134 re
->before
.data_disks
= (info
->array
.raid_disks
+
1136 re
->after
.data_disks
= re
->before
.data_disks
;
1137 re
->before
.layout
= info
->new_layout
;
1138 re
->backup_blocks
= 0;
1142 /* RAID0 can also covert to RAID0/4/5/6 by first converting to
1143 * a raid4 style layout of the final level.
1145 switch (info
->new_level
) {
1150 re
->before
.layout
= 0;
1155 re
->before
.layout
= ALGORITHM_PARITY_N
;
1160 re
->before
.layout
= ALGORITHM_PARITY_N
;
1163 return "Impossible level change requested";
1165 re
->before
.data_disks
= info
->array
.raid_disks
;
1166 /* determining 'after' layout happens outside this 'switch' */
1170 info
->array
.layout
= ALGORITHM_PARITY_N
;
1172 switch (info
->new_level
) {
1176 re
->level
= info
->array
.level
;
1177 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1178 re
->before
.layout
= info
->array
.layout
;
1182 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1183 re
->before
.layout
= info
->array
.layout
;
1188 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1189 switch (info
->array
.layout
) {
1190 case ALGORITHM_LEFT_ASYMMETRIC
:
1191 re
->before
.layout
= ALGORITHM_LEFT_ASYMMETRIC_6
;
1193 case ALGORITHM_RIGHT_ASYMMETRIC
:
1194 re
->before
.layout
= ALGORITHM_RIGHT_ASYMMETRIC_6
;
1196 case ALGORITHM_LEFT_SYMMETRIC
:
1197 re
->before
.layout
= ALGORITHM_LEFT_SYMMETRIC_6
;
1199 case ALGORITHM_RIGHT_SYMMETRIC
:
1200 re
->before
.layout
= ALGORITHM_RIGHT_SYMMETRIC_6
;
1202 case ALGORITHM_PARITY_0
:
1203 re
->before
.layout
= ALGORITHM_PARITY_0_6
;
1205 case ALGORITHM_PARITY_N
:
1206 re
->before
.layout
= ALGORITHM_PARITY_N_6
;
1209 return "Cannot convert an array with this layout";
1213 if (info
->array
.raid_disks
!= 2)
1214 return "Can only convert a 2-device array to RAID1";
1215 if (info
->delta_disks
!= UnSet
&&
1216 info
->delta_disks
!= 0)
1217 return "Cannot set raid_disk when "
1218 "converting RAID5->RAID1";
1222 return "Impossible level change requested";
1226 switch (info
->new_level
) {
1232 re
->before
.data_disks
= info
->array
.raid_disks
- 2;
1233 re
->before
.layout
= info
->array
.layout
;
1236 return "Impossible level change requested";
1241 /* If we reached here then it looks like a re-stripe is
1242 * happening. We have determined the intermediate level
1243 * and initial raid_disks/layout and stored these in 're'.
1245 * We need to deduce the final layout that can be atomically
1246 * converted to the end state.
1248 switch (info
->new_level
) {
1250 /* We can only get to RAID0 from RAID4 or RAID5
1251 * with appropriate layout and one extra device
1253 if (re
->level
!= 4 && re
->level
!= 5)
1254 return "Cannot covert to RAID0 from this level";
1256 switch (re
->level
) {
1258 re
->after
.layout
= 0 ; break;
1260 re
->after
.layout
= ALGORITHM_PARITY_N
; break;
1265 /* We can only get to RAID4 from RAID5 */
1266 if (re
->level
!= 4 && re
->level
!= 5)
1267 return "Cannot convert to RAID4 from this level";
1269 switch (re
->level
) {
1271 re
->after
.layout
= 0 ; break;
1273 re
->after
.layout
= ALGORITHM_PARITY_N
; break;
1278 /* We get to RAID5 for RAID5 or RAID6 */
1279 if (re
->level
!= 5 && re
->level
!= 6)
1280 return "Cannot convert to RAID5 from this level";
1282 switch (re
->level
) {
1284 if (info
->new_layout
== UnSet
)
1285 re
->after
.layout
= re
->before
.layout
;
1287 re
->after
.layout
= info
->new_layout
;
1290 if (info
->new_layout
== UnSet
)
1291 info
->new_layout
= re
->before
.layout
;
1293 /* after.layout needs to be raid6 version of new_layout */
1294 if (info
->new_layout
== ALGORITHM_PARITY_N
)
1295 re
->after
.layout
= ALGORITHM_PARITY_N
;
1298 char *ls
= map_num(r5layout
, info
->new_layout
);
1300 strcat(strcpy(layout
, ls
), "-6");
1301 l
= map_name(r6layout
, layout
);
1303 return "Cannot find RAID6 layout"
1305 re
->after
.layout
= l
;
1311 /* We must already be at level 6 */
1313 return "Impossible level change";
1314 if (info
->new_layout
== UnSet
)
1315 re
->after
.layout
= info
->array
.layout
;
1317 re
->after
.layout
= info
->new_layout
;
1320 return "Impossible level change requested";
1322 if (info
->delta_disks
== UnSet
)
1323 info
->delta_disks
= delta_parity
;
1325 re
->after
.data_disks
= (re
->before
.data_disks
1328 switch (re
->level
) {
1329 case 6: re
->parity
= 2; break;
1331 case 5: re
->parity
= 1; break;
1332 default: re
->parity
= 0; break;
1334 /* So we have a restripe operation, we need to calculate the number
1335 * of blocks per reshape operation.
1337 if (info
->new_chunk
== 0)
1338 info
->new_chunk
= info
->array
.chunk_size
;
1339 if (re
->after
.data_disks
== re
->before
.data_disks
&&
1340 re
->after
.layout
== re
->before
.layout
&&
1341 info
->new_chunk
== info
->array
.chunk_size
) {
1342 /* Nothing to change */
1343 re
->backup_blocks
= 0;
1346 if (re
->after
.data_disks
== 1 && re
->before
.data_disks
== 1) {
1347 /* chunk and layout changes make no difference */
1348 re
->backup_blocks
= 0;
1352 if (re
->after
.data_disks
== re
->before
.data_disks
&&
1353 get_linux_version() < 2006032)
1354 return "in-place reshape is not safe before 2.6.32 - sorry.";
1356 if (re
->after
.data_disks
< re
->before
.data_disks
&&
1357 get_linux_version() < 2006030)
1358 return "reshape to fewer devices is not supported before 2.6.30 - sorry.";
1360 re
->backup_blocks
= compute_backup_blocks(
1361 info
->new_chunk
, info
->array
.chunk_size
,
1362 re
->after
.data_disks
,
1363 re
->before
.data_disks
);
1365 re
->new_size
= info
->component_size
* re
->after
.data_disks
;
1369 static int reshape_array(char *container
, int fd
, char *devname
,
1370 struct supertype
*st
, struct mdinfo
*info
,
1371 int force
, struct mddev_dev
*devlist
,
1372 char *backup_file
, int quiet
, int forked
,
1373 int restart
, int freeze_reshape
);
1374 static int reshape_container(char *container
, char *devname
,
1376 struct supertype
*st
,
1377 struct mdinfo
*info
,
1380 int quiet
, int restart
, int freeze_reshape
);
1382 int Grow_reshape(char *devname
, int fd
, int quiet
, char *backup_file
,
1384 int level
, char *layout_str
, int chunksize
, int raid_disks
,
1385 struct mddev_dev
*devlist
,
1386 int assume_clean
, int force
)
1388 /* Make some changes in the shape of an array.
1389 * The kernel must support the change.
1391 * There are three different changes. Each can trigger
1392 * a resync or recovery so we freeze that until we have
1393 * requested everything (if kernel supports freezing - 2.6.30).
1395 * - change size (i.e. component_size)
1397 * - change layout/chunksize/ndisks
1399 * The last can require a reshape. It is different on different
1400 * levels so we need to check the level before actioning it.
1401 * Some times the level change needs to be requested after the
1402 * reshape (e.g. raid6->raid5, raid5->raid0)
1405 struct mdu_array_info_s array
;
1407 struct supertype
*st
;
1408 char *subarray
= NULL
;
1412 char *container
= NULL
;
1413 char container_buf
[20];
1416 struct mddev_dev
*dv
;
1422 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) < 0) {
1423 fprintf(stderr
, Name
": %s is not an active md array - aborting\n",
1429 (chunksize
|| level
!= UnSet
|| layout_str
|| raid_disks
)) {
1430 fprintf(stderr
, Name
": cannot change component size at the same time "
1431 "as other changes.\n"
1432 " Change size first, then check data is intact before "
1433 "making other changes.\n");
1437 if (raid_disks
&& raid_disks
< array
.raid_disks
&& array
.level
> 1 &&
1438 get_linux_version() < 2006032 &&
1439 !check_env("MDADM_FORCE_FEWER")) {
1440 fprintf(stderr
, Name
": reducing the number of devices is not safe before Linux 2.6.32\n"
1441 " Please use a newer kernel\n");
1445 st
= super_by_fd(fd
, &subarray
);
1447 fprintf(stderr
, Name
": Unable to determine metadata format for %s\n", devname
);
1450 if (raid_disks
> st
->max_devs
) {
1451 fprintf(stderr
, Name
": Cannot increase raid-disks on this array"
1452 " beyond %d\n", st
->max_devs
);
1456 /* in the external case we need to check that the requested reshape is
1457 * supported, and perform an initial check that the container holds the
1458 * pre-requisite spare devices (mdmon owns final validation)
1460 if (st
->ss
->external
) {
1465 container_dev
= st
->container_dev
;
1466 cfd
= open_dev_excl(st
->container_dev
);
1468 container_dev
= st
->devnum
;
1470 cfd
= open_dev_excl(st
->devnum
);
1474 fprintf(stderr
, Name
": Unable to open container for %s\n",
1480 fmt_devname(container_buf
, container_dev
);
1481 container
= container_buf
;
1483 rv
= st
->ss
->load_container(st
, cfd
, NULL
);
1486 fprintf(stderr
, Name
": Cannot read superblock for %s\n",
1492 /* check if operation is supported for metadata handler */
1493 if (st
->ss
->container_content
) {
1494 struct mdinfo
*cc
= NULL
;
1495 struct mdinfo
*content
= NULL
;
1497 cc
= st
->ss
->container_content(st
, subarray
);
1498 for (content
= cc
; content
; content
= content
->next
) {
1499 int allow_reshape
= 1;
1501 /* check if reshape is allowed based on metadata
1502 * indications stored in content.array.status
1504 if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
))
1506 if (content
->array
.state
1507 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
))
1509 if (!allow_reshape
) {
1510 fprintf(stderr
, Name
1511 " cannot reshape arrays in"
1512 " container with unsupported"
1513 " metadata: %s(%s)\n",
1514 devname
, container_buf
);
1522 if (mdmon_running(container_dev
))
1523 st
->update_tail
= &st
->updates
;
1527 for (dv
= devlist
; dv
; dv
= dv
->next
)
1529 if (raid_disks
> array
.raid_disks
&&
1530 array
.spare_disks
+added_disks
< (raid_disks
- array
.raid_disks
) &&
1533 Name
": Need %d spare%s to avoid degraded array,"
1534 " and only have %d.\n"
1535 " Use --force to over-ride this check.\n",
1536 raid_disks
- array
.raid_disks
,
1537 raid_disks
- array
.raid_disks
== 1 ? "" : "s",
1538 array
.spare_disks
+ added_disks
);
1542 sra
= sysfs_read(fd
, 0, GET_LEVEL
| GET_DISKS
| GET_DEVS
1543 | GET_STATE
| GET_VERSION
);
1545 if (st
->ss
->external
&& subarray
== NULL
) {
1546 array
.level
= LEVEL_CONTAINER
;
1547 sra
->array
.level
= LEVEL_CONTAINER
;
1550 fprintf(stderr
, Name
": failed to read sysfs parameters for %s\n",
1554 frozen
= freeze(st
);
1556 /* freeze() already spewed the reason */
1559 } else if (frozen
< 0) {
1560 fprintf(stderr
, Name
": %s is performing resync/recovery and cannot"
1561 " be reshaped\n", devname
);
1566 /* ========= set size =============== */
1567 if (size
>= 0 && (size
== 0 || size
!= array
.size
)) {
1568 long long orig_size
= get_component_size(fd
)/2;
1569 long long min_csize
;
1573 orig_size
= array
.size
;
1575 if (reshape_super(st
, size
, UnSet
, UnSet
, 0, 0, UnSet
, NULL
,
1582 /* Update the size of each member device in case
1583 * they have been resized. This will never reduce
1584 * below the current used-size. The "size" attribute
1585 * understands '0' to mean 'max'.
1588 for (mdi
= sra
->devs
; mdi
; mdi
= mdi
->next
) {
1589 if (sysfs_set_num(sra
, mdi
, "size", size
) < 0)
1591 if (array
.not_persistent
== 0 &&
1592 array
.major_version
== 0 &&
1593 get_linux_version() < 3001000) {
1594 /* Dangerous to allow size to exceed 2TB */
1595 unsigned long long csize
;
1596 if (sysfs_get_ll(sra
, mdi
, "size", &csize
) == 0) {
1597 if (csize
>= 2ULL*1024*1024*1024)
1598 csize
= 2ULL*1024*1024*1024;
1599 if ((min_csize
== 0 || (min_csize
1600 > (long long)csize
)))
1605 if (min_csize
&& size
> min_csize
) {
1606 fprintf(stderr
, Name
": Cannot safely make this array "
1607 "use more than 2TB per device on this kernel.\n");
1611 if (min_csize
&& size
== 0) {
1612 /* Don't let the kernel choose a size - it will get
1615 fprintf(stderr
, Name
": Limited v0.90 array to "
1616 "2TB per device\n");
1621 if (array
.size
!= size
) {
1622 /* got truncated to 32bit, write to
1623 * component_size instead
1626 rv
= sysfs_set_num(sra
, NULL
,
1627 "component_size", size
);
1631 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
1635 /* restore metadata */
1636 if (reshape_super(st
, orig_size
, UnSet
, UnSet
, 0, 0,
1637 UnSet
, NULL
, devname
, !quiet
) == 0)
1639 fprintf(stderr
, Name
": Cannot set device size for %s: %s\n",
1640 devname
, strerror(err
));
1642 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1643 fprintf(stderr
, " Bitmap must be removed before size can be changed\n");
1648 /* This will fail on kernels newer than 3.0 unless
1649 * a backport has been arranged.
1652 sysfs_set_str(sra
, NULL
, "resync_start", "none") < 0)
1653 fprintf(stderr
, Name
": --assume-clean not support with --grow on this kernel\n");
1655 ioctl(fd
, GET_ARRAY_INFO
, &array
);
1656 size
= get_component_size(fd
)/2;
1660 if (size
== orig_size
)
1661 fprintf(stderr
, Name
": component size of %s "
1662 "unchanged at %lluK\n",
1665 fprintf(stderr
, Name
": component size of %s "
1666 "has been set to %lluK\n",
1670 } else if (array
.level
!= LEVEL_CONTAINER
) {
1671 size
= get_component_size(fd
)/2;
1676 /* See if there is anything else to do */
1677 if ((level
== UnSet
|| level
== array
.level
) &&
1678 (layout_str
== NULL
) &&
1679 (chunksize
== 0 || chunksize
== array
.chunk_size
) &&
1680 (raid_disks
== 0 || raid_disks
== array
.raid_disks
)) {
1681 /* Nothing more to do */
1682 if (!changed
&& !quiet
)
1683 fprintf(stderr
, Name
": %s: no change requested\n",
1688 /* ========= check for Raid10/Raid1 -> Raid0 conversion ===============
1689 * current implementation assumes that following conditions must be met:
1692 * - near_copies == 2
1694 if ((level
== 0 && array
.level
== 10 && sra
&&
1695 array
.layout
== ((1 << 8) + 2) && !(array
.raid_disks
& 1)) ||
1696 (level
== 0 && array
.level
== 1 && sra
)) {
1698 err
= remove_disks_for_takeover(st
, sra
, array
.layout
);
1700 dprintf(Name
": Array cannot be reshaped\n");
1706 /* Make sure mdmon has seen the device removal
1707 * and updated metadata before we continue with
1711 ping_monitor(container
);
1714 memset(&info
, 0, sizeof(info
));
1716 sysfs_init(&info
, fd
, NoMdDev
);
1717 strcpy(info
.text_version
, sra
->text_version
);
1718 info
.component_size
= size
*2;
1719 info
.new_level
= level
;
1720 info
.new_chunk
= chunksize
* 1024;
1721 if (info
.array
.level
== LEVEL_CONTAINER
) {
1722 info
.delta_disks
= UnSet
;
1723 info
.array
.raid_disks
= raid_disks
;
1724 } else if (raid_disks
)
1725 info
.delta_disks
= raid_disks
- info
.array
.raid_disks
;
1727 info
.delta_disks
= UnSet
;
1728 if (layout_str
== NULL
) {
1729 info
.new_layout
= UnSet
;
1730 if (info
.array
.level
== 6 &&
1731 (info
.new_level
== 6 || info
.new_level
== UnSet
) &&
1732 info
.array
.layout
>= 16) {
1733 fprintf(stderr
, Name
1734 ": %s has a non-standard layout. If you"
1735 " wish to preserve this\n"
1736 " during the reshape, please specify"
1737 " --layout=preserve\n"
1738 " If you want to change it, specify a"
1739 " layout or use --layout=normalise\n",
1744 } else if (strcmp(layout_str
, "normalise") == 0 ||
1745 strcmp(layout_str
, "normalize") == 0) {
1746 /* If we have a -6 RAID6 layout, remove the '-6'. */
1747 info
.new_layout
= UnSet
;
1748 if (info
.array
.level
== 6 && info
.new_level
== UnSet
) {
1750 strcpy(l
, map_num(r6layout
, info
.array
.layout
));
1751 h
= strrchr(l
, '-');
1752 if (h
&& strcmp(h
, "-6") == 0) {
1754 info
.new_layout
= map_name(r6layout
, l
);
1757 } else if (strcmp(layout_str
, "preserve") == 0) {
1758 info
.new_layout
= UnSet
;
1760 int l
= info
.new_level
;
1762 l
= info
.array
.level
;
1765 info
.new_layout
= map_name(r5layout
, layout_str
);
1768 info
.new_layout
= map_name(r6layout
, layout_str
);
1771 info
.new_layout
= parse_layout_10(layout_str
);
1774 info
.new_layout
= parse_layout_faulty(layout_str
);
1777 fprintf(stderr
, Name
": layout not meaningful"
1778 " with this level\n");
1782 if (info
.new_layout
== UnSet
) {
1783 fprintf(stderr
, Name
": layout %s not understood"
1784 " for this level\n",
1791 if (array
.level
== LEVEL_FAULTY
) {
1792 if (level
!= UnSet
&& level
!= array
.level
) {
1793 fprintf(stderr
, Name
": cannot change level of Faulty device\n");
1797 fprintf(stderr
, Name
": cannot set chunksize of Faulty device\n");
1800 if (raid_disks
&& raid_disks
!= 1) {
1801 fprintf(stderr
, Name
": cannot set raid_disks of Faulty device\n");
1805 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
1806 dprintf("Cannot get array information.\n");
1809 array
.layout
= info
.new_layout
;
1810 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
1811 fprintf(stderr
, Name
": failed to set new layout\n");
1814 printf("layout for %s set to %d\n",
1815 devname
, array
.layout
);
1817 } else if (array
.level
== LEVEL_CONTAINER
) {
1818 /* This change is to be applied to every array in the
1819 * container. This is only needed when the metadata imposes
1820 * restraints of the various arrays in the container.
1821 * Currently we only know that IMSM requires all arrays
1822 * to have the same number of devices so changing the
1823 * number of devices (On-Line Capacity Expansion) must be
1824 * performed at the level of the container
1826 rv
= reshape_container(container
, devname
, -1, st
, &info
,
1827 force
, backup_file
, quiet
, 0, 0);
1830 /* get spare devices from external metadata
1832 if (st
->ss
->external
) {
1833 struct mdinfo
*info2
;
1835 info2
= st
->ss
->container_content(st
, subarray
);
1837 info
.array
.spare_disks
=
1838 info2
->array
.spare_disks
;
1843 /* Impose these changes on a single array. First
1844 * check that the metadata is OK with the change. */
1846 if (reshape_super(st
, info
.component_size
, info
.new_level
,
1847 info
.new_layout
, info
.new_chunk
,
1848 info
.array
.raid_disks
, info
.delta_disks
,
1849 backup_file
, devname
, quiet
)) {
1854 rv
= reshape_array(container
, fd
, devname
, st
, &info
, force
,
1855 devlist
, backup_file
, quiet
, 0, 0, 0);
1865 /* verify_reshape_position()
1866 * Function checks if reshape position in metadata is not farther
1867 * than position in md.
1869 * 0 : not valid sysfs entry
1870 * it can be caused by not started reshape, it should be started
1871 * by reshape array or raid0 array is before takeover
1872 * -1 : error, reshape position is obviously wrong
1873 * 1 : success, reshape progress correct or updated
1875 static int verify_reshape_position(struct mdinfo
*info
, int level
)
1880 /* read sync_max, failure can mean raid0 array */
1881 if (sysfs_get_str(info
, NULL
, "sync_max", buf
, 40) > 0) {
1883 unsigned long long position
= strtoull(buf
, &ep
, 0);
1885 dprintf(Name
": Read sync_max sysfs entry is: %s\n", buf
);
1886 if (!(ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))) {
1887 position
*= get_data_disks(level
,
1889 info
->array
.raid_disks
);
1890 if (info
->reshape_progress
< position
) {
1891 dprintf("Corrected reshape progress (%llu) to "
1892 "md position (%llu)\n",
1893 info
->reshape_progress
, position
);
1894 info
->reshape_progress
= position
;
1896 } else if (info
->reshape_progress
> position
) {
1897 fprintf(stderr
, Name
": Fatal error: array "
1898 "reshape was not properly frozen "
1899 "(expected reshape position is %llu, "
1900 "but reshape progress is %llu.\n",
1901 position
, info
->reshape_progress
);
1904 dprintf("Reshape position in md and metadata "
1914 static int reshape_array(char *container
, int fd
, char *devname
,
1915 struct supertype
*st
, struct mdinfo
*info
,
1916 int force
, struct mddev_dev
*devlist
,
1917 char *backup_file
, int quiet
, int forked
,
1918 int restart
, int freeze_reshape
)
1920 struct reshape reshape
;
1923 int orig_level
= UnSet
;
1926 struct mdu_array_info_s array
;
1929 struct mddev_dev
*dv
;
1933 unsigned long long *offsets
= NULL
;
1937 unsigned long blocks
;
1938 unsigned long cache
;
1939 unsigned long long array_size
;
1941 struct mdinfo
*sra
= NULL
;
1943 /* when reshaping a RAID0, the component_size might be zero.
1944 * So try to fix that up.
1946 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
1947 dprintf("Cannot get array information.\n");
1950 if (array
.level
== 0 && info
->component_size
== 0) {
1951 get_dev_size(fd
, NULL
, &array_size
);
1952 info
->component_size
= array_size
/ array
.raid_disks
;
1955 if (info
->reshape_active
) {
1956 int new_level
= info
->new_level
;
1957 info
->new_level
= UnSet
;
1958 if (info
->delta_disks
> 0)
1959 info
->array
.raid_disks
-= info
->delta_disks
;
1960 msg
= analyse_change(info
, &reshape
);
1961 info
->new_level
= new_level
;
1962 if (info
->delta_disks
> 0)
1963 info
->array
.raid_disks
+= info
->delta_disks
;
1965 /* Make sure the array isn't read-only */
1966 ioctl(fd
, RESTART_ARRAY_RW
, 0);
1968 msg
= analyse_change(info
, &reshape
);
1970 fprintf(stderr
, Name
": %s\n", msg
);
1974 (reshape
.level
!= info
->array
.level
||
1975 reshape
.before
.layout
!= info
->array
.layout
||
1976 reshape
.before
.data_disks
+ reshape
.parity
1977 != info
->array
.raid_disks
- max(0, info
->delta_disks
))) {
1978 fprintf(stderr
, Name
": reshape info is not in native format -"
1979 " cannot continue.\n");
1984 /* reshape already started. just skip to monitoring the reshape */
1985 if (reshape
.backup_blocks
== 0)
1989 /* The container is frozen but the array may not be.
1990 * So freeze the array so spares don't get put to the wrong use
1991 * FIXME there should probably be a cleaner separation between
1992 * freeze_array and freeze_container.
1994 sysfs_freeze_array(info
);
1995 /* Check we have enough spares to not be degraded */
1997 for (dv
= devlist
; dv
; dv
=dv
->next
)
1999 spares_needed
= max(reshape
.before
.data_disks
,
2000 reshape
.after
.data_disks
)
2001 + reshape
.parity
- array
.raid_disks
;
2004 info
->new_level
> 1 && info
->array
.level
> 1 &&
2005 spares_needed
> info
->array
.spare_disks
+ added_disks
) {
2007 Name
": Need %d spare%s to avoid degraded array,"
2008 " and only have %d.\n"
2009 " Use --force to over-ride this check.\n",
2011 spares_needed
== 1 ? "" : "s",
2012 info
->array
.spare_disks
+ added_disks
);
2015 /* Check we have enough spares to not fail */
2016 spares_needed
= max(reshape
.before
.data_disks
,
2017 reshape
.after
.data_disks
)
2019 if ((info
->new_level
> 1 || info
->new_level
== 0) &&
2020 spares_needed
> info
->array
.spare_disks
+added_disks
) {
2022 Name
": Need %d spare%s to create working array,"
2023 " and only have %d.\n",
2025 spares_needed
== 1 ? "" : "s",
2026 info
->array
.spare_disks
+ added_disks
);
2030 if (reshape
.level
!= array
.level
) {
2031 char *c
= map_num(pers
, reshape
.level
);
2036 err
= sysfs_set_str(info
, NULL
, "level", c
);
2039 fprintf(stderr
, Name
": %s: could not set level to %s\n",
2042 (info
->array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2043 fprintf(stderr
, " Bitmap must be removed"
2044 " before level can be changed\n");
2048 fprintf(stderr
, Name
": level of %s changed to %s\n",
2050 orig_level
= array
.level
;
2051 sysfs_freeze_array(info
);
2053 if (reshape
.level
> 0 && st
->ss
->external
) {
2054 /* make sure mdmon is aware of the new level */
2055 if (mdmon_running(st
->container_dev
))
2056 flush_mdmon(container
);
2058 if (!mdmon_running(st
->container_dev
))
2059 start_mdmon(st
->container_dev
);
2060 ping_monitor(container
);
2061 if (mdmon_running(st
->container_dev
) &&
2062 st
->update_tail
== NULL
)
2063 st
->update_tail
= &st
->updates
;
2066 /* ->reshape_super might have chosen some spares from the
2067 * container that it wants to be part of the new array.
2068 * We can collect them with ->container_content and give
2069 * them to the kernel.
2071 if (st
->ss
->reshape_super
&& st
->ss
->container_content
) {
2072 char *subarray
= strchr(info
->text_version
+1, '/')+1;
2073 struct mdinfo
*info2
=
2074 st
->ss
->container_content(st
, subarray
);
2078 sysfs_init(info2
, fd
, st
->devnum
);
2079 /* When increasing number of devices, we need to set
2080 * new raid_disks before adding these, or they might
2083 if (reshape
.backup_blocks
&&
2084 reshape
.after
.data_disks
> reshape
.before
.data_disks
)
2085 subarray_set_num(container
, info2
, "raid_disks",
2086 reshape
.after
.data_disks
+
2088 for (d
= info2
->devs
; d
; d
= d
->next
) {
2089 if (d
->disk
.state
== 0 &&
2090 d
->disk
.raid_disk
>= 0) {
2091 /* This is a spare that wants to
2092 * be part of the array.
2094 add_disk(fd
, st
, info2
, d
);
2100 /* We might have been given some devices to add to the
2101 * array. Now that the array has been changed to the right
2102 * level and frozen, we can safely add them.
2105 Manage_subdevs(devname
, fd
, devlist
, !quiet
,
2108 if (reshape
.backup_blocks
== 0) {
2109 /* No restriping needed, but we might need to impose
2110 * some more changes: layout, raid_disks, chunk_size
2112 /* read current array info */
2113 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
2114 dprintf("Cannot get array information.\n");
2117 /* compare current array info with new values and if
2118 * it is different update them to new */
2119 if (info
->new_layout
!= UnSet
&&
2120 info
->new_layout
!= array
.layout
) {
2121 array
.layout
= info
->new_layout
;
2122 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2123 fprintf(stderr
, Name
": failed to set new layout\n");
2126 printf("layout for %s set to %d\n",
2127 devname
, array
.layout
);
2129 if (info
->delta_disks
!= UnSet
&&
2130 info
->delta_disks
!= 0 &&
2131 array
.raid_disks
!= (info
->array
.raid_disks
+ info
->delta_disks
)) {
2132 array
.raid_disks
+= info
->delta_disks
;
2133 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2134 fprintf(stderr
, Name
": failed to set raid disks\n");
2136 } else if (!quiet
) {
2137 printf("raid_disks for %s set to %d\n",
2138 devname
, array
.raid_disks
);
2141 if (info
->new_chunk
!= 0 &&
2142 info
->new_chunk
!= array
.chunk_size
) {
2143 if (sysfs_set_num(info
, NULL
,
2144 "chunk_size", info
->new_chunk
) != 0) {
2145 fprintf(stderr
, Name
": failed to set chunk size\n");
2148 printf("chunk size for %s set to %d\n",
2149 devname
, array
.chunk_size
);
2156 * There are three possibilities.
2157 * 1/ The array will shrink.
2158 * We need to ensure the reshape will pause before reaching
2159 * the 'critical section'. We also need to fork and wait for
2160 * that to happen. When it does we
2161 * suspend/backup/complete/unfreeze
2163 * 2/ The array will not change size.
2164 * This requires that we keep a backup of a sliding window
2165 * so that we can restore data after a crash. So we need
2166 * to fork and monitor progress.
2167 * In future we will allow the data_offset to change, so
2168 * a sliding backup becomes unnecessary.
2170 * 3/ The array will grow. This is relatively easy.
2171 * However the kernel's restripe routines will cheerfully
2172 * overwrite some early data before it is safe. So we
2173 * need to make a backup of the early parts of the array
2174 * and be ready to restore it if rebuild aborts very early.
2175 * For externally managed metadata, we still need a forked
2176 * child to monitor the reshape and suspend IO over the region
2177 * that is being reshaped.
2179 * We backup data by writing it to one spare, or to a
2180 * file which was given on command line.
2182 * In each case, we first make sure that storage is available
2183 * for the required backup.
2185 * - request the shape change.
2186 * - fork to handle backup etc.
2189 /* Check that we can hold all the data */
2190 get_dev_size(fd
, NULL
, &array_size
);
2191 if (reshape
.new_size
< (array_size
/512)) {
2193 Name
": this change will reduce the size of the array.\n"
2194 " use --grow --array-size first to truncate array.\n"
2195 " e.g. mdadm --grow %s --array-size %llu\n",
2196 devname
, reshape
.new_size
/2);
2200 sra
= sysfs_read(fd
, 0,
2201 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
|
2204 fprintf(stderr
, Name
": %s: Cannot get array details from sysfs\n",
2209 /* Decide how many blocks (sectors) for a reshape
2210 * unit. The number we have so far is just a minimum
2212 blocks
= reshape
.backup_blocks
;
2213 if (reshape
.before
.data_disks
==
2214 reshape
.after
.data_disks
) {
2215 /* Make 'blocks' bigger for better throughput, but
2216 * not so big that we reject it below.
2217 * Try for 16 megabytes
2219 while (blocks
* 32 < sra
->component_size
&&
2223 fprintf(stderr
, Name
": Need to backup %luK of critical "
2224 "section..\n", blocks
/2);
2226 if (blocks
>= sra
->component_size
/2) {
2227 fprintf(stderr
, Name
": %s: Something wrong"
2228 " - reshape aborted\n",
2233 /* Now we need to open all these devices so we can read/write.
2235 nrdisks
= max(reshape
.before
.data_disks
,
2236 reshape
.after
.data_disks
) + reshape
.parity
2237 + sra
->array
.spare_disks
;
2238 fdlist
= malloc((1+nrdisks
) * sizeof(int));
2239 offsets
= malloc((1+nrdisks
) * sizeof(offsets
[0]));
2240 if (!fdlist
|| !offsets
) {
2241 fprintf(stderr
, Name
": malloc failed: grow aborted\n");
2245 odisks
= reshape
.before
.data_disks
+ reshape
.parity
;
2246 d
= reshape_prepare_fdlist(devname
, sra
, odisks
,
2247 nrdisks
, blocks
, backup_file
,
2252 if ((st
->ss
->manage_reshape
== NULL
) ||
2253 (st
->ss
->recover_backup
== NULL
)) {
2254 if (backup_file
== NULL
) {
2255 if (reshape
.after
.data_disks
<=
2256 reshape
.before
.data_disks
) {
2257 fprintf(stderr
, Name
": %s: Cannot grow - "
2258 "need backup-file\n", devname
);
2260 } else if (sra
->array
.spare_disks
== 0) {
2261 fprintf(stderr
, Name
": %s: Cannot grow - "
2262 "need a spare or backup-file to backup "
2263 "critical section\n", devname
);
2267 if (!reshape_open_backup_file(backup_file
, fd
, devname
,
2269 fdlist
+d
, offsets
+d
,
2277 /* lastly, check that the internal stripe cache is
2278 * large enough, or it won't work.
2279 * It must hold at least 4 stripes of the larger
2282 cache
= max(info
->array
.chunk_size
, info
->new_chunk
);
2283 cache
*= 4; /* 4 stripes minimum */
2284 cache
/= 512; /* convert to sectors */
2285 disks
= min(reshape
.before
.data_disks
, reshape
.after
.data_disks
);
2286 /* make sure there is room for 'blocks' with a bit to spare */
2287 if (cache
< 16 + blocks
/ disks
)
2288 cache
= 16 + blocks
/ disks
;
2289 cache
/= (4096/512); /* Covert from sectors to pages */
2291 if (sra
->cache_size
< cache
)
2292 subarray_set_num(container
, sra
, "stripe_cache_size",
2295 /* Right, everything seems fine. Let's kick things off.
2296 * If only changing raid_disks, use ioctl, else use
2301 sra
->new_chunk
= info
->new_chunk
;
2304 /* for external metadata checkpoint saved by mdmon can be lost
2305 * or missed /due to e.g. crash/. Check if md is not during
2306 * restart farther than metadata points to.
2307 * If so, this means metadata information is obsolete.
2309 if (st
->ss
->external
)
2310 verify_reshape_position(info
, reshape
.level
);
2311 sra
->reshape_progress
= info
->reshape_progress
;
2313 sra
->reshape_progress
= 0;
2314 if (reshape
.after
.data_disks
< reshape
.before
.data_disks
)
2315 /* start from the end of the new array */
2316 sra
->reshape_progress
= (sra
->component_size
2317 * reshape
.after
.data_disks
);
2320 if (info
->array
.chunk_size
== info
->new_chunk
&&
2321 reshape
.before
.layout
== reshape
.after
.layout
&&
2322 st
->ss
->external
== 0) {
2323 /* use SET_ARRAY_INFO but only if reshape hasn't started */
2324 ioctl(fd
, GET_ARRAY_INFO
, &array
);
2325 array
.raid_disks
= reshape
.after
.data_disks
+ reshape
.parity
;
2327 ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2331 Name
": Cannot set device shape for %s: %s\n",
2332 devname
, strerror(errno
));
2335 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2337 " Bitmap must be removed before"
2338 " shape can be changed\n");
2342 } else if (!restart
) {
2343 /* set them all just in case some old 'new_*' value
2344 * persists from some earlier problem.
2347 if (sysfs_set_num(sra
, NULL
, "chunk_size", info
->new_chunk
) < 0)
2349 if (!err
&& sysfs_set_num(sra
, NULL
, "layout",
2350 reshape
.after
.layout
) < 0)
2352 if (!err
&& subarray_set_num(container
, sra
, "raid_disks",
2353 reshape
.after
.data_disks
+
2354 reshape
.parity
) < 0)
2357 fprintf(stderr
, Name
": Cannot set device shape for %s\n",
2361 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2363 " Bitmap must be removed before"
2364 " shape can be changed\n");
2369 err
= start_reshape(sra
, restart
, reshape
.before
.data_disks
,
2370 reshape
.after
.data_disks
);
2373 Name
": Cannot %s reshape for %s\n",
2374 restart
? "continue" : "start",
2379 sysfs_set_str(sra
, NULL
, "array_state", "active");
2380 if (freeze_reshape
) {
2384 fprintf(stderr
, Name
": Reshape has to be continued from"
2385 " location %llu when root filesystem has been mounted.\n",
2386 sra
->reshape_progress
);
2390 /* Now we just need to kick off the reshape and watch, while
2391 * handling backups of the data...
2392 * This is all done by a forked background process.
2394 switch(forked
? 0 : fork()) {
2396 fprintf(stderr
, Name
": Cannot run child to monitor reshape: %s\n",
2411 if (check_env("MDADM_GROW_VERIFY"))
2412 fd
= open(devname
, O_RDONLY
| O_DIRECT
);
2415 mlockall(MCL_FUTURE
);
2417 if (st
->ss
->external
) {
2418 /* metadata handler takes it from here */
2419 done
= st
->ss
->manage_reshape(
2420 fd
, sra
, &reshape
, st
, blocks
,
2422 d
- odisks
, fdlist
+odisks
,
2425 done
= child_monitor(
2426 fd
, sra
, &reshape
, st
, blocks
,
2428 d
- odisks
, fdlist
+odisks
,
2434 if (backup_file
&& done
)
2435 unlink(backup_file
);
2441 if (!st
->ss
->external
&&
2442 !(reshape
.before
.data_disks
!= reshape
.after
.data_disks
2443 && info
->custom_array_size
) &&
2444 info
->new_level
== reshape
.level
&&
2446 /* no need to wait for the reshape to finish as
2447 * there is nothing more to do.
2454 if (st
->ss
->external
) {
2455 /* Re-load the metadata as much could have changed */
2456 int cfd
= open_dev(st
->container_dev
);
2458 flush_mdmon(container
);
2459 st
->ss
->free_super(st
);
2460 st
->ss
->load_container(st
, cfd
, container
);
2465 /* set new array size if required customer_array_size is used
2468 if (reshape
.before
.data_disks
!=
2469 reshape
.after
.data_disks
&&
2470 info
->custom_array_size
) {
2471 struct mdinfo
*info2
;
2472 char *subarray
= strchr(info
->text_version
+1, '/')+1;
2474 info2
= st
->ss
->container_content(st
, subarray
);
2476 unsigned long long current_size
= 0;
2477 unsigned long long new_size
=
2478 info2
->custom_array_size
/2;
2480 if (sysfs_get_ll(sra
,
2483 ¤t_size
) == 0 &&
2484 new_size
> current_size
) {
2485 if (sysfs_set_num(sra
, NULL
,
2486 "array_size", new_size
)
2488 dprintf("Error: Cannot"
2491 dprintf("Array size "
2493 dprintf(" from %llu to %llu.\n",
2494 current_size
, new_size
);
2500 if (info
->new_level
!= reshape
.level
) {
2502 c
= map_num(pers
, info
->new_level
);
2504 err
= sysfs_set_str(sra
, NULL
, "level", c
);
2506 fprintf(stderr
, Name\
2507 ": %s: could not set level "
2508 "to %s\n", devname
, c
);
2510 if (info
->new_level
== 0)
2511 st
->update_tail
= NULL
;
2523 if (orig_level
!= UnSet
&& sra
) {
2524 c
= map_num(pers
, orig_level
);
2525 if (c
&& sysfs_set_str(sra
, NULL
, "level", c
) == 0)
2526 fprintf(stderr
, Name
": aborting level change\n");
2534 /* mdfd handle is passed to be closed in child process (after fork).
2536 int reshape_container(char *container
, char *devname
,
2538 struct supertype
*st
,
2539 struct mdinfo
*info
,
2542 int quiet
, int restart
, int freeze_reshape
)
2544 struct mdinfo
*cc
= NULL
;
2546 int last_devnum
= -1;
2548 /* component_size is not meaningful for a container,
2549 * so pass '-1' meaning 'no change'
2552 reshape_super(st
, -1, info
->new_level
,
2553 info
->new_layout
, info
->new_chunk
,
2554 info
->array
.raid_disks
, info
->delta_disks
,
2555 backup_file
, devname
, quiet
)) {
2562 /* ping monitor to be sure that update is on disk
2564 ping_monitor(container
);
2567 case -1: /* error */
2568 perror("Cannot fork to complete reshape\n");
2571 default: /* parent */
2572 if (!freeze_reshape
)
2573 printf(Name
": multi-array reshape continues"
2574 " in background\n");
2581 /* close unused handle in child process
2587 /* For each member array with reshape_active,
2588 * we need to perform the reshape.
2589 * We pick the first array that needs reshaping and
2590 * reshape it. reshape_array() will re-read the metadata
2591 * so the next time through a different array should be
2592 * ready for reshape.
2593 * It is possible that the 'different' array will not
2594 * be assembled yet. In that case we simple exit.
2595 * When it is assembled, the mdadm which assembles it
2596 * will take over the reshape.
2598 struct mdinfo
*content
;
2600 struct mdstat_ent
*mdstat
;
2605 cc
= st
->ss
->container_content(st
, NULL
);
2607 for (content
= cc
; content
; content
= content
->next
) {
2609 if (!content
->reshape_active
)
2612 subarray
= strchr(content
->text_version
+1, '/')+1;
2613 mdstat
= mdstat_by_subdev(subarray
,
2614 devname2devnum(container
));
2622 adev
= map_dev(dev2major(mdstat
->devnum
),
2623 dev2minor(mdstat
->devnum
),
2626 adev
= content
->text_version
;
2628 fd
= open_dev(mdstat
->devnum
);
2630 printf(Name
": Device %s cannot be opened for reshape.",
2635 if (last_devnum
== mdstat
->devnum
) {
2636 /* Do not allow for multiple reshape_array() calls for
2638 * It can happen when reshape_array() returns without
2639 * error, when reshape is not finished (wrong reshape
2640 * starting/continuation conditions). Mdmon doesn't
2641 * switch to next array in container and reentry
2642 * conditions for the same array occur.
2643 * This is possibly interim until the behaviour of
2644 * reshape_array is resolved().
2646 printf(Name
": Multiple reshape execution detected for "
2647 "device %s.", adev
);
2651 last_devnum
= mdstat
->devnum
;
2653 sysfs_init(content
, fd
, mdstat
->devnum
);
2655 if (mdmon_running(devname2devnum(container
)))
2656 flush_mdmon(container
);
2658 rv
= reshape_array(container
, fd
, adev
, st
,
2659 content
, force
, NULL
,
2660 backup_file
, quiet
, 1, restart
,
2664 if (freeze_reshape
) {
2673 if (mdmon_running(devname2devnum(container
)))
2674 flush_mdmon(container
);
2683 * We run a child process in the background which performs the following
2685 * - wait for resync to reach a certain point
2686 * - suspend io to the following section
2687 * - backup that section
2688 * - allow resync to proceed further
2690 * - discard the backup.
2692 * When are combined in slightly different ways in the three cases.
2694 * - suspend/backup/allow/wait/resume/discard
2696 * - allow/wait/suspend/backup/allow/wait/resume/discard
2698 * - wait/resume/discard/suspend/backup/allow
2700 * suspend/backup/allow always come together
2701 * wait/resume/discard do too.
2702 * For the same-size case we have two backups to improve flow.
2706 int progress_reshape(struct mdinfo
*info
, struct reshape
*reshape
,
2707 unsigned long long backup_point
,
2708 unsigned long long wait_point
,
2709 unsigned long long *suspend_point
,
2710 unsigned long long *reshape_completed
)
2712 /* This function is called repeatedly by the reshape manager.
2713 * It determines how much progress can safely be made and allows
2715 * - 'info' identifies the array and particularly records in
2716 * ->reshape_progress the metadata's knowledge of progress
2717 * This is a sector offset from the start of the array
2718 * of the next array block to be relocated. This number
2719 * may increase from 0 or decrease from array_size, depending
2720 * on the type of reshape that is happening.
2721 * Note that in contrast, 'sync_completed' is a block count of the
2722 * reshape so far. It gives the distance between the start point
2723 * (head or tail of device) and the next place that data will be
2724 * written. It always increases.
2725 * - 'reshape' is the structure created by analyse_change
2726 * - 'backup_point' shows how much the metadata manager has backed-up
2727 * data. For reshapes with increasing progress, it is the next address
2728 * to be backed up, previous addresses have been backed-up. For
2729 * decreasing progress, it is the earliest address that has been
2730 * backed up - later address are also backed up.
2731 * So addresses between reshape_progress and backup_point are
2732 * backed up providing those are in the 'correct' order.
2733 * - 'wait_point' is an array address. When reshape_completed
2734 * passes this point, progress_reshape should return. It might
2735 * return earlier if it determines that ->reshape_progress needs
2736 * to be updated or further backup is needed.
2737 * - suspend_point is maintained by progress_reshape and the caller
2738 * should not touch it except to initialise to zero.
2739 * It is an array address and it only increases in 2.6.37 and earlier.
2740 * This makes it difficult to handle reducing reshapes with
2741 * external metadata.
2742 * However: it is similar to backup_point in that it records the
2743 * other end of a suspended region from reshape_progress.
2744 * it is moved to extend the region that is safe to backup and/or
2746 * - reshape_completed is read from sysfs and returned. The caller
2747 * should copy this into ->reshape_progress when it has reason to
2748 * believe that the metadata knows this, and any backup outside this
2752 * 1 if more data from backup_point - but only as far as suspend_point,
2753 * should be backed up
2754 * 0 if things are progressing smoothly
2755 * -1 if the reshape is finished because it is all done,
2756 * -2 if the reshape is finished due to an error.
2759 int advancing
= (reshape
->after
.data_disks
2760 >= reshape
->before
.data_disks
);
2761 unsigned long long need_backup
; /* All data between start of array and
2762 * here will at some point need to
2765 unsigned long long read_offset
, write_offset
;
2766 unsigned long long write_range
;
2767 unsigned long long max_progress
, target
, completed
;
2768 unsigned long long array_size
= (info
->component_size
2769 * reshape
->before
.data_disks
);
2773 /* First, we unsuspend any region that is now known to be safe.
2774 * If suspend_point is on the 'wrong' side of reshape_progress, then
2775 * we don't have or need suspension at the moment. This is true for
2776 * native metadata when we don't need to back-up.
2779 if (info
->reshape_progress
<= *suspend_point
)
2780 sysfs_set_num(info
, NULL
, "suspend_lo",
2781 info
->reshape_progress
);
2783 /* Note: this won't work in 2.6.37 and before.
2784 * Something somewhere should make sure we don't need it!
2786 if (info
->reshape_progress
>= *suspend_point
)
2787 sysfs_set_num(info
, NULL
, "suspend_hi",
2788 info
->reshape_progress
);
2791 /* Now work out how far it is safe to progress.
2792 * If the read_offset for ->reshape_progress is less than
2793 * 'blocks' beyond the write_offset, we can only progress as far
2795 * Otherwise we can progress until the write_offset for the new location
2796 * reaches (within 'blocks' of) the read_offset at the current location.
2797 * However that region must be suspended unless we are using native
2799 * If we need to suspend more, we limit it to 128M per device, which is
2800 * rather arbitrary and should be some time-based calculation.
2802 read_offset
= info
->reshape_progress
/ reshape
->before
.data_disks
;
2803 write_offset
= info
->reshape_progress
/ reshape
->after
.data_disks
;
2804 write_range
= info
->new_chunk
/512;
2805 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
2806 need_backup
= array_size
;
2808 need_backup
= reshape
->backup_blocks
;
2810 if (read_offset
< write_offset
+ write_range
)
2811 max_progress
= backup_point
;
2815 reshape
->after
.data_disks
;
2817 if (read_offset
> write_offset
- write_range
)
2818 /* Can only progress as far as has been backed up,
2819 * which must be suspended */
2820 max_progress
= backup_point
;
2821 else if (info
->reshape_progress
<= need_backup
)
2822 max_progress
= backup_point
;
2824 if (info
->array
.major_version
>= 0)
2825 /* Can progress until backup is needed */
2826 max_progress
= need_backup
;
2828 /* Can progress until metadata update is required */
2831 reshape
->after
.data_disks
;
2832 /* but data must be suspended */
2833 if (max_progress
< *suspend_point
)
2834 max_progress
= *suspend_point
;
2839 /* We know it is safe to progress to 'max_progress' providing
2840 * it is suspended or we are using native metadata.
2841 * Consider extending suspend_point 128M per device if it
2842 * is less than 64M per device beyond reshape_progress.
2843 * But always do a multiple of 'blocks'
2844 * FIXME this is too big - it takes to long to complete
2847 target
= 64*1024*2 * min(reshape
->before
.data_disks
,
2848 reshape
->after
.data_disks
);
2849 target
/= reshape
->backup_blocks
;
2852 target
*= reshape
->backup_blocks
;
2854 /* For externally managed metadata we always need to suspend IO to
2855 * the area being reshaped so we regularly push suspend_point forward.
2856 * For native metadata we only need the suspend if we are going to do
2860 if ((need_backup
> info
->reshape_progress
2861 || info
->array
.major_version
< 0) &&
2862 *suspend_point
< info
->reshape_progress
+ target
) {
2863 if (need_backup
< *suspend_point
+ 2 * target
)
2864 *suspend_point
= need_backup
;
2865 else if (*suspend_point
+ 2 * target
< array_size
)
2866 *suspend_point
+= 2 * target
;
2868 *suspend_point
= array_size
;
2869 sysfs_set_num(info
, NULL
, "suspend_hi", *suspend_point
);
2870 if (max_progress
> *suspend_point
)
2871 max_progress
= *suspend_point
;
2874 if (info
->array
.major_version
>= 0) {
2875 /* Only need to suspend when about to backup */
2876 if (info
->reshape_progress
< need_backup
* 2 &&
2877 *suspend_point
> 0) {
2879 sysfs_set_num(info
, NULL
, "suspend_lo", 0);
2880 sysfs_set_num(info
, NULL
, "suspend_hi", need_backup
);
2883 /* Need to suspend continually */
2884 if (info
->reshape_progress
< *suspend_point
)
2885 *suspend_point
= info
->reshape_progress
;
2886 if (*suspend_point
+ target
< info
->reshape_progress
)
2887 /* No need to move suspend region yet */;
2889 if (*suspend_point
>= 2 * target
)
2890 *suspend_point
-= 2 * target
;
2893 sysfs_set_num(info
, NULL
, "suspend_lo",
2896 if (max_progress
< *suspend_point
)
2897 max_progress
= *suspend_point
;
2901 /* now set sync_max to allow that progress. sync_max, like
2902 * sync_completed is a count of sectors written per device, so
2903 * we find the difference between max_progress and the start point,
2904 * and divide that by after.data_disks to get a sync_max
2906 * At the same time we convert wait_point to a similar number
2907 * for comparing against sync_completed.
2909 /* scale down max_progress to per_disk */
2910 max_progress
/= reshape
->after
.data_disks
;
2911 /* Round to chunk size as some kernels give an erroneously high number */
2912 max_progress
/= info
->new_chunk
/512;
2913 max_progress
*= info
->new_chunk
/512;
2914 /* And round to old chunk size as the kernel wants that */
2915 max_progress
/= info
->array
.chunk_size
/512;
2916 max_progress
*= info
->array
.chunk_size
/512;
2917 /* Limit progress to the whole device */
2918 if (max_progress
> info
->component_size
)
2919 max_progress
= info
->component_size
;
2920 wait_point
/= reshape
->after
.data_disks
;
2922 /* switch from 'device offset' to 'processed block count' */
2923 max_progress
= info
->component_size
- max_progress
;
2924 wait_point
= info
->component_size
- wait_point
;
2927 sysfs_set_num(info
, NULL
, "sync_max", max_progress
);
2929 /* Now wait. If we have already reached the point that we were
2930 * asked to wait to, don't wait at all, else wait for any change.
2931 * We need to select on 'sync_completed' as that is the place that
2932 * notifications happen, but we are really interested in
2933 * 'reshape_position'
2935 fd
= sysfs_get_fd(info
, NULL
, "sync_completed");
2937 goto check_progress
;
2939 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
2940 goto check_progress
;
2942 while (completed
< max_progress
&& completed
< wait_point
) {
2943 /* Check that sync_action is still 'reshape' to avoid
2944 * waiting forever on a dead array
2948 if (sysfs_get_str(info
, NULL
, "sync_action",
2950 strncmp(action
, "reshape", 7) != 0)
2952 /* Some kernels reset 'sync_completed' to zero
2953 * before setting 'sync_action' to 'idle'.
2954 * So we need these extra tests.
2956 if (completed
== 0 && advancing
2957 && info
->reshape_progress
> 0)
2959 if (completed
== 0 && !advancing
2960 && info
->reshape_progress
< (info
->component_size
2961 * reshape
->after
.data_disks
))
2965 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
2966 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
2967 goto check_progress
;
2969 /* Some kernels reset 'sync_completed' to zero,
2970 * we need to have real point we are in md
2973 completed
= max_progress
;
2975 /* some kernels can give an incorrectly high 'completed' number */
2976 completed
/= (info
->new_chunk
/512);
2977 completed
*= (info
->new_chunk
/512);
2978 /* Convert 'completed' back in to a 'progress' number */
2979 completed
*= reshape
->after
.data_disks
;
2981 completed
= info
->component_size
* reshape
->after
.data_disks
2984 *reshape_completed
= completed
;
2988 /* We return the need_backup flag. Caller will decide
2989 * how much - a multiple of ->backup_blocks up to *suspend_point
2992 return need_backup
> info
->reshape_progress
;
2994 return need_backup
>= info
->reshape_progress
;
2997 /* if we couldn't read a number from sync_completed, then
2998 * either the reshape did complete, or it aborted.
2999 * We can tell which by checking for 'none' in reshape_position.
3000 * If it did abort, then it might immediately restart if it
3001 * it was just a device failure that leaves us degraded but
3005 if (sysfs_get_str(info
, NULL
, "reshape_position", buf
, sizeof(buf
)) < 0
3006 || strncmp(buf
, "none", 4) != 0) {
3007 /* The abort might only be temporary. Wait up to 10
3008 * seconds for fd to contain a valid number again.
3014 while (fd
>= 0 && rv
< 0 && tv
.tv_sec
> 0) {
3018 if (select(fd
+1, NULL
, NULL
, &rfds
, &tv
) != 1)
3020 switch (sysfs_fd_get_ll(fd
, &completed
)) {
3022 /* all good again */
3025 case -2: /* read error - abort */
3032 return rv
; /* abort */
3034 /* Maybe racing with array shutdown - check state */
3037 if (sysfs_get_str(info
, NULL
, "array_state", buf
, sizeof(buf
)) < 0
3038 || strncmp(buf
, "inactive", 8) == 0
3039 || strncmp(buf
, "clear",5) == 0)
3040 return -2; /* abort */
3041 return -1; /* complete */
3045 /* FIXME return status is never checked */
3046 static int grow_backup(struct mdinfo
*sra
,
3047 unsigned long long offset
, /* per device */
3048 unsigned long stripes
, /* per device, in old chunks */
3049 int *sources
, unsigned long long *offsets
,
3050 int disks
, int chunk
, int level
, int layout
,
3051 int dests
, int *destfd
, unsigned long long *destoffsets
,
3052 int part
, int *degraded
,
3055 /* Backup 'blocks' sectors at 'offset' on each device of the array,
3056 * to storage 'destfd' (offset 'destoffsets'), after first
3057 * suspending IO. Then allow resync to continue
3058 * over the suspended section.
3059 * Use part 'part' of the backup-super-block.
3064 unsigned long long ll
;
3066 //printf("offset %llu\n", offset);
3072 /* Check that array hasn't become degraded, else we might backup the wrong data */
3073 if (sysfs_get_ll(sra
, NULL
, "degraded", &ll
) < 0)
3074 return -1; /* FIXME this error is ignored */
3075 new_degraded
= (int)ll
;
3076 if (new_degraded
!= *degraded
) {
3077 /* check each device to ensure it is still working */
3079 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
3080 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
3082 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
3084 if (sysfs_get_str(sra
, sd
, "state", sbuf
, 20) < 0 ||
3085 strstr(sbuf
, "faulty") ||
3086 strstr(sbuf
, "in_sync") == NULL
) {
3087 /* this device is dead */
3088 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
3089 if (sd
->disk
.raid_disk
>= 0 &&
3090 sources
[sd
->disk
.raid_disk
] >= 0) {
3091 close(sources
[sd
->disk
.raid_disk
]);
3092 sources
[sd
->disk
.raid_disk
] = -1;
3097 *degraded
= new_degraded
;
3100 bsb
.arraystart2
= __cpu_to_le64(offset
* odata
);
3101 bsb
.length2
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
3103 bsb
.arraystart
= __cpu_to_le64(offset
* odata
);
3104 bsb
.length
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
3107 bsb
.magic
[15] = '2';
3108 for (i
= 0; i
< dests
; i
++)
3110 lseek64(destfd
[i
], destoffsets
[i
] + __le64_to_cpu(bsb
.devstart2
)*512, 0);
3112 lseek64(destfd
[i
], destoffsets
[i
], 0);
3114 rv
= save_stripes(sources
, offsets
,
3115 disks
, chunk
, level
, layout
,
3117 offset
*512*odata
, stripes
* chunk
* odata
,
3122 bsb
.mtime
= __cpu_to_le64(time(0));
3123 for (i
= 0; i
< dests
; i
++) {
3124 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
3126 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
3127 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
3128 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
3129 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
3132 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
] - 4096, 0)
3133 != destoffsets
[i
] - 4096)
3135 if (write(destfd
[i
], &bsb
, 512) != 512)
3137 if (destoffsets
[i
] > 4096) {
3138 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]+stripes
*chunk
*odata
, 0) !=
3139 destoffsets
[i
]+stripes
*chunk
*odata
)
3141 if (write(destfd
[i
], &bsb
, 512) != 512)
3151 /* in 2.6.30, the value reported by sync_completed can be
3152 * less that it should be by one stripe.
3153 * This only happens when reshape hits sync_max and pauses.
3154 * So allow wait_backup to either extent sync_max further
3155 * than strictly necessary, or return before the
3156 * sync has got quite as far as we would really like.
3157 * This is what 'blocks2' is for.
3158 * The various caller give appropriate values so that
3161 /* FIXME return value is often ignored */
3162 static int forget_backup(int dests
, int *destfd
,
3163 unsigned long long *destoffsets
,
3167 * Erase backup 'part' (which is 0 or 1)
3173 bsb
.arraystart2
= __cpu_to_le64(0);
3174 bsb
.length2
= __cpu_to_le64(0);
3176 bsb
.arraystart
= __cpu_to_le64(0);
3177 bsb
.length
= __cpu_to_le64(0);
3179 bsb
.mtime
= __cpu_to_le64(time(0));
3181 for (i
= 0; i
< dests
; i
++) {
3182 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
3183 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
3184 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
3185 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
3186 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
3187 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]-4096, 0) !=
3188 destoffsets
[i
]-4096)
3191 write(destfd
[i
], &bsb
, 512) != 512)
3198 static void fail(char *msg
)
3201 rv
= (write(2, msg
, strlen(msg
)) != (int)strlen(msg
));
3202 rv
|= (write(2, "\n", 1) != 1);
3206 static char *abuf
, *bbuf
;
3207 static unsigned long long abuflen
;
3208 static void validate(int afd
, int bfd
, unsigned long long offset
)
3210 /* check that the data in the backup against the array.
3211 * This is only used for regression testing and should not
3212 * be used while the array is active
3216 lseek64(bfd
, offset
- 4096, 0);
3217 if (read(bfd
, &bsb2
, 512) != 512)
3218 fail("cannot read bsb");
3219 if (bsb2
.sb_csum
!= bsb_csum((char*)&bsb2
,
3220 ((char*)&bsb2
.sb_csum
)-((char*)&bsb2
)))
3221 fail("first csum bad");
3222 if (memcmp(bsb2
.magic
, "md_backup_data", 14) != 0)
3223 fail("magic is bad");
3224 if (memcmp(bsb2
.magic
, "md_backup_data-2", 16) == 0 &&
3225 bsb2
.sb_csum2
!= bsb_csum((char*)&bsb2
,
3226 ((char*)&bsb2
.sb_csum2
)-((char*)&bsb2
)))
3227 fail("second csum bad");
3229 if (__le64_to_cpu(bsb2
.devstart
)*512 != offset
)
3230 fail("devstart is wrong");
3233 unsigned long long len
= __le64_to_cpu(bsb2
.length
)*512;
3235 if (abuflen
< len
) {
3239 if (posix_memalign((void**)&abuf
, 4096, abuflen
) ||
3240 posix_memalign((void**)&bbuf
, 4096, abuflen
)) {
3242 /* just stop validating on mem-alloc failure */
3247 lseek64(bfd
, offset
, 0);
3248 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
) {
3249 //printf("len %llu\n", len);
3250 fail("read first backup failed");
3252 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart
)*512, 0);
3253 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
3254 fail("read first from array failed");
3255 if (memcmp(bbuf
, abuf
, len
) != 0) {
3258 printf("offset=%llu len=%llu\n",
3259 (unsigned long long)__le64_to_cpu(bsb2
.arraystart
)*512, len
);
3260 for (i
=0; i
<len
; i
++)
3261 if (bbuf
[i
] != abuf
[i
]) {
3262 printf("first diff byte %d\n", i
);
3266 fail("data1 compare failed");
3270 unsigned long long len
= __le64_to_cpu(bsb2
.length2
)*512;
3272 if (abuflen
< len
) {
3276 abuf
= malloc(abuflen
);
3277 bbuf
= malloc(abuflen
);
3280 lseek64(bfd
, offset
+__le64_to_cpu(bsb2
.devstart2
)*512, 0);
3281 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
)
3282 fail("read second backup failed");
3283 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart2
)*512, 0);
3284 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
3285 fail("read second from array failed");
3286 if (memcmp(bbuf
, abuf
, len
) != 0)
3287 fail("data2 compare failed");
3291 int child_monitor(int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
3292 struct supertype
*st
, unsigned long blocks
,
3293 int *fds
, unsigned long long *offsets
,
3294 int dests
, int *destfd
, unsigned long long *destoffsets
)
3296 /* Monitor a reshape where backup is being performed using
3297 * 'native' mechanism - either to a backup file, or
3298 * to some space in a spare.
3302 unsigned long long speed
;
3303 unsigned long long suspend_point
, array_size
;
3304 unsigned long long backup_point
, wait_point
;
3305 unsigned long long reshape_completed
;
3307 int increasing
= reshape
->after
.data_disks
>= reshape
->before
.data_disks
;
3308 int part
= 0; /* The next part of the backup area to fill. It may already
3309 * be full, so we need to check */
3310 int level
= reshape
->level
;
3311 int layout
= reshape
->before
.layout
;
3312 int data
= reshape
->before
.data_disks
;
3313 int disks
= reshape
->before
.data_disks
+ reshape
->parity
;
3314 int chunk
= sra
->array
.chunk_size
;
3316 unsigned long stripes
;
3319 /* set up the backup-super-block. This requires the
3320 * uuid from the array.
3322 /* Find a superblock */
3323 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
3327 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
3329 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
3330 devfd
= dev_open(dn
, O_RDONLY
);
3333 ok
= st
->ss
->load_super(st
, devfd
, NULL
);
3339 fprintf(stderr
, Name
": Cannot find a superblock\n");
3343 memset(&bsb
, 0, 512);
3344 memcpy(bsb
.magic
, "md_backup_data-1", 16);
3345 st
->ss
->uuid_from_super(st
, uuid
);
3346 memcpy(bsb
.set_uuid
, uuid
, 16);
3347 bsb
.mtime
= __cpu_to_le64(time(0));
3348 bsb
.devstart2
= blocks
;
3350 stripes
= blocks
/ (sra
->array
.chunk_size
/512) /
3351 reshape
->before
.data_disks
;
3353 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
3354 /* Don't start the 'reshape' */
3356 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
) {
3357 sysfs_get_ll(sra
, NULL
, "sync_speed_min", &speed
);
3358 sysfs_set_num(sra
, NULL
, "sync_speed_min", 200000);
3362 array_size
= sra
->component_size
* reshape
->after
.data_disks
;
3363 backup_point
= sra
->reshape_progress
;
3366 array_size
= sra
->component_size
* reshape
->before
.data_disks
;
3367 backup_point
= reshape
->backup_blocks
;
3368 suspend_point
= array_size
;
3374 /* Want to return as soon the oldest backup slot can
3375 * be released as that allows us to start backing up
3376 * some more, providing suspend_point has been
3377 * advanced, which it should have.
3380 wait_point
= array_size
;
3381 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
3382 wait_point
= (__le64_to_cpu(bsb
.arraystart
) +
3383 __le64_to_cpu(bsb
.length
));
3384 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
3385 wait_point
= (__le64_to_cpu(bsb
.arraystart2
) +
3386 __le64_to_cpu(bsb
.length2
));
3389 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
3390 wait_point
= __le64_to_cpu(bsb
.arraystart
);
3391 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
3392 wait_point
= __le64_to_cpu(bsb
.arraystart2
);
3395 rv
= progress_reshape(sra
, reshape
,
3396 backup_point
, wait_point
,
3397 &suspend_point
, &reshape_completed
);
3398 /* external metadata would need to ping_monitor here */
3399 sra
->reshape_progress
= reshape_completed
;
3401 /* Clear any backup region that is before 'here' */
3403 if (__le64_to_cpu(bsb
.length
) > 0 &&
3404 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart
) +
3405 __le64_to_cpu(bsb
.length
)))
3406 forget_backup(dests
, destfd
,
3408 if (__le64_to_cpu(bsb
.length2
) > 0 &&
3409 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart2
) +
3410 __le64_to_cpu(bsb
.length2
)))
3411 forget_backup(dests
, destfd
,
3414 if (__le64_to_cpu(bsb
.length
) > 0 &&
3415 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart
)))
3416 forget_backup(dests
, destfd
,
3418 if (__le64_to_cpu(bsb
.length2
) > 0 &&
3419 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart2
)))
3420 forget_backup(dests
, destfd
,
3429 if (rv
== 0 && increasing
&& !st
->ss
->external
) {
3430 /* No longer need to monitor this reshape */
3436 unsigned long long offset
;
3437 unsigned long actual_stripes
;
3438 /* Need to backup some data.
3439 * If 'part' is not used and the desired
3440 * backup size is suspended, do a backup,
3441 * then consider the next part.
3443 /* Check that 'part' is unused */
3444 if (part
== 0 && __le64_to_cpu(bsb
.length
) != 0)
3446 if (part
== 1 && __le64_to_cpu(bsb
.length2
) != 0)
3449 offset
= backup_point
/ data
;
3450 actual_stripes
= stripes
;
3452 if (offset
+ actual_stripes
* (chunk
/512) >
3453 sra
->component_size
)
3454 actual_stripes
= ((sra
->component_size
- offset
)
3456 if (offset
+ actual_stripes
* (chunk
/512) >
3460 if (offset
< actual_stripes
* (chunk
/512))
3461 actual_stripes
= offset
/ (chunk
/512);
3462 offset
-= actual_stripes
* (chunk
/512);
3463 if (offset
< suspend_point
/data
)
3466 if (actual_stripes
== 0)
3468 grow_backup(sra
, offset
, actual_stripes
,
3470 disks
, chunk
, level
, layout
,
3471 dests
, destfd
, destoffsets
,
3472 part
, °raded
, buf
);
3473 validate(afd
, destfd
[0], destoffsets
[0]);
3474 /* record where 'part' is up to */
3477 backup_point
+= actual_stripes
* (chunk
/512) * data
;
3479 backup_point
-= actual_stripes
* (chunk
/512) * data
;
3483 /* FIXME maybe call progress_reshape one more time instead */
3484 abort_reshape(sra
); /* remove any remaining suspension */
3485 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
3486 sysfs_set_num(sra
, NULL
, "sync_speed_min", speed
);
3492 * If any spare contains md_back_data-1 which is recent wrt mtime,
3493 * write that data into the array and update the super blocks with
3494 * the new reshape_progress
3496 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
, int cnt
,
3497 char *backup_file
, int verbose
)
3501 unsigned long long *offsets
;
3502 unsigned long long nstripe
, ostripe
;
3505 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
3506 if (info
->array
.level
== 6) odata
--; /* number of data disks */
3507 ndata
= info
->array
.raid_disks
- 1;
3508 if (info
->new_level
== 6) ndata
--;
3510 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
3512 if (info
->delta_disks
<= 0)
3513 /* Didn't grow, so the backup file must have
3517 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
3518 struct mdinfo dinfo
;
3521 char *devname
, namebuf
[20];
3522 unsigned long long lo
, hi
;
3524 /* This was a spare and may have some saved data on it.
3525 * Load the superblock, find and load the
3526 * backup_super_block.
3527 * If either fail, go on to next device.
3528 * If the backup contains no new info, just return
3529 * else restore data and update all superblocks
3531 if (i
== old_disks
-1) {
3532 fd
= open(backup_file
, O_RDONLY
);
3534 fprintf(stderr
, Name
": backup file %s inaccessible: %s\n",
3535 backup_file
, strerror(errno
));
3538 devname
= backup_file
;
3543 if (st
->ss
->load_super(st
, fd
, NULL
))
3546 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
3547 st
->ss
->free_super(st
);
3550 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
3552 fprintf(stderr
, Name
": Cannot seek on device %d\n", i
);
3553 continue; /* Cannot seek */
3555 sprintf(namebuf
, "device-%d", i
);
3558 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
)) {
3560 fprintf(stderr
, Name
": Cannot read from %s\n", devname
);
3561 continue; /* Cannot read */
3563 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0 &&
3564 memcmp(bsb
.magic
, "md_backup_data-2", 16) != 0) {
3566 fprintf(stderr
, Name
": No backup metadata on %s\n", devname
);
3569 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
))) {
3571 fprintf(stderr
, Name
": Bad backup-metadata checksum on %s\n", devname
);
3572 continue; /* bad checksum */
3574 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0 &&
3575 bsb
.sb_csum2
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum2
)-((char*)&bsb
))) {
3577 fprintf(stderr
, Name
": Bad backup-metadata checksum2 on %s\n", devname
);
3578 continue; /* Bad second checksum */
3580 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0) {
3582 fprintf(stderr
, Name
": Wrong uuid on backup-metadata on %s\n", devname
);
3583 continue; /* Wrong uuid */
3586 /* array utime and backup-mtime should be updated at much the same time, but it seems that
3587 * sometimes they aren't... So allow considerable flexability in matching, and allow
3588 * this test to be overridden by an environment variable.
3590 if (info
->array
.utime
> (int)__le64_to_cpu(bsb
.mtime
) + 2*60*60 ||
3591 info
->array
.utime
< (int)__le64_to_cpu(bsb
.mtime
) - 10*60) {
3592 if (check_env("MDADM_GROW_ALLOW_OLD")) {
3593 fprintf(stderr
, Name
": accepting backup with timestamp %lu "
3594 "for array with timestamp %lu\n",
3595 (unsigned long)__le64_to_cpu(bsb
.mtime
),
3596 (unsigned long)info
->array
.utime
);
3599 fprintf(stderr
, Name
": too-old timestamp on "
3600 "backup-metadata on %s\n", devname
);
3601 continue; /* time stamp is too bad */
3605 if (bsb
.magic
[15] == '1') {
3606 if (bsb
.length
== 0)
3608 if (info
->delta_disks
>= 0) {
3609 /* reshape_progress is increasing */
3610 if (__le64_to_cpu(bsb
.arraystart
)
3611 + __le64_to_cpu(bsb
.length
)
3612 < info
->reshape_progress
) {
3615 fprintf(stderr
, Name
3616 ": backup-metadata found on %s but is not needed\n", devname
);
3617 continue; /* No new data here */
3620 /* reshape_progress is decreasing */
3621 if (__le64_to_cpu(bsb
.arraystart
) >=
3622 info
->reshape_progress
)
3623 goto nonew
; /* No new data here */
3626 if (bsb
.length
== 0 && bsb
.length2
== 0)
3628 if (info
->delta_disks
>= 0) {
3629 /* reshape_progress is increasing */
3630 if ((__le64_to_cpu(bsb
.arraystart
)
3631 + __le64_to_cpu(bsb
.length
)
3632 < info
->reshape_progress
)
3634 (__le64_to_cpu(bsb
.arraystart2
)
3635 + __le64_to_cpu(bsb
.length2
)
3636 < info
->reshape_progress
))
3637 goto nonew
; /* No new data here */
3639 /* reshape_progress is decreasing */
3640 if (__le64_to_cpu(bsb
.arraystart
) >=
3641 info
->reshape_progress
&&
3642 __le64_to_cpu(bsb
.arraystart2
) >=
3643 info
->reshape_progress
)
3644 goto nonew
; /* No new data here */
3647 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0) {
3650 fprintf(stderr
, Name
3651 ": Failed to verify secondary backup-metadata block on %s\n",
3653 continue; /* Cannot seek */
3655 /* There should be a duplicate backup superblock 4k before here */
3656 if (lseek64(fd
, -4096, 1) < 0 ||
3657 read(fd
, &bsb2
, sizeof(bsb2
)) != sizeof(bsb2
))
3658 goto second_fail
; /* Cannot find leading superblock */
3659 if (bsb
.magic
[15] == '1')
3660 bsbsize
= offsetof(struct mdp_backup_super
, pad1
);
3662 bsbsize
= offsetof(struct mdp_backup_super
, pad
);
3663 if (memcmp(&bsb2
, &bsb
, bsbsize
) != 0)
3664 goto second_fail
; /* Cannot find leading superblock */
3666 /* Now need the data offsets for all devices. */
3667 offsets
= malloc(sizeof(*offsets
)*info
->array
.raid_disks
);
3668 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
3671 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
3672 /* FIXME should be this be an error */
3674 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
3675 st
->ss
->free_super(st
);
3676 offsets
[j
] = dinfo
.data_offset
* 512;
3678 printf(Name
": restoring critical section\n");
3680 if (restore_stripes(fdlist
, offsets
,
3681 info
->array
.raid_disks
,
3685 fd
, __le64_to_cpu(bsb
.devstart
)*512,
3686 __le64_to_cpu(bsb
.arraystart
)*512,
3687 __le64_to_cpu(bsb
.length
)*512, NULL
)) {
3688 /* didn't succeed, so giveup */
3690 fprintf(stderr
, Name
": Error restoring backup from %s\n",
3696 if (bsb
.magic
[15] == '2' &&
3697 restore_stripes(fdlist
, offsets
,
3698 info
->array
.raid_disks
,
3702 fd
, __le64_to_cpu(bsb
.devstart
)*512 +
3703 __le64_to_cpu(bsb
.devstart2
)*512,
3704 __le64_to_cpu(bsb
.arraystart2
)*512,
3705 __le64_to_cpu(bsb
.length2
)*512, NULL
)) {
3706 /* didn't succeed, so giveup */
3708 fprintf(stderr
, Name
": Error restoring second backup from %s\n",
3716 /* Ok, so the data is restored. Let's update those superblocks. */
3720 lo
= __le64_to_cpu(bsb
.arraystart
);
3721 hi
= lo
+ __le64_to_cpu(bsb
.length
);
3723 if (bsb
.magic
[15] == '2' && bsb
.length2
) {
3724 unsigned long long lo1
, hi1
;
3725 lo1
= __le64_to_cpu(bsb
.arraystart2
);
3726 hi1
= lo1
+ __le64_to_cpu(bsb
.length2
);
3730 } else if (lo
< lo1
)
3736 (info
->reshape_progress
< lo
||
3737 info
->reshape_progress
> hi
))
3738 /* backup does not affect reshape_progress*/ ;
3739 else if (info
->delta_disks
>= 0) {
3740 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
) +
3741 __le64_to_cpu(bsb
.length
);
3742 if (bsb
.magic
[15] == '2') {
3743 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
) +
3744 __le64_to_cpu(bsb
.length2
);
3745 if (p2
> info
->reshape_progress
)
3746 info
->reshape_progress
= p2
;
3749 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
);
3750 if (bsb
.magic
[15] == '2') {
3751 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
);
3752 if (p2
< info
->reshape_progress
)
3753 info
->reshape_progress
= p2
;
3756 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
3757 if (fdlist
[j
] < 0) continue;
3758 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
3760 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
3761 dinfo
.reshape_progress
= info
->reshape_progress
;
3762 st
->ss
->update_super(st
, &dinfo
,
3763 "_reshape_progress",
3765 st
->ss
->store_super(st
, fdlist
[j
]);
3766 st
->ss
->free_super(st
);
3770 /* Didn't find any backup data, try to see if any
3773 if (info
->delta_disks
< 0) {
3774 /* When shrinking, the critical section is at the end.
3775 * So see if we are before the critical section.
3777 unsigned long long first_block
;
3778 nstripe
= ostripe
= 0;
3780 while (ostripe
>= nstripe
) {
3781 ostripe
+= info
->array
.chunk_size
/ 512;
3782 first_block
= ostripe
* odata
;
3783 nstripe
= first_block
/ ndata
/ (info
->new_chunk
/512) *
3784 (info
->new_chunk
/512);
3787 if (info
->reshape_progress
>= first_block
)
3790 if (info
->delta_disks
> 0) {
3791 /* See if we are beyond the critical section. */
3792 unsigned long long last_block
;
3793 nstripe
= ostripe
= 0;
3795 while (nstripe
>= ostripe
) {
3796 nstripe
+= info
->new_chunk
/ 512;
3797 last_block
= nstripe
* ndata
;
3798 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
3799 (info
->array
.chunk_size
/512);
3802 if (info
->reshape_progress
>= last_block
)
3805 /* needed to recover critical section! */
3807 fprintf(stderr
, Name
": Failed to find backup of critical section\n");
3811 int Grow_continue_command(char *devname
, int fd
,
3812 char *backup_file
, int verbose
)
3815 struct supertype
*st
= NULL
;
3816 struct mdinfo
*content
= NULL
;
3817 struct mdinfo array
;
3818 char *subarray
= NULL
;
3819 struct mdinfo
*cc
= NULL
;
3820 struct mdstat_ent
*mdstat
= NULL
;
3825 dprintf("Grow continue from command line called for %s\n",
3828 st
= super_by_fd(fd
, &subarray
);
3829 if (!st
|| !st
->ss
) {
3831 Name
": Unable to determine metadata format for %s\n",
3835 dprintf("Grow continue is run for ");
3836 if (st
->ss
->external
== 0) {
3837 dprintf("native array (%s)\n", devname
);
3838 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) < 0) {
3839 fprintf(stderr
, Name
": %s is not an active md array -"
3840 " aborting\n", devname
);
3842 goto Grow_continue_command_exit
;
3845 sysfs_init(content
, fd
, st
->devnum
);
3850 dprintf("subarray (%s)\n", subarray
);
3851 container_dev
= st
->container_dev
;
3852 cfd
= open_dev_excl(st
->container_dev
);
3854 container_dev
= st
->devnum
;
3856 cfd
= open_dev_excl(st
->devnum
);
3857 dprintf("container (%i)\n", container_dev
);
3861 fprintf(stderr
, Name
": Unable to open container "
3862 "for %s\n", devname
);
3864 goto Grow_continue_command_exit
;
3866 fmt_devname(buf
, container_dev
);
3868 /* find in container array under reshape
3870 ret_val
= st
->ss
->load_container(st
, cfd
, NULL
);
3873 Name
": Cannot read superblock for %s\n",
3876 goto Grow_continue_command_exit
;
3879 cc
= st
->ss
->container_content(st
, subarray
);
3880 for (content
= cc
; content
; content
= content
->next
) {
3882 int allow_reshape
= 1;
3884 if (content
->reshape_active
== 0)
3886 /* The decision about array or container wide
3887 * reshape is taken in Grow_continue based
3888 * content->reshape_active state, therefore we
3889 * need to check_reshape based on
3890 * reshape_active and subarray name
3892 if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
))
3894 if (content
->reshape_active
== CONTAINER_RESHAPE
&&
3895 (content
->array
.state
3896 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
)))
3899 if (!allow_reshape
) {
3900 fprintf(stderr
, Name
3901 ": cannot continue reshape of an array"
3902 " in container with unsupported"
3903 " metadata: %s(%s)\n",
3906 goto Grow_continue_command_exit
;
3909 array
= strchr(content
->text_version
+1, '/')+1;
3910 mdstat
= mdstat_by_subdev(array
, container_dev
);
3917 Name
": Unable to determine reshaped "
3918 "array for %s\n", devname
);
3920 goto Grow_continue_command_exit
;
3922 fd2
= open_dev(mdstat
->devnum
);
3924 fprintf(stderr
, Name
": cannot open (md%i)\n",
3927 goto Grow_continue_command_exit
;
3930 sysfs_init(content
, fd2
, mdstat
->devnum
);
3932 /* start mdmon in case it is not running
3934 if (!mdmon_running(container_dev
))
3935 start_mdmon(container_dev
);
3938 if (mdmon_running(container_dev
))
3939 st
->update_tail
= &st
->updates
;
3941 fprintf(stderr
, Name
": No mdmon found. "
3942 "Grow cannot continue.\n");
3944 goto Grow_continue_command_exit
;
3948 /* verify that array under reshape is started from
3951 if (verify_reshape_position(content
,
3952 map_name(pers
, mdstat
->level
)) <= 0) {
3954 goto Grow_continue_command_exit
;
3959 ret_val
= Grow_continue(fd
, st
, content
, backup_file
, 0);
3961 Grow_continue_command_exit
:
3966 st
->ss
->free_super(st
);
3967 free_mdstat(mdstat
);
3974 int Grow_continue(int mdfd
, struct supertype
*st
, struct mdinfo
*info
,
3975 char *backup_file
, int freeze_reshape
)
3979 if (!info
->reshape_active
)
3982 if (st
->ss
->external
) {
3984 int cfd
= open_dev(st
->container_dev
);
3989 fmt_devname(container
, st
->container_dev
);
3990 st
->ss
->load_container(st
, cfd
, container
);
3992 ret_val
= reshape_container(container
, NULL
, mdfd
,
3993 st
, info
, 0, backup_file
,
3994 0, 1, freeze_reshape
);
3996 ret_val
= reshape_array(NULL
, mdfd
, "array", st
, info
, 1,
3997 NULL
, backup_file
, 0, 0, 1,