Can now mostly assemble DDF arrays
[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>
a322f70c 34#include <dirent.h>
0a816ef9
NB
35
36/*
37 * following taken from linux/blkpg.h because they aren't
38 * anywhere else and it isn't safe to #include linux/ * stuff.
39 */
40
41#define BLKPG _IO(0x12,105)
42
43/* The argument structure */
44struct blkpg_ioctl_arg {
45 int op;
46 int flags;
47 int datalen;
48 void *data;
49};
50
51/* The subfunctions (for the op field) */
52#define BLKPG_ADD_PARTITION 1
53#define BLKPG_DEL_PARTITION 2
54
55/* Sizes of name fields. Unused at present. */
56#define BLKPG_DEVNAMELTH 64
57#define BLKPG_VOLNAMELTH 64
58
59/* The data structure for ADD_PARTITION and DEL_PARTITION */
60struct blkpg_partition {
61 long long start; /* starting offset in bytes */
62 long long length; /* length in bytes */
63 int pno; /* partition number */
64 char devname[BLKPG_DEVNAMELTH]; /* partition name, like sda5 or c0d1p2,
65 to be used in kernel messages */
66 char volname[BLKPG_VOLNAMELTH]; /* volume label */
67};
64c4757e
NB
68
69/*
70 * Parse a 128 bit uuid in 4 integers
71 * format is 32 hexx nibbles with options :.<space> separator
72 * If not exactly 32 hex digits are found, return 0
73 * else return 1
74 */
75int parse_uuid(char *str, int uuid[4])
76{
aba69144
NB
77 int hit = 0; /* number of Hex digIT */
78 int i;
79 char c;
80 for (i=0; i<4; i++) uuid[i]=0;
81
82 while ((c= *str++)) {
83 int n;
84 if (c>='0' && c<='9')
85 n = c-'0';
86 else if (c>='a' && c <= 'f')
87 n = 10 + c - 'a';
88 else if (c>='A' && c <= 'F')
89 n = 10 + c - 'A';
90 else if (strchr(":. -", c))
91 continue;
92 else return 0;
93
94 if (hit<32) {
95 uuid[hit/8] <<= 4;
96 uuid[hit/8] += n;
97 }
98 hit++;
82b27616 99 }
aba69144
NB
100 if (hit == 32)
101 return 1;
102 return 0;
64c4757e
NB
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
64c4757e
NB
135int get_linux_version()
136{
137 struct utsname name;
98c6faba 138 char *cp;
64c4757e
NB
139 int a,b,c;
140 if (uname(&name) <0)
141 return -1;
142
98c6faba
NB
143 cp = name.release;
144 a = strtoul(cp, &cp, 10);
145 if (*cp != '.') return -1;
146 b = strtoul(cp+1, &cp, 10);
147 if (*cp != '.') return -1;
148 c = strtoul(cp+1, NULL, 10);
149
682c7051 150 return (a*1000000)+(b*1000)+c;
64c4757e
NB
151}
152
0430ed48
NB
153void remove_partitions(int fd)
154{
155 /* remove partitions from this block devices.
156 * This is used for components added to an array
157 */
158#ifdef BLKPG_DEL_PARTITION
159 struct blkpg_ioctl_arg a;
160 struct blkpg_partition p;
161
162 a.op = BLKPG_DEL_PARTITION;
163 a.data = (void*)&p;
164 a.datalen = sizeof(p);
165 a.flags = 0;
166 memset(a.data, 0, a.datalen);
167 for (p.pno=0; p.pno < 16; p.pno++)
168 ioctl(fd, BLKPG, &a);
169#endif
170}
171
583315d9 172int enough(int level, int raid_disks, int layout, int clean,
265e0f17 173 char *avail, int avail_disks)
64c4757e 174{
265e0f17 175 int copies, first;
64c4757e 176 switch (level) {
265e0f17
NB
177 case 10:
178 /* This is the tricky one - we need to check
179 * which actual disks are present.
180 */
702b557b 181 copies = (layout&255)* ((layout>>8) & 255);
265e0f17
NB
182 first=0;
183 do {
184 /* there must be one of the 'copies' form 'first' */
185 int n = copies;
186 int cnt=0;
187 while (n--) {
188 if (avail[first])
189 cnt++;
190 first = (first+1) % raid_disks;
191 }
192 if (cnt == 0)
193 return 0;
194
195 } while (first != 0);
196 return 1;
e5329c37 197
e0d19036
NB
198 case -4:
199 return avail_disks>= 1;
64c4757e
NB
200 case -1:
201 case 0:
202 return avail_disks == raid_disks;
203 case 1:
204 return avail_disks >= 1;
205 case 4:
206 case 5:
583315d9
NB
207 if (clean)
208 return avail_disks >= raid_disks-1;
209 else
210 return avail_disks >= raid_disks;
98c6faba 211 case 6:
583315d9
NB
212 if (clean)
213 return avail_disks >= raid_disks-2;
214 else
215 return avail_disks >= raid_disks;
64c4757e
NB
216 default:
217 return 0;
218 }
219}
220
f277ce36 221int same_uuid(int a[4], int b[4], int swapuuid)
64c4757e 222{
f277ce36
NB
223 if (swapuuid) {
224 /* parse uuids are hostendian.
225 * uuid's from some superblocks are big-ending
aba69144 226 * if there is a difference, we need to swap..
f277ce36
NB
227 */
228 unsigned char *ac = (unsigned char *)a;
229 unsigned char *bc = (unsigned char *)b;
230 int i;
231 for (i=0; i<16; i+= 4) {
232 if (ac[i+0] != bc[i+3] ||
233 ac[i+1] != bc[i+2] ||
234 ac[i+2] != bc[i+1] ||
235 ac[i+3] != bc[i+0])
236 return 0;
237 }
238 return 1;
239 } else {
240 if (a[0]==b[0] &&
241 a[1]==b[1] &&
242 a[2]==b[2] &&
243 a[3]==b[3])
244 return 1;
245 return 0;
246 }
64c4757e 247}
350f29f9
NB
248void copy_uuid(void *a, int b[4], int swapuuid)
249{
250 if (swapuuid) {
251 /* parse uuids are hostendian.
252 * uuid's from some superblocks are big-ending
253 * if there is a difference, we need to swap..
254 */
255 unsigned char *ac = (unsigned char *)a;
256 unsigned char *bc = (unsigned char *)b;
257 int i;
258 for (i=0; i<16; i+= 4) {
259 ac[i+0] = bc[i+3];
260 ac[i+1] = bc[i+2];
261 ac[i+2] = bc[i+1];
262 ac[i+3] = bc[i+0];
263 }
264 } else
265 memcpy(a, b, 16);
266}
64c4757e 267
435d4ebb 268#ifndef MDASSEMBLE
682c7051
NB
269int check_ext2(int fd, char *name)
270{
271 /*
272 * Check for an ext2fs file system.
273 * Superblock is always 1K at 1K offset
274 *
275 * s_magic is le16 at 56 == 0xEF53
276 * report mtime - le32 at 44
277 * blocks - le32 at 4
278 * logblksize - le32 at 24
279 */
280 unsigned char sb[1024];
281 time_t mtime;
282 int size, bsize;
283 if (lseek(fd, 1024,0)!= 1024)
284 return 0;
285 if (read(fd, sb, 1024)!= 1024)
286 return 0;
287 if (sb[56] != 0x53 || sb[57] != 0xef)
288 return 0;
289
290 mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8;
291 bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8;
292 size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8;
293 fprintf(stderr, Name ": %s appears to contain an ext2fs file system\n",
294 name);
295 fprintf(stderr," size=%dK mtime=%s",
296 size*(1<<bsize), ctime(&mtime));
297 return 1;
298}
299
300int check_reiser(int fd, char *name)
301{
302 /*
303 * superblock is at 64K
304 * size is 1024;
305 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
306 *
307 */
308 unsigned char sb[1024];
881990a2 309 unsigned long size;
682c7051
NB
310 if (lseek(fd, 64*1024, 0) != 64*1024)
311 return 0;
312 if (read(fd, sb, 1024) != 1024)
313 return 0;
a46f4061
NB
314 if (strncmp((char*)sb+52, "ReIsErFs",8)!=0 &&
315 strncmp((char*)sb+52, "ReIsEr2Fs",9)!=0)
682c7051
NB
316 return 0;
317 fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name);
318 size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8;
881990a2 319 fprintf(stderr, " size = %luK\n", size*4);
aba69144 320
682c7051
NB
321 return 1;
322}
323
324int check_raid(int fd, char *name)
325{
4b1ac34b 326 struct mdinfo info;
682c7051 327 time_t crtime;
d078d77c 328 char *level;
82d9eba6 329 struct supertype *st = guess_super(fd);
f9ce90ba 330
82d9eba6 331 if (!st) return 0;
3da92f27 332 st->ss->load_super(st, fd, name);
82d9eba6
NB
333 /* Looks like a raid array .. */
334 fprintf(stderr, Name ": %s appears to be part of a raid array:\n",
335 name);
3da92f27
NB
336 st->ss->getinfo_super(st, &info);
337 st->ss->free_super(st);
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
598f0d58
NB
393 * Returns 1 if a partitionable standard,
394 * -1 if non-partitonable,
395 * 0 if not a standard name.
e5329c37 396 */
8d80900b
NB
397 char *d = strrchr(dev, '/');
398 int type=0;
399 int num;
400 if (!d)
e5329c37 401 return 0;
8d80900b
NB
402 if (strncmp(d, "/d",2)==0)
403 d += 2, type=1; /* /dev/md/dN{pM} */
404 else if (strncmp(d, "/md_d", 5)==0)
405 d += 5, type=1; /* /dev/md_dNpM */
406 else if (strncmp(d, "/md", 3)==0)
407 d += 3, type=-1; /* /dev/mdN */
408 else if (d-dev > 3 && strncmp(d-2, "md/", 3)==0)
5a6d1148 409 d += 1, type=-1; /* /dev/md/N */
e5329c37
NB
410 else
411 return 0;
8d80900b 412 if (!*d)
e5329c37 413 return 0;
8d80900b
NB
414 num = atoi(d);
415 while (isdigit(*d))
416 d++;
417 if (*d)
e5329c37 418 return 0;
8d80900b
NB
419 if (nump) *nump = num;
420
421 return type;
e5329c37
NB
422}
423
424
82b27616
NB
425/*
426 * convert a major/minor pair for a block device into a name in /dev, if possible.
427 * On the first call, walk /dev collecting name.
428 * Put them in a simple linked listfor now.
429 */
430struct devmap {
431 int major, minor;
432 char *name;
433 struct devmap *next;
434} *devlist = NULL;
435int devlist_ready = 0;
436
82b27616
NB
437int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
438{
bed256c2
NB
439 struct stat st;
440 if (S_ISLNK(stb->st_mode)) {
441 stat(name, &st);
442 stb = &st;
82b27616 443 }
bed256c2
NB
444
445 if ((stb->st_mode&S_IFMT)== S_IFBLK) {
446 char *n = strdup(name);
447 struct devmap *dm = malloc(sizeof(*dm));
448 if (strncmp(n, "/dev/./", 7)==0)
449 strcpy(n+4, name+6);
450 if (dm) {
451 dm->major = major(stb->st_rdev);
452 dm->minor = minor(stb->st_rdev);
453 dm->name = n;
454 dm->next = devlist;
455 devlist = dm;
456 }
457 }
458 return 0;
82b27616
NB
459}
460
45e878bb
NB
461#ifndef HAVE_NFTW
462#ifdef HAVE_FTW
463int add_dev_1(const char *name, const struct stat *stb, int flag)
464{
465 return add_dev(name, stb, flag, NULL);
466}
467int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
468{
469 return ftw(path, add_dev_1, nopenfd);
470}
471#else
45e878bb
NB
472int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
473{
474 return 0;
475}
476#endif /* HAVE_FTW */
477#endif /* HAVE_NFTW */
478
dd0781e5
NB
479/*
480 * Find a block device with the right major/minor number.
b79713f8
NB
481 * If we find multiple names, choose the shortest.
482 * If we find a non-standard name, it is probably there
483 * deliberately so prefer it over a standard name.
484 * This applies only to names for MD devices.
dd0781e5 485 */
16c6fa80 486char *map_dev(int major, int minor, int create)
82b27616 487{
dd0781e5 488 struct devmap *p;
b79713f8 489 char *std = NULL, *nonstd=NULL;
e7bb5d23 490 int did_check = 0;
eed35d66 491
e81cdd9f 492 if (major == 0 && minor == 0)
eed35d66 493 return NULL;
e81cdd9f 494
e7bb5d23 495 retry:
dd0781e5 496 if (!devlist_ready) {
0a416ec3
NB
497 char *dev = "/dev";
498 struct stat stb;
eed35d66
NB
499 while(devlist) {
500 struct devmap *d = devlist;
501 devlist = d->next;
502 free(d->name);
503 free(d);
504 }
0a416ec3
NB
505 if (lstat(dev, &stb)==0 &&
506 S_ISLNK(stb.st_mode))
507 dev = "/dev/.";
508 nftw(dev, add_dev, 10, FTW_PHYS);
dd0781e5 509 devlist_ready=1;
e7bb5d23 510 did_check = 1;
dd0781e5 511 }
82b27616 512
dd0781e5
NB
513 for (p=devlist; p; p=p->next)
514 if (p->major == major &&
515 p->minor == minor) {
b79713f8
NB
516 if (is_standard(p->name, NULL)) {
517 if (std == NULL ||
518 strlen(p->name) < strlen(std))
519 std = p->name;
520 } else {
521 if (nonstd == NULL ||
522 strlen(p->name) < strlen(nonstd))
523 nonstd = p->name;
524 }
dd0781e5 525 }
e7bb5d23
NB
526 if (!std && !nonstd && !did_check) {
527 devlist_ready = 0;
528 goto retry;
529 }
16c6fa80
NB
530 if (create && !std && !nonstd) {
531 static char buf[30];
382245c3 532 snprintf(buf, sizeof(buf), "%d:%d", major, minor);
16c6fa80
NB
533 nonstd = buf;
534 }
535
b79713f8 536 return nonstd ? nonstd : std;
82b27616
NB
537}
538
4b1ac34b 539unsigned long calc_csum(void *super, int bytes)
82b27616 540{
56eb10c0 541 unsigned long long newcsum = 0;
82b27616 542 int i;
4b1ac34b
NB
543 unsigned int csum;
544 unsigned int *superc = (unsigned int*) super;
82b27616 545
4b1ac34b 546 for(i=0; i<bytes/4; i++)
82b27616
NB
547 newcsum+= superc[i];
548 csum = (newcsum& 0xffffffff) + (newcsum>>32);
570c0542 549#ifdef __alpha__
aba69144 550/* The in-kernel checksum calculation is always 16bit on
570c0542
NB
551 * the alpha, though it is 32 bit on i386...
552 * I wonder what it is elsewhere... (it uses and API in
553 * a way that it shouldn't).
554 */
555 csum = (csum & 0xffff) + (csum >> 16);
556 csum = (csum & 0xffff) + (csum >> 16);
557#endif
82b27616
NB
558 return csum;
559}
cd29a5c8 560
435d4ebb 561#ifndef MDASSEMBLE
56eb10c0 562char *human_size(long long bytes)
cd29a5c8
NB
563{
564 static char buf[30];
d5d3721e
NB
565
566 /* We convert bytes to either centi-M{ega,ibi}bytes or
567 * centi-G{igi,ibi}bytes, with appropriate rounding,
568 * and then print 1/100th of those as a decimal.
569 * We allow upto 2048Megabytes before converting to
570 * gigabytes, as that shows more precision and isn't
571 * too large a number.
572 * Terrabytes are not yet handled.
573 */
cd29a5c8 574
56eb10c0 575 if (bytes < 5000*1024)
cd29a5c8 576 buf[0]=0;
d5d3721e
NB
577 else if (bytes < 2*1024LL*1024LL*1024LL) {
578 long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2;
579 long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2;
8f23b0b3 580 snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)",
d5d3721e
NB
581 cMiB/100 , cMiB % 100,
582 cMB/100, cMB % 100);
583 } else {
584 long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2;
585 long cGB = (bytes / (1000000000LL/200LL ) +1) /2;
8f23b0b3 586 snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)",
d5d3721e
NB
587 cGiB/100 , cGiB % 100,
588 cGB/100, cGB % 100);
589 }
cd29a5c8
NB
590 return buf;
591}
e0d19036
NB
592
593char *human_size_brief(long long bytes)
594{
595 static char buf[30];
e0d19036
NB
596
597 if (bytes < 5000*1024)
8f23b0b3 598 snprintf(buf, sizeof(buf), "%ld.%02ldKiB",
bd526cee 599 (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
e0d19036
NB
600 );
601 else if (bytes < 2*1024LL*1024LL*1024LL)
8f23b0b3 602 snprintf(buf, sizeof(buf), "%ld.%02ldMiB",
e0d19036 603 (long)(bytes>>20),
bd526cee 604 (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
e0d19036
NB
605 );
606 else
8f23b0b3 607 snprintf(buf, sizeof(buf), "%ld.%02ldGiB",
e0d19036 608 (long)(bytes>>30),
bd526cee 609 (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
e0d19036
NB
610 );
611 return buf;
612}
435d4ebb 613#endif
e0d19036 614
5f8097be
NB
615unsigned long long calc_array_size(int level, int raid_disks, int layout,
616 int chunksize, unsigned long long devsize)
617{
618 int data_disks = 0;
619 switch (level) {
620 case 0: data_disks = raid_disks; break;
621 case 1: data_disks = 1; break;
622 case 4:
623 case 5: data_disks = raid_disks - 1; break;
624 case 6: data_disks = raid_disks - 2; break;
625 case 10: data_disks = raid_disks / (layout & 255) / ((layout>>8)&255);
626 break;
627 }
628 devsize &= ~(unsigned long long)((chunksize>>9)-1);
629 return data_disks * devsize;
630}
631
435d4ebb 632#if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
dd0781e5 633int get_mdp_major(void)
98c6faba 634{
dd0781e5
NB
635static int mdp_major = -1;
636 FILE *fl;
98c6faba
NB
637 char *w;
638 int have_block = 0;
639 int have_devices = 0;
640 int last_num = -1;
dd0781e5
NB
641
642 if (mdp_major != -1)
643 return mdp_major;
644 fl = fopen("/proc/devices", "r");
98c6faba 645 if (!fl)
dd0781e5 646 return -1;
98c6faba
NB
647 while ((w = conf_word(fl, 1))) {
648 if (have_block && strcmp(w, "devices:")==0)
649 have_devices = 1;
650 have_block = (strcmp(w, "Block")==0);
651 if (isdigit(w[0]))
652 last_num = atoi(w);
653 if (have_devices && strcmp(w, "mdp")==0)
654 mdp_major = last_num;
655 free(w);
656 }
657 fclose(fl);
dd0781e5 658 return mdp_major;
98c6faba
NB
659}
660
661
e0d19036 662
e0d19036
NB
663char *get_md_name(int dev)
664{
665 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
98c6faba 666 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
e0d19036
NB
667 static char devname[50];
668 struct stat stb;
98c6faba 669 dev_t rdev;
dd0781e5 670 char *dn;
98c6faba
NB
671
672 if (dev < 0) {
dd0781e5
NB
673 int mdp = get_mdp_major();
674 if (mdp < 0) return NULL;
0df46c2a 675 rdev = makedev(mdp, (-1-dev)<<6);
8f23b0b3 676 snprintf(devname, sizeof(devname), "/dev/md/d%d", -1-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 } else {
0df46c2a 682 rdev = makedev(MD_MAJOR, dev);
8f23b0b3 683 snprintf(devname, sizeof(devname), "/dev/md%d", dev);
98c6faba
NB
684 if (stat(devname, &stb) == 0
685 && (S_IFMT&stb.st_mode) == S_IFBLK
686 && (stb.st_rdev == rdev))
687 return devname;
688
8f23b0b3 689 snprintf(devname, sizeof(devname), "/dev/md/%d", dev);
98c6faba
NB
690 if (stat(devname, &stb) == 0
691 && (S_IFMT&stb.st_mode) == S_IFBLK
692 && (stb.st_rdev == rdev))
693 return devname;
694 }
16c6fa80 695 dn = map_dev(major(rdev), minor(rdev), 0);
dd0781e5
NB
696 if (dn)
697 return dn;
8f23b0b3 698 snprintf(devname, sizeof(devname), "/dev/.tmp.md%d", dev);
e0d19036 699 if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
dd0781e5
NB
700 if (errno != EEXIST)
701 return NULL;
e0d19036
NB
702
703 if (stat(devname, &stb) == 0
704 && (S_IFMT&stb.st_mode) == S_IFBLK
705 && (stb.st_rdev == rdev))
706 return devname;
707 unlink(devname);
708 return NULL;
709}
710
711void put_md_name(char *name)
712{
713 if (strncmp(name, "/dev/.tmp.md", 12)==0)
714 unlink(name);
715}
ea24acd0
NB
716
717static int dev2major(int d)
718{
719 if (d >= 0)
720 return MD_MAJOR;
721 else
722 return get_mdp_major();
723}
724
725static int dev2minor(int d)
726{
727 if (d >= 0)
728 return d;
729 return (-1-d) << MdpMinorShift;
730}
731
732int find_free_devnum(int use_partitions)
733{
734 int devnum;
735 for (devnum = 127; devnum != 128;
736 devnum = devnum ? devnum-1 : (1<<22)-1) {
737 char *dn;
738 int _devnum;
739
740 _devnum = use_partitions ? (-1-devnum) : devnum;
741 if (mddev_busy(_devnum))
742 continue;
743 /* make sure it is new to /dev too, at least as a
744 * non-standard */
745 dn = map_dev(dev2major(_devnum), dev2minor(_devnum), 0);
746 if (dn && ! is_standard(dn, NULL))
747 continue;
748 break;
749 }
750 if (devnum == 128)
751 return NoMdDev;
752 return use_partitions ? (-1-devnum) : devnum;
753}
435d4ebb 754#endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
f9ce90ba 755
8b0dabea
NB
756int dev_open(char *dev, int flags)
757{
758 /* like 'open', but if 'dev' matches %d:%d, create a temp
759 * block device and open that
760 */
761 char *e;
762 int fd = -1;
763 char devname[32];
e81cdd9f 764 int major;
8b0dabea 765 int minor;
e81cdd9f
NB
766
767 if (!dev) return -1;
768
769 major = strtoul(dev, &e, 0);
8b0dabea
NB
770 if (e > dev && *e == ':' && e[1] &&
771 (minor = strtoul(e+1, &e, 0)) >= 0 &&
772 *e == 0) {
773 snprintf(devname, sizeof(devname), "/dev/.tmp.md.%d:%d", major, minor);
774 if (mknod(devname, S_IFBLK|0600, makedev(major, minor))==0) {
775 fd = open(devname, flags);
776 unlink(devname);
777 }
778 } else
779 fd = open(dev, flags);
780 return fd;
781}
f9ce90ba 782
a322f70c 783struct superswitch *superlist[] = { &super0, &super1, &super_ddf, NULL };
f9ce90ba 784
ea24acd0 785#if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
1686dc25 786struct supertype *super_by_fd(int fd)
f9ce90ba 787{
1686dc25
NB
788 mdu_array_info_t array;
789 int vers;
790 int minor;
791 struct supertype *st = NULL;
7e0f6979 792 struct mdinfo *sra;
142cb9e1 793 char *verstr;
1686dc25
NB
794 char version[20];
795 int i;
796
797 sra = sysfs_read(fd, 0, GET_VERSION);
798
799 if (sra) {
7e0f6979
NB
800 vers = sra->array.major_version;
801 minor = sra->array.minor_version;
142cb9e1 802 verstr = sra->text_version;
1686dc25
NB
803 } else {
804 if (ioctl(fd, GET_ARRAY_INFO, &array))
805 array.major_version = array.minor_version = 0;
806 vers = array.major_version;
807 minor = array.minor_version;
142cb9e1 808 verstr = "";
6fbba4c9 809 }
82d9eba6 810
1686dc25
NB
811 if (vers != -1) {
812 sprintf(version, "%d.%d", vers, minor);
813 verstr = version;
6fbba4c9 814 }
1686dc25
NB
815 for (i = 0; st == NULL && superlist[i] ; i++)
816 st = superlist[i]->match_metadata_desc(verstr);
817
818 if (sra)
819 sysfs_free(sra);
3b0896f8
NB
820 if (st)
821 st->sb = NULL;
82d9eba6 822 return st;
f9ce90ba 823}
ea24acd0
NB
824#endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
825
f9ce90ba 826
3da92f27
NB
827struct supertype *dup_super(struct supertype *st)
828{
1686dc25
NB
829 struct supertype *stnew = NULL;
830 char *verstr = NULL;
831 char version[20];
832 int i;
833
3da92f27
NB
834 if (!st)
835 return st;
1686dc25 836
d03373f1
NB
837 if (st->ss->text_version)
838 strcpy(version, st->ss->text_version);
839 else if (st->minor_version == -1)
1686dc25
NB
840 sprintf(version, "%d", st->ss->major);
841 else
842 sprintf(version, "%d.%d", st->ss->major, st->minor_version);
843 verstr = version;
844
845 for (i = 0; stnew == NULL && superlist[i] ; i++)
846 stnew = superlist[i]->match_metadata_desc(verstr);
847
ff1f6545
NB
848 if (stnew)
849 stnew->sb = NULL;
1686dc25 850 return stnew;
3da92f27
NB
851}
852
82d9eba6 853struct supertype *guess_super(int fd)
f9ce90ba
NB
854{
855 /* try each load_super to find the best match,
856 * and return the best superswitch
857 */
82d9eba6
NB
858 struct superswitch *ss;
859 struct supertype *st;
570c0542
NB
860 unsigned long besttime = 0;
861 int bestsuper = -1;
f9ce90ba
NB
862 int i;
863
82d9eba6
NB
864 st = malloc(sizeof(*st));
865 memset(st, 0, sizeof(*st));
f9ce90ba
NB
866 for (i=0 ; superlist[i]; i++) {
867 int rv;
868 ss = superlist[i];
f277ce36 869 st->ss = NULL;
3da92f27 870 rv = ss->load_super(st, fd, NULL);
570c0542
NB
871 if (rv == 0) {
872 struct mdinfo info;
3da92f27 873 st->ss->getinfo_super(st, &info);
570c0542
NB
874 if (bestsuper == -1 ||
875 besttime < info.array.ctime) {
876 bestsuper = i;
877 besttime = info.array.ctime;
570c0542 878 }
3da92f27 879 ss->free_super(st);
570c0542
NB
880 }
881 }
882 if (bestsuper != -1) {
883 int rv;
f277ce36 884 st->ss = NULL;
3da92f27 885 rv = superlist[bestsuper]->load_super(st, fd, NULL);
f9ce90ba 886 if (rv == 0) {
5e747af2 887 superlist[bestsuper]->free_super(st);
82d9eba6 888 return st;
f9ce90ba
NB
889 }
890 }
570c0542 891 free(st);
f9ce90ba
NB
892 return NULL;
893}
fe6729fa 894
beae1dfe
NB
895/* Return size of device in bytes */
896int get_dev_size(int fd, char *dname, unsigned long long *sizep)
897{
898 unsigned long long ldsize;
c2c9bb6f
NB
899 struct stat st;
900
901 if (fstat(fd, &st) != -1 && S_ISREG(st.st_mode))
902 ldsize = (unsigned long long)st.st_size;
903 else
beae1dfe
NB
904#ifdef BLKGETSIZE64
905 if (ioctl(fd, BLKGETSIZE64, &ldsize) != 0)
906#endif
907 {
908 unsigned long dsize;
909 if (ioctl(fd, BLKGETSIZE, &dsize) == 0) {
910 ldsize = dsize;
911 ldsize <<= 9;
912 } else {
913 if (dname)
914 fprintf(stderr, Name ": Cannot get size of %s: %s\b",
915 dname, strerror(errno));
916 return 0;
917 }
918 }
919 *sizep = ldsize;
920 return 1;
921}
8fac0577 922
8382f19b
NB
923void get_one_disk(int mdfd, mdu_array_info_t *ainf, mdu_disk_info_t *disk)
924{
925 int d;
926 ioctl(mdfd, GET_ARRAY_INFO, ainf);
927 for (d = 0 ; d < ainf->raid_disks + ainf->nr_disks ; d++)
928 if (ioctl(mdfd, GET_DISK_INFO, disk) == 0)
929 return;
930}
63152c1b 931
a322f70c
DW
932int open_container(int fd)
933{
934 /* 'fd' is a block device. Find out if it is in use
935 * by a container, and return an open fd on that container.
936 */
937 char path[256];
938 char *e;
939 DIR *dir;
940 struct dirent *de;
941 int dfd, n;
942 char buf[200];
943 int major, minor;
944 struct stat st;
945
946 if (fstat(fd, &st) != 0)
947 return -1;
948 sprintf(path, "/sys/dev/block/%d:%d/holders",
949 (int)major(st.st_rdev), (int)minor(st.st_rdev));
950 e = path + strlen(path);
951
952 dir = opendir(path);
953 if (!dir)
954 return -1;
955 while ((de = readdir(dir))) {
956 if (de->d_ino == 0)
957 continue;
958 if (de->d_name[0] == '.')
959 continue;
960 sprintf(e, "/%s/dev", de->d_name);
961 dfd = open(path, O_RDONLY);
962 if (dfd < 0)
963 continue;
964 n = read(dfd, buf, sizeof(buf));
965 close(dfd);
966 if (n <= 0 || n >= sizeof(buf))
967 continue;
968 buf[n] = 0;
969 if (sscanf(buf, "%d:%d", &major, &minor) != 2)
970 continue;
971 sprintf(buf, "%d:%d", major, minor);
972 dfd = dev_open(buf, O_RDONLY);
973 if (dfd >= 0) {
974 closedir(dir);
975 return dfd;
976 }
977 }
978 return -1;
979}
980
fe6729fa
NB
981#ifdef __TINYC__
982/* tinyc doesn't optimize this check in ioctl.h out ... */
983unsigned int __invalid_size_argument_for_IOC = 0;
984#endif
985