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