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