]>
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 | ||
3f54bd62 AK |
38 | int restore_backup(struct supertype *st, |
39 | struct mdinfo *content, | |
40 | int working_disks, | |
41 | int next_spare, | |
42 | char *backup_file, | |
43 | int verbose) | |
44 | { | |
45 | int i; | |
46 | int *fdlist; | |
47 | struct mdinfo *dev; | |
48 | int err; | |
49 | int disk_count = next_spare + working_disks; | |
50 | ||
51 | dprintf("Called restore_backup()\n"); | |
52 | fdlist = malloc(sizeof(int) * disk_count); | |
53 | if (fdlist == NULL) { | |
54 | fprintf(stderr, | |
55 | Name ": cannot allocate memory for disk list\n"); | |
56 | return 1; | |
57 | } | |
58 | for (i = 0; i < next_spare; i++) | |
59 | fdlist[i] = -1; | |
60 | for (dev = content->devs; dev; dev = dev->next) { | |
61 | char buf[22]; | |
62 | int fd; | |
63 | sprintf(buf, "%d:%d", | |
64 | dev->disk.major, | |
65 | dev->disk.minor); | |
66 | fd = dev_open(buf, O_RDWR); | |
67 | ||
68 | if (dev->disk.raid_disk >= 0) | |
69 | fdlist[dev->disk.raid_disk] = fd; | |
70 | else | |
71 | fdlist[next_spare++] = fd; | |
72 | } | |
73 | ||
74 | if (st->ss->external && st->ss->recover_backup) | |
75 | err = st->ss->recover_backup(st, content); | |
76 | else | |
77 | err = Grow_restart(st, content, fdlist, next_spare, | |
78 | backup_file, verbose > 0); | |
79 | ||
80 | while (next_spare > 0) { | |
81 | disk_count--; | |
82 | if (fdlist[disk_count] >= 0) | |
83 | close(fdlist[disk_count]); | |
84 | } | |
85 | free(fdlist); | |
86 | if (err) { | |
87 | fprintf(stderr, Name ": Failed to restore critical" | |
88 | " section for reshape - sorry.\n"); | |
89 | if (!backup_file) | |
90 | fprintf(stderr, Name ": Possibly you need" | |
91 | " to specify a --backup-file\n"); | |
92 | return 1; | |
93 | } | |
94 | ||
95 | dprintf("restore_backup() returns status OK.\n"); | |
96 | return 0; | |
97 | } | |
98 | ||
e5329c37 NB |
99 | int Grow_Add_device(char *devname, int fd, char *newdev) |
100 | { | |
101 | /* Add a device to an active array. | |
102 | * Currently, just extend a linear array. | |
103 | * This requires writing a new superblock on the | |
104 | * new device, calling the kernel to add the device, | |
105 | * and if that succeeds, update the superblock on | |
106 | * all other devices. | |
107 | * This means that we need to *find* all other devices. | |
108 | */ | |
4b1ac34b NB |
109 | struct mdinfo info; |
110 | ||
e5329c37 NB |
111 | struct stat stb; |
112 | int nfd, fd2; | |
113 | int d, nd; | |
82d9eba6 | 114 | struct supertype *st = NULL; |
4725bc31 | 115 | char *subarray = NULL; |
e5329c37 | 116 | |
4b1ac34b | 117 | if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) { |
e5329c37 NB |
118 | fprintf(stderr, Name ": cannot get array info for %s\n", devname); |
119 | return 1; | |
120 | } | |
121 | ||
4725bc31 N |
122 | if (info.array.level != -1) { |
123 | fprintf(stderr, Name ": can only add devices to linear arrays\n"); | |
124 | return 1; | |
125 | } | |
126 | ||
127 | st = super_by_fd(fd, &subarray); | |
82d9eba6 | 128 | if (!st) { |
f9ce90ba NB |
129 | fprintf(stderr, Name ": cannot handle arrays with superblock version %d\n", info.array.major_version); |
130 | return 1; | |
131 | } | |
132 | ||
4725bc31 N |
133 | if (subarray) { |
134 | fprintf(stderr, Name ": Cannot grow linear sub-arrays yet\n"); | |
135 | free(subarray); | |
136 | free(st); | |
e5329c37 NB |
137 | } |
138 | ||
6416d527 | 139 | nfd = open(newdev, O_RDWR|O_EXCL|O_DIRECT); |
e5329c37 NB |
140 | if (nfd < 0) { |
141 | fprintf(stderr, Name ": cannot open %s\n", newdev); | |
4725bc31 | 142 | free(st); |
e5329c37 NB |
143 | return 1; |
144 | } | |
145 | fstat(nfd, &stb); | |
146 | if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
147 | fprintf(stderr, Name ": %s is not a block device!\n", newdev); | |
148 | close(nfd); | |
4725bc31 | 149 | free(st); |
e5329c37 NB |
150 | return 1; |
151 | } | |
152 | /* now check out all the devices and make sure we can read the superblock */ | |
4b1ac34b | 153 | for (d=0 ; d < info.array.raid_disks ; d++) { |
e5329c37 NB |
154 | mdu_disk_info_t disk; |
155 | char *dv; | |
156 | ||
4725bc31 N |
157 | st->ss->free_super(st); |
158 | ||
e5329c37 NB |
159 | disk.number = d; |
160 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) { | |
161 | fprintf(stderr, Name ": cannot get device detail for device %d\n", | |
162 | d); | |
4725bc31 N |
163 | close(nfd); |
164 | free(st); | |
e5329c37 NB |
165 | return 1; |
166 | } | |
16c6fa80 | 167 | dv = map_dev(disk.major, disk.minor, 1); |
e5329c37 NB |
168 | if (!dv) { |
169 | fprintf(stderr, Name ": cannot find device file for device %d\n", | |
170 | d); | |
4725bc31 N |
171 | close(nfd); |
172 | free(st); | |
e5329c37 NB |
173 | return 1; |
174 | } | |
16c6fa80 | 175 | fd2 = dev_open(dv, O_RDWR); |
e5329c37 NB |
176 | if (!fd2) { |
177 | fprintf(stderr, Name ": cannot open device file %s\n", dv); | |
4725bc31 N |
178 | close(nfd); |
179 | free(st); | |
e5329c37 NB |
180 | return 1; |
181 | } | |
3da92f27 NB |
182 | |
183 | if (st->ss->load_super(st, fd2, NULL)) { | |
e5329c37 | 184 | fprintf(stderr, Name ": cannot find super block on %s\n", dv); |
4725bc31 | 185 | close(nfd); |
e5329c37 | 186 | close(fd2); |
4725bc31 | 187 | free(st); |
e5329c37 NB |
188 | return 1; |
189 | } | |
190 | close(fd2); | |
191 | } | |
192 | /* Ok, looks good. Lets update the superblock and write it out to | |
193 | * newdev. | |
194 | */ | |
aba69144 | 195 | |
4b1ac34b NB |
196 | info.disk.number = d; |
197 | info.disk.major = major(stb.st_rdev); | |
198 | info.disk.minor = minor(stb.st_rdev); | |
199 | info.disk.raid_disk = d; | |
200 | info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE); | |
3da92f27 | 201 | st->ss->update_super(st, &info, "linear-grow-new", newdev, |
f752781f | 202 | 0, 0, NULL); |
e5329c37 | 203 | |
3da92f27 | 204 | if (st->ss->store_super(st, nfd)) { |
f752781f NB |
205 | fprintf(stderr, Name ": Cannot store new superblock on %s\n", |
206 | newdev); | |
e5329c37 NB |
207 | close(nfd); |
208 | return 1; | |
209 | } | |
e5329c37 | 210 | close(nfd); |
4b1ac34b NB |
211 | |
212 | if (ioctl(fd, ADD_NEW_DISK, &info.disk) != 0) { | |
e5329c37 NB |
213 | fprintf(stderr, Name ": Cannot add new disk to this array\n"); |
214 | return 1; | |
215 | } | |
216 | /* Well, that seems to have worked. | |
217 | * Now go through and update all superblocks | |
218 | */ | |
219 | ||
4b1ac34b | 220 | if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) { |
e5329c37 NB |
221 | fprintf(stderr, Name ": cannot get array info for %s\n", devname); |
222 | return 1; | |
223 | } | |
224 | ||
225 | nd = d; | |
4b1ac34b | 226 | for (d=0 ; d < info.array.raid_disks ; d++) { |
e5329c37 NB |
227 | mdu_disk_info_t disk; |
228 | char *dv; | |
229 | ||
230 | disk.number = d; | |
231 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) { | |
232 | fprintf(stderr, Name ": cannot get device detail for device %d\n", | |
233 | d); | |
234 | return 1; | |
235 | } | |
16c6fa80 | 236 | dv = map_dev(disk.major, disk.minor, 1); |
e5329c37 NB |
237 | if (!dv) { |
238 | fprintf(stderr, Name ": cannot find device file for device %d\n", | |
239 | d); | |
240 | return 1; | |
241 | } | |
16c6fa80 | 242 | fd2 = dev_open(dv, O_RDWR); |
e5329c37 NB |
243 | if (fd2 < 0) { |
244 | fprintf(stderr, Name ": cannot open device file %s\n", dv); | |
245 | return 1; | |
246 | } | |
3da92f27 | 247 | if (st->ss->load_super(st, fd2, NULL)) { |
e5329c37 NB |
248 | fprintf(stderr, Name ": cannot find super block on %s\n", dv); |
249 | close(fd); | |
250 | return 1; | |
251 | } | |
4b1ac34b NB |
252 | info.array.raid_disks = nd+1; |
253 | info.array.nr_disks = nd+1; | |
254 | info.array.active_disks = nd+1; | |
255 | info.array.working_disks = nd+1; | |
f752781f | 256 | |
3da92f27 | 257 | st->ss->update_super(st, &info, "linear-grow-update", dv, |
f752781f | 258 | 0, 0, NULL); |
aba69144 | 259 | |
3da92f27 | 260 | if (st->ss->store_super(st, fd2)) { |
e5329c37 NB |
261 | fprintf(stderr, Name ": Cannot store new superblock on %s\n", dv); |
262 | close(fd2); | |
263 | return 1; | |
264 | } | |
265 | close(fd2); | |
266 | } | |
267 | ||
268 | return 0; | |
269 | } | |
f5e166fe | 270 | |
8fac0577 | 271 | int Grow_addbitmap(char *devname, int fd, char *file, int chunk, int delay, int write_behind, int force) |
f5e166fe NB |
272 | { |
273 | /* | |
274 | * First check that array doesn't have a bitmap | |
275 | * Then create the bitmap | |
276 | * Then add it | |
277 | * | |
278 | * For internal bitmaps, we need to check the version, | |
279 | * find all the active devices, and write the bitmap block | |
280 | * to all devices | |
281 | */ | |
282 | mdu_bitmap_file_t bmf; | |
283 | mdu_array_info_t array; | |
284 | struct supertype *st; | |
4725bc31 | 285 | char *subarray = NULL; |
dcec9ee5 NB |
286 | int major = BITMAP_MAJOR_HI; |
287 | int vers = md_get_version(fd); | |
8fac0577 | 288 | unsigned long long bitmapsize, array_size; |
dcec9ee5 NB |
289 | |
290 | if (vers < 9003) { | |
291 | major = BITMAP_MAJOR_HOSTENDIAN; | |
b3bd581b N |
292 | fprintf(stderr, Name ": Warning - bitmaps created on this kernel" |
293 | " are not portable\n" | |
294 | " between different architectures. Consider upgrading" | |
295 | " the Linux kernel.\n"); | |
dcec9ee5 | 296 | } |
f5e166fe NB |
297 | |
298 | if (ioctl(fd, GET_BITMAP_FILE, &bmf) != 0) { | |
353632d9 | 299 | if (errno == ENOMEM) |
f5e166fe NB |
300 | fprintf(stderr, Name ": Memory allocation failure.\n"); |
301 | else | |
302 | fprintf(stderr, Name ": bitmaps not supported by this kernel.\n"); | |
303 | return 1; | |
304 | } | |
305 | if (bmf.pathname[0]) { | |
fe80f49b NB |
306 | if (strcmp(file,"none")==0) { |
307 | if (ioctl(fd, SET_BITMAP_FILE, -1)!= 0) { | |
308 | fprintf(stderr, Name ": failed to remove bitmap %s\n", | |
309 | bmf.pathname); | |
310 | return 1; | |
311 | } | |
312 | return 0; | |
313 | } | |
f5e166fe NB |
314 | fprintf(stderr, Name ": %s already has a bitmap (%s)\n", |
315 | devname, bmf.pathname); | |
316 | return 1; | |
317 | } | |
318 | if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) { | |
319 | fprintf(stderr, Name ": cannot get array status for %s\n", devname); | |
320 | return 1; | |
321 | } | |
322 | if (array.state & (1<<MD_SB_BITMAP_PRESENT)) { | |
fe80f49b NB |
323 | if (strcmp(file, "none")==0) { |
324 | array.state &= ~(1<<MD_SB_BITMAP_PRESENT); | |
325 | if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) { | |
326 | fprintf(stderr, Name ": failed to remove internal bitmap.\n"); | |
327 | return 1; | |
328 | } | |
329 | return 0; | |
330 | } | |
f5e166fe NB |
331 | fprintf(stderr, Name ": Internal bitmap already present on %s\n", |
332 | devname); | |
333 | return 1; | |
334 | } | |
4725bc31 N |
335 | |
336 | if (strcmp(file, "none") == 0) { | |
337 | fprintf(stderr, Name ": no bitmap found on %s\n", devname); | |
338 | return 1; | |
339 | } | |
5b28bd56 NB |
340 | if (array.level <= 0) { |
341 | fprintf(stderr, Name ": Bitmaps not meaningful with level %s\n", | |
342 | map_num(pers, array.level)?:"of this array"); | |
343 | return 1; | |
344 | } | |
8fac0577 NB |
345 | bitmapsize = array.size; |
346 | bitmapsize <<= 1; | |
beae1dfe | 347 | if (get_dev_size(fd, NULL, &array_size) && |
8fac0577 NB |
348 | array_size > (0x7fffffffULL<<9)) { |
349 | /* Array is big enough that we cannot trust array.size | |
350 | * try other approaches | |
351 | */ | |
352 | bitmapsize = get_component_size(fd); | |
353 | } | |
8fac0577 NB |
354 | if (bitmapsize == 0) { |
355 | fprintf(stderr, Name ": Cannot reliably determine size of array to create bitmap - sorry.\n"); | |
356 | return 1; | |
357 | } | |
358 | ||
f9c25f1d | 359 | if (array.level == 10) { |
8686f3ed | 360 | int ncopies = (array.layout&255)*((array.layout>>8)&255); |
f9c25f1d NB |
361 | bitmapsize = bitmapsize * array.raid_disks / ncopies; |
362 | } | |
363 | ||
4725bc31 | 364 | st = super_by_fd(fd, &subarray); |
f5e166fe NB |
365 | if (!st) { |
366 | fprintf(stderr, Name ": Cannot understand version %d.%d\n", | |
367 | array.major_version, array.minor_version); | |
368 | return 1; | |
369 | } | |
4725bc31 N |
370 | if (subarray) { |
371 | fprintf(stderr, Name ": Cannot add bitmaps to sub-arrays yet\n"); | |
372 | free(subarray); | |
373 | free(st); | |
fe80f49b | 374 | return 1; |
4725bc31 N |
375 | } |
376 | if (strcmp(file, "internal") == 0) { | |
f5e166fe | 377 | int d; |
ebeb3663 N |
378 | if (st->ss->add_internal_bitmap == NULL) { |
379 | fprintf(stderr, Name ": Internal bitmaps not supported " | |
380 | "with %s metadata\n", st->ss->name); | |
381 | return 1; | |
382 | } | |
ea329559 | 383 | for (d=0; d< st->max_devs; d++) { |
f5e166fe NB |
384 | mdu_disk_info_t disk; |
385 | char *dv; | |
386 | disk.number = d; | |
387 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) | |
388 | continue; | |
389 | if (disk.major == 0 && | |
390 | disk.minor == 0) | |
391 | continue; | |
392 | if ((disk.state & (1<<MD_DISK_SYNC))==0) | |
393 | continue; | |
16c6fa80 | 394 | dv = map_dev(disk.major, disk.minor, 1); |
f5e166fe | 395 | if (dv) { |
16c6fa80 | 396 | int fd2 = dev_open(dv, O_RDWR); |
f5e166fe NB |
397 | if (fd2 < 0) |
398 | continue; | |
3da92f27 | 399 | if (st->ss->load_super(st, fd2, NULL)==0) { |
199171a2 | 400 | if (st->ss->add_internal_bitmap( |
3da92f27 | 401 | st, |
199171a2 NB |
402 | &chunk, delay, write_behind, |
403 | bitmapsize, 0, major) | |
404 | ) | |
3da92f27 | 405 | st->ss->write_bitmap(st, fd2); |
21e92547 NB |
406 | else { |
407 | fprintf(stderr, Name ": failed to create internal bitmap - chunksize problem.\n"); | |
408 | close(fd2); | |
409 | return 1; | |
410 | } | |
f5e166fe NB |
411 | } |
412 | close(fd2); | |
413 | } | |
414 | } | |
415 | array.state |= (1<<MD_SB_BITMAP_PRESENT); | |
416 | if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) { | |
ff634064 N |
417 | if (errno == EBUSY) |
418 | fprintf(stderr, Name | |
419 | ": Cannot add bitmap while array is" | |
420 | " resyncing or reshaping etc.\n"); | |
f5e166fe NB |
421 | fprintf(stderr, Name ": failed to set internal bitmap.\n"); |
422 | return 1; | |
423 | } | |
fe80f49b NB |
424 | } else { |
425 | int uuid[4]; | |
426 | int bitmap_fd; | |
427 | int d; | |
428 | int max_devs = st->max_devs; | |
fe80f49b NB |
429 | |
430 | /* try to load a superblock */ | |
431 | for (d=0; d<max_devs; d++) { | |
432 | mdu_disk_info_t disk; | |
433 | char *dv; | |
434 | int fd2; | |
435 | disk.number = d; | |
436 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) | |
437 | continue; | |
438 | if ((disk.major==0 && disk.minor==0) || | |
439 | (disk.state & (1<<MD_DISK_REMOVED))) | |
440 | continue; | |
16c6fa80 | 441 | dv = map_dev(disk.major, disk.minor, 1); |
fe80f49b | 442 | if (!dv) continue; |
16c6fa80 | 443 | fd2 = dev_open(dv, O_RDONLY); |
fe80f49b | 444 | if (fd2 >= 0 && |
3da92f27 | 445 | st->ss->load_super(st, fd2, NULL) == 0) { |
fe80f49b | 446 | close(fd2); |
3da92f27 | 447 | st->ss->uuid_from_super(st, uuid); |
fe80f49b NB |
448 | break; |
449 | } | |
450 | close(fd2); | |
451 | } | |
452 | if (d == max_devs) { | |
453 | fprintf(stderr, Name ": cannot find UUID for array!\n"); | |
454 | return 1; | |
455 | } | |
8fac0577 | 456 | if (CreateBitmap(file, force, (char*)uuid, chunk, |
f9c25f1d | 457 | delay, write_behind, bitmapsize, major)) { |
fe80f49b NB |
458 | return 1; |
459 | } | |
460 | bitmap_fd = open(file, O_RDWR); | |
461 | if (bitmap_fd < 0) { | |
8fac0577 | 462 | fprintf(stderr, Name ": weird: %s cannot be opened\n", |
fe80f49b NB |
463 | file); |
464 | return 1; | |
465 | } | |
466 | if (ioctl(fd, SET_BITMAP_FILE, bitmap_fd) < 0) { | |
ff634064 N |
467 | int err = errno; |
468 | if (errno == EBUSY) | |
469 | fprintf(stderr, Name | |
470 | ": Cannot add bitmap while array is" | |
471 | " resyncing or reshaping etc.\n"); | |
fe80f49b | 472 | fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n", |
ff634064 | 473 | devname, strerror(err)); |
fe80f49b NB |
474 | return 1; |
475 | } | |
476 | } | |
f5e166fe NB |
477 | |
478 | return 0; | |
479 | } | |
480 | ||
e86c9dd6 NB |
481 | |
482 | /* | |
483 | * When reshaping an array we might need to backup some data. | |
484 | * This is written to all spares with a 'super_block' describing it. | |
ff94fb86 | 485 | * The superblock goes 4K from the end of the used space on the |
e86c9dd6 NB |
486 | * device. |
487 | * It if written after the backup is complete. | |
488 | * It has the following structure. | |
489 | */ | |
490 | ||
5fdf37e3 | 491 | static struct mdp_backup_super { |
7236ee7a | 492 | char magic[16]; /* md_backup_data-1 or -2 */ |
e86c9dd6 NB |
493 | __u8 set_uuid[16]; |
494 | __u64 mtime; | |
495 | /* start/sizes in 512byte sectors */ | |
7236ee7a | 496 | __u64 devstart; /* address on backup device/file of data */ |
e86c9dd6 NB |
497 | __u64 arraystart; |
498 | __u64 length; | |
499 | __u32 sb_csum; /* csum of preceeding bytes. */ | |
7236ee7a N |
500 | __u32 pad1; |
501 | __u64 devstart2; /* offset in to data of second section */ | |
502 | __u64 arraystart2; | |
503 | __u64 length2; | |
504 | __u32 sb_csum2; /* csum of preceeding bytes. */ | |
505 | __u8 pad[512-68-32]; | |
5fdf37e3 | 506 | } __attribute__((aligned(512))) bsb, bsb2; |
e86c9dd6 | 507 | |
4411fb17 | 508 | static __u32 bsb_csum(char *buf, int len) |
e86c9dd6 NB |
509 | { |
510 | int i; | |
511 | int csum = 0; | |
512 | for (i=0; i<len; i++) | |
513 | csum = (csum<<3) + buf[0]; | |
514 | return __cpu_to_le32(csum); | |
515 | } | |
516 | ||
d7ca196c N |
517 | static int check_idle(struct supertype *st) |
518 | { | |
519 | /* Check that all member arrays for this container, or the | |
520 | * container of this array, are idle | |
521 | */ | |
522 | int container_dev = (st->container_dev != NoMdDev | |
523 | ? st->container_dev : st->devnum); | |
524 | char container[40]; | |
525 | struct mdstat_ent *ent, *e; | |
526 | int is_idle = 1; | |
527 | ||
528 | fmt_devname(container, container_dev); | |
529 | ent = mdstat_read(0, 0); | |
530 | for (e = ent ; e; e = e->next) { | |
531 | if (!is_container_member(e, container)) | |
532 | continue; | |
533 | if (e->percent >= 0) { | |
534 | is_idle = 0; | |
535 | break; | |
536 | } | |
537 | } | |
538 | free_mdstat(ent); | |
539 | return is_idle; | |
540 | } | |
541 | ||
7f2ba464 | 542 | static int freeze_container(struct supertype *st) |
7236ee7a | 543 | { |
7f2ba464 DW |
544 | int container_dev = (st->container_dev != NoMdDev |
545 | ? st->container_dev : st->devnum); | |
d7ca196c | 546 | char container[40]; |
7f2ba464 | 547 | |
d7ca196c N |
548 | if (!check_idle(st)) |
549 | return -1; | |
550 | ||
551 | fmt_devname(container, container_dev); | |
7f2ba464 DW |
552 | |
553 | if (block_monitor(container, 1)) { | |
554 | fprintf(stderr, Name ": failed to freeze container\n"); | |
555 | return -2; | |
556 | } | |
557 | ||
558 | return 1; | |
559 | } | |
560 | ||
561 | static void unfreeze_container(struct supertype *st) | |
562 | { | |
563 | int container_dev = (st->container_dev != NoMdDev | |
564 | ? st->container_dev : st->devnum); | |
d7ca196c N |
565 | char container[40]; |
566 | ||
567 | fmt_devname(container, container_dev); | |
7f2ba464 DW |
568 | |
569 | unblock_monitor(container, 1); | |
570 | } | |
571 | ||
572 | static int freeze(struct supertype *st) | |
573 | { | |
574 | /* Try to freeze resync/rebuild on this array/container. | |
7236ee7a | 575 | * Return -1 if the array is busy, |
7f2ba464 | 576 | * return -2 container cannot be frozen, |
7236ee7a N |
577 | * return 0 if this kernel doesn't support 'frozen' |
578 | * return 1 if it worked. | |
579 | */ | |
7f2ba464 DW |
580 | if (st->ss->external) |
581 | return freeze_container(st); | |
582 | else { | |
583 | struct mdinfo *sra = sysfs_read(-1, st->devnum, GET_VERSION); | |
584 | int err; | |
815c8a7e | 585 | char buf[20]; |
7f2ba464 DW |
586 | |
587 | if (!sra) | |
588 | return -1; | |
815c8a7e N |
589 | /* Need to clear any 'read-auto' status */ |
590 | if (sysfs_get_str(sra, NULL, "array_state", buf, 20) > 0 && | |
591 | strncmp(buf, "read-auto", 9) == 0) | |
592 | sysfs_set_str(sra, NULL, "array_state", "clean"); | |
593 | ||
7f2ba464 DW |
594 | err = sysfs_freeze_array(sra); |
595 | sysfs_free(sra); | |
596 | return err; | |
597 | } | |
7236ee7a N |
598 | } |
599 | ||
9202b8eb | 600 | static void unfreeze(struct supertype *st) |
7236ee7a | 601 | { |
7f2ba464 DW |
602 | if (st->ss->external) |
603 | return unfreeze_container(st); | |
604 | else { | |
605 | struct mdinfo *sra = sysfs_read(-1, st->devnum, GET_VERSION); | |
606 | ||
607 | if (sra) | |
608 | sysfs_set_str(sra, NULL, "sync_action", "idle"); | |
609 | else | |
610 | fprintf(stderr, Name ": failed to unfreeze array\n"); | |
611 | sysfs_free(sra); | |
612 | } | |
7236ee7a N |
613 | } |
614 | ||
4411fb17 | 615 | static void wait_reshape(struct mdinfo *sra) |
7236ee7a N |
616 | { |
617 | int fd = sysfs_get_fd(sra, NULL, "sync_action"); | |
618 | char action[20]; | |
619 | ||
92a19f1a AK |
620 | if (fd < 0) |
621 | return; | |
622 | ||
623 | while (sysfs_fd_get_str(fd, action, 20) > 0 && | |
624 | strncmp(action, "reshape", 7) == 0) { | |
7236ee7a N |
625 | fd_set rfds; |
626 | FD_ZERO(&rfds); | |
627 | FD_SET(fd, &rfds); | |
628 | select(fd+1, NULL, NULL, &rfds, NULL); | |
92a19f1a AK |
629 | } |
630 | close(fd); | |
7236ee7a | 631 | } |
7bc71196 DW |
632 | |
633 | static int reshape_super(struct supertype *st, long long size, int level, | |
634 | int layout, int chunksize, int raid_disks, | |
41784c88 AK |
635 | int delta_disks, char *backup_file, char *dev, |
636 | int verbose) | |
7bc71196 DW |
637 | { |
638 | /* nothing extra to check in the native case */ | |
639 | if (!st->ss->external) | |
640 | return 0; | |
641 | if (!st->ss->reshape_super || | |
642 | !st->ss->manage_reshape) { | |
643 | fprintf(stderr, Name ": %s metadata does not support reshape\n", | |
644 | st->ss->name); | |
645 | return 1; | |
646 | } | |
647 | ||
648 | return st->ss->reshape_super(st, size, level, layout, chunksize, | |
41784c88 AK |
649 | raid_disks, delta_disks, backup_file, dev, |
650 | verbose); | |
7bc71196 DW |
651 | } |
652 | ||
653 | static void sync_metadata(struct supertype *st) | |
654 | { | |
655 | if (st->ss->external) { | |
76266030 | 656 | if (st->update_tail) { |
7bc71196 | 657 | flush_metadata_updates(st); |
76266030 N |
658 | st->update_tail = &st->updates; |
659 | } else | |
7bc71196 DW |
660 | st->ss->sync_metadata(st); |
661 | } | |
662 | } | |
663 | ||
664 | static int subarray_set_num(char *container, struct mdinfo *sra, char *name, int n) | |
665 | { | |
666 | /* when dealing with external metadata subarrays we need to be | |
667 | * prepared to handle EAGAIN. The kernel may need to wait for | |
668 | * mdmon to mark the array active so the kernel can handle | |
669 | * allocations/writeback when preparing the reshape action | |
670 | * (md_allow_write()). We temporarily disable safe_mode_delay | |
671 | * to close a race with the array_state going clean before the | |
672 | * next write to raid_disks / stripe_cache_size | |
673 | */ | |
674 | char safe[50]; | |
675 | int rc; | |
676 | ||
677 | /* only 'raid_disks' and 'stripe_cache_size' trigger md_allow_write */ | |
5da9ab98 N |
678 | if (!container || |
679 | (strcmp(name, "raid_disks") != 0 && | |
680 | strcmp(name, "stripe_cache_size") != 0)) | |
7bc71196 DW |
681 | return sysfs_set_num(sra, NULL, name, n); |
682 | ||
683 | rc = sysfs_get_str(sra, NULL, "safe_mode_delay", safe, sizeof(safe)); | |
684 | if (rc <= 0) | |
685 | return -1; | |
686 | sysfs_set_num(sra, NULL, "safe_mode_delay", 0); | |
687 | rc = sysfs_set_num(sra, NULL, name, n); | |
688 | if (rc < 0 && errno == EAGAIN) { | |
689 | ping_monitor(container); | |
690 | /* if we get EAGAIN here then the monitor is not active | |
691 | * so stop trying | |
692 | */ | |
693 | rc = sysfs_set_num(sra, NULL, name, n); | |
694 | } | |
695 | sysfs_set_str(sra, NULL, "safe_mode_delay", safe); | |
696 | return rc; | |
697 | } | |
698 | ||
1e971e71 | 699 | int start_reshape(struct mdinfo *sra, int already_running) |
47eb4d5a N |
700 | { |
701 | int err; | |
0f28668b | 702 | sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL); |
47eb4d5a N |
703 | err = sysfs_set_num(sra, NULL, "suspend_hi", 0); |
704 | err = err ?: sysfs_set_num(sra, NULL, "suspend_lo", 0); | |
1e971e71 N |
705 | if (!already_running) |
706 | sysfs_set_num(sra, NULL, "sync_min", 0); | |
47eb4d5a | 707 | err = err ?: sysfs_set_num(sra, NULL, "sync_max", 0); |
1e971e71 N |
708 | if (!already_running) |
709 | err = err ?: sysfs_set_str(sra, NULL, "sync_action", "reshape"); | |
47eb4d5a N |
710 | |
711 | return err; | |
712 | } | |
713 | ||
714 | void abort_reshape(struct mdinfo *sra) | |
715 | { | |
716 | sysfs_set_str(sra, NULL, "sync_action", "idle"); | |
717 | sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL); | |
718 | sysfs_set_num(sra, NULL, "suspend_hi", 0); | |
719 | sysfs_set_num(sra, NULL, "suspend_lo", 0); | |
720 | sysfs_set_num(sra, NULL, "sync_min", 0); | |
721 | sysfs_set_str(sra, NULL, "sync_max", "max"); | |
722 | } | |
723 | ||
dfe77a9e KW |
724 | int remove_disks_for_takeover(struct supertype *st, |
725 | struct mdinfo *sra, | |
726 | int layout) | |
62a48395 AK |
727 | { |
728 | int nr_of_copies; | |
729 | struct mdinfo *remaining; | |
730 | int slot; | |
731 | ||
dfe77a9e KW |
732 | if (sra->array.level == 10) |
733 | nr_of_copies = layout & 0xff; | |
734 | else if (sra->array.level == 1) | |
735 | nr_of_copies = sra->array.raid_disks; | |
736 | else | |
737 | return 1; | |
62a48395 AK |
738 | |
739 | remaining = sra->devs; | |
740 | sra->devs = NULL; | |
741 | /* for each 'copy', select one device and remove from the list. */ | |
742 | for (slot = 0; slot < sra->array.raid_disks; slot += nr_of_copies) { | |
743 | struct mdinfo **diskp; | |
744 | int found = 0; | |
745 | ||
746 | /* Find a working device to keep */ | |
747 | for (diskp = &remaining; *diskp ; diskp = &(*diskp)->next) { | |
748 | struct mdinfo *disk = *diskp; | |
749 | ||
750 | if (disk->disk.raid_disk < slot) | |
751 | continue; | |
752 | if (disk->disk.raid_disk >= slot + nr_of_copies) | |
753 | continue; | |
754 | if (disk->disk.state & (1<<MD_DISK_REMOVED)) | |
755 | continue; | |
756 | if (disk->disk.state & (1<<MD_DISK_FAULTY)) | |
757 | continue; | |
758 | if (!(disk->disk.state & (1<<MD_DISK_SYNC))) | |
759 | continue; | |
760 | ||
761 | /* We have found a good disk to use! */ | |
762 | *diskp = disk->next; | |
763 | disk->next = sra->devs; | |
764 | sra->devs = disk; | |
765 | found = 1; | |
766 | break; | |
767 | } | |
768 | if (!found) | |
769 | break; | |
770 | } | |
771 | ||
772 | if (slot < sra->array.raid_disks) { | |
773 | /* didn't find all slots */ | |
774 | struct mdinfo **e; | |
775 | e = &remaining; | |
776 | while (*e) | |
777 | e = &(*e)->next; | |
778 | *e = sra->devs; | |
779 | sra->devs = remaining; | |
780 | return 1; | |
781 | } | |
782 | ||
783 | /* Remove all 'remaining' devices from the array */ | |
784 | while (remaining) { | |
785 | struct mdinfo *sd = remaining; | |
786 | remaining = sd->next; | |
787 | ||
788 | sysfs_set_str(sra, sd, "state", "faulty"); | |
789 | sysfs_set_str(sra, sd, "slot", "none"); | |
d5ca4a23 KW |
790 | /* for external metadata disks should be removed in mdmon */ |
791 | if (!st->ss->external) | |
792 | sysfs_set_str(sra, sd, "state", "remove"); | |
62a48395 AK |
793 | sd->disk.state |= (1<<MD_DISK_REMOVED); |
794 | sd->disk.state &= ~(1<<MD_DISK_SYNC); | |
795 | sd->next = sra->devs; | |
796 | sra->devs = sd; | |
797 | } | |
798 | return 0; | |
799 | } | |
800 | ||
130994cb AK |
801 | void reshape_free_fdlist(int *fdlist, |
802 | unsigned long long *offsets, | |
803 | int size) | |
804 | { | |
805 | int i; | |
806 | ||
807 | for (i = 0; i < size; i++) | |
808 | if (fdlist[i] >= 0) | |
809 | close(fdlist[i]); | |
810 | ||
811 | free(fdlist); | |
812 | free(offsets); | |
813 | } | |
814 | ||
815 | int reshape_prepare_fdlist(char *devname, | |
816 | struct mdinfo *sra, | |
817 | int raid_disks, | |
818 | int nrdisks, | |
819 | unsigned long blocks, | |
820 | char *backup_file, | |
821 | int *fdlist, | |
822 | unsigned long long *offsets) | |
823 | { | |
824 | int d = 0; | |
825 | struct mdinfo *sd; | |
826 | ||
827 | for (d = 0; d <= nrdisks; d++) | |
828 | fdlist[d] = -1; | |
829 | d = raid_disks; | |
830 | for (sd = sra->devs; sd; sd = sd->next) { | |
831 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) | |
832 | continue; | |
833 | if (sd->disk.state & (1<<MD_DISK_SYNC)) { | |
834 | char *dn = map_dev(sd->disk.major, | |
835 | sd->disk.minor, 1); | |
836 | fdlist[sd->disk.raid_disk] | |
837 | = dev_open(dn, O_RDONLY); | |
838 | offsets[sd->disk.raid_disk] = sd->data_offset*512; | |
839 | if (fdlist[sd->disk.raid_disk] < 0) { | |
840 | fprintf(stderr, | |
841 | Name ": %s: cannot open component %s\n", | |
842 | devname, dn ? dn : "-unknown-"); | |
843 | d = -1; | |
844 | goto release; | |
845 | } | |
846 | } else if (backup_file == NULL) { | |
847 | /* spare */ | |
848 | char *dn = map_dev(sd->disk.major, | |
849 | sd->disk.minor, 1); | |
850 | fdlist[d] = dev_open(dn, O_RDWR); | |
851 | offsets[d] = (sd->data_offset + sra->component_size - blocks - 8)*512; | |
852 | if (fdlist[d] < 0) { | |
853 | fprintf(stderr, Name ": %s: cannot open component %s\n", | |
854 | devname, dn ? dn : "-unknown-"); | |
855 | d = -1; | |
856 | goto release; | |
857 | } | |
858 | d++; | |
859 | } | |
860 | } | |
861 | release: | |
862 | return d; | |
863 | } | |
864 | ||
e6e9d47b AK |
865 | int reshape_open_backup_file(char *backup_file, |
866 | int fd, | |
867 | char *devname, | |
868 | long blocks, | |
869 | int *fdlist, | |
a93f87ee N |
870 | unsigned long long *offsets, |
871 | int restart) | |
e6e9d47b AK |
872 | { |
873 | /* Return 1 on success, 0 on any form of failure */ | |
874 | /* need to check backup file is large enough */ | |
875 | char buf[512]; | |
876 | struct stat stb; | |
877 | unsigned int dev; | |
878 | int i; | |
879 | ||
a93f87ee | 880 | *fdlist = open(backup_file, O_RDWR|O_CREAT|(restart ? O_TRUNC : O_EXCL), |
e6e9d47b AK |
881 | S_IRUSR | S_IWUSR); |
882 | *offsets = 8 * 512; | |
883 | if (*fdlist < 0) { | |
884 | fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n", | |
885 | devname, backup_file, strerror(errno)); | |
886 | return 0; | |
887 | } | |
888 | /* Guard against backup file being on array device. | |
889 | * If array is partitioned or if LVM etc is in the | |
890 | * way this will not notice, but it is better than | |
891 | * nothing. | |
892 | */ | |
893 | fstat(*fdlist, &stb); | |
894 | dev = stb.st_dev; | |
895 | fstat(fd, &stb); | |
896 | if (stb.st_rdev == dev) { | |
897 | fprintf(stderr, Name ": backup file must NOT be" | |
898 | " on the array being reshaped.\n"); | |
899 | close(*fdlist); | |
900 | return 0; | |
901 | } | |
902 | ||
903 | memset(buf, 0, 512); | |
ca4fe0bf | 904 | for (i=0; i < blocks + 8 ; i++) { |
e6e9d47b AK |
905 | if (write(*fdlist, buf, 512) != 512) { |
906 | fprintf(stderr, Name ": %s: cannot create" | |
907 | " backup file %s: %s\n", | |
908 | devname, backup_file, strerror(errno)); | |
909 | return 0; | |
910 | } | |
911 | } | |
912 | if (fsync(*fdlist) != 0) { | |
913 | fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n", | |
914 | devname, backup_file, strerror(errno)); | |
915 | return 0; | |
916 | } | |
917 | ||
918 | return 1; | |
919 | } | |
920 | ||
1c009fc2 AK |
921 | unsigned long compute_backup_blocks(int nchunk, int ochunk, |
922 | unsigned int ndata, unsigned int odata) | |
923 | { | |
924 | unsigned long a, b, blocks; | |
925 | /* So how much do we need to backup. | |
926 | * We need an amount of data which is both a whole number of | |
927 | * old stripes and a whole number of new stripes. | |
928 | * So LCM for (chunksize*datadisks). | |
929 | */ | |
930 | a = (ochunk/512) * odata; | |
931 | b = (nchunk/512) * ndata; | |
932 | /* Find GCD */ | |
933 | while (a != b) { | |
934 | if (a < b) | |
935 | b -= a; | |
936 | if (b < a) | |
937 | a -= b; | |
938 | } | |
939 | /* LCM == product / GCD */ | |
940 | blocks = (ochunk/512) * (nchunk/512) * odata * ndata / a; | |
941 | ||
942 | return blocks; | |
943 | } | |
944 | ||
5da9ab98 N |
945 | char *analyse_change(struct mdinfo *info, struct reshape *re) |
946 | { | |
947 | /* Based on the current array state in info->array and | |
948 | * the changes in info->new_* etc, determine: | |
949 | * - whether the change is possible | |
950 | * - Intermediate level/raid_disks/layout | |
951 | * - whether a restriping reshape is needed | |
952 | * - number of sectors in minimum change unit. This | |
953 | * will cover a whole number of stripes in 'before' and | |
954 | * 'after'. | |
955 | * | |
956 | * Return message if the change should be rejected | |
957 | * NULL if the change can be achieved | |
958 | * | |
959 | * This can be called as part of starting a reshape, or | |
960 | * when assembling an array that is undergoing reshape. | |
961 | */ | |
962 | int new_disks; | |
b545e14a N |
963 | /* delta_parity records change in number of devices |
964 | * caused by level change | |
965 | */ | |
966 | int delta_parity = 0; | |
5da9ab98 N |
967 | |
968 | /* If a new level not explicitly given, we assume no-change */ | |
969 | if (info->new_level == UnSet) | |
970 | info->new_level = info->array.level; | |
971 | ||
972 | if (info->new_chunk) | |
973 | switch (info->new_level) { | |
974 | case 0: | |
975 | case 4: | |
976 | case 5: | |
977 | case 6: | |
978 | case 10: | |
979 | /* chunk size is meaningful, must divide component_size | |
980 | * evenly | |
981 | */ | |
982 | if (info->component_size % (info->new_chunk/512)) | |
983 | return "New chunk size does not" | |
984 | " divide component size"; | |
985 | break; | |
986 | default: | |
987 | return "chunk size not meaningful for this level"; | |
988 | } | |
989 | else | |
990 | info->new_chunk = info->array.chunk_size; | |
991 | ||
992 | switch (info->array.level) { | |
90b60dfa N |
993 | default: |
994 | return "Cannot understand this RAID level"; | |
5da9ab98 N |
995 | case 1: |
996 | /* RAID1 can convert to RAID1 with different disks, or | |
dfe77a9e KW |
997 | * raid5 with 2 disks, or |
998 | * raid0 with 1 disk | |
5da9ab98 | 999 | */ |
dfe77a9e | 1000 | if (info->new_level == 0) { |
b545e14a N |
1001 | if (info->delta_disks != UnSet && |
1002 | info->delta_disks != 0) | |
1003 | return "Cannot change number of disks " | |
1004 | "with RAID1->RAID0 conversion"; | |
dfe77a9e KW |
1005 | re->level = 0; |
1006 | re->before.data_disks = 1; | |
1007 | re->after.data_disks = 1; | |
1008 | re->before.layout = 0; | |
1009 | re->backup_blocks = 0; | |
1010 | re->parity = 0; | |
1011 | return NULL; | |
1012 | } | |
5da9ab98 N |
1013 | if (info->new_level == 1) { |
1014 | if (info->delta_disks == UnSet) | |
1015 | /* Don't know what to do */ | |
1016 | return "no change requested for Growing RAID1"; | |
1017 | re->level = 1; | |
b4f8e38b | 1018 | re->backup_blocks = 0; |
5da9ab98 N |
1019 | re->parity = 0; |
1020 | return NULL; | |
1021 | } | |
1022 | if (info->array.raid_disks == 2 && | |
446d2a5a | 1023 | info->new_level == 5) { |
5652f2d9 | 1024 | |
5da9ab98 | 1025 | re->level = 5; |
5da9ab98 | 1026 | re->before.data_disks = 1; |
5652f2d9 N |
1027 | if (info->delta_disks != UnSet && |
1028 | info->delta_disks != 0) | |
1029 | re->after.data_disks = 1 + info->delta_disks; | |
1030 | else | |
1031 | re->after.data_disks = 1; | |
1032 | if (re->after.data_disks < 1) | |
1033 | return "Number of disks too small for RAID5"; | |
1034 | ||
5da9ab98 | 1035 | re->before.layout = ALGORITHM_LEFT_SYMMETRIC; |
446d2a5a N |
1036 | info->array.chunk_size = 65536; |
1037 | break; | |
5da9ab98 N |
1038 | } |
1039 | /* Could do some multi-stage conversions, but leave that to | |
1040 | * later. | |
1041 | */ | |
1042 | return "Impossibly level change request for RAID1"; | |
1043 | ||
1044 | case 10: | |
1045 | /* RAID10 can only be converted from near mode to | |
1046 | * RAID0 by removing some devices | |
1047 | */ | |
1048 | if ((info->array.layout & ~0xff) != 0x100) | |
1049 | return "Cannot Grow RAID10 with far/offset layout"; | |
1050 | /* number of devices must be multiple of number of copies */ | |
1051 | if (info->array.raid_disks % (info->array.layout & 0xff)) | |
1052 | return "RAID10 layout too complex for Grow operation"; | |
1053 | ||
1054 | if (info->new_level != 0) | |
1055 | return "RAID10 can only be changed to RAID0"; | |
1056 | new_disks = (info->array.raid_disks | |
1057 | / (info->array.layout & 0xff)); | |
b545e14a | 1058 | if (info->delta_disks == UnSet) |
5da9ab98 N |
1059 | info->delta_disks = (new_disks |
1060 | - info->array.raid_disks); | |
b545e14a | 1061 | |
5da9ab98 N |
1062 | if (info->delta_disks != new_disks - info->array.raid_disks) |
1063 | return "New number of raid-devices impossible for RAID10"; | |
1064 | if (info->new_chunk && | |
1065 | info->new_chunk != info->array.chunk_size) | |
1066 | return "Cannot change chunk-size with RAID10 Grow"; | |
1067 | ||
1068 | /* looks good */ | |
1069 | re->level = 0; | |
1070 | re->parity = 0; | |
1071 | re->before.data_disks = new_disks; | |
031d445c | 1072 | re->after.data_disks = re->before.data_disks; |
5da9ab98 | 1073 | re->before.layout = 0; |
b4f8e38b | 1074 | re->backup_blocks = 0; |
5da9ab98 N |
1075 | return NULL; |
1076 | ||
1077 | case 0: | |
1078 | /* RAID0 can be converted to RAID10, or to RAID456 */ | |
1079 | if (info->new_level == 10) { | |
1080 | if (info->new_layout == UnSet && info->delta_disks == UnSet) { | |
1081 | /* Assume near=2 layout */ | |
1082 | info->new_layout = 0x102; | |
1083 | info->delta_disks = info->array.raid_disks; | |
1084 | } | |
1085 | if (info->new_layout == UnSet) { | |
1086 | int copies = 1 + (info->delta_disks | |
1087 | / info->array.raid_disks); | |
1088 | if (info->array.raid_disks * (copies-1) | |
1089 | != info->delta_disks) | |
1090 | return "Impossible number of devices" | |
1091 | " for RAID0->RAID10"; | |
1092 | info->new_layout = 0x100 + copies; | |
1093 | } | |
1094 | if (info->delta_disks == UnSet) { | |
1095 | int copies = info->new_layout & 0xff; | |
1096 | if (info->new_layout != 0x100 + copies) | |
1097 | return "New layout impossible" | |
1098 | " for RAID0->RAID10";; | |
1099 | info->delta_disks = (copies - 1) * | |
1100 | info->array.raid_disks; | |
1101 | } | |
1102 | if (info->new_chunk && | |
1103 | info->new_chunk != info->array.chunk_size) | |
1104 | return "Cannot change chunk-size with RAID0->RAID10"; | |
1105 | /* looks good */ | |
1106 | re->level = 10; | |
1107 | re->parity = 0; | |
1108 | re->before.data_disks = (info->array.raid_disks + | |
1109 | info->delta_disks); | |
031d445c | 1110 | re->after.data_disks = re->before.data_disks; |
5da9ab98 | 1111 | re->before.layout = info->new_layout; |
b4f8e38b | 1112 | re->backup_blocks = 0; |
5da9ab98 N |
1113 | return NULL; |
1114 | } | |
1115 | ||
1116 | /* RAID0 can also covert to RAID0/4/5/6 by first converting to | |
1117 | * a raid4 style layout of the final level. | |
1118 | */ | |
1119 | switch (info->new_level) { | |
5da9ab98 | 1120 | case 4: |
b545e14a N |
1121 | delta_parity = 1; |
1122 | case 0: | |
5da9ab98 N |
1123 | re->level = 4; |
1124 | re->before.layout = 0; | |
1125 | break; | |
1126 | case 5: | |
b545e14a | 1127 | delta_parity = 1; |
5da9ab98 N |
1128 | re->level = 5; |
1129 | re->before.layout = ALGORITHM_PARITY_N; | |
1130 | break; | |
1131 | case 6: | |
b545e14a | 1132 | delta_parity = 2; |
5da9ab98 N |
1133 | re->level = 6; |
1134 | re->before.layout = ALGORITHM_PARITY_N; | |
1135 | break; | |
1136 | default: | |
1137 | return "Impossible level change requested"; | |
1138 | } | |
1139 | re->before.data_disks = info->array.raid_disks; | |
1140 | /* determining 'after' layout happens outside this 'switch' */ | |
1141 | break; | |
1142 | ||
1143 | case 4: | |
1144 | info->array.layout = ALGORITHM_PARITY_N; | |
1145 | case 5: | |
1146 | switch (info->new_level) { | |
f0cce442 | 1147 | case 0: |
b545e14a | 1148 | delta_parity = -1; |
5da9ab98 N |
1149 | case 4: |
1150 | re->level = info->array.level; | |
1151 | re->before.data_disks = info->array.raid_disks - 1; | |
1152 | re->before.layout = info->array.layout; | |
1153 | break; | |
1154 | case 5: | |
1155 | re->level = 5; | |
1156 | re->before.data_disks = info->array.raid_disks - 1; | |
1157 | re->before.layout = info->array.layout; | |
1158 | break; | |
1159 | case 6: | |
b545e14a | 1160 | delta_parity = 1; |
5da9ab98 N |
1161 | re->level = 6; |
1162 | re->before.data_disks = info->array.raid_disks - 1; | |
1163 | switch (info->array.layout) { | |
1164 | case ALGORITHM_LEFT_ASYMMETRIC: | |
1165 | re->before.layout = ALGORITHM_LEFT_ASYMMETRIC_6; | |
1166 | break; | |
1167 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
1168 | re->before.layout = ALGORITHM_RIGHT_ASYMMETRIC_6; | |
1169 | break; | |
1170 | case ALGORITHM_LEFT_SYMMETRIC: | |
1171 | re->before.layout = ALGORITHM_LEFT_SYMMETRIC_6; | |
1172 | break; | |
1173 | case ALGORITHM_RIGHT_SYMMETRIC: | |
1174 | re->before.layout = ALGORITHM_RIGHT_SYMMETRIC_6; | |
1175 | break; | |
1176 | case ALGORITHM_PARITY_0: | |
1177 | re->before.layout = ALGORITHM_PARITY_0_6; | |
1178 | break; | |
1179 | case ALGORITHM_PARITY_N: | |
1180 | re->before.layout = ALGORITHM_PARITY_N_6; | |
1181 | break; | |
1182 | default: | |
1183 | return "Cannot convert an array with this layout"; | |
1184 | } | |
1185 | break; | |
1186 | case 1: | |
1187 | if (info->array.raid_disks != 2) | |
1188 | return "Can only convert a 2-device array to RAID1"; | |
b545e14a N |
1189 | if (info->delta_disks != UnSet && |
1190 | info->delta_disks != 0) | |
1191 | return "Cannot set raid_disk when " | |
1192 | "converting RAID5->RAID1"; | |
5da9ab98 | 1193 | re->level = 1; |
5da9ab98 N |
1194 | break; |
1195 | default: | |
1196 | return "Impossible level change requested"; | |
1197 | } | |
1198 | break; | |
1199 | case 6: | |
1200 | switch (info->new_level) { | |
1201 | case 4: | |
1202 | case 5: | |
b545e14a | 1203 | delta_parity = -1; |
5da9ab98 N |
1204 | case 6: |
1205 | re->level = 6; | |
1206 | re->before.data_disks = info->array.raid_disks - 2; | |
1207 | re->before.layout = info->array.layout; | |
1208 | break; | |
1209 | default: | |
1210 | return "Impossible level change requested"; | |
1211 | } | |
1212 | break; | |
1213 | } | |
1214 | ||
1215 | /* If we reached here then it looks like a re-stripe is | |
1216 | * happening. We have determined the intermediate level | |
1217 | * and initial raid_disks/layout and stored these in 're'. | |
1218 | * | |
1219 | * We need to deduce the final layout that can be atomically | |
1220 | * converted to the end state. | |
1221 | */ | |
1222 | switch (info->new_level) { | |
1223 | case 0: | |
1224 | /* We can only get to RAID0 from RAID4 or RAID5 | |
1225 | * with appropriate layout and one extra device | |
1226 | */ | |
1227 | if (re->level != 4 && re->level != 5) | |
1228 | return "Cannot covert to RAID0 from this level"; | |
b545e14a | 1229 | |
5da9ab98 N |
1230 | switch (re->level) { |
1231 | case 4: | |
1232 | re->after.layout = 0 ; break; | |
1233 | case 5: | |
1234 | re->after.layout = ALGORITHM_PARITY_N; break; | |
1235 | } | |
1236 | break; | |
1237 | ||
1238 | case 4: | |
1239 | /* We can only get to RAID4 from RAID5 */ | |
1240 | if (re->level != 4 && re->level != 5) | |
1241 | return "Cannot convert to RAID4 from this level"; | |
b545e14a | 1242 | |
5da9ab98 N |
1243 | switch (re->level) { |
1244 | case 4: | |
1245 | re->after.layout = 0 ; break; | |
1246 | case 5: | |
1247 | re->after.layout = ALGORITHM_PARITY_N; break; | |
1248 | } | |
1249 | break; | |
1250 | ||
1251 | case 5: | |
1252 | /* We get to RAID5 for RAID5 or RAID6 */ | |
1253 | if (re->level != 5 && re->level != 6) | |
1254 | return "Cannot convert to RAID5 from this level"; | |
b545e14a | 1255 | |
5da9ab98 N |
1256 | switch (re->level) { |
1257 | case 5: | |
1258 | if (info->new_layout == UnSet) | |
1259 | re->after.layout = re->before.layout; | |
1260 | else | |
1261 | re->after.layout = info->new_layout; | |
1262 | break; | |
1263 | case 6: | |
4a870364 N |
1264 | if (info->new_layout == UnSet) |
1265 | info->new_layout = re->before.layout; | |
1266 | ||
5da9ab98 N |
1267 | /* after.layout needs to be raid6 version of new_layout */ |
1268 | if (info->new_layout == ALGORITHM_PARITY_N) | |
1269 | re->after.layout = ALGORITHM_PARITY_N; | |
1270 | else { | |
1271 | char layout[40]; | |
1272 | char *ls = map_num(r5layout, info->new_layout); | |
1273 | int l; | |
1274 | strcat(strcpy(layout, ls), "-6"); | |
1275 | l = map_name(r6layout, layout); | |
1276 | if (l == UnSet) | |
1277 | return "Cannot find RAID6 layout" | |
1278 | " to convert to"; | |
1279 | re->after.layout = l; | |
1280 | } | |
1281 | } | |
1282 | break; | |
1283 | ||
1284 | case 6: | |
1285 | /* We must already be at level 6 */ | |
1286 | if (re->level != 6) | |
1287 | return "Impossible level change"; | |
5da9ab98 | 1288 | if (info->new_layout == UnSet) |
4a870364 | 1289 | re->after.layout = info->array.layout; |
5da9ab98 N |
1290 | else |
1291 | re->after.layout = info->new_layout; | |
1292 | break; | |
1293 | default: | |
1294 | return "Impossible level change requested"; | |
1295 | } | |
b545e14a N |
1296 | if (info->delta_disks == UnSet) |
1297 | info->delta_disks = delta_parity; | |
1298 | ||
1299 | re->after.data_disks = (re->before.data_disks | |
1300 | + info->delta_disks | |
1301 | - delta_parity); | |
5da9ab98 N |
1302 | switch (re->level) { |
1303 | case 6: re->parity = 2; break; | |
1304 | case 4: | |
1305 | case 5: re->parity = 1; break; | |
1306 | default: re->parity = 0; break; | |
1307 | } | |
1308 | /* So we have a restripe operation, we need to calculate the number | |
1309 | * of blocks per reshape operation. | |
1310 | */ | |
1311 | if (info->new_chunk == 0) | |
1312 | info->new_chunk = info->array.chunk_size; | |
1313 | if (re->after.data_disks == re->before.data_disks && | |
1314 | re->after.layout == re->before.layout && | |
1315 | info->new_chunk == info->array.chunk_size) { | |
1316 | /* Nothing to change */ | |
b4f8e38b | 1317 | re->backup_blocks = 0; |
5da9ab98 N |
1318 | return NULL; |
1319 | } | |
1320 | if (re->after.data_disks == 1 && re->before.data_disks == 1) { | |
446d2a5a | 1321 | /* chunk and layout changes make no difference */ |
b4f8e38b | 1322 | re->backup_blocks = 0; |
5da9ab98 N |
1323 | return NULL; |
1324 | } | |
1325 | ||
1326 | if (re->after.data_disks == re->before.data_disks && | |
1327 | get_linux_version() < 2006032) | |
1328 | return "in-place reshape is not safe before 2.6.32 - sorry."; | |
1329 | ||
1330 | if (re->after.data_disks < re->before.data_disks && | |
1331 | get_linux_version() < 2006030) | |
508ede86 | 1332 | return "reshape to fewer devices is not supported before 2.6.30 - sorry."; |
5da9ab98 | 1333 | |
b4f8e38b | 1334 | re->backup_blocks = compute_backup_blocks( |
5da9ab98 N |
1335 | info->new_chunk, info->array.chunk_size, |
1336 | re->after.data_disks, | |
1337 | re->before.data_disks); | |
1338 | ||
1339 | re->new_size = info->component_size * re->after.data_disks; | |
1340 | return NULL; | |
1341 | } | |
1342 | ||
1343 | static int reshape_array(char *container, int fd, char *devname, | |
1344 | struct supertype *st, struct mdinfo *info, | |
e2e53a2d N |
1345 | int force, struct mddev_dev *devlist, |
1346 | char *backup_file, int quiet, int forked, | |
a93f87ee | 1347 | int restart); |
493f5dd6 | 1348 | static int reshape_container(char *container, char *devname, |
5da9ab98 N |
1349 | struct supertype *st, |
1350 | struct mdinfo *info, | |
1351 | int force, | |
1352 | char *backup_file, | |
493f5dd6 | 1353 | int quiet, int restart); |
1c009fc2 | 1354 | |
06b0d786 | 1355 | int Grow_reshape(char *devname, int fd, int quiet, char *backup_file, |
e86c9dd6 | 1356 | long long size, |
691a36b7 | 1357 | int level, char *layout_str, int chunksize, int raid_disks, |
e2e53a2d | 1358 | struct mddev_dev *devlist, |
ce52f92f | 1359 | int assume_clean, int force) |
e86c9dd6 NB |
1360 | { |
1361 | /* Make some changes in the shape of an array. | |
1362 | * The kernel must support the change. | |
7236ee7a N |
1363 | * |
1364 | * There are three different changes. Each can trigger | |
1365 | * a resync or recovery so we freeze that until we have | |
1366 | * requested everything (if kernel supports freezing - 2.6.30). | |
1367 | * The steps are: | |
1368 | * - change size (i.e. component_size) | |
1369 | * - change level | |
1370 | * - change layout/chunksize/ndisks | |
1371 | * | |
1372 | * The last can require a reshape. It is different on different | |
1373 | * levels so we need to check the level before actioning it. | |
1374 | * Some times the level change needs to be requested after the | |
1375 | * reshape (e.g. raid6->raid5, raid5->raid0) | |
1376 | * | |
e86c9dd6 | 1377 | */ |
5da9ab98 | 1378 | struct mdu_array_info_s array; |
7236ee7a | 1379 | int rv = 0; |
e86c9dd6 | 1380 | struct supertype *st; |
4725bc31 | 1381 | char *subarray = NULL; |
e86c9dd6 | 1382 | |
7236ee7a | 1383 | int frozen; |
7236ee7a | 1384 | int changed = 0; |
7bc71196 | 1385 | char *container = NULL; |
5da9ab98 | 1386 | char container_buf[20]; |
7bc71196 | 1387 | int cfd = -1; |
e86c9dd6 | 1388 | |
e2e53a2d N |
1389 | struct mddev_dev *dv; |
1390 | int added_disks; | |
1391 | ||
5da9ab98 | 1392 | struct mdinfo info; |
7e0f6979 | 1393 | struct mdinfo *sra; |
e86c9dd6 NB |
1394 | |
1395 | if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) { | |
1396 | fprintf(stderr, Name ": %s is not an active md array - aborting\n", | |
1397 | devname); | |
1398 | return 1; | |
1399 | } | |
24d40069 | 1400 | |
9ce510be N |
1401 | if (size >= 0 && |
1402 | (chunksize || level!= UnSet || layout_str || raid_disks)) { | |
1403 | fprintf(stderr, Name ": cannot change component size at the same time " | |
1404 | "as other changes.\n" | |
1405 | " Change size first, then check data is intact before " | |
1406 | "making other changes.\n"); | |
1407 | return 1; | |
1408 | } | |
1409 | ||
24d40069 N |
1410 | if (raid_disks && raid_disks < array.raid_disks && array.level > 1 && |
1411 | get_linux_version() < 2006032 && | |
1412 | !check_env("MDADM_FORCE_FEWER")) { | |
1413 | fprintf(stderr, Name ": reducing the number of devices is not safe before Linux 2.6.32\n" | |
1414 | " Please use a newer kernel\n"); | |
1415 | return 1; | |
1416 | } | |
7bc71196 DW |
1417 | |
1418 | st = super_by_fd(fd, &subarray); | |
1419 | if (!st) { | |
1420 | fprintf(stderr, Name ": Unable to determine metadata format for %s\n", devname); | |
1421 | return 1; | |
1422 | } | |
acab7bb1 N |
1423 | if (raid_disks > st->max_devs) { |
1424 | fprintf(stderr, Name ": Cannot increase raid-disks on this array" | |
1425 | " beyond %d\n", st->max_devs); | |
1426 | return 1; | |
1427 | } | |
7bc71196 DW |
1428 | |
1429 | /* in the external case we need to check that the requested reshape is | |
1430 | * supported, and perform an initial check that the container holds the | |
1431 | * pre-requisite spare devices (mdmon owns final validation) | |
1432 | */ | |
1433 | if (st->ss->external) { | |
1434 | int container_dev; | |
7f2ba464 | 1435 | int rv; |
7bc71196 DW |
1436 | |
1437 | if (subarray) { | |
1438 | container_dev = st->container_dev; | |
1439 | cfd = open_dev_excl(st->container_dev); | |
7bc71196 DW |
1440 | } else { |
1441 | container_dev = st->devnum; | |
1442 | close(fd); | |
1443 | cfd = open_dev_excl(st->devnum); | |
1444 | fd = cfd; | |
1445 | } | |
1446 | if (cfd < 0) { | |
1447 | fprintf(stderr, Name ": Unable to open container for %s\n", | |
1448 | devname); | |
7f2ba464 | 1449 | free(subarray); |
7bc71196 DW |
1450 | return 1; |
1451 | } | |
1452 | ||
5da9ab98 N |
1453 | fmt_devname(container_buf, container_dev); |
1454 | container = container_buf; | |
7bc71196 | 1455 | |
eb744788 AK |
1456 | rv = st->ss->load_container(st, cfd, NULL); |
1457 | ||
7f2ba464 | 1458 | if (rv) { |
7bc71196 DW |
1459 | fprintf(stderr, Name ": Cannot read superblock for %s\n", |
1460 | devname); | |
7f2ba464 | 1461 | free(subarray); |
7bc71196 DW |
1462 | return 1; |
1463 | } | |
1464 | ||
1465 | if (mdmon_running(container_dev)) | |
1466 | st->update_tail = &st->updates; | |
e2e53a2d | 1467 | } |
691a36b7 | 1468 | |
e2e53a2d N |
1469 | added_disks = 0; |
1470 | for (dv = devlist; dv; dv = dv->next) | |
1471 | added_disks++; | |
691a36b7 | 1472 | if (raid_disks > array.raid_disks && |
e2e53a2d | 1473 | array.spare_disks +added_disks < (raid_disks - array.raid_disks) && |
691a36b7 N |
1474 | !force) { |
1475 | fprintf(stderr, | |
1476 | Name ": Need %d spare%s to avoid degraded array," | |
1477 | " and only have %d.\n" | |
1478 | " Use --force to over-ride this check.\n", | |
1479 | raid_disks - array.raid_disks, | |
1480 | raid_disks - array.raid_disks == 1 ? "" : "s", | |
e2e53a2d | 1481 | array.spare_disks + added_disks); |
691a36b7 | 1482 | return 1; |
7bc71196 DW |
1483 | } |
1484 | ||
3fa436ac ML |
1485 | sra = sysfs_read(fd, 0, GET_LEVEL | GET_DISKS | GET_DEVS |
1486 | | GET_STATE | GET_VERSION); | |
1487 | if (sra) { | |
7f2ba464 | 1488 | if (st->ss->external && subarray == NULL) { |
7bc71196 DW |
1489 | array.level = LEVEL_CONTAINER; |
1490 | sra->array.level = LEVEL_CONTAINER; | |
1491 | } | |
7bc71196 | 1492 | } else { |
b526e52d DW |
1493 | fprintf(stderr, Name ": failed to read sysfs parameters for %s\n", |
1494 | devname); | |
1495 | return 1; | |
1496 | } | |
7f2ba464 DW |
1497 | frozen = freeze(st); |
1498 | if (frozen < -1) { | |
1499 | /* freeze() already spewed the reason */ | |
1500 | return 1; | |
1501 | } else if (frozen < 0) { | |
7236ee7a N |
1502 | fprintf(stderr, Name ": %s is performing resync/recovery and cannot" |
1503 | " be reshaped\n", devname); | |
1504 | return 1; | |
1505 | } | |
1506 | ||
1507 | /* ========= set size =============== */ | |
1508 | if (size >= 0 && (size == 0 || size != array.size)) { | |
85f10287 | 1509 | long long orig_size = get_component_size(fd)/2; |
20a46756 | 1510 | long long min_csize; |
d1537ed1 | 1511 | struct mdinfo *mdi; |
7bc71196 | 1512 | |
85f10287 N |
1513 | if (orig_size == 0) |
1514 | orig_size = array.size; | |
1515 | ||
41784c88 AK |
1516 | if (reshape_super(st, size, UnSet, UnSet, 0, 0, UnSet, NULL, |
1517 | devname, !quiet)) { | |
7bc71196 DW |
1518 | rv = 1; |
1519 | goto release; | |
1520 | } | |
1521 | sync_metadata(st); | |
d1537ed1 N |
1522 | |
1523 | /* Update the size of each member device in case | |
1524 | * they have been resized. This will never reduce | |
1525 | * below the current used-size. The "size" attribute | |
20a46756 | 1526 | * understands '0' to mean 'max'. |
d1537ed1 | 1527 | */ |
20a46756 N |
1528 | min_csize = 0; |
1529 | for (mdi = sra->devs; mdi; mdi = mdi->next) { | |
1530 | if (sysfs_set_num(sra, mdi, "size", size) < 0) | |
1531 | break; | |
1532 | if (array.not_persistent == 0 && | |
1533 | array.major_version == 0 && | |
1534 | get_linux_version() < 3001000) { | |
1535 | /* Dangerous to allow size to exceed 2TB */ | |
1536 | unsigned long long csize; | |
1537 | if (sysfs_get_ll(sra, mdi, "size", &csize) == 0) { | |
1538 | if (csize >= 2ULL*1024*1024*1024) | |
1539 | csize = 2ULL*1024*1024*1024; | |
1540 | if ((min_csize == 0 || (min_csize | |
1541 | > (long long)csize))) | |
1542 | min_csize = csize; | |
1543 | } | |
1544 | } | |
1545 | } | |
1546 | if (min_csize && size > min_csize) { | |
1547 | fprintf(stderr, Name ": Cannot safely make this array " | |
1548 | "use more than 2TB per device on this kernel.\n"); | |
1549 | rv = 1; | |
1550 | goto release; | |
1551 | } | |
1552 | if (min_csize && size == 0) { | |
1553 | /* Don't let the kernel choose a size - it will get | |
1554 | * it wrong | |
1555 | */ | |
1556 | fprintf(stderr, Name ": Limited v0.90 array to " | |
1557 | "2TB per device\n"); | |
1558 | size = min_csize; | |
1559 | } | |
d1537ed1 | 1560 | |
7236ee7a N |
1561 | array.size = size; |
1562 | if (array.size != size) { | |
1563 | /* got truncated to 32bit, write to | |
1564 | * component_size instead | |
1565 | */ | |
1566 | if (sra) | |
1567 | rv = sysfs_set_num(sra, NULL, | |
1568 | "component_size", size); | |
1569 | else | |
1570 | rv = -1; | |
1571 | } else | |
1572 | rv = ioctl(fd, SET_ARRAY_INFO, &array); | |
1573 | if (rv != 0) { | |
39bbb392 | 1574 | int err = errno; |
7bc71196 DW |
1575 | |
1576 | /* restore metadata */ | |
1577 | if (reshape_super(st, orig_size, UnSet, UnSet, 0, 0, | |
41784c88 | 1578 | UnSet, NULL, devname, !quiet) == 0) |
7bc71196 | 1579 | sync_metadata(st); |
7236ee7a | 1580 | fprintf(stderr, Name ": Cannot set device size for %s: %s\n", |
39bbb392 N |
1581 | devname, strerror(err)); |
1582 | if (err == EBUSY && | |
1583 | (array.state & (1<<MD_SB_BITMAP_PRESENT))) | |
1584 | fprintf(stderr, " Bitmap must be removed before size can be changed\n"); | |
7236ee7a N |
1585 | rv = 1; |
1586 | goto release; | |
1587 | } | |
ce52f92f | 1588 | if (assume_clean) { |
6cbf8fb8 | 1589 | /* This will fail on kernels newer than 3.0 unless |
ce52f92f N |
1590 | * a backport has been arranged. |
1591 | */ | |
1592 | if (sra == NULL || | |
1593 | sysfs_set_str(sra, NULL, "resync_start", "none") < 0) | |
1594 | fprintf(stderr, Name ": --assume-clean not support with --grow on this kernel\n"); | |
1595 | } | |
7236ee7a | 1596 | ioctl(fd, GET_ARRAY_INFO, &array); |
be1cabbd | 1597 | size = get_component_size(fd)/2; |
f98841b3 N |
1598 | if (size == 0) |
1599 | size = array.size; | |
85f10287 N |
1600 | if (!quiet) { |
1601 | if (size == orig_size) | |
1602 | fprintf(stderr, Name ": component size of %s " | |
1603 | "unchanged at %lluK\n", | |
1604 | devname, size); | |
1605 | else | |
1606 | fprintf(stderr, Name ": component size of %s " | |
1607 | "has been set to %lluK\n", | |
1608 | devname, size); | |
1609 | } | |
7236ee7a | 1610 | changed = 1; |
7bc71196 | 1611 | } else if (array.level != LEVEL_CONTAINER) { |
be1cabbd | 1612 | size = get_component_size(fd)/2; |
f98841b3 N |
1613 | if (size == 0) |
1614 | size = array.size; | |
7236ee7a N |
1615 | } |
1616 | ||
d515d27f N |
1617 | /* See if there is anything else to do */ |
1618 | if ((level == UnSet || level == array.level) && | |
1619 | (layout_str == NULL) && | |
1620 | (chunksize == 0 || chunksize == array.chunk_size) && | |
1621 | (raid_disks == 0 || raid_disks == array.raid_disks)) { | |
1622 | /* Nothing more to do */ | |
1623 | if (!changed && !quiet) | |
1624 | fprintf(stderr, Name ": %s: no change requested\n", | |
1625 | devname); | |
1626 | goto release; | |
1627 | } | |
1628 | ||
dfe77a9e | 1629 | /* ========= check for Raid10/Raid1 -> Raid0 conversion =============== |
5da9ab98 | 1630 | * current implementation assumes that following conditions must be met: |
dfe77a9e KW |
1631 | * - RAID10: |
1632 | * - far_copies == 1 | |
1633 | * - near_copies == 2 | |
62a48395 | 1634 | */ |
dfe77a9e KW |
1635 | if ((level == 0 && array.level == 10 && sra && |
1636 | array.layout == ((1 << 8) + 2) && !(array.raid_disks & 1)) || | |
1637 | (level == 0 && array.level == 1 && sra)) { | |
62a48395 | 1638 | int err; |
dfe77a9e | 1639 | err = remove_disks_for_takeover(st, sra, array.layout); |
62a48395 AK |
1640 | if (err) { |
1641 | dprintf(Name": Array cannot be reshaped\n"); | |
62a48395 AK |
1642 | if (cfd > -1) |
1643 | close(cfd); | |
5da9ab98 N |
1644 | rv = 1; |
1645 | goto release; | |
7236ee7a | 1646 | } |
bb025c2f KW |
1647 | /* FIXME this is added with no justification - why is it here */ |
1648 | ping_monitor(container); | |
7236ee7a N |
1649 | } |
1650 | ||
8ff6d094 | 1651 | memset(&info, 0, sizeof(info)); |
5da9ab98 | 1652 | info.array = array; |
7443ee81 | 1653 | sysfs_init(&info, fd, NoMdDev); |
3fa436ac | 1654 | strcpy(info.text_version, sra->text_version); |
7443ee81 | 1655 | info.component_size = size*2; |
5da9ab98 N |
1656 | info.new_level = level; |
1657 | info.new_chunk = chunksize * 1024; | |
eff4954d N |
1658 | if (info.array.level == LEVEL_CONTAINER) { |
1659 | info.delta_disks = UnSet; | |
1660 | info.array.raid_disks = raid_disks; | |
1661 | } else if (raid_disks) | |
5da9ab98 N |
1662 | info.delta_disks = raid_disks - info.array.raid_disks; |
1663 | else | |
1664 | info.delta_disks = UnSet; | |
1665 | if (layout_str == NULL) { | |
1666 | info.new_layout = UnSet; | |
1667 | if (info.array.level == 6 && | |
1668 | (info.new_level == 6 || info.new_level == UnSet) && | |
1669 | info.array.layout >= 16) { | |
7236ee7a | 1670 | fprintf(stderr, Name |
5da9ab98 N |
1671 | ": %s has a non-standard layout. If you" |
1672 | " wish to preserve this\n" | |
1673 | " during the reshape, please specify" | |
1674 | " --layout=preserve\n" | |
1675 | " If you want to change it, specify a" | |
1676 | " layout or use --layout=normalise\n", | |
7236ee7a N |
1677 | devname); |
1678 | rv = 1; | |
1679 | goto release; | |
1680 | } | |
5da9ab98 N |
1681 | } else if (strcmp(layout_str, "normalise") == 0 || |
1682 | strcmp(layout_str, "normalize") == 0) { | |
1683 | /* If we have a -6 RAID6 layout, remove the '-6'. */ | |
1684 | info.new_layout = UnSet; | |
1685 | if (info.array.level == 6 && info.new_level == UnSet) { | |
1686 | char l[40], *h; | |
1687 | strcpy(l, map_num(r6layout, info.array.layout)); | |
1688 | h = strrchr(l, '-'); | |
1689 | if (h && strcmp(h, "-6") == 0) { | |
1690 | *h = 0; | |
1691 | info.new_layout = map_name(r6layout, l); | |
7236ee7a N |
1692 | } |
1693 | } | |
5da9ab98 N |
1694 | } else if (strcmp(layout_str, "preserve") == 0) { |
1695 | info.new_layout = UnSet; | |
1696 | } else { | |
1697 | int l = info.new_level; | |
1698 | if (l == UnSet) | |
1699 | l = info.array.level; | |
1700 | switch (l) { | |
1701 | case 5: | |
1702 | info.new_layout = map_name(r5layout, layout_str); | |
1703 | break; | |
1704 | case 6: | |
1705 | info.new_layout = map_name(r6layout, layout_str); | |
1706 | break; | |
1707 | case 10: | |
1708 | info.new_layout = parse_layout_10(layout_str); | |
1709 | break; | |
1710 | case LEVEL_FAULTY: | |
1711 | info.new_layout = parse_layout_faulty(layout_str); | |
1712 | break; | |
1713 | default: | |
1714 | fprintf(stderr, Name ": layout not meaningful" | |
1715 | " with this level\n"); | |
7bc71196 DW |
1716 | rv = 1; |
1717 | goto release; | |
1718 | } | |
5da9ab98 N |
1719 | if (info.new_layout == UnSet) { |
1720 | fprintf(stderr, Name ": layout %s not understood" | |
1721 | " for this level\n", | |
1722 | layout_str); | |
1723 | rv = 1; | |
1724 | goto release; | |
7bc71196 | 1725 | } |
7236ee7a N |
1726 | } |
1727 | ||
907ea753 N |
1728 | if (array.level == LEVEL_FAULTY) { |
1729 | if (level != UnSet && level != array.level) { | |
1730 | fprintf(stderr, Name ": cannot change level of Faulty device\n"); | |
1731 | rv =1 ; | |
1732 | } | |
1733 | if (chunksize) { | |
1734 | fprintf(stderr, Name ": cannot set chunksize of Faulty device\n"); | |
1735 | rv =1 ; | |
1736 | } | |
1737 | if (raid_disks && raid_disks != 1) { | |
1738 | fprintf(stderr, Name ": cannot set raid_disks of Faulty device\n"); | |
1739 | rv =1 ; | |
1740 | } | |
1741 | if (layout_str) { | |
1742 | if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) { | |
1743 | dprintf("Cannot get array information.\n"); | |
1744 | goto release; | |
1745 | } | |
1746 | array.layout = info.new_layout; | |
1747 | if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) { | |
1748 | fprintf(stderr, Name ": failed to set new layout\n"); | |
1749 | rv = 1; | |
1750 | } else if (!quiet) | |
1751 | printf("layout for %s set to %d\n", | |
1752 | devname, array.layout); | |
1753 | } | |
1754 | } else if (array.level == LEVEL_CONTAINER) { | |
5da9ab98 N |
1755 | /* This change is to be applied to every array in the |
1756 | * container. This is only needed when the metadata imposes | |
1757 | * restraints of the various arrays in the container. | |
1758 | * Currently we only know that IMSM requires all arrays | |
1759 | * to have the same number of devices so changing the | |
1760 | * number of devices (On-Line Capacity Expansion) must be | |
1761 | * performed at the level of the container | |
1762 | */ | |
493f5dd6 N |
1763 | rv = reshape_container(container, devname, st, &info, |
1764 | force, backup_file, quiet, 0); | |
9202b8eb | 1765 | frozen = 0; |
5da9ab98 | 1766 | } else { |
08f9e34b AK |
1767 | /* get spare devices from external metadata |
1768 | */ | |
1769 | if (st->ss->external) { | |
1770 | struct mdinfo *info2; | |
1771 | ||
1772 | info2 = st->ss->container_content(st, subarray); | |
1773 | if (info2) { | |
1774 | info.array.spare_disks = | |
1775 | info2->array.spare_disks; | |
1776 | sysfs_free(info2); | |
1777 | } | |
1778 | } | |
1779 | ||
5da9ab98 N |
1780 | /* Impose these changes on a single array. First |
1781 | * check that the metadata is OK with the change. */ | |
7bc71196 | 1782 | |
5da9ab98 N |
1783 | if (reshape_super(st, info.component_size, info.new_level, |
1784 | info.new_layout, info.new_chunk, | |
41784c88 | 1785 | info.array.raid_disks, info.delta_disks, |
5da9ab98 | 1786 | backup_file, devname, quiet)) { |
7bc71196 DW |
1787 | rv = 1; |
1788 | goto release; | |
1789 | } | |
5da9ab98 N |
1790 | sync_metadata(st); |
1791 | rv = reshape_array(container, fd, devname, st, &info, force, | |
e2e53a2d | 1792 | devlist, backup_file, quiet, 0, 0); |
9202b8eb | 1793 | frozen = 0; |
5da9ab98 N |
1794 | } |
1795 | release: | |
9202b8eb N |
1796 | if (frozen > 0) |
1797 | unfreeze(st); | |
5da9ab98 N |
1798 | return rv; |
1799 | } | |
7bc71196 | 1800 | |
5da9ab98 N |
1801 | static int reshape_array(char *container, int fd, char *devname, |
1802 | struct supertype *st, struct mdinfo *info, | |
e2e53a2d | 1803 | int force, struct mddev_dev *devlist, |
a93f87ee N |
1804 | char *backup_file, int quiet, int forked, |
1805 | int restart) | |
5da9ab98 N |
1806 | { |
1807 | struct reshape reshape; | |
1808 | int spares_needed; | |
1809 | char *msg; | |
1810 | int orig_level = UnSet; | |
1811 | int disks, odisks; | |
7bc71196 | 1812 | |
5da9ab98 N |
1813 | struct mdu_array_info_s array; |
1814 | char *c; | |
e86c9dd6 | 1815 | |
e2e53a2d N |
1816 | struct mddev_dev *dv; |
1817 | int added_disks; | |
1818 | ||
5da9ab98 N |
1819 | int *fdlist; |
1820 | unsigned long long *offsets; | |
1821 | int d; | |
1822 | int nrdisks; | |
1823 | int err; | |
da8100ca | 1824 | unsigned long blocks; |
5da9ab98 N |
1825 | unsigned long cache; |
1826 | unsigned long long array_size; | |
1827 | int done; | |
cf6ac177 | 1828 | struct mdinfo *sra = NULL; |
7bc71196 | 1829 | |
9468aeac N |
1830 | /* when reshaping a RAID0, the component_size might be zero. |
1831 | * So try to fix that up. | |
1832 | */ | |
1833 | if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) { | |
1834 | dprintf("Cannot get array information.\n"); | |
1835 | goto release; | |
1836 | } | |
1837 | if (array.level == 0 && info->component_size == 0) { | |
1838 | get_dev_size(fd, NULL, &array_size); | |
1839 | info->component_size = array_size / array.raid_disks; | |
1840 | } | |
1841 | ||
3cb2aed2 N |
1842 | if (info->reshape_active) { |
1843 | int new_level = info->new_level; | |
1844 | info->new_level = UnSet; | |
178b8f35 | 1845 | info->array.raid_disks -= info->delta_disks; |
3cb2aed2 N |
1846 | msg = analyse_change(info, &reshape); |
1847 | info->new_level = new_level; | |
178b8f35 | 1848 | info->array.raid_disks += info->delta_disks; |
fcdfb814 AK |
1849 | if (!restart) |
1850 | /* Make sure the array isn't read-only */ | |
1851 | ioctl(fd, RESTART_ARRAY_RW, 0); | |
3cb2aed2 N |
1852 | } else |
1853 | msg = analyse_change(info, &reshape); | |
5da9ab98 N |
1854 | if (msg) { |
1855 | fprintf(stderr, Name ": %s\n", msg); | |
631d7405 | 1856 | goto release; |
5da9ab98 | 1857 | } |
817ed7d6 N |
1858 | if (restart && |
1859 | (reshape.level != info->array.level || | |
1860 | reshape.before.layout != info->array.layout || | |
384e9be1 AK |
1861 | reshape.before.data_disks + reshape.parity |
1862 | != info->array.raid_disks - info->delta_disks)) { | |
20a40eca N |
1863 | fprintf(stderr, Name ": reshape info is not in native format -" |
1864 | " cannot continue.\n"); | |
1865 | goto release; | |
1866 | } | |
a93f87ee N |
1867 | |
1868 | if (restart) { | |
1869 | /* reshape already started. just skip to monitoring the reshape */ | |
1870 | if (reshape.backup_blocks == 0) | |
1871 | return 0; | |
1872 | goto started; | |
1873 | } | |
a9c3e78f AK |
1874 | /* The container is frozen but the array may not be. |
1875 | * So freeze the array so spares don't get put to the wrong use | |
1876 | * FIXME there should probably be a cleaner separation between | |
1877 | * freeze_array and freeze_container. | |
1878 | */ | |
1879 | sysfs_freeze_array(info); | |
e06c4e59 | 1880 | /* Check we have enough spares to not be degraded */ |
e2e53a2d N |
1881 | added_disks = 0; |
1882 | for (dv = devlist; dv ; dv=dv->next) | |
1883 | added_disks++; | |
5da9ab98 N |
1884 | spares_needed = max(reshape.before.data_disks, |
1885 | reshape.after.data_disks) | |
1886 | + reshape.parity - array.raid_disks; | |
7bc71196 | 1887 | |
88c1a083 | 1888 | if (!force && |
e2e53a2d N |
1889 | info->new_level > 1 && info->array.level > 1 && |
1890 | spares_needed > info->array.spare_disks + added_disks) { | |
5da9ab98 N |
1891 | fprintf(stderr, |
1892 | Name ": Need %d spare%s to avoid degraded array," | |
1893 | " and only have %d.\n" | |
1894 | " Use --force to over-ride this check.\n", | |
1895 | spares_needed, | |
1896 | spares_needed == 1 ? "" : "s", | |
e2e53a2d | 1897 | info->array.spare_disks + added_disks); |
631d7405 | 1898 | goto release; |
7bc71196 | 1899 | } |
e06c4e59 N |
1900 | /* Check we have enough spares to not fail */ |
1901 | spares_needed = max(reshape.before.data_disks, | |
1902 | reshape.after.data_disks) | |
1903 | - array.raid_disks; | |
1904 | if ((info->new_level > 1 || info->new_level == 0) && | |
e2e53a2d | 1905 | spares_needed > info->array.spare_disks +added_disks) { |
e06c4e59 N |
1906 | fprintf(stderr, |
1907 | Name ": Need %d spare%s to create working array," | |
1908 | " and only have %d.\n", | |
1909 | spares_needed, | |
1910 | spares_needed == 1 ? "" : "s", | |
e2e53a2d | 1911 | info->array.spare_disks + added_disks); |
e06c4e59 N |
1912 | goto release; |
1913 | } | |
e86c9dd6 | 1914 | |
eae35f5c | 1915 | if (reshape.level != array.level) { |
5da9ab98 N |
1916 | char *c = map_num(pers, reshape.level); |
1917 | int err; | |
1918 | if (c == NULL) | |
631d7405 | 1919 | goto release; |
e86c9dd6 | 1920 | |
5da9ab98 N |
1921 | err = sysfs_set_str(info, NULL, "level", c); |
1922 | if (err) { | |
1923 | err = errno; | |
1924 | fprintf(stderr, Name ": %s: could not set level to %s\n", | |
1925 | devname, c); | |
1926 | if (err == EBUSY && | |
1927 | (info->array.state & (1<<MD_SB_BITMAP_PRESENT))) | |
1928 | fprintf(stderr, " Bitmap must be removed" | |
1929 | " before level can be changed\n"); | |
631d7405 | 1930 | goto release; |
19678e53 | 1931 | } |
5da9ab98 | 1932 | if (!quiet) |
b6b95155 | 1933 | fprintf(stderr, Name ": level of %s changed to %s\n", |
5da9ab98 | 1934 | devname, c); |
eae35f5c | 1935 | orig_level = array.level; |
3cd4e7c4 | 1936 | sysfs_freeze_array(info); |
e86c9dd6 | 1937 | |
16d4d84e AK |
1938 | if (reshape.level > 0 && st->ss->external) { |
1939 | /* make sure mdmon is aware of the new level */ | |
1940 | if (!mdmon_running(st->container_dev)) | |
1941 | start_mdmon(st->container_dev); | |
1942 | ping_monitor(container); | |
e919fb0a AK |
1943 | if (mdmon_running(st->container_dev) && |
1944 | st->update_tail == NULL) | |
1945 | st->update_tail = &st->updates; | |
16d4d84e | 1946 | } |
5da9ab98 | 1947 | } |
5da9ab98 N |
1948 | /* ->reshape_super might have chosen some spares from the |
1949 | * container that it wants to be part of the new array. | |
1950 | * We can collect them with ->container_content and give | |
1951 | * them to the kernel. | |
1952 | */ | |
1953 | if (st->ss->reshape_super && st->ss->container_content) { | |
1954 | char *subarray = strchr(info->text_version+1, '/')+1; | |
1955 | struct mdinfo *info2 = | |
1956 | st->ss->container_content(st, subarray); | |
1957 | struct mdinfo *d; | |
1958 | ||
2dd0d697 AK |
1959 | if (info2) { |
1960 | sysfs_init(info2, fd, st->devnum); | |
0d5ac3c6 N |
1961 | /* When increasing number of devices, we need to set |
1962 | * new raid_disks before adding these, or they might | |
1963 | * be rejected. | |
1964 | */ | |
1965 | if (reshape.backup_blocks && | |
1966 | reshape.after.data_disks > reshape.before.data_disks) | |
1967 | subarray_set_num(container, info2, "raid_disks", | |
1968 | reshape.after.data_disks + | |
1969 | reshape.parity); | |
5da9ab98 N |
1970 | for (d = info2->devs; d; d = d->next) { |
1971 | if (d->disk.state == 0 && | |
1972 | d->disk.raid_disk >= 0) { | |
1973 | /* This is a spare that wants to | |
1974 | * be part of the array. | |
1975 | */ | |
1976 | add_disk(fd, st, info2, d); | |
1977 | } | |
9860f271 | 1978 | } |
2dd0d697 AK |
1979 | sysfs_free(info2); |
1980 | } | |
5da9ab98 | 1981 | } |
e2e53a2d N |
1982 | /* We might have been given some devices to add to the |
1983 | * array. Now that the array has been changed to the right | |
1984 | * level and frozen, we can safely add them. | |
1985 | */ | |
1986 | if (devlist) | |
1987 | Manage_subdevs(devname, fd, devlist, !quiet, | |
11b391ec | 1988 | 0,NULL, 0); |
1686dc25 | 1989 | |
b4f8e38b | 1990 | if (reshape.backup_blocks == 0) { |
5da9ab98 N |
1991 | /* No restriping needed, but we might need to impose |
1992 | * some more changes: layout, raid_disks, chunk_size | |
e86c9dd6 | 1993 | */ |
24aebf3a KW |
1994 | /* read current array info */ |
1995 | if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) { | |
83732c28 | 1996 | dprintf("Cannot get array information.\n"); |
24aebf3a KW |
1997 | goto release; |
1998 | } | |
1999 | /* compare current array info with new values and if | |
2000 | * it is different update them to new */ | |
5da9ab98 | 2001 | if (info->new_layout != UnSet && |
24aebf3a KW |
2002 | info->new_layout != array.layout) { |
2003 | array.layout = info->new_layout; | |
2004 | if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) { | |
5da9ab98 | 2005 | fprintf(stderr, Name ": failed to set new layout\n"); |
631d7405 | 2006 | goto release; |
5da9ab98 N |
2007 | } else if (!quiet) |
2008 | printf("layout for %s set to %d\n", | |
24aebf3a | 2009 | devname, array.layout); |
5da9ab98 N |
2010 | } |
2011 | if (info->delta_disks != UnSet && | |
24aebf3a KW |
2012 | info->delta_disks != 0 && |
2013 | array.raid_disks != (info->array.raid_disks + info->delta_disks)) { | |
2014 | array.raid_disks += info->delta_disks; | |
2015 | if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) { | |
5da9ab98 | 2016 | fprintf(stderr, Name ": failed to set raid disks\n"); |
631d7405 | 2017 | goto release; |
24aebf3a | 2018 | } else if (!quiet) { |
5da9ab98 | 2019 | printf("raid_disks for %s set to %d\n", |
24aebf3a KW |
2020 | devname, array.raid_disks); |
2021 | } | |
5da9ab98 N |
2022 | } |
2023 | if (info->new_chunk != 0 && | |
24aebf3a | 2024 | info->new_chunk != array.chunk_size) { |
5da9ab98 N |
2025 | if (sysfs_set_num(info, NULL, |
2026 | "chunk_size", info->new_chunk) != 0) { | |
2027 | fprintf(stderr, Name ": failed to set chunk size\n"); | |
631d7405 | 2028 | goto release; |
5da9ab98 N |
2029 | } else if (!quiet) |
2030 | printf("chunk size for %s set to %d\n", | |
24aebf3a | 2031 | devname, array.chunk_size); |
4725bc31 | 2032 | } |
e35b189b | 2033 | unfreeze(st); |
631d7405 | 2034 | return 0; |
5da9ab98 | 2035 | } |
19678e53 | 2036 | |
5da9ab98 N |
2037 | /* |
2038 | * There are three possibilities. | |
2039 | * 1/ The array will shrink. | |
2040 | * We need to ensure the reshape will pause before reaching | |
2041 | * the 'critical section'. We also need to fork and wait for | |
2042 | * that to happen. When it does we | |
2043 | * suspend/backup/complete/unfreeze | |
2044 | * | |
2045 | * 2/ The array will not change size. | |
2046 | * This requires that we keep a backup of a sliding window | |
2047 | * so that we can restore data after a crash. So we need | |
2048 | * to fork and monitor progress. | |
2049 | * In future we will allow the data_offset to change, so | |
2050 | * a sliding backup becomes unnecessary. | |
2051 | * | |
2052 | * 3/ The array will grow. This is relatively easy. | |
2053 | * However the kernel's restripe routines will cheerfully | |
2054 | * overwrite some early data before it is safe. So we | |
2055 | * need to make a backup of the early parts of the array | |
2056 | * and be ready to restore it if rebuild aborts very early. | |
2057 | * For externally managed metadata, we still need a forked | |
2058 | * child to monitor the reshape and suspend IO over the region | |
2059 | * that is being reshaped. | |
2060 | * | |
2061 | * We backup data by writing it to one spare, or to a | |
2062 | * file which was given on command line. | |
2063 | * | |
2064 | * In each case, we first make sure that storage is available | |
2065 | * for the required backup. | |
2066 | * Then we: | |
2067 | * - request the shape change. | |
2068 | * - fork to handle backup etc. | |
2069 | */ | |
a93f87ee | 2070 | started: |
5da9ab98 N |
2071 | /* Check that we can hold all the data */ |
2072 | get_dev_size(fd, NULL, &array_size); | |
2073 | if (reshape.new_size < (array_size/512)) { | |
2074 | fprintf(stderr, | |
2075 | Name ": this change will reduce the size of the array.\n" | |
2076 | " use --grow --array-size first to truncate array.\n" | |
2077 | " e.g. mdadm --grow %s --array-size %llu\n", | |
2078 | devname, reshape.new_size/2); | |
5da9ab98 N |
2079 | goto release; |
2080 | } | |
7236ee7a | 2081 | |
5da9ab98 | 2082 | sra = sysfs_read(fd, 0, |
3656edcd | 2083 | GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK| |
5da9ab98 | 2084 | GET_CACHE); |
5da9ab98 N |
2085 | if (!sra) { |
2086 | fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n", | |
2087 | devname); | |
5da9ab98 N |
2088 | goto release; |
2089 | } | |
7236ee7a | 2090 | |
5da9ab98 N |
2091 | /* Decide how many blocks (sectors) for a reshape |
2092 | * unit. The number we have so far is just a minimum | |
2093 | */ | |
b4f8e38b | 2094 | blocks = reshape.backup_blocks; |
5da9ab98 N |
2095 | if (reshape.before.data_disks == |
2096 | reshape.after.data_disks) { | |
2097 | /* Make 'blocks' bigger for better throughput, but | |
2098 | * not so big that we reject it below. | |
2099 | * Try for 16 megabytes | |
2100 | */ | |
2101 | while (blocks * 32 < sra->component_size && | |
2102 | blocks < 16*1024*2) | |
2103 | blocks *= 2; | |
2104 | } else | |
2105 | fprintf(stderr, Name ": Need to backup %luK of critical " | |
2106 | "section..\n", blocks/2); | |
2107 | ||
2108 | if (blocks >= sra->component_size/2) { | |
2109 | fprintf(stderr, Name ": %s: Something wrong" | |
2110 | " - reshape aborted\n", | |
2111 | devname); | |
5da9ab98 N |
2112 | goto release; |
2113 | } | |
7236ee7a | 2114 | |
5da9ab98 N |
2115 | /* Now we need to open all these devices so we can read/write. |
2116 | */ | |
b8b286a6 N |
2117 | nrdisks = max(reshape.before.data_disks, |
2118 | reshape.after.data_disks) + reshape.parity | |
2119 | + sra->array.spare_disks; | |
5da9ab98 N |
2120 | fdlist = malloc((1+nrdisks) * sizeof(int)); |
2121 | offsets = malloc((1+nrdisks) * sizeof(offsets[0])); | |
2122 | if (!fdlist || !offsets) { | |
2123 | fprintf(stderr, Name ": malloc failed: grow aborted\n"); | |
5da9ab98 N |
2124 | goto release; |
2125 | } | |
e380d3be | 2126 | |
b8b286a6 N |
2127 | odisks = reshape.before.data_disks + reshape.parity; |
2128 | d = reshape_prepare_fdlist(devname, sra, odisks, | |
5da9ab98 N |
2129 | nrdisks, blocks, backup_file, |
2130 | fdlist, offsets); | |
2131 | if (d < 0) { | |
5da9ab98 N |
2132 | goto release; |
2133 | } | |
13c37ad3 AK |
2134 | if ((st->ss->manage_reshape == NULL) || |
2135 | (st->ss->recover_backup == NULL)) { | |
2136 | if (backup_file == NULL) { | |
2137 | if (reshape.after.data_disks <= | |
2138 | reshape.before.data_disks) { | |
2139 | fprintf(stderr, Name ": %s: Cannot grow - " | |
2140 | "need backup-file\n", devname); | |
2141 | goto release; | |
2142 | } else if (sra->array.spare_disks == 0) { | |
2143 | fprintf(stderr, Name ": %s: Cannot grow - " | |
2144 | "need a spare or backup-file to backup " | |
2145 | "critical section\n", devname); | |
2146 | goto release; | |
2147 | } | |
2148 | } else { | |
2149 | if (!reshape_open_backup_file(backup_file, fd, devname, | |
2150 | (signed)blocks, | |
2151 | fdlist+d, offsets+d, | |
2152 | restart)) { | |
2153 | goto release; | |
2154 | } | |
2155 | d++; | |
e86c9dd6 | 2156 | } |
5da9ab98 | 2157 | } |
7236ee7a | 2158 | |
5da9ab98 N |
2159 | /* lastly, check that the internal stripe cache is |
2160 | * large enough, or it won't work. | |
2161 | * It must hold at least 4 stripes of the larger | |
2162 | * chunk size | |
2163 | */ | |
2164 | cache = max(info->array.chunk_size, info->new_chunk); | |
2165 | cache *= 4; /* 4 stripes minimum */ | |
2166 | cache /= 512; /* convert to sectors */ | |
2167 | disks = min(reshape.before.data_disks, reshape.after.data_disks); | |
2168 | /* make sure there is room for 'blocks' with a bit to spare */ | |
2169 | if (cache < 16 + blocks / disks) | |
2170 | cache = 16 + blocks / disks; | |
2171 | cache /= (4096/512); /* Covert from sectors to pages */ | |
2172 | ||
2173 | if (sra->cache_size < cache) | |
2174 | subarray_set_num(container, sra, "stripe_cache_size", | |
2175 | cache+1); | |
2176 | ||
2177 | /* Right, everything seems fine. Let's kick things off. | |
2178 | * If only changing raid_disks, use ioctl, else use | |
2179 | * sysfs. | |
2180 | */ | |
2181 | sync_metadata(st); | |
2182 | ||
b4f8e38b | 2183 | sra->new_chunk = info->new_chunk; |
ddee071d | 2184 | |
20a40eca | 2185 | if (restart) |
ddee071d | 2186 | sra->reshape_progress = info->reshape_progress; |
f897078e N |
2187 | else { |
2188 | sra->reshape_progress = 0; | |
2189 | if (reshape.after.data_disks < reshape.before.data_disks) | |
2190 | /* start from the end of the new array */ | |
2191 | sra->reshape_progress = (sra->component_size | |
2192 | * reshape.after.data_disks); | |
2193 | } | |
2194 | ||
2195 | if (info->array.chunk_size == info->new_chunk && | |
7477d513 AK |
2196 | reshape.before.layout == reshape.after.layout && |
2197 | st->ss->external == 0) { | |
f897078e | 2198 | /* use SET_ARRAY_INFO but only if reshape hasn't started */ |
6ef421be | 2199 | ioctl(fd, GET_ARRAY_INFO, &array); |
5da9ab98 | 2200 | array.raid_disks = reshape.after.data_disks + reshape.parity; |
20a40eca | 2201 | if (!restart && |
f897078e | 2202 | ioctl(fd, SET_ARRAY_INFO, &array) != 0) { |
5da9ab98 | 2203 | int err = errno; |
631d7405 | 2204 | |
5da9ab98 N |
2205 | fprintf(stderr, |
2206 | Name ": Cannot set device shape for %s: %s\n", | |
2207 | devname, strerror(errno)); | |
7bc71196 | 2208 | |
5da9ab98 N |
2209 | if (err == EBUSY && |
2210 | (array.state & (1<<MD_SB_BITMAP_PRESENT))) | |
2211 | fprintf(stderr, | |
2212 | " Bitmap must be removed before" | |
2213 | " shape can be changed\n"); | |
b5420ef3 | 2214 | |
5da9ab98 N |
2215 | goto release; |
2216 | } | |
20a40eca | 2217 | } else if (!restart) { |
5da9ab98 | 2218 | /* set them all just in case some old 'new_*' value |
f897078e | 2219 | * persists from some earlier problem. |
7236ee7a | 2220 | */ |
631d7405 | 2221 | int err = 0; |
5da9ab98 | 2222 | if (sysfs_set_num(sra, NULL, "chunk_size", info->new_chunk) < 0) |
631d7405 N |
2223 | err = errno; |
2224 | if (!err && sysfs_set_num(sra, NULL, "layout", | |
5da9ab98 | 2225 | reshape.after.layout) < 0) |
631d7405 N |
2226 | err = errno; |
2227 | if (!err && subarray_set_num(container, sra, "raid_disks", | |
5da9ab98 N |
2228 | reshape.after.data_disks + |
2229 | reshape.parity) < 0) | |
631d7405 N |
2230 | err = errno; |
2231 | if (err) { | |
5da9ab98 N |
2232 | fprintf(stderr, Name ": Cannot set device shape for %s\n", |
2233 | devname); | |
7236ee7a | 2234 | |
5da9ab98 N |
2235 | if (err == EBUSY && |
2236 | (array.state & (1<<MD_SB_BITMAP_PRESENT))) | |
2237 | fprintf(stderr, | |
2238 | " Bitmap must be removed before" | |
2239 | " shape can be changed\n"); | |
2240 | goto release; | |
e86c9dd6 | 2241 | } |
5da9ab98 | 2242 | } |
e86c9dd6 | 2243 | |
20a40eca | 2244 | err = start_reshape(sra, restart); |
fab32c97 | 2245 | if (err) { |
20a40eca N |
2246 | fprintf(stderr, |
2247 | Name ": Cannot %s reshape for %s\n", | |
2248 | restart ? "continue" : "start", | |
fab32c97 AK |
2249 | devname); |
2250 | goto release; | |
2251 | } | |
a93f87ee N |
2252 | if (restart) |
2253 | sysfs_set_str(sra, NULL, "array_state", "active"); | |
5da9ab98 N |
2254 | |
2255 | /* Now we just need to kick off the reshape and watch, while | |
2256 | * handling backups of the data... | |
2257 | * This is all done by a forked background process. | |
2258 | */ | |
2259 | switch(forked ? 0 : fork()) { | |
6b2d630c N |
2260 | case -1: |
2261 | fprintf(stderr, Name ": Cannot run child to monitor reshape: %s\n", | |
2262 | strerror(errno)); | |
6b2d630c | 2263 | abort_reshape(sra); |
631d7405 | 2264 | goto release; |
6b2d630c N |
2265 | default: |
2266 | return 0; | |
5da9ab98 | 2267 | case 0: |
6b2d630c N |
2268 | break; |
2269 | } | |
5da9ab98 | 2270 | |
6b2d630c N |
2271 | close(fd); |
2272 | if (check_env("MDADM_GROW_VERIFY")) | |
2273 | fd = open(devname, O_RDONLY | O_DIRECT); | |
2274 | else | |
2275 | fd = -1; | |
2276 | mlockall(MCL_FUTURE); | |
5da9ab98 | 2277 | |
6b2d630c N |
2278 | if (st->ss->external) { |
2279 | /* metadata handler takes it from here */ | |
2280 | done = st->ss->manage_reshape( | |
2281 | fd, sra, &reshape, st, blocks, | |
2282 | fdlist, offsets, | |
2283 | d - odisks, fdlist+odisks, | |
2284 | offsets+odisks); | |
2285 | } else | |
2286 | done = child_monitor( | |
2287 | fd, sra, &reshape, st, blocks, | |
2288 | fdlist, offsets, | |
2289 | d - odisks, fdlist+odisks, | |
2290 | offsets+odisks); | |
2291 | ||
2292 | if (backup_file && done) | |
2293 | unlink(backup_file); | |
2294 | if (!done) { | |
2295 | abort_reshape(sra); | |
2296 | goto out; | |
2297 | } | |
1cbc5680 N |
2298 | |
2299 | if (!st->ss->external && | |
2300 | !(reshape.before.data_disks != reshape.after.data_disks | |
2301 | && info->custom_array_size) && | |
2302 | info->new_level == reshape.level && | |
2303 | !forked) { | |
2304 | /* no need to wait for the reshape to finish as | |
2305 | * there is nothing more to do. | |
2306 | */ | |
2307 | exit(0); | |
2308 | } | |
2309 | wait_reshape(sra); | |
2310 | ||
2311 | if (st->ss->external) { | |
2312 | /* Re-load the metadata as much could have changed */ | |
2313 | int cfd = open_dev(st->container_dev); | |
2314 | if (cfd >= 0) { | |
2315 | ping_monitor(container); | |
2316 | st->ss->free_super(st); | |
2317 | st->ss->load_container(st, cfd, container); | |
2318 | close(cfd); | |
2319 | } | |
2320 | } | |
2321 | ||
6b2d630c N |
2322 | /* set new array size if required customer_array_size is used |
2323 | * by this metadata. | |
2324 | */ | |
2325 | if (reshape.before.data_disks != | |
2326 | reshape.after.data_disks && | |
2327 | info->custom_array_size) { | |
2328 | struct mdinfo *info2; | |
2329 | char *subarray = strchr(info->text_version+1, '/')+1; | |
582496b2 | 2330 | |
6b2d630c N |
2331 | info2 = st->ss->container_content(st, subarray); |
2332 | if (info2) { | |
2333 | unsigned long long current_size = 0; | |
2334 | unsigned long long new_size = | |
2335 | info2->custom_array_size/2; | |
2336 | ||
2337 | if (sysfs_get_ll(sra, | |
2338 | NULL, | |
2339 | "array_size", | |
2340 | ¤t_size) == 0 && | |
2341 | new_size > current_size) { | |
2342 | if (sysfs_set_num(sra, NULL, | |
2343 | "array_size", new_size) | |
2344 | < 0) | |
2345 | dprintf("Error: Cannot" | |
2346 | " set array size"); | |
2347 | else | |
2348 | dprintf("Array size " | |
2349 | "changed"); | |
2350 | dprintf(" from %llu to %llu.\n", | |
2351 | current_size, new_size); | |
582496b2 | 2352 | } |
6b2d630c | 2353 | sysfs_free(info2); |
582496b2 | 2354 | } |
6b2d630c | 2355 | } |
582496b2 | 2356 | |
6b2d630c | 2357 | if (info->new_level != reshape.level) { |
582496b2 | 2358 | |
6b2d630c | 2359 | c = map_num(pers, info->new_level); |
1cbc5680 N |
2360 | if (c) { |
2361 | err = sysfs_set_str(sra, NULL, "level", c); | |
2362 | if (err) | |
2363 | fprintf(stderr, Name\ | |
2364 | ": %s: could not set level " | |
2365 | "to %s\n", devname, c); | |
2366 | } | |
e919fb0a AK |
2367 | if (info->new_level == 0) |
2368 | st->update_tail = NULL; | |
7236ee7a | 2369 | } |
6b2d630c N |
2370 | out: |
2371 | if (forked) | |
2372 | return 0; | |
e84f2c00 | 2373 | unfreeze(st); |
6b2d630c | 2374 | exit(0); |
e86c9dd6 | 2375 | |
6b2d630c | 2376 | release: |
1cbc5680 | 2377 | if (orig_level != UnSet && sra) { |
7236ee7a N |
2378 | c = map_num(pers, orig_level); |
2379 | if (c && sysfs_set_str(sra, NULL, "level", c) == 0) | |
2380 | fprintf(stderr, Name ": aborting level change\n"); | |
2381 | } | |
9202b8eb N |
2382 | if (!forked) |
2383 | unfreeze(st); | |
631d7405 | 2384 | return 1; |
7236ee7a | 2385 | } |
e86c9dd6 | 2386 | |
493f5dd6 | 2387 | int reshape_container(char *container, char *devname, |
5da9ab98 N |
2388 | struct supertype *st, |
2389 | struct mdinfo *info, | |
2390 | int force, | |
2391 | char *backup_file, | |
493f5dd6 | 2392 | int quiet, int restart) |
5da9ab98 | 2393 | { |
1bb174ba | 2394 | struct mdinfo *cc = NULL; |
bcc9e9ed | 2395 | int rv = restart; |
1bb174ba | 2396 | |
d8eb27f7 KA |
2397 | /* component_size is not meaningful for a container, |
2398 | * so pass '-1' meaning 'no change' | |
2399 | */ | |
493f5dd6 N |
2400 | if (!restart && |
2401 | reshape_super(st, -1, info->new_level, | |
5da9ab98 | 2402 | info->new_layout, info->new_chunk, |
41784c88 | 2403 | info->array.raid_disks, info->delta_disks, |
9e325442 AK |
2404 | backup_file, devname, quiet)) { |
2405 | unfreeze(st); | |
5da9ab98 | 2406 | return 1; |
9e325442 | 2407 | } |
5da9ab98 N |
2408 | |
2409 | sync_metadata(st); | |
2410 | ||
1bb174ba AK |
2411 | /* ping monitor to be sure that update is on disk |
2412 | */ | |
2413 | ping_monitor(container); | |
5da9ab98 N |
2414 | |
2415 | switch (fork()) { | |
2416 | case -1: /* error */ | |
2417 | perror("Cannot fork to complete reshape\n"); | |
9e325442 | 2418 | unfreeze(st); |
5da9ab98 N |
2419 | return 1; |
2420 | default: /* parent */ | |
2421 | printf(Name ": multi-array reshape continues in background\n"); | |
2422 | return 0; | |
2423 | case 0: /* child */ | |
2424 | break; | |
2425 | } | |
2426 | ||
1bb174ba AK |
2427 | while(1) { |
2428 | /* For each member array with reshape_active, | |
2429 | * we need to perform the reshape. | |
2430 | * We pick the first array that needs reshaping and | |
2431 | * reshape it. reshape_array() will re-read the metadata | |
2432 | * so the next time through a different array should be | |
2433 | * ready for reshape. | |
493f5dd6 N |
2434 | * It is possible that the 'different' array will not |
2435 | * be assembled yet. In that case we simple exit. | |
2436 | * When it is assembled, the mdadm which assembles it | |
2437 | * will take over the reshape. | |
1bb174ba AK |
2438 | */ |
2439 | struct mdinfo *content; | |
5da9ab98 N |
2440 | int fd; |
2441 | struct mdstat_ent *mdstat; | |
5da9ab98 N |
2442 | char *adev; |
2443 | ||
1bb174ba | 2444 | sysfs_free(cc); |
5da9ab98 | 2445 | |
1bb174ba AK |
2446 | cc = st->ss->container_content(st, NULL); |
2447 | ||
2448 | for (content = cc; content ; content = content->next) { | |
2449 | char *subarray; | |
2450 | if (!content->reshape_active) | |
2451 | continue; | |
2452 | ||
2453 | subarray = strchr(content->text_version+1, '/')+1; | |
2454 | mdstat = mdstat_by_subdev(subarray, | |
2455 | devname2devnum(container)); | |
2456 | if (!mdstat) | |
2457 | continue; | |
2458 | break; | |
2459 | } | |
2460 | if (!content) | |
2461 | break; | |
5da9ab98 | 2462 | |
55f14721 | 2463 | fd = open_dev(mdstat->devnum); |
5da9ab98 N |
2464 | if (fd < 0) |
2465 | break; | |
2466 | adev = map_dev(dev2major(mdstat->devnum), | |
2467 | dev2minor(mdstat->devnum), | |
2468 | 0); | |
2469 | if (!adev) | |
1bb174ba AK |
2470 | adev = content->text_version; |
2471 | ||
2472 | sysfs_init(content, fd, mdstat->devnum); | |
5da9ab98 | 2473 | |
1bb174ba | 2474 | rv = reshape_array(container, fd, adev, st, |
e2e53a2d | 2475 | content, force, NULL, |
493f5dd6 | 2476 | backup_file, quiet, 1, restart); |
5da9ab98 | 2477 | close(fd); |
493f5dd6 | 2478 | restart = 0; |
5da9ab98 N |
2479 | if (rv) |
2480 | break; | |
2481 | } | |
bcc9e9ed AK |
2482 | if (!rv) |
2483 | unfreeze(st); | |
1bb174ba | 2484 | sysfs_free(cc); |
5da9ab98 N |
2485 | exit(0); |
2486 | } | |
2487 | ||
7236ee7a N |
2488 | /* |
2489 | * We run a child process in the background which performs the following | |
2490 | * steps: | |
2491 | * - wait for resync to reach a certain point | |
2492 | * - suspend io to the following section | |
2493 | * - backup that section | |
2494 | * - allow resync to proceed further | |
2495 | * - resume io | |
2496 | * - discard the backup. | |
2497 | * | |
2498 | * When are combined in slightly different ways in the three cases. | |
2499 | * Grow: | |
2500 | * - suspend/backup/allow/wait/resume/discard | |
2501 | * Shrink: | |
2502 | * - allow/wait/suspend/backup/allow/wait/resume/discard | |
2503 | * same-size: | |
2504 | * - wait/resume/discard/suspend/backup/allow | |
2505 | * | |
2506 | * suspend/backup/allow always come together | |
2507 | * wait/resume/discard do too. | |
2508 | * For the same-size case we have two backups to improve flow. | |
2509 | * | |
2510 | */ | |
e86c9dd6 | 2511 | |
7443ee81 N |
2512 | int progress_reshape(struct mdinfo *info, struct reshape *reshape, |
2513 | unsigned long long backup_point, | |
2514 | unsigned long long wait_point, | |
2515 | unsigned long long *suspend_point, | |
2516 | unsigned long long *reshape_completed) | |
2517 | { | |
2518 | /* This function is called repeatedly by the reshape manager. | |
2519 | * It determines how much progress can safely be made and allows | |
2520 | * that progress. | |
2521 | * - 'info' identifies the array and particularly records in | |
2522 | * ->reshape_progress the metadata's knowledge of progress | |
2523 | * This is a sector offset from the start of the array | |
2524 | * of the next array block to be relocated. This number | |
2525 | * may increase from 0 or decrease from array_size, depending | |
2526 | * on the type of reshape that is happening. | |
2527 | * Note that in contrast, 'sync_completed' is a block count of the | |
2528 | * reshape so far. It gives the distance between the start point | |
2529 | * (head or tail of device) and the next place that data will be | |
2530 | * written. It always increases. | |
2531 | * - 'reshape' is the structure created by analyse_change | |
2532 | * - 'backup_point' shows how much the metadata manager has backed-up | |
2533 | * data. For reshapes with increasing progress, it is the next address | |
2534 | * to be backed up, previous addresses have been backed-up. For | |
2535 | * decreasing progress, it is the earliest address that has been | |
2536 | * backed up - later address are also backed up. | |
2537 | * So addresses between reshape_progress and backup_point are | |
2538 | * backed up providing those are in the 'correct' order. | |
2539 | * - 'wait_point' is an array address. When reshape_completed | |
2540 | * passes this point, progress_reshape should return. It might | |
2541 | * return earlier if it determines that ->reshape_progress needs | |
2542 | * to be updated or further backup is needed. | |
2543 | * - suspend_point is maintained by progress_reshape and the caller | |
2544 | * should not touch it except to initialise to zero. | |
2545 | * It is an array address and it only increases in 2.6.37 and earlier. | |
b6b95155 | 2546 | * This makes it difficult to handle reducing reshapes with |
7443ee81 N |
2547 | * external metadata. |
2548 | * However: it is similar to backup_point in that it records the | |
2549 | * other end of a suspended region from reshape_progress. | |
2550 | * it is moved to extend the region that is safe to backup and/or | |
2551 | * reshape | |
2552 | * - reshape_completed is read from sysfs and returned. The caller | |
2553 | * should copy this into ->reshape_progress when it has reason to | |
2554 | * believe that the metadata knows this, and any backup outside this | |
2555 | * has been erased. | |
2556 | * | |
2557 | * Return value is: | |
2558 | * 1 if more data from backup_point - but only as far as suspend_point, | |
2559 | * should be backed up | |
2560 | * 0 if things are progressing smoothly | |
9e034f70 N |
2561 | * -1 if the reshape is finished because it is all done, |
2562 | * -2 if the reshape is finished due to an error. | |
7443ee81 N |
2563 | */ |
2564 | ||
2565 | int advancing = (reshape->after.data_disks | |
2566 | >= reshape->before.data_disks); | |
38dad34a N |
2567 | unsigned long long need_backup; /* All data between start of array and |
2568 | * here will at some point need to | |
2569 | * be backed up. | |
d0ab945e | 2570 | */ |
7443ee81 | 2571 | unsigned long long read_offset, write_offset; |
b4f8e38b | 2572 | unsigned long long write_range; |
7443ee81 | 2573 | unsigned long long max_progress, target, completed; |
d0ab945e N |
2574 | unsigned long long array_size = (info->component_size |
2575 | * reshape->before.data_disks); | |
7443ee81 | 2576 | int fd; |
77a73a17 | 2577 | char buf[20]; |
7443ee81 N |
2578 | |
2579 | /* First, we unsuspend any region that is now known to be safe. | |
2580 | * If suspend_point is on the 'wrong' side of reshape_progress, then | |
2581 | * we don't have or need suspension at the moment. This is true for | |
2582 | * native metadata when we don't need to back-up. | |
2583 | */ | |
2584 | if (advancing) { | |
49cd738b | 2585 | if (info->reshape_progress <= *suspend_point) |
7443ee81 N |
2586 | sysfs_set_num(info, NULL, "suspend_lo", |
2587 | info->reshape_progress); | |
2588 | } else { | |
2589 | /* Note: this won't work in 2.6.37 and before. | |
2590 | * Something somewhere should make sure we don't need it! | |
2591 | */ | |
49cd738b | 2592 | if (info->reshape_progress >= *suspend_point) |
7443ee81 N |
2593 | sysfs_set_num(info, NULL, "suspend_hi", |
2594 | info->reshape_progress); | |
2595 | } | |
2596 | ||
2597 | /* Now work out how far it is safe to progress. | |
2598 | * If the read_offset for ->reshape_progress is less than | |
2599 | * 'blocks' beyond the write_offset, we can only progress as far | |
2600 | * as a backup. | |
2601 | * Otherwise we can progress until the write_offset for the new location | |
2602 | * reaches (within 'blocks' of) the read_offset at the current location. | |
2603 | * However that region must be suspended unless we are using native | |
2604 | * metadata. | |
2605 | * If we need to suspend more, we limit it to 128M per device, which is | |
2606 | * rather arbitrary and should be some time-based calculation. | |
2607 | */ | |
ec757320 N |
2608 | read_offset = info->reshape_progress / reshape->before.data_disks; |
2609 | write_offset = info->reshape_progress / reshape->after.data_disks; | |
b4f8e38b | 2610 | write_range = info->new_chunk/512; |
38dad34a N |
2611 | if (reshape->before.data_disks == reshape->after.data_disks) |
2612 | need_backup = array_size; | |
2613 | else | |
2614 | need_backup = reshape->backup_blocks; | |
7443ee81 | 2615 | if (advancing) { |
38dad34a | 2616 | if (read_offset < write_offset + write_range) |
7443ee81 | 2617 | max_progress = backup_point; |
ddee071d | 2618 | else |
7443ee81 | 2619 | max_progress = |
2f3dd5e4 N |
2620 | read_offset * |
2621 | reshape->after.data_disks; | |
7443ee81 | 2622 | } else { |
38dad34a N |
2623 | if (read_offset > write_offset - write_range) |
2624 | /* Can only progress as far as has been backed up, | |
2625 | * which must be suspended */ | |
7443ee81 | 2626 | max_progress = backup_point; |
38dad34a N |
2627 | else if (info->reshape_progress <= need_backup) |
2628 | max_progress = backup_point; | |
2629 | else { | |
2630 | if (info->array.major_version >= 0) | |
2631 | /* Can progress until backup is needed */ | |
2632 | max_progress = need_backup; | |
2633 | else { | |
2634 | /* Can progress until metadata update is required */ | |
2635 | max_progress = | |
2636 | read_offset * | |
2637 | reshape->after.data_disks; | |
2638 | /* but data must be suspended */ | |
2639 | if (max_progress < *suspend_point) | |
2640 | max_progress = *suspend_point; | |
2641 | } | |
7443ee81 N |
2642 | } |
2643 | } | |
2644 | ||
2645 | /* We know it is safe to progress to 'max_progress' providing | |
2646 | * it is suspended or we are using native metadata. | |
2647 | * Consider extending suspend_point 128M per device if it | |
2648 | * is less than 64M per device beyond reshape_progress. | |
2649 | * But always do a multiple of 'blocks' | |
832b2b9a N |
2650 | * FIXME this is too big - it takes to long to complete |
2651 | * this much. | |
7443ee81 N |
2652 | */ |
2653 | target = 64*1024*2 * min(reshape->before.data_disks, | |
2654 | reshape->after.data_disks); | |
b4f8e38b | 2655 | target /= reshape->backup_blocks; |
7443ee81 N |
2656 | if (target < 2) |
2657 | target = 2; | |
b4f8e38b | 2658 | target *= reshape->backup_blocks; |
7443ee81 N |
2659 | |
2660 | /* For externally managed metadata we always need to suspend IO to | |
2661 | * the area being reshaped so we regularly push suspend_point forward. | |
2662 | * For native metadata we only need the suspend if we are going to do | |
2663 | * a backup. | |
2664 | */ | |
2665 | if (advancing) { | |
d0ab945e N |
2666 | if ((need_backup > info->reshape_progress |
2667 | || info->array.major_version < 0) && | |
7443ee81 | 2668 | *suspend_point < info->reshape_progress + target) { |
d0ab945e N |
2669 | if (need_backup < *suspend_point + 2 * target) |
2670 | *suspend_point = need_backup; | |
2671 | else if (*suspend_point + 2 * target < array_size) | |
7443ee81 | 2672 | *suspend_point += 2 * target; |
d0ab945e N |
2673 | else |
2674 | *suspend_point = array_size; | |
7443ee81 | 2675 | sysfs_set_num(info, NULL, "suspend_hi", *suspend_point); |
d0ab945e N |
2676 | if (max_progress > *suspend_point) |
2677 | max_progress = *suspend_point; | |
7443ee81 N |
2678 | } |
2679 | } else { | |
38dad34a N |
2680 | if (info->array.major_version >= 0) { |
2681 | /* Only need to suspend when about to backup */ | |
2682 | if (info->reshape_progress < need_backup * 2 && | |
2683 | *suspend_point > 0) { | |
d0ab945e | 2684 | *suspend_point = 0; |
38dad34a N |
2685 | sysfs_set_num(info, NULL, "suspend_lo", 0); |
2686 | sysfs_set_num(info, NULL, "suspend_hi", need_backup); | |
2687 | } | |
2688 | } else { | |
2689 | /* Need to suspend continually */ | |
2690 | if (info->reshape_progress < *suspend_point) | |
2691 | *suspend_point = info->reshape_progress; | |
2692 | if (*suspend_point + target < info->reshape_progress) | |
2693 | /* No need to move suspend region yet */; | |
2694 | else { | |
2695 | if (*suspend_point >= 2 * target) | |
2696 | *suspend_point -= 2 * target; | |
2697 | else | |
2698 | *suspend_point = 0; | |
2699 | sysfs_set_num(info, NULL, "suspend_lo", | |
2700 | *suspend_point); | |
2701 | } | |
d0ab945e N |
2702 | if (max_progress < *suspend_point) |
2703 | max_progress = *suspend_point; | |
7443ee81 N |
2704 | } |
2705 | } | |
2706 | ||
2707 | /* now set sync_max to allow that progress. sync_max, like | |
2708 | * sync_completed is a count of sectors written per device, so | |
2709 | * we find the difference between max_progress and the start point, | |
2710 | * and divide that by after.data_disks to get a sync_max | |
2711 | * number. | |
2712 | * At the same time we convert wait_point to a similar number | |
2713 | * for comparing against sync_completed. | |
2714 | */ | |
92d1991f | 2715 | /* scale down max_progress to per_disk */ |
7443ee81 | 2716 | max_progress /= reshape->after.data_disks; |
92d1991f N |
2717 | /* Round to chunk size as some kernels give an erroneously high number */ |
2718 | max_progress /= info->new_chunk/512; | |
2719 | max_progress *= info->new_chunk/512; | |
7f913e9b N |
2720 | /* And round to old chunk size as the kernel wants that */ |
2721 | max_progress /= info->array.chunk_size/512; | |
2722 | max_progress *= info->array.chunk_size/512; | |
92d1991f N |
2723 | /* Limit progress to the whole device */ |
2724 | if (max_progress > info->component_size) | |
2725 | max_progress = info->component_size; | |
7443ee81 | 2726 | wait_point /= reshape->after.data_disks; |
92d1991f N |
2727 | if (!advancing) { |
2728 | /* switch from 'device offset' to 'processed block count' */ | |
2729 | max_progress = info->component_size - max_progress; | |
2730 | wait_point = info->component_size - wait_point; | |
2731 | } | |
7443ee81 N |
2732 | |
2733 | sysfs_set_num(info, NULL, "sync_max", max_progress); | |
2734 | ||
2735 | /* Now wait. If we have already reached the point that we were | |
2736 | * asked to wait to, don't wait at all, else wait for any change. | |
2737 | * We need to select on 'sync_completed' as that is the place that | |
2738 | * notifications happen, but we are really interested in | |
2739 | * 'reshape_position' | |
2740 | */ | |
2741 | fd = sysfs_get_fd(info, NULL, "sync_completed"); | |
2742 | if (fd < 0) | |
77a73a17 | 2743 | goto check_progress; |
7443ee81 | 2744 | |
621ea11b | 2745 | if (sysfs_fd_get_ll(fd, &completed) < 0) |
77a73a17 | 2746 | goto check_progress; |
621ea11b | 2747 | |
7443ee81 N |
2748 | while (completed < max_progress && completed < wait_point) { |
2749 | /* Check that sync_action is still 'reshape' to avoid | |
2750 | * waiting forever on a dead array | |
2751 | */ | |
2752 | char action[20]; | |
2753 | fd_set rfds; | |
2754 | if (sysfs_get_str(info, NULL, "sync_action", | |
2755 | action, 20) <= 0 || | |
2756 | strncmp(action, "reshape", 7) != 0) | |
2757 | break; | |
26d6e157 AK |
2758 | /* Some kernels reset 'sync_completed' to zero |
2759 | * before setting 'sync_action' to 'idle'. | |
2760 | * So we need these extra tests. | |
2761 | */ | |
2762 | if (completed == 0 && advancing | |
2763 | && info->reshape_progress > 0) | |
2764 | break; | |
2765 | if (completed == 0 && !advancing | |
2766 | && info->reshape_progress < (info->component_size | |
2767 | * reshape->after.data_disks)) | |
2768 | break; | |
7443ee81 N |
2769 | FD_ZERO(&rfds); |
2770 | FD_SET(fd, &rfds); | |
2771 | select(fd+1, NULL, NULL, &rfds, NULL); | |
621ea11b | 2772 | if (sysfs_fd_get_ll(fd, &completed) < 0) |
77a73a17 | 2773 | goto check_progress; |
7443ee81 | 2774 | } |
10d0d365 AK |
2775 | /* Some kernels reset 'sync_completed' to zero, |
2776 | * we need to have real point we are in md | |
2777 | */ | |
2778 | if (completed == 0) | |
2779 | completed = max_progress; | |
2780 | ||
92d1991f N |
2781 | /* some kernels can give an incorrectly high 'completed' number */ |
2782 | completed /= (info->new_chunk/512); | |
2783 | completed *= (info->new_chunk/512); | |
7443ee81 N |
2784 | /* Convert 'completed' back in to a 'progress' number */ |
2785 | completed *= reshape->after.data_disks; | |
2786 | if (!advancing) { | |
2787 | completed = info->component_size * reshape->after.data_disks | |
2788 | - completed; | |
2789 | } | |
2790 | *reshape_completed = completed; | |
2791 | ||
2792 | close(fd); | |
2793 | ||
2794 | /* We return the need_backup flag. Caller will decide | |
d0ab945e | 2795 | * how much - a multiple of ->backup_blocks up to *suspend_point |
7443ee81 | 2796 | */ |
38dad34a N |
2797 | if (advancing) |
2798 | return need_backup > info->reshape_progress; | |
2799 | else | |
2800 | return need_backup >= info->reshape_progress; | |
77a73a17 N |
2801 | |
2802 | check_progress: | |
2803 | /* if we couldn't read a number from sync_completed, then | |
2804 | * either the reshape did complete, or it aborted. | |
2805 | * We can tell which by checking for 'none' in reshape_position. | |
621ea11b N |
2806 | * If it did abort, then it might immediately restart if it |
2807 | * it was just a device failure that leaves us degraded but | |
2808 | * functioning. | |
77a73a17 N |
2809 | */ |
2810 | strcpy(buf, "hi"); | |
2811 | if (sysfs_get_str(info, NULL, "reshape_position", buf, sizeof(buf)) < 0 | |
621ea11b N |
2812 | || strncmp(buf, "none", 4) != 0) { |
2813 | /* The abort might only be temporary. Wait up to 10 | |
2814 | * seconds for fd to contain a valid number again. | |
2815 | */ | |
2816 | struct timeval tv; | |
2817 | int rv = -2; | |
2818 | tv.tv_sec = 10; | |
2819 | tv.tv_usec = 0; | |
6560987b | 2820 | while (fd >= 0 && rv < 0 && tv.tv_sec > 0) { |
621ea11b N |
2821 | fd_set rfds; |
2822 | FD_ZERO(&rfds); | |
2823 | FD_SET(fd, &rfds); | |
2824 | if (select(fd+1, NULL, NULL, &rfds, &tv) != 1) | |
2825 | break; | |
6560987b N |
2826 | switch (sysfs_fd_get_ll(fd, &completed)) { |
2827 | case 0: | |
621ea11b N |
2828 | /* all good again */ |
2829 | rv = 1; | |
6560987b N |
2830 | break; |
2831 | case -2: /* read error - abort */ | |
2832 | tv.tv_sec = 0; | |
2833 | break; | |
2834 | } | |
621ea11b N |
2835 | } |
2836 | if (fd >= 0) | |
2837 | close(fd); | |
2838 | return rv; /* abort */ | |
2839 | } else { | |
6d5316f6 | 2840 | /* Maybe racing with array shutdown - check state */ |
621ea11b N |
2841 | if (fd >= 0) |
2842 | close(fd); | |
6d5316f6 N |
2843 | if (sysfs_get_str(info, NULL, "array_state", buf, sizeof(buf)) < 0 |
2844 | || strncmp(buf, "inactive", 8) == 0 | |
2845 | || strncmp(buf, "clear",5) == 0) | |
2846 | return -2; /* abort */ | |
77a73a17 | 2847 | return -1; /* complete */ |
6d5316f6 | 2848 | } |
7443ee81 N |
2849 | } |
2850 | ||
2851 | ||
fcf57625 | 2852 | /* FIXME return status is never checked */ |
4411fb17 | 2853 | static int grow_backup(struct mdinfo *sra, |
7236ee7a | 2854 | unsigned long long offset, /* per device */ |
7443ee81 | 2855 | unsigned long stripes, /* per device, in old chunks */ |
7236ee7a N |
2856 | int *sources, unsigned long long *offsets, |
2857 | int disks, int chunk, int level, int layout, | |
2858 | int dests, int *destfd, unsigned long long *destoffsets, | |
d4445387 | 2859 | int part, int *degraded, |
7236ee7a N |
2860 | char *buf) |
2861 | { | |
2862 | /* Backup 'blocks' sectors at 'offset' on each device of the array, | |
2863 | * to storage 'destfd' (offset 'destoffsets'), after first | |
2864 | * suspending IO. Then allow resync to continue | |
2865 | * over the suspended section. | |
2866 | * Use part 'part' of the backup-super-block. | |
2867 | */ | |
2868 | int odata = disks; | |
2869 | int rv = 0; | |
2870 | int i; | |
f21e18ca N |
2871 | unsigned long long ll; |
2872 | int new_degraded; | |
7236ee7a N |
2873 | //printf("offset %llu\n", offset); |
2874 | if (level >= 4) | |
2875 | odata--; | |
2876 | if (level == 6) | |
2877 | odata--; | |
7443ee81 | 2878 | |
d4445387 | 2879 | /* Check that array hasn't become degraded, else we might backup the wrong data */ |
2c6ac128 N |
2880 | if (sysfs_get_ll(sra, NULL, "degraded", &ll) < 0) |
2881 | return -1; /* FIXME this error is ignored */ | |
f21e18ca | 2882 | new_degraded = (int)ll; |
d4445387 N |
2883 | if (new_degraded != *degraded) { |
2884 | /* check each device to ensure it is still working */ | |
2885 | struct mdinfo *sd; | |
2886 | for (sd = sra->devs ; sd ; sd = sd->next) { | |
2887 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) | |
2888 | continue; | |
2889 | if (sd->disk.state & (1<<MD_DISK_SYNC)) { | |
2890 | char sbuf[20]; | |
2891 | if (sysfs_get_str(sra, sd, "state", sbuf, 20) < 0 || | |
2892 | strstr(sbuf, "faulty") || | |
2893 | strstr(sbuf, "in_sync") == NULL) { | |
2894 | /* this device is dead */ | |
2895 | sd->disk.state = (1<<MD_DISK_FAULTY); | |
2896 | if (sd->disk.raid_disk >= 0 && | |
2897 | sources[sd->disk.raid_disk] >= 0) { | |
2898 | close(sources[sd->disk.raid_disk]); | |
2899 | sources[sd->disk.raid_disk] = -1; | |
2900 | } | |
2901 | } | |
2902 | } | |
2903 | } | |
2904 | *degraded = new_degraded; | |
2905 | } | |
7236ee7a N |
2906 | if (part) { |
2907 | bsb.arraystart2 = __cpu_to_le64(offset * odata); | |
200871ad | 2908 | bsb.length2 = __cpu_to_le64(stripes * (chunk/512) * odata); |
7236ee7a N |
2909 | } else { |
2910 | bsb.arraystart = __cpu_to_le64(offset * odata); | |
200871ad | 2911 | bsb.length = __cpu_to_le64(stripes * (chunk/512) * odata); |
7236ee7a N |
2912 | } |
2913 | if (part) | |
2914 | bsb.magic[15] = '2'; | |
2915 | for (i = 0; i < dests; i++) | |
2916 | if (part) | |
2917 | lseek64(destfd[i], destoffsets[i] + __le64_to_cpu(bsb.devstart2)*512, 0); | |
2918 | else | |
2919 | lseek64(destfd[i], destoffsets[i], 0); | |
2920 | ||
2921 | rv = save_stripes(sources, offsets, | |
2922 | disks, chunk, level, layout, | |
2923 | dests, destfd, | |
2924 | offset*512*odata, stripes * chunk * odata, | |
2925 | buf); | |
2926 | ||
2927 | if (rv) | |
2928 | return rv; | |
97396422 | 2929 | bsb.mtime = __cpu_to_le64(time(0)); |
7236ee7a N |
2930 | for (i = 0; i < dests; i++) { |
2931 | bsb.devstart = __cpu_to_le64(destoffsets[i]/512); | |
2932 | ||
2933 | bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb)); | |
2934 | if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0) | |
2935 | bsb.sb_csum2 = bsb_csum((char*)&bsb, | |
2936 | ((char*)&bsb.sb_csum2)-((char*)&bsb)); | |
2937 | ||
72044953 | 2938 | rv = -1; |
f21e18ca N |
2939 | if ((unsigned long long)lseek64(destfd[i], destoffsets[i] - 4096, 0) |
2940 | != destoffsets[i] - 4096) | |
72044953 N |
2941 | break; |
2942 | if (write(destfd[i], &bsb, 512) != 512) | |
2943 | break; | |
ff94fb86 | 2944 | if (destoffsets[i] > 4096) { |
f21e18ca | 2945 | if ((unsigned long long)lseek64(destfd[i], destoffsets[i]+stripes*chunk*odata, 0) != |
a847575a | 2946 | destoffsets[i]+stripes*chunk*odata) |
72044953 N |
2947 | break; |
2948 | if (write(destfd[i], &bsb, 512) != 512) | |
2949 | break; | |
ff94fb86 | 2950 | } |
7236ee7a | 2951 | fsync(destfd[i]); |
72044953 | 2952 | rv = 0; |
7236ee7a | 2953 | } |
2efedc7b | 2954 | |
fcf57625 | 2955 | return rv; |
7236ee7a N |
2956 | } |
2957 | ||
2958 | /* in 2.6.30, the value reported by sync_completed can be | |
2959 | * less that it should be by one stripe. | |
2960 | * This only happens when reshape hits sync_max and pauses. | |
2961 | * So allow wait_backup to either extent sync_max further | |
2962 | * than strictly necessary, or return before the | |
2963 | * sync has got quite as far as we would really like. | |
2964 | * This is what 'blocks2' is for. | |
2965 | * The various caller give appropriate values so that | |
2966 | * every works. | |
2967 | */ | |
fcf57625 | 2968 | /* FIXME return value is often ignored */ |
7443ee81 | 2969 | static int forget_backup( |
7236ee7a N |
2970 | int dests, int *destfd, unsigned long long *destoffsets, |
2971 | int part) | |
2972 | { | |
7443ee81 N |
2973 | /* |
2974 | * Erase backup 'part' (which is 0 or 1) | |
7236ee7a | 2975 | */ |
7236ee7a | 2976 | int i; |
fcf57625 | 2977 | int rv; |
7236ee7a | 2978 | |
7236ee7a N |
2979 | if (part) { |
2980 | bsb.arraystart2 = __cpu_to_le64(0); | |
2981 | bsb.length2 = __cpu_to_le64(0); | |
2982 | } else { | |
2983 | bsb.arraystart = __cpu_to_le64(0); | |
2984 | bsb.length = __cpu_to_le64(0); | |
2985 | } | |
97396422 | 2986 | bsb.mtime = __cpu_to_le64(time(0)); |
fcf57625 | 2987 | rv = 0; |
7236ee7a N |
2988 | for (i = 0; i < dests; i++) { |
2989 | bsb.devstart = __cpu_to_le64(destoffsets[i]/512); | |
2990 | bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb)); | |
2991 | if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0) | |
2992 | bsb.sb_csum2 = bsb_csum((char*)&bsb, | |
2993 | ((char*)&bsb.sb_csum2)-((char*)&bsb)); | |
f21e18ca | 2994 | if ((unsigned long long)lseek64(destfd[i], destoffsets[i]-4096, 0) != |
a847575a | 2995 | destoffsets[i]-4096) |
72044953 N |
2996 | rv = -1; |
2997 | if (rv == 0 && | |
2998 | write(destfd[i], &bsb, 512) != 512) | |
2999 | rv = -1; | |
7236ee7a N |
3000 | fsync(destfd[i]); |
3001 | } | |
fcf57625 | 3002 | return rv; |
7236ee7a | 3003 | } |
e86c9dd6 | 3004 | |
7236ee7a N |
3005 | static void fail(char *msg) |
3006 | { | |
fcf57625 | 3007 | int rv; |
72044953 N |
3008 | rv = (write(2, msg, strlen(msg)) != (int)strlen(msg)); |
3009 | rv |= (write(2, "\n", 1) != 1); | |
fcf57625 | 3010 | exit(rv ? 1 : 2); |
7236ee7a N |
3011 | } |
3012 | ||
3013 | static char *abuf, *bbuf; | |
f21e18ca | 3014 | static unsigned long long abuflen; |
7236ee7a N |
3015 | static void validate(int afd, int bfd, unsigned long long offset) |
3016 | { | |
3017 | /* check that the data in the backup against the array. | |
3018 | * This is only used for regression testing and should not | |
3019 | * be used while the array is active | |
3020 | */ | |
7236ee7a N |
3021 | if (afd < 0) |
3022 | return; | |
3023 | lseek64(bfd, offset - 4096, 0); | |
3024 | if (read(bfd, &bsb2, 512) != 512) | |
3025 | fail("cannot read bsb"); | |
3026 | if (bsb2.sb_csum != bsb_csum((char*)&bsb2, | |
3027 | ((char*)&bsb2.sb_csum)-((char*)&bsb2))) | |
3028 | fail("first csum bad"); | |
3029 | if (memcmp(bsb2.magic, "md_backup_data", 14) != 0) | |
3030 | fail("magic is bad"); | |
3031 | if (memcmp(bsb2.magic, "md_backup_data-2", 16) == 0 && | |
3032 | bsb2.sb_csum2 != bsb_csum((char*)&bsb2, | |
3033 | ((char*)&bsb2.sb_csum2)-((char*)&bsb2))) | |
3034 | fail("second csum bad"); | |
3035 | ||
3036 | if (__le64_to_cpu(bsb2.devstart)*512 != offset) | |
3037 | fail("devstart is wrong"); | |
3038 | ||
3039 | if (bsb2.length) { | |
3040 | unsigned long long len = __le64_to_cpu(bsb2.length)*512; | |
3041 | ||
3042 | if (abuflen < len) { | |
3043 | free(abuf); | |
3044 | free(bbuf); | |
3045 | abuflen = len; | |
fcf57625 N |
3046 | if (posix_memalign((void**)&abuf, 4096, abuflen) || |
3047 | posix_memalign((void**)&bbuf, 4096, abuflen)) { | |
3048 | abuflen = 0; | |
3049 | /* just stop validating on mem-alloc failure */ | |
3050 | return; | |
3051 | } | |
e86c9dd6 | 3052 | } |
48924014 | 3053 | |
7236ee7a | 3054 | lseek64(bfd, offset, 0); |
f21e18ca | 3055 | if ((unsigned long long)read(bfd, bbuf, len) != len) { |
080fd005 | 3056 | //printf("len %llu\n", len); |
7236ee7a N |
3057 | fail("read first backup failed"); |
3058 | } | |
3059 | lseek64(afd, __le64_to_cpu(bsb2.arraystart)*512, 0); | |
f21e18ca | 3060 | if ((unsigned long long)read(afd, abuf, len) != len) |
7236ee7a N |
3061 | fail("read first from array failed"); |
3062 | if (memcmp(bbuf, abuf, len) != 0) { | |
080fd005 | 3063 | #if 0 |
7236ee7a N |
3064 | int i; |
3065 | printf("offset=%llu len=%llu\n", | |
080fd005 | 3066 | (unsigned long long)__le64_to_cpu(bsb2.arraystart)*512, len); |
7236ee7a N |
3067 | for (i=0; i<len; i++) |
3068 | if (bbuf[i] != abuf[i]) { | |
3069 | printf("first diff byte %d\n", i); | |
93ecfa01 | 3070 | break; |
7236ee7a | 3071 | } |
080fd005 | 3072 | #endif |
7236ee7a | 3073 | fail("data1 compare failed"); |
e86c9dd6 | 3074 | } |
7236ee7a N |
3075 | } |
3076 | if (bsb2.length2) { | |
3077 | unsigned long long len = __le64_to_cpu(bsb2.length2)*512; | |
3078 | ||
3079 | if (abuflen < len) { | |
3080 | free(abuf); | |
3081 | free(bbuf); | |
3082 | abuflen = len; | |
3083 | abuf = malloc(abuflen); | |
3084 | bbuf = malloc(abuflen); | |
e86c9dd6 NB |
3085 | } |
3086 | ||
7236ee7a | 3087 | lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0); |
f21e18ca | 3088 | if ((unsigned long long)read(bfd, bbuf, len) != len) |
7236ee7a N |
3089 | fail("read second backup failed"); |
3090 | lseek64(afd, __le64_to_cpu(bsb2.arraystart2)*512, 0); | |
f21e18ca | 3091 | if ((unsigned long long)read(afd, abuf, len) != len) |
7236ee7a N |
3092 | fail("read second from array failed"); |
3093 | if (memcmp(bbuf, abuf, len) != 0) | |
3094 | fail("data2 compare failed"); | |
e86c9dd6 | 3095 | } |
7236ee7a | 3096 | } |
e86c9dd6 | 3097 | |
999b4972 N |
3098 | int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape, |
3099 | struct supertype *st, unsigned long blocks, | |
3100 | int *fds, unsigned long long *offsets, | |
3101 | int dests, int *destfd, unsigned long long *destoffsets) | |
7236ee7a | 3102 | { |
7443ee81 N |
3103 | /* Monitor a reshape where backup is being performed using |
3104 | * 'native' mechanism - either to a backup file, or | |
3105 | * to some space in a spare. | |
3106 | */ | |
7236ee7a | 3107 | char *buf; |
7443ee81 N |
3108 | int degraded = -1; |
3109 | unsigned long long speed; | |
3110 | unsigned long long suspend_point, array_size; | |
3111 | unsigned long long backup_point, wait_point; | |
3112 | unsigned long long reshape_completed; | |
3113 | int done = 0; | |
3114 | int increasing = reshape->after.data_disks >= reshape->before.data_disks; | |
3115 | int part = 0; /* The next part of the backup area to fill. It may already | |
3116 | * be full, so we need to check */ | |
3117 | int level = reshape->level; | |
3118 | int layout = reshape->before.layout; | |
3119 | int data = reshape->before.data_disks; | |
3120 | int disks = reshape->before.data_disks + reshape->parity; | |
3121 | int chunk = sra->array.chunk_size; | |
da8100ca N |
3122 | struct mdinfo *sd; |
3123 | unsigned long stripes; | |
3b7e9d0c | 3124 | int uuid[4]; |
da8100ca N |
3125 | |
3126 | /* set up the backup-super-block. This requires the | |
3127 | * uuid from the array. | |
3128 | */ | |
3129 | /* Find a superblock */ | |
3130 | for (sd = sra->devs; sd; sd = sd->next) { | |
3131 | char *dn; | |
3132 | int devfd; | |
3133 | int ok; | |
3134 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) | |
3135 | continue; | |
3136 | dn = map_dev(sd->disk.major, sd->disk.minor, 1); | |
3137 | devfd = dev_open(dn, O_RDONLY); | |
3138 | if (devfd < 0) | |
3139 | continue; | |
3140 | ok = st->ss->load_super(st, devfd, NULL); | |
3141 | close(devfd); | |
77f8d358 | 3142 | if (ok == 0) |
da8100ca N |
3143 | break; |
3144 | } | |
3145 | if (!sd) { | |
3146 | fprintf(stderr, Name ": Cannot find a superblock\n"); | |
3147 | return 0; | |
3148 | } | |
3149 | ||
3150 | memset(&bsb, 0, 512); | |
3151 | memcpy(bsb.magic, "md_backup_data-1", 16); | |
3b7e9d0c LB |
3152 | st->ss->uuid_from_super(st, uuid); |
3153 | memcpy(bsb.set_uuid, uuid, 16); | |
da8100ca N |
3154 | bsb.mtime = __cpu_to_le64(time(0)); |
3155 | bsb.devstart2 = blocks; | |
3156 | ||
3157 | stripes = blocks / (sra->array.chunk_size/512) / | |
3158 | reshape->before.data_disks; | |
e86c9dd6 | 3159 | |
fcf57625 N |
3160 | if (posix_memalign((void**)&buf, 4096, disks * chunk)) |
3161 | /* Don't start the 'reshape' */ | |
3162 | return 0; | |
7443ee81 N |
3163 | if (reshape->before.data_disks == reshape->after.data_disks) { |
3164 | sysfs_get_ll(sra, NULL, "sync_speed_min", &speed); | |
3165 | sysfs_set_num(sra, NULL, "sync_speed_min", 200000); | |
3166 | } | |
e86c9dd6 | 3167 | |
7443ee81 | 3168 | if (increasing) { |
96533072 | 3169 | array_size = sra->component_size * reshape->after.data_disks; |
7443ee81 N |
3170 | backup_point = sra->reshape_progress; |
3171 | suspend_point = 0; | |
3172 | } else { | |
96533072 | 3173 | array_size = sra->component_size * reshape->before.data_disks; |
25da62d9 | 3174 | backup_point = reshape->backup_blocks; |
7443ee81 N |
3175 | suspend_point = array_size; |
3176 | } | |
7236ee7a | 3177 | |
7443ee81 N |
3178 | while (!done) { |
3179 | int rv; | |
7236ee7a | 3180 | |
7443ee81 N |
3181 | /* Want to return as soon the oldest backup slot can |
3182 | * be released as that allows us to start backing up | |
3183 | * some more, providing suspend_point has been | |
b6b95155 | 3184 | * advanced, which it should have. |
7443ee81 N |
3185 | */ |
3186 | if (increasing) { | |
3187 | wait_point = array_size; | |
3188 | if (part == 0 && __le64_to_cpu(bsb.length) > 0) | |
3189 | wait_point = (__le64_to_cpu(bsb.arraystart) + | |
3190 | __le64_to_cpu(bsb.length)); | |
3191 | if (part == 1 && __le64_to_cpu(bsb.length2) > 0) | |
3192 | wait_point = (__le64_to_cpu(bsb.arraystart2) + | |
3193 | __le64_to_cpu(bsb.length2)); | |
3194 | } else { | |
3195 | wait_point = 0; | |
3196 | if (part == 0 && __le64_to_cpu(bsb.length) > 0) | |
3197 | wait_point = __le64_to_cpu(bsb.arraystart); | |
3198 | if (part == 1 && __le64_to_cpu(bsb.length2) > 0) | |
3199 | wait_point = __le64_to_cpu(bsb.arraystart2); | |
3200 | } | |
3201 | ||
3202 | rv = progress_reshape(sra, reshape, | |
3203 | backup_point, wait_point, | |
3204 | &suspend_point, &reshape_completed); | |
7443ee81 N |
3205 | /* external metadata would need to ping_monitor here */ |
3206 | sra->reshape_progress = reshape_completed; | |
7236ee7a | 3207 | |
7443ee81 N |
3208 | /* Clear any backup region that is before 'here' */ |
3209 | if (increasing) { | |
b3cf095a N |
3210 | if (__le64_to_cpu(bsb.length) > 0 && |
3211 | reshape_completed >= (__le64_to_cpu(bsb.arraystart) + | |
7443ee81 N |
3212 | __le64_to_cpu(bsb.length))) |
3213 | forget_backup(dests, destfd, | |
3214 | destoffsets, 0); | |
b3cf095a N |
3215 | if (__le64_to_cpu(bsb.length2) > 0 && |
3216 | reshape_completed >= (__le64_to_cpu(bsb.arraystart2) + | |
7443ee81 N |
3217 | __le64_to_cpu(bsb.length2))) |
3218 | forget_backup(dests, destfd, | |
3219 | destoffsets, 1); | |
3220 | } else { | |
b3cf095a N |
3221 | if (__le64_to_cpu(bsb.length) > 0 && |
3222 | reshape_completed <= (__le64_to_cpu(bsb.arraystart))) | |
7443ee81 N |
3223 | forget_backup(dests, destfd, |
3224 | destoffsets, 0); | |
b3cf095a N |
3225 | if (__le64_to_cpu(bsb.length2) > 0 && |
3226 | reshape_completed <= (__le64_to_cpu(bsb.arraystart2))) | |
7443ee81 N |
3227 | forget_backup(dests, destfd, |
3228 | destoffsets, 1); | |
3229 | } | |
7236ee7a | 3230 | |
223c5c99 | 3231 | if (rv < 0) { |
77a73a17 N |
3232 | if (rv == -1) |
3233 | done = 1; | |
223c5c99 N |
3234 | break; |
3235 | } | |
2d4de5f9 N |
3236 | if (rv == 0 && increasing && !st->ss->external) { |
3237 | /* No longer need to monitor this reshape */ | |
3238 | done = 1; | |
3239 | break; | |
3240 | } | |
223c5c99 | 3241 | |
60204e4e | 3242 | while (rv) { |
7443ee81 | 3243 | unsigned long long offset; |
e97ca002 | 3244 | unsigned long actual_stripes; |
60204e4e N |
3245 | /* Need to backup some data. |
3246 | * If 'part' is not used and the desired | |
3247 | * backup size is suspended, do a backup, | |
3248 | * then consider the next part. | |
3249 | */ | |
7443ee81 N |
3250 | /* Check that 'part' is unused */ |
3251 | if (part == 0 && __le64_to_cpu(bsb.length) != 0) | |
60204e4e | 3252 | break; |
7443ee81 | 3253 | if (part == 1 && __le64_to_cpu(bsb.length2) != 0) |
60204e4e | 3254 | break; |
7443ee81 N |
3255 | |
3256 | offset = backup_point / data; | |
e97ca002 | 3257 | actual_stripes = stripes; |
60204e4e | 3258 | if (increasing) { |
e97ca002 N |
3259 | if (offset + actual_stripes * (chunk/512) > |
3260 | sra->component_size) | |
3261 | actual_stripes = ((sra->component_size - offset) | |
3262 | / (chunk/512)); | |
3263 | if (offset + actual_stripes * (chunk/512) > | |
60204e4e N |
3264 | suspend_point/data) |
3265 | break; | |
3266 | } else { | |
e97ca002 N |
3267 | if (offset < actual_stripes * (chunk/512)) |
3268 | actual_stripes = offset / (chunk/512); | |
3269 | offset -= actual_stripes * (chunk/512); | |
3270 | if (offset < suspend_point/data) | |
60204e4e N |
3271 | break; |
3272 | } | |
e4b11073 N |
3273 | if (actual_stripes == 0) |
3274 | break; | |
e97ca002 | 3275 | grow_backup(sra, offset, actual_stripes, |
7443ee81 N |
3276 | fds, offsets, |
3277 | disks, chunk, level, layout, | |
3278 | dests, destfd, destoffsets, | |
3279 | part, °raded, buf); | |
3280 | validate(afd, destfd[0], destoffsets[0]); | |
3281 | /* record where 'part' is up to */ | |
3282 | part = !part; | |
3283 | if (increasing) | |
e97ca002 | 3284 | backup_point += actual_stripes * (chunk/512) * data; |
7443ee81 | 3285 | else |
e97ca002 | 3286 | backup_point -= actual_stripes * (chunk/512) * data; |
7443ee81 N |
3287 | } |
3288 | } | |
3289 | ||
223c5c99 N |
3290 | /* FIXME maybe call progress_reshape one more time instead */ |
3291 | abort_reshape(sra); /* remove any remaining suspension */ | |
7443ee81 N |
3292 | if (reshape->before.data_disks == reshape->after.data_disks) |
3293 | sysfs_set_num(sra, NULL, "sync_speed_min", speed); | |
7236ee7a | 3294 | free(buf); |
77a73a17 | 3295 | return done; |
e86c9dd6 | 3296 | } |
353632d9 NB |
3297 | |
3298 | /* | |
3299 | * If any spare contains md_back_data-1 which is recent wrt mtime, | |
3300 | * write that data into the array and update the super blocks with | |
3301 | * the new reshape_progress | |
3302 | */ | |
ea0ebe96 N |
3303 | int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt, |
3304 | char *backup_file, int verbose) | |
353632d9 NB |
3305 | { |
3306 | int i, j; | |
3307 | int old_disks; | |
353632d9 | 3308 | unsigned long long *offsets; |
82f2d6ab | 3309 | unsigned long long nstripe, ostripe; |
6e9eac4f | 3310 | int ndata, odata; |
353632d9 | 3311 | |
e9e43ec3 N |
3312 | odata = info->array.raid_disks - info->delta_disks - 1; |
3313 | if (info->array.level == 6) odata--; /* number of data disks */ | |
3314 | ndata = info->array.raid_disks - 1; | |
3315 | if (info->new_level == 6) ndata--; | |
353632d9 NB |
3316 | |
3317 | old_disks = info->array.raid_disks - info->delta_disks; | |
3318 | ||
e9e43ec3 N |
3319 | if (info->delta_disks <= 0) |
3320 | /* Didn't grow, so the backup file must have | |
3321 | * been used | |
3322 | */ | |
3323 | old_disks = cnt; | |
06b0d786 | 3324 | for (i=old_disks-(backup_file?1:0); i<cnt; i++) { |
353632d9 | 3325 | struct mdinfo dinfo; |
06b0d786 | 3326 | int fd; |
e9e43ec3 | 3327 | int bsbsize; |
ea0ebe96 | 3328 | char *devname, namebuf[20]; |
10af14c4 | 3329 | unsigned long long lo, hi; |
353632d9 NB |
3330 | |
3331 | /* This was a spare and may have some saved data on it. | |
3332 | * Load the superblock, find and load the | |
3333 | * backup_super_block. | |
3334 | * If either fail, go on to next device. | |
3335 | * If the backup contains no new info, just return | |
206c5eae | 3336 | * else restore data and update all superblocks |
353632d9 | 3337 | */ |
06b0d786 NB |
3338 | if (i == old_disks-1) { |
3339 | fd = open(backup_file, O_RDONLY); | |
e9e43ec3 N |
3340 | if (fd<0) { |
3341 | fprintf(stderr, Name ": backup file %s inaccessible: %s\n", | |
3342 | backup_file, strerror(errno)); | |
06b0d786 | 3343 | continue; |
e9e43ec3 | 3344 | } |
ea0ebe96 | 3345 | devname = backup_file; |
06b0d786 NB |
3346 | } else { |
3347 | fd = fdlist[i]; | |
3348 | if (fd < 0) | |
3349 | continue; | |
3da92f27 | 3350 | if (st->ss->load_super(st, fd, NULL)) |
06b0d786 | 3351 | continue; |
353632d9 | 3352 | |
a5d85af7 | 3353 | st->ss->getinfo_super(st, &dinfo, NULL); |
3da92f27 NB |
3354 | st->ss->free_super(st); |
3355 | ||
06b0d786 NB |
3356 | if (lseek64(fd, |
3357 | (dinfo.data_offset + dinfo.component_size - 8) <<9, | |
ea0ebe96 N |
3358 | 0) < 0) { |
3359 | fprintf(stderr, Name ": Cannot seek on device %d\n", i); | |
06b0d786 | 3360 | continue; /* Cannot seek */ |
ea0ebe96 N |
3361 | } |
3362 | sprintf(namebuf, "device-%d", i); | |
3363 | devname = namebuf; | |
06b0d786 | 3364 | } |
ea0ebe96 N |
3365 | if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb)) { |
3366 | if (verbose) | |
3367 | fprintf(stderr, Name ": Cannot read from %s\n", devname); | |
353632d9 | 3368 | continue; /* Cannot read */ |
ea0ebe96 | 3369 | } |
e9e43ec3 | 3370 | if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0 && |
ea0ebe96 N |
3371 | memcmp(bsb.magic, "md_backup_data-2", 16) != 0) { |
3372 | if (verbose) | |
3373 | fprintf(stderr, Name ": No backup metadata on %s\n", devname); | |
353632d9 | 3374 | continue; |
ea0ebe96 N |
3375 | } |
3376 | if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb))) { | |
3377 | if (verbose) | |
3378 | fprintf(stderr, Name ": Bad backup-metadata checksum on %s\n", devname); | |
353632d9 | 3379 | continue; /* bad checksum */ |
ea0ebe96 | 3380 | } |
e9e43ec3 | 3381 | if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0 && |
ea0ebe96 N |
3382 | bsb.sb_csum2 != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum2)-((char*)&bsb))) { |
3383 | if (verbose) | |
3384 | fprintf(stderr, Name ": Bad backup-metadata checksum2 on %s\n", devname); | |
22e30516 | 3385 | continue; /* Bad second checksum */ |
ea0ebe96 N |
3386 | } |
3387 | if (memcmp(bsb.set_uuid,info->uuid, 16) != 0) { | |
3388 | if (verbose) | |
3389 | fprintf(stderr, Name ": Wrong uuid on backup-metadata on %s\n", devname); | |
353632d9 | 3390 | continue; /* Wrong uuid */ |
ea0ebe96 | 3391 | } |
353632d9 | 3392 | |
097075b6 N |
3393 | /* array utime and backup-mtime should be updated at much the same time, but it seems that |
3394 | * sometimes they aren't... So allow considerable flexability in matching, and allow | |
3395 | * this test to be overridden by an environment variable. | |
3396 | */ | |
f21e18ca N |
3397 | if (info->array.utime > (int)__le64_to_cpu(bsb.mtime) + 2*60*60 || |
3398 | info->array.utime < (int)__le64_to_cpu(bsb.mtime) - 10*60) { | |
097075b6 N |
3399 | if (check_env("MDADM_GROW_ALLOW_OLD")) { |
3400 | fprintf(stderr, Name ": accepting backup with timestamp %lu " | |
3401 | "for array with timestamp %lu\n", | |
3402 | (unsigned long)__le64_to_cpu(bsb.mtime), | |
3403 | (unsigned long)info->array.utime); | |
3404 | } else { | |
3405 | if (verbose) | |
3406 | fprintf(stderr, Name ": too-old timestamp on " | |
3407 | "backup-metadata on %s\n", devname); | |
3408 | continue; /* time stamp is too bad */ | |
3409 | } | |
ea0ebe96 | 3410 | } |
353632d9 | 3411 | |
e9e43ec3 | 3412 | if (bsb.magic[15] == '1') { |
10af14c4 N |
3413 | if (bsb.length == 0) |
3414 | continue; | |
e7a71c6b N |
3415 | if (info->delta_disks >= 0) { |
3416 | /* reshape_progress is increasing */ | |
3417 | if (__le64_to_cpu(bsb.arraystart) | |
3418 | + __le64_to_cpu(bsb.length) | |
3419 | < info->reshape_progress) { | |
3420 | nonew: | |
3421 | if (verbose) | |
3422 | fprintf(stderr, Name | |
3423 | ": backup-metadata found on %s but is not needed\n", devname); | |
3424 | continue; /* No new data here */ | |
3425 | } | |
3426 | } else { | |
3427 | /* reshape_progress is decreasing */ | |
3428 | if (__le64_to_cpu(bsb.arraystart) >= | |
3429 | info->reshape_progress) | |
3430 | goto nonew; /* No new data here */ | |
ea0ebe96 | 3431 | } |
e9e43ec3 | 3432 | } else { |
10af14c4 N |
3433 | if (bsb.length == 0 && bsb.length2 == 0) |
3434 | continue; | |
e7a71c6b N |
3435 | if (info->delta_disks >= 0) { |
3436 | /* reshape_progress is increasing */ | |
3437 | if ((__le64_to_cpu(bsb.arraystart) | |
3438 | + __le64_to_cpu(bsb.length) | |
3439 | < info->reshape_progress) | |
3440 | && | |
3441 | (__le64_to_cpu(bsb.arraystart2) | |
3442 | + __le64_to_cpu(bsb.length2) | |
3443 | < info->reshape_progress)) | |
3444 | goto nonew; /* No new data here */ | |
3445 | } else { | |
3446 | /* reshape_progress is decreasing */ | |
3447 | if (__le64_to_cpu(bsb.arraystart) >= | |
3448 | info->reshape_progress && | |
3449 | __le64_to_cpu(bsb.arraystart2) >= | |
3450 | info->reshape_progress) | |
3451 | goto nonew; /* No new data here */ | |
3452 | } | |
e9e43ec3 | 3453 | } |
ea0ebe96 N |
3454 | if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0) { |
3455 | second_fail: | |
3456 | if (verbose) | |
e7a71c6b N |
3457 | fprintf(stderr, Name |
3458 | ": Failed to verify secondary backup-metadata block on %s\n", | |
ea0ebe96 | 3459 | devname); |
353632d9 | 3460 | continue; /* Cannot seek */ |
ea0ebe96 | 3461 | } |
2efedc7b | 3462 | /* There should be a duplicate backup superblock 4k before here */ |
06b0d786 | 3463 | if (lseek64(fd, -4096, 1) < 0 || |
0155af90 | 3464 | read(fd, &bsb2, sizeof(bsb2)) != sizeof(bsb2)) |
ea0ebe96 | 3465 | goto second_fail; /* Cannot find leading superblock */ |
e9e43ec3 N |
3466 | if (bsb.magic[15] == '1') |
3467 | bsbsize = offsetof(struct mdp_backup_super, pad1); | |
3468 | else | |
3469 | bsbsize = offsetof(struct mdp_backup_super, pad); | |
ff94fb86 | 3470 | if (memcmp(&bsb2, &bsb, bsbsize) != 0) |
ea0ebe96 | 3471 | goto second_fail; /* Cannot find leading superblock */ |
2efedc7b | 3472 | |
353632d9 NB |
3473 | /* Now need the data offsets for all devices. */ |
3474 | offsets = malloc(sizeof(*offsets)*info->array.raid_disks); | |
3475 | for(j=0; j<info->array.raid_disks; j++) { | |
3476 | if (fdlist[j] < 0) | |
3477 | continue; | |
3da92f27 | 3478 | if (st->ss->load_super(st, fdlist[j], NULL)) |
353632d9 NB |
3479 | /* FIXME should be this be an error */ |
3480 | continue; | |
a5d85af7 | 3481 | st->ss->getinfo_super(st, &dinfo, NULL); |
3da92f27 | 3482 | st->ss->free_super(st); |
14e5b4d7 | 3483 | offsets[j] = dinfo.data_offset * 512; |
353632d9 NB |
3484 | } |
3485 | printf(Name ": restoring critical section\n"); | |
3486 | ||
3487 | if (restore_stripes(fdlist, offsets, | |
3488 | info->array.raid_disks, | |
3489 | info->new_chunk, | |
3490 | info->new_level, | |
3491 | info->new_layout, | |
06b0d786 | 3492 | fd, __le64_to_cpu(bsb.devstart)*512, |
92dcdf7c | 3493 | __le64_to_cpu(bsb.arraystart)*512, |
2fcb75ae | 3494 | __le64_to_cpu(bsb.length)*512, NULL)) { |
e9e43ec3 | 3495 | /* didn't succeed, so giveup */ |
ea0ebe96 N |
3496 | if (verbose) |
3497 | fprintf(stderr, Name ": Error restoring backup from %s\n", | |
3498 | devname); | |
e9e43ec3 N |
3499 | return 1; |
3500 | } | |
3501 | ||
3502 | if (bsb.magic[15] == '2' && | |
3503 | restore_stripes(fdlist, offsets, | |
3504 | info->array.raid_disks, | |
3505 | info->new_chunk, | |
3506 | info->new_level, | |
3507 | info->new_layout, | |
3508 | fd, __le64_to_cpu(bsb.devstart)*512 + | |
3509 | __le64_to_cpu(bsb.devstart2)*512, | |
92dcdf7c | 3510 | __le64_to_cpu(bsb.arraystart2)*512, |
2fcb75ae | 3511 | __le64_to_cpu(bsb.length2)*512, NULL)) { |
353632d9 | 3512 | /* didn't succeed, so giveup */ |
ea0ebe96 N |
3513 | if (verbose) |
3514 | fprintf(stderr, Name ": Error restoring second backup from %s\n", | |
3515 | devname); | |
2295250a | 3516 | return 1; |
353632d9 NB |
3517 | } |
3518 | ||
e9e43ec3 | 3519 | |
353632d9 NB |
3520 | /* Ok, so the data is restored. Let's update those superblocks. */ |
3521 | ||
10af14c4 N |
3522 | lo = hi = 0; |
3523 | if (bsb.length) { | |
3524 | lo = __le64_to_cpu(bsb.arraystart); | |
3525 | hi = lo + __le64_to_cpu(bsb.length); | |
3526 | } | |
3527 | if (bsb.magic[15] == '2' && bsb.length2) { | |
3528 | unsigned long long lo1, hi1; | |
3529 | lo1 = __le64_to_cpu(bsb.arraystart2); | |
3530 | hi1 = lo1 + __le64_to_cpu(bsb.length2); | |
3531 | if (lo == hi) { | |
3532 | lo = lo1; | |
3533 | hi = hi1; | |
3534 | } else if (lo < lo1) | |
3535 | hi = hi1; | |
3536 | else | |
3537 | lo = lo1; | |
3538 | } | |
3539 | if (lo < hi && | |
3540 | (info->reshape_progress < lo || | |
3541 | info->reshape_progress > hi)) | |
3542 | /* backup does not affect reshape_progress*/ ; | |
3543 | else if (info->delta_disks >= 0) { | |
e9e43ec3 N |
3544 | info->reshape_progress = __le64_to_cpu(bsb.arraystart) + |
3545 | __le64_to_cpu(bsb.length); | |
3546 | if (bsb.magic[15] == '2') { | |
3547 | unsigned long long p2 = __le64_to_cpu(bsb.arraystart2) + | |
3548 | __le64_to_cpu(bsb.length2); | |
3549 | if (p2 > info->reshape_progress) | |
3550 | info->reshape_progress = p2; | |
3551 | } | |
3552 | } else { | |
3553 | info->reshape_progress = __le64_to_cpu(bsb.arraystart); | |
3554 | if (bsb.magic[15] == '2') { | |
3555 | unsigned long long p2 = __le64_to_cpu(bsb.arraystart2); | |
3556 | if (p2 < info->reshape_progress) | |
3557 | info->reshape_progress = p2; | |
3558 | } | |
3559 | } | |
353632d9 NB |
3560 | for (j=0; j<info->array.raid_disks; j++) { |
3561 | if (fdlist[j] < 0) continue; | |
3da92f27 | 3562 | if (st->ss->load_super(st, fdlist[j], NULL)) |
353632d9 | 3563 | continue; |
a5d85af7 | 3564 | st->ss->getinfo_super(st, &dinfo, NULL); |
e9e43ec3 | 3565 | dinfo.reshape_progress = info->reshape_progress; |
3da92f27 | 3566 | st->ss->update_super(st, &dinfo, |
68c7d6d7 NB |
3567 | "_reshape_progress", |
3568 | NULL,0, 0, NULL); | |
3da92f27 NB |
3569 | st->ss->store_super(st, fdlist[j]); |
3570 | st->ss->free_super(st); | |
353632d9 | 3571 | } |
353632d9 NB |
3572 | return 0; |
3573 | } | |
6e9eac4f NB |
3574 | /* Didn't find any backup data, try to see if any |
3575 | * was needed. | |
3576 | */ | |
82f2d6ab N |
3577 | if (info->delta_disks < 0) { |
3578 | /* When shrinking, the critical section is at the end. | |
3579 | * So see if we are before the critical section. | |
3580 | */ | |
3581 | unsigned long long first_block; | |
3582 | nstripe = ostripe = 0; | |
3583 | first_block = 0; | |
3584 | while (ostripe >= nstripe) { | |
3585 | ostripe += info->array.chunk_size / 512; | |
3586 | first_block = ostripe * odata; | |
3587 | nstripe = first_block / ndata / (info->new_chunk/512) * | |
3588 | (info->new_chunk/512); | |
3589 | } | |
3590 | ||
3591 | if (info->reshape_progress >= first_block) | |
3592 | return 0; | |
6e9eac4f | 3593 | } |
82f2d6ab N |
3594 | if (info->delta_disks > 0) { |
3595 | /* See if we are beyond the critical section. */ | |
3596 | unsigned long long last_block; | |
3597 | nstripe = ostripe = 0; | |
3598 | last_block = 0; | |
3599 | while (nstripe >= ostripe) { | |
3600 | nstripe += info->new_chunk / 512; | |
3601 | last_block = nstripe * ndata; | |
3602 | ostripe = last_block / odata / (info->array.chunk_size/512) * | |
3603 | (info->array.chunk_size/512); | |
3604 | } | |
6e9eac4f | 3605 | |
82f2d6ab N |
3606 | if (info->reshape_progress >= last_block) |
3607 | return 0; | |
3608 | } | |
6e9eac4f | 3609 | /* needed to recover critical section! */ |
ea0ebe96 N |
3610 | if (verbose) |
3611 | fprintf(stderr, Name ": Failed to find backup of critical section\n"); | |
2295250a | 3612 | return 1; |
353632d9 | 3613 | } |
e9e43ec3 N |
3614 | |
3615 | int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info, | |
3616 | char *backup_file) | |
3617 | { | |
864a004f AK |
3618 | char buf[40]; |
3619 | char *container = NULL; | |
ef5414b2 | 3620 | int err; |
864a004f | 3621 | |
20a40eca N |
3622 | err = sysfs_set_str(info, NULL, "array_state", "readonly"); |
3623 | if (err) | |
3624 | return err; | |
3625 | if (st->ss->external) { | |
864a004f AK |
3626 | fmt_devname(buf, st->container_dev); |
3627 | container = buf; | |
493f5dd6 | 3628 | freeze(st); |
3db2fdd8 | 3629 | |
f362d22b N |
3630 | if (!mdmon_running(st->container_dev)) |
3631 | start_mdmon(st->container_dev); | |
983fff45 | 3632 | ping_monitor_by_id(st->container_dev); |
f362d22b N |
3633 | |
3634 | ||
3db2fdd8 AK |
3635 | if (info->reshape_active == 2) { |
3636 | int cfd = open_dev(st->container_dev); | |
3637 | if (cfd < 0) | |
3638 | return 1; | |
3639 | st->ss->load_container(st, cfd, container); | |
3640 | close(cfd); | |
493f5dd6 N |
3641 | return reshape_container(container, NULL, |
3642 | st, info, 0, backup_file, | |
3643 | 0, 1); | |
3db2fdd8 | 3644 | } |
864a004f AK |
3645 | } |
3646 | return reshape_array(container, mdfd, "array", st, info, 1, | |
e2e53a2d | 3647 | NULL, backup_file, 0, 0, 1); |
e9e43ec3 | 3648 | } |