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