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