When assembling with --auto, honour common device names
[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;
64c4757e 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
153void uuid_from_super(int uuid[4], mdp_super_t *super)
154{
155 uuid[0] = super->set_uuid0;
156 if (super->minor_version >= 90) {
157 uuid[1] = super->set_uuid1;
158 uuid[2] = super->set_uuid2;
159 uuid[3] = super->set_uuid3;
160 } else {
161 uuid[1] = 0;
162 uuid[2] = 0;
163 uuid[3] = 0;
164 }
165}
166
167int compare_super(mdp_super_t *first, mdp_super_t *second)
168{
169 /*
170 * return:
171 * 0 same, or first was empty, and second was copied
172 * 1 second had wrong number
173 * 2 wrong uuid
174 * 3 wrong other info
175 */
176 int uuid1[4], uuid2[4];
177 if (second->md_magic != MD_SB_MAGIC)
178 return 1;
179 if (first-> md_magic != MD_SB_MAGIC) {
180 memcpy(first, second, sizeof(*first));
181 return 0;
182 }
183
184 uuid_from_super(uuid1, first);
185 uuid_from_super(uuid2, second);
186 if (!same_uuid(uuid1, uuid2))
187 return 2;
188 if (first->major_version != second->major_version ||
189 first->minor_version != second->minor_version ||
190 first->patch_version != second->patch_version ||
191 first->gvalid_words != second->gvalid_words ||
192 first->ctime != second->ctime ||
193 first->level != second->level ||
194 first->size != second->size ||
195 first->raid_disks != second->raid_disks )
196 return 3;
197
198 return 0;
199}
200
201int load_super(int fd, mdp_super_t *super)
202{
203 /* try to read in the superblock
204 *
205 * return
206 * 0 - success
207 * 1 - no block size
208 * 2 - too small
209 * 3 - no seek
210 * 4 - no read
211 * 5 - no magic
212 * 6 - wrong major version
213 */
e0d19036 214 unsigned long size;
b5e64645 215 unsigned long long dsize;
e0d19036 216 unsigned long long offset;
64c4757e 217
b5e64645
NB
218#ifdef BLKGETSIZE64
219 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
220#endif
221 {
222 if (ioctl(fd, BLKGETSIZE, &size))
223 return 1;
224 else
225 dsize = size << 9;
226 }
64c4757e 227
b5e64645 228 if (dsize < MD_RESERVED_SECTORS*2)
64c4757e
NB
229 return 2;
230
b5e64645 231 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
64c4757e
NB
232
233 offset *= 512;
234
5787fa49
NB
235 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
236
64c4757e
NB
237 if (lseek64(fd, offset, 0)< 0LL)
238 return 3;
239
682c7051 240 if (read(fd, super, sizeof(*super)) != sizeof(*super))
64c4757e
NB
241 return 4;
242
243 if (super->md_magic != MD_SB_MAGIC)
244 return 5;
245
246 if (super->major_version != 0)
247 return 6;
248 return 0;
249}
250
82b27616
NB
251int store_super(int fd, mdp_super_t *super)
252{
b5e64645
NB
253 unsigned long size;
254 unsigned long long dsize;
255
82b27616
NB
256 long long offset;
257
b5e64645
NB
258#ifdef BLKGETSIZE64
259 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
260#endif
261 {
262 if (ioctl(fd, BLKGETSIZE, &size))
263 return 1;
264 else
265 dsize = ((unsigned long long)size) << 9;
266 }
82b27616 267
b5e64645 268 if (dsize < MD_RESERVED_SECTORS*2)
82b27616
NB
269 return 2;
270
b5e64645 271 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
82b27616
NB
272
273 offset *= 512;
274
275 if (lseek64(fd, offset, 0)< 0LL)
276 return 3;
277
278 if (write(fd, super, sizeof(*super)) != sizeof(*super))
279 return 4;
280
281 return 0;
282}
283
284
682c7051
NB
285
286int check_ext2(int fd, char *name)
287{
288 /*
289 * Check for an ext2fs file system.
290 * Superblock is always 1K at 1K offset
291 *
292 * s_magic is le16 at 56 == 0xEF53
293 * report mtime - le32 at 44
294 * blocks - le32 at 4
295 * logblksize - le32 at 24
296 */
297 unsigned char sb[1024];
298 time_t mtime;
299 int size, bsize;
300 if (lseek(fd, 1024,0)!= 1024)
301 return 0;
302 if (read(fd, sb, 1024)!= 1024)
303 return 0;
304 if (sb[56] != 0x53 || sb[57] != 0xef)
305 return 0;
306
307 mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8;
308 bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8;
309 size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8;
310 fprintf(stderr, Name ": %s appears to contain an ext2fs file system\n",
311 name);
312 fprintf(stderr," size=%dK mtime=%s",
313 size*(1<<bsize), ctime(&mtime));
314 return 1;
315}
316
317int check_reiser(int fd, char *name)
318{
319 /*
320 * superblock is at 64K
321 * size is 1024;
322 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
323 *
324 */
325 unsigned char sb[1024];
326 int size;
327 if (lseek(fd, 64*1024, 0) != 64*1024)
328 return 0;
329 if (read(fd, sb, 1024) != 1024)
330 return 0;
331 if (strncmp(sb+52, "ReIsErFs",8)!=0 &&
332 strncmp(sb+52, "ReIsEr2Fs",9)!=0)
333 return 0;
334 fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name);
335 size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8;
336 fprintf(stderr, " size = %dK\n", size*4);
337
338 return 1;
339}
340
341int check_raid(int fd, char *name)
342{
343 mdp_super_t super;
344 time_t crtime;
345 if (load_super(fd, &super))
346 return 0;
347 /* Looks like a raid array .. */
5787fa49 348 fprintf(stderr, Name ": %s appears to be part of a raid array:\n",
682c7051
NB
349 name);
350 crtime = super.ctime;
b83d95f3 351 fprintf(stderr, " level=%d devices=%d ctime=%s",
682c7051
NB
352 super.level, super.raid_disks, ctime(&crtime));
353 return 1;
354}
355
356
357int ask(char *mesg)
358{
359 char *add = "";
360 int i;
361 for (i=0; i<5; i++) {
362 char buf[100];
363 fprintf(stderr, "%s%s", mesg, add);
364 fflush(stderr);
365 if (fgets(buf, 100, stdin)==NULL)
366 return 0;
367 if (buf[0]=='y' || buf[0]=='Y')
368 return 1;
369 if (buf[0]=='n' || buf[0]=='N')
370 return 0;
371 add = "(y/n) ";
372 }
373 fprintf(stderr, Name ": assuming 'no'\n");
374 return 0;
375}
376
377char *map_num(mapping_t *map, int num)
378{
379 while (map->name) {
380 if (map->num == num)
381 return map->name;
382 map++;
383 }
384 return NULL;
385}
386
387int map_name(mapping_t *map, char *name)
388{
389 while (map->name) {
390 if (strcmp(map->name, name)==0)
391 return map->num;
392 map++;
393 }
98c6faba 394 return UnSet;
682c7051 395}
82b27616 396
e5329c37 397
8d80900b 398int is_standard(char *dev, int *nump)
e5329c37
NB
399{
400 /* tests if dev is a "standard" md dev name.
401 * i.e if the last component is "/dNN" or "/mdNN",
402 * where NN is a string of digits
403 */
8d80900b
NB
404 char *d = strrchr(dev, '/');
405 int type=0;
406 int num;
407 if (!d)
e5329c37 408 return 0;
8d80900b
NB
409 if (strncmp(d, "/d",2)==0)
410 d += 2, type=1; /* /dev/md/dN{pM} */
411 else if (strncmp(d, "/md_d", 5)==0)
412 d += 5, type=1; /* /dev/md_dNpM */
413 else if (strncmp(d, "/md", 3)==0)
414 d += 3, type=-1; /* /dev/mdN */
415 else if (d-dev > 3 && strncmp(d-2, "md/", 3)==0)
416 type = -1; /* /dev/md/N */
e5329c37
NB
417 else
418 return 0;
8d80900b 419 if (!*d)
e5329c37 420 return 0;
8d80900b
NB
421 num = atoi(d);
422 while (isdigit(*d))
423 d++;
424 if (*d)
e5329c37 425 return 0;
8d80900b
NB
426 if (nump) *nump = num;
427
428 return type;
e5329c37
NB
429}
430
431
82b27616
NB
432/*
433 * convert a major/minor pair for a block device into a name in /dev, if possible.
434 * On the first call, walk /dev collecting name.
435 * Put them in a simple linked listfor now.
436 */
437struct devmap {
438 int major, minor;
439 char *name;
440 struct devmap *next;
441} *devlist = NULL;
442int devlist_ready = 0;
443
5787fa49
NB
444#ifdef UCLIBC
445char *map_dev(int major, int minor)
446{
447#if 0
448 fprintf(stderr, "Warning - fail to map %d,%d to a device name\n",
449 major, minor);
450#endif
451 return NULL;
452}
453#else
82b27616
NB
454#define __USE_XOPEN_EXTENDED
455#include <ftw.h>
456
457
98c6faba 458#ifndef __dietlibc__
82b27616 459int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
98c6faba
NB
460#else
461int add_dev(const char *name, const struct stat *stb, int flag)
462#endif
82b27616
NB
463{
464 if ((stb->st_mode&S_IFMT)== S_IFBLK) {
465 char *n = strdup(name);
466 struct devmap *dm = malloc(sizeof(*dm));
467 if (dm) {
468 dm->major = MAJOR(stb->st_rdev);
469 dm->minor = MINOR(stb->st_rdev);
470 dm->name = n;
471 dm->next = devlist;
472 devlist = dm;
473 }
474 }
475 return 0;
476}
477
dd0781e5
NB
478/*
479 * Find a block device with the right major/minor number.
8d80900b 480 * Avoid /dev/mdNN and /dev/md/dNN if possible
dd0781e5 481 */
82b27616
NB
482char *map_dev(int major, int minor)
483{
dd0781e5
NB
484 struct devmap *p;
485 char *std = NULL;
486 if (!devlist_ready) {
98c6faba 487#ifndef __dietlibc__
dd0781e5 488 nftw("/dev", add_dev, 10, FTW_PHYS);
98c6faba 489#else
dd0781e5 490 ftw("/dev", add_dev, 10);
98c6faba 491#endif
dd0781e5
NB
492 devlist_ready=1;
493 }
82b27616 494
dd0781e5
NB
495 for (p=devlist; p; p=p->next)
496 if (p->major == major &&
497 p->minor == minor) {
8d80900b 498 if (is_standard(p->name, NULL))
dd0781e5
NB
499 std = p->name;
500 else
501 return p->name;
502 }
503 return std;
82b27616
NB
504}
505
5787fa49 506#endif
82b27616 507
98c6faba 508unsigned long calc_sb_csum(mdp_super_t *super)
82b27616
NB
509{
510 unsigned int oldcsum = super->sb_csum;
56eb10c0 511 unsigned long long newcsum = 0;
82b27616
NB
512 unsigned long csum;
513 int i;
514 unsigned int *superc = (int*) super;
515 super->sb_csum = 0;
516
517 for(i=0; i<MD_SB_BYTES/4; i++)
518 newcsum+= superc[i];
519 csum = (newcsum& 0xffffffff) + (newcsum>>32);
520 super->sb_csum = oldcsum;
521 return csum;
522}
cd29a5c8 523
56eb10c0 524char *human_size(long long bytes)
cd29a5c8
NB
525{
526 static char buf[30];
56eb10c0 527
cd29a5c8 528
56eb10c0 529 if (bytes < 5000*1024)
cd29a5c8 530 buf[0]=0;
56eb10c0 531 else if (bytes < 2*1024LL*1024LL*1024LL)
e0d19036 532 sprintf(buf, " (%ld.%02ld MiB %ld.%02ld MB)",
56eb10c0 533 (long)(bytes>>20),
bd526cee 534 (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100),
56eb10c0 535 (long)(bytes/1000/1000),
bd526cee 536 (long)(((bytes%1000000)+5000)/10000)
56eb10c0 537 );
cd29a5c8 538 else
e0d19036 539 sprintf(buf, " (%ld.%02ld GiB %ld.%02ld GB)",
56eb10c0 540 (long)(bytes>>30),
bd526cee 541 (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100),
56eb10c0 542 (long)(bytes/1000LL/1000LL/1000LL),
5787fa49 543 (long)((((bytes/1000)%1000000)+5000)/10000)
56eb10c0 544 );
cd29a5c8
NB
545 return buf;
546}
e0d19036
NB
547
548char *human_size_brief(long long bytes)
549{
550 static char buf[30];
551
552
553 if (bytes < 5000*1024)
554 sprintf(buf, "%ld.%02ldKiB",
bd526cee 555 (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
e0d19036
NB
556 );
557 else if (bytes < 2*1024LL*1024LL*1024LL)
558 sprintf(buf, "%ld.%02ldMiB",
559 (long)(bytes>>20),
bd526cee 560 (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
e0d19036
NB
561 );
562 else
563 sprintf(buf, "%ld.%02ldGiB",
564 (long)(bytes>>30),
bd526cee 565 (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
e0d19036
NB
566 );
567 return buf;
568}
569
dd0781e5 570int get_mdp_major(void)
98c6faba 571{
dd0781e5
NB
572static int mdp_major = -1;
573 FILE *fl;
98c6faba
NB
574 char *w;
575 int have_block = 0;
576 int have_devices = 0;
577 int last_num = -1;
dd0781e5
NB
578
579 if (mdp_major != -1)
580 return mdp_major;
581 fl = fopen("/proc/devices", "r");
98c6faba 582 if (!fl)
dd0781e5 583 return -1;
98c6faba
NB
584 while ((w = conf_word(fl, 1))) {
585 if (have_block && strcmp(w, "devices:")==0)
586 have_devices = 1;
587 have_block = (strcmp(w, "Block")==0);
588 if (isdigit(w[0]))
589 last_num = atoi(w);
590 if (have_devices && strcmp(w, "mdp")==0)
591 mdp_major = last_num;
592 free(w);
593 }
594 fclose(fl);
dd0781e5 595 return mdp_major;
98c6faba
NB
596}
597
598
e0d19036 599
e0d19036
NB
600char *get_md_name(int dev)
601{
602 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
98c6faba 603 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
e0d19036
NB
604 static char devname[50];
605 struct stat stb;
98c6faba 606 dev_t rdev;
dd0781e5 607 char *dn;
98c6faba
NB
608
609 if (dev < 0) {
dd0781e5
NB
610 int mdp = get_mdp_major();
611 if (mdp < 0) return NULL;
612 rdev = MKDEV(mdp, (-1-dev)<<6);
98c6faba
NB
613 sprintf(devname, "/dev/md/d%d", -1-dev);
614 if (stat(devname, &stb) == 0
615 && (S_IFMT&stb.st_mode) == S_IFBLK
616 && (stb.st_rdev == rdev))
617 return devname;
618 } else {
619 rdev = MKDEV(MD_MAJOR, dev);
620 sprintf(devname, "/dev/md%d", dev);
621 if (stat(devname, &stb) == 0
622 && (S_IFMT&stb.st_mode) == S_IFBLK
623 && (stb.st_rdev == rdev))
624 return devname;
625
626 sprintf(devname, "/dev/md/%d", dev);
627 if (stat(devname, &stb) == 0
628 && (S_IFMT&stb.st_mode) == S_IFBLK
629 && (stb.st_rdev == rdev))
630 return devname;
631 }
8d80900b 632 dn = map_dev(major(rdev), minor(rdev));
dd0781e5
NB
633 if (dn)
634 return dn;
e0d19036
NB
635 sprintf(devname, "/dev/.tmp.md%d", dev);
636 if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
dd0781e5
NB
637 if (errno != EEXIST)
638 return NULL;
e0d19036
NB
639
640 if (stat(devname, &stb) == 0
641 && (S_IFMT&stb.st_mode) == S_IFBLK
642 && (stb.st_rdev == rdev))
643 return devname;
644 unlink(devname);
645 return NULL;
646}
647
648void put_md_name(char *name)
649{
650 if (strncmp(name, "/dev/.tmp.md", 12)==0)
651 unlink(name);
652}