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