Release!!
[thirdparty/mdadm.git] / util.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
cd29a5c8 4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
64c4757e
NB
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
9a9dab36 30#include "mdadm.h"
64c4757e
NB
31#include "md_p.h"
32#include <sys/utsname.h>
98c6faba 33#include <ctype.h>
64c4757e
NB
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 */
41int 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
82b27616
NB
60 if (hit<32) {
61 uuid[hit/8] <<= 4;
62 uuid[hit/8] += n;
63 }
64c4757e
NB
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
83int 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)
682c7051 94 return (vers.major*10000) + (vers.minor*100) + vers.patchlevel;
5787fa49
NB
95 if (errno == EACCES)
96 return -1;
0df46c2a 97 if (major(stb.st_rdev) == MD_MAJOR)
682c7051 98 return (3600);
64c4757e
NB
99 return -1;
100}
101
102
103int get_linux_version()
104{
105 struct utsname name;
98c6faba 106 char *cp;
64c4757e
NB
107 int a,b,c;
108 if (uname(&name) <0)
109 return -1;
110
98c6faba
NB
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
682c7051 118 return (a*1000000)+(b*1000)+c;
64c4757e
NB
119}
120
121int enough(int level, int raid_disks, int avail_disks)
122{
123 switch (level) {
e5329c37
NB
124 case 10: return 1; /* a lie, but it is hard to tell */
125
e0d19036
NB
126 case -4:
127 return avail_disks>= 1;
64c4757e
NB
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;
98c6faba
NB
136 case 6:
137 return avail_disks >= raid_disks-2;
64c4757e
NB
138 default:
139 return 0;
140 }
141}
142
f277ce36 143int same_uuid(int a[4], int b[4], int swapuuid)
64c4757e 144{
f277ce36
NB
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 }
64c4757e
NB
169}
170
682c7051
NB
171int 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
202int 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;
a46f4061
NB
216 if (strncmp((char*)sb+52, "ReIsErFs",8)!=0 &&
217 strncmp((char*)sb+52, "ReIsEr2Fs",9)!=0)
682c7051
NB
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
226int check_raid(int fd, char *name)
227{
4b1ac34b
NB
228 void *super;
229 struct mdinfo info;
947fd4dd 230 struct mddev_ident_s ident;
682c7051 231 time_t crtime;
d078d77c 232 char *level;
82d9eba6 233 struct supertype *st = guess_super(fd);
f9ce90ba 234
82d9eba6
NB
235 if (!st) return 0;
236 st->ss->load_super(st, fd, &super, name);
237 /* Looks like a raid array .. */
238 fprintf(stderr, Name ": %s appears to be part of a raid array:\n",
239 name);
947fd4dd 240 st->ss->getinfo_super(&info, &ident, super);
82d9eba6
NB
241 free(super);
242 crtime = info.array.ctime;
d078d77c
NB
243 level = map_num(pers, info.array.level);
244 if (!level) level = "-unknown-";
245 fprintf(stderr, " level=%s devices=%d ctime=%s",
246 level, info.array.raid_disks, ctime(&crtime));
82d9eba6 247 return 1;
682c7051
NB
248}
249
682c7051
NB
250int ask(char *mesg)
251{
252 char *add = "";
253 int i;
254 for (i=0; i<5; i++) {
255 char buf[100];
256 fprintf(stderr, "%s%s", mesg, add);
257 fflush(stderr);
258 if (fgets(buf, 100, stdin)==NULL)
259 return 0;
260 if (buf[0]=='y' || buf[0]=='Y')
261 return 1;
262 if (buf[0]=='n' || buf[0]=='N')
263 return 0;
264 add = "(y/n) ";
265 }
266 fprintf(stderr, Name ": assuming 'no'\n");
267 return 0;
268}
269
270char *map_num(mapping_t *map, int num)
271{
272 while (map->name) {
273 if (map->num == num)
274 return map->name;
275 map++;
276 }
277 return NULL;
278}
279
280int map_name(mapping_t *map, char *name)
281{
282 while (map->name) {
283 if (strcmp(map->name, name)==0)
284 return map->num;
285 map++;
286 }
98c6faba 287 return UnSet;
682c7051 288}
82b27616 289
e5329c37 290
8d80900b 291int is_standard(char *dev, int *nump)
e5329c37
NB
292{
293 /* tests if dev is a "standard" md dev name.
294 * i.e if the last component is "/dNN" or "/mdNN",
295 * where NN is a string of digits
296 */
8d80900b
NB
297 char *d = strrchr(dev, '/');
298 int type=0;
299 int num;
300 if (!d)
e5329c37 301 return 0;
8d80900b
NB
302 if (strncmp(d, "/d",2)==0)
303 d += 2, type=1; /* /dev/md/dN{pM} */
304 else if (strncmp(d, "/md_d", 5)==0)
305 d += 5, type=1; /* /dev/md_dNpM */
306 else if (strncmp(d, "/md", 3)==0)
307 d += 3, type=-1; /* /dev/mdN */
308 else if (d-dev > 3 && strncmp(d-2, "md/", 3)==0)
5a6d1148 309 d += 1, type=-1; /* /dev/md/N */
e5329c37
NB
310 else
311 return 0;
8d80900b 312 if (!*d)
e5329c37 313 return 0;
8d80900b
NB
314 num = atoi(d);
315 while (isdigit(*d))
316 d++;
317 if (*d)
e5329c37 318 return 0;
8d80900b
NB
319 if (nump) *nump = num;
320
321 return type;
e5329c37
NB
322}
323
324
82b27616
NB
325/*
326 * convert a major/minor pair for a block device into a name in /dev, if possible.
327 * On the first call, walk /dev collecting name.
328 * Put them in a simple linked listfor now.
329 */
330struct devmap {
331 int major, minor;
332 char *name;
333 struct devmap *next;
334} *devlist = NULL;
335int devlist_ready = 0;
336
5787fa49 337#ifdef UCLIBC
173fc515
NB
338int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
339{
340}
5787fa49
NB
341char *map_dev(int major, int minor)
342{
343#if 0
344 fprintf(stderr, "Warning - fail to map %d,%d to a device name\n",
345 major, minor);
346#endif
347 return NULL;
348}
349#else
82b27616 350
173fc515
NB
351#ifdef __dietlibc__
352int add_dev_1(const char *name, const struct stat *stb, int flag)
353{
354 return add_dev(name, stb, flag, NULL);
355}
356int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
357{
358 ftw(path, add_dev_1, nopenfd);
359}
360#endif
82b27616
NB
361
362int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
363{
364 if ((stb->st_mode&S_IFMT)== S_IFBLK) {
365 char *n = strdup(name);
366 struct devmap *dm = malloc(sizeof(*dm));
367 if (dm) {
0df46c2a
NB
368 dm->major = major(stb->st_rdev);
369 dm->minor = minor(stb->st_rdev);
82b27616
NB
370 dm->name = n;
371 dm->next = devlist;
372 devlist = dm;
373 }
374 }
375 return 0;
376}
377
dd0781e5
NB
378/*
379 * Find a block device with the right major/minor number.
b79713f8
NB
380 * If we find multiple names, choose the shortest.
381 * If we find a non-standard name, it is probably there
382 * deliberately so prefer it over a standard name.
383 * This applies only to names for MD devices.
dd0781e5 384 */
82b27616
NB
385char *map_dev(int major, int minor)
386{
dd0781e5 387 struct devmap *p;
b79713f8 388 char *std = NULL, *nonstd=NULL;
dd0781e5 389 if (!devlist_ready) {
dd0781e5 390 nftw("/dev", add_dev, 10, FTW_PHYS);
dd0781e5
NB
391 devlist_ready=1;
392 }
82b27616 393
dd0781e5
NB
394 for (p=devlist; p; p=p->next)
395 if (p->major == major &&
396 p->minor == minor) {
b79713f8
NB
397 if (is_standard(p->name, NULL)) {
398 if (std == NULL ||
399 strlen(p->name) < strlen(std))
400 std = p->name;
401 } else {
402 if (nonstd == NULL ||
403 strlen(p->name) < strlen(nonstd))
404 nonstd = p->name;
405 }
dd0781e5 406 }
b79713f8 407 return nonstd ? nonstd : std;
82b27616
NB
408}
409
5787fa49 410#endif
82b27616 411
4b1ac34b 412unsigned long calc_csum(void *super, int bytes)
82b27616 413{
56eb10c0 414 unsigned long long newcsum = 0;
82b27616 415 int i;
4b1ac34b
NB
416 unsigned int csum;
417 unsigned int *superc = (unsigned int*) super;
82b27616 418
4b1ac34b 419 for(i=0; i<bytes/4; i++)
82b27616
NB
420 newcsum+= superc[i];
421 csum = (newcsum& 0xffffffff) + (newcsum>>32);
570c0542
NB
422#ifdef __alpha__
423/* The in-kernel checksum calculation is always 16bit on
424 * the alpha, though it is 32 bit on i386...
425 * I wonder what it is elsewhere... (it uses and API in
426 * a way that it shouldn't).
427 */
428 csum = (csum & 0xffff) + (csum >> 16);
429 csum = (csum & 0xffff) + (csum >> 16);
430#endif
82b27616
NB
431 return csum;
432}
cd29a5c8 433
56eb10c0 434char *human_size(long long bytes)
cd29a5c8
NB
435{
436 static char buf[30];
d5d3721e
NB
437
438 /* We convert bytes to either centi-M{ega,ibi}bytes or
439 * centi-G{igi,ibi}bytes, with appropriate rounding,
440 * and then print 1/100th of those as a decimal.
441 * We allow upto 2048Megabytes before converting to
442 * gigabytes, as that shows more precision and isn't
443 * too large a number.
444 * Terrabytes are not yet handled.
445 */
cd29a5c8 446
56eb10c0 447 if (bytes < 5000*1024)
cd29a5c8 448 buf[0]=0;
d5d3721e
NB
449 else if (bytes < 2*1024LL*1024LL*1024LL) {
450 long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2;
451 long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2;
8f23b0b3 452 snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)",
d5d3721e
NB
453 cMiB/100 , cMiB % 100,
454 cMB/100, cMB % 100);
455 } else {
456 long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2;
457 long cGB = (bytes / (1000000000LL/200LL ) +1) /2;
8f23b0b3 458 snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)",
d5d3721e
NB
459 cGiB/100 , cGiB % 100,
460 cGB/100, cGB % 100);
461 }
cd29a5c8
NB
462 return buf;
463}
e0d19036
NB
464
465char *human_size_brief(long long bytes)
466{
467 static char buf[30];
468
469
470 if (bytes < 5000*1024)
8f23b0b3 471 snprintf(buf, sizeof(buf), "%ld.%02ldKiB",
bd526cee 472 (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
e0d19036
NB
473 );
474 else if (bytes < 2*1024LL*1024LL*1024LL)
8f23b0b3 475 snprintf(buf, sizeof(buf), "%ld.%02ldMiB",
e0d19036 476 (long)(bytes>>20),
bd526cee 477 (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
e0d19036
NB
478 );
479 else
8f23b0b3 480 snprintf(buf, sizeof(buf), "%ld.%02ldGiB",
e0d19036 481 (long)(bytes>>30),
bd526cee 482 (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
e0d19036
NB
483 );
484 return buf;
485}
486
dd0781e5 487int get_mdp_major(void)
98c6faba 488{
dd0781e5
NB
489static int mdp_major = -1;
490 FILE *fl;
98c6faba
NB
491 char *w;
492 int have_block = 0;
493 int have_devices = 0;
494 int last_num = -1;
dd0781e5
NB
495
496 if (mdp_major != -1)
497 return mdp_major;
498 fl = fopen("/proc/devices", "r");
98c6faba 499 if (!fl)
dd0781e5 500 return -1;
98c6faba
NB
501 while ((w = conf_word(fl, 1))) {
502 if (have_block && strcmp(w, "devices:")==0)
503 have_devices = 1;
504 have_block = (strcmp(w, "Block")==0);
505 if (isdigit(w[0]))
506 last_num = atoi(w);
507 if (have_devices && strcmp(w, "mdp")==0)
508 mdp_major = last_num;
509 free(w);
510 }
511 fclose(fl);
dd0781e5 512 return mdp_major;
98c6faba
NB
513}
514
515
e0d19036 516
e0d19036
NB
517char *get_md_name(int dev)
518{
519 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
98c6faba 520 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
e0d19036
NB
521 static char devname[50];
522 struct stat stb;
98c6faba 523 dev_t rdev;
dd0781e5 524 char *dn;
98c6faba
NB
525
526 if (dev < 0) {
dd0781e5
NB
527 int mdp = get_mdp_major();
528 if (mdp < 0) return NULL;
0df46c2a 529 rdev = makedev(mdp, (-1-dev)<<6);
8f23b0b3 530 snprintf(devname, sizeof(devname), "/dev/md/d%d", -1-dev);
98c6faba
NB
531 if (stat(devname, &stb) == 0
532 && (S_IFMT&stb.st_mode) == S_IFBLK
533 && (stb.st_rdev == rdev))
534 return devname;
535 } else {
0df46c2a 536 rdev = makedev(MD_MAJOR, dev);
8f23b0b3 537 snprintf(devname, sizeof(devname), "/dev/md%d", dev);
98c6faba
NB
538 if (stat(devname, &stb) == 0
539 && (S_IFMT&stb.st_mode) == S_IFBLK
540 && (stb.st_rdev == rdev))
541 return devname;
542
8f23b0b3 543 snprintf(devname, sizeof(devname), "/dev/md/%d", dev);
98c6faba
NB
544 if (stat(devname, &stb) == 0
545 && (S_IFMT&stb.st_mode) == S_IFBLK
546 && (stb.st_rdev == rdev))
547 return devname;
548 }
8d80900b 549 dn = map_dev(major(rdev), minor(rdev));
dd0781e5
NB
550 if (dn)
551 return dn;
8f23b0b3 552 snprintf(devname, sizeof(devname), "/dev/.tmp.md%d", dev);
e0d19036 553 if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
dd0781e5
NB
554 if (errno != EEXIST)
555 return NULL;
e0d19036
NB
556
557 if (stat(devname, &stb) == 0
558 && (S_IFMT&stb.st_mode) == S_IFBLK
559 && (stb.st_rdev == rdev))
560 return devname;
561 unlink(devname);
562 return NULL;
563}
564
565void put_md_name(char *name)
566{
567 if (strncmp(name, "/dev/.tmp.md", 12)==0)
568 unlink(name);
569}
f9ce90ba
NB
570
571
572
82d9eba6 573struct superswitch *superlist[] = { &super0, &super1, NULL };
f9ce90ba 574
82d9eba6 575struct supertype *super_by_version(int vers, int minor)
f9ce90ba 576{
82d9eba6
NB
577 struct supertype *st = malloc(sizeof(*st));
578 if (!st) return st;
6fbba4c9 579 if (vers == 0) {
82d9eba6 580 st->ss = &super0;
6fbba4c9
NB
581 st->max_devs = MD_SB_DISKS;
582 }
82d9eba6 583
6fbba4c9 584 if (vers == 1) {
82d9eba6 585 st->ss = &super1;
6fbba4c9
NB
586 st->max_devs = 384;
587 }
82d9eba6
NB
588 st->minor_version = minor;
589 return st;
f9ce90ba
NB
590}
591
82d9eba6 592struct supertype *guess_super(int fd)
f9ce90ba
NB
593{
594 /* try each load_super to find the best match,
595 * and return the best superswitch
596 */
82d9eba6
NB
597 struct superswitch *ss;
598 struct supertype *st;
570c0542
NB
599 unsigned long besttime = 0;
600 int bestsuper = -1;
82d9eba6 601
f9ce90ba
NB
602 void *sbp = NULL;
603 int i;
604
82d9eba6
NB
605 st = malloc(sizeof(*st));
606 memset(st, 0, sizeof(*st));
f9ce90ba
NB
607 for (i=0 ; superlist[i]; i++) {
608 int rv;
609 ss = superlist[i];
f277ce36 610 st->ss = NULL;
82d9eba6 611 rv = ss->load_super(st, fd, &sbp, NULL);
570c0542
NB
612 if (rv == 0) {
613 struct mdinfo info;
947fd4dd
NB
614 struct mddev_ident_s ident;
615 ss->getinfo_super(&info, &ident, sbp);
570c0542
NB
616 if (bestsuper == -1 ||
617 besttime < info.array.ctime) {
618 bestsuper = i;
619 besttime = info.array.ctime;
570c0542
NB
620 }
621 free(sbp);
622 }
623 }
624 if (bestsuper != -1) {
625 int rv;
f277ce36 626 st->ss = NULL;
570c0542 627 rv = superlist[bestsuper]->load_super(st, fd, &sbp, NULL);
f9ce90ba
NB
628 if (rv == 0) {
629 free(sbp);
82d9eba6 630 return st;
f9ce90ba
NB
631 }
632 }
570c0542 633 free(st);
f9ce90ba
NB
634 return NULL;
635}
fe6729fa
NB
636
637#ifdef __TINYC__
638/* tinyc doesn't optimize this check in ioctl.h out ... */
639unsigned int __invalid_size_argument_for_IOC = 0;
640#endif
641