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