2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2013 Neil Brown <neilb@suse.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Email: <neilb@suse.de>
31 #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
32 #error no endian defined
38 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
41 int restore_backup(struct supertype
*st
,
42 struct mdinfo
*content
,
52 int disk_count
= next_spare
+ working_disks
;
53 char *backup_file
= *backup_filep
;
55 dprintf("Called restore_backup()\n");
56 fdlist
= xmalloc(sizeof(int) * disk_count
);
58 enable_fds(next_spare
);
59 for (i
= 0; i
< next_spare
; i
++)
61 for (dev
= content
->devs
; dev
; dev
= dev
->next
) {
67 fd
= dev_open(buf
, O_RDWR
);
69 if (dev
->disk
.raid_disk
>= 0)
70 fdlist
[dev
->disk
.raid_disk
] = fd
;
72 fdlist
[next_spare
++] = fd
;
76 backup_file
= locate_backup(content
->sys_name
);
77 *backup_filep
= backup_file
;
80 if (st
->ss
->external
&& st
->ss
->recover_backup
)
81 err
= st
->ss
->recover_backup(st
, content
);
83 err
= Grow_restart(st
, content
, fdlist
, next_spare
,
84 backup_file
, verbose
> 0);
86 while (next_spare
> 0) {
88 if (fdlist
[next_spare
] >= 0)
89 close(fdlist
[next_spare
]);
93 pr_err("Failed to restore critical"
94 " section for reshape - sorry.\n");
96 pr_err("Possibly you need"
97 " to specify a --backup-file\n");
101 dprintf("restore_backup() returns status OK.\n");
105 int Grow_Add_device(char *devname
, int fd
, char *newdev
)
107 /* Add a device to an active array.
108 * Currently, just extend a linear array.
109 * This requires writing a new superblock on the
110 * new device, calling the kernel to add the device,
111 * and if that succeeds, update the superblock on
113 * This means that we need to *find* all other devices.
120 struct supertype
*st
= NULL
;
121 char *subarray
= NULL
;
123 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
124 pr_err("cannot get array info for %s\n", devname
);
128 if (info
.array
.level
!= -1) {
129 pr_err("can only add devices to linear arrays\n");
133 st
= super_by_fd(fd
, &subarray
);
135 pr_err("cannot handle arrays with superblock version %d\n",
136 info
.array
.major_version
);
141 pr_err("Cannot grow linear sub-arrays yet\n");
147 nfd
= open(newdev
, O_RDWR
|O_EXCL
|O_DIRECT
);
149 pr_err("cannot open %s\n", newdev
);
154 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
155 pr_err("%s is not a block device!\n", newdev
);
160 /* now check out all the devices and make sure we can read the
162 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
163 mdu_disk_info_t disk
;
166 st
->ss
->free_super(st
);
169 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
170 pr_err("cannot get device detail for device %d\n",
176 dv
= map_dev(disk
.major
, disk
.minor
, 1);
178 pr_err("cannot find device file for device %d\n",
184 fd2
= dev_open(dv
, O_RDWR
);
186 pr_err("cannot open device file %s\n", dv
);
192 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
193 pr_err("cannot find super block on %s\n", dv
);
201 /* Ok, looks good. Lets update the superblock and write it out to
205 info
.disk
.number
= d
;
206 info
.disk
.major
= major(stb
.st_rdev
);
207 info
.disk
.minor
= minor(stb
.st_rdev
);
208 info
.disk
.raid_disk
= d
;
209 info
.disk
.state
= (1 << MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
);
210 st
->ss
->update_super(st
, &info
, "linear-grow-new", newdev
,
213 if (st
->ss
->store_super(st
, nfd
)) {
214 pr_err("Cannot store new superblock on %s\n",
221 if (ioctl(fd
, ADD_NEW_DISK
, &info
.disk
) != 0) {
222 pr_err("Cannot add new disk to this array\n");
225 /* Well, that seems to have worked.
226 * Now go through and update all superblocks
229 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
230 pr_err("cannot get array info for %s\n", devname
);
235 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
236 mdu_disk_info_t disk
;
240 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
241 pr_err("cannot get device detail for device %d\n",
245 dv
= map_dev(disk
.major
, disk
.minor
, 1);
247 pr_err("cannot find device file for device %d\n",
251 fd2
= dev_open(dv
, O_RDWR
);
253 pr_err("cannot open device file %s\n", dv
);
256 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
257 pr_err("cannot find super block on %s\n", dv
);
261 info
.array
.raid_disks
= nd
+1;
262 info
.array
.nr_disks
= nd
+1;
263 info
.array
.active_disks
= nd
+1;
264 info
.array
.working_disks
= nd
+1;
266 st
->ss
->update_super(st
, &info
, "linear-grow-update", dv
,
269 if (st
->ss
->store_super(st
, fd2
)) {
270 pr_err("Cannot store new superblock on %s\n", dv
);
280 int Grow_addbitmap(char *devname
, int fd
, struct context
*c
, struct shape
*s
)
283 * First check that array doesn't have a bitmap
284 * Then create the bitmap
287 * For internal bitmaps, we need to check the version,
288 * find all the active devices, and write the bitmap block
291 mdu_bitmap_file_t bmf
;
292 mdu_array_info_t array
;
293 struct supertype
*st
;
294 char *subarray
= NULL
;
295 int major
= BITMAP_MAJOR_HI
;
296 int vers
= md_get_version(fd
);
297 unsigned long long bitmapsize
, array_size
;
300 major
= BITMAP_MAJOR_HOSTENDIAN
;
301 pr_err("Warning - bitmaps created on this kernel"
302 " are not portable\n"
303 " between different architectures. Consider upgrading"
304 " the Linux kernel.\n");
307 if (ioctl(fd
, GET_BITMAP_FILE
, &bmf
) != 0) {
309 pr_err("Memory allocation failure.\n");
311 pr_err("bitmaps not supported by this kernel.\n");
314 if (bmf
.pathname
[0]) {
315 if (strcmp(s
->bitmap_file
,"none")==0) {
316 if (ioctl(fd
, SET_BITMAP_FILE
, -1)!= 0) {
317 pr_err("failed to remove bitmap %s\n",
323 pr_err("%s already has a bitmap (%s)\n",
324 devname
, bmf
.pathname
);
327 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
328 pr_err("cannot get array status for %s\n", devname
);
331 if (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)) {
332 if (strcmp(s
->bitmap_file
, "none")==0) {
333 array
.state
&= ~(1<<MD_SB_BITMAP_PRESENT
);
334 if (ioctl(fd
, SET_ARRAY_INFO
, &array
)!= 0) {
335 pr_err("failed to remove internal bitmap.\n");
340 pr_err("Internal bitmap already present on %s\n",
345 if (strcmp(s
->bitmap_file
, "none") == 0) {
346 pr_err("no bitmap found on %s\n", devname
);
349 if (array
.level
<= 0) {
350 pr_err("Bitmaps not meaningful with level %s\n",
351 map_num(pers
, array
.level
)?:"of this array");
354 bitmapsize
= array
.size
;
356 if (get_dev_size(fd
, NULL
, &array_size
) &&
357 array_size
> (0x7fffffffULL
<<9)) {
358 /* Array is big enough that we cannot trust array.size
359 * try other approaches
361 bitmapsize
= get_component_size(fd
);
363 if (bitmapsize
== 0) {
364 pr_err("Cannot reliably determine size of array to create bitmap - sorry.\n");
368 if (array
.level
== 10) {
369 int ncopies
= (array
.layout
&255)*((array
.layout
>>8)&255);
370 bitmapsize
= bitmapsize
* array
.raid_disks
/ ncopies
;
373 st
= super_by_fd(fd
, &subarray
);
375 pr_err("Cannot understand version %d.%d\n",
376 array
.major_version
, array
.minor_version
);
380 pr_err("Cannot add bitmaps to sub-arrays yet\n");
385 if (strcmp(s
->bitmap_file
, "internal") == 0) {
388 int offset_setable
= 0;
390 if (st
->ss
->add_internal_bitmap
== NULL
) {
391 pr_err("Internal bitmaps not supported "
392 "with %s metadata\n", st
->ss
->name
);
395 mdi
= sysfs_read(fd
, NULL
, GET_BITMAP_LOCATION
);
398 for (d
=0; d
< st
->max_devs
; d
++) {
399 mdu_disk_info_t disk
;
402 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
404 if (disk
.major
== 0 &&
407 if ((disk
.state
& (1<<MD_DISK_SYNC
))==0)
409 dv
= map_dev(disk
.major
, disk
.minor
, 1);
411 int fd2
= dev_open(dv
, O_RDWR
);
414 if (st
->ss
->load_super(st
, fd2
, NULL
)==0) {
415 if (st
->ss
->add_internal_bitmap(
417 &s
->bitmap_chunk
, c
->delay
, s
->write_behind
,
418 bitmapsize
, offset_setable
,
421 st
->ss
->write_bitmap(st
, fd2
);
423 pr_err("failed to create internal bitmap"
424 " - chunksize problem.\n");
432 if (offset_setable
) {
433 st
->ss
->getinfo_super(st
, mdi
, NULL
);
434 sysfs_init(mdi
, fd
, NULL
);
435 rv
= sysfs_set_num_signed(mdi
, NULL
, "bitmap/location",
438 array
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
439 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
443 pr_err("Cannot add bitmap while array is"
444 " resyncing or reshaping etc.\n");
445 pr_err("failed to set internal bitmap.\n");
452 int max_devs
= st
->max_devs
;
454 /* try to load a superblock */
455 for (d
= 0; d
< max_devs
; d
++) {
456 mdu_disk_info_t disk
;
460 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
462 if ((disk
.major
==0 && disk
.minor
==0) ||
463 (disk
.state
& (1<<MD_DISK_REMOVED
)))
465 dv
= map_dev(disk
.major
, disk
.minor
, 1);
468 fd2
= dev_open(dv
, O_RDONLY
);
470 if (st
->ss
->load_super(st
, fd2
, NULL
) == 0) {
472 st
->ss
->uuid_from_super(st
, uuid
);
479 pr_err("cannot find UUID for array!\n");
482 if (CreateBitmap(s
->bitmap_file
, c
->force
, (char*)uuid
, s
->bitmap_chunk
,
483 c
->delay
, s
->write_behind
, bitmapsize
, major
)) {
486 bitmap_fd
= open(s
->bitmap_file
, O_RDWR
);
488 pr_err("weird: %s cannot be opened\n",
492 if (ioctl(fd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
495 pr_err("Cannot add bitmap while array is"
496 " resyncing or reshaping etc.\n");
497 pr_err("Cannot set bitmap file for %s: %s\n",
498 devname
, strerror(err
));
507 * When reshaping an array we might need to backup some data.
508 * This is written to all spares with a 'super_block' describing it.
509 * The superblock goes 4K from the end of the used space on the
511 * It if written after the backup is complete.
512 * It has the following structure.
515 static struct mdp_backup_super
{
516 char magic
[16]; /* md_backup_data-1 or -2 */
519 /* start/sizes in 512byte sectors */
520 __u64 devstart
; /* address on backup device/file of data */
523 __u32 sb_csum
; /* csum of preceeding bytes. */
525 __u64 devstart2
; /* offset in to data of second section */
528 __u32 sb_csum2
; /* csum of preceeding bytes. */
530 } __attribute__((aligned(512))) bsb
, bsb2
;
532 static __u32
bsb_csum(char *buf
, int len
)
536 for (i
= 0; i
< len
; i
++)
537 csum
= (csum
<<3) + buf
[0];
538 return __cpu_to_le32(csum
);
541 static int check_idle(struct supertype
*st
)
543 /* Check that all member arrays for this container, or the
544 * container of this array, are idle
546 char *container
= (st
->container_devnm
[0]
547 ? st
->container_devnm
: st
->devnm
);
548 struct mdstat_ent
*ent
, *e
;
551 ent
= mdstat_read(0, 0);
552 for (e
= ent
; e
; e
= e
->next
) {
553 if (!is_container_member(e
, container
))
555 if (e
->percent
>= 0) {
564 static int freeze_container(struct supertype
*st
)
566 char *container
= (st
->container_devnm
[0]
567 ? st
->container_devnm
: st
->devnm
);
572 if (block_monitor(container
, 1)) {
573 pr_err("failed to freeze container\n");
580 static void unfreeze_container(struct supertype
*st
)
582 char *container
= (st
->container_devnm
[0]
583 ? st
->container_devnm
: st
->devnm
);
585 unblock_monitor(container
, 1);
588 static int freeze(struct supertype
*st
)
590 /* Try to freeze resync/rebuild on this array/container.
591 * Return -1 if the array is busy,
592 * return -2 container cannot be frozen,
593 * return 0 if this kernel doesn't support 'frozen'
594 * return 1 if it worked.
596 if (st
->ss
->external
)
597 return freeze_container(st
);
599 struct mdinfo
*sra
= sysfs_read(-1, st
->devnm
, GET_VERSION
);
605 /* Need to clear any 'read-auto' status */
606 if (sysfs_get_str(sra
, NULL
, "array_state", buf
, 20) > 0 &&
607 strncmp(buf
, "read-auto", 9) == 0)
608 sysfs_set_str(sra
, NULL
, "array_state", "clean");
610 err
= sysfs_freeze_array(sra
);
616 static void unfreeze(struct supertype
*st
)
618 if (st
->ss
->external
)
619 return unfreeze_container(st
);
621 struct mdinfo
*sra
= sysfs_read(-1, st
->devnm
, GET_VERSION
);
625 sysfs_get_str(sra
, NULL
, "sync_action", buf
, 20) > 0
626 && strcmp(buf
, "frozen\n") == 0) {
627 printf("unfreeze\n");
628 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
634 static void wait_reshape(struct mdinfo
*sra
)
636 int fd
= sysfs_get_fd(sra
, NULL
, "sync_action");
642 while (sysfs_fd_get_str(fd
, action
, 20) > 0 &&
643 strncmp(action
, "reshape", 7) == 0)
644 sysfs_wait(fd
, NULL
);
648 static int reshape_super(struct supertype
*st
, unsigned long long size
,
649 int level
, int layout
, int chunksize
, int raid_disks
,
650 int delta_disks
, char *backup_file
, char *dev
,
651 int direction
, int verbose
)
653 /* nothing extra to check in the native case */
654 if (!st
->ss
->external
)
656 if (!st
->ss
->reshape_super
||
657 !st
->ss
->manage_reshape
) {
658 pr_err("%s metadata does not support reshape\n",
663 return st
->ss
->reshape_super(st
, size
, level
, layout
, chunksize
,
664 raid_disks
, delta_disks
, backup_file
, dev
,
668 static void sync_metadata(struct supertype
*st
)
670 if (st
->ss
->external
) {
671 if (st
->update_tail
) {
672 flush_metadata_updates(st
);
673 st
->update_tail
= &st
->updates
;
675 st
->ss
->sync_metadata(st
);
679 static int subarray_set_num(char *container
, struct mdinfo
*sra
, char *name
, int n
)
681 /* when dealing with external metadata subarrays we need to be
682 * prepared to handle EAGAIN. The kernel may need to wait for
683 * mdmon to mark the array active so the kernel can handle
684 * allocations/writeback when preparing the reshape action
685 * (md_allow_write()). We temporarily disable safe_mode_delay
686 * to close a race with the array_state going clean before the
687 * next write to raid_disks / stripe_cache_size
692 /* only 'raid_disks' and 'stripe_cache_size' trigger md_allow_write */
694 (strcmp(name
, "raid_disks") != 0 &&
695 strcmp(name
, "stripe_cache_size") != 0))
696 return sysfs_set_num(sra
, NULL
, name
, n
);
698 rc
= sysfs_get_str(sra
, NULL
, "safe_mode_delay", safe
, sizeof(safe
));
701 sysfs_set_num(sra
, NULL
, "safe_mode_delay", 0);
702 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
703 if (rc
< 0 && errno
== EAGAIN
) {
704 ping_monitor(container
);
705 /* if we get EAGAIN here then the monitor is not active
708 rc
= sysfs_set_num(sra
, NULL
, name
, n
);
710 sysfs_set_str(sra
, NULL
, "safe_mode_delay", safe
);
714 int start_reshape(struct mdinfo
*sra
, int already_running
,
715 int before_data_disks
, int data_disks
)
718 unsigned long long sync_max_to_set
;
720 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
721 err
= sysfs_set_num(sra
, NULL
, "suspend_hi", sra
->reshape_progress
);
722 err
= err
?: sysfs_set_num(sra
, NULL
, "suspend_lo",
723 sra
->reshape_progress
);
724 if (before_data_disks
<= data_disks
)
725 sync_max_to_set
= sra
->reshape_progress
/ data_disks
;
727 sync_max_to_set
= (sra
->component_size
* data_disks
728 - sra
->reshape_progress
) / data_disks
;
729 if (!already_running
)
730 sysfs_set_num(sra
, NULL
, "sync_min", sync_max_to_set
);
731 err
= err
?: sysfs_set_num(sra
, NULL
, "sync_max", sync_max_to_set
);
732 if (!already_running
)
733 err
= err
?: sysfs_set_str(sra
, NULL
, "sync_action", "reshape");
738 void abort_reshape(struct mdinfo
*sra
)
740 sysfs_set_str(sra
, NULL
, "sync_action", "idle");
741 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
742 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
743 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
744 sysfs_set_num(sra
, NULL
, "sync_min", 0);
745 // It isn't safe to reset sync_max as we aren't monitoring.
746 // Array really should be stopped at this point.
749 int remove_disks_for_takeover(struct supertype
*st
,
754 struct mdinfo
*remaining
;
757 if (sra
->array
.level
== 10)
758 nr_of_copies
= layout
& 0xff;
759 else if (sra
->array
.level
== 1)
760 nr_of_copies
= sra
->array
.raid_disks
;
764 remaining
= sra
->devs
;
766 /* for each 'copy', select one device and remove from the list. */
767 for (slot
= 0; slot
< sra
->array
.raid_disks
; slot
+= nr_of_copies
) {
768 struct mdinfo
**diskp
;
771 /* Find a working device to keep */
772 for (diskp
= &remaining
; *diskp
; diskp
= &(*diskp
)->next
) {
773 struct mdinfo
*disk
= *diskp
;
775 if (disk
->disk
.raid_disk
< slot
)
777 if (disk
->disk
.raid_disk
>= slot
+ nr_of_copies
)
779 if (disk
->disk
.state
& (1<<MD_DISK_REMOVED
))
781 if (disk
->disk
.state
& (1<<MD_DISK_FAULTY
))
783 if (!(disk
->disk
.state
& (1<<MD_DISK_SYNC
)))
786 /* We have found a good disk to use! */
788 disk
->next
= sra
->devs
;
797 if (slot
< sra
->array
.raid_disks
) {
798 /* didn't find all slots */
804 sra
->devs
= remaining
;
808 /* Remove all 'remaining' devices from the array */
810 struct mdinfo
*sd
= remaining
;
811 remaining
= sd
->next
;
813 sysfs_set_str(sra
, sd
, "state", "faulty");
814 sysfs_set_str(sra
, sd
, "slot", "none");
815 /* for external metadata disks should be removed in mdmon */
816 if (!st
->ss
->external
)
817 sysfs_set_str(sra
, sd
, "state", "remove");
818 sd
->disk
.state
|= (1<<MD_DISK_REMOVED
);
819 sd
->disk
.state
&= ~(1<<MD_DISK_SYNC
);
820 sd
->next
= sra
->devs
;
826 void reshape_free_fdlist(int *fdlist
,
827 unsigned long long *offsets
,
832 for (i
= 0; i
< size
; i
++)
840 int reshape_prepare_fdlist(char *devname
,
844 unsigned long blocks
,
847 unsigned long long *offsets
)
853 for (d
= 0; d
<= nrdisks
; d
++)
856 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
857 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
859 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
860 char *dn
= map_dev(sd
->disk
.major
,
862 fdlist
[sd
->disk
.raid_disk
]
863 = dev_open(dn
, O_RDONLY
);
864 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
*512;
865 if (fdlist
[sd
->disk
.raid_disk
] < 0) {
866 pr_err("%s: cannot open component %s\n",
867 devname
, dn
? dn
: "-unknown-");
871 } else if (backup_file
== NULL
) {
873 char *dn
= map_dev(sd
->disk
.major
,
875 fdlist
[d
] = dev_open(dn
, O_RDWR
);
876 offsets
[d
] = (sd
->data_offset
+ sra
->component_size
- blocks
- 8)*512;
878 pr_err("%s: cannot open component %s\n",
879 devname
, dn
? dn
: "-unknown-");
890 int reshape_open_backup_file(char *backup_file
,
895 unsigned long long *offsets
,
899 /* Return 1 on success, 0 on any form of failure */
900 /* need to check backup file is large enough */
906 *fdlist
= open(backup_file
, O_RDWR
|O_CREAT
|(restart
? O_TRUNC
: O_EXCL
),
910 pr_err("%s: cannot create backup file %s: %s\n",
911 devname
, backup_file
, strerror(errno
));
914 /* Guard against backup file being on array device.
915 * If array is partitioned or if LVM etc is in the
916 * way this will not notice, but it is better than
919 fstat(*fdlist
, &stb
);
922 if (stb
.st_rdev
== dev
) {
923 pr_err("backup file must NOT be"
924 " on the array being reshaped.\n");
930 for (i
=0; i
< blocks
+ 8 ; i
++) {
931 if (write(*fdlist
, buf
, 512) != 512) {
932 pr_err("%s: cannot create"
933 " backup file %s: %s\n",
934 devname
, backup_file
, strerror(errno
));
938 if (fsync(*fdlist
) != 0) {
939 pr_err("%s: cannot create backup file %s: %s\n",
940 devname
, backup_file
, strerror(errno
));
944 if (!restart
&& strncmp(backup_file
, MAP_DIR
, strlen(MAP_DIR
)) != 0) {
945 char *bu
= make_backup(sys_name
);
946 symlink(backup_file
, bu
);
953 unsigned long compute_backup_blocks(int nchunk
, int ochunk
,
954 unsigned int ndata
, unsigned int odata
)
956 unsigned long a
, b
, blocks
;
957 /* So how much do we need to backup.
958 * We need an amount of data which is both a whole number of
959 * old stripes and a whole number of new stripes.
960 * So LCM for (chunksize*datadisks).
962 a
= (ochunk
/512) * odata
;
963 b
= (nchunk
/512) * ndata
;
966 /* LCM == product / GCD */
967 blocks
= (ochunk
/512) * (nchunk
/512) * odata
* ndata
/ a
;
972 char *analyse_change(char *devname
, struct mdinfo
*info
, struct reshape
*re
)
974 /* Based on the current array state in info->array and
975 * the changes in info->new_* etc, determine:
976 * - whether the change is possible
977 * - Intermediate level/raid_disks/layout
978 * - whether a restriping reshape is needed
979 * - number of sectors in minimum change unit. This
980 * will cover a whole number of stripes in 'before' and
983 * Return message if the change should be rejected
984 * NULL if the change can be achieved
986 * This can be called as part of starting a reshape, or
987 * when assembling an array that is undergoing reshape.
989 int near
, far
, offset
, copies
;
991 int old_chunk
, new_chunk
;
992 /* delta_parity records change in number of devices
993 * caused by level change
995 int delta_parity
= 0;
997 memset(re
, 0, sizeof(*re
));
999 /* If a new level not explicitly given, we assume no-change */
1000 if (info
->new_level
== UnSet
)
1001 info
->new_level
= info
->array
.level
;
1003 if (info
->new_chunk
)
1004 switch (info
->new_level
) {
1010 /* chunk size is meaningful, must divide component_size
1013 if (info
->component_size
% (info
->new_chunk
/512)) {
1014 unsigned long long shrink
= info
->component_size
;
1015 shrink
&= ~(unsigned long long)(info
->new_chunk
/512-1);
1016 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk)\n",
1017 info
->new_chunk
/1024, info
->component_size
/2);
1018 pr_err("After shrinking any filesystem, \"mdadm --grow %s --size %llu\"\n",
1020 pr_err("will shrink the array so the given chunk size would work.\n");
1025 return "chunk size not meaningful for this level";
1028 info
->new_chunk
= info
->array
.chunk_size
;
1030 switch (info
->array
.level
) {
1032 return "Cannot understand this RAID level";
1034 /* RAID1 can convert to RAID1 with different disks, or
1035 * raid5 with 2 disks, or
1038 if (info
->new_level
> 1 &&
1039 (info
->component_size
& 7))
1040 return "Cannot convert RAID1 of this size - "
1041 "reduce size to multiple of 4K first.";
1042 if (info
->new_level
== 0) {
1043 if (info
->delta_disks
!= UnSet
&&
1044 info
->delta_disks
!= 0)
1045 return "Cannot change number of disks "
1046 "with RAID1->RAID0 conversion";
1048 re
->before
.data_disks
= 1;
1049 re
->after
.data_disks
= 1;
1052 if (info
->new_level
== 1) {
1053 if (info
->delta_disks
== UnSet
)
1054 /* Don't know what to do */
1055 return "no change requested for Growing RAID1";
1059 if (info
->array
.raid_disks
== 2 &&
1060 info
->new_level
== 5) {
1063 re
->before
.data_disks
= 1;
1064 if (info
->delta_disks
!= UnSet
&&
1065 info
->delta_disks
!= 0)
1066 re
->after
.data_disks
= 1 + info
->delta_disks
;
1068 re
->after
.data_disks
= 1;
1069 if (re
->after
.data_disks
< 1)
1070 return "Number of disks too small for RAID5";
1072 re
->before
.layout
= ALGORITHM_LEFT_SYMMETRIC
;
1073 info
->array
.chunk_size
= 65536;
1076 /* Could do some multi-stage conversions, but leave that to
1079 return "Impossibly level change request for RAID1";
1082 /* RAID10 can be converted from near mode to
1083 * RAID0 by removing some devices.
1084 * It can also be reshaped if the kernel supports
1087 switch (info
->new_level
) {
1089 if ((info
->array
.layout
& ~0xff) != 0x100)
1090 return "Cannot Grow RAID10 with far/offset layout";
1091 /* number of devices must be multiple of number of copies */
1092 if (info
->array
.raid_disks
% (info
->array
.layout
& 0xff))
1093 return "RAID10 layout too complex for Grow operation";
1095 new_disks
= (info
->array
.raid_disks
1096 / (info
->array
.layout
& 0xff));
1097 if (info
->delta_disks
== UnSet
)
1098 info
->delta_disks
= (new_disks
1099 - info
->array
.raid_disks
);
1101 if (info
->delta_disks
!= new_disks
- info
->array
.raid_disks
)
1102 return "New number of raid-devices impossible for RAID10";
1103 if (info
->new_chunk
&&
1104 info
->new_chunk
!= info
->array
.chunk_size
)
1105 return "Cannot change chunk-size with RAID10 Grow";
1109 re
->before
.data_disks
= new_disks
;
1110 re
->after
.data_disks
= re
->before
.data_disks
;
1114 near
= info
->array
.layout
& 0xff;
1115 far
= (info
->array
.layout
>> 8) & 0xff;
1116 offset
= info
->array
.layout
& 0x10000;
1117 if (far
> 1 && !offset
)
1118 return "Cannot reshape RAID10 in far-mode";
1119 copies
= near
* far
;
1121 old_chunk
= info
->array
.chunk_size
* far
;
1123 if (info
->new_layout
== UnSet
)
1124 info
->new_layout
= info
->array
.layout
;
1126 near
= info
->new_layout
& 0xff;
1127 far
= (info
->new_layout
>> 8) & 0xff;
1128 offset
= info
->new_layout
& 0x10000;
1129 if (far
> 1 && !offset
)
1130 return "Cannot reshape RAID10 to far-mode";
1131 if (near
* far
!= copies
)
1132 return "Cannot change number of copies"
1133 " when reshaping RAID10";
1135 if (info
->delta_disks
== UnSet
)
1136 info
->delta_disks
= 0;
1137 new_disks
= (info
->array
.raid_disks
+
1140 new_chunk
= info
->new_chunk
* far
;
1143 re
->before
.layout
= info
->array
.layout
;
1144 re
->before
.data_disks
= info
->array
.raid_disks
;
1145 re
->after
.layout
= info
->new_layout
;
1146 re
->after
.data_disks
= new_disks
;
1147 /* For RAID10 we don't do backup but do allow reshape,
1148 * so set backup_blocks to INVALID_SECTORS rather than
1150 * And there is no need to synchronise stripes on both
1151 * 'old' and 'new'. So the important
1152 * number is the minimum data_offset difference
1153 * which is the larger of (offset copies * chunk).
1155 re
->backup_blocks
= INVALID_SECTORS
;
1156 re
->min_offset_change
= max(old_chunk
, new_chunk
) / 512;
1157 if (new_disks
< re
->before
.data_disks
&&
1158 info
->space_after
< re
->min_offset_change
)
1159 /* Reduce component size by one chunk */
1160 re
->new_size
= (info
->component_size
-
1161 re
->min_offset_change
);
1163 re
->new_size
= info
->component_size
;
1164 re
->new_size
= re
->new_size
* new_disks
/ copies
;
1168 return "RAID10 can only be changed to RAID0";
1171 /* RAID0 can be converted to RAID10, or to RAID456 */
1172 if (info
->new_level
== 10) {
1173 if (info
->new_layout
== UnSet
&& info
->delta_disks
== UnSet
) {
1174 /* Assume near=2 layout */
1175 info
->new_layout
= 0x102;
1176 info
->delta_disks
= info
->array
.raid_disks
;
1178 if (info
->new_layout
== UnSet
) {
1179 int copies
= 1 + (info
->delta_disks
1180 / info
->array
.raid_disks
);
1181 if (info
->array
.raid_disks
* (copies
-1)
1182 != info
->delta_disks
)
1183 return "Impossible number of devices"
1184 " for RAID0->RAID10";
1185 info
->new_layout
= 0x100 + copies
;
1187 if (info
->delta_disks
== UnSet
) {
1188 int copies
= info
->new_layout
& 0xff;
1189 if (info
->new_layout
!= 0x100 + copies
)
1190 return "New layout impossible"
1191 " for RAID0->RAID10";;
1192 info
->delta_disks
= (copies
- 1) *
1193 info
->array
.raid_disks
;
1195 if (info
->new_chunk
&&
1196 info
->new_chunk
!= info
->array
.chunk_size
)
1197 return "Cannot change chunk-size with RAID0->RAID10";
1200 re
->before
.data_disks
= (info
->array
.raid_disks
+
1202 re
->after
.data_disks
= re
->before
.data_disks
;
1203 re
->before
.layout
= info
->new_layout
;
1207 /* RAID0 can also covert to RAID0/4/5/6 by first converting to
1208 * a raid4 style layout of the final level.
1210 switch (info
->new_level
) {
1215 re
->before
.layout
= 0;
1220 re
->before
.layout
= ALGORITHM_PARITY_N
;
1221 if (info
->new_layout
== UnSet
)
1222 info
->new_layout
= map_name(r5layout
, "default");
1227 re
->before
.layout
= ALGORITHM_PARITY_N
;
1228 if (info
->new_layout
== UnSet
)
1229 info
->new_layout
= map_name(r6layout
, "default");
1232 return "Impossible level change requested";
1234 re
->before
.data_disks
= info
->array
.raid_disks
;
1235 /* determining 'after' layout happens outside this 'switch' */
1239 info
->array
.layout
= ALGORITHM_PARITY_N
;
1241 switch (info
->new_level
) {
1245 re
->level
= info
->array
.level
;
1246 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1247 re
->before
.layout
= info
->array
.layout
;
1251 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1252 re
->before
.layout
= info
->array
.layout
;
1257 re
->before
.data_disks
= info
->array
.raid_disks
- 1;
1258 switch (info
->array
.layout
) {
1259 case ALGORITHM_LEFT_ASYMMETRIC
:
1260 re
->before
.layout
= ALGORITHM_LEFT_ASYMMETRIC_6
;
1262 case ALGORITHM_RIGHT_ASYMMETRIC
:
1263 re
->before
.layout
= ALGORITHM_RIGHT_ASYMMETRIC_6
;
1265 case ALGORITHM_LEFT_SYMMETRIC
:
1266 re
->before
.layout
= ALGORITHM_LEFT_SYMMETRIC_6
;
1268 case ALGORITHM_RIGHT_SYMMETRIC
:
1269 re
->before
.layout
= ALGORITHM_RIGHT_SYMMETRIC_6
;
1271 case ALGORITHM_PARITY_0
:
1272 re
->before
.layout
= ALGORITHM_PARITY_0_6
;
1274 case ALGORITHM_PARITY_N
:
1275 re
->before
.layout
= ALGORITHM_PARITY_N_6
;
1278 return "Cannot convert an array with this layout";
1282 if (info
->array
.raid_disks
!= 2)
1283 return "Can only convert a 2-device array to RAID1";
1284 if (info
->delta_disks
!= UnSet
&&
1285 info
->delta_disks
!= 0)
1286 return "Cannot set raid_disk when "
1287 "converting RAID5->RAID1";
1289 info
->new_chunk
= 0;
1292 return "Impossible level change requested";
1296 switch (info
->new_level
) {
1302 re
->before
.data_disks
= info
->array
.raid_disks
- 2;
1303 re
->before
.layout
= info
->array
.layout
;
1306 return "Impossible level change requested";
1311 /* If we reached here then it looks like a re-stripe is
1312 * happening. We have determined the intermediate level
1313 * and initial raid_disks/layout and stored these in 're'.
1315 * We need to deduce the final layout that can be atomically
1316 * converted to the end state.
1318 switch (info
->new_level
) {
1320 /* We can only get to RAID0 from RAID4 or RAID5
1321 * with appropriate layout and one extra device
1323 if (re
->level
!= 4 && re
->level
!= 5)
1324 return "Cannot covert to RAID0 from this level";
1326 switch (re
->level
) {
1328 re
->before
.layout
= 0;
1329 re
->after
.layout
= 0;
1332 re
->after
.layout
= ALGORITHM_PARITY_N
;
1338 /* We can only get to RAID4 from RAID5 */
1339 if (re
->level
!= 4 && re
->level
!= 5)
1340 return "Cannot convert to RAID4 from this level";
1342 switch (re
->level
) {
1344 re
->before
.layout
= 0;
1345 re
->after
.layout
= 0;
1348 re
->after
.layout
= ALGORITHM_PARITY_N
;
1354 /* We get to RAID5 from RAID5 or RAID6 */
1355 if (re
->level
!= 5 && re
->level
!= 6)
1356 return "Cannot convert to RAID5 from this level";
1358 switch (re
->level
) {
1360 if (info
->new_layout
== UnSet
)
1361 re
->after
.layout
= re
->before
.layout
;
1363 re
->after
.layout
= info
->new_layout
;
1366 if (info
->new_layout
== UnSet
)
1367 info
->new_layout
= re
->before
.layout
;
1369 /* after.layout needs to be raid6 version of new_layout */
1370 if (info
->new_layout
== ALGORITHM_PARITY_N
)
1371 re
->after
.layout
= ALGORITHM_PARITY_N
;
1374 char *ls
= map_num(r5layout
, info
->new_layout
);
1377 /* Current RAID6 layout has a RAID5
1380 strcat(strcpy(layout
, ls
), "-6");
1381 l
= map_name(r6layout
, layout
);
1383 return "Cannot find RAID6 layout"
1386 /* Current RAID6 has no equivalent.
1387 * If it is already a '-6' layout we
1388 * can leave it unchanged, else we must
1391 ls
= map_num(r6layout
, info
->new_layout
);
1393 strcmp(ls
+strlen(ls
)-2, "-6") != 0)
1394 return "Please specify new layout";
1395 l
= info
->new_layout
;
1397 re
->after
.layout
= l
;
1403 /* We must already be at level 6 */
1405 return "Impossible level change";
1406 if (info
->new_layout
== UnSet
)
1407 re
->after
.layout
= info
->array
.layout
;
1409 re
->after
.layout
= info
->new_layout
;
1412 return "Impossible level change requested";
1414 if (info
->delta_disks
== UnSet
)
1415 info
->delta_disks
= delta_parity
;
1417 re
->after
.data_disks
= (re
->before
.data_disks
1420 switch (re
->level
) {
1421 case 6: re
->parity
= 2;
1424 case 5: re
->parity
= 1;
1426 default: re
->parity
= 0;
1429 /* So we have a restripe operation, we need to calculate the number
1430 * of blocks per reshape operation.
1432 re
->new_size
= info
->component_size
* re
->before
.data_disks
;
1433 if (info
->new_chunk
== 0)
1434 info
->new_chunk
= info
->array
.chunk_size
;
1435 if (re
->after
.data_disks
== re
->before
.data_disks
&&
1436 re
->after
.layout
== re
->before
.layout
&&
1437 info
->new_chunk
== info
->array
.chunk_size
) {
1438 /* Nothing to change, can change level immediately. */
1439 re
->level
= info
->new_level
;
1440 re
->backup_blocks
= 0;
1443 if (re
->after
.data_disks
== 1 && re
->before
.data_disks
== 1) {
1444 /* chunk and layout changes make no difference */
1445 re
->level
= info
->new_level
;
1446 re
->backup_blocks
= 0;
1450 if (re
->after
.data_disks
== re
->before
.data_disks
&&
1451 get_linux_version() < 2006032)
1452 return "in-place reshape is not safe before 2.6.32 - sorry.";
1454 if (re
->after
.data_disks
< re
->before
.data_disks
&&
1455 get_linux_version() < 2006030)
1456 return "reshape to fewer devices is not supported before 2.6.30 - sorry.";
1458 re
->backup_blocks
= compute_backup_blocks(
1459 info
->new_chunk
, info
->array
.chunk_size
,
1460 re
->after
.data_disks
,
1461 re
->before
.data_disks
);
1462 re
->min_offset_change
= re
->backup_blocks
/ re
->before
.data_disks
;
1464 re
->new_size
= info
->component_size
* re
->after
.data_disks
;
1468 static int set_array_size(struct supertype
*st
, struct mdinfo
*sra
,
1471 struct mdinfo
*info
;
1475 if ((st
== NULL
) || (sra
== NULL
))
1478 if (text_version
== NULL
)
1479 text_version
= sra
->text_version
;
1480 subarray
= strchr(text_version
+1, '/')+1;
1481 info
= st
->ss
->container_content(st
, subarray
);
1483 unsigned long long current_size
= 0;
1484 unsigned long long new_size
=
1485 info
->custom_array_size
/2;
1487 if (sysfs_get_ll(sra
, NULL
, "array_size", ¤t_size
) == 0 &&
1488 new_size
> current_size
) {
1489 if (sysfs_set_num(sra
, NULL
, "array_size", new_size
)
1491 dprintf("Error: Cannot set array size");
1494 dprintf("Array size changed");
1496 dprintf(" from %llu to %llu.\n",
1497 current_size
, new_size
);
1501 dprintf("Error: set_array_size(): info pointer in NULL\n");
1506 static int reshape_array(char *container
, int fd
, char *devname
,
1507 struct supertype
*st
, struct mdinfo
*info
,
1508 int force
, struct mddev_dev
*devlist
,
1509 unsigned long long data_offset
,
1510 char *backup_file
, int verbose
, int forked
,
1511 int restart
, int freeze_reshape
);
1512 static int reshape_container(char *container
, char *devname
,
1514 struct supertype
*st
,
1515 struct mdinfo
*info
,
1517 char *backup_file
, int verbose
,
1518 int forked
, int restart
, int freeze_reshape
);
1520 int Grow_reshape(char *devname
, int fd
,
1521 struct mddev_dev
*devlist
,
1522 unsigned long long data_offset
,
1523 struct context
*c
, struct shape
*s
)
1525 /* Make some changes in the shape of an array.
1526 * The kernel must support the change.
1528 * There are three different changes. Each can trigger
1529 * a resync or recovery so we freeze that until we have
1530 * requested everything (if kernel supports freezing - 2.6.30).
1532 * - change size (i.e. component_size)
1534 * - change layout/chunksize/ndisks
1536 * The last can require a reshape. It is different on different
1537 * levels so we need to check the level before actioning it.
1538 * Some times the level change needs to be requested after the
1539 * reshape (e.g. raid6->raid5, raid5->raid0)
1542 struct mdu_array_info_s array
;
1544 struct supertype
*st
;
1545 char *subarray
= NULL
;
1549 char *container
= NULL
;
1552 struct mddev_dev
*dv
;
1558 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) < 0) {
1559 pr_err("%s is not an active md array - aborting\n",
1563 if (data_offset
!= INVALID_SECTORS
&& array
.level
!= 10
1564 && (array
.level
< 4 || array
.level
> 6)) {
1565 pr_err("--grow --data-offset not yet supported\n");
1570 (s
->chunk
|| s
->level
!= UnSet
|| s
->layout_str
|| s
->raiddisks
)) {
1571 pr_err("cannot change component size at the same time "
1572 "as other changes.\n"
1573 " Change size first, then check data is intact before "
1574 "making other changes.\n");
1578 if (s
->raiddisks
&& s
->raiddisks
< array
.raid_disks
&& array
.level
> 1 &&
1579 get_linux_version() < 2006032 &&
1580 !check_env("MDADM_FORCE_FEWER")) {
1581 pr_err("reducing the number of devices is not safe before Linux 2.6.32\n"
1582 " Please use a newer kernel\n");
1586 st
= super_by_fd(fd
, &subarray
);
1588 pr_err("Unable to determine metadata format for %s\n", devname
);
1591 if (s
->raiddisks
> st
->max_devs
) {
1592 pr_err("Cannot increase raid-disks on this array"
1593 " beyond %d\n", st
->max_devs
);
1597 /* in the external case we need to check that the requested reshape is
1598 * supported, and perform an initial check that the container holds the
1599 * pre-requisite spare devices (mdmon owns final validation)
1601 if (st
->ss
->external
) {
1605 container
= st
->container_devnm
;
1606 cfd
= open_dev_excl(st
->container_devnm
);
1608 container
= st
->devnm
;
1610 cfd
= open_dev_excl(st
->devnm
);
1614 pr_err("Unable to open container for %s\n",
1620 rv
= st
->ss
->load_container(st
, cfd
, NULL
);
1623 pr_err("Cannot read superblock for %s\n",
1629 /* check if operation is supported for metadata handler */
1630 if (st
->ss
->container_content
) {
1631 struct mdinfo
*cc
= NULL
;
1632 struct mdinfo
*content
= NULL
;
1634 cc
= st
->ss
->container_content(st
, subarray
);
1635 for (content
= cc
; content
; content
= content
->next
) {
1636 int allow_reshape
= 1;
1638 /* check if reshape is allowed based on metadata
1639 * indications stored in content.array.status
1641 if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
))
1643 if (content
->array
.state
1644 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
))
1646 if (!allow_reshape
) {
1647 pr_err("cannot reshape arrays in"
1648 " container with unsupported"
1649 " metadata: %s(%s)\n",
1650 devname
, container
);
1658 if (mdmon_running(container
))
1659 st
->update_tail
= &st
->updates
;
1663 for (dv
= devlist
; dv
; dv
= dv
->next
)
1665 if (s
->raiddisks
> array
.raid_disks
&&
1666 array
.spare_disks
+added_disks
< (s
->raiddisks
- array
.raid_disks
) &&
1668 pr_err("Need %d spare%s to avoid degraded array,"
1669 " and only have %d.\n"
1670 " Use --force to over-ride this check.\n",
1671 s
->raiddisks
- array
.raid_disks
,
1672 s
->raiddisks
- array
.raid_disks
== 1 ? "" : "s",
1673 array
.spare_disks
+ added_disks
);
1677 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
| GET_DISKS
| GET_DEVS
1678 | GET_STATE
| GET_VERSION
);
1680 if (st
->ss
->external
&& subarray
== NULL
) {
1681 array
.level
= LEVEL_CONTAINER
;
1682 sra
->array
.level
= LEVEL_CONTAINER
;
1685 pr_err("failed to read sysfs parameters for %s\n",
1689 frozen
= freeze(st
);
1691 /* freeze() already spewed the reason */
1694 } else if (frozen
< 0) {
1695 pr_err("%s is performing resync/recovery and cannot"
1696 " be reshaped\n", devname
);
1701 /* ========= set size =============== */
1702 if (s
->size
> 0 && (s
->size
== MAX_SIZE
|| s
->size
!= (unsigned)array
.size
)) {
1703 unsigned long long orig_size
= get_component_size(fd
)/2;
1704 unsigned long long min_csize
;
1706 int raid0_takeover
= 0;
1709 orig_size
= (unsigned) array
.size
;
1711 if (orig_size
== 0) {
1712 pr_err("Cannot set device size in this type of array.\n");
1717 if (reshape_super(st
, s
->size
, UnSet
, UnSet
, 0, 0, UnSet
, NULL
,
1718 devname
, APPLY_METADATA_CHANGES
, c
->verbose
> 0)) {
1723 if (st
->ss
->external
) {
1724 /* metadata can have size limitation
1725 * update size value according to metadata information
1727 struct mdinfo
*sizeinfo
=
1728 st
->ss
->container_content(st
, subarray
);
1730 unsigned long long new_size
=
1731 sizeinfo
->custom_array_size
/2;
1732 int data_disks
= get_data_disks(
1733 sizeinfo
->array
.level
,
1734 sizeinfo
->array
.layout
,
1735 sizeinfo
->array
.raid_disks
);
1736 new_size
/= data_disks
;
1737 dprintf("Metadata size correction from %llu to "
1738 "%llu (%llu)\n", orig_size
, new_size
,
1739 new_size
* data_disks
);
1741 sysfs_free(sizeinfo
);
1745 /* Update the size of each member device in case
1746 * they have been resized. This will never reduce
1747 * below the current used-size. The "size" attribute
1748 * understands '0' to mean 'max'.
1752 for (mdi
= sra
->devs
; mdi
; mdi
= mdi
->next
) {
1753 if (sysfs_set_num(sra
, mdi
, "size",
1754 s
->size
== MAX_SIZE
? 0 : s
->size
) < 0) {
1755 /* Probably kernel refusing to let us
1756 * reduce the size - not an error.
1760 if (array
.not_persistent
== 0 &&
1761 array
.major_version
== 0 &&
1762 get_linux_version() < 3001000) {
1763 /* Dangerous to allow size to exceed 2TB */
1764 unsigned long long csize
;
1765 if (sysfs_get_ll(sra
, mdi
, "size", &csize
) == 0) {
1766 if (csize
>= 2ULL*1024*1024*1024)
1767 csize
= 2ULL*1024*1024*1024;
1768 if ((min_csize
== 0 || (min_csize
1775 pr_err("Cannot set size on "
1776 "array members.\n");
1777 goto size_change_error
;
1779 if (min_csize
&& s
->size
> min_csize
) {
1780 pr_err("Cannot safely make this array "
1781 "use more than 2TB per device on this kernel.\n");
1783 goto size_change_error
;
1785 if (min_csize
&& s
->size
== MAX_SIZE
) {
1786 /* Don't let the kernel choose a size - it will get
1789 pr_err("Limited v0.90 array to "
1790 "2TB per device\n");
1791 s
->size
= min_csize
;
1793 if (st
->ss
->external
) {
1794 if (sra
->array
.level
== 0) {
1795 rv
= sysfs_set_str(sra
, NULL
, "level",
1799 /* get array parametes after takeover
1800 * to chane one parameter at time only
1802 rv
= ioctl(fd
, GET_ARRAY_INFO
, &array
);
1805 /* make sure mdmon is
1806 * aware of the new level */
1807 if (!mdmon_running(st
->container_devnm
))
1808 start_mdmon(st
->container_devnm
);
1809 ping_monitor(container
);
1810 if (mdmon_running(st
->container_devnm
) &&
1811 st
->update_tail
== NULL
)
1812 st
->update_tail
= &st
->updates
;
1815 if (s
->size
== MAX_SIZE
)
1817 array
.size
= s
->size
;
1818 if ((unsigned)array
.size
!= s
->size
) {
1819 /* got truncated to 32bit, write to
1820 * component_size instead
1823 rv
= sysfs_set_num(sra
, NULL
,
1824 "component_size", s
->size
);
1828 rv
= ioctl(fd
, SET_ARRAY_INFO
, &array
);
1830 /* manage array size when it is managed externally
1832 if ((rv
== 0) && st
->ss
->external
)
1833 rv
= set_array_size(st
, sra
, sra
->text_version
);
1836 if (raid0_takeover
) {
1837 /* do not recync non-existing parity,
1838 * we will drop it anyway
1840 sysfs_set_str(sra
, NULL
, "sync_action", "frozen");
1841 /* go back to raid0, drop parity disk
1843 sysfs_set_str(sra
, NULL
, "level", "raid0");
1844 ioctl(fd
, GET_ARRAY_INFO
, &array
);
1851 /* restore metadata */
1852 if (reshape_super(st
, orig_size
, UnSet
, UnSet
, 0, 0,
1853 UnSet
, NULL
, devname
,
1854 ROLLBACK_METADATA_CHANGES
,
1857 pr_err("Cannot set device size for %s: %s\n",
1858 devname
, strerror(err
));
1860 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
1861 cont_err("Bitmap must be removed before size can be changed\n");
1865 if (s
->assume_clean
) {
1866 /* This will fail on kernels older than 3.0 unless
1867 * a backport has been arranged.
1870 sysfs_set_str(sra
, NULL
, "resync_start", "none") < 0)
1871 pr_err("--assume-clean not supported with --grow on this kernel\n");
1873 ioctl(fd
, GET_ARRAY_INFO
, &array
);
1874 s
->size
= get_component_size(fd
)/2;
1876 s
->size
= array
.size
;
1877 if (c
->verbose
>= 0) {
1878 if (s
->size
== orig_size
)
1879 pr_err("component size of %s "
1880 "unchanged at %lluK\n",
1883 pr_err("component size of %s "
1884 "has been set to %lluK\n",
1888 } else if (array
.level
!= LEVEL_CONTAINER
) {
1889 s
->size
= get_component_size(fd
)/2;
1891 s
->size
= array
.size
;
1894 /* See if there is anything else to do */
1895 if ((s
->level
== UnSet
|| s
->level
== array
.level
) &&
1896 (s
->layout_str
== NULL
) &&
1897 (s
->chunk
== 0 || s
->chunk
== array
.chunk_size
) &&
1898 data_offset
== INVALID_SECTORS
&&
1899 (s
->raiddisks
== 0 || s
->raiddisks
== array
.raid_disks
)) {
1900 /* Nothing more to do */
1901 if (!changed
&& c
->verbose
>= 0)
1902 pr_err("%s: no change requested\n",
1907 /* ========= check for Raid10/Raid1 -> Raid0 conversion ===============
1908 * current implementation assumes that following conditions must be met:
1911 * - near_copies == 2
1913 if ((s
->level
== 0 && array
.level
== 10 && sra
&&
1914 array
.layout
== ((1 << 8) + 2) && !(array
.raid_disks
& 1)) ||
1915 (s
->level
== 0 && array
.level
== 1 && sra
)) {
1917 err
= remove_disks_for_takeover(st
, sra
, array
.layout
);
1919 dprintf(Name
": Array cannot be reshaped\n");
1925 /* Make sure mdmon has seen the device removal
1926 * and updated metadata before we continue with
1930 ping_monitor(container
);
1933 memset(&info
, 0, sizeof(info
));
1935 sysfs_init(&info
, fd
, NULL
);
1936 strcpy(info
.text_version
, sra
->text_version
);
1937 info
.component_size
= s
->size
*2;
1938 info
.new_level
= s
->level
;
1939 info
.new_chunk
= s
->chunk
* 1024;
1940 if (info
.array
.level
== LEVEL_CONTAINER
) {
1941 info
.delta_disks
= UnSet
;
1942 info
.array
.raid_disks
= s
->raiddisks
;
1943 } else if (s
->raiddisks
)
1944 info
.delta_disks
= s
->raiddisks
- info
.array
.raid_disks
;
1946 info
.delta_disks
= UnSet
;
1947 if (s
->layout_str
== NULL
) {
1948 info
.new_layout
= UnSet
;
1949 if (info
.array
.level
== 6 &&
1950 (info
.new_level
== 6 || info
.new_level
== UnSet
) &&
1951 info
.array
.layout
>= 16) {
1952 pr_err("%s has a non-standard layout. If you"
1953 " wish to preserve this\n", devname
);
1954 cont_err("during the reshape, please specify"
1955 " --layout=preserve\n");
1956 cont_err("If you want to change it, specify a"
1957 " layout or use --layout=normalise\n");
1961 } else if (strcmp(s
->layout_str
, "normalise") == 0 ||
1962 strcmp(s
->layout_str
, "normalize") == 0) {
1963 /* If we have a -6 RAID6 layout, remove the '-6'. */
1964 info
.new_layout
= UnSet
;
1965 if (info
.array
.level
== 6 && info
.new_level
== UnSet
) {
1967 strcpy(l
, map_num(r6layout
, info
.array
.layout
));
1968 h
= strrchr(l
, '-');
1969 if (h
&& strcmp(h
, "-6") == 0) {
1971 info
.new_layout
= map_name(r6layout
, l
);
1974 pr_err("%s is only meaningful when reshaping"
1975 " a RAID6 array.\n", s
->layout_str
);
1979 } else if (strcmp(s
->layout_str
, "preserve") == 0) {
1980 /* This means that a non-standard RAID6 layout
1983 * - When reshape a RAID6 (e.g. adding a device)
1984 * which is in a non-standard layout, it is OK
1985 * to preserve that layout.
1986 * - When converting a RAID5 to RAID6, leave it in
1987 * the XXX-6 layout, don't re-layout.
1989 if (info
.array
.level
== 6 && info
.new_level
== UnSet
)
1990 info
.new_layout
= info
.array
.layout
;
1991 else if (info
.array
.level
== 5 && info
.new_level
== 6) {
1993 strcpy(l
, map_num(r5layout
, info
.array
.layout
));
1995 info
.new_layout
= map_name(r6layout
, l
);
1997 pr_err("%s in only meaningful when reshaping"
1998 " to RAID6\n", s
->layout_str
);
2003 int l
= info
.new_level
;
2005 l
= info
.array
.level
;
2008 info
.new_layout
= map_name(r5layout
, s
->layout_str
);
2011 info
.new_layout
= map_name(r6layout
, s
->layout_str
);
2014 info
.new_layout
= parse_layout_10(s
->layout_str
);
2017 info
.new_layout
= parse_layout_faulty(s
->layout_str
);
2020 pr_err("layout not meaningful"
2021 " with this level\n");
2025 if (info
.new_layout
== UnSet
) {
2026 pr_err("layout %s not understood"
2027 " for this level\n",
2034 if (array
.level
== LEVEL_FAULTY
) {
2035 if (s
->level
!= UnSet
&& s
->level
!= array
.level
) {
2036 pr_err("cannot change level of Faulty device\n");
2040 pr_err("cannot set chunksize of Faulty device\n");
2043 if (s
->raiddisks
&& s
->raiddisks
!= 1) {
2044 pr_err("cannot set raid_disks of Faulty device\n");
2047 if (s
->layout_str
) {
2048 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
2049 dprintf("Cannot get array information.\n");
2052 array
.layout
= info
.new_layout
;
2053 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2054 pr_err("failed to set new layout\n");
2056 } else if (c
->verbose
>= 0)
2057 printf("layout for %s set to %d\n",
2058 devname
, array
.layout
);
2060 } else if (array
.level
== LEVEL_CONTAINER
) {
2061 /* This change is to be applied to every array in the
2062 * container. This is only needed when the metadata imposes
2063 * restraints of the various arrays in the container.
2064 * Currently we only know that IMSM requires all arrays
2065 * to have the same number of devices so changing the
2066 * number of devices (On-Line Capacity Expansion) must be
2067 * performed at the level of the container
2069 rv
= reshape_container(container
, devname
, -1, st
, &info
,
2070 c
->force
, c
->backup_file
, c
->verbose
, 0, 0, 0);
2073 /* get spare devices from external metadata
2075 if (st
->ss
->external
) {
2076 struct mdinfo
*info2
;
2078 info2
= st
->ss
->container_content(st
, subarray
);
2080 info
.array
.spare_disks
=
2081 info2
->array
.spare_disks
;
2086 /* Impose these changes on a single array. First
2087 * check that the metadata is OK with the change. */
2089 if (reshape_super(st
, 0, info
.new_level
,
2090 info
.new_layout
, info
.new_chunk
,
2091 info
.array
.raid_disks
, info
.delta_disks
,
2092 c
->backup_file
, devname
, APPLY_METADATA_CHANGES
,
2098 rv
= reshape_array(container
, fd
, devname
, st
, &info
, c
->force
,
2099 devlist
, data_offset
, c
->backup_file
, c
->verbose
,
2110 /* verify_reshape_position()
2111 * Function checks if reshape position in metadata is not farther
2112 * than position in md.
2114 * 0 : not valid sysfs entry
2115 * it can be caused by not started reshape, it should be started
2116 * by reshape array or raid0 array is before takeover
2117 * -1 : error, reshape position is obviously wrong
2118 * 1 : success, reshape progress correct or updated
2120 static int verify_reshape_position(struct mdinfo
*info
, int level
)
2126 /* read sync_max, failure can mean raid0 array */
2127 rv
= sysfs_get_str(info
, NULL
, "sync_max", buf
, 40);
2131 unsigned long long position
= strtoull(buf
, &ep
, 0);
2133 dprintf(Name
": Read sync_max sysfs entry is: %s\n", buf
);
2134 if (!(ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))) {
2135 position
*= get_data_disks(level
,
2137 info
->array
.raid_disks
);
2138 if (info
->reshape_progress
< position
) {
2139 dprintf("Corrected reshape progress (%llu) to "
2140 "md position (%llu)\n",
2141 info
->reshape_progress
, position
);
2142 info
->reshape_progress
= position
;
2144 } else if (info
->reshape_progress
> position
) {
2145 pr_err("Fatal error: array "
2146 "reshape was not properly frozen "
2147 "(expected reshape position is %llu, "
2148 "but reshape progress is %llu.\n",
2149 position
, info
->reshape_progress
);
2152 dprintf("Reshape position in md and metadata "
2157 } else if (rv
== 0) {
2158 /* for valid sysfs entry, 0-length content
2159 * should be indicated as error
2167 static unsigned long long choose_offset(unsigned long long lo
,
2168 unsigned long long hi
,
2169 unsigned long long min
,
2170 unsigned long long max
)
2172 /* Choose a new offset between hi and lo.
2173 * It must be between min and max, but
2174 * we would prefer something near the middle of hi/lo, and also
2175 * prefer to be aligned to a big power of 2.
2177 * So we start with the middle, then for each bit,
2178 * starting at '1' and increasing, if it is set, we either
2179 * add it or subtract it if possible, preferring the option
2180 * which is furthest from the boundary.
2182 * We stop once we get a 1MB alignment. As units are in sectors,
2183 * 1MB = 2*1024 sectors.
2185 unsigned long long choice
= (lo
+ hi
) / 2;
2186 unsigned long long bit
= 1;
2188 for (bit
= 1; bit
< 2*1024; bit
= bit
<< 1) {
2189 unsigned long long bigger
, smaller
;
2190 if (! (bit
& choice
))
2192 bigger
= choice
+ bit
;
2193 smaller
= choice
- bit
;
2194 if (bigger
> max
&& smaller
< min
)
2198 else if (smaller
< min
)
2200 else if (hi
- bigger
> smaller
- lo
)
2208 static int set_new_data_offset(struct mdinfo
*sra
, struct supertype
*st
,
2209 char *devname
, int delta_disks
,
2210 unsigned long long data_offset
,
2211 unsigned long long min
,
2217 unsigned long long before
, after
;
2219 /* Need to find min space before and after so same is used
2222 before
= UINT64_MAX
;
2224 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2228 struct supertype
*st2
;
2229 struct mdinfo info2
;
2231 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2233 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2234 dfd
= dev_open(dn
, O_RDONLY
);
2236 pr_err("%s: cannot open component %s\n",
2237 devname
, dn
? dn
: "-unknown-");
2240 st2
= dup_super(st
);
2241 rv
= st2
->ss
->load_super(st2
,dfd
, NULL
);
2245 pr_err("%s: cannot get superblock from %s\n",
2249 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
2250 st2
->ss
->free_super(st2
);
2252 if (info2
.space_before
== 0 &&
2253 info2
.space_after
== 0) {
2254 /* Metadata doesn't support data_offset changes */
2257 if (before
> info2
.space_before
)
2258 before
= info2
.space_before
;
2259 if (after
> info2
.space_after
)
2260 after
= info2
.space_after
;
2262 if (data_offset
!= INVALID_SECTORS
) {
2264 if (info2
.data_offset
== data_offset
) {
2265 pr_err("%s: already has that data_offset\n",
2269 if (data_offset
< info2
.data_offset
)
2273 } else if ((data_offset
<= info2
.data_offset
&& dir
== 1) ||
2274 (data_offset
>= info2
.data_offset
&& dir
== -1)) {
2275 pr_err("%s: differing data offsets on devices make this --data-offset setting impossible\n",
2281 if (before
== UINT64_MAX
)
2282 /* impossible really, there must be no devices */
2285 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2286 char *dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2287 unsigned long long new_data_offset
;
2289 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2291 if (delta_disks
< 0) {
2292 /* Don't need any space as array is shrinking
2293 * just move data_offset up by min
2295 if (data_offset
== INVALID_SECTORS
)
2296 new_data_offset
= sd
->data_offset
+ min
;
2298 if (data_offset
< sd
->data_offset
+ min
) {
2299 pr_err("--data-offset too small for %s\n",
2303 new_data_offset
= data_offset
;
2305 } else if (delta_disks
> 0) {
2306 /* need space before */
2310 pr_err("Insufficient head-space for reshape on %s\n",
2314 if (data_offset
== INVALID_SECTORS
)
2315 new_data_offset
= sd
->data_offset
- min
;
2317 if (data_offset
> sd
->data_offset
- min
) {
2318 pr_err("--data-offset too large for %s\n",
2322 new_data_offset
= data_offset
;
2326 /* can move up or down. If 'data_offset'
2327 * was set we would have already decided,
2328 * so just choose direction with most space.
2335 sysfs_set_str(sra
, NULL
, "reshape_direction",
2336 dir
== 1 ? "backwards" : "forwards");
2338 /* Increase data offset */
2342 pr_err("Insufficient tail-space for reshape on %s\n",
2346 if (data_offset
!= INVALID_SECTORS
&&
2347 data_offset
< sd
->data_offset
+ min
) {
2348 pr_err("--data-offset too small on %s\n",
2352 if (data_offset
!= INVALID_SECTORS
)
2353 new_data_offset
= data_offset
;
2355 new_data_offset
= choose_offset(sd
->data_offset
,
2356 sd
->data_offset
+ after
,
2357 sd
->data_offset
+ min
,
2358 sd
->data_offset
+ after
);
2360 /* Decrease data offset */
2364 pr_err("insufficient head-room on %s\n",
2368 if (data_offset
!= INVALID_SECTORS
&&
2369 data_offset
< sd
->data_offset
- min
) {
2370 pr_err("--data-offset too small on %s\n",
2374 if (data_offset
!= INVALID_SECTORS
)
2375 new_data_offset
= data_offset
;
2377 new_data_offset
= choose_offset(sd
->data_offset
- before
,
2379 sd
->data_offset
- before
,
2380 sd
->data_offset
- min
);
2383 err
= sysfs_set_num(sra
, sd
, "new_offset", new_data_offset
);
2384 if (err
< 0 && errno
== E2BIG
) {
2385 /* try again after increasing data size to max */
2386 err
= sysfs_set_num(sra
, sd
, "size", 0);
2387 if (err
< 0 && errno
== EINVAL
&&
2388 !(sd
->disk
.state
& (1<<MD_DISK_SYNC
))) {
2389 /* some kernels have a bug where you cannot
2390 * use '0' on spare devices. */
2391 sysfs_set_num(sra
, sd
, "size",
2392 (sra
->component_size
+ after
)/2);
2394 err
= sysfs_set_num(sra
, sd
, "new_offset",
2398 if (errno
== E2BIG
&& data_offset
!= INVALID_SECTORS
) {
2399 pr_err("data-offset is too big for %s\n",
2403 if (sd
== sra
->devs
&&
2404 (errno
== ENOENT
|| errno
== E2BIG
))
2405 /* Early kernel, no 'new_offset' file,
2406 * or kernel doesn't like us.
2407 * For RAID5/6 this is not fatal
2410 pr_err("Cannot set new_offset for %s\n",
2419 /* Just use a backup file */
2423 static int raid10_reshape(char *container
, int fd
, char *devname
,
2424 struct supertype
*st
, struct mdinfo
*info
,
2425 struct reshape
*reshape
,
2426 unsigned long long data_offset
,
2427 int force
, int verbose
)
2429 /* Changing raid_disks, layout, chunksize or possibly
2430 * just data_offset for a RAID10.
2431 * We must always change data_offset. We change by at least
2432 * ->min_offset_change which is the largest of the old and new
2434 * If raid_disks is increasing, then data_offset must decrease
2435 * by at least this copy size.
2436 * If raid_disks is unchanged, data_offset must increase or
2437 * decrease by at least min_offset_change but preferably by much more.
2438 * We choose half of the available space.
2439 * If raid_disks is decreasing, data_offset must increase by
2440 * at least min_offset_change. To allow of this, component_size
2441 * must be decreased by the same amount.
2443 * So we calculate the required minimum and direction, possibly
2444 * reduce the component_size, then iterate through the devices
2445 * and set the new_data_offset.
2446 * If that all works, we set chunk_size, layout, raid_disks, and start
2450 unsigned long long min
;
2453 sra
= sysfs_read(fd
, NULL
,
2454 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
2457 pr_err("%s: Cannot get array details from sysfs\n",
2461 min
= reshape
->min_offset_change
;
2463 if (info
->delta_disks
)
2464 sysfs_set_str(sra
, NULL
, "reshape_direction",
2465 info
->delta_disks
< 0 ? "backwards" : "forwards");
2466 if (info
->delta_disks
< 0 &&
2467 info
->space_after
< min
) {
2468 int rv
= sysfs_set_num(sra
, NULL
, "component_size",
2469 (sra
->component_size
-
2472 pr_err("cannot reduce component size\n");
2476 err
= set_new_data_offset(sra
, st
, devname
, info
->delta_disks
, data_offset
,
2479 pr_err("Cannot set new_data_offset: RAID10 reshape not\n");
2480 cont_err("supported on this kernel\n");
2486 if (!err
&& sysfs_set_num(sra
, NULL
, "chunk_size", info
->new_chunk
) < 0)
2488 if (!err
&& sysfs_set_num(sra
, NULL
, "layout", reshape
->after
.layout
) < 0)
2490 if (!err
&& sysfs_set_num(sra
, NULL
, "raid_disks",
2491 info
->array
.raid_disks
+ info
->delta_disks
) < 0)
2493 if (!err
&& sysfs_set_str(sra
, NULL
, "sync_action", "reshape") < 0)
2496 pr_err("Cannot set array shape for %s\n",
2499 (info
->array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2500 cont_err(" Bitmap must be removed before"
2501 " shape can be changed\n");
2511 static void get_space_after(int fd
, struct supertype
*st
, struct mdinfo
*info
)
2513 struct mdinfo
*sra
, *sd
;
2514 /* Initialisation to silence compiler warning */
2515 unsigned long long min_space_before
= 0, min_space_after
= 0;
2518 sra
= sysfs_read(fd
, NULL
, GET_DEVS
);
2521 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2524 struct supertype
*st2
;
2525 struct mdinfo info2
;
2527 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
2529 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 0);
2530 dfd
= dev_open(dn
, O_RDONLY
);
2533 st2
= dup_super(st
);
2534 if (st2
->ss
->load_super(st2
,dfd
, NULL
)) {
2540 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
2541 st2
->ss
->free_super(st2
);
2544 min_space_before
> info2
.space_before
)
2545 min_space_before
= info2
.space_before
;
2547 min_space_after
> info2
.space_after
)
2548 min_space_after
= info2
.space_after
;
2551 if (sd
== NULL
&& !first
) {
2552 info
->space_after
= min_space_after
;
2553 info
->space_before
= min_space_before
;
2558 static void update_cache_size(char *container
, struct mdinfo
*sra
,
2559 struct mdinfo
*info
,
2560 int disks
, unsigned long long blocks
)
2562 /* Check that the internal stripe cache is
2563 * large enough, or it won't work.
2564 * It must hold at least 4 stripes of the larger
2567 unsigned long cache
;
2568 cache
= max(info
->array
.chunk_size
, info
->new_chunk
);
2569 cache
*= 4; /* 4 stripes minimum */
2570 cache
/= 512; /* convert to sectors */
2571 /* make sure there is room for 'blocks' with a bit to spare */
2572 if (cache
< 16 + blocks
/ disks
)
2573 cache
= 16 + blocks
/ disks
;
2574 cache
/= (4096/512); /* Covert from sectors to pages */
2576 if (sra
->cache_size
< cache
)
2577 subarray_set_num(container
, sra
, "stripe_cache_size",
2581 static int impose_reshape(struct mdinfo
*sra
,
2582 struct mdinfo
*info
,
2583 struct supertype
*st
,
2586 char *devname
, char *container
,
2587 struct reshape
*reshape
)
2589 struct mdu_array_info_s array
;
2591 sra
->new_chunk
= info
->new_chunk
;
2594 /* for external metadata checkpoint saved by mdmon can be lost
2595 * or missed /due to e.g. crash/. Check if md is not during
2596 * restart farther than metadata points to.
2597 * If so, this means metadata information is obsolete.
2599 if (st
->ss
->external
)
2600 verify_reshape_position(info
, reshape
->level
);
2601 sra
->reshape_progress
= info
->reshape_progress
;
2603 sra
->reshape_progress
= 0;
2604 if (reshape
->after
.data_disks
< reshape
->before
.data_disks
)
2605 /* start from the end of the new array */
2606 sra
->reshape_progress
= (sra
->component_size
2607 * reshape
->after
.data_disks
);
2610 ioctl(fd
, GET_ARRAY_INFO
, &array
);
2611 if (info
->array
.chunk_size
== info
->new_chunk
&&
2612 reshape
->before
.layout
== reshape
->after
.layout
&&
2613 st
->ss
->external
== 0) {
2614 /* use SET_ARRAY_INFO but only if reshape hasn't started */
2615 array
.raid_disks
= reshape
->after
.data_disks
+ reshape
->parity
;
2617 ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
2620 pr_err("Cannot set device shape for %s: %s\n",
2621 devname
, strerror(errno
));
2624 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2625 cont_err("Bitmap must be removed before"
2626 " shape can be changed\n");
2630 } else if (!restart
) {
2631 /* set them all just in case some old 'new_*' value
2632 * persists from some earlier problem.
2635 if (sysfs_set_num(sra
, NULL
, "chunk_size", info
->new_chunk
) < 0)
2637 if (!err
&& sysfs_set_num(sra
, NULL
, "layout",
2638 reshape
->after
.layout
) < 0)
2640 if (!err
&& subarray_set_num(container
, sra
, "raid_disks",
2641 reshape
->after
.data_disks
+
2642 reshape
->parity
) < 0)
2645 pr_err("Cannot set device shape for %s\n",
2649 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2650 cont_err("Bitmap must be removed before"
2651 " shape can be changed\n");
2660 static int impose_level(int fd
, int level
, char *devname
, int verbose
)
2663 struct mdu_array_info_s array
;
2665 sysfs_init(&info
, fd
, NULL
);
2667 ioctl(fd
, GET_ARRAY_INFO
, &array
);
2669 (array
.level
>= 4 && array
.level
<= 6)) {
2670 /* To convert to RAID0 we need to fail and
2671 * remove any non-data devices. */
2674 int data_disks
= array
.raid_disks
- 1;
2675 if (array
.level
== 6)
2677 if (array
.level
== 5 &&
2678 array
.layout
!= ALGORITHM_PARITY_N
)
2680 if (array
.level
== 6 &&
2681 array
.layout
!= ALGORITHM_PARITY_N_6
)
2683 sysfs_set_str(&info
, NULL
,"sync_action", "idle");
2684 /* First remove any spares so no recovery starts */
2685 for (d
= 0, found
= 0;
2686 d
< MAX_DISKS
&& found
< array
.nr_disks
;
2688 mdu_disk_info_t disk
;
2690 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
2692 if (disk
.major
== 0 && disk
.minor
== 0)
2695 if ((disk
.state
& (1 << MD_DISK_ACTIVE
))
2696 && disk
.raid_disk
< data_disks
)
2699 ioctl(fd
, HOT_REMOVE_DISK
,
2700 makedev(disk
.major
, disk
.minor
));
2702 /* Now fail anything left */
2703 ioctl(fd
, GET_ARRAY_INFO
, &array
);
2704 for (d
= 0, found
= 0;
2705 d
< MAX_DISKS
&& found
< array
.nr_disks
;
2708 mdu_disk_info_t disk
;
2710 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
2712 if (disk
.major
== 0 && disk
.minor
== 0)
2715 if ((disk
.state
& (1 << MD_DISK_ACTIVE
))
2716 && disk
.raid_disk
< data_disks
)
2719 ioctl(fd
, SET_DISK_FAULTY
,
2720 makedev(disk
.major
, disk
.minor
));
2722 while (ioctl(fd
, HOT_REMOVE_DISK
,
2723 makedev(disk
.major
, disk
.minor
)) < 0
2730 c
= map_num(pers
, level
);
2732 int err
= sysfs_set_str(&info
, NULL
, "level", c
);
2735 pr_err("%s: could not set level to %s\n",
2738 (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)))
2739 cont_err("Bitmap must be removed"
2740 " before level can be changed\n");
2744 pr_err("level of %s changed to %s\n",
2751 static void catch_term(int sig
)
2756 static int continue_via_systemd(char *devnm
)
2758 int skipped
, i
, pid
, status
;
2760 /* In a systemd/udev world, it is best to get systemd to
2761 * run "mdadm --grow --continue" rather than running in the
2766 /* FIXME yuk. CLOSE_EXEC?? */
2768 for (i
= 3; skipped
< 20; i
++)
2774 /* Don't want to see error messages from
2775 * systemctl. If the service doesn't exist,
2776 * we fork ourselves.
2779 open("/dev/null", O_WRONLY
);
2780 snprintf(pathbuf
, sizeof(pathbuf
), "mdadm-grow-continue@%s.service",
2782 status
= execl("/usr/bin/systemctl", "systemctl",
2785 status
= execl("/bin/systemctl", "systemctl", "start",
2788 case -1: /* Just do it ourselves. */
2790 default: /* parent - good */
2791 pid
= wait(&status
);
2792 if (pid
>= 0 && status
== 0)
2798 static int reshape_array(char *container
, int fd
, char *devname
,
2799 struct supertype
*st
, struct mdinfo
*info
,
2800 int force
, struct mddev_dev
*devlist
,
2801 unsigned long long data_offset
,
2802 char *backup_file
, int verbose
, int forked
,
2803 int restart
, int freeze_reshape
)
2805 struct reshape reshape
;
2808 int orig_level
= UnSet
;
2812 struct mdu_array_info_s array
;
2815 struct mddev_dev
*dv
;
2819 unsigned long long *offsets
= NULL
;
2823 unsigned long blocks
;
2824 unsigned long long array_size
;
2826 struct mdinfo
*sra
= NULL
;
2828 /* when reshaping a RAID0, the component_size might be zero.
2829 * So try to fix that up.
2831 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
2832 dprintf("Cannot get array information.\n");
2835 if (array
.level
== 0 && info
->component_size
== 0) {
2836 get_dev_size(fd
, NULL
, &array_size
);
2837 info
->component_size
= array_size
/ array
.raid_disks
;
2840 if (array
.level
== 10)
2841 /* Need space_after info */
2842 get_space_after(fd
, st
, info
);
2844 if (info
->reshape_active
) {
2845 int new_level
= info
->new_level
;
2846 info
->new_level
= UnSet
;
2847 if (info
->delta_disks
> 0)
2848 info
->array
.raid_disks
-= info
->delta_disks
;
2849 msg
= analyse_change(devname
, info
, &reshape
);
2850 info
->new_level
= new_level
;
2851 if (info
->delta_disks
> 0)
2852 info
->array
.raid_disks
+= info
->delta_disks
;
2854 /* Make sure the array isn't read-only */
2855 ioctl(fd
, RESTART_ARRAY_RW
, 0);
2857 msg
= analyse_change(devname
, info
, &reshape
);
2859 /* if msg == "", error has already been printed */
2861 pr_err("%s\n", msg
);
2865 (reshape
.level
!= info
->array
.level
||
2866 reshape
.before
.layout
!= info
->array
.layout
||
2867 reshape
.before
.data_disks
+ reshape
.parity
2868 != info
->array
.raid_disks
- max(0, info
->delta_disks
))) {
2869 pr_err("reshape info is not in native format -"
2870 " cannot continue.\n");
2874 if (st
->ss
->external
&& restart
&& (info
->reshape_progress
== 0)) {
2875 /* When reshape is restarted from '0', very begin of array
2876 * it is possible that for external metadata reshape and array
2877 * configuration doesn't happen.
2878 * Check if md has the same opinion, and reshape is restarted
2879 * from 0. If so, this is regular reshape start after reshape
2880 * switch in metadata to next array only.
2882 if ((verify_reshape_position(info
, reshape
.level
) >= 0) &&
2883 (info
->reshape_progress
== 0))
2887 /* reshape already started. just skip to monitoring the reshape */
2888 if (reshape
.backup_blocks
== 0)
2890 if (restart
& RESHAPE_NO_BACKUP
)
2893 /* Need 'sra' down at 'started:' */
2894 sra
= sysfs_read(fd
, NULL
,
2895 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
|
2898 pr_err("%s: Cannot get array details from sysfs\n",
2904 backup_file
= locate_backup(sra
->sys_name
);
2908 /* The container is frozen but the array may not be.
2909 * So freeze the array so spares don't get put to the wrong use
2910 * FIXME there should probably be a cleaner separation between
2911 * freeze_array and freeze_container.
2913 sysfs_freeze_array(info
);
2914 /* Check we have enough spares to not be degraded */
2916 for (dv
= devlist
; dv
; dv
=dv
->next
)
2918 spares_needed
= max(reshape
.before
.data_disks
,
2919 reshape
.after
.data_disks
)
2920 + reshape
.parity
- array
.raid_disks
;
2923 info
->new_level
> 1 && info
->array
.level
> 1 &&
2924 spares_needed
> info
->array
.spare_disks
+ added_disks
) {
2925 pr_err("Need %d spare%s to avoid degraded array,"
2926 " and only have %d.\n"
2927 " Use --force to over-ride this check.\n",
2929 spares_needed
== 1 ? "" : "s",
2930 info
->array
.spare_disks
+ added_disks
);
2933 /* Check we have enough spares to not fail */
2934 spares_needed
= max(reshape
.before
.data_disks
,
2935 reshape
.after
.data_disks
)
2937 if ((info
->new_level
> 1 || info
->new_level
== 0) &&
2938 spares_needed
> info
->array
.spare_disks
+added_disks
) {
2939 pr_err("Need %d spare%s to create working array,"
2940 " and only have %d.\n",
2942 spares_needed
== 1 ? "" : "s",
2943 info
->array
.spare_disks
+ added_disks
);
2947 if (reshape
.level
!= array
.level
) {
2948 int err
= impose_level(fd
, reshape
.level
, devname
, verbose
);
2951 info
->new_layout
= UnSet
; /* after level change,
2952 * layout is meaningless */
2953 orig_level
= array
.level
;
2954 sysfs_freeze_array(info
);
2956 if (reshape
.level
> 0 && st
->ss
->external
) {
2957 /* make sure mdmon is aware of the new level */
2958 if (mdmon_running(container
))
2959 flush_mdmon(container
);
2961 if (!mdmon_running(container
))
2962 start_mdmon(container
);
2963 ping_monitor(container
);
2964 if (mdmon_running(container
) &&
2965 st
->update_tail
== NULL
)
2966 st
->update_tail
= &st
->updates
;
2969 /* ->reshape_super might have chosen some spares from the
2970 * container that it wants to be part of the new array.
2971 * We can collect them with ->container_content and give
2972 * them to the kernel.
2974 if (st
->ss
->reshape_super
&& st
->ss
->container_content
) {
2975 char *subarray
= strchr(info
->text_version
+1, '/')+1;
2976 struct mdinfo
*info2
=
2977 st
->ss
->container_content(st
, subarray
);
2981 sysfs_init(info2
, fd
, st
->devnm
);
2982 /* When increasing number of devices, we need to set
2983 * new raid_disks before adding these, or they might
2986 if (reshape
.backup_blocks
&&
2987 reshape
.after
.data_disks
> reshape
.before
.data_disks
)
2988 subarray_set_num(container
, info2
, "raid_disks",
2989 reshape
.after
.data_disks
+
2991 for (d
= info2
->devs
; d
; d
= d
->next
) {
2992 if (d
->disk
.state
== 0 &&
2993 d
->disk
.raid_disk
>= 0) {
2994 /* This is a spare that wants to
2995 * be part of the array.
2997 add_disk(fd
, st
, info2
, d
);
3003 /* We might have been given some devices to add to the
3004 * array. Now that the array has been changed to the right
3005 * level and frozen, we can safely add them.
3008 Manage_subdevs(devname
, fd
, devlist
, verbose
,
3011 if (reshape
.backup_blocks
== 0 && data_offset
!= INVALID_SECTORS
)
3012 reshape
.backup_blocks
= reshape
.before
.data_disks
* info
->array
.chunk_size
/512;
3013 if (reshape
.backup_blocks
== 0) {
3014 /* No restriping needed, but we might need to impose
3015 * some more changes: layout, raid_disks, chunk_size
3017 /* read current array info */
3018 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
3019 dprintf("Cannot get array information.\n");
3022 /* compare current array info with new values and if
3023 * it is different update them to new */
3024 if (info
->new_layout
!= UnSet
&&
3025 info
->new_layout
!= array
.layout
) {
3026 array
.layout
= info
->new_layout
;
3027 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
3028 pr_err("failed to set new layout\n");
3030 } else if (verbose
>= 0)
3031 printf("layout for %s set to %d\n",
3032 devname
, array
.layout
);
3034 if (info
->delta_disks
!= UnSet
&&
3035 info
->delta_disks
!= 0 &&
3036 array
.raid_disks
!= (info
->array
.raid_disks
+ info
->delta_disks
)) {
3037 array
.raid_disks
+= info
->delta_disks
;
3038 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
3039 pr_err("failed to set raid disks\n");
3041 } else if (verbose
>= 0) {
3042 printf("raid_disks for %s set to %d\n",
3043 devname
, array
.raid_disks
);
3046 if (info
->new_chunk
!= 0 &&
3047 info
->new_chunk
!= array
.chunk_size
) {
3048 if (sysfs_set_num(info
, NULL
,
3049 "chunk_size", info
->new_chunk
) != 0) {
3050 pr_err("failed to set chunk size\n");
3052 } else if (verbose
>= 0)
3053 printf("chunk size for %s set to %d\n",
3054 devname
, array
.chunk_size
);
3061 * There are three possibilities.
3062 * 1/ The array will shrink.
3063 * We need to ensure the reshape will pause before reaching
3064 * the 'critical section'. We also need to fork and wait for
3065 * that to happen. When it does we
3066 * suspend/backup/complete/unfreeze
3068 * 2/ The array will not change size.
3069 * This requires that we keep a backup of a sliding window
3070 * so that we can restore data after a crash. So we need
3071 * to fork and monitor progress.
3072 * In future we will allow the data_offset to change, so
3073 * a sliding backup becomes unnecessary.
3075 * 3/ The array will grow. This is relatively easy.
3076 * However the kernel's restripe routines will cheerfully
3077 * overwrite some early data before it is safe. So we
3078 * need to make a backup of the early parts of the array
3079 * and be ready to restore it if rebuild aborts very early.
3080 * For externally managed metadata, we still need a forked
3081 * child to monitor the reshape and suspend IO over the region
3082 * that is being reshaped.
3084 * We backup data by writing it to one spare, or to a
3085 * file which was given on command line.
3087 * In each case, we first make sure that storage is available
3088 * for the required backup.
3090 * - request the shape change.
3091 * - fork to handle backup etc.
3093 /* Check that we can hold all the data */
3094 get_dev_size(fd
, NULL
, &array_size
);
3095 if (reshape
.new_size
< (array_size
/512)) {
3096 pr_err("this change will reduce the size of the array.\n"
3097 " use --grow --array-size first to truncate array.\n"
3098 " e.g. mdadm --grow %s --array-size %llu\n",
3099 devname
, reshape
.new_size
/2);
3103 if (array
.level
== 10) {
3104 /* Reshaping RAID10 does not require any data backup by
3105 * user-space. Instead it requires that the data_offset
3106 * is changed to avoid the need for backup.
3107 * So this is handled very separately
3110 /* Nothing to do. */
3112 return raid10_reshape(container
, fd
, devname
, st
, info
,
3113 &reshape
, data_offset
,
3116 sra
= sysfs_read(fd
, NULL
,
3117 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|GET_CHUNK
|
3120 pr_err("%s: Cannot get array details from sysfs\n",
3126 switch(set_new_data_offset(sra
, st
, devname
,
3127 reshape
.after
.data_disks
- reshape
.before
.data_disks
,
3129 reshape
.min_offset_change
, 1)) {
3133 /* Updated data_offset, so it's easy now */
3134 update_cache_size(container
, sra
, info
,
3135 min(reshape
.before
.data_disks
,
3136 reshape
.after
.data_disks
),
3137 reshape
.backup_blocks
);
3139 /* Right, everything seems fine. Let's kick things off.
3143 if (impose_reshape(sra
, info
, st
, fd
, restart
,
3144 devname
, container
, &reshape
) < 0)
3146 if (sysfs_set_str(sra
, NULL
, "sync_action", "reshape") < 0) {
3147 pr_err("Failed to initiate reshape!\n");
3150 if (info
->new_level
== reshape
.level
)
3152 /* need to adjust level when reshape completes */
3154 case -1: /* ignore error, but don't wait */
3156 default: /* parent */
3164 fd
= open_dev(sra
->sys_name
);
3166 impose_level(fd
, info
->new_level
, devname
, verbose
);
3168 case 1: /* Couldn't set data_offset, try the old way */
3169 if (data_offset
!= INVALID_SECTORS
) {
3170 pr_err("Cannot update data_offset on this array\n");
3177 /* Decide how many blocks (sectors) for a reshape
3178 * unit. The number we have so far is just a minimum
3180 blocks
= reshape
.backup_blocks
;
3181 if (reshape
.before
.data_disks
==
3182 reshape
.after
.data_disks
) {
3183 /* Make 'blocks' bigger for better throughput, but
3184 * not so big that we reject it below.
3185 * Try for 16 megabytes
3187 while (blocks
* 32 < sra
->component_size
&&
3191 pr_err("Need to backup %luK of critical "
3192 "section..\n", blocks
/2);
3194 if (blocks
>= sra
->component_size
/2) {
3195 pr_err("%s: Something wrong"
3196 " - reshape aborted\n",
3201 /* Now we need to open all these devices so we can read/write.
3203 nrdisks
= max(reshape
.before
.data_disks
,
3204 reshape
.after
.data_disks
) + reshape
.parity
3205 + sra
->array
.spare_disks
;
3206 fdlist
= xcalloc((1+nrdisks
), sizeof(int));
3207 offsets
= xcalloc((1+nrdisks
), sizeof(offsets
[0]));
3209 odisks
= reshape
.before
.data_disks
+ reshape
.parity
;
3210 d
= reshape_prepare_fdlist(devname
, sra
, odisks
,
3211 nrdisks
, blocks
, backup_file
,
3216 if ((st
->ss
->manage_reshape
== NULL
) ||
3217 (st
->ss
->recover_backup
== NULL
)) {
3218 if (backup_file
== NULL
) {
3219 if (reshape
.after
.data_disks
<=
3220 reshape
.before
.data_disks
) {
3221 pr_err("%s: Cannot grow - need backup-file\n",
3223 pr_err(" Please provide one with \"--backup=...\"\n");
3225 } else if (sra
->array
.spare_disks
== 0) {
3226 pr_err("%s: Cannot grow - "
3227 "need a spare or backup-file to backup "
3228 "critical section\n", devname
);
3232 if (!reshape_open_backup_file(backup_file
, fd
, devname
,
3234 fdlist
+d
, offsets
+d
,
3243 update_cache_size(container
, sra
, info
,
3244 min(reshape
.before
.data_disks
, reshape
.after
.data_disks
),
3247 /* Right, everything seems fine. Let's kick things off.
3248 * If only changing raid_disks, use ioctl, else use
3253 if (impose_reshape(sra
, info
, st
, fd
, restart
,
3254 devname
, container
, &reshape
) < 0)
3257 err
= start_reshape(sra
, restart
, reshape
.before
.data_disks
,
3258 reshape
.after
.data_disks
);
3260 pr_err("Cannot %s reshape for %s\n",
3261 restart
? "continue" : "start",
3266 sysfs_set_str(sra
, NULL
, "array_state", "active");
3267 if (freeze_reshape
) {
3271 pr_err("Reshape has to be continued from"
3272 " location %llu when root filesystem has been mounted.\n",
3273 sra
->reshape_progress
);
3277 if (!forked
&& !check_env("MDADM_NO_SYSTEMCTL"))
3278 if (continue_via_systemd(container
?: sra
->sys_name
)) {
3285 /* Now we just need to kick off the reshape and watch, while
3286 * handling backups of the data...
3287 * This is all done by a forked background process.
3289 switch(forked
? 0 : fork()) {
3291 pr_err("Cannot run child to monitor reshape: %s\n",
3305 /* If another array on the same devices is busy, the
3306 * reshape will wait for them. This would mean that
3307 * the first section that we suspend will stay suspended
3308 * for a long time. So check on that possibility
3309 * by looking for "DELAYED" in /proc/mdstat, and if found,
3313 struct mdstat_ent
*mds
, *m
;
3315 mds
= mdstat_read(1, 0);
3316 for (m
= mds
; m
; m
= m
->next
)
3317 if (strcmp(m
->devnm
, sra
->sys_name
) == 0) {
3319 m
->percent
== RESYNC_DELAYED
)
3322 /* Haven't started the reshape thread
3329 if (delayed
== 1 && get_linux_version() < 3007000) {
3330 pr_err("Reshape is delayed, but cannot wait carefully with this kernel.\n"
3331 " You might experience problems until other reshapes complete.\n");
3335 mdstat_wait(30 - (delayed
-1) * 25);
3339 if (check_env("MDADM_GROW_VERIFY"))
3340 fd
= open(devname
, O_RDONLY
| O_DIRECT
);
3343 mlockall(MCL_FUTURE
);
3345 signal(SIGTERM
, catch_term
);
3347 if (st
->ss
->external
) {
3348 /* metadata handler takes it from here */
3349 done
= st
->ss
->manage_reshape(
3350 fd
, sra
, &reshape
, st
, blocks
,
3352 d
- odisks
, fdlist
+odisks
,
3355 done
= child_monitor(
3356 fd
, sra
, &reshape
, st
, blocks
,
3358 d
- odisks
, fdlist
+odisks
,
3364 if (backup_file
&& done
) {
3366 bul
= make_backup(sra
->sys_name
);
3369 int l
= readlink(bul
, buf
, sizeof(buf
));
3377 unlink(backup_file
);
3384 if (!st
->ss
->external
&&
3385 !(reshape
.before
.data_disks
!= reshape
.after
.data_disks
3386 && info
->custom_array_size
) &&
3387 info
->new_level
== reshape
.level
&&
3389 /* no need to wait for the reshape to finish as
3390 * there is nothing more to do.
3397 if (st
->ss
->external
) {
3398 /* Re-load the metadata as much could have changed */
3399 int cfd
= open_dev(st
->container_devnm
);
3401 flush_mdmon(container
);
3402 st
->ss
->free_super(st
);
3403 st
->ss
->load_container(st
, cfd
, container
);
3408 /* set new array size if required customer_array_size is used
3411 if (reshape
.before
.data_disks
!=
3412 reshape
.after
.data_disks
&&
3413 info
->custom_array_size
)
3414 set_array_size(st
, info
, info
->text_version
);
3416 if (info
->new_level
!= reshape
.level
) {
3418 fd
= open(devname
, O_RDONLY
);
3419 impose_level(fd
, info
->new_level
, devname
, verbose
);
3421 if (info
->new_level
== 0)
3422 st
->update_tail
= NULL
;
3434 if (orig_level
!= UnSet
&& sra
) {
3435 c
= map_num(pers
, orig_level
);
3436 if (c
&& sysfs_set_str(sra
, NULL
, "level", c
) == 0)
3437 pr_err("aborting level change\n");
3445 /* mdfd handle is passed to be closed in child process (after fork).
3447 int reshape_container(char *container
, char *devname
,
3449 struct supertype
*st
,
3450 struct mdinfo
*info
,
3452 char *backup_file
, int verbose
,
3453 int forked
, int restart
, int freeze_reshape
)
3455 struct mdinfo
*cc
= NULL
;
3457 char last_devnm
[32] = "";
3459 /* component_size is not meaningful for a container,
3460 * so pass '0' meaning 'no change'
3463 reshape_super(st
, 0, info
->new_level
,
3464 info
->new_layout
, info
->new_chunk
,
3465 info
->array
.raid_disks
, info
->delta_disks
,
3466 backup_file
, devname
, APPLY_METADATA_CHANGES
,
3474 /* ping monitor to be sure that update is on disk
3476 ping_monitor(container
);
3478 if (!forked
&& !check_env("MDADM_NO_SYSTEMCTL"))
3479 if (continue_via_systemd(container
))
3482 switch (forked
? 0 : fork()) {
3483 case -1: /* error */
3484 perror("Cannot fork to complete reshape\n");
3487 default: /* parent */
3488 if (!freeze_reshape
)
3489 printf(Name
": multi-array reshape continues"
3490 " in background\n");
3497 /* close unused handle in child process
3503 /* For each member array with reshape_active,
3504 * we need to perform the reshape.
3505 * We pick the first array that needs reshaping and
3506 * reshape it. reshape_array() will re-read the metadata
3507 * so the next time through a different array should be
3508 * ready for reshape.
3509 * It is possible that the 'different' array will not
3510 * be assembled yet. In that case we simple exit.
3511 * When it is assembled, the mdadm which assembles it
3512 * will take over the reshape.
3514 struct mdinfo
*content
;
3516 struct mdstat_ent
*mdstat
;
3522 cc
= st
->ss
->container_content(st
, NULL
);
3524 for (content
= cc
; content
; content
= content
->next
) {
3526 if (!content
->reshape_active
)
3529 subarray
= strchr(content
->text_version
+1, '/')+1;
3530 mdstat
= mdstat_by_subdev(subarray
, container
);
3533 if (mdstat
->active
== 0) {
3534 pr_err("Skipping inactive array %s.\n",
3536 free_mdstat(mdstat
);
3545 devid
= devnm2devid(mdstat
->devnm
);
3546 adev
= map_dev(major(devid
), minor(devid
), 0);
3548 adev
= content
->text_version
;
3550 fd
= open_dev(mdstat
->devnm
);
3552 printf(Name
": Device %s cannot be opened for reshape.",
3557 if (strcmp(last_devnm
, mdstat
->devnm
) == 0) {
3558 /* Do not allow for multiple reshape_array() calls for
3560 * It can happen when reshape_array() returns without
3561 * error, when reshape is not finished (wrong reshape
3562 * starting/continuation conditions). Mdmon doesn't
3563 * switch to next array in container and reentry
3564 * conditions for the same array occur.
3565 * This is possibly interim until the behaviour of
3566 * reshape_array is resolved().
3568 printf(Name
": Multiple reshape execution detected for "
3569 "device %s.", adev
);
3573 strcpy(last_devnm
, mdstat
->devnm
);
3575 sysfs_init(content
, fd
, mdstat
->devnm
);
3577 if (mdmon_running(container
))
3578 flush_mdmon(container
);
3580 rv
= reshape_array(container
, fd
, adev
, st
,
3581 content
, force
, NULL
, INVALID_SECTORS
,
3582 backup_file
, verbose
, 1, restart
,
3586 if (freeze_reshape
) {
3595 if (mdmon_running(container
))
3596 flush_mdmon(container
);
3605 * We run a child process in the background which performs the following
3607 * - wait for resync to reach a certain point
3608 * - suspend io to the following section
3609 * - backup that section
3610 * - allow resync to proceed further
3612 * - discard the backup.
3614 * When are combined in slightly different ways in the three cases.
3616 * - suspend/backup/allow/wait/resume/discard
3618 * - allow/wait/suspend/backup/allow/wait/resume/discard
3620 * - wait/resume/discard/suspend/backup/allow
3622 * suspend/backup/allow always come together
3623 * wait/resume/discard do too.
3624 * For the same-size case we have two backups to improve flow.
3628 int progress_reshape(struct mdinfo
*info
, struct reshape
*reshape
,
3629 unsigned long long backup_point
,
3630 unsigned long long wait_point
,
3631 unsigned long long *suspend_point
,
3632 unsigned long long *reshape_completed
, int *frozen
)
3634 /* This function is called repeatedly by the reshape manager.
3635 * It determines how much progress can safely be made and allows
3637 * - 'info' identifies the array and particularly records in
3638 * ->reshape_progress the metadata's knowledge of progress
3639 * This is a sector offset from the start of the array
3640 * of the next array block to be relocated. This number
3641 * may increase from 0 or decrease from array_size, depending
3642 * on the type of reshape that is happening.
3643 * Note that in contrast, 'sync_completed' is a block count of the
3644 * reshape so far. It gives the distance between the start point
3645 * (head or tail of device) and the next place that data will be
3646 * written. It always increases.
3647 * - 'reshape' is the structure created by analyse_change
3648 * - 'backup_point' shows how much the metadata manager has backed-up
3649 * data. For reshapes with increasing progress, it is the next address
3650 * to be backed up, previous addresses have been backed-up. For
3651 * decreasing progress, it is the earliest address that has been
3652 * backed up - later address are also backed up.
3653 * So addresses between reshape_progress and backup_point are
3654 * backed up providing those are in the 'correct' order.
3655 * - 'wait_point' is an array address. When reshape_completed
3656 * passes this point, progress_reshape should return. It might
3657 * return earlier if it determines that ->reshape_progress needs
3658 * to be updated or further backup is needed.
3659 * - suspend_point is maintained by progress_reshape and the caller
3660 * should not touch it except to initialise to zero.
3661 * It is an array address and it only increases in 2.6.37 and earlier.
3662 * This makes it difficult to handle reducing reshapes with
3663 * external metadata.
3664 * However: it is similar to backup_point in that it records the
3665 * other end of a suspended region from reshape_progress.
3666 * it is moved to extend the region that is safe to backup and/or
3668 * - reshape_completed is read from sysfs and returned. The caller
3669 * should copy this into ->reshape_progress when it has reason to
3670 * believe that the metadata knows this, and any backup outside this
3674 * 1 if more data from backup_point - but only as far as suspend_point,
3675 * should be backed up
3676 * 0 if things are progressing smoothly
3677 * -1 if the reshape is finished because it is all done,
3678 * -2 if the reshape is finished due to an error.
3681 int advancing
= (reshape
->after
.data_disks
3682 >= reshape
->before
.data_disks
);
3683 unsigned long long need_backup
; /* All data between start of array and
3684 * here will at some point need to
3687 unsigned long long read_offset
, write_offset
;
3688 unsigned long long write_range
;
3689 unsigned long long max_progress
, target
, completed
;
3690 unsigned long long array_size
= (info
->component_size
3691 * reshape
->before
.data_disks
);
3695 /* First, we unsuspend any region that is now known to be safe.
3696 * If suspend_point is on the 'wrong' side of reshape_progress, then
3697 * we don't have or need suspension at the moment. This is true for
3698 * native metadata when we don't need to back-up.
3701 if (info
->reshape_progress
<= *suspend_point
)
3702 sysfs_set_num(info
, NULL
, "suspend_lo",
3703 info
->reshape_progress
);
3705 /* Note: this won't work in 2.6.37 and before.
3706 * Something somewhere should make sure we don't need it!
3708 if (info
->reshape_progress
>= *suspend_point
)
3709 sysfs_set_num(info
, NULL
, "suspend_hi",
3710 info
->reshape_progress
);
3713 /* Now work out how far it is safe to progress.
3714 * If the read_offset for ->reshape_progress is less than
3715 * 'blocks' beyond the write_offset, we can only progress as far
3717 * Otherwise we can progress until the write_offset for the new location
3718 * reaches (within 'blocks' of) the read_offset at the current location.
3719 * However that region must be suspended unless we are using native
3721 * If we need to suspend more, we limit it to 128M per device, which is
3722 * rather arbitrary and should be some time-based calculation.
3724 read_offset
= info
->reshape_progress
/ reshape
->before
.data_disks
;
3725 write_offset
= info
->reshape_progress
/ reshape
->after
.data_disks
;
3726 write_range
= info
->new_chunk
/512;
3727 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
3728 need_backup
= array_size
;
3730 need_backup
= reshape
->backup_blocks
;
3732 if (read_offset
< write_offset
+ write_range
)
3733 max_progress
= backup_point
;
3737 reshape
->after
.data_disks
;
3739 if (read_offset
> write_offset
- write_range
)
3740 /* Can only progress as far as has been backed up,
3741 * which must be suspended */
3742 max_progress
= backup_point
;
3743 else if (info
->reshape_progress
<= need_backup
)
3744 max_progress
= backup_point
;
3746 if (info
->array
.major_version
>= 0)
3747 /* Can progress until backup is needed */
3748 max_progress
= need_backup
;
3750 /* Can progress until metadata update is required */
3753 reshape
->after
.data_disks
;
3754 /* but data must be suspended */
3755 if (max_progress
< *suspend_point
)
3756 max_progress
= *suspend_point
;
3761 /* We know it is safe to progress to 'max_progress' providing
3762 * it is suspended or we are using native metadata.
3763 * Consider extending suspend_point 128M per device if it
3764 * is less than 64M per device beyond reshape_progress.
3765 * But always do a multiple of 'blocks'
3766 * FIXME this is too big - it takes to long to complete
3769 target
= 64*1024*2 * min(reshape
->before
.data_disks
,
3770 reshape
->after
.data_disks
);
3771 target
/= reshape
->backup_blocks
;
3774 target
*= reshape
->backup_blocks
;
3776 /* For externally managed metadata we always need to suspend IO to
3777 * the area being reshaped so we regularly push suspend_point forward.
3778 * For native metadata we only need the suspend if we are going to do
3782 if ((need_backup
> info
->reshape_progress
3783 || info
->array
.major_version
< 0) &&
3784 *suspend_point
< info
->reshape_progress
+ target
) {
3785 if (need_backup
< *suspend_point
+ 2 * target
)
3786 *suspend_point
= need_backup
;
3787 else if (*suspend_point
+ 2 * target
< array_size
)
3788 *suspend_point
+= 2 * target
;
3790 *suspend_point
= array_size
;
3791 sysfs_set_num(info
, NULL
, "suspend_hi", *suspend_point
);
3792 if (max_progress
> *suspend_point
)
3793 max_progress
= *suspend_point
;
3796 if (info
->array
.major_version
>= 0) {
3797 /* Only need to suspend when about to backup */
3798 if (info
->reshape_progress
< need_backup
* 2 &&
3799 *suspend_point
> 0) {
3801 sysfs_set_num(info
, NULL
, "suspend_lo", 0);
3802 sysfs_set_num(info
, NULL
, "suspend_hi", need_backup
);
3805 /* Need to suspend continually */
3806 if (info
->reshape_progress
< *suspend_point
)
3807 *suspend_point
= info
->reshape_progress
;
3808 if (*suspend_point
+ target
< info
->reshape_progress
)
3809 /* No need to move suspend region yet */;
3811 if (*suspend_point
>= 2 * target
)
3812 *suspend_point
-= 2 * target
;
3815 sysfs_set_num(info
, NULL
, "suspend_lo",
3818 if (max_progress
< *suspend_point
)
3819 max_progress
= *suspend_point
;
3823 /* now set sync_max to allow that progress. sync_max, like
3824 * sync_completed is a count of sectors written per device, so
3825 * we find the difference between max_progress and the start point,
3826 * and divide that by after.data_disks to get a sync_max
3828 * At the same time we convert wait_point to a similar number
3829 * for comparing against sync_completed.
3831 /* scale down max_progress to per_disk */
3832 max_progress
/= reshape
->after
.data_disks
;
3833 /* Round to chunk size as some kernels give an erroneously high number */
3834 max_progress
/= info
->new_chunk
/512;
3835 max_progress
*= info
->new_chunk
/512;
3836 /* And round to old chunk size as the kernel wants that */
3837 max_progress
/= info
->array
.chunk_size
/512;
3838 max_progress
*= info
->array
.chunk_size
/512;
3839 /* Limit progress to the whole device */
3840 if (max_progress
> info
->component_size
)
3841 max_progress
= info
->component_size
;
3842 wait_point
/= reshape
->after
.data_disks
;
3844 /* switch from 'device offset' to 'processed block count' */
3845 max_progress
= info
->component_size
- max_progress
;
3846 wait_point
= info
->component_size
- wait_point
;
3850 sysfs_set_num(info
, NULL
, "sync_max", max_progress
);
3852 /* Now wait. If we have already reached the point that we were
3853 * asked to wait to, don't wait at all, else wait for any change.
3854 * We need to select on 'sync_completed' as that is the place that
3855 * notifications happen, but we are really interested in
3856 * 'reshape_position'
3858 fd
= sysfs_get_fd(info
, NULL
, "sync_completed");
3860 goto check_progress
;
3862 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
3863 goto check_progress
;
3865 while (completed
< max_progress
&& completed
< wait_point
) {
3866 /* Check that sync_action is still 'reshape' to avoid
3867 * waiting forever on a dead array
3870 if (sysfs_get_str(info
, NULL
, "sync_action",
3872 strncmp(action
, "reshape", 7) != 0)
3874 /* Some kernels reset 'sync_completed' to zero
3875 * before setting 'sync_action' to 'idle'.
3876 * So we need these extra tests.
3878 if (completed
== 0 && advancing
3879 && info
->reshape_progress
> 0)
3881 if (completed
== 0 && !advancing
3882 && info
->reshape_progress
< (info
->component_size
3883 * reshape
->after
.data_disks
))
3885 sysfs_wait(fd
, NULL
);
3886 if (sysfs_fd_get_ll(fd
, &completed
) < 0)
3887 goto check_progress
;
3889 /* Some kernels reset 'sync_completed' to zero,
3890 * we need to have real point we are in md
3893 completed
= max_progress
;
3895 /* some kernels can give an incorrectly high 'completed' number */
3896 completed
/= (info
->new_chunk
/512);
3897 completed
*= (info
->new_chunk
/512);
3898 /* Convert 'completed' back in to a 'progress' number */
3899 completed
*= reshape
->after
.data_disks
;
3901 completed
= info
->component_size
* reshape
->after
.data_disks
3904 *reshape_completed
= completed
;
3908 /* We return the need_backup flag. Caller will decide
3909 * how much - a multiple of ->backup_blocks up to *suspend_point
3912 return need_backup
> info
->reshape_progress
;
3914 return need_backup
>= info
->reshape_progress
;
3917 /* if we couldn't read a number from sync_completed, then
3918 * either the reshape did complete, or it aborted.
3919 * We can tell which by checking for 'none' in reshape_position.
3920 * If it did abort, then it might immediately restart if it
3921 * it was just a device failure that leaves us degraded but
3925 if (sysfs_get_str(info
, NULL
, "reshape_position", buf
, sizeof(buf
)) < 0
3926 || strncmp(buf
, "none", 4) != 0) {
3927 /* The abort might only be temporary. Wait up to 10
3928 * seconds for fd to contain a valid number again.
3932 unsigned long long new_sync_max
;
3933 while (fd
>= 0 && rv
< 0 && wait
> 0) {
3934 if (sysfs_wait(fd
, &wait
) != 1)
3936 switch (sysfs_fd_get_ll(fd
, &completed
)) {
3938 /* all good again */
3940 /* If "sync_max" is no longer max_progress
3941 * we need to freeze things
3943 sysfs_get_ll(info
, NULL
, "sync_max", &new_sync_max
);
3944 *frozen
= (new_sync_max
!= max_progress
);
3946 case -2: /* read error - abort */
3953 return rv
; /* abort */
3955 /* Maybe racing with array shutdown - check state */
3958 if (sysfs_get_str(info
, NULL
, "array_state", buf
, sizeof(buf
)) < 0
3959 || strncmp(buf
, "inactive", 8) == 0
3960 || strncmp(buf
, "clear",5) == 0)
3961 return -2; /* abort */
3962 return -1; /* complete */
3966 /* FIXME return status is never checked */
3967 static int grow_backup(struct mdinfo
*sra
,
3968 unsigned long long offset
, /* per device */
3969 unsigned long stripes
, /* per device, in old chunks */
3970 int *sources
, unsigned long long *offsets
,
3971 int disks
, int chunk
, int level
, int layout
,
3972 int dests
, int *destfd
, unsigned long long *destoffsets
,
3973 int part
, int *degraded
,
3976 /* Backup 'blocks' sectors at 'offset' on each device of the array,
3977 * to storage 'destfd' (offset 'destoffsets'), after first
3978 * suspending IO. Then allow resync to continue
3979 * over the suspended section.
3980 * Use part 'part' of the backup-super-block.
3985 unsigned long long ll
;
3987 //printf("offset %llu\n", offset);
3993 /* Check that array hasn't become degraded, else we might backup the wrong data */
3994 if (sysfs_get_ll(sra
, NULL
, "degraded", &ll
) < 0)
3995 return -1; /* FIXME this error is ignored */
3996 new_degraded
= (int)ll
;
3997 if (new_degraded
!= *degraded
) {
3998 /* check each device to ensure it is still working */
4000 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
4001 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
4003 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
4005 if (sysfs_get_str(sra
, sd
, "state", sbuf
, 20) < 0 ||
4006 strstr(sbuf
, "faulty") ||
4007 strstr(sbuf
, "in_sync") == NULL
) {
4008 /* this device is dead */
4009 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
4010 if (sd
->disk
.raid_disk
>= 0 &&
4011 sources
[sd
->disk
.raid_disk
] >= 0) {
4012 close(sources
[sd
->disk
.raid_disk
]);
4013 sources
[sd
->disk
.raid_disk
] = -1;
4018 *degraded
= new_degraded
;
4021 bsb
.arraystart2
= __cpu_to_le64(offset
* odata
);
4022 bsb
.length2
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
4024 bsb
.arraystart
= __cpu_to_le64(offset
* odata
);
4025 bsb
.length
= __cpu_to_le64(stripes
* (chunk
/512) * odata
);
4028 bsb
.magic
[15] = '2';
4029 for (i
= 0; i
< dests
; i
++)
4031 lseek64(destfd
[i
], destoffsets
[i
] + __le64_to_cpu(bsb
.devstart2
)*512, 0);
4033 lseek64(destfd
[i
], destoffsets
[i
], 0);
4035 rv
= save_stripes(sources
, offsets
,
4036 disks
, chunk
, level
, layout
,
4038 offset
*512*odata
, stripes
* chunk
* odata
,
4043 bsb
.mtime
= __cpu_to_le64(time(0));
4044 for (i
= 0; i
< dests
; i
++) {
4045 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
4047 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
4048 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
4049 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
4050 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
4053 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
] - 4096, 0)
4054 != destoffsets
[i
] - 4096)
4056 if (write(destfd
[i
], &bsb
, 512) != 512)
4058 if (destoffsets
[i
] > 4096) {
4059 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]+stripes
*chunk
*odata
, 0) !=
4060 destoffsets
[i
]+stripes
*chunk
*odata
)
4062 if (write(destfd
[i
], &bsb
, 512) != 512)
4072 /* in 2.6.30, the value reported by sync_completed can be
4073 * less that it should be by one stripe.
4074 * This only happens when reshape hits sync_max and pauses.
4075 * So allow wait_backup to either extent sync_max further
4076 * than strictly necessary, or return before the
4077 * sync has got quite as far as we would really like.
4078 * This is what 'blocks2' is for.
4079 * The various caller give appropriate values so that
4082 /* FIXME return value is often ignored */
4083 static int forget_backup(int dests
, int *destfd
,
4084 unsigned long long *destoffsets
,
4088 * Erase backup 'part' (which is 0 or 1)
4094 bsb
.arraystart2
= __cpu_to_le64(0);
4095 bsb
.length2
= __cpu_to_le64(0);
4097 bsb
.arraystart
= __cpu_to_le64(0);
4098 bsb
.length
= __cpu_to_le64(0);
4100 bsb
.mtime
= __cpu_to_le64(time(0));
4102 for (i
= 0; i
< dests
; i
++) {
4103 bsb
.devstart
= __cpu_to_le64(destoffsets
[i
]/512);
4104 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
4105 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0)
4106 bsb
.sb_csum2
= bsb_csum((char*)&bsb
,
4107 ((char*)&bsb
.sb_csum2
)-((char*)&bsb
));
4108 if ((unsigned long long)lseek64(destfd
[i
], destoffsets
[i
]-4096, 0) !=
4109 destoffsets
[i
]-4096)
4112 write(destfd
[i
], &bsb
, 512) != 512)
4119 static void fail(char *msg
)
4122 rv
= (write(2, msg
, strlen(msg
)) != (int)strlen(msg
));
4123 rv
|= (write(2, "\n", 1) != 1);
4127 static char *abuf
, *bbuf
;
4128 static unsigned long long abuflen
;
4129 static void validate(int afd
, int bfd
, unsigned long long offset
)
4131 /* check that the data in the backup against the array.
4132 * This is only used for regression testing and should not
4133 * be used while the array is active
4137 lseek64(bfd
, offset
- 4096, 0);
4138 if (read(bfd
, &bsb2
, 512) != 512)
4139 fail("cannot read bsb");
4140 if (bsb2
.sb_csum
!= bsb_csum((char*)&bsb2
,
4141 ((char*)&bsb2
.sb_csum
)-((char*)&bsb2
)))
4142 fail("first csum bad");
4143 if (memcmp(bsb2
.magic
, "md_backup_data", 14) != 0)
4144 fail("magic is bad");
4145 if (memcmp(bsb2
.magic
, "md_backup_data-2", 16) == 0 &&
4146 bsb2
.sb_csum2
!= bsb_csum((char*)&bsb2
,
4147 ((char*)&bsb2
.sb_csum2
)-((char*)&bsb2
)))
4148 fail("second csum bad");
4150 if (__le64_to_cpu(bsb2
.devstart
)*512 != offset
)
4151 fail("devstart is wrong");
4154 unsigned long long len
= __le64_to_cpu(bsb2
.length
)*512;
4156 if (abuflen
< len
) {
4160 if (posix_memalign((void**)&abuf
, 4096, abuflen
) ||
4161 posix_memalign((void**)&bbuf
, 4096, abuflen
)) {
4163 /* just stop validating on mem-alloc failure */
4168 lseek64(bfd
, offset
, 0);
4169 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
) {
4170 //printf("len %llu\n", len);
4171 fail("read first backup failed");
4173 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart
)*512, 0);
4174 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
4175 fail("read first from array failed");
4176 if (memcmp(bbuf
, abuf
, len
) != 0) {
4179 printf("offset=%llu len=%llu\n",
4180 (unsigned long long)__le64_to_cpu(bsb2
.arraystart
)*512, len
);
4181 for (i
=0; i
<len
; i
++)
4182 if (bbuf
[i
] != abuf
[i
]) {
4183 printf("first diff byte %d\n", i
);
4187 fail("data1 compare failed");
4191 unsigned long long len
= __le64_to_cpu(bsb2
.length2
)*512;
4193 if (abuflen
< len
) {
4197 abuf
= xmalloc(abuflen
);
4198 bbuf
= xmalloc(abuflen
);
4201 lseek64(bfd
, offset
+__le64_to_cpu(bsb2
.devstart2
)*512, 0);
4202 if ((unsigned long long)read(bfd
, bbuf
, len
) != len
)
4203 fail("read second backup failed");
4204 lseek64(afd
, __le64_to_cpu(bsb2
.arraystart2
)*512, 0);
4205 if ((unsigned long long)read(afd
, abuf
, len
) != len
)
4206 fail("read second from array failed");
4207 if (memcmp(bbuf
, abuf
, len
) != 0)
4208 fail("data2 compare failed");
4212 int child_monitor(int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
4213 struct supertype
*st
, unsigned long blocks
,
4214 int *fds
, unsigned long long *offsets
,
4215 int dests
, int *destfd
, unsigned long long *destoffsets
)
4217 /* Monitor a reshape where backup is being performed using
4218 * 'native' mechanism - either to a backup file, or
4219 * to some space in a spare.
4223 unsigned long long speed
;
4224 unsigned long long suspend_point
, array_size
;
4225 unsigned long long backup_point
, wait_point
;
4226 unsigned long long reshape_completed
;
4228 int increasing
= reshape
->after
.data_disks
>= reshape
->before
.data_disks
;
4229 int part
= 0; /* The next part of the backup area to fill. It may already
4230 * be full, so we need to check */
4231 int level
= reshape
->level
;
4232 int layout
= reshape
->before
.layout
;
4233 int data
= reshape
->before
.data_disks
;
4234 int disks
= reshape
->before
.data_disks
+ reshape
->parity
;
4235 int chunk
= sra
->array
.chunk_size
;
4237 unsigned long stripes
;
4241 /* set up the backup-super-block. This requires the
4242 * uuid from the array.
4244 /* Find a superblock */
4245 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
4249 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
4251 dn
= map_dev(sd
->disk
.major
, sd
->disk
.minor
, 1);
4252 devfd
= dev_open(dn
, O_RDONLY
);
4255 ok
= st
->ss
->load_super(st
, devfd
, NULL
);
4261 pr_err("Cannot find a superblock\n");
4265 memset(&bsb
, 0, 512);
4266 memcpy(bsb
.magic
, "md_backup_data-1", 16);
4267 st
->ss
->uuid_from_super(st
, uuid
);
4268 memcpy(bsb
.set_uuid
, uuid
, 16);
4269 bsb
.mtime
= __cpu_to_le64(time(0));
4270 bsb
.devstart2
= blocks
;
4272 stripes
= blocks
/ (sra
->array
.chunk_size
/512) /
4273 reshape
->before
.data_disks
;
4275 if (posix_memalign((void**)&buf
, 4096, disks
* chunk
))
4276 /* Don't start the 'reshape' */
4278 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
) {
4279 sysfs_get_ll(sra
, NULL
, "sync_speed_min", &speed
);
4280 sysfs_set_num(sra
, NULL
, "sync_speed_min", 200000);
4284 array_size
= sra
->component_size
* reshape
->after
.data_disks
;
4285 backup_point
= sra
->reshape_progress
;
4288 array_size
= sra
->component_size
* reshape
->before
.data_disks
;
4289 backup_point
= reshape
->backup_blocks
;
4290 suspend_point
= array_size
;
4296 /* Want to return as soon the oldest backup slot can
4297 * be released as that allows us to start backing up
4298 * some more, providing suspend_point has been
4299 * advanced, which it should have.
4302 wait_point
= array_size
;
4303 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
4304 wait_point
= (__le64_to_cpu(bsb
.arraystart
) +
4305 __le64_to_cpu(bsb
.length
));
4306 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
4307 wait_point
= (__le64_to_cpu(bsb
.arraystart2
) +
4308 __le64_to_cpu(bsb
.length2
));
4311 if (part
== 0 && __le64_to_cpu(bsb
.length
) > 0)
4312 wait_point
= __le64_to_cpu(bsb
.arraystart
);
4313 if (part
== 1 && __le64_to_cpu(bsb
.length2
) > 0)
4314 wait_point
= __le64_to_cpu(bsb
.arraystart2
);
4317 reshape_completed
= sra
->reshape_progress
;
4318 rv
= progress_reshape(sra
, reshape
,
4319 backup_point
, wait_point
,
4320 &suspend_point
, &reshape_completed
,
4322 /* external metadata would need to ping_monitor here */
4323 sra
->reshape_progress
= reshape_completed
;
4325 /* Clear any backup region that is before 'here' */
4327 if (__le64_to_cpu(bsb
.length
) > 0 &&
4328 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart
) +
4329 __le64_to_cpu(bsb
.length
)))
4330 forget_backup(dests
, destfd
,
4332 if (__le64_to_cpu(bsb
.length2
) > 0 &&
4333 reshape_completed
>= (__le64_to_cpu(bsb
.arraystart2
) +
4334 __le64_to_cpu(bsb
.length2
)))
4335 forget_backup(dests
, destfd
,
4338 if (__le64_to_cpu(bsb
.length
) > 0 &&
4339 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart
)))
4340 forget_backup(dests
, destfd
,
4342 if (__le64_to_cpu(bsb
.length2
) > 0 &&
4343 reshape_completed
<= (__le64_to_cpu(bsb
.arraystart2
)))
4344 forget_backup(dests
, destfd
,
4354 if (rv
== 0 && increasing
&& !st
->ss
->external
) {
4355 /* No longer need to monitor this reshape */
4356 sysfs_set_str(sra
, NULL
, "sync_max", "max");
4362 unsigned long long offset
;
4363 unsigned long actual_stripes
;
4364 /* Need to backup some data.
4365 * If 'part' is not used and the desired
4366 * backup size is suspended, do a backup,
4367 * then consider the next part.
4369 /* Check that 'part' is unused */
4370 if (part
== 0 && __le64_to_cpu(bsb
.length
) != 0)
4372 if (part
== 1 && __le64_to_cpu(bsb
.length2
) != 0)
4375 offset
= backup_point
/ data
;
4376 actual_stripes
= stripes
;
4378 if (offset
+ actual_stripes
* (chunk
/512) >
4379 sra
->component_size
)
4380 actual_stripes
= ((sra
->component_size
- offset
)
4382 if (offset
+ actual_stripes
* (chunk
/512) >
4386 if (offset
< actual_stripes
* (chunk
/512))
4387 actual_stripes
= offset
/ (chunk
/512);
4388 offset
-= actual_stripes
* (chunk
/512);
4389 if (offset
< suspend_point
/data
)
4392 if (actual_stripes
== 0)
4394 grow_backup(sra
, offset
, actual_stripes
,
4396 disks
, chunk
, level
, layout
,
4397 dests
, destfd
, destoffsets
,
4398 part
, °raded
, buf
);
4399 validate(afd
, destfd
[0], destoffsets
[0]);
4400 /* record where 'part' is up to */
4403 backup_point
+= actual_stripes
* (chunk
/512) * data
;
4405 backup_point
-= actual_stripes
* (chunk
/512) * data
;
4409 /* FIXME maybe call progress_reshape one more time instead */
4410 /* remove any remaining suspension */
4411 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
4412 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
4413 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
4414 sysfs_set_num(sra
, NULL
, "sync_min", 0);
4416 if (reshape
->before
.data_disks
== reshape
->after
.data_disks
)
4417 sysfs_set_num(sra
, NULL
, "sync_speed_min", speed
);
4423 * If any spare contains md_back_data-1 which is recent wrt mtime,
4424 * write that data into the array and update the super blocks with
4425 * the new reshape_progress
4427 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
, int cnt
,
4428 char *backup_file
, int verbose
)
4432 unsigned long long *offsets
;
4433 unsigned long long nstripe
, ostripe
;
4436 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
4437 if (info
->array
.level
== 6) odata
--; /* number of data disks */
4438 ndata
= info
->array
.raid_disks
- 1;
4439 if (info
->new_level
== 6) ndata
--;
4441 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
4443 if (info
->delta_disks
<= 0)
4444 /* Didn't grow, so the backup file must have
4448 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
4449 struct mdinfo dinfo
;
4452 char *devname
, namebuf
[20];
4453 unsigned long long lo
, hi
;
4455 /* This was a spare and may have some saved data on it.
4456 * Load the superblock, find and load the
4457 * backup_super_block.
4458 * If either fail, go on to next device.
4459 * If the backup contains no new info, just return
4460 * else restore data and update all superblocks
4462 if (i
== old_disks
-1) {
4463 fd
= open(backup_file
, O_RDONLY
);
4465 pr_err("backup file %s inaccessible: %s\n",
4466 backup_file
, strerror(errno
));
4469 devname
= backup_file
;
4474 if (st
->ss
->load_super(st
, fd
, NULL
))
4477 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4478 st
->ss
->free_super(st
);
4481 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
4483 pr_err("Cannot seek on device %d\n", i
);
4484 continue; /* Cannot seek */
4486 sprintf(namebuf
, "device-%d", i
);
4489 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
)) {
4491 pr_err("Cannot read from %s\n", devname
);
4492 continue; /* Cannot read */
4494 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0 &&
4495 memcmp(bsb
.magic
, "md_backup_data-2", 16) != 0) {
4497 pr_err("No backup metadata on %s\n", devname
);
4500 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
))) {
4502 pr_err("Bad backup-metadata checksum on %s\n", devname
);
4503 continue; /* bad checksum */
4505 if (memcmp(bsb
.magic
, "md_backup_data-2", 16) == 0 &&
4506 bsb
.sb_csum2
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum2
)-((char*)&bsb
))) {
4508 pr_err("Bad backup-metadata checksum2 on %s\n", devname
);
4509 continue; /* Bad second checksum */
4511 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0) {
4513 pr_err("Wrong uuid on backup-metadata on %s\n", devname
);
4514 continue; /* Wrong uuid */
4517 /* array utime and backup-mtime should be updated at much the same time, but it seems that
4518 * sometimes they aren't... So allow considerable flexability in matching, and allow
4519 * this test to be overridden by an environment variable.
4521 if (info
->array
.utime
> (int)__le64_to_cpu(bsb
.mtime
) + 2*60*60 ||
4522 info
->array
.utime
< (int)__le64_to_cpu(bsb
.mtime
) - 10*60) {
4523 if (check_env("MDADM_GROW_ALLOW_OLD")) {
4524 pr_err("accepting backup with timestamp %lu "
4525 "for array with timestamp %lu\n",
4526 (unsigned long)__le64_to_cpu(bsb
.mtime
),
4527 (unsigned long)info
->array
.utime
);
4529 pr_err("too-old timestamp on backup-metadata on %s\n", devname
);
4530 pr_err("If you think it is should be safe, try 'export MDADM_GROW_ALLOW_OLD=1'\n");
4531 continue; /* time stamp is too bad */
4535 if (bsb
.magic
[15] == '1') {
4536 if (bsb
.length
== 0)
4538 if (info
->delta_disks
>= 0) {
4539 /* reshape_progress is increasing */
4540 if (__le64_to_cpu(bsb
.arraystart
)
4541 + __le64_to_cpu(bsb
.length
)
4542 < info
->reshape_progress
) {
4545 pr_err("backup-metadata found on %s but is not needed\n", devname
);
4546 continue; /* No new data here */
4549 /* reshape_progress is decreasing */
4550 if (__le64_to_cpu(bsb
.arraystart
) >=
4551 info
->reshape_progress
)
4552 goto nonew
; /* No new data here */
4555 if (bsb
.length
== 0 && bsb
.length2
== 0)
4557 if (info
->delta_disks
>= 0) {
4558 /* reshape_progress is increasing */
4559 if ((__le64_to_cpu(bsb
.arraystart
)
4560 + __le64_to_cpu(bsb
.length
)
4561 < info
->reshape_progress
)
4563 (__le64_to_cpu(bsb
.arraystart2
)
4564 + __le64_to_cpu(bsb
.length2
)
4565 < info
->reshape_progress
))
4566 goto nonew
; /* No new data here */
4568 /* reshape_progress is decreasing */
4569 if (__le64_to_cpu(bsb
.arraystart
) >=
4570 info
->reshape_progress
&&
4571 __le64_to_cpu(bsb
.arraystart2
) >=
4572 info
->reshape_progress
)
4573 goto nonew
; /* No new data here */
4576 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0) {
4579 pr_err("Failed to verify secondary backup-metadata block on %s\n",
4581 continue; /* Cannot seek */
4583 /* There should be a duplicate backup superblock 4k before here */
4584 if (lseek64(fd
, -4096, 1) < 0 ||
4585 read(fd
, &bsb2
, sizeof(bsb2
)) != sizeof(bsb2
))
4586 goto second_fail
; /* Cannot find leading superblock */
4587 if (bsb
.magic
[15] == '1')
4588 bsbsize
= offsetof(struct mdp_backup_super
, pad1
);
4590 bsbsize
= offsetof(struct mdp_backup_super
, pad
);
4591 if (memcmp(&bsb2
, &bsb
, bsbsize
) != 0)
4592 goto second_fail
; /* Cannot find leading superblock */
4594 /* Now need the data offsets for all devices. */
4595 offsets
= xmalloc(sizeof(*offsets
)*info
->array
.raid_disks
);
4596 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
4599 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
4600 /* FIXME should be this be an error */
4602 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4603 st
->ss
->free_super(st
);
4604 offsets
[j
] = dinfo
.data_offset
* 512;
4606 printf(Name
": restoring critical section\n");
4608 if (restore_stripes(fdlist
, offsets
,
4609 info
->array
.raid_disks
,
4613 fd
, __le64_to_cpu(bsb
.devstart
)*512,
4614 __le64_to_cpu(bsb
.arraystart
)*512,
4615 __le64_to_cpu(bsb
.length
)*512, NULL
)) {
4616 /* didn't succeed, so giveup */
4618 pr_err("Error restoring backup from %s\n",
4624 if (bsb
.magic
[15] == '2' &&
4625 restore_stripes(fdlist
, offsets
,
4626 info
->array
.raid_disks
,
4630 fd
, __le64_to_cpu(bsb
.devstart
)*512 +
4631 __le64_to_cpu(bsb
.devstart2
)*512,
4632 __le64_to_cpu(bsb
.arraystart2
)*512,
4633 __le64_to_cpu(bsb
.length2
)*512, NULL
)) {
4634 /* didn't succeed, so giveup */
4636 pr_err("Error restoring second backup from %s\n",
4644 /* Ok, so the data is restored. Let's update those superblocks. */
4648 lo
= __le64_to_cpu(bsb
.arraystart
);
4649 hi
= lo
+ __le64_to_cpu(bsb
.length
);
4651 if (bsb
.magic
[15] == '2' && bsb
.length2
) {
4652 unsigned long long lo1
, hi1
;
4653 lo1
= __le64_to_cpu(bsb
.arraystart2
);
4654 hi1
= lo1
+ __le64_to_cpu(bsb
.length2
);
4658 } else if (lo
< lo1
)
4664 (info
->reshape_progress
< lo
||
4665 info
->reshape_progress
> hi
))
4666 /* backup does not affect reshape_progress*/ ;
4667 else if (info
->delta_disks
>= 0) {
4668 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
) +
4669 __le64_to_cpu(bsb
.length
);
4670 if (bsb
.magic
[15] == '2') {
4671 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
) +
4672 __le64_to_cpu(bsb
.length2
);
4673 if (p2
> info
->reshape_progress
)
4674 info
->reshape_progress
= p2
;
4677 info
->reshape_progress
= __le64_to_cpu(bsb
.arraystart
);
4678 if (bsb
.magic
[15] == '2') {
4679 unsigned long long p2
= __le64_to_cpu(bsb
.arraystart2
);
4680 if (p2
< info
->reshape_progress
)
4681 info
->reshape_progress
= p2
;
4684 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
4687 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
4689 st
->ss
->getinfo_super(st
, &dinfo
, NULL
);
4690 dinfo
.reshape_progress
= info
->reshape_progress
;
4691 st
->ss
->update_super(st
, &dinfo
,
4692 "_reshape_progress",
4694 st
->ss
->store_super(st
, fdlist
[j
]);
4695 st
->ss
->free_super(st
);
4699 /* Didn't find any backup data, try to see if any
4702 if (info
->delta_disks
< 0) {
4703 /* When shrinking, the critical section is at the end.
4704 * So see if we are before the critical section.
4706 unsigned long long first_block
;
4707 nstripe
= ostripe
= 0;
4709 while (ostripe
>= nstripe
) {
4710 ostripe
+= info
->array
.chunk_size
/ 512;
4711 first_block
= ostripe
* odata
;
4712 nstripe
= first_block
/ ndata
/ (info
->new_chunk
/512) *
4713 (info
->new_chunk
/512);
4716 if (info
->reshape_progress
>= first_block
)
4719 if (info
->delta_disks
> 0) {
4720 /* See if we are beyond the critical section. */
4721 unsigned long long last_block
;
4722 nstripe
= ostripe
= 0;
4724 while (nstripe
>= ostripe
) {
4725 nstripe
+= info
->new_chunk
/ 512;
4726 last_block
= nstripe
* ndata
;
4727 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
4728 (info
->array
.chunk_size
/512);
4731 if (info
->reshape_progress
>= last_block
)
4734 /* needed to recover critical section! */
4736 pr_err("Failed to find backup of critical section\n");
4740 int Grow_continue_command(char *devname
, int fd
,
4741 char *backup_file
, int verbose
)
4744 struct supertype
*st
= NULL
;
4745 struct mdinfo
*content
= NULL
;
4746 struct mdinfo array
;
4747 char *subarray
= NULL
;
4748 struct mdinfo
*cc
= NULL
;
4749 struct mdstat_ent
*mdstat
= NULL
;
4753 dprintf("Grow continue from command line called for %s\n",
4756 st
= super_by_fd(fd
, &subarray
);
4757 if (!st
|| !st
->ss
) {
4758 pr_err("Unable to determine metadata format for %s\n",
4762 dprintf("Grow continue is run for ");
4763 if (st
->ss
->external
== 0) {
4765 dprintf("native array (%s)\n", devname
);
4766 if (ioctl(fd
, GET_ARRAY_INFO
, &array
.array
) < 0) {
4767 pr_err("%s is not an active md array -"
4768 " aborting\n", devname
);
4770 goto Grow_continue_command_exit
;
4773 /* Need to load a superblock.
4774 * FIXME we should really get what we need from
4777 for (d
= 0; d
< MAX_DISKS
; d
++) {
4778 mdu_disk_info_t disk
;
4782 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
4784 if (disk
.major
== 0 && disk
.minor
== 0)
4786 if ((disk
.state
& (1 << MD_DISK_ACTIVE
)) == 0)
4788 dv
= map_dev(disk
.major
, disk
.minor
, 1);
4791 fd2
= dev_open(dv
, O_RDONLY
);
4794 err
= st
->ss
->load_super(st
, fd2
, NULL
);
4796 /* invalidate fd2 to avoid possible double close() */
4802 if (d
== MAX_DISKS
) {
4803 pr_err("Unable to load metadata for %s\n",
4806 goto Grow_continue_command_exit
;
4808 st
->ss
->getinfo_super(st
, content
, NULL
);
4813 dprintf("subarray (%s)\n", subarray
);
4814 container
= st
->container_devnm
;
4815 cfd
= open_dev_excl(st
->container_devnm
);
4817 container
= st
->devnm
;
4819 cfd
= open_dev_excl(st
->devnm
);
4820 dprintf("container (%s)\n", container
);
4824 pr_err("Unable to open container "
4825 "for %s\n", devname
);
4827 goto Grow_continue_command_exit
;
4830 /* find in container array under reshape
4832 ret_val
= st
->ss
->load_container(st
, cfd
, NULL
);
4834 pr_err("Cannot read superblock for %s\n",
4837 goto Grow_continue_command_exit
;
4840 cc
= st
->ss
->container_content(st
, subarray
);
4841 for (content
= cc
; content
; content
= content
->next
) {
4843 int allow_reshape
= 1;
4845 if (content
->reshape_active
== 0)
4847 /* The decision about array or container wide
4848 * reshape is taken in Grow_continue based
4849 * content->reshape_active state, therefore we
4850 * need to check_reshape based on
4851 * reshape_active and subarray name
4853 if (content
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
))
4855 if (content
->reshape_active
== CONTAINER_RESHAPE
&&
4856 (content
->array
.state
4857 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
)))
4860 if (!allow_reshape
) {
4861 pr_err("cannot continue reshape of an array"
4862 " in container with unsupported"
4863 " metadata: %s(%s)\n",
4864 devname
, container
);
4866 goto Grow_continue_command_exit
;
4869 array
= strchr(content
->text_version
+1, '/')+1;
4870 mdstat
= mdstat_by_subdev(array
, container
);
4873 if (mdstat
->active
== 0) {
4874 pr_err("Skipping inactive array %s.\n",
4876 free_mdstat(mdstat
);
4883 pr_err("Unable to determine reshaped "
4884 "array for %s\n", devname
);
4886 goto Grow_continue_command_exit
;
4888 fd2
= open_dev(mdstat
->devnm
);
4890 pr_err("cannot open (%s)\n", mdstat
->devnm
);
4892 goto Grow_continue_command_exit
;
4895 sysfs_init(content
, fd2
, mdstat
->devnm
);
4897 /* start mdmon in case it is not running
4899 if (!mdmon_running(container
))
4900 start_mdmon(container
);
4901 ping_monitor(container
);
4903 if (mdmon_running(container
))
4904 st
->update_tail
= &st
->updates
;
4906 pr_err("No mdmon found. "
4907 "Grow cannot continue.\n");
4909 goto Grow_continue_command_exit
;
4913 /* verify that array under reshape is started from
4916 if (verify_reshape_position(content
, content
->array
.level
) < 0) {
4918 goto Grow_continue_command_exit
;
4923 ret_val
= Grow_continue(fd
, st
, content
, backup_file
, 0);
4925 Grow_continue_command_exit
:
4930 st
->ss
->free_super(st
);
4931 free_mdstat(mdstat
);
4938 int Grow_continue(int mdfd
, struct supertype
*st
, struct mdinfo
*info
,
4939 char *backup_file
, int freeze_reshape
)
4943 if (!info
->reshape_active
)
4946 if (st
->ss
->external
) {
4947 int cfd
= open_dev(st
->container_devnm
);
4952 st
->ss
->load_container(st
, cfd
, st
->container_devnm
);
4954 ret_val
= reshape_container(st
->container_devnm
, NULL
, mdfd
,
4955 st
, info
, 0, backup_file
,
4957 1 | info
->reshape_active
,
4960 ret_val
= reshape_array(NULL
, mdfd
, "array", st
, info
, 1,
4961 NULL
, INVALID_SECTORS
,
4963 1 | info
->reshape_active
,
4969 char *make_backup(char *name
)
4971 char *base
= "backup_file-";
4975 len
= strlen(MAP_DIR
) + 1 + strlen(base
) + strlen(name
)+1;
4976 fname
= xmalloc(len
);
4977 sprintf(fname
, "%s/%s%s", MAP_DIR
, base
, name
);
4981 char *locate_backup(char *name
)
4983 char *fl
= make_backup(name
);
4986 if (stat(fl
, &stb
) == 0 &&
4987 S_ISREG(stb
.st_mode
))