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