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