]>
Commit | Line | Data |
---|---|---|
e5329c37 NB |
1 | /* |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
4f589ad0 | 4 | * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de> |
e5329c37 NB |
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" | |
7236ee7a | 31 | #include <sys/mman.h> |
e5329c37 NB |
32 | |
33 | #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN) | |
34 | #error no endian defined | |
35 | #endif | |
36 | #include "md_u.h" | |
37 | #include "md_p.h" | |
38 | ||
39 | int Grow_Add_device(char *devname, int fd, char *newdev) | |
40 | { | |
41 | /* Add a device to an active array. | |
42 | * Currently, just extend a linear array. | |
43 | * This requires writing a new superblock on the | |
44 | * new device, calling the kernel to add the device, | |
45 | * and if that succeeds, update the superblock on | |
46 | * all other devices. | |
47 | * This means that we need to *find* all other devices. | |
48 | */ | |
4b1ac34b NB |
49 | struct mdinfo info; |
50 | ||
e5329c37 NB |
51 | struct stat stb; |
52 | int nfd, fd2; | |
53 | int d, nd; | |
82d9eba6 | 54 | struct supertype *st = NULL; |
aba69144 | 55 | |
e5329c37 | 56 | |
4b1ac34b | 57 | if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) { |
e5329c37 NB |
58 | fprintf(stderr, Name ": cannot get array info for %s\n", devname); |
59 | return 1; | |
60 | } | |
61 | ||
1686dc25 | 62 | st = super_by_fd(fd); |
82d9eba6 | 63 | if (!st) { |
f9ce90ba NB |
64 | fprintf(stderr, Name ": cannot handle arrays with superblock version %d\n", info.array.major_version); |
65 | return 1; | |
66 | } | |
67 | ||
4b1ac34b | 68 | if (info.array.level != -1) { |
e5329c37 NB |
69 | fprintf(stderr, Name ": can only add devices to linear arrays\n"); |
70 | return 1; | |
71 | } | |
72 | ||
6416d527 | 73 | nfd = open(newdev, O_RDWR|O_EXCL|O_DIRECT); |
e5329c37 NB |
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 */ | |
4b1ac34b | 85 | for (d=0 ; d < info.array.raid_disks ; d++) { |
e5329c37 NB |
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 | } | |
16c6fa80 | 95 | dv = map_dev(disk.major, disk.minor, 1); |
e5329c37 NB |
96 | if (!dv) { |
97 | fprintf(stderr, Name ": cannot find device file for device %d\n", | |
98 | d); | |
99 | return 1; | |
100 | } | |
16c6fa80 | 101 | fd2 = dev_open(dv, O_RDWR); |
e5329c37 NB |
102 | if (!fd2) { |
103 | fprintf(stderr, Name ": cannot open device file %s\n", dv); | |
104 | return 1; | |
105 | } | |
3da92f27 NB |
106 | st->ss->free_super(st); |
107 | ||
108 | if (st->ss->load_super(st, fd2, NULL)) { | |
e5329c37 NB |
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 | */ | |
aba69144 | 118 | |
4b1ac34b NB |
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); | |
3da92f27 | 124 | st->ss->update_super(st, &info, "linear-grow-new", newdev, |
f752781f | 125 | 0, 0, NULL); |
e5329c37 | 126 | |
3da92f27 | 127 | if (st->ss->store_super(st, nfd)) { |
f752781f NB |
128 | fprintf(stderr, Name ": Cannot store new superblock on %s\n", |
129 | newdev); | |
e5329c37 NB |
130 | close(nfd); |
131 | return 1; | |
132 | } | |
e5329c37 | 133 | close(nfd); |
4b1ac34b NB |
134 | |
135 | if (ioctl(fd, ADD_NEW_DISK, &info.disk) != 0) { | |
e5329c37 NB |
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 | ||
4b1ac34b | 143 | if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) { |
e5329c37 NB |
144 | fprintf(stderr, Name ": cannot get array info for %s\n", devname); |
145 | return 1; | |
146 | } | |
147 | ||
148 | nd = d; | |
4b1ac34b | 149 | for (d=0 ; d < info.array.raid_disks ; d++) { |
e5329c37 NB |
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 | } | |
16c6fa80 | 159 | dv = map_dev(disk.major, disk.minor, 1); |
e5329c37 NB |
160 | if (!dv) { |
161 | fprintf(stderr, Name ": cannot find device file for device %d\n", | |
162 | d); | |
163 | return 1; | |
164 | } | |
16c6fa80 | 165 | fd2 = dev_open(dv, O_RDWR); |
e5329c37 NB |
166 | if (fd2 < 0) { |
167 | fprintf(stderr, Name ": cannot open device file %s\n", dv); | |
168 | return 1; | |
169 | } | |
3da92f27 | 170 | if (st->ss->load_super(st, fd2, NULL)) { |
e5329c37 NB |
171 | fprintf(stderr, Name ": cannot find super block on %s\n", dv); |
172 | close(fd); | |
173 | return 1; | |
174 | } | |
4b1ac34b NB |
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; | |
f752781f | 179 | |
3da92f27 | 180 | st->ss->update_super(st, &info, "linear-grow-update", dv, |
f752781f | 181 | 0, 0, NULL); |
aba69144 | 182 | |
3da92f27 | 183 | if (st->ss->store_super(st, fd2)) { |
e5329c37 NB |
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 | } | |
f5e166fe | 193 | |
8fac0577 | 194 | int Grow_addbitmap(char *devname, int fd, char *file, int chunk, int delay, int write_behind, int force) |
f5e166fe NB |
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; | |
dcec9ee5 NB |
208 | int major = BITMAP_MAJOR_HI; |
209 | int vers = md_get_version(fd); | |
8fac0577 | 210 | unsigned long long bitmapsize, array_size; |
dcec9ee5 NB |
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 | } | |
f5e166fe NB |
219 | |
220 | if (ioctl(fd, GET_BITMAP_FILE, &bmf) != 0) { | |
353632d9 | 221 | if (errno == ENOMEM) |
f5e166fe NB |
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]) { | |
fe80f49b NB |
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 | } | |
f5e166fe NB |
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)) { | |
fe80f49b NB |
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 | } | |
f5e166fe NB |
253 | fprintf(stderr, Name ": Internal bitmap already present on %s\n", |
254 | devname); | |
255 | return 1; | |
256 | } | |
5b28bd56 NB |
257 | if (array.level <= 0) { |
258 | fprintf(stderr, Name ": Bitmaps not meaningful with level %s\n", | |
259 | map_num(pers, array.level)?:"of this array"); | |
260 | return 1; | |
261 | } | |
8fac0577 NB |
262 | bitmapsize = array.size; |
263 | bitmapsize <<= 1; | |
beae1dfe | 264 | if (get_dev_size(fd, NULL, &array_size) && |
8fac0577 NB |
265 | array_size > (0x7fffffffULL<<9)) { |
266 | /* Array is big enough that we cannot trust array.size | |
267 | * try other approaches | |
268 | */ | |
269 | bitmapsize = get_component_size(fd); | |
270 | } | |
8fac0577 NB |
271 | if (bitmapsize == 0) { |
272 | fprintf(stderr, Name ": Cannot reliably determine size of array to create bitmap - sorry.\n"); | |
273 | return 1; | |
274 | } | |
275 | ||
f9c25f1d | 276 | if (array.level == 10) { |
8686f3ed | 277 | int ncopies = (array.layout&255)*((array.layout>>8)&255); |
f9c25f1d NB |
278 | bitmapsize = bitmapsize * array.raid_disks / ncopies; |
279 | } | |
280 | ||
1686dc25 | 281 | st = super_by_fd(fd); |
f5e166fe NB |
282 | if (!st) { |
283 | fprintf(stderr, Name ": Cannot understand version %d.%d\n", | |
284 | array.major_version, array.minor_version); | |
285 | return 1; | |
286 | } | |
fe80f49b NB |
287 | if (strcmp(file, "none") == 0) { |
288 | fprintf(stderr, Name ": no bitmap found on %s\n", devname); | |
289 | return 1; | |
290 | } else if (strcmp(file, "internal") == 0) { | |
f5e166fe | 291 | int d; |
ea329559 | 292 | for (d=0; d< st->max_devs; d++) { |
f5e166fe NB |
293 | mdu_disk_info_t disk; |
294 | char *dv; | |
295 | disk.number = d; | |
296 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) | |
297 | continue; | |
298 | if (disk.major == 0 && | |
299 | disk.minor == 0) | |
300 | continue; | |
301 | if ((disk.state & (1<<MD_DISK_SYNC))==0) | |
302 | continue; | |
16c6fa80 | 303 | dv = map_dev(disk.major, disk.minor, 1); |
f5e166fe | 304 | if (dv) { |
16c6fa80 | 305 | int fd2 = dev_open(dv, O_RDWR); |
f5e166fe NB |
306 | if (fd2 < 0) |
307 | continue; | |
3da92f27 | 308 | if (st->ss->load_super(st, fd2, NULL)==0) { |
199171a2 | 309 | if (st->ss->add_internal_bitmap( |
3da92f27 | 310 | st, |
199171a2 NB |
311 | &chunk, delay, write_behind, |
312 | bitmapsize, 0, major) | |
313 | ) | |
3da92f27 | 314 | st->ss->write_bitmap(st, fd2); |
21e92547 NB |
315 | else { |
316 | fprintf(stderr, Name ": failed to create internal bitmap - chunksize problem.\n"); | |
317 | close(fd2); | |
318 | return 1; | |
319 | } | |
f5e166fe NB |
320 | } |
321 | close(fd2); | |
322 | } | |
323 | } | |
324 | array.state |= (1<<MD_SB_BITMAP_PRESENT); | |
325 | if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) { | |
326 | fprintf(stderr, Name ": failed to set internal bitmap.\n"); | |
327 | return 1; | |
328 | } | |
fe80f49b NB |
329 | } else { |
330 | int uuid[4]; | |
331 | int bitmap_fd; | |
332 | int d; | |
333 | int max_devs = st->max_devs; | |
fe80f49b NB |
334 | |
335 | /* try to load a superblock */ | |
336 | for (d=0; d<max_devs; d++) { | |
337 | mdu_disk_info_t disk; | |
338 | char *dv; | |
339 | int fd2; | |
340 | disk.number = d; | |
341 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) | |
342 | continue; | |
343 | if ((disk.major==0 && disk.minor==0) || | |
344 | (disk.state & (1<<MD_DISK_REMOVED))) | |
345 | continue; | |
16c6fa80 | 346 | dv = map_dev(disk.major, disk.minor, 1); |
fe80f49b | 347 | if (!dv) continue; |
16c6fa80 | 348 | fd2 = dev_open(dv, O_RDONLY); |
fe80f49b | 349 | if (fd2 >= 0 && |
3da92f27 | 350 | st->ss->load_super(st, fd2, NULL) == 0) { |
fe80f49b | 351 | close(fd2); |
3da92f27 | 352 | st->ss->uuid_from_super(st, uuid); |
fe80f49b NB |
353 | break; |
354 | } | |
355 | close(fd2); | |
356 | } | |
357 | if (d == max_devs) { | |
358 | fprintf(stderr, Name ": cannot find UUID for array!\n"); | |
359 | return 1; | |
360 | } | |
8fac0577 | 361 | if (CreateBitmap(file, force, (char*)uuid, chunk, |
f9c25f1d | 362 | delay, write_behind, bitmapsize, major)) { |
fe80f49b NB |
363 | return 1; |
364 | } | |
365 | bitmap_fd = open(file, O_RDWR); | |
366 | if (bitmap_fd < 0) { | |
8fac0577 | 367 | fprintf(stderr, Name ": weird: %s cannot be opened\n", |
fe80f49b NB |
368 | file); |
369 | return 1; | |
370 | } | |
371 | if (ioctl(fd, SET_BITMAP_FILE, bitmap_fd) < 0) { | |
372 | fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n", | |
373 | devname, strerror(errno)); | |
374 | return 1; | |
375 | } | |
376 | } | |
f5e166fe NB |
377 | |
378 | return 0; | |
379 | } | |
380 | ||
e86c9dd6 NB |
381 | |
382 | /* | |
383 | * When reshaping an array we might need to backup some data. | |
384 | * This is written to all spares with a 'super_block' describing it. | |
385 | * The superblock goes 1K form the end of the used space on the | |
386 | * device. | |
387 | * It if written after the backup is complete. | |
388 | * It has the following structure. | |
389 | */ | |
390 | ||
391 | struct mdp_backup_super { | |
7236ee7a | 392 | char magic[16]; /* md_backup_data-1 or -2 */ |
e86c9dd6 NB |
393 | __u8 set_uuid[16]; |
394 | __u64 mtime; | |
395 | /* start/sizes in 512byte sectors */ | |
7236ee7a | 396 | __u64 devstart; /* address on backup device/file of data */ |
e86c9dd6 NB |
397 | __u64 arraystart; |
398 | __u64 length; | |
399 | __u32 sb_csum; /* csum of preceeding bytes. */ | |
7236ee7a N |
400 | __u32 pad1; |
401 | __u64 devstart2; /* offset in to data of second section */ | |
402 | __u64 arraystart2; | |
403 | __u64 length2; | |
404 | __u32 sb_csum2; /* csum of preceeding bytes. */ | |
405 | __u8 pad[512-68-32]; | |
94a20f0c | 406 | } __attribute__((aligned(512))) bsb; |
e86c9dd6 NB |
407 | |
408 | int bsb_csum(char *buf, int len) | |
409 | { | |
410 | int i; | |
411 | int csum = 0; | |
412 | for (i=0; i<len; i++) | |
413 | csum = (csum<<3) + buf[0]; | |
414 | return __cpu_to_le32(csum); | |
415 | } | |
416 | ||
7236ee7a N |
417 | static int child_grow(int afd, struct mdinfo *sra, unsigned long blocks, |
418 | int *fds, unsigned long long *offsets, | |
419 | int disks, int chunk, int level, int layout, int data, | |
420 | int dests, int *destfd, unsigned long long *destoffsets); | |
421 | static int child_shrink(int afd, struct mdinfo *sra, unsigned long blocks, | |
422 | int *fds, unsigned long long *offsets, | |
423 | int disks, int chunk, int level, int layout, int data, | |
424 | int dests, int *destfd, unsigned long long *destoffsets); | |
425 | static int child_same_size(int afd, struct mdinfo *sra, unsigned long blocks, | |
426 | int *fds, unsigned long long *offsets, | |
427 | int disks, int chunk, int level, int layout, int data, | |
428 | int dests, int *destfd, unsigned long long *destoffsets); | |
429 | ||
430 | int freeze_array(struct mdinfo *sra) | |
431 | { | |
432 | /* Try to freeze resync on this array. | |
433 | * Return -1 if the array is busy, | |
434 | * return 0 if this kernel doesn't support 'frozen' | |
435 | * return 1 if it worked. | |
436 | */ | |
437 | char buf[20]; | |
438 | if (sysfs_get_str(sra, NULL, "sync_action", buf, 20) <= 0) | |
439 | return 0; | |
440 | if (strcmp(buf, "idle\n") != 0 && | |
441 | strcmp(buf, "frozen\n") != 0) | |
442 | return -1; | |
443 | if (sysfs_set_str(sra, NULL, "sync_action", "frozen") < 0) | |
444 | return 0; | |
445 | return 1; | |
446 | } | |
447 | ||
448 | void unfreeze_array(struct mdinfo *sra, int frozen) | |
449 | { | |
450 | /* If 'frozen' is 1, unfreeze the array */ | |
451 | if (frozen > 0) | |
452 | sysfs_set_str(sra, NULL, "sync_action", "idle"); | |
453 | } | |
454 | ||
455 | void wait_reshape(struct mdinfo *sra) | |
456 | { | |
457 | int fd = sysfs_get_fd(sra, NULL, "sync_action"); | |
458 | char action[20]; | |
459 | ||
460 | do { | |
461 | fd_set rfds; | |
462 | FD_ZERO(&rfds); | |
463 | FD_SET(fd, &rfds); | |
464 | select(fd+1, NULL, NULL, &rfds, NULL); | |
465 | ||
466 | if (sysfs_fd_get_str(fd, action, 20) < 0) { | |
467 | close(fd); | |
468 | return; | |
469 | } | |
470 | } while (strncmp(action, "reshape", 7) == 0); | |
471 | } | |
472 | ||
473 | ||
06b0d786 | 474 | int Grow_reshape(char *devname, int fd, int quiet, char *backup_file, |
e86c9dd6 | 475 | long long size, |
19678e53 | 476 | int level, char *layout_str, int chunksize, int raid_disks) |
e86c9dd6 NB |
477 | { |
478 | /* Make some changes in the shape of an array. | |
479 | * The kernel must support the change. | |
7236ee7a N |
480 | * |
481 | * There are three different changes. Each can trigger | |
482 | * a resync or recovery so we freeze that until we have | |
483 | * requested everything (if kernel supports freezing - 2.6.30). | |
484 | * The steps are: | |
485 | * - change size (i.e. component_size) | |
486 | * - change level | |
487 | * - change layout/chunksize/ndisks | |
488 | * | |
489 | * The last can require a reshape. It is different on different | |
490 | * levels so we need to check the level before actioning it. | |
491 | * Some times the level change needs to be requested after the | |
492 | * reshape (e.g. raid6->raid5, raid5->raid0) | |
493 | * | |
e86c9dd6 | 494 | */ |
7236ee7a | 495 | struct mdu_array_info_s array, orig; |
e86c9dd6 | 496 | char *c; |
7236ee7a | 497 | int rv = 0; |
e86c9dd6 NB |
498 | struct supertype *st; |
499 | ||
e86c9dd6 NB |
500 | int nchunk, ochunk; |
501 | int nlayout, olayout; | |
502 | int ndisks, odisks; | |
503 | int ndata, odata; | |
7236ee7a N |
504 | int orig_level = UnSet; |
505 | char alt_layout[40]; | |
e86c9dd6 NB |
506 | int *fdlist; |
507 | unsigned long long *offsets; | |
7236ee7a | 508 | int d, i; |
e86c9dd6 NB |
509 | int nrdisks; |
510 | int err; | |
7236ee7a N |
511 | int frozen; |
512 | unsigned long a,b, blocks, stripes; | |
513 | int cache; | |
514 | unsigned long long array_size; | |
515 | int changed = 0; | |
516 | int done; | |
e86c9dd6 | 517 | |
7e0f6979 | 518 | struct mdinfo *sra; |
06c7f68e | 519 | struct mdinfo *sd; |
e86c9dd6 NB |
520 | |
521 | if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) { | |
522 | fprintf(stderr, Name ": %s is not an active md array - aborting\n", | |
523 | devname); | |
524 | return 1; | |
525 | } | |
7236ee7a N |
526 | sra = sysfs_read(fd, 0, GET_LEVEL); |
527 | frozen = freeze_array(sra); | |
528 | if (frozen < 0) { | |
529 | fprintf(stderr, Name ": %s is performing resync/recovery and cannot" | |
530 | " be reshaped\n", devname); | |
531 | return 1; | |
532 | } | |
533 | ||
534 | /* ========= set size =============== */ | |
535 | if (size >= 0 && (size == 0 || size != array.size)) { | |
536 | array.size = size; | |
537 | if (array.size != size) { | |
538 | /* got truncated to 32bit, write to | |
539 | * component_size instead | |
540 | */ | |
541 | if (sra) | |
542 | rv = sysfs_set_num(sra, NULL, | |
543 | "component_size", size); | |
544 | else | |
545 | rv = -1; | |
546 | } else | |
547 | rv = ioctl(fd, SET_ARRAY_INFO, &array); | |
548 | if (rv != 0) { | |
549 | fprintf(stderr, Name ": Cannot set device size for %s: %s\n", | |
550 | devname, strerror(errno)); | |
551 | rv = 1; | |
552 | goto release; | |
553 | } | |
554 | ioctl(fd, GET_ARRAY_INFO, &array); | |
555 | if (!quiet) | |
556 | fprintf(stderr, Name ": component size of %s has been set to %dK\n", | |
557 | devname, array.size); | |
558 | changed = 1; | |
559 | } | |
560 | ||
561 | /* ======= set level =========== */ | |
562 | if (level != UnSet && level != array.level) { | |
563 | /* Trying to change the level. | |
564 | * We might need to change layout first and schedule a | |
565 | * level change for later. | |
566 | * Level changes that can happen immediately are: | |
567 | * 0->4,5,6 1->5 4->5,6 5->1,6 | |
568 | * Level changes that need a layout change first are: | |
569 | * 6->5,4,0 : need a -6 layout, or parity-last | |
570 | * 5->4,0 : need parity-last | |
571 | */ | |
572 | if ((array.level == 6 || array.level == 5) && | |
573 | (level == 5 || level == 4 || level == 0)) { | |
574 | /* Don't change level yet, but choose intermediate | |
575 | * layout | |
576 | */ | |
577 | if (level == 5) { | |
578 | if (layout_str == NULL) | |
579 | switch (array.layout) { | |
580 | case ALGORITHM_LEFT_ASYMMETRIC: | |
581 | case ALGORITHM_LEFT_ASYMMETRIC_6: | |
582 | case ALGORITHM_ROTATING_N_RESTART: | |
583 | layout_str = "left-asymmetric-6"; | |
584 | break; | |
585 | case ALGORITHM_LEFT_SYMMETRIC: | |
586 | case ALGORITHM_LEFT_SYMMETRIC_6: | |
587 | case ALGORITHM_ROTATING_N_CONTINUE: | |
588 | layout_str = "left-symmetric-6"; | |
589 | break; | |
590 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
591 | case ALGORITHM_RIGHT_ASYMMETRIC_6: | |
592 | case ALGORITHM_ROTATING_ZERO_RESTART: | |
593 | layout_str = "right-asymmetric-6"; | |
594 | break; | |
595 | case ALGORITHM_RIGHT_SYMMETRIC: | |
596 | case ALGORITHM_RIGHT_SYMMETRIC_6: | |
597 | layout_str = "right-symmetric-6"; | |
598 | break; | |
599 | case ALGORITHM_PARITY_0: | |
600 | case ALGORITHM_PARITY_0_6: | |
601 | layout_str = "parity-first-6"; | |
602 | break; | |
603 | case ALGORITHM_PARITY_N: | |
604 | layout_str = "parity-last"; | |
605 | break; | |
606 | default: | |
607 | fprintf(stderr, Name ": %s: cannot" | |
608 | "convert layout to RAID5 equivalent\n", | |
609 | devname); | |
610 | rv = 1; | |
611 | goto release; | |
612 | } | |
613 | else { | |
614 | int l = map_name(r5layout, layout_str); | |
615 | if (l == UnSet) { | |
616 | fprintf(stderr, Name ": %s: layout '%s' not recognised\n", | |
617 | devname, layout_str); | |
618 | rv = 1; | |
619 | goto release; | |
620 | } | |
621 | if (l != ALGORITHM_PARITY_N) { | |
622 | /* need the -6 version */ | |
623 | char *ls = map_num(r5layout, l); | |
624 | strcat(strcpy(alt_layout, ls), | |
625 | "-6"); | |
626 | layout_str = alt_layout; | |
627 | } | |
628 | } | |
629 | if (raid_disks) | |
630 | /* The find raid6->raid5 conversion | |
631 | * will reduce the number of disks, | |
632 | * so now we need to aim higher | |
633 | */ | |
634 | raid_disks++; | |
635 | } else | |
636 | layout_str = "parity-last"; | |
637 | } else { | |
638 | c = map_num(pers, level); | |
639 | if (c == NULL) | |
640 | return 1;/* not possible */ | |
641 | err = sysfs_set_str(sra, NULL, "level", c); | |
642 | if (err) { | |
643 | fprintf(stderr, Name ": %s: could not set level to %s\n", | |
644 | devname, c); | |
645 | rv = 1; | |
646 | goto release; | |
647 | } | |
648 | orig = array; | |
649 | orig_level = orig.level; | |
650 | ioctl(fd, GET_ARRAY_INFO, &array); | |
651 | if (layout_str == NULL && | |
652 | orig.level == 5 && level == 6 && | |
653 | array.layout != orig.layout) | |
654 | layout_str = map_num(r5layout, orig.layout); | |
655 | if (!quiet) | |
656 | fprintf(stderr, Name " level of %s changed to %s\n", | |
657 | devname, c); | |
658 | changed = 1; | |
659 | } | |
660 | } | |
661 | ||
662 | /* ========= set shape (chunk_size / layout / ndisks) ============== */ | |
663 | /* Check if layout change is a no-op */ | |
664 | if (layout_str) switch(array.level) { | |
665 | case 5: | |
666 | if (array.layout == map_name(r5layout, layout_str)) | |
667 | layout_str = NULL; | |
668 | break; | |
669 | case 6: | |
670 | if (layout_str == NULL && | |
671 | ((chunksize && chunksize * 1024 != array.chunk_size) || | |
672 | (raid_disks && raid_disks != array.raid_disks)) && | |
673 | array.layout >= 16) { | |
674 | fprintf(stderr, Name | |
675 | ": %s has a non-standard layout. If you wish to preserve this\n" | |
676 | " during the reshape, please specify --layout=preserve\n" | |
677 | " If you want to change it, specify a layout or use --layout=normalise\n", | |
678 | devname); | |
679 | rv = 1; | |
680 | goto release; | |
681 | } | |
682 | if (strcmp(layout_str, "normalise") == 0 || | |
683 | strcmp(layout_str, "normalize") == 0) { | |
684 | char *hyphen; | |
685 | strcpy(alt_layout, map_num(r6layout, array.layout)); | |
686 | hyphen = strrchr(alt_layout, '-'); | |
687 | if (hyphen && strcmp(hyphen, "-6") == 0) { | |
688 | *hyphen = 0; | |
689 | layout_str = alt_layout; | |
690 | } | |
691 | } | |
692 | ||
693 | if (array.layout == map_name(r6layout, layout_str)) | |
694 | layout_str = NULL; | |
695 | if (layout_str && strcmp(layout_str, "preserve") == 0) | |
696 | layout_str = NULL; | |
697 | break; | |
698 | } | |
699 | if (layout_str == NULL | |
700 | && (chunksize == 0 || chunksize*1024 == array.chunk_size) | |
701 | && (raid_disks == 0 || raid_disks == array.raid_disks)) { | |
702 | rv = 0; | |
703 | if (level != UnSet && level != array.level) { | |
704 | /* Looks like this level change doesn't need | |
705 | * a reshape after all. | |
706 | */ | |
707 | c = map_num(pers, level); | |
708 | if (c) { | |
709 | rv = sysfs_set_str(sra, NULL, "level", c); | |
710 | if (rv) | |
711 | fprintf(stderr, Name ": %s: could not set level to %s\n", | |
712 | devname, c); | |
713 | } | |
714 | } else if (!changed && !quiet) | |
715 | fprintf(stderr, Name ": %s: no change requested\n", | |
716 | devname); | |
717 | goto release; | |
718 | } | |
719 | ||
e86c9dd6 NB |
720 | c = map_num(pers, array.level); |
721 | if (c == NULL) c = "-unknown-"; | |
722 | switch(array.level) { | |
723 | default: /* raid0, linear, multipath cannot be reconfigured */ | |
724 | fprintf(stderr, Name ": %s array %s cannot be reshaped.\n", | |
725 | c, devname); | |
7236ee7a N |
726 | rv = 1; |
727 | break; | |
e86c9dd6 NB |
728 | |
729 | case LEVEL_FAULTY: /* only 'layout' change is permitted */ | |
730 | ||
e86c9dd6 NB |
731 | if (chunksize || raid_disks) { |
732 | fprintf(stderr, Name ": %s: Cannot change chunksize or disks of a 'faulty' array\n", | |
733 | devname); | |
7236ee7a N |
734 | rv = 1; |
735 | break; | |
e86c9dd6 | 736 | } |
19678e53 | 737 | if (layout_str == NULL) |
7236ee7a | 738 | break; /* nothing to do.... */ |
e86c9dd6 | 739 | |
19678e53 N |
740 | array.layout = parse_layout_faulty(layout_str); |
741 | if (array.layout < 0) { | |
7236ee7a | 742 | int rv; |
19678e53 N |
743 | fprintf(stderr, Name ": %s: layout %s not understood for 'faulty' array\n", |
744 | devname, layout_str); | |
7236ee7a N |
745 | rv = 1; |
746 | break; | |
19678e53 | 747 | } |
e86c9dd6 NB |
748 | if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) { |
749 | fprintf(stderr, Name ": Cannot set layout for %s: %s\n", | |
750 | devname, strerror(errno)); | |
7236ee7a N |
751 | rv = 1; |
752 | } else if (!quiet) | |
e86c9dd6 | 753 | printf("layout for %s set to %d\n", devname, array.layout); |
7236ee7a | 754 | break; |
e86c9dd6 | 755 | |
7236ee7a | 756 | case 1: /* only raid_disks can each be changed. */ |
e86c9dd6 | 757 | |
19678e53 | 758 | if (chunksize || layout_str != NULL) { |
7236ee7a | 759 | fprintf(stderr, Name ": %s: Cannot change chunk size or layout for a RAID1 array.\n", |
e86c9dd6 | 760 | devname); |
7236ee7a N |
761 | rv = 1; |
762 | break; | |
e86c9dd6 | 763 | } |
9860f271 | 764 | if (raid_disks > 0) { |
e86c9dd6 | 765 | array.raid_disks = raid_disks; |
9860f271 NB |
766 | if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) { |
767 | fprintf(stderr, Name ": Cannot set raid-devices for %s: %s\n", | |
768 | devname, strerror(errno)); | |
7236ee7a | 769 | rv = 1; |
9860f271 | 770 | } |
e86c9dd6 | 771 | } |
7236ee7a | 772 | break; |
e86c9dd6 NB |
773 | |
774 | case 4: | |
775 | case 5: | |
776 | case 6: | |
1686dc25 | 777 | |
7236ee7a N |
778 | /* |
779 | * layout/chunksize/raid_disks can be changed | |
e86c9dd6 | 780 | * though the kernel may not support it all. |
e86c9dd6 | 781 | */ |
7236ee7a N |
782 | st = super_by_fd(fd); |
783 | ||
784 | /* | |
785 | * There are three possibilities. | |
786 | * 1/ The array will shrink. | |
787 | * We need to ensure the reshape will pause before reaching | |
788 | * the 'critical section'. We also need to fork and wait for | |
789 | * that to happen. When it does we | |
790 | * suspend/backup/complete/unfreeze | |
791 | * | |
792 | * 2/ The array will not change size. | |
793 | * This requires that we keep a backup of a sliding window | |
794 | * so that we can restore data after a crash. So we need | |
795 | * to fork and monitor progress. | |
796 | * | |
797 | * 3/ The array will grow. This is relatively easy. | |
e86c9dd6 NB |
798 | * However the kernel's restripe routines will cheerfully |
799 | * overwrite some early data before it is safe. So we | |
800 | * need to make a backup of the early parts of the array | |
801 | * and be ready to restore it if rebuild aborts very early. | |
802 | * | |
7236ee7a N |
803 | * We backup data by writing it to one spare, or to a |
804 | * file which was given on command line. | |
e86c9dd6 | 805 | * |
7236ee7a | 806 | * [FOLLOWING IS OLD AND PARTLY WRONG] |
e86c9dd6 NB |
807 | * So: we enumerate the devices in the array and |
808 | * make sure we can open all of them. | |
809 | * Then we freeze the early part of the array and | |
810 | * backup to the various spares. | |
811 | * Then we request changes and start the reshape. | |
812 | * Monitor progress until it has passed the danger zone. | |
813 | * and finally invalidate the copied data and unfreeze the | |
814 | * start of the array. | |
815 | * | |
7236ee7a N |
816 | * In each case, we first make sure that storage is available |
817 | * for the required backup. | |
818 | * Then we: | |
819 | * - request the shape change. | |
820 | * - for to handle backup etc. | |
e86c9dd6 | 821 | */ |
e86c9dd6 NB |
822 | nchunk = ochunk = array.chunk_size; |
823 | nlayout = olayout = array.layout; | |
824 | ndisks = odisks = array.raid_disks; | |
825 | ||
7236ee7a N |
826 | if (chunksize) { |
827 | nchunk = chunksize * 1024; | |
828 | if (array.size % chunksize) { | |
829 | fprintf(stderr, Name ": component size %dK is not" | |
830 | " a multiple of chunksize %dK\n", | |
831 | array.size, chunksize); | |
832 | break; | |
833 | } | |
834 | } | |
19678e53 | 835 | if (layout_str != NULL) |
7236ee7a | 836 | switch(array.level) { |
19678e53 N |
837 | case 4: /* ignore layout */ |
838 | break; | |
839 | case 5: | |
840 | nlayout = map_name(r5layout, layout_str); | |
841 | if (nlayout == UnSet) { | |
842 | fprintf(stderr, Name ": layout %s not understood for raid5.\n", | |
843 | layout_str); | |
844 | return 1; | |
845 | } | |
846 | break; | |
847 | ||
848 | case 6: | |
849 | nlayout = map_name(r6layout, layout_str); | |
850 | if (nlayout == UnSet) { | |
851 | fprintf(stderr, Name ": layout %s not understood for raid6.\n", | |
852 | layout_str); | |
853 | return 1; | |
854 | } | |
855 | break; | |
856 | } | |
e86c9dd6 NB |
857 | if (raid_disks) ndisks = raid_disks; |
858 | ||
859 | odata = odisks-1; | |
e86c9dd6 | 860 | ndata = ndisks-1; |
7236ee7a N |
861 | if (array.level == 6) { |
862 | odata--; /* number of data disks */ | |
863 | ndata--; | |
e86c9dd6 | 864 | } |
7236ee7a N |
865 | |
866 | /* Check that we can hold all the data */ | |
867 | size = ndata * array.size; | |
868 | get_dev_size(fd, NULL, &array_size); | |
869 | if (size < (array_size/1024)) { | |
870 | fprintf(stderr, Name ": this change will reduce the size of the array.\n" | |
871 | " use --grow --array-size first to truncate array.\n" | |
872 | " e.g. mdadm --grow %s --array-size %llu\n", | |
873 | devname, size); | |
874 | rv = 1; | |
875 | break; | |
e86c9dd6 | 876 | } |
7236ee7a N |
877 | |
878 | /* So how much do we need to backup. | |
879 | * We need an amount of data which is both a whole number of | |
880 | * old stripes and a whole number of new stripes. | |
881 | * So LCM for (chunksize*datadisks). | |
e86c9dd6 | 882 | */ |
7236ee7a N |
883 | a = ochunk/512 * odata; |
884 | b = nchunk/512 * ndata; | |
885 | /* Find GCD */ | |
886 | while (a != b) { | |
887 | if (a < b) | |
888 | b -= a; | |
889 | if (b < a) | |
890 | a -= b; | |
e86c9dd6 | 891 | } |
7236ee7a N |
892 | /* LCM == product / GCD */ |
893 | blocks = ochunk/512 * nchunk/512 * odata * ndata / a; | |
894 | ||
895 | if (ndata == odata) | |
896 | blocks *= 16; | |
897 | else | |
898 | fprintf(stderr, Name ": Need to backup %luK of critical " | |
899 | "section..\n", blocks/2); | |
e86c9dd6 | 900 | |
7236ee7a | 901 | sysfs_free(sra); |
e86c9dd6 | 902 | sra = sysfs_read(fd, 0, |
758d3a8e NB |
903 | GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE| |
904 | GET_CACHE); | |
e86c9dd6 NB |
905 | if (!sra) { |
906 | fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n", | |
907 | devname); | |
7236ee7a N |
908 | rv = 1; |
909 | break; | |
e86c9dd6 NB |
910 | } |
911 | ||
7236ee7a | 912 | if (blocks >= sra->component_size/2) { |
e86c9dd6 NB |
913 | fprintf(stderr, Name ": %s: Something wrong - reshape aborted\n", |
914 | devname); | |
7236ee7a N |
915 | rv = 1; |
916 | break; | |
353632d9 | 917 | } |
7e0f6979 | 918 | nrdisks = array.nr_disks + sra->array.spare_disks; |
e86c9dd6 NB |
919 | /* Now we need to open all these devices so we can read/write. |
920 | */ | |
06b0d786 NB |
921 | fdlist = malloc((1+nrdisks) * sizeof(int)); |
922 | offsets = malloc((1+nrdisks) * sizeof(offsets[0])); | |
e86c9dd6 NB |
923 | if (!fdlist || !offsets) { |
924 | fprintf(stderr, Name ": malloc failed: grow aborted\n"); | |
7236ee7a N |
925 | rv = 1; |
926 | break; | |
e86c9dd6 | 927 | } |
06b0d786 | 928 | for (d=0; d <= nrdisks; d++) |
e86c9dd6 NB |
929 | fdlist[d] = -1; |
930 | d = array.raid_disks; | |
931 | for (sd = sra->devs; sd; sd=sd->next) { | |
06c7f68e | 932 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) |
e86c9dd6 | 933 | continue; |
06c7f68e NB |
934 | if (sd->disk.state & (1<<MD_DISK_SYNC)) { |
935 | char *dn = map_dev(sd->disk.major, | |
936 | sd->disk.minor, 1); | |
937 | fdlist[sd->disk.raid_disk] | |
938 | = dev_open(dn, O_RDONLY); | |
7236ee7a | 939 | offsets[sd->disk.raid_disk] = sd->data_offset*512; |
06c7f68e | 940 | if (fdlist[sd->disk.raid_disk] < 0) { |
e86c9dd6 | 941 | fprintf(stderr, Name ": %s: cannot open component %s\n", |
e81cdd9f | 942 | devname, dn?dn:"-unknown-"); |
7236ee7a N |
943 | rv = 1; |
944 | goto release; | |
e86c9dd6 | 945 | } |
7236ee7a | 946 | } else if (backup_file == NULL) { |
e86c9dd6 | 947 | /* spare */ |
06c7f68e NB |
948 | char *dn = map_dev(sd->disk.major, |
949 | sd->disk.minor, 1); | |
16c6fa80 | 950 | fdlist[d] = dev_open(dn, O_RDWR); |
7236ee7a | 951 | offsets[d] = (sra->component_size - blocks - 8)*512; |
e86c9dd6 NB |
952 | if (fdlist[d]<0) { |
953 | fprintf(stderr, Name ": %s: cannot open component %s\n", | |
e81cdd9f | 954 | devname, dn?dn:"-unknown"); |
7236ee7a N |
955 | rv = 1; |
956 | goto release; | |
e86c9dd6 NB |
957 | } |
958 | d++; | |
959 | } | |
960 | } | |
7236ee7a N |
961 | if (backup_file == NULL) { |
962 | if (ndata <= odata) { | |
963 | fprintf(stderr, Name ": %s: Cannot grow - need backup-file\n", | |
964 | devname); | |
965 | rv = 1; | |
966 | break; | |
967 | } else if (sra->array.spare_disks == 0) { | |
968 | fprintf(stderr, Name ": %s: Cannot grow - need a spare or " | |
969 | "backup-file to backup critical section\n", | |
970 | devname); | |
971 | rv = 1; | |
972 | break; | |
973 | } | |
974 | if (d == array.raid_disks) { | |
975 | fprintf(stderr, Name ": %s: No spare device for backup\n", | |
976 | devname); | |
977 | rv = 1; | |
978 | break; | |
e86c9dd6 | 979 | } |
7236ee7a N |
980 | } else { |
981 | /* need to check backup file is large enough */ | |
982 | char buf[512]; | |
983 | fdlist[d] = open(backup_file, O_RDWR|O_CREAT|O_EXCL, | |
984 | S_IRUSR | S_IWUSR); | |
985 | offsets[d] = 8 * 512; | |
06b0d786 NB |
986 | if (fdlist[d] < 0) { |
987 | fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n", | |
988 | devname, backup_file, strerror(errno)); | |
7236ee7a N |
989 | rv = 1; |
990 | break; | |
991 | } | |
992 | memset(buf, 0, 512); | |
993 | for (i=0; i < blocks + 1 ; i++) { | |
994 | if (write(fdlist[d], buf, 512) != 512) { | |
995 | fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n", | |
996 | devname, backup_file, strerror(errno)); | |
997 | rv = 1; | |
998 | break; | |
999 | } | |
1000 | } | |
1001 | if (fsync(fdlist[d]) != 0) { | |
1002 | fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n", | |
1003 | devname, backup_file, strerror(errno)); | |
1004 | rv = 1; | |
1005 | break; | |
06b0d786 | 1006 | } |
06b0d786 | 1007 | d++; |
06b0d786 | 1008 | } |
7236ee7a N |
1009 | |
1010 | /* lastly, check that the internal stripe cache is | |
1011 | * large enough, or it won't work. | |
1012 | */ | |
1013 | ||
1014 | cache = (nchunk < ochunk) ? ochunk : nchunk; | |
1015 | cache = cache * 4 / 4096; | |
1016 | if (sra->cache_size < cache) | |
1017 | sysfs_set_num(sra, NULL, "stripe_cache_size", | |
1018 | cache+1); | |
1019 | /* Right, everything seems fine. Let's kick things off. | |
1020 | * If only changing raid_disks, use ioctl, else use | |
1021 | * sysfs. | |
1022 | */ | |
1023 | if (ochunk == nchunk && olayout == nlayout) { | |
1024 | array.raid_disks = ndisks; | |
1025 | if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) { | |
1026 | rv = 1; | |
1027 | fprintf(stderr, Name ": Cannot set device shape for %s: %s\n", | |
1028 | devname, strerror(errno)); | |
1029 | if (ndisks < odisks && | |
1030 | get_linux_version() < 2006030) | |
1031 | fprintf(stderr, Name ": linux 2.6.30 or later required\n"); | |
1032 | ||
1033 | break; | |
1034 | } | |
1035 | } else { | |
1036 | /* set them all just in case some old 'new_*' value | |
1037 | * persists from some earlier problem | |
1038 | */ | |
1039 | if (sysfs_set_num(sra, NULL, "chunk_size", nchunk) < 0) | |
1040 | rv = 1; | |
1041 | if (sysfs_set_num(sra, NULL, "layout", nlayout) < 0) | |
1042 | rv = 1; | |
1043 | if (sysfs_set_num(sra, NULL, "raid_disks", ndisks) < 0) | |
1044 | rv = 1; | |
1045 | if (rv) { | |
1046 | fprintf(stderr, Name ": Cannot set device shape for %s\n", | |
1047 | devname); | |
1048 | if (get_linux_version() < 2006030) | |
1049 | fprintf(stderr, Name ": linux 2.6.30 or later required\n"); | |
1050 | break; | |
1051 | } | |
e86c9dd6 NB |
1052 | } |
1053 | ||
7236ee7a N |
1054 | if (ndisks == 2 && odisks == 2) { |
1055 | /* No reshape is needed in this trivial case */ | |
1056 | rv = 0; | |
1057 | break; | |
1058 | } | |
1059 | ||
1060 | /* set up the backup-super-block. This requires the | |
1061 | * uuid from the array. | |
1062 | */ | |
e86c9dd6 | 1063 | /* Find a superblock */ |
7236ee7a N |
1064 | for (sd = sra->devs; sd; sd = sd->next) { |
1065 | char *dn; | |
1066 | int devfd; | |
1067 | int ok; | |
1068 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) | |
1069 | continue; | |
1070 | dn = map_dev(sd->disk.major, sd->disk.minor, 1); | |
1071 | devfd = dev_open(dn, O_RDONLY); | |
1072 | if (devfd < 0) | |
1073 | continue; | |
1074 | ok = st->ss->load_super(st, devfd, NULL); | |
1075 | close(devfd); | |
1076 | if (ok >= 0) | |
1077 | break; | |
1078 | } | |
1079 | if (!sd) { | |
e86c9dd6 NB |
1080 | fprintf(stderr, Name ": %s: Cannot find a superblock\n", |
1081 | devname); | |
7236ee7a N |
1082 | rv = 1; |
1083 | break; | |
e86c9dd6 NB |
1084 | } |
1085 | ||
7236ee7a | 1086 | memset(&bsb, 0, 512); |
2efedc7b | 1087 | memcpy(bsb.magic, "md_backup_data-1", 16); |
3da92f27 | 1088 | st->ss->uuid_from_super(st, (int*)&bsb.set_uuid); |
2efedc7b | 1089 | bsb.mtime = __cpu_to_le64(time(0)); |
7236ee7a N |
1090 | bsb.devstart2 = blocks; |
1091 | stripes = blocks / (ochunk/512) / odata; | |
1092 | /* Now we just need to kick off the reshape and watch, while | |
1093 | * handling backups of the data... | |
1094 | * This is all done by a forked background process. | |
2efedc7b | 1095 | */ |
7236ee7a N |
1096 | switch(fork()) { |
1097 | case 0: | |
1098 | close(fd); | |
1099 | if (check_env("MDADM_GROW_VERIFY")) | |
1100 | fd = open(devname, O_RDONLY | O_DIRECT); | |
1101 | else | |
1102 | fd = -1; | |
1103 | mlockall(MCL_FUTURE); | |
1104 | ||
1105 | if (odata < ndata) | |
1106 | done = child_grow(fd, sra, stripes, | |
1107 | fdlist, offsets, | |
1108 | odisks, ochunk, array.level, olayout, odata, | |
1109 | d - odisks, fdlist+odisks, offsets+odisks); | |
1110 | else if (odata > ndata) | |
1111 | done = child_shrink(fd, sra, stripes, | |
1112 | fdlist, offsets, | |
1113 | odisks, ochunk, array.level, olayout, odata, | |
1114 | d - odisks, fdlist+odisks, offsets+odisks); | |
1115 | else | |
1116 | done = child_same_size(fd, sra, stripes, | |
1117 | fdlist, offsets, | |
1118 | odisks, ochunk, array.level, olayout, odata, | |
1119 | d - odisks, fdlist+odisks, offsets+odisks); | |
1120 | if (backup_file && done) | |
1121 | unlink(backup_file); | |
1122 | if (level != UnSet && level != array.level) { | |
1123 | /* We need to wait for the reshape to finish | |
1124 | * (which will have happened unless odata < ndata) | |
1125 | * and then set the level | |
758d3a8e | 1126 | */ |
7236ee7a N |
1127 | |
1128 | c = map_num(pers, level); | |
1129 | if (c == NULL) | |
1130 | exit(0);/* not possible */ | |
1131 | ||
1132 | if (odata < ndata) | |
1133 | wait_reshape(sra); | |
1134 | err = sysfs_set_str(sra, NULL, "level", c); | |
1135 | if (err) | |
1136 | fprintf(stderr, Name ": %s: could not set level to %s\n", | |
1137 | devname, c); | |
758d3a8e | 1138 | } |
7236ee7a N |
1139 | exit(0); |
1140 | case -1: | |
1141 | fprintf(stderr, Name ": Cannot run child to monitor reshape: %s\n", | |
1142 | strerror(errno)); | |
1143 | rv = 1; | |
1144 | break; | |
1145 | default: | |
1146 | /* The child will take care of unfreezing the array */ | |
1147 | frozen = 0; | |
1148 | break; | |
e86c9dd6 | 1149 | } |
7236ee7a | 1150 | break; |
e86c9dd6 | 1151 | |
7236ee7a | 1152 | } |
e86c9dd6 | 1153 | |
7236ee7a N |
1154 | release: |
1155 | if (rv && orig_level != UnSet && sra) { | |
1156 | c = map_num(pers, orig_level); | |
1157 | if (c && sysfs_set_str(sra, NULL, "level", c) == 0) | |
1158 | fprintf(stderr, Name ": aborting level change\n"); | |
1159 | } | |
1160 | if (sra) | |
1161 | unfreeze_array(sra, frozen); | |
1162 | return rv; | |
1163 | } | |
e86c9dd6 | 1164 | |
7236ee7a N |
1165 | /* |
1166 | * We run a child process in the background which performs the following | |
1167 | * steps: | |
1168 | * - wait for resync to reach a certain point | |
1169 | * - suspend io to the following section | |
1170 | * - backup that section | |
1171 | * - allow resync to proceed further | |
1172 | * - resume io | |
1173 | * - discard the backup. | |
1174 | * | |
1175 | * When are combined in slightly different ways in the three cases. | |
1176 | * Grow: | |
1177 | * - suspend/backup/allow/wait/resume/discard | |
1178 | * Shrink: | |
1179 | * - allow/wait/suspend/backup/allow/wait/resume/discard | |
1180 | * same-size: | |
1181 | * - wait/resume/discard/suspend/backup/allow | |
1182 | * | |
1183 | * suspend/backup/allow always come together | |
1184 | * wait/resume/discard do too. | |
1185 | * For the same-size case we have two backups to improve flow. | |
1186 | * | |
1187 | */ | |
e86c9dd6 | 1188 | |
7236ee7a N |
1189 | int grow_backup(struct mdinfo *sra, |
1190 | unsigned long long offset, /* per device */ | |
1191 | unsigned long stripes, /* per device */ | |
1192 | int *sources, unsigned long long *offsets, | |
1193 | int disks, int chunk, int level, int layout, | |
1194 | int dests, int *destfd, unsigned long long *destoffsets, | |
1195 | int part, | |
1196 | char *buf) | |
1197 | { | |
1198 | /* Backup 'blocks' sectors at 'offset' on each device of the array, | |
1199 | * to storage 'destfd' (offset 'destoffsets'), after first | |
1200 | * suspending IO. Then allow resync to continue | |
1201 | * over the suspended section. | |
1202 | * Use part 'part' of the backup-super-block. | |
1203 | */ | |
1204 | int odata = disks; | |
1205 | int rv = 0; | |
1206 | int i; | |
1207 | //printf("offset %llu\n", offset); | |
1208 | if (level >= 4) | |
1209 | odata--; | |
1210 | if (level == 6) | |
1211 | odata--; | |
1212 | sysfs_set_num(sra, NULL, "suspend_hi", (offset + stripes * chunk/512) * odata); | |
1213 | if (part) { | |
1214 | bsb.arraystart2 = __cpu_to_le64(offset * odata); | |
1215 | bsb.length2 = __cpu_to_le64(stripes * chunk/512 * odata); | |
1216 | } else { | |
1217 | bsb.arraystart = __cpu_to_le64(offset * odata); | |
1218 | bsb.length = __cpu_to_le64(stripes * chunk/512 * odata); | |
1219 | } | |
1220 | if (part) | |
1221 | bsb.magic[15] = '2'; | |
1222 | for (i = 0; i < dests; i++) | |
1223 | if (part) | |
1224 | lseek64(destfd[i], destoffsets[i] + __le64_to_cpu(bsb.devstart2)*512, 0); | |
1225 | else | |
1226 | lseek64(destfd[i], destoffsets[i], 0); | |
1227 | ||
1228 | rv = save_stripes(sources, offsets, | |
1229 | disks, chunk, level, layout, | |
1230 | dests, destfd, | |
1231 | offset*512*odata, stripes * chunk * odata, | |
1232 | buf); | |
1233 | ||
1234 | if (rv) | |
1235 | return rv; | |
1236 | for (i = 0; i < dests; i++) { | |
1237 | bsb.devstart = __cpu_to_le64(destoffsets[i]/512); | |
1238 | ||
1239 | bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb)); | |
1240 | if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0) | |
1241 | bsb.sb_csum2 = bsb_csum((char*)&bsb, | |
1242 | ((char*)&bsb.sb_csum2)-((char*)&bsb)); | |
1243 | ||
1244 | lseek64(destfd[i], destoffsets[i] - 4096, 0); | |
1245 | write(destfd[i], &bsb, 512); | |
1246 | fsync(destfd[i]); | |
1247 | } | |
2efedc7b | 1248 | |
7236ee7a N |
1249 | return 0; |
1250 | } | |
1251 | ||
1252 | /* in 2.6.30, the value reported by sync_completed can be | |
1253 | * less that it should be by one stripe. | |
1254 | * This only happens when reshape hits sync_max and pauses. | |
1255 | * So allow wait_backup to either extent sync_max further | |
1256 | * than strictly necessary, or return before the | |
1257 | * sync has got quite as far as we would really like. | |
1258 | * This is what 'blocks2' is for. | |
1259 | * The various caller give appropriate values so that | |
1260 | * every works. | |
1261 | */ | |
1262 | int wait_backup(struct mdinfo *sra, | |
1263 | unsigned long long offset, /* per device */ | |
1264 | unsigned long long blocks, /* per device */ | |
1265 | unsigned long long blocks2, /* per device - hack */ | |
1266 | int dests, int *destfd, unsigned long long *destoffsets, | |
1267 | int part) | |
1268 | { | |
1269 | /* Wait for resync to pass the section that was backed up | |
1270 | * then erase the backup and allow IO | |
1271 | */ | |
1272 | int fd = sysfs_get_fd(sra, NULL, "sync_completed"); | |
1273 | unsigned long long completed; | |
1274 | int i; | |
1275 | ||
1276 | if (fd < 0) | |
1277 | return -1; | |
1278 | sysfs_set_num(sra, NULL, "sync_max", offset + blocks + blocks2); | |
1279 | if (offset == 0) | |
1280 | sysfs_set_str(sra, NULL, "sync_action", "reshape"); | |
1281 | do { | |
1282 | char action[20]; | |
1283 | fd_set rfds; | |
1284 | FD_ZERO(&rfds); | |
1285 | FD_SET(fd, &rfds); | |
1286 | select(fd+1, NULL, NULL, &rfds, NULL); | |
1287 | if (sysfs_fd_get_ll(fd, &completed) < 0) { | |
1288 | close(fd); | |
1289 | return -1; | |
e86c9dd6 | 1290 | } |
7236ee7a N |
1291 | if (sysfs_get_str(sra, NULL, "sync_action", |
1292 | action, 20) > 0 && | |
1293 | strncmp(action, "reshape", 7) != 0) | |
1294 | break; | |
1295 | } while (completed < offset + blocks); | |
1296 | close(fd); | |
1297 | ||
1298 | if (part) { | |
1299 | bsb.arraystart2 = __cpu_to_le64(0); | |
1300 | bsb.length2 = __cpu_to_le64(0); | |
1301 | } else { | |
1302 | bsb.arraystart = __cpu_to_le64(0); | |
1303 | bsb.length = __cpu_to_le64(0); | |
1304 | } | |
1305 | for (i = 0; i < dests; i++) { | |
1306 | bsb.devstart = __cpu_to_le64(destoffsets[i]/512); | |
1307 | bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb)); | |
1308 | if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0) | |
1309 | bsb.sb_csum2 = bsb_csum((char*)&bsb, | |
1310 | ((char*)&bsb.sb_csum2)-((char*)&bsb)); | |
1311 | lseek64(destfd[i], destoffsets[i]-4096, 0); | |
1312 | write(destfd[i], &bsb, 512); | |
1313 | fsync(destfd[i]); | |
1314 | } | |
1315 | return 0; | |
1316 | } | |
e86c9dd6 | 1317 | |
7236ee7a N |
1318 | static void fail(char *msg) |
1319 | { | |
1320 | write(2, msg, strlen(msg)); | |
1321 | write(2, "\n", 1); | |
1322 | exit(1); | |
1323 | } | |
1324 | ||
1325 | static char *abuf, *bbuf; | |
1326 | static int abuflen; | |
1327 | static void validate(int afd, int bfd, unsigned long long offset) | |
1328 | { | |
1329 | /* check that the data in the backup against the array. | |
1330 | * This is only used for regression testing and should not | |
1331 | * be used while the array is active | |
1332 | */ | |
1333 | struct mdp_backup_super bsb2; | |
1334 | if (afd < 0) | |
1335 | return; | |
1336 | lseek64(bfd, offset - 4096, 0); | |
1337 | if (read(bfd, &bsb2, 512) != 512) | |
1338 | fail("cannot read bsb"); | |
1339 | if (bsb2.sb_csum != bsb_csum((char*)&bsb2, | |
1340 | ((char*)&bsb2.sb_csum)-((char*)&bsb2))) | |
1341 | fail("first csum bad"); | |
1342 | if (memcmp(bsb2.magic, "md_backup_data", 14) != 0) | |
1343 | fail("magic is bad"); | |
1344 | if (memcmp(bsb2.magic, "md_backup_data-2", 16) == 0 && | |
1345 | bsb2.sb_csum2 != bsb_csum((char*)&bsb2, | |
1346 | ((char*)&bsb2.sb_csum2)-((char*)&bsb2))) | |
1347 | fail("second csum bad"); | |
1348 | ||
1349 | if (__le64_to_cpu(bsb2.devstart)*512 != offset) | |
1350 | fail("devstart is wrong"); | |
1351 | ||
1352 | if (bsb2.length) { | |
1353 | unsigned long long len = __le64_to_cpu(bsb2.length)*512; | |
1354 | ||
1355 | if (abuflen < len) { | |
1356 | free(abuf); | |
1357 | free(bbuf); | |
1358 | abuflen = len; | |
1359 | posix_memalign((void**)&abuf, 4096, abuflen); | |
1360 | posix_memalign((void**)&bbuf, 4096, abuflen); | |
e86c9dd6 | 1361 | } |
48924014 | 1362 | |
7236ee7a N |
1363 | lseek64(bfd, offset, 0); |
1364 | if (read(bfd, bbuf, len) != len) { | |
1365 | printf("len %llu\n", len); | |
1366 | fail("read first backup failed"); | |
1367 | } | |
1368 | lseek64(afd, __le64_to_cpu(bsb2.arraystart)*512, 0); | |
1369 | if (read(afd, abuf, len) != len) | |
1370 | fail("read first from array failed"); | |
1371 | if (memcmp(bbuf, abuf, len) != 0) { | |
1372 | int i; | |
1373 | printf("offset=%llu len=%llu\n", | |
1374 | __le64_to_cpu(bsb2.arraystart)*512, len); | |
1375 | for (i=0; i<len; i++) | |
1376 | if (bbuf[i] != abuf[i]) { | |
1377 | printf("first diff byte %d\n", i); | |
93ecfa01 | 1378 | break; |
7236ee7a N |
1379 | } |
1380 | fail("data1 compare failed"); | |
e86c9dd6 | 1381 | } |
7236ee7a N |
1382 | } |
1383 | if (bsb2.length2) { | |
1384 | unsigned long long len = __le64_to_cpu(bsb2.length2)*512; | |
1385 | ||
1386 | if (abuflen < len) { | |
1387 | free(abuf); | |
1388 | free(bbuf); | |
1389 | abuflen = len; | |
1390 | abuf = malloc(abuflen); | |
1391 | bbuf = malloc(abuflen); | |
e86c9dd6 NB |
1392 | } |
1393 | ||
7236ee7a N |
1394 | lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0); |
1395 | if (read(bfd, bbuf, len) != len) | |
1396 | fail("read second backup failed"); | |
1397 | lseek64(afd, __le64_to_cpu(bsb2.arraystart2)*512, 0); | |
1398 | if (read(afd, abuf, len) != len) | |
1399 | fail("read second from array failed"); | |
1400 | if (memcmp(bbuf, abuf, len) != 0) | |
1401 | fail("data2 compare failed"); | |
e86c9dd6 | 1402 | } |
7236ee7a | 1403 | } |
e86c9dd6 | 1404 | |
7236ee7a N |
1405 | static int child_grow(int afd, struct mdinfo *sra, unsigned long stripes, |
1406 | int *fds, unsigned long long *offsets, | |
1407 | int disks, int chunk, int level, int layout, int data, | |
1408 | int dests, int *destfd, unsigned long long *destoffsets) | |
1409 | { | |
1410 | char *buf; | |
e86c9dd6 | 1411 | |
7236ee7a N |
1412 | posix_memalign((void**)&buf, 4096, disks * chunk); |
1413 | sysfs_set_num(sra, NULL, "suspend_hi", 0); | |
1414 | sysfs_set_num(sra, NULL, "suspend_lo", 0); | |
1415 | grow_backup(sra, 0, stripes, | |
1416 | fds, offsets, disks, chunk, level, layout, | |
1417 | dests, destfd, destoffsets, | |
1418 | 0, buf); | |
1419 | validate(afd, destfd[0], destoffsets[0]); | |
1420 | if (wait_backup(sra, 0, stripes * chunk / 512, stripes * chunk / 512, | |
1421 | dests, destfd, destoffsets, | |
1422 | 0) < 0) | |
1423 | return 0; | |
1424 | sysfs_set_num(sra, NULL, "suspend_lo", (stripes * chunk/512) * data); | |
1425 | free(buf); | |
1426 | /* FIXME this should probably be numeric */ | |
1427 | sysfs_set_str(sra, NULL, "sync_max", "max"); | |
e86c9dd6 | 1428 | return 1; |
7236ee7a | 1429 | } |
e86c9dd6 | 1430 | |
7236ee7a N |
1431 | static int child_shrink(int afd, struct mdinfo *sra, unsigned long stripes, |
1432 | int *fds, unsigned long long *offsets, | |
1433 | int disks, int chunk, int level, int layout, int data, | |
1434 | int dests, int *destfd, unsigned long long *destoffsets) | |
1435 | { | |
1436 | char *buf; | |
1437 | unsigned long long start; | |
1438 | int rv; | |
1439 | ||
1440 | posix_memalign((void**)&buf, 4096, disks * chunk); | |
1441 | start = sra->component_size - stripes * chunk/512; | |
1442 | sysfs_set_num(sra, NULL, "sync_max", start); | |
1443 | sysfs_set_str(sra, NULL, "sync_action", "reshape"); | |
1444 | sysfs_set_num(sra, NULL, "suspend_lo", 0); | |
1445 | sysfs_set_num(sra, NULL, "suspend_hi", 0); | |
1446 | rv = wait_backup(sra, 0, start - stripes * chunk/512, stripes * chunk/512, | |
1447 | dests, destfd, destoffsets, 0); | |
1448 | if (rv < 0) | |
1449 | return 0; | |
1450 | grow_backup(sra, 0, stripes, | |
1451 | fds, offsets, | |
1452 | disks, chunk, level, layout, | |
1453 | dests, destfd, destoffsets, | |
1454 | 0, buf); | |
1455 | validate(afd, destfd[0], destoffsets[0]); | |
1456 | rv = wait_backup(sra, start, stripes*chunk/512, 0, | |
1457 | dests, destfd, destoffsets, 0); | |
1458 | if (rv < 0) | |
1459 | return 0; | |
1460 | sysfs_set_num(sra, NULL, "suspend_lo", (stripes * chunk/512) * data); | |
1461 | free(buf); | |
1462 | /* FIXME this should probably be numeric */ | |
1463 | sysfs_set_str(sra, NULL, "sync_max", "max"); | |
1464 | return 1; | |
1465 | } | |
1466 | ||
1467 | static int child_same_size(int afd, struct mdinfo *sra, unsigned long stripes, | |
1468 | int *fds, unsigned long long *offsets, | |
1469 | int disks, int chunk, int level, int layout, int data, | |
1470 | int dests, int *destfd, unsigned long long *destoffsets) | |
1471 | { | |
1472 | unsigned long long start, size; | |
1473 | unsigned long tailstripes = stripes; | |
1474 | int part; | |
1475 | char *buf; | |
1476 | unsigned long long speed; | |
1477 | ||
1478 | ||
1479 | posix_memalign((void**)&buf, 4096, disks * chunk); | |
1480 | ||
1481 | sysfs_set_num(sra, NULL, "suspend_lo", 0); | |
1482 | sysfs_set_num(sra, NULL, "suspend_hi", 0); | |
1483 | ||
1484 | sysfs_get_ll(sra, NULL, "sync_speed_min", &speed); | |
1485 | sysfs_set_num(sra, NULL, "sync_speed_min", 200000); | |
1486 | ||
1487 | grow_backup(sra, 0, stripes, | |
1488 | fds, offsets, | |
1489 | disks, chunk, level, layout, | |
1490 | dests, destfd, destoffsets, | |
1491 | 0, buf); | |
1492 | grow_backup(sra, stripes * chunk/512, stripes, | |
1493 | fds, offsets, | |
1494 | disks, chunk, level, layout, | |
1495 | dests, destfd, destoffsets, | |
1496 | 1, buf); | |
1497 | validate(afd, destfd[0], destoffsets[0]); | |
1498 | part = 0; | |
1499 | start = stripes * 2; /* where to read next */ | |
1500 | size = sra->component_size / (chunk/512); | |
1501 | while (start < size) { | |
1502 | if (wait_backup(sra, (start-stripes*2)*chunk/512, | |
1503 | stripes*chunk/512, 0, | |
1504 | dests, destfd, destoffsets, | |
1505 | part) < 0) | |
1506 | return 0; | |
1507 | sysfs_set_num(sra, NULL, "suspend_lo", start*chunk/512 * data); | |
1508 | if (start + stripes > size) | |
1509 | tailstripes = (size - start); | |
1510 | ||
1511 | grow_backup(sra, start*chunk/512, tailstripes, | |
1512 | fds, offsets, | |
1513 | disks, chunk, level, layout, | |
1514 | dests, destfd, destoffsets, | |
1515 | part, buf); | |
1516 | start += stripes; | |
1517 | part = 1 - part; | |
1518 | validate(afd, destfd[0], destoffsets[0]); | |
1519 | } | |
1520 | if (wait_backup(sra, (start-stripes*2) * chunk/512, stripes * chunk/512, 0, | |
1521 | dests, destfd, destoffsets, | |
1522 | part) < 0) | |
1523 | return 0; | |
1524 | sysfs_set_num(sra, NULL, "suspend_lo", ((start-stripes)*chunk/512) * data); | |
1525 | if (wait_backup(sra, (start-stripes) * chunk/512, tailstripes * chunk/512, 0, | |
1526 | dests, destfd, destoffsets, | |
1527 | 1-part) < 0) | |
1528 | return 0; | |
1529 | sysfs_set_num(sra, NULL, "suspend_lo", (size*chunk/512) * data); | |
1530 | sysfs_set_num(sra, NULL, "sync_speed_min", speed); | |
1531 | free(buf); | |
1532 | return 1; | |
e86c9dd6 | 1533 | } |
353632d9 NB |
1534 | |
1535 | /* | |
1536 | * If any spare contains md_back_data-1 which is recent wrt mtime, | |
1537 | * write that data into the array and update the super blocks with | |
1538 | * the new reshape_progress | |
1539 | */ | |
06b0d786 | 1540 | int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt, char *backup_file) |
353632d9 NB |
1541 | { |
1542 | int i, j; | |
1543 | int old_disks; | |
353632d9 | 1544 | unsigned long long *offsets; |
6e9eac4f NB |
1545 | unsigned long long nstripe, ostripe, last_block; |
1546 | int ndata, odata; | |
353632d9 NB |
1547 | |
1548 | if (info->delta_disks < 0) | |
1549 | return 1; /* cannot handle a shrink */ | |
1550 | if (info->new_level != info->array.level || | |
1551 | info->new_layout != info->array.layout || | |
1552 | info->new_chunk != info->array.chunk_size) | |
1553 | return 1; /* Can only handle change in disks */ | |
1554 | ||
1555 | old_disks = info->array.raid_disks - info->delta_disks; | |
1556 | ||
06b0d786 | 1557 | for (i=old_disks-(backup_file?1:0); i<cnt; i++) { |
353632d9 | 1558 | struct mdinfo dinfo; |
2efedc7b | 1559 | char buf[4096]; |
06b0d786 | 1560 | int fd; |
353632d9 NB |
1561 | |
1562 | /* This was a spare and may have some saved data on it. | |
1563 | * Load the superblock, find and load the | |
1564 | * backup_super_block. | |
1565 | * If either fail, go on to next device. | |
1566 | * If the backup contains no new info, just return | |
206c5eae | 1567 | * else restore data and update all superblocks |
353632d9 | 1568 | */ |
06b0d786 NB |
1569 | if (i == old_disks-1) { |
1570 | fd = open(backup_file, O_RDONLY); | |
1571 | if (fd<0) | |
1572 | continue; | |
06b0d786 NB |
1573 | } else { |
1574 | fd = fdlist[i]; | |
1575 | if (fd < 0) | |
1576 | continue; | |
3da92f27 | 1577 | if (st->ss->load_super(st, fd, NULL)) |
06b0d786 | 1578 | continue; |
353632d9 | 1579 | |
3da92f27 NB |
1580 | st->ss->getinfo_super(st, &dinfo); |
1581 | st->ss->free_super(st); | |
1582 | ||
06b0d786 NB |
1583 | if (lseek64(fd, |
1584 | (dinfo.data_offset + dinfo.component_size - 8) <<9, | |
1585 | 0) < 0) | |
1586 | continue; /* Cannot seek */ | |
1587 | } | |
1588 | if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb)) | |
353632d9 NB |
1589 | continue; /* Cannot read */ |
1590 | if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0) | |
1591 | continue; | |
1592 | if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb))) | |
1593 | continue; /* bad checksum */ | |
1594 | if (memcmp(bsb.set_uuid,info->uuid, 16) != 0) | |
1595 | continue; /* Wrong uuid */ | |
1596 | ||
1597 | if (info->array.utime > __le64_to_cpu(bsb.mtime) + 3600 || | |
1598 | info->array.utime < __le64_to_cpu(bsb.mtime)) | |
1599 | continue; /* time stamp is too bad */ | |
1600 | ||
1601 | if (__le64_to_cpu(bsb.arraystart) != 0) | |
1602 | continue; /* Can only handle backup from start of array */ | |
1603 | if (__le64_to_cpu(bsb.length) < | |
1604 | info->reshape_progress) | |
1605 | continue; /* No new data here */ | |
1606 | ||
06b0d786 | 1607 | if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0) |
353632d9 | 1608 | continue; /* Cannot seek */ |
2efedc7b | 1609 | /* There should be a duplicate backup superblock 4k before here */ |
06b0d786 NB |
1610 | if (lseek64(fd, -4096, 1) < 0 || |
1611 | read(fd, buf, 4096) != 4096 || | |
9860f271 | 1612 | memcmp(buf, &bsb, sizeof(bsb)) != 0) |
2efedc7b NB |
1613 | continue; /* Cannot find leading superblock */ |
1614 | ||
353632d9 NB |
1615 | /* Now need the data offsets for all devices. */ |
1616 | offsets = malloc(sizeof(*offsets)*info->array.raid_disks); | |
1617 | for(j=0; j<info->array.raid_disks; j++) { | |
1618 | if (fdlist[j] < 0) | |
1619 | continue; | |
3da92f27 | 1620 | if (st->ss->load_super(st, fdlist[j], NULL)) |
353632d9 NB |
1621 | /* FIXME should be this be an error */ |
1622 | continue; | |
3da92f27 NB |
1623 | st->ss->getinfo_super(st, &dinfo); |
1624 | st->ss->free_super(st); | |
353632d9 NB |
1625 | offsets[j] = dinfo.data_offset; |
1626 | } | |
1627 | printf(Name ": restoring critical section\n"); | |
1628 | ||
1629 | if (restore_stripes(fdlist, offsets, | |
1630 | info->array.raid_disks, | |
1631 | info->new_chunk, | |
1632 | info->new_level, | |
1633 | info->new_layout, | |
06b0d786 | 1634 | fd, __le64_to_cpu(bsb.devstart)*512, |
353632d9 NB |
1635 | 0, __le64_to_cpu(bsb.length)*512)) { |
1636 | /* didn't succeed, so giveup */ | |
2295250a | 1637 | return 1; |
353632d9 NB |
1638 | } |
1639 | ||
1640 | /* Ok, so the data is restored. Let's update those superblocks. */ | |
1641 | ||
1642 | for (j=0; j<info->array.raid_disks; j++) { | |
1643 | if (fdlist[j] < 0) continue; | |
3da92f27 | 1644 | if (st->ss->load_super(st, fdlist[j], NULL)) |
353632d9 | 1645 | continue; |
3da92f27 | 1646 | st->ss->getinfo_super(st, &dinfo); |
353632d9 | 1647 | dinfo.reshape_progress = __le64_to_cpu(bsb.length); |
3da92f27 | 1648 | st->ss->update_super(st, &dinfo, |
68c7d6d7 NB |
1649 | "_reshape_progress", |
1650 | NULL,0, 0, NULL); | |
3da92f27 NB |
1651 | st->ss->store_super(st, fdlist[j]); |
1652 | st->ss->free_super(st); | |
353632d9 NB |
1653 | } |
1654 | ||
1655 | /* And we are done! */ | |
1656 | return 0; | |
1657 | } | |
6e9eac4f NB |
1658 | /* Didn't find any backup data, try to see if any |
1659 | * was needed. | |
1660 | */ | |
1661 | nstripe = ostripe = 0; | |
1662 | odata = info->array.raid_disks - info->delta_disks - 1; | |
1663 | if (info->array.level == 6) odata--; /* number of data disks */ | |
1664 | ndata = info->array.raid_disks - 1; | |
1665 | if (info->new_level == 6) ndata--; | |
1666 | last_block = 0; | |
1667 | while (nstripe >= ostripe) { | |
1668 | nstripe += info->new_chunk / 512; | |
1669 | last_block = nstripe * ndata; | |
1670 | ostripe = last_block / odata / (info->array.chunk_size/512) * | |
1671 | (info->array.chunk_size/512); | |
1672 | } | |
1673 | ||
1674 | if (info->reshape_progress >= last_block) | |
1675 | return 0; | |
1676 | /* needed to recover critical section! */ | |
2295250a | 1677 | return 1; |
353632d9 | 1678 | } |