]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
4 | * Copyright (C) 2001-2006 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@cse.unsw.edu.au> | |
23 | * Paper: Neil Brown | |
24 | * School of Computer Science and Engineering | |
25 | * The University of New South Wales | |
26 | * Sydney, 2052 | |
27 | * Australia | |
28 | */ | |
29 | ||
30 | #include "mdadm.h" | |
31 | #include "md_p.h" | |
32 | #include <sys/utsname.h> | |
33 | #include <ctype.h> | |
34 | #include <dirent.h> | |
35 | ||
36 | /* | |
37 | * following taken from linux/blkpg.h because they aren't | |
38 | * anywhere else and it isn't safe to #include linux/ * stuff. | |
39 | */ | |
40 | ||
41 | #define BLKPG _IO(0x12,105) | |
42 | ||
43 | /* The argument structure */ | |
44 | struct blkpg_ioctl_arg { | |
45 | int op; | |
46 | int flags; | |
47 | int datalen; | |
48 | void *data; | |
49 | }; | |
50 | ||
51 | /* The subfunctions (for the op field) */ | |
52 | #define BLKPG_ADD_PARTITION 1 | |
53 | #define BLKPG_DEL_PARTITION 2 | |
54 | ||
55 | /* Sizes of name fields. Unused at present. */ | |
56 | #define BLKPG_DEVNAMELTH 64 | |
57 | #define BLKPG_VOLNAMELTH 64 | |
58 | ||
59 | /* The data structure for ADD_PARTITION and DEL_PARTITION */ | |
60 | struct blkpg_partition { | |
61 | long long start; /* starting offset in bytes */ | |
62 | long long length; /* length in bytes */ | |
63 | int pno; /* partition number */ | |
64 | char devname[BLKPG_DEVNAMELTH]; /* partition name, like sda5 or c0d1p2, | |
65 | to be used in kernel messages */ | |
66 | char volname[BLKPG_VOLNAMELTH]; /* volume label */ | |
67 | }; | |
68 | ||
69 | /* | |
70 | * Parse a 128 bit uuid in 4 integers | |
71 | * format is 32 hexx nibbles with options :.<space> separator | |
72 | * If not exactly 32 hex digits are found, return 0 | |
73 | * else return 1 | |
74 | */ | |
75 | int parse_uuid(char *str, int uuid[4]) | |
76 | { | |
77 | int hit = 0; /* number of Hex digIT */ | |
78 | int i; | |
79 | char c; | |
80 | for (i=0; i<4; i++) uuid[i]=0; | |
81 | ||
82 | while ((c= *str++)) { | |
83 | int n; | |
84 | if (c>='0' && c<='9') | |
85 | n = c-'0'; | |
86 | else if (c>='a' && c <= 'f') | |
87 | n = 10 + c - 'a'; | |
88 | else if (c>='A' && c <= 'F') | |
89 | n = 10 + c - 'A'; | |
90 | else if (strchr(":. -", c)) | |
91 | continue; | |
92 | else return 0; | |
93 | ||
94 | if (hit<32) { | |
95 | uuid[hit/8] <<= 4; | |
96 | uuid[hit/8] += n; | |
97 | } | |
98 | hit++; | |
99 | } | |
100 | if (hit == 32) | |
101 | return 1; | |
102 | return 0; | |
103 | } | |
104 | ||
105 | ||
106 | /* | |
107 | * Get the md version number. | |
108 | * We use the RAID_VERSION ioctl if it is supported | |
109 | * If not, but we have a block device with major '9', we assume | |
110 | * 0.36.0 | |
111 | * | |
112 | * Return version number as 24 but number - assume version parts | |
113 | * always < 255 | |
114 | */ | |
115 | ||
116 | int md_get_version(int fd) | |
117 | { | |
118 | struct stat stb; | |
119 | mdu_version_t vers; | |
120 | ||
121 | if (fstat(fd, &stb)<0) | |
122 | return -1; | |
123 | if ((S_IFMT&stb.st_mode) != S_IFBLK) | |
124 | return -1; | |
125 | ||
126 | if (ioctl(fd, RAID_VERSION, &vers) == 0) | |
127 | return (vers.major*10000) + (vers.minor*100) + vers.patchlevel; | |
128 | if (errno == EACCES) | |
129 | return -1; | |
130 | if (major(stb.st_rdev) == MD_MAJOR) | |
131 | return (3600); | |
132 | return -1; | |
133 | } | |
134 | ||
135 | int get_linux_version() | |
136 | { | |
137 | struct utsname name; | |
138 | char *cp; | |
139 | int a,b,c; | |
140 | if (uname(&name) <0) | |
141 | return -1; | |
142 | ||
143 | cp = name.release; | |
144 | a = strtoul(cp, &cp, 10); | |
145 | if (*cp != '.') return -1; | |
146 | b = strtoul(cp+1, &cp, 10); | |
147 | if (*cp != '.') return -1; | |
148 | c = strtoul(cp+1, NULL, 10); | |
149 | ||
150 | return (a*1000000)+(b*1000)+c; | |
151 | } | |
152 | ||
153 | void remove_partitions(int fd) | |
154 | { | |
155 | /* remove partitions from this block devices. | |
156 | * This is used for components added to an array | |
157 | */ | |
158 | #ifdef BLKPG_DEL_PARTITION | |
159 | struct blkpg_ioctl_arg a; | |
160 | struct blkpg_partition p; | |
161 | ||
162 | a.op = BLKPG_DEL_PARTITION; | |
163 | a.data = (void*)&p; | |
164 | a.datalen = sizeof(p); | |
165 | a.flags = 0; | |
166 | memset(a.data, 0, a.datalen); | |
167 | for (p.pno=0; p.pno < 16; p.pno++) | |
168 | ioctl(fd, BLKPG, &a); | |
169 | #endif | |
170 | } | |
171 | ||
172 | int enough(int level, int raid_disks, int layout, int clean, | |
173 | char *avail, int avail_disks) | |
174 | { | |
175 | int copies, first; | |
176 | switch (level) { | |
177 | case 10: | |
178 | /* This is the tricky one - we need to check | |
179 | * which actual disks are present. | |
180 | */ | |
181 | copies = (layout&255)* ((layout>>8) & 255); | |
182 | first=0; | |
183 | do { | |
184 | /* there must be one of the 'copies' form 'first' */ | |
185 | int n = copies; | |
186 | int cnt=0; | |
187 | while (n--) { | |
188 | if (avail[first]) | |
189 | cnt++; | |
190 | first = (first+1) % raid_disks; | |
191 | } | |
192 | if (cnt == 0) | |
193 | return 0; | |
194 | ||
195 | } while (first != 0); | |
196 | return 1; | |
197 | ||
198 | case -4: | |
199 | return avail_disks>= 1; | |
200 | case -1: | |
201 | case 0: | |
202 | return avail_disks == raid_disks; | |
203 | case 1: | |
204 | return avail_disks >= 1; | |
205 | case 4: | |
206 | case 5: | |
207 | if (clean) | |
208 | return avail_disks >= raid_disks-1; | |
209 | else | |
210 | return avail_disks >= raid_disks; | |
211 | case 6: | |
212 | if (clean) | |
213 | return avail_disks >= raid_disks-2; | |
214 | else | |
215 | return avail_disks >= raid_disks; | |
216 | default: | |
217 | return 0; | |
218 | } | |
219 | } | |
220 | ||
221 | int same_uuid(int a[4], int b[4], int swapuuid) | |
222 | { | |
223 | if (swapuuid) { | |
224 | /* parse uuids are hostendian. | |
225 | * uuid's from some superblocks are big-ending | |
226 | * if there is a difference, we need to swap.. | |
227 | */ | |
228 | unsigned char *ac = (unsigned char *)a; | |
229 | unsigned char *bc = (unsigned char *)b; | |
230 | int i; | |
231 | for (i=0; i<16; i+= 4) { | |
232 | if (ac[i+0] != bc[i+3] || | |
233 | ac[i+1] != bc[i+2] || | |
234 | ac[i+2] != bc[i+1] || | |
235 | ac[i+3] != bc[i+0]) | |
236 | return 0; | |
237 | } | |
238 | return 1; | |
239 | } else { | |
240 | if (a[0]==b[0] && | |
241 | a[1]==b[1] && | |
242 | a[2]==b[2] && | |
243 | a[3]==b[3]) | |
244 | return 1; | |
245 | return 0; | |
246 | } | |
247 | } | |
248 | void copy_uuid(void *a, int b[4], int swapuuid) | |
249 | { | |
250 | if (swapuuid) { | |
251 | /* parse uuids are hostendian. | |
252 | * uuid's from some superblocks are big-ending | |
253 | * if there is a difference, we need to swap.. | |
254 | */ | |
255 | unsigned char *ac = (unsigned char *)a; | |
256 | unsigned char *bc = (unsigned char *)b; | |
257 | int i; | |
258 | for (i=0; i<16; i+= 4) { | |
259 | ac[i+0] = bc[i+3]; | |
260 | ac[i+1] = bc[i+2]; | |
261 | ac[i+2] = bc[i+1]; | |
262 | ac[i+3] = bc[i+0]; | |
263 | } | |
264 | } else | |
265 | memcpy(a, b, 16); | |
266 | } | |
267 | ||
268 | #ifndef MDASSEMBLE | |
269 | int check_ext2(int fd, char *name) | |
270 | { | |
271 | /* | |
272 | * Check for an ext2fs file system. | |
273 | * Superblock is always 1K at 1K offset | |
274 | * | |
275 | * s_magic is le16 at 56 == 0xEF53 | |
276 | * report mtime - le32 at 44 | |
277 | * blocks - le32 at 4 | |
278 | * logblksize - le32 at 24 | |
279 | */ | |
280 | unsigned char sb[1024]; | |
281 | time_t mtime; | |
282 | int size, bsize; | |
283 | if (lseek(fd, 1024,0)!= 1024) | |
284 | return 0; | |
285 | if (read(fd, sb, 1024)!= 1024) | |
286 | return 0; | |
287 | if (sb[56] != 0x53 || sb[57] != 0xef) | |
288 | return 0; | |
289 | ||
290 | mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8; | |
291 | bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8; | |
292 | size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8; | |
293 | fprintf(stderr, Name ": %s appears to contain an ext2fs file system\n", | |
294 | name); | |
295 | fprintf(stderr," size=%dK mtime=%s", | |
296 | size*(1<<bsize), ctime(&mtime)); | |
297 | return 1; | |
298 | } | |
299 | ||
300 | int check_reiser(int fd, char *name) | |
301 | { | |
302 | /* | |
303 | * superblock is at 64K | |
304 | * size is 1024; | |
305 | * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52 | |
306 | * | |
307 | */ | |
308 | unsigned char sb[1024]; | |
309 | unsigned long size; | |
310 | if (lseek(fd, 64*1024, 0) != 64*1024) | |
311 | return 0; | |
312 | if (read(fd, sb, 1024) != 1024) | |
313 | return 0; | |
314 | if (strncmp((char*)sb+52, "ReIsErFs",8)!=0 && | |
315 | strncmp((char*)sb+52, "ReIsEr2Fs",9)!=0) | |
316 | return 0; | |
317 | fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name); | |
318 | size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8; | |
319 | fprintf(stderr, " size = %luK\n", size*4); | |
320 | ||
321 | return 1; | |
322 | } | |
323 | ||
324 | int check_raid(int fd, char *name) | |
325 | { | |
326 | struct mdinfo info; | |
327 | time_t crtime; | |
328 | char *level; | |
329 | struct supertype *st = guess_super(fd); | |
330 | ||
331 | if (!st) return 0; | |
332 | st->ss->load_super(st, fd, name); | |
333 | /* Looks like a raid array .. */ | |
334 | fprintf(stderr, Name ": %s appears to be part of a raid array:\n", | |
335 | name); | |
336 | st->ss->getinfo_super(st, &info); | |
337 | st->ss->free_super(st); | |
338 | crtime = info.array.ctime; | |
339 | level = map_num(pers, info.array.level); | |
340 | if (!level) level = "-unknown-"; | |
341 | fprintf(stderr, " level=%s devices=%d ctime=%s", | |
342 | level, info.array.raid_disks, ctime(&crtime)); | |
343 | return 1; | |
344 | } | |
345 | ||
346 | int ask(char *mesg) | |
347 | { | |
348 | char *add = ""; | |
349 | int i; | |
350 | for (i=0; i<5; i++) { | |
351 | char buf[100]; | |
352 | fprintf(stderr, "%s%s", mesg, add); | |
353 | fflush(stderr); | |
354 | if (fgets(buf, 100, stdin)==NULL) | |
355 | return 0; | |
356 | if (buf[0]=='y' || buf[0]=='Y') | |
357 | return 1; | |
358 | if (buf[0]=='n' || buf[0]=='N') | |
359 | return 0; | |
360 | add = "(y/n) "; | |
361 | } | |
362 | fprintf(stderr, Name ": assuming 'no'\n"); | |
363 | return 0; | |
364 | } | |
365 | #endif /* MDASSEMBLE */ | |
366 | ||
367 | char *map_num(mapping_t *map, int num) | |
368 | { | |
369 | while (map->name) { | |
370 | if (map->num == num) | |
371 | return map->name; | |
372 | map++; | |
373 | } | |
374 | return NULL; | |
375 | } | |
376 | ||
377 | int map_name(mapping_t *map, char *name) | |
378 | { | |
379 | while (map->name) { | |
380 | if (strcmp(map->name, name)==0) | |
381 | return map->num; | |
382 | map++; | |
383 | } | |
384 | return UnSet; | |
385 | } | |
386 | ||
387 | ||
388 | int is_standard(char *dev, int *nump) | |
389 | { | |
390 | /* tests if dev is a "standard" md dev name. | |
391 | * i.e if the last component is "/dNN" or "/mdNN", | |
392 | * where NN is a string of digits | |
393 | */ | |
394 | char *d = strrchr(dev, '/'); | |
395 | int type=0; | |
396 | int num; | |
397 | if (!d) | |
398 | return 0; | |
399 | if (strncmp(d, "/d",2)==0) | |
400 | d += 2, type=1; /* /dev/md/dN{pM} */ | |
401 | else if (strncmp(d, "/md_d", 5)==0) | |
402 | d += 5, type=1; /* /dev/md_dNpM */ | |
403 | else if (strncmp(d, "/md", 3)==0) | |
404 | d += 3, type=-1; /* /dev/mdN */ | |
405 | else if (d-dev > 3 && strncmp(d-2, "md/", 3)==0) | |
406 | d += 1, type=-1; /* /dev/md/N */ | |
407 | else | |
408 | return 0; | |
409 | if (!*d) | |
410 | return 0; | |
411 | num = atoi(d); | |
412 | while (isdigit(*d)) | |
413 | d++; | |
414 | if (*d) | |
415 | return 0; | |
416 | if (nump) *nump = num; | |
417 | ||
418 | return type; | |
419 | } | |
420 | ||
421 | ||
422 | /* | |
423 | * convert a major/minor pair for a block device into a name in /dev, if possible. | |
424 | * On the first call, walk /dev collecting name. | |
425 | * Put them in a simple linked listfor now. | |
426 | */ | |
427 | struct devmap { | |
428 | int major, minor; | |
429 | char *name; | |
430 | struct devmap *next; | |
431 | } *devlist = NULL; | |
432 | int devlist_ready = 0; | |
433 | ||
434 | int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s) | |
435 | { | |
436 | struct stat st; | |
437 | if (S_ISLNK(stb->st_mode)) { | |
438 | stat(name, &st); | |
439 | stb = &st; | |
440 | } | |
441 | ||
442 | if ((stb->st_mode&S_IFMT)== S_IFBLK) { | |
443 | char *n = strdup(name); | |
444 | struct devmap *dm = malloc(sizeof(*dm)); | |
445 | if (strncmp(n, "/dev/./", 7)==0) | |
446 | strcpy(n+4, name+6); | |
447 | if (dm) { | |
448 | dm->major = major(stb->st_rdev); | |
449 | dm->minor = minor(stb->st_rdev); | |
450 | dm->name = n; | |
451 | dm->next = devlist; | |
452 | devlist = dm; | |
453 | } | |
454 | } | |
455 | return 0; | |
456 | } | |
457 | ||
458 | #ifndef HAVE_NFTW | |
459 | #ifdef HAVE_FTW | |
460 | int add_dev_1(const char *name, const struct stat *stb, int flag) | |
461 | { | |
462 | return add_dev(name, stb, flag, NULL); | |
463 | } | |
464 | int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags) | |
465 | { | |
466 | return ftw(path, add_dev_1, nopenfd); | |
467 | } | |
468 | #else | |
469 | int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags) | |
470 | { | |
471 | return 0; | |
472 | } | |
473 | #endif /* HAVE_FTW */ | |
474 | #endif /* HAVE_NFTW */ | |
475 | ||
476 | /* | |
477 | * Find a block device with the right major/minor number. | |
478 | * If we find multiple names, choose the shortest. | |
479 | * If we find a non-standard name, it is probably there | |
480 | * deliberately so prefer it over a standard name. | |
481 | * This applies only to names for MD devices. | |
482 | */ | |
483 | char *map_dev(int major, int minor, int create) | |
484 | { | |
485 | struct devmap *p; | |
486 | char *std = NULL, *nonstd=NULL; | |
487 | int did_check = 0; | |
488 | ||
489 | if (major == 0 && minor == 0) | |
490 | return NULL; | |
491 | ||
492 | retry: | |
493 | if (!devlist_ready) { | |
494 | char *dev = "/dev"; | |
495 | struct stat stb; | |
496 | while(devlist) { | |
497 | struct devmap *d = devlist; | |
498 | devlist = d->next; | |
499 | free(d->name); | |
500 | free(d); | |
501 | } | |
502 | if (lstat(dev, &stb)==0 && | |
503 | S_ISLNK(stb.st_mode)) | |
504 | dev = "/dev/."; | |
505 | nftw(dev, add_dev, 10, FTW_PHYS); | |
506 | devlist_ready=1; | |
507 | did_check = 1; | |
508 | } | |
509 | ||
510 | for (p=devlist; p; p=p->next) | |
511 | if (p->major == major && | |
512 | p->minor == minor) { | |
513 | if (is_standard(p->name, NULL)) { | |
514 | if (std == NULL || | |
515 | strlen(p->name) < strlen(std)) | |
516 | std = p->name; | |
517 | } else { | |
518 | if (nonstd == NULL || | |
519 | strlen(p->name) < strlen(nonstd)) | |
520 | nonstd = p->name; | |
521 | } | |
522 | } | |
523 | if (!std && !nonstd && !did_check) { | |
524 | devlist_ready = 0; | |
525 | goto retry; | |
526 | } | |
527 | if (create && !std && !nonstd) { | |
528 | static char buf[30]; | |
529 | snprintf(buf, sizeof(buf), "%d:%d", major, minor); | |
530 | nonstd = buf; | |
531 | } | |
532 | ||
533 | return nonstd ? nonstd : std; | |
534 | } | |
535 | ||
536 | unsigned long calc_csum(void *super, int bytes) | |
537 | { | |
538 | unsigned long long newcsum = 0; | |
539 | int i; | |
540 | unsigned int csum; | |
541 | unsigned int *superc = (unsigned int*) super; | |
542 | ||
543 | for(i=0; i<bytes/4; i++) | |
544 | newcsum+= superc[i]; | |
545 | csum = (newcsum& 0xffffffff) + (newcsum>>32); | |
546 | #ifdef __alpha__ | |
547 | /* The in-kernel checksum calculation is always 16bit on | |
548 | * the alpha, though it is 32 bit on i386... | |
549 | * I wonder what it is elsewhere... (it uses and API in | |
550 | * a way that it shouldn't). | |
551 | */ | |
552 | csum = (csum & 0xffff) + (csum >> 16); | |
553 | csum = (csum & 0xffff) + (csum >> 16); | |
554 | #endif | |
555 | return csum; | |
556 | } | |
557 | ||
558 | #ifndef MDASSEMBLE | |
559 | char *human_size(long long bytes) | |
560 | { | |
561 | static char buf[30]; | |
562 | ||
563 | /* We convert bytes to either centi-M{ega,ibi}bytes or | |
564 | * centi-G{igi,ibi}bytes, with appropriate rounding, | |
565 | * and then print 1/100th of those as a decimal. | |
566 | * We allow upto 2048Megabytes before converting to | |
567 | * gigabytes, as that shows more precision and isn't | |
568 | * too large a number. | |
569 | * Terrabytes are not yet handled. | |
570 | */ | |
571 | ||
572 | if (bytes < 5000*1024) | |
573 | buf[0]=0; | |
574 | else if (bytes < 2*1024LL*1024LL*1024LL) { | |
575 | long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2; | |
576 | long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2; | |
577 | snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)", | |
578 | cMiB/100 , cMiB % 100, | |
579 | cMB/100, cMB % 100); | |
580 | } else { | |
581 | long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2; | |
582 | long cGB = (bytes / (1000000000LL/200LL ) +1) /2; | |
583 | snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)", | |
584 | cGiB/100 , cGiB % 100, | |
585 | cGB/100, cGB % 100); | |
586 | } | |
587 | return buf; | |
588 | } | |
589 | ||
590 | char *human_size_brief(long long bytes) | |
591 | { | |
592 | static char buf[30]; | |
593 | ||
594 | if (bytes < 5000*1024) | |
595 | snprintf(buf, sizeof(buf), "%ld.%02ldKiB", | |
596 | (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024) | |
597 | ); | |
598 | else if (bytes < 2*1024LL*1024LL*1024LL) | |
599 | snprintf(buf, sizeof(buf), "%ld.%02ldMiB", | |
600 | (long)(bytes>>20), | |
601 | (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100) | |
602 | ); | |
603 | else | |
604 | snprintf(buf, sizeof(buf), "%ld.%02ldGiB", | |
605 | (long)(bytes>>30), | |
606 | (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100) | |
607 | ); | |
608 | return buf; | |
609 | } | |
610 | #endif | |
611 | ||
612 | #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) | |
613 | int get_mdp_major(void) | |
614 | { | |
615 | static int mdp_major = -1; | |
616 | FILE *fl; | |
617 | char *w; | |
618 | int have_block = 0; | |
619 | int have_devices = 0; | |
620 | int last_num = -1; | |
621 | ||
622 | if (mdp_major != -1) | |
623 | return mdp_major; | |
624 | fl = fopen("/proc/devices", "r"); | |
625 | if (!fl) | |
626 | return -1; | |
627 | while ((w = conf_word(fl, 1))) { | |
628 | if (have_block && strcmp(w, "devices:")==0) | |
629 | have_devices = 1; | |
630 | have_block = (strcmp(w, "Block")==0); | |
631 | if (isdigit(w[0])) | |
632 | last_num = atoi(w); | |
633 | if (have_devices && strcmp(w, "mdp")==0) | |
634 | mdp_major = last_num; | |
635 | free(w); | |
636 | } | |
637 | fclose(fl); | |
638 | return mdp_major; | |
639 | } | |
640 | ||
641 | ||
642 | ||
643 | char *get_md_name(int dev) | |
644 | { | |
645 | /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */ | |
646 | /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */ | |
647 | static char devname[50]; | |
648 | struct stat stb; | |
649 | dev_t rdev; | |
650 | char *dn; | |
651 | ||
652 | if (dev < 0) { | |
653 | int mdp = get_mdp_major(); | |
654 | if (mdp < 0) return NULL; | |
655 | rdev = makedev(mdp, (-1-dev)<<6); | |
656 | snprintf(devname, sizeof(devname), "/dev/md/d%d", -1-dev); | |
657 | if (stat(devname, &stb) == 0 | |
658 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
659 | && (stb.st_rdev == rdev)) | |
660 | return devname; | |
661 | } else { | |
662 | rdev = makedev(MD_MAJOR, dev); | |
663 | snprintf(devname, sizeof(devname), "/dev/md%d", dev); | |
664 | if (stat(devname, &stb) == 0 | |
665 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
666 | && (stb.st_rdev == rdev)) | |
667 | return devname; | |
668 | ||
669 | snprintf(devname, sizeof(devname), "/dev/md/%d", dev); | |
670 | if (stat(devname, &stb) == 0 | |
671 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
672 | && (stb.st_rdev == rdev)) | |
673 | return devname; | |
674 | } | |
675 | dn = map_dev(major(rdev), minor(rdev), 0); | |
676 | if (dn) | |
677 | return dn; | |
678 | snprintf(devname, sizeof(devname), "/dev/.tmp.md%d", dev); | |
679 | if (mknod(devname, S_IFBLK | 0600, rdev) == -1) | |
680 | if (errno != EEXIST) | |
681 | return NULL; | |
682 | ||
683 | if (stat(devname, &stb) == 0 | |
684 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
685 | && (stb.st_rdev == rdev)) | |
686 | return devname; | |
687 | unlink(devname); | |
688 | return NULL; | |
689 | } | |
690 | ||
691 | void put_md_name(char *name) | |
692 | { | |
693 | if (strncmp(name, "/dev/.tmp.md", 12)==0) | |
694 | unlink(name); | |
695 | } | |
696 | ||
697 | static int dev2major(int d) | |
698 | { | |
699 | if (d >= 0) | |
700 | return MD_MAJOR; | |
701 | else | |
702 | return get_mdp_major(); | |
703 | } | |
704 | ||
705 | static int dev2minor(int d) | |
706 | { | |
707 | if (d >= 0) | |
708 | return d; | |
709 | return (-1-d) << MdpMinorShift; | |
710 | } | |
711 | ||
712 | int find_free_devnum(int use_partitions) | |
713 | { | |
714 | int devnum; | |
715 | for (devnum = 127; devnum != 128; | |
716 | devnum = devnum ? devnum-1 : (1<<22)-1) { | |
717 | char *dn; | |
718 | int _devnum; | |
719 | ||
720 | _devnum = use_partitions ? (-1-devnum) : devnum; | |
721 | if (mddev_busy(_devnum)) | |
722 | continue; | |
723 | /* make sure it is new to /dev too, at least as a | |
724 | * non-standard */ | |
725 | dn = map_dev(dev2major(_devnum), dev2minor(_devnum), 0); | |
726 | if (dn && ! is_standard(dn, NULL)) | |
727 | continue; | |
728 | break; | |
729 | } | |
730 | if (devnum == 128) | |
731 | return NoMdDev; | |
732 | return use_partitions ? (-1-devnum) : devnum; | |
733 | } | |
734 | #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */ | |
735 | ||
736 | int dev_open(char *dev, int flags) | |
737 | { | |
738 | /* like 'open', but if 'dev' matches %d:%d, create a temp | |
739 | * block device and open that | |
740 | */ | |
741 | char *e; | |
742 | int fd = -1; | |
743 | char devname[32]; | |
744 | int major; | |
745 | int minor; | |
746 | ||
747 | if (!dev) return -1; | |
748 | ||
749 | major = strtoul(dev, &e, 0); | |
750 | if (e > dev && *e == ':' && e[1] && | |
751 | (minor = strtoul(e+1, &e, 0)) >= 0 && | |
752 | *e == 0) { | |
753 | snprintf(devname, sizeof(devname), "/dev/.tmp.md.%d:%d", major, minor); | |
754 | if (mknod(devname, S_IFBLK|0600, makedev(major, minor))==0) { | |
755 | fd = open(devname, flags); | |
756 | unlink(devname); | |
757 | } | |
758 | } else | |
759 | fd = open(dev, flags); | |
760 | return fd; | |
761 | } | |
762 | ||
763 | struct superswitch *superlist[] = { &super0, &super1, &super_ddf, NULL }; | |
764 | ||
765 | #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) | |
766 | struct supertype *super_by_fd(int fd) | |
767 | { | |
768 | mdu_array_info_t array; | |
769 | int vers; | |
770 | int minor; | |
771 | struct supertype *st = NULL; | |
772 | struct mdinfo *sra; | |
773 | char *verstr; | |
774 | char version[20]; | |
775 | int i; | |
776 | ||
777 | sra = sysfs_read(fd, 0, GET_VERSION); | |
778 | ||
779 | if (sra) { | |
780 | vers = sra->array.major_version; | |
781 | minor = sra->array.minor_version; | |
782 | verstr = sra->text_version; | |
783 | } else { | |
784 | if (ioctl(fd, GET_ARRAY_INFO, &array)) | |
785 | array.major_version = array.minor_version = 0; | |
786 | vers = array.major_version; | |
787 | minor = array.minor_version; | |
788 | verstr = ""; | |
789 | } | |
790 | ||
791 | if (vers != -1) { | |
792 | sprintf(version, "%d.%d", vers, minor); | |
793 | verstr = version; | |
794 | } | |
795 | for (i = 0; st == NULL && superlist[i] ; i++) | |
796 | st = superlist[i]->match_metadata_desc(verstr); | |
797 | ||
798 | if (sra) | |
799 | sysfs_free(sra); | |
800 | if (st) | |
801 | st->sb = NULL; | |
802 | return st; | |
803 | } | |
804 | #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */ | |
805 | ||
806 | ||
807 | struct supertype *dup_super(struct supertype *st) | |
808 | { | |
809 | struct supertype *stnew = NULL; | |
810 | char *verstr = NULL; | |
811 | char version[20]; | |
812 | int i; | |
813 | ||
814 | if (!st) | |
815 | return st; | |
816 | ||
817 | if (st->ss->text_version) | |
818 | strcpy(version, st->ss->text_version); | |
819 | else if (st->minor_version == -1) | |
820 | sprintf(version, "%d", st->ss->major); | |
821 | else | |
822 | sprintf(version, "%d.%d", st->ss->major, st->minor_version); | |
823 | verstr = version; | |
824 | ||
825 | for (i = 0; stnew == NULL && superlist[i] ; i++) | |
826 | stnew = superlist[i]->match_metadata_desc(verstr); | |
827 | ||
828 | if (stnew) | |
829 | stnew->sb = NULL; | |
830 | return stnew; | |
831 | } | |
832 | ||
833 | struct supertype *guess_super(int fd) | |
834 | { | |
835 | /* try each load_super to find the best match, | |
836 | * and return the best superswitch | |
837 | */ | |
838 | struct superswitch *ss; | |
839 | struct supertype *st; | |
840 | unsigned long besttime = 0; | |
841 | int bestsuper = -1; | |
842 | int i; | |
843 | ||
844 | st = malloc(sizeof(*st)); | |
845 | memset(st, 0, sizeof(*st)); | |
846 | for (i=0 ; superlist[i]; i++) { | |
847 | int rv; | |
848 | ss = superlist[i]; | |
849 | st->ss = NULL; | |
850 | rv = ss->load_super(st, fd, NULL); | |
851 | if (rv == 0) { | |
852 | struct mdinfo info; | |
853 | st->ss->getinfo_super(st, &info); | |
854 | if (bestsuper == -1 || | |
855 | besttime < info.array.ctime) { | |
856 | bestsuper = i; | |
857 | besttime = info.array.ctime; | |
858 | } | |
859 | ss->free_super(st); | |
860 | } | |
861 | } | |
862 | if (bestsuper != -1) { | |
863 | int rv; | |
864 | st->ss = NULL; | |
865 | rv = superlist[bestsuper]->load_super(st, fd, NULL); | |
866 | if (rv == 0) { | |
867 | superlist[bestsuper]->free_super(st); | |
868 | return st; | |
869 | } | |
870 | } | |
871 | free(st); | |
872 | return NULL; | |
873 | } | |
874 | ||
875 | /* Return size of device in bytes */ | |
876 | int get_dev_size(int fd, char *dname, unsigned long long *sizep) | |
877 | { | |
878 | unsigned long long ldsize; | |
879 | struct stat st; | |
880 | ||
881 | if (fstat(fd, &st) != -1 && S_ISREG(st.st_mode)) | |
882 | ldsize = (unsigned long long)st.st_size; | |
883 | else | |
884 | #ifdef BLKGETSIZE64 | |
885 | if (ioctl(fd, BLKGETSIZE64, &ldsize) != 0) | |
886 | #endif | |
887 | { | |
888 | unsigned long dsize; | |
889 | if (ioctl(fd, BLKGETSIZE, &dsize) == 0) { | |
890 | ldsize = dsize; | |
891 | ldsize <<= 9; | |
892 | } else { | |
893 | if (dname) | |
894 | fprintf(stderr, Name ": Cannot get size of %s: %s\b", | |
895 | dname, strerror(errno)); | |
896 | return 0; | |
897 | } | |
898 | } | |
899 | *sizep = ldsize; | |
900 | return 1; | |
901 | } | |
902 | ||
903 | void get_one_disk(int mdfd, mdu_array_info_t *ainf, mdu_disk_info_t *disk) | |
904 | { | |
905 | int d; | |
906 | ioctl(mdfd, GET_ARRAY_INFO, ainf); | |
907 | for (d = 0 ; d < ainf->raid_disks + ainf->nr_disks ; d++) | |
908 | if (ioctl(mdfd, GET_DISK_INFO, disk) == 0) | |
909 | return; | |
910 | } | |
911 | ||
912 | int open_container(int fd) | |
913 | { | |
914 | /* 'fd' is a block device. Find out if it is in use | |
915 | * by a container, and return an open fd on that container. | |
916 | */ | |
917 | char path[256]; | |
918 | char *e; | |
919 | DIR *dir; | |
920 | struct dirent *de; | |
921 | int dfd, n; | |
922 | char buf[200]; | |
923 | int major, minor; | |
924 | struct stat st; | |
925 | ||
926 | if (fstat(fd, &st) != 0) | |
927 | return -1; | |
928 | sprintf(path, "/sys/dev/block/%d:%d/holders", | |
929 | (int)major(st.st_rdev), (int)minor(st.st_rdev)); | |
930 | e = path + strlen(path); | |
931 | ||
932 | dir = opendir(path); | |
933 | if (!dir) | |
934 | return -1; | |
935 | while ((de = readdir(dir))) { | |
936 | if (de->d_ino == 0) | |
937 | continue; | |
938 | if (de->d_name[0] == '.') | |
939 | continue; | |
940 | sprintf(e, "/%s/dev", de->d_name); | |
941 | dfd = open(path, O_RDONLY); | |
942 | if (dfd < 0) | |
943 | continue; | |
944 | n = read(dfd, buf, sizeof(buf)); | |
945 | close(dfd); | |
946 | if (n <= 0 || n >= sizeof(buf)) | |
947 | continue; | |
948 | buf[n] = 0; | |
949 | if (sscanf(buf, "%d:%d", &major, &minor) != 2) | |
950 | continue; | |
951 | sprintf(buf, "%d:%d", major, minor); | |
952 | dfd = dev_open(buf, O_RDONLY); | |
953 | if (dfd >= 0) { | |
954 | closedir(dir); | |
955 | return dfd; | |
956 | } | |
957 | } | |
958 | return -1; | |
959 | } | |
960 | ||
961 | #ifdef __TINYC__ | |
962 | /* tinyc doesn't optimize this check in ioctl.h out ... */ | |
963 | unsigned int __invalid_size_argument_for_IOC = 0; | |
964 | #endif | |
965 |