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");
139 nfd
= open(newdev
, O_RDWR
|O_EXCL
|O_DIRECT
);
141 fprintf(stderr
, Name
": cannot open %s\n", newdev
);
146 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
147 fprintf(stderr
, Name
": %s is not a block device!\n", newdev
);
152 /* now check out all the devices and make sure we can read the superblock */
153 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
154 mdu_disk_info_t disk
;
157 st
->ss
->free_super(st
);
160 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
161 fprintf(stderr
, Name
": cannot get device detail for device %d\n",
167 dv
= map_dev(disk
.major
, disk
.minor
, 1);
169 fprintf(stderr
, Name
": cannot find device file for device %d\n",
175 fd2
= dev_open(dv
, O_RDWR
);
177 fprintf(stderr
, Name
": cannot open device file %s\n", dv
);
183 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
184 fprintf(stderr
, Name
": cannot find super block on %s\n", dv
);
192 /* Ok, looks good. Lets update the superblock and write it out to
196 info
.disk
.number
= d
;
197 info
.disk
.major
= major(stb
.st_rdev
);
198 info
.disk
.minor
= minor(stb
.st_rdev
);
199 info
.disk
.raid_disk
= d
;
200 info
.disk
.state
= (1 << MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
);
201 st
->ss
->update_super(st
, &info
, "linear-grow-new", newdev
,
204 if (st
->ss
->store_super(st
, nfd
)) {
205 fprintf(stderr
, Name
": Cannot store new superblock on %s\n",
212 if (ioctl(fd
, ADD_NEW_DISK
, &info
.disk
) != 0) {
213 fprintf(stderr
, Name
": Cannot add new disk to this array\n");
216 /* Well, that seems to have worked.
217 * Now go through and update all superblocks
220 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
221 fprintf(stderr
, Name
": cannot get array info for %s\n", devname
);
226 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
227 mdu_disk_info_t disk
;
231 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
232 fprintf(stderr
, Name
": cannot get device detail for device %d\n",
236 dv
= map_dev(disk
.major
, disk
.minor
, 1);
238 fprintf(stderr
, Name
": cannot find device file for device %d\n",
242 fd2
= dev_open(dv
, O_RDWR
);
244 fprintf(stderr
, Name
": cannot open device file %s\n", dv
);
247 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
248 fprintf(stderr
, Name
": cannot find super block on %s\n", dv
);
252 info
.array
.raid_disks
= nd
+1;
253 info
.array
.nr_disks
= nd
+1;
254 info
.array
.active_disks
= nd
+1;
255 info
.array
.working_disks
= nd
+1;
257 st
->ss
->update_super(st
, &info
, "linear-grow-update", dv
,
260 if (st
->ss
->store_super(st
, fd2
)) {
261 fprintf(stderr
, Name
": Cannot store new superblock on %s\n", dv
);
271 int Grow_addbitmap(char *devname
, int fd
, char *file
, int chunk
, int delay
, int write_behind
, int force
)
274 * First check that array doesn't have a bitmap
275 * Then create the bitmap
278 * For internal bitmaps, we need to check the version,
279 * find all the active devices, and write the bitmap block
282 mdu_bitmap_file_t bmf
;
283 mdu_array_info_t array
;
284 struct supertype
*st
;
285 char *subarray
= NULL
;
286 int major
= BITMAP_MAJOR_HI
;
287 int vers
= md_get_version(fd
);
288 unsigned long long bitmapsize
, array_size
;
291 major
= BITMAP_MAJOR_HOSTENDIAN
;
292 fprintf(stderr
, Name
": Warning - bitmaps created on this kernel"
293 " are not portable\n"
294 " between different architectures. Consider upgrading"
295 " the Linux kernel.\n");
298 if (ioctl(fd
, GET_BITMAP_FILE
, &bmf
) != 0) {
300 fprintf(stderr
, Name
": Memory allocation failure.\n");
302 fprintf(stderr
, Name
": bitmaps not supported by this kernel.\n");
305 if (bmf
.pathname
[0]) {
306 if (strcmp(file
,"none")==0) {
307 if (ioctl(fd
, SET_BITMAP_FILE
, -1)!= 0) {
308 fprintf(stderr
, Name
": failed to remove bitmap %s\n",
314 fprintf(stderr
, Name
": %s already has a bitmap (%s)\n",
315 devname
, bmf
.pathname
);
318 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
319 fprintf(stderr
, Name
": cannot get array status for %s\n", devname
);
322 if (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)) {
323 if (strcmp(file
, "none")==0) {
324 array
.state
&= ~(1<<MD_SB_BITMAP_PRESENT
);
325 if (ioctl(fd
, SET_ARRAY_INFO
, &array
)!= 0) {
326 fprintf(stderr
, Name
": failed to remove internal bitmap.\n");
331 fprintf(stderr
, Name
": Internal bitmap already present on %s\n",
336 if (strcmp(file
, "none") == 0) {
337 fprintf(stderr
, Name
": no bitmap found on %s\n", devname
);
340 if (array
.level
<= 0) {
341 fprintf(stderr
, Name
": Bitmaps not meaningful with level %s\n",
342 map_num(pers
, array
.level
)?:"of this array");
345 bitmapsize
= array
.size
;
347 if (get_dev_size(fd
, NULL
, &array_size
) &&
348 array_size
> (0x7fffffffULL
<<9)) {
349 /* Array is big enough that we cannot trust array.size
350 * try other approaches
352 bitmapsize
= get_component_size(fd
);
354 if (bitmapsize
== 0) {
355 fprintf(stderr
, Name
": Cannot reliably determine size of array to create bitmap - sorry.\n");
359 if (array
.level
== 10) {
360 int ncopies
= (array
.layout
&255)*((array
.layout
>>8)&255);
361 bitmapsize
= bitmapsize
* array
.raid_disks
/ ncopies
;
364 st
= super_by_fd(fd
, &subarray
);
366 fprintf(stderr
, Name
": Cannot understand version %d.%d\n",
367 array
.major_version
, array
.minor_version
);
371 fprintf(stderr
, Name
": Cannot add bitmaps to sub-arrays yet\n");
376 if (strcmp(file
, "internal") == 0) {
378 if (st
->ss
->add_internal_bitmap
== NULL
) {
379 fprintf(stderr
, Name
": Internal bitmaps not supported "
380 "with %s metadata\n", st
->ss
->name
);
383 for (d
=0; d
< st
->max_devs
; d
++) {
384 mdu_disk_info_t disk
;
387 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
389 if (disk
.major
== 0 &&
392 if ((disk
.state
& (1<<MD_DISK_SYNC
))==0)
394 dv
= map_dev(disk
.major
, disk
.minor
, 1);
396 int fd2
= dev_open(dv
, O_RDWR
);
399 if (st
->ss
->load_super(st
, fd2
, NULL
)==0) {
400 if (st
->ss
->add_internal_bitmap(
402 &chunk
, delay
, write_behind
,
403 bitmapsize
, 0, major
)
405 st
->ss
->write_bitmap(st
, fd2
);
407 fprintf(stderr
, Name
": failed to create internal bitmap - chunksize problem.\n");
415 array
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
416 if (ioctl(fd
, SET_ARRAY_INFO
, &array
)!= 0) {
419 ": Cannot add bitmap while array is"
420 " resyncing or reshaping etc.\n");
421 fprintf(stderr
, Name
": failed to set internal bitmap.\n");
428 int max_devs
= st
->max_devs
;
430 /* try to load a superblock */
431 for (d
=0; d
<max_devs
; d
++) {
432 mdu_disk_info_t disk
;
436 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
438 if ((disk
.major
==0 && disk
.minor
==0) ||
439 (disk
.state
& (1<<MD_DISK_REMOVED
)))
441 dv
= map_dev(disk
.major
, disk
.minor
, 1);
443 fd2
= dev_open(dv
, O_RDONLY
);
445 st
->ss
->load_super(st
, fd2
, NULL
) == 0) {
447 st
->ss
->uuid_from_super(st
, uuid
);
453 fprintf(stderr
, Name
": cannot find UUID for array!\n");
456 if (CreateBitmap(file
, force
, (char*)uuid
, chunk
,
457 delay
, write_behind
, bitmapsize
, major
)) {
460 bitmap_fd
= open(file
, O_RDWR
);
462 fprintf(stderr
, Name
": weird: %s cannot be opened\n",
466 if (ioctl(fd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
470 ": Cannot add bitmap while array is"
471 " resyncing or reshaping etc.\n");
472 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
473 devname
, strerror(err
));
483 * When reshaping an array we might need to backup some data.
484 * This is written to all spares with a 'super_block' describing it.
485 * The superblock goes 4K from the end of the used space on the
487 * It if written after the backup is complete.
488 * It has the following structure.
491 static struct mdp_backup_super
{
492 char magic
[16]; /* md_backup_data-1 or -2 */
495 /* start/sizes in 512byte sectors */
496 __u64 devstart
; /* address on backup device/file of data */
499 __u32 sb_csum
; /* csum of preceeding bytes. */
501 __u64 devstart2
; /* offset in to data of second section */
504 __u32 sb_csum2
; /* csum of preceeding bytes. */
506 } __attribute__((aligned(512))) bsb
, bsb2
;
508 static __u32
bsb_csum(char *buf
, int len
)
512 for (i
=0; i
<len
; i
++)
513 csum
= (csum
<<3) + buf
[0];
514 return __cpu_to_le32(csum
);
517 static int check_idle(struct supertype
*st
)
519 /* Check that all member arrays for this container, or the
520 * container of this array, are idle
522 int container_dev
= (st
->container_dev
!= NoMdDev
523 ? st
->container_dev
: st
->devnum
);
525 struct mdstat_ent
*ent
, *e
;
528 fmt_devname(container
, container_dev
);
529 ent
= mdstat_read(0, 0);
530 for (e
= ent
; e
; e
= e
->next
) {
531 if (!is_container_member(e
, container
))
533 if (e
->percent
>= 0) {
542 static int freeze_container(struct supertype
*st
)
544 int container_dev
= (st
->container_dev
!= NoMdDev
545 ? st
->container_dev
: st
->devnum
);
551 fmt_devname(container
, container_dev
);
553 if (block_monitor(container
, 1)) {
554 fprintf(stderr
, Name
": failed to freeze container\n");
561 static void unfreeze_container(struct supertype
*st
)
563 int container_dev
= (st
->container_dev
!= NoMdDev
564 ? st
->container_dev
: st
->devnum
);
567 fmt_devname(container
, container_dev
);
569 unblock_monitor(container
, 1);
572 static int freeze(struct supertype
*st
)
574 /* Try to freeze resync/rebuild on this array/container.
575 * Return -1 if the array is busy,
576 * return -2 container cannot be frozen,
577 * return 0 if this kernel doesn't support 'frozen'
578 * return 1 if it worked.
580 if (st
->ss
->external
)
581 return freeze_container(st
);
583 struct mdinfo
*sra
= sysfs_read(-1, st
->devnum
, GET_VERSION
);
589 /* Need to clear any 'read-auto' status */
590 if (sysfs_get_str(sra
, NULL
, "array_state", buf
, 20) > 0 &&
591 strncmp(buf
, "read-auto", 9) == 0)
592 sysfs_set_str(sra
, NULL
, "array_state", "clean");
594 err
= sysfs_freeze_array(sra
);
600 static void unfreeze(struct supertype
*st
)
602 if (st
->ss
->external
)
603 return unfreeze_container(st
);
605 struct mdinfo
*sra
= sysfs_read(-1, st
->devnum
, GET_VERSION
);
608 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
610 fprintf(stderr
, Name
": failed to unfreeze array\n");
615 static void wait_reshape(struct mdinfo
*sra
)
617 int fd
= sysfs_get_fd(sra
, NULL
, "sync_action");
623 while (sysfs_fd_get_str(fd
, action
, 20) > 0 &&
624 strncmp(action
, "reshape", 7) == 0) {
628 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
633 static int reshape_super(struct supertype
*st
, long long size
, int level
,
634 int layout
, int chunksize
, int raid_disks
,
635 int delta_disks
, char *backup_file
, char *dev
,
638 /* nothing extra to check in the native case */
639 if (!st
->ss
->external
)
641 if (!st
->ss
->reshape_super
||
642 !st
->ss
->manage_reshape
) {
643 fprintf(stderr
, Name
": %s metadata does not support reshape\n",
648 return st
->ss
->reshape_super(st
, size
, level
, layout
, chunksize
,
649 raid_disks
, delta_disks
, backup_file
, dev
,
653 static void sync_metadata(struct supertype
*st
)
655 if (st
->ss
->external
) {
656 if (st
->update_tail
) {
657 flush_metadata_updates(st
);
658 st
->update_tail
= &st
->updates
;
660 st
->ss
->sync_metadata(st
);
664 static int subarray_set_num(char *container
, struct mdinfo
*sra
, char *name
, int n
)
666 /* when dealing with external metadata subarrays we need to be
667 * prepared to handle EAGAIN. The kernel may need to wait for
668 * mdmon to mark the array active so the kernel can handle
669 * allocations/writeback when preparing the reshape action
670 * (md_allow_write()). We temporarily disable safe_mode_delay
671 * to close a race with the array_state going clean before the
672 * next write to raid_disks / stripe_cache_size
677 /* only 'raid_disks' and 'stripe_cache_size' trigger md_allow_write */
679 (strcmp(name
, "raid_disks") != 0 &&
680 strcmp(name
, "stripe_cache_size") != 0))
681 return sysfs_set_num(sra
, NULL
, name
, n
);
683 rc
= sysfs_get_str(sra
, NULL
, "safe_mode_delay", safe
, sizeof(safe
));
686 sysfs_set_num(sra
, NULL
, "safe_mode_delay", 0);
687 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
688 if (rc
< 0 && errno
== EAGAIN
) {
689 ping_monitor(container
);
690 /* if we get EAGAIN here then the monitor is not active
693 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
695 sysfs_set_str(sra
, NULL
, "safe_mode_delay", safe
);
699 int start_reshape(struct mdinfo
*sra
, int already_running
, int data_disks
)
702 unsigned long long sync_max_to_set
;
704 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
705 err
= sysfs_set_num(sra
, NULL
, "suspend_hi", sra
->reshape_progress
);
706 err
= err
?: sysfs_set_num(sra
, NULL
, "suspend_lo",
707 sra
->reshape_progress
);
708 sync_max_to_set
= sra
->reshape_progress
/ data_disks
;
709 if (!already_running
)
710 sysfs_set_num(sra
, NULL
, "sync_min", sync_max_to_set
);
711 err
= err
?: sysfs_set_num(sra
, NULL
, "sync_max", sync_max_to_set
);
712 if (!already_running
)
713 err
= err
?: sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
718 void abort_reshape(struct mdinfo
*sra
)
720 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
721 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
722 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
723 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
724 sysfs_set_num(sra
, NULL
, "sync_min", 0);
725 sysfs_set_str(sra
, NULL
, "sync_max", "max");
728 int remove_disks_for_takeover(struct supertype
*st
,
733 struct mdinfo
*remaining
;
736 if (sra
->array
.level
== 10)
737 nr_of_copies
= layout
& 0xff;
738 else if (sra
->array
.level
== 1)
739 nr_of_copies
= sra
->array
.raid_disks
;
743 remaining
= sra
->devs
;
745 /* for each 'copy', select one device and remove from the list. */
746 for (slot
= 0; slot
< sra
->array
.raid_disks
; slot
+= nr_of_copies
) {
747 struct mdinfo
**diskp
;
750 /* Find a working device to keep */
751 for (diskp
= &remaining
; *diskp
; diskp
= &(*diskp
)->next
) {
752 struct mdinfo
*disk
= *diskp
;
754 if (disk
->disk
.raid_disk
< slot
)
756 if (disk
->disk
.raid_disk
>= slot
+ nr_of_copies
)
758 if (disk
->disk
.state
& (1<<MD_DISK_REMOVED
))
760 if (disk
->disk
.state
& (1<<MD_DISK_FAULTY
))
762 if (!(disk
->disk
.state
& (1<<MD_DISK_SYNC
)))
765 /* We have found a good disk to use! */
767 disk
->next
= sra
->devs
;
776 if (slot
< sra
->array
.raid_disks
) {
777 /* didn't find all slots */
783 sra
->devs
= remaining
;
787 /* Remove all 'remaining' devices from the array */
789 struct mdinfo
*sd
= remaining
;
790 remaining
= sd
->next
;
792 sysfs_set_str(sra
, sd
, "state", "faulty");
793 sysfs_set_str(sra
, sd
, "slot", "none");
794 /* for external metadata disks should be removed in mdmon */
795 if (!st
->ss
->external
)
796 sysfs_set_str(sra
, sd
, "state", "remove");
797 sd
->disk
.state
|= (1<<MD_DISK_REMOVED
);
798 sd
->disk
.state
&= ~(1<<MD_DISK_SYNC
);
799 sd
->next
= sra
->devs
;
805 void reshape_free_fdlist(int *fdlist
,
806 unsigned long long *offsets
,
811 for (i
= 0; i
< size
; i
++)
819 int reshape_prepare_fdlist(char *devname
,
823 unsigned long blocks
,
826 unsigned long long *offsets
)
831 for (d
= 0; d
<= nrdisks
; d
++)
834 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
835 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
837 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
838 char *dn
= map_dev(sd
->disk
.major
,
840 fdlist
[sd
->disk
.raid_disk
]
841 = dev_open(dn
, O_RDONLY
);
842 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
843 if (fdlist
[sd
->disk
.raid_disk
] < 0) {
845 Name
": %s: cannot open component %s\n",
846 devname
, dn
? dn
: "-unknown-");
850 } else if (backup_file
== NULL
) {
852 char *dn
= map_dev(sd
->disk
.major
,
854 fdlist
[d
] = dev_open(dn
, O_RDWR
);
855 offsets
[d
] = (sd
->data_offset
+ sra
->component_size
- blocks
- 8)*512;
857 fprintf(stderr
, Name
": %s: cannot open component %s\n",
858 devname
, dn
? dn
: "-unknown-");
869 int reshape_open_backup_file(char *backup_file
,
874 unsigned long long *offsets
,
877 /* Return 1 on success, 0 on any form of failure */
878 /* need to check backup file is large enough */
884 *fdlist
= open(backup_file
, O_RDWR
|O_CREAT
|(restart
? O_TRUNC
: O_EXCL
),
888 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
889 devname
, backup_file
, strerror(errno
));
892 /* Guard against backup file being on array device.
893 * If array is partitioned or if LVM etc is in the
894 * way this will not notice, but it is better than
897 fstat(*fdlist
, &stb
);
900 if (stb
.st_rdev
== dev
) {
901 fprintf(stderr
, Name
": backup file must NOT be"
902 " on the array being reshaped.\n");
908 for (i
=0; i
< blocks
+ 8 ; i
++) {
909 if (write(*fdlist
, buf
, 512) != 512) {
910 fprintf(stderr
, Name
": %s: cannot create"
911 " backup file %s: %s\n",
912 devname
, backup_file
, strerror(errno
));
916 if (fsync(*fdlist
) != 0) {
917 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
918 devname
, backup_file
, strerror(errno
));
925 unsigned long compute_backup_blocks(int nchunk
, int ochunk
,
926 unsigned int ndata
, unsigned int odata
)
928 unsigned long a
, b
, blocks
;
929 /* So how much do we need to backup.
930 * We need an amount of data which is both a whole number of
931 * old stripes and a whole number of new stripes.
932 * So LCM for (chunksize*datadisks).
934 a
= (ochunk
/512) * odata
;
935 b
= (nchunk
/512) * ndata
;
943 /* LCM == product / GCD */
944 blocks
= (ochunk
/512) * (nchunk
/512) * odata
* ndata
/ a
;
949 char *analyse_change(struct mdinfo
*info
, struct reshape
*re
)
951 /* Based on the current array state in info->array and
952 * the changes in info->new_* etc, determine:
953 * - whether the change is possible
954 * - Intermediate level/raid_disks/layout
955 * - whether a restriping reshape is needed
956 * - number of sectors in minimum change unit. This
957 * will cover a whole number of stripes in 'before' and
960 * Return message if the change should be rejected
961 * NULL if the change can be achieved
963 * This can be called as part of starting a reshape, or
964 * when assembling an array that is undergoing reshape.
967 /* delta_parity records change in number of devices
968 * caused by level change
970 int delta_parity
= 0;
972 /* If a new level not explicitly given, we assume no-change */
973 if (info
->new_level
== UnSet
)
974 info
->new_level
= info
->array
.level
;
977 switch (info
->new_level
) {
983 /* chunk size is meaningful, must divide component_size
986 if (info
->component_size
% (info
->new_chunk
/512))
987 return "New chunk size does not"
988 " divide component size";
991 return "chunk size not meaningful for this level";
994 info
->new_chunk
= info
->array
.chunk_size
;
996 switch (info
->array
.level
) {
998 return "Cannot understand this RAID level";
1000 /* RAID1 can convert to RAID1 with different disks, or
1001 * raid5 with 2 disks, or
1004 if (info
->new_level
== 0) {
1005 if (info
->delta_disks
!= UnSet
&&
1006 info
->delta_disks
!= 0)
1007 return "Cannot change number of disks "
1008 "with RAID1->RAID0 conversion";
1010 re
->before
.data_disks
= 1;
1011 re
->after
.data_disks
= 1;
1012 re
->before
.layout
= 0;
1013 re
->backup_blocks
= 0;
1017 if (info
->new_level
== 1) {
1018 if (info
->delta_disks
== UnSet
)
1019 /* Don't know what to do */
1020 return "no change requested for Growing RAID1";
1022 re
->backup_blocks
= 0;
1026 if (info
->array
.raid_disks
== 2 &&
1027 info
->new_level
== 5) {
1030 re
->before
.data_disks
= 1;
1031 if (info
->delta_disks
!= UnSet
&&
1032 info
->delta_disks
!= 0)
1033 re
->after
.data_disks
= 1 + info
->delta_disks
;
1035 re
->after
.data_disks
= 1;
1036 if (re
->after
.data_disks
< 1)
1037 return "Number of disks too small for RAID5";
1039 re
->before
.layout
= ALGORITHM_LEFT_SYMMETRIC
;
1040 info
->array
.chunk_size
= 65536;
1043 /* Could do some multi-stage conversions, but leave that to
1046 return "Impossibly level change request for RAID1";
1049 /* RAID10 can only be converted from near mode to
1050 * RAID0 by removing some devices
1052 if ((info
->array
.layout
& ~0xff) != 0x100)
1053 return "Cannot Grow RAID10 with far/offset layout";
1054 /* number of devices must be multiple of number of copies */
1055 if (info
->array
.raid_disks
% (info
->array
.layout
& 0xff))
1056 return "RAID10 layout too complex for Grow operation";
1058 if (info
->new_level
!= 0)
1059 return "RAID10 can only be changed to RAID0";
1060 new_disks
= (info
->array
.raid_disks
1061 / (info
->array
.layout
& 0xff));
1062 if (info
->delta_disks
== UnSet
)
1063 info
->delta_disks
= (new_disks
1064 - info
->array
.raid_disks
);
1066 if (info
->delta_disks
!= new_disks
- info
->array
.raid_disks
)
1067 return "New number of raid-devices impossible for RAID10";
1068 if (info
->new_chunk
&&
1069 info
->new_chunk
!= info
->array
.chunk_size
)
1070 return "Cannot change chunk-size with RAID10 Grow";
1075 re
->before
.data_disks
= new_disks
;
1076 re
->after
.data_disks
= re
->before
.data_disks
;
1077 re
->before
.layout
= 0;
1078 re
->backup_blocks
= 0;
1082 /* RAID0 can be converted to RAID10, or to RAID456 */
1083 if (info
->new_level
== 10) {
1084 if (info
->new_layout
== UnSet
&& info
->delta_disks
== UnSet
) {
1085 /* Assume near=2 layout */
1086 info
->new_layout
= 0x102;
1087 info
->delta_disks
= info
->array
.raid_disks
;
1089 if (info
->new_layout
== UnSet
) {
1090 int copies
= 1 + (info
->delta_disks
1091 / info
->array
.raid_disks
);
1092 if (info
->array
.raid_disks
* (copies
-1)
1093 != info
->delta_disks
)
1094 return "Impossible number of devices"
1095 " for RAID0->RAID10";
1096 info
->new_layout
= 0x100 + copies
;
1098 if (info
->delta_disks
== UnSet
) {
1099 int copies
= info
->new_layout
& 0xff;
1100 if (info
->new_layout
!= 0x100 + copies
)
1101 return "New layout impossible"
1102 " for RAID0->RAID10";;
1103 info
->delta_disks
= (copies
- 1) *
1104 info
->array
.raid_disks
;
1106 if (info
->new_chunk
&&
1107 info
->new_chunk
!= info
->array
.chunk_size
)
1108 return "Cannot change chunk-size with RAID0->RAID10";
1112 re
->before
.data_disks
= (info
->array
.raid_disks
+
1114 re
->after
.data_disks
= re
->before
.data_disks
;
1115 re
->before
.layout
= info
->new_layout
;
1116 re
->backup_blocks
= 0;
1120 /* RAID0 can also covert to RAID0/4/5/6 by first converting to
1121 * a raid4 style layout of the final level.
1123 switch (info
->new_level
) {
1128 re
->before
.layout
= 0;
1133 re
->before
.layout
= ALGORITHM_PARITY_N
;
1138 re
->before
.layout
= ALGORITHM_PARITY_N
;
1141 return "Impossible level change requested";
1143 re
->before
.data_disks
= info
->array
.raid_disks
;
1144 /* determining 'after' layout happens outside this 'switch' */
1148 info
->array
.layout
= ALGORITHM_PARITY_N
;
1150 switch (info
->new_level
) {
1154 re
->level
= info
->array
.level
;
1155 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1156 re
->before
.layout
= info
->array
.layout
;
1160 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1161 re
->before
.layout
= info
->array
.layout
;
1166 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1167 switch (info
->array
.layout
) {
1168 case ALGORITHM_LEFT_ASYMMETRIC
:
1169 re
->before
.layout
= ALGORITHM_LEFT_ASYMMETRIC_6
;
1171 case ALGORITHM_RIGHT_ASYMMETRIC
:
1172 re
->before
.layout
= ALGORITHM_RIGHT_ASYMMETRIC_6
;
1174 case ALGORITHM_LEFT_SYMMETRIC
:
1175 re
->before
.layout
= ALGORITHM_LEFT_SYMMETRIC_6
;
1177 case ALGORITHM_RIGHT_SYMMETRIC
:
1178 re
->before
.layout
= ALGORITHM_RIGHT_SYMMETRIC_6
;
1180 case ALGORITHM_PARITY_0
:
1181 re
->before
.layout
= ALGORITHM_PARITY_0_6
;
1183 case ALGORITHM_PARITY_N
:
1184 re
->before
.layout
= ALGORITHM_PARITY_N_6
;
1187 return "Cannot convert an array with this layout";
1191 if (info
->array
.raid_disks
!= 2)
1192 return "Can only convert a 2-device array to RAID1";
1193 if (info
->delta_disks
!= UnSet
&&
1194 info
->delta_disks
!= 0)
1195 return "Cannot set raid_disk when "
1196 "converting RAID5->RAID1";
1200 return "Impossible level change requested";
1204 switch (info
->new_level
) {
1210 re
->before
.data_disks
= info
->array
.raid_disks
- 2;
1211 re
->before
.layout
= info
->array
.layout
;
1214 return "Impossible level change requested";
1219 /* If we reached here then it looks like a re-stripe is
1220 * happening. We have determined the intermediate level
1221 * and initial raid_disks/layout and stored these in 're'.
1223 * We need to deduce the final layout that can be atomically
1224 * converted to the end state.
1226 switch (info
->new_level
) {
1228 /* We can only get to RAID0 from RAID4 or RAID5
1229 * with appropriate layout and one extra device
1231 if (re
->level
!= 4 && re
->level
!= 5)
1232 return "Cannot covert to RAID0 from this level";
1234 switch (re
->level
) {
1236 re
->after
.layout
= 0 ; break;
1238 re
->after
.layout
= ALGORITHM_PARITY_N
; break;
1243 /* We can only get to RAID4 from RAID5 */
1244 if (re
->level
!= 4 && re
->level
!= 5)
1245 return "Cannot convert to RAID4 from this level";
1247 switch (re
->level
) {
1249 re
->after
.layout
= 0 ; break;
1251 re
->after
.layout
= ALGORITHM_PARITY_N
; break;
1256 /* We get to RAID5 for RAID5 or RAID6 */
1257 if (re
->level
!= 5 && re
->level
!= 6)
1258 return "Cannot convert to RAID5 from this level";
1260 switch (re
->level
) {
1262 if (info
->new_layout
== UnSet
)
1263 re
->after
.layout
= re
->before
.layout
;
1265 re
->after
.layout
= info
->new_layout
;
1268 if (info
->new_layout
== UnSet
)
1269 info
->new_layout
= re
->before
.layout
;
1271 /* after.layout needs to be raid6 version of new_layout */
1272 if (info
->new_layout
== ALGORITHM_PARITY_N
)
1273 re
->after
.layout
= ALGORITHM_PARITY_N
;
1276 char *ls
= map_num(r5layout
, info
->new_layout
);
1278 strcat(strcpy(layout
, ls
), "-6");
1279 l
= map_name(r6layout
, layout
);
1281 return "Cannot find RAID6 layout"
1283 re
->after
.layout
= l
;
1289 /* We must already be at level 6 */
1291 return "Impossible level change";
1292 if (info
->new_layout
== UnSet
)
1293 re
->after
.layout
= info
->array
.layout
;
1295 re
->after
.layout
= info
->new_layout
;
1298 return "Impossible level change requested";
1300 if (info
->delta_disks
== UnSet
)
1301 info
->delta_disks
= delta_parity
;
1303 re
->after
.data_disks
= (re
->before
.data_disks
1306 switch (re
->level
) {
1307 case 6: re
->parity
= 2; break;
1309 case 5: re
->parity
= 1; break;
1310 default: re
->parity
= 0; break;
1312 /* So we have a restripe operation, we need to calculate the number
1313 * of blocks per reshape operation.
1315 if (info
->new_chunk
== 0)
1316 info
->new_chunk
= info
->array
.chunk_size
;
1317 if (re
->after
.data_disks
== re
->before
.data_disks
&&
1318 re
->after
.layout
== re
->before
.layout
&&
1319 info
->new_chunk
== info
->array
.chunk_size
) {
1320 /* Nothing to change */
1321 re
->backup_blocks
= 0;
1324 if (re
->after
.data_disks
== 1 && re
->before
.data_disks
== 1) {
1325 /* chunk and layout changes make no difference */
1326 re
->backup_blocks
= 0;
1330 if (re
->after
.data_disks
== re
->before
.data_disks
&&
1331 get_linux_version() < 2006032)
1332 return "in-place reshape is not safe before 2.6.32 - sorry.";
1334 if (re
->after
.data_disks
< re
->before
.data_disks
&&
1335 get_linux_version() < 2006030)
1336 return "reshape to fewer devices is not supported before 2.6.30 - sorry.";
1338 re
->backup_blocks
= compute_backup_blocks(
1339 info
->new_chunk
, info
->array
.chunk_size
,
1340 re
->after
.data_disks
,
1341 re
->before
.data_disks
);
1343 re
->new_size
= info
->component_size
* re
->after
.data_disks
;
1347 static int reshape_array(char *container
, int fd
, char *devname
,
1348 struct supertype
*st
, struct mdinfo
*info
,
1349 int force
, struct mddev_dev
*devlist
,
1350 char *backup_file
, int quiet
, int forked
,
1351 int restart
, int freeze_reshape
);
1352 static int reshape_container(char *container
, char *devname
,
1354 struct supertype
*st
,
1355 struct mdinfo
*info
,
1358 int quiet
, int restart
, int freeze_reshape
);
1361 * helper routine to check metadata reshape avalability
1362 * 1. Do not "grow" arrays with volume activation blocked
1363 * 2. do not reshape containers with container reshape blocked
1366 * subarray - array name or NULL for container wide reshape
1367 * content - md device info from container_content
1371 static int check_reshape(char *subarray
, struct mdinfo
*content
)
1377 if (content
->array
.state
& (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
))
1380 /* do not "grow" arrays with volume activation blocked */
1381 idx
= strtoul(subarray
, &ep
, 10);
1383 && content
->container_member
== (int) idx
1384 && (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)))
1390 int Grow_reshape(char *devname
, int fd
, int quiet
, char *backup_file
,
1392 int level
, char *layout_str
, int chunksize
, int raid_disks
,
1393 struct mddev_dev
*devlist
,
1394 int assume_clean
, int force
)
1396 /* Make some changes in the shape of an array.
1397 * The kernel must support the change.
1399 * There are three different changes. Each can trigger
1400 * a resync or recovery so we freeze that until we have
1401 * requested everything (if kernel supports freezing - 2.6.30).
1403 * - change size (i.e. component_size)
1405 * - change layout/chunksize/ndisks
1407 * The last can require a reshape. It is different on different
1408 * levels so we need to check the level before actioning it.
1409 * Some times the level change needs to be requested after the
1410 * reshape (e.g. raid6->raid5, raid5->raid0)
1413 struct mdu_array_info_s array
;
1415 struct supertype
*st
;
1416 char *subarray
= NULL
;
1420 char *container
= NULL
;
1421 char container_buf
[20];
1424 struct mddev_dev
*dv
;
1430 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) < 0) {
1431 fprintf(stderr
, Name
": %s is not an active md array - aborting\n",
1437 (chunksize
|| level
!= UnSet
|| layout_str
|| raid_disks
)) {
1438 fprintf(stderr
, Name
": cannot change component size at the same time "
1439 "as other changes.\n"
1440 " Change size first, then check data is intact before "
1441 "making other changes.\n");
1445 if (raid_disks
&& raid_disks
< array
.raid_disks
&& array
.level
> 1 &&
1446 get_linux_version() < 2006032 &&
1447 !check_env("MDADM_FORCE_FEWER")) {
1448 fprintf(stderr
, Name
": reducing the number of devices is not safe before Linux 2.6.32\n"
1449 " Please use a newer kernel\n");
1453 st
= super_by_fd(fd
, &subarray
);
1455 fprintf(stderr
, Name
": Unable to determine metadata format for %s\n", devname
);
1458 if (raid_disks
> st
->max_devs
) {
1459 fprintf(stderr
, Name
": Cannot increase raid-disks on this array"
1460 " beyond %d\n", st
->max_devs
);
1464 /* in the external case we need to check that the requested reshape is
1465 * supported, and perform an initial check that the container holds the
1466 * pre-requisite spare devices (mdmon owns final validation)
1468 if (st
->ss
->external
) {
1473 container_dev
= st
->container_dev
;
1474 cfd
= open_dev_excl(st
->container_dev
);
1476 container_dev
= st
->devnum
;
1478 cfd
= open_dev_excl(st
->devnum
);
1482 fprintf(stderr
, Name
": Unable to open container for %s\n",
1488 fmt_devname(container_buf
, container_dev
);
1489 container
= container_buf
;
1491 rv
= st
->ss
->load_container(st
, cfd
, NULL
);
1494 fprintf(stderr
, Name
": Cannot read superblock for %s\n",
1500 /* check if operation is supported for metadata handler */
1501 if (st
->ss
->container_content
) {
1502 struct mdinfo
*cc
= NULL
;
1503 struct mdinfo
*content
= NULL
;
1505 cc
= st
->ss
->container_content(st
, subarray
);
1506 for (content
= cc
; content
; content
= content
->next
) {
1509 /* check if reshape is allowed based on metadata
1510 * indications stored in content.array.status
1512 allow_reshape
= check_reshape(subarray
, content
);
1513 if (!allow_reshape
) {
1514 fprintf(stderr
, Name
1515 " cannot reshape arrays in"
1516 " container with unsupported"
1517 " metadata: %s(%s)\n",
1518 devname
, container_buf
);
1526 if (mdmon_running(container_dev
))
1527 st
->update_tail
= &st
->updates
;
1531 for (dv
= devlist
; dv
; dv
= dv
->next
)
1533 if (raid_disks
> array
.raid_disks
&&
1534 array
.spare_disks
+added_disks
< (raid_disks
- array
.raid_disks
) &&
1537 Name
": Need %d spare%s to avoid degraded array,"
1538 " and only have %d.\n"
1539 " Use --force to over-ride this check.\n",
1540 raid_disks
- array
.raid_disks
,
1541 raid_disks
- array
.raid_disks
== 1 ? "" : "s",
1542 array
.spare_disks
+ added_disks
);
1546 sra
= sysfs_read(fd
, 0, GET_LEVEL
| GET_DISKS
| GET_DEVS
1547 | GET_STATE
| GET_VERSION
);
1549 if (st
->ss
->external
&& subarray
== NULL
) {
1550 array
.level
= LEVEL_CONTAINER
;
1551 sra
->array
.level
= LEVEL_CONTAINER
;
1554 fprintf(stderr
, Name
": failed to read sysfs parameters for %s\n",
1558 frozen
= freeze(st
);
1560 /* freeze() already spewed the reason */
1562 } else if (frozen
< 0) {
1563 fprintf(stderr
, Name
": %s is performing resync/recovery and cannot"
1564 " be reshaped\n", devname
);
1568 /* ========= set size =============== */
1569 if (size
>= 0 && (size
== 0 || size
!= array
.size
)) {
1570 long long orig_size
= get_component_size(fd
)/2;
1571 long long min_csize
;
1575 orig_size
= array
.size
;
1577 if (reshape_super(st
, size
, UnSet
, UnSet
, 0, 0, UnSet
, NULL
,
1584 /* Update the size of each member device in case
1585 * they have been resized. This will never reduce
1586 * below the current used-size. The "size" attribute
1587 * understands '0' to mean 'max'.
1590 for (mdi
= sra
->devs
; mdi
; mdi
= mdi
->next
) {
1591 if (sysfs_set_num(sra
, mdi
, "size", size
) < 0)
1593 if (array
.not_persistent
== 0 &&
1594 array
.major_version
== 0 &&
1595 get_linux_version() < 3001000) {
1596 /* Dangerous to allow size to exceed 2TB */
1597 unsigned long long csize
;
1598 if (sysfs_get_ll(sra
, mdi
, "size", &csize
) == 0) {
1599 if (csize
>= 2ULL*1024*1024*1024)
1600 csize
= 2ULL*1024*1024*1024;
1601 if ((min_csize
== 0 || (min_csize
1602 > (long long)csize
)))
1607 if (min_csize
&& size
> min_csize
) {
1608 fprintf(stderr
, Name
": Cannot safely make this array "
1609 "use more than 2TB per device on this kernel.\n");
1613 if (min_csize
&& size
== 0) {
1614 /* Don't let the kernel choose a size - it will get
1617 fprintf(stderr
, Name
": Limited v0.90 array to "
1618 "2TB per device\n");
1623 if (array
.size
!= size
) {
1624 /* got truncated to 32bit, write to
1625 * component_size instead
1628 rv
= sysfs_set_num(sra
, NULL
,
1629 "component_size", size
);
1633 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
1637 /* restore metadata */
1638 if (reshape_super(st
, orig_size
, UnSet
, UnSet
, 0, 0,
1639 UnSet
, NULL
, devname
, !quiet
) == 0)
1641 fprintf(stderr
, Name
": Cannot set device size for %s: %s\n",
1642 devname
, strerror(err
));
1644 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1645 fprintf(stderr
, " Bitmap must be removed before size can be changed\n");
1650 /* This will fail on kernels newer than 3.0 unless
1651 * a backport has been arranged.
1654 sysfs_set_str(sra
, NULL
, "resync_start", "none") < 0)
1655 fprintf(stderr
, Name
": --assume-clean not support with --grow on this kernel\n");
1657 ioctl(fd
, GET_ARRAY_INFO
, &array
);
1658 size
= get_component_size(fd
)/2;
1662 if (size
== orig_size
)
1663 fprintf(stderr
, Name
": component size of %s "
1664 "unchanged at %lluK\n",
1667 fprintf(stderr
, Name
": component size of %s "
1668 "has been set to %lluK\n",
1672 } else if (array
.level
!= LEVEL_CONTAINER
) {
1673 size
= get_component_size(fd
)/2;
1678 /* See if there is anything else to do */
1679 if ((level
== UnSet
|| level
== array
.level
) &&
1680 (layout_str
== NULL
) &&
1681 (chunksize
== 0 || chunksize
== array
.chunk_size
) &&
1682 (raid_disks
== 0 || raid_disks
== array
.raid_disks
)) {
1683 /* Nothing more to do */
1684 if (!changed
&& !quiet
)
1685 fprintf(stderr
, Name
": %s: no change requested\n",
1690 /* ========= check for Raid10/Raid1 -> Raid0 conversion ===============
1691 * current implementation assumes that following conditions must be met:
1694 * - near_copies == 2
1696 if ((level
== 0 && array
.level
== 10 && sra
&&
1697 array
.layout
== ((1 << 8) + 2) && !(array
.raid_disks
& 1)) ||
1698 (level
== 0 && array
.level
== 1 && sra
)) {
1700 err
= remove_disks_for_takeover(st
, sra
, array
.layout
);
1702 dprintf(Name
": Array cannot be reshaped\n");
1708 /* Make sure mdmon has seen the device removal
1709 * and updated metadata before we continue with
1713 ping_monitor(container
);
1716 memset(&info
, 0, sizeof(info
));
1718 sysfs_init(&info
, fd
, NoMdDev
);
1719 strcpy(info
.text_version
, sra
->text_version
);
1720 info
.component_size
= size
*2;
1721 info
.new_level
= level
;
1722 info
.new_chunk
= chunksize
* 1024;
1723 if (info
.array
.level
== LEVEL_CONTAINER
) {
1724 info
.delta_disks
= UnSet
;
1725 info
.array
.raid_disks
= raid_disks
;
1726 } else if (raid_disks
)
1727 info
.delta_disks
= raid_disks
- info
.array
.raid_disks
;
1729 info
.delta_disks
= UnSet
;
1730 if (layout_str
== NULL
) {
1731 info
.new_layout
= UnSet
;
1732 if (info
.array
.level
== 6 &&
1733 (info
.new_level
== 6 || info
.new_level
== UnSet
) &&
1734 info
.array
.layout
>= 16) {
1735 fprintf(stderr
, Name
1736 ": %s has a non-standard layout. If you"
1737 " wish to preserve this\n"
1738 " during the reshape, please specify"
1739 " --layout=preserve\n"
1740 " If you want to change it, specify a"
1741 " layout or use --layout=normalise\n",
1746 } else if (strcmp(layout_str
, "normalise") == 0 ||
1747 strcmp(layout_str
, "normalize") == 0) {
1748 /* If we have a -6 RAID6 layout, remove the '-6'. */
1749 info
.new_layout
= UnSet
;
1750 if (info
.array
.level
== 6 && info
.new_level
== UnSet
) {
1752 strcpy(l
, map_num(r6layout
, info
.array
.layout
));
1753 h
= strrchr(l
, '-');
1754 if (h
&& strcmp(h
, "-6") == 0) {
1756 info
.new_layout
= map_name(r6layout
, l
);
1759 } else if (strcmp(layout_str
, "preserve") == 0) {
1760 info
.new_layout
= UnSet
;
1762 int l
= info
.new_level
;
1764 l
= info
.array
.level
;
1767 info
.new_layout
= map_name(r5layout
, layout_str
);
1770 info
.new_layout
= map_name(r6layout
, layout_str
);
1773 info
.new_layout
= parse_layout_10(layout_str
);
1776 info
.new_layout
= parse_layout_faulty(layout_str
);
1779 fprintf(stderr
, Name
": layout not meaningful"
1780 " with this level\n");
1784 if (info
.new_layout
== UnSet
) {
1785 fprintf(stderr
, Name
": layout %s not understood"
1786 " for this level\n",
1793 if (array
.level
== LEVEL_FAULTY
) {
1794 if (level
!= UnSet
&& level
!= array
.level
) {
1795 fprintf(stderr
, Name
": cannot change level of Faulty device\n");
1799 fprintf(stderr
, Name
": cannot set chunksize of Faulty device\n");
1802 if (raid_disks
&& raid_disks
!= 1) {
1803 fprintf(stderr
, Name
": cannot set raid_disks of Faulty device\n");
1807 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
1808 dprintf("Cannot get array information.\n");
1811 array
.layout
= info
.new_layout
;
1812 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
1813 fprintf(stderr
, Name
": failed to set new layout\n");
1816 printf("layout for %s set to %d\n",
1817 devname
, array
.layout
);
1819 } else if (array
.level
== LEVEL_CONTAINER
) {
1820 /* This change is to be applied to every array in the
1821 * container. This is only needed when the metadata imposes
1822 * restraints of the various arrays in the container.
1823 * Currently we only know that IMSM requires all arrays
1824 * to have the same number of devices so changing the
1825 * number of devices (On-Line Capacity Expansion) must be
1826 * performed at the level of the container
1828 rv
= reshape_container(container
, devname
, -1, st
, &info
,
1829 force
, backup_file
, quiet
, 0, 0);
1832 /* get spare devices from external metadata
1834 if (st
->ss
->external
) {
1835 struct mdinfo
*info2
;
1837 info2
= st
->ss
->container_content(st
, subarray
);
1839 info
.array
.spare_disks
=
1840 info2
->array
.spare_disks
;
1845 /* Impose these changes on a single array. First
1846 * check that the metadata is OK with the change. */
1848 if (reshape_super(st
, info
.component_size
, info
.new_level
,
1849 info
.new_layout
, info
.new_chunk
,
1850 info
.array
.raid_disks
, info
.delta_disks
,
1851 backup_file
, devname
, quiet
)) {
1856 rv
= reshape_array(container
, fd
, devname
, st
, &info
, force
,
1857 devlist
, backup_file
, quiet
, 0, 0, 0);
1866 static int reshape_array(char *container
, int fd
, char *devname
,
1867 struct supertype
*st
, struct mdinfo
*info
,
1868 int force
, struct mddev_dev
*devlist
,
1869 char *backup_file
, int quiet
, int forked
,
1870 int restart
, int freeze_reshape
)
1872 struct reshape reshape
;
1875 int orig_level
= UnSet
;
1878 struct mdu_array_info_s array
;
1881 struct mddev_dev
*dv
;
1885 unsigned long long *offsets
;
1889 unsigned long blocks
;
1890 unsigned long cache
;
1891 unsigned long long array_size
;
1893 struct mdinfo
*sra
= NULL
;
1895 /* when reshaping a RAID0, the component_size might be zero.
1896 * So try to fix that up.
1898 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
1899 dprintf("Cannot get array information.\n");
1902 if (array
.level
== 0 && info
->component_size
== 0) {
1903 get_dev_size(fd
, NULL
, &array_size
);
1904 info
->component_size
= array_size
/ array
.raid_disks
;
1907 if (info
->reshape_active
) {
1908 int new_level
= info
->new_level
;
1909 info
->new_level
= UnSet
;
1910 info
->array
.raid_disks
-= info
->delta_disks
;
1911 msg
= analyse_change(info
, &reshape
);
1912 info
->new_level
= new_level
;
1913 info
->array
.raid_disks
+= info
->delta_disks
;
1915 /* Make sure the array isn't read-only */
1916 ioctl(fd
, RESTART_ARRAY_RW
, 0);
1918 msg
= analyse_change(info
, &reshape
);
1920 fprintf(stderr
, Name
": %s\n", msg
);
1924 (reshape
.level
!= info
->array
.level
||
1925 reshape
.before
.layout
!= info
->array
.layout
||
1926 reshape
.before
.data_disks
+ reshape
.parity
1927 != info
->array
.raid_disks
- info
->delta_disks
)) {
1928 fprintf(stderr
, Name
": reshape info is not in native format -"
1929 " cannot continue.\n");
1934 /* reshape already started. just skip to monitoring the reshape */
1935 if (reshape
.backup_blocks
== 0)
1939 /* The container is frozen but the array may not be.
1940 * So freeze the array so spares don't get put to the wrong use
1941 * FIXME there should probably be a cleaner separation between
1942 * freeze_array and freeze_container.
1944 sysfs_freeze_array(info
);
1945 /* Check we have enough spares to not be degraded */
1947 for (dv
= devlist
; dv
; dv
=dv
->next
)
1949 spares_needed
= max(reshape
.before
.data_disks
,
1950 reshape
.after
.data_disks
)
1951 + reshape
.parity
- array
.raid_disks
;
1954 info
->new_level
> 1 && info
->array
.level
> 1 &&
1955 spares_needed
> info
->array
.spare_disks
+ added_disks
) {
1957 Name
": Need %d spare%s to avoid degraded array,"
1958 " and only have %d.\n"
1959 " Use --force to over-ride this check.\n",
1961 spares_needed
== 1 ? "" : "s",
1962 info
->array
.spare_disks
+ added_disks
);
1965 /* Check we have enough spares to not fail */
1966 spares_needed
= max(reshape
.before
.data_disks
,
1967 reshape
.after
.data_disks
)
1969 if ((info
->new_level
> 1 || info
->new_level
== 0) &&
1970 spares_needed
> info
->array
.spare_disks
+added_disks
) {
1972 Name
": Need %d spare%s to create working array,"
1973 " and only have %d.\n",
1975 spares_needed
== 1 ? "" : "s",
1976 info
->array
.spare_disks
+ added_disks
);
1980 if (reshape
.level
!= array
.level
) {
1981 char *c
= map_num(pers
, reshape
.level
);
1986 err
= sysfs_set_str(info
, NULL
, "level", c
);
1989 fprintf(stderr
, Name
": %s: could not set level to %s\n",
1992 (info
->array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1993 fprintf(stderr
, " Bitmap must be removed"
1994 " before level can be changed\n");
1998 fprintf(stderr
, Name
": level of %s changed to %s\n",
2000 orig_level
= array
.level
;
2001 sysfs_freeze_array(info
);
2003 if (reshape
.level
> 0 && st
->ss
->external
) {
2004 /* make sure mdmon is aware of the new level */
2005 if (!mdmon_running(st
->container_dev
))
2006 start_mdmon(st
->container_dev
);
2007 ping_monitor(container
);
2008 if (mdmon_running(st
->container_dev
) &&
2009 st
->update_tail
== NULL
)
2010 st
->update_tail
= &st
->updates
;
2013 /* ->reshape_super might have chosen some spares from the
2014 * container that it wants to be part of the new array.
2015 * We can collect them with ->container_content and give
2016 * them to the kernel.
2018 if (st
->ss
->reshape_super
&& st
->ss
->container_content
) {
2019 char *subarray
= strchr(info
->text_version
+1, '/')+1;
2020 struct mdinfo
*info2
=
2021 st
->ss
->container_content(st
, subarray
);
2025 sysfs_init(info2
, fd
, st
->devnum
);
2026 /* When increasing number of devices, we need to set
2027 * new raid_disks before adding these, or they might
2030 if (reshape
.backup_blocks
&&
2031 reshape
.after
.data_disks
> reshape
.before
.data_disks
)
2032 subarray_set_num(container
, info2
, "raid_disks",
2033 reshape
.after
.data_disks
+
2035 for (d
= info2
->devs
; d
; d
= d
->next
) {
2036 if (d
->disk
.state
== 0 &&
2037 d
->disk
.raid_disk
>= 0) {
2038 /* This is a spare that wants to
2039 * be part of the array.
2041 add_disk(fd
, st
, info2
, d
);
2047 /* We might have been given some devices to add to the
2048 * array. Now that the array has been changed to the right
2049 * level and frozen, we can safely add them.
2052 Manage_subdevs(devname
, fd
, devlist
, !quiet
,
2055 if (reshape
.backup_blocks
== 0) {
2056 /* No restriping needed, but we might need to impose
2057 * some more changes: layout, raid_disks, chunk_size
2059 /* read current array info */
2060 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
2061 dprintf("Cannot get array information.\n");
2064 /* compare current array info with new values and if
2065 * it is different update them to new */
2066 if (info
->new_layout
!= UnSet
&&
2067 info
->new_layout
!= array
.layout
) {
2068 array
.layout
= info
->new_layout
;
2069 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2070 fprintf(stderr
, Name
": failed to set new layout\n");
2073 printf("layout for %s set to %d\n",
2074 devname
, array
.layout
);
2076 if (info
->delta_disks
!= UnSet
&&
2077 info
->delta_disks
!= 0 &&
2078 array
.raid_disks
!= (info
->array
.raid_disks
+ info
->delta_disks
)) {
2079 array
.raid_disks
+= info
->delta_disks
;
2080 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2081 fprintf(stderr
, Name
": failed to set raid disks\n");
2083 } else if (!quiet
) {
2084 printf("raid_disks for %s set to %d\n",
2085 devname
, array
.raid_disks
);
2088 if (info
->new_chunk
!= 0 &&
2089 info
->new_chunk
!= array
.chunk_size
) {
2090 if (sysfs_set_num(info
, NULL
,
2091 "chunk_size", info
->new_chunk
) != 0) {
2092 fprintf(stderr
, Name
": failed to set chunk size\n");
2095 printf("chunk size for %s set to %d\n",
2096 devname
, array
.chunk_size
);
2103 * There are three possibilities.
2104 * 1/ The array will shrink.
2105 * We need to ensure the reshape will pause before reaching
2106 * the 'critical section'. We also need to fork and wait for
2107 * that to happen. When it does we
2108 * suspend/backup/complete/unfreeze
2110 * 2/ The array will not change size.
2111 * This requires that we keep a backup of a sliding window
2112 * so that we can restore data after a crash. So we need
2113 * to fork and monitor progress.
2114 * In future we will allow the data_offset to change, so
2115 * a sliding backup becomes unnecessary.
2117 * 3/ The array will grow. This is relatively easy.
2118 * However the kernel's restripe routines will cheerfully
2119 * overwrite some early data before it is safe. So we
2120 * need to make a backup of the early parts of the array
2121 * and be ready to restore it if rebuild aborts very early.
2122 * For externally managed metadata, we still need a forked
2123 * child to monitor the reshape and suspend IO over the region
2124 * that is being reshaped.
2126 * We backup data by writing it to one spare, or to a
2127 * file which was given on command line.
2129 * In each case, we first make sure that storage is available
2130 * for the required backup.
2132 * - request the shape change.
2133 * - fork to handle backup etc.
2136 /* Check that we can hold all the data */
2137 get_dev_size(fd
, NULL
, &array_size
);
2138 if (reshape
.new_size
< (array_size
/512)) {
2140 Name
": this change will reduce the size of the array.\n"
2141 " use --grow --array-size first to truncate array.\n"
2142 " e.g. mdadm --grow %s --array-size %llu\n",
2143 devname
, reshape
.new_size
/2);
2147 sra
= sysfs_read(fd
, 0,
2148 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
|
2151 fprintf(stderr
, Name
": %s: Cannot get array details from sysfs\n",
2156 /* Decide how many blocks (sectors) for a reshape
2157 * unit. The number we have so far is just a minimum
2159 blocks
= reshape
.backup_blocks
;
2160 if (reshape
.before
.data_disks
==
2161 reshape
.after
.data_disks
) {
2162 /* Make 'blocks' bigger for better throughput, but
2163 * not so big that we reject it below.
2164 * Try for 16 megabytes
2166 while (blocks
* 32 < sra
->component_size
&&
2170 fprintf(stderr
, Name
": Need to backup %luK of critical "
2171 "section..\n", blocks
/2);
2173 if (blocks
>= sra
->component_size
/2) {
2174 fprintf(stderr
, Name
": %s: Something wrong"
2175 " - reshape aborted\n",
2180 /* Now we need to open all these devices so we can read/write.
2182 nrdisks
= max(reshape
.before
.data_disks
,
2183 reshape
.after
.data_disks
) + reshape
.parity
2184 + sra
->array
.spare_disks
;
2185 fdlist
= malloc((1+nrdisks
) * sizeof(int));
2186 offsets
= malloc((1+nrdisks
) * sizeof(offsets
[0]));
2187 if (!fdlist
|| !offsets
) {
2188 fprintf(stderr
, Name
": malloc failed: grow aborted\n");
2192 odisks
= reshape
.before
.data_disks
+ reshape
.parity
;
2193 d
= reshape_prepare_fdlist(devname
, sra
, odisks
,
2194 nrdisks
, blocks
, backup_file
,
2199 if ((st
->ss
->manage_reshape
== NULL
) ||
2200 (st
->ss
->recover_backup
== NULL
)) {
2201 if (backup_file
== NULL
) {
2202 if (reshape
.after
.data_disks
<=
2203 reshape
.before
.data_disks
) {
2204 fprintf(stderr
, Name
": %s: Cannot grow - "
2205 "need backup-file\n", devname
);
2207 } else if (sra
->array
.spare_disks
== 0) {
2208 fprintf(stderr
, Name
": %s: Cannot grow - "
2209 "need a spare or backup-file to backup "
2210 "critical section\n", devname
);
2214 if (!reshape_open_backup_file(backup_file
, fd
, devname
,
2216 fdlist
+d
, offsets
+d
,
2224 /* lastly, check that the internal stripe cache is
2225 * large enough, or it won't work.
2226 * It must hold at least 4 stripes of the larger
2229 cache
= max(info
->array
.chunk_size
, info
->new_chunk
);
2230 cache
*= 4; /* 4 stripes minimum */
2231 cache
/= 512; /* convert to sectors */
2232 disks
= min(reshape
.before
.data_disks
, reshape
.after
.data_disks
);
2233 /* make sure there is room for 'blocks' with a bit to spare */
2234 if (cache
< 16 + blocks
/ disks
)
2235 cache
= 16 + blocks
/ disks
;
2236 cache
/= (4096/512); /* Covert from sectors to pages */
2238 if (sra
->cache_size
< cache
)
2239 subarray_set_num(container
, sra
, "stripe_cache_size",
2242 /* Right, everything seems fine. Let's kick things off.
2243 * If only changing raid_disks, use ioctl, else use
2248 sra
->new_chunk
= info
->new_chunk
;
2251 sra
->reshape_progress
= info
->reshape_progress
;
2253 sra
->reshape_progress
= 0;
2254 if (reshape
.after
.data_disks
< reshape
.before
.data_disks
)
2255 /* start from the end of the new array */
2256 sra
->reshape_progress
= (sra
->component_size
2257 * reshape
.after
.data_disks
);
2260 if (info
->array
.chunk_size
== info
->new_chunk
&&
2261 reshape
.before
.layout
== reshape
.after
.layout
&&
2262 st
->ss
->external
== 0) {
2263 /* use SET_ARRAY_INFO but only if reshape hasn't started */
2264 ioctl(fd
, GET_ARRAY_INFO
, &array
);
2265 array
.raid_disks
= reshape
.after
.data_disks
+ reshape
.parity
;
2267 ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2271 Name
": Cannot set device shape for %s: %s\n",
2272 devname
, strerror(errno
));
2275 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2277 " Bitmap must be removed before"
2278 " shape can be changed\n");
2282 } else if (!restart
) {
2283 /* set them all just in case some old 'new_*' value
2284 * persists from some earlier problem.
2287 if (sysfs_set_num(sra
, NULL
, "chunk_size", info
->new_chunk
) < 0)
2289 if (!err
&& sysfs_set_num(sra
, NULL
, "layout",
2290 reshape
.after
.layout
) < 0)
2292 if (!err
&& subarray_set_num(container
, sra
, "raid_disks",
2293 reshape
.after
.data_disks
+
2294 reshape
.parity
) < 0)
2297 fprintf(stderr
, Name
": Cannot set device shape for %s\n",
2301 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2303 " Bitmap must be removed before"
2304 " shape can be changed\n");
2309 err
= start_reshape(sra
, restart
,
2310 info
->array
.raid_disks
- reshape
.parity
);
2313 Name
": Cannot %s reshape for %s\n",
2314 restart
? "continue" : "start",
2319 sysfs_set_str(sra
, NULL
, "array_state", "active");
2320 if (freeze_reshape
) {
2324 fprintf(stderr
, Name
": Reshape has to be continued from"
2325 " location %llu when root fileststem has been mounted\n",
2326 sra
->reshape_progress
);
2330 /* Now we just need to kick off the reshape and watch, while
2331 * handling backups of the data...
2332 * This is all done by a forked background process.
2334 switch(forked
? 0 : fork()) {
2336 fprintf(stderr
, Name
": Cannot run child to monitor reshape: %s\n",
2348 if (check_env("MDADM_GROW_VERIFY"))
2349 fd
= open(devname
, O_RDONLY
| O_DIRECT
);
2352 mlockall(MCL_FUTURE
);
2354 if (st
->ss
->external
) {
2355 /* metadata handler takes it from here */
2356 done
= st
->ss
->manage_reshape(
2357 fd
, sra
, &reshape
, st
, blocks
,
2359 d
- odisks
, fdlist
+odisks
,
2362 done
= child_monitor(
2363 fd
, sra
, &reshape
, st
, blocks
,
2365 d
- odisks
, fdlist
+odisks
,
2368 if (backup_file
&& done
)
2369 unlink(backup_file
);
2375 if (!st
->ss
->external
&&
2376 !(reshape
.before
.data_disks
!= reshape
.after
.data_disks
2377 && info
->custom_array_size
) &&
2378 info
->new_level
== reshape
.level
&&
2380 /* no need to wait for the reshape to finish as
2381 * there is nothing more to do.
2387 if (st
->ss
->external
) {
2388 /* Re-load the metadata as much could have changed */
2389 int cfd
= open_dev(st
->container_dev
);
2391 ping_monitor(container
);
2392 st
->ss
->free_super(st
);
2393 st
->ss
->load_container(st
, cfd
, container
);
2398 /* set new array size if required customer_array_size is used
2401 if (reshape
.before
.data_disks
!=
2402 reshape
.after
.data_disks
&&
2403 info
->custom_array_size
) {
2404 struct mdinfo
*info2
;
2405 char *subarray
= strchr(info
->text_version
+1, '/')+1;
2407 info2
= st
->ss
->container_content(st
, subarray
);
2409 unsigned long long current_size
= 0;
2410 unsigned long long new_size
=
2411 info2
->custom_array_size
/2;
2413 if (sysfs_get_ll(sra
,
2416 ¤t_size
) == 0 &&
2417 new_size
> current_size
) {
2418 if (sysfs_set_num(sra
, NULL
,
2419 "array_size", new_size
)
2421 dprintf("Error: Cannot"
2424 dprintf("Array size "
2426 dprintf(" from %llu to %llu.\n",
2427 current_size
, new_size
);
2433 if (info
->new_level
!= reshape
.level
) {
2435 c
= map_num(pers
, info
->new_level
);
2437 err
= sysfs_set_str(sra
, NULL
, "level", c
);
2439 fprintf(stderr
, Name\
2440 ": %s: could not set level "
2441 "to %s\n", devname
, c
);
2443 if (info
->new_level
== 0)
2444 st
->update_tail
= NULL
;
2453 if (orig_level
!= UnSet
&& sra
) {
2454 c
= map_num(pers
, orig_level
);
2455 if (c
&& sysfs_set_str(sra
, NULL
, "level", c
) == 0)
2456 fprintf(stderr
, Name
": aborting level change\n");
2463 /* mdfd handle is passed to be closed in child process (after fork).
2465 int reshape_container(char *container
, char *devname
,
2467 struct supertype
*st
,
2468 struct mdinfo
*info
,
2471 int quiet
, int restart
, int freeze_reshape
)
2473 struct mdinfo
*cc
= NULL
;
2476 /* component_size is not meaningful for a container,
2477 * so pass '-1' meaning 'no change'
2480 reshape_super(st
, -1, info
->new_level
,
2481 info
->new_layout
, info
->new_chunk
,
2482 info
->array
.raid_disks
, info
->delta_disks
,
2483 backup_file
, devname
, quiet
)) {
2490 /* ping monitor to be sure that update is on disk
2492 ping_monitor(container
);
2495 case -1: /* error */
2496 perror("Cannot fork to complete reshape\n");
2499 default: /* parent */
2500 if (!freeze_reshape
)
2501 printf(Name
": multi-array reshape continues"
2502 " in background\n");
2509 /* close unused handle in child process
2515 /* For each member array with reshape_active,
2516 * we need to perform the reshape.
2517 * We pick the first array that needs reshaping and
2518 * reshape it. reshape_array() will re-read the metadata
2519 * so the next time through a different array should be
2520 * ready for reshape.
2521 * It is possible that the 'different' array will not
2522 * be assembled yet. In that case we simple exit.
2523 * When it is assembled, the mdadm which assembles it
2524 * will take over the reshape.
2526 struct mdinfo
*content
;
2528 struct mdstat_ent
*mdstat
;
2533 cc
= st
->ss
->container_content(st
, NULL
);
2535 for (content
= cc
; content
; content
= content
->next
) {
2537 if (!content
->reshape_active
)
2540 subarray
= strchr(content
->text_version
+1, '/')+1;
2541 mdstat
= mdstat_by_subdev(subarray
,
2542 devname2devnum(container
));
2550 fd
= open_dev(mdstat
->devnum
);
2553 adev
= map_dev(dev2major(mdstat
->devnum
),
2554 dev2minor(mdstat
->devnum
),
2557 adev
= content
->text_version
;
2559 sysfs_init(content
, fd
, mdstat
->devnum
);
2561 rv
= reshape_array(container
, fd
, adev
, st
,
2562 content
, force
, NULL
,
2563 backup_file
, quiet
, 1, restart
,
2567 if (freeze_reshape
) {
2583 * We run a child process in the background which performs the following
2585 * - wait for resync to reach a certain point
2586 * - suspend io to the following section
2587 * - backup that section
2588 * - allow resync to proceed further
2590 * - discard the backup.
2592 * When are combined in slightly different ways in the three cases.
2594 * - suspend/backup/allow/wait/resume/discard
2596 * - allow/wait/suspend/backup/allow/wait/resume/discard
2598 * - wait/resume/discard/suspend/backup/allow
2600 * suspend/backup/allow always come together
2601 * wait/resume/discard do too.
2602 * For the same-size case we have two backups to improve flow.
2606 int progress_reshape(struct mdinfo
*info
, struct reshape
*reshape
,
2607 unsigned long long backup_point
,
2608 unsigned long long wait_point
,
2609 unsigned long long *suspend_point
,
2610 unsigned long long *reshape_completed
)
2612 /* This function is called repeatedly by the reshape manager.
2613 * It determines how much progress can safely be made and allows
2615 * - 'info' identifies the array and particularly records in
2616 * ->reshape_progress the metadata's knowledge of progress
2617 * This is a sector offset from the start of the array
2618 * of the next array block to be relocated. This number
2619 * may increase from 0 or decrease from array_size, depending
2620 * on the type of reshape that is happening.
2621 * Note that in contrast, 'sync_completed' is a block count of the
2622 * reshape so far. It gives the distance between the start point
2623 * (head or tail of device) and the next place that data will be
2624 * written. It always increases.
2625 * - 'reshape' is the structure created by analyse_change
2626 * - 'backup_point' shows how much the metadata manager has backed-up
2627 * data. For reshapes with increasing progress, it is the next address
2628 * to be backed up, previous addresses have been backed-up. For
2629 * decreasing progress, it is the earliest address that has been
2630 * backed up - later address are also backed up.
2631 * So addresses between reshape_progress and backup_point are
2632 * backed up providing those are in the 'correct' order.
2633 * - 'wait_point' is an array address. When reshape_completed
2634 * passes this point, progress_reshape should return. It might
2635 * return earlier if it determines that ->reshape_progress needs
2636 * to be updated or further backup is needed.
2637 * - suspend_point is maintained by progress_reshape and the caller
2638 * should not touch it except to initialise to zero.
2639 * It is an array address and it only increases in 2.6.37 and earlier.
2640 * This makes it difficult to handle reducing reshapes with
2641 * external metadata.
2642 * However: it is similar to backup_point in that it records the
2643 * other end of a suspended region from reshape_progress.
2644 * it is moved to extend the region that is safe to backup and/or
2646 * - reshape_completed is read from sysfs and returned. The caller
2647 * should copy this into ->reshape_progress when it has reason to
2648 * believe that the metadata knows this, and any backup outside this
2652 * 1 if more data from backup_point - but only as far as suspend_point,
2653 * should be backed up
2654 * 0 if things are progressing smoothly
2655 * -1 if the reshape is finished because it is all done,
2656 * -2 if the reshape is finished due to an error.
2659 int advancing
= (reshape
->after
.data_disks
2660 >= reshape
->before
.data_disks
);
2661 unsigned long long need_backup
; /* All data between start of array and
2662 * here will at some point need to
2665 unsigned long long read_offset
, write_offset
;
2666 unsigned long long write_range
;
2667 unsigned long long max_progress
, target
, completed
;
2668 unsigned long long array_size
= (info
->component_size
2669 * reshape
->before
.data_disks
);
2673 /* First, we unsuspend any region that is now known to be safe.
2674 * If suspend_point is on the 'wrong' side of reshape_progress, then
2675 * we don't have or need suspension at the moment. This is true for
2676 * native metadata when we don't need to back-up.
2679 if (info
->reshape_progress
<= *suspend_point
)
2680 sysfs_set_num(info
, NULL
, "suspend_lo",
2681 info
->reshape_progress
);
2683 /* Note: this won't work in 2.6.37 and before.
2684 * Something somewhere should make sure we don't need it!
2686 if (info
->reshape_progress
>= *suspend_point
)
2687 sysfs_set_num(info
, NULL
, "suspend_hi",
2688 info
->reshape_progress
);
2691 /* Now work out how far it is safe to progress.
2692 * If the read_offset for ->reshape_progress is less than
2693 * 'blocks' beyond the write_offset, we can only progress as far
2695 * Otherwise we can progress until the write_offset for the new location
2696 * reaches (within 'blocks' of) the read_offset at the current location.
2697 * However that region must be suspended unless we are using native
2699 * If we need to suspend more, we limit it to 128M per device, which is
2700 * rather arbitrary and should be some time-based calculation.
2702 read_offset
= info
->reshape_progress
/ reshape
->before
.data_disks
;
2703 write_offset
= info
->reshape_progress
/ reshape
->after
.data_disks
;
2704 write_range
= info
->new_chunk
/512;
2705 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
2706 need_backup
= array_size
;
2708 need_backup
= reshape
->backup_blocks
;
2710 if (read_offset
< write_offset
+ write_range
)
2711 max_progress
= backup_point
;
2715 reshape
->after
.data_disks
;
2717 if (read_offset
> write_offset
- write_range
)
2718 /* Can only progress as far as has been backed up,
2719 * which must be suspended */
2720 max_progress
= backup_point
;
2721 else if (info
->reshape_progress
<= need_backup
)
2722 max_progress
= backup_point
;
2724 if (info
->array
.major_version
>= 0)
2725 /* Can progress until backup is needed */
2726 max_progress
= need_backup
;
2728 /* Can progress until metadata update is required */
2731 reshape
->after
.data_disks
;
2732 /* but data must be suspended */
2733 if (max_progress
< *suspend_point
)
2734 max_progress
= *suspend_point
;
2739 /* We know it is safe to progress to 'max_progress' providing
2740 * it is suspended or we are using native metadata.
2741 * Consider extending suspend_point 128M per device if it
2742 * is less than 64M per device beyond reshape_progress.
2743 * But always do a multiple of 'blocks'
2744 * FIXME this is too big - it takes to long to complete
2747 target
= 64*1024*2 * min(reshape
->before
.data_disks
,
2748 reshape
->after
.data_disks
);
2749 target
/= reshape
->backup_blocks
;
2752 target
*= reshape
->backup_blocks
;
2754 /* For externally managed metadata we always need to suspend IO to
2755 * the area being reshaped so we regularly push suspend_point forward.
2756 * For native metadata we only need the suspend if we are going to do
2760 if ((need_backup
> info
->reshape_progress
2761 || info
->array
.major_version
< 0) &&
2762 *suspend_point
< info
->reshape_progress
+ target
) {
2763 if (need_backup
< *suspend_point
+ 2 * target
)
2764 *suspend_point
= need_backup
;
2765 else if (*suspend_point
+ 2 * target
< array_size
)
2766 *suspend_point
+= 2 * target
;
2768 *suspend_point
= array_size
;
2769 sysfs_set_num(info
, NULL
, "suspend_hi", *suspend_point
);
2770 if (max_progress
> *suspend_point
)
2771 max_progress
= *suspend_point
;
2774 if (info
->array
.major_version
>= 0) {
2775 /* Only need to suspend when about to backup */
2776 if (info
->reshape_progress
< need_backup
* 2 &&
2777 *suspend_point
> 0) {
2779 sysfs_set_num(info
, NULL
, "suspend_lo", 0);
2780 sysfs_set_num(info
, NULL
, "suspend_hi", need_backup
);
2783 /* Need to suspend continually */
2784 if (info
->reshape_progress
< *suspend_point
)
2785 *suspend_point
= info
->reshape_progress
;
2786 if (*suspend_point
+ target
< info
->reshape_progress
)
2787 /* No need to move suspend region yet */;
2789 if (*suspend_point
>= 2 * target
)
2790 *suspend_point
-= 2 * target
;
2793 sysfs_set_num(info
, NULL
, "suspend_lo",
2796 if (max_progress
< *suspend_point
)
2797 max_progress
= *suspend_point
;
2801 /* now set sync_max to allow that progress. sync_max, like
2802 * sync_completed is a count of sectors written per device, so
2803 * we find the difference between max_progress and the start point,
2804 * and divide that by after.data_disks to get a sync_max
2806 * At the same time we convert wait_point to a similar number
2807 * for comparing against sync_completed.
2809 /* scale down max_progress to per_disk */
2810 max_progress
/= reshape
->after
.data_disks
;
2811 /* Round to chunk size as some kernels give an erroneously high number */
2812 max_progress
/= info
->new_chunk
/512;
2813 max_progress
*= info
->new_chunk
/512;
2814 /* And round to old chunk size as the kernel wants that */
2815 max_progress
/= info
->array
.chunk_size
/512;
2816 max_progress
*= info
->array
.chunk_size
/512;
2817 /* Limit progress to the whole device */
2818 if (max_progress
> info
->component_size
)
2819 max_progress
= info
->component_size
;
2820 wait_point
/= reshape
->after
.data_disks
;
2822 /* switch from 'device offset' to 'processed block count' */
2823 max_progress
= info
->component_size
- max_progress
;
2824 wait_point
= info
->component_size
- wait_point
;
2827 sysfs_set_num(info
, NULL
, "sync_max", max_progress
);
2829 /* Now wait. If we have already reached the point that we were
2830 * asked to wait to, don't wait at all, else wait for any change.
2831 * We need to select on 'sync_completed' as that is the place that
2832 * notifications happen, but we are really interested in
2833 * 'reshape_position'
2835 fd
= sysfs_get_fd(info
, NULL
, "sync_completed");
2837 goto check_progress
;
2839 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
2840 goto check_progress
;
2842 while (completed
< max_progress
&& completed
< wait_point
) {
2843 /* Check that sync_action is still 'reshape' to avoid
2844 * waiting forever on a dead array
2848 if (sysfs_get_str(info
, NULL
, "sync_action",
2850 strncmp(action
, "reshape", 7) != 0)
2852 /* Some kernels reset 'sync_completed' to zero
2853 * before setting 'sync_action' to 'idle'.
2854 * So we need these extra tests.
2856 if (completed
== 0 && advancing
2857 && info
->reshape_progress
> 0)
2859 if (completed
== 0 && !advancing
2860 && info
->reshape_progress
< (info
->component_size
2861 * reshape
->after
.data_disks
))
2865 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
2866 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
2867 goto check_progress
;
2869 /* Some kernels reset 'sync_completed' to zero,
2870 * we need to have real point we are in md
2873 completed
= max_progress
;
2875 /* some kernels can give an incorrectly high 'completed' number */
2876 completed
/= (info
->new_chunk
/512);
2877 completed
*= (info
->new_chunk
/512);
2878 /* Convert 'completed' back in to a 'progress' number */
2879 completed
*= reshape
->after
.data_disks
;
2881 completed
= info
->component_size
* reshape
->after
.data_disks
2884 *reshape_completed
= completed
;
2888 /* We return the need_backup flag. Caller will decide
2889 * how much - a multiple of ->backup_blocks up to *suspend_point
2892 return need_backup
> info
->reshape_progress
;
2894 return need_backup
>= info
->reshape_progress
;
2897 /* if we couldn't read a number from sync_completed, then
2898 * either the reshape did complete, or it aborted.
2899 * We can tell which by checking for 'none' in reshape_position.
2900 * If it did abort, then it might immediately restart if it
2901 * it was just a device failure that leaves us degraded but
2905 if (sysfs_get_str(info
, NULL
, "reshape_position", buf
, sizeof(buf
)) < 0
2906 || strncmp(buf
, "none", 4) != 0) {
2907 /* The abort might only be temporary. Wait up to 10
2908 * seconds for fd to contain a valid number again.
2914 while (fd
>= 0 && rv
< 0 && tv
.tv_sec
> 0) {
2918 if (select(fd
+1, NULL
, NULL
, &rfds
, &tv
) != 1)
2920 switch (sysfs_fd_get_ll(fd
, &completed
)) {
2922 /* all good again */
2925 case -2: /* read error - abort */
2932 return rv
; /* abort */
2934 /* Maybe racing with array shutdown - check state */
2937 if (sysfs_get_str(info
, NULL
, "array_state", buf
, sizeof(buf
)) < 0
2938 || strncmp(buf
, "inactive", 8) == 0
2939 || strncmp(buf
, "clear",5) == 0)
2940 return -2; /* abort */
2941 return -1; /* complete */
2946 /* FIXME return status is never checked */
2947 static int grow_backup(struct mdinfo
*sra
,
2948 unsigned long long offset
, /* per device */
2949 unsigned long stripes
, /* per device, in old chunks */
2950 int *sources
, unsigned long long *offsets
,
2951 int disks
, int chunk
, int level
, int layout
,
2952 int dests
, int *destfd
, unsigned long long *destoffsets
,
2953 int part
, int *degraded
,
2956 /* Backup 'blocks' sectors at 'offset' on each device of the array,
2957 * to storage 'destfd' (offset 'destoffsets'), after first
2958 * suspending IO. Then allow resync to continue
2959 * over the suspended section.
2960 * Use part 'part' of the backup-super-block.
2965 unsigned long long ll
;
2967 //printf("offset %llu\n", offset);
2973 /* Check that array hasn't become degraded, else we might backup the wrong data */
2974 if (sysfs_get_ll(sra
, NULL
, "degraded", &ll
) < 0)
2975 return -1; /* FIXME this error is ignored */
2976 new_degraded
= (int)ll
;
2977 if (new_degraded
!= *degraded
) {
2978 /* check each device to ensure it is still working */
2980 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2981 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2983 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
2985 if (sysfs_get_str(sra
, sd
, "state", sbuf
, 20) < 0 ||
2986 strstr(sbuf
, "faulty") ||
2987 strstr(sbuf
, "in_sync") == NULL
) {
2988 /* this device is dead */
2989 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
2990 if (sd
->disk
.raid_disk
>= 0 &&
2991 sources
[sd
->disk
.raid_disk
] >= 0) {
2992 close(sources
[sd
->disk
.raid_disk
]);
2993 sources
[sd
->disk
.raid_disk
] = -1;
2998 *degraded
= new_degraded
;
3001 bsb
.arraystart2
= __cpu_to_le64(offset
* odata
);
3002 bsb
.length2
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
3004 bsb
.arraystart
= __cpu_to_le64(offset
* odata
);
3005 bsb
.length
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
3008 bsb
.magic
[15] = '2';
3009 for (i
= 0; i
< dests
; i
++)
3011 lseek64(destfd
[i
], destoffsets
[i
] + __le64_to_cpu(bsb
.devstart2
)*512, 0);
3013 lseek64(destfd
[i
], destoffsets
[i
], 0);
3015 rv
= save_stripes(sources
, offsets
,
3016 disks
, chunk
, level
, layout
,
3018 offset
*512*odata
, stripes
* chunk
* odata
,
3023 bsb
.mtime
= __cpu_to_le64(time(0));
3024 for (i
= 0; i
< dests
; i
++) {
3025 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
3027 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
3028 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
3029 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
3030 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
3033 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
] - 4096, 0)
3034 != destoffsets
[i
] - 4096)
3036 if (write(destfd
[i
], &bsb
, 512) != 512)
3038 if (destoffsets
[i
] > 4096) {
3039 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]+stripes
*chunk
*odata
, 0) !=
3040 destoffsets
[i
]+stripes
*chunk
*odata
)
3042 if (write(destfd
[i
], &bsb
, 512) != 512)
3052 /* in 2.6.30, the value reported by sync_completed can be
3053 * less that it should be by one stripe.
3054 * This only happens when reshape hits sync_max and pauses.
3055 * So allow wait_backup to either extent sync_max further
3056 * than strictly necessary, or return before the
3057 * sync has got quite as far as we would really like.
3058 * This is what 'blocks2' is for.
3059 * The various caller give appropriate values so that
3062 /* FIXME return value is often ignored */
3063 static int forget_backup(
3064 int dests
, int *destfd
, unsigned long long *destoffsets
,
3068 * Erase backup 'part' (which is 0 or 1)
3074 bsb
.arraystart2
= __cpu_to_le64(0);
3075 bsb
.length2
= __cpu_to_le64(0);
3077 bsb
.arraystart
= __cpu_to_le64(0);
3078 bsb
.length
= __cpu_to_le64(0);
3080 bsb
.mtime
= __cpu_to_le64(time(0));
3082 for (i
= 0; i
< dests
; i
++) {
3083 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
3084 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
3085 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
3086 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
3087 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
3088 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]-4096, 0) !=
3089 destoffsets
[i
]-4096)
3092 write(destfd
[i
], &bsb
, 512) != 512)
3099 static void fail(char *msg
)
3102 rv
= (write(2, msg
, strlen(msg
)) != (int)strlen(msg
));
3103 rv
|= (write(2, "\n", 1) != 1);
3107 static char *abuf
, *bbuf
;
3108 static unsigned long long abuflen
;
3109 static void validate(int afd
, int bfd
, unsigned long long offset
)
3111 /* check that the data in the backup against the array.
3112 * This is only used for regression testing and should not
3113 * be used while the array is active
3117 lseek64(bfd
, offset
- 4096, 0);
3118 if (read(bfd
, &bsb2
, 512) != 512)
3119 fail("cannot read bsb");
3120 if (bsb2
.sb_csum
!= bsb_csum((char*)&bsb2
,
3121 ((char*)&bsb2
.sb_csum
)-((char*)&bsb2
)))
3122 fail("first csum bad");
3123 if (memcmp(bsb2
.magic
, "md_backup_data", 14) != 0)
3124 fail("magic is bad");
3125 if (memcmp(bsb2
.magic
, "md_backup_data-2", 16) == 0 &&
3126 bsb2
.sb_csum2
!= bsb_csum((char*)&bsb2
,
3127 ((char*)&bsb2
.sb_csum2
)-((char*)&bsb2
)))
3128 fail("second csum bad");
3130 if (__le64_to_cpu(bsb2
.devstart
)*512 != offset
)
3131 fail("devstart is wrong");
3134 unsigned long long len
= __le64_to_cpu(bsb2
.length
)*512;
3136 if (abuflen
< len
) {
3140 if (posix_memalign((void**)&abuf
, 4096, abuflen
) ||
3141 posix_memalign((void**)&bbuf
, 4096, abuflen
)) {
3143 /* just stop validating on mem-alloc failure */
3148 lseek64(bfd
, offset
, 0);
3149 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
) {
3150 //printf("len %llu\n", len);
3151 fail("read first backup failed");
3153 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart
)*512, 0);
3154 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
3155 fail("read first from array failed");
3156 if (memcmp(bbuf
, abuf
, len
) != 0) {
3159 printf("offset=%llu len=%llu\n",
3160 (unsigned long long)__le64_to_cpu(bsb2
.arraystart
)*512, len
);
3161 for (i
=0; i
<len
; i
++)
3162 if (bbuf
[i
] != abuf
[i
]) {
3163 printf("first diff byte %d\n", i
);
3167 fail("data1 compare failed");
3171 unsigned long long len
= __le64_to_cpu(bsb2
.length2
)*512;
3173 if (abuflen
< len
) {
3177 abuf
= malloc(abuflen
);
3178 bbuf
= malloc(abuflen
);
3181 lseek64(bfd
, offset
+__le64_to_cpu(bsb2
.devstart2
)*512, 0);
3182 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
)
3183 fail("read second backup failed");
3184 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart2
)*512, 0);
3185 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
3186 fail("read second from array failed");
3187 if (memcmp(bbuf
, abuf
, len
) != 0)
3188 fail("data2 compare failed");
3192 int child_monitor(int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
3193 struct supertype
*st
, unsigned long blocks
,
3194 int *fds
, unsigned long long *offsets
,
3195 int dests
, int *destfd
, unsigned long long *destoffsets
)
3197 /* Monitor a reshape where backup is being performed using
3198 * 'native' mechanism - either to a backup file, or
3199 * to some space in a spare.
3203 unsigned long long speed
;
3204 unsigned long long suspend_point
, array_size
;
3205 unsigned long long backup_point
, wait_point
;
3206 unsigned long long reshape_completed
;
3208 int increasing
= reshape
->after
.data_disks
>= reshape
->before
.data_disks
;
3209 int part
= 0; /* The next part of the backup area to fill. It may already
3210 * be full, so we need to check */
3211 int level
= reshape
->level
;
3212 int layout
= reshape
->before
.layout
;
3213 int data
= reshape
->before
.data_disks
;
3214 int disks
= reshape
->before
.data_disks
+ reshape
->parity
;
3215 int chunk
= sra
->array
.chunk_size
;
3217 unsigned long stripes
;
3220 /* set up the backup-super-block. This requires the
3221 * uuid from the array.
3223 /* Find a superblock */
3224 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
3228 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
3230 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
3231 devfd
= dev_open(dn
, O_RDONLY
);
3234 ok
= st
->ss
->load_super(st
, devfd
, NULL
);
3240 fprintf(stderr
, Name
": Cannot find a superblock\n");
3244 memset(&bsb
, 0, 512);
3245 memcpy(bsb
.magic
, "md_backup_data-1", 16);
3246 st
->ss
->uuid_from_super(st
, uuid
);
3247 memcpy(bsb
.set_uuid
, uuid
, 16);
3248 bsb
.mtime
= __cpu_to_le64(time(0));
3249 bsb
.devstart2
= blocks
;
3251 stripes
= blocks
/ (sra
->array
.chunk_size
/512) /
3252 reshape
->before
.data_disks
;
3254 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
3255 /* Don't start the 'reshape' */
3257 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
) {
3258 sysfs_get_ll(sra
, NULL
, "sync_speed_min", &speed
);
3259 sysfs_set_num(sra
, NULL
, "sync_speed_min", 200000);
3263 array_size
= sra
->component_size
* reshape
->after
.data_disks
;
3264 backup_point
= sra
->reshape_progress
;
3267 array_size
= sra
->component_size
* reshape
->before
.data_disks
;
3268 backup_point
= reshape
->backup_blocks
;
3269 suspend_point
= array_size
;
3275 /* Want to return as soon the oldest backup slot can
3276 * be released as that allows us to start backing up
3277 * some more, providing suspend_point has been
3278 * advanced, which it should have.
3281 wait_point
= array_size
;
3282 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
3283 wait_point
= (__le64_to_cpu(bsb
.arraystart
) +
3284 __le64_to_cpu(bsb
.length
));
3285 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
3286 wait_point
= (__le64_to_cpu(bsb
.arraystart2
) +
3287 __le64_to_cpu(bsb
.length2
));
3290 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
3291 wait_point
= __le64_to_cpu(bsb
.arraystart
);
3292 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
3293 wait_point
= __le64_to_cpu(bsb
.arraystart2
);
3296 rv
= progress_reshape(sra
, reshape
,
3297 backup_point
, wait_point
,
3298 &suspend_point
, &reshape_completed
);
3299 /* external metadata would need to ping_monitor here */
3300 sra
->reshape_progress
= reshape_completed
;
3302 /* Clear any backup region that is before 'here' */
3304 if (__le64_to_cpu(bsb
.length
) > 0 &&
3305 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart
) +
3306 __le64_to_cpu(bsb
.length
)))
3307 forget_backup(dests
, destfd
,
3309 if (__le64_to_cpu(bsb
.length2
) > 0 &&
3310 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart2
) +
3311 __le64_to_cpu(bsb
.length2
)))
3312 forget_backup(dests
, destfd
,
3315 if (__le64_to_cpu(bsb
.length
) > 0 &&
3316 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart
)))
3317 forget_backup(dests
, destfd
,
3319 if (__le64_to_cpu(bsb
.length2
) > 0 &&
3320 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart2
)))
3321 forget_backup(dests
, destfd
,
3330 if (rv
== 0 && increasing
&& !st
->ss
->external
) {
3331 /* No longer need to monitor this reshape */
3337 unsigned long long offset
;
3338 unsigned long actual_stripes
;
3339 /* Need to backup some data.
3340 * If 'part' is not used and the desired
3341 * backup size is suspended, do a backup,
3342 * then consider the next part.
3344 /* Check that 'part' is unused */
3345 if (part
== 0 && __le64_to_cpu(bsb
.length
) != 0)
3347 if (part
== 1 && __le64_to_cpu(bsb
.length2
) != 0)
3350 offset
= backup_point
/ data
;
3351 actual_stripes
= stripes
;
3353 if (offset
+ actual_stripes
* (chunk
/512) >
3354 sra
->component_size
)
3355 actual_stripes
= ((sra
->component_size
- offset
)
3357 if (offset
+ actual_stripes
* (chunk
/512) >
3361 if (offset
< actual_stripes
* (chunk
/512))
3362 actual_stripes
= offset
/ (chunk
/512);
3363 offset
-= actual_stripes
* (chunk
/512);
3364 if (offset
< suspend_point
/data
)
3367 if (actual_stripes
== 0)
3369 grow_backup(sra
, offset
, actual_stripes
,
3371 disks
, chunk
, level
, layout
,
3372 dests
, destfd
, destoffsets
,
3373 part
, °raded
, buf
);
3374 validate(afd
, destfd
[0], destoffsets
[0]);
3375 /* record where 'part' is up to */
3378 backup_point
+= actual_stripes
* (chunk
/512) * data
;
3380 backup_point
-= actual_stripes
* (chunk
/512) * data
;
3384 /* FIXME maybe call progress_reshape one more time instead */
3385 abort_reshape(sra
); /* remove any remaining suspension */
3386 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
3387 sysfs_set_num(sra
, NULL
, "sync_speed_min", speed
);
3393 * If any spare contains md_back_data-1 which is recent wrt mtime,
3394 * write that data into the array and update the super blocks with
3395 * the new reshape_progress
3397 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
, int cnt
,
3398 char *backup_file
, int verbose
)
3402 unsigned long long *offsets
;
3403 unsigned long long nstripe
, ostripe
;
3406 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
3407 if (info
->array
.level
== 6) odata
--; /* number of data disks */
3408 ndata
= info
->array
.raid_disks
- 1;
3409 if (info
->new_level
== 6) ndata
--;
3411 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
3413 if (info
->delta_disks
<= 0)
3414 /* Didn't grow, so the backup file must have
3418 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
3419 struct mdinfo dinfo
;
3422 char *devname
, namebuf
[20];
3423 unsigned long long lo
, hi
;
3425 /* This was a spare and may have some saved data on it.
3426 * Load the superblock, find and load the
3427 * backup_super_block.
3428 * If either fail, go on to next device.
3429 * If the backup contains no new info, just return
3430 * else restore data and update all superblocks
3432 if (i
== old_disks
-1) {
3433 fd
= open(backup_file
, O_RDONLY
);
3435 fprintf(stderr
, Name
": backup file %s inaccessible: %s\n",
3436 backup_file
, strerror(errno
));
3439 devname
= backup_file
;
3444 if (st
->ss
->load_super(st
, fd
, NULL
))
3447 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
3448 st
->ss
->free_super(st
);
3451 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
3453 fprintf(stderr
, Name
": Cannot seek on device %d\n", i
);
3454 continue; /* Cannot seek */
3456 sprintf(namebuf
, "device-%d", i
);
3459 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
)) {
3461 fprintf(stderr
, Name
": Cannot read from %s\n", devname
);
3462 continue; /* Cannot read */
3464 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0 &&
3465 memcmp(bsb
.magic
, "md_backup_data-2", 16) != 0) {
3467 fprintf(stderr
, Name
": No backup metadata on %s\n", devname
);
3470 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
))) {
3472 fprintf(stderr
, Name
": Bad backup-metadata checksum on %s\n", devname
);
3473 continue; /* bad checksum */
3475 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0 &&
3476 bsb
.sb_csum2
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum2
)-((char*)&bsb
))) {
3478 fprintf(stderr
, Name
": Bad backup-metadata checksum2 on %s\n", devname
);
3479 continue; /* Bad second checksum */
3481 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0) {
3483 fprintf(stderr
, Name
": Wrong uuid on backup-metadata on %s\n", devname
);
3484 continue; /* Wrong uuid */
3487 /* array utime and backup-mtime should be updated at much the same time, but it seems that
3488 * sometimes they aren't... So allow considerable flexability in matching, and allow
3489 * this test to be overridden by an environment variable.
3491 if (info
->array
.utime
> (int)__le64_to_cpu(bsb
.mtime
) + 2*60*60 ||
3492 info
->array
.utime
< (int)__le64_to_cpu(bsb
.mtime
) - 10*60) {
3493 if (check_env("MDADM_GROW_ALLOW_OLD")) {
3494 fprintf(stderr
, Name
": accepting backup with timestamp %lu "
3495 "for array with timestamp %lu\n",
3496 (unsigned long)__le64_to_cpu(bsb
.mtime
),
3497 (unsigned long)info
->array
.utime
);
3500 fprintf(stderr
, Name
": too-old timestamp on "
3501 "backup-metadata on %s\n", devname
);
3502 continue; /* time stamp is too bad */
3506 if (bsb
.magic
[15] == '1') {
3507 if (bsb
.length
== 0)
3509 if (info
->delta_disks
>= 0) {
3510 /* reshape_progress is increasing */
3511 if (__le64_to_cpu(bsb
.arraystart
)
3512 + __le64_to_cpu(bsb
.length
)
3513 < info
->reshape_progress
) {
3516 fprintf(stderr
, Name
3517 ": backup-metadata found on %s but is not needed\n", devname
);
3518 continue; /* No new data here */
3521 /* reshape_progress is decreasing */
3522 if (__le64_to_cpu(bsb
.arraystart
) >=
3523 info
->reshape_progress
)
3524 goto nonew
; /* No new data here */
3527 if (bsb
.length
== 0 && bsb
.length2
== 0)
3529 if (info
->delta_disks
>= 0) {
3530 /* reshape_progress is increasing */
3531 if ((__le64_to_cpu(bsb
.arraystart
)
3532 + __le64_to_cpu(bsb
.length
)
3533 < info
->reshape_progress
)
3535 (__le64_to_cpu(bsb
.arraystart2
)
3536 + __le64_to_cpu(bsb
.length2
)
3537 < info
->reshape_progress
))
3538 goto nonew
; /* No new data here */
3540 /* reshape_progress is decreasing */
3541 if (__le64_to_cpu(bsb
.arraystart
) >=
3542 info
->reshape_progress
&&
3543 __le64_to_cpu(bsb
.arraystart2
) >=
3544 info
->reshape_progress
)
3545 goto nonew
; /* No new data here */
3548 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0) {
3551 fprintf(stderr
, Name
3552 ": Failed to verify secondary backup-metadata block on %s\n",
3554 continue; /* Cannot seek */
3556 /* There should be a duplicate backup superblock 4k before here */
3557 if (lseek64(fd
, -4096, 1) < 0 ||
3558 read(fd
, &bsb2
, sizeof(bsb2
)) != sizeof(bsb2
))
3559 goto second_fail
; /* Cannot find leading superblock */
3560 if (bsb
.magic
[15] == '1')
3561 bsbsize
= offsetof(struct mdp_backup_super
, pad1
);
3563 bsbsize
= offsetof(struct mdp_backup_super
, pad
);
3564 if (memcmp(&bsb2
, &bsb
, bsbsize
) != 0)
3565 goto second_fail
; /* Cannot find leading superblock */
3567 /* Now need the data offsets for all devices. */
3568 offsets
= malloc(sizeof(*offsets
)*info
->array
.raid_disks
);
3569 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
3572 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
3573 /* FIXME should be this be an error */
3575 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
3576 st
->ss
->free_super(st
);
3577 offsets
[j
] = dinfo
.data_offset
* 512;
3579 printf(Name
": restoring critical section\n");
3581 if (restore_stripes(fdlist
, offsets
,
3582 info
->array
.raid_disks
,
3586 fd
, __le64_to_cpu(bsb
.devstart
)*512,
3587 __le64_to_cpu(bsb
.arraystart
)*512,
3588 __le64_to_cpu(bsb
.length
)*512, NULL
)) {
3589 /* didn't succeed, so giveup */
3591 fprintf(stderr
, Name
": Error restoring backup from %s\n",
3596 if (bsb
.magic
[15] == '2' &&
3597 restore_stripes(fdlist
, offsets
,
3598 info
->array
.raid_disks
,
3602 fd
, __le64_to_cpu(bsb
.devstart
)*512 +
3603 __le64_to_cpu(bsb
.devstart2
)*512,
3604 __le64_to_cpu(bsb
.arraystart2
)*512,
3605 __le64_to_cpu(bsb
.length2
)*512, NULL
)) {
3606 /* didn't succeed, so giveup */
3608 fprintf(stderr
, Name
": Error restoring second backup from %s\n",
3614 /* Ok, so the data is restored. Let's update those superblocks. */
3618 lo
= __le64_to_cpu(bsb
.arraystart
);
3619 hi
= lo
+ __le64_to_cpu(bsb
.length
);
3621 if (bsb
.magic
[15] == '2' && bsb
.length2
) {
3622 unsigned long long lo1
, hi1
;
3623 lo1
= __le64_to_cpu(bsb
.arraystart2
);
3624 hi1
= lo1
+ __le64_to_cpu(bsb
.length2
);
3628 } else if (lo
< lo1
)
3634 (info
->reshape_progress
< lo
||
3635 info
->reshape_progress
> hi
))
3636 /* backup does not affect reshape_progress*/ ;
3637 else if (info
->delta_disks
>= 0) {
3638 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
) +
3639 __le64_to_cpu(bsb
.length
);
3640 if (bsb
.magic
[15] == '2') {
3641 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
) +
3642 __le64_to_cpu(bsb
.length2
);
3643 if (p2
> info
->reshape_progress
)
3644 info
->reshape_progress
= p2
;
3647 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
);
3648 if (bsb
.magic
[15] == '2') {
3649 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
);
3650 if (p2
< info
->reshape_progress
)
3651 info
->reshape_progress
= p2
;
3654 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
3655 if (fdlist
[j
] < 0) continue;
3656 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
3658 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
3659 dinfo
.reshape_progress
= info
->reshape_progress
;
3660 st
->ss
->update_super(st
, &dinfo
,
3661 "_reshape_progress",
3663 st
->ss
->store_super(st
, fdlist
[j
]);
3664 st
->ss
->free_super(st
);
3668 /* Didn't find any backup data, try to see if any
3671 if (info
->delta_disks
< 0) {
3672 /* When shrinking, the critical section is at the end.
3673 * So see if we are before the critical section.
3675 unsigned long long first_block
;
3676 nstripe
= ostripe
= 0;
3678 while (ostripe
>= nstripe
) {
3679 ostripe
+= info
->array
.chunk_size
/ 512;
3680 first_block
= ostripe
* odata
;
3681 nstripe
= first_block
/ ndata
/ (info
->new_chunk
/512) *
3682 (info
->new_chunk
/512);
3685 if (info
->reshape_progress
>= first_block
)
3688 if (info
->delta_disks
> 0) {
3689 /* See if we are beyond the critical section. */
3690 unsigned long long last_block
;
3691 nstripe
= ostripe
= 0;
3693 while (nstripe
>= ostripe
) {
3694 nstripe
+= info
->new_chunk
/ 512;
3695 last_block
= nstripe
* ndata
;
3696 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
3697 (info
->array
.chunk_size
/512);
3700 if (info
->reshape_progress
>= last_block
)
3703 /* needed to recover critical section! */
3705 fprintf(stderr
, Name
": Failed to find backup of critical section\n");
3709 int Grow_continue_command(char *devname
, int fd
,
3710 char *backup_file
, int verbose
)
3713 struct supertype
*st
= NULL
;
3714 struct mdinfo
*content
= NULL
;
3715 struct mdinfo array
;
3716 char *subarray
= NULL
;
3717 struct mdinfo
*cc
= NULL
;
3718 struct mdstat_ent
*mdstat
= NULL
;
3723 unsigned long long position
;
3725 dprintf("Grow continue from command line called for %s\n",
3728 st
= super_by_fd(fd
, &subarray
);
3729 if (!st
|| !st
->ss
) {
3731 Name
": Unable to determine metadata format for %s\n",
3735 dprintf("Grow continue is run for ");
3736 if (st
->ss
->external
== 0) {
3737 dprintf("native array (%s)\n", devname
);
3738 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) < 0) {
3739 fprintf(stderr
, Name
": %s is not an active md array -"
3740 " aborting\n", devname
);
3742 goto Grow_continue_command_exit
;
3745 sysfs_init(content
, fd
, st
->devnum
);
3750 dprintf("subarray (%s)\n", subarray
);
3751 container_dev
= st
->container_dev
;
3752 cfd
= open_dev_excl(st
->container_dev
);
3754 container_dev
= st
->devnum
;
3756 cfd
= open_dev_excl(st
->devnum
);
3757 dprintf("container (%i)\n", container_dev
);
3761 fprintf(stderr
, Name
": Unable to open container "
3762 "for %s\n", devname
);
3764 goto Grow_continue_command_exit
;
3766 fmt_devname(buf
, container_dev
);
3768 /* find in container array under reshape
3770 ret_val
= st
->ss
->load_container(st
, cfd
, NULL
);
3773 Name
": Cannot read superblock for %s\n",
3776 goto Grow_continue_command_exit
;
3779 cc
= st
->ss
->container_content(st
, NULL
);
3780 for (content
= cc
; content
; content
= content
->next
) {
3784 if (content
->reshape_active
== 0)
3786 /* The decision about array or container wide
3787 * reshape is taken in Grow_continue based
3788 * content->reshape_active state, therefore we
3789 * need to check_reshape based on
3790 * reshape_active and subarray name
3793 check_reshape((content
->reshape_active
== CONTAINER_RESHAPE
)? NULL
: subarray
,
3795 if (!allow_reshape
) {
3796 fprintf(stderr
, Name
3797 ": cannot continue reshape of an array"
3798 " in container with unsupported"
3799 " metadata: %s(%s)\n",
3802 goto Grow_continue_command_exit
;
3805 array
= strchr(content
->text_version
+1, '/')+1;
3806 mdstat
= mdstat_by_subdev(array
, container_dev
);
3813 Name
": Unable to determine reshaped "
3814 "array for %s\n", devname
);
3816 goto Grow_continue_command_exit
;
3818 fd2
= open_dev(mdstat
->devnum
);
3820 fprintf(stderr
, Name
": cannot open (md%i)\n",
3823 goto Grow_continue_command_exit
;
3826 sysfs_init(content
, fd2
, mdstat
->devnum
);
3828 /* start mdmon in case it is not running
3830 if (!mdmon_running(container_dev
))
3831 start_mdmon(container_dev
);
3834 if (mdmon_running(container_dev
))
3835 st
->update_tail
= &st
->updates
;
3837 fprintf(stderr
, Name
": No mdmon found. "
3838 "Grow cannot continue.\n");
3840 goto Grow_continue_command_exit
;
3844 /* verify that array under reshape is started from
3847 ret_val
= sysfs_get_str(content
, NULL
, "sync_max", buf
, 40);
3849 fprintf(stderr
, Name
3850 ": cannot open verify reshape progress for %s (%i)\n",
3851 content
->sys_name
, ret_val
);
3853 goto Grow_continue_command_exit
;
3855 dprintf(Name
": Read sync_max sysfs entry is: %s\n", buf
);
3856 position
= strtoull(buf
, &ep
, 0);
3857 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' ')) {
3858 fprintf(stderr
, Name
": Fatal error: array reshape was"
3859 " not properly frozen\n");
3861 goto Grow_continue_command_exit
;
3863 position
*= get_data_disks(map_name(pers
, mdstat
->level
),
3864 content
->new_layout
,
3865 content
->array
.raid_disks
);
3866 if (position
!= content
->reshape_progress
) {
3867 fprintf(stderr
, Name
": Fatal error: array reshape was"
3868 " not properly frozen.\n");
3870 goto Grow_continue_command_exit
;
3875 ret_val
= Grow_continue(fd
, st
, content
, backup_file
, 0);
3877 Grow_continue_command_exit
:
3882 st
->ss
->free_super(st
);
3883 free_mdstat(mdstat
);
3890 int Grow_continue(int mdfd
, struct supertype
*st
, struct mdinfo
*info
,
3891 char *backup_file
, int freeze_reshape
)
3895 if (!info
->reshape_active
)
3898 if (st
->ss
->external
) {
3900 int cfd
= open_dev(st
->container_dev
);
3905 fmt_devname(container
, st
->container_dev
);
3906 st
->ss
->load_container(st
, cfd
, container
);
3908 ret_val
= reshape_container(container
, NULL
, mdfd
,
3909 st
, info
, 0, backup_file
,
3910 0, 1, freeze_reshape
);
3912 ret_val
= reshape_array(NULL
, mdfd
, "array", st
, info
, 1,
3913 NULL
, backup_file
, 0, 0, 1,