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