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