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