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