2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2012 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
= xmalloc(sizeof(int) * disk_count
);
54 for (i
= 0; i
< next_spare
; i
++)
56 for (dev
= content
->devs
; dev
; dev
= dev
->next
) {
62 fd
= dev_open(buf
, O_RDWR
);
64 if (dev
->disk
.raid_disk
>= 0)
65 fdlist
[dev
->disk
.raid_disk
] = fd
;
67 fdlist
[next_spare
++] = fd
;
70 if (st
->ss
->external
&& st
->ss
->recover_backup
)
71 err
= st
->ss
->recover_backup(st
, content
);
73 err
= Grow_restart(st
, content
, fdlist
, next_spare
,
74 backup_file
, verbose
> 0);
76 while (next_spare
> 0) {
78 if (fdlist
[next_spare
] >= 0)
79 close(fdlist
[next_spare
]);
83 pr_err("Failed to restore critical"
84 " section for reshape - sorry.\n");
86 pr_err("Possibly you need"
87 " to specify a --backup-file\n");
91 dprintf("restore_backup() returns status OK.\n");
95 int Grow_Add_device(char *devname
, int fd
, char *newdev
)
97 /* Add a device to an active array.
98 * Currently, just extend a linear array.
99 * This requires writing a new superblock on the
100 * new device, calling the kernel to add the device,
101 * and if that succeeds, update the superblock on
103 * This means that we need to *find* all other devices.
110 struct supertype
*st
= NULL
;
111 char *subarray
= NULL
;
113 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
114 pr_err("cannot get array info for %s\n", devname
);
118 if (info
.array
.level
!= -1) {
119 pr_err("can only add devices to linear arrays\n");
123 st
= super_by_fd(fd
, &subarray
);
125 pr_err("cannot handle arrays with superblock version %d\n",
126 info
.array
.major_version
);
131 pr_err("Cannot grow linear sub-arrays yet\n");
137 nfd
= open(newdev
, O_RDWR
|O_EXCL
|O_DIRECT
);
139 pr_err("cannot open %s\n", newdev
);
144 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
145 pr_err("%s is not a block device!\n", newdev
);
150 /* now check out all the devices and make sure we can read the
152 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
153 mdu_disk_info_t disk
;
156 st
->ss
->free_super(st
);
159 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
160 pr_err("cannot get device detail for device %d\n",
166 dv
= map_dev(disk
.major
, disk
.minor
, 1);
168 pr_err("cannot find device file for device %d\n",
174 fd2
= dev_open(dv
, O_RDWR
);
176 pr_err("cannot open device file %s\n", dv
);
182 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
183 pr_err("cannot find super block on %s\n", dv
);
191 /* Ok, looks good. Lets update the superblock and write it out to
195 info
.disk
.number
= d
;
196 info
.disk
.major
= major(stb
.st_rdev
);
197 info
.disk
.minor
= minor(stb
.st_rdev
);
198 info
.disk
.raid_disk
= d
;
199 info
.disk
.state
= (1 << MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
);
200 st
->ss
->update_super(st
, &info
, "linear-grow-new", newdev
,
203 if (st
->ss
->store_super(st
, nfd
)) {
204 pr_err("Cannot store new superblock on %s\n",
211 if (ioctl(fd
, ADD_NEW_DISK
, &info
.disk
) != 0) {
212 pr_err("Cannot add new disk to this array\n");
215 /* Well, that seems to have worked.
216 * Now go through and update all superblocks
219 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
220 pr_err("cannot get array info for %s\n", devname
);
225 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
226 mdu_disk_info_t disk
;
230 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
231 pr_err("cannot get device detail for device %d\n",
235 dv
= map_dev(disk
.major
, disk
.minor
, 1);
237 pr_err("cannot find device file for device %d\n",
241 fd2
= dev_open(dv
, O_RDWR
);
243 pr_err("cannot open device file %s\n", dv
);
246 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
247 pr_err("cannot find super block on %s\n", dv
);
251 info
.array
.raid_disks
= nd
+1;
252 info
.array
.nr_disks
= nd
+1;
253 info
.array
.active_disks
= nd
+1;
254 info
.array
.working_disks
= nd
+1;
256 st
->ss
->update_super(st
, &info
, "linear-grow-update", dv
,
259 if (st
->ss
->store_super(st
, fd2
)) {
260 pr_err("Cannot store new superblock on %s\n", dv
);
270 int Grow_addbitmap(char *devname
, int fd
, struct context
*c
, struct shape
*s
)
273 * First check that array doesn't have a bitmap
274 * Then create the bitmap
277 * For internal bitmaps, we need to check the version,
278 * find all the active devices, and write the bitmap block
281 mdu_bitmap_file_t bmf
;
282 mdu_array_info_t array
;
283 struct supertype
*st
;
284 char *subarray
= NULL
;
285 int major
= BITMAP_MAJOR_HI
;
286 int vers
= md_get_version(fd
);
287 unsigned long long bitmapsize
, array_size
;
290 major
= BITMAP_MAJOR_HOSTENDIAN
;
291 pr_err("Warning - bitmaps created on this kernel"
292 " are not portable\n"
293 " between different architectures. Consider upgrading"
294 " the Linux kernel.\n");
297 if (ioctl(fd
, GET_BITMAP_FILE
, &bmf
) != 0) {
299 pr_err("Memory allocation failure.\n");
301 pr_err("bitmaps not supported by this kernel.\n");
304 if (bmf
.pathname
[0]) {
305 if (strcmp(s
->bitmap_file
,"none")==0) {
306 if (ioctl(fd
, SET_BITMAP_FILE
, -1)!= 0) {
307 pr_err("failed to remove bitmap %s\n",
313 pr_err("%s already has a bitmap (%s)\n",
314 devname
, bmf
.pathname
);
317 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
318 pr_err("cannot get array status for %s\n", devname
);
321 if (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)) {
322 if (strcmp(s
->bitmap_file
, "none")==0) {
323 array
.state
&= ~(1<<MD_SB_BITMAP_PRESENT
);
324 if (ioctl(fd
, SET_ARRAY_INFO
, &array
)!= 0) {
325 pr_err("failed to remove internal bitmap.\n");
330 pr_err("Internal bitmap already present on %s\n",
335 if (strcmp(s
->bitmap_file
, "none") == 0) {
336 pr_err("no bitmap found on %s\n", devname
);
339 if (array
.level
<= 0) {
340 pr_err("Bitmaps not meaningful with level %s\n",
341 map_num(pers
, array
.level
)?:"of this array");
344 bitmapsize
= array
.size
;
346 if (get_dev_size(fd
, NULL
, &array_size
) &&
347 array_size
> (0x7fffffffULL
<<9)) {
348 /* Array is big enough that we cannot trust array.size
349 * try other approaches
351 bitmapsize
= get_component_size(fd
);
353 if (bitmapsize
== 0) {
354 pr_err("Cannot reliably determine size of array to create bitmap - sorry.\n");
358 if (array
.level
== 10) {
359 int ncopies
= (array
.layout
&255)*((array
.layout
>>8)&255);
360 bitmapsize
= bitmapsize
* array
.raid_disks
/ ncopies
;
363 st
= super_by_fd(fd
, &subarray
);
365 pr_err("Cannot understand version %d.%d\n",
366 array
.major_version
, array
.minor_version
);
370 pr_err("Cannot add bitmaps to sub-arrays yet\n");
375 if (strcmp(s
->bitmap_file
, "internal") == 0) {
378 int offset_setable
= 0;
380 if (st
->ss
->add_internal_bitmap
== NULL
) {
381 pr_err("Internal bitmaps not supported "
382 "with %s metadata\n", st
->ss
->name
);
385 mdi
= sysfs_read(fd
, NULL
, GET_BITMAP_LOCATION
);
388 for (d
=0; d
< st
->max_devs
; d
++) {
389 mdu_disk_info_t disk
;
392 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
394 if (disk
.major
== 0 &&
397 if ((disk
.state
& (1<<MD_DISK_SYNC
))==0)
399 dv
= map_dev(disk
.major
, disk
.minor
, 1);
401 int fd2
= dev_open(dv
, O_RDWR
);
404 if (st
->ss
->load_super(st
, fd2
, NULL
)==0) {
405 if (st
->ss
->add_internal_bitmap(
407 &s
->bitmap_chunk
, c
->delay
, s
->write_behind
,
408 bitmapsize
, offset_setable
,
411 st
->ss
->write_bitmap(st
, fd2
);
413 pr_err("failed to create internal bitmap"
414 " - chunksize problem.\n");
422 if (offset_setable
) {
423 st
->ss
->getinfo_super(st
, mdi
, NULL
);
424 sysfs_init(mdi
, fd
, NULL
);
425 rv
= sysfs_set_num_signed(mdi
, NULL
, "bitmap/location",
428 array
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
429 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
433 pr_err("Cannot add bitmap while array is"
434 " resyncing or reshaping etc.\n");
435 pr_err("failed to set internal bitmap.\n");
442 int max_devs
= st
->max_devs
;
444 /* try to load a superblock */
445 for (d
= 0; d
< max_devs
; d
++) {
446 mdu_disk_info_t disk
;
450 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
452 if ((disk
.major
==0 && disk
.minor
==0) ||
453 (disk
.state
& (1<<MD_DISK_REMOVED
)))
455 dv
= map_dev(disk
.major
, disk
.minor
, 1);
458 fd2
= dev_open(dv
, O_RDONLY
);
460 if (st
->ss
->load_super(st
, fd2
, NULL
) == 0) {
462 st
->ss
->uuid_from_super(st
, uuid
);
469 pr_err("cannot find UUID for array!\n");
472 if (CreateBitmap(s
->bitmap_file
, c
->force
, (char*)uuid
, s
->bitmap_chunk
,
473 c
->delay
, s
->write_behind
, bitmapsize
, major
)) {
476 bitmap_fd
= open(s
->bitmap_file
, O_RDWR
);
478 pr_err("weird: %s cannot be opened\n",
482 if (ioctl(fd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
485 pr_err("Cannot add bitmap while array is"
486 " resyncing or reshaping etc.\n");
487 pr_err("Cannot set bitmap file for %s: %s\n",
488 devname
, strerror(err
));
497 * When reshaping an array we might need to backup some data.
498 * This is written to all spares with a 'super_block' describing it.
499 * The superblock goes 4K from the end of the used space on the
501 * It if written after the backup is complete.
502 * It has the following structure.
505 static struct mdp_backup_super
{
506 char magic
[16]; /* md_backup_data-1 or -2 */
509 /* start/sizes in 512byte sectors */
510 __u64 devstart
; /* address on backup device/file of data */
513 __u32 sb_csum
; /* csum of preceeding bytes. */
515 __u64 devstart2
; /* offset in to data of second section */
518 __u32 sb_csum2
; /* csum of preceeding bytes. */
520 } __attribute__((aligned(512))) bsb
, bsb2
;
522 static __u32
bsb_csum(char *buf
, int len
)
526 for (i
= 0; i
< len
; i
++)
527 csum
= (csum
<<3) + buf
[0];
528 return __cpu_to_le32(csum
);
531 static int check_idle(struct supertype
*st
)
533 /* Check that all member arrays for this container, or the
534 * container of this array, are idle
536 char *container
= (st
->container_devnm
[0]
537 ? st
->container_devnm
: st
->devnm
);
538 struct mdstat_ent
*ent
, *e
;
541 ent
= mdstat_read(0, 0);
542 for (e
= ent
; e
; e
= e
->next
) {
543 if (!is_container_member(e
, container
))
545 if (e
->percent
>= 0) {
554 static int freeze_container(struct supertype
*st
)
556 char *container
= (st
->container_devnm
[0]
557 ? st
->container_devnm
: st
->devnm
);
562 if (block_monitor(container
, 1)) {
563 pr_err("failed to freeze container\n");
570 static void unfreeze_container(struct supertype
*st
)
572 char *container
= (st
->container_devnm
[0]
573 ? st
->container_devnm
: st
->devnm
);
575 unblock_monitor(container
, 1);
578 static int freeze(struct supertype
*st
)
580 /* Try to freeze resync/rebuild on this array/container.
581 * Return -1 if the array is busy,
582 * return -2 container cannot be frozen,
583 * return 0 if this kernel doesn't support 'frozen'
584 * return 1 if it worked.
586 if (st
->ss
->external
)
587 return freeze_container(st
);
589 struct mdinfo
*sra
= sysfs_read(-1, st
->devnm
, GET_VERSION
);
595 /* Need to clear any 'read-auto' status */
596 if (sysfs_get_str(sra
, NULL
, "array_state", buf
, 20) > 0 &&
597 strncmp(buf
, "read-auto", 9) == 0)
598 sysfs_set_str(sra
, NULL
, "array_state", "clean");
600 err
= sysfs_freeze_array(sra
);
606 static void unfreeze(struct supertype
*st
)
608 if (st
->ss
->external
)
609 return unfreeze_container(st
);
611 struct mdinfo
*sra
= sysfs_read(-1, st
->devnm
, GET_VERSION
);
614 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
619 static void wait_reshape(struct mdinfo
*sra
)
621 int fd
= sysfs_get_fd(sra
, NULL
, "sync_action");
627 while (sysfs_fd_get_str(fd
, action
, 20) > 0 &&
628 strncmp(action
, "reshape", 7) == 0) {
632 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
637 static int reshape_super(struct supertype
*st
, unsigned long long size
,
638 int level
, int layout
, int chunksize
, int raid_disks
,
639 int delta_disks
, char *backup_file
, char *dev
,
640 int direction
, int verbose
)
642 /* nothing extra to check in the native case */
643 if (!st
->ss
->external
)
645 if (!st
->ss
->reshape_super
||
646 !st
->ss
->manage_reshape
) {
647 pr_err("%s metadata does not support reshape\n",
652 return st
->ss
->reshape_super(st
, size
, level
, layout
, chunksize
,
653 raid_disks
, delta_disks
, backup_file
, dev
,
657 static void sync_metadata(struct supertype
*st
)
659 if (st
->ss
->external
) {
660 if (st
->update_tail
) {
661 flush_metadata_updates(st
);
662 st
->update_tail
= &st
->updates
;
664 st
->ss
->sync_metadata(st
);
668 static int subarray_set_num(char *container
, struct mdinfo
*sra
, char *name
, int n
)
670 /* when dealing with external metadata subarrays we need to be
671 * prepared to handle EAGAIN. The kernel may need to wait for
672 * mdmon to mark the array active so the kernel can handle
673 * allocations/writeback when preparing the reshape action
674 * (md_allow_write()). We temporarily disable safe_mode_delay
675 * to close a race with the array_state going clean before the
676 * next write to raid_disks / stripe_cache_size
681 /* only 'raid_disks' and 'stripe_cache_size' trigger md_allow_write */
683 (strcmp(name
, "raid_disks") != 0 &&
684 strcmp(name
, "stripe_cache_size") != 0))
685 return sysfs_set_num(sra
, NULL
, name
, n
);
687 rc
= sysfs_get_str(sra
, NULL
, "safe_mode_delay", safe
, sizeof(safe
));
690 sysfs_set_num(sra
, NULL
, "safe_mode_delay", 0);
691 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
692 if (rc
< 0 && errno
== EAGAIN
) {
693 ping_monitor(container
);
694 /* if we get EAGAIN here then the monitor is not active
697 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
699 sysfs_set_str(sra
, NULL
, "safe_mode_delay", safe
);
703 int start_reshape(struct mdinfo
*sra
, int already_running
,
704 int before_data_disks
, int data_disks
)
707 unsigned long long sync_max_to_set
;
709 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
710 err
= sysfs_set_num(sra
, NULL
, "suspend_hi", sra
->reshape_progress
);
711 err
= err
?: sysfs_set_num(sra
, NULL
, "suspend_lo",
712 sra
->reshape_progress
);
713 if (before_data_disks
<= data_disks
)
714 sync_max_to_set
= sra
->reshape_progress
/ data_disks
;
716 sync_max_to_set
= (sra
->component_size
* data_disks
717 - sra
->reshape_progress
) / data_disks
;
718 if (!already_running
)
719 sysfs_set_num(sra
, NULL
, "sync_min", sync_max_to_set
);
720 err
= err
?: sysfs_set_num(sra
, NULL
, "sync_max", sync_max_to_set
);
721 if (!already_running
)
722 err
= err
?: sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
727 void abort_reshape(struct mdinfo
*sra
)
729 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
730 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
731 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
732 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
733 sysfs_set_num(sra
, NULL
, "sync_min", 0);
734 sysfs_set_str(sra
, NULL
, "sync_max", "max");
737 int remove_disks_for_takeover(struct supertype
*st
,
742 struct mdinfo
*remaining
;
745 if (sra
->array
.level
== 10)
746 nr_of_copies
= layout
& 0xff;
747 else if (sra
->array
.level
== 1)
748 nr_of_copies
= sra
->array
.raid_disks
;
752 remaining
= sra
->devs
;
754 /* for each 'copy', select one device and remove from the list. */
755 for (slot
= 0; slot
< sra
->array
.raid_disks
; slot
+= nr_of_copies
) {
756 struct mdinfo
**diskp
;
759 /* Find a working device to keep */
760 for (diskp
= &remaining
; *diskp
; diskp
= &(*diskp
)->next
) {
761 struct mdinfo
*disk
= *diskp
;
763 if (disk
->disk
.raid_disk
< slot
)
765 if (disk
->disk
.raid_disk
>= slot
+ nr_of_copies
)
767 if (disk
->disk
.state
& (1<<MD_DISK_REMOVED
))
769 if (disk
->disk
.state
& (1<<MD_DISK_FAULTY
))
771 if (!(disk
->disk
.state
& (1<<MD_DISK_SYNC
)))
774 /* We have found a good disk to use! */
776 disk
->next
= sra
->devs
;
785 if (slot
< sra
->array
.raid_disks
) {
786 /* didn't find all slots */
792 sra
->devs
= remaining
;
796 /* Remove all 'remaining' devices from the array */
798 struct mdinfo
*sd
= remaining
;
799 remaining
= sd
->next
;
801 sysfs_set_str(sra
, sd
, "state", "faulty");
802 sysfs_set_str(sra
, sd
, "slot", "none");
803 /* for external metadata disks should be removed in mdmon */
804 if (!st
->ss
->external
)
805 sysfs_set_str(sra
, sd
, "state", "remove");
806 sd
->disk
.state
|= (1<<MD_DISK_REMOVED
);
807 sd
->disk
.state
&= ~(1<<MD_DISK_SYNC
);
808 sd
->next
= sra
->devs
;
814 void reshape_free_fdlist(int *fdlist
,
815 unsigned long long *offsets
,
820 for (i
= 0; i
< size
; i
++)
828 int reshape_prepare_fdlist(char *devname
,
832 unsigned long blocks
,
835 unsigned long long *offsets
)
840 for (d
= 0; d
<= nrdisks
; d
++)
843 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
844 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
846 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
847 char *dn
= map_dev(sd
->disk
.major
,
849 fdlist
[sd
->disk
.raid_disk
]
850 = dev_open(dn
, O_RDONLY
);
851 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
852 if (fdlist
[sd
->disk
.raid_disk
] < 0) {
853 pr_err("%s: cannot open component %s\n",
854 devname
, dn
? dn
: "-unknown-");
858 } else if (backup_file
== NULL
) {
860 char *dn
= map_dev(sd
->disk
.major
,
862 fdlist
[d
] = dev_open(dn
, O_RDWR
);
863 offsets
[d
] = (sd
->data_offset
+ sra
->component_size
- blocks
- 8)*512;
865 pr_err("%s: cannot open component %s\n",
866 devname
, dn
? dn
: "-unknown-");
877 int reshape_open_backup_file(char *backup_file
,
882 unsigned long long *offsets
,
885 /* Return 1 on success, 0 on any form of failure */
886 /* need to check backup file is large enough */
892 *fdlist
= open(backup_file
, O_RDWR
|O_CREAT
|(restart
? O_TRUNC
: O_EXCL
),
896 pr_err("%s: cannot create backup file %s: %s\n",
897 devname
, backup_file
, strerror(errno
));
900 /* Guard against backup file being on array device.
901 * If array is partitioned or if LVM etc is in the
902 * way this will not notice, but it is better than
905 fstat(*fdlist
, &stb
);
908 if (stb
.st_rdev
== dev
) {
909 pr_err("backup file must NOT be"
910 " on the array being reshaped.\n");
916 for (i
=0; i
< blocks
+ 8 ; i
++) {
917 if (write(*fdlist
, buf
, 512) != 512) {
918 pr_err("%s: cannot create"
919 " backup file %s: %s\n",
920 devname
, backup_file
, strerror(errno
));
924 if (fsync(*fdlist
) != 0) {
925 pr_err("%s: cannot create backup file %s: %s\n",
926 devname
, backup_file
, strerror(errno
));
933 unsigned long GCD(unsigned long a
, unsigned long b
)
944 unsigned long compute_backup_blocks(int nchunk
, int ochunk
,
945 unsigned int ndata
, unsigned int odata
)
947 unsigned long a
, b
, blocks
;
948 /* So how much do we need to backup.
949 * We need an amount of data which is both a whole number of
950 * old stripes and a whole number of new stripes.
951 * So LCM for (chunksize*datadisks).
953 a
= (ochunk
/512) * odata
;
954 b
= (nchunk
/512) * ndata
;
957 /* LCM == product / GCD */
958 blocks
= (ochunk
/512) * (nchunk
/512) * odata
* ndata
/ a
;
963 char *analyse_change(struct mdinfo
*info
, struct reshape
*re
)
965 /* Based on the current array state in info->array and
966 * the changes in info->new_* etc, determine:
967 * - whether the change is possible
968 * - Intermediate level/raid_disks/layout
969 * - whether a restriping reshape is needed
970 * - number of sectors in minimum change unit. This
971 * will cover a whole number of stripes in 'before' and
974 * Return message if the change should be rejected
975 * NULL if the change can be achieved
977 * This can be called as part of starting a reshape, or
978 * when assembling an array that is undergoing reshape.
980 int near
, far
, offset
, copies
;
982 int old_chunk
, new_chunk
;
983 /* delta_parity records change in number of devices
984 * caused by level change
986 int delta_parity
= 0;
988 /* If a new level not explicitly given, we assume no-change */
989 if (info
->new_level
== UnSet
)
990 info
->new_level
= info
->array
.level
;
993 switch (info
->new_level
) {
999 /* chunk size is meaningful, must divide component_size
1002 if (info
->component_size
% (info
->new_chunk
/512))
1003 return "New chunk size does not"
1004 " divide component size";
1007 return "chunk size not meaningful for this level";
1010 info
->new_chunk
= info
->array
.chunk_size
;
1012 switch (info
->array
.level
) {
1014 return "Cannot understand this RAID level";
1016 /* RAID1 can convert to RAID1 with different disks, or
1017 * raid5 with 2 disks, or
1020 if (info
->new_level
> 1 &&
1021 (info
->component_size
& 7))
1022 return "Cannot convert RAID1 of this size - "
1023 "reduce size to multiple of 4K first.";
1024 if (info
->new_level
== 0) {
1025 if (info
->delta_disks
!= UnSet
&&
1026 info
->delta_disks
!= 0)
1027 return "Cannot change number of disks "
1028 "with RAID1->RAID0 conversion";
1030 re
->before
.data_disks
= 1;
1031 re
->after
.data_disks
= 1;
1032 re
->before
.layout
= 0;
1033 re
->backup_blocks
= 0;
1037 if (info
->new_level
== 1) {
1038 if (info
->delta_disks
== UnSet
)
1039 /* Don't know what to do */
1040 return "no change requested for Growing RAID1";
1042 re
->backup_blocks
= 0;
1046 if (info
->array
.raid_disks
== 2 &&
1047 info
->new_level
== 5) {
1050 re
->before
.data_disks
= 1;
1051 if (info
->delta_disks
!= UnSet
&&
1052 info
->delta_disks
!= 0)
1053 re
->after
.data_disks
= 1 + info
->delta_disks
;
1055 re
->after
.data_disks
= 1;
1056 if (re
->after
.data_disks
< 1)
1057 return "Number of disks too small for RAID5";
1059 re
->before
.layout
= ALGORITHM_LEFT_SYMMETRIC
;
1060 info
->array
.chunk_size
= 65536;
1063 /* Could do some multi-stage conversions, but leave that to
1066 return "Impossibly level change request for RAID1";
1069 /* RAID10 can be converted from near mode to
1070 * RAID0 by removing some devices.
1071 * It can also be reshaped if the kernel supports
1074 switch (info
->new_level
) {
1076 if ((info
->array
.layout
& ~0xff) != 0x100)
1077 return "Cannot Grow RAID10 with far/offset layout";
1078 /* number of devices must be multiple of number of copies */
1079 if (info
->array
.raid_disks
% (info
->array
.layout
& 0xff))
1080 return "RAID10 layout too complex for Grow operation";
1082 new_disks
= (info
->array
.raid_disks
1083 / (info
->array
.layout
& 0xff));
1084 if (info
->delta_disks
== UnSet
)
1085 info
->delta_disks
= (new_disks
1086 - info
->array
.raid_disks
);
1088 if (info
->delta_disks
!= new_disks
- info
->array
.raid_disks
)
1089 return "New number of raid-devices impossible for RAID10";
1090 if (info
->new_chunk
&&
1091 info
->new_chunk
!= info
->array
.chunk_size
)
1092 return "Cannot change chunk-size with RAID10 Grow";
1097 re
->before
.data_disks
= new_disks
;
1098 re
->after
.data_disks
= re
->before
.data_disks
;
1099 re
->before
.layout
= 0;
1100 re
->backup_blocks
= 0;
1104 near
= info
->array
.layout
& 0xff;
1105 far
= (info
->array
.layout
>> 8) & 0xff;
1106 offset
= info
->array
.layout
& 0x10000;
1107 if (far
> 1 && !offset
)
1108 return "Cannot reshape RAID10 in far-mode";
1109 copies
= near
* far
;
1111 old_chunk
= info
->array
.chunk_size
* far
;
1113 if (info
->new_layout
== UnSet
)
1114 info
->new_layout
= info
->array
.layout
;
1116 near
= info
->new_layout
& 0xff;
1117 far
= (info
->new_layout
>> 8) & 0xff;
1118 offset
= info
->new_layout
& 0x10000;
1119 if (far
> 1 && !offset
)
1120 return "Cannot reshape RAID10 to far-mode";
1121 if (near
* far
!= copies
)
1122 return "Cannot change number of copies"
1123 " when reshaping RAID10";
1125 if (info
->delta_disks
== UnSet
)
1126 info
->delta_disks
= 0;
1127 new_disks
= (info
->array
.raid_disks
+
1130 new_chunk
= info
->new_chunk
* far
;
1134 re
->before
.layout
= info
->array
.layout
;
1135 re
->before
.data_disks
= info
->array
.raid_disks
;
1136 re
->after
.layout
= info
->new_layout
;
1137 re
->after
.data_disks
= new_disks
;
1138 /* For RAID10 we don't do backup, and there is
1139 * no need to synchronise stripes on both
1140 * 'old' and 'new'. So the important
1141 * number is the minimum data_offset difference
1142 * which is the larger of (offset copies * chunk).
1145 re
->backup_blocks
= max(old_chunk
, new_chunk
) / 512;
1146 if (new_disks
< re
->before
.data_disks
&&
1147 info
->space_after
< re
->backup_blocks
)
1148 /* Reduce component size by one chunk */
1149 re
->new_size
= (info
->component_size
-
1152 re
->new_size
= info
->component_size
;
1153 re
->new_size
= re
->new_size
* new_disks
/ copies
;
1157 return "RAID10 can only be changed to RAID0";
1160 /* RAID0 can be converted to RAID10, or to RAID456 */
1161 if (info
->new_level
== 10) {
1162 if (info
->new_layout
== UnSet
&& info
->delta_disks
== UnSet
) {
1163 /* Assume near=2 layout */
1164 info
->new_layout
= 0x102;
1165 info
->delta_disks
= info
->array
.raid_disks
;
1167 if (info
->new_layout
== UnSet
) {
1168 int copies
= 1 + (info
->delta_disks
1169 / info
->array
.raid_disks
);
1170 if (info
->array
.raid_disks
* (copies
-1)
1171 != info
->delta_disks
)
1172 return "Impossible number of devices"
1173 " for RAID0->RAID10";
1174 info
->new_layout
= 0x100 + copies
;
1176 if (info
->delta_disks
== UnSet
) {
1177 int copies
= info
->new_layout
& 0xff;
1178 if (info
->new_layout
!= 0x100 + copies
)
1179 return "New layout impossible"
1180 " for RAID0->RAID10";;
1181 info
->delta_disks
= (copies
- 1) *
1182 info
->array
.raid_disks
;
1184 if (info
->new_chunk
&&
1185 info
->new_chunk
!= info
->array
.chunk_size
)
1186 return "Cannot change chunk-size with RAID0->RAID10";
1190 re
->before
.data_disks
= (info
->array
.raid_disks
+
1192 re
->after
.data_disks
= re
->before
.data_disks
;
1193 re
->before
.layout
= info
->new_layout
;
1194 re
->backup_blocks
= 0;
1198 /* RAID0 can also covert to RAID0/4/5/6 by first converting to
1199 * a raid4 style layout of the final level.
1201 switch (info
->new_level
) {
1206 re
->before
.layout
= 0;
1211 re
->before
.layout
= ALGORITHM_PARITY_N
;
1216 re
->before
.layout
= ALGORITHM_PARITY_N
;
1219 return "Impossible level change requested";
1221 re
->before
.data_disks
= info
->array
.raid_disks
;
1222 /* determining 'after' layout happens outside this 'switch' */
1226 info
->array
.layout
= ALGORITHM_PARITY_N
;
1228 switch (info
->new_level
) {
1232 re
->level
= info
->array
.level
;
1233 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1234 re
->before
.layout
= info
->array
.layout
;
1238 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1239 re
->before
.layout
= info
->array
.layout
;
1244 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1245 switch (info
->array
.layout
) {
1246 case ALGORITHM_LEFT_ASYMMETRIC
:
1247 re
->before
.layout
= ALGORITHM_LEFT_ASYMMETRIC_6
;
1249 case ALGORITHM_RIGHT_ASYMMETRIC
:
1250 re
->before
.layout
= ALGORITHM_RIGHT_ASYMMETRIC_6
;
1252 case ALGORITHM_LEFT_SYMMETRIC
:
1253 re
->before
.layout
= ALGORITHM_LEFT_SYMMETRIC_6
;
1255 case ALGORITHM_RIGHT_SYMMETRIC
:
1256 re
->before
.layout
= ALGORITHM_RIGHT_SYMMETRIC_6
;
1258 case ALGORITHM_PARITY_0
:
1259 re
->before
.layout
= ALGORITHM_PARITY_0_6
;
1261 case ALGORITHM_PARITY_N
:
1262 re
->before
.layout
= ALGORITHM_PARITY_N_6
;
1265 return "Cannot convert an array with this layout";
1269 if (info
->array
.raid_disks
!= 2)
1270 return "Can only convert a 2-device array to RAID1";
1271 if (info
->delta_disks
!= UnSet
&&
1272 info
->delta_disks
!= 0)
1273 return "Cannot set raid_disk when "
1274 "converting RAID5->RAID1";
1276 re
->backup_blocks
= 0;
1277 info
->new_chunk
= 0;
1280 return "Impossible level change requested";
1284 switch (info
->new_level
) {
1290 re
->before
.data_disks
= info
->array
.raid_disks
- 2;
1291 re
->before
.layout
= info
->array
.layout
;
1294 return "Impossible level change requested";
1299 /* If we reached here then it looks like a re-stripe is
1300 * happening. We have determined the intermediate level
1301 * and initial raid_disks/layout and stored these in 're'.
1303 * We need to deduce the final layout that can be atomically
1304 * converted to the end state.
1306 switch (info
->new_level
) {
1308 /* We can only get to RAID0 from RAID4 or RAID5
1309 * with appropriate layout and one extra device
1311 if (re
->level
!= 4 && re
->level
!= 5)
1312 return "Cannot covert to RAID0 from this level";
1314 switch (re
->level
) {
1316 re
->before
.layout
= 0;
1317 re
->after
.layout
= 0;
1320 re
->after
.layout
= ALGORITHM_PARITY_N
;
1326 /* We can only get to RAID4 from RAID5 */
1327 if (re
->level
!= 4 && re
->level
!= 5)
1328 return "Cannot convert to RAID4 from this level";
1330 switch (re
->level
) {
1332 re
->before
.layout
= 0;
1333 re
->after
.layout
= 0;
1336 re
->after
.layout
= ALGORITHM_PARITY_N
;
1342 /* We get to RAID5 from RAID5 or RAID6 */
1343 if (re
->level
!= 5 && re
->level
!= 6)
1344 return "Cannot convert to RAID5 from this level";
1346 switch (re
->level
) {
1348 if (info
->new_layout
== UnSet
)
1349 re
->after
.layout
= re
->before
.layout
;
1351 re
->after
.layout
= info
->new_layout
;
1354 if (info
->new_layout
== UnSet
)
1355 info
->new_layout
= re
->before
.layout
;
1357 /* after.layout needs to be raid6 version of new_layout */
1358 if (info
->new_layout
== ALGORITHM_PARITY_N
)
1359 re
->after
.layout
= ALGORITHM_PARITY_N
;
1362 char *ls
= map_num(r5layout
, info
->new_layout
);
1365 /* Current RAID6 layout has a RAID5
1368 strcat(strcpy(layout
, ls
), "-6");
1369 l
= map_name(r6layout
, layout
);
1371 return "Cannot find RAID6 layout"
1374 /* Current RAID6 has no equivalent.
1375 * If it is already a '-6' layout we
1376 * can leave it unchanged, else we must
1379 ls
= map_num(r6layout
, info
->new_layout
);
1381 strcmp(ls
+strlen(ls
)-2, "-6") != 0)
1382 return "Please specify new layout";
1383 l
= info
->new_layout
;
1385 re
->after
.layout
= l
;
1391 /* We must already be at level 6 */
1393 return "Impossible level change";
1394 if (info
->new_layout
== UnSet
)
1395 re
->after
.layout
= info
->array
.layout
;
1397 re
->after
.layout
= info
->new_layout
;
1400 return "Impossible level change requested";
1402 if (info
->delta_disks
== UnSet
)
1403 info
->delta_disks
= delta_parity
;
1405 re
->after
.data_disks
= (re
->before
.data_disks
1408 switch (re
->level
) {
1409 case 6: re
->parity
= 2;
1412 case 5: re
->parity
= 1;
1414 default: re
->parity
= 0;
1417 /* So we have a restripe operation, we need to calculate the number
1418 * of blocks per reshape operation.
1420 if (info
->new_chunk
== 0)
1421 info
->new_chunk
= info
->array
.chunk_size
;
1422 if (re
->after
.data_disks
== re
->before
.data_disks
&&
1423 re
->after
.layout
== re
->before
.layout
&&
1424 info
->new_chunk
== info
->array
.chunk_size
) {
1425 /* Nothing to change, can change level immediately. */
1426 re
->level
= info
->new_level
;
1427 re
->backup_blocks
= 0;
1430 if (re
->after
.data_disks
== 1 && re
->before
.data_disks
== 1) {
1431 /* chunk and layout changes make no difference */
1432 re
->level
= info
->new_level
;
1433 re
->backup_blocks
= 0;
1437 if (re
->after
.data_disks
== re
->before
.data_disks
&&
1438 get_linux_version() < 2006032)
1439 return "in-place reshape is not safe before 2.6.32 - sorry.";
1441 if (re
->after
.data_disks
< re
->before
.data_disks
&&
1442 get_linux_version() < 2006030)
1443 return "reshape to fewer devices is not supported before 2.6.30 - sorry.";
1445 re
->backup_blocks
= compute_backup_blocks(
1446 info
->new_chunk
, info
->array
.chunk_size
,
1447 re
->after
.data_disks
,
1448 re
->before
.data_disks
);
1450 re
->new_size
= info
->component_size
* re
->after
.data_disks
;
1454 static int set_array_size(struct supertype
*st
, struct mdinfo
*sra
,
1457 struct mdinfo
*info
;
1461 if ((st
== NULL
) || (sra
== NULL
))
1464 if (text_version
== NULL
)
1465 text_version
= sra
->text_version
;
1466 subarray
= strchr(text_version
+1, '/')+1;
1467 info
= st
->ss
->container_content(st
, subarray
);
1469 unsigned long long current_size
= 0;
1470 unsigned long long new_size
=
1471 info
->custom_array_size
/2;
1473 if (sysfs_get_ll(sra
, NULL
, "array_size", ¤t_size
) == 0 &&
1474 new_size
> current_size
) {
1475 if (sysfs_set_num(sra
, NULL
, "array_size", new_size
)
1477 dprintf("Error: Cannot set array size");
1480 dprintf("Array size changed");
1482 dprintf(" from %llu to %llu.\n",
1483 current_size
, new_size
);
1487 dprintf("Error: set_array_size(): info pointer in NULL\n");
1492 static int reshape_array(char *container
, int fd
, char *devname
,
1493 struct supertype
*st
, struct mdinfo
*info
,
1494 int force
, struct mddev_dev
*devlist
,
1495 unsigned long long data_offset
,
1496 char *backup_file
, int verbose
, int forked
,
1497 int restart
, int freeze_reshape
);
1498 static int reshape_container(char *container
, char *devname
,
1500 struct supertype
*st
,
1501 struct mdinfo
*info
,
1504 int verbose
, int restart
, int freeze_reshape
);
1506 int Grow_reshape(char *devname
, int fd
,
1507 struct mddev_dev
*devlist
,
1508 unsigned long long data_offset
,
1509 struct context
*c
, struct shape
*s
)
1511 /* Make some changes in the shape of an array.
1512 * The kernel must support the change.
1514 * There are three different changes. Each can trigger
1515 * a resync or recovery so we freeze that until we have
1516 * requested everything (if kernel supports freezing - 2.6.30).
1518 * - change size (i.e. component_size)
1520 * - change layout/chunksize/ndisks
1522 * The last can require a reshape. It is different on different
1523 * levels so we need to check the level before actioning it.
1524 * Some times the level change needs to be requested after the
1525 * reshape (e.g. raid6->raid5, raid5->raid0)
1528 struct mdu_array_info_s array
;
1530 struct supertype
*st
;
1531 char *subarray
= NULL
;
1535 char *container
= NULL
;
1538 struct mddev_dev
*dv
;
1545 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) < 0) {
1546 pr_err("%s is not an active md array - aborting\n",
1550 if (data_offset
!= INVALID_SECTORS
&& array
.level
!= 10) {
1551 pr_err("--grow --data-offset not yet supported\n");
1556 (s
->chunk
|| s
->level
!= UnSet
|| s
->layout_str
|| s
->raiddisks
)) {
1557 pr_err("cannot change component size at the same time "
1558 "as other changes.\n"
1559 " Change size first, then check data is intact before "
1560 "making other changes.\n");
1564 if (s
->raiddisks
&& s
->raiddisks
< array
.raid_disks
&& array
.level
> 1 &&
1565 get_linux_version() < 2006032 &&
1566 !check_env("MDADM_FORCE_FEWER")) {
1567 pr_err("reducing the number of devices is not safe before Linux 2.6.32\n"
1568 " Please use a newer kernel\n");
1572 st
= super_by_fd(fd
, &subarray
);
1574 pr_err("Unable to determine metadata format for %s\n", devname
);
1577 if (s
->raiddisks
> st
->max_devs
) {
1578 pr_err("Cannot increase raid-disks on this array"
1579 " beyond %d\n", st
->max_devs
);
1583 /* in the external case we need to check that the requested reshape is
1584 * supported, and perform an initial check that the container holds the
1585 * pre-requisite spare devices (mdmon owns final validation)
1587 if (st
->ss
->external
) {
1591 container
= st
->container_devnm
;
1592 cfd
= open_dev_excl(st
->container_devnm
);
1594 container
= st
->devnm
;
1596 cfd
= open_dev_excl(st
->devnm
);
1600 pr_err("Unable to open container for %s\n",
1606 rv
= st
->ss
->load_container(st
, cfd
, NULL
);
1609 pr_err("Cannot read superblock for %s\n",
1615 /* check if operation is supported for metadata handler */
1616 if (st
->ss
->container_content
) {
1617 struct mdinfo
*cc
= NULL
;
1618 struct mdinfo
*content
= NULL
;
1620 cc
= st
->ss
->container_content(st
, subarray
);
1621 for (content
= cc
; content
; content
= content
->next
) {
1622 int allow_reshape
= 1;
1624 /* check if reshape is allowed based on metadata
1625 * indications stored in content.array.status
1627 if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
))
1629 if (content
->array
.state
1630 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
))
1632 if (!allow_reshape
) {
1633 pr_err("cannot reshape arrays in"
1634 " container with unsupported"
1635 " metadata: %s(%s)\n",
1636 devname
, container
);
1644 if (mdmon_running(container
))
1645 st
->update_tail
= &st
->updates
;
1649 for (dv
= devlist
; dv
; dv
= dv
->next
)
1651 if (s
->raiddisks
> array
.raid_disks
&&
1652 array
.spare_disks
+added_disks
< (s
->raiddisks
- array
.raid_disks
) &&
1654 pr_err("Need %d spare%s to avoid degraded array,"
1655 " and only have %d.\n"
1656 " Use --force to over-ride this check.\n",
1657 s
->raiddisks
- array
.raid_disks
,
1658 s
->raiddisks
- array
.raid_disks
== 1 ? "" : "s",
1659 array
.spare_disks
+ added_disks
);
1663 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
| GET_DISKS
| GET_DEVS
1664 | GET_STATE
| GET_VERSION
);
1666 if (st
->ss
->external
&& subarray
== NULL
) {
1667 array
.level
= LEVEL_CONTAINER
;
1668 sra
->array
.level
= LEVEL_CONTAINER
;
1671 pr_err("failed to read sysfs parameters for %s\n",
1675 frozen
= freeze(st
);
1677 /* freeze() already spewed the reason */
1680 } else if (frozen
< 0) {
1681 pr_err("%s is performing resync/recovery and cannot"
1682 " be reshaped\n", devname
);
1687 /* ========= set size =============== */
1688 if (s
->size
> 0 && (s
->size
== MAX_SIZE
|| s
->size
!= (unsigned)array
.size
)) {
1689 unsigned long long orig_size
= get_component_size(fd
)/2;
1690 unsigned long long min_csize
;
1692 int raid0_takeover
= 0;
1695 orig_size
= (unsigned) array
.size
;
1697 if (orig_size
== 0) {
1698 pr_err("Cannot set device size in this type of array.\n");
1703 if (reshape_super(st
, s
->size
, UnSet
, UnSet
, 0, 0, UnSet
, NULL
,
1704 devname
, APPLY_METADATA_CHANGES
, c
->verbose
> 0)) {
1709 if (st
->ss
->external
) {
1710 /* metadata can have size limitation
1711 * update size value according to metadata information
1713 struct mdinfo
*sizeinfo
=
1714 st
->ss
->container_content(st
, subarray
);
1716 unsigned long long new_size
=
1717 sizeinfo
->custom_array_size
/2;
1718 int data_disks
= get_data_disks(
1719 sizeinfo
->array
.level
,
1720 sizeinfo
->array
.layout
,
1721 sizeinfo
->array
.raid_disks
);
1722 new_size
/= data_disks
;
1723 dprintf("Metadata size correction from %llu to "
1724 "%llu (%llu)\n", orig_size
, new_size
,
1725 new_size
* data_disks
);
1727 sysfs_free(sizeinfo
);
1731 /* Update the size of each member device in case
1732 * they have been resized. This will never reduce
1733 * below the current used-size. The "size" attribute
1734 * understands '0' to mean 'max'.
1738 for (mdi
= sra
->devs
; mdi
; mdi
= mdi
->next
) {
1739 if (sysfs_set_num(sra
, mdi
, "size",
1740 s
->size
== MAX_SIZE
? 0 : s
->size
) < 0) {
1741 /* Probably kernel refusing to let us
1742 * reduce the size - not an error.
1746 if (array
.not_persistent
== 0 &&
1747 array
.major_version
== 0 &&
1748 get_linux_version() < 3001000) {
1749 /* Dangerous to allow size to exceed 2TB */
1750 unsigned long long csize
;
1751 if (sysfs_get_ll(sra
, mdi
, "size", &csize
) == 0) {
1752 if (csize
>= 2ULL*1024*1024*1024)
1753 csize
= 2ULL*1024*1024*1024;
1754 if ((min_csize
== 0 || (min_csize
1761 pr_err("Cannot set size on "
1762 "array members.\n");
1763 goto size_change_error
;
1765 if (min_csize
&& s
->size
> min_csize
) {
1766 pr_err("Cannot safely make this array "
1767 "use more than 2TB per device on this kernel.\n");
1769 goto size_change_error
;
1771 if (min_csize
&& s
->size
== MAX_SIZE
) {
1772 /* Don't let the kernel choose a size - it will get
1775 pr_err("Limited v0.90 array to "
1776 "2TB per device\n");
1777 s
->size
= min_csize
;
1779 if (st
->ss
->external
) {
1780 if (sra
->array
.level
== 0) {
1781 rv
= sysfs_set_str(sra
, NULL
, "level",
1785 /* get array parametes after takeover
1786 * to chane one parameter at time only
1788 rv
= ioctl(fd
, GET_ARRAY_INFO
, &array
);
1791 /* make sure mdmon is
1792 * aware of the new level */
1793 if (!mdmon_running(st
->container_devnm
))
1794 start_mdmon(st
->container_devnm
);
1795 ping_monitor(container
);
1796 if (mdmon_running(st
->container_devnm
) &&
1797 st
->update_tail
== NULL
)
1798 st
->update_tail
= &st
->updates
;
1801 if (s
->size
== MAX_SIZE
)
1803 array
.size
= s
->size
;
1804 if ((unsigned)array
.size
!= s
->size
) {
1805 /* got truncated to 32bit, write to
1806 * component_size instead
1809 rv
= sysfs_set_num(sra
, NULL
,
1810 "component_size", s
->size
);
1814 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
1816 /* manage array size when it is managed externally
1818 if ((rv
== 0) && st
->ss
->external
)
1819 rv
= set_array_size(st
, sra
, sra
->text_version
);
1822 if (raid0_takeover
) {
1823 /* do not recync non-existing parity,
1824 * we will drop it anyway
1826 sysfs_set_str(sra
, NULL
, "sync_action", "frozen");
1827 /* go back to raid0, drop parity disk
1829 sysfs_set_str(sra
, NULL
, "level", "raid0");
1830 ioctl(fd
, GET_ARRAY_INFO
, &array
);
1837 /* restore metadata */
1838 if (reshape_super(st
, orig_size
, UnSet
, UnSet
, 0, 0,
1839 UnSet
, NULL
, devname
,
1840 ROLLBACK_METADATA_CHANGES
,
1843 pr_err("Cannot set device size for %s: %s\n",
1844 devname
, strerror(err
));
1846 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1847 cont_err("Bitmap must be removed before size can be changed\n");
1851 if (s
->assume_clean
) {
1852 /* This will fail on kernels older than 3.0 unless
1853 * a backport has been arranged.
1856 sysfs_set_str(sra
, NULL
, "resync_start", "none") < 0)
1857 pr_err("--assume-clean not supported with --grow on this kernel\n");
1859 ioctl(fd
, GET_ARRAY_INFO
, &array
);
1860 s
->size
= get_component_size(fd
)/2;
1862 s
->size
= array
.size
;
1863 if (c
->verbose
>= 0) {
1864 if (s
->size
== orig_size
)
1865 pr_err("component size of %s "
1866 "unchanged at %lluK\n",
1869 pr_err("component size of %s "
1870 "has been set to %lluK\n",
1874 } else if (array
.level
!= LEVEL_CONTAINER
) {
1875 s
->size
= get_component_size(fd
)/2;
1877 s
->size
= array
.size
;
1880 /* See if there is anything else to do */
1881 if ((s
->level
== UnSet
|| s
->level
== array
.level
) &&
1882 (s
->layout_str
== NULL
) &&
1883 (s
->chunk
== 0 || s
->chunk
== array
.chunk_size
) &&
1884 (s
->raiddisks
== 0 || s
->raiddisks
== array
.raid_disks
)) {
1885 /* Nothing more to do */
1886 if (!changed
&& c
->verbose
>= 0)
1887 pr_err("%s: no change requested\n",
1892 /* ========= check for Raid10/Raid1 -> Raid0 conversion ===============
1893 * current implementation assumes that following conditions must be met:
1896 * - near_copies == 2
1898 if ((s
->level
== 0 && array
.level
== 10 && sra
&&
1899 array
.layout
== ((1 << 8) + 2) && !(array
.raid_disks
& 1)) ||
1900 (s
->level
== 0 && array
.level
== 1 && sra
)) {
1902 err
= remove_disks_for_takeover(st
, sra
, array
.layout
);
1904 dprintf(Name
": Array cannot be reshaped\n");
1910 /* Make sure mdmon has seen the device removal
1911 * and updated metadata before we continue with
1915 ping_monitor(container
);
1918 memset(&info
, 0, sizeof(info
));
1920 sysfs_init(&info
, fd
, NULL
);
1921 strcpy(info
.text_version
, sra
->text_version
);
1922 info
.component_size
= s
->size
*2;
1923 info
.new_level
= s
->level
;
1924 info
.new_chunk
= s
->chunk
* 1024;
1925 if (info
.array
.level
== LEVEL_CONTAINER
) {
1926 info
.delta_disks
= UnSet
;
1927 info
.array
.raid_disks
= s
->raiddisks
;
1928 } else if (s
->raiddisks
)
1929 info
.delta_disks
= s
->raiddisks
- info
.array
.raid_disks
;
1931 info
.delta_disks
= UnSet
;
1932 if (s
->layout_str
== NULL
) {
1933 info
.new_layout
= UnSet
;
1934 if (info
.array
.level
== 6 &&
1935 (info
.new_level
== 6 || info
.new_level
== UnSet
) &&
1936 info
.array
.layout
>= 16) {
1937 pr_err("%s has a non-standard layout. If you"
1938 " wish to preserve this\n", devname
);
1939 cont_err("during the reshape, please specify"
1940 " --layout=preserve\n");
1941 cont_err("If you want to change it, specify a"
1942 " layout or use --layout=normalise\n");
1946 } else if (strcmp(s
->layout_str
, "normalise") == 0 ||
1947 strcmp(s
->layout_str
, "normalize") == 0) {
1948 /* If we have a -6 RAID6 layout, remove the '-6'. */
1949 info
.new_layout
= UnSet
;
1950 if (info
.array
.level
== 6 && info
.new_level
== UnSet
) {
1952 strcpy(l
, map_num(r6layout
, info
.array
.layout
));
1953 h
= strrchr(l
, '-');
1954 if (h
&& strcmp(h
, "-6") == 0) {
1956 info
.new_layout
= map_name(r6layout
, l
);
1959 pr_err("%s is only meaningful when reshaping"
1960 " a RAID6 array.\n", s
->layout_str
);
1964 } else if (strcmp(s
->layout_str
, "preserve") == 0) {
1965 /* This means that a non-standard RAID6 layout
1968 * - When reshape a RAID6 (e.g. adding a device)
1969 * which is in a non-standard layout, it is OK
1970 * to preserve that layout.
1971 * - When converting a RAID5 to RAID6, leave it in
1972 * the XXX-6 layout, don't re-layout.
1974 if (info
.array
.level
== 6 && info
.new_level
== UnSet
)
1975 info
.new_layout
= info
.array
.layout
;
1976 else if (info
.array
.level
== 5 && info
.new_level
== 6) {
1978 strcpy(l
, map_num(r5layout
, info
.array
.layout
));
1980 info
.new_layout
= map_name(r6layout
, l
);
1982 pr_err("%s in only meaningful when reshaping"
1983 " to RAID6\n", s
->layout_str
);
1988 int l
= info
.new_level
;
1990 l
= info
.array
.level
;
1993 info
.new_layout
= map_name(r5layout
, s
->layout_str
);
1996 info
.new_layout
= map_name(r6layout
, s
->layout_str
);
1999 info
.new_layout
= parse_layout_10(s
->layout_str
);
2002 info
.new_layout
= parse_layout_faulty(s
->layout_str
);
2005 pr_err("layout not meaningful"
2006 " with this level\n");
2010 if (info
.new_layout
== UnSet
) {
2011 pr_err("layout %s not understood"
2012 " for this level\n",
2019 if (array
.level
== LEVEL_FAULTY
) {
2020 if (s
->level
!= UnSet
&& s
->level
!= array
.level
) {
2021 pr_err("cannot change level of Faulty device\n");
2025 pr_err("cannot set chunksize of Faulty device\n");
2028 if (s
->raiddisks
&& s
->raiddisks
!= 1) {
2029 pr_err("cannot set raid_disks of Faulty device\n");
2032 if (s
->layout_str
) {
2033 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
2034 dprintf("Cannot get array information.\n");
2037 array
.layout
= info
.new_layout
;
2038 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2039 pr_err("failed to set new layout\n");
2041 } else if (c
->verbose
>= 0)
2042 printf("layout for %s set to %d\n",
2043 devname
, array
.layout
);
2045 } else if (array
.level
== LEVEL_CONTAINER
) {
2046 /* This change is to be applied to every array in the
2047 * container. This is only needed when the metadata imposes
2048 * restraints of the various arrays in the container.
2049 * Currently we only know that IMSM requires all arrays
2050 * to have the same number of devices so changing the
2051 * number of devices (On-Line Capacity Expansion) must be
2052 * performed at the level of the container
2054 rv
= reshape_container(container
, devname
, -1, st
, &info
,
2055 c
->force
, c
->backup_file
, c
->verbose
, 0, 0);
2058 /* get spare devices from external metadata
2060 if (st
->ss
->external
) {
2061 struct mdinfo
*info2
;
2063 info2
= st
->ss
->container_content(st
, subarray
);
2065 info
.array
.spare_disks
=
2066 info2
->array
.spare_disks
;
2071 /* Impose these changes on a single array. First
2072 * check that the metadata is OK with the change. */
2074 if (reshape_super(st
, 0, info
.new_level
,
2075 info
.new_layout
, info
.new_chunk
,
2076 info
.array
.raid_disks
, info
.delta_disks
,
2077 c
->backup_file
, devname
, APPLY_METADATA_CHANGES
,
2083 rv
= reshape_array(container
, fd
, devname
, st
, &info
, c
->force
,
2084 devlist
, data_offset
, c
->backup_file
, c
->verbose
,
2095 /* verify_reshape_position()
2096 * Function checks if reshape position in metadata is not farther
2097 * than position in md.
2099 * 0 : not valid sysfs entry
2100 * it can be caused by not started reshape, it should be started
2101 * by reshape array or raid0 array is before takeover
2102 * -1 : error, reshape position is obviously wrong
2103 * 1 : success, reshape progress correct or updated
2105 static int verify_reshape_position(struct mdinfo
*info
, int level
)
2111 /* read sync_max, failure can mean raid0 array */
2112 rv
= sysfs_get_str(info
, NULL
, "sync_max", buf
, 40);
2116 unsigned long long position
= strtoull(buf
, &ep
, 0);
2118 dprintf(Name
": Read sync_max sysfs entry is: %s\n", buf
);
2119 if (!(ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))) {
2120 position
*= get_data_disks(level
,
2122 info
->array
.raid_disks
);
2123 if (info
->reshape_progress
< position
) {
2124 dprintf("Corrected reshape progress (%llu) to "
2125 "md position (%llu)\n",
2126 info
->reshape_progress
, position
);
2127 info
->reshape_progress
= position
;
2129 } else if (info
->reshape_progress
> position
) {
2130 pr_err("Fatal error: array "
2131 "reshape was not properly frozen "
2132 "(expected reshape position is %llu, "
2133 "but reshape progress is %llu.\n",
2134 position
, info
->reshape_progress
);
2137 dprintf("Reshape position in md and metadata "
2142 } else if (rv
== 0) {
2143 /* for valid sysfs entry, 0-length content
2144 * should be indicated as error
2152 static int set_new_data_offset(struct mdinfo
*sra
, struct supertype
*st
,
2153 char *devname
, int delta_disks
,
2154 unsigned long long data_offset
,
2155 unsigned long long min
)
2161 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2165 struct supertype
*st2
;
2166 struct mdinfo info2
;
2168 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2170 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2171 dfd
= dev_open(dn
, O_RDONLY
);
2173 pr_err("%s: cannot open component %s\n",
2174 devname
, dn
? dn
: "-unknown-");
2177 st2
= dup_super(st
);
2178 rv
= st2
->ss
->load_super(st2
,dfd
, NULL
);
2182 pr_err("%s: cannot get superblock from %s\n",
2186 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
2187 st2
->ss
->free_super(st2
);
2189 if (delta_disks
< 0) {
2190 /* Don't need any space as array is shrinking
2191 * just move data_offset up by min
2193 if (data_offset
== INVALID_SECTORS
)
2194 info2
.new_data_offset
= info2
.data_offset
+ min
;
2196 if (data_offset
< info2
.data_offset
+ min
) {
2197 pr_err("--data-offset too small for %s\n",
2201 info2
.new_data_offset
= data_offset
;
2203 } else if (delta_disks
> 0) {
2204 /* need space before */
2205 if (info2
.space_before
< min
) {
2206 pr_err("Insufficient head-space for reshape on %s\n",
2210 if (data_offset
== INVALID_SECTORS
)
2211 info2
.new_data_offset
= info2
.data_offset
- min
;
2213 if (data_offset
> info2
.data_offset
- min
) {
2214 pr_err("--data-offset too large for %s\n",
2218 info2
.new_data_offset
= data_offset
;
2222 /* can move up or down. 'data_offset'
2223 * might guide us, otherwise choose
2224 * direction with most space
2226 if (data_offset
== INVALID_SECTORS
) {
2227 if (info2
.space_before
> info2
.space_after
)
2231 } else if (data_offset
< info2
.data_offset
)
2235 sysfs_set_str(sra
, NULL
, "reshape_direction",
2236 dir
== 1 ? "backwards" : "forwards");
2239 case 1: /* Increase data offset */
2240 if (info2
.space_after
< min
) {
2241 pr_err("Insufficient tail-space for reshape on %s\n",
2245 if (data_offset
!= INVALID_SECTORS
&&
2246 data_offset
< info2
.data_offset
+ min
) {
2247 pr_err("--data-offset too small on %s\n",
2251 if (data_offset
!= INVALID_SECTORS
)
2252 info2
.new_data_offset
= data_offset
;
2254 unsigned long long off
=
2255 info2
.space_after
/ 2;
2259 info2
.new_data_offset
=
2260 info2
.data_offset
+ off
;
2263 case -1: /* Decrease data offset */
2264 if (info2
.space_before
< min
) {
2265 pr_err("insufficient head-room on %s\n",
2269 if (data_offset
!= INVALID_SECTORS
&&
2270 data_offset
< info2
.data_offset
- min
) {
2271 pr_err("--data-offset too small on %s\n",
2275 if (data_offset
!= INVALID_SECTORS
)
2276 info2
.new_data_offset
= data_offset
;
2278 unsigned long long off
=
2279 info2
.space_before
/ 2;
2283 info2
.new_data_offset
=
2284 info2
.data_offset
- off
;
2289 if (sysfs_set_num(sra
, sd
, "new_offset",
2290 info2
.new_data_offset
) < 0) {
2292 if (sd
== sra
->devs
&& err
== ENOENT
)
2293 /* Early kernel, no 'new_offset' file.
2294 * For RAID5/6 this is not fatal
2297 pr_err("Cannot set new_offset for %s\n",
2307 static int raid10_reshape(char *container
, int fd
, char *devname
,
2308 struct supertype
*st
, struct mdinfo
*info
,
2309 struct reshape
*reshape
,
2310 unsigned long long data_offset
,
2311 int force
, int verbose
)
2313 /* Changing raid_disks, layout, chunksize or possibly
2314 * just data_offset for a RAID10.
2315 * We must always change data_offset. We change by at least
2316 * ->backup_blocks which is the largest of the old and new
2318 * If raid_disks is increasing, then data_offset must decrease
2319 * by at least this copy size.
2320 * If raid_disks is unchanged, data_offset must increase or
2321 * decrease by at least backup_blocks but preferably by much more.
2322 * We choose half of the available space.
2323 * If raid_disks is decreasing, data_offset must increase by
2324 * at least backup_blocks. To allow of this, component_size
2325 * must be decreased by the same amount.
2327 * So we calculate the required minimum and direction, possibly
2328 * reduce the component_size, then iterate through the devices
2329 * and set the new_data_offset.
2330 * If that all works, we set chunk_size, layout, raid_disks, and start
2334 unsigned long long min
;
2337 sra
= sysfs_read(fd
, NULL
,
2338 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
2341 pr_err("%s: Cannot get array details from sysfs\n",
2345 min
= reshape
->backup_blocks
;
2347 if (info
->delta_disks
)
2348 sysfs_set_str(sra
, NULL
, "reshape_direction",
2349 info
->delta_disks
< 0 ? "backwards" : "forwards");
2350 if (info
->delta_disks
< 0 &&
2351 info
->space_after
< reshape
->backup_blocks
) {
2352 int rv
= sysfs_set_num(sra
, NULL
, "component_size",
2353 (sra
->component_size
-
2354 reshape
->backup_blocks
)/2);
2356 pr_err("cannot reduce component size\n");
2360 err
= set_new_data_offset(sra
, st
, devname
, info
->delta_disks
, data_offset
,
2363 pr_err("Cannot set new_data_offset: RAID10 reshape not\n");
2364 cont_err("supported on this kernel\n");
2370 if (!err
&& sysfs_set_num(sra
, NULL
, "chunk_size", info
->new_chunk
) < 0)
2372 if (!err
&& sysfs_set_num(sra
, NULL
, "layout", reshape
->after
.layout
) < 0)
2374 if (!err
&& sysfs_set_num(sra
, NULL
, "raid_disks",
2375 info
->array
.raid_disks
+ info
->delta_disks
) < 0)
2377 if (!err
&& sysfs_set_str(sra
, NULL
, "sync_action", "reshape") < 0)
2380 pr_err("Cannot set array shape for %s\n",
2383 (info
->array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2384 cont_err(" Bitmap must be removed before"
2385 " shape can be changed\n");
2395 static void get_space_after(int fd
, struct supertype
*st
, struct mdinfo
*info
)
2397 struct mdinfo
*sra
, *sd
;
2398 /* Initialisation to silence compiler warning */
2399 unsigned long long min_space_before
= 0, min_space_after
= 0;
2402 sra
= sysfs_read(fd
, NULL
, GET_DEVS
);
2405 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2408 struct supertype
*st2
;
2409 struct mdinfo info2
;
2411 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2413 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2414 dfd
= dev_open(dn
, O_RDONLY
);
2417 st2
= dup_super(st
);
2418 if (st2
->ss
->load_super(st2
,dfd
, NULL
)) {
2424 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
2425 st2
->ss
->free_super(st2
);
2428 min_space_before
> info2
.space_before
)
2429 min_space_before
= info2
.space_before
;
2431 min_space_after
> info2
.space_after
)
2432 min_space_after
= info2
.space_after
;
2435 if (sd
== NULL
&& !first
) {
2436 info
->space_after
= min_space_after
;
2437 info
->space_before
= min_space_before
;
2442 static void update_cache_size(char *container
, struct mdinfo
*sra
,
2443 struct mdinfo
*info
,
2444 int disks
, unsigned long long blocks
)
2446 /* Check that the internal stripe cache is
2447 * large enough, or it won't work.
2448 * It must hold at least 4 stripes of the larger
2451 unsigned long cache
;
2452 cache
= max(info
->array
.chunk_size
, info
->new_chunk
);
2453 cache
*= 4; /* 4 stripes minimum */
2454 cache
/= 512; /* convert to sectors */
2455 /* make sure there is room for 'blocks' with a bit to spare */
2456 if (cache
< 16 + blocks
/ disks
)
2457 cache
= 16 + blocks
/ disks
;
2458 cache
/= (4096/512); /* Covert from sectors to pages */
2460 if (sra
->cache_size
< cache
)
2461 subarray_set_num(container
, sra
, "stripe_cache_size",
2465 static int impose_reshape(struct mdinfo
*sra
,
2466 struct mdinfo
*info
,
2467 struct supertype
*st
,
2470 char *devname
, char *container
,
2471 struct reshape
*reshape
)
2473 struct mdu_array_info_s array
;
2475 sra
->new_chunk
= info
->new_chunk
;
2478 /* for external metadata checkpoint saved by mdmon can be lost
2479 * or missed /due to e.g. crash/. Check if md is not during
2480 * restart farther than metadata points to.
2481 * If so, this means metadata information is obsolete.
2483 if (st
->ss
->external
)
2484 verify_reshape_position(info
, reshape
->level
);
2485 sra
->reshape_progress
= info
->reshape_progress
;
2487 sra
->reshape_progress
= 0;
2488 if (reshape
->after
.data_disks
< reshape
->before
.data_disks
)
2489 /* start from the end of the new array */
2490 sra
->reshape_progress
= (sra
->component_size
2491 * reshape
->after
.data_disks
);
2494 ioctl(fd
, GET_ARRAY_INFO
, &array
);
2495 if (info
->array
.chunk_size
== info
->new_chunk
&&
2496 reshape
->before
.layout
== reshape
->after
.layout
&&
2497 st
->ss
->external
== 0) {
2498 /* use SET_ARRAY_INFO but only if reshape hasn't started */
2499 array
.raid_disks
= reshape
->after
.data_disks
+ reshape
->parity
;
2501 ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2504 pr_err("Cannot set device shape for %s: %s\n",
2505 devname
, strerror(errno
));
2508 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2509 cont_err("Bitmap must be removed before"
2510 " shape can be changed\n");
2514 } else if (!restart
) {
2515 /* set them all just in case some old 'new_*' value
2516 * persists from some earlier problem.
2519 if (sysfs_set_num(sra
, NULL
, "chunk_size", info
->new_chunk
) < 0)
2521 if (!err
&& sysfs_set_num(sra
, NULL
, "layout",
2522 reshape
->after
.layout
) < 0)
2524 if (!err
&& subarray_set_num(container
, sra
, "raid_disks",
2525 reshape
->after
.data_disks
+
2526 reshape
->parity
) < 0)
2529 pr_err("Cannot set device shape for %s\n",
2533 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2534 cont_err("Bitmap must be removed before"
2535 " shape can be changed\n");
2544 static int reshape_array(char *container
, int fd
, char *devname
,
2545 struct supertype
*st
, struct mdinfo
*info
,
2546 int force
, struct mddev_dev
*devlist
,
2547 unsigned long long data_offset
,
2548 char *backup_file
, int verbose
, int forked
,
2549 int restart
, int freeze_reshape
)
2551 struct reshape reshape
;
2554 int orig_level
= UnSet
;
2558 struct mdu_array_info_s array
;
2561 struct mddev_dev
*dv
;
2565 unsigned long long *offsets
= NULL
;
2569 unsigned long blocks
;
2570 unsigned long long array_size
;
2572 struct mdinfo
*sra
= NULL
;
2574 /* when reshaping a RAID0, the component_size might be zero.
2575 * So try to fix that up.
2577 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
2578 dprintf("Cannot get array information.\n");
2581 if (array
.level
== 0 && info
->component_size
== 0) {
2582 get_dev_size(fd
, NULL
, &array_size
);
2583 info
->component_size
= array_size
/ array
.raid_disks
;
2586 if (array
.level
== 10)
2587 /* Need space_after info */
2588 get_space_after(fd
, st
, info
);
2590 if (info
->reshape_active
) {
2591 int new_level
= info
->new_level
;
2592 info
->new_level
= UnSet
;
2593 if (info
->delta_disks
> 0)
2594 info
->array
.raid_disks
-= info
->delta_disks
;
2595 msg
= analyse_change(info
, &reshape
);
2596 info
->new_level
= new_level
;
2597 if (info
->delta_disks
> 0)
2598 info
->array
.raid_disks
+= info
->delta_disks
;
2600 /* Make sure the array isn't read-only */
2601 ioctl(fd
, RESTART_ARRAY_RW
, 0);
2603 msg
= analyse_change(info
, &reshape
);
2605 pr_err("%s\n", msg
);
2609 (reshape
.level
!= info
->array
.level
||
2610 reshape
.before
.layout
!= info
->array
.layout
||
2611 reshape
.before
.data_disks
+ reshape
.parity
2612 != info
->array
.raid_disks
- max(0, info
->delta_disks
))) {
2613 pr_err("reshape info is not in native format -"
2614 " cannot continue.\n");
2618 if (st
->ss
->external
&& restart
&& (info
->reshape_progress
== 0)) {
2619 /* When reshape is restarted from '0', very begin of array
2620 * it is possible that for external metadata reshape and array
2621 * configuration doesn't happen.
2622 * Check if md has the same opinion, and reshape is restarted
2623 * from 0. If so, this is regular reshape start after reshape
2624 * switch in metadata to next array only.
2626 if ((verify_reshape_position(info
, reshape
.level
) >= 0) &&
2627 (info
->reshape_progress
== 0))
2631 /* reshape already started. just skip to monitoring the reshape */
2632 if (reshape
.backup_blocks
== 0)
2636 /* The container is frozen but the array may not be.
2637 * So freeze the array so spares don't get put to the wrong use
2638 * FIXME there should probably be a cleaner separation between
2639 * freeze_array and freeze_container.
2641 sysfs_freeze_array(info
);
2642 /* Check we have enough spares to not be degraded */
2644 for (dv
= devlist
; dv
; dv
=dv
->next
)
2646 spares_needed
= max(reshape
.before
.data_disks
,
2647 reshape
.after
.data_disks
)
2648 + reshape
.parity
- array
.raid_disks
;
2651 info
->new_level
> 1 && info
->array
.level
> 1 &&
2652 spares_needed
> info
->array
.spare_disks
+ added_disks
) {
2653 pr_err("Need %d spare%s to avoid degraded array,"
2654 " and only have %d.\n"
2655 " Use --force to over-ride this check.\n",
2657 spares_needed
== 1 ? "" : "s",
2658 info
->array
.spare_disks
+ added_disks
);
2661 /* Check we have enough spares to not fail */
2662 spares_needed
= max(reshape
.before
.data_disks
,
2663 reshape
.after
.data_disks
)
2665 if ((info
->new_level
> 1 || info
->new_level
== 0) &&
2666 spares_needed
> info
->array
.spare_disks
+added_disks
) {
2667 pr_err("Need %d spare%s to create working array,"
2668 " and only have %d.\n",
2670 spares_needed
== 1 ? "" : "s",
2671 info
->array
.spare_disks
+ added_disks
);
2675 if (reshape
.level
!= array
.level
) {
2676 char *c
= map_num(pers
, reshape
.level
);
2681 err
= sysfs_set_str(info
, NULL
, "level", c
);
2684 pr_err("%s: could not set level to %s\n",
2687 (info
->array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2688 cont_err("Bitmap must be removed"
2689 " before level can be changed\n");
2693 pr_err("level of %s changed to %s\n",
2695 orig_level
= array
.level
;
2696 sysfs_freeze_array(info
);
2698 if (reshape
.level
> 0 && st
->ss
->external
) {
2699 /* make sure mdmon is aware of the new level */
2700 if (mdmon_running(container
))
2701 flush_mdmon(container
);
2703 if (!mdmon_running(container
))
2704 start_mdmon(container
);
2705 ping_monitor(container
);
2706 if (mdmon_running(container
) &&
2707 st
->update_tail
== NULL
)
2708 st
->update_tail
= &st
->updates
;
2711 /* ->reshape_super might have chosen some spares from the
2712 * container that it wants to be part of the new array.
2713 * We can collect them with ->container_content and give
2714 * them to the kernel.
2716 if (st
->ss
->reshape_super
&& st
->ss
->container_content
) {
2717 char *subarray
= strchr(info
->text_version
+1, '/')+1;
2718 struct mdinfo
*info2
=
2719 st
->ss
->container_content(st
, subarray
);
2723 sysfs_init(info2
, fd
, st
->devnm
);
2724 /* When increasing number of devices, we need to set
2725 * new raid_disks before adding these, or they might
2728 if (reshape
.backup_blocks
&&
2729 reshape
.after
.data_disks
> reshape
.before
.data_disks
)
2730 subarray_set_num(container
, info2
, "raid_disks",
2731 reshape
.after
.data_disks
+
2733 for (d
= info2
->devs
; d
; d
= d
->next
) {
2734 if (d
->disk
.state
== 0 &&
2735 d
->disk
.raid_disk
>= 0) {
2736 /* This is a spare that wants to
2737 * be part of the array.
2739 add_disk(fd
, st
, info2
, d
);
2745 /* We might have been given some devices to add to the
2746 * array. Now that the array has been changed to the right
2747 * level and frozen, we can safely add them.
2750 Manage_subdevs(devname
, fd
, devlist
, verbose
,
2753 if (reshape
.backup_blocks
== 0) {
2754 /* No restriping needed, but we might need to impose
2755 * some more changes: layout, raid_disks, chunk_size
2757 /* read current array info */
2758 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
2759 dprintf("Cannot get array information.\n");
2762 /* compare current array info with new values and if
2763 * it is different update them to new */
2764 if (info
->new_layout
!= UnSet
&&
2765 info
->new_layout
!= array
.layout
) {
2766 array
.layout
= info
->new_layout
;
2767 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2768 pr_err("failed to set new layout\n");
2770 } else if (verbose
>= 0)
2771 printf("layout for %s set to %d\n",
2772 devname
, array
.layout
);
2774 if (info
->delta_disks
!= UnSet
&&
2775 info
->delta_disks
!= 0 &&
2776 array
.raid_disks
!= (info
->array
.raid_disks
+ info
->delta_disks
)) {
2777 array
.raid_disks
+= info
->delta_disks
;
2778 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2779 pr_err("failed to set raid disks\n");
2781 } else if (verbose
>= 0) {
2782 printf("raid_disks for %s set to %d\n",
2783 devname
, array
.raid_disks
);
2786 if (info
->new_chunk
!= 0 &&
2787 info
->new_chunk
!= array
.chunk_size
) {
2788 if (sysfs_set_num(info
, NULL
,
2789 "chunk_size", info
->new_chunk
) != 0) {
2790 pr_err("failed to set chunk size\n");
2792 } else if (verbose
>= 0)
2793 printf("chunk size for %s set to %d\n",
2794 devname
, array
.chunk_size
);
2801 * There are three possibilities.
2802 * 1/ The array will shrink.
2803 * We need to ensure the reshape will pause before reaching
2804 * the 'critical section'. We also need to fork and wait for
2805 * that to happen. When it does we
2806 * suspend/backup/complete/unfreeze
2808 * 2/ The array will not change size.
2809 * This requires that we keep a backup of a sliding window
2810 * so that we can restore data after a crash. So we need
2811 * to fork and monitor progress.
2812 * In future we will allow the data_offset to change, so
2813 * a sliding backup becomes unnecessary.
2815 * 3/ The array will grow. This is relatively easy.
2816 * However the kernel's restripe routines will cheerfully
2817 * overwrite some early data before it is safe. So we
2818 * need to make a backup of the early parts of the array
2819 * and be ready to restore it if rebuild aborts very early.
2820 * For externally managed metadata, we still need a forked
2821 * child to monitor the reshape and suspend IO over the region
2822 * that is being reshaped.
2824 * We backup data by writing it to one spare, or to a
2825 * file which was given on command line.
2827 * In each case, we first make sure that storage is available
2828 * for the required backup.
2830 * - request the shape change.
2831 * - fork to handle backup etc.
2833 /* Check that we can hold all the data */
2834 get_dev_size(fd
, NULL
, &array_size
);
2835 if (reshape
.new_size
< (array_size
/512)) {
2836 pr_err("this change will reduce the size of the array.\n"
2837 " use --grow --array-size first to truncate array.\n"
2838 " e.g. mdadm --grow %s --array-size %llu\n",
2839 devname
, reshape
.new_size
/2);
2845 if (array
.level
== 10) {
2846 /* Reshaping RAID10 does not require any data backup by
2847 * user-space. Instead it requires that the data_offset
2848 * is changed to avoid the need for backup.
2849 * So this is handled very separately
2852 /* Nothing to do. */
2854 return raid10_reshape(container
, fd
, devname
, st
, info
,
2855 &reshape
, data_offset
,
2858 sra
= sysfs_read(fd
, NULL
,
2859 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
|
2862 pr_err("%s: Cannot get array details from sysfs\n",
2867 /* Decide how many blocks (sectors) for a reshape
2868 * unit. The number we have so far is just a minimum
2870 blocks
= reshape
.backup_blocks
;
2871 if (reshape
.before
.data_disks
==
2872 reshape
.after
.data_disks
) {
2873 /* Make 'blocks' bigger for better throughput, but
2874 * not so big that we reject it below.
2875 * Try for 16 megabytes
2877 while (blocks
* 32 < sra
->component_size
&&
2881 pr_err("Need to backup %luK of critical "
2882 "section..\n", blocks
/2);
2884 if (blocks
>= sra
->component_size
/2) {
2885 pr_err("%s: Something wrong"
2886 " - reshape aborted\n",
2891 /* Now we need to open all these devices so we can read/write.
2893 nrdisks
= max(reshape
.before
.data_disks
,
2894 reshape
.after
.data_disks
) + reshape
.parity
2895 + sra
->array
.spare_disks
;
2896 fdlist
= xcalloc((1+nrdisks
), sizeof(int));
2897 offsets
= xcalloc((1+nrdisks
), sizeof(offsets
[0]));
2899 odisks
= reshape
.before
.data_disks
+ reshape
.parity
;
2900 d
= reshape_prepare_fdlist(devname
, sra
, odisks
,
2901 nrdisks
, blocks
, backup_file
,
2906 if ((st
->ss
->manage_reshape
== NULL
) ||
2907 (st
->ss
->recover_backup
== NULL
)) {
2908 if (backup_file
== NULL
) {
2909 if (reshape
.after
.data_disks
<=
2910 reshape
.before
.data_disks
) {
2911 pr_err("%s: Cannot grow - "
2912 "need backup-file\n", devname
);
2914 } else if (sra
->array
.spare_disks
== 0) {
2915 pr_err("%s: Cannot grow - "
2916 "need a spare or backup-file to backup "
2917 "critical section\n", devname
);
2921 if (!reshape_open_backup_file(backup_file
, fd
, devname
,
2923 fdlist
+d
, offsets
+d
,
2931 update_cache_size(container
, sra
, info
,
2932 min(reshape
.before
.data_disks
, reshape
.after
.data_disks
),
2935 /* Right, everything seems fine. Let's kick things off.
2936 * If only changing raid_disks, use ioctl, else use
2941 if (impose_reshape(sra
, info
, st
, fd
, restart
,
2942 devname
, container
, &reshape
) < 0)
2945 err
= start_reshape(sra
, restart
, reshape
.before
.data_disks
,
2946 reshape
.after
.data_disks
);
2948 pr_err("Cannot %s reshape for %s\n",
2949 restart
? "continue" : "start",
2954 sysfs_set_str(sra
, NULL
, "array_state", "active");
2955 if (freeze_reshape
) {
2959 pr_err("Reshape has to be continued from"
2960 " location %llu when root filesystem has been mounted.\n",
2961 sra
->reshape_progress
);
2965 /* Now we just need to kick off the reshape and watch, while
2966 * handling backups of the data...
2967 * This is all done by a forked background process.
2969 switch(forked
? 0 : fork()) {
2971 pr_err("Cannot run child to monitor reshape: %s\n",
2985 /* If another array on the same devices is busy, the
2986 * reshape will wait for them. This would mean that
2987 * the first section that we suspend will stay suspended
2988 * for a long time. So check on that possibility
2989 * by looking for "DELAYED" in /proc/mdstat, and if found,
2993 struct mdstat_ent
*mds
, *m
;
2995 mds
= mdstat_read(0, 0);
2996 for (m
= mds
; m
; m
= m
->next
)
2997 if (strcmp(m
->devnm
, sra
->sys_name
) == 0) {
2999 m
->percent
== RESYNC_DELAYED
)
3002 /* Haven't started the reshape thread
3009 if (delayed
== 1 && get_linux_version() < 3007000) {
3010 pr_err("Reshape is delayed, but cannot wait carefully with this kernel.\n"
3011 " You might experience problems until other reshapes complete.\n");
3015 sleep(30 - (delayed
-1) * 25);
3019 if (check_env("MDADM_GROW_VERIFY"))
3020 fd
= open(devname
, O_RDONLY
| O_DIRECT
);
3023 mlockall(MCL_FUTURE
);
3025 if (st
->ss
->external
) {
3026 /* metadata handler takes it from here */
3027 done
= st
->ss
->manage_reshape(
3028 fd
, sra
, &reshape
, st
, blocks
,
3030 d
- odisks
, fdlist
+odisks
,
3033 done
= child_monitor(
3034 fd
, sra
, &reshape
, st
, blocks
,
3036 d
- odisks
, fdlist
+odisks
,
3042 if (backup_file
&& done
)
3043 unlink(backup_file
);
3049 if (!st
->ss
->external
&&
3050 !(reshape
.before
.data_disks
!= reshape
.after
.data_disks
3051 && info
->custom_array_size
) &&
3052 info
->new_level
== reshape
.level
&&
3054 /* no need to wait for the reshape to finish as
3055 * there is nothing more to do.
3062 if (st
->ss
->external
) {
3063 /* Re-load the metadata as much could have changed */
3064 int cfd
= open_dev(st
->container_devnm
);
3066 flush_mdmon(container
);
3067 st
->ss
->free_super(st
);
3068 st
->ss
->load_container(st
, cfd
, container
);
3073 /* set new array size if required customer_array_size is used
3076 if (reshape
.before
.data_disks
!=
3077 reshape
.after
.data_disks
&&
3078 info
->custom_array_size
)
3079 set_array_size(st
, info
, info
->text_version
);
3081 if (info
->new_level
!= reshape
.level
) {
3083 c
= map_num(pers
, info
->new_level
);
3085 err
= sysfs_set_str(sra
, NULL
, "level", c
);
3087 pr_err("%s: could not set level "
3088 "to %s\n", devname
, c
);
3090 if (info
->new_level
== 0)
3091 st
->update_tail
= NULL
;
3103 if (orig_level
!= UnSet
&& sra
) {
3104 c
= map_num(pers
, orig_level
);
3105 if (c
&& sysfs_set_str(sra
, NULL
, "level", c
) == 0)
3106 pr_err("aborting level change\n");
3114 /* mdfd handle is passed to be closed in child process (after fork).
3116 int reshape_container(char *container
, char *devname
,
3118 struct supertype
*st
,
3119 struct mdinfo
*info
,
3122 int verbose
, int restart
, int freeze_reshape
)
3124 struct mdinfo
*cc
= NULL
;
3126 char last_devnm
[32] = "";
3128 /* component_size is not meaningful for a container,
3129 * so pass '0' meaning 'no change'
3132 reshape_super(st
, 0, info
->new_level
,
3133 info
->new_layout
, info
->new_chunk
,
3134 info
->array
.raid_disks
, info
->delta_disks
,
3135 backup_file
, devname
, APPLY_METADATA_CHANGES
,
3143 /* ping monitor to be sure that update is on disk
3145 ping_monitor(container
);
3148 case -1: /* error */
3149 perror("Cannot fork to complete reshape\n");
3152 default: /* parent */
3153 if (!freeze_reshape
)
3154 printf(Name
": multi-array reshape continues"
3155 " in background\n");
3162 /* close unused handle in child process
3168 /* For each member array with reshape_active,
3169 * we need to perform the reshape.
3170 * We pick the first array that needs reshaping and
3171 * reshape it. reshape_array() will re-read the metadata
3172 * so the next time through a different array should be
3173 * ready for reshape.
3174 * It is possible that the 'different' array will not
3175 * be assembled yet. In that case we simple exit.
3176 * When it is assembled, the mdadm which assembles it
3177 * will take over the reshape.
3179 struct mdinfo
*content
;
3181 struct mdstat_ent
*mdstat
;
3187 cc
= st
->ss
->container_content(st
, NULL
);
3189 for (content
= cc
; content
; content
= content
->next
) {
3191 if (!content
->reshape_active
)
3194 subarray
= strchr(content
->text_version
+1, '/')+1;
3195 mdstat
= mdstat_by_subdev(subarray
, container
);
3198 if (mdstat
->active
== 0) {
3199 pr_err("Skipping inactive array %s.\n",
3201 free_mdstat(mdstat
);
3210 devid
= devnm2devid(mdstat
->devnm
);
3211 adev
= map_dev(major(devid
), minor(devid
), 0);
3213 adev
= content
->text_version
;
3215 fd
= open_dev(mdstat
->devnm
);
3217 printf(Name
": Device %s cannot be opened for reshape.",
3222 if (strcmp(last_devnm
, mdstat
->devnm
) == 0) {
3223 /* Do not allow for multiple reshape_array() calls for
3225 * It can happen when reshape_array() returns without
3226 * error, when reshape is not finished (wrong reshape
3227 * starting/continuation conditions). Mdmon doesn't
3228 * switch to next array in container and reentry
3229 * conditions for the same array occur.
3230 * This is possibly interim until the behaviour of
3231 * reshape_array is resolved().
3233 printf(Name
": Multiple reshape execution detected for "
3234 "device %s.", adev
);
3238 strcpy(last_devnm
, mdstat
->devnm
);
3240 sysfs_init(content
, fd
, mdstat
->devnm
);
3242 if (mdmon_running(container
))
3243 flush_mdmon(container
);
3245 rv
= reshape_array(container
, fd
, adev
, st
,
3246 content
, force
, NULL
, 0ULL,
3247 backup_file
, verbose
, 1, restart
,
3251 if (freeze_reshape
) {
3260 if (mdmon_running(container
))
3261 flush_mdmon(container
);
3270 * We run a child process in the background which performs the following
3272 * - wait for resync to reach a certain point
3273 * - suspend io to the following section
3274 * - backup that section
3275 * - allow resync to proceed further
3277 * - discard the backup.
3279 * When are combined in slightly different ways in the three cases.
3281 * - suspend/backup/allow/wait/resume/discard
3283 * - allow/wait/suspend/backup/allow/wait/resume/discard
3285 * - wait/resume/discard/suspend/backup/allow
3287 * suspend/backup/allow always come together
3288 * wait/resume/discard do too.
3289 * For the same-size case we have two backups to improve flow.
3293 int progress_reshape(struct mdinfo
*info
, struct reshape
*reshape
,
3294 unsigned long long backup_point
,
3295 unsigned long long wait_point
,
3296 unsigned long long *suspend_point
,
3297 unsigned long long *reshape_completed
)
3299 /* This function is called repeatedly by the reshape manager.
3300 * It determines how much progress can safely be made and allows
3302 * - 'info' identifies the array and particularly records in
3303 * ->reshape_progress the metadata's knowledge of progress
3304 * This is a sector offset from the start of the array
3305 * of the next array block to be relocated. This number
3306 * may increase from 0 or decrease from array_size, depending
3307 * on the type of reshape that is happening.
3308 * Note that in contrast, 'sync_completed' is a block count of the
3309 * reshape so far. It gives the distance between the start point
3310 * (head or tail of device) and the next place that data will be
3311 * written. It always increases.
3312 * - 'reshape' is the structure created by analyse_change
3313 * - 'backup_point' shows how much the metadata manager has backed-up
3314 * data. For reshapes with increasing progress, it is the next address
3315 * to be backed up, previous addresses have been backed-up. For
3316 * decreasing progress, it is the earliest address that has been
3317 * backed up - later address are also backed up.
3318 * So addresses between reshape_progress and backup_point are
3319 * backed up providing those are in the 'correct' order.
3320 * - 'wait_point' is an array address. When reshape_completed
3321 * passes this point, progress_reshape should return. It might
3322 * return earlier if it determines that ->reshape_progress needs
3323 * to be updated or further backup is needed.
3324 * - suspend_point is maintained by progress_reshape and the caller
3325 * should not touch it except to initialise to zero.
3326 * It is an array address and it only increases in 2.6.37 and earlier.
3327 * This makes it difficult to handle reducing reshapes with
3328 * external metadata.
3329 * However: it is similar to backup_point in that it records the
3330 * other end of a suspended region from reshape_progress.
3331 * it is moved to extend the region that is safe to backup and/or
3333 * - reshape_completed is read from sysfs and returned. The caller
3334 * should copy this into ->reshape_progress when it has reason to
3335 * believe that the metadata knows this, and any backup outside this
3339 * 1 if more data from backup_point - but only as far as suspend_point,
3340 * should be backed up
3341 * 0 if things are progressing smoothly
3342 * -1 if the reshape is finished because it is all done,
3343 * -2 if the reshape is finished due to an error.
3346 int advancing
= (reshape
->after
.data_disks
3347 >= reshape
->before
.data_disks
);
3348 unsigned long long need_backup
; /* All data between start of array and
3349 * here will at some point need to
3352 unsigned long long read_offset
, write_offset
;
3353 unsigned long long write_range
;
3354 unsigned long long max_progress
, target
, completed
;
3355 unsigned long long array_size
= (info
->component_size
3356 * reshape
->before
.data_disks
);
3360 /* First, we unsuspend any region that is now known to be safe.
3361 * If suspend_point is on the 'wrong' side of reshape_progress, then
3362 * we don't have or need suspension at the moment. This is true for
3363 * native metadata when we don't need to back-up.
3366 if (info
->reshape_progress
<= *suspend_point
)
3367 sysfs_set_num(info
, NULL
, "suspend_lo",
3368 info
->reshape_progress
);
3370 /* Note: this won't work in 2.6.37 and before.
3371 * Something somewhere should make sure we don't need it!
3373 if (info
->reshape_progress
>= *suspend_point
)
3374 sysfs_set_num(info
, NULL
, "suspend_hi",
3375 info
->reshape_progress
);
3378 /* Now work out how far it is safe to progress.
3379 * If the read_offset for ->reshape_progress is less than
3380 * 'blocks' beyond the write_offset, we can only progress as far
3382 * Otherwise we can progress until the write_offset for the new location
3383 * reaches (within 'blocks' of) the read_offset at the current location.
3384 * However that region must be suspended unless we are using native
3386 * If we need to suspend more, we limit it to 128M per device, which is
3387 * rather arbitrary and should be some time-based calculation.
3389 read_offset
= info
->reshape_progress
/ reshape
->before
.data_disks
;
3390 write_offset
= info
->reshape_progress
/ reshape
->after
.data_disks
;
3391 write_range
= info
->new_chunk
/512;
3392 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
3393 need_backup
= array_size
;
3395 need_backup
= reshape
->backup_blocks
;
3397 if (read_offset
< write_offset
+ write_range
)
3398 max_progress
= backup_point
;
3402 reshape
->after
.data_disks
;
3404 if (read_offset
> write_offset
- write_range
)
3405 /* Can only progress as far as has been backed up,
3406 * which must be suspended */
3407 max_progress
= backup_point
;
3408 else if (info
->reshape_progress
<= need_backup
)
3409 max_progress
= backup_point
;
3411 if (info
->array
.major_version
>= 0)
3412 /* Can progress until backup is needed */
3413 max_progress
= need_backup
;
3415 /* Can progress until metadata update is required */
3418 reshape
->after
.data_disks
;
3419 /* but data must be suspended */
3420 if (max_progress
< *suspend_point
)
3421 max_progress
= *suspend_point
;
3426 /* We know it is safe to progress to 'max_progress' providing
3427 * it is suspended or we are using native metadata.
3428 * Consider extending suspend_point 128M per device if it
3429 * is less than 64M per device beyond reshape_progress.
3430 * But always do a multiple of 'blocks'
3431 * FIXME this is too big - it takes to long to complete
3434 target
= 64*1024*2 * min(reshape
->before
.data_disks
,
3435 reshape
->after
.data_disks
);
3436 target
/= reshape
->backup_blocks
;
3439 target
*= reshape
->backup_blocks
;
3441 /* For externally managed metadata we always need to suspend IO to
3442 * the area being reshaped so we regularly push suspend_point forward.
3443 * For native metadata we only need the suspend if we are going to do
3447 if ((need_backup
> info
->reshape_progress
3448 || info
->array
.major_version
< 0) &&
3449 *suspend_point
< info
->reshape_progress
+ target
) {
3450 if (need_backup
< *suspend_point
+ 2 * target
)
3451 *suspend_point
= need_backup
;
3452 else if (*suspend_point
+ 2 * target
< array_size
)
3453 *suspend_point
+= 2 * target
;
3455 *suspend_point
= array_size
;
3456 sysfs_set_num(info
, NULL
, "suspend_hi", *suspend_point
);
3457 if (max_progress
> *suspend_point
)
3458 max_progress
= *suspend_point
;
3461 if (info
->array
.major_version
>= 0) {
3462 /* Only need to suspend when about to backup */
3463 if (info
->reshape_progress
< need_backup
* 2 &&
3464 *suspend_point
> 0) {
3466 sysfs_set_num(info
, NULL
, "suspend_lo", 0);
3467 sysfs_set_num(info
, NULL
, "suspend_hi", need_backup
);
3470 /* Need to suspend continually */
3471 if (info
->reshape_progress
< *suspend_point
)
3472 *suspend_point
= info
->reshape_progress
;
3473 if (*suspend_point
+ target
< info
->reshape_progress
)
3474 /* No need to move suspend region yet */;
3476 if (*suspend_point
>= 2 * target
)
3477 *suspend_point
-= 2 * target
;
3480 sysfs_set_num(info
, NULL
, "suspend_lo",
3483 if (max_progress
< *suspend_point
)
3484 max_progress
= *suspend_point
;
3488 /* now set sync_max to allow that progress. sync_max, like
3489 * sync_completed is a count of sectors written per device, so
3490 * we find the difference between max_progress and the start point,
3491 * and divide that by after.data_disks to get a sync_max
3493 * At the same time we convert wait_point to a similar number
3494 * for comparing against sync_completed.
3496 /* scale down max_progress to per_disk */
3497 max_progress
/= reshape
->after
.data_disks
;
3498 /* Round to chunk size as some kernels give an erroneously high number */
3499 max_progress
/= info
->new_chunk
/512;
3500 max_progress
*= info
->new_chunk
/512;
3501 /* And round to old chunk size as the kernel wants that */
3502 max_progress
/= info
->array
.chunk_size
/512;
3503 max_progress
*= info
->array
.chunk_size
/512;
3504 /* Limit progress to the whole device */
3505 if (max_progress
> info
->component_size
)
3506 max_progress
= info
->component_size
;
3507 wait_point
/= reshape
->after
.data_disks
;
3509 /* switch from 'device offset' to 'processed block count' */
3510 max_progress
= info
->component_size
- max_progress
;
3511 wait_point
= info
->component_size
- wait_point
;
3514 sysfs_set_num(info
, NULL
, "sync_max", max_progress
);
3516 /* Now wait. If we have already reached the point that we were
3517 * asked to wait to, don't wait at all, else wait for any change.
3518 * We need to select on 'sync_completed' as that is the place that
3519 * notifications happen, but we are really interested in
3520 * 'reshape_position'
3522 fd
= sysfs_get_fd(info
, NULL
, "sync_completed");
3524 goto check_progress
;
3526 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
3527 goto check_progress
;
3529 while (completed
< max_progress
&& completed
< wait_point
) {
3530 /* Check that sync_action is still 'reshape' to avoid
3531 * waiting forever on a dead array
3535 if (sysfs_get_str(info
, NULL
, "sync_action",
3537 strncmp(action
, "reshape", 7) != 0)
3539 /* Some kernels reset 'sync_completed' to zero
3540 * before setting 'sync_action' to 'idle'.
3541 * So we need these extra tests.
3543 if (completed
== 0 && advancing
3544 && info
->reshape_progress
> 0)
3546 if (completed
== 0 && !advancing
3547 && info
->reshape_progress
< (info
->component_size
3548 * reshape
->after
.data_disks
))
3552 select(fd
+1, NULL
, NULL
, &rfds
, NULL
);
3553 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
3554 goto check_progress
;
3556 /* Some kernels reset 'sync_completed' to zero,
3557 * we need to have real point we are in md
3560 completed
= max_progress
;
3562 /* some kernels can give an incorrectly high 'completed' number */
3563 completed
/= (info
->new_chunk
/512);
3564 completed
*= (info
->new_chunk
/512);
3565 /* Convert 'completed' back in to a 'progress' number */
3566 completed
*= reshape
->after
.data_disks
;
3568 completed
= info
->component_size
* reshape
->after
.data_disks
3571 *reshape_completed
= completed
;
3575 /* We return the need_backup flag. Caller will decide
3576 * how much - a multiple of ->backup_blocks up to *suspend_point
3579 return need_backup
> info
->reshape_progress
;
3581 return need_backup
>= info
->reshape_progress
;
3584 /* if we couldn't read a number from sync_completed, then
3585 * either the reshape did complete, or it aborted.
3586 * We can tell which by checking for 'none' in reshape_position.
3587 * If it did abort, then it might immediately restart if it
3588 * it was just a device failure that leaves us degraded but
3592 if (sysfs_get_str(info
, NULL
, "reshape_position", buf
, sizeof(buf
)) < 0
3593 || strncmp(buf
, "none", 4) != 0) {
3594 /* The abort might only be temporary. Wait up to 10
3595 * seconds for fd to contain a valid number again.
3601 while (fd
>= 0 && rv
< 0 && tv
.tv_sec
> 0) {
3605 if (select(fd
+1, NULL
, NULL
, &rfds
, &tv
) != 1)
3607 switch (sysfs_fd_get_ll(fd
, &completed
)) {
3609 /* all good again */
3612 case -2: /* read error - abort */
3619 return rv
; /* abort */
3621 /* Maybe racing with array shutdown - check state */
3624 if (sysfs_get_str(info
, NULL
, "array_state", buf
, sizeof(buf
)) < 0
3625 || strncmp(buf
, "inactive", 8) == 0
3626 || strncmp(buf
, "clear",5) == 0)
3627 return -2; /* abort */
3628 return -1; /* complete */
3632 /* FIXME return status is never checked */
3633 static int grow_backup(struct mdinfo
*sra
,
3634 unsigned long long offset
, /* per device */
3635 unsigned long stripes
, /* per device, in old chunks */
3636 int *sources
, unsigned long long *offsets
,
3637 int disks
, int chunk
, int level
, int layout
,
3638 int dests
, int *destfd
, unsigned long long *destoffsets
,
3639 int part
, int *degraded
,
3642 /* Backup 'blocks' sectors at 'offset' on each device of the array,
3643 * to storage 'destfd' (offset 'destoffsets'), after first
3644 * suspending IO. Then allow resync to continue
3645 * over the suspended section.
3646 * Use part 'part' of the backup-super-block.
3651 unsigned long long ll
;
3653 //printf("offset %llu\n", offset);
3659 /* Check that array hasn't become degraded, else we might backup the wrong data */
3660 if (sysfs_get_ll(sra
, NULL
, "degraded", &ll
) < 0)
3661 return -1; /* FIXME this error is ignored */
3662 new_degraded
= (int)ll
;
3663 if (new_degraded
!= *degraded
) {
3664 /* check each device to ensure it is still working */
3666 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
3667 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
3669 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
3671 if (sysfs_get_str(sra
, sd
, "state", sbuf
, 20) < 0 ||
3672 strstr(sbuf
, "faulty") ||
3673 strstr(sbuf
, "in_sync") == NULL
) {
3674 /* this device is dead */
3675 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
3676 if (sd
->disk
.raid_disk
>= 0 &&
3677 sources
[sd
->disk
.raid_disk
] >= 0) {
3678 close(sources
[sd
->disk
.raid_disk
]);
3679 sources
[sd
->disk
.raid_disk
] = -1;
3684 *degraded
= new_degraded
;
3687 bsb
.arraystart2
= __cpu_to_le64(offset
* odata
);
3688 bsb
.length2
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
3690 bsb
.arraystart
= __cpu_to_le64(offset
* odata
);
3691 bsb
.length
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
3694 bsb
.magic
[15] = '2';
3695 for (i
= 0; i
< dests
; i
++)
3697 lseek64(destfd
[i
], destoffsets
[i
] + __le64_to_cpu(bsb
.devstart2
)*512, 0);
3699 lseek64(destfd
[i
], destoffsets
[i
], 0);
3701 rv
= save_stripes(sources
, offsets
,
3702 disks
, chunk
, level
, layout
,
3704 offset
*512*odata
, stripes
* chunk
* odata
,
3709 bsb
.mtime
= __cpu_to_le64(time(0));
3710 for (i
= 0; i
< dests
; i
++) {
3711 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
3713 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
3714 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
3715 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
3716 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
3719 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
] - 4096, 0)
3720 != destoffsets
[i
] - 4096)
3722 if (write(destfd
[i
], &bsb
, 512) != 512)
3724 if (destoffsets
[i
] > 4096) {
3725 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]+stripes
*chunk
*odata
, 0) !=
3726 destoffsets
[i
]+stripes
*chunk
*odata
)
3728 if (write(destfd
[i
], &bsb
, 512) != 512)
3738 /* in 2.6.30, the value reported by sync_completed can be
3739 * less that it should be by one stripe.
3740 * This only happens when reshape hits sync_max and pauses.
3741 * So allow wait_backup to either extent sync_max further
3742 * than strictly necessary, or return before the
3743 * sync has got quite as far as we would really like.
3744 * This is what 'blocks2' is for.
3745 * The various caller give appropriate values so that
3748 /* FIXME return value is often ignored */
3749 static int forget_backup(int dests
, int *destfd
,
3750 unsigned long long *destoffsets
,
3754 * Erase backup 'part' (which is 0 or 1)
3760 bsb
.arraystart2
= __cpu_to_le64(0);
3761 bsb
.length2
= __cpu_to_le64(0);
3763 bsb
.arraystart
= __cpu_to_le64(0);
3764 bsb
.length
= __cpu_to_le64(0);
3766 bsb
.mtime
= __cpu_to_le64(time(0));
3768 for (i
= 0; i
< dests
; i
++) {
3769 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
3770 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
3771 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
3772 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
3773 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
3774 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]-4096, 0) !=
3775 destoffsets
[i
]-4096)
3778 write(destfd
[i
], &bsb
, 512) != 512)
3785 static void fail(char *msg
)
3788 rv
= (write(2, msg
, strlen(msg
)) != (int)strlen(msg
));
3789 rv
|= (write(2, "\n", 1) != 1);
3793 static char *abuf
, *bbuf
;
3794 static unsigned long long abuflen
;
3795 static void validate(int afd
, int bfd
, unsigned long long offset
)
3797 /* check that the data in the backup against the array.
3798 * This is only used for regression testing and should not
3799 * be used while the array is active
3803 lseek64(bfd
, offset
- 4096, 0);
3804 if (read(bfd
, &bsb2
, 512) != 512)
3805 fail("cannot read bsb");
3806 if (bsb2
.sb_csum
!= bsb_csum((char*)&bsb2
,
3807 ((char*)&bsb2
.sb_csum
)-((char*)&bsb2
)))
3808 fail("first csum bad");
3809 if (memcmp(bsb2
.magic
, "md_backup_data", 14) != 0)
3810 fail("magic is bad");
3811 if (memcmp(bsb2
.magic
, "md_backup_data-2", 16) == 0 &&
3812 bsb2
.sb_csum2
!= bsb_csum((char*)&bsb2
,
3813 ((char*)&bsb2
.sb_csum2
)-((char*)&bsb2
)))
3814 fail("second csum bad");
3816 if (__le64_to_cpu(bsb2
.devstart
)*512 != offset
)
3817 fail("devstart is wrong");
3820 unsigned long long len
= __le64_to_cpu(bsb2
.length
)*512;
3822 if (abuflen
< len
) {
3826 if (posix_memalign((void**)&abuf
, 4096, abuflen
) ||
3827 posix_memalign((void**)&bbuf
, 4096, abuflen
)) {
3829 /* just stop validating on mem-alloc failure */
3834 lseek64(bfd
, offset
, 0);
3835 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
) {
3836 //printf("len %llu\n", len);
3837 fail("read first backup failed");
3839 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart
)*512, 0);
3840 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
3841 fail("read first from array failed");
3842 if (memcmp(bbuf
, abuf
, len
) != 0) {
3845 printf("offset=%llu len=%llu\n",
3846 (unsigned long long)__le64_to_cpu(bsb2
.arraystart
)*512, len
);
3847 for (i
=0; i
<len
; i
++)
3848 if (bbuf
[i
] != abuf
[i
]) {
3849 printf("first diff byte %d\n", i
);
3853 fail("data1 compare failed");
3857 unsigned long long len
= __le64_to_cpu(bsb2
.length2
)*512;
3859 if (abuflen
< len
) {
3863 abuf
= xmalloc(abuflen
);
3864 bbuf
= xmalloc(abuflen
);
3867 lseek64(bfd
, offset
+__le64_to_cpu(bsb2
.devstart2
)*512, 0);
3868 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
)
3869 fail("read second backup failed");
3870 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart2
)*512, 0);
3871 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
3872 fail("read second from array failed");
3873 if (memcmp(bbuf
, abuf
, len
) != 0)
3874 fail("data2 compare failed");
3878 int child_monitor(int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
3879 struct supertype
*st
, unsigned long blocks
,
3880 int *fds
, unsigned long long *offsets
,
3881 int dests
, int *destfd
, unsigned long long *destoffsets
)
3883 /* Monitor a reshape where backup is being performed using
3884 * 'native' mechanism - either to a backup file, or
3885 * to some space in a spare.
3889 unsigned long long speed
;
3890 unsigned long long suspend_point
, array_size
;
3891 unsigned long long backup_point
, wait_point
;
3892 unsigned long long reshape_completed
;
3894 int increasing
= reshape
->after
.data_disks
>= reshape
->before
.data_disks
;
3895 int part
= 0; /* The next part of the backup area to fill. It may already
3896 * be full, so we need to check */
3897 int level
= reshape
->level
;
3898 int layout
= reshape
->before
.layout
;
3899 int data
= reshape
->before
.data_disks
;
3900 int disks
= reshape
->before
.data_disks
+ reshape
->parity
;
3901 int chunk
= sra
->array
.chunk_size
;
3903 unsigned long stripes
;
3906 /* set up the backup-super-block. This requires the
3907 * uuid from the array.
3909 /* Find a superblock */
3910 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
3914 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
3916 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
3917 devfd
= dev_open(dn
, O_RDONLY
);
3920 ok
= st
->ss
->load_super(st
, devfd
, NULL
);
3926 pr_err("Cannot find a superblock\n");
3930 memset(&bsb
, 0, 512);
3931 memcpy(bsb
.magic
, "md_backup_data-1", 16);
3932 st
->ss
->uuid_from_super(st
, uuid
);
3933 memcpy(bsb
.set_uuid
, uuid
, 16);
3934 bsb
.mtime
= __cpu_to_le64(time(0));
3935 bsb
.devstart2
= blocks
;
3937 stripes
= blocks
/ (sra
->array
.chunk_size
/512) /
3938 reshape
->before
.data_disks
;
3940 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
3941 /* Don't start the 'reshape' */
3943 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
) {
3944 sysfs_get_ll(sra
, NULL
, "sync_speed_min", &speed
);
3945 sysfs_set_num(sra
, NULL
, "sync_speed_min", 200000);
3949 array_size
= sra
->component_size
* reshape
->after
.data_disks
;
3950 backup_point
= sra
->reshape_progress
;
3953 array_size
= sra
->component_size
* reshape
->before
.data_disks
;
3954 backup_point
= reshape
->backup_blocks
;
3955 suspend_point
= array_size
;
3961 /* Want to return as soon the oldest backup slot can
3962 * be released as that allows us to start backing up
3963 * some more, providing suspend_point has been
3964 * advanced, which it should have.
3967 wait_point
= array_size
;
3968 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
3969 wait_point
= (__le64_to_cpu(bsb
.arraystart
) +
3970 __le64_to_cpu(bsb
.length
));
3971 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
3972 wait_point
= (__le64_to_cpu(bsb
.arraystart2
) +
3973 __le64_to_cpu(bsb
.length2
));
3976 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
3977 wait_point
= __le64_to_cpu(bsb
.arraystart
);
3978 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
3979 wait_point
= __le64_to_cpu(bsb
.arraystart2
);
3982 rv
= progress_reshape(sra
, reshape
,
3983 backup_point
, wait_point
,
3984 &suspend_point
, &reshape_completed
);
3985 /* external metadata would need to ping_monitor here */
3986 sra
->reshape_progress
= reshape_completed
;
3988 /* Clear any backup region that is before 'here' */
3990 if (__le64_to_cpu(bsb
.length
) > 0 &&
3991 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart
) +
3992 __le64_to_cpu(bsb
.length
)))
3993 forget_backup(dests
, destfd
,
3995 if (__le64_to_cpu(bsb
.length2
) > 0 &&
3996 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart2
) +
3997 __le64_to_cpu(bsb
.length2
)))
3998 forget_backup(dests
, destfd
,
4001 if (__le64_to_cpu(bsb
.length
) > 0 &&
4002 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart
)))
4003 forget_backup(dests
, destfd
,
4005 if (__le64_to_cpu(bsb
.length2
) > 0 &&
4006 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart2
)))
4007 forget_backup(dests
, destfd
,
4016 if (rv
== 0 && increasing
&& !st
->ss
->external
) {
4017 /* No longer need to monitor this reshape */
4023 unsigned long long offset
;
4024 unsigned long actual_stripes
;
4025 /* Need to backup some data.
4026 * If 'part' is not used and the desired
4027 * backup size is suspended, do a backup,
4028 * then consider the next part.
4030 /* Check that 'part' is unused */
4031 if (part
== 0 && __le64_to_cpu(bsb
.length
) != 0)
4033 if (part
== 1 && __le64_to_cpu(bsb
.length2
) != 0)
4036 offset
= backup_point
/ data
;
4037 actual_stripes
= stripes
;
4039 if (offset
+ actual_stripes
* (chunk
/512) >
4040 sra
->component_size
)
4041 actual_stripes
= ((sra
->component_size
- offset
)
4043 if (offset
+ actual_stripes
* (chunk
/512) >
4047 if (offset
< actual_stripes
* (chunk
/512))
4048 actual_stripes
= offset
/ (chunk
/512);
4049 offset
-= actual_stripes
* (chunk
/512);
4050 if (offset
< suspend_point
/data
)
4053 if (actual_stripes
== 0)
4055 grow_backup(sra
, offset
, actual_stripes
,
4057 disks
, chunk
, level
, layout
,
4058 dests
, destfd
, destoffsets
,
4059 part
, °raded
, buf
);
4060 validate(afd
, destfd
[0], destoffsets
[0]);
4061 /* record where 'part' is up to */
4064 backup_point
+= actual_stripes
* (chunk
/512) * data
;
4066 backup_point
-= actual_stripes
* (chunk
/512) * data
;
4070 /* FIXME maybe call progress_reshape one more time instead */
4071 abort_reshape(sra
); /* remove any remaining suspension */
4072 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
4073 sysfs_set_num(sra
, NULL
, "sync_speed_min", speed
);
4079 * If any spare contains md_back_data-1 which is recent wrt mtime,
4080 * write that data into the array and update the super blocks with
4081 * the new reshape_progress
4083 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
, int cnt
,
4084 char *backup_file
, int verbose
)
4088 unsigned long long *offsets
;
4089 unsigned long long nstripe
, ostripe
;
4092 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
4093 if (info
->array
.level
== 6) odata
--; /* number of data disks */
4094 ndata
= info
->array
.raid_disks
- 1;
4095 if (info
->new_level
== 6) ndata
--;
4097 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
4099 if (info
->delta_disks
<= 0)
4100 /* Didn't grow, so the backup file must have
4104 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
4105 struct mdinfo dinfo
;
4108 char *devname
, namebuf
[20];
4109 unsigned long long lo
, hi
;
4111 /* This was a spare and may have some saved data on it.
4112 * Load the superblock, find and load the
4113 * backup_super_block.
4114 * If either fail, go on to next device.
4115 * If the backup contains no new info, just return
4116 * else restore data and update all superblocks
4118 if (i
== old_disks
-1) {
4119 fd
= open(backup_file
, O_RDONLY
);
4121 pr_err("backup file %s inaccessible: %s\n",
4122 backup_file
, strerror(errno
));
4125 devname
= backup_file
;
4130 if (st
->ss
->load_super(st
, fd
, NULL
))
4133 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4134 st
->ss
->free_super(st
);
4137 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
4139 pr_err("Cannot seek on device %d\n", i
);
4140 continue; /* Cannot seek */
4142 sprintf(namebuf
, "device-%d", i
);
4145 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
)) {
4147 pr_err("Cannot read from %s\n", devname
);
4148 continue; /* Cannot read */
4150 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0 &&
4151 memcmp(bsb
.magic
, "md_backup_data-2", 16) != 0) {
4153 pr_err("No backup metadata on %s\n", devname
);
4156 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
))) {
4158 pr_err("Bad backup-metadata checksum on %s\n", devname
);
4159 continue; /* bad checksum */
4161 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0 &&
4162 bsb
.sb_csum2
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum2
)-((char*)&bsb
))) {
4164 pr_err("Bad backup-metadata checksum2 on %s\n", devname
);
4165 continue; /* Bad second checksum */
4167 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0) {
4169 pr_err("Wrong uuid on backup-metadata on %s\n", devname
);
4170 continue; /* Wrong uuid */
4173 /* array utime and backup-mtime should be updated at much the same time, but it seems that
4174 * sometimes they aren't... So allow considerable flexability in matching, and allow
4175 * this test to be overridden by an environment variable.
4177 if (info
->array
.utime
> (int)__le64_to_cpu(bsb
.mtime
) + 2*60*60 ||
4178 info
->array
.utime
< (int)__le64_to_cpu(bsb
.mtime
) - 10*60) {
4179 if (check_env("MDADM_GROW_ALLOW_OLD")) {
4180 pr_err("accepting backup with timestamp %lu "
4181 "for array with timestamp %lu\n",
4182 (unsigned long)__le64_to_cpu(bsb
.mtime
),
4183 (unsigned long)info
->array
.utime
);
4185 pr_err("too-old timestamp on backup-metadata on %s\n", devname
);
4186 pr_err("If you think it is should be safe, try 'export MDADM_GROW_ALLOW_OLD=1'\n");
4187 continue; /* time stamp is too bad */
4191 if (bsb
.magic
[15] == '1') {
4192 if (bsb
.length
== 0)
4194 if (info
->delta_disks
>= 0) {
4195 /* reshape_progress is increasing */
4196 if (__le64_to_cpu(bsb
.arraystart
)
4197 + __le64_to_cpu(bsb
.length
)
4198 < info
->reshape_progress
) {
4201 pr_err("backup-metadata found on %s but is not needed\n", devname
);
4202 continue; /* No new data here */
4205 /* reshape_progress is decreasing */
4206 if (__le64_to_cpu(bsb
.arraystart
) >=
4207 info
->reshape_progress
)
4208 goto nonew
; /* No new data here */
4211 if (bsb
.length
== 0 && bsb
.length2
== 0)
4213 if (info
->delta_disks
>= 0) {
4214 /* reshape_progress is increasing */
4215 if ((__le64_to_cpu(bsb
.arraystart
)
4216 + __le64_to_cpu(bsb
.length
)
4217 < info
->reshape_progress
)
4219 (__le64_to_cpu(bsb
.arraystart2
)
4220 + __le64_to_cpu(bsb
.length2
)
4221 < info
->reshape_progress
))
4222 goto nonew
; /* No new data here */
4224 /* reshape_progress is decreasing */
4225 if (__le64_to_cpu(bsb
.arraystart
) >=
4226 info
->reshape_progress
&&
4227 __le64_to_cpu(bsb
.arraystart2
) >=
4228 info
->reshape_progress
)
4229 goto nonew
; /* No new data here */
4232 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0) {
4235 pr_err("Failed to verify secondary backup-metadata block on %s\n",
4237 continue; /* Cannot seek */
4239 /* There should be a duplicate backup superblock 4k before here */
4240 if (lseek64(fd
, -4096, 1) < 0 ||
4241 read(fd
, &bsb2
, sizeof(bsb2
)) != sizeof(bsb2
))
4242 goto second_fail
; /* Cannot find leading superblock */
4243 if (bsb
.magic
[15] == '1')
4244 bsbsize
= offsetof(struct mdp_backup_super
, pad1
);
4246 bsbsize
= offsetof(struct mdp_backup_super
, pad
);
4247 if (memcmp(&bsb2
, &bsb
, bsbsize
) != 0)
4248 goto second_fail
; /* Cannot find leading superblock */
4250 /* Now need the data offsets for all devices. */
4251 offsets
= xmalloc(sizeof(*offsets
)*info
->array
.raid_disks
);
4252 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
4255 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
4256 /* FIXME should be this be an error */
4258 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4259 st
->ss
->free_super(st
);
4260 offsets
[j
] = dinfo
.data_offset
* 512;
4262 printf(Name
": restoring critical section\n");
4264 if (restore_stripes(fdlist
, offsets
,
4265 info
->array
.raid_disks
,
4269 fd
, __le64_to_cpu(bsb
.devstart
)*512,
4270 __le64_to_cpu(bsb
.arraystart
)*512,
4271 __le64_to_cpu(bsb
.length
)*512, NULL
)) {
4272 /* didn't succeed, so giveup */
4274 pr_err("Error restoring backup from %s\n",
4280 if (bsb
.magic
[15] == '2' &&
4281 restore_stripes(fdlist
, offsets
,
4282 info
->array
.raid_disks
,
4286 fd
, __le64_to_cpu(bsb
.devstart
)*512 +
4287 __le64_to_cpu(bsb
.devstart2
)*512,
4288 __le64_to_cpu(bsb
.arraystart2
)*512,
4289 __le64_to_cpu(bsb
.length2
)*512, NULL
)) {
4290 /* didn't succeed, so giveup */
4292 pr_err("Error restoring second backup from %s\n",
4300 /* Ok, so the data is restored. Let's update those superblocks. */
4304 lo
= __le64_to_cpu(bsb
.arraystart
);
4305 hi
= lo
+ __le64_to_cpu(bsb
.length
);
4307 if (bsb
.magic
[15] == '2' && bsb
.length2
) {
4308 unsigned long long lo1
, hi1
;
4309 lo1
= __le64_to_cpu(bsb
.arraystart2
);
4310 hi1
= lo1
+ __le64_to_cpu(bsb
.length2
);
4314 } else if (lo
< lo1
)
4320 (info
->reshape_progress
< lo
||
4321 info
->reshape_progress
> hi
))
4322 /* backup does not affect reshape_progress*/ ;
4323 else if (info
->delta_disks
>= 0) {
4324 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
) +
4325 __le64_to_cpu(bsb
.length
);
4326 if (bsb
.magic
[15] == '2') {
4327 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
) +
4328 __le64_to_cpu(bsb
.length2
);
4329 if (p2
> info
->reshape_progress
)
4330 info
->reshape_progress
= p2
;
4333 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
);
4334 if (bsb
.magic
[15] == '2') {
4335 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
);
4336 if (p2
< info
->reshape_progress
)
4337 info
->reshape_progress
= p2
;
4340 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
4343 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
4345 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4346 dinfo
.reshape_progress
= info
->reshape_progress
;
4347 st
->ss
->update_super(st
, &dinfo
,
4348 "_reshape_progress",
4350 st
->ss
->store_super(st
, fdlist
[j
]);
4351 st
->ss
->free_super(st
);
4355 /* Didn't find any backup data, try to see if any
4358 if (info
->delta_disks
< 0) {
4359 /* When shrinking, the critical section is at the end.
4360 * So see if we are before the critical section.
4362 unsigned long long first_block
;
4363 nstripe
= ostripe
= 0;
4365 while (ostripe
>= nstripe
) {
4366 ostripe
+= info
->array
.chunk_size
/ 512;
4367 first_block
= ostripe
* odata
;
4368 nstripe
= first_block
/ ndata
/ (info
->new_chunk
/512) *
4369 (info
->new_chunk
/512);
4372 if (info
->reshape_progress
>= first_block
)
4375 if (info
->delta_disks
> 0) {
4376 /* See if we are beyond the critical section. */
4377 unsigned long long last_block
;
4378 nstripe
= ostripe
= 0;
4380 while (nstripe
>= ostripe
) {
4381 nstripe
+= info
->new_chunk
/ 512;
4382 last_block
= nstripe
* ndata
;
4383 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
4384 (info
->array
.chunk_size
/512);
4387 if (info
->reshape_progress
>= last_block
)
4390 /* needed to recover critical section! */
4392 pr_err("Failed to find backup of critical section\n");
4396 int Grow_continue_command(char *devname
, int fd
,
4397 char *backup_file
, int verbose
)
4400 struct supertype
*st
= NULL
;
4401 struct mdinfo
*content
= NULL
;
4402 struct mdinfo array
;
4403 char *subarray
= NULL
;
4404 struct mdinfo
*cc
= NULL
;
4405 struct mdstat_ent
*mdstat
= NULL
;
4409 dprintf("Grow continue from command line called for %s\n",
4412 st
= super_by_fd(fd
, &subarray
);
4413 if (!st
|| !st
->ss
) {
4414 pr_err("Unable to determine metadata format for %s\n",
4418 dprintf("Grow continue is run for ");
4419 if (st
->ss
->external
== 0) {
4421 dprintf("native array (%s)\n", devname
);
4422 if (ioctl(fd
, GET_ARRAY_INFO
, &array
.array
) < 0) {
4423 pr_err("%s is not an active md array -"
4424 " aborting\n", devname
);
4426 goto Grow_continue_command_exit
;
4429 /* Need to load a superblock.
4430 * FIXME we should really get what we need from
4433 for (d
= 0; d
< MAX_DISKS
; d
++) {
4434 mdu_disk_info_t disk
;
4438 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
4440 if (disk
.major
== 0 && disk
.minor
== 0)
4442 if ((disk
.state
& (1 << MD_DISK_ACTIVE
)) == 0)
4444 dv
= map_dev(disk
.major
, disk
.minor
, 1);
4447 fd2
= dev_open(dv
, O_RDONLY
);
4450 err
= st
->ss
->load_super(st
, fd2
, NULL
);
4456 if (d
== MAX_DISKS
) {
4457 pr_err("Unable to load metadata for %s\n",
4460 goto Grow_continue_command_exit
;
4462 st
->ss
->getinfo_super(st
, content
, NULL
);
4467 dprintf("subarray (%s)\n", subarray
);
4468 container
= st
->container_devnm
;
4469 cfd
= open_dev_excl(st
->container_devnm
);
4471 container
= st
->devnm
;
4473 cfd
= open_dev_excl(st
->devnm
);
4474 dprintf("container (%s)\n", container
);
4478 pr_err("Unable to open container "
4479 "for %s\n", devname
);
4481 goto Grow_continue_command_exit
;
4484 /* find in container array under reshape
4486 ret_val
= st
->ss
->load_container(st
, cfd
, NULL
);
4488 pr_err("Cannot read superblock for %s\n",
4491 goto Grow_continue_command_exit
;
4494 cc
= st
->ss
->container_content(st
, subarray
);
4495 for (content
= cc
; content
; content
= content
->next
) {
4497 int allow_reshape
= 1;
4499 if (content
->reshape_active
== 0)
4501 /* The decision about array or container wide
4502 * reshape is taken in Grow_continue based
4503 * content->reshape_active state, therefore we
4504 * need to check_reshape based on
4505 * reshape_active and subarray name
4507 if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
))
4509 if (content
->reshape_active
== CONTAINER_RESHAPE
&&
4510 (content
->array
.state
4511 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
)))
4514 if (!allow_reshape
) {
4515 pr_err("cannot continue reshape of an array"
4516 " in container with unsupported"
4517 " metadata: %s(%s)\n",
4518 devname
, container
);
4520 goto Grow_continue_command_exit
;
4523 array
= strchr(content
->text_version
+1, '/')+1;
4524 mdstat
= mdstat_by_subdev(array
, container
);
4527 if (mdstat
->active
== 0) {
4528 pr_err("Skipping inactive array %s.\n",
4530 free_mdstat(mdstat
);
4537 pr_err("Unable to determine reshaped "
4538 "array for %s\n", devname
);
4540 goto Grow_continue_command_exit
;
4542 fd2
= open_dev(mdstat
->devnm
);
4544 pr_err("cannot open (%s)\n", mdstat
->devnm
);
4546 goto Grow_continue_command_exit
;
4549 sysfs_init(content
, fd2
, mdstat
->devnm
);
4551 /* start mdmon in case it is not running
4553 if (!mdmon_running(container
))
4554 start_mdmon(container
);
4555 ping_monitor(container
);
4557 if (mdmon_running(container
))
4558 st
->update_tail
= &st
->updates
;
4560 pr_err("No mdmon found. "
4561 "Grow cannot continue.\n");
4563 goto Grow_continue_command_exit
;
4567 /* verify that array under reshape is started from
4570 if (verify_reshape_position(content
, content
->array
.level
) < 0) {
4572 goto Grow_continue_command_exit
;
4577 ret_val
= Grow_continue(fd
, st
, content
, backup_file
, 0);
4579 Grow_continue_command_exit
:
4584 st
->ss
->free_super(st
);
4585 free_mdstat(mdstat
);
4592 int Grow_continue(int mdfd
, struct supertype
*st
, struct mdinfo
*info
,
4593 char *backup_file
, int freeze_reshape
)
4597 if (!info
->reshape_active
)
4600 if (st
->ss
->external
) {
4601 int cfd
= open_dev(st
->container_devnm
);
4606 st
->ss
->load_container(st
, cfd
, st
->container_devnm
);
4608 ret_val
= reshape_container(st
->container_devnm
, NULL
, mdfd
,
4609 st
, info
, 0, backup_file
,
4610 0, 1, freeze_reshape
);
4612 ret_val
= reshape_array(NULL
, mdfd
, "array", st
, info
, 1,
4613 NULL
, 0ULL, backup_file
, 0, 0, 1,