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