Add a 'super-switch' so that different format superblocks can be used.
[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;
198 if (strncmp(sb+52, "ReIsErFs",8)!=0 &&
199 strncmp(sb+52, "ReIsEr2Fs",9)!=0)
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;
f9ce90ba
NB
213
214 int i;
215 for (i=0; superlist[i]; i++) {
216 if (superlist[i]->load_super(fd, &super, name))
217 continue;
218 /* Looks like a raid array .. */
219 fprintf(stderr, Name ": %s appears to be part of a raid array:\n",
220 name);
221 superlist[i]->getinfo_super(&info, super);
222 free(super);
223 crtime = info.array.ctime;
224 fprintf(stderr, " level=%d devices=%d ctime=%s",
225 info.array.level, info.array.raid_disks, ctime(&crtime));
226 return 1;
227 }
228 return 0;
682c7051
NB
229}
230
231
232int ask(char *mesg)
233{
234 char *add = "";
235 int i;
236 for (i=0; i<5; i++) {
237 char buf[100];
238 fprintf(stderr, "%s%s", mesg, add);
239 fflush(stderr);
240 if (fgets(buf, 100, stdin)==NULL)
241 return 0;
242 if (buf[0]=='y' || buf[0]=='Y')
243 return 1;
244 if (buf[0]=='n' || buf[0]=='N')
245 return 0;
246 add = "(y/n) ";
247 }
248 fprintf(stderr, Name ": assuming 'no'\n");
249 return 0;
250}
251
252char *map_num(mapping_t *map, int num)
253{
254 while (map->name) {
255 if (map->num == num)
256 return map->name;
257 map++;
258 }
259 return NULL;
260}
261
262int map_name(mapping_t *map, char *name)
263{
264 while (map->name) {
265 if (strcmp(map->name, name)==0)
266 return map->num;
267 map++;
268 }
98c6faba 269 return UnSet;
682c7051 270}
82b27616 271
e5329c37 272
8d80900b 273int is_standard(char *dev, int *nump)
e5329c37
NB
274{
275 /* tests if dev is a "standard" md dev name.
276 * i.e if the last component is "/dNN" or "/mdNN",
277 * where NN is a string of digits
278 */
8d80900b
NB
279 char *d = strrchr(dev, '/');
280 int type=0;
281 int num;
282 if (!d)
e5329c37 283 return 0;
8d80900b
NB
284 if (strncmp(d, "/d",2)==0)
285 d += 2, type=1; /* /dev/md/dN{pM} */
286 else if (strncmp(d, "/md_d", 5)==0)
287 d += 5, type=1; /* /dev/md_dNpM */
288 else if (strncmp(d, "/md", 3)==0)
289 d += 3, type=-1; /* /dev/mdN */
290 else if (d-dev > 3 && strncmp(d-2, "md/", 3)==0)
291 type = -1; /* /dev/md/N */
e5329c37
NB
292 else
293 return 0;
8d80900b 294 if (!*d)
e5329c37 295 return 0;
8d80900b
NB
296 num = atoi(d);
297 while (isdigit(*d))
298 d++;
299 if (*d)
e5329c37 300 return 0;
8d80900b
NB
301 if (nump) *nump = num;
302
303 return type;
e5329c37
NB
304}
305
306
82b27616
NB
307/*
308 * convert a major/minor pair for a block device into a name in /dev, if possible.
309 * On the first call, walk /dev collecting name.
310 * Put them in a simple linked listfor now.
311 */
312struct devmap {
313 int major, minor;
314 char *name;
315 struct devmap *next;
316} *devlist = NULL;
317int devlist_ready = 0;
318
5787fa49
NB
319#ifdef UCLIBC
320char *map_dev(int major, int minor)
321{
322#if 0
323 fprintf(stderr, "Warning - fail to map %d,%d to a device name\n",
324 major, minor);
325#endif
326 return NULL;
327}
328#else
82b27616
NB
329#define __USE_XOPEN_EXTENDED
330#include <ftw.h>
331
332
98c6faba 333#ifndef __dietlibc__
82b27616 334int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
98c6faba
NB
335#else
336int add_dev(const char *name, const struct stat *stb, int flag)
337#endif
82b27616
NB
338{
339 if ((stb->st_mode&S_IFMT)== S_IFBLK) {
340 char *n = strdup(name);
341 struct devmap *dm = malloc(sizeof(*dm));
342 if (dm) {
0df46c2a
NB
343 dm->major = major(stb->st_rdev);
344 dm->minor = minor(stb->st_rdev);
82b27616
NB
345 dm->name = n;
346 dm->next = devlist;
347 devlist = dm;
348 }
349 }
350 return 0;
351}
352
dd0781e5
NB
353/*
354 * Find a block device with the right major/minor number.
8d80900b 355 * Avoid /dev/mdNN and /dev/md/dNN if possible
dd0781e5 356 */
82b27616
NB
357char *map_dev(int major, int minor)
358{
dd0781e5
NB
359 struct devmap *p;
360 char *std = NULL;
361 if (!devlist_ready) {
98c6faba 362#ifndef __dietlibc__
dd0781e5 363 nftw("/dev", add_dev, 10, FTW_PHYS);
98c6faba 364#else
dd0781e5 365 ftw("/dev", add_dev, 10);
98c6faba 366#endif
dd0781e5
NB
367 devlist_ready=1;
368 }
82b27616 369
dd0781e5
NB
370 for (p=devlist; p; p=p->next)
371 if (p->major == major &&
372 p->minor == minor) {
8d80900b 373 if (is_standard(p->name, NULL))
dd0781e5
NB
374 std = p->name;
375 else
376 return p->name;
377 }
378 return std;
82b27616
NB
379}
380
5787fa49 381#endif
82b27616 382
4b1ac34b 383unsigned long calc_csum(void *super, int bytes)
82b27616 384{
56eb10c0 385 unsigned long long newcsum = 0;
82b27616 386 int i;
4b1ac34b
NB
387 unsigned int csum;
388 unsigned int *superc = (unsigned int*) super;
82b27616 389
4b1ac34b 390 for(i=0; i<bytes/4; i++)
82b27616
NB
391 newcsum+= superc[i];
392 csum = (newcsum& 0xffffffff) + (newcsum>>32);
82b27616
NB
393 return csum;
394}
cd29a5c8 395
56eb10c0 396char *human_size(long long bytes)
cd29a5c8
NB
397{
398 static char buf[30];
56eb10c0 399
cd29a5c8 400
56eb10c0 401 if (bytes < 5000*1024)
cd29a5c8 402 buf[0]=0;
56eb10c0 403 else if (bytes < 2*1024LL*1024LL*1024LL)
e0d19036 404 sprintf(buf, " (%ld.%02ld MiB %ld.%02ld MB)",
56eb10c0 405 (long)(bytes>>20),
bd526cee 406 (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100),
56eb10c0 407 (long)(bytes/1000/1000),
bd526cee 408 (long)(((bytes%1000000)+5000)/10000)
56eb10c0 409 );
cd29a5c8 410 else
e0d19036 411 sprintf(buf, " (%ld.%02ld GiB %ld.%02ld GB)",
56eb10c0 412 (long)(bytes>>30),
bd526cee 413 (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100),
56eb10c0 414 (long)(bytes/1000LL/1000LL/1000LL),
5787fa49 415 (long)((((bytes/1000)%1000000)+5000)/10000)
56eb10c0 416 );
cd29a5c8
NB
417 return buf;
418}
e0d19036
NB
419
420char *human_size_brief(long long bytes)
421{
422 static char buf[30];
423
424
425 if (bytes < 5000*1024)
426 sprintf(buf, "%ld.%02ldKiB",
bd526cee 427 (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
e0d19036
NB
428 );
429 else if (bytes < 2*1024LL*1024LL*1024LL)
430 sprintf(buf, "%ld.%02ldMiB",
431 (long)(bytes>>20),
bd526cee 432 (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
e0d19036
NB
433 );
434 else
435 sprintf(buf, "%ld.%02ldGiB",
436 (long)(bytes>>30),
bd526cee 437 (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
e0d19036
NB
438 );
439 return buf;
440}
441
dd0781e5 442int get_mdp_major(void)
98c6faba 443{
dd0781e5
NB
444static int mdp_major = -1;
445 FILE *fl;
98c6faba
NB
446 char *w;
447 int have_block = 0;
448 int have_devices = 0;
449 int last_num = -1;
dd0781e5
NB
450
451 if (mdp_major != -1)
452 return mdp_major;
453 fl = fopen("/proc/devices", "r");
98c6faba 454 if (!fl)
dd0781e5 455 return -1;
98c6faba
NB
456 while ((w = conf_word(fl, 1))) {
457 if (have_block && strcmp(w, "devices:")==0)
458 have_devices = 1;
459 have_block = (strcmp(w, "Block")==0);
460 if (isdigit(w[0]))
461 last_num = atoi(w);
462 if (have_devices && strcmp(w, "mdp")==0)
463 mdp_major = last_num;
464 free(w);
465 }
466 fclose(fl);
dd0781e5 467 return mdp_major;
98c6faba
NB
468}
469
470
e0d19036 471
e0d19036
NB
472char *get_md_name(int dev)
473{
474 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
98c6faba 475 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
e0d19036
NB
476 static char devname[50];
477 struct stat stb;
98c6faba 478 dev_t rdev;
dd0781e5 479 char *dn;
98c6faba
NB
480
481 if (dev < 0) {
dd0781e5
NB
482 int mdp = get_mdp_major();
483 if (mdp < 0) return NULL;
0df46c2a 484 rdev = makedev(mdp, (-1-dev)<<6);
98c6faba
NB
485 sprintf(devname, "/dev/md/d%d", -1-dev);
486 if (stat(devname, &stb) == 0
487 && (S_IFMT&stb.st_mode) == S_IFBLK
488 && (stb.st_rdev == rdev))
489 return devname;
490 } else {
0df46c2a 491 rdev = makedev(MD_MAJOR, dev);
98c6faba
NB
492 sprintf(devname, "/dev/md%d", dev);
493 if (stat(devname, &stb) == 0
494 && (S_IFMT&stb.st_mode) == S_IFBLK
495 && (stb.st_rdev == rdev))
496 return devname;
497
498 sprintf(devname, "/dev/md/%d", dev);
499 if (stat(devname, &stb) == 0
500 && (S_IFMT&stb.st_mode) == S_IFBLK
501 && (stb.st_rdev == rdev))
502 return devname;
503 }
8d80900b 504 dn = map_dev(major(rdev), minor(rdev));
dd0781e5
NB
505 if (dn)
506 return dn;
e0d19036
NB
507 sprintf(devname, "/dev/.tmp.md%d", dev);
508 if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
dd0781e5
NB
509 if (errno != EEXIST)
510 return NULL;
e0d19036
NB
511
512 if (stat(devname, &stb) == 0
513 && (S_IFMT&stb.st_mode) == S_IFBLK
514 && (stb.st_rdev == rdev))
515 return devname;
516 unlink(devname);
517 return NULL;
518}
519
520void put_md_name(char *name)
521{
522 if (strncmp(name, "/dev/.tmp.md", 12)==0)
523 unlink(name);
524}
f9ce90ba
NB
525
526
527
528
529struct superswitch *superlist[] = { &super0, NULL };
530
531struct superswitch *super_by_version(int vers)
532{
533 if (vers == 0) return &super0;
534 return NULL;
535}
536
537struct superswitch *guess_super(int fd, char *dev)
538{
539 /* try each load_super to find the best match,
540 * and return the best superswitch
541 */
542 struct superswitch *best = NULL, *ss;
543 int bestrv = 0;
544 void *sbp = NULL;
545 int i;
546
547 for (i=0 ; superlist[i]; i++) {
548 int rv;
549 ss = superlist[i];
550 rv = ss->load_super(fd, &sbp, NULL);
551 if (rv == 0) {
552 free(sbp);
553 return ss;
554 }
555 if (rv > bestrv) {
556 bestrv = rv;
557 best = ss;
558 }
559 }
560 if (bestrv > 2) /* FIXME */
561 return best;
562 return NULL;
563}