]> git.ipfire.org Git - thirdparty/mdadm.git/blob - util.c
Free mdstat data structures properly.
[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_dN{pM} */
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
437 if (S_ISLNK(stb->st_mode)) {
438 if (stat(name, &st) != 0)
439 return 0;
440 stb = &st;
441 }
442
443 if ((stb->st_mode&S_IFMT)== S_IFBLK) {
444 char *n = strdup(name);
445 struct devmap *dm = malloc(sizeof(*dm));
446 if (strncmp(n, "/dev/./", 7)==0)
447 strcpy(n+4, name+6);
448 if (dm) {
449 dm->major = major(stb->st_rdev);
450 dm->minor = minor(stb->st_rdev);
451 dm->name = n;
452 dm->next = devlist;
453 devlist = dm;
454 }
455 }
456 return 0;
457 }
458
459 #ifndef HAVE_NFTW
460 #ifdef HAVE_FTW
461 int add_dev_1(const char *name, const struct stat *stb, int flag)
462 {
463 return add_dev(name, stb, flag, NULL);
464 }
465 int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
466 {
467 return ftw(path, add_dev_1, nopenfd);
468 }
469 #else
470 int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
471 {
472 return 0;
473 }
474 #endif /* HAVE_FTW */
475 #endif /* HAVE_NFTW */
476
477 /*
478 * Find a block device with the right major/minor number.
479 * If we find multiple names, choose the shortest.
480 * If we find a non-standard name, it is probably there
481 * deliberately so prefer it over a standard name.
482 * This applies only to names for MD devices.
483 */
484 char *map_dev(int major, int minor, int create)
485 {
486 struct devmap *p;
487 char *std = NULL, *nonstd=NULL;
488 int did_check = 0;
489
490 if (major == 0 && minor == 0)
491 return NULL;
492
493 retry:
494 if (!devlist_ready) {
495 char *dev = "/dev";
496 struct stat stb;
497 while(devlist) {
498 struct devmap *d = devlist;
499 devlist = d->next;
500 free(d->name);
501 free(d);
502 }
503 if (lstat(dev, &stb)==0 &&
504 S_ISLNK(stb.st_mode))
505 dev = "/dev/.";
506 nftw(dev, add_dev, 10, FTW_PHYS);
507 devlist_ready=1;
508 did_check = 1;
509 }
510
511 for (p=devlist; p; p=p->next)
512 if (p->major == major &&
513 p->minor == minor) {
514 if (is_standard(p->name, NULL)) {
515 if (std == NULL ||
516 strlen(p->name) < strlen(std))
517 std = p->name;
518 } else {
519 if (nonstd == NULL ||
520 strlen(p->name) < strlen(nonstd))
521 nonstd = p->name;
522 }
523 }
524 if (!std && !nonstd && !did_check) {
525 devlist_ready = 0;
526 goto retry;
527 }
528 if (create && !std && !nonstd) {
529 static char buf[30];
530 snprintf(buf, sizeof(buf), "%d:%d", major, minor);
531 nonstd = buf;
532 }
533
534 return nonstd ? nonstd : std;
535 }
536
537 unsigned long calc_csum(void *super, int bytes)
538 {
539 unsigned long long newcsum = 0;
540 int i;
541 unsigned int csum;
542 unsigned int *superc = (unsigned int*) super;
543
544 for(i=0; i<bytes/4; i++)
545 newcsum+= superc[i];
546 csum = (newcsum& 0xffffffff) + (newcsum>>32);
547 #ifdef __alpha__
548 /* The in-kernel checksum calculation is always 16bit on
549 * the alpha, though it is 32 bit on i386...
550 * I wonder what it is elsewhere... (it uses and API in
551 * a way that it shouldn't).
552 */
553 csum = (csum & 0xffff) + (csum >> 16);
554 csum = (csum & 0xffff) + (csum >> 16);
555 #endif
556 return csum;
557 }
558
559 #ifndef MDASSEMBLE
560 char *human_size(long long bytes)
561 {
562 static char buf[30];
563
564 /* We convert bytes to either centi-M{ega,ibi}bytes or
565 * centi-G{igi,ibi}bytes, with appropriate rounding,
566 * and then print 1/100th of those as a decimal.
567 * We allow upto 2048Megabytes before converting to
568 * gigabytes, as that shows more precision and isn't
569 * too large a number.
570 * Terrabytes are not yet handled.
571 */
572
573 if (bytes < 5000*1024)
574 buf[0]=0;
575 else if (bytes < 2*1024LL*1024LL*1024LL) {
576 long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2;
577 long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2;
578 snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)",
579 cMiB/100 , cMiB % 100,
580 cMB/100, cMB % 100);
581 } else {
582 long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2;
583 long cGB = (bytes / (1000000000LL/200LL ) +1) /2;
584 snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)",
585 cGiB/100 , cGiB % 100,
586 cGB/100, cGB % 100);
587 }
588 return buf;
589 }
590
591 char *human_size_brief(long long bytes)
592 {
593 static char buf[30];
594
595 if (bytes < 5000*1024)
596 snprintf(buf, sizeof(buf), "%ld.%02ldKiB",
597 (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
598 );
599 else if (bytes < 2*1024LL*1024LL*1024LL)
600 snprintf(buf, sizeof(buf), "%ld.%02ldMiB",
601 (long)(bytes>>20),
602 (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
603 );
604 else
605 snprintf(buf, sizeof(buf), "%ld.%02ldGiB",
606 (long)(bytes>>30),
607 (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
608 );
609 return buf;
610 }
611
612 void print_r10_layout(int layout)
613 {
614 int near = layout & 255;
615 int far = (layout >> 8) & 255;
616 int offset = (layout&0x10000);
617 char *sep = "";
618
619 if (near != 1) {
620 printf("%s near=%d", sep, near);
621 sep = ",";
622 }
623 if (far != 1)
624 printf("%s %s=%d", sep, offset?"offset":"far", far);
625 if (near*far == 1)
626 printf("NO REDUNDANCY");
627 }
628 #endif
629
630 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
631 int get_mdp_major(void)
632 {
633 static int mdp_major = -1;
634 FILE *fl;
635 char *w;
636 int have_block = 0;
637 int have_devices = 0;
638 int last_num = -1;
639
640 if (mdp_major != -1)
641 return mdp_major;
642 fl = fopen("/proc/devices", "r");
643 if (!fl)
644 return -1;
645 while ((w = conf_word(fl, 1))) {
646 if (have_block && strcmp(w, "devices:")==0)
647 have_devices = 1;
648 have_block = (strcmp(w, "Block")==0);
649 if (isdigit(w[0]))
650 last_num = atoi(w);
651 if (have_devices && strcmp(w, "mdp")==0)
652 mdp_major = last_num;
653 free(w);
654 }
655 fclose(fl);
656 return mdp_major;
657 }
658
659
660
661 char *get_md_name(int dev)
662 {
663 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
664 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
665 static char devname[50];
666 struct stat stb;
667 dev_t rdev;
668 char *dn;
669
670 if (dev < 0) {
671 int mdp = get_mdp_major();
672 if (mdp < 0) return NULL;
673 rdev = makedev(mdp, (-1-dev)<<6);
674 snprintf(devname, sizeof(devname), "/dev/md/d%d", -1-dev);
675 if (stat(devname, &stb) == 0
676 && (S_IFMT&stb.st_mode) == S_IFBLK
677 && (stb.st_rdev == rdev))
678 return devname;
679 } else {
680 rdev = makedev(MD_MAJOR, dev);
681 snprintf(devname, sizeof(devname), "/dev/md%d", dev);
682 if (stat(devname, &stb) == 0
683 && (S_IFMT&stb.st_mode) == S_IFBLK
684 && (stb.st_rdev == rdev))
685 return devname;
686
687 snprintf(devname, sizeof(devname), "/dev/md/%d", dev);
688 if (stat(devname, &stb) == 0
689 && (S_IFMT&stb.st_mode) == S_IFBLK
690 && (stb.st_rdev == rdev))
691 return devname;
692 }
693 dn = map_dev(major(rdev), minor(rdev), 0);
694 if (dn)
695 return dn;
696 snprintf(devname, sizeof(devname), "/dev/.tmp.md%d", dev);
697 if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
698 if (errno != EEXIST)
699 return NULL;
700
701 if (stat(devname, &stb) == 0
702 && (S_IFMT&stb.st_mode) == S_IFBLK
703 && (stb.st_rdev == rdev))
704 return devname;
705 unlink(devname);
706 return NULL;
707 }
708
709 void put_md_name(char *name)
710 {
711 if (strncmp(name, "/dev/.tmp.md", 12)==0)
712 unlink(name);
713 }
714
715 static int dev2major(int d)
716 {
717 if (d >= 0)
718 return MD_MAJOR;
719 else
720 return get_mdp_major();
721 }
722
723 static int dev2minor(int d)
724 {
725 if (d >= 0)
726 return d;
727 return (-1-d) << MdpMinorShift;
728 }
729
730 int find_free_devnum(int use_partitions)
731 {
732 int devnum;
733 for (devnum = 127; devnum != 128;
734 devnum = devnum ? devnum-1 : (1<<22)-1) {
735 char *dn;
736 int _devnum;
737
738 _devnum = use_partitions ? (-1-devnum) : devnum;
739 if (mddev_busy(_devnum))
740 continue;
741 /* make sure it is new to /dev too, at least as a
742 * non-standard */
743 dn = map_dev(dev2major(_devnum), dev2minor(_devnum), 0);
744 if (dn && ! is_standard(dn, NULL))
745 continue;
746 break;
747 }
748 if (devnum == 128)
749 return NoMdDev;
750 return use_partitions ? (-1-devnum) : devnum;
751 }
752 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
753
754 int dev_open(char *dev, int flags)
755 {
756 /* like 'open', but if 'dev' matches %d:%d, create a temp
757 * block device and open that
758 */
759 char *e;
760 int fd = -1;
761 char devname[32];
762 int major;
763 int minor;
764
765 if (!dev) return -1;
766
767 major = strtoul(dev, &e, 0);
768 if (e > dev && *e == ':' && e[1] &&
769 (minor = strtoul(e+1, &e, 0)) >= 0 &&
770 *e == 0) {
771 snprintf(devname, sizeof(devname), "/dev/.tmp.md.%d:%d", major, minor);
772 if (mknod(devname, S_IFBLK|0600, makedev(major, minor))==0) {
773 fd = open(devname, flags);
774 unlink(devname);
775 }
776 } else
777 fd = open(dev, flags);
778 return fd;
779 }
780
781 struct superswitch *superlist[] = { &super0, &super1, NULL };
782
783 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
784 struct supertype *super_by_fd(int fd)
785 {
786 mdu_array_info_t array;
787 int vers;
788 int minor;
789 struct supertype *st = NULL;
790 struct mdinfo *sra;
791 char *verstr;
792 char version[20];
793 int i;
794
795 sra = sysfs_read(fd, 0, GET_VERSION);
796
797 if (sra) {
798 vers = sra->array.major_version;
799 minor = sra->array.minor_version;
800 verstr = sra->text_version;
801 } else {
802 if (ioctl(fd, GET_ARRAY_INFO, &array))
803 array.major_version = array.minor_version = 0;
804 vers = array.major_version;
805 minor = array.minor_version;
806 verstr = "";
807 }
808
809 if (vers != -1) {
810 sprintf(version, "%d.%d", vers, minor);
811 verstr = version;
812 }
813 for (i = 0; st == NULL && superlist[i] ; i++)
814 st = superlist[i]->match_metadata_desc(verstr);
815
816 if (sra)
817 sysfs_free(sra);
818 if (st)
819 st->sb = NULL;
820 return st;
821 }
822 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
823
824
825 struct supertype *dup_super(struct supertype *st)
826 {
827 struct supertype *stnew = NULL;
828 char *verstr = NULL;
829 char version[20];
830 int i;
831
832 if (!st)
833 return st;
834
835 if (st->minor_version == -1)
836 sprintf(version, "%d", st->ss->major);
837 else
838 sprintf(version, "%d.%d", st->ss->major, st->minor_version);
839 verstr = version;
840
841 for (i = 0; stnew == NULL && superlist[i] ; i++)
842 stnew = superlist[i]->match_metadata_desc(verstr);
843
844 if (stnew)
845 stnew->sb = NULL;
846 return stnew;
847 }
848
849 struct supertype *guess_super(int fd)
850 {
851 /* try each load_super to find the best match,
852 * and return the best superswitch
853 */
854 struct superswitch *ss;
855 struct supertype *st;
856 unsigned long besttime = 0;
857 int bestsuper = -1;
858 int i;
859
860 st = malloc(sizeof(*st));
861 memset(st, 0, sizeof(*st));
862 for (i=0 ; superlist[i]; i++) {
863 int rv;
864 ss = superlist[i];
865 st->ss = NULL;
866 rv = ss->load_super(st, fd, NULL);
867 if (rv == 0) {
868 struct mdinfo info;
869 st->ss->getinfo_super(st, &info);
870 if (bestsuper == -1 ||
871 besttime < info.array.ctime) {
872 bestsuper = i;
873 besttime = info.array.ctime;
874 }
875 ss->free_super(st);
876 }
877 }
878 if (bestsuper != -1) {
879 int rv;
880 st->ss = NULL;
881 rv = superlist[bestsuper]->load_super(st, fd, NULL);
882 if (rv == 0) {
883 superlist[bestsuper]->free_super(st);
884 return st;
885 }
886 }
887 free(st);
888 return NULL;
889 }
890
891 /* Return size of device in bytes */
892 int get_dev_size(int fd, char *dname, unsigned long long *sizep)
893 {
894 unsigned long long ldsize;
895 struct stat st;
896
897 if (fstat(fd, &st) != -1 && S_ISREG(st.st_mode))
898 ldsize = (unsigned long long)st.st_size;
899 else
900 #ifdef BLKGETSIZE64
901 if (ioctl(fd, BLKGETSIZE64, &ldsize) != 0)
902 #endif
903 {
904 unsigned long dsize;
905 if (ioctl(fd, BLKGETSIZE, &dsize) == 0) {
906 ldsize = dsize;
907 ldsize <<= 9;
908 } else {
909 if (dname)
910 fprintf(stderr, Name ": Cannot get size of %s: %s\b",
911 dname, strerror(errno));
912 return 0;
913 }
914 }
915 *sizep = ldsize;
916 return 1;
917 }
918
919 void get_one_disk(int mdfd, mdu_array_info_t *ainf, mdu_disk_info_t *disk)
920 {
921 int d;
922 ioctl(mdfd, GET_ARRAY_INFO, ainf);
923 for (d = 0 ; d < ainf->raid_disks + ainf->nr_disks ; d++)
924 if (ioctl(mdfd, GET_DISK_INFO, disk) == 0)
925 return;
926 }
927
928 #ifdef __TINYC__
929 /* tinyc doesn't optimize this check in ioctl.h out ... */
930 unsigned int __invalid_size_argument_for_IOC = 0;
931 #endif
932