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