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