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