]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Grow.c
Grow: allow metadata to indicate that changing data_offset not supported.
[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 pr_err("--grow --data-offset not yet supported\n");
1545 return 1;
1546 }
1547
1548 if (s->size > 0 &&
1549 (s->chunk || s->level!= UnSet || s->layout_str || s->raiddisks)) {
1550 pr_err("cannot change component size at the same time "
1551 "as other changes.\n"
1552 " Change size first, then check data is intact before "
1553 "making other changes.\n");
1554 return 1;
1555 }
1556
1557 if (s->raiddisks && s->raiddisks < array.raid_disks && array.level > 1 &&
1558 get_linux_version() < 2006032 &&
1559 !check_env("MDADM_FORCE_FEWER")) {
1560 pr_err("reducing the number of devices is not safe before Linux 2.6.32\n"
1561 " Please use a newer kernel\n");
1562 return 1;
1563 }
1564
1565 st = super_by_fd(fd, &subarray);
1566 if (!st) {
1567 pr_err("Unable to determine metadata format for %s\n", devname);
1568 return 1;
1569 }
1570 if (s->raiddisks > st->max_devs) {
1571 pr_err("Cannot increase raid-disks on this array"
1572 " beyond %d\n", st->max_devs);
1573 return 1;
1574 }
1575
1576 /* in the external case we need to check that the requested reshape is
1577 * supported, and perform an initial check that the container holds the
1578 * pre-requisite spare devices (mdmon owns final validation)
1579 */
1580 if (st->ss->external) {
1581 int rv;
1582
1583 if (subarray) {
1584 container = st->container_devnm;
1585 cfd = open_dev_excl(st->container_devnm);
1586 } else {
1587 container = st->devnm;
1588 close(fd);
1589 cfd = open_dev_excl(st->devnm);
1590 fd = cfd;
1591 }
1592 if (cfd < 0) {
1593 pr_err("Unable to open container for %s\n",
1594 devname);
1595 free(subarray);
1596 return 1;
1597 }
1598
1599 rv = st->ss->load_container(st, cfd, NULL);
1600
1601 if (rv) {
1602 pr_err("Cannot read superblock for %s\n",
1603 devname);
1604 free(subarray);
1605 return 1;
1606 }
1607
1608 /* check if operation is supported for metadata handler */
1609 if (st->ss->container_content) {
1610 struct mdinfo *cc = NULL;
1611 struct mdinfo *content = NULL;
1612
1613 cc = st->ss->container_content(st, subarray);
1614 for (content = cc; content ; content = content->next) {
1615 int allow_reshape = 1;
1616
1617 /* check if reshape is allowed based on metadata
1618 * indications stored in content.array.status
1619 */
1620 if (content->array.state & (1<<MD_SB_BLOCK_VOLUME))
1621 allow_reshape = 0;
1622 if (content->array.state
1623 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE))
1624 allow_reshape = 0;
1625 if (!allow_reshape) {
1626 pr_err("cannot reshape arrays in"
1627 " container with unsupported"
1628 " metadata: %s(%s)\n",
1629 devname, container);
1630 sysfs_free(cc);
1631 free(subarray);
1632 return 1;
1633 }
1634 }
1635 sysfs_free(cc);
1636 }
1637 if (mdmon_running(container))
1638 st->update_tail = &st->updates;
1639 }
1640
1641 added_disks = 0;
1642 for (dv = devlist; dv; dv = dv->next)
1643 added_disks++;
1644 if (s->raiddisks > array.raid_disks &&
1645 array.spare_disks +added_disks < (s->raiddisks - array.raid_disks) &&
1646 !c->force) {
1647 pr_err("Need %d spare%s to avoid degraded array,"
1648 " and only have %d.\n"
1649 " Use --force to over-ride this check.\n",
1650 s->raiddisks - array.raid_disks,
1651 s->raiddisks - array.raid_disks == 1 ? "" : "s",
1652 array.spare_disks + added_disks);
1653 return 1;
1654 }
1655
1656 sra = sysfs_read(fd, NULL, GET_LEVEL | GET_DISKS | GET_DEVS
1657 | GET_STATE | GET_VERSION);
1658 if (sra) {
1659 if (st->ss->external && subarray == NULL) {
1660 array.level = LEVEL_CONTAINER;
1661 sra->array.level = LEVEL_CONTAINER;
1662 }
1663 } else {
1664 pr_err("failed to read sysfs parameters for %s\n",
1665 devname);
1666 return 1;
1667 }
1668 frozen = freeze(st);
1669 if (frozen < -1) {
1670 /* freeze() already spewed the reason */
1671 sysfs_free(sra);
1672 return 1;
1673 } else if (frozen < 0) {
1674 pr_err("%s is performing resync/recovery and cannot"
1675 " be reshaped\n", devname);
1676 sysfs_free(sra);
1677 return 1;
1678 }
1679
1680 /* ========= set size =============== */
1681 if (s->size > 0 && (s->size == MAX_SIZE || s->size != (unsigned)array.size)) {
1682 unsigned long long orig_size = get_component_size(fd)/2;
1683 unsigned long long min_csize;
1684 struct mdinfo *mdi;
1685 int raid0_takeover = 0;
1686
1687 if (orig_size == 0)
1688 orig_size = (unsigned) array.size;
1689
1690 if (orig_size == 0) {
1691 pr_err("Cannot set device size in this type of array.\n");
1692 rv = 1;
1693 goto release;
1694 }
1695
1696 if (reshape_super(st, s->size, UnSet, UnSet, 0, 0, UnSet, NULL,
1697 devname, APPLY_METADATA_CHANGES, c->verbose > 0)) {
1698 rv = 1;
1699 goto release;
1700 }
1701 sync_metadata(st);
1702 if (st->ss->external) {
1703 /* metadata can have size limitation
1704 * update size value according to metadata information
1705 */
1706 struct mdinfo *sizeinfo =
1707 st->ss->container_content(st, subarray);
1708 if (sizeinfo) {
1709 unsigned long long new_size =
1710 sizeinfo->custom_array_size/2;
1711 int data_disks = get_data_disks(
1712 sizeinfo->array.level,
1713 sizeinfo->array.layout,
1714 sizeinfo->array.raid_disks);
1715 new_size /= data_disks;
1716 dprintf("Metadata size correction from %llu to "
1717 "%llu (%llu)\n", orig_size, new_size,
1718 new_size * data_disks);
1719 s->size = new_size;
1720 sysfs_free(sizeinfo);
1721 }
1722 }
1723
1724 /* Update the size of each member device in case
1725 * they have been resized. This will never reduce
1726 * below the current used-size. The "size" attribute
1727 * understands '0' to mean 'max'.
1728 */
1729 min_csize = 0;
1730 rv = 0;
1731 for (mdi = sra->devs; mdi; mdi = mdi->next) {
1732 if (sysfs_set_num(sra, mdi, "size",
1733 s->size == MAX_SIZE ? 0 : s->size) < 0) {
1734 /* Probably kernel refusing to let us
1735 * reduce the size - not an error.
1736 */
1737 break;
1738 }
1739 if (array.not_persistent == 0 &&
1740 array.major_version == 0 &&
1741 get_linux_version() < 3001000) {
1742 /* Dangerous to allow size to exceed 2TB */
1743 unsigned long long csize;
1744 if (sysfs_get_ll(sra, mdi, "size", &csize) == 0) {
1745 if (csize >= 2ULL*1024*1024*1024)
1746 csize = 2ULL*1024*1024*1024;
1747 if ((min_csize == 0 || (min_csize
1748 > csize)))
1749 min_csize = csize;
1750 }
1751 }
1752 }
1753 if (rv) {
1754 pr_err("Cannot set size on "
1755 "array members.\n");
1756 goto size_change_error;
1757 }
1758 if (min_csize && s->size > min_csize) {
1759 pr_err("Cannot safely make this array "
1760 "use more than 2TB per device on this kernel.\n");
1761 rv = 1;
1762 goto size_change_error;
1763 }
1764 if (min_csize && s->size == MAX_SIZE) {
1765 /* Don't let the kernel choose a size - it will get
1766 * it wrong
1767 */
1768 pr_err("Limited v0.90 array to "
1769 "2TB per device\n");
1770 s->size = min_csize;
1771 }
1772 if (st->ss->external) {
1773 if (sra->array.level == 0) {
1774 rv = sysfs_set_str(sra, NULL, "level",
1775 "raid5");
1776 if (!rv) {
1777 raid0_takeover = 1;
1778 /* get array parametes after takeover
1779 * to chane one parameter at time only
1780 */
1781 rv = ioctl(fd, GET_ARRAY_INFO, &array);
1782 }
1783 }
1784 /* make sure mdmon is
1785 * aware of the new level */
1786 if (!mdmon_running(st->container_devnm))
1787 start_mdmon(st->container_devnm);
1788 ping_monitor(container);
1789 if (mdmon_running(st->container_devnm) &&
1790 st->update_tail == NULL)
1791 st->update_tail = &st->updates;
1792 }
1793
1794 if (s->size == MAX_SIZE)
1795 s->size = 0;
1796 array.size = s->size;
1797 if ((unsigned)array.size != s->size) {
1798 /* got truncated to 32bit, write to
1799 * component_size instead
1800 */
1801 if (sra)
1802 rv = sysfs_set_num(sra, NULL,
1803 "component_size", s->size);
1804 else
1805 rv = -1;
1806 } else {
1807 rv = ioctl(fd, SET_ARRAY_INFO, &array);
1808
1809 /* manage array size when it is managed externally
1810 */
1811 if ((rv == 0) && st->ss->external)
1812 rv = set_array_size(st, sra, sra->text_version);
1813 }
1814
1815 if (raid0_takeover) {
1816 /* do not recync non-existing parity,
1817 * we will drop it anyway
1818 */
1819 sysfs_set_str(sra, NULL, "sync_action", "frozen");
1820 /* go back to raid0, drop parity disk
1821 */
1822 sysfs_set_str(sra, NULL, "level", "raid0");
1823 ioctl(fd, GET_ARRAY_INFO, &array);
1824 }
1825
1826 size_change_error:
1827 if (rv != 0) {
1828 int err = errno;
1829
1830 /* restore metadata */
1831 if (reshape_super(st, orig_size, UnSet, UnSet, 0, 0,
1832 UnSet, NULL, devname,
1833 ROLLBACK_METADATA_CHANGES,
1834 c->verbose) == 0)
1835 sync_metadata(st);
1836 pr_err("Cannot set device size for %s: %s\n",
1837 devname, strerror(err));
1838 if (err == EBUSY &&
1839 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
1840 cont_err("Bitmap must be removed before size can be changed\n");
1841 rv = 1;
1842 goto release;
1843 }
1844 if (s->assume_clean) {
1845 /* This will fail on kernels older than 3.0 unless
1846 * a backport has been arranged.
1847 */
1848 if (sra == NULL ||
1849 sysfs_set_str(sra, NULL, "resync_start", "none") < 0)
1850 pr_err("--assume-clean not supported with --grow on this kernel\n");
1851 }
1852 ioctl(fd, GET_ARRAY_INFO, &array);
1853 s->size = get_component_size(fd)/2;
1854 if (s->size == 0)
1855 s->size = array.size;
1856 if (c->verbose >= 0) {
1857 if (s->size == orig_size)
1858 pr_err("component size of %s "
1859 "unchanged at %lluK\n",
1860 devname, s->size);
1861 else
1862 pr_err("component size of %s "
1863 "has been set to %lluK\n",
1864 devname, s->size);
1865 }
1866 changed = 1;
1867 } else if (array.level != LEVEL_CONTAINER) {
1868 s->size = get_component_size(fd)/2;
1869 if (s->size == 0)
1870 s->size = array.size;
1871 }
1872
1873 /* See if there is anything else to do */
1874 if ((s->level == UnSet || s->level == array.level) &&
1875 (s->layout_str == NULL) &&
1876 (s->chunk == 0 || s->chunk == array.chunk_size) &&
1877 (s->raiddisks == 0 || s->raiddisks == array.raid_disks)) {
1878 /* Nothing more to do */
1879 if (!changed && c->verbose >= 0)
1880 pr_err("%s: no change requested\n",
1881 devname);
1882 goto release;
1883 }
1884
1885 /* ========= check for Raid10/Raid1 -> Raid0 conversion ===============
1886 * current implementation assumes that following conditions must be met:
1887 * - RAID10:
1888 * - far_copies == 1
1889 * - near_copies == 2
1890 */
1891 if ((s->level == 0 && array.level == 10 && sra &&
1892 array.layout == ((1 << 8) + 2) && !(array.raid_disks & 1)) ||
1893 (s->level == 0 && array.level == 1 && sra)) {
1894 int err;
1895 err = remove_disks_for_takeover(st, sra, array.layout);
1896 if (err) {
1897 dprintf(Name": Array cannot be reshaped\n");
1898 if (cfd > -1)
1899 close(cfd);
1900 rv = 1;
1901 goto release;
1902 }
1903 /* Make sure mdmon has seen the device removal
1904 * and updated metadata before we continue with
1905 * level change
1906 */
1907 if (container)
1908 ping_monitor(container);
1909 }
1910
1911 memset(&info, 0, sizeof(info));
1912 info.array = array;
1913 sysfs_init(&info, fd, NULL);
1914 strcpy(info.text_version, sra->text_version);
1915 info.component_size = s->size*2;
1916 info.new_level = s->level;
1917 info.new_chunk = s->chunk * 1024;
1918 if (info.array.level == LEVEL_CONTAINER) {
1919 info.delta_disks = UnSet;
1920 info.array.raid_disks = s->raiddisks;
1921 } else if (s->raiddisks)
1922 info.delta_disks = s->raiddisks - info.array.raid_disks;
1923 else
1924 info.delta_disks = UnSet;
1925 if (s->layout_str == NULL) {
1926 info.new_layout = UnSet;
1927 if (info.array.level == 6 &&
1928 (info.new_level == 6 || info.new_level == UnSet) &&
1929 info.array.layout >= 16) {
1930 pr_err("%s has a non-standard layout. If you"
1931 " wish to preserve this\n", devname);
1932 cont_err("during the reshape, please specify"
1933 " --layout=preserve\n");
1934 cont_err("If you want to change it, specify a"
1935 " layout or use --layout=normalise\n");
1936 rv = 1;
1937 goto release;
1938 }
1939 } else if (strcmp(s->layout_str, "normalise") == 0 ||
1940 strcmp(s->layout_str, "normalize") == 0) {
1941 /* If we have a -6 RAID6 layout, remove the '-6'. */
1942 info.new_layout = UnSet;
1943 if (info.array.level == 6 && info.new_level == UnSet) {
1944 char l[40], *h;
1945 strcpy(l, map_num(r6layout, info.array.layout));
1946 h = strrchr(l, '-');
1947 if (h && strcmp(h, "-6") == 0) {
1948 *h = 0;
1949 info.new_layout = map_name(r6layout, l);
1950 }
1951 } else {
1952 pr_err("%s is only meaningful when reshaping"
1953 " a RAID6 array.\n", s->layout_str);
1954 rv = 1;
1955 goto release;
1956 }
1957 } else if (strcmp(s->layout_str, "preserve") == 0) {
1958 /* This means that a non-standard RAID6 layout
1959 * is OK.
1960 * In particular:
1961 * - When reshape a RAID6 (e.g. adding a device)
1962 * which is in a non-standard layout, it is OK
1963 * to preserve that layout.
1964 * - When converting a RAID5 to RAID6, leave it in
1965 * the XXX-6 layout, don't re-layout.
1966 */
1967 if (info.array.level == 6 && info.new_level == UnSet)
1968 info.new_layout = info.array.layout;
1969 else if (info.array.level == 5 && info.new_level == 6) {
1970 char l[40];
1971 strcpy(l, map_num(r5layout, info.array.layout));
1972 strcat(l, "-6");
1973 info.new_layout = map_name(r6layout, l);
1974 } else {
1975 pr_err("%s in only meaningful when reshaping"
1976 " to RAID6\n", s->layout_str);
1977 rv = 1;
1978 goto release;
1979 }
1980 } else {
1981 int l = info.new_level;
1982 if (l == UnSet)
1983 l = info.array.level;
1984 switch (l) {
1985 case 5:
1986 info.new_layout = map_name(r5layout, s->layout_str);
1987 break;
1988 case 6:
1989 info.new_layout = map_name(r6layout, s->layout_str);
1990 break;
1991 case 10:
1992 info.new_layout = parse_layout_10(s->layout_str);
1993 break;
1994 case LEVEL_FAULTY:
1995 info.new_layout = parse_layout_faulty(s->layout_str);
1996 break;
1997 default:
1998 pr_err("layout not meaningful"
1999 " with this level\n");
2000 rv = 1;
2001 goto release;
2002 }
2003 if (info.new_layout == UnSet) {
2004 pr_err("layout %s not understood"
2005 " for this level\n",
2006 s->layout_str);
2007 rv = 1;
2008 goto release;
2009 }
2010 }
2011
2012 if (array.level == LEVEL_FAULTY) {
2013 if (s->level != UnSet && s->level != array.level) {
2014 pr_err("cannot change level of Faulty device\n");
2015 rv =1 ;
2016 }
2017 if (s->chunk) {
2018 pr_err("cannot set chunksize of Faulty device\n");
2019 rv =1 ;
2020 }
2021 if (s->raiddisks && s->raiddisks != 1) {
2022 pr_err("cannot set raid_disks of Faulty device\n");
2023 rv =1 ;
2024 }
2025 if (s->layout_str) {
2026 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
2027 dprintf("Cannot get array information.\n");
2028 goto release;
2029 }
2030 array.layout = info.new_layout;
2031 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
2032 pr_err("failed to set new layout\n");
2033 rv = 1;
2034 } else if (c->verbose >= 0)
2035 printf("layout for %s set to %d\n",
2036 devname, array.layout);
2037 }
2038 } else if (array.level == LEVEL_CONTAINER) {
2039 /* This change is to be applied to every array in the
2040 * container. This is only needed when the metadata imposes
2041 * restraints of the various arrays in the container.
2042 * Currently we only know that IMSM requires all arrays
2043 * to have the same number of devices so changing the
2044 * number of devices (On-Line Capacity Expansion) must be
2045 * performed at the level of the container
2046 */
2047 rv = reshape_container(container, devname, -1, st, &info,
2048 c->force, c->backup_file, c->verbose, 0, 0);
2049 frozen = 0;
2050 } else {
2051 /* get spare devices from external metadata
2052 */
2053 if (st->ss->external) {
2054 struct mdinfo *info2;
2055
2056 info2 = st->ss->container_content(st, subarray);
2057 if (info2) {
2058 info.array.spare_disks =
2059 info2->array.spare_disks;
2060 sysfs_free(info2);
2061 }
2062 }
2063
2064 /* Impose these changes on a single array. First
2065 * check that the metadata is OK with the change. */
2066
2067 if (reshape_super(st, 0, info.new_level,
2068 info.new_layout, info.new_chunk,
2069 info.array.raid_disks, info.delta_disks,
2070 c->backup_file, devname, APPLY_METADATA_CHANGES,
2071 c->verbose)) {
2072 rv = 1;
2073 goto release;
2074 }
2075 sync_metadata(st);
2076 rv = reshape_array(container, fd, devname, st, &info, c->force,
2077 devlist, data_offset, c->backup_file, c->verbose,
2078 0, 0, 0);
2079 frozen = 0;
2080 }
2081 release:
2082 sysfs_free(sra);
2083 if (frozen > 0)
2084 unfreeze(st);
2085 return rv;
2086 }
2087
2088 /* verify_reshape_position()
2089 * Function checks if reshape position in metadata is not farther
2090 * than position in md.
2091 * Return value:
2092 * 0 : not valid sysfs entry
2093 * it can be caused by not started reshape, it should be started
2094 * by reshape array or raid0 array is before takeover
2095 * -1 : error, reshape position is obviously wrong
2096 * 1 : success, reshape progress correct or updated
2097 */
2098 static int verify_reshape_position(struct mdinfo *info, int level)
2099 {
2100 int ret_val = 0;
2101 char buf[40];
2102 int rv;
2103
2104 /* read sync_max, failure can mean raid0 array */
2105 rv = sysfs_get_str(info, NULL, "sync_max", buf, 40);
2106
2107 if (rv > 0) {
2108 char *ep;
2109 unsigned long long position = strtoull(buf, &ep, 0);
2110
2111 dprintf(Name": Read sync_max sysfs entry is: %s\n", buf);
2112 if (!(ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))) {
2113 position *= get_data_disks(level,
2114 info->new_layout,
2115 info->array.raid_disks);
2116 if (info->reshape_progress < position) {
2117 dprintf("Corrected reshape progress (%llu) to "
2118 "md position (%llu)\n",
2119 info->reshape_progress, position);
2120 info->reshape_progress = position;
2121 ret_val = 1;
2122 } else if (info->reshape_progress > position) {
2123 pr_err("Fatal error: array "
2124 "reshape was not properly frozen "
2125 "(expected reshape position is %llu, "
2126 "but reshape progress is %llu.\n",
2127 position, info->reshape_progress);
2128 ret_val = -1;
2129 } else {
2130 dprintf("Reshape position in md and metadata "
2131 "are the same;");
2132 ret_val = 1;
2133 }
2134 }
2135 } else if (rv == 0) {
2136 /* for valid sysfs entry, 0-length content
2137 * should be indicated as error
2138 */
2139 ret_val = -1;
2140 }
2141
2142 return ret_val;
2143 }
2144
2145 static int set_new_data_offset(struct mdinfo *sra, struct supertype *st,
2146 char *devname, int delta_disks,
2147 unsigned long long data_offset,
2148 unsigned long long min)
2149 {
2150 struct mdinfo *sd;
2151 int dir = 0;
2152 int err = 0;
2153
2154 for (sd = sra->devs; sd; sd = sd->next) {
2155 char *dn;
2156 int dfd;
2157 int rv;
2158 struct supertype *st2;
2159 struct mdinfo info2;
2160
2161 if (sd->disk.state & (1<<MD_DISK_FAULTY))
2162 continue;
2163 dn = map_dev(sd->disk.major, sd->disk.minor, 0);
2164 dfd = dev_open(dn, O_RDONLY);
2165 if (dfd < 0) {
2166 pr_err("%s: cannot open component %s\n",
2167 devname, dn ? dn : "-unknown-");
2168 goto release;
2169 }
2170 st2 = dup_super(st);
2171 rv = st2->ss->load_super(st2,dfd, NULL);
2172 close(dfd);
2173 if (rv) {
2174 free(st2);
2175 pr_err("%s: cannot get superblock from %s\n",
2176 devname, dn);
2177 goto release;
2178 }
2179 st2->ss->getinfo_super(st2, &info2, NULL);
2180 st2->ss->free_super(st2);
2181 free(st2);
2182 if (info2.space_before == 0 &&
2183 info2.space_after == 0) {
2184 /* Metadata doesn't support data_offset changes */
2185 return 1;
2186 }
2187 if (delta_disks < 0) {
2188 /* Don't need any space as array is shrinking
2189 * just move data_offset up by min
2190 */
2191 if (data_offset == INVALID_SECTORS)
2192 info2.new_data_offset = info2.data_offset + min;
2193 else {
2194 if (data_offset < info2.data_offset + min) {
2195 pr_err("--data-offset too small for %s\n",
2196 dn);
2197 goto release;
2198 }
2199 info2.new_data_offset = data_offset;
2200 }
2201 } else if (delta_disks > 0) {
2202 /* need space before */
2203 if (info2.space_before < min) {
2204 pr_err("Insufficient head-space for reshape on %s\n",
2205 dn);
2206 goto release;
2207 }
2208 if (data_offset == INVALID_SECTORS)
2209 info2.new_data_offset = info2.data_offset - min;
2210 else {
2211 if (data_offset > info2.data_offset - min) {
2212 pr_err("--data-offset too large for %s\n",
2213 dn);
2214 goto release;
2215 }
2216 info2.new_data_offset = data_offset;
2217 }
2218 } else {
2219 if (dir == 0) {
2220 /* can move up or down. 'data_offset'
2221 * might guide us, otherwise choose
2222 * direction with most space
2223 */
2224 if (data_offset == INVALID_SECTORS) {
2225 if (info2.space_before > info2.space_after)
2226 dir = -1;
2227 else
2228 dir = 1;
2229 } else if (data_offset < info2.data_offset)
2230 dir = -1;
2231 else
2232 dir = 1;
2233 sysfs_set_str(sra, NULL, "reshape_direction",
2234 dir == 1 ? "backwards" : "forwards");
2235 }
2236 switch (dir) {
2237 case 1: /* Increase data offset */
2238 if (info2.space_after < min) {
2239 pr_err("Insufficient tail-space for reshape on %s\n",
2240 dn);
2241 goto release;
2242 }
2243 if (data_offset != INVALID_SECTORS &&
2244 data_offset < info2.data_offset + min) {
2245 pr_err("--data-offset too small on %s\n",
2246 dn);
2247 goto release;
2248 }
2249 if (data_offset != INVALID_SECTORS)
2250 info2.new_data_offset = data_offset;
2251 else {
2252 unsigned long long off =
2253 info2.space_after / 2;
2254 off &= ~7ULL;
2255 if (off < min)
2256 off = min;
2257 info2.new_data_offset =
2258 info2.data_offset + off;
2259 }
2260 break;
2261 case -1: /* Decrease data offset */
2262 if (info2.space_before < min) {
2263 pr_err("insufficient head-room on %s\n",
2264 dn);
2265 goto release;
2266 }
2267 if (data_offset != INVALID_SECTORS &&
2268 data_offset < info2.data_offset - min) {
2269 pr_err("--data-offset too small on %s\n",
2270 dn);
2271 goto release;
2272 }
2273 if (data_offset != INVALID_SECTORS)
2274 info2.new_data_offset = data_offset;
2275 else {
2276 unsigned long long off =
2277 info2.space_before / 2;
2278 off &= ~7ULL;
2279 if (off < min)
2280 off = min;
2281 info2.new_data_offset =
2282 info2.data_offset - off;
2283 }
2284 break;
2285 }
2286 }
2287 if (sysfs_set_num(sra, sd, "new_offset",
2288 info2.new_data_offset) < 0) {
2289 err = errno;
2290 if (sd == sra->devs && err == ENOENT)
2291 /* Early kernel, no 'new_offset' file.
2292 * For RAID5/6 this is not fatal
2293 */
2294 return 1;
2295 pr_err("Cannot set new_offset for %s\n",
2296 dn);
2297 break;
2298 }
2299 }
2300 return err;
2301 release:
2302 return -1;
2303 }
2304
2305 static int raid10_reshape(char *container, int fd, char *devname,
2306 struct supertype *st, struct mdinfo *info,
2307 struct reshape *reshape,
2308 unsigned long long data_offset,
2309 int force, int verbose)
2310 {
2311 /* Changing raid_disks, layout, chunksize or possibly
2312 * just data_offset for a RAID10.
2313 * We must always change data_offset. We change by at least
2314 * ->min_offset_change which is the largest of the old and new
2315 * chunk sizes.
2316 * If raid_disks is increasing, then data_offset must decrease
2317 * by at least this copy size.
2318 * If raid_disks is unchanged, data_offset must increase or
2319 * decrease by at least min_offset_change but preferably by much more.
2320 * We choose half of the available space.
2321 * If raid_disks is decreasing, data_offset must increase by
2322 * at least min_offset_change. To allow of this, component_size
2323 * must be decreased by the same amount.
2324 *
2325 * So we calculate the required minimum and direction, possibly
2326 * reduce the component_size, then iterate through the devices
2327 * and set the new_data_offset.
2328 * If that all works, we set chunk_size, layout, raid_disks, and start
2329 * 'reshape'
2330 */
2331 struct mdinfo *sra;
2332 unsigned long long min;
2333 int err = 0;
2334
2335 sra = sysfs_read(fd, NULL,
2336 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK
2337 );
2338 if (!sra) {
2339 pr_err("%s: Cannot get array details from sysfs\n",
2340 devname);
2341 goto release;
2342 }
2343 min = reshape->min_offset_change;
2344
2345 if (info->delta_disks)
2346 sysfs_set_str(sra, NULL, "reshape_direction",
2347 info->delta_disks < 0 ? "backwards" : "forwards");
2348 if (info->delta_disks < 0 &&
2349 info->space_after < min) {
2350 int rv = sysfs_set_num(sra, NULL, "component_size",
2351 (sra->component_size -
2352 min)/2);
2353 if (rv) {
2354 pr_err("cannot reduce component size\n");
2355 goto release;
2356 }
2357 }
2358 err = set_new_data_offset(sra, st, devname, info->delta_disks, data_offset,
2359 min);
2360 if (err == 1) {
2361 pr_err("Cannot set new_data_offset: RAID10 reshape not\n");
2362 cont_err("supported on this kernel\n");
2363 err = -1;
2364 }
2365 if (err < 0)
2366 goto release;
2367
2368 if (!err && sysfs_set_num(sra, NULL, "chunk_size", info->new_chunk) < 0)
2369 err = errno;
2370 if (!err && sysfs_set_num(sra, NULL, "layout", reshape->after.layout) < 0)
2371 err = errno;
2372 if (!err && sysfs_set_num(sra, NULL, "raid_disks",
2373 info->array.raid_disks + info->delta_disks) < 0)
2374 err = errno;
2375 if (!err && sysfs_set_str(sra, NULL, "sync_action", "reshape") < 0)
2376 err = errno;
2377 if (err) {
2378 pr_err("Cannot set array shape for %s\n",
2379 devname);
2380 if (err == EBUSY &&
2381 (info->array.state & (1<<MD_SB_BITMAP_PRESENT)))
2382 cont_err(" Bitmap must be removed before"
2383 " shape can be changed\n");
2384 goto release;
2385 }
2386 sysfs_free(sra);
2387 return 0;
2388 release:
2389 sysfs_free(sra);
2390 return 1;
2391 }
2392
2393 static void get_space_after(int fd, struct supertype *st, struct mdinfo *info)
2394 {
2395 struct mdinfo *sra, *sd;
2396 /* Initialisation to silence compiler warning */
2397 unsigned long long min_space_before = 0, min_space_after = 0;
2398 int first = 1;
2399
2400 sra = sysfs_read(fd, NULL, GET_DEVS);
2401 if (!sra)
2402 return;
2403 for (sd = sra->devs; sd; sd = sd->next) {
2404 char *dn;
2405 int dfd;
2406 struct supertype *st2;
2407 struct mdinfo info2;
2408
2409 if (sd->disk.state & (1<<MD_DISK_FAULTY))
2410 continue;
2411 dn = map_dev(sd->disk.major, sd->disk.minor, 0);
2412 dfd = dev_open(dn, O_RDONLY);
2413 if (dfd < 0)
2414 break;
2415 st2 = dup_super(st);
2416 if (st2->ss->load_super(st2,dfd, NULL)) {
2417 close(dfd);
2418 free(st2);
2419 break;
2420 }
2421 close(dfd);
2422 st2->ss->getinfo_super(st2, &info2, NULL);
2423 st2->ss->free_super(st2);
2424 free(st2);
2425 if (first ||
2426 min_space_before > info2.space_before)
2427 min_space_before = info2.space_before;
2428 if (first ||
2429 min_space_after > info2.space_after)
2430 min_space_after = info2.space_after;
2431 first = 0;
2432 }
2433 if (sd == NULL && !first) {
2434 info->space_after = min_space_after;
2435 info->space_before = min_space_before;
2436 }
2437 sysfs_free(sra);
2438 }
2439
2440 static void update_cache_size(char *container, struct mdinfo *sra,
2441 struct mdinfo *info,
2442 int disks, unsigned long long blocks)
2443 {
2444 /* Check that the internal stripe cache is
2445 * large enough, or it won't work.
2446 * It must hold at least 4 stripes of the larger
2447 * chunk size
2448 */
2449 unsigned long cache;
2450 cache = max(info->array.chunk_size, info->new_chunk);
2451 cache *= 4; /* 4 stripes minimum */
2452 cache /= 512; /* convert to sectors */
2453 /* make sure there is room for 'blocks' with a bit to spare */
2454 if (cache < 16 + blocks / disks)
2455 cache = 16 + blocks / disks;
2456 cache /= (4096/512); /* Covert from sectors to pages */
2457
2458 if (sra->cache_size < cache)
2459 subarray_set_num(container, sra, "stripe_cache_size",
2460 cache+1);
2461 }
2462
2463 static int impose_reshape(struct mdinfo *sra,
2464 struct mdinfo *info,
2465 struct supertype *st,
2466 int fd,
2467 int restart,
2468 char *devname, char *container,
2469 struct reshape *reshape)
2470 {
2471 struct mdu_array_info_s array;
2472
2473 sra->new_chunk = info->new_chunk;
2474
2475 if (restart) {
2476 /* for external metadata checkpoint saved by mdmon can be lost
2477 * or missed /due to e.g. crash/. Check if md is not during
2478 * restart farther than metadata points to.
2479 * If so, this means metadata information is obsolete.
2480 */
2481 if (st->ss->external)
2482 verify_reshape_position(info, reshape->level);
2483 sra->reshape_progress = info->reshape_progress;
2484 } else {
2485 sra->reshape_progress = 0;
2486 if (reshape->after.data_disks < reshape->before.data_disks)
2487 /* start from the end of the new array */
2488 sra->reshape_progress = (sra->component_size
2489 * reshape->after.data_disks);
2490 }
2491
2492 ioctl(fd, GET_ARRAY_INFO, &array);
2493 if (info->array.chunk_size == info->new_chunk &&
2494 reshape->before.layout == reshape->after.layout &&
2495 st->ss->external == 0) {
2496 /* use SET_ARRAY_INFO but only if reshape hasn't started */
2497 array.raid_disks = reshape->after.data_disks + reshape->parity;
2498 if (!restart &&
2499 ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
2500 int err = errno;
2501
2502 pr_err("Cannot set device shape for %s: %s\n",
2503 devname, strerror(errno));
2504
2505 if (err == EBUSY &&
2506 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
2507 cont_err("Bitmap must be removed before"
2508 " shape can be changed\n");
2509
2510 goto release;
2511 }
2512 } else if (!restart) {
2513 /* set them all just in case some old 'new_*' value
2514 * persists from some earlier problem.
2515 */
2516 int err = 0;
2517 if (sysfs_set_num(sra, NULL, "chunk_size", info->new_chunk) < 0)
2518 err = errno;
2519 if (!err && sysfs_set_num(sra, NULL, "layout",
2520 reshape->after.layout) < 0)
2521 err = errno;
2522 if (!err && subarray_set_num(container, sra, "raid_disks",
2523 reshape->after.data_disks +
2524 reshape->parity) < 0)
2525 err = errno;
2526 if (err) {
2527 pr_err("Cannot set device shape for %s\n",
2528 devname);
2529
2530 if (err == EBUSY &&
2531 (array.state & (1<<MD_SB_BITMAP_PRESENT)))
2532 cont_err("Bitmap must be removed before"
2533 " shape can be changed\n");
2534 goto release;
2535 }
2536 }
2537 return 0;
2538 release:
2539 return -1;
2540 }
2541
2542 static int reshape_array(char *container, int fd, char *devname,
2543 struct supertype *st, struct mdinfo *info,
2544 int force, struct mddev_dev *devlist,
2545 unsigned long long data_offset,
2546 char *backup_file, int verbose, int forked,
2547 int restart, int freeze_reshape)
2548 {
2549 struct reshape reshape;
2550 int spares_needed;
2551 char *msg;
2552 int orig_level = UnSet;
2553 int odisks;
2554 int delayed;
2555
2556 struct mdu_array_info_s array;
2557 char *c;
2558
2559 struct mddev_dev *dv;
2560 int added_disks;
2561
2562 int *fdlist = NULL;
2563 unsigned long long *offsets = NULL;
2564 int d;
2565 int nrdisks;
2566 int err;
2567 unsigned long blocks;
2568 unsigned long long array_size;
2569 int done;
2570 struct mdinfo *sra = NULL;
2571
2572 /* when reshaping a RAID0, the component_size might be zero.
2573 * So try to fix that up.
2574 */
2575 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
2576 dprintf("Cannot get array information.\n");
2577 goto release;
2578 }
2579 if (array.level == 0 && info->component_size == 0) {
2580 get_dev_size(fd, NULL, &array_size);
2581 info->component_size = array_size / array.raid_disks;
2582 }
2583
2584 if (array.level == 10)
2585 /* Need space_after info */
2586 get_space_after(fd, st, info);
2587
2588 if (info->reshape_active) {
2589 int new_level = info->new_level;
2590 info->new_level = UnSet;
2591 if (info->delta_disks > 0)
2592 info->array.raid_disks -= info->delta_disks;
2593 msg = analyse_change(info, &reshape);
2594 info->new_level = new_level;
2595 if (info->delta_disks > 0)
2596 info->array.raid_disks += info->delta_disks;
2597 if (!restart)
2598 /* Make sure the array isn't read-only */
2599 ioctl(fd, RESTART_ARRAY_RW, 0);
2600 } else
2601 msg = analyse_change(info, &reshape);
2602 if (msg) {
2603 pr_err("%s\n", msg);
2604 goto release;
2605 }
2606 if (restart &&
2607 (reshape.level != info->array.level ||
2608 reshape.before.layout != info->array.layout ||
2609 reshape.before.data_disks + reshape.parity
2610 != info->array.raid_disks - max(0, info->delta_disks))) {
2611 pr_err("reshape info is not in native format -"
2612 " cannot continue.\n");
2613 goto release;
2614 }
2615
2616 if (st->ss->external && restart && (info->reshape_progress == 0)) {
2617 /* When reshape is restarted from '0', very begin of array
2618 * it is possible that for external metadata reshape and array
2619 * configuration doesn't happen.
2620 * Check if md has the same opinion, and reshape is restarted
2621 * from 0. If so, this is regular reshape start after reshape
2622 * switch in metadata to next array only.
2623 */
2624 if ((verify_reshape_position(info, reshape.level) >= 0) &&
2625 (info->reshape_progress == 0))
2626 restart = 0;
2627 }
2628 if (restart) {
2629 /* reshape already started. just skip to monitoring the reshape */
2630 if (reshape.backup_blocks == 0)
2631 return 0;
2632 goto started;
2633 }
2634 /* The container is frozen but the array may not be.
2635 * So freeze the array so spares don't get put to the wrong use
2636 * FIXME there should probably be a cleaner separation between
2637 * freeze_array and freeze_container.
2638 */
2639 sysfs_freeze_array(info);
2640 /* Check we have enough spares to not be degraded */
2641 added_disks = 0;
2642 for (dv = devlist; dv ; dv=dv->next)
2643 added_disks++;
2644 spares_needed = max(reshape.before.data_disks,
2645 reshape.after.data_disks)
2646 + reshape.parity - array.raid_disks;
2647
2648 if (!force &&
2649 info->new_level > 1 && info->array.level > 1 &&
2650 spares_needed > info->array.spare_disks + added_disks) {
2651 pr_err("Need %d spare%s to avoid degraded array,"
2652 " and only have %d.\n"
2653 " Use --force to over-ride this check.\n",
2654 spares_needed,
2655 spares_needed == 1 ? "" : "s",
2656 info->array.spare_disks + added_disks);
2657 goto release;
2658 }
2659 /* Check we have enough spares to not fail */
2660 spares_needed = max(reshape.before.data_disks,
2661 reshape.after.data_disks)
2662 - array.raid_disks;
2663 if ((info->new_level > 1 || info->new_level == 0) &&
2664 spares_needed > info->array.spare_disks +added_disks) {
2665 pr_err("Need %d spare%s to create working array,"
2666 " and only have %d.\n",
2667 spares_needed,
2668 spares_needed == 1 ? "" : "s",
2669 info->array.spare_disks + added_disks);
2670 goto release;
2671 }
2672
2673 if (reshape.level != array.level) {
2674 char *c = map_num(pers, reshape.level);
2675 int err;
2676 if (c == NULL)
2677 goto release;
2678
2679 err = sysfs_set_str(info, NULL, "level", c);
2680 if (err) {
2681 err = errno;
2682 pr_err("%s: could not set level to %s\n",
2683 devname, c);
2684 if (err == EBUSY &&
2685 (info->array.state & (1<<MD_SB_BITMAP_PRESENT)))
2686 cont_err("Bitmap must be removed"
2687 " before level can be changed\n");
2688 goto release;
2689 }
2690 if (verbose >= 0)
2691 pr_err("level of %s changed to %s\n",
2692 devname, c);
2693 orig_level = array.level;
2694 sysfs_freeze_array(info);
2695
2696 if (reshape.level > 0 && st->ss->external) {
2697 /* make sure mdmon is aware of the new level */
2698 if (mdmon_running(container))
2699 flush_mdmon(container);
2700
2701 if (!mdmon_running(container))
2702 start_mdmon(container);
2703 ping_monitor(container);
2704 if (mdmon_running(container) &&
2705 st->update_tail == NULL)
2706 st->update_tail = &st->updates;
2707 }
2708 }
2709 /* ->reshape_super might have chosen some spares from the
2710 * container that it wants to be part of the new array.
2711 * We can collect them with ->container_content and give
2712 * them to the kernel.
2713 */
2714 if (st->ss->reshape_super && st->ss->container_content) {
2715 char *subarray = strchr(info->text_version+1, '/')+1;
2716 struct mdinfo *info2 =
2717 st->ss->container_content(st, subarray);
2718 struct mdinfo *d;
2719
2720 if (info2) {
2721 sysfs_init(info2, fd, st->devnm);
2722 /* When increasing number of devices, we need to set
2723 * new raid_disks before adding these, or they might
2724 * be rejected.
2725 */
2726 if (reshape.backup_blocks &&
2727 reshape.after.data_disks > reshape.before.data_disks)
2728 subarray_set_num(container, info2, "raid_disks",
2729 reshape.after.data_disks +
2730 reshape.parity);
2731 for (d = info2->devs; d; d = d->next) {
2732 if (d->disk.state == 0 &&
2733 d->disk.raid_disk >= 0) {
2734 /* This is a spare that wants to
2735 * be part of the array.
2736 */
2737 add_disk(fd, st, info2, d);
2738 }
2739 }
2740 sysfs_free(info2);
2741 }
2742 }
2743 /* We might have been given some devices to add to the
2744 * array. Now that the array has been changed to the right
2745 * level and frozen, we can safely add them.
2746 */
2747 if (devlist)
2748 Manage_subdevs(devname, fd, devlist, verbose,
2749 0,NULL, 0);
2750
2751 if (reshape.backup_blocks == 0) {
2752 /* No restriping needed, but we might need to impose
2753 * some more changes: layout, raid_disks, chunk_size
2754 */
2755 /* read current array info */
2756 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
2757 dprintf("Cannot get array information.\n");
2758 goto release;
2759 }
2760 /* compare current array info with new values and if
2761 * it is different update them to new */
2762 if (info->new_layout != UnSet &&
2763 info->new_layout != array.layout) {
2764 array.layout = info->new_layout;
2765 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
2766 pr_err("failed to set new layout\n");
2767 goto release;
2768 } else if (verbose >= 0)
2769 printf("layout for %s set to %d\n",
2770 devname, array.layout);
2771 }
2772 if (info->delta_disks != UnSet &&
2773 info->delta_disks != 0 &&
2774 array.raid_disks != (info->array.raid_disks + info->delta_disks)) {
2775 array.raid_disks += info->delta_disks;
2776 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
2777 pr_err("failed to set raid disks\n");
2778 goto release;
2779 } else if (verbose >= 0) {
2780 printf("raid_disks for %s set to %d\n",
2781 devname, array.raid_disks);
2782 }
2783 }
2784 if (info->new_chunk != 0 &&
2785 info->new_chunk != array.chunk_size) {
2786 if (sysfs_set_num(info, NULL,
2787 "chunk_size", info->new_chunk) != 0) {
2788 pr_err("failed to set chunk size\n");
2789 goto release;
2790 } else if (verbose >= 0)
2791 printf("chunk size for %s set to %d\n",
2792 devname, array.chunk_size);
2793 }
2794 unfreeze(st);
2795 return 0;
2796 }
2797
2798 /*
2799 * There are three possibilities.
2800 * 1/ The array will shrink.
2801 * We need to ensure the reshape will pause before reaching
2802 * the 'critical section'. We also need to fork and wait for
2803 * that to happen. When it does we
2804 * suspend/backup/complete/unfreeze
2805 *
2806 * 2/ The array will not change size.
2807 * This requires that we keep a backup of a sliding window
2808 * so that we can restore data after a crash. So we need
2809 * to fork and monitor progress.
2810 * In future we will allow the data_offset to change, so
2811 * a sliding backup becomes unnecessary.
2812 *
2813 * 3/ The array will grow. This is relatively easy.
2814 * However the kernel's restripe routines will cheerfully
2815 * overwrite some early data before it is safe. So we
2816 * need to make a backup of the early parts of the array
2817 * and be ready to restore it if rebuild aborts very early.
2818 * For externally managed metadata, we still need a forked
2819 * child to monitor the reshape and suspend IO over the region
2820 * that is being reshaped.
2821 *
2822 * We backup data by writing it to one spare, or to a
2823 * file which was given on command line.
2824 *
2825 * In each case, we first make sure that storage is available
2826 * for the required backup.
2827 * Then we:
2828 * - request the shape change.
2829 * - fork to handle backup etc.
2830 */
2831 /* Check that we can hold all the data */
2832 get_dev_size(fd, NULL, &array_size);
2833 if (reshape.new_size < (array_size/512)) {
2834 pr_err("this change will reduce the size of the array.\n"
2835 " use --grow --array-size first to truncate array.\n"
2836 " e.g. mdadm --grow %s --array-size %llu\n",
2837 devname, reshape.new_size/2);
2838 goto release;
2839 }
2840
2841 started:
2842
2843 if (array.level == 10) {
2844 /* Reshaping RAID10 does not require any data backup by
2845 * user-space. Instead it requires that the data_offset
2846 * is changed to avoid the need for backup.
2847 * So this is handled very separately
2848 */
2849 if (restart)
2850 /* Nothing to do. */
2851 return 0;
2852 return raid10_reshape(container, fd, devname, st, info,
2853 &reshape, data_offset,
2854 force, verbose);
2855 }
2856 sra = sysfs_read(fd, NULL,
2857 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK|
2858 GET_CACHE);
2859 if (!sra) {
2860 pr_err("%s: Cannot get array details from sysfs\n",
2861 devname);
2862 goto release;
2863 }
2864
2865 switch(set_new_data_offset(sra, st, devname, info->delta_disks,
2866 data_offset,
2867 reshape.min_offset_change)) {
2868 case -1:
2869 goto release;
2870 case 0:
2871 /* Updated data_offset, so it's easy now */
2872 update_cache_size(container, sra, info,
2873 min(reshape.before.data_disks,
2874 reshape.after.data_disks),
2875 reshape.backup_blocks);
2876
2877 /* Right, everything seems fine. Let's kick things off.
2878 */
2879 sync_metadata(st);
2880
2881 if (impose_reshape(sra, info, st, fd, restart,
2882 devname, container, &reshape) < 0)
2883 goto release;
2884 if (sysfs_set_str(sra, NULL, "sync_action", "reshape") < 0) {
2885 pr_err("Failed to initiate reshape!\n");
2886 goto release;
2887 }
2888
2889 return 0;
2890 case 1: /* Couldn't set data_offset, try the old way */
2891 if (data_offset != INVALID_SECTORS) {
2892 pr_err("Cannot update data_offset on this array\n");
2893 goto release;
2894 }
2895 break;
2896 }
2897
2898 /* Decide how many blocks (sectors) for a reshape
2899 * unit. The number we have so far is just a minimum
2900 */
2901 blocks = reshape.backup_blocks;
2902 if (reshape.before.data_disks ==
2903 reshape.after.data_disks) {
2904 /* Make 'blocks' bigger for better throughput, but
2905 * not so big that we reject it below.
2906 * Try for 16 megabytes
2907 */
2908 while (blocks * 32 < sra->component_size &&
2909 blocks < 16*1024*2)
2910 blocks *= 2;
2911 } else
2912 pr_err("Need to backup %luK of critical "
2913 "section..\n", blocks/2);
2914
2915 if (blocks >= sra->component_size/2) {
2916 pr_err("%s: Something wrong"
2917 " - reshape aborted\n",
2918 devname);
2919 goto release;
2920 }
2921
2922 /* Now we need to open all these devices so we can read/write.
2923 */
2924 nrdisks = max(reshape.before.data_disks,
2925 reshape.after.data_disks) + reshape.parity
2926 + sra->array.spare_disks;
2927 fdlist = xcalloc((1+nrdisks), sizeof(int));
2928 offsets = xcalloc((1+nrdisks), sizeof(offsets[0]));
2929
2930 odisks = reshape.before.data_disks + reshape.parity;
2931 d = reshape_prepare_fdlist(devname, sra, odisks,
2932 nrdisks, blocks, backup_file,
2933 fdlist, offsets);
2934 if (d < 0) {
2935 goto release;
2936 }
2937 if ((st->ss->manage_reshape == NULL) ||
2938 (st->ss->recover_backup == NULL)) {
2939 if (backup_file == NULL) {
2940 if (reshape.after.data_disks <=
2941 reshape.before.data_disks) {
2942 pr_err("%s: Cannot grow - "
2943 "need backup-file\n", devname);
2944 goto release;
2945 } else if (sra->array.spare_disks == 0) {
2946 pr_err("%s: Cannot grow - "
2947 "need a spare or backup-file to backup "
2948 "critical section\n", devname);
2949 goto release;
2950 }
2951 } else {
2952 if (!reshape_open_backup_file(backup_file, fd, devname,
2953 (signed)blocks,
2954 fdlist+d, offsets+d,
2955 restart)) {
2956 goto release;
2957 }
2958 d++;
2959 }
2960 }
2961
2962 update_cache_size(container, sra, info,
2963 min(reshape.before.data_disks, reshape.after.data_disks),
2964 blocks);
2965
2966 /* Right, everything seems fine. Let's kick things off.
2967 * If only changing raid_disks, use ioctl, else use
2968 * sysfs.
2969 */
2970 sync_metadata(st);
2971
2972 if (impose_reshape(sra, info, st, fd, restart,
2973 devname, container, &reshape) < 0)
2974 goto release;
2975
2976 err = start_reshape(sra, restart, reshape.before.data_disks,
2977 reshape.after.data_disks);
2978 if (err) {
2979 pr_err("Cannot %s reshape for %s\n",
2980 restart ? "continue" : "start",
2981 devname);
2982 goto release;
2983 }
2984 if (restart)
2985 sysfs_set_str(sra, NULL, "array_state", "active");
2986 if (freeze_reshape) {
2987 free(fdlist);
2988 free(offsets);
2989 sysfs_free(sra);
2990 pr_err("Reshape has to be continued from"
2991 " location %llu when root filesystem has been mounted.\n",
2992 sra->reshape_progress);
2993 return 1;
2994 }
2995
2996 /* Now we just need to kick off the reshape and watch, while
2997 * handling backups of the data...
2998 * This is all done by a forked background process.
2999 */
3000 switch(forked ? 0 : fork()) {
3001 case -1:
3002 pr_err("Cannot run child to monitor reshape: %s\n",
3003 strerror(errno));
3004 abort_reshape(sra);
3005 goto release;
3006 default:
3007 free(fdlist);
3008 free(offsets);
3009 sysfs_free(sra);
3010 return 0;
3011 case 0:
3012 map_fork();
3013 break;
3014 }
3015
3016 /* If another array on the same devices is busy, the
3017 * reshape will wait for them. This would mean that
3018 * the first section that we suspend will stay suspended
3019 * for a long time. So check on that possibility
3020 * by looking for "DELAYED" in /proc/mdstat, and if found,
3021 * wait a while
3022 */
3023 do {
3024 struct mdstat_ent *mds, *m;
3025 delayed = 0;
3026 mds = mdstat_read(0, 0);
3027 for (m = mds; m; m = m->next)
3028 if (strcmp(m->devnm, sra->sys_name) == 0) {
3029 if (m->resync &&
3030 m->percent == RESYNC_DELAYED)
3031 delayed = 1;
3032 if (m->resync == 0)
3033 /* Haven't started the reshape thread
3034 * yet, wait a bit
3035 */
3036 delayed = 2;
3037 break;
3038 }
3039 free_mdstat(mds);
3040 if (delayed == 1 && get_linux_version() < 3007000) {
3041 pr_err("Reshape is delayed, but cannot wait carefully with this kernel.\n"
3042 " You might experience problems until other reshapes complete.\n");
3043 delayed = 0;
3044 }
3045 if (delayed)
3046 sleep(30 - (delayed-1) * 25);
3047 } while (delayed);
3048
3049 close(fd);
3050 if (check_env("MDADM_GROW_VERIFY"))
3051 fd = open(devname, O_RDONLY | O_DIRECT);
3052 else
3053 fd = -1;
3054 mlockall(MCL_FUTURE);
3055
3056 if (st->ss->external) {
3057 /* metadata handler takes it from here */
3058 done = st->ss->manage_reshape(
3059 fd, sra, &reshape, st, blocks,
3060 fdlist, offsets,
3061 d - odisks, fdlist+odisks,
3062 offsets+odisks);
3063 } else
3064 done = child_monitor(
3065 fd, sra, &reshape, st, blocks,
3066 fdlist, offsets,
3067 d - odisks, fdlist+odisks,
3068 offsets+odisks);
3069
3070 free(fdlist);
3071 free(offsets);
3072
3073 if (backup_file && done)
3074 unlink(backup_file);
3075 if (!done) {
3076 abort_reshape(sra);
3077 goto out;
3078 }
3079
3080 if (!st->ss->external &&
3081 !(reshape.before.data_disks != reshape.after.data_disks
3082 && info->custom_array_size) &&
3083 info->new_level == reshape.level &&
3084 !forked) {
3085 /* no need to wait for the reshape to finish as
3086 * there is nothing more to do.
3087 */
3088 sysfs_free(sra);
3089 exit(0);
3090 }
3091 wait_reshape(sra);
3092
3093 if (st->ss->external) {
3094 /* Re-load the metadata as much could have changed */
3095 int cfd = open_dev(st->container_devnm);
3096 if (cfd >= 0) {
3097 flush_mdmon(container);
3098 st->ss->free_super(st);
3099 st->ss->load_container(st, cfd, container);
3100 close(cfd);
3101 }
3102 }
3103
3104 /* set new array size if required customer_array_size is used
3105 * by this metadata.
3106 */
3107 if (reshape.before.data_disks !=
3108 reshape.after.data_disks &&
3109 info->custom_array_size)
3110 set_array_size(st, info, info->text_version);
3111
3112 if (info->new_level != reshape.level) {
3113
3114 c = map_num(pers, info->new_level);
3115 if (c) {
3116 err = sysfs_set_str(sra, NULL, "level", c);
3117 if (err)
3118 pr_err("%s: could not set level "
3119 "to %s\n", devname, c);
3120 }
3121 if (info->new_level == 0)
3122 st->update_tail = NULL;
3123 }
3124 out:
3125 sysfs_free(sra);
3126 if (forked)
3127 return 0;
3128 unfreeze(st);
3129 exit(0);
3130
3131 release:
3132 free(fdlist);
3133 free(offsets);
3134 if (orig_level != UnSet && sra) {
3135 c = map_num(pers, orig_level);
3136 if (c && sysfs_set_str(sra, NULL, "level", c) == 0)
3137 pr_err("aborting level change\n");
3138 }
3139 sysfs_free(sra);
3140 if (!forked)
3141 unfreeze(st);
3142 return 1;
3143 }
3144
3145 /* mdfd handle is passed to be closed in child process (after fork).
3146 */
3147 int reshape_container(char *container, char *devname,
3148 int mdfd,
3149 struct supertype *st,
3150 struct mdinfo *info,
3151 int force,
3152 char *backup_file,
3153 int verbose, int restart, int freeze_reshape)
3154 {
3155 struct mdinfo *cc = NULL;
3156 int rv = restart;
3157 char last_devnm[32] = "";
3158
3159 /* component_size is not meaningful for a container,
3160 * so pass '0' meaning 'no change'
3161 */
3162 if (!restart &&
3163 reshape_super(st, 0, info->new_level,
3164 info->new_layout, info->new_chunk,
3165 info->array.raid_disks, info->delta_disks,
3166 backup_file, devname, APPLY_METADATA_CHANGES,
3167 verbose)) {
3168 unfreeze(st);
3169 return 1;
3170 }
3171
3172 sync_metadata(st);
3173
3174 /* ping monitor to be sure that update is on disk
3175 */
3176 ping_monitor(container);
3177
3178 switch (fork()) {
3179 case -1: /* error */
3180 perror("Cannot fork to complete reshape\n");
3181 unfreeze(st);
3182 return 1;
3183 default: /* parent */
3184 if (!freeze_reshape)
3185 printf(Name ": multi-array reshape continues"
3186 " in background\n");
3187 return 0;
3188 case 0: /* child */
3189 map_fork();
3190 break;
3191 }
3192
3193 /* close unused handle in child process
3194 */
3195 if (mdfd > -1)
3196 close(mdfd);
3197
3198 while(1) {
3199 /* For each member array with reshape_active,
3200 * we need to perform the reshape.
3201 * We pick the first array that needs reshaping and
3202 * reshape it. reshape_array() will re-read the metadata
3203 * so the next time through a different array should be
3204 * ready for reshape.
3205 * It is possible that the 'different' array will not
3206 * be assembled yet. In that case we simple exit.
3207 * When it is assembled, the mdadm which assembles it
3208 * will take over the reshape.
3209 */
3210 struct mdinfo *content;
3211 int fd;
3212 struct mdstat_ent *mdstat;
3213 char *adev;
3214 int devid;
3215
3216 sysfs_free(cc);
3217
3218 cc = st->ss->container_content(st, NULL);
3219
3220 for (content = cc; content ; content = content->next) {
3221 char *subarray;
3222 if (!content->reshape_active)
3223 continue;
3224
3225 subarray = strchr(content->text_version+1, '/')+1;
3226 mdstat = mdstat_by_subdev(subarray, container);
3227 if (!mdstat)
3228 continue;
3229 if (mdstat->active == 0) {
3230 pr_err("Skipping inactive array %s.\n",
3231 mdstat->devnm);
3232 free_mdstat(mdstat);
3233 mdstat = NULL;
3234 continue;
3235 }
3236 break;
3237 }
3238 if (!content)
3239 break;
3240
3241 devid = devnm2devid(mdstat->devnm);
3242 adev = map_dev(major(devid), minor(devid), 0);
3243 if (!adev)
3244 adev = content->text_version;
3245
3246 fd = open_dev(mdstat->devnm);
3247 if (fd < 0) {
3248 printf(Name ": Device %s cannot be opened for reshape.",
3249 adev);
3250 break;
3251 }
3252
3253 if (strcmp(last_devnm, mdstat->devnm) == 0) {
3254 /* Do not allow for multiple reshape_array() calls for
3255 * the same array.
3256 * It can happen when reshape_array() returns without
3257 * error, when reshape is not finished (wrong reshape
3258 * starting/continuation conditions). Mdmon doesn't
3259 * switch to next array in container and reentry
3260 * conditions for the same array occur.
3261 * This is possibly interim until the behaviour of
3262 * reshape_array is resolved().
3263 */
3264 printf(Name ": Multiple reshape execution detected for "
3265 "device %s.", adev);
3266 close(fd);
3267 break;
3268 }
3269 strcpy(last_devnm, mdstat->devnm);
3270
3271 sysfs_init(content, fd, mdstat->devnm);
3272
3273 if (mdmon_running(container))
3274 flush_mdmon(container);
3275
3276 rv = reshape_array(container, fd, adev, st,
3277 content, force, NULL, 0ULL,
3278 backup_file, verbose, 1, restart,
3279 freeze_reshape);
3280 close(fd);
3281
3282 if (freeze_reshape) {
3283 sysfs_free(cc);
3284 exit(0);
3285 }
3286
3287 restart = 0;
3288 if (rv)
3289 break;
3290
3291 if (mdmon_running(container))
3292 flush_mdmon(container);
3293 }
3294 if (!rv)
3295 unfreeze(st);
3296 sysfs_free(cc);
3297 exit(0);
3298 }
3299
3300 /*
3301 * We run a child process in the background which performs the following
3302 * steps:
3303 * - wait for resync to reach a certain point
3304 * - suspend io to the following section
3305 * - backup that section
3306 * - allow resync to proceed further
3307 * - resume io
3308 * - discard the backup.
3309 *
3310 * When are combined in slightly different ways in the three cases.
3311 * Grow:
3312 * - suspend/backup/allow/wait/resume/discard
3313 * Shrink:
3314 * - allow/wait/suspend/backup/allow/wait/resume/discard
3315 * same-size:
3316 * - wait/resume/discard/suspend/backup/allow
3317 *
3318 * suspend/backup/allow always come together
3319 * wait/resume/discard do too.
3320 * For the same-size case we have two backups to improve flow.
3321 *
3322 */
3323
3324 int progress_reshape(struct mdinfo *info, struct reshape *reshape,
3325 unsigned long long backup_point,
3326 unsigned long long wait_point,
3327 unsigned long long *suspend_point,
3328 unsigned long long *reshape_completed)
3329 {
3330 /* This function is called repeatedly by the reshape manager.
3331 * It determines how much progress can safely be made and allows
3332 * that progress.
3333 * - 'info' identifies the array and particularly records in
3334 * ->reshape_progress the metadata's knowledge of progress
3335 * This is a sector offset from the start of the array
3336 * of the next array block to be relocated. This number
3337 * may increase from 0 or decrease from array_size, depending
3338 * on the type of reshape that is happening.
3339 * Note that in contrast, 'sync_completed' is a block count of the
3340 * reshape so far. It gives the distance between the start point
3341 * (head or tail of device) and the next place that data will be
3342 * written. It always increases.
3343 * - 'reshape' is the structure created by analyse_change
3344 * - 'backup_point' shows how much the metadata manager has backed-up
3345 * data. For reshapes with increasing progress, it is the next address
3346 * to be backed up, previous addresses have been backed-up. For
3347 * decreasing progress, it is the earliest address that has been
3348 * backed up - later address are also backed up.
3349 * So addresses between reshape_progress and backup_point are
3350 * backed up providing those are in the 'correct' order.
3351 * - 'wait_point' is an array address. When reshape_completed
3352 * passes this point, progress_reshape should return. It might
3353 * return earlier if it determines that ->reshape_progress needs
3354 * to be updated or further backup is needed.
3355 * - suspend_point is maintained by progress_reshape and the caller
3356 * should not touch it except to initialise to zero.
3357 * It is an array address and it only increases in 2.6.37 and earlier.
3358 * This makes it difficult to handle reducing reshapes with
3359 * external metadata.
3360 * However: it is similar to backup_point in that it records the
3361 * other end of a suspended region from reshape_progress.
3362 * it is moved to extend the region that is safe to backup and/or
3363 * reshape
3364 * - reshape_completed is read from sysfs and returned. The caller
3365 * should copy this into ->reshape_progress when it has reason to
3366 * believe that the metadata knows this, and any backup outside this
3367 * has been erased.
3368 *
3369 * Return value is:
3370 * 1 if more data from backup_point - but only as far as suspend_point,
3371 * should be backed up
3372 * 0 if things are progressing smoothly
3373 * -1 if the reshape is finished because it is all done,
3374 * -2 if the reshape is finished due to an error.
3375 */
3376
3377 int advancing = (reshape->after.data_disks
3378 >= reshape->before.data_disks);
3379 unsigned long long need_backup; /* All data between start of array and
3380 * here will at some point need to
3381 * be backed up.
3382 */
3383 unsigned long long read_offset, write_offset;
3384 unsigned long long write_range;
3385 unsigned long long max_progress, target, completed;
3386 unsigned long long array_size = (info->component_size
3387 * reshape->before.data_disks);
3388 int fd;
3389 char buf[20];
3390
3391 /* First, we unsuspend any region that is now known to be safe.
3392 * If suspend_point is on the 'wrong' side of reshape_progress, then
3393 * we don't have or need suspension at the moment. This is true for
3394 * native metadata when we don't need to back-up.
3395 */
3396 if (advancing) {
3397 if (info->reshape_progress <= *suspend_point)
3398 sysfs_set_num(info, NULL, "suspend_lo",
3399 info->reshape_progress);
3400 } else {
3401 /* Note: this won't work in 2.6.37 and before.
3402 * Something somewhere should make sure we don't need it!
3403 */
3404 if (info->reshape_progress >= *suspend_point)
3405 sysfs_set_num(info, NULL, "suspend_hi",
3406 info->reshape_progress);
3407 }
3408
3409 /* Now work out how far it is safe to progress.
3410 * If the read_offset for ->reshape_progress is less than
3411 * 'blocks' beyond the write_offset, we can only progress as far
3412 * as a backup.
3413 * Otherwise we can progress until the write_offset for the new location
3414 * reaches (within 'blocks' of) the read_offset at the current location.
3415 * However that region must be suspended unless we are using native
3416 * metadata.
3417 * If we need to suspend more, we limit it to 128M per device, which is
3418 * rather arbitrary and should be some time-based calculation.
3419 */
3420 read_offset = info->reshape_progress / reshape->before.data_disks;
3421 write_offset = info->reshape_progress / reshape->after.data_disks;
3422 write_range = info->new_chunk/512;
3423 if (reshape->before.data_disks == reshape->after.data_disks)
3424 need_backup = array_size;
3425 else
3426 need_backup = reshape->backup_blocks;
3427 if (advancing) {
3428 if (read_offset < write_offset + write_range)
3429 max_progress = backup_point;
3430 else
3431 max_progress =
3432 read_offset *
3433 reshape->after.data_disks;
3434 } else {
3435 if (read_offset > write_offset - write_range)
3436 /* Can only progress as far as has been backed up,
3437 * which must be suspended */
3438 max_progress = backup_point;
3439 else if (info->reshape_progress <= need_backup)
3440 max_progress = backup_point;
3441 else {
3442 if (info->array.major_version >= 0)
3443 /* Can progress until backup is needed */
3444 max_progress = need_backup;
3445 else {
3446 /* Can progress until metadata update is required */
3447 max_progress =
3448 read_offset *
3449 reshape->after.data_disks;
3450 /* but data must be suspended */
3451 if (max_progress < *suspend_point)
3452 max_progress = *suspend_point;
3453 }
3454 }
3455 }
3456
3457 /* We know it is safe to progress to 'max_progress' providing
3458 * it is suspended or we are using native metadata.
3459 * Consider extending suspend_point 128M per device if it
3460 * is less than 64M per device beyond reshape_progress.
3461 * But always do a multiple of 'blocks'
3462 * FIXME this is too big - it takes to long to complete
3463 * this much.
3464 */
3465 target = 64*1024*2 * min(reshape->before.data_disks,
3466 reshape->after.data_disks);
3467 target /= reshape->backup_blocks;
3468 if (target < 2)
3469 target = 2;
3470 target *= reshape->backup_blocks;
3471
3472 /* For externally managed metadata we always need to suspend IO to
3473 * the area being reshaped so we regularly push suspend_point forward.
3474 * For native metadata we only need the suspend if we are going to do
3475 * a backup.
3476 */
3477 if (advancing) {
3478 if ((need_backup > info->reshape_progress
3479 || info->array.major_version < 0) &&
3480 *suspend_point < info->reshape_progress + target) {
3481 if (need_backup < *suspend_point + 2 * target)
3482 *suspend_point = need_backup;
3483 else if (*suspend_point + 2 * target < array_size)
3484 *suspend_point += 2 * target;
3485 else
3486 *suspend_point = array_size;
3487 sysfs_set_num(info, NULL, "suspend_hi", *suspend_point);
3488 if (max_progress > *suspend_point)
3489 max_progress = *suspend_point;
3490 }
3491 } else {
3492 if (info->array.major_version >= 0) {
3493 /* Only need to suspend when about to backup */
3494 if (info->reshape_progress < need_backup * 2 &&
3495 *suspend_point > 0) {
3496 *suspend_point = 0;
3497 sysfs_set_num(info, NULL, "suspend_lo", 0);
3498 sysfs_set_num(info, NULL, "suspend_hi", need_backup);
3499 }
3500 } else {
3501 /* Need to suspend continually */
3502 if (info->reshape_progress < *suspend_point)
3503 *suspend_point = info->reshape_progress;
3504 if (*suspend_point + target < info->reshape_progress)
3505 /* No need to move suspend region yet */;
3506 else {
3507 if (*suspend_point >= 2 * target)
3508 *suspend_point -= 2 * target;
3509 else
3510 *suspend_point = 0;
3511 sysfs_set_num(info, NULL, "suspend_lo",
3512 *suspend_point);
3513 }
3514 if (max_progress < *suspend_point)
3515 max_progress = *suspend_point;
3516 }
3517 }
3518
3519 /* now set sync_max to allow that progress. sync_max, like
3520 * sync_completed is a count of sectors written per device, so
3521 * we find the difference between max_progress and the start point,
3522 * and divide that by after.data_disks to get a sync_max
3523 * number.
3524 * At the same time we convert wait_point to a similar number
3525 * for comparing against sync_completed.
3526 */
3527 /* scale down max_progress to per_disk */
3528 max_progress /= reshape->after.data_disks;
3529 /* Round to chunk size as some kernels give an erroneously high number */
3530 max_progress /= info->new_chunk/512;
3531 max_progress *= info->new_chunk/512;
3532 /* And round to old chunk size as the kernel wants that */
3533 max_progress /= info->array.chunk_size/512;
3534 max_progress *= info->array.chunk_size/512;
3535 /* Limit progress to the whole device */
3536 if (max_progress > info->component_size)
3537 max_progress = info->component_size;
3538 wait_point /= reshape->after.data_disks;
3539 if (!advancing) {
3540 /* switch from 'device offset' to 'processed block count' */
3541 max_progress = info->component_size - max_progress;
3542 wait_point = info->component_size - wait_point;
3543 }
3544
3545 sysfs_set_num(info, NULL, "sync_max", max_progress);
3546
3547 /* Now wait. If we have already reached the point that we were
3548 * asked to wait to, don't wait at all, else wait for any change.
3549 * We need to select on 'sync_completed' as that is the place that
3550 * notifications happen, but we are really interested in
3551 * 'reshape_position'
3552 */
3553 fd = sysfs_get_fd(info, NULL, "sync_completed");
3554 if (fd < 0)
3555 goto check_progress;
3556
3557 if (sysfs_fd_get_ll(fd, &completed) < 0)
3558 goto check_progress;
3559
3560 while (completed < max_progress && completed < wait_point) {
3561 /* Check that sync_action is still 'reshape' to avoid
3562 * waiting forever on a dead array
3563 */
3564 char action[20];
3565 fd_set rfds;
3566 if (sysfs_get_str(info, NULL, "sync_action",
3567 action, 20) <= 0 ||
3568 strncmp(action, "reshape", 7) != 0)
3569 break;
3570 /* Some kernels reset 'sync_completed' to zero
3571 * before setting 'sync_action' to 'idle'.
3572 * So we need these extra tests.
3573 */
3574 if (completed == 0 && advancing
3575 && info->reshape_progress > 0)
3576 break;
3577 if (completed == 0 && !advancing
3578 && info->reshape_progress < (info->component_size
3579 * reshape->after.data_disks))
3580 break;
3581 FD_ZERO(&rfds);
3582 FD_SET(fd, &rfds);
3583 select(fd+1, NULL, NULL, &rfds, NULL);
3584 if (sysfs_fd_get_ll(fd, &completed) < 0)
3585 goto check_progress;
3586 }
3587 /* Some kernels reset 'sync_completed' to zero,
3588 * we need to have real point we are in md
3589 */
3590 if (completed == 0)
3591 completed = max_progress;
3592
3593 /* some kernels can give an incorrectly high 'completed' number */
3594 completed /= (info->new_chunk/512);
3595 completed *= (info->new_chunk/512);
3596 /* Convert 'completed' back in to a 'progress' number */
3597 completed *= reshape->after.data_disks;
3598 if (!advancing) {
3599 completed = info->component_size * reshape->after.data_disks
3600 - completed;
3601 }
3602 *reshape_completed = completed;
3603
3604 close(fd);
3605
3606 /* We return the need_backup flag. Caller will decide
3607 * how much - a multiple of ->backup_blocks up to *suspend_point
3608 */
3609 if (advancing)
3610 return need_backup > info->reshape_progress;
3611 else
3612 return need_backup >= info->reshape_progress;
3613
3614 check_progress:
3615 /* if we couldn't read a number from sync_completed, then
3616 * either the reshape did complete, or it aborted.
3617 * We can tell which by checking for 'none' in reshape_position.
3618 * If it did abort, then it might immediately restart if it
3619 * it was just a device failure that leaves us degraded but
3620 * functioning.
3621 */
3622 strcpy(buf, "hi");
3623 if (sysfs_get_str(info, NULL, "reshape_position", buf, sizeof(buf)) < 0
3624 || strncmp(buf, "none", 4) != 0) {
3625 /* The abort might only be temporary. Wait up to 10
3626 * seconds for fd to contain a valid number again.
3627 */
3628 struct timeval tv;
3629 int rv = -2;
3630 tv.tv_sec = 10;
3631 tv.tv_usec = 0;
3632 while (fd >= 0 && rv < 0 && tv.tv_sec > 0) {
3633 fd_set rfds;
3634 FD_ZERO(&rfds);
3635 FD_SET(fd, &rfds);
3636 if (select(fd+1, NULL, NULL, &rfds, &tv) != 1)
3637 break;
3638 switch (sysfs_fd_get_ll(fd, &completed)) {
3639 case 0:
3640 /* all good again */
3641 rv = 1;
3642 break;
3643 case -2: /* read error - abort */
3644 tv.tv_sec = 0;
3645 break;
3646 }
3647 }
3648 if (fd >= 0)
3649 close(fd);
3650 return rv; /* abort */
3651 } else {
3652 /* Maybe racing with array shutdown - check state */
3653 if (fd >= 0)
3654 close(fd);
3655 if (sysfs_get_str(info, NULL, "array_state", buf, sizeof(buf)) < 0
3656 || strncmp(buf, "inactive", 8) == 0
3657 || strncmp(buf, "clear",5) == 0)
3658 return -2; /* abort */
3659 return -1; /* complete */
3660 }
3661 }
3662
3663 /* FIXME return status is never checked */
3664 static int grow_backup(struct mdinfo *sra,
3665 unsigned long long offset, /* per device */
3666 unsigned long stripes, /* per device, in old chunks */
3667 int *sources, unsigned long long *offsets,
3668 int disks, int chunk, int level, int layout,
3669 int dests, int *destfd, unsigned long long *destoffsets,
3670 int part, int *degraded,
3671 char *buf)
3672 {
3673 /* Backup 'blocks' sectors at 'offset' on each device of the array,
3674 * to storage 'destfd' (offset 'destoffsets'), after first
3675 * suspending IO. Then allow resync to continue
3676 * over the suspended section.
3677 * Use part 'part' of the backup-super-block.
3678 */
3679 int odata = disks;
3680 int rv = 0;
3681 int i;
3682 unsigned long long ll;
3683 int new_degraded;
3684 //printf("offset %llu\n", offset);
3685 if (level >= 4)
3686 odata--;
3687 if (level == 6)
3688 odata--;
3689
3690 /* Check that array hasn't become degraded, else we might backup the wrong data */
3691 if (sysfs_get_ll(sra, NULL, "degraded", &ll) < 0)
3692 return -1; /* FIXME this error is ignored */
3693 new_degraded = (int)ll;
3694 if (new_degraded != *degraded) {
3695 /* check each device to ensure it is still working */
3696 struct mdinfo *sd;
3697 for (sd = sra->devs ; sd ; sd = sd->next) {
3698 if (sd->disk.state & (1<<MD_DISK_FAULTY))
3699 continue;
3700 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
3701 char sbuf[20];
3702 if (sysfs_get_str(sra, sd, "state", sbuf, 20) < 0 ||
3703 strstr(sbuf, "faulty") ||
3704 strstr(sbuf, "in_sync") == NULL) {
3705 /* this device is dead */
3706 sd->disk.state = (1<<MD_DISK_FAULTY);
3707 if (sd->disk.raid_disk >= 0 &&
3708 sources[sd->disk.raid_disk] >= 0) {
3709 close(sources[sd->disk.raid_disk]);
3710 sources[sd->disk.raid_disk] = -1;
3711 }
3712 }
3713 }
3714 }
3715 *degraded = new_degraded;
3716 }
3717 if (part) {
3718 bsb.arraystart2 = __cpu_to_le64(offset * odata);
3719 bsb.length2 = __cpu_to_le64(stripes * (chunk/512) * odata);
3720 } else {
3721 bsb.arraystart = __cpu_to_le64(offset * odata);
3722 bsb.length = __cpu_to_le64(stripes * (chunk/512) * odata);
3723 }
3724 if (part)
3725 bsb.magic[15] = '2';
3726 for (i = 0; i < dests; i++)
3727 if (part)
3728 lseek64(destfd[i], destoffsets[i] + __le64_to_cpu(bsb.devstart2)*512, 0);
3729 else
3730 lseek64(destfd[i], destoffsets[i], 0);
3731
3732 rv = save_stripes(sources, offsets,
3733 disks, chunk, level, layout,
3734 dests, destfd,
3735 offset*512*odata, stripes * chunk * odata,
3736 buf);
3737
3738 if (rv)
3739 return rv;
3740 bsb.mtime = __cpu_to_le64(time(0));
3741 for (i = 0; i < dests; i++) {
3742 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
3743
3744 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
3745 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
3746 bsb.sb_csum2 = bsb_csum((char*)&bsb,
3747 ((char*)&bsb.sb_csum2)-((char*)&bsb));
3748
3749 rv = -1;
3750 if ((unsigned long long)lseek64(destfd[i], destoffsets[i] - 4096, 0)
3751 != destoffsets[i] - 4096)
3752 break;
3753 if (write(destfd[i], &bsb, 512) != 512)
3754 break;
3755 if (destoffsets[i] > 4096) {
3756 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]+stripes*chunk*odata, 0) !=
3757 destoffsets[i]+stripes*chunk*odata)
3758 break;
3759 if (write(destfd[i], &bsb, 512) != 512)
3760 break;
3761 }
3762 fsync(destfd[i]);
3763 rv = 0;
3764 }
3765
3766 return rv;
3767 }
3768
3769 /* in 2.6.30, the value reported by sync_completed can be
3770 * less that it should be by one stripe.
3771 * This only happens when reshape hits sync_max and pauses.
3772 * So allow wait_backup to either extent sync_max further
3773 * than strictly necessary, or return before the
3774 * sync has got quite as far as we would really like.
3775 * This is what 'blocks2' is for.
3776 * The various caller give appropriate values so that
3777 * every works.
3778 */
3779 /* FIXME return value is often ignored */
3780 static int forget_backup(int dests, int *destfd,
3781 unsigned long long *destoffsets,
3782 int part)
3783 {
3784 /*
3785 * Erase backup 'part' (which is 0 or 1)
3786 */
3787 int i;
3788 int rv;
3789
3790 if (part) {
3791 bsb.arraystart2 = __cpu_to_le64(0);
3792 bsb.length2 = __cpu_to_le64(0);
3793 } else {
3794 bsb.arraystart = __cpu_to_le64(0);
3795 bsb.length = __cpu_to_le64(0);
3796 }
3797 bsb.mtime = __cpu_to_le64(time(0));
3798 rv = 0;
3799 for (i = 0; i < dests; i++) {
3800 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
3801 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
3802 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
3803 bsb.sb_csum2 = bsb_csum((char*)&bsb,
3804 ((char*)&bsb.sb_csum2)-((char*)&bsb));
3805 if ((unsigned long long)lseek64(destfd[i], destoffsets[i]-4096, 0) !=
3806 destoffsets[i]-4096)
3807 rv = -1;
3808 if (rv == 0 &&
3809 write(destfd[i], &bsb, 512) != 512)
3810 rv = -1;
3811 fsync(destfd[i]);
3812 }
3813 return rv;
3814 }
3815
3816 static void fail(char *msg)
3817 {
3818 int rv;
3819 rv = (write(2, msg, strlen(msg)) != (int)strlen(msg));
3820 rv |= (write(2, "\n", 1) != 1);
3821 exit(rv ? 1 : 2);
3822 }
3823
3824 static char *abuf, *bbuf;
3825 static unsigned long long abuflen;
3826 static void validate(int afd, int bfd, unsigned long long offset)
3827 {
3828 /* check that the data in the backup against the array.
3829 * This is only used for regression testing and should not
3830 * be used while the array is active
3831 */
3832 if (afd < 0)
3833 return;
3834 lseek64(bfd, offset - 4096, 0);
3835 if (read(bfd, &bsb2, 512) != 512)
3836 fail("cannot read bsb");
3837 if (bsb2.sb_csum != bsb_csum((char*)&bsb2,
3838 ((char*)&bsb2.sb_csum)-((char*)&bsb2)))
3839 fail("first csum bad");
3840 if (memcmp(bsb2.magic, "md_backup_data", 14) != 0)
3841 fail("magic is bad");
3842 if (memcmp(bsb2.magic, "md_backup_data-2", 16) == 0 &&
3843 bsb2.sb_csum2 != bsb_csum((char*)&bsb2,
3844 ((char*)&bsb2.sb_csum2)-((char*)&bsb2)))
3845 fail("second csum bad");
3846
3847 if (__le64_to_cpu(bsb2.devstart)*512 != offset)
3848 fail("devstart is wrong");
3849
3850 if (bsb2.length) {
3851 unsigned long long len = __le64_to_cpu(bsb2.length)*512;
3852
3853 if (abuflen < len) {
3854 free(abuf);
3855 free(bbuf);
3856 abuflen = len;
3857 if (posix_memalign((void**)&abuf, 4096, abuflen) ||
3858 posix_memalign((void**)&bbuf, 4096, abuflen)) {
3859 abuflen = 0;
3860 /* just stop validating on mem-alloc failure */
3861 return;
3862 }
3863 }
3864
3865 lseek64(bfd, offset, 0);
3866 if ((unsigned long long)read(bfd, bbuf, len) != len) {
3867 //printf("len %llu\n", len);
3868 fail("read first backup failed");
3869 }
3870 lseek64(afd, __le64_to_cpu(bsb2.arraystart)*512, 0);
3871 if ((unsigned long long)read(afd, abuf, len) != len)
3872 fail("read first from array failed");
3873 if (memcmp(bbuf, abuf, len) != 0) {
3874 #if 0
3875 int i;
3876 printf("offset=%llu len=%llu\n",
3877 (unsigned long long)__le64_to_cpu(bsb2.arraystart)*512, len);
3878 for (i=0; i<len; i++)
3879 if (bbuf[i] != abuf[i]) {
3880 printf("first diff byte %d\n", i);
3881 break;
3882 }
3883 #endif
3884 fail("data1 compare failed");
3885 }
3886 }
3887 if (bsb2.length2) {
3888 unsigned long long len = __le64_to_cpu(bsb2.length2)*512;
3889
3890 if (abuflen < len) {
3891 free(abuf);
3892 free(bbuf);
3893 abuflen = len;
3894 abuf = xmalloc(abuflen);
3895 bbuf = xmalloc(abuflen);
3896 }
3897
3898 lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0);
3899 if ((unsigned long long)read(bfd, bbuf, len) != len)
3900 fail("read second backup failed");
3901 lseek64(afd, __le64_to_cpu(bsb2.arraystart2)*512, 0);
3902 if ((unsigned long long)read(afd, abuf, len) != len)
3903 fail("read second from array failed");
3904 if (memcmp(bbuf, abuf, len) != 0)
3905 fail("data2 compare failed");
3906 }
3907 }
3908
3909 int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
3910 struct supertype *st, unsigned long blocks,
3911 int *fds, unsigned long long *offsets,
3912 int dests, int *destfd, unsigned long long *destoffsets)
3913 {
3914 /* Monitor a reshape where backup is being performed using
3915 * 'native' mechanism - either to a backup file, or
3916 * to some space in a spare.
3917 */
3918 char *buf;
3919 int degraded = -1;
3920 unsigned long long speed;
3921 unsigned long long suspend_point, array_size;
3922 unsigned long long backup_point, wait_point;
3923 unsigned long long reshape_completed;
3924 int done = 0;
3925 int increasing = reshape->after.data_disks >= reshape->before.data_disks;
3926 int part = 0; /* The next part of the backup area to fill. It may already
3927 * be full, so we need to check */
3928 int level = reshape->level;
3929 int layout = reshape->before.layout;
3930 int data = reshape->before.data_disks;
3931 int disks = reshape->before.data_disks + reshape->parity;
3932 int chunk = sra->array.chunk_size;
3933 struct mdinfo *sd;
3934 unsigned long stripes;
3935 int uuid[4];
3936
3937 /* set up the backup-super-block. This requires the
3938 * uuid from the array.
3939 */
3940 /* Find a superblock */
3941 for (sd = sra->devs; sd; sd = sd->next) {
3942 char *dn;
3943 int devfd;
3944 int ok;
3945 if (sd->disk.state & (1<<MD_DISK_FAULTY))
3946 continue;
3947 dn = map_dev(sd->disk.major, sd->disk.minor, 1);
3948 devfd = dev_open(dn, O_RDONLY);
3949 if (devfd < 0)
3950 continue;
3951 ok = st->ss->load_super(st, devfd, NULL);
3952 close(devfd);
3953 if (ok == 0)
3954 break;
3955 }
3956 if (!sd) {
3957 pr_err("Cannot find a superblock\n");
3958 return 0;
3959 }
3960
3961 memset(&bsb, 0, 512);
3962 memcpy(bsb.magic, "md_backup_data-1", 16);
3963 st->ss->uuid_from_super(st, uuid);
3964 memcpy(bsb.set_uuid, uuid, 16);
3965 bsb.mtime = __cpu_to_le64(time(0));
3966 bsb.devstart2 = blocks;
3967
3968 stripes = blocks / (sra->array.chunk_size/512) /
3969 reshape->before.data_disks;
3970
3971 if (posix_memalign((void**)&buf, 4096, disks * chunk))
3972 /* Don't start the 'reshape' */
3973 return 0;
3974 if (reshape->before.data_disks == reshape->after.data_disks) {
3975 sysfs_get_ll(sra, NULL, "sync_speed_min", &speed);
3976 sysfs_set_num(sra, NULL, "sync_speed_min", 200000);
3977 }
3978
3979 if (increasing) {
3980 array_size = sra->component_size * reshape->after.data_disks;
3981 backup_point = sra->reshape_progress;
3982 suspend_point = 0;
3983 } else {
3984 array_size = sra->component_size * reshape->before.data_disks;
3985 backup_point = reshape->backup_blocks;
3986 suspend_point = array_size;
3987 }
3988
3989 while (!done) {
3990 int rv;
3991
3992 /* Want to return as soon the oldest backup slot can
3993 * be released as that allows us to start backing up
3994 * some more, providing suspend_point has been
3995 * advanced, which it should have.
3996 */
3997 if (increasing) {
3998 wait_point = array_size;
3999 if (part == 0 && __le64_to_cpu(bsb.length) > 0)
4000 wait_point = (__le64_to_cpu(bsb.arraystart) +
4001 __le64_to_cpu(bsb.length));
4002 if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
4003 wait_point = (__le64_to_cpu(bsb.arraystart2) +
4004 __le64_to_cpu(bsb.length2));
4005 } else {
4006 wait_point = 0;
4007 if (part == 0 && __le64_to_cpu(bsb.length) > 0)
4008 wait_point = __le64_to_cpu(bsb.arraystart);
4009 if (part == 1 && __le64_to_cpu(bsb.length2) > 0)
4010 wait_point = __le64_to_cpu(bsb.arraystart2);
4011 }
4012
4013 rv = progress_reshape(sra, reshape,
4014 backup_point, wait_point,
4015 &suspend_point, &reshape_completed);
4016 /* external metadata would need to ping_monitor here */
4017 sra->reshape_progress = reshape_completed;
4018
4019 /* Clear any backup region that is before 'here' */
4020 if (increasing) {
4021 if (__le64_to_cpu(bsb.length) > 0 &&
4022 reshape_completed >= (__le64_to_cpu(bsb.arraystart) +
4023 __le64_to_cpu(bsb.length)))
4024 forget_backup(dests, destfd,
4025 destoffsets, 0);
4026 if (__le64_to_cpu(bsb.length2) > 0 &&
4027 reshape_completed >= (__le64_to_cpu(bsb.arraystart2) +
4028 __le64_to_cpu(bsb.length2)))
4029 forget_backup(dests, destfd,
4030 destoffsets, 1);
4031 } else {
4032 if (__le64_to_cpu(bsb.length) > 0 &&
4033 reshape_completed <= (__le64_to_cpu(bsb.arraystart)))
4034 forget_backup(dests, destfd,
4035 destoffsets, 0);
4036 if (__le64_to_cpu(bsb.length2) > 0 &&
4037 reshape_completed <= (__le64_to_cpu(bsb.arraystart2)))
4038 forget_backup(dests, destfd,
4039 destoffsets, 1);
4040 }
4041
4042 if (rv < 0) {
4043 if (rv == -1)
4044 done = 1;
4045 break;
4046 }
4047 if (rv == 0 && increasing && !st->ss->external) {
4048 /* No longer need to monitor this reshape */
4049 done = 1;
4050 break;
4051 }
4052
4053 while (rv) {
4054 unsigned long long offset;
4055 unsigned long actual_stripes;
4056 /* Need to backup some data.
4057 * If 'part' is not used and the desired
4058 * backup size is suspended, do a backup,
4059 * then consider the next part.
4060 */
4061 /* Check that 'part' is unused */
4062 if (part == 0 && __le64_to_cpu(bsb.length) != 0)
4063 break;
4064 if (part == 1 && __le64_to_cpu(bsb.length2) != 0)
4065 break;
4066
4067 offset = backup_point / data;
4068 actual_stripes = stripes;
4069 if (increasing) {
4070 if (offset + actual_stripes * (chunk/512) >
4071 sra->component_size)
4072 actual_stripes = ((sra->component_size - offset)
4073 / (chunk/512));
4074 if (offset + actual_stripes * (chunk/512) >
4075 suspend_point/data)
4076 break;
4077 } else {
4078 if (offset < actual_stripes * (chunk/512))
4079 actual_stripes = offset / (chunk/512);
4080 offset -= actual_stripes * (chunk/512);
4081 if (offset < suspend_point/data)
4082 break;
4083 }
4084 if (actual_stripes == 0)
4085 break;
4086 grow_backup(sra, offset, actual_stripes,
4087 fds, offsets,
4088 disks, chunk, level, layout,
4089 dests, destfd, destoffsets,
4090 part, &degraded, buf);
4091 validate(afd, destfd[0], destoffsets[0]);
4092 /* record where 'part' is up to */
4093 part = !part;
4094 if (increasing)
4095 backup_point += actual_stripes * (chunk/512) * data;
4096 else
4097 backup_point -= actual_stripes * (chunk/512) * data;
4098 }
4099 }
4100
4101 /* FIXME maybe call progress_reshape one more time instead */
4102 abort_reshape(sra); /* remove any remaining suspension */
4103 if (reshape->before.data_disks == reshape->after.data_disks)
4104 sysfs_set_num(sra, NULL, "sync_speed_min", speed);
4105 free(buf);
4106 return done;
4107 }
4108
4109 /*
4110 * If any spare contains md_back_data-1 which is recent wrt mtime,
4111 * write that data into the array and update the super blocks with
4112 * the new reshape_progress
4113 */
4114 int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt,
4115 char *backup_file, int verbose)
4116 {
4117 int i, j;
4118 int old_disks;
4119 unsigned long long *offsets;
4120 unsigned long long nstripe, ostripe;
4121 int ndata, odata;
4122
4123 odata = info->array.raid_disks - info->delta_disks - 1;
4124 if (info->array.level == 6) odata--; /* number of data disks */
4125 ndata = info->array.raid_disks - 1;
4126 if (info->new_level == 6) ndata--;
4127
4128 old_disks = info->array.raid_disks - info->delta_disks;
4129
4130 if (info->delta_disks <= 0)
4131 /* Didn't grow, so the backup file must have
4132 * been used
4133 */
4134 old_disks = cnt;
4135 for (i=old_disks-(backup_file?1:0); i<cnt; i++) {
4136 struct mdinfo dinfo;
4137 int fd;
4138 int bsbsize;
4139 char *devname, namebuf[20];
4140 unsigned long long lo, hi;
4141
4142 /* This was a spare and may have some saved data on it.
4143 * Load the superblock, find and load the
4144 * backup_super_block.
4145 * If either fail, go on to next device.
4146 * If the backup contains no new info, just return
4147 * else restore data and update all superblocks
4148 */
4149 if (i == old_disks-1) {
4150 fd = open(backup_file, O_RDONLY);
4151 if (fd<0) {
4152 pr_err("backup file %s inaccessible: %s\n",
4153 backup_file, strerror(errno));
4154 continue;
4155 }
4156 devname = backup_file;
4157 } else {
4158 fd = fdlist[i];
4159 if (fd < 0)
4160 continue;
4161 if (st->ss->load_super(st, fd, NULL))
4162 continue;
4163
4164 st->ss->getinfo_super(st, &dinfo, NULL);
4165 st->ss->free_super(st);
4166
4167 if (lseek64(fd,
4168 (dinfo.data_offset + dinfo.component_size - 8) <<9,
4169 0) < 0) {
4170 pr_err("Cannot seek on device %d\n", i);
4171 continue; /* Cannot seek */
4172 }
4173 sprintf(namebuf, "device-%d", i);
4174 devname = namebuf;
4175 }
4176 if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb)) {
4177 if (verbose)
4178 pr_err("Cannot read from %s\n", devname);
4179 continue; /* Cannot read */
4180 }
4181 if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0 &&
4182 memcmp(bsb.magic, "md_backup_data-2", 16) != 0) {
4183 if (verbose)
4184 pr_err("No backup metadata on %s\n", devname);
4185 continue;
4186 }
4187 if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb))) {
4188 if (verbose)
4189 pr_err("Bad backup-metadata checksum on %s\n", devname);
4190 continue; /* bad checksum */
4191 }
4192 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0 &&
4193 bsb.sb_csum2 != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum2)-((char*)&bsb))) {
4194 if (verbose)
4195 pr_err("Bad backup-metadata checksum2 on %s\n", devname);
4196 continue; /* Bad second checksum */
4197 }
4198 if (memcmp(bsb.set_uuid,info->uuid, 16) != 0) {
4199 if (verbose)
4200 pr_err("Wrong uuid on backup-metadata on %s\n", devname);
4201 continue; /* Wrong uuid */
4202 }
4203
4204 /* array utime and backup-mtime should be updated at much the same time, but it seems that
4205 * sometimes they aren't... So allow considerable flexability in matching, and allow
4206 * this test to be overridden by an environment variable.
4207 */
4208 if (info->array.utime > (int)__le64_to_cpu(bsb.mtime) + 2*60*60 ||
4209 info->array.utime < (int)__le64_to_cpu(bsb.mtime) - 10*60) {
4210 if (check_env("MDADM_GROW_ALLOW_OLD")) {
4211 pr_err("accepting backup with timestamp %lu "
4212 "for array with timestamp %lu\n",
4213 (unsigned long)__le64_to_cpu(bsb.mtime),
4214 (unsigned long)info->array.utime);
4215 } else {
4216 pr_err("too-old timestamp on backup-metadata on %s\n", devname);
4217 pr_err("If you think it is should be safe, try 'export MDADM_GROW_ALLOW_OLD=1'\n");
4218 continue; /* time stamp is too bad */
4219 }
4220 }
4221
4222 if (bsb.magic[15] == '1') {
4223 if (bsb.length == 0)
4224 continue;
4225 if (info->delta_disks >= 0) {
4226 /* reshape_progress is increasing */
4227 if (__le64_to_cpu(bsb.arraystart)
4228 + __le64_to_cpu(bsb.length)
4229 < info->reshape_progress) {
4230 nonew:
4231 if (verbose)
4232 pr_err("backup-metadata found on %s but is not needed\n", devname);
4233 continue; /* No new data here */
4234 }
4235 } else {
4236 /* reshape_progress is decreasing */
4237 if (__le64_to_cpu(bsb.arraystart) >=
4238 info->reshape_progress)
4239 goto nonew; /* No new data here */
4240 }
4241 } else {
4242 if (bsb.length == 0 && bsb.length2 == 0)
4243 continue;
4244 if (info->delta_disks >= 0) {
4245 /* reshape_progress is increasing */
4246 if ((__le64_to_cpu(bsb.arraystart)
4247 + __le64_to_cpu(bsb.length)
4248 < info->reshape_progress)
4249 &&
4250 (__le64_to_cpu(bsb.arraystart2)
4251 + __le64_to_cpu(bsb.length2)
4252 < info->reshape_progress))
4253 goto nonew; /* No new data here */
4254 } else {
4255 /* reshape_progress is decreasing */
4256 if (__le64_to_cpu(bsb.arraystart) >=
4257 info->reshape_progress &&
4258 __le64_to_cpu(bsb.arraystart2) >=
4259 info->reshape_progress)
4260 goto nonew; /* No new data here */
4261 }
4262 }
4263 if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0) {
4264 second_fail:
4265 if (verbose)
4266 pr_err("Failed to verify secondary backup-metadata block on %s\n",
4267 devname);
4268 continue; /* Cannot seek */
4269 }
4270 /* There should be a duplicate backup superblock 4k before here */
4271 if (lseek64(fd, -4096, 1) < 0 ||
4272 read(fd, &bsb2, sizeof(bsb2)) != sizeof(bsb2))
4273 goto second_fail; /* Cannot find leading superblock */
4274 if (bsb.magic[15] == '1')
4275 bsbsize = offsetof(struct mdp_backup_super, pad1);
4276 else
4277 bsbsize = offsetof(struct mdp_backup_super, pad);
4278 if (memcmp(&bsb2, &bsb, bsbsize) != 0)
4279 goto second_fail; /* Cannot find leading superblock */
4280
4281 /* Now need the data offsets for all devices. */
4282 offsets = xmalloc(sizeof(*offsets)*info->array.raid_disks);
4283 for(j=0; j<info->array.raid_disks; j++) {
4284 if (fdlist[j] < 0)
4285 continue;
4286 if (st->ss->load_super(st, fdlist[j], NULL))
4287 /* FIXME should be this be an error */
4288 continue;
4289 st->ss->getinfo_super(st, &dinfo, NULL);
4290 st->ss->free_super(st);
4291 offsets[j] = dinfo.data_offset * 512;
4292 }
4293 printf(Name ": restoring critical section\n");
4294
4295 if (restore_stripes(fdlist, offsets,
4296 info->array.raid_disks,
4297 info->new_chunk,
4298 info->new_level,
4299 info->new_layout,
4300 fd, __le64_to_cpu(bsb.devstart)*512,
4301 __le64_to_cpu(bsb.arraystart)*512,
4302 __le64_to_cpu(bsb.length)*512, NULL)) {
4303 /* didn't succeed, so giveup */
4304 if (verbose)
4305 pr_err("Error restoring backup from %s\n",
4306 devname);
4307 free(offsets);
4308 return 1;
4309 }
4310
4311 if (bsb.magic[15] == '2' &&
4312 restore_stripes(fdlist, offsets,
4313 info->array.raid_disks,
4314 info->new_chunk,
4315 info->new_level,
4316 info->new_layout,
4317 fd, __le64_to_cpu(bsb.devstart)*512 +
4318 __le64_to_cpu(bsb.devstart2)*512,
4319 __le64_to_cpu(bsb.arraystart2)*512,
4320 __le64_to_cpu(bsb.length2)*512, NULL)) {
4321 /* didn't succeed, so giveup */
4322 if (verbose)
4323 pr_err("Error restoring second backup from %s\n",
4324 devname);
4325 free(offsets);
4326 return 1;
4327 }
4328
4329 free(offsets);
4330
4331 /* Ok, so the data is restored. Let's update those superblocks. */
4332
4333 lo = hi = 0;
4334 if (bsb.length) {
4335 lo = __le64_to_cpu(bsb.arraystart);
4336 hi = lo + __le64_to_cpu(bsb.length);
4337 }
4338 if (bsb.magic[15] == '2' && bsb.length2) {
4339 unsigned long long lo1, hi1;
4340 lo1 = __le64_to_cpu(bsb.arraystart2);
4341 hi1 = lo1 + __le64_to_cpu(bsb.length2);
4342 if (lo == hi) {
4343 lo = lo1;
4344 hi = hi1;
4345 } else if (lo < lo1)
4346 hi = hi1;
4347 else
4348 lo = lo1;
4349 }
4350 if (lo < hi &&
4351 (info->reshape_progress < lo ||
4352 info->reshape_progress > hi))
4353 /* backup does not affect reshape_progress*/ ;
4354 else if (info->delta_disks >= 0) {
4355 info->reshape_progress = __le64_to_cpu(bsb.arraystart) +
4356 __le64_to_cpu(bsb.length);
4357 if (bsb.magic[15] == '2') {
4358 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2) +
4359 __le64_to_cpu(bsb.length2);
4360 if (p2 > info->reshape_progress)
4361 info->reshape_progress = p2;
4362 }
4363 } else {
4364 info->reshape_progress = __le64_to_cpu(bsb.arraystart);
4365 if (bsb.magic[15] == '2') {
4366 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2);
4367 if (p2 < info->reshape_progress)
4368 info->reshape_progress = p2;
4369 }
4370 }
4371 for (j=0; j<info->array.raid_disks; j++) {
4372 if (fdlist[j] < 0)
4373 continue;
4374 if (st->ss->load_super(st, fdlist[j], NULL))
4375 continue;
4376 st->ss->getinfo_super(st, &dinfo, NULL);
4377 dinfo.reshape_progress = info->reshape_progress;
4378 st->ss->update_super(st, &dinfo,
4379 "_reshape_progress",
4380 NULL,0, 0, NULL);
4381 st->ss->store_super(st, fdlist[j]);
4382 st->ss->free_super(st);
4383 }
4384 return 0;
4385 }
4386 /* Didn't find any backup data, try to see if any
4387 * was needed.
4388 */
4389 if (info->delta_disks < 0) {
4390 /* When shrinking, the critical section is at the end.
4391 * So see if we are before the critical section.
4392 */
4393 unsigned long long first_block;
4394 nstripe = ostripe = 0;
4395 first_block = 0;
4396 while (ostripe >= nstripe) {
4397 ostripe += info->array.chunk_size / 512;
4398 first_block = ostripe * odata;
4399 nstripe = first_block / ndata / (info->new_chunk/512) *
4400 (info->new_chunk/512);
4401 }
4402
4403 if (info->reshape_progress >= first_block)
4404 return 0;
4405 }
4406 if (info->delta_disks > 0) {
4407 /* See if we are beyond the critical section. */
4408 unsigned long long last_block;
4409 nstripe = ostripe = 0;
4410 last_block = 0;
4411 while (nstripe >= ostripe) {
4412 nstripe += info->new_chunk / 512;
4413 last_block = nstripe * ndata;
4414 ostripe = last_block / odata / (info->array.chunk_size/512) *
4415 (info->array.chunk_size/512);
4416 }
4417
4418 if (info->reshape_progress >= last_block)
4419 return 0;
4420 }
4421 /* needed to recover critical section! */
4422 if (verbose)
4423 pr_err("Failed to find backup of critical section\n");
4424 return 1;
4425 }
4426
4427 int Grow_continue_command(char *devname, int fd,
4428 char *backup_file, int verbose)
4429 {
4430 int ret_val = 0;
4431 struct supertype *st = NULL;
4432 struct mdinfo *content = NULL;
4433 struct mdinfo array;
4434 char *subarray = NULL;
4435 struct mdinfo *cc = NULL;
4436 struct mdstat_ent *mdstat = NULL;
4437 int cfd = -1;
4438 int fd2 = -1;
4439
4440 dprintf("Grow continue from command line called for %s\n",
4441 devname);
4442
4443 st = super_by_fd(fd, &subarray);
4444 if (!st || !st->ss) {
4445 pr_err("Unable to determine metadata format for %s\n",
4446 devname);
4447 return 1;
4448 }
4449 dprintf("Grow continue is run for ");
4450 if (st->ss->external == 0) {
4451 int d;
4452 dprintf("native array (%s)\n", devname);
4453 if (ioctl(fd, GET_ARRAY_INFO, &array.array) < 0) {
4454 pr_err("%s is not an active md array -"
4455 " aborting\n", devname);
4456 ret_val = 1;
4457 goto Grow_continue_command_exit;
4458 }
4459 content = &array;
4460 /* Need to load a superblock.
4461 * FIXME we should really get what we need from
4462 * sysfs
4463 */
4464 for (d = 0; d < MAX_DISKS; d++) {
4465 mdu_disk_info_t disk;
4466 char *dv;
4467 int err;
4468 disk.number = d;
4469 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
4470 continue;
4471 if (disk.major == 0 && disk.minor == 0)
4472 continue;
4473 if ((disk.state & (1 << MD_DISK_ACTIVE)) == 0)
4474 continue;
4475 dv = map_dev(disk.major, disk.minor, 1);
4476 if (!dv)
4477 continue;
4478 fd2 = dev_open(dv, O_RDONLY);
4479 if (fd2 < 0)
4480 continue;
4481 err = st->ss->load_super(st, fd2, NULL);
4482 close(fd2);
4483 if (err)
4484 continue;
4485 break;
4486 }
4487 if (d == MAX_DISKS) {
4488 pr_err("Unable to load metadata for %s\n",
4489 devname);
4490 ret_val = 1;
4491 goto Grow_continue_command_exit;
4492 }
4493 st->ss->getinfo_super(st, content, NULL);
4494 } else {
4495 char *container;
4496
4497 if (subarray) {
4498 dprintf("subarray (%s)\n", subarray);
4499 container = st->container_devnm;
4500 cfd = open_dev_excl(st->container_devnm);
4501 } else {
4502 container = st->devnm;
4503 close(fd);
4504 cfd = open_dev_excl(st->devnm);
4505 dprintf("container (%s)\n", container);
4506 fd = cfd;
4507 }
4508 if (cfd < 0) {
4509 pr_err("Unable to open container "
4510 "for %s\n", devname);
4511 ret_val = 1;
4512 goto Grow_continue_command_exit;
4513 }
4514
4515 /* find in container array under reshape
4516 */
4517 ret_val = st->ss->load_container(st, cfd, NULL);
4518 if (ret_val) {
4519 pr_err("Cannot read superblock for %s\n",
4520 devname);
4521 ret_val = 1;
4522 goto Grow_continue_command_exit;
4523 }
4524
4525 cc = st->ss->container_content(st, subarray);
4526 for (content = cc; content ; content = content->next) {
4527 char *array;
4528 int allow_reshape = 1;
4529
4530 if (content->reshape_active == 0)
4531 continue;
4532 /* The decision about array or container wide
4533 * reshape is taken in Grow_continue based
4534 * content->reshape_active state, therefore we
4535 * need to check_reshape based on
4536 * reshape_active and subarray name
4537 */
4538 if (content->array.state & (1<<MD_SB_BLOCK_VOLUME))
4539 allow_reshape = 0;
4540 if (content->reshape_active == CONTAINER_RESHAPE &&
4541 (content->array.state
4542 & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE)))
4543 allow_reshape = 0;
4544
4545 if (!allow_reshape) {
4546 pr_err("cannot continue reshape of an array"
4547 " in container with unsupported"
4548 " metadata: %s(%s)\n",
4549 devname, container);
4550 ret_val = 1;
4551 goto Grow_continue_command_exit;
4552 }
4553
4554 array = strchr(content->text_version+1, '/')+1;
4555 mdstat = mdstat_by_subdev(array, container);
4556 if (!mdstat)
4557 continue;
4558 if (mdstat->active == 0) {
4559 pr_err("Skipping inactive array %s.\n",
4560 mdstat->devnm);
4561 free_mdstat(mdstat);
4562 mdstat = NULL;
4563 continue;
4564 }
4565 break;
4566 }
4567 if (!content) {
4568 pr_err("Unable to determine reshaped "
4569 "array for %s\n", devname);
4570 ret_val = 1;
4571 goto Grow_continue_command_exit;
4572 }
4573 fd2 = open_dev(mdstat->devnm);
4574 if (fd2 < 0) {
4575 pr_err("cannot open (%s)\n", mdstat->devnm);
4576 ret_val = 1;
4577 goto Grow_continue_command_exit;
4578 }
4579
4580 sysfs_init(content, fd2, mdstat->devnm);
4581
4582 /* start mdmon in case it is not running
4583 */
4584 if (!mdmon_running(container))
4585 start_mdmon(container);
4586 ping_monitor(container);
4587
4588 if (mdmon_running(container))
4589 st->update_tail = &st->updates;
4590 else {
4591 pr_err("No mdmon found. "
4592 "Grow cannot continue.\n");
4593 ret_val = 1;
4594 goto Grow_continue_command_exit;
4595 }
4596 }
4597
4598 /* verify that array under reshape is started from
4599 * correct position
4600 */
4601 if (verify_reshape_position(content, content->array.level) < 0) {
4602 ret_val = 1;
4603 goto Grow_continue_command_exit;
4604 }
4605
4606 /* continue reshape
4607 */
4608 ret_val = Grow_continue(fd, st, content, backup_file, 0);
4609
4610 Grow_continue_command_exit:
4611 if (fd2 > -1)
4612 close(fd2);
4613 if (cfd > -1)
4614 close(cfd);
4615 st->ss->free_super(st);
4616 free_mdstat(mdstat);
4617 sysfs_free(cc);
4618 free(subarray);
4619
4620 return ret_val;
4621 }
4622
4623 int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
4624 char *backup_file, int freeze_reshape)
4625 {
4626 int ret_val = 2;
4627
4628 if (!info->reshape_active)
4629 return ret_val;
4630
4631 if (st->ss->external) {
4632 int cfd = open_dev(st->container_devnm);
4633
4634 if (cfd < 0)
4635 return 1;
4636
4637 st->ss->load_container(st, cfd, st->container_devnm);
4638 close(cfd);
4639 ret_val = reshape_container(st->container_devnm, NULL, mdfd,
4640 st, info, 0, backup_file,
4641 0, 1, freeze_reshape);
4642 } else
4643 ret_val = reshape_array(NULL, mdfd, "array", st, info, 1,
4644 NULL, 0ULL, backup_file, 0, 0, 1,
4645 freeze_reshape);
4646
4647 return ret_val;
4648 }