Release 1.11.0
[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
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
b2b46bfc 281 fsync(fd);
82b27616
NB
282 return 0;
283}
284
285
682c7051
NB
286
287int check_ext2(int fd, char *name)
288{
289 /*
290 * Check for an ext2fs file system.
291 * Superblock is always 1K at 1K offset
292 *
293 * s_magic is le16 at 56 == 0xEF53
294 * report mtime - le32 at 44
295 * blocks - le32 at 4
296 * logblksize - le32 at 24
297 */
298 unsigned char sb[1024];
299 time_t mtime;
300 int size, bsize;
301 if (lseek(fd, 1024,0)!= 1024)
302 return 0;
303 if (read(fd, sb, 1024)!= 1024)
304 return 0;
305 if (sb[56] != 0x53 || sb[57] != 0xef)
306 return 0;
307
308 mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8;
309 bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8;
310 size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8;
311 fprintf(stderr, Name ": %s appears to contain an ext2fs file system\n",
312 name);
313 fprintf(stderr," size=%dK mtime=%s",
314 size*(1<<bsize), ctime(&mtime));
315 return 1;
316}
317
318int check_reiser(int fd, char *name)
319{
320 /*
321 * superblock is at 64K
322 * size is 1024;
323 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
324 *
325 */
326 unsigned char sb[1024];
327 int size;
328 if (lseek(fd, 64*1024, 0) != 64*1024)
329 return 0;
330 if (read(fd, sb, 1024) != 1024)
331 return 0;
332 if (strncmp(sb+52, "ReIsErFs",8)!=0 &&
333 strncmp(sb+52, "ReIsEr2Fs",9)!=0)
334 return 0;
335 fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name);
336 size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8;
337 fprintf(stderr, " size = %dK\n", size*4);
338
339 return 1;
340}
341
342int check_raid(int fd, char *name)
343{
344 mdp_super_t super;
345 time_t crtime;
346 if (load_super(fd, &super))
347 return 0;
348 /* Looks like a raid array .. */
5787fa49 349 fprintf(stderr, Name ": %s appears to be part of a raid array:\n",
682c7051
NB
350 name);
351 crtime = super.ctime;
b83d95f3 352 fprintf(stderr, " level=%d devices=%d ctime=%s",
682c7051
NB
353 super.level, super.raid_disks, ctime(&crtime));
354 return 1;
355}
356
357
358int ask(char *mesg)
359{
360 char *add = "";
361 int i;
362 for (i=0; i<5; i++) {
363 char buf[100];
364 fprintf(stderr, "%s%s", mesg, add);
365 fflush(stderr);
366 if (fgets(buf, 100, stdin)==NULL)
367 return 0;
368 if (buf[0]=='y' || buf[0]=='Y')
369 return 1;
370 if (buf[0]=='n' || buf[0]=='N')
371 return 0;
372 add = "(y/n) ";
373 }
374 fprintf(stderr, Name ": assuming 'no'\n");
375 return 0;
376}
377
378char *map_num(mapping_t *map, int num)
379{
380 while (map->name) {
381 if (map->num == num)
382 return map->name;
383 map++;
384 }
385 return NULL;
386}
387
388int map_name(mapping_t *map, char *name)
389{
390 while (map->name) {
391 if (strcmp(map->name, name)==0)
392 return map->num;
393 map++;
394 }
98c6faba 395 return UnSet;
682c7051 396}
82b27616 397
e5329c37 398
8d80900b 399int is_standard(char *dev, int *nump)
e5329c37
NB
400{
401 /* tests if dev is a "standard" md dev name.
402 * i.e if the last component is "/dNN" or "/mdNN",
403 * where NN is a string of digits
404 */
8d80900b
NB
405 char *d = strrchr(dev, '/');
406 int type=0;
407 int num;
408 if (!d)
e5329c37 409 return 0;
8d80900b
NB
410 if (strncmp(d, "/d",2)==0)
411 d += 2, type=1; /* /dev/md/dN{pM} */
412 else if (strncmp(d, "/md_d", 5)==0)
413 d += 5, type=1; /* /dev/md_dNpM */
414 else if (strncmp(d, "/md", 3)==0)
415 d += 3, type=-1; /* /dev/mdN */
416 else if (d-dev > 3 && strncmp(d-2, "md/", 3)==0)
417 type = -1; /* /dev/md/N */
e5329c37
NB
418 else
419 return 0;
8d80900b 420 if (!*d)
e5329c37 421 return 0;
8d80900b
NB
422 num = atoi(d);
423 while (isdigit(*d))
424 d++;
425 if (*d)
e5329c37 426 return 0;
8d80900b
NB
427 if (nump) *nump = num;
428
429 return type;
e5329c37
NB
430}
431
432
82b27616
NB
433/*
434 * convert a major/minor pair for a block device into a name in /dev, if possible.
435 * On the first call, walk /dev collecting name.
436 * Put them in a simple linked listfor now.
437 */
438struct devmap {
439 int major, minor;
440 char *name;
441 struct devmap *next;
442} *devlist = NULL;
443int devlist_ready = 0;
444
5787fa49
NB
445#ifdef UCLIBC
446char *map_dev(int major, int minor)
447{
448#if 0
449 fprintf(stderr, "Warning - fail to map %d,%d to a device name\n",
450 major, minor);
451#endif
452 return NULL;
453}
454#else
82b27616
NB
455#define __USE_XOPEN_EXTENDED
456#include <ftw.h>
457
458
98c6faba 459#ifndef __dietlibc__
82b27616 460int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
98c6faba
NB
461#else
462int add_dev(const char *name, const struct stat *stb, int flag)
463#endif
82b27616
NB
464{
465 if ((stb->st_mode&S_IFMT)== S_IFBLK) {
466 char *n = strdup(name);
467 struct devmap *dm = malloc(sizeof(*dm));
468 if (dm) {
0df46c2a
NB
469 dm->major = major(stb->st_rdev);
470 dm->minor = minor(stb->st_rdev);
82b27616
NB
471 dm->name = n;
472 dm->next = devlist;
473 devlist = dm;
474 }
475 }
476 return 0;
477}
478
dd0781e5
NB
479/*
480 * Find a block device with the right major/minor number.
8d80900b 481 * Avoid /dev/mdNN and /dev/md/dNN if possible
dd0781e5 482 */
82b27616
NB
483char *map_dev(int major, int minor)
484{
dd0781e5
NB
485 struct devmap *p;
486 char *std = NULL;
487 if (!devlist_ready) {
98c6faba 488#ifndef __dietlibc__
dd0781e5 489 nftw("/dev", add_dev, 10, FTW_PHYS);
98c6faba 490#else
dd0781e5 491 ftw("/dev", add_dev, 10);
98c6faba 492#endif
dd0781e5
NB
493 devlist_ready=1;
494 }
82b27616 495
dd0781e5
NB
496 for (p=devlist; p; p=p->next)
497 if (p->major == major &&
498 p->minor == minor) {
8d80900b 499 if (is_standard(p->name, NULL))
dd0781e5
NB
500 std = p->name;
501 else
502 return p->name;
503 }
504 return std;
82b27616
NB
505}
506
5787fa49 507#endif
82b27616 508
98c6faba 509unsigned long calc_sb_csum(mdp_super_t *super)
82b27616
NB
510{
511 unsigned int oldcsum = super->sb_csum;
56eb10c0 512 unsigned long long newcsum = 0;
82b27616
NB
513 unsigned long csum;
514 int i;
515 unsigned int *superc = (int*) super;
516 super->sb_csum = 0;
517
518 for(i=0; i<MD_SB_BYTES/4; i++)
519 newcsum+= superc[i];
520 csum = (newcsum& 0xffffffff) + (newcsum>>32);
521 super->sb_csum = oldcsum;
522 return csum;
523}
cd29a5c8 524
56eb10c0 525char *human_size(long long bytes)
cd29a5c8
NB
526{
527 static char buf[30];
56eb10c0 528
cd29a5c8 529
56eb10c0 530 if (bytes < 5000*1024)
cd29a5c8 531 buf[0]=0;
56eb10c0 532 else if (bytes < 2*1024LL*1024LL*1024LL)
e0d19036 533 sprintf(buf, " (%ld.%02ld MiB %ld.%02ld MB)",
56eb10c0 534 (long)(bytes>>20),
bd526cee 535 (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100),
56eb10c0 536 (long)(bytes/1000/1000),
bd526cee 537 (long)(((bytes%1000000)+5000)/10000)
56eb10c0 538 );
cd29a5c8 539 else
e0d19036 540 sprintf(buf, " (%ld.%02ld GiB %ld.%02ld GB)",
56eb10c0 541 (long)(bytes>>30),
bd526cee 542 (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100),
56eb10c0 543 (long)(bytes/1000LL/1000LL/1000LL),
5787fa49 544 (long)((((bytes/1000)%1000000)+5000)/10000)
56eb10c0 545 );
cd29a5c8
NB
546 return buf;
547}
e0d19036
NB
548
549char *human_size_brief(long long bytes)
550{
551 static char buf[30];
552
553
554 if (bytes < 5000*1024)
555 sprintf(buf, "%ld.%02ldKiB",
bd526cee 556 (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
e0d19036
NB
557 );
558 else if (bytes < 2*1024LL*1024LL*1024LL)
559 sprintf(buf, "%ld.%02ldMiB",
560 (long)(bytes>>20),
bd526cee 561 (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
e0d19036
NB
562 );
563 else
564 sprintf(buf, "%ld.%02ldGiB",
565 (long)(bytes>>30),
bd526cee 566 (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
e0d19036
NB
567 );
568 return buf;
569}
570
dd0781e5 571int get_mdp_major(void)
98c6faba 572{
dd0781e5
NB
573static int mdp_major = -1;
574 FILE *fl;
98c6faba
NB
575 char *w;
576 int have_block = 0;
577 int have_devices = 0;
578 int last_num = -1;
dd0781e5
NB
579
580 if (mdp_major != -1)
581 return mdp_major;
582 fl = fopen("/proc/devices", "r");
98c6faba 583 if (!fl)
dd0781e5 584 return -1;
98c6faba
NB
585 while ((w = conf_word(fl, 1))) {
586 if (have_block && strcmp(w, "devices:")==0)
587 have_devices = 1;
588 have_block = (strcmp(w, "Block")==0);
589 if (isdigit(w[0]))
590 last_num = atoi(w);
591 if (have_devices && strcmp(w, "mdp")==0)
592 mdp_major = last_num;
593 free(w);
594 }
595 fclose(fl);
dd0781e5 596 return mdp_major;
98c6faba
NB
597}
598
599
e0d19036 600
e0d19036
NB
601char *get_md_name(int dev)
602{
603 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
98c6faba 604 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
e0d19036
NB
605 static char devname[50];
606 struct stat stb;
98c6faba 607 dev_t rdev;
dd0781e5 608 char *dn;
98c6faba
NB
609
610 if (dev < 0) {
dd0781e5
NB
611 int mdp = get_mdp_major();
612 if (mdp < 0) return NULL;
0df46c2a 613 rdev = makedev(mdp, (-1-dev)<<6);
98c6faba
NB
614 sprintf(devname, "/dev/md/d%d", -1-dev);
615 if (stat(devname, &stb) == 0
616 && (S_IFMT&stb.st_mode) == S_IFBLK
617 && (stb.st_rdev == rdev))
618 return devname;
619 } else {
0df46c2a 620 rdev = makedev(MD_MAJOR, dev);
98c6faba
NB
621 sprintf(devname, "/dev/md%d", dev);
622 if (stat(devname, &stb) == 0
623 && (S_IFMT&stb.st_mode) == S_IFBLK
624 && (stb.st_rdev == rdev))
625 return devname;
626
627 sprintf(devname, "/dev/md/%d", dev);
628 if (stat(devname, &stb) == 0
629 && (S_IFMT&stb.st_mode) == S_IFBLK
630 && (stb.st_rdev == rdev))
631 return devname;
632 }
8d80900b 633 dn = map_dev(major(rdev), minor(rdev));
dd0781e5
NB
634 if (dn)
635 return dn;
e0d19036
NB
636 sprintf(devname, "/dev/.tmp.md%d", dev);
637 if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
dd0781e5
NB
638 if (errno != EEXIST)
639 return NULL;
e0d19036
NB
640
641 if (stat(devname, &stb) == 0
642 && (S_IFMT&stb.st_mode) == S_IFBLK
643 && (stb.st_rdev == rdev))
644 return devname;
645 unlink(devname);
646 return NULL;
647}
648
649void put_md_name(char *name)
650{
651 if (strncmp(name, "/dev/.tmp.md", 12)==0)
652 unlink(name);
653}