]>
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 | /* | |
485 | * When reshaping an array we might need to backup some data. | |
486 | * This is written to all spares with a 'super_block' describing it. | |
ff94fb86 | 487 | * The superblock goes 4K from the end of the used space on the |
e86c9dd6 NB |
488 | * device. |
489 | * It if written after the backup is complete. | |
490 | * It has the following structure. | |
491 | */ | |
492 | ||
5fdf37e3 | 493 | static struct mdp_backup_super { |
7236ee7a | 494 | char magic[16]; /* md_backup_data-1 or -2 */ |
e86c9dd6 NB |
495 | __u8 set_uuid[16]; |
496 | __u64 mtime; | |
497 | /* start/sizes in 512byte sectors */ | |
7236ee7a | 498 | __u64 devstart; /* address on backup device/file of data */ |
e86c9dd6 NB |
499 | __u64 arraystart; |
500 | __u64 length; | |
501 | __u32 sb_csum; /* csum of preceeding bytes. */ | |
7236ee7a N |
502 | __u32 pad1; |
503 | __u64 devstart2; /* offset in to data of second section */ | |
504 | __u64 arraystart2; | |
505 | __u64 length2; | |
506 | __u32 sb_csum2; /* csum of preceeding bytes. */ | |
507 | __u8 pad[512-68-32]; | |
5fdf37e3 | 508 | } __attribute__((aligned(512))) bsb, bsb2; |
e86c9dd6 | 509 | |
4411fb17 | 510 | static __u32 bsb_csum(char *buf, int len) |
e86c9dd6 NB |
511 | { |
512 | int i; | |
513 | int csum = 0; | |
514 | for (i=0; i<len; i++) | |
515 | csum = (csum<<3) + buf[0]; | |
516 | return __cpu_to_le32(csum); | |
517 | } | |
518 | ||
d7ca196c N |
519 | static int check_idle(struct supertype *st) |
520 | { | |
521 | /* Check that all member arrays for this container, or the | |
522 | * container of this array, are idle | |
523 | */ | |
524 | int container_dev = (st->container_dev != NoMdDev | |
525 | ? st->container_dev : st->devnum); | |
526 | char container[40]; | |
527 | struct mdstat_ent *ent, *e; | |
528 | int is_idle = 1; | |
529 | ||
530 | fmt_devname(container, container_dev); | |
531 | ent = mdstat_read(0, 0); | |
532 | for (e = ent ; e; e = e->next) { | |
533 | if (!is_container_member(e, container)) | |
534 | continue; | |
535 | if (e->percent >= 0) { | |
536 | is_idle = 0; | |
537 | break; | |
538 | } | |
539 | } | |
540 | free_mdstat(ent); | |
541 | return is_idle; | |
542 | } | |
543 | ||
7f2ba464 | 544 | static int freeze_container(struct supertype *st) |
7236ee7a | 545 | { |
7f2ba464 DW |
546 | int container_dev = (st->container_dev != NoMdDev |
547 | ? st->container_dev : st->devnum); | |
d7ca196c | 548 | char container[40]; |
7f2ba464 | 549 | |
d7ca196c N |
550 | if (!check_idle(st)) |
551 | return -1; | |
552 | ||
553 | fmt_devname(container, container_dev); | |
7f2ba464 DW |
554 | |
555 | if (block_monitor(container, 1)) { | |
556 | fprintf(stderr, Name ": failed to freeze container\n"); | |
557 | return -2; | |
558 | } | |
559 | ||
560 | return 1; | |
561 | } | |
562 | ||
563 | static void unfreeze_container(struct supertype *st) | |
564 | { | |
565 | int container_dev = (st->container_dev != NoMdDev | |
566 | ? st->container_dev : st->devnum); | |
d7ca196c N |
567 | char container[40]; |
568 | ||
569 | fmt_devname(container, container_dev); | |
7f2ba464 DW |
570 | |
571 | unblock_monitor(container, 1); | |
572 | } | |
573 | ||
574 | static int freeze(struct supertype *st) | |
575 | { | |
576 | /* Try to freeze resync/rebuild on this array/container. | |
7236ee7a | 577 | * Return -1 if the array is busy, |
7f2ba464 | 578 | * return -2 container cannot be frozen, |
7236ee7a N |
579 | * return 0 if this kernel doesn't support 'frozen' |
580 | * return 1 if it worked. | |
581 | */ | |
7f2ba464 DW |
582 | if (st->ss->external) |
583 | return freeze_container(st); | |
584 | else { | |
585 | struct mdinfo *sra = sysfs_read(-1, st->devnum, GET_VERSION); | |
586 | int err; | |
815c8a7e | 587 | char buf[20]; |
7f2ba464 DW |
588 | |
589 | if (!sra) | |
590 | return -1; | |
815c8a7e N |
591 | /* Need to clear any 'read-auto' status */ |
592 | if (sysfs_get_str(sra, NULL, "array_state", buf, 20) > 0 && | |
593 | strncmp(buf, "read-auto", 9) == 0) | |
594 | sysfs_set_str(sra, NULL, "array_state", "clean"); | |
595 | ||
7f2ba464 DW |
596 | err = sysfs_freeze_array(sra); |
597 | sysfs_free(sra); | |
598 | return err; | |
599 | } | |
7236ee7a N |
600 | } |
601 | ||
9202b8eb | 602 | static void unfreeze(struct supertype *st) |
7236ee7a | 603 | { |
7f2ba464 DW |
604 | if (st->ss->external) |
605 | return unfreeze_container(st); | |
606 | else { | |
607 | struct mdinfo *sra = sysfs_read(-1, st->devnum, GET_VERSION); | |
608 | ||
609 | if (sra) | |
610 | sysfs_set_str(sra, NULL, "sync_action", "idle"); | |
611 | else | |
612 | fprintf(stderr, Name ": failed to unfreeze array\n"); | |
613 | sysfs_free(sra); | |
614 | } | |
7236ee7a N |
615 | } |
616 | ||
4411fb17 | 617 | static void wait_reshape(struct mdinfo *sra) |
7236ee7a N |
618 | { |
619 | int fd = sysfs_get_fd(sra, NULL, "sync_action"); | |
620 | char action[20]; | |
621 | ||
92a19f1a AK |
622 | if (fd < 0) |
623 | return; | |
624 | ||
625 | while (sysfs_fd_get_str(fd, action, 20) > 0 && | |
626 | strncmp(action, "reshape", 7) == 0) { | |
7236ee7a N |
627 | fd_set rfds; |
628 | FD_ZERO(&rfds); | |
629 | FD_SET(fd, &rfds); | |
630 | select(fd+1, NULL, NULL, &rfds, NULL); | |
92a19f1a AK |
631 | } |
632 | close(fd); | |
7236ee7a | 633 | } |
7bc71196 DW |
634 | |
635 | static int reshape_super(struct supertype *st, long long size, int level, | |
636 | int layout, int chunksize, int raid_disks, | |
41784c88 AK |
637 | int delta_disks, char *backup_file, char *dev, |
638 | int verbose) | |
7bc71196 DW |
639 | { |
640 | /* nothing extra to check in the native case */ | |
641 | if (!st->ss->external) | |
642 | return 0; | |
643 | if (!st->ss->reshape_super || | |
644 | !st->ss->manage_reshape) { | |
645 | fprintf(stderr, Name ": %s metadata does not support reshape\n", | |
646 | st->ss->name); | |
647 | return 1; | |
648 | } | |
649 | ||
650 | return st->ss->reshape_super(st, size, level, layout, chunksize, | |
41784c88 AK |
651 | raid_disks, delta_disks, backup_file, dev, |
652 | verbose); | |
7bc71196 DW |
653 | } |
654 | ||
655 | static void sync_metadata(struct supertype *st) | |
656 | { | |
657 | if (st->ss->external) { | |
76266030 | 658 | if (st->update_tail) { |
7bc71196 | 659 | flush_metadata_updates(st); |
76266030 N |
660 | st->update_tail = &st->updates; |
661 | } else | |
7bc71196 DW |
662 | st->ss->sync_metadata(st); |
663 | } | |
664 | } | |
665 | ||
666 | static int subarray_set_num(char *container, struct mdinfo *sra, char *name, int n) | |
667 | { | |
668 | /* when dealing with external metadata subarrays we need to be | |
669 | * prepared to handle EAGAIN. The kernel may need to wait for | |
670 | * mdmon to mark the array active so the kernel can handle | |
671 | * allocations/writeback when preparing the reshape action | |
672 | * (md_allow_write()). We temporarily disable safe_mode_delay | |
673 | * to close a race with the array_state going clean before the | |
674 | * next write to raid_disks / stripe_cache_size | |
675 | */ | |
676 | char safe[50]; | |
677 | int rc; | |
678 | ||
679 | /* only 'raid_disks' and 'stripe_cache_size' trigger md_allow_write */ | |
5da9ab98 N |
680 | if (!container || |
681 | (strcmp(name, "raid_disks") != 0 && | |
682 | strcmp(name, "stripe_cache_size") != 0)) | |
7bc71196 DW |
683 | return sysfs_set_num(sra, NULL, name, n); |
684 | ||
685 | rc = sysfs_get_str(sra, NULL, "safe_mode_delay", safe, sizeof(safe)); | |
686 | if (rc <= 0) | |
687 | return -1; | |
688 | sysfs_set_num(sra, NULL, "safe_mode_delay", 0); | |
689 | rc = sysfs_set_num(sra, NULL, name, n); | |
690 | if (rc < 0 && errno == EAGAIN) { | |
691 | ping_monitor(container); | |
692 | /* if we get EAGAIN here then the monitor is not active | |
693 | * so stop trying | |
694 | */ | |
695 | rc = sysfs_set_num(sra, NULL, name, n); | |
696 | } | |
697 | sysfs_set_str(sra, NULL, "safe_mode_delay", safe); | |
698 | return rc; | |
699 | } | |
700 | ||
27a1e5b5 N |
701 | int start_reshape(struct mdinfo *sra, int already_running, |
702 | int before_data_disks, int data_disks) | |
47eb4d5a N |
703 | { |
704 | int err; | |
6937e6d2 AK |
705 | unsigned long long sync_max_to_set; |
706 | ||
0f28668b | 707 | sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL); |
6937e6d2 AK |
708 | err = sysfs_set_num(sra, NULL, "suspend_hi", sra->reshape_progress); |
709 | err = err ?: sysfs_set_num(sra, NULL, "suspend_lo", | |
710 | sra->reshape_progress); | |
27a1e5b5 N |
711 | if (before_data_disks <= data_disks) |
712 | sync_max_to_set = sra->reshape_progress / data_disks; | |
713 | else | |
714 | sync_max_to_set = (sra->component_size * data_disks | |
715 | - sra->reshape_progress) / data_disks; | |
1e971e71 | 716 | if (!already_running) |
6937e6d2 AK |
717 | sysfs_set_num(sra, NULL, "sync_min", sync_max_to_set); |
718 | err = err ?: sysfs_set_num(sra, NULL, "sync_max", sync_max_to_set); | |
1e971e71 N |
719 | if (!already_running) |
720 | err = err ?: sysfs_set_str(sra, NULL, "sync_action", "reshape"); | |
47eb4d5a N |
721 | |
722 | return err; | |
723 | } | |
724 | ||
725 | void abort_reshape(struct mdinfo *sra) | |
726 | { | |
727 | sysfs_set_str(sra, NULL, "sync_action", "idle"); | |
728 | sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL); | |
729 | sysfs_set_num(sra, NULL, "suspend_hi", 0); | |
730 | sysfs_set_num(sra, NULL, "suspend_lo", 0); | |
731 | sysfs_set_num(sra, NULL, "sync_min", 0); | |
732 | sysfs_set_str(sra, NULL, "sync_max", "max"); | |
733 | } | |
734 | ||
dfe77a9e KW |
735 | int remove_disks_for_takeover(struct supertype *st, |
736 | struct mdinfo *sra, | |
737 | int layout) | |
62a48395 AK |
738 | { |
739 | int nr_of_copies; | |
740 | struct mdinfo *remaining; | |
741 | int slot; | |
742 | ||
dfe77a9e KW |
743 | if (sra->array.level == 10) |
744 | nr_of_copies = layout & 0xff; | |
745 | else if (sra->array.level == 1) | |
746 | nr_of_copies = sra->array.raid_disks; | |
747 | else | |
748 | return 1; | |
62a48395 AK |
749 | |
750 | remaining = sra->devs; | |
751 | sra->devs = NULL; | |
752 | /* for each 'copy', select one device and remove from the list. */ | |
753 | for (slot = 0; slot < sra->array.raid_disks; slot += nr_of_copies) { | |
754 | struct mdinfo **diskp; | |
755 | int found = 0; | |
756 | ||
757 | /* Find a working device to keep */ | |
758 | for (diskp = &remaining; *diskp ; diskp = &(*diskp)->next) { | |
759 | struct mdinfo *disk = *diskp; | |
760 | ||
761 | if (disk->disk.raid_disk < slot) | |
762 | continue; | |
763 | if (disk->disk.raid_disk >= slot + nr_of_copies) | |
764 | continue; | |
765 | if (disk->disk.state & (1<<MD_DISK_REMOVED)) | |
766 | continue; | |
767 | if (disk->disk.state & (1<<MD_DISK_FAULTY)) | |
768 | continue; | |
769 | if (!(disk->disk.state & (1<<MD_DISK_SYNC))) | |
770 | continue; | |
771 | ||
772 | /* We have found a good disk to use! */ | |
773 | *diskp = disk->next; | |
774 | disk->next = sra->devs; | |
775 | sra->devs = disk; | |
776 | found = 1; | |
777 | break; | |
778 | } | |
779 | if (!found) | |
780 | break; | |
781 | } | |
782 | ||
783 | if (slot < sra->array.raid_disks) { | |
784 | /* didn't find all slots */ | |
785 | struct mdinfo **e; | |
786 | e = &remaining; | |
787 | while (*e) | |
788 | e = &(*e)->next; | |
789 | *e = sra->devs; | |
790 | sra->devs = remaining; | |
791 | return 1; | |
792 | } | |
793 | ||
794 | /* Remove all 'remaining' devices from the array */ | |
795 | while (remaining) { | |
796 | struct mdinfo *sd = remaining; | |
797 | remaining = sd->next; | |
798 | ||
799 | sysfs_set_str(sra, sd, "state", "faulty"); | |
800 | sysfs_set_str(sra, sd, "slot", "none"); | |
d5ca4a23 KW |
801 | /* for external metadata disks should be removed in mdmon */ |
802 | if (!st->ss->external) | |
803 | sysfs_set_str(sra, sd, "state", "remove"); | |
62a48395 AK |
804 | sd->disk.state |= (1<<MD_DISK_REMOVED); |
805 | sd->disk.state &= ~(1<<MD_DISK_SYNC); | |
806 | sd->next = sra->devs; | |
807 | sra->devs = sd; | |
808 | } | |
809 | return 0; | |
810 | } | |
811 | ||
130994cb AK |
812 | void reshape_free_fdlist(int *fdlist, |
813 | unsigned long long *offsets, | |
814 | int size) | |
815 | { | |
816 | int i; | |
817 | ||
818 | for (i = 0; i < size; i++) | |
819 | if (fdlist[i] >= 0) | |
820 | close(fdlist[i]); | |
821 | ||
822 | free(fdlist); | |
823 | free(offsets); | |
824 | } | |
825 | ||
826 | int reshape_prepare_fdlist(char *devname, | |
827 | struct mdinfo *sra, | |
828 | int raid_disks, | |
829 | int nrdisks, | |
830 | unsigned long blocks, | |
831 | char *backup_file, | |
832 | int *fdlist, | |
833 | unsigned long long *offsets) | |
834 | { | |
835 | int d = 0; | |
836 | struct mdinfo *sd; | |
837 | ||
838 | for (d = 0; d <= nrdisks; d++) | |
839 | fdlist[d] = -1; | |
840 | d = raid_disks; | |
841 | for (sd = sra->devs; sd; sd = sd->next) { | |
842 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) | |
843 | continue; | |
844 | if (sd->disk.state & (1<<MD_DISK_SYNC)) { | |
845 | char *dn = map_dev(sd->disk.major, | |
846 | sd->disk.minor, 1); | |
847 | fdlist[sd->disk.raid_disk] | |
848 | = dev_open(dn, O_RDONLY); | |
849 | offsets[sd->disk.raid_disk] = sd->data_offset*512; | |
850 | if (fdlist[sd->disk.raid_disk] < 0) { | |
851 | fprintf(stderr, | |
852 | Name ": %s: cannot open component %s\n", | |
853 | devname, dn ? dn : "-unknown-"); | |
854 | d = -1; | |
855 | goto release; | |
856 | } | |
857 | } else if (backup_file == NULL) { | |
858 | /* spare */ | |
859 | char *dn = map_dev(sd->disk.major, | |
860 | sd->disk.minor, 1); | |
861 | fdlist[d] = dev_open(dn, O_RDWR); | |
862 | offsets[d] = (sd->data_offset + sra->component_size - blocks - 8)*512; | |
863 | if (fdlist[d] < 0) { | |
864 | fprintf(stderr, Name ": %s: cannot open component %s\n", | |
865 | devname, dn ? dn : "-unknown-"); | |
866 | d = -1; | |
867 | goto release; | |
868 | } | |
869 | d++; | |
870 | } | |
871 | } | |
872 | release: | |
873 | return d; | |
874 | } | |
875 | ||
e6e9d47b AK |
876 | int reshape_open_backup_file(char *backup_file, |
877 | int fd, | |
878 | char *devname, | |
879 | long blocks, | |
880 | int *fdlist, | |
a93f87ee N |
881 | unsigned long long *offsets, |
882 | int restart) | |
e6e9d47b AK |
883 | { |
884 | /* Return 1 on success, 0 on any form of failure */ | |
885 | /* need to check backup file is large enough */ | |
886 | char buf[512]; | |
887 | struct stat stb; | |
888 | unsigned int dev; | |
889 | int i; | |
890 | ||
a93f87ee | 891 | *fdlist = open(backup_file, O_RDWR|O_CREAT|(restart ? O_TRUNC : O_EXCL), |
e6e9d47b AK |
892 | S_IRUSR | S_IWUSR); |
893 | *offsets = 8 * 512; | |
894 | if (*fdlist < 0) { | |
895 | fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n", | |
896 | devname, backup_file, strerror(errno)); | |
897 | return 0; | |
898 | } | |
899 | /* Guard against backup file being on array device. | |
900 | * If array is partitioned or if LVM etc is in the | |
901 | * way this will not notice, but it is better than | |
902 | * nothing. | |
903 | */ | |
904 | fstat(*fdlist, &stb); | |
905 | dev = stb.st_dev; | |
906 | fstat(fd, &stb); | |
907 | if (stb.st_rdev == dev) { | |
908 | fprintf(stderr, Name ": backup file must NOT be" | |
909 | " on the array being reshaped.\n"); | |
910 | close(*fdlist); | |
911 | return 0; | |
912 | } | |
913 | ||
914 | memset(buf, 0, 512); | |
ca4fe0bf | 915 | for (i=0; i < blocks + 8 ; i++) { |
e6e9d47b AK |
916 | if (write(*fdlist, buf, 512) != 512) { |
917 | fprintf(stderr, Name ": %s: cannot create" | |
918 | " backup file %s: %s\n", | |
919 | devname, backup_file, strerror(errno)); | |
920 | return 0; | |
921 | } | |
922 | } | |
923 | if (fsync(*fdlist) != 0) { | |
924 | fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n", | |
925 | devname, backup_file, strerror(errno)); | |
926 | return 0; | |
927 | } | |
928 | ||
929 | return 1; | |
930 | } | |
931 | ||
1c009fc2 AK |
932 | unsigned long compute_backup_blocks(int nchunk, int ochunk, |
933 | unsigned int ndata, unsigned int odata) | |
934 | { | |
935 | unsigned long a, b, blocks; | |
936 | /* So how much do we need to backup. | |
937 | * We need an amount of data which is both a whole number of | |
938 | * old stripes and a whole number of new stripes. | |
939 | * So LCM for (chunksize*datadisks). | |
940 | */ | |
941 | a = (ochunk/512) * odata; | |
942 | b = (nchunk/512) * ndata; | |
943 | /* Find GCD */ | |
944 | while (a != b) { | |
945 | if (a < b) | |
946 | b -= a; | |
947 | if (b < a) | |
948 | a -= b; | |
949 | } | |
950 | /* LCM == product / GCD */ | |
951 | blocks = (ochunk/512) * (nchunk/512) * odata * ndata / a; | |
952 | ||
953 | return blocks; | |
954 | } | |
955 | ||
5da9ab98 N |
956 | char *analyse_change(struct mdinfo *info, struct reshape *re) |
957 | { | |
958 | /* Based on the current array state in info->array and | |
959 | * the changes in info->new_* etc, determine: | |
960 | * - whether the change is possible | |
961 | * - Intermediate level/raid_disks/layout | |
962 | * - whether a restriping reshape is needed | |
963 | * - number of sectors in minimum change unit. This | |
964 | * will cover a whole number of stripes in 'before' and | |
965 | * 'after'. | |
966 | * | |
967 | * Return message if the change should be rejected | |
968 | * NULL if the change can be achieved | |
969 | * | |
970 | * This can be called as part of starting a reshape, or | |
971 | * when assembling an array that is undergoing reshape. | |
972 | */ | |
973 | int new_disks; | |
b545e14a N |
974 | /* delta_parity records change in number of devices |
975 | * caused by level change | |
976 | */ | |
977 | int delta_parity = 0; | |
5da9ab98 N |
978 | |
979 | /* If a new level not explicitly given, we assume no-change */ | |
980 | if (info->new_level == UnSet) | |
981 | info->new_level = info->array.level; | |
982 | ||
983 | if (info->new_chunk) | |
984 | switch (info->new_level) { | |
985 | case 0: | |
986 | case 4: | |
987 | case 5: | |
988 | case 6: | |
989 | case 10: | |
990 | /* chunk size is meaningful, must divide component_size | |
991 | * evenly | |
992 | */ | |
993 | if (info->component_size % (info->new_chunk/512)) | |
994 | return "New chunk size does not" | |
995 | " divide component size"; | |
996 | break; | |
997 | default: | |
998 | return "chunk size not meaningful for this level"; | |
999 | } | |
1000 | else | |
1001 | info->new_chunk = info->array.chunk_size; | |
1002 | ||
1003 | switch (info->array.level) { | |
90b60dfa N |
1004 | default: |
1005 | return "Cannot understand this RAID level"; | |
5da9ab98 N |
1006 | case 1: |
1007 | /* RAID1 can convert to RAID1 with different disks, or | |
dfe77a9e KW |
1008 | * raid5 with 2 disks, or |
1009 | * raid0 with 1 disk | |
5da9ab98 | 1010 | */ |
dfe77a9e | 1011 | if (info->new_level == 0) { |
b545e14a N |
1012 | if (info->delta_disks != UnSet && |
1013 | info->delta_disks != 0) | |
1014 | return "Cannot change number of disks " | |
1015 | "with RAID1->RAID0 conversion"; | |
dfe77a9e KW |
1016 | re->level = 0; |
1017 | re->before.data_disks = 1; | |
1018 | re->after.data_disks = 1; | |
1019 | re->before.layout = 0; | |
1020 | re->backup_blocks = 0; | |
1021 | re->parity = 0; | |
1022 | return NULL; | |
1023 | } | |
5da9ab98 N |
1024 | if (info->new_level == 1) { |
1025 | if (info->delta_disks == UnSet) | |
1026 | /* Don't know what to do */ | |
1027 | return "no change requested for Growing RAID1"; | |
1028 | re->level = 1; | |
b4f8e38b | 1029 | re->backup_blocks = 0; |
5da9ab98 N |
1030 | re->parity = 0; |
1031 | return NULL; | |
1032 | } | |
1033 | if (info->array.raid_disks == 2 && | |
446d2a5a | 1034 | info->new_level == 5) { |
5652f2d9 | 1035 | |
5da9ab98 | 1036 | re->level = 5; |
5da9ab98 | 1037 | re->before.data_disks = 1; |
5652f2d9 N |
1038 | if (info->delta_disks != UnSet && |
1039 | info->delta_disks != 0) | |
1040 | re->after.data_disks = 1 + info->delta_disks; | |
1041 | else | |
1042 | re->after.data_disks = 1; | |
1043 | if (re->after.data_disks < 1) | |
1044 | return "Number of disks too small for RAID5"; | |
1045 | ||
5da9ab98 | 1046 | re->before.layout = ALGORITHM_LEFT_SYMMETRIC; |
446d2a5a N |
1047 | info->array.chunk_size = 65536; |
1048 | break; | |
5da9ab98 N |
1049 | } |
1050 | /* Could do some multi-stage conversions, but leave that to | |
1051 | * later. | |
1052 | */ | |
1053 | return "Impossibly level change request for RAID1"; | |
1054 | ||
1055 | case 10: | |
1056 | /* RAID10 can only be converted from near mode to | |
1057 | * RAID0 by removing some devices | |
1058 | */ | |
1059 | if ((info->array.layout & ~0xff) != 0x100) | |
1060 | return "Cannot Grow RAID10 with far/offset layout"; | |
1061 | /* number of devices must be multiple of number of copies */ | |
1062 | if (info->array.raid_disks % (info->array.layout & 0xff)) | |
1063 | return "RAID10 layout too complex for Grow operation"; | |
1064 | ||
1065 | if (info->new_level != 0) | |
1066 | return "RAID10 can only be changed to RAID0"; | |
1067 | new_disks = (info->array.raid_disks | |
1068 | / (info->array.layout & 0xff)); | |
b545e14a | 1069 | if (info->delta_disks == UnSet) |
5da9ab98 N |
1070 | info->delta_disks = (new_disks |
1071 | - info->array.raid_disks); | |
b545e14a | 1072 | |
5da9ab98 N |
1073 | if (info->delta_disks != new_disks - info->array.raid_disks) |
1074 | return "New number of raid-devices impossible for RAID10"; | |
1075 | if (info->new_chunk && | |
1076 | info->new_chunk != info->array.chunk_size) | |
1077 | return "Cannot change chunk-size with RAID10 Grow"; | |
1078 | ||
1079 | /* looks good */ | |
1080 | re->level = 0; | |
1081 | re->parity = 0; | |
1082 | re->before.data_disks = new_disks; | |
031d445c | 1083 | re->after.data_disks = re->before.data_disks; |
5da9ab98 | 1084 | re->before.layout = 0; |
b4f8e38b | 1085 | re->backup_blocks = 0; |
5da9ab98 N |
1086 | return NULL; |
1087 | ||
1088 | case 0: | |
1089 | /* RAID0 can be converted to RAID10, or to RAID456 */ | |
1090 | if (info->new_level == 10) { | |
1091 | if (info->new_layout == UnSet && info->delta_disks == UnSet) { | |
1092 | /* Assume near=2 layout */ | |
1093 | info->new_layout = 0x102; | |
1094 | info->delta_disks = info->array.raid_disks; | |
1095 | } | |
1096 | if (info->new_layout == UnSet) { | |
1097 | int copies = 1 + (info->delta_disks | |
1098 | / info->array.raid_disks); | |
1099 | if (info->array.raid_disks * (copies-1) | |
1100 | != info->delta_disks) | |
1101 | return "Impossible number of devices" | |
1102 | " for RAID0->RAID10"; | |
1103 | info->new_layout = 0x100 + copies; | |
1104 | } | |
1105 | if (info->delta_disks == UnSet) { | |
1106 | int copies = info->new_layout & 0xff; | |
1107 | if (info->new_layout != 0x100 + copies) | |
1108 | return "New layout impossible" | |
1109 | " for RAID0->RAID10";; | |
1110 | info->delta_disks = (copies - 1) * | |
1111 | info->array.raid_disks; | |
1112 | } | |
1113 | if (info->new_chunk && | |
1114 | info->new_chunk != info->array.chunk_size) | |
1115 | return "Cannot change chunk-size with RAID0->RAID10"; | |
1116 | /* looks good */ | |
1117 | re->level = 10; | |
1118 | re->parity = 0; | |
1119 | re->before.data_disks = (info->array.raid_disks + | |
1120 | info->delta_disks); | |
031d445c | 1121 | re->after.data_disks = re->before.data_disks; |
5da9ab98 | 1122 | re->before.layout = info->new_layout; |
b4f8e38b | 1123 | re->backup_blocks = 0; |
5da9ab98 N |
1124 | return NULL; |
1125 | } | |
1126 | ||
1127 | /* RAID0 can also covert to RAID0/4/5/6 by first converting to | |
1128 | * a raid4 style layout of the final level. | |
1129 | */ | |
1130 | switch (info->new_level) { | |
5da9ab98 | 1131 | case 4: |
b545e14a N |
1132 | delta_parity = 1; |
1133 | case 0: | |
5da9ab98 N |
1134 | re->level = 4; |
1135 | re->before.layout = 0; | |
1136 | break; | |
1137 | case 5: | |
b545e14a | 1138 | delta_parity = 1; |
5da9ab98 N |
1139 | re->level = 5; |
1140 | re->before.layout = ALGORITHM_PARITY_N; | |
1141 | break; | |
1142 | case 6: | |
b545e14a | 1143 | delta_parity = 2; |
5da9ab98 N |
1144 | re->level = 6; |
1145 | re->before.layout = ALGORITHM_PARITY_N; | |
1146 | break; | |
1147 | default: | |
1148 | return "Impossible level change requested"; | |
1149 | } | |
1150 | re->before.data_disks = info->array.raid_disks; | |
1151 | /* determining 'after' layout happens outside this 'switch' */ | |
1152 | break; | |
1153 | ||
1154 | case 4: | |
1155 | info->array.layout = ALGORITHM_PARITY_N; | |
1156 | case 5: | |
1157 | switch (info->new_level) { | |
f0cce442 | 1158 | case 0: |
b545e14a | 1159 | delta_parity = -1; |
5da9ab98 N |
1160 | case 4: |
1161 | re->level = info->array.level; | |
1162 | re->before.data_disks = info->array.raid_disks - 1; | |
1163 | re->before.layout = info->array.layout; | |
1164 | break; | |
1165 | case 5: | |
1166 | re->level = 5; | |
1167 | re->before.data_disks = info->array.raid_disks - 1; | |
1168 | re->before.layout = info->array.layout; | |
1169 | break; | |
1170 | case 6: | |
b545e14a | 1171 | delta_parity = 1; |
5da9ab98 N |
1172 | re->level = 6; |
1173 | re->before.data_disks = info->array.raid_disks - 1; | |
1174 | switch (info->array.layout) { | |
1175 | case ALGORITHM_LEFT_ASYMMETRIC: | |
1176 | re->before.layout = ALGORITHM_LEFT_ASYMMETRIC_6; | |
1177 | break; | |
1178 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
1179 | re->before.layout = ALGORITHM_RIGHT_ASYMMETRIC_6; | |
1180 | break; | |
1181 | case ALGORITHM_LEFT_SYMMETRIC: | |
1182 | re->before.layout = ALGORITHM_LEFT_SYMMETRIC_6; | |
1183 | break; | |
1184 | case ALGORITHM_RIGHT_SYMMETRIC: | |
1185 | re->before.layout = ALGORITHM_RIGHT_SYMMETRIC_6; | |
1186 | break; | |
1187 | case ALGORITHM_PARITY_0: | |
1188 | re->before.layout = ALGORITHM_PARITY_0_6; | |
1189 | break; | |
1190 | case ALGORITHM_PARITY_N: | |
1191 | re->before.layout = ALGORITHM_PARITY_N_6; | |
1192 | break; | |
1193 | default: | |
1194 | return "Cannot convert an array with this layout"; | |
1195 | } | |
1196 | break; | |
1197 | case 1: | |
1198 | if (info->array.raid_disks != 2) | |
1199 | return "Can only convert a 2-device array to RAID1"; | |
b545e14a N |
1200 | if (info->delta_disks != UnSet && |
1201 | info->delta_disks != 0) | |
1202 | return "Cannot set raid_disk when " | |
1203 | "converting RAID5->RAID1"; | |
5da9ab98 | 1204 | re->level = 1; |
5da9ab98 N |
1205 | break; |
1206 | default: | |
1207 | return "Impossible level change requested"; | |
1208 | } | |
1209 | break; | |
1210 | case 6: | |
1211 | switch (info->new_level) { | |
1212 | case 4: | |
1213 | case 5: | |
b545e14a | 1214 | delta_parity = -1; |
5da9ab98 N |
1215 | case 6: |
1216 | re->level = 6; | |
1217 | re->before.data_disks = info->array.raid_disks - 2; | |
1218 | re->before.layout = info->array.layout; | |
1219 | break; | |
1220 | default: | |
1221 | return "Impossible level change requested"; | |
1222 | } | |
1223 | break; | |
1224 | } | |
1225 | ||
1226 | /* If we reached here then it looks like a re-stripe is | |
1227 | * happening. We have determined the intermediate level | |
1228 | * and initial raid_disks/layout and stored these in 're'. | |
1229 | * | |
1230 | * We need to deduce the final layout that can be atomically | |
1231 | * converted to the end state. | |
1232 | */ | |
1233 | switch (info->new_level) { | |
1234 | case 0: | |
1235 | /* We can only get to RAID0 from RAID4 or RAID5 | |
1236 | * with appropriate layout and one extra device | |
1237 | */ | |
1238 | if (re->level != 4 && re->level != 5) | |
1239 | return "Cannot covert to RAID0 from this level"; | |
b545e14a | 1240 | |
5da9ab98 N |
1241 | switch (re->level) { |
1242 | case 4: | |
1243 | re->after.layout = 0 ; break; | |
1244 | case 5: | |
1245 | re->after.layout = ALGORITHM_PARITY_N; break; | |
1246 | } | |
1247 | break; | |
1248 | ||
1249 | case 4: | |
1250 | /* We can only get to RAID4 from RAID5 */ | |
1251 | if (re->level != 4 && re->level != 5) | |
1252 | return "Cannot convert to RAID4 from this level"; | |
b545e14a | 1253 | |
5da9ab98 N |
1254 | switch (re->level) { |
1255 | case 4: | |
1256 | re->after.layout = 0 ; break; | |
1257 | case 5: | |
1258 | re->after.layout = ALGORITHM_PARITY_N; break; | |
1259 | } | |
1260 | break; | |
1261 | ||
1262 | case 5: | |
1263 | /* We get to RAID5 for RAID5 or RAID6 */ | |
1264 | if (re->level != 5 && re->level != 6) | |
1265 | return "Cannot convert to RAID5 from this level"; | |
b545e14a | 1266 | |
5da9ab98 N |
1267 | switch (re->level) { |
1268 | case 5: | |
1269 | if (info->new_layout == UnSet) | |
1270 | re->after.layout = re->before.layout; | |
1271 | else | |
1272 | re->after.layout = info->new_layout; | |
1273 | break; | |
1274 | case 6: | |
4a870364 N |
1275 | if (info->new_layout == UnSet) |
1276 | info->new_layout = re->before.layout; | |
1277 | ||
5da9ab98 N |
1278 | /* after.layout needs to be raid6 version of new_layout */ |
1279 | if (info->new_layout == ALGORITHM_PARITY_N) | |
1280 | re->after.layout = ALGORITHM_PARITY_N; | |
1281 | else { | |
1282 | char layout[40]; | |
1283 | char *ls = map_num(r5layout, info->new_layout); | |
1284 | int l; | |
1285 | strcat(strcpy(layout, ls), "-6"); | |
1286 | l = map_name(r6layout, layout); | |
1287 | if (l == UnSet) | |
1288 | return "Cannot find RAID6 layout" | |
1289 | " to convert to"; | |
1290 | re->after.layout = l; | |
1291 | } | |
1292 | } | |
1293 | break; | |
1294 | ||
1295 | case 6: | |
1296 | /* We must already be at level 6 */ | |
1297 | if (re->level != 6) | |
1298 | return "Impossible level change"; | |
5da9ab98 | 1299 | if (info->new_layout == UnSet) |
4a870364 | 1300 | re->after.layout = info->array.layout; |
5da9ab98 N |
1301 | else |
1302 | re->after.layout = info->new_layout; | |
1303 | break; | |
1304 | default: | |
1305 | return "Impossible level change requested"; | |
1306 | } | |
b545e14a N |
1307 | if (info->delta_disks == UnSet) |
1308 | info->delta_disks = delta_parity; | |
1309 | ||
1310 | re->after.data_disks = (re->before.data_disks | |
1311 | + info->delta_disks | |
1312 | - delta_parity); | |
5da9ab98 N |
1313 | switch (re->level) { |
1314 | case 6: re->parity = 2; break; | |
1315 | case 4: | |
1316 | case 5: re->parity = 1; break; | |
1317 | default: re->parity = 0; break; | |
1318 | } | |
1319 | /* So we have a restripe operation, we need to calculate the number | |
1320 | * of blocks per reshape operation. | |
1321 | */ | |
1322 | if (info->new_chunk == 0) | |
1323 | info->new_chunk = info->array.chunk_size; | |
1324 | if (re->after.data_disks == re->before.data_disks && | |
1325 | re->after.layout == re->before.layout && | |
1326 | info->new_chunk == info->array.chunk_size) { | |
1327 | /* Nothing to change */ | |
b4f8e38b | 1328 | re->backup_blocks = 0; |
5da9ab98 N |
1329 | return NULL; |
1330 | } | |
1331 | if (re->after.data_disks == 1 && re->before.data_disks == 1) { | |
446d2a5a | 1332 | /* chunk and layout changes make no difference */ |
b4f8e38b | 1333 | re->backup_blocks = 0; |
5da9ab98 N |
1334 | return NULL; |
1335 | } | |
1336 | ||
1337 | if (re->after.data_disks == re->before.data_disks && | |
1338 | get_linux_version() < 2006032) | |
1339 | return "in-place reshape is not safe before 2.6.32 - sorry."; | |
1340 | ||
1341 | if (re->after.data_disks < re->before.data_disks && | |
1342 | get_linux_version() < 2006030) | |
508ede86 | 1343 | return "reshape to fewer devices is not supported before 2.6.30 - sorry."; |
5da9ab98 | 1344 | |
b4f8e38b | 1345 | re->backup_blocks = compute_backup_blocks( |
5da9ab98 N |
1346 | info->new_chunk, info->array.chunk_size, |
1347 | re->after.data_disks, | |
1348 | re->before.data_disks); | |
1349 | ||
1350 | re->new_size = info->component_size * re->after.data_disks; | |
1351 | return NULL; | |
1352 | } | |
1353 | ||
1354 | static int reshape_array(char *container, int fd, char *devname, | |
1355 | struct supertype *st, struct mdinfo *info, | |
e2e53a2d N |
1356 | int force, struct mddev_dev *devlist, |
1357 | char *backup_file, int quiet, int forked, | |
b76b30e0 | 1358 | int restart, int freeze_reshape); |
493f5dd6 | 1359 | static int reshape_container(char *container, char *devname, |
9ad6f6e6 | 1360 | int mdfd, |
5da9ab98 N |
1361 | struct supertype *st, |
1362 | struct mdinfo *info, | |
1363 | int force, | |
1364 | char *backup_file, | |
b76b30e0 | 1365 | int quiet, int restart, int freeze_reshape); |
1c009fc2 | 1366 | |
06b0d786 | 1367 | int Grow_reshape(char *devname, int fd, int quiet, char *backup_file, |
e86c9dd6 | 1368 | long long size, |
691a36b7 | 1369 | int level, char *layout_str, int chunksize, int raid_disks, |
e2e53a2d | 1370 | struct mddev_dev *devlist, |
ce52f92f | 1371 | int assume_clean, int force) |
e86c9dd6 NB |
1372 | { |
1373 | /* Make some changes in the shape of an array. | |
1374 | * The kernel must support the change. | |
7236ee7a N |
1375 | * |
1376 | * There are three different changes. Each can trigger | |
1377 | * a resync or recovery so we freeze that until we have | |
1378 | * requested everything (if kernel supports freezing - 2.6.30). | |
1379 | * The steps are: | |
1380 | * - change size (i.e. component_size) | |
1381 | * - change level | |
1382 | * - change layout/chunksize/ndisks | |
1383 | * | |
1384 | * The last can require a reshape. It is different on different | |
1385 | * levels so we need to check the level before actioning it. | |
1386 | * Some times the level change needs to be requested after the | |
1387 | * reshape (e.g. raid6->raid5, raid5->raid0) | |
1388 | * | |
e86c9dd6 | 1389 | */ |
5da9ab98 | 1390 | struct mdu_array_info_s array; |
7236ee7a | 1391 | int rv = 0; |
e86c9dd6 | 1392 | struct supertype *st; |
4725bc31 | 1393 | char *subarray = NULL; |
e86c9dd6 | 1394 | |
7236ee7a | 1395 | int frozen; |
7236ee7a | 1396 | int changed = 0; |
7bc71196 | 1397 | char *container = NULL; |
5da9ab98 | 1398 | char container_buf[20]; |
7bc71196 | 1399 | int cfd = -1; |
e86c9dd6 | 1400 | |
e2e53a2d N |
1401 | struct mddev_dev *dv; |
1402 | int added_disks; | |
1403 | ||
5da9ab98 | 1404 | struct mdinfo info; |
7e0f6979 | 1405 | struct mdinfo *sra; |
e86c9dd6 NB |
1406 | |
1407 | if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) { | |
1408 | fprintf(stderr, Name ": %s is not an active md array - aborting\n", | |
1409 | devname); | |
1410 | return 1; | |
1411 | } | |
24d40069 | 1412 | |
9ce510be N |
1413 | if (size >= 0 && |
1414 | (chunksize || level!= UnSet || layout_str || raid_disks)) { | |
1415 | fprintf(stderr, Name ": cannot change component size at the same time " | |
1416 | "as other changes.\n" | |
1417 | " Change size first, then check data is intact before " | |
1418 | "making other changes.\n"); | |
1419 | return 1; | |
1420 | } | |
1421 | ||
24d40069 N |
1422 | if (raid_disks && raid_disks < array.raid_disks && array.level > 1 && |
1423 | get_linux_version() < 2006032 && | |
1424 | !check_env("MDADM_FORCE_FEWER")) { | |
1425 | fprintf(stderr, Name ": reducing the number of devices is not safe before Linux 2.6.32\n" | |
1426 | " Please use a newer kernel\n"); | |
1427 | return 1; | |
1428 | } | |
7bc71196 DW |
1429 | |
1430 | st = super_by_fd(fd, &subarray); | |
1431 | if (!st) { | |
1432 | fprintf(stderr, Name ": Unable to determine metadata format for %s\n", devname); | |
1433 | return 1; | |
1434 | } | |
acab7bb1 N |
1435 | if (raid_disks > st->max_devs) { |
1436 | fprintf(stderr, Name ": Cannot increase raid-disks on this array" | |
1437 | " beyond %d\n", st->max_devs); | |
1438 | return 1; | |
1439 | } | |
7bc71196 DW |
1440 | |
1441 | /* in the external case we need to check that the requested reshape is | |
1442 | * supported, and perform an initial check that the container holds the | |
1443 | * pre-requisite spare devices (mdmon owns final validation) | |
1444 | */ | |
1445 | if (st->ss->external) { | |
1446 | int container_dev; | |
7f2ba464 | 1447 | int rv; |
7bc71196 DW |
1448 | |
1449 | if (subarray) { | |
1450 | container_dev = st->container_dev; | |
1451 | cfd = open_dev_excl(st->container_dev); | |
7bc71196 DW |
1452 | } else { |
1453 | container_dev = st->devnum; | |
1454 | close(fd); | |
1455 | cfd = open_dev_excl(st->devnum); | |
1456 | fd = cfd; | |
1457 | } | |
1458 | if (cfd < 0) { | |
1459 | fprintf(stderr, Name ": Unable to open container for %s\n", | |
1460 | devname); | |
7f2ba464 | 1461 | free(subarray); |
7bc71196 DW |
1462 | return 1; |
1463 | } | |
1464 | ||
5da9ab98 N |
1465 | fmt_devname(container_buf, container_dev); |
1466 | container = container_buf; | |
7bc71196 | 1467 | |
eb744788 AK |
1468 | rv = st->ss->load_container(st, cfd, NULL); |
1469 | ||
7f2ba464 | 1470 | if (rv) { |
7bc71196 DW |
1471 | fprintf(stderr, Name ": Cannot read superblock for %s\n", |
1472 | devname); | |
7f2ba464 | 1473 | free(subarray); |
7bc71196 DW |
1474 | return 1; |
1475 | } | |
1476 | ||
81219e70 LM |
1477 | /* check if operation is supported for metadata handler */ |
1478 | if (st->ss->container_content) { | |
1479 | struct mdinfo *cc = NULL; | |
1480 | struct mdinfo *content = NULL; | |
1481 | ||
1482 | cc = st->ss->container_content(st, subarray); | |
1483 | for (content = cc; content ; content = content->next) { | |
446894ea | 1484 | int allow_reshape = 1; |
81219e70 LM |
1485 | |
1486 | /* check if reshape is allowed based on metadata | |
1487 | * indications stored in content.array.status | |
1488 | */ | |
446894ea N |
1489 | if (content->array.state & (1<<MD_SB_BLOCK_VOLUME)) |
1490 | allow_reshape = 0; | |
1491 | if (content->array.state | |
1492 | & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE)) | |
1493 | allow_reshape = 0; | |
81219e70 LM |
1494 | if (!allow_reshape) { |
1495 | fprintf(stderr, Name | |
1496 | " cannot reshape arrays in" | |
1497 | " container with unsupported" | |
1498 | " metadata: %s(%s)\n", | |
1499 | devname, container_buf); | |
1500 | sysfs_free(cc); | |
1501 | free(subarray); | |
1502 | return 1; | |
1503 | } | |
1504 | } | |
1505 | sysfs_free(cc); | |
1506 | } | |
7bc71196 DW |
1507 | if (mdmon_running(container_dev)) |
1508 | st->update_tail = &st->updates; | |
e2e53a2d | 1509 | } |
691a36b7 | 1510 | |
e2e53a2d N |
1511 | added_disks = 0; |
1512 | for (dv = devlist; dv; dv = dv->next) | |
1513 | added_disks++; | |
691a36b7 | 1514 | if (raid_disks > array.raid_disks && |
e2e53a2d | 1515 | array.spare_disks +added_disks < (raid_disks - array.raid_disks) && |
691a36b7 N |
1516 | !force) { |
1517 | fprintf(stderr, | |
1518 | Name ": Need %d spare%s to avoid degraded array," | |
1519 | " and only have %d.\n" | |
1520 | " Use --force to over-ride this check.\n", | |
1521 | raid_disks - array.raid_disks, | |
1522 | raid_disks - array.raid_disks == 1 ? "" : "s", | |
e2e53a2d | 1523 | array.spare_disks + added_disks); |
691a36b7 | 1524 | return 1; |
7bc71196 DW |
1525 | } |
1526 | ||
3fa436ac ML |
1527 | sra = sysfs_read(fd, 0, GET_LEVEL | GET_DISKS | GET_DEVS |
1528 | | GET_STATE | GET_VERSION); | |
1529 | if (sra) { | |
7f2ba464 | 1530 | if (st->ss->external && subarray == NULL) { |
7bc71196 DW |
1531 | array.level = LEVEL_CONTAINER; |
1532 | sra->array.level = LEVEL_CONTAINER; | |
1533 | } | |
7bc71196 | 1534 | } else { |
b526e52d DW |
1535 | fprintf(stderr, Name ": failed to read sysfs parameters for %s\n", |
1536 | devname); | |
1537 | return 1; | |
1538 | } | |
7f2ba464 DW |
1539 | frozen = freeze(st); |
1540 | if (frozen < -1) { | |
1541 | /* freeze() already spewed the reason */ | |
8e61e0d7 | 1542 | sysfs_free(sra); |
7f2ba464 DW |
1543 | return 1; |
1544 | } else if (frozen < 0) { | |
7236ee7a N |
1545 | fprintf(stderr, Name ": %s is performing resync/recovery and cannot" |
1546 | " be reshaped\n", devname); | |
8e61e0d7 | 1547 | sysfs_free(sra); |
7236ee7a N |
1548 | return 1; |
1549 | } | |
1550 | ||
1551 | /* ========= set size =============== */ | |
1552 | if (size >= 0 && (size == 0 || size != array.size)) { | |
85f10287 | 1553 | long long orig_size = get_component_size(fd)/2; |
20a46756 | 1554 | long long min_csize; |
d1537ed1 | 1555 | struct mdinfo *mdi; |
7bc71196 | 1556 | |
85f10287 N |
1557 | if (orig_size == 0) |
1558 | orig_size = array.size; | |
1559 | ||
41784c88 AK |
1560 | if (reshape_super(st, size, UnSet, UnSet, 0, 0, UnSet, NULL, |
1561 | devname, !quiet)) { | |
7bc71196 DW |
1562 | rv = 1; |
1563 | goto release; | |
1564 | } | |
1565 | sync_metadata(st); | |
d1537ed1 N |
1566 | |
1567 | /* Update the size of each member device in case | |
1568 | * they have been resized. This will never reduce | |
1569 | * below the current used-size. The "size" attribute | |
20a46756 | 1570 | * understands '0' to mean 'max'. |
d1537ed1 | 1571 | */ |
20a46756 N |
1572 | min_csize = 0; |
1573 | for (mdi = sra->devs; mdi; mdi = mdi->next) { | |
1574 | if (sysfs_set_num(sra, mdi, "size", size) < 0) | |
1575 | break; | |
1576 | if (array.not_persistent == 0 && | |
1577 | array.major_version == 0 && | |
1578 | get_linux_version() < 3001000) { | |
1579 | /* Dangerous to allow size to exceed 2TB */ | |
1580 | unsigned long long csize; | |
1581 | if (sysfs_get_ll(sra, mdi, "size", &csize) == 0) { | |
1582 | if (csize >= 2ULL*1024*1024*1024) | |
1583 | csize = 2ULL*1024*1024*1024; | |
1584 | if ((min_csize == 0 || (min_csize | |
1585 | > (long long)csize))) | |
1586 | min_csize = csize; | |
1587 | } | |
1588 | } | |
1589 | } | |
1590 | if (min_csize && size > min_csize) { | |
1591 | fprintf(stderr, Name ": Cannot safely make this array " | |
1592 | "use more than 2TB per device on this kernel.\n"); | |
1593 | rv = 1; | |
1594 | goto release; | |
1595 | } | |
1596 | if (min_csize && size == 0) { | |
1597 | /* Don't let the kernel choose a size - it will get | |
1598 | * it wrong | |
1599 | */ | |
1600 | fprintf(stderr, Name ": Limited v0.90 array to " | |
1601 | "2TB per device\n"); | |
1602 | size = min_csize; | |
1603 | } | |
d1537ed1 | 1604 | |
7236ee7a N |
1605 | array.size = size; |
1606 | if (array.size != size) { | |
1607 | /* got truncated to 32bit, write to | |
1608 | * component_size instead | |
1609 | */ | |
1610 | if (sra) | |
1611 | rv = sysfs_set_num(sra, NULL, | |
1612 | "component_size", size); | |
1613 | else | |
1614 | rv = -1; | |
1615 | } else | |
1616 | rv = ioctl(fd, SET_ARRAY_INFO, &array); | |
1617 | if (rv != 0) { | |
39bbb392 | 1618 | int err = errno; |
7bc71196 DW |
1619 | |
1620 | /* restore metadata */ | |
1621 | if (reshape_super(st, orig_size, UnSet, UnSet, 0, 0, | |
41784c88 | 1622 | UnSet, NULL, devname, !quiet) == 0) |
7bc71196 | 1623 | sync_metadata(st); |
7236ee7a | 1624 | fprintf(stderr, Name ": Cannot set device size for %s: %s\n", |
39bbb392 N |
1625 | devname, strerror(err)); |
1626 | if (err == EBUSY && | |
1627 | (array.state & (1<<MD_SB_BITMAP_PRESENT))) | |
1628 | fprintf(stderr, " Bitmap must be removed before size can be changed\n"); | |
7236ee7a N |
1629 | rv = 1; |
1630 | goto release; | |
1631 | } | |
ce52f92f | 1632 | if (assume_clean) { |
6cbf8fb8 | 1633 | /* This will fail on kernels newer than 3.0 unless |
ce52f92f N |
1634 | * a backport has been arranged. |
1635 | */ | |
1636 | if (sra == NULL || | |
1637 | sysfs_set_str(sra, NULL, "resync_start", "none") < 0) | |
1638 | fprintf(stderr, Name ": --assume-clean not support with --grow on this kernel\n"); | |
1639 | } | |
7236ee7a | 1640 | ioctl(fd, GET_ARRAY_INFO, &array); |
be1cabbd | 1641 | size = get_component_size(fd)/2; |
f98841b3 N |
1642 | if (size == 0) |
1643 | size = array.size; | |
85f10287 N |
1644 | if (!quiet) { |
1645 | if (size == orig_size) | |
1646 | fprintf(stderr, Name ": component size of %s " | |
1647 | "unchanged at %lluK\n", | |
1648 | devname, size); | |
1649 | else | |
1650 | fprintf(stderr, Name ": component size of %s " | |
1651 | "has been set to %lluK\n", | |
1652 | devname, size); | |
1653 | } | |
7236ee7a | 1654 | changed = 1; |
7bc71196 | 1655 | } else if (array.level != LEVEL_CONTAINER) { |
be1cabbd | 1656 | size = get_component_size(fd)/2; |
f98841b3 N |
1657 | if (size == 0) |
1658 | size = array.size; | |
7236ee7a N |
1659 | } |
1660 | ||
d515d27f N |
1661 | /* See if there is anything else to do */ |
1662 | if ((level == UnSet || level == array.level) && | |
1663 | (layout_str == NULL) && | |
1664 | (chunksize == 0 || chunksize == array.chunk_size) && | |
1665 | (raid_disks == 0 || raid_disks == array.raid_disks)) { | |
1666 | /* Nothing more to do */ | |
1667 | if (!changed && !quiet) | |
1668 | fprintf(stderr, Name ": %s: no change requested\n", | |
1669 | devname); | |
1670 | goto release; | |
1671 | } | |
1672 | ||
dfe77a9e | 1673 | /* ========= check for Raid10/Raid1 -> Raid0 conversion =============== |
5da9ab98 | 1674 | * current implementation assumes that following conditions must be met: |
dfe77a9e KW |
1675 | * - RAID10: |
1676 | * - far_copies == 1 | |
1677 | * - near_copies == 2 | |
62a48395 | 1678 | */ |
dfe77a9e KW |
1679 | if ((level == 0 && array.level == 10 && sra && |
1680 | array.layout == ((1 << 8) + 2) && !(array.raid_disks & 1)) || | |
1681 | (level == 0 && array.level == 1 && sra)) { | |
62a48395 | 1682 | int err; |
dfe77a9e | 1683 | err = remove_disks_for_takeover(st, sra, array.layout); |
62a48395 AK |
1684 | if (err) { |
1685 | dprintf(Name": Array cannot be reshaped\n"); | |
62a48395 AK |
1686 | if (cfd > -1) |
1687 | close(cfd); | |
5da9ab98 N |
1688 | rv = 1; |
1689 | goto release; | |
7236ee7a | 1690 | } |
fde139b9 N |
1691 | /* Make sure mdmon has seen the device removal |
1692 | * and updated metadata before we continue with | |
1693 | * level change | |
1694 | */ | |
1695 | if (container) | |
1696 | ping_monitor(container); | |
7236ee7a N |
1697 | } |
1698 | ||
8ff6d094 | 1699 | memset(&info, 0, sizeof(info)); |
5da9ab98 | 1700 | info.array = array; |
7443ee81 | 1701 | sysfs_init(&info, fd, NoMdDev); |
3fa436ac | 1702 | strcpy(info.text_version, sra->text_version); |
7443ee81 | 1703 | info.component_size = size*2; |
5da9ab98 N |
1704 | info.new_level = level; |
1705 | info.new_chunk = chunksize * 1024; | |
eff4954d N |
1706 | if (info.array.level == LEVEL_CONTAINER) { |
1707 | info.delta_disks = UnSet; | |
1708 | info.array.raid_disks = raid_disks; | |
1709 | } else if (raid_disks) | |
5da9ab98 N |
1710 | info.delta_disks = raid_disks - info.array.raid_disks; |
1711 | else | |
1712 | info.delta_disks = UnSet; | |
1713 | if (layout_str == NULL) { | |
1714 | info.new_layout = UnSet; | |
1715 | if (info.array.level == 6 && | |
1716 | (info.new_level == 6 || info.new_level == UnSet) && | |
1717 | info.array.layout >= 16) { | |
7236ee7a | 1718 | fprintf(stderr, Name |
5da9ab98 N |
1719 | ": %s has a non-standard layout. If you" |
1720 | " wish to preserve this\n" | |
1721 | " during the reshape, please specify" | |
1722 | " --layout=preserve\n" | |
1723 | " If you want to change it, specify a" | |
1724 | " layout or use --layout=normalise\n", | |
7236ee7a N |
1725 | devname); |
1726 | rv = 1; | |
1727 | goto release; | |
1728 | } | |
5da9ab98 N |
1729 | } else if (strcmp(layout_str, "normalise") == 0 || |
1730 | strcmp(layout_str, "normalize") == 0) { | |
1731 | /* If we have a -6 RAID6 layout, remove the '-6'. */ | |
1732 | info.new_layout = UnSet; | |
1733 | if (info.array.level == 6 && info.new_level == UnSet) { | |
1734 | char l[40], *h; | |
1735 | strcpy(l, map_num(r6layout, info.array.layout)); | |
1736 | h = strrchr(l, '-'); | |
1737 | if (h && strcmp(h, "-6") == 0) { | |
1738 | *h = 0; | |
1739 | info.new_layout = map_name(r6layout, l); | |
7236ee7a N |
1740 | } |
1741 | } | |
5da9ab98 N |
1742 | } else if (strcmp(layout_str, "preserve") == 0) { |
1743 | info.new_layout = UnSet; | |
1744 | } else { | |
1745 | int l = info.new_level; | |
1746 | if (l == UnSet) | |
1747 | l = info.array.level; | |
1748 | switch (l) { | |
1749 | case 5: | |
1750 | info.new_layout = map_name(r5layout, layout_str); | |
1751 | break; | |
1752 | case 6: | |
1753 | info.new_layout = map_name(r6layout, layout_str); | |
1754 | break; | |
1755 | case 10: | |
1756 | info.new_layout = parse_layout_10(layout_str); | |
1757 | break; | |
1758 | case LEVEL_FAULTY: | |
1759 | info.new_layout = parse_layout_faulty(layout_str); | |
1760 | break; | |
1761 | default: | |
1762 | fprintf(stderr, Name ": layout not meaningful" | |
1763 | " with this level\n"); | |
7bc71196 DW |
1764 | rv = 1; |
1765 | goto release; | |
1766 | } | |
5da9ab98 N |
1767 | if (info.new_layout == UnSet) { |
1768 | fprintf(stderr, Name ": layout %s not understood" | |
1769 | " for this level\n", | |
1770 | layout_str); | |
1771 | rv = 1; | |
1772 | goto release; | |
7bc71196 | 1773 | } |
7236ee7a N |
1774 | } |
1775 | ||
907ea753 N |
1776 | if (array.level == LEVEL_FAULTY) { |
1777 | if (level != UnSet && level != array.level) { | |
1778 | fprintf(stderr, Name ": cannot change level of Faulty device\n"); | |
1779 | rv =1 ; | |
1780 | } | |
1781 | if (chunksize) { | |
1782 | fprintf(stderr, Name ": cannot set chunksize of Faulty device\n"); | |
1783 | rv =1 ; | |
1784 | } | |
1785 | if (raid_disks && raid_disks != 1) { | |
1786 | fprintf(stderr, Name ": cannot set raid_disks of Faulty device\n"); | |
1787 | rv =1 ; | |
1788 | } | |
1789 | if (layout_str) { | |
1790 | if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) { | |
1791 | dprintf("Cannot get array information.\n"); | |
1792 | goto release; | |
1793 | } | |
1794 | array.layout = info.new_layout; | |
1795 | if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) { | |
1796 | fprintf(stderr, Name ": failed to set new layout\n"); | |
1797 | rv = 1; | |
1798 | } else if (!quiet) | |
1799 | printf("layout for %s set to %d\n", | |
1800 | devname, array.layout); | |
1801 | } | |
1802 | } else if (array.level == LEVEL_CONTAINER) { | |
5da9ab98 N |
1803 | /* This change is to be applied to every array in the |
1804 | * container. This is only needed when the metadata imposes | |
1805 | * restraints of the various arrays in the container. | |
1806 | * Currently we only know that IMSM requires all arrays | |
1807 | * to have the same number of devices so changing the | |
1808 | * number of devices (On-Line Capacity Expansion) must be | |
1809 | * performed at the level of the container | |
1810 | */ | |
9ad6f6e6 | 1811 | rv = reshape_container(container, devname, -1, st, &info, |
b76b30e0 | 1812 | force, backup_file, quiet, 0, 0); |
9202b8eb | 1813 | frozen = 0; |
5da9ab98 | 1814 | } else { |
08f9e34b AK |
1815 | /* get spare devices from external metadata |
1816 | */ | |
1817 | if (st->ss->external) { | |
1818 | struct mdinfo *info2; | |
1819 | ||
1820 | info2 = st->ss->container_content(st, subarray); | |
1821 | if (info2) { | |
1822 | info.array.spare_disks = | |
1823 | info2->array.spare_disks; | |
1824 | sysfs_free(info2); | |
1825 | } | |
1826 | } | |
1827 | ||
5da9ab98 N |
1828 | /* Impose these changes on a single array. First |
1829 | * check that the metadata is OK with the change. */ | |
7bc71196 | 1830 | |
5da9ab98 N |
1831 | if (reshape_super(st, info.component_size, info.new_level, |
1832 | info.new_layout, info.new_chunk, | |
41784c88 | 1833 | info.array.raid_disks, info.delta_disks, |
5da9ab98 | 1834 | backup_file, devname, quiet)) { |
7bc71196 DW |
1835 | rv = 1; |
1836 | goto release; | |
1837 | } | |
5da9ab98 N |
1838 | sync_metadata(st); |
1839 | rv = reshape_array(container, fd, devname, st, &info, force, | |
b76b30e0 | 1840 | devlist, backup_file, quiet, 0, 0, 0); |
9202b8eb | 1841 | frozen = 0; |
5da9ab98 N |
1842 | } |
1843 | release: | |
8e61e0d7 | 1844 | sysfs_free(sra); |
9202b8eb N |
1845 | if (frozen > 0) |
1846 | unfreeze(st); | |
5da9ab98 N |
1847 | return rv; |
1848 | } | |
7bc71196 | 1849 | |
5da9ab98 N |
1850 | static int reshape_array(char *container, int fd, char *devname, |
1851 | struct supertype *st, struct mdinfo *info, | |
e2e53a2d | 1852 | int force, struct mddev_dev *devlist, |
a93f87ee | 1853 | char *backup_file, int quiet, int forked, |
b76b30e0 | 1854 | int restart, int freeze_reshape) |
5da9ab98 N |
1855 | { |
1856 | struct reshape reshape; | |
1857 | int spares_needed; | |
1858 | char *msg; | |
1859 | int orig_level = UnSet; | |
1860 | int disks, odisks; | |
7bc71196 | 1861 | |
5da9ab98 N |
1862 | struct mdu_array_info_s array; |
1863 | char *c; | |
e86c9dd6 | 1864 | |
e2e53a2d N |
1865 | struct mddev_dev *dv; |
1866 | int added_disks; | |
1867 | ||
d152f53e JS |
1868 | int *fdlist = NULL; |
1869 | unsigned long long *offsets = NULL; | |
5da9ab98 N |
1870 | int d; |
1871 | int nrdisks; | |
1872 | int err; | |
da8100ca | 1873 | unsigned long blocks; |
5da9ab98 N |
1874 | unsigned long cache; |
1875 | unsigned long long array_size; | |
1876 | int done; | |
cf6ac177 | 1877 | struct mdinfo *sra = NULL; |
7bc71196 | 1878 | |
9468aeac N |
1879 | /* when reshaping a RAID0, the component_size might be zero. |
1880 | * So try to fix that up. | |
1881 | */ | |
1882 | if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) { | |
1883 | dprintf("Cannot get array information.\n"); | |
1884 | goto release; | |
1885 | } | |
1886 | if (array.level == 0 && info->component_size == 0) { | |
1887 | get_dev_size(fd, NULL, &array_size); | |
1888 | info->component_size = array_size / array.raid_disks; | |
1889 | } | |
1890 | ||
3cb2aed2 N |
1891 | if (info->reshape_active) { |
1892 | int new_level = info->new_level; | |
1893 | info->new_level = UnSet; | |
178b8f35 | 1894 | info->array.raid_disks -= info->delta_disks; |
3cb2aed2 N |
1895 | msg = analyse_change(info, &reshape); |
1896 | info->new_level = new_level; | |
178b8f35 | 1897 | info->array.raid_disks += info->delta_disks; |
fcdfb814 AK |
1898 | if (!restart) |
1899 | /* Make sure the array isn't read-only */ | |
1900 | ioctl(fd, RESTART_ARRAY_RW, 0); | |
3cb2aed2 N |
1901 | } else |
1902 | msg = analyse_change(info, &reshape); | |
5da9ab98 N |
1903 | if (msg) { |
1904 | fprintf(stderr, Name ": %s\n", msg); | |
631d7405 | 1905 | goto release; |
5da9ab98 | 1906 | } |
817ed7d6 N |
1907 | if (restart && |
1908 | (reshape.level != info->array.level || | |
1909 | reshape.before.layout != info->array.layout || | |
384e9be1 AK |
1910 | reshape.before.data_disks + reshape.parity |
1911 | != info->array.raid_disks - info->delta_disks)) { | |
20a40eca N |
1912 | fprintf(stderr, Name ": reshape info is not in native format -" |
1913 | " cannot continue.\n"); | |
1914 | goto release; | |
1915 | } | |
a93f87ee N |
1916 | |
1917 | if (restart) { | |
1918 | /* reshape already started. just skip to monitoring the reshape */ | |
1919 | if (reshape.backup_blocks == 0) | |
1920 | return 0; | |
1921 | goto started; | |
1922 | } | |
a9c3e78f AK |
1923 | /* The container is frozen but the array may not be. |
1924 | * So freeze the array so spares don't get put to the wrong use | |
1925 | * FIXME there should probably be a cleaner separation between | |
1926 | * freeze_array and freeze_container. | |
1927 | */ | |
1928 | sysfs_freeze_array(info); | |
e06c4e59 | 1929 | /* Check we have enough spares to not be degraded */ |
e2e53a2d N |
1930 | added_disks = 0; |
1931 | for (dv = devlist; dv ; dv=dv->next) | |
1932 | added_disks++; | |
5da9ab98 N |
1933 | spares_needed = max(reshape.before.data_disks, |
1934 | reshape.after.data_disks) | |
1935 | + reshape.parity - array.raid_disks; | |
7bc71196 | 1936 | |
88c1a083 | 1937 | if (!force && |
e2e53a2d N |
1938 | info->new_level > 1 && info->array.level > 1 && |
1939 | spares_needed > info->array.spare_disks + added_disks) { | |
5da9ab98 N |
1940 | fprintf(stderr, |
1941 | Name ": Need %d spare%s to avoid degraded array," | |
1942 | " and only have %d.\n" | |
1943 | " Use --force to over-ride this check.\n", | |
1944 | spares_needed, | |
1945 | spares_needed == 1 ? "" : "s", | |
e2e53a2d | 1946 | info->array.spare_disks + added_disks); |
631d7405 | 1947 | goto release; |
7bc71196 | 1948 | } |
e06c4e59 N |
1949 | /* Check we have enough spares to not fail */ |
1950 | spares_needed = max(reshape.before.data_disks, | |
1951 | reshape.after.data_disks) | |
1952 | - array.raid_disks; | |
1953 | if ((info->new_level > 1 || info->new_level == 0) && | |
e2e53a2d | 1954 | spares_needed > info->array.spare_disks +added_disks) { |
e06c4e59 N |
1955 | fprintf(stderr, |
1956 | Name ": Need %d spare%s to create working array," | |
1957 | " and only have %d.\n", | |
1958 | spares_needed, | |
1959 | spares_needed == 1 ? "" : "s", | |
e2e53a2d | 1960 | info->array.spare_disks + added_disks); |
e06c4e59 N |
1961 | goto release; |
1962 | } | |
e86c9dd6 | 1963 | |
eae35f5c | 1964 | if (reshape.level != array.level) { |
5da9ab98 N |
1965 | char *c = map_num(pers, reshape.level); |
1966 | int err; | |
1967 | if (c == NULL) | |
631d7405 | 1968 | goto release; |
e86c9dd6 | 1969 | |
5da9ab98 N |
1970 | err = sysfs_set_str(info, NULL, "level", c); |
1971 | if (err) { | |
1972 | err = errno; | |
1973 | fprintf(stderr, Name ": %s: could not set level to %s\n", | |
1974 | devname, c); | |
1975 | if (err == EBUSY && | |
1976 | (info->array.state & (1<<MD_SB_BITMAP_PRESENT))) | |
1977 | fprintf(stderr, " Bitmap must be removed" | |
1978 | " before level can be changed\n"); | |
631d7405 | 1979 | goto release; |
19678e53 | 1980 | } |
5da9ab98 | 1981 | if (!quiet) |
b6b95155 | 1982 | fprintf(stderr, Name ": level of %s changed to %s\n", |
5da9ab98 | 1983 | devname, c); |
eae35f5c | 1984 | orig_level = array.level; |
3cd4e7c4 | 1985 | sysfs_freeze_array(info); |
e86c9dd6 | 1986 | |
16d4d84e AK |
1987 | if (reshape.level > 0 && st->ss->external) { |
1988 | /* make sure mdmon is aware of the new level */ | |
1989 | if (!mdmon_running(st->container_dev)) | |
1990 | start_mdmon(st->container_dev); | |
1991 | ping_monitor(container); | |
e919fb0a AK |
1992 | if (mdmon_running(st->container_dev) && |
1993 | st->update_tail == NULL) | |
1994 | st->update_tail = &st->updates; | |
16d4d84e | 1995 | } |
5da9ab98 | 1996 | } |
5da9ab98 N |
1997 | /* ->reshape_super might have chosen some spares from the |
1998 | * container that it wants to be part of the new array. | |
1999 | * We can collect them with ->container_content and give | |
2000 | * them to the kernel. | |
2001 | */ | |
2002 | if (st->ss->reshape_super && st->ss->container_content) { | |
2003 | char *subarray = strchr(info->text_version+1, '/')+1; | |
2004 | struct mdinfo *info2 = | |
2005 | st->ss->container_content(st, subarray); | |
2006 | struct mdinfo *d; | |
2007 | ||
2dd0d697 AK |
2008 | if (info2) { |
2009 | sysfs_init(info2, fd, st->devnum); | |
0d5ac3c6 N |
2010 | /* When increasing number of devices, we need to set |
2011 | * new raid_disks before adding these, or they might | |
2012 | * be rejected. | |
2013 | */ | |
2014 | if (reshape.backup_blocks && | |
2015 | reshape.after.data_disks > reshape.before.data_disks) | |
2016 | subarray_set_num(container, info2, "raid_disks", | |
2017 | reshape.after.data_disks + | |
2018 | reshape.parity); | |
5da9ab98 N |
2019 | for (d = info2->devs; d; d = d->next) { |
2020 | if (d->disk.state == 0 && | |
2021 | d->disk.raid_disk >= 0) { | |
2022 | /* This is a spare that wants to | |
2023 | * be part of the array. | |
2024 | */ | |
2025 | add_disk(fd, st, info2, d); | |
2026 | } | |
9860f271 | 2027 | } |
2dd0d697 AK |
2028 | sysfs_free(info2); |
2029 | } | |
5da9ab98 | 2030 | } |
e2e53a2d N |
2031 | /* We might have been given some devices to add to the |
2032 | * array. Now that the array has been changed to the right | |
2033 | * level and frozen, we can safely add them. | |
2034 | */ | |
2035 | if (devlist) | |
2036 | Manage_subdevs(devname, fd, devlist, !quiet, | |
11b391ec | 2037 | 0,NULL, 0); |
1686dc25 | 2038 | |
b4f8e38b | 2039 | if (reshape.backup_blocks == 0) { |
5da9ab98 N |
2040 | /* No restriping needed, but we might need to impose |
2041 | * some more changes: layout, raid_disks, chunk_size | |
e86c9dd6 | 2042 | */ |
24aebf3a KW |
2043 | /* read current array info */ |
2044 | if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) { | |
83732c28 | 2045 | dprintf("Cannot get array information.\n"); |
24aebf3a KW |
2046 | goto release; |
2047 | } | |
2048 | /* compare current array info with new values and if | |
2049 | * it is different update them to new */ | |
5da9ab98 | 2050 | if (info->new_layout != UnSet && |
24aebf3a KW |
2051 | info->new_layout != array.layout) { |
2052 | array.layout = info->new_layout; | |
2053 | if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) { | |
5da9ab98 | 2054 | fprintf(stderr, Name ": failed to set new layout\n"); |
631d7405 | 2055 | goto release; |
5da9ab98 N |
2056 | } else if (!quiet) |
2057 | printf("layout for %s set to %d\n", | |
24aebf3a | 2058 | devname, array.layout); |
5da9ab98 N |
2059 | } |
2060 | if (info->delta_disks != UnSet && | |
24aebf3a KW |
2061 | info->delta_disks != 0 && |
2062 | array.raid_disks != (info->array.raid_disks + info->delta_disks)) { | |
2063 | array.raid_disks += info->delta_disks; | |
2064 | if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) { | |
5da9ab98 | 2065 | fprintf(stderr, Name ": failed to set raid disks\n"); |
631d7405 | 2066 | goto release; |
24aebf3a | 2067 | } else if (!quiet) { |
5da9ab98 | 2068 | printf("raid_disks for %s set to %d\n", |
24aebf3a KW |
2069 | devname, array.raid_disks); |
2070 | } | |
5da9ab98 N |
2071 | } |
2072 | if (info->new_chunk != 0 && | |
24aebf3a | 2073 | info->new_chunk != array.chunk_size) { |
5da9ab98 N |
2074 | if (sysfs_set_num(info, NULL, |
2075 | "chunk_size", info->new_chunk) != 0) { | |
2076 | fprintf(stderr, Name ": failed to set chunk size\n"); | |
631d7405 | 2077 | goto release; |
5da9ab98 N |
2078 | } else if (!quiet) |
2079 | printf("chunk size for %s set to %d\n", | |
24aebf3a | 2080 | devname, array.chunk_size); |
4725bc31 | 2081 | } |
e35b189b | 2082 | unfreeze(st); |
631d7405 | 2083 | return 0; |
5da9ab98 | 2084 | } |
19678e53 | 2085 | |
5da9ab98 N |
2086 | /* |
2087 | * There are three possibilities. | |
2088 | * 1/ The array will shrink. | |
2089 | * We need to ensure the reshape will pause before reaching | |
2090 | * the 'critical section'. We also need to fork and wait for | |
2091 | * that to happen. When it does we | |
2092 | * suspend/backup/complete/unfreeze | |
2093 | * | |
2094 | * 2/ The array will not change size. | |
2095 | * This requires that we keep a backup of a sliding window | |
2096 | * so that we can restore data after a crash. So we need | |
2097 | * to fork and monitor progress. | |
2098 | * In future we will allow the data_offset to change, so | |
2099 | * a sliding backup becomes unnecessary. | |
2100 | * | |
2101 | * 3/ The array will grow. This is relatively easy. | |
2102 | * However the kernel's restripe routines will cheerfully | |
2103 | * overwrite some early data before it is safe. So we | |
2104 | * need to make a backup of the early parts of the array | |
2105 | * and be ready to restore it if rebuild aborts very early. | |
2106 | * For externally managed metadata, we still need a forked | |
2107 | * child to monitor the reshape and suspend IO over the region | |
2108 | * that is being reshaped. | |
2109 | * | |
2110 | * We backup data by writing it to one spare, or to a | |
2111 | * file which was given on command line. | |
2112 | * | |
2113 | * In each case, we first make sure that storage is available | |
2114 | * for the required backup. | |
2115 | * Then we: | |
2116 | * - request the shape change. | |
2117 | * - fork to handle backup etc. | |
2118 | */ | |
a93f87ee | 2119 | started: |
5da9ab98 N |
2120 | /* Check that we can hold all the data */ |
2121 | get_dev_size(fd, NULL, &array_size); | |
2122 | if (reshape.new_size < (array_size/512)) { | |
2123 | fprintf(stderr, | |
2124 | Name ": this change will reduce the size of the array.\n" | |
2125 | " use --grow --array-size first to truncate array.\n" | |
2126 | " e.g. mdadm --grow %s --array-size %llu\n", | |
2127 | devname, reshape.new_size/2); | |
5da9ab98 N |
2128 | goto release; |
2129 | } | |
7236ee7a | 2130 | |
5da9ab98 | 2131 | sra = sysfs_read(fd, 0, |
3656edcd | 2132 | GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK| |
5da9ab98 | 2133 | GET_CACHE); |
5da9ab98 N |
2134 | if (!sra) { |
2135 | fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n", | |
2136 | devname); | |
5da9ab98 N |
2137 | goto release; |
2138 | } | |
7236ee7a | 2139 | |
5da9ab98 N |
2140 | /* Decide how many blocks (sectors) for a reshape |
2141 | * unit. The number we have so far is just a minimum | |
2142 | */ | |
b4f8e38b | 2143 | blocks = reshape.backup_blocks; |
5da9ab98 N |
2144 | if (reshape.before.data_disks == |
2145 | reshape.after.data_disks) { | |
2146 | /* Make 'blocks' bigger for better throughput, but | |
2147 | * not so big that we reject it below. | |
2148 | * Try for 16 megabytes | |
2149 | */ | |
2150 | while (blocks * 32 < sra->component_size && | |
2151 | blocks < 16*1024*2) | |
2152 | blocks *= 2; | |
2153 | } else | |
2154 | fprintf(stderr, Name ": Need to backup %luK of critical " | |
2155 | "section..\n", blocks/2); | |
2156 | ||
2157 | if (blocks >= sra->component_size/2) { | |
2158 | fprintf(stderr, Name ": %s: Something wrong" | |
2159 | " - reshape aborted\n", | |
2160 | devname); | |
5da9ab98 N |
2161 | goto release; |
2162 | } | |
7236ee7a | 2163 | |
5da9ab98 N |
2164 | /* Now we need to open all these devices so we can read/write. |
2165 | */ | |
b8b286a6 N |
2166 | nrdisks = max(reshape.before.data_disks, |
2167 | reshape.after.data_disks) + reshape.parity | |
2168 | + sra->array.spare_disks; | |
5da9ab98 N |
2169 | fdlist = malloc((1+nrdisks) * sizeof(int)); |
2170 | offsets = malloc((1+nrdisks) * sizeof(offsets[0])); | |
2171 | if (!fdlist || !offsets) { | |
2172 | fprintf(stderr, Name ": malloc failed: grow aborted\n"); | |
5da9ab98 N |
2173 | goto release; |
2174 | } | |
e380d3be | 2175 | |
b8b286a6 N |
2176 | odisks = reshape.before.data_disks + reshape.parity; |
2177 | d = reshape_prepare_fdlist(devname, sra, odisks, | |
5da9ab98 N |
2178 | nrdisks, blocks, backup_file, |
2179 | fdlist, offsets); | |
2180 | if (d < 0) { | |
5da9ab98 N |
2181 | goto release; |
2182 | } | |
13c37ad3 AK |
2183 | if ((st->ss->manage_reshape == NULL) || |
2184 | (st->ss->recover_backup == NULL)) { | |
2185 | if (backup_file == NULL) { | |
2186 | if (reshape.after.data_disks <= | |
2187 | reshape.before.data_disks) { | |
2188 | fprintf(stderr, Name ": %s: Cannot grow - " | |
2189 | "need backup-file\n", devname); | |
2190 | goto release; | |
2191 | } else if (sra->array.spare_disks == 0) { | |
2192 | fprintf(stderr, Name ": %s: Cannot grow - " | |
2193 | "need a spare or backup-file to backup " | |
2194 | "critical section\n", devname); | |
2195 | goto release; | |
2196 | } | |
2197 | } else { | |
2198 | if (!reshape_open_backup_file(backup_file, fd, devname, | |
2199 | (signed)blocks, | |
2200 | fdlist+d, offsets+d, | |
2201 | restart)) { | |
2202 | goto release; | |
2203 | } | |
2204 | d++; | |
e86c9dd6 | 2205 | } |
5da9ab98 | 2206 | } |
7236ee7a | 2207 | |
5da9ab98 N |
2208 | /* lastly, check that the internal stripe cache is |
2209 | * large enough, or it won't work. | |
2210 | * It must hold at least 4 stripes of the larger | |
2211 | * chunk size | |
2212 | */ | |
2213 | cache = max(info->array.chunk_size, info->new_chunk); | |
2214 | cache *= 4; /* 4 stripes minimum */ | |
2215 | cache /= 512; /* convert to sectors */ | |
2216 | disks = min(reshape.before.data_disks, reshape.after.data_disks); | |
2217 | /* make sure there is room for 'blocks' with a bit to spare */ | |
2218 | if (cache < 16 + blocks / disks) | |
2219 | cache = 16 + blocks / disks; | |
2220 | cache /= (4096/512); /* Covert from sectors to pages */ | |
2221 | ||
2222 | if (sra->cache_size < cache) | |
2223 | subarray_set_num(container, sra, "stripe_cache_size", | |
2224 | cache+1); | |
2225 | ||
2226 | /* Right, everything seems fine. Let's kick things off. | |
2227 | * If only changing raid_disks, use ioctl, else use | |
2228 | * sysfs. | |
2229 | */ | |
2230 | sync_metadata(st); | |
2231 | ||
b4f8e38b | 2232 | sra->new_chunk = info->new_chunk; |
ddee071d | 2233 | |
20a40eca | 2234 | if (restart) |
ddee071d | 2235 | sra->reshape_progress = info->reshape_progress; |
f897078e N |
2236 | else { |
2237 | sra->reshape_progress = 0; | |
2238 | if (reshape.after.data_disks < reshape.before.data_disks) | |
2239 | /* start from the end of the new array */ | |
2240 | sra->reshape_progress = (sra->component_size | |
2241 | * reshape.after.data_disks); | |
2242 | } | |
2243 | ||
2244 | if (info->array.chunk_size == info->new_chunk && | |
7477d513 AK |
2245 | reshape.before.layout == reshape.after.layout && |
2246 | st->ss->external == 0) { | |
f897078e | 2247 | /* use SET_ARRAY_INFO but only if reshape hasn't started */ |
6ef421be | 2248 | ioctl(fd, GET_ARRAY_INFO, &array); |
5da9ab98 | 2249 | array.raid_disks = reshape.after.data_disks + reshape.parity; |
20a40eca | 2250 | if (!restart && |
f897078e | 2251 | ioctl(fd, SET_ARRAY_INFO, &array) != 0) { |
5da9ab98 | 2252 | int err = errno; |
631d7405 | 2253 | |
5da9ab98 N |
2254 | fprintf(stderr, |
2255 | Name ": Cannot set device shape for %s: %s\n", | |
2256 | devname, strerror(errno)); | |
7bc71196 | 2257 | |
5da9ab98 N |
2258 | if (err == EBUSY && |
2259 | (array.state & (1<<MD_SB_BITMAP_PRESENT))) | |
2260 | fprintf(stderr, | |
2261 | " Bitmap must be removed before" | |
2262 | " shape can be changed\n"); | |
b5420ef3 | 2263 | |
5da9ab98 N |
2264 | goto release; |
2265 | } | |
20a40eca | 2266 | } else if (!restart) { |
5da9ab98 | 2267 | /* set them all just in case some old 'new_*' value |
f897078e | 2268 | * persists from some earlier problem. |
7236ee7a | 2269 | */ |
631d7405 | 2270 | int err = 0; |
5da9ab98 | 2271 | if (sysfs_set_num(sra, NULL, "chunk_size", info->new_chunk) < 0) |
631d7405 N |
2272 | err = errno; |
2273 | if (!err && sysfs_set_num(sra, NULL, "layout", | |
5da9ab98 | 2274 | reshape.after.layout) < 0) |
631d7405 N |
2275 | err = errno; |
2276 | if (!err && subarray_set_num(container, sra, "raid_disks", | |
5da9ab98 N |
2277 | reshape.after.data_disks + |
2278 | reshape.parity) < 0) | |
631d7405 N |
2279 | err = errno; |
2280 | if (err) { | |
5da9ab98 N |
2281 | fprintf(stderr, Name ": Cannot set device shape for %s\n", |
2282 | devname); | |
7236ee7a | 2283 | |
5da9ab98 N |
2284 | if (err == EBUSY && |
2285 | (array.state & (1<<MD_SB_BITMAP_PRESENT))) | |
2286 | fprintf(stderr, | |
2287 | " Bitmap must be removed before" | |
2288 | " shape can be changed\n"); | |
2289 | goto release; | |
e86c9dd6 | 2290 | } |
5da9ab98 | 2291 | } |
e86c9dd6 | 2292 | |
27a1e5b5 N |
2293 | err = start_reshape(sra, restart, reshape.before.data_disks, |
2294 | reshape.after.data_disks); | |
fab32c97 | 2295 | if (err) { |
20a40eca N |
2296 | fprintf(stderr, |
2297 | Name ": Cannot %s reshape for %s\n", | |
2298 | restart ? "continue" : "start", | |
fab32c97 AK |
2299 | devname); |
2300 | goto release; | |
2301 | } | |
a93f87ee N |
2302 | if (restart) |
2303 | sysfs_set_str(sra, NULL, "array_state", "active"); | |
b76b30e0 AK |
2304 | if (freeze_reshape) { |
2305 | free(fdlist); | |
2306 | free(offsets); | |
2307 | sysfs_free(sra); | |
2308 | fprintf(stderr, Name ": Reshape has to be continued from" | |
2309 | " location %llu when root fileststem has been mounted\n", | |
2310 | sra->reshape_progress); | |
2311 | return 1; | |
2312 | } | |
5da9ab98 N |
2313 | |
2314 | /* Now we just need to kick off the reshape and watch, while | |
2315 | * handling backups of the data... | |
2316 | * This is all done by a forked background process. | |
2317 | */ | |
2318 | switch(forked ? 0 : fork()) { | |
6b2d630c N |
2319 | case -1: |
2320 | fprintf(stderr, Name ": Cannot run child to monitor reshape: %s\n", | |
2321 | strerror(errno)); | |
6b2d630c | 2322 | abort_reshape(sra); |
631d7405 | 2323 | goto release; |
6b2d630c | 2324 | default: |
d152f53e JS |
2325 | free(fdlist); |
2326 | free(offsets); | |
2327 | sysfs_free(sra); | |
6b2d630c | 2328 | return 0; |
5da9ab98 | 2329 | case 0: |
cc700db3 | 2330 | map_fork(); |
6b2d630c N |
2331 | break; |
2332 | } | |
5da9ab98 | 2333 | |
6b2d630c N |
2334 | close(fd); |
2335 | if (check_env("MDADM_GROW_VERIFY")) | |
2336 | fd = open(devname, O_RDONLY | O_DIRECT); | |
2337 | else | |
2338 | fd = -1; | |
2339 | mlockall(MCL_FUTURE); | |
5da9ab98 | 2340 | |
6b2d630c N |
2341 | if (st->ss->external) { |
2342 | /* metadata handler takes it from here */ | |
2343 | done = st->ss->manage_reshape( | |
2344 | fd, sra, &reshape, st, blocks, | |
2345 | fdlist, offsets, | |
2346 | d - odisks, fdlist+odisks, | |
2347 | offsets+odisks); | |
2348 | } else | |
2349 | done = child_monitor( | |
2350 | fd, sra, &reshape, st, blocks, | |
2351 | fdlist, offsets, | |
2352 | d - odisks, fdlist+odisks, | |
2353 | offsets+odisks); | |
2354 | ||
d152f53e JS |
2355 | free(fdlist); |
2356 | free(offsets); | |
2357 | ||
6b2d630c N |
2358 | if (backup_file && done) |
2359 | unlink(backup_file); | |
2360 | if (!done) { | |
2361 | abort_reshape(sra); | |
2362 | goto out; | |
2363 | } | |
1cbc5680 N |
2364 | |
2365 | if (!st->ss->external && | |
2366 | !(reshape.before.data_disks != reshape.after.data_disks | |
2367 | && info->custom_array_size) && | |
2368 | info->new_level == reshape.level && | |
2369 | !forked) { | |
2370 | /* no need to wait for the reshape to finish as | |
2371 | * there is nothing more to do. | |
2372 | */ | |
d152f53e | 2373 | sysfs_free(sra); |
1cbc5680 N |
2374 | exit(0); |
2375 | } | |
2376 | wait_reshape(sra); | |
2377 | ||
2378 | if (st->ss->external) { | |
2379 | /* Re-load the metadata as much could have changed */ | |
2380 | int cfd = open_dev(st->container_dev); | |
2381 | if (cfd >= 0) { | |
2382 | ping_monitor(container); | |
2383 | st->ss->free_super(st); | |
2384 | st->ss->load_container(st, cfd, container); | |
2385 | close(cfd); | |
2386 | } | |
2387 | } | |
2388 | ||
6b2d630c N |
2389 | /* set new array size if required customer_array_size is used |
2390 | * by this metadata. | |
2391 | */ | |
2392 | if (reshape.before.data_disks != | |
2393 | reshape.after.data_disks && | |
2394 | info->custom_array_size) { | |
2395 | struct mdinfo *info2; | |
2396 | char *subarray = strchr(info->text_version+1, '/')+1; | |
582496b2 | 2397 | |
6b2d630c N |
2398 | info2 = st->ss->container_content(st, subarray); |
2399 | if (info2) { | |
2400 | unsigned long long current_size = 0; | |
2401 | unsigned long long new_size = | |
2402 | info2->custom_array_size/2; | |
2403 | ||
2404 | if (sysfs_get_ll(sra, | |
2405 | NULL, | |
2406 | "array_size", | |
2407 | ¤t_size) == 0 && | |
2408 | new_size > current_size) { | |
2409 | if (sysfs_set_num(sra, NULL, | |
2410 | "array_size", new_size) | |
2411 | < 0) | |
2412 | dprintf("Error: Cannot" | |
2413 | " set array size"); | |
2414 | else | |
2415 | dprintf("Array size " | |
2416 | "changed"); | |
2417 | dprintf(" from %llu to %llu.\n", | |
2418 | current_size, new_size); | |
582496b2 | 2419 | } |
6b2d630c | 2420 | sysfs_free(info2); |
582496b2 | 2421 | } |
6b2d630c | 2422 | } |
582496b2 | 2423 | |
6b2d630c | 2424 | if (info->new_level != reshape.level) { |
582496b2 | 2425 | |
6b2d630c | 2426 | c = map_num(pers, info->new_level); |
1cbc5680 N |
2427 | if (c) { |
2428 | err = sysfs_set_str(sra, NULL, "level", c); | |
2429 | if (err) | |
2430 | fprintf(stderr, Name\ | |
2431 | ": %s: could not set level " | |
2432 | "to %s\n", devname, c); | |
2433 | } | |
e919fb0a AK |
2434 | if (info->new_level == 0) |
2435 | st->update_tail = NULL; | |
7236ee7a | 2436 | } |
6b2d630c | 2437 | out: |
d152f53e | 2438 | sysfs_free(sra); |
6b2d630c N |
2439 | if (forked) |
2440 | return 0; | |
e84f2c00 | 2441 | unfreeze(st); |
6b2d630c | 2442 | exit(0); |
e86c9dd6 | 2443 | |
6b2d630c | 2444 | release: |
d152f53e JS |
2445 | free(fdlist); |
2446 | free(offsets); | |
1cbc5680 | 2447 | if (orig_level != UnSet && sra) { |
7236ee7a N |
2448 | c = map_num(pers, orig_level); |
2449 | if (c && sysfs_set_str(sra, NULL, "level", c) == 0) | |
2450 | fprintf(stderr, Name ": aborting level change\n"); | |
2451 | } | |
d152f53e | 2452 | sysfs_free(sra); |
9202b8eb N |
2453 | if (!forked) |
2454 | unfreeze(st); | |
631d7405 | 2455 | return 1; |
7236ee7a | 2456 | } |
e86c9dd6 | 2457 | |
9ad6f6e6 AK |
2458 | /* mdfd handle is passed to be closed in child process (after fork). |
2459 | */ | |
493f5dd6 | 2460 | int reshape_container(char *container, char *devname, |
9ad6f6e6 | 2461 | int mdfd, |
5da9ab98 N |
2462 | struct supertype *st, |
2463 | struct mdinfo *info, | |
2464 | int force, | |
2465 | char *backup_file, | |
b76b30e0 | 2466 | int quiet, int restart, int freeze_reshape) |
5da9ab98 | 2467 | { |
1bb174ba | 2468 | struct mdinfo *cc = NULL; |
bcc9e9ed | 2469 | int rv = restart; |
2d04d7e5 | 2470 | int last_devnum = -1; |
1bb174ba | 2471 | |
d8eb27f7 KA |
2472 | /* component_size is not meaningful for a container, |
2473 | * so pass '-1' meaning 'no change' | |
2474 | */ | |
493f5dd6 N |
2475 | if (!restart && |
2476 | reshape_super(st, -1, info->new_level, | |
5da9ab98 | 2477 | info->new_layout, info->new_chunk, |
41784c88 | 2478 | info->array.raid_disks, info->delta_disks, |
9e325442 AK |
2479 | backup_file, devname, quiet)) { |
2480 | unfreeze(st); | |
5da9ab98 | 2481 | return 1; |
9e325442 | 2482 | } |
5da9ab98 N |
2483 | |
2484 | sync_metadata(st); | |
2485 | ||
1bb174ba AK |
2486 | /* ping monitor to be sure that update is on disk |
2487 | */ | |
2488 | ping_monitor(container); | |
5da9ab98 N |
2489 | |
2490 | switch (fork()) { | |
2491 | case -1: /* error */ | |
2492 | perror("Cannot fork to complete reshape\n"); | |
9e325442 | 2493 | unfreeze(st); |
5da9ab98 N |
2494 | return 1; |
2495 | default: /* parent */ | |
b76b30e0 AK |
2496 | if (!freeze_reshape) |
2497 | printf(Name ": multi-array reshape continues" | |
2498 | " in background\n"); | |
5da9ab98 N |
2499 | return 0; |
2500 | case 0: /* child */ | |
cc700db3 | 2501 | map_fork(); |
5da9ab98 N |
2502 | break; |
2503 | } | |
2504 | ||
9ad6f6e6 AK |
2505 | /* close unused handle in child process |
2506 | */ | |
2507 | if (mdfd > -1) | |
2508 | close(mdfd); | |
2509 | ||
1bb174ba AK |
2510 | while(1) { |
2511 | /* For each member array with reshape_active, | |
2512 | * we need to perform the reshape. | |
2513 | * We pick the first array that needs reshaping and | |
2514 | * reshape it. reshape_array() will re-read the metadata | |
2515 | * so the next time through a different array should be | |
2516 | * ready for reshape. | |
493f5dd6 N |
2517 | * It is possible that the 'different' array will not |
2518 | * be assembled yet. In that case we simple exit. | |
2519 | * When it is assembled, the mdadm which assembles it | |
2520 | * will take over the reshape. | |
1bb174ba AK |
2521 | */ |
2522 | struct mdinfo *content; | |
5da9ab98 N |
2523 | int fd; |
2524 | struct mdstat_ent *mdstat; | |
5da9ab98 N |
2525 | char *adev; |
2526 | ||
1bb174ba | 2527 | sysfs_free(cc); |
5da9ab98 | 2528 | |
1bb174ba AK |
2529 | cc = st->ss->container_content(st, NULL); |
2530 | ||
2531 | for (content = cc; content ; content = content->next) { | |
2532 | char *subarray; | |
2533 | if (!content->reshape_active) | |
2534 | continue; | |
2535 | ||
2536 | subarray = strchr(content->text_version+1, '/')+1; | |
2537 | mdstat = mdstat_by_subdev(subarray, | |
2538 | devname2devnum(container)); | |
2539 | if (!mdstat) | |
2540 | continue; | |
2541 | break; | |
2542 | } | |
2543 | if (!content) | |
2544 | break; | |
5da9ab98 | 2545 | |
5da9ab98 N |
2546 | adev = map_dev(dev2major(mdstat->devnum), |
2547 | dev2minor(mdstat->devnum), | |
2548 | 0); | |
2549 | if (!adev) | |
1bb174ba AK |
2550 | adev = content->text_version; |
2551 | ||
97a3490c AK |
2552 | fd = open_dev(mdstat->devnum); |
2553 | if (fd < 0) { | |
2554 | printf(Name ": Device %s cannot be opened for reshape.", | |
2555 | adev); | |
2556 | break; | |
2557 | } | |
2558 | ||
2d04d7e5 AK |
2559 | if (last_devnum == mdstat->devnum) { |
2560 | /* Do not allow for multiple reshape_array() calls for | |
2561 | * the same array. | |
2562 | * It can happen when reshape_array() returns without | |
2563 | * error, when reshape is not finished (wrong reshape | |
2564 | * starting/continuation conditions). Mdmon doesn't | |
2565 | * switch to next array in container and reentry | |
2566 | * conditions for the same array occur. | |
2567 | * This is possibly interim until the behaviour of | |
2568 | * reshape_array is resolved(). | |
2569 | */ | |
2570 | printf(Name ": Multiple reshape execution detected for " | |
2571 | "device %s.", adev); | |
2572 | close(fd); | |
2573 | break; | |
2574 | } | |
2575 | last_devnum = mdstat->devnum; | |
2576 | ||
1bb174ba | 2577 | sysfs_init(content, fd, mdstat->devnum); |
5da9ab98 | 2578 | |
1bb174ba | 2579 | rv = reshape_array(container, fd, adev, st, |
e2e53a2d | 2580 | content, force, NULL, |
b76b30e0 AK |
2581 | backup_file, quiet, 1, restart, |
2582 | freeze_reshape); | |
5da9ab98 | 2583 | close(fd); |
b76b30e0 AK |
2584 | |
2585 | if (freeze_reshape) { | |
2586 | sysfs_free(cc); | |
2587 | exit(0); | |
2588 | } | |
2589 | ||
493f5dd6 | 2590 | restart = 0; |
5da9ab98 N |
2591 | if (rv) |
2592 | break; | |
4584621a AK |
2593 | rv = !mdmon_running(devname2devnum(container)); |
2594 | if (rv) { | |
2595 | printf(Name ": Mdmon is not found. " | |
2596 | "Cannot continue container reshape.\n"); | |
2597 | break; | |
2598 | } | |
5da9ab98 | 2599 | } |
bcc9e9ed AK |
2600 | if (!rv) |
2601 | unfreeze(st); | |
1bb174ba | 2602 | sysfs_free(cc); |
5da9ab98 N |
2603 | exit(0); |
2604 | } | |
2605 | ||
7236ee7a N |
2606 | /* |
2607 | * We run a child process in the background which performs the following | |
2608 | * steps: | |
2609 | * - wait for resync to reach a certain point | |
2610 | * - suspend io to the following section | |
2611 | * - backup that section | |
2612 | * - allow resync to proceed further | |
2613 | * - resume io | |
2614 | * - discard the backup. | |
2615 | * | |
2616 | * When are combined in slightly different ways in the three cases. | |
2617 | * Grow: | |
2618 | * - suspend/backup/allow/wait/resume/discard | |
2619 | * Shrink: | |
2620 | * - allow/wait/suspend/backup/allow/wait/resume/discard | |
2621 | * same-size: | |
2622 | * - wait/resume/discard/suspend/backup/allow | |
2623 | * | |
2624 | * suspend/backup/allow always come together | |
2625 | * wait/resume/discard do too. | |
2626 | * For the same-size case we have two backups to improve flow. | |
2627 | * | |
2628 | */ | |
e86c9dd6 | 2629 | |
7443ee81 N |
2630 | int progress_reshape(struct mdinfo *info, struct reshape *reshape, |
2631 | unsigned long long backup_point, | |
2632 | unsigned long long wait_point, | |
2633 | unsigned long long *suspend_point, | |
2634 | unsigned long long *reshape_completed) | |
2635 | { | |
2636 | /* This function is called repeatedly by the reshape manager. | |
2637 | * It determines how much progress can safely be made and allows | |
2638 | * that progress. | |
2639 | * - 'info' identifies the array and particularly records in | |
2640 | * ->reshape_progress the metadata's knowledge of progress | |
2641 | * This is a sector offset from the start of the array | |
2642 | * of the next array block to be relocated. This number | |
2643 | * may increase from 0 or decrease from array_size, depending | |
2644 | * on the type of reshape that is happening. | |
2645 | * Note that in contrast, 'sync_completed' is a block count of the | |
2646 | * reshape so far. It gives the distance between the start point | |
2647 | * (head or tail of device) and the next place that data will be | |
2648 | * written. It always increases. | |
2649 | * - 'reshape' is the structure created by analyse_change | |
2650 | * - 'backup_point' shows how much the metadata manager has backed-up | |
2651 | * data. For reshapes with increasing progress, it is the next address | |
2652 | * to be backed up, previous addresses have been backed-up. For | |
2653 | * decreasing progress, it is the earliest address that has been | |
2654 | * backed up - later address are also backed up. | |
2655 | * So addresses between reshape_progress and backup_point are | |
2656 | * backed up providing those are in the 'correct' order. | |
2657 | * - 'wait_point' is an array address. When reshape_completed | |
2658 | * passes this point, progress_reshape should return. It might | |
2659 | * return earlier if it determines that ->reshape_progress needs | |
2660 | * to be updated or further backup is needed. | |
2661 | * - suspend_point is maintained by progress_reshape and the caller | |
2662 | * should not touch it except to initialise to zero. | |
2663 | * It is an array address and it only increases in 2.6.37 and earlier. | |
b6b95155 | 2664 | * This makes it difficult to handle reducing reshapes with |
7443ee81 N |
2665 | * external metadata. |
2666 | * However: it is similar to backup_point in that it records the | |
2667 | * other end of a suspended region from reshape_progress. | |
2668 | * it is moved to extend the region that is safe to backup and/or | |
2669 | * reshape | |
2670 | * - reshape_completed is read from sysfs and returned. The caller | |
2671 | * should copy this into ->reshape_progress when it has reason to | |
2672 | * believe that the metadata knows this, and any backup outside this | |
2673 | * has been erased. | |
2674 | * | |
2675 | * Return value is: | |
2676 | * 1 if more data from backup_point - but only as far as suspend_point, | |
2677 | * should be backed up | |
2678 | * 0 if things are progressing smoothly | |
9e034f70 N |
2679 | * -1 if the reshape is finished because it is all done, |
2680 | * -2 if the reshape is finished due to an error. | |
7443ee81 N |
2681 | */ |
2682 | ||
2683 | int advancing = (reshape->after.data_disks | |
2684 | >= reshape->before.data_disks); | |
38dad34a N |
2685 | unsigned long long need_backup; /* All data between start of array and |
2686 | * here will at some point need to | |
2687 | * be backed up. | |
d0ab945e | 2688 | */ |
7443ee81 | 2689 | unsigned long long read_offset, write_offset; |
b4f8e38b | 2690 | unsigned long long write_range; |
7443ee81 | 2691 | unsigned long long max_progress, target, completed; |
d0ab945e N |
2692 | unsigned long long array_size = (info->component_size |
2693 | * reshape->before.data_disks); | |
7443ee81 | 2694 | int fd; |
77a73a17 | 2695 | char buf[20]; |
7443ee81 N |
2696 | |
2697 | /* First, we unsuspend any region that is now known to be safe. | |
2698 | * If suspend_point is on the 'wrong' side of reshape_progress, then | |
2699 | * we don't have or need suspension at the moment. This is true for | |
2700 | * native metadata when we don't need to back-up. | |
2701 | */ | |
2702 | if (advancing) { | |
49cd738b | 2703 | if (info->reshape_progress <= *suspend_point) |
7443ee81 N |
2704 | sysfs_set_num(info, NULL, "suspend_lo", |
2705 | info->reshape_progress); | |
2706 | } else { | |
2707 | /* Note: this won't work in 2.6.37 and before. | |
2708 | * Something somewhere should make sure we don't need it! | |
2709 | */ | |
49cd738b | 2710 | if (info->reshape_progress >= *suspend_point) |
7443ee81 N |
2711 | sysfs_set_num(info, NULL, "suspend_hi", |
2712 | info->reshape_progress); | |
2713 | } | |
2714 | ||
2715 | /* Now work out how far it is safe to progress. | |
2716 | * If the read_offset for ->reshape_progress is less than | |
2717 | * 'blocks' beyond the write_offset, we can only progress as far | |
2718 | * as a backup. | |
2719 | * Otherwise we can progress until the write_offset for the new location | |
2720 | * reaches (within 'blocks' of) the read_offset at the current location. | |
2721 | * However that region must be suspended unless we are using native | |
2722 | * metadata. | |
2723 | * If we need to suspend more, we limit it to 128M per device, which is | |
2724 | * rather arbitrary and should be some time-based calculation. | |
2725 | */ | |
ec757320 N |
2726 | read_offset = info->reshape_progress / reshape->before.data_disks; |
2727 | write_offset = info->reshape_progress / reshape->after.data_disks; | |
b4f8e38b | 2728 | write_range = info->new_chunk/512; |
38dad34a N |
2729 | if (reshape->before.data_disks == reshape->after.data_disks) |
2730 | need_backup = array_size; | |
2731 | else | |
2732 | need_backup = reshape->backup_blocks; | |
7443ee81 | 2733 | if (advancing) { |
38dad34a | 2734 | if (read_offset < write_offset + write_range) |
7443ee81 | 2735 | max_progress = backup_point; |
ddee071d | 2736 | else |
7443ee81 | 2737 | max_progress = |
2f3dd5e4 N |
2738 | read_offset * |
2739 | reshape->after.data_disks; | |
7443ee81 | 2740 | } else { |
38dad34a N |
2741 | if (read_offset > write_offset - write_range) |
2742 | /* Can only progress as far as has been backed up, | |
2743 | * which must be suspended */ | |
7443ee81 | 2744 | max_progress = backup_point; |
38dad34a N |
2745 | else if (info->reshape_progress <= need_backup) |
2746 | max_progress = backup_point; | |
2747 | else { | |
2748 | if (info->array.major_version >= 0) | |
2749 | /* Can progress until backup is needed */ | |
2750 | max_progress = need_backup; | |
2751 | else { | |
2752 | /* Can progress until metadata update is required */ | |
2753 | max_progress = | |
2754 | read_offset * | |
2755 | reshape->after.data_disks; | |
2756 | /* but data must be suspended */ | |
2757 | if (max_progress < *suspend_point) | |
2758 | max_progress = *suspend_point; | |
2759 | } | |
7443ee81 N |
2760 | } |
2761 | } | |
2762 | ||
2763 | /* We know it is safe to progress to 'max_progress' providing | |
2764 | * it is suspended or we are using native metadata. | |
2765 | * Consider extending suspend_point 128M per device if it | |
2766 | * is less than 64M per device beyond reshape_progress. | |
2767 | * But always do a multiple of 'blocks' | |
832b2b9a N |
2768 | * FIXME this is too big - it takes to long to complete |
2769 | * this much. | |
7443ee81 N |
2770 | */ |
2771 | target = 64*1024*2 * min(reshape->before.data_disks, | |
2772 | reshape->after.data_disks); | |
b4f8e38b | 2773 | target /= reshape->backup_blocks; |
7443ee81 N |
2774 | if (target < 2) |
2775 | target = 2; | |
b4f8e38b | 2776 | target *= reshape->backup_blocks; |
7443ee81 N |
2777 | |
2778 | /* For externally managed metadata we always need to suspend IO to | |
2779 | * the area being reshaped so we regularly push suspend_point forward. | |
2780 | * For native metadata we only need the suspend if we are going to do | |
2781 | * a backup. | |
2782 | */ | |
2783 | if (advancing) { | |
d0ab945e N |
2784 | if ((need_backup > info->reshape_progress |
2785 | || info->array.major_version < 0) && | |
7443ee81 | 2786 | *suspend_point < info->reshape_progress + target) { |
d0ab945e N |
2787 | if (need_backup < *suspend_point + 2 * target) |
2788 | *suspend_point = need_backup; | |
2789 | else if (*suspend_point + 2 * target < array_size) | |
7443ee81 | 2790 | *suspend_point += 2 * target; |
d0ab945e N |
2791 | else |
2792 | *suspend_point = array_size; | |
7443ee81 | 2793 | sysfs_set_num(info, NULL, "suspend_hi", *suspend_point); |
d0ab945e N |
2794 | if (max_progress > *suspend_point) |
2795 | max_progress = *suspend_point; | |
7443ee81 N |
2796 | } |
2797 | } else { | |
38dad34a N |
2798 | if (info->array.major_version >= 0) { |
2799 | /* Only need to suspend when about to backup */ | |
2800 | if (info->reshape_progress < need_backup * 2 && | |
2801 | *suspend_point > 0) { | |
d0ab945e | 2802 | *suspend_point = 0; |
38dad34a N |
2803 | sysfs_set_num(info, NULL, "suspend_lo", 0); |
2804 | sysfs_set_num(info, NULL, "suspend_hi", need_backup); | |
2805 | } | |
2806 | } else { | |
2807 | /* Need to suspend continually */ | |
2808 | if (info->reshape_progress < *suspend_point) | |
2809 | *suspend_point = info->reshape_progress; | |
2810 | if (*suspend_point + target < info->reshape_progress) | |
2811 | /* No need to move suspend region yet */; | |
2812 | else { | |
2813 | if (*suspend_point >= 2 * target) | |
2814 | *suspend_point -= 2 * target; | |
2815 | else | |
2816 | *suspend_point = 0; | |
2817 | sysfs_set_num(info, NULL, "suspend_lo", | |
2818 | *suspend_point); | |
2819 | } | |
d0ab945e N |
2820 | if (max_progress < *suspend_point) |
2821 | max_progress = *suspend_point; | |
7443ee81 N |
2822 | } |
2823 | } | |
2824 | ||
2825 | /* now set sync_max to allow that progress. sync_max, like | |
2826 | * sync_completed is a count of sectors written per device, so | |
2827 | * we find the difference between max_progress and the start point, | |
2828 | * and divide that by after.data_disks to get a sync_max | |
2829 | * number. | |
2830 | * At the same time we convert wait_point to a similar number | |
2831 | * for comparing against sync_completed. | |
2832 | */ | |
92d1991f | 2833 | /* scale down max_progress to per_disk */ |
7443ee81 | 2834 | max_progress /= reshape->after.data_disks; |
92d1991f N |
2835 | /* Round to chunk size as some kernels give an erroneously high number */ |
2836 | max_progress /= info->new_chunk/512; | |
2837 | max_progress *= info->new_chunk/512; | |
7f913e9b N |
2838 | /* And round to old chunk size as the kernel wants that */ |
2839 | max_progress /= info->array.chunk_size/512; | |
2840 | max_progress *= info->array.chunk_size/512; | |
92d1991f N |
2841 | /* Limit progress to the whole device */ |
2842 | if (max_progress > info->component_size) | |
2843 | max_progress = info->component_size; | |
7443ee81 | 2844 | wait_point /= reshape->after.data_disks; |
92d1991f N |
2845 | if (!advancing) { |
2846 | /* switch from 'device offset' to 'processed block count' */ | |
2847 | max_progress = info->component_size - max_progress; | |
2848 | wait_point = info->component_size - wait_point; | |
2849 | } | |
7443ee81 N |
2850 | |
2851 | sysfs_set_num(info, NULL, "sync_max", max_progress); | |
2852 | ||
2853 | /* Now wait. If we have already reached the point that we were | |
2854 | * asked to wait to, don't wait at all, else wait for any change. | |
2855 | * We need to select on 'sync_completed' as that is the place that | |
2856 | * notifications happen, but we are really interested in | |
2857 | * 'reshape_position' | |
2858 | */ | |
2859 | fd = sysfs_get_fd(info, NULL, "sync_completed"); | |
2860 | if (fd < 0) | |
77a73a17 | 2861 | goto check_progress; |
7443ee81 | 2862 | |
621ea11b | 2863 | if (sysfs_fd_get_ll(fd, &completed) < 0) |
77a73a17 | 2864 | goto check_progress; |
621ea11b | 2865 | |
7443ee81 N |
2866 | while (completed < max_progress && completed < wait_point) { |
2867 | /* Check that sync_action is still 'reshape' to avoid | |
2868 | * waiting forever on a dead array | |
2869 | */ | |
2870 | char action[20]; | |
2871 | fd_set rfds; | |
2872 | if (sysfs_get_str(info, NULL, "sync_action", | |
2873 | action, 20) <= 0 || | |
2874 | strncmp(action, "reshape", 7) != 0) | |
2875 | break; | |
26d6e157 AK |
2876 | /* Some kernels reset 'sync_completed' to zero |
2877 | * before setting 'sync_action' to 'idle'. | |
2878 | * So we need these extra tests. | |
2879 | */ | |
2880 | if (completed == 0 && advancing | |
2881 | && info->reshape_progress > 0) | |
2882 | break; | |
2883 | if (completed == 0 && !advancing | |
2884 | && info->reshape_progress < (info->component_size | |
2885 | * reshape->after.data_disks)) | |
2886 | break; | |
7443ee81 N |
2887 | FD_ZERO(&rfds); |
2888 | FD_SET(fd, &rfds); | |
2889 | select(fd+1, NULL, NULL, &rfds, NULL); | |
621ea11b | 2890 | if (sysfs_fd_get_ll(fd, &completed) < 0) |
77a73a17 | 2891 | goto check_progress; |
7443ee81 | 2892 | } |
10d0d365 AK |
2893 | /* Some kernels reset 'sync_completed' to zero, |
2894 | * we need to have real point we are in md | |
2895 | */ | |
2896 | if (completed == 0) | |
2897 | completed = max_progress; | |
2898 | ||
92d1991f N |
2899 | /* some kernels can give an incorrectly high 'completed' number */ |
2900 | completed /= (info->new_chunk/512); | |
2901 | completed *= (info->new_chunk/512); | |
7443ee81 N |
2902 | /* Convert 'completed' back in to a 'progress' number */ |
2903 | completed *= reshape->after.data_disks; | |
2904 | if (!advancing) { | |
2905 | completed = info->component_size * reshape->after.data_disks | |
2906 | - completed; | |
2907 | } | |
2908 | *reshape_completed = completed; | |
2909 | ||
2910 | close(fd); | |
2911 | ||
2912 | /* We return the need_backup flag. Caller will decide | |
d0ab945e | 2913 | * how much - a multiple of ->backup_blocks up to *suspend_point |
7443ee81 | 2914 | */ |
38dad34a N |
2915 | if (advancing) |
2916 | return need_backup > info->reshape_progress; | |
2917 | else | |
2918 | return need_backup >= info->reshape_progress; | |
77a73a17 N |
2919 | |
2920 | check_progress: | |
2921 | /* if we couldn't read a number from sync_completed, then | |
2922 | * either the reshape did complete, or it aborted. | |
2923 | * We can tell which by checking for 'none' in reshape_position. | |
621ea11b N |
2924 | * If it did abort, then it might immediately restart if it |
2925 | * it was just a device failure that leaves us degraded but | |
2926 | * functioning. | |
77a73a17 N |
2927 | */ |
2928 | strcpy(buf, "hi"); | |
2929 | if (sysfs_get_str(info, NULL, "reshape_position", buf, sizeof(buf)) < 0 | |
621ea11b N |
2930 | || strncmp(buf, "none", 4) != 0) { |
2931 | /* The abort might only be temporary. Wait up to 10 | |
2932 | * seconds for fd to contain a valid number again. | |
2933 | */ | |
2934 | struct timeval tv; | |
2935 | int rv = -2; | |
2936 | tv.tv_sec = 10; | |
2937 | tv.tv_usec = 0; | |
6560987b | 2938 | while (fd >= 0 && rv < 0 && tv.tv_sec > 0) { |
621ea11b N |
2939 | fd_set rfds; |
2940 | FD_ZERO(&rfds); | |
2941 | FD_SET(fd, &rfds); | |
2942 | if (select(fd+1, NULL, NULL, &rfds, &tv) != 1) | |
2943 | break; | |
6560987b N |
2944 | switch (sysfs_fd_get_ll(fd, &completed)) { |
2945 | case 0: | |
621ea11b N |
2946 | /* all good again */ |
2947 | rv = 1; | |
6560987b N |
2948 | break; |
2949 | case -2: /* read error - abort */ | |
2950 | tv.tv_sec = 0; | |
2951 | break; | |
2952 | } | |
621ea11b N |
2953 | } |
2954 | if (fd >= 0) | |
2955 | close(fd); | |
2956 | return rv; /* abort */ | |
2957 | } else { | |
6d5316f6 | 2958 | /* Maybe racing with array shutdown - check state */ |
621ea11b N |
2959 | if (fd >= 0) |
2960 | close(fd); | |
6d5316f6 N |
2961 | if (sysfs_get_str(info, NULL, "array_state", buf, sizeof(buf)) < 0 |
2962 | || strncmp(buf, "inactive", 8) == 0 | |
2963 | || strncmp(buf, "clear",5) == 0) | |
2964 | return -2; /* abort */ | |
77a73a17 | 2965 | return -1; /* complete */ |
6d5316f6 | 2966 | } |
7443ee81 N |
2967 | } |
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 | ||
3039 | rv = save_stripes(sources, offsets, | |
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 */ |
7443ee81 | 3087 | static int forget_backup( |
7236ee7a N |
3088 | int dests, int *destfd, unsigned long long *destoffsets, |
3089 | int part) | |
3090 | { | |
7443ee81 N |
3091 | /* |
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 N |
3114 | rv = -1; |
3115 | if (rv == 0 && | |
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, | |
3151 | ((char*)&bsb2.sb_csum2)-((char*)&bsb2))) | |
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) { | |
080fd005 | 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 | } |
080fd005 | 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 | |
3541 | ": backup-metadata found on %s but is not needed\n", devname); | |
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 N |
3575 | fprintf(stderr, Name |
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 | } | |
3620 | ||
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 | } |