]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
4 | * Copyright (C) 2001-2006 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@cse.unsw.edu.au> | |
23 | * Paper: Neil Brown | |
24 | * School of Computer Science and Engineering | |
25 | * The University of New South Wales | |
26 | * Sydney, 2052 | |
27 | * Australia | |
28 | */ | |
29 | #include "mdadm.h" | |
30 | #include "dlink.h" | |
31 | ||
32 | #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN) | |
33 | #error no endian defined | |
34 | #endif | |
35 | #include "md_u.h" | |
36 | #include "md_p.h" | |
37 | ||
38 | int Grow_Add_device(char *devname, int fd, char *newdev) | |
39 | { | |
40 | /* Add a device to an active array. | |
41 | * Currently, just extend a linear array. | |
42 | * This requires writing a new superblock on the | |
43 | * new device, calling the kernel to add the device, | |
44 | * and if that succeeds, update the superblock on | |
45 | * all other devices. | |
46 | * This means that we need to *find* all other devices. | |
47 | */ | |
48 | struct mdinfo info; | |
49 | ||
50 | void *super = NULL; | |
51 | struct stat stb; | |
52 | int nfd, fd2; | |
53 | int d, nd; | |
54 | struct supertype *st = NULL; | |
55 | ||
56 | ||
57 | if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) { | |
58 | fprintf(stderr, Name ": cannot get array info for %s\n", devname); | |
59 | return 1; | |
60 | } | |
61 | ||
62 | st = super_by_version(info.array.major_version, info.array.minor_version); | |
63 | if (!st) { | |
64 | fprintf(stderr, Name ": cannot handle arrays with superblock version %d\n", info.array.major_version); | |
65 | return 1; | |
66 | } | |
67 | ||
68 | if (info.array.level != -1) { | |
69 | fprintf(stderr, Name ": can only add devices to linear arrays\n"); | |
70 | return 1; | |
71 | } | |
72 | ||
73 | nfd = open(newdev, O_RDWR|O_EXCL); | |
74 | if (nfd < 0) { | |
75 | fprintf(stderr, Name ": cannot open %s\n", newdev); | |
76 | return 1; | |
77 | } | |
78 | fstat(nfd, &stb); | |
79 | if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
80 | fprintf(stderr, Name ": %s is not a block device!\n", newdev); | |
81 | close(nfd); | |
82 | return 1; | |
83 | } | |
84 | /* now check out all the devices and make sure we can read the superblock */ | |
85 | for (d=0 ; d < info.array.raid_disks ; d++) { | |
86 | mdu_disk_info_t disk; | |
87 | char *dv; | |
88 | ||
89 | disk.number = d; | |
90 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) { | |
91 | fprintf(stderr, Name ": cannot get device detail for device %d\n", | |
92 | d); | |
93 | return 1; | |
94 | } | |
95 | dv = map_dev(disk.major, disk.minor, 1); | |
96 | if (!dv) { | |
97 | fprintf(stderr, Name ": cannot find device file for device %d\n", | |
98 | d); | |
99 | return 1; | |
100 | } | |
101 | fd2 = dev_open(dv, O_RDWR); | |
102 | if (!fd2) { | |
103 | fprintf(stderr, Name ": cannot open device file %s\n", dv); | |
104 | return 1; | |
105 | } | |
106 | if (super) free(super); | |
107 | super= NULL; | |
108 | if (st->ss->load_super(st, fd2, &super, NULL)) { | |
109 | fprintf(stderr, Name ": cannot find super block on %s\n", dv); | |
110 | close(fd2); | |
111 | return 1; | |
112 | } | |
113 | close(fd2); | |
114 | } | |
115 | /* Ok, looks good. Lets update the superblock and write it out to | |
116 | * newdev. | |
117 | */ | |
118 | ||
119 | info.disk.number = d; | |
120 | info.disk.major = major(stb.st_rdev); | |
121 | info.disk.minor = minor(stb.st_rdev); | |
122 | info.disk.raid_disk = d; | |
123 | info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE); | |
124 | st->ss->update_super(&info, super, "linear-grow-new", newdev, | |
125 | 0, 0, NULL); | |
126 | ||
127 | if (st->ss->store_super(st, nfd, super)) { | |
128 | fprintf(stderr, Name ": Cannot store new superblock on %s\n", | |
129 | newdev); | |
130 | close(nfd); | |
131 | return 1; | |
132 | } | |
133 | close(nfd); | |
134 | ||
135 | if (ioctl(fd, ADD_NEW_DISK, &info.disk) != 0) { | |
136 | fprintf(stderr, Name ": Cannot add new disk to this array\n"); | |
137 | return 1; | |
138 | } | |
139 | /* Well, that seems to have worked. | |
140 | * Now go through and update all superblocks | |
141 | */ | |
142 | ||
143 | if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) { | |
144 | fprintf(stderr, Name ": cannot get array info for %s\n", devname); | |
145 | return 1; | |
146 | } | |
147 | ||
148 | nd = d; | |
149 | for (d=0 ; d < info.array.raid_disks ; d++) { | |
150 | mdu_disk_info_t disk; | |
151 | char *dv; | |
152 | ||
153 | disk.number = d; | |
154 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) { | |
155 | fprintf(stderr, Name ": cannot get device detail for device %d\n", | |
156 | d); | |
157 | return 1; | |
158 | } | |
159 | dv = map_dev(disk.major, disk.minor, 1); | |
160 | if (!dv) { | |
161 | fprintf(stderr, Name ": cannot find device file for device %d\n", | |
162 | d); | |
163 | return 1; | |
164 | } | |
165 | fd2 = dev_open(dv, O_RDWR); | |
166 | if (fd2 < 0) { | |
167 | fprintf(stderr, Name ": cannot open device file %s\n", dv); | |
168 | return 1; | |
169 | } | |
170 | if (st->ss->load_super(st, fd2, &super, NULL)) { | |
171 | fprintf(stderr, Name ": cannot find super block on %s\n", dv); | |
172 | close(fd); | |
173 | return 1; | |
174 | } | |
175 | info.array.raid_disks = nd+1; | |
176 | info.array.nr_disks = nd+1; | |
177 | info.array.active_disks = nd+1; | |
178 | info.array.working_disks = nd+1; | |
179 | ||
180 | st->ss->update_super(&info, super, "linear-grow-update", dv, | |
181 | 0, 0, NULL); | |
182 | ||
183 | if (st->ss->store_super(st, fd2, super)) { | |
184 | fprintf(stderr, Name ": Cannot store new superblock on %s\n", dv); | |
185 | close(fd2); | |
186 | return 1; | |
187 | } | |
188 | close(fd2); | |
189 | } | |
190 | ||
191 | return 0; | |
192 | } | |
193 | ||
194 | int Grow_addbitmap(char *devname, int fd, char *file, int chunk, int delay, int write_behind, int force) | |
195 | { | |
196 | /* | |
197 | * First check that array doesn't have a bitmap | |
198 | * Then create the bitmap | |
199 | * Then add it | |
200 | * | |
201 | * For internal bitmaps, we need to check the version, | |
202 | * find all the active devices, and write the bitmap block | |
203 | * to all devices | |
204 | */ | |
205 | mdu_bitmap_file_t bmf; | |
206 | mdu_array_info_t array; | |
207 | struct supertype *st; | |
208 | int major = BITMAP_MAJOR_HI; | |
209 | int vers = md_get_version(fd); | |
210 | unsigned long long bitmapsize, array_size; | |
211 | ||
212 | if (vers < 9003) { | |
213 | major = BITMAP_MAJOR_HOSTENDIAN; | |
214 | #ifdef __BIG_ENDIAN | |
215 | fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n" | |
216 | " between different architectured. Consider upgrading the Linux kernel.\n"); | |
217 | #endif | |
218 | } | |
219 | ||
220 | if (ioctl(fd, GET_BITMAP_FILE, &bmf) != 0) { | |
221 | if (errno == ENOMEM) | |
222 | fprintf(stderr, Name ": Memory allocation failure.\n"); | |
223 | else | |
224 | fprintf(stderr, Name ": bitmaps not supported by this kernel.\n"); | |
225 | return 1; | |
226 | } | |
227 | if (bmf.pathname[0]) { | |
228 | if (strcmp(file,"none")==0) { | |
229 | if (ioctl(fd, SET_BITMAP_FILE, -1)!= 0) { | |
230 | fprintf(stderr, Name ": failed to remove bitmap %s\n", | |
231 | bmf.pathname); | |
232 | return 1; | |
233 | } | |
234 | return 0; | |
235 | } | |
236 | fprintf(stderr, Name ": %s already has a bitmap (%s)\n", | |
237 | devname, bmf.pathname); | |
238 | return 1; | |
239 | } | |
240 | if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) { | |
241 | fprintf(stderr, Name ": cannot get array status for %s\n", devname); | |
242 | return 1; | |
243 | } | |
244 | if (array.state & (1<<MD_SB_BITMAP_PRESENT)) { | |
245 | if (strcmp(file, "none")==0) { | |
246 | array.state &= ~(1<<MD_SB_BITMAP_PRESENT); | |
247 | if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) { | |
248 | fprintf(stderr, Name ": failed to remove internal bitmap.\n"); | |
249 | return 1; | |
250 | } | |
251 | return 0; | |
252 | } | |
253 | fprintf(stderr, Name ": Internal bitmap already present on %s\n", | |
254 | devname); | |
255 | return 1; | |
256 | } | |
257 | bitmapsize = array.size; | |
258 | bitmapsize <<= 1; | |
259 | if (get_dev_size(fd, NULL, &array_size) && | |
260 | array_size > (0x7fffffffULL<<9)) { | |
261 | /* Array is big enough that we cannot trust array.size | |
262 | * try other approaches | |
263 | */ | |
264 | bitmapsize = get_component_size(fd); | |
265 | } | |
266 | if (bitmapsize == 0) { | |
267 | fprintf(stderr, Name ": Cannot reliably determine size of array to create bitmap - sorry.\n"); | |
268 | return 1; | |
269 | } | |
270 | ||
271 | if (array.level == 10) { | |
272 | int ncopies = (array.layout&255)*((array.layout>>8)&255); | |
273 | bitmapsize = bitmapsize * array.raid_disks / ncopies; | |
274 | } | |
275 | ||
276 | st = super_by_version(array.major_version, array.minor_version); | |
277 | if (!st) { | |
278 | fprintf(stderr, Name ": Cannot understand version %d.%d\n", | |
279 | array.major_version, array.minor_version); | |
280 | return 1; | |
281 | } | |
282 | if (strcmp(file, "none") == 0) { | |
283 | fprintf(stderr, Name ": no bitmap found on %s\n", devname); | |
284 | return 1; | |
285 | } else if (strcmp(file, "internal") == 0) { | |
286 | int d; | |
287 | for (d=0; d< st->max_devs; d++) { | |
288 | mdu_disk_info_t disk; | |
289 | char *dv; | |
290 | disk.number = d; | |
291 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) | |
292 | continue; | |
293 | if (disk.major == 0 && | |
294 | disk.minor == 0) | |
295 | continue; | |
296 | if ((disk.state & (1<<MD_DISK_SYNC))==0) | |
297 | continue; | |
298 | dv = map_dev(disk.major, disk.minor, 1); | |
299 | if (dv) { | |
300 | void *super; | |
301 | int fd2 = dev_open(dv, O_RDWR); | |
302 | if (fd2 < 0) | |
303 | continue; | |
304 | if (st->ss->load_super(st, fd2, &super, NULL)==0) { | |
305 | if (st->ss->add_internal_bitmap( | |
306 | st, super, | |
307 | &chunk, delay, write_behind, | |
308 | bitmapsize, 0, major) | |
309 | ) | |
310 | st->ss->write_bitmap(st, fd2, super); | |
311 | else { | |
312 | fprintf(stderr, Name ": failed to create internal bitmap - chunksize problem.\n"); | |
313 | close(fd2); | |
314 | return 1; | |
315 | } | |
316 | } | |
317 | close(fd2); | |
318 | } | |
319 | } | |
320 | array.state |= (1<<MD_SB_BITMAP_PRESENT); | |
321 | if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) { | |
322 | fprintf(stderr, Name ": failed to set internal bitmap.\n"); | |
323 | return 1; | |
324 | } | |
325 | } else { | |
326 | int uuid[4]; | |
327 | int bitmap_fd; | |
328 | int d; | |
329 | int max_devs = st->max_devs; | |
330 | void *super = NULL; | |
331 | ||
332 | /* try to load a superblock */ | |
333 | for (d=0; d<max_devs; d++) { | |
334 | mdu_disk_info_t disk; | |
335 | char *dv; | |
336 | int fd2; | |
337 | disk.number = d; | |
338 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) | |
339 | continue; | |
340 | if ((disk.major==0 && disk.minor==0) || | |
341 | (disk.state & (1<<MD_DISK_REMOVED))) | |
342 | continue; | |
343 | dv = map_dev(disk.major, disk.minor, 1); | |
344 | if (!dv) continue; | |
345 | fd2 = dev_open(dv, O_RDONLY); | |
346 | if (fd2 >= 0 && | |
347 | st->ss->load_super(st, fd2, &super, NULL) == 0) { | |
348 | close(fd2); | |
349 | st->ss->uuid_from_super(uuid, super); | |
350 | break; | |
351 | } | |
352 | close(fd2); | |
353 | } | |
354 | if (d == max_devs) { | |
355 | fprintf(stderr, Name ": cannot find UUID for array!\n"); | |
356 | return 1; | |
357 | } | |
358 | if (CreateBitmap(file, force, (char*)uuid, chunk, | |
359 | delay, write_behind, bitmapsize, major)) { | |
360 | return 1; | |
361 | } | |
362 | bitmap_fd = open(file, O_RDWR); | |
363 | if (bitmap_fd < 0) { | |
364 | fprintf(stderr, Name ": weird: %s cannot be opened\n", | |
365 | file); | |
366 | return 1; | |
367 | } | |
368 | if (ioctl(fd, SET_BITMAP_FILE, bitmap_fd) < 0) { | |
369 | fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n", | |
370 | devname, strerror(errno)); | |
371 | return 1; | |
372 | } | |
373 | } | |
374 | ||
375 | return 0; | |
376 | } | |
377 | ||
378 | ||
379 | /* | |
380 | * When reshaping an array we might need to backup some data. | |
381 | * This is written to all spares with a 'super_block' describing it. | |
382 | * The superblock goes 1K form the end of the used space on the | |
383 | * device. | |
384 | * It if written after the backup is complete. | |
385 | * It has the following structure. | |
386 | */ | |
387 | ||
388 | struct mdp_backup_super { | |
389 | char magic[16]; /* md_backup_data-1 */ | |
390 | __u8 set_uuid[16]; | |
391 | __u64 mtime; | |
392 | /* start/sizes in 512byte sectors */ | |
393 | __u64 devstart; | |
394 | __u64 arraystart; | |
395 | __u64 length; | |
396 | __u32 sb_csum; /* csum of preceeding bytes. */ | |
397 | }; | |
398 | ||
399 | int bsb_csum(char *buf, int len) | |
400 | { | |
401 | int i; | |
402 | int csum = 0; | |
403 | for (i=0; i<len; i++) | |
404 | csum = (csum<<3) + buf[0]; | |
405 | return __cpu_to_le32(csum); | |
406 | } | |
407 | ||
408 | int Grow_reshape(char *devname, int fd, int quiet, char *backup_file, | |
409 | long long size, | |
410 | int level, int layout, int chunksize, int raid_disks) | |
411 | { | |
412 | /* Make some changes in the shape of an array. | |
413 | * The kernel must support the change. | |
414 | * Different reshapes have subtly different meaning for different | |
415 | * levels, so we need to check the current state of the array | |
416 | * and go from there. | |
417 | */ | |
418 | struct mdu_array_info_s array; | |
419 | char *c; | |
420 | ||
421 | struct mdp_backup_super bsb; | |
422 | struct supertype *st; | |
423 | ||
424 | int nlevel, olevel; | |
425 | int nchunk, ochunk; | |
426 | int nlayout, olayout; | |
427 | int ndisks, odisks; | |
428 | int ndata, odata; | |
429 | unsigned long long nstripe, ostripe, last_block; | |
430 | int *fdlist; | |
431 | unsigned long long *offsets; | |
432 | int d, i, spares; | |
433 | int nrdisks; | |
434 | int err; | |
435 | void *super = NULL; | |
436 | ||
437 | struct sysarray *sra; | |
438 | struct sysdev *sd; | |
439 | ||
440 | if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) { | |
441 | fprintf(stderr, Name ": %s is not an active md array - aborting\n", | |
442 | devname); | |
443 | return 1; | |
444 | } | |
445 | c = map_num(pers, array.level); | |
446 | if (c == NULL) c = "-unknown-"; | |
447 | switch(array.level) { | |
448 | default: /* raid0, linear, multipath cannot be reconfigured */ | |
449 | fprintf(stderr, Name ": %s array %s cannot be reshaped.\n", | |
450 | c, devname); | |
451 | return 1; | |
452 | ||
453 | case LEVEL_FAULTY: /* only 'layout' change is permitted */ | |
454 | ||
455 | if (size >= 0) { | |
456 | fprintf(stderr, Name ": %s: Cannot change size of a 'faulty' array\n", | |
457 | devname); | |
458 | return 1; | |
459 | } | |
460 | if (level != UnSet && level != LEVEL_FAULTY) { | |
461 | fprintf(stderr, Name ": %s: Cannot change RAID level of a 'faulty' array\n", | |
462 | devname); | |
463 | return 1; | |
464 | } | |
465 | if (chunksize || raid_disks) { | |
466 | fprintf(stderr, Name ": %s: Cannot change chunksize or disks of a 'faulty' array\n", | |
467 | devname); | |
468 | return 1; | |
469 | } | |
470 | if (layout == UnSet) | |
471 | return 0; /* nothing to do.... */ | |
472 | ||
473 | array.layout = layout; | |
474 | if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) { | |
475 | fprintf(stderr, Name ": Cannot set layout for %s: %s\n", | |
476 | devname, strerror(errno)); | |
477 | return 1; | |
478 | } | |
479 | if (!quiet) | |
480 | printf("layout for %s set to %d\n", devname, array.layout); | |
481 | return 0; | |
482 | ||
483 | case 1: /* raid_disks and size can each be changed. They are independant */ | |
484 | ||
485 | if (level != UnSet && level != 1) { | |
486 | fprintf(stderr, Name ": %s: Cannot change RAID level of a RAID1 array.\n", | |
487 | devname); | |
488 | return 1; | |
489 | } | |
490 | if (chunksize || layout != UnSet) { | |
491 | fprintf(stderr, Name ": %s: Cannot change chunk size of layout for a RAID1 array.\n", | |
492 | devname); | |
493 | return 1; | |
494 | } | |
495 | ||
496 | /* Each can trigger a resync/recovery which will block the | |
497 | * other from happening. Later we could block | |
498 | * resync for the duration via 'sync_action'... | |
499 | */ | |
500 | if (raid_disks > 0) { | |
501 | array.raid_disks = raid_disks; | |
502 | if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) { | |
503 | fprintf(stderr, Name ": Cannot set raid-devices for %s: %s\n", | |
504 | devname, strerror(errno)); | |
505 | return 1; | |
506 | } | |
507 | } | |
508 | if (size >= 0) { | |
509 | array.size = size; | |
510 | if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) { | |
511 | fprintf(stderr, Name ": Cannot set device size for %s: %s\n", | |
512 | devname, strerror(errno)); | |
513 | return 1; | |
514 | } | |
515 | } | |
516 | return 0; | |
517 | ||
518 | case 4: | |
519 | case 5: | |
520 | case 6: | |
521 | st = super_by_version(array.major_version, | |
522 | array.minor_version); | |
523 | /* size can be changed independently. | |
524 | * layout/chunksize/raid_disks/level can be changed | |
525 | * though the kernel may not support it all. | |
526 | * If 'suspend_lo' is not present in devfs, then | |
527 | * these cannot be changed. | |
528 | */ | |
529 | if (size >= 0) { | |
530 | /* Cannot change other details as well.. */ | |
531 | if (layout != UnSet || | |
532 | chunksize != 0 || | |
533 | raid_disks != 0 || | |
534 | level != UnSet) { | |
535 | fprintf(stderr, Name ": %s: Cannot change shape as well as size of a %s array.\n", | |
536 | devname, c); | |
537 | return 1; | |
538 | } | |
539 | array.size = size; | |
540 | if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) { | |
541 | fprintf(stderr, Name ": Cannot set device size/shape for %s: %s\n", | |
542 | devname, strerror(errno)); | |
543 | return 1; | |
544 | } | |
545 | return 0; | |
546 | } | |
547 | /* Ok, just change the shape. This can be awkward. | |
548 | * There are three possibilities. | |
549 | * 1/ The array will shrink. We don't support this | |
550 | * possibility. Maybe one day... | |
551 | * 2/ The array will not change size. This is easy enough | |
552 | * to do, but not reliably. If the process is aborted | |
553 | * the array *will* be corrupted. So maybe we can allow | |
554 | * this but only if the user is really certain. e.g. | |
555 | * --really-risk-everything | |
556 | * 3/ The array will grow. This can be reliably achieved. | |
557 | * However the kernel's restripe routines will cheerfully | |
558 | * overwrite some early data before it is safe. So we | |
559 | * need to make a backup of the early parts of the array | |
560 | * and be ready to restore it if rebuild aborts very early. | |
561 | * | |
562 | * We backup data by writing it to all spares (there must be | |
563 | * at least 1, so even raid6->raid5 requires a spare to be | |
564 | * present). | |
565 | * | |
566 | * So: we enumerate the devices in the array and | |
567 | * make sure we can open all of them. | |
568 | * Then we freeze the early part of the array and | |
569 | * backup to the various spares. | |
570 | * Then we request changes and start the reshape. | |
571 | * Monitor progress until it has passed the danger zone. | |
572 | * and finally invalidate the copied data and unfreeze the | |
573 | * start of the array. | |
574 | * | |
575 | * Before we can do this we need to decide: | |
576 | * - will the array grow? Just calculate size | |
577 | * - how much needs to be saved: count stripes. | |
578 | * - where to save data... good question. | |
579 | * | |
580 | */ | |
581 | nlevel = olevel = array.level; | |
582 | nchunk = ochunk = array.chunk_size; | |
583 | nlayout = olayout = array.layout; | |
584 | ndisks = odisks = array.raid_disks; | |
585 | ||
586 | if (level != UnSet) nlevel = level; | |
587 | if (chunksize) nchunk = chunksize; | |
588 | if (layout != UnSet) nlayout = layout; | |
589 | if (raid_disks) ndisks = raid_disks; | |
590 | ||
591 | odata = odisks-1; | |
592 | if (olevel == 6) odata--; /* number of data disks */ | |
593 | ndata = ndisks-1; | |
594 | if (nlevel == 6) ndata--; | |
595 | ||
596 | if (ndata < odata) { | |
597 | fprintf(stderr, Name ": %s: Cannot reduce number of data disks (yet).\n", | |
598 | devname); | |
599 | return 1; | |
600 | } | |
601 | if (ndata == odata) { | |
602 | fprintf(stderr, Name ": %s: Cannot reshape array without increasing size (yet).\n", | |
603 | devname); | |
604 | return 1; | |
605 | } | |
606 | /* Well, it is growing... so how much do we need to backup. | |
607 | * Need to backup a full number of new-stripes, such that the | |
608 | * last one does not over-write any place that it would be read | |
609 | * from | |
610 | */ | |
611 | nstripe = ostripe = 0; | |
612 | while (nstripe >= ostripe) { | |
613 | nstripe += nchunk/512; | |
614 | last_block = nstripe * ndata; | |
615 | ostripe = last_block / odata / (ochunk/512) * (ochunk/512); | |
616 | } | |
617 | printf("mdadm: Need to backup %lluK of critical section..\n", last_block/2); | |
618 | ||
619 | sra = sysfs_read(fd, 0, | |
620 | GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE| | |
621 | GET_CACHE); | |
622 | if (!sra) { | |
623 | fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n", | |
624 | devname); | |
625 | return 1; | |
626 | } | |
627 | ||
628 | if (last_block >= sra->component_size/2) { | |
629 | fprintf(stderr, Name ": %s: Something wrong - reshape aborted\n", | |
630 | devname); | |
631 | return 1; | |
632 | } | |
633 | if (sra->spares == 0 && backup_file == NULL) { | |
634 | fprintf(stderr, Name ": %s: Cannot grow - need a spare or backup-file to backup critical section\n", | |
635 | devname); | |
636 | return 1; | |
637 | } | |
638 | ||
639 | nrdisks = array.nr_disks + sra->spares; | |
640 | /* Now we need to open all these devices so we can read/write. | |
641 | */ | |
642 | fdlist = malloc((1+nrdisks) * sizeof(int)); | |
643 | offsets = malloc((1+nrdisks) * sizeof(offsets[0])); | |
644 | if (!fdlist || !offsets) { | |
645 | fprintf(stderr, Name ": malloc failed: grow aborted\n"); | |
646 | return 1; | |
647 | } | |
648 | for (d=0; d <= nrdisks; d++) | |
649 | fdlist[d] = -1; | |
650 | d = array.raid_disks; | |
651 | for (sd = sra->devs; sd; sd=sd->next) { | |
652 | if (sd->state & (1<<MD_DISK_FAULTY)) | |
653 | continue; | |
654 | if (sd->state & (1<<MD_DISK_SYNC)) { | |
655 | char *dn = map_dev(sd->major, sd->minor, 1); | |
656 | fdlist[sd->role] = dev_open(dn, O_RDONLY); | |
657 | offsets[sd->role] = sd->offset; | |
658 | if (fdlist[sd->role] < 0) { | |
659 | fprintf(stderr, Name ": %s: cannot open component %s\n", | |
660 | devname, dn?dn:"-unknown-"); | |
661 | goto abort; | |
662 | } | |
663 | } else { | |
664 | /* spare */ | |
665 | char *dn = map_dev(sd->major, sd->minor, 1); | |
666 | fdlist[d] = dev_open(dn, O_RDWR); | |
667 | offsets[d] = sd->offset; | |
668 | if (fdlist[d]<0) { | |
669 | fprintf(stderr, Name ": %s: cannot open component %s\n", | |
670 | devname, dn?dn:"-unknown"); | |
671 | goto abort; | |
672 | } | |
673 | d++; | |
674 | } | |
675 | } | |
676 | for (i=0 ; i<array.raid_disks; i++) | |
677 | if (fdlist[i] < 0) { | |
678 | fprintf(stderr, Name ": %s: failed to find device %d. Array might be degraded.\n" | |
679 | " --grow aborted\n", devname, i); | |
680 | goto abort; | |
681 | } | |
682 | spares = sra->spares; | |
683 | if (backup_file) { | |
684 | fdlist[d] = open(backup_file, O_RDWR|O_CREAT|O_EXCL, 0600); | |
685 | if (fdlist[d] < 0) { | |
686 | fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n", | |
687 | devname, backup_file, strerror(errno)); | |
688 | goto abort; | |
689 | } | |
690 | offsets[d] = 8; | |
691 | d++; | |
692 | spares++; | |
693 | } | |
694 | if (fdlist[array.raid_disks] < 0) { | |
695 | fprintf(stderr, Name ": %s: failed to find a spare and no backup-file given - --grow aborted\n", | |
696 | devname); | |
697 | goto abort; | |
698 | } | |
699 | ||
700 | /* Find a superblock */ | |
701 | if (st->ss->load_super(st, fdlist[0], &super, NULL)) { | |
702 | fprintf(stderr, Name ": %s: Cannot find a superblock\n", | |
703 | devname); | |
704 | goto abort; | |
705 | } | |
706 | ||
707 | ||
708 | memcpy(bsb.magic, "md_backup_data-1", 16); | |
709 | st->ss->uuid_from_super((int*)&bsb.set_uuid, super); | |
710 | bsb.mtime = __cpu_to_le64(time(0)); | |
711 | bsb.arraystart = 0; | |
712 | bsb.length = __cpu_to_le64(last_block); | |
713 | ||
714 | /* Decide offset for the backup, llseek the spares, and write | |
715 | * a leading superblock 4K earlier. | |
716 | */ | |
717 | for (i=array.raid_disks; i<d; i++) { | |
718 | char buf[4096]; | |
719 | if (i==d-1 && backup_file) { | |
720 | /* This is the backup file */ | |
721 | offsets[i] = 8; | |
722 | } else | |
723 | offsets[i] += sra->component_size - last_block - 8; | |
724 | if (lseek64(fdlist[i], (offsets[i]<<9) - 4096, 0) | |
725 | != (offsets[i]<<9) - 4096) { | |
726 | fprintf(stderr, Name ": could not seek...\n"); | |
727 | goto abort; | |
728 | } | |
729 | memset(buf, 0, sizeof(buf)); | |
730 | bsb.devstart = __cpu_to_le64(offsets[i]); | |
731 | bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb)); | |
732 | memcpy(buf, &bsb, sizeof(bsb)); | |
733 | if (write(fdlist[i], buf, 4096) != 4096) { | |
734 | fprintf(stderr, Name ": could not write leading superblock\n"); | |
735 | goto abort; | |
736 | } | |
737 | } | |
738 | array.level = nlevel; | |
739 | array.raid_disks = ndisks; | |
740 | array.chunk_size = nchunk; | |
741 | array.layout = nlayout; | |
742 | if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) { | |
743 | if (errno == ENOSPC) { | |
744 | /* stripe cache is not big enough. | |
745 | * It needs to be 4 times chunksize_size, | |
746 | * and we assume pagesize is 4K | |
747 | */ | |
748 | if (sra->cache_size < 4 * (nchunk/4096)) { | |
749 | sysfs_set_num(sra, NULL, | |
750 | "stripe_cache_size", | |
751 | 4 * (nchunk/4096) +1); | |
752 | if (ioctl(fd, SET_ARRAY_INFO, | |
753 | &array) == 0) | |
754 | goto ok; | |
755 | } | |
756 | } | |
757 | fprintf(stderr, Name ": Cannot set device size/shape for %s: %s\n", | |
758 | devname, strerror(errno)); | |
759 | goto abort; | |
760 | } | |
761 | ok: ; | |
762 | ||
763 | /* suspend the relevant region */ | |
764 | sysfs_set_num(sra, NULL, "suspend_hi", 0); /* just in case */ | |
765 | if (sysfs_set_num(sra, NULL, "suspend_lo", 0) < 0 || | |
766 | sysfs_set_num(sra, NULL, "suspend_hi", last_block) < 0) { | |
767 | fprintf(stderr, Name ": %s: failed to suspend device.\n", | |
768 | devname); | |
769 | goto abort_resume; | |
770 | } | |
771 | ||
772 | ||
773 | err = save_stripes(fdlist, offsets, | |
774 | odisks, ochunk, olevel, olayout, | |
775 | spares, fdlist+odisks, | |
776 | 0ULL, last_block*512); | |
777 | ||
778 | /* abort if there was an error */ | |
779 | if (err < 0) { | |
780 | fprintf(stderr, Name ": %s: failed to save critical region\n", | |
781 | devname); | |
782 | goto abort_resume; | |
783 | } | |
784 | ||
785 | for (i=odisks; i<d ; i++) { | |
786 | bsb.devstart = __cpu_to_le64(offsets[i]); | |
787 | bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb)); | |
788 | if (lseek64(fdlist[i], (offsets[i]+last_block)<<9, 0) < 0 || | |
789 | write(fdlist[i], &bsb, sizeof(bsb)) != sizeof(bsb) || | |
790 | fsync(fdlist[i]) != 0) { | |
791 | fprintf(stderr, Name ": %s: fail to save metadata for critical region backups.\n", | |
792 | devname); | |
793 | goto abort_resume; | |
794 | } | |
795 | } | |
796 | ||
797 | /* start the reshape happening */ | |
798 | if (sysfs_set_str(sra, NULL, "sync_action", "reshape") < 0) { | |
799 | fprintf(stderr, Name ": %s: failed to initiate reshape\n", | |
800 | devname); | |
801 | goto abort_resume; | |
802 | } | |
803 | /* wait for reshape to pass the critical region */ | |
804 | while(1) { | |
805 | unsigned long long comp; | |
806 | if (sysfs_get_ll(sra, NULL, "sync_completed", &comp)<0) { | |
807 | sleep(5); | |
808 | break; | |
809 | } | |
810 | if (comp >= nstripe) | |
811 | break; | |
812 | sleep(1); | |
813 | } | |
814 | ||
815 | /* invalidate superblocks */ | |
816 | memset(&bsb, 0, sizeof(bsb)); | |
817 | for (i=odisks; i<d ; i++) { | |
818 | lseek64(fdlist[i], (offsets[i]+last_block)<<9, 0); | |
819 | if (write(fdlist[i], &bsb, sizeof(bsb)) < 0) { | |
820 | fprintf(stderr, Name ": %s: failed to invalidate metadata for raid disk %d\n", | |
821 | devname, i); | |
822 | } | |
823 | } | |
824 | ||
825 | /* unsuspend. */ | |
826 | sysfs_set_num(sra, NULL, "suspend_lo", last_block); | |
827 | ||
828 | for (i=0; i<d; i++) | |
829 | if (fdlist[i] >= 0) | |
830 | close(fdlist[i]); | |
831 | free(fdlist); | |
832 | free(offsets); | |
833 | if (backup_file) | |
834 | unlink(backup_file); | |
835 | ||
836 | printf(Name ": ... critical section passed.\n"); | |
837 | break; | |
838 | } | |
839 | return 0; | |
840 | ||
841 | ||
842 | abort_resume: | |
843 | sysfs_set_num(sra, NULL, "suspend_lo", last_block); | |
844 | abort: | |
845 | for (i=0; i<array.nr_disks; i++) | |
846 | if (fdlist[i] >= 0) | |
847 | close(fdlist[i]); | |
848 | free(fdlist); | |
849 | free(offsets); | |
850 | if (backup_file) | |
851 | unlink(backup_file); | |
852 | return 1; | |
853 | ||
854 | } | |
855 | ||
856 | /* | |
857 | * If any spare contains md_back_data-1 which is recent wrt mtime, | |
858 | * write that data into the array and update the super blocks with | |
859 | * the new reshape_progress | |
860 | */ | |
861 | int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt, char *backup_file) | |
862 | { | |
863 | int i, j; | |
864 | int old_disks; | |
865 | int err = 0; | |
866 | unsigned long long *offsets; | |
867 | ||
868 | if (info->delta_disks < 0) | |
869 | return 1; /* cannot handle a shrink */ | |
870 | if (info->new_level != info->array.level || | |
871 | info->new_layout != info->array.layout || | |
872 | info->new_chunk != info->array.chunk_size) | |
873 | return 1; /* Can only handle change in disks */ | |
874 | ||
875 | old_disks = info->array.raid_disks - info->delta_disks; | |
876 | ||
877 | for (i=old_disks-(backup_file?1:0); i<cnt; i++) { | |
878 | void *super = NULL; | |
879 | struct mdinfo dinfo; | |
880 | struct mdp_backup_super bsb; | |
881 | char buf[4096]; | |
882 | int fd; | |
883 | ||
884 | /* This was a spare and may have some saved data on it. | |
885 | * Load the superblock, find and load the | |
886 | * backup_super_block. | |
887 | * If either fail, go on to next device. | |
888 | * If the backup contains no new info, just return | |
889 | * else restore data and update all superblocks | |
890 | */ | |
891 | if (i == old_disks-1) { | |
892 | fd = open(backup_file, O_RDONLY); | |
893 | if (fd<0) | |
894 | continue; | |
895 | } else { | |
896 | fd = fdlist[i]; | |
897 | if (fd < 0) | |
898 | continue; | |
899 | if (st->ss->load_super(st, fd, &super, NULL)) | |
900 | continue; | |
901 | ||
902 | st->ss->getinfo_super(&dinfo, super); | |
903 | free(super); super = NULL; | |
904 | if (lseek64(fd, | |
905 | (dinfo.data_offset + dinfo.component_size - 8) <<9, | |
906 | 0) < 0) | |
907 | continue; /* Cannot seek */ | |
908 | } | |
909 | if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb)) | |
910 | continue; /* Cannot read */ | |
911 | if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0) | |
912 | continue; | |
913 | if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb))) | |
914 | continue; /* bad checksum */ | |
915 | if (memcmp(bsb.set_uuid,info->uuid, 16) != 0) | |
916 | continue; /* Wrong uuid */ | |
917 | ||
918 | if (info->array.utime > __le64_to_cpu(bsb.mtime) + 3600 || | |
919 | info->array.utime < __le64_to_cpu(bsb.mtime)) | |
920 | continue; /* time stamp is too bad */ | |
921 | ||
922 | if (__le64_to_cpu(bsb.arraystart) != 0) | |
923 | continue; /* Can only handle backup from start of array */ | |
924 | if (__le64_to_cpu(bsb.length) < | |
925 | info->reshape_progress) | |
926 | continue; /* No new data here */ | |
927 | ||
928 | if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0) | |
929 | continue; /* Cannot seek */ | |
930 | /* There should be a duplicate backup superblock 4k before here */ | |
931 | if (lseek64(fd, -4096, 1) < 0 || | |
932 | read(fd, buf, 4096) != 4096 || | |
933 | memcmp(buf, &bsb, sizeof(bsb)) != 0) | |
934 | continue; /* Cannot find leading superblock */ | |
935 | ||
936 | /* Now need the data offsets for all devices. */ | |
937 | offsets = malloc(sizeof(*offsets)*info->array.raid_disks); | |
938 | for(j=0; j<info->array.raid_disks; j++) { | |
939 | if (fdlist[j] < 0) | |
940 | continue; | |
941 | if (st->ss->load_super(st, fdlist[j], &super, NULL)) | |
942 | /* FIXME should be this be an error */ | |
943 | continue; | |
944 | st->ss->getinfo_super(&dinfo, super); | |
945 | free(super); super = NULL; | |
946 | offsets[j] = dinfo.data_offset; | |
947 | } | |
948 | printf(Name ": restoring critical section\n"); | |
949 | ||
950 | if (restore_stripes(fdlist, offsets, | |
951 | info->array.raid_disks, | |
952 | info->new_chunk, | |
953 | info->new_level, | |
954 | info->new_layout, | |
955 | fd, __le64_to_cpu(bsb.devstart)*512, | |
956 | 0, __le64_to_cpu(bsb.length)*512)) { | |
957 | /* didn't succeed, so giveup */ | |
958 | return -1; | |
959 | } | |
960 | ||
961 | /* Ok, so the data is restored. Let's update those superblocks. */ | |
962 | ||
963 | for (j=0; j<info->array.raid_disks; j++) { | |
964 | if (fdlist[j] < 0) continue; | |
965 | if (st->ss->load_super(st, fdlist[j], &super, NULL)) | |
966 | continue; | |
967 | st->ss->getinfo_super(&dinfo, super); | |
968 | dinfo.reshape_progress = __le64_to_cpu(bsb.length); | |
969 | st->ss->update_super(&dinfo, super, "_reshape_progress",NULL,0, 0, NULL); | |
970 | st->ss->store_super(st, fdlist[j], super); | |
971 | free(super); | |
972 | } | |
973 | ||
974 | /* And we are done! */ | |
975 | return 0; | |
976 | } | |
977 | return err; | |
978 | } |