]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Grow.c
Reshape: use systemd to continue containers as well as native arrays.
[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 continue_via_systemd(char *devnm)
2757 {
2758 int skipped, i, pid, status;
2759 char pathbuf[1024];
2760 /* In a systemd/udev world, it is best to get systemd to
2761 * run "mdadm --grow --continue" rather than running in the
2762 * background.
2763 */
2764 switch(fork()) {
2765 case 0:
2766 /* FIXME yuk. CLOSE_EXEC?? */
2767 skipped = 0;
2768 for (i = 3; skipped < 20; i++)
2769 if (close(i) < 0)
2770 skipped++;
2771 else
2772 skipped = 0;
2773
2774 /* Don't want to see error messages from
2775 * systemctl. If the service doesn't exist,
2776 * we fork ourselves.
2777 */
2778 close(2);
2779 open("/dev/null", O_WRONLY);
2780 snprintf(pathbuf, sizeof(pathbuf), "mdadm-grow-continue@%s.service",
2781 devnm);
2782 status = execl("/usr/bin/systemctl", "systemctl",
2783 "start",
2784 pathbuf, NULL);
2785 status = execl("/bin/systemctl", "systemctl", "start",
2786 pathbuf, NULL);
2787 exit(1);
2788 case -1: /* Just do it ourselves. */
2789 break;
2790 default: /* parent - good */
2791 pid = wait(&status);
2792 if (pid >= 0 && status == 0)
2793 return 1;
2794 }
2795 return 0;
2796 }
2797
2798 static int reshape_array(char *container, int fd, char *devname,
2799 struct supertype *st, struct mdinfo *info,
2800 int force, struct mddev_dev *devlist,
2801 unsigned long long data_offset,
2802 char *backup_file, int verbose, int forked,
2803 int restart, int freeze_reshape)
2804 {
2805 struct reshape reshape;
2806 int spares_needed;
2807 char *msg;
2808 int orig_level = UnSet;
2809 int odisks;
2810 int delayed;
2811
2812 struct mdu_array_info_s array;
2813 char *c;
2814
2815 struct mddev_dev *dv;
2816 int added_disks;
2817
2818 int *fdlist = NULL;
2819 unsigned long long *offsets = NULL;
2820 int d;
2821 int nrdisks;
2822 int err;
2823 unsigned long blocks;
2824 unsigned long long array_size;
2825 int done;
2826 struct mdinfo *sra = NULL;
2827
2828 /* when reshaping a RAID0, the component_size might be zero.
2829 * So try to fix that up.
2830 */
2831 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
2832 dprintf("Cannot get array information.\n");
2833 goto release;
2834 }
2835 if (array.level == 0 && info->component_size == 0) {
2836 get_dev_size(fd, NULL, &array_size);
2837 info->component_size = array_size / array.raid_disks;
2838 }
2839
2840 if (array.level == 10)
2841 /* Need space_after info */
2842 get_space_after(fd, st, info);
2843
2844 if (info->reshape_active) {
2845 int new_level = info->new_level;
2846 info->new_level = UnSet;
2847 if (info->delta_disks > 0)
2848 info->array.raid_disks -= info->delta_disks;
2849 msg = analyse_change(devname, info, &reshape);
2850 info->new_level = new_level;
2851 if (info->delta_disks > 0)
2852 info->array.raid_disks += info->delta_disks;
2853 if (!restart)
2854 /* Make sure the array isn't read-only */
2855 ioctl(fd, RESTART_ARRAY_RW, 0);
2856 } else
2857 msg = analyse_change(devname, info, &reshape);
2858 if (msg) {
2859 /* if msg == "", error has already been printed */
2860 if (msg[0])
2861 pr_err("%s\n", msg);
2862 goto release;
2863 }
2864 if (restart &&
2865 (reshape.level != info->array.level ||
2866 reshape.before.layout != info->array.layout ||
2867 reshape.before.data_disks + reshape.parity
2868 != info->array.raid_disks - max(0, info->delta_disks))) {
2869 pr_err("reshape info is not in native format -"
2870 " cannot continue.\n");
2871 goto release;
2872 }
2873
2874 if (st->ss->external && restart && (info->reshape_progress == 0)) {
2875 /* When reshape is restarted from '0', very begin of array
2876 * it is possible that for external metadata reshape and array
2877 * configuration doesn't happen.
2878 * Check if md has the same opinion, and reshape is restarted
2879 * from 0. If so, this is regular reshape start after reshape
2880 * switch in metadata to next array only.
2881 */
2882 if ((verify_reshape_position(info, reshape.level) >= 0) &&
2883 (info->reshape_progress == 0))
2884 restart = 0;
2885 }
2886 if (restart) {
2887 /* reshape already started. just skip to monitoring the reshape */
2888 if (reshape.backup_blocks == 0)
2889 return 0;
2890 if (restart & RESHAPE_NO_BACKUP)
2891 return 0;
2892
2893 /* Need 'sra' down at 'started:' */
2894 sra = sysfs_read(fd, NULL,
2895 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK|
2896 GET_CACHE);
2897 if (!sra) {
2898 pr_err("%s: Cannot get array details from sysfs\n",
2899 devname);
2900 goto release;
2901 }
2902
2903 if (!backup_file)
2904 backup_file = locate_backup(sra->sys_name);
2905
2906 goto started;
2907 }
2908 /* The container is frozen but the array may not be.
2909 * So freeze the array so spares don't get put to the wrong use
2910 * FIXME there should probably be a cleaner separation between
2911 * freeze_array and freeze_container.
2912 */
2913 sysfs_freeze_array(info);
2914 /* Check we have enough spares to not be degraded */
2915 added_disks = 0;
2916 for (dv = devlist; dv ; dv=dv->next)
2917 added_disks++;
2918 spares_needed = max(reshape.before.data_disks,
2919 reshape.after.data_disks)
2920 + reshape.parity - array.raid_disks;
2921
2922 if (!force &&
2923 info->new_level > 1 && info->array.level > 1 &&
2924 spares_needed > info->array.spare_disks + added_disks) {
2925 pr_err("Need %d spare%s to avoid degraded array,"
2926 " and only have %d.\n"
2927 " Use --force to over-ride this check.\n",
2928 spares_needed,
2929 spares_needed == 1 ? "" : "s",
2930 info->array.spare_disks + added_disks);
2931 goto release;
2932 }
2933 /* Check we have enough spares to not fail */
2934 spares_needed = max(reshape.before.data_disks,
2935 reshape.after.data_disks)
2936 - array.raid_disks;
2937 if ((info->new_level > 1 || info->new_level == 0) &&
2938 spares_needed > info->array.spare_disks +added_disks) {
2939 pr_err("Need %d spare%s to create working array,"
2940 " and only have %d.\n",
2941 spares_needed,
2942 spares_needed == 1 ? "" : "s",
2943 info->array.spare_disks + added_disks);
2944 goto release;
2945 }
2946
2947 if (reshape.level != array.level) {
2948 int err = impose_level(fd, reshape.level, devname, verbose);
2949 if (err)
2950 goto release;
2951 info->new_layout = UnSet; /* after level change,
2952 * layout is meaningless */
2953 orig_level = array.level;
2954 sysfs_freeze_array(info);
2955
2956 if (reshape.level > 0 && st->ss->external) {
2957 /* make sure mdmon is aware of the new level */
2958 if (mdmon_running(container))
2959 flush_mdmon(container);
2960
2961 if (!mdmon_running(container))
2962 start_mdmon(container);
2963 ping_monitor(container);
2964 if (mdmon_running(container) &&
2965 st->update_tail == NULL)
2966 st->update_tail = &st->updates;
2967 }
2968 }
2969 /* ->reshape_super might have chosen some spares from the
2970 * container that it wants to be part of the new array.
2971 * We can collect them with ->container_content and give
2972 * them to the kernel.
2973 */
2974 if (st->ss->reshape_super && st->ss->container_content) {
2975 char *subarray = strchr(info->text_version+1, '/')+1;
2976 struct mdinfo *info2 =
2977 st->ss->container_content(st, subarray);
2978 struct mdinfo *d;
2979
2980 if (info2) {
2981 sysfs_init(info2, fd, st->devnm);
2982 /* When increasing number of devices, we need to set
2983 * new raid_disks before adding these, or they might
2984 * be rejected.
2985 */
2986 if (reshape.backup_blocks &&
2987 reshape.after.data_disks > reshape.before.data_disks)
2988 subarray_set_num(container, info2, "raid_disks",
2989 reshape.after.data_disks +
2990 reshape.parity);
2991 for (d = info2->devs; d; d = d->next) {
2992 if (d->disk.state == 0 &&
2993 d->disk.raid_disk >= 0) {
2994 /* This is a spare that wants to
2995 * be part of the array.
2996 */
2997 add_disk(fd, st, info2, d);
2998 }
2999 }
3000 sysfs_free(info2);
3001 }
3002 }
3003 /* We might have been given some devices to add to the
3004 * array. Now that the array has been changed to the right
3005 * level and frozen, we can safely add them.
3006 */
3007 if (devlist)
3008 Manage_subdevs(devname, fd, devlist, verbose,
3009 0,NULL, 0);
3010
3011 if (reshape.backup_blocks == 0 && data_offset != INVALID_SECTORS)
3012 reshape.backup_blocks = reshape.before.data_disks * info->array.chunk_size/512;
3013 if (reshape.backup_blocks == 0) {
3014 /* No restriping needed, but we might need to impose
3015 * some more changes: layout, raid_disks, chunk_size
3016 */
3017 /* read current array info */
3018 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
3019 dprintf("Cannot get array information.\n");
3020 goto release;
3021 }
3022 /* compare current array info with new values and if
3023 * it is different update them to new */
3024 if (info->new_layout != UnSet &&
3025 info->new_layout != array.layout) {
3026 array.layout = info->new_layout;
3027 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
3028 pr_err("failed to set new layout\n");
3029 goto release;
3030 } else if (verbose >= 0)
3031 printf("layout for %s set to %d\n",
3032 devname, array.layout);
3033 }
3034 if (info->delta_disks != UnSet &&
3035 info->delta_disks != 0 &&
3036 array.raid_disks != (info->array.raid_disks + info->delta_disks)) {
3037 array.raid_disks += info->delta_disks;
3038 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
3039 pr_err("failed to set raid disks\n");
3040 goto release;
3041 } else if (verbose >= 0) {
3042 printf("raid_disks for %s set to %d\n",
3043 devname, array.raid_disks);
3044 }
3045 }
3046 if (info->new_chunk != 0 &&
3047 info->new_chunk != array.chunk_size) {
3048 if (sysfs_set_num(info, NULL,
3049 "chunk_size", info->new_chunk) != 0) {
3050 pr_err("failed to set chunk size\n");
3051 goto release;
3052 } else if (verbose >= 0)
3053 printf("chunk size for %s set to %d\n",
3054 devname, array.chunk_size);
3055 }
3056 unfreeze(st);
3057 return 0;
3058 }
3059
3060 /*
3061 * There are three possibilities.
3062 * 1/ The array will shrink.
3063 * We need to ensure the reshape will pause before reaching
3064 * the 'critical section'. We also need to fork and wait for
3065 * that to happen. When it does we
3066 * suspend/backup/complete/unfreeze
3067 *
3068 * 2/ The array will not change size.
3069 * This requires that we keep a backup of a sliding window
3070 * so that we can restore data after a crash. So we need
3071 * to fork and monitor progress.
3072 * In future we will allow the data_offset to change, so
3073 * a sliding backup becomes unnecessary.
3074 *
3075 * 3/ The array will grow. This is relatively easy.
3076 * However the kernel's restripe routines will cheerfully
3077 * overwrite some early data before it is safe. So we
3078 * need to make a backup of the early parts of the array
3079 * and be ready to restore it if rebuild aborts very early.
3080 * For externally managed metadata, we still need a forked
3081 * child to monitor the reshape and suspend IO over the region
3082 * that is being reshaped.
3083 *
3084 * We backup data by writing it to one spare, or to a
3085 * file which was given on command line.
3086 *
3087 * In each case, we first make sure that storage is available
3088 * for the required backup.
3089 * Then we:
3090 * - request the shape change.
3091 * - fork to handle backup etc.
3092 */
3093 /* Check that we can hold all the data */
3094 get_dev_size(fd, NULL, &array_size);
3095 if (reshape.new_size < (array_size/512)) {
3096 pr_err("this change will reduce the size of the array.\n"
3097 " use --grow --array-size first to truncate array.\n"
3098 " e.g. mdadm --grow %s --array-size %llu\n",
3099 devname, reshape.new_size/2);
3100 goto release;
3101 }
3102
3103 if (array.level == 10) {
3104 /* Reshaping RAID10 does not require any data backup by
3105 * user-space. Instead it requires that the data_offset
3106 * is changed to avoid the need for backup.
3107 * So this is handled very separately
3108 */
3109 if (restart)
3110 /* Nothing to do. */
3111 return 0;
3112 return raid10_reshape(container, fd, devname, st, info,
3113 &reshape, data_offset,
3114 force, verbose);
3115 }
3116 sra = sysfs_read(fd, NULL,
3117 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK|
3118 GET_CACHE);
3119 if (!sra) {
3120 pr_err("%s: Cannot get array details from sysfs\n",
3121 devname);
3122 goto release;
3123 }
3124
3125 if (!backup_file)
3126 switch(set_new_data_offset(sra, st, devname,
3127 reshape.after.data_disks - reshape.before.data_disks,
3128 data_offset,
3129 reshape.min_offset_change, 1)) {
3130 case -1:
3131 goto release;
3132 case 0:
3133 /* Updated data_offset, so it's easy now */
3134 update_cache_size(container, sra, info,
3135 min(reshape.before.data_disks,
3136 reshape.after.data_disks),
3137 reshape.backup_blocks);
3138
3139 /* Right, everything seems fine. Let's kick things off.
3140 */
3141 sync_metadata(st);
3142
3143 if (impose_reshape(sra, info, st, fd, restart,
3144 devname, container, &reshape) < 0)
3145 goto release;
3146 if (sysfs_set_str(sra, NULL, "sync_action", "reshape") < 0) {
3147 pr_err("Failed to initiate reshape!\n");
3148 goto release;
3149 }
3150 if (info->new_level == reshape.level)
3151 return 0;
3152 /* need to adjust level when reshape completes */
3153 switch(fork()) {
3154 case -1: /* ignore error, but don't wait */
3155 return 0;
3156 default: /* parent */
3157 return 0;
3158 case 0:
3159 map_fork();
3160 break;
3161 }
3162 close(fd);
3163 wait_reshape(sra);
3164 fd = open_dev(sra->sys_name);
3165 if (fd >= 0)
3166 impose_level(fd, info->new_level, devname, verbose);
3167 return 0;
3168 case 1: /* Couldn't set data_offset, try the old way */
3169 if (data_offset != INVALID_SECTORS) {
3170 pr_err("Cannot update data_offset on this array\n");
3171 goto release;
3172 }
3173 break;
3174 }
3175
3176 started:
3177 /* Decide how many blocks (sectors) for a reshape
3178 * unit. The number we have so far is just a minimum
3179 */
3180 blocks = reshape.backup_blocks;
3181 if (reshape.before.data_disks ==
3182 reshape.after.data_disks) {
3183 /* Make 'blocks' bigger for better throughput, but
3184 * not so big that we reject it below.
3185 * Try for 16 megabytes
3186 */
3187 while (blocks * 32 < sra->component_size &&
3188 blocks < 16*1024*2)
3189 blocks *= 2;
3190 } else
3191 pr_err("Need to backup %luK of critical "
3192 "section..\n", blocks/2);
3193
3194 if (blocks >= sra->component_size/2) {
3195 pr_err("%s: Something wrong"
3196 " - reshape aborted\n",
3197 devname);
3198 goto release;
3199 }
3200
3201 /* Now we need to open all these devices so we can read/write.
3202 */
3203 nrdisks = max(reshape.before.data_disks,
3204 reshape.after.data_disks) + reshape.parity
3205 + sra->array.spare_disks;
3206 fdlist = xcalloc((1+nrdisks), sizeof(int));
3207 offsets = xcalloc((1+nrdisks), sizeof(offsets[0]));
3208
3209 odisks = reshape.before.data_disks + reshape.parity;
3210 d = reshape_prepare_fdlist(devname, sra, odisks,
3211 nrdisks, blocks, backup_file,
3212 fdlist, offsets);
3213 if (d < 0) {
3214 goto release;
3215 }
3216 if ((st->ss->manage_reshape == NULL) ||
3217 (st->ss->recover_backup == NULL)) {
3218 if (backup_file == NULL) {
3219 if (reshape.after.data_disks <=
3220 reshape.before.data_disks) {
3221 pr_err("%s: Cannot grow - need backup-file\n",
3222 devname);
3223 pr_err(" Please provide one with \"--backup=...\"\n");
3224 goto release;
3225 } else if (sra->array.spare_disks == 0) {
3226 pr_err("%s: Cannot grow - "
3227 "need a spare or backup-file to backup "
3228 "critical section\n", devname);
3229 goto release;
3230 }
3231 } else {
3232 if (!reshape_open_backup_file(backup_file, fd, devname,
3233 (signed)blocks,
3234 fdlist+d, offsets+d,
3235 sra->sys_name,
3236 restart)) {
3237 goto release;
3238 }
3239 d++;
3240 }
3241 }
3242
3243 update_cache_size(container, sra, info,
3244 min(reshape.before.data_disks, reshape.after.data_disks),
3245 blocks);
3246
3247 /* Right, everything seems fine. Let's kick things off.
3248 * If only changing raid_disks, use ioctl, else use
3249 * sysfs.
3250 */
3251 sync_metadata(st);
3252
3253 if (impose_reshape(sra, info, st, fd, restart,
3254 devname, container, &reshape) < 0)
3255 goto release;
3256
3257 err = start_reshape(sra, restart, reshape.before.data_disks,
3258 reshape.after.data_disks);
3259 if (err) {
3260 pr_err("Cannot %s reshape for %s\n",
3261 restart ? "continue" : "start",
3262 devname);
3263 goto release;
3264 }
3265 if (restart)
3266 sysfs_set_str(sra, NULL, "array_state", "active");
3267 if (freeze_reshape) {
3268 free(fdlist);
3269 free(offsets);
3270 sysfs_free(sra);
3271 pr_err("Reshape has to be continued from"
3272 " location %llu when root filesystem has been mounted.\n",
3273 sra->reshape_progress);
3274 return 1;
3275 }
3276
3277 if (!forked && !check_env("MDADM_NO_SYSTEMCTL"))
3278 if (continue_via_systemd(container ?: sra->sys_name)) {
3279 free(fdlist);
3280 free(offsets);
3281 sysfs_free(sra);
3282 return 0;
3283 }
3284
3285 /* Now we just need to kick off the reshape and watch, while
3286 * handling backups of the data...
3287 * This is all done by a forked background process.
3288 */
3289 switch(forked ? 0 : fork()) {
3290 case -1:
3291 pr_err("Cannot run child to monitor reshape: %s\n",
3292 strerror(errno));
3293 abort_reshape(sra);
3294 goto release;
3295 default:
3296 free(fdlist);
3297 free(offsets);
3298 sysfs_free(sra);
3299 return 0;
3300 case 0:
3301 map_fork();
3302 break;
3303 }
3304
3305 /* If another array on the same devices is busy, the
3306 * reshape will wait for them. This would mean that
3307 * the first section that we suspend will stay suspended
3308 * for a long time. So check on that possibility
3309 * by looking for "DELAYED" in /proc/mdstat, and if found,
3310 * wait a while
3311 */
3312 do {
3313 struct mdstat_ent *mds, *m;
3314 delayed = 0;
3315 mds = mdstat_read(1, 0);
3316 for (m = mds; m; m = m->next)
3317 if (strcmp(m->devnm, sra->sys_name) == 0) {
3318 if (m->resync &&
3319 m->percent == RESYNC_DELAYED)
3320 delayed = 1;
3321 if (m->resync == 0)
3322 /* Haven't started the reshape thread
3323 * yet, wait a bit
3324 */
3325 delayed = 2;
3326 break;
3327 }
3328 free_mdstat(mds);
3329 if (delayed == 1 && get_linux_version() < 3007000) {
3330 pr_err("Reshape is delayed, but cannot wait carefully with this kernel.\n"
3331 " You might experience problems until other reshapes complete.\n");
3332 delayed = 0;
3333 }
3334 if (delayed)
3335 mdstat_wait(30 - (delayed-1) * 25);
3336 } while (delayed);
3337 mdstat_close();
3338 close(fd);
3339 if (check_env("MDADM_GROW_VERIFY"))
3340 fd = open(devname, O_RDONLY | O_DIRECT);
3341 else
3342 fd = -1;
3343 mlockall(MCL_FUTURE);
3344
3345 signal(SIGTERM, catch_term);
3346
3347 if (st->ss->external) {
3348 /* metadata handler takes it from here */
3349 done = st->ss->manage_reshape(
3350 fd, sra, &reshape, st, blocks,
3351 fdlist, offsets,
3352 d - odisks, fdlist+odisks,
3353 offsets+odisks);
3354 } else
3355 done = child_monitor(
3356 fd, sra, &reshape, st, blocks,
3357 fdlist, offsets,
3358 d - odisks, fdlist+odisks,
3359 offsets+odisks);
3360
3361 free(fdlist);
3362 free(offsets);
3363
3364 if (backup_file && done) {
3365 char *bul;
3366 bul = make_backup(sra->sys_name);
3367 if (bul) {
3368 char buf[1024];
3369 int l = readlink(bul, buf, sizeof(buf));
3370 if (l > 0) {
3371 buf[l]=0;
3372 unlink(buf);
3373 }
3374 unlink(bul);
3375 free(bul);
3376 }
3377 unlink(backup_file);
3378 }
3379 if (!done) {
3380 abort_reshape(sra);
3381 goto out;
3382 }
3383
3384 if (!st->ss->external &&
3385 !(reshape.before.data_disks != reshape.after.data_disks
3386 && info->custom_array_size) &&
3387 info->new_level == reshape.level &&
3388 !forked) {
3389 /* no need to wait for the reshape to finish as
3390 * there is nothing more to do.
3391 */
3392 sysfs_free(sra);
3393 exit(0);
3394 }
3395 wait_reshape(sra);
3396
3397 if (st->ss->external) {
3398 /* Re-load the metadata as much could have changed */
3399 int cfd = open_dev(st->container_devnm);
3400 if (cfd >= 0) {
3401 flush_mdmon(container);
3402 st->ss->free_super(st);
3403 st->ss->load_container(st, cfd, container);
3404 close(cfd);
3405 }
3406 }
3407
3408 /* set new array size if required customer_array_size is used
3409 * by this metadata.
3410 */
3411 if (reshape.before.data_disks !=
3412 reshape.after.data_disks &&
3413 info->custom_array_size)
3414 set_array_size(st, info, info->text_version);
3415
3416 if (info->new_level != reshape.level) {
3417 if (fd < 0)
3418 fd = open(devname, O_RDONLY);
3419 impose_level(fd, info->new_level, devname, verbose);
3420 close(fd);
3421 if (info->new_level == 0)
3422 st->update_tail = NULL;
3423 }
3424 out:
3425 sysfs_free(sra);
3426 if (forked)
3427 return 0;
3428 unfreeze(st);
3429 exit(0);
3430
3431 release:
3432 free(fdlist);
3433 free(offsets);
3434 if (orig_level != UnSet && sra) {
3435 c = map_num(pers, orig_level);
3436 if (c && sysfs_set_str(sra, NULL, "level", c) == 0)
3437 pr_err("aborting level change\n");
3438 }
3439 sysfs_free(sra);
3440 if (!forked)
3441 unfreeze(st);
3442 return 1;
3443 }
3444
3445 /* mdfd handle is passed to be closed in child process (after fork).
3446 */
3447 int reshape_container(char *container, char *devname,
3448 int mdfd,
3449 struct supertype *st,
3450 struct mdinfo *info,
3451 int force,
3452 char *backup_file, int verbose,
3453 int forked, int restart, int freeze_reshape)
3454 {
3455 struct mdinfo *cc = NULL;
3456 int rv = restart;
3457 char last_devnm[32] = "";
3458
3459 /* component_size is not meaningful for a container,
3460 * so pass '0' meaning 'no change'
3461 */
3462 if (!restart &&
3463 reshape_super(st, 0, info->new_level,
3464 info->new_layout, info->new_chunk,
3465 info->array.raid_disks, info->delta_disks,
3466 backup_file, devname, APPLY_METADATA_CHANGES,
3467 verbose)) {
3468 unfreeze(st);
3469 return 1;
3470 }
3471
3472 sync_metadata(st);
3473
3474 /* ping monitor to be sure that update is on disk
3475 */
3476 ping_monitor(container);
3477
3478 if (!forked && !check_env("MDADM_NO_SYSTEMCTL"))
3479 if (continue_via_systemd(container))
3480 return 0;
3481
3482 switch (forked ? 0 : fork()) {
3483 case -1: /* error */
3484 perror("Cannot fork to complete reshape\n");
3485 unfreeze(st);
3486 return 1;
3487 default: /* parent */
3488 if (!freeze_reshape)
3489 printf(Name ": multi-array reshape continues"
3490 " in background\n");
3491 return 0;
3492 case 0: /* child */
3493 map_fork();
3494 break;
3495 }
3496
3497 /* close unused handle in child process
3498 */
3499 if (mdfd > -1)
3500 close(mdfd);
3501
3502 while(1) {
3503 /* For each member array with reshape_active,
3504 * we need to perform the reshape.
3505 * We pick the first array that needs reshaping and
3506 * reshape it. reshape_array() will re-read the metadata
3507 * so the next time through a different array should be
3508 * ready for reshape.
3509 * It is possible that the 'different' array will not
3510 * be assembled yet. In that case we simple exit.
3511 * When it is assembled, the mdadm which assembles it
3512 * will take over the reshape.
3513 */
3514 struct mdinfo *content;
3515 int fd;
3516 struct mdstat_ent *mdstat;
3517 char *adev;
3518 int devid;
3519
3520 sysfs_free(cc);
3521
3522 cc = st->ss->container_content(st, NULL);
3523
3524 for (content = cc; content ; content = content->next) {
3525 char *subarray;
3526 if (!content->reshape_active)
3527 continue;
3528
3529 subarray = strchr(content->text_version+1, '/')+1;
3530 mdstat = mdstat_by_subdev(subarray, container);
3531 if (!mdstat)
3532 continue;
3533 if (mdstat->active == 0) {
3534 pr_err("Skipping inactive array %s.\n",
3535 mdstat->devnm);
3536 free_mdstat(mdstat);
3537 mdstat = NULL;
3538 continue;
3539 }
3540 break;
3541 }
3542 if (!content)
3543 break;
3544
3545 devid = devnm2devid(mdstat->devnm);
3546 adev = map_dev(major(devid), minor(devid), 0);
3547 if (!adev)
3548 adev = content->text_version;
3549
3550 fd = open_dev(mdstat->devnm);
3551 if (fd < 0) {
3552 printf(Name ": Device %s cannot be opened for reshape.",
3553 adev);
3554 break;
3555 }
3556
3557 if (strcmp(last_devnm, mdstat->devnm) == 0) {
3558 /* Do not allow for multiple reshape_array() calls for
3559 * the same array.
3560 * It can happen when reshape_array() returns without
3561 * error, when reshape is not finished (wrong reshape
3562 * starting/continuation conditions). Mdmon doesn't
3563 * switch to next array in container and reentry
3564 * conditions for the same array occur.
3565 * This is possibly interim until the behaviour of
3566 * reshape_array is resolved().
3567 */
3568 printf(Name ": Multiple reshape execution detected for "
3569 "device %s.", adev);
3570 close(fd);
3571 break;
3572 }
3573 strcpy(last_devnm, mdstat->devnm);
3574
3575 sysfs_init(content, fd, mdstat->devnm);
3576
3577 if (mdmon_running(container))
3578 flush_mdmon(container);
3579
3580 rv = reshape_array(container, fd, adev, st,
3581 content, force, NULL, INVALID_SECTORS,
3582 backup_file, verbose, 1, restart,
3583 freeze_reshape);
3584 close(fd);
3585
3586 if (freeze_reshape) {
3587 sysfs_free(cc);
3588 exit(0);
3589 }
3590
3591 restart = 0;
3592 if (rv)
3593 break;
3594
3595 if (mdmon_running(container))
3596 flush_mdmon(container);
3597 }
3598 if (!rv)
3599 unfreeze(st);
3600 sysfs_free(cc);
3601 exit(0);
3602 }
3603
3604 /*
3605 * We run a child process in the background which performs the following
3606 * steps:
3607 * - wait for resync to reach a certain point
3608 * - suspend io to the following section
3609 * - backup that section
3610 * - allow resync to proceed further
3611 * - resume io
3612 * - discard the backup.
3613 *
3614 * When are combined in slightly different ways in the three cases.
3615 * Grow:
3616 * - suspend/backup/allow/wait/resume/discard
3617 * Shrink:
3618 * - allow/wait/suspend/backup/allow/wait/resume/discard
3619 * same-size:
3620 * - wait/resume/discard/suspend/backup/allow
3621 *
3622 * suspend/backup/allow always come together
3623 * wait/resume/discard do too.
3624 * For the same-size case we have two backups to improve flow.
3625 *
3626 */
3627
3628 int progress_reshape(struct mdinfo *info, struct reshape *reshape,
3629 unsigned long long backup_point,
3630 unsigned long long wait_point,
3631 unsigned long long *suspend_point,
3632 unsigned long long *reshape_completed, int *frozen)
3633 {
3634 /* This function is called repeatedly by the reshape manager.
3635 * It determines how much progress can safely be made and allows
3636 * that progress.
3637 * - 'info' identifies the array and particularly records in
3638 * ->reshape_progress the metadata's knowledge of progress
3639 * This is a sector offset from the start of the array
3640 * of the next array block to be relocated. This number
3641 * may increase from 0 or decrease from array_size, depending
3642 * on the type of reshape that is happening.
3643 * Note that in contrast, 'sync_completed' is a block count of the
3644 * reshape so far. It gives the distance between the start point
3645 * (head or tail of device) and the next place that data will be
3646 * written. It always increases.
3647 * - 'reshape' is the structure created by analyse_change
3648 * - 'backup_point' shows how much the metadata manager has backed-up
3649 * data. For reshapes with increasing progress, it is the next address
3650 * to be backed up, previous addresses have been backed-up. For
3651 * decreasing progress, it is the earliest address that has been
3652 * backed up - later address are also backed up.
3653 * So addresses between reshape_progress and backup_point are
3654 * backed up providing those are in the 'correct' order.
3655 * - 'wait_point' is an array address. When reshape_completed
3656 * passes this point, progress_reshape should return. It might
3657 * return earlier if it determines that ->reshape_progress needs
3658 * to be updated or further backup is needed.
3659 * - suspend_point is maintained by progress_reshape and the caller
3660 * should not touch it except to initialise to zero.
3661 * It is an array address and it only increases in 2.6.37 and earlier.
3662 * This makes it difficult to handle reducing reshapes with
3663 * external metadata.
3664 * However: it is similar to backup_point in that it records the
3665 * other end of a suspended region from reshape_progress.
3666 * it is moved to extend the region that is safe to backup and/or
3667 * reshape
3668 * - reshape_completed is read from sysfs and returned. The caller
3669 * should copy this into ->reshape_progress when it has reason to
3670 * believe that the metadata knows this, and any backup outside this
3671 * has been erased.
3672 *
3673 * Return value is:
3674 * 1 if more data from backup_point - but only as far as suspend_point,
3675 * should be backed up
3676 * 0 if things are progressing smoothly
3677 * -1 if the reshape is finished because it is all done,
3678 * -2 if the reshape is finished due to an error.
3679 */
3680
3681 int advancing = (reshape->after.data_disks
3682 >= reshape->before.data_disks);
3683 unsigned long long need_backup; /* All data between start of array and
3684 * here will at some point need to
3685 * be backed up.
3686 */
3687 unsigned long long read_offset, write_offset;
3688 unsigned long long write_range;
3689 unsigned long long max_progress, target, completed;
3690 unsigned long long array_size = (info->component_size
3691 * reshape->before.data_disks);
3692 int fd;
3693 char buf[20];
3694
3695 /* First, we unsuspend any region that is now known to be safe.
3696 * If suspend_point is on the 'wrong' side of reshape_progress, then
3697 * we don't have or need suspension at the moment. This is true for
3698 * native metadata when we don't need to back-up.
3699 */
3700 if (advancing) {
3701 if (info->reshape_progress <= *suspend_point)
3702 sysfs_set_num(info, NULL, "suspend_lo",
3703 info->reshape_progress);
3704 } else {
3705 /* Note: this won't work in 2.6.37 and before.
3706 * Something somewhere should make sure we don't need it!
3707 */
3708 if (info->reshape_progress >= *suspend_point)
3709 sysfs_set_num(info, NULL, "suspend_hi",
3710 info->reshape_progress);
3711 }
3712
3713 /* Now work out how far it is safe to progress.
3714 * If the read_offset for ->reshape_progress is less than
3715 * 'blocks' beyond the write_offset, we can only progress as far
3716 * as a backup.
3717 * Otherwise we can progress until the write_offset for the new location
3718 * reaches (within 'blocks' of) the read_offset at the current location.
3719 * However that region must be suspended unless we are using native
3720 * metadata.
3721 * If we need to suspend more, we limit it to 128M per device, which is
3722 * rather arbitrary and should be some time-based calculation.
3723 */
3724 read_offset = info->reshape_progress / reshape->before.data_disks;
3725 write_offset = info->reshape_progress / reshape->after.data_disks;
3726 write_range = info->new_chunk/512;
3727 if (reshape->before.data_disks == reshape->after.data_disks)
3728 need_backup = array_size;
3729 else
3730 need_backup = reshape->backup_blocks;
3731 if (advancing) {
3732 if (read_offset < write_offset + write_range)
3733 max_progress = backup_point;
3734 else
3735 max_progress =
3736 read_offset *
3737 reshape->after.data_disks;
3738 } else {
3739 if (read_offset > write_offset - write_range)
3740 /* Can only progress as far as has been backed up,
3741 * which must be suspended */
3742 max_progress = backup_point;
3743 else if (info->reshape_progress <= need_backup)
3744 max_progress = backup_point;
3745 else {
3746 if (info->array.major_version >= 0)
3747 /* Can progress until backup is needed */
3748 max_progress = need_backup;
3749 else {
3750 /* Can progress until metadata update is required */
3751 max_progress =
3752 read_offset *
3753 reshape->after.data_disks;
3754 /* but data must be suspended */
3755 if (max_progress < *suspend_point)
3756 max_progress = *suspend_point;
3757 }
3758 }
3759 }
3760
3761 /* We know it is safe to progress to 'max_progress' providing
3762 * it is suspended or we are using native metadata.
3763 * Consider extending suspend_point 128M per device if it
3764 * is less than 64M per device beyond reshape_progress.
3765 * But always do a multiple of 'blocks'
3766 * FIXME this is too big - it takes to long to complete
3767 * this much.
3768 */
3769 target = 64*1024*2 * min(reshape->before.data_disks,
3770 reshape->after.data_disks);
3771 target /= reshape->backup_blocks;
3772 if (target < 2)
3773 target = 2;
3774 target *= reshape->backup_blocks;
3775
3776 /* For externally managed metadata we always need to suspend IO to
3777 * the area being reshaped so we regularly push suspend_point forward.
3778 * For native metadata we only need the suspend if we are going to do
3779 * a backup.
3780 */
3781 if (advancing) {
3782 if ((need_backup > info->reshape_progress
3783 || info->array.major_version < 0) &&
3784 *suspend_point < info->reshape_progress + target) {
3785 if (need_backup < *suspend_point + 2 * target)
3786 *suspend_point = need_backup;
3787 else if (*suspend_point + 2 * target < array_size)
3788 *suspend_point += 2 * target;
3789 else
3790 *suspend_point = array_size;
3791 sysfs_set_num(info, NULL, "suspend_hi", *suspend_point);
3792 if (max_progress > *suspend_point)
3793 max_progress = *suspend_point;
3794 }
3795 } else {
3796 if (info->array.major_version >= 0) {
3797 /* Only need to suspend when about to backup */
3798 if (info->reshape_progress < need_backup * 2 &&
3799 *suspend_point > 0) {
3800 *suspend_point = 0;
3801 sysfs_set_num(info, NULL, "suspend_lo", 0);
3802 sysfs_set_num(info, NULL, "suspend_hi", need_backup);
3803 }
3804 } else {
3805 /* Need to suspend continually */
3806 if (info->reshape_progress < *suspend_point)
3807 *suspend_point = info->reshape_progress;
3808 if (*suspend_point + target < info->reshape_progress)
3809 /* No need to move suspend region yet */;
3810 else {
3811 if (*suspend_point >= 2 * target)
3812 *suspend_point -= 2 * target;
3813 else
3814 *suspend_point = 0;
3815 sysfs_set_num(info, NULL, "suspend_lo",
3816 *suspend_point);
3817 }
3818 if (max_progress < *suspend_point)
3819 max_progress = *suspend_point;
3820 }
3821 }
3822
3823 /* now set sync_max to allow that progress. sync_max, like
3824 * sync_completed is a count of sectors written per device, so
3825 * we find the difference between max_progress and the start point,
3826 * and divide that by after.data_disks to get a sync_max
3827 * number.
3828 * At the same time we convert wait_point to a similar number
3829 * for comparing against sync_completed.
3830 */
3831 /* scale down max_progress to per_disk */
3832 max_progress /= reshape->after.data_disks;
3833 /* Round to chunk size as some kernels give an erroneously high number */
3834 max_progress /= info->new_chunk/512;
3835 max_progress *= info->new_chunk/512;
3836 /* And round to old chunk size as the kernel wants that */
3837 max_progress /= info->array.chunk_size/512;
3838 max_progress *= info->array.chunk_size/512;
3839 /* Limit progress to the whole device */
3840 if (max_progress > info->component_size)
3841 max_progress = info->component_size;
3842 wait_point /= reshape->after.data_disks;
3843 if (!advancing) {
3844 /* switch from 'device offset' to 'processed block count' */
3845 max_progress = info->component_size - max_progress;
3846 wait_point = info->component_size - wait_point;
3847 }
3848
3849 if (!*frozen)
3850 sysfs_set_num(info, NULL, "sync_max", max_progress);
3851
3852 /* Now wait. If we have already reached the point that we were
3853 * asked to wait to, don't wait at all, else wait for any change.
3854 * We need to select on 'sync_completed' as that is the place that
3855 * notifications happen, but we are really interested in
3856 * 'reshape_position'
3857 */
3858 fd = sysfs_get_fd(info, NULL, "sync_completed");
3859 if (fd < 0)
3860 goto check_progress;
3861
3862 if (sysfs_fd_get_ll(fd, &completed) < 0)
3863 goto check_progress;
3864
3865 while (completed < max_progress && completed < wait_point) {
3866 /* Check that sync_action is still 'reshape' to avoid
3867 * waiting forever on a dead array
3868 */
3869 char action[20];
3870 if (sysfs_get_str(info, NULL, "sync_action",
3871 action, 20) <= 0 ||
3872 strncmp(action, "reshape", 7) != 0)
3873 break;
3874 /* Some kernels reset 'sync_completed' to zero
3875 * before setting 'sync_action' to 'idle'.
3876 * So we need these extra tests.
3877 */
3878 if (completed == 0 && advancing
3879 && info->reshape_progress > 0)
3880 break;
3881 if (completed == 0 && !advancing
3882 && info->reshape_progress < (info->component_size
3883 * reshape->after.data_disks))
3884 break;
3885 sysfs_wait(fd, NULL);
3886 if (sysfs_fd_get_ll(fd, &completed) < 0)
3887 goto check_progress;
3888 }
3889 /* Some kernels reset 'sync_completed' to zero,
3890 * we need to have real point we are in md
3891 */
3892 if (completed == 0)
3893 completed = max_progress;
3894
3895 /* some kernels can give an incorrectly high 'completed' number */
3896 completed /= (info->new_chunk/512);
3897 completed *= (info->new_chunk/512);
3898 /* Convert 'completed' back in to a 'progress' number */
3899 completed *= reshape->after.data_disks;
3900 if (!advancing) {
3901 completed = info->component_size * reshape->after.data_disks
3902 - completed;
3903 }
3904 *reshape_completed = completed;
3905
3906 close(fd);
3907
3908 /* We return the need_backup flag. Caller will decide
3909 * how much - a multiple of ->backup_blocks up to *suspend_point
3910 */
3911 if (advancing)
3912 return need_backup > info->reshape_progress;
3913 else
3914 return need_backup >= info->reshape_progress;
3915
3916 check_progress:
3917 /* if we couldn't read a number from sync_completed, then
3918 * either the reshape did complete, or it aborted.
3919 * We can tell which by checking for 'none' in reshape_position.
3920 * If it did abort, then it might immediately restart if it
3921 * it was just a device failure that leaves us degraded but
3922 * functioning.
3923 */
3924 strcpy(buf, "hi");
3925 if (sysfs_get_str(info, NULL, "reshape_position", buf, sizeof(buf)) < 0
3926 || strncmp(buf, "none", 4) != 0) {
3927 /* The abort might only be temporary. Wait up to 10
3928 * seconds for fd to contain a valid number again.
3929 */
3930 int wait = 10000;
3931 int rv = -2;
3932 unsigned long long new_sync_max;
3933 while (fd >= 0 && rv < 0 && wait > 0) {
3934 if (sysfs_wait(fd, &wait) != 1)
3935 break;
3936 switch (sysfs_fd_get_ll(fd, &completed)) {
3937 case 0:
3938 /* all good again */
3939 rv = 1;
3940 /* If "sync_max" is no longer max_progress
3941 * we need to freeze things
3942 */
3943 sysfs_get_ll(info, NULL, "sync_max", &new_sync_max);
3944 *frozen = (new_sync_max != max_progress);
3945 break;
3946 case -2: /* read error - abort */
3947 wait = 0;
3948 break;
3949 }
3950 }
3951 if (fd >= 0)
3952 close(fd);
3953 return rv; /* abort */
3954 } else {
3955 /* Maybe racing with array shutdown - check state */
3956 if (fd >= 0)
3957 close(fd);
3958 if (sysfs_get_str(info, NULL, "array_state", buf, sizeof(buf)) < 0
3959 || strncmp(buf, "inactive", 8) == 0
3960 || strncmp(buf, "clear",5) == 0)
3961 return -2; /* abort */
3962 return -1; /* complete */
3963 }
3964 }
3965
3966 /* FIXME return status is never checked */
3967 static int grow_backup(struct mdinfo *sra,
3968 unsigned long long offset, /* per device */
3969 unsigned long stripes, /* per device, in old chunks */
3970 int *sources, unsigned long long *offsets,
3971 int disks, int chunk, int level, int layout,
3972 int dests, int *destfd, unsigned long long *destoffsets,
3973 int part, int *degraded,
3974 char *buf)
3975 {
3976 /* Backup 'blocks' sectors at 'offset' on each device of the array,
3977 * to storage 'destfd' (offset 'destoffsets'), after first
3978 * suspending IO. Then allow resync to continue
3979 * over the suspended section.
3980 * Use part 'part' of the backup-super-block.
3981 */
3982 int odata = disks;
3983 int rv = 0;
3984 int i;
3985 unsigned long long ll;
3986 int new_degraded;
3987 //printf("offset %llu\n", offset);
3988 if (level >= 4)
3989 odata--;
3990 if (level == 6)
3991 odata--;
3992
3993 /* Check that array hasn't become degraded, else we might backup the wrong data */
3994 if (sysfs_get_ll(sra, NULL, "degraded", &ll) < 0)
3995 return -1; /* FIXME this error is ignored */
3996 new_degraded = (int)ll;
3997 if (new_degraded != *degraded) {
3998 /* check each device to ensure it is still working */
3999 struct mdinfo *sd;
4000 for (sd = sra->devs ; sd ; sd = sd->next) {
4001 if (sd->disk.state & (1<<MD_DISK_FAULTY))
4002 continue;
4003 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
4004 char sbuf[20];
4005 if (sysfs_get_str(sra, sd, "state", sbuf, 20) < 0 ||
4006 strstr(sbuf, "faulty") ||
4007 strstr(sbuf, "in_sync") == NULL) {
4008 /* this device is dead */
4009 sd->disk.state = (1<<MD_DISK_FAULTY);
4010 if (sd->disk.raid_disk >= 0 &&
4011 sources[sd->disk.raid_disk] >= 0) {
4012 close(sources[sd->disk.raid_disk]);
4013 sources[sd->disk.raid_disk] = -1;
4014 }
4015 }
4016 }
4017 }
4018 *degraded = new_degraded;
4019 }
4020 if (part) {
4021 bsb.arraystart2 = __cpu_to_le64(offset * odata);
4022 bsb.length2 = __cpu_to_le64(stripes * (chunk/512) * odata);
4023 } else {
4024 bsb.arraystart = __cpu_to_le64(offset * odata);
4025 bsb.length = __cpu_to_le64(stripes * (chunk/512) * odata);
4026 }
4027 if (part)
4028 bsb.magic[15] = '2';
4029 for (i = 0; i < dests; i++)
4030 if (part)
4031 lseek64(destfd[i], destoffsets[i] + __le64_to_cpu(bsb.devstart2)*512, 0);
4032 else
4033 lseek64(destfd[i], destoffsets[i], 0);
4034
4035 rv = save_stripes(sources, offsets,
4036 disks, chunk, level, layout,
4037 dests, destfd,
4038 offset*512*odata, stripes * chunk * odata,
4039 buf);
4040
4041 if (rv)
4042 return rv;
4043 bsb.mtime = __cpu_to_le64(time(0));
4044 for (i = 0; i < dests; i++) {
4045 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
4046
4047 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
4048 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
4049 bsb.sb_csum2 = bsb_csum((char*)&bsb,
4050 ((char*)&bsb.sb_csum2)-((char*)&bsb));
4051
4052 rv = -1;
4053 if ((unsigned long long)lseek64(destfd[i], destoffsets[i] - 4096, 0)
4054 != destoffsets[i] - 4096)
4055 break;
4056 if (write(destfd[i], &bsb, 512) != 512)
4057 break;
4058 if (destoffsets[i] > 4096) {
4059 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]+stripes*chunk*odata, 0) !=
4060 destoffsets[i]+stripes*chunk*odata)
4061 break;
4062 if (write(destfd[i], &bsb, 512) != 512)
4063 break;
4064 }
4065 fsync(destfd[i]);
4066 rv = 0;
4067 }
4068
4069 return rv;
4070 }
4071
4072 /* in 2.6.30, the value reported by sync_completed can be
4073 * less that it should be by one stripe.
4074 * This only happens when reshape hits sync_max and pauses.
4075 * So allow wait_backup to either extent sync_max further
4076 * than strictly necessary, or return before the
4077 * sync has got quite as far as we would really like.
4078 * This is what 'blocks2' is for.
4079 * The various caller give appropriate values so that
4080 * every works.
4081 */
4082 /* FIXME return value is often ignored */
4083 static int forget_backup(int dests, int *destfd,
4084 unsigned long long *destoffsets,
4085 int part)
4086 {
4087 /*
4088 * Erase backup 'part' (which is 0 or 1)
4089 */
4090 int i;
4091 int rv;
4092
4093 if (part) {
4094 bsb.arraystart2 = __cpu_to_le64(0);
4095 bsb.length2 = __cpu_to_le64(0);
4096 } else {
4097 bsb.arraystart = __cpu_to_le64(0);
4098 bsb.length = __cpu_to_le64(0);
4099 }
4100 bsb.mtime = __cpu_to_le64(time(0));
4101 rv = 0;
4102 for (i = 0; i < dests; i++) {
4103 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
4104 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
4105 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
4106 bsb.sb_csum2 = bsb_csum((char*)&bsb,
4107 ((char*)&bsb.sb_csum2)-((char*)&bsb));
4108 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]-4096, 0) !=
4109 destoffsets[i]-4096)
4110 rv = -1;
4111 if (rv == 0 &&
4112 write(destfd[i], &bsb, 512) != 512)
4113 rv = -1;
4114 fsync(destfd[i]);
4115 }
4116 return rv;
4117 }
4118
4119 static void fail(char *msg)
4120 {
4121 int rv;
4122 rv = (write(2, msg, strlen(msg)) != (int)strlen(msg));
4123 rv |= (write(2, "\n", 1) != 1);
4124 exit(rv ? 1 : 2);
4125 }
4126
4127 static char *abuf, *bbuf;
4128 static unsigned long long abuflen;
4129 static void validate(int afd, int bfd, unsigned long long offset)
4130 {
4131 /* check that the data in the backup against the array.
4132 * This is only used for regression testing and should not
4133 * be used while the array is active
4134 */
4135 if (afd < 0)
4136 return;
4137 lseek64(bfd, offset - 4096, 0);
4138 if (read(bfd, &bsb2, 512) != 512)
4139 fail("cannot read bsb");
4140 if (bsb2.sb_csum != bsb_csum((char*)&bsb2,
4141 ((char*)&bsb2.sb_csum)-((char*)&bsb2)))
4142 fail("first csum bad");
4143 if (memcmp(bsb2.magic, "md_backup_data", 14) != 0)
4144 fail("magic is bad");
4145 if (memcmp(bsb2.magic, "md_backup_data-2", 16) == 0 &&
4146 bsb2.sb_csum2 != bsb_csum((char*)&bsb2,
4147 ((char*)&bsb2.sb_csum2)-((char*)&bsb2)))
4148 fail("second csum bad");
4149
4150 if (__le64_to_cpu(bsb2.devstart)*512 != offset)
4151 fail("devstart is wrong");
4152
4153 if (bsb2.length) {
4154 unsigned long long len = __le64_to_cpu(bsb2.length)*512;
4155
4156 if (abuflen < len) {
4157 free(abuf);
4158 free(bbuf);
4159 abuflen = len;
4160 if (posix_memalign((void**)&abuf, 4096, abuflen) ||
4161 posix_memalign((void**)&bbuf, 4096, abuflen)) {
4162 abuflen = 0;
4163 /* just stop validating on mem-alloc failure */
4164 return;
4165 }
4166 }
4167
4168 lseek64(bfd, offset, 0);
4169 if ((unsigned long long)read(bfd, bbuf, len) != len) {
4170 //printf("len %llu\n", len);
4171 fail("read first backup failed");
4172 }
4173 lseek64(afd, __le64_to_cpu(bsb2.arraystart)*512, 0);
4174 if ((unsigned long long)read(afd, abuf, len) != len)
4175 fail("read first from array failed");
4176 if (memcmp(bbuf, abuf, len) != 0) {
4177 #if 0
4178 int i;
4179 printf("offset=%llu len=%llu\n",
4180 (unsigned long long)__le64_to_cpu(bsb2.arraystart)*512, len);
4181 for (i=0; i<len; i++)
4182 if (bbuf[i] != abuf[i]) {
4183 printf("first diff byte %d\n", i);
4184 break;
4185 }
4186 #endif
4187 fail("data1 compare failed");
4188 }
4189 }
4190 if (bsb2.length2) {
4191 unsigned long long len = __le64_to_cpu(bsb2.length2)*512;
4192
4193 if (abuflen < len) {
4194 free(abuf);
4195 free(bbuf);
4196 abuflen = len;
4197 abuf = xmalloc(abuflen);
4198 bbuf = xmalloc(abuflen);
4199 }
4200
4201 lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0);
4202 if ((unsigned long long)read(bfd, bbuf, len) != len)
4203 fail("read second backup failed");
4204 lseek64(afd, __le64_to_cpu(bsb2.arraystart2)*512, 0);
4205 if ((unsigned long long)read(afd, abuf, len) != len)
4206 fail("read second from array failed");
4207 if (memcmp(bbuf, abuf, len) != 0)
4208 fail("data2 compare failed");
4209 }
4210 }
4211
4212 int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
4213 struct supertype *st, unsigned long blocks,
4214 int *fds, unsigned long long *offsets,
4215 int dests, int *destfd, unsigned long long *destoffsets)
4216 {
4217 /* Monitor a reshape where backup is being performed using
4218 * 'native' mechanism - either to a backup file, or
4219 * to some space in a spare.
4220 */
4221 char *buf;
4222 int degraded = -1;
4223 unsigned long long speed;
4224 unsigned long long suspend_point, array_size;
4225 unsigned long long backup_point, wait_point;
4226 unsigned long long reshape_completed;
4227 int done = 0;
4228 int increasing = reshape->after.data_disks >= reshape->before.data_disks;
4229 int part = 0; /* The next part of the backup area to fill. It may already
4230 * be full, so we need to check */
4231 int level = reshape->level;
4232 int layout = reshape->before.layout;
4233 int data = reshape->before.data_disks;
4234 int disks = reshape->before.data_disks + reshape->parity;
4235 int chunk = sra->array.chunk_size;
4236 struct mdinfo *sd;
4237 unsigned long stripes;
4238 int uuid[4];
4239 int frozen = 0;
4240
4241 /* set up the backup-super-block. This requires the
4242 * uuid from the array.
4243 */
4244 /* Find a superblock */
4245 for (sd = sra->devs; sd; sd = sd->next) {
4246 char *dn;
4247 int devfd;
4248 int ok;
4249 if (sd->disk.state & (1<<MD_DISK_FAULTY))
4250 continue;
4251 dn = map_dev(sd->disk.major, sd->disk.minor, 1);
4252 devfd = dev_open(dn, O_RDONLY);
4253 if (devfd < 0)
4254 continue;
4255 ok = st->ss->load_super(st, devfd, NULL);
4256 close(devfd);
4257 if (ok == 0)
4258 break;
4259 }
4260 if (!sd) {
4261 pr_err("Cannot find a superblock\n");
4262 return 0;
4263 }
4264
4265 memset(&bsb, 0, 512);
4266 memcpy(bsb.magic, "md_backup_data-1", 16);
4267 st->ss->uuid_from_super(st, uuid);
4268 memcpy(bsb.set_uuid, uuid, 16);
4269 bsb.mtime = __cpu_to_le64(time(0));
4270 bsb.devstart2 = blocks;
4271
4272 stripes = blocks / (sra->array.chunk_size/512) /
4273 reshape->before.data_disks;
4274
4275 if (posix_memalign((void**)&buf, 4096, disks * chunk))
4276 /* Don't start the 'reshape' */
4277 return 0;
4278 if (reshape->before.data_disks == reshape->after.data_disks) {
4279 sysfs_get_ll(sra, NULL, "sync_speed_min", &speed);
4280 sysfs_set_num(sra, NULL, "sync_speed_min", 200000);
4281 }
4282
4283 if (increasing) {
4284 array_size = sra->component_size * reshape->after.data_disks;
4285 backup_point = sra->reshape_progress;
4286 suspend_point = 0;
4287 } else {
4288 array_size = sra->component_size * reshape->before.data_disks;
4289 backup_point = reshape->backup_blocks;
4290 suspend_point = array_size;
4291 }
4292
4293 while (!done) {
4294 int rv;
4295
4296 /* Want to return as soon the oldest backup slot can
4297 * be released as that allows us to start backing up
4298 * some more, providing suspend_point has been
4299 * advanced, which it should have.
4300 */
4301 if (increasing) {
4302 wait_point = array_size;
4303 if (part == 0 && __le64_to_cpu(bsb.length) > 0)
4304 wait_point = (__le64_to_cpu(bsb.arraystart) +
4305 __le64_to_cpu(bsb.length));
4306 if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
4307 wait_point = (__le64_to_cpu(bsb.arraystart2) +
4308 __le64_to_cpu(bsb.length2));
4309 } else {
4310 wait_point = 0;
4311 if (part == 0 && __le64_to_cpu(bsb.length) > 0)
4312 wait_point = __le64_to_cpu(bsb.arraystart);
4313 if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
4314 wait_point = __le64_to_cpu(bsb.arraystart2);
4315 }
4316
4317 reshape_completed = sra->reshape_progress;
4318 rv = progress_reshape(sra, reshape,
4319 backup_point, wait_point,
4320 &suspend_point, &reshape_completed,
4321 &frozen);
4322 /* external metadata would need to ping_monitor here */
4323 sra->reshape_progress = reshape_completed;
4324
4325 /* Clear any backup region that is before 'here' */
4326 if (increasing) {
4327 if (__le64_to_cpu(bsb.length) > 0 &&
4328 reshape_completed >= (__le64_to_cpu(bsb.arraystart) +
4329 __le64_to_cpu(bsb.length)))
4330 forget_backup(dests, destfd,
4331 destoffsets, 0);
4332 if (__le64_to_cpu(bsb.length2) > 0 &&
4333 reshape_completed >= (__le64_to_cpu(bsb.arraystart2) +
4334 __le64_to_cpu(bsb.length2)))
4335 forget_backup(dests, destfd,
4336 destoffsets, 1);
4337 } else {
4338 if (__le64_to_cpu(bsb.length) > 0 &&
4339 reshape_completed <= (__le64_to_cpu(bsb.arraystart)))
4340 forget_backup(dests, destfd,
4341 destoffsets, 0);
4342 if (__le64_to_cpu(bsb.length2) > 0 &&
4343 reshape_completed <= (__le64_to_cpu(bsb.arraystart2)))
4344 forget_backup(dests, destfd,
4345 destoffsets, 1);
4346 }
4347 if (sigterm)
4348 rv = -2;
4349 if (rv < 0) {
4350 if (rv == -1)
4351 done = 1;
4352 break;
4353 }
4354 if (rv == 0 && increasing && !st->ss->external) {
4355 /* No longer need to monitor this reshape */
4356 sysfs_set_str(sra, NULL, "sync_max", "max");
4357 done = 1;
4358 break;
4359 }
4360
4361 while (rv) {
4362 unsigned long long offset;
4363 unsigned long actual_stripes;
4364 /* Need to backup some data.
4365 * If 'part' is not used and the desired
4366 * backup size is suspended, do a backup,
4367 * then consider the next part.
4368 */
4369 /* Check that 'part' is unused */
4370 if (part == 0 && __le64_to_cpu(bsb.length) != 0)
4371 break;
4372 if (part == 1 && __le64_to_cpu(bsb.length2) != 0)
4373 break;
4374
4375 offset = backup_point / data;
4376 actual_stripes = stripes;
4377 if (increasing) {
4378 if (offset + actual_stripes * (chunk/512) >
4379 sra->component_size)
4380 actual_stripes = ((sra->component_size - offset)
4381 / (chunk/512));
4382 if (offset + actual_stripes * (chunk/512) >
4383 suspend_point/data)
4384 break;
4385 } else {
4386 if (offset < actual_stripes * (chunk/512))
4387 actual_stripes = offset / (chunk/512);
4388 offset -= actual_stripes * (chunk/512);
4389 if (offset < suspend_point/data)
4390 break;
4391 }
4392 if (actual_stripes == 0)
4393 break;
4394 grow_backup(sra, offset, actual_stripes,
4395 fds, offsets,
4396 disks, chunk, level, layout,
4397 dests, destfd, destoffsets,
4398 part, &degraded, buf);
4399 validate(afd, destfd[0], destoffsets[0]);
4400 /* record where 'part' is up to */
4401 part = !part;
4402 if (increasing)
4403 backup_point += actual_stripes * (chunk/512) * data;
4404 else
4405 backup_point -= actual_stripes * (chunk/512) * data;
4406 }
4407 }
4408
4409 /* FIXME maybe call progress_reshape one more time instead */
4410 /* remove any remaining suspension */
4411 sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
4412 sysfs_set_num(sra, NULL, "suspend_hi", 0);
4413 sysfs_set_num(sra, NULL, "suspend_lo", 0);
4414 sysfs_set_num(sra, NULL, "sync_min", 0);
4415
4416 if (reshape->before.data_disks == reshape->after.data_disks)
4417 sysfs_set_num(sra, NULL, "sync_speed_min", speed);
4418 free(buf);
4419 return done;
4420 }
4421
4422 /*
4423 * If any spare contains md_back_data-1 which is recent wrt mtime,
4424 * write that data into the array and update the super blocks with
4425 * the new reshape_progress
4426 */
4427 int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt,
4428 char *backup_file, int verbose)
4429 {
4430 int i, j;
4431 int old_disks;
4432 unsigned long long *offsets;
4433 unsigned long long nstripe, ostripe;
4434 int ndata, odata;
4435
4436 odata = info->array.raid_disks - info->delta_disks - 1;
4437 if (info->array.level == 6) odata--; /* number of data disks */
4438 ndata = info->array.raid_disks - 1;
4439 if (info->new_level == 6) ndata--;
4440
4441 old_disks = info->array.raid_disks - info->delta_disks;
4442
4443 if (info->delta_disks <= 0)
4444 /* Didn't grow, so the backup file must have
4445 * been used
4446 */
4447 old_disks = cnt;
4448 for (i=old_disks-(backup_file?1:0); i<cnt; i++) {
4449 struct mdinfo dinfo;
4450 int fd;
4451 int bsbsize;
4452 char *devname, namebuf[20];
4453 unsigned long long lo, hi;
4454
4455 /* This was a spare and may have some saved data on it.
4456 * Load the superblock, find and load the
4457 * backup_super_block.
4458 * If either fail, go on to next device.
4459 * If the backup contains no new info, just return
4460 * else restore data and update all superblocks
4461 */
4462 if (i == old_disks-1) {
4463 fd = open(backup_file, O_RDONLY);
4464 if (fd<0) {
4465 pr_err("backup file %s inaccessible: %s\n",
4466 backup_file, strerror(errno));
4467 continue;
4468 }
4469 devname = backup_file;
4470 } else {
4471 fd = fdlist[i];
4472 if (fd < 0)
4473 continue;
4474 if (st->ss->load_super(st, fd, NULL))
4475 continue;
4476
4477 st->ss->getinfo_super(st, &dinfo, NULL);
4478 st->ss->free_super(st);
4479
4480 if (lseek64(fd,
4481 (dinfo.data_offset + dinfo.component_size - 8) <<9,
4482 0) < 0) {
4483 pr_err("Cannot seek on device %d\n", i);
4484 continue; /* Cannot seek */
4485 }
4486 sprintf(namebuf, "device-%d", i);
4487 devname = namebuf;
4488 }
4489 if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb)) {
4490 if (verbose)
4491 pr_err("Cannot read from %s\n", devname);
4492 continue; /* Cannot read */
4493 }
4494 if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0 &&
4495 memcmp(bsb.magic, "md_backup_data-2", 16) != 0) {
4496 if (verbose)
4497 pr_err("No backup metadata on %s\n", devname);
4498 continue;
4499 }
4500 if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb))) {
4501 if (verbose)
4502 pr_err("Bad backup-metadata checksum on %s\n", devname);
4503 continue; /* bad checksum */
4504 }
4505 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0 &&
4506 bsb.sb_csum2 != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum2)-((char*)&bsb))) {
4507 if (verbose)
4508 pr_err("Bad backup-metadata checksum2 on %s\n", devname);
4509 continue; /* Bad second checksum */
4510 }
4511 if (memcmp(bsb.set_uuid,info->uuid, 16) != 0) {
4512 if (verbose)
4513 pr_err("Wrong uuid on backup-metadata on %s\n", devname);
4514 continue; /* Wrong uuid */
4515 }
4516
4517 /* array utime and backup-mtime should be updated at much the same time, but it seems that
4518 * sometimes they aren't... So allow considerable flexability in matching, and allow
4519 * this test to be overridden by an environment variable.
4520 */
4521 if (info->array.utime > (int)__le64_to_cpu(bsb.mtime) + 2*60*60 ||
4522 info->array.utime < (int)__le64_to_cpu(bsb.mtime) - 10*60) {
4523 if (check_env("MDADM_GROW_ALLOW_OLD")) {
4524 pr_err("accepting backup with timestamp %lu "
4525 "for array with timestamp %lu\n",
4526 (unsigned long)__le64_to_cpu(bsb.mtime),
4527 (unsigned long)info->array.utime);
4528 } else {
4529 pr_err("too-old timestamp on backup-metadata on %s\n", devname);
4530 pr_err("If you think it is should be safe, try 'export MDADM_GROW_ALLOW_OLD=1'\n");
4531 continue; /* time stamp is too bad */
4532 }
4533 }
4534
4535 if (bsb.magic[15] == '1') {
4536 if (bsb.length == 0)
4537 continue;
4538 if (info->delta_disks >= 0) {
4539 /* reshape_progress is increasing */
4540 if (__le64_to_cpu(bsb.arraystart)
4541 + __le64_to_cpu(bsb.length)
4542 < info->reshape_progress) {
4543 nonew:
4544 if (verbose)
4545 pr_err("backup-metadata found on %s but is not needed\n", devname);
4546 continue; /* No new data here */
4547 }
4548 } else {
4549 /* reshape_progress is decreasing */
4550 if (__le64_to_cpu(bsb.arraystart) >=
4551 info->reshape_progress)
4552 goto nonew; /* No new data here */
4553 }
4554 } else {
4555 if (bsb.length == 0 && bsb.length2 == 0)
4556 continue;
4557 if (info->delta_disks >= 0) {
4558 /* reshape_progress is increasing */
4559 if ((__le64_to_cpu(bsb.arraystart)
4560 + __le64_to_cpu(bsb.length)
4561 < info->reshape_progress)
4562 &&
4563 (__le64_to_cpu(bsb.arraystart2)
4564 + __le64_to_cpu(bsb.length2)
4565 < info->reshape_progress))
4566 goto nonew; /* No new data here */
4567 } else {
4568 /* reshape_progress is decreasing */
4569 if (__le64_to_cpu(bsb.arraystart) >=
4570 info->reshape_progress &&
4571 __le64_to_cpu(bsb.arraystart2) >=
4572 info->reshape_progress)
4573 goto nonew; /* No new data here */
4574 }
4575 }
4576 if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0) {
4577 second_fail:
4578 if (verbose)
4579 pr_err("Failed to verify secondary backup-metadata block on %s\n",
4580 devname);
4581 continue; /* Cannot seek */
4582 }
4583 /* There should be a duplicate backup superblock 4k before here */
4584 if (lseek64(fd, -4096, 1) < 0 ||
4585 read(fd, &bsb2, sizeof(bsb2)) != sizeof(bsb2))
4586 goto second_fail; /* Cannot find leading superblock */
4587 if (bsb.magic[15] == '1')
4588 bsbsize = offsetof(struct mdp_backup_super, pad1);
4589 else
4590 bsbsize = offsetof(struct mdp_backup_super, pad);
4591 if (memcmp(&bsb2, &bsb, bsbsize) != 0)
4592 goto second_fail; /* Cannot find leading superblock */
4593
4594 /* Now need the data offsets for all devices. */
4595 offsets = xmalloc(sizeof(*offsets)*info->array.raid_disks);
4596 for(j=0; j<info->array.raid_disks; j++) {
4597 if (fdlist[j] < 0)
4598 continue;
4599 if (st->ss->load_super(st, fdlist[j], NULL))
4600 /* FIXME should be this be an error */
4601 continue;
4602 st->ss->getinfo_super(st, &dinfo, NULL);
4603 st->ss->free_super(st);
4604 offsets[j] = dinfo.data_offset * 512;
4605 }
4606 printf(Name ": restoring critical section\n");
4607
4608 if (restore_stripes(fdlist, offsets,
4609 info->array.raid_disks,
4610 info->new_chunk,
4611 info->new_level,
4612 info->new_layout,
4613 fd, __le64_to_cpu(bsb.devstart)*512,
4614 __le64_to_cpu(bsb.arraystart)*512,
4615 __le64_to_cpu(bsb.length)*512, NULL)) {
4616 /* didn't succeed, so giveup */
4617 if (verbose)
4618 pr_err("Error restoring backup from %s\n",
4619 devname);
4620 free(offsets);
4621 return 1;
4622 }
4623
4624 if (bsb.magic[15] == '2' &&
4625 restore_stripes(fdlist, offsets,
4626 info->array.raid_disks,
4627 info->new_chunk,
4628 info->new_level,
4629 info->new_layout,
4630 fd, __le64_to_cpu(bsb.devstart)*512 +
4631 __le64_to_cpu(bsb.devstart2)*512,
4632 __le64_to_cpu(bsb.arraystart2)*512,
4633 __le64_to_cpu(bsb.length2)*512, NULL)) {
4634 /* didn't succeed, so giveup */
4635 if (verbose)
4636 pr_err("Error restoring second backup from %s\n",
4637 devname);
4638 free(offsets);
4639 return 1;
4640 }
4641
4642 free(offsets);
4643
4644 /* Ok, so the data is restored. Let's update those superblocks. */
4645
4646 lo = hi = 0;
4647 if (bsb.length) {
4648 lo = __le64_to_cpu(bsb.arraystart);
4649 hi = lo + __le64_to_cpu(bsb.length);
4650 }
4651 if (bsb.magic[15] == '2' && bsb.length2) {
4652 unsigned long long lo1, hi1;
4653 lo1 = __le64_to_cpu(bsb.arraystart2);
4654 hi1 = lo1 + __le64_to_cpu(bsb.length2);
4655 if (lo == hi) {
4656 lo = lo1;
4657 hi = hi1;
4658 } else if (lo < lo1)
4659 hi = hi1;
4660 else
4661 lo = lo1;
4662 }
4663 if (lo < hi &&
4664 (info->reshape_progress < lo ||
4665 info->reshape_progress > hi))
4666 /* backup does not affect reshape_progress*/ ;
4667 else if (info->delta_disks >= 0) {
4668 info->reshape_progress = __le64_to_cpu(bsb.arraystart) +
4669 __le64_to_cpu(bsb.length);
4670 if (bsb.magic[15] == '2') {
4671 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2) +
4672 __le64_to_cpu(bsb.length2);
4673 if (p2 > info->reshape_progress)
4674 info->reshape_progress = p2;
4675 }
4676 } else {
4677 info->reshape_progress = __le64_to_cpu(bsb.arraystart);
4678 if (bsb.magic[15] == '2') {
4679 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2);
4680 if (p2 < info->reshape_progress)
4681 info->reshape_progress = p2;
4682 }
4683 }
4684 for (j=0; j<info->array.raid_disks; j++) {
4685 if (fdlist[j] < 0)
4686 continue;
4687 if (st->ss->load_super(st, fdlist[j], NULL))
4688 continue;
4689 st->ss->getinfo_super(st, &dinfo, NULL);
4690 dinfo.reshape_progress = info->reshape_progress;
4691 st->ss->update_super(st, &dinfo,
4692 "_reshape_progress",
4693 NULL,0, 0, NULL);
4694 st->ss->store_super(st, fdlist[j]);
4695 st->ss->free_super(st);
4696 }
4697 return 0;
4698 }
4699 /* Didn't find any backup data, try to see if any
4700 * was needed.
4701 */
4702 if (info->delta_disks < 0) {
4703 /* When shrinking, the critical section is at the end.
4704 * So see if we are before the critical section.
4705 */
4706 unsigned long long first_block;
4707 nstripe = ostripe = 0;
4708 first_block = 0;
4709 while (ostripe >= nstripe) {
4710 ostripe += info->array.chunk_size / 512;
4711 first_block = ostripe * odata;
4712 nstripe = first_block / ndata / (info->new_chunk/512) *
4713 (info->new_chunk/512);
4714 }
4715
4716 if (info->reshape_progress >= first_block)
4717 return 0;
4718 }
4719 if (info->delta_disks > 0) {
4720 /* See if we are beyond the critical section. */
4721 unsigned long long last_block;
4722 nstripe = ostripe = 0;
4723 last_block = 0;
4724 while (nstripe >= ostripe) {
4725 nstripe += info->new_chunk / 512;
4726 last_block = nstripe * ndata;
4727 ostripe = last_block / odata / (info->array.chunk_size/512) *
4728 (info->array.chunk_size/512);
4729 }
4730
4731 if (info->reshape_progress >= last_block)
4732 return 0;
4733 }
4734 /* needed to recover critical section! */
4735 if (verbose)
4736 pr_err("Failed to find backup of critical section\n");
4737 return 1;
4738 }
4739
4740 int Grow_continue_command(char *devname, int fd,
4741 char *backup_file, int verbose)
4742 {
4743 int ret_val = 0;
4744 struct supertype *st = NULL;
4745 struct mdinfo *content = NULL;
4746 struct mdinfo array;
4747 char *subarray = NULL;
4748 struct mdinfo *cc = NULL;
4749 struct mdstat_ent *mdstat = NULL;
4750 int cfd = -1;
4751 int fd2 = -1;
4752
4753 dprintf("Grow continue from command line called for %s\n",
4754 devname);
4755
4756 st = super_by_fd(fd, &subarray);
4757 if (!st || !st->ss) {
4758 pr_err("Unable to determine metadata format for %s\n",
4759 devname);
4760 return 1;
4761 }
4762 dprintf("Grow continue is run for ");
4763 if (st->ss->external == 0) {
4764 int d;
4765 dprintf("native array (%s)\n", devname);
4766 if (ioctl(fd, GET_ARRAY_INFO, &array.array) < 0) {
4767 pr_err("%s is not an active md array -"
4768 " aborting\n", devname);
4769 ret_val = 1;
4770 goto Grow_continue_command_exit;
4771 }
4772 content = &array;
4773 /* Need to load a superblock.
4774 * FIXME we should really get what we need from
4775 * sysfs
4776 */
4777 for (d = 0; d < MAX_DISKS; d++) {
4778 mdu_disk_info_t disk;
4779 char *dv;
4780 int err;
4781 disk.number = d;
4782 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
4783 continue;
4784 if (disk.major == 0 && disk.minor == 0)
4785 continue;
4786 if ((disk.state & (1 << MD_DISK_ACTIVE)) == 0)
4787 continue;
4788 dv = map_dev(disk.major, disk.minor, 1);
4789 if (!dv)
4790 continue;
4791 fd2 = dev_open(dv, O_RDONLY);
4792 if (fd2 < 0)
4793 continue;
4794 err = st->ss->load_super(st, fd2, NULL);
4795 close(fd2);
4796 /* invalidate fd2 to avoid possible double close() */
4797 fd2 = -1;
4798 if (err)
4799 continue;
4800 break;
4801 }
4802 if (d == MAX_DISKS) {
4803 pr_err("Unable to load metadata for %s\n",
4804 devname);
4805 ret_val = 1;
4806 goto Grow_continue_command_exit;
4807 }
4808 st->ss->getinfo_super(st, content, NULL);
4809 } else {
4810 char *container;
4811
4812 if (subarray) {
4813 dprintf("subarray (%s)\n", subarray);
4814 container = st->container_devnm;
4815 cfd = open_dev_excl(st->container_devnm);
4816 } else {
4817 container = st->devnm;
4818 close(fd);
4819 cfd = open_dev_excl(st->devnm);
4820 dprintf("container (%s)\n", container);
4821 fd = cfd;
4822 }
4823 if (cfd < 0) {
4824 pr_err("Unable to open container "
4825 "for %s\n", devname);
4826 ret_val = 1;
4827 goto Grow_continue_command_exit;
4828 }
4829
4830 /* find in container array under reshape
4831 */
4832 ret_val = st->ss->load_container(st, cfd, NULL);
4833 if (ret_val) {
4834 pr_err("Cannot read superblock for %s\n",
4835 devname);
4836 ret_val = 1;
4837 goto Grow_continue_command_exit;
4838 }
4839
4840 cc = st->ss->container_content(st, subarray);
4841 for (content = cc; content ; content = content->next) {
4842 char *array;
4843 int allow_reshape = 1;
4844
4845 if (content->reshape_active == 0)
4846 continue;
4847 /* The decision about array or container wide
4848 * reshape is taken in Grow_continue based
4849 * content->reshape_active state, therefore we
4850 * need to check_reshape based on
4851 * reshape_active and subarray name
4852 */
4853 if (content->array.state & (1<<MD_SB_BLOCK_VOLUME))
4854 allow_reshape = 0;
4855 if (content->reshape_active == CONTAINER_RESHAPE &&
4856 (content->array.state
4857 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE)))
4858 allow_reshape = 0;
4859
4860 if (!allow_reshape) {
4861 pr_err("cannot continue reshape of an array"
4862 " in container with unsupported"
4863 " metadata: %s(%s)\n",
4864 devname, container);
4865 ret_val = 1;
4866 goto Grow_continue_command_exit;
4867 }
4868
4869 array = strchr(content->text_version+1, '/')+1;
4870 mdstat = mdstat_by_subdev(array, container);
4871 if (!mdstat)
4872 continue;
4873 if (mdstat->active == 0) {
4874 pr_err("Skipping inactive array %s.\n",
4875 mdstat->devnm);
4876 free_mdstat(mdstat);
4877 mdstat = NULL;
4878 continue;
4879 }
4880 break;
4881 }
4882 if (!content) {
4883 pr_err("Unable to determine reshaped "
4884 "array for %s\n", devname);
4885 ret_val = 1;
4886 goto Grow_continue_command_exit;
4887 }
4888 fd2 = open_dev(mdstat->devnm);
4889 if (fd2 < 0) {
4890 pr_err("cannot open (%s)\n", mdstat->devnm);
4891 ret_val = 1;
4892 goto Grow_continue_command_exit;
4893 }
4894
4895 sysfs_init(content, fd2, mdstat->devnm);
4896
4897 /* start mdmon in case it is not running
4898 */
4899 if (!mdmon_running(container))
4900 start_mdmon(container);
4901 ping_monitor(container);
4902
4903 if (mdmon_running(container))
4904 st->update_tail = &st->updates;
4905 else {
4906 pr_err("No mdmon found. "
4907 "Grow cannot continue.\n");
4908 ret_val = 1;
4909 goto Grow_continue_command_exit;
4910 }
4911 }
4912
4913 /* verify that array under reshape is started from
4914 * correct position
4915 */
4916 if (verify_reshape_position(content, content->array.level) < 0) {
4917 ret_val = 1;
4918 goto Grow_continue_command_exit;
4919 }
4920
4921 /* continue reshape
4922 */
4923 ret_val = Grow_continue(fd, st, content, backup_file, 0);
4924
4925 Grow_continue_command_exit:
4926 if (fd2 > -1)
4927 close(fd2);
4928 if (cfd > -1)
4929 close(cfd);
4930 st->ss->free_super(st);
4931 free_mdstat(mdstat);
4932 sysfs_free(cc);
4933 free(subarray);
4934
4935 return ret_val;
4936 }
4937
4938 int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
4939 char *backup_file, int freeze_reshape)
4940 {
4941 int ret_val = 2;
4942
4943 if (!info->reshape_active)
4944 return ret_val;
4945
4946 if (st->ss->external) {
4947 int cfd = open_dev(st->container_devnm);
4948
4949 if (cfd < 0)
4950 return 1;
4951
4952 st->ss->load_container(st, cfd, st->container_devnm);
4953 close(cfd);
4954 ret_val = reshape_container(st->container_devnm, NULL, mdfd,
4955 st, info, 0, backup_file,
4956 0, 1,
4957 1 | info->reshape_active,
4958 freeze_reshape);
4959 } else
4960 ret_val = reshape_array(NULL, mdfd, "array", st, info, 1,
4961 NULL, INVALID_SECTORS,
4962 backup_file, 0, 1,
4963 1 | info->reshape_active,
4964 freeze_reshape);
4965
4966 return ret_val;
4967 }
4968
4969 char *make_backup(char *name)
4970 {
4971 char *base = "backup_file-";
4972 int len;
4973 char *fname;
4974
4975 len = strlen(MAP_DIR) + 1 + strlen(base) + strlen(name)+1;
4976 fname = xmalloc(len);
4977 sprintf(fname, "%s/%s%s", MAP_DIR, base, name);
4978 return fname;
4979 }
4980
4981 char *locate_backup(char *name)
4982 {
4983 char *fl = make_backup(name);
4984 struct stat stb;
4985
4986 if (stat(fl, &stb) == 0 &&
4987 S_ISREG(stb.st_mode))
4988 return fl;
4989
4990 free(fl);
4991 return NULL;
4992 }