]>
Commit | Line | Data |
---|---|---|
64c4757e | 1 | /* |
9a9dab36 | 2 | * mdadm - manage Linux "md" devices aka RAID arrays. |
64c4757e | 3 | * |
cd29a5c8 | 4 | * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au> |
64c4757e NB |
5 | * |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | * Author: Neil Brown | |
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 | ||
9a9dab36 | 30 | #include "mdadm.h" |
64c4757e NB |
31 | #include "md_p.h" |
32 | #include <sys/utsname.h> | |
98c6faba | 33 | #include <ctype.h> |
64c4757e NB |
34 | |
35 | /* | |
36 | * Parse a 128 bit uuid in 4 integers | |
37 | * format is 32 hexx nibbles with options :.<space> separator | |
38 | * If not exactly 32 hex digits are found, return 0 | |
39 | * else return 1 | |
40 | */ | |
41 | int parse_uuid(char *str, int uuid[4]) | |
42 | { | |
43 | int hit = 0; /* number of Hex digIT */ | |
44 | int i; | |
45 | char c; | |
46 | for (i=0; i<4; i++) uuid[i]=0; | |
47 | ||
48 | while ((c= *str++)) { | |
49 | int n; | |
50 | if (c>='0' && c<='9') | |
51 | n = c-'0'; | |
52 | else if (c>='a' && c <= 'f') | |
53 | n = 10 + c - 'a'; | |
54 | else if (c>='A' && c <= 'F') | |
55 | n = 10 + c - 'A'; | |
56 | else if (strchr(":. -", c)) | |
57 | continue; | |
58 | else return 0; | |
59 | ||
82b27616 NB |
60 | if (hit<32) { |
61 | uuid[hit/8] <<= 4; | |
62 | uuid[hit/8] += n; | |
63 | } | |
64c4757e NB |
64 | hit++; |
65 | } | |
66 | if (hit == 32) | |
67 | return 1; | |
68 | return 0; | |
69 | ||
70 | } | |
71 | ||
72 | ||
73 | /* | |
74 | * Get the md version number. | |
75 | * We use the RAID_VERSION ioctl if it is supported | |
76 | * If not, but we have a block device with major '9', we assume | |
77 | * 0.36.0 | |
78 | * | |
79 | * Return version number as 24 but number - assume version parts | |
80 | * always < 255 | |
81 | */ | |
82 | ||
83 | int md_get_version(int fd) | |
84 | { | |
85 | struct stat stb; | |
86 | mdu_version_t vers; | |
87 | ||
88 | if (fstat(fd, &stb)<0) | |
89 | return -1; | |
90 | if ((S_IFMT&stb.st_mode) != S_IFBLK) | |
91 | return -1; | |
92 | ||
93 | if (ioctl(fd, RAID_VERSION, &vers) == 0) | |
682c7051 | 94 | return (vers.major*10000) + (vers.minor*100) + vers.patchlevel; |
5787fa49 NB |
95 | if (errno == EACCES) |
96 | return -1; | |
0df46c2a | 97 | if (major(stb.st_rdev) == MD_MAJOR) |
682c7051 | 98 | return (3600); |
64c4757e NB |
99 | return -1; |
100 | } | |
101 | ||
102 | ||
103 | int get_linux_version() | |
104 | { | |
105 | struct utsname name; | |
98c6faba | 106 | char *cp; |
64c4757e NB |
107 | int a,b,c; |
108 | if (uname(&name) <0) | |
109 | return -1; | |
110 | ||
98c6faba NB |
111 | cp = name.release; |
112 | a = strtoul(cp, &cp, 10); | |
113 | if (*cp != '.') return -1; | |
114 | b = strtoul(cp+1, &cp, 10); | |
115 | if (*cp != '.') return -1; | |
116 | c = strtoul(cp+1, NULL, 10); | |
117 | ||
682c7051 | 118 | return (a*1000000)+(b*1000)+c; |
64c4757e NB |
119 | } |
120 | ||
121 | int enough(int level, int raid_disks, int avail_disks) | |
122 | { | |
123 | switch (level) { | |
e5329c37 NB |
124 | case 10: return 1; /* a lie, but it is hard to tell */ |
125 | ||
e0d19036 NB |
126 | case -4: |
127 | return avail_disks>= 1; | |
64c4757e NB |
128 | case -1: |
129 | case 0: | |
130 | return avail_disks == raid_disks; | |
131 | case 1: | |
132 | return avail_disks >= 1; | |
133 | case 4: | |
134 | case 5: | |
135 | return avail_disks >= raid_disks-1; | |
98c6faba NB |
136 | case 6: |
137 | return avail_disks >= raid_disks-2; | |
64c4757e NB |
138 | default: |
139 | return 0; | |
140 | } | |
141 | } | |
142 | ||
f277ce36 | 143 | int same_uuid(int a[4], int b[4], int swapuuid) |
64c4757e | 144 | { |
f277ce36 NB |
145 | if (swapuuid) { |
146 | /* parse uuids are hostendian. | |
147 | * uuid's from some superblocks are big-ending | |
148 | * if there is a difference, we need to swap.. | |
149 | */ | |
150 | unsigned char *ac = (unsigned char *)a; | |
151 | unsigned char *bc = (unsigned char *)b; | |
152 | int i; | |
153 | for (i=0; i<16; i+= 4) { | |
154 | if (ac[i+0] != bc[i+3] || | |
155 | ac[i+1] != bc[i+2] || | |
156 | ac[i+2] != bc[i+1] || | |
157 | ac[i+3] != bc[i+0]) | |
158 | return 0; | |
159 | } | |
160 | return 1; | |
161 | } else { | |
162 | if (a[0]==b[0] && | |
163 | a[1]==b[1] && | |
164 | a[2]==b[2] && | |
165 | a[3]==b[3]) | |
166 | return 1; | |
167 | return 0; | |
168 | } | |
64c4757e NB |
169 | } |
170 | ||
682c7051 NB |
171 | int check_ext2(int fd, char *name) |
172 | { | |
173 | /* | |
174 | * Check for an ext2fs file system. | |
175 | * Superblock is always 1K at 1K offset | |
176 | * | |
177 | * s_magic is le16 at 56 == 0xEF53 | |
178 | * report mtime - le32 at 44 | |
179 | * blocks - le32 at 4 | |
180 | * logblksize - le32 at 24 | |
181 | */ | |
182 | unsigned char sb[1024]; | |
183 | time_t mtime; | |
184 | int size, bsize; | |
185 | if (lseek(fd, 1024,0)!= 1024) | |
186 | return 0; | |
187 | if (read(fd, sb, 1024)!= 1024) | |
188 | return 0; | |
189 | if (sb[56] != 0x53 || sb[57] != 0xef) | |
190 | return 0; | |
191 | ||
192 | mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8; | |
193 | bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8; | |
194 | size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8; | |
195 | fprintf(stderr, Name ": %s appears to contain an ext2fs file system\n", | |
196 | name); | |
197 | fprintf(stderr," size=%dK mtime=%s", | |
198 | size*(1<<bsize), ctime(&mtime)); | |
199 | return 1; | |
200 | } | |
201 | ||
202 | int check_reiser(int fd, char *name) | |
203 | { | |
204 | /* | |
205 | * superblock is at 64K | |
206 | * size is 1024; | |
207 | * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52 | |
208 | * | |
209 | */ | |
210 | unsigned char sb[1024]; | |
211 | int size; | |
212 | if (lseek(fd, 64*1024, 0) != 64*1024) | |
213 | return 0; | |
214 | if (read(fd, sb, 1024) != 1024) | |
215 | return 0; | |
a46f4061 NB |
216 | if (strncmp((char*)sb+52, "ReIsErFs",8)!=0 && |
217 | strncmp((char*)sb+52, "ReIsEr2Fs",9)!=0) | |
682c7051 NB |
218 | return 0; |
219 | fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name); | |
220 | size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8; | |
221 | fprintf(stderr, " size = %dK\n", size*4); | |
222 | ||
223 | return 1; | |
224 | } | |
225 | ||
226 | int check_raid(int fd, char *name) | |
227 | { | |
4b1ac34b NB |
228 | void *super; |
229 | struct mdinfo info; | |
682c7051 | 230 | time_t crtime; |
82d9eba6 | 231 | struct supertype *st = guess_super(fd); |
f9ce90ba | 232 | |
82d9eba6 NB |
233 | if (!st) return 0; |
234 | st->ss->load_super(st, fd, &super, name); | |
235 | /* Looks like a raid array .. */ | |
236 | fprintf(stderr, Name ": %s appears to be part of a raid array:\n", | |
237 | name); | |
238 | st->ss->getinfo_super(&info, super); | |
239 | free(super); | |
240 | crtime = info.array.ctime; | |
241 | fprintf(stderr, " level=%d devices=%d ctime=%s", | |
242 | info.array.level, info.array.raid_disks, ctime(&crtime)); | |
243 | return 1; | |
682c7051 NB |
244 | } |
245 | ||
682c7051 NB |
246 | int ask(char *mesg) |
247 | { | |
248 | char *add = ""; | |
249 | int i; | |
250 | for (i=0; i<5; i++) { | |
251 | char buf[100]; | |
252 | fprintf(stderr, "%s%s", mesg, add); | |
253 | fflush(stderr); | |
254 | if (fgets(buf, 100, stdin)==NULL) | |
255 | return 0; | |
256 | if (buf[0]=='y' || buf[0]=='Y') | |
257 | return 1; | |
258 | if (buf[0]=='n' || buf[0]=='N') | |
259 | return 0; | |
260 | add = "(y/n) "; | |
261 | } | |
262 | fprintf(stderr, Name ": assuming 'no'\n"); | |
263 | return 0; | |
264 | } | |
265 | ||
266 | char *map_num(mapping_t *map, int num) | |
267 | { | |
268 | while (map->name) { | |
269 | if (map->num == num) | |
270 | return map->name; | |
271 | map++; | |
272 | } | |
273 | return NULL; | |
274 | } | |
275 | ||
276 | int map_name(mapping_t *map, char *name) | |
277 | { | |
278 | while (map->name) { | |
279 | if (strcmp(map->name, name)==0) | |
280 | return map->num; | |
281 | map++; | |
282 | } | |
98c6faba | 283 | return UnSet; |
682c7051 | 284 | } |
82b27616 | 285 | |
e5329c37 | 286 | |
8d80900b | 287 | int is_standard(char *dev, int *nump) |
e5329c37 NB |
288 | { |
289 | /* tests if dev is a "standard" md dev name. | |
290 | * i.e if the last component is "/dNN" or "/mdNN", | |
291 | * where NN is a string of digits | |
292 | */ | |
8d80900b NB |
293 | char *d = strrchr(dev, '/'); |
294 | int type=0; | |
295 | int num; | |
296 | if (!d) | |
e5329c37 | 297 | return 0; |
8d80900b NB |
298 | if (strncmp(d, "/d",2)==0) |
299 | d += 2, type=1; /* /dev/md/dN{pM} */ | |
300 | else if (strncmp(d, "/md_d", 5)==0) | |
301 | d += 5, type=1; /* /dev/md_dNpM */ | |
302 | else if (strncmp(d, "/md", 3)==0) | |
303 | d += 3, type=-1; /* /dev/mdN */ | |
304 | else if (d-dev > 3 && strncmp(d-2, "md/", 3)==0) | |
5a6d1148 | 305 | d += 1, type=-1; /* /dev/md/N */ |
e5329c37 NB |
306 | else |
307 | return 0; | |
8d80900b | 308 | if (!*d) |
e5329c37 | 309 | return 0; |
8d80900b NB |
310 | num = atoi(d); |
311 | while (isdigit(*d)) | |
312 | d++; | |
313 | if (*d) | |
e5329c37 | 314 | return 0; |
8d80900b NB |
315 | if (nump) *nump = num; |
316 | ||
317 | return type; | |
e5329c37 NB |
318 | } |
319 | ||
320 | ||
82b27616 NB |
321 | /* |
322 | * convert a major/minor pair for a block device into a name in /dev, if possible. | |
323 | * On the first call, walk /dev collecting name. | |
324 | * Put them in a simple linked listfor now. | |
325 | */ | |
326 | struct devmap { | |
327 | int major, minor; | |
328 | char *name; | |
329 | struct devmap *next; | |
330 | } *devlist = NULL; | |
331 | int devlist_ready = 0; | |
332 | ||
5787fa49 | 333 | #ifdef UCLIBC |
173fc515 NB |
334 | int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s) |
335 | { | |
336 | } | |
5787fa49 NB |
337 | char *map_dev(int major, int minor) |
338 | { | |
339 | #if 0 | |
340 | fprintf(stderr, "Warning - fail to map %d,%d to a device name\n", | |
341 | major, minor); | |
342 | #endif | |
343 | return NULL; | |
344 | } | |
345 | #else | |
82b27616 | 346 | |
173fc515 NB |
347 | #ifdef __dietlibc__ |
348 | int add_dev_1(const char *name, const struct stat *stb, int flag) | |
349 | { | |
350 | return add_dev(name, stb, flag, NULL); | |
351 | } | |
352 | int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags) | |
353 | { | |
354 | ftw(path, add_dev_1, nopenfd); | |
355 | } | |
356 | #endif | |
82b27616 NB |
357 | |
358 | int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s) | |
359 | { | |
360 | if ((stb->st_mode&S_IFMT)== S_IFBLK) { | |
361 | char *n = strdup(name); | |
362 | struct devmap *dm = malloc(sizeof(*dm)); | |
363 | if (dm) { | |
0df46c2a NB |
364 | dm->major = major(stb->st_rdev); |
365 | dm->minor = minor(stb->st_rdev); | |
82b27616 NB |
366 | dm->name = n; |
367 | dm->next = devlist; | |
368 | devlist = dm; | |
369 | } | |
370 | } | |
371 | return 0; | |
372 | } | |
373 | ||
dd0781e5 NB |
374 | /* |
375 | * Find a block device with the right major/minor number. | |
b79713f8 NB |
376 | * If we find multiple names, choose the shortest. |
377 | * If we find a non-standard name, it is probably there | |
378 | * deliberately so prefer it over a standard name. | |
379 | * This applies only to names for MD devices. | |
dd0781e5 | 380 | */ |
82b27616 NB |
381 | char *map_dev(int major, int minor) |
382 | { | |
dd0781e5 | 383 | struct devmap *p; |
b79713f8 | 384 | char *std = NULL, *nonstd=NULL; |
dd0781e5 | 385 | if (!devlist_ready) { |
dd0781e5 | 386 | nftw("/dev", add_dev, 10, FTW_PHYS); |
dd0781e5 NB |
387 | devlist_ready=1; |
388 | } | |
82b27616 | 389 | |
dd0781e5 NB |
390 | for (p=devlist; p; p=p->next) |
391 | if (p->major == major && | |
392 | p->minor == minor) { | |
b79713f8 NB |
393 | if (is_standard(p->name, NULL)) { |
394 | if (std == NULL || | |
395 | strlen(p->name) < strlen(std)) | |
396 | std = p->name; | |
397 | } else { | |
398 | if (nonstd == NULL || | |
399 | strlen(p->name) < strlen(nonstd)) | |
400 | nonstd = p->name; | |
401 | } | |
dd0781e5 | 402 | } |
b79713f8 | 403 | return nonstd ? nonstd : std; |
82b27616 NB |
404 | } |
405 | ||
5787fa49 | 406 | #endif |
82b27616 | 407 | |
4b1ac34b | 408 | unsigned long calc_csum(void *super, int bytes) |
82b27616 | 409 | { |
56eb10c0 | 410 | unsigned long long newcsum = 0; |
82b27616 | 411 | int i; |
4b1ac34b NB |
412 | unsigned int csum; |
413 | unsigned int *superc = (unsigned int*) super; | |
82b27616 | 414 | |
4b1ac34b | 415 | for(i=0; i<bytes/4; i++) |
82b27616 NB |
416 | newcsum+= superc[i]; |
417 | csum = (newcsum& 0xffffffff) + (newcsum>>32); | |
570c0542 NB |
418 | #ifdef __alpha__ |
419 | /* The in-kernel checksum calculation is always 16bit on | |
420 | * the alpha, though it is 32 bit on i386... | |
421 | * I wonder what it is elsewhere... (it uses and API in | |
422 | * a way that it shouldn't). | |
423 | */ | |
424 | csum = (csum & 0xffff) + (csum >> 16); | |
425 | csum = (csum & 0xffff) + (csum >> 16); | |
426 | #endif | |
82b27616 NB |
427 | return csum; |
428 | } | |
cd29a5c8 | 429 | |
56eb10c0 | 430 | char *human_size(long long bytes) |
cd29a5c8 NB |
431 | { |
432 | static char buf[30]; | |
d5d3721e NB |
433 | |
434 | /* We convert bytes to either centi-M{ega,ibi}bytes or | |
435 | * centi-G{igi,ibi}bytes, with appropriate rounding, | |
436 | * and then print 1/100th of those as a decimal. | |
437 | * We allow upto 2048Megabytes before converting to | |
438 | * gigabytes, as that shows more precision and isn't | |
439 | * too large a number. | |
440 | * Terrabytes are not yet handled. | |
441 | */ | |
cd29a5c8 | 442 | |
56eb10c0 | 443 | if (bytes < 5000*1024) |
cd29a5c8 | 444 | buf[0]=0; |
d5d3721e NB |
445 | else if (bytes < 2*1024LL*1024LL*1024LL) { |
446 | long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2; | |
447 | long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2; | |
8f23b0b3 | 448 | snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)", |
d5d3721e NB |
449 | cMiB/100 , cMiB % 100, |
450 | cMB/100, cMB % 100); | |
451 | } else { | |
452 | long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2; | |
453 | long cGB = (bytes / (1000000000LL/200LL ) +1) /2; | |
8f23b0b3 | 454 | snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)", |
d5d3721e NB |
455 | cGiB/100 , cGiB % 100, |
456 | cGB/100, cGB % 100); | |
457 | } | |
cd29a5c8 NB |
458 | return buf; |
459 | } | |
e0d19036 NB |
460 | |
461 | char *human_size_brief(long long bytes) | |
462 | { | |
463 | static char buf[30]; | |
464 | ||
465 | ||
466 | if (bytes < 5000*1024) | |
8f23b0b3 | 467 | snprintf(buf, sizeof(buf), "%ld.%02ldKiB", |
bd526cee | 468 | (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024) |
e0d19036 NB |
469 | ); |
470 | else if (bytes < 2*1024LL*1024LL*1024LL) | |
8f23b0b3 | 471 | snprintf(buf, sizeof(buf), "%ld.%02ldMiB", |
e0d19036 | 472 | (long)(bytes>>20), |
bd526cee | 473 | (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100) |
e0d19036 NB |
474 | ); |
475 | else | |
8f23b0b3 | 476 | snprintf(buf, sizeof(buf), "%ld.%02ldGiB", |
e0d19036 | 477 | (long)(bytes>>30), |
bd526cee | 478 | (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100) |
e0d19036 NB |
479 | ); |
480 | return buf; | |
481 | } | |
482 | ||
dd0781e5 | 483 | int get_mdp_major(void) |
98c6faba | 484 | { |
dd0781e5 NB |
485 | static int mdp_major = -1; |
486 | FILE *fl; | |
98c6faba NB |
487 | char *w; |
488 | int have_block = 0; | |
489 | int have_devices = 0; | |
490 | int last_num = -1; | |
dd0781e5 NB |
491 | |
492 | if (mdp_major != -1) | |
493 | return mdp_major; | |
494 | fl = fopen("/proc/devices", "r"); | |
98c6faba | 495 | if (!fl) |
dd0781e5 | 496 | return -1; |
98c6faba NB |
497 | while ((w = conf_word(fl, 1))) { |
498 | if (have_block && strcmp(w, "devices:")==0) | |
499 | have_devices = 1; | |
500 | have_block = (strcmp(w, "Block")==0); | |
501 | if (isdigit(w[0])) | |
502 | last_num = atoi(w); | |
503 | if (have_devices && strcmp(w, "mdp")==0) | |
504 | mdp_major = last_num; | |
505 | free(w); | |
506 | } | |
507 | fclose(fl); | |
dd0781e5 | 508 | return mdp_major; |
98c6faba NB |
509 | } |
510 | ||
511 | ||
e0d19036 | 512 | |
e0d19036 NB |
513 | char *get_md_name(int dev) |
514 | { | |
515 | /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */ | |
98c6faba | 516 | /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */ |
e0d19036 NB |
517 | static char devname[50]; |
518 | struct stat stb; | |
98c6faba | 519 | dev_t rdev; |
dd0781e5 | 520 | char *dn; |
98c6faba NB |
521 | |
522 | if (dev < 0) { | |
dd0781e5 NB |
523 | int mdp = get_mdp_major(); |
524 | if (mdp < 0) return NULL; | |
0df46c2a | 525 | rdev = makedev(mdp, (-1-dev)<<6); |
8f23b0b3 | 526 | snprintf(devname, sizeof(devname), "/dev/md/d%d", -1-dev); |
98c6faba NB |
527 | if (stat(devname, &stb) == 0 |
528 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
529 | && (stb.st_rdev == rdev)) | |
530 | return devname; | |
531 | } else { | |
0df46c2a | 532 | rdev = makedev(MD_MAJOR, dev); |
8f23b0b3 | 533 | snprintf(devname, sizeof(devname), "/dev/md%d", dev); |
98c6faba NB |
534 | if (stat(devname, &stb) == 0 |
535 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
536 | && (stb.st_rdev == rdev)) | |
537 | return devname; | |
538 | ||
8f23b0b3 | 539 | snprintf(devname, sizeof(devname), "/dev/md/%d", dev); |
98c6faba NB |
540 | if (stat(devname, &stb) == 0 |
541 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
542 | && (stb.st_rdev == rdev)) | |
543 | return devname; | |
544 | } | |
8d80900b | 545 | dn = map_dev(major(rdev), minor(rdev)); |
dd0781e5 NB |
546 | if (dn) |
547 | return dn; | |
8f23b0b3 | 548 | snprintf(devname, sizeof(devname), "/dev/.tmp.md%d", dev); |
e0d19036 | 549 | if (mknod(devname, S_IFBLK | 0600, rdev) == -1) |
dd0781e5 NB |
550 | if (errno != EEXIST) |
551 | return NULL; | |
e0d19036 NB |
552 | |
553 | if (stat(devname, &stb) == 0 | |
554 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
555 | && (stb.st_rdev == rdev)) | |
556 | return devname; | |
557 | unlink(devname); | |
558 | return NULL; | |
559 | } | |
560 | ||
561 | void put_md_name(char *name) | |
562 | { | |
563 | if (strncmp(name, "/dev/.tmp.md", 12)==0) | |
564 | unlink(name); | |
565 | } | |
f9ce90ba NB |
566 | |
567 | ||
568 | ||
82d9eba6 | 569 | struct superswitch *superlist[] = { &super0, &super1, NULL }; |
f9ce90ba | 570 | |
82d9eba6 | 571 | struct supertype *super_by_version(int vers, int minor) |
f9ce90ba | 572 | { |
82d9eba6 NB |
573 | struct supertype *st = malloc(sizeof(*st)); |
574 | if (!st) return st; | |
6fbba4c9 | 575 | if (vers == 0) { |
82d9eba6 | 576 | st->ss = &super0; |
6fbba4c9 NB |
577 | st->max_devs = MD_SB_DISKS; |
578 | } | |
82d9eba6 | 579 | |
6fbba4c9 | 580 | if (vers == 1) { |
82d9eba6 | 581 | st->ss = &super1; |
6fbba4c9 NB |
582 | st->max_devs = 384; |
583 | } | |
82d9eba6 NB |
584 | st->minor_version = minor; |
585 | return st; | |
f9ce90ba NB |
586 | } |
587 | ||
82d9eba6 | 588 | struct supertype *guess_super(int fd) |
f9ce90ba NB |
589 | { |
590 | /* try each load_super to find the best match, | |
591 | * and return the best superswitch | |
592 | */ | |
82d9eba6 NB |
593 | struct superswitch *ss; |
594 | struct supertype *st; | |
570c0542 NB |
595 | unsigned long besttime = 0; |
596 | int bestsuper = -1; | |
82d9eba6 | 597 | |
f9ce90ba NB |
598 | void *sbp = NULL; |
599 | int i; | |
600 | ||
82d9eba6 NB |
601 | st = malloc(sizeof(*st)); |
602 | memset(st, 0, sizeof(*st)); | |
f9ce90ba NB |
603 | for (i=0 ; superlist[i]; i++) { |
604 | int rv; | |
605 | ss = superlist[i]; | |
f277ce36 | 606 | st->ss = NULL; |
82d9eba6 | 607 | rv = ss->load_super(st, fd, &sbp, NULL); |
570c0542 NB |
608 | if (rv == 0) { |
609 | struct mdinfo info; | |
610 | ss->getinfo_super(&info, sbp); | |
611 | if (bestsuper == -1 || | |
612 | besttime < info.array.ctime) { | |
613 | bestsuper = i; | |
614 | besttime = info.array.ctime; | |
570c0542 NB |
615 | } |
616 | free(sbp); | |
617 | } | |
618 | } | |
619 | if (bestsuper != -1) { | |
620 | int rv; | |
f277ce36 | 621 | st->ss = NULL; |
570c0542 | 622 | rv = superlist[bestsuper]->load_super(st, fd, &sbp, NULL); |
f9ce90ba NB |
623 | if (rv == 0) { |
624 | free(sbp); | |
82d9eba6 | 625 | return st; |
f9ce90ba NB |
626 | } |
627 | } | |
570c0542 | 628 | free(st); |
f9ce90ba NB |
629 | return NULL; |
630 | } | |
fe6729fa NB |
631 | |
632 | #ifdef __TINYC__ | |
633 | /* tinyc doesn't optimize this check in ioctl.h out ... */ | |
634 | unsigned int __invalid_size_argument_for_IOC = 0; | |
635 | #endif | |
636 |