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