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