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