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