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