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