]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - util.c
2 * mdctl - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001 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>
35 * Parse a 128 bit uuid in 4 integers
36 * format is 32 hexx nibbles with options :.<space> separator
37 * If not exactly 32 hex digits are found, return 0
40 int parse_uuid(char *str
, int uuid
[4])
42 int hit
= 0; /* number of Hex digIT */
45 for (i
=0; i
<4; i
++) uuid
[i
]=0;
51 else if (c
>='a' && c
<= 'f')
53 else if (c
>='A' && c
<= 'F')
55 else if (strchr(":. -", c
))
71 * Get the md version number.
72 * We use the RAID_VERSION ioctl if it is supported
73 * If not, but we have a block device with major '9', we assume
76 * Return version number as 24 but number - assume version parts
80 int md_get_version(int fd
)
85 if (fstat(fd
, &stb
)<0)
87 if ((S_IFMT
&stb
.st_mode
) != S_IFBLK
)
90 if (ioctl(fd
, RAID_VERSION
, &vers
) == 0)
91 return (vers
.major
<<16) | (vers
.minor
<<8) | vers
.patchlevel
;
93 if (MAJOR(stb
.st_rdev
) == MD_MAJOR
)
99 int get_linux_version()
106 if (sscanf(name
.release
, "%d.%d.%d", &a
,&b
,&c
)!= 3)
108 return (a
<<16)+(b
<<8)+c
;
111 int enough(int level
, int raid_disks
, int avail_disks
)
116 return avail_disks
== raid_disks
;
118 return avail_disks
>= 1;
121 return avail_disks
>= raid_disks
-1;
127 int same_uuid(int a
[4], int b
[4])
137 void uuid_from_super(int uuid
[4], mdp_super_t
*super
)
139 uuid
[0] = super
->set_uuid0
;
140 if (super
->minor_version
>= 90) {
141 uuid
[1] = super
->set_uuid1
;
142 uuid
[2] = super
->set_uuid2
;
143 uuid
[3] = super
->set_uuid3
;
151 int compare_super(mdp_super_t
*first
, mdp_super_t
*second
)
155 * 0 same, or first was empty, and second was copied
156 * 1 second had wrong number
160 int uuid1
[4], uuid2
[4];
161 if (second
->md_magic
!= MD_SB_MAGIC
)
163 if (first
-> md_magic
!= MD_SB_MAGIC
) {
164 memcpy(first
, second
, sizeof(*first
));
168 uuid_from_super(uuid1
, first
);
169 uuid_from_super(uuid2
, second
);
170 if (!same_uuid(uuid1
, uuid2
))
172 if (first
->major_version
!= second
->major_version
||
173 first
->minor_version
!= second
->minor_version
||
174 first
->patch_version
!= second
->patch_version
||
175 first
->gvalid_words
!= second
->gvalid_words
||
176 first
->ctime
!= second
->ctime
||
177 first
->level
!= second
->level
||
178 first
->size
!= second
->size
||
179 first
->raid_disks
!= second
->raid_disks
)
185 int load_super(int fd
, mdp_super_t
*super
)
187 /* try to read in the superblock
196 * 6 - wrong major version
201 if (ioctl(fd
, BLKGETSIZE
, &size
))
204 if (size
< MD_RESERVED_SECTORS
*2)
207 offset
= MD_NEW_SIZE_SECTORS(size
);
211 if (lseek64(fd
, offset
, 0)< 0LL)
214 if (read(fd
, &super
, sizeof(super
)) != sizeof(super
))
217 if (super
->md_magic
!= MD_SB_MAGIC
)
220 if (super
->major_version
!= 0)