]> git.ipfire.org Git - thirdparty/mdadm.git/blob - util.c
mdadm-1.7.0
[thirdparty/mdadm.git] / util.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
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
30 #include "mdadm.h"
31 #include "md_p.h"
32 #include <sys/utsname.h>
33 #include <ctype.h>
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 */
41 int 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
60 if (hit<32) {
61 uuid[hit/8] <<= 4;
62 uuid[hit/8] += n;
63 }
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
83 int 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)
94 return (vers.major*10000) + (vers.minor*100) + vers.patchlevel;
95 if (errno == EACCES)
96 return -1;
97 if (MAJOR(stb.st_rdev) == MD_MAJOR)
98 return (3600);
99 return -1;
100 }
101
102
103 int get_linux_version()
104 {
105 struct utsname name;
106 char *cp;
107 int a,b,c;
108 if (uname(&name) <0)
109 return -1;
110
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
118 return (a*1000000)+(b*1000)+c;
119 }
120
121 int enough(int level, int raid_disks, int avail_disks)
122 {
123 switch (level) {
124 case 10: return 1; /* a lie, but it is hard to tell */
125
126 case -4:
127 return avail_disks>= 1;
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;
136 case 6:
137 return avail_disks >= raid_disks-2;
138 default:
139 return 0;
140 }
141 }
142
143 int 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
153 void 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
167 int 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
201 int 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 */
214 unsigned long size;
215 unsigned long long offset;
216
217 if (ioctl(fd, BLKGETSIZE, &size))
218 return 1;
219
220 if (size < MD_RESERVED_SECTORS*2)
221 return 2;
222
223 offset = MD_NEW_SIZE_SECTORS(size);
224
225 offset *= 512;
226
227 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
228
229 if (lseek64(fd, offset, 0)< 0LL)
230 return 3;
231
232 if (read(fd, super, sizeof(*super)) != sizeof(*super))
233 return 4;
234
235 if (super->md_magic != MD_SB_MAGIC)
236 return 5;
237
238 if (super->major_version != 0)
239 return 6;
240 return 0;
241 }
242
243 int store_super(int fd, mdp_super_t *super)
244 {
245 long size;
246 long long offset;
247
248 if (ioctl(fd, BLKGETSIZE, &size))
249 return 1;
250
251 if (size < MD_RESERVED_SECTORS*2)
252 return 2;
253
254 offset = MD_NEW_SIZE_SECTORS(size);
255
256 offset *= 512;
257
258 if (lseek64(fd, offset, 0)< 0LL)
259 return 3;
260
261 if (write(fd, super, sizeof(*super)) != sizeof(*super))
262 return 4;
263
264 return 0;
265 }
266
267
268
269 int check_ext2(int fd, char *name)
270 {
271 /*
272 * Check for an ext2fs file system.
273 * Superblock is always 1K at 1K offset
274 *
275 * s_magic is le16 at 56 == 0xEF53
276 * report mtime - le32 at 44
277 * blocks - le32 at 4
278 * logblksize - le32 at 24
279 */
280 unsigned char sb[1024];
281 time_t mtime;
282 int size, bsize;
283 if (lseek(fd, 1024,0)!= 1024)
284 return 0;
285 if (read(fd, sb, 1024)!= 1024)
286 return 0;
287 if (sb[56] != 0x53 || sb[57] != 0xef)
288 return 0;
289
290 mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8;
291 bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8;
292 size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8;
293 fprintf(stderr, Name ": %s appears to contain an ext2fs file system\n",
294 name);
295 fprintf(stderr," size=%dK mtime=%s",
296 size*(1<<bsize), ctime(&mtime));
297 return 1;
298 }
299
300 int check_reiser(int fd, char *name)
301 {
302 /*
303 * superblock is at 64K
304 * size is 1024;
305 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
306 *
307 */
308 unsigned char sb[1024];
309 int size;
310 if (lseek(fd, 64*1024, 0) != 64*1024)
311 return 0;
312 if (read(fd, sb, 1024) != 1024)
313 return 0;
314 if (strncmp(sb+52, "ReIsErFs",8)!=0 &&
315 strncmp(sb+52, "ReIsEr2Fs",9)!=0)
316 return 0;
317 fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name);
318 size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8;
319 fprintf(stderr, " size = %dK\n", size*4);
320
321 return 1;
322 }
323
324 int check_raid(int fd, char *name)
325 {
326 mdp_super_t super;
327 time_t crtime;
328 if (load_super(fd, &super))
329 return 0;
330 /* Looks like a raid array .. */
331 fprintf(stderr, Name ": %s appears to be part of a raid array:\n",
332 name);
333 crtime = super.ctime;
334 fprintf(stderr, " level=%d devices=%d ctime=%s",
335 super.level, super.raid_disks, ctime(&crtime));
336 return 1;
337 }
338
339
340 int ask(char *mesg)
341 {
342 char *add = "";
343 int i;
344 for (i=0; i<5; i++) {
345 char buf[100];
346 fprintf(stderr, "%s%s", mesg, add);
347 fflush(stderr);
348 if (fgets(buf, 100, stdin)==NULL)
349 return 0;
350 if (buf[0]=='y' || buf[0]=='Y')
351 return 1;
352 if (buf[0]=='n' || buf[0]=='N')
353 return 0;
354 add = "(y/n) ";
355 }
356 fprintf(stderr, Name ": assuming 'no'\n");
357 return 0;
358 }
359
360 char *map_num(mapping_t *map, int num)
361 {
362 while (map->name) {
363 if (map->num == num)
364 return map->name;
365 map++;
366 }
367 return NULL;
368 }
369
370 int map_name(mapping_t *map, char *name)
371 {
372 while (map->name) {
373 if (strcmp(map->name, name)==0)
374 return map->num;
375 map++;
376 }
377 return UnSet;
378 }
379
380
381 int is_standard(char *dev)
382 {
383 /* tests if dev is a "standard" md dev name.
384 * i.e if the last component is "/dNN" or "/mdNN",
385 * where NN is a string of digits
386 */
387 dev = strrchr(dev, '/');
388 if (!dev)
389 return 0;
390 if (strncmp(dev, "/d",2)==0)
391 dev += 2;
392 else if (strncmp(dev, "/md", 3)==0)
393 dev += 3;
394 else
395 return 0;
396 if (!*dev)
397 return 0;
398 while (isdigit(*dev))
399 dev++;
400 if (*dev)
401 return 0;
402 return 1;
403 }
404
405
406 /*
407 * convert a major/minor pair for a block device into a name in /dev, if possible.
408 * On the first call, walk /dev collecting name.
409 * Put them in a simple linked listfor now.
410 */
411 struct devmap {
412 int major, minor;
413 char *name;
414 struct devmap *next;
415 } *devlist = NULL;
416 int devlist_ready = 0;
417
418 #ifdef UCLIBC
419 char *map_dev(int major, int minor)
420 {
421 #if 0
422 fprintf(stderr, "Warning - fail to map %d,%d to a device name\n",
423 major, minor);
424 #endif
425 return NULL;
426 }
427 #else
428 #define __USE_XOPEN_EXTENDED
429 #include <ftw.h>
430
431
432 #ifndef __dietlibc__
433 int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
434 #else
435 int add_dev(const char *name, const struct stat *stb, int flag)
436 #endif
437 {
438 if ((stb->st_mode&S_IFMT)== S_IFBLK) {
439 char *n = strdup(name);
440 struct devmap *dm = malloc(sizeof(*dm));
441 if (dm) {
442 dm->major = MAJOR(stb->st_rdev);
443 dm->minor = MINOR(stb->st_rdev);
444 dm->name = n;
445 dm->next = devlist;
446 devlist = dm;
447 }
448 }
449 return 0;
450 }
451
452 /*
453 * Find a block device with the right major/minor number.
454 * Avoid /dev/mdNN and /dev/md/dNN is possible
455 */
456 char *map_dev(int major, int minor)
457 {
458 struct devmap *p;
459 char *std = NULL;
460 if (!devlist_ready) {
461 #ifndef __dietlibc__
462 nftw("/dev", add_dev, 10, FTW_PHYS);
463 #else
464 ftw("/dev", add_dev, 10);
465 #endif
466 devlist_ready=1;
467 }
468
469 for (p=devlist; p; p=p->next)
470 if (p->major == major &&
471 p->minor == minor) {
472 if (is_standard(p->name))
473 std = p->name;
474 else
475 return p->name;
476 }
477 return std;
478 }
479
480 #endif
481
482 unsigned long calc_sb_csum(mdp_super_t *super)
483 {
484 unsigned int oldcsum = super->sb_csum;
485 unsigned long long newcsum = 0;
486 unsigned long csum;
487 int i;
488 unsigned int *superc = (int*) super;
489 super->sb_csum = 0;
490
491 for(i=0; i<MD_SB_BYTES/4; i++)
492 newcsum+= superc[i];
493 csum = (newcsum& 0xffffffff) + (newcsum>>32);
494 super->sb_csum = oldcsum;
495 return csum;
496 }
497
498 char *human_size(long long bytes)
499 {
500 static char buf[30];
501
502
503 if (bytes < 5000*1024)
504 buf[0]=0;
505 else if (bytes < 2*1024LL*1024LL*1024LL)
506 sprintf(buf, " (%ld.%02ld MiB %ld.%02ld MB)",
507 (long)(bytes>>20),
508 (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100),
509 (long)(bytes/1000/1000),
510 (long)(((bytes%1000000)+5000)/10000)
511 );
512 else
513 sprintf(buf, " (%ld.%02ld GiB %ld.%02ld GB)",
514 (long)(bytes>>30),
515 (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100),
516 (long)(bytes/1000LL/1000LL/1000LL),
517 (long)((((bytes/1000)%1000000)+5000)/10000)
518 );
519 return buf;
520 }
521
522 char *human_size_brief(long long bytes)
523 {
524 static char buf[30];
525
526
527 if (bytes < 5000*1024)
528 sprintf(buf, "%ld.%02ldKiB",
529 (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
530 );
531 else if (bytes < 2*1024LL*1024LL*1024LL)
532 sprintf(buf, "%ld.%02ldMiB",
533 (long)(bytes>>20),
534 (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
535 );
536 else
537 sprintf(buf, "%ld.%02ldGiB",
538 (long)(bytes>>30),
539 (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
540 );
541 return buf;
542 }
543
544 int get_mdp_major(void)
545 {
546 static int mdp_major = -1;
547 FILE *fl;
548 char *w;
549 int have_block = 0;
550 int have_devices = 0;
551 int last_num = -1;
552
553 if (mdp_major != -1)
554 return mdp_major;
555 fl = fopen("/proc/devices", "r");
556 if (!fl)
557 return -1;
558 while ((w = conf_word(fl, 1))) {
559 if (have_block && strcmp(w, "devices:")==0)
560 have_devices = 1;
561 have_block = (strcmp(w, "Block")==0);
562 if (isdigit(w[0]))
563 last_num = atoi(w);
564 if (have_devices && strcmp(w, "mdp")==0)
565 mdp_major = last_num;
566 free(w);
567 }
568 fclose(fl);
569 return mdp_major;
570 }
571
572
573
574 char *get_md_name(int dev)
575 {
576 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
577 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
578 static char devname[50];
579 struct stat stb;
580 dev_t rdev;
581 char *dn;
582
583 if (dev < 0) {
584 int mdp = get_mdp_major();
585 if (mdp < 0) return NULL;
586 rdev = MKDEV(mdp, (-1-dev)<<6);
587 sprintf(devname, "/dev/md/d%d", -1-dev);
588 if (stat(devname, &stb) == 0
589 && (S_IFMT&stb.st_mode) == S_IFBLK
590 && (stb.st_rdev == rdev))
591 return devname;
592 } else {
593 rdev = MKDEV(MD_MAJOR, dev);
594 sprintf(devname, "/dev/md%d", dev);
595 if (stat(devname, &stb) == 0
596 && (S_IFMT&stb.st_mode) == S_IFBLK
597 && (stb.st_rdev == rdev))
598 return devname;
599
600 sprintf(devname, "/dev/md/%d", dev);
601 if (stat(devname, &stb) == 0
602 && (S_IFMT&stb.st_mode) == S_IFBLK
603 && (stb.st_rdev == rdev))
604 return devname;
605 }
606 dn = map_dev(MAJOR(rdev), MINOR(rdev));
607 if (dn)
608 return dn;
609 sprintf(devname, "/dev/.tmp.md%d", dev);
610 if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
611 if (errno != EEXIST)
612 return NULL;
613
614 if (stat(devname, &stb) == 0
615 && (S_IFMT&stb.st_mode) == S_IFBLK
616 && (stb.st_rdev == rdev))
617 return devname;
618 unlink(devname);
619 return NULL;
620 }
621
622 void put_md_name(char *name)
623 {
624 if (strncmp(name, "/dev/.tmp.md", 12)==0)
625 unlink(name);
626 }