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