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