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