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