]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - util.c
04a689bb9523b06c179306a95890f27fa08be9f6
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
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.
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.
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
22 * Email: <neilb@cse.unsw.edu.au>
24 * School of Computer Science and Engineering
25 * The University of New South Wales
32 #include <sys/utsname.h>
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
41 int parse_uuid(char *str
, int uuid
[4])
43 int hit
= 0; /* number of Hex digIT */
46 for (i
=0; i
<4; i
++) uuid
[i
]=0;
52 else if (c
>='a' && c
<= 'f')
54 else if (c
>='A' && c
<= 'F')
56 else if (strchr(":. -", c
))
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
79 * Return version number as 24 but number - assume version parts
83 int md_get_version(int fd
)
88 if (fstat(fd
, &stb
)<0)
90 if ((S_IFMT
&stb
.st_mode
) != S_IFBLK
)
93 if (ioctl(fd
, RAID_VERSION
, &vers
) == 0)
94 return (vers
.major
*10000) + (vers
.minor
*100) + vers
.patchlevel
;
97 if (major(stb
.st_rdev
) == MD_MAJOR
)
103 int get_linux_version()
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);
118 return (a
*1000000)+(b
*1000)+c
;
121 int enough(int level
, int raid_disks
, int avail_disks
)
124 case 10: return 1; /* a lie, but it is hard to tell */
127 return avail_disks
>= 1;
130 return avail_disks
== raid_disks
;
132 return avail_disks
>= 1;
135 return avail_disks
>= raid_disks
-1;
137 return avail_disks
>= raid_disks
-2;
143 int same_uuid(int a
[4], int b
[4])
153 int check_ext2(int fd
, char *name
)
156 * Check for an ext2fs file system.
157 * Superblock is always 1K at 1K offset
159 * s_magic is le16 at 56 == 0xEF53
160 * report mtime - le32 at 44
162 * logblksize - le32 at 24
164 unsigned char sb
[1024];
167 if (lseek(fd
, 1024,0)!= 1024)
169 if (read(fd
, sb
, 1024)!= 1024)
171 if (sb
[56] != 0x53 || sb
[57] != 0xef)
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",
179 fprintf(stderr
," size=%dK mtime=%s",
180 size
*(1<<bsize
), ctime(&mtime
));
184 int check_reiser(int fd
, char *name
)
187 * superblock is at 64K
189 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
192 unsigned char sb
[1024];
194 if (lseek(fd
, 64*1024, 0) != 64*1024)
196 if (read(fd
, sb
, 1024) != 1024)
198 if (strncmp(sb
+52, "ReIsErFs",8)!=0 &&
199 strncmp(sb
+52, "ReIsEr2Fs",9)!=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);
208 int check_raid(int fd
, char *name
)
213 struct supertype
*st
= guess_super(fd
);
216 st
->ss
->load_super(st
, fd
, &super
, name
);
217 /* Looks like a raid array .. */
218 fprintf(stderr
, Name
": %s appears to be part of a raid array:\n",
220 st
->ss
->getinfo_super(&info
, super
);
222 crtime
= info
.array
.ctime
;
223 fprintf(stderr
, " level=%d devices=%d ctime=%s",
224 info
.array
.level
, info
.array
.raid_disks
, ctime(&crtime
));
232 for (i
=0; i
<5; i
++) {
234 fprintf(stderr
, "%s%s", mesg
, add
);
236 if (fgets(buf
, 100, stdin
)==NULL
)
238 if (buf
[0]=='y' || buf
[0]=='Y')
240 if (buf
[0]=='n' || buf
[0]=='N')
244 fprintf(stderr
, Name
": assuming 'no'\n");
248 char *map_num(mapping_t
*map
, int num
)
258 int map_name(mapping_t
*map
, char *name
)
261 if (strcmp(map
->name
, name
)==0)
269 int is_standard(char *dev
, int *nump
)
271 /* tests if dev is a "standard" md dev name.
272 * i.e if the last component is "/dNN" or "/mdNN",
273 * where NN is a string of digits
275 char *d
= strrchr(dev
, '/');
280 if (strncmp(d
, "/d",2)==0)
281 d
+= 2, type
=1; /* /dev/md/dN{pM} */
282 else if (strncmp(d
, "/md_d", 5)==0)
283 d
+= 5, type
=1; /* /dev/md_dNpM */
284 else if (strncmp(d
, "/md", 3)==0)
285 d
+= 3, type
=-1; /* /dev/mdN */
286 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3)==0)
287 type
= -1; /* /dev/md/N */
297 if (nump
) *nump
= num
;
304 * convert a major/minor pair for a block device into a name in /dev, if possible.
305 * On the first call, walk /dev collecting name.
306 * Put them in a simple linked listfor now.
313 int devlist_ready
= 0;
316 char *map_dev(int major
, int minor
)
319 fprintf(stderr
, "Warning - fail to map %d,%d to a device name\n",
325 #define __USE_XOPEN_EXTENDED
330 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
332 int add_dev(const char *name
, const struct stat
*stb
, int flag
)
335 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
336 char *n
= strdup(name
);
337 struct devmap
*dm
= malloc(sizeof(*dm
));
339 dm
->major
= major(stb
->st_rdev
);
340 dm
->minor
= minor(stb
->st_rdev
);
350 * Find a block device with the right major/minor number.
351 * Avoid /dev/mdNN and /dev/md/dNN if possible
353 char *map_dev(int major
, int minor
)
357 if (!devlist_ready
) {
359 nftw("/dev", add_dev
, 10, FTW_PHYS
);
361 ftw("/dev", add_dev
, 10);
366 for (p
=devlist
; p
; p
=p
->next
)
367 if (p
->major
== major
&&
369 if (is_standard(p
->name
, NULL
))
379 unsigned long calc_csum(void *super
, int bytes
)
381 unsigned long long newcsum
= 0;
384 unsigned int *superc
= (unsigned int*) super
;
386 for(i
=0; i
<bytes
/4; i
++)
388 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
390 /* The in-kernel checksum calculation is always 16bit on
391 * the alpha, though it is 32 bit on i386...
392 * I wonder what it is elsewhere... (it uses and API in
393 * a way that it shouldn't).
395 csum
= (csum
& 0xffff) + (csum
>> 16);
396 csum
= (csum
& 0xffff) + (csum
>> 16);
401 char *human_size(long long bytes
)
406 if (bytes
< 5000*1024)
408 else if (bytes
< 2*1024LL*1024LL*1024LL)
409 sprintf(buf
, " (%ld.%02ld MiB %ld.%02ld MB)",
411 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100),
412 (long)(bytes
/1000/1000),
413 (long)(((bytes
%1000000)+5000)/10000)
416 sprintf(buf
, " (%ld.%02ld GiB %ld.%02ld GB)",
418 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100),
419 (long)(bytes
/1000LL/1000LL/1000LL),
420 (long)((((bytes
/1000)%1000000)+5000)/10000)
425 char *human_size_brief(long long bytes
)
430 if (bytes
< 5000*1024)
431 sprintf(buf
, "%ld.%02ldKiB",
432 (long)(bytes
>>10), (long)(((bytes
&1023)*100+512)/1024)
434 else if (bytes
< 2*1024LL*1024LL*1024LL)
435 sprintf(buf
, "%ld.%02ldMiB",
437 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100)
440 sprintf(buf
, "%ld.%02ldGiB",
442 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100)
447 int get_mdp_major(void)
449 static int mdp_major
= -1;
453 int have_devices
= 0;
458 fl
= fopen("/proc/devices", "r");
461 while ((w
= conf_word(fl
, 1))) {
462 if (have_block
&& strcmp(w
, "devices:")==0)
464 have_block
= (strcmp(w
, "Block")==0);
467 if (have_devices
&& strcmp(w
, "mdp")==0)
468 mdp_major
= last_num
;
477 char *get_md_name(int dev
)
479 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
480 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
481 static char devname
[50];
487 int mdp
= get_mdp_major();
488 if (mdp
< 0) return NULL
;
489 rdev
= makedev(mdp
, (-1-dev
)<<6);
490 sprintf(devname
, "/dev/md/d%d", -1-dev
);
491 if (stat(devname
, &stb
) == 0
492 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
493 && (stb
.st_rdev
== rdev
))
496 rdev
= makedev(MD_MAJOR
, dev
);
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
))
503 sprintf(devname
, "/dev/md/%d", dev
);
504 if (stat(devname
, &stb
) == 0
505 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
506 && (stb
.st_rdev
== rdev
))
509 dn
= map_dev(major(rdev
), minor(rdev
));
512 sprintf(devname
, "/dev/.tmp.md%d", dev
);
513 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
517 if (stat(devname
, &stb
) == 0
518 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
519 && (stb
.st_rdev
== rdev
))
525 void put_md_name(char *name
)
527 if (strncmp(name
, "/dev/.tmp.md", 12)==0)
534 struct superswitch
*superlist
[] = { &super0
, &super1
, NULL
};
536 struct supertype
*super_by_version(int vers
, int minor
)
538 struct supertype
*st
= malloc(sizeof(*st
));
545 st
->minor_version
= minor
;
549 struct supertype
*guess_super(int fd
)
551 /* try each load_super to find the best match,
552 * and return the best superswitch
554 struct superswitch
*ss
;
555 struct supertype
*st
;
556 unsigned long besttime
= 0;
562 st
= malloc(sizeof(*st
));
563 memset(st
, 0, sizeof(*st
));
564 for (i
=0 ; superlist
[i
]; i
++) {
567 rv
= ss
->load_super(st
, fd
, &sbp
, NULL
);
570 ss
->getinfo_super(&info
, sbp
);
571 if (bestsuper
== -1 ||
572 besttime
< info
.array
.ctime
) {
574 besttime
= info
.array
.ctime
;
580 if (bestsuper
!= -1) {
582 rv
= superlist
[bestsuper
]->load_super(st
, fd
, &sbp
, NULL
);