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