]>
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 | ||
265e0f17 NB |
121 | int enough(int level, int raid_disks, int layout, |
122 | char *avail, int avail_disks) | |
64c4757e | 123 | { |
265e0f17 | 124 | int copies, first; |
64c4757e | 125 | switch (level) { |
265e0f17 NB |
126 | case 10: |
127 | /* This is the tricky one - we need to check | |
128 | * which actual disks are present. | |
129 | */ | |
130 | copies = (layout&255)* (layout>>8); | |
131 | first=0; | |
132 | do { | |
133 | /* there must be one of the 'copies' form 'first' */ | |
134 | int n = copies; | |
135 | int cnt=0; | |
136 | while (n--) { | |
137 | if (avail[first]) | |
138 | cnt++; | |
139 | first = (first+1) % raid_disks; | |
140 | } | |
141 | if (cnt == 0) | |
142 | return 0; | |
143 | ||
144 | } while (first != 0); | |
145 | return 1; | |
e5329c37 | 146 | |
e0d19036 NB |
147 | case -4: |
148 | return avail_disks>= 1; | |
64c4757e NB |
149 | case -1: |
150 | case 0: | |
151 | return avail_disks == raid_disks; | |
152 | case 1: | |
153 | return avail_disks >= 1; | |
154 | case 4: | |
155 | case 5: | |
156 | return avail_disks >= raid_disks-1; | |
98c6faba NB |
157 | case 6: |
158 | return avail_disks >= raid_disks-2; | |
64c4757e NB |
159 | default: |
160 | return 0; | |
161 | } | |
162 | } | |
163 | ||
f277ce36 | 164 | int same_uuid(int a[4], int b[4], int swapuuid) |
64c4757e | 165 | { |
f277ce36 NB |
166 | if (swapuuid) { |
167 | /* parse uuids are hostendian. | |
168 | * uuid's from some superblocks are big-ending | |
169 | * if there is a difference, we need to swap.. | |
170 | */ | |
171 | unsigned char *ac = (unsigned char *)a; | |
172 | unsigned char *bc = (unsigned char *)b; | |
173 | int i; | |
174 | for (i=0; i<16; i+= 4) { | |
175 | if (ac[i+0] != bc[i+3] || | |
176 | ac[i+1] != bc[i+2] || | |
177 | ac[i+2] != bc[i+1] || | |
178 | ac[i+3] != bc[i+0]) | |
179 | return 0; | |
180 | } | |
181 | return 1; | |
182 | } else { | |
183 | if (a[0]==b[0] && | |
184 | a[1]==b[1] && | |
185 | a[2]==b[2] && | |
186 | a[3]==b[3]) | |
187 | return 1; | |
188 | return 0; | |
189 | } | |
64c4757e NB |
190 | } |
191 | ||
682c7051 NB |
192 | int check_ext2(int fd, char *name) |
193 | { | |
194 | /* | |
195 | * Check for an ext2fs file system. | |
196 | * Superblock is always 1K at 1K offset | |
197 | * | |
198 | * s_magic is le16 at 56 == 0xEF53 | |
199 | * report mtime - le32 at 44 | |
200 | * blocks - le32 at 4 | |
201 | * logblksize - le32 at 24 | |
202 | */ | |
203 | unsigned char sb[1024]; | |
204 | time_t mtime; | |
205 | int size, bsize; | |
206 | if (lseek(fd, 1024,0)!= 1024) | |
207 | return 0; | |
208 | if (read(fd, sb, 1024)!= 1024) | |
209 | return 0; | |
210 | if (sb[56] != 0x53 || sb[57] != 0xef) | |
211 | return 0; | |
212 | ||
213 | mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8; | |
214 | bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8; | |
215 | size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8; | |
216 | fprintf(stderr, Name ": %s appears to contain an ext2fs file system\n", | |
217 | name); | |
218 | fprintf(stderr," size=%dK mtime=%s", | |
219 | size*(1<<bsize), ctime(&mtime)); | |
220 | return 1; | |
221 | } | |
222 | ||
223 | int check_reiser(int fd, char *name) | |
224 | { | |
225 | /* | |
226 | * superblock is at 64K | |
227 | * size is 1024; | |
228 | * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52 | |
229 | * | |
230 | */ | |
231 | unsigned char sb[1024]; | |
232 | int size; | |
233 | if (lseek(fd, 64*1024, 0) != 64*1024) | |
234 | return 0; | |
235 | if (read(fd, sb, 1024) != 1024) | |
236 | return 0; | |
a46f4061 NB |
237 | if (strncmp((char*)sb+52, "ReIsErFs",8)!=0 && |
238 | strncmp((char*)sb+52, "ReIsEr2Fs",9)!=0) | |
682c7051 NB |
239 | return 0; |
240 | fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name); | |
241 | size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8; | |
242 | fprintf(stderr, " size = %dK\n", size*4); | |
243 | ||
244 | return 1; | |
245 | } | |
246 | ||
247 | int check_raid(int fd, char *name) | |
248 | { | |
4b1ac34b NB |
249 | void *super; |
250 | struct mdinfo info; | |
947fd4dd | 251 | struct mddev_ident_s ident; |
682c7051 | 252 | time_t crtime; |
d078d77c | 253 | char *level; |
82d9eba6 | 254 | struct supertype *st = guess_super(fd); |
f9ce90ba | 255 | |
82d9eba6 NB |
256 | if (!st) return 0; |
257 | st->ss->load_super(st, fd, &super, name); | |
258 | /* Looks like a raid array .. */ | |
259 | fprintf(stderr, Name ": %s appears to be part of a raid array:\n", | |
260 | name); | |
947fd4dd | 261 | st->ss->getinfo_super(&info, &ident, super); |
82d9eba6 NB |
262 | free(super); |
263 | crtime = info.array.ctime; | |
d078d77c NB |
264 | level = map_num(pers, info.array.level); |
265 | if (!level) level = "-unknown-"; | |
266 | fprintf(stderr, " level=%s devices=%d ctime=%s", | |
267 | level, info.array.raid_disks, ctime(&crtime)); | |
82d9eba6 | 268 | return 1; |
682c7051 NB |
269 | } |
270 | ||
682c7051 NB |
271 | int ask(char *mesg) |
272 | { | |
273 | char *add = ""; | |
274 | int i; | |
275 | for (i=0; i<5; i++) { | |
276 | char buf[100]; | |
277 | fprintf(stderr, "%s%s", mesg, add); | |
278 | fflush(stderr); | |
279 | if (fgets(buf, 100, stdin)==NULL) | |
280 | return 0; | |
281 | if (buf[0]=='y' || buf[0]=='Y') | |
282 | return 1; | |
283 | if (buf[0]=='n' || buf[0]=='N') | |
284 | return 0; | |
285 | add = "(y/n) "; | |
286 | } | |
287 | fprintf(stderr, Name ": assuming 'no'\n"); | |
288 | return 0; | |
289 | } | |
290 | ||
291 | char *map_num(mapping_t *map, int num) | |
292 | { | |
293 | while (map->name) { | |
294 | if (map->num == num) | |
295 | return map->name; | |
296 | map++; | |
297 | } | |
298 | return NULL; | |
299 | } | |
300 | ||
301 | int map_name(mapping_t *map, char *name) | |
302 | { | |
303 | while (map->name) { | |
304 | if (strcmp(map->name, name)==0) | |
305 | return map->num; | |
306 | map++; | |
307 | } | |
98c6faba | 308 | return UnSet; |
682c7051 | 309 | } |
82b27616 | 310 | |
e5329c37 | 311 | |
8d80900b | 312 | int is_standard(char *dev, int *nump) |
e5329c37 NB |
313 | { |
314 | /* tests if dev is a "standard" md dev name. | |
315 | * i.e if the last component is "/dNN" or "/mdNN", | |
316 | * where NN is a string of digits | |
317 | */ | |
8d80900b NB |
318 | char *d = strrchr(dev, '/'); |
319 | int type=0; | |
320 | int num; | |
321 | if (!d) | |
e5329c37 | 322 | return 0; |
8d80900b NB |
323 | if (strncmp(d, "/d",2)==0) |
324 | d += 2, type=1; /* /dev/md/dN{pM} */ | |
325 | else if (strncmp(d, "/md_d", 5)==0) | |
326 | d += 5, type=1; /* /dev/md_dNpM */ | |
327 | else if (strncmp(d, "/md", 3)==0) | |
328 | d += 3, type=-1; /* /dev/mdN */ | |
329 | else if (d-dev > 3 && strncmp(d-2, "md/", 3)==0) | |
5a6d1148 | 330 | d += 1, type=-1; /* /dev/md/N */ |
e5329c37 NB |
331 | else |
332 | return 0; | |
8d80900b | 333 | if (!*d) |
e5329c37 | 334 | return 0; |
8d80900b NB |
335 | num = atoi(d); |
336 | while (isdigit(*d)) | |
337 | d++; | |
338 | if (*d) | |
e5329c37 | 339 | return 0; |
8d80900b NB |
340 | if (nump) *nump = num; |
341 | ||
342 | return type; | |
e5329c37 NB |
343 | } |
344 | ||
345 | ||
82b27616 NB |
346 | /* |
347 | * convert a major/minor pair for a block device into a name in /dev, if possible. | |
348 | * On the first call, walk /dev collecting name. | |
349 | * Put them in a simple linked listfor now. | |
350 | */ | |
351 | struct devmap { | |
352 | int major, minor; | |
353 | char *name; | |
354 | struct devmap *next; | |
355 | } *devlist = NULL; | |
356 | int devlist_ready = 0; | |
357 | ||
5787fa49 | 358 | #ifdef UCLIBC |
173fc515 NB |
359 | int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s) |
360 | { | |
361 | } | |
5787fa49 NB |
362 | char *map_dev(int major, int minor) |
363 | { | |
364 | #if 0 | |
365 | fprintf(stderr, "Warning - fail to map %d,%d to a device name\n", | |
366 | major, minor); | |
367 | #endif | |
368 | return NULL; | |
369 | } | |
370 | #else | |
82b27616 | 371 | |
173fc515 NB |
372 | #ifdef __dietlibc__ |
373 | int add_dev_1(const char *name, const struct stat *stb, int flag) | |
374 | { | |
375 | return add_dev(name, stb, flag, NULL); | |
376 | } | |
377 | int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags) | |
378 | { | |
379 | ftw(path, add_dev_1, nopenfd); | |
380 | } | |
381 | #endif | |
82b27616 NB |
382 | |
383 | int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s) | |
384 | { | |
385 | if ((stb->st_mode&S_IFMT)== S_IFBLK) { | |
386 | char *n = strdup(name); | |
387 | struct devmap *dm = malloc(sizeof(*dm)); | |
388 | if (dm) { | |
0df46c2a NB |
389 | dm->major = major(stb->st_rdev); |
390 | dm->minor = minor(stb->st_rdev); | |
82b27616 NB |
391 | dm->name = n; |
392 | dm->next = devlist; | |
393 | devlist = dm; | |
394 | } | |
395 | } | |
396 | return 0; | |
397 | } | |
398 | ||
dd0781e5 NB |
399 | /* |
400 | * Find a block device with the right major/minor number. | |
b79713f8 NB |
401 | * If we find multiple names, choose the shortest. |
402 | * If we find a non-standard name, it is probably there | |
403 | * deliberately so prefer it over a standard name. | |
404 | * This applies only to names for MD devices. | |
dd0781e5 | 405 | */ |
82b27616 NB |
406 | char *map_dev(int major, int minor) |
407 | { | |
dd0781e5 | 408 | struct devmap *p; |
b79713f8 | 409 | char *std = NULL, *nonstd=NULL; |
dd0781e5 | 410 | if (!devlist_ready) { |
dd0781e5 | 411 | nftw("/dev", add_dev, 10, FTW_PHYS); |
dd0781e5 NB |
412 | devlist_ready=1; |
413 | } | |
82b27616 | 414 | |
dd0781e5 NB |
415 | for (p=devlist; p; p=p->next) |
416 | if (p->major == major && | |
417 | p->minor == minor) { | |
b79713f8 NB |
418 | if (is_standard(p->name, NULL)) { |
419 | if (std == NULL || | |
420 | strlen(p->name) < strlen(std)) | |
421 | std = p->name; | |
422 | } else { | |
423 | if (nonstd == NULL || | |
424 | strlen(p->name) < strlen(nonstd)) | |
425 | nonstd = p->name; | |
426 | } | |
dd0781e5 | 427 | } |
b79713f8 | 428 | return nonstd ? nonstd : std; |
82b27616 NB |
429 | } |
430 | ||
5787fa49 | 431 | #endif |
82b27616 | 432 | |
4b1ac34b | 433 | unsigned long calc_csum(void *super, int bytes) |
82b27616 | 434 | { |
56eb10c0 | 435 | unsigned long long newcsum = 0; |
82b27616 | 436 | int i; |
4b1ac34b NB |
437 | unsigned int csum; |
438 | unsigned int *superc = (unsigned int*) super; | |
82b27616 | 439 | |
4b1ac34b | 440 | for(i=0; i<bytes/4; i++) |
82b27616 NB |
441 | newcsum+= superc[i]; |
442 | csum = (newcsum& 0xffffffff) + (newcsum>>32); | |
570c0542 NB |
443 | #ifdef __alpha__ |
444 | /* The in-kernel checksum calculation is always 16bit on | |
445 | * the alpha, though it is 32 bit on i386... | |
446 | * I wonder what it is elsewhere... (it uses and API in | |
447 | * a way that it shouldn't). | |
448 | */ | |
449 | csum = (csum & 0xffff) + (csum >> 16); | |
450 | csum = (csum & 0xffff) + (csum >> 16); | |
451 | #endif | |
82b27616 NB |
452 | return csum; |
453 | } | |
cd29a5c8 | 454 | |
56eb10c0 | 455 | char *human_size(long long bytes) |
cd29a5c8 NB |
456 | { |
457 | static char buf[30]; | |
d5d3721e NB |
458 | |
459 | /* We convert bytes to either centi-M{ega,ibi}bytes or | |
460 | * centi-G{igi,ibi}bytes, with appropriate rounding, | |
461 | * and then print 1/100th of those as a decimal. | |
462 | * We allow upto 2048Megabytes before converting to | |
463 | * gigabytes, as that shows more precision and isn't | |
464 | * too large a number. | |
465 | * Terrabytes are not yet handled. | |
466 | */ | |
cd29a5c8 | 467 | |
56eb10c0 | 468 | if (bytes < 5000*1024) |
cd29a5c8 | 469 | buf[0]=0; |
d5d3721e NB |
470 | else if (bytes < 2*1024LL*1024LL*1024LL) { |
471 | long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2; | |
472 | long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2; | |
8f23b0b3 | 473 | snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)", |
d5d3721e NB |
474 | cMiB/100 , cMiB % 100, |
475 | cMB/100, cMB % 100); | |
476 | } else { | |
477 | long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2; | |
478 | long cGB = (bytes / (1000000000LL/200LL ) +1) /2; | |
8f23b0b3 | 479 | snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)", |
d5d3721e NB |
480 | cGiB/100 , cGiB % 100, |
481 | cGB/100, cGB % 100); | |
482 | } | |
cd29a5c8 NB |
483 | return buf; |
484 | } | |
e0d19036 NB |
485 | |
486 | char *human_size_brief(long long bytes) | |
487 | { | |
488 | static char buf[30]; | |
489 | ||
490 | ||
491 | if (bytes < 5000*1024) | |
8f23b0b3 | 492 | snprintf(buf, sizeof(buf), "%ld.%02ldKiB", |
bd526cee | 493 | (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024) |
e0d19036 NB |
494 | ); |
495 | else if (bytes < 2*1024LL*1024LL*1024LL) | |
8f23b0b3 | 496 | snprintf(buf, sizeof(buf), "%ld.%02ldMiB", |
e0d19036 | 497 | (long)(bytes>>20), |
bd526cee | 498 | (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100) |
e0d19036 NB |
499 | ); |
500 | else | |
8f23b0b3 | 501 | snprintf(buf, sizeof(buf), "%ld.%02ldGiB", |
e0d19036 | 502 | (long)(bytes>>30), |
bd526cee | 503 | (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100) |
e0d19036 NB |
504 | ); |
505 | return buf; | |
506 | } | |
507 | ||
dd0781e5 | 508 | int get_mdp_major(void) |
98c6faba | 509 | { |
dd0781e5 NB |
510 | static int mdp_major = -1; |
511 | FILE *fl; | |
98c6faba NB |
512 | char *w; |
513 | int have_block = 0; | |
514 | int have_devices = 0; | |
515 | int last_num = -1; | |
dd0781e5 NB |
516 | |
517 | if (mdp_major != -1) | |
518 | return mdp_major; | |
519 | fl = fopen("/proc/devices", "r"); | |
98c6faba | 520 | if (!fl) |
dd0781e5 | 521 | return -1; |
98c6faba NB |
522 | while ((w = conf_word(fl, 1))) { |
523 | if (have_block && strcmp(w, "devices:")==0) | |
524 | have_devices = 1; | |
525 | have_block = (strcmp(w, "Block")==0); | |
526 | if (isdigit(w[0])) | |
527 | last_num = atoi(w); | |
528 | if (have_devices && strcmp(w, "mdp")==0) | |
529 | mdp_major = last_num; | |
530 | free(w); | |
531 | } | |
532 | fclose(fl); | |
dd0781e5 | 533 | return mdp_major; |
98c6faba NB |
534 | } |
535 | ||
536 | ||
e0d19036 | 537 | |
e0d19036 NB |
538 | char *get_md_name(int dev) |
539 | { | |
540 | /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */ | |
98c6faba | 541 | /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */ |
e0d19036 NB |
542 | static char devname[50]; |
543 | struct stat stb; | |
98c6faba | 544 | dev_t rdev; |
dd0781e5 | 545 | char *dn; |
98c6faba NB |
546 | |
547 | if (dev < 0) { | |
dd0781e5 NB |
548 | int mdp = get_mdp_major(); |
549 | if (mdp < 0) return NULL; | |
0df46c2a | 550 | rdev = makedev(mdp, (-1-dev)<<6); |
8f23b0b3 | 551 | snprintf(devname, sizeof(devname), "/dev/md/d%d", -1-dev); |
98c6faba NB |
552 | if (stat(devname, &stb) == 0 |
553 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
554 | && (stb.st_rdev == rdev)) | |
555 | return devname; | |
556 | } else { | |
0df46c2a | 557 | rdev = makedev(MD_MAJOR, dev); |
8f23b0b3 | 558 | snprintf(devname, sizeof(devname), "/dev/md%d", dev); |
98c6faba NB |
559 | if (stat(devname, &stb) == 0 |
560 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
561 | && (stb.st_rdev == rdev)) | |
562 | return devname; | |
563 | ||
8f23b0b3 | 564 | snprintf(devname, sizeof(devname), "/dev/md/%d", dev); |
98c6faba NB |
565 | if (stat(devname, &stb) == 0 |
566 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
567 | && (stb.st_rdev == rdev)) | |
568 | return devname; | |
569 | } | |
8d80900b | 570 | dn = map_dev(major(rdev), minor(rdev)); |
dd0781e5 NB |
571 | if (dn) |
572 | return dn; | |
8f23b0b3 | 573 | snprintf(devname, sizeof(devname), "/dev/.tmp.md%d", dev); |
e0d19036 | 574 | if (mknod(devname, S_IFBLK | 0600, rdev) == -1) |
dd0781e5 NB |
575 | if (errno != EEXIST) |
576 | return NULL; | |
e0d19036 NB |
577 | |
578 | if (stat(devname, &stb) == 0 | |
579 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
580 | && (stb.st_rdev == rdev)) | |
581 | return devname; | |
582 | unlink(devname); | |
583 | return NULL; | |
584 | } | |
585 | ||
586 | void put_md_name(char *name) | |
587 | { | |
588 | if (strncmp(name, "/dev/.tmp.md", 12)==0) | |
589 | unlink(name); | |
590 | } | |
f9ce90ba | 591 | |
8b0dabea NB |
592 | int dev_open(char *dev, int flags) |
593 | { | |
594 | /* like 'open', but if 'dev' matches %d:%d, create a temp | |
595 | * block device and open that | |
596 | */ | |
597 | char *e; | |
598 | int fd = -1; | |
599 | char devname[32]; | |
600 | int major = strtoul(dev, &e, 0); | |
601 | int minor; | |
602 | if (e > dev && *e == ':' && e[1] && | |
603 | (minor = strtoul(e+1, &e, 0)) >= 0 && | |
604 | *e == 0) { | |
605 | snprintf(devname, sizeof(devname), "/dev/.tmp.md.%d:%d", major, minor); | |
606 | if (mknod(devname, S_IFBLK|0600, makedev(major, minor))==0) { | |
607 | fd = open(devname, flags); | |
608 | unlink(devname); | |
609 | } | |
610 | } else | |
611 | fd = open(dev, flags); | |
612 | return fd; | |
613 | } | |
f9ce90ba | 614 | |
82d9eba6 | 615 | struct superswitch *superlist[] = { &super0, &super1, NULL }; |
f9ce90ba | 616 | |
82d9eba6 | 617 | struct supertype *super_by_version(int vers, int minor) |
f9ce90ba | 618 | { |
82d9eba6 NB |
619 | struct supertype *st = malloc(sizeof(*st)); |
620 | if (!st) return st; | |
6fbba4c9 | 621 | if (vers == 0) { |
82d9eba6 | 622 | st->ss = &super0; |
6fbba4c9 NB |
623 | st->max_devs = MD_SB_DISKS; |
624 | } | |
82d9eba6 | 625 | |
6fbba4c9 | 626 | if (vers == 1) { |
82d9eba6 | 627 | st->ss = &super1; |
6fbba4c9 NB |
628 | st->max_devs = 384; |
629 | } | |
82d9eba6 NB |
630 | st->minor_version = minor; |
631 | return st; | |
f9ce90ba NB |
632 | } |
633 | ||
82d9eba6 | 634 | struct supertype *guess_super(int fd) |
f9ce90ba NB |
635 | { |
636 | /* try each load_super to find the best match, | |
637 | * and return the best superswitch | |
638 | */ | |
82d9eba6 NB |
639 | struct superswitch *ss; |
640 | struct supertype *st; | |
570c0542 NB |
641 | unsigned long besttime = 0; |
642 | int bestsuper = -1; | |
82d9eba6 | 643 | |
f9ce90ba NB |
644 | void *sbp = NULL; |
645 | int i; | |
646 | ||
82d9eba6 NB |
647 | st = malloc(sizeof(*st)); |
648 | memset(st, 0, sizeof(*st)); | |
f9ce90ba NB |
649 | for (i=0 ; superlist[i]; i++) { |
650 | int rv; | |
651 | ss = superlist[i]; | |
f277ce36 | 652 | st->ss = NULL; |
82d9eba6 | 653 | rv = ss->load_super(st, fd, &sbp, NULL); |
570c0542 NB |
654 | if (rv == 0) { |
655 | struct mdinfo info; | |
947fd4dd NB |
656 | struct mddev_ident_s ident; |
657 | ss->getinfo_super(&info, &ident, sbp); | |
570c0542 NB |
658 | if (bestsuper == -1 || |
659 | besttime < info.array.ctime) { | |
660 | bestsuper = i; | |
661 | besttime = info.array.ctime; | |
570c0542 NB |
662 | } |
663 | free(sbp); | |
664 | } | |
665 | } | |
666 | if (bestsuper != -1) { | |
667 | int rv; | |
f277ce36 | 668 | st->ss = NULL; |
570c0542 | 669 | rv = superlist[bestsuper]->load_super(st, fd, &sbp, NULL); |
f9ce90ba NB |
670 | if (rv == 0) { |
671 | free(sbp); | |
82d9eba6 | 672 | return st; |
f9ce90ba NB |
673 | } |
674 | } | |
570c0542 | 675 | free(st); |
f9ce90ba NB |
676 | return NULL; |
677 | } | |
fe6729fa NB |
678 | |
679 | #ifdef __TINYC__ | |
680 | /* tinyc doesn't optimize this check in ioctl.h out ... */ | |
681 | unsigned int __invalid_size_argument_for_IOC = 0; | |
682 | #endif | |
683 |