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