]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Grow.c
FIX: Check correct 'old' level to see if reshape is needed.
[thirdparty/mdadm.git] / Grow.c
CommitLineData
e5329c37
NB
1/*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
e736b623 4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
e5329c37
NB
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
e736b623 22 * Email: <neilb@suse.de>
e5329c37
NB
23 */
24#include "mdadm.h"
25#include "dlink.h"
7236ee7a 26#include <sys/mman.h>
e5329c37
NB
27
28#if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
29#error no endian defined
30#endif
31#include "md_u.h"
32#include "md_p.h"
33
e9e43ec3
N
34#ifndef offsetof
35#define offsetof(t,f) ((size_t)&(((t*)0)->f))
36#endif
37
e5329c37
NB
38int Grow_Add_device(char *devname, int fd, char *newdev)
39{
40 /* Add a device to an active array.
41 * Currently, just extend a linear array.
42 * This requires writing a new superblock on the
43 * new device, calling the kernel to add the device,
44 * and if that succeeds, update the superblock on
45 * all other devices.
46 * This means that we need to *find* all other devices.
47 */
4b1ac34b
NB
48 struct mdinfo info;
49
e5329c37
NB
50 struct stat stb;
51 int nfd, fd2;
52 int d, nd;
82d9eba6 53 struct supertype *st = NULL;
4725bc31 54 char *subarray = NULL;
e5329c37 55
4b1ac34b 56 if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
e5329c37
NB
57 fprintf(stderr, Name ": cannot get array info for %s\n", devname);
58 return 1;
59 }
60
4725bc31
N
61 if (info.array.level != -1) {
62 fprintf(stderr, Name ": can only add devices to linear arrays\n");
63 return 1;
64 }
65
66 st = super_by_fd(fd, &subarray);
82d9eba6 67 if (!st) {
f9ce90ba
NB
68 fprintf(stderr, Name ": cannot handle arrays with superblock version %d\n", info.array.major_version);
69 return 1;
70 }
71
4725bc31
N
72 if (subarray) {
73 fprintf(stderr, Name ": Cannot grow linear sub-arrays yet\n");
74 free(subarray);
75 free(st);
e5329c37
NB
76 }
77
6416d527 78 nfd = open(newdev, O_RDWR|O_EXCL|O_DIRECT);
e5329c37
NB
79 if (nfd < 0) {
80 fprintf(stderr, Name ": cannot open %s\n", newdev);
4725bc31 81 free(st);
e5329c37
NB
82 return 1;
83 }
84 fstat(nfd, &stb);
85 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
86 fprintf(stderr, Name ": %s is not a block device!\n", newdev);
87 close(nfd);
4725bc31 88 free(st);
e5329c37
NB
89 return 1;
90 }
91 /* now check out all the devices and make sure we can read the superblock */
4b1ac34b 92 for (d=0 ; d < info.array.raid_disks ; d++) {
e5329c37
NB
93 mdu_disk_info_t disk;
94 char *dv;
95
4725bc31
N
96 st->ss->free_super(st);
97
e5329c37
NB
98 disk.number = d;
99 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
100 fprintf(stderr, Name ": cannot get device detail for device %d\n",
101 d);
4725bc31
N
102 close(nfd);
103 free(st);
e5329c37
NB
104 return 1;
105 }
16c6fa80 106 dv = map_dev(disk.major, disk.minor, 1);
e5329c37
NB
107 if (!dv) {
108 fprintf(stderr, Name ": cannot find device file for device %d\n",
109 d);
4725bc31
N
110 close(nfd);
111 free(st);
e5329c37
NB
112 return 1;
113 }
16c6fa80 114 fd2 = dev_open(dv, O_RDWR);
e5329c37
NB
115 if (!fd2) {
116 fprintf(stderr, Name ": cannot open device file %s\n", dv);
4725bc31
N
117 close(nfd);
118 free(st);
e5329c37
NB
119 return 1;
120 }
3da92f27
NB
121
122 if (st->ss->load_super(st, fd2, NULL)) {
e5329c37 123 fprintf(stderr, Name ": cannot find super block on %s\n", dv);
4725bc31 124 close(nfd);
e5329c37 125 close(fd2);
4725bc31 126 free(st);
e5329c37
NB
127 return 1;
128 }
129 close(fd2);
130 }
131 /* Ok, looks good. Lets update the superblock and write it out to
132 * newdev.
133 */
aba69144 134
4b1ac34b
NB
135 info.disk.number = d;
136 info.disk.major = major(stb.st_rdev);
137 info.disk.minor = minor(stb.st_rdev);
138 info.disk.raid_disk = d;
139 info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE);
3da92f27 140 st->ss->update_super(st, &info, "linear-grow-new", newdev,
f752781f 141 0, 0, NULL);
e5329c37 142
3da92f27 143 if (st->ss->store_super(st, nfd)) {
f752781f
NB
144 fprintf(stderr, Name ": Cannot store new superblock on %s\n",
145 newdev);
e5329c37
NB
146 close(nfd);
147 return 1;
148 }
e5329c37 149 close(nfd);
4b1ac34b
NB
150
151 if (ioctl(fd, ADD_NEW_DISK, &info.disk) != 0) {
e5329c37
NB
152 fprintf(stderr, Name ": Cannot add new disk to this array\n");
153 return 1;
154 }
155 /* Well, that seems to have worked.
156 * Now go through and update all superblocks
157 */
158
4b1ac34b 159 if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
e5329c37
NB
160 fprintf(stderr, Name ": cannot get array info for %s\n", devname);
161 return 1;
162 }
163
164 nd = d;
4b1ac34b 165 for (d=0 ; d < info.array.raid_disks ; d++) {
e5329c37
NB
166 mdu_disk_info_t disk;
167 char *dv;
168
169 disk.number = d;
170 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
171 fprintf(stderr, Name ": cannot get device detail for device %d\n",
172 d);
173 return 1;
174 }
16c6fa80 175 dv = map_dev(disk.major, disk.minor, 1);
e5329c37
NB
176 if (!dv) {
177 fprintf(stderr, Name ": cannot find device file for device %d\n",
178 d);
179 return 1;
180 }
16c6fa80 181 fd2 = dev_open(dv, O_RDWR);
e5329c37
NB
182 if (fd2 < 0) {
183 fprintf(stderr, Name ": cannot open device file %s\n", dv);
184 return 1;
185 }
3da92f27 186 if (st->ss->load_super(st, fd2, NULL)) {
e5329c37
NB
187 fprintf(stderr, Name ": cannot find super block on %s\n", dv);
188 close(fd);
189 return 1;
190 }
4b1ac34b
NB
191 info.array.raid_disks = nd+1;
192 info.array.nr_disks = nd+1;
193 info.array.active_disks = nd+1;
194 info.array.working_disks = nd+1;
f752781f 195
3da92f27 196 st->ss->update_super(st, &info, "linear-grow-update", dv,
f752781f 197 0, 0, NULL);
aba69144 198
3da92f27 199 if (st->ss->store_super(st, fd2)) {
e5329c37
NB
200 fprintf(stderr, Name ": Cannot store new superblock on %s\n", dv);
201 close(fd2);
202 return 1;
203 }
204 close(fd2);
205 }
206
207 return 0;
208}
f5e166fe 209
8fac0577 210int Grow_addbitmap(char *devname, int fd, char *file, int chunk, int delay, int write_behind, int force)
f5e166fe
NB
211{
212 /*
213 * First check that array doesn't have a bitmap
214 * Then create the bitmap
215 * Then add it
216 *
217 * For internal bitmaps, we need to check the version,
218 * find all the active devices, and write the bitmap block
219 * to all devices
220 */
221 mdu_bitmap_file_t bmf;
222 mdu_array_info_t array;
223 struct supertype *st;
4725bc31 224 char *subarray = NULL;
dcec9ee5
NB
225 int major = BITMAP_MAJOR_HI;
226 int vers = md_get_version(fd);
8fac0577 227 unsigned long long bitmapsize, array_size;
dcec9ee5
NB
228
229 if (vers < 9003) {
230 major = BITMAP_MAJOR_HOSTENDIAN;
b3bd581b
N
231 fprintf(stderr, Name ": Warning - bitmaps created on this kernel"
232 " are not portable\n"
233 " between different architectures. Consider upgrading"
234 " the Linux kernel.\n");
dcec9ee5 235 }
f5e166fe
NB
236
237 if (ioctl(fd, GET_BITMAP_FILE, &bmf) != 0) {
353632d9 238 if (errno == ENOMEM)
f5e166fe
NB
239 fprintf(stderr, Name ": Memory allocation failure.\n");
240 else
241 fprintf(stderr, Name ": bitmaps not supported by this kernel.\n");
242 return 1;
243 }
244 if (bmf.pathname[0]) {
fe80f49b
NB
245 if (strcmp(file,"none")==0) {
246 if (ioctl(fd, SET_BITMAP_FILE, -1)!= 0) {
247 fprintf(stderr, Name ": failed to remove bitmap %s\n",
248 bmf.pathname);
249 return 1;
250 }
251 return 0;
252 }
f5e166fe
NB
253 fprintf(stderr, Name ": %s already has a bitmap (%s)\n",
254 devname, bmf.pathname);
255 return 1;
256 }
257 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
258 fprintf(stderr, Name ": cannot get array status for %s\n", devname);
259 return 1;
260 }
261 if (array.state & (1<<MD_SB_BITMAP_PRESENT)) {
fe80f49b
NB
262 if (strcmp(file, "none")==0) {
263 array.state &= ~(1<<MD_SB_BITMAP_PRESENT);
264 if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
265 fprintf(stderr, Name ": failed to remove internal bitmap.\n");
266 return 1;
267 }
268 return 0;
269 }
f5e166fe
NB
270 fprintf(stderr, Name ": Internal bitmap already present on %s\n",
271 devname);
272 return 1;
273 }
4725bc31
N
274
275 if (strcmp(file, "none") == 0) {
276 fprintf(stderr, Name ": no bitmap found on %s\n", devname);
277 return 1;
278 }
5b28bd56
NB
279 if (array.level <= 0) {
280 fprintf(stderr, Name ": Bitmaps not meaningful with level %s\n",
281 map_num(pers, array.level)?:"of this array");
282 return 1;
283 }
8fac0577
NB
284 bitmapsize = array.size;
285 bitmapsize <<= 1;
beae1dfe 286 if (get_dev_size(fd, NULL, &array_size) &&
8fac0577
NB
287 array_size > (0x7fffffffULL<<9)) {
288 /* Array is big enough that we cannot trust array.size
289 * try other approaches
290 */
291 bitmapsize = get_component_size(fd);
292 }
8fac0577
NB
293 if (bitmapsize == 0) {
294 fprintf(stderr, Name ": Cannot reliably determine size of array to create bitmap - sorry.\n");
295 return 1;
296 }
297
f9c25f1d 298 if (array.level == 10) {
8686f3ed 299 int ncopies = (array.layout&255)*((array.layout>>8)&255);
f9c25f1d
NB
300 bitmapsize = bitmapsize * array.raid_disks / ncopies;
301 }
302
4725bc31 303 st = super_by_fd(fd, &subarray);
f5e166fe
NB
304 if (!st) {
305 fprintf(stderr, Name ": Cannot understand version %d.%d\n",
306 array.major_version, array.minor_version);
307 return 1;
308 }
4725bc31
N
309 if (subarray) {
310 fprintf(stderr, Name ": Cannot add bitmaps to sub-arrays yet\n");
311 free(subarray);
312 free(st);
fe80f49b 313 return 1;
4725bc31
N
314 }
315 if (strcmp(file, "internal") == 0) {
f5e166fe 316 int d;
ebeb3663
N
317 if (st->ss->add_internal_bitmap == NULL) {
318 fprintf(stderr, Name ": Internal bitmaps not supported "
319 "with %s metadata\n", st->ss->name);
320 return 1;
321 }
ea329559 322 for (d=0; d< st->max_devs; d++) {
f5e166fe
NB
323 mdu_disk_info_t disk;
324 char *dv;
325 disk.number = d;
326 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
327 continue;
328 if (disk.major == 0 &&
329 disk.minor == 0)
330 continue;
331 if ((disk.state & (1<<MD_DISK_SYNC))==0)
332 continue;
16c6fa80 333 dv = map_dev(disk.major, disk.minor, 1);
f5e166fe 334 if (dv) {
16c6fa80 335 int fd2 = dev_open(dv, O_RDWR);
f5e166fe
NB
336 if (fd2 < 0)
337 continue;
3da92f27 338 if (st->ss->load_super(st, fd2, NULL)==0) {
199171a2 339 if (st->ss->add_internal_bitmap(
3da92f27 340 st,
199171a2
NB
341 &chunk, delay, write_behind,
342 bitmapsize, 0, major)
343 )
3da92f27 344 st->ss->write_bitmap(st, fd2);
21e92547
NB
345 else {
346 fprintf(stderr, Name ": failed to create internal bitmap - chunksize problem.\n");
347 close(fd2);
348 return 1;
349 }
f5e166fe
NB
350 }
351 close(fd2);
352 }
353 }
354 array.state |= (1<<MD_SB_BITMAP_PRESENT);
355 if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
ff634064
N
356 if (errno == EBUSY)
357 fprintf(stderr, Name
358 ": Cannot add bitmap while array is"
359 " resyncing or reshaping etc.\n");
f5e166fe
NB
360 fprintf(stderr, Name ": failed to set internal bitmap.\n");
361 return 1;
362 }
fe80f49b
NB
363 } else {
364 int uuid[4];
365 int bitmap_fd;
366 int d;
367 int max_devs = st->max_devs;
fe80f49b
NB
368
369 /* try to load a superblock */
370 for (d=0; d<max_devs; d++) {
371 mdu_disk_info_t disk;
372 char *dv;
373 int fd2;
374 disk.number = d;
375 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
376 continue;
377 if ((disk.major==0 && disk.minor==0) ||
378 (disk.state & (1<<MD_DISK_REMOVED)))
379 continue;
16c6fa80 380 dv = map_dev(disk.major, disk.minor, 1);
fe80f49b 381 if (!dv) continue;
16c6fa80 382 fd2 = dev_open(dv, O_RDONLY);
fe80f49b 383 if (fd2 >= 0 &&
3da92f27 384 st->ss->load_super(st, fd2, NULL) == 0) {
fe80f49b 385 close(fd2);
3da92f27 386 st->ss->uuid_from_super(st, uuid);
fe80f49b
NB
387 break;
388 }
389 close(fd2);
390 }
391 if (d == max_devs) {
392 fprintf(stderr, Name ": cannot find UUID for array!\n");
393 return 1;
394 }
8fac0577 395 if (CreateBitmap(file, force, (char*)uuid, chunk,
f9c25f1d 396 delay, write_behind, bitmapsize, major)) {
fe80f49b
NB
397 return 1;
398 }
399 bitmap_fd = open(file, O_RDWR);
400 if (bitmap_fd < 0) {
8fac0577 401 fprintf(stderr, Name ": weird: %s cannot be opened\n",
fe80f49b
NB
402 file);
403 return 1;
404 }
405 if (ioctl(fd, SET_BITMAP_FILE, bitmap_fd) < 0) {
ff634064
N
406 int err = errno;
407 if (errno == EBUSY)
408 fprintf(stderr, Name
409 ": Cannot add bitmap while array is"
410 " resyncing or reshaping etc.\n");
fe80f49b 411 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
ff634064 412 devname, strerror(err));
fe80f49b
NB
413 return 1;
414 }
415 }
f5e166fe
NB
416
417 return 0;
418}
419
e86c9dd6
NB
420
421/*
422 * When reshaping an array we might need to backup some data.
423 * This is written to all spares with a 'super_block' describing it.
ff94fb86 424 * The superblock goes 4K from the end of the used space on the
e86c9dd6
NB
425 * device.
426 * It if written after the backup is complete.
427 * It has the following structure.
428 */
429
5fdf37e3 430static struct mdp_backup_super {
7236ee7a 431 char magic[16]; /* md_backup_data-1 or -2 */
e86c9dd6
NB
432 __u8 set_uuid[16];
433 __u64 mtime;
434 /* start/sizes in 512byte sectors */
7236ee7a 435 __u64 devstart; /* address on backup device/file of data */
e86c9dd6
NB
436 __u64 arraystart;
437 __u64 length;
438 __u32 sb_csum; /* csum of preceeding bytes. */
7236ee7a
N
439 __u32 pad1;
440 __u64 devstart2; /* offset in to data of second section */
441 __u64 arraystart2;
442 __u64 length2;
443 __u32 sb_csum2; /* csum of preceeding bytes. */
444 __u8 pad[512-68-32];
5fdf37e3 445} __attribute__((aligned(512))) bsb, bsb2;
e86c9dd6 446
4411fb17 447static __u32 bsb_csum(char *buf, int len)
e86c9dd6
NB
448{
449 int i;
450 int csum = 0;
451 for (i=0; i<len; i++)
452 csum = (csum<<3) + buf[0];
453 return __cpu_to_le32(csum);
454}
455
d7ca196c
N
456static int check_idle(struct supertype *st)
457{
458 /* Check that all member arrays for this container, or the
459 * container of this array, are idle
460 */
461 int container_dev = (st->container_dev != NoMdDev
462 ? st->container_dev : st->devnum);
463 char container[40];
464 struct mdstat_ent *ent, *e;
465 int is_idle = 1;
466
467 fmt_devname(container, container_dev);
468 ent = mdstat_read(0, 0);
469 for (e = ent ; e; e = e->next) {
470 if (!is_container_member(e, container))
471 continue;
472 if (e->percent >= 0) {
473 is_idle = 0;
474 break;
475 }
476 }
477 free_mdstat(ent);
478 return is_idle;
479}
480
7f2ba464 481static int freeze_container(struct supertype *st)
7236ee7a 482{
7f2ba464
DW
483 int container_dev = (st->container_dev != NoMdDev
484 ? st->container_dev : st->devnum);
d7ca196c 485 char container[40];
7f2ba464 486
d7ca196c
N
487 if (!check_idle(st))
488 return -1;
489
490 fmt_devname(container, container_dev);
7f2ba464
DW
491
492 if (block_monitor(container, 1)) {
493 fprintf(stderr, Name ": failed to freeze container\n");
494 return -2;
495 }
496
497 return 1;
498}
499
500static void unfreeze_container(struct supertype *st)
501{
502 int container_dev = (st->container_dev != NoMdDev
503 ? st->container_dev : st->devnum);
d7ca196c
N
504 char container[40];
505
506 fmt_devname(container, container_dev);
7f2ba464
DW
507
508 unblock_monitor(container, 1);
509}
510
511static int freeze(struct supertype *st)
512{
513 /* Try to freeze resync/rebuild on this array/container.
7236ee7a 514 * Return -1 if the array is busy,
7f2ba464 515 * return -2 container cannot be frozen,
7236ee7a
N
516 * return 0 if this kernel doesn't support 'frozen'
517 * return 1 if it worked.
518 */
7f2ba464
DW
519 if (st->ss->external)
520 return freeze_container(st);
521 else {
522 struct mdinfo *sra = sysfs_read(-1, st->devnum, GET_VERSION);
523 int err;
524
525 if (!sra)
526 return -1;
527 err = sysfs_freeze_array(sra);
528 sysfs_free(sra);
529 return err;
530 }
7236ee7a
N
531}
532
9202b8eb 533static void unfreeze(struct supertype *st)
7236ee7a 534{
7f2ba464
DW
535 if (st->ss->external)
536 return unfreeze_container(st);
537 else {
538 struct mdinfo *sra = sysfs_read(-1, st->devnum, GET_VERSION);
539
540 if (sra)
541 sysfs_set_str(sra, NULL, "sync_action", "idle");
542 else
543 fprintf(stderr, Name ": failed to unfreeze array\n");
544 sysfs_free(sra);
545 }
7236ee7a
N
546}
547
4411fb17 548static void wait_reshape(struct mdinfo *sra)
7236ee7a
N
549{
550 int fd = sysfs_get_fd(sra, NULL, "sync_action");
551 char action[20];
552
92a19f1a
AK
553 if (fd < 0)
554 return;
555
556 while (sysfs_fd_get_str(fd, action, 20) > 0 &&
557 strncmp(action, "reshape", 7) == 0) {
7236ee7a
N
558 fd_set rfds;
559 FD_ZERO(&rfds);
560 FD_SET(fd, &rfds);
561 select(fd+1, NULL, NULL, &rfds, NULL);
92a19f1a
AK
562 }
563 close(fd);
7236ee7a 564}
7bc71196
DW
565
566static int reshape_super(struct supertype *st, long long size, int level,
567 int layout, int chunksize, int raid_disks,
41784c88
AK
568 int delta_disks, char *backup_file, char *dev,
569 int verbose)
7bc71196
DW
570{
571 /* nothing extra to check in the native case */
572 if (!st->ss->external)
573 return 0;
574 if (!st->ss->reshape_super ||
575 !st->ss->manage_reshape) {
576 fprintf(stderr, Name ": %s metadata does not support reshape\n",
577 st->ss->name);
578 return 1;
579 }
580
581 return st->ss->reshape_super(st, size, level, layout, chunksize,
41784c88
AK
582 raid_disks, delta_disks, backup_file, dev,
583 verbose);
7bc71196
DW
584}
585
586static void sync_metadata(struct supertype *st)
587{
588 if (st->ss->external) {
76266030 589 if (st->update_tail) {
7bc71196 590 flush_metadata_updates(st);
76266030
N
591 st->update_tail = &st->updates;
592 } else
7bc71196
DW
593 st->ss->sync_metadata(st);
594 }
595}
596
597static int subarray_set_num(char *container, struct mdinfo *sra, char *name, int n)
598{
599 /* when dealing with external metadata subarrays we need to be
600 * prepared to handle EAGAIN. The kernel may need to wait for
601 * mdmon to mark the array active so the kernel can handle
602 * allocations/writeback when preparing the reshape action
603 * (md_allow_write()). We temporarily disable safe_mode_delay
604 * to close a race with the array_state going clean before the
605 * next write to raid_disks / stripe_cache_size
606 */
607 char safe[50];
608 int rc;
609
610 /* only 'raid_disks' and 'stripe_cache_size' trigger md_allow_write */
5da9ab98
N
611 if (!container ||
612 (strcmp(name, "raid_disks") != 0 &&
613 strcmp(name, "stripe_cache_size") != 0))
7bc71196
DW
614 return sysfs_set_num(sra, NULL, name, n);
615
616 rc = sysfs_get_str(sra, NULL, "safe_mode_delay", safe, sizeof(safe));
617 if (rc <= 0)
618 return -1;
619 sysfs_set_num(sra, NULL, "safe_mode_delay", 0);
620 rc = sysfs_set_num(sra, NULL, name, n);
621 if (rc < 0 && errno == EAGAIN) {
622 ping_monitor(container);
623 /* if we get EAGAIN here then the monitor is not active
624 * so stop trying
625 */
626 rc = sysfs_set_num(sra, NULL, name, n);
627 }
628 sysfs_set_str(sra, NULL, "safe_mode_delay", safe);
629 return rc;
630}
631
1e971e71 632int start_reshape(struct mdinfo *sra, int already_running)
47eb4d5a
N
633{
634 int err;
0f28668b 635 sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
47eb4d5a
N
636 err = sysfs_set_num(sra, NULL, "suspend_hi", 0);
637 err = err ?: sysfs_set_num(sra, NULL, "suspend_lo", 0);
1e971e71
N
638 if (!already_running)
639 sysfs_set_num(sra, NULL, "sync_min", 0);
47eb4d5a 640 err = err ?: sysfs_set_num(sra, NULL, "sync_max", 0);
1e971e71
N
641 if (!already_running)
642 err = err ?: sysfs_set_str(sra, NULL, "sync_action", "reshape");
47eb4d5a
N
643
644 return err;
645}
646
647void abort_reshape(struct mdinfo *sra)
648{
649 sysfs_set_str(sra, NULL, "sync_action", "idle");
650 sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
651 sysfs_set_num(sra, NULL, "suspend_hi", 0);
652 sysfs_set_num(sra, NULL, "suspend_lo", 0);
653 sysfs_set_num(sra, NULL, "sync_min", 0);
654 sysfs_set_str(sra, NULL, "sync_max", "max");
655}
656
dfe77a9e
KW
657int remove_disks_for_takeover(struct supertype *st,
658 struct mdinfo *sra,
659 int layout)
62a48395
AK
660{
661 int nr_of_copies;
662 struct mdinfo *remaining;
663 int slot;
664
dfe77a9e
KW
665 if (sra->array.level == 10)
666 nr_of_copies = layout & 0xff;
667 else if (sra->array.level == 1)
668 nr_of_copies = sra->array.raid_disks;
669 else
670 return 1;
62a48395
AK
671
672 remaining = sra->devs;
673 sra->devs = NULL;
674 /* for each 'copy', select one device and remove from the list. */
675 for (slot = 0; slot < sra->array.raid_disks; slot += nr_of_copies) {
676 struct mdinfo **diskp;
677 int found = 0;
678
679 /* Find a working device to keep */
680 for (diskp = &remaining; *diskp ; diskp = &(*diskp)->next) {
681 struct mdinfo *disk = *diskp;
682
683 if (disk->disk.raid_disk < slot)
684 continue;
685 if (disk->disk.raid_disk >= slot + nr_of_copies)
686 continue;
687 if (disk->disk.state & (1<<MD_DISK_REMOVED))
688 continue;
689 if (disk->disk.state & (1<<MD_DISK_FAULTY))
690 continue;
691 if (!(disk->disk.state & (1<<MD_DISK_SYNC)))
692 continue;
693
694 /* We have found a good disk to use! */
695 *diskp = disk->next;
696 disk->next = sra->devs;
697 sra->devs = disk;
698 found = 1;
699 break;
700 }
701 if (!found)
702 break;
703 }
704
705 if (slot < sra->array.raid_disks) {
706 /* didn't find all slots */
707 struct mdinfo **e;
708 e = &remaining;
709 while (*e)
710 e = &(*e)->next;
711 *e = sra->devs;
712 sra->devs = remaining;
713 return 1;
714 }
715
716 /* Remove all 'remaining' devices from the array */
717 while (remaining) {
718 struct mdinfo *sd = remaining;
719 remaining = sd->next;
720
721 sysfs_set_str(sra, sd, "state", "faulty");
722 sysfs_set_str(sra, sd, "slot", "none");
d5ca4a23
KW
723 /* for external metadata disks should be removed in mdmon */
724 if (!st->ss->external)
725 sysfs_set_str(sra, sd, "state", "remove");
62a48395
AK
726 sd->disk.state |= (1<<MD_DISK_REMOVED);
727 sd->disk.state &= ~(1<<MD_DISK_SYNC);
728 sd->next = sra->devs;
729 sra->devs = sd;
730 }
731 return 0;
732}
733
130994cb
AK
734void reshape_free_fdlist(int *fdlist,
735 unsigned long long *offsets,
736 int size)
737{
738 int i;
739
740 for (i = 0; i < size; i++)
741 if (fdlist[i] >= 0)
742 close(fdlist[i]);
743
744 free(fdlist);
745 free(offsets);
746}
747
748int reshape_prepare_fdlist(char *devname,
749 struct mdinfo *sra,
750 int raid_disks,
751 int nrdisks,
752 unsigned long blocks,
753 char *backup_file,
754 int *fdlist,
755 unsigned long long *offsets)
756{
757 int d = 0;
758 struct mdinfo *sd;
759
760 for (d = 0; d <= nrdisks; d++)
761 fdlist[d] = -1;
762 d = raid_disks;
763 for (sd = sra->devs; sd; sd = sd->next) {
764 if (sd->disk.state & (1<<MD_DISK_FAULTY))
765 continue;
766 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
767 char *dn = map_dev(sd->disk.major,
768 sd->disk.minor, 1);
769 fdlist[sd->disk.raid_disk]
770 = dev_open(dn, O_RDONLY);
771 offsets[sd->disk.raid_disk] = sd->data_offset*512;
772 if (fdlist[sd->disk.raid_disk] < 0) {
773 fprintf(stderr,
774 Name ": %s: cannot open component %s\n",
775 devname, dn ? dn : "-unknown-");
776 d = -1;
777 goto release;
778 }
779 } else if (backup_file == NULL) {
780 /* spare */
781 char *dn = map_dev(sd->disk.major,
782 sd->disk.minor, 1);
783 fdlist[d] = dev_open(dn, O_RDWR);
784 offsets[d] = (sd->data_offset + sra->component_size - blocks - 8)*512;
785 if (fdlist[d] < 0) {
786 fprintf(stderr, Name ": %s: cannot open component %s\n",
787 devname, dn ? dn : "-unknown-");
788 d = -1;
789 goto release;
790 }
791 d++;
792 }
793 }
794release:
795 return d;
796}
797
e6e9d47b
AK
798int reshape_open_backup_file(char *backup_file,
799 int fd,
800 char *devname,
801 long blocks,
802 int *fdlist,
a93f87ee
N
803 unsigned long long *offsets,
804 int restart)
e6e9d47b
AK
805{
806 /* Return 1 on success, 0 on any form of failure */
807 /* need to check backup file is large enough */
808 char buf[512];
809 struct stat stb;
810 unsigned int dev;
811 int i;
812
a93f87ee 813 *fdlist = open(backup_file, O_RDWR|O_CREAT|(restart ? O_TRUNC : O_EXCL),
e6e9d47b
AK
814 S_IRUSR | S_IWUSR);
815 *offsets = 8 * 512;
816 if (*fdlist < 0) {
817 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
818 devname, backup_file, strerror(errno));
819 return 0;
820 }
821 /* Guard against backup file being on array device.
822 * If array is partitioned or if LVM etc is in the
823 * way this will not notice, but it is better than
824 * nothing.
825 */
826 fstat(*fdlist, &stb);
827 dev = stb.st_dev;
828 fstat(fd, &stb);
829 if (stb.st_rdev == dev) {
830 fprintf(stderr, Name ": backup file must NOT be"
831 " on the array being reshaped.\n");
832 close(*fdlist);
833 return 0;
834 }
835
836 memset(buf, 0, 512);
ca4fe0bf 837 for (i=0; i < blocks + 8 ; i++) {
e6e9d47b
AK
838 if (write(*fdlist, buf, 512) != 512) {
839 fprintf(stderr, Name ": %s: cannot create"
840 " backup file %s: %s\n",
841 devname, backup_file, strerror(errno));
842 return 0;
843 }
844 }
845 if (fsync(*fdlist) != 0) {
846 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
847 devname, backup_file, strerror(errno));
848 return 0;
849 }
850
851 return 1;
852}
853
1c009fc2
AK
854unsigned long compute_backup_blocks(int nchunk, int ochunk,
855 unsigned int ndata, unsigned int odata)
856{
857 unsigned long a, b, blocks;
858 /* So how much do we need to backup.
859 * We need an amount of data which is both a whole number of
860 * old stripes and a whole number of new stripes.
861 * So LCM for (chunksize*datadisks).
862 */
863 a = (ochunk/512) * odata;
864 b = (nchunk/512) * ndata;
865 /* Find GCD */
866 while (a != b) {
867 if (a < b)
868 b -= a;
869 if (b < a)
870 a -= b;
871 }
872 /* LCM == product / GCD */
873 blocks = (ochunk/512) * (nchunk/512) * odata * ndata / a;
874
875 return blocks;
876}
877
5da9ab98
N
878char *analyse_change(struct mdinfo *info, struct reshape *re)
879{
880 /* Based on the current array state in info->array and
881 * the changes in info->new_* etc, determine:
882 * - whether the change is possible
883 * - Intermediate level/raid_disks/layout
884 * - whether a restriping reshape is needed
885 * - number of sectors in minimum change unit. This
886 * will cover a whole number of stripes in 'before' and
887 * 'after'.
888 *
889 * Return message if the change should be rejected
890 * NULL if the change can be achieved
891 *
892 * This can be called as part of starting a reshape, or
893 * when assembling an array that is undergoing reshape.
894 */
895 int new_disks;
b545e14a
N
896 /* delta_parity records change in number of devices
897 * caused by level change
898 */
899 int delta_parity = 0;
5da9ab98
N
900
901 /* If a new level not explicitly given, we assume no-change */
902 if (info->new_level == UnSet)
903 info->new_level = info->array.level;
904
905 if (info->new_chunk)
906 switch (info->new_level) {
907 case 0:
908 case 4:
909 case 5:
910 case 6:
911 case 10:
912 /* chunk size is meaningful, must divide component_size
913 * evenly
914 */
915 if (info->component_size % (info->new_chunk/512))
916 return "New chunk size does not"
917 " divide component size";
918 break;
919 default:
920 return "chunk size not meaningful for this level";
921 }
922 else
923 info->new_chunk = info->array.chunk_size;
924
925 switch (info->array.level) {
926 case 1:
927 /* RAID1 can convert to RAID1 with different disks, or
dfe77a9e
KW
928 * raid5 with 2 disks, or
929 * raid0 with 1 disk
5da9ab98 930 */
dfe77a9e 931 if (info->new_level == 0) {
b545e14a
N
932 if (info->delta_disks != UnSet &&
933 info->delta_disks != 0)
934 return "Cannot change number of disks "
935 "with RAID1->RAID0 conversion";
dfe77a9e
KW
936 re->level = 0;
937 re->before.data_disks = 1;
938 re->after.data_disks = 1;
939 re->before.layout = 0;
940 re->backup_blocks = 0;
941 re->parity = 0;
942 return NULL;
943 }
5da9ab98
N
944 if (info->new_level == 1) {
945 if (info->delta_disks == UnSet)
946 /* Don't know what to do */
947 return "no change requested for Growing RAID1";
948 re->level = 1;
b4f8e38b 949 re->backup_blocks = 0;
5da9ab98
N
950 re->parity = 0;
951 return NULL;
952 }
953 if (info->array.raid_disks == 2 &&
446d2a5a 954 info->new_level == 5) {
5652f2d9 955
5da9ab98 956 re->level = 5;
5da9ab98 957 re->before.data_disks = 1;
5652f2d9
N
958 if (info->delta_disks != UnSet &&
959 info->delta_disks != 0)
960 re->after.data_disks = 1 + info->delta_disks;
961 else
962 re->after.data_disks = 1;
963 if (re->after.data_disks < 1)
964 return "Number of disks too small for RAID5";
965
5da9ab98 966 re->before.layout = ALGORITHM_LEFT_SYMMETRIC;
446d2a5a
N
967 info->array.chunk_size = 65536;
968 break;
5da9ab98
N
969 }
970 /* Could do some multi-stage conversions, but leave that to
971 * later.
972 */
973 return "Impossibly level change request for RAID1";
974
975 case 10:
976 /* RAID10 can only be converted from near mode to
977 * RAID0 by removing some devices
978 */
979 if ((info->array.layout & ~0xff) != 0x100)
980 return "Cannot Grow RAID10 with far/offset layout";
981 /* number of devices must be multiple of number of copies */
982 if (info->array.raid_disks % (info->array.layout & 0xff))
983 return "RAID10 layout too complex for Grow operation";
984
985 if (info->new_level != 0)
986 return "RAID10 can only be changed to RAID0";
987 new_disks = (info->array.raid_disks
988 / (info->array.layout & 0xff));
b545e14a 989 if (info->delta_disks == UnSet)
5da9ab98
N
990 info->delta_disks = (new_disks
991 - info->array.raid_disks);
b545e14a 992
5da9ab98
N
993 if (info->delta_disks != new_disks - info->array.raid_disks)
994 return "New number of raid-devices impossible for RAID10";
995 if (info->new_chunk &&
996 info->new_chunk != info->array.chunk_size)
997 return "Cannot change chunk-size with RAID10 Grow";
998
999 /* looks good */
1000 re->level = 0;
1001 re->parity = 0;
1002 re->before.data_disks = new_disks;
031d445c 1003 re->after.data_disks = re->before.data_disks;
5da9ab98 1004 re->before.layout = 0;
b4f8e38b 1005 re->backup_blocks = 0;
5da9ab98
N
1006 return NULL;
1007
1008 case 0:
1009 /* RAID0 can be converted to RAID10, or to RAID456 */
1010 if (info->new_level == 10) {
1011 if (info->new_layout == UnSet && info->delta_disks == UnSet) {
1012 /* Assume near=2 layout */
1013 info->new_layout = 0x102;
1014 info->delta_disks = info->array.raid_disks;
1015 }
1016 if (info->new_layout == UnSet) {
1017 int copies = 1 + (info->delta_disks
1018 / info->array.raid_disks);
1019 if (info->array.raid_disks * (copies-1)
1020 != info->delta_disks)
1021 return "Impossible number of devices"
1022 " for RAID0->RAID10";
1023 info->new_layout = 0x100 + copies;
1024 }
1025 if (info->delta_disks == UnSet) {
1026 int copies = info->new_layout & 0xff;
1027 if (info->new_layout != 0x100 + copies)
1028 return "New layout impossible"
1029 " for RAID0->RAID10";;
1030 info->delta_disks = (copies - 1) *
1031 info->array.raid_disks;
1032 }
1033 if (info->new_chunk &&
1034 info->new_chunk != info->array.chunk_size)
1035 return "Cannot change chunk-size with RAID0->RAID10";
1036 /* looks good */
1037 re->level = 10;
1038 re->parity = 0;
1039 re->before.data_disks = (info->array.raid_disks +
1040 info->delta_disks);
031d445c 1041 re->after.data_disks = re->before.data_disks;
5da9ab98 1042 re->before.layout = info->new_layout;
b4f8e38b 1043 re->backup_blocks = 0;
5da9ab98
N
1044 return NULL;
1045 }
1046
1047 /* RAID0 can also covert to RAID0/4/5/6 by first converting to
1048 * a raid4 style layout of the final level.
1049 */
1050 switch (info->new_level) {
5da9ab98 1051 case 4:
b545e14a
N
1052 delta_parity = 1;
1053 case 0:
5da9ab98
N
1054 re->level = 4;
1055 re->before.layout = 0;
1056 break;
1057 case 5:
b545e14a 1058 delta_parity = 1;
5da9ab98
N
1059 re->level = 5;
1060 re->before.layout = ALGORITHM_PARITY_N;
1061 break;
1062 case 6:
b545e14a 1063 delta_parity = 2;
5da9ab98
N
1064 re->level = 6;
1065 re->before.layout = ALGORITHM_PARITY_N;
1066 break;
1067 default:
1068 return "Impossible level change requested";
1069 }
1070 re->before.data_disks = info->array.raid_disks;
1071 /* determining 'after' layout happens outside this 'switch' */
1072 break;
1073
1074 case 4:
1075 info->array.layout = ALGORITHM_PARITY_N;
1076 case 5:
1077 switch (info->new_level) {
f0cce442 1078 case 0:
b545e14a 1079 delta_parity = -1;
5da9ab98
N
1080 case 4:
1081 re->level = info->array.level;
1082 re->before.data_disks = info->array.raid_disks - 1;
1083 re->before.layout = info->array.layout;
1084 break;
1085 case 5:
1086 re->level = 5;
1087 re->before.data_disks = info->array.raid_disks - 1;
1088 re->before.layout = info->array.layout;
1089 break;
1090 case 6:
b545e14a 1091 delta_parity = 1;
5da9ab98
N
1092 re->level = 6;
1093 re->before.data_disks = info->array.raid_disks - 1;
1094 switch (info->array.layout) {
1095 case ALGORITHM_LEFT_ASYMMETRIC:
1096 re->before.layout = ALGORITHM_LEFT_ASYMMETRIC_6;
1097 break;
1098 case ALGORITHM_RIGHT_ASYMMETRIC:
1099 re->before.layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
1100 break;
1101 case ALGORITHM_LEFT_SYMMETRIC:
1102 re->before.layout = ALGORITHM_LEFT_SYMMETRIC_6;
1103 break;
1104 case ALGORITHM_RIGHT_SYMMETRIC:
1105 re->before.layout = ALGORITHM_RIGHT_SYMMETRIC_6;
1106 break;
1107 case ALGORITHM_PARITY_0:
1108 re->before.layout = ALGORITHM_PARITY_0_6;
1109 break;
1110 case ALGORITHM_PARITY_N:
1111 re->before.layout = ALGORITHM_PARITY_N_6;
1112 break;
1113 default:
1114 return "Cannot convert an array with this layout";
1115 }
1116 break;
1117 case 1:
1118 if (info->array.raid_disks != 2)
1119 return "Can only convert a 2-device array to RAID1";
b545e14a
N
1120 if (info->delta_disks != UnSet &&
1121 info->delta_disks != 0)
1122 return "Cannot set raid_disk when "
1123 "converting RAID5->RAID1";
5da9ab98 1124 re->level = 1;
5da9ab98
N
1125 break;
1126 default:
1127 return "Impossible level change requested";
1128 }
1129 break;
1130 case 6:
1131 switch (info->new_level) {
1132 case 4:
1133 case 5:
b545e14a 1134 delta_parity = -1;
5da9ab98
N
1135 case 6:
1136 re->level = 6;
1137 re->before.data_disks = info->array.raid_disks - 2;
1138 re->before.layout = info->array.layout;
1139 break;
1140 default:
1141 return "Impossible level change requested";
1142 }
1143 break;
1144 }
1145
1146 /* If we reached here then it looks like a re-stripe is
1147 * happening. We have determined the intermediate level
1148 * and initial raid_disks/layout and stored these in 're'.
1149 *
1150 * We need to deduce the final layout that can be atomically
1151 * converted to the end state.
1152 */
1153 switch (info->new_level) {
1154 case 0:
1155 /* We can only get to RAID0 from RAID4 or RAID5
1156 * with appropriate layout and one extra device
1157 */
1158 if (re->level != 4 && re->level != 5)
1159 return "Cannot covert to RAID0 from this level";
b545e14a 1160
5da9ab98
N
1161 switch (re->level) {
1162 case 4:
1163 re->after.layout = 0 ; break;
1164 case 5:
1165 re->after.layout = ALGORITHM_PARITY_N; break;
1166 }
1167 break;
1168
1169 case 4:
1170 /* We can only get to RAID4 from RAID5 */
1171 if (re->level != 4 && re->level != 5)
1172 return "Cannot convert to RAID4 from this level";
b545e14a 1173
5da9ab98
N
1174 switch (re->level) {
1175 case 4:
1176 re->after.layout = 0 ; break;
1177 case 5:
1178 re->after.layout = ALGORITHM_PARITY_N; break;
1179 }
1180 break;
1181
1182 case 5:
1183 /* We get to RAID5 for RAID5 or RAID6 */
1184 if (re->level != 5 && re->level != 6)
1185 return "Cannot convert to RAID5 from this level";
b545e14a 1186
5da9ab98
N
1187 switch (re->level) {
1188 case 5:
1189 if (info->new_layout == UnSet)
1190 re->after.layout = re->before.layout;
1191 else
1192 re->after.layout = info->new_layout;
1193 break;
1194 case 6:
4a870364
N
1195 if (info->new_layout == UnSet)
1196 info->new_layout = re->before.layout;
1197
5da9ab98
N
1198 /* after.layout needs to be raid6 version of new_layout */
1199 if (info->new_layout == ALGORITHM_PARITY_N)
1200 re->after.layout = ALGORITHM_PARITY_N;
1201 else {
1202 char layout[40];
1203 char *ls = map_num(r5layout, info->new_layout);
1204 int l;
1205 strcat(strcpy(layout, ls), "-6");
1206 l = map_name(r6layout, layout);
1207 if (l == UnSet)
1208 return "Cannot find RAID6 layout"
1209 " to convert to";
1210 re->after.layout = l;
1211 }
1212 }
1213 break;
1214
1215 case 6:
1216 /* We must already be at level 6 */
1217 if (re->level != 6)
1218 return "Impossible level change";
5da9ab98 1219 if (info->new_layout == UnSet)
4a870364 1220 re->after.layout = info->array.layout;
5da9ab98
N
1221 else
1222 re->after.layout = info->new_layout;
1223 break;
1224 default:
1225 return "Impossible level change requested";
1226 }
b545e14a
N
1227 if (info->delta_disks == UnSet)
1228 info->delta_disks = delta_parity;
1229
1230 re->after.data_disks = (re->before.data_disks
1231 + info->delta_disks
1232 - delta_parity);
5da9ab98
N
1233 switch (re->level) {
1234 case 6: re->parity = 2; break;
1235 case 4:
1236 case 5: re->parity = 1; break;
1237 default: re->parity = 0; break;
1238 }
1239 /* So we have a restripe operation, we need to calculate the number
1240 * of blocks per reshape operation.
1241 */
1242 if (info->new_chunk == 0)
1243 info->new_chunk = info->array.chunk_size;
1244 if (re->after.data_disks == re->before.data_disks &&
1245 re->after.layout == re->before.layout &&
1246 info->new_chunk == info->array.chunk_size) {
1247 /* Nothing to change */
b4f8e38b 1248 re->backup_blocks = 0;
5da9ab98
N
1249 return NULL;
1250 }
1251 if (re->after.data_disks == 1 && re->before.data_disks == 1) {
446d2a5a 1252 /* chunk and layout changes make no difference */
b4f8e38b 1253 re->backup_blocks = 0;
5da9ab98
N
1254 return NULL;
1255 }
1256
1257 if (re->after.data_disks == re->before.data_disks &&
1258 get_linux_version() < 2006032)
1259 return "in-place reshape is not safe before 2.6.32 - sorry.";
1260
1261 if (re->after.data_disks < re->before.data_disks &&
1262 get_linux_version() < 2006030)
1263 return "reshape to fewer devices is not supported before 2.6.32 - sorry.";
1264
b4f8e38b 1265 re->backup_blocks = compute_backup_blocks(
5da9ab98
N
1266 info->new_chunk, info->array.chunk_size,
1267 re->after.data_disks,
1268 re->before.data_disks);
1269
1270 re->new_size = info->component_size * re->after.data_disks;
1271 return NULL;
1272}
1273
1274static int reshape_array(char *container, int fd, char *devname,
1275 struct supertype *st, struct mdinfo *info,
a93f87ee
N
1276 int force, char *backup_file, int quiet, int forked,
1277 int restart);
493f5dd6 1278static int reshape_container(char *container, char *devname,
5da9ab98
N
1279 struct supertype *st,
1280 struct mdinfo *info,
1281 int force,
1282 char *backup_file,
493f5dd6 1283 int quiet, int restart);
1c009fc2 1284
06b0d786 1285int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
e86c9dd6 1286 long long size,
691a36b7
N
1287 int level, char *layout_str, int chunksize, int raid_disks,
1288 int force)
e86c9dd6
NB
1289{
1290 /* Make some changes in the shape of an array.
1291 * The kernel must support the change.
7236ee7a
N
1292 *
1293 * There are three different changes. Each can trigger
1294 * a resync or recovery so we freeze that until we have
1295 * requested everything (if kernel supports freezing - 2.6.30).
1296 * The steps are:
1297 * - change size (i.e. component_size)
1298 * - change level
1299 * - change layout/chunksize/ndisks
1300 *
1301 * The last can require a reshape. It is different on different
1302 * levels so we need to check the level before actioning it.
1303 * Some times the level change needs to be requested after the
1304 * reshape (e.g. raid6->raid5, raid5->raid0)
1305 *
e86c9dd6 1306 */
5da9ab98 1307 struct mdu_array_info_s array;
7236ee7a 1308 int rv = 0;
e86c9dd6 1309 struct supertype *st;
4725bc31 1310 char *subarray = NULL;
e86c9dd6 1311
7236ee7a 1312 int frozen;
7236ee7a 1313 int changed = 0;
7bc71196 1314 char *container = NULL;
5da9ab98 1315 char container_buf[20];
7bc71196 1316 int cfd = -1;
e86c9dd6 1317
5da9ab98 1318 struct mdinfo info;
7e0f6979 1319 struct mdinfo *sra;
e86c9dd6
NB
1320
1321 if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) {
1322 fprintf(stderr, Name ": %s is not an active md array - aborting\n",
1323 devname);
1324 return 1;
1325 }
24d40069 1326
9ce510be
N
1327 if (size >= 0 &&
1328 (chunksize || level!= UnSet || layout_str || raid_disks)) {
1329 fprintf(stderr, Name ": cannot change component size at the same time "
1330 "as other changes.\n"
1331 " Change size first, then check data is intact before "
1332 "making other changes.\n");
1333 return 1;
1334 }
1335
24d40069
N
1336 if (raid_disks && raid_disks < array.raid_disks && array.level > 1 &&
1337 get_linux_version() < 2006032 &&
1338 !check_env("MDADM_FORCE_FEWER")) {
1339 fprintf(stderr, Name ": reducing the number of devices is not safe before Linux 2.6.32\n"
1340 " Please use a newer kernel\n");
1341 return 1;
1342 }
7bc71196
DW
1343
1344 st = super_by_fd(fd, &subarray);
1345 if (!st) {
1346 fprintf(stderr, Name ": Unable to determine metadata format for %s\n", devname);
1347 return 1;
1348 }
acab7bb1
N
1349 if (raid_disks > st->max_devs) {
1350 fprintf(stderr, Name ": Cannot increase raid-disks on this array"
1351 " beyond %d\n", st->max_devs);
1352 return 1;
1353 }
7bc71196
DW
1354
1355 /* in the external case we need to check that the requested reshape is
1356 * supported, and perform an initial check that the container holds the
1357 * pre-requisite spare devices (mdmon owns final validation)
1358 */
1359 if (st->ss->external) {
1360 int container_dev;
7f2ba464 1361 int rv;
7bc71196
DW
1362
1363 if (subarray) {
1364 container_dev = st->container_dev;
1365 cfd = open_dev_excl(st->container_dev);
7bc71196
DW
1366 } else {
1367 container_dev = st->devnum;
1368 close(fd);
1369 cfd = open_dev_excl(st->devnum);
1370 fd = cfd;
1371 }
1372 if (cfd < 0) {
1373 fprintf(stderr, Name ": Unable to open container for %s\n",
1374 devname);
7f2ba464 1375 free(subarray);
7bc71196
DW
1376 return 1;
1377 }
1378
5da9ab98
N
1379 fmt_devname(container_buf, container_dev);
1380 container = container_buf;
7bc71196 1381
eb744788
AK
1382 rv = st->ss->load_container(st, cfd, NULL);
1383
7f2ba464 1384 if (rv) {
7bc71196
DW
1385 fprintf(stderr, Name ": Cannot read superblock for %s\n",
1386 devname);
7f2ba464 1387 free(subarray);
7bc71196
DW
1388 return 1;
1389 }
1390
1391 if (mdmon_running(container_dev))
1392 st->update_tail = &st->updates;
691a36b7
N
1393 }
1394
1395 if (raid_disks > array.raid_disks &&
1396 array.spare_disks < (raid_disks - array.raid_disks) &&
1397 !force) {
1398 fprintf(stderr,
1399 Name ": Need %d spare%s to avoid degraded array,"
1400 " and only have %d.\n"
1401 " Use --force to over-ride this check.\n",
1402 raid_disks - array.raid_disks,
1403 raid_disks - array.raid_disks == 1 ? "" : "s",
1404 array.spare_disks);
1405 return 1;
7bc71196
DW
1406 }
1407
3fa436ac
ML
1408 sra = sysfs_read(fd, 0, GET_LEVEL | GET_DISKS | GET_DEVS
1409 | GET_STATE | GET_VERSION);
1410 if (sra) {
7f2ba464 1411 if (st->ss->external && subarray == NULL) {
7bc71196
DW
1412 array.level = LEVEL_CONTAINER;
1413 sra->array.level = LEVEL_CONTAINER;
1414 }
7bc71196 1415 } else {
b526e52d
DW
1416 fprintf(stderr, Name ": failed to read sysfs parameters for %s\n",
1417 devname);
1418 return 1;
1419 }
7f2ba464
DW
1420 frozen = freeze(st);
1421 if (frozen < -1) {
1422 /* freeze() already spewed the reason */
1423 return 1;
1424 } else if (frozen < 0) {
7236ee7a
N
1425 fprintf(stderr, Name ": %s is performing resync/recovery and cannot"
1426 " be reshaped\n", devname);
1427 return 1;
1428 }
1429
1430 /* ========= set size =============== */
1431 if (size >= 0 && (size == 0 || size != array.size)) {
7bc71196
DW
1432 long long orig_size = array.size;
1433
41784c88
AK
1434 if (reshape_super(st, size, UnSet, UnSet, 0, 0, UnSet, NULL,
1435 devname, !quiet)) {
7bc71196
DW
1436 rv = 1;
1437 goto release;
1438 }
1439 sync_metadata(st);
7236ee7a
N
1440 array.size = size;
1441 if (array.size != size) {
1442 /* got truncated to 32bit, write to
1443 * component_size instead
1444 */
1445 if (sra)
1446 rv = sysfs_set_num(sra, NULL,
1447 "component_size", size);
1448 else
1449 rv = -1;
1450 } else
1451 rv = ioctl(fd, SET_ARRAY_INFO, &array);
1452 if (rv != 0) {
39bbb392 1453 int err = errno;
7bc71196
DW
1454
1455 /* restore metadata */
1456 if (reshape_super(st, orig_size, UnSet, UnSet, 0, 0,
41784c88 1457 UnSet, NULL, devname, !quiet) == 0)
7bc71196 1458 sync_metadata(st);
7236ee7a 1459 fprintf(stderr, Name ": Cannot set device size for %s: %s\n",
39bbb392
N
1460 devname, strerror(err));
1461 if (err == EBUSY &&
1462 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
1463 fprintf(stderr, " Bitmap must be removed before size can be changed\n");
7236ee7a
N
1464 rv = 1;
1465 goto release;
1466 }
1467 ioctl(fd, GET_ARRAY_INFO, &array);
be1cabbd 1468 size = get_component_size(fd)/2;
f98841b3
N
1469 if (size == 0)
1470 size = array.size;
7236ee7a 1471 if (!quiet)
f98841b3
N
1472 fprintf(stderr, Name ": component size of %s has been set to %lluK\n",
1473 devname, size);
7236ee7a 1474 changed = 1;
7bc71196 1475 } else if (array.level != LEVEL_CONTAINER) {
be1cabbd 1476 size = get_component_size(fd)/2;
f98841b3
N
1477 if (size == 0)
1478 size = array.size;
7236ee7a
N
1479 }
1480
dfe77a9e 1481 /* ========= check for Raid10/Raid1 -> Raid0 conversion ===============
5da9ab98 1482 * current implementation assumes that following conditions must be met:
dfe77a9e
KW
1483 * - RAID10:
1484 * - far_copies == 1
1485 * - near_copies == 2
62a48395 1486 */
dfe77a9e
KW
1487 if ((level == 0 && array.level == 10 && sra &&
1488 array.layout == ((1 << 8) + 2) && !(array.raid_disks & 1)) ||
1489 (level == 0 && array.level == 1 && sra)) {
62a48395 1490 int err;
dfe77a9e 1491 err = remove_disks_for_takeover(st, sra, array.layout);
62a48395
AK
1492 if (err) {
1493 dprintf(Name": Array cannot be reshaped\n");
62a48395
AK
1494 if (cfd > -1)
1495 close(cfd);
5da9ab98
N
1496 rv = 1;
1497 goto release;
7236ee7a 1498 }
bb025c2f
KW
1499 /* FIXME this is added with no justification - why is it here */
1500 ping_monitor(container);
7236ee7a
N
1501 }
1502
5da9ab98 1503 info.array = array;
7443ee81 1504 sysfs_init(&info, fd, NoMdDev);
3fa436ac 1505 strcpy(info.text_version, sra->text_version);
7443ee81 1506 info.component_size = size*2;
5da9ab98
N
1507 info.new_level = level;
1508 info.new_chunk = chunksize * 1024;
1509 if (raid_disks)
1510 info.delta_disks = raid_disks - info.array.raid_disks;
1511 else
1512 info.delta_disks = UnSet;
1513 if (layout_str == NULL) {
1514 info.new_layout = UnSet;
1515 if (info.array.level == 6 &&
1516 (info.new_level == 6 || info.new_level == UnSet) &&
1517 info.array.layout >= 16) {
7236ee7a 1518 fprintf(stderr, Name
5da9ab98
N
1519 ": %s has a non-standard layout. If you"
1520 " wish to preserve this\n"
1521 " during the reshape, please specify"
1522 " --layout=preserve\n"
1523 " If you want to change it, specify a"
1524 " layout or use --layout=normalise\n",
7236ee7a
N
1525 devname);
1526 rv = 1;
1527 goto release;
1528 }
5da9ab98
N
1529 } else if (strcmp(layout_str, "normalise") == 0 ||
1530 strcmp(layout_str, "normalize") == 0) {
1531 /* If we have a -6 RAID6 layout, remove the '-6'. */
1532 info.new_layout = UnSet;
1533 if (info.array.level == 6 && info.new_level == UnSet) {
1534 char l[40], *h;
1535 strcpy(l, map_num(r6layout, info.array.layout));
1536 h = strrchr(l, '-');
1537 if (h && strcmp(h, "-6") == 0) {
1538 *h = 0;
1539 info.new_layout = map_name(r6layout, l);
7236ee7a
N
1540 }
1541 }
5da9ab98
N
1542 } else if (strcmp(layout_str, "preserve") == 0) {
1543 info.new_layout = UnSet;
1544 } else {
1545 int l = info.new_level;
1546 if (l == UnSet)
1547 l = info.array.level;
1548 switch (l) {
1549 case 5:
1550 info.new_layout = map_name(r5layout, layout_str);
1551 break;
1552 case 6:
1553 info.new_layout = map_name(r6layout, layout_str);
1554 break;
1555 case 10:
1556 info.new_layout = parse_layout_10(layout_str);
1557 break;
1558 case LEVEL_FAULTY:
1559 info.new_layout = parse_layout_faulty(layout_str);
1560 break;
1561 default:
1562 fprintf(stderr, Name ": layout not meaningful"
1563 " with this level\n");
7bc71196
DW
1564 rv = 1;
1565 goto release;
1566 }
5da9ab98
N
1567 if (info.new_layout == UnSet) {
1568 fprintf(stderr, Name ": layout %s not understood"
1569 " for this level\n",
1570 layout_str);
1571 rv = 1;
1572 goto release;
7bc71196 1573 }
7236ee7a
N
1574 }
1575
5da9ab98
N
1576 if (array.level == LEVEL_CONTAINER) {
1577 /* This change is to be applied to every array in the
1578 * container. This is only needed when the metadata imposes
1579 * restraints of the various arrays in the container.
1580 * Currently we only know that IMSM requires all arrays
1581 * to have the same number of devices so changing the
1582 * number of devices (On-Line Capacity Expansion) must be
1583 * performed at the level of the container
1584 */
493f5dd6
N
1585 rv = reshape_container(container, devname, st, &info,
1586 force, backup_file, quiet, 0);
9202b8eb 1587 frozen = 0;
5da9ab98 1588 } else {
08f9e34b
AK
1589 /* get spare devices from external metadata
1590 */
1591 if (st->ss->external) {
1592 struct mdinfo *info2;
1593
1594 info2 = st->ss->container_content(st, subarray);
1595 if (info2) {
1596 info.array.spare_disks =
1597 info2->array.spare_disks;
1598 sysfs_free(info2);
1599 }
1600 }
1601
5da9ab98
N
1602 /* Impose these changes on a single array. First
1603 * check that the metadata is OK with the change. */
7bc71196 1604
5da9ab98
N
1605 if (reshape_super(st, info.component_size, info.new_level,
1606 info.new_layout, info.new_chunk,
41784c88 1607 info.array.raid_disks, info.delta_disks,
5da9ab98 1608 backup_file, devname, quiet)) {
7bc71196
DW
1609 rv = 1;
1610 goto release;
1611 }
5da9ab98
N
1612 sync_metadata(st);
1613 rv = reshape_array(container, fd, devname, st, &info, force,
a93f87ee 1614 backup_file, quiet, 0, 0);
9202b8eb 1615 frozen = 0;
5da9ab98
N
1616 }
1617release:
9202b8eb
N
1618 if (frozen > 0)
1619 unfreeze(st);
5da9ab98
N
1620 return rv;
1621}
7bc71196 1622
5da9ab98
N
1623static int reshape_array(char *container, int fd, char *devname,
1624 struct supertype *st, struct mdinfo *info,
1625 int force,
a93f87ee
N
1626 char *backup_file, int quiet, int forked,
1627 int restart)
5da9ab98
N
1628{
1629 struct reshape reshape;
1630 int spares_needed;
1631 char *msg;
1632 int orig_level = UnSet;
1633 int disks, odisks;
7bc71196 1634
5da9ab98
N
1635 struct mdu_array_info_s array;
1636 char *c;
e86c9dd6 1637
5da9ab98
N
1638 int *fdlist;
1639 unsigned long long *offsets;
1640 int d;
1641 int nrdisks;
1642 int err;
da8100ca 1643 unsigned long blocks;
5da9ab98
N
1644 unsigned long cache;
1645 unsigned long long array_size;
1646 int done;
cf6ac177 1647 struct mdinfo *sra = NULL;
7bc71196 1648
5da9ab98
N
1649 msg = analyse_change(info, &reshape);
1650 if (msg) {
1651 fprintf(stderr, Name ": %s\n", msg);
631d7405 1652 goto release;
5da9ab98 1653 }
20a40eca
N
1654 if (reshape.level != info->array.level ||
1655 reshape.before.layout != info->array.layout ||
1656 reshape.before.data_disks + reshape.parity != info->array.raid_disks) {
1657 fprintf(stderr, Name ": reshape info is not in native format -"
1658 " cannot continue.\n");
1659 goto release;
1660 }
9ff87c16 1661 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
20c8698d 1662 dprintf("Cannot get array information.\n");
631d7405 1663 goto release;
9ff87c16 1664 }
a93f87ee
N
1665
1666 if (restart) {
1667 /* reshape already started. just skip to monitoring the reshape */
1668 if (reshape.backup_blocks == 0)
1669 return 0;
1670 goto started;
1671 }
a9c3e78f
AK
1672 /* The container is frozen but the array may not be.
1673 * So freeze the array so spares don't get put to the wrong use
1674 * FIXME there should probably be a cleaner separation between
1675 * freeze_array and freeze_container.
1676 */
1677 sysfs_freeze_array(info);
e06c4e59 1678 /* Check we have enough spares to not be degraded */
5da9ab98
N
1679 spares_needed = max(reshape.before.data_disks,
1680 reshape.after.data_disks)
1681 + reshape.parity - array.raid_disks;
7bc71196 1682
88c1a083 1683 if (!force &&
d6bd632c 1684 info->new_level > 1 &&
88c1a083 1685 spares_needed > info->array.spare_disks) {
5da9ab98
N
1686 fprintf(stderr,
1687 Name ": Need %d spare%s to avoid degraded array,"
1688 " and only have %d.\n"
1689 " Use --force to over-ride this check.\n",
1690 spares_needed,
1691 spares_needed == 1 ? "" : "s",
1692 info->array.spare_disks);
631d7405 1693 goto release;
7bc71196 1694 }
e06c4e59
N
1695 /* Check we have enough spares to not fail */
1696 spares_needed = max(reshape.before.data_disks,
1697 reshape.after.data_disks)
1698 - array.raid_disks;
1699 if ((info->new_level > 1 || info->new_level == 0) &&
1700 spares_needed > info->array.spare_disks) {
1701 fprintf(stderr,
1702 Name ": Need %d spare%s to create working array,"
1703 " and only have %d.\n",
1704 spares_needed,
1705 spares_needed == 1 ? "" : "s",
1706 info->array.spare_disks);
1707 goto release;
1708 }
e86c9dd6 1709
eae35f5c 1710 if (reshape.level != array.level) {
5da9ab98
N
1711 char *c = map_num(pers, reshape.level);
1712 int err;
1713 if (c == NULL)
631d7405 1714 goto release;
e86c9dd6 1715
5da9ab98
N
1716 err = sysfs_set_str(info, NULL, "level", c);
1717 if (err) {
1718 err = errno;
1719 fprintf(stderr, Name ": %s: could not set level to %s\n",
1720 devname, c);
1721 if (err == EBUSY &&
1722 (info->array.state & (1<<MD_SB_BITMAP_PRESENT)))
1723 fprintf(stderr, " Bitmap must be removed"
1724 " before level can be changed\n");
631d7405 1725 goto release;
19678e53 1726 }
5da9ab98 1727 if (!quiet)
b6b95155 1728 fprintf(stderr, Name ": level of %s changed to %s\n",
5da9ab98 1729 devname, c);
eae35f5c 1730 orig_level = array.level;
3cd4e7c4 1731 sysfs_freeze_array(info);
e86c9dd6 1732
16d4d84e
AK
1733 if (reshape.level > 0 && st->ss->external) {
1734 /* make sure mdmon is aware of the new level */
1735 if (!mdmon_running(st->container_dev))
1736 start_mdmon(st->container_dev);
1737 ping_monitor(container);
1738 }
5da9ab98 1739 }
5da9ab98
N
1740 /* ->reshape_super might have chosen some spares from the
1741 * container that it wants to be part of the new array.
1742 * We can collect them with ->container_content and give
1743 * them to the kernel.
1744 */
1745 if (st->ss->reshape_super && st->ss->container_content) {
1746 char *subarray = strchr(info->text_version+1, '/')+1;
1747 struct mdinfo *info2 =
1748 st->ss->container_content(st, subarray);
1749 struct mdinfo *d;
1750
2dd0d697
AK
1751 if (info2) {
1752 sysfs_init(info2, fd, st->devnum);
5da9ab98
N
1753 for (d = info2->devs; d; d = d->next) {
1754 if (d->disk.state == 0 &&
1755 d->disk.raid_disk >= 0) {
1756 /* This is a spare that wants to
1757 * be part of the array.
1758 */
1759 add_disk(fd, st, info2, d);
1760 }
9860f271 1761 }
2dd0d697
AK
1762 sysfs_free(info2);
1763 }
5da9ab98 1764 }
1686dc25 1765
b4f8e38b 1766 if (reshape.backup_blocks == 0) {
5da9ab98
N
1767 /* No restriping needed, but we might need to impose
1768 * some more changes: layout, raid_disks, chunk_size
e86c9dd6 1769 */
24aebf3a
KW
1770 /* read current array info */
1771 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
83732c28 1772 dprintf("Cannot get array information.\n");
24aebf3a
KW
1773 goto release;
1774 }
1775 /* compare current array info with new values and if
1776 * it is different update them to new */
5da9ab98 1777 if (info->new_layout != UnSet &&
24aebf3a
KW
1778 info->new_layout != array.layout) {
1779 array.layout = info->new_layout;
1780 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
5da9ab98 1781 fprintf(stderr, Name ": failed to set new layout\n");
631d7405 1782 goto release;
5da9ab98
N
1783 } else if (!quiet)
1784 printf("layout for %s set to %d\n",
24aebf3a 1785 devname, array.layout);
5da9ab98
N
1786 }
1787 if (info->delta_disks != UnSet &&
24aebf3a
KW
1788 info->delta_disks != 0 &&
1789 array.raid_disks != (info->array.raid_disks + info->delta_disks)) {
1790 array.raid_disks += info->delta_disks;
1791 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
5da9ab98 1792 fprintf(stderr, Name ": failed to set raid disks\n");
631d7405 1793 goto release;
24aebf3a 1794 } else if (!quiet) {
5da9ab98 1795 printf("raid_disks for %s set to %d\n",
24aebf3a
KW
1796 devname, array.raid_disks);
1797 }
5da9ab98
N
1798 }
1799 if (info->new_chunk != 0 &&
24aebf3a 1800 info->new_chunk != array.chunk_size) {
5da9ab98
N
1801 if (sysfs_set_num(info, NULL,
1802 "chunk_size", info->new_chunk) != 0) {
1803 fprintf(stderr, Name ": failed to set chunk size\n");
631d7405 1804 goto release;
5da9ab98
N
1805 } else if (!quiet)
1806 printf("chunk size for %s set to %d\n",
24aebf3a 1807 devname, array.chunk_size);
4725bc31 1808 }
e35b189b 1809 unfreeze(st);
631d7405 1810 return 0;
5da9ab98 1811 }
19678e53 1812
5da9ab98
N
1813 /*
1814 * There are three possibilities.
1815 * 1/ The array will shrink.
1816 * We need to ensure the reshape will pause before reaching
1817 * the 'critical section'. We also need to fork and wait for
1818 * that to happen. When it does we
1819 * suspend/backup/complete/unfreeze
1820 *
1821 * 2/ The array will not change size.
1822 * This requires that we keep a backup of a sliding window
1823 * so that we can restore data after a crash. So we need
1824 * to fork and monitor progress.
1825 * In future we will allow the data_offset to change, so
1826 * a sliding backup becomes unnecessary.
1827 *
1828 * 3/ The array will grow. This is relatively easy.
1829 * However the kernel's restripe routines will cheerfully
1830 * overwrite some early data before it is safe. So we
1831 * need to make a backup of the early parts of the array
1832 * and be ready to restore it if rebuild aborts very early.
1833 * For externally managed metadata, we still need a forked
1834 * child to monitor the reshape and suspend IO over the region
1835 * that is being reshaped.
1836 *
1837 * We backup data by writing it to one spare, or to a
1838 * file which was given on command line.
1839 *
1840 * In each case, we first make sure that storage is available
1841 * for the required backup.
1842 * Then we:
1843 * - request the shape change.
1844 * - fork to handle backup etc.
1845 */
a93f87ee 1846started:
5da9ab98
N
1847 /* Check that we can hold all the data */
1848 get_dev_size(fd, NULL, &array_size);
1849 if (reshape.new_size < (array_size/512)) {
1850 fprintf(stderr,
1851 Name ": this change will reduce the size of the array.\n"
1852 " use --grow --array-size first to truncate array.\n"
1853 " e.g. mdadm --grow %s --array-size %llu\n",
1854 devname, reshape.new_size/2);
5da9ab98
N
1855 goto release;
1856 }
7236ee7a 1857
5da9ab98 1858 sra = sysfs_read(fd, 0,
3656edcd 1859 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK|
5da9ab98 1860 GET_CACHE);
5da9ab98
N
1861 if (!sra) {
1862 fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n",
1863 devname);
5da9ab98
N
1864 goto release;
1865 }
7236ee7a 1866
5da9ab98
N
1867 /* Decide how many blocks (sectors) for a reshape
1868 * unit. The number we have so far is just a minimum
1869 */
b4f8e38b 1870 blocks = reshape.backup_blocks;
5da9ab98
N
1871 if (reshape.before.data_disks ==
1872 reshape.after.data_disks) {
1873 /* Make 'blocks' bigger for better throughput, but
1874 * not so big that we reject it below.
1875 * Try for 16 megabytes
1876 */
1877 while (blocks * 32 < sra->component_size &&
1878 blocks < 16*1024*2)
1879 blocks *= 2;
1880 } else
1881 fprintf(stderr, Name ": Need to backup %luK of critical "
1882 "section..\n", blocks/2);
1883
1884 if (blocks >= sra->component_size/2) {
1885 fprintf(stderr, Name ": %s: Something wrong"
1886 " - reshape aborted\n",
1887 devname);
5da9ab98
N
1888 goto release;
1889 }
7236ee7a 1890
5da9ab98
N
1891 /* Now we need to open all these devices so we can read/write.
1892 */
b8b286a6
N
1893 nrdisks = max(reshape.before.data_disks,
1894 reshape.after.data_disks) + reshape.parity
1895 + sra->array.spare_disks;
5da9ab98
N
1896 fdlist = malloc((1+nrdisks) * sizeof(int));
1897 offsets = malloc((1+nrdisks) * sizeof(offsets[0]));
1898 if (!fdlist || !offsets) {
1899 fprintf(stderr, Name ": malloc failed: grow aborted\n");
5da9ab98
N
1900 goto release;
1901 }
e380d3be 1902
b8b286a6
N
1903 odisks = reshape.before.data_disks + reshape.parity;
1904 d = reshape_prepare_fdlist(devname, sra, odisks,
5da9ab98
N
1905 nrdisks, blocks, backup_file,
1906 fdlist, offsets);
1907 if (d < 0) {
5da9ab98
N
1908 goto release;
1909 }
1910 if (backup_file == NULL) {
374ba335 1911 if (reshape.after.data_disks <= reshape.before.data_disks) {
5da9ab98
N
1912 fprintf(stderr,
1913 Name ": %s: Cannot grow - need backup-file\n",
c03ef02d 1914 devname);
5da9ab98
N
1915 goto release;
1916 } else if (sra->array.spare_disks == 0) {
1917 fprintf(stderr, Name ": %s: Cannot grow - need a spare or "
1918 "backup-file to backup critical section\n",
e86c9dd6 1919 devname);
5da9ab98 1920 goto release;
e86c9dd6 1921 }
5da9ab98
N
1922 } else {
1923 if (!reshape_open_backup_file(backup_file, fd, devname,
1924 (signed)blocks,
a93f87ee 1925 fdlist+d, offsets+d, restart)) {
130994cb 1926 goto release;
e86c9dd6 1927 }
5da9ab98
N
1928 d++;
1929 }
7236ee7a 1930
5da9ab98
N
1931 /* lastly, check that the internal stripe cache is
1932 * large enough, or it won't work.
1933 * It must hold at least 4 stripes of the larger
1934 * chunk size
1935 */
1936 cache = max(info->array.chunk_size, info->new_chunk);
1937 cache *= 4; /* 4 stripes minimum */
1938 cache /= 512; /* convert to sectors */
1939 disks = min(reshape.before.data_disks, reshape.after.data_disks);
1940 /* make sure there is room for 'blocks' with a bit to spare */
1941 if (cache < 16 + blocks / disks)
1942 cache = 16 + blocks / disks;
1943 cache /= (4096/512); /* Covert from sectors to pages */
1944
1945 if (sra->cache_size < cache)
1946 subarray_set_num(container, sra, "stripe_cache_size",
1947 cache+1);
1948
1949 /* Right, everything seems fine. Let's kick things off.
1950 * If only changing raid_disks, use ioctl, else use
1951 * sysfs.
1952 */
1953 sync_metadata(st);
1954
b4f8e38b 1955 sra->new_chunk = info->new_chunk;
ddee071d 1956
20a40eca 1957 if (restart)
ddee071d 1958 sra->reshape_progress = info->reshape_progress;
f897078e
N
1959 else {
1960 sra->reshape_progress = 0;
1961 if (reshape.after.data_disks < reshape.before.data_disks)
1962 /* start from the end of the new array */
1963 sra->reshape_progress = (sra->component_size
1964 * reshape.after.data_disks);
1965 }
1966
1967 if (info->array.chunk_size == info->new_chunk &&
7477d513
AK
1968 reshape.before.layout == reshape.after.layout &&
1969 st->ss->external == 0) {
f897078e 1970 /* use SET_ARRAY_INFO but only if reshape hasn't started */
6ef421be 1971 ioctl(fd, GET_ARRAY_INFO, &array);
5da9ab98 1972 array.raid_disks = reshape.after.data_disks + reshape.parity;
20a40eca 1973 if (!restart &&
f897078e 1974 ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
5da9ab98 1975 int err = errno;
631d7405 1976
5da9ab98
N
1977 fprintf(stderr,
1978 Name ": Cannot set device shape for %s: %s\n",
1979 devname, strerror(errno));
7bc71196 1980
5da9ab98
N
1981 if (err == EBUSY &&
1982 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
1983 fprintf(stderr,
1984 " Bitmap must be removed before"
1985 " shape can be changed\n");
b5420ef3 1986
5da9ab98
N
1987 goto release;
1988 }
20a40eca 1989 } else if (!restart) {
5da9ab98 1990 /* set them all just in case some old 'new_*' value
f897078e 1991 * persists from some earlier problem.
7236ee7a 1992 */
631d7405 1993 int err = 0;
5da9ab98 1994 if (sysfs_set_num(sra, NULL, "chunk_size", info->new_chunk) < 0)
631d7405
N
1995 err = errno;
1996 if (!err && sysfs_set_num(sra, NULL, "layout",
5da9ab98 1997 reshape.after.layout) < 0)
631d7405
N
1998 err = errno;
1999 if (!err && subarray_set_num(container, sra, "raid_disks",
5da9ab98
N
2000 reshape.after.data_disks +
2001 reshape.parity) < 0)
631d7405
N
2002 err = errno;
2003 if (err) {
5da9ab98
N
2004 fprintf(stderr, Name ": Cannot set device shape for %s\n",
2005 devname);
7236ee7a 2006
5da9ab98
N
2007 if (err == EBUSY &&
2008 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
2009 fprintf(stderr,
2010 " Bitmap must be removed before"
2011 " shape can be changed\n");
2012 goto release;
e86c9dd6 2013 }
5da9ab98 2014 }
e86c9dd6 2015
20a40eca 2016 err = start_reshape(sra, restart);
fab32c97 2017 if (err) {
20a40eca
N
2018 fprintf(stderr,
2019 Name ": Cannot %s reshape for %s\n",
2020 restart ? "continue" : "start",
fab32c97
AK
2021 devname);
2022 goto release;
2023 }
a93f87ee
N
2024 if (restart)
2025 sysfs_set_str(sra, NULL, "array_state", "active");
5da9ab98
N
2026
2027 /* Now we just need to kick off the reshape and watch, while
2028 * handling backups of the data...
2029 * This is all done by a forked background process.
2030 */
2031 switch(forked ? 0 : fork()) {
6b2d630c
N
2032 case -1:
2033 fprintf(stderr, Name ": Cannot run child to monitor reshape: %s\n",
2034 strerror(errno));
6b2d630c 2035 abort_reshape(sra);
631d7405 2036 goto release;
6b2d630c
N
2037 default:
2038 return 0;
5da9ab98 2039 case 0:
6b2d630c
N
2040 break;
2041 }
5da9ab98 2042
6b2d630c
N
2043 close(fd);
2044 if (check_env("MDADM_GROW_VERIFY"))
2045 fd = open(devname, O_RDONLY | O_DIRECT);
2046 else
2047 fd = -1;
2048 mlockall(MCL_FUTURE);
5da9ab98 2049
6b2d630c
N
2050 if (st->ss->external) {
2051 /* metadata handler takes it from here */
2052 done = st->ss->manage_reshape(
2053 fd, sra, &reshape, st, blocks,
2054 fdlist, offsets,
2055 d - odisks, fdlist+odisks,
2056 offsets+odisks);
2057 } else
2058 done = child_monitor(
2059 fd, sra, &reshape, st, blocks,
2060 fdlist, offsets,
2061 d - odisks, fdlist+odisks,
2062 offsets+odisks);
2063
2064 if (backup_file && done)
2065 unlink(backup_file);
2066 if (!done) {
2067 abort_reshape(sra);
2068 goto out;
2069 }
1cbc5680
N
2070
2071 if (!st->ss->external &&
2072 !(reshape.before.data_disks != reshape.after.data_disks
2073 && info->custom_array_size) &&
2074 info->new_level == reshape.level &&
2075 !forked) {
2076 /* no need to wait for the reshape to finish as
2077 * there is nothing more to do.
2078 */
2079 exit(0);
2080 }
2081 wait_reshape(sra);
2082
2083 if (st->ss->external) {
2084 /* Re-load the metadata as much could have changed */
2085 int cfd = open_dev(st->container_dev);
2086 if (cfd >= 0) {
2087 ping_monitor(container);
2088 st->ss->free_super(st);
2089 st->ss->load_container(st, cfd, container);
2090 close(cfd);
2091 }
2092 }
2093
6b2d630c
N
2094 /* set new array size if required customer_array_size is used
2095 * by this metadata.
2096 */
2097 if (reshape.before.data_disks !=
2098 reshape.after.data_disks &&
2099 info->custom_array_size) {
2100 struct mdinfo *info2;
2101 char *subarray = strchr(info->text_version+1, '/')+1;
582496b2 2102
6b2d630c
N
2103 info2 = st->ss->container_content(st, subarray);
2104 if (info2) {
2105 unsigned long long current_size = 0;
2106 unsigned long long new_size =
2107 info2->custom_array_size/2;
2108
2109 if (sysfs_get_ll(sra,
2110 NULL,
2111 "array_size",
2112 &current_size) == 0 &&
2113 new_size > current_size) {
2114 if (sysfs_set_num(sra, NULL,
2115 "array_size", new_size)
2116 < 0)
2117 dprintf("Error: Cannot"
2118 " set array size");
2119 else
2120 dprintf("Array size "
2121 "changed");
2122 dprintf(" from %llu to %llu.\n",
2123 current_size, new_size);
582496b2 2124 }
6b2d630c 2125 sysfs_free(info2);
582496b2 2126 }
6b2d630c 2127 }
582496b2 2128
6b2d630c 2129 if (info->new_level != reshape.level) {
582496b2 2130
6b2d630c 2131 c = map_num(pers, info->new_level);
1cbc5680
N
2132 if (c) {
2133 err = sysfs_set_str(sra, NULL, "level", c);
2134 if (err)
2135 fprintf(stderr, Name\
2136 ": %s: could not set level "
2137 "to %s\n", devname, c);
2138 }
7236ee7a 2139 }
6b2d630c
N
2140out:
2141 if (forked)
2142 return 0;
e84f2c00 2143 unfreeze(st);
6b2d630c 2144 exit(0);
e86c9dd6 2145
6b2d630c 2146release:
1cbc5680 2147 if (orig_level != UnSet && sra) {
7236ee7a
N
2148 c = map_num(pers, orig_level);
2149 if (c && sysfs_set_str(sra, NULL, "level", c) == 0)
2150 fprintf(stderr, Name ": aborting level change\n");
2151 }
9202b8eb
N
2152 if (!forked)
2153 unfreeze(st);
631d7405 2154 return 1;
7236ee7a 2155}
e86c9dd6 2156
493f5dd6 2157int reshape_container(char *container, char *devname,
5da9ab98
N
2158 struct supertype *st,
2159 struct mdinfo *info,
2160 int force,
2161 char *backup_file,
493f5dd6 2162 int quiet, int restart)
5da9ab98 2163{
1bb174ba
AK
2164 struct mdinfo *cc = NULL;
2165
d8eb27f7
KA
2166 /* component_size is not meaningful for a container,
2167 * so pass '-1' meaning 'no change'
2168 */
493f5dd6
N
2169 if (!restart &&
2170 reshape_super(st, -1, info->new_level,
5da9ab98 2171 info->new_layout, info->new_chunk,
41784c88 2172 info->array.raid_disks, info->delta_disks,
9e325442
AK
2173 backup_file, devname, quiet)) {
2174 unfreeze(st);
5da9ab98 2175 return 1;
9e325442 2176 }
5da9ab98
N
2177
2178 sync_metadata(st);
2179
1bb174ba
AK
2180 /* ping monitor to be sure that update is on disk
2181 */
2182 ping_monitor(container);
5da9ab98
N
2183
2184 switch (fork()) {
2185 case -1: /* error */
2186 perror("Cannot fork to complete reshape\n");
9e325442 2187 unfreeze(st);
5da9ab98
N
2188 return 1;
2189 default: /* parent */
2190 printf(Name ": multi-array reshape continues in background\n");
2191 return 0;
2192 case 0: /* child */
2193 break;
2194 }
2195
1bb174ba
AK
2196 while(1) {
2197 /* For each member array with reshape_active,
2198 * we need to perform the reshape.
2199 * We pick the first array that needs reshaping and
2200 * reshape it. reshape_array() will re-read the metadata
2201 * so the next time through a different array should be
2202 * ready for reshape.
493f5dd6
N
2203 * It is possible that the 'different' array will not
2204 * be assembled yet. In that case we simple exit.
2205 * When it is assembled, the mdadm which assembles it
2206 * will take over the reshape.
1bb174ba
AK
2207 */
2208 struct mdinfo *content;
5da9ab98
N
2209 int rv;
2210 int fd;
2211 struct mdstat_ent *mdstat;
5da9ab98
N
2212 char *adev;
2213
1bb174ba 2214 sysfs_free(cc);
5da9ab98 2215
1bb174ba
AK
2216 cc = st->ss->container_content(st, NULL);
2217
2218 for (content = cc; content ; content = content->next) {
2219 char *subarray;
2220 if (!content->reshape_active)
2221 continue;
2222
2223 subarray = strchr(content->text_version+1, '/')+1;
2224 mdstat = mdstat_by_subdev(subarray,
2225 devname2devnum(container));
2226 if (!mdstat)
2227 continue;
2228 break;
2229 }
2230 if (!content)
2231 break;
5da9ab98 2232
55f14721 2233 fd = open_dev(mdstat->devnum);
5da9ab98
N
2234 if (fd < 0)
2235 break;
2236 adev = map_dev(dev2major(mdstat->devnum),
2237 dev2minor(mdstat->devnum),
2238 0);
2239 if (!adev)
1bb174ba
AK
2240 adev = content->text_version;
2241
2242 sysfs_init(content, fd, mdstat->devnum);
5da9ab98 2243
1bb174ba
AK
2244 rv = reshape_array(container, fd, adev, st,
2245 content, force,
493f5dd6 2246 backup_file, quiet, 1, restart);
5da9ab98 2247 close(fd);
493f5dd6 2248 restart = 0;
5da9ab98
N
2249 if (rv)
2250 break;
2251 }
9202b8eb 2252 unfreeze(st);
1bb174ba 2253 sysfs_free(cc);
5da9ab98
N
2254 exit(0);
2255}
2256
7236ee7a
N
2257/*
2258 * We run a child process in the background which performs the following
2259 * steps:
2260 * - wait for resync to reach a certain point
2261 * - suspend io to the following section
2262 * - backup that section
2263 * - allow resync to proceed further
2264 * - resume io
2265 * - discard the backup.
2266 *
2267 * When are combined in slightly different ways in the three cases.
2268 * Grow:
2269 * - suspend/backup/allow/wait/resume/discard
2270 * Shrink:
2271 * - allow/wait/suspend/backup/allow/wait/resume/discard
2272 * same-size:
2273 * - wait/resume/discard/suspend/backup/allow
2274 *
2275 * suspend/backup/allow always come together
2276 * wait/resume/discard do too.
2277 * For the same-size case we have two backups to improve flow.
2278 *
2279 */
e86c9dd6 2280
7443ee81
N
2281int progress_reshape(struct mdinfo *info, struct reshape *reshape,
2282 unsigned long long backup_point,
2283 unsigned long long wait_point,
2284 unsigned long long *suspend_point,
2285 unsigned long long *reshape_completed)
2286{
2287 /* This function is called repeatedly by the reshape manager.
2288 * It determines how much progress can safely be made and allows
2289 * that progress.
2290 * - 'info' identifies the array and particularly records in
2291 * ->reshape_progress the metadata's knowledge of progress
2292 * This is a sector offset from the start of the array
2293 * of the next array block to be relocated. This number
2294 * may increase from 0 or decrease from array_size, depending
2295 * on the type of reshape that is happening.
2296 * Note that in contrast, 'sync_completed' is a block count of the
2297 * reshape so far. It gives the distance between the start point
2298 * (head or tail of device) and the next place that data will be
2299 * written. It always increases.
2300 * - 'reshape' is the structure created by analyse_change
2301 * - 'backup_point' shows how much the metadata manager has backed-up
2302 * data. For reshapes with increasing progress, it is the next address
2303 * to be backed up, previous addresses have been backed-up. For
2304 * decreasing progress, it is the earliest address that has been
2305 * backed up - later address are also backed up.
2306 * So addresses between reshape_progress and backup_point are
2307 * backed up providing those are in the 'correct' order.
2308 * - 'wait_point' is an array address. When reshape_completed
2309 * passes this point, progress_reshape should return. It might
2310 * return earlier if it determines that ->reshape_progress needs
2311 * to be updated or further backup is needed.
2312 * - suspend_point is maintained by progress_reshape and the caller
2313 * should not touch it except to initialise to zero.
2314 * It is an array address and it only increases in 2.6.37 and earlier.
b6b95155 2315 * This makes it difficult to handle reducing reshapes with
7443ee81
N
2316 * external metadata.
2317 * However: it is similar to backup_point in that it records the
2318 * other end of a suspended region from reshape_progress.
2319 * it is moved to extend the region that is safe to backup and/or
2320 * reshape
2321 * - reshape_completed is read from sysfs and returned. The caller
2322 * should copy this into ->reshape_progress when it has reason to
2323 * believe that the metadata knows this, and any backup outside this
2324 * has been erased.
2325 *
2326 * Return value is:
2327 * 1 if more data from backup_point - but only as far as suspend_point,
2328 * should be backed up
2329 * 0 if things are progressing smoothly
9e034f70
N
2330 * -1 if the reshape is finished because it is all done,
2331 * -2 if the reshape is finished due to an error.
7443ee81
N
2332 */
2333
2334 int advancing = (reshape->after.data_disks
2335 >= reshape->before.data_disks);
38dad34a
N
2336 unsigned long long need_backup; /* All data between start of array and
2337 * here will at some point need to
2338 * be backed up.
d0ab945e 2339 */
7443ee81 2340 unsigned long long read_offset, write_offset;
b4f8e38b 2341 unsigned long long write_range;
7443ee81 2342 unsigned long long max_progress, target, completed;
d0ab945e
N
2343 unsigned long long array_size = (info->component_size
2344 * reshape->before.data_disks);
7443ee81 2345 int fd;
77a73a17 2346 char buf[20];
7443ee81
N
2347
2348 /* First, we unsuspend any region that is now known to be safe.
2349 * If suspend_point is on the 'wrong' side of reshape_progress, then
2350 * we don't have or need suspension at the moment. This is true for
2351 * native metadata when we don't need to back-up.
2352 */
2353 if (advancing) {
49cd738b 2354 if (info->reshape_progress <= *suspend_point)
7443ee81
N
2355 sysfs_set_num(info, NULL, "suspend_lo",
2356 info->reshape_progress);
2357 } else {
2358 /* Note: this won't work in 2.6.37 and before.
2359 * Something somewhere should make sure we don't need it!
2360 */
49cd738b 2361 if (info->reshape_progress >= *suspend_point)
7443ee81
N
2362 sysfs_set_num(info, NULL, "suspend_hi",
2363 info->reshape_progress);
2364 }
2365
2366 /* Now work out how far it is safe to progress.
2367 * If the read_offset for ->reshape_progress is less than
2368 * 'blocks' beyond the write_offset, we can only progress as far
2369 * as a backup.
2370 * Otherwise we can progress until the write_offset for the new location
2371 * reaches (within 'blocks' of) the read_offset at the current location.
2372 * However that region must be suspended unless we are using native
2373 * metadata.
2374 * If we need to suspend more, we limit it to 128M per device, which is
2375 * rather arbitrary and should be some time-based calculation.
2376 */
ec757320
N
2377 read_offset = info->reshape_progress / reshape->before.data_disks;
2378 write_offset = info->reshape_progress / reshape->after.data_disks;
b4f8e38b 2379 write_range = info->new_chunk/512;
38dad34a
N
2380 if (reshape->before.data_disks == reshape->after.data_disks)
2381 need_backup = array_size;
2382 else
2383 need_backup = reshape->backup_blocks;
7443ee81 2384 if (advancing) {
38dad34a 2385 if (read_offset < write_offset + write_range)
7443ee81 2386 max_progress = backup_point;
ddee071d 2387 else
7443ee81 2388 max_progress =
2f3dd5e4
N
2389 read_offset *
2390 reshape->after.data_disks;
7443ee81 2391 } else {
38dad34a
N
2392 if (read_offset > write_offset - write_range)
2393 /* Can only progress as far as has been backed up,
2394 * which must be suspended */
7443ee81 2395 max_progress = backup_point;
38dad34a
N
2396 else if (info->reshape_progress <= need_backup)
2397 max_progress = backup_point;
2398 else {
2399 if (info->array.major_version >= 0)
2400 /* Can progress until backup is needed */
2401 max_progress = need_backup;
2402 else {
2403 /* Can progress until metadata update is required */
2404 max_progress =
2405 read_offset *
2406 reshape->after.data_disks;
2407 /* but data must be suspended */
2408 if (max_progress < *suspend_point)
2409 max_progress = *suspend_point;
2410 }
7443ee81
N
2411 }
2412 }
2413
2414 /* We know it is safe to progress to 'max_progress' providing
2415 * it is suspended or we are using native metadata.
2416 * Consider extending suspend_point 128M per device if it
2417 * is less than 64M per device beyond reshape_progress.
2418 * But always do a multiple of 'blocks'
832b2b9a
N
2419 * FIXME this is too big - it takes to long to complete
2420 * this much.
7443ee81
N
2421 */
2422 target = 64*1024*2 * min(reshape->before.data_disks,
2423 reshape->after.data_disks);
b4f8e38b 2424 target /= reshape->backup_blocks;
7443ee81
N
2425 if (target < 2)
2426 target = 2;
b4f8e38b 2427 target *= reshape->backup_blocks;
7443ee81
N
2428
2429 /* For externally managed metadata we always need to suspend IO to
2430 * the area being reshaped so we regularly push suspend_point forward.
2431 * For native metadata we only need the suspend if we are going to do
2432 * a backup.
2433 */
2434 if (advancing) {
d0ab945e
N
2435 if ((need_backup > info->reshape_progress
2436 || info->array.major_version < 0) &&
7443ee81 2437 *suspend_point < info->reshape_progress + target) {
d0ab945e
N
2438 if (need_backup < *suspend_point + 2 * target)
2439 *suspend_point = need_backup;
2440 else if (*suspend_point + 2 * target < array_size)
7443ee81 2441 *suspend_point += 2 * target;
d0ab945e
N
2442 else
2443 *suspend_point = array_size;
7443ee81 2444 sysfs_set_num(info, NULL, "suspend_hi", *suspend_point);
d0ab945e
N
2445 if (max_progress > *suspend_point)
2446 max_progress = *suspend_point;
7443ee81
N
2447 }
2448 } else {
38dad34a
N
2449 if (info->array.major_version >= 0) {
2450 /* Only need to suspend when about to backup */
2451 if (info->reshape_progress < need_backup * 2 &&
2452 *suspend_point > 0) {
d0ab945e 2453 *suspend_point = 0;
38dad34a
N
2454 sysfs_set_num(info, NULL, "suspend_lo", 0);
2455 sysfs_set_num(info, NULL, "suspend_hi", need_backup);
2456 }
2457 } else {
2458 /* Need to suspend continually */
2459 if (info->reshape_progress < *suspend_point)
2460 *suspend_point = info->reshape_progress;
2461 if (*suspend_point + target < info->reshape_progress)
2462 /* No need to move suspend region yet */;
2463 else {
2464 if (*suspend_point >= 2 * target)
2465 *suspend_point -= 2 * target;
2466 else
2467 *suspend_point = 0;
2468 sysfs_set_num(info, NULL, "suspend_lo",
2469 *suspend_point);
2470 }
d0ab945e
N
2471 if (max_progress < *suspend_point)
2472 max_progress = *suspend_point;
7443ee81
N
2473 }
2474 }
2475
2476 /* now set sync_max to allow that progress. sync_max, like
2477 * sync_completed is a count of sectors written per device, so
2478 * we find the difference between max_progress and the start point,
2479 * and divide that by after.data_disks to get a sync_max
2480 * number.
2481 * At the same time we convert wait_point to a similar number
2482 * for comparing against sync_completed.
2483 */
92d1991f 2484 /* scale down max_progress to per_disk */
7443ee81 2485 max_progress /= reshape->after.data_disks;
92d1991f
N
2486 /* Round to chunk size as some kernels give an erroneously high number */
2487 max_progress /= info->new_chunk/512;
2488 max_progress *= info->new_chunk/512;
7f913e9b
N
2489 /* And round to old chunk size as the kernel wants that */
2490 max_progress /= info->array.chunk_size/512;
2491 max_progress *= info->array.chunk_size/512;
92d1991f
N
2492 /* Limit progress to the whole device */
2493 if (max_progress > info->component_size)
2494 max_progress = info->component_size;
7443ee81 2495 wait_point /= reshape->after.data_disks;
92d1991f
N
2496 if (!advancing) {
2497 /* switch from 'device offset' to 'processed block count' */
2498 max_progress = info->component_size - max_progress;
2499 wait_point = info->component_size - wait_point;
2500 }
7443ee81
N
2501
2502 sysfs_set_num(info, NULL, "sync_max", max_progress);
2503
2504 /* Now wait. If we have already reached the point that we were
2505 * asked to wait to, don't wait at all, else wait for any change.
2506 * We need to select on 'sync_completed' as that is the place that
2507 * notifications happen, but we are really interested in
2508 * 'reshape_position'
2509 */
2510 fd = sysfs_get_fd(info, NULL, "sync_completed");
2511 if (fd < 0)
77a73a17 2512 goto check_progress;
7443ee81
N
2513
2514 if (sysfs_fd_get_ll(fd, &completed) < 0) {
2515 close(fd);
77a73a17 2516 goto check_progress;
7443ee81
N
2517 }
2518 while (completed < max_progress && completed < wait_point) {
2519 /* Check that sync_action is still 'reshape' to avoid
2520 * waiting forever on a dead array
2521 */
2522 char action[20];
2523 fd_set rfds;
2524 if (sysfs_get_str(info, NULL, "sync_action",
2525 action, 20) <= 0 ||
2526 strncmp(action, "reshape", 7) != 0)
2527 break;
26d6e157
AK
2528 /* Some kernels reset 'sync_completed' to zero
2529 * before setting 'sync_action' to 'idle'.
2530 * So we need these extra tests.
2531 */
2532 if (completed == 0 && advancing
2533 && info->reshape_progress > 0)
2534 break;
2535 if (completed == 0 && !advancing
2536 && info->reshape_progress < (info->component_size
2537 * reshape->after.data_disks))
2538 break;
7443ee81
N
2539 FD_ZERO(&rfds);
2540 FD_SET(fd, &rfds);
2541 select(fd+1, NULL, NULL, &rfds, NULL);
2542 if (sysfs_fd_get_ll(fd, &completed) < 0) {
2543 close(fd);
77a73a17 2544 goto check_progress;
7443ee81
N
2545 }
2546 }
10d0d365
AK
2547 /* Some kernels reset 'sync_completed' to zero,
2548 * we need to have real point we are in md
2549 */
2550 if (completed == 0)
2551 completed = max_progress;
2552
92d1991f
N
2553 /* some kernels can give an incorrectly high 'completed' number */
2554 completed /= (info->new_chunk/512);
2555 completed *= (info->new_chunk/512);
7443ee81
N
2556 /* Convert 'completed' back in to a 'progress' number */
2557 completed *= reshape->after.data_disks;
2558 if (!advancing) {
2559 completed = info->component_size * reshape->after.data_disks
2560 - completed;
2561 }
2562 *reshape_completed = completed;
2563
2564 close(fd);
2565
2566 /* We return the need_backup flag. Caller will decide
d0ab945e 2567 * how much - a multiple of ->backup_blocks up to *suspend_point
7443ee81 2568 */
38dad34a
N
2569 if (advancing)
2570 return need_backup > info->reshape_progress;
2571 else
2572 return need_backup >= info->reshape_progress;
77a73a17
N
2573
2574check_progress:
2575 /* if we couldn't read a number from sync_completed, then
2576 * either the reshape did complete, or it aborted.
2577 * We can tell which by checking for 'none' in reshape_position.
2578 */
2579 strcpy(buf, "hi");
2580 if (sysfs_get_str(info, NULL, "reshape_position", buf, sizeof(buf)) < 0
2581 || strncmp(buf, "none", 4) != 0)
2582 return -2; /* abort */
6d5316f6
N
2583 else {
2584 /* Maybe racing with array shutdown - check state */
2585 if (sysfs_get_str(info, NULL, "array_state", buf, sizeof(buf)) < 0
2586 || strncmp(buf, "inactive", 8) == 0
2587 || strncmp(buf, "clear",5) == 0)
2588 return -2; /* abort */
77a73a17 2589 return -1; /* complete */
6d5316f6 2590 }
7443ee81
N
2591}
2592
2593
fcf57625 2594/* FIXME return status is never checked */
4411fb17 2595static int grow_backup(struct mdinfo *sra,
7236ee7a 2596 unsigned long long offset, /* per device */
7443ee81 2597 unsigned long stripes, /* per device, in old chunks */
7236ee7a
N
2598 int *sources, unsigned long long *offsets,
2599 int disks, int chunk, int level, int layout,
2600 int dests, int *destfd, unsigned long long *destoffsets,
d4445387 2601 int part, int *degraded,
7236ee7a
N
2602 char *buf)
2603{
2604 /* Backup 'blocks' sectors at 'offset' on each device of the array,
2605 * to storage 'destfd' (offset 'destoffsets'), after first
2606 * suspending IO. Then allow resync to continue
2607 * over the suspended section.
2608 * Use part 'part' of the backup-super-block.
2609 */
2610 int odata = disks;
2611 int rv = 0;
2612 int i;
f21e18ca
N
2613 unsigned long long ll;
2614 int new_degraded;
7236ee7a
N
2615 //printf("offset %llu\n", offset);
2616 if (level >= 4)
2617 odata--;
2618 if (level == 6)
2619 odata--;
7443ee81 2620
d4445387 2621 /* Check that array hasn't become degraded, else we might backup the wrong data */
2c6ac128
N
2622 if (sysfs_get_ll(sra, NULL, "degraded", &ll) < 0)
2623 return -1; /* FIXME this error is ignored */
f21e18ca 2624 new_degraded = (int)ll;
d4445387
N
2625 if (new_degraded != *degraded) {
2626 /* check each device to ensure it is still working */
2627 struct mdinfo *sd;
2628 for (sd = sra->devs ; sd ; sd = sd->next) {
2629 if (sd->disk.state & (1<<MD_DISK_FAULTY))
2630 continue;
2631 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
2632 char sbuf[20];
2633 if (sysfs_get_str(sra, sd, "state", sbuf, 20) < 0 ||
2634 strstr(sbuf, "faulty") ||
2635 strstr(sbuf, "in_sync") == NULL) {
2636 /* this device is dead */
2637 sd->disk.state = (1<<MD_DISK_FAULTY);
2638 if (sd->disk.raid_disk >= 0 &&
2639 sources[sd->disk.raid_disk] >= 0) {
2640 close(sources[sd->disk.raid_disk]);
2641 sources[sd->disk.raid_disk] = -1;
2642 }
2643 }
2644 }
2645 }
2646 *degraded = new_degraded;
2647 }
7236ee7a
N
2648 if (part) {
2649 bsb.arraystart2 = __cpu_to_le64(offset * odata);
200871ad 2650 bsb.length2 = __cpu_to_le64(stripes * (chunk/512) * odata);
7236ee7a
N
2651 } else {
2652 bsb.arraystart = __cpu_to_le64(offset * odata);
200871ad 2653 bsb.length = __cpu_to_le64(stripes * (chunk/512) * odata);
7236ee7a
N
2654 }
2655 if (part)
2656 bsb.magic[15] = '2';
2657 for (i = 0; i < dests; i++)
2658 if (part)
2659 lseek64(destfd[i], destoffsets[i] + __le64_to_cpu(bsb.devstart2)*512, 0);
2660 else
2661 lseek64(destfd[i], destoffsets[i], 0);
2662
2663 rv = save_stripes(sources, offsets,
2664 disks, chunk, level, layout,
2665 dests, destfd,
2666 offset*512*odata, stripes * chunk * odata,
2667 buf);
2668
2669 if (rv)
2670 return rv;
97396422 2671 bsb.mtime = __cpu_to_le64(time(0));
7236ee7a
N
2672 for (i = 0; i < dests; i++) {
2673 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
2674
2675 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
2676 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
2677 bsb.sb_csum2 = bsb_csum((char*)&bsb,
2678 ((char*)&bsb.sb_csum2)-((char*)&bsb));
2679
72044953 2680 rv = -1;
f21e18ca
N
2681 if ((unsigned long long)lseek64(destfd[i], destoffsets[i] - 4096, 0)
2682 != destoffsets[i] - 4096)
72044953
N
2683 break;
2684 if (write(destfd[i], &bsb, 512) != 512)
2685 break;
ff94fb86 2686 if (destoffsets[i] > 4096) {
f21e18ca 2687 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]+stripes*chunk*odata, 0) !=
a847575a 2688 destoffsets[i]+stripes*chunk*odata)
72044953
N
2689 break;
2690 if (write(destfd[i], &bsb, 512) != 512)
2691 break;
ff94fb86 2692 }
7236ee7a 2693 fsync(destfd[i]);
72044953 2694 rv = 0;
7236ee7a 2695 }
2efedc7b 2696
fcf57625 2697 return rv;
7236ee7a
N
2698}
2699
2700/* in 2.6.30, the value reported by sync_completed can be
2701 * less that it should be by one stripe.
2702 * This only happens when reshape hits sync_max and pauses.
2703 * So allow wait_backup to either extent sync_max further
2704 * than strictly necessary, or return before the
2705 * sync has got quite as far as we would really like.
2706 * This is what 'blocks2' is for.
2707 * The various caller give appropriate values so that
2708 * every works.
2709 */
fcf57625 2710/* FIXME return value is often ignored */
7443ee81 2711static int forget_backup(
7236ee7a
N
2712 int dests, int *destfd, unsigned long long *destoffsets,
2713 int part)
2714{
7443ee81
N
2715 /*
2716 * Erase backup 'part' (which is 0 or 1)
7236ee7a 2717 */
7236ee7a 2718 int i;
fcf57625 2719 int rv;
7236ee7a 2720
7236ee7a
N
2721 if (part) {
2722 bsb.arraystart2 = __cpu_to_le64(0);
2723 bsb.length2 = __cpu_to_le64(0);
2724 } else {
2725 bsb.arraystart = __cpu_to_le64(0);
2726 bsb.length = __cpu_to_le64(0);
2727 }
97396422 2728 bsb.mtime = __cpu_to_le64(time(0));
fcf57625 2729 rv = 0;
7236ee7a
N
2730 for (i = 0; i < dests; i++) {
2731 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
2732 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
2733 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
2734 bsb.sb_csum2 = bsb_csum((char*)&bsb,
2735 ((char*)&bsb.sb_csum2)-((char*)&bsb));
f21e18ca 2736 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]-4096, 0) !=
a847575a 2737 destoffsets[i]-4096)
72044953
N
2738 rv = -1;
2739 if (rv == 0 &&
2740 write(destfd[i], &bsb, 512) != 512)
2741 rv = -1;
7236ee7a
N
2742 fsync(destfd[i]);
2743 }
fcf57625 2744 return rv;
7236ee7a 2745}
e86c9dd6 2746
7236ee7a
N
2747static void fail(char *msg)
2748{
fcf57625 2749 int rv;
72044953
N
2750 rv = (write(2, msg, strlen(msg)) != (int)strlen(msg));
2751 rv |= (write(2, "\n", 1) != 1);
fcf57625 2752 exit(rv ? 1 : 2);
7236ee7a
N
2753}
2754
2755static char *abuf, *bbuf;
f21e18ca 2756static unsigned long long abuflen;
7236ee7a
N
2757static void validate(int afd, int bfd, unsigned long long offset)
2758{
2759 /* check that the data in the backup against the array.
2760 * This is only used for regression testing and should not
2761 * be used while the array is active
2762 */
7236ee7a
N
2763 if (afd < 0)
2764 return;
2765 lseek64(bfd, offset - 4096, 0);
2766 if (read(bfd, &bsb2, 512) != 512)
2767 fail("cannot read bsb");
2768 if (bsb2.sb_csum != bsb_csum((char*)&bsb2,
2769 ((char*)&bsb2.sb_csum)-((char*)&bsb2)))
2770 fail("first csum bad");
2771 if (memcmp(bsb2.magic, "md_backup_data", 14) != 0)
2772 fail("magic is bad");
2773 if (memcmp(bsb2.magic, "md_backup_data-2", 16) == 0 &&
2774 bsb2.sb_csum2 != bsb_csum((char*)&bsb2,
2775 ((char*)&bsb2.sb_csum2)-((char*)&bsb2)))
2776 fail("second csum bad");
2777
2778 if (__le64_to_cpu(bsb2.devstart)*512 != offset)
2779 fail("devstart is wrong");
2780
2781 if (bsb2.length) {
2782 unsigned long long len = __le64_to_cpu(bsb2.length)*512;
2783
2784 if (abuflen < len) {
2785 free(abuf);
2786 free(bbuf);
2787 abuflen = len;
fcf57625
N
2788 if (posix_memalign((void**)&abuf, 4096, abuflen) ||
2789 posix_memalign((void**)&bbuf, 4096, abuflen)) {
2790 abuflen = 0;
2791 /* just stop validating on mem-alloc failure */
2792 return;
2793 }
e86c9dd6 2794 }
48924014 2795
7236ee7a 2796 lseek64(bfd, offset, 0);
f21e18ca 2797 if ((unsigned long long)read(bfd, bbuf, len) != len) {
080fd005 2798 //printf("len %llu\n", len);
7236ee7a
N
2799 fail("read first backup failed");
2800 }
2801 lseek64(afd, __le64_to_cpu(bsb2.arraystart)*512, 0);
f21e18ca 2802 if ((unsigned long long)read(afd, abuf, len) != len)
7236ee7a
N
2803 fail("read first from array failed");
2804 if (memcmp(bbuf, abuf, len) != 0) {
080fd005 2805 #if 0
7236ee7a
N
2806 int i;
2807 printf("offset=%llu len=%llu\n",
080fd005 2808 (unsigned long long)__le64_to_cpu(bsb2.arraystart)*512, len);
7236ee7a
N
2809 for (i=0; i<len; i++)
2810 if (bbuf[i] != abuf[i]) {
2811 printf("first diff byte %d\n", i);
93ecfa01 2812 break;
7236ee7a 2813 }
080fd005 2814 #endif
7236ee7a 2815 fail("data1 compare failed");
e86c9dd6 2816 }
7236ee7a
N
2817 }
2818 if (bsb2.length2) {
2819 unsigned long long len = __le64_to_cpu(bsb2.length2)*512;
2820
2821 if (abuflen < len) {
2822 free(abuf);
2823 free(bbuf);
2824 abuflen = len;
2825 abuf = malloc(abuflen);
2826 bbuf = malloc(abuflen);
e86c9dd6
NB
2827 }
2828
7236ee7a 2829 lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0);
f21e18ca 2830 if ((unsigned long long)read(bfd, bbuf, len) != len)
7236ee7a
N
2831 fail("read second backup failed");
2832 lseek64(afd, __le64_to_cpu(bsb2.arraystart2)*512, 0);
f21e18ca 2833 if ((unsigned long long)read(afd, abuf, len) != len)
7236ee7a
N
2834 fail("read second from array failed");
2835 if (memcmp(bbuf, abuf, len) != 0)
2836 fail("data2 compare failed");
e86c9dd6 2837 }
7236ee7a 2838}
e86c9dd6 2839
999b4972
N
2840int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
2841 struct supertype *st, unsigned long blocks,
2842 int *fds, unsigned long long *offsets,
2843 int dests, int *destfd, unsigned long long *destoffsets)
7236ee7a 2844{
7443ee81
N
2845 /* Monitor a reshape where backup is being performed using
2846 * 'native' mechanism - either to a backup file, or
2847 * to some space in a spare.
2848 */
7236ee7a 2849 char *buf;
7443ee81
N
2850 int degraded = -1;
2851 unsigned long long speed;
2852 unsigned long long suspend_point, array_size;
2853 unsigned long long backup_point, wait_point;
2854 unsigned long long reshape_completed;
2855 int done = 0;
2856 int increasing = reshape->after.data_disks >= reshape->before.data_disks;
2857 int part = 0; /* The next part of the backup area to fill. It may already
2858 * be full, so we need to check */
2859 int level = reshape->level;
2860 int layout = reshape->before.layout;
2861 int data = reshape->before.data_disks;
2862 int disks = reshape->before.data_disks + reshape->parity;
2863 int chunk = sra->array.chunk_size;
da8100ca
N
2864 struct mdinfo *sd;
2865 unsigned long stripes;
2866
2867 /* set up the backup-super-block. This requires the
2868 * uuid from the array.
2869 */
2870 /* Find a superblock */
2871 for (sd = sra->devs; sd; sd = sd->next) {
2872 char *dn;
2873 int devfd;
2874 int ok;
2875 if (sd->disk.state & (1<<MD_DISK_FAULTY))
2876 continue;
2877 dn = map_dev(sd->disk.major, sd->disk.minor, 1);
2878 devfd = dev_open(dn, O_RDONLY);
2879 if (devfd < 0)
2880 continue;
2881 ok = st->ss->load_super(st, devfd, NULL);
2882 close(devfd);
2883 if (ok >= 0)
2884 break;
2885 }
2886 if (!sd) {
2887 fprintf(stderr, Name ": Cannot find a superblock\n");
2888 return 0;
2889 }
2890
2891 memset(&bsb, 0, 512);
2892 memcpy(bsb.magic, "md_backup_data-1", 16);
2893 st->ss->uuid_from_super(st, (int*)&bsb.set_uuid);
2894 bsb.mtime = __cpu_to_le64(time(0));
2895 bsb.devstart2 = blocks;
2896
2897 stripes = blocks / (sra->array.chunk_size/512) /
2898 reshape->before.data_disks;
e86c9dd6 2899
fcf57625
N
2900 if (posix_memalign((void**)&buf, 4096, disks * chunk))
2901 /* Don't start the 'reshape' */
2902 return 0;
7443ee81
N
2903 if (reshape->before.data_disks == reshape->after.data_disks) {
2904 sysfs_get_ll(sra, NULL, "sync_speed_min", &speed);
2905 sysfs_set_num(sra, NULL, "sync_speed_min", 200000);
2906 }
e86c9dd6 2907
7443ee81 2908 if (increasing) {
96533072 2909 array_size = sra->component_size * reshape->after.data_disks;
7443ee81
N
2910 backup_point = sra->reshape_progress;
2911 suspend_point = 0;
2912 } else {
96533072 2913 array_size = sra->component_size * reshape->before.data_disks;
25da62d9 2914 backup_point = reshape->backup_blocks;
7443ee81
N
2915 suspend_point = array_size;
2916 }
7236ee7a 2917
7443ee81
N
2918 while (!done) {
2919 int rv;
7236ee7a 2920
7443ee81
N
2921 /* Want to return as soon the oldest backup slot can
2922 * be released as that allows us to start backing up
2923 * some more, providing suspend_point has been
b6b95155 2924 * advanced, which it should have.
7443ee81
N
2925 */
2926 if (increasing) {
2927 wait_point = array_size;
2928 if (part == 0 && __le64_to_cpu(bsb.length) > 0)
2929 wait_point = (__le64_to_cpu(bsb.arraystart) +
2930 __le64_to_cpu(bsb.length));
2931 if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
2932 wait_point = (__le64_to_cpu(bsb.arraystart2) +
2933 __le64_to_cpu(bsb.length2));
2934 } else {
2935 wait_point = 0;
2936 if (part == 0 && __le64_to_cpu(bsb.length) > 0)
2937 wait_point = __le64_to_cpu(bsb.arraystart);
2938 if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
2939 wait_point = __le64_to_cpu(bsb.arraystart2);
2940 }
2941
2942 rv = progress_reshape(sra, reshape,
2943 backup_point, wait_point,
2944 &suspend_point, &reshape_completed);
7443ee81
N
2945 /* external metadata would need to ping_monitor here */
2946 sra->reshape_progress = reshape_completed;
7236ee7a 2947
7443ee81
N
2948 /* Clear any backup region that is before 'here' */
2949 if (increasing) {
2950 if (reshape_completed >= (__le64_to_cpu(bsb.arraystart) +
2951 __le64_to_cpu(bsb.length)))
2952 forget_backup(dests, destfd,
2953 destoffsets, 0);
2954 if (reshape_completed >= (__le64_to_cpu(bsb.arraystart2) +
2955 __le64_to_cpu(bsb.length2)))
2956 forget_backup(dests, destfd,
2957 destoffsets, 1);
2958 } else {
2959 if (reshape_completed <= (__le64_to_cpu(bsb.arraystart)))
2960 forget_backup(dests, destfd,
2961 destoffsets, 0);
2962 if (reshape_completed <= (__le64_to_cpu(bsb.arraystart2)))
2963 forget_backup(dests, destfd,
2964 destoffsets, 1);
2965 }
7236ee7a 2966
223c5c99 2967 if (rv < 0) {
77a73a17
N
2968 if (rv == -1)
2969 done = 1;
223c5c99
N
2970 break;
2971 }
2972
60204e4e 2973 while (rv) {
7443ee81 2974 unsigned long long offset;
e97ca002 2975 unsigned long actual_stripes;
60204e4e
N
2976 /* Need to backup some data.
2977 * If 'part' is not used and the desired
2978 * backup size is suspended, do a backup,
2979 * then consider the next part.
2980 */
7443ee81
N
2981 /* Check that 'part' is unused */
2982 if (part == 0 && __le64_to_cpu(bsb.length) != 0)
60204e4e 2983 break;
7443ee81 2984 if (part == 1 && __le64_to_cpu(bsb.length2) != 0)
60204e4e 2985 break;
7443ee81
N
2986
2987 offset = backup_point / data;
e97ca002 2988 actual_stripes = stripes;
60204e4e 2989 if (increasing) {
e97ca002
N
2990 if (offset + actual_stripes * (chunk/512) >
2991 sra->component_size)
2992 actual_stripes = ((sra->component_size - offset)
2993 / (chunk/512));
2994 if (offset + actual_stripes * (chunk/512) >
60204e4e
N
2995 suspend_point/data)
2996 break;
2997 } else {
e97ca002
N
2998 if (offset < actual_stripes * (chunk/512))
2999 actual_stripes = offset / (chunk/512);
3000 offset -= actual_stripes * (chunk/512);
3001 if (offset < suspend_point/data)
60204e4e
N
3002 break;
3003 }
e4b11073
N
3004 if (actual_stripes == 0)
3005 break;
e97ca002 3006 grow_backup(sra, offset, actual_stripes,
7443ee81
N
3007 fds, offsets,
3008 disks, chunk, level, layout,
3009 dests, destfd, destoffsets,
3010 part, &degraded, buf);
3011 validate(afd, destfd[0], destoffsets[0]);
3012 /* record where 'part' is up to */
3013 part = !part;
3014 if (increasing)
e97ca002 3015 backup_point += actual_stripes * (chunk/512) * data;
7443ee81 3016 else
e97ca002 3017 backup_point -= actual_stripes * (chunk/512) * data;
7443ee81
N
3018 }
3019 }
3020
223c5c99
N
3021 /* FIXME maybe call progress_reshape one more time instead */
3022 abort_reshape(sra); /* remove any remaining suspension */
7443ee81
N
3023 if (reshape->before.data_disks == reshape->after.data_disks)
3024 sysfs_set_num(sra, NULL, "sync_speed_min", speed);
7236ee7a 3025 free(buf);
77a73a17 3026 return done;
e86c9dd6 3027}
353632d9
NB
3028
3029/*
3030 * If any spare contains md_back_data-1 which is recent wrt mtime,
3031 * write that data into the array and update the super blocks with
3032 * the new reshape_progress
3033 */
ea0ebe96
N
3034int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt,
3035 char *backup_file, int verbose)
353632d9
NB
3036{
3037 int i, j;
3038 int old_disks;
353632d9 3039 unsigned long long *offsets;
82f2d6ab 3040 unsigned long long nstripe, ostripe;
6e9eac4f 3041 int ndata, odata;
353632d9 3042
e9e43ec3
N
3043 if (info->new_level != info->array.level)
3044 return 1; /* Cannot handle level changes (they are instantaneous) */
3045
3046 odata = info->array.raid_disks - info->delta_disks - 1;
3047 if (info->array.level == 6) odata--; /* number of data disks */
3048 ndata = info->array.raid_disks - 1;
3049 if (info->new_level == 6) ndata--;
353632d9
NB
3050
3051 old_disks = info->array.raid_disks - info->delta_disks;
3052
e9e43ec3
N
3053 if (info->delta_disks <= 0)
3054 /* Didn't grow, so the backup file must have
3055 * been used
3056 */
3057 old_disks = cnt;
06b0d786 3058 for (i=old_disks-(backup_file?1:0); i<cnt; i++) {
353632d9 3059 struct mdinfo dinfo;
06b0d786 3060 int fd;
e9e43ec3 3061 int bsbsize;
ea0ebe96 3062 char *devname, namebuf[20];
10af14c4 3063 unsigned long long lo, hi;
353632d9
NB
3064
3065 /* This was a spare and may have some saved data on it.
3066 * Load the superblock, find and load the
3067 * backup_super_block.
3068 * If either fail, go on to next device.
3069 * If the backup contains no new info, just return
206c5eae 3070 * else restore data and update all superblocks
353632d9 3071 */
06b0d786
NB
3072 if (i == old_disks-1) {
3073 fd = open(backup_file, O_RDONLY);
e9e43ec3
N
3074 if (fd<0) {
3075 fprintf(stderr, Name ": backup file %s inaccessible: %s\n",
3076 backup_file, strerror(errno));
06b0d786 3077 continue;
e9e43ec3 3078 }
ea0ebe96 3079 devname = backup_file;
06b0d786
NB
3080 } else {
3081 fd = fdlist[i];
3082 if (fd < 0)
3083 continue;
3da92f27 3084 if (st->ss->load_super(st, fd, NULL))
06b0d786 3085 continue;
353632d9 3086
a5d85af7 3087 st->ss->getinfo_super(st, &dinfo, NULL);
3da92f27
NB
3088 st->ss->free_super(st);
3089
06b0d786
NB
3090 if (lseek64(fd,
3091 (dinfo.data_offset + dinfo.component_size - 8) <<9,
ea0ebe96
N
3092 0) < 0) {
3093 fprintf(stderr, Name ": Cannot seek on device %d\n", i);
06b0d786 3094 continue; /* Cannot seek */
ea0ebe96
N
3095 }
3096 sprintf(namebuf, "device-%d", i);
3097 devname = namebuf;
06b0d786 3098 }
ea0ebe96
N
3099 if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb)) {
3100 if (verbose)
3101 fprintf(stderr, Name ": Cannot read from %s\n", devname);
353632d9 3102 continue; /* Cannot read */
ea0ebe96 3103 }
e9e43ec3 3104 if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0 &&
ea0ebe96
N
3105 memcmp(bsb.magic, "md_backup_data-2", 16) != 0) {
3106 if (verbose)
3107 fprintf(stderr, Name ": No backup metadata on %s\n", devname);
353632d9 3108 continue;
ea0ebe96
N
3109 }
3110 if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb))) {
3111 if (verbose)
3112 fprintf(stderr, Name ": Bad backup-metadata checksum on %s\n", devname);
353632d9 3113 continue; /* bad checksum */
ea0ebe96 3114 }
e9e43ec3 3115 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0 &&
ea0ebe96
N
3116 bsb.sb_csum2 != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum2)-((char*)&bsb))) {
3117 if (verbose)
3118 fprintf(stderr, Name ": Bad backup-metadata checksum2 on %s\n", devname);
22e30516 3119 continue; /* Bad second checksum */
ea0ebe96
N
3120 }
3121 if (memcmp(bsb.set_uuid,info->uuid, 16) != 0) {
3122 if (verbose)
3123 fprintf(stderr, Name ": Wrong uuid on backup-metadata on %s\n", devname);
353632d9 3124 continue; /* Wrong uuid */
ea0ebe96 3125 }
353632d9 3126
097075b6
N
3127 /* array utime and backup-mtime should be updated at much the same time, but it seems that
3128 * sometimes they aren't... So allow considerable flexability in matching, and allow
3129 * this test to be overridden by an environment variable.
3130 */
f21e18ca
N
3131 if (info->array.utime > (int)__le64_to_cpu(bsb.mtime) + 2*60*60 ||
3132 info->array.utime < (int)__le64_to_cpu(bsb.mtime) - 10*60) {
097075b6
N
3133 if (check_env("MDADM_GROW_ALLOW_OLD")) {
3134 fprintf(stderr, Name ": accepting backup with timestamp %lu "
3135 "for array with timestamp %lu\n",
3136 (unsigned long)__le64_to_cpu(bsb.mtime),
3137 (unsigned long)info->array.utime);
3138 } else {
3139 if (verbose)
3140 fprintf(stderr, Name ": too-old timestamp on "
3141 "backup-metadata on %s\n", devname);
3142 continue; /* time stamp is too bad */
3143 }
ea0ebe96 3144 }
353632d9 3145
e9e43ec3 3146 if (bsb.magic[15] == '1') {
10af14c4
N
3147 if (bsb.length == 0)
3148 continue;
e7a71c6b
N
3149 if (info->delta_disks >= 0) {
3150 /* reshape_progress is increasing */
3151 if (__le64_to_cpu(bsb.arraystart)
3152 + __le64_to_cpu(bsb.length)
3153 < info->reshape_progress) {
3154 nonew:
3155 if (verbose)
3156 fprintf(stderr, Name
3157 ": backup-metadata found on %s but is not needed\n", devname);
3158 continue; /* No new data here */
3159 }
3160 } else {
3161 /* reshape_progress is decreasing */
3162 if (__le64_to_cpu(bsb.arraystart) >=
3163 info->reshape_progress)
3164 goto nonew; /* No new data here */
ea0ebe96 3165 }
e9e43ec3 3166 } else {
10af14c4
N
3167 if (bsb.length == 0 && bsb.length2 == 0)
3168 continue;
e7a71c6b
N
3169 if (info->delta_disks >= 0) {
3170 /* reshape_progress is increasing */
3171 if ((__le64_to_cpu(bsb.arraystart)
3172 + __le64_to_cpu(bsb.length)
3173 < info->reshape_progress)
3174 &&
3175 (__le64_to_cpu(bsb.arraystart2)
3176 + __le64_to_cpu(bsb.length2)
3177 < info->reshape_progress))
3178 goto nonew; /* No new data here */
3179 } else {
3180 /* reshape_progress is decreasing */
3181 if (__le64_to_cpu(bsb.arraystart) >=
3182 info->reshape_progress &&
3183 __le64_to_cpu(bsb.arraystart2) >=
3184 info->reshape_progress)
3185 goto nonew; /* No new data here */
3186 }
e9e43ec3 3187 }
ea0ebe96
N
3188 if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0) {
3189 second_fail:
3190 if (verbose)
e7a71c6b
N
3191 fprintf(stderr, Name
3192 ": Failed to verify secondary backup-metadata block on %s\n",
ea0ebe96 3193 devname);
353632d9 3194 continue; /* Cannot seek */
ea0ebe96 3195 }
2efedc7b 3196 /* There should be a duplicate backup superblock 4k before here */
06b0d786 3197 if (lseek64(fd, -4096, 1) < 0 ||
0155af90 3198 read(fd, &bsb2, sizeof(bsb2)) != sizeof(bsb2))
ea0ebe96 3199 goto second_fail; /* Cannot find leading superblock */
e9e43ec3
N
3200 if (bsb.magic[15] == '1')
3201 bsbsize = offsetof(struct mdp_backup_super, pad1);
3202 else
3203 bsbsize = offsetof(struct mdp_backup_super, pad);
ff94fb86 3204 if (memcmp(&bsb2, &bsb, bsbsize) != 0)
ea0ebe96 3205 goto second_fail; /* Cannot find leading superblock */
2efedc7b 3206
353632d9
NB
3207 /* Now need the data offsets for all devices. */
3208 offsets = malloc(sizeof(*offsets)*info->array.raid_disks);
3209 for(j=0; j<info->array.raid_disks; j++) {
3210 if (fdlist[j] < 0)
3211 continue;
3da92f27 3212 if (st->ss->load_super(st, fdlist[j], NULL))
353632d9
NB
3213 /* FIXME should be this be an error */
3214 continue;
a5d85af7 3215 st->ss->getinfo_super(st, &dinfo, NULL);
3da92f27 3216 st->ss->free_super(st);
14e5b4d7 3217 offsets[j] = dinfo.data_offset * 512;
353632d9
NB
3218 }
3219 printf(Name ": restoring critical section\n");
3220
3221 if (restore_stripes(fdlist, offsets,
3222 info->array.raid_disks,
3223 info->new_chunk,
3224 info->new_level,
3225 info->new_layout,
06b0d786 3226 fd, __le64_to_cpu(bsb.devstart)*512,
92dcdf7c 3227 __le64_to_cpu(bsb.arraystart)*512,
e9e43ec3
N
3228 __le64_to_cpu(bsb.length)*512)) {
3229 /* didn't succeed, so giveup */
ea0ebe96
N
3230 if (verbose)
3231 fprintf(stderr, Name ": Error restoring backup from %s\n",
3232 devname);
e9e43ec3
N
3233 return 1;
3234 }
3235
3236 if (bsb.magic[15] == '2' &&
3237 restore_stripes(fdlist, offsets,
3238 info->array.raid_disks,
3239 info->new_chunk,
3240 info->new_level,
3241 info->new_layout,
3242 fd, __le64_to_cpu(bsb.devstart)*512 +
3243 __le64_to_cpu(bsb.devstart2)*512,
92dcdf7c 3244 __le64_to_cpu(bsb.arraystart2)*512,
e9e43ec3 3245 __le64_to_cpu(bsb.length2)*512)) {
353632d9 3246 /* didn't succeed, so giveup */
ea0ebe96
N
3247 if (verbose)
3248 fprintf(stderr, Name ": Error restoring second backup from %s\n",
3249 devname);
2295250a 3250 return 1;
353632d9
NB
3251 }
3252
e9e43ec3 3253
353632d9
NB
3254 /* Ok, so the data is restored. Let's update those superblocks. */
3255
10af14c4
N
3256 lo = hi = 0;
3257 if (bsb.length) {
3258 lo = __le64_to_cpu(bsb.arraystart);
3259 hi = lo + __le64_to_cpu(bsb.length);
3260 }
3261 if (bsb.magic[15] == '2' && bsb.length2) {
3262 unsigned long long lo1, hi1;
3263 lo1 = __le64_to_cpu(bsb.arraystart2);
3264 hi1 = lo1 + __le64_to_cpu(bsb.length2);
3265 if (lo == hi) {
3266 lo = lo1;
3267 hi = hi1;
3268 } else if (lo < lo1)
3269 hi = hi1;
3270 else
3271 lo = lo1;
3272 }
3273 if (lo < hi &&
3274 (info->reshape_progress < lo ||
3275 info->reshape_progress > hi))
3276 /* backup does not affect reshape_progress*/ ;
3277 else if (info->delta_disks >= 0) {
e9e43ec3
N
3278 info->reshape_progress = __le64_to_cpu(bsb.arraystart) +
3279 __le64_to_cpu(bsb.length);
3280 if (bsb.magic[15] == '2') {
3281 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2) +
3282 __le64_to_cpu(bsb.length2);
3283 if (p2 > info->reshape_progress)
3284 info->reshape_progress = p2;
3285 }
3286 } else {
3287 info->reshape_progress = __le64_to_cpu(bsb.arraystart);
3288 if (bsb.magic[15] == '2') {
3289 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2);
3290 if (p2 < info->reshape_progress)
3291 info->reshape_progress = p2;
3292 }
3293 }
353632d9
NB
3294 for (j=0; j<info->array.raid_disks; j++) {
3295 if (fdlist[j] < 0) continue;
3da92f27 3296 if (st->ss->load_super(st, fdlist[j], NULL))
353632d9 3297 continue;
a5d85af7 3298 st->ss->getinfo_super(st, &dinfo, NULL);
e9e43ec3 3299 dinfo.reshape_progress = info->reshape_progress;
3da92f27 3300 st->ss->update_super(st, &dinfo,
68c7d6d7
NB
3301 "_reshape_progress",
3302 NULL,0, 0, NULL);
3da92f27
NB
3303 st->ss->store_super(st, fdlist[j]);
3304 st->ss->free_super(st);
353632d9 3305 }
353632d9
NB
3306 return 0;
3307 }
6e9eac4f
NB
3308 /* Didn't find any backup data, try to see if any
3309 * was needed.
3310 */
82f2d6ab
N
3311 if (info->delta_disks < 0) {
3312 /* When shrinking, the critical section is at the end.
3313 * So see if we are before the critical section.
3314 */
3315 unsigned long long first_block;
3316 nstripe = ostripe = 0;
3317 first_block = 0;
3318 while (ostripe >= nstripe) {
3319 ostripe += info->array.chunk_size / 512;
3320 first_block = ostripe * odata;
3321 nstripe = first_block / ndata / (info->new_chunk/512) *
3322 (info->new_chunk/512);
3323 }
3324
3325 if (info->reshape_progress >= first_block)
3326 return 0;
6e9eac4f 3327 }
82f2d6ab
N
3328 if (info->delta_disks > 0) {
3329 /* See if we are beyond the critical section. */
3330 unsigned long long last_block;
3331 nstripe = ostripe = 0;
3332 last_block = 0;
3333 while (nstripe >= ostripe) {
3334 nstripe += info->new_chunk / 512;
3335 last_block = nstripe * ndata;
3336 ostripe = last_block / odata / (info->array.chunk_size/512) *
3337 (info->array.chunk_size/512);
3338 }
6e9eac4f 3339
82f2d6ab
N
3340 if (info->reshape_progress >= last_block)
3341 return 0;
3342 }
6e9eac4f 3343 /* needed to recover critical section! */
ea0ebe96
N
3344 if (verbose)
3345 fprintf(stderr, Name ": Failed to find backup of critical section\n");
2295250a 3346 return 1;
353632d9 3347}
e9e43ec3
N
3348
3349int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
3350 char *backup_file)
3351{
864a004f
AK
3352 char buf[40];
3353 char *container = NULL;
ef5414b2 3354 int err;
864a004f 3355
20a40eca
N
3356 err = sysfs_set_str(info, NULL, "array_state", "readonly");
3357 if (err)
3358 return err;
3359 if (st->ss->external) {
864a004f
AK
3360 fmt_devname(buf, st->container_dev);
3361 container = buf;
493f5dd6
N
3362 freeze(st);
3363 if (info->reshape_active == 2)
3364 return reshape_container(container, NULL,
3365 st, info, 0, backup_file,
3366 0, 1);
864a004f
AK
3367 }
3368 return reshape_array(container, mdfd, "array", st, info, 1,
3369 backup_file, 0, 0, 1);
e9e43ec3 3370}