]> git.ipfire.org Git - thirdparty/mdadm.git/blob - util.c
Release 2-5-4
[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 <linux/blkpg.h>
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 */
42 int 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
61 if (hit<32) {
62 uuid[hit/8] <<= 4;
63 uuid[hit/8] += n;
64 }
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
84 int 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)
95 return (vers.major*10000) + (vers.minor*100) + vers.patchlevel;
96 if (errno == EACCES)
97 return -1;
98 if (major(stb.st_rdev) == MD_MAJOR)
99 return (3600);
100 return -1;
101 }
102
103
104 int get_linux_version()
105 {
106 struct utsname name;
107 char *cp;
108 int a,b,c;
109 if (uname(&name) <0)
110 return -1;
111
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
119 return (a*1000000)+(b*1000)+c;
120 }
121
122 void 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
141 int enough(int level, int raid_disks, int layout,
142 char *avail, int avail_disks)
143 {
144 int copies, first;
145 switch (level) {
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;
166
167 case -4:
168 return avail_disks>= 1;
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;
177 case 6:
178 return avail_disks >= raid_disks-2;
179 default:
180 return 0;
181 }
182 }
183
184 int same_uuid(int a[4], int b[4], int swapuuid)
185 {
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 }
210 }
211
212 int 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
243 int 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];
252 unsigned long size;
253 if (lseek(fd, 64*1024, 0) != 64*1024)
254 return 0;
255 if (read(fd, sb, 1024) != 1024)
256 return 0;
257 if (strncmp((char*)sb+52, "ReIsErFs",8)!=0 &&
258 strncmp((char*)sb+52, "ReIsEr2Fs",9)!=0)
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;
262 fprintf(stderr, " size = %luK\n", size*4);
263
264 return 1;
265 }
266
267 int check_raid(int fd, char *name)
268 {
269 void *super;
270 struct mdinfo info;
271 time_t crtime;
272 char *level;
273 struct supertype *st = guess_super(fd);
274
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);
280 st->ss->getinfo_super(&info, super);
281 free(super);
282 crtime = info.array.ctime;
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));
287 return 1;
288 }
289
290 int 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
310 char *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
320 int 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 }
327 return UnSet;
328 }
329
330
331 int is_standard(char *dev, int *nump)
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 */
337 char *d = strrchr(dev, '/');
338 int type=0;
339 int num;
340 if (!d)
341 return 0;
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)
349 d += 1, type=-1; /* /dev/md/N */
350 else
351 return 0;
352 if (!*d)
353 return 0;
354 num = atoi(d);
355 while (isdigit(*d))
356 d++;
357 if (*d)
358 return 0;
359 if (nump) *nump = num;
360
361 return type;
362 }
363
364
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 */
370 struct devmap {
371 int major, minor;
372 char *name;
373 struct devmap *next;
374 } *devlist = NULL;
375 int devlist_ready = 0;
376
377 int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
378 {
379 struct stat st;
380 if (S_ISLNK(stb->st_mode)) {
381 stat(name, &st);
382 stb = &st;
383 }
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;
399 }
400
401 #ifndef HAVE_NFTW
402 #ifdef HAVE_FTW
403 int add_dev_1(const char *name, const struct stat *stb, int flag)
404 {
405 return add_dev(name, stb, flag, NULL);
406 }
407 int 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
412 int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
413 {
414 return 0;
415 }
416 int 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
423 /*
424 * Find a block device with the right major/minor number.
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.
429 */
430 char *map_dev(int major, int minor, int create)
431 {
432 struct devmap *p;
433 char *std = NULL, *nonstd=NULL;
434 int did_check = 0;
435
436 if (major == 0 && minor == 0)
437 return NULL;
438
439 retry:
440 if (!devlist_ready) {
441 char *dev = "/dev";
442 struct stat stb;
443 while(devlist) {
444 struct devmap *d = devlist;
445 devlist = d->next;
446 free(d->name);
447 free(d);
448 }
449 if (lstat(dev, &stb)==0 &&
450 S_ISLNK(stb.st_mode))
451 dev = "/dev/.";
452 nftw(dev, add_dev, 10, FTW_PHYS);
453 devlist_ready=1;
454 did_check = 1;
455 }
456
457 for (p=devlist; p; p=p->next)
458 if (p->major == major &&
459 p->minor == minor) {
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 }
469 }
470 if (!std && !nonstd && !did_check) {
471 devlist_ready = 0;
472 goto retry;
473 }
474 if (create && !std && !nonstd) {
475 static char buf[30];
476 snprintf(buf, sizeof(buf), "%d:%d", major, minor);
477 nonstd = buf;
478 }
479
480 return nonstd ? nonstd : std;
481 }
482
483 unsigned long calc_csum(void *super, int bytes)
484 {
485 unsigned long long newcsum = 0;
486 int i;
487 unsigned int csum;
488 unsigned int *superc = (unsigned int*) super;
489
490 for(i=0; i<bytes/4; i++)
491 newcsum+= superc[i];
492 csum = (newcsum& 0xffffffff) + (newcsum>>32);
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
502 return csum;
503 }
504
505 char *human_size(long long bytes)
506 {
507 static char buf[30];
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 */
517
518 if (bytes < 5000*1024)
519 buf[0]=0;
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;
523 snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)",
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;
529 snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)",
530 cGiB/100 , cGiB % 100,
531 cGB/100, cGB % 100);
532 }
533 return buf;
534 }
535
536 char *human_size_brief(long long bytes)
537 {
538 static char buf[30];
539
540
541 if (bytes < 5000*1024)
542 snprintf(buf, sizeof(buf), "%ld.%02ldKiB",
543 (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
544 );
545 else if (bytes < 2*1024LL*1024LL*1024LL)
546 snprintf(buf, sizeof(buf), "%ld.%02ldMiB",
547 (long)(bytes>>20),
548 (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
549 );
550 else
551 snprintf(buf, sizeof(buf), "%ld.%02ldGiB",
552 (long)(bytes>>30),
553 (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
554 );
555 return buf;
556 }
557
558 int get_mdp_major(void)
559 {
560 static int mdp_major = -1;
561 FILE *fl;
562 char *w;
563 int have_block = 0;
564 int have_devices = 0;
565 int last_num = -1;
566
567 if (mdp_major != -1)
568 return mdp_major;
569 fl = fopen("/proc/devices", "r");
570 if (!fl)
571 return -1;
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);
583 return mdp_major;
584 }
585
586
587
588 char *get_md_name(int dev)
589 {
590 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
591 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
592 static char devname[50];
593 struct stat stb;
594 dev_t rdev;
595 char *dn;
596
597 if (dev < 0) {
598 int mdp = get_mdp_major();
599 if (mdp < 0) return NULL;
600 rdev = makedev(mdp, (-1-dev)<<6);
601 snprintf(devname, sizeof(devname), "/dev/md/d%d", -1-dev);
602 if (stat(devname, &stb) == 0
603 && (S_IFMT&stb.st_mode) == S_IFBLK
604 && (stb.st_rdev == rdev))
605 return devname;
606 } else {
607 rdev = makedev(MD_MAJOR, dev);
608 snprintf(devname, sizeof(devname), "/dev/md%d", dev);
609 if (stat(devname, &stb) == 0
610 && (S_IFMT&stb.st_mode) == S_IFBLK
611 && (stb.st_rdev == rdev))
612 return devname;
613
614 snprintf(devname, sizeof(devname), "/dev/md/%d", dev);
615 if (stat(devname, &stb) == 0
616 && (S_IFMT&stb.st_mode) == S_IFBLK
617 && (stb.st_rdev == rdev))
618 return devname;
619 }
620 dn = map_dev(major(rdev), minor(rdev), 0);
621 if (dn)
622 return dn;
623 snprintf(devname, sizeof(devname), "/dev/.tmp.md%d", dev);
624 if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
625 if (errno != EEXIST)
626 return NULL;
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
636 void put_md_name(char *name)
637 {
638 if (strncmp(name, "/dev/.tmp.md", 12)==0)
639 unlink(name);
640 }
641
642 int 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];
650 int major;
651 int minor;
652
653 if (!dev) return -1;
654
655 major = strtoul(dev, &e, 0);
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 }
668
669 struct superswitch *superlist[] = { &super0, &super1, NULL };
670
671 struct supertype *super_by_version(int vers, int minor)
672 {
673 struct supertype *st = malloc(sizeof(*st));
674 if (!st) return st;
675 if (vers == 0) {
676 st->ss = &super0;
677 st->max_devs = MD_SB_DISKS;
678 }
679
680 if (vers == 1) {
681 st->ss = &super1;
682 st->max_devs = 384;
683 }
684 st->minor_version = minor;
685 return st;
686 }
687
688 struct supertype *guess_super(int fd)
689 {
690 /* try each load_super to find the best match,
691 * and return the best superswitch
692 */
693 struct superswitch *ss;
694 struct supertype *st;
695 unsigned long besttime = 0;
696 int bestsuper = -1;
697
698 void *sbp = NULL;
699 int i;
700
701 st = malloc(sizeof(*st));
702 memset(st, 0, sizeof(*st));
703 for (i=0 ; superlist[i]; i++) {
704 int rv;
705 ss = superlist[i];
706 st->ss = NULL;
707 rv = ss->load_super(st, fd, &sbp, NULL);
708 if (rv == 0) {
709 struct mdinfo info;
710 ss->getinfo_super(&info, sbp);
711 if (bestsuper == -1 ||
712 besttime < info.array.ctime) {
713 bestsuper = i;
714 besttime = info.array.ctime;
715 }
716 free(sbp);
717 }
718 }
719 if (bestsuper != -1) {
720 int rv;
721 st->ss = NULL;
722 rv = superlist[bestsuper]->load_super(st, fd, &sbp, NULL);
723 if (rv == 0) {
724 free(sbp);
725 return st;
726 }
727 }
728 free(st);
729 return NULL;
730 }
731
732
733 #ifdef __TINYC__
734 /* tinyc doesn't optimize this check in ioctl.h out ... */
735 unsigned int __invalid_size_argument_for_IOC = 0;
736 #endif
737