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