]>
Commit | Line | Data |
---|---|---|
64c4757e | 1 | /* |
9a9dab36 | 2 | * mdadm - manage Linux "md" devices aka RAID arrays. |
64c4757e | 3 | * |
6f02172d | 4 | * Copyright (C) 2001-2013 Neil Brown <neilb@suse.de> |
64c4757e 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> |
64c4757e NB |
23 | */ |
24 | ||
9a9dab36 | 25 | #include "mdadm.h" |
64c4757e | 26 | #include "md_p.h" |
edd8d13c | 27 | #include <sys/socket.h> |
64c4757e | 28 | #include <sys/utsname.h> |
9fe32043 | 29 | #include <sys/wait.h> |
edd8d13c | 30 | #include <sys/un.h> |
a7dec3fd | 31 | #include <sys/resource.h> |
a9c15847 N |
32 | #include <sys/vfs.h> |
33 | #include <linux/magic.h> | |
98c6faba | 34 | #include <ctype.h> |
a322f70c | 35 | #include <dirent.h> |
a931db9e | 36 | #include <signal.h> |
0a816ef9 NB |
37 | |
38 | /* | |
39 | * following taken from linux/blkpg.h because they aren't | |
40 | * anywhere else and it isn't safe to #include linux/ * stuff. | |
41 | */ | |
42 | ||
43 | #define BLKPG _IO(0x12,105) | |
44 | ||
45 | /* The argument structure */ | |
46 | struct blkpg_ioctl_arg { | |
5d500228 N |
47 | int op; |
48 | int flags; | |
49 | int datalen; | |
50 | void *data; | |
0a816ef9 NB |
51 | }; |
52 | ||
53 | /* The subfunctions (for the op field) */ | |
54 | #define BLKPG_ADD_PARTITION 1 | |
55 | #define BLKPG_DEL_PARTITION 2 | |
56 | ||
57 | /* Sizes of name fields. Unused at present. */ | |
58 | #define BLKPG_DEVNAMELTH 64 | |
59 | #define BLKPG_VOLNAMELTH 64 | |
60 | ||
61 | /* The data structure for ADD_PARTITION and DEL_PARTITION */ | |
62 | struct blkpg_partition { | |
63 | long long start; /* starting offset in bytes */ | |
64 | long long length; /* length in bytes */ | |
65 | int pno; /* partition number */ | |
66 | char devname[BLKPG_DEVNAMELTH]; /* partition name, like sda5 or c0d1p2, | |
67 | to be used in kernel messages */ | |
68 | char volname[BLKPG_VOLNAMELTH]; /* volume label */ | |
69 | }; | |
64c4757e | 70 | |
0f22b998 | 71 | #include "part.h" |
056b331e N |
72 | |
73 | /* Force a compilation error if condition is true */ | |
74 | #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition)) | |
75 | ||
76 | /* Force a compilation error if condition is true, but also produce a | |
77 | result (of value 0 and type size_t), so the expression can be used | |
78 | e.g. in a structure initializer (or where-ever else comma expressions | |
79 | aren't permitted). */ | |
80 | #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); })) | |
81 | ||
64c4757e NB |
82 | /* |
83 | * Parse a 128 bit uuid in 4 integers | |
84 | * format is 32 hexx nibbles with options :.<space> separator | |
85 | * If not exactly 32 hex digits are found, return 0 | |
86 | * else return 1 | |
87 | */ | |
88 | int parse_uuid(char *str, int uuid[4]) | |
89 | { | |
aba69144 NB |
90 | int hit = 0; /* number of Hex digIT */ |
91 | int i; | |
92 | char c; | |
ca3b6696 N |
93 | for (i = 0; i < 4; i++) |
94 | uuid[i] = 0; | |
aba69144 | 95 | |
ca3b6696 | 96 | while ((c = *str++) != 0) { |
aba69144 | 97 | int n; |
ca3b6696 | 98 | if (c >= '0' && c <= '9') |
aba69144 | 99 | n = c-'0'; |
ca3b6696 | 100 | else if (c >= 'a' && c <= 'f') |
aba69144 | 101 | n = 10 + c - 'a'; |
ca3b6696 | 102 | else if (c >= 'A' && c <= 'F') |
aba69144 NB |
103 | n = 10 + c - 'A'; |
104 | else if (strchr(":. -", c)) | |
105 | continue; | |
106 | else return 0; | |
107 | ||
108 | if (hit<32) { | |
109 | uuid[hit/8] <<= 4; | |
110 | uuid[hit/8] += n; | |
111 | } | |
112 | hit++; | |
82b27616 | 113 | } |
aba69144 NB |
114 | if (hit == 32) |
115 | return 1; | |
116 | return 0; | |
64c4757e NB |
117 | } |
118 | ||
64c4757e NB |
119 | /* |
120 | * Get the md version number. | |
121 | * We use the RAID_VERSION ioctl if it is supported | |
122 | * If not, but we have a block device with major '9', we assume | |
123 | * 0.36.0 | |
124 | * | |
125 | * Return version number as 24 but number - assume version parts | |
126 | * always < 255 | |
127 | */ | |
128 | ||
129 | int md_get_version(int fd) | |
130 | { | |
5d500228 N |
131 | struct stat stb; |
132 | mdu_version_t vers; | |
133 | ||
134 | if (fstat(fd, &stb)<0) | |
135 | return -1; | |
136 | if ((S_IFMT&stb.st_mode) != S_IFBLK) | |
137 | return -1; | |
138 | ||
139 | if (ioctl(fd, RAID_VERSION, &vers) == 0) | |
140 | return (vers.major*10000) + (vers.minor*100) + vers.patchlevel; | |
141 | if (errno == EACCES) | |
142 | return -1; | |
143 | if (major(stb.st_rdev) == MD_MAJOR) | |
144 | return (3600); | |
64c4757e | 145 | return -1; |
64c4757e NB |
146 | } |
147 | ||
64c4757e NB |
148 | int get_linux_version() |
149 | { | |
150 | struct utsname name; | |
98c6faba | 151 | char *cp; |
f161d047 | 152 | int a = 0, b = 0,c = 0; |
64c4757e NB |
153 | if (uname(&name) <0) |
154 | return -1; | |
155 | ||
98c6faba NB |
156 | cp = name.release; |
157 | a = strtoul(cp, &cp, 10); | |
f161d047 N |
158 | if (*cp == '.') |
159 | b = strtoul(cp+1, &cp, 10); | |
160 | if (*cp == '.') | |
161 | c = strtoul(cp+1, &cp, 10); | |
98c6faba | 162 | |
682c7051 | 163 | return (a*1000000)+(b*1000)+c; |
64c4757e NB |
164 | } |
165 | ||
71204a50 | 166 | #ifndef MDASSEMBLE |
bc77ed53 DW |
167 | int mdadm_version(char *version) |
168 | { | |
169 | int a, b, c; | |
170 | char *cp; | |
171 | ||
172 | if (!version) | |
173 | version = Version; | |
174 | ||
175 | cp = strchr(version, '-'); | |
176 | if (!cp || *(cp+1) != ' ' || *(cp+2) != 'v') | |
177 | return -1; | |
178 | cp += 3; | |
179 | a = strtoul(cp, &cp, 10); | |
180 | if (*cp != '.') | |
181 | return -1; | |
182 | b = strtoul(cp+1, &cp, 10); | |
183 | if (*cp == '.') | |
184 | c = strtoul(cp+1, &cp, 10); | |
185 | else | |
186 | c = 0; | |
187 | if (*cp != ' ' && *cp != '-') | |
188 | return -1; | |
189 | return (a*1000000)+(b*1000)+c; | |
190 | } | |
191 | ||
51d4261c | 192 | unsigned long long parse_size(char *size) |
84e11361 N |
193 | { |
194 | /* parse 'size' which should be a number optionally | |
195 | * followed by 'K', 'M', or 'G'. | |
196 | * Without a suffix, K is assumed. | |
197 | * Number returned is in sectors (half-K) | |
822e393a | 198 | * INVALID_SECTORS returned on error. |
84e11361 N |
199 | */ |
200 | char *c; | |
201 | long long s = strtoll(size, &c, 10); | |
202 | if (s > 0) { | |
203 | switch (*c) { | |
204 | case 'K': | |
205 | c++; | |
206 | default: | |
207 | s *= 2; | |
208 | break; | |
209 | case 'M': | |
210 | c++; | |
211 | s *= 1024 * 2; | |
212 | break; | |
213 | case 'G': | |
214 | c++; | |
215 | s *= 1024 * 1024 * 2; | |
216 | break; | |
72ca9bcf N |
217 | case 's': /* sectors */ |
218 | c++; | |
219 | break; | |
84e11361 | 220 | } |
51d4261c | 221 | } else |
822e393a | 222 | s = INVALID_SECTORS; |
84e11361 | 223 | if (*c) |
822e393a | 224 | s = INVALID_SECTORS; |
84e11361 N |
225 | return s; |
226 | } | |
227 | ||
4a06e2c2 N |
228 | int parse_layout_10(char *layout) |
229 | { | |
230 | int copies, rv; | |
231 | char *cp; | |
232 | /* Parse the layout string for raid10 */ | |
233 | /* 'f', 'o' or 'n' followed by a number <= raid_disks */ | |
234 | if ((layout[0] != 'n' && layout[0] != 'f' && layout[0] != 'o') || | |
235 | (copies = strtoul(layout+1, &cp, 10)) < 1 || | |
236 | copies > 200 || | |
237 | *cp) | |
238 | return -1; | |
239 | if (layout[0] == 'n') | |
240 | rv = 256 + copies; | |
241 | else if (layout[0] == 'o') | |
242 | rv = 0x10000 + (copies<<8) + 1; | |
243 | else | |
244 | rv = 1 + (copies<<8); | |
245 | return rv; | |
246 | } | |
247 | ||
248 | int parse_layout_faulty(char *layout) | |
249 | { | |
250 | /* Parse the layout string for 'faulty' */ | |
251 | int ln = strcspn(layout, "0123456789"); | |
503975b9 | 252 | char *m = xstrdup(layout); |
4a06e2c2 N |
253 | int mode; |
254 | m[ln] = 0; | |
255 | mode = map_name(faultylayout, m); | |
256 | if (mode == UnSet) | |
257 | return -1; | |
258 | ||
259 | return mode | (atoi(layout+ln)<< ModeShift); | |
260 | } | |
79868890 N |
261 | |
262 | long parse_num(char *num) | |
263 | { | |
264 | /* Either return a valid number, or -1 */ | |
265 | char *c; | |
266 | long rv = strtol(num, &c, 10); | |
267 | if (rv < 0 || *c || !num[0]) | |
268 | return -1; | |
269 | else | |
270 | return rv; | |
271 | } | |
eb3929a4 | 272 | #endif |
4a06e2c2 | 273 | |
0430ed48 NB |
274 | void remove_partitions(int fd) |
275 | { | |
276 | /* remove partitions from this block devices. | |
277 | * This is used for components added to an array | |
278 | */ | |
279 | #ifdef BLKPG_DEL_PARTITION | |
280 | struct blkpg_ioctl_arg a; | |
281 | struct blkpg_partition p; | |
282 | ||
283 | a.op = BLKPG_DEL_PARTITION; | |
284 | a.data = (void*)&p; | |
285 | a.datalen = sizeof(p); | |
286 | a.flags = 0; | |
287 | memset(a.data, 0, a.datalen); | |
ca3b6696 | 288 | for (p.pno = 0; p.pno < 16; p.pno++) |
0430ed48 NB |
289 | ioctl(fd, BLKPG, &a); |
290 | #endif | |
291 | } | |
292 | ||
691c6ee1 N |
293 | int test_partition(int fd) |
294 | { | |
295 | /* Check if fd is a whole-disk or a partition. | |
296 | * BLKPG will return EINVAL on a partition, and BLKPG_DEL_PARTITION | |
297 | * will return ENXIO on an invalid partition number. | |
298 | */ | |
299 | struct blkpg_ioctl_arg a; | |
300 | struct blkpg_partition p; | |
301 | a.op = BLKPG_DEL_PARTITION; | |
302 | a.data = (void*)&p; | |
303 | a.datalen = sizeof(p); | |
304 | a.flags = 0; | |
305 | memset(a.data, 0, a.datalen); | |
306 | p.pno = 1<<30; | |
307 | if (ioctl(fd, BLKPG, &a) == 0) | |
308 | /* Very unlikely, but not a partition */ | |
309 | return 0; | |
357ac106 | 310 | if (errno == ENXIO || errno == ENOTTY) |
691c6ee1 N |
311 | /* not a partition */ |
312 | return 0; | |
313 | ||
314 | return 1; | |
315 | } | |
316 | ||
bfd76b93 CA |
317 | int test_partition_from_id(dev_t id) |
318 | { | |
319 | char buf[20]; | |
320 | int fd, rv; | |
321 | ||
322 | sprintf(buf, "%d:%d", major(id), minor(id)); | |
323 | fd = dev_open(buf, O_RDONLY); | |
324 | if (fd < 0) | |
325 | return -1; | |
326 | rv = test_partition(fd); | |
327 | close(fd); | |
328 | return rv; | |
329 | } | |
691c6ee1 | 330 | |
de5a472e | 331 | int enough(int level, int raid_disks, int layout, int clean, char *avail) |
64c4757e | 332 | { |
265e0f17 | 333 | int copies, first; |
de5a472e N |
334 | int i; |
335 | int avail_disks = 0; | |
336 | ||
337 | for (i = 0; i < raid_disks; i++) | |
338 | avail_disks += !!avail[i]; | |
339 | ||
64c4757e | 340 | switch (level) { |
265e0f17 NB |
341 | case 10: |
342 | /* This is the tricky one - we need to check | |
343 | * which actual disks are present. | |
344 | */ | |
702b557b | 345 | copies = (layout&255)* ((layout>>8) & 255); |
ca3b6696 | 346 | first = 0; |
265e0f17 NB |
347 | do { |
348 | /* there must be one of the 'copies' form 'first' */ | |
349 | int n = copies; | |
ca3b6696 | 350 | int cnt = 0; |
b3ec716d | 351 | int this = first; |
265e0f17 | 352 | while (n--) { |
b3ec716d | 353 | if (avail[this]) |
265e0f17 | 354 | cnt++; |
b3ec716d | 355 | this = (this+1) % raid_disks; |
265e0f17 NB |
356 | } |
357 | if (cnt == 0) | |
358 | return 0; | |
b3ec716d | 359 | first = (first+(layout&255)) % raid_disks; |
265e0f17 NB |
360 | } while (first != 0); |
361 | return 1; | |
e5329c37 | 362 | |
df0d4ea0 | 363 | case LEVEL_MULTIPATH: |
e0d19036 | 364 | return avail_disks>= 1; |
df0d4ea0 | 365 | case LEVEL_LINEAR: |
64c4757e NB |
366 | case 0: |
367 | return avail_disks == raid_disks; | |
368 | case 1: | |
369 | return avail_disks >= 1; | |
370 | case 4: | |
371 | case 5: | |
583315d9 NB |
372 | if (clean) |
373 | return avail_disks >= raid_disks-1; | |
374 | else | |
375 | return avail_disks >= raid_disks; | |
98c6faba | 376 | case 6: |
583315d9 NB |
377 | if (clean) |
378 | return avail_disks >= raid_disks-2; | |
379 | else | |
380 | return avail_disks >= raid_disks; | |
64c4757e NB |
381 | default: |
382 | return 0; | |
383 | } | |
384 | } | |
385 | ||
8453e704 N |
386 | int enough_fd(int fd) |
387 | { | |
388 | struct mdu_array_info_s array; | |
389 | struct mdu_disk_info_s disk; | |
aabe020d | 390 | int i, rv; |
8453e704 N |
391 | char *avail; |
392 | ||
393 | if (ioctl(fd, GET_ARRAY_INFO, &array) != 0 || | |
394 | array.raid_disks <= 0) | |
395 | return 0; | |
503975b9 | 396 | avail = xcalloc(array.raid_disks, 1); |
ca3b6696 | 397 | for (i = 0; i < MAX_DISKS && array.nr_disks > 0; i++) { |
8453e704 N |
398 | disk.number = i; |
399 | if (ioctl(fd, GET_DISK_INFO, &disk) != 0) | |
400 | continue; | |
9e6d9291 N |
401 | if (disk.major == 0 && disk.minor == 0) |
402 | continue; | |
403 | array.nr_disks--; | |
404 | ||
8453e704 N |
405 | if (! (disk.state & (1<<MD_DISK_SYNC))) |
406 | continue; | |
407 | if (disk.raid_disk < 0 || disk.raid_disk >= array.raid_disks) | |
408 | continue; | |
8453e704 N |
409 | avail[disk.raid_disk] = 1; |
410 | } | |
411 | /* This is used on an active array, so assume it is clean */ | |
aabe020d | 412 | rv = enough(array.level, array.raid_disks, array.layout, |
de5a472e | 413 | 1, avail); |
aabe020d JS |
414 | free(avail); |
415 | return rv; | |
8453e704 N |
416 | } |
417 | ||
22e263f6 AC |
418 | const int uuid_zero[4] = { 0, 0, 0, 0 }; |
419 | ||
f277ce36 | 420 | int same_uuid(int a[4], int b[4], int swapuuid) |
64c4757e | 421 | { |
f277ce36 NB |
422 | if (swapuuid) { |
423 | /* parse uuids are hostendian. | |
424 | * uuid's from some superblocks are big-ending | |
aba69144 | 425 | * if there is a difference, we need to swap.. |
f277ce36 NB |
426 | */ |
427 | unsigned char *ac = (unsigned char *)a; | |
428 | unsigned char *bc = (unsigned char *)b; | |
429 | int i; | |
ca3b6696 | 430 | for (i = 0; i < 16; i += 4) { |
f277ce36 NB |
431 | if (ac[i+0] != bc[i+3] || |
432 | ac[i+1] != bc[i+2] || | |
433 | ac[i+2] != bc[i+1] || | |
434 | ac[i+3] != bc[i+0]) | |
435 | return 0; | |
436 | } | |
437 | return 1; | |
438 | } else { | |
439 | if (a[0]==b[0] && | |
440 | a[1]==b[1] && | |
441 | a[2]==b[2] && | |
442 | a[3]==b[3]) | |
443 | return 1; | |
444 | return 0; | |
445 | } | |
64c4757e | 446 | } |
ca3b6696 | 447 | |
350f29f9 NB |
448 | void copy_uuid(void *a, int b[4], int swapuuid) |
449 | { | |
450 | if (swapuuid) { | |
451 | /* parse uuids are hostendian. | |
452 | * uuid's from some superblocks are big-ending | |
453 | * if there is a difference, we need to swap.. | |
454 | */ | |
455 | unsigned char *ac = (unsigned char *)a; | |
456 | unsigned char *bc = (unsigned char *)b; | |
457 | int i; | |
ca3b6696 | 458 | for (i = 0; i < 16; i += 4) { |
350f29f9 NB |
459 | ac[i+0] = bc[i+3]; |
460 | ac[i+1] = bc[i+2]; | |
461 | ac[i+2] = bc[i+1]; | |
462 | ac[i+3] = bc[i+0]; | |
463 | } | |
464 | } else | |
465 | memcpy(a, b, 16); | |
466 | } | |
64c4757e | 467 | |
aae5a112 | 468 | char *__fname_from_uuid(int id[4], int swap, char *buf, char sep) |
d7288ddc | 469 | { |
9968e376 | 470 | int i, j; |
d7288ddc N |
471 | char uuid[16]; |
472 | char *c = buf; | |
473 | strcpy(c, "UUID-"); | |
474 | c += strlen(c); | |
aae5a112 | 475 | copy_uuid(uuid, id, swap); |
9968e376 | 476 | for (i = 0; i < 4; i++) { |
9968e376 | 477 | if (i) |
ff54de6e | 478 | *c++ = sep; |
9968e376 DW |
479 | for (j = 3; j >= 0; j--) { |
480 | sprintf(c,"%02x", (unsigned char) uuid[j+4*i]); | |
481 | c+= 2; | |
482 | } | |
d7288ddc N |
483 | } |
484 | return buf; | |
aae5a112 DW |
485 | |
486 | } | |
487 | ||
488 | char *fname_from_uuid(struct supertype *st, struct mdinfo *info, char *buf, char sep) | |
489 | { | |
86983cce N |
490 | // dirty hack to work around an issue with super1 superblocks... |
491 | // super1 superblocks need swapuuid set in order for assembly to | |
492 | // work, but can't have it set if we want this printout to match | |
493 | // all the other uuid printouts in super1.c, so we force swapuuid | |
494 | // to 1 to make our printout match the rest of super1 | |
495 | return __fname_from_uuid(info->uuid, (st->ss == &super1) ? 1 : st->ss->swapuuid, buf, sep); | |
d7288ddc N |
496 | } |
497 | ||
435d4ebb | 498 | #ifndef MDASSEMBLE |
682c7051 NB |
499 | int check_ext2(int fd, char *name) |
500 | { | |
501 | /* | |
502 | * Check for an ext2fs file system. | |
503 | * Superblock is always 1K at 1K offset | |
504 | * | |
505 | * s_magic is le16 at 56 == 0xEF53 | |
506 | * report mtime - le32 at 44 | |
507 | * blocks - le32 at 4 | |
508 | * logblksize - le32 at 24 | |
509 | */ | |
510 | unsigned char sb[1024]; | |
511 | time_t mtime; | |
2538ba2a N |
512 | unsigned long long size; |
513 | int bsize; | |
682c7051 NB |
514 | if (lseek(fd, 1024,0)!= 1024) |
515 | return 0; | |
516 | if (read(fd, sb, 1024)!= 1024) | |
517 | return 0; | |
518 | if (sb[56] != 0x53 || sb[57] != 0xef) | |
519 | return 0; | |
520 | ||
521 | mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8; | |
522 | bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8; | |
523 | size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8; | |
2538ba2a | 524 | size <<= bsize; |
e7b84f9d | 525 | pr_err("%s appears to contain an ext2fs file system\n", |
682c7051 | 526 | name); |
2538ba2a | 527 | cont_err("size=%lluK mtime=%s", size, ctime(&mtime)); |
682c7051 NB |
528 | return 1; |
529 | } | |
530 | ||
531 | int check_reiser(int fd, char *name) | |
532 | { | |
533 | /* | |
534 | * superblock is at 64K | |
535 | * size is 1024; | |
536 | * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52 | |
537 | * | |
538 | */ | |
539 | unsigned char sb[1024]; | |
2538ba2a | 540 | unsigned long long size; |
682c7051 NB |
541 | if (lseek(fd, 64*1024, 0) != 64*1024) |
542 | return 0; | |
543 | if (read(fd, sb, 1024) != 1024) | |
544 | return 0; | |
ca3b6696 N |
545 | if (strncmp((char*)sb+52, "ReIsErFs",8) != 0 && |
546 | strncmp((char*)sb+52, "ReIsEr2Fs",9) != 0) | |
682c7051 | 547 | return 0; |
e7b84f9d | 548 | pr_err("%s appears to contain a reiserfs file system\n",name); |
682c7051 | 549 | size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8; |
2538ba2a | 550 | cont_err("size = %lluK\n", size*4); |
aba69144 | 551 | |
682c7051 NB |
552 | return 1; |
553 | } | |
554 | ||
555 | int check_raid(int fd, char *name) | |
556 | { | |
4b1ac34b | 557 | struct mdinfo info; |
682c7051 | 558 | time_t crtime; |
d078d77c | 559 | char *level; |
82d9eba6 | 560 | struct supertype *st = guess_super(fd); |
f9ce90ba | 561 | |
cb8f6859 N |
562 | if (!st) |
563 | return 0; | |
3da92f27 | 564 | st->ss->load_super(st, fd, name); |
82d9eba6 | 565 | /* Looks like a raid array .. */ |
e7b84f9d | 566 | pr_err("%s appears to be part of a raid array:\n", |
82d9eba6 | 567 | name); |
a5d85af7 | 568 | st->ss->getinfo_super(st, &info, NULL); |
3da92f27 | 569 | st->ss->free_super(st); |
82d9eba6 | 570 | crtime = info.array.ctime; |
d078d77c NB |
571 | level = map_num(pers, info.array.level); |
572 | if (!level) level = "-unknown-"; | |
d33f1518 N |
573 | cont_err("level=%s devices=%d ctime=%s", |
574 | level, info.array.raid_disks, ctime(&crtime)); | |
82d9eba6 | 575 | return 1; |
682c7051 NB |
576 | } |
577 | ||
682c7051 NB |
578 | int ask(char *mesg) |
579 | { | |
580 | char *add = ""; | |
581 | int i; | |
ca3b6696 | 582 | for (i = 0; i < 5; i++) { |
682c7051 NB |
583 | char buf[100]; |
584 | fprintf(stderr, "%s%s", mesg, add); | |
585 | fflush(stderr); | |
586 | if (fgets(buf, 100, stdin)==NULL) | |
587 | return 0; | |
588 | if (buf[0]=='y' || buf[0]=='Y') | |
589 | return 1; | |
590 | if (buf[0]=='n' || buf[0]=='N') | |
591 | return 0; | |
592 | add = "(y/n) "; | |
593 | } | |
e7b84f9d | 594 | pr_err("assuming 'no'\n"); |
682c7051 NB |
595 | return 0; |
596 | } | |
435d4ebb | 597 | #endif /* MDASSEMBLE */ |
682c7051 | 598 | |
8d80900b | 599 | int is_standard(char *dev, int *nump) |
e5329c37 NB |
600 | { |
601 | /* tests if dev is a "standard" md dev name. | |
602 | * i.e if the last component is "/dNN" or "/mdNN", | |
aba69144 | 603 | * where NN is a string of digits |
598f0d58 NB |
604 | * Returns 1 if a partitionable standard, |
605 | * -1 if non-partitonable, | |
606 | * 0 if not a standard name. | |
e5329c37 | 607 | */ |
8d80900b | 608 | char *d = strrchr(dev, '/'); |
ca3b6696 | 609 | int type = 0; |
8d80900b NB |
610 | int num; |
611 | if (!d) | |
e5329c37 | 612 | return 0; |
ca3b6696 N |
613 | if (strncmp(d, "/d",2) == 0) |
614 | d += 2, type = 1; /* /dev/md/dN{pM} */ | |
615 | else if (strncmp(d, "/md_d", 5) == 0) | |
616 | d += 5, type = 1; /* /dev/md_dN{pM} */ | |
617 | else if (strncmp(d, "/md", 3) == 0) | |
618 | d += 3, type = -1; /* /dev/mdN */ | |
619 | else if (d-dev > 3 && strncmp(d-2, "md/", 3) == 0) | |
620 | d += 1, type = -1; /* /dev/md/N */ | |
e5329c37 NB |
621 | else |
622 | return 0; | |
8d80900b | 623 | if (!*d) |
e5329c37 | 624 | return 0; |
8d80900b NB |
625 | num = atoi(d); |
626 | while (isdigit(*d)) | |
627 | d++; | |
628 | if (*d) | |
e5329c37 | 629 | return 0; |
8d80900b NB |
630 | if (nump) *nump = num; |
631 | ||
632 | return type; | |
e5329c37 NB |
633 | } |
634 | ||
4b1ac34b | 635 | unsigned long calc_csum(void *super, int bytes) |
82b27616 | 636 | { |
56eb10c0 | 637 | unsigned long long newcsum = 0; |
82b27616 | 638 | int i; |
4b1ac34b NB |
639 | unsigned int csum; |
640 | unsigned int *superc = (unsigned int*) super; | |
82b27616 | 641 | |
ca3b6696 N |
642 | for(i = 0; i < bytes/4; i++) |
643 | newcsum += superc[i]; | |
82b27616 | 644 | csum = (newcsum& 0xffffffff) + (newcsum>>32); |
570c0542 | 645 | #ifdef __alpha__ |
aba69144 | 646 | /* The in-kernel checksum calculation is always 16bit on |
570c0542 | 647 | * the alpha, though it is 32 bit on i386... |
ca3b6696 | 648 | * I wonder what it is elsewhere... (it uses an API in |
570c0542 NB |
649 | * a way that it shouldn't). |
650 | */ | |
651 | csum = (csum & 0xffff) + (csum >> 16); | |
652 | csum = (csum & 0xffff) + (csum >> 16); | |
653 | #endif | |
82b27616 NB |
654 | return csum; |
655 | } | |
cd29a5c8 | 656 | |
435d4ebb | 657 | #ifndef MDASSEMBLE |
56eb10c0 | 658 | char *human_size(long long bytes) |
cd29a5c8 NB |
659 | { |
660 | static char buf[30]; | |
d5d3721e NB |
661 | |
662 | /* We convert bytes to either centi-M{ega,ibi}bytes or | |
663 | * centi-G{igi,ibi}bytes, with appropriate rounding, | |
664 | * and then print 1/100th of those as a decimal. | |
665 | * We allow upto 2048Megabytes before converting to | |
666 | * gigabytes, as that shows more precision and isn't | |
667 | * too large a number. | |
01619b48 | 668 | * Terabytes are not yet handled. |
d5d3721e | 669 | */ |
cd29a5c8 | 670 | |
56eb10c0 | 671 | if (bytes < 5000*1024) |
ca3b6696 | 672 | buf[0] = 0; |
d5d3721e NB |
673 | else if (bytes < 2*1024LL*1024LL*1024LL) { |
674 | long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2; | |
675 | long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2; | |
8f23b0b3 | 676 | snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)", |
d5d3721e NB |
677 | cMiB/100 , cMiB % 100, |
678 | cMB/100, cMB % 100); | |
679 | } else { | |
680 | long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2; | |
681 | long cGB = (bytes / (1000000000LL/200LL ) +1) /2; | |
8f23b0b3 | 682 | snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)", |
d5d3721e NB |
683 | cGiB/100 , cGiB % 100, |
684 | cGB/100, cGB % 100); | |
685 | } | |
cd29a5c8 NB |
686 | return buf; |
687 | } | |
e0d19036 | 688 | |
f0ec6710 | 689 | char *human_size_brief(long long bytes, int prefix) |
e0d19036 NB |
690 | { |
691 | static char buf[30]; | |
e0d19036 | 692 | |
570abc6f MN |
693 | /* We convert bytes to either centi-M{ega,ibi}bytes or |
694 | * centi-G{igi,ibi}bytes, with appropriate rounding, | |
695 | * and then print 1/100th of those as a decimal. | |
696 | * We allow upto 2048Megabytes before converting to | |
697 | * gigabytes, as that shows more precision and isn't | |
698 | * too large a number. | |
699 | * Terabytes are not yet handled. | |
f0ec6710 MN |
700 | * |
701 | * If prefix == IEC, we mean prefixes like kibi,mebi,gibi etc. | |
702 | * If prefix == JEDEC, we mean prefixes like kilo,mega,giga etc. | |
570abc6f MN |
703 | */ |
704 | ||
e0d19036 | 705 | if (bytes < 5000*1024) |
570abc6f | 706 | buf[0] = 0; |
f0ec6710 MN |
707 | else if (prefix == IEC) { |
708 | if (bytes < 2*1024LL*1024LL*1024LL) { | |
709 | long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2; | |
710 | snprintf(buf, sizeof(buf), "%ld.%02ldMiB", | |
711 | cMiB/100 , cMiB % 100); | |
712 | } else { | |
713 | long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2; | |
714 | snprintf(buf, sizeof(buf), "%ld.%02ldGiB", | |
715 | cGiB/100 , cGiB % 100); | |
716 | } | |
717 | } | |
718 | else if (prefix == JEDEC) { | |
719 | if (bytes < 2*1024LL*1024LL*1024LL) { | |
720 | long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2; | |
721 | snprintf(buf, sizeof(buf), "%ld.%02ldMB", | |
722 | cMB/100, cMB % 100); | |
723 | } else { | |
724 | long cGB = (bytes / (1000000000LL/200LL ) +1) /2; | |
725 | snprintf(buf, sizeof(buf), "%ld.%02ldGB", | |
726 | cGB/100 , cGB % 100); | |
727 | } | |
570abc6f | 728 | } |
f0ec6710 MN |
729 | else |
730 | buf[0] = 0; | |
570abc6f | 731 | |
e0d19036 NB |
732 | return buf; |
733 | } | |
e4965ef8 N |
734 | |
735 | void print_r10_layout(int layout) | |
736 | { | |
737 | int near = layout & 255; | |
738 | int far = (layout >> 8) & 255; | |
739 | int offset = (layout&0x10000); | |
740 | char *sep = ""; | |
741 | ||
742 | if (near != 1) { | |
743 | printf("%s near=%d", sep, near); | |
744 | sep = ","; | |
745 | } | |
746 | if (far != 1) | |
747 | printf("%s %s=%d", sep, offset?"offset":"far", far); | |
748 | if (near*far == 1) | |
749 | printf("NO REDUNDANCY"); | |
750 | } | |
435d4ebb | 751 | #endif |
e0d19036 | 752 | |
5f8097be NB |
753 | unsigned long long calc_array_size(int level, int raid_disks, int layout, |
754 | int chunksize, unsigned long long devsize) | |
577e8448 | 755 | { |
e03640bd CA |
756 | if (level == 1) |
757 | return devsize; | |
577e8448 AK |
758 | devsize &= ~(unsigned long long)((chunksize>>9)-1); |
759 | return get_data_disks(level, layout, raid_disks) * devsize; | |
760 | } | |
761 | ||
762 | int get_data_disks(int level, int layout, int raid_disks) | |
5f8097be NB |
763 | { |
764 | int data_disks = 0; | |
765 | switch (level) { | |
ca3b6696 N |
766 | case 0: data_disks = raid_disks; |
767 | break; | |
768 | case 1: data_disks = 1; | |
769 | break; | |
5f8097be | 770 | case 4: |
ca3b6696 N |
771 | case 5: data_disks = raid_disks - 1; |
772 | break; | |
773 | case 6: data_disks = raid_disks - 2; | |
774 | break; | |
5f8097be NB |
775 | case 10: data_disks = raid_disks / (layout & 255) / ((layout>>8)&255); |
776 | break; | |
777 | } | |
577e8448 AK |
778 | |
779 | return data_disks; | |
5f8097be NB |
780 | } |
781 | ||
4dd2df09 N |
782 | int devnm2devid(char *devnm) |
783 | { | |
784 | /* First look in /sys/block/$DEVNM/dev for %d:%d | |
785 | * If that fails, try parsing out a number | |
786 | */ | |
787 | char path[100]; | |
788 | char *ep; | |
789 | int fd; | |
790 | int mjr,mnr; | |
791 | ||
792 | sprintf(path, "/sys/block/%s/dev", devnm); | |
793 | fd = open(path, O_RDONLY); | |
794 | if (fd >= 0) { | |
795 | char buf[20]; | |
796 | int n = read(fd, buf, sizeof(buf)); | |
797 | close(fd); | |
798 | if (n > 0) | |
799 | buf[n] = 0; | |
800 | if (n > 0 && sscanf(buf, "%d:%d\n", &mjr, &mnr) == 2) | |
801 | return makedev(mjr, mnr); | |
802 | } | |
803 | if (strncmp(devnm, "md_d", 4) == 0 && | |
804 | isdigit(devnm[4]) && | |
805 | (mnr = strtoul(devnm+4, &ep, 10)) >= 0 && | |
806 | ep > devnm && *ep == 0) | |
807 | return makedev(get_mdp_major(), mnr << MdpMinorShift); | |
808 | ||
809 | if (strncmp(devnm, "md", 2) == 0 && | |
810 | isdigit(devnm[2]) && | |
811 | (mnr = strtoul(devnm+2, &ep, 10)) >= 0 && | |
812 | ep > devnm && *ep == 0) | |
813 | return makedev(MD_MAJOR, mnr); | |
814 | ||
815 | return 0; | |
816 | } | |
817 | ||
5a23a06e | 818 | #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) |
4dd2df09 | 819 | char *get_md_name(char *devnm) |
e0d19036 NB |
820 | { |
821 | /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */ | |
98c6faba | 822 | /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */ |
4dd2df09 | 823 | |
e0d19036 NB |
824 | static char devname[50]; |
825 | struct stat stb; | |
4dd2df09 | 826 | dev_t rdev = devnm2devid(devnm); |
dd0781e5 | 827 | char *dn; |
98c6faba | 828 | |
4dd2df09 N |
829 | if (rdev == 0) |
830 | return 0; | |
831 | if (strncmp(devnm, "md_", 3) == 0) { | |
832 | snprintf(devname, sizeof(devname), "/dev/md/%s", | |
833 | devnm + 3); | |
98c6faba NB |
834 | if (stat(devname, &stb) == 0 |
835 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
836 | && (stb.st_rdev == rdev)) | |
837 | return devname; | |
838 | } | |
4dd2df09 N |
839 | snprintf(devname, sizeof(devname), "/dev/%s", devnm); |
840 | if (stat(devname, &stb) == 0 | |
841 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
842 | && (stb.st_rdev == rdev)) | |
843 | return devname; | |
844 | ||
845 | snprintf(devname, sizeof(devname), "/dev/md/%s", devnm+2); | |
846 | if (stat(devname, &stb) == 0 | |
847 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
848 | && (stb.st_rdev == rdev)) | |
849 | return devname; | |
850 | ||
16c6fa80 | 851 | dn = map_dev(major(rdev), minor(rdev), 0); |
dd0781e5 NB |
852 | if (dn) |
853 | return dn; | |
4dd2df09 | 854 | snprintf(devname, sizeof(devname), "/dev/.tmp.%s", devnm); |
e0d19036 | 855 | if (mknod(devname, S_IFBLK | 0600, rdev) == -1) |
dd0781e5 NB |
856 | if (errno != EEXIST) |
857 | return NULL; | |
e0d19036 NB |
858 | |
859 | if (stat(devname, &stb) == 0 | |
860 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
861 | && (stb.st_rdev == rdev)) | |
862 | return devname; | |
863 | unlink(devname); | |
864 | return NULL; | |
865 | } | |
866 | ||
867 | void put_md_name(char *name) | |
868 | { | |
ca3b6696 | 869 | if (strncmp(name, "/dev/.tmp.md", 12) == 0) |
e0d19036 NB |
870 | unlink(name); |
871 | } | |
435d4ebb | 872 | #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */ |
f9ce90ba | 873 | |
cc742d38 N |
874 | int get_maj_min(char *dev, int *major, int *minor) |
875 | { | |
876 | char *e; | |
877 | *major = strtoul(dev, &e, 0); | |
878 | return (e > dev && *e == ':' && e[1] && | |
879 | (*minor = strtoul(e+1, &e, 0)) >= 0 && | |
880 | *e == 0); | |
881 | } | |
882 | ||
8b0dabea NB |
883 | int dev_open(char *dev, int flags) |
884 | { | |
885 | /* like 'open', but if 'dev' matches %d:%d, create a temp | |
886 | * block device and open that | |
887 | */ | |
8b0dabea NB |
888 | int fd = -1; |
889 | char devname[32]; | |
e81cdd9f | 890 | int major; |
8b0dabea | 891 | int minor; |
e81cdd9f NB |
892 | |
893 | if (!dev) return -1; | |
6df6a774 | 894 | flags |= O_DIRECT; |
e81cdd9f | 895 | |
cc742d38 | 896 | if (get_maj_min(dev, &major, &minor)) { |
9dc7d357 N |
897 | snprintf(devname, sizeof(devname), "/dev/.tmp.md.%d:%d:%d", |
898 | (int)getpid(), major, minor); | |
899 | if (mknod(devname, S_IFBLK|0600, makedev(major, minor)) == 0) { | |
900 | fd = open(devname, flags); | |
901 | unlink(devname); | |
6df6a774 N |
902 | } |
903 | if (fd < 0) { | |
9dc7d357 | 904 | /* Try /tmp as /dev appear to be read-only */ |
6df6a774 N |
905 | snprintf(devname, sizeof(devname), "/tmp/.tmp.md.%d:%d:%d", |
906 | (int)getpid(), major, minor); | |
ca3b6696 | 907 | if (mknod(devname, S_IFBLK|0600, makedev(major, minor)) == 0) { |
6df6a774 N |
908 | fd = open(devname, flags); |
909 | unlink(devname); | |
910 | } | |
8b0dabea NB |
911 | } |
912 | } else | |
6df6a774 | 913 | fd = open(dev, flags); |
8b0dabea NB |
914 | return fd; |
915 | } | |
f9ce90ba | 916 | |
4dd2df09 | 917 | int open_dev_flags(char *devnm, int flags) |
e8a70c89 | 918 | { |
4dd2df09 | 919 | int devid; |
e8a70c89 N |
920 | char buf[20]; |
921 | ||
4dd2df09 N |
922 | devid = devnm2devid(devnm); |
923 | sprintf(buf, "%d:%d", major(devid), minor(devid)); | |
d998b738 N |
924 | return dev_open(buf, flags); |
925 | } | |
926 | ||
4dd2df09 | 927 | int open_dev(char *devnm) |
d998b738 | 928 | { |
4dd2df09 | 929 | return open_dev_flags(devnm, O_RDONLY); |
e8a70c89 N |
930 | } |
931 | ||
4dd2df09 | 932 | int open_dev_excl(char *devnm) |
a931db9e NB |
933 | { |
934 | char buf[20]; | |
935 | int i; | |
7187750e | 936 | int flags = O_RDWR; |
4dd2df09 | 937 | int devid = devnm2devid(devnm); |
399e0b97 | 938 | long delay = 1000; |
a931db9e | 939 | |
4dd2df09 | 940 | sprintf(buf, "%d:%d", major(devid), minor(devid)); |
ca3b6696 | 941 | for (i = 0 ; i < 25 ; i++) { |
7187750e | 942 | int fd = dev_open(buf, flags|O_EXCL); |
a931db9e NB |
943 | if (fd >= 0) |
944 | return fd; | |
7187750e N |
945 | if (errno == EACCES && flags == O_RDWR) { |
946 | flags = O_RDONLY; | |
947 | continue; | |
948 | } | |
a931db9e NB |
949 | if (errno != EBUSY) |
950 | return fd; | |
399e0b97 N |
951 | usleep(delay); |
952 | if (delay < 200000) | |
953 | delay *= 2; | |
a931db9e NB |
954 | } |
955 | return -1; | |
956 | } | |
957 | ||
9008ed1c N |
958 | int same_dev(char *one, char *two) |
959 | { | |
960 | struct stat st1, st2; | |
961 | if (stat(one, &st1) != 0) | |
962 | return 0; | |
963 | if (stat(two, &st2) != 0) | |
964 | return 0; | |
965 | if ((st1.st_mode & S_IFMT) != S_IFBLK) | |
966 | return 0; | |
967 | if ((st2.st_mode & S_IFMT) != S_IFBLK) | |
968 | return 0; | |
969 | return st1.st_rdev == st2.st_rdev; | |
970 | } | |
971 | ||
a7c6e3fb | 972 | void wait_for(char *dev, int fd) |
a714580e N |
973 | { |
974 | int i; | |
a7c6e3fb | 975 | struct stat stb_want; |
399e0b97 | 976 | long delay = 1000; |
a7c6e3fb N |
977 | |
978 | if (fstat(fd, &stb_want) != 0 || | |
979 | (stb_want.st_mode & S_IFMT) != S_IFBLK) | |
980 | return; | |
a714580e | 981 | |
ca3b6696 | 982 | for (i = 0 ; i < 25 ; i++) { |
a714580e | 983 | struct stat stb; |
a7c6e3fb N |
984 | if (stat(dev, &stb) == 0 && |
985 | (stb.st_mode & S_IFMT) == S_IFBLK && | |
986 | (stb.st_rdev == stb_want.st_rdev)) | |
a714580e | 987 | return; |
399e0b97 N |
988 | usleep(delay); |
989 | if (delay < 200000) | |
990 | delay *= 2; | |
a714580e | 991 | } |
436305c6 DW |
992 | if (i == 25) |
993 | dprintf("%s: timeout waiting for %s\n", __func__, dev); | |
a714580e N |
994 | } |
995 | ||
0f22b998 N |
996 | struct superswitch *superlist[] = |
997 | { | |
998 | &super0, &super1, | |
999 | &super_ddf, &super_imsm, | |
0592faeb | 1000 | &mbr, &gpt, |
0f22b998 | 1001 | NULL }; |
f9ce90ba | 1002 | |
ea24acd0 | 1003 | #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) |
f7dd881f | 1004 | |
4725bc31 | 1005 | struct supertype *super_by_fd(int fd, char **subarrayp) |
f9ce90ba | 1006 | { |
1686dc25 NB |
1007 | mdu_array_info_t array; |
1008 | int vers; | |
1009 | int minor; | |
1010 | struct supertype *st = NULL; | |
7e0f6979 | 1011 | struct mdinfo *sra; |
142cb9e1 | 1012 | char *verstr; |
1686dc25 NB |
1013 | char version[20]; |
1014 | int i; | |
f7e7067b | 1015 | char *subarray = NULL; |
4dd2df09 | 1016 | char container[32] = ""; |
1686dc25 | 1017 | |
4dd2df09 | 1018 | sra = sysfs_read(fd, NULL, GET_VERSION); |
1686dc25 NB |
1019 | |
1020 | if (sra) { | |
7e0f6979 NB |
1021 | vers = sra->array.major_version; |
1022 | minor = sra->array.minor_version; | |
142cb9e1 | 1023 | verstr = sra->text_version; |
1686dc25 NB |
1024 | } else { |
1025 | if (ioctl(fd, GET_ARRAY_INFO, &array)) | |
1026 | array.major_version = array.minor_version = 0; | |
1027 | vers = array.major_version; | |
1028 | minor = array.minor_version; | |
142cb9e1 | 1029 | verstr = ""; |
6fbba4c9 | 1030 | } |
82d9eba6 | 1031 | |
1686dc25 NB |
1032 | if (vers != -1) { |
1033 | sprintf(version, "%d.%d", vers, minor); | |
1034 | verstr = version; | |
6fbba4c9 | 1035 | } |
3c558363 | 1036 | if (minor == -2 && is_subarray(verstr)) { |
f7e7067b | 1037 | char *dev = verstr+1; |
5f7e44b2 | 1038 | |
f7e7067b | 1039 | subarray = strchr(dev, '/'); |
92d49ecf | 1040 | if (subarray) { |
f7e7067b | 1041 | *subarray++ = '\0'; |
503975b9 | 1042 | subarray = xstrdup(subarray); |
92d49ecf | 1043 | } |
4dd2df09 | 1044 | strcpy(container, dev); |
f7e7067b NB |
1045 | if (sra) |
1046 | sysfs_free(sra); | |
5f7e44b2 | 1047 | sra = sysfs_read(-1, container, GET_VERSION); |
603f24a0 N |
1048 | if (sra && sra->text_version[0]) |
1049 | verstr = sra->text_version; | |
1050 | else | |
1051 | verstr = "-no-metadata-"; | |
f7e7067b NB |
1052 | } |
1053 | ||
1054 | for (i = 0; st == NULL && superlist[i] ; i++) | |
1055 | st = superlist[i]->match_metadata_desc(verstr); | |
1686dc25 NB |
1056 | |
1057 | if (sra) | |
1058 | sysfs_free(sra); | |
f7e7067b | 1059 | if (st) { |
3b0896f8 | 1060 | st->sb = NULL; |
1f49fb3a N |
1061 | if (subarrayp) |
1062 | *subarrayp = subarray; | |
4dd2df09 N |
1063 | strcpy(st->container_devnm, container); |
1064 | strcpy(st->devnm, fd2devnm(fd)); | |
4725bc31 N |
1065 | } else |
1066 | free(subarray); | |
5f7e44b2 | 1067 | |
82d9eba6 | 1068 | return st; |
f9ce90ba | 1069 | } |
ea24acd0 NB |
1070 | #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */ |
1071 | ||
0f0749ad | 1072 | int dev_size_from_id(dev_t id, unsigned long long *size) |
52d5d101 AC |
1073 | { |
1074 | char buf[20]; | |
1075 | int fd; | |
1076 | ||
1077 | sprintf(buf, "%d:%d", major(id), minor(id)); | |
1078 | fd = dev_open(buf, O_RDONLY); | |
1079 | if (fd < 0) | |
1080 | return 0; | |
1081 | if (get_dev_size(fd, NULL, size)) { | |
1082 | close(fd); | |
1083 | return 1; | |
1084 | } | |
1085 | close(fd); | |
1086 | return 0; | |
1087 | } | |
f9ce90ba | 1088 | |
159c3a1a | 1089 | struct supertype *dup_super(struct supertype *orig) |
3da92f27 | 1090 | { |
159c3a1a | 1091 | struct supertype *st; |
1686dc25 | 1092 | |
d2ca6449 NB |
1093 | if (!orig) |
1094 | return orig; | |
503975b9 | 1095 | st = xcalloc(1, sizeof(*st)); |
159c3a1a NB |
1096 | st->ss = orig->ss; |
1097 | st->max_devs = orig->max_devs; | |
1098 | st->minor_version = orig->minor_version; | |
cb8f6859 | 1099 | st->ignore_hw_compat = orig->ignore_hw_compat; |
23bf42cc | 1100 | st->data_offset = orig->data_offset; |
159c3a1a NB |
1101 | st->sb = NULL; |
1102 | st->info = NULL; | |
1103 | return st; | |
3da92f27 NB |
1104 | } |
1105 | ||
54887ad8 | 1106 | struct supertype *guess_super_type(int fd, enum guess_types guess_type) |
f9ce90ba NB |
1107 | { |
1108 | /* try each load_super to find the best match, | |
1109 | * and return the best superswitch | |
1110 | */ | |
82d9eba6 NB |
1111 | struct superswitch *ss; |
1112 | struct supertype *st; | |
f21e18ca | 1113 | time_t besttime = 0; |
570c0542 | 1114 | int bestsuper = -1; |
f9ce90ba NB |
1115 | int i; |
1116 | ||
503975b9 | 1117 | st = xcalloc(1, sizeof(*st)); |
4dd2df09 | 1118 | st->container_devnm[0] = 0; |
d1d599ea | 1119 | |
ca3b6696 | 1120 | for (i = 0 ; superlist[i]; i++) { |
f9ce90ba NB |
1121 | int rv; |
1122 | ss = superlist[i]; | |
54887ad8 N |
1123 | if (guess_type == guess_array && ss->add_to_super == NULL) |
1124 | continue; | |
1125 | if (guess_type == guess_partitions && ss->add_to_super != NULL) | |
1126 | continue; | |
ef609477 | 1127 | memset(st, 0, sizeof(*st)); |
df3346e6 | 1128 | st->ignore_hw_compat = 1; |
3da92f27 | 1129 | rv = ss->load_super(st, fd, NULL); |
570c0542 NB |
1130 | if (rv == 0) { |
1131 | struct mdinfo info; | |
a5d85af7 | 1132 | st->ss->getinfo_super(st, &info, NULL); |
570c0542 NB |
1133 | if (bestsuper == -1 || |
1134 | besttime < info.array.ctime) { | |
1135 | bestsuper = i; | |
1136 | besttime = info.array.ctime; | |
570c0542 | 1137 | } |
3da92f27 | 1138 | ss->free_super(st); |
570c0542 NB |
1139 | } |
1140 | } | |
1141 | if (bestsuper != -1) { | |
1142 | int rv; | |
ef609477 | 1143 | memset(st, 0, sizeof(*st)); |
df3346e6 | 1144 | st->ignore_hw_compat = 1; |
3da92f27 | 1145 | rv = superlist[bestsuper]->load_super(st, fd, NULL); |
f9ce90ba | 1146 | if (rv == 0) { |
5e747af2 | 1147 | superlist[bestsuper]->free_super(st); |
82d9eba6 | 1148 | return st; |
f9ce90ba NB |
1149 | } |
1150 | } | |
570c0542 | 1151 | free(st); |
f9ce90ba NB |
1152 | return NULL; |
1153 | } | |
fe6729fa | 1154 | |
beae1dfe NB |
1155 | /* Return size of device in bytes */ |
1156 | int get_dev_size(int fd, char *dname, unsigned long long *sizep) | |
1157 | { | |
1158 | unsigned long long ldsize; | |
c2c9bb6f NB |
1159 | struct stat st; |
1160 | ||
1161 | if (fstat(fd, &st) != -1 && S_ISREG(st.st_mode)) | |
1162 | ldsize = (unsigned long long)st.st_size; | |
1163 | else | |
beae1dfe NB |
1164 | #ifdef BLKGETSIZE64 |
1165 | if (ioctl(fd, BLKGETSIZE64, &ldsize) != 0) | |
1166 | #endif | |
1167 | { | |
1168 | unsigned long dsize; | |
1169 | if (ioctl(fd, BLKGETSIZE, &dsize) == 0) { | |
1170 | ldsize = dsize; | |
1171 | ldsize <<= 9; | |
1172 | } else { | |
1173 | if (dname) | |
e7b84f9d | 1174 | pr_err("Cannot get size of %s: %s\b", |
beae1dfe NB |
1175 | dname, strerror(errno)); |
1176 | return 0; | |
1177 | } | |
1178 | } | |
1179 | *sizep = ldsize; | |
1180 | return 1; | |
1181 | } | |
8fac0577 | 1182 | |
3a371610 N |
1183 | /* Return true if this can only be a container, not a member device. |
1184 | * i.e. is and md device and size is zero | |
1185 | */ | |
1186 | int must_be_container(int fd) | |
1187 | { | |
1188 | unsigned long long size; | |
1189 | if (md_get_version(fd) < 0) | |
1190 | return 0; | |
1191 | if (get_dev_size(fd, NULL, &size) == 0) | |
1192 | return 1; | |
1193 | if (size == 0) | |
1194 | return 1; | |
1195 | return 0; | |
1196 | } | |
034b203a TM |
1197 | |
1198 | /* Sets endofpart parameter to the last block used by the last GPT partition on the device. | |
1199 | * Returns: 1 if successful | |
1200 | * -1 for unknown partition type | |
1201 | * 0 for other errors | |
1202 | */ | |
1203 | static int get_gpt_last_partition_end(int fd, unsigned long long *endofpart) | |
1204 | { | |
056b331e | 1205 | struct GPT gpt; |
034b203a | 1206 | unsigned char empty_gpt_entry[16]= {0}; |
db7fdfe4 JS |
1207 | struct GPT_part_entry *part; |
1208 | char buf[512]; | |
034b203a TM |
1209 | unsigned long long curr_part_end; |
1210 | unsigned all_partitions, entry_size; | |
f21e18ca | 1211 | unsigned part_nr; |
034b203a TM |
1212 | |
1213 | *endofpart = 0; | |
1214 | ||
056b331e | 1215 | BUILD_BUG_ON(sizeof(gpt) != 512); |
73e658d8 | 1216 | /* skip protective MBR */ |
034b203a | 1217 | lseek(fd, 512, SEEK_SET); |
73e658d8 | 1218 | /* read GPT header */ |
056b331e | 1219 | if (read(fd, &gpt, 512) != 512) |
034b203a TM |
1220 | return 0; |
1221 | ||
1222 | /* get the number of partition entries and the entry size */ | |
056b331e N |
1223 | all_partitions = __le32_to_cpu(gpt.part_cnt); |
1224 | entry_size = __le32_to_cpu(gpt.part_size); | |
034b203a TM |
1225 | |
1226 | /* Check GPT signature*/ | |
056b331e | 1227 | if (gpt.magic != GPT_SIGNATURE_MAGIC) |
034b203a TM |
1228 | return -1; |
1229 | ||
1230 | /* sanity checks */ | |
1231 | if (all_partitions > 1024 || | |
db7fdfe4 | 1232 | entry_size > sizeof(buf)) |
034b203a TM |
1233 | return -1; |
1234 | ||
db7fdfe4 JS |
1235 | part = (struct GPT_part_entry *)buf; |
1236 | ||
ca3b6696 | 1237 | for (part_nr = 0; part_nr < all_partitions; part_nr++) { |
73e658d8 | 1238 | /* read partition entry */ |
db7fdfe4 | 1239 | if (read(fd, buf, entry_size) != (ssize_t)entry_size) |
73e658d8 LB |
1240 | return 0; |
1241 | ||
034b203a | 1242 | /* is this valid partition? */ |
db7fdfe4 | 1243 | if (memcmp(part->type_guid, empty_gpt_entry, 16) != 0) { |
034b203a | 1244 | /* check the last lba for the current partition */ |
db7fdfe4 | 1245 | curr_part_end = __le64_to_cpu(part->ending_lba); |
034b203a TM |
1246 | if (curr_part_end > *endofpart) |
1247 | *endofpart = curr_part_end; | |
1248 | } | |
1249 | ||
034b203a TM |
1250 | } |
1251 | return 1; | |
1252 | } | |
1253 | ||
1254 | /* Sets endofpart parameter to the last block used by the last partition on the device. | |
1255 | * Returns: 1 if successful | |
1256 | * -1 for unknown partition type | |
1257 | * 0 for other errors | |
1258 | */ | |
1259 | static int get_last_partition_end(int fd, unsigned long long *endofpart) | |
1260 | { | |
056b331e | 1261 | struct MBR boot_sect; |
034b203a TM |
1262 | struct MBR_part_record *part; |
1263 | unsigned long long curr_part_end; | |
f21e18ca | 1264 | unsigned part_nr; |
034b203a TM |
1265 | int retval = 0; |
1266 | ||
1267 | *endofpart = 0; | |
1268 | ||
056b331e | 1269 | BUILD_BUG_ON(sizeof(boot_sect) != 512); |
034b203a TM |
1270 | /* read MBR */ |
1271 | lseek(fd, 0, 0); | |
056b331e | 1272 | if (read(fd, &boot_sect, 512) != 512) |
034b203a TM |
1273 | goto abort; |
1274 | ||
1275 | /* check MBP signature */ | |
056b331e | 1276 | if (boot_sect.magic == MBR_SIGNATURE_MAGIC) { |
034b203a TM |
1277 | retval = 1; |
1278 | /* found the correct signature */ | |
056b331e | 1279 | part = boot_sect.parts; |
034b203a | 1280 | |
ca3b6696 | 1281 | for (part_nr = 0; part_nr < MBR_PARTITIONS; part_nr++) { |
034b203a TM |
1282 | /* check for GPT type */ |
1283 | if (part->part_type == MBR_GPT_PARTITION_TYPE) { | |
1284 | retval = get_gpt_last_partition_end(fd, endofpart); | |
1285 | break; | |
1286 | } | |
1287 | /* check the last used lba for the current partition */ | |
1288 | curr_part_end = __le32_to_cpu(part->first_sect_lba) + | |
1289 | __le32_to_cpu(part->blocks_num); | |
1290 | if (curr_part_end > *endofpart) | |
1291 | *endofpart = curr_part_end; | |
1292 | ||
1293 | part++; | |
1294 | } | |
1295 | } else { | |
1296 | /* Unknown partition table */ | |
1297 | retval = -1; | |
1298 | } | |
1299 | abort: | |
1300 | return retval; | |
1301 | } | |
1302 | ||
53ed6ac3 KW |
1303 | int check_partitions(int fd, char *dname, unsigned long long freesize, |
1304 | unsigned long long size) | |
034b203a TM |
1305 | { |
1306 | /* | |
1307 | * Check where the last partition ends | |
1308 | */ | |
1309 | unsigned long long endofpart; | |
1310 | int ret; | |
1311 | ||
1312 | if ((ret = get_last_partition_end(fd, &endofpart)) > 0) { | |
1313 | /* There appears to be a partition table here */ | |
1314 | if (freesize == 0) { | |
1315 | /* partitions will not be visible in new device */ | |
e7b84f9d N |
1316 | pr_err("partition table exists on %s but will be lost or\n" |
1317 | " meaningless after creating array\n", | |
1318 | dname); | |
034b203a TM |
1319 | return 1; |
1320 | } else if (endofpart > freesize) { | |
1321 | /* last partition overlaps metadata */ | |
e7b84f9d N |
1322 | pr_err("metadata will over-write last partition on %s.\n", |
1323 | dname); | |
034b203a | 1324 | return 1; |
53ed6ac3 KW |
1325 | } else if (size && endofpart > size) { |
1326 | /* partitions will be truncated in new device */ | |
e7b84f9d N |
1327 | pr_err("array size is too small to cover all partitions on %s.\n", |
1328 | dname); | |
53ed6ac3 | 1329 | return 1; |
034b203a TM |
1330 | } |
1331 | } | |
1332 | return 0; | |
1333 | } | |
1334 | ||
a322f70c DW |
1335 | int open_container(int fd) |
1336 | { | |
1337 | /* 'fd' is a block device. Find out if it is in use | |
1338 | * by a container, and return an open fd on that container. | |
1339 | */ | |
1340 | char path[256]; | |
1341 | char *e; | |
1342 | DIR *dir; | |
1343 | struct dirent *de; | |
1344 | int dfd, n; | |
1345 | char buf[200]; | |
1346 | int major, minor; | |
1347 | struct stat st; | |
1348 | ||
1349 | if (fstat(fd, &st) != 0) | |
1350 | return -1; | |
1351 | sprintf(path, "/sys/dev/block/%d:%d/holders", | |
1352 | (int)major(st.st_rdev), (int)minor(st.st_rdev)); | |
1353 | e = path + strlen(path); | |
1354 | ||
1355 | dir = opendir(path); | |
1356 | if (!dir) | |
1357 | return -1; | |
1358 | while ((de = readdir(dir))) { | |
1359 | if (de->d_ino == 0) | |
1360 | continue; | |
1361 | if (de->d_name[0] == '.') | |
1362 | continue; | |
a994592d N |
1363 | /* Need to make sure it is a container and not a volume */ |
1364 | sprintf(e, "/%s/md/metadata_version", de->d_name); | |
1365 | dfd = open(path, O_RDONLY); | |
1366 | if (dfd < 0) | |
1367 | continue; | |
1368 | n = read(dfd, buf, sizeof(buf)); | |
1369 | close(dfd); | |
1370 | if (n <= 0 || (unsigned)n >= sizeof(buf)) | |
1371 | continue; | |
1372 | buf[n] = 0; | |
1373 | if (strncmp(buf, "external", 8) != 0 || | |
1374 | n < 10 || | |
1375 | buf[9] == '/') | |
1376 | continue; | |
a322f70c DW |
1377 | sprintf(e, "/%s/dev", de->d_name); |
1378 | dfd = open(path, O_RDONLY); | |
1379 | if (dfd < 0) | |
1380 | continue; | |
1381 | n = read(dfd, buf, sizeof(buf)); | |
1382 | close(dfd); | |
f21e18ca | 1383 | if (n <= 0 || (unsigned)n >= sizeof(buf)) |
a322f70c DW |
1384 | continue; |
1385 | buf[n] = 0; | |
1386 | if (sscanf(buf, "%d:%d", &major, &minor) != 2) | |
1387 | continue; | |
1388 | sprintf(buf, "%d:%d", major, minor); | |
1389 | dfd = dev_open(buf, O_RDONLY); | |
1390 | if (dfd >= 0) { | |
1391 | closedir(dir); | |
1392 | return dfd; | |
1393 | } | |
1394 | } | |
355726fa | 1395 | closedir(dir); |
a322f70c DW |
1396 | return -1; |
1397 | } | |
1398 | ||
33414a01 DW |
1399 | struct superswitch *version_to_superswitch(char *vers) |
1400 | { | |
1401 | int i; | |
1402 | ||
1403 | for (i = 0; superlist[i]; i++) { | |
1404 | struct superswitch *ss = superlist[i]; | |
1405 | ||
1406 | if (strcmp(vers, ss->name) == 0) | |
1407 | return ss; | |
1408 | } | |
1409 | ||
1410 | return NULL; | |
1411 | } | |
1412 | ||
4dd2df09 N |
1413 | int metadata_container_matches(char *metadata, char *devnm) |
1414 | { | |
1415 | /* Check if 'devnm' is the container named in 'metadata' | |
1416 | * which is | |
1417 | * /containername/componentname or | |
1418 | * -containername/componentname | |
1419 | */ | |
1420 | int l; | |
1421 | if (*metadata != '/' && *metadata != '-') | |
1422 | return 0; | |
1423 | l = strlen(devnm); | |
1424 | if (strncmp(metadata+1, devnm, l) != 0) | |
1425 | return 0; | |
1426 | if (metadata[l+1] != '/') | |
1427 | return 0; | |
1428 | return 1; | |
1429 | } | |
1430 | ||
1431 | int metadata_subdev_matches(char *metadata, char *devnm) | |
1432 | { | |
1433 | /* Check if 'devnm' is the subdev named in 'metadata' | |
1434 | * which is | |
1435 | * /containername/subdev or | |
1436 | * -containername/subdev | |
1437 | */ | |
1438 | char *sl; | |
1439 | if (*metadata != '/' && *metadata != '-') | |
1440 | return 0; | |
1441 | sl = strchr(metadata+1, '/'); | |
1442 | if (!sl) | |
1443 | return 0; | |
1444 | if (strcmp(sl+1, devnm) == 0) | |
1445 | return 1; | |
1446 | return 0; | |
1447 | } | |
1448 | ||
33414a01 DW |
1449 | int is_container_member(struct mdstat_ent *mdstat, char *container) |
1450 | { | |
1451 | if (mdstat->metadata_version == NULL || | |
1452 | strncmp(mdstat->metadata_version, "external:", 9) != 0 || | |
4dd2df09 | 1453 | !metadata_container_matches(mdstat->metadata_version+9, container)) |
33414a01 DW |
1454 | return 0; |
1455 | ||
1456 | return 1; | |
1457 | } | |
1458 | ||
1459 | int is_subarray_active(char *subarray, char *container) | |
1460 | { | |
1461 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
1462 | struct mdstat_ent *ent; | |
1463 | ||
e5408a32 DW |
1464 | for (ent = mdstat; ent; ent = ent->next) |
1465 | if (is_container_member(ent, container)) | |
e5e5d7ce | 1466 | if (strcmp(to_subarray(ent, container), subarray) == 0) |
33414a01 | 1467 | break; |
33414a01 DW |
1468 | |
1469 | free_mdstat(mdstat); | |
1470 | ||
1471 | return ent != NULL; | |
1472 | } | |
1473 | ||
1474 | /* open_subarray - opens a subarray in a container | |
1475 | * @dev: container device name | |
feab51f8 | 1476 | * @st: empty supertype |
33414a01 DW |
1477 | * @quiet: block reporting errors flag |
1478 | * | |
1479 | * On success returns an fd to a container and fills in *st | |
1480 | */ | |
feab51f8 | 1481 | int open_subarray(char *dev, char *subarray, struct supertype *st, int quiet) |
33414a01 DW |
1482 | { |
1483 | struct mdinfo *mdi; | |
a951a4f7 | 1484 | struct mdinfo *info; |
33414a01 | 1485 | int fd, err = 1; |
4dd2df09 | 1486 | char *_devnm; |
33414a01 DW |
1487 | |
1488 | fd = open(dev, O_RDWR|O_EXCL); | |
1489 | if (fd < 0) { | |
1490 | if (!quiet) | |
e7b84f9d | 1491 | pr_err("Couldn't open %s, aborting\n", |
33414a01 | 1492 | dev); |
b990032d | 1493 | return -1; |
33414a01 DW |
1494 | } |
1495 | ||
4dd2df09 N |
1496 | _devnm = fd2devnm(fd); |
1497 | if (_devnm == NULL) { | |
33414a01 | 1498 | if (!quiet) |
e7b84f9d N |
1499 | pr_err("Failed to determine device number for %s\n", |
1500 | dev); | |
33414a01 DW |
1501 | goto close_fd; |
1502 | } | |
4dd2df09 | 1503 | strcpy(st->devnm, _devnm); |
33414a01 | 1504 | |
4dd2df09 | 1505 | mdi = sysfs_read(fd, st->devnm, GET_VERSION|GET_LEVEL); |
33414a01 DW |
1506 | if (!mdi) { |
1507 | if (!quiet) | |
e7b84f9d | 1508 | pr_err("Failed to read sysfs for %s\n", |
33414a01 DW |
1509 | dev); |
1510 | goto close_fd; | |
1511 | } | |
1512 | ||
1513 | if (mdi->array.level != UnSet) { | |
1514 | if (!quiet) | |
e7b84f9d | 1515 | pr_err("%s is not a container\n", dev); |
33414a01 DW |
1516 | goto free_sysfs; |
1517 | } | |
1518 | ||
1519 | st->ss = version_to_superswitch(mdi->text_version); | |
1520 | if (!st->ss) { | |
1521 | if (!quiet) | |
e7b84f9d N |
1522 | pr_err("Operation not supported for %s metadata\n", |
1523 | mdi->text_version); | |
33414a01 DW |
1524 | goto free_sysfs; |
1525 | } | |
1526 | ||
4dd2df09 | 1527 | if (st->devnm[0] == 0) { |
33414a01 | 1528 | if (!quiet) |
e7b84f9d | 1529 | pr_err("Failed to allocate device name\n"); |
33414a01 DW |
1530 | goto free_sysfs; |
1531 | } | |
1532 | ||
db20d413 | 1533 | if (!st->ss->load_container) { |
33414a01 | 1534 | if (!quiet) |
e7b84f9d | 1535 | pr_err("%s is not a container\n", dev); |
4dd2df09 | 1536 | goto free_sysfs; |
33414a01 DW |
1537 | } |
1538 | ||
db20d413 | 1539 | if (st->ss->load_container(st, fd, NULL)) { |
33414a01 | 1540 | if (!quiet) |
e7b84f9d | 1541 | pr_err("Failed to load metadata for %s\n", |
db20d413 | 1542 | dev); |
4dd2df09 | 1543 | goto free_sysfs; |
33414a01 DW |
1544 | } |
1545 | ||
a951a4f7 N |
1546 | info = st->ss->container_content(st, subarray); |
1547 | if (!info) { | |
1548 | if (!quiet) | |
e7b84f9d | 1549 | pr_err("Failed to find subarray-%s in %s\n", |
a951a4f7 N |
1550 | subarray, dev); |
1551 | goto free_super; | |
1552 | } | |
1553 | free(info); | |
1554 | ||
33414a01 DW |
1555 | err = 0; |
1556 | ||
1557 | free_super: | |
1558 | if (err) | |
1559 | st->ss->free_super(st); | |
33414a01 DW |
1560 | free_sysfs: |
1561 | sysfs_free(mdi); | |
1562 | close_fd: | |
1563 | if (err) | |
1564 | close(fd); | |
1565 | ||
1566 | if (err) | |
1567 | return -1; | |
1568 | else | |
1569 | return fd; | |
1570 | } | |
1571 | ||
7801ac20 N |
1572 | int add_disk(int mdfd, struct supertype *st, |
1573 | struct mdinfo *sra, struct mdinfo *info) | |
1574 | { | |
1575 | /* Add a device to an array, in one of 2 ways. */ | |
1576 | int rv; | |
1577 | #ifndef MDASSEMBLE | |
1578 | if (st->ss->external) { | |
d23534e4 DW |
1579 | if (info->disk.state & (1<<MD_DISK_SYNC)) |
1580 | info->recovery_start = MaxSector; | |
1581 | else | |
1582 | info->recovery_start = 0; | |
2904b26f | 1583 | rv = sysfs_add_disk(sra, info, 0); |
7801ac20 N |
1584 | if (! rv) { |
1585 | struct mdinfo *sd2; | |
f35f2525 N |
1586 | for (sd2 = sra->devs; sd2; sd2=sd2->next) |
1587 | if (sd2 == info) | |
1588 | break; | |
1589 | if (sd2 == NULL) { | |
503975b9 | 1590 | sd2 = xmalloc(sizeof(*sd2)); |
f35f2525 N |
1591 | *sd2 = *info; |
1592 | sd2->next = sra->devs; | |
1593 | sra->devs = sd2; | |
1594 | } | |
7801ac20 N |
1595 | } |
1596 | } else | |
1597 | #endif | |
1598 | rv = ioctl(mdfd, ADD_NEW_DISK, &info->disk); | |
1599 | return rv; | |
1600 | } | |
1601 | ||
de6ae750 N |
1602 | int remove_disk(int mdfd, struct supertype *st, |
1603 | struct mdinfo *sra, struct mdinfo *info) | |
1604 | { | |
1605 | int rv; | |
1606 | /* Remove the disk given by 'info' from the array */ | |
1607 | #ifndef MDASSEMBLE | |
1608 | if (st->ss->external) | |
1609 | rv = sysfs_set_str(sra, info, "slot", "none"); | |
1610 | else | |
1611 | #endif | |
1612 | rv = ioctl(mdfd, HOT_REMOVE_DISK, makedev(info->disk.major, | |
1613 | info->disk.minor)); | |
1614 | return rv; | |
1615 | } | |
1616 | ||
f35f2525 N |
1617 | int set_array_info(int mdfd, struct supertype *st, struct mdinfo *info) |
1618 | { | |
1619 | /* Initialise kernel's knowledge of array. | |
1620 | * This varies between externally managed arrays | |
1621 | * and older kernels | |
1622 | */ | |
1623 | int vers = md_get_version(mdfd); | |
1624 | int rv; | |
1625 | ||
1626 | #ifndef MDASSEMBLE | |
1627 | if (st->ss->external) | |
1628 | rv = sysfs_set_array(info, vers); | |
1629 | else | |
1630 | #endif | |
1631 | if ((vers % 100) >= 1) { /* can use different versions */ | |
1632 | mdu_array_info_t inf; | |
1633 | memset(&inf, 0, sizeof(inf)); | |
1634 | inf.major_version = info->array.major_version; | |
1635 | inf.minor_version = info->array.minor_version; | |
1636 | rv = ioctl(mdfd, SET_ARRAY_INFO, &inf); | |
1637 | } else | |
1638 | rv = ioctl(mdfd, SET_ARRAY_INFO, NULL); | |
1639 | return rv; | |
1640 | } | |
1641 | ||
1e5c6983 DW |
1642 | unsigned long long min_recovery_start(struct mdinfo *array) |
1643 | { | |
1644 | /* find the minimum recovery_start in an array for metadata | |
1645 | * formats that only record per-array recovery progress instead | |
1646 | * of per-device | |
1647 | */ | |
1648 | unsigned long long recovery_start = MaxSector; | |
1649 | struct mdinfo *d; | |
1650 | ||
1651 | for (d = array->devs; d; d = d->next) | |
1652 | recovery_start = min(recovery_start, d->recovery_start); | |
1653 | ||
1654 | return recovery_start; | |
1655 | } | |
1656 | ||
4dd2df09 | 1657 | int mdmon_pid(char *devnm) |
a931db9e NB |
1658 | { |
1659 | char path[100]; | |
1660 | char pid[10]; | |
1661 | int fd; | |
1662 | int n; | |
10013317 | 1663 | |
4dd2df09 | 1664 | sprintf(path, "%s/%s.pid", MDMON_DIR, devnm); |
10013317 | 1665 | |
24f6f99b | 1666 | fd = open(path, O_RDONLY | O_NOATIME, 0); |
a931db9e NB |
1667 | |
1668 | if (fd < 0) | |
cf556303 | 1669 | return -1; |
a931db9e NB |
1670 | n = read(fd, pid, 9); |
1671 | close(fd); | |
1672 | if (n <= 0) | |
cf556303 | 1673 | return -1; |
24f6f99b | 1674 | return atoi(pid); |
a931db9e NB |
1675 | } |
1676 | ||
4dd2df09 | 1677 | int mdmon_running(char *devnm) |
a931db9e | 1678 | { |
4dd2df09 | 1679 | int pid = mdmon_pid(devnm); |
24f6f99b | 1680 | if (pid <= 0) |
a931db9e | 1681 | return 0; |
24f6f99b | 1682 | if (kill(pid, 0) == 0) |
a931db9e NB |
1683 | return 1; |
1684 | return 0; | |
1685 | } | |
1686 | ||
4dd2df09 | 1687 | int start_mdmon(char *devnm) |
8850ee3e | 1688 | { |
1913c325 | 1689 | int i, skipped; |
44d2e365 | 1690 | int len; |
ca3b6696 | 1691 | pid_t pid; |
9fe32043 | 1692 | int status; |
44d2e365 N |
1693 | char pathbuf[1024]; |
1694 | char *paths[4] = { | |
1695 | pathbuf, | |
85945e19 | 1696 | BINDIR "/mdmon", |
2f1bcf43 | 1697 | "./mdmon", |
44d2e365 N |
1698 | NULL |
1699 | }; | |
8850ee3e | 1700 | |
40ebbb9c | 1701 | if (check_env("MDADM_NO_MDMON")) |
8850ee3e N |
1702 | return 0; |
1703 | ||
9cf014ec | 1704 | len = readlink("/proc/self/exe", pathbuf, sizeof(pathbuf)-1); |
44d2e365 N |
1705 | if (len > 0) { |
1706 | char *sl; | |
1707 | pathbuf[len] = 0; | |
1708 | sl = strrchr(pathbuf, '/'); | |
1709 | if (sl) | |
1710 | sl++; | |
1711 | else | |
1712 | sl = pathbuf; | |
1713 | strcpy(sl, "mdmon"); | |
1714 | } else | |
1715 | pathbuf[0] = '\0'; | |
1716 | ||
0f7bdf89 | 1717 | /* First try to run systemctl */ |
9540cc24 N |
1718 | if (!check_env("MDADM_NO_SYSTEMCTL")) |
1719 | switch(fork()) { | |
1720 | case 0: | |
1721 | /* FIXME yuk. CLOSE_EXEC?? */ | |
1722 | skipped = 0; | |
1723 | for (i = 3; skipped < 20; i++) | |
1724 | if (close(i) < 0) | |
1725 | skipped++; | |
1726 | else | |
1727 | skipped = 0; | |
1728 | ||
1729 | /* Don't want to see error messages from | |
1730 | * systemctl. If the service doesn't exist, | |
1731 | * we start mdmon ourselves. | |
1732 | */ | |
1733 | close(2); | |
1734 | open("/dev/null", O_WRONLY); | |
1735 | snprintf(pathbuf, sizeof(pathbuf), "mdmon@%s.service", | |
1736 | devnm); | |
1737 | status = execl("/usr/bin/systemctl", "systemctl", | |
1738 | "start", | |
1739 | pathbuf, NULL); | |
1740 | status = execl("/bin/systemctl", "systemctl", "start", | |
1741 | pathbuf, NULL); | |
1742 | exit(1); | |
1743 | case -1: pr_err("cannot run mdmon. " | |
1744 | "Array remains readonly\n"); | |
1745 | return -1; | |
1746 | default: /* parent - good */ | |
1747 | pid = wait(&status); | |
1748 | if (pid >= 0 && status == 0) | |
1749 | return 0; | |
1750 | } | |
0f7bdf89 JS |
1751 | |
1752 | /* That failed, try running mdmon directly */ | |
8850ee3e N |
1753 | switch(fork()) { |
1754 | case 0: | |
1755 | /* FIXME yuk. CLOSE_EXEC?? */ | |
1913c325 | 1756 | skipped = 0; |
ca3b6696 | 1757 | for (i = 3; skipped < 20; i++) |
1913c325 N |
1758 | if (close(i) < 0) |
1759 | skipped++; | |
1760 | else | |
1761 | skipped = 0; | |
1762 | ||
ca3b6696 | 1763 | for (i = 0; paths[i]; i++) |
a0963a86 | 1764 | if (paths[i][0]) { |
2f1bcf43 | 1765 | execl(paths[i], paths[i], |
4dd2df09 | 1766 | devnm, NULL); |
a0963a86 | 1767 | } |
8850ee3e | 1768 | exit(1); |
e7b84f9d | 1769 | case -1: pr_err("cannot run mdmon. " |
8850ee3e N |
1770 | "Array remains readonly\n"); |
1771 | return -1; | |
9fe32043 N |
1772 | default: /* parent - good */ |
1773 | pid = wait(&status); | |
15c10423 JS |
1774 | if (pid < 0 || status != 0) { |
1775 | pr_err("failed to launch mdmon. " | |
1776 | "Array remains readonly\n"); | |
9fe32043 | 1777 | return -1; |
15c10423 | 1778 | } |
8850ee3e N |
1779 | } |
1780 | return 0; | |
1781 | } | |
1782 | ||
148acb7b DW |
1783 | __u32 random32(void) |
1784 | { | |
1785 | __u32 rv; | |
1786 | int rfd = open("/dev/urandom", O_RDONLY); | |
1787 | if (rfd < 0 || read(rfd, &rv, 4) != 4) | |
1788 | rv = random(); | |
1789 | if (rfd >= 0) | |
1790 | close(rfd); | |
1791 | return rv; | |
1792 | } | |
1793 | ||
0e600426 | 1794 | #ifndef MDASSEMBLE |
edd8d13c NB |
1795 | int flush_metadata_updates(struct supertype *st) |
1796 | { | |
1797 | int sfd; | |
1798 | if (!st->updates) { | |
1799 | st->update_tail = NULL; | |
1800 | return -1; | |
1801 | } | |
1802 | ||
4dd2df09 | 1803 | sfd = connect_monitor(st->container_devnm); |
edd8d13c NB |
1804 | if (sfd < 0) |
1805 | return -1; | |
1806 | ||
1807 | while (st->updates) { | |
1808 | struct metadata_update *mu = st->updates; | |
1809 | st->updates = mu->next; | |
1810 | ||
1811 | send_message(sfd, mu, 0); | |
1812 | wait_reply(sfd, 0); | |
1813 | free(mu->buf); | |
1814 | free(mu); | |
1815 | } | |
1816 | ack(sfd, 0); | |
1817 | wait_reply(sfd, 0); | |
1818 | close(sfd); | |
1819 | st->update_tail = NULL; | |
1820 | return 0; | |
1821 | } | |
1822 | ||
1823 | void append_metadata_update(struct supertype *st, void *buf, int len) | |
1824 | { | |
1825 | ||
503975b9 | 1826 | struct metadata_update *mu = xmalloc(sizeof(*mu)); |
edd8d13c NB |
1827 | |
1828 | mu->buf = buf; | |
1829 | mu->len = len; | |
1830 | mu->space = NULL; | |
cb23f1f4 | 1831 | mu->space_list = NULL; |
edd8d13c NB |
1832 | mu->next = NULL; |
1833 | *st->update_tail = mu; | |
1834 | st->update_tail = &mu->next; | |
1835 | } | |
0e600426 | 1836 | #endif /* MDASSEMBLE */ |
a931db9e | 1837 | |
fe6729fa NB |
1838 | #ifdef __TINYC__ |
1839 | /* tinyc doesn't optimize this check in ioctl.h out ... */ | |
1840 | unsigned int __invalid_size_argument_for_IOC = 0; | |
1841 | #endif | |
1842 | ||
6d11ec6f AK |
1843 | int experimental(void) |
1844 | { | |
1845 | if (check_env("MDADM_EXPERIMENTAL")) | |
1846 | return 1; | |
1847 | else { | |
e7b84f9d | 1848 | pr_err("To use this feature MDADM_EXPERIMENTAL" |
65c83a80 | 1849 | " environment variable has to be defined.\n"); |
6d11ec6f AK |
1850 | return 0; |
1851 | } | |
1852 | } | |
1853 | ||
326727d9 AC |
1854 | /* Pick all spares matching given criteria from a container |
1855 | * if min_size == 0 do not check size | |
1856 | * if domlist == NULL do not check domains | |
1857 | * if spare_group given add it to domains of each spare | |
1858 | * metadata allows to test domains using metadata of destination array */ | |
1859 | struct mdinfo *container_choose_spares(struct supertype *st, | |
1860 | unsigned long long min_size, | |
1861 | struct domainlist *domlist, | |
1862 | char *spare_group, | |
1863 | const char *metadata, int get_one) | |
1864 | { | |
1865 | struct mdinfo *d, **dp, *disks = NULL; | |
1866 | ||
1867 | /* get list of all disks in container */ | |
1868 | if (st->ss->getinfo_super_disks) | |
1869 | disks = st->ss->getinfo_super_disks(st); | |
1870 | ||
1871 | if (!disks) | |
1872 | return disks; | |
1873 | /* find spare devices on the list */ | |
1874 | dp = &disks->devs; | |
1875 | disks->array.spare_disks = 0; | |
1876 | while (*dp) { | |
1877 | int found = 0; | |
1878 | d = *dp; | |
1879 | if (d->disk.state == 0) { | |
1880 | /* check if size is acceptable */ | |
1881 | unsigned long long dev_size; | |
1882 | dev_t dev = makedev(d->disk.major,d->disk.minor); | |
1883 | ||
1884 | if (!min_size || | |
1885 | (dev_size_from_id(dev, &dev_size) && | |
1886 | dev_size >= min_size)) | |
1887 | found = 1; | |
1888 | /* check if domain matches */ | |
1889 | if (found && domlist) { | |
4dd2df09 | 1890 | struct dev_policy *pol = devid_policy(dev); |
326727d9 AC |
1891 | if (spare_group) |
1892 | pol_add(&pol, pol_domain, | |
1893 | spare_group, NULL); | |
e5508b36 | 1894 | if (domain_test(domlist, pol, metadata) != 1) |
326727d9 AC |
1895 | found = 0; |
1896 | dev_policy_free(pol); | |
1897 | } | |
1898 | } | |
1899 | if (found) { | |
1900 | dp = &d->next; | |
1901 | disks->array.spare_disks++; | |
1902 | if (get_one) { | |
1903 | sysfs_free(*dp); | |
1904 | d->next = NULL; | |
1905 | } | |
1906 | } else { | |
1907 | *dp = d->next; | |
1908 | d->next = NULL; | |
1909 | sysfs_free(d); | |
1910 | } | |
1911 | } | |
1912 | return disks; | |
1913 | } | |
9eafa1de MN |
1914 | |
1915 | /* Checks if paths point to the same device | |
1916 | * Returns 0 if they do. | |
1917 | * Returns 1 if they don't. | |
1918 | * Returns -1 if something went wrong, | |
1919 | * e.g. paths are empty or the files | |
1920 | * they point to don't exist */ | |
1921 | int compare_paths (char* path1, char* path2) | |
1922 | { | |
1923 | struct stat st1,st2; | |
1924 | ||
1925 | if (path1 == NULL || path2 == NULL) | |
1926 | return -1; | |
1927 | if (stat(path1,&st1) != 0) | |
1928 | return -1; | |
1929 | if (stat(path2,&st2) != 0) | |
1930 | return -1; | |
1931 | if ((st1.st_ino == st2.st_ino) && (st1.st_dev == st2.st_dev)) | |
1932 | return 0; | |
1933 | return 1; | |
1934 | } | |
a7dec3fd N |
1935 | |
1936 | /* Make sure we can open as many devices as needed */ | |
1937 | void enable_fds(int devices) | |
1938 | { | |
1939 | unsigned int fds = 20 + devices; | |
1940 | struct rlimit lim; | |
1941 | if (getrlimit(RLIMIT_NOFILE, &lim) != 0 | |
1942 | || lim.rlim_cur >= fds) | |
1943 | return; | |
1944 | if (lim.rlim_max < fds) | |
1945 | lim.rlim_max = fds; | |
1946 | lim.rlim_cur = fds; | |
1947 | setrlimit(RLIMIT_NOFILE, &lim); | |
1948 | } | |
a9c15847 N |
1949 | |
1950 | int in_initrd(void) | |
1951 | { | |
1952 | /* This is based on similar function in systemd. */ | |
1953 | struct statfs s; | |
76d0f188 JS |
1954 | /* statfs.f_type is signed long on s390x and MIPS, causing all |
1955 | sorts of sign extension problems with RAMFS_MAGIC being | |
1956 | defined as 0x858458f6 */ | |
a9c15847 | 1957 | return statfs("/", &s) >= 0 && |
7ac5d47e | 1958 | ((unsigned long)s.f_type == TMPFS_MAGIC || |
76d0f188 JS |
1959 | ((unsigned long)s.f_type & 0xFFFFFFFFUL) == |
1960 | ((unsigned long)RAMFS_MAGIC & 0xFFFFFFFFUL)); | |
a9c15847 | 1961 | } |
8832342d N |
1962 | |
1963 | void reopen_mddev(int mdfd) | |
1964 | { | |
1965 | /* Re-open without any O_EXCL, but keep | |
1966 | * the same fd | |
1967 | */ | |
1968 | char *devnm; | |
1969 | int fd; | |
1970 | devnm = fd2devnm(mdfd); | |
1971 | close(mdfd); | |
1972 | fd = open_dev(devnm); | |
1973 | if (fd >= 0 && fd != mdfd) | |
1974 | dup2(fd, mdfd); | |
1975 | } |