]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Grow.c
Merge https://github.com/makelinux/mdadm
[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 }
7bc71196
DW
1593
1594 /* in the external case we need to check that the requested reshape is
1595 * supported, and perform an initial check that the container holds the
1596 * pre-requisite spare devices (mdmon owns final validation)
1597 */
1598 if (st->ss->external) {
7f2ba464 1599 int rv;
7bc71196
DW
1600
1601 if (subarray) {
4dd2df09
N
1602 container = st->container_devnm;
1603 cfd = open_dev_excl(st->container_devnm);
7bc71196 1604 } else {
4dd2df09 1605 container = st->devnm;
7bc71196 1606 close(fd);
4dd2df09 1607 cfd = open_dev_excl(st->devnm);
7bc71196
DW
1608 fd = cfd;
1609 }
1610 if (cfd < 0) {
e7b84f9d 1611 pr_err("Unable to open container for %s\n",
7bc71196 1612 devname);
7f2ba464 1613 free(subarray);
7bc71196
DW
1614 return 1;
1615 }
1616
eb744788
AK
1617 rv = st->ss->load_container(st, cfd, NULL);
1618
7f2ba464 1619 if (rv) {
e7b84f9d 1620 pr_err("Cannot read superblock for %s\n",
7bc71196 1621 devname);
7f2ba464 1622 free(subarray);
7bc71196
DW
1623 return 1;
1624 }
1625
81219e70
LM
1626 /* check if operation is supported for metadata handler */
1627 if (st->ss->container_content) {
1628 struct mdinfo *cc = NULL;
1629 struct mdinfo *content = NULL;
1630
1631 cc = st->ss->container_content(st, subarray);
1632 for (content = cc; content ; content = content->next) {
446894ea 1633 int allow_reshape = 1;
81219e70
LM
1634
1635 /* check if reshape is allowed based on metadata
1636 * indications stored in content.array.status
1637 */
446894ea
N
1638 if (content->array.state & (1<<MD_SB_BLOCK_VOLUME))
1639 allow_reshape = 0;
1640 if (content->array.state
1641 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE))
1642 allow_reshape = 0;
81219e70 1643 if (!allow_reshape) {
7a862a02 1644 pr_err("cannot reshape arrays in container with unsupported metadata: %s(%s)\n",
4dd2df09 1645 devname, container);
81219e70
LM
1646 sysfs_free(cc);
1647 free(subarray);
1648 return 1;
1649 }
1650 }
1651 sysfs_free(cc);
1652 }
4dd2df09 1653 if (mdmon_running(container))
7bc71196 1654 st->update_tail = &st->updates;
e2e53a2d 1655 }
691a36b7 1656
e2e53a2d
N
1657 added_disks = 0;
1658 for (dv = devlist; dv; dv = dv->next)
1659 added_disks++;
32754b7d
N
1660 if (s->raiddisks > array.raid_disks &&
1661 array.spare_disks +added_disks < (s->raiddisks - array.raid_disks) &&
1662 !c->force) {
7a862a02 1663 pr_err("Need %d spare%s to avoid degraded array, and only have %d.\n"
e7b84f9d 1664 " Use --force to over-ride this check.\n",
32754b7d
N
1665 s->raiddisks - array.raid_disks,
1666 s->raiddisks - array.raid_disks == 1 ? "" : "s",
e7b84f9d 1667 array.spare_disks + added_disks);
691a36b7 1668 return 1;
7bc71196
DW
1669 }
1670
4dd2df09 1671 sra = sysfs_read(fd, NULL, GET_LEVEL | GET_DISKS | GET_DEVS
3fa436ac 1672 | GET_STATE | GET_VERSION);
24daa16f 1673 if (sra) {
7f2ba464 1674 if (st->ss->external && subarray == NULL) {
7bc71196
DW
1675 array.level = LEVEL_CONTAINER;
1676 sra->array.level = LEVEL_CONTAINER;
1677 }
7bc71196 1678 } else {
e7b84f9d 1679 pr_err("failed to read sysfs parameters for %s\n",
b526e52d
DW
1680 devname);
1681 return 1;
1682 }
7f2ba464
DW
1683 frozen = freeze(st);
1684 if (frozen < -1) {
1685 /* freeze() already spewed the reason */
8e61e0d7 1686 sysfs_free(sra);
7f2ba464
DW
1687 return 1;
1688 } else if (frozen < 0) {
7a862a02 1689 pr_err("%s is performing resync/recovery and cannot be reshaped\n", devname);
8e61e0d7 1690 sysfs_free(sra);
7236ee7a
N
1691 return 1;
1692 }
1693
1694 /* ========= set size =============== */
887a7a9e 1695 if (s->size > 0 && (s->size == MAX_SIZE || s->size != (unsigned)array.size)) {
d04f65f4
N
1696 unsigned long long orig_size = get_component_size(fd)/2;
1697 unsigned long long min_csize;
d1537ed1 1698 struct mdinfo *mdi;
44f6f181 1699 int raid0_takeover = 0;
7bc71196 1700
85f10287 1701 if (orig_size == 0)
d04f65f4 1702 orig_size = (unsigned) array.size;
85f10287 1703
3920235e
N
1704 if (orig_size == 0) {
1705 pr_err("Cannot set device size in this type of array.\n");
1706 rv = 1;
1707 goto release;
1708 }
1709
32754b7d
N
1710 if (reshape_super(st, s->size, UnSet, UnSet, 0, 0, UnSet, NULL,
1711 devname, APPLY_METADATA_CHANGES, c->verbose > 0)) {
7bc71196
DW
1712 rv = 1;
1713 goto release;
1714 }
1715 sync_metadata(st);
7e7e9a4d
AK
1716 if (st->ss->external) {
1717 /* metadata can have size limitation
1718 * update size value according to metadata information
1719 */
1720 struct mdinfo *sizeinfo =
1721 st->ss->container_content(st, subarray);
1722 if (sizeinfo) {
1723 unsigned long long new_size =
1724 sizeinfo->custom_array_size/2;
1725 int data_disks = get_data_disks(
1726 sizeinfo->array.level,
1727 sizeinfo->array.layout,
1728 sizeinfo->array.raid_disks);
1729 new_size /= data_disks;
7a862a02 1730 dprintf("Metadata size correction from %llu to %llu (%llu)\n", orig_size, new_size,
7e7e9a4d 1731 new_size * data_disks);
32754b7d 1732 s->size = new_size;
7e7e9a4d
AK
1733 sysfs_free(sizeinfo);
1734 }
1735 }
d1537ed1
N
1736
1737 /* Update the size of each member device in case
1738 * they have been resized. This will never reduce
1739 * below the current used-size. The "size" attribute
20a46756 1740 * understands '0' to mean 'max'.
d1537ed1 1741 */
20a46756 1742 min_csize = 0;
65a9798b 1743 rv = 0;
20a46756 1744 for (mdi = sra->devs; mdi; mdi = mdi->next) {
d04f65f4 1745 if (sysfs_set_num(sra, mdi, "size",
32754b7d 1746 s->size == MAX_SIZE ? 0 : s->size) < 0) {
b0a658ff
N
1747 /* Probably kernel refusing to let us
1748 * reduce the size - not an error.
1749 */
20a46756 1750 break;
65a9798b 1751 }
20a46756
N
1752 if (array.not_persistent == 0 &&
1753 array.major_version == 0 &&
1754 get_linux_version() < 3001000) {
1755 /* Dangerous to allow size to exceed 2TB */
1756 unsigned long long csize;
1757 if (sysfs_get_ll(sra, mdi, "size", &csize) == 0) {
1758 if (csize >= 2ULL*1024*1024*1024)
1759 csize = 2ULL*1024*1024*1024;
1760 if ((min_csize == 0 || (min_csize
d04f65f4 1761 > csize)))
20a46756
N
1762 min_csize = csize;
1763 }
1764 }
1765 }
65a9798b 1766 if (rv) {
7a862a02 1767 pr_err("Cannot set size on array members.\n");
65a9798b
AK
1768 goto size_change_error;
1769 }
32754b7d 1770 if (min_csize && s->size > min_csize) {
7a862a02 1771 pr_err("Cannot safely make this array use more than 2TB per device on this kernel.\n");
20a46756 1772 rv = 1;
65a9798b 1773 goto size_change_error;
20a46756 1774 }
32754b7d 1775 if (min_csize && s->size == MAX_SIZE) {
20a46756
N
1776 /* Don't let the kernel choose a size - it will get
1777 * it wrong
1778 */
7a862a02 1779 pr_err("Limited v0.90 array to 2TB per device\n");
32754b7d 1780 s->size = min_csize;
20a46756 1781 }
44f6f181
AK
1782 if (st->ss->external) {
1783 if (sra->array.level == 0) {
1784 rv = sysfs_set_str(sra, NULL, "level",
1785 "raid5");
1786 if (!rv) {
1787 raid0_takeover = 1;
2a6493cf
N
1788 /* get array parameters after takeover
1789 * to change one parameter at time only
44f6f181
AK
1790 */
1791 rv = ioctl(fd, GET_ARRAY_INFO, &array);
1792 }
1793 }
1794 /* make sure mdmon is
1795 * aware of the new level */
4dd2df09
N
1796 if (!mdmon_running(st->container_devnm))
1797 start_mdmon(st->container_devnm);
44f6f181 1798 ping_monitor(container);
4dd2df09 1799 if (mdmon_running(st->container_devnm) &&
44f6f181
AK
1800 st->update_tail == NULL)
1801 st->update_tail = &st->updates;
1802 }
1803
0a8b92a6
N
1804 if (s->size == MAX_SIZE)
1805 s->size = 0;
1806 array.size = s->size;
0448027b 1807 if (s->size & ~INT32_MAX) {
7236ee7a
N
1808 /* got truncated to 32bit, write to
1809 * component_size instead
1810 */
1811 if (sra)
1812 rv = sysfs_set_num(sra, NULL,
32754b7d 1813 "component_size", s->size);
7236ee7a
N
1814 else
1815 rv = -1;
54397ed9 1816 } else {
7236ee7a 1817 rv = ioctl(fd, SET_ARRAY_INFO, &array);
44f6f181 1818
54397ed9
AK
1819 /* manage array size when it is managed externally
1820 */
1821 if ((rv == 0) && st->ss->external)
1822 rv = set_array_size(st, sra, sra->text_version);
1823 }
1824
44f6f181
AK
1825 if (raid0_takeover) {
1826 /* do not recync non-existing parity,
1827 * we will drop it anyway
1828 */
b51702b8 1829 sysfs_set_str(sra, NULL, "sync_action", "frozen");
44f6f181
AK
1830 /* go back to raid0, drop parity disk
1831 */
1832 sysfs_set_str(sra, NULL, "level", "raid0");
1833 ioctl(fd, GET_ARRAY_INFO, &array);
1834 }
1835
65a9798b 1836size_change_error:
7236ee7a 1837 if (rv != 0) {
39bbb392 1838 int err = errno;
7bc71196
DW
1839
1840 /* restore metadata */
1841 if (reshape_super(st, orig_size, UnSet, UnSet, 0, 0,
016e00f5
AK
1842 UnSet, NULL, devname,
1843 ROLLBACK_METADATA_CHANGES,
32754b7d 1844 c->verbose) == 0)
7bc71196 1845 sync_metadata(st);
e7b84f9d 1846 pr_err("Cannot set device size for %s: %s\n",
39bbb392 1847 devname, strerror(err));
24daa16f 1848 if (err == EBUSY &&
39bbb392 1849 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
e7b84f9d 1850 cont_err("Bitmap must be removed before size can be changed\n");
7236ee7a
N
1851 rv = 1;
1852 goto release;
1853 }
32754b7d 1854 if (s->assume_clean) {
86952387 1855 /* This will fail on kernels older than 3.0 unless
ce52f92f
N
1856 * a backport has been arranged.
1857 */
1858 if (sra == NULL ||
1859 sysfs_set_str(sra, NULL, "resync_start", "none") < 0)
86952387 1860 pr_err("--assume-clean not supported with --grow on this kernel\n");
ce52f92f 1861 }
7236ee7a 1862 ioctl(fd, GET_ARRAY_INFO, &array);
32754b7d
N
1863 s->size = get_component_size(fd)/2;
1864 if (s->size == 0)
1865 s->size = array.size;
1866 if (c->verbose >= 0) {
1867 if (s->size == orig_size)
7a862a02 1868 pr_err("component size of %s unchanged at %lluK\n",
32754b7d 1869 devname, s->size);
85f10287 1870 else
7a862a02 1871 pr_err("component size of %s has been set to %lluK\n",
32754b7d 1872 devname, s->size);
85f10287 1873 }
7236ee7a 1874 changed = 1;
7bc71196 1875 } else if (array.level != LEVEL_CONTAINER) {
32754b7d
N
1876 s->size = get_component_size(fd)/2;
1877 if (s->size == 0)
1878 s->size = array.size;
7236ee7a
N
1879 }
1880
d515d27f 1881 /* See if there is anything else to do */
32754b7d
N
1882 if ((s->level == UnSet || s->level == array.level) &&
1883 (s->layout_str == NULL) &&
1884 (s->chunk == 0 || s->chunk == array.chunk_size) &&
8876bf0b 1885 data_offset == INVALID_SECTORS &&
32754b7d 1886 (s->raiddisks == 0 || s->raiddisks == array.raid_disks)) {
d515d27f 1887 /* Nothing more to do */
32754b7d 1888 if (!changed && c->verbose >= 0)
e7b84f9d 1889 pr_err("%s: no change requested\n",
d515d27f
N
1890 devname);
1891 goto release;
1892 }
1893
dfe77a9e 1894 /* ========= check for Raid10/Raid1 -> Raid0 conversion ===============
5da9ab98 1895 * current implementation assumes that following conditions must be met:
dfe77a9e 1896 * - RAID10:
24daa16f
N
1897 * - far_copies == 1
1898 * - near_copies == 2
62a48395 1899 */
32754b7d 1900 if ((s->level == 0 && array.level == 10 && sra &&
24daa16f 1901 array.layout == ((1 << 8) + 2) && !(array.raid_disks & 1)) ||
32754b7d 1902 (s->level == 0 && array.level == 1 && sra)) {
62a48395 1903 int err;
dfe77a9e 1904 err = remove_disks_for_takeover(st, sra, array.layout);
62a48395 1905 if (err) {
1ade5cc1 1906 dprintf("Array cannot be reshaped\n");
62a48395
AK
1907 if (cfd > -1)
1908 close(cfd);
5da9ab98
N
1909 rv = 1;
1910 goto release;
7236ee7a 1911 }
fde139b9
N
1912 /* Make sure mdmon has seen the device removal
1913 * and updated metadata before we continue with
1914 * level change
1915 */
1916 if (container)
1917 ping_monitor(container);
7236ee7a
N
1918 }
1919
8ff6d094 1920 memset(&info, 0, sizeof(info));
5da9ab98 1921 info.array = array;
4dd2df09 1922 sysfs_init(&info, fd, NULL);
3fa436ac 1923 strcpy(info.text_version, sra->text_version);
32754b7d
N
1924 info.component_size = s->size*2;
1925 info.new_level = s->level;
1926 info.new_chunk = s->chunk * 1024;
eff4954d
N
1927 if (info.array.level == LEVEL_CONTAINER) {
1928 info.delta_disks = UnSet;
32754b7d
N
1929 info.array.raid_disks = s->raiddisks;
1930 } else if (s->raiddisks)
1931 info.delta_disks = s->raiddisks - info.array.raid_disks;
5da9ab98
N
1932 else
1933 info.delta_disks = UnSet;
32754b7d 1934 if (s->layout_str == NULL) {
5da9ab98
N
1935 info.new_layout = UnSet;
1936 if (info.array.level == 6 &&
1937 (info.new_level == 6 || info.new_level == UnSet) &&
1938 info.array.layout >= 16) {
7a862a02
N
1939 pr_err("%s has a non-standard layout. If you wish to preserve this\n", devname);
1940 cont_err("during the reshape, please specify --layout=preserve\n");
1941 cont_err("If you want to change it, specify a layout or use --layout=normalise\n");
7236ee7a
N
1942 rv = 1;
1943 goto release;
1944 }
32754b7d
N
1945 } else if (strcmp(s->layout_str, "normalise") == 0 ||
1946 strcmp(s->layout_str, "normalize") == 0) {
5da9ab98
N
1947 /* If we have a -6 RAID6 layout, remove the '-6'. */
1948 info.new_layout = UnSet;
1949 if (info.array.level == 6 && info.new_level == UnSet) {
1950 char l[40], *h;
1951 strcpy(l, map_num(r6layout, info.array.layout));
1952 h = strrchr(l, '-');
1953 if (h && strcmp(h, "-6") == 0) {
1954 *h = 0;
1955 info.new_layout = map_name(r6layout, l);
7236ee7a 1956 }
385167f3 1957 } else {
7a862a02 1958 pr_err("%s is only meaningful when reshaping a RAID6 array.\n", s->layout_str);
385167f3
N
1959 rv = 1;
1960 goto release;
7236ee7a 1961 }
32754b7d 1962 } else if (strcmp(s->layout_str, "preserve") == 0) {
385167f3
N
1963 /* This means that a non-standard RAID6 layout
1964 * is OK.
1965 * In particular:
1966 * - When reshape a RAID6 (e.g. adding a device)
1967 * which is in a non-standard layout, it is OK
1968 * to preserve that layout.
1969 * - When converting a RAID5 to RAID6, leave it in
1970 * the XXX-6 layout, don't re-layout.
1971 */
1972 if (info.array.level == 6 && info.new_level == UnSet)
1973 info.new_layout = info.array.layout;
1974 else if (info.array.level == 5 && info.new_level == 6) {
1975 char l[40];
1976 strcpy(l, map_num(r5layout, info.array.layout));
1977 strcat(l, "-6");
1978 info.new_layout = map_name(r6layout, l);
1979 } else {
7a862a02 1980 pr_err("%s in only meaningful when reshaping to RAID6\n", s->layout_str);
385167f3
N
1981 rv = 1;
1982 goto release;
1983 }
5da9ab98
N
1984 } else {
1985 int l = info.new_level;
1986 if (l == UnSet)
1987 l = info.array.level;
1988 switch (l) {
1989 case 5:
32754b7d 1990 info.new_layout = map_name(r5layout, s->layout_str);
5da9ab98
N
1991 break;
1992 case 6:
32754b7d 1993 info.new_layout = map_name(r6layout, s->layout_str);
5da9ab98
N
1994 break;
1995 case 10:
32754b7d 1996 info.new_layout = parse_layout_10(s->layout_str);
5da9ab98
N
1997 break;
1998 case LEVEL_FAULTY:
32754b7d 1999 info.new_layout = parse_layout_faulty(s->layout_str);
5da9ab98
N
2000 break;
2001 default:
7a862a02 2002 pr_err("layout not meaningful with this level\n");
7bc71196
DW
2003 rv = 1;
2004 goto release;
2005 }
5da9ab98 2006 if (info.new_layout == UnSet) {
7a862a02 2007 pr_err("layout %s not understood for this level\n",
32754b7d 2008 s->layout_str);
5da9ab98
N
2009 rv = 1;
2010 goto release;
7bc71196 2011 }
7236ee7a
N
2012 }
2013
907ea753 2014 if (array.level == LEVEL_FAULTY) {
32754b7d 2015 if (s->level != UnSet && s->level != array.level) {
e7b84f9d 2016 pr_err("cannot change level of Faulty device\n");
907ea753
N
2017 rv =1 ;
2018 }
32754b7d 2019 if (s->chunk) {
e7b84f9d 2020 pr_err("cannot set chunksize of Faulty device\n");
907ea753
N
2021 rv =1 ;
2022 }
32754b7d 2023 if (s->raiddisks && s->raiddisks != 1) {
e7b84f9d 2024 pr_err("cannot set raid_disks of Faulty device\n");
907ea753
N
2025 rv =1 ;
2026 }
32754b7d 2027 if (s->layout_str) {
907ea753
N
2028 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
2029 dprintf("Cannot get array information.\n");
2030 goto release;
2031 }
2032 array.layout = info.new_layout;
2033 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
e7b84f9d 2034 pr_err("failed to set new layout\n");
907ea753 2035 rv = 1;
32754b7d 2036 } else if (c->verbose >= 0)
907ea753
N
2037 printf("layout for %s set to %d\n",
2038 devname, array.layout);
2039 }
2040 } else if (array.level == LEVEL_CONTAINER) {
5da9ab98
N
2041 /* This change is to be applied to every array in the
2042 * container. This is only needed when the metadata imposes
2043 * restraints of the various arrays in the container.
2044 * Currently we only know that IMSM requires all arrays
2045 * to have the same number of devices so changing the
2046 * number of devices (On-Line Capacity Expansion) must be
2047 * performed at the level of the container
2048 */
9ad6f6e6 2049 rv = reshape_container(container, devname, -1, st, &info,
b0140ae8 2050 c->force, c->backup_file, c->verbose, 0, 0, 0);
9202b8eb 2051 frozen = 0;
5da9ab98 2052 } else {
08f9e34b
AK
2053 /* get spare devices from external metadata
2054 */
2055 if (st->ss->external) {
2056 struct mdinfo *info2;
2057
2058 info2 = st->ss->container_content(st, subarray);
2059 if (info2) {
2060 info.array.spare_disks =
2061 info2->array.spare_disks;
2062 sysfs_free(info2);
2063 }
2064 }
2065
5da9ab98
N
2066 /* Impose these changes on a single array. First
2067 * check that the metadata is OK with the change. */
7bc71196 2068
7ec0996c 2069 if (reshape_super(st, 0, info.new_level,
5da9ab98 2070 info.new_layout, info.new_chunk,
41784c88 2071 info.array.raid_disks, info.delta_disks,
32754b7d
N
2072 c->backup_file, devname, APPLY_METADATA_CHANGES,
2073 c->verbose)) {
7bc71196
DW
2074 rv = 1;
2075 goto release;
2076 }
5da9ab98 2077 sync_metadata(st);
32754b7d 2078 rv = reshape_array(container, fd, devname, st, &info, c->force,
19ceb16d
N
2079 devlist, data_offset, c->backup_file, c->verbose,
2080 0, 0, 0);
9202b8eb 2081 frozen = 0;
5da9ab98
N
2082 }
2083release:
8e61e0d7 2084 sysfs_free(sra);
9202b8eb
N
2085 if (frozen > 0)
2086 unfreeze(st);
5da9ab98
N
2087 return rv;
2088}
7bc71196 2089
f93346ef
AK
2090/* verify_reshape_position()
2091 * Function checks if reshape position in metadata is not farther
2092 * than position in md.
2093 * Return value:
2094 * 0 : not valid sysfs entry
2095 * it can be caused by not started reshape, it should be started
2096 * by reshape array or raid0 array is before takeover
2097 * -1 : error, reshape position is obviously wrong
2098 * 1 : success, reshape progress correct or updated
2099*/
2100static int verify_reshape_position(struct mdinfo *info, int level)
2101{
2102 int ret_val = 0;
2103 char buf[40];
178950ea 2104 int rv;
f93346ef
AK
2105
2106 /* read sync_max, failure can mean raid0 array */
178950ea
AK
2107 rv = sysfs_get_str(info, NULL, "sync_max", buf, 40);
2108
2109 if (rv > 0) {
f93346ef
AK
2110 char *ep;
2111 unsigned long long position = strtoull(buf, &ep, 0);
2112
1ade5cc1 2113 dprintf("Read sync_max sysfs entry is: %s\n", buf);
f93346ef
AK
2114 if (!(ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))) {
2115 position *= get_data_disks(level,
2116 info->new_layout,
2117 info->array.raid_disks);
2118 if (info->reshape_progress < position) {
7a862a02 2119 dprintf("Corrected reshape progress (%llu) to md position (%llu)\n",
f93346ef
AK
2120 info->reshape_progress, position);
2121 info->reshape_progress = position;
2122 ret_val = 1;
2123 } else if (info->reshape_progress > position) {
7a862a02 2124 pr_err("Fatal error: array reshape was not properly frozen (expected reshape position is %llu, but reshape progress is %llu.\n",
e7b84f9d 2125 position, info->reshape_progress);
f93346ef
AK
2126 ret_val = -1;
2127 } else {
7a862a02 2128 dprintf("Reshape position in md and metadata are the same;");
f93346ef
AK
2129 ret_val = 1;
2130 }
2131 }
178950ea
AK
2132 } else if (rv == 0) {
2133 /* for valid sysfs entry, 0-length content
2134 * should be indicated as error
2135 */
2136 ret_val = -1;
f93346ef
AK
2137 }
2138
2139 return ret_val;
2140}
2141
534f5432
N
2142static unsigned long long choose_offset(unsigned long long lo,
2143 unsigned long long hi,
2144 unsigned long long min,
2145 unsigned long long max)
2146{
2147 /* Choose a new offset between hi and lo.
2148 * It must be between min and max, but
2149 * we would prefer something near the middle of hi/lo, and also
2150 * prefer to be aligned to a big power of 2.
2151 *
2152 * So we start with the middle, then for each bit,
2153 * starting at '1' and increasing, if it is set, we either
2154 * add it or subtract it if possible, preferring the option
2155 * which is furthest from the boundary.
2156 *
2157 * We stop once we get a 1MB alignment. As units are in sectors,
2158 * 1MB = 2*1024 sectors.
2159 */
2160 unsigned long long choice = (lo + hi) / 2;
2161 unsigned long long bit = 1;
2162
2163 for (bit = 1; bit < 2*1024; bit = bit << 1) {
2164 unsigned long long bigger, smaller;
2165 if (! (bit & choice))
2166 continue;
2167 bigger = choice + bit;
2168 smaller = choice - bit;
2169 if (bigger > max && smaller < min)
2170 break;
2171 if (bigger > max)
2172 choice = smaller;
2173 else if (smaller < min)
2174 choice = bigger;
2175 else if (hi - bigger > smaller - lo)
2176 choice = bigger;
2177 else
2178 choice = smaller;
2179 }
2180 return choice;
2181}
2182
13bbb145 2183static int set_new_data_offset(struct mdinfo *sra, struct supertype *st,
50a4962f 2184 char *devname, int delta_disks,
13bbb145 2185 unsigned long long data_offset,
6a23fb9d
N
2186 unsigned long long min,
2187 int can_fallback)
19ceb16d 2188{
13bbb145 2189 struct mdinfo *sd;
19ceb16d
N
2190 int dir = 0;
2191 int err = 0;
f9b08fec 2192 unsigned long long before, after;
19ceb16d 2193
f9b08fec
N
2194 /* Need to find min space before and after so same is used
2195 * on all devices
2196 */
2197 before = UINT64_MAX;
2198 after = UINT64_MAX;
19ceb16d
N
2199 for (sd = sra->devs; sd; sd = sd->next) {
2200 char *dn;
2201 int dfd;
2202 int rv;
2203 struct supertype *st2;
2204 struct mdinfo info2;
2205
2206 if (sd->disk.state & (1<<MD_DISK_FAULTY))
2207 continue;
2208 dn = map_dev(sd->disk.major, sd->disk.minor, 0);
2209 dfd = dev_open(dn, O_RDONLY);
2210 if (dfd < 0) {
ed503f89 2211 pr_err("%s: cannot open component %s\n",
19ceb16d 2212 devname, dn ? dn : "-unknown-");
19ceb16d
N
2213 goto release;
2214 }
2215 st2 = dup_super(st);
2216 rv = st2->ss->load_super(st2,dfd, NULL);
2217 close(dfd);
2218 if (rv) {
2219 free(st2);
ed503f89 2220 pr_err("%s: cannot get superblock from %s\n",
19ceb16d
N
2221 devname, dn);
2222 goto release;
2223 }
2224 st2->ss->getinfo_super(st2, &info2, NULL);
2225 st2->ss->free_super(st2);
2226 free(st2);
c4b26c64
N
2227 if (info2.space_before == 0 &&
2228 info2.space_after == 0) {
2229 /* Metadata doesn't support data_offset changes */
dd243f56
N
2230 if (!can_fallback)
2231 pr_err("%s: Metadata version doesn't support data_offset changes\n",
2232 devname);
16afb1a5 2233 goto fallback;
c4b26c64 2234 }
f9b08fec
N
2235 if (before > info2.space_before)
2236 before = info2.space_before;
2237 if (after > info2.space_after)
2238 after = info2.space_after;
2239
2240 if (data_offset != INVALID_SECTORS) {
2241 if (dir == 0) {
2242 if (info2.data_offset == data_offset) {
2243 pr_err("%s: already has that data_offset\n",
2244 dn);
2245 goto release;
2246 }
2247 if (data_offset < info2.data_offset)
2248 dir = -1;
2249 else
2250 dir = 1;
2251 } else if ((data_offset <= info2.data_offset && dir == 1) ||
2252 (data_offset >= info2.data_offset && dir == -1)) {
2253 pr_err("%s: differing data offsets on devices make this --data-offset setting impossible\n",
2254 dn);
2255 goto release;
2256 }
2257 }
2258 }
2259 if (before == UINT64_MAX)
2260 /* impossible really, there must be no devices */
2261 return 1;
2262
2263 for (sd = sra->devs; sd; sd = sd->next) {
2264 char *dn = map_dev(sd->disk.major, sd->disk.minor, 0);
c0f0d812 2265 unsigned long long new_data_offset;
f9b08fec 2266
b397d7f3
N
2267 if (sd->disk.state & (1<<MD_DISK_FAULTY))
2268 continue;
50a4962f 2269 if (delta_disks < 0) {
19ceb16d
N
2270 /* Don't need any space as array is shrinking
2271 * just move data_offset up by min
2272 */
5582b118 2273 if (data_offset == INVALID_SECTORS)
c0f0d812 2274 new_data_offset = sd->data_offset + min;
19ceb16d 2275 else {
c0f0d812 2276 if (data_offset < sd->data_offset + min) {
ed503f89 2277 pr_err("--data-offset too small for %s\n",
19ceb16d
N
2278 dn);
2279 goto release;
2280 }
c0f0d812 2281 new_data_offset = data_offset;
19ceb16d 2282 }
50a4962f 2283 } else if (delta_disks > 0) {
19ceb16d 2284 /* need space before */
f9b08fec 2285 if (before < min) {
6a23fb9d
N
2286 if (can_fallback)
2287 goto fallback;
ed503f89 2288 pr_err("Insufficient head-space for reshape on %s\n",
19ceb16d
N
2289 dn);
2290 goto release;
2291 }
5582b118 2292 if (data_offset == INVALID_SECTORS)
c0f0d812 2293 new_data_offset = sd->data_offset - min;
19ceb16d 2294 else {
c0f0d812 2295 if (data_offset > sd->data_offset - min) {
ed503f89 2296 pr_err("--data-offset too large for %s\n",
19ceb16d
N
2297 dn);
2298 goto release;
2299 }
c0f0d812 2300 new_data_offset = data_offset;
19ceb16d
N
2301 }
2302 } else {
2303 if (dir == 0) {
f9b08fec
N
2304 /* can move up or down. If 'data_offset'
2305 * was set we would have already decided,
2306 * so just choose direction with most space.
19ceb16d 2307 */
c0f0d812 2308 if (before > after)
19ceb16d
N
2309 dir = -1;
2310 else
2311 dir = 1;
19ceb16d 2312 }
b397d7f3
N
2313 sysfs_set_str(sra, NULL, "reshape_direction",
2314 dir == 1 ? "backwards" : "forwards");
c0f0d812
N
2315 if (dir > 0) {
2316 /* Increase data offset */
f9b08fec 2317 if (after < min) {
6a23fb9d
N
2318 if (can_fallback)
2319 goto fallback;
ed503f89 2320 pr_err("Insufficient tail-space for reshape on %s\n",
19ceb16d
N
2321 dn);
2322 goto release;
2323 }
5582b118 2324 if (data_offset != INVALID_SECTORS &&
c0f0d812 2325 data_offset < sd->data_offset + min) {
ed503f89 2326 pr_err("--data-offset too small on %s\n",
19ceb16d
N
2327 dn);
2328 goto release;
2329 }
5582b118 2330 if (data_offset != INVALID_SECTORS)
c0f0d812 2331 new_data_offset = data_offset;
534f5432
N
2332 else
2333 new_data_offset = choose_offset(sd->data_offset,
2334 sd->data_offset + after,
2335 sd->data_offset + min,
2336 sd->data_offset + after);
c0f0d812
N
2337 } else {
2338 /* Decrease data offset */
f9b08fec 2339 if (before < min) {
6a23fb9d
N
2340 if (can_fallback)
2341 goto fallback;
ed503f89 2342 pr_err("insufficient head-room on %s\n",
19ceb16d
N
2343 dn);
2344 goto release;
2345 }
5582b118 2346 if (data_offset != INVALID_SECTORS &&
c0f0d812 2347 data_offset < sd->data_offset - min) {
ed503f89 2348 pr_err("--data-offset too small on %s\n",
19ceb16d
N
2349 dn);
2350 goto release;
2351 }
5582b118 2352 if (data_offset != INVALID_SECTORS)
c0f0d812 2353 new_data_offset = data_offset;
534f5432
N
2354 else
2355 new_data_offset = choose_offset(sd->data_offset - before,
2356 sd->data_offset,
2357 sd->data_offset - before,
2358 sd->data_offset - min);
19ceb16d
N
2359 }
2360 }
a6a78630
N
2361 err = sysfs_set_num(sra, sd, "new_offset", new_data_offset);
2362 if (err < 0 && errno == E2BIG) {
2363 /* try again after increasing data size to max */
2364 err = sysfs_set_num(sra, sd, "size", 0);
2365 if (err < 0 && errno == EINVAL &&
2366 !(sd->disk.state & (1<<MD_DISK_SYNC))) {
2367 /* some kernels have a bug where you cannot
2368 * use '0' on spare devices. */
2369 sysfs_set_num(sra, sd, "size",
2370 (sra->component_size + after)/2);
2371 }
2372 err = sysfs_set_num(sra, sd, "new_offset",
2373 new_data_offset);
2374 }
2375 if (err < 0) {
d7e1f52b
N
2376 if (errno == E2BIG && data_offset != INVALID_SECTORS) {
2377 pr_err("data-offset is too big for %s\n",
2378 dn);
2379 goto release;
2380 }
9ad2a640
N
2381 if (sd == sra->devs &&
2382 (errno == ENOENT || errno == E2BIG))
2383 /* Early kernel, no 'new_offset' file,
2384 * or kernel doesn't like us.
93f174b9
N
2385 * For RAID5/6 this is not fatal
2386 */
2387 return 1;
ed503f89 2388 pr_err("Cannot set new_offset for %s\n",
19ceb16d
N
2389 dn);
2390 break;
2391 }
2392 }
13bbb145
N
2393 return err;
2394release:
2395 return -1;
6a23fb9d
N
2396fallback:
2397 /* Just use a backup file */
2398 return 1;
13bbb145
N
2399}
2400
2401static int raid10_reshape(char *container, int fd, char *devname,
2402 struct supertype *st, struct mdinfo *info,
2403 struct reshape *reshape,
2404 unsigned long long data_offset,
2405 int force, int verbose)
2406{
2407 /* Changing raid_disks, layout, chunksize or possibly
2408 * just data_offset for a RAID10.
2409 * We must always change data_offset. We change by at least
89ecd3cf 2410 * ->min_offset_change which is the largest of the old and new
13bbb145
N
2411 * chunk sizes.
2412 * If raid_disks is increasing, then data_offset must decrease
2413 * by at least this copy size.
2414 * If raid_disks is unchanged, data_offset must increase or
89ecd3cf 2415 * decrease by at least min_offset_change but preferably by much more.
13bbb145
N
2416 * We choose half of the available space.
2417 * If raid_disks is decreasing, data_offset must increase by
89ecd3cf 2418 * at least min_offset_change. To allow of this, component_size
13bbb145
N
2419 * must be decreased by the same amount.
2420 *
2421 * So we calculate the required minimum and direction, possibly
2422 * reduce the component_size, then iterate through the devices
2423 * and set the new_data_offset.
2424 * If that all works, we set chunk_size, layout, raid_disks, and start
2425 * 'reshape'
2426 */
2427 struct mdinfo *sra;
2428 unsigned long long min;
2429 int err = 0;
2430
2431 sra = sysfs_read(fd, NULL,
2432 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK
2433 );
2434 if (!sra) {
ed503f89 2435 pr_err("%s: Cannot get array details from sysfs\n",
13bbb145
N
2436 devname);
2437 goto release;
2438 }
89ecd3cf 2439 min = reshape->min_offset_change;
13bbb145
N
2440
2441 if (info->delta_disks)
2442 sysfs_set_str(sra, NULL, "reshape_direction",
2443 info->delta_disks < 0 ? "backwards" : "forwards");
2444 if (info->delta_disks < 0 &&
89ecd3cf 2445 info->space_after < min) {
13bbb145
N
2446 int rv = sysfs_set_num(sra, NULL, "component_size",
2447 (sra->component_size -
89ecd3cf 2448 min)/2);
13bbb145 2449 if (rv) {
ed503f89 2450 pr_err("cannot reduce component size\n");
13bbb145
N
2451 goto release;
2452 }
2453 }
50a4962f 2454 err = set_new_data_offset(sra, st, devname, info->delta_disks, data_offset,
6a23fb9d 2455 min, 0);
93f174b9
N
2456 if (err == 1) {
2457 pr_err("Cannot set new_data_offset: RAID10 reshape not\n");
2458 cont_err("supported on this kernel\n");
2459 err = -1;
2460 }
13bbb145
N
2461 if (err < 0)
2462 goto release;
2463
ee2429e0 2464 if (!err && sysfs_set_num(sra, NULL, "chunk_size", info->new_chunk) < 0)
19ceb16d
N
2465 err = errno;
2466 if (!err && sysfs_set_num(sra, NULL, "layout", reshape->after.layout) < 0)
2467 err = errno;
2468 if (!err && sysfs_set_num(sra, NULL, "raid_disks",
2469 info->array.raid_disks + info->delta_disks) < 0)
2470 err = errno;
2471 if (!err && sysfs_set_str(sra, NULL, "sync_action", "reshape") < 0)
2472 err = errno;
2473 if (err) {
ed503f89 2474 pr_err("Cannot set array shape for %s\n",
d33f1518
N
2475 devname);
2476 if (err == EBUSY &&
2477 (info->array.state & (1<<MD_SB_BITMAP_PRESENT)))
7a862a02 2478 cont_err(" Bitmap must be removed before shape can be changed\n");
d33f1518 2479 goto release;
19ceb16d
N
2480 }
2481 sysfs_free(sra);
2482 return 0;
2483release:
2484 sysfs_free(sra);
2485 return 1;
2486}
2487
ee2429e0
N
2488static void get_space_after(int fd, struct supertype *st, struct mdinfo *info)
2489{
2490 struct mdinfo *sra, *sd;
f3f09a52
LD
2491 /* Initialisation to silence compiler warning */
2492 unsigned long long min_space_before = 0, min_space_after = 0;
ee2429e0
N
2493 int first = 1;
2494
4dd2df09 2495 sra = sysfs_read(fd, NULL, GET_DEVS);
ee2429e0
N
2496 if (!sra)
2497 return;
2498 for (sd = sra->devs; sd; sd = sd->next) {
2499 char *dn;
2500 int dfd;
2501 struct supertype *st2;
2502 struct mdinfo info2;
2503
2504 if (sd->disk.state & (1<<MD_DISK_FAULTY))
2505 continue;
2506 dn = map_dev(sd->disk.major, sd->disk.minor, 0);
2507 dfd = dev_open(dn, O_RDONLY);
2508 if (dfd < 0)
2509 break;
2510 st2 = dup_super(st);
2511 if (st2->ss->load_super(st2,dfd, NULL)) {
2512 close(dfd);
2513 free(st2);
2514 break;
2515 }
2516 close(dfd);
2517 st2->ss->getinfo_super(st2, &info2, NULL);
2518 st2->ss->free_super(st2);
2519 free(st2);
2520 if (first ||
2521 min_space_before > info2.space_before)
2522 min_space_before = info2.space_before;
2523 if (first ||
2524 min_space_after > info2.space_after)
2525 min_space_after = info2.space_after;
2526 first = 0;
2527 }
2528 if (sd == NULL && !first) {
2529 info->space_after = min_space_after;
2530 info->space_before = min_space_before;
2531 }
2532 sysfs_free(sra);
2533}
2534
434d167e
N
2535static void update_cache_size(char *container, struct mdinfo *sra,
2536 struct mdinfo *info,
2537 int disks, unsigned long long blocks)
2538{
2539 /* Check that the internal stripe cache is
2540 * large enough, or it won't work.
2541 * It must hold at least 4 stripes of the larger
2542 * chunk size
2543 */
2544 unsigned long cache;
2545 cache = max(info->array.chunk_size, info->new_chunk);
2546 cache *= 4; /* 4 stripes minimum */
2547 cache /= 512; /* convert to sectors */
2548 /* make sure there is room for 'blocks' with a bit to spare */
2549 if (cache < 16 + blocks / disks)
2550 cache = 16 + blocks / disks;
caf9ac0c 2551 cache /= (4096/512); /* Convert from sectors to pages */
434d167e
N
2552
2553 if (sra->cache_size < cache)
2554 subarray_set_num(container, sra, "stripe_cache_size",
2555 cache+1);
2556}
2557
77afa056
N
2558static int impose_reshape(struct mdinfo *sra,
2559 struct mdinfo *info,
2560 struct supertype *st,
2561 int fd,
2562 int restart,
2563 char *devname, char *container,
2564 struct reshape *reshape)
2565{
2566 struct mdu_array_info_s array;
2567
2568 sra->new_chunk = info->new_chunk;
2569
2570 if (restart) {
2571 /* for external metadata checkpoint saved by mdmon can be lost
2572 * or missed /due to e.g. crash/. Check if md is not during
2573 * restart farther than metadata points to.
2574 * If so, this means metadata information is obsolete.
2575 */
2576 if (st->ss->external)
2577 verify_reshape_position(info, reshape->level);
2578 sra->reshape_progress = info->reshape_progress;
2579 } else {
2580 sra->reshape_progress = 0;
2581 if (reshape->after.data_disks < reshape->before.data_disks)
2582 /* start from the end of the new array */
2583 sra->reshape_progress = (sra->component_size
2584 * reshape->after.data_disks);
2585 }
2586
2587 ioctl(fd, GET_ARRAY_INFO, &array);
2588 if (info->array.chunk_size == info->new_chunk &&
2589 reshape->before.layout == reshape->after.layout &&
2590 st->ss->external == 0) {
2591 /* use SET_ARRAY_INFO but only if reshape hasn't started */
2592 array.raid_disks = reshape->after.data_disks + reshape->parity;
2593 if (!restart &&
2594 ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
2595 int err = errno;
2596
2597 pr_err("Cannot set device shape for %s: %s\n",
2598 devname, strerror(errno));
2599
2600 if (err == EBUSY &&
2601 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
7a862a02 2602 cont_err("Bitmap must be removed before shape can be changed\n");
77afa056
N
2603
2604 goto release;
2605 }
2606 } else if (!restart) {
2607 /* set them all just in case some old 'new_*' value
2608 * persists from some earlier problem.
2609 */
2610 int err = 0;
2611 if (sysfs_set_num(sra, NULL, "chunk_size", info->new_chunk) < 0)
2612 err = errno;
2613 if (!err && sysfs_set_num(sra, NULL, "layout",
2614 reshape->after.layout) < 0)
2615 err = errno;
2616 if (!err && subarray_set_num(container, sra, "raid_disks",
2617 reshape->after.data_disks +
2618 reshape->parity) < 0)
2619 err = errno;
2620 if (err) {
2621 pr_err("Cannot set device shape for %s\n",
2622 devname);
2623
2624 if (err == EBUSY &&
2625 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
7a862a02 2626 cont_err("Bitmap must be removed before shape can be changed\n");
77afa056
N
2627 goto release;
2628 }
2629 }
2630 return 0;
2631release:
2632 return -1;
2633}
2634
97e3a6a0
N
2635static int impose_level(int fd, int level, char *devname, int verbose)
2636{
2637 char *c;
2638 struct mdu_array_info_s array;
2639 struct mdinfo info;
2640 sysfs_init(&info, fd, NULL);
2641
2642 ioctl(fd, GET_ARRAY_INFO, &array);
2643 if (level == 0 &&
2644 (array.level >= 4 && array.level <= 6)) {
2645 /* To convert to RAID0 we need to fail and
2646 * remove any non-data devices. */
2647 int found = 0;
2648 int d;
2649 int data_disks = array.raid_disks - 1;
2650 if (array.level == 6)
2651 data_disks -= 1;
2652 if (array.level == 5 &&
2653 array.layout != ALGORITHM_PARITY_N)
2654 return -1;
2655 if (array.level == 6 &&
2656 array.layout != ALGORITHM_PARITY_N_6)
2657 return -1;
2658 sysfs_set_str(&info, NULL,"sync_action", "idle");
2659 /* First remove any spares so no recovery starts */
2660 for (d = 0, found = 0;
2661 d < MAX_DISKS && found < array.nr_disks;
2662 d++) {
5e7be838 2663 mdu_disk_info_t disk;
97e3a6a0
N
2664 disk.number = d;
2665 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
2666 continue;
2667 if (disk.major == 0 && disk.minor == 0)
2668 continue;
2669 found++;
2670 if ((disk.state & (1 << MD_DISK_ACTIVE))
2671 && disk.raid_disk < data_disks)
2672 /* keep this */
2673 continue;
2674 ioctl(fd, HOT_REMOVE_DISK,
2675 makedev(disk.major, disk.minor));
2676 }
2677 /* Now fail anything left */
2678 ioctl(fd, GET_ARRAY_INFO, &array);
2679 for (d = 0, found = 0;
2680 d < MAX_DISKS && found < array.nr_disks;
2681 d++) {
2682 int cnt;
2683 mdu_disk_info_t disk;
2684 disk.number = d;
2685 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
2686 continue;
2687 if (disk.major == 0 && disk.minor == 0)
2688 continue;
2689 found++;
2690 if ((disk.state & (1 << MD_DISK_ACTIVE))
2691 && disk.raid_disk < data_disks)
2692 /* keep this */
2693 continue;
2694 ioctl(fd, SET_DISK_FAULTY,
2695 makedev(disk.major, disk.minor));
2696 cnt = 5;
2697 while (ioctl(fd, HOT_REMOVE_DISK,
2698 makedev(disk.major, disk.minor)) < 0
2699 && errno == EBUSY
2700 && cnt--) {
2701 usleep(10000);
2702 }
2703 }
2704 }
2705 c = map_num(pers, level);
2706 if (c) {
2707 int err = sysfs_set_str(&info, NULL, "level", c);
2708 if (err) {
2709 err = errno;
2710 pr_err("%s: could not set level to %s\n",
2711 devname, c);
2712 if (err == EBUSY &&
2713 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
7a862a02 2714 cont_err("Bitmap must be removed before level can be changed\n");
97e3a6a0
N
2715 return err;
2716 }
2717 if (verbose >= 0)
2718 pr_err("level of %s changed to %s\n",
2719 devname, c);
2720 }
2721 return 0;
2722}
2723
84d11e6c
N
2724int sigterm = 0;
2725static void catch_term(int sig)
2726{
2727 sigterm = 1;
2728}
2729
b0b67933
N
2730static int continue_via_systemd(char *devnm)
2731{
2732 int skipped, i, pid, status;
2733 char pathbuf[1024];
2734 /* In a systemd/udev world, it is best to get systemd to
2735 * run "mdadm --grow --continue" rather than running in the
2736 * background.
2737 */
2738 switch(fork()) {
2739 case 0:
2740 /* FIXME yuk. CLOSE_EXEC?? */
2741 skipped = 0;
2742 for (i = 3; skipped < 20; i++)
2743 if (close(i) < 0)
2744 skipped++;
2745 else
2746 skipped = 0;
2747
2748 /* Don't want to see error messages from
2749 * systemctl. If the service doesn't exist,
2750 * we fork ourselves.
2751 */
2752 close(2);
2753 open("/dev/null", O_WRONLY);
2754 snprintf(pathbuf, sizeof(pathbuf), "mdadm-grow-continue@%s.service",
2755 devnm);
2756 status = execl("/usr/bin/systemctl", "systemctl",
2757 "start",
2758 pathbuf, NULL);
2759 status = execl("/bin/systemctl", "systemctl", "start",
2760 pathbuf, NULL);
2761 exit(1);
2762 case -1: /* Just do it ourselves. */
2763 break;
2764 default: /* parent - good */
2765 pid = wait(&status);
2766 if (pid >= 0 && status == 0)
2767 return 1;
2768 }
2769 return 0;
2770}
2771
5da9ab98
N
2772static int reshape_array(char *container, int fd, char *devname,
2773 struct supertype *st, struct mdinfo *info,
e2e53a2d 2774 int force, struct mddev_dev *devlist,
19ceb16d 2775 unsigned long long data_offset,
ba728be7 2776 char *backup_file, int verbose, int forked,
b76b30e0 2777 int restart, int freeze_reshape)
5da9ab98
N
2778{
2779 struct reshape reshape;
2780 int spares_needed;
2781 char *msg;
2782 int orig_level = UnSet;
434d167e 2783 int odisks;
1f9b0e28 2784 int delayed;
7bc71196 2785
5da9ab98
N
2786 struct mdu_array_info_s array;
2787 char *c;
e86c9dd6 2788
e2e53a2d
N
2789 struct mddev_dev *dv;
2790 int added_disks;
2791
d152f53e
JS
2792 int *fdlist = NULL;
2793 unsigned long long *offsets = NULL;
5da9ab98
N
2794 int d;
2795 int nrdisks;
2796 int err;
da8100ca 2797 unsigned long blocks;
5da9ab98
N
2798 unsigned long long array_size;
2799 int done;
cf6ac177 2800 struct mdinfo *sra = NULL;
13ffbe89 2801 char buf[20];
7bc71196 2802
9468aeac
N
2803 /* when reshaping a RAID0, the component_size might be zero.
2804 * So try to fix that up.
2805 */
2806 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
2807 dprintf("Cannot get array information.\n");
2808 goto release;
2809 }
2810 if (array.level == 0 && info->component_size == 0) {
2811 get_dev_size(fd, NULL, &array_size);
2812 info->component_size = array_size / array.raid_disks;
2813 }
2814
ee2429e0
N
2815 if (array.level == 10)
2816 /* Need space_after info */
2817 get_space_after(fd, st, info);
2818
3cb2aed2
N
2819 if (info->reshape_active) {
2820 int new_level = info->new_level;
2821 info->new_level = UnSet;
ce4783d3
N
2822 if (info->delta_disks > 0)
2823 info->array.raid_disks -= info->delta_disks;
a73b0081 2824 msg = analyse_change(devname, info, &reshape);
3cb2aed2 2825 info->new_level = new_level;
ce4783d3
N
2826 if (info->delta_disks > 0)
2827 info->array.raid_disks += info->delta_disks;
fcdfb814
AK
2828 if (!restart)
2829 /* Make sure the array isn't read-only */
2830 ioctl(fd, RESTART_ARRAY_RW, 0);
3cb2aed2 2831 } else
a73b0081 2832 msg = analyse_change(devname, info, &reshape);
5da9ab98 2833 if (msg) {
a73b0081
N
2834 /* if msg == "", error has already been printed */
2835 if (msg[0])
2836 pr_err("%s\n", msg);
631d7405 2837 goto release;
5da9ab98 2838 }
817ed7d6
N
2839 if (restart &&
2840 (reshape.level != info->array.level ||
2841 reshape.before.layout != info->array.layout ||
384e9be1 2842 reshape.before.data_disks + reshape.parity
ce4783d3 2843 != info->array.raid_disks - max(0, info->delta_disks))) {
7a862a02 2844 pr_err("reshape info is not in native format - cannot continue.\n");
20a40eca
N
2845 goto release;
2846 }
a93f87ee 2847
13ffbe89
PB
2848 if (st->ss->external && restart && (info->reshape_progress == 0) &&
2849 !((sysfs_get_str(info, NULL, "sync_action", buf, sizeof(buf)) > 0) &&
2850 (strncmp(buf, "reshape", 7) == 0))) {
e1dd332a
AK
2851 /* When reshape is restarted from '0', very begin of array
2852 * it is possible that for external metadata reshape and array
2853 * configuration doesn't happen.
2854 * Check if md has the same opinion, and reshape is restarted
2855 * from 0. If so, this is regular reshape start after reshape
2856 * switch in metadata to next array only.
2857 */
2858 if ((verify_reshape_position(info, reshape.level) >= 0) &&
2859 (info->reshape_progress == 0))
2860 restart = 0;
2861 }
a93f87ee
N
2862 if (restart) {
2863 /* reshape already started. just skip to monitoring the reshape */
2864 if (reshape.backup_blocks == 0)
2865 return 0;
8ecf12b9
N
2866 if (restart & RESHAPE_NO_BACKUP)
2867 return 0;
dfa4d769
N
2868
2869 /* Need 'sra' down at 'started:' */
2870 sra = sysfs_read(fd, NULL,
2871 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK|
2872 GET_CACHE);
2873 if (!sra) {
2874 pr_err("%s: Cannot get array details from sysfs\n",
2875 devname);
2876 goto release;
2877 }
54ded86f
N
2878
2879 if (!backup_file)
2880 backup_file = locate_backup(sra->sys_name);
2881
a93f87ee
N
2882 goto started;
2883 }
a9c3e78f
AK
2884 /* The container is frozen but the array may not be.
2885 * So freeze the array so spares don't get put to the wrong use
2886 * FIXME there should probably be a cleaner separation between
2887 * freeze_array and freeze_container.
2888 */
2889 sysfs_freeze_array(info);
e06c4e59 2890 /* Check we have enough spares to not be degraded */
e2e53a2d
N
2891 added_disks = 0;
2892 for (dv = devlist; dv ; dv=dv->next)
2893 added_disks++;
5da9ab98
N
2894 spares_needed = max(reshape.before.data_disks,
2895 reshape.after.data_disks)
2896 + reshape.parity - array.raid_disks;
7bc71196 2897
88c1a083 2898 if (!force &&
e2e53a2d
N
2899 info->new_level > 1 && info->array.level > 1 &&
2900 spares_needed > info->array.spare_disks + added_disks) {
7a862a02 2901 pr_err("Need %d spare%s to avoid degraded array, and only have %d.\n"
e7b84f9d
N
2902 " Use --force to over-ride this check.\n",
2903 spares_needed,
2904 spares_needed == 1 ? "" : "s",
2905 info->array.spare_disks + added_disks);
631d7405 2906 goto release;
7bc71196 2907 }
e06c4e59
N
2908 /* Check we have enough spares to not fail */
2909 spares_needed = max(reshape.before.data_disks,
2910 reshape.after.data_disks)
2911 - array.raid_disks;
2912 if ((info->new_level > 1 || info->new_level == 0) &&
e2e53a2d 2913 spares_needed > info->array.spare_disks +added_disks) {
7a862a02 2914 pr_err("Need %d spare%s to create working array, and only have %d.\n",
e7b84f9d
N
2915 spares_needed,
2916 spares_needed == 1 ? "" : "s",
2917 info->array.spare_disks + added_disks);
e06c4e59
N
2918 goto release;
2919 }
e86c9dd6 2920
eae35f5c 2921 if (reshape.level != array.level) {
97e3a6a0
N
2922 int err = impose_level(fd, reshape.level, devname, verbose);
2923 if (err)
631d7405 2924 goto release;
97e3a6a0
N
2925 info->new_layout = UnSet; /* after level change,
2926 * layout is meaningless */
eae35f5c 2927 orig_level = array.level;
3cd4e7c4 2928 sysfs_freeze_array(info);
e86c9dd6 2929
16d4d84e
AK
2930 if (reshape.level > 0 && st->ss->external) {
2931 /* make sure mdmon is aware of the new level */
4dd2df09 2932 if (mdmon_running(container))
78340e26
AK
2933 flush_mdmon(container);
2934
4dd2df09
N
2935 if (!mdmon_running(container))
2936 start_mdmon(container);
16d4d84e 2937 ping_monitor(container);
4dd2df09 2938 if (mdmon_running(container) &&
e919fb0a
AK
2939 st->update_tail == NULL)
2940 st->update_tail = &st->updates;
16d4d84e 2941 }
5da9ab98 2942 }
5da9ab98
N
2943 /* ->reshape_super might have chosen some spares from the
2944 * container that it wants to be part of the new array.
2945 * We can collect them with ->container_content and give
2946 * them to the kernel.
2947 */
2948 if (st->ss->reshape_super && st->ss->container_content) {
2949 char *subarray = strchr(info->text_version+1, '/')+1;
2950 struct mdinfo *info2 =
2951 st->ss->container_content(st, subarray);
2952 struct mdinfo *d;
2953
2dd0d697 2954 if (info2) {
4dd2df09 2955 sysfs_init(info2, fd, st->devnm);
0d5ac3c6
N
2956 /* When increasing number of devices, we need to set
2957 * new raid_disks before adding these, or they might
2958 * be rejected.
2959 */
2960 if (reshape.backup_blocks &&
2961 reshape.after.data_disks > reshape.before.data_disks)
2962 subarray_set_num(container, info2, "raid_disks",
2963 reshape.after.data_disks +
2964 reshape.parity);
5da9ab98
N
2965 for (d = info2->devs; d; d = d->next) {
2966 if (d->disk.state == 0 &&
2967 d->disk.raid_disk >= 0) {
2968 /* This is a spare that wants to
2969 * be part of the array.
2970 */
2971 add_disk(fd, st, info2, d);
2972 }
9860f271 2973 }
2dd0d697
AK
2974 sysfs_free(info2);
2975 }
5da9ab98 2976 }
e2e53a2d
N
2977 /* We might have been given some devices to add to the
2978 * array. Now that the array has been changed to the right
2979 * level and frozen, we can safely add them.
2980 */
2981 if (devlist)
ba728be7 2982 Manage_subdevs(devname, fd, devlist, verbose,
11b391ec 2983 0,NULL, 0);
1686dc25 2984
e09233d0 2985 if (reshape.backup_blocks == 0 && data_offset != INVALID_SECTORS)
8876bf0b 2986 reshape.backup_blocks = reshape.before.data_disks * info->array.chunk_size/512;
b4f8e38b 2987 if (reshape.backup_blocks == 0) {
5da9ab98
N
2988 /* No restriping needed, but we might need to impose
2989 * some more changes: layout, raid_disks, chunk_size
e86c9dd6 2990 */
24aebf3a
KW
2991 /* read current array info */
2992 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
83732c28 2993 dprintf("Cannot get array information.\n");
24aebf3a
KW
2994 goto release;
2995 }
2996 /* compare current array info with new values and if
2997 * it is different update them to new */
5da9ab98 2998 if (info->new_layout != UnSet &&
24aebf3a
KW
2999 info->new_layout != array.layout) {
3000 array.layout = info->new_layout;
3001 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
e7b84f9d 3002 pr_err("failed to set new layout\n");
631d7405 3003 goto release;
ba728be7 3004 } else if (verbose >= 0)
5da9ab98 3005 printf("layout for %s set to %d\n",
24aebf3a 3006 devname, array.layout);
5da9ab98
N
3007 }
3008 if (info->delta_disks != UnSet &&
24aebf3a
KW
3009 info->delta_disks != 0 &&
3010 array.raid_disks != (info->array.raid_disks + info->delta_disks)) {
3011 array.raid_disks += info->delta_disks;
3012 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
e7b84f9d 3013 pr_err("failed to set raid disks\n");
631d7405 3014 goto release;
ba728be7 3015 } else if (verbose >= 0) {
5da9ab98 3016 printf("raid_disks for %s set to %d\n",
24aebf3a
KW
3017 devname, array.raid_disks);
3018 }
5da9ab98
N
3019 }
3020 if (info->new_chunk != 0 &&
24aebf3a 3021 info->new_chunk != array.chunk_size) {
5da9ab98
N
3022 if (sysfs_set_num(info, NULL,
3023 "chunk_size", info->new_chunk) != 0) {
e7b84f9d 3024 pr_err("failed to set chunk size\n");
631d7405 3025 goto release;
ba728be7 3026 } else if (verbose >= 0)
5da9ab98 3027 printf("chunk size for %s set to %d\n",
24aebf3a 3028 devname, array.chunk_size);
4725bc31 3029 }
e35b189b 3030 unfreeze(st);
631d7405 3031 return 0;
5da9ab98 3032 }
19678e53 3033
5da9ab98
N
3034 /*
3035 * There are three possibilities.
3036 * 1/ The array will shrink.
3037 * We need to ensure the reshape will pause before reaching
3038 * the 'critical section'. We also need to fork and wait for
24daa16f 3039 * that to happen. When it does we
5da9ab98
N
3040 * suspend/backup/complete/unfreeze
3041 *
3042 * 2/ The array will not change size.
3043 * This requires that we keep a backup of a sliding window
3044 * so that we can restore data after a crash. So we need
3045 * to fork and monitor progress.
3046 * In future we will allow the data_offset to change, so
3047 * a sliding backup becomes unnecessary.
3048 *
3049 * 3/ The array will grow. This is relatively easy.
3050 * However the kernel's restripe routines will cheerfully
3051 * overwrite some early data before it is safe. So we
3052 * need to make a backup of the early parts of the array
3053 * and be ready to restore it if rebuild aborts very early.
3054 * For externally managed metadata, we still need a forked
3055 * child to monitor the reshape and suspend IO over the region
3056 * that is being reshaped.
3057 *
3058 * We backup data by writing it to one spare, or to a
3059 * file which was given on command line.
3060 *
3061 * In each case, we first make sure that storage is available
3062 * for the required backup.
3063 * Then we:
3064 * - request the shape change.
3065 * - fork to handle backup etc.
3066 */
5da9ab98
N
3067 /* Check that we can hold all the data */
3068 get_dev_size(fd, NULL, &array_size);
3069 if (reshape.new_size < (array_size/512)) {
e7b84f9d
N
3070 pr_err("this change will reduce the size of the array.\n"
3071 " use --grow --array-size first to truncate array.\n"
3072 " e.g. mdadm --grow %s --array-size %llu\n",
3073 devname, reshape.new_size/2);
5da9ab98
N
3074 goto release;
3075 }
7236ee7a 3076
19ceb16d 3077 if (array.level == 10) {
033d0929 3078 /* Reshaping RAID10 does not require any data backup by
19ceb16d
N
3079 * user-space. Instead it requires that the data_offset
3080 * is changed to avoid the need for backup.
3081 * So this is handled very separately
3082 */
3083 if (restart)
3084 /* Nothing to do. */
3085 return 0;
3086 return raid10_reshape(container, fd, devname, st, info,
3087 &reshape, data_offset,
3088 force, verbose);
3089 }
4dd2df09 3090 sra = sysfs_read(fd, NULL,
3656edcd 3091 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK|
5da9ab98 3092 GET_CACHE);
5da9ab98 3093 if (!sra) {
e7b84f9d 3094 pr_err("%s: Cannot get array details from sysfs\n",
5da9ab98 3095 devname);
5da9ab98
N
3096 goto release;
3097 }
7236ee7a 3098
8192902f 3099 if (!backup_file)
199f1a1f
N
3100 switch(set_new_data_offset(sra, st, devname,
3101 reshape.after.data_disks - reshape.before.data_disks,
8192902f 3102 data_offset,
6a23fb9d 3103 reshape.min_offset_change, 1)) {
63c12c89
N
3104 case -1:
3105 goto release;
3106 case 0:
3107 /* Updated data_offset, so it's easy now */
3108 update_cache_size(container, sra, info,
3109 min(reshape.before.data_disks,
3110 reshape.after.data_disks),
3111 reshape.backup_blocks);
3112
3113 /* Right, everything seems fine. Let's kick things off.
3114 */
3115 sync_metadata(st);
3116
3117 if (impose_reshape(sra, info, st, fd, restart,
3118 devname, container, &reshape) < 0)
3119 goto release;
3120 if (sysfs_set_str(sra, NULL, "sync_action", "reshape") < 0) {
783bbc2b
N
3121 struct mdinfo *sd;
3122 if (errno != EINVAL) {
3123 pr_err("Failed to initiate reshape!\n");
3124 goto release;
3125 }
3126 /* revert data_offset and try the old way */
3127 for (sd = sra->devs; sd; sd = sd->next) {
3128 sysfs_set_num(sra, sd, "new_offset",
3129 sd->data_offset);
3130 sysfs_set_str(sra, NULL, "reshape_direction",
3131 "forwards");
3132 }
3133 break;
63c12c89 3134 }
97e3a6a0
N
3135 if (info->new_level == reshape.level)
3136 return 0;
3137 /* need to adjust level when reshape completes */
3138 switch(fork()) {
3139 case -1: /* ignore error, but don't wait */
3140 return 0;
3141 default: /* parent */
3142 return 0;
3143 case 0:
3144 map_fork();
3145 break;
3146 }
3377ee42 3147 close(fd);
97e3a6a0 3148 wait_reshape(sra);
3377ee42
N
3149 fd = open_dev(sra->sys_name);
3150 if (fd >= 0)
3151 impose_level(fd, info->new_level, devname, verbose);
63c12c89
N
3152 return 0;
3153 case 1: /* Couldn't set data_offset, try the old way */
3154 if (data_offset != INVALID_SECTORS) {
3155 pr_err("Cannot update data_offset on this array\n");
3156 goto release;
3157 }
3158 break;
3159 }
3160
8ecf12b9 3161started:
5da9ab98
N
3162 /* Decide how many blocks (sectors) for a reshape
3163 * unit. The number we have so far is just a minimum
3164 */
b4f8e38b 3165 blocks = reshape.backup_blocks;
24daa16f 3166 if (reshape.before.data_disks ==
5da9ab98
N
3167 reshape.after.data_disks) {
3168 /* Make 'blocks' bigger for better throughput, but
3169 * not so big that we reject it below.
3170 * Try for 16 megabytes
3171 */
3172 while (blocks * 32 < sra->component_size &&
3173 blocks < 16*1024*2)
3174 blocks *= 2;
3175 } else
7a862a02 3176 pr_err("Need to backup %luK of critical section..\n", blocks/2);
5da9ab98
N
3177
3178 if (blocks >= sra->component_size/2) {
7a862a02 3179 pr_err("%s: Something wrong - reshape aborted\n",
5da9ab98 3180 devname);
5da9ab98
N
3181 goto release;
3182 }
7236ee7a 3183
5da9ab98
N
3184 /* Now we need to open all these devices so we can read/write.
3185 */
b8b286a6
N
3186 nrdisks = max(reshape.before.data_disks,
3187 reshape.after.data_disks) + reshape.parity
3188 + sra->array.spare_disks;
503975b9
N
3189 fdlist = xcalloc((1+nrdisks), sizeof(int));
3190 offsets = xcalloc((1+nrdisks), sizeof(offsets[0]));
e380d3be 3191
b8b286a6
N
3192 odisks = reshape.before.data_disks + reshape.parity;
3193 d = reshape_prepare_fdlist(devname, sra, odisks,
5da9ab98
N
3194 nrdisks, blocks, backup_file,
3195 fdlist, offsets);
8e7ddc5f 3196 if (d < odisks) {
5da9ab98
N
3197 goto release;
3198 }
13c37ad3
AK
3199 if ((st->ss->manage_reshape == NULL) ||
3200 (st->ss->recover_backup == NULL)) {
3201 if (backup_file == NULL) {
3202 if (reshape.after.data_disks <=
3203 reshape.before.data_disks) {
be7c26b4
N
3204 pr_err("%s: Cannot grow - need backup-file\n",
3205 devname);
3206 pr_err(" Please provide one with \"--backup=...\"\n");
13c37ad3 3207 goto release;
8e7ddc5f 3208 } else if (d == odisks) {
7a862a02 3209 pr_err("%s: Cannot grow - need a spare or backup-file to backup critical section\n", devname);
13c37ad3
AK
3210 goto release;
3211 }
3212 } else {
3213 if (!reshape_open_backup_file(backup_file, fd, devname,
3214 (signed)blocks,
3215 fdlist+d, offsets+d,
54ded86f 3216 sra->sys_name,
13c37ad3
AK
3217 restart)) {
3218 goto release;
3219 }
3220 d++;
e86c9dd6 3221 }
5da9ab98 3222 }
7236ee7a 3223
434d167e
N
3224 update_cache_size(container, sra, info,
3225 min(reshape.before.data_disks, reshape.after.data_disks),
3226 blocks);
5da9ab98
N
3227
3228 /* Right, everything seems fine. Let's kick things off.
3229 * If only changing raid_disks, use ioctl, else use
3230 * sysfs.
3231 */
3232 sync_metadata(st);
3233
77afa056
N
3234 if (impose_reshape(sra, info, st, fd, restart,
3235 devname, container, &reshape) < 0)
3236 goto release;
e86c9dd6 3237
27a1e5b5
N
3238 err = start_reshape(sra, restart, reshape.before.data_disks,
3239 reshape.after.data_disks);
fab32c97 3240 if (err) {
e7b84f9d
N
3241 pr_err("Cannot %s reshape for %s\n",
3242 restart ? "continue" : "start",
3243 devname);
fab32c97
AK
3244 goto release;
3245 }
a93f87ee
N
3246 if (restart)
3247 sysfs_set_str(sra, NULL, "array_state", "active");
b76b30e0
AK
3248 if (freeze_reshape) {
3249 free(fdlist);
3250 free(offsets);
3251 sysfs_free(sra);
7a862a02 3252 pr_err("Reshape has to be continued from location %llu when root filesystem has been mounted.\n",
b76b30e0
AK
3253 sra->reshape_progress);
3254 return 1;
3255 }
5da9ab98 3256
b0b67933
N
3257 if (!forked && !check_env("MDADM_NO_SYSTEMCTL"))
3258 if (continue_via_systemd(container ?: sra->sys_name)) {
3259 free(fdlist);
3260 free(offsets);
3261 sysfs_free(sra);
3262 return 0;
5e76dce1 3263 }
5e76dce1 3264
5da9ab98
N
3265 /* Now we just need to kick off the reshape and watch, while
3266 * handling backups of the data...
3267 * This is all done by a forked background process.
3268 */
3269 switch(forked ? 0 : fork()) {
6b2d630c 3270 case -1:
e7b84f9d 3271 pr_err("Cannot run child to monitor reshape: %s\n",
6b2d630c 3272 strerror(errno));
6b2d630c 3273 abort_reshape(sra);
631d7405 3274 goto release;
6b2d630c 3275 default:
d152f53e
JS
3276 free(fdlist);
3277 free(offsets);
3278 sysfs_free(sra);
6b2d630c 3279 return 0;
5da9ab98 3280 case 0:
cc700db3 3281 map_fork();
6b2d630c
N
3282 break;
3283 }
5da9ab98 3284
1f9b0e28
N
3285 /* If another array on the same devices is busy, the
3286 * reshape will wait for them. This would mean that
3287 * the first section that we suspend will stay suspended
3288 * for a long time. So check on that possibility
3289 * by looking for "DELAYED" in /proc/mdstat, and if found,
3290 * wait a while
3291 */
3292 do {
3293 struct mdstat_ent *mds, *m;
3294 delayed = 0;
a7a0d8a1 3295 mds = mdstat_read(1, 0);
ae0dcfbd 3296 for (m = mds; m; m = m->next)
4dd2df09 3297 if (strcmp(m->devnm, sra->sys_name) == 0) {
1f9b0e28
N
3298 if (m->resync &&
3299 m->percent == RESYNC_DELAYED)
3300 delayed = 1;
3301 if (m->resync == 0)
3302 /* Haven't started the reshape thread
3303 * yet, wait a bit
3304 */
3305 delayed = 2;
3306 break;
3307 }
3308 free_mdstat(mds);
3309 if (delayed == 1 && get_linux_version() < 3007000) {
3310 pr_err("Reshape is delayed, but cannot wait carefully with this kernel.\n"
3311 " You might experience problems until other reshapes complete.\n");
3312 delayed = 0;
3313 }
3314 if (delayed)
a7a0d8a1 3315 mdstat_wait(30 - (delayed-1) * 25);
1f9b0e28 3316 } while (delayed);
a7a0d8a1 3317 mdstat_close();
6b2d630c
N
3318 close(fd);
3319 if (check_env("MDADM_GROW_VERIFY"))
3320 fd = open(devname, O_RDONLY | O_DIRECT);
3321 else
3322 fd = -1;
3323 mlockall(MCL_FUTURE);
5da9ab98 3324
84d11e6c
N
3325 signal(SIGTERM, catch_term);
3326
6b2d630c
N
3327 if (st->ss->external) {
3328 /* metadata handler takes it from here */
3329 done = st->ss->manage_reshape(
3330 fd, sra, &reshape, st, blocks,
3331 fdlist, offsets,
3332 d - odisks, fdlist+odisks,
3333 offsets+odisks);
3334 } else
3335 done = child_monitor(
3336 fd, sra, &reshape, st, blocks,
3337 fdlist, offsets,
3338 d - odisks, fdlist+odisks,
3339 offsets+odisks);
3340
d152f53e
JS
3341 free(fdlist);
3342 free(offsets);
3343
54ded86f
N
3344 if (backup_file && done) {
3345 char *bul;
54ded86f
N
3346 bul = make_backup(sra->sys_name);
3347 if (bul) {
5e76dce1 3348 char buf[1024];
9eb5ce5a 3349 int l = readlink(bul, buf, sizeof(buf) - 1);
5e76dce1
N
3350 if (l > 0) {
3351 buf[l]=0;
3352 unlink(buf);
3353 }
54ded86f
N
3354 unlink(bul);
3355 free(bul);
3356 }
5e76dce1 3357 unlink(backup_file);
54ded86f 3358 }
6b2d630c
N
3359 if (!done) {
3360 abort_reshape(sra);
3361 goto out;
3362 }
1cbc5680
N
3363
3364 if (!st->ss->external &&
3365 !(reshape.before.data_disks != reshape.after.data_disks
3366 && info->custom_array_size) &&
3367 info->new_level == reshape.level &&
3368 !forked) {
3369 /* no need to wait for the reshape to finish as
3370 * there is nothing more to do.
3371 */
d152f53e 3372 sysfs_free(sra);
1cbc5680
N
3373 exit(0);
3374 }
3375 wait_reshape(sra);
3376
3377 if (st->ss->external) {
3378 /* Re-load the metadata as much could have changed */
4dd2df09 3379 int cfd = open_dev(st->container_devnm);
1cbc5680 3380 if (cfd >= 0) {
78340e26 3381 flush_mdmon(container);
1cbc5680
N
3382 st->ss->free_super(st);
3383 st->ss->load_container(st, cfd, container);
3384 close(cfd);
3385 }
3386 }
3387
6b2d630c
N
3388 /* set new array size if required customer_array_size is used
3389 * by this metadata.
3390 */
3391 if (reshape.before.data_disks !=
3392 reshape.after.data_disks &&
54397ed9
AK
3393 info->custom_array_size)
3394 set_array_size(st, info, info->text_version);
582496b2 3395
6b2d630c 3396 if (info->new_level != reshape.level) {
97e3a6a0
N
3397 if (fd < 0)
3398 fd = open(devname, O_RDONLY);
3399 impose_level(fd, info->new_level, devname, verbose);
3400 close(fd);
e919fb0a
AK
3401 if (info->new_level == 0)
3402 st->update_tail = NULL;
7236ee7a 3403 }
6b2d630c 3404out:
d152f53e 3405 sysfs_free(sra);
6b2d630c
N
3406 if (forked)
3407 return 0;
e84f2c00 3408 unfreeze(st);
6b2d630c 3409 exit(0);
e86c9dd6 3410
6b2d630c 3411release:
d152f53e
JS
3412 free(fdlist);
3413 free(offsets);
1cbc5680 3414 if (orig_level != UnSet && sra) {
7236ee7a
N
3415 c = map_num(pers, orig_level);
3416 if (c && sysfs_set_str(sra, NULL, "level", c) == 0)
e7b84f9d 3417 pr_err("aborting level change\n");
7236ee7a 3418 }
d152f53e 3419 sysfs_free(sra);
9202b8eb
N
3420 if (!forked)
3421 unfreeze(st);
631d7405 3422 return 1;
7236ee7a 3423}
e86c9dd6 3424
9ad6f6e6
AK
3425/* mdfd handle is passed to be closed in child process (after fork).
3426 */
493f5dd6 3427int reshape_container(char *container, char *devname,
9ad6f6e6 3428 int mdfd,
24daa16f 3429 struct supertype *st,
5da9ab98
N
3430 struct mdinfo *info,
3431 int force,
b0140ae8
N
3432 char *backup_file, int verbose,
3433 int forked, int restart, int freeze_reshape)
5da9ab98 3434{
1bb174ba 3435 struct mdinfo *cc = NULL;
bcc9e9ed 3436 int rv = restart;
4dd2df09 3437 char last_devnm[32] = "";
1bb174ba 3438
d8eb27f7 3439 /* component_size is not meaningful for a container,
7ec0996c 3440 * so pass '0' meaning 'no change'
d8eb27f7 3441 */
493f5dd6 3442 if (!restart &&
7ec0996c 3443 reshape_super(st, 0, info->new_level,
5da9ab98 3444 info->new_layout, info->new_chunk,
41784c88 3445 info->array.raid_disks, info->delta_disks,
016e00f5 3446 backup_file, devname, APPLY_METADATA_CHANGES,
ba728be7 3447 verbose)) {
9e325442 3448 unfreeze(st);
5da9ab98 3449 return 1;
9e325442 3450 }
5da9ab98
N
3451
3452 sync_metadata(st);
3453
1bb174ba
AK
3454 /* ping monitor to be sure that update is on disk
3455 */
3456 ping_monitor(container);
5da9ab98 3457
40b941b8 3458 if (!forked && !freeze_reshape && !check_env("MDADM_NO_SYSTEMCTL"))
4e0eb0db
N
3459 if (continue_via_systemd(container))
3460 return 0;
3461
b0140ae8 3462 switch (forked ? 0 : fork()) {
5da9ab98
N
3463 case -1: /* error */
3464 perror("Cannot fork to complete reshape\n");
9e325442 3465 unfreeze(st);
5da9ab98
N
3466 return 1;
3467 default: /* parent */
b76b30e0 3468 if (!freeze_reshape)
7a862a02 3469 printf("%s: multi-array reshape continues in background\n", Name);
5da9ab98
N
3470 return 0;
3471 case 0: /* child */
cc700db3 3472 map_fork();
5da9ab98
N
3473 break;
3474 }
3475
9ad6f6e6
AK
3476 /* close unused handle in child process
3477 */
3478 if (mdfd > -1)
3479 close(mdfd);
3480
1bb174ba
AK
3481 while(1) {
3482 /* For each member array with reshape_active,
3483 * we need to perform the reshape.
3484 * We pick the first array that needs reshaping and
3485 * reshape it. reshape_array() will re-read the metadata
3486 * so the next time through a different array should be
3487 * ready for reshape.
493f5dd6
N
3488 * It is possible that the 'different' array will not
3489 * be assembled yet. In that case we simple exit.
3490 * When it is assembled, the mdadm which assembles it
3491 * will take over the reshape.
1bb174ba
AK
3492 */
3493 struct mdinfo *content;
5da9ab98
N
3494 int fd;
3495 struct mdstat_ent *mdstat;
5da9ab98 3496 char *adev;
4dd2df09 3497 int devid;
5da9ab98 3498
1bb174ba 3499 sysfs_free(cc);
5da9ab98 3500
1bb174ba
AK
3501 cc = st->ss->container_content(st, NULL);
3502
3503 for (content = cc; content ; content = content->next) {
3504 char *subarray;
3505 if (!content->reshape_active)
3506 continue;
3507
3508 subarray = strchr(content->text_version+1, '/')+1;
4dd2df09 3509 mdstat = mdstat_by_subdev(subarray, container);
1bb174ba
AK
3510 if (!mdstat)
3511 continue;
1ca90aa6 3512 if (mdstat->active == 0) {
4dd2df09
N
3513 pr_err("Skipping inactive array %s.\n",
3514 mdstat->devnm);
1ca90aa6
AK
3515 free_mdstat(mdstat);
3516 mdstat = NULL;
3517 continue;
3518 }
1bb174ba
AK
3519 break;
3520 }
3521 if (!content)
3522 break;
5da9ab98 3523
4dd2df09
N
3524 devid = devnm2devid(mdstat->devnm);
3525 adev = map_dev(major(devid), minor(devid), 0);
5da9ab98 3526 if (!adev)
1bb174ba
AK
3527 adev = content->text_version;
3528
4dd2df09 3529 fd = open_dev(mdstat->devnm);
97a3490c 3530 if (fd < 0) {
1ade5cc1 3531 pr_err("Device %s cannot be opened for reshape.\n", adev);
97a3490c
AK
3532 break;
3533 }
3534
4dd2df09 3535 if (strcmp(last_devnm, mdstat->devnm) == 0) {
2d04d7e5
AK
3536 /* Do not allow for multiple reshape_array() calls for
3537 * the same array.
3538 * It can happen when reshape_array() returns without
3539 * error, when reshape is not finished (wrong reshape
3540 * starting/continuation conditions). Mdmon doesn't
3541 * switch to next array in container and reentry
3542 * conditions for the same array occur.
3543 * This is possibly interim until the behaviour of
3544 * reshape_array is resolved().
3545 */
7a862a02 3546 printf("%s: Multiple reshape execution detected for device %s.\n", Name, adev);
2d04d7e5
AK
3547 close(fd);
3548 break;
3549 }
4dd2df09 3550 strcpy(last_devnm, mdstat->devnm);
2d04d7e5 3551
4dd2df09 3552 sysfs_init(content, fd, mdstat->devnm);
5da9ab98 3553
4dd2df09 3554 if (mdmon_running(container))
78340e26
AK
3555 flush_mdmon(container);
3556
1bb174ba 3557 rv = reshape_array(container, fd, adev, st,
ca36d707 3558 content, force, NULL, INVALID_SECTORS,
ba728be7 3559 backup_file, verbose, 1, restart,
b76b30e0 3560 freeze_reshape);
5da9ab98 3561 close(fd);
b76b30e0
AK
3562
3563 if (freeze_reshape) {
3564 sysfs_free(cc);
3565 exit(0);
3566 }
3567
493f5dd6 3568 restart = 0;
5da9ab98
N
3569 if (rv)
3570 break;
78340e26 3571
4dd2df09 3572 if (mdmon_running(container))
78340e26 3573 flush_mdmon(container);
5da9ab98 3574 }
bcc9e9ed
AK
3575 if (!rv)
3576 unfreeze(st);
1bb174ba 3577 sysfs_free(cc);
5da9ab98
N
3578 exit(0);
3579}
3580
7236ee7a
N
3581/*
3582 * We run a child process in the background which performs the following
3583 * steps:
3584 * - wait for resync to reach a certain point
3585 * - suspend io to the following section
3586 * - backup that section
3587 * - allow resync to proceed further
3588 * - resume io
3589 * - discard the backup.
3590 *
3591 * When are combined in slightly different ways in the three cases.
3592 * Grow:
3593 * - suspend/backup/allow/wait/resume/discard
3594 * Shrink:
3595 * - allow/wait/suspend/backup/allow/wait/resume/discard
3596 * same-size:
3597 * - wait/resume/discard/suspend/backup/allow
3598 *
3599 * suspend/backup/allow always come together
3600 * wait/resume/discard do too.
3601 * For the same-size case we have two backups to improve flow.
24daa16f 3602 *
7236ee7a 3603 */
e86c9dd6 3604
7443ee81
N
3605int progress_reshape(struct mdinfo *info, struct reshape *reshape,
3606 unsigned long long backup_point,
3607 unsigned long long wait_point,
3608 unsigned long long *suspend_point,
a6b2d86c 3609 unsigned long long *reshape_completed, int *frozen)
7443ee81
N
3610{
3611 /* This function is called repeatedly by the reshape manager.
3612 * It determines how much progress can safely be made and allows
3613 * that progress.
3614 * - 'info' identifies the array and particularly records in
3615 * ->reshape_progress the metadata's knowledge of progress
3616 * This is a sector offset from the start of the array
3617 * of the next array block to be relocated. This number
3618 * may increase from 0 or decrease from array_size, depending
3619 * on the type of reshape that is happening.
3620 * Note that in contrast, 'sync_completed' is a block count of the
3621 * reshape so far. It gives the distance between the start point
3622 * (head or tail of device) and the next place that data will be
3623 * written. It always increases.
3624 * - 'reshape' is the structure created by analyse_change
3625 * - 'backup_point' shows how much the metadata manager has backed-up
3626 * data. For reshapes with increasing progress, it is the next address
3627 * to be backed up, previous addresses have been backed-up. For
3628 * decreasing progress, it is the earliest address that has been
3629 * backed up - later address are also backed up.
3630 * So addresses between reshape_progress and backup_point are
3631 * backed up providing those are in the 'correct' order.
3632 * - 'wait_point' is an array address. When reshape_completed
3633 * passes this point, progress_reshape should return. It might
3634 * return earlier if it determines that ->reshape_progress needs
3635 * to be updated or further backup is needed.
3636 * - suspend_point is maintained by progress_reshape and the caller
3637 * should not touch it except to initialise to zero.
3638 * It is an array address and it only increases in 2.6.37 and earlier.
b6b95155 3639 * This makes it difficult to handle reducing reshapes with
7443ee81
N
3640 * external metadata.
3641 * However: it is similar to backup_point in that it records the
3642 * other end of a suspended region from reshape_progress.
3643 * it is moved to extend the region that is safe to backup and/or
3644 * reshape
3645 * - reshape_completed is read from sysfs and returned. The caller
3646 * should copy this into ->reshape_progress when it has reason to
3647 * believe that the metadata knows this, and any backup outside this
3648 * has been erased.
3649 *
3650 * Return value is:
3651 * 1 if more data from backup_point - but only as far as suspend_point,
3652 * should be backed up
3653 * 0 if things are progressing smoothly
9e034f70
N
3654 * -1 if the reshape is finished because it is all done,
3655 * -2 if the reshape is finished due to an error.
7443ee81
N
3656 */
3657
3658 int advancing = (reshape->after.data_disks
3659 >= reshape->before.data_disks);
38dad34a
N
3660 unsigned long long need_backup; /* All data between start of array and
3661 * here will at some point need to
3662 * be backed up.
d0ab945e 3663 */
7443ee81 3664 unsigned long long read_offset, write_offset;
b4f8e38b 3665 unsigned long long write_range;
7443ee81 3666 unsigned long long max_progress, target, completed;
d0ab945e
N
3667 unsigned long long array_size = (info->component_size
3668 * reshape->before.data_disks);
7443ee81 3669 int fd;
77a73a17 3670 char buf[20];
7443ee81
N
3671
3672 /* First, we unsuspend any region that is now known to be safe.
3673 * If suspend_point is on the 'wrong' side of reshape_progress, then
3674 * we don't have or need suspension at the moment. This is true for
3675 * native metadata when we don't need to back-up.
3676 */
3677 if (advancing) {
49cd738b 3678 if (info->reshape_progress <= *suspend_point)
7443ee81
N
3679 sysfs_set_num(info, NULL, "suspend_lo",
3680 info->reshape_progress);
3681 } else {
3682 /* Note: this won't work in 2.6.37 and before.
3683 * Something somewhere should make sure we don't need it!
3684 */
49cd738b 3685 if (info->reshape_progress >= *suspend_point)
7443ee81
N
3686 sysfs_set_num(info, NULL, "suspend_hi",
3687 info->reshape_progress);
3688 }
3689
3690 /* Now work out how far it is safe to progress.
3691 * If the read_offset for ->reshape_progress is less than
3692 * 'blocks' beyond the write_offset, we can only progress as far
3693 * as a backup.
3694 * Otherwise we can progress until the write_offset for the new location
3695 * reaches (within 'blocks' of) the read_offset at the current location.
3696 * However that region must be suspended unless we are using native
3697 * metadata.
3698 * If we need to suspend more, we limit it to 128M per device, which is
3699 * rather arbitrary and should be some time-based calculation.
3700 */
ec757320
N
3701 read_offset = info->reshape_progress / reshape->before.data_disks;
3702 write_offset = info->reshape_progress / reshape->after.data_disks;
b4f8e38b 3703 write_range = info->new_chunk/512;
38dad34a
N
3704 if (reshape->before.data_disks == reshape->after.data_disks)
3705 need_backup = array_size;
3706 else
3707 need_backup = reshape->backup_blocks;
7443ee81 3708 if (advancing) {
38dad34a 3709 if (read_offset < write_offset + write_range)
7443ee81 3710 max_progress = backup_point;
ddee071d 3711 else
7443ee81 3712 max_progress =
2f3dd5e4
N
3713 read_offset *
3714 reshape->after.data_disks;
7443ee81 3715 } else {
38dad34a
N
3716 if (read_offset > write_offset - write_range)
3717 /* Can only progress as far as has been backed up,
3718 * which must be suspended */
7443ee81 3719 max_progress = backup_point;
38dad34a
N
3720 else if (info->reshape_progress <= need_backup)
3721 max_progress = backup_point;
3722 else {
3723 if (info->array.major_version >= 0)
3724 /* Can progress until backup is needed */
3725 max_progress = need_backup;
3726 else {
3727 /* Can progress until metadata update is required */
3728 max_progress =
3729 read_offset *
3730 reshape->after.data_disks;
3731 /* but data must be suspended */
3732 if (max_progress < *suspend_point)
3733 max_progress = *suspend_point;
3734 }
7443ee81
N
3735 }
3736 }
3737
3738 /* We know it is safe to progress to 'max_progress' providing
3739 * it is suspended or we are using native metadata.
3740 * Consider extending suspend_point 128M per device if it
3741 * is less than 64M per device beyond reshape_progress.
3742 * But always do a multiple of 'blocks'
832b2b9a
N
3743 * FIXME this is too big - it takes to long to complete
3744 * this much.
7443ee81
N
3745 */
3746 target = 64*1024*2 * min(reshape->before.data_disks,
24daa16f 3747 reshape->after.data_disks);
b4f8e38b 3748 target /= reshape->backup_blocks;
7443ee81
N
3749 if (target < 2)
3750 target = 2;
b4f8e38b 3751 target *= reshape->backup_blocks;
7443ee81
N
3752
3753 /* For externally managed metadata we always need to suspend IO to
3754 * the area being reshaped so we regularly push suspend_point forward.
3755 * For native metadata we only need the suspend if we are going to do
3756 * a backup.
3757 */
3758 if (advancing) {
d0ab945e
N
3759 if ((need_backup > info->reshape_progress
3760 || info->array.major_version < 0) &&
7443ee81 3761 *suspend_point < info->reshape_progress + target) {
d0ab945e
N
3762 if (need_backup < *suspend_point + 2 * target)
3763 *suspend_point = need_backup;
3764 else if (*suspend_point + 2 * target < array_size)
7443ee81 3765 *suspend_point += 2 * target;
d0ab945e
N
3766 else
3767 *suspend_point = array_size;
7443ee81 3768 sysfs_set_num(info, NULL, "suspend_hi", *suspend_point);
d0ab945e
N
3769 if (max_progress > *suspend_point)
3770 max_progress = *suspend_point;
7443ee81
N
3771 }
3772 } else {
38dad34a
N
3773 if (info->array.major_version >= 0) {
3774 /* Only need to suspend when about to backup */
3775 if (info->reshape_progress < need_backup * 2 &&
3776 *suspend_point > 0) {
d0ab945e 3777 *suspend_point = 0;
38dad34a
N
3778 sysfs_set_num(info, NULL, "suspend_lo", 0);
3779 sysfs_set_num(info, NULL, "suspend_hi", need_backup);
3780 }
3781 } else {
3782 /* Need to suspend continually */
3783 if (info->reshape_progress < *suspend_point)
3784 *suspend_point = info->reshape_progress;
3785 if (*suspend_point + target < info->reshape_progress)
3786 /* No need to move suspend region yet */;
3787 else {
3788 if (*suspend_point >= 2 * target)
3789 *suspend_point -= 2 * target;
3790 else
3791 *suspend_point = 0;
3792 sysfs_set_num(info, NULL, "suspend_lo",
3793 *suspend_point);
3794 }
d0ab945e
N
3795 if (max_progress < *suspend_point)
3796 max_progress = *suspend_point;
7443ee81
N
3797 }
3798 }
3799
3800 /* now set sync_max to allow that progress. sync_max, like
3801 * sync_completed is a count of sectors written per device, so
3802 * we find the difference between max_progress and the start point,
3803 * and divide that by after.data_disks to get a sync_max
3804 * number.
3805 * At the same time we convert wait_point to a similar number
3806 * for comparing against sync_completed.
3807 */
92d1991f 3808 /* scale down max_progress to per_disk */
7443ee81 3809 max_progress /= reshape->after.data_disks;
92d1991f
N
3810 /* Round to chunk size as some kernels give an erroneously high number */
3811 max_progress /= info->new_chunk/512;
3812 max_progress *= info->new_chunk/512;
7f913e9b
N
3813 /* And round to old chunk size as the kernel wants that */
3814 max_progress /= info->array.chunk_size/512;
3815 max_progress *= info->array.chunk_size/512;
92d1991f
N
3816 /* Limit progress to the whole device */
3817 if (max_progress > info->component_size)
3818 max_progress = info->component_size;
7443ee81 3819 wait_point /= reshape->after.data_disks;
92d1991f
N
3820 if (!advancing) {
3821 /* switch from 'device offset' to 'processed block count' */
3822 max_progress = info->component_size - max_progress;
3823 wait_point = info->component_size - wait_point;
3824 }
7443ee81 3825
a6b2d86c
N
3826 if (!*frozen)
3827 sysfs_set_num(info, NULL, "sync_max", max_progress);
7443ee81
N
3828
3829 /* Now wait. If we have already reached the point that we were
3830 * asked to wait to, don't wait at all, else wait for any change.
3831 * We need to select on 'sync_completed' as that is the place that
3832 * notifications happen, but we are really interested in
3833 * 'reshape_position'
3834 */
3835 fd = sysfs_get_fd(info, NULL, "sync_completed");
3836 if (fd < 0)
77a73a17 3837 goto check_progress;
7443ee81 3838
621ea11b 3839 if (sysfs_fd_get_ll(fd, &completed) < 0)
77a73a17 3840 goto check_progress;
621ea11b 3841
7443ee81
N
3842 while (completed < max_progress && completed < wait_point) {
3843 /* Check that sync_action is still 'reshape' to avoid
3844 * waiting forever on a dead array
3845 */
3846 char action[20];
7443ee81
N
3847 if (sysfs_get_str(info, NULL, "sync_action",
3848 action, 20) <= 0 ||
3849 strncmp(action, "reshape", 7) != 0)
3850 break;
26d6e157
AK
3851 /* Some kernels reset 'sync_completed' to zero
3852 * before setting 'sync_action' to 'idle'.
3853 * So we need these extra tests.
3854 */
3855 if (completed == 0 && advancing
3ee556f8 3856 && strncmp(action, "idle", 4) == 0
26d6e157
AK
3857 && info->reshape_progress > 0)
3858 break;
3859 if (completed == 0 && !advancing
3ee556f8 3860 && strncmp(action, "idle", 4) == 0
26d6e157
AK
3861 && info->reshape_progress < (info->component_size
3862 * reshape->after.data_disks))
3863 break;
efc67e8e 3864 sysfs_wait(fd, NULL);
621ea11b 3865 if (sysfs_fd_get_ll(fd, &completed) < 0)
77a73a17 3866 goto check_progress;
7443ee81 3867 }
10d0d365 3868 /* Some kernels reset 'sync_completed' to zero,
e0184a0c 3869 * we need to have real point we are in md.
e0cc1c8d 3870 * So in that case, read 'reshape_position' from sysfs.
10d0d365 3871 */
e0184a0c 3872 if (completed == 0) {
e0cc1c8d 3873 unsigned long long reshapep;
e0184a0c
N
3874 char action[20];
3875 if (sysfs_get_str(info, NULL, "sync_action",
3876 action, 20) > 0 &&
e0cc1c8d
N
3877 strncmp(action, "idle", 4) == 0 &&
3878 sysfs_get_ll(info, NULL,
3879 "reshape_position", &reshapep) == 0)
3880 *reshape_completed = reshapep;
3881 } else {
3882 /* some kernels can give an incorrectly high
3883 * 'completed' number, so round down */
3884 completed /= (info->new_chunk/512);
3885 completed *= (info->new_chunk/512);
3886 /* Convert 'completed' back in to a 'progress' number */
3887 completed *= reshape->after.data_disks;
3888 if (!advancing)
3889 completed = (info->component_size
3890 * reshape->after.data_disks
3891 - completed);
3892 *reshape_completed = completed;
7443ee81 3893 }
24daa16f 3894
7443ee81
N
3895 close(fd);
3896
3897 /* We return the need_backup flag. Caller will decide
d0ab945e 3898 * how much - a multiple of ->backup_blocks up to *suspend_point
7443ee81 3899 */
38dad34a
N
3900 if (advancing)
3901 return need_backup > info->reshape_progress;
3902 else
3903 return need_backup >= info->reshape_progress;
77a73a17
N
3904
3905check_progress:
3906 /* if we couldn't read a number from sync_completed, then
3907 * either the reshape did complete, or it aborted.
3908 * We can tell which by checking for 'none' in reshape_position.
621ea11b
N
3909 * If it did abort, then it might immediately restart if it
3910 * it was just a device failure that leaves us degraded but
3911 * functioning.
77a73a17 3912 */
77a73a17 3913 if (sysfs_get_str(info, NULL, "reshape_position", buf, sizeof(buf)) < 0
621ea11b
N
3914 || strncmp(buf, "none", 4) != 0) {
3915 /* The abort might only be temporary. Wait up to 10
3916 * seconds for fd to contain a valid number again.
3917 */
efc67e8e 3918 int wait = 10000;
621ea11b 3919 int rv = -2;
a6b2d86c 3920 unsigned long long new_sync_max;
efc67e8e
N
3921 while (fd >= 0 && rv < 0 && wait > 0) {
3922 if (sysfs_wait(fd, &wait) != 1)
621ea11b 3923 break;
6560987b
N
3924 switch (sysfs_fd_get_ll(fd, &completed)) {
3925 case 0:
621ea11b
N
3926 /* all good again */
3927 rv = 1;
a6b2d86c
N
3928 /* If "sync_max" is no longer max_progress
3929 * we need to freeze things
3930 */
3931 sysfs_get_ll(info, NULL, "sync_max", &new_sync_max);
3932 *frozen = (new_sync_max != max_progress);
6560987b
N
3933 break;
3934 case -2: /* read error - abort */
efc67e8e 3935 wait = 0;
6560987b
N
3936 break;
3937 }
621ea11b
N
3938 }
3939 if (fd >= 0)
3940 close(fd);
3941 return rv; /* abort */
3942 } else {
6d5316f6 3943 /* Maybe racing with array shutdown - check state */
621ea11b
N
3944 if (fd >= 0)
3945 close(fd);
6d5316f6
N
3946 if (sysfs_get_str(info, NULL, "array_state", buf, sizeof(buf)) < 0
3947 || strncmp(buf, "inactive", 8) == 0
3948 || strncmp(buf, "clear",5) == 0)
3949 return -2; /* abort */
77a73a17 3950 return -1; /* complete */
6d5316f6 3951 }
7443ee81
N
3952}
3953
fcf57625 3954/* FIXME return status is never checked */
4411fb17 3955static int grow_backup(struct mdinfo *sra,
7236ee7a 3956 unsigned long long offset, /* per device */
7443ee81 3957 unsigned long stripes, /* per device, in old chunks */
7236ee7a
N
3958 int *sources, unsigned long long *offsets,
3959 int disks, int chunk, int level, int layout,
3960 int dests, int *destfd, unsigned long long *destoffsets,
d4445387 3961 int part, int *degraded,
7236ee7a
N
3962 char *buf)
3963{
3964 /* Backup 'blocks' sectors at 'offset' on each device of the array,
3965 * to storage 'destfd' (offset 'destoffsets'), after first
3966 * suspending IO. Then allow resync to continue
3967 * over the suspended section.
3968 * Use part 'part' of the backup-super-block.
3969 */
3970 int odata = disks;
3971 int rv = 0;
3972 int i;
f21e18ca
N
3973 unsigned long long ll;
3974 int new_degraded;
7236ee7a
N
3975 //printf("offset %llu\n", offset);
3976 if (level >= 4)
3977 odata--;
3978 if (level == 6)
3979 odata--;
7443ee81 3980
d4445387 3981 /* Check that array hasn't become degraded, else we might backup the wrong data */
2c6ac128
N
3982 if (sysfs_get_ll(sra, NULL, "degraded", &ll) < 0)
3983 return -1; /* FIXME this error is ignored */
f21e18ca 3984 new_degraded = (int)ll;
d4445387
N
3985 if (new_degraded != *degraded) {
3986 /* check each device to ensure it is still working */
3987 struct mdinfo *sd;
3988 for (sd = sra->devs ; sd ; sd = sd->next) {
3989 if (sd->disk.state & (1<<MD_DISK_FAULTY))
3990 continue;
3991 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
3992 char sbuf[20];
3993 if (sysfs_get_str(sra, sd, "state", sbuf, 20) < 0 ||
3994 strstr(sbuf, "faulty") ||
3995 strstr(sbuf, "in_sync") == NULL) {
3996 /* this device is dead */
3997 sd->disk.state = (1<<MD_DISK_FAULTY);
3998 if (sd->disk.raid_disk >= 0 &&
3999 sources[sd->disk.raid_disk] >= 0) {
4000 close(sources[sd->disk.raid_disk]);
4001 sources[sd->disk.raid_disk] = -1;
4002 }
4003 }
4004 }
4005 }
4006 *degraded = new_degraded;
4007 }
7236ee7a
N
4008 if (part) {
4009 bsb.arraystart2 = __cpu_to_le64(offset * odata);
200871ad 4010 bsb.length2 = __cpu_to_le64(stripes * (chunk/512) * odata);
7236ee7a
N
4011 } else {
4012 bsb.arraystart = __cpu_to_le64(offset * odata);
200871ad 4013 bsb.length = __cpu_to_le64(stripes * (chunk/512) * odata);
7236ee7a
N
4014 }
4015 if (part)
4016 bsb.magic[15] = '2';
4017 for (i = 0; i < dests; i++)
4018 if (part)
4019 lseek64(destfd[i], destoffsets[i] + __le64_to_cpu(bsb.devstart2)*512, 0);
4020 else
4021 lseek64(destfd[i], destoffsets[i], 0);
4022
24daa16f 4023 rv = save_stripes(sources, offsets,
7236ee7a
N
4024 disks, chunk, level, layout,
4025 dests, destfd,
4026 offset*512*odata, stripes * chunk * odata,
4027 buf);
4028
4029 if (rv)
4030 return rv;
97396422 4031 bsb.mtime = __cpu_to_le64(time(0));
7236ee7a
N
4032 for (i = 0; i < dests; i++) {
4033 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
4034
4035 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
4036 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
4037 bsb.sb_csum2 = bsb_csum((char*)&bsb,
4038 ((char*)&bsb.sb_csum2)-((char*)&bsb));
4039
72044953 4040 rv = -1;
f21e18ca
N
4041 if ((unsigned long long)lseek64(destfd[i], destoffsets[i] - 4096, 0)
4042 != destoffsets[i] - 4096)
72044953
N
4043 break;
4044 if (write(destfd[i], &bsb, 512) != 512)
4045 break;
ff94fb86 4046 if (destoffsets[i] > 4096) {
f21e18ca 4047 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]+stripes*chunk*odata, 0) !=
a847575a 4048 destoffsets[i]+stripes*chunk*odata)
72044953
N
4049 break;
4050 if (write(destfd[i], &bsb, 512) != 512)
4051 break;
ff94fb86 4052 }
7236ee7a 4053 fsync(destfd[i]);
72044953 4054 rv = 0;
7236ee7a 4055 }
2efedc7b 4056
fcf57625 4057 return rv;
7236ee7a
N
4058}
4059
4060/* in 2.6.30, the value reported by sync_completed can be
4061 * less that it should be by one stripe.
4062 * This only happens when reshape hits sync_max and pauses.
4063 * So allow wait_backup to either extent sync_max further
4064 * than strictly necessary, or return before the
4065 * sync has got quite as far as we would really like.
4066 * This is what 'blocks2' is for.
4067 * The various caller give appropriate values so that
4068 * every works.
4069 */
fcf57625 4070/* FIXME return value is often ignored */
24daa16f
N
4071static int forget_backup(int dests, int *destfd,
4072 unsigned long long *destoffsets,
4073 int part)
7236ee7a 4074{
24daa16f 4075 /*
7443ee81 4076 * Erase backup 'part' (which is 0 or 1)
7236ee7a 4077 */
7236ee7a 4078 int i;
fcf57625 4079 int rv;
7236ee7a 4080
7236ee7a
N
4081 if (part) {
4082 bsb.arraystart2 = __cpu_to_le64(0);
4083 bsb.length2 = __cpu_to_le64(0);
4084 } else {
4085 bsb.arraystart = __cpu_to_le64(0);
4086 bsb.length = __cpu_to_le64(0);
4087 }
97396422 4088 bsb.mtime = __cpu_to_le64(time(0));
fcf57625 4089 rv = 0;
7236ee7a
N
4090 for (i = 0; i < dests; i++) {
4091 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
4092 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
4093 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
4094 bsb.sb_csum2 = bsb_csum((char*)&bsb,
4095 ((char*)&bsb.sb_csum2)-((char*)&bsb));
f21e18ca 4096 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]-4096, 0) !=
a847575a 4097 destoffsets[i]-4096)
72044953 4098 rv = -1;
24daa16f 4099 if (rv == 0 &&
72044953
N
4100 write(destfd[i], &bsb, 512) != 512)
4101 rv = -1;
7236ee7a
N
4102 fsync(destfd[i]);
4103 }
fcf57625 4104 return rv;
7236ee7a 4105}
e86c9dd6 4106
7236ee7a
N
4107static void fail(char *msg)
4108{
fcf57625 4109 int rv;
72044953
N
4110 rv = (write(2, msg, strlen(msg)) != (int)strlen(msg));
4111 rv |= (write(2, "\n", 1) != 1);
fcf57625 4112 exit(rv ? 1 : 2);
7236ee7a
N
4113}
4114
4115static char *abuf, *bbuf;
f21e18ca 4116static unsigned long long abuflen;
7236ee7a
N
4117static void validate(int afd, int bfd, unsigned long long offset)
4118{
4119 /* check that the data in the backup against the array.
4120 * This is only used for regression testing and should not
4121 * be used while the array is active
4122 */
7236ee7a
N
4123 if (afd < 0)
4124 return;
4125 lseek64(bfd, offset - 4096, 0);
4126 if (read(bfd, &bsb2, 512) != 512)
4127 fail("cannot read bsb");
4128 if (bsb2.sb_csum != bsb_csum((char*)&bsb2,
4129 ((char*)&bsb2.sb_csum)-((char*)&bsb2)))
4130 fail("first csum bad");
4131 if (memcmp(bsb2.magic, "md_backup_data", 14) != 0)
4132 fail("magic is bad");
4133 if (memcmp(bsb2.magic, "md_backup_data-2", 16) == 0 &&
4134 bsb2.sb_csum2 != bsb_csum((char*)&bsb2,
24daa16f 4135 ((char*)&bsb2.sb_csum2)-((char*)&bsb2)))
7236ee7a
N
4136 fail("second csum bad");
4137
4138 if (__le64_to_cpu(bsb2.devstart)*512 != offset)
4139 fail("devstart is wrong");
4140
4141 if (bsb2.length) {
4142 unsigned long long len = __le64_to_cpu(bsb2.length)*512;
4143
4144 if (abuflen < len) {
4145 free(abuf);
4146 free(bbuf);
4147 abuflen = len;
fcf57625
N
4148 if (posix_memalign((void**)&abuf, 4096, abuflen) ||
4149 posix_memalign((void**)&bbuf, 4096, abuflen)) {
4150 abuflen = 0;
4151 /* just stop validating on mem-alloc failure */
4152 return;
4153 }
e86c9dd6 4154 }
48924014 4155
7236ee7a 4156 lseek64(bfd, offset, 0);
f21e18ca 4157 if ((unsigned long long)read(bfd, bbuf, len) != len) {
080fd005 4158 //printf("len %llu\n", len);
7236ee7a
N
4159 fail("read first backup failed");
4160 }
4161 lseek64(afd, __le64_to_cpu(bsb2.arraystart)*512, 0);
f21e18ca 4162 if ((unsigned long long)read(afd, abuf, len) != len)
7236ee7a
N
4163 fail("read first from array failed");
4164 if (memcmp(bbuf, abuf, len) != 0) {
24daa16f 4165#if 0
7236ee7a
N
4166 int i;
4167 printf("offset=%llu len=%llu\n",
080fd005 4168 (unsigned long long)__le64_to_cpu(bsb2.arraystart)*512, len);
7236ee7a
N
4169 for (i=0; i<len; i++)
4170 if (bbuf[i] != abuf[i]) {
4171 printf("first diff byte %d\n", i);
93ecfa01 4172 break;
7236ee7a 4173 }
24daa16f 4174#endif
7236ee7a 4175 fail("data1 compare failed");
e86c9dd6 4176 }
7236ee7a
N
4177 }
4178 if (bsb2.length2) {
4179 unsigned long long len = __le64_to_cpu(bsb2.length2)*512;
4180
4181 if (abuflen < len) {
4182 free(abuf);
4183 free(bbuf);
4184 abuflen = len;
503975b9
N
4185 abuf = xmalloc(abuflen);
4186 bbuf = xmalloc(abuflen);
e86c9dd6
NB
4187 }
4188
7236ee7a 4189 lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0);
f21e18ca 4190 if ((unsigned long long)read(bfd, bbuf, len) != len)
7236ee7a
N
4191 fail("read second backup failed");
4192 lseek64(afd, __le64_to_cpu(bsb2.arraystart2)*512, 0);
f21e18ca 4193 if ((unsigned long long)read(afd, abuf, len) != len)
7236ee7a
N
4194 fail("read second from array failed");
4195 if (memcmp(bbuf, abuf, len) != 0)
4196 fail("data2 compare failed");
e86c9dd6 4197 }
7236ee7a 4198}
e86c9dd6 4199
999b4972
N
4200int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
4201 struct supertype *st, unsigned long blocks,
4202 int *fds, unsigned long long *offsets,
4203 int dests, int *destfd, unsigned long long *destoffsets)
7236ee7a 4204{
7443ee81
N
4205 /* Monitor a reshape where backup is being performed using
4206 * 'native' mechanism - either to a backup file, or
4207 * to some space in a spare.
4208 */
7236ee7a 4209 char *buf;
7443ee81
N
4210 int degraded = -1;
4211 unsigned long long speed;
4212 unsigned long long suspend_point, array_size;
4213 unsigned long long backup_point, wait_point;
4214 unsigned long long reshape_completed;
4215 int done = 0;
4216 int increasing = reshape->after.data_disks >= reshape->before.data_disks;
4217 int part = 0; /* The next part of the backup area to fill. It may already
4218 * be full, so we need to check */
4219 int level = reshape->level;
4220 int layout = reshape->before.layout;
4221 int data = reshape->before.data_disks;
4222 int disks = reshape->before.data_disks + reshape->parity;
4223 int chunk = sra->array.chunk_size;
da8100ca
N
4224 struct mdinfo *sd;
4225 unsigned long stripes;
3b7e9d0c 4226 int uuid[4];
a6b2d86c 4227 int frozen = 0;
da8100ca
N
4228
4229 /* set up the backup-super-block. This requires the
4230 * uuid from the array.
4231 */
4232 /* Find a superblock */
4233 for (sd = sra->devs; sd; sd = sd->next) {
4234 char *dn;
4235 int devfd;
4236 int ok;
4237 if (sd->disk.state & (1<<MD_DISK_FAULTY))
4238 continue;
4239 dn = map_dev(sd->disk.major, sd->disk.minor, 1);
4240 devfd = dev_open(dn, O_RDONLY);
4241 if (devfd < 0)
4242 continue;
4243 ok = st->ss->load_super(st, devfd, NULL);
4244 close(devfd);
77f8d358 4245 if (ok == 0)
da8100ca
N
4246 break;
4247 }
4248 if (!sd) {
e7b84f9d 4249 pr_err("Cannot find a superblock\n");
da8100ca
N
4250 return 0;
4251 }
4252
4253 memset(&bsb, 0, 512);
4254 memcpy(bsb.magic, "md_backup_data-1", 16);
3b7e9d0c
LB
4255 st->ss->uuid_from_super(st, uuid);
4256 memcpy(bsb.set_uuid, uuid, 16);
da8100ca
N
4257 bsb.mtime = __cpu_to_le64(time(0));
4258 bsb.devstart2 = blocks;
4259
4260 stripes = blocks / (sra->array.chunk_size/512) /
4261 reshape->before.data_disks;
e86c9dd6 4262
fcf57625
N
4263 if (posix_memalign((void**)&buf, 4096, disks * chunk))
4264 /* Don't start the 'reshape' */
4265 return 0;
7443ee81
N
4266 if (reshape->before.data_disks == reshape->after.data_disks) {
4267 sysfs_get_ll(sra, NULL, "sync_speed_min", &speed);
4268 sysfs_set_num(sra, NULL, "sync_speed_min", 200000);
4269 }
e86c9dd6 4270
7443ee81 4271 if (increasing) {
96533072 4272 array_size = sra->component_size * reshape->after.data_disks;
7443ee81
N
4273 backup_point = sra->reshape_progress;
4274 suspend_point = 0;
4275 } else {
96533072 4276 array_size = sra->component_size * reshape->before.data_disks;
25da62d9 4277 backup_point = reshape->backup_blocks;
7443ee81
N
4278 suspend_point = array_size;
4279 }
7236ee7a 4280
7443ee81
N
4281 while (!done) {
4282 int rv;
7236ee7a 4283
7443ee81
N
4284 /* Want to return as soon the oldest backup slot can
4285 * be released as that allows us to start backing up
4286 * some more, providing suspend_point has been
b6b95155 4287 * advanced, which it should have.
7443ee81
N
4288 */
4289 if (increasing) {
4290 wait_point = array_size;
4291 if (part == 0 && __le64_to_cpu(bsb.length) > 0)
4292 wait_point = (__le64_to_cpu(bsb.arraystart) +
4293 __le64_to_cpu(bsb.length));
4294 if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
4295 wait_point = (__le64_to_cpu(bsb.arraystart2) +
4296 __le64_to_cpu(bsb.length2));
4297 } else {
4298 wait_point = 0;
4299 if (part == 0 && __le64_to_cpu(bsb.length) > 0)
4300 wait_point = __le64_to_cpu(bsb.arraystart);
4301 if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
4302 wait_point = __le64_to_cpu(bsb.arraystart2);
4303 }
4304
737f8574 4305 reshape_completed = sra->reshape_progress;
7443ee81
N
4306 rv = progress_reshape(sra, reshape,
4307 backup_point, wait_point,
a6b2d86c
N
4308 &suspend_point, &reshape_completed,
4309 &frozen);
7443ee81
N
4310 /* external metadata would need to ping_monitor here */
4311 sra->reshape_progress = reshape_completed;
7236ee7a 4312
7443ee81
N
4313 /* Clear any backup region that is before 'here' */
4314 if (increasing) {
b3cf095a
N
4315 if (__le64_to_cpu(bsb.length) > 0 &&
4316 reshape_completed >= (__le64_to_cpu(bsb.arraystart) +
7443ee81
N
4317 __le64_to_cpu(bsb.length)))
4318 forget_backup(dests, destfd,
4319 destoffsets, 0);
b3cf095a
N
4320 if (__le64_to_cpu(bsb.length2) > 0 &&
4321 reshape_completed >= (__le64_to_cpu(bsb.arraystart2) +
7443ee81
N
4322 __le64_to_cpu(bsb.length2)))
4323 forget_backup(dests, destfd,
4324 destoffsets, 1);
4325 } else {
b3cf095a
N
4326 if (__le64_to_cpu(bsb.length) > 0 &&
4327 reshape_completed <= (__le64_to_cpu(bsb.arraystart)))
7443ee81
N
4328 forget_backup(dests, destfd,
4329 destoffsets, 0);
b3cf095a
N
4330 if (__le64_to_cpu(bsb.length2) > 0 &&
4331 reshape_completed <= (__le64_to_cpu(bsb.arraystart2)))
7443ee81
N
4332 forget_backup(dests, destfd,
4333 destoffsets, 1);
4334 }
84d11e6c
N
4335 if (sigterm)
4336 rv = -2;
223c5c99 4337 if (rv < 0) {
77a73a17
N
4338 if (rv == -1)
4339 done = 1;
223c5c99
N
4340 break;
4341 }
2d4de5f9
N
4342 if (rv == 0 && increasing && !st->ss->external) {
4343 /* No longer need to monitor this reshape */
2cdd5ce0 4344 sysfs_set_str(sra, NULL, "sync_max", "max");
2d4de5f9
N
4345 done = 1;
4346 break;
4347 }
223c5c99 4348
60204e4e 4349 while (rv) {
7443ee81 4350 unsigned long long offset;
e97ca002 4351 unsigned long actual_stripes;
60204e4e
N
4352 /* Need to backup some data.
4353 * If 'part' is not used and the desired
4354 * backup size is suspended, do a backup,
4355 * then consider the next part.
4356 */
7443ee81
N
4357 /* Check that 'part' is unused */
4358 if (part == 0 && __le64_to_cpu(bsb.length) != 0)
60204e4e 4359 break;
7443ee81 4360 if (part == 1 && __le64_to_cpu(bsb.length2) != 0)
60204e4e 4361 break;
7443ee81
N
4362
4363 offset = backup_point / data;
e97ca002 4364 actual_stripes = stripes;
60204e4e 4365 if (increasing) {
e97ca002
N
4366 if (offset + actual_stripes * (chunk/512) >
4367 sra->component_size)
4368 actual_stripes = ((sra->component_size - offset)
4369 / (chunk/512));
4370 if (offset + actual_stripes * (chunk/512) >
60204e4e
N
4371 suspend_point/data)
4372 break;
4373 } else {
e97ca002
N
4374 if (offset < actual_stripes * (chunk/512))
4375 actual_stripes = offset / (chunk/512);
4376 offset -= actual_stripes * (chunk/512);
4377 if (offset < suspend_point/data)
60204e4e
N
4378 break;
4379 }
e4b11073
N
4380 if (actual_stripes == 0)
4381 break;
e97ca002 4382 grow_backup(sra, offset, actual_stripes,
7443ee81
N
4383 fds, offsets,
4384 disks, chunk, level, layout,
4385 dests, destfd, destoffsets,
4386 part, &degraded, buf);
4387 validate(afd, destfd[0], destoffsets[0]);
4388 /* record where 'part' is up to */
4389 part = !part;
4390 if (increasing)
e97ca002 4391 backup_point += actual_stripes * (chunk/512) * data;
7443ee81 4392 else
e97ca002 4393 backup_point -= actual_stripes * (chunk/512) * data;
7443ee81
N
4394 }
4395 }
4396
223c5c99 4397 /* FIXME maybe call progress_reshape one more time instead */
5e7be838
N
4398 /* remove any remaining suspension */
4399 sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
4400 sysfs_set_num(sra, NULL, "suspend_hi", 0);
4401 sysfs_set_num(sra, NULL, "suspend_lo", 0);
4402 sysfs_set_num(sra, NULL, "sync_min", 0);
4403
7443ee81
N
4404 if (reshape->before.data_disks == reshape->after.data_disks)
4405 sysfs_set_num(sra, NULL, "sync_speed_min", speed);
7236ee7a 4406 free(buf);
77a73a17 4407 return done;
e86c9dd6 4408}
353632d9
NB
4409
4410/*
4411 * If any spare contains md_back_data-1 which is recent wrt mtime,
4412 * write that data into the array and update the super blocks with
4413 * the new reshape_progress
4414 */
ea0ebe96
N
4415int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt,
4416 char *backup_file, int verbose)
353632d9
NB
4417{
4418 int i, j;
4419 int old_disks;
353632d9 4420 unsigned long long *offsets;
82f2d6ab 4421 unsigned long long nstripe, ostripe;
6e9eac4f 4422 int ndata, odata;
353632d9 4423
e9e43ec3
N
4424 odata = info->array.raid_disks - info->delta_disks - 1;
4425 if (info->array.level == 6) odata--; /* number of data disks */
4426 ndata = info->array.raid_disks - 1;
4427 if (info->new_level == 6) ndata--;
353632d9
NB
4428
4429 old_disks = info->array.raid_disks - info->delta_disks;
4430
e9e43ec3
N
4431 if (info->delta_disks <= 0)
4432 /* Didn't grow, so the backup file must have
4433 * been used
4434 */
4435 old_disks = cnt;
06b0d786 4436 for (i=old_disks-(backup_file?1:0); i<cnt; i++) {
353632d9 4437 struct mdinfo dinfo;
06b0d786 4438 int fd;
e9e43ec3 4439 int bsbsize;
ea0ebe96 4440 char *devname, namebuf[20];
10af14c4 4441 unsigned long long lo, hi;
353632d9
NB
4442
4443 /* This was a spare and may have some saved data on it.
4444 * Load the superblock, find and load the
4445 * backup_super_block.
4446 * If either fail, go on to next device.
4447 * If the backup contains no new info, just return
206c5eae 4448 * else restore data and update all superblocks
353632d9 4449 */
06b0d786
NB
4450 if (i == old_disks-1) {
4451 fd = open(backup_file, O_RDONLY);
e9e43ec3 4452 if (fd<0) {
e7b84f9d 4453 pr_err("backup file %s inaccessible: %s\n",
e9e43ec3 4454 backup_file, strerror(errno));
06b0d786 4455 continue;
e9e43ec3 4456 }
ea0ebe96 4457 devname = backup_file;
06b0d786
NB
4458 } else {
4459 fd = fdlist[i];
4460 if (fd < 0)
4461 continue;
3da92f27 4462 if (st->ss->load_super(st, fd, NULL))
06b0d786 4463 continue;
353632d9 4464
a5d85af7 4465 st->ss->getinfo_super(st, &dinfo, NULL);
3da92f27
NB
4466 st->ss->free_super(st);
4467
06b0d786
NB
4468 if (lseek64(fd,
4469 (dinfo.data_offset + dinfo.component_size - 8) <<9,
ea0ebe96 4470 0) < 0) {
e7b84f9d 4471 pr_err("Cannot seek on device %d\n", i);
06b0d786 4472 continue; /* Cannot seek */
ea0ebe96
N
4473 }
4474 sprintf(namebuf, "device-%d", i);
4475 devname = namebuf;
06b0d786 4476 }
ea0ebe96
N
4477 if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb)) {
4478 if (verbose)
e7b84f9d 4479 pr_err("Cannot read from %s\n", devname);
353632d9 4480 continue; /* Cannot read */
ea0ebe96 4481 }
e9e43ec3 4482 if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0 &&
ea0ebe96
N
4483 memcmp(bsb.magic, "md_backup_data-2", 16) != 0) {
4484 if (verbose)
e7b84f9d 4485 pr_err("No backup metadata on %s\n", devname);
353632d9 4486 continue;
ea0ebe96
N
4487 }
4488 if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb))) {
4489 if (verbose)
e7b84f9d 4490 pr_err("Bad backup-metadata checksum on %s\n", devname);
353632d9 4491 continue; /* bad checksum */
ea0ebe96 4492 }
e9e43ec3 4493 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0 &&
ea0ebe96
N
4494 bsb.sb_csum2 != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum2)-((char*)&bsb))) {
4495 if (verbose)
e7b84f9d 4496 pr_err("Bad backup-metadata checksum2 on %s\n", devname);
22e30516 4497 continue; /* Bad second checksum */
ea0ebe96
N
4498 }
4499 if (memcmp(bsb.set_uuid,info->uuid, 16) != 0) {
4500 if (verbose)
e7b84f9d 4501 pr_err("Wrong uuid on backup-metadata on %s\n", devname);
353632d9 4502 continue; /* Wrong uuid */
ea0ebe96 4503 }
353632d9 4504
097075b6
N
4505 /* array utime and backup-mtime should be updated at much the same time, but it seems that
4506 * sometimes they aren't... So allow considerable flexability in matching, and allow
4507 * this test to be overridden by an environment variable.
4508 */
26714713
DD
4509 if(time_after(info->array.utime, (unsigned int)__le64_to_cpu(bsb.mtime) + 2*60*60) ||
4510 time_before(info->array.utime, (unsigned int)__le64_to_cpu(bsb.mtime) - 10*60)) {
097075b6 4511 if (check_env("MDADM_GROW_ALLOW_OLD")) {
7a862a02 4512 pr_err("accepting backup with timestamp %lu for array with timestamp %lu\n",
097075b6
N
4513 (unsigned long)__le64_to_cpu(bsb.mtime),
4514 (unsigned long)info->array.utime);
4515 } else {
676ab312
N
4516 pr_err("too-old timestamp on backup-metadata on %s\n", devname);
4517 pr_err("If you think it is should be safe, try 'export MDADM_GROW_ALLOW_OLD=1'\n");
097075b6
N
4518 continue; /* time stamp is too bad */
4519 }
ea0ebe96 4520 }
353632d9 4521
e9e43ec3 4522 if (bsb.magic[15] == '1') {
10af14c4
N
4523 if (bsb.length == 0)
4524 continue;
e7a71c6b
N
4525 if (info->delta_disks >= 0) {
4526 /* reshape_progress is increasing */
4527 if (__le64_to_cpu(bsb.arraystart)
4528 + __le64_to_cpu(bsb.length)
4529 < info->reshape_progress) {
4530 nonew:
4531 if (verbose)
e7b84f9d 4532 pr_err("backup-metadata found on %s but is not needed\n", devname);
e7a71c6b
N
4533 continue; /* No new data here */
4534 }
4535 } else {
4536 /* reshape_progress is decreasing */
4537 if (__le64_to_cpu(bsb.arraystart) >=
4538 info->reshape_progress)
4539 goto nonew; /* No new data here */
ea0ebe96 4540 }
e9e43ec3 4541 } else {
10af14c4
N
4542 if (bsb.length == 0 && bsb.length2 == 0)
4543 continue;
e7a71c6b
N
4544 if (info->delta_disks >= 0) {
4545 /* reshape_progress is increasing */
4546 if ((__le64_to_cpu(bsb.arraystart)
4547 + __le64_to_cpu(bsb.length)
4548 < info->reshape_progress)
4549 &&
4550 (__le64_to_cpu(bsb.arraystart2)
4551 + __le64_to_cpu(bsb.length2)
4552 < info->reshape_progress))
4553 goto nonew; /* No new data here */
4554 } else {
4555 /* reshape_progress is decreasing */
4556 if (__le64_to_cpu(bsb.arraystart) >=
4557 info->reshape_progress &&
4558 __le64_to_cpu(bsb.arraystart2) >=
4559 info->reshape_progress)
4560 goto nonew; /* No new data here */
4561 }
e9e43ec3 4562 }
ea0ebe96
N
4563 if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0) {
4564 second_fail:
4565 if (verbose)
e7b84f9d
N
4566 pr_err("Failed to verify secondary backup-metadata block on %s\n",
4567 devname);
353632d9 4568 continue; /* Cannot seek */
ea0ebe96 4569 }
2efedc7b 4570 /* There should be a duplicate backup superblock 4k before here */
06b0d786 4571 if (lseek64(fd, -4096, 1) < 0 ||
0155af90 4572 read(fd, &bsb2, sizeof(bsb2)) != sizeof(bsb2))
ea0ebe96 4573 goto second_fail; /* Cannot find leading superblock */
e9e43ec3
N
4574 if (bsb.magic[15] == '1')
4575 bsbsize = offsetof(struct mdp_backup_super, pad1);
4576 else
4577 bsbsize = offsetof(struct mdp_backup_super, pad);
ff94fb86 4578 if (memcmp(&bsb2, &bsb, bsbsize) != 0)
ea0ebe96 4579 goto second_fail; /* Cannot find leading superblock */
2efedc7b 4580
353632d9 4581 /* Now need the data offsets for all devices. */
503975b9 4582 offsets = xmalloc(sizeof(*offsets)*info->array.raid_disks);
353632d9
NB
4583 for(j=0; j<info->array.raid_disks; j++) {
4584 if (fdlist[j] < 0)
4585 continue;
3da92f27 4586 if (st->ss->load_super(st, fdlist[j], NULL))
353632d9
NB
4587 /* FIXME should be this be an error */
4588 continue;
a5d85af7 4589 st->ss->getinfo_super(st, &dinfo, NULL);
3da92f27 4590 st->ss->free_super(st);
14e5b4d7 4591 offsets[j] = dinfo.data_offset * 512;
353632d9 4592 }
d56dd607 4593 printf("%s: restoring critical section\n", Name);
353632d9
NB
4594
4595 if (restore_stripes(fdlist, offsets,
4596 info->array.raid_disks,
4597 info->new_chunk,
4598 info->new_level,
4599 info->new_layout,
06b0d786 4600 fd, __le64_to_cpu(bsb.devstart)*512,
92dcdf7c 4601 __le64_to_cpu(bsb.arraystart)*512,
2fcb75ae 4602 __le64_to_cpu(bsb.length)*512, NULL)) {
e9e43ec3 4603 /* didn't succeed, so giveup */
ea0ebe96 4604 if (verbose)
e7b84f9d 4605 pr_err("Error restoring backup from %s\n",
ea0ebe96 4606 devname);
730ae51f 4607 free(offsets);
e9e43ec3
N
4608 return 1;
4609 }
24daa16f 4610
e9e43ec3
N
4611 if (bsb.magic[15] == '2' &&
4612 restore_stripes(fdlist, offsets,
4613 info->array.raid_disks,
4614 info->new_chunk,
4615 info->new_level,
4616 info->new_layout,
4617 fd, __le64_to_cpu(bsb.devstart)*512 +
4618 __le64_to_cpu(bsb.devstart2)*512,
92dcdf7c 4619 __le64_to_cpu(bsb.arraystart2)*512,
2fcb75ae 4620 __le64_to_cpu(bsb.length2)*512, NULL)) {
353632d9 4621 /* didn't succeed, so giveup */
ea0ebe96 4622 if (verbose)
e7b84f9d 4623 pr_err("Error restoring second backup from %s\n",
ea0ebe96 4624 devname);
730ae51f 4625 free(offsets);
2295250a 4626 return 1;
353632d9
NB
4627 }
4628
730ae51f 4629 free(offsets);
e9e43ec3 4630
353632d9
NB
4631 /* Ok, so the data is restored. Let's update those superblocks. */
4632
10af14c4
N
4633 lo = hi = 0;
4634 if (bsb.length) {
4635 lo = __le64_to_cpu(bsb.arraystart);
4636 hi = lo + __le64_to_cpu(bsb.length);
4637 }
4638 if (bsb.magic[15] == '2' && bsb.length2) {
4639 unsigned long long lo1, hi1;
4640 lo1 = __le64_to_cpu(bsb.arraystart2);
4641 hi1 = lo1 + __le64_to_cpu(bsb.length2);
4642 if (lo == hi) {
4643 lo = lo1;
4644 hi = hi1;
4645 } else if (lo < lo1)
4646 hi = hi1;
4647 else
4648 lo = lo1;
4649 }
4650 if (lo < hi &&
4651 (info->reshape_progress < lo ||
4652 info->reshape_progress > hi))
4653 /* backup does not affect reshape_progress*/ ;
4654 else if (info->delta_disks >= 0) {
e9e43ec3
N
4655 info->reshape_progress = __le64_to_cpu(bsb.arraystart) +
4656 __le64_to_cpu(bsb.length);
4657 if (bsb.magic[15] == '2') {
4658 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2) +
4659 __le64_to_cpu(bsb.length2);
4660 if (p2 > info->reshape_progress)
4661 info->reshape_progress = p2;
4662 }
4663 } else {
4664 info->reshape_progress = __le64_to_cpu(bsb.arraystart);
4665 if (bsb.magic[15] == '2') {
4666 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2);
4667 if (p2 < info->reshape_progress)
4668 info->reshape_progress = p2;
4669 }
4670 }
353632d9 4671 for (j=0; j<info->array.raid_disks; j++) {
ca3b6696
N
4672 if (fdlist[j] < 0)
4673 continue;
3da92f27 4674 if (st->ss->load_super(st, fdlist[j], NULL))
353632d9 4675 continue;
a5d85af7 4676 st->ss->getinfo_super(st, &dinfo, NULL);
e9e43ec3 4677 dinfo.reshape_progress = info->reshape_progress;
3da92f27 4678 st->ss->update_super(st, &dinfo,
68c7d6d7
NB
4679 "_reshape_progress",
4680 NULL,0, 0, NULL);
3da92f27
NB
4681 st->ss->store_super(st, fdlist[j]);
4682 st->ss->free_super(st);
353632d9 4683 }
353632d9
NB
4684 return 0;
4685 }
6e9eac4f
NB
4686 /* Didn't find any backup data, try to see if any
4687 * was needed.
4688 */
82f2d6ab
N
4689 if (info->delta_disks < 0) {
4690 /* When shrinking, the critical section is at the end.
4691 * So see if we are before the critical section.
4692 */
4693 unsigned long long first_block;
4694 nstripe = ostripe = 0;
4695 first_block = 0;
4696 while (ostripe >= nstripe) {
4697 ostripe += info->array.chunk_size / 512;
4698 first_block = ostripe * odata;
4699 nstripe = first_block / ndata / (info->new_chunk/512) *
4700 (info->new_chunk/512);
4701 }
4702
4703 if (info->reshape_progress >= first_block)
4704 return 0;
6e9eac4f 4705 }
82f2d6ab
N
4706 if (info->delta_disks > 0) {
4707 /* See if we are beyond the critical section. */
4708 unsigned long long last_block;
4709 nstripe = ostripe = 0;
4710 last_block = 0;
4711 while (nstripe >= ostripe) {
4712 nstripe += info->new_chunk / 512;
4713 last_block = nstripe * ndata;
4714 ostripe = last_block / odata / (info->array.chunk_size/512) *
4715 (info->array.chunk_size/512);
4716 }
6e9eac4f 4717
82f2d6ab
N
4718 if (info->reshape_progress >= last_block)
4719 return 0;
4720 }
6e9eac4f 4721 /* needed to recover critical section! */
ea0ebe96 4722 if (verbose)
e7b84f9d 4723 pr_err("Failed to find backup of critical section\n");
2295250a 4724 return 1;
353632d9 4725}
e9e43ec3 4726
2dddadb0
AK
4727int Grow_continue_command(char *devname, int fd,
4728 char *backup_file, int verbose)
4729{
4730 int ret_val = 0;
4731 struct supertype *st = NULL;
4732 struct mdinfo *content = NULL;
4733 struct mdinfo array;
4734 char *subarray = NULL;
4735 struct mdinfo *cc = NULL;
4736 struct mdstat_ent *mdstat = NULL;
2dddadb0
AK
4737 int cfd = -1;
4738 int fd2 = -1;
4739
4740 dprintf("Grow continue from command line called for %s\n",
4741 devname);
4742
4743 st = super_by_fd(fd, &subarray);
4744 if (!st || !st->ss) {
e7b84f9d
N
4745 pr_err("Unable to determine metadata format for %s\n",
4746 devname);
2dddadb0
AK
4747 return 1;
4748 }
4749 dprintf("Grow continue is run for ");
4750 if (st->ss->external == 0) {
e0ab37a3 4751 int d;
1ade5cc1 4752 dprintf_cont("native array (%s)\n", devname);
e0ab37a3 4753 if (ioctl(fd, GET_ARRAY_INFO, &array.array) < 0) {
7a862a02 4754 pr_err("%s is not an active md array - aborting\n", devname);
2dddadb0
AK
4755 ret_val = 1;
4756 goto Grow_continue_command_exit;
4757 }
4758 content = &array;
e0ab37a3
N
4759 /* Need to load a superblock.
4760 * FIXME we should really get what we need from
4761 * sysfs
4762 */
4763 for (d = 0; d < MAX_DISKS; d++) {
4764 mdu_disk_info_t disk;
4765 char *dv;
4766 int err;
4767 disk.number = d;
4768 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
4769 continue;
4770 if (disk.major == 0 && disk.minor == 0)
4771 continue;
4772 if ((disk.state & (1 << MD_DISK_ACTIVE)) == 0)
4773 continue;
4774 dv = map_dev(disk.major, disk.minor, 1);
4775 if (!dv)
4776 continue;
4777 fd2 = dev_open(dv, O_RDONLY);
4778 if (fd2 < 0)
4779 continue;
4780 err = st->ss->load_super(st, fd2, NULL);
4781 close(fd2);
364a48c9
JS
4782 /* invalidate fd2 to avoid possible double close() */
4783 fd2 = -1;
e0ab37a3
N
4784 if (err)
4785 continue;
4786 break;
4787 }
4788 if (d == MAX_DISKS) {
4789 pr_err("Unable to load metadata for %s\n",
4790 devname);
4791 ret_val = 1;
4792 goto Grow_continue_command_exit;
4793 }
4794 st->ss->getinfo_super(st, content, NULL);
2dddadb0 4795 } else {
4dd2df09 4796 char *container;
2dddadb0
AK
4797
4798 if (subarray) {
1ade5cc1 4799 dprintf_cont("subarray (%s)\n", subarray);
4dd2df09
N
4800 container = st->container_devnm;
4801 cfd = open_dev_excl(st->container_devnm);
2dddadb0 4802 } else {
4dd2df09 4803 container = st->devnm;
2dddadb0 4804 close(fd);
4dd2df09 4805 cfd = open_dev_excl(st->devnm);
1ade5cc1 4806 dprintf_cont("container (%s)\n", container);
2dddadb0
AK
4807 fd = cfd;
4808 }
4809 if (cfd < 0) {
7a862a02 4810 pr_err("Unable to open container for %s\n", devname);
2dddadb0
AK
4811 ret_val = 1;
4812 goto Grow_continue_command_exit;
4813 }
2dddadb0
AK
4814
4815 /* find in container array under reshape
4816 */
4817 ret_val = st->ss->load_container(st, cfd, NULL);
4818 if (ret_val) {
e7b84f9d
N
4819 pr_err("Cannot read superblock for %s\n",
4820 devname);
2dddadb0
AK
4821 ret_val = 1;
4822 goto Grow_continue_command_exit;
4823 }
4824
446894ea 4825 cc = st->ss->container_content(st, subarray);
2dddadb0
AK
4826 for (content = cc; content ; content = content->next) {
4827 char *array;
446894ea 4828 int allow_reshape = 1;
2dddadb0
AK
4829
4830 if (content->reshape_active == 0)
4831 continue;
81219e70
LM
4832 /* The decision about array or container wide
4833 * reshape is taken in Grow_continue based
4834 * content->reshape_active state, therefore we
4835 * need to check_reshape based on
4836 * reshape_active and subarray name
446894ea
N
4837 */
4838 if (content->array.state & (1<<MD_SB_BLOCK_VOLUME))
4839 allow_reshape = 0;
4840 if (content->reshape_active == CONTAINER_RESHAPE &&
4841 (content->array.state
4842 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE)))
4843 allow_reshape = 0;
4844
81219e70 4845 if (!allow_reshape) {
7a862a02 4846 pr_err("cannot continue reshape of an array in container with unsupported metadata: %s(%s)\n",
4dd2df09 4847 devname, container);
81219e70
LM
4848 ret_val = 1;
4849 goto Grow_continue_command_exit;
4850 }
2dddadb0
AK
4851
4852 array = strchr(content->text_version+1, '/')+1;
4dd2df09 4853 mdstat = mdstat_by_subdev(array, container);
2dddadb0
AK
4854 if (!mdstat)
4855 continue;
1ca90aa6 4856 if (mdstat->active == 0) {
4dd2df09
N
4857 pr_err("Skipping inactive array %s.\n",
4858 mdstat->devnm);
1ca90aa6
AK
4859 free_mdstat(mdstat);
4860 mdstat = NULL;
4861 continue;
4862 }
2dddadb0
AK
4863 break;
4864 }
4865 if (!content) {
7a862a02 4866 pr_err("Unable to determine reshaped array for %s\n", devname);
2dddadb0
AK
4867 ret_val = 1;
4868 goto Grow_continue_command_exit;
4869 }
4dd2df09 4870 fd2 = open_dev(mdstat->devnm);
2dddadb0 4871 if (fd2 < 0) {
4dd2df09 4872 pr_err("cannot open (%s)\n", mdstat->devnm);
2dddadb0
AK
4873 ret_val = 1;
4874 goto Grow_continue_command_exit;
4875 }
4876
4dd2df09 4877 sysfs_init(content, fd2, mdstat->devnm);
2dddadb0
AK
4878
4879 /* start mdmon in case it is not running
4880 */
4dd2df09
N
4881 if (!mdmon_running(container))
4882 start_mdmon(container);
4883 ping_monitor(container);
2dddadb0 4884
4dd2df09 4885 if (mdmon_running(container))
2dddadb0
AK
4886 st->update_tail = &st->updates;
4887 else {
7a862a02 4888 pr_err("No mdmon found. Grow cannot continue.\n");
2dddadb0
AK
4889 ret_val = 1;
4890 goto Grow_continue_command_exit;
4891 }
4892 }
4893
f1fe496b
AK
4894 /* verify that array under reshape is started from
4895 * correct position
4896 */
e0ab37a3 4897 if (verify_reshape_position(content, content->array.level) < 0) {
f1fe496b
AK
4898 ret_val = 1;
4899 goto Grow_continue_command_exit;
4900 }
4901
2dddadb0
AK
4902 /* continue reshape
4903 */
06e293d0 4904 ret_val = Grow_continue(fd, st, content, backup_file, 1, 0);
2dddadb0
AK
4905
4906Grow_continue_command_exit:
4907 if (fd2 > -1)
4908 close(fd2);
4909 if (cfd > -1)
4910 close(cfd);
4911 st->ss->free_super(st);
4912 free_mdstat(mdstat);
4913 sysfs_free(cc);
4914 free(subarray);
4915
4916 return ret_val;
4917}
4918
e9e43ec3 4919int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
06e293d0 4920 char *backup_file, int forked, int freeze_reshape)
e9e43ec3 4921{
3bd58dc6
AK
4922 int ret_val = 2;
4923
4924 if (!info->reshape_active)
4925 return ret_val;
864a004f 4926
20a40eca 4927 if (st->ss->external) {
4dd2df09 4928 int cfd = open_dev(st->container_devnm);
3db2fdd8 4929
3bd58dc6
AK
4930 if (cfd < 0)
4931 return 1;
f362d22b 4932
4dd2df09 4933 st->ss->load_container(st, cfd, st->container_devnm);
3bd58dc6 4934 close(cfd);
4dd2df09 4935 ret_val = reshape_container(st->container_devnm, NULL, mdfd,
3bd58dc6 4936 st, info, 0, backup_file,
054cba77 4937 0, forked,
8ecf12b9
N
4938 1 | info->reshape_active,
4939 freeze_reshape);
3bd58dc6
AK
4940 } else
4941 ret_val = reshape_array(NULL, mdfd, "array", st, info, 1,
ca36d707 4942 NULL, INVALID_SECTORS,
06e293d0 4943 backup_file, 0, forked,
8ecf12b9 4944 1 | info->reshape_active,
3bd58dc6 4945 freeze_reshape);
f362d22b 4946
3bd58dc6 4947 return ret_val;
e9e43ec3 4948}
54ded86f
N
4949
4950char *make_backup(char *name)
4951{
4952 char *base = "backup_file-";
4953 int len;
4954 char *fname;
4955
4956 len = strlen(MAP_DIR) + 1 + strlen(base) + strlen(name)+1;
4957 fname = xmalloc(len);
4958 sprintf(fname, "%s/%s%s", MAP_DIR, base, name);
4959 return fname;
4960}
4961
4962char *locate_backup(char *name)
4963{
4964 char *fl = make_backup(name);
4965 struct stat stb;
4966
4967 if (stat(fl, &stb) == 0 &&
4968 S_ISREG(stb.st_mode))
4969 return fl;
4970
4971 free(fl);
4972 return NULL;
4973}