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