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
void uuid_from_super(int uuid[4], mdp_super_t *super)
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;
167
int compare_super(mdp_super_t *first, mdp_super_t *second)
171
* 0 same, or first was empty, and second was copied
172
* 1 second had wrong number
176
int uuid1[4], uuid2[4];
177
if (second->md_magic != MD_SB_MAGIC)
179
if (first-> md_magic != MD_SB_MAGIC) {
180
memcpy(first, second, sizeof(*first));
184
uuid_from_super(uuid1, first);
185
uuid_from_super(uuid2, second);
186
if (!same_uuid(uuid1, uuid2))
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 )
201
int load_super(int fd, mdp_super_t *super)
203
/* try to read in the superblock
212
* 6 - wrong major version
215
unsigned long long dsize;
216
unsigned long long offset;
219
if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
222
if (ioctl(fd, BLKGETSIZE, &size))
230
if (dsize < MD_RESERVED_SECTORS*2)
233
offset = MD_NEW_SIZE_SECTORS(dsize>>9);
237
ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
239
if (lseek64(fd, offset, 0)< 0LL)
242
if (read(fd, super, sizeof(*super)) != sizeof(*super))
245
if (super->md_magic != MD_SB_MAGIC)
248
if (super->major_version != 0)
253
int store_super(int fd, mdp_super_t *super)
256
unsigned long long dsize;
261
if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
264
if (ioctl(fd, BLKGETSIZE, &size))
267
dsize = ((unsigned long long)size) << 9;
270
if (dsize < MD_RESERVED_SECTORS*2)
273
offset = MD_NEW_SIZE_SECTORS(dsize>>9);
277
if (lseek64(fd, offset, 0)< 0LL)
280
if (write(fd, super, sizeof(*super)) != sizeof(*super))
289
int check_ext2(int fd, char *name)
292
* Check for an ext2fs file system.
293
* Superblock is always 1K at 1K offset
295
* s_magic is le16 at 56 == 0xEF53
296
* report mtime - le32 at 44
298
* logblksize - le32 at 24
300
unsigned char sb[1024];
303
if (lseek(fd, 1024,0)!= 1024)
305
if (read(fd, sb, 1024)!= 1024)
307
if (sb[56] != 0x53 || sb[57] != 0xef)
310
mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8;
311
bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8;
312
size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8;
313
fprintf(stderr, Name ": %s appears to contain an ext2fs file system\n",
315
fprintf(stderr," size=%dK mtime=%s",
316
size*(1<<bsize), ctime(&mtime));
320
int check_reiser(int fd, char *name)
323
* superblock is at 64K
325
* Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
328
unsigned char sb[1024];
330
if (lseek(fd, 64*1024, 0) != 64*1024)
332
if (read(fd, sb, 1024) != 1024)
334
if (strncmp((char*)sb+52, "ReIsErFs",8)!=0 &&
335
strncmp((char*)sb+52, "ReIsEr2Fs",9)!=0)
337
fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name);
338
size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8;
339
fprintf(stderr, " size = %dK\n", size*4);
344
int check_raid(int fd, char *name)
348
if (load_super(fd, &super))
350
/* Looks like a raid array .. */
351
fprintf(stderr, Name ": %s appears to be part of a raid array:\n",
353
crtime = super.ctime;
354
fprintf(stderr, " level=%d devices=%d ctime=%s",
355
super.level, super.raid_disks, ctime(&crtime));
364
for (i=0; i<5; i++) {
366
fprintf(stderr, "%s%s", mesg, add);
368
if (fgets(buf, 100, stdin)==NULL)
370
if (buf[0]=='y' || buf[0]=='Y')
372
if (buf[0]=='n' || buf[0]=='N')
376
fprintf(stderr, Name ": assuming 'no'\n");
380
char *map_num(mapping_t *map, int num)
390
int map_name(mapping_t *map, char *name)
393
if (strcmp(map->name, name)==0)
401
int is_standard(char *dev, int *nump)
403
/* tests if dev is a "standard" md dev name.
404
* i.e if the last component is "/dNN" or "/mdNN",
405
* where NN is a string of digits
407
char *d = strrchr(dev, '/');
412
if (strncmp(d, "/d",2)==0)
413
d += 2, type=1; /* /dev/md/dN{pM} */
414
else if (strncmp(d, "/md_d", 5)==0)
415
d += 5, type=1; /* /dev/md_dNpM */
416
else if (strncmp(d, "/md", 3)==0)
417
d += 3, type=-1; /* /dev/mdN */
418
else if (d-dev > 3 && strncmp(d-2, "md/", 3)==0)
419
d += 1, type=-1; /* /dev/md/N */
429
if (nump) *nump = num;
436
* convert a major/minor pair for a block device into a name in /dev, if possible.
437
* On the first call, walk /dev collecting name.
438
* Put them in a simple linked listfor now.
445
int devlist_ready = 0;
448
int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
451
char *map_dev(int major, int minor)
454
fprintf(stderr, "Warning - fail to map %d,%d to a device name\n",
462
int add_dev_1(const char *name, const struct stat *stb, int flag)
464
return add_dev(name, stb, flag, NULL);
466
int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
468
ftw(path, add_dev_1, nopenfd);
472
int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
474
if ((stb->st_mode&S_IFMT)== S_IFBLK) {
475
char *n = strdup(name);
476
struct devmap *dm = malloc(sizeof(*dm));
478
dm->major = major(stb->st_rdev);
479
dm->minor = minor(stb->st_rdev);
489
* Find a block device with the right major/minor number.
490
* If we find multiple names, choose the shortest.
491
* If we find a non-standard name, it is probably there
492
* deliberately so prefer it over a standard name.
493
* This applies only to names for MD devices.
495
char *map_dev(int major, int minor)
498
char *std = NULL, *nonstd=NULL;
499
if (!devlist_ready) {
500
nftw("/dev", add_dev, 10, FTW_PHYS);
504
for (p=devlist; p; p=p->next)
505
if (p->major == major &&
507
if (is_standard(p->name, NULL)) {
509
strlen(p->name) < strlen(std))
512
if (nonstd == NULL ||
513
strlen(p->name) < strlen(nonstd))
517
return nonstd ? nonstd : std;
522
unsigned long calc_sb_csum(mdp_super_t *super)
524
unsigned int oldcsum = super->sb_csum;
525
unsigned long long newcsum = 0;
528
unsigned int *superc = (unsigned int*) super;
531
for(i=0; i<MD_SB_BYTES/4; i++)
533
csum = (newcsum& 0xffffffff) + (newcsum>>32);
534
super->sb_csum = oldcsum;
538
char *human_size(long long bytes)
542
/* We convert bytes to either centi-M{ega,ibi}bytes or
543
* centi-G{igi,ibi}bytes, with appropriate rounding,
544
* and then print 1/100th of those as a decimal.
545
* We allow upto 2048Megabytes before converting to
546
* gigabytes, as that shows more precision and isn't
547
* too large a number.
548
* Terrabytes are not yet handled.
551
if (bytes < 1024*1024)
553
else if (bytes < 2*1024LL*1024LL*1024LL) {
554
long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2;
555
long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2;
556
snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)",
557
cMiB/100 , cMiB % 100,
560
long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2;
561
long cGB = (bytes / (1000000000LL/200LL ) +1) /2;
562
snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)",
563
cGiB/100 , cGiB % 100,
569
char *human_size_brief(long long bytes)
574
if (bytes < 5000*1024)
575
snprintf(buf, sizeof(buf), "%ld.%02ldKiB",
576
(long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
578
else if (bytes < 2*1024LL*1024LL*1024LL)
579
snprintf(buf, sizeof(buf), "%ld.%02ldMiB",
581
(long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
584
snprintf(buf, sizeof(buf), "%ld.%02ldGiB",
586
(long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
591
int get_mdp_major(void)
593
static int mdp_major = -1;
597
int have_devices = 0;
602
fl = fopen("/proc/devices", "r");
605
while ((w = conf_word(fl, 1))) {
606
if (have_block && strcmp(w, "devices:")==0)
608
have_block = (strcmp(w, "Block")==0);
611
if (have_devices && strcmp(w, "mdp")==0)
612
mdp_major = last_num;
621
char *get_md_name(int dev)
623
/* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
624
/* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
625
static char devname[50];
631
int mdp = get_mdp_major();
632
if (mdp < 0) return NULL;
633
rdev = makedev(mdp, (-1-dev)<<6);
634
snprintf(devname, sizeof(devname), "/dev/md/d%d", -1-dev);
635
if (stat(devname, &stb) == 0
636
&& (S_IFMT&stb.st_mode) == S_IFBLK
637
&& (stb.st_rdev == rdev))
640
rdev = makedev(MD_MAJOR, dev);
641
snprintf(devname, sizeof(devname), "/dev/md%d", dev);
642
if (stat(devname, &stb) == 0
643
&& (S_IFMT&stb.st_mode) == S_IFBLK
644
&& (stb.st_rdev == rdev))
647
snprintf(devname, sizeof(devname), "/dev/md/%d", dev);
648
if (stat(devname, &stb) == 0
649
&& (S_IFMT&stb.st_mode) == S_IFBLK
650
&& (stb.st_rdev == rdev))
653
dn = map_dev(major(rdev), minor(rdev));
656
snprintf(devname, sizeof(devname), "/dev/.tmp.md%d", dev);
657
if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
661
if (stat(devname, &stb) == 0
662
&& (S_IFMT&stb.st_mode) == S_IFBLK
663
&& (stb.st_rdev == rdev))
669
void put_md_name(char *name)
671
if (strncmp(name, "/dev/.tmp.md", 12)==0)
676
/* tinyc doesn't optimize this check in ioctl.h out ... */
677
unsigned int __invalid_size_argument_for_IOC = 0;