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