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