]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Grow.c
Fix tests sh too many arguments warnings
[thirdparty/mdadm.git] / Grow.c
CommitLineData
e5329c37
NB
1/*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
ca3b6696 4 * Copyright (C) 2001-2012 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
3f54bd62
AK
38int restore_backup(struct supertype *st,
39 struct mdinfo *content,
40 int working_disks,
41 int next_spare,
42 char *backup_file,
43 int verbose)
44{
45 int i;
46 int *fdlist;
47 struct mdinfo *dev;
48 int err;
49 int disk_count = next_spare + working_disks;
50
51 dprintf("Called restore_backup()\n");
503975b9
N
52 fdlist = xmalloc(sizeof(int) * disk_count);
53
3f54bd62
AK
54 for (i = 0; i < next_spare; i++)
55 fdlist[i] = -1;
56 for (dev = content->devs; dev; dev = dev->next) {
57 char buf[22];
58 int fd;
59 sprintf(buf, "%d:%d",
60 dev->disk.major,
61 dev->disk.minor);
62 fd = dev_open(buf, O_RDWR);
63
64 if (dev->disk.raid_disk >= 0)
65 fdlist[dev->disk.raid_disk] = fd;
66 else
67 fdlist[next_spare++] = fd;
68 }
69
70 if (st->ss->external && st->ss->recover_backup)
71 err = st->ss->recover_backup(st, content);
72 else
73 err = Grow_restart(st, content, fdlist, next_spare,
74 backup_file, verbose > 0);
75
76 while (next_spare > 0) {
cc7f63e5
N
77 next_spare--;
78 if (fdlist[next_spare] >= 0)
79 close(fdlist[next_spare]);
3f54bd62
AK
80 }
81 free(fdlist);
82 if (err) {
e7b84f9d
N
83 pr_err("Failed to restore critical"
84 " section for reshape - sorry.\n");
3f54bd62 85 if (!backup_file)
e7b84f9d 86 pr_err("Possibly you need"
3f54bd62
AK
87 " to specify a --backup-file\n");
88 return 1;
89 }
90
91 dprintf("restore_backup() returns status OK.\n");
92 return 0;
93}
94
e5329c37
NB
95int Grow_Add_device(char *devname, int fd, char *newdev)
96{
97 /* Add a device to an active array.
98 * Currently, just extend a linear array.
99 * This requires writing a new superblock on the
100 * new device, calling the kernel to add the device,
101 * and if that succeeds, update the superblock on
102 * all other devices.
103 * This means that we need to *find* all other devices.
104 */
4b1ac34b
NB
105 struct mdinfo info;
106
e5329c37
NB
107 struct stat stb;
108 int nfd, fd2;
109 int d, nd;
82d9eba6 110 struct supertype *st = NULL;
4725bc31 111 char *subarray = NULL;
e5329c37 112
4b1ac34b 113 if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
e7b84f9d 114 pr_err("cannot get array info for %s\n", devname);
e5329c37
NB
115 return 1;
116 }
117
4725bc31 118 if (info.array.level != -1) {
e7b84f9d 119 pr_err("can only add devices to linear arrays\n");
4725bc31
N
120 return 1;
121 }
122
123 st = super_by_fd(fd, &subarray);
82d9eba6 124 if (!st) {
ca3b6696
N
125 pr_err("cannot handle arrays with superblock version %d\n",
126 info.array.major_version);
f9ce90ba
NB
127 return 1;
128 }
129
4725bc31 130 if (subarray) {
e7b84f9d 131 pr_err("Cannot grow linear sub-arrays yet\n");
4725bc31
N
132 free(subarray);
133 free(st);
2641101b 134 return 1;
e5329c37
NB
135 }
136
6416d527 137 nfd = open(newdev, O_RDWR|O_EXCL|O_DIRECT);
e5329c37 138 if (nfd < 0) {
e7b84f9d 139 pr_err("cannot open %s\n", newdev);
4725bc31 140 free(st);
e5329c37
NB
141 return 1;
142 }
143 fstat(nfd, &stb);
144 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
e7b84f9d 145 pr_err("%s is not a block device!\n", newdev);
e5329c37 146 close(nfd);
4725bc31 147 free(st);
e5329c37
NB
148 return 1;
149 }
ca3b6696
N
150 /* now check out all the devices and make sure we can read the
151 * superblock */
4b1ac34b 152 for (d=0 ; d < info.array.raid_disks ; d++) {
e5329c37
NB
153 mdu_disk_info_t disk;
154 char *dv;
155
4725bc31
N
156 st->ss->free_super(st);
157
e5329c37
NB
158 disk.number = d;
159 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
e7b84f9d 160 pr_err("cannot get device detail for device %d\n",
e5329c37 161 d);
4725bc31
N
162 close(nfd);
163 free(st);
e5329c37
NB
164 return 1;
165 }
16c6fa80 166 dv = map_dev(disk.major, disk.minor, 1);
e5329c37 167 if (!dv) {
e7b84f9d 168 pr_err("cannot find device file for device %d\n",
e5329c37 169 d);
4725bc31
N
170 close(nfd);
171 free(st);
e5329c37
NB
172 return 1;
173 }
16c6fa80 174 fd2 = dev_open(dv, O_RDWR);
68fe8c6e 175 if (fd2 < 0) {
e7b84f9d 176 pr_err("cannot open device file %s\n", dv);
4725bc31
N
177 close(nfd);
178 free(st);
e5329c37
NB
179 return 1;
180 }
3da92f27
NB
181
182 if (st->ss->load_super(st, fd2, NULL)) {
e7b84f9d 183 pr_err("cannot find super block on %s\n", dv);
4725bc31 184 close(nfd);
e5329c37 185 close(fd2);
4725bc31 186 free(st);
e5329c37
NB
187 return 1;
188 }
189 close(fd2);
190 }
191 /* Ok, looks good. Lets update the superblock and write it out to
192 * newdev.
193 */
aba69144 194
4b1ac34b
NB
195 info.disk.number = d;
196 info.disk.major = major(stb.st_rdev);
197 info.disk.minor = minor(stb.st_rdev);
198 info.disk.raid_disk = d;
199 info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE);
3da92f27 200 st->ss->update_super(st, &info, "linear-grow-new", newdev,
f752781f 201 0, 0, NULL);
e5329c37 202
3da92f27 203 if (st->ss->store_super(st, nfd)) {
e7b84f9d 204 pr_err("Cannot store new superblock on %s\n",
f752781f 205 newdev);
e5329c37
NB
206 close(nfd);
207 return 1;
208 }
e5329c37 209 close(nfd);
4b1ac34b
NB
210
211 if (ioctl(fd, ADD_NEW_DISK, &info.disk) != 0) {
e7b84f9d 212 pr_err("Cannot add new disk to this array\n");
e5329c37
NB
213 return 1;
214 }
215 /* Well, that seems to have worked.
216 * Now go through and update all superblocks
217 */
218
4b1ac34b 219 if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
e7b84f9d 220 pr_err("cannot get array info for %s\n", devname);
e5329c37
NB
221 return 1;
222 }
223
224 nd = d;
4b1ac34b 225 for (d=0 ; d < info.array.raid_disks ; d++) {
e5329c37
NB
226 mdu_disk_info_t disk;
227 char *dv;
228
229 disk.number = d;
230 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
e7b84f9d 231 pr_err("cannot get device detail for device %d\n",
e5329c37
NB
232 d);
233 return 1;
234 }
16c6fa80 235 dv = map_dev(disk.major, disk.minor, 1);
e5329c37 236 if (!dv) {
e7b84f9d 237 pr_err("cannot find device file for device %d\n",
e5329c37
NB
238 d);
239 return 1;
240 }
16c6fa80 241 fd2 = dev_open(dv, O_RDWR);
e5329c37 242 if (fd2 < 0) {
e7b84f9d 243 pr_err("cannot open device file %s\n", dv);
e5329c37
NB
244 return 1;
245 }
3da92f27 246 if (st->ss->load_super(st, fd2, NULL)) {
e7b84f9d 247 pr_err("cannot find super block on %s\n", dv);
e5329c37
NB
248 close(fd);
249 return 1;
250 }
4b1ac34b
NB
251 info.array.raid_disks = nd+1;
252 info.array.nr_disks = nd+1;
253 info.array.active_disks = nd+1;
254 info.array.working_disks = nd+1;
f752781f 255
3da92f27 256 st->ss->update_super(st, &info, "linear-grow-update", dv,
f752781f 257 0, 0, NULL);
aba69144 258
3da92f27 259 if (st->ss->store_super(st, fd2)) {
e7b84f9d 260 pr_err("Cannot store new superblock on %s\n", dv);
e5329c37
NB
261 close(fd2);
262 return 1;
263 }
264 close(fd2);
265 }
266
267 return 0;
268}
f5e166fe 269
50f01ba5 270int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
f5e166fe
NB
271{
272 /*
273 * First check that array doesn't have a bitmap
274 * Then create the bitmap
275 * Then add it
276 *
277 * For internal bitmaps, we need to check the version,
278 * find all the active devices, and write the bitmap block
279 * to all devices
280 */
281 mdu_bitmap_file_t bmf;
282 mdu_array_info_t array;
283 struct supertype *st;
4725bc31 284 char *subarray = NULL;
dcec9ee5
NB
285 int major = BITMAP_MAJOR_HI;
286 int vers = md_get_version(fd);
8fac0577 287 unsigned long long bitmapsize, array_size;
dcec9ee5
NB
288
289 if (vers < 9003) {
290 major = BITMAP_MAJOR_HOSTENDIAN;
e7b84f9d 291 pr_err("Warning - bitmaps created on this kernel"
b3bd581b
N
292 " are not portable\n"
293 " between different architectures. Consider upgrading"
294 " the Linux kernel.\n");
dcec9ee5 295 }
f5e166fe
NB
296
297 if (ioctl(fd, GET_BITMAP_FILE, &bmf) != 0) {
353632d9 298 if (errno == ENOMEM)
e7b84f9d 299 pr_err("Memory allocation failure.\n");
f5e166fe 300 else
e7b84f9d 301 pr_err("bitmaps not supported by this kernel.\n");
f5e166fe
NB
302 return 1;
303 }
304 if (bmf.pathname[0]) {
50f01ba5 305 if (strcmp(s->bitmap_file,"none")==0) {
fe80f49b 306 if (ioctl(fd, SET_BITMAP_FILE, -1)!= 0) {
e7b84f9d 307 pr_err("failed to remove bitmap %s\n",
fe80f49b
NB
308 bmf.pathname);
309 return 1;
310 }
311 return 0;
312 }
e7b84f9d 313 pr_err("%s already has a bitmap (%s)\n",
f5e166fe
NB
314 devname, bmf.pathname);
315 return 1;
316 }
317 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
e7b84f9d 318 pr_err("cannot get array status for %s\n", devname);
f5e166fe
NB
319 return 1;
320 }
321 if (array.state & (1<<MD_SB_BITMAP_PRESENT)) {
50f01ba5 322 if (strcmp(s->bitmap_file, "none")==0) {
fe80f49b
NB
323 array.state &= ~(1<<MD_SB_BITMAP_PRESENT);
324 if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
e7b84f9d 325 pr_err("failed to remove internal bitmap.\n");
fe80f49b
NB
326 return 1;
327 }
328 return 0;
329 }
e7b84f9d 330 pr_err("Internal bitmap already present on %s\n",
f5e166fe
NB
331 devname);
332 return 1;
333 }
4725bc31 334
50f01ba5 335 if (strcmp(s->bitmap_file, "none") == 0) {
e7b84f9d 336 pr_err("no bitmap found on %s\n", devname);
4725bc31
N
337 return 1;
338 }
5b28bd56 339 if (array.level <= 0) {
e7b84f9d 340 pr_err("Bitmaps not meaningful with level %s\n",
5b28bd56
NB
341 map_num(pers, array.level)?:"of this array");
342 return 1;
343 }
8fac0577
NB
344 bitmapsize = array.size;
345 bitmapsize <<= 1;
beae1dfe 346 if (get_dev_size(fd, NULL, &array_size) &&
8fac0577
NB
347 array_size > (0x7fffffffULL<<9)) {
348 /* Array is big enough that we cannot trust array.size
349 * try other approaches
350 */
351 bitmapsize = get_component_size(fd);
352 }
8fac0577 353 if (bitmapsize == 0) {
e7b84f9d 354 pr_err("Cannot reliably determine size of array to create bitmap - sorry.\n");
8fac0577
NB
355 return 1;
356 }
357
f9c25f1d 358 if (array.level == 10) {
8686f3ed 359 int ncopies = (array.layout&255)*((array.layout>>8)&255);
f9c25f1d
NB
360 bitmapsize = bitmapsize * array.raid_disks / ncopies;
361 }
362
4725bc31 363 st = super_by_fd(fd, &subarray);
f5e166fe 364 if (!st) {
e7b84f9d 365 pr_err("Cannot understand version %d.%d\n",
f5e166fe
NB
366 array.major_version, array.minor_version);
367 return 1;
368 }
4725bc31 369 if (subarray) {
e7b84f9d 370 pr_err("Cannot add bitmaps to sub-arrays yet\n");
4725bc31
N
371 free(subarray);
372 free(st);
fe80f49b 373 return 1;
4725bc31 374 }
50f01ba5 375 if (strcmp(s->bitmap_file, "internal") == 0) {
c0c1acd6 376 int rv;
f5e166fe 377 int d;
c0c1acd6
N
378 int offset_setable = 0;
379 struct mdinfo *mdi;
ebeb3663 380 if (st->ss->add_internal_bitmap == NULL) {
e7b84f9d 381 pr_err("Internal bitmaps not supported "
ebeb3663
N
382 "with %s metadata\n", st->ss->name);
383 return 1;
384 }
c0c1acd6
N
385 mdi = sysfs_read(fd, -1, GET_BITMAP_LOCATION);
386 if (mdi)
387 offset_setable = 1;
ea329559 388 for (d=0; d< st->max_devs; d++) {
f5e166fe
NB
389 mdu_disk_info_t disk;
390 char *dv;
391 disk.number = d;
392 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
393 continue;
394 if (disk.major == 0 &&
395 disk.minor == 0)
396 continue;
397 if ((disk.state & (1<<MD_DISK_SYNC))==0)
398 continue;
16c6fa80 399 dv = map_dev(disk.major, disk.minor, 1);
f5e166fe 400 if (dv) {
16c6fa80 401 int fd2 = dev_open(dv, O_RDWR);
f5e166fe
NB
402 if (fd2 < 0)
403 continue;
3da92f27 404 if (st->ss->load_super(st, fd2, NULL)==0) {
199171a2 405 if (st->ss->add_internal_bitmap(
3da92f27 406 st,
50f01ba5 407 &s->bitmap_chunk, c->delay, s->write_behind,
c0c1acd6
N
408 bitmapsize, offset_setable,
409 major)
199171a2 410 )
3da92f27 411 st->ss->write_bitmap(st, fd2);
21e92547 412 else {
ca3b6696
N
413 pr_err("failed to create internal bitmap"
414 " - chunksize problem.\n");
21e92547
NB
415 close(fd2);
416 return 1;
417 }
f5e166fe
NB
418 }
419 close(fd2);
420 }
421 }
c0c1acd6
N
422 if (offset_setable) {
423 st->ss->getinfo_super(st, mdi, NULL);
424 sysfs_init(mdi, fd, -1);
012a8641
JS
425 rv = sysfs_set_num_signed(mdi, NULL, "bitmap/location",
426 mdi->bitmap_offset);
c0c1acd6
N
427 } else {
428 array.state |= (1<<MD_SB_BITMAP_PRESENT);
429 rv = ioctl(fd, SET_ARRAY_INFO, &array);
430 }
431 if (rv < 0) {
ff634064 432 if (errno == EBUSY)
e7b84f9d
N
433 pr_err("Cannot add bitmap while array is"
434 " resyncing or reshaping etc.\n");
435 pr_err("failed to set internal bitmap.\n");
f5e166fe
NB
436 return 1;
437 }
fe80f49b
NB
438 } else {
439 int uuid[4];
440 int bitmap_fd;
441 int d;
442 int max_devs = st->max_devs;
fe80f49b
NB
443
444 /* try to load a superblock */
ca3b6696 445 for (d = 0; d < max_devs; d++) {
fe80f49b
NB
446 mdu_disk_info_t disk;
447 char *dv;
448 int fd2;
449 disk.number = d;
450 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
451 continue;
452 if ((disk.major==0 && disk.minor==0) ||
453 (disk.state & (1<<MD_DISK_REMOVED)))
454 continue;
16c6fa80 455 dv = map_dev(disk.major, disk.minor, 1);
ca3b6696
N
456 if (!dv)
457 continue;
16c6fa80 458 fd2 = dev_open(dv, O_RDONLY);
e7344e90
JS
459 if (fd2 >= 0) {
460 if (st->ss->load_super(st, fd2, NULL) == 0) {
461 close(fd2);
462 st->ss->uuid_from_super(st, uuid);
463 break;
464 }
fe80f49b 465 close(fd2);
fe80f49b 466 }
fe80f49b
NB
467 }
468 if (d == max_devs) {
e7b84f9d 469 pr_err("cannot find UUID for array!\n");
fe80f49b
NB
470 return 1;
471 }
50f01ba5
N
472 if (CreateBitmap(s->bitmap_file, c->force, (char*)uuid, s->bitmap_chunk,
473 c->delay, s->write_behind, bitmapsize, major)) {
fe80f49b
NB
474 return 1;
475 }
50f01ba5 476 bitmap_fd = open(s->bitmap_file, O_RDWR);
fe80f49b 477 if (bitmap_fd < 0) {
e7b84f9d 478 pr_err("weird: %s cannot be opened\n",
50f01ba5 479 s->bitmap_file);
fe80f49b
NB
480 return 1;
481 }
482 if (ioctl(fd, SET_BITMAP_FILE, bitmap_fd) < 0) {
ff634064
N
483 int err = errno;
484 if (errno == EBUSY)
e7b84f9d
N
485 pr_err("Cannot add bitmap while array is"
486 " resyncing or reshaping etc.\n");
487 pr_err("Cannot set bitmap file for %s: %s\n",
ff634064 488 devname, strerror(err));
fe80f49b
NB
489 return 1;
490 }
491 }
f5e166fe
NB
492
493 return 0;
494}
495
e86c9dd6
NB
496/*
497 * When reshaping an array we might need to backup some data.
498 * This is written to all spares with a 'super_block' describing it.
ff94fb86 499 * The superblock goes 4K from the end of the used space on the
e86c9dd6
NB
500 * device.
501 * It if written after the backup is complete.
502 * It has the following structure.
503 */
504
5fdf37e3 505static struct mdp_backup_super {
7236ee7a 506 char magic[16]; /* md_backup_data-1 or -2 */
e86c9dd6
NB
507 __u8 set_uuid[16];
508 __u64 mtime;
509 /* start/sizes in 512byte sectors */
7236ee7a 510 __u64 devstart; /* address on backup device/file of data */
e86c9dd6
NB
511 __u64 arraystart;
512 __u64 length;
513 __u32 sb_csum; /* csum of preceeding bytes. */
7236ee7a
N
514 __u32 pad1;
515 __u64 devstart2; /* offset in to data of second section */
516 __u64 arraystart2;
517 __u64 length2;
518 __u32 sb_csum2; /* csum of preceeding bytes. */
519 __u8 pad[512-68-32];
5fdf37e3 520} __attribute__((aligned(512))) bsb, bsb2;
e86c9dd6 521
4411fb17 522static __u32 bsb_csum(char *buf, int len)
e86c9dd6
NB
523{
524 int i;
525 int csum = 0;
ca3b6696 526 for (i = 0; i < len; i++)
e86c9dd6
NB
527 csum = (csum<<3) + buf[0];
528 return __cpu_to_le32(csum);
529}
530
d7ca196c
N
531static int check_idle(struct supertype *st)
532{
533 /* Check that all member arrays for this container, or the
534 * container of this array, are idle
535 */
536 int container_dev = (st->container_dev != NoMdDev
537 ? st->container_dev : st->devnum);
538 char container[40];
539 struct mdstat_ent *ent, *e;
540 int is_idle = 1;
24daa16f 541
d7ca196c
N
542 fmt_devname(container, container_dev);
543 ent = mdstat_read(0, 0);
544 for (e = ent ; e; e = e->next) {
545 if (!is_container_member(e, container))
546 continue;
547 if (e->percent >= 0) {
548 is_idle = 0;
549 break;
550 }
551 }
552 free_mdstat(ent);
553 return is_idle;
554}
555
7f2ba464 556static int freeze_container(struct supertype *st)
7236ee7a 557{
7f2ba464
DW
558 int container_dev = (st->container_dev != NoMdDev
559 ? st->container_dev : st->devnum);
d7ca196c 560 char container[40];
7f2ba464 561
d7ca196c
N
562 if (!check_idle(st))
563 return -1;
24daa16f 564
d7ca196c 565 fmt_devname(container, container_dev);
7f2ba464
DW
566
567 if (block_monitor(container, 1)) {
e7b84f9d 568 pr_err("failed to freeze container\n");
7f2ba464
DW
569 return -2;
570 }
571
572 return 1;
573}
574
575static void unfreeze_container(struct supertype *st)
576{
577 int container_dev = (st->container_dev != NoMdDev
578 ? st->container_dev : st->devnum);
d7ca196c 579 char container[40];
24daa16f 580
d7ca196c 581 fmt_devname(container, container_dev);
7f2ba464
DW
582
583 unblock_monitor(container, 1);
584}
585
586static int freeze(struct supertype *st)
587{
588 /* Try to freeze resync/rebuild on this array/container.
7236ee7a 589 * Return -1 if the array is busy,
7f2ba464 590 * return -2 container cannot be frozen,
7236ee7a
N
591 * return 0 if this kernel doesn't support 'frozen'
592 * return 1 if it worked.
593 */
7f2ba464
DW
594 if (st->ss->external)
595 return freeze_container(st);
596 else {
597 struct mdinfo *sra = sysfs_read(-1, st->devnum, GET_VERSION);
598 int err;
815c8a7e 599 char buf[20];
7f2ba464
DW
600
601 if (!sra)
602 return -1;
815c8a7e
N
603 /* Need to clear any 'read-auto' status */
604 if (sysfs_get_str(sra, NULL, "array_state", buf, 20) > 0 &&
605 strncmp(buf, "read-auto", 9) == 0)
606 sysfs_set_str(sra, NULL, "array_state", "clean");
607
7f2ba464
DW
608 err = sysfs_freeze_array(sra);
609 sysfs_free(sra);
610 return err;
611 }
7236ee7a
N
612}
613
9202b8eb 614static void unfreeze(struct supertype *st)
7236ee7a 615{
7f2ba464
DW
616 if (st->ss->external)
617 return unfreeze_container(st);
618 else {
619 struct mdinfo *sra = sysfs_read(-1, st->devnum, GET_VERSION);
620
621 if (sra)
622 sysfs_set_str(sra, NULL, "sync_action", "idle");
7f2ba464
DW
623 sysfs_free(sra);
624 }
7236ee7a
N
625}
626
4411fb17 627static void wait_reshape(struct mdinfo *sra)
7236ee7a
N
628{
629 int fd = sysfs_get_fd(sra, NULL, "sync_action");
630 char action[20];
631
92a19f1a
AK
632 if (fd < 0)
633 return;
634
635 while (sysfs_fd_get_str(fd, action, 20) > 0 &&
636 strncmp(action, "reshape", 7) == 0) {
7236ee7a
N
637 fd_set rfds;
638 FD_ZERO(&rfds);
639 FD_SET(fd, &rfds);
640 select(fd+1, NULL, NULL, &rfds, NULL);
92a19f1a
AK
641 }
642 close(fd);
7236ee7a 643}
7bc71196 644
7ec0996c
LD
645static int reshape_super(struct supertype *st, unsigned long long size,
646 int level, int layout, int chunksize, int raid_disks,
41784c88 647 int delta_disks, char *backup_file, char *dev,
016e00f5 648 int direction, int verbose)
7bc71196
DW
649{
650 /* nothing extra to check in the native case */
651 if (!st->ss->external)
652 return 0;
653 if (!st->ss->reshape_super ||
654 !st->ss->manage_reshape) {
e7b84f9d 655 pr_err("%s metadata does not support reshape\n",
7bc71196
DW
656 st->ss->name);
657 return 1;
658 }
659
660 return st->ss->reshape_super(st, size, level, layout, chunksize,
41784c88 661 raid_disks, delta_disks, backup_file, dev,
016e00f5 662 direction, verbose);
7bc71196
DW
663}
664
665static void sync_metadata(struct supertype *st)
666{
667 if (st->ss->external) {
76266030 668 if (st->update_tail) {
7bc71196 669 flush_metadata_updates(st);
76266030
N
670 st->update_tail = &st->updates;
671 } else
7bc71196
DW
672 st->ss->sync_metadata(st);
673 }
674}
675
676static int subarray_set_num(char *container, struct mdinfo *sra, char *name, int n)
677{
678 /* when dealing with external metadata subarrays we need to be
679 * prepared to handle EAGAIN. The kernel may need to wait for
680 * mdmon to mark the array active so the kernel can handle
681 * allocations/writeback when preparing the reshape action
682 * (md_allow_write()). We temporarily disable safe_mode_delay
683 * to close a race with the array_state going clean before the
684 * next write to raid_disks / stripe_cache_size
685 */
686 char safe[50];
687 int rc;
688
689 /* only 'raid_disks' and 'stripe_cache_size' trigger md_allow_write */
5da9ab98
N
690 if (!container ||
691 (strcmp(name, "raid_disks") != 0 &&
692 strcmp(name, "stripe_cache_size") != 0))
7bc71196
DW
693 return sysfs_set_num(sra, NULL, name, n);
694
695 rc = sysfs_get_str(sra, NULL, "safe_mode_delay", safe, sizeof(safe));
696 if (rc <= 0)
697 return -1;
698 sysfs_set_num(sra, NULL, "safe_mode_delay", 0);
699 rc = sysfs_set_num(sra, NULL, name, n);
700 if (rc < 0 && errno == EAGAIN) {
701 ping_monitor(container);
702 /* if we get EAGAIN here then the monitor is not active
703 * so stop trying
704 */
705 rc = sysfs_set_num(sra, NULL, name, n);
706 }
707 sysfs_set_str(sra, NULL, "safe_mode_delay", safe);
708 return rc;
709}
710
27a1e5b5
N
711int start_reshape(struct mdinfo *sra, int already_running,
712 int before_data_disks, int data_disks)
47eb4d5a
N
713{
714 int err;
6937e6d2
AK
715 unsigned long long sync_max_to_set;
716
0f28668b 717 sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
6937e6d2
AK
718 err = sysfs_set_num(sra, NULL, "suspend_hi", sra->reshape_progress);
719 err = err ?: sysfs_set_num(sra, NULL, "suspend_lo",
720 sra->reshape_progress);
27a1e5b5
N
721 if (before_data_disks <= data_disks)
722 sync_max_to_set = sra->reshape_progress / data_disks;
723 else
724 sync_max_to_set = (sra->component_size * data_disks
725 - sra->reshape_progress) / data_disks;
1e971e71 726 if (!already_running)
6937e6d2
AK
727 sysfs_set_num(sra, NULL, "sync_min", sync_max_to_set);
728 err = err ?: sysfs_set_num(sra, NULL, "sync_max", sync_max_to_set);
1e971e71
N
729 if (!already_running)
730 err = err ?: sysfs_set_str(sra, NULL, "sync_action", "reshape");
47eb4d5a
N
731
732 return err;
733}
734
735void abort_reshape(struct mdinfo *sra)
736{
737 sysfs_set_str(sra, NULL, "sync_action", "idle");
738 sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
739 sysfs_set_num(sra, NULL, "suspend_hi", 0);
740 sysfs_set_num(sra, NULL, "suspend_lo", 0);
741 sysfs_set_num(sra, NULL, "sync_min", 0);
742 sysfs_set_str(sra, NULL, "sync_max", "max");
743}
744
dfe77a9e
KW
745int remove_disks_for_takeover(struct supertype *st,
746 struct mdinfo *sra,
747 int layout)
62a48395
AK
748{
749 int nr_of_copies;
750 struct mdinfo *remaining;
751 int slot;
752
dfe77a9e
KW
753 if (sra->array.level == 10)
754 nr_of_copies = layout & 0xff;
755 else if (sra->array.level == 1)
756 nr_of_copies = sra->array.raid_disks;
757 else
758 return 1;
62a48395
AK
759
760 remaining = sra->devs;
761 sra->devs = NULL;
762 /* for each 'copy', select one device and remove from the list. */
763 for (slot = 0; slot < sra->array.raid_disks; slot += nr_of_copies) {
764 struct mdinfo **diskp;
765 int found = 0;
766
767 /* Find a working device to keep */
768 for (diskp = &remaining; *diskp ; diskp = &(*diskp)->next) {
769 struct mdinfo *disk = *diskp;
770
771 if (disk->disk.raid_disk < slot)
772 continue;
773 if (disk->disk.raid_disk >= slot + nr_of_copies)
774 continue;
775 if (disk->disk.state & (1<<MD_DISK_REMOVED))
776 continue;
777 if (disk->disk.state & (1<<MD_DISK_FAULTY))
778 continue;
779 if (!(disk->disk.state & (1<<MD_DISK_SYNC)))
780 continue;
781
782 /* We have found a good disk to use! */
783 *diskp = disk->next;
784 disk->next = sra->devs;
785 sra->devs = disk;
786 found = 1;
787 break;
788 }
789 if (!found)
790 break;
791 }
792
793 if (slot < sra->array.raid_disks) {
794 /* didn't find all slots */
795 struct mdinfo **e;
796 e = &remaining;
797 while (*e)
798 e = &(*e)->next;
799 *e = sra->devs;
800 sra->devs = remaining;
801 return 1;
802 }
803
804 /* Remove all 'remaining' devices from the array */
805 while (remaining) {
806 struct mdinfo *sd = remaining;
807 remaining = sd->next;
808
809 sysfs_set_str(sra, sd, "state", "faulty");
810 sysfs_set_str(sra, sd, "slot", "none");
d5ca4a23
KW
811 /* for external metadata disks should be removed in mdmon */
812 if (!st->ss->external)
813 sysfs_set_str(sra, sd, "state", "remove");
62a48395
AK
814 sd->disk.state |= (1<<MD_DISK_REMOVED);
815 sd->disk.state &= ~(1<<MD_DISK_SYNC);
816 sd->next = sra->devs;
817 sra->devs = sd;
818 }
819 return 0;
820}
821
130994cb
AK
822void reshape_free_fdlist(int *fdlist,
823 unsigned long long *offsets,
824 int size)
825{
826 int i;
827
828 for (i = 0; i < size; i++)
829 if (fdlist[i] >= 0)
830 close(fdlist[i]);
831
832 free(fdlist);
833 free(offsets);
834}
835
836int reshape_prepare_fdlist(char *devname,
837 struct mdinfo *sra,
838 int raid_disks,
839 int nrdisks,
840 unsigned long blocks,
841 char *backup_file,
842 int *fdlist,
843 unsigned long long *offsets)
844{
845 int d = 0;
846 struct mdinfo *sd;
847
848 for (d = 0; d <= nrdisks; d++)
849 fdlist[d] = -1;
850 d = raid_disks;
851 for (sd = sra->devs; sd; sd = sd->next) {
852 if (sd->disk.state & (1<<MD_DISK_FAULTY))
853 continue;
854 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
855 char *dn = map_dev(sd->disk.major,
856 sd->disk.minor, 1);
857 fdlist[sd->disk.raid_disk]
858 = dev_open(dn, O_RDONLY);
859 offsets[sd->disk.raid_disk] = sd->data_offset*512;
860 if (fdlist[sd->disk.raid_disk] < 0) {
e7b84f9d
N
861 pr_err("%s: cannot open component %s\n",
862 devname, dn ? dn : "-unknown-");
130994cb
AK
863 d = -1;
864 goto release;
865 }
866 } else if (backup_file == NULL) {
867 /* spare */
868 char *dn = map_dev(sd->disk.major,
869 sd->disk.minor, 1);
870 fdlist[d] = dev_open(dn, O_RDWR);
871 offsets[d] = (sd->data_offset + sra->component_size - blocks - 8)*512;
872 if (fdlist[d] < 0) {
e7b84f9d 873 pr_err("%s: cannot open component %s\n",
130994cb
AK
874 devname, dn ? dn : "-unknown-");
875 d = -1;
876 goto release;
877 }
878 d++;
879 }
880 }
881release:
882 return d;
883}
884
e6e9d47b
AK
885int reshape_open_backup_file(char *backup_file,
886 int fd,
887 char *devname,
888 long blocks,
889 int *fdlist,
a93f87ee
N
890 unsigned long long *offsets,
891 int restart)
e6e9d47b
AK
892{
893 /* Return 1 on success, 0 on any form of failure */
894 /* need to check backup file is large enough */
895 char buf[512];
896 struct stat stb;
897 unsigned int dev;
898 int i;
899
a93f87ee 900 *fdlist = open(backup_file, O_RDWR|O_CREAT|(restart ? O_TRUNC : O_EXCL),
e6e9d47b
AK
901 S_IRUSR | S_IWUSR);
902 *offsets = 8 * 512;
903 if (*fdlist < 0) {
e7b84f9d 904 pr_err("%s: cannot create backup file %s: %s\n",
e6e9d47b
AK
905 devname, backup_file, strerror(errno));
906 return 0;
907 }
908 /* Guard against backup file being on array device.
909 * If array is partitioned or if LVM etc is in the
910 * way this will not notice, but it is better than
911 * nothing.
912 */
913 fstat(*fdlist, &stb);
914 dev = stb.st_dev;
915 fstat(fd, &stb);
916 if (stb.st_rdev == dev) {
e7b84f9d 917 pr_err("backup file must NOT be"
e6e9d47b
AK
918 " on the array being reshaped.\n");
919 close(*fdlist);
920 return 0;
921 }
922
923 memset(buf, 0, 512);
ca4fe0bf 924 for (i=0; i < blocks + 8 ; i++) {
e6e9d47b 925 if (write(*fdlist, buf, 512) != 512) {
e7b84f9d 926 pr_err("%s: cannot create"
e6e9d47b
AK
927 " backup file %s: %s\n",
928 devname, backup_file, strerror(errno));
929 return 0;
930 }
931 }
932 if (fsync(*fdlist) != 0) {
e7b84f9d 933 pr_err("%s: cannot create backup file %s: %s\n",
e6e9d47b
AK
934 devname, backup_file, strerror(errno));
935 return 0;
936 }
937
938 return 1;
939}
940
b48e2e25
N
941unsigned long GCD(unsigned long a, unsigned long b)
942{
943 while (a != b) {
944 if (a < b)
945 b -= a;
946 if (b < a)
947 a -= b;
948 }
949 return a;
950}
951
1c009fc2
AK
952unsigned long compute_backup_blocks(int nchunk, int ochunk,
953 unsigned int ndata, unsigned int odata)
954{
955 unsigned long a, b, blocks;
956 /* So how much do we need to backup.
957 * We need an amount of data which is both a whole number of
958 * old stripes and a whole number of new stripes.
959 * So LCM for (chunksize*datadisks).
960 */
961 a = (ochunk/512) * odata;
962 b = (nchunk/512) * ndata;
963 /* Find GCD */
b48e2e25 964 a = GCD(a, b);
1c009fc2
AK
965 /* LCM == product / GCD */
966 blocks = (ochunk/512) * (nchunk/512) * odata * ndata / a;
967
968 return blocks;
969}
970
5da9ab98
N
971char *analyse_change(struct mdinfo *info, struct reshape *re)
972{
973 /* Based on the current array state in info->array and
974 * the changes in info->new_* etc, determine:
975 * - whether the change is possible
976 * - Intermediate level/raid_disks/layout
977 * - whether a restriping reshape is needed
978 * - number of sectors in minimum change unit. This
979 * will cover a whole number of stripes in 'before' and
980 * 'after'.
981 *
982 * Return message if the change should be rejected
983 * NULL if the change can be achieved
984 *
985 * This can be called as part of starting a reshape, or
986 * when assembling an array that is undergoing reshape.
987 */
19ceb16d 988 int near, far, offset, copies;
5da9ab98 989 int new_disks;
19ceb16d 990 int old_chunk, new_chunk;
b545e14a
N
991 /* delta_parity records change in number of devices
992 * caused by level change
993 */
994 int delta_parity = 0;
5da9ab98
N
995
996 /* If a new level not explicitly given, we assume no-change */
997 if (info->new_level == UnSet)
998 info->new_level = info->array.level;
999
1000 if (info->new_chunk)
1001 switch (info->new_level) {
1002 case 0:
1003 case 4:
1004 case 5:
1005 case 6:
1006 case 10:
1007 /* chunk size is meaningful, must divide component_size
1008 * evenly
1009 */
1010 if (info->component_size % (info->new_chunk/512))
1011 return "New chunk size does not"
1012 " divide component size";
1013 break;
1014 default:
1015 return "chunk size not meaningful for this level";
1016 }
1017 else
1018 info->new_chunk = info->array.chunk_size;
1019
1020 switch (info->array.level) {
90b60dfa
N
1021 default:
1022 return "Cannot understand this RAID level";
5da9ab98
N
1023 case 1:
1024 /* RAID1 can convert to RAID1 with different disks, or
dfe77a9e
KW
1025 * raid5 with 2 disks, or
1026 * raid0 with 1 disk
5da9ab98 1027 */
5ca3a902
N
1028 if (info->new_level > 1 &&
1029 (info->component_size & 7))
1030 return "Cannot convert RAID1 of this size - "
1031 "reduce size to multiple of 4K first.";
dfe77a9e 1032 if (info->new_level == 0) {
b545e14a
N
1033 if (info->delta_disks != UnSet &&
1034 info->delta_disks != 0)
1035 return "Cannot change number of disks "
1036 "with RAID1->RAID0 conversion";
dfe77a9e
KW
1037 re->level = 0;
1038 re->before.data_disks = 1;
1039 re->after.data_disks = 1;
1040 re->before.layout = 0;
1041 re->backup_blocks = 0;
1042 re->parity = 0;
1043 return NULL;
1044 }
5da9ab98
N
1045 if (info->new_level == 1) {
1046 if (info->delta_disks == UnSet)
1047 /* Don't know what to do */
1048 return "no change requested for Growing RAID1";
1049 re->level = 1;
b4f8e38b 1050 re->backup_blocks = 0;
5da9ab98
N
1051 re->parity = 0;
1052 return NULL;
1053 }
1054 if (info->array.raid_disks == 2 &&
446d2a5a 1055 info->new_level == 5) {
5652f2d9 1056
5da9ab98 1057 re->level = 5;
5da9ab98 1058 re->before.data_disks = 1;
5652f2d9
N
1059 if (info->delta_disks != UnSet &&
1060 info->delta_disks != 0)
1061 re->after.data_disks = 1 + info->delta_disks;
1062 else
1063 re->after.data_disks = 1;
1064 if (re->after.data_disks < 1)
1065 return "Number of disks too small for RAID5";
1066
5da9ab98 1067 re->before.layout = ALGORITHM_LEFT_SYMMETRIC;
446d2a5a
N
1068 info->array.chunk_size = 65536;
1069 break;
5da9ab98
N
1070 }
1071 /* Could do some multi-stage conversions, but leave that to
1072 * later.
1073 */
1074 return "Impossibly level change request for RAID1";
1075
1076 case 10:
19ceb16d
N
1077 /* RAID10 can be converted from near mode to
1078 * RAID0 by removing some devices.
1079 * It can also be reshaped if the kernel supports
1080 * new_data_offset.
5da9ab98 1081 */
19ceb16d
N
1082 switch (info->new_level) {
1083 case 0:
1084 if ((info->array.layout & ~0xff) != 0x100)
1085 return "Cannot Grow RAID10 with far/offset layout";
1086 /* number of devices must be multiple of number of copies */
1087 if (info->array.raid_disks % (info->array.layout & 0xff))
1088 return "RAID10 layout too complex for Grow operation";
1089
1090 new_disks = (info->array.raid_disks
1091 / (info->array.layout & 0xff));
1092 if (info->delta_disks == UnSet)
1093 info->delta_disks = (new_disks
1094 - info->array.raid_disks);
5da9ab98 1095
19ceb16d
N
1096 if (info->delta_disks != new_disks - info->array.raid_disks)
1097 return "New number of raid-devices impossible for RAID10";
1098 if (info->new_chunk &&
1099 info->new_chunk != info->array.chunk_size)
1100 return "Cannot change chunk-size with RAID10 Grow";
1101
1102 /* looks good */
1103 re->level = 0;
1104 re->parity = 0;
1105 re->before.data_disks = new_disks;
1106 re->after.data_disks = re->before.data_disks;
1107 re->before.layout = 0;
1108 re->backup_blocks = 0;
1109 return NULL;
1110
1111 case 10:
1112 near = info->array.layout & 0xff;
1113 far = (info->array.layout >> 8) & 0xff;
1114 offset = info->array.layout & 0x10000;
1115 if (far > 1 && !offset)
1116 return "Cannot reshape RAID10 in far-mode";
1117 copies = near * far;
1118
1119 old_chunk = info->array.chunk_size * far;
1120
1121 if (info->new_layout == UnSet)
1122 info->new_layout = info->array.layout;
1123 else {
1124 near = info->new_layout & 0xff;
1125 far = (info->new_layout >> 8) & 0xff;
1126 offset = info->new_layout & 0x10000;
1127 if (far > 1 && !offset)
1128 return "Cannot reshape RAID10 to far-mode";
1129 if (near * far != copies)
1130 return "Cannot change number of copies"
1131 " when reshaping RAID10";
1132 }
1133 if (info->delta_disks == UnSet)
1134 info->delta_disks = 0;
1135 new_disks = (info->array.raid_disks +
1136 info->delta_disks);
1137
1138 new_chunk = info->new_chunk * far;
1139
1140 re->level = 10;
1141 re->parity = 0;
1142 re->before.layout = info->array.layout;
1143 re->before.data_disks = info->array.raid_disks;
1144 re->after.layout = info->new_layout;
1145 re->after.data_disks = new_disks;
1146 /* For RAID10 we don't do backup, and there is
1147 * no need to synchronise stripes on both
1148 * 'old' and 'new'. So the important
1149 * number is the minimum data_offset difference
1150 * which is the larger of (offset copies * chunk).
1151 */
1152
1153 re->backup_blocks = max(old_chunk, new_chunk) / 512;
ee2429e0
N
1154 if (new_disks < re->before.data_disks &&
1155 info->space_after < re->backup_blocks)
1156 /* Reduce component size by one chunk */
1157 re->new_size = (info->component_size -
1158 re->backup_blocks);
1159 else
1160 re->new_size = info->component_size;
1161 re->new_size = re->new_size * new_disks / copies;
19ceb16d 1162 return NULL;
5da9ab98 1163
19ceb16d
N
1164 default:
1165 return "RAID10 can only be changed to RAID0";
1166 }
5da9ab98
N
1167 case 0:
1168 /* RAID0 can be converted to RAID10, or to RAID456 */
1169 if (info->new_level == 10) {
1170 if (info->new_layout == UnSet && info->delta_disks == UnSet) {
1171 /* Assume near=2 layout */
1172 info->new_layout = 0x102;
1173 info->delta_disks = info->array.raid_disks;
1174 }
1175 if (info->new_layout == UnSet) {
1176 int copies = 1 + (info->delta_disks
1177 / info->array.raid_disks);
1178 if (info->array.raid_disks * (copies-1)
1179 != info->delta_disks)
1180 return "Impossible number of devices"
1181 " for RAID0->RAID10";
1182 info->new_layout = 0x100 + copies;
1183 }
1184 if (info->delta_disks == UnSet) {
1185 int copies = info->new_layout & 0xff;
1186 if (info->new_layout != 0x100 + copies)
1187 return "New layout impossible"
1188 " for RAID0->RAID10";;
1189 info->delta_disks = (copies - 1) *
1190 info->array.raid_disks;
1191 }
1192 if (info->new_chunk &&
1193 info->new_chunk != info->array.chunk_size)
1194 return "Cannot change chunk-size with RAID0->RAID10";
1195 /* looks good */
1196 re->level = 10;
1197 re->parity = 0;
1198 re->before.data_disks = (info->array.raid_disks +
1199 info->delta_disks);
031d445c 1200 re->after.data_disks = re->before.data_disks;
5da9ab98 1201 re->before.layout = info->new_layout;
b4f8e38b 1202 re->backup_blocks = 0;
5da9ab98
N
1203 return NULL;
1204 }
1205
1206 /* RAID0 can also covert to RAID0/4/5/6 by first converting to
1207 * a raid4 style layout of the final level.
1208 */
1209 switch (info->new_level) {
5da9ab98 1210 case 4:
b545e14a
N
1211 delta_parity = 1;
1212 case 0:
5da9ab98
N
1213 re->level = 4;
1214 re->before.layout = 0;
1215 break;
1216 case 5:
b545e14a 1217 delta_parity = 1;
5da9ab98
N
1218 re->level = 5;
1219 re->before.layout = ALGORITHM_PARITY_N;
1220 break;
1221 case 6:
b545e14a 1222 delta_parity = 2;
5da9ab98
N
1223 re->level = 6;
1224 re->before.layout = ALGORITHM_PARITY_N;
1225 break;
1226 default:
1227 return "Impossible level change requested";
1228 }
1229 re->before.data_disks = info->array.raid_disks;
1230 /* determining 'after' layout happens outside this 'switch' */
1231 break;
1232
1233 case 4:
1234 info->array.layout = ALGORITHM_PARITY_N;
1235 case 5:
1236 switch (info->new_level) {
f0cce442 1237 case 0:
b545e14a 1238 delta_parity = -1;
5da9ab98
N
1239 case 4:
1240 re->level = info->array.level;
1241 re->before.data_disks = info->array.raid_disks - 1;
1242 re->before.layout = info->array.layout;
1243 break;
1244 case 5:
1245 re->level = 5;
1246 re->before.data_disks = info->array.raid_disks - 1;
1247 re->before.layout = info->array.layout;
1248 break;
1249 case 6:
b545e14a 1250 delta_parity = 1;
5da9ab98
N
1251 re->level = 6;
1252 re->before.data_disks = info->array.raid_disks - 1;
1253 switch (info->array.layout) {
1254 case ALGORITHM_LEFT_ASYMMETRIC:
1255 re->before.layout = ALGORITHM_LEFT_ASYMMETRIC_6;
1256 break;
1257 case ALGORITHM_RIGHT_ASYMMETRIC:
1258 re->before.layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
1259 break;
1260 case ALGORITHM_LEFT_SYMMETRIC:
1261 re->before.layout = ALGORITHM_LEFT_SYMMETRIC_6;
1262 break;
1263 case ALGORITHM_RIGHT_SYMMETRIC:
1264 re->before.layout = ALGORITHM_RIGHT_SYMMETRIC_6;
1265 break;
1266 case ALGORITHM_PARITY_0:
1267 re->before.layout = ALGORITHM_PARITY_0_6;
1268 break;
1269 case ALGORITHM_PARITY_N:
1270 re->before.layout = ALGORITHM_PARITY_N_6;
1271 break;
1272 default:
1273 return "Cannot convert an array with this layout";
1274 }
1275 break;
1276 case 1:
1277 if (info->array.raid_disks != 2)
1278 return "Can only convert a 2-device array to RAID1";
b545e14a
N
1279 if (info->delta_disks != UnSet &&
1280 info->delta_disks != 0)
1281 return "Cannot set raid_disk when "
1282 "converting RAID5->RAID1";
5da9ab98 1283 re->level = 1;
5da9ab98
N
1284 break;
1285 default:
1286 return "Impossible level change requested";
1287 }
1288 break;
1289 case 6:
1290 switch (info->new_level) {
1291 case 4:
1292 case 5:
b545e14a 1293 delta_parity = -1;
5da9ab98
N
1294 case 6:
1295 re->level = 6;
1296 re->before.data_disks = info->array.raid_disks - 2;
1297 re->before.layout = info->array.layout;
1298 break;
1299 default:
1300 return "Impossible level change requested";
1301 }
1302 break;
1303 }
1304
1305 /* If we reached here then it looks like a re-stripe is
1306 * happening. We have determined the intermediate level
1307 * and initial raid_disks/layout and stored these in 're'.
1308 *
1309 * We need to deduce the final layout that can be atomically
1310 * converted to the end state.
1311 */
1312 switch (info->new_level) {
1313 case 0:
1314 /* We can only get to RAID0 from RAID4 or RAID5
1315 * with appropriate layout and one extra device
1316 */
1317 if (re->level != 4 && re->level != 5)
1318 return "Cannot covert to RAID0 from this level";
b545e14a 1319
5da9ab98
N
1320 switch (re->level) {
1321 case 4:
ca3b6696
N
1322 re->after.layout = 0;
1323 break;
5da9ab98 1324 case 5:
ca3b6696
N
1325 re->after.layout = ALGORITHM_PARITY_N;
1326 break;
5da9ab98
N
1327 }
1328 break;
1329
1330 case 4:
1331 /* We can only get to RAID4 from RAID5 */
1332 if (re->level != 4 && re->level != 5)
1333 return "Cannot convert to RAID4 from this level";
b545e14a 1334
5da9ab98
N
1335 switch (re->level) {
1336 case 4:
ca3b6696
N
1337 re->after.layout = 0;
1338 break;
5da9ab98 1339 case 5:
ca3b6696
N
1340 re->after.layout = ALGORITHM_PARITY_N;
1341 break;
5da9ab98
N
1342 }
1343 break;
1344
1345 case 5:
0073a6e1 1346 /* We get to RAID5 from RAID5 or RAID6 */
5da9ab98
N
1347 if (re->level != 5 && re->level != 6)
1348 return "Cannot convert to RAID5 from this level";
b545e14a 1349
5da9ab98
N
1350 switch (re->level) {
1351 case 5:
1352 if (info->new_layout == UnSet)
1353 re->after.layout = re->before.layout;
1354 else
1355 re->after.layout = info->new_layout;
1356 break;
1357 case 6:
4a870364
N
1358 if (info->new_layout == UnSet)
1359 info->new_layout = re->before.layout;
1360
5da9ab98
N
1361 /* after.layout needs to be raid6 version of new_layout */
1362 if (info->new_layout == ALGORITHM_PARITY_N)
1363 re->after.layout = ALGORITHM_PARITY_N;
1364 else {
1365 char layout[40];
1366 char *ls = map_num(r5layout, info->new_layout);
1367 int l;
0073a6e1
N
1368 if (ls) {
1369 /* Current RAID6 layout has a RAID5
1370 * equivalent - good
1371 */
1372 strcat(strcpy(layout, ls), "-6");
1373 l = map_name(r6layout, layout);
1374 if (l == UnSet)
1375 return "Cannot find RAID6 layout"
1376 " to convert to";
1377 } else {
1378 /* Current RAID6 has no equivalent.
1379 * If it is already a '-6' layout we
1380 * can leave it unchanged, else we must
1381 * fail
1382 */
1383 ls = map_num(r6layout, info->new_layout);
1384 if (!ls ||
1385 strcmp(ls+strlen(ls)-2, "-6") != 0)
1386 return "Please specify new layout";
1387 l = info->new_layout;
1388 }
5da9ab98
N
1389 re->after.layout = l;
1390 }
1391 }
1392 break;
1393
1394 case 6:
1395 /* We must already be at level 6 */
1396 if (re->level != 6)
1397 return "Impossible level change";
5da9ab98 1398 if (info->new_layout == UnSet)
4a870364 1399 re->after.layout = info->array.layout;
5da9ab98
N
1400 else
1401 re->after.layout = info->new_layout;
1402 break;
1403 default:
1404 return "Impossible level change requested";
1405 }
b545e14a
N
1406 if (info->delta_disks == UnSet)
1407 info->delta_disks = delta_parity;
1408
1409 re->after.data_disks = (re->before.data_disks
1410 + info->delta_disks
1411 - delta_parity);
5da9ab98 1412 switch (re->level) {
ca3b6696
N
1413 case 6: re->parity = 2;
1414 break;
5da9ab98 1415 case 4:
ca3b6696
N
1416 case 5: re->parity = 1;
1417 break;
1418 default: re->parity = 0;
1419 break;
5da9ab98
N
1420 }
1421 /* So we have a restripe operation, we need to calculate the number
1422 * of blocks per reshape operation.
1423 */
1424 if (info->new_chunk == 0)
1425 info->new_chunk = info->array.chunk_size;
1426 if (re->after.data_disks == re->before.data_disks &&
1427 re->after.layout == re->before.layout &&
1428 info->new_chunk == info->array.chunk_size) {
1429 /* Nothing to change */
b4f8e38b 1430 re->backup_blocks = 0;
5da9ab98
N
1431 return NULL;
1432 }
1433 if (re->after.data_disks == 1 && re->before.data_disks == 1) {
446d2a5a 1434 /* chunk and layout changes make no difference */
b4f8e38b 1435 re->backup_blocks = 0;
5da9ab98
N
1436 return NULL;
1437 }
1438
1439 if (re->after.data_disks == re->before.data_disks &&
1440 get_linux_version() < 2006032)
1441 return "in-place reshape is not safe before 2.6.32 - sorry.";
1442
1443 if (re->after.data_disks < re->before.data_disks &&
1444 get_linux_version() < 2006030)
508ede86 1445 return "reshape to fewer devices is not supported before 2.6.30 - sorry.";
5da9ab98 1446
b4f8e38b 1447 re->backup_blocks = compute_backup_blocks(
5da9ab98
N
1448 info->new_chunk, info->array.chunk_size,
1449 re->after.data_disks,
1450 re->before.data_disks);
1451
1452 re->new_size = info->component_size * re->after.data_disks;
1453 return NULL;
1454}
1455
54397ed9
AK
1456static int set_array_size(struct supertype *st, struct mdinfo *sra,
1457 char *text_version)
1458{
1459 struct mdinfo *info;
1460 char *subarray;
1461 int ret_val = -1;
1462
1463 if ((st == NULL) || (sra == NULL))
1464 return ret_val;
1465
1466 if (text_version == NULL)
1467 text_version = sra->text_version;
1468 subarray = strchr(text_version+1, '/')+1;
1469 info = st->ss->container_content(st, subarray);
1470 if (info) {
1471 unsigned long long current_size = 0;
1472 unsigned long long new_size =
1473 info->custom_array_size/2;
1474
1475 if (sysfs_get_ll(sra, NULL, "array_size", &current_size) == 0 &&
1476 new_size > current_size) {
1477 if (sysfs_set_num(sra, NULL, "array_size", new_size)
1478 < 0)
1479 dprintf("Error: Cannot set array size");
1480 else {
1481 ret_val = 0;
1482 dprintf("Array size changed");
1483 }
1484 dprintf(" from %llu to %llu.\n",
1485 current_size, new_size);
1486 }
1487 sysfs_free(info);
1488 } else
1489 dprintf("Error: set_array_size(): info pointer in NULL\n");
1490
1491 return ret_val;
1492}
1493
5da9ab98
N
1494static int reshape_array(char *container, int fd, char *devname,
1495 struct supertype *st, struct mdinfo *info,
e2e53a2d 1496 int force, struct mddev_dev *devlist,
19ceb16d 1497 unsigned long long data_offset,
ba728be7 1498 char *backup_file, int verbose, int forked,
b76b30e0 1499 int restart, int freeze_reshape);
493f5dd6 1500static int reshape_container(char *container, char *devname,
9ad6f6e6 1501 int mdfd,
24daa16f 1502 struct supertype *st,
5da9ab98
N
1503 struct mdinfo *info,
1504 int force,
1505 char *backup_file,
ba728be7 1506 int verbose, int restart, int freeze_reshape);
1c009fc2 1507
32754b7d 1508int Grow_reshape(char *devname, int fd,
e2e53a2d 1509 struct mddev_dev *devlist,
40c9a66a 1510 unsigned long long data_offset,
32754b7d 1511 struct context *c, struct shape *s)
e86c9dd6
NB
1512{
1513 /* Make some changes in the shape of an array.
1514 * The kernel must support the change.
7236ee7a
N
1515 *
1516 * There are three different changes. Each can trigger
1517 * a resync or recovery so we freeze that until we have
1518 * requested everything (if kernel supports freezing - 2.6.30).
1519 * The steps are:
1520 * - change size (i.e. component_size)
1521 * - change level
1522 * - change layout/chunksize/ndisks
1523 *
1524 * The last can require a reshape. It is different on different
1525 * levels so we need to check the level before actioning it.
1526 * Some times the level change needs to be requested after the
1527 * reshape (e.g. raid6->raid5, raid5->raid0)
1528 *
e86c9dd6 1529 */
5da9ab98 1530 struct mdu_array_info_s array;
7236ee7a 1531 int rv = 0;
e86c9dd6 1532 struct supertype *st;
4725bc31 1533 char *subarray = NULL;
e86c9dd6 1534
7236ee7a 1535 int frozen;
7236ee7a 1536 int changed = 0;
7bc71196 1537 char *container = NULL;
5da9ab98 1538 char container_buf[20];
7bc71196 1539 int cfd = -1;
e86c9dd6 1540
e2e53a2d
N
1541 struct mddev_dev *dv;
1542 int added_disks;
1543
5da9ab98 1544 struct mdinfo info;
7e0f6979 1545 struct mdinfo *sra;
e86c9dd6 1546
40c9a66a 1547
e86c9dd6 1548 if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) {
19ceb16d 1549 fprintf(stderr, Name ": %s is not an active md array - aborting\n",
e86c9dd6
NB
1550 devname);
1551 return 1;
1552 }
19ceb16d
N
1553 if (data_offset != INVALID_SECTORS && array.level != 10) {
1554 pr_err("--grow --data-offset not yet supported\n");
1555 return 1;
1556 }
24d40069 1557
32754b7d
N
1558 if (s->size > 0 &&
1559 (s->chunk || s->level!= UnSet || s->layout_str || s->raiddisks)) {
e7b84f9d 1560 pr_err("cannot change component size at the same time "
9ce510be
N
1561 "as other changes.\n"
1562 " Change size first, then check data is intact before "
1563 "making other changes.\n");
1564 return 1;
1565 }
1566
32754b7d 1567 if (s->raiddisks && s->raiddisks < array.raid_disks && array.level > 1 &&
24d40069
N
1568 get_linux_version() < 2006032 &&
1569 !check_env("MDADM_FORCE_FEWER")) {
e7b84f9d 1570 pr_err("reducing the number of devices is not safe before Linux 2.6.32\n"
24d40069
N
1571 " Please use a newer kernel\n");
1572 return 1;
1573 }
7bc71196
DW
1574
1575 st = super_by_fd(fd, &subarray);
1576 if (!st) {
e7b84f9d 1577 pr_err("Unable to determine metadata format for %s\n", devname);
7bc71196
DW
1578 return 1;
1579 }
32754b7d 1580 if (s->raiddisks > st->max_devs) {
e7b84f9d 1581 pr_err("Cannot increase raid-disks on this array"
acab7bb1
N
1582 " beyond %d\n", st->max_devs);
1583 return 1;
1584 }
7bc71196
DW
1585
1586 /* in the external case we need to check that the requested reshape is
1587 * supported, and perform an initial check that the container holds the
1588 * pre-requisite spare devices (mdmon owns final validation)
1589 */
1590 if (st->ss->external) {
1591 int container_dev;
7f2ba464 1592 int rv;
7bc71196
DW
1593
1594 if (subarray) {
1595 container_dev = st->container_dev;
1596 cfd = open_dev_excl(st->container_dev);
7bc71196
DW
1597 } else {
1598 container_dev = st->devnum;
1599 close(fd);
1600 cfd = open_dev_excl(st->devnum);
1601 fd = cfd;
1602 }
1603 if (cfd < 0) {
e7b84f9d 1604 pr_err("Unable to open container for %s\n",
7bc71196 1605 devname);
7f2ba464 1606 free(subarray);
7bc71196
DW
1607 return 1;
1608 }
1609
5da9ab98
N
1610 fmt_devname(container_buf, container_dev);
1611 container = container_buf;
7bc71196 1612
eb744788
AK
1613 rv = st->ss->load_container(st, cfd, NULL);
1614
7f2ba464 1615 if (rv) {
e7b84f9d 1616 pr_err("Cannot read superblock for %s\n",
7bc71196 1617 devname);
7f2ba464 1618 free(subarray);
7bc71196
DW
1619 return 1;
1620 }
1621
81219e70
LM
1622 /* check if operation is supported for metadata handler */
1623 if (st->ss->container_content) {
1624 struct mdinfo *cc = NULL;
1625 struct mdinfo *content = NULL;
1626
1627 cc = st->ss->container_content(st, subarray);
1628 for (content = cc; content ; content = content->next) {
446894ea 1629 int allow_reshape = 1;
81219e70
LM
1630
1631 /* check if reshape is allowed based on metadata
1632 * indications stored in content.array.status
1633 */
446894ea
N
1634 if (content->array.state & (1<<MD_SB_BLOCK_VOLUME))
1635 allow_reshape = 0;
1636 if (content->array.state
1637 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE))
1638 allow_reshape = 0;
81219e70 1639 if (!allow_reshape) {
e7b84f9d
N
1640 pr_err("cannot reshape arrays in"
1641 " container with unsupported"
1642 " metadata: %s(%s)\n",
1643 devname, container_buf);
81219e70
LM
1644 sysfs_free(cc);
1645 free(subarray);
1646 return 1;
1647 }
1648 }
1649 sysfs_free(cc);
1650 }
7bc71196
DW
1651 if (mdmon_running(container_dev))
1652 st->update_tail = &st->updates;
e2e53a2d 1653 }
691a36b7 1654
e2e53a2d
N
1655 added_disks = 0;
1656 for (dv = devlist; dv; dv = dv->next)
1657 added_disks++;
32754b7d
N
1658 if (s->raiddisks > array.raid_disks &&
1659 array.spare_disks +added_disks < (s->raiddisks - array.raid_disks) &&
1660 !c->force) {
e7b84f9d
N
1661 pr_err("Need %d spare%s to avoid degraded array,"
1662 " and only have %d.\n"
1663 " Use --force to over-ride this check.\n",
32754b7d
N
1664 s->raiddisks - array.raid_disks,
1665 s->raiddisks - array.raid_disks == 1 ? "" : "s",
e7b84f9d 1666 array.spare_disks + added_disks);
691a36b7 1667 return 1;
7bc71196
DW
1668 }
1669
3fa436ac
ML
1670 sra = sysfs_read(fd, 0, GET_LEVEL | GET_DISKS | GET_DEVS
1671 | GET_STATE | GET_VERSION);
24daa16f 1672 if (sra) {
7f2ba464 1673 if (st->ss->external && subarray == NULL) {
7bc71196
DW
1674 array.level = LEVEL_CONTAINER;
1675 sra->array.level = LEVEL_CONTAINER;
1676 }
7bc71196 1677 } else {
e7b84f9d 1678 pr_err("failed to read sysfs parameters for %s\n",
b526e52d
DW
1679 devname);
1680 return 1;
1681 }
7f2ba464
DW
1682 frozen = freeze(st);
1683 if (frozen < -1) {
1684 /* freeze() already spewed the reason */
8e61e0d7 1685 sysfs_free(sra);
7f2ba464
DW
1686 return 1;
1687 } else if (frozen < 0) {
e7b84f9d 1688 pr_err("%s is performing resync/recovery and cannot"
7236ee7a 1689 " be reshaped\n", devname);
8e61e0d7 1690 sysfs_free(sra);
7236ee7a
N
1691 return 1;
1692 }
1693
1694 /* ========= set size =============== */
887a7a9e 1695 if (s->size > 0 && (s->size == MAX_SIZE || s->size != (unsigned)array.size)) {
d04f65f4
N
1696 unsigned long long orig_size = get_component_size(fd)/2;
1697 unsigned long long min_csize;
d1537ed1 1698 struct mdinfo *mdi;
44f6f181 1699 int raid0_takeover = 0;
7bc71196 1700
85f10287 1701 if (orig_size == 0)
d04f65f4 1702 orig_size = (unsigned) array.size;
85f10287 1703
32754b7d
N
1704 if (reshape_super(st, s->size, UnSet, UnSet, 0, 0, UnSet, NULL,
1705 devname, APPLY_METADATA_CHANGES, c->verbose > 0)) {
7bc71196
DW
1706 rv = 1;
1707 goto release;
1708 }
1709 sync_metadata(st);
7e7e9a4d
AK
1710 if (st->ss->external) {
1711 /* metadata can have size limitation
1712 * update size value according to metadata information
1713 */
1714 struct mdinfo *sizeinfo =
1715 st->ss->container_content(st, subarray);
1716 if (sizeinfo) {
1717 unsigned long long new_size =
1718 sizeinfo->custom_array_size/2;
1719 int data_disks = get_data_disks(
1720 sizeinfo->array.level,
1721 sizeinfo->array.layout,
1722 sizeinfo->array.raid_disks);
1723 new_size /= data_disks;
1724 dprintf("Metadata size correction from %llu to "
1725 "%llu (%llu)\n", orig_size, new_size,
1726 new_size * data_disks);
32754b7d 1727 s->size = new_size;
7e7e9a4d
AK
1728 sysfs_free(sizeinfo);
1729 }
1730 }
d1537ed1
N
1731
1732 /* Update the size of each member device in case
1733 * they have been resized. This will never reduce
1734 * below the current used-size. The "size" attribute
20a46756 1735 * understands '0' to mean 'max'.
d1537ed1 1736 */
20a46756 1737 min_csize = 0;
65a9798b 1738 rv = 0;
20a46756 1739 for (mdi = sra->devs; mdi; mdi = mdi->next) {
d04f65f4 1740 if (sysfs_set_num(sra, mdi, "size",
32754b7d 1741 s->size == MAX_SIZE ? 0 : s->size) < 0) {
b0a658ff
N
1742 /* Probably kernel refusing to let us
1743 * reduce the size - not an error.
1744 */
20a46756 1745 break;
65a9798b 1746 }
20a46756
N
1747 if (array.not_persistent == 0 &&
1748 array.major_version == 0 &&
1749 get_linux_version() < 3001000) {
1750 /* Dangerous to allow size to exceed 2TB */
1751 unsigned long long csize;
1752 if (sysfs_get_ll(sra, mdi, "size", &csize) == 0) {
1753 if (csize >= 2ULL*1024*1024*1024)
1754 csize = 2ULL*1024*1024*1024;
1755 if ((min_csize == 0 || (min_csize
d04f65f4 1756 > csize)))
20a46756
N
1757 min_csize = csize;
1758 }
1759 }
1760 }
65a9798b 1761 if (rv) {
e7b84f9d 1762 pr_err("Cannot set size on "
65a9798b
AK
1763 "array members.\n");
1764 goto size_change_error;
1765 }
32754b7d 1766 if (min_csize && s->size > min_csize) {
e7b84f9d 1767 pr_err("Cannot safely make this array "
20a46756
N
1768 "use more than 2TB per device on this kernel.\n");
1769 rv = 1;
65a9798b 1770 goto size_change_error;
20a46756 1771 }
32754b7d 1772 if (min_csize && s->size == MAX_SIZE) {
20a46756
N
1773 /* Don't let the kernel choose a size - it will get
1774 * it wrong
1775 */
e7b84f9d
N
1776 pr_err("Limited v0.90 array to "
1777 "2TB per device\n");
32754b7d 1778 s->size = min_csize;
20a46756 1779 }
44f6f181
AK
1780 if (st->ss->external) {
1781 if (sra->array.level == 0) {
1782 rv = sysfs_set_str(sra, NULL, "level",
1783 "raid5");
1784 if (!rv) {
1785 raid0_takeover = 1;
1786 /* get array parametes after takeover
1787 * to chane one parameter at time only
1788 */
1789 rv = ioctl(fd, GET_ARRAY_INFO, &array);
1790 }
1791 }
1792 /* make sure mdmon is
1793 * aware of the new level */
1794 if (!mdmon_running(st->container_dev))
1795 start_mdmon(st->container_dev);
1796 ping_monitor(container);
1797 if (mdmon_running(st->container_dev) &&
1798 st->update_tail == NULL)
1799 st->update_tail = &st->updates;
1800 }
1801
0a8b92a6
N
1802 if (s->size == MAX_SIZE)
1803 s->size = 0;
1804 array.size = s->size;
32754b7d 1805 if ((unsigned)array.size != s->size) {
7236ee7a
N
1806 /* got truncated to 32bit, write to
1807 * component_size instead
1808 */
1809 if (sra)
1810 rv = sysfs_set_num(sra, NULL,
32754b7d 1811 "component_size", s->size);
7236ee7a
N
1812 else
1813 rv = -1;
54397ed9 1814 } else {
7236ee7a 1815 rv = ioctl(fd, SET_ARRAY_INFO, &array);
44f6f181 1816
54397ed9
AK
1817 /* manage array size when it is managed externally
1818 */
1819 if ((rv == 0) && st->ss->external)
1820 rv = set_array_size(st, sra, sra->text_version);
1821 }
1822
44f6f181
AK
1823 if (raid0_takeover) {
1824 /* do not recync non-existing parity,
1825 * we will drop it anyway
1826 */
b51702b8 1827 sysfs_set_str(sra, NULL, "sync_action", "frozen");
44f6f181
AK
1828 /* go back to raid0, drop parity disk
1829 */
1830 sysfs_set_str(sra, NULL, "level", "raid0");
1831 ioctl(fd, GET_ARRAY_INFO, &array);
1832 }
1833
65a9798b 1834size_change_error:
7236ee7a 1835 if (rv != 0) {
39bbb392 1836 int err = errno;
7bc71196
DW
1837
1838 /* restore metadata */
1839 if (reshape_super(st, orig_size, UnSet, UnSet, 0, 0,
016e00f5
AK
1840 UnSet, NULL, devname,
1841 ROLLBACK_METADATA_CHANGES,
32754b7d 1842 c->verbose) == 0)
7bc71196 1843 sync_metadata(st);
e7b84f9d 1844 pr_err("Cannot set device size for %s: %s\n",
39bbb392 1845 devname, strerror(err));
24daa16f 1846 if (err == EBUSY &&
39bbb392 1847 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
e7b84f9d 1848 cont_err("Bitmap must be removed before size can be changed\n");
7236ee7a
N
1849 rv = 1;
1850 goto release;
1851 }
32754b7d 1852 if (s->assume_clean) {
6cbf8fb8 1853 /* This will fail on kernels newer than 3.0 unless
ce52f92f
N
1854 * a backport has been arranged.
1855 */
1856 if (sra == NULL ||
1857 sysfs_set_str(sra, NULL, "resync_start", "none") < 0)
e7b84f9d 1858 pr_err("--assume-clean not support with --grow on this kernel\n");
ce52f92f 1859 }
7236ee7a 1860 ioctl(fd, GET_ARRAY_INFO, &array);
32754b7d
N
1861 s->size = get_component_size(fd)/2;
1862 if (s->size == 0)
1863 s->size = array.size;
1864 if (c->verbose >= 0) {
1865 if (s->size == orig_size)
e7b84f9d 1866 pr_err("component size of %s "
85f10287 1867 "unchanged at %lluK\n",
32754b7d 1868 devname, s->size);
85f10287 1869 else
e7b84f9d 1870 pr_err("component size of %s "
85f10287 1871 "has been set to %lluK\n",
32754b7d 1872 devname, s->size);
85f10287 1873 }
7236ee7a 1874 changed = 1;
7bc71196 1875 } else if (array.level != LEVEL_CONTAINER) {
32754b7d
N
1876 s->size = get_component_size(fd)/2;
1877 if (s->size == 0)
1878 s->size = array.size;
7236ee7a
N
1879 }
1880
d515d27f 1881 /* See if there is anything else to do */
32754b7d
N
1882 if ((s->level == UnSet || s->level == array.level) &&
1883 (s->layout_str == NULL) &&
1884 (s->chunk == 0 || s->chunk == array.chunk_size) &&
1885 (s->raiddisks == 0 || s->raiddisks == array.raid_disks)) {
d515d27f 1886 /* Nothing more to do */
32754b7d 1887 if (!changed && c->verbose >= 0)
e7b84f9d 1888 pr_err("%s: no change requested\n",
d515d27f
N
1889 devname);
1890 goto release;
1891 }
1892
dfe77a9e 1893 /* ========= check for Raid10/Raid1 -> Raid0 conversion ===============
5da9ab98 1894 * current implementation assumes that following conditions must be met:
dfe77a9e 1895 * - RAID10:
24daa16f
N
1896 * - far_copies == 1
1897 * - near_copies == 2
62a48395 1898 */
32754b7d 1899 if ((s->level == 0 && array.level == 10 && sra &&
24daa16f 1900 array.layout == ((1 << 8) + 2) && !(array.raid_disks & 1)) ||
32754b7d 1901 (s->level == 0 && array.level == 1 && sra)) {
62a48395 1902 int err;
dfe77a9e 1903 err = remove_disks_for_takeover(st, sra, array.layout);
62a48395
AK
1904 if (err) {
1905 dprintf(Name": Array cannot be reshaped\n");
62a48395
AK
1906 if (cfd > -1)
1907 close(cfd);
5da9ab98
N
1908 rv = 1;
1909 goto release;
7236ee7a 1910 }
fde139b9
N
1911 /* Make sure mdmon has seen the device removal
1912 * and updated metadata before we continue with
1913 * level change
1914 */
1915 if (container)
1916 ping_monitor(container);
7236ee7a
N
1917 }
1918
8ff6d094 1919 memset(&info, 0, sizeof(info));
5da9ab98 1920 info.array = array;
7443ee81 1921 sysfs_init(&info, fd, NoMdDev);
3fa436ac 1922 strcpy(info.text_version, sra->text_version);
32754b7d
N
1923 info.component_size = s->size*2;
1924 info.new_level = s->level;
1925 info.new_chunk = s->chunk * 1024;
eff4954d
N
1926 if (info.array.level == LEVEL_CONTAINER) {
1927 info.delta_disks = UnSet;
32754b7d
N
1928 info.array.raid_disks = s->raiddisks;
1929 } else if (s->raiddisks)
1930 info.delta_disks = s->raiddisks - info.array.raid_disks;
5da9ab98
N
1931 else
1932 info.delta_disks = UnSet;
32754b7d 1933 if (s->layout_str == NULL) {
5da9ab98
N
1934 info.new_layout = UnSet;
1935 if (info.array.level == 6 &&
1936 (info.new_level == 6 || info.new_level == UnSet) &&
1937 info.array.layout >= 16) {
e7b84f9d
N
1938 pr_err("%s has a non-standard layout. If you"
1939 " wish to preserve this\n", devname);
1940 cont_err("during the reshape, please specify"
1941 " --layout=preserve\n");
1942 cont_err("If you want to change it, specify a"
1943 " layout or use --layout=normalise\n");
7236ee7a
N
1944 rv = 1;
1945 goto release;
1946 }
32754b7d
N
1947 } else if (strcmp(s->layout_str, "normalise") == 0 ||
1948 strcmp(s->layout_str, "normalize") == 0) {
5da9ab98
N
1949 /* If we have a -6 RAID6 layout, remove the '-6'. */
1950 info.new_layout = UnSet;
1951 if (info.array.level == 6 && info.new_level == UnSet) {
1952 char l[40], *h;
1953 strcpy(l, map_num(r6layout, info.array.layout));
1954 h = strrchr(l, '-');
1955 if (h && strcmp(h, "-6") == 0) {
1956 *h = 0;
1957 info.new_layout = map_name(r6layout, l);
7236ee7a 1958 }
385167f3 1959 } else {
e7b84f9d 1960 pr_err("%s is only meaningful when reshaping"
32754b7d 1961 " a RAID6 array.\n", s->layout_str);
385167f3
N
1962 rv = 1;
1963 goto release;
7236ee7a 1964 }
32754b7d 1965 } else if (strcmp(s->layout_str, "preserve") == 0) {
385167f3
N
1966 /* This means that a non-standard RAID6 layout
1967 * is OK.
1968 * In particular:
1969 * - When reshape a RAID6 (e.g. adding a device)
1970 * which is in a non-standard layout, it is OK
1971 * to preserve that layout.
1972 * - When converting a RAID5 to RAID6, leave it in
1973 * the XXX-6 layout, don't re-layout.
1974 */
1975 if (info.array.level == 6 && info.new_level == UnSet)
1976 info.new_layout = info.array.layout;
1977 else if (info.array.level == 5 && info.new_level == 6) {
1978 char l[40];
1979 strcpy(l, map_num(r5layout, info.array.layout));
1980 strcat(l, "-6");
1981 info.new_layout = map_name(r6layout, l);
1982 } else {
e7b84f9d 1983 pr_err("%s in only meaningful when reshaping"
32754b7d 1984 " to RAID6\n", s->layout_str);
385167f3
N
1985 rv = 1;
1986 goto release;
1987 }
5da9ab98
N
1988 } else {
1989 int l = info.new_level;
1990 if (l == UnSet)
1991 l = info.array.level;
1992 switch (l) {
1993 case 5:
32754b7d 1994 info.new_layout = map_name(r5layout, s->layout_str);
5da9ab98
N
1995 break;
1996 case 6:
32754b7d 1997 info.new_layout = map_name(r6layout, s->layout_str);
5da9ab98
N
1998 break;
1999 case 10:
32754b7d 2000 info.new_layout = parse_layout_10(s->layout_str);
5da9ab98
N
2001 break;
2002 case LEVEL_FAULTY:
32754b7d 2003 info.new_layout = parse_layout_faulty(s->layout_str);
5da9ab98
N
2004 break;
2005 default:
e7b84f9d 2006 pr_err("layout not meaningful"
5da9ab98 2007 " with this level\n");
7bc71196
DW
2008 rv = 1;
2009 goto release;
2010 }
5da9ab98 2011 if (info.new_layout == UnSet) {
e7b84f9d 2012 pr_err("layout %s not understood"
5da9ab98 2013 " for this level\n",
32754b7d 2014 s->layout_str);
5da9ab98
N
2015 rv = 1;
2016 goto release;
7bc71196 2017 }
7236ee7a
N
2018 }
2019
907ea753 2020 if (array.level == LEVEL_FAULTY) {
32754b7d 2021 if (s->level != UnSet && s->level != array.level) {
e7b84f9d 2022 pr_err("cannot change level of Faulty device\n");
907ea753
N
2023 rv =1 ;
2024 }
32754b7d 2025 if (s->chunk) {
e7b84f9d 2026 pr_err("cannot set chunksize of Faulty device\n");
907ea753
N
2027 rv =1 ;
2028 }
32754b7d 2029 if (s->raiddisks && s->raiddisks != 1) {
e7b84f9d 2030 pr_err("cannot set raid_disks of Faulty device\n");
907ea753
N
2031 rv =1 ;
2032 }
32754b7d 2033 if (s->layout_str) {
907ea753
N
2034 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
2035 dprintf("Cannot get array information.\n");
2036 goto release;
2037 }
2038 array.layout = info.new_layout;
2039 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
e7b84f9d 2040 pr_err("failed to set new layout\n");
907ea753 2041 rv = 1;
32754b7d 2042 } else if (c->verbose >= 0)
907ea753
N
2043 printf("layout for %s set to %d\n",
2044 devname, array.layout);
2045 }
2046 } else if (array.level == LEVEL_CONTAINER) {
5da9ab98
N
2047 /* This change is to be applied to every array in the
2048 * container. This is only needed when the metadata imposes
2049 * restraints of the various arrays in the container.
2050 * Currently we only know that IMSM requires all arrays
2051 * to have the same number of devices so changing the
2052 * number of devices (On-Line Capacity Expansion) must be
2053 * performed at the level of the container
2054 */
9ad6f6e6 2055 rv = reshape_container(container, devname, -1, st, &info,
32754b7d 2056 c->force, c->backup_file, c->verbose, 0, 0);
9202b8eb 2057 frozen = 0;
5da9ab98 2058 } else {
08f9e34b
AK
2059 /* get spare devices from external metadata
2060 */
2061 if (st->ss->external) {
2062 struct mdinfo *info2;
2063
2064 info2 = st->ss->container_content(st, subarray);
2065 if (info2) {
2066 info.array.spare_disks =
2067 info2->array.spare_disks;
2068 sysfs_free(info2);
2069 }
2070 }
2071
5da9ab98
N
2072 /* Impose these changes on a single array. First
2073 * check that the metadata is OK with the change. */
7bc71196 2074
7ec0996c 2075 if (reshape_super(st, 0, info.new_level,
5da9ab98 2076 info.new_layout, info.new_chunk,
41784c88 2077 info.array.raid_disks, info.delta_disks,
32754b7d
N
2078 c->backup_file, devname, APPLY_METADATA_CHANGES,
2079 c->verbose)) {
7bc71196
DW
2080 rv = 1;
2081 goto release;
2082 }
5da9ab98 2083 sync_metadata(st);
32754b7d 2084 rv = reshape_array(container, fd, devname, st, &info, c->force,
19ceb16d
N
2085 devlist, data_offset, c->backup_file, c->verbose,
2086 0, 0, 0);
9202b8eb 2087 frozen = 0;
5da9ab98
N
2088 }
2089release:
8e61e0d7 2090 sysfs_free(sra);
9202b8eb
N
2091 if (frozen > 0)
2092 unfreeze(st);
5da9ab98
N
2093 return rv;
2094}
7bc71196 2095
f93346ef
AK
2096/* verify_reshape_position()
2097 * Function checks if reshape position in metadata is not farther
2098 * than position in md.
2099 * Return value:
2100 * 0 : not valid sysfs entry
2101 * it can be caused by not started reshape, it should be started
2102 * by reshape array or raid0 array is before takeover
2103 * -1 : error, reshape position is obviously wrong
2104 * 1 : success, reshape progress correct or updated
2105*/
2106static int verify_reshape_position(struct mdinfo *info, int level)
2107{
2108 int ret_val = 0;
2109 char buf[40];
178950ea 2110 int rv;
f93346ef
AK
2111
2112 /* read sync_max, failure can mean raid0 array */
178950ea
AK
2113 rv = sysfs_get_str(info, NULL, "sync_max", buf, 40);
2114
2115 if (rv > 0) {
f93346ef
AK
2116 char *ep;
2117 unsigned long long position = strtoull(buf, &ep, 0);
2118
2119 dprintf(Name": Read sync_max sysfs entry is: %s\n", buf);
2120 if (!(ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))) {
2121 position *= get_data_disks(level,
2122 info->new_layout,
2123 info->array.raid_disks);
2124 if (info->reshape_progress < position) {
2125 dprintf("Corrected reshape progress (%llu) to "
2126 "md position (%llu)\n",
2127 info->reshape_progress, position);
2128 info->reshape_progress = position;
2129 ret_val = 1;
2130 } else if (info->reshape_progress > position) {
e7b84f9d
N
2131 pr_err("Fatal error: array "
2132 "reshape was not properly frozen "
2133 "(expected reshape position is %llu, "
2134 "but reshape progress is %llu.\n",
2135 position, info->reshape_progress);
f93346ef
AK
2136 ret_val = -1;
2137 } else {
2138 dprintf("Reshape position in md and metadata "
2139 "are the same;");
2140 ret_val = 1;
2141 }
2142 }
178950ea
AK
2143 } else if (rv == 0) {
2144 /* for valid sysfs entry, 0-length content
2145 * should be indicated as error
2146 */
2147 ret_val = -1;
f93346ef
AK
2148 }
2149
2150 return ret_val;
2151}
2152
19ceb16d
N
2153static int raid10_reshape(char *container, int fd, char *devname,
2154 struct supertype *st, struct mdinfo *info,
2155 struct reshape *reshape,
2156 unsigned long long data_offset,
2157 int force, int verbose)
2158{
2159 /* Changing raid_disks, layout, chunksize or possibly
2160 * just data_offset for a RAID10.
ee2429e0
N
2161 * We must always change data_offset. We change by at least
2162 * ->backup_blocks which is the largest of the old and new
2163 * chunk sizes.
19ceb16d
N
2164 * If raid_disks is increasing, then data_offset must decrease
2165 * by at least this copy size.
2166 * If raid_disks is unchanged, data_offset must increase or
ee2429e0 2167 * decrease by at least backup_blocks but preferably by much more.
19ceb16d
N
2168 * We choose half of the available space.
2169 * If raid_disks is decreasing, data_offset must increase by
ee2429e0
N
2170 * at least backup_blocks. To allow of this, component_size
2171 * must be decreased by the same amount.
19ceb16d 2172 *
ee2429e0
N
2173 * So we calculate the required minimum and direction, possibly
2174 * reduce the component_size, then iterate through the devices
2175 * and set the new_data_offset.
19ceb16d
N
2176 * If that all works, we set chunk_size, layout, raid_disks, and start
2177 * 'reshape'
2178 */
2179 struct mdinfo *sra, *sd;
2180 unsigned long long min;
2181 int dir = 0;
2182 int err = 0;
2183
2184 sra = sysfs_read(fd, 0,
2185 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK
2186 );
2187 if (!sra) {
2188 fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n",
2189 devname);
2190 goto release;
2191 }
2192 min = reshape->backup_blocks;
2193
2194 if (info->delta_disks)
2195 sysfs_set_str(sra, NULL, "reshape_direction",
2196 info->delta_disks < 0 ? "backwards" : "forwards");
ee2429e0
N
2197 if (info->delta_disks < 0 &&
2198 info->space_after < reshape->backup_blocks) {
2199 int rv = sysfs_set_num(sra, NULL, "component_size",
2200 (sra->component_size -
2201 reshape->backup_blocks)/2);
2202 if (rv) {
2203 fprintf(stderr, Name ": cannot reduce component size\n");
2204 goto release;
2205 }
2206 }
19ceb16d
N
2207 for (sd = sra->devs; sd; sd = sd->next) {
2208 char *dn;
2209 int dfd;
2210 int rv;
2211 struct supertype *st2;
2212 struct mdinfo info2;
2213
2214 if (sd->disk.state & (1<<MD_DISK_FAULTY))
2215 continue;
2216 dn = map_dev(sd->disk.major, sd->disk.minor, 0);
2217 dfd = dev_open(dn, O_RDONLY);
2218 if (dfd < 0) {
2219 fprintf(stderr,
2220 Name ": %s: cannot open component %s\n",
2221 devname, dn ? dn : "-unknown-");
2222 rv = -1;
2223 goto release;
2224 }
2225 st2 = dup_super(st);
2226 rv = st2->ss->load_super(st2,dfd, NULL);
2227 close(dfd);
2228 if (rv) {
2229 free(st2);
2230 fprintf(stderr, ": %s: cannot get superblock from %s\n",
2231 devname, dn);
2232 goto release;
2233 }
2234 st2->ss->getinfo_super(st2, &info2, NULL);
2235 st2->ss->free_super(st2);
2236 free(st2);
2237 if (info->delta_disks < 0) {
2238 /* Don't need any space as array is shrinking
2239 * just move data_offset up by min
2240 */
2241 if (data_offset == 1)
2242 info2.new_data_offset = info2.data_offset + min;
2243 else {
2244 if ((unsigned long long)data_offset
2245 < info2.data_offset + min) {
2246 fprintf(stderr, Name ": --data-offset too small for %s\n",
2247 dn);
2248 goto release;
2249 }
2250 info2.new_data_offset = data_offset;
2251 }
2252 } else if (info->delta_disks > 0) {
2253 /* need space before */
2254 if (info2.space_before < min) {
2255 fprintf(stderr, Name ": Insufficient head-space for reshape on %s\n",
2256 dn);
2257 goto release;
2258 }
2259 if (data_offset == 1)
2260 info2.new_data_offset = info2.data_offset - min;
2261 else {
2262 if ((unsigned long long)data_offset
2263 > info2.data_offset - min) {
2264 fprintf(stderr, Name ": --data-offset too large for %s\n",
2265 dn);
2266 goto release;
2267 }
2268 info2.new_data_offset = data_offset;
2269 }
2270 } else {
2271 if (dir == 0) {
2272 /* can move up or down. 'data_offset'
2273 * might guide us, otherwise choose
2274 * direction with most space
2275 */
2276 if (data_offset == 1) {
2277 if (info2.space_before > info2.space_after)
2278 dir = -1;
2279 else
2280 dir = 1;
2281 } else if (data_offset < info2.data_offset)
2282 dir = -1;
2283 else
2284 dir = 1;
2285 sysfs_set_str(sra, NULL, "reshape_direction",
2286 dir == 1 ? "backwards" : "forwards");
2287 }
2288 switch (dir) {
2289 case 1: /* Increase data offset */
2290 if (info2.space_after < min) {
2291 fprintf(stderr, Name ": Insufficient tail-space for reshape on %s\n",
2292 dn);
2293 goto release;
2294 }
2295 if (data_offset != 1 &&
2296 data_offset < info2.data_offset + min) {
2297 fprintf(stderr, Name ": --data-offset too small on %s\n",
2298 dn);
2299 goto release;
2300 }
2301 if (data_offset != 1)
2302 info2.new_data_offset = data_offset;
2303 else {
2304 unsigned long long off =
2305 info2.space_after / 2;
2306 off &= ~7ULL;
2307 if (off < min)
2308 off = min;
2309 info2.new_data_offset =
2310 info2.data_offset + off;
2311 }
2312 break;
2313 case -1: /* Decrease data offset */
2314 if (info2.space_before < min) {
2315 fprintf(stderr, Name ": insufficient head-room on %s\n",
2316 dn);
2317 goto release;
2318 }
2319 if (data_offset != 1 &&
2320 data_offset < info2.data_offset - min) {
2321 fprintf(stderr, Name ": --data-offset too small on %s\n",
2322 dn);
2323 goto release;
2324 }
2325 if (data_offset != 1)
2326 info2.new_data_offset = data_offset;
2327 else {
2328 unsigned long long off =
2329 info2.space_before / 2;
2330 off &= ~7ULL;
2331 if (off < min)
2332 off = min;
2333 info2.new_data_offset =
2334 info2.data_offset - off;
2335 }
2336 break;
2337 }
2338 }
2339 if (sysfs_set_num(sra, sd, "new_offset",
2340 info2.new_data_offset) < 0) {
2341 err = errno;
2342 fprintf(stderr, Name ": Cannot set new_offset for %s\n",
2343 dn);
2344 break;
2345 }
2346 }
ee2429e0 2347 if (!err && sysfs_set_num(sra, NULL, "chunk_size", info->new_chunk) < 0)
19ceb16d
N
2348 err = errno;
2349 if (!err && sysfs_set_num(sra, NULL, "layout", reshape->after.layout) < 0)
2350 err = errno;
2351 if (!err && sysfs_set_num(sra, NULL, "raid_disks",
2352 info->array.raid_disks + info->delta_disks) < 0)
2353 err = errno;
2354 if (!err && sysfs_set_str(sra, NULL, "sync_action", "reshape") < 0)
2355 err = errno;
2356 if (err) {
2357 fprintf(stderr, Name ": Cannot set array shape for %s\n",
2358 devname);
2359 if (err == EBUSY &&
2360 (info->array.state & (1<<MD_SB_BITMAP_PRESENT)))
2361 fprintf(stderr,
2362 " Bitmap must be removed before"
2363 " shape can be changed\n");
2364 goto release;
2365 }
2366 sysfs_free(sra);
2367 return 0;
2368release:
2369 sysfs_free(sra);
2370 return 1;
2371}
2372
ee2429e0
N
2373static void get_space_after(int fd, struct supertype *st, struct mdinfo *info)
2374{
2375 struct mdinfo *sra, *sd;
2376 unsigned long long min_space_before, min_space_after;
2377 int first = 1;
2378
2379 sra = sysfs_read(fd, 0, GET_DEVS);
2380 if (!sra)
2381 return;
2382 for (sd = sra->devs; sd; sd = sd->next) {
2383 char *dn;
2384 int dfd;
2385 struct supertype *st2;
2386 struct mdinfo info2;
2387
2388 if (sd->disk.state & (1<<MD_DISK_FAULTY))
2389 continue;
2390 dn = map_dev(sd->disk.major, sd->disk.minor, 0);
2391 dfd = dev_open(dn, O_RDONLY);
2392 if (dfd < 0)
2393 break;
2394 st2 = dup_super(st);
2395 if (st2->ss->load_super(st2,dfd, NULL)) {
2396 close(dfd);
2397 free(st2);
2398 break;
2399 }
2400 close(dfd);
2401 st2->ss->getinfo_super(st2, &info2, NULL);
2402 st2->ss->free_super(st2);
2403 free(st2);
2404 if (first ||
2405 min_space_before > info2.space_before)
2406 min_space_before = info2.space_before;
2407 if (first ||
2408 min_space_after > info2.space_after)
2409 min_space_after = info2.space_after;
2410 first = 0;
2411 }
2412 if (sd == NULL && !first) {
2413 info->space_after = min_space_after;
2414 info->space_before = min_space_before;
2415 }
2416 sysfs_free(sra);
2417}
2418
5da9ab98
N
2419static int reshape_array(char *container, int fd, char *devname,
2420 struct supertype *st, struct mdinfo *info,
e2e53a2d 2421 int force, struct mddev_dev *devlist,
19ceb16d 2422 unsigned long long data_offset,
ba728be7 2423 char *backup_file, int verbose, int forked,
b76b30e0 2424 int restart, int freeze_reshape)
5da9ab98
N
2425{
2426 struct reshape reshape;
2427 int spares_needed;
2428 char *msg;
2429 int orig_level = UnSet;
2430 int disks, odisks;
1f9b0e28 2431 int delayed;
7bc71196 2432
5da9ab98
N
2433 struct mdu_array_info_s array;
2434 char *c;
e86c9dd6 2435
e2e53a2d
N
2436 struct mddev_dev *dv;
2437 int added_disks;
2438
d152f53e
JS
2439 int *fdlist = NULL;
2440 unsigned long long *offsets = NULL;
5da9ab98
N
2441 int d;
2442 int nrdisks;
2443 int err;
da8100ca 2444 unsigned long blocks;
5da9ab98
N
2445 unsigned long cache;
2446 unsigned long long array_size;
2447 int done;
cf6ac177 2448 struct mdinfo *sra = NULL;
7bc71196 2449
9468aeac
N
2450 /* when reshaping a RAID0, the component_size might be zero.
2451 * So try to fix that up.
2452 */
2453 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
2454 dprintf("Cannot get array information.\n");
2455 goto release;
2456 }
2457 if (array.level == 0 && info->component_size == 0) {
2458 get_dev_size(fd, NULL, &array_size);
2459 info->component_size = array_size / array.raid_disks;
2460 }
2461
ee2429e0
N
2462 if (array.level == 10)
2463 /* Need space_after info */
2464 get_space_after(fd, st, info);
2465
3cb2aed2
N
2466 if (info->reshape_active) {
2467 int new_level = info->new_level;
2468 info->new_level = UnSet;
ce4783d3
N
2469 if (info->delta_disks > 0)
2470 info->array.raid_disks -= info->delta_disks;
3cb2aed2
N
2471 msg = analyse_change(info, &reshape);
2472 info->new_level = new_level;
ce4783d3
N
2473 if (info->delta_disks > 0)
2474 info->array.raid_disks += info->delta_disks;
fcdfb814
AK
2475 if (!restart)
2476 /* Make sure the array isn't read-only */
2477 ioctl(fd, RESTART_ARRAY_RW, 0);
3cb2aed2
N
2478 } else
2479 msg = analyse_change(info, &reshape);
5da9ab98 2480 if (msg) {
e7b84f9d 2481 pr_err("%s\n", msg);
631d7405 2482 goto release;
5da9ab98 2483 }
817ed7d6
N
2484 if (restart &&
2485 (reshape.level != info->array.level ||
2486 reshape.before.layout != info->array.layout ||
384e9be1 2487 reshape.before.data_disks + reshape.parity
ce4783d3 2488 != info->array.raid_disks - max(0, info->delta_disks))) {
e7b84f9d 2489 pr_err("reshape info is not in native format -"
20a40eca
N
2490 " cannot continue.\n");
2491 goto release;
2492 }
a93f87ee 2493
e1dd332a
AK
2494 if (st->ss->external && restart && (info->reshape_progress == 0)) {
2495 /* When reshape is restarted from '0', very begin of array
2496 * it is possible that for external metadata reshape and array
2497 * configuration doesn't happen.
2498 * Check if md has the same opinion, and reshape is restarted
2499 * from 0. If so, this is regular reshape start after reshape
2500 * switch in metadata to next array only.
2501 */
2502 if ((verify_reshape_position(info, reshape.level) >= 0) &&
2503 (info->reshape_progress == 0))
2504 restart = 0;
2505 }
a93f87ee
N
2506 if (restart) {
2507 /* reshape already started. just skip to monitoring the reshape */
2508 if (reshape.backup_blocks == 0)
2509 return 0;
2510 goto started;
2511 }
a9c3e78f
AK
2512 /* The container is frozen but the array may not be.
2513 * So freeze the array so spares don't get put to the wrong use
2514 * FIXME there should probably be a cleaner separation between
2515 * freeze_array and freeze_container.
2516 */
2517 sysfs_freeze_array(info);
e06c4e59 2518 /* Check we have enough spares to not be degraded */
e2e53a2d
N
2519 added_disks = 0;
2520 for (dv = devlist; dv ; dv=dv->next)
2521 added_disks++;
5da9ab98
N
2522 spares_needed = max(reshape.before.data_disks,
2523 reshape.after.data_disks)
2524 + reshape.parity - array.raid_disks;
7bc71196 2525
88c1a083 2526 if (!force &&
e2e53a2d
N
2527 info->new_level > 1 && info->array.level > 1 &&
2528 spares_needed > info->array.spare_disks + added_disks) {
e7b84f9d
N
2529 pr_err("Need %d spare%s to avoid degraded array,"
2530 " and only have %d.\n"
2531 " Use --force to over-ride this check.\n",
2532 spares_needed,
2533 spares_needed == 1 ? "" : "s",
2534 info->array.spare_disks + added_disks);
631d7405 2535 goto release;
7bc71196 2536 }
e06c4e59
N
2537 /* Check we have enough spares to not fail */
2538 spares_needed = max(reshape.before.data_disks,
2539 reshape.after.data_disks)
2540 - array.raid_disks;
2541 if ((info->new_level > 1 || info->new_level == 0) &&
e2e53a2d 2542 spares_needed > info->array.spare_disks +added_disks) {
e7b84f9d
N
2543 pr_err("Need %d spare%s to create working array,"
2544 " and only have %d.\n",
2545 spares_needed,
2546 spares_needed == 1 ? "" : "s",
2547 info->array.spare_disks + added_disks);
e06c4e59
N
2548 goto release;
2549 }
e86c9dd6 2550
eae35f5c 2551 if (reshape.level != array.level) {
5da9ab98
N
2552 char *c = map_num(pers, reshape.level);
2553 int err;
2554 if (c == NULL)
631d7405 2555 goto release;
e86c9dd6 2556
5da9ab98
N
2557 err = sysfs_set_str(info, NULL, "level", c);
2558 if (err) {
2559 err = errno;
e7b84f9d 2560 pr_err("%s: could not set level to %s\n",
5da9ab98 2561 devname, c);
24daa16f 2562 if (err == EBUSY &&
5da9ab98 2563 (info->array.state & (1<<MD_SB_BITMAP_PRESENT)))
e7b84f9d
N
2564 cont_err("Bitmap must be removed"
2565 " before level can be changed\n");
631d7405 2566 goto release;
19678e53 2567 }
ba728be7 2568 if (verbose >= 0)
e7b84f9d 2569 pr_err("level of %s changed to %s\n",
24daa16f 2570 devname, c);
eae35f5c 2571 orig_level = array.level;
3cd4e7c4 2572 sysfs_freeze_array(info);
e86c9dd6 2573
16d4d84e
AK
2574 if (reshape.level > 0 && st->ss->external) {
2575 /* make sure mdmon is aware of the new level */
78340e26
AK
2576 if (mdmon_running(st->container_dev))
2577 flush_mdmon(container);
2578
16d4d84e
AK
2579 if (!mdmon_running(st->container_dev))
2580 start_mdmon(st->container_dev);
2581 ping_monitor(container);
e919fb0a
AK
2582 if (mdmon_running(st->container_dev) &&
2583 st->update_tail == NULL)
2584 st->update_tail = &st->updates;
16d4d84e 2585 }
5da9ab98 2586 }
5da9ab98
N
2587 /* ->reshape_super might have chosen some spares from the
2588 * container that it wants to be part of the new array.
2589 * We can collect them with ->container_content and give
2590 * them to the kernel.
2591 */
2592 if (st->ss->reshape_super && st->ss->container_content) {
2593 char *subarray = strchr(info->text_version+1, '/')+1;
2594 struct mdinfo *info2 =
2595 st->ss->container_content(st, subarray);
2596 struct mdinfo *d;
2597
2dd0d697
AK
2598 if (info2) {
2599 sysfs_init(info2, fd, st->devnum);
0d5ac3c6
N
2600 /* When increasing number of devices, we need to set
2601 * new raid_disks before adding these, or they might
2602 * be rejected.
2603 */
2604 if (reshape.backup_blocks &&
2605 reshape.after.data_disks > reshape.before.data_disks)
2606 subarray_set_num(container, info2, "raid_disks",
2607 reshape.after.data_disks +
2608 reshape.parity);
5da9ab98
N
2609 for (d = info2->devs; d; d = d->next) {
2610 if (d->disk.state == 0 &&
2611 d->disk.raid_disk >= 0) {
2612 /* This is a spare that wants to
2613 * be part of the array.
2614 */
2615 add_disk(fd, st, info2, d);
2616 }
9860f271 2617 }
2dd0d697
AK
2618 sysfs_free(info2);
2619 }
5da9ab98 2620 }
e2e53a2d
N
2621 /* We might have been given some devices to add to the
2622 * array. Now that the array has been changed to the right
2623 * level and frozen, we can safely add them.
2624 */
2625 if (devlist)
ba728be7 2626 Manage_subdevs(devname, fd, devlist, verbose,
11b391ec 2627 0,NULL, 0);
1686dc25 2628
b4f8e38b 2629 if (reshape.backup_blocks == 0) {
5da9ab98
N
2630 /* No restriping needed, but we might need to impose
2631 * some more changes: layout, raid_disks, chunk_size
e86c9dd6 2632 */
24aebf3a
KW
2633 /* read current array info */
2634 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
83732c28 2635 dprintf("Cannot get array information.\n");
24aebf3a
KW
2636 goto release;
2637 }
2638 /* compare current array info with new values and if
2639 * it is different update them to new */
5da9ab98 2640 if (info->new_layout != UnSet &&
24aebf3a
KW
2641 info->new_layout != array.layout) {
2642 array.layout = info->new_layout;
2643 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
e7b84f9d 2644 pr_err("failed to set new layout\n");
631d7405 2645 goto release;
ba728be7 2646 } else if (verbose >= 0)
5da9ab98 2647 printf("layout for %s set to %d\n",
24aebf3a 2648 devname, array.layout);
5da9ab98
N
2649 }
2650 if (info->delta_disks != UnSet &&
24aebf3a
KW
2651 info->delta_disks != 0 &&
2652 array.raid_disks != (info->array.raid_disks + info->delta_disks)) {
2653 array.raid_disks += info->delta_disks;
2654 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
e7b84f9d 2655 pr_err("failed to set raid disks\n");
631d7405 2656 goto release;
ba728be7 2657 } else if (verbose >= 0) {
5da9ab98 2658 printf("raid_disks for %s set to %d\n",
24aebf3a
KW
2659 devname, array.raid_disks);
2660 }
5da9ab98
N
2661 }
2662 if (info->new_chunk != 0 &&
24aebf3a 2663 info->new_chunk != array.chunk_size) {
5da9ab98
N
2664 if (sysfs_set_num(info, NULL,
2665 "chunk_size", info->new_chunk) != 0) {
e7b84f9d 2666 pr_err("failed to set chunk size\n");
631d7405 2667 goto release;
ba728be7 2668 } else if (verbose >= 0)
5da9ab98 2669 printf("chunk size for %s set to %d\n",
24aebf3a 2670 devname, array.chunk_size);
4725bc31 2671 }
e35b189b 2672 unfreeze(st);
631d7405 2673 return 0;
5da9ab98 2674 }
19678e53 2675
5da9ab98
N
2676 /*
2677 * There are three possibilities.
2678 * 1/ The array will shrink.
2679 * We need to ensure the reshape will pause before reaching
2680 * the 'critical section'. We also need to fork and wait for
24daa16f 2681 * that to happen. When it does we
5da9ab98
N
2682 * suspend/backup/complete/unfreeze
2683 *
2684 * 2/ The array will not change size.
2685 * This requires that we keep a backup of a sliding window
2686 * so that we can restore data after a crash. So we need
2687 * to fork and monitor progress.
2688 * In future we will allow the data_offset to change, so
2689 * a sliding backup becomes unnecessary.
2690 *
2691 * 3/ The array will grow. This is relatively easy.
2692 * However the kernel's restripe routines will cheerfully
2693 * overwrite some early data before it is safe. So we
2694 * need to make a backup of the early parts of the array
2695 * and be ready to restore it if rebuild aborts very early.
2696 * For externally managed metadata, we still need a forked
2697 * child to monitor the reshape and suspend IO over the region
2698 * that is being reshaped.
2699 *
2700 * We backup data by writing it to one spare, or to a
2701 * file which was given on command line.
2702 *
2703 * In each case, we first make sure that storage is available
2704 * for the required backup.
2705 * Then we:
2706 * - request the shape change.
2707 * - fork to handle backup etc.
2708 */
5da9ab98
N
2709 /* Check that we can hold all the data */
2710 get_dev_size(fd, NULL, &array_size);
2711 if (reshape.new_size < (array_size/512)) {
e7b84f9d
N
2712 pr_err("this change will reduce the size of the array.\n"
2713 " use --grow --array-size first to truncate array.\n"
2714 " e.g. mdadm --grow %s --array-size %llu\n",
2715 devname, reshape.new_size/2);
5da9ab98
N
2716 goto release;
2717 }
7236ee7a 2718
19ceb16d
N
2719started:
2720
2721 if (array.level == 10) {
2722 /* Reshaping RAID10 does not require and data backup by
2723 * user-space. Instead it requires that the data_offset
2724 * is changed to avoid the need for backup.
2725 * So this is handled very separately
2726 */
2727 if (restart)
2728 /* Nothing to do. */
2729 return 0;
2730 return raid10_reshape(container, fd, devname, st, info,
2731 &reshape, data_offset,
2732 force, verbose);
2733 }
5da9ab98 2734 sra = sysfs_read(fd, 0,
3656edcd 2735 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK|
5da9ab98 2736 GET_CACHE);
5da9ab98 2737 if (!sra) {
e7b84f9d 2738 pr_err("%s: Cannot get array details from sysfs\n",
5da9ab98 2739 devname);
5da9ab98
N
2740 goto release;
2741 }
7236ee7a 2742
5da9ab98
N
2743 /* Decide how many blocks (sectors) for a reshape
2744 * unit. The number we have so far is just a minimum
2745 */
b4f8e38b 2746 blocks = reshape.backup_blocks;
24daa16f 2747 if (reshape.before.data_disks ==
5da9ab98
N
2748 reshape.after.data_disks) {
2749 /* Make 'blocks' bigger for better throughput, but
2750 * not so big that we reject it below.
2751 * Try for 16 megabytes
2752 */
2753 while (blocks * 32 < sra->component_size &&
2754 blocks < 16*1024*2)
2755 blocks *= 2;
2756 } else
e7b84f9d 2757 pr_err("Need to backup %luK of critical "
5da9ab98
N
2758 "section..\n", blocks/2);
2759
2760 if (blocks >= sra->component_size/2) {
e7b84f9d 2761 pr_err("%s: Something wrong"
5da9ab98
N
2762 " - reshape aborted\n",
2763 devname);
5da9ab98
N
2764 goto release;
2765 }
7236ee7a 2766
5da9ab98
N
2767 /* Now we need to open all these devices so we can read/write.
2768 */
b8b286a6
N
2769 nrdisks = max(reshape.before.data_disks,
2770 reshape.after.data_disks) + reshape.parity
2771 + sra->array.spare_disks;
503975b9
N
2772 fdlist = xcalloc((1+nrdisks), sizeof(int));
2773 offsets = xcalloc((1+nrdisks), sizeof(offsets[0]));
e380d3be 2774
b8b286a6
N
2775 odisks = reshape.before.data_disks + reshape.parity;
2776 d = reshape_prepare_fdlist(devname, sra, odisks,
5da9ab98
N
2777 nrdisks, blocks, backup_file,
2778 fdlist, offsets);
2779 if (d < 0) {
5da9ab98
N
2780 goto release;
2781 }
13c37ad3
AK
2782 if ((st->ss->manage_reshape == NULL) ||
2783 (st->ss->recover_backup == NULL)) {
2784 if (backup_file == NULL) {
2785 if (reshape.after.data_disks <=
2786 reshape.before.data_disks) {
e7b84f9d 2787 pr_err("%s: Cannot grow - "
13c37ad3
AK
2788 "need backup-file\n", devname);
2789 goto release;
2790 } else if (sra->array.spare_disks == 0) {
e7b84f9d 2791 pr_err("%s: Cannot grow - "
13c37ad3
AK
2792 "need a spare or backup-file to backup "
2793 "critical section\n", devname);
2794 goto release;
2795 }
2796 } else {
2797 if (!reshape_open_backup_file(backup_file, fd, devname,
2798 (signed)blocks,
2799 fdlist+d, offsets+d,
2800 restart)) {
2801 goto release;
2802 }
2803 d++;
e86c9dd6 2804 }
5da9ab98 2805 }
7236ee7a 2806
5da9ab98
N
2807 /* lastly, check that the internal stripe cache is
2808 * large enough, or it won't work.
2809 * It must hold at least 4 stripes of the larger
2810 * chunk size
2811 */
2812 cache = max(info->array.chunk_size, info->new_chunk);
2813 cache *= 4; /* 4 stripes minimum */
2814 cache /= 512; /* convert to sectors */
2815 disks = min(reshape.before.data_disks, reshape.after.data_disks);
2816 /* make sure there is room for 'blocks' with a bit to spare */
2817 if (cache < 16 + blocks / disks)
2818 cache = 16 + blocks / disks;
2819 cache /= (4096/512); /* Covert from sectors to pages */
2820
2821 if (sra->cache_size < cache)
2822 subarray_set_num(container, sra, "stripe_cache_size",
2823 cache+1);
2824
2825 /* Right, everything seems fine. Let's kick things off.
2826 * If only changing raid_disks, use ioctl, else use
2827 * sysfs.
2828 */
2829 sync_metadata(st);
2830
b4f8e38b 2831 sra->new_chunk = info->new_chunk;
ddee071d 2832
f93346ef
AK
2833 if (restart) {
2834 /* for external metadata checkpoint saved by mdmon can be lost
2835 * or missed /due to e.g. crash/. Check if md is not during
2836 * restart farther than metadata points to.
2837 * If so, this means metadata information is obsolete.
2838 */
2839 if (st->ss->external)
2840 verify_reshape_position(info, reshape.level);
ddee071d 2841 sra->reshape_progress = info->reshape_progress;
f93346ef 2842 } else {
f897078e
N
2843 sra->reshape_progress = 0;
2844 if (reshape.after.data_disks < reshape.before.data_disks)
2845 /* start from the end of the new array */
2846 sra->reshape_progress = (sra->component_size
2847 * reshape.after.data_disks);
2848 }
2849
2850 if (info->array.chunk_size == info->new_chunk &&
7477d513
AK
2851 reshape.before.layout == reshape.after.layout &&
2852 st->ss->external == 0) {
f897078e 2853 /* use SET_ARRAY_INFO but only if reshape hasn't started */
6ef421be 2854 ioctl(fd, GET_ARRAY_INFO, &array);
5da9ab98 2855 array.raid_disks = reshape.after.data_disks + reshape.parity;
20a40eca 2856 if (!restart &&
f897078e 2857 ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
5da9ab98 2858 int err = errno;
631d7405 2859
e7b84f9d
N
2860 pr_err("Cannot set device shape for %s: %s\n",
2861 devname, strerror(errno));
7bc71196 2862
24daa16f 2863 if (err == EBUSY &&
5da9ab98 2864 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
e7b84f9d
N
2865 cont_err("Bitmap must be removed before"
2866 " shape can be changed\n");
b5420ef3 2867
5da9ab98
N
2868 goto release;
2869 }
20a40eca 2870 } else if (!restart) {
5da9ab98 2871 /* set them all just in case some old 'new_*' value
f897078e 2872 * persists from some earlier problem.
7236ee7a 2873 */
631d7405 2874 int err = 0;
5da9ab98 2875 if (sysfs_set_num(sra, NULL, "chunk_size", info->new_chunk) < 0)
631d7405 2876 err = errno;
24daa16f
N
2877 if (!err && sysfs_set_num(sra, NULL, "layout",
2878 reshape.after.layout) < 0)
631d7405
N
2879 err = errno;
2880 if (!err && subarray_set_num(container, sra, "raid_disks",
24daa16f
N
2881 reshape.after.data_disks +
2882 reshape.parity) < 0)
631d7405
N
2883 err = errno;
2884 if (err) {
e7b84f9d 2885 pr_err("Cannot set device shape for %s\n",
5da9ab98 2886 devname);
7236ee7a 2887
24daa16f 2888 if (err == EBUSY &&
5da9ab98 2889 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
e7b84f9d
N
2890 cont_err("Bitmap must be removed before"
2891 " shape can be changed\n");
5da9ab98 2892 goto release;
e86c9dd6 2893 }
5da9ab98 2894 }
e86c9dd6 2895
27a1e5b5
N
2896 err = start_reshape(sra, restart, reshape.before.data_disks,
2897 reshape.after.data_disks);
fab32c97 2898 if (err) {
e7b84f9d
N
2899 pr_err("Cannot %s reshape for %s\n",
2900 restart ? "continue" : "start",
2901 devname);
fab32c97
AK
2902 goto release;
2903 }
a93f87ee
N
2904 if (restart)
2905 sysfs_set_str(sra, NULL, "array_state", "active");
b76b30e0
AK
2906 if (freeze_reshape) {
2907 free(fdlist);
2908 free(offsets);
2909 sysfs_free(sra);
e7b84f9d 2910 pr_err("Reshape has to be continued from"
59ab9f54 2911 " location %llu when root filesystem has been mounted.\n",
b76b30e0
AK
2912 sra->reshape_progress);
2913 return 1;
2914 }
5da9ab98
N
2915
2916 /* Now we just need to kick off the reshape and watch, while
2917 * handling backups of the data...
2918 * This is all done by a forked background process.
2919 */
2920 switch(forked ? 0 : fork()) {
6b2d630c 2921 case -1:
e7b84f9d 2922 pr_err("Cannot run child to monitor reshape: %s\n",
6b2d630c 2923 strerror(errno));
6b2d630c 2924 abort_reshape(sra);
631d7405 2925 goto release;
6b2d630c 2926 default:
d152f53e
JS
2927 free(fdlist);
2928 free(offsets);
2929 sysfs_free(sra);
6b2d630c 2930 return 0;
5da9ab98 2931 case 0:
cc700db3 2932 map_fork();
6b2d630c
N
2933 break;
2934 }
5da9ab98 2935
1f9b0e28
N
2936 /* If another array on the same devices is busy, the
2937 * reshape will wait for them. This would mean that
2938 * the first section that we suspend will stay suspended
2939 * for a long time. So check on that possibility
2940 * by looking for "DELAYED" in /proc/mdstat, and if found,
2941 * wait a while
2942 */
2943 do {
2944 struct mdstat_ent *mds, *m;
2945 delayed = 0;
2946 mds = mdstat_read(0, 0);
2947 for (m = mds; m; m = mds->next)
2948 if (m->devnum == devname2devnum(sra->sys_name)) {
2949 if (m->resync &&
2950 m->percent == RESYNC_DELAYED)
2951 delayed = 1;
2952 if (m->resync == 0)
2953 /* Haven't started the reshape thread
2954 * yet, wait a bit
2955 */
2956 delayed = 2;
2957 break;
2958 }
2959 free_mdstat(mds);
2960 if (delayed == 1 && get_linux_version() < 3007000) {
2961 pr_err("Reshape is delayed, but cannot wait carefully with this kernel.\n"
2962 " You might experience problems until other reshapes complete.\n");
2963 delayed = 0;
2964 }
2965 if (delayed)
2966 sleep(30 - (delayed-1) * 25);
2967 } while (delayed);
2968
6b2d630c
N
2969 close(fd);
2970 if (check_env("MDADM_GROW_VERIFY"))
2971 fd = open(devname, O_RDONLY | O_DIRECT);
2972 else
2973 fd = -1;
2974 mlockall(MCL_FUTURE);
5da9ab98 2975
6b2d630c
N
2976 if (st->ss->external) {
2977 /* metadata handler takes it from here */
2978 done = st->ss->manage_reshape(
2979 fd, sra, &reshape, st, blocks,
2980 fdlist, offsets,
2981 d - odisks, fdlist+odisks,
2982 offsets+odisks);
2983 } else
2984 done = child_monitor(
2985 fd, sra, &reshape, st, blocks,
2986 fdlist, offsets,
2987 d - odisks, fdlist+odisks,
2988 offsets+odisks);
2989
d152f53e
JS
2990 free(fdlist);
2991 free(offsets);
2992
6b2d630c
N
2993 if (backup_file && done)
2994 unlink(backup_file);
2995 if (!done) {
2996 abort_reshape(sra);
2997 goto out;
2998 }
1cbc5680
N
2999
3000 if (!st->ss->external &&
3001 !(reshape.before.data_disks != reshape.after.data_disks
3002 && info->custom_array_size) &&
3003 info->new_level == reshape.level &&
3004 !forked) {
3005 /* no need to wait for the reshape to finish as
3006 * there is nothing more to do.
3007 */
d152f53e 3008 sysfs_free(sra);
1cbc5680
N
3009 exit(0);
3010 }
3011 wait_reshape(sra);
3012
3013 if (st->ss->external) {
3014 /* Re-load the metadata as much could have changed */
3015 int cfd = open_dev(st->container_dev);
3016 if (cfd >= 0) {
78340e26 3017 flush_mdmon(container);
1cbc5680
N
3018 st->ss->free_super(st);
3019 st->ss->load_container(st, cfd, container);
3020 close(cfd);
3021 }
3022 }
3023
6b2d630c
N
3024 /* set new array size if required customer_array_size is used
3025 * by this metadata.
3026 */
3027 if (reshape.before.data_disks !=
3028 reshape.after.data_disks &&
54397ed9
AK
3029 info->custom_array_size)
3030 set_array_size(st, info, info->text_version);
582496b2 3031
6b2d630c 3032 if (info->new_level != reshape.level) {
582496b2 3033
6b2d630c 3034 c = map_num(pers, info->new_level);
1cbc5680
N
3035 if (c) {
3036 err = sysfs_set_str(sra, NULL, "level", c);
3037 if (err)
e7b84f9d
N
3038 pr_err("%s: could not set level "
3039 "to %s\n", devname, c);
1cbc5680 3040 }
e919fb0a
AK
3041 if (info->new_level == 0)
3042 st->update_tail = NULL;
7236ee7a 3043 }
6b2d630c 3044out:
d152f53e 3045 sysfs_free(sra);
6b2d630c
N
3046 if (forked)
3047 return 0;
e84f2c00 3048 unfreeze(st);
6b2d630c 3049 exit(0);
e86c9dd6 3050
6b2d630c 3051release:
d152f53e
JS
3052 free(fdlist);
3053 free(offsets);
1cbc5680 3054 if (orig_level != UnSet && sra) {
7236ee7a
N
3055 c = map_num(pers, orig_level);
3056 if (c && sysfs_set_str(sra, NULL, "level", c) == 0)
e7b84f9d 3057 pr_err("aborting level change\n");
7236ee7a 3058 }
d152f53e 3059 sysfs_free(sra);
9202b8eb
N
3060 if (!forked)
3061 unfreeze(st);
631d7405 3062 return 1;
7236ee7a 3063}
e86c9dd6 3064
9ad6f6e6
AK
3065/* mdfd handle is passed to be closed in child process (after fork).
3066 */
493f5dd6 3067int reshape_container(char *container, char *devname,
9ad6f6e6 3068 int mdfd,
24daa16f 3069 struct supertype *st,
5da9ab98
N
3070 struct mdinfo *info,
3071 int force,
3072 char *backup_file,
ba728be7 3073 int verbose, int restart, int freeze_reshape)
5da9ab98 3074{
1bb174ba 3075 struct mdinfo *cc = NULL;
bcc9e9ed 3076 int rv = restart;
2d04d7e5 3077 int last_devnum = -1;
1bb174ba 3078
d8eb27f7 3079 /* component_size is not meaningful for a container,
7ec0996c 3080 * so pass '0' meaning 'no change'
d8eb27f7 3081 */
493f5dd6 3082 if (!restart &&
7ec0996c 3083 reshape_super(st, 0, info->new_level,
5da9ab98 3084 info->new_layout, info->new_chunk,
41784c88 3085 info->array.raid_disks, info->delta_disks,
016e00f5 3086 backup_file, devname, APPLY_METADATA_CHANGES,
ba728be7 3087 verbose)) {
9e325442 3088 unfreeze(st);
5da9ab98 3089 return 1;
9e325442 3090 }
5da9ab98
N
3091
3092 sync_metadata(st);
3093
1bb174ba
AK
3094 /* ping monitor to be sure that update is on disk
3095 */
3096 ping_monitor(container);
5da9ab98
N
3097
3098 switch (fork()) {
3099 case -1: /* error */
3100 perror("Cannot fork to complete reshape\n");
9e325442 3101 unfreeze(st);
5da9ab98
N
3102 return 1;
3103 default: /* parent */
b76b30e0
AK
3104 if (!freeze_reshape)
3105 printf(Name ": multi-array reshape continues"
3106 " in background\n");
5da9ab98
N
3107 return 0;
3108 case 0: /* child */
cc700db3 3109 map_fork();
5da9ab98
N
3110 break;
3111 }
3112
9ad6f6e6
AK
3113 /* close unused handle in child process
3114 */
3115 if (mdfd > -1)
3116 close(mdfd);
3117
1bb174ba
AK
3118 while(1) {
3119 /* For each member array with reshape_active,
3120 * we need to perform the reshape.
3121 * We pick the first array that needs reshaping and
3122 * reshape it. reshape_array() will re-read the metadata
3123 * so the next time through a different array should be
3124 * ready for reshape.
493f5dd6
N
3125 * It is possible that the 'different' array will not
3126 * be assembled yet. In that case we simple exit.
3127 * When it is assembled, the mdadm which assembles it
3128 * will take over the reshape.
1bb174ba
AK
3129 */
3130 struct mdinfo *content;
5da9ab98
N
3131 int fd;
3132 struct mdstat_ent *mdstat;
5da9ab98
N
3133 char *adev;
3134
1bb174ba 3135 sysfs_free(cc);
5da9ab98 3136
1bb174ba
AK
3137 cc = st->ss->container_content(st, NULL);
3138
3139 for (content = cc; content ; content = content->next) {
3140 char *subarray;
3141 if (!content->reshape_active)
3142 continue;
3143
3144 subarray = strchr(content->text_version+1, '/')+1;
3145 mdstat = mdstat_by_subdev(subarray,
3146 devname2devnum(container));
3147 if (!mdstat)
3148 continue;
1ca90aa6 3149 if (mdstat->active == 0) {
e7b84f9d 3150 pr_err("Skipping inactive "
1ca90aa6
AK
3151 "array md%i.\n", mdstat->devnum);
3152 free_mdstat(mdstat);
3153 mdstat = NULL;
3154 continue;
3155 }
1bb174ba
AK
3156 break;
3157 }
3158 if (!content)
3159 break;
5da9ab98 3160
5da9ab98
N
3161 adev = map_dev(dev2major(mdstat->devnum),
3162 dev2minor(mdstat->devnum),
3163 0);
3164 if (!adev)
1bb174ba
AK
3165 adev = content->text_version;
3166
97a3490c
AK
3167 fd = open_dev(mdstat->devnum);
3168 if (fd < 0) {
3169 printf(Name ": Device %s cannot be opened for reshape.",
3170 adev);
3171 break;
3172 }
3173
2d04d7e5
AK
3174 if (last_devnum == mdstat->devnum) {
3175 /* Do not allow for multiple reshape_array() calls for
3176 * the same array.
3177 * It can happen when reshape_array() returns without
3178 * error, when reshape is not finished (wrong reshape
3179 * starting/continuation conditions). Mdmon doesn't
3180 * switch to next array in container and reentry
3181 * conditions for the same array occur.
3182 * This is possibly interim until the behaviour of
3183 * reshape_array is resolved().
3184 */
3185 printf(Name ": Multiple reshape execution detected for "
3186 "device %s.", adev);
3187 close(fd);
3188 break;
3189 }
3190 last_devnum = mdstat->devnum;
3191
1bb174ba 3192 sysfs_init(content, fd, mdstat->devnum);
5da9ab98 3193
78340e26
AK
3194 if (mdmon_running(devname2devnum(container)))
3195 flush_mdmon(container);
3196
1bb174ba 3197 rv = reshape_array(container, fd, adev, st,
19ceb16d 3198 content, force, NULL, 0ULL,
ba728be7 3199 backup_file, verbose, 1, restart,
b76b30e0 3200 freeze_reshape);
5da9ab98 3201 close(fd);
b76b30e0
AK
3202
3203 if (freeze_reshape) {
3204 sysfs_free(cc);
3205 exit(0);
3206 }
3207
493f5dd6 3208 restart = 0;
5da9ab98
N
3209 if (rv)
3210 break;
78340e26
AK
3211
3212 if (mdmon_running(devname2devnum(container)))
3213 flush_mdmon(container);
5da9ab98 3214 }
bcc9e9ed
AK
3215 if (!rv)
3216 unfreeze(st);
1bb174ba 3217 sysfs_free(cc);
5da9ab98
N
3218 exit(0);
3219}
3220
7236ee7a
N
3221/*
3222 * We run a child process in the background which performs the following
3223 * steps:
3224 * - wait for resync to reach a certain point
3225 * - suspend io to the following section
3226 * - backup that section
3227 * - allow resync to proceed further
3228 * - resume io
3229 * - discard the backup.
3230 *
3231 * When are combined in slightly different ways in the three cases.
3232 * Grow:
3233 * - suspend/backup/allow/wait/resume/discard
3234 * Shrink:
3235 * - allow/wait/suspend/backup/allow/wait/resume/discard
3236 * same-size:
3237 * - wait/resume/discard/suspend/backup/allow
3238 *
3239 * suspend/backup/allow always come together
3240 * wait/resume/discard do too.
3241 * For the same-size case we have two backups to improve flow.
24daa16f 3242 *
7236ee7a 3243 */
e86c9dd6 3244
7443ee81
N
3245int progress_reshape(struct mdinfo *info, struct reshape *reshape,
3246 unsigned long long backup_point,
3247 unsigned long long wait_point,
3248 unsigned long long *suspend_point,
3249 unsigned long long *reshape_completed)
3250{
3251 /* This function is called repeatedly by the reshape manager.
3252 * It determines how much progress can safely be made and allows
3253 * that progress.
3254 * - 'info' identifies the array and particularly records in
3255 * ->reshape_progress the metadata's knowledge of progress
3256 * This is a sector offset from the start of the array
3257 * of the next array block to be relocated. This number
3258 * may increase from 0 or decrease from array_size, depending
3259 * on the type of reshape that is happening.
3260 * Note that in contrast, 'sync_completed' is a block count of the
3261 * reshape so far. It gives the distance between the start point
3262 * (head or tail of device) and the next place that data will be
3263 * written. It always increases.
3264 * - 'reshape' is the structure created by analyse_change
3265 * - 'backup_point' shows how much the metadata manager has backed-up
3266 * data. For reshapes with increasing progress, it is the next address
3267 * to be backed up, previous addresses have been backed-up. For
3268 * decreasing progress, it is the earliest address that has been
3269 * backed up - later address are also backed up.
3270 * So addresses between reshape_progress and backup_point are
3271 * backed up providing those are in the 'correct' order.
3272 * - 'wait_point' is an array address. When reshape_completed
3273 * passes this point, progress_reshape should return. It might
3274 * return earlier if it determines that ->reshape_progress needs
3275 * to be updated or further backup is needed.
3276 * - suspend_point is maintained by progress_reshape and the caller
3277 * should not touch it except to initialise to zero.
3278 * It is an array address and it only increases in 2.6.37 and earlier.
b6b95155 3279 * This makes it difficult to handle reducing reshapes with
7443ee81
N
3280 * external metadata.
3281 * However: it is similar to backup_point in that it records the
3282 * other end of a suspended region from reshape_progress.
3283 * it is moved to extend the region that is safe to backup and/or
3284 * reshape
3285 * - reshape_completed is read from sysfs and returned. The caller
3286 * should copy this into ->reshape_progress when it has reason to
3287 * believe that the metadata knows this, and any backup outside this
3288 * has been erased.
3289 *
3290 * Return value is:
3291 * 1 if more data from backup_point - but only as far as suspend_point,
3292 * should be backed up
3293 * 0 if things are progressing smoothly
9e034f70
N
3294 * -1 if the reshape is finished because it is all done,
3295 * -2 if the reshape is finished due to an error.
7443ee81
N
3296 */
3297
3298 int advancing = (reshape->after.data_disks
3299 >= reshape->before.data_disks);
38dad34a
N
3300 unsigned long long need_backup; /* All data between start of array and
3301 * here will at some point need to
3302 * be backed up.
d0ab945e 3303 */
7443ee81 3304 unsigned long long read_offset, write_offset;
b4f8e38b 3305 unsigned long long write_range;
7443ee81 3306 unsigned long long max_progress, target, completed;
d0ab945e
N
3307 unsigned long long array_size = (info->component_size
3308 * reshape->before.data_disks);
7443ee81 3309 int fd;
77a73a17 3310 char buf[20];
7443ee81
N
3311
3312 /* First, we unsuspend any region that is now known to be safe.
3313 * If suspend_point is on the 'wrong' side of reshape_progress, then
3314 * we don't have or need suspension at the moment. This is true for
3315 * native metadata when we don't need to back-up.
3316 */
3317 if (advancing) {
49cd738b 3318 if (info->reshape_progress <= *suspend_point)
7443ee81
N
3319 sysfs_set_num(info, NULL, "suspend_lo",
3320 info->reshape_progress);
3321 } else {
3322 /* Note: this won't work in 2.6.37 and before.
3323 * Something somewhere should make sure we don't need it!
3324 */
49cd738b 3325 if (info->reshape_progress >= *suspend_point)
7443ee81
N
3326 sysfs_set_num(info, NULL, "suspend_hi",
3327 info->reshape_progress);
3328 }
3329
3330 /* Now work out how far it is safe to progress.
3331 * If the read_offset for ->reshape_progress is less than
3332 * 'blocks' beyond the write_offset, we can only progress as far
3333 * as a backup.
3334 * Otherwise we can progress until the write_offset for the new location
3335 * reaches (within 'blocks' of) the read_offset at the current location.
3336 * However that region must be suspended unless we are using native
3337 * metadata.
3338 * If we need to suspend more, we limit it to 128M per device, which is
3339 * rather arbitrary and should be some time-based calculation.
3340 */
ec757320
N
3341 read_offset = info->reshape_progress / reshape->before.data_disks;
3342 write_offset = info->reshape_progress / reshape->after.data_disks;
b4f8e38b 3343 write_range = info->new_chunk/512;
38dad34a
N
3344 if (reshape->before.data_disks == reshape->after.data_disks)
3345 need_backup = array_size;
3346 else
3347 need_backup = reshape->backup_blocks;
7443ee81 3348 if (advancing) {
38dad34a 3349 if (read_offset < write_offset + write_range)
7443ee81 3350 max_progress = backup_point;
ddee071d 3351 else
7443ee81 3352 max_progress =
2f3dd5e4
N
3353 read_offset *
3354 reshape->after.data_disks;
7443ee81 3355 } else {
38dad34a
N
3356 if (read_offset > write_offset - write_range)
3357 /* Can only progress as far as has been backed up,
3358 * which must be suspended */
7443ee81 3359 max_progress = backup_point;
38dad34a
N
3360 else if (info->reshape_progress <= need_backup)
3361 max_progress = backup_point;
3362 else {
3363 if (info->array.major_version >= 0)
3364 /* Can progress until backup is needed */
3365 max_progress = need_backup;
3366 else {
3367 /* Can progress until metadata update is required */
3368 max_progress =
3369 read_offset *
3370 reshape->after.data_disks;
3371 /* but data must be suspended */
3372 if (max_progress < *suspend_point)
3373 max_progress = *suspend_point;
3374 }
7443ee81
N
3375 }
3376 }
3377
3378 /* We know it is safe to progress to 'max_progress' providing
3379 * it is suspended or we are using native metadata.
3380 * Consider extending suspend_point 128M per device if it
3381 * is less than 64M per device beyond reshape_progress.
3382 * But always do a multiple of 'blocks'
832b2b9a
N
3383 * FIXME this is too big - it takes to long to complete
3384 * this much.
7443ee81
N
3385 */
3386 target = 64*1024*2 * min(reshape->before.data_disks,
24daa16f 3387 reshape->after.data_disks);
b4f8e38b 3388 target /= reshape->backup_blocks;
7443ee81
N
3389 if (target < 2)
3390 target = 2;
b4f8e38b 3391 target *= reshape->backup_blocks;
7443ee81
N
3392
3393 /* For externally managed metadata we always need to suspend IO to
3394 * the area being reshaped so we regularly push suspend_point forward.
3395 * For native metadata we only need the suspend if we are going to do
3396 * a backup.
3397 */
3398 if (advancing) {
d0ab945e
N
3399 if ((need_backup > info->reshape_progress
3400 || info->array.major_version < 0) &&
7443ee81 3401 *suspend_point < info->reshape_progress + target) {
d0ab945e
N
3402 if (need_backup < *suspend_point + 2 * target)
3403 *suspend_point = need_backup;
3404 else if (*suspend_point + 2 * target < array_size)
7443ee81 3405 *suspend_point += 2 * target;
d0ab945e
N
3406 else
3407 *suspend_point = array_size;
7443ee81 3408 sysfs_set_num(info, NULL, "suspend_hi", *suspend_point);
d0ab945e
N
3409 if (max_progress > *suspend_point)
3410 max_progress = *suspend_point;
7443ee81
N
3411 }
3412 } else {
38dad34a
N
3413 if (info->array.major_version >= 0) {
3414 /* Only need to suspend when about to backup */
3415 if (info->reshape_progress < need_backup * 2 &&
3416 *suspend_point > 0) {
d0ab945e 3417 *suspend_point = 0;
38dad34a
N
3418 sysfs_set_num(info, NULL, "suspend_lo", 0);
3419 sysfs_set_num(info, NULL, "suspend_hi", need_backup);
3420 }
3421 } else {
3422 /* Need to suspend continually */
3423 if (info->reshape_progress < *suspend_point)
3424 *suspend_point = info->reshape_progress;
3425 if (*suspend_point + target < info->reshape_progress)
3426 /* No need to move suspend region yet */;
3427 else {
3428 if (*suspend_point >= 2 * target)
3429 *suspend_point -= 2 * target;
3430 else
3431 *suspend_point = 0;
3432 sysfs_set_num(info, NULL, "suspend_lo",
3433 *suspend_point);
3434 }
d0ab945e
N
3435 if (max_progress < *suspend_point)
3436 max_progress = *suspend_point;
7443ee81
N
3437 }
3438 }
3439
3440 /* now set sync_max to allow that progress. sync_max, like
3441 * sync_completed is a count of sectors written per device, so
3442 * we find the difference between max_progress and the start point,
3443 * and divide that by after.data_disks to get a sync_max
3444 * number.
3445 * At the same time we convert wait_point to a similar number
3446 * for comparing against sync_completed.
3447 */
92d1991f 3448 /* scale down max_progress to per_disk */
7443ee81 3449 max_progress /= reshape->after.data_disks;
92d1991f
N
3450 /* Round to chunk size as some kernels give an erroneously high number */
3451 max_progress /= info->new_chunk/512;
3452 max_progress *= info->new_chunk/512;
7f913e9b
N
3453 /* And round to old chunk size as the kernel wants that */
3454 max_progress /= info->array.chunk_size/512;
3455 max_progress *= info->array.chunk_size/512;
92d1991f
N
3456 /* Limit progress to the whole device */
3457 if (max_progress > info->component_size)
3458 max_progress = info->component_size;
7443ee81 3459 wait_point /= reshape->after.data_disks;
92d1991f
N
3460 if (!advancing) {
3461 /* switch from 'device offset' to 'processed block count' */
3462 max_progress = info->component_size - max_progress;
3463 wait_point = info->component_size - wait_point;
3464 }
7443ee81
N
3465
3466 sysfs_set_num(info, NULL, "sync_max", max_progress);
3467
3468 /* Now wait. If we have already reached the point that we were
3469 * asked to wait to, don't wait at all, else wait for any change.
3470 * We need to select on 'sync_completed' as that is the place that
3471 * notifications happen, but we are really interested in
3472 * 'reshape_position'
3473 */
3474 fd = sysfs_get_fd(info, NULL, "sync_completed");
3475 if (fd < 0)
77a73a17 3476 goto check_progress;
7443ee81 3477
621ea11b 3478 if (sysfs_fd_get_ll(fd, &completed) < 0)
77a73a17 3479 goto check_progress;
621ea11b 3480
7443ee81
N
3481 while (completed < max_progress && completed < wait_point) {
3482 /* Check that sync_action is still 'reshape' to avoid
3483 * waiting forever on a dead array
3484 */
3485 char action[20];
3486 fd_set rfds;
3487 if (sysfs_get_str(info, NULL, "sync_action",
3488 action, 20) <= 0 ||
3489 strncmp(action, "reshape", 7) != 0)
3490 break;
26d6e157
AK
3491 /* Some kernels reset 'sync_completed' to zero
3492 * before setting 'sync_action' to 'idle'.
3493 * So we need these extra tests.
3494 */
3495 if (completed == 0 && advancing
3496 && info->reshape_progress > 0)
3497 break;
3498 if (completed == 0 && !advancing
3499 && info->reshape_progress < (info->component_size
3500 * reshape->after.data_disks))
3501 break;
7443ee81
N
3502 FD_ZERO(&rfds);
3503 FD_SET(fd, &rfds);
3504 select(fd+1, NULL, NULL, &rfds, NULL);
621ea11b 3505 if (sysfs_fd_get_ll(fd, &completed) < 0)
77a73a17 3506 goto check_progress;
7443ee81 3507 }
10d0d365
AK
3508 /* Some kernels reset 'sync_completed' to zero,
3509 * we need to have real point we are in md
3510 */
3511 if (completed == 0)
3512 completed = max_progress;
3513
92d1991f
N
3514 /* some kernels can give an incorrectly high 'completed' number */
3515 completed /= (info->new_chunk/512);
3516 completed *= (info->new_chunk/512);
7443ee81
N
3517 /* Convert 'completed' back in to a 'progress' number */
3518 completed *= reshape->after.data_disks;
3519 if (!advancing) {
3520 completed = info->component_size * reshape->after.data_disks
3521 - completed;
3522 }
3523 *reshape_completed = completed;
24daa16f 3524
7443ee81
N
3525 close(fd);
3526
3527 /* We return the need_backup flag. Caller will decide
d0ab945e 3528 * how much - a multiple of ->backup_blocks up to *suspend_point
7443ee81 3529 */
38dad34a
N
3530 if (advancing)
3531 return need_backup > info->reshape_progress;
3532 else
3533 return need_backup >= info->reshape_progress;
77a73a17
N
3534
3535check_progress:
3536 /* if we couldn't read a number from sync_completed, then
3537 * either the reshape did complete, or it aborted.
3538 * We can tell which by checking for 'none' in reshape_position.
621ea11b
N
3539 * If it did abort, then it might immediately restart if it
3540 * it was just a device failure that leaves us degraded but
3541 * functioning.
77a73a17
N
3542 */
3543 strcpy(buf, "hi");
3544 if (sysfs_get_str(info, NULL, "reshape_position", buf, sizeof(buf)) < 0
621ea11b
N
3545 || strncmp(buf, "none", 4) != 0) {
3546 /* The abort might only be temporary. Wait up to 10
3547 * seconds for fd to contain a valid number again.
3548 */
3549 struct timeval tv;
3550 int rv = -2;
3551 tv.tv_sec = 10;
3552 tv.tv_usec = 0;
6560987b 3553 while (fd >= 0 && rv < 0 && tv.tv_sec > 0) {
621ea11b
N
3554 fd_set rfds;
3555 FD_ZERO(&rfds);
3556 FD_SET(fd, &rfds);
3557 if (select(fd+1, NULL, NULL, &rfds, &tv) != 1)
3558 break;
6560987b
N
3559 switch (sysfs_fd_get_ll(fd, &completed)) {
3560 case 0:
621ea11b
N
3561 /* all good again */
3562 rv = 1;
6560987b
N
3563 break;
3564 case -2: /* read error - abort */
3565 tv.tv_sec = 0;
3566 break;
3567 }
621ea11b
N
3568 }
3569 if (fd >= 0)
3570 close(fd);
3571 return rv; /* abort */
3572 } else {
6d5316f6 3573 /* Maybe racing with array shutdown - check state */
621ea11b
N
3574 if (fd >= 0)
3575 close(fd);
6d5316f6
N
3576 if (sysfs_get_str(info, NULL, "array_state", buf, sizeof(buf)) < 0
3577 || strncmp(buf, "inactive", 8) == 0
3578 || strncmp(buf, "clear",5) == 0)
3579 return -2; /* abort */
77a73a17 3580 return -1; /* complete */
6d5316f6 3581 }
7443ee81
N
3582}
3583
fcf57625 3584/* FIXME return status is never checked */
4411fb17 3585static int grow_backup(struct mdinfo *sra,
7236ee7a 3586 unsigned long long offset, /* per device */
7443ee81 3587 unsigned long stripes, /* per device, in old chunks */
7236ee7a
N
3588 int *sources, unsigned long long *offsets,
3589 int disks, int chunk, int level, int layout,
3590 int dests, int *destfd, unsigned long long *destoffsets,
d4445387 3591 int part, int *degraded,
7236ee7a
N
3592 char *buf)
3593{
3594 /* Backup 'blocks' sectors at 'offset' on each device of the array,
3595 * to storage 'destfd' (offset 'destoffsets'), after first
3596 * suspending IO. Then allow resync to continue
3597 * over the suspended section.
3598 * Use part 'part' of the backup-super-block.
3599 */
3600 int odata = disks;
3601 int rv = 0;
3602 int i;
f21e18ca
N
3603 unsigned long long ll;
3604 int new_degraded;
7236ee7a
N
3605 //printf("offset %llu\n", offset);
3606 if (level >= 4)
3607 odata--;
3608 if (level == 6)
3609 odata--;
7443ee81 3610
d4445387 3611 /* Check that array hasn't become degraded, else we might backup the wrong data */
2c6ac128
N
3612 if (sysfs_get_ll(sra, NULL, "degraded", &ll) < 0)
3613 return -1; /* FIXME this error is ignored */
f21e18ca 3614 new_degraded = (int)ll;
d4445387
N
3615 if (new_degraded != *degraded) {
3616 /* check each device to ensure it is still working */
3617 struct mdinfo *sd;
3618 for (sd = sra->devs ; sd ; sd = sd->next) {
3619 if (sd->disk.state & (1<<MD_DISK_FAULTY))
3620 continue;
3621 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
3622 char sbuf[20];
3623 if (sysfs_get_str(sra, sd, "state", sbuf, 20) < 0 ||
3624 strstr(sbuf, "faulty") ||
3625 strstr(sbuf, "in_sync") == NULL) {
3626 /* this device is dead */
3627 sd->disk.state = (1<<MD_DISK_FAULTY);
3628 if (sd->disk.raid_disk >= 0 &&
3629 sources[sd->disk.raid_disk] >= 0) {
3630 close(sources[sd->disk.raid_disk]);
3631 sources[sd->disk.raid_disk] = -1;
3632 }
3633 }
3634 }
3635 }
3636 *degraded = new_degraded;
3637 }
7236ee7a
N
3638 if (part) {
3639 bsb.arraystart2 = __cpu_to_le64(offset * odata);
200871ad 3640 bsb.length2 = __cpu_to_le64(stripes * (chunk/512) * odata);
7236ee7a
N
3641 } else {
3642 bsb.arraystart = __cpu_to_le64(offset * odata);
200871ad 3643 bsb.length = __cpu_to_le64(stripes * (chunk/512) * odata);
7236ee7a
N
3644 }
3645 if (part)
3646 bsb.magic[15] = '2';
3647 for (i = 0; i < dests; i++)
3648 if (part)
3649 lseek64(destfd[i], destoffsets[i] + __le64_to_cpu(bsb.devstart2)*512, 0);
3650 else
3651 lseek64(destfd[i], destoffsets[i], 0);
3652
24daa16f 3653 rv = save_stripes(sources, offsets,
7236ee7a
N
3654 disks, chunk, level, layout,
3655 dests, destfd,
3656 offset*512*odata, stripes * chunk * odata,
3657 buf);
3658
3659 if (rv)
3660 return rv;
97396422 3661 bsb.mtime = __cpu_to_le64(time(0));
7236ee7a
N
3662 for (i = 0; i < dests; i++) {
3663 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
3664
3665 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
3666 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
3667 bsb.sb_csum2 = bsb_csum((char*)&bsb,
3668 ((char*)&bsb.sb_csum2)-((char*)&bsb));
3669
72044953 3670 rv = -1;
f21e18ca
N
3671 if ((unsigned long long)lseek64(destfd[i], destoffsets[i] - 4096, 0)
3672 != destoffsets[i] - 4096)
72044953
N
3673 break;
3674 if (write(destfd[i], &bsb, 512) != 512)
3675 break;
ff94fb86 3676 if (destoffsets[i] > 4096) {
f21e18ca 3677 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]+stripes*chunk*odata, 0) !=
a847575a 3678 destoffsets[i]+stripes*chunk*odata)
72044953
N
3679 break;
3680 if (write(destfd[i], &bsb, 512) != 512)
3681 break;
ff94fb86 3682 }
7236ee7a 3683 fsync(destfd[i]);
72044953 3684 rv = 0;
7236ee7a 3685 }
2efedc7b 3686
fcf57625 3687 return rv;
7236ee7a
N
3688}
3689
3690/* in 2.6.30, the value reported by sync_completed can be
3691 * less that it should be by one stripe.
3692 * This only happens when reshape hits sync_max and pauses.
3693 * So allow wait_backup to either extent sync_max further
3694 * than strictly necessary, or return before the
3695 * sync has got quite as far as we would really like.
3696 * This is what 'blocks2' is for.
3697 * The various caller give appropriate values so that
3698 * every works.
3699 */
fcf57625 3700/* FIXME return value is often ignored */
24daa16f
N
3701static int forget_backup(int dests, int *destfd,
3702 unsigned long long *destoffsets,
3703 int part)
7236ee7a 3704{
24daa16f 3705 /*
7443ee81 3706 * Erase backup 'part' (which is 0 or 1)
7236ee7a 3707 */
7236ee7a 3708 int i;
fcf57625 3709 int rv;
7236ee7a 3710
7236ee7a
N
3711 if (part) {
3712 bsb.arraystart2 = __cpu_to_le64(0);
3713 bsb.length2 = __cpu_to_le64(0);
3714 } else {
3715 bsb.arraystart = __cpu_to_le64(0);
3716 bsb.length = __cpu_to_le64(0);
3717 }
97396422 3718 bsb.mtime = __cpu_to_le64(time(0));
fcf57625 3719 rv = 0;
7236ee7a
N
3720 for (i = 0; i < dests; i++) {
3721 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
3722 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
3723 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
3724 bsb.sb_csum2 = bsb_csum((char*)&bsb,
3725 ((char*)&bsb.sb_csum2)-((char*)&bsb));
f21e18ca 3726 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]-4096, 0) !=
a847575a 3727 destoffsets[i]-4096)
72044953 3728 rv = -1;
24daa16f 3729 if (rv == 0 &&
72044953
N
3730 write(destfd[i], &bsb, 512) != 512)
3731 rv = -1;
7236ee7a
N
3732 fsync(destfd[i]);
3733 }
fcf57625 3734 return rv;
7236ee7a 3735}
e86c9dd6 3736
7236ee7a
N
3737static void fail(char *msg)
3738{
fcf57625 3739 int rv;
72044953
N
3740 rv = (write(2, msg, strlen(msg)) != (int)strlen(msg));
3741 rv |= (write(2, "\n", 1) != 1);
fcf57625 3742 exit(rv ? 1 : 2);
7236ee7a
N
3743}
3744
3745static char *abuf, *bbuf;
f21e18ca 3746static unsigned long long abuflen;
7236ee7a
N
3747static void validate(int afd, int bfd, unsigned long long offset)
3748{
3749 /* check that the data in the backup against the array.
3750 * This is only used for regression testing and should not
3751 * be used while the array is active
3752 */
7236ee7a
N
3753 if (afd < 0)
3754 return;
3755 lseek64(bfd, offset - 4096, 0);
3756 if (read(bfd, &bsb2, 512) != 512)
3757 fail("cannot read bsb");
3758 if (bsb2.sb_csum != bsb_csum((char*)&bsb2,
3759 ((char*)&bsb2.sb_csum)-((char*)&bsb2)))
3760 fail("first csum bad");
3761 if (memcmp(bsb2.magic, "md_backup_data", 14) != 0)
3762 fail("magic is bad");
3763 if (memcmp(bsb2.magic, "md_backup_data-2", 16) == 0 &&
3764 bsb2.sb_csum2 != bsb_csum((char*)&bsb2,
24daa16f 3765 ((char*)&bsb2.sb_csum2)-((char*)&bsb2)))
7236ee7a
N
3766 fail("second csum bad");
3767
3768 if (__le64_to_cpu(bsb2.devstart)*512 != offset)
3769 fail("devstart is wrong");
3770
3771 if (bsb2.length) {
3772 unsigned long long len = __le64_to_cpu(bsb2.length)*512;
3773
3774 if (abuflen < len) {
3775 free(abuf);
3776 free(bbuf);
3777 abuflen = len;
fcf57625
N
3778 if (posix_memalign((void**)&abuf, 4096, abuflen) ||
3779 posix_memalign((void**)&bbuf, 4096, abuflen)) {
3780 abuflen = 0;
3781 /* just stop validating on mem-alloc failure */
3782 return;
3783 }
e86c9dd6 3784 }
48924014 3785
7236ee7a 3786 lseek64(bfd, offset, 0);
f21e18ca 3787 if ((unsigned long long)read(bfd, bbuf, len) != len) {
080fd005 3788 //printf("len %llu\n", len);
7236ee7a
N
3789 fail("read first backup failed");
3790 }
3791 lseek64(afd, __le64_to_cpu(bsb2.arraystart)*512, 0);
f21e18ca 3792 if ((unsigned long long)read(afd, abuf, len) != len)
7236ee7a
N
3793 fail("read first from array failed");
3794 if (memcmp(bbuf, abuf, len) != 0) {
24daa16f 3795#if 0
7236ee7a
N
3796 int i;
3797 printf("offset=%llu len=%llu\n",
080fd005 3798 (unsigned long long)__le64_to_cpu(bsb2.arraystart)*512, len);
7236ee7a
N
3799 for (i=0; i<len; i++)
3800 if (bbuf[i] != abuf[i]) {
3801 printf("first diff byte %d\n", i);
93ecfa01 3802 break;
7236ee7a 3803 }
24daa16f 3804#endif
7236ee7a 3805 fail("data1 compare failed");
e86c9dd6 3806 }
7236ee7a
N
3807 }
3808 if (bsb2.length2) {
3809 unsigned long long len = __le64_to_cpu(bsb2.length2)*512;
3810
3811 if (abuflen < len) {
3812 free(abuf);
3813 free(bbuf);
3814 abuflen = len;
503975b9
N
3815 abuf = xmalloc(abuflen);
3816 bbuf = xmalloc(abuflen);
e86c9dd6
NB
3817 }
3818
7236ee7a 3819 lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0);
f21e18ca 3820 if ((unsigned long long)read(bfd, bbuf, len) != len)
7236ee7a
N
3821 fail("read second backup failed");
3822 lseek64(afd, __le64_to_cpu(bsb2.arraystart2)*512, 0);
f21e18ca 3823 if ((unsigned long long)read(afd, abuf, len) != len)
7236ee7a
N
3824 fail("read second from array failed");
3825 if (memcmp(bbuf, abuf, len) != 0)
3826 fail("data2 compare failed");
e86c9dd6 3827 }
7236ee7a 3828}
e86c9dd6 3829
999b4972
N
3830int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
3831 struct supertype *st, unsigned long blocks,
3832 int *fds, unsigned long long *offsets,
3833 int dests, int *destfd, unsigned long long *destoffsets)
7236ee7a 3834{
7443ee81
N
3835 /* Monitor a reshape where backup is being performed using
3836 * 'native' mechanism - either to a backup file, or
3837 * to some space in a spare.
3838 */
7236ee7a 3839 char *buf;
7443ee81
N
3840 int degraded = -1;
3841 unsigned long long speed;
3842 unsigned long long suspend_point, array_size;
3843 unsigned long long backup_point, wait_point;
3844 unsigned long long reshape_completed;
3845 int done = 0;
3846 int increasing = reshape->after.data_disks >= reshape->before.data_disks;
3847 int part = 0; /* The next part of the backup area to fill. It may already
3848 * be full, so we need to check */
3849 int level = reshape->level;
3850 int layout = reshape->before.layout;
3851 int data = reshape->before.data_disks;
3852 int disks = reshape->before.data_disks + reshape->parity;
3853 int chunk = sra->array.chunk_size;
da8100ca
N
3854 struct mdinfo *sd;
3855 unsigned long stripes;
3b7e9d0c 3856 int uuid[4];
da8100ca
N
3857
3858 /* set up the backup-super-block. This requires the
3859 * uuid from the array.
3860 */
3861 /* Find a superblock */
3862 for (sd = sra->devs; sd; sd = sd->next) {
3863 char *dn;
3864 int devfd;
3865 int ok;
3866 if (sd->disk.state & (1<<MD_DISK_FAULTY))
3867 continue;
3868 dn = map_dev(sd->disk.major, sd->disk.minor, 1);
3869 devfd = dev_open(dn, O_RDONLY);
3870 if (devfd < 0)
3871 continue;
3872 ok = st->ss->load_super(st, devfd, NULL);
3873 close(devfd);
77f8d358 3874 if (ok == 0)
da8100ca
N
3875 break;
3876 }
3877 if (!sd) {
e7b84f9d 3878 pr_err("Cannot find a superblock\n");
da8100ca
N
3879 return 0;
3880 }
3881
3882 memset(&bsb, 0, 512);
3883 memcpy(bsb.magic, "md_backup_data-1", 16);
3b7e9d0c
LB
3884 st->ss->uuid_from_super(st, uuid);
3885 memcpy(bsb.set_uuid, uuid, 16);
da8100ca
N
3886 bsb.mtime = __cpu_to_le64(time(0));
3887 bsb.devstart2 = blocks;
3888
3889 stripes = blocks / (sra->array.chunk_size/512) /
3890 reshape->before.data_disks;
e86c9dd6 3891
fcf57625
N
3892 if (posix_memalign((void**)&buf, 4096, disks * chunk))
3893 /* Don't start the 'reshape' */
3894 return 0;
7443ee81
N
3895 if (reshape->before.data_disks == reshape->after.data_disks) {
3896 sysfs_get_ll(sra, NULL, "sync_speed_min", &speed);
3897 sysfs_set_num(sra, NULL, "sync_speed_min", 200000);
3898 }
e86c9dd6 3899
7443ee81 3900 if (increasing) {
96533072 3901 array_size = sra->component_size * reshape->after.data_disks;
7443ee81
N
3902 backup_point = sra->reshape_progress;
3903 suspend_point = 0;
3904 } else {
96533072 3905 array_size = sra->component_size * reshape->before.data_disks;
25da62d9 3906 backup_point = reshape->backup_blocks;
7443ee81
N
3907 suspend_point = array_size;
3908 }
7236ee7a 3909
7443ee81
N
3910 while (!done) {
3911 int rv;
7236ee7a 3912
7443ee81
N
3913 /* Want to return as soon the oldest backup slot can
3914 * be released as that allows us to start backing up
3915 * some more, providing suspend_point has been
b6b95155 3916 * advanced, which it should have.
7443ee81
N
3917 */
3918 if (increasing) {
3919 wait_point = array_size;
3920 if (part == 0 && __le64_to_cpu(bsb.length) > 0)
3921 wait_point = (__le64_to_cpu(bsb.arraystart) +
3922 __le64_to_cpu(bsb.length));
3923 if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
3924 wait_point = (__le64_to_cpu(bsb.arraystart2) +
3925 __le64_to_cpu(bsb.length2));
3926 } else {
3927 wait_point = 0;
3928 if (part == 0 && __le64_to_cpu(bsb.length) > 0)
3929 wait_point = __le64_to_cpu(bsb.arraystart);
3930 if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
3931 wait_point = __le64_to_cpu(bsb.arraystart2);
3932 }
3933
3934 rv = progress_reshape(sra, reshape,
3935 backup_point, wait_point,
3936 &suspend_point, &reshape_completed);
7443ee81
N
3937 /* external metadata would need to ping_monitor here */
3938 sra->reshape_progress = reshape_completed;
7236ee7a 3939
7443ee81
N
3940 /* Clear any backup region that is before 'here' */
3941 if (increasing) {
b3cf095a
N
3942 if (__le64_to_cpu(bsb.length) > 0 &&
3943 reshape_completed >= (__le64_to_cpu(bsb.arraystart) +
7443ee81
N
3944 __le64_to_cpu(bsb.length)))
3945 forget_backup(dests, destfd,
3946 destoffsets, 0);
b3cf095a
N
3947 if (__le64_to_cpu(bsb.length2) > 0 &&
3948 reshape_completed >= (__le64_to_cpu(bsb.arraystart2) +
7443ee81
N
3949 __le64_to_cpu(bsb.length2)))
3950 forget_backup(dests, destfd,
3951 destoffsets, 1);
3952 } else {
b3cf095a
N
3953 if (__le64_to_cpu(bsb.length) > 0 &&
3954 reshape_completed <= (__le64_to_cpu(bsb.arraystart)))
7443ee81
N
3955 forget_backup(dests, destfd,
3956 destoffsets, 0);
b3cf095a
N
3957 if (__le64_to_cpu(bsb.length2) > 0 &&
3958 reshape_completed <= (__le64_to_cpu(bsb.arraystart2)))
7443ee81
N
3959 forget_backup(dests, destfd,
3960 destoffsets, 1);
3961 }
7236ee7a 3962
223c5c99 3963 if (rv < 0) {
77a73a17
N
3964 if (rv == -1)
3965 done = 1;
223c5c99
N
3966 break;
3967 }
2d4de5f9
N
3968 if (rv == 0 && increasing && !st->ss->external) {
3969 /* No longer need to monitor this reshape */
3970 done = 1;
3971 break;
3972 }
223c5c99 3973
60204e4e 3974 while (rv) {
7443ee81 3975 unsigned long long offset;
e97ca002 3976 unsigned long actual_stripes;
60204e4e
N
3977 /* Need to backup some data.
3978 * If 'part' is not used and the desired
3979 * backup size is suspended, do a backup,
3980 * then consider the next part.
3981 */
7443ee81
N
3982 /* Check that 'part' is unused */
3983 if (part == 0 && __le64_to_cpu(bsb.length) != 0)
60204e4e 3984 break;
7443ee81 3985 if (part == 1 && __le64_to_cpu(bsb.length2) != 0)
60204e4e 3986 break;
7443ee81
N
3987
3988 offset = backup_point / data;
e97ca002 3989 actual_stripes = stripes;
60204e4e 3990 if (increasing) {
e97ca002
N
3991 if (offset + actual_stripes * (chunk/512) >
3992 sra->component_size)
3993 actual_stripes = ((sra->component_size - offset)
3994 / (chunk/512));
3995 if (offset + actual_stripes * (chunk/512) >
60204e4e
N
3996 suspend_point/data)
3997 break;
3998 } else {
e97ca002
N
3999 if (offset < actual_stripes * (chunk/512))
4000 actual_stripes = offset / (chunk/512);
4001 offset -= actual_stripes * (chunk/512);
4002 if (offset < suspend_point/data)
60204e4e
N
4003 break;
4004 }
e4b11073
N
4005 if (actual_stripes == 0)
4006 break;
e97ca002 4007 grow_backup(sra, offset, actual_stripes,
7443ee81
N
4008 fds, offsets,
4009 disks, chunk, level, layout,
4010 dests, destfd, destoffsets,
4011 part, &degraded, buf);
4012 validate(afd, destfd[0], destoffsets[0]);
4013 /* record where 'part' is up to */
4014 part = !part;
4015 if (increasing)
e97ca002 4016 backup_point += actual_stripes * (chunk/512) * data;
7443ee81 4017 else
e97ca002 4018 backup_point -= actual_stripes * (chunk/512) * data;
7443ee81
N
4019 }
4020 }
4021
223c5c99
N
4022 /* FIXME maybe call progress_reshape one more time instead */
4023 abort_reshape(sra); /* remove any remaining suspension */
7443ee81
N
4024 if (reshape->before.data_disks == reshape->after.data_disks)
4025 sysfs_set_num(sra, NULL, "sync_speed_min", speed);
7236ee7a 4026 free(buf);
77a73a17 4027 return done;
e86c9dd6 4028}
353632d9
NB
4029
4030/*
4031 * If any spare contains md_back_data-1 which is recent wrt mtime,
4032 * write that data into the array and update the super blocks with
4033 * the new reshape_progress
4034 */
ea0ebe96
N
4035int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt,
4036 char *backup_file, int verbose)
353632d9
NB
4037{
4038 int i, j;
4039 int old_disks;
353632d9 4040 unsigned long long *offsets;
82f2d6ab 4041 unsigned long long nstripe, ostripe;
6e9eac4f 4042 int ndata, odata;
353632d9 4043
e9e43ec3
N
4044 odata = info->array.raid_disks - info->delta_disks - 1;
4045 if (info->array.level == 6) odata--; /* number of data disks */
4046 ndata = info->array.raid_disks - 1;
4047 if (info->new_level == 6) ndata--;
353632d9
NB
4048
4049 old_disks = info->array.raid_disks - info->delta_disks;
4050
e9e43ec3
N
4051 if (info->delta_disks <= 0)
4052 /* Didn't grow, so the backup file must have
4053 * been used
4054 */
4055 old_disks = cnt;
06b0d786 4056 for (i=old_disks-(backup_file?1:0); i<cnt; i++) {
353632d9 4057 struct mdinfo dinfo;
06b0d786 4058 int fd;
e9e43ec3 4059 int bsbsize;
ea0ebe96 4060 char *devname, namebuf[20];
10af14c4 4061 unsigned long long lo, hi;
353632d9
NB
4062
4063 /* This was a spare and may have some saved data on it.
4064 * Load the superblock, find and load the
4065 * backup_super_block.
4066 * If either fail, go on to next device.
4067 * If the backup contains no new info, just return
206c5eae 4068 * else restore data and update all superblocks
353632d9 4069 */
06b0d786
NB
4070 if (i == old_disks-1) {
4071 fd = open(backup_file, O_RDONLY);
e9e43ec3 4072 if (fd<0) {
e7b84f9d 4073 pr_err("backup file %s inaccessible: %s\n",
e9e43ec3 4074 backup_file, strerror(errno));
06b0d786 4075 continue;
e9e43ec3 4076 }
ea0ebe96 4077 devname = backup_file;
06b0d786
NB
4078 } else {
4079 fd = fdlist[i];
4080 if (fd < 0)
4081 continue;
3da92f27 4082 if (st->ss->load_super(st, fd, NULL))
06b0d786 4083 continue;
353632d9 4084
a5d85af7 4085 st->ss->getinfo_super(st, &dinfo, NULL);
3da92f27
NB
4086 st->ss->free_super(st);
4087
06b0d786
NB
4088 if (lseek64(fd,
4089 (dinfo.data_offset + dinfo.component_size - 8) <<9,
ea0ebe96 4090 0) < 0) {
e7b84f9d 4091 pr_err("Cannot seek on device %d\n", i);
06b0d786 4092 continue; /* Cannot seek */
ea0ebe96
N
4093 }
4094 sprintf(namebuf, "device-%d", i);
4095 devname = namebuf;
06b0d786 4096 }
ea0ebe96
N
4097 if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb)) {
4098 if (verbose)
e7b84f9d 4099 pr_err("Cannot read from %s\n", devname);
353632d9 4100 continue; /* Cannot read */
ea0ebe96 4101 }
e9e43ec3 4102 if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0 &&
ea0ebe96
N
4103 memcmp(bsb.magic, "md_backup_data-2", 16) != 0) {
4104 if (verbose)
e7b84f9d 4105 pr_err("No backup metadata on %s\n", devname);
353632d9 4106 continue;
ea0ebe96
N
4107 }
4108 if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb))) {
4109 if (verbose)
e7b84f9d 4110 pr_err("Bad backup-metadata checksum on %s\n", devname);
353632d9 4111 continue; /* bad checksum */
ea0ebe96 4112 }
e9e43ec3 4113 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0 &&
ea0ebe96
N
4114 bsb.sb_csum2 != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum2)-((char*)&bsb))) {
4115 if (verbose)
e7b84f9d 4116 pr_err("Bad backup-metadata checksum2 on %s\n", devname);
22e30516 4117 continue; /* Bad second checksum */
ea0ebe96
N
4118 }
4119 if (memcmp(bsb.set_uuid,info->uuid, 16) != 0) {
4120 if (verbose)
e7b84f9d 4121 pr_err("Wrong uuid on backup-metadata on %s\n", devname);
353632d9 4122 continue; /* Wrong uuid */
ea0ebe96 4123 }
353632d9 4124
097075b6
N
4125 /* array utime and backup-mtime should be updated at much the same time, but it seems that
4126 * sometimes they aren't... So allow considerable flexability in matching, and allow
4127 * this test to be overridden by an environment variable.
4128 */
f21e18ca
N
4129 if (info->array.utime > (int)__le64_to_cpu(bsb.mtime) + 2*60*60 ||
4130 info->array.utime < (int)__le64_to_cpu(bsb.mtime) - 10*60) {
097075b6 4131 if (check_env("MDADM_GROW_ALLOW_OLD")) {
e7b84f9d 4132 pr_err("accepting backup with timestamp %lu "
097075b6
N
4133 "for array with timestamp %lu\n",
4134 (unsigned long)__le64_to_cpu(bsb.mtime),
4135 (unsigned long)info->array.utime);
4136 } else {
676ab312
N
4137 pr_err("too-old timestamp on backup-metadata on %s\n", devname);
4138 pr_err("If you think it is should be safe, try 'export MDADM_GROW_ALLOW_OLD=1'\n");
097075b6
N
4139 continue; /* time stamp is too bad */
4140 }
ea0ebe96 4141 }
353632d9 4142
e9e43ec3 4143 if (bsb.magic[15] == '1') {
10af14c4
N
4144 if (bsb.length == 0)
4145 continue;
e7a71c6b
N
4146 if (info->delta_disks >= 0) {
4147 /* reshape_progress is increasing */
4148 if (__le64_to_cpu(bsb.arraystart)
4149 + __le64_to_cpu(bsb.length)
4150 < info->reshape_progress) {
4151 nonew:
4152 if (verbose)
e7b84f9d 4153 pr_err("backup-metadata found on %s but is not needed\n", devname);
e7a71c6b
N
4154 continue; /* No new data here */
4155 }
4156 } else {
4157 /* reshape_progress is decreasing */
4158 if (__le64_to_cpu(bsb.arraystart) >=
4159 info->reshape_progress)
4160 goto nonew; /* No new data here */
ea0ebe96 4161 }
e9e43ec3 4162 } else {
10af14c4
N
4163 if (bsb.length == 0 && bsb.length2 == 0)
4164 continue;
e7a71c6b
N
4165 if (info->delta_disks >= 0) {
4166 /* reshape_progress is increasing */
4167 if ((__le64_to_cpu(bsb.arraystart)
4168 + __le64_to_cpu(bsb.length)
4169 < info->reshape_progress)
4170 &&
4171 (__le64_to_cpu(bsb.arraystart2)
4172 + __le64_to_cpu(bsb.length2)
4173 < info->reshape_progress))
4174 goto nonew; /* No new data here */
4175 } else {
4176 /* reshape_progress is decreasing */
4177 if (__le64_to_cpu(bsb.arraystart) >=
4178 info->reshape_progress &&
4179 __le64_to_cpu(bsb.arraystart2) >=
4180 info->reshape_progress)
4181 goto nonew; /* No new data here */
4182 }
e9e43ec3 4183 }
ea0ebe96
N
4184 if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0) {
4185 second_fail:
4186 if (verbose)
e7b84f9d
N
4187 pr_err("Failed to verify secondary backup-metadata block on %s\n",
4188 devname);
353632d9 4189 continue; /* Cannot seek */
ea0ebe96 4190 }
2efedc7b 4191 /* There should be a duplicate backup superblock 4k before here */
06b0d786 4192 if (lseek64(fd, -4096, 1) < 0 ||
0155af90 4193 read(fd, &bsb2, sizeof(bsb2)) != sizeof(bsb2))
ea0ebe96 4194 goto second_fail; /* Cannot find leading superblock */
e9e43ec3
N
4195 if (bsb.magic[15] == '1')
4196 bsbsize = offsetof(struct mdp_backup_super, pad1);
4197 else
4198 bsbsize = offsetof(struct mdp_backup_super, pad);
ff94fb86 4199 if (memcmp(&bsb2, &bsb, bsbsize) != 0)
ea0ebe96 4200 goto second_fail; /* Cannot find leading superblock */
2efedc7b 4201
353632d9 4202 /* Now need the data offsets for all devices. */
503975b9 4203 offsets = xmalloc(sizeof(*offsets)*info->array.raid_disks);
353632d9
NB
4204 for(j=0; j<info->array.raid_disks; j++) {
4205 if (fdlist[j] < 0)
4206 continue;
3da92f27 4207 if (st->ss->load_super(st, fdlist[j], NULL))
353632d9
NB
4208 /* FIXME should be this be an error */
4209 continue;
a5d85af7 4210 st->ss->getinfo_super(st, &dinfo, NULL);
3da92f27 4211 st->ss->free_super(st);
14e5b4d7 4212 offsets[j] = dinfo.data_offset * 512;
353632d9
NB
4213 }
4214 printf(Name ": restoring critical section\n");
4215
4216 if (restore_stripes(fdlist, offsets,
4217 info->array.raid_disks,
4218 info->new_chunk,
4219 info->new_level,
4220 info->new_layout,
06b0d786 4221 fd, __le64_to_cpu(bsb.devstart)*512,
92dcdf7c 4222 __le64_to_cpu(bsb.arraystart)*512,
2fcb75ae 4223 __le64_to_cpu(bsb.length)*512, NULL)) {
e9e43ec3 4224 /* didn't succeed, so giveup */
ea0ebe96 4225 if (verbose)
e7b84f9d 4226 pr_err("Error restoring backup from %s\n",
ea0ebe96 4227 devname);
730ae51f 4228 free(offsets);
e9e43ec3
N
4229 return 1;
4230 }
24daa16f 4231
e9e43ec3
N
4232 if (bsb.magic[15] == '2' &&
4233 restore_stripes(fdlist, offsets,
4234 info->array.raid_disks,
4235 info->new_chunk,
4236 info->new_level,
4237 info->new_layout,
4238 fd, __le64_to_cpu(bsb.devstart)*512 +
4239 __le64_to_cpu(bsb.devstart2)*512,
92dcdf7c 4240 __le64_to_cpu(bsb.arraystart2)*512,
2fcb75ae 4241 __le64_to_cpu(bsb.length2)*512, NULL)) {
353632d9 4242 /* didn't succeed, so giveup */
ea0ebe96 4243 if (verbose)
e7b84f9d 4244 pr_err("Error restoring second backup from %s\n",
ea0ebe96 4245 devname);
730ae51f 4246 free(offsets);
2295250a 4247 return 1;
353632d9
NB
4248 }
4249
730ae51f 4250 free(offsets);
e9e43ec3 4251
353632d9
NB
4252 /* Ok, so the data is restored. Let's update those superblocks. */
4253
10af14c4
N
4254 lo = hi = 0;
4255 if (bsb.length) {
4256 lo = __le64_to_cpu(bsb.arraystart);
4257 hi = lo + __le64_to_cpu(bsb.length);
4258 }
4259 if (bsb.magic[15] == '2' && bsb.length2) {
4260 unsigned long long lo1, hi1;
4261 lo1 = __le64_to_cpu(bsb.arraystart2);
4262 hi1 = lo1 + __le64_to_cpu(bsb.length2);
4263 if (lo == hi) {
4264 lo = lo1;
4265 hi = hi1;
4266 } else if (lo < lo1)
4267 hi = hi1;
4268 else
4269 lo = lo1;
4270 }
4271 if (lo < hi &&
4272 (info->reshape_progress < lo ||
4273 info->reshape_progress > hi))
4274 /* backup does not affect reshape_progress*/ ;
4275 else if (info->delta_disks >= 0) {
e9e43ec3
N
4276 info->reshape_progress = __le64_to_cpu(bsb.arraystart) +
4277 __le64_to_cpu(bsb.length);
4278 if (bsb.magic[15] == '2') {
4279 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2) +
4280 __le64_to_cpu(bsb.length2);
4281 if (p2 > info->reshape_progress)
4282 info->reshape_progress = p2;
4283 }
4284 } else {
4285 info->reshape_progress = __le64_to_cpu(bsb.arraystart);
4286 if (bsb.magic[15] == '2') {
4287 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2);
4288 if (p2 < info->reshape_progress)
4289 info->reshape_progress = p2;
4290 }
4291 }
353632d9 4292 for (j=0; j<info->array.raid_disks; j++) {
ca3b6696
N
4293 if (fdlist[j] < 0)
4294 continue;
3da92f27 4295 if (st->ss->load_super(st, fdlist[j], NULL))
353632d9 4296 continue;
a5d85af7 4297 st->ss->getinfo_super(st, &dinfo, NULL);
e9e43ec3 4298 dinfo.reshape_progress = info->reshape_progress;
3da92f27 4299 st->ss->update_super(st, &dinfo,
68c7d6d7
NB
4300 "_reshape_progress",
4301 NULL,0, 0, NULL);
3da92f27
NB
4302 st->ss->store_super(st, fdlist[j]);
4303 st->ss->free_super(st);
353632d9 4304 }
353632d9
NB
4305 return 0;
4306 }
6e9eac4f
NB
4307 /* Didn't find any backup data, try to see if any
4308 * was needed.
4309 */
82f2d6ab
N
4310 if (info->delta_disks < 0) {
4311 /* When shrinking, the critical section is at the end.
4312 * So see if we are before the critical section.
4313 */
4314 unsigned long long first_block;
4315 nstripe = ostripe = 0;
4316 first_block = 0;
4317 while (ostripe >= nstripe) {
4318 ostripe += info->array.chunk_size / 512;
4319 first_block = ostripe * odata;
4320 nstripe = first_block / ndata / (info->new_chunk/512) *
4321 (info->new_chunk/512);
4322 }
4323
4324 if (info->reshape_progress >= first_block)
4325 return 0;
6e9eac4f 4326 }
82f2d6ab
N
4327 if (info->delta_disks > 0) {
4328 /* See if we are beyond the critical section. */
4329 unsigned long long last_block;
4330 nstripe = ostripe = 0;
4331 last_block = 0;
4332 while (nstripe >= ostripe) {
4333 nstripe += info->new_chunk / 512;
4334 last_block = nstripe * ndata;
4335 ostripe = last_block / odata / (info->array.chunk_size/512) *
4336 (info->array.chunk_size/512);
4337 }
6e9eac4f 4338
82f2d6ab
N
4339 if (info->reshape_progress >= last_block)
4340 return 0;
4341 }
6e9eac4f 4342 /* needed to recover critical section! */
ea0ebe96 4343 if (verbose)
e7b84f9d 4344 pr_err("Failed to find backup of critical section\n");
2295250a 4345 return 1;
353632d9 4346}
e9e43ec3 4347
2dddadb0
AK
4348int Grow_continue_command(char *devname, int fd,
4349 char *backup_file, int verbose)
4350{
4351 int ret_val = 0;
4352 struct supertype *st = NULL;
4353 struct mdinfo *content = NULL;
4354 struct mdinfo array;
4355 char *subarray = NULL;
4356 struct mdinfo *cc = NULL;
4357 struct mdstat_ent *mdstat = NULL;
4358 char buf[40];
4359 int cfd = -1;
4360 int fd2 = -1;
4361
4362 dprintf("Grow continue from command line called for %s\n",
4363 devname);
4364
4365 st = super_by_fd(fd, &subarray);
4366 if (!st || !st->ss) {
e7b84f9d
N
4367 pr_err("Unable to determine metadata format for %s\n",
4368 devname);
2dddadb0
AK
4369 return 1;
4370 }
4371 dprintf("Grow continue is run for ");
4372 if (st->ss->external == 0) {
e0ab37a3 4373 int d;
2dddadb0 4374 dprintf("native array (%s)\n", devname);
e0ab37a3 4375 if (ioctl(fd, GET_ARRAY_INFO, &array.array) < 0) {
e7b84f9d 4376 pr_err("%s is not an active md array -"
2dddadb0
AK
4377 " aborting\n", devname);
4378 ret_val = 1;
4379 goto Grow_continue_command_exit;
4380 }
4381 content = &array;
e0ab37a3
N
4382 /* Need to load a superblock.
4383 * FIXME we should really get what we need from
4384 * sysfs
4385 */
4386 for (d = 0; d < MAX_DISKS; d++) {
4387 mdu_disk_info_t disk;
4388 char *dv;
4389 int err;
4390 disk.number = d;
4391 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
4392 continue;
4393 if (disk.major == 0 && disk.minor == 0)
4394 continue;
4395 if ((disk.state & (1 << MD_DISK_ACTIVE)) == 0)
4396 continue;
4397 dv = map_dev(disk.major, disk.minor, 1);
4398 if (!dv)
4399 continue;
4400 fd2 = dev_open(dv, O_RDONLY);
4401 if (fd2 < 0)
4402 continue;
4403 err = st->ss->load_super(st, fd2, NULL);
4404 close(fd2);
4405 if (err)
4406 continue;
4407 break;
4408 }
4409 if (d == MAX_DISKS) {
4410 pr_err("Unable to load metadata for %s\n",
4411 devname);
4412 ret_val = 1;
4413 goto Grow_continue_command_exit;
4414 }
4415 st->ss->getinfo_super(st, content, NULL);
2dddadb0
AK
4416 } else {
4417 int container_dev;
4418
4419 if (subarray) {
4420 dprintf("subarray (%s)\n", subarray);
4421 container_dev = st->container_dev;
4422 cfd = open_dev_excl(st->container_dev);
4423 } else {
4424 container_dev = st->devnum;
4425 close(fd);
4426 cfd = open_dev_excl(st->devnum);
4427 dprintf("container (%i)\n", container_dev);
4428 fd = cfd;
4429 }
4430 if (cfd < 0) {
e7b84f9d 4431 pr_err("Unable to open container "
2dddadb0
AK
4432 "for %s\n", devname);
4433 ret_val = 1;
4434 goto Grow_continue_command_exit;
4435 }
4436 fmt_devname(buf, container_dev);
4437
4438 /* find in container array under reshape
4439 */
4440 ret_val = st->ss->load_container(st, cfd, NULL);
4441 if (ret_val) {
e7b84f9d
N
4442 pr_err("Cannot read superblock for %s\n",
4443 devname);
2dddadb0
AK
4444 ret_val = 1;
4445 goto Grow_continue_command_exit;
4446 }
4447
446894ea 4448 cc = st->ss->container_content(st, subarray);
2dddadb0
AK
4449 for (content = cc; content ; content = content->next) {
4450 char *array;
446894ea 4451 int allow_reshape = 1;
2dddadb0
AK
4452
4453 if (content->reshape_active == 0)
4454 continue;
81219e70
LM
4455 /* The decision about array or container wide
4456 * reshape is taken in Grow_continue based
4457 * content->reshape_active state, therefore we
4458 * need to check_reshape based on
4459 * reshape_active and subarray name
446894ea
N
4460 */
4461 if (content->array.state & (1<<MD_SB_BLOCK_VOLUME))
4462 allow_reshape = 0;
4463 if (content->reshape_active == CONTAINER_RESHAPE &&
4464 (content->array.state
4465 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE)))
4466 allow_reshape = 0;
4467
81219e70 4468 if (!allow_reshape) {
e7b84f9d
N
4469 pr_err("cannot continue reshape of an array"
4470 " in container with unsupported"
4471 " metadata: %s(%s)\n",
4472 devname, buf);
81219e70
LM
4473 ret_val = 1;
4474 goto Grow_continue_command_exit;
4475 }
2dddadb0
AK
4476
4477 array = strchr(content->text_version+1, '/')+1;
4478 mdstat = mdstat_by_subdev(array, container_dev);
4479 if (!mdstat)
4480 continue;
1ca90aa6 4481 if (mdstat->active == 0) {
e7b84f9d 4482 pr_err("Skipping inactive "
1ca90aa6
AK
4483 "array md%i.\n", mdstat->devnum);
4484 free_mdstat(mdstat);
4485 mdstat = NULL;
4486 continue;
4487 }
2dddadb0
AK
4488 break;
4489 }
4490 if (!content) {
e7b84f9d
N
4491 pr_err("Unable to determine reshaped "
4492 "array for %s\n", devname);
2dddadb0
AK
4493 ret_val = 1;
4494 goto Grow_continue_command_exit;
4495 }
4496 fd2 = open_dev(mdstat->devnum);
4497 if (fd2 < 0) {
e7b84f9d 4498 pr_err("cannot open (md%i)\n",
2dddadb0
AK
4499 mdstat->devnum);
4500 ret_val = 1;
4501 goto Grow_continue_command_exit;
4502 }
4503
4504 sysfs_init(content, fd2, mdstat->devnum);
4505
4506 /* start mdmon in case it is not running
4507 */
4508 if (!mdmon_running(container_dev))
4509 start_mdmon(container_dev);
4510 ping_monitor(buf);
4511
4512 if (mdmon_running(container_dev))
4513 st->update_tail = &st->updates;
4514 else {
e7b84f9d 4515 pr_err("No mdmon found. "
2dddadb0
AK
4516 "Grow cannot continue.\n");
4517 ret_val = 1;
4518 goto Grow_continue_command_exit;
4519 }
4520 }
4521
f1fe496b
AK
4522 /* verify that array under reshape is started from
4523 * correct position
4524 */
e0ab37a3 4525 if (verify_reshape_position(content, content->array.level) < 0) {
f1fe496b
AK
4526 ret_val = 1;
4527 goto Grow_continue_command_exit;
4528 }
4529
2dddadb0
AK
4530 /* continue reshape
4531 */
4532 ret_val = Grow_continue(fd, st, content, backup_file, 0);
4533
4534Grow_continue_command_exit:
4535 if (fd2 > -1)
4536 close(fd2);
4537 if (cfd > -1)
4538 close(cfd);
4539 st->ss->free_super(st);
4540 free_mdstat(mdstat);
4541 sysfs_free(cc);
4542 free(subarray);
4543
4544 return ret_val;
4545}
4546
e9e43ec3 4547int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
b76b30e0 4548 char *backup_file, int freeze_reshape)
e9e43ec3 4549{
3bd58dc6
AK
4550 int ret_val = 2;
4551
4552 if (!info->reshape_active)
4553 return ret_val;
864a004f 4554
20a40eca 4555 if (st->ss->external) {
3bd58dc6
AK
4556 char container[40];
4557 int cfd = open_dev(st->container_dev);
3db2fdd8 4558
3bd58dc6
AK
4559 if (cfd < 0)
4560 return 1;
f362d22b 4561
3bd58dc6
AK
4562 fmt_devname(container, st->container_dev);
4563 st->ss->load_container(st, cfd, container);
4564 close(cfd);
9ad6f6e6 4565 ret_val = reshape_container(container, NULL, mdfd,
3bd58dc6
AK
4566 st, info, 0, backup_file,
4567 0, 1, freeze_reshape);
4568 } else
4569 ret_val = reshape_array(NULL, mdfd, "array", st, info, 1,
19ceb16d 4570 NULL, 0ULL, backup_file, 0, 0, 1,
3bd58dc6 4571 freeze_reshape);
f362d22b 4572
3bd58dc6 4573 return ret_val;
e9e43ec3 4574}