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