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