]>
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 | ||
35 | /* | |
36 | * following taken from linux/blkpg.h because they aren't | |
37 | * anywhere else and it isn't safe to #include linux/ * stuff. | |
38 | */ | |
39 | ||
40 | #define BLKPG _IO(0x12,105) | |
41 | ||
42 | /* The argument structure */ | |
43 | struct blkpg_ioctl_arg { | |
44 | int op; | |
45 | int flags; | |
46 | int datalen; | |
47 | void *data; | |
48 | }; | |
49 | ||
50 | /* The subfunctions (for the op field) */ | |
51 | #define BLKPG_ADD_PARTITION 1 | |
52 | #define BLKPG_DEL_PARTITION 2 | |
53 | ||
54 | /* Sizes of name fields. Unused at present. */ | |
55 | #define BLKPG_DEVNAMELTH 64 | |
56 | #define BLKPG_VOLNAMELTH 64 | |
57 | ||
58 | /* The data structure for ADD_PARTITION and DEL_PARTITION */ | |
59 | struct blkpg_partition { | |
60 | long long start; /* starting offset in bytes */ | |
61 | long long length; /* length in bytes */ | |
62 | int pno; /* partition number */ | |
63 | char devname[BLKPG_DEVNAMELTH]; /* partition name, like sda5 or c0d1p2, | |
64 | to be used in kernel messages */ | |
65 | char volname[BLKPG_VOLNAMELTH]; /* volume label */ | |
66 | }; | |
67 | ||
68 | /* | |
69 | * Parse a 128 bit uuid in 4 integers | |
70 | * format is 32 hexx nibbles with options :.<space> separator | |
71 | * If not exactly 32 hex digits are found, return 0 | |
72 | * else return 1 | |
73 | */ | |
74 | int parse_uuid(char *str, int uuid[4]) | |
75 | { | |
76 | int hit = 0; /* number of Hex digIT */ | |
77 | int i; | |
78 | char c; | |
79 | for (i=0; i<4; i++) uuid[i]=0; | |
80 | ||
81 | while ((c= *str++)) { | |
82 | int n; | |
83 | if (c>='0' && c<='9') | |
84 | n = c-'0'; | |
85 | else if (c>='a' && c <= 'f') | |
86 | n = 10 + c - 'a'; | |
87 | else if (c>='A' && c <= 'F') | |
88 | n = 10 + c - 'A'; | |
89 | else if (strchr(":. -", c)) | |
90 | continue; | |
91 | else return 0; | |
92 | ||
93 | if (hit<32) { | |
94 | uuid[hit/8] <<= 4; | |
95 | uuid[hit/8] += n; | |
96 | } | |
97 | hit++; | |
98 | } | |
99 | if (hit == 32) | |
100 | return 1; | |
101 | return 0; | |
102 | ||
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 | ||
136 | int get_linux_version() | |
137 | { | |
138 | struct utsname name; | |
139 | char *cp; | |
140 | int a,b,c; | |
141 | if (uname(&name) <0) | |
142 | return -1; | |
143 | ||
144 | cp = name.release; | |
145 | a = strtoul(cp, &cp, 10); | |
146 | if (*cp != '.') return -1; | |
147 | b = strtoul(cp+1, &cp, 10); | |
148 | if (*cp != '.') return -1; | |
149 | c = strtoul(cp+1, NULL, 10); | |
150 | ||
151 | return (a*1000000)+(b*1000)+c; | |
152 | } | |
153 | ||
154 | void remove_partitions(int fd) | |
155 | { | |
156 | /* remove partitions from this block devices. | |
157 | * This is used for components added to an array | |
158 | */ | |
159 | #ifdef BLKPG_DEL_PARTITION | |
160 | struct blkpg_ioctl_arg a; | |
161 | struct blkpg_partition p; | |
162 | ||
163 | a.op = BLKPG_DEL_PARTITION; | |
164 | a.data = (void*)&p; | |
165 | a.datalen = sizeof(p); | |
166 | a.flags = 0; | |
167 | memset(a.data, 0, a.datalen); | |
168 | for (p.pno=0; p.pno < 16; p.pno++) | |
169 | ioctl(fd, BLKPG, &a); | |
170 | #endif | |
171 | } | |
172 | ||
173 | int enough(int level, int raid_disks, int layout, | |
174 | char *avail, int avail_disks) | |
175 | { | |
176 | int copies, first; | |
177 | switch (level) { | |
178 | case 10: | |
179 | /* This is the tricky one - we need to check | |
180 | * which actual disks are present. | |
181 | */ | |
182 | copies = (layout&255)* ((layout>>8) & 255); | |
183 | first=0; | |
184 | do { | |
185 | /* there must be one of the 'copies' form 'first' */ | |
186 | int n = copies; | |
187 | int cnt=0; | |
188 | while (n--) { | |
189 | if (avail[first]) | |
190 | cnt++; | |
191 | first = (first+1) % raid_disks; | |
192 | } | |
193 | if (cnt == 0) | |
194 | return 0; | |
195 | ||
196 | } while (first != 0); | |
197 | return 1; | |
198 | ||
199 | case -4: | |
200 | return avail_disks>= 1; | |
201 | case -1: | |
202 | case 0: | |
203 | return avail_disks == raid_disks; | |
204 | case 1: | |
205 | return avail_disks >= 1; | |
206 | case 4: | |
207 | case 5: | |
208 | return avail_disks >= raid_disks-1; | |
209 | case 6: | |
210 | return avail_disks >= raid_disks-2; | |
211 | default: | |
212 | return 0; | |
213 | } | |
214 | } | |
215 | ||
216 | int same_uuid(int a[4], int b[4], int swapuuid) | |
217 | { | |
218 | if (swapuuid) { | |
219 | /* parse uuids are hostendian. | |
220 | * uuid's from some superblocks are big-ending | |
221 | * if there is a difference, we need to swap.. | |
222 | */ | |
223 | unsigned char *ac = (unsigned char *)a; | |
224 | unsigned char *bc = (unsigned char *)b; | |
225 | int i; | |
226 | for (i=0; i<16; i+= 4) { | |
227 | if (ac[i+0] != bc[i+3] || | |
228 | ac[i+1] != bc[i+2] || | |
229 | ac[i+2] != bc[i+1] || | |
230 | ac[i+3] != bc[i+0]) | |
231 | return 0; | |
232 | } | |
233 | return 1; | |
234 | } else { | |
235 | if (a[0]==b[0] && | |
236 | a[1]==b[1] && | |
237 | a[2]==b[2] && | |
238 | a[3]==b[3]) | |
239 | return 1; | |
240 | return 0; | |
241 | } | |
242 | } | |
243 | ||
244 | #ifndef MDASSEMBLE | |
245 | int check_ext2(int fd, char *name) | |
246 | { | |
247 | /* | |
248 | * Check for an ext2fs file system. | |
249 | * Superblock is always 1K at 1K offset | |
250 | * | |
251 | * s_magic is le16 at 56 == 0xEF53 | |
252 | * report mtime - le32 at 44 | |
253 | * blocks - le32 at 4 | |
254 | * logblksize - le32 at 24 | |
255 | */ | |
256 | unsigned char sb[1024]; | |
257 | time_t mtime; | |
258 | int size, bsize; | |
259 | if (lseek(fd, 1024,0)!= 1024) | |
260 | return 0; | |
261 | if (read(fd, sb, 1024)!= 1024) | |
262 | return 0; | |
263 | if (sb[56] != 0x53 || sb[57] != 0xef) | |
264 | return 0; | |
265 | ||
266 | mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8; | |
267 | bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8; | |
268 | size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8; | |
269 | fprintf(stderr, Name ": %s appears to contain an ext2fs file system\n", | |
270 | name); | |
271 | fprintf(stderr," size=%dK mtime=%s", | |
272 | size*(1<<bsize), ctime(&mtime)); | |
273 | return 1; | |
274 | } | |
275 | ||
276 | int check_reiser(int fd, char *name) | |
277 | { | |
278 | /* | |
279 | * superblock is at 64K | |
280 | * size is 1024; | |
281 | * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52 | |
282 | * | |
283 | */ | |
284 | unsigned char sb[1024]; | |
285 | unsigned long size; | |
286 | if (lseek(fd, 64*1024, 0) != 64*1024) | |
287 | return 0; | |
288 | if (read(fd, sb, 1024) != 1024) | |
289 | return 0; | |
290 | if (strncmp((char*)sb+52, "ReIsErFs",8)!=0 && | |
291 | strncmp((char*)sb+52, "ReIsEr2Fs",9)!=0) | |
292 | return 0; | |
293 | fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name); | |
294 | size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8; | |
295 | fprintf(stderr, " size = %luK\n", size*4); | |
296 | ||
297 | return 1; | |
298 | } | |
299 | ||
300 | int check_raid(int fd, char *name) | |
301 | { | |
302 | void *super; | |
303 | struct mdinfo info; | |
304 | time_t crtime; | |
305 | char *level; | |
306 | struct supertype *st = guess_super(fd); | |
307 | ||
308 | if (!st) return 0; | |
309 | st->ss->load_super(st, fd, &super, name); | |
310 | /* Looks like a raid array .. */ | |
311 | fprintf(stderr, Name ": %s appears to be part of a raid array:\n", | |
312 | name); | |
313 | st->ss->getinfo_super(&info, super); | |
314 | free(super); | |
315 | crtime = info.array.ctime; | |
316 | level = map_num(pers, info.array.level); | |
317 | if (!level) level = "-unknown-"; | |
318 | fprintf(stderr, " level=%s devices=%d ctime=%s", | |
319 | level, info.array.raid_disks, ctime(&crtime)); | |
320 | return 1; | |
321 | } | |
322 | ||
323 | int ask(char *mesg) | |
324 | { | |
325 | char *add = ""; | |
326 | int i; | |
327 | for (i=0; i<5; i++) { | |
328 | char buf[100]; | |
329 | fprintf(stderr, "%s%s", mesg, add); | |
330 | fflush(stderr); | |
331 | if (fgets(buf, 100, stdin)==NULL) | |
332 | return 0; | |
333 | if (buf[0]=='y' || buf[0]=='Y') | |
334 | return 1; | |
335 | if (buf[0]=='n' || buf[0]=='N') | |
336 | return 0; | |
337 | add = "(y/n) "; | |
338 | } | |
339 | fprintf(stderr, Name ": assuming 'no'\n"); | |
340 | return 0; | |
341 | } | |
342 | #endif /* MDASSEMBLE */ | |
343 | ||
344 | char *map_num(mapping_t *map, int num) | |
345 | { | |
346 | while (map->name) { | |
347 | if (map->num == num) | |
348 | return map->name; | |
349 | map++; | |
350 | } | |
351 | return NULL; | |
352 | } | |
353 | ||
354 | int map_name(mapping_t *map, char *name) | |
355 | { | |
356 | while (map->name) { | |
357 | if (strcmp(map->name, name)==0) | |
358 | return map->num; | |
359 | map++; | |
360 | } | |
361 | return UnSet; | |
362 | } | |
363 | ||
364 | ||
365 | int is_standard(char *dev, int *nump) | |
366 | { | |
367 | /* tests if dev is a "standard" md dev name. | |
368 | * i.e if the last component is "/dNN" or "/mdNN", | |
369 | * where NN is a string of digits | |
370 | */ | |
371 | char *d = strrchr(dev, '/'); | |
372 | int type=0; | |
373 | int num; | |
374 | if (!d) | |
375 | return 0; | |
376 | if (strncmp(d, "/d",2)==0) | |
377 | d += 2, type=1; /* /dev/md/dN{pM} */ | |
378 | else if (strncmp(d, "/md_d", 5)==0) | |
379 | d += 5, type=1; /* /dev/md_dNpM */ | |
380 | else if (strncmp(d, "/md", 3)==0) | |
381 | d += 3, type=-1; /* /dev/mdN */ | |
382 | else if (d-dev > 3 && strncmp(d-2, "md/", 3)==0) | |
383 | d += 1, type=-1; /* /dev/md/N */ | |
384 | else | |
385 | return 0; | |
386 | if (!*d) | |
387 | return 0; | |
388 | num = atoi(d); | |
389 | while (isdigit(*d)) | |
390 | d++; | |
391 | if (*d) | |
392 | return 0; | |
393 | if (nump) *nump = num; | |
394 | ||
395 | return type; | |
396 | } | |
397 | ||
398 | ||
399 | /* | |
400 | * convert a major/minor pair for a block device into a name in /dev, if possible. | |
401 | * On the first call, walk /dev collecting name. | |
402 | * Put them in a simple linked listfor now. | |
403 | */ | |
404 | struct devmap { | |
405 | int major, minor; | |
406 | char *name; | |
407 | struct devmap *next; | |
408 | } *devlist = NULL; | |
409 | int devlist_ready = 0; | |
410 | ||
411 | int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s) | |
412 | { | |
413 | struct stat st; | |
414 | if (S_ISLNK(stb->st_mode)) { | |
415 | stat(name, &st); | |
416 | stb = &st; | |
417 | } | |
418 | ||
419 | if ((stb->st_mode&S_IFMT)== S_IFBLK) { | |
420 | char *n = strdup(name); | |
421 | struct devmap *dm = malloc(sizeof(*dm)); | |
422 | if (strncmp(n, "/dev/./", 7)==0) | |
423 | strcpy(n+4, name+6); | |
424 | if (dm) { | |
425 | dm->major = major(stb->st_rdev); | |
426 | dm->minor = minor(stb->st_rdev); | |
427 | dm->name = n; | |
428 | dm->next = devlist; | |
429 | devlist = dm; | |
430 | } | |
431 | } | |
432 | return 0; | |
433 | } | |
434 | ||
435 | #ifndef HAVE_NFTW | |
436 | #ifdef HAVE_FTW | |
437 | int add_dev_1(const char *name, const struct stat *stb, int flag) | |
438 | { | |
439 | return add_dev(name, stb, flag, NULL); | |
440 | } | |
441 | int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags) | |
442 | { | |
443 | return ftw(path, add_dev_1, nopenfd); | |
444 | } | |
445 | #else | |
446 | int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s) | |
447 | { | |
448 | return 0; | |
449 | } | |
450 | int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags) | |
451 | { | |
452 | return 0; | |
453 | } | |
454 | #endif /* HAVE_FTW */ | |
455 | #endif /* HAVE_NFTW */ | |
456 | ||
457 | /* | |
458 | * Find a block device with the right major/minor number. | |
459 | * If we find multiple names, choose the shortest. | |
460 | * If we find a non-standard name, it is probably there | |
461 | * deliberately so prefer it over a standard name. | |
462 | * This applies only to names for MD devices. | |
463 | */ | |
464 | char *map_dev(int major, int minor, int create) | |
465 | { | |
466 | struct devmap *p; | |
467 | char *std = NULL, *nonstd=NULL; | |
468 | int did_check = 0; | |
469 | ||
470 | if (major == 0 && minor == 0) | |
471 | return NULL; | |
472 | ||
473 | retry: | |
474 | if (!devlist_ready) { | |
475 | char *dev = "/dev"; | |
476 | struct stat stb; | |
477 | while(devlist) { | |
478 | struct devmap *d = devlist; | |
479 | devlist = d->next; | |
480 | free(d->name); | |
481 | free(d); | |
482 | } | |
483 | if (lstat(dev, &stb)==0 && | |
484 | S_ISLNK(stb.st_mode)) | |
485 | dev = "/dev/."; | |
486 | nftw(dev, add_dev, 10, FTW_PHYS); | |
487 | devlist_ready=1; | |
488 | did_check = 1; | |
489 | } | |
490 | ||
491 | for (p=devlist; p; p=p->next) | |
492 | if (p->major == major && | |
493 | p->minor == minor) { | |
494 | if (is_standard(p->name, NULL)) { | |
495 | if (std == NULL || | |
496 | strlen(p->name) < strlen(std)) | |
497 | std = p->name; | |
498 | } else { | |
499 | if (nonstd == NULL || | |
500 | strlen(p->name) < strlen(nonstd)) | |
501 | nonstd = p->name; | |
502 | } | |
503 | } | |
504 | if (!std && !nonstd && !did_check) { | |
505 | devlist_ready = 0; | |
506 | goto retry; | |
507 | } | |
508 | if (create && !std && !nonstd) { | |
509 | static char buf[30]; | |
510 | snprintf(buf, sizeof(buf), "%d:%d", major, minor); | |
511 | nonstd = buf; | |
512 | } | |
513 | ||
514 | return nonstd ? nonstd : std; | |
515 | } | |
516 | ||
517 | unsigned long calc_csum(void *super, int bytes) | |
518 | { | |
519 | unsigned long long newcsum = 0; | |
520 | int i; | |
521 | unsigned int csum; | |
522 | unsigned int *superc = (unsigned int*) super; | |
523 | ||
524 | for(i=0; i<bytes/4; i++) | |
525 | newcsum+= superc[i]; | |
526 | csum = (newcsum& 0xffffffff) + (newcsum>>32); | |
527 | #ifdef __alpha__ | |
528 | /* The in-kernel checksum calculation is always 16bit on | |
529 | * the alpha, though it is 32 bit on i386... | |
530 | * I wonder what it is elsewhere... (it uses and API in | |
531 | * a way that it shouldn't). | |
532 | */ | |
533 | csum = (csum & 0xffff) + (csum >> 16); | |
534 | csum = (csum & 0xffff) + (csum >> 16); | |
535 | #endif | |
536 | return csum; | |
537 | } | |
538 | ||
539 | #ifndef MDASSEMBLE | |
540 | char *human_size(long long bytes) | |
541 | { | |
542 | static char buf[30]; | |
543 | ||
544 | /* We convert bytes to either centi-M{ega,ibi}bytes or | |
545 | * centi-G{igi,ibi}bytes, with appropriate rounding, | |
546 | * and then print 1/100th of those as a decimal. | |
547 | * We allow upto 2048Megabytes before converting to | |
548 | * gigabytes, as that shows more precision and isn't | |
549 | * too large a number. | |
550 | * Terrabytes are not yet handled. | |
551 | */ | |
552 | ||
553 | if (bytes < 5000*1024) | |
554 | buf[0]=0; | |
555 | else if (bytes < 2*1024LL*1024LL*1024LL) { | |
556 | long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2; | |
557 | long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2; | |
558 | snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)", | |
559 | cMiB/100 , cMiB % 100, | |
560 | cMB/100, cMB % 100); | |
561 | } else { | |
562 | long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2; | |
563 | long cGB = (bytes / (1000000000LL/200LL ) +1) /2; | |
564 | snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)", | |
565 | cGiB/100 , cGiB % 100, | |
566 | cGB/100, cGB % 100); | |
567 | } | |
568 | return buf; | |
569 | } | |
570 | ||
571 | char *human_size_brief(long long bytes) | |
572 | { | |
573 | static char buf[30]; | |
574 | ||
575 | ||
576 | if (bytes < 5000*1024) | |
577 | snprintf(buf, sizeof(buf), "%ld.%02ldKiB", | |
578 | (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024) | |
579 | ); | |
580 | else if (bytes < 2*1024LL*1024LL*1024LL) | |
581 | snprintf(buf, sizeof(buf), "%ld.%02ldMiB", | |
582 | (long)(bytes>>20), | |
583 | (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100) | |
584 | ); | |
585 | else | |
586 | snprintf(buf, sizeof(buf), "%ld.%02ldGiB", | |
587 | (long)(bytes>>30), | |
588 | (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100) | |
589 | ); | |
590 | return buf; | |
591 | } | |
592 | #endif | |
593 | ||
594 | #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) | |
595 | int get_mdp_major(void) | |
596 | { | |
597 | static int mdp_major = -1; | |
598 | FILE *fl; | |
599 | char *w; | |
600 | int have_block = 0; | |
601 | int have_devices = 0; | |
602 | int last_num = -1; | |
603 | ||
604 | if (mdp_major != -1) | |
605 | return mdp_major; | |
606 | fl = fopen("/proc/devices", "r"); | |
607 | if (!fl) | |
608 | return -1; | |
609 | while ((w = conf_word(fl, 1))) { | |
610 | if (have_block && strcmp(w, "devices:")==0) | |
611 | have_devices = 1; | |
612 | have_block = (strcmp(w, "Block")==0); | |
613 | if (isdigit(w[0])) | |
614 | last_num = atoi(w); | |
615 | if (have_devices && strcmp(w, "mdp")==0) | |
616 | mdp_major = last_num; | |
617 | free(w); | |
618 | } | |
619 | fclose(fl); | |
620 | return mdp_major; | |
621 | } | |
622 | ||
623 | ||
624 | ||
625 | char *get_md_name(int dev) | |
626 | { | |
627 | /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */ | |
628 | /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */ | |
629 | static char devname[50]; | |
630 | struct stat stb; | |
631 | dev_t rdev; | |
632 | char *dn; | |
633 | ||
634 | if (dev < 0) { | |
635 | int mdp = get_mdp_major(); | |
636 | if (mdp < 0) return NULL; | |
637 | rdev = makedev(mdp, (-1-dev)<<6); | |
638 | snprintf(devname, sizeof(devname), "/dev/md/d%d", -1-dev); | |
639 | if (stat(devname, &stb) == 0 | |
640 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
641 | && (stb.st_rdev == rdev)) | |
642 | return devname; | |
643 | } else { | |
644 | rdev = makedev(MD_MAJOR, dev); | |
645 | snprintf(devname, sizeof(devname), "/dev/md%d", dev); | |
646 | if (stat(devname, &stb) == 0 | |
647 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
648 | && (stb.st_rdev == rdev)) | |
649 | return devname; | |
650 | ||
651 | snprintf(devname, sizeof(devname), "/dev/md/%d", dev); | |
652 | if (stat(devname, &stb) == 0 | |
653 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
654 | && (stb.st_rdev == rdev)) | |
655 | return devname; | |
656 | } | |
657 | dn = map_dev(major(rdev), minor(rdev), 0); | |
658 | if (dn) | |
659 | return dn; | |
660 | snprintf(devname, sizeof(devname), "/dev/.tmp.md%d", dev); | |
661 | if (mknod(devname, S_IFBLK | 0600, rdev) == -1) | |
662 | if (errno != EEXIST) | |
663 | return NULL; | |
664 | ||
665 | if (stat(devname, &stb) == 0 | |
666 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
667 | && (stb.st_rdev == rdev)) | |
668 | return devname; | |
669 | unlink(devname); | |
670 | return NULL; | |
671 | } | |
672 | ||
673 | void put_md_name(char *name) | |
674 | { | |
675 | if (strncmp(name, "/dev/.tmp.md", 12)==0) | |
676 | unlink(name); | |
677 | } | |
678 | #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */ | |
679 | ||
680 | int dev_open(char *dev, int flags) | |
681 | { | |
682 | /* like 'open', but if 'dev' matches %d:%d, create a temp | |
683 | * block device and open that | |
684 | */ | |
685 | char *e; | |
686 | int fd = -1; | |
687 | char devname[32]; | |
688 | int major; | |
689 | int minor; | |
690 | ||
691 | if (!dev) return -1; | |
692 | ||
693 | major = strtoul(dev, &e, 0); | |
694 | if (e > dev && *e == ':' && e[1] && | |
695 | (minor = strtoul(e+1, &e, 0)) >= 0 && | |
696 | *e == 0) { | |
697 | snprintf(devname, sizeof(devname), "/dev/.tmp.md.%d:%d", major, minor); | |
698 | if (mknod(devname, S_IFBLK|0600, makedev(major, minor))==0) { | |
699 | fd = open(devname, flags); | |
700 | unlink(devname); | |
701 | } | |
702 | } else | |
703 | fd = open(dev, flags); | |
704 | return fd; | |
705 | } | |
706 | ||
707 | struct superswitch *superlist[] = { &super0, &super1, NULL }; | |
708 | ||
709 | struct supertype *super_by_version(int vers, int minor) | |
710 | { | |
711 | struct supertype *st = malloc(sizeof(*st)); | |
712 | if (!st) return st; | |
713 | if (vers == 0) { | |
714 | st->ss = &super0; | |
715 | st->max_devs = MD_SB_DISKS; | |
716 | } | |
717 | ||
718 | if (vers == 1) { | |
719 | st->ss = &super1; | |
720 | st->max_devs = 384; | |
721 | } | |
722 | st->minor_version = minor; | |
723 | return st; | |
724 | } | |
725 | ||
726 | struct supertype *guess_super(int fd) | |
727 | { | |
728 | /* try each load_super to find the best match, | |
729 | * and return the best superswitch | |
730 | */ | |
731 | struct superswitch *ss; | |
732 | struct supertype *st; | |
733 | unsigned long besttime = 0; | |
734 | int bestsuper = -1; | |
735 | ||
736 | void *sbp = NULL; | |
737 | int i; | |
738 | ||
739 | st = malloc(sizeof(*st)); | |
740 | memset(st, 0, sizeof(*st)); | |
741 | for (i=0 ; superlist[i]; i++) { | |
742 | int rv; | |
743 | ss = superlist[i]; | |
744 | st->ss = NULL; | |
745 | rv = ss->load_super(st, fd, &sbp, NULL); | |
746 | if (rv == 0) { | |
747 | struct mdinfo info; | |
748 | ss->getinfo_super(&info, sbp); | |
749 | if (bestsuper == -1 || | |
750 | besttime < info.array.ctime) { | |
751 | bestsuper = i; | |
752 | besttime = info.array.ctime; | |
753 | } | |
754 | free(sbp); | |
755 | } | |
756 | } | |
757 | if (bestsuper != -1) { | |
758 | int rv; | |
759 | st->ss = NULL; | |
760 | rv = superlist[bestsuper]->load_super(st, fd, &sbp, NULL); | |
761 | if (rv == 0) { | |
762 | free(sbp); | |
763 | return st; | |
764 | } | |
765 | } | |
766 | free(st); | |
767 | return NULL; | |
768 | } | |
769 | ||
770 | ||
771 | #ifdef __TINYC__ | |
772 | /* tinyc doesn't optimize this check in ioctl.h out ... */ | |
773 | unsigned int __invalid_size_argument_for_IOC = 0; | |
774 | #endif | |
775 |