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