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