]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Grow.c
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Email: <neilb@suse.de>
27 #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
28 #error no endian defined
33 int Grow_Add_device(char *devname
, int fd
, char *newdev
)
35 /* Add a device to an active array.
36 * Currently, just extend a linear array.
37 * This requires writing a new superblock on the
38 * new device, calling the kernel to add the device,
39 * and if that succeeds, update the superblock on
41 * This means that we need to *find* all other devices.
48 struct supertype
*st
= NULL
;
51 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
52 fprintf(stderr
, Name
": cannot get array info for %s\n", devname
);
58 fprintf(stderr
, Name
": cannot handle arrays with superblock version %d\n", info
.array
.major_version
);
62 if (info
.array
.level
!= -1) {
63 fprintf(stderr
, Name
": can only add devices to linear arrays\n");
67 nfd
= open(newdev
, O_RDWR
|O_EXCL
|O_DIRECT
);
69 fprintf(stderr
, Name
": cannot open %s\n", newdev
);
73 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
74 fprintf(stderr
, Name
": %s is not a block device!\n", newdev
);
78 /* now check out all the devices and make sure we can read the superblock */
79 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
84 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
85 fprintf(stderr
, Name
": cannot get device detail for device %d\n",
89 dv
= map_dev(disk
.major
, disk
.minor
, 1);
91 fprintf(stderr
, Name
": cannot find device file for device %d\n",
95 fd2
= dev_open(dv
, O_RDWR
);
97 fprintf(stderr
, Name
": cannot open device file %s\n", dv
);
100 st
->ss
->free_super(st
);
102 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
103 fprintf(stderr
, Name
": cannot find super block on %s\n", dv
);
109 /* Ok, looks good. Lets update the superblock and write it out to
113 info
.disk
.number
= d
;
114 info
.disk
.major
= major(stb
.st_rdev
);
115 info
.disk
.minor
= minor(stb
.st_rdev
);
116 info
.disk
.raid_disk
= d
;
117 info
.disk
.state
= (1 << MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
);
118 st
->ss
->update_super(st
, &info
, "linear-grow-new", newdev
,
121 if (st
->ss
->store_super(st
, nfd
)) {
122 fprintf(stderr
, Name
": Cannot store new superblock on %s\n",
129 if (ioctl(fd
, ADD_NEW_DISK
, &info
.disk
) != 0) {
130 fprintf(stderr
, Name
": Cannot add new disk to this array\n");
133 /* Well, that seems to have worked.
134 * Now go through and update all superblocks
137 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
138 fprintf(stderr
, Name
": cannot get array info for %s\n", devname
);
143 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
144 mdu_disk_info_t disk
;
148 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
149 fprintf(stderr
, Name
": cannot get device detail for device %d\n",
153 dv
= map_dev(disk
.major
, disk
.minor
, 1);
155 fprintf(stderr
, Name
": cannot find device file for device %d\n",
159 fd2
= dev_open(dv
, O_RDWR
);
161 fprintf(stderr
, Name
": cannot open device file %s\n", dv
);
164 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
165 fprintf(stderr
, Name
": cannot find super block on %s\n", dv
);
169 info
.array
.raid_disks
= nd
+1;
170 info
.array
.nr_disks
= nd
+1;
171 info
.array
.active_disks
= nd
+1;
172 info
.array
.working_disks
= nd
+1;
174 st
->ss
->update_super(st
, &info
, "linear-grow-update", dv
,
177 if (st
->ss
->store_super(st
, fd2
)) {
178 fprintf(stderr
, Name
": Cannot store new superblock on %s\n", dv
);
188 int Grow_addbitmap(char *devname
, int fd
, char *file
, int chunk
, int delay
, int write_behind
, int force
)
191 * First check that array doesn't have a bitmap
192 * Then create the bitmap
195 * For internal bitmaps, we need to check the version,
196 * find all the active devices, and write the bitmap block
199 mdu_bitmap_file_t bmf
;
200 mdu_array_info_t array
;
201 struct supertype
*st
;
202 int major
= BITMAP_MAJOR_HI
;
203 int vers
= md_get_version(fd
);
204 unsigned long long bitmapsize
, array_size
;
207 major
= BITMAP_MAJOR_HOSTENDIAN
;
209 fprintf(stderr
, Name
": Warning - bitmaps created on this kernel are not portable\n"
210 " between different architectured. Consider upgrading the Linux kernel.\n");
214 if (ioctl(fd
, GET_BITMAP_FILE
, &bmf
) != 0) {
216 fprintf(stderr
, Name
": Memory allocation failure.\n");
218 fprintf(stderr
, Name
": bitmaps not supported by this kernel.\n");
221 if (bmf
.pathname
[0]) {
222 if (strcmp(file
,"none")==0) {
223 if (ioctl(fd
, SET_BITMAP_FILE
, -1)!= 0) {
224 fprintf(stderr
, Name
": failed to remove bitmap %s\n",
230 fprintf(stderr
, Name
": %s already has a bitmap (%s)\n",
231 devname
, bmf
.pathname
);
234 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
235 fprintf(stderr
, Name
": cannot get array status for %s\n", devname
);
238 if (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)) {
239 if (strcmp(file
, "none")==0) {
240 array
.state
&= ~(1<<MD_SB_BITMAP_PRESENT
);
241 if (ioctl(fd
, SET_ARRAY_INFO
, &array
)!= 0) {
242 fprintf(stderr
, Name
": failed to remove internal bitmap.\n");
247 fprintf(stderr
, Name
": Internal bitmap already present on %s\n",
251 if (array
.level
<= 0) {
252 fprintf(stderr
, Name
": Bitmaps not meaningful with level %s\n",
253 map_num(pers
, array
.level
)?:"of this array");
256 bitmapsize
= array
.size
;
258 if (get_dev_size(fd
, NULL
, &array_size
) &&
259 array_size
> (0x7fffffffULL
<<9)) {
260 /* Array is big enough that we cannot trust array.size
261 * try other approaches
263 bitmapsize
= get_component_size(fd
);
265 if (bitmapsize
== 0) {
266 fprintf(stderr
, Name
": Cannot reliably determine size of array to create bitmap - sorry.\n");
270 if (array
.level
== 10) {
271 int ncopies
= (array
.layout
&255)*((array
.layout
>>8)&255);
272 bitmapsize
= bitmapsize
* array
.raid_disks
/ ncopies
;
275 st
= super_by_fd(fd
);
277 fprintf(stderr
, Name
": Cannot understand version %d.%d\n",
278 array
.major_version
, array
.minor_version
);
281 if (strcmp(file
, "none") == 0) {
282 fprintf(stderr
, Name
": no bitmap found on %s\n", devname
);
284 } else if (strcmp(file
, "internal") == 0) {
286 for (d
=0; d
< st
->max_devs
; d
++) {
287 mdu_disk_info_t disk
;
290 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
292 if (disk
.major
== 0 &&
295 if ((disk
.state
& (1<<MD_DISK_SYNC
))==0)
297 dv
= map_dev(disk
.major
, disk
.minor
, 1);
299 int fd2
= dev_open(dv
, O_RDWR
);
302 if (st
->ss
->load_super(st
, fd2
, NULL
)==0) {
303 if (st
->ss
->add_internal_bitmap(
305 &chunk
, delay
, write_behind
,
306 bitmapsize
, 0, major
)
308 st
->ss
->write_bitmap(st
, fd2
);
310 fprintf(stderr
, Name
": failed to create internal bitmap - chunksize problem.\n");
318 array
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
319 if (ioctl(fd
, SET_ARRAY_INFO
, &array
)!= 0) {
320 fprintf(stderr
, Name
": failed to set internal bitmap.\n");
327 int max_devs
= st
->max_devs
;
329 /* try to load a superblock */
330 for (d
=0; d
<max_devs
; d
++) {
331 mdu_disk_info_t disk
;
335 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
337 if ((disk
.major
==0 && disk
.minor
==0) ||
338 (disk
.state
& (1<<MD_DISK_REMOVED
)))
340 dv
= map_dev(disk
.major
, disk
.minor
, 1);
342 fd2
= dev_open(dv
, O_RDONLY
);
344 st
->ss
->load_super(st
, fd2
, NULL
) == 0) {
346 st
->ss
->uuid_from_super(st
, uuid
);
352 fprintf(stderr
, Name
": cannot find UUID for array!\n");
355 if (CreateBitmap(file
, force
, (char*)uuid
, chunk
,
356 delay
, write_behind
, bitmapsize
, major
)) {
359 bitmap_fd
= open(file
, O_RDWR
);
361 fprintf(stderr
, Name
": weird: %s cannot be opened\n",
365 if (ioctl(fd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
366 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
367 devname
, strerror(errno
));
377 * When reshaping an array we might need to backup some data.
378 * This is written to all spares with a 'super_block' describing it.
379 * The superblock goes 1K form the end of the used space on the
381 * It if written after the backup is complete.
382 * It has the following structure.
385 struct mdp_backup_super
{
386 char magic
[16]; /* md_backup_data-1 */
389 /* start/sizes in 512byte sectors */
393 __u32 sb_csum
; /* csum of preceeding bytes. */
395 } __attribute__((aligned(512))) bsb
;
397 int bsb_csum(char *buf
, int len
)
401 for (i
=0; i
<len
; i
++)
402 csum
= (csum
<<3) + buf
[0];
403 return __cpu_to_le32(csum
);
406 int Grow_reshape(char *devname
, int fd
, int quiet
, char *backup_file
,
408 int level
, int layout
, int chunksize
, int raid_disks
)
410 /* Make some changes in the shape of an array.
411 * The kernel must support the change.
412 * Different reshapes have subtly different meaning for different
413 * levels, so we need to check the current state of the array
416 struct mdu_array_info_s array
;
419 struct supertype
*st
;
423 int nlayout
, olayout
;
426 unsigned long long nstripe
, ostripe
, last_block
;
428 unsigned long long *offsets
;
436 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) < 0) {
437 fprintf(stderr
, Name
": %s is not an active md array - aborting\n",
441 c
= map_num(pers
, array
.level
);
442 if (c
== NULL
) c
= "-unknown-";
443 switch(array
.level
) {
444 default: /* raid0, linear, multipath cannot be reconfigured */
445 fprintf(stderr
, Name
": %s array %s cannot be reshaped.\n",
449 case LEVEL_FAULTY
: /* only 'layout' change is permitted */
452 fprintf(stderr
, Name
": %s: Cannot change size of a 'faulty' array\n",
456 if (level
!= UnSet
&& level
!= LEVEL_FAULTY
) {
457 fprintf(stderr
, Name
": %s: Cannot change RAID level of a 'faulty' array\n",
461 if (chunksize
|| raid_disks
) {
462 fprintf(stderr
, Name
": %s: Cannot change chunksize or disks of a 'faulty' array\n",
467 return 0; /* nothing to do.... */
469 array
.layout
= layout
;
470 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
471 fprintf(stderr
, Name
": Cannot set layout for %s: %s\n",
472 devname
, strerror(errno
));
476 printf("layout for %s set to %d\n", devname
, array
.layout
);
479 case 1: /* raid_disks and size can each be changed. They are independant */
481 if (level
!= UnSet
&& level
!= 1) {
482 fprintf(stderr
, Name
": %s: Cannot change RAID level of a RAID1 array.\n",
486 if (chunksize
|| layout
!= UnSet
) {
487 fprintf(stderr
, Name
": %s: Cannot change chunk size of layout for a RAID1 array.\n",
492 /* Each can trigger a resync/recovery which will block the
493 * other from happening. Later we could block
494 * resync for the duration via 'sync_action'...
496 if (raid_disks
> 0) {
497 array
.raid_disks
= raid_disks
;
498 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
499 fprintf(stderr
, Name
": Cannot set raid-devices for %s: %s\n",
500 devname
, strerror(errno
));
506 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
507 fprintf(stderr
, Name
": Cannot set device size for %s: %s\n",
508 devname
, strerror(errno
));
517 st
= super_by_fd(fd
);
519 /* size can be changed independently.
520 * layout/chunksize/raid_disks/level can be changed
521 * though the kernel may not support it all.
522 * If 'suspend_lo' is not present in devfs, then
523 * these cannot be changed.
526 /* Cannot change other details as well.. */
527 if (layout
!= UnSet
||
531 fprintf(stderr
, Name
": %s: Cannot change shape as well as size of a %s array.\n",
536 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
537 fprintf(stderr
, Name
": Cannot set device size/shape for %s: %s\n",
538 devname
, strerror(errno
));
543 /* Ok, just change the shape. This can be awkward.
544 * There are three possibilities.
545 * 1/ The array will shrink. We don't support this
546 * possibility. Maybe one day...
547 * 2/ The array will not change size. This is easy enough
548 * to do, but not reliably. If the process is aborted
549 * the array *will* be corrupted. So maybe we can allow
550 * this but only if the user is really certain. e.g.
551 * --really-risk-everything
552 * 3/ The array will grow. This can be reliably achieved.
553 * However the kernel's restripe routines will cheerfully
554 * overwrite some early data before it is safe. So we
555 * need to make a backup of the early parts of the array
556 * and be ready to restore it if rebuild aborts very early.
558 * We backup data by writing it to all spares (there must be
559 * at least 1, so even raid6->raid5 requires a spare to be
562 * So: we enumerate the devices in the array and
563 * make sure we can open all of them.
564 * Then we freeze the early part of the array and
565 * backup to the various spares.
566 * Then we request changes and start the reshape.
567 * Monitor progress until it has passed the danger zone.
568 * and finally invalidate the copied data and unfreeze the
569 * start of the array.
571 * Before we can do this we need to decide:
572 * - will the array grow? Just calculate size
573 * - how much needs to be saved: count stripes.
574 * - where to save data... good question.
577 nlevel
= olevel
= array
.level
;
578 nchunk
= ochunk
= array
.chunk_size
;
579 nlayout
= olayout
= array
.layout
;
580 ndisks
= odisks
= array
.raid_disks
;
582 if (level
!= UnSet
) nlevel
= level
;
583 if (chunksize
) nchunk
= chunksize
;
584 if (layout
!= UnSet
) nlayout
= layout
;
585 if (raid_disks
) ndisks
= raid_disks
;
588 if (olevel
== 6) odata
--; /* number of data disks */
590 if (nlevel
== 6) ndata
--;
593 fprintf(stderr
, Name
": %s: Cannot reduce number of data disks (yet).\n",
597 if (ndata
== odata
) {
598 fprintf(stderr
, Name
": %s: Cannot reshape array without increasing size (yet).\n",
602 /* Well, it is growing... so how much do we need to backup.
603 * Need to backup a full number of new-stripes, such that the
604 * last one does not over-write any place that it would be read
607 nstripe
= ostripe
= 0;
608 while (nstripe
>= ostripe
) {
609 nstripe
+= nchunk
/512;
610 last_block
= nstripe
* ndata
;
611 ostripe
= last_block
/ odata
/ (ochunk
/512) * (ochunk
/512);
613 fprintf(stderr
, Name
": Need to backup %lluK of critical "
614 "section..\n", last_block
/2);
616 sra
= sysfs_read(fd
, 0,
617 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
620 fprintf(stderr
, Name
": %s: Cannot get array details from sysfs\n",
625 if (last_block
>= sra
->component_size
/2) {
626 fprintf(stderr
, Name
": %s: Something wrong - reshape aborted\n",
630 if (sra
->array
.spare_disks
== 0 && backup_file
== NULL
) {
631 fprintf(stderr
, Name
": %s: Cannot grow - need a spare or backup-file to backup critical section\n",
636 nrdisks
= array
.nr_disks
+ sra
->array
.spare_disks
;
637 /* Now we need to open all these devices so we can read/write.
639 fdlist
= malloc((1+nrdisks
) * sizeof(int));
640 offsets
= malloc((1+nrdisks
) * sizeof(offsets
[0]));
641 if (!fdlist
|| !offsets
) {
642 fprintf(stderr
, Name
": malloc failed: grow aborted\n");
645 for (d
=0; d
<= nrdisks
; d
++)
647 d
= array
.raid_disks
;
648 for (sd
= sra
->devs
; sd
; sd
=sd
->next
) {
649 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
651 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
652 char *dn
= map_dev(sd
->disk
.major
,
654 fdlist
[sd
->disk
.raid_disk
]
655 = dev_open(dn
, O_RDONLY
);
656 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
;
657 if (fdlist
[sd
->disk
.raid_disk
] < 0) {
658 fprintf(stderr
, Name
": %s: cannot open component %s\n",
659 devname
, dn
?dn
:"-unknown-");
664 char *dn
= map_dev(sd
->disk
.major
,
666 fdlist
[d
] = dev_open(dn
, O_RDWR
);
667 offsets
[d
] = sd
->data_offset
;
669 fprintf(stderr
, Name
": %s: cannot open component %s\n",
670 devname
, dn
?dn
:"-unknown");
676 for (i
=0 ; i
<array
.raid_disks
; i
++)
678 fprintf(stderr
, Name
": %s: failed to find device %d. Array might be degraded.\n"
679 " --grow aborted\n", devname
, i
);
682 spares
= sra
->array
.spare_disks
;
684 fdlist
[d
] = open(backup_file
, O_RDWR
|O_CREAT
|O_EXCL
, S_IRUSR
| S_IWUSR
);
686 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
687 devname
, backup_file
, strerror(errno
));
694 if (fdlist
[array
.raid_disks
] < 0) {
695 fprintf(stderr
, Name
": %s: failed to find a spare and no backup-file given - --grow aborted\n",
700 /* Find a superblock */
701 if (st
->ss
->load_super(st
, fdlist
[0], NULL
)) {
702 fprintf(stderr
, Name
": %s: Cannot find a superblock\n",
708 memcpy(bsb
.magic
, "md_backup_data-1", 16);
709 st
->ss
->uuid_from_super(st
, (int*)&bsb
.set_uuid
);
710 bsb
.mtime
= __cpu_to_le64(time(0));
712 bsb
.length
= __cpu_to_le64(last_block
);
714 /* Decide offset for the backup, llseek the spares, and write
715 * a leading superblock 4K earlier.
717 for (i
=array
.raid_disks
; i
<d
; i
++) {
719 char *buf
= (char*)(((unsigned long)abuf
+511)& ~511);
720 if (i
==d
-1 && backup_file
) {
721 /* This is the backup file */
724 offsets
[i
] += sra
->component_size
- last_block
- 8;
725 if (lseek64(fdlist
[i
], (offsets
[i
]<<9) - 4096, 0)
726 != (offsets
[i
]<<9) - 4096) {
727 fprintf(stderr
, Name
": could not seek...\n");
730 memset(buf
, 0, 4096);
731 bsb
.devstart
= __cpu_to_le64(offsets
[i
]);
732 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
733 memcpy(buf
, &bsb
, sizeof(bsb
));
734 if (write(fdlist
[i
], buf
, 4096) != 4096) {
735 fprintf(stderr
, Name
": could not write leading superblock\n");
739 array
.level
= nlevel
;
740 array
.raid_disks
= ndisks
;
741 array
.chunk_size
= nchunk
;
742 array
.layout
= nlayout
;
743 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
744 if (errno
== ENOSPC
) {
745 /* stripe cache is not big enough.
746 * It needs to be 4 times chunksize_size,
747 * and we assume pagesize is 4K
749 if (sra
->cache_size
< 4 * (nchunk
/4096)) {
750 sysfs_set_num(sra
, NULL
,
752 4 * (nchunk
/4096) +1);
753 if (ioctl(fd
, SET_ARRAY_INFO
,
758 fprintf(stderr
, Name
": Cannot set device size/shape for %s: %s\n",
759 devname
, strerror(errno
));
764 /* suspend the relevant region */
765 sysfs_set_num(sra
, NULL
, "suspend_hi", 0); /* just in case */
766 if (sysfs_set_num(sra
, NULL
, "suspend_lo", 0) < 0 ||
767 sysfs_set_num(sra
, NULL
, "suspend_hi", last_block
) < 0) {
768 fprintf(stderr
, Name
": %s: failed to suspend device.\n",
774 err
= save_stripes(fdlist
, offsets
,
775 odisks
, ochunk
, olevel
, olayout
,
776 spares
, fdlist
+odisks
,
777 0ULL, last_block
*512);
779 /* abort if there was an error */
781 fprintf(stderr
, Name
": %s: failed to save critical region\n",
786 for (i
=odisks
; i
<d
; i
++) {
787 bsb
.devstart
= __cpu_to_le64(offsets
[i
]);
788 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
789 if (lseek64(fdlist
[i
], (offsets
[i
]+last_block
)<<9, 0) < 0 ||
790 write(fdlist
[i
], &bsb
, sizeof(bsb
)) != sizeof(bsb
) ||
791 fsync(fdlist
[i
]) != 0) {
792 fprintf(stderr
, Name
": %s: failed to save metadata for critical region backups.\n",
798 /* start the reshape happening */
799 if (sysfs_set_str(sra
, NULL
, "sync_action", "reshape") < 0) {
800 fprintf(stderr
, Name
": %s: failed to initiate reshape\n",
804 /* wait for reshape to pass the critical region */
806 unsigned long long comp
;
808 if (sysfs_get_ll(sra
, NULL
, "sync_completed", &comp
)<0) {
815 /* Maybe it finished already */
817 if (sysfs_get_str(sra
, NULL
, "sync_action",
819 strncmp(action
, "reshape", 7) != 0)
825 /* invalidate superblocks */
826 memset(&bsb
, 0, sizeof(bsb
));
827 for (i
=odisks
; i
<d
; i
++) {
828 lseek64(fdlist
[i
], (offsets
[i
]+last_block
)<<9, 0);
829 if (write(fdlist
[i
], &bsb
, sizeof(bsb
)) < 0) {
830 fprintf(stderr
, Name
": %s: failed to invalidate metadata for raid disk %d\n",
836 sysfs_set_num(sra
, NULL
, "suspend_lo", last_block
);
846 fprintf(stderr
, Name
": ... critical section passed.\n");
853 sysfs_set_num(sra
, NULL
, "suspend_lo", last_block
);
855 for (i
=0; i
<array
.nr_disks
; i
++)
867 * If any spare contains md_back_data-1 which is recent wrt mtime,
868 * write that data into the array and update the super blocks with
869 * the new reshape_progress
871 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
, int cnt
, char *backup_file
)
875 unsigned long long *offsets
;
876 unsigned long long nstripe
, ostripe
, last_block
;
879 if (info
->delta_disks
< 0)
880 return 1; /* cannot handle a shrink */
881 if (info
->new_level
!= info
->array
.level
||
882 info
->new_layout
!= info
->array
.layout
||
883 info
->new_chunk
!= info
->array
.chunk_size
)
884 return 1; /* Can only handle change in disks */
886 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
888 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
893 /* This was a spare and may have some saved data on it.
894 * Load the superblock, find and load the
895 * backup_super_block.
896 * If either fail, go on to next device.
897 * If the backup contains no new info, just return
898 * else restore data and update all superblocks
900 if (i
== old_disks
-1) {
901 fd
= open(backup_file
, O_RDONLY
);
908 if (st
->ss
->load_super(st
, fd
, NULL
))
911 st
->ss
->getinfo_super(st
, &dinfo
);
912 st
->ss
->free_super(st
);
915 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
917 continue; /* Cannot seek */
919 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
))
920 continue; /* Cannot read */
921 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0)
923 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
)))
924 continue; /* bad checksum */
925 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0)
926 continue; /* Wrong uuid */
928 if (info
->array
.utime
> __le64_to_cpu(bsb
.mtime
) + 3600 ||
929 info
->array
.utime
< __le64_to_cpu(bsb
.mtime
))
930 continue; /* time stamp is too bad */
932 if (__le64_to_cpu(bsb
.arraystart
) != 0)
933 continue; /* Can only handle backup from start of array */
934 if (__le64_to_cpu(bsb
.length
) <
935 info
->reshape_progress
)
936 continue; /* No new data here */
938 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0)
939 continue; /* Cannot seek */
940 /* There should be a duplicate backup superblock 4k before here */
941 if (lseek64(fd
, -4096, 1) < 0 ||
942 read(fd
, buf
, 4096) != 4096 ||
943 memcmp(buf
, &bsb
, sizeof(bsb
)) != 0)
944 continue; /* Cannot find leading superblock */
946 /* Now need the data offsets for all devices. */
947 offsets
= malloc(sizeof(*offsets
)*info
->array
.raid_disks
);
948 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
951 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
952 /* FIXME should be this be an error */
954 st
->ss
->getinfo_super(st
, &dinfo
);
955 st
->ss
->free_super(st
);
956 offsets
[j
] = dinfo
.data_offset
;
958 printf(Name
": restoring critical section\n");
960 if (restore_stripes(fdlist
, offsets
,
961 info
->array
.raid_disks
,
965 fd
, __le64_to_cpu(bsb
.devstart
)*512,
966 0, __le64_to_cpu(bsb
.length
)*512)) {
967 /* didn't succeed, so giveup */
971 /* Ok, so the data is restored. Let's update those superblocks. */
973 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
974 if (fdlist
[j
] < 0) continue;
975 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
977 st
->ss
->getinfo_super(st
, &dinfo
);
978 dinfo
.reshape_progress
= __le64_to_cpu(bsb
.length
);
979 st
->ss
->update_super(st
, &dinfo
,
982 st
->ss
->store_super(st
, fdlist
[j
]);
983 st
->ss
->free_super(st
);
986 /* And we are done! */
989 /* Didn't find any backup data, try to see if any
992 nstripe
= ostripe
= 0;
993 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
994 if (info
->array
.level
== 6) odata
--; /* number of data disks */
995 ndata
= info
->array
.raid_disks
- 1;
996 if (info
->new_level
== 6) ndata
--;
998 while (nstripe
>= ostripe
) {
999 nstripe
+= info
->new_chunk
/ 512;
1000 last_block
= nstripe
* ndata
;
1001 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
1002 (info
->array
.chunk_size
/512);
1005 if (info
->reshape_progress
>= last_block
)
1007 /* needed to recover critical section! */