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