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