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