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