]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Grow.c
Fix memory leak
[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 int mdfd,
1354 struct supertype *st,
1355 struct mdinfo *info,
1356 int force,
1357 char *backup_file,
1358 int quiet, int restart, int freeze_reshape);
1359
1360 /*
1361 * helper routine to check metadata reshape avalability
1362 * 1. Do not "grow" arrays with volume activation blocked
1363 * 2. do not reshape containers with container reshape blocked
1364 *
1365 * IN:
1366 * subarray - array name or NULL for container wide reshape
1367 * content - md device info from container_content
1368 * OUT:
1369 * 0 - block reshape
1370 */
1371 static int check_reshape(char *subarray, struct mdinfo *content)
1372 {
1373 char *ep;
1374 unsigned int idx;
1375
1376 if (!subarray) {
1377 if (content->array.state & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE))
1378 return 0;
1379 } else {
1380 /* do not "grow" arrays with volume activation blocked */
1381 idx = strtoul(subarray, &ep, 10);
1382 if (*ep == '\0'
1383 && content->container_member == (int) idx
1384 && (content->array.state & (1<<MD_SB_BLOCK_VOLUME)))
1385 return 0;
1386 }
1387 return 1;
1388 }
1389
1390 int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
1391 long long size,
1392 int level, char *layout_str, int chunksize, int raid_disks,
1393 struct mddev_dev *devlist,
1394 int assume_clean, int force)
1395 {
1396 /* Make some changes in the shape of an array.
1397 * The kernel must support the change.
1398 *
1399 * There are three different changes. Each can trigger
1400 * a resync or recovery so we freeze that until we have
1401 * requested everything (if kernel supports freezing - 2.6.30).
1402 * The steps are:
1403 * - change size (i.e. component_size)
1404 * - change level
1405 * - change layout/chunksize/ndisks
1406 *
1407 * The last can require a reshape. It is different on different
1408 * levels so we need to check the level before actioning it.
1409 * Some times the level change needs to be requested after the
1410 * reshape (e.g. raid6->raid5, raid5->raid0)
1411 *
1412 */
1413 struct mdu_array_info_s array;
1414 int rv = 0;
1415 struct supertype *st;
1416 char *subarray = NULL;
1417
1418 int frozen;
1419 int changed = 0;
1420 char *container = NULL;
1421 char container_buf[20];
1422 int cfd = -1;
1423
1424 struct mddev_dev *dv;
1425 int added_disks;
1426
1427 struct mdinfo info;
1428 struct mdinfo *sra;
1429
1430 if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) {
1431 fprintf(stderr, Name ": %s is not an active md array - aborting\n",
1432 devname);
1433 return 1;
1434 }
1435
1436 if (size >= 0 &&
1437 (chunksize || level!= UnSet || layout_str || raid_disks)) {
1438 fprintf(stderr, Name ": cannot change component size at the same time "
1439 "as other changes.\n"
1440 " Change size first, then check data is intact before "
1441 "making other changes.\n");
1442 return 1;
1443 }
1444
1445 if (raid_disks && raid_disks < array.raid_disks && array.level > 1 &&
1446 get_linux_version() < 2006032 &&
1447 !check_env("MDADM_FORCE_FEWER")) {
1448 fprintf(stderr, Name ": reducing the number of devices is not safe before Linux 2.6.32\n"
1449 " Please use a newer kernel\n");
1450 return 1;
1451 }
1452
1453 st = super_by_fd(fd, &subarray);
1454 if (!st) {
1455 fprintf(stderr, Name ": Unable to determine metadata format for %s\n", devname);
1456 return 1;
1457 }
1458 if (raid_disks > st->max_devs) {
1459 fprintf(stderr, Name ": Cannot increase raid-disks on this array"
1460 " beyond %d\n", st->max_devs);
1461 return 1;
1462 }
1463
1464 /* in the external case we need to check that the requested reshape is
1465 * supported, and perform an initial check that the container holds the
1466 * pre-requisite spare devices (mdmon owns final validation)
1467 */
1468 if (st->ss->external) {
1469 int container_dev;
1470 int rv;
1471
1472 if (subarray) {
1473 container_dev = st->container_dev;
1474 cfd = open_dev_excl(st->container_dev);
1475 } else {
1476 container_dev = st->devnum;
1477 close(fd);
1478 cfd = open_dev_excl(st->devnum);
1479 fd = cfd;
1480 }
1481 if (cfd < 0) {
1482 fprintf(stderr, Name ": Unable to open container for %s\n",
1483 devname);
1484 free(subarray);
1485 return 1;
1486 }
1487
1488 fmt_devname(container_buf, container_dev);
1489 container = container_buf;
1490
1491 rv = st->ss->load_container(st, cfd, NULL);
1492
1493 if (rv) {
1494 fprintf(stderr, Name ": Cannot read superblock for %s\n",
1495 devname);
1496 free(subarray);
1497 return 1;
1498 }
1499
1500 /* check if operation is supported for metadata handler */
1501 if (st->ss->container_content) {
1502 struct mdinfo *cc = NULL;
1503 struct mdinfo *content = NULL;
1504
1505 cc = st->ss->container_content(st, subarray);
1506 for (content = cc; content ; content = content->next) {
1507 int allow_reshape;
1508
1509 /* check if reshape is allowed based on metadata
1510 * indications stored in content.array.status
1511 */
1512 allow_reshape = check_reshape(subarray, content);
1513 if (!allow_reshape) {
1514 fprintf(stderr, Name
1515 " cannot reshape arrays in"
1516 " container with unsupported"
1517 " metadata: %s(%s)\n",
1518 devname, container_buf);
1519 sysfs_free(cc);
1520 free(subarray);
1521 return 1;
1522 }
1523 }
1524 sysfs_free(cc);
1525 }
1526 if (mdmon_running(container_dev))
1527 st->update_tail = &st->updates;
1528 }
1529
1530 added_disks = 0;
1531 for (dv = devlist; dv; dv = dv->next)
1532 added_disks++;
1533 if (raid_disks > array.raid_disks &&
1534 array.spare_disks +added_disks < (raid_disks - array.raid_disks) &&
1535 !force) {
1536 fprintf(stderr,
1537 Name ": Need %d spare%s to avoid degraded array,"
1538 " and only have %d.\n"
1539 " Use --force to over-ride this check.\n",
1540 raid_disks - array.raid_disks,
1541 raid_disks - array.raid_disks == 1 ? "" : "s",
1542 array.spare_disks + added_disks);
1543 return 1;
1544 }
1545
1546 sra = sysfs_read(fd, 0, GET_LEVEL | GET_DISKS | GET_DEVS
1547 | GET_STATE | GET_VERSION);
1548 if (sra) {
1549 if (st->ss->external && subarray == NULL) {
1550 array.level = LEVEL_CONTAINER;
1551 sra->array.level = LEVEL_CONTAINER;
1552 }
1553 } else {
1554 fprintf(stderr, Name ": failed to read sysfs parameters for %s\n",
1555 devname);
1556 return 1;
1557 }
1558 frozen = freeze(st);
1559 if (frozen < -1) {
1560 /* freeze() already spewed the reason */
1561 return 1;
1562 } else if (frozen < 0) {
1563 fprintf(stderr, Name ": %s is performing resync/recovery and cannot"
1564 " be reshaped\n", devname);
1565 return 1;
1566 }
1567
1568 /* ========= set size =============== */
1569 if (size >= 0 && (size == 0 || size != array.size)) {
1570 long long orig_size = get_component_size(fd)/2;
1571 long long min_csize;
1572 struct mdinfo *mdi;
1573
1574 if (orig_size == 0)
1575 orig_size = array.size;
1576
1577 if (reshape_super(st, size, UnSet, UnSet, 0, 0, UnSet, NULL,
1578 devname, !quiet)) {
1579 rv = 1;
1580 goto release;
1581 }
1582 sync_metadata(st);
1583
1584 /* Update the size of each member device in case
1585 * they have been resized. This will never reduce
1586 * below the current used-size. The "size" attribute
1587 * understands '0' to mean 'max'.
1588 */
1589 min_csize = 0;
1590 for (mdi = sra->devs; mdi; mdi = mdi->next) {
1591 if (sysfs_set_num(sra, mdi, "size", size) < 0)
1592 break;
1593 if (array.not_persistent == 0 &&
1594 array.major_version == 0 &&
1595 get_linux_version() < 3001000) {
1596 /* Dangerous to allow size to exceed 2TB */
1597 unsigned long long csize;
1598 if (sysfs_get_ll(sra, mdi, "size", &csize) == 0) {
1599 if (csize >= 2ULL*1024*1024*1024)
1600 csize = 2ULL*1024*1024*1024;
1601 if ((min_csize == 0 || (min_csize
1602 > (long long)csize)))
1603 min_csize = csize;
1604 }
1605 }
1606 }
1607 if (min_csize && size > min_csize) {
1608 fprintf(stderr, Name ": Cannot safely make this array "
1609 "use more than 2TB per device on this kernel.\n");
1610 rv = 1;
1611 goto release;
1612 }
1613 if (min_csize && size == 0) {
1614 /* Don't let the kernel choose a size - it will get
1615 * it wrong
1616 */
1617 fprintf(stderr, Name ": Limited v0.90 array to "
1618 "2TB per device\n");
1619 size = min_csize;
1620 }
1621
1622 array.size = size;
1623 if (array.size != size) {
1624 /* got truncated to 32bit, write to
1625 * component_size instead
1626 */
1627 if (sra)
1628 rv = sysfs_set_num(sra, NULL,
1629 "component_size", size);
1630 else
1631 rv = -1;
1632 } else
1633 rv = ioctl(fd, SET_ARRAY_INFO, &array);
1634 if (rv != 0) {
1635 int err = errno;
1636
1637 /* restore metadata */
1638 if (reshape_super(st, orig_size, UnSet, UnSet, 0, 0,
1639 UnSet, NULL, devname, !quiet) == 0)
1640 sync_metadata(st);
1641 fprintf(stderr, Name ": Cannot set device size for %s: %s\n",
1642 devname, strerror(err));
1643 if (err == EBUSY &&
1644 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
1645 fprintf(stderr, " Bitmap must be removed before size can be changed\n");
1646 rv = 1;
1647 goto release;
1648 }
1649 if (assume_clean) {
1650 /* This will fail on kernels newer than 3.0 unless
1651 * a backport has been arranged.
1652 */
1653 if (sra == NULL ||
1654 sysfs_set_str(sra, NULL, "resync_start", "none") < 0)
1655 fprintf(stderr, Name ": --assume-clean not support with --grow on this kernel\n");
1656 }
1657 ioctl(fd, GET_ARRAY_INFO, &array);
1658 size = get_component_size(fd)/2;
1659 if (size == 0)
1660 size = array.size;
1661 if (!quiet) {
1662 if (size == orig_size)
1663 fprintf(stderr, Name ": component size of %s "
1664 "unchanged at %lluK\n",
1665 devname, size);
1666 else
1667 fprintf(stderr, Name ": component size of %s "
1668 "has been set to %lluK\n",
1669 devname, size);
1670 }
1671 changed = 1;
1672 } else if (array.level != LEVEL_CONTAINER) {
1673 size = get_component_size(fd)/2;
1674 if (size == 0)
1675 size = array.size;
1676 }
1677
1678 /* See if there is anything else to do */
1679 if ((level == UnSet || level == array.level) &&
1680 (layout_str == NULL) &&
1681 (chunksize == 0 || chunksize == array.chunk_size) &&
1682 (raid_disks == 0 || raid_disks == array.raid_disks)) {
1683 /* Nothing more to do */
1684 if (!changed && !quiet)
1685 fprintf(stderr, Name ": %s: no change requested\n",
1686 devname);
1687 goto release;
1688 }
1689
1690 /* ========= check for Raid10/Raid1 -> Raid0 conversion ===============
1691 * current implementation assumes that following conditions must be met:
1692 * - RAID10:
1693 * - far_copies == 1
1694 * - near_copies == 2
1695 */
1696 if ((level == 0 && array.level == 10 && sra &&
1697 array.layout == ((1 << 8) + 2) && !(array.raid_disks & 1)) ||
1698 (level == 0 && array.level == 1 && sra)) {
1699 int err;
1700 err = remove_disks_for_takeover(st, sra, array.layout);
1701 if (err) {
1702 dprintf(Name": Array cannot be reshaped\n");
1703 if (cfd > -1)
1704 close(cfd);
1705 rv = 1;
1706 goto release;
1707 }
1708 /* Make sure mdmon has seen the device removal
1709 * and updated metadata before we continue with
1710 * level change
1711 */
1712 if (container)
1713 ping_monitor(container);
1714 }
1715
1716 memset(&info, 0, sizeof(info));
1717 info.array = array;
1718 sysfs_init(&info, fd, NoMdDev);
1719 strcpy(info.text_version, sra->text_version);
1720 info.component_size = size*2;
1721 info.new_level = level;
1722 info.new_chunk = chunksize * 1024;
1723 if (info.array.level == LEVEL_CONTAINER) {
1724 info.delta_disks = UnSet;
1725 info.array.raid_disks = raid_disks;
1726 } else if (raid_disks)
1727 info.delta_disks = raid_disks - info.array.raid_disks;
1728 else
1729 info.delta_disks = UnSet;
1730 if (layout_str == NULL) {
1731 info.new_layout = UnSet;
1732 if (info.array.level == 6 &&
1733 (info.new_level == 6 || info.new_level == UnSet) &&
1734 info.array.layout >= 16) {
1735 fprintf(stderr, Name
1736 ": %s has a non-standard layout. If you"
1737 " wish to preserve this\n"
1738 " during the reshape, please specify"
1739 " --layout=preserve\n"
1740 " If you want to change it, specify a"
1741 " layout or use --layout=normalise\n",
1742 devname);
1743 rv = 1;
1744 goto release;
1745 }
1746 } else if (strcmp(layout_str, "normalise") == 0 ||
1747 strcmp(layout_str, "normalize") == 0) {
1748 /* If we have a -6 RAID6 layout, remove the '-6'. */
1749 info.new_layout = UnSet;
1750 if (info.array.level == 6 && info.new_level == UnSet) {
1751 char l[40], *h;
1752 strcpy(l, map_num(r6layout, info.array.layout));
1753 h = strrchr(l, '-');
1754 if (h && strcmp(h, "-6") == 0) {
1755 *h = 0;
1756 info.new_layout = map_name(r6layout, l);
1757 }
1758 }
1759 } else if (strcmp(layout_str, "preserve") == 0) {
1760 info.new_layout = UnSet;
1761 } else {
1762 int l = info.new_level;
1763 if (l == UnSet)
1764 l = info.array.level;
1765 switch (l) {
1766 case 5:
1767 info.new_layout = map_name(r5layout, layout_str);
1768 break;
1769 case 6:
1770 info.new_layout = map_name(r6layout, layout_str);
1771 break;
1772 case 10:
1773 info.new_layout = parse_layout_10(layout_str);
1774 break;
1775 case LEVEL_FAULTY:
1776 info.new_layout = parse_layout_faulty(layout_str);
1777 break;
1778 default:
1779 fprintf(stderr, Name ": layout not meaningful"
1780 " with this level\n");
1781 rv = 1;
1782 goto release;
1783 }
1784 if (info.new_layout == UnSet) {
1785 fprintf(stderr, Name ": layout %s not understood"
1786 " for this level\n",
1787 layout_str);
1788 rv = 1;
1789 goto release;
1790 }
1791 }
1792
1793 if (array.level == LEVEL_FAULTY) {
1794 if (level != UnSet && level != array.level) {
1795 fprintf(stderr, Name ": cannot change level of Faulty device\n");
1796 rv =1 ;
1797 }
1798 if (chunksize) {
1799 fprintf(stderr, Name ": cannot set chunksize of Faulty device\n");
1800 rv =1 ;
1801 }
1802 if (raid_disks && raid_disks != 1) {
1803 fprintf(stderr, Name ": cannot set raid_disks of Faulty device\n");
1804 rv =1 ;
1805 }
1806 if (layout_str) {
1807 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
1808 dprintf("Cannot get array information.\n");
1809 goto release;
1810 }
1811 array.layout = info.new_layout;
1812 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
1813 fprintf(stderr, Name ": failed to set new layout\n");
1814 rv = 1;
1815 } else if (!quiet)
1816 printf("layout for %s set to %d\n",
1817 devname, array.layout);
1818 }
1819 } else if (array.level == LEVEL_CONTAINER) {
1820 /* This change is to be applied to every array in the
1821 * container. This is only needed when the metadata imposes
1822 * restraints of the various arrays in the container.
1823 * Currently we only know that IMSM requires all arrays
1824 * to have the same number of devices so changing the
1825 * number of devices (On-Line Capacity Expansion) must be
1826 * performed at the level of the container
1827 */
1828 rv = reshape_container(container, devname, -1, st, &info,
1829 force, backup_file, quiet, 0, 0);
1830 frozen = 0;
1831 } else {
1832 /* get spare devices from external metadata
1833 */
1834 if (st->ss->external) {
1835 struct mdinfo *info2;
1836
1837 info2 = st->ss->container_content(st, subarray);
1838 if (info2) {
1839 info.array.spare_disks =
1840 info2->array.spare_disks;
1841 sysfs_free(info2);
1842 }
1843 }
1844
1845 /* Impose these changes on a single array. First
1846 * check that the metadata is OK with the change. */
1847
1848 if (reshape_super(st, info.component_size, info.new_level,
1849 info.new_layout, info.new_chunk,
1850 info.array.raid_disks, info.delta_disks,
1851 backup_file, devname, quiet)) {
1852 rv = 1;
1853 goto release;
1854 }
1855 sync_metadata(st);
1856 rv = reshape_array(container, fd, devname, st, &info, force,
1857 devlist, backup_file, quiet, 0, 0, 0);
1858 frozen = 0;
1859 }
1860 release:
1861 if (frozen > 0)
1862 unfreeze(st);
1863 return rv;
1864 }
1865
1866 static int reshape_array(char *container, int fd, char *devname,
1867 struct supertype *st, struct mdinfo *info,
1868 int force, struct mddev_dev *devlist,
1869 char *backup_file, int quiet, int forked,
1870 int restart, int freeze_reshape)
1871 {
1872 struct reshape reshape;
1873 int spares_needed;
1874 char *msg;
1875 int orig_level = UnSet;
1876 int disks, odisks;
1877
1878 struct mdu_array_info_s array;
1879 char *c;
1880
1881 struct mddev_dev *dv;
1882 int added_disks;
1883
1884 int *fdlist = NULL;
1885 unsigned long long *offsets = NULL;
1886 int d;
1887 int nrdisks;
1888 int err;
1889 unsigned long blocks;
1890 unsigned long cache;
1891 unsigned long long array_size;
1892 int done;
1893 struct mdinfo *sra = NULL;
1894
1895 /* when reshaping a RAID0, the component_size might be zero.
1896 * So try to fix that up.
1897 */
1898 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
1899 dprintf("Cannot get array information.\n");
1900 goto release;
1901 }
1902 if (array.level == 0 && info->component_size == 0) {
1903 get_dev_size(fd, NULL, &array_size);
1904 info->component_size = array_size / array.raid_disks;
1905 }
1906
1907 if (info->reshape_active) {
1908 int new_level = info->new_level;
1909 info->new_level = UnSet;
1910 info->array.raid_disks -= info->delta_disks;
1911 msg = analyse_change(info, &reshape);
1912 info->new_level = new_level;
1913 info->array.raid_disks += info->delta_disks;
1914 if (!restart)
1915 /* Make sure the array isn't read-only */
1916 ioctl(fd, RESTART_ARRAY_RW, 0);
1917 } else
1918 msg = analyse_change(info, &reshape);
1919 if (msg) {
1920 fprintf(stderr, Name ": %s\n", msg);
1921 goto release;
1922 }
1923 if (restart &&
1924 (reshape.level != info->array.level ||
1925 reshape.before.layout != info->array.layout ||
1926 reshape.before.data_disks + reshape.parity
1927 != info->array.raid_disks - info->delta_disks)) {
1928 fprintf(stderr, Name ": reshape info is not in native format -"
1929 " cannot continue.\n");
1930 goto release;
1931 }
1932
1933 if (restart) {
1934 /* reshape already started. just skip to monitoring the reshape */
1935 if (reshape.backup_blocks == 0)
1936 return 0;
1937 goto started;
1938 }
1939 /* The container is frozen but the array may not be.
1940 * So freeze the array so spares don't get put to the wrong use
1941 * FIXME there should probably be a cleaner separation between
1942 * freeze_array and freeze_container.
1943 */
1944 sysfs_freeze_array(info);
1945 /* Check we have enough spares to not be degraded */
1946 added_disks = 0;
1947 for (dv = devlist; dv ; dv=dv->next)
1948 added_disks++;
1949 spares_needed = max(reshape.before.data_disks,
1950 reshape.after.data_disks)
1951 + reshape.parity - array.raid_disks;
1952
1953 if (!force &&
1954 info->new_level > 1 && info->array.level > 1 &&
1955 spares_needed > info->array.spare_disks + added_disks) {
1956 fprintf(stderr,
1957 Name ": Need %d spare%s to avoid degraded array,"
1958 " and only have %d.\n"
1959 " Use --force to over-ride this check.\n",
1960 spares_needed,
1961 spares_needed == 1 ? "" : "s",
1962 info->array.spare_disks + added_disks);
1963 goto release;
1964 }
1965 /* Check we have enough spares to not fail */
1966 spares_needed = max(reshape.before.data_disks,
1967 reshape.after.data_disks)
1968 - array.raid_disks;
1969 if ((info->new_level > 1 || info->new_level == 0) &&
1970 spares_needed > info->array.spare_disks +added_disks) {
1971 fprintf(stderr,
1972 Name ": Need %d spare%s to create working array,"
1973 " and only have %d.\n",
1974 spares_needed,
1975 spares_needed == 1 ? "" : "s",
1976 info->array.spare_disks + added_disks);
1977 goto release;
1978 }
1979
1980 if (reshape.level != array.level) {
1981 char *c = map_num(pers, reshape.level);
1982 int err;
1983 if (c == NULL)
1984 goto release;
1985
1986 err = sysfs_set_str(info, NULL, "level", c);
1987 if (err) {
1988 err = errno;
1989 fprintf(stderr, Name ": %s: could not set level to %s\n",
1990 devname, c);
1991 if (err == EBUSY &&
1992 (info->array.state & (1<<MD_SB_BITMAP_PRESENT)))
1993 fprintf(stderr, " Bitmap must be removed"
1994 " before level can be changed\n");
1995 goto release;
1996 }
1997 if (!quiet)
1998 fprintf(stderr, Name ": level of %s changed to %s\n",
1999 devname, c);
2000 orig_level = array.level;
2001 sysfs_freeze_array(info);
2002
2003 if (reshape.level > 0 && st->ss->external) {
2004 /* make sure mdmon is aware of the new level */
2005 if (!mdmon_running(st->container_dev))
2006 start_mdmon(st->container_dev);
2007 ping_monitor(container);
2008 if (mdmon_running(st->container_dev) &&
2009 st->update_tail == NULL)
2010 st->update_tail = &st->updates;
2011 }
2012 }
2013 /* ->reshape_super might have chosen some spares from the
2014 * container that it wants to be part of the new array.
2015 * We can collect them with ->container_content and give
2016 * them to the kernel.
2017 */
2018 if (st->ss->reshape_super && st->ss->container_content) {
2019 char *subarray = strchr(info->text_version+1, '/')+1;
2020 struct mdinfo *info2 =
2021 st->ss->container_content(st, subarray);
2022 struct mdinfo *d;
2023
2024 if (info2) {
2025 sysfs_init(info2, fd, st->devnum);
2026 /* When increasing number of devices, we need to set
2027 * new raid_disks before adding these, or they might
2028 * be rejected.
2029 */
2030 if (reshape.backup_blocks &&
2031 reshape.after.data_disks > reshape.before.data_disks)
2032 subarray_set_num(container, info2, "raid_disks",
2033 reshape.after.data_disks +
2034 reshape.parity);
2035 for (d = info2->devs; d; d = d->next) {
2036 if (d->disk.state == 0 &&
2037 d->disk.raid_disk >= 0) {
2038 /* This is a spare that wants to
2039 * be part of the array.
2040 */
2041 add_disk(fd, st, info2, d);
2042 }
2043 }
2044 sysfs_free(info2);
2045 }
2046 }
2047 /* We might have been given some devices to add to the
2048 * array. Now that the array has been changed to the right
2049 * level and frozen, we can safely add them.
2050 */
2051 if (devlist)
2052 Manage_subdevs(devname, fd, devlist, !quiet,
2053 0,NULL, 0);
2054
2055 if (reshape.backup_blocks == 0) {
2056 /* No restriping needed, but we might need to impose
2057 * some more changes: layout, raid_disks, chunk_size
2058 */
2059 /* read current array info */
2060 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
2061 dprintf("Cannot get array information.\n");
2062 goto release;
2063 }
2064 /* compare current array info with new values and if
2065 * it is different update them to new */
2066 if (info->new_layout != UnSet &&
2067 info->new_layout != array.layout) {
2068 array.layout = info->new_layout;
2069 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
2070 fprintf(stderr, Name ": failed to set new layout\n");
2071 goto release;
2072 } else if (!quiet)
2073 printf("layout for %s set to %d\n",
2074 devname, array.layout);
2075 }
2076 if (info->delta_disks != UnSet &&
2077 info->delta_disks != 0 &&
2078 array.raid_disks != (info->array.raid_disks + info->delta_disks)) {
2079 array.raid_disks += info->delta_disks;
2080 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
2081 fprintf(stderr, Name ": failed to set raid disks\n");
2082 goto release;
2083 } else if (!quiet) {
2084 printf("raid_disks for %s set to %d\n",
2085 devname, array.raid_disks);
2086 }
2087 }
2088 if (info->new_chunk != 0 &&
2089 info->new_chunk != array.chunk_size) {
2090 if (sysfs_set_num(info, NULL,
2091 "chunk_size", info->new_chunk) != 0) {
2092 fprintf(stderr, Name ": failed to set chunk size\n");
2093 goto release;
2094 } else if (!quiet)
2095 printf("chunk size for %s set to %d\n",
2096 devname, array.chunk_size);
2097 }
2098 unfreeze(st);
2099 return 0;
2100 }
2101
2102 /*
2103 * There are three possibilities.
2104 * 1/ The array will shrink.
2105 * We need to ensure the reshape will pause before reaching
2106 * the 'critical section'. We also need to fork and wait for
2107 * that to happen. When it does we
2108 * suspend/backup/complete/unfreeze
2109 *
2110 * 2/ The array will not change size.
2111 * This requires that we keep a backup of a sliding window
2112 * so that we can restore data after a crash. So we need
2113 * to fork and monitor progress.
2114 * In future we will allow the data_offset to change, so
2115 * a sliding backup becomes unnecessary.
2116 *
2117 * 3/ The array will grow. This is relatively easy.
2118 * However the kernel's restripe routines will cheerfully
2119 * overwrite some early data before it is safe. So we
2120 * need to make a backup of the early parts of the array
2121 * and be ready to restore it if rebuild aborts very early.
2122 * For externally managed metadata, we still need a forked
2123 * child to monitor the reshape and suspend IO over the region
2124 * that is being reshaped.
2125 *
2126 * We backup data by writing it to one spare, or to a
2127 * file which was given on command line.
2128 *
2129 * In each case, we first make sure that storage is available
2130 * for the required backup.
2131 * Then we:
2132 * - request the shape change.
2133 * - fork to handle backup etc.
2134 */
2135 started:
2136 /* Check that we can hold all the data */
2137 get_dev_size(fd, NULL, &array_size);
2138 if (reshape.new_size < (array_size/512)) {
2139 fprintf(stderr,
2140 Name ": this change will reduce the size of the array.\n"
2141 " use --grow --array-size first to truncate array.\n"
2142 " e.g. mdadm --grow %s --array-size %llu\n",
2143 devname, reshape.new_size/2);
2144 goto release;
2145 }
2146
2147 sra = sysfs_read(fd, 0,
2148 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK|
2149 GET_CACHE);
2150 if (!sra) {
2151 fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n",
2152 devname);
2153 goto release;
2154 }
2155
2156 /* Decide how many blocks (sectors) for a reshape
2157 * unit. The number we have so far is just a minimum
2158 */
2159 blocks = reshape.backup_blocks;
2160 if (reshape.before.data_disks ==
2161 reshape.after.data_disks) {
2162 /* Make 'blocks' bigger for better throughput, but
2163 * not so big that we reject it below.
2164 * Try for 16 megabytes
2165 */
2166 while (blocks * 32 < sra->component_size &&
2167 blocks < 16*1024*2)
2168 blocks *= 2;
2169 } else
2170 fprintf(stderr, Name ": Need to backup %luK of critical "
2171 "section..\n", blocks/2);
2172
2173 if (blocks >= sra->component_size/2) {
2174 fprintf(stderr, Name ": %s: Something wrong"
2175 " - reshape aborted\n",
2176 devname);
2177 goto release;
2178 }
2179
2180 /* Now we need to open all these devices so we can read/write.
2181 */
2182 nrdisks = max(reshape.before.data_disks,
2183 reshape.after.data_disks) + reshape.parity
2184 + sra->array.spare_disks;
2185 fdlist = malloc((1+nrdisks) * sizeof(int));
2186 offsets = malloc((1+nrdisks) * sizeof(offsets[0]));
2187 if (!fdlist || !offsets) {
2188 fprintf(stderr, Name ": malloc failed: grow aborted\n");
2189 goto release;
2190 }
2191
2192 odisks = reshape.before.data_disks + reshape.parity;
2193 d = reshape_prepare_fdlist(devname, sra, odisks,
2194 nrdisks, blocks, backup_file,
2195 fdlist, offsets);
2196 if (d < 0) {
2197 goto release;
2198 }
2199 if ((st->ss->manage_reshape == NULL) ||
2200 (st->ss->recover_backup == NULL)) {
2201 if (backup_file == NULL) {
2202 if (reshape.after.data_disks <=
2203 reshape.before.data_disks) {
2204 fprintf(stderr, Name ": %s: Cannot grow - "
2205 "need backup-file\n", devname);
2206 goto release;
2207 } else if (sra->array.spare_disks == 0) {
2208 fprintf(stderr, Name ": %s: Cannot grow - "
2209 "need a spare or backup-file to backup "
2210 "critical section\n", devname);
2211 goto release;
2212 }
2213 } else {
2214 if (!reshape_open_backup_file(backup_file, fd, devname,
2215 (signed)blocks,
2216 fdlist+d, offsets+d,
2217 restart)) {
2218 goto release;
2219 }
2220 d++;
2221 }
2222 }
2223
2224 /* lastly, check that the internal stripe cache is
2225 * large enough, or it won't work.
2226 * It must hold at least 4 stripes of the larger
2227 * chunk size
2228 */
2229 cache = max(info->array.chunk_size, info->new_chunk);
2230 cache *= 4; /* 4 stripes minimum */
2231 cache /= 512; /* convert to sectors */
2232 disks = min(reshape.before.data_disks, reshape.after.data_disks);
2233 /* make sure there is room for 'blocks' with a bit to spare */
2234 if (cache < 16 + blocks / disks)
2235 cache = 16 + blocks / disks;
2236 cache /= (4096/512); /* Covert from sectors to pages */
2237
2238 if (sra->cache_size < cache)
2239 subarray_set_num(container, sra, "stripe_cache_size",
2240 cache+1);
2241
2242 /* Right, everything seems fine. Let's kick things off.
2243 * If only changing raid_disks, use ioctl, else use
2244 * sysfs.
2245 */
2246 sync_metadata(st);
2247
2248 sra->new_chunk = info->new_chunk;
2249
2250 if (restart)
2251 sra->reshape_progress = info->reshape_progress;
2252 else {
2253 sra->reshape_progress = 0;
2254 if (reshape.after.data_disks < reshape.before.data_disks)
2255 /* start from the end of the new array */
2256 sra->reshape_progress = (sra->component_size
2257 * reshape.after.data_disks);
2258 }
2259
2260 if (info->array.chunk_size == info->new_chunk &&
2261 reshape.before.layout == reshape.after.layout &&
2262 st->ss->external == 0) {
2263 /* use SET_ARRAY_INFO but only if reshape hasn't started */
2264 ioctl(fd, GET_ARRAY_INFO, &array);
2265 array.raid_disks = reshape.after.data_disks + reshape.parity;
2266 if (!restart &&
2267 ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
2268 int err = errno;
2269
2270 fprintf(stderr,
2271 Name ": Cannot set device shape for %s: %s\n",
2272 devname, strerror(errno));
2273
2274 if (err == EBUSY &&
2275 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
2276 fprintf(stderr,
2277 " Bitmap must be removed before"
2278 " shape can be changed\n");
2279
2280 goto release;
2281 }
2282 } else if (!restart) {
2283 /* set them all just in case some old 'new_*' value
2284 * persists from some earlier problem.
2285 */
2286 int err = 0;
2287 if (sysfs_set_num(sra, NULL, "chunk_size", info->new_chunk) < 0)
2288 err = errno;
2289 if (!err && sysfs_set_num(sra, NULL, "layout",
2290 reshape.after.layout) < 0)
2291 err = errno;
2292 if (!err && subarray_set_num(container, sra, "raid_disks",
2293 reshape.after.data_disks +
2294 reshape.parity) < 0)
2295 err = errno;
2296 if (err) {
2297 fprintf(stderr, Name ": Cannot set device shape for %s\n",
2298 devname);
2299
2300 if (err == EBUSY &&
2301 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
2302 fprintf(stderr,
2303 " Bitmap must be removed before"
2304 " shape can be changed\n");
2305 goto release;
2306 }
2307 }
2308
2309 err = start_reshape(sra, restart,
2310 info->array.raid_disks - reshape.parity);
2311 if (err) {
2312 fprintf(stderr,
2313 Name ": Cannot %s reshape for %s\n",
2314 restart ? "continue" : "start",
2315 devname);
2316 goto release;
2317 }
2318 if (restart)
2319 sysfs_set_str(sra, NULL, "array_state", "active");
2320 if (freeze_reshape) {
2321 free(fdlist);
2322 free(offsets);
2323 sysfs_free(sra);
2324 fprintf(stderr, Name ": Reshape has to be continued from"
2325 " location %llu when root fileststem has been mounted\n",
2326 sra->reshape_progress);
2327 return 1;
2328 }
2329
2330 /* Now we just need to kick off the reshape and watch, while
2331 * handling backups of the data...
2332 * This is all done by a forked background process.
2333 */
2334 switch(forked ? 0 : fork()) {
2335 case -1:
2336 fprintf(stderr, Name ": Cannot run child to monitor reshape: %s\n",
2337 strerror(errno));
2338 abort_reshape(sra);
2339 goto release;
2340 default:
2341 free(fdlist);
2342 free(offsets);
2343 sysfs_free(sra);
2344 return 0;
2345 case 0:
2346 map_fork();
2347 break;
2348 }
2349
2350 close(fd);
2351 if (check_env("MDADM_GROW_VERIFY"))
2352 fd = open(devname, O_RDONLY | O_DIRECT);
2353 else
2354 fd = -1;
2355 mlockall(MCL_FUTURE);
2356
2357 if (st->ss->external) {
2358 /* metadata handler takes it from here */
2359 done = st->ss->manage_reshape(
2360 fd, sra, &reshape, st, blocks,
2361 fdlist, offsets,
2362 d - odisks, fdlist+odisks,
2363 offsets+odisks);
2364 } else
2365 done = child_monitor(
2366 fd, sra, &reshape, st, blocks,
2367 fdlist, offsets,
2368 d - odisks, fdlist+odisks,
2369 offsets+odisks);
2370
2371 free(fdlist);
2372 free(offsets);
2373
2374 if (backup_file && done)
2375 unlink(backup_file);
2376 if (!done) {
2377 abort_reshape(sra);
2378 goto out;
2379 }
2380
2381 if (!st->ss->external &&
2382 !(reshape.before.data_disks != reshape.after.data_disks
2383 && info->custom_array_size) &&
2384 info->new_level == reshape.level &&
2385 !forked) {
2386 /* no need to wait for the reshape to finish as
2387 * there is nothing more to do.
2388 */
2389 sysfs_free(sra);
2390 exit(0);
2391 }
2392 wait_reshape(sra);
2393
2394 if (st->ss->external) {
2395 /* Re-load the metadata as much could have changed */
2396 int cfd = open_dev(st->container_dev);
2397 if (cfd >= 0) {
2398 ping_monitor(container);
2399 st->ss->free_super(st);
2400 st->ss->load_container(st, cfd, container);
2401 close(cfd);
2402 }
2403 }
2404
2405 /* set new array size if required customer_array_size is used
2406 * by this metadata.
2407 */
2408 if (reshape.before.data_disks !=
2409 reshape.after.data_disks &&
2410 info->custom_array_size) {
2411 struct mdinfo *info2;
2412 char *subarray = strchr(info->text_version+1, '/')+1;
2413
2414 info2 = st->ss->container_content(st, subarray);
2415 if (info2) {
2416 unsigned long long current_size = 0;
2417 unsigned long long new_size =
2418 info2->custom_array_size/2;
2419
2420 if (sysfs_get_ll(sra,
2421 NULL,
2422 "array_size",
2423 &current_size) == 0 &&
2424 new_size > current_size) {
2425 if (sysfs_set_num(sra, NULL,
2426 "array_size", new_size)
2427 < 0)
2428 dprintf("Error: Cannot"
2429 " set array size");
2430 else
2431 dprintf("Array size "
2432 "changed");
2433 dprintf(" from %llu to %llu.\n",
2434 current_size, new_size);
2435 }
2436 sysfs_free(info2);
2437 }
2438 }
2439
2440 if (info->new_level != reshape.level) {
2441
2442 c = map_num(pers, info->new_level);
2443 if (c) {
2444 err = sysfs_set_str(sra, NULL, "level", c);
2445 if (err)
2446 fprintf(stderr, Name\
2447 ": %s: could not set level "
2448 "to %s\n", devname, c);
2449 }
2450 if (info->new_level == 0)
2451 st->update_tail = NULL;
2452 }
2453 out:
2454 sysfs_free(sra);
2455 if (forked)
2456 return 0;
2457 unfreeze(st);
2458 exit(0);
2459
2460 release:
2461 free(fdlist);
2462 free(offsets);
2463 if (orig_level != UnSet && sra) {
2464 c = map_num(pers, orig_level);
2465 if (c && sysfs_set_str(sra, NULL, "level", c) == 0)
2466 fprintf(stderr, Name ": aborting level change\n");
2467 }
2468 sysfs_free(sra);
2469 if (!forked)
2470 unfreeze(st);
2471 return 1;
2472 }
2473
2474 /* mdfd handle is passed to be closed in child process (after fork).
2475 */
2476 int reshape_container(char *container, char *devname,
2477 int mdfd,
2478 struct supertype *st,
2479 struct mdinfo *info,
2480 int force,
2481 char *backup_file,
2482 int quiet, int restart, int freeze_reshape)
2483 {
2484 struct mdinfo *cc = NULL;
2485 int rv = restart;
2486
2487 /* component_size is not meaningful for a container,
2488 * so pass '-1' meaning 'no change'
2489 */
2490 if (!restart &&
2491 reshape_super(st, -1, info->new_level,
2492 info->new_layout, info->new_chunk,
2493 info->array.raid_disks, info->delta_disks,
2494 backup_file, devname, quiet)) {
2495 unfreeze(st);
2496 return 1;
2497 }
2498
2499 sync_metadata(st);
2500
2501 /* ping monitor to be sure that update is on disk
2502 */
2503 ping_monitor(container);
2504
2505 switch (fork()) {
2506 case -1: /* error */
2507 perror("Cannot fork to complete reshape\n");
2508 unfreeze(st);
2509 return 1;
2510 default: /* parent */
2511 if (!freeze_reshape)
2512 printf(Name ": multi-array reshape continues"
2513 " in background\n");
2514 return 0;
2515 case 0: /* child */
2516 map_fork();
2517 break;
2518 }
2519
2520 /* close unused handle in child process
2521 */
2522 if (mdfd > -1)
2523 close(mdfd);
2524
2525 while(1) {
2526 /* For each member array with reshape_active,
2527 * we need to perform the reshape.
2528 * We pick the first array that needs reshaping and
2529 * reshape it. reshape_array() will re-read the metadata
2530 * so the next time through a different array should be
2531 * ready for reshape.
2532 * It is possible that the 'different' array will not
2533 * be assembled yet. In that case we simple exit.
2534 * When it is assembled, the mdadm which assembles it
2535 * will take over the reshape.
2536 */
2537 struct mdinfo *content;
2538 int fd;
2539 struct mdstat_ent *mdstat;
2540 char *adev;
2541
2542 sysfs_free(cc);
2543
2544 cc = st->ss->container_content(st, NULL);
2545
2546 for (content = cc; content ; content = content->next) {
2547 char *subarray;
2548 if (!content->reshape_active)
2549 continue;
2550
2551 subarray = strchr(content->text_version+1, '/')+1;
2552 mdstat = mdstat_by_subdev(subarray,
2553 devname2devnum(container));
2554 if (!mdstat)
2555 continue;
2556 break;
2557 }
2558 if (!content)
2559 break;
2560
2561 fd = open_dev(mdstat->devnum);
2562 if (fd < 0)
2563 break;
2564 adev = map_dev(dev2major(mdstat->devnum),
2565 dev2minor(mdstat->devnum),
2566 0);
2567 if (!adev)
2568 adev = content->text_version;
2569
2570 sysfs_init(content, fd, mdstat->devnum);
2571
2572 rv = reshape_array(container, fd, adev, st,
2573 content, force, NULL,
2574 backup_file, quiet, 1, restart,
2575 freeze_reshape);
2576 close(fd);
2577
2578 if (freeze_reshape) {
2579 sysfs_free(cc);
2580 exit(0);
2581 }
2582
2583 restart = 0;
2584 if (rv)
2585 break;
2586 }
2587 if (!rv)
2588 unfreeze(st);
2589 sysfs_free(cc);
2590 exit(0);
2591 }
2592
2593 /*
2594 * We run a child process in the background which performs the following
2595 * steps:
2596 * - wait for resync to reach a certain point
2597 * - suspend io to the following section
2598 * - backup that section
2599 * - allow resync to proceed further
2600 * - resume io
2601 * - discard the backup.
2602 *
2603 * When are combined in slightly different ways in the three cases.
2604 * Grow:
2605 * - suspend/backup/allow/wait/resume/discard
2606 * Shrink:
2607 * - allow/wait/suspend/backup/allow/wait/resume/discard
2608 * same-size:
2609 * - wait/resume/discard/suspend/backup/allow
2610 *
2611 * suspend/backup/allow always come together
2612 * wait/resume/discard do too.
2613 * For the same-size case we have two backups to improve flow.
2614 *
2615 */
2616
2617 int progress_reshape(struct mdinfo *info, struct reshape *reshape,
2618 unsigned long long backup_point,
2619 unsigned long long wait_point,
2620 unsigned long long *suspend_point,
2621 unsigned long long *reshape_completed)
2622 {
2623 /* This function is called repeatedly by the reshape manager.
2624 * It determines how much progress can safely be made and allows
2625 * that progress.
2626 * - 'info' identifies the array and particularly records in
2627 * ->reshape_progress the metadata's knowledge of progress
2628 * This is a sector offset from the start of the array
2629 * of the next array block to be relocated. This number
2630 * may increase from 0 or decrease from array_size, depending
2631 * on the type of reshape that is happening.
2632 * Note that in contrast, 'sync_completed' is a block count of the
2633 * reshape so far. It gives the distance between the start point
2634 * (head or tail of device) and the next place that data will be
2635 * written. It always increases.
2636 * - 'reshape' is the structure created by analyse_change
2637 * - 'backup_point' shows how much the metadata manager has backed-up
2638 * data. For reshapes with increasing progress, it is the next address
2639 * to be backed up, previous addresses have been backed-up. For
2640 * decreasing progress, it is the earliest address that has been
2641 * backed up - later address are also backed up.
2642 * So addresses between reshape_progress and backup_point are
2643 * backed up providing those are in the 'correct' order.
2644 * - 'wait_point' is an array address. When reshape_completed
2645 * passes this point, progress_reshape should return. It might
2646 * return earlier if it determines that ->reshape_progress needs
2647 * to be updated or further backup is needed.
2648 * - suspend_point is maintained by progress_reshape and the caller
2649 * should not touch it except to initialise to zero.
2650 * It is an array address and it only increases in 2.6.37 and earlier.
2651 * This makes it difficult to handle reducing reshapes with
2652 * external metadata.
2653 * However: it is similar to backup_point in that it records the
2654 * other end of a suspended region from reshape_progress.
2655 * it is moved to extend the region that is safe to backup and/or
2656 * reshape
2657 * - reshape_completed is read from sysfs and returned. The caller
2658 * should copy this into ->reshape_progress when it has reason to
2659 * believe that the metadata knows this, and any backup outside this
2660 * has been erased.
2661 *
2662 * Return value is:
2663 * 1 if more data from backup_point - but only as far as suspend_point,
2664 * should be backed up
2665 * 0 if things are progressing smoothly
2666 * -1 if the reshape is finished because it is all done,
2667 * -2 if the reshape is finished due to an error.
2668 */
2669
2670 int advancing = (reshape->after.data_disks
2671 >= reshape->before.data_disks);
2672 unsigned long long need_backup; /* All data between start of array and
2673 * here will at some point need to
2674 * be backed up.
2675 */
2676 unsigned long long read_offset, write_offset;
2677 unsigned long long write_range;
2678 unsigned long long max_progress, target, completed;
2679 unsigned long long array_size = (info->component_size
2680 * reshape->before.data_disks);
2681 int fd;
2682 char buf[20];
2683
2684 /* First, we unsuspend any region that is now known to be safe.
2685 * If suspend_point is on the 'wrong' side of reshape_progress, then
2686 * we don't have or need suspension at the moment. This is true for
2687 * native metadata when we don't need to back-up.
2688 */
2689 if (advancing) {
2690 if (info->reshape_progress <= *suspend_point)
2691 sysfs_set_num(info, NULL, "suspend_lo",
2692 info->reshape_progress);
2693 } else {
2694 /* Note: this won't work in 2.6.37 and before.
2695 * Something somewhere should make sure we don't need it!
2696 */
2697 if (info->reshape_progress >= *suspend_point)
2698 sysfs_set_num(info, NULL, "suspend_hi",
2699 info->reshape_progress);
2700 }
2701
2702 /* Now work out how far it is safe to progress.
2703 * If the read_offset for ->reshape_progress is less than
2704 * 'blocks' beyond the write_offset, we can only progress as far
2705 * as a backup.
2706 * Otherwise we can progress until the write_offset for the new location
2707 * reaches (within 'blocks' of) the read_offset at the current location.
2708 * However that region must be suspended unless we are using native
2709 * metadata.
2710 * If we need to suspend more, we limit it to 128M per device, which is
2711 * rather arbitrary and should be some time-based calculation.
2712 */
2713 read_offset = info->reshape_progress / reshape->before.data_disks;
2714 write_offset = info->reshape_progress / reshape->after.data_disks;
2715 write_range = info->new_chunk/512;
2716 if (reshape->before.data_disks == reshape->after.data_disks)
2717 need_backup = array_size;
2718 else
2719 need_backup = reshape->backup_blocks;
2720 if (advancing) {
2721 if (read_offset < write_offset + write_range)
2722 max_progress = backup_point;
2723 else
2724 max_progress =
2725 read_offset *
2726 reshape->after.data_disks;
2727 } else {
2728 if (read_offset > write_offset - write_range)
2729 /* Can only progress as far as has been backed up,
2730 * which must be suspended */
2731 max_progress = backup_point;
2732 else if (info->reshape_progress <= need_backup)
2733 max_progress = backup_point;
2734 else {
2735 if (info->array.major_version >= 0)
2736 /* Can progress until backup is needed */
2737 max_progress = need_backup;
2738 else {
2739 /* Can progress until metadata update is required */
2740 max_progress =
2741 read_offset *
2742 reshape->after.data_disks;
2743 /* but data must be suspended */
2744 if (max_progress < *suspend_point)
2745 max_progress = *suspend_point;
2746 }
2747 }
2748 }
2749
2750 /* We know it is safe to progress to 'max_progress' providing
2751 * it is suspended or we are using native metadata.
2752 * Consider extending suspend_point 128M per device if it
2753 * is less than 64M per device beyond reshape_progress.
2754 * But always do a multiple of 'blocks'
2755 * FIXME this is too big - it takes to long to complete
2756 * this much.
2757 */
2758 target = 64*1024*2 * min(reshape->before.data_disks,
2759 reshape->after.data_disks);
2760 target /= reshape->backup_blocks;
2761 if (target < 2)
2762 target = 2;
2763 target *= reshape->backup_blocks;
2764
2765 /* For externally managed metadata we always need to suspend IO to
2766 * the area being reshaped so we regularly push suspend_point forward.
2767 * For native metadata we only need the suspend if we are going to do
2768 * a backup.
2769 */
2770 if (advancing) {
2771 if ((need_backup > info->reshape_progress
2772 || info->array.major_version < 0) &&
2773 *suspend_point < info->reshape_progress + target) {
2774 if (need_backup < *suspend_point + 2 * target)
2775 *suspend_point = need_backup;
2776 else if (*suspend_point + 2 * target < array_size)
2777 *suspend_point += 2 * target;
2778 else
2779 *suspend_point = array_size;
2780 sysfs_set_num(info, NULL, "suspend_hi", *suspend_point);
2781 if (max_progress > *suspend_point)
2782 max_progress = *suspend_point;
2783 }
2784 } else {
2785 if (info->array.major_version >= 0) {
2786 /* Only need to suspend when about to backup */
2787 if (info->reshape_progress < need_backup * 2 &&
2788 *suspend_point > 0) {
2789 *suspend_point = 0;
2790 sysfs_set_num(info, NULL, "suspend_lo", 0);
2791 sysfs_set_num(info, NULL, "suspend_hi", need_backup);
2792 }
2793 } else {
2794 /* Need to suspend continually */
2795 if (info->reshape_progress < *suspend_point)
2796 *suspend_point = info->reshape_progress;
2797 if (*suspend_point + target < info->reshape_progress)
2798 /* No need to move suspend region yet */;
2799 else {
2800 if (*suspend_point >= 2 * target)
2801 *suspend_point -= 2 * target;
2802 else
2803 *suspend_point = 0;
2804 sysfs_set_num(info, NULL, "suspend_lo",
2805 *suspend_point);
2806 }
2807 if (max_progress < *suspend_point)
2808 max_progress = *suspend_point;
2809 }
2810 }
2811
2812 /* now set sync_max to allow that progress. sync_max, like
2813 * sync_completed is a count of sectors written per device, so
2814 * we find the difference between max_progress and the start point,
2815 * and divide that by after.data_disks to get a sync_max
2816 * number.
2817 * At the same time we convert wait_point to a similar number
2818 * for comparing against sync_completed.
2819 */
2820 /* scale down max_progress to per_disk */
2821 max_progress /= reshape->after.data_disks;
2822 /* Round to chunk size as some kernels give an erroneously high number */
2823 max_progress /= info->new_chunk/512;
2824 max_progress *= info->new_chunk/512;
2825 /* And round to old chunk size as the kernel wants that */
2826 max_progress /= info->array.chunk_size/512;
2827 max_progress *= info->array.chunk_size/512;
2828 /* Limit progress to the whole device */
2829 if (max_progress > info->component_size)
2830 max_progress = info->component_size;
2831 wait_point /= reshape->after.data_disks;
2832 if (!advancing) {
2833 /* switch from 'device offset' to 'processed block count' */
2834 max_progress = info->component_size - max_progress;
2835 wait_point = info->component_size - wait_point;
2836 }
2837
2838 sysfs_set_num(info, NULL, "sync_max", max_progress);
2839
2840 /* Now wait. If we have already reached the point that we were
2841 * asked to wait to, don't wait at all, else wait for any change.
2842 * We need to select on 'sync_completed' as that is the place that
2843 * notifications happen, but we are really interested in
2844 * 'reshape_position'
2845 */
2846 fd = sysfs_get_fd(info, NULL, "sync_completed");
2847 if (fd < 0)
2848 goto check_progress;
2849
2850 if (sysfs_fd_get_ll(fd, &completed) < 0)
2851 goto check_progress;
2852
2853 while (completed < max_progress && completed < wait_point) {
2854 /* Check that sync_action is still 'reshape' to avoid
2855 * waiting forever on a dead array
2856 */
2857 char action[20];
2858 fd_set rfds;
2859 if (sysfs_get_str(info, NULL, "sync_action",
2860 action, 20) <= 0 ||
2861 strncmp(action, "reshape", 7) != 0)
2862 break;
2863 /* Some kernels reset 'sync_completed' to zero
2864 * before setting 'sync_action' to 'idle'.
2865 * So we need these extra tests.
2866 */
2867 if (completed == 0 && advancing
2868 && info->reshape_progress > 0)
2869 break;
2870 if (completed == 0 && !advancing
2871 && info->reshape_progress < (info->component_size
2872 * reshape->after.data_disks))
2873 break;
2874 FD_ZERO(&rfds);
2875 FD_SET(fd, &rfds);
2876 select(fd+1, NULL, NULL, &rfds, NULL);
2877 if (sysfs_fd_get_ll(fd, &completed) < 0)
2878 goto check_progress;
2879 }
2880 /* Some kernels reset 'sync_completed' to zero,
2881 * we need to have real point we are in md
2882 */
2883 if (completed == 0)
2884 completed = max_progress;
2885
2886 /* some kernels can give an incorrectly high 'completed' number */
2887 completed /= (info->new_chunk/512);
2888 completed *= (info->new_chunk/512);
2889 /* Convert 'completed' back in to a 'progress' number */
2890 completed *= reshape->after.data_disks;
2891 if (!advancing) {
2892 completed = info->component_size * reshape->after.data_disks
2893 - completed;
2894 }
2895 *reshape_completed = completed;
2896
2897 close(fd);
2898
2899 /* We return the need_backup flag. Caller will decide
2900 * how much - a multiple of ->backup_blocks up to *suspend_point
2901 */
2902 if (advancing)
2903 return need_backup > info->reshape_progress;
2904 else
2905 return need_backup >= info->reshape_progress;
2906
2907 check_progress:
2908 /* if we couldn't read a number from sync_completed, then
2909 * either the reshape did complete, or it aborted.
2910 * We can tell which by checking for 'none' in reshape_position.
2911 * If it did abort, then it might immediately restart if it
2912 * it was just a device failure that leaves us degraded but
2913 * functioning.
2914 */
2915 strcpy(buf, "hi");
2916 if (sysfs_get_str(info, NULL, "reshape_position", buf, sizeof(buf)) < 0
2917 || strncmp(buf, "none", 4) != 0) {
2918 /* The abort might only be temporary. Wait up to 10
2919 * seconds for fd to contain a valid number again.
2920 */
2921 struct timeval tv;
2922 int rv = -2;
2923 tv.tv_sec = 10;
2924 tv.tv_usec = 0;
2925 while (fd >= 0 && rv < 0 && tv.tv_sec > 0) {
2926 fd_set rfds;
2927 FD_ZERO(&rfds);
2928 FD_SET(fd, &rfds);
2929 if (select(fd+1, NULL, NULL, &rfds, &tv) != 1)
2930 break;
2931 switch (sysfs_fd_get_ll(fd, &completed)) {
2932 case 0:
2933 /* all good again */
2934 rv = 1;
2935 break;
2936 case -2: /* read error - abort */
2937 tv.tv_sec = 0;
2938 break;
2939 }
2940 }
2941 if (fd >= 0)
2942 close(fd);
2943 return rv; /* abort */
2944 } else {
2945 /* Maybe racing with array shutdown - check state */
2946 if (fd >= 0)
2947 close(fd);
2948 if (sysfs_get_str(info, NULL, "array_state", buf, sizeof(buf)) < 0
2949 || strncmp(buf, "inactive", 8) == 0
2950 || strncmp(buf, "clear",5) == 0)
2951 return -2; /* abort */
2952 return -1; /* complete */
2953 }
2954 }
2955
2956
2957 /* FIXME return status is never checked */
2958 static int grow_backup(struct mdinfo *sra,
2959 unsigned long long offset, /* per device */
2960 unsigned long stripes, /* per device, in old chunks */
2961 int *sources, unsigned long long *offsets,
2962 int disks, int chunk, int level, int layout,
2963 int dests, int *destfd, unsigned long long *destoffsets,
2964 int part, int *degraded,
2965 char *buf)
2966 {
2967 /* Backup 'blocks' sectors at 'offset' on each device of the array,
2968 * to storage 'destfd' (offset 'destoffsets'), after first
2969 * suspending IO. Then allow resync to continue
2970 * over the suspended section.
2971 * Use part 'part' of the backup-super-block.
2972 */
2973 int odata = disks;
2974 int rv = 0;
2975 int i;
2976 unsigned long long ll;
2977 int new_degraded;
2978 //printf("offset %llu\n", offset);
2979 if (level >= 4)
2980 odata--;
2981 if (level == 6)
2982 odata--;
2983
2984 /* Check that array hasn't become degraded, else we might backup the wrong data */
2985 if (sysfs_get_ll(sra, NULL, "degraded", &ll) < 0)
2986 return -1; /* FIXME this error is ignored */
2987 new_degraded = (int)ll;
2988 if (new_degraded != *degraded) {
2989 /* check each device to ensure it is still working */
2990 struct mdinfo *sd;
2991 for (sd = sra->devs ; sd ; sd = sd->next) {
2992 if (sd->disk.state & (1<<MD_DISK_FAULTY))
2993 continue;
2994 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
2995 char sbuf[20];
2996 if (sysfs_get_str(sra, sd, "state", sbuf, 20) < 0 ||
2997 strstr(sbuf, "faulty") ||
2998 strstr(sbuf, "in_sync") == NULL) {
2999 /* this device is dead */
3000 sd->disk.state = (1<<MD_DISK_FAULTY);
3001 if (sd->disk.raid_disk >= 0 &&
3002 sources[sd->disk.raid_disk] >= 0) {
3003 close(sources[sd->disk.raid_disk]);
3004 sources[sd->disk.raid_disk] = -1;
3005 }
3006 }
3007 }
3008 }
3009 *degraded = new_degraded;
3010 }
3011 if (part) {
3012 bsb.arraystart2 = __cpu_to_le64(offset * odata);
3013 bsb.length2 = __cpu_to_le64(stripes * (chunk/512) * odata);
3014 } else {
3015 bsb.arraystart = __cpu_to_le64(offset * odata);
3016 bsb.length = __cpu_to_le64(stripes * (chunk/512) * odata);
3017 }
3018 if (part)
3019 bsb.magic[15] = '2';
3020 for (i = 0; i < dests; i++)
3021 if (part)
3022 lseek64(destfd[i], destoffsets[i] + __le64_to_cpu(bsb.devstart2)*512, 0);
3023 else
3024 lseek64(destfd[i], destoffsets[i], 0);
3025
3026 rv = save_stripes(sources, offsets,
3027 disks, chunk, level, layout,
3028 dests, destfd,
3029 offset*512*odata, stripes * chunk * odata,
3030 buf);
3031
3032 if (rv)
3033 return rv;
3034 bsb.mtime = __cpu_to_le64(time(0));
3035 for (i = 0; i < dests; i++) {
3036 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
3037
3038 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
3039 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
3040 bsb.sb_csum2 = bsb_csum((char*)&bsb,
3041 ((char*)&bsb.sb_csum2)-((char*)&bsb));
3042
3043 rv = -1;
3044 if ((unsigned long long)lseek64(destfd[i], destoffsets[i] - 4096, 0)
3045 != destoffsets[i] - 4096)
3046 break;
3047 if (write(destfd[i], &bsb, 512) != 512)
3048 break;
3049 if (destoffsets[i] > 4096) {
3050 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]+stripes*chunk*odata, 0) !=
3051 destoffsets[i]+stripes*chunk*odata)
3052 break;
3053 if (write(destfd[i], &bsb, 512) != 512)
3054 break;
3055 }
3056 fsync(destfd[i]);
3057 rv = 0;
3058 }
3059
3060 return rv;
3061 }
3062
3063 /* in 2.6.30, the value reported by sync_completed can be
3064 * less that it should be by one stripe.
3065 * This only happens when reshape hits sync_max and pauses.
3066 * So allow wait_backup to either extent sync_max further
3067 * than strictly necessary, or return before the
3068 * sync has got quite as far as we would really like.
3069 * This is what 'blocks2' is for.
3070 * The various caller give appropriate values so that
3071 * every works.
3072 */
3073 /* FIXME return value is often ignored */
3074 static int forget_backup(
3075 int dests, int *destfd, unsigned long long *destoffsets,
3076 int part)
3077 {
3078 /*
3079 * Erase backup 'part' (which is 0 or 1)
3080 */
3081 int i;
3082 int rv;
3083
3084 if (part) {
3085 bsb.arraystart2 = __cpu_to_le64(0);
3086 bsb.length2 = __cpu_to_le64(0);
3087 } else {
3088 bsb.arraystart = __cpu_to_le64(0);
3089 bsb.length = __cpu_to_le64(0);
3090 }
3091 bsb.mtime = __cpu_to_le64(time(0));
3092 rv = 0;
3093 for (i = 0; i < dests; i++) {
3094 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
3095 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
3096 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
3097 bsb.sb_csum2 = bsb_csum((char*)&bsb,
3098 ((char*)&bsb.sb_csum2)-((char*)&bsb));
3099 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]-4096, 0) !=
3100 destoffsets[i]-4096)
3101 rv = -1;
3102 if (rv == 0 &&
3103 write(destfd[i], &bsb, 512) != 512)
3104 rv = -1;
3105 fsync(destfd[i]);
3106 }
3107 return rv;
3108 }
3109
3110 static void fail(char *msg)
3111 {
3112 int rv;
3113 rv = (write(2, msg, strlen(msg)) != (int)strlen(msg));
3114 rv |= (write(2, "\n", 1) != 1);
3115 exit(rv ? 1 : 2);
3116 }
3117
3118 static char *abuf, *bbuf;
3119 static unsigned long long abuflen;
3120 static void validate(int afd, int bfd, unsigned long long offset)
3121 {
3122 /* check that the data in the backup against the array.
3123 * This is only used for regression testing and should not
3124 * be used while the array is active
3125 */
3126 if (afd < 0)
3127 return;
3128 lseek64(bfd, offset - 4096, 0);
3129 if (read(bfd, &bsb2, 512) != 512)
3130 fail("cannot read bsb");
3131 if (bsb2.sb_csum != bsb_csum((char*)&bsb2,
3132 ((char*)&bsb2.sb_csum)-((char*)&bsb2)))
3133 fail("first csum bad");
3134 if (memcmp(bsb2.magic, "md_backup_data", 14) != 0)
3135 fail("magic is bad");
3136 if (memcmp(bsb2.magic, "md_backup_data-2", 16) == 0 &&
3137 bsb2.sb_csum2 != bsb_csum((char*)&bsb2,
3138 ((char*)&bsb2.sb_csum2)-((char*)&bsb2)))
3139 fail("second csum bad");
3140
3141 if (__le64_to_cpu(bsb2.devstart)*512 != offset)
3142 fail("devstart is wrong");
3143
3144 if (bsb2.length) {
3145 unsigned long long len = __le64_to_cpu(bsb2.length)*512;
3146
3147 if (abuflen < len) {
3148 free(abuf);
3149 free(bbuf);
3150 abuflen = len;
3151 if (posix_memalign((void**)&abuf, 4096, abuflen) ||
3152 posix_memalign((void**)&bbuf, 4096, abuflen)) {
3153 abuflen = 0;
3154 /* just stop validating on mem-alloc failure */
3155 return;
3156 }
3157 }
3158
3159 lseek64(bfd, offset, 0);
3160 if ((unsigned long long)read(bfd, bbuf, len) != len) {
3161 //printf("len %llu\n", len);
3162 fail("read first backup failed");
3163 }
3164 lseek64(afd, __le64_to_cpu(bsb2.arraystart)*512, 0);
3165 if ((unsigned long long)read(afd, abuf, len) != len)
3166 fail("read first from array failed");
3167 if (memcmp(bbuf, abuf, len) != 0) {
3168 #if 0
3169 int i;
3170 printf("offset=%llu len=%llu\n",
3171 (unsigned long long)__le64_to_cpu(bsb2.arraystart)*512, len);
3172 for (i=0; i<len; i++)
3173 if (bbuf[i] != abuf[i]) {
3174 printf("first diff byte %d\n", i);
3175 break;
3176 }
3177 #endif
3178 fail("data1 compare failed");
3179 }
3180 }
3181 if (bsb2.length2) {
3182 unsigned long long len = __le64_to_cpu(bsb2.length2)*512;
3183
3184 if (abuflen < len) {
3185 free(abuf);
3186 free(bbuf);
3187 abuflen = len;
3188 abuf = malloc(abuflen);
3189 bbuf = malloc(abuflen);
3190 }
3191
3192 lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0);
3193 if ((unsigned long long)read(bfd, bbuf, len) != len)
3194 fail("read second backup failed");
3195 lseek64(afd, __le64_to_cpu(bsb2.arraystart2)*512, 0);
3196 if ((unsigned long long)read(afd, abuf, len) != len)
3197 fail("read second from array failed");
3198 if (memcmp(bbuf, abuf, len) != 0)
3199 fail("data2 compare failed");
3200 }
3201 }
3202
3203 int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
3204 struct supertype *st, unsigned long blocks,
3205 int *fds, unsigned long long *offsets,
3206 int dests, int *destfd, unsigned long long *destoffsets)
3207 {
3208 /* Monitor a reshape where backup is being performed using
3209 * 'native' mechanism - either to a backup file, or
3210 * to some space in a spare.
3211 */
3212 char *buf;
3213 int degraded = -1;
3214 unsigned long long speed;
3215 unsigned long long suspend_point, array_size;
3216 unsigned long long backup_point, wait_point;
3217 unsigned long long reshape_completed;
3218 int done = 0;
3219 int increasing = reshape->after.data_disks >= reshape->before.data_disks;
3220 int part = 0; /* The next part of the backup area to fill. It may already
3221 * be full, so we need to check */
3222 int level = reshape->level;
3223 int layout = reshape->before.layout;
3224 int data = reshape->before.data_disks;
3225 int disks = reshape->before.data_disks + reshape->parity;
3226 int chunk = sra->array.chunk_size;
3227 struct mdinfo *sd;
3228 unsigned long stripes;
3229 int uuid[4];
3230
3231 /* set up the backup-super-block. This requires the
3232 * uuid from the array.
3233 */
3234 /* Find a superblock */
3235 for (sd = sra->devs; sd; sd = sd->next) {
3236 char *dn;
3237 int devfd;
3238 int ok;
3239 if (sd->disk.state & (1<<MD_DISK_FAULTY))
3240 continue;
3241 dn = map_dev(sd->disk.major, sd->disk.minor, 1);
3242 devfd = dev_open(dn, O_RDONLY);
3243 if (devfd < 0)
3244 continue;
3245 ok = st->ss->load_super(st, devfd, NULL);
3246 close(devfd);
3247 if (ok == 0)
3248 break;
3249 }
3250 if (!sd) {
3251 fprintf(stderr, Name ": Cannot find a superblock\n");
3252 return 0;
3253 }
3254
3255 memset(&bsb, 0, 512);
3256 memcpy(bsb.magic, "md_backup_data-1", 16);
3257 st->ss->uuid_from_super(st, uuid);
3258 memcpy(bsb.set_uuid, uuid, 16);
3259 bsb.mtime = __cpu_to_le64(time(0));
3260 bsb.devstart2 = blocks;
3261
3262 stripes = blocks / (sra->array.chunk_size/512) /
3263 reshape->before.data_disks;
3264
3265 if (posix_memalign((void**)&buf, 4096, disks * chunk))
3266 /* Don't start the 'reshape' */
3267 return 0;
3268 if (reshape->before.data_disks == reshape->after.data_disks) {
3269 sysfs_get_ll(sra, NULL, "sync_speed_min", &speed);
3270 sysfs_set_num(sra, NULL, "sync_speed_min", 200000);
3271 }
3272
3273 if (increasing) {
3274 array_size = sra->component_size * reshape->after.data_disks;
3275 backup_point = sra->reshape_progress;
3276 suspend_point = 0;
3277 } else {
3278 array_size = sra->component_size * reshape->before.data_disks;
3279 backup_point = reshape->backup_blocks;
3280 suspend_point = array_size;
3281 }
3282
3283 while (!done) {
3284 int rv;
3285
3286 /* Want to return as soon the oldest backup slot can
3287 * be released as that allows us to start backing up
3288 * some more, providing suspend_point has been
3289 * advanced, which it should have.
3290 */
3291 if (increasing) {
3292 wait_point = array_size;
3293 if (part == 0 && __le64_to_cpu(bsb.length) > 0)
3294 wait_point = (__le64_to_cpu(bsb.arraystart) +
3295 __le64_to_cpu(bsb.length));
3296 if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
3297 wait_point = (__le64_to_cpu(bsb.arraystart2) +
3298 __le64_to_cpu(bsb.length2));
3299 } else {
3300 wait_point = 0;
3301 if (part == 0 && __le64_to_cpu(bsb.length) > 0)
3302 wait_point = __le64_to_cpu(bsb.arraystart);
3303 if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
3304 wait_point = __le64_to_cpu(bsb.arraystart2);
3305 }
3306
3307 rv = progress_reshape(sra, reshape,
3308 backup_point, wait_point,
3309 &suspend_point, &reshape_completed);
3310 /* external metadata would need to ping_monitor here */
3311 sra->reshape_progress = reshape_completed;
3312
3313 /* Clear any backup region that is before 'here' */
3314 if (increasing) {
3315 if (__le64_to_cpu(bsb.length) > 0 &&
3316 reshape_completed >= (__le64_to_cpu(bsb.arraystart) +
3317 __le64_to_cpu(bsb.length)))
3318 forget_backup(dests, destfd,
3319 destoffsets, 0);
3320 if (__le64_to_cpu(bsb.length2) > 0 &&
3321 reshape_completed >= (__le64_to_cpu(bsb.arraystart2) +
3322 __le64_to_cpu(bsb.length2)))
3323 forget_backup(dests, destfd,
3324 destoffsets, 1);
3325 } else {
3326 if (__le64_to_cpu(bsb.length) > 0 &&
3327 reshape_completed <= (__le64_to_cpu(bsb.arraystart)))
3328 forget_backup(dests, destfd,
3329 destoffsets, 0);
3330 if (__le64_to_cpu(bsb.length2) > 0 &&
3331 reshape_completed <= (__le64_to_cpu(bsb.arraystart2)))
3332 forget_backup(dests, destfd,
3333 destoffsets, 1);
3334 }
3335
3336 if (rv < 0) {
3337 if (rv == -1)
3338 done = 1;
3339 break;
3340 }
3341 if (rv == 0 && increasing && !st->ss->external) {
3342 /* No longer need to monitor this reshape */
3343 done = 1;
3344 break;
3345 }
3346
3347 while (rv) {
3348 unsigned long long offset;
3349 unsigned long actual_stripes;
3350 /* Need to backup some data.
3351 * If 'part' is not used and the desired
3352 * backup size is suspended, do a backup,
3353 * then consider the next part.
3354 */
3355 /* Check that 'part' is unused */
3356 if (part == 0 && __le64_to_cpu(bsb.length) != 0)
3357 break;
3358 if (part == 1 && __le64_to_cpu(bsb.length2) != 0)
3359 break;
3360
3361 offset = backup_point / data;
3362 actual_stripes = stripes;
3363 if (increasing) {
3364 if (offset + actual_stripes * (chunk/512) >
3365 sra->component_size)
3366 actual_stripes = ((sra->component_size - offset)
3367 / (chunk/512));
3368 if (offset + actual_stripes * (chunk/512) >
3369 suspend_point/data)
3370 break;
3371 } else {
3372 if (offset < actual_stripes * (chunk/512))
3373 actual_stripes = offset / (chunk/512);
3374 offset -= actual_stripes * (chunk/512);
3375 if (offset < suspend_point/data)
3376 break;
3377 }
3378 if (actual_stripes == 0)
3379 break;
3380 grow_backup(sra, offset, actual_stripes,
3381 fds, offsets,
3382 disks, chunk, level, layout,
3383 dests, destfd, destoffsets,
3384 part, &degraded, buf);
3385 validate(afd, destfd[0], destoffsets[0]);
3386 /* record where 'part' is up to */
3387 part = !part;
3388 if (increasing)
3389 backup_point += actual_stripes * (chunk/512) * data;
3390 else
3391 backup_point -= actual_stripes * (chunk/512) * data;
3392 }
3393 }
3394
3395 /* FIXME maybe call progress_reshape one more time instead */
3396 abort_reshape(sra); /* remove any remaining suspension */
3397 if (reshape->before.data_disks == reshape->after.data_disks)
3398 sysfs_set_num(sra, NULL, "sync_speed_min", speed);
3399 free(buf);
3400 return done;
3401 }
3402
3403 /*
3404 * If any spare contains md_back_data-1 which is recent wrt mtime,
3405 * write that data into the array and update the super blocks with
3406 * the new reshape_progress
3407 */
3408 int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt,
3409 char *backup_file, int verbose)
3410 {
3411 int i, j;
3412 int old_disks;
3413 unsigned long long *offsets;
3414 unsigned long long nstripe, ostripe;
3415 int ndata, odata;
3416
3417 odata = info->array.raid_disks - info->delta_disks - 1;
3418 if (info->array.level == 6) odata--; /* number of data disks */
3419 ndata = info->array.raid_disks - 1;
3420 if (info->new_level == 6) ndata--;
3421
3422 old_disks = info->array.raid_disks - info->delta_disks;
3423
3424 if (info->delta_disks <= 0)
3425 /* Didn't grow, so the backup file must have
3426 * been used
3427 */
3428 old_disks = cnt;
3429 for (i=old_disks-(backup_file?1:0); i<cnt; i++) {
3430 struct mdinfo dinfo;
3431 int fd;
3432 int bsbsize;
3433 char *devname, namebuf[20];
3434 unsigned long long lo, hi;
3435
3436 /* This was a spare and may have some saved data on it.
3437 * Load the superblock, find and load the
3438 * backup_super_block.
3439 * If either fail, go on to next device.
3440 * If the backup contains no new info, just return
3441 * else restore data and update all superblocks
3442 */
3443 if (i == old_disks-1) {
3444 fd = open(backup_file, O_RDONLY);
3445 if (fd<0) {
3446 fprintf(stderr, Name ": backup file %s inaccessible: %s\n",
3447 backup_file, strerror(errno));
3448 continue;
3449 }
3450 devname = backup_file;
3451 } else {
3452 fd = fdlist[i];
3453 if (fd < 0)
3454 continue;
3455 if (st->ss->load_super(st, fd, NULL))
3456 continue;
3457
3458 st->ss->getinfo_super(st, &dinfo, NULL);
3459 st->ss->free_super(st);
3460
3461 if (lseek64(fd,
3462 (dinfo.data_offset + dinfo.component_size - 8) <<9,
3463 0) < 0) {
3464 fprintf(stderr, Name ": Cannot seek on device %d\n", i);
3465 continue; /* Cannot seek */
3466 }
3467 sprintf(namebuf, "device-%d", i);
3468 devname = namebuf;
3469 }
3470 if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb)) {
3471 if (verbose)
3472 fprintf(stderr, Name ": Cannot read from %s\n", devname);
3473 continue; /* Cannot read */
3474 }
3475 if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0 &&
3476 memcmp(bsb.magic, "md_backup_data-2", 16) != 0) {
3477 if (verbose)
3478 fprintf(stderr, Name ": No backup metadata on %s\n", devname);
3479 continue;
3480 }
3481 if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb))) {
3482 if (verbose)
3483 fprintf(stderr, Name ": Bad backup-metadata checksum on %s\n", devname);
3484 continue; /* bad checksum */
3485 }
3486 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0 &&
3487 bsb.sb_csum2 != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum2)-((char*)&bsb))) {
3488 if (verbose)
3489 fprintf(stderr, Name ": Bad backup-metadata checksum2 on %s\n", devname);
3490 continue; /* Bad second checksum */
3491 }
3492 if (memcmp(bsb.set_uuid,info->uuid, 16) != 0) {
3493 if (verbose)
3494 fprintf(stderr, Name ": Wrong uuid on backup-metadata on %s\n", devname);
3495 continue; /* Wrong uuid */
3496 }
3497
3498 /* array utime and backup-mtime should be updated at much the same time, but it seems that
3499 * sometimes they aren't... So allow considerable flexability in matching, and allow
3500 * this test to be overridden by an environment variable.
3501 */
3502 if (info->array.utime > (int)__le64_to_cpu(bsb.mtime) + 2*60*60 ||
3503 info->array.utime < (int)__le64_to_cpu(bsb.mtime) - 10*60) {
3504 if (check_env("MDADM_GROW_ALLOW_OLD")) {
3505 fprintf(stderr, Name ": accepting backup with timestamp %lu "
3506 "for array with timestamp %lu\n",
3507 (unsigned long)__le64_to_cpu(bsb.mtime),
3508 (unsigned long)info->array.utime);
3509 } else {
3510 if (verbose)
3511 fprintf(stderr, Name ": too-old timestamp on "
3512 "backup-metadata on %s\n", devname);
3513 continue; /* time stamp is too bad */
3514 }
3515 }
3516
3517 if (bsb.magic[15] == '1') {
3518 if (bsb.length == 0)
3519 continue;
3520 if (info->delta_disks >= 0) {
3521 /* reshape_progress is increasing */
3522 if (__le64_to_cpu(bsb.arraystart)
3523 + __le64_to_cpu(bsb.length)
3524 < info->reshape_progress) {
3525 nonew:
3526 if (verbose)
3527 fprintf(stderr, Name
3528 ": backup-metadata found on %s but is not needed\n", devname);
3529 continue; /* No new data here */
3530 }
3531 } else {
3532 /* reshape_progress is decreasing */
3533 if (__le64_to_cpu(bsb.arraystart) >=
3534 info->reshape_progress)
3535 goto nonew; /* No new data here */
3536 }
3537 } else {
3538 if (bsb.length == 0 && bsb.length2 == 0)
3539 continue;
3540 if (info->delta_disks >= 0) {
3541 /* reshape_progress is increasing */
3542 if ((__le64_to_cpu(bsb.arraystart)
3543 + __le64_to_cpu(bsb.length)
3544 < info->reshape_progress)
3545 &&
3546 (__le64_to_cpu(bsb.arraystart2)
3547 + __le64_to_cpu(bsb.length2)
3548 < info->reshape_progress))
3549 goto nonew; /* No new data here */
3550 } else {
3551 /* reshape_progress is decreasing */
3552 if (__le64_to_cpu(bsb.arraystart) >=
3553 info->reshape_progress &&
3554 __le64_to_cpu(bsb.arraystart2) >=
3555 info->reshape_progress)
3556 goto nonew; /* No new data here */
3557 }
3558 }
3559 if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0) {
3560 second_fail:
3561 if (verbose)
3562 fprintf(stderr, Name
3563 ": Failed to verify secondary backup-metadata block on %s\n",
3564 devname);
3565 continue; /* Cannot seek */
3566 }
3567 /* There should be a duplicate backup superblock 4k before here */
3568 if (lseek64(fd, -4096, 1) < 0 ||
3569 read(fd, &bsb2, sizeof(bsb2)) != sizeof(bsb2))
3570 goto second_fail; /* Cannot find leading superblock */
3571 if (bsb.magic[15] == '1')
3572 bsbsize = offsetof(struct mdp_backup_super, pad1);
3573 else
3574 bsbsize = offsetof(struct mdp_backup_super, pad);
3575 if (memcmp(&bsb2, &bsb, bsbsize) != 0)
3576 goto second_fail; /* Cannot find leading superblock */
3577
3578 /* Now need the data offsets for all devices. */
3579 offsets = malloc(sizeof(*offsets)*info->array.raid_disks);
3580 for(j=0; j<info->array.raid_disks; j++) {
3581 if (fdlist[j] < 0)
3582 continue;
3583 if (st->ss->load_super(st, fdlist[j], NULL))
3584 /* FIXME should be this be an error */
3585 continue;
3586 st->ss->getinfo_super(st, &dinfo, NULL);
3587 st->ss->free_super(st);
3588 offsets[j] = dinfo.data_offset * 512;
3589 }
3590 printf(Name ": restoring critical section\n");
3591
3592 if (restore_stripes(fdlist, offsets,
3593 info->array.raid_disks,
3594 info->new_chunk,
3595 info->new_level,
3596 info->new_layout,
3597 fd, __le64_to_cpu(bsb.devstart)*512,
3598 __le64_to_cpu(bsb.arraystart)*512,
3599 __le64_to_cpu(bsb.length)*512, NULL)) {
3600 /* didn't succeed, so giveup */
3601 if (verbose)
3602 fprintf(stderr, Name ": Error restoring backup from %s\n",
3603 devname);
3604 return 1;
3605 }
3606
3607 if (bsb.magic[15] == '2' &&
3608 restore_stripes(fdlist, offsets,
3609 info->array.raid_disks,
3610 info->new_chunk,
3611 info->new_level,
3612 info->new_layout,
3613 fd, __le64_to_cpu(bsb.devstart)*512 +
3614 __le64_to_cpu(bsb.devstart2)*512,
3615 __le64_to_cpu(bsb.arraystart2)*512,
3616 __le64_to_cpu(bsb.length2)*512, NULL)) {
3617 /* didn't succeed, so giveup */
3618 if (verbose)
3619 fprintf(stderr, Name ": Error restoring second backup from %s\n",
3620 devname);
3621 return 1;
3622 }
3623
3624
3625 /* Ok, so the data is restored. Let's update those superblocks. */
3626
3627 lo = hi = 0;
3628 if (bsb.length) {
3629 lo = __le64_to_cpu(bsb.arraystart);
3630 hi = lo + __le64_to_cpu(bsb.length);
3631 }
3632 if (bsb.magic[15] == '2' && bsb.length2) {
3633 unsigned long long lo1, hi1;
3634 lo1 = __le64_to_cpu(bsb.arraystart2);
3635 hi1 = lo1 + __le64_to_cpu(bsb.length2);
3636 if (lo == hi) {
3637 lo = lo1;
3638 hi = hi1;
3639 } else if (lo < lo1)
3640 hi = hi1;
3641 else
3642 lo = lo1;
3643 }
3644 if (lo < hi &&
3645 (info->reshape_progress < lo ||
3646 info->reshape_progress > hi))
3647 /* backup does not affect reshape_progress*/ ;
3648 else if (info->delta_disks >= 0) {
3649 info->reshape_progress = __le64_to_cpu(bsb.arraystart) +
3650 __le64_to_cpu(bsb.length);
3651 if (bsb.magic[15] == '2') {
3652 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2) +
3653 __le64_to_cpu(bsb.length2);
3654 if (p2 > info->reshape_progress)
3655 info->reshape_progress = p2;
3656 }
3657 } else {
3658 info->reshape_progress = __le64_to_cpu(bsb.arraystart);
3659 if (bsb.magic[15] == '2') {
3660 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2);
3661 if (p2 < info->reshape_progress)
3662 info->reshape_progress = p2;
3663 }
3664 }
3665 for (j=0; j<info->array.raid_disks; j++) {
3666 if (fdlist[j] < 0) continue;
3667 if (st->ss->load_super(st, fdlist[j], NULL))
3668 continue;
3669 st->ss->getinfo_super(st, &dinfo, NULL);
3670 dinfo.reshape_progress = info->reshape_progress;
3671 st->ss->update_super(st, &dinfo,
3672 "_reshape_progress",
3673 NULL,0, 0, NULL);
3674 st->ss->store_super(st, fdlist[j]);
3675 st->ss->free_super(st);
3676 }
3677 return 0;
3678 }
3679 /* Didn't find any backup data, try to see if any
3680 * was needed.
3681 */
3682 if (info->delta_disks < 0) {
3683 /* When shrinking, the critical section is at the end.
3684 * So see if we are before the critical section.
3685 */
3686 unsigned long long first_block;
3687 nstripe = ostripe = 0;
3688 first_block = 0;
3689 while (ostripe >= nstripe) {
3690 ostripe += info->array.chunk_size / 512;
3691 first_block = ostripe * odata;
3692 nstripe = first_block / ndata / (info->new_chunk/512) *
3693 (info->new_chunk/512);
3694 }
3695
3696 if (info->reshape_progress >= first_block)
3697 return 0;
3698 }
3699 if (info->delta_disks > 0) {
3700 /* See if we are beyond the critical section. */
3701 unsigned long long last_block;
3702 nstripe = ostripe = 0;
3703 last_block = 0;
3704 while (nstripe >= ostripe) {
3705 nstripe += info->new_chunk / 512;
3706 last_block = nstripe * ndata;
3707 ostripe = last_block / odata / (info->array.chunk_size/512) *
3708 (info->array.chunk_size/512);
3709 }
3710
3711 if (info->reshape_progress >= last_block)
3712 return 0;
3713 }
3714 /* needed to recover critical section! */
3715 if (verbose)
3716 fprintf(stderr, Name ": Failed to find backup of critical section\n");
3717 return 1;
3718 }
3719
3720 int Grow_continue_command(char *devname, int fd,
3721 char *backup_file, int verbose)
3722 {
3723 int ret_val = 0;
3724 struct supertype *st = NULL;
3725 struct mdinfo *content = NULL;
3726 struct mdinfo array;
3727 char *subarray = NULL;
3728 struct mdinfo *cc = NULL;
3729 struct mdstat_ent *mdstat = NULL;
3730 char buf[40];
3731 int cfd = -1;
3732 int fd2 = -1;
3733 char *ep;
3734 unsigned long long position;
3735
3736 dprintf("Grow continue from command line called for %s\n",
3737 devname);
3738
3739 st = super_by_fd(fd, &subarray);
3740 if (!st || !st->ss) {
3741 fprintf(stderr,
3742 Name ": Unable to determine metadata format for %s\n",
3743 devname);
3744 return 1;
3745 }
3746 dprintf("Grow continue is run for ");
3747 if (st->ss->external == 0) {
3748 dprintf("native array (%s)\n", devname);
3749 if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) {
3750 fprintf(stderr, Name ": %s is not an active md array -"
3751 " aborting\n", devname);
3752 ret_val = 1;
3753 goto Grow_continue_command_exit;
3754 }
3755 content = &array;
3756 sysfs_init(content, fd, st->devnum);
3757 } else {
3758 int container_dev;
3759
3760 if (subarray) {
3761 dprintf("subarray (%s)\n", subarray);
3762 container_dev = st->container_dev;
3763 cfd = open_dev_excl(st->container_dev);
3764 } else {
3765 container_dev = st->devnum;
3766 close(fd);
3767 cfd = open_dev_excl(st->devnum);
3768 dprintf("container (%i)\n", container_dev);
3769 fd = cfd;
3770 }
3771 if (cfd < 0) {
3772 fprintf(stderr, Name ": Unable to open container "
3773 "for %s\n", devname);
3774 ret_val = 1;
3775 goto Grow_continue_command_exit;
3776 }
3777 fmt_devname(buf, container_dev);
3778
3779 /* find in container array under reshape
3780 */
3781 ret_val = st->ss->load_container(st, cfd, NULL);
3782 if (ret_val) {
3783 fprintf(stderr,
3784 Name ": Cannot read superblock for %s\n",
3785 devname);
3786 ret_val = 1;
3787 goto Grow_continue_command_exit;
3788 }
3789
3790 cc = st->ss->container_content(st, NULL);
3791 for (content = cc; content ; content = content->next) {
3792 char *array;
3793 int allow_reshape;
3794
3795 if (content->reshape_active == 0)
3796 continue;
3797 /* The decision about array or container wide
3798 * reshape is taken in Grow_continue based
3799 * content->reshape_active state, therefore we
3800 * need to check_reshape based on
3801 * reshape_active and subarray name
3802 */
3803 allow_reshape =
3804 check_reshape((content->reshape_active == CONTAINER_RESHAPE)? NULL : subarray,
3805 content);
3806 if (!allow_reshape) {
3807 fprintf(stderr, Name
3808 ": cannot continue reshape of an array"
3809 " in container with unsupported"
3810 " metadata: %s(%s)\n",
3811 devname, buf);
3812 ret_val = 1;
3813 goto Grow_continue_command_exit;
3814 }
3815
3816 array = strchr(content->text_version+1, '/')+1;
3817 mdstat = mdstat_by_subdev(array, container_dev);
3818 if (!mdstat)
3819 continue;
3820 break;
3821 }
3822 if (!content) {
3823 fprintf(stderr,
3824 Name ": Unable to determine reshaped "
3825 "array for %s\n", devname);
3826 ret_val = 1;
3827 goto Grow_continue_command_exit;
3828 }
3829 fd2 = open_dev(mdstat->devnum);
3830 if (fd2 < 0) {
3831 fprintf(stderr, Name ": cannot open (md%i)\n",
3832 mdstat->devnum);
3833 ret_val = 1;
3834 goto Grow_continue_command_exit;
3835 }
3836
3837 sysfs_init(content, fd2, mdstat->devnum);
3838
3839 /* start mdmon in case it is not running
3840 */
3841 if (!mdmon_running(container_dev))
3842 start_mdmon(container_dev);
3843 ping_monitor(buf);
3844
3845 if (mdmon_running(container_dev))
3846 st->update_tail = &st->updates;
3847 else {
3848 fprintf(stderr, Name ": No mdmon found. "
3849 "Grow cannot continue.\n");
3850 ret_val = 1;
3851 goto Grow_continue_command_exit;
3852 }
3853 }
3854
3855 /* verify that array under reshape is started from
3856 * correct position
3857 */
3858 ret_val = sysfs_get_str(content, NULL, "sync_max", buf, 40);
3859 if (ret_val <= 0) {
3860 fprintf(stderr, Name
3861 ": cannot open verify reshape progress for %s (%i)\n",
3862 content->sys_name, ret_val);
3863 ret_val = 1;
3864 goto Grow_continue_command_exit;
3865 }
3866 dprintf(Name ": Read sync_max sysfs entry is: %s\n", buf);
3867 position = strtoull(buf, &ep, 0);
3868 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) {
3869 fprintf(stderr, Name ": Fatal error: array reshape was"
3870 " not properly frozen\n");
3871 ret_val = 1;
3872 goto Grow_continue_command_exit;
3873 }
3874 position *= get_data_disks(map_name(pers, mdstat->level),
3875 content->new_layout,
3876 content->array.raid_disks);
3877 if (position != content->reshape_progress) {
3878 fprintf(stderr, Name ": Fatal error: array reshape was"
3879 " not properly frozen.\n");
3880 ret_val = 1;
3881 goto Grow_continue_command_exit;
3882 }
3883
3884 /* continue reshape
3885 */
3886 ret_val = Grow_continue(fd, st, content, backup_file, 0);
3887
3888 Grow_continue_command_exit:
3889 if (fd2 > -1)
3890 close(fd2);
3891 if (cfd > -1)
3892 close(cfd);
3893 st->ss->free_super(st);
3894 free_mdstat(mdstat);
3895 sysfs_free(cc);
3896 free(subarray);
3897
3898 return ret_val;
3899 }
3900
3901 int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
3902 char *backup_file, int freeze_reshape)
3903 {
3904 int ret_val = 2;
3905
3906 if (!info->reshape_active)
3907 return ret_val;
3908
3909 if (st->ss->external) {
3910 char container[40];
3911 int cfd = open_dev(st->container_dev);
3912
3913 if (cfd < 0)
3914 return 1;
3915
3916 fmt_devname(container, st->container_dev);
3917 st->ss->load_container(st, cfd, container);
3918 close(cfd);
3919 ret_val = reshape_container(container, NULL, mdfd,
3920 st, info, 0, backup_file,
3921 0, 1, freeze_reshape);
3922 } else
3923 ret_val = reshape_array(NULL, mdfd, "array", st, info, 1,
3924 NULL, backup_file, 0, 0, 1,
3925 freeze_reshape);
3926
3927 return ret_val;
3928 }