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