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