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