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