]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Grow.c
Change some fprintf(stderrs to cont_err()
[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 {
2196 if ((unsigned long long)data_offset
2197 < info2.data_offset + min) {
ed503f89 2198 pr_err("--data-offset too small for %s\n",
19ceb16d
N
2199 dn);
2200 goto release;
2201 }
2202 info2.new_data_offset = data_offset;
2203 }
50a4962f 2204 } else if (delta_disks > 0) {
19ceb16d
N
2205 /* need space before */
2206 if (info2.space_before < min) {
ed503f89 2207 pr_err("Insufficient head-space for reshape on %s\n",
19ceb16d
N
2208 dn);
2209 goto release;
2210 }
5582b118 2211 if (data_offset == INVALID_SECTORS)
19ceb16d
N
2212 info2.new_data_offset = info2.data_offset - min;
2213 else {
2214 if ((unsigned long long)data_offset
2215 > info2.data_offset - min) {
ed503f89 2216 pr_err("--data-offset too large for %s\n",
19ceb16d
N
2217 dn);
2218 goto release;
2219 }
2220 info2.new_data_offset = data_offset;
2221 }
2222 } else {
2223 if (dir == 0) {
2224 /* can move up or down. 'data_offset'
2225 * might guide us, otherwise choose
2226 * direction with most space
2227 */
5582b118 2228 if (data_offset == INVALID_SECTORS) {
19ceb16d
N
2229 if (info2.space_before > info2.space_after)
2230 dir = -1;
2231 else
2232 dir = 1;
2233 } else if (data_offset < info2.data_offset)
2234 dir = -1;
2235 else
2236 dir = 1;
2237 sysfs_set_str(sra, NULL, "reshape_direction",
2238 dir == 1 ? "backwards" : "forwards");
2239 }
2240 switch (dir) {
2241 case 1: /* Increase data offset */
2242 if (info2.space_after < min) {
ed503f89 2243 pr_err("Insufficient tail-space for reshape on %s\n",
19ceb16d
N
2244 dn);
2245 goto release;
2246 }
5582b118 2247 if (data_offset != INVALID_SECTORS &&
19ceb16d 2248 data_offset < info2.data_offset + min) {
ed503f89 2249 pr_err("--data-offset too small on %s\n",
19ceb16d
N
2250 dn);
2251 goto release;
2252 }
5582b118 2253 if (data_offset != INVALID_SECTORS)
19ceb16d
N
2254 info2.new_data_offset = data_offset;
2255 else {
2256 unsigned long long off =
2257 info2.space_after / 2;
2258 off &= ~7ULL;
2259 if (off < min)
2260 off = min;
2261 info2.new_data_offset =
2262 info2.data_offset + off;
2263 }
2264 break;
2265 case -1: /* Decrease data offset */
2266 if (info2.space_before < min) {
ed503f89 2267 pr_err("insufficient head-room on %s\n",
19ceb16d
N
2268 dn);
2269 goto release;
2270 }
5582b118 2271 if (data_offset != INVALID_SECTORS &&
19ceb16d 2272 data_offset < info2.data_offset - min) {
ed503f89 2273 pr_err("--data-offset too small on %s\n",
19ceb16d
N
2274 dn);
2275 goto release;
2276 }
5582b118 2277 if (data_offset != INVALID_SECTORS)
19ceb16d
N
2278 info2.new_data_offset = data_offset;
2279 else {
2280 unsigned long long off =
2281 info2.space_before / 2;
2282 off &= ~7ULL;
2283 if (off < min)
2284 off = min;
2285 info2.new_data_offset =
2286 info2.data_offset - off;
2287 }
2288 break;
2289 }
2290 }
2291 if (sysfs_set_num(sra, sd, "new_offset",
2292 info2.new_data_offset) < 0) {
2293 err = errno;
93f174b9
N
2294 if (sd == sra->devs && err == ENOENT)
2295 /* Early kernel, no 'new_offset' file.
2296 * For RAID5/6 this is not fatal
2297 */
2298 return 1;
ed503f89 2299 pr_err("Cannot set new_offset for %s\n",
19ceb16d
N
2300 dn);
2301 break;
2302 }
2303 }
13bbb145
N
2304 return err;
2305release:
2306 return -1;
2307}
2308
2309static int raid10_reshape(char *container, int fd, char *devname,
2310 struct supertype *st, struct mdinfo *info,
2311 struct reshape *reshape,
2312 unsigned long long data_offset,
2313 int force, int verbose)
2314{
2315 /* Changing raid_disks, layout, chunksize or possibly
2316 * just data_offset for a RAID10.
2317 * We must always change data_offset. We change by at least
2318 * ->backup_blocks which is the largest of the old and new
2319 * chunk sizes.
2320 * If raid_disks is increasing, then data_offset must decrease
2321 * by at least this copy size.
2322 * If raid_disks is unchanged, data_offset must increase or
2323 * decrease by at least backup_blocks but preferably by much more.
2324 * We choose half of the available space.
2325 * If raid_disks is decreasing, data_offset must increase by
2326 * at least backup_blocks. To allow of this, component_size
2327 * must be decreased by the same amount.
2328 *
2329 * So we calculate the required minimum and direction, possibly
2330 * reduce the component_size, then iterate through the devices
2331 * and set the new_data_offset.
2332 * If that all works, we set chunk_size, layout, raid_disks, and start
2333 * 'reshape'
2334 */
2335 struct mdinfo *sra;
2336 unsigned long long min;
2337 int err = 0;
2338
2339 sra = sysfs_read(fd, NULL,
2340 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK
2341 );
2342 if (!sra) {
ed503f89 2343 pr_err("%s: Cannot get array details from sysfs\n",
13bbb145
N
2344 devname);
2345 goto release;
2346 }
2347 min = reshape->backup_blocks;
2348
2349 if (info->delta_disks)
2350 sysfs_set_str(sra, NULL, "reshape_direction",
2351 info->delta_disks < 0 ? "backwards" : "forwards");
2352 if (info->delta_disks < 0 &&
2353 info->space_after < reshape->backup_blocks) {
2354 int rv = sysfs_set_num(sra, NULL, "component_size",
2355 (sra->component_size -
2356 reshape->backup_blocks)/2);
2357 if (rv) {
ed503f89 2358 pr_err("cannot reduce component size\n");
13bbb145
N
2359 goto release;
2360 }
2361 }
50a4962f 2362 err = set_new_data_offset(sra, st, devname, info->delta_disks, data_offset,
13bbb145 2363 min);
93f174b9
N
2364 if (err == 1) {
2365 pr_err("Cannot set new_data_offset: RAID10 reshape not\n");
2366 cont_err("supported on this kernel\n");
2367 err = -1;
2368 }
13bbb145
N
2369 if (err < 0)
2370 goto release;
2371
ee2429e0 2372 if (!err && sysfs_set_num(sra, NULL, "chunk_size", info->new_chunk) < 0)
19ceb16d
N
2373 err = errno;
2374 if (!err && sysfs_set_num(sra, NULL, "layout", reshape->after.layout) < 0)
2375 err = errno;
2376 if (!err && sysfs_set_num(sra, NULL, "raid_disks",
2377 info->array.raid_disks + info->delta_disks) < 0)
2378 err = errno;
2379 if (!err && sysfs_set_str(sra, NULL, "sync_action", "reshape") < 0)
2380 err = errno;
2381 if (err) {
ed503f89 2382 pr_err("Cannot set array shape for %s\n",
d33f1518
N
2383 devname);
2384 if (err == EBUSY &&
2385 (info->array.state & (1<<MD_SB_BITMAP_PRESENT)))
2386 cont_err(" Bitmap must be removed before"
2387 " shape can be changed\n");
2388 goto release;
19ceb16d
N
2389 }
2390 sysfs_free(sra);
2391 return 0;
2392release:
2393 sysfs_free(sra);
2394 return 1;
2395}
2396
ee2429e0
N
2397static void get_space_after(int fd, struct supertype *st, struct mdinfo *info)
2398{
2399 struct mdinfo *sra, *sd;
f3f09a52
LD
2400 /* Initialisation to silence compiler warning */
2401 unsigned long long min_space_before = 0, min_space_after = 0;
ee2429e0
N
2402 int first = 1;
2403
4dd2df09 2404 sra = sysfs_read(fd, NULL, GET_DEVS);
ee2429e0
N
2405 if (!sra)
2406 return;
2407 for (sd = sra->devs; sd; sd = sd->next) {
2408 char *dn;
2409 int dfd;
2410 struct supertype *st2;
2411 struct mdinfo info2;
2412
2413 if (sd->disk.state & (1<<MD_DISK_FAULTY))
2414 continue;
2415 dn = map_dev(sd->disk.major, sd->disk.minor, 0);
2416 dfd = dev_open(dn, O_RDONLY);
2417 if (dfd < 0)
2418 break;
2419 st2 = dup_super(st);
2420 if (st2->ss->load_super(st2,dfd, NULL)) {
2421 close(dfd);
2422 free(st2);
2423 break;
2424 }
2425 close(dfd);
2426 st2->ss->getinfo_super(st2, &info2, NULL);
2427 st2->ss->free_super(st2);
2428 free(st2);
2429 if (first ||
2430 min_space_before > info2.space_before)
2431 min_space_before = info2.space_before;
2432 if (first ||
2433 min_space_after > info2.space_after)
2434 min_space_after = info2.space_after;
2435 first = 0;
2436 }
2437 if (sd == NULL && !first) {
2438 info->space_after = min_space_after;
2439 info->space_before = min_space_before;
2440 }
2441 sysfs_free(sra);
2442}
2443
5da9ab98
N
2444static int reshape_array(char *container, int fd, char *devname,
2445 struct supertype *st, struct mdinfo *info,
e2e53a2d 2446 int force, struct mddev_dev *devlist,
19ceb16d 2447 unsigned long long data_offset,
ba728be7 2448 char *backup_file, int verbose, int forked,
b76b30e0 2449 int restart, int freeze_reshape)
5da9ab98
N
2450{
2451 struct reshape reshape;
2452 int spares_needed;
2453 char *msg;
2454 int orig_level = UnSet;
2455 int disks, odisks;
1f9b0e28 2456 int delayed;
7bc71196 2457
5da9ab98
N
2458 struct mdu_array_info_s array;
2459 char *c;
e86c9dd6 2460
e2e53a2d
N
2461 struct mddev_dev *dv;
2462 int added_disks;
2463
d152f53e
JS
2464 int *fdlist = NULL;
2465 unsigned long long *offsets = NULL;
5da9ab98
N
2466 int d;
2467 int nrdisks;
2468 int err;
da8100ca 2469 unsigned long blocks;
5da9ab98
N
2470 unsigned long cache;
2471 unsigned long long array_size;
2472 int done;
cf6ac177 2473 struct mdinfo *sra = NULL;
7bc71196 2474
9468aeac
N
2475 /* when reshaping a RAID0, the component_size might be zero.
2476 * So try to fix that up.
2477 */
2478 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
2479 dprintf("Cannot get array information.\n");
2480 goto release;
2481 }
2482 if (array.level == 0 && info->component_size == 0) {
2483 get_dev_size(fd, NULL, &array_size);
2484 info->component_size = array_size / array.raid_disks;
2485 }
2486
ee2429e0
N
2487 if (array.level == 10)
2488 /* Need space_after info */
2489 get_space_after(fd, st, info);
2490
3cb2aed2
N
2491 if (info->reshape_active) {
2492 int new_level = info->new_level;
2493 info->new_level = UnSet;
ce4783d3
N
2494 if (info->delta_disks > 0)
2495 info->array.raid_disks -= info->delta_disks;
3cb2aed2
N
2496 msg = analyse_change(info, &reshape);
2497 info->new_level = new_level;
ce4783d3
N
2498 if (info->delta_disks > 0)
2499 info->array.raid_disks += info->delta_disks;
fcdfb814
AK
2500 if (!restart)
2501 /* Make sure the array isn't read-only */
2502 ioctl(fd, RESTART_ARRAY_RW, 0);
3cb2aed2
N
2503 } else
2504 msg = analyse_change(info, &reshape);
5da9ab98 2505 if (msg) {
e7b84f9d 2506 pr_err("%s\n", msg);
631d7405 2507 goto release;
5da9ab98 2508 }
817ed7d6
N
2509 if (restart &&
2510 (reshape.level != info->array.level ||
2511 reshape.before.layout != info->array.layout ||
384e9be1 2512 reshape.before.data_disks + reshape.parity
ce4783d3 2513 != info->array.raid_disks - max(0, info->delta_disks))) {
e7b84f9d 2514 pr_err("reshape info is not in native format -"
20a40eca
N
2515 " cannot continue.\n");
2516 goto release;
2517 }
a93f87ee 2518
e1dd332a
AK
2519 if (st->ss->external && restart && (info->reshape_progress == 0)) {
2520 /* When reshape is restarted from '0', very begin of array
2521 * it is possible that for external metadata reshape and array
2522 * configuration doesn't happen.
2523 * Check if md has the same opinion, and reshape is restarted
2524 * from 0. If so, this is regular reshape start after reshape
2525 * switch in metadata to next array only.
2526 */
2527 if ((verify_reshape_position(info, reshape.level) >= 0) &&
2528 (info->reshape_progress == 0))
2529 restart = 0;
2530 }
a93f87ee
N
2531 if (restart) {
2532 /* reshape already started. just skip to monitoring the reshape */
2533 if (reshape.backup_blocks == 0)
2534 return 0;
2535 goto started;
2536 }
a9c3e78f
AK
2537 /* The container is frozen but the array may not be.
2538 * So freeze the array so spares don't get put to the wrong use
2539 * FIXME there should probably be a cleaner separation between
2540 * freeze_array and freeze_container.
2541 */
2542 sysfs_freeze_array(info);
e06c4e59 2543 /* Check we have enough spares to not be degraded */
e2e53a2d
N
2544 added_disks = 0;
2545 for (dv = devlist; dv ; dv=dv->next)
2546 added_disks++;
5da9ab98
N
2547 spares_needed = max(reshape.before.data_disks,
2548 reshape.after.data_disks)
2549 + reshape.parity - array.raid_disks;
7bc71196 2550
88c1a083 2551 if (!force &&
e2e53a2d
N
2552 info->new_level > 1 && info->array.level > 1 &&
2553 spares_needed > info->array.spare_disks + added_disks) {
e7b84f9d
N
2554 pr_err("Need %d spare%s to avoid degraded array,"
2555 " and only have %d.\n"
2556 " Use --force to over-ride this check.\n",
2557 spares_needed,
2558 spares_needed == 1 ? "" : "s",
2559 info->array.spare_disks + added_disks);
631d7405 2560 goto release;
7bc71196 2561 }
e06c4e59
N
2562 /* Check we have enough spares to not fail */
2563 spares_needed = max(reshape.before.data_disks,
2564 reshape.after.data_disks)
2565 - array.raid_disks;
2566 if ((info->new_level > 1 || info->new_level == 0) &&
e2e53a2d 2567 spares_needed > info->array.spare_disks +added_disks) {
e7b84f9d
N
2568 pr_err("Need %d spare%s to create working array,"
2569 " and only have %d.\n",
2570 spares_needed,
2571 spares_needed == 1 ? "" : "s",
2572 info->array.spare_disks + added_disks);
e06c4e59
N
2573 goto release;
2574 }
e86c9dd6 2575
eae35f5c 2576 if (reshape.level != array.level) {
5da9ab98
N
2577 char *c = map_num(pers, reshape.level);
2578 int err;
2579 if (c == NULL)
631d7405 2580 goto release;
e86c9dd6 2581
5da9ab98
N
2582 err = sysfs_set_str(info, NULL, "level", c);
2583 if (err) {
2584 err = errno;
e7b84f9d 2585 pr_err("%s: could not set level to %s\n",
5da9ab98 2586 devname, c);
24daa16f 2587 if (err == EBUSY &&
5da9ab98 2588 (info->array.state & (1<<MD_SB_BITMAP_PRESENT)))
e7b84f9d
N
2589 cont_err("Bitmap must be removed"
2590 " before level can be changed\n");
631d7405 2591 goto release;
19678e53 2592 }
ba728be7 2593 if (verbose >= 0)
e7b84f9d 2594 pr_err("level of %s changed to %s\n",
24daa16f 2595 devname, c);
eae35f5c 2596 orig_level = array.level;
3cd4e7c4 2597 sysfs_freeze_array(info);
e86c9dd6 2598
16d4d84e
AK
2599 if (reshape.level > 0 && st->ss->external) {
2600 /* make sure mdmon is aware of the new level */
4dd2df09 2601 if (mdmon_running(container))
78340e26
AK
2602 flush_mdmon(container);
2603
4dd2df09
N
2604 if (!mdmon_running(container))
2605 start_mdmon(container);
16d4d84e 2606 ping_monitor(container);
4dd2df09 2607 if (mdmon_running(container) &&
e919fb0a
AK
2608 st->update_tail == NULL)
2609 st->update_tail = &st->updates;
16d4d84e 2610 }
5da9ab98 2611 }
5da9ab98
N
2612 /* ->reshape_super might have chosen some spares from the
2613 * container that it wants to be part of the new array.
2614 * We can collect them with ->container_content and give
2615 * them to the kernel.
2616 */
2617 if (st->ss->reshape_super && st->ss->container_content) {
2618 char *subarray = strchr(info->text_version+1, '/')+1;
2619 struct mdinfo *info2 =
2620 st->ss->container_content(st, subarray);
2621 struct mdinfo *d;
2622
2dd0d697 2623 if (info2) {
4dd2df09 2624 sysfs_init(info2, fd, st->devnm);
0d5ac3c6
N
2625 /* When increasing number of devices, we need to set
2626 * new raid_disks before adding these, or they might
2627 * be rejected.
2628 */
2629 if (reshape.backup_blocks &&
2630 reshape.after.data_disks > reshape.before.data_disks)
2631 subarray_set_num(container, info2, "raid_disks",
2632 reshape.after.data_disks +
2633 reshape.parity);
5da9ab98
N
2634 for (d = info2->devs; d; d = d->next) {
2635 if (d->disk.state == 0 &&
2636 d->disk.raid_disk >= 0) {
2637 /* This is a spare that wants to
2638 * be part of the array.
2639 */
2640 add_disk(fd, st, info2, d);
2641 }
9860f271 2642 }
2dd0d697
AK
2643 sysfs_free(info2);
2644 }
5da9ab98 2645 }
e2e53a2d
N
2646 /* We might have been given some devices to add to the
2647 * array. Now that the array has been changed to the right
2648 * level and frozen, we can safely add them.
2649 */
2650 if (devlist)
ba728be7 2651 Manage_subdevs(devname, fd, devlist, verbose,
11b391ec 2652 0,NULL, 0);
1686dc25 2653
b4f8e38b 2654 if (reshape.backup_blocks == 0) {
5da9ab98
N
2655 /* No restriping needed, but we might need to impose
2656 * some more changes: layout, raid_disks, chunk_size
e86c9dd6 2657 */
24aebf3a
KW
2658 /* read current array info */
2659 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
83732c28 2660 dprintf("Cannot get array information.\n");
24aebf3a
KW
2661 goto release;
2662 }
2663 /* compare current array info with new values and if
2664 * it is different update them to new */
5da9ab98 2665 if (info->new_layout != UnSet &&
24aebf3a
KW
2666 info->new_layout != array.layout) {
2667 array.layout = info->new_layout;
2668 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
e7b84f9d 2669 pr_err("failed to set new layout\n");
631d7405 2670 goto release;
ba728be7 2671 } else if (verbose >= 0)
5da9ab98 2672 printf("layout for %s set to %d\n",
24aebf3a 2673 devname, array.layout);
5da9ab98
N
2674 }
2675 if (info->delta_disks != UnSet &&
24aebf3a
KW
2676 info->delta_disks != 0 &&
2677 array.raid_disks != (info->array.raid_disks + info->delta_disks)) {
2678 array.raid_disks += info->delta_disks;
2679 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
e7b84f9d 2680 pr_err("failed to set raid disks\n");
631d7405 2681 goto release;
ba728be7 2682 } else if (verbose >= 0) {
5da9ab98 2683 printf("raid_disks for %s set to %d\n",
24aebf3a
KW
2684 devname, array.raid_disks);
2685 }
5da9ab98
N
2686 }
2687 if (info->new_chunk != 0 &&
24aebf3a 2688 info->new_chunk != array.chunk_size) {
5da9ab98
N
2689 if (sysfs_set_num(info, NULL,
2690 "chunk_size", info->new_chunk) != 0) {
e7b84f9d 2691 pr_err("failed to set chunk size\n");
631d7405 2692 goto release;
ba728be7 2693 } else if (verbose >= 0)
5da9ab98 2694 printf("chunk size for %s set to %d\n",
24aebf3a 2695 devname, array.chunk_size);
4725bc31 2696 }
e35b189b 2697 unfreeze(st);
631d7405 2698 return 0;
5da9ab98 2699 }
19678e53 2700
5da9ab98
N
2701 /*
2702 * There are three possibilities.
2703 * 1/ The array will shrink.
2704 * We need to ensure the reshape will pause before reaching
2705 * the 'critical section'. We also need to fork and wait for
24daa16f 2706 * that to happen. When it does we
5da9ab98
N
2707 * suspend/backup/complete/unfreeze
2708 *
2709 * 2/ The array will not change size.
2710 * This requires that we keep a backup of a sliding window
2711 * so that we can restore data after a crash. So we need
2712 * to fork and monitor progress.
2713 * In future we will allow the data_offset to change, so
2714 * a sliding backup becomes unnecessary.
2715 *
2716 * 3/ The array will grow. This is relatively easy.
2717 * However the kernel's restripe routines will cheerfully
2718 * overwrite some early data before it is safe. So we
2719 * need to make a backup of the early parts of the array
2720 * and be ready to restore it if rebuild aborts very early.
2721 * For externally managed metadata, we still need a forked
2722 * child to monitor the reshape and suspend IO over the region
2723 * that is being reshaped.
2724 *
2725 * We backup data by writing it to one spare, or to a
2726 * file which was given on command line.
2727 *
2728 * In each case, we first make sure that storage is available
2729 * for the required backup.
2730 * Then we:
2731 * - request the shape change.
2732 * - fork to handle backup etc.
2733 */
5da9ab98
N
2734 /* Check that we can hold all the data */
2735 get_dev_size(fd, NULL, &array_size);
2736 if (reshape.new_size < (array_size/512)) {
e7b84f9d
N
2737 pr_err("this change will reduce the size of the array.\n"
2738 " use --grow --array-size first to truncate array.\n"
2739 " e.g. mdadm --grow %s --array-size %llu\n",
2740 devname, reshape.new_size/2);
5da9ab98
N
2741 goto release;
2742 }
7236ee7a 2743
19ceb16d
N
2744started:
2745
2746 if (array.level == 10) {
033d0929 2747 /* Reshaping RAID10 does not require any data backup by
19ceb16d
N
2748 * user-space. Instead it requires that the data_offset
2749 * is changed to avoid the need for backup.
2750 * So this is handled very separately
2751 */
2752 if (restart)
2753 /* Nothing to do. */
2754 return 0;
2755 return raid10_reshape(container, fd, devname, st, info,
2756 &reshape, data_offset,
2757 force, verbose);
2758 }
4dd2df09 2759 sra = sysfs_read(fd, NULL,
3656edcd 2760 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK|
5da9ab98 2761 GET_CACHE);
5da9ab98 2762 if (!sra) {
e7b84f9d 2763 pr_err("%s: Cannot get array details from sysfs\n",
5da9ab98 2764 devname);
5da9ab98
N
2765 goto release;
2766 }
7236ee7a 2767
5da9ab98
N
2768 /* Decide how many blocks (sectors) for a reshape
2769 * unit. The number we have so far is just a minimum
2770 */
b4f8e38b 2771 blocks = reshape.backup_blocks;
24daa16f 2772 if (reshape.before.data_disks ==
5da9ab98
N
2773 reshape.after.data_disks) {
2774 /* Make 'blocks' bigger for better throughput, but
2775 * not so big that we reject it below.
2776 * Try for 16 megabytes
2777 */
2778 while (blocks * 32 < sra->component_size &&
2779 blocks < 16*1024*2)
2780 blocks *= 2;
2781 } else
e7b84f9d 2782 pr_err("Need to backup %luK of critical "
5da9ab98
N
2783 "section..\n", blocks/2);
2784
2785 if (blocks >= sra->component_size/2) {
e7b84f9d 2786 pr_err("%s: Something wrong"
5da9ab98
N
2787 " - reshape aborted\n",
2788 devname);
5da9ab98
N
2789 goto release;
2790 }
7236ee7a 2791
5da9ab98
N
2792 /* Now we need to open all these devices so we can read/write.
2793 */
b8b286a6
N
2794 nrdisks = max(reshape.before.data_disks,
2795 reshape.after.data_disks) + reshape.parity
2796 + sra->array.spare_disks;
503975b9
N
2797 fdlist = xcalloc((1+nrdisks), sizeof(int));
2798 offsets = xcalloc((1+nrdisks), sizeof(offsets[0]));
e380d3be 2799
b8b286a6
N
2800 odisks = reshape.before.data_disks + reshape.parity;
2801 d = reshape_prepare_fdlist(devname, sra, odisks,
5da9ab98
N
2802 nrdisks, blocks, backup_file,
2803 fdlist, offsets);
2804 if (d < 0) {
5da9ab98
N
2805 goto release;
2806 }
13c37ad3
AK
2807 if ((st->ss->manage_reshape == NULL) ||
2808 (st->ss->recover_backup == NULL)) {
2809 if (backup_file == NULL) {
2810 if (reshape.after.data_disks <=
2811 reshape.before.data_disks) {
e7b84f9d 2812 pr_err("%s: Cannot grow - "
13c37ad3
AK
2813 "need backup-file\n", devname);
2814 goto release;
2815 } else if (sra->array.spare_disks == 0) {
e7b84f9d 2816 pr_err("%s: Cannot grow - "
13c37ad3
AK
2817 "need a spare or backup-file to backup "
2818 "critical section\n", devname);
2819 goto release;
2820 }
2821 } else {
2822 if (!reshape_open_backup_file(backup_file, fd, devname,
2823 (signed)blocks,
2824 fdlist+d, offsets+d,
2825 restart)) {
2826 goto release;
2827 }
2828 d++;
e86c9dd6 2829 }
5da9ab98 2830 }
7236ee7a 2831
5da9ab98
N
2832 /* lastly, check that the internal stripe cache is
2833 * large enough, or it won't work.
2834 * It must hold at least 4 stripes of the larger
2835 * chunk size
2836 */
2837 cache = max(info->array.chunk_size, info->new_chunk);
2838 cache *= 4; /* 4 stripes minimum */
2839 cache /= 512; /* convert to sectors */
2840 disks = min(reshape.before.data_disks, reshape.after.data_disks);
2841 /* make sure there is room for 'blocks' with a bit to spare */
2842 if (cache < 16 + blocks / disks)
2843 cache = 16 + blocks / disks;
2844 cache /= (4096/512); /* Covert from sectors to pages */
2845
2846 if (sra->cache_size < cache)
2847 subarray_set_num(container, sra, "stripe_cache_size",
2848 cache+1);
2849
2850 /* Right, everything seems fine. Let's kick things off.
2851 * If only changing raid_disks, use ioctl, else use
2852 * sysfs.
2853 */
2854 sync_metadata(st);
2855
b4f8e38b 2856 sra->new_chunk = info->new_chunk;
ddee071d 2857
f93346ef
AK
2858 if (restart) {
2859 /* for external metadata checkpoint saved by mdmon can be lost
2860 * or missed /due to e.g. crash/. Check if md is not during
2861 * restart farther than metadata points to.
2862 * If so, this means metadata information is obsolete.
2863 */
2864 if (st->ss->external)
2865 verify_reshape_position(info, reshape.level);
ddee071d 2866 sra->reshape_progress = info->reshape_progress;
f93346ef 2867 } else {
f897078e
N
2868 sra->reshape_progress = 0;
2869 if (reshape.after.data_disks < reshape.before.data_disks)
2870 /* start from the end of the new array */
2871 sra->reshape_progress = (sra->component_size
2872 * reshape.after.data_disks);
2873 }
2874
2875 if (info->array.chunk_size == info->new_chunk &&
7477d513
AK
2876 reshape.before.layout == reshape.after.layout &&
2877 st->ss->external == 0) {
f897078e 2878 /* use SET_ARRAY_INFO but only if reshape hasn't started */
6ef421be 2879 ioctl(fd, GET_ARRAY_INFO, &array);
5da9ab98 2880 array.raid_disks = reshape.after.data_disks + reshape.parity;
20a40eca 2881 if (!restart &&
f897078e 2882 ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
5da9ab98 2883 int err = errno;
631d7405 2884
e7b84f9d
N
2885 pr_err("Cannot set device shape for %s: %s\n",
2886 devname, strerror(errno));
7bc71196 2887
24daa16f 2888 if (err == EBUSY &&
5da9ab98 2889 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
e7b84f9d
N
2890 cont_err("Bitmap must be removed before"
2891 " shape can be changed\n");
b5420ef3 2892
5da9ab98
N
2893 goto release;
2894 }
20a40eca 2895 } else if (!restart) {
5da9ab98 2896 /* set them all just in case some old 'new_*' value
f897078e 2897 * persists from some earlier problem.
7236ee7a 2898 */
631d7405 2899 int err = 0;
5da9ab98 2900 if (sysfs_set_num(sra, NULL, "chunk_size", info->new_chunk) < 0)
631d7405 2901 err = errno;
24daa16f
N
2902 if (!err && sysfs_set_num(sra, NULL, "layout",
2903 reshape.after.layout) < 0)
631d7405
N
2904 err = errno;
2905 if (!err && subarray_set_num(container, sra, "raid_disks",
24daa16f
N
2906 reshape.after.data_disks +
2907 reshape.parity) < 0)
631d7405
N
2908 err = errno;
2909 if (err) {
e7b84f9d 2910 pr_err("Cannot set device shape for %s\n",
5da9ab98 2911 devname);
7236ee7a 2912
24daa16f 2913 if (err == EBUSY &&
5da9ab98 2914 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
e7b84f9d
N
2915 cont_err("Bitmap must be removed before"
2916 " shape can be changed\n");
5da9ab98 2917 goto release;
e86c9dd6 2918 }
5da9ab98 2919 }
e86c9dd6 2920
27a1e5b5
N
2921 err = start_reshape(sra, restart, reshape.before.data_disks,
2922 reshape.after.data_disks);
fab32c97 2923 if (err) {
e7b84f9d
N
2924 pr_err("Cannot %s reshape for %s\n",
2925 restart ? "continue" : "start",
2926 devname);
fab32c97
AK
2927 goto release;
2928 }
a93f87ee
N
2929 if (restart)
2930 sysfs_set_str(sra, NULL, "array_state", "active");
b76b30e0
AK
2931 if (freeze_reshape) {
2932 free(fdlist);
2933 free(offsets);
2934 sysfs_free(sra);
e7b84f9d 2935 pr_err("Reshape has to be continued from"
59ab9f54 2936 " location %llu when root filesystem has been mounted.\n",
b76b30e0
AK
2937 sra->reshape_progress);
2938 return 1;
2939 }
5da9ab98
N
2940
2941 /* Now we just need to kick off the reshape and watch, while
2942 * handling backups of the data...
2943 * This is all done by a forked background process.
2944 */
2945 switch(forked ? 0 : fork()) {
6b2d630c 2946 case -1:
e7b84f9d 2947 pr_err("Cannot run child to monitor reshape: %s\n",
6b2d630c 2948 strerror(errno));
6b2d630c 2949 abort_reshape(sra);
631d7405 2950 goto release;
6b2d630c 2951 default:
d152f53e
JS
2952 free(fdlist);
2953 free(offsets);
2954 sysfs_free(sra);
6b2d630c 2955 return 0;
5da9ab98 2956 case 0:
cc700db3 2957 map_fork();
6b2d630c
N
2958 break;
2959 }
5da9ab98 2960
1f9b0e28
N
2961 /* If another array on the same devices is busy, the
2962 * reshape will wait for them. This would mean that
2963 * the first section that we suspend will stay suspended
2964 * for a long time. So check on that possibility
2965 * by looking for "DELAYED" in /proc/mdstat, and if found,
2966 * wait a while
2967 */
2968 do {
2969 struct mdstat_ent *mds, *m;
2970 delayed = 0;
2971 mds = mdstat_read(0, 0);
ae0dcfbd 2972 for (m = mds; m; m = m->next)
4dd2df09 2973 if (strcmp(m->devnm, sra->sys_name) == 0) {
1f9b0e28
N
2974 if (m->resync &&
2975 m->percent == RESYNC_DELAYED)
2976 delayed = 1;
2977 if (m->resync == 0)
2978 /* Haven't started the reshape thread
2979 * yet, wait a bit
2980 */
2981 delayed = 2;
2982 break;
2983 }
2984 free_mdstat(mds);
2985 if (delayed == 1 && get_linux_version() < 3007000) {
2986 pr_err("Reshape is delayed, but cannot wait carefully with this kernel.\n"
2987 " You might experience problems until other reshapes complete.\n");
2988 delayed = 0;
2989 }
2990 if (delayed)
2991 sleep(30 - (delayed-1) * 25);
2992 } while (delayed);
2993
6b2d630c
N
2994 close(fd);
2995 if (check_env("MDADM_GROW_VERIFY"))
2996 fd = open(devname, O_RDONLY | O_DIRECT);
2997 else
2998 fd = -1;
2999 mlockall(MCL_FUTURE);
5da9ab98 3000
6b2d630c
N
3001 if (st->ss->external) {
3002 /* metadata handler takes it from here */
3003 done = st->ss->manage_reshape(
3004 fd, sra, &reshape, st, blocks,
3005 fdlist, offsets,
3006 d - odisks, fdlist+odisks,
3007 offsets+odisks);
3008 } else
3009 done = child_monitor(
3010 fd, sra, &reshape, st, blocks,
3011 fdlist, offsets,
3012 d - odisks, fdlist+odisks,
3013 offsets+odisks);
3014
d152f53e
JS
3015 free(fdlist);
3016 free(offsets);
3017
6b2d630c
N
3018 if (backup_file && done)
3019 unlink(backup_file);
3020 if (!done) {
3021 abort_reshape(sra);
3022 goto out;
3023 }
1cbc5680
N
3024
3025 if (!st->ss->external &&
3026 !(reshape.before.data_disks != reshape.after.data_disks
3027 && info->custom_array_size) &&
3028 info->new_level == reshape.level &&
3029 !forked) {
3030 /* no need to wait for the reshape to finish as
3031 * there is nothing more to do.
3032 */
d152f53e 3033 sysfs_free(sra);
1cbc5680
N
3034 exit(0);
3035 }
3036 wait_reshape(sra);
3037
3038 if (st->ss->external) {
3039 /* Re-load the metadata as much could have changed */
4dd2df09 3040 int cfd = open_dev(st->container_devnm);
1cbc5680 3041 if (cfd >= 0) {
78340e26 3042 flush_mdmon(container);
1cbc5680
N
3043 st->ss->free_super(st);
3044 st->ss->load_container(st, cfd, container);
3045 close(cfd);
3046 }
3047 }
3048
6b2d630c
N
3049 /* set new array size if required customer_array_size is used
3050 * by this metadata.
3051 */
3052 if (reshape.before.data_disks !=
3053 reshape.after.data_disks &&
54397ed9
AK
3054 info->custom_array_size)
3055 set_array_size(st, info, info->text_version);
582496b2 3056
6b2d630c 3057 if (info->new_level != reshape.level) {
582496b2 3058
6b2d630c 3059 c = map_num(pers, info->new_level);
1cbc5680
N
3060 if (c) {
3061 err = sysfs_set_str(sra, NULL, "level", c);
3062 if (err)
e7b84f9d
N
3063 pr_err("%s: could not set level "
3064 "to %s\n", devname, c);
1cbc5680 3065 }
e919fb0a
AK
3066 if (info->new_level == 0)
3067 st->update_tail = NULL;
7236ee7a 3068 }
6b2d630c 3069out:
d152f53e 3070 sysfs_free(sra);
6b2d630c
N
3071 if (forked)
3072 return 0;
e84f2c00 3073 unfreeze(st);
6b2d630c 3074 exit(0);
e86c9dd6 3075
6b2d630c 3076release:
d152f53e
JS
3077 free(fdlist);
3078 free(offsets);
1cbc5680 3079 if (orig_level != UnSet && sra) {
7236ee7a
N
3080 c = map_num(pers, orig_level);
3081 if (c && sysfs_set_str(sra, NULL, "level", c) == 0)
e7b84f9d 3082 pr_err("aborting level change\n");
7236ee7a 3083 }
d152f53e 3084 sysfs_free(sra);
9202b8eb
N
3085 if (!forked)
3086 unfreeze(st);
631d7405 3087 return 1;
7236ee7a 3088}
e86c9dd6 3089
9ad6f6e6
AK
3090/* mdfd handle is passed to be closed in child process (after fork).
3091 */
493f5dd6 3092int reshape_container(char *container, char *devname,
9ad6f6e6 3093 int mdfd,
24daa16f 3094 struct supertype *st,
5da9ab98
N
3095 struct mdinfo *info,
3096 int force,
3097 char *backup_file,
ba728be7 3098 int verbose, int restart, int freeze_reshape)
5da9ab98 3099{
1bb174ba 3100 struct mdinfo *cc = NULL;
bcc9e9ed 3101 int rv = restart;
4dd2df09 3102 char last_devnm[32] = "";
1bb174ba 3103
d8eb27f7 3104 /* component_size is not meaningful for a container,
7ec0996c 3105 * so pass '0' meaning 'no change'
d8eb27f7 3106 */
493f5dd6 3107 if (!restart &&
7ec0996c 3108 reshape_super(st, 0, info->new_level,
5da9ab98 3109 info->new_layout, info->new_chunk,
41784c88 3110 info->array.raid_disks, info->delta_disks,
016e00f5 3111 backup_file, devname, APPLY_METADATA_CHANGES,
ba728be7 3112 verbose)) {
9e325442 3113 unfreeze(st);
5da9ab98 3114 return 1;
9e325442 3115 }
5da9ab98
N
3116
3117 sync_metadata(st);
3118
1bb174ba
AK
3119 /* ping monitor to be sure that update is on disk
3120 */
3121 ping_monitor(container);
5da9ab98
N
3122
3123 switch (fork()) {
3124 case -1: /* error */
3125 perror("Cannot fork to complete reshape\n");
9e325442 3126 unfreeze(st);
5da9ab98
N
3127 return 1;
3128 default: /* parent */
b76b30e0
AK
3129 if (!freeze_reshape)
3130 printf(Name ": multi-array reshape continues"
3131 " in background\n");
5da9ab98
N
3132 return 0;
3133 case 0: /* child */
cc700db3 3134 map_fork();
5da9ab98
N
3135 break;
3136 }
3137
9ad6f6e6
AK
3138 /* close unused handle in child process
3139 */
3140 if (mdfd > -1)
3141 close(mdfd);
3142
1bb174ba
AK
3143 while(1) {
3144 /* For each member array with reshape_active,
3145 * we need to perform the reshape.
3146 * We pick the first array that needs reshaping and
3147 * reshape it. reshape_array() will re-read the metadata
3148 * so the next time through a different array should be
3149 * ready for reshape.
493f5dd6
N
3150 * It is possible that the 'different' array will not
3151 * be assembled yet. In that case we simple exit.
3152 * When it is assembled, the mdadm which assembles it
3153 * will take over the reshape.
1bb174ba
AK
3154 */
3155 struct mdinfo *content;
5da9ab98
N
3156 int fd;
3157 struct mdstat_ent *mdstat;
5da9ab98 3158 char *adev;
4dd2df09 3159 int devid;
5da9ab98 3160
1bb174ba 3161 sysfs_free(cc);
5da9ab98 3162
1bb174ba
AK
3163 cc = st->ss->container_content(st, NULL);
3164
3165 for (content = cc; content ; content = content->next) {
3166 char *subarray;
3167 if (!content->reshape_active)
3168 continue;
3169
3170 subarray = strchr(content->text_version+1, '/')+1;
4dd2df09 3171 mdstat = mdstat_by_subdev(subarray, container);
1bb174ba
AK
3172 if (!mdstat)
3173 continue;
1ca90aa6 3174 if (mdstat->active == 0) {
4dd2df09
N
3175 pr_err("Skipping inactive array %s.\n",
3176 mdstat->devnm);
1ca90aa6
AK
3177 free_mdstat(mdstat);
3178 mdstat = NULL;
3179 continue;
3180 }
1bb174ba
AK
3181 break;
3182 }
3183 if (!content)
3184 break;
5da9ab98 3185
4dd2df09
N
3186 devid = devnm2devid(mdstat->devnm);
3187 adev = map_dev(major(devid), minor(devid), 0);
5da9ab98 3188 if (!adev)
1bb174ba
AK
3189 adev = content->text_version;
3190
4dd2df09 3191 fd = open_dev(mdstat->devnm);
97a3490c
AK
3192 if (fd < 0) {
3193 printf(Name ": Device %s cannot be opened for reshape.",
3194 adev);
3195 break;
3196 }
3197
4dd2df09 3198 if (strcmp(last_devnm, mdstat->devnm) == 0) {
2d04d7e5
AK
3199 /* Do not allow for multiple reshape_array() calls for
3200 * the same array.
3201 * It can happen when reshape_array() returns without
3202 * error, when reshape is not finished (wrong reshape
3203 * starting/continuation conditions). Mdmon doesn't
3204 * switch to next array in container and reentry
3205 * conditions for the same array occur.
3206 * This is possibly interim until the behaviour of
3207 * reshape_array is resolved().
3208 */
3209 printf(Name ": Multiple reshape execution detected for "
3210 "device %s.", adev);
3211 close(fd);
3212 break;
3213 }
4dd2df09 3214 strcpy(last_devnm, mdstat->devnm);
2d04d7e5 3215
4dd2df09 3216 sysfs_init(content, fd, mdstat->devnm);
5da9ab98 3217
4dd2df09 3218 if (mdmon_running(container))
78340e26
AK
3219 flush_mdmon(container);
3220
1bb174ba 3221 rv = reshape_array(container, fd, adev, st,
19ceb16d 3222 content, force, NULL, 0ULL,
ba728be7 3223 backup_file, verbose, 1, restart,
b76b30e0 3224 freeze_reshape);
5da9ab98 3225 close(fd);
b76b30e0
AK
3226
3227 if (freeze_reshape) {
3228 sysfs_free(cc);
3229 exit(0);
3230 }
3231
493f5dd6 3232 restart = 0;
5da9ab98
N
3233 if (rv)
3234 break;
78340e26 3235
4dd2df09 3236 if (mdmon_running(container))
78340e26 3237 flush_mdmon(container);
5da9ab98 3238 }
bcc9e9ed
AK
3239 if (!rv)
3240 unfreeze(st);
1bb174ba 3241 sysfs_free(cc);
5da9ab98
N
3242 exit(0);
3243}
3244
7236ee7a
N
3245/*
3246 * We run a child process in the background which performs the following
3247 * steps:
3248 * - wait for resync to reach a certain point
3249 * - suspend io to the following section
3250 * - backup that section
3251 * - allow resync to proceed further
3252 * - resume io
3253 * - discard the backup.
3254 *
3255 * When are combined in slightly different ways in the three cases.
3256 * Grow:
3257 * - suspend/backup/allow/wait/resume/discard
3258 * Shrink:
3259 * - allow/wait/suspend/backup/allow/wait/resume/discard
3260 * same-size:
3261 * - wait/resume/discard/suspend/backup/allow
3262 *
3263 * suspend/backup/allow always come together
3264 * wait/resume/discard do too.
3265 * For the same-size case we have two backups to improve flow.
24daa16f 3266 *
7236ee7a 3267 */
e86c9dd6 3268
7443ee81
N
3269int progress_reshape(struct mdinfo *info, struct reshape *reshape,
3270 unsigned long long backup_point,
3271 unsigned long long wait_point,
3272 unsigned long long *suspend_point,
3273 unsigned long long *reshape_completed)
3274{
3275 /* This function is called repeatedly by the reshape manager.
3276 * It determines how much progress can safely be made and allows
3277 * that progress.
3278 * - 'info' identifies the array and particularly records in
3279 * ->reshape_progress the metadata's knowledge of progress
3280 * This is a sector offset from the start of the array
3281 * of the next array block to be relocated. This number
3282 * may increase from 0 or decrease from array_size, depending
3283 * on the type of reshape that is happening.
3284 * Note that in contrast, 'sync_completed' is a block count of the
3285 * reshape so far. It gives the distance between the start point
3286 * (head or tail of device) and the next place that data will be
3287 * written. It always increases.
3288 * - 'reshape' is the structure created by analyse_change
3289 * - 'backup_point' shows how much the metadata manager has backed-up
3290 * data. For reshapes with increasing progress, it is the next address
3291 * to be backed up, previous addresses have been backed-up. For
3292 * decreasing progress, it is the earliest address that has been
3293 * backed up - later address are also backed up.
3294 * So addresses between reshape_progress and backup_point are
3295 * backed up providing those are in the 'correct' order.
3296 * - 'wait_point' is an array address. When reshape_completed
3297 * passes this point, progress_reshape should return. It might
3298 * return earlier if it determines that ->reshape_progress needs
3299 * to be updated or further backup is needed.
3300 * - suspend_point is maintained by progress_reshape and the caller
3301 * should not touch it except to initialise to zero.
3302 * It is an array address and it only increases in 2.6.37 and earlier.
b6b95155 3303 * This makes it difficult to handle reducing reshapes with
7443ee81
N
3304 * external metadata.
3305 * However: it is similar to backup_point in that it records the
3306 * other end of a suspended region from reshape_progress.
3307 * it is moved to extend the region that is safe to backup and/or
3308 * reshape
3309 * - reshape_completed is read from sysfs and returned. The caller
3310 * should copy this into ->reshape_progress when it has reason to
3311 * believe that the metadata knows this, and any backup outside this
3312 * has been erased.
3313 *
3314 * Return value is:
3315 * 1 if more data from backup_point - but only as far as suspend_point,
3316 * should be backed up
3317 * 0 if things are progressing smoothly
9e034f70
N
3318 * -1 if the reshape is finished because it is all done,
3319 * -2 if the reshape is finished due to an error.
7443ee81
N
3320 */
3321
3322 int advancing = (reshape->after.data_disks
3323 >= reshape->before.data_disks);
38dad34a
N
3324 unsigned long long need_backup; /* All data between start of array and
3325 * here will at some point need to
3326 * be backed up.
d0ab945e 3327 */
7443ee81 3328 unsigned long long read_offset, write_offset;
b4f8e38b 3329 unsigned long long write_range;
7443ee81 3330 unsigned long long max_progress, target, completed;
d0ab945e
N
3331 unsigned long long array_size = (info->component_size
3332 * reshape->before.data_disks);
7443ee81 3333 int fd;
77a73a17 3334 char buf[20];
7443ee81
N
3335
3336 /* First, we unsuspend any region that is now known to be safe.
3337 * If suspend_point is on the 'wrong' side of reshape_progress, then
3338 * we don't have or need suspension at the moment. This is true for
3339 * native metadata when we don't need to back-up.
3340 */
3341 if (advancing) {
49cd738b 3342 if (info->reshape_progress <= *suspend_point)
7443ee81
N
3343 sysfs_set_num(info, NULL, "suspend_lo",
3344 info->reshape_progress);
3345 } else {
3346 /* Note: this won't work in 2.6.37 and before.
3347 * Something somewhere should make sure we don't need it!
3348 */
49cd738b 3349 if (info->reshape_progress >= *suspend_point)
7443ee81
N
3350 sysfs_set_num(info, NULL, "suspend_hi",
3351 info->reshape_progress);
3352 }
3353
3354 /* Now work out how far it is safe to progress.
3355 * If the read_offset for ->reshape_progress is less than
3356 * 'blocks' beyond the write_offset, we can only progress as far
3357 * as a backup.
3358 * Otherwise we can progress until the write_offset for the new location
3359 * reaches (within 'blocks' of) the read_offset at the current location.
3360 * However that region must be suspended unless we are using native
3361 * metadata.
3362 * If we need to suspend more, we limit it to 128M per device, which is
3363 * rather arbitrary and should be some time-based calculation.
3364 */
ec757320
N
3365 read_offset = info->reshape_progress / reshape->before.data_disks;
3366 write_offset = info->reshape_progress / reshape->after.data_disks;
b4f8e38b 3367 write_range = info->new_chunk/512;
38dad34a
N
3368 if (reshape->before.data_disks == reshape->after.data_disks)
3369 need_backup = array_size;
3370 else
3371 need_backup = reshape->backup_blocks;
7443ee81 3372 if (advancing) {
38dad34a 3373 if (read_offset < write_offset + write_range)
7443ee81 3374 max_progress = backup_point;
ddee071d 3375 else
7443ee81 3376 max_progress =
2f3dd5e4
N
3377 read_offset *
3378 reshape->after.data_disks;
7443ee81 3379 } else {
38dad34a
N
3380 if (read_offset > write_offset - write_range)
3381 /* Can only progress as far as has been backed up,
3382 * which must be suspended */
7443ee81 3383 max_progress = backup_point;
38dad34a
N
3384 else if (info->reshape_progress <= need_backup)
3385 max_progress = backup_point;
3386 else {
3387 if (info->array.major_version >= 0)
3388 /* Can progress until backup is needed */
3389 max_progress = need_backup;
3390 else {
3391 /* Can progress until metadata update is required */
3392 max_progress =
3393 read_offset *
3394 reshape->after.data_disks;
3395 /* but data must be suspended */
3396 if (max_progress < *suspend_point)
3397 max_progress = *suspend_point;
3398 }
7443ee81
N
3399 }
3400 }
3401
3402 /* We know it is safe to progress to 'max_progress' providing
3403 * it is suspended or we are using native metadata.
3404 * Consider extending suspend_point 128M per device if it
3405 * is less than 64M per device beyond reshape_progress.
3406 * But always do a multiple of 'blocks'
832b2b9a
N
3407 * FIXME this is too big - it takes to long to complete
3408 * this much.
7443ee81
N
3409 */
3410 target = 64*1024*2 * min(reshape->before.data_disks,
24daa16f 3411 reshape->after.data_disks);
b4f8e38b 3412 target /= reshape->backup_blocks;
7443ee81
N
3413 if (target < 2)
3414 target = 2;
b4f8e38b 3415 target *= reshape->backup_blocks;
7443ee81
N
3416
3417 /* For externally managed metadata we always need to suspend IO to
3418 * the area being reshaped so we regularly push suspend_point forward.
3419 * For native metadata we only need the suspend if we are going to do
3420 * a backup.
3421 */
3422 if (advancing) {
d0ab945e
N
3423 if ((need_backup > info->reshape_progress
3424 || info->array.major_version < 0) &&
7443ee81 3425 *suspend_point < info->reshape_progress + target) {
d0ab945e
N
3426 if (need_backup < *suspend_point + 2 * target)
3427 *suspend_point = need_backup;
3428 else if (*suspend_point + 2 * target < array_size)
7443ee81 3429 *suspend_point += 2 * target;
d0ab945e
N
3430 else
3431 *suspend_point = array_size;
7443ee81 3432 sysfs_set_num(info, NULL, "suspend_hi", *suspend_point);
d0ab945e
N
3433 if (max_progress > *suspend_point)
3434 max_progress = *suspend_point;
7443ee81
N
3435 }
3436 } else {
38dad34a
N
3437 if (info->array.major_version >= 0) {
3438 /* Only need to suspend when about to backup */
3439 if (info->reshape_progress < need_backup * 2 &&
3440 *suspend_point > 0) {
d0ab945e 3441 *suspend_point = 0;
38dad34a
N
3442 sysfs_set_num(info, NULL, "suspend_lo", 0);
3443 sysfs_set_num(info, NULL, "suspend_hi", need_backup);
3444 }
3445 } else {
3446 /* Need to suspend continually */
3447 if (info->reshape_progress < *suspend_point)
3448 *suspend_point = info->reshape_progress;
3449 if (*suspend_point + target < info->reshape_progress)
3450 /* No need to move suspend region yet */;
3451 else {
3452 if (*suspend_point >= 2 * target)
3453 *suspend_point -= 2 * target;
3454 else
3455 *suspend_point = 0;
3456 sysfs_set_num(info, NULL, "suspend_lo",
3457 *suspend_point);
3458 }
d0ab945e
N
3459 if (max_progress < *suspend_point)
3460 max_progress = *suspend_point;
7443ee81
N
3461 }
3462 }
3463
3464 /* now set sync_max to allow that progress. sync_max, like
3465 * sync_completed is a count of sectors written per device, so
3466 * we find the difference between max_progress and the start point,
3467 * and divide that by after.data_disks to get a sync_max
3468 * number.
3469 * At the same time we convert wait_point to a similar number
3470 * for comparing against sync_completed.
3471 */
92d1991f 3472 /* scale down max_progress to per_disk */
7443ee81 3473 max_progress /= reshape->after.data_disks;
92d1991f
N
3474 /* Round to chunk size as some kernels give an erroneously high number */
3475 max_progress /= info->new_chunk/512;
3476 max_progress *= info->new_chunk/512;
7f913e9b
N
3477 /* And round to old chunk size as the kernel wants that */
3478 max_progress /= info->array.chunk_size/512;
3479 max_progress *= info->array.chunk_size/512;
92d1991f
N
3480 /* Limit progress to the whole device */
3481 if (max_progress > info->component_size)
3482 max_progress = info->component_size;
7443ee81 3483 wait_point /= reshape->after.data_disks;
92d1991f
N
3484 if (!advancing) {
3485 /* switch from 'device offset' to 'processed block count' */
3486 max_progress = info->component_size - max_progress;
3487 wait_point = info->component_size - wait_point;
3488 }
7443ee81
N
3489
3490 sysfs_set_num(info, NULL, "sync_max", max_progress);
3491
3492 /* Now wait. If we have already reached the point that we were
3493 * asked to wait to, don't wait at all, else wait for any change.
3494 * We need to select on 'sync_completed' as that is the place that
3495 * notifications happen, but we are really interested in
3496 * 'reshape_position'
3497 */
3498 fd = sysfs_get_fd(info, NULL, "sync_completed");
3499 if (fd < 0)
77a73a17 3500 goto check_progress;
7443ee81 3501
621ea11b 3502 if (sysfs_fd_get_ll(fd, &completed) < 0)
77a73a17 3503 goto check_progress;
621ea11b 3504
7443ee81
N
3505 while (completed < max_progress && completed < wait_point) {
3506 /* Check that sync_action is still 'reshape' to avoid
3507 * waiting forever on a dead array
3508 */
3509 char action[20];
3510 fd_set rfds;
3511 if (sysfs_get_str(info, NULL, "sync_action",
3512 action, 20) <= 0 ||
3513 strncmp(action, "reshape", 7) != 0)
3514 break;
26d6e157
AK
3515 /* Some kernels reset 'sync_completed' to zero
3516 * before setting 'sync_action' to 'idle'.
3517 * So we need these extra tests.
3518 */
3519 if (completed == 0 && advancing
3520 && info->reshape_progress > 0)
3521 break;
3522 if (completed == 0 && !advancing
3523 && info->reshape_progress < (info->component_size
3524 * reshape->after.data_disks))
3525 break;
7443ee81
N
3526 FD_ZERO(&rfds);
3527 FD_SET(fd, &rfds);
3528 select(fd+1, NULL, NULL, &rfds, NULL);
621ea11b 3529 if (sysfs_fd_get_ll(fd, &completed) < 0)
77a73a17 3530 goto check_progress;
7443ee81 3531 }
10d0d365
AK
3532 /* Some kernels reset 'sync_completed' to zero,
3533 * we need to have real point we are in md
3534 */
3535 if (completed == 0)
3536 completed = max_progress;
3537
92d1991f
N
3538 /* some kernels can give an incorrectly high 'completed' number */
3539 completed /= (info->new_chunk/512);
3540 completed *= (info->new_chunk/512);
7443ee81
N
3541 /* Convert 'completed' back in to a 'progress' number */
3542 completed *= reshape->after.data_disks;
3543 if (!advancing) {
3544 completed = info->component_size * reshape->after.data_disks
3545 - completed;
3546 }
3547 *reshape_completed = completed;
24daa16f 3548
7443ee81
N
3549 close(fd);
3550
3551 /* We return the need_backup flag. Caller will decide
d0ab945e 3552 * how much - a multiple of ->backup_blocks up to *suspend_point
7443ee81 3553 */
38dad34a
N
3554 if (advancing)
3555 return need_backup > info->reshape_progress;
3556 else
3557 return need_backup >= info->reshape_progress;
77a73a17
N
3558
3559check_progress:
3560 /* if we couldn't read a number from sync_completed, then
3561 * either the reshape did complete, or it aborted.
3562 * We can tell which by checking for 'none' in reshape_position.
621ea11b
N
3563 * If it did abort, then it might immediately restart if it
3564 * it was just a device failure that leaves us degraded but
3565 * functioning.
77a73a17
N
3566 */
3567 strcpy(buf, "hi");
3568 if (sysfs_get_str(info, NULL, "reshape_position", buf, sizeof(buf)) < 0
621ea11b
N
3569 || strncmp(buf, "none", 4) != 0) {
3570 /* The abort might only be temporary. Wait up to 10
3571 * seconds for fd to contain a valid number again.
3572 */
3573 struct timeval tv;
3574 int rv = -2;
3575 tv.tv_sec = 10;
3576 tv.tv_usec = 0;
6560987b 3577 while (fd >= 0 && rv < 0 && tv.tv_sec > 0) {
621ea11b
N
3578 fd_set rfds;
3579 FD_ZERO(&rfds);
3580 FD_SET(fd, &rfds);
3581 if (select(fd+1, NULL, NULL, &rfds, &tv) != 1)
3582 break;
6560987b
N
3583 switch (sysfs_fd_get_ll(fd, &completed)) {
3584 case 0:
621ea11b
N
3585 /* all good again */
3586 rv = 1;
6560987b
N
3587 break;
3588 case -2: /* read error - abort */
3589 tv.tv_sec = 0;
3590 break;
3591 }
621ea11b
N
3592 }
3593 if (fd >= 0)
3594 close(fd);
3595 return rv; /* abort */
3596 } else {
6d5316f6 3597 /* Maybe racing with array shutdown - check state */
621ea11b
N
3598 if (fd >= 0)
3599 close(fd);
6d5316f6
N
3600 if (sysfs_get_str(info, NULL, "array_state", buf, sizeof(buf)) < 0
3601 || strncmp(buf, "inactive", 8) == 0
3602 || strncmp(buf, "clear",5) == 0)
3603 return -2; /* abort */
77a73a17 3604 return -1; /* complete */
6d5316f6 3605 }
7443ee81
N
3606}
3607
fcf57625 3608/* FIXME return status is never checked */
4411fb17 3609static int grow_backup(struct mdinfo *sra,
7236ee7a 3610 unsigned long long offset, /* per device */
7443ee81 3611 unsigned long stripes, /* per device, in old chunks */
7236ee7a
N
3612 int *sources, unsigned long long *offsets,
3613 int disks, int chunk, int level, int layout,
3614 int dests, int *destfd, unsigned long long *destoffsets,
d4445387 3615 int part, int *degraded,
7236ee7a
N
3616 char *buf)
3617{
3618 /* Backup 'blocks' sectors at 'offset' on each device of the array,
3619 * to storage 'destfd' (offset 'destoffsets'), after first
3620 * suspending IO. Then allow resync to continue
3621 * over the suspended section.
3622 * Use part 'part' of the backup-super-block.
3623 */
3624 int odata = disks;
3625 int rv = 0;
3626 int i;
f21e18ca
N
3627 unsigned long long ll;
3628 int new_degraded;
7236ee7a
N
3629 //printf("offset %llu\n", offset);
3630 if (level >= 4)
3631 odata--;
3632 if (level == 6)
3633 odata--;
7443ee81 3634
d4445387 3635 /* Check that array hasn't become degraded, else we might backup the wrong data */
2c6ac128
N
3636 if (sysfs_get_ll(sra, NULL, "degraded", &ll) < 0)
3637 return -1; /* FIXME this error is ignored */
f21e18ca 3638 new_degraded = (int)ll;
d4445387
N
3639 if (new_degraded != *degraded) {
3640 /* check each device to ensure it is still working */
3641 struct mdinfo *sd;
3642 for (sd = sra->devs ; sd ; sd = sd->next) {
3643 if (sd->disk.state & (1<<MD_DISK_FAULTY))
3644 continue;
3645 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
3646 char sbuf[20];
3647 if (sysfs_get_str(sra, sd, "state", sbuf, 20) < 0 ||
3648 strstr(sbuf, "faulty") ||
3649 strstr(sbuf, "in_sync") == NULL) {
3650 /* this device is dead */
3651 sd->disk.state = (1<<MD_DISK_FAULTY);
3652 if (sd->disk.raid_disk >= 0 &&
3653 sources[sd->disk.raid_disk] >= 0) {
3654 close(sources[sd->disk.raid_disk]);
3655 sources[sd->disk.raid_disk] = -1;
3656 }
3657 }
3658 }
3659 }
3660 *degraded = new_degraded;
3661 }
7236ee7a
N
3662 if (part) {
3663 bsb.arraystart2 = __cpu_to_le64(offset * odata);
200871ad 3664 bsb.length2 = __cpu_to_le64(stripes * (chunk/512) * odata);
7236ee7a
N
3665 } else {
3666 bsb.arraystart = __cpu_to_le64(offset * odata);
200871ad 3667 bsb.length = __cpu_to_le64(stripes * (chunk/512) * odata);
7236ee7a
N
3668 }
3669 if (part)
3670 bsb.magic[15] = '2';
3671 for (i = 0; i < dests; i++)
3672 if (part)
3673 lseek64(destfd[i], destoffsets[i] + __le64_to_cpu(bsb.devstart2)*512, 0);
3674 else
3675 lseek64(destfd[i], destoffsets[i], 0);
3676
24daa16f 3677 rv = save_stripes(sources, offsets,
7236ee7a
N
3678 disks, chunk, level, layout,
3679 dests, destfd,
3680 offset*512*odata, stripes * chunk * odata,
3681 buf);
3682
3683 if (rv)
3684 return rv;
97396422 3685 bsb.mtime = __cpu_to_le64(time(0));
7236ee7a
N
3686 for (i = 0; i < dests; i++) {
3687 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
3688
3689 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
3690 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
3691 bsb.sb_csum2 = bsb_csum((char*)&bsb,
3692 ((char*)&bsb.sb_csum2)-((char*)&bsb));
3693
72044953 3694 rv = -1;
f21e18ca
N
3695 if ((unsigned long long)lseek64(destfd[i], destoffsets[i] - 4096, 0)
3696 != destoffsets[i] - 4096)
72044953
N
3697 break;
3698 if (write(destfd[i], &bsb, 512) != 512)
3699 break;
ff94fb86 3700 if (destoffsets[i] > 4096) {
f21e18ca 3701 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]+stripes*chunk*odata, 0) !=
a847575a 3702 destoffsets[i]+stripes*chunk*odata)
72044953
N
3703 break;
3704 if (write(destfd[i], &bsb, 512) != 512)
3705 break;
ff94fb86 3706 }
7236ee7a 3707 fsync(destfd[i]);
72044953 3708 rv = 0;
7236ee7a 3709 }
2efedc7b 3710
fcf57625 3711 return rv;
7236ee7a
N
3712}
3713
3714/* in 2.6.30, the value reported by sync_completed can be
3715 * less that it should be by one stripe.
3716 * This only happens when reshape hits sync_max and pauses.
3717 * So allow wait_backup to either extent sync_max further
3718 * than strictly necessary, or return before the
3719 * sync has got quite as far as we would really like.
3720 * This is what 'blocks2' is for.
3721 * The various caller give appropriate values so that
3722 * every works.
3723 */
fcf57625 3724/* FIXME return value is often ignored */
24daa16f
N
3725static int forget_backup(int dests, int *destfd,
3726 unsigned long long *destoffsets,
3727 int part)
7236ee7a 3728{
24daa16f 3729 /*
7443ee81 3730 * Erase backup 'part' (which is 0 or 1)
7236ee7a 3731 */
7236ee7a 3732 int i;
fcf57625 3733 int rv;
7236ee7a 3734
7236ee7a
N
3735 if (part) {
3736 bsb.arraystart2 = __cpu_to_le64(0);
3737 bsb.length2 = __cpu_to_le64(0);
3738 } else {
3739 bsb.arraystart = __cpu_to_le64(0);
3740 bsb.length = __cpu_to_le64(0);
3741 }
97396422 3742 bsb.mtime = __cpu_to_le64(time(0));
fcf57625 3743 rv = 0;
7236ee7a
N
3744 for (i = 0; i < dests; i++) {
3745 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
3746 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
3747 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
3748 bsb.sb_csum2 = bsb_csum((char*)&bsb,
3749 ((char*)&bsb.sb_csum2)-((char*)&bsb));
f21e18ca 3750 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]-4096, 0) !=
a847575a 3751 destoffsets[i]-4096)
72044953 3752 rv = -1;
24daa16f 3753 if (rv == 0 &&
72044953
N
3754 write(destfd[i], &bsb, 512) != 512)
3755 rv = -1;
7236ee7a
N
3756 fsync(destfd[i]);
3757 }
fcf57625 3758 return rv;
7236ee7a 3759}
e86c9dd6 3760
7236ee7a
N
3761static void fail(char *msg)
3762{
fcf57625 3763 int rv;
72044953
N
3764 rv = (write(2, msg, strlen(msg)) != (int)strlen(msg));
3765 rv |= (write(2, "\n", 1) != 1);
fcf57625 3766 exit(rv ? 1 : 2);
7236ee7a
N
3767}
3768
3769static char *abuf, *bbuf;
f21e18ca 3770static unsigned long long abuflen;
7236ee7a
N
3771static void validate(int afd, int bfd, unsigned long long offset)
3772{
3773 /* check that the data in the backup against the array.
3774 * This is only used for regression testing and should not
3775 * be used while the array is active
3776 */
7236ee7a
N
3777 if (afd < 0)
3778 return;
3779 lseek64(bfd, offset - 4096, 0);
3780 if (read(bfd, &bsb2, 512) != 512)
3781 fail("cannot read bsb");
3782 if (bsb2.sb_csum != bsb_csum((char*)&bsb2,
3783 ((char*)&bsb2.sb_csum)-((char*)&bsb2)))
3784 fail("first csum bad");
3785 if (memcmp(bsb2.magic, "md_backup_data", 14) != 0)
3786 fail("magic is bad");
3787 if (memcmp(bsb2.magic, "md_backup_data-2", 16) == 0 &&
3788 bsb2.sb_csum2 != bsb_csum((char*)&bsb2,
24daa16f 3789 ((char*)&bsb2.sb_csum2)-((char*)&bsb2)))
7236ee7a
N
3790 fail("second csum bad");
3791
3792 if (__le64_to_cpu(bsb2.devstart)*512 != offset)
3793 fail("devstart is wrong");
3794
3795 if (bsb2.length) {
3796 unsigned long long len = __le64_to_cpu(bsb2.length)*512;
3797
3798 if (abuflen < len) {
3799 free(abuf);
3800 free(bbuf);
3801 abuflen = len;
fcf57625
N
3802 if (posix_memalign((void**)&abuf, 4096, abuflen) ||
3803 posix_memalign((void**)&bbuf, 4096, abuflen)) {
3804 abuflen = 0;
3805 /* just stop validating on mem-alloc failure */
3806 return;
3807 }
e86c9dd6 3808 }
48924014 3809
7236ee7a 3810 lseek64(bfd, offset, 0);
f21e18ca 3811 if ((unsigned long long)read(bfd, bbuf, len) != len) {
080fd005 3812 //printf("len %llu\n", len);
7236ee7a
N
3813 fail("read first backup failed");
3814 }
3815 lseek64(afd, __le64_to_cpu(bsb2.arraystart)*512, 0);
f21e18ca 3816 if ((unsigned long long)read(afd, abuf, len) != len)
7236ee7a
N
3817 fail("read first from array failed");
3818 if (memcmp(bbuf, abuf, len) != 0) {
24daa16f 3819#if 0
7236ee7a
N
3820 int i;
3821 printf("offset=%llu len=%llu\n",
080fd005 3822 (unsigned long long)__le64_to_cpu(bsb2.arraystart)*512, len);
7236ee7a
N
3823 for (i=0; i<len; i++)
3824 if (bbuf[i] != abuf[i]) {
3825 printf("first diff byte %d\n", i);
93ecfa01 3826 break;
7236ee7a 3827 }
24daa16f 3828#endif
7236ee7a 3829 fail("data1 compare failed");
e86c9dd6 3830 }
7236ee7a
N
3831 }
3832 if (bsb2.length2) {
3833 unsigned long long len = __le64_to_cpu(bsb2.length2)*512;
3834
3835 if (abuflen < len) {
3836 free(abuf);
3837 free(bbuf);
3838 abuflen = len;
503975b9
N
3839 abuf = xmalloc(abuflen);
3840 bbuf = xmalloc(abuflen);
e86c9dd6
NB
3841 }
3842
7236ee7a 3843 lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0);
f21e18ca 3844 if ((unsigned long long)read(bfd, bbuf, len) != len)
7236ee7a
N
3845 fail("read second backup failed");
3846 lseek64(afd, __le64_to_cpu(bsb2.arraystart2)*512, 0);
f21e18ca 3847 if ((unsigned long long)read(afd, abuf, len) != len)
7236ee7a
N
3848 fail("read second from array failed");
3849 if (memcmp(bbuf, abuf, len) != 0)
3850 fail("data2 compare failed");
e86c9dd6 3851 }
7236ee7a 3852}
e86c9dd6 3853
999b4972
N
3854int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
3855 struct supertype *st, unsigned long blocks,
3856 int *fds, unsigned long long *offsets,
3857 int dests, int *destfd, unsigned long long *destoffsets)
7236ee7a 3858{
7443ee81
N
3859 /* Monitor a reshape where backup is being performed using
3860 * 'native' mechanism - either to a backup file, or
3861 * to some space in a spare.
3862 */
7236ee7a 3863 char *buf;
7443ee81
N
3864 int degraded = -1;
3865 unsigned long long speed;
3866 unsigned long long suspend_point, array_size;
3867 unsigned long long backup_point, wait_point;
3868 unsigned long long reshape_completed;
3869 int done = 0;
3870 int increasing = reshape->after.data_disks >= reshape->before.data_disks;
3871 int part = 0; /* The next part of the backup area to fill. It may already
3872 * be full, so we need to check */
3873 int level = reshape->level;
3874 int layout = reshape->before.layout;
3875 int data = reshape->before.data_disks;
3876 int disks = reshape->before.data_disks + reshape->parity;
3877 int chunk = sra->array.chunk_size;
da8100ca
N
3878 struct mdinfo *sd;
3879 unsigned long stripes;
3b7e9d0c 3880 int uuid[4];
da8100ca
N
3881
3882 /* set up the backup-super-block. This requires the
3883 * uuid from the array.
3884 */
3885 /* Find a superblock */
3886 for (sd = sra->devs; sd; sd = sd->next) {
3887 char *dn;
3888 int devfd;
3889 int ok;
3890 if (sd->disk.state & (1<<MD_DISK_FAULTY))
3891 continue;
3892 dn = map_dev(sd->disk.major, sd->disk.minor, 1);
3893 devfd = dev_open(dn, O_RDONLY);
3894 if (devfd < 0)
3895 continue;
3896 ok = st->ss->load_super(st, devfd, NULL);
3897 close(devfd);
77f8d358 3898 if (ok == 0)
da8100ca
N
3899 break;
3900 }
3901 if (!sd) {
e7b84f9d 3902 pr_err("Cannot find a superblock\n");
da8100ca
N
3903 return 0;
3904 }
3905
3906 memset(&bsb, 0, 512);
3907 memcpy(bsb.magic, "md_backup_data-1", 16);
3b7e9d0c
LB
3908 st->ss->uuid_from_super(st, uuid);
3909 memcpy(bsb.set_uuid, uuid, 16);
da8100ca
N
3910 bsb.mtime = __cpu_to_le64(time(0));
3911 bsb.devstart2 = blocks;
3912
3913 stripes = blocks / (sra->array.chunk_size/512) /
3914 reshape->before.data_disks;
e86c9dd6 3915
fcf57625
N
3916 if (posix_memalign((void**)&buf, 4096, disks * chunk))
3917 /* Don't start the 'reshape' */
3918 return 0;
7443ee81
N
3919 if (reshape->before.data_disks == reshape->after.data_disks) {
3920 sysfs_get_ll(sra, NULL, "sync_speed_min", &speed);
3921 sysfs_set_num(sra, NULL, "sync_speed_min", 200000);
3922 }
e86c9dd6 3923
7443ee81 3924 if (increasing) {
96533072 3925 array_size = sra->component_size * reshape->after.data_disks;
7443ee81
N
3926 backup_point = sra->reshape_progress;
3927 suspend_point = 0;
3928 } else {
96533072 3929 array_size = sra->component_size * reshape->before.data_disks;
25da62d9 3930 backup_point = reshape->backup_blocks;
7443ee81
N
3931 suspend_point = array_size;
3932 }
7236ee7a 3933
7443ee81
N
3934 while (!done) {
3935 int rv;
7236ee7a 3936
7443ee81
N
3937 /* Want to return as soon the oldest backup slot can
3938 * be released as that allows us to start backing up
3939 * some more, providing suspend_point has been
b6b95155 3940 * advanced, which it should have.
7443ee81
N
3941 */
3942 if (increasing) {
3943 wait_point = array_size;
3944 if (part == 0 && __le64_to_cpu(bsb.length) > 0)
3945 wait_point = (__le64_to_cpu(bsb.arraystart) +
3946 __le64_to_cpu(bsb.length));
3947 if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
3948 wait_point = (__le64_to_cpu(bsb.arraystart2) +
3949 __le64_to_cpu(bsb.length2));
3950 } else {
3951 wait_point = 0;
3952 if (part == 0 && __le64_to_cpu(bsb.length) > 0)
3953 wait_point = __le64_to_cpu(bsb.arraystart);
3954 if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
3955 wait_point = __le64_to_cpu(bsb.arraystart2);
3956 }
3957
3958 rv = progress_reshape(sra, reshape,
3959 backup_point, wait_point,
3960 &suspend_point, &reshape_completed);
7443ee81
N
3961 /* external metadata would need to ping_monitor here */
3962 sra->reshape_progress = reshape_completed;
7236ee7a 3963
7443ee81
N
3964 /* Clear any backup region that is before 'here' */
3965 if (increasing) {
b3cf095a
N
3966 if (__le64_to_cpu(bsb.length) > 0 &&
3967 reshape_completed >= (__le64_to_cpu(bsb.arraystart) +
7443ee81
N
3968 __le64_to_cpu(bsb.length)))
3969 forget_backup(dests, destfd,
3970 destoffsets, 0);
b3cf095a
N
3971 if (__le64_to_cpu(bsb.length2) > 0 &&
3972 reshape_completed >= (__le64_to_cpu(bsb.arraystart2) +
7443ee81
N
3973 __le64_to_cpu(bsb.length2)))
3974 forget_backup(dests, destfd,
3975 destoffsets, 1);
3976 } else {
b3cf095a
N
3977 if (__le64_to_cpu(bsb.length) > 0 &&
3978 reshape_completed <= (__le64_to_cpu(bsb.arraystart)))
7443ee81
N
3979 forget_backup(dests, destfd,
3980 destoffsets, 0);
b3cf095a
N
3981 if (__le64_to_cpu(bsb.length2) > 0 &&
3982 reshape_completed <= (__le64_to_cpu(bsb.arraystart2)))
7443ee81
N
3983 forget_backup(dests, destfd,
3984 destoffsets, 1);
3985 }
7236ee7a 3986
223c5c99 3987 if (rv < 0) {
77a73a17
N
3988 if (rv == -1)
3989 done = 1;
223c5c99
N
3990 break;
3991 }
2d4de5f9
N
3992 if (rv == 0 && increasing && !st->ss->external) {
3993 /* No longer need to monitor this reshape */
3994 done = 1;
3995 break;
3996 }
223c5c99 3997
60204e4e 3998 while (rv) {
7443ee81 3999 unsigned long long offset;
e97ca002 4000 unsigned long actual_stripes;
60204e4e
N
4001 /* Need to backup some data.
4002 * If 'part' is not used and the desired
4003 * backup size is suspended, do a backup,
4004 * then consider the next part.
4005 */
7443ee81
N
4006 /* Check that 'part' is unused */
4007 if (part == 0 && __le64_to_cpu(bsb.length) != 0)
60204e4e 4008 break;
7443ee81 4009 if (part == 1 && __le64_to_cpu(bsb.length2) != 0)
60204e4e 4010 break;
7443ee81
N
4011
4012 offset = backup_point / data;
e97ca002 4013 actual_stripes = stripes;
60204e4e 4014 if (increasing) {
e97ca002
N
4015 if (offset + actual_stripes * (chunk/512) >
4016 sra->component_size)
4017 actual_stripes = ((sra->component_size - offset)
4018 / (chunk/512));
4019 if (offset + actual_stripes * (chunk/512) >
60204e4e
N
4020 suspend_point/data)
4021 break;
4022 } else {
e97ca002
N
4023 if (offset < actual_stripes * (chunk/512))
4024 actual_stripes = offset / (chunk/512);
4025 offset -= actual_stripes * (chunk/512);
4026 if (offset < suspend_point/data)
60204e4e
N
4027 break;
4028 }
e4b11073
N
4029 if (actual_stripes == 0)
4030 break;
e97ca002 4031 grow_backup(sra, offset, actual_stripes,
7443ee81
N
4032 fds, offsets,
4033 disks, chunk, level, layout,
4034 dests, destfd, destoffsets,
4035 part, &degraded, buf);
4036 validate(afd, destfd[0], destoffsets[0]);
4037 /* record where 'part' is up to */
4038 part = !part;
4039 if (increasing)
e97ca002 4040 backup_point += actual_stripes * (chunk/512) * data;
7443ee81 4041 else
e97ca002 4042 backup_point -= actual_stripes * (chunk/512) * data;
7443ee81
N
4043 }
4044 }
4045
223c5c99
N
4046 /* FIXME maybe call progress_reshape one more time instead */
4047 abort_reshape(sra); /* remove any remaining suspension */
7443ee81
N
4048 if (reshape->before.data_disks == reshape->after.data_disks)
4049 sysfs_set_num(sra, NULL, "sync_speed_min", speed);
7236ee7a 4050 free(buf);
77a73a17 4051 return done;
e86c9dd6 4052}
353632d9
NB
4053
4054/*
4055 * If any spare contains md_back_data-1 which is recent wrt mtime,
4056 * write that data into the array and update the super blocks with
4057 * the new reshape_progress
4058 */
ea0ebe96
N
4059int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt,
4060 char *backup_file, int verbose)
353632d9
NB
4061{
4062 int i, j;
4063 int old_disks;
353632d9 4064 unsigned long long *offsets;
82f2d6ab 4065 unsigned long long nstripe, ostripe;
6e9eac4f 4066 int ndata, odata;
353632d9 4067
e9e43ec3
N
4068 odata = info->array.raid_disks - info->delta_disks - 1;
4069 if (info->array.level == 6) odata--; /* number of data disks */
4070 ndata = info->array.raid_disks - 1;
4071 if (info->new_level == 6) ndata--;
353632d9
NB
4072
4073 old_disks = info->array.raid_disks - info->delta_disks;
4074
e9e43ec3
N
4075 if (info->delta_disks <= 0)
4076 /* Didn't grow, so the backup file must have
4077 * been used
4078 */
4079 old_disks = cnt;
06b0d786 4080 for (i=old_disks-(backup_file?1:0); i<cnt; i++) {
353632d9 4081 struct mdinfo dinfo;
06b0d786 4082 int fd;
e9e43ec3 4083 int bsbsize;
ea0ebe96 4084 char *devname, namebuf[20];
10af14c4 4085 unsigned long long lo, hi;
353632d9
NB
4086
4087 /* This was a spare and may have some saved data on it.
4088 * Load the superblock, find and load the
4089 * backup_super_block.
4090 * If either fail, go on to next device.
4091 * If the backup contains no new info, just return
206c5eae 4092 * else restore data and update all superblocks
353632d9 4093 */
06b0d786
NB
4094 if (i == old_disks-1) {
4095 fd = open(backup_file, O_RDONLY);
e9e43ec3 4096 if (fd<0) {
e7b84f9d 4097 pr_err("backup file %s inaccessible: %s\n",
e9e43ec3 4098 backup_file, strerror(errno));
06b0d786 4099 continue;
e9e43ec3 4100 }
ea0ebe96 4101 devname = backup_file;
06b0d786
NB
4102 } else {
4103 fd = fdlist[i];
4104 if (fd < 0)
4105 continue;
3da92f27 4106 if (st->ss->load_super(st, fd, NULL))
06b0d786 4107 continue;
353632d9 4108
a5d85af7 4109 st->ss->getinfo_super(st, &dinfo, NULL);
3da92f27
NB
4110 st->ss->free_super(st);
4111
06b0d786
NB
4112 if (lseek64(fd,
4113 (dinfo.data_offset + dinfo.component_size - 8) <<9,
ea0ebe96 4114 0) < 0) {
e7b84f9d 4115 pr_err("Cannot seek on device %d\n", i);
06b0d786 4116 continue; /* Cannot seek */
ea0ebe96
N
4117 }
4118 sprintf(namebuf, "device-%d", i);
4119 devname = namebuf;
06b0d786 4120 }
ea0ebe96
N
4121 if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb)) {
4122 if (verbose)
e7b84f9d 4123 pr_err("Cannot read from %s\n", devname);
353632d9 4124 continue; /* Cannot read */
ea0ebe96 4125 }
e9e43ec3 4126 if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0 &&
ea0ebe96
N
4127 memcmp(bsb.magic, "md_backup_data-2", 16) != 0) {
4128 if (verbose)
e7b84f9d 4129 pr_err("No backup metadata on %s\n", devname);
353632d9 4130 continue;
ea0ebe96
N
4131 }
4132 if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb))) {
4133 if (verbose)
e7b84f9d 4134 pr_err("Bad backup-metadata checksum on %s\n", devname);
353632d9 4135 continue; /* bad checksum */
ea0ebe96 4136 }
e9e43ec3 4137 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0 &&
ea0ebe96
N
4138 bsb.sb_csum2 != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum2)-((char*)&bsb))) {
4139 if (verbose)
e7b84f9d 4140 pr_err("Bad backup-metadata checksum2 on %s\n", devname);
22e30516 4141 continue; /* Bad second checksum */
ea0ebe96
N
4142 }
4143 if (memcmp(bsb.set_uuid,info->uuid, 16) != 0) {
4144 if (verbose)
e7b84f9d 4145 pr_err("Wrong uuid on backup-metadata on %s\n", devname);
353632d9 4146 continue; /* Wrong uuid */
ea0ebe96 4147 }
353632d9 4148
097075b6
N
4149 /* array utime and backup-mtime should be updated at much the same time, but it seems that
4150 * sometimes they aren't... So allow considerable flexability in matching, and allow
4151 * this test to be overridden by an environment variable.
4152 */
f21e18ca
N
4153 if (info->array.utime > (int)__le64_to_cpu(bsb.mtime) + 2*60*60 ||
4154 info->array.utime < (int)__le64_to_cpu(bsb.mtime) - 10*60) {
097075b6 4155 if (check_env("MDADM_GROW_ALLOW_OLD")) {
e7b84f9d 4156 pr_err("accepting backup with timestamp %lu "
097075b6
N
4157 "for array with timestamp %lu\n",
4158 (unsigned long)__le64_to_cpu(bsb.mtime),
4159 (unsigned long)info->array.utime);
4160 } else {
676ab312
N
4161 pr_err("too-old timestamp on backup-metadata on %s\n", devname);
4162 pr_err("If you think it is should be safe, try 'export MDADM_GROW_ALLOW_OLD=1'\n");
097075b6
N
4163 continue; /* time stamp is too bad */
4164 }
ea0ebe96 4165 }
353632d9 4166
e9e43ec3 4167 if (bsb.magic[15] == '1') {
10af14c4
N
4168 if (bsb.length == 0)
4169 continue;
e7a71c6b
N
4170 if (info->delta_disks >= 0) {
4171 /* reshape_progress is increasing */
4172 if (__le64_to_cpu(bsb.arraystart)
4173 + __le64_to_cpu(bsb.length)
4174 < info->reshape_progress) {
4175 nonew:
4176 if (verbose)
e7b84f9d 4177 pr_err("backup-metadata found on %s but is not needed\n", devname);
e7a71c6b
N
4178 continue; /* No new data here */
4179 }
4180 } else {
4181 /* reshape_progress is decreasing */
4182 if (__le64_to_cpu(bsb.arraystart) >=
4183 info->reshape_progress)
4184 goto nonew; /* No new data here */
ea0ebe96 4185 }
e9e43ec3 4186 } else {
10af14c4
N
4187 if (bsb.length == 0 && bsb.length2 == 0)
4188 continue;
e7a71c6b
N
4189 if (info->delta_disks >= 0) {
4190 /* reshape_progress is increasing */
4191 if ((__le64_to_cpu(bsb.arraystart)
4192 + __le64_to_cpu(bsb.length)
4193 < info->reshape_progress)
4194 &&
4195 (__le64_to_cpu(bsb.arraystart2)
4196 + __le64_to_cpu(bsb.length2)
4197 < info->reshape_progress))
4198 goto nonew; /* No new data here */
4199 } else {
4200 /* reshape_progress is decreasing */
4201 if (__le64_to_cpu(bsb.arraystart) >=
4202 info->reshape_progress &&
4203 __le64_to_cpu(bsb.arraystart2) >=
4204 info->reshape_progress)
4205 goto nonew; /* No new data here */
4206 }
e9e43ec3 4207 }
ea0ebe96
N
4208 if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0) {
4209 second_fail:
4210 if (verbose)
e7b84f9d
N
4211 pr_err("Failed to verify secondary backup-metadata block on %s\n",
4212 devname);
353632d9 4213 continue; /* Cannot seek */
ea0ebe96 4214 }
2efedc7b 4215 /* There should be a duplicate backup superblock 4k before here */
06b0d786 4216 if (lseek64(fd, -4096, 1) < 0 ||
0155af90 4217 read(fd, &bsb2, sizeof(bsb2)) != sizeof(bsb2))
ea0ebe96 4218 goto second_fail; /* Cannot find leading superblock */
e9e43ec3
N
4219 if (bsb.magic[15] == '1')
4220 bsbsize = offsetof(struct mdp_backup_super, pad1);
4221 else
4222 bsbsize = offsetof(struct mdp_backup_super, pad);
ff94fb86 4223 if (memcmp(&bsb2, &bsb, bsbsize) != 0)
ea0ebe96 4224 goto second_fail; /* Cannot find leading superblock */
2efedc7b 4225
353632d9 4226 /* Now need the data offsets for all devices. */
503975b9 4227 offsets = xmalloc(sizeof(*offsets)*info->array.raid_disks);
353632d9
NB
4228 for(j=0; j<info->array.raid_disks; j++) {
4229 if (fdlist[j] < 0)
4230 continue;
3da92f27 4231 if (st->ss->load_super(st, fdlist[j], NULL))
353632d9
NB
4232 /* FIXME should be this be an error */
4233 continue;
a5d85af7 4234 st->ss->getinfo_super(st, &dinfo, NULL);
3da92f27 4235 st->ss->free_super(st);
14e5b4d7 4236 offsets[j] = dinfo.data_offset * 512;
353632d9
NB
4237 }
4238 printf(Name ": restoring critical section\n");
4239
4240 if (restore_stripes(fdlist, offsets,
4241 info->array.raid_disks,
4242 info->new_chunk,
4243 info->new_level,
4244 info->new_layout,
06b0d786 4245 fd, __le64_to_cpu(bsb.devstart)*512,
92dcdf7c 4246 __le64_to_cpu(bsb.arraystart)*512,
2fcb75ae 4247 __le64_to_cpu(bsb.length)*512, NULL)) {
e9e43ec3 4248 /* didn't succeed, so giveup */
ea0ebe96 4249 if (verbose)
e7b84f9d 4250 pr_err("Error restoring backup from %s\n",
ea0ebe96 4251 devname);
730ae51f 4252 free(offsets);
e9e43ec3
N
4253 return 1;
4254 }
24daa16f 4255
e9e43ec3
N
4256 if (bsb.magic[15] == '2' &&
4257 restore_stripes(fdlist, offsets,
4258 info->array.raid_disks,
4259 info->new_chunk,
4260 info->new_level,
4261 info->new_layout,
4262 fd, __le64_to_cpu(bsb.devstart)*512 +
4263 __le64_to_cpu(bsb.devstart2)*512,
92dcdf7c 4264 __le64_to_cpu(bsb.arraystart2)*512,
2fcb75ae 4265 __le64_to_cpu(bsb.length2)*512, NULL)) {
353632d9 4266 /* didn't succeed, so giveup */
ea0ebe96 4267 if (verbose)
e7b84f9d 4268 pr_err("Error restoring second backup from %s\n",
ea0ebe96 4269 devname);
730ae51f 4270 free(offsets);
2295250a 4271 return 1;
353632d9
NB
4272 }
4273
730ae51f 4274 free(offsets);
e9e43ec3 4275
353632d9
NB
4276 /* Ok, so the data is restored. Let's update those superblocks. */
4277
10af14c4
N
4278 lo = hi = 0;
4279 if (bsb.length) {
4280 lo = __le64_to_cpu(bsb.arraystart);
4281 hi = lo + __le64_to_cpu(bsb.length);
4282 }
4283 if (bsb.magic[15] == '2' && bsb.length2) {
4284 unsigned long long lo1, hi1;
4285 lo1 = __le64_to_cpu(bsb.arraystart2);
4286 hi1 = lo1 + __le64_to_cpu(bsb.length2);
4287 if (lo == hi) {
4288 lo = lo1;
4289 hi = hi1;
4290 } else if (lo < lo1)
4291 hi = hi1;
4292 else
4293 lo = lo1;
4294 }
4295 if (lo < hi &&
4296 (info->reshape_progress < lo ||
4297 info->reshape_progress > hi))
4298 /* backup does not affect reshape_progress*/ ;
4299 else if (info->delta_disks >= 0) {
e9e43ec3
N
4300 info->reshape_progress = __le64_to_cpu(bsb.arraystart) +
4301 __le64_to_cpu(bsb.length);
4302 if (bsb.magic[15] == '2') {
4303 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2) +
4304 __le64_to_cpu(bsb.length2);
4305 if (p2 > info->reshape_progress)
4306 info->reshape_progress = p2;
4307 }
4308 } else {
4309 info->reshape_progress = __le64_to_cpu(bsb.arraystart);
4310 if (bsb.magic[15] == '2') {
4311 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2);
4312 if (p2 < info->reshape_progress)
4313 info->reshape_progress = p2;
4314 }
4315 }
353632d9 4316 for (j=0; j<info->array.raid_disks; j++) {
ca3b6696
N
4317 if (fdlist[j] < 0)
4318 continue;
3da92f27 4319 if (st->ss->load_super(st, fdlist[j], NULL))
353632d9 4320 continue;
a5d85af7 4321 st->ss->getinfo_super(st, &dinfo, NULL);
e9e43ec3 4322 dinfo.reshape_progress = info->reshape_progress;
3da92f27 4323 st->ss->update_super(st, &dinfo,
68c7d6d7
NB
4324 "_reshape_progress",
4325 NULL,0, 0, NULL);
3da92f27
NB
4326 st->ss->store_super(st, fdlist[j]);
4327 st->ss->free_super(st);
353632d9 4328 }
353632d9
NB
4329 return 0;
4330 }
6e9eac4f
NB
4331 /* Didn't find any backup data, try to see if any
4332 * was needed.
4333 */
82f2d6ab
N
4334 if (info->delta_disks < 0) {
4335 /* When shrinking, the critical section is at the end.
4336 * So see if we are before the critical section.
4337 */
4338 unsigned long long first_block;
4339 nstripe = ostripe = 0;
4340 first_block = 0;
4341 while (ostripe >= nstripe) {
4342 ostripe += info->array.chunk_size / 512;
4343 first_block = ostripe * odata;
4344 nstripe = first_block / ndata / (info->new_chunk/512) *
4345 (info->new_chunk/512);
4346 }
4347
4348 if (info->reshape_progress >= first_block)
4349 return 0;
6e9eac4f 4350 }
82f2d6ab
N
4351 if (info->delta_disks > 0) {
4352 /* See if we are beyond the critical section. */
4353 unsigned long long last_block;
4354 nstripe = ostripe = 0;
4355 last_block = 0;
4356 while (nstripe >= ostripe) {
4357 nstripe += info->new_chunk / 512;
4358 last_block = nstripe * ndata;
4359 ostripe = last_block / odata / (info->array.chunk_size/512) *
4360 (info->array.chunk_size/512);
4361 }
6e9eac4f 4362
82f2d6ab
N
4363 if (info->reshape_progress >= last_block)
4364 return 0;
4365 }
6e9eac4f 4366 /* needed to recover critical section! */
ea0ebe96 4367 if (verbose)
e7b84f9d 4368 pr_err("Failed to find backup of critical section\n");
2295250a 4369 return 1;
353632d9 4370}
e9e43ec3 4371
2dddadb0
AK
4372int Grow_continue_command(char *devname, int fd,
4373 char *backup_file, int verbose)
4374{
4375 int ret_val = 0;
4376 struct supertype *st = NULL;
4377 struct mdinfo *content = NULL;
4378 struct mdinfo array;
4379 char *subarray = NULL;
4380 struct mdinfo *cc = NULL;
4381 struct mdstat_ent *mdstat = NULL;
2dddadb0
AK
4382 int cfd = -1;
4383 int fd2 = -1;
4384
4385 dprintf("Grow continue from command line called for %s\n",
4386 devname);
4387
4388 st = super_by_fd(fd, &subarray);
4389 if (!st || !st->ss) {
e7b84f9d
N
4390 pr_err("Unable to determine metadata format for %s\n",
4391 devname);
2dddadb0
AK
4392 return 1;
4393 }
4394 dprintf("Grow continue is run for ");
4395 if (st->ss->external == 0) {
e0ab37a3 4396 int d;
2dddadb0 4397 dprintf("native array (%s)\n", devname);
e0ab37a3 4398 if (ioctl(fd, GET_ARRAY_INFO, &array.array) < 0) {
e7b84f9d 4399 pr_err("%s is not an active md array -"
2dddadb0
AK
4400 " aborting\n", devname);
4401 ret_val = 1;
4402 goto Grow_continue_command_exit;
4403 }
4404 content = &array;
e0ab37a3
N
4405 /* Need to load a superblock.
4406 * FIXME we should really get what we need from
4407 * sysfs
4408 */
4409 for (d = 0; d < MAX_DISKS; d++) {
4410 mdu_disk_info_t disk;
4411 char *dv;
4412 int err;
4413 disk.number = d;
4414 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
4415 continue;
4416 if (disk.major == 0 && disk.minor == 0)
4417 continue;
4418 if ((disk.state & (1 << MD_DISK_ACTIVE)) == 0)
4419 continue;
4420 dv = map_dev(disk.major, disk.minor, 1);
4421 if (!dv)
4422 continue;
4423 fd2 = dev_open(dv, O_RDONLY);
4424 if (fd2 < 0)
4425 continue;
4426 err = st->ss->load_super(st, fd2, NULL);
4427 close(fd2);
4428 if (err)
4429 continue;
4430 break;
4431 }
4432 if (d == MAX_DISKS) {
4433 pr_err("Unable to load metadata for %s\n",
4434 devname);
4435 ret_val = 1;
4436 goto Grow_continue_command_exit;
4437 }
4438 st->ss->getinfo_super(st, content, NULL);
2dddadb0 4439 } else {
4dd2df09 4440 char *container;
2dddadb0
AK
4441
4442 if (subarray) {
4443 dprintf("subarray (%s)\n", subarray);
4dd2df09
N
4444 container = st->container_devnm;
4445 cfd = open_dev_excl(st->container_devnm);
2dddadb0 4446 } else {
4dd2df09 4447 container = st->devnm;
2dddadb0 4448 close(fd);
4dd2df09
N
4449 cfd = open_dev_excl(st->devnm);
4450 dprintf("container (%s)\n", container);
2dddadb0
AK
4451 fd = cfd;
4452 }
4453 if (cfd < 0) {
e7b84f9d 4454 pr_err("Unable to open container "
2dddadb0
AK
4455 "for %s\n", devname);
4456 ret_val = 1;
4457 goto Grow_continue_command_exit;
4458 }
2dddadb0
AK
4459
4460 /* find in container array under reshape
4461 */
4462 ret_val = st->ss->load_container(st, cfd, NULL);
4463 if (ret_val) {
e7b84f9d
N
4464 pr_err("Cannot read superblock for %s\n",
4465 devname);
2dddadb0
AK
4466 ret_val = 1;
4467 goto Grow_continue_command_exit;
4468 }
4469
446894ea 4470 cc = st->ss->container_content(st, subarray);
2dddadb0
AK
4471 for (content = cc; content ; content = content->next) {
4472 char *array;
446894ea 4473 int allow_reshape = 1;
2dddadb0
AK
4474
4475 if (content->reshape_active == 0)
4476 continue;
81219e70
LM
4477 /* The decision about array or container wide
4478 * reshape is taken in Grow_continue based
4479 * content->reshape_active state, therefore we
4480 * need to check_reshape based on
4481 * reshape_active and subarray name
446894ea
N
4482 */
4483 if (content->array.state & (1<<MD_SB_BLOCK_VOLUME))
4484 allow_reshape = 0;
4485 if (content->reshape_active == CONTAINER_RESHAPE &&
4486 (content->array.state
4487 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE)))
4488 allow_reshape = 0;
4489
81219e70 4490 if (!allow_reshape) {
e7b84f9d
N
4491 pr_err("cannot continue reshape of an array"
4492 " in container with unsupported"
4493 " metadata: %s(%s)\n",
4dd2df09 4494 devname, container);
81219e70
LM
4495 ret_val = 1;
4496 goto Grow_continue_command_exit;
4497 }
2dddadb0
AK
4498
4499 array = strchr(content->text_version+1, '/')+1;
4dd2df09 4500 mdstat = mdstat_by_subdev(array, container);
2dddadb0
AK
4501 if (!mdstat)
4502 continue;
1ca90aa6 4503 if (mdstat->active == 0) {
4dd2df09
N
4504 pr_err("Skipping inactive array %s.\n",
4505 mdstat->devnm);
1ca90aa6
AK
4506 free_mdstat(mdstat);
4507 mdstat = NULL;
4508 continue;
4509 }
2dddadb0
AK
4510 break;
4511 }
4512 if (!content) {
e7b84f9d
N
4513 pr_err("Unable to determine reshaped "
4514 "array for %s\n", devname);
2dddadb0
AK
4515 ret_val = 1;
4516 goto Grow_continue_command_exit;
4517 }
4dd2df09 4518 fd2 = open_dev(mdstat->devnm);
2dddadb0 4519 if (fd2 < 0) {
4dd2df09 4520 pr_err("cannot open (%s)\n", mdstat->devnm);
2dddadb0
AK
4521 ret_val = 1;
4522 goto Grow_continue_command_exit;
4523 }
4524
4dd2df09 4525 sysfs_init(content, fd2, mdstat->devnm);
2dddadb0
AK
4526
4527 /* start mdmon in case it is not running
4528 */
4dd2df09
N
4529 if (!mdmon_running(container))
4530 start_mdmon(container);
4531 ping_monitor(container);
2dddadb0 4532
4dd2df09 4533 if (mdmon_running(container))
2dddadb0
AK
4534 st->update_tail = &st->updates;
4535 else {
e7b84f9d 4536 pr_err("No mdmon found. "
2dddadb0
AK
4537 "Grow cannot continue.\n");
4538 ret_val = 1;
4539 goto Grow_continue_command_exit;
4540 }
4541 }
4542
f1fe496b
AK
4543 /* verify that array under reshape is started from
4544 * correct position
4545 */
e0ab37a3 4546 if (verify_reshape_position(content, content->array.level) < 0) {
f1fe496b
AK
4547 ret_val = 1;
4548 goto Grow_continue_command_exit;
4549 }
4550
2dddadb0
AK
4551 /* continue reshape
4552 */
4553 ret_val = Grow_continue(fd, st, content, backup_file, 0);
4554
4555Grow_continue_command_exit:
4556 if (fd2 > -1)
4557 close(fd2);
4558 if (cfd > -1)
4559 close(cfd);
4560 st->ss->free_super(st);
4561 free_mdstat(mdstat);
4562 sysfs_free(cc);
4563 free(subarray);
4564
4565 return ret_val;
4566}
4567
e9e43ec3 4568int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
b76b30e0 4569 char *backup_file, int freeze_reshape)
e9e43ec3 4570{
3bd58dc6
AK
4571 int ret_val = 2;
4572
4573 if (!info->reshape_active)
4574 return ret_val;
864a004f 4575
20a40eca 4576 if (st->ss->external) {
4dd2df09 4577 int cfd = open_dev(st->container_devnm);
3db2fdd8 4578
3bd58dc6
AK
4579 if (cfd < 0)
4580 return 1;
f362d22b 4581
4dd2df09 4582 st->ss->load_container(st, cfd, st->container_devnm);
3bd58dc6 4583 close(cfd);
4dd2df09 4584 ret_val = reshape_container(st->container_devnm, NULL, mdfd,
3bd58dc6
AK
4585 st, info, 0, backup_file,
4586 0, 1, freeze_reshape);
4587 } else
4588 ret_val = reshape_array(NULL, mdfd, "array", st, info, 1,
19ceb16d 4589 NULL, 0ULL, backup_file, 0, 0, 1,
3bd58dc6 4590 freeze_reshape);
f362d22b 4591
3bd58dc6 4592 return ret_val;
e9e43ec3 4593}