]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - util.c
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
)
125 return avail_disks
>= 1;
128 return avail_disks
== raid_disks
;
130 return avail_disks
>= 1;
133 return avail_disks
>= raid_disks
-1;
135 return avail_disks
>= raid_disks
-2;
141 int same_uuid(int a
[4], int b
[4])
151 void uuid_from_super(int uuid
[4], mdp_super_t
*super
)
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
;
165 int compare_super(mdp_super_t
*first
, mdp_super_t
*second
)
169 * 0 same, or first was empty, and second was copied
170 * 1 second had wrong number
174 int uuid1
[4], uuid2
[4];
175 if (second
->md_magic
!= MD_SB_MAGIC
)
177 if (first
-> md_magic
!= MD_SB_MAGIC
) {
178 memcpy(first
, second
, sizeof(*first
));
182 uuid_from_super(uuid1
, first
);
183 uuid_from_super(uuid2
, second
);
184 if (!same_uuid(uuid1
, uuid2
))
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
)
199 int load_super(int fd
, mdp_super_t
*super
)
201 /* try to read in the superblock
210 * 6 - wrong major version
213 unsigned long long offset
;
215 if (ioctl(fd
, BLKGETSIZE
, &size
))
218 if (size
< MD_RESERVED_SECTORS
*2)
221 offset
= MD_NEW_SIZE_SECTORS(size
);
225 ioctl(fd
, BLKFLSBUF
, 0); /* make sure we read current data */
227 if (lseek64(fd
, offset
, 0)< 0LL)
230 if (read(fd
, super
, sizeof(*super
)) != sizeof(*super
))
233 if (super
->md_magic
!= MD_SB_MAGIC
)
236 if (super
->major_version
!= 0)
241 int store_super(int fd
, mdp_super_t
*super
)
246 if (ioctl(fd
, BLKGETSIZE
, &size
))
249 if (size
< MD_RESERVED_SECTORS
*2)
252 offset
= MD_NEW_SIZE_SECTORS(size
);
256 if (lseek64(fd
, offset
, 0)< 0LL)
259 if (write(fd
, super
, sizeof(*super
)) != sizeof(*super
))
267 int check_ext2(int fd
, char *name
)
270 * Check for an ext2fs file system.
271 * Superblock is always 1K at 1K offset
273 * s_magic is le16 at 56 == 0xEF53
274 * report mtime - le32 at 44
276 * logblksize - le32 at 24
278 unsigned char sb
[1024];
281 if (lseek(fd
, 1024,0)!= 1024)
283 if (read(fd
, sb
, 1024)!= 1024)
285 if (sb
[56] != 0x53 || sb
[57] != 0xef)
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",
293 fprintf(stderr
," size=%dK mtime=%s",
294 size
*(1<<bsize
), ctime(&mtime
));
298 int check_reiser(int fd
, char *name
)
301 * superblock is at 64K
303 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
306 unsigned char sb
[1024];
308 if (lseek(fd
, 64*1024, 0) != 64*1024)
310 if (read(fd
, sb
, 1024) != 1024)
312 if (strncmp(sb
+52, "ReIsErFs",8)!=0 &&
313 strncmp(sb
+52, "ReIsEr2Fs",9)!=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);
322 int check_raid(int fd
, char *name
)
326 if (load_super(fd
, &super
))
328 /* Looks like a raid array .. */
329 fprintf(stderr
, Name
": %s appears to be part of a raid array:\n",
331 crtime
= super
.ctime
;
332 fprintf(stderr
, " level=%d devices=%d ctime=%s",
333 super
.level
, super
.raid_disks
, ctime(&crtime
));
342 for (i
=0; i
<5; i
++) {
344 fprintf(stderr
, "%s%s", mesg
, add
);
346 if (fgets(buf
, 100, stdin
)==NULL
)
348 if (buf
[0]=='y' || buf
[0]=='Y')
350 if (buf
[0]=='n' || buf
[0]=='N')
354 fprintf(stderr
, Name
": assuming 'no'\n");
358 char *map_num(mapping_t
*map
, int num
)
368 int map_name(mapping_t
*map
, char *name
)
371 if (strcmp(map
->name
, name
)==0)
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.
388 int devlist_ready
= 0;
391 char *map_dev(int major
, int minor
)
394 fprintf(stderr
, "Warning - fail to map %d,%d to a device name\n",
400 #define __USE_XOPEN_EXTENDED
405 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
407 int add_dev(const char *name
, const struct stat
*stb
, int flag
)
410 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
411 char *n
= strdup(name
);
412 struct devmap
*dm
= malloc(sizeof(*dm
));
414 dm
->major
= MAJOR(stb
->st_rdev
);
415 dm
->minor
= MINOR(stb
->st_rdev
);
424 char *map_dev(int major
, int minor
)
427 if (!devlist_ready
) {
429 nftw("/dev", add_dev
, 10, FTW_PHYS
);
431 ftw("/dev", add_dev
, 10);
436 for (p
=devlist
; p
; p
=p
->next
)
437 if (p
->major
== major
&&
445 unsigned long calc_sb_csum(mdp_super_t
*super
)
447 unsigned int oldcsum
= super
->sb_csum
;
448 unsigned long long newcsum
= 0;
451 unsigned int *superc
= (int*) super
;
454 for(i
=0; i
<MD_SB_BYTES
/4; i
++)
456 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
457 super
->sb_csum
= oldcsum
;
461 char *human_size(long long bytes
)
466 if (bytes
< 5000*1024)
468 else if (bytes
< 2*1024LL*1024LL*1024LL)
469 sprintf(buf
, " (%ld.%02ld MiB %ld.%02ld MB)",
471 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100),
472 (long)(bytes
/1000/1000),
473 (long)(((bytes
%1000000)+5000)/10000)
476 sprintf(buf
, " (%ld.%02ld GiB %ld.%02ld GB)",
478 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100),
479 (long)(bytes
/1000LL/1000LL/1000LL),
480 (long)((((bytes
/1000)%1000000)+5000)/10000)
485 char *human_size_brief(long long bytes
)
490 if (bytes
< 5000*1024)
491 sprintf(buf
, "%ld.%02ldKiB",
492 (long)(bytes
>>10), (long)(((bytes
&1023)*100+512)/1024)
494 else if (bytes
< 2*1024LL*1024LL*1024LL)
495 sprintf(buf
, "%ld.%02ldMiB",
497 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100)
500 sprintf(buf
, "%ld.%02ldGiB",
502 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100)
507 static int mdp_major
= -1;
508 void get_mdp_major(void)
510 FILE *fl
= fopen("/proc/devices", "r");
513 int have_devices
= 0;
517 while ((w
= conf_word(fl
, 1))) {
518 if (have_block
&& strcmp(w
, "devices:")==0)
520 have_block
= (strcmp(w
, "Block")==0);
523 if (have_devices
&& strcmp(w
, "mdp")==0)
524 mdp_major
= last_num
;
532 char *get_md_name(int dev
)
534 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
535 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
536 static char devname
[50];
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
))
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
))
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
))
564 sprintf(devname
, "/dev/.tmp.md%d", dev
);
565 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
568 if (stat(devname
, &stb
) == 0
569 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
570 && (stb
.st_rdev
== rdev
))
576 void put_md_name(char *name
)
578 if (strncmp(name
, "/dev/.tmp.md", 12)==0)