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