]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Grow.c
82533dee5fe8d1f39b6c98031a66b0733c14e86e
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.
54 struct supertype
*st
= NULL
;
57 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
58 fprintf(stderr
, Name
": cannot get array info for %s\n", devname
);
62 st
= super_by_version(info
.array
.major_version
, info
.array
.minor_version
);
64 fprintf(stderr
, Name
": cannot handle arrays with superblock version %d\n", info
.array
.major_version
);
68 if (info
.array
.level
!= -1) {
69 fprintf(stderr
, Name
": can only add devices to linear arrays\n");
73 nfd
= open(newdev
, O_RDWR
|O_EXCL
);
75 fprintf(stderr
, Name
": cannot open %s\n", newdev
);
79 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
80 fprintf(stderr
, Name
": %s is not a block device!\n", newdev
);
84 /* now check out all the devices and make sure we can read the superblock */
85 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
90 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
91 fprintf(stderr
, Name
": cannot get device detail for device %d\n",
95 dv
= map_dev(disk
.major
, disk
.minor
, 1);
97 fprintf(stderr
, Name
": cannot find device file for device %d\n",
101 fd2
= dev_open(dv
, O_RDWR
);
103 fprintf(stderr
, Name
": cannot open device file %s\n", dv
);
106 if (super
) free(super
);
108 if (st
->ss
->load_super(st
, fd2
, &super
, NULL
)) {
109 fprintf(stderr
, Name
": cannot find super block on %s\n", dv
);
115 /* Ok, looks good. Lets update the superblock and write it out to
119 info
.disk
.number
= d
;
120 info
.disk
.major
= major(stb
.st_rdev
);
121 info
.disk
.minor
= minor(stb
.st_rdev
);
122 info
.disk
.raid_disk
= d
;
123 info
.disk
.state
= (1 << MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
);
124 st
->ss
->update_super(&info
, super
, "linear-grow-new", newdev
,
127 if (st
->ss
->store_super(st
, nfd
, super
)) {
128 fprintf(stderr
, Name
": Cannot store new superblock on %s\n",
135 if (ioctl(fd
, ADD_NEW_DISK
, &info
.disk
) != 0) {
136 fprintf(stderr
, Name
": Cannot add new disk to this array\n");
139 /* Well, that seems to have worked.
140 * Now go through and update all superblocks
143 if (ioctl(fd
, GET_ARRAY_INFO
, &info
.array
) < 0) {
144 fprintf(stderr
, Name
": cannot get array info for %s\n", devname
);
149 for (d
=0 ; d
< info
.array
.raid_disks
; d
++) {
150 mdu_disk_info_t disk
;
154 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0) {
155 fprintf(stderr
, Name
": cannot get device detail for device %d\n",
159 dv
= map_dev(disk
.major
, disk
.minor
, 1);
161 fprintf(stderr
, Name
": cannot find device file for device %d\n",
165 fd2
= dev_open(dv
, O_RDWR
);
167 fprintf(stderr
, Name
": cannot open device file %s\n", dv
);
170 if (st
->ss
->load_super(st
, fd2
, &super
, NULL
)) {
171 fprintf(stderr
, Name
": cannot find super block on %s\n", dv
);
175 info
.array
.raid_disks
= nd
+1;
176 info
.array
.nr_disks
= nd
+1;
177 info
.array
.active_disks
= nd
+1;
178 info
.array
.working_disks
= nd
+1;
180 st
->ss
->update_super(&info
, super
, "linear-grow-update", dv
,
183 if (st
->ss
->store_super(st
, fd2
, super
)) {
184 fprintf(stderr
, Name
": Cannot store new superblock on %s\n", dv
);
194 int Grow_addbitmap(char *devname
, int fd
, char *file
, int chunk
, int delay
, int write_behind
, int force
)
197 * First check that array doesn't have a bitmap
198 * Then create the bitmap
201 * For internal bitmaps, we need to check the version,
202 * find all the active devices, and write the bitmap block
205 mdu_bitmap_file_t bmf
;
206 mdu_array_info_t array
;
207 struct supertype
*st
;
208 int major
= BITMAP_MAJOR_HI
;
209 int vers
= md_get_version(fd
);
210 unsigned long long bitmapsize
, array_size
;
213 major
= BITMAP_MAJOR_HOSTENDIAN
;
215 fprintf(stderr
, Name
": Warning - bitmaps created on this kernel are not portable\n"
216 " between different architectured. Consider upgrading the Linux kernel.\n");
220 if (ioctl(fd
, GET_BITMAP_FILE
, &bmf
) != 0) {
222 fprintf(stderr
, Name
": Memory allocation failure.\n");
224 fprintf(stderr
, Name
": bitmaps not supported by this kernel.\n");
227 if (bmf
.pathname
[0]) {
228 if (strcmp(file
,"none")==0) {
229 if (ioctl(fd
, SET_BITMAP_FILE
, -1)!= 0) {
230 fprintf(stderr
, Name
": failed to remove bitmap %s\n",
236 fprintf(stderr
, Name
": %s already has a bitmap (%s)\n",
237 devname
, bmf
.pathname
);
240 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0) {
241 fprintf(stderr
, Name
": cannot get array status for %s\n", devname
);
244 if (array
.state
& (1<<MD_SB_BITMAP_PRESENT
)) {
245 if (strcmp(file
, "none")==0) {
246 array
.state
&= ~(1<<MD_SB_BITMAP_PRESENT
);
247 if (ioctl(fd
, SET_ARRAY_INFO
, &array
)!= 0) {
248 fprintf(stderr
, Name
": failed to remove internal bitmap.\n");
253 fprintf(stderr
, Name
": Internal bitmap already present on %s\n",
257 if (array
.level
<= 0) {
258 fprintf(stderr
, Name
": Bitmaps not meaningful with level %s\n",
259 map_num(pers
, array
.level
)?:"of this array");
262 bitmapsize
= array
.size
;
264 if (get_dev_size(fd
, NULL
, &array_size
) &&
265 array_size
> (0x7fffffffULL
<<9)) {
266 /* Array is big enough that we cannot trust array.size
267 * try other approaches
269 bitmapsize
= get_component_size(fd
);
271 if (bitmapsize
== 0) {
272 fprintf(stderr
, Name
": Cannot reliably determine size of array to create bitmap - sorry.\n");
276 if (array
.level
== 10) {
277 int ncopies
= (array
.layout
&255)*((array
.layout
>>8)&255);
278 bitmapsize
= bitmapsize
* array
.raid_disks
/ ncopies
;
281 st
= super_by_version(array
.major_version
, array
.minor_version
);
283 fprintf(stderr
, Name
": Cannot understand version %d.%d\n",
284 array
.major_version
, array
.minor_version
);
287 if (strcmp(file
, "none") == 0) {
288 fprintf(stderr
, Name
": no bitmap found on %s\n", devname
);
290 } else if (strcmp(file
, "internal") == 0) {
292 for (d
=0; d
< st
->max_devs
; d
++) {
293 mdu_disk_info_t disk
;
296 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
298 if (disk
.major
== 0 &&
301 if ((disk
.state
& (1<<MD_DISK_SYNC
))==0)
303 dv
= map_dev(disk
.major
, disk
.minor
, 1);
306 int fd2
= dev_open(dv
, O_RDWR
);
309 if (st
->ss
->load_super(st
, fd2
, &super
, NULL
)==0) {
310 if (st
->ss
->add_internal_bitmap(
312 &chunk
, delay
, write_behind
,
313 bitmapsize
, 0, major
)
315 st
->ss
->write_bitmap(st
, fd2
, super
);
317 fprintf(stderr
, Name
": failed to create internal bitmap - chunksize problem.\n");
325 array
.state
|= (1<<MD_SB_BITMAP_PRESENT
);
326 if (ioctl(fd
, SET_ARRAY_INFO
, &array
)!= 0) {
327 fprintf(stderr
, Name
": failed to set internal bitmap.\n");
334 int max_devs
= st
->max_devs
;
337 /* try to load a superblock */
338 for (d
=0; d
<max_devs
; d
++) {
339 mdu_disk_info_t disk
;
343 if (ioctl(fd
, GET_DISK_INFO
, &disk
) < 0)
345 if ((disk
.major
==0 && disk
.minor
==0) ||
346 (disk
.state
& (1<<MD_DISK_REMOVED
)))
348 dv
= map_dev(disk
.major
, disk
.minor
, 1);
350 fd2
= dev_open(dv
, O_RDONLY
);
352 st
->ss
->load_super(st
, fd2
, &super
, NULL
) == 0) {
354 st
->ss
->uuid_from_super(uuid
, super
);
360 fprintf(stderr
, Name
": cannot find UUID for array!\n");
363 if (CreateBitmap(file
, force
, (char*)uuid
, chunk
,
364 delay
, write_behind
, bitmapsize
, major
)) {
367 bitmap_fd
= open(file
, O_RDWR
);
369 fprintf(stderr
, Name
": weird: %s cannot be opened\n",
373 if (ioctl(fd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
374 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
375 devname
, strerror(errno
));
385 * When reshaping an array we might need to backup some data.
386 * This is written to all spares with a 'super_block' describing it.
387 * The superblock goes 1K form the end of the used space on the
389 * It if written after the backup is complete.
390 * It has the following structure.
393 struct mdp_backup_super
{
394 char magic
[16]; /* md_backup_data-1 */
397 /* start/sizes in 512byte sectors */
401 __u32 sb_csum
; /* csum of preceeding bytes. */
404 int bsb_csum(char *buf
, int len
)
408 for (i
=0; i
<len
; i
++)
409 csum
= (csum
<<3) + buf
[0];
410 return __cpu_to_le32(csum
);
413 int Grow_reshape(char *devname
, int fd
, int quiet
, char *backup_file
,
415 int level
, int layout
, int chunksize
, int raid_disks
)
417 /* Make some changes in the shape of an array.
418 * The kernel must support the change.
419 * Different reshapes have subtly different meaning for different
420 * levels, so we need to check the current state of the array
423 struct mdu_array_info_s array
;
426 struct mdp_backup_super bsb
;
427 struct supertype
*st
;
431 int nlayout
, olayout
;
434 unsigned long long nstripe
, ostripe
, last_block
;
436 unsigned long long *offsets
;
442 struct sysarray
*sra
;
445 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) < 0) {
446 fprintf(stderr
, Name
": %s is not an active md array - aborting\n",
450 c
= map_num(pers
, array
.level
);
451 if (c
== NULL
) c
= "-unknown-";
452 switch(array
.level
) {
453 default: /* raid0, linear, multipath cannot be reconfigured */
454 fprintf(stderr
, Name
": %s array %s cannot be reshaped.\n",
458 case LEVEL_FAULTY
: /* only 'layout' change is permitted */
461 fprintf(stderr
, Name
": %s: Cannot change size of a 'faulty' array\n",
465 if (level
!= UnSet
&& level
!= LEVEL_FAULTY
) {
466 fprintf(stderr
, Name
": %s: Cannot change RAID level of a 'faulty' array\n",
470 if (chunksize
|| raid_disks
) {
471 fprintf(stderr
, Name
": %s: Cannot change chunksize or disks of a 'faulty' array\n",
476 return 0; /* nothing to do.... */
478 array
.layout
= layout
;
479 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
480 fprintf(stderr
, Name
": Cannot set layout for %s: %s\n",
481 devname
, strerror(errno
));
485 printf("layout for %s set to %d\n", devname
, array
.layout
);
488 case 1: /* raid_disks and size can each be changed. They are independant */
490 if (level
!= UnSet
&& level
!= 1) {
491 fprintf(stderr
, Name
": %s: Cannot change RAID level of a RAID1 array.\n",
495 if (chunksize
|| layout
!= UnSet
) {
496 fprintf(stderr
, Name
": %s: Cannot change chunk size of layout for a RAID1 array.\n",
501 /* Each can trigger a resync/recovery which will block the
502 * other from happening. Later we could block
503 * resync for the duration via 'sync_action'...
505 if (raid_disks
> 0) {
506 array
.raid_disks
= raid_disks
;
507 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
508 fprintf(stderr
, Name
": Cannot set raid-devices for %s: %s\n",
509 devname
, strerror(errno
));
515 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
516 fprintf(stderr
, Name
": Cannot set device size for %s: %s\n",
517 devname
, strerror(errno
));
526 st
= super_by_version(array
.major_version
,
527 array
.minor_version
);
528 /* size can be changed independently.
529 * layout/chunksize/raid_disks/level can be changed
530 * though the kernel may not support it all.
531 * If 'suspend_lo' is not present in devfs, then
532 * these cannot be changed.
535 /* Cannot change other details as well.. */
536 if (layout
!= UnSet
||
540 fprintf(stderr
, Name
": %s: Cannot change shape as well as size of a %s array.\n",
545 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
546 fprintf(stderr
, Name
": Cannot set device size/shape for %s: %s\n",
547 devname
, strerror(errno
));
552 /* Ok, just change the shape. This can be awkward.
553 * There are three possibilities.
554 * 1/ The array will shrink. We don't support this
555 * possibility. Maybe one day...
556 * 2/ The array will not change size. This is easy enough
557 * to do, but not reliably. If the process is aborted
558 * the array *will* be corrupted. So maybe we can allow
559 * this but only if the user is really certain. e.g.
560 * --really-risk-everything
561 * 3/ The array will grow. This can be reliably achieved.
562 * However the kernel's restripe routines will cheerfully
563 * overwrite some early data before it is safe. So we
564 * need to make a backup of the early parts of the array
565 * and be ready to restore it if rebuild aborts very early.
567 * We backup data by writing it to all spares (there must be
568 * at least 1, so even raid6->raid5 requires a spare to be
571 * So: we enumerate the devices in the array and
572 * make sure we can open all of them.
573 * Then we freeze the early part of the array and
574 * backup to the various spares.
575 * Then we request changes and start the reshape.
576 * Monitor progress until it has passed the danger zone.
577 * and finally invalidate the copied data and unfreeze the
578 * start of the array.
580 * Before we can do this we need to decide:
581 * - will the array grow? Just calculate size
582 * - how much needs to be saved: count stripes.
583 * - where to save data... good question.
586 nlevel
= olevel
= array
.level
;
587 nchunk
= ochunk
= array
.chunk_size
;
588 nlayout
= olayout
= array
.layout
;
589 ndisks
= odisks
= array
.raid_disks
;
591 if (level
!= UnSet
) nlevel
= level
;
592 if (chunksize
) nchunk
= chunksize
;
593 if (layout
!= UnSet
) nlayout
= layout
;
594 if (raid_disks
) ndisks
= raid_disks
;
597 if (olevel
== 6) odata
--; /* number of data disks */
599 if (nlevel
== 6) ndata
--;
602 fprintf(stderr
, Name
": %s: Cannot reduce number of data disks (yet).\n",
606 if (ndata
== odata
) {
607 fprintf(stderr
, Name
": %s: Cannot reshape array without increasing size (yet).\n",
611 /* Well, it is growing... so how much do we need to backup.
612 * Need to backup a full number of new-stripes, such that the
613 * last one does not over-write any place that it would be read
616 nstripe
= ostripe
= 0;
617 while (nstripe
>= ostripe
) {
618 nstripe
+= nchunk
/512;
619 last_block
= nstripe
* ndata
;
620 ostripe
= last_block
/ odata
/ (ochunk
/512) * (ochunk
/512);
622 printf("mdadm: Need to backup %lluK of critical section..\n", last_block
/2);
624 sra
= sysfs_read(fd
, 0,
625 GET_COMPONENT
|GET_DEVS
|GET_OFFSET
|GET_STATE
|
628 fprintf(stderr
, Name
": %s: Cannot get array details from sysfs\n",
633 if (last_block
>= sra
->component_size
/2) {
634 fprintf(stderr
, Name
": %s: Something wrong - reshape aborted\n",
638 if (sra
->spares
== 0 && backup_file
== NULL
) {
639 fprintf(stderr
, Name
": %s: Cannot grow - need a spare or backup-file to backup critical section\n",
644 nrdisks
= array
.nr_disks
+ sra
->spares
;
645 /* Now we need to open all these devices so we can read/write.
647 fdlist
= malloc((1+nrdisks
) * sizeof(int));
648 offsets
= malloc((1+nrdisks
) * sizeof(offsets
[0]));
649 if (!fdlist
|| !offsets
) {
650 fprintf(stderr
, Name
": malloc failed: grow aborted\n");
653 for (d
=0; d
<= nrdisks
; d
++)
655 d
= array
.raid_disks
;
656 for (sd
= sra
->devs
; sd
; sd
=sd
->next
) {
657 if (sd
->state
& (1<<MD_DISK_FAULTY
))
659 if (sd
->state
& (1<<MD_DISK_SYNC
)) {
660 char *dn
= map_dev(sd
->major
, sd
->minor
, 1);
661 fdlist
[sd
->role
] = dev_open(dn
, O_RDONLY
);
662 offsets
[sd
->role
] = sd
->offset
;
663 if (fdlist
[sd
->role
] < 0) {
664 fprintf(stderr
, Name
": %s: cannot open component %s\n",
665 devname
, dn
?dn
:"-unknown-");
670 char *dn
= map_dev(sd
->major
, sd
->minor
, 1);
671 fdlist
[d
] = dev_open(dn
, O_RDWR
);
672 offsets
[d
] = sd
->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
->spares
;
689 fdlist
[d
] = open(backup_file
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
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], &super
, 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((int*)&bsb
.set_uuid
, super
);
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 if (i
==d
-1 && backup_file
) {
725 /* This is the backup file */
728 offsets
[i
] += sra
->component_size
- last_block
- 8;
729 if (lseek64(fdlist
[i
], (offsets
[i
]<<9) - 4096, 0)
730 != (offsets
[i
]<<9) - 4096) {
731 fprintf(stderr
, Name
": could not seek...\n");
734 memset(buf
, 0, sizeof(buf
));
735 bsb
.devstart
= __cpu_to_le64(offsets
[i
]);
736 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
737 memcpy(buf
, &bsb
, sizeof(bsb
));
738 if (write(fdlist
[i
], buf
, 4096) != 4096) {
739 fprintf(stderr
, Name
": could not write leading superblock\n");
743 array
.level
= nlevel
;
744 array
.raid_disks
= ndisks
;
745 array
.chunk_size
= nchunk
;
746 array
.layout
= nlayout
;
747 if (ioctl(fd
, SET_ARRAY_INFO
, &array
) != 0) {
748 if (errno
== ENOSPC
) {
749 /* stripe cache is not big enough.
750 * It needs to be 4 times chunksize_size,
751 * and we assume pagesize is 4K
753 if (sra
->cache_size
< 4 * (nchunk
/4096)) {
754 sysfs_set_num(sra
, NULL
,
756 4 * (nchunk
/4096) +1);
757 if (ioctl(fd
, SET_ARRAY_INFO
,
762 fprintf(stderr
, Name
": Cannot set device size/shape for %s: %s\n",
763 devname
, strerror(errno
));
768 /* suspend the relevant region */
769 sysfs_set_num(sra
, NULL
, "suspend_hi", 0); /* just in case */
770 if (sysfs_set_num(sra
, NULL
, "suspend_lo", 0) < 0 ||
771 sysfs_set_num(sra
, NULL
, "suspend_hi", last_block
) < 0) {
772 fprintf(stderr
, Name
": %s: failed to suspend device.\n",
778 err
= save_stripes(fdlist
, offsets
,
779 odisks
, ochunk
, olevel
, olayout
,
780 spares
, fdlist
+odisks
,
781 0ULL, last_block
*512);
783 /* abort if there was an error */
785 fprintf(stderr
, Name
": %s: failed to save critical region\n",
790 for (i
=odisks
; i
<d
; i
++) {
791 bsb
.devstart
= __cpu_to_le64(offsets
[i
]);
792 bsb
.sb_csum
= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
));
793 if (lseek64(fdlist
[i
], (offsets
[i
]+last_block
)<<9, 0) < 0 ||
794 write(fdlist
[i
], &bsb
, sizeof(bsb
)) != sizeof(bsb
) ||
795 fsync(fdlist
[i
]) != 0) {
796 fprintf(stderr
, Name
": %s: fail to save metadata for critical region backups.\n",
802 /* start the reshape happening */
803 if (sysfs_set_str(sra
, NULL
, "sync_action", "reshape") < 0) {
804 fprintf(stderr
, Name
": %s: failed to initiate reshape\n",
808 /* wait for reshape to pass the critical region */
810 unsigned long long comp
;
811 if (sysfs_get_ll(sra
, NULL
, "sync_completed", &comp
)<0) {
820 /* invalidate superblocks */
821 memset(&bsb
, 0, sizeof(bsb
));
822 for (i
=odisks
; i
<d
; i
++) {
823 lseek64(fdlist
[i
], (offsets
[i
]+last_block
)<<9, 0);
824 if (write(fdlist
[i
], &bsb
, sizeof(bsb
)) < 0) {
825 fprintf(stderr
, Name
": %s: failed to invalidate metadata for raid disk %d\n",
831 sysfs_set_num(sra
, NULL
, "suspend_lo", last_block
);
841 printf(Name
": ... critical section passed.\n");
848 sysfs_set_num(sra
, NULL
, "suspend_lo", last_block
);
850 for (i
=0; i
<array
.nr_disks
; i
++)
862 * If any spare contains md_back_data-1 which is recent wrt mtime,
863 * write that data into the array and update the super blocks with
864 * the new reshape_progress
866 int Grow_restart(struct supertype
*st
, struct mdinfo
*info
, int *fdlist
, int cnt
, char *backup_file
)
870 unsigned long long *offsets
;
871 unsigned long long nstripe
, ostripe
, last_block
;
874 if (info
->delta_disks
< 0)
875 return 1; /* cannot handle a shrink */
876 if (info
->new_level
!= info
->array
.level
||
877 info
->new_layout
!= info
->array
.layout
||
878 info
->new_chunk
!= info
->array
.chunk_size
)
879 return 1; /* Can only handle change in disks */
881 old_disks
= info
->array
.raid_disks
- info
->delta_disks
;
883 for (i
=old_disks
-(backup_file
?1:0); i
<cnt
; i
++) {
886 struct mdp_backup_super bsb
;
890 /* This was a spare and may have some saved data on it.
891 * Load the superblock, find and load the
892 * backup_super_block.
893 * If either fail, go on to next device.
894 * If the backup contains no new info, just return
895 * else restore data and update all superblocks
897 if (i
== old_disks
-1) {
898 fd
= open(backup_file
, O_RDONLY
);
905 if (st
->ss
->load_super(st
, fd
, &super
, NULL
))
908 st
->ss
->getinfo_super(&dinfo
, super
);
909 free(super
); super
= NULL
;
911 (dinfo
.data_offset
+ dinfo
.component_size
- 8) <<9,
913 continue; /* Cannot seek */
915 if (read(fd
, &bsb
, sizeof(bsb
)) != sizeof(bsb
))
916 continue; /* Cannot read */
917 if (memcmp(bsb
.magic
, "md_backup_data-1", 16) != 0)
919 if (bsb
.sb_csum
!= bsb_csum((char*)&bsb
, ((char*)&bsb
.sb_csum
)-((char*)&bsb
)))
920 continue; /* bad checksum */
921 if (memcmp(bsb
.set_uuid
,info
->uuid
, 16) != 0)
922 continue; /* Wrong uuid */
924 if (info
->array
.utime
> __le64_to_cpu(bsb
.mtime
) + 3600 ||
925 info
->array
.utime
< __le64_to_cpu(bsb
.mtime
))
926 continue; /* time stamp is too bad */
928 if (__le64_to_cpu(bsb
.arraystart
) != 0)
929 continue; /* Can only handle backup from start of array */
930 if (__le64_to_cpu(bsb
.length
) <
931 info
->reshape_progress
)
932 continue; /* No new data here */
934 if (lseek64(fd
, __le64_to_cpu(bsb
.devstart
)*512, 0)< 0)
935 continue; /* Cannot seek */
936 /* There should be a duplicate backup superblock 4k before here */
937 if (lseek64(fd
, -4096, 1) < 0 ||
938 read(fd
, buf
, 4096) != 4096 ||
939 memcmp(buf
, &bsb
, sizeof(bsb
)) != 0)
940 continue; /* Cannot find leading superblock */
942 /* Now need the data offsets for all devices. */
943 offsets
= malloc(sizeof(*offsets
)*info
->array
.raid_disks
);
944 for(j
=0; j
<info
->array
.raid_disks
; j
++) {
947 if (st
->ss
->load_super(st
, fdlist
[j
], &super
, NULL
))
948 /* FIXME should be this be an error */
950 st
->ss
->getinfo_super(&dinfo
, super
);
951 free(super
); super
= NULL
;
952 offsets
[j
] = dinfo
.data_offset
;
954 printf(Name
": restoring critical section\n");
956 if (restore_stripes(fdlist
, offsets
,
957 info
->array
.raid_disks
,
961 fd
, __le64_to_cpu(bsb
.devstart
)*512,
962 0, __le64_to_cpu(bsb
.length
)*512)) {
963 /* didn't succeed, so giveup */
967 /* Ok, so the data is restored. Let's update those superblocks. */
969 for (j
=0; j
<info
->array
.raid_disks
; j
++) {
970 if (fdlist
[j
] < 0) continue;
971 if (st
->ss
->load_super(st
, fdlist
[j
], &super
, NULL
))
973 st
->ss
->getinfo_super(&dinfo
, super
);
974 dinfo
.reshape_progress
= __le64_to_cpu(bsb
.length
);
975 st
->ss
->update_super(&dinfo
, super
, "_reshape_progress",NULL
,0, 0, NULL
);
976 st
->ss
->store_super(st
, fdlist
[j
], super
);
980 /* And we are done! */
983 /* Didn't find any backup data, try to see if any
986 nstripe
= ostripe
= 0;
987 odata
= info
->array
.raid_disks
- info
->delta_disks
- 1;
988 if (info
->array
.level
== 6) odata
--; /* number of data disks */
989 ndata
= info
->array
.raid_disks
- 1;
990 if (info
->new_level
== 6) ndata
--;
992 while (nstripe
>= ostripe
) {
993 nstripe
+= info
->new_chunk
/ 512;
994 last_block
= nstripe
* ndata
;
995 ostripe
= last_block
/ odata
/ (info
->array
.chunk_size
/512) *
996 (info
->array
.chunk_size
/512);
999 if (info
->reshape_progress
>= last_block
)
1001 /* needed to recover critical section! */