]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Grow.c
External reshape (step 1): container reshape and ->reshape_super()
[thirdparty/mdadm.git] / Grow.c
CommitLineData
e5329c37
NB
1/*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
e736b623 4 * Copyright (C) 2001-2009 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
e736b623 22 * Email: <neilb@suse.de>
e5329c37
NB
23 */
24#include "mdadm.h"
25#include "dlink.h"
7236ee7a 26#include <sys/mman.h>
e5329c37
NB
27
28#if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
29#error no endian defined
30#endif
31#include "md_u.h"
32#include "md_p.h"
33
e9e43ec3
N
34#ifndef offsetof
35#define offsetof(t,f) ((size_t)&(((t*)0)->f))
36#endif
37
e5329c37
NB
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
e5329c37
NB
50 struct stat stb;
51 int nfd, fd2;
52 int d, nd;
82d9eba6 53 struct supertype *st = NULL;
4725bc31 54 char *subarray = NULL;
e5329c37 55
4b1ac34b 56 if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
e5329c37
NB
57 fprintf(stderr, Name ": cannot get array info for %s\n", devname);
58 return 1;
59 }
60
4725bc31
N
61 if (info.array.level != -1) {
62 fprintf(stderr, Name ": can only add devices to linear arrays\n");
63 return 1;
64 }
65
66 st = super_by_fd(fd, &subarray);
82d9eba6 67 if (!st) {
f9ce90ba
NB
68 fprintf(stderr, Name ": cannot handle arrays with superblock version %d\n", info.array.major_version);
69 return 1;
70 }
71
4725bc31
N
72 if (subarray) {
73 fprintf(stderr, Name ": Cannot grow linear sub-arrays yet\n");
74 free(subarray);
75 free(st);
e5329c37
NB
76 }
77
6416d527 78 nfd = open(newdev, O_RDWR|O_EXCL|O_DIRECT);
e5329c37
NB
79 if (nfd < 0) {
80 fprintf(stderr, Name ": cannot open %s\n", newdev);
4725bc31 81 free(st);
e5329c37
NB
82 return 1;
83 }
84 fstat(nfd, &stb);
85 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
86 fprintf(stderr, Name ": %s is not a block device!\n", newdev);
87 close(nfd);
4725bc31 88 free(st);
e5329c37
NB
89 return 1;
90 }
91 /* now check out all the devices and make sure we can read the superblock */
4b1ac34b 92 for (d=0 ; d < info.array.raid_disks ; d++) {
e5329c37
NB
93 mdu_disk_info_t disk;
94 char *dv;
95
4725bc31
N
96 st->ss->free_super(st);
97
e5329c37
NB
98 disk.number = d;
99 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
100 fprintf(stderr, Name ": cannot get device detail for device %d\n",
101 d);
4725bc31
N
102 close(nfd);
103 free(st);
e5329c37
NB
104 return 1;
105 }
16c6fa80 106 dv = map_dev(disk.major, disk.minor, 1);
e5329c37
NB
107 if (!dv) {
108 fprintf(stderr, Name ": cannot find device file for device %d\n",
109 d);
4725bc31
N
110 close(nfd);
111 free(st);
e5329c37
NB
112 return 1;
113 }
16c6fa80 114 fd2 = dev_open(dv, O_RDWR);
e5329c37
NB
115 if (!fd2) {
116 fprintf(stderr, Name ": cannot open device file %s\n", dv);
4725bc31
N
117 close(nfd);
118 free(st);
e5329c37
NB
119 return 1;
120 }
3da92f27
NB
121
122 if (st->ss->load_super(st, fd2, NULL)) {
e5329c37 123 fprintf(stderr, Name ": cannot find super block on %s\n", dv);
4725bc31 124 close(nfd);
e5329c37 125 close(fd2);
4725bc31 126 free(st);
e5329c37
NB
127 return 1;
128 }
129 close(fd2);
130 }
131 /* Ok, looks good. Lets update the superblock and write it out to
132 * newdev.
133 */
aba69144 134
4b1ac34b
NB
135 info.disk.number = d;
136 info.disk.major = major(stb.st_rdev);
137 info.disk.minor = minor(stb.st_rdev);
138 info.disk.raid_disk = d;
139 info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE);
3da92f27 140 st->ss->update_super(st, &info, "linear-grow-new", newdev,
f752781f 141 0, 0, NULL);
e5329c37 142
3da92f27 143 if (st->ss->store_super(st, nfd)) {
f752781f
NB
144 fprintf(stderr, Name ": Cannot store new superblock on %s\n",
145 newdev);
e5329c37
NB
146 close(nfd);
147 return 1;
148 }
e5329c37 149 close(nfd);
4b1ac34b
NB
150
151 if (ioctl(fd, ADD_NEW_DISK, &info.disk) != 0) {
e5329c37
NB
152 fprintf(stderr, Name ": Cannot add new disk to this array\n");
153 return 1;
154 }
155 /* Well, that seems to have worked.
156 * Now go through and update all superblocks
157 */
158
4b1ac34b 159 if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
e5329c37
NB
160 fprintf(stderr, Name ": cannot get array info for %s\n", devname);
161 return 1;
162 }
163
164 nd = d;
4b1ac34b 165 for (d=0 ; d < info.array.raid_disks ; d++) {
e5329c37
NB
166 mdu_disk_info_t disk;
167 char *dv;
168
169 disk.number = d;
170 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
171 fprintf(stderr, Name ": cannot get device detail for device %d\n",
172 d);
173 return 1;
174 }
16c6fa80 175 dv = map_dev(disk.major, disk.minor, 1);
e5329c37
NB
176 if (!dv) {
177 fprintf(stderr, Name ": cannot find device file for device %d\n",
178 d);
179 return 1;
180 }
16c6fa80 181 fd2 = dev_open(dv, O_RDWR);
e5329c37
NB
182 if (fd2 < 0) {
183 fprintf(stderr, Name ": cannot open device file %s\n", dv);
184 return 1;
185 }
3da92f27 186 if (st->ss->load_super(st, fd2, NULL)) {
e5329c37
NB
187 fprintf(stderr, Name ": cannot find super block on %s\n", dv);
188 close(fd);
189 return 1;
190 }
4b1ac34b
NB
191 info.array.raid_disks = nd+1;
192 info.array.nr_disks = nd+1;
193 info.array.active_disks = nd+1;
194 info.array.working_disks = nd+1;
f752781f 195
3da92f27 196 st->ss->update_super(st, &info, "linear-grow-update", dv,
f752781f 197 0, 0, NULL);
aba69144 198
3da92f27 199 if (st->ss->store_super(st, fd2)) {
e5329c37
NB
200 fprintf(stderr, Name ": Cannot store new superblock on %s\n", dv);
201 close(fd2);
202 return 1;
203 }
204 close(fd2);
205 }
206
207 return 0;
208}
f5e166fe 209
8fac0577 210int Grow_addbitmap(char *devname, int fd, char *file, int chunk, int delay, int write_behind, int force)
f5e166fe
NB
211{
212 /*
213 * First check that array doesn't have a bitmap
214 * Then create the bitmap
215 * Then add it
216 *
217 * For internal bitmaps, we need to check the version,
218 * find all the active devices, and write the bitmap block
219 * to all devices
220 */
221 mdu_bitmap_file_t bmf;
222 mdu_array_info_t array;
223 struct supertype *st;
4725bc31 224 char *subarray = NULL;
dcec9ee5
NB
225 int major = BITMAP_MAJOR_HI;
226 int vers = md_get_version(fd);
8fac0577 227 unsigned long long bitmapsize, array_size;
dcec9ee5
NB
228
229 if (vers < 9003) {
230 major = BITMAP_MAJOR_HOSTENDIAN;
231#ifdef __BIG_ENDIAN
232 fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
233 " between different architectured. Consider upgrading the Linux kernel.\n");
234#endif
235 }
f5e166fe
NB
236
237 if (ioctl(fd, GET_BITMAP_FILE, &bmf) != 0) {
353632d9 238 if (errno == ENOMEM)
f5e166fe
NB
239 fprintf(stderr, Name ": Memory allocation failure.\n");
240 else
241 fprintf(stderr, Name ": bitmaps not supported by this kernel.\n");
242 return 1;
243 }
244 if (bmf.pathname[0]) {
fe80f49b
NB
245 if (strcmp(file,"none")==0) {
246 if (ioctl(fd, SET_BITMAP_FILE, -1)!= 0) {
247 fprintf(stderr, Name ": failed to remove bitmap %s\n",
248 bmf.pathname);
249 return 1;
250 }
251 return 0;
252 }
f5e166fe
NB
253 fprintf(stderr, Name ": %s already has a bitmap (%s)\n",
254 devname, bmf.pathname);
255 return 1;
256 }
257 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
258 fprintf(stderr, Name ": cannot get array status for %s\n", devname);
259 return 1;
260 }
261 if (array.state & (1<<MD_SB_BITMAP_PRESENT)) {
fe80f49b
NB
262 if (strcmp(file, "none")==0) {
263 array.state &= ~(1<<MD_SB_BITMAP_PRESENT);
264 if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
265 fprintf(stderr, Name ": failed to remove internal bitmap.\n");
266 return 1;
267 }
268 return 0;
269 }
f5e166fe
NB
270 fprintf(stderr, Name ": Internal bitmap already present on %s\n",
271 devname);
272 return 1;
273 }
4725bc31
N
274
275 if (strcmp(file, "none") == 0) {
276 fprintf(stderr, Name ": no bitmap found on %s\n", devname);
277 return 1;
278 }
5b28bd56
NB
279 if (array.level <= 0) {
280 fprintf(stderr, Name ": Bitmaps not meaningful with level %s\n",
281 map_num(pers, array.level)?:"of this array");
282 return 1;
283 }
8fac0577
NB
284 bitmapsize = array.size;
285 bitmapsize <<= 1;
beae1dfe 286 if (get_dev_size(fd, NULL, &array_size) &&
8fac0577
NB
287 array_size > (0x7fffffffULL<<9)) {
288 /* Array is big enough that we cannot trust array.size
289 * try other approaches
290 */
291 bitmapsize = get_component_size(fd);
292 }
8fac0577
NB
293 if (bitmapsize == 0) {
294 fprintf(stderr, Name ": Cannot reliably determine size of array to create bitmap - sorry.\n");
295 return 1;
296 }
297
f9c25f1d 298 if (array.level == 10) {
8686f3ed 299 int ncopies = (array.layout&255)*((array.layout>>8)&255);
f9c25f1d
NB
300 bitmapsize = bitmapsize * array.raid_disks / ncopies;
301 }
302
4725bc31 303 st = super_by_fd(fd, &subarray);
f5e166fe
NB
304 if (!st) {
305 fprintf(stderr, Name ": Cannot understand version %d.%d\n",
306 array.major_version, array.minor_version);
307 return 1;
308 }
4725bc31
N
309 if (subarray) {
310 fprintf(stderr, Name ": Cannot add bitmaps to sub-arrays yet\n");
311 free(subarray);
312 free(st);
fe80f49b 313 return 1;
4725bc31
N
314 }
315 if (strcmp(file, "internal") == 0) {
f5e166fe 316 int d;
ebeb3663
N
317 if (st->ss->add_internal_bitmap == NULL) {
318 fprintf(stderr, Name ": Internal bitmaps not supported "
319 "with %s metadata\n", st->ss->name);
320 return 1;
321 }
ea329559 322 for (d=0; d< st->max_devs; d++) {
f5e166fe
NB
323 mdu_disk_info_t disk;
324 char *dv;
325 disk.number = d;
326 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
327 continue;
328 if (disk.major == 0 &&
329 disk.minor == 0)
330 continue;
331 if ((disk.state & (1<<MD_DISK_SYNC))==0)
332 continue;
16c6fa80 333 dv = map_dev(disk.major, disk.minor, 1);
f5e166fe 334 if (dv) {
16c6fa80 335 int fd2 = dev_open(dv, O_RDWR);
f5e166fe
NB
336 if (fd2 < 0)
337 continue;
3da92f27 338 if (st->ss->load_super(st, fd2, NULL)==0) {
199171a2 339 if (st->ss->add_internal_bitmap(
3da92f27 340 st,
199171a2
NB
341 &chunk, delay, write_behind,
342 bitmapsize, 0, major)
343 )
3da92f27 344 st->ss->write_bitmap(st, fd2);
21e92547
NB
345 else {
346 fprintf(stderr, Name ": failed to create internal bitmap - chunksize problem.\n");
347 close(fd2);
348 return 1;
349 }
f5e166fe
NB
350 }
351 close(fd2);
352 }
353 }
354 array.state |= (1<<MD_SB_BITMAP_PRESENT);
355 if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
356 fprintf(stderr, Name ": failed to set internal bitmap.\n");
357 return 1;
358 }
fe80f49b
NB
359 } else {
360 int uuid[4];
361 int bitmap_fd;
362 int d;
363 int max_devs = st->max_devs;
fe80f49b
NB
364
365 /* try to load a superblock */
366 for (d=0; d<max_devs; d++) {
367 mdu_disk_info_t disk;
368 char *dv;
369 int fd2;
370 disk.number = d;
371 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
372 continue;
373 if ((disk.major==0 && disk.minor==0) ||
374 (disk.state & (1<<MD_DISK_REMOVED)))
375 continue;
16c6fa80 376 dv = map_dev(disk.major, disk.minor, 1);
fe80f49b 377 if (!dv) continue;
16c6fa80 378 fd2 = dev_open(dv, O_RDONLY);
fe80f49b 379 if (fd2 >= 0 &&
3da92f27 380 st->ss->load_super(st, fd2, NULL) == 0) {
fe80f49b 381 close(fd2);
3da92f27 382 st->ss->uuid_from_super(st, uuid);
fe80f49b
NB
383 break;
384 }
385 close(fd2);
386 }
387 if (d == max_devs) {
388 fprintf(stderr, Name ": cannot find UUID for array!\n");
389 return 1;
390 }
8fac0577 391 if (CreateBitmap(file, force, (char*)uuid, chunk,
f9c25f1d 392 delay, write_behind, bitmapsize, major)) {
fe80f49b
NB
393 return 1;
394 }
395 bitmap_fd = open(file, O_RDWR);
396 if (bitmap_fd < 0) {
8fac0577 397 fprintf(stderr, Name ": weird: %s cannot be opened\n",
fe80f49b
NB
398 file);
399 return 1;
400 }
401 if (ioctl(fd, SET_BITMAP_FILE, bitmap_fd) < 0) {
402 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
403 devname, strerror(errno));
404 return 1;
405 }
406 }
f5e166fe
NB
407
408 return 0;
409}
410
e86c9dd6
NB
411
412/*
413 * When reshaping an array we might need to backup some data.
414 * This is written to all spares with a 'super_block' describing it.
ff94fb86 415 * The superblock goes 4K from the end of the used space on the
e86c9dd6
NB
416 * device.
417 * It if written after the backup is complete.
418 * It has the following structure.
419 */
420
5fdf37e3 421static struct mdp_backup_super {
7236ee7a 422 char magic[16]; /* md_backup_data-1 or -2 */
e86c9dd6
NB
423 __u8 set_uuid[16];
424 __u64 mtime;
425 /* start/sizes in 512byte sectors */
7236ee7a 426 __u64 devstart; /* address on backup device/file of data */
e86c9dd6
NB
427 __u64 arraystart;
428 __u64 length;
429 __u32 sb_csum; /* csum of preceeding bytes. */
7236ee7a
N
430 __u32 pad1;
431 __u64 devstart2; /* offset in to data of second section */
432 __u64 arraystart2;
433 __u64 length2;
434 __u32 sb_csum2; /* csum of preceeding bytes. */
435 __u8 pad[512-68-32];
5fdf37e3 436} __attribute__((aligned(512))) bsb, bsb2;
e86c9dd6 437
4411fb17 438static __u32 bsb_csum(char *buf, int len)
e86c9dd6
NB
439{
440 int i;
441 int csum = 0;
442 for (i=0; i<len; i++)
443 csum = (csum<<3) + buf[0];
444 return __cpu_to_le32(csum);
445}
446
7236ee7a
N
447static int child_grow(int afd, struct mdinfo *sra, unsigned long blocks,
448 int *fds, unsigned long long *offsets,
449 int disks, int chunk, int level, int layout, int data,
450 int dests, int *destfd, unsigned long long *destoffsets);
451static int child_shrink(int afd, struct mdinfo *sra, unsigned long blocks,
452 int *fds, unsigned long long *offsets,
453 int disks, int chunk, int level, int layout, int data,
454 int dests, int *destfd, unsigned long long *destoffsets);
455static int child_same_size(int afd, struct mdinfo *sra, unsigned long blocks,
456 int *fds, unsigned long long *offsets,
e9e43ec3 457 unsigned long long start,
7236ee7a
N
458 int disks, int chunk, int level, int layout, int data,
459 int dests, int *destfd, unsigned long long *destoffsets);
460
4411fb17 461static int freeze_array(struct mdinfo *sra)
7236ee7a
N
462{
463 /* Try to freeze resync on this array.
464 * Return -1 if the array is busy,
465 * return 0 if this kernel doesn't support 'frozen'
466 * return 1 if it worked.
467 */
468 char buf[20];
469 if (sysfs_get_str(sra, NULL, "sync_action", buf, 20) <= 0)
470 return 0;
471 if (strcmp(buf, "idle\n") != 0 &&
472 strcmp(buf, "frozen\n") != 0)
473 return -1;
474 if (sysfs_set_str(sra, NULL, "sync_action", "frozen") < 0)
475 return 0;
476 return 1;
477}
478
4411fb17 479static void unfreeze_array(struct mdinfo *sra, int frozen)
7236ee7a
N
480{
481 /* If 'frozen' is 1, unfreeze the array */
482 if (frozen > 0)
483 sysfs_set_str(sra, NULL, "sync_action", "idle");
484}
485
4411fb17 486static void wait_reshape(struct mdinfo *sra)
7236ee7a
N
487{
488 int fd = sysfs_get_fd(sra, NULL, "sync_action");
489 char action[20];
490
491 do {
492 fd_set rfds;
493 FD_ZERO(&rfds);
494 FD_SET(fd, &rfds);
495 select(fd+1, NULL, NULL, &rfds, NULL);
496
497 if (sysfs_fd_get_str(fd, action, 20) < 0) {
498 close(fd);
499 return;
500 }
501 } while (strncmp(action, "reshape", 7) == 0);
502}
7bc71196
DW
503
504static int reshape_super(struct supertype *st, long long size, int level,
505 int layout, int chunksize, int raid_disks,
506 char *backup_file, char *dev, int verbose)
507{
508 /* nothing extra to check in the native case */
509 if (!st->ss->external)
510 return 0;
511 if (!st->ss->reshape_super ||
512 !st->ss->manage_reshape) {
513 fprintf(stderr, Name ": %s metadata does not support reshape\n",
514 st->ss->name);
515 return 1;
516 }
517
518 return st->ss->reshape_super(st, size, level, layout, chunksize,
519 raid_disks, backup_file, dev, verbose);
520}
521
522static void sync_metadata(struct supertype *st)
523{
524 if (st->ss->external) {
525 if (st->update_tail)
526 flush_metadata_updates(st);
527 else
528 st->ss->sync_metadata(st);
529 }
530}
531
532static int subarray_set_num(char *container, struct mdinfo *sra, char *name, int n)
533{
534 /* when dealing with external metadata subarrays we need to be
535 * prepared to handle EAGAIN. The kernel may need to wait for
536 * mdmon to mark the array active so the kernel can handle
537 * allocations/writeback when preparing the reshape action
538 * (md_allow_write()). We temporarily disable safe_mode_delay
539 * to close a race with the array_state going clean before the
540 * next write to raid_disks / stripe_cache_size
541 */
542 char safe[50];
543 int rc;
544
545 /* only 'raid_disks' and 'stripe_cache_size' trigger md_allow_write */
546 if (strcmp(name, "raid_disks") != 0 &&
547 strcmp(name, "stripe_cache_size") != 0)
548 return sysfs_set_num(sra, NULL, name, n);
549
550 rc = sysfs_get_str(sra, NULL, "safe_mode_delay", safe, sizeof(safe));
551 if (rc <= 0)
552 return -1;
553 sysfs_set_num(sra, NULL, "safe_mode_delay", 0);
554 rc = sysfs_set_num(sra, NULL, name, n);
555 if (rc < 0 && errno == EAGAIN) {
556 ping_monitor(container);
557 /* if we get EAGAIN here then the monitor is not active
558 * so stop trying
559 */
560 rc = sysfs_set_num(sra, NULL, name, n);
561 }
562 sysfs_set_str(sra, NULL, "safe_mode_delay", safe);
563 return rc;
564}
565
566static int reshape_container_raid_disks(char *container, int raid_disks)
567{
568 /* for each subarray switch to a raid level that can
569 * support the reshape, and set raid disks
570 */
571 struct mdstat_ent *ent, *e;
572 int changed = 0, rv = 0, err = 0;
573
574 ent = mdstat_read(1, 0);
575 if (!ent) {
576 fprintf(stderr, Name ": unable to read /proc/mdstat\n");
577 return -1;
578 }
579
580 changed = 0;
581 for (e = ent; e; e = e->next) {
582 struct mdinfo *sub;
583 unsigned int cache;
584 int level, takeover_delta = 0;
585
586 if (!is_container_member(e, container))
587 continue;
588
589 level = map_name(pers, e->level);
590 if (level == 0) {
591 sub = sysfs_read(-1, e->devnum, GET_VERSION);
592 if (!sub)
593 break;
594 /* metadata records 'orig_level' */
595 rv = sysfs_set_num(sub, NULL, "level", 4);
596 if (rv < 0) {
597 err = errno;
598 break;
599 }
600 /* we want spares to be used for capacity
601 * expansion, not rebuild
602 */
603 takeover_delta = 1;
604
605 sysfs_free(sub);
606 level = 4;
607 }
608
609 sub = NULL;
610 switch (level) {
611 default:
612 rv = -1;
613 break;
614 case 4:
615 case 5:
616 case 6:
617 sub = sysfs_read(-1, e->devnum, GET_CHUNK|GET_CACHE);
618 if (!sub)
619 break;
620 cache = (sub->array.chunk_size / 4096) * 4;
621 if (cache > sub->cache_size)
622 rv = subarray_set_num(container, sub,
623 "stripe_cache_size", cache);
624 if (rv) {
625 err = errno;
626 break;
627 }
628 /* fall through */
629 case 1:
630 if (!sub)
631 sub = sysfs_read(-1, e->devnum, GET_VERSION);
632 if (!sub)
633 break;
634
635 rv = subarray_set_num(container, sub, "raid_disks",
636 raid_disks + takeover_delta);
637 if (rv)
638 err = errno;
639 else
640 changed++;
641 break;
642 }
643 sysfs_free(sub);
644 if (rv)
645 break;
646 }
647 free_mdstat(ent);
648 if (rv) {
649 fprintf(stderr, Name
650 ": failed to initiate container reshape%s%s\n",
651 err ? ": " : "", err ? strerror(err) : "");
652 return rv;
653 }
654
655 return changed;
656}
657
658static void revert_container_raid_disks(struct supertype *st, int fd, char *container)
659{
660 /* we failed to prepare all subarrays in the container for
661 * reshape, so cancel the changes and restore the nominal raid
662 * level
663 */
664 struct mdstat_ent *ent, *e;
665
666 ent = mdstat_read(0, 0);
667 if (!ent) {
668 fprintf(stderr, Name
669 ": failed to read /proc/mdstat while aborting reshape\n");
670 return;
671 }
672
673 for (e = ent; e; e = e->next) {
674 int level_fixed = 0, disks_fixed = 0;
675 struct mdinfo *sub, prev;
676
677 if (!is_container_member(e, container))
678 continue;
679
680 st->ss->free_super(st);
681 sprintf(st->subarray, "%s", to_subarray(e, container));
682 if (st->ss->load_super(st, fd, NULL)) {
683 fprintf(stderr, Name
684 ": failed read metadata while aborting reshape\n");
685 continue;
686 }
687 st->ss->getinfo_super(st, &prev);
688
689 /* changing level might change raid_disks so we do it
690 * first and then check if raid_disks still needs fixing
691 */
692 if (map_name(pers, e->level) != prev.array.level) {
693 sub = sysfs_read(-1, e->devnum, GET_VERSION);
694 if (sub &&
695 !sysfs_set_num(sub, NULL, "level", prev.array.level))
696 level_fixed = 1;
697 sysfs_free(sub);
698 } else
699 level_fixed = 1;
700
701 sub = sysfs_read(-1, e->devnum, GET_DISKS);
702 if (sub && sub->array.raid_disks != prev.array.raid_disks) {
703 if (!subarray_set_num(container, sub, "raid_disks",
704 prev.array.raid_disks))
705 disks_fixed = 1;
706 } else if (sub)
707 disks_fixed = 1;
708 sysfs_free(sub);
709
710 if (!disks_fixed || !level_fixed)
711 fprintf(stderr, Name
712 ": failed to restore %s to a %d-disk %s array\n",
713 e->dev, prev.array.raid_disks,
714 map_num(pers, prev.array.level));
715 }
716 free_mdstat(ent);
717}
718
06b0d786 719int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
e86c9dd6 720 long long size,
19678e53 721 int level, char *layout_str, int chunksize, int raid_disks)
e86c9dd6
NB
722{
723 /* Make some changes in the shape of an array.
724 * The kernel must support the change.
7236ee7a
N
725 *
726 * There are three different changes. Each can trigger
727 * a resync or recovery so we freeze that until we have
728 * requested everything (if kernel supports freezing - 2.6.30).
729 * The steps are:
730 * - change size (i.e. component_size)
731 * - change level
732 * - change layout/chunksize/ndisks
733 *
734 * The last can require a reshape. It is different on different
735 * levels so we need to check the level before actioning it.
736 * Some times the level change needs to be requested after the
737 * reshape (e.g. raid6->raid5, raid5->raid0)
738 *
e86c9dd6 739 */
7236ee7a 740 struct mdu_array_info_s array, orig;
e86c9dd6 741 char *c;
7236ee7a 742 int rv = 0;
e86c9dd6 743 struct supertype *st;
4725bc31 744 char *subarray = NULL;
e86c9dd6 745
e86c9dd6
NB
746 int nchunk, ochunk;
747 int nlayout, olayout;
748 int ndisks, odisks;
f21e18ca 749 unsigned int ndata, odata;
7236ee7a
N
750 int orig_level = UnSet;
751 char alt_layout[40];
e86c9dd6
NB
752 int *fdlist;
753 unsigned long long *offsets;
7236ee7a 754 int d, i;
e86c9dd6
NB
755 int nrdisks;
756 int err;
7236ee7a
N
757 int frozen;
758 unsigned long a,b, blocks, stripes;
f21e18ca 759 unsigned long cache;
7236ee7a
N
760 unsigned long long array_size;
761 int changed = 0;
7bc71196
DW
762 char *container = NULL;
763 int cfd = -1;
7236ee7a 764 int done;
e86c9dd6 765
7e0f6979 766 struct mdinfo *sra;
06c7f68e 767 struct mdinfo *sd;
e86c9dd6
NB
768
769 if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) {
770 fprintf(stderr, Name ": %s is not an active md array - aborting\n",
771 devname);
772 return 1;
773 }
24d40069 774
9ce510be
N
775 if (size >= 0 &&
776 (chunksize || level!= UnSet || layout_str || raid_disks)) {
777 fprintf(stderr, Name ": cannot change component size at the same time "
778 "as other changes.\n"
779 " Change size first, then check data is intact before "
780 "making other changes.\n");
781 return 1;
782 }
783
24d40069
N
784 if (raid_disks && raid_disks < array.raid_disks && array.level > 1 &&
785 get_linux_version() < 2006032 &&
786 !check_env("MDADM_FORCE_FEWER")) {
787 fprintf(stderr, Name ": reducing the number of devices is not safe before Linux 2.6.32\n"
788 " Please use a newer kernel\n");
789 return 1;
790 }
7bc71196
DW
791
792 st = super_by_fd(fd, &subarray);
793 if (!st) {
794 fprintf(stderr, Name ": Unable to determine metadata format for %s\n", devname);
795 return 1;
796 }
797
798 /* in the external case we need to check that the requested reshape is
799 * supported, and perform an initial check that the container holds the
800 * pre-requisite spare devices (mdmon owns final validation)
801 */
802 if (st->ss->external) {
803 int container_dev;
804
805 if (subarray) {
806 container_dev = st->container_dev;
807 cfd = open_dev_excl(st->container_dev);
808 } else if (size >= 0 || layout_str != NULL || chunksize != 0 ||
809 level != UnSet) {
810 fprintf(stderr,
811 Name ": %s is a container, only 'raid-devices' can be changed\n",
812 devname);
813 return 1;
814 } else {
815 container_dev = st->devnum;
816 close(fd);
817 cfd = open_dev_excl(st->devnum);
818 fd = cfd;
819 }
820 if (cfd < 0) {
821 fprintf(stderr, Name ": Unable to open container for %s\n",
822 devname);
823 return 1;
824 }
825
826 container = devnum2devname(st->devnum);
827 if (!container) {
828 fprintf(stderr, Name ": Could not determine container name\n");
829 return 1;
830 }
831
832 if (st->ss->load_super(st, cfd, NULL)) {
833 fprintf(stderr, Name ": Cannot read superblock for %s\n",
834 devname);
835 return 1;
836 }
837
838 if (mdmon_running(container_dev))
839 st->update_tail = &st->updates;
840 }
841
7236ee7a 842 sra = sysfs_read(fd, 0, GET_LEVEL);
7bc71196
DW
843 if (sra) {
844 if (st->ss->external && st->subarray[0] == 0) {
845 array.level = LEVEL_CONTAINER;
846 sra->array.level = LEVEL_CONTAINER;
847 }
b526e52d 848 frozen = freeze_array(sra);
7bc71196 849 } else {
b526e52d
DW
850 fprintf(stderr, Name ": failed to read sysfs parameters for %s\n",
851 devname);
852 return 1;
853 }
7236ee7a
N
854 if (frozen < 0) {
855 fprintf(stderr, Name ": %s is performing resync/recovery and cannot"
856 " be reshaped\n", devname);
857 return 1;
858 }
859
7bc71196 860
7236ee7a
N
861 /* ========= set size =============== */
862 if (size >= 0 && (size == 0 || size != array.size)) {
7bc71196
DW
863 long long orig_size = array.size;
864
865 if (reshape_super(st, size, UnSet, UnSet, 0, 0, NULL, devname, !quiet)) {
866 rv = 1;
867 goto release;
868 }
869 sync_metadata(st);
7236ee7a
N
870 array.size = size;
871 if (array.size != size) {
872 /* got truncated to 32bit, write to
873 * component_size instead
874 */
875 if (sra)
876 rv = sysfs_set_num(sra, NULL,
877 "component_size", size);
878 else
879 rv = -1;
880 } else
881 rv = ioctl(fd, SET_ARRAY_INFO, &array);
882 if (rv != 0) {
39bbb392 883 int err = errno;
7bc71196
DW
884
885 /* restore metadata */
886 if (reshape_super(st, orig_size, UnSet, UnSet, 0, 0,
887 NULL, devname, !quiet) == 0)
888 sync_metadata(st);
7236ee7a 889 fprintf(stderr, Name ": Cannot set device size for %s: %s\n",
39bbb392
N
890 devname, strerror(err));
891 if (err == EBUSY &&
892 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
893 fprintf(stderr, " Bitmap must be removed before size can be changed\n");
7236ee7a
N
894 rv = 1;
895 goto release;
896 }
897 ioctl(fd, GET_ARRAY_INFO, &array);
be1cabbd 898 size = get_component_size(fd)/2;
f98841b3
N
899 if (size == 0)
900 size = array.size;
7236ee7a 901 if (!quiet)
f98841b3
N
902 fprintf(stderr, Name ": component size of %s has been set to %lluK\n",
903 devname, size);
7236ee7a 904 changed = 1;
7bc71196 905 } else if (array.level != LEVEL_CONTAINER) {
be1cabbd 906 size = get_component_size(fd)/2;
f98841b3
N
907 if (size == 0)
908 size = array.size;
7236ee7a
N
909 }
910
911 /* ======= set level =========== */
912 if (level != UnSet && level != array.level) {
913 /* Trying to change the level.
914 * We might need to change layout first and schedule a
915 * level change for later.
916 * Level changes that can happen immediately are:
917 * 0->4,5,6 1->5 4->5,6 5->1,6
918 * Level changes that need a layout change first are:
919 * 6->5,4,0 : need a -6 layout, or parity-last
920 * 5->4,0 : need parity-last
921 */
922 if ((array.level == 6 || array.level == 5) &&
923 (level == 5 || level == 4 || level == 0)) {
924 /* Don't change level yet, but choose intermediate
925 * layout
926 */
927 if (level == 5) {
928 if (layout_str == NULL)
929 switch (array.layout) {
930 case ALGORITHM_LEFT_ASYMMETRIC:
931 case ALGORITHM_LEFT_ASYMMETRIC_6:
932 case ALGORITHM_ROTATING_N_RESTART:
933 layout_str = "left-asymmetric-6";
934 break;
935 case ALGORITHM_LEFT_SYMMETRIC:
936 case ALGORITHM_LEFT_SYMMETRIC_6:
937 case ALGORITHM_ROTATING_N_CONTINUE:
938 layout_str = "left-symmetric-6";
939 break;
940 case ALGORITHM_RIGHT_ASYMMETRIC:
941 case ALGORITHM_RIGHT_ASYMMETRIC_6:
942 case ALGORITHM_ROTATING_ZERO_RESTART:
943 layout_str = "right-asymmetric-6";
944 break;
945 case ALGORITHM_RIGHT_SYMMETRIC:
946 case ALGORITHM_RIGHT_SYMMETRIC_6:
947 layout_str = "right-symmetric-6";
948 break;
949 case ALGORITHM_PARITY_0:
950 case ALGORITHM_PARITY_0_6:
951 layout_str = "parity-first-6";
952 break;
953 case ALGORITHM_PARITY_N:
954 layout_str = "parity-last";
955 break;
956 default:
957 fprintf(stderr, Name ": %s: cannot"
958 "convert layout to RAID5 equivalent\n",
959 devname);
960 rv = 1;
961 goto release;
962 }
963 else {
964 int l = map_name(r5layout, layout_str);
965 if (l == UnSet) {
966 fprintf(stderr, Name ": %s: layout '%s' not recognised\n",
967 devname, layout_str);
968 rv = 1;
969 goto release;
970 }
971 if (l != ALGORITHM_PARITY_N) {
972 /* need the -6 version */
973 char *ls = map_num(r5layout, l);
974 strcat(strcpy(alt_layout, ls),
975 "-6");
976 layout_str = alt_layout;
977 }
978 }
979 if (raid_disks)
24d40069 980 /* The final raid6->raid5 conversion
7236ee7a
N
981 * will reduce the number of disks,
982 * so now we need to aim higher
983 */
984 raid_disks++;
985 } else
986 layout_str = "parity-last";
987 } else {
7bc71196
DW
988 /* Level change is a simple takeover. In the external
989 * case we don't check with the metadata handler until
990 * we establish what the final layout will be. If the
991 * level change is disallowed we will revert to
992 * orig_level without disturbing the metadata, otherwise
993 * we will send an update.
994 */
7236ee7a 995 c = map_num(pers, level);
b5ea446a
N
996 if (c == NULL) {
997 rv = 1;/* not possible */
998 goto release;
999 }
7236ee7a
N
1000 err = sysfs_set_str(sra, NULL, "level", c);
1001 if (err) {
39bbb392 1002 err = errno;
7236ee7a
N
1003 fprintf(stderr, Name ": %s: could not set level to %s\n",
1004 devname, c);
39bbb392
N
1005 if (err == EBUSY &&
1006 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
1007 fprintf(stderr, " Bitmap must be removed before level can be changed\n");
7236ee7a
N
1008 rv = 1;
1009 goto release;
1010 }
1011 orig = array;
1012 orig_level = orig.level;
1013 ioctl(fd, GET_ARRAY_INFO, &array);
1014 if (layout_str == NULL &&
1015 orig.level == 5 && level == 6 &&
1016 array.layout != orig.layout)
1017 layout_str = map_num(r5layout, orig.layout);
1018 if (!quiet)
1019 fprintf(stderr, Name " level of %s changed to %s\n",
1020 devname, c);
1021 changed = 1;
1022 }
1023 }
1024
1025 /* ========= set shape (chunk_size / layout / ndisks) ============== */
1026 /* Check if layout change is a no-op */
7bc71196 1027 switch (array.level) {
7236ee7a 1028 case 5:
72e4a378 1029 if (layout_str && array.layout == map_name(r5layout, layout_str))
7236ee7a
N
1030 layout_str = NULL;
1031 break;
1032 case 6:
1033 if (layout_str == NULL &&
1034 ((chunksize && chunksize * 1024 != array.chunk_size) ||
1035 (raid_disks && raid_disks != array.raid_disks)) &&
1036 array.layout >= 16) {
1037 fprintf(stderr, Name
1038 ": %s has a non-standard layout. If you wish to preserve this\n"
1039 " during the reshape, please specify --layout=preserve\n"
1040 " If you want to change it, specify a layout or use --layout=normalise\n",
1041 devname);
1042 rv = 1;
1043 goto release;
1044 }
72e4a378
DW
1045 if (layout_str &&
1046 (strcmp(layout_str, "normalise") == 0 ||
1047 strcmp(layout_str, "normalize") == 0)) {
7236ee7a
N
1048 char *hyphen;
1049 strcpy(alt_layout, map_num(r6layout, array.layout));
1050 hyphen = strrchr(alt_layout, '-');
1051 if (hyphen && strcmp(hyphen, "-6") == 0) {
1052 *hyphen = 0;
1053 layout_str = alt_layout;
1054 }
1055 }
1056
72e4a378 1057 if (layout_str && array.layout == map_name(r6layout, layout_str))
7236ee7a
N
1058 layout_str = NULL;
1059 if (layout_str && strcmp(layout_str, "preserve") == 0)
1060 layout_str = NULL;
1061 break;
1062 }
1063 if (layout_str == NULL
1064 && (chunksize == 0 || chunksize*1024 == array.chunk_size)
1065 && (raid_disks == 0 || raid_disks == array.raid_disks)) {
7bc71196
DW
1066 if (reshape_super(st, -1, level, UnSet, 0, 0, NULL, devname, !quiet)) {
1067 rv = 1;
1068 goto release;
1069 }
1070 sync_metadata(st);
7236ee7a
N
1071 rv = 0;
1072 if (level != UnSet && level != array.level) {
1073 /* Looks like this level change doesn't need
1074 * a reshape after all.
1075 */
1076 c = map_num(pers, level);
1077 if (c) {
1078 rv = sysfs_set_str(sra, NULL, "level", c);
39bbb392
N
1079 if (rv) {
1080 int err = errno;
7236ee7a
N
1081 fprintf(stderr, Name ": %s: could not set level to %s\n",
1082 devname, c);
39bbb392
N
1083 if (err == EBUSY &&
1084 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
1085 fprintf(stderr, " Bitmap must be removed before level can be changed\n");
b7e734fc 1086 rv = 1;
39bbb392 1087 }
7236ee7a
N
1088 }
1089 } else if (!changed && !quiet)
1090 fprintf(stderr, Name ": %s: no change requested\n",
1091 devname);
7bc71196
DW
1092
1093 if (st->ss->external && !mdmon_running(st->container_dev) &&
1094 level > 0) {
1095 start_mdmon(st->container_dev);
1096 ping_monitor(container);
1097 }
7236ee7a
N
1098 goto release;
1099 }
1100
e86c9dd6
NB
1101 c = map_num(pers, array.level);
1102 if (c == NULL) c = "-unknown-";
7bc71196 1103 switch (array.level) {
e86c9dd6
NB
1104 default: /* raid0, linear, multipath cannot be reconfigured */
1105 fprintf(stderr, Name ": %s array %s cannot be reshaped.\n",
1106 c, devname);
7bc71196 1107 /* TODO raid0 raiddisks can be reshaped via raid4 */
7236ee7a
N
1108 rv = 1;
1109 break;
7bc71196
DW
1110 case LEVEL_CONTAINER: {
1111 int count;
1112
1113 /* double check that we are not changing anything but raid_disks */
1114 if (size >= 0 || layout_str != NULL || chunksize != 0 || level != UnSet) {
1115 fprintf(stderr,
1116 Name ": %s is a container, only 'raid-devices' can be changed\n",
1117 devname);
1118 rv = 1;
1119 goto release;
1120 }
1121
1122 st->update_tail = &st->updates;
1123 if (reshape_super(st, -1, UnSet, UnSet, 0, raid_disks,
1124 backup_file, devname, !quiet)) {
1125 rv = 1;
1126 goto release;
1127 }
1128
1129 count = reshape_container_raid_disks(container, raid_disks);
1130 if (count < 0) {
1131 revert_container_raid_disks(st, fd, container);
1132 rv = 1;
1133 goto release;
1134 } else if (count == 0) {
1135 if (!quiet)
1136 fprintf(stderr, Name
1137 ": no active subarrays to reshape\n");
1138 goto release;
1139 }
e86c9dd6 1140
7bc71196
DW
1141 if (!mdmon_running(st->devnum)) {
1142 start_mdmon(st->devnum);
1143 ping_monitor(container);
1144 }
1145 sync_metadata(st);
1146
1147 /* give mdmon a chance to allocate spares */
1148 ping_manager(container);
1149
1150 /* manage_reshape takes care of releasing the array(s) */
1151 st->ss->manage_reshape(st, backup_file);
1152 frozen = 0;
1153 goto release;
1154 }
e86c9dd6
NB
1155 case LEVEL_FAULTY: /* only 'layout' change is permitted */
1156
e86c9dd6
NB
1157 if (chunksize || raid_disks) {
1158 fprintf(stderr, Name ": %s: Cannot change chunksize or disks of a 'faulty' array\n",
1159 devname);
7236ee7a
N
1160 rv = 1;
1161 break;
e86c9dd6 1162 }
19678e53 1163 if (layout_str == NULL)
7236ee7a 1164 break; /* nothing to do.... */
e86c9dd6 1165
19678e53
N
1166 array.layout = parse_layout_faulty(layout_str);
1167 if (array.layout < 0) {
1168 fprintf(stderr, Name ": %s: layout %s not understood for 'faulty' array\n",
1169 devname, layout_str);
7236ee7a
N
1170 rv = 1;
1171 break;
19678e53 1172 }
e86c9dd6
NB
1173 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
1174 fprintf(stderr, Name ": Cannot set layout for %s: %s\n",
1175 devname, strerror(errno));
7236ee7a
N
1176 rv = 1;
1177 } else if (!quiet)
e86c9dd6 1178 printf("layout for %s set to %d\n", devname, array.layout);
7236ee7a 1179 break;
e86c9dd6 1180
7236ee7a 1181 case 1: /* only raid_disks can each be changed. */
e86c9dd6 1182
19678e53 1183 if (chunksize || layout_str != NULL) {
7236ee7a 1184 fprintf(stderr, Name ": %s: Cannot change chunk size or layout for a RAID1 array.\n",
e86c9dd6 1185 devname);
7236ee7a
N
1186 rv = 1;
1187 break;
e86c9dd6 1188 }
9860f271 1189 if (raid_disks > 0) {
7bc71196
DW
1190 if (reshape_super(st, -1, UnSet, UnSet, 0, raid_disks,
1191 NULL, devname, !quiet)) {
1192 rv = 1;
1193 goto release;
1194 }
1195 sync_metadata(st);
e86c9dd6 1196 array.raid_disks = raid_disks;
9860f271
NB
1197 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
1198 fprintf(stderr, Name ": Cannot set raid-devices for %s: %s\n",
1199 devname, strerror(errno));
7236ee7a 1200 rv = 1;
9860f271 1201 }
e86c9dd6 1202 }
7236ee7a 1203 break;
e86c9dd6
NB
1204
1205 case 4:
1206 case 5:
1207 case 6:
1686dc25 1208
7236ee7a
N
1209 /*
1210 * layout/chunksize/raid_disks can be changed
e86c9dd6 1211 * though the kernel may not support it all.
e86c9dd6 1212 */
4725bc31
N
1213 if (subarray) {
1214 fprintf(stderr, Name ": Cannot reshape subarrays yet\n");
1215 break;
1216 }
7236ee7a
N
1217
1218 /*
1219 * There are three possibilities.
1220 * 1/ The array will shrink.
1221 * We need to ensure the reshape will pause before reaching
1222 * the 'critical section'. We also need to fork and wait for
1223 * that to happen. When it does we
1224 * suspend/backup/complete/unfreeze
1225 *
1226 * 2/ The array will not change size.
1227 * This requires that we keep a backup of a sliding window
1228 * so that we can restore data after a crash. So we need
1229 * to fork and monitor progress.
1230 *
1231 * 3/ The array will grow. This is relatively easy.
e86c9dd6
NB
1232 * However the kernel's restripe routines will cheerfully
1233 * overwrite some early data before it is safe. So we
1234 * need to make a backup of the early parts of the array
1235 * and be ready to restore it if rebuild aborts very early.
1236 *
7236ee7a
N
1237 * We backup data by writing it to one spare, or to a
1238 * file which was given on command line.
e86c9dd6 1239 *
7236ee7a 1240 * [FOLLOWING IS OLD AND PARTLY WRONG]
e86c9dd6
NB
1241 * So: we enumerate the devices in the array and
1242 * make sure we can open all of them.
1243 * Then we freeze the early part of the array and
1244 * backup to the various spares.
1245 * Then we request changes and start the reshape.
1246 * Monitor progress until it has passed the danger zone.
1247 * and finally invalidate the copied data and unfreeze the
1248 * start of the array.
1249 *
7236ee7a
N
1250 * In each case, we first make sure that storage is available
1251 * for the required backup.
1252 * Then we:
1253 * - request the shape change.
1254 * - for to handle backup etc.
e86c9dd6 1255 */
e86c9dd6
NB
1256 nchunk = ochunk = array.chunk_size;
1257 nlayout = olayout = array.layout;
1258 ndisks = odisks = array.raid_disks;
1259
7236ee7a
N
1260 if (chunksize) {
1261 nchunk = chunksize * 1024;
f98841b3
N
1262 if (size % chunksize) {
1263 fprintf(stderr, Name ": component size %lluK is not"
7236ee7a 1264 " a multiple of chunksize %dK\n",
f98841b3 1265 size, chunksize);
7236ee7a
N
1266 break;
1267 }
1268 }
19678e53 1269 if (layout_str != NULL)
7236ee7a 1270 switch(array.level) {
19678e53
N
1271 case 4: /* ignore layout */
1272 break;
1273 case 5:
1274 nlayout = map_name(r5layout, layout_str);
1275 if (nlayout == UnSet) {
1276 fprintf(stderr, Name ": layout %s not understood for raid5.\n",
1277 layout_str);
b5ea446a
N
1278 rv = 1;
1279 goto release;
19678e53
N
1280 }
1281 break;
1282
1283 case 6:
1284 nlayout = map_name(r6layout, layout_str);
1285 if (nlayout == UnSet) {
1286 fprintf(stderr, Name ": layout %s not understood for raid6.\n",
1287 layout_str);
b5ea446a
N
1288 rv = 1;
1289 goto release;
19678e53
N
1290 }
1291 break;
1292 }
e86c9dd6
NB
1293 if (raid_disks) ndisks = raid_disks;
1294
1295 odata = odisks-1;
e86c9dd6 1296 ndata = ndisks-1;
7236ee7a
N
1297 if (array.level == 6) {
1298 odata--; /* number of data disks */
1299 ndata--;
e86c9dd6 1300 }
7236ee7a 1301
d2505cff
N
1302 if (odata == ndata &&
1303 get_linux_version() < 2006032) {
1304 fprintf(stderr, Name ": in-place reshape is not safe before 2.6.32, sorry.\n");
1305 break;
1306 }
1307
7236ee7a 1308 /* Check that we can hold all the data */
7236ee7a 1309 get_dev_size(fd, NULL, &array_size);
f21e18ca 1310 if (ndata * (unsigned long long)size < (array_size/1024)) {
7236ee7a
N
1311 fprintf(stderr, Name ": this change will reduce the size of the array.\n"
1312 " use --grow --array-size first to truncate array.\n"
1313 " e.g. mdadm --grow %s --array-size %llu\n",
f98841b3 1314 devname, ndata * size);
7236ee7a
N
1315 rv = 1;
1316 break;
e86c9dd6 1317 }
7236ee7a
N
1318
1319 /* So how much do we need to backup.
1320 * We need an amount of data which is both a whole number of
1321 * old stripes and a whole number of new stripes.
1322 * So LCM for (chunksize*datadisks).
e86c9dd6 1323 */
200871ad
N
1324 a = (ochunk/512) * odata;
1325 b = (nchunk/512) * ndata;
7236ee7a
N
1326 /* Find GCD */
1327 while (a != b) {
1328 if (a < b)
1329 b -= a;
1330 if (b < a)
1331 a -= b;
e86c9dd6 1332 }
7236ee7a 1333 /* LCM == product / GCD */
200871ad 1334 blocks = (ochunk/512) * (nchunk/512) * odata * ndata / a;
7236ee7a 1335
e380d3be
N
1336 sysfs_free(sra);
1337 sra = sysfs_read(fd, 0,
1338 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|
1339 GET_CACHE);
1340
c03ef02d
N
1341 if (!sra) {
1342 fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n",
1343 devname);
1344 rv = 1;
1345 break;
1346 }
1347
eba71529
N
1348 if (ndata == odata) {
1349 /* Make 'blocks' bigger for better throughput, but
1350 * not so big that we reject it below.
1b13faf7 1351 * Try for 16 megabytes
eba71529 1352 */
1b13faf7
N
1353 while (blocks * 32 < sra->component_size &&
1354 blocks < 16*1024*2)
1355 blocks *= 2;
eba71529 1356 } else
7236ee7a
N
1357 fprintf(stderr, Name ": Need to backup %luK of critical "
1358 "section..\n", blocks/2);
e86c9dd6 1359
7236ee7a 1360 if (blocks >= sra->component_size/2) {
e86c9dd6
NB
1361 fprintf(stderr, Name ": %s: Something wrong - reshape aborted\n",
1362 devname);
7236ee7a
N
1363 rv = 1;
1364 break;
353632d9 1365 }
925211e3 1366 nrdisks = array.raid_disks + sra->array.spare_disks;
e86c9dd6
NB
1367 /* Now we need to open all these devices so we can read/write.
1368 */
06b0d786
NB
1369 fdlist = malloc((1+nrdisks) * sizeof(int));
1370 offsets = malloc((1+nrdisks) * sizeof(offsets[0]));
e86c9dd6
NB
1371 if (!fdlist || !offsets) {
1372 fprintf(stderr, Name ": malloc failed: grow aborted\n");
7236ee7a
N
1373 rv = 1;
1374 break;
e86c9dd6 1375 }
06b0d786 1376 for (d=0; d <= nrdisks; d++)
e86c9dd6
NB
1377 fdlist[d] = -1;
1378 d = array.raid_disks;
1379 for (sd = sra->devs; sd; sd=sd->next) {
06c7f68e 1380 if (sd->disk.state & (1<<MD_DISK_FAULTY))
e86c9dd6 1381 continue;
06c7f68e
NB
1382 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
1383 char *dn = map_dev(sd->disk.major,
1384 sd->disk.minor, 1);
1385 fdlist[sd->disk.raid_disk]
1386 = dev_open(dn, O_RDONLY);
7236ee7a 1387 offsets[sd->disk.raid_disk] = sd->data_offset*512;
06c7f68e 1388 if (fdlist[sd->disk.raid_disk] < 0) {
e86c9dd6 1389 fprintf(stderr, Name ": %s: cannot open component %s\n",
e81cdd9f 1390 devname, dn?dn:"-unknown-");
7236ee7a
N
1391 rv = 1;
1392 goto release;
e86c9dd6 1393 }
7236ee7a 1394 } else if (backup_file == NULL) {
e86c9dd6 1395 /* spare */
06c7f68e
NB
1396 char *dn = map_dev(sd->disk.major,
1397 sd->disk.minor, 1);
16c6fa80 1398 fdlist[d] = dev_open(dn, O_RDWR);
ff94fb86 1399 offsets[d] = (sd->data_offset + sra->component_size - blocks - 8)*512;
e86c9dd6
NB
1400 if (fdlist[d]<0) {
1401 fprintf(stderr, Name ": %s: cannot open component %s\n",
e81cdd9f 1402 devname, dn?dn:"-unknown");
7236ee7a
N
1403 rv = 1;
1404 goto release;
e86c9dd6
NB
1405 }
1406 d++;
1407 }
1408 }
7236ee7a 1409 if (backup_file == NULL) {
7bc71196
DW
1410 if (st->ss->external && !st->ss->manage_reshape) {
1411 fprintf(stderr, Name ": %s Grow operation not supported by %s metadata\n",
1412 devname, st->ss->name);
1413 rv = 1;
1414 break;
1415 }
7236ee7a
N
1416 if (ndata <= odata) {
1417 fprintf(stderr, Name ": %s: Cannot grow - need backup-file\n",
1418 devname);
1419 rv = 1;
1420 break;
1421 } else if (sra->array.spare_disks == 0) {
1422 fprintf(stderr, Name ": %s: Cannot grow - need a spare or "
1423 "backup-file to backup critical section\n",
1424 devname);
1425 rv = 1;
1426 break;
1427 }
1428 if (d == array.raid_disks) {
1429 fprintf(stderr, Name ": %s: No spare device for backup\n",
1430 devname);
1431 rv = 1;
1432 break;
e86c9dd6 1433 }
7236ee7a
N
1434 } else {
1435 /* need to check backup file is large enough */
1436 char buf[512];
1437 fdlist[d] = open(backup_file, O_RDWR|O_CREAT|O_EXCL,
1438 S_IRUSR | S_IWUSR);
1439 offsets[d] = 8 * 512;
06b0d786
NB
1440 if (fdlist[d] < 0) {
1441 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
1442 devname, backup_file, strerror(errno));
7236ee7a
N
1443 rv = 1;
1444 break;
1445 }
1446 memset(buf, 0, 512);
f21e18ca 1447 for (i=0; i < (signed)blocks + 1 ; i++) {
7236ee7a
N
1448 if (write(fdlist[d], buf, 512) != 512) {
1449 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
1450 devname, backup_file, strerror(errno));
1451 rv = 1;
1452 break;
1453 }
1454 }
1455 if (fsync(fdlist[d]) != 0) {
1456 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
1457 devname, backup_file, strerror(errno));
1458 rv = 1;
1459 break;
06b0d786 1460 }
06b0d786 1461 d++;
06b0d786 1462 }
7236ee7a 1463
7bc71196
DW
1464 /* check that the operation is supported by the metadata */
1465 if (reshape_super(st, -1, level, nlayout, nchunk, ndisks,
1466 backup_file, devname, !quiet)) {
1467 rv = 1;
1468 break;
1469 }
1470
7236ee7a
N
1471 /* lastly, check that the internal stripe cache is
1472 * large enough, or it won't work.
1473 */
1474
1475 cache = (nchunk < ochunk) ? ochunk : nchunk;
1476 cache = cache * 4 / 4096;
1b13faf7
N
1477 if (cache < blocks / 8 / odisks + 16)
1478 /* Make it big enough to hold 'blocks' */
1479 cache = blocks / 8 / odisks + 16;
7236ee7a
N
1480 if (sra->cache_size < cache)
1481 sysfs_set_num(sra, NULL, "stripe_cache_size",
1482 cache+1);
1483 /* Right, everything seems fine. Let's kick things off.
1484 * If only changing raid_disks, use ioctl, else use
1485 * sysfs.
1486 */
7bc71196 1487 sync_metadata(st);
7236ee7a
N
1488 if (ochunk == nchunk && olayout == nlayout) {
1489 array.raid_disks = ndisks;
1490 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
39bbb392 1491 int err = errno;
7236ee7a
N
1492 rv = 1;
1493 fprintf(stderr, Name ": Cannot set device shape for %s: %s\n",
1494 devname, strerror(errno));
1495 if (ndisks < odisks &&
1496 get_linux_version() < 2006030)
1497 fprintf(stderr, Name ": linux 2.6.30 or later required\n");
39bbb392
N
1498 if (err == EBUSY &&
1499 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
1500 fprintf(stderr, " Bitmap must be removed before shape can be changed\n");
7236ee7a
N
1501
1502 break;
1503 }
1504 } else {
1505 /* set them all just in case some old 'new_*' value
1506 * persists from some earlier problem
1507 */
53f50353
N
1508 int err = err; /* only used if rv==1, and always set if
1509 * rv==1, so initialisation not needed,
1510 * despite gcc warning
1511 */
7236ee7a 1512 if (sysfs_set_num(sra, NULL, "chunk_size", nchunk) < 0)
39bbb392
N
1513 rv = 1, err = errno;
1514 if (!rv && sysfs_set_num(sra, NULL, "layout", nlayout) < 0)
1515 rv = 1, err = errno;
1516 if (!rv && sysfs_set_num(sra, NULL, "raid_disks", ndisks) < 0)
1517 rv = 1, err = errno;
7236ee7a
N
1518 if (rv) {
1519 fprintf(stderr, Name ": Cannot set device shape for %s\n",
1520 devname);
1521 if (get_linux_version() < 2006030)
1522 fprintf(stderr, Name ": linux 2.6.30 or later required\n");
39bbb392
N
1523 if (err == EBUSY &&
1524 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
1525 fprintf(stderr, " Bitmap must be removed before shape can be changed\n");
7236ee7a
N
1526 break;
1527 }
e86c9dd6
NB
1528 }
1529
7236ee7a
N
1530 if (ndisks == 2 && odisks == 2) {
1531 /* No reshape is needed in this trivial case */
1532 rv = 0;
1533 break;
1534 }
1535
7bc71196
DW
1536 if (st->ss->external) {
1537 /* metadata handler takes it from here */
1538 ping_manager(container);
1539 st->ss->manage_reshape(st, backup_file);
1540 frozen = 0;
1541 break;
1542 }
1543
7236ee7a
N
1544 /* set up the backup-super-block. This requires the
1545 * uuid from the array.
1546 */
e86c9dd6 1547 /* Find a superblock */
7236ee7a
N
1548 for (sd = sra->devs; sd; sd = sd->next) {
1549 char *dn;
1550 int devfd;
1551 int ok;
1552 if (sd->disk.state & (1<<MD_DISK_FAULTY))
1553 continue;
1554 dn = map_dev(sd->disk.major, sd->disk.minor, 1);
1555 devfd = dev_open(dn, O_RDONLY);
1556 if (devfd < 0)
1557 continue;
1558 ok = st->ss->load_super(st, devfd, NULL);
1559 close(devfd);
1560 if (ok >= 0)
1561 break;
1562 }
1563 if (!sd) {
e86c9dd6
NB
1564 fprintf(stderr, Name ": %s: Cannot find a superblock\n",
1565 devname);
7236ee7a
N
1566 rv = 1;
1567 break;
e86c9dd6
NB
1568 }
1569
7236ee7a 1570 memset(&bsb, 0, 512);
2efedc7b 1571 memcpy(bsb.magic, "md_backup_data-1", 16);
3da92f27 1572 st->ss->uuid_from_super(st, (int*)&bsb.set_uuid);
2efedc7b 1573 bsb.mtime = __cpu_to_le64(time(0));
7236ee7a
N
1574 bsb.devstart2 = blocks;
1575 stripes = blocks / (ochunk/512) / odata;
1576 /* Now we just need to kick off the reshape and watch, while
1577 * handling backups of the data...
1578 * This is all done by a forked background process.
2efedc7b 1579 */
7236ee7a
N
1580 switch(fork()) {
1581 case 0:
1582 close(fd);
1583 if (check_env("MDADM_GROW_VERIFY"))
1584 fd = open(devname, O_RDONLY | O_DIRECT);
1585 else
1586 fd = -1;
1587 mlockall(MCL_FUTURE);
1588
1589 if (odata < ndata)
1590 done = child_grow(fd, sra, stripes,
1591 fdlist, offsets,
1592 odisks, ochunk, array.level, olayout, odata,
1593 d - odisks, fdlist+odisks, offsets+odisks);
1594 else if (odata > ndata)
1595 done = child_shrink(fd, sra, stripes,
1596 fdlist, offsets,
1597 odisks, ochunk, array.level, olayout, odata,
1598 d - odisks, fdlist+odisks, offsets+odisks);
1599 else
1600 done = child_same_size(fd, sra, stripes,
1601 fdlist, offsets,
e9e43ec3 1602 0,
7236ee7a
N
1603 odisks, ochunk, array.level, olayout, odata,
1604 d - odisks, fdlist+odisks, offsets+odisks);
1605 if (backup_file && done)
1606 unlink(backup_file);
1607 if (level != UnSet && level != array.level) {
1608 /* We need to wait for the reshape to finish
1609 * (which will have happened unless odata < ndata)
1610 * and then set the level
758d3a8e 1611 */
7236ee7a
N
1612
1613 c = map_num(pers, level);
1614 if (c == NULL)
1615 exit(0);/* not possible */
1616
1617 if (odata < ndata)
1618 wait_reshape(sra);
1619 err = sysfs_set_str(sra, NULL, "level", c);
1620 if (err)
1621 fprintf(stderr, Name ": %s: could not set level to %s\n",
1622 devname, c);
758d3a8e 1623 }
7236ee7a
N
1624 exit(0);
1625 case -1:
1626 fprintf(stderr, Name ": Cannot run child to monitor reshape: %s\n",
1627 strerror(errno));
1628 rv = 1;
1629 break;
1630 default:
1631 /* The child will take care of unfreezing the array */
1632 frozen = 0;
1633 break;
e86c9dd6 1634 }
7236ee7a 1635 break;
e86c9dd6 1636
7236ee7a 1637 }
e86c9dd6 1638
7236ee7a
N
1639 release:
1640 if (rv && orig_level != UnSet && sra) {
1641 c = map_num(pers, orig_level);
1642 if (c && sysfs_set_str(sra, NULL, "level", c) == 0)
1643 fprintf(stderr, Name ": aborting level change\n");
1644 }
1645 if (sra)
1646 unfreeze_array(sra, frozen);
1647 return rv;
1648}
e86c9dd6 1649
7236ee7a
N
1650/*
1651 * We run a child process in the background which performs the following
1652 * steps:
1653 * - wait for resync to reach a certain point
1654 * - suspend io to the following section
1655 * - backup that section
1656 * - allow resync to proceed further
1657 * - resume io
1658 * - discard the backup.
1659 *
1660 * When are combined in slightly different ways in the three cases.
1661 * Grow:
1662 * - suspend/backup/allow/wait/resume/discard
1663 * Shrink:
1664 * - allow/wait/suspend/backup/allow/wait/resume/discard
1665 * same-size:
1666 * - wait/resume/discard/suspend/backup/allow
1667 *
1668 * suspend/backup/allow always come together
1669 * wait/resume/discard do too.
1670 * For the same-size case we have two backups to improve flow.
1671 *
1672 */
e86c9dd6 1673
fcf57625 1674/* FIXME return status is never checked */
4411fb17 1675static int grow_backup(struct mdinfo *sra,
7236ee7a
N
1676 unsigned long long offset, /* per device */
1677 unsigned long stripes, /* per device */
1678 int *sources, unsigned long long *offsets,
1679 int disks, int chunk, int level, int layout,
1680 int dests, int *destfd, unsigned long long *destoffsets,
d4445387 1681 int part, int *degraded,
7236ee7a
N
1682 char *buf)
1683{
1684 /* Backup 'blocks' sectors at 'offset' on each device of the array,
1685 * to storage 'destfd' (offset 'destoffsets'), after first
1686 * suspending IO. Then allow resync to continue
1687 * over the suspended section.
1688 * Use part 'part' of the backup-super-block.
1689 */
1690 int odata = disks;
1691 int rv = 0;
1692 int i;
f21e18ca
N
1693 unsigned long long ll;
1694 int new_degraded;
7236ee7a
N
1695 //printf("offset %llu\n", offset);
1696 if (level >= 4)
1697 odata--;
1698 if (level == 6)
1699 odata--;
200871ad 1700 sysfs_set_num(sra, NULL, "suspend_hi", (offset + stripes * (chunk/512)) * odata);
d4445387 1701 /* Check that array hasn't become degraded, else we might backup the wrong data */
f21e18ca
N
1702 sysfs_get_ll(sra, NULL, "degraded", &ll);
1703 new_degraded = (int)ll;
d4445387
N
1704 if (new_degraded != *degraded) {
1705 /* check each device to ensure it is still working */
1706 struct mdinfo *sd;
1707 for (sd = sra->devs ; sd ; sd = sd->next) {
1708 if (sd->disk.state & (1<<MD_DISK_FAULTY))
1709 continue;
1710 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
1711 char sbuf[20];
1712 if (sysfs_get_str(sra, sd, "state", sbuf, 20) < 0 ||
1713 strstr(sbuf, "faulty") ||
1714 strstr(sbuf, "in_sync") == NULL) {
1715 /* this device is dead */
1716 sd->disk.state = (1<<MD_DISK_FAULTY);
1717 if (sd->disk.raid_disk >= 0 &&
1718 sources[sd->disk.raid_disk] >= 0) {
1719 close(sources[sd->disk.raid_disk]);
1720 sources[sd->disk.raid_disk] = -1;
1721 }
1722 }
1723 }
1724 }
1725 *degraded = new_degraded;
1726 }
7236ee7a
N
1727 if (part) {
1728 bsb.arraystart2 = __cpu_to_le64(offset * odata);
200871ad 1729 bsb.length2 = __cpu_to_le64(stripes * (chunk/512) * odata);
7236ee7a
N
1730 } else {
1731 bsb.arraystart = __cpu_to_le64(offset * odata);
200871ad 1732 bsb.length = __cpu_to_le64(stripes * (chunk/512) * odata);
7236ee7a
N
1733 }
1734 if (part)
1735 bsb.magic[15] = '2';
1736 for (i = 0; i < dests; i++)
1737 if (part)
1738 lseek64(destfd[i], destoffsets[i] + __le64_to_cpu(bsb.devstart2)*512, 0);
1739 else
1740 lseek64(destfd[i], destoffsets[i], 0);
1741
1742 rv = save_stripes(sources, offsets,
1743 disks, chunk, level, layout,
1744 dests, destfd,
1745 offset*512*odata, stripes * chunk * odata,
1746 buf);
1747
1748 if (rv)
1749 return rv;
97396422 1750 bsb.mtime = __cpu_to_le64(time(0));
7236ee7a
N
1751 for (i = 0; i < dests; i++) {
1752 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
1753
1754 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
1755 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
1756 bsb.sb_csum2 = bsb_csum((char*)&bsb,
1757 ((char*)&bsb.sb_csum2)-((char*)&bsb));
1758
72044953 1759 rv = -1;
f21e18ca
N
1760 if ((unsigned long long)lseek64(destfd[i], destoffsets[i] - 4096, 0)
1761 != destoffsets[i] - 4096)
72044953
N
1762 break;
1763 if (write(destfd[i], &bsb, 512) != 512)
1764 break;
ff94fb86 1765 if (destoffsets[i] > 4096) {
f21e18ca 1766 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]+stripes*chunk*odata, 0) !=
a847575a 1767 destoffsets[i]+stripes*chunk*odata)
72044953
N
1768 break;
1769 if (write(destfd[i], &bsb, 512) != 512)
1770 break;
ff94fb86 1771 }
7236ee7a 1772 fsync(destfd[i]);
72044953 1773 rv = 0;
7236ee7a 1774 }
2efedc7b 1775
fcf57625 1776 return rv;
7236ee7a
N
1777}
1778
1779/* in 2.6.30, the value reported by sync_completed can be
1780 * less that it should be by one stripe.
1781 * This only happens when reshape hits sync_max and pauses.
1782 * So allow wait_backup to either extent sync_max further
1783 * than strictly necessary, or return before the
1784 * sync has got quite as far as we would really like.
1785 * This is what 'blocks2' is for.
1786 * The various caller give appropriate values so that
1787 * every works.
1788 */
fcf57625 1789/* FIXME return value is often ignored */
4411fb17 1790static int wait_backup(struct mdinfo *sra,
7236ee7a
N
1791 unsigned long long offset, /* per device */
1792 unsigned long long blocks, /* per device */
1793 unsigned long long blocks2, /* per device - hack */
1794 int dests, int *destfd, unsigned long long *destoffsets,
1795 int part)
1796{
1797 /* Wait for resync to pass the section that was backed up
1798 * then erase the backup and allow IO
1799 */
1800 int fd = sysfs_get_fd(sra, NULL, "sync_completed");
1801 unsigned long long completed;
1802 int i;
fcf57625 1803 int rv;
7236ee7a
N
1804
1805 if (fd < 0)
1806 return -1;
1807 sysfs_set_num(sra, NULL, "sync_max", offset + blocks + blocks2);
1808 if (offset == 0)
1809 sysfs_set_str(sra, NULL, "sync_action", "reshape");
1810 do {
1811 char action[20];
1812 fd_set rfds;
1813 FD_ZERO(&rfds);
1814 FD_SET(fd, &rfds);
1815 select(fd+1, NULL, NULL, &rfds, NULL);
1816 if (sysfs_fd_get_ll(fd, &completed) < 0) {
1817 close(fd);
1818 return -1;
e86c9dd6 1819 }
7236ee7a
N
1820 if (sysfs_get_str(sra, NULL, "sync_action",
1821 action, 20) > 0 &&
1822 strncmp(action, "reshape", 7) != 0)
1823 break;
1824 } while (completed < offset + blocks);
1825 close(fd);
1826
1827 if (part) {
1828 bsb.arraystart2 = __cpu_to_le64(0);
1829 bsb.length2 = __cpu_to_le64(0);
1830 } else {
1831 bsb.arraystart = __cpu_to_le64(0);
1832 bsb.length = __cpu_to_le64(0);
1833 }
97396422 1834 bsb.mtime = __cpu_to_le64(time(0));
fcf57625 1835 rv = 0;
7236ee7a
N
1836 for (i = 0; i < dests; i++) {
1837 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
1838 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
1839 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
1840 bsb.sb_csum2 = bsb_csum((char*)&bsb,
1841 ((char*)&bsb.sb_csum2)-((char*)&bsb));
f21e18ca 1842 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]-4096, 0) !=
a847575a 1843 destoffsets[i]-4096)
72044953
N
1844 rv = -1;
1845 if (rv == 0 &&
1846 write(destfd[i], &bsb, 512) != 512)
1847 rv = -1;
7236ee7a
N
1848 fsync(destfd[i]);
1849 }
fcf57625 1850 return rv;
7236ee7a 1851}
e86c9dd6 1852
7236ee7a
N
1853static void fail(char *msg)
1854{
fcf57625 1855 int rv;
72044953
N
1856 rv = (write(2, msg, strlen(msg)) != (int)strlen(msg));
1857 rv |= (write(2, "\n", 1) != 1);
fcf57625 1858 exit(rv ? 1 : 2);
7236ee7a
N
1859}
1860
1861static char *abuf, *bbuf;
f21e18ca 1862static unsigned long long abuflen;
7236ee7a
N
1863static void validate(int afd, int bfd, unsigned long long offset)
1864{
1865 /* check that the data in the backup against the array.
1866 * This is only used for regression testing and should not
1867 * be used while the array is active
1868 */
7236ee7a
N
1869 if (afd < 0)
1870 return;
1871 lseek64(bfd, offset - 4096, 0);
1872 if (read(bfd, &bsb2, 512) != 512)
1873 fail("cannot read bsb");
1874 if (bsb2.sb_csum != bsb_csum((char*)&bsb2,
1875 ((char*)&bsb2.sb_csum)-((char*)&bsb2)))
1876 fail("first csum bad");
1877 if (memcmp(bsb2.magic, "md_backup_data", 14) != 0)
1878 fail("magic is bad");
1879 if (memcmp(bsb2.magic, "md_backup_data-2", 16) == 0 &&
1880 bsb2.sb_csum2 != bsb_csum((char*)&bsb2,
1881 ((char*)&bsb2.sb_csum2)-((char*)&bsb2)))
1882 fail("second csum bad");
1883
1884 if (__le64_to_cpu(bsb2.devstart)*512 != offset)
1885 fail("devstart is wrong");
1886
1887 if (bsb2.length) {
1888 unsigned long long len = __le64_to_cpu(bsb2.length)*512;
1889
1890 if (abuflen < len) {
1891 free(abuf);
1892 free(bbuf);
1893 abuflen = len;
fcf57625
N
1894 if (posix_memalign((void**)&abuf, 4096, abuflen) ||
1895 posix_memalign((void**)&bbuf, 4096, abuflen)) {
1896 abuflen = 0;
1897 /* just stop validating on mem-alloc failure */
1898 return;
1899 }
e86c9dd6 1900 }
48924014 1901
7236ee7a 1902 lseek64(bfd, offset, 0);
f21e18ca 1903 if ((unsigned long long)read(bfd, bbuf, len) != len) {
080fd005 1904 //printf("len %llu\n", len);
7236ee7a
N
1905 fail("read first backup failed");
1906 }
1907 lseek64(afd, __le64_to_cpu(bsb2.arraystart)*512, 0);
f21e18ca 1908 if ((unsigned long long)read(afd, abuf, len) != len)
7236ee7a
N
1909 fail("read first from array failed");
1910 if (memcmp(bbuf, abuf, len) != 0) {
080fd005 1911 #if 0
7236ee7a
N
1912 int i;
1913 printf("offset=%llu len=%llu\n",
080fd005 1914 (unsigned long long)__le64_to_cpu(bsb2.arraystart)*512, len);
7236ee7a
N
1915 for (i=0; i<len; i++)
1916 if (bbuf[i] != abuf[i]) {
1917 printf("first diff byte %d\n", i);
93ecfa01 1918 break;
7236ee7a 1919 }
080fd005 1920 #endif
7236ee7a 1921 fail("data1 compare failed");
e86c9dd6 1922 }
7236ee7a
N
1923 }
1924 if (bsb2.length2) {
1925 unsigned long long len = __le64_to_cpu(bsb2.length2)*512;
1926
1927 if (abuflen < len) {
1928 free(abuf);
1929 free(bbuf);
1930 abuflen = len;
1931 abuf = malloc(abuflen);
1932 bbuf = malloc(abuflen);
e86c9dd6
NB
1933 }
1934
7236ee7a 1935 lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0);
f21e18ca 1936 if ((unsigned long long)read(bfd, bbuf, len) != len)
7236ee7a
N
1937 fail("read second backup failed");
1938 lseek64(afd, __le64_to_cpu(bsb2.arraystart2)*512, 0);
f21e18ca 1939 if ((unsigned long long)read(afd, abuf, len) != len)
7236ee7a
N
1940 fail("read second from array failed");
1941 if (memcmp(bbuf, abuf, len) != 0)
1942 fail("data2 compare failed");
e86c9dd6 1943 }
7236ee7a 1944}
e86c9dd6 1945
7236ee7a
N
1946static int child_grow(int afd, struct mdinfo *sra, unsigned long stripes,
1947 int *fds, unsigned long long *offsets,
1948 int disks, int chunk, int level, int layout, int data,
1949 int dests, int *destfd, unsigned long long *destoffsets)
1950{
1951 char *buf;
d4445387 1952 int degraded = 0;
e86c9dd6 1953
fcf57625
N
1954 if (posix_memalign((void**)&buf, 4096, disks * chunk))
1955 /* Don't start the 'reshape' */
1956 return 0;
7236ee7a
N
1957 sysfs_set_num(sra, NULL, "suspend_hi", 0);
1958 sysfs_set_num(sra, NULL, "suspend_lo", 0);
1959 grow_backup(sra, 0, stripes,
1960 fds, offsets, disks, chunk, level, layout,
1961 dests, destfd, destoffsets,
d4445387 1962 0, &degraded, buf);
7236ee7a 1963 validate(afd, destfd[0], destoffsets[0]);
200871ad 1964 wait_backup(sra, 0, stripes * (chunk / 512), stripes * (chunk / 512),
725cac4c
N
1965 dests, destfd, destoffsets,
1966 0);
200871ad 1967 sysfs_set_num(sra, NULL, "suspend_lo", (stripes * (chunk/512)) * data);
7236ee7a
N
1968 free(buf);
1969 /* FIXME this should probably be numeric */
1970 sysfs_set_str(sra, NULL, "sync_max", "max");
e86c9dd6 1971 return 1;
7236ee7a 1972}
e86c9dd6 1973
7236ee7a
N
1974static int child_shrink(int afd, struct mdinfo *sra, unsigned long stripes,
1975 int *fds, unsigned long long *offsets,
1976 int disks, int chunk, int level, int layout, int data,
1977 int dests, int *destfd, unsigned long long *destoffsets)
1978{
1979 char *buf;
1980 unsigned long long start;
1981 int rv;
d4445387 1982 int degraded = 0;
7236ee7a 1983
fcf57625
N
1984 if (posix_memalign((void**)&buf, 4096, disks * chunk))
1985 return 0;
200871ad 1986 start = sra->component_size - stripes * (chunk/512);
7236ee7a
N
1987 sysfs_set_num(sra, NULL, "sync_max", start);
1988 sysfs_set_str(sra, NULL, "sync_action", "reshape");
1989 sysfs_set_num(sra, NULL, "suspend_lo", 0);
1990 sysfs_set_num(sra, NULL, "suspend_hi", 0);
200871ad 1991 rv = wait_backup(sra, 0, start - stripes * (chunk/512), stripes * (chunk/512),
7236ee7a
N
1992 dests, destfd, destoffsets, 0);
1993 if (rv < 0)
1994 return 0;
1995 grow_backup(sra, 0, stripes,
1996 fds, offsets,
1997 disks, chunk, level, layout,
1998 dests, destfd, destoffsets,
d4445387 1999 0, &degraded, buf);
7236ee7a 2000 validate(afd, destfd[0], destoffsets[0]);
200871ad 2001 wait_backup(sra, start, stripes*(chunk/512), 0,
725cac4c 2002 dests, destfd, destoffsets, 0);
200871ad 2003 sysfs_set_num(sra, NULL, "suspend_lo", (stripes * (chunk/512)) * data);
7236ee7a
N
2004 free(buf);
2005 /* FIXME this should probably be numeric */
2006 sysfs_set_str(sra, NULL, "sync_max", "max");
2007 return 1;
2008}
2009
2010static int child_same_size(int afd, struct mdinfo *sra, unsigned long stripes,
2011 int *fds, unsigned long long *offsets,
e9e43ec3 2012 unsigned long long start,
7236ee7a
N
2013 int disks, int chunk, int level, int layout, int data,
2014 int dests, int *destfd, unsigned long long *destoffsets)
2015{
e9e43ec3 2016 unsigned long long size;
7236ee7a
N
2017 unsigned long tailstripes = stripes;
2018 int part;
2019 char *buf;
2020 unsigned long long speed;
d4445387 2021 int degraded = 0;
7236ee7a
N
2022
2023
fcf57625
N
2024 if (posix_memalign((void**)&buf, 4096, disks * chunk))
2025 return 0;
7236ee7a
N
2026
2027 sysfs_set_num(sra, NULL, "suspend_lo", 0);
2028 sysfs_set_num(sra, NULL, "suspend_hi", 0);
2029
2030 sysfs_get_ll(sra, NULL, "sync_speed_min", &speed);
2031 sysfs_set_num(sra, NULL, "sync_speed_min", 200000);
2032
e9e43ec3 2033 grow_backup(sra, start, stripes,
7236ee7a
N
2034 fds, offsets,
2035 disks, chunk, level, layout,
2036 dests, destfd, destoffsets,
d4445387 2037 0, &degraded, buf);
200871ad 2038 grow_backup(sra, (start + stripes) * (chunk/512), stripes,
7236ee7a
N
2039 fds, offsets,
2040 disks, chunk, level, layout,
2041 dests, destfd, destoffsets,
d4445387 2042 1, &degraded, buf);
7236ee7a
N
2043 validate(afd, destfd[0], destoffsets[0]);
2044 part = 0;
e9e43ec3 2045 start += stripes * 2; /* where to read next */
7236ee7a
N
2046 size = sra->component_size / (chunk/512);
2047 while (start < size) {
200871ad
N
2048 if (wait_backup(sra, (start-stripes*2)*(chunk/512),
2049 stripes*(chunk/512), 0,
7236ee7a
N
2050 dests, destfd, destoffsets,
2051 part) < 0)
2052 return 0;
200871ad 2053 sysfs_set_num(sra, NULL, "suspend_lo", start*(chunk/512) * data);
7236ee7a
N
2054 if (start + stripes > size)
2055 tailstripes = (size - start);
2056
200871ad 2057 grow_backup(sra, start*(chunk/512), tailstripes,
7236ee7a
N
2058 fds, offsets,
2059 disks, chunk, level, layout,
2060 dests, destfd, destoffsets,
d4445387 2061 part, &degraded, buf);
7236ee7a
N
2062 start += stripes;
2063 part = 1 - part;
2064 validate(afd, destfd[0], destoffsets[0]);
2065 }
200871ad 2066 if (wait_backup(sra, (start-stripes*2) * (chunk/512), stripes * (chunk/512), 0,
7236ee7a
N
2067 dests, destfd, destoffsets,
2068 part) < 0)
2069 return 0;
200871ad
N
2070 sysfs_set_num(sra, NULL, "suspend_lo", ((start-stripes)*(chunk/512)) * data);
2071 wait_backup(sra, (start-stripes) * (chunk/512), tailstripes * (chunk/512), 0,
725cac4c
N
2072 dests, destfd, destoffsets,
2073 1-part);
200871ad 2074 sysfs_set_num(sra, NULL, "suspend_lo", (size*(chunk/512)) * data);
7236ee7a
N
2075 sysfs_set_num(sra, NULL, "sync_speed_min", speed);
2076 free(buf);
2077 return 1;
e86c9dd6 2078}
353632d9
NB
2079
2080/*
2081 * If any spare contains md_back_data-1 which is recent wrt mtime,
2082 * write that data into the array and update the super blocks with
2083 * the new reshape_progress
2084 */
ea0ebe96
N
2085int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt,
2086 char *backup_file, int verbose)
353632d9
NB
2087{
2088 int i, j;
2089 int old_disks;
353632d9 2090 unsigned long long *offsets;
82f2d6ab 2091 unsigned long long nstripe, ostripe;
6e9eac4f 2092 int ndata, odata;
353632d9 2093
e9e43ec3
N
2094 if (info->new_level != info->array.level)
2095 return 1; /* Cannot handle level changes (they are instantaneous) */
2096
2097 odata = info->array.raid_disks - info->delta_disks - 1;
2098 if (info->array.level == 6) odata--; /* number of data disks */
2099 ndata = info->array.raid_disks - 1;
2100 if (info->new_level == 6) ndata--;
353632d9
NB
2101
2102 old_disks = info->array.raid_disks - info->delta_disks;
2103
e9e43ec3
N
2104 if (info->delta_disks <= 0)
2105 /* Didn't grow, so the backup file must have
2106 * been used
2107 */
2108 old_disks = cnt;
06b0d786 2109 for (i=old_disks-(backup_file?1:0); i<cnt; i++) {
353632d9 2110 struct mdinfo dinfo;
06b0d786 2111 int fd;
e9e43ec3 2112 int bsbsize;
ea0ebe96 2113 char *devname, namebuf[20];
353632d9
NB
2114
2115 /* This was a spare and may have some saved data on it.
2116 * Load the superblock, find and load the
2117 * backup_super_block.
2118 * If either fail, go on to next device.
2119 * If the backup contains no new info, just return
206c5eae 2120 * else restore data and update all superblocks
353632d9 2121 */
06b0d786
NB
2122 if (i == old_disks-1) {
2123 fd = open(backup_file, O_RDONLY);
e9e43ec3
N
2124 if (fd<0) {
2125 fprintf(stderr, Name ": backup file %s inaccessible: %s\n",
2126 backup_file, strerror(errno));
06b0d786 2127 continue;
e9e43ec3 2128 }
ea0ebe96 2129 devname = backup_file;
06b0d786
NB
2130 } else {
2131 fd = fdlist[i];
2132 if (fd < 0)
2133 continue;
3da92f27 2134 if (st->ss->load_super(st, fd, NULL))
06b0d786 2135 continue;
353632d9 2136
a5d85af7 2137 st->ss->getinfo_super(st, &dinfo, NULL);
3da92f27
NB
2138 st->ss->free_super(st);
2139
06b0d786
NB
2140 if (lseek64(fd,
2141 (dinfo.data_offset + dinfo.component_size - 8) <<9,
ea0ebe96
N
2142 0) < 0) {
2143 fprintf(stderr, Name ": Cannot seek on device %d\n", i);
06b0d786 2144 continue; /* Cannot seek */
ea0ebe96
N
2145 }
2146 sprintf(namebuf, "device-%d", i);
2147 devname = namebuf;
06b0d786 2148 }
ea0ebe96
N
2149 if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb)) {
2150 if (verbose)
2151 fprintf(stderr, Name ": Cannot read from %s\n", devname);
353632d9 2152 continue; /* Cannot read */
ea0ebe96 2153 }
e9e43ec3 2154 if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0 &&
ea0ebe96
N
2155 memcmp(bsb.magic, "md_backup_data-2", 16) != 0) {
2156 if (verbose)
2157 fprintf(stderr, Name ": No backup metadata on %s\n", devname);
353632d9 2158 continue;
ea0ebe96
N
2159 }
2160 if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb))) {
2161 if (verbose)
2162 fprintf(stderr, Name ": Bad backup-metadata checksum on %s\n", devname);
353632d9 2163 continue; /* bad checksum */
ea0ebe96 2164 }
e9e43ec3 2165 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0 &&
ea0ebe96
N
2166 bsb.sb_csum2 != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum2)-((char*)&bsb))) {
2167 if (verbose)
2168 fprintf(stderr, Name ": Bad backup-metadata checksum2 on %s\n", devname);
22e30516 2169 continue; /* Bad second checksum */
ea0ebe96
N
2170 }
2171 if (memcmp(bsb.set_uuid,info->uuid, 16) != 0) {
2172 if (verbose)
2173 fprintf(stderr, Name ": Wrong uuid on backup-metadata on %s\n", devname);
353632d9 2174 continue; /* Wrong uuid */
ea0ebe96 2175 }
353632d9 2176
097075b6
N
2177 /* array utime and backup-mtime should be updated at much the same time, but it seems that
2178 * sometimes they aren't... So allow considerable flexability in matching, and allow
2179 * this test to be overridden by an environment variable.
2180 */
f21e18ca
N
2181 if (info->array.utime > (int)__le64_to_cpu(bsb.mtime) + 2*60*60 ||
2182 info->array.utime < (int)__le64_to_cpu(bsb.mtime) - 10*60) {
097075b6
N
2183 if (check_env("MDADM_GROW_ALLOW_OLD")) {
2184 fprintf(stderr, Name ": accepting backup with timestamp %lu "
2185 "for array with timestamp %lu\n",
2186 (unsigned long)__le64_to_cpu(bsb.mtime),
2187 (unsigned long)info->array.utime);
2188 } else {
2189 if (verbose)
2190 fprintf(stderr, Name ": too-old timestamp on "
2191 "backup-metadata on %s\n", devname);
2192 continue; /* time stamp is too bad */
2193 }
ea0ebe96 2194 }
353632d9 2195
e9e43ec3
N
2196 if (bsb.magic[15] == '1') {
2197 if (info->delta_disks >= 0) {
2198 /* reshape_progress is increasing */
2199 if (__le64_to_cpu(bsb.arraystart) + __le64_to_cpu(bsb.length) <
ea0ebe96
N
2200 info->reshape_progress) {
2201 nonew:
2202 if (verbose)
2203 fprintf(stderr, Name ": backup-metadata found on %s but is not needed\n", devname);
e9e43ec3 2204 continue; /* No new data here */
ea0ebe96 2205 }
e9e43ec3
N
2206 } else {
2207 /* reshape_progress is decreasing */
2208 if (__le64_to_cpu(bsb.arraystart) >=
2209 info->reshape_progress)
ea0ebe96 2210 goto nonew; /* No new data here */
e9e43ec3
N
2211 }
2212 } else {
2213 if (info->delta_disks >= 0) {
2214 /* reshape_progress is increasing */
2215 if (__le64_to_cpu(bsb.arraystart) + __le64_to_cpu(bsb.length) <
2216 info->reshape_progress &&
2217 __le64_to_cpu(bsb.arraystart2) + __le64_to_cpu(bsb.length2) <
2218 info->reshape_progress)
ea0ebe96 2219 goto nonew; /* No new data here */
e9e43ec3
N
2220 } else {
2221 /* reshape_progress is decreasing */
2222 if (__le64_to_cpu(bsb.arraystart) >=
2223 info->reshape_progress &&
2224 __le64_to_cpu(bsb.arraystart2) >=
2225 info->reshape_progress)
ea0ebe96 2226 goto nonew; /* No new data here */
e9e43ec3
N
2227 }
2228 }
ea0ebe96
N
2229 if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0) {
2230 second_fail:
2231 if (verbose)
2232 fprintf(stderr, Name ": Failed to verify secondary backup-metadata block on %s\n",
2233 devname);
353632d9 2234 continue; /* Cannot seek */
ea0ebe96 2235 }
2efedc7b 2236 /* There should be a duplicate backup superblock 4k before here */
06b0d786 2237 if (lseek64(fd, -4096, 1) < 0 ||
0155af90 2238 read(fd, &bsb2, sizeof(bsb2)) != sizeof(bsb2))
ea0ebe96 2239 goto second_fail; /* Cannot find leading superblock */
e9e43ec3
N
2240 if (bsb.magic[15] == '1')
2241 bsbsize = offsetof(struct mdp_backup_super, pad1);
2242 else
2243 bsbsize = offsetof(struct mdp_backup_super, pad);
ff94fb86 2244 if (memcmp(&bsb2, &bsb, bsbsize) != 0)
ea0ebe96 2245 goto second_fail; /* Cannot find leading superblock */
2efedc7b 2246
353632d9
NB
2247 /* Now need the data offsets for all devices. */
2248 offsets = malloc(sizeof(*offsets)*info->array.raid_disks);
2249 for(j=0; j<info->array.raid_disks; j++) {
2250 if (fdlist[j] < 0)
2251 continue;
3da92f27 2252 if (st->ss->load_super(st, fdlist[j], NULL))
353632d9
NB
2253 /* FIXME should be this be an error */
2254 continue;
a5d85af7 2255 st->ss->getinfo_super(st, &dinfo, NULL);
3da92f27 2256 st->ss->free_super(st);
14e5b4d7 2257 offsets[j] = dinfo.data_offset * 512;
353632d9
NB
2258 }
2259 printf(Name ": restoring critical section\n");
2260
2261 if (restore_stripes(fdlist, offsets,
2262 info->array.raid_disks,
2263 info->new_chunk,
2264 info->new_level,
2265 info->new_layout,
06b0d786 2266 fd, __le64_to_cpu(bsb.devstart)*512,
92dcdf7c 2267 __le64_to_cpu(bsb.arraystart)*512,
e9e43ec3
N
2268 __le64_to_cpu(bsb.length)*512)) {
2269 /* didn't succeed, so giveup */
ea0ebe96
N
2270 if (verbose)
2271 fprintf(stderr, Name ": Error restoring backup from %s\n",
2272 devname);
e9e43ec3
N
2273 return 1;
2274 }
2275
2276 if (bsb.magic[15] == '2' &&
2277 restore_stripes(fdlist, offsets,
2278 info->array.raid_disks,
2279 info->new_chunk,
2280 info->new_level,
2281 info->new_layout,
2282 fd, __le64_to_cpu(bsb.devstart)*512 +
2283 __le64_to_cpu(bsb.devstart2)*512,
92dcdf7c 2284 __le64_to_cpu(bsb.arraystart2)*512,
e9e43ec3 2285 __le64_to_cpu(bsb.length2)*512)) {
353632d9 2286 /* didn't succeed, so giveup */
ea0ebe96
N
2287 if (verbose)
2288 fprintf(stderr, Name ": Error restoring second backup from %s\n",
2289 devname);
2295250a 2290 return 1;
353632d9
NB
2291 }
2292
e9e43ec3 2293
353632d9
NB
2294 /* Ok, so the data is restored. Let's update those superblocks. */
2295
e9e43ec3
N
2296 if (info->delta_disks >= 0) {
2297 info->reshape_progress = __le64_to_cpu(bsb.arraystart) +
2298 __le64_to_cpu(bsb.length);
2299 if (bsb.magic[15] == '2') {
2300 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2) +
2301 __le64_to_cpu(bsb.length2);
2302 if (p2 > info->reshape_progress)
2303 info->reshape_progress = p2;
2304 }
2305 } else {
2306 info->reshape_progress = __le64_to_cpu(bsb.arraystart);
2307 if (bsb.magic[15] == '2') {
2308 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2);
2309 if (p2 < info->reshape_progress)
2310 info->reshape_progress = p2;
2311 }
2312 }
353632d9
NB
2313 for (j=0; j<info->array.raid_disks; j++) {
2314 if (fdlist[j] < 0) continue;
3da92f27 2315 if (st->ss->load_super(st, fdlist[j], NULL))
353632d9 2316 continue;
a5d85af7 2317 st->ss->getinfo_super(st, &dinfo, NULL);
e9e43ec3 2318 dinfo.reshape_progress = info->reshape_progress;
3da92f27 2319 st->ss->update_super(st, &dinfo,
68c7d6d7
NB
2320 "_reshape_progress",
2321 NULL,0, 0, NULL);
3da92f27
NB
2322 st->ss->store_super(st, fdlist[j]);
2323 st->ss->free_super(st);
353632d9 2324 }
353632d9
NB
2325 return 0;
2326 }
6e9eac4f
NB
2327 /* Didn't find any backup data, try to see if any
2328 * was needed.
2329 */
82f2d6ab
N
2330 if (info->delta_disks < 0) {
2331 /* When shrinking, the critical section is at the end.
2332 * So see if we are before the critical section.
2333 */
2334 unsigned long long first_block;
2335 nstripe = ostripe = 0;
2336 first_block = 0;
2337 while (ostripe >= nstripe) {
2338 ostripe += info->array.chunk_size / 512;
2339 first_block = ostripe * odata;
2340 nstripe = first_block / ndata / (info->new_chunk/512) *
2341 (info->new_chunk/512);
2342 }
2343
2344 if (info->reshape_progress >= first_block)
2345 return 0;
6e9eac4f 2346 }
82f2d6ab
N
2347 if (info->delta_disks > 0) {
2348 /* See if we are beyond the critical section. */
2349 unsigned long long last_block;
2350 nstripe = ostripe = 0;
2351 last_block = 0;
2352 while (nstripe >= ostripe) {
2353 nstripe += info->new_chunk / 512;
2354 last_block = nstripe * ndata;
2355 ostripe = last_block / odata / (info->array.chunk_size/512) *
2356 (info->array.chunk_size/512);
2357 }
6e9eac4f 2358
82f2d6ab
N
2359 if (info->reshape_progress >= last_block)
2360 return 0;
2361 }
6e9eac4f 2362 /* needed to recover critical section! */
ea0ebe96
N
2363 if (verbose)
2364 fprintf(stderr, Name ": Failed to find backup of critical section\n");
2295250a 2365 return 1;
353632d9 2366}
e9e43ec3
N
2367
2368int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
2369 char *backup_file)
2370{
2371 /* Array is assembled and ready to be started, but
2372 * monitoring is probably required.
2373 * So:
2374 * - start read-only
2375 * - set upper bound for resync
2376 * - initialise the 'suspend' boundaries
2377 * - switch to read-write
2378 * - fork and continue monitoring
2379 */
2380 int err;
2381 int backup_list[1];
2382 unsigned long long backup_offsets[1];
2383 int odisks, ndisks, ochunk, nchunk,odata,ndata;
2384 unsigned long a,b,blocks,stripes;
2385 int backup_fd;
2386 int *fds;
2387 unsigned long long *offsets;
2388 int d;
2389 struct mdinfo *sra, *sd;
2390 int rv;
f21e18ca 2391 unsigned long cache;
e9e43ec3
N
2392 int done = 0;
2393
2394 err = sysfs_set_str(info, NULL, "array_state", "readonly");
2395 if (err)
2396 return err;
2397
2398 /* make sure reshape doesn't progress until we are ready */
2399 sysfs_set_str(info, NULL, "sync_max", "0");
2400 sysfs_set_str(info, NULL, "array_state", "active"); /* FIXME or clean */
5f6ca90a
N
2401
2402 sra = sysfs_read(-1, devname2devnum(info->sys_name),
2403 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|
2404 GET_CACHE);
2405 if (!sra)
2406 return 1;
2407
e9e43ec3
N
2408 /* ndisks is not growing, so raid_disks is old and +delta is new */
2409 odisks = info->array.raid_disks;
2410 ndisks = odisks + info->delta_disks;
2411 odata = odisks - 1;
2412 ndata = ndisks - 1;
2413 if (info->array.level == 6) {
2414 odata--;
2415 ndata--;
2416 }
2417 ochunk = info->array.chunk_size;
2418 nchunk = info->new_chunk;
2419
200871ad
N
2420 a = (ochunk/512) * odata;
2421 b = (nchunk/512) * ndata;
e9e43ec3
N
2422 /* Find GCD */
2423 while (a != b) {
2424 if (a < b)
2425 b -= a;
2426 if (b < a)
2427 a -= b;
2428 }
2429 /* LCM == product / GCD */
200871ad 2430 blocks = (ochunk/512) * (nchunk/512) * odata * ndata / a;
e9e43ec3
N
2431
2432 if (ndata == odata)
1b13faf7
N
2433 while (blocks * 32 < sra->component_size &&
2434 blocks < 16*1024*2)
2435 blocks *= 2;
e9e43ec3
N
2436 stripes = blocks / (info->array.chunk_size/512) / odata;
2437
1b13faf7
N
2438 /* check that the internal stripe cache is
2439 * large enough, or it won't work.
2440 */
2441 cache = (nchunk < ochunk) ? ochunk : nchunk;
2442 cache = cache * 4 / 4096;
2443 if (cache < blocks / 8 / odisks + 16)
2444 /* Make it big enough to hold 'blocks' */
2445 cache = blocks / 8 / odisks + 16;
2446 if (sra->cache_size < cache)
2447 sysfs_set_num(sra, NULL, "stripe_cache_size",
2448 cache+1);
e9e43ec3
N
2449
2450 memset(&bsb, 0, 512);
2451 memcpy(bsb.magic, "md_backup_data-1", 16);
2452 memcpy(&bsb.set_uuid, info->uuid, 16);
2453 bsb.mtime = __cpu_to_le64(time(0));
2454 bsb.devstart2 = blocks;
2455
2456 backup_fd = open(backup_file, O_RDWR|O_CREAT, S_IRUSR | S_IWUSR);
2457 backup_list[0] = backup_fd;
2458 backup_offsets[0] = 8 * 512;
2459 fds = malloc(odisks * sizeof(fds[0]));
2460 offsets = malloc(odisks * sizeof(offsets[0]));
2461 for (d=0; d<odisks; d++)
2462 fds[d] = -1;
2463
e9e43ec3
N
2464 for (sd = sra->devs; sd; sd = sd->next) {
2465 if (sd->disk.state & (1<<MD_DISK_FAULTY))
2466 continue;
2467 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
2468 char *dn = map_dev(sd->disk.major,
2469 sd->disk.minor, 1);
2470 fds[sd->disk.raid_disk]
2471 = dev_open(dn, O_RDONLY);
2472 offsets[sd->disk.raid_disk] = sd->data_offset*512;
2473 if (fds[sd->disk.raid_disk] < 0) {
2474 fprintf(stderr, Name ": %s: cannot open component %s\n",
2475 info->sys_name, dn?dn:"-unknown-");
2476 rv = 1;
2477 goto release;
2478 }
2479 free(dn);
2480 }
2481 }
2482
2483 switch(fork()) {
2484 case 0:
2485 close(mdfd);
2486 mlockall(MCL_FUTURE);
2487 if (info->delta_disks < 0)
2488 done = child_shrink(-1, info, stripes,
2489 fds, offsets,
2490 info->array.raid_disks,
2491 info->array.chunk_size,
2492 info->array.level, info->array.layout,
2493 odata,
2494 1, backup_list, backup_offsets);
2495 else if (info->delta_disks == 0) {
2496 /* The 'start' is a per-device stripe number.
2497 * reshape_progress is a per-array sector number.
2498 * So divide by ndata * chunk_size
2499 */
2500 unsigned long long start = info->reshape_progress / ndata;
2501 start /= (info->array.chunk_size/512);
2502 done = child_same_size(-1, info, stripes,
2503 fds, offsets,
2504 start,
2505 info->array.raid_disks,
2506 info->array.chunk_size,
2507 info->array.level, info->array.layout,
2508 odata,
2509 1, backup_list, backup_offsets);
2510 }
2511 if (backup_file && done)
2512 unlink(backup_file);
2513 /* FIXME should I intuit a level change */
2514 exit(0);
2515 case -1:
2516 fprintf(stderr, Name ": Cannot run child to continue monitoring reshape: %s\n",
2517 strerror(errno));
2518 return 1;
2519 default:
2520 break;
2521 }
2522release:
2523 return 0;
2524}
2525
2526