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