]>
Commit | Line | Data |
---|---|---|
e5329c37 NB |
1 | /* |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
e736b623 | 4 | * Copyright (C) 2001-2009 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 | |
e736b623 | 22 | * Email: <neilb@suse.de> |
e5329c37 NB |
23 | */ |
24 | #include "mdadm.h" | |
25 | #include "dlink.h" | |
7236ee7a | 26 | #include <sys/mman.h> |
e5329c37 NB |
27 | |
28 | #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN) | |
29 | #error no endian defined | |
30 | #endif | |
31 | #include "md_u.h" | |
32 | #include "md_p.h" | |
33 | ||
e9e43ec3 N |
34 | #ifndef offsetof |
35 | #define offsetof(t,f) ((size_t)&(((t*)0)->f)) | |
36 | #endif | |
37 | ||
e5329c37 NB |
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 | */ | |
4b1ac34b NB |
48 | struct mdinfo info; |
49 | ||
e5329c37 NB |
50 | struct stat stb; |
51 | int nfd, fd2; | |
52 | int d, nd; | |
82d9eba6 | 53 | struct supertype *st = NULL; |
4725bc31 | 54 | char *subarray = NULL; |
e5329c37 | 55 | |
4b1ac34b | 56 | if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) { |
e5329c37 NB |
57 | fprintf(stderr, Name ": cannot get array info for %s\n", devname); |
58 | return 1; | |
59 | } | |
60 | ||
4725bc31 N |
61 | if (info.array.level != -1) { |
62 | fprintf(stderr, Name ": can only add devices to linear arrays\n"); | |
63 | return 1; | |
64 | } | |
65 | ||
66 | st = super_by_fd(fd, &subarray); | |
82d9eba6 | 67 | if (!st) { |
f9ce90ba NB |
68 | fprintf(stderr, Name ": cannot handle arrays with superblock version %d\n", info.array.major_version); |
69 | return 1; | |
70 | } | |
71 | ||
4725bc31 N |
72 | if (subarray) { |
73 | fprintf(stderr, Name ": Cannot grow linear sub-arrays yet\n"); | |
74 | free(subarray); | |
75 | free(st); | |
e5329c37 NB |
76 | } |
77 | ||
6416d527 | 78 | nfd = open(newdev, O_RDWR|O_EXCL|O_DIRECT); |
e5329c37 NB |
79 | if (nfd < 0) { |
80 | fprintf(stderr, Name ": cannot open %s\n", newdev); | |
4725bc31 | 81 | free(st); |
e5329c37 NB |
82 | return 1; |
83 | } | |
84 | fstat(nfd, &stb); | |
85 | if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
86 | fprintf(stderr, Name ": %s is not a block device!\n", newdev); | |
87 | close(nfd); | |
4725bc31 | 88 | free(st); |
e5329c37 NB |
89 | return 1; |
90 | } | |
91 | /* now check out all the devices and make sure we can read the superblock */ | |
4b1ac34b | 92 | for (d=0 ; d < info.array.raid_disks ; d++) { |
e5329c37 NB |
93 | mdu_disk_info_t disk; |
94 | char *dv; | |
95 | ||
4725bc31 N |
96 | st->ss->free_super(st); |
97 | ||
e5329c37 NB |
98 | disk.number = d; |
99 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) { | |
100 | fprintf(stderr, Name ": cannot get device detail for device %d\n", | |
101 | d); | |
4725bc31 N |
102 | close(nfd); |
103 | free(st); | |
e5329c37 NB |
104 | return 1; |
105 | } | |
16c6fa80 | 106 | dv = map_dev(disk.major, disk.minor, 1); |
e5329c37 NB |
107 | if (!dv) { |
108 | fprintf(stderr, Name ": cannot find device file for device %d\n", | |
109 | d); | |
4725bc31 N |
110 | close(nfd); |
111 | free(st); | |
e5329c37 NB |
112 | return 1; |
113 | } | |
16c6fa80 | 114 | fd2 = dev_open(dv, O_RDWR); |
e5329c37 NB |
115 | if (!fd2) { |
116 | fprintf(stderr, Name ": cannot open device file %s\n", dv); | |
4725bc31 N |
117 | close(nfd); |
118 | free(st); | |
e5329c37 NB |
119 | return 1; |
120 | } | |
3da92f27 NB |
121 | |
122 | if (st->ss->load_super(st, fd2, NULL)) { | |
e5329c37 | 123 | fprintf(stderr, Name ": cannot find super block on %s\n", dv); |
4725bc31 | 124 | close(nfd); |
e5329c37 | 125 | close(fd2); |
4725bc31 | 126 | free(st); |
e5329c37 NB |
127 | return 1; |
128 | } | |
129 | close(fd2); | |
130 | } | |
131 | /* Ok, looks good. Lets update the superblock and write it out to | |
132 | * newdev. | |
133 | */ | |
aba69144 | 134 | |
4b1ac34b NB |
135 | info.disk.number = d; |
136 | info.disk.major = major(stb.st_rdev); | |
137 | info.disk.minor = minor(stb.st_rdev); | |
138 | info.disk.raid_disk = d; | |
139 | info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE); | |
3da92f27 | 140 | st->ss->update_super(st, &info, "linear-grow-new", newdev, |
f752781f | 141 | 0, 0, NULL); |
e5329c37 | 142 | |
3da92f27 | 143 | if (st->ss->store_super(st, nfd)) { |
f752781f NB |
144 | fprintf(stderr, Name ": Cannot store new superblock on %s\n", |
145 | newdev); | |
e5329c37 NB |
146 | close(nfd); |
147 | return 1; | |
148 | } | |
e5329c37 | 149 | close(nfd); |
4b1ac34b NB |
150 | |
151 | if (ioctl(fd, ADD_NEW_DISK, &info.disk) != 0) { | |
e5329c37 NB |
152 | fprintf(stderr, Name ": Cannot add new disk to this array\n"); |
153 | return 1; | |
154 | } | |
155 | /* Well, that seems to have worked. | |
156 | * Now go through and update all superblocks | |
157 | */ | |
158 | ||
4b1ac34b | 159 | if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) { |
e5329c37 NB |
160 | fprintf(stderr, Name ": cannot get array info for %s\n", devname); |
161 | return 1; | |
162 | } | |
163 | ||
164 | nd = d; | |
4b1ac34b | 165 | for (d=0 ; d < info.array.raid_disks ; d++) { |
e5329c37 NB |
166 | mdu_disk_info_t disk; |
167 | char *dv; | |
168 | ||
169 | disk.number = d; | |
170 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) { | |
171 | fprintf(stderr, Name ": cannot get device detail for device %d\n", | |
172 | d); | |
173 | return 1; | |
174 | } | |
16c6fa80 | 175 | dv = map_dev(disk.major, disk.minor, 1); |
e5329c37 NB |
176 | if (!dv) { |
177 | fprintf(stderr, Name ": cannot find device file for device %d\n", | |
178 | d); | |
179 | return 1; | |
180 | } | |
16c6fa80 | 181 | fd2 = dev_open(dv, O_RDWR); |
e5329c37 NB |
182 | if (fd2 < 0) { |
183 | fprintf(stderr, Name ": cannot open device file %s\n", dv); | |
184 | return 1; | |
185 | } | |
3da92f27 | 186 | if (st->ss->load_super(st, fd2, NULL)) { |
e5329c37 NB |
187 | fprintf(stderr, Name ": cannot find super block on %s\n", dv); |
188 | close(fd); | |
189 | return 1; | |
190 | } | |
4b1ac34b NB |
191 | info.array.raid_disks = nd+1; |
192 | info.array.nr_disks = nd+1; | |
193 | info.array.active_disks = nd+1; | |
194 | info.array.working_disks = nd+1; | |
f752781f | 195 | |
3da92f27 | 196 | st->ss->update_super(st, &info, "linear-grow-update", dv, |
f752781f | 197 | 0, 0, NULL); |
aba69144 | 198 | |
3da92f27 | 199 | if (st->ss->store_super(st, fd2)) { |
e5329c37 NB |
200 | fprintf(stderr, Name ": Cannot store new superblock on %s\n", dv); |
201 | close(fd2); | |
202 | return 1; | |
203 | } | |
204 | close(fd2); | |
205 | } | |
206 | ||
207 | return 0; | |
208 | } | |
f5e166fe | 209 | |
8fac0577 | 210 | int Grow_addbitmap(char *devname, int fd, char *file, int chunk, int delay, int write_behind, int force) |
f5e166fe NB |
211 | { |
212 | /* | |
213 | * First check that array doesn't have a bitmap | |
214 | * Then create the bitmap | |
215 | * Then add it | |
216 | * | |
217 | * For internal bitmaps, we need to check the version, | |
218 | * find all the active devices, and write the bitmap block | |
219 | * to all devices | |
220 | */ | |
221 | mdu_bitmap_file_t bmf; | |
222 | mdu_array_info_t array; | |
223 | struct supertype *st; | |
4725bc31 | 224 | char *subarray = NULL; |
dcec9ee5 NB |
225 | int major = BITMAP_MAJOR_HI; |
226 | int vers = md_get_version(fd); | |
8fac0577 | 227 | unsigned long long bitmapsize, array_size; |
dcec9ee5 NB |
228 | |
229 | if (vers < 9003) { | |
230 | major = BITMAP_MAJOR_HOSTENDIAN; | |
231 | #ifdef __BIG_ENDIAN | |
232 | fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n" | |
233 | " between different architectured. Consider upgrading the Linux kernel.\n"); | |
234 | #endif | |
235 | } | |
f5e166fe NB |
236 | |
237 | if (ioctl(fd, GET_BITMAP_FILE, &bmf) != 0) { | |
353632d9 | 238 | if (errno == ENOMEM) |
f5e166fe NB |
239 | fprintf(stderr, Name ": Memory allocation failure.\n"); |
240 | else | |
241 | fprintf(stderr, Name ": bitmaps not supported by this kernel.\n"); | |
242 | return 1; | |
243 | } | |
244 | if (bmf.pathname[0]) { | |
fe80f49b NB |
245 | if (strcmp(file,"none")==0) { |
246 | if (ioctl(fd, SET_BITMAP_FILE, -1)!= 0) { | |
247 | fprintf(stderr, Name ": failed to remove bitmap %s\n", | |
248 | bmf.pathname); | |
249 | return 1; | |
250 | } | |
251 | return 0; | |
252 | } | |
f5e166fe NB |
253 | fprintf(stderr, Name ": %s already has a bitmap (%s)\n", |
254 | devname, bmf.pathname); | |
255 | return 1; | |
256 | } | |
257 | if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) { | |
258 | fprintf(stderr, Name ": cannot get array status for %s\n", devname); | |
259 | return 1; | |
260 | } | |
261 | if (array.state & (1<<MD_SB_BITMAP_PRESENT)) { | |
fe80f49b NB |
262 | if (strcmp(file, "none")==0) { |
263 | array.state &= ~(1<<MD_SB_BITMAP_PRESENT); | |
264 | if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) { | |
265 | fprintf(stderr, Name ": failed to remove internal bitmap.\n"); | |
266 | return 1; | |
267 | } | |
268 | return 0; | |
269 | } | |
f5e166fe NB |
270 | fprintf(stderr, Name ": Internal bitmap already present on %s\n", |
271 | devname); | |
272 | return 1; | |
273 | } | |
4725bc31 N |
274 | |
275 | if (strcmp(file, "none") == 0) { | |
276 | fprintf(stderr, Name ": no bitmap found on %s\n", devname); | |
277 | return 1; | |
278 | } | |
5b28bd56 NB |
279 | if (array.level <= 0) { |
280 | fprintf(stderr, Name ": Bitmaps not meaningful with level %s\n", | |
281 | map_num(pers, array.level)?:"of this array"); | |
282 | return 1; | |
283 | } | |
8fac0577 NB |
284 | bitmapsize = array.size; |
285 | bitmapsize <<= 1; | |
beae1dfe | 286 | if (get_dev_size(fd, NULL, &array_size) && |
8fac0577 NB |
287 | array_size > (0x7fffffffULL<<9)) { |
288 | /* Array is big enough that we cannot trust array.size | |
289 | * try other approaches | |
290 | */ | |
291 | bitmapsize = get_component_size(fd); | |
292 | } | |
8fac0577 NB |
293 | if (bitmapsize == 0) { |
294 | fprintf(stderr, Name ": Cannot reliably determine size of array to create bitmap - sorry.\n"); | |
295 | return 1; | |
296 | } | |
297 | ||
f9c25f1d | 298 | if (array.level == 10) { |
8686f3ed | 299 | int ncopies = (array.layout&255)*((array.layout>>8)&255); |
f9c25f1d NB |
300 | bitmapsize = bitmapsize * array.raid_disks / ncopies; |
301 | } | |
302 | ||
4725bc31 | 303 | st = super_by_fd(fd, &subarray); |
f5e166fe NB |
304 | if (!st) { |
305 | fprintf(stderr, Name ": Cannot understand version %d.%d\n", | |
306 | array.major_version, array.minor_version); | |
307 | return 1; | |
308 | } | |
4725bc31 N |
309 | if (subarray) { |
310 | fprintf(stderr, Name ": Cannot add bitmaps to sub-arrays yet\n"); | |
311 | free(subarray); | |
312 | free(st); | |
fe80f49b | 313 | return 1; |
4725bc31 N |
314 | } |
315 | if (strcmp(file, "internal") == 0) { | |
f5e166fe | 316 | int d; |
ebeb3663 N |
317 | if (st->ss->add_internal_bitmap == NULL) { |
318 | fprintf(stderr, Name ": Internal bitmaps not supported " | |
319 | "with %s metadata\n", st->ss->name); | |
320 | return 1; | |
321 | } | |
ea329559 | 322 | for (d=0; d< st->max_devs; d++) { |
f5e166fe NB |
323 | mdu_disk_info_t disk; |
324 | char *dv; | |
325 | disk.number = d; | |
326 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) | |
327 | continue; | |
328 | if (disk.major == 0 && | |
329 | disk.minor == 0) | |
330 | continue; | |
331 | if ((disk.state & (1<<MD_DISK_SYNC))==0) | |
332 | continue; | |
16c6fa80 | 333 | dv = map_dev(disk.major, disk.minor, 1); |
f5e166fe | 334 | if (dv) { |
16c6fa80 | 335 | int fd2 = dev_open(dv, O_RDWR); |
f5e166fe NB |
336 | if (fd2 < 0) |
337 | continue; | |
3da92f27 | 338 | if (st->ss->load_super(st, fd2, NULL)==0) { |
199171a2 | 339 | if (st->ss->add_internal_bitmap( |
3da92f27 | 340 | st, |
199171a2 NB |
341 | &chunk, delay, write_behind, |
342 | bitmapsize, 0, major) | |
343 | ) | |
3da92f27 | 344 | st->ss->write_bitmap(st, fd2); |
21e92547 NB |
345 | else { |
346 | fprintf(stderr, Name ": failed to create internal bitmap - chunksize problem.\n"); | |
347 | close(fd2); | |
348 | return 1; | |
349 | } | |
f5e166fe NB |
350 | } |
351 | close(fd2); | |
352 | } | |
353 | } | |
354 | array.state |= (1<<MD_SB_BITMAP_PRESENT); | |
355 | if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) { | |
356 | fprintf(stderr, Name ": failed to set internal bitmap.\n"); | |
357 | return 1; | |
358 | } | |
fe80f49b NB |
359 | } else { |
360 | int uuid[4]; | |
361 | int bitmap_fd; | |
362 | int d; | |
363 | int max_devs = st->max_devs; | |
fe80f49b NB |
364 | |
365 | /* try to load a superblock */ | |
366 | for (d=0; d<max_devs; d++) { | |
367 | mdu_disk_info_t disk; | |
368 | char *dv; | |
369 | int fd2; | |
370 | disk.number = d; | |
371 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) | |
372 | continue; | |
373 | if ((disk.major==0 && disk.minor==0) || | |
374 | (disk.state & (1<<MD_DISK_REMOVED))) | |
375 | continue; | |
16c6fa80 | 376 | dv = map_dev(disk.major, disk.minor, 1); |
fe80f49b | 377 | if (!dv) continue; |
16c6fa80 | 378 | fd2 = dev_open(dv, O_RDONLY); |
fe80f49b | 379 | if (fd2 >= 0 && |
3da92f27 | 380 | st->ss->load_super(st, fd2, NULL) == 0) { |
fe80f49b | 381 | close(fd2); |
3da92f27 | 382 | st->ss->uuid_from_super(st, uuid); |
fe80f49b NB |
383 | break; |
384 | } | |
385 | close(fd2); | |
386 | } | |
387 | if (d == max_devs) { | |
388 | fprintf(stderr, Name ": cannot find UUID for array!\n"); | |
389 | return 1; | |
390 | } | |
8fac0577 | 391 | if (CreateBitmap(file, force, (char*)uuid, chunk, |
f9c25f1d | 392 | delay, write_behind, bitmapsize, major)) { |
fe80f49b NB |
393 | return 1; |
394 | } | |
395 | bitmap_fd = open(file, O_RDWR); | |
396 | if (bitmap_fd < 0) { | |
8fac0577 | 397 | fprintf(stderr, Name ": weird: %s cannot be opened\n", |
fe80f49b NB |
398 | file); |
399 | return 1; | |
400 | } | |
401 | if (ioctl(fd, SET_BITMAP_FILE, bitmap_fd) < 0) { | |
402 | fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n", | |
403 | devname, strerror(errno)); | |
404 | return 1; | |
405 | } | |
406 | } | |
f5e166fe NB |
407 | |
408 | return 0; | |
409 | } | |
410 | ||
e86c9dd6 NB |
411 | |
412 | /* | |
413 | * When reshaping an array we might need to backup some data. | |
414 | * This is written to all spares with a 'super_block' describing it. | |
ff94fb86 | 415 | * The superblock goes 4K from the end of the used space on the |
e86c9dd6 NB |
416 | * device. |
417 | * It if written after the backup is complete. | |
418 | * It has the following structure. | |
419 | */ | |
420 | ||
5fdf37e3 | 421 | static struct mdp_backup_super { |
7236ee7a | 422 | char magic[16]; /* md_backup_data-1 or -2 */ |
e86c9dd6 NB |
423 | __u8 set_uuid[16]; |
424 | __u64 mtime; | |
425 | /* start/sizes in 512byte sectors */ | |
7236ee7a | 426 | __u64 devstart; /* address on backup device/file of data */ |
e86c9dd6 NB |
427 | __u64 arraystart; |
428 | __u64 length; | |
429 | __u32 sb_csum; /* csum of preceeding bytes. */ | |
7236ee7a N |
430 | __u32 pad1; |
431 | __u64 devstart2; /* offset in to data of second section */ | |
432 | __u64 arraystart2; | |
433 | __u64 length2; | |
434 | __u32 sb_csum2; /* csum of preceeding bytes. */ | |
435 | __u8 pad[512-68-32]; | |
5fdf37e3 | 436 | } __attribute__((aligned(512))) bsb, bsb2; |
e86c9dd6 | 437 | |
f21e18ca | 438 | __u32 bsb_csum(char *buf, int len) |
e86c9dd6 NB |
439 | { |
440 | int i; | |
441 | int csum = 0; | |
442 | for (i=0; i<len; i++) | |
443 | csum = (csum<<3) + buf[0]; | |
444 | return __cpu_to_le32(csum); | |
445 | } | |
446 | ||
7236ee7a N |
447 | static int child_grow(int afd, struct mdinfo *sra, unsigned long blocks, |
448 | int *fds, unsigned long long *offsets, | |
449 | int disks, int chunk, int level, int layout, int data, | |
450 | int dests, int *destfd, unsigned long long *destoffsets); | |
451 | static int child_shrink(int afd, struct mdinfo *sra, unsigned long blocks, | |
452 | int *fds, unsigned long long *offsets, | |
453 | int disks, int chunk, int level, int layout, int data, | |
454 | int dests, int *destfd, unsigned long long *destoffsets); | |
455 | static int child_same_size(int afd, struct mdinfo *sra, unsigned long blocks, | |
456 | int *fds, unsigned long long *offsets, | |
e9e43ec3 | 457 | unsigned long long start, |
7236ee7a N |
458 | int disks, int chunk, int level, int layout, int data, |
459 | int dests, int *destfd, unsigned long long *destoffsets); | |
460 | ||
461 | int freeze_array(struct mdinfo *sra) | |
462 | { | |
463 | /* Try to freeze resync on this array. | |
464 | * Return -1 if the array is busy, | |
465 | * return 0 if this kernel doesn't support 'frozen' | |
466 | * return 1 if it worked. | |
467 | */ | |
468 | char buf[20]; | |
469 | if (sysfs_get_str(sra, NULL, "sync_action", buf, 20) <= 0) | |
470 | return 0; | |
471 | if (strcmp(buf, "idle\n") != 0 && | |
472 | strcmp(buf, "frozen\n") != 0) | |
473 | return -1; | |
474 | if (sysfs_set_str(sra, NULL, "sync_action", "frozen") < 0) | |
475 | return 0; | |
476 | return 1; | |
477 | } | |
478 | ||
479 | void unfreeze_array(struct mdinfo *sra, int frozen) | |
480 | { | |
481 | /* If 'frozen' is 1, unfreeze the array */ | |
482 | if (frozen > 0) | |
483 | sysfs_set_str(sra, NULL, "sync_action", "idle"); | |
484 | } | |
485 | ||
486 | void wait_reshape(struct mdinfo *sra) | |
487 | { | |
488 | int fd = sysfs_get_fd(sra, NULL, "sync_action"); | |
489 | char action[20]; | |
490 | ||
491 | do { | |
492 | fd_set rfds; | |
493 | FD_ZERO(&rfds); | |
494 | FD_SET(fd, &rfds); | |
495 | select(fd+1, NULL, NULL, &rfds, NULL); | |
496 | ||
497 | if (sysfs_fd_get_str(fd, action, 20) < 0) { | |
498 | close(fd); | |
499 | return; | |
500 | } | |
501 | } while (strncmp(action, "reshape", 7) == 0); | |
502 | } | |
503 | ||
504 | ||
06b0d786 | 505 | int Grow_reshape(char *devname, int fd, int quiet, char *backup_file, |
e86c9dd6 | 506 | long long size, |
19678e53 | 507 | int level, char *layout_str, int chunksize, int raid_disks) |
e86c9dd6 NB |
508 | { |
509 | /* Make some changes in the shape of an array. | |
510 | * The kernel must support the change. | |
7236ee7a N |
511 | * |
512 | * There are three different changes. Each can trigger | |
513 | * a resync or recovery so we freeze that until we have | |
514 | * requested everything (if kernel supports freezing - 2.6.30). | |
515 | * The steps are: | |
516 | * - change size (i.e. component_size) | |
517 | * - change level | |
518 | * - change layout/chunksize/ndisks | |
519 | * | |
520 | * The last can require a reshape. It is different on different | |
521 | * levels so we need to check the level before actioning it. | |
522 | * Some times the level change needs to be requested after the | |
523 | * reshape (e.g. raid6->raid5, raid5->raid0) | |
524 | * | |
e86c9dd6 | 525 | */ |
7236ee7a | 526 | struct mdu_array_info_s array, orig; |
e86c9dd6 | 527 | char *c; |
7236ee7a | 528 | int rv = 0; |
e86c9dd6 | 529 | struct supertype *st; |
4725bc31 | 530 | char *subarray = NULL; |
e86c9dd6 | 531 | |
e86c9dd6 NB |
532 | int nchunk, ochunk; |
533 | int nlayout, olayout; | |
534 | int ndisks, odisks; | |
f21e18ca | 535 | unsigned int ndata, odata; |
7236ee7a N |
536 | int orig_level = UnSet; |
537 | char alt_layout[40]; | |
e86c9dd6 NB |
538 | int *fdlist; |
539 | unsigned long long *offsets; | |
7236ee7a | 540 | int d, i; |
e86c9dd6 NB |
541 | int nrdisks; |
542 | int err; | |
7236ee7a N |
543 | int frozen; |
544 | unsigned long a,b, blocks, stripes; | |
f21e18ca | 545 | unsigned long cache; |
7236ee7a N |
546 | unsigned long long array_size; |
547 | int changed = 0; | |
548 | int done; | |
e86c9dd6 | 549 | |
7e0f6979 | 550 | struct mdinfo *sra; |
06c7f68e | 551 | struct mdinfo *sd; |
e86c9dd6 NB |
552 | |
553 | if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) { | |
554 | fprintf(stderr, Name ": %s is not an active md array - aborting\n", | |
555 | devname); | |
556 | return 1; | |
557 | } | |
24d40069 | 558 | |
9ce510be N |
559 | if (size >= 0 && |
560 | (chunksize || level!= UnSet || layout_str || raid_disks)) { | |
561 | fprintf(stderr, Name ": cannot change component size at the same time " | |
562 | "as other changes.\n" | |
563 | " Change size first, then check data is intact before " | |
564 | "making other changes.\n"); | |
565 | return 1; | |
566 | } | |
567 | ||
24d40069 N |
568 | if (raid_disks && raid_disks < array.raid_disks && array.level > 1 && |
569 | get_linux_version() < 2006032 && | |
570 | !check_env("MDADM_FORCE_FEWER")) { | |
571 | fprintf(stderr, Name ": reducing the number of devices is not safe before Linux 2.6.32\n" | |
572 | " Please use a newer kernel\n"); | |
573 | return 1; | |
574 | } | |
7236ee7a | 575 | sra = sysfs_read(fd, 0, GET_LEVEL); |
b526e52d DW |
576 | if (sra) |
577 | frozen = freeze_array(sra); | |
578 | else { | |
579 | fprintf(stderr, Name ": failed to read sysfs parameters for %s\n", | |
580 | devname); | |
581 | return 1; | |
582 | } | |
7236ee7a N |
583 | if (frozen < 0) { |
584 | fprintf(stderr, Name ": %s is performing resync/recovery and cannot" | |
585 | " be reshaped\n", devname); | |
586 | return 1; | |
587 | } | |
588 | ||
589 | /* ========= set size =============== */ | |
590 | if (size >= 0 && (size == 0 || size != array.size)) { | |
591 | array.size = size; | |
592 | if (array.size != size) { | |
593 | /* got truncated to 32bit, write to | |
594 | * component_size instead | |
595 | */ | |
596 | if (sra) | |
597 | rv = sysfs_set_num(sra, NULL, | |
598 | "component_size", size); | |
599 | else | |
600 | rv = -1; | |
601 | } else | |
602 | rv = ioctl(fd, SET_ARRAY_INFO, &array); | |
603 | if (rv != 0) { | |
39bbb392 | 604 | int err = errno; |
7236ee7a | 605 | fprintf(stderr, Name ": Cannot set device size for %s: %s\n", |
39bbb392 N |
606 | devname, strerror(err)); |
607 | if (err == EBUSY && | |
608 | (array.state & (1<<MD_SB_BITMAP_PRESENT))) | |
609 | fprintf(stderr, " Bitmap must be removed before size can be changed\n"); | |
7236ee7a N |
610 | rv = 1; |
611 | goto release; | |
612 | } | |
613 | ioctl(fd, GET_ARRAY_INFO, &array); | |
be1cabbd | 614 | size = get_component_size(fd)/2; |
f98841b3 N |
615 | if (size == 0) |
616 | size = array.size; | |
7236ee7a | 617 | if (!quiet) |
f98841b3 N |
618 | fprintf(stderr, Name ": component size of %s has been set to %lluK\n", |
619 | devname, size); | |
7236ee7a | 620 | changed = 1; |
f98841b3 | 621 | } else { |
be1cabbd | 622 | size = get_component_size(fd)/2; |
f98841b3 N |
623 | if (size == 0) |
624 | size = array.size; | |
7236ee7a N |
625 | } |
626 | ||
627 | /* ======= set level =========== */ | |
628 | if (level != UnSet && level != array.level) { | |
629 | /* Trying to change the level. | |
630 | * We might need to change layout first and schedule a | |
631 | * level change for later. | |
632 | * Level changes that can happen immediately are: | |
633 | * 0->4,5,6 1->5 4->5,6 5->1,6 | |
634 | * Level changes that need a layout change first are: | |
635 | * 6->5,4,0 : need a -6 layout, or parity-last | |
636 | * 5->4,0 : need parity-last | |
637 | */ | |
638 | if ((array.level == 6 || array.level == 5) && | |
639 | (level == 5 || level == 4 || level == 0)) { | |
640 | /* Don't change level yet, but choose intermediate | |
641 | * layout | |
642 | */ | |
643 | if (level == 5) { | |
644 | if (layout_str == NULL) | |
645 | switch (array.layout) { | |
646 | case ALGORITHM_LEFT_ASYMMETRIC: | |
647 | case ALGORITHM_LEFT_ASYMMETRIC_6: | |
648 | case ALGORITHM_ROTATING_N_RESTART: | |
649 | layout_str = "left-asymmetric-6"; | |
650 | break; | |
651 | case ALGORITHM_LEFT_SYMMETRIC: | |
652 | case ALGORITHM_LEFT_SYMMETRIC_6: | |
653 | case ALGORITHM_ROTATING_N_CONTINUE: | |
654 | layout_str = "left-symmetric-6"; | |
655 | break; | |
656 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
657 | case ALGORITHM_RIGHT_ASYMMETRIC_6: | |
658 | case ALGORITHM_ROTATING_ZERO_RESTART: | |
659 | layout_str = "right-asymmetric-6"; | |
660 | break; | |
661 | case ALGORITHM_RIGHT_SYMMETRIC: | |
662 | case ALGORITHM_RIGHT_SYMMETRIC_6: | |
663 | layout_str = "right-symmetric-6"; | |
664 | break; | |
665 | case ALGORITHM_PARITY_0: | |
666 | case ALGORITHM_PARITY_0_6: | |
667 | layout_str = "parity-first-6"; | |
668 | break; | |
669 | case ALGORITHM_PARITY_N: | |
670 | layout_str = "parity-last"; | |
671 | break; | |
672 | default: | |
673 | fprintf(stderr, Name ": %s: cannot" | |
674 | "convert layout to RAID5 equivalent\n", | |
675 | devname); | |
676 | rv = 1; | |
677 | goto release; | |
678 | } | |
679 | else { | |
680 | int l = map_name(r5layout, layout_str); | |
681 | if (l == UnSet) { | |
682 | fprintf(stderr, Name ": %s: layout '%s' not recognised\n", | |
683 | devname, layout_str); | |
684 | rv = 1; | |
685 | goto release; | |
686 | } | |
687 | if (l != ALGORITHM_PARITY_N) { | |
688 | /* need the -6 version */ | |
689 | char *ls = map_num(r5layout, l); | |
690 | strcat(strcpy(alt_layout, ls), | |
691 | "-6"); | |
692 | layout_str = alt_layout; | |
693 | } | |
694 | } | |
695 | if (raid_disks) | |
24d40069 | 696 | /* The final raid6->raid5 conversion |
7236ee7a N |
697 | * will reduce the number of disks, |
698 | * so now we need to aim higher | |
699 | */ | |
700 | raid_disks++; | |
701 | } else | |
702 | layout_str = "parity-last"; | |
703 | } else { | |
704 | c = map_num(pers, level); | |
b5ea446a N |
705 | if (c == NULL) { |
706 | rv = 1;/* not possible */ | |
707 | goto release; | |
708 | } | |
7236ee7a N |
709 | err = sysfs_set_str(sra, NULL, "level", c); |
710 | if (err) { | |
39bbb392 | 711 | err = errno; |
7236ee7a N |
712 | fprintf(stderr, Name ": %s: could not set level to %s\n", |
713 | devname, c); | |
39bbb392 N |
714 | if (err == EBUSY && |
715 | (array.state & (1<<MD_SB_BITMAP_PRESENT))) | |
716 | fprintf(stderr, " Bitmap must be removed before level can be changed\n"); | |
7236ee7a N |
717 | rv = 1; |
718 | goto release; | |
719 | } | |
720 | orig = array; | |
721 | orig_level = orig.level; | |
722 | ioctl(fd, GET_ARRAY_INFO, &array); | |
723 | if (layout_str == NULL && | |
724 | orig.level == 5 && level == 6 && | |
725 | array.layout != orig.layout) | |
726 | layout_str = map_num(r5layout, orig.layout); | |
727 | if (!quiet) | |
728 | fprintf(stderr, Name " level of %s changed to %s\n", | |
729 | devname, c); | |
730 | changed = 1; | |
731 | } | |
732 | } | |
733 | ||
734 | /* ========= set shape (chunk_size / layout / ndisks) ============== */ | |
735 | /* Check if layout change is a no-op */ | |
736 | if (layout_str) switch(array.level) { | |
737 | case 5: | |
738 | if (array.layout == map_name(r5layout, layout_str)) | |
739 | layout_str = NULL; | |
740 | break; | |
741 | case 6: | |
742 | if (layout_str == NULL && | |
743 | ((chunksize && chunksize * 1024 != array.chunk_size) || | |
744 | (raid_disks && raid_disks != array.raid_disks)) && | |
745 | array.layout >= 16) { | |
746 | fprintf(stderr, Name | |
747 | ": %s has a non-standard layout. If you wish to preserve this\n" | |
748 | " during the reshape, please specify --layout=preserve\n" | |
749 | " If you want to change it, specify a layout or use --layout=normalise\n", | |
750 | devname); | |
751 | rv = 1; | |
752 | goto release; | |
753 | } | |
754 | if (strcmp(layout_str, "normalise") == 0 || | |
755 | strcmp(layout_str, "normalize") == 0) { | |
756 | char *hyphen; | |
757 | strcpy(alt_layout, map_num(r6layout, array.layout)); | |
758 | hyphen = strrchr(alt_layout, '-'); | |
759 | if (hyphen && strcmp(hyphen, "-6") == 0) { | |
760 | *hyphen = 0; | |
761 | layout_str = alt_layout; | |
762 | } | |
763 | } | |
764 | ||
765 | if (array.layout == map_name(r6layout, layout_str)) | |
766 | layout_str = NULL; | |
767 | if (layout_str && strcmp(layout_str, "preserve") == 0) | |
768 | layout_str = NULL; | |
769 | break; | |
770 | } | |
771 | if (layout_str == NULL | |
772 | && (chunksize == 0 || chunksize*1024 == array.chunk_size) | |
773 | && (raid_disks == 0 || raid_disks == array.raid_disks)) { | |
774 | rv = 0; | |
775 | if (level != UnSet && level != array.level) { | |
776 | /* Looks like this level change doesn't need | |
777 | * a reshape after all. | |
778 | */ | |
779 | c = map_num(pers, level); | |
780 | if (c) { | |
781 | rv = sysfs_set_str(sra, NULL, "level", c); | |
39bbb392 N |
782 | if (rv) { |
783 | int err = errno; | |
7236ee7a N |
784 | fprintf(stderr, Name ": %s: could not set level to %s\n", |
785 | devname, c); | |
39bbb392 N |
786 | if (err == EBUSY && |
787 | (array.state & (1<<MD_SB_BITMAP_PRESENT))) | |
788 | fprintf(stderr, " Bitmap must be removed before level can be changed\n"); | |
b7e734fc | 789 | rv = 1; |
39bbb392 | 790 | } |
7236ee7a N |
791 | } |
792 | } else if (!changed && !quiet) | |
793 | fprintf(stderr, Name ": %s: no change requested\n", | |
794 | devname); | |
795 | goto release; | |
796 | } | |
797 | ||
e86c9dd6 NB |
798 | c = map_num(pers, array.level); |
799 | if (c == NULL) c = "-unknown-"; | |
800 | switch(array.level) { | |
801 | default: /* raid0, linear, multipath cannot be reconfigured */ | |
802 | fprintf(stderr, Name ": %s array %s cannot be reshaped.\n", | |
803 | c, devname); | |
7236ee7a N |
804 | rv = 1; |
805 | break; | |
e86c9dd6 NB |
806 | |
807 | case LEVEL_FAULTY: /* only 'layout' change is permitted */ | |
808 | ||
e86c9dd6 NB |
809 | if (chunksize || raid_disks) { |
810 | fprintf(stderr, Name ": %s: Cannot change chunksize or disks of a 'faulty' array\n", | |
811 | devname); | |
7236ee7a N |
812 | rv = 1; |
813 | break; | |
e86c9dd6 | 814 | } |
19678e53 | 815 | if (layout_str == NULL) |
7236ee7a | 816 | break; /* nothing to do.... */ |
e86c9dd6 | 817 | |
19678e53 N |
818 | array.layout = parse_layout_faulty(layout_str); |
819 | if (array.layout < 0) { | |
820 | fprintf(stderr, Name ": %s: layout %s not understood for 'faulty' array\n", | |
821 | devname, layout_str); | |
7236ee7a N |
822 | rv = 1; |
823 | break; | |
19678e53 | 824 | } |
e86c9dd6 NB |
825 | if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) { |
826 | fprintf(stderr, Name ": Cannot set layout for %s: %s\n", | |
827 | devname, strerror(errno)); | |
7236ee7a N |
828 | rv = 1; |
829 | } else if (!quiet) | |
e86c9dd6 | 830 | printf("layout for %s set to %d\n", devname, array.layout); |
7236ee7a | 831 | break; |
e86c9dd6 | 832 | |
7236ee7a | 833 | case 1: /* only raid_disks can each be changed. */ |
e86c9dd6 | 834 | |
19678e53 | 835 | if (chunksize || layout_str != NULL) { |
7236ee7a | 836 | fprintf(stderr, Name ": %s: Cannot change chunk size or layout for a RAID1 array.\n", |
e86c9dd6 | 837 | devname); |
7236ee7a N |
838 | rv = 1; |
839 | break; | |
e86c9dd6 | 840 | } |
9860f271 | 841 | if (raid_disks > 0) { |
e86c9dd6 | 842 | array.raid_disks = raid_disks; |
9860f271 NB |
843 | if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) { |
844 | fprintf(stderr, Name ": Cannot set raid-devices for %s: %s\n", | |
845 | devname, strerror(errno)); | |
7236ee7a | 846 | rv = 1; |
9860f271 | 847 | } |
e86c9dd6 | 848 | } |
7236ee7a | 849 | break; |
e86c9dd6 NB |
850 | |
851 | case 4: | |
852 | case 5: | |
853 | case 6: | |
1686dc25 | 854 | |
7236ee7a N |
855 | /* |
856 | * layout/chunksize/raid_disks can be changed | |
e86c9dd6 | 857 | * though the kernel may not support it all. |
e86c9dd6 | 858 | */ |
4725bc31 N |
859 | st = super_by_fd(fd, &subarray); |
860 | if (subarray) { | |
861 | fprintf(stderr, Name ": Cannot reshape subarrays yet\n"); | |
862 | break; | |
863 | } | |
7236ee7a N |
864 | |
865 | /* | |
866 | * There are three possibilities. | |
867 | * 1/ The array will shrink. | |
868 | * We need to ensure the reshape will pause before reaching | |
869 | * the 'critical section'. We also need to fork and wait for | |
870 | * that to happen. When it does we | |
871 | * suspend/backup/complete/unfreeze | |
872 | * | |
873 | * 2/ The array will not change size. | |
874 | * This requires that we keep a backup of a sliding window | |
875 | * so that we can restore data after a crash. So we need | |
876 | * to fork and monitor progress. | |
877 | * | |
878 | * 3/ The array will grow. This is relatively easy. | |
e86c9dd6 NB |
879 | * However the kernel's restripe routines will cheerfully |
880 | * overwrite some early data before it is safe. So we | |
881 | * need to make a backup of the early parts of the array | |
882 | * and be ready to restore it if rebuild aborts very early. | |
883 | * | |
7236ee7a N |
884 | * We backup data by writing it to one spare, or to a |
885 | * file which was given on command line. | |
e86c9dd6 | 886 | * |
7236ee7a | 887 | * [FOLLOWING IS OLD AND PARTLY WRONG] |
e86c9dd6 NB |
888 | * So: we enumerate the devices in the array and |
889 | * make sure we can open all of them. | |
890 | * Then we freeze the early part of the array and | |
891 | * backup to the various spares. | |
892 | * Then we request changes and start the reshape. | |
893 | * Monitor progress until it has passed the danger zone. | |
894 | * and finally invalidate the copied data and unfreeze the | |
895 | * start of the array. | |
896 | * | |
7236ee7a N |
897 | * In each case, we first make sure that storage is available |
898 | * for the required backup. | |
899 | * Then we: | |
900 | * - request the shape change. | |
901 | * - for to handle backup etc. | |
e86c9dd6 | 902 | */ |
e86c9dd6 NB |
903 | nchunk = ochunk = array.chunk_size; |
904 | nlayout = olayout = array.layout; | |
905 | ndisks = odisks = array.raid_disks; | |
906 | ||
7236ee7a N |
907 | if (chunksize) { |
908 | nchunk = chunksize * 1024; | |
f98841b3 N |
909 | if (size % chunksize) { |
910 | fprintf(stderr, Name ": component size %lluK is not" | |
7236ee7a | 911 | " a multiple of chunksize %dK\n", |
f98841b3 | 912 | size, chunksize); |
7236ee7a N |
913 | break; |
914 | } | |
915 | } | |
19678e53 | 916 | if (layout_str != NULL) |
7236ee7a | 917 | switch(array.level) { |
19678e53 N |
918 | case 4: /* ignore layout */ |
919 | break; | |
920 | case 5: | |
921 | nlayout = map_name(r5layout, layout_str); | |
922 | if (nlayout == UnSet) { | |
923 | fprintf(stderr, Name ": layout %s not understood for raid5.\n", | |
924 | layout_str); | |
b5ea446a N |
925 | rv = 1; |
926 | goto release; | |
19678e53 N |
927 | } |
928 | break; | |
929 | ||
930 | case 6: | |
931 | nlayout = map_name(r6layout, layout_str); | |
932 | if (nlayout == UnSet) { | |
933 | fprintf(stderr, Name ": layout %s not understood for raid6.\n", | |
934 | layout_str); | |
b5ea446a N |
935 | rv = 1; |
936 | goto release; | |
19678e53 N |
937 | } |
938 | break; | |
939 | } | |
e86c9dd6 NB |
940 | if (raid_disks) ndisks = raid_disks; |
941 | ||
942 | odata = odisks-1; | |
e86c9dd6 | 943 | ndata = ndisks-1; |
7236ee7a N |
944 | if (array.level == 6) { |
945 | odata--; /* number of data disks */ | |
946 | ndata--; | |
e86c9dd6 | 947 | } |
7236ee7a | 948 | |
d2505cff N |
949 | if (odata == ndata && |
950 | get_linux_version() < 2006032) { | |
951 | fprintf(stderr, Name ": in-place reshape is not safe before 2.6.32, sorry.\n"); | |
952 | break; | |
953 | } | |
954 | ||
7236ee7a | 955 | /* Check that we can hold all the data */ |
7236ee7a | 956 | get_dev_size(fd, NULL, &array_size); |
f21e18ca | 957 | if (ndata * (unsigned long long)size < (array_size/1024)) { |
7236ee7a N |
958 | fprintf(stderr, Name ": this change will reduce the size of the array.\n" |
959 | " use --grow --array-size first to truncate array.\n" | |
960 | " e.g. mdadm --grow %s --array-size %llu\n", | |
f98841b3 | 961 | devname, ndata * size); |
7236ee7a N |
962 | rv = 1; |
963 | break; | |
e86c9dd6 | 964 | } |
7236ee7a N |
965 | |
966 | /* So how much do we need to backup. | |
967 | * We need an amount of data which is both a whole number of | |
968 | * old stripes and a whole number of new stripes. | |
969 | * So LCM for (chunksize*datadisks). | |
e86c9dd6 | 970 | */ |
200871ad N |
971 | a = (ochunk/512) * odata; |
972 | b = (nchunk/512) * ndata; | |
7236ee7a N |
973 | /* Find GCD */ |
974 | while (a != b) { | |
975 | if (a < b) | |
976 | b -= a; | |
977 | if (b < a) | |
978 | a -= b; | |
e86c9dd6 | 979 | } |
7236ee7a | 980 | /* LCM == product / GCD */ |
200871ad | 981 | blocks = (ochunk/512) * (nchunk/512) * odata * ndata / a; |
7236ee7a | 982 | |
e380d3be N |
983 | sysfs_free(sra); |
984 | sra = sysfs_read(fd, 0, | |
985 | GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE| | |
986 | GET_CACHE); | |
987 | ||
c03ef02d N |
988 | if (!sra) { |
989 | fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n", | |
990 | devname); | |
991 | rv = 1; | |
992 | break; | |
993 | } | |
994 | ||
eba71529 N |
995 | if (ndata == odata) { |
996 | /* Make 'blocks' bigger for better throughput, but | |
997 | * not so big that we reject it below. | |
1b13faf7 | 998 | * Try for 16 megabytes |
eba71529 | 999 | */ |
1b13faf7 N |
1000 | while (blocks * 32 < sra->component_size && |
1001 | blocks < 16*1024*2) | |
1002 | blocks *= 2; | |
eba71529 | 1003 | } else |
7236ee7a N |
1004 | fprintf(stderr, Name ": Need to backup %luK of critical " |
1005 | "section..\n", blocks/2); | |
e86c9dd6 | 1006 | |
7236ee7a | 1007 | if (blocks >= sra->component_size/2) { |
e86c9dd6 NB |
1008 | fprintf(stderr, Name ": %s: Something wrong - reshape aborted\n", |
1009 | devname); | |
7236ee7a N |
1010 | rv = 1; |
1011 | break; | |
353632d9 | 1012 | } |
925211e3 | 1013 | nrdisks = array.raid_disks + sra->array.spare_disks; |
e86c9dd6 NB |
1014 | /* Now we need to open all these devices so we can read/write. |
1015 | */ | |
06b0d786 NB |
1016 | fdlist = malloc((1+nrdisks) * sizeof(int)); |
1017 | offsets = malloc((1+nrdisks) * sizeof(offsets[0])); | |
e86c9dd6 NB |
1018 | if (!fdlist || !offsets) { |
1019 | fprintf(stderr, Name ": malloc failed: grow aborted\n"); | |
7236ee7a N |
1020 | rv = 1; |
1021 | break; | |
e86c9dd6 | 1022 | } |
06b0d786 | 1023 | for (d=0; d <= nrdisks; d++) |
e86c9dd6 NB |
1024 | fdlist[d] = -1; |
1025 | d = array.raid_disks; | |
1026 | for (sd = sra->devs; sd; sd=sd->next) { | |
06c7f68e | 1027 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) |
e86c9dd6 | 1028 | continue; |
06c7f68e NB |
1029 | if (sd->disk.state & (1<<MD_DISK_SYNC)) { |
1030 | char *dn = map_dev(sd->disk.major, | |
1031 | sd->disk.minor, 1); | |
1032 | fdlist[sd->disk.raid_disk] | |
1033 | = dev_open(dn, O_RDONLY); | |
7236ee7a | 1034 | offsets[sd->disk.raid_disk] = sd->data_offset*512; |
06c7f68e | 1035 | if (fdlist[sd->disk.raid_disk] < 0) { |
e86c9dd6 | 1036 | fprintf(stderr, Name ": %s: cannot open component %s\n", |
e81cdd9f | 1037 | devname, dn?dn:"-unknown-"); |
7236ee7a N |
1038 | rv = 1; |
1039 | goto release; | |
e86c9dd6 | 1040 | } |
7236ee7a | 1041 | } else if (backup_file == NULL) { |
e86c9dd6 | 1042 | /* spare */ |
06c7f68e NB |
1043 | char *dn = map_dev(sd->disk.major, |
1044 | sd->disk.minor, 1); | |
16c6fa80 | 1045 | fdlist[d] = dev_open(dn, O_RDWR); |
ff94fb86 | 1046 | offsets[d] = (sd->data_offset + sra->component_size - blocks - 8)*512; |
e86c9dd6 NB |
1047 | if (fdlist[d]<0) { |
1048 | fprintf(stderr, Name ": %s: cannot open component %s\n", | |
e81cdd9f | 1049 | devname, dn?dn:"-unknown"); |
7236ee7a N |
1050 | rv = 1; |
1051 | goto release; | |
e86c9dd6 NB |
1052 | } |
1053 | d++; | |
1054 | } | |
1055 | } | |
7236ee7a N |
1056 | if (backup_file == NULL) { |
1057 | if (ndata <= odata) { | |
1058 | fprintf(stderr, Name ": %s: Cannot grow - need backup-file\n", | |
1059 | devname); | |
1060 | rv = 1; | |
1061 | break; | |
1062 | } else if (sra->array.spare_disks == 0) { | |
1063 | fprintf(stderr, Name ": %s: Cannot grow - need a spare or " | |
1064 | "backup-file to backup critical section\n", | |
1065 | devname); | |
1066 | rv = 1; | |
1067 | break; | |
1068 | } | |
1069 | if (d == array.raid_disks) { | |
1070 | fprintf(stderr, Name ": %s: No spare device for backup\n", | |
1071 | devname); | |
1072 | rv = 1; | |
1073 | break; | |
e86c9dd6 | 1074 | } |
7236ee7a N |
1075 | } else { |
1076 | /* need to check backup file is large enough */ | |
1077 | char buf[512]; | |
1078 | fdlist[d] = open(backup_file, O_RDWR|O_CREAT|O_EXCL, | |
1079 | S_IRUSR | S_IWUSR); | |
1080 | offsets[d] = 8 * 512; | |
06b0d786 NB |
1081 | if (fdlist[d] < 0) { |
1082 | fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n", | |
1083 | devname, backup_file, strerror(errno)); | |
7236ee7a N |
1084 | rv = 1; |
1085 | break; | |
1086 | } | |
1087 | memset(buf, 0, 512); | |
f21e18ca | 1088 | for (i=0; i < (signed)blocks + 1 ; i++) { |
7236ee7a N |
1089 | if (write(fdlist[d], buf, 512) != 512) { |
1090 | fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n", | |
1091 | devname, backup_file, strerror(errno)); | |
1092 | rv = 1; | |
1093 | break; | |
1094 | } | |
1095 | } | |
1096 | if (fsync(fdlist[d]) != 0) { | |
1097 | fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n", | |
1098 | devname, backup_file, strerror(errno)); | |
1099 | rv = 1; | |
1100 | break; | |
06b0d786 | 1101 | } |
06b0d786 | 1102 | d++; |
06b0d786 | 1103 | } |
7236ee7a N |
1104 | |
1105 | /* lastly, check that the internal stripe cache is | |
1106 | * large enough, or it won't work. | |
1107 | */ | |
1108 | ||
1109 | cache = (nchunk < ochunk) ? ochunk : nchunk; | |
1110 | cache = cache * 4 / 4096; | |
1b13faf7 N |
1111 | if (cache < blocks / 8 / odisks + 16) |
1112 | /* Make it big enough to hold 'blocks' */ | |
1113 | cache = blocks / 8 / odisks + 16; | |
7236ee7a N |
1114 | if (sra->cache_size < cache) |
1115 | sysfs_set_num(sra, NULL, "stripe_cache_size", | |
1116 | cache+1); | |
1117 | /* Right, everything seems fine. Let's kick things off. | |
1118 | * If only changing raid_disks, use ioctl, else use | |
1119 | * sysfs. | |
1120 | */ | |
1121 | if (ochunk == nchunk && olayout == nlayout) { | |
1122 | array.raid_disks = ndisks; | |
1123 | if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) { | |
39bbb392 | 1124 | int err = errno; |
7236ee7a N |
1125 | rv = 1; |
1126 | fprintf(stderr, Name ": Cannot set device shape for %s: %s\n", | |
1127 | devname, strerror(errno)); | |
1128 | if (ndisks < odisks && | |
1129 | get_linux_version() < 2006030) | |
1130 | fprintf(stderr, Name ": linux 2.6.30 or later required\n"); | |
39bbb392 N |
1131 | if (err == EBUSY && |
1132 | (array.state & (1<<MD_SB_BITMAP_PRESENT))) | |
1133 | fprintf(stderr, " Bitmap must be removed before shape can be changed\n"); | |
7236ee7a N |
1134 | |
1135 | break; | |
1136 | } | |
1137 | } else { | |
1138 | /* set them all just in case some old 'new_*' value | |
1139 | * persists from some earlier problem | |
1140 | */ | |
53f50353 N |
1141 | int err = err; /* only used if rv==1, and always set if |
1142 | * rv==1, so initialisation not needed, | |
1143 | * despite gcc warning | |
1144 | */ | |
7236ee7a | 1145 | if (sysfs_set_num(sra, NULL, "chunk_size", nchunk) < 0) |
39bbb392 N |
1146 | rv = 1, err = errno; |
1147 | if (!rv && sysfs_set_num(sra, NULL, "layout", nlayout) < 0) | |
1148 | rv = 1, err = errno; | |
1149 | if (!rv && sysfs_set_num(sra, NULL, "raid_disks", ndisks) < 0) | |
1150 | rv = 1, err = errno; | |
7236ee7a N |
1151 | if (rv) { |
1152 | fprintf(stderr, Name ": Cannot set device shape for %s\n", | |
1153 | devname); | |
1154 | if (get_linux_version() < 2006030) | |
1155 | fprintf(stderr, Name ": linux 2.6.30 or later required\n"); | |
39bbb392 N |
1156 | if (err == EBUSY && |
1157 | (array.state & (1<<MD_SB_BITMAP_PRESENT))) | |
1158 | fprintf(stderr, " Bitmap must be removed before shape can be changed\n"); | |
7236ee7a N |
1159 | break; |
1160 | } | |
e86c9dd6 NB |
1161 | } |
1162 | ||
7236ee7a N |
1163 | if (ndisks == 2 && odisks == 2) { |
1164 | /* No reshape is needed in this trivial case */ | |
1165 | rv = 0; | |
1166 | break; | |
1167 | } | |
1168 | ||
1169 | /* set up the backup-super-block. This requires the | |
1170 | * uuid from the array. | |
1171 | */ | |
e86c9dd6 | 1172 | /* Find a superblock */ |
7236ee7a N |
1173 | for (sd = sra->devs; sd; sd = sd->next) { |
1174 | char *dn; | |
1175 | int devfd; | |
1176 | int ok; | |
1177 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) | |
1178 | continue; | |
1179 | dn = map_dev(sd->disk.major, sd->disk.minor, 1); | |
1180 | devfd = dev_open(dn, O_RDONLY); | |
1181 | if (devfd < 0) | |
1182 | continue; | |
1183 | ok = st->ss->load_super(st, devfd, NULL); | |
1184 | close(devfd); | |
1185 | if (ok >= 0) | |
1186 | break; | |
1187 | } | |
1188 | if (!sd) { | |
e86c9dd6 NB |
1189 | fprintf(stderr, Name ": %s: Cannot find a superblock\n", |
1190 | devname); | |
7236ee7a N |
1191 | rv = 1; |
1192 | break; | |
e86c9dd6 NB |
1193 | } |
1194 | ||
7236ee7a | 1195 | memset(&bsb, 0, 512); |
2efedc7b | 1196 | memcpy(bsb.magic, "md_backup_data-1", 16); |
3da92f27 | 1197 | st->ss->uuid_from_super(st, (int*)&bsb.set_uuid); |
2efedc7b | 1198 | bsb.mtime = __cpu_to_le64(time(0)); |
7236ee7a N |
1199 | bsb.devstart2 = blocks; |
1200 | stripes = blocks / (ochunk/512) / odata; | |
1201 | /* Now we just need to kick off the reshape and watch, while | |
1202 | * handling backups of the data... | |
1203 | * This is all done by a forked background process. | |
2efedc7b | 1204 | */ |
7236ee7a N |
1205 | switch(fork()) { |
1206 | case 0: | |
1207 | close(fd); | |
1208 | if (check_env("MDADM_GROW_VERIFY")) | |
1209 | fd = open(devname, O_RDONLY | O_DIRECT); | |
1210 | else | |
1211 | fd = -1; | |
1212 | mlockall(MCL_FUTURE); | |
1213 | ||
1214 | if (odata < ndata) | |
1215 | done = child_grow(fd, sra, stripes, | |
1216 | fdlist, offsets, | |
1217 | odisks, ochunk, array.level, olayout, odata, | |
1218 | d - odisks, fdlist+odisks, offsets+odisks); | |
1219 | else if (odata > ndata) | |
1220 | done = child_shrink(fd, sra, stripes, | |
1221 | fdlist, offsets, | |
1222 | odisks, ochunk, array.level, olayout, odata, | |
1223 | d - odisks, fdlist+odisks, offsets+odisks); | |
1224 | else | |
1225 | done = child_same_size(fd, sra, stripes, | |
1226 | fdlist, offsets, | |
e9e43ec3 | 1227 | 0, |
7236ee7a N |
1228 | odisks, ochunk, array.level, olayout, odata, |
1229 | d - odisks, fdlist+odisks, offsets+odisks); | |
1230 | if (backup_file && done) | |
1231 | unlink(backup_file); | |
1232 | if (level != UnSet && level != array.level) { | |
1233 | /* We need to wait for the reshape to finish | |
1234 | * (which will have happened unless odata < ndata) | |
1235 | * and then set the level | |
758d3a8e | 1236 | */ |
7236ee7a N |
1237 | |
1238 | c = map_num(pers, level); | |
1239 | if (c == NULL) | |
1240 | exit(0);/* not possible */ | |
1241 | ||
1242 | if (odata < ndata) | |
1243 | wait_reshape(sra); | |
1244 | err = sysfs_set_str(sra, NULL, "level", c); | |
1245 | if (err) | |
1246 | fprintf(stderr, Name ": %s: could not set level to %s\n", | |
1247 | devname, c); | |
758d3a8e | 1248 | } |
7236ee7a N |
1249 | exit(0); |
1250 | case -1: | |
1251 | fprintf(stderr, Name ": Cannot run child to monitor reshape: %s\n", | |
1252 | strerror(errno)); | |
1253 | rv = 1; | |
1254 | break; | |
1255 | default: | |
1256 | /* The child will take care of unfreezing the array */ | |
1257 | frozen = 0; | |
1258 | break; | |
e86c9dd6 | 1259 | } |
7236ee7a | 1260 | break; |
e86c9dd6 | 1261 | |
7236ee7a | 1262 | } |
e86c9dd6 | 1263 | |
7236ee7a N |
1264 | release: |
1265 | if (rv && orig_level != UnSet && sra) { | |
1266 | c = map_num(pers, orig_level); | |
1267 | if (c && sysfs_set_str(sra, NULL, "level", c) == 0) | |
1268 | fprintf(stderr, Name ": aborting level change\n"); | |
1269 | } | |
1270 | if (sra) | |
1271 | unfreeze_array(sra, frozen); | |
1272 | return rv; | |
1273 | } | |
e86c9dd6 | 1274 | |
7236ee7a N |
1275 | /* |
1276 | * We run a child process in the background which performs the following | |
1277 | * steps: | |
1278 | * - wait for resync to reach a certain point | |
1279 | * - suspend io to the following section | |
1280 | * - backup that section | |
1281 | * - allow resync to proceed further | |
1282 | * - resume io | |
1283 | * - discard the backup. | |
1284 | * | |
1285 | * When are combined in slightly different ways in the three cases. | |
1286 | * Grow: | |
1287 | * - suspend/backup/allow/wait/resume/discard | |
1288 | * Shrink: | |
1289 | * - allow/wait/suspend/backup/allow/wait/resume/discard | |
1290 | * same-size: | |
1291 | * - wait/resume/discard/suspend/backup/allow | |
1292 | * | |
1293 | * suspend/backup/allow always come together | |
1294 | * wait/resume/discard do too. | |
1295 | * For the same-size case we have two backups to improve flow. | |
1296 | * | |
1297 | */ | |
e86c9dd6 | 1298 | |
fcf57625 | 1299 | /* FIXME return status is never checked */ |
7236ee7a N |
1300 | int grow_backup(struct mdinfo *sra, |
1301 | unsigned long long offset, /* per device */ | |
1302 | unsigned long stripes, /* per device */ | |
1303 | int *sources, unsigned long long *offsets, | |
1304 | int disks, int chunk, int level, int layout, | |
1305 | int dests, int *destfd, unsigned long long *destoffsets, | |
d4445387 | 1306 | int part, int *degraded, |
7236ee7a N |
1307 | char *buf) |
1308 | { | |
1309 | /* Backup 'blocks' sectors at 'offset' on each device of the array, | |
1310 | * to storage 'destfd' (offset 'destoffsets'), after first | |
1311 | * suspending IO. Then allow resync to continue | |
1312 | * over the suspended section. | |
1313 | * Use part 'part' of the backup-super-block. | |
1314 | */ | |
1315 | int odata = disks; | |
1316 | int rv = 0; | |
1317 | int i; | |
f21e18ca N |
1318 | unsigned long long ll; |
1319 | int new_degraded; | |
7236ee7a N |
1320 | //printf("offset %llu\n", offset); |
1321 | if (level >= 4) | |
1322 | odata--; | |
1323 | if (level == 6) | |
1324 | odata--; | |
200871ad | 1325 | sysfs_set_num(sra, NULL, "suspend_hi", (offset + stripes * (chunk/512)) * odata); |
d4445387 | 1326 | /* Check that array hasn't become degraded, else we might backup the wrong data */ |
f21e18ca N |
1327 | sysfs_get_ll(sra, NULL, "degraded", &ll); |
1328 | new_degraded = (int)ll; | |
d4445387 N |
1329 | if (new_degraded != *degraded) { |
1330 | /* check each device to ensure it is still working */ | |
1331 | struct mdinfo *sd; | |
1332 | for (sd = sra->devs ; sd ; sd = sd->next) { | |
1333 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) | |
1334 | continue; | |
1335 | if (sd->disk.state & (1<<MD_DISK_SYNC)) { | |
1336 | char sbuf[20]; | |
1337 | if (sysfs_get_str(sra, sd, "state", sbuf, 20) < 0 || | |
1338 | strstr(sbuf, "faulty") || | |
1339 | strstr(sbuf, "in_sync") == NULL) { | |
1340 | /* this device is dead */ | |
1341 | sd->disk.state = (1<<MD_DISK_FAULTY); | |
1342 | if (sd->disk.raid_disk >= 0 && | |
1343 | sources[sd->disk.raid_disk] >= 0) { | |
1344 | close(sources[sd->disk.raid_disk]); | |
1345 | sources[sd->disk.raid_disk] = -1; | |
1346 | } | |
1347 | } | |
1348 | } | |
1349 | } | |
1350 | *degraded = new_degraded; | |
1351 | } | |
7236ee7a N |
1352 | if (part) { |
1353 | bsb.arraystart2 = __cpu_to_le64(offset * odata); | |
200871ad | 1354 | bsb.length2 = __cpu_to_le64(stripes * (chunk/512) * odata); |
7236ee7a N |
1355 | } else { |
1356 | bsb.arraystart = __cpu_to_le64(offset * odata); | |
200871ad | 1357 | bsb.length = __cpu_to_le64(stripes * (chunk/512) * odata); |
7236ee7a N |
1358 | } |
1359 | if (part) | |
1360 | bsb.magic[15] = '2'; | |
1361 | for (i = 0; i < dests; i++) | |
1362 | if (part) | |
1363 | lseek64(destfd[i], destoffsets[i] + __le64_to_cpu(bsb.devstart2)*512, 0); | |
1364 | else | |
1365 | lseek64(destfd[i], destoffsets[i], 0); | |
1366 | ||
1367 | rv = save_stripes(sources, offsets, | |
1368 | disks, chunk, level, layout, | |
1369 | dests, destfd, | |
1370 | offset*512*odata, stripes * chunk * odata, | |
1371 | buf); | |
1372 | ||
1373 | if (rv) | |
1374 | return rv; | |
97396422 | 1375 | bsb.mtime = __cpu_to_le64(time(0)); |
7236ee7a N |
1376 | for (i = 0; i < dests; i++) { |
1377 | bsb.devstart = __cpu_to_le64(destoffsets[i]/512); | |
1378 | ||
1379 | bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb)); | |
1380 | if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0) | |
1381 | bsb.sb_csum2 = bsb_csum((char*)&bsb, | |
1382 | ((char*)&bsb.sb_csum2)-((char*)&bsb)); | |
1383 | ||
72044953 | 1384 | rv = -1; |
f21e18ca N |
1385 | if ((unsigned long long)lseek64(destfd[i], destoffsets[i] - 4096, 0) |
1386 | != destoffsets[i] - 4096) | |
72044953 N |
1387 | break; |
1388 | if (write(destfd[i], &bsb, 512) != 512) | |
1389 | break; | |
ff94fb86 | 1390 | if (destoffsets[i] > 4096) { |
f21e18ca | 1391 | if ((unsigned long long)lseek64(destfd[i], destoffsets[i]+stripes*chunk*odata, 0) != |
a847575a | 1392 | destoffsets[i]+stripes*chunk*odata) |
72044953 N |
1393 | break; |
1394 | if (write(destfd[i], &bsb, 512) != 512) | |
1395 | break; | |
ff94fb86 | 1396 | } |
7236ee7a | 1397 | fsync(destfd[i]); |
72044953 | 1398 | rv = 0; |
7236ee7a | 1399 | } |
2efedc7b | 1400 | |
fcf57625 | 1401 | return rv; |
7236ee7a N |
1402 | } |
1403 | ||
1404 | /* in 2.6.30, the value reported by sync_completed can be | |
1405 | * less that it should be by one stripe. | |
1406 | * This only happens when reshape hits sync_max and pauses. | |
1407 | * So allow wait_backup to either extent sync_max further | |
1408 | * than strictly necessary, or return before the | |
1409 | * sync has got quite as far as we would really like. | |
1410 | * This is what 'blocks2' is for. | |
1411 | * The various caller give appropriate values so that | |
1412 | * every works. | |
1413 | */ | |
fcf57625 | 1414 | /* FIXME return value is often ignored */ |
7236ee7a N |
1415 | int wait_backup(struct mdinfo *sra, |
1416 | unsigned long long offset, /* per device */ | |
1417 | unsigned long long blocks, /* per device */ | |
1418 | unsigned long long blocks2, /* per device - hack */ | |
1419 | int dests, int *destfd, unsigned long long *destoffsets, | |
1420 | int part) | |
1421 | { | |
1422 | /* Wait for resync to pass the section that was backed up | |
1423 | * then erase the backup and allow IO | |
1424 | */ | |
1425 | int fd = sysfs_get_fd(sra, NULL, "sync_completed"); | |
1426 | unsigned long long completed; | |
1427 | int i; | |
fcf57625 | 1428 | int rv; |
7236ee7a N |
1429 | |
1430 | if (fd < 0) | |
1431 | return -1; | |
1432 | sysfs_set_num(sra, NULL, "sync_max", offset + blocks + blocks2); | |
1433 | if (offset == 0) | |
1434 | sysfs_set_str(sra, NULL, "sync_action", "reshape"); | |
1435 | do { | |
1436 | char action[20]; | |
1437 | fd_set rfds; | |
1438 | FD_ZERO(&rfds); | |
1439 | FD_SET(fd, &rfds); | |
1440 | select(fd+1, NULL, NULL, &rfds, NULL); | |
1441 | if (sysfs_fd_get_ll(fd, &completed) < 0) { | |
1442 | close(fd); | |
1443 | return -1; | |
e86c9dd6 | 1444 | } |
7236ee7a N |
1445 | if (sysfs_get_str(sra, NULL, "sync_action", |
1446 | action, 20) > 0 && | |
1447 | strncmp(action, "reshape", 7) != 0) | |
1448 | break; | |
1449 | } while (completed < offset + blocks); | |
1450 | close(fd); | |
1451 | ||
1452 | if (part) { | |
1453 | bsb.arraystart2 = __cpu_to_le64(0); | |
1454 | bsb.length2 = __cpu_to_le64(0); | |
1455 | } else { | |
1456 | bsb.arraystart = __cpu_to_le64(0); | |
1457 | bsb.length = __cpu_to_le64(0); | |
1458 | } | |
97396422 | 1459 | bsb.mtime = __cpu_to_le64(time(0)); |
fcf57625 | 1460 | rv = 0; |
7236ee7a N |
1461 | for (i = 0; i < dests; i++) { |
1462 | bsb.devstart = __cpu_to_le64(destoffsets[i]/512); | |
1463 | bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb)); | |
1464 | if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0) | |
1465 | bsb.sb_csum2 = bsb_csum((char*)&bsb, | |
1466 | ((char*)&bsb.sb_csum2)-((char*)&bsb)); | |
f21e18ca | 1467 | if ((unsigned long long)lseek64(destfd[i], destoffsets[i]-4096, 0) != |
a847575a | 1468 | destoffsets[i]-4096) |
72044953 N |
1469 | rv = -1; |
1470 | if (rv == 0 && | |
1471 | write(destfd[i], &bsb, 512) != 512) | |
1472 | rv = -1; | |
7236ee7a N |
1473 | fsync(destfd[i]); |
1474 | } | |
fcf57625 | 1475 | return rv; |
7236ee7a | 1476 | } |
e86c9dd6 | 1477 | |
7236ee7a N |
1478 | static void fail(char *msg) |
1479 | { | |
fcf57625 | 1480 | int rv; |
72044953 N |
1481 | rv = (write(2, msg, strlen(msg)) != (int)strlen(msg)); |
1482 | rv |= (write(2, "\n", 1) != 1); | |
fcf57625 | 1483 | exit(rv ? 1 : 2); |
7236ee7a N |
1484 | } |
1485 | ||
1486 | static char *abuf, *bbuf; | |
f21e18ca | 1487 | static unsigned long long abuflen; |
7236ee7a N |
1488 | static void validate(int afd, int bfd, unsigned long long offset) |
1489 | { | |
1490 | /* check that the data in the backup against the array. | |
1491 | * This is only used for regression testing and should not | |
1492 | * be used while the array is active | |
1493 | */ | |
7236ee7a N |
1494 | if (afd < 0) |
1495 | return; | |
1496 | lseek64(bfd, offset - 4096, 0); | |
1497 | if (read(bfd, &bsb2, 512) != 512) | |
1498 | fail("cannot read bsb"); | |
1499 | if (bsb2.sb_csum != bsb_csum((char*)&bsb2, | |
1500 | ((char*)&bsb2.sb_csum)-((char*)&bsb2))) | |
1501 | fail("first csum bad"); | |
1502 | if (memcmp(bsb2.magic, "md_backup_data", 14) != 0) | |
1503 | fail("magic is bad"); | |
1504 | if (memcmp(bsb2.magic, "md_backup_data-2", 16) == 0 && | |
1505 | bsb2.sb_csum2 != bsb_csum((char*)&bsb2, | |
1506 | ((char*)&bsb2.sb_csum2)-((char*)&bsb2))) | |
1507 | fail("second csum bad"); | |
1508 | ||
1509 | if (__le64_to_cpu(bsb2.devstart)*512 != offset) | |
1510 | fail("devstart is wrong"); | |
1511 | ||
1512 | if (bsb2.length) { | |
1513 | unsigned long long len = __le64_to_cpu(bsb2.length)*512; | |
1514 | ||
1515 | if (abuflen < len) { | |
1516 | free(abuf); | |
1517 | free(bbuf); | |
1518 | abuflen = len; | |
fcf57625 N |
1519 | if (posix_memalign((void**)&abuf, 4096, abuflen) || |
1520 | posix_memalign((void**)&bbuf, 4096, abuflen)) { | |
1521 | abuflen = 0; | |
1522 | /* just stop validating on mem-alloc failure */ | |
1523 | return; | |
1524 | } | |
e86c9dd6 | 1525 | } |
48924014 | 1526 | |
7236ee7a | 1527 | lseek64(bfd, offset, 0); |
f21e18ca | 1528 | if ((unsigned long long)read(bfd, bbuf, len) != len) { |
080fd005 | 1529 | //printf("len %llu\n", len); |
7236ee7a N |
1530 | fail("read first backup failed"); |
1531 | } | |
1532 | lseek64(afd, __le64_to_cpu(bsb2.arraystart)*512, 0); | |
f21e18ca | 1533 | if ((unsigned long long)read(afd, abuf, len) != len) |
7236ee7a N |
1534 | fail("read first from array failed"); |
1535 | if (memcmp(bbuf, abuf, len) != 0) { | |
080fd005 | 1536 | #if 0 |
7236ee7a N |
1537 | int i; |
1538 | printf("offset=%llu len=%llu\n", | |
080fd005 | 1539 | (unsigned long long)__le64_to_cpu(bsb2.arraystart)*512, len); |
7236ee7a N |
1540 | for (i=0; i<len; i++) |
1541 | if (bbuf[i] != abuf[i]) { | |
1542 | printf("first diff byte %d\n", i); | |
93ecfa01 | 1543 | break; |
7236ee7a | 1544 | } |
080fd005 | 1545 | #endif |
7236ee7a | 1546 | fail("data1 compare failed"); |
e86c9dd6 | 1547 | } |
7236ee7a N |
1548 | } |
1549 | if (bsb2.length2) { | |
1550 | unsigned long long len = __le64_to_cpu(bsb2.length2)*512; | |
1551 | ||
1552 | if (abuflen < len) { | |
1553 | free(abuf); | |
1554 | free(bbuf); | |
1555 | abuflen = len; | |
1556 | abuf = malloc(abuflen); | |
1557 | bbuf = malloc(abuflen); | |
e86c9dd6 NB |
1558 | } |
1559 | ||
7236ee7a | 1560 | lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0); |
f21e18ca | 1561 | if ((unsigned long long)read(bfd, bbuf, len) != len) |
7236ee7a N |
1562 | fail("read second backup failed"); |
1563 | lseek64(afd, __le64_to_cpu(bsb2.arraystart2)*512, 0); | |
f21e18ca | 1564 | if ((unsigned long long)read(afd, abuf, len) != len) |
7236ee7a N |
1565 | fail("read second from array failed"); |
1566 | if (memcmp(bbuf, abuf, len) != 0) | |
1567 | fail("data2 compare failed"); | |
e86c9dd6 | 1568 | } |
7236ee7a | 1569 | } |
e86c9dd6 | 1570 | |
7236ee7a N |
1571 | static int child_grow(int afd, struct mdinfo *sra, unsigned long stripes, |
1572 | int *fds, unsigned long long *offsets, | |
1573 | int disks, int chunk, int level, int layout, int data, | |
1574 | int dests, int *destfd, unsigned long long *destoffsets) | |
1575 | { | |
1576 | char *buf; | |
d4445387 | 1577 | int degraded = 0; |
e86c9dd6 | 1578 | |
fcf57625 N |
1579 | if (posix_memalign((void**)&buf, 4096, disks * chunk)) |
1580 | /* Don't start the 'reshape' */ | |
1581 | return 0; | |
7236ee7a N |
1582 | sysfs_set_num(sra, NULL, "suspend_hi", 0); |
1583 | sysfs_set_num(sra, NULL, "suspend_lo", 0); | |
1584 | grow_backup(sra, 0, stripes, | |
1585 | fds, offsets, disks, chunk, level, layout, | |
1586 | dests, destfd, destoffsets, | |
d4445387 | 1587 | 0, °raded, buf); |
7236ee7a | 1588 | validate(afd, destfd[0], destoffsets[0]); |
200871ad | 1589 | wait_backup(sra, 0, stripes * (chunk / 512), stripes * (chunk / 512), |
725cac4c N |
1590 | dests, destfd, destoffsets, |
1591 | 0); | |
200871ad | 1592 | sysfs_set_num(sra, NULL, "suspend_lo", (stripes * (chunk/512)) * data); |
7236ee7a N |
1593 | free(buf); |
1594 | /* FIXME this should probably be numeric */ | |
1595 | sysfs_set_str(sra, NULL, "sync_max", "max"); | |
e86c9dd6 | 1596 | return 1; |
7236ee7a | 1597 | } |
e86c9dd6 | 1598 | |
7236ee7a N |
1599 | static int child_shrink(int afd, struct mdinfo *sra, unsigned long stripes, |
1600 | int *fds, unsigned long long *offsets, | |
1601 | int disks, int chunk, int level, int layout, int data, | |
1602 | int dests, int *destfd, unsigned long long *destoffsets) | |
1603 | { | |
1604 | char *buf; | |
1605 | unsigned long long start; | |
1606 | int rv; | |
d4445387 | 1607 | int degraded = 0; |
7236ee7a | 1608 | |
fcf57625 N |
1609 | if (posix_memalign((void**)&buf, 4096, disks * chunk)) |
1610 | return 0; | |
200871ad | 1611 | start = sra->component_size - stripes * (chunk/512); |
7236ee7a N |
1612 | sysfs_set_num(sra, NULL, "sync_max", start); |
1613 | sysfs_set_str(sra, NULL, "sync_action", "reshape"); | |
1614 | sysfs_set_num(sra, NULL, "suspend_lo", 0); | |
1615 | sysfs_set_num(sra, NULL, "suspend_hi", 0); | |
200871ad | 1616 | rv = wait_backup(sra, 0, start - stripes * (chunk/512), stripes * (chunk/512), |
7236ee7a N |
1617 | dests, destfd, destoffsets, 0); |
1618 | if (rv < 0) | |
1619 | return 0; | |
1620 | grow_backup(sra, 0, stripes, | |
1621 | fds, offsets, | |
1622 | disks, chunk, level, layout, | |
1623 | dests, destfd, destoffsets, | |
d4445387 | 1624 | 0, °raded, buf); |
7236ee7a | 1625 | validate(afd, destfd[0], destoffsets[0]); |
200871ad | 1626 | wait_backup(sra, start, stripes*(chunk/512), 0, |
725cac4c | 1627 | dests, destfd, destoffsets, 0); |
200871ad | 1628 | sysfs_set_num(sra, NULL, "suspend_lo", (stripes * (chunk/512)) * data); |
7236ee7a N |
1629 | free(buf); |
1630 | /* FIXME this should probably be numeric */ | |
1631 | sysfs_set_str(sra, NULL, "sync_max", "max"); | |
1632 | return 1; | |
1633 | } | |
1634 | ||
1635 | static int child_same_size(int afd, struct mdinfo *sra, unsigned long stripes, | |
1636 | int *fds, unsigned long long *offsets, | |
e9e43ec3 | 1637 | unsigned long long start, |
7236ee7a N |
1638 | int disks, int chunk, int level, int layout, int data, |
1639 | int dests, int *destfd, unsigned long long *destoffsets) | |
1640 | { | |
e9e43ec3 | 1641 | unsigned long long size; |
7236ee7a N |
1642 | unsigned long tailstripes = stripes; |
1643 | int part; | |
1644 | char *buf; | |
1645 | unsigned long long speed; | |
d4445387 | 1646 | int degraded = 0; |
7236ee7a N |
1647 | |
1648 | ||
fcf57625 N |
1649 | if (posix_memalign((void**)&buf, 4096, disks * chunk)) |
1650 | return 0; | |
7236ee7a N |
1651 | |
1652 | sysfs_set_num(sra, NULL, "suspend_lo", 0); | |
1653 | sysfs_set_num(sra, NULL, "suspend_hi", 0); | |
1654 | ||
1655 | sysfs_get_ll(sra, NULL, "sync_speed_min", &speed); | |
1656 | sysfs_set_num(sra, NULL, "sync_speed_min", 200000); | |
1657 | ||
e9e43ec3 | 1658 | grow_backup(sra, start, stripes, |
7236ee7a N |
1659 | fds, offsets, |
1660 | disks, chunk, level, layout, | |
1661 | dests, destfd, destoffsets, | |
d4445387 | 1662 | 0, °raded, buf); |
200871ad | 1663 | grow_backup(sra, (start + stripes) * (chunk/512), stripes, |
7236ee7a N |
1664 | fds, offsets, |
1665 | disks, chunk, level, layout, | |
1666 | dests, destfd, destoffsets, | |
d4445387 | 1667 | 1, °raded, buf); |
7236ee7a N |
1668 | validate(afd, destfd[0], destoffsets[0]); |
1669 | part = 0; | |
e9e43ec3 | 1670 | start += stripes * 2; /* where to read next */ |
7236ee7a N |
1671 | size = sra->component_size / (chunk/512); |
1672 | while (start < size) { | |
200871ad N |
1673 | if (wait_backup(sra, (start-stripes*2)*(chunk/512), |
1674 | stripes*(chunk/512), 0, | |
7236ee7a N |
1675 | dests, destfd, destoffsets, |
1676 | part) < 0) | |
1677 | return 0; | |
200871ad | 1678 | sysfs_set_num(sra, NULL, "suspend_lo", start*(chunk/512) * data); |
7236ee7a N |
1679 | if (start + stripes > size) |
1680 | tailstripes = (size - start); | |
1681 | ||
200871ad | 1682 | grow_backup(sra, start*(chunk/512), tailstripes, |
7236ee7a N |
1683 | fds, offsets, |
1684 | disks, chunk, level, layout, | |
1685 | dests, destfd, destoffsets, | |
d4445387 | 1686 | part, °raded, buf); |
7236ee7a N |
1687 | start += stripes; |
1688 | part = 1 - part; | |
1689 | validate(afd, destfd[0], destoffsets[0]); | |
1690 | } | |
200871ad | 1691 | if (wait_backup(sra, (start-stripes*2) * (chunk/512), stripes * (chunk/512), 0, |
7236ee7a N |
1692 | dests, destfd, destoffsets, |
1693 | part) < 0) | |
1694 | return 0; | |
200871ad N |
1695 | sysfs_set_num(sra, NULL, "suspend_lo", ((start-stripes)*(chunk/512)) * data); |
1696 | wait_backup(sra, (start-stripes) * (chunk/512), tailstripes * (chunk/512), 0, | |
725cac4c N |
1697 | dests, destfd, destoffsets, |
1698 | 1-part); | |
200871ad | 1699 | sysfs_set_num(sra, NULL, "suspend_lo", (size*(chunk/512)) * data); |
7236ee7a N |
1700 | sysfs_set_num(sra, NULL, "sync_speed_min", speed); |
1701 | free(buf); | |
1702 | return 1; | |
e86c9dd6 | 1703 | } |
353632d9 NB |
1704 | |
1705 | /* | |
1706 | * If any spare contains md_back_data-1 which is recent wrt mtime, | |
1707 | * write that data into the array and update the super blocks with | |
1708 | * the new reshape_progress | |
1709 | */ | |
ea0ebe96 N |
1710 | int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt, |
1711 | char *backup_file, int verbose) | |
353632d9 NB |
1712 | { |
1713 | int i, j; | |
1714 | int old_disks; | |
353632d9 | 1715 | unsigned long long *offsets; |
82f2d6ab | 1716 | unsigned long long nstripe, ostripe; |
6e9eac4f | 1717 | int ndata, odata; |
353632d9 | 1718 | |
e9e43ec3 N |
1719 | if (info->new_level != info->array.level) |
1720 | return 1; /* Cannot handle level changes (they are instantaneous) */ | |
1721 | ||
1722 | odata = info->array.raid_disks - info->delta_disks - 1; | |
1723 | if (info->array.level == 6) odata--; /* number of data disks */ | |
1724 | ndata = info->array.raid_disks - 1; | |
1725 | if (info->new_level == 6) ndata--; | |
353632d9 NB |
1726 | |
1727 | old_disks = info->array.raid_disks - info->delta_disks; | |
1728 | ||
e9e43ec3 N |
1729 | if (info->delta_disks <= 0) |
1730 | /* Didn't grow, so the backup file must have | |
1731 | * been used | |
1732 | */ | |
1733 | old_disks = cnt; | |
06b0d786 | 1734 | for (i=old_disks-(backup_file?1:0); i<cnt; i++) { |
353632d9 | 1735 | struct mdinfo dinfo; |
06b0d786 | 1736 | int fd; |
e9e43ec3 | 1737 | int bsbsize; |
ea0ebe96 | 1738 | char *devname, namebuf[20]; |
353632d9 NB |
1739 | |
1740 | /* This was a spare and may have some saved data on it. | |
1741 | * Load the superblock, find and load the | |
1742 | * backup_super_block. | |
1743 | * If either fail, go on to next device. | |
1744 | * If the backup contains no new info, just return | |
206c5eae | 1745 | * else restore data and update all superblocks |
353632d9 | 1746 | */ |
06b0d786 NB |
1747 | if (i == old_disks-1) { |
1748 | fd = open(backup_file, O_RDONLY); | |
e9e43ec3 N |
1749 | if (fd<0) { |
1750 | fprintf(stderr, Name ": backup file %s inaccessible: %s\n", | |
1751 | backup_file, strerror(errno)); | |
06b0d786 | 1752 | continue; |
e9e43ec3 | 1753 | } |
ea0ebe96 | 1754 | devname = backup_file; |
06b0d786 NB |
1755 | } else { |
1756 | fd = fdlist[i]; | |
1757 | if (fd < 0) | |
1758 | continue; | |
3da92f27 | 1759 | if (st->ss->load_super(st, fd, NULL)) |
06b0d786 | 1760 | continue; |
353632d9 | 1761 | |
a5d85af7 | 1762 | st->ss->getinfo_super(st, &dinfo, NULL); |
3da92f27 NB |
1763 | st->ss->free_super(st); |
1764 | ||
06b0d786 NB |
1765 | if (lseek64(fd, |
1766 | (dinfo.data_offset + dinfo.component_size - 8) <<9, | |
ea0ebe96 N |
1767 | 0) < 0) { |
1768 | fprintf(stderr, Name ": Cannot seek on device %d\n", i); | |
06b0d786 | 1769 | continue; /* Cannot seek */ |
ea0ebe96 N |
1770 | } |
1771 | sprintf(namebuf, "device-%d", i); | |
1772 | devname = namebuf; | |
06b0d786 | 1773 | } |
ea0ebe96 N |
1774 | if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb)) { |
1775 | if (verbose) | |
1776 | fprintf(stderr, Name ": Cannot read from %s\n", devname); | |
353632d9 | 1777 | continue; /* Cannot read */ |
ea0ebe96 | 1778 | } |
e9e43ec3 | 1779 | if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0 && |
ea0ebe96 N |
1780 | memcmp(bsb.magic, "md_backup_data-2", 16) != 0) { |
1781 | if (verbose) | |
1782 | fprintf(stderr, Name ": No backup metadata on %s\n", devname); | |
353632d9 | 1783 | continue; |
ea0ebe96 N |
1784 | } |
1785 | if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb))) { | |
1786 | if (verbose) | |
1787 | fprintf(stderr, Name ": Bad backup-metadata checksum on %s\n", devname); | |
353632d9 | 1788 | continue; /* bad checksum */ |
ea0ebe96 | 1789 | } |
e9e43ec3 | 1790 | if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0 && |
ea0ebe96 N |
1791 | bsb.sb_csum2 != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum2)-((char*)&bsb))) { |
1792 | if (verbose) | |
1793 | fprintf(stderr, Name ": Bad backup-metadata checksum2 on %s\n", devname); | |
22e30516 | 1794 | continue; /* Bad second checksum */ |
ea0ebe96 N |
1795 | } |
1796 | if (memcmp(bsb.set_uuid,info->uuid, 16) != 0) { | |
1797 | if (verbose) | |
1798 | fprintf(stderr, Name ": Wrong uuid on backup-metadata on %s\n", devname); | |
353632d9 | 1799 | continue; /* Wrong uuid */ |
ea0ebe96 | 1800 | } |
353632d9 | 1801 | |
097075b6 N |
1802 | /* array utime and backup-mtime should be updated at much the same time, but it seems that |
1803 | * sometimes they aren't... So allow considerable flexability in matching, and allow | |
1804 | * this test to be overridden by an environment variable. | |
1805 | */ | |
f21e18ca N |
1806 | if (info->array.utime > (int)__le64_to_cpu(bsb.mtime) + 2*60*60 || |
1807 | info->array.utime < (int)__le64_to_cpu(bsb.mtime) - 10*60) { | |
097075b6 N |
1808 | if (check_env("MDADM_GROW_ALLOW_OLD")) { |
1809 | fprintf(stderr, Name ": accepting backup with timestamp %lu " | |
1810 | "for array with timestamp %lu\n", | |
1811 | (unsigned long)__le64_to_cpu(bsb.mtime), | |
1812 | (unsigned long)info->array.utime); | |
1813 | } else { | |
1814 | if (verbose) | |
1815 | fprintf(stderr, Name ": too-old timestamp on " | |
1816 | "backup-metadata on %s\n", devname); | |
1817 | continue; /* time stamp is too bad */ | |
1818 | } | |
ea0ebe96 | 1819 | } |
353632d9 | 1820 | |
e9e43ec3 N |
1821 | if (bsb.magic[15] == '1') { |
1822 | if (info->delta_disks >= 0) { | |
1823 | /* reshape_progress is increasing */ | |
1824 | if (__le64_to_cpu(bsb.arraystart) + __le64_to_cpu(bsb.length) < | |
ea0ebe96 N |
1825 | info->reshape_progress) { |
1826 | nonew: | |
1827 | if (verbose) | |
1828 | fprintf(stderr, Name ": backup-metadata found on %s but is not needed\n", devname); | |
e9e43ec3 | 1829 | continue; /* No new data here */ |
ea0ebe96 | 1830 | } |
e9e43ec3 N |
1831 | } else { |
1832 | /* reshape_progress is decreasing */ | |
1833 | if (__le64_to_cpu(bsb.arraystart) >= | |
1834 | info->reshape_progress) | |
ea0ebe96 | 1835 | goto nonew; /* No new data here */ |
e9e43ec3 N |
1836 | } |
1837 | } else { | |
1838 | if (info->delta_disks >= 0) { | |
1839 | /* reshape_progress is increasing */ | |
1840 | if (__le64_to_cpu(bsb.arraystart) + __le64_to_cpu(bsb.length) < | |
1841 | info->reshape_progress && | |
1842 | __le64_to_cpu(bsb.arraystart2) + __le64_to_cpu(bsb.length2) < | |
1843 | info->reshape_progress) | |
ea0ebe96 | 1844 | goto nonew; /* No new data here */ |
e9e43ec3 N |
1845 | } else { |
1846 | /* reshape_progress is decreasing */ | |
1847 | if (__le64_to_cpu(bsb.arraystart) >= | |
1848 | info->reshape_progress && | |
1849 | __le64_to_cpu(bsb.arraystart2) >= | |
1850 | info->reshape_progress) | |
ea0ebe96 | 1851 | goto nonew; /* No new data here */ |
e9e43ec3 N |
1852 | } |
1853 | } | |
ea0ebe96 N |
1854 | if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0) { |
1855 | second_fail: | |
1856 | if (verbose) | |
1857 | fprintf(stderr, Name ": Failed to verify secondary backup-metadata block on %s\n", | |
1858 | devname); | |
353632d9 | 1859 | continue; /* Cannot seek */ |
ea0ebe96 | 1860 | } |
2efedc7b | 1861 | /* There should be a duplicate backup superblock 4k before here */ |
06b0d786 | 1862 | if (lseek64(fd, -4096, 1) < 0 || |
0155af90 | 1863 | read(fd, &bsb2, sizeof(bsb2)) != sizeof(bsb2)) |
ea0ebe96 | 1864 | goto second_fail; /* Cannot find leading superblock */ |
e9e43ec3 N |
1865 | if (bsb.magic[15] == '1') |
1866 | bsbsize = offsetof(struct mdp_backup_super, pad1); | |
1867 | else | |
1868 | bsbsize = offsetof(struct mdp_backup_super, pad); | |
ff94fb86 | 1869 | if (memcmp(&bsb2, &bsb, bsbsize) != 0) |
ea0ebe96 | 1870 | goto second_fail; /* Cannot find leading superblock */ |
2efedc7b | 1871 | |
353632d9 NB |
1872 | /* Now need the data offsets for all devices. */ |
1873 | offsets = malloc(sizeof(*offsets)*info->array.raid_disks); | |
1874 | for(j=0; j<info->array.raid_disks; j++) { | |
1875 | if (fdlist[j] < 0) | |
1876 | continue; | |
3da92f27 | 1877 | if (st->ss->load_super(st, fdlist[j], NULL)) |
353632d9 NB |
1878 | /* FIXME should be this be an error */ |
1879 | continue; | |
a5d85af7 | 1880 | st->ss->getinfo_super(st, &dinfo, NULL); |
3da92f27 | 1881 | st->ss->free_super(st); |
14e5b4d7 | 1882 | offsets[j] = dinfo.data_offset * 512; |
353632d9 NB |
1883 | } |
1884 | printf(Name ": restoring critical section\n"); | |
1885 | ||
1886 | if (restore_stripes(fdlist, offsets, | |
1887 | info->array.raid_disks, | |
1888 | info->new_chunk, | |
1889 | info->new_level, | |
1890 | info->new_layout, | |
06b0d786 | 1891 | fd, __le64_to_cpu(bsb.devstart)*512, |
92dcdf7c | 1892 | __le64_to_cpu(bsb.arraystart)*512, |
e9e43ec3 N |
1893 | __le64_to_cpu(bsb.length)*512)) { |
1894 | /* didn't succeed, so giveup */ | |
ea0ebe96 N |
1895 | if (verbose) |
1896 | fprintf(stderr, Name ": Error restoring backup from %s\n", | |
1897 | devname); | |
e9e43ec3 N |
1898 | return 1; |
1899 | } | |
1900 | ||
1901 | if (bsb.magic[15] == '2' && | |
1902 | restore_stripes(fdlist, offsets, | |
1903 | info->array.raid_disks, | |
1904 | info->new_chunk, | |
1905 | info->new_level, | |
1906 | info->new_layout, | |
1907 | fd, __le64_to_cpu(bsb.devstart)*512 + | |
1908 | __le64_to_cpu(bsb.devstart2)*512, | |
92dcdf7c | 1909 | __le64_to_cpu(bsb.arraystart2)*512, |
e9e43ec3 | 1910 | __le64_to_cpu(bsb.length2)*512)) { |
353632d9 | 1911 | /* didn't succeed, so giveup */ |
ea0ebe96 N |
1912 | if (verbose) |
1913 | fprintf(stderr, Name ": Error restoring second backup from %s\n", | |
1914 | devname); | |
2295250a | 1915 | return 1; |
353632d9 NB |
1916 | } |
1917 | ||
e9e43ec3 | 1918 | |
353632d9 NB |
1919 | /* Ok, so the data is restored. Let's update those superblocks. */ |
1920 | ||
e9e43ec3 N |
1921 | if (info->delta_disks >= 0) { |
1922 | info->reshape_progress = __le64_to_cpu(bsb.arraystart) + | |
1923 | __le64_to_cpu(bsb.length); | |
1924 | if (bsb.magic[15] == '2') { | |
1925 | unsigned long long p2 = __le64_to_cpu(bsb.arraystart2) + | |
1926 | __le64_to_cpu(bsb.length2); | |
1927 | if (p2 > info->reshape_progress) | |
1928 | info->reshape_progress = p2; | |
1929 | } | |
1930 | } else { | |
1931 | info->reshape_progress = __le64_to_cpu(bsb.arraystart); | |
1932 | if (bsb.magic[15] == '2') { | |
1933 | unsigned long long p2 = __le64_to_cpu(bsb.arraystart2); | |
1934 | if (p2 < info->reshape_progress) | |
1935 | info->reshape_progress = p2; | |
1936 | } | |
1937 | } | |
353632d9 NB |
1938 | for (j=0; j<info->array.raid_disks; j++) { |
1939 | if (fdlist[j] < 0) continue; | |
3da92f27 | 1940 | if (st->ss->load_super(st, fdlist[j], NULL)) |
353632d9 | 1941 | continue; |
a5d85af7 | 1942 | st->ss->getinfo_super(st, &dinfo, NULL); |
e9e43ec3 | 1943 | dinfo.reshape_progress = info->reshape_progress; |
3da92f27 | 1944 | st->ss->update_super(st, &dinfo, |
68c7d6d7 NB |
1945 | "_reshape_progress", |
1946 | NULL,0, 0, NULL); | |
3da92f27 NB |
1947 | st->ss->store_super(st, fdlist[j]); |
1948 | st->ss->free_super(st); | |
353632d9 | 1949 | } |
353632d9 NB |
1950 | return 0; |
1951 | } | |
6e9eac4f NB |
1952 | /* Didn't find any backup data, try to see if any |
1953 | * was needed. | |
1954 | */ | |
82f2d6ab N |
1955 | if (info->delta_disks < 0) { |
1956 | /* When shrinking, the critical section is at the end. | |
1957 | * So see if we are before the critical section. | |
1958 | */ | |
1959 | unsigned long long first_block; | |
1960 | nstripe = ostripe = 0; | |
1961 | first_block = 0; | |
1962 | while (ostripe >= nstripe) { | |
1963 | ostripe += info->array.chunk_size / 512; | |
1964 | first_block = ostripe * odata; | |
1965 | nstripe = first_block / ndata / (info->new_chunk/512) * | |
1966 | (info->new_chunk/512); | |
1967 | } | |
1968 | ||
1969 | if (info->reshape_progress >= first_block) | |
1970 | return 0; | |
6e9eac4f | 1971 | } |
82f2d6ab N |
1972 | if (info->delta_disks > 0) { |
1973 | /* See if we are beyond the critical section. */ | |
1974 | unsigned long long last_block; | |
1975 | nstripe = ostripe = 0; | |
1976 | last_block = 0; | |
1977 | while (nstripe >= ostripe) { | |
1978 | nstripe += info->new_chunk / 512; | |
1979 | last_block = nstripe * ndata; | |
1980 | ostripe = last_block / odata / (info->array.chunk_size/512) * | |
1981 | (info->array.chunk_size/512); | |
1982 | } | |
6e9eac4f | 1983 | |
82f2d6ab N |
1984 | if (info->reshape_progress >= last_block) |
1985 | return 0; | |
1986 | } | |
6e9eac4f | 1987 | /* needed to recover critical section! */ |
ea0ebe96 N |
1988 | if (verbose) |
1989 | fprintf(stderr, Name ": Failed to find backup of critical section\n"); | |
2295250a | 1990 | return 1; |
353632d9 | 1991 | } |
e9e43ec3 N |
1992 | |
1993 | int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info, | |
1994 | char *backup_file) | |
1995 | { | |
1996 | /* Array is assembled and ready to be started, but | |
1997 | * monitoring is probably required. | |
1998 | * So: | |
1999 | * - start read-only | |
2000 | * - set upper bound for resync | |
2001 | * - initialise the 'suspend' boundaries | |
2002 | * - switch to read-write | |
2003 | * - fork and continue monitoring | |
2004 | */ | |
2005 | int err; | |
2006 | int backup_list[1]; | |
2007 | unsigned long long backup_offsets[1]; | |
2008 | int odisks, ndisks, ochunk, nchunk,odata,ndata; | |
2009 | unsigned long a,b,blocks,stripes; | |
2010 | int backup_fd; | |
2011 | int *fds; | |
2012 | unsigned long long *offsets; | |
2013 | int d; | |
2014 | struct mdinfo *sra, *sd; | |
2015 | int rv; | |
f21e18ca | 2016 | unsigned long cache; |
e9e43ec3 N |
2017 | int done = 0; |
2018 | ||
2019 | err = sysfs_set_str(info, NULL, "array_state", "readonly"); | |
2020 | if (err) | |
2021 | return err; | |
2022 | ||
2023 | /* make sure reshape doesn't progress until we are ready */ | |
2024 | sysfs_set_str(info, NULL, "sync_max", "0"); | |
2025 | sysfs_set_str(info, NULL, "array_state", "active"); /* FIXME or clean */ | |
5f6ca90a N |
2026 | |
2027 | sra = sysfs_read(-1, devname2devnum(info->sys_name), | |
2028 | GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE| | |
2029 | GET_CACHE); | |
2030 | if (!sra) | |
2031 | return 1; | |
2032 | ||
e9e43ec3 N |
2033 | /* ndisks is not growing, so raid_disks is old and +delta is new */ |
2034 | odisks = info->array.raid_disks; | |
2035 | ndisks = odisks + info->delta_disks; | |
2036 | odata = odisks - 1; | |
2037 | ndata = ndisks - 1; | |
2038 | if (info->array.level == 6) { | |
2039 | odata--; | |
2040 | ndata--; | |
2041 | } | |
2042 | ochunk = info->array.chunk_size; | |
2043 | nchunk = info->new_chunk; | |
2044 | ||
200871ad N |
2045 | a = (ochunk/512) * odata; |
2046 | b = (nchunk/512) * ndata; | |
e9e43ec3 N |
2047 | /* Find GCD */ |
2048 | while (a != b) { | |
2049 | if (a < b) | |
2050 | b -= a; | |
2051 | if (b < a) | |
2052 | a -= b; | |
2053 | } | |
2054 | /* LCM == product / GCD */ | |
200871ad | 2055 | blocks = (ochunk/512) * (nchunk/512) * odata * ndata / a; |
e9e43ec3 N |
2056 | |
2057 | if (ndata == odata) | |
1b13faf7 N |
2058 | while (blocks * 32 < sra->component_size && |
2059 | blocks < 16*1024*2) | |
2060 | blocks *= 2; | |
e9e43ec3 N |
2061 | stripes = blocks / (info->array.chunk_size/512) / odata; |
2062 | ||
1b13faf7 N |
2063 | /* check that the internal stripe cache is |
2064 | * large enough, or it won't work. | |
2065 | */ | |
2066 | cache = (nchunk < ochunk) ? ochunk : nchunk; | |
2067 | cache = cache * 4 / 4096; | |
2068 | if (cache < blocks / 8 / odisks + 16) | |
2069 | /* Make it big enough to hold 'blocks' */ | |
2070 | cache = blocks / 8 / odisks + 16; | |
2071 | if (sra->cache_size < cache) | |
2072 | sysfs_set_num(sra, NULL, "stripe_cache_size", | |
2073 | cache+1); | |
e9e43ec3 N |
2074 | |
2075 | memset(&bsb, 0, 512); | |
2076 | memcpy(bsb.magic, "md_backup_data-1", 16); | |
2077 | memcpy(&bsb.set_uuid, info->uuid, 16); | |
2078 | bsb.mtime = __cpu_to_le64(time(0)); | |
2079 | bsb.devstart2 = blocks; | |
2080 | ||
2081 | backup_fd = open(backup_file, O_RDWR|O_CREAT, S_IRUSR | S_IWUSR); | |
2082 | backup_list[0] = backup_fd; | |
2083 | backup_offsets[0] = 8 * 512; | |
2084 | fds = malloc(odisks * sizeof(fds[0])); | |
2085 | offsets = malloc(odisks * sizeof(offsets[0])); | |
2086 | for (d=0; d<odisks; d++) | |
2087 | fds[d] = -1; | |
2088 | ||
e9e43ec3 N |
2089 | for (sd = sra->devs; sd; sd = sd->next) { |
2090 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) | |
2091 | continue; | |
2092 | if (sd->disk.state & (1<<MD_DISK_SYNC)) { | |
2093 | char *dn = map_dev(sd->disk.major, | |
2094 | sd->disk.minor, 1); | |
2095 | fds[sd->disk.raid_disk] | |
2096 | = dev_open(dn, O_RDONLY); | |
2097 | offsets[sd->disk.raid_disk] = sd->data_offset*512; | |
2098 | if (fds[sd->disk.raid_disk] < 0) { | |
2099 | fprintf(stderr, Name ": %s: cannot open component %s\n", | |
2100 | info->sys_name, dn?dn:"-unknown-"); | |
2101 | rv = 1; | |
2102 | goto release; | |
2103 | } | |
2104 | free(dn); | |
2105 | } | |
2106 | } | |
2107 | ||
2108 | switch(fork()) { | |
2109 | case 0: | |
2110 | close(mdfd); | |
2111 | mlockall(MCL_FUTURE); | |
2112 | if (info->delta_disks < 0) | |
2113 | done = child_shrink(-1, info, stripes, | |
2114 | fds, offsets, | |
2115 | info->array.raid_disks, | |
2116 | info->array.chunk_size, | |
2117 | info->array.level, info->array.layout, | |
2118 | odata, | |
2119 | 1, backup_list, backup_offsets); | |
2120 | else if (info->delta_disks == 0) { | |
2121 | /* The 'start' is a per-device stripe number. | |
2122 | * reshape_progress is a per-array sector number. | |
2123 | * So divide by ndata * chunk_size | |
2124 | */ | |
2125 | unsigned long long start = info->reshape_progress / ndata; | |
2126 | start /= (info->array.chunk_size/512); | |
2127 | done = child_same_size(-1, info, stripes, | |
2128 | fds, offsets, | |
2129 | start, | |
2130 | info->array.raid_disks, | |
2131 | info->array.chunk_size, | |
2132 | info->array.level, info->array.layout, | |
2133 | odata, | |
2134 | 1, backup_list, backup_offsets); | |
2135 | } | |
2136 | if (backup_file && done) | |
2137 | unlink(backup_file); | |
2138 | /* FIXME should I intuit a level change */ | |
2139 | exit(0); | |
2140 | case -1: | |
2141 | fprintf(stderr, Name ": Cannot run child to continue monitoring reshape: %s\n", | |
2142 | strerror(errno)); | |
2143 | return 1; | |
2144 | default: | |
2145 | break; | |
2146 | } | |
2147 | release: | |
2148 | return 0; | |
2149 | } | |
2150 | ||
2151 |