Give useful message if raid4/5/6 cannot be started because it is not clean and is...
[thirdparty/mdadm.git] / Grow.c
CommitLineData
e5329c37
NB
1/*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4f589ad0 4 * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
e5329c37
NB
5 *
6 *
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.
11 *
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.
16 *
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
20 *
21 * Author: Neil Brown
22 * Email: <neilb@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29#include "mdadm.h"
30#include "dlink.h"
31
32#if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
33#error no endian defined
34#endif
35#include "md_u.h"
36#include "md_p.h"
37
38int Grow_Add_device(char *devname, int fd, char *newdev)
39{
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
45 * all other devices.
46 * This means that we need to *find* all other devices.
47 */
4b1ac34b
NB
48 struct mdinfo info;
49
50 void *super = NULL;
e5329c37
NB
51 struct stat stb;
52 int nfd, fd2;
53 int d, nd;
82d9eba6 54 struct supertype *st = NULL;
e5329c37
NB
55
56
4b1ac34b 57 if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
e5329c37
NB
58 fprintf(stderr, Name ": cannot get array info for %s\n", devname);
59 return 1;
60 }
61
82d9eba6
NB
62 st = super_by_version(info.array.major_version, info.array.minor_version);
63 if (!st) {
f9ce90ba
NB
64 fprintf(stderr, Name ": cannot handle arrays with superblock version %d\n", info.array.major_version);
65 return 1;
66 }
67
4b1ac34b 68 if (info.array.level != -1) {
e5329c37
NB
69 fprintf(stderr, Name ": can only add devices to linear arrays\n");
70 return 1;
71 }
72
73 nfd = open(newdev, O_RDWR|O_EXCL);
74 if (nfd < 0) {
75 fprintf(stderr, Name ": cannot open %s\n", newdev);
76 return 1;
77 }
78 fstat(nfd, &stb);
79 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
80 fprintf(stderr, Name ": %s is not a block device!\n", newdev);
81 close(nfd);
82 return 1;
83 }
84 /* now check out all the devices and make sure we can read the superblock */
4b1ac34b 85 for (d=0 ; d < info.array.raid_disks ; d++) {
e5329c37
NB
86 mdu_disk_info_t disk;
87 char *dv;
88
89 disk.number = d;
90 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
91 fprintf(stderr, Name ": cannot get device detail for device %d\n",
92 d);
93 return 1;
94 }
16c6fa80 95 dv = map_dev(disk.major, disk.minor, 1);
e5329c37
NB
96 if (!dv) {
97 fprintf(stderr, Name ": cannot find device file for device %d\n",
98 d);
99 return 1;
100 }
16c6fa80 101 fd2 = dev_open(dv, O_RDWR);
e5329c37
NB
102 if (!fd2) {
103 fprintf(stderr, Name ": cannot open device file %s\n", dv);
104 return 1;
105 }
4b1ac34b
NB
106 if (super) free(super);
107 super= NULL;
82d9eba6 108 if (st->ss->load_super(st, fd2, &super, NULL)) {
e5329c37
NB
109 fprintf(stderr, Name ": cannot find super block on %s\n", dv);
110 close(fd2);
111 return 1;
112 }
113 close(fd2);
114 }
115 /* Ok, looks good. Lets update the superblock and write it out to
116 * newdev.
117 */
118
4b1ac34b
NB
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);
e5eac01f 124 st->ss->update_super(&info, super, "grow", newdev, 0, 0, NULL);
e5329c37 125
96395475 126 if (st->ss->store_super(st, nfd, super)) {
e5329c37
NB
127 fprintf(stderr, Name ": Cannot store new superblock on %s\n", newdev);
128 close(nfd);
129 return 1;
130 }
e5329c37 131 close(nfd);
4b1ac34b
NB
132
133 if (ioctl(fd, ADD_NEW_DISK, &info.disk) != 0) {
e5329c37
NB
134 fprintf(stderr, Name ": Cannot add new disk to this array\n");
135 return 1;
136 }
137 /* Well, that seems to have worked.
138 * Now go through and update all superblocks
139 */
140
4b1ac34b 141 if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
e5329c37
NB
142 fprintf(stderr, Name ": cannot get array info for %s\n", devname);
143 return 1;
144 }
145
146 nd = d;
4b1ac34b 147 for (d=0 ; d < info.array.raid_disks ; d++) {
e5329c37
NB
148 mdu_disk_info_t disk;
149 char *dv;
150
151 disk.number = d;
152 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
153 fprintf(stderr, Name ": cannot get device detail for device %d\n",
154 d);
155 return 1;
156 }
16c6fa80 157 dv = map_dev(disk.major, disk.minor, 1);
e5329c37
NB
158 if (!dv) {
159 fprintf(stderr, Name ": cannot find device file for device %d\n",
160 d);
161 return 1;
162 }
16c6fa80 163 fd2 = dev_open(dv, O_RDWR);
e5329c37
NB
164 if (fd2 < 0) {
165 fprintf(stderr, Name ": cannot open device file %s\n", dv);
166 return 1;
167 }
82d9eba6 168 if (st->ss->load_super(st, fd2, &super, NULL)) {
e5329c37
NB
169 fprintf(stderr, Name ": cannot find super block on %s\n", dv);
170 close(fd);
171 return 1;
172 }
4b1ac34b
NB
173 info.array.raid_disks = nd+1;
174 info.array.nr_disks = nd+1;
175 info.array.active_disks = nd+1;
176 info.array.working_disks = nd+1;
177 info.disk.number = nd;
178 info.disk.major = major(stb.st_rdev);
179 info.disk.minor = minor(stb.st_rdev);
180 info.disk.raid_disk = nd;
181 info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE);
e5eac01f 182 st->ss->update_super(&info, super, "grow", dv, 0, 0, NULL);
4b1ac34b 183
96395475 184 if (st->ss->store_super(st, fd2, super)) {
e5329c37
NB
185 fprintf(stderr, Name ": Cannot store new superblock on %s\n", dv);
186 close(fd2);
187 return 1;
188 }
189 close(fd2);
190 }
191
192 return 0;
193}
f5e166fe 194
8fac0577 195int Grow_addbitmap(char *devname, int fd, char *file, int chunk, int delay, int write_behind, int force)
f5e166fe
NB
196{
197 /*
198 * First check that array doesn't have a bitmap
199 * Then create the bitmap
200 * Then add it
201 *
202 * For internal bitmaps, we need to check the version,
203 * find all the active devices, and write the bitmap block
204 * to all devices
205 */
206 mdu_bitmap_file_t bmf;
207 mdu_array_info_t array;
208 struct supertype *st;
dcec9ee5
NB
209 int major = BITMAP_MAJOR_HI;
210 int vers = md_get_version(fd);
8fac0577 211 unsigned long long bitmapsize, array_size;
dcec9ee5
NB
212
213 if (vers < 9003) {
214 major = BITMAP_MAJOR_HOSTENDIAN;
215#ifdef __BIG_ENDIAN
216 fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
217 " between different architectured. Consider upgrading the Linux kernel.\n");
218#endif
219 }
f5e166fe
NB
220
221 if (ioctl(fd, GET_BITMAP_FILE, &bmf) != 0) {
353632d9 222 if (errno == ENOMEM)
f5e166fe
NB
223 fprintf(stderr, Name ": Memory allocation failure.\n");
224 else
225 fprintf(stderr, Name ": bitmaps not supported by this kernel.\n");
226 return 1;
227 }
228 if (bmf.pathname[0]) {
fe80f49b
NB
229 if (strcmp(file,"none")==0) {
230 if (ioctl(fd, SET_BITMAP_FILE, -1)!= 0) {
231 fprintf(stderr, Name ": failed to remove bitmap %s\n",
232 bmf.pathname);
233 return 1;
234 }
235 return 0;
236 }
f5e166fe
NB
237 fprintf(stderr, Name ": %s already has a bitmap (%s)\n",
238 devname, bmf.pathname);
239 return 1;
240 }
241 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
242 fprintf(stderr, Name ": cannot get array status for %s\n", devname);
243 return 1;
244 }
245 if (array.state & (1<<MD_SB_BITMAP_PRESENT)) {
fe80f49b
NB
246 if (strcmp(file, "none")==0) {
247 array.state &= ~(1<<MD_SB_BITMAP_PRESENT);
248 if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
249 fprintf(stderr, Name ": failed to remove internal bitmap.\n");
250 return 1;
251 }
252 return 0;
253 }
f5e166fe
NB
254 fprintf(stderr, Name ": Internal bitmap already present on %s\n",
255 devname);
256 return 1;
257 }
8fac0577
NB
258 bitmapsize = array.size;
259 bitmapsize <<= 1;
260#ifdef BLKGETSIZE64
261 if (ioctl(fd, BLKGETSIZE64, &array_size) == 0 &&
262 array_size > (0x7fffffffULL<<9)) {
263 /* Array is big enough that we cannot trust array.size
264 * try other approaches
265 */
266 bitmapsize = get_component_size(fd);
267 }
268#endif
269 if (bitmapsize == 0) {
270 fprintf(stderr, Name ": Cannot reliably determine size of array to create bitmap - sorry.\n");
271 return 1;
272 }
273
f9c25f1d 274 if (array.level == 10) {
8686f3ed 275 int ncopies = (array.layout&255)*((array.layout>>8)&255);
f9c25f1d
NB
276 bitmapsize = bitmapsize * array.raid_disks / ncopies;
277 }
278
f5e166fe
NB
279 st = super_by_version(array.major_version, array.minor_version);
280 if (!st) {
281 fprintf(stderr, Name ": Cannot understand version %d.%d\n",
282 array.major_version, array.minor_version);
283 return 1;
284 }
fe80f49b
NB
285 if (strcmp(file, "none") == 0) {
286 fprintf(stderr, Name ": no bitmap found on %s\n", devname);
287 return 1;
288 } else if (strcmp(file, "internal") == 0) {
f5e166fe 289 int d;
ea329559 290 for (d=0; d< st->max_devs; d++) {
f5e166fe
NB
291 mdu_disk_info_t disk;
292 char *dv;
293 disk.number = d;
294 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
295 continue;
296 if (disk.major == 0 &&
297 disk.minor == 0)
298 continue;
299 if ((disk.state & (1<<MD_DISK_SYNC))==0)
300 continue;
16c6fa80 301 dv = map_dev(disk.major, disk.minor, 1);
f5e166fe
NB
302 if (dv) {
303 void *super;
16c6fa80 304 int fd2 = dev_open(dv, O_RDWR);
f5e166fe
NB
305 if (fd2 < 0)
306 continue;
307 if (st->ss->load_super(st, fd2, &super, NULL)==0) {
199171a2
NB
308 if (st->ss->add_internal_bitmap(
309 st, super,
310 &chunk, delay, write_behind,
311 bitmapsize, 0, major)
312 )
21e92547
NB
313 st->ss->write_bitmap(st, fd2, super);
314 else {
315 fprintf(stderr, Name ": failed to create internal bitmap - chunksize problem.\n");
316 close(fd2);
317 return 1;
318 }
f5e166fe
NB
319 }
320 close(fd2);
321 }
322 }
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");
326 return 1;
327 }
fe80f49b
NB
328 } else {
329 int uuid[4];
330 int bitmap_fd;
331 int d;
332 int max_devs = st->max_devs;
333 void *super = NULL;
fe80f49b
NB
334
335 /* try to load a superblock */
336 for (d=0; d<max_devs; d++) {
337 mdu_disk_info_t disk;
338 char *dv;
339 int fd2;
340 disk.number = d;
341 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
342 continue;
343 if ((disk.major==0 && disk.minor==0) ||
344 (disk.state & (1<<MD_DISK_REMOVED)))
345 continue;
16c6fa80 346 dv = map_dev(disk.major, disk.minor, 1);
fe80f49b 347 if (!dv) continue;
16c6fa80 348 fd2 = dev_open(dv, O_RDONLY);
fe80f49b
NB
349 if (fd2 >= 0 &&
350 st->ss->load_super(st, fd2, &super, NULL) == 0) {
351 close(fd2);
352 st->ss->uuid_from_super(uuid, super);
353 break;
354 }
355 close(fd2);
356 }
357 if (d == max_devs) {
358 fprintf(stderr, Name ": cannot find UUID for array!\n");
359 return 1;
360 }
8fac0577 361 if (CreateBitmap(file, force, (char*)uuid, chunk,
f9c25f1d 362 delay, write_behind, bitmapsize, major)) {
fe80f49b
NB
363 return 1;
364 }
365 bitmap_fd = open(file, O_RDWR);
366 if (bitmap_fd < 0) {
8fac0577 367 fprintf(stderr, Name ": weird: %s cannot be opened\n",
fe80f49b
NB
368 file);
369 return 1;
370 }
371 if (ioctl(fd, SET_BITMAP_FILE, bitmap_fd) < 0) {
372 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
373 devname, strerror(errno));
374 return 1;
375 }
376 }
f5e166fe
NB
377
378 return 0;
379}
380
e86c9dd6
NB
381
382/*
383 * When reshaping an array we might need to backup some data.
384 * This is written to all spares with a 'super_block' describing it.
385 * The superblock goes 1K form the end of the used space on the
386 * device.
387 * It if written after the backup is complete.
388 * It has the following structure.
389 */
390
391struct mdp_backup_super {
392 char magic[16]; /* md_backup_data-1 */
393 __u8 set_uuid[16];
394 __u64 mtime;
395 /* start/sizes in 512byte sectors */
396 __u64 devstart;
397 __u64 arraystart;
398 __u64 length;
399 __u32 sb_csum; /* csum of preceeding bytes. */
400};
401
402int bsb_csum(char *buf, int len)
403{
404 int i;
405 int csum = 0;
406 for (i=0; i<len; i++)
407 csum = (csum<<3) + buf[0];
408 return __cpu_to_le32(csum);
409}
410
06b0d786 411int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
e86c9dd6
NB
412 long long size,
413 int level, int layout, int chunksize, int raid_disks)
414{
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
419 * and go from there.
420 */
421 struct mdu_array_info_s array;
422 char *c;
423
424 struct mdp_backup_super bsb;
425 struct supertype *st;
426
427 int nlevel, olevel;
428 int nchunk, ochunk;
429 int nlayout, olayout;
430 int ndisks, odisks;
431 int ndata, odata;
432 unsigned long long nstripe, ostripe, last_block;
433 int *fdlist;
434 unsigned long long *offsets;
435 int d, i, spares;
436 int nrdisks;
437 int err;
438 void *super = NULL;
439
440 struct sysarray *sra;
441 struct sysdev *sd;
442
443 if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) {
444 fprintf(stderr, Name ": %s is not an active md array - aborting\n",
445 devname);
446 return 1;
447 }
448 c = map_num(pers, array.level);
449 if (c == NULL) c = "-unknown-";
450 switch(array.level) {
451 default: /* raid0, linear, multipath cannot be reconfigured */
452 fprintf(stderr, Name ": %s array %s cannot be reshaped.\n",
453 c, devname);
454 return 1;
455
456 case LEVEL_FAULTY: /* only 'layout' change is permitted */
457
458 if (size >= 0) {
459 fprintf(stderr, Name ": %s: Cannot change size of a 'faulty' array\n",
460 devname);
461 return 1;
462 }
463 if (level != UnSet && level != LEVEL_FAULTY) {
464 fprintf(stderr, Name ": %s: Cannot change RAID level of a 'faulty' array\n",
465 devname);
466 return 1;
467 }
468 if (chunksize || raid_disks) {
469 fprintf(stderr, Name ": %s: Cannot change chunksize or disks of a 'faulty' array\n",
470 devname);
471 return 1;
472 }
473 if (layout == UnSet)
474 return 0; /* nothing to do.... */
475
476 array.layout = layout;
477 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
478 fprintf(stderr, Name ": Cannot set layout for %s: %s\n",
479 devname, strerror(errno));
480 return 1;
481 }
482 if (!quiet)
483 printf("layout for %s set to %d\n", devname, array.layout);
484 return 0;
485
486 case 1: /* raid_disks and size can each be changed. They are independant */
487
488 if (level != UnSet && level != 1) {
489 fprintf(stderr, Name ": %s: Cannot change RAID level of a RAID1 array.\n",
490 devname);
491 return 1;
492 }
493 if (chunksize || layout != UnSet) {
494 fprintf(stderr, Name ": %s: Cannot change chunk size of layout for a RAID1 array.\n",
495 devname);
496 return 1;
497 }
498
499 /* Each can trigger a resync/recovery which will block the
500 * other from happening. Later we could block
501 * resync for the duration via 'sync_action'...
502 */
9860f271 503 if (raid_disks > 0) {
e86c9dd6 504 array.raid_disks = raid_disks;
9860f271
NB
505 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
506 fprintf(stderr, Name ": Cannot set raid-devices for %s: %s\n",
507 devname, strerror(errno));
508 return 1;
509 }
510 }
511 if (size >= 0) {
e86c9dd6 512 array.size = size;
9860f271
NB
513 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
514 fprintf(stderr, Name ": Cannot set device size for %s: %s\n",
515 devname, strerror(errno));
516 return 1;
517 }
e86c9dd6
NB
518 }
519 return 0;
520
521 case 4:
522 case 5:
523 case 6:
524 st = super_by_version(array.major_version,
525 array.minor_version);
526 /* size can be changed independantly.
527 * layout/chunksize/raid_disks/level can be changed
528 * though the kernel may not support it all.
529 * If 'suspend_lo' is not present in devfs, then
530 * these cannot be changed.
531 */
532 if (size >= 0) {
533 /* Cannot change other details as well.. */
534 if (layout != UnSet ||
535 chunksize != 0 ||
536 raid_disks != 0 ||
537 level != UnSet) {
538 fprintf(stderr, Name ": %s: Cannot change shape as well as size of a %s array.\n",
539 devname, c);
540 return 1;
541 }
542 array.size = size;
543 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
544 fprintf(stderr, Name ": Cannot set device size/shape for %s: %s\n",
545 devname, strerror(errno));
546 return 1;
547 }
548 return 0;
549 }
550 /* Ok, just change the shape. This can be awkward.
551 * There are three possibilities.
552 * 1/ The array will shrink. We don't support this
553 * possibility. Maybe one day...
554 * 2/ The array will not change size. This is easy enough
555 * to do, but not reliably. If the process is aborted
556 * the array *will* be corrupted. So maybe we can allow
557 * this but only if the user is really certain. e.g.
558 * --really-risk-everything
559 * 3/ The array will grow. This can be reliably achieved.
560 * However the kernel's restripe routines will cheerfully
561 * overwrite some early data before it is safe. So we
562 * need to make a backup of the early parts of the array
563 * and be ready to restore it if rebuild aborts very early.
564 *
565 * We backup data by writing it to all spares (there must be
566 * at least 1, so even raid6->raid5 requires a spare to be
567 * present).
568 *
569 * So: we enumerate the devices in the array and
570 * make sure we can open all of them.
571 * Then we freeze the early part of the array and
572 * backup to the various spares.
573 * Then we request changes and start the reshape.
574 * Monitor progress until it has passed the danger zone.
575 * and finally invalidate the copied data and unfreeze the
576 * start of the array.
577 *
578 * Before we can do this we need to decide:
579 * - will the array grow? Just calculate size
580 * - how much needs to be saved: count stripes.
581 * - where to save data... good question.
582 *
583 */
584 nlevel = olevel = array.level;
585 nchunk = ochunk = array.chunk_size;
586 nlayout = olayout = array.layout;
587 ndisks = odisks = array.raid_disks;
588
589 if (level != UnSet) nlevel = level;
590 if (chunksize) nchunk = chunksize;
591 if (layout != UnSet) nlayout = layout;
592 if (raid_disks) ndisks = raid_disks;
593
594 odata = odisks-1;
595 if (olevel == 6) odata--; /* number of data disks */
596 ndata = ndisks-1;
597 if (nlevel == 6) ndata--;
598
599 if (ndata < odata) {
600 fprintf(stderr, Name ": %s: Cannot reduce number of data disks (yet).\n",
601 devname);
602 return 1;
603 }
604 if (ndata == odata) {
605 fprintf(stderr, Name ": %s: Cannot reshape array without increasing size (yet).\n",
606 devname);
607 return 1;
608 }
609 /* Well, it is growing... so how much do we need to backup.
610 * Need to backup a full number of new-stripes, such that the
611 * last one does not over-write any place that it would be read
612 * from
613 */
614 nstripe = ostripe = 0;
353632d9 615 while (nstripe >= ostripe) {
e86c9dd6
NB
616 nstripe += nchunk/512;
617 last_block = nstripe * ndata;
353632d9 618 ostripe = last_block / odata / (ochunk/512) * (ochunk/512);
e86c9dd6 619 }
353632d9 620 printf("mdadm: Need to backup %lluK of critical section..\n", last_block/2);
e86c9dd6
NB
621
622 sra = sysfs_read(fd, 0,
623 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE);
624 if (!sra) {
625 fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n",
626 devname);
627 return 1;
628 }
629
630 if (last_block >= sra->component_size/2) {
631 fprintf(stderr, Name ": %s: Something wrong - reshape aborted\n",
632 devname);
633 return 1;
634 }
06b0d786
NB
635 if (sra->spares == 0 && backup_file == NULL) {
636 fprintf(stderr, Name ": %s: Cannot grow - need a spare or backup-file to backup critical section\n",
353632d9
NB
637 devname);
638 return 1;
639 }
e86c9dd6
NB
640
641 nrdisks = array.nr_disks + sra->spares;
642 /* Now we need to open all these devices so we can read/write.
643 */
06b0d786
NB
644 fdlist = malloc((1+nrdisks) * sizeof(int));
645 offsets = malloc((1+nrdisks) * sizeof(offsets[0]));
e86c9dd6
NB
646 if (!fdlist || !offsets) {
647 fprintf(stderr, Name ": malloc failed: grow aborted\n");
648 return 1;
649 }
06b0d786 650 for (d=0; d <= nrdisks; d++)
e86c9dd6
NB
651 fdlist[d] = -1;
652 d = array.raid_disks;
653 for (sd = sra->devs; sd; sd=sd->next) {
654 if (sd->state & (1<<MD_DISK_FAULTY))
655 continue;
656 if (sd->state & (1<<MD_DISK_SYNC)) {
16c6fa80
NB
657 char *dn = map_dev(sd->major, sd->minor, 1);
658 fdlist[sd->role] = dev_open(dn, O_RDONLY);
e86c9dd6
NB
659 offsets[sd->role] = sd->offset;
660 if (fdlist[sd->role] < 0) {
661 fprintf(stderr, Name ": %s: cannot open component %s\n",
e81cdd9f 662 devname, dn?dn:"-unknown-");
e86c9dd6
NB
663 goto abort;
664 }
665 } else {
666 /* spare */
16c6fa80
NB
667 char *dn = map_dev(sd->major, sd->minor, 1);
668 fdlist[d] = dev_open(dn, O_RDWR);
e86c9dd6
NB
669 offsets[d] = sd->offset;
670 if (fdlist[d]<0) {
671 fprintf(stderr, Name ": %s: cannot open component %s\n",
e81cdd9f 672 devname, dn?dn:"-unknown");
e86c9dd6
NB
673 goto abort;
674 }
675 d++;
676 }
677 }
678 for (i=0 ; i<array.raid_disks; i++)
679 if (fdlist[i] < 0) {
680 fprintf(stderr, Name ": %s: failed to find device %d. Array might be degraded.\n"
681 " --grow aborted\n", devname, i);
682 goto abort;
683 }
06b0d786
NB
684 spares = sra->spares;
685 if (backup_file) {
686 fdlist[d] = open(backup_file, O_RDWR|O_CREAT|O_EXCL, 0600);
687 if (fdlist[d] < 0) {
688 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
689 devname, backup_file, strerror(errno));
690 goto abort;
691 }
692 offsets[d] = 8;
693 d++;
694 spares++;
695 }
e86c9dd6 696 if (fdlist[array.raid_disks] < 0) {
06b0d786 697 fprintf(stderr, Name ": %s: failed to find a spare and no backup-file given - --grow aborted\n",
e86c9dd6
NB
698 devname);
699 goto abort;
700 }
701
702 /* Find a superblock */
703 if (st->ss->load_super(st, fdlist[0], &super, NULL)) {
704 fprintf(stderr, Name ": %s: Cannot find a superblock\n",
705 devname);
706 goto abort;
707 }
708
2efedc7b
NB
709
710 memcpy(bsb.magic, "md_backup_data-1", 16);
711 st->ss->uuid_from_super((int*)&bsb.set_uuid, super);
712 bsb.mtime = __cpu_to_le64(time(0));
713 bsb.arraystart = 0;
714 bsb.length = __cpu_to_le64(last_block);
715
716 /* Decide offset for the backup, llseek the spares, and write
717 * a leading superblock 4K earlier.
718 */
e86c9dd6 719 for (i=array.raid_disks; i<d; i++) {
2efedc7b 720 char buf[4096];
06b0d786
NB
721 if (i==d-1 && backup_file) {
722 /* This is the backup file */
723 offsets[i] = 8;
724 } else
725 offsets[i] += sra->component_size - last_block - 8;
2efedc7b
NB
726 if (lseek64(fdlist[i], (offsets[i]<<9) - 4096, 0)
727 != (offsets[i]<<9) - 4096) {
e86c9dd6
NB
728 fprintf(stderr, Name ": could not seek...\n");
729 goto abort;
730 }
2efedc7b
NB
731 memset(buf, 0, sizeof(buf));
732 bsb.devstart = __cpu_to_le64(offsets[i]);
733 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
734 memcpy(buf, &bsb, sizeof(bsb));
735 if (write(fdlist[i], buf, 4096) != 4096) {
736 fprintf(stderr, Name ": could not write leading superblock\n");
737 goto abort;
738 }
e86c9dd6
NB
739 }
740 array.level = nlevel;
741 array.raid_disks = ndisks;
742 array.chunk_size = nchunk;
743 array.layout = nlayout;
744 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
745 fprintf(stderr, Name ": Cannot set device size/shape for %s: %s\n",
746 devname, strerror(errno));
747 goto abort;
748 }
749
750 /* suspend the relevant region */
751 sysfs_set_num(sra, NULL, "suspend_hi", 0); /* just in case */
752 if (sysfs_set_num(sra, NULL, "suspend_lo", 0) < 0 ||
753 sysfs_set_num(sra, NULL, "suspend_hi", last_block) < 0) {
754 fprintf(stderr, Name ": %s: failed to suspend device.\n",
755 devname);
756 goto abort_resume;
757 }
758
759
760 err = save_stripes(fdlist, offsets,
761 odisks, ochunk, olevel, olayout,
762 spares, fdlist+odisks,
06b0d786 763 0ULL, last_block*512);
e86c9dd6
NB
764
765 /* abort if there was an error */
766 if (err < 0) {
767 fprintf(stderr, Name ": %s: failed to save critical region\n",
768 devname);
769 goto abort_resume;
770 }
2efedc7b 771
e86c9dd6 772 for (i=odisks; i<d ; i++) {
353632d9 773 bsb.devstart = __cpu_to_le64(offsets[i]);
e86c9dd6 774 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
206c5eae 775 if (lseek64(fdlist[i], (offsets[i]+last_block)<<9, 0) < 0 ||
06b0d786
NB
776 write(fdlist[i], &bsb, sizeof(bsb)) != sizeof(bsb) ||
777 fsync(fdlist[i]) != 0) {
206c5eae
NB
778 fprintf(stderr, Name ": %s: fail to save metadata for critical region backups.\n",
779 devname);
780 goto abort_resume;
781 }
e86c9dd6
NB
782 }
783
784 /* start the reshape happening */
785 if (sysfs_set_str(sra, NULL, "sync_action", "reshape") < 0) {
786 fprintf(stderr, Name ": %s: failed to initiate reshape\n",
787 devname);
788 goto abort_resume;
789 }
790 /* wait for reshape to pass the critical region */
791 while(1) {
792 unsigned long long comp;
206c5eae
NB
793 if (sysfs_get_ll(sra, NULL, "sync_completed", &comp)<0) {
794 sleep(5);
e86c9dd6 795 break;
206c5eae 796 }
e86c9dd6
NB
797 if (comp >= nstripe)
798 break;
799 sleep(1);
800 }
f5e166fe 801
e86c9dd6
NB
802 /* invalidate superblocks */
803 memset(&bsb, 0, sizeof(bsb));
804 for (i=odisks; i<d ; i++) {
805 lseek64(fdlist[i], (offsets[i]+last_block)<<9, 0);
9fca7d62
NB
806 if (write(fdlist[i], &bsb, sizeof(bsb)) < 0) {
807 fprintf(stderr, Name ": %s: failed to invalidate metadata for raid disk %d\n",
808 devname, i);
809 }
e86c9dd6
NB
810 }
811
812 /* unsuspend. */
813 sysfs_set_num(sra, NULL, "suspend_lo", last_block);
814
815 for (i=0; i<d; i++)
816 if (fdlist[i] >= 0)
817 close(fdlist[i]);
818 free(fdlist);
819 free(offsets);
06b0d786
NB
820 if (backup_file)
821 unlink(backup_file);
e86c9dd6 822
206c5eae 823 printf(Name ": ... critical section passed.\n");
e86c9dd6
NB
824 break;
825 }
826 return 0;
827
828
829 abort_resume:
830 sysfs_set_num(sra, NULL, "suspend_lo", last_block);
831 abort:
832 for (i=0; i<array.nr_disks; i++)
833 if (fdlist[i] >= 0)
834 close(fdlist[i]);
835 free(fdlist);
836 free(offsets);
06b0d786
NB
837 if (backup_file)
838 unlink(backup_file);
e86c9dd6
NB
839 return 1;
840
841}
353632d9
NB
842
843/*
844 * If any spare contains md_back_data-1 which is recent wrt mtime,
845 * write that data into the array and update the super blocks with
846 * the new reshape_progress
847 */
06b0d786 848int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt, char *backup_file)
353632d9
NB
849{
850 int i, j;
851 int old_disks;
852 int err = 0;
853 unsigned long long *offsets;
854
855 if (info->delta_disks < 0)
856 return 1; /* cannot handle a shrink */
857 if (info->new_level != info->array.level ||
858 info->new_layout != info->array.layout ||
859 info->new_chunk != info->array.chunk_size)
860 return 1; /* Can only handle change in disks */
861
862 old_disks = info->array.raid_disks - info->delta_disks;
863
06b0d786 864 for (i=old_disks-(backup_file?1:0); i<cnt; i++) {
353632d9
NB
865 void *super = NULL;
866 struct mdinfo dinfo;
353632d9 867 struct mdp_backup_super bsb;
2efedc7b 868 char buf[4096];
06b0d786 869 int fd;
353632d9
NB
870
871 /* This was a spare and may have some saved data on it.
872 * Load the superblock, find and load the
873 * backup_super_block.
874 * If either fail, go on to next device.
875 * If the backup contains no new info, just return
206c5eae 876 * else restore data and update all superblocks
353632d9 877 */
06b0d786
NB
878 if (i == old_disks-1) {
879 fd = open(backup_file, O_RDONLY);
880 if (fd<0)
881 continue;
06b0d786
NB
882 } else {
883 fd = fdlist[i];
884 if (fd < 0)
885 continue;
886 if (st->ss->load_super(st, fd, &super, NULL))
887 continue;
353632d9 888
06b0d786
NB
889 st->ss->getinfo_super(&dinfo, super);
890 free(super); super = NULL;
891 if (lseek64(fd,
892 (dinfo.data_offset + dinfo.component_size - 8) <<9,
893 0) < 0)
894 continue; /* Cannot seek */
895 }
896 if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb))
353632d9
NB
897 continue; /* Cannot read */
898 if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0)
899 continue;
900 if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb)))
901 continue; /* bad checksum */
902 if (memcmp(bsb.set_uuid,info->uuid, 16) != 0)
903 continue; /* Wrong uuid */
904
905 if (info->array.utime > __le64_to_cpu(bsb.mtime) + 3600 ||
906 info->array.utime < __le64_to_cpu(bsb.mtime))
907 continue; /* time stamp is too bad */
908
909 if (__le64_to_cpu(bsb.arraystart) != 0)
910 continue; /* Can only handle backup from start of array */
911 if (__le64_to_cpu(bsb.length) <
912 info->reshape_progress)
913 continue; /* No new data here */
914
06b0d786 915 if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0)
353632d9 916 continue; /* Cannot seek */
2efedc7b 917 /* There should be a duplicate backup superblock 4k before here */
06b0d786
NB
918 if (lseek64(fd, -4096, 1) < 0 ||
919 read(fd, buf, 4096) != 4096 ||
9860f271 920 memcmp(buf, &bsb, sizeof(bsb)) != 0)
2efedc7b
NB
921 continue; /* Cannot find leading superblock */
922
353632d9
NB
923 /* Now need the data offsets for all devices. */
924 offsets = malloc(sizeof(*offsets)*info->array.raid_disks);
925 for(j=0; j<info->array.raid_disks; j++) {
926 if (fdlist[j] < 0)
927 continue;
928 if (st->ss->load_super(st, fdlist[j], &super, NULL))
929 /* FIXME should be this be an error */
930 continue;
31317663 931 st->ss->getinfo_super(&dinfo, super);
353632d9
NB
932 free(super); super = NULL;
933 offsets[j] = dinfo.data_offset;
934 }
935 printf(Name ": restoring critical section\n");
936
937 if (restore_stripes(fdlist, offsets,
938 info->array.raid_disks,
939 info->new_chunk,
940 info->new_level,
941 info->new_layout,
06b0d786 942 fd, __le64_to_cpu(bsb.devstart)*512,
353632d9
NB
943 0, __le64_to_cpu(bsb.length)*512)) {
944 /* didn't succeed, so giveup */
206c5eae 945 return -1;
353632d9
NB
946 }
947
948 /* Ok, so the data is restored. Let's update those superblocks. */
949
950 for (j=0; j<info->array.raid_disks; j++) {
951 if (fdlist[j] < 0) continue;
952 if (st->ss->load_super(st, fdlist[j], &super, NULL))
953 continue;
31317663 954 st->ss->getinfo_super(&dinfo, super);
353632d9 955 dinfo.reshape_progress = __le64_to_cpu(bsb.length);
e5eac01f 956 st->ss->update_super(&dinfo, super, "_reshape_progress",NULL,0, 0, NULL);
353632d9
NB
957 st->ss->store_super(st, fdlist[j], super);
958 free(super);
959 }
960
961 /* And we are done! */
962 return 0;
963 }
964 return err;
965}