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