mdadm-1.5.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
424char *map_dev(int major, int minor)
425{
426 struct devmap *p;
427 if (!devlist_ready) {
98c6faba 428#ifndef __dietlibc__
82b27616 429 nftw("/dev", add_dev, 10, FTW_PHYS);
98c6faba
NB
430#else
431 ftw("/dev", add_dev, 10);
432#endif
82b27616
NB
433 devlist_ready=1;
434 }
435
436 for (p=devlist; p; p=p->next)
437 if (p->major == major &&
438 p->minor == minor)
439 return p->name;
440 return NULL;
441}
442
5787fa49 443#endif
82b27616 444
98c6faba 445unsigned long calc_sb_csum(mdp_super_t *super)
82b27616
NB
446{
447 unsigned int oldcsum = super->sb_csum;
56eb10c0 448 unsigned long long newcsum = 0;
82b27616
NB
449 unsigned long csum;
450 int i;
451 unsigned int *superc = (int*) super;
452 super->sb_csum = 0;
453
454 for(i=0; i<MD_SB_BYTES/4; i++)
455 newcsum+= superc[i];
456 csum = (newcsum& 0xffffffff) + (newcsum>>32);
457 super->sb_csum = oldcsum;
458 return csum;
459}
cd29a5c8 460
56eb10c0 461char *human_size(long long bytes)
cd29a5c8
NB
462{
463 static char buf[30];
56eb10c0 464
cd29a5c8 465
56eb10c0 466 if (bytes < 5000*1024)
cd29a5c8 467 buf[0]=0;
56eb10c0 468 else if (bytes < 2*1024LL*1024LL*1024LL)
e0d19036 469 sprintf(buf, " (%ld.%02ld MiB %ld.%02ld MB)",
56eb10c0 470 (long)(bytes>>20),
bd526cee 471 (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100),
56eb10c0 472 (long)(bytes/1000/1000),
bd526cee 473 (long)(((bytes%1000000)+5000)/10000)
56eb10c0 474 );
cd29a5c8 475 else
e0d19036 476 sprintf(buf, " (%ld.%02ld GiB %ld.%02ld GB)",
56eb10c0 477 (long)(bytes>>30),
bd526cee 478 (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100),
56eb10c0 479 (long)(bytes/1000LL/1000LL/1000LL),
5787fa49 480 (long)((((bytes/1000)%1000000)+5000)/10000)
56eb10c0 481 );
cd29a5c8
NB
482 return buf;
483}
e0d19036
NB
484
485char *human_size_brief(long long bytes)
486{
487 static char buf[30];
488
489
490 if (bytes < 5000*1024)
491 sprintf(buf, "%ld.%02ldKiB",
bd526cee 492 (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
e0d19036
NB
493 );
494 else if (bytes < 2*1024LL*1024LL*1024LL)
495 sprintf(buf, "%ld.%02ldMiB",
496 (long)(bytes>>20),
bd526cee 497 (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
e0d19036
NB
498 );
499 else
500 sprintf(buf, "%ld.%02ldGiB",
501 (long)(bytes>>30),
bd526cee 502 (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
e0d19036
NB
503 );
504 return buf;
505}
506
98c6faba
NB
507static int mdp_major = -1;
508void get_mdp_major(void)
509{
510 FILE *fl = fopen("/proc/devices", "r");
511 char *w;
512 int have_block = 0;
513 int have_devices = 0;
514 int last_num = -1;
515 if (!fl)
516 return;
517 while ((w = conf_word(fl, 1))) {
518 if (have_block && strcmp(w, "devices:")==0)
519 have_devices = 1;
520 have_block = (strcmp(w, "Block")==0);
521 if (isdigit(w[0]))
522 last_num = atoi(w);
523 if (have_devices && strcmp(w, "mdp")==0)
524 mdp_major = last_num;
525 free(w);
526 }
527 fclose(fl);
528}
529
530
e0d19036 531
e0d19036
NB
532char *get_md_name(int dev)
533{
534 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
98c6faba 535 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
e0d19036
NB
536 static char devname[50];
537 struct stat stb;
98c6faba
NB
538 dev_t rdev;
539
540 if (dev < 0) {
541
542 if (mdp_major < 0) get_mdp_major();
543 if (mdp_major < 0) return NULL;
544 rdev = MKDEV(mdp_major, (-1-dev)<<6);
545 sprintf(devname, "/dev/md/d%d", -1-dev);
546 if (stat(devname, &stb) == 0
547 && (S_IFMT&stb.st_mode) == S_IFBLK
548 && (stb.st_rdev == rdev))
549 return devname;
550 } else {
551 rdev = MKDEV(MD_MAJOR, dev);
552 sprintf(devname, "/dev/md%d", dev);
553 if (stat(devname, &stb) == 0
554 && (S_IFMT&stb.st_mode) == S_IFBLK
555 && (stb.st_rdev == rdev))
556 return devname;
557
558 sprintf(devname, "/dev/md/%d", dev);
559 if (stat(devname, &stb) == 0
560 && (S_IFMT&stb.st_mode) == S_IFBLK
561 && (stb.st_rdev == rdev))
562 return devname;
563 }
e0d19036
NB
564 sprintf(devname, "/dev/.tmp.md%d", dev);
565 if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
566 return NULL;
567
568 if (stat(devname, &stb) == 0
569 && (S_IFMT&stb.st_mode) == S_IFBLK
570 && (stb.st_rdev == rdev))
571 return devname;
572 unlink(devname);
573 return NULL;
574}
575
576void put_md_name(char *name)
577{
578 if (strncmp(name, "/dev/.tmp.md", 12)==0)
579 unlink(name);
580}