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