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