]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Grow.c
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2006 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@cse.unsw.edu.au>
24 * School of Computer Science and Engineering
25 * The University of New South Wales
32 #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
33 #error no endian defined
38 int Grow_Add_device(char *devname
, int fd
, char *newdev
)
40 /* Add a device to an active array.
41 * Currently, just extend a linear array.
42 * This requires writing a new superblock on the
43 * new device, calling the kernel to add the device,
44 * and if that succeeds, update the superblock on
46 * This means that we need to *find* all other devices.
53 struct supertype
*st
= NULL
;
56 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
57 fprintf(stderr
, Name
": cannot get array info for %s\n", devname
);
63 fprintf(stderr
, Name
": cannot handle arrays with superblock version %d\n", info
.array
.major_version
);
67 if (info
.array
.level
!= -1) {
68 fprintf(stderr
, Name
": can only add devices to linear arrays\n");
72 nfd
= open(newdev
, O_RDWR
|O_EXCL
|O_DIRECT
);
74 fprintf(stderr
, Name
": cannot open %s\n", newdev
);
78 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
79 fprintf(stderr
, Name
": %s is not a block device!\n", newdev
);
83 /* now check out all the devices and make sure we can read the superblock */
84 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
89 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
90 fprintf(stderr
, Name
": cannot get device detail for device %d\n",
94 dv
= map_dev(disk
.major
, disk
.minor
, 1);
96 fprintf(stderr
, Name
": cannot find device file for device %d\n",
100 fd2
= dev_open(dv
, O_RDWR
);
102 fprintf(stderr
, Name
": cannot open device file %s\n", dv
);
105 st
->ss
->free_super(st
);
107 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
108 fprintf(stderr
, Name
": cannot find super block on %s\n", dv
);
114 /* Ok, looks good. Lets update the superblock and write it out to
118 info
.disk
.number
= d
;
119 info
.disk
.major
= major(stb
.st_rdev
);
120 info
.disk
.minor
= minor(stb
.st_rdev
);
121 info
.disk
.raid_disk
= d
;
122 info
.disk
.state
= (1 << MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
);
123 st
->ss
->update_super(st
, &info
, "linear-grow-new", newdev
,
126 if (st
->ss
->store_super(st
, nfd
)) {
127 fprintf(stderr
, Name
": Cannot store new superblock on %s\n",
134 if (ioctl(fd
, ADD_NEW_DISK
, &info
.disk
) != 0) {
135 fprintf(stderr
, Name
": Cannot add new disk to this array\n");
138 /* Well, that seems to have worked.
139 * Now go through and update all superblocks
142 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
143 fprintf(stderr
, Name
": cannot get array info for %s\n", devname
);
148 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
149 mdu_disk_info_t disk
;
153 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
154 fprintf(stderr
, Name
": cannot get device detail for device %d\n",
158 dv
= map_dev(disk
.major
, disk
.minor
, 1);
160 fprintf(stderr
, Name
": cannot find device file for device %d\n",
164 fd2
= dev_open(dv
, O_RDWR
);
166 fprintf(stderr
, Name
": cannot open device file %s\n", dv
);
169 if (st
->ss
->load_super(st
, fd2
, NULL
)) {
170 fprintf(stderr
, Name
": cannot find super block on %s\n", dv
);
174 info
.array
.raid_disks
= nd
+1;
175 info
.array
.nr_disks
= nd
+1;
176 info
.array
.active_disks
= nd
+1;
177 info
.array
.working_disks
= nd
+1;
179 st
->ss
->update_super(st
, &info
, "linear-grow-update", dv
,
182 if (st
->ss
->store_super(st
, fd2
)) {
183 fprintf(stderr
, Name
": Cannot store new superblock on %s\n", dv
);
193 int Grow_addbitmap(char *devname
, int fd
, char *file
, int chunk
, int delay
, int write_behind
, int force
)
196 * First check that array doesn't have a bitmap
197 * Then create the bitmap
200 * For internal bitmaps, we need to check the version,
201 * find all the active devices, and write the bitmap block
204 mdu_bitmap_file_t bmf
;
205 mdu_array_info_t array
;
206 struct supertype
*st
;
207 int major
= BITMAP_MAJOR_HI
;
208 int vers
= md_get_version(fd
);
209 unsigned long long bitmapsize
, array_size
;
212 major
= BITMAP_MAJOR_HOSTENDIAN
;
214 fprintf(stderr
, Name
": Warning - bitmaps created on this kernel are not portable\n"
215 " between different architectured. Consider upgrading the Linux kernel.\n");
219 if (ioctl(fd
, GET_BITMAP_FILE
, &bmf
) != 0) {
221 fprintf(stderr
, Name
": Memory allocation failure.\n");
223 fprintf(stderr
, Name
": bitmaps not supported by this kernel.\n");
226 if (bmf
.pathname
[0]) {
227 if (strcmp(file
,"none")==0) {
228 if (ioctl(fd
, SET_BITMAP_FILE
, -1)!= 0) {
229 fprintf(stderr
, Name
": failed to remove bitmap %s\n",
235 fprintf(stderr
, Name
": %s already has a bitmap (%s)\n",
236 devname
, bmf
.pathname
);
239 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
240 fprintf(stderr
, Name
": cannot get array status for %s\n", devname
);
243 if (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)) {
244 if (strcmp(file
, "none")==0) {
245 array
.state
&= ~(1<<MD_SB_BITMAP_PRESENT
);
246 if (ioctl(fd
, SET_ARRAY_INFO
, &array
)!= 0) {
247 fprintf(stderr
, Name
": failed to remove internal bitmap.\n");
252 fprintf(stderr
, Name
": Internal bitmap already present on %s\n",
256 if (array
.level
<= 0) {
257 fprintf(stderr
, Name
": Bitmaps not meaningful with level %s\n",
258 map_num(pers
, array
.level
)?:"of this array");
261 bitmapsize
= array
.size
;
263 if (get_dev_size(fd
, NULL
, &array_size
) &&
264 array_size
> (0x7fffffffULL
<<9)) {
265 /* Array is big enough that we cannot trust array.size
266 * try other approaches
268 bitmapsize
= get_component_size(fd
);
270 if (bitmapsize
== 0) {
271 fprintf(stderr
, Name
": Cannot reliably determine size of array to create bitmap - sorry.\n");
275 if (array
.level
== 10) {
276 int ncopies
= (array
.layout
&255)*((array
.layout
>>8)&255);
277 bitmapsize
= bitmapsize
* array
.raid_disks
/ ncopies
;
280 st
= super_by_fd(fd
);
282 fprintf(stderr
, Name
": Cannot understand version %d.%d\n",
283 array
.major_version
, array
.minor_version
);
286 if (strcmp(file
, "none") == 0) {
287 fprintf(stderr
, Name
": no bitmap found on %s\n", devname
);
289 } else if (strcmp(file
, "internal") == 0) {
291 for (d
=0; d
< st
->max_devs
; d
++) {
292 mdu_disk_info_t disk
;
295 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
297 if (disk
.major
== 0 &&
300 if ((disk
.state
& (1<<MD_DISK_SYNC
))==0)
302 dv
= map_dev(disk
.major
, disk
.minor
, 1);
304 int fd2
= dev_open(dv
, O_RDWR
);
307 if (st
->ss
->load_super(st
, fd2
, NULL
)==0) {
308 if (st
->ss
->add_internal_bitmap(
310 &chunk
, delay
, write_behind
,
311 bitmapsize
, 0, major
)
313 st
->ss
->write_bitmap(st
, fd2
);
315 fprintf(stderr
, Name
": failed to create internal bitmap - chunksize problem.\n");
323 array
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
324 if (ioctl(fd
, SET_ARRAY_INFO
, &array
)!= 0) {
325 fprintf(stderr
, Name
": failed to set internal bitmap.\n");
332 int max_devs
= st
->max_devs
;
334 /* try to load a superblock */
335 for (d
=0; d
<max_devs
; d
++) {
336 mdu_disk_info_t disk
;
340 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
342 if ((disk
.major
==0 && disk
.minor
==0) ||
343 (disk
.state
& (1<<MD_DISK_REMOVED
)))
345 dv
= map_dev(disk
.major
, disk
.minor
, 1);
347 fd2
= dev_open(dv
, O_RDONLY
);
349 st
->ss
->load_super(st
, fd2
, NULL
) == 0) {
351 st
->ss
->uuid_from_super(st
, uuid
);
357 fprintf(stderr
, Name
": cannot find UUID for array!\n");
360 if (CreateBitmap(file
, force
, (char*)uuid
, chunk
,
361 delay
, write_behind
, bitmapsize
, major
)) {
364 bitmap_fd
= open(file
, O_RDWR
);
366 fprintf(stderr
, Name
": weird: %s cannot be opened\n",
370 if (ioctl(fd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
371 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
372 devname
, strerror(errno
));
382 * When reshaping an array we might need to backup some data.
383 * This is written to all spares with a 'super_block' describing it.
384 * The superblock goes 1K form the end of the used space on the
386 * It if written after the backup is complete.
387 * It has the following structure.
390 struct mdp_backup_super
{
391 char magic
[16]; /* md_backup_data-1 */
394 /* start/sizes in 512byte sectors */
398 __u32 sb_csum
; /* csum of preceeding bytes. */
400 } __attribute__((aligned(512))) bsb
;
402 int bsb_csum(char *buf
, int len
)
406 for (i
=0; i
<len
; i
++)
407 csum
= (csum
<<3) + buf
[0];
408 return __cpu_to_le32(csum
);
411 int Grow_reshape(char *devname
, int fd
, int quiet
, char *backup_file
,
413 int level
, int layout
, int chunksize
, int raid_disks
)
415 /* Make some changes in the shape of an array.
416 * The kernel must support the change.
417 * Different reshapes have subtly different meaning for different
418 * levels, so we need to check the current state of the array
421 struct mdu_array_info_s array
;
424 struct supertype
*st
;
428 int nlayout
, olayout
;
431 unsigned long long nstripe
, ostripe
, last_block
;
433 unsigned long long *offsets
;
441 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) < 0) {
442 fprintf(stderr
, Name
": %s is not an active md array - aborting\n",
446 c
= map_num(pers
, array
.level
);
447 if (c
== NULL
) c
= "-unknown-";
448 switch(array
.level
) {
449 default: /* raid0, linear, multipath cannot be reconfigured */
450 fprintf(stderr
, Name
": %s array %s cannot be reshaped.\n",
454 case LEVEL_FAULTY
: /* only 'layout' change is permitted */
457 fprintf(stderr
, Name
": %s: Cannot change size of a 'faulty' array\n",
461 if (level
!= UnSet
&& level
!= LEVEL_FAULTY
) {
462 fprintf(stderr
, Name
": %s: Cannot change RAID level of a 'faulty' array\n",
466 if (chunksize
|| raid_disks
) {
467 fprintf(stderr
, Name
": %s: Cannot change chunksize or disks of a 'faulty' array\n",
472 return 0; /* nothing to do.... */
474 array
.layout
= layout
;
475 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
476 fprintf(stderr
, Name
": Cannot set layout for %s: %s\n",
477 devname
, strerror(errno
));
481 printf("layout for %s set to %d\n", devname
, array
.layout
);
484 case 1: /* raid_disks and size can each be changed. They are independant */
486 if (level
!= UnSet
&& level
!= 1) {
487 fprintf(stderr
, Name
": %s: Cannot change RAID level of a RAID1 array.\n",
491 if (chunksize
|| layout
!= UnSet
) {
492 fprintf(stderr
, Name
": %s: Cannot change chunk size of layout for a RAID1 array.\n",
497 /* Each can trigger a resync/recovery which will block the
498 * other from happening. Later we could block
499 * resync for the duration via 'sync_action'...
501 if (raid_disks
> 0) {
502 array
.raid_disks
= raid_disks
;
503 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
504 fprintf(stderr
, Name
": Cannot set raid-devices for %s: %s\n",
505 devname
, strerror(errno
));
511 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
512 fprintf(stderr
, Name
": Cannot set device size for %s: %s\n",
513 devname
, strerror(errno
));
522 st
= super_by_fd(fd
);
524 /* size can be changed independently.
525 * layout/chunksize/raid_disks/level can be changed
526 * though the kernel may not support it all.
527 * If 'suspend_lo' is not present in devfs, then
528 * these cannot be changed.
531 /* Cannot change other details as well.. */
532 if (layout
!= UnSet
||
536 fprintf(stderr
, Name
": %s: Cannot change shape as well as size of a %s array.\n",
541 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
542 fprintf(stderr
, Name
": Cannot set device size/shape for %s: %s\n",
543 devname
, strerror(errno
));
548 /* Ok, just change the shape. This can be awkward.
549 * There are three possibilities.
550 * 1/ The array will shrink. We don't support this
551 * possibility. Maybe one day...
552 * 2/ The array will not change size. This is easy enough
553 * to do, but not reliably. If the process is aborted
554 * the array *will* be corrupted. So maybe we can allow
555 * this but only if the user is really certain. e.g.
556 * --really-risk-everything
557 * 3/ The array will grow. This can be reliably achieved.
558 * However the kernel's restripe routines will cheerfully
559 * overwrite some early data before it is safe. So we
560 * need to make a backup of the early parts of the array
561 * and be ready to restore it if rebuild aborts very early.
563 * We backup data by writing it to all spares (there must be
564 * at least 1, so even raid6->raid5 requires a spare to be
567 * So: we enumerate the devices in the array and
568 * make sure we can open all of them.
569 * Then we freeze the early part of the array and
570 * backup to the various spares.
571 * Then we request changes and start the reshape.
572 * Monitor progress until it has passed the danger zone.
573 * and finally invalidate the copied data and unfreeze the
574 * start of the array.
576 * Before we can do this we need to decide:
577 * - will the array grow? Just calculate size
578 * - how much needs to be saved: count stripes.
579 * - where to save data... good question.
582 nlevel
= olevel
= array
.level
;
583 nchunk
= ochunk
= array
.chunk_size
;
584 nlayout
= olayout
= array
.layout
;
585 ndisks
= odisks
= array
.raid_disks
;
587 if (level
!= UnSet
) nlevel
= level
;
588 if (chunksize
) nchunk
= chunksize
;
589 if (layout
!= UnSet
) nlayout
= layout
;
590 if (raid_disks
) ndisks
= raid_disks
;
593 if (olevel
== 6) odata
--; /* number of data disks */
595 if (nlevel
== 6) ndata
--;
598 fprintf(stderr
, Name
": %s: Cannot reduce number of data disks (yet).\n",
602 if (ndata
== odata
) {
603 fprintf(stderr
, Name
": %s: Cannot reshape array without increasing size (yet).\n",
607 /* Well, it is growing... so how much do we need to backup.
608 * Need to backup a full number of new-stripes, such that the
609 * last one does not over-write any place that it would be read
612 nstripe
= ostripe
= 0;
613 while (nstripe
>= ostripe
) {
614 nstripe
+= nchunk
/512;
615 last_block
= nstripe
* ndata
;
616 ostripe
= last_block
/ odata
/ (ochunk
/512) * (ochunk
/512);
618 fprintf(stderr
, Name
": Need to backup %lluK of critical "
619 "section..\n", last_block
/2);
621 sra
= sysfs_read(fd
, 0,
622 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
625 fprintf(stderr
, Name
": %s: Cannot get array details from sysfs\n",
630 if (last_block
>= sra
->component_size
/2) {
631 fprintf(stderr
, Name
": %s: Something wrong - reshape aborted\n",
635 if (sra
->array
.spare_disks
== 0 && backup_file
== NULL
) {
636 fprintf(stderr
, Name
": %s: Cannot grow - need a spare or backup-file to backup critical section\n",
641 nrdisks
= array
.nr_disks
+ sra
->array
.spare_disks
;
642 /* Now we need to open all these devices so we can read/write.
644 fdlist
= malloc((1+nrdisks
) * sizeof(int));
645 offsets
= malloc((1+nrdisks
) * sizeof(offsets
[0]));
646 if (!fdlist
|| !offsets
) {
647 fprintf(stderr
, Name
": malloc failed: grow aborted\n");
650 for (d
=0; d
<= nrdisks
; d
++)
652 d
= array
.raid_disks
;
653 for (sd
= sra
->devs
; sd
; sd
=sd
->next
) {
654 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
656 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
657 char *dn
= map_dev(sd
->disk
.major
,
659 fdlist
[sd
->disk
.raid_disk
]
660 = dev_open(dn
, O_RDONLY
);
661 offsets
[sd
->disk
.raid_disk
] = sd
->data_offset
;
662 if (fdlist
[sd
->disk
.raid_disk
] < 0) {
663 fprintf(stderr
, Name
": %s: cannot open component %s\n",
664 devname
, dn
?dn
:"-unknown-");
669 char *dn
= map_dev(sd
->disk
.major
,
671 fdlist
[d
] = dev_open(dn
, O_RDWR
);
672 offsets
[d
] = sd
->data_offset
;
674 fprintf(stderr
, Name
": %s: cannot open component %s\n",
675 devname
, dn
?dn
:"-unknown");
681 for (i
=0 ; i
<array
.raid_disks
; i
++)
683 fprintf(stderr
, Name
": %s: failed to find device %d. Array might be degraded.\n"
684 " --grow aborted\n", devname
, i
);
687 spares
= sra
->array
.spare_disks
;
689 fdlist
[d
] = open(backup_file
, O_RDWR
|O_CREAT
|O_EXCL
, S_IRUSR
| S_IWUSR
);
691 fprintf(stderr
, Name
": %s: cannot create backup file %s: %s\n",
692 devname
, backup_file
, strerror(errno
));
699 if (fdlist
[array
.raid_disks
] < 0) {
700 fprintf(stderr
, Name
": %s: failed to find a spare and no backup-file given - --grow aborted\n",
705 /* Find a superblock */
706 if (st
->ss
->load_super(st
, fdlist
[0], NULL
)) {
707 fprintf(stderr
, Name
": %s: Cannot find a superblock\n",
713 memcpy(bsb
.magic
, "md_backup_data-1", 16);
714 st
->ss
->uuid_from_super(st
, (int*)&bsb
.set_uuid
);
715 bsb
.mtime
= __cpu_to_le64(time(0));
717 bsb
.length
= __cpu_to_le64(last_block
);
719 /* Decide offset for the backup, llseek the spares, and write
720 * a leading superblock 4K earlier.
722 for (i
=array
.raid_disks
; i
<d
; i
++) {
724 char *buf
= (char*)(((unsigned long)abuf
+511)& ~511);
725 if (i
==d
-1 && backup_file
) {
726 /* This is the backup file */
729 offsets
[i
] += sra
->component_size
- last_block
- 8;
730 if (lseek64(fdlist
[i
], (offsets
[i
]<<9) - 4096, 0)
731 != (offsets
[i
]<<9) - 4096) {
732 fprintf(stderr
, Name
": could not seek...\n");
735 memset(buf
, 0, 4096);
736 bsb
.devstart
= __cpu_to_le64(offsets
[i
]);
737 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
738 memcpy(buf
, &bsb
, sizeof(bsb
));
739 if (write(fdlist
[i
], buf
, 4096) != 4096) {
740 fprintf(stderr
, Name
": could not write leading superblock\n");
744 array
.level
= nlevel
;
745 array
.raid_disks
= ndisks
;
746 array
.chunk_size
= nchunk
;
747 array
.layout
= nlayout
;
748 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
749 if (errno
== ENOSPC
) {
750 /* stripe cache is not big enough.
751 * It needs to be 4 times chunksize_size,
752 * and we assume pagesize is 4K
754 if (sra
->cache_size
< 4 * (nchunk
/4096)) {
755 sysfs_set_num(sra
, NULL
,
757 4 * (nchunk
/4096) +1);
758 if (ioctl(fd
, SET_ARRAY_INFO
,
763 fprintf(stderr
, Name
": Cannot set device size/shape for %s: %s\n",
764 devname
, strerror(errno
));
769 /* suspend the relevant region */
770 sysfs_set_num(sra
, NULL
, "suspend_hi", 0); /* just in case */
771 if (sysfs_set_num(sra
, NULL
, "suspend_lo", 0) < 0 ||
772 sysfs_set_num(sra
, NULL
, "suspend_hi", last_block
) < 0) {
773 fprintf(stderr
, Name
": %s: failed to suspend device.\n",
779 err
= save_stripes(fdlist
, offsets
,
780 odisks
, ochunk
, olevel
, olayout
,
781 spares
, fdlist
+odisks
,
782 0ULL, last_block
*512);
784 /* abort if there was an error */
786 fprintf(stderr
, Name
": %s: failed to save critical region\n",
791 for (i
=odisks
; i
<d
; i
++) {
792 bsb
.devstart
= __cpu_to_le64(offsets
[i
]);
793 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
794 if (lseek64(fdlist
[i
], (offsets
[i
]+last_block
)<<9, 0) < 0 ||
795 write(fdlist
[i
], &bsb
, sizeof(bsb
)) != sizeof(bsb
) ||
796 fsync(fdlist
[i
]) != 0) {
797 fprintf(stderr
, Name
": %s: failed to save metadata for critical region backups.\n",
803 /* start the reshape happening */
804 if (sysfs_set_str(sra
, NULL
, "sync_action", "reshape") < 0) {
805 fprintf(stderr
, Name
": %s: failed to initiate reshape\n",
809 /* wait for reshape to pass the critical region */
811 unsigned long long comp
;
812 if (sysfs_get_ll(sra
, NULL
, "sync_completed", &comp
)<0) {
819 /* Maybe it finished already */
821 if (sysfs_get_str(sra
, NULL
, "sync_action",
823 strncmp(action
, "reshape", 7) != 0)
829 /* invalidate superblocks */
830 memset(&bsb
, 0, sizeof(bsb
));
831 for (i
=odisks
; i
<d
; i
++) {
832 lseek64(fdlist
[i
], (offsets
[i
]+last_block
)<<9, 0);
833 if (write(fdlist
[i
], &bsb
, sizeof(bsb
)) < 0) {
834 fprintf(stderr
, Name
": %s: failed to invalidate metadata for raid disk %d\n",
840 sysfs_set_num(sra
, NULL
, "suspend_lo", last_block
);
850 fprintf(stderr
, Name
": ... critical section passed.\n");
857 sysfs_set_num(sra
, NULL
, "suspend_lo", last_block
);
859 for (i
=0; i
<array
.nr_disks
; i
++)
871 * If any spare contains md_back_data-1 which is recent wrt mtime,
872 * write that data into the array and update the super blocks with
873 * the new reshape_progress
875 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
, int cnt
, char *backup_file
)
879 unsigned long long *offsets
;
880 unsigned long long nstripe
, ostripe
, last_block
;
883 if (info
->delta_disks
< 0)
884 return 1; /* cannot handle a shrink */
885 if (info
->new_level
!= info
->array
.level
||
886 info
->new_layout
!= info
->array
.layout
||
887 info
->new_chunk
!= info
->array
.chunk_size
)
888 return 1; /* Can only handle change in disks */
890 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
892 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
897 /* This was a spare and may have some saved data on it.
898 * Load the superblock, find and load the
899 * backup_super_block.
900 * If either fail, go on to next device.
901 * If the backup contains no new info, just return
902 * else restore data and update all superblocks
904 if (i
== old_disks
-1) {
905 fd
= open(backup_file
, O_RDONLY
);
912 if (st
->ss
->load_super(st
, fd
, NULL
))
915 st
->ss
->getinfo_super(st
, &dinfo
);
916 st
->ss
->free_super(st
);
919 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
921 continue; /* Cannot seek */
923 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
))
924 continue; /* Cannot read */
925 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0)
927 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
)))
928 continue; /* bad checksum */
929 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0)
930 continue; /* Wrong uuid */
932 if (info
->array
.utime
> __le64_to_cpu(bsb
.mtime
) + 3600 ||
933 info
->array
.utime
< __le64_to_cpu(bsb
.mtime
))
934 continue; /* time stamp is too bad */
936 if (__le64_to_cpu(bsb
.arraystart
) != 0)
937 continue; /* Can only handle backup from start of array */
938 if (__le64_to_cpu(bsb
.length
) <
939 info
->reshape_progress
)
940 continue; /* No new data here */
942 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0)
943 continue; /* Cannot seek */
944 /* There should be a duplicate backup superblock 4k before here */
945 if (lseek64(fd
, -4096, 1) < 0 ||
946 read(fd
, buf
, 4096) != 4096 ||
947 memcmp(buf
, &bsb
, sizeof(bsb
)) != 0)
948 continue; /* Cannot find leading superblock */
950 /* Now need the data offsets for all devices. */
951 offsets
= malloc(sizeof(*offsets
)*info
->array
.raid_disks
);
952 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
955 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
956 /* FIXME should be this be an error */
958 st
->ss
->getinfo_super(st
, &dinfo
);
959 st
->ss
->free_super(st
);
960 offsets
[j
] = dinfo
.data_offset
;
962 printf(Name
": restoring critical section\n");
964 if (restore_stripes(fdlist
, offsets
,
965 info
->array
.raid_disks
,
969 fd
, __le64_to_cpu(bsb
.devstart
)*512,
970 0, __le64_to_cpu(bsb
.length
)*512)) {
971 /* didn't succeed, so giveup */
975 /* Ok, so the data is restored. Let's update those superblocks. */
977 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
978 if (fdlist
[j
] < 0) continue;
979 if (st
->ss
->load_super(st
, fdlist
[j
], NULL
))
981 st
->ss
->getinfo_super(st
, &dinfo
);
982 dinfo
.reshape_progress
= __le64_to_cpu(bsb
.length
);
983 st
->ss
->update_super(st
, &dinfo
,
986 st
->ss
->store_super(st
, fdlist
[j
]);
987 st
->ss
->free_super(st
);
990 /* And we are done! */
993 /* Didn't find any backup data, try to see if any
996 nstripe
= ostripe
= 0;
997 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
998 if (info
->array
.level
== 6) odata
--; /* number of data disks */
999 ndata
= info
->array
.raid_disks
- 1;
1000 if (info
->new_level
== 6) ndata
--;
1002 while (nstripe
>= ostripe
) {
1003 nstripe
+= info
->new_chunk
/ 512;
1004 last_block
= nstripe
* ndata
;
1005 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
1006 (info
->array
.chunk_size
/512);
1009 if (info
->reshape_progress
>= last_block
)
1011 /* needed to recover critical section! */