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