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