]>
Commit | Line | Data |
---|---|---|
e5329c37 NB |
1 | /* |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
e736b623 | 4 | * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de> |
e5329c37 NB |
5 | * |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | * Author: Neil Brown | |
e736b623 | 22 | * Email: <neilb@suse.de> |
e5329c37 NB |
23 | */ |
24 | #include "mdadm.h" | |
25 | #include "dlink.h" | |
7236ee7a | 26 | #include <sys/mman.h> |
e5329c37 NB |
27 | |
28 | #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN) | |
29 | #error no endian defined | |
30 | #endif | |
31 | #include "md_u.h" | |
32 | #include "md_p.h" | |
33 | ||
e9e43ec3 N |
34 | #ifndef offsetof |
35 | #define offsetof(t,f) ((size_t)&(((t*)0)->f)) | |
36 | #endif | |
37 | ||
e5329c37 NB |
38 | int Grow_Add_device(char *devname, int fd, char *newdev) |
39 | { | |
40 | /* Add a device to an active array. | |
41 | * Currently, just extend a linear array. | |
42 | * This requires writing a new superblock on the | |
43 | * new device, calling the kernel to add the device, | |
44 | * and if that succeeds, update the superblock on | |
45 | * all other devices. | |
46 | * This means that we need to *find* all other devices. | |
47 | */ | |
4b1ac34b NB |
48 | struct mdinfo info; |
49 | ||
e5329c37 NB |
50 | struct stat stb; |
51 | int nfd, fd2; | |
52 | int d, nd; | |
82d9eba6 | 53 | struct supertype *st = NULL; |
4725bc31 | 54 | char *subarray = NULL; |
e5329c37 | 55 | |
4b1ac34b | 56 | if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) { |
e5329c37 NB |
57 | fprintf(stderr, Name ": cannot get array info for %s\n", devname); |
58 | return 1; | |
59 | } | |
60 | ||
4725bc31 N |
61 | if (info.array.level != -1) { |
62 | fprintf(stderr, Name ": can only add devices to linear arrays\n"); | |
63 | return 1; | |
64 | } | |
65 | ||
66 | st = super_by_fd(fd, &subarray); | |
82d9eba6 | 67 | if (!st) { |
f9ce90ba NB |
68 | fprintf(stderr, Name ": cannot handle arrays with superblock version %d\n", info.array.major_version); |
69 | return 1; | |
70 | } | |
71 | ||
4725bc31 N |
72 | if (subarray) { |
73 | fprintf(stderr, Name ": Cannot grow linear sub-arrays yet\n"); | |
74 | free(subarray); | |
75 | free(st); | |
e5329c37 NB |
76 | } |
77 | ||
6416d527 | 78 | nfd = open(newdev, O_RDWR|O_EXCL|O_DIRECT); |
e5329c37 NB |
79 | if (nfd < 0) { |
80 | fprintf(stderr, Name ": cannot open %s\n", newdev); | |
4725bc31 | 81 | free(st); |
e5329c37 NB |
82 | return 1; |
83 | } | |
84 | fstat(nfd, &stb); | |
85 | if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
86 | fprintf(stderr, Name ": %s is not a block device!\n", newdev); | |
87 | close(nfd); | |
4725bc31 | 88 | free(st); |
e5329c37 NB |
89 | return 1; |
90 | } | |
91 | /* now check out all the devices and make sure we can read the superblock */ | |
4b1ac34b | 92 | for (d=0 ; d < info.array.raid_disks ; d++) { |
e5329c37 NB |
93 | mdu_disk_info_t disk; |
94 | char *dv; | |
95 | ||
4725bc31 N |
96 | st->ss->free_super(st); |
97 | ||
e5329c37 NB |
98 | disk.number = d; |
99 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) { | |
100 | fprintf(stderr, Name ": cannot get device detail for device %d\n", | |
101 | d); | |
4725bc31 N |
102 | close(nfd); |
103 | free(st); | |
e5329c37 NB |
104 | return 1; |
105 | } | |
16c6fa80 | 106 | dv = map_dev(disk.major, disk.minor, 1); |
e5329c37 NB |
107 | if (!dv) { |
108 | fprintf(stderr, Name ": cannot find device file for device %d\n", | |
109 | d); | |
4725bc31 N |
110 | close(nfd); |
111 | free(st); | |
e5329c37 NB |
112 | return 1; |
113 | } | |
16c6fa80 | 114 | fd2 = dev_open(dv, O_RDWR); |
e5329c37 NB |
115 | if (!fd2) { |
116 | fprintf(stderr, Name ": cannot open device file %s\n", dv); | |
4725bc31 N |
117 | close(nfd); |
118 | free(st); | |
e5329c37 NB |
119 | return 1; |
120 | } | |
3da92f27 NB |
121 | |
122 | if (st->ss->load_super(st, fd2, NULL)) { | |
e5329c37 | 123 | fprintf(stderr, Name ": cannot find super block on %s\n", dv); |
4725bc31 | 124 | close(nfd); |
e5329c37 | 125 | close(fd2); |
4725bc31 | 126 | free(st); |
e5329c37 NB |
127 | return 1; |
128 | } | |
129 | close(fd2); | |
130 | } | |
131 | /* Ok, looks good. Lets update the superblock and write it out to | |
132 | * newdev. | |
133 | */ | |
aba69144 | 134 | |
4b1ac34b NB |
135 | info.disk.number = d; |
136 | info.disk.major = major(stb.st_rdev); | |
137 | info.disk.minor = minor(stb.st_rdev); | |
138 | info.disk.raid_disk = d; | |
139 | info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE); | |
3da92f27 | 140 | st->ss->update_super(st, &info, "linear-grow-new", newdev, |
f752781f | 141 | 0, 0, NULL); |
e5329c37 | 142 | |
3da92f27 | 143 | if (st->ss->store_super(st, nfd)) { |
f752781f NB |
144 | fprintf(stderr, Name ": Cannot store new superblock on %s\n", |
145 | newdev); | |
e5329c37 NB |
146 | close(nfd); |
147 | return 1; | |
148 | } | |
e5329c37 | 149 | close(nfd); |
4b1ac34b NB |
150 | |
151 | if (ioctl(fd, ADD_NEW_DISK, &info.disk) != 0) { | |
e5329c37 NB |
152 | fprintf(stderr, Name ": Cannot add new disk to this array\n"); |
153 | return 1; | |
154 | } | |
155 | /* Well, that seems to have worked. | |
156 | * Now go through and update all superblocks | |
157 | */ | |
158 | ||
4b1ac34b | 159 | if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) { |
e5329c37 NB |
160 | fprintf(stderr, Name ": cannot get array info for %s\n", devname); |
161 | return 1; | |
162 | } | |
163 | ||
164 | nd = d; | |
4b1ac34b | 165 | for (d=0 ; d < info.array.raid_disks ; d++) { |
e5329c37 NB |
166 | mdu_disk_info_t disk; |
167 | char *dv; | |
168 | ||
169 | disk.number = d; | |
170 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) { | |
171 | fprintf(stderr, Name ": cannot get device detail for device %d\n", | |
172 | d); | |
173 | return 1; | |
174 | } | |
16c6fa80 | 175 | dv = map_dev(disk.major, disk.minor, 1); |
e5329c37 NB |
176 | if (!dv) { |
177 | fprintf(stderr, Name ": cannot find device file for device %d\n", | |
178 | d); | |
179 | return 1; | |
180 | } | |
16c6fa80 | 181 | fd2 = dev_open(dv, O_RDWR); |
e5329c37 NB |
182 | if (fd2 < 0) { |
183 | fprintf(stderr, Name ": cannot open device file %s\n", dv); | |
184 | return 1; | |
185 | } | |
3da92f27 | 186 | if (st->ss->load_super(st, fd2, NULL)) { |
e5329c37 NB |
187 | fprintf(stderr, Name ": cannot find super block on %s\n", dv); |
188 | close(fd); | |
189 | return 1; | |
190 | } | |
4b1ac34b NB |
191 | info.array.raid_disks = nd+1; |
192 | info.array.nr_disks = nd+1; | |
193 | info.array.active_disks = nd+1; | |
194 | info.array.working_disks = nd+1; | |
f752781f | 195 | |
3da92f27 | 196 | st->ss->update_super(st, &info, "linear-grow-update", dv, |
f752781f | 197 | 0, 0, NULL); |
aba69144 | 198 | |
3da92f27 | 199 | if (st->ss->store_super(st, fd2)) { |
e5329c37 NB |
200 | fprintf(stderr, Name ": Cannot store new superblock on %s\n", dv); |
201 | close(fd2); | |
202 | return 1; | |
203 | } | |
204 | close(fd2); | |
205 | } | |
206 | ||
207 | return 0; | |
208 | } | |
f5e166fe | 209 | |
8fac0577 | 210 | int Grow_addbitmap(char *devname, int fd, char *file, int chunk, int delay, int write_behind, int force) |
f5e166fe NB |
211 | { |
212 | /* | |
213 | * First check that array doesn't have a bitmap | |
214 | * Then create the bitmap | |
215 | * Then add it | |
216 | * | |
217 | * For internal bitmaps, we need to check the version, | |
218 | * find all the active devices, and write the bitmap block | |
219 | * to all devices | |
220 | */ | |
221 | mdu_bitmap_file_t bmf; | |
222 | mdu_array_info_t array; | |
223 | struct supertype *st; | |
4725bc31 | 224 | char *subarray = NULL; |
dcec9ee5 NB |
225 | int major = BITMAP_MAJOR_HI; |
226 | int vers = md_get_version(fd); | |
8fac0577 | 227 | unsigned long long bitmapsize, array_size; |
dcec9ee5 NB |
228 | |
229 | if (vers < 9003) { | |
230 | major = BITMAP_MAJOR_HOSTENDIAN; | |
b3bd581b N |
231 | fprintf(stderr, Name ": Warning - bitmaps created on this kernel" |
232 | " are not portable\n" | |
233 | " between different architectures. Consider upgrading" | |
234 | " the Linux kernel.\n"); | |
dcec9ee5 | 235 | } |
f5e166fe NB |
236 | |
237 | if (ioctl(fd, GET_BITMAP_FILE, &bmf) != 0) { | |
353632d9 | 238 | if (errno == ENOMEM) |
f5e166fe NB |
239 | fprintf(stderr, Name ": Memory allocation failure.\n"); |
240 | else | |
241 | fprintf(stderr, Name ": bitmaps not supported by this kernel.\n"); | |
242 | return 1; | |
243 | } | |
244 | if (bmf.pathname[0]) { | |
fe80f49b NB |
245 | if (strcmp(file,"none")==0) { |
246 | if (ioctl(fd, SET_BITMAP_FILE, -1)!= 0) { | |
247 | fprintf(stderr, Name ": failed to remove bitmap %s\n", | |
248 | bmf.pathname); | |
249 | return 1; | |
250 | } | |
251 | return 0; | |
252 | } | |
f5e166fe NB |
253 | fprintf(stderr, Name ": %s already has a bitmap (%s)\n", |
254 | devname, bmf.pathname); | |
255 | return 1; | |
256 | } | |
257 | if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) { | |
258 | fprintf(stderr, Name ": cannot get array status for %s\n", devname); | |
259 | return 1; | |
260 | } | |
261 | if (array.state & (1<<MD_SB_BITMAP_PRESENT)) { | |
fe80f49b NB |
262 | if (strcmp(file, "none")==0) { |
263 | array.state &= ~(1<<MD_SB_BITMAP_PRESENT); | |
264 | if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) { | |
265 | fprintf(stderr, Name ": failed to remove internal bitmap.\n"); | |
266 | return 1; | |
267 | } | |
268 | return 0; | |
269 | } | |
f5e166fe NB |
270 | fprintf(stderr, Name ": Internal bitmap already present on %s\n", |
271 | devname); | |
272 | return 1; | |
273 | } | |
4725bc31 N |
274 | |
275 | if (strcmp(file, "none") == 0) { | |
276 | fprintf(stderr, Name ": no bitmap found on %s\n", devname); | |
277 | return 1; | |
278 | } | |
5b28bd56 NB |
279 | if (array.level <= 0) { |
280 | fprintf(stderr, Name ": Bitmaps not meaningful with level %s\n", | |
281 | map_num(pers, array.level)?:"of this array"); | |
282 | return 1; | |
283 | } | |
8fac0577 NB |
284 | bitmapsize = array.size; |
285 | bitmapsize <<= 1; | |
beae1dfe | 286 | if (get_dev_size(fd, NULL, &array_size) && |
8fac0577 NB |
287 | array_size > (0x7fffffffULL<<9)) { |
288 | /* Array is big enough that we cannot trust array.size | |
289 | * try other approaches | |
290 | */ | |
291 | bitmapsize = get_component_size(fd); | |
292 | } | |
8fac0577 NB |
293 | if (bitmapsize == 0) { |
294 | fprintf(stderr, Name ": Cannot reliably determine size of array to create bitmap - sorry.\n"); | |
295 | return 1; | |
296 | } | |
297 | ||
f9c25f1d | 298 | if (array.level == 10) { |
8686f3ed | 299 | int ncopies = (array.layout&255)*((array.layout>>8)&255); |
f9c25f1d NB |
300 | bitmapsize = bitmapsize * array.raid_disks / ncopies; |
301 | } | |
302 | ||
4725bc31 | 303 | st = super_by_fd(fd, &subarray); |
f5e166fe NB |
304 | if (!st) { |
305 | fprintf(stderr, Name ": Cannot understand version %d.%d\n", | |
306 | array.major_version, array.minor_version); | |
307 | return 1; | |
308 | } | |
4725bc31 N |
309 | if (subarray) { |
310 | fprintf(stderr, Name ": Cannot add bitmaps to sub-arrays yet\n"); | |
311 | free(subarray); | |
312 | free(st); | |
fe80f49b | 313 | return 1; |
4725bc31 N |
314 | } |
315 | if (strcmp(file, "internal") == 0) { | |
f5e166fe | 316 | int d; |
ebeb3663 N |
317 | if (st->ss->add_internal_bitmap == NULL) { |
318 | fprintf(stderr, Name ": Internal bitmaps not supported " | |
319 | "with %s metadata\n", st->ss->name); | |
320 | return 1; | |
321 | } | |
ea329559 | 322 | for (d=0; d< st->max_devs; d++) { |
f5e166fe NB |
323 | mdu_disk_info_t disk; |
324 | char *dv; | |
325 | disk.number = d; | |
326 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) | |
327 | continue; | |
328 | if (disk.major == 0 && | |
329 | disk.minor == 0) | |
330 | continue; | |
331 | if ((disk.state & (1<<MD_DISK_SYNC))==0) | |
332 | continue; | |
16c6fa80 | 333 | dv = map_dev(disk.major, disk.minor, 1); |
f5e166fe | 334 | if (dv) { |
16c6fa80 | 335 | int fd2 = dev_open(dv, O_RDWR); |
f5e166fe NB |
336 | if (fd2 < 0) |
337 | continue; | |
3da92f27 | 338 | if (st->ss->load_super(st, fd2, NULL)==0) { |
199171a2 | 339 | if (st->ss->add_internal_bitmap( |
3da92f27 | 340 | st, |
199171a2 NB |
341 | &chunk, delay, write_behind, |
342 | bitmapsize, 0, major) | |
343 | ) | |
3da92f27 | 344 | st->ss->write_bitmap(st, fd2); |
21e92547 NB |
345 | else { |
346 | fprintf(stderr, Name ": failed to create internal bitmap - chunksize problem.\n"); | |
347 | close(fd2); | |
348 | return 1; | |
349 | } | |
f5e166fe NB |
350 | } |
351 | close(fd2); | |
352 | } | |
353 | } | |
354 | array.state |= (1<<MD_SB_BITMAP_PRESENT); | |
355 | if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) { | |
ff634064 N |
356 | if (errno == EBUSY) |
357 | fprintf(stderr, Name | |
358 | ": Cannot add bitmap while array is" | |
359 | " resyncing or reshaping etc.\n"); | |
f5e166fe NB |
360 | fprintf(stderr, Name ": failed to set internal bitmap.\n"); |
361 | return 1; | |
362 | } | |
fe80f49b NB |
363 | } else { |
364 | int uuid[4]; | |
365 | int bitmap_fd; | |
366 | int d; | |
367 | int max_devs = st->max_devs; | |
fe80f49b NB |
368 | |
369 | /* try to load a superblock */ | |
370 | for (d=0; d<max_devs; d++) { | |
371 | mdu_disk_info_t disk; | |
372 | char *dv; | |
373 | int fd2; | |
374 | disk.number = d; | |
375 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) | |
376 | continue; | |
377 | if ((disk.major==0 && disk.minor==0) || | |
378 | (disk.state & (1<<MD_DISK_REMOVED))) | |
379 | continue; | |
16c6fa80 | 380 | dv = map_dev(disk.major, disk.minor, 1); |
fe80f49b | 381 | if (!dv) continue; |
16c6fa80 | 382 | fd2 = dev_open(dv, O_RDONLY); |
fe80f49b | 383 | if (fd2 >= 0 && |
3da92f27 | 384 | st->ss->load_super(st, fd2, NULL) == 0) { |
fe80f49b | 385 | close(fd2); |
3da92f27 | 386 | st->ss->uuid_from_super(st, uuid); |
fe80f49b NB |
387 | break; |
388 | } | |
389 | close(fd2); | |
390 | } | |
391 | if (d == max_devs) { | |
392 | fprintf(stderr, Name ": cannot find UUID for array!\n"); | |
393 | return 1; | |
394 | } | |
8fac0577 | 395 | if (CreateBitmap(file, force, (char*)uuid, chunk, |
f9c25f1d | 396 | delay, write_behind, bitmapsize, major)) { |
fe80f49b NB |
397 | return 1; |
398 | } | |
399 | bitmap_fd = open(file, O_RDWR); | |
400 | if (bitmap_fd < 0) { | |
8fac0577 | 401 | fprintf(stderr, Name ": weird: %s cannot be opened\n", |
fe80f49b NB |
402 | file); |
403 | return 1; | |
404 | } | |
405 | if (ioctl(fd, SET_BITMAP_FILE, bitmap_fd) < 0) { | |
ff634064 N |
406 | int err = errno; |
407 | if (errno == EBUSY) | |
408 | fprintf(stderr, Name | |
409 | ": Cannot add bitmap while array is" | |
410 | " resyncing or reshaping etc.\n"); | |
fe80f49b | 411 | fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n", |
ff634064 | 412 | devname, strerror(err)); |
fe80f49b NB |
413 | return 1; |
414 | } | |
415 | } | |
f5e166fe NB |
416 | |
417 | return 0; | |
418 | } | |
419 | ||
e86c9dd6 NB |
420 | |
421 | /* | |
422 | * When reshaping an array we might need to backup some data. | |
423 | * This is written to all spares with a 'super_block' describing it. | |
ff94fb86 | 424 | * The superblock goes 4K from the end of the used space on the |
e86c9dd6 NB |
425 | * device. |
426 | * It if written after the backup is complete. | |
427 | * It has the following structure. | |
428 | */ | |
429 | ||
5fdf37e3 | 430 | static struct mdp_backup_super { |
7236ee7a | 431 | char magic[16]; /* md_backup_data-1 or -2 */ |
e86c9dd6 NB |
432 | __u8 set_uuid[16]; |
433 | __u64 mtime; | |
434 | /* start/sizes in 512byte sectors */ | |
7236ee7a | 435 | __u64 devstart; /* address on backup device/file of data */ |
e86c9dd6 NB |
436 | __u64 arraystart; |
437 | __u64 length; | |
438 | __u32 sb_csum; /* csum of preceeding bytes. */ | |
7236ee7a N |
439 | __u32 pad1; |
440 | __u64 devstart2; /* offset in to data of second section */ | |
441 | __u64 arraystart2; | |
442 | __u64 length2; | |
443 | __u32 sb_csum2; /* csum of preceeding bytes. */ | |
444 | __u8 pad[512-68-32]; | |
5fdf37e3 | 445 | } __attribute__((aligned(512))) bsb, bsb2; |
e86c9dd6 | 446 | |
4411fb17 | 447 | static __u32 bsb_csum(char *buf, int len) |
e86c9dd6 NB |
448 | { |
449 | int i; | |
450 | int csum = 0; | |
451 | for (i=0; i<len; i++) | |
452 | csum = (csum<<3) + buf[0]; | |
453 | return __cpu_to_le32(csum); | |
454 | } | |
455 | ||
7236ee7a N |
456 | static int child_grow(int afd, struct mdinfo *sra, unsigned long blocks, |
457 | int *fds, unsigned long long *offsets, | |
458 | int disks, int chunk, int level, int layout, int data, | |
459 | int dests, int *destfd, unsigned long long *destoffsets); | |
460 | static int child_shrink(int afd, struct mdinfo *sra, unsigned long blocks, | |
461 | int *fds, unsigned long long *offsets, | |
462 | int disks, int chunk, int level, int layout, int data, | |
463 | int dests, int *destfd, unsigned long long *destoffsets); | |
464 | static int child_same_size(int afd, struct mdinfo *sra, unsigned long blocks, | |
465 | int *fds, unsigned long long *offsets, | |
e9e43ec3 | 466 | unsigned long long start, |
7236ee7a N |
467 | int disks, int chunk, int level, int layout, int data, |
468 | int dests, int *destfd, unsigned long long *destoffsets); | |
469 | ||
d7ca196c N |
470 | static int check_idle(struct supertype *st) |
471 | { | |
472 | /* Check that all member arrays for this container, or the | |
473 | * container of this array, are idle | |
474 | */ | |
475 | int container_dev = (st->container_dev != NoMdDev | |
476 | ? st->container_dev : st->devnum); | |
477 | char container[40]; | |
478 | struct mdstat_ent *ent, *e; | |
479 | int is_idle = 1; | |
480 | ||
481 | fmt_devname(container, container_dev); | |
482 | ent = mdstat_read(0, 0); | |
483 | for (e = ent ; e; e = e->next) { | |
484 | if (!is_container_member(e, container)) | |
485 | continue; | |
486 | if (e->percent >= 0) { | |
487 | is_idle = 0; | |
488 | break; | |
489 | } | |
490 | } | |
491 | free_mdstat(ent); | |
492 | return is_idle; | |
493 | } | |
494 | ||
7f2ba464 | 495 | static int freeze_container(struct supertype *st) |
7236ee7a | 496 | { |
7f2ba464 DW |
497 | int container_dev = (st->container_dev != NoMdDev |
498 | ? st->container_dev : st->devnum); | |
d7ca196c | 499 | char container[40]; |
7f2ba464 | 500 | |
d7ca196c N |
501 | if (!check_idle(st)) |
502 | return -1; | |
503 | ||
504 | fmt_devname(container, container_dev); | |
7f2ba464 DW |
505 | |
506 | if (block_monitor(container, 1)) { | |
507 | fprintf(stderr, Name ": failed to freeze container\n"); | |
508 | return -2; | |
509 | } | |
510 | ||
511 | return 1; | |
512 | } | |
513 | ||
514 | static void unfreeze_container(struct supertype *st) | |
515 | { | |
516 | int container_dev = (st->container_dev != NoMdDev | |
517 | ? st->container_dev : st->devnum); | |
d7ca196c N |
518 | char container[40]; |
519 | ||
520 | fmt_devname(container, container_dev); | |
7f2ba464 DW |
521 | |
522 | unblock_monitor(container, 1); | |
523 | } | |
524 | ||
525 | static int freeze(struct supertype *st) | |
526 | { | |
527 | /* Try to freeze resync/rebuild on this array/container. | |
7236ee7a | 528 | * Return -1 if the array is busy, |
7f2ba464 | 529 | * return -2 container cannot be frozen, |
7236ee7a N |
530 | * return 0 if this kernel doesn't support 'frozen' |
531 | * return 1 if it worked. | |
532 | */ | |
7f2ba464 DW |
533 | if (st->ss->external) |
534 | return freeze_container(st); | |
535 | else { | |
536 | struct mdinfo *sra = sysfs_read(-1, st->devnum, GET_VERSION); | |
537 | int err; | |
538 | ||
539 | if (!sra) | |
540 | return -1; | |
541 | err = sysfs_freeze_array(sra); | |
542 | sysfs_free(sra); | |
543 | return err; | |
544 | } | |
7236ee7a N |
545 | } |
546 | ||
7f2ba464 | 547 | static void unfreeze(struct supertype *st, int frozen) |
7236ee7a N |
548 | { |
549 | /* If 'frozen' is 1, unfreeze the array */ | |
7f2ba464 DW |
550 | if (frozen <= 0) |
551 | return; | |
552 | ||
553 | if (st->ss->external) | |
554 | return unfreeze_container(st); | |
555 | else { | |
556 | struct mdinfo *sra = sysfs_read(-1, st->devnum, GET_VERSION); | |
557 | ||
558 | if (sra) | |
559 | sysfs_set_str(sra, NULL, "sync_action", "idle"); | |
560 | else | |
561 | fprintf(stderr, Name ": failed to unfreeze array\n"); | |
562 | sysfs_free(sra); | |
563 | } | |
7236ee7a N |
564 | } |
565 | ||
4411fb17 | 566 | static void wait_reshape(struct mdinfo *sra) |
7236ee7a N |
567 | { |
568 | int fd = sysfs_get_fd(sra, NULL, "sync_action"); | |
569 | char action[20]; | |
570 | ||
92a19f1a AK |
571 | if (fd < 0) |
572 | return; | |
573 | ||
574 | while (sysfs_fd_get_str(fd, action, 20) > 0 && | |
575 | strncmp(action, "reshape", 7) == 0) { | |
7236ee7a N |
576 | fd_set rfds; |
577 | FD_ZERO(&rfds); | |
578 | FD_SET(fd, &rfds); | |
579 | select(fd+1, NULL, NULL, &rfds, NULL); | |
92a19f1a AK |
580 | } |
581 | close(fd); | |
7236ee7a | 582 | } |
7bc71196 DW |
583 | |
584 | static int reshape_super(struct supertype *st, long long size, int level, | |
585 | int layout, int chunksize, int raid_disks, | |
586 | char *backup_file, char *dev, int verbose) | |
587 | { | |
588 | /* nothing extra to check in the native case */ | |
589 | if (!st->ss->external) | |
590 | return 0; | |
591 | if (!st->ss->reshape_super || | |
592 | !st->ss->manage_reshape) { | |
593 | fprintf(stderr, Name ": %s metadata does not support reshape\n", | |
594 | st->ss->name); | |
595 | return 1; | |
596 | } | |
597 | ||
598 | return st->ss->reshape_super(st, size, level, layout, chunksize, | |
599 | raid_disks, backup_file, dev, verbose); | |
600 | } | |
601 | ||
602 | static void sync_metadata(struct supertype *st) | |
603 | { | |
604 | if (st->ss->external) { | |
605 | if (st->update_tail) | |
606 | flush_metadata_updates(st); | |
607 | else | |
608 | st->ss->sync_metadata(st); | |
609 | } | |
610 | } | |
611 | ||
612 | static int subarray_set_num(char *container, struct mdinfo *sra, char *name, int n) | |
613 | { | |
614 | /* when dealing with external metadata subarrays we need to be | |
615 | * prepared to handle EAGAIN. The kernel may need to wait for | |
616 | * mdmon to mark the array active so the kernel can handle | |
617 | * allocations/writeback when preparing the reshape action | |
618 | * (md_allow_write()). We temporarily disable safe_mode_delay | |
619 | * to close a race with the array_state going clean before the | |
620 | * next write to raid_disks / stripe_cache_size | |
621 | */ | |
622 | char safe[50]; | |
623 | int rc; | |
624 | ||
625 | /* only 'raid_disks' and 'stripe_cache_size' trigger md_allow_write */ | |
626 | if (strcmp(name, "raid_disks") != 0 && | |
627 | strcmp(name, "stripe_cache_size") != 0) | |
628 | return sysfs_set_num(sra, NULL, name, n); | |
629 | ||
630 | rc = sysfs_get_str(sra, NULL, "safe_mode_delay", safe, sizeof(safe)); | |
631 | if (rc <= 0) | |
632 | return -1; | |
633 | sysfs_set_num(sra, NULL, "safe_mode_delay", 0); | |
634 | rc = sysfs_set_num(sra, NULL, name, n); | |
635 | if (rc < 0 && errno == EAGAIN) { | |
636 | ping_monitor(container); | |
637 | /* if we get EAGAIN here then the monitor is not active | |
638 | * so stop trying | |
639 | */ | |
640 | rc = sysfs_set_num(sra, NULL, name, n); | |
641 | } | |
642 | sysfs_set_str(sra, NULL, "safe_mode_delay", safe); | |
643 | return rc; | |
644 | } | |
645 | ||
646 | static int reshape_container_raid_disks(char *container, int raid_disks) | |
647 | { | |
648 | /* for each subarray switch to a raid level that can | |
649 | * support the reshape, and set raid disks | |
650 | */ | |
651 | struct mdstat_ent *ent, *e; | |
652 | int changed = 0, rv = 0, err = 0; | |
653 | ||
654 | ent = mdstat_read(1, 0); | |
655 | if (!ent) { | |
656 | fprintf(stderr, Name ": unable to read /proc/mdstat\n"); | |
657 | return -1; | |
658 | } | |
659 | ||
660 | changed = 0; | |
661 | for (e = ent; e; e = e->next) { | |
662 | struct mdinfo *sub; | |
663 | unsigned int cache; | |
664 | int level, takeover_delta = 0; | |
665 | ||
666 | if (!is_container_member(e, container)) | |
667 | continue; | |
668 | ||
669 | level = map_name(pers, e->level); | |
670 | if (level == 0) { | |
671 | sub = sysfs_read(-1, e->devnum, GET_VERSION); | |
672 | if (!sub) | |
673 | break; | |
674 | /* metadata records 'orig_level' */ | |
675 | rv = sysfs_set_num(sub, NULL, "level", 4); | |
676 | if (rv < 0) { | |
677 | err = errno; | |
678 | break; | |
679 | } | |
680 | /* we want spares to be used for capacity | |
681 | * expansion, not rebuild | |
682 | */ | |
683 | takeover_delta = 1; | |
684 | ||
685 | sysfs_free(sub); | |
686 | level = 4; | |
687 | } | |
688 | ||
689 | sub = NULL; | |
690 | switch (level) { | |
691 | default: | |
692 | rv = -1; | |
693 | break; | |
694 | case 4: | |
695 | case 5: | |
696 | case 6: | |
697 | sub = sysfs_read(-1, e->devnum, GET_CHUNK|GET_CACHE); | |
698 | if (!sub) | |
699 | break; | |
700 | cache = (sub->array.chunk_size / 4096) * 4; | |
701 | if (cache > sub->cache_size) | |
702 | rv = subarray_set_num(container, sub, | |
703 | "stripe_cache_size", cache); | |
704 | if (rv) { | |
705 | err = errno; | |
706 | break; | |
707 | } | |
708 | /* fall through */ | |
709 | case 1: | |
710 | if (!sub) | |
711 | sub = sysfs_read(-1, e->devnum, GET_VERSION); | |
712 | if (!sub) | |
713 | break; | |
714 | ||
715 | rv = subarray_set_num(container, sub, "raid_disks", | |
716 | raid_disks + takeover_delta); | |
717 | if (rv) | |
718 | err = errno; | |
719 | else | |
720 | changed++; | |
721 | break; | |
722 | } | |
723 | sysfs_free(sub); | |
724 | if (rv) | |
725 | break; | |
726 | } | |
727 | free_mdstat(ent); | |
728 | if (rv) { | |
729 | fprintf(stderr, Name | |
730 | ": failed to initiate container reshape%s%s\n", | |
731 | err ? ": " : "", err ? strerror(err) : ""); | |
732 | return rv; | |
733 | } | |
734 | ||
735 | return changed; | |
736 | } | |
737 | ||
738 | static void revert_container_raid_disks(struct supertype *st, int fd, char *container) | |
739 | { | |
740 | /* we failed to prepare all subarrays in the container for | |
741 | * reshape, so cancel the changes and restore the nominal raid | |
742 | * level | |
743 | */ | |
744 | struct mdstat_ent *ent, *e; | |
745 | ||
746 | ent = mdstat_read(0, 0); | |
747 | if (!ent) { | |
748 | fprintf(stderr, Name | |
749 | ": failed to read /proc/mdstat while aborting reshape\n"); | |
750 | return; | |
751 | } | |
752 | ||
7f2ba464 DW |
753 | if (st->ss->load_container(st, fd, NULL)) { |
754 | fprintf(stderr, Name | |
755 | ": failed read metadata while aborting reshape\n"); | |
756 | return ; | |
757 | } | |
758 | ||
759 | ||
7bc71196 DW |
760 | for (e = ent; e; e = e->next) { |
761 | int level_fixed = 0, disks_fixed = 0; | |
7f2ba464 DW |
762 | struct mdinfo *sub, *prev; |
763 | char *subarray; | |
7bc71196 DW |
764 | |
765 | if (!is_container_member(e, container)) | |
766 | continue; | |
767 | ||
7f2ba464 DW |
768 | subarray = to_subarray(e, container); |
769 | prev = st->ss->container_content(st, subarray); | |
7bc71196 DW |
770 | |
771 | /* changing level might change raid_disks so we do it | |
772 | * first and then check if raid_disks still needs fixing | |
773 | */ | |
7f2ba464 | 774 | if (map_name(pers, e->level) != prev->array.level) { |
7bc71196 DW |
775 | sub = sysfs_read(-1, e->devnum, GET_VERSION); |
776 | if (sub && | |
7f2ba464 | 777 | !sysfs_set_num(sub, NULL, "level", prev->array.level)) |
7bc71196 DW |
778 | level_fixed = 1; |
779 | sysfs_free(sub); | |
780 | } else | |
781 | level_fixed = 1; | |
782 | ||
783 | sub = sysfs_read(-1, e->devnum, GET_DISKS); | |
7f2ba464 | 784 | if (sub && sub->array.raid_disks != prev->array.raid_disks) { |
7bc71196 | 785 | if (!subarray_set_num(container, sub, "raid_disks", |
7f2ba464 | 786 | prev->array.raid_disks)) |
7bc71196 DW |
787 | disks_fixed = 1; |
788 | } else if (sub) | |
789 | disks_fixed = 1; | |
790 | sysfs_free(sub); | |
791 | ||
792 | if (!disks_fixed || !level_fixed) | |
793 | fprintf(stderr, Name | |
794 | ": failed to restore %s to a %d-disk %s array\n", | |
7f2ba464 DW |
795 | e->dev, prev->array.raid_disks, |
796 | map_num(pers, prev->array.level)); | |
797 | free(prev); | |
7bc71196 | 798 | } |
7f2ba464 | 799 | st->ss->free_super(st); |
7bc71196 DW |
800 | free_mdstat(ent); |
801 | } | |
802 | ||
62a48395 AK |
803 | int remove_disks_on_raid10_to_raid0_takeover(struct supertype *st, |
804 | struct mdinfo *sra, | |
805 | int layout) | |
806 | { | |
807 | int nr_of_copies; | |
808 | struct mdinfo *remaining; | |
809 | int slot; | |
810 | ||
811 | nr_of_copies = layout & 0xff; | |
812 | ||
813 | remaining = sra->devs; | |
814 | sra->devs = NULL; | |
815 | /* for each 'copy', select one device and remove from the list. */ | |
816 | for (slot = 0; slot < sra->array.raid_disks; slot += nr_of_copies) { | |
817 | struct mdinfo **diskp; | |
818 | int found = 0; | |
819 | ||
820 | /* Find a working device to keep */ | |
821 | for (diskp = &remaining; *diskp ; diskp = &(*diskp)->next) { | |
822 | struct mdinfo *disk = *diskp; | |
823 | ||
824 | if (disk->disk.raid_disk < slot) | |
825 | continue; | |
826 | if (disk->disk.raid_disk >= slot + nr_of_copies) | |
827 | continue; | |
828 | if (disk->disk.state & (1<<MD_DISK_REMOVED)) | |
829 | continue; | |
830 | if (disk->disk.state & (1<<MD_DISK_FAULTY)) | |
831 | continue; | |
832 | if (!(disk->disk.state & (1<<MD_DISK_SYNC))) | |
833 | continue; | |
834 | ||
835 | /* We have found a good disk to use! */ | |
836 | *diskp = disk->next; | |
837 | disk->next = sra->devs; | |
838 | sra->devs = disk; | |
839 | found = 1; | |
840 | break; | |
841 | } | |
842 | if (!found) | |
843 | break; | |
844 | } | |
845 | ||
846 | if (slot < sra->array.raid_disks) { | |
847 | /* didn't find all slots */ | |
848 | struct mdinfo **e; | |
849 | e = &remaining; | |
850 | while (*e) | |
851 | e = &(*e)->next; | |
852 | *e = sra->devs; | |
853 | sra->devs = remaining; | |
854 | return 1; | |
855 | } | |
856 | ||
857 | /* Remove all 'remaining' devices from the array */ | |
858 | while (remaining) { | |
859 | struct mdinfo *sd = remaining; | |
860 | remaining = sd->next; | |
861 | ||
862 | sysfs_set_str(sra, sd, "state", "faulty"); | |
863 | sysfs_set_str(sra, sd, "slot", "none"); | |
864 | sysfs_set_str(sra, sd, "state", "remove"); | |
865 | sd->disk.state |= (1<<MD_DISK_REMOVED); | |
866 | sd->disk.state &= ~(1<<MD_DISK_SYNC); | |
867 | sd->next = sra->devs; | |
868 | sra->devs = sd; | |
869 | } | |
870 | return 0; | |
871 | } | |
872 | ||
130994cb AK |
873 | void reshape_free_fdlist(int *fdlist, |
874 | unsigned long long *offsets, | |
875 | int size) | |
876 | { | |
877 | int i; | |
878 | ||
879 | for (i = 0; i < size; i++) | |
880 | if (fdlist[i] >= 0) | |
881 | close(fdlist[i]); | |
882 | ||
883 | free(fdlist); | |
884 | free(offsets); | |
885 | } | |
886 | ||
887 | int reshape_prepare_fdlist(char *devname, | |
888 | struct mdinfo *sra, | |
889 | int raid_disks, | |
890 | int nrdisks, | |
891 | unsigned long blocks, | |
892 | char *backup_file, | |
893 | int *fdlist, | |
894 | unsigned long long *offsets) | |
895 | { | |
896 | int d = 0; | |
897 | struct mdinfo *sd; | |
898 | ||
899 | for (d = 0; d <= nrdisks; d++) | |
900 | fdlist[d] = -1; | |
901 | d = raid_disks; | |
902 | for (sd = sra->devs; sd; sd = sd->next) { | |
903 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) | |
904 | continue; | |
905 | if (sd->disk.state & (1<<MD_DISK_SYNC)) { | |
906 | char *dn = map_dev(sd->disk.major, | |
907 | sd->disk.minor, 1); | |
908 | fdlist[sd->disk.raid_disk] | |
909 | = dev_open(dn, O_RDONLY); | |
910 | offsets[sd->disk.raid_disk] = sd->data_offset*512; | |
911 | if (fdlist[sd->disk.raid_disk] < 0) { | |
912 | fprintf(stderr, | |
913 | Name ": %s: cannot open component %s\n", | |
914 | devname, dn ? dn : "-unknown-"); | |
915 | d = -1; | |
916 | goto release; | |
917 | } | |
918 | } else if (backup_file == NULL) { | |
919 | /* spare */ | |
920 | char *dn = map_dev(sd->disk.major, | |
921 | sd->disk.minor, 1); | |
922 | fdlist[d] = dev_open(dn, O_RDWR); | |
923 | offsets[d] = (sd->data_offset + sra->component_size - blocks - 8)*512; | |
924 | if (fdlist[d] < 0) { | |
925 | fprintf(stderr, Name ": %s: cannot open component %s\n", | |
926 | devname, dn ? dn : "-unknown-"); | |
927 | d = -1; | |
928 | goto release; | |
929 | } | |
930 | d++; | |
931 | } | |
932 | } | |
933 | release: | |
934 | return d; | |
935 | } | |
936 | ||
e6e9d47b AK |
937 | int reshape_open_backup_file(char *backup_file, |
938 | int fd, | |
939 | char *devname, | |
940 | long blocks, | |
941 | int *fdlist, | |
942 | unsigned long long *offsets) | |
943 | { | |
944 | /* Return 1 on success, 0 on any form of failure */ | |
945 | /* need to check backup file is large enough */ | |
946 | char buf[512]; | |
947 | struct stat stb; | |
948 | unsigned int dev; | |
949 | int i; | |
950 | ||
951 | *fdlist = open(backup_file, O_RDWR|O_CREAT|O_EXCL, | |
952 | S_IRUSR | S_IWUSR); | |
953 | *offsets = 8 * 512; | |
954 | if (*fdlist < 0) { | |
955 | fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n", | |
956 | devname, backup_file, strerror(errno)); | |
957 | return 0; | |
958 | } | |
959 | /* Guard against backup file being on array device. | |
960 | * If array is partitioned or if LVM etc is in the | |
961 | * way this will not notice, but it is better than | |
962 | * nothing. | |
963 | */ | |
964 | fstat(*fdlist, &stb); | |
965 | dev = stb.st_dev; | |
966 | fstat(fd, &stb); | |
967 | if (stb.st_rdev == dev) { | |
968 | fprintf(stderr, Name ": backup file must NOT be" | |
969 | " on the array being reshaped.\n"); | |
970 | close(*fdlist); | |
971 | return 0; | |
972 | } | |
973 | ||
974 | memset(buf, 0, 512); | |
975 | for (i=0; i < blocks + 1 ; i++) { | |
976 | if (write(*fdlist, buf, 512) != 512) { | |
977 | fprintf(stderr, Name ": %s: cannot create" | |
978 | " backup file %s: %s\n", | |
979 | devname, backup_file, strerror(errno)); | |
980 | return 0; | |
981 | } | |
982 | } | |
983 | if (fsync(*fdlist) != 0) { | |
984 | fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n", | |
985 | devname, backup_file, strerror(errno)); | |
986 | return 0; | |
987 | } | |
988 | ||
989 | return 1; | |
990 | } | |
991 | ||
1c009fc2 AK |
992 | unsigned long compute_backup_blocks(int nchunk, int ochunk, |
993 | unsigned int ndata, unsigned int odata) | |
994 | { | |
995 | unsigned long a, b, blocks; | |
996 | /* So how much do we need to backup. | |
997 | * We need an amount of data which is both a whole number of | |
998 | * old stripes and a whole number of new stripes. | |
999 | * So LCM for (chunksize*datadisks). | |
1000 | */ | |
1001 | a = (ochunk/512) * odata; | |
1002 | b = (nchunk/512) * ndata; | |
1003 | /* Find GCD */ | |
1004 | while (a != b) { | |
1005 | if (a < b) | |
1006 | b -= a; | |
1007 | if (b < a) | |
1008 | a -= b; | |
1009 | } | |
1010 | /* LCM == product / GCD */ | |
1011 | blocks = (ochunk/512) * (nchunk/512) * odata * ndata / a; | |
1012 | ||
1013 | return blocks; | |
1014 | } | |
1015 | ||
1016 | ||
06b0d786 | 1017 | int Grow_reshape(char *devname, int fd, int quiet, char *backup_file, |
e86c9dd6 | 1018 | long long size, |
691a36b7 N |
1019 | int level, char *layout_str, int chunksize, int raid_disks, |
1020 | int force) | |
e86c9dd6 NB |
1021 | { |
1022 | /* Make some changes in the shape of an array. | |
1023 | * The kernel must support the change. | |
7236ee7a N |
1024 | * |
1025 | * There are three different changes. Each can trigger | |
1026 | * a resync or recovery so we freeze that until we have | |
1027 | * requested everything (if kernel supports freezing - 2.6.30). | |
1028 | * The steps are: | |
1029 | * - change size (i.e. component_size) | |
1030 | * - change level | |
1031 | * - change layout/chunksize/ndisks | |
1032 | * | |
1033 | * The last can require a reshape. It is different on different | |
1034 | * levels so we need to check the level before actioning it. | |
1035 | * Some times the level change needs to be requested after the | |
1036 | * reshape (e.g. raid6->raid5, raid5->raid0) | |
1037 | * | |
e86c9dd6 | 1038 | */ |
7236ee7a | 1039 | struct mdu_array_info_s array, orig; |
e86c9dd6 | 1040 | char *c; |
7236ee7a | 1041 | int rv = 0; |
e86c9dd6 | 1042 | struct supertype *st; |
4725bc31 | 1043 | char *subarray = NULL; |
e86c9dd6 | 1044 | |
e86c9dd6 NB |
1045 | int nchunk, ochunk; |
1046 | int nlayout, olayout; | |
1047 | int ndisks, odisks; | |
f21e18ca | 1048 | unsigned int ndata, odata; |
7236ee7a N |
1049 | int orig_level = UnSet; |
1050 | char alt_layout[40]; | |
e86c9dd6 NB |
1051 | int *fdlist; |
1052 | unsigned long long *offsets; | |
e6e9d47b | 1053 | int d; |
e86c9dd6 NB |
1054 | int nrdisks; |
1055 | int err; | |
7236ee7a | 1056 | int frozen; |
1c009fc2 | 1057 | unsigned long blocks, stripes; |
f21e18ca | 1058 | unsigned long cache; |
7236ee7a N |
1059 | unsigned long long array_size; |
1060 | int changed = 0; | |
7bc71196 DW |
1061 | char *container = NULL; |
1062 | int cfd = -1; | |
7236ee7a | 1063 | int done; |
e86c9dd6 | 1064 | |
7e0f6979 | 1065 | struct mdinfo *sra; |
06c7f68e | 1066 | struct mdinfo *sd; |
e86c9dd6 NB |
1067 | |
1068 | if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) { | |
1069 | fprintf(stderr, Name ": %s is not an active md array - aborting\n", | |
1070 | devname); | |
1071 | return 1; | |
1072 | } | |
24d40069 | 1073 | |
9ce510be N |
1074 | if (size >= 0 && |
1075 | (chunksize || level!= UnSet || layout_str || raid_disks)) { | |
1076 | fprintf(stderr, Name ": cannot change component size at the same time " | |
1077 | "as other changes.\n" | |
1078 | " Change size first, then check data is intact before " | |
1079 | "making other changes.\n"); | |
1080 | return 1; | |
1081 | } | |
1082 | ||
24d40069 N |
1083 | if (raid_disks && raid_disks < array.raid_disks && array.level > 1 && |
1084 | get_linux_version() < 2006032 && | |
1085 | !check_env("MDADM_FORCE_FEWER")) { | |
1086 | fprintf(stderr, Name ": reducing the number of devices is not safe before Linux 2.6.32\n" | |
1087 | " Please use a newer kernel\n"); | |
1088 | return 1; | |
1089 | } | |
7bc71196 DW |
1090 | |
1091 | st = super_by_fd(fd, &subarray); | |
1092 | if (!st) { | |
1093 | fprintf(stderr, Name ": Unable to determine metadata format for %s\n", devname); | |
1094 | return 1; | |
1095 | } | |
acab7bb1 N |
1096 | if (raid_disks > st->max_devs) { |
1097 | fprintf(stderr, Name ": Cannot increase raid-disks on this array" | |
1098 | " beyond %d\n", st->max_devs); | |
1099 | return 1; | |
1100 | } | |
7bc71196 DW |
1101 | |
1102 | /* in the external case we need to check that the requested reshape is | |
1103 | * supported, and perform an initial check that the container holds the | |
1104 | * pre-requisite spare devices (mdmon owns final validation) | |
1105 | */ | |
1106 | if (st->ss->external) { | |
1107 | int container_dev; | |
7f2ba464 | 1108 | int rv; |
7bc71196 DW |
1109 | |
1110 | if (subarray) { | |
1111 | container_dev = st->container_dev; | |
1112 | cfd = open_dev_excl(st->container_dev); | |
1113 | } else if (size >= 0 || layout_str != NULL || chunksize != 0 || | |
1114 | level != UnSet) { | |
1115 | fprintf(stderr, | |
1116 | Name ": %s is a container, only 'raid-devices' can be changed\n", | |
1117 | devname); | |
1118 | return 1; | |
1119 | } else { | |
1120 | container_dev = st->devnum; | |
1121 | close(fd); | |
1122 | cfd = open_dev_excl(st->devnum); | |
1123 | fd = cfd; | |
1124 | } | |
1125 | if (cfd < 0) { | |
1126 | fprintf(stderr, Name ": Unable to open container for %s\n", | |
1127 | devname); | |
7f2ba464 | 1128 | free(subarray); |
7bc71196 DW |
1129 | return 1; |
1130 | } | |
1131 | ||
1132 | container = devnum2devname(st->devnum); | |
1133 | if (!container) { | |
1134 | fprintf(stderr, Name ": Could not determine container name\n"); | |
7f2ba464 | 1135 | free(subarray); |
7bc71196 DW |
1136 | return 1; |
1137 | } | |
1138 | ||
7f2ba464 DW |
1139 | if (subarray) |
1140 | rv = st->ss->load_container(st, cfd, NULL); | |
1141 | else | |
1142 | rv = st->ss->load_super(st, cfd, NULL); | |
1143 | if (rv) { | |
7bc71196 DW |
1144 | fprintf(stderr, Name ": Cannot read superblock for %s\n", |
1145 | devname); | |
7f2ba464 | 1146 | free(subarray); |
7bc71196 DW |
1147 | return 1; |
1148 | } | |
1149 | ||
1150 | if (mdmon_running(container_dev)) | |
1151 | st->update_tail = &st->updates; | |
691a36b7 N |
1152 | } |
1153 | ||
1154 | if (raid_disks > array.raid_disks && | |
1155 | array.spare_disks < (raid_disks - array.raid_disks) && | |
1156 | !force) { | |
1157 | fprintf(stderr, | |
1158 | Name ": Need %d spare%s to avoid degraded array," | |
1159 | " and only have %d.\n" | |
1160 | " Use --force to over-ride this check.\n", | |
1161 | raid_disks - array.raid_disks, | |
1162 | raid_disks - array.raid_disks == 1 ? "" : "s", | |
1163 | array.spare_disks); | |
1164 | return 1; | |
7bc71196 DW |
1165 | } |
1166 | ||
62a48395 | 1167 | sra = sysfs_read(fd, 0, GET_LEVEL | GET_DISKS | GET_DEVS | GET_STATE); |
7bc71196 | 1168 | if (sra) { |
7f2ba464 | 1169 | if (st->ss->external && subarray == NULL) { |
7bc71196 DW |
1170 | array.level = LEVEL_CONTAINER; |
1171 | sra->array.level = LEVEL_CONTAINER; | |
1172 | } | |
7bc71196 | 1173 | } else { |
b526e52d DW |
1174 | fprintf(stderr, Name ": failed to read sysfs parameters for %s\n", |
1175 | devname); | |
1176 | return 1; | |
1177 | } | |
7f2ba464 DW |
1178 | frozen = freeze(st); |
1179 | if (frozen < -1) { | |
1180 | /* freeze() already spewed the reason */ | |
1181 | return 1; | |
1182 | } else if (frozen < 0) { | |
7236ee7a N |
1183 | fprintf(stderr, Name ": %s is performing resync/recovery and cannot" |
1184 | " be reshaped\n", devname); | |
1185 | return 1; | |
1186 | } | |
1187 | ||
1188 | /* ========= set size =============== */ | |
1189 | if (size >= 0 && (size == 0 || size != array.size)) { | |
7bc71196 DW |
1190 | long long orig_size = array.size; |
1191 | ||
1192 | if (reshape_super(st, size, UnSet, UnSet, 0, 0, NULL, devname, !quiet)) { | |
1193 | rv = 1; | |
1194 | goto release; | |
1195 | } | |
1196 | sync_metadata(st); | |
7236ee7a N |
1197 | array.size = size; |
1198 | if (array.size != size) { | |
1199 | /* got truncated to 32bit, write to | |
1200 | * component_size instead | |
1201 | */ | |
1202 | if (sra) | |
1203 | rv = sysfs_set_num(sra, NULL, | |
1204 | "component_size", size); | |
1205 | else | |
1206 | rv = -1; | |
1207 | } else | |
1208 | rv = ioctl(fd, SET_ARRAY_INFO, &array); | |
1209 | if (rv != 0) { | |
39bbb392 | 1210 | int err = errno; |
7bc71196 DW |
1211 | |
1212 | /* restore metadata */ | |
1213 | if (reshape_super(st, orig_size, UnSet, UnSet, 0, 0, | |
1214 | NULL, devname, !quiet) == 0) | |
1215 | sync_metadata(st); | |
7236ee7a | 1216 | fprintf(stderr, Name ": Cannot set device size for %s: %s\n", |
39bbb392 N |
1217 | devname, strerror(err)); |
1218 | if (err == EBUSY && | |
1219 | (array.state & (1<<MD_SB_BITMAP_PRESENT))) | |
1220 | fprintf(stderr, " Bitmap must be removed before size can be changed\n"); | |
7236ee7a N |
1221 | rv = 1; |
1222 | goto release; | |
1223 | } | |
1224 | ioctl(fd, GET_ARRAY_INFO, &array); | |
be1cabbd | 1225 | size = get_component_size(fd)/2; |
f98841b3 N |
1226 | if (size == 0) |
1227 | size = array.size; | |
7236ee7a | 1228 | if (!quiet) |
f98841b3 N |
1229 | fprintf(stderr, Name ": component size of %s has been set to %lluK\n", |
1230 | devname, size); | |
7236ee7a | 1231 | changed = 1; |
7bc71196 | 1232 | } else if (array.level != LEVEL_CONTAINER) { |
be1cabbd | 1233 | size = get_component_size(fd)/2; |
f98841b3 N |
1234 | if (size == 0) |
1235 | size = array.size; | |
7236ee7a N |
1236 | } |
1237 | ||
62a48395 AK |
1238 | /* ========= check for Raid10 -> Raid0 conversion =============== |
1239 | * current implemenation assumes that following conditions must be met: | |
1240 | * - far_copies == 1 | |
1241 | * - near_copies == 2 | |
1242 | */ | |
1243 | if (level == 0 && array.level == 10 && | |
1244 | array.layout == ((1 << 8) + 2) && !(array.raid_disks & 1)) { | |
1245 | int err; | |
1246 | err = remove_disks_on_raid10_to_raid0_takeover(st, sra, array.layout); | |
1247 | if (err) { | |
1248 | dprintf(Name": Array cannot be reshaped\n"); | |
1249 | if (container) | |
1250 | free(container); | |
1251 | if (cfd > -1) | |
1252 | close(cfd); | |
1253 | return 1; | |
1254 | } | |
1255 | } | |
1256 | ||
7236ee7a N |
1257 | /* ======= set level =========== */ |
1258 | if (level != UnSet && level != array.level) { | |
1259 | /* Trying to change the level. | |
1260 | * We might need to change layout first and schedule a | |
1261 | * level change for later. | |
1262 | * Level changes that can happen immediately are: | |
1263 | * 0->4,5,6 1->5 4->5,6 5->1,6 | |
1264 | * Level changes that need a layout change first are: | |
1265 | * 6->5,4,0 : need a -6 layout, or parity-last | |
1266 | * 5->4,0 : need parity-last | |
1267 | */ | |
1268 | if ((array.level == 6 || array.level == 5) && | |
1269 | (level == 5 || level == 4 || level == 0)) { | |
1270 | /* Don't change level yet, but choose intermediate | |
1271 | * layout | |
1272 | */ | |
1273 | if (level == 5) { | |
1274 | if (layout_str == NULL) | |
1275 | switch (array.layout) { | |
1276 | case ALGORITHM_LEFT_ASYMMETRIC: | |
1277 | case ALGORITHM_LEFT_ASYMMETRIC_6: | |
1278 | case ALGORITHM_ROTATING_N_RESTART: | |
1279 | layout_str = "left-asymmetric-6"; | |
1280 | break; | |
1281 | case ALGORITHM_LEFT_SYMMETRIC: | |
1282 | case ALGORITHM_LEFT_SYMMETRIC_6: | |
1283 | case ALGORITHM_ROTATING_N_CONTINUE: | |
1284 | layout_str = "left-symmetric-6"; | |
1285 | break; | |
1286 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
1287 | case ALGORITHM_RIGHT_ASYMMETRIC_6: | |
1288 | case ALGORITHM_ROTATING_ZERO_RESTART: | |
1289 | layout_str = "right-asymmetric-6"; | |
1290 | break; | |
1291 | case ALGORITHM_RIGHT_SYMMETRIC: | |
1292 | case ALGORITHM_RIGHT_SYMMETRIC_6: | |
1293 | layout_str = "right-symmetric-6"; | |
1294 | break; | |
1295 | case ALGORITHM_PARITY_0: | |
1296 | case ALGORITHM_PARITY_0_6: | |
1297 | layout_str = "parity-first-6"; | |
1298 | break; | |
1299 | case ALGORITHM_PARITY_N: | |
1300 | layout_str = "parity-last"; | |
1301 | break; | |
1302 | default: | |
1303 | fprintf(stderr, Name ": %s: cannot" | |
1304 | "convert layout to RAID5 equivalent\n", | |
1305 | devname); | |
1306 | rv = 1; | |
1307 | goto release; | |
1308 | } | |
1309 | else { | |
1310 | int l = map_name(r5layout, layout_str); | |
1311 | if (l == UnSet) { | |
1312 | fprintf(stderr, Name ": %s: layout '%s' not recognised\n", | |
1313 | devname, layout_str); | |
1314 | rv = 1; | |
1315 | goto release; | |
1316 | } | |
1317 | if (l != ALGORITHM_PARITY_N) { | |
1318 | /* need the -6 version */ | |
1319 | char *ls = map_num(r5layout, l); | |
1320 | strcat(strcpy(alt_layout, ls), | |
1321 | "-6"); | |
1322 | layout_str = alt_layout; | |
1323 | } | |
1324 | } | |
1325 | if (raid_disks) | |
24d40069 | 1326 | /* The final raid6->raid5 conversion |
7236ee7a N |
1327 | * will reduce the number of disks, |
1328 | * so now we need to aim higher | |
1329 | */ | |
1330 | raid_disks++; | |
1331 | } else | |
1332 | layout_str = "parity-last"; | |
1333 | } else { | |
7bc71196 DW |
1334 | /* Level change is a simple takeover. In the external |
1335 | * case we don't check with the metadata handler until | |
1336 | * we establish what the final layout will be. If the | |
1337 | * level change is disallowed we will revert to | |
1338 | * orig_level without disturbing the metadata, otherwise | |
1339 | * we will send an update. | |
1340 | */ | |
7236ee7a | 1341 | c = map_num(pers, level); |
b5ea446a N |
1342 | if (c == NULL) { |
1343 | rv = 1;/* not possible */ | |
1344 | goto release; | |
1345 | } | |
691a36b7 N |
1346 | if (!force) { |
1347 | /* Need to check there are enough spares */ | |
1348 | int spares_needed = 0; | |
1349 | switch (array.level * 16 + level) { | |
1350 | case 0x05: | |
1351 | spares_needed = 1; break; | |
1352 | case 0x06: | |
1353 | spares_needed = 2; break; | |
1354 | case 0x15: | |
1355 | spares_needed = 1; break; | |
1356 | case 0x16: | |
1357 | spares_needed = 2; break; | |
1358 | case 0x56: | |
1359 | spares_needed = 1; break; | |
1360 | } | |
1361 | if (raid_disks > array.raid_disks) | |
1362 | spares_needed += raid_disks-array.raid_disks; | |
1363 | if (spares_needed > array.spare_disks) { | |
1364 | fprintf(stderr, | |
1365 | Name ": Need %d spare%s to avoid" | |
1366 | " degraded array, and only have %d.\n" | |
1367 | " Use --force to over-ride" | |
1368 | " this check.\n", | |
1369 | spares_needed, | |
1370 | spares_needed == 1 ? "" : "s", | |
1371 | array.spare_disks); | |
1372 | rv = 1; | |
1373 | goto release; | |
1374 | } | |
1375 | } | |
7236ee7a N |
1376 | err = sysfs_set_str(sra, NULL, "level", c); |
1377 | if (err) { | |
39bbb392 | 1378 | err = errno; |
7236ee7a N |
1379 | fprintf(stderr, Name ": %s: could not set level to %s\n", |
1380 | devname, c); | |
39bbb392 N |
1381 | if (err == EBUSY && |
1382 | (array.state & (1<<MD_SB_BITMAP_PRESENT))) | |
1383 | fprintf(stderr, " Bitmap must be removed before level can be changed\n"); | |
7236ee7a N |
1384 | rv = 1; |
1385 | goto release; | |
1386 | } | |
1387 | orig = array; | |
1388 | orig_level = orig.level; | |
1389 | ioctl(fd, GET_ARRAY_INFO, &array); | |
1390 | if (layout_str == NULL && | |
1391 | orig.level == 5 && level == 6 && | |
1392 | array.layout != orig.layout) | |
1393 | layout_str = map_num(r5layout, orig.layout); | |
1394 | if (!quiet) | |
1395 | fprintf(stderr, Name " level of %s changed to %s\n", | |
1396 | devname, c); | |
1397 | changed = 1; | |
1398 | } | |
1399 | } | |
1400 | ||
1401 | /* ========= set shape (chunk_size / layout / ndisks) ============== */ | |
1402 | /* Check if layout change is a no-op */ | |
7bc71196 | 1403 | switch (array.level) { |
7236ee7a | 1404 | case 5: |
72e4a378 | 1405 | if (layout_str && array.layout == map_name(r5layout, layout_str)) |
7236ee7a N |
1406 | layout_str = NULL; |
1407 | break; | |
1408 | case 6: | |
1409 | if (layout_str == NULL && | |
1410 | ((chunksize && chunksize * 1024 != array.chunk_size) || | |
1411 | (raid_disks && raid_disks != array.raid_disks)) && | |
1412 | array.layout >= 16) { | |
1413 | fprintf(stderr, Name | |
1414 | ": %s has a non-standard layout. If you wish to preserve this\n" | |
1415 | " during the reshape, please specify --layout=preserve\n" | |
1416 | " If you want to change it, specify a layout or use --layout=normalise\n", | |
1417 | devname); | |
1418 | rv = 1; | |
1419 | goto release; | |
1420 | } | |
72e4a378 DW |
1421 | if (layout_str && |
1422 | (strcmp(layout_str, "normalise") == 0 || | |
1423 | strcmp(layout_str, "normalize") == 0)) { | |
7236ee7a N |
1424 | char *hyphen; |
1425 | strcpy(alt_layout, map_num(r6layout, array.layout)); | |
1426 | hyphen = strrchr(alt_layout, '-'); | |
1427 | if (hyphen && strcmp(hyphen, "-6") == 0) { | |
1428 | *hyphen = 0; | |
1429 | layout_str = alt_layout; | |
1430 | } | |
1431 | } | |
1432 | ||
72e4a378 | 1433 | if (layout_str && array.layout == map_name(r6layout, layout_str)) |
7236ee7a N |
1434 | layout_str = NULL; |
1435 | if (layout_str && strcmp(layout_str, "preserve") == 0) | |
1436 | layout_str = NULL; | |
1437 | break; | |
1438 | } | |
1439 | if (layout_str == NULL | |
1440 | && (chunksize == 0 || chunksize*1024 == array.chunk_size) | |
1441 | && (raid_disks == 0 || raid_disks == array.raid_disks)) { | |
7bc71196 DW |
1442 | if (reshape_super(st, -1, level, UnSet, 0, 0, NULL, devname, !quiet)) { |
1443 | rv = 1; | |
1444 | goto release; | |
1445 | } | |
1446 | sync_metadata(st); | |
7236ee7a N |
1447 | rv = 0; |
1448 | if (level != UnSet && level != array.level) { | |
1449 | /* Looks like this level change doesn't need | |
1450 | * a reshape after all. | |
1451 | */ | |
1452 | c = map_num(pers, level); | |
1453 | if (c) { | |
1454 | rv = sysfs_set_str(sra, NULL, "level", c); | |
39bbb392 N |
1455 | if (rv) { |
1456 | int err = errno; | |
7236ee7a N |
1457 | fprintf(stderr, Name ": %s: could not set level to %s\n", |
1458 | devname, c); | |
39bbb392 N |
1459 | if (err == EBUSY && |
1460 | (array.state & (1<<MD_SB_BITMAP_PRESENT))) | |
1461 | fprintf(stderr, " Bitmap must be removed before level can be changed\n"); | |
b7e734fc | 1462 | rv = 1; |
39bbb392 | 1463 | } |
7236ee7a N |
1464 | } |
1465 | } else if (!changed && !quiet) | |
1466 | fprintf(stderr, Name ": %s: no change requested\n", | |
1467 | devname); | |
7bc71196 DW |
1468 | |
1469 | if (st->ss->external && !mdmon_running(st->container_dev) && | |
1470 | level > 0) { | |
1471 | start_mdmon(st->container_dev); | |
1472 | ping_monitor(container); | |
1473 | } | |
7236ee7a N |
1474 | goto release; |
1475 | } | |
1476 | ||
e86c9dd6 NB |
1477 | c = map_num(pers, array.level); |
1478 | if (c == NULL) c = "-unknown-"; | |
7bc71196 | 1479 | switch (array.level) { |
e86c9dd6 NB |
1480 | default: /* raid0, linear, multipath cannot be reconfigured */ |
1481 | fprintf(stderr, Name ": %s array %s cannot be reshaped.\n", | |
1482 | c, devname); | |
7bc71196 | 1483 | /* TODO raid0 raiddisks can be reshaped via raid4 */ |
7236ee7a N |
1484 | rv = 1; |
1485 | break; | |
7bc71196 DW |
1486 | case LEVEL_CONTAINER: { |
1487 | int count; | |
1488 | ||
1489 | /* double check that we are not changing anything but raid_disks */ | |
1490 | if (size >= 0 || layout_str != NULL || chunksize != 0 || level != UnSet) { | |
1491 | fprintf(stderr, | |
1492 | Name ": %s is a container, only 'raid-devices' can be changed\n", | |
1493 | devname); | |
1494 | rv = 1; | |
1495 | goto release; | |
1496 | } | |
1497 | ||
1498 | st->update_tail = &st->updates; | |
1499 | if (reshape_super(st, -1, UnSet, UnSet, 0, raid_disks, | |
1500 | backup_file, devname, !quiet)) { | |
1501 | rv = 1; | |
1502 | goto release; | |
1503 | } | |
1504 | ||
1505 | count = reshape_container_raid_disks(container, raid_disks); | |
1506 | if (count < 0) { | |
1507 | revert_container_raid_disks(st, fd, container); | |
1508 | rv = 1; | |
1509 | goto release; | |
1510 | } else if (count == 0) { | |
1511 | if (!quiet) | |
1512 | fprintf(stderr, Name | |
1513 | ": no active subarrays to reshape\n"); | |
1514 | goto release; | |
1515 | } | |
e86c9dd6 | 1516 | |
7bc71196 DW |
1517 | if (!mdmon_running(st->devnum)) { |
1518 | start_mdmon(st->devnum); | |
1519 | ping_monitor(container); | |
1520 | } | |
1521 | sync_metadata(st); | |
1522 | ||
1523 | /* give mdmon a chance to allocate spares */ | |
1524 | ping_manager(container); | |
1525 | ||
1526 | /* manage_reshape takes care of releasing the array(s) */ | |
1527 | st->ss->manage_reshape(st, backup_file); | |
1528 | frozen = 0; | |
1529 | goto release; | |
1530 | } | |
e86c9dd6 NB |
1531 | case LEVEL_FAULTY: /* only 'layout' change is permitted */ |
1532 | ||
e86c9dd6 NB |
1533 | if (chunksize || raid_disks) { |
1534 | fprintf(stderr, Name ": %s: Cannot change chunksize or disks of a 'faulty' array\n", | |
1535 | devname); | |
7236ee7a N |
1536 | rv = 1; |
1537 | break; | |
e86c9dd6 | 1538 | } |
19678e53 | 1539 | if (layout_str == NULL) |
7236ee7a | 1540 | break; /* nothing to do.... */ |
e86c9dd6 | 1541 | |
19678e53 N |
1542 | array.layout = parse_layout_faulty(layout_str); |
1543 | if (array.layout < 0) { | |
1544 | fprintf(stderr, Name ": %s: layout %s not understood for 'faulty' array\n", | |
1545 | devname, layout_str); | |
7236ee7a N |
1546 | rv = 1; |
1547 | break; | |
19678e53 | 1548 | } |
e86c9dd6 NB |
1549 | if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) { |
1550 | fprintf(stderr, Name ": Cannot set layout for %s: %s\n", | |
1551 | devname, strerror(errno)); | |
7236ee7a N |
1552 | rv = 1; |
1553 | } else if (!quiet) | |
e86c9dd6 | 1554 | printf("layout for %s set to %d\n", devname, array.layout); |
7236ee7a | 1555 | break; |
e86c9dd6 | 1556 | |
7236ee7a | 1557 | case 1: /* only raid_disks can each be changed. */ |
e86c9dd6 | 1558 | |
19678e53 | 1559 | if (chunksize || layout_str != NULL) { |
7236ee7a | 1560 | fprintf(stderr, Name ": %s: Cannot change chunk size or layout for a RAID1 array.\n", |
e86c9dd6 | 1561 | devname); |
7236ee7a N |
1562 | rv = 1; |
1563 | break; | |
e86c9dd6 | 1564 | } |
9860f271 | 1565 | if (raid_disks > 0) { |
7bc71196 DW |
1566 | if (reshape_super(st, -1, UnSet, UnSet, 0, raid_disks, |
1567 | NULL, devname, !quiet)) { | |
1568 | rv = 1; | |
1569 | goto release; | |
1570 | } | |
1571 | sync_metadata(st); | |
e86c9dd6 | 1572 | array.raid_disks = raid_disks; |
9860f271 NB |
1573 | if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) { |
1574 | fprintf(stderr, Name ": Cannot set raid-devices for %s: %s\n", | |
1575 | devname, strerror(errno)); | |
7236ee7a | 1576 | rv = 1; |
9860f271 | 1577 | } |
e86c9dd6 | 1578 | } |
7236ee7a | 1579 | break; |
e86c9dd6 NB |
1580 | |
1581 | case 4: | |
1582 | case 5: | |
1583 | case 6: | |
1686dc25 | 1584 | |
7236ee7a N |
1585 | /* |
1586 | * layout/chunksize/raid_disks can be changed | |
e86c9dd6 | 1587 | * though the kernel may not support it all. |
e86c9dd6 | 1588 | */ |
4725bc31 N |
1589 | if (subarray) { |
1590 | fprintf(stderr, Name ": Cannot reshape subarrays yet\n"); | |
1591 | break; | |
1592 | } | |
7236ee7a N |
1593 | |
1594 | /* | |
1595 | * There are three possibilities. | |
1596 | * 1/ The array will shrink. | |
1597 | * We need to ensure the reshape will pause before reaching | |
1598 | * the 'critical section'. We also need to fork and wait for | |
1599 | * that to happen. When it does we | |
1600 | * suspend/backup/complete/unfreeze | |
1601 | * | |
1602 | * 2/ The array will not change size. | |
1603 | * This requires that we keep a backup of a sliding window | |
1604 | * so that we can restore data after a crash. So we need | |
1605 | * to fork and monitor progress. | |
1606 | * | |
1607 | * 3/ The array will grow. This is relatively easy. | |
e86c9dd6 NB |
1608 | * However the kernel's restripe routines will cheerfully |
1609 | * overwrite some early data before it is safe. So we | |
1610 | * need to make a backup of the early parts of the array | |
1611 | * and be ready to restore it if rebuild aborts very early. | |
1612 | * | |
7236ee7a N |
1613 | * We backup data by writing it to one spare, or to a |
1614 | * file which was given on command line. | |
e86c9dd6 | 1615 | * |
7236ee7a | 1616 | * [FOLLOWING IS OLD AND PARTLY WRONG] |
e86c9dd6 NB |
1617 | * So: we enumerate the devices in the array and |
1618 | * make sure we can open all of them. | |
1619 | * Then we freeze the early part of the array and | |
1620 | * backup to the various spares. | |
1621 | * Then we request changes and start the reshape. | |
1622 | * Monitor progress until it has passed the danger zone. | |
1623 | * and finally invalidate the copied data and unfreeze the | |
1624 | * start of the array. | |
1625 | * | |
7236ee7a N |
1626 | * In each case, we first make sure that storage is available |
1627 | * for the required backup. | |
1628 | * Then we: | |
1629 | * - request the shape change. | |
1630 | * - for to handle backup etc. | |
e86c9dd6 | 1631 | */ |
e86c9dd6 NB |
1632 | nchunk = ochunk = array.chunk_size; |
1633 | nlayout = olayout = array.layout; | |
1634 | ndisks = odisks = array.raid_disks; | |
1635 | ||
7236ee7a N |
1636 | if (chunksize) { |
1637 | nchunk = chunksize * 1024; | |
f98841b3 N |
1638 | if (size % chunksize) { |
1639 | fprintf(stderr, Name ": component size %lluK is not" | |
7236ee7a | 1640 | " a multiple of chunksize %dK\n", |
f98841b3 | 1641 | size, chunksize); |
7236ee7a N |
1642 | break; |
1643 | } | |
1644 | } | |
19678e53 | 1645 | if (layout_str != NULL) |
7236ee7a | 1646 | switch(array.level) { |
19678e53 N |
1647 | case 4: /* ignore layout */ |
1648 | break; | |
1649 | case 5: | |
1650 | nlayout = map_name(r5layout, layout_str); | |
1651 | if (nlayout == UnSet) { | |
1652 | fprintf(stderr, Name ": layout %s not understood for raid5.\n", | |
1653 | layout_str); | |
b5ea446a N |
1654 | rv = 1; |
1655 | goto release; | |
19678e53 N |
1656 | } |
1657 | break; | |
1658 | ||
1659 | case 6: | |
1660 | nlayout = map_name(r6layout, layout_str); | |
1661 | if (nlayout == UnSet) { | |
1662 | fprintf(stderr, Name ": layout %s not understood for raid6.\n", | |
1663 | layout_str); | |
b5ea446a N |
1664 | rv = 1; |
1665 | goto release; | |
19678e53 N |
1666 | } |
1667 | break; | |
1668 | } | |
e86c9dd6 NB |
1669 | if (raid_disks) ndisks = raid_disks; |
1670 | ||
1671 | odata = odisks-1; | |
e86c9dd6 | 1672 | ndata = ndisks-1; |
7236ee7a N |
1673 | if (array.level == 6) { |
1674 | odata--; /* number of data disks */ | |
1675 | ndata--; | |
e86c9dd6 | 1676 | } |
7236ee7a | 1677 | |
d2505cff N |
1678 | if (odata == ndata && |
1679 | get_linux_version() < 2006032) { | |
1680 | fprintf(stderr, Name ": in-place reshape is not safe before 2.6.32, sorry.\n"); | |
1681 | break; | |
1682 | } | |
1683 | ||
7236ee7a | 1684 | /* Check that we can hold all the data */ |
7236ee7a | 1685 | get_dev_size(fd, NULL, &array_size); |
f21e18ca | 1686 | if (ndata * (unsigned long long)size < (array_size/1024)) { |
7236ee7a N |
1687 | fprintf(stderr, Name ": this change will reduce the size of the array.\n" |
1688 | " use --grow --array-size first to truncate array.\n" | |
1689 | " e.g. mdadm --grow %s --array-size %llu\n", | |
f98841b3 | 1690 | devname, ndata * size); |
7236ee7a N |
1691 | rv = 1; |
1692 | break; | |
e86c9dd6 | 1693 | } |
7236ee7a | 1694 | |
1c009fc2 | 1695 | blocks = compute_backup_blocks(nchunk, ochunk, ndata, odata); |
7236ee7a | 1696 | |
e380d3be N |
1697 | sysfs_free(sra); |
1698 | sra = sysfs_read(fd, 0, | |
1699 | GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE| | |
1700 | GET_CACHE); | |
1701 | ||
c03ef02d N |
1702 | if (!sra) { |
1703 | fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n", | |
1704 | devname); | |
1705 | rv = 1; | |
1706 | break; | |
1707 | } | |
1708 | ||
eba71529 N |
1709 | if (ndata == odata) { |
1710 | /* Make 'blocks' bigger for better throughput, but | |
1711 | * not so big that we reject it below. | |
1b13faf7 | 1712 | * Try for 16 megabytes |
eba71529 | 1713 | */ |
1b13faf7 N |
1714 | while (blocks * 32 < sra->component_size && |
1715 | blocks < 16*1024*2) | |
1716 | blocks *= 2; | |
eba71529 | 1717 | } else |
7236ee7a N |
1718 | fprintf(stderr, Name ": Need to backup %luK of critical " |
1719 | "section..\n", blocks/2); | |
e86c9dd6 | 1720 | |
7236ee7a | 1721 | if (blocks >= sra->component_size/2) { |
e86c9dd6 NB |
1722 | fprintf(stderr, Name ": %s: Something wrong - reshape aborted\n", |
1723 | devname); | |
7236ee7a N |
1724 | rv = 1; |
1725 | break; | |
353632d9 | 1726 | } |
925211e3 | 1727 | nrdisks = array.raid_disks + sra->array.spare_disks; |
e86c9dd6 NB |
1728 | /* Now we need to open all these devices so we can read/write. |
1729 | */ | |
06b0d786 NB |
1730 | fdlist = malloc((1+nrdisks) * sizeof(int)); |
1731 | offsets = malloc((1+nrdisks) * sizeof(offsets[0])); | |
e86c9dd6 NB |
1732 | if (!fdlist || !offsets) { |
1733 | fprintf(stderr, Name ": malloc failed: grow aborted\n"); | |
7236ee7a N |
1734 | rv = 1; |
1735 | break; | |
e86c9dd6 | 1736 | } |
130994cb AK |
1737 | |
1738 | d = reshape_prepare_fdlist(devname, sra, array.raid_disks, | |
1739 | nrdisks, blocks, backup_file, | |
1740 | fdlist, offsets); | |
1741 | if (d < 0) { | |
1742 | rv = 1; | |
1743 | goto release; | |
e86c9dd6 | 1744 | } |
7236ee7a | 1745 | if (backup_file == NULL) { |
7bc71196 DW |
1746 | if (st->ss->external && !st->ss->manage_reshape) { |
1747 | fprintf(stderr, Name ": %s Grow operation not supported by %s metadata\n", | |
1748 | devname, st->ss->name); | |
1749 | rv = 1; | |
1750 | break; | |
1751 | } | |
7236ee7a N |
1752 | if (ndata <= odata) { |
1753 | fprintf(stderr, Name ": %s: Cannot grow - need backup-file\n", | |
1754 | devname); | |
1755 | rv = 1; | |
1756 | break; | |
1757 | } else if (sra->array.spare_disks == 0) { | |
1758 | fprintf(stderr, Name ": %s: Cannot grow - need a spare or " | |
1759 | "backup-file to backup critical section\n", | |
1760 | devname); | |
1761 | rv = 1; | |
1762 | break; | |
1763 | } | |
1764 | if (d == array.raid_disks) { | |
1765 | fprintf(stderr, Name ": %s: No spare device for backup\n", | |
1766 | devname); | |
1767 | rv = 1; | |
1768 | break; | |
e86c9dd6 | 1769 | } |
7236ee7a | 1770 | } else { |
e6e9d47b AK |
1771 | if (!reshape_open_backup_file(backup_file, fd, devname, |
1772 | (signed)blocks, | |
1773 | fdlist+d, offsets+d)) { | |
7236ee7a N |
1774 | rv = 1; |
1775 | break; | |
06b0d786 | 1776 | } |
06b0d786 | 1777 | d++; |
06b0d786 | 1778 | } |
7236ee7a | 1779 | |
7bc71196 DW |
1780 | /* check that the operation is supported by the metadata */ |
1781 | if (reshape_super(st, -1, level, nlayout, nchunk, ndisks, | |
1782 | backup_file, devname, !quiet)) { | |
1783 | rv = 1; | |
1784 | break; | |
1785 | } | |
1786 | ||
7236ee7a N |
1787 | /* lastly, check that the internal stripe cache is |
1788 | * large enough, or it won't work. | |
1789 | */ | |
1790 | ||
1791 | cache = (nchunk < ochunk) ? ochunk : nchunk; | |
1792 | cache = cache * 4 / 4096; | |
1b13faf7 N |
1793 | if (cache < blocks / 8 / odisks + 16) |
1794 | /* Make it big enough to hold 'blocks' */ | |
1795 | cache = blocks / 8 / odisks + 16; | |
7236ee7a N |
1796 | if (sra->cache_size < cache) |
1797 | sysfs_set_num(sra, NULL, "stripe_cache_size", | |
1798 | cache+1); | |
1799 | /* Right, everything seems fine. Let's kick things off. | |
1800 | * If only changing raid_disks, use ioctl, else use | |
1801 | * sysfs. | |
1802 | */ | |
7bc71196 | 1803 | sync_metadata(st); |
7236ee7a N |
1804 | if (ochunk == nchunk && olayout == nlayout) { |
1805 | array.raid_disks = ndisks; | |
1806 | if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) { | |
39bbb392 | 1807 | int err = errno; |
7236ee7a N |
1808 | rv = 1; |
1809 | fprintf(stderr, Name ": Cannot set device shape for %s: %s\n", | |
1810 | devname, strerror(errno)); | |
1811 | if (ndisks < odisks && | |
1812 | get_linux_version() < 2006030) | |
1813 | fprintf(stderr, Name ": linux 2.6.30 or later required\n"); | |
39bbb392 N |
1814 | if (err == EBUSY && |
1815 | (array.state & (1<<MD_SB_BITMAP_PRESENT))) | |
1816 | fprintf(stderr, " Bitmap must be removed before shape can be changed\n"); | |
7236ee7a N |
1817 | |
1818 | break; | |
1819 | } | |
1820 | } else { | |
1821 | /* set them all just in case some old 'new_*' value | |
1822 | * persists from some earlier problem | |
1823 | */ | |
53f50353 N |
1824 | int err = err; /* only used if rv==1, and always set if |
1825 | * rv==1, so initialisation not needed, | |
1826 | * despite gcc warning | |
1827 | */ | |
7236ee7a | 1828 | if (sysfs_set_num(sra, NULL, "chunk_size", nchunk) < 0) |
39bbb392 N |
1829 | rv = 1, err = errno; |
1830 | if (!rv && sysfs_set_num(sra, NULL, "layout", nlayout) < 0) | |
1831 | rv = 1, err = errno; | |
1832 | if (!rv && sysfs_set_num(sra, NULL, "raid_disks", ndisks) < 0) | |
1833 | rv = 1, err = errno; | |
7236ee7a N |
1834 | if (rv) { |
1835 | fprintf(stderr, Name ": Cannot set device shape for %s\n", | |
1836 | devname); | |
1837 | if (get_linux_version() < 2006030) | |
1838 | fprintf(stderr, Name ": linux 2.6.30 or later required\n"); | |
39bbb392 N |
1839 | if (err == EBUSY && |
1840 | (array.state & (1<<MD_SB_BITMAP_PRESENT))) | |
1841 | fprintf(stderr, " Bitmap must be removed before shape can be changed\n"); | |
7236ee7a N |
1842 | break; |
1843 | } | |
e86c9dd6 NB |
1844 | } |
1845 | ||
7236ee7a N |
1846 | if (ndisks == 2 && odisks == 2) { |
1847 | /* No reshape is needed in this trivial case */ | |
1848 | rv = 0; | |
1849 | break; | |
1850 | } | |
1851 | ||
7bc71196 DW |
1852 | if (st->ss->external) { |
1853 | /* metadata handler takes it from here */ | |
1854 | ping_manager(container); | |
1855 | st->ss->manage_reshape(st, backup_file); | |
1856 | frozen = 0; | |
1857 | break; | |
1858 | } | |
1859 | ||
7236ee7a N |
1860 | /* set up the backup-super-block. This requires the |
1861 | * uuid from the array. | |
1862 | */ | |
e86c9dd6 | 1863 | /* Find a superblock */ |
7236ee7a N |
1864 | for (sd = sra->devs; sd; sd = sd->next) { |
1865 | char *dn; | |
1866 | int devfd; | |
1867 | int ok; | |
1868 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) | |
1869 | continue; | |
1870 | dn = map_dev(sd->disk.major, sd->disk.minor, 1); | |
1871 | devfd = dev_open(dn, O_RDONLY); | |
1872 | if (devfd < 0) | |
1873 | continue; | |
1874 | ok = st->ss->load_super(st, devfd, NULL); | |
1875 | close(devfd); | |
1876 | if (ok >= 0) | |
1877 | break; | |
1878 | } | |
1879 | if (!sd) { | |
e86c9dd6 NB |
1880 | fprintf(stderr, Name ": %s: Cannot find a superblock\n", |
1881 | devname); | |
7236ee7a N |
1882 | rv = 1; |
1883 | break; | |
e86c9dd6 NB |
1884 | } |
1885 | ||
7236ee7a | 1886 | memset(&bsb, 0, 512); |
2efedc7b | 1887 | memcpy(bsb.magic, "md_backup_data-1", 16); |
3da92f27 | 1888 | st->ss->uuid_from_super(st, (int*)&bsb.set_uuid); |
2efedc7b | 1889 | bsb.mtime = __cpu_to_le64(time(0)); |
7236ee7a N |
1890 | bsb.devstart2 = blocks; |
1891 | stripes = blocks / (ochunk/512) / odata; | |
1892 | /* Now we just need to kick off the reshape and watch, while | |
1893 | * handling backups of the data... | |
1894 | * This is all done by a forked background process. | |
2efedc7b | 1895 | */ |
7236ee7a N |
1896 | switch(fork()) { |
1897 | case 0: | |
1898 | close(fd); | |
1899 | if (check_env("MDADM_GROW_VERIFY")) | |
1900 | fd = open(devname, O_RDONLY | O_DIRECT); | |
1901 | else | |
1902 | fd = -1; | |
1903 | mlockall(MCL_FUTURE); | |
1904 | ||
1905 | if (odata < ndata) | |
1906 | done = child_grow(fd, sra, stripes, | |
1907 | fdlist, offsets, | |
1908 | odisks, ochunk, array.level, olayout, odata, | |
1909 | d - odisks, fdlist+odisks, offsets+odisks); | |
1910 | else if (odata > ndata) | |
1911 | done = child_shrink(fd, sra, stripes, | |
1912 | fdlist, offsets, | |
1913 | odisks, ochunk, array.level, olayout, odata, | |
1914 | d - odisks, fdlist+odisks, offsets+odisks); | |
1915 | else | |
1916 | done = child_same_size(fd, sra, stripes, | |
1917 | fdlist, offsets, | |
e9e43ec3 | 1918 | 0, |
7236ee7a N |
1919 | odisks, ochunk, array.level, olayout, odata, |
1920 | d - odisks, fdlist+odisks, offsets+odisks); | |
1921 | if (backup_file && done) | |
1922 | unlink(backup_file); | |
1923 | if (level != UnSet && level != array.level) { | |
1924 | /* We need to wait for the reshape to finish | |
1925 | * (which will have happened unless odata < ndata) | |
1926 | * and then set the level | |
758d3a8e | 1927 | */ |
7236ee7a N |
1928 | |
1929 | c = map_num(pers, level); | |
1930 | if (c == NULL) | |
1931 | exit(0);/* not possible */ | |
1932 | ||
1933 | if (odata < ndata) | |
1934 | wait_reshape(sra); | |
1935 | err = sysfs_set_str(sra, NULL, "level", c); | |
1936 | if (err) | |
1937 | fprintf(stderr, Name ": %s: could not set level to %s\n", | |
1938 | devname, c); | |
758d3a8e | 1939 | } |
7236ee7a N |
1940 | exit(0); |
1941 | case -1: | |
1942 | fprintf(stderr, Name ": Cannot run child to monitor reshape: %s\n", | |
1943 | strerror(errno)); | |
1944 | rv = 1; | |
1945 | break; | |
1946 | default: | |
1947 | /* The child will take care of unfreezing the array */ | |
1948 | frozen = 0; | |
1949 | break; | |
e86c9dd6 | 1950 | } |
7236ee7a | 1951 | break; |
e86c9dd6 | 1952 | |
7236ee7a | 1953 | } |
e86c9dd6 | 1954 | |
7236ee7a N |
1955 | release: |
1956 | if (rv && orig_level != UnSet && sra) { | |
1957 | c = map_num(pers, orig_level); | |
1958 | if (c && sysfs_set_str(sra, NULL, "level", c) == 0) | |
1959 | fprintf(stderr, Name ": aborting level change\n"); | |
1960 | } | |
7f2ba464 | 1961 | unfreeze(st, frozen); |
7236ee7a N |
1962 | return rv; |
1963 | } | |
e86c9dd6 | 1964 | |
7236ee7a N |
1965 | /* |
1966 | * We run a child process in the background which performs the following | |
1967 | * steps: | |
1968 | * - wait for resync to reach a certain point | |
1969 | * - suspend io to the following section | |
1970 | * - backup that section | |
1971 | * - allow resync to proceed further | |
1972 | * - resume io | |
1973 | * - discard the backup. | |
1974 | * | |
1975 | * When are combined in slightly different ways in the three cases. | |
1976 | * Grow: | |
1977 | * - suspend/backup/allow/wait/resume/discard | |
1978 | * Shrink: | |
1979 | * - allow/wait/suspend/backup/allow/wait/resume/discard | |
1980 | * same-size: | |
1981 | * - wait/resume/discard/suspend/backup/allow | |
1982 | * | |
1983 | * suspend/backup/allow always come together | |
1984 | * wait/resume/discard do too. | |
1985 | * For the same-size case we have two backups to improve flow. | |
1986 | * | |
1987 | */ | |
e86c9dd6 | 1988 | |
fcf57625 | 1989 | /* FIXME return status is never checked */ |
4411fb17 | 1990 | static int grow_backup(struct mdinfo *sra, |
7236ee7a N |
1991 | unsigned long long offset, /* per device */ |
1992 | unsigned long stripes, /* per device */ | |
1993 | int *sources, unsigned long long *offsets, | |
1994 | int disks, int chunk, int level, int layout, | |
1995 | int dests, int *destfd, unsigned long long *destoffsets, | |
d4445387 | 1996 | int part, int *degraded, |
7236ee7a N |
1997 | char *buf) |
1998 | { | |
1999 | /* Backup 'blocks' sectors at 'offset' on each device of the array, | |
2000 | * to storage 'destfd' (offset 'destoffsets'), after first | |
2001 | * suspending IO. Then allow resync to continue | |
2002 | * over the suspended section. | |
2003 | * Use part 'part' of the backup-super-block. | |
2004 | */ | |
2005 | int odata = disks; | |
2006 | int rv = 0; | |
2007 | int i; | |
f21e18ca N |
2008 | unsigned long long ll; |
2009 | int new_degraded; | |
7236ee7a N |
2010 | //printf("offset %llu\n", offset); |
2011 | if (level >= 4) | |
2012 | odata--; | |
2013 | if (level == 6) | |
2014 | odata--; | |
200871ad | 2015 | sysfs_set_num(sra, NULL, "suspend_hi", (offset + stripes * (chunk/512)) * odata); |
d4445387 | 2016 | /* Check that array hasn't become degraded, else we might backup the wrong data */ |
f21e18ca N |
2017 | sysfs_get_ll(sra, NULL, "degraded", &ll); |
2018 | new_degraded = (int)ll; | |
d4445387 N |
2019 | if (new_degraded != *degraded) { |
2020 | /* check each device to ensure it is still working */ | |
2021 | struct mdinfo *sd; | |
2022 | for (sd = sra->devs ; sd ; sd = sd->next) { | |
2023 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) | |
2024 | continue; | |
2025 | if (sd->disk.state & (1<<MD_DISK_SYNC)) { | |
2026 | char sbuf[20]; | |
2027 | if (sysfs_get_str(sra, sd, "state", sbuf, 20) < 0 || | |
2028 | strstr(sbuf, "faulty") || | |
2029 | strstr(sbuf, "in_sync") == NULL) { | |
2030 | /* this device is dead */ | |
2031 | sd->disk.state = (1<<MD_DISK_FAULTY); | |
2032 | if (sd->disk.raid_disk >= 0 && | |
2033 | sources[sd->disk.raid_disk] >= 0) { | |
2034 | close(sources[sd->disk.raid_disk]); | |
2035 | sources[sd->disk.raid_disk] = -1; | |
2036 | } | |
2037 | } | |
2038 | } | |
2039 | } | |
2040 | *degraded = new_degraded; | |
2041 | } | |
7236ee7a N |
2042 | if (part) { |
2043 | bsb.arraystart2 = __cpu_to_le64(offset * odata); | |
200871ad | 2044 | bsb.length2 = __cpu_to_le64(stripes * (chunk/512) * odata); |
7236ee7a N |
2045 | } else { |
2046 | bsb.arraystart = __cpu_to_le64(offset * odata); | |
200871ad | 2047 | bsb.length = __cpu_to_le64(stripes * (chunk/512) * odata); |
7236ee7a N |
2048 | } |
2049 | if (part) | |
2050 | bsb.magic[15] = '2'; | |
2051 | for (i = 0; i < dests; i++) | |
2052 | if (part) | |
2053 | lseek64(destfd[i], destoffsets[i] + __le64_to_cpu(bsb.devstart2)*512, 0); | |
2054 | else | |
2055 | lseek64(destfd[i], destoffsets[i], 0); | |
2056 | ||
2057 | rv = save_stripes(sources, offsets, | |
2058 | disks, chunk, level, layout, | |
2059 | dests, destfd, | |
2060 | offset*512*odata, stripes * chunk * odata, | |
2061 | buf); | |
2062 | ||
2063 | if (rv) | |
2064 | return rv; | |
97396422 | 2065 | bsb.mtime = __cpu_to_le64(time(0)); |
7236ee7a N |
2066 | for (i = 0; i < dests; i++) { |
2067 | bsb.devstart = __cpu_to_le64(destoffsets[i]/512); | |
2068 | ||
2069 | bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb)); | |
2070 | if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0) | |
2071 | bsb.sb_csum2 = bsb_csum((char*)&bsb, | |
2072 | ((char*)&bsb.sb_csum2)-((char*)&bsb)); | |
2073 | ||
72044953 | 2074 | rv = -1; |
f21e18ca N |
2075 | if ((unsigned long long)lseek64(destfd[i], destoffsets[i] - 4096, 0) |
2076 | != destoffsets[i] - 4096) | |
72044953 N |
2077 | break; |
2078 | if (write(destfd[i], &bsb, 512) != 512) | |
2079 | break; | |
ff94fb86 | 2080 | if (destoffsets[i] > 4096) { |
f21e18ca | 2081 | if ((unsigned long long)lseek64(destfd[i], destoffsets[i]+stripes*chunk*odata, 0) != |
a847575a | 2082 | destoffsets[i]+stripes*chunk*odata) |
72044953 N |
2083 | break; |
2084 | if (write(destfd[i], &bsb, 512) != 512) | |
2085 | break; | |
ff94fb86 | 2086 | } |
7236ee7a | 2087 | fsync(destfd[i]); |
72044953 | 2088 | rv = 0; |
7236ee7a | 2089 | } |
2efedc7b | 2090 | |
fcf57625 | 2091 | return rv; |
7236ee7a N |
2092 | } |
2093 | ||
2094 | /* in 2.6.30, the value reported by sync_completed can be | |
2095 | * less that it should be by one stripe. | |
2096 | * This only happens when reshape hits sync_max and pauses. | |
2097 | * So allow wait_backup to either extent sync_max further | |
2098 | * than strictly necessary, or return before the | |
2099 | * sync has got quite as far as we would really like. | |
2100 | * This is what 'blocks2' is for. | |
2101 | * The various caller give appropriate values so that | |
2102 | * every works. | |
2103 | */ | |
fcf57625 | 2104 | /* FIXME return value is often ignored */ |
4411fb17 | 2105 | static int wait_backup(struct mdinfo *sra, |
7236ee7a N |
2106 | unsigned long long offset, /* per device */ |
2107 | unsigned long long blocks, /* per device */ | |
2108 | unsigned long long blocks2, /* per device - hack */ | |
2109 | int dests, int *destfd, unsigned long long *destoffsets, | |
2110 | int part) | |
2111 | { | |
2112 | /* Wait for resync to pass the section that was backed up | |
2113 | * then erase the backup and allow IO | |
2114 | */ | |
2115 | int fd = sysfs_get_fd(sra, NULL, "sync_completed"); | |
2116 | unsigned long long completed; | |
2117 | int i; | |
fcf57625 | 2118 | int rv; |
7236ee7a N |
2119 | |
2120 | if (fd < 0) | |
2121 | return -1; | |
2122 | sysfs_set_num(sra, NULL, "sync_max", offset + blocks + blocks2); | |
2123 | if (offset == 0) | |
2124 | sysfs_set_str(sra, NULL, "sync_action", "reshape"); | |
9376b5aa AK |
2125 | |
2126 | if (sysfs_fd_get_ll(fd, &completed) < 0) { | |
2127 | close(fd); | |
2128 | return -1; | |
2129 | } | |
2130 | while (completed < offset + blocks) { | |
7236ee7a N |
2131 | char action[20]; |
2132 | fd_set rfds; | |
2133 | FD_ZERO(&rfds); | |
2134 | FD_SET(fd, &rfds); | |
2135 | select(fd+1, NULL, NULL, &rfds, NULL); | |
2136 | if (sysfs_fd_get_ll(fd, &completed) < 0) { | |
2137 | close(fd); | |
2138 | return -1; | |
e86c9dd6 | 2139 | } |
7236ee7a N |
2140 | if (sysfs_get_str(sra, NULL, "sync_action", |
2141 | action, 20) > 0 && | |
2142 | strncmp(action, "reshape", 7) != 0) | |
2143 | break; | |
9376b5aa | 2144 | } |
7236ee7a N |
2145 | close(fd); |
2146 | ||
2147 | if (part) { | |
2148 | bsb.arraystart2 = __cpu_to_le64(0); | |
2149 | bsb.length2 = __cpu_to_le64(0); | |
2150 | } else { | |
2151 | bsb.arraystart = __cpu_to_le64(0); | |
2152 | bsb.length = __cpu_to_le64(0); | |
2153 | } | |
97396422 | 2154 | bsb.mtime = __cpu_to_le64(time(0)); |
fcf57625 | 2155 | rv = 0; |
7236ee7a N |
2156 | for (i = 0; i < dests; i++) { |
2157 | bsb.devstart = __cpu_to_le64(destoffsets[i]/512); | |
2158 | bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb)); | |
2159 | if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0) | |
2160 | bsb.sb_csum2 = bsb_csum((char*)&bsb, | |
2161 | ((char*)&bsb.sb_csum2)-((char*)&bsb)); | |
f21e18ca | 2162 | if ((unsigned long long)lseek64(destfd[i], destoffsets[i]-4096, 0) != |
a847575a | 2163 | destoffsets[i]-4096) |
72044953 N |
2164 | rv = -1; |
2165 | if (rv == 0 && | |
2166 | write(destfd[i], &bsb, 512) != 512) | |
2167 | rv = -1; | |
7236ee7a N |
2168 | fsync(destfd[i]); |
2169 | } | |
fcf57625 | 2170 | return rv; |
7236ee7a | 2171 | } |
e86c9dd6 | 2172 | |
7236ee7a N |
2173 | static void fail(char *msg) |
2174 | { | |
fcf57625 | 2175 | int rv; |
72044953 N |
2176 | rv = (write(2, msg, strlen(msg)) != (int)strlen(msg)); |
2177 | rv |= (write(2, "\n", 1) != 1); | |
fcf57625 | 2178 | exit(rv ? 1 : 2); |
7236ee7a N |
2179 | } |
2180 | ||
2181 | static char *abuf, *bbuf; | |
f21e18ca | 2182 | static unsigned long long abuflen; |
7236ee7a N |
2183 | static void validate(int afd, int bfd, unsigned long long offset) |
2184 | { | |
2185 | /* check that the data in the backup against the array. | |
2186 | * This is only used for regression testing and should not | |
2187 | * be used while the array is active | |
2188 | */ | |
7236ee7a N |
2189 | if (afd < 0) |
2190 | return; | |
2191 | lseek64(bfd, offset - 4096, 0); | |
2192 | if (read(bfd, &bsb2, 512) != 512) | |
2193 | fail("cannot read bsb"); | |
2194 | if (bsb2.sb_csum != bsb_csum((char*)&bsb2, | |
2195 | ((char*)&bsb2.sb_csum)-((char*)&bsb2))) | |
2196 | fail("first csum bad"); | |
2197 | if (memcmp(bsb2.magic, "md_backup_data", 14) != 0) | |
2198 | fail("magic is bad"); | |
2199 | if (memcmp(bsb2.magic, "md_backup_data-2", 16) == 0 && | |
2200 | bsb2.sb_csum2 != bsb_csum((char*)&bsb2, | |
2201 | ((char*)&bsb2.sb_csum2)-((char*)&bsb2))) | |
2202 | fail("second csum bad"); | |
2203 | ||
2204 | if (__le64_to_cpu(bsb2.devstart)*512 != offset) | |
2205 | fail("devstart is wrong"); | |
2206 | ||
2207 | if (bsb2.length) { | |
2208 | unsigned long long len = __le64_to_cpu(bsb2.length)*512; | |
2209 | ||
2210 | if (abuflen < len) { | |
2211 | free(abuf); | |
2212 | free(bbuf); | |
2213 | abuflen = len; | |
fcf57625 N |
2214 | if (posix_memalign((void**)&abuf, 4096, abuflen) || |
2215 | posix_memalign((void**)&bbuf, 4096, abuflen)) { | |
2216 | abuflen = 0; | |
2217 | /* just stop validating on mem-alloc failure */ | |
2218 | return; | |
2219 | } | |
e86c9dd6 | 2220 | } |
48924014 | 2221 | |
7236ee7a | 2222 | lseek64(bfd, offset, 0); |
f21e18ca | 2223 | if ((unsigned long long)read(bfd, bbuf, len) != len) { |
080fd005 | 2224 | //printf("len %llu\n", len); |
7236ee7a N |
2225 | fail("read first backup failed"); |
2226 | } | |
2227 | lseek64(afd, __le64_to_cpu(bsb2.arraystart)*512, 0); | |
f21e18ca | 2228 | if ((unsigned long long)read(afd, abuf, len) != len) |
7236ee7a N |
2229 | fail("read first from array failed"); |
2230 | if (memcmp(bbuf, abuf, len) != 0) { | |
080fd005 | 2231 | #if 0 |
7236ee7a N |
2232 | int i; |
2233 | printf("offset=%llu len=%llu\n", | |
080fd005 | 2234 | (unsigned long long)__le64_to_cpu(bsb2.arraystart)*512, len); |
7236ee7a N |
2235 | for (i=0; i<len; i++) |
2236 | if (bbuf[i] != abuf[i]) { | |
2237 | printf("first diff byte %d\n", i); | |
93ecfa01 | 2238 | break; |
7236ee7a | 2239 | } |
080fd005 | 2240 | #endif |
7236ee7a | 2241 | fail("data1 compare failed"); |
e86c9dd6 | 2242 | } |
7236ee7a N |
2243 | } |
2244 | if (bsb2.length2) { | |
2245 | unsigned long long len = __le64_to_cpu(bsb2.length2)*512; | |
2246 | ||
2247 | if (abuflen < len) { | |
2248 | free(abuf); | |
2249 | free(bbuf); | |
2250 | abuflen = len; | |
2251 | abuf = malloc(abuflen); | |
2252 | bbuf = malloc(abuflen); | |
e86c9dd6 NB |
2253 | } |
2254 | ||
7236ee7a | 2255 | lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0); |
f21e18ca | 2256 | if ((unsigned long long)read(bfd, bbuf, len) != len) |
7236ee7a N |
2257 | fail("read second backup failed"); |
2258 | lseek64(afd, __le64_to_cpu(bsb2.arraystart2)*512, 0); | |
f21e18ca | 2259 | if ((unsigned long long)read(afd, abuf, len) != len) |
7236ee7a N |
2260 | fail("read second from array failed"); |
2261 | if (memcmp(bbuf, abuf, len) != 0) | |
2262 | fail("data2 compare failed"); | |
e86c9dd6 | 2263 | } |
7236ee7a | 2264 | } |
e86c9dd6 | 2265 | |
7236ee7a N |
2266 | static int child_grow(int afd, struct mdinfo *sra, unsigned long stripes, |
2267 | int *fds, unsigned long long *offsets, | |
2268 | int disks, int chunk, int level, int layout, int data, | |
2269 | int dests, int *destfd, unsigned long long *destoffsets) | |
2270 | { | |
2271 | char *buf; | |
d4445387 | 2272 | int degraded = 0; |
e86c9dd6 | 2273 | |
fcf57625 N |
2274 | if (posix_memalign((void**)&buf, 4096, disks * chunk)) |
2275 | /* Don't start the 'reshape' */ | |
2276 | return 0; | |
7236ee7a N |
2277 | sysfs_set_num(sra, NULL, "suspend_hi", 0); |
2278 | sysfs_set_num(sra, NULL, "suspend_lo", 0); | |
2279 | grow_backup(sra, 0, stripes, | |
2280 | fds, offsets, disks, chunk, level, layout, | |
2281 | dests, destfd, destoffsets, | |
d4445387 | 2282 | 0, °raded, buf); |
7236ee7a | 2283 | validate(afd, destfd[0], destoffsets[0]); |
200871ad | 2284 | wait_backup(sra, 0, stripes * (chunk / 512), stripes * (chunk / 512), |
725cac4c N |
2285 | dests, destfd, destoffsets, |
2286 | 0); | |
200871ad | 2287 | sysfs_set_num(sra, NULL, "suspend_lo", (stripes * (chunk/512)) * data); |
7236ee7a N |
2288 | free(buf); |
2289 | /* FIXME this should probably be numeric */ | |
2290 | sysfs_set_str(sra, NULL, "sync_max", "max"); | |
e86c9dd6 | 2291 | return 1; |
7236ee7a | 2292 | } |
e86c9dd6 | 2293 | |
7236ee7a N |
2294 | static int child_shrink(int afd, struct mdinfo *sra, unsigned long stripes, |
2295 | int *fds, unsigned long long *offsets, | |
2296 | int disks, int chunk, int level, int layout, int data, | |
2297 | int dests, int *destfd, unsigned long long *destoffsets) | |
2298 | { | |
2299 | char *buf; | |
2300 | unsigned long long start; | |
2301 | int rv; | |
d4445387 | 2302 | int degraded = 0; |
7236ee7a | 2303 | |
fcf57625 N |
2304 | if (posix_memalign((void**)&buf, 4096, disks * chunk)) |
2305 | return 0; | |
200871ad | 2306 | start = sra->component_size - stripes * (chunk/512); |
7236ee7a N |
2307 | sysfs_set_num(sra, NULL, "sync_max", start); |
2308 | sysfs_set_str(sra, NULL, "sync_action", "reshape"); | |
2309 | sysfs_set_num(sra, NULL, "suspend_lo", 0); | |
2310 | sysfs_set_num(sra, NULL, "suspend_hi", 0); | |
200871ad | 2311 | rv = wait_backup(sra, 0, start - stripes * (chunk/512), stripes * (chunk/512), |
7236ee7a N |
2312 | dests, destfd, destoffsets, 0); |
2313 | if (rv < 0) | |
2314 | return 0; | |
2315 | grow_backup(sra, 0, stripes, | |
2316 | fds, offsets, | |
2317 | disks, chunk, level, layout, | |
2318 | dests, destfd, destoffsets, | |
d4445387 | 2319 | 0, °raded, buf); |
7236ee7a | 2320 | validate(afd, destfd[0], destoffsets[0]); |
200871ad | 2321 | wait_backup(sra, start, stripes*(chunk/512), 0, |
725cac4c | 2322 | dests, destfd, destoffsets, 0); |
200871ad | 2323 | sysfs_set_num(sra, NULL, "suspend_lo", (stripes * (chunk/512)) * data); |
7236ee7a N |
2324 | free(buf); |
2325 | /* FIXME this should probably be numeric */ | |
2326 | sysfs_set_str(sra, NULL, "sync_max", "max"); | |
2327 | return 1; | |
2328 | } | |
2329 | ||
2330 | static int child_same_size(int afd, struct mdinfo *sra, unsigned long stripes, | |
2331 | int *fds, unsigned long long *offsets, | |
e9e43ec3 | 2332 | unsigned long long start, |
7236ee7a N |
2333 | int disks, int chunk, int level, int layout, int data, |
2334 | int dests, int *destfd, unsigned long long *destoffsets) | |
2335 | { | |
e9e43ec3 | 2336 | unsigned long long size; |
7236ee7a N |
2337 | unsigned long tailstripes = stripes; |
2338 | int part; | |
2339 | char *buf; | |
2340 | unsigned long long speed; | |
d4445387 | 2341 | int degraded = 0; |
7236ee7a N |
2342 | |
2343 | ||
fcf57625 N |
2344 | if (posix_memalign((void**)&buf, 4096, disks * chunk)) |
2345 | return 0; | |
7236ee7a N |
2346 | |
2347 | sysfs_set_num(sra, NULL, "suspend_lo", 0); | |
2348 | sysfs_set_num(sra, NULL, "suspend_hi", 0); | |
2349 | ||
2350 | sysfs_get_ll(sra, NULL, "sync_speed_min", &speed); | |
2351 | sysfs_set_num(sra, NULL, "sync_speed_min", 200000); | |
2352 | ||
e9e43ec3 | 2353 | grow_backup(sra, start, stripes, |
7236ee7a N |
2354 | fds, offsets, |
2355 | disks, chunk, level, layout, | |
2356 | dests, destfd, destoffsets, | |
d4445387 | 2357 | 0, °raded, buf); |
200871ad | 2358 | grow_backup(sra, (start + stripes) * (chunk/512), stripes, |
7236ee7a N |
2359 | fds, offsets, |
2360 | disks, chunk, level, layout, | |
2361 | dests, destfd, destoffsets, | |
d4445387 | 2362 | 1, °raded, buf); |
7236ee7a N |
2363 | validate(afd, destfd[0], destoffsets[0]); |
2364 | part = 0; | |
e9e43ec3 | 2365 | start += stripes * 2; /* where to read next */ |
7236ee7a N |
2366 | size = sra->component_size / (chunk/512); |
2367 | while (start < size) { | |
200871ad N |
2368 | if (wait_backup(sra, (start-stripes*2)*(chunk/512), |
2369 | stripes*(chunk/512), 0, | |
7236ee7a N |
2370 | dests, destfd, destoffsets, |
2371 | part) < 0) | |
2372 | return 0; | |
200871ad | 2373 | sysfs_set_num(sra, NULL, "suspend_lo", start*(chunk/512) * data); |
7236ee7a N |
2374 | if (start + stripes > size) |
2375 | tailstripes = (size - start); | |
2376 | ||
200871ad | 2377 | grow_backup(sra, start*(chunk/512), tailstripes, |
7236ee7a N |
2378 | fds, offsets, |
2379 | disks, chunk, level, layout, | |
2380 | dests, destfd, destoffsets, | |
d4445387 | 2381 | part, °raded, buf); |
7236ee7a N |
2382 | start += stripes; |
2383 | part = 1 - part; | |
2384 | validate(afd, destfd[0], destoffsets[0]); | |
2385 | } | |
200871ad | 2386 | if (wait_backup(sra, (start-stripes*2) * (chunk/512), stripes * (chunk/512), 0, |
7236ee7a N |
2387 | dests, destfd, destoffsets, |
2388 | part) < 0) | |
2389 | return 0; | |
200871ad N |
2390 | sysfs_set_num(sra, NULL, "suspend_lo", ((start-stripes)*(chunk/512)) * data); |
2391 | wait_backup(sra, (start-stripes) * (chunk/512), tailstripes * (chunk/512), 0, | |
725cac4c N |
2392 | dests, destfd, destoffsets, |
2393 | 1-part); | |
200871ad | 2394 | sysfs_set_num(sra, NULL, "suspend_lo", (size*(chunk/512)) * data); |
7236ee7a N |
2395 | sysfs_set_num(sra, NULL, "sync_speed_min", speed); |
2396 | free(buf); | |
2397 | return 1; | |
e86c9dd6 | 2398 | } |
353632d9 NB |
2399 | |
2400 | /* | |
2401 | * If any spare contains md_back_data-1 which is recent wrt mtime, | |
2402 | * write that data into the array and update the super blocks with | |
2403 | * the new reshape_progress | |
2404 | */ | |
ea0ebe96 N |
2405 | int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt, |
2406 | char *backup_file, int verbose) | |
353632d9 NB |
2407 | { |
2408 | int i, j; | |
2409 | int old_disks; | |
353632d9 | 2410 | unsigned long long *offsets; |
82f2d6ab | 2411 | unsigned long long nstripe, ostripe; |
6e9eac4f | 2412 | int ndata, odata; |
353632d9 | 2413 | |
e9e43ec3 N |
2414 | if (info->new_level != info->array.level) |
2415 | return 1; /* Cannot handle level changes (they are instantaneous) */ | |
2416 | ||
2417 | odata = info->array.raid_disks - info->delta_disks - 1; | |
2418 | if (info->array.level == 6) odata--; /* number of data disks */ | |
2419 | ndata = info->array.raid_disks - 1; | |
2420 | if (info->new_level == 6) ndata--; | |
353632d9 NB |
2421 | |
2422 | old_disks = info->array.raid_disks - info->delta_disks; | |
2423 | ||
e9e43ec3 N |
2424 | if (info->delta_disks <= 0) |
2425 | /* Didn't grow, so the backup file must have | |
2426 | * been used | |
2427 | */ | |
2428 | old_disks = cnt; | |
06b0d786 | 2429 | for (i=old_disks-(backup_file?1:0); i<cnt; i++) { |
353632d9 | 2430 | struct mdinfo dinfo; |
06b0d786 | 2431 | int fd; |
e9e43ec3 | 2432 | int bsbsize; |
ea0ebe96 | 2433 | char *devname, namebuf[20]; |
353632d9 NB |
2434 | |
2435 | /* This was a spare and may have some saved data on it. | |
2436 | * Load the superblock, find and load the | |
2437 | * backup_super_block. | |
2438 | * If either fail, go on to next device. | |
2439 | * If the backup contains no new info, just return | |
206c5eae | 2440 | * else restore data and update all superblocks |
353632d9 | 2441 | */ |
06b0d786 NB |
2442 | if (i == old_disks-1) { |
2443 | fd = open(backup_file, O_RDONLY); | |
e9e43ec3 N |
2444 | if (fd<0) { |
2445 | fprintf(stderr, Name ": backup file %s inaccessible: %s\n", | |
2446 | backup_file, strerror(errno)); | |
06b0d786 | 2447 | continue; |
e9e43ec3 | 2448 | } |
ea0ebe96 | 2449 | devname = backup_file; |
06b0d786 NB |
2450 | } else { |
2451 | fd = fdlist[i]; | |
2452 | if (fd < 0) | |
2453 | continue; | |
3da92f27 | 2454 | if (st->ss->load_super(st, fd, NULL)) |
06b0d786 | 2455 | continue; |
353632d9 | 2456 | |
a5d85af7 | 2457 | st->ss->getinfo_super(st, &dinfo, NULL); |
3da92f27 NB |
2458 | st->ss->free_super(st); |
2459 | ||
06b0d786 NB |
2460 | if (lseek64(fd, |
2461 | (dinfo.data_offset + dinfo.component_size - 8) <<9, | |
ea0ebe96 N |
2462 | 0) < 0) { |
2463 | fprintf(stderr, Name ": Cannot seek on device %d\n", i); | |
06b0d786 | 2464 | continue; /* Cannot seek */ |
ea0ebe96 N |
2465 | } |
2466 | sprintf(namebuf, "device-%d", i); | |
2467 | devname = namebuf; | |
06b0d786 | 2468 | } |
ea0ebe96 N |
2469 | if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb)) { |
2470 | if (verbose) | |
2471 | fprintf(stderr, Name ": Cannot read from %s\n", devname); | |
353632d9 | 2472 | continue; /* Cannot read */ |
ea0ebe96 | 2473 | } |
e9e43ec3 | 2474 | if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0 && |
ea0ebe96 N |
2475 | memcmp(bsb.magic, "md_backup_data-2", 16) != 0) { |
2476 | if (verbose) | |
2477 | fprintf(stderr, Name ": No backup metadata on %s\n", devname); | |
353632d9 | 2478 | continue; |
ea0ebe96 N |
2479 | } |
2480 | if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb))) { | |
2481 | if (verbose) | |
2482 | fprintf(stderr, Name ": Bad backup-metadata checksum on %s\n", devname); | |
353632d9 | 2483 | continue; /* bad checksum */ |
ea0ebe96 | 2484 | } |
e9e43ec3 | 2485 | if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0 && |
ea0ebe96 N |
2486 | bsb.sb_csum2 != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum2)-((char*)&bsb))) { |
2487 | if (verbose) | |
2488 | fprintf(stderr, Name ": Bad backup-metadata checksum2 on %s\n", devname); | |
22e30516 | 2489 | continue; /* Bad second checksum */ |
ea0ebe96 N |
2490 | } |
2491 | if (memcmp(bsb.set_uuid,info->uuid, 16) != 0) { | |
2492 | if (verbose) | |
2493 | fprintf(stderr, Name ": Wrong uuid on backup-metadata on %s\n", devname); | |
353632d9 | 2494 | continue; /* Wrong uuid */ |
ea0ebe96 | 2495 | } |
353632d9 | 2496 | |
097075b6 N |
2497 | /* array utime and backup-mtime should be updated at much the same time, but it seems that |
2498 | * sometimes they aren't... So allow considerable flexability in matching, and allow | |
2499 | * this test to be overridden by an environment variable. | |
2500 | */ | |
f21e18ca N |
2501 | if (info->array.utime > (int)__le64_to_cpu(bsb.mtime) + 2*60*60 || |
2502 | info->array.utime < (int)__le64_to_cpu(bsb.mtime) - 10*60) { | |
097075b6 N |
2503 | if (check_env("MDADM_GROW_ALLOW_OLD")) { |
2504 | fprintf(stderr, Name ": accepting backup with timestamp %lu " | |
2505 | "for array with timestamp %lu\n", | |
2506 | (unsigned long)__le64_to_cpu(bsb.mtime), | |
2507 | (unsigned long)info->array.utime); | |
2508 | } else { | |
2509 | if (verbose) | |
2510 | fprintf(stderr, Name ": too-old timestamp on " | |
2511 | "backup-metadata on %s\n", devname); | |
2512 | continue; /* time stamp is too bad */ | |
2513 | } | |
ea0ebe96 | 2514 | } |
353632d9 | 2515 | |
e9e43ec3 N |
2516 | if (bsb.magic[15] == '1') { |
2517 | if (info->delta_disks >= 0) { | |
2518 | /* reshape_progress is increasing */ | |
2519 | if (__le64_to_cpu(bsb.arraystart) + __le64_to_cpu(bsb.length) < | |
ea0ebe96 N |
2520 | info->reshape_progress) { |
2521 | nonew: | |
2522 | if (verbose) | |
2523 | fprintf(stderr, Name ": backup-metadata found on %s but is not needed\n", devname); | |
e9e43ec3 | 2524 | continue; /* No new data here */ |
ea0ebe96 | 2525 | } |
e9e43ec3 N |
2526 | } else { |
2527 | /* reshape_progress is decreasing */ | |
2528 | if (__le64_to_cpu(bsb.arraystart) >= | |
2529 | info->reshape_progress) | |
ea0ebe96 | 2530 | goto nonew; /* No new data here */ |
e9e43ec3 N |
2531 | } |
2532 | } else { | |
2533 | if (info->delta_disks >= 0) { | |
2534 | /* reshape_progress is increasing */ | |
2535 | if (__le64_to_cpu(bsb.arraystart) + __le64_to_cpu(bsb.length) < | |
2536 | info->reshape_progress && | |
2537 | __le64_to_cpu(bsb.arraystart2) + __le64_to_cpu(bsb.length2) < | |
2538 | info->reshape_progress) | |
ea0ebe96 | 2539 | goto nonew; /* No new data here */ |
e9e43ec3 N |
2540 | } else { |
2541 | /* reshape_progress is decreasing */ | |
2542 | if (__le64_to_cpu(bsb.arraystart) >= | |
2543 | info->reshape_progress && | |
2544 | __le64_to_cpu(bsb.arraystart2) >= | |
2545 | info->reshape_progress) | |
ea0ebe96 | 2546 | goto nonew; /* No new data here */ |
e9e43ec3 N |
2547 | } |
2548 | } | |
ea0ebe96 N |
2549 | if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0) { |
2550 | second_fail: | |
2551 | if (verbose) | |
2552 | fprintf(stderr, Name ": Failed to verify secondary backup-metadata block on %s\n", | |
2553 | devname); | |
353632d9 | 2554 | continue; /* Cannot seek */ |
ea0ebe96 | 2555 | } |
2efedc7b | 2556 | /* There should be a duplicate backup superblock 4k before here */ |
06b0d786 | 2557 | if (lseek64(fd, -4096, 1) < 0 || |
0155af90 | 2558 | read(fd, &bsb2, sizeof(bsb2)) != sizeof(bsb2)) |
ea0ebe96 | 2559 | goto second_fail; /* Cannot find leading superblock */ |
e9e43ec3 N |
2560 | if (bsb.magic[15] == '1') |
2561 | bsbsize = offsetof(struct mdp_backup_super, pad1); | |
2562 | else | |
2563 | bsbsize = offsetof(struct mdp_backup_super, pad); | |
ff94fb86 | 2564 | if (memcmp(&bsb2, &bsb, bsbsize) != 0) |
ea0ebe96 | 2565 | goto second_fail; /* Cannot find leading superblock */ |
2efedc7b | 2566 | |
353632d9 NB |
2567 | /* Now need the data offsets for all devices. */ |
2568 | offsets = malloc(sizeof(*offsets)*info->array.raid_disks); | |
2569 | for(j=0; j<info->array.raid_disks; j++) { | |
2570 | if (fdlist[j] < 0) | |
2571 | continue; | |
3da92f27 | 2572 | if (st->ss->load_super(st, fdlist[j], NULL)) |
353632d9 NB |
2573 | /* FIXME should be this be an error */ |
2574 | continue; | |
a5d85af7 | 2575 | st->ss->getinfo_super(st, &dinfo, NULL); |
3da92f27 | 2576 | st->ss->free_super(st); |
14e5b4d7 | 2577 | offsets[j] = dinfo.data_offset * 512; |
353632d9 NB |
2578 | } |
2579 | printf(Name ": restoring critical section\n"); | |
2580 | ||
2581 | if (restore_stripes(fdlist, offsets, | |
2582 | info->array.raid_disks, | |
2583 | info->new_chunk, | |
2584 | info->new_level, | |
2585 | info->new_layout, | |
06b0d786 | 2586 | fd, __le64_to_cpu(bsb.devstart)*512, |
92dcdf7c | 2587 | __le64_to_cpu(bsb.arraystart)*512, |
e9e43ec3 N |
2588 | __le64_to_cpu(bsb.length)*512)) { |
2589 | /* didn't succeed, so giveup */ | |
ea0ebe96 N |
2590 | if (verbose) |
2591 | fprintf(stderr, Name ": Error restoring backup from %s\n", | |
2592 | devname); | |
e9e43ec3 N |
2593 | return 1; |
2594 | } | |
2595 | ||
2596 | if (bsb.magic[15] == '2' && | |
2597 | restore_stripes(fdlist, offsets, | |
2598 | info->array.raid_disks, | |
2599 | info->new_chunk, | |
2600 | info->new_level, | |
2601 | info->new_layout, | |
2602 | fd, __le64_to_cpu(bsb.devstart)*512 + | |
2603 | __le64_to_cpu(bsb.devstart2)*512, | |
92dcdf7c | 2604 | __le64_to_cpu(bsb.arraystart2)*512, |
e9e43ec3 | 2605 | __le64_to_cpu(bsb.length2)*512)) { |
353632d9 | 2606 | /* didn't succeed, so giveup */ |
ea0ebe96 N |
2607 | if (verbose) |
2608 | fprintf(stderr, Name ": Error restoring second backup from %s\n", | |
2609 | devname); | |
2295250a | 2610 | return 1; |
353632d9 NB |
2611 | } |
2612 | ||
e9e43ec3 | 2613 | |
353632d9 NB |
2614 | /* Ok, so the data is restored. Let's update those superblocks. */ |
2615 | ||
e9e43ec3 N |
2616 | if (info->delta_disks >= 0) { |
2617 | info->reshape_progress = __le64_to_cpu(bsb.arraystart) + | |
2618 | __le64_to_cpu(bsb.length); | |
2619 | if (bsb.magic[15] == '2') { | |
2620 | unsigned long long p2 = __le64_to_cpu(bsb.arraystart2) + | |
2621 | __le64_to_cpu(bsb.length2); | |
2622 | if (p2 > info->reshape_progress) | |
2623 | info->reshape_progress = p2; | |
2624 | } | |
2625 | } else { | |
2626 | info->reshape_progress = __le64_to_cpu(bsb.arraystart); | |
2627 | if (bsb.magic[15] == '2') { | |
2628 | unsigned long long p2 = __le64_to_cpu(bsb.arraystart2); | |
2629 | if (p2 < info->reshape_progress) | |
2630 | info->reshape_progress = p2; | |
2631 | } | |
2632 | } | |
353632d9 NB |
2633 | for (j=0; j<info->array.raid_disks; j++) { |
2634 | if (fdlist[j] < 0) continue; | |
3da92f27 | 2635 | if (st->ss->load_super(st, fdlist[j], NULL)) |
353632d9 | 2636 | continue; |
a5d85af7 | 2637 | st->ss->getinfo_super(st, &dinfo, NULL); |
e9e43ec3 | 2638 | dinfo.reshape_progress = info->reshape_progress; |
3da92f27 | 2639 | st->ss->update_super(st, &dinfo, |
68c7d6d7 NB |
2640 | "_reshape_progress", |
2641 | NULL,0, 0, NULL); | |
3da92f27 NB |
2642 | st->ss->store_super(st, fdlist[j]); |
2643 | st->ss->free_super(st); | |
353632d9 | 2644 | } |
353632d9 NB |
2645 | return 0; |
2646 | } | |
6e9eac4f NB |
2647 | /* Didn't find any backup data, try to see if any |
2648 | * was needed. | |
2649 | */ | |
82f2d6ab N |
2650 | if (info->delta_disks < 0) { |
2651 | /* When shrinking, the critical section is at the end. | |
2652 | * So see if we are before the critical section. | |
2653 | */ | |
2654 | unsigned long long first_block; | |
2655 | nstripe = ostripe = 0; | |
2656 | first_block = 0; | |
2657 | while (ostripe >= nstripe) { | |
2658 | ostripe += info->array.chunk_size / 512; | |
2659 | first_block = ostripe * odata; | |
2660 | nstripe = first_block / ndata / (info->new_chunk/512) * | |
2661 | (info->new_chunk/512); | |
2662 | } | |
2663 | ||
2664 | if (info->reshape_progress >= first_block) | |
2665 | return 0; | |
6e9eac4f | 2666 | } |
82f2d6ab N |
2667 | if (info->delta_disks > 0) { |
2668 | /* See if we are beyond the critical section. */ | |
2669 | unsigned long long last_block; | |
2670 | nstripe = ostripe = 0; | |
2671 | last_block = 0; | |
2672 | while (nstripe >= ostripe) { | |
2673 | nstripe += info->new_chunk / 512; | |
2674 | last_block = nstripe * ndata; | |
2675 | ostripe = last_block / odata / (info->array.chunk_size/512) * | |
2676 | (info->array.chunk_size/512); | |
2677 | } | |
6e9eac4f | 2678 | |
82f2d6ab N |
2679 | if (info->reshape_progress >= last_block) |
2680 | return 0; | |
2681 | } | |
6e9eac4f | 2682 | /* needed to recover critical section! */ |
ea0ebe96 N |
2683 | if (verbose) |
2684 | fprintf(stderr, Name ": Failed to find backup of critical section\n"); | |
2295250a | 2685 | return 1; |
353632d9 | 2686 | } |
e9e43ec3 N |
2687 | |
2688 | int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info, | |
2689 | char *backup_file) | |
2690 | { | |
2691 | /* Array is assembled and ready to be started, but | |
2692 | * monitoring is probably required. | |
2693 | * So: | |
2694 | * - start read-only | |
2695 | * - set upper bound for resync | |
2696 | * - initialise the 'suspend' boundaries | |
2697 | * - switch to read-write | |
2698 | * - fork and continue monitoring | |
2699 | */ | |
2700 | int err; | |
2701 | int backup_list[1]; | |
2702 | unsigned long long backup_offsets[1]; | |
2703 | int odisks, ndisks, ochunk, nchunk,odata,ndata; | |
2704 | unsigned long a,b,blocks,stripes; | |
2705 | int backup_fd; | |
2706 | int *fds; | |
2707 | unsigned long long *offsets; | |
2708 | int d; | |
2709 | struct mdinfo *sra, *sd; | |
2710 | int rv; | |
f21e18ca | 2711 | unsigned long cache; |
e9e43ec3 N |
2712 | int done = 0; |
2713 | ||
2714 | err = sysfs_set_str(info, NULL, "array_state", "readonly"); | |
2715 | if (err) | |
2716 | return err; | |
2717 | ||
2718 | /* make sure reshape doesn't progress until we are ready */ | |
2719 | sysfs_set_str(info, NULL, "sync_max", "0"); | |
2720 | sysfs_set_str(info, NULL, "array_state", "active"); /* FIXME or clean */ | |
5f6ca90a N |
2721 | |
2722 | sra = sysfs_read(-1, devname2devnum(info->sys_name), | |
2723 | GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE| | |
2724 | GET_CACHE); | |
2725 | if (!sra) | |
2726 | return 1; | |
2727 | ||
e9e43ec3 N |
2728 | /* ndisks is not growing, so raid_disks is old and +delta is new */ |
2729 | odisks = info->array.raid_disks; | |
2730 | ndisks = odisks + info->delta_disks; | |
2731 | odata = odisks - 1; | |
2732 | ndata = ndisks - 1; | |
2733 | if (info->array.level == 6) { | |
2734 | odata--; | |
2735 | ndata--; | |
2736 | } | |
2737 | ochunk = info->array.chunk_size; | |
2738 | nchunk = info->new_chunk; | |
2739 | ||
200871ad N |
2740 | a = (ochunk/512) * odata; |
2741 | b = (nchunk/512) * ndata; | |
e9e43ec3 N |
2742 | /* Find GCD */ |
2743 | while (a != b) { | |
2744 | if (a < b) | |
2745 | b -= a; | |
2746 | if (b < a) | |
2747 | a -= b; | |
2748 | } | |
2749 | /* LCM == product / GCD */ | |
200871ad | 2750 | blocks = (ochunk/512) * (nchunk/512) * odata * ndata / a; |
e9e43ec3 N |
2751 | |
2752 | if (ndata == odata) | |
1b13faf7 N |
2753 | while (blocks * 32 < sra->component_size && |
2754 | blocks < 16*1024*2) | |
2755 | blocks *= 2; | |
e9e43ec3 N |
2756 | stripes = blocks / (info->array.chunk_size/512) / odata; |
2757 | ||
1b13faf7 N |
2758 | /* check that the internal stripe cache is |
2759 | * large enough, or it won't work. | |
2760 | */ | |
2761 | cache = (nchunk < ochunk) ? ochunk : nchunk; | |
2762 | cache = cache * 4 / 4096; | |
2763 | if (cache < blocks / 8 / odisks + 16) | |
2764 | /* Make it big enough to hold 'blocks' */ | |
2765 | cache = blocks / 8 / odisks + 16; | |
2766 | if (sra->cache_size < cache) | |
2767 | sysfs_set_num(sra, NULL, "stripe_cache_size", | |
2768 | cache+1); | |
e9e43ec3 N |
2769 | |
2770 | memset(&bsb, 0, 512); | |
2771 | memcpy(bsb.magic, "md_backup_data-1", 16); | |
2772 | memcpy(&bsb.set_uuid, info->uuid, 16); | |
2773 | bsb.mtime = __cpu_to_le64(time(0)); | |
2774 | bsb.devstart2 = blocks; | |
2775 | ||
2776 | backup_fd = open(backup_file, O_RDWR|O_CREAT, S_IRUSR | S_IWUSR); | |
87f26d14 N |
2777 | if (backup_fd < 0) { |
2778 | fprintf(stderr, Name ": Cannot open backup file %s\n", | |
2779 | backup_file ?: "- no backup-file given"); | |
2780 | return 1; | |
2781 | } | |
e9e43ec3 N |
2782 | backup_list[0] = backup_fd; |
2783 | backup_offsets[0] = 8 * 512; | |
2784 | fds = malloc(odisks * sizeof(fds[0])); | |
2785 | offsets = malloc(odisks * sizeof(offsets[0])); | |
2786 | for (d=0; d<odisks; d++) | |
2787 | fds[d] = -1; | |
2788 | ||
e9e43ec3 N |
2789 | for (sd = sra->devs; sd; sd = sd->next) { |
2790 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) | |
2791 | continue; | |
2792 | if (sd->disk.state & (1<<MD_DISK_SYNC)) { | |
2793 | char *dn = map_dev(sd->disk.major, | |
2794 | sd->disk.minor, 1); | |
2795 | fds[sd->disk.raid_disk] | |
2796 | = dev_open(dn, O_RDONLY); | |
2797 | offsets[sd->disk.raid_disk] = sd->data_offset*512; | |
2798 | if (fds[sd->disk.raid_disk] < 0) { | |
2799 | fprintf(stderr, Name ": %s: cannot open component %s\n", | |
2800 | info->sys_name, dn?dn:"-unknown-"); | |
2801 | rv = 1; | |
2802 | goto release; | |
2803 | } | |
2804 | free(dn); | |
2805 | } | |
2806 | } | |
2807 | ||
2808 | switch(fork()) { | |
2809 | case 0: | |
2810 | close(mdfd); | |
2811 | mlockall(MCL_FUTURE); | |
2812 | if (info->delta_disks < 0) | |
2813 | done = child_shrink(-1, info, stripes, | |
2814 | fds, offsets, | |
2815 | info->array.raid_disks, | |
2816 | info->array.chunk_size, | |
2817 | info->array.level, info->array.layout, | |
2818 | odata, | |
2819 | 1, backup_list, backup_offsets); | |
2820 | else if (info->delta_disks == 0) { | |
2821 | /* The 'start' is a per-device stripe number. | |
2822 | * reshape_progress is a per-array sector number. | |
2823 | * So divide by ndata * chunk_size | |
2824 | */ | |
2825 | unsigned long long start = info->reshape_progress / ndata; | |
2826 | start /= (info->array.chunk_size/512); | |
2827 | done = child_same_size(-1, info, stripes, | |
2828 | fds, offsets, | |
2829 | start, | |
2830 | info->array.raid_disks, | |
2831 | info->array.chunk_size, | |
2832 | info->array.level, info->array.layout, | |
2833 | odata, | |
2834 | 1, backup_list, backup_offsets); | |
2835 | } | |
2836 | if (backup_file && done) | |
2837 | unlink(backup_file); | |
2838 | /* FIXME should I intuit a level change */ | |
2839 | exit(0); | |
2840 | case -1: | |
2841 | fprintf(stderr, Name ": Cannot run child to continue monitoring reshape: %s\n", | |
2842 | strerror(errno)); | |
2843 | return 1; | |
2844 | default: | |
2845 | break; | |
2846 | } | |
2847 | release: | |
2848 | return 0; | |
2849 | } | |
2850 | ||
2851 |