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