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