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