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