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