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