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