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