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