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