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