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