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