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