Fix possible NULL dereference in super_by_fd
[thirdparty/mdadm.git] / util.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
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
30 #include "mdadm.h"
31 #include "md_p.h"
32 #include <sys/utsname.h>
33 #include <ctype.h>
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 */
43 struct 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 */
59 struct 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 };
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 */
74 int 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
93 if (hit<32) {
94 uuid[hit/8] <<= 4;
95 uuid[hit/8] += n;
96 }
97 hit++;
98 }
99 if (hit == 32)
100 return 1;
101 return 0;
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
115 int 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)
126 return (vers.major*10000) + (vers.minor*100) + vers.patchlevel;
127 if (errno == EACCES)
128 return -1;
129 if (major(stb.st_rdev) == MD_MAJOR)
130 return (3600);
131 return -1;
132 }
133
134 int get_linux_version()
135 {
136 struct utsname name;
137 char *cp;
138 int a,b,c;
139 if (uname(&name) <0)
140 return -1;
141
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
149 return (a*1000000)+(b*1000)+c;
150 }
151
152 void 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
171 int enough(int level, int raid_disks, int layout, int clean,
172 char *avail, int avail_disks)
173 {
174 int copies, first;
175 switch (level) {
176 case 10:
177 /* This is the tricky one - we need to check
178 * which actual disks are present.
179 */
180 copies = (layout&255)* ((layout>>8) & 255);
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;
196
197 case -4:
198 return avail_disks>= 1;
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:
206 if (clean)
207 return avail_disks >= raid_disks-1;
208 else
209 return avail_disks >= raid_disks;
210 case 6:
211 if (clean)
212 return avail_disks >= raid_disks-2;
213 else
214 return avail_disks >= raid_disks;
215 default:
216 return 0;
217 }
218 }
219
220 int same_uuid(int a[4], int b[4], int swapuuid)
221 {
222 if (swapuuid) {
223 /* parse uuids are hostendian.
224 * uuid's from some superblocks are big-ending
225 * if there is a difference, we need to swap..
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 }
246 }
247 void 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 }
266
267 #ifndef MDASSEMBLE
268 int 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
299 int 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];
308 unsigned long size;
309 if (lseek(fd, 64*1024, 0) != 64*1024)
310 return 0;
311 if (read(fd, sb, 1024) != 1024)
312 return 0;
313 if (strncmp((char*)sb+52, "ReIsErFs",8)!=0 &&
314 strncmp((char*)sb+52, "ReIsEr2Fs",9)!=0)
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;
318 fprintf(stderr, " size = %luK\n", size*4);
319
320 return 1;
321 }
322
323 int check_raid(int fd, char *name)
324 {
325 struct mdinfo info;
326 time_t crtime;
327 char *level;
328 struct supertype *st = guess_super(fd);
329
330 if (!st) return 0;
331 st->ss->load_super(st, fd, name);
332 /* Looks like a raid array .. */
333 fprintf(stderr, Name ": %s appears to be part of a raid array:\n",
334 name);
335 st->ss->getinfo_super(st, &info);
336 st->ss->free_super(st);
337 crtime = info.array.ctime;
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));
342 return 1;
343 }
344
345 int 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 }
364 #endif /* MDASSEMBLE */
365
366 char *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
376 int 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 }
383 return UnSet;
384 }
385
386
387 int is_standard(char *dev, int *nump)
388 {
389 /* tests if dev is a "standard" md dev name.
390 * i.e if the last component is "/dNN" or "/mdNN",
391 * where NN is a string of digits
392 */
393 char *d = strrchr(dev, '/');
394 int type=0;
395 int num;
396 if (!d)
397 return 0;
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)
405 d += 1, type=-1; /* /dev/md/N */
406 else
407 return 0;
408 if (!*d)
409 return 0;
410 num = atoi(d);
411 while (isdigit(*d))
412 d++;
413 if (*d)
414 return 0;
415 if (nump) *nump = num;
416
417 return type;
418 }
419
420
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 */
426 struct devmap {
427 int major, minor;
428 char *name;
429 struct devmap *next;
430 } *devlist = NULL;
431 int devlist_ready = 0;
432
433 int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
434 {
435 struct stat st;
436 if (S_ISLNK(stb->st_mode)) {
437 stat(name, &st);
438 stb = &st;
439 }
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;
455 }
456
457 #ifndef HAVE_NFTW
458 #ifdef HAVE_FTW
459 int add_dev_1(const char *name, const struct stat *stb, int flag)
460 {
461 return add_dev(name, stb, flag, NULL);
462 }
463 int 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
468 int 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
475 /*
476 * Find a block device with the right major/minor number.
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.
481 */
482 char *map_dev(int major, int minor, int create)
483 {
484 struct devmap *p;
485 char *std = NULL, *nonstd=NULL;
486 int did_check = 0;
487
488 if (major == 0 && minor == 0)
489 return NULL;
490
491 retry:
492 if (!devlist_ready) {
493 char *dev = "/dev";
494 struct stat stb;
495 while(devlist) {
496 struct devmap *d = devlist;
497 devlist = d->next;
498 free(d->name);
499 free(d);
500 }
501 if (lstat(dev, &stb)==0 &&
502 S_ISLNK(stb.st_mode))
503 dev = "/dev/.";
504 nftw(dev, add_dev, 10, FTW_PHYS);
505 devlist_ready=1;
506 did_check = 1;
507 }
508
509 for (p=devlist; p; p=p->next)
510 if (p->major == major &&
511 p->minor == minor) {
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 }
521 }
522 if (!std && !nonstd && !did_check) {
523 devlist_ready = 0;
524 goto retry;
525 }
526 if (create && !std && !nonstd) {
527 static char buf[30];
528 snprintf(buf, sizeof(buf), "%d:%d", major, minor);
529 nonstd = buf;
530 }
531
532 return nonstd ? nonstd : std;
533 }
534
535 unsigned long calc_csum(void *super, int bytes)
536 {
537 unsigned long long newcsum = 0;
538 int i;
539 unsigned int csum;
540 unsigned int *superc = (unsigned int*) super;
541
542 for(i=0; i<bytes/4; i++)
543 newcsum+= superc[i];
544 csum = (newcsum& 0xffffffff) + (newcsum>>32);
545 #ifdef __alpha__
546 /* The in-kernel checksum calculation is always 16bit on
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
554 return csum;
555 }
556
557 #ifndef MDASSEMBLE
558 char *human_size(long long bytes)
559 {
560 static char buf[30];
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 */
570
571 if (bytes < 5000*1024)
572 buf[0]=0;
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;
576 snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)",
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;
582 snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)",
583 cGiB/100 , cGiB % 100,
584 cGB/100, cGB % 100);
585 }
586 return buf;
587 }
588
589 char *human_size_brief(long long bytes)
590 {
591 static char buf[30];
592
593 if (bytes < 5000*1024)
594 snprintf(buf, sizeof(buf), "%ld.%02ldKiB",
595 (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
596 );
597 else if (bytes < 2*1024LL*1024LL*1024LL)
598 snprintf(buf, sizeof(buf), "%ld.%02ldMiB",
599 (long)(bytes>>20),
600 (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
601 );
602 else
603 snprintf(buf, sizeof(buf), "%ld.%02ldGiB",
604 (long)(bytes>>30),
605 (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
606 );
607 return buf;
608 }
609 #endif
610
611 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
612 int get_mdp_major(void)
613 {
614 static int mdp_major = -1;
615 FILE *fl;
616 char *w;
617 int have_block = 0;
618 int have_devices = 0;
619 int last_num = -1;
620
621 if (mdp_major != -1)
622 return mdp_major;
623 fl = fopen("/proc/devices", "r");
624 if (!fl)
625 return -1;
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);
637 return mdp_major;
638 }
639
640
641
642 char *get_md_name(int dev)
643 {
644 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
645 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
646 static char devname[50];
647 struct stat stb;
648 dev_t rdev;
649 char *dn;
650
651 if (dev < 0) {
652 int mdp = get_mdp_major();
653 if (mdp < 0) return NULL;
654 rdev = makedev(mdp, (-1-dev)<<6);
655 snprintf(devname, sizeof(devname), "/dev/md/d%d", -1-dev);
656 if (stat(devname, &stb) == 0
657 && (S_IFMT&stb.st_mode) == S_IFBLK
658 && (stb.st_rdev == rdev))
659 return devname;
660 } else {
661 rdev = makedev(MD_MAJOR, dev);
662 snprintf(devname, sizeof(devname), "/dev/md%d", dev);
663 if (stat(devname, &stb) == 0
664 && (S_IFMT&stb.st_mode) == S_IFBLK
665 && (stb.st_rdev == rdev))
666 return devname;
667
668 snprintf(devname, sizeof(devname), "/dev/md/%d", dev);
669 if (stat(devname, &stb) == 0
670 && (S_IFMT&stb.st_mode) == S_IFBLK
671 && (stb.st_rdev == rdev))
672 return devname;
673 }
674 dn = map_dev(major(rdev), minor(rdev), 0);
675 if (dn)
676 return dn;
677 snprintf(devname, sizeof(devname), "/dev/.tmp.md%d", dev);
678 if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
679 if (errno != EEXIST)
680 return NULL;
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
690 void put_md_name(char *name)
691 {
692 if (strncmp(name, "/dev/.tmp.md", 12)==0)
693 unlink(name);
694 }
695 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
696
697 int 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];
705 int major;
706 int minor;
707
708 if (!dev) return -1;
709
710 major = strtoul(dev, &e, 0);
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 }
723
724 struct superswitch *superlist[] = { &super0, &super1, NULL };
725
726 struct supertype *super_by_fd(int fd)
727 {
728 mdu_array_info_t array;
729 int vers;
730 int minor;
731 struct supertype *st = NULL;
732 struct mdinfo *sra;
733 char *verstr;
734 char version[20];
735 int i;
736
737 sra = sysfs_read(fd, 0, GET_VERSION);
738
739 if (sra) {
740 vers = sra->array.major_version;
741 minor = sra->array.minor_version;
742 verstr = sra->text_version;
743 } else {
744 if (ioctl(fd, GET_ARRAY_INFO, &array))
745 array.major_version = array.minor_version = 0;
746 vers = array.major_version;
747 minor = array.minor_version;
748 verstr = "";
749 }
750
751 if (vers != -1) {
752 sprintf(version, "%d.%d", vers, minor);
753 verstr = version;
754 }
755 for (i = 0; st == NULL && superlist[i] ; i++)
756 st = superlist[i]->match_metadata_desc(verstr);
757
758 if (sra)
759 sysfs_free(sra);
760 if (st)
761 st->sb = NULL;
762 return st;
763 }
764
765 struct supertype *dup_super(struct supertype *st)
766 {
767 struct supertype *stnew = NULL;
768 char *verstr = NULL;
769 char version[20];
770 int i;
771
772 if (!st)
773 return st;
774
775 if (st->minor_version == -1)
776 sprintf(version, "%d", st->ss->major);
777 else
778 sprintf(version, "%d.%d", st->ss->major, st->minor_version);
779 verstr = version;
780
781 for (i = 0; stnew == NULL && superlist[i] ; i++)
782 stnew = superlist[i]->match_metadata_desc(verstr);
783
784 stnew->sb = NULL;
785 return stnew;
786 }
787
788 struct supertype *guess_super(int fd)
789 {
790 /* try each load_super to find the best match,
791 * and return the best superswitch
792 */
793 struct superswitch *ss;
794 struct supertype *st;
795 unsigned long besttime = 0;
796 int bestsuper = -1;
797 int i;
798
799 st = malloc(sizeof(*st));
800 memset(st, 0, sizeof(*st));
801 for (i=0 ; superlist[i]; i++) {
802 int rv;
803 ss = superlist[i];
804 st->ss = NULL;
805 rv = ss->load_super(st, fd, NULL);
806 if (rv == 0) {
807 struct mdinfo info;
808 st->ss->getinfo_super(st, &info);
809 if (bestsuper == -1 ||
810 besttime < info.array.ctime) {
811 bestsuper = i;
812 besttime = info.array.ctime;
813 }
814 ss->free_super(st);
815 }
816 }
817 if (bestsuper != -1) {
818 int rv;
819 st->ss = NULL;
820 rv = superlist[bestsuper]->load_super(st, fd, NULL);
821 if (rv == 0) {
822 superlist[bestsuper]->free_super(st);
823 return st;
824 }
825 }
826 free(st);
827 return NULL;
828 }
829
830 /* Return size of device in bytes */
831 int get_dev_size(int fd, char *dname, unsigned long long *sizep)
832 {
833 unsigned long long ldsize;
834 struct stat st;
835
836 if (fstat(fd, &st) != -1 && S_ISREG(st.st_mode))
837 ldsize = (unsigned long long)st.st_size;
838 else
839 #ifdef BLKGETSIZE64
840 if (ioctl(fd, BLKGETSIZE64, &ldsize) != 0)
841 #endif
842 {
843 unsigned long dsize;
844 if (ioctl(fd, BLKGETSIZE, &dsize) == 0) {
845 ldsize = dsize;
846 ldsize <<= 9;
847 } else {
848 if (dname)
849 fprintf(stderr, Name ": Cannot get size of %s: %s\b",
850 dname, strerror(errno));
851 return 0;
852 }
853 }
854 *sizep = ldsize;
855 return 1;
856 }
857
858 void get_one_disk(int mdfd, mdu_array_info_t *ainf, mdu_disk_info_t *disk)
859 {
860 int d;
861 ioctl(mdfd, GET_ARRAY_INFO, ainf);
862 for (d = 0 ; d < ainf->raid_disks + ainf->nr_disks ; d++)
863 if (ioctl(mdfd, GET_DISK_INFO, disk) == 0)
864 return;
865 }
866
867 static int dev2major(int d)
868 {
869 if (d >= 0)
870 return MD_MAJOR;
871 else
872 return get_mdp_major();
873 }
874
875 static int dev2minor(int d)
876 {
877 if (d >= 0)
878 return d;
879 return (-1-d) << MdpMinorShift;
880 }
881
882 int find_free_devnum(int use_partitions)
883 {
884 int devnum;
885 for (devnum = 127; devnum != 128;
886 devnum = devnum ? devnum-1 : (1<<22)-1) {
887 char *dn;
888 int _devnum;
889
890 _devnum = use_partitions ? (-1-devnum) : devnum;
891 if (mddev_busy(_devnum))
892 continue;
893 /* make sure it is new to /dev too, at least as a
894 * non-standard */
895 dn = map_dev(dev2major(_devnum), dev2minor(_devnum), 0);
896 if (dn && ! is_standard(dn, NULL))
897 continue;
898 break;
899 }
900 if (devnum == 128)
901 return NoMdDev;
902 return use_partitions ? (-1-devnum) : devnum;
903 }
904
905
906 #ifdef __TINYC__
907 /* tinyc doesn't optimize this check in ioctl.h out ... */
908 unsigned int __invalid_size_argument_for_IOC = 0;
909 #endif
910