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