]>
Commit | Line | Data |
---|---|---|
64c4757e NB |
1 | /* |
2 | * mdctl - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
4 | * Copyright (C) 2001 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 "mdctl.h" | |
31 | #include "md_p.h" | |
32 | #include <sys/utsname.h> | |
33 | ||
34 | /* | |
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 | |
38 | * else return 1 | |
39 | */ | |
40 | int parse_uuid(char *str, int uuid[4]) | |
41 | { | |
42 | int hit = 0; /* number of Hex digIT */ | |
43 | int i; | |
44 | char c; | |
45 | for (i=0; i<4; i++) uuid[i]=0; | |
46 | ||
47 | while ((c= *str++)) { | |
48 | int n; | |
49 | if (c>='0' && c<='9') | |
50 | n = c-'0'; | |
51 | else if (c>='a' && c <= 'f') | |
52 | n = 10 + c - 'a'; | |
53 | else if (c>='A' && c <= 'F') | |
54 | n = 10 + c - 'A'; | |
55 | else if (strchr(":. -", c)) | |
56 | continue; | |
57 | else return 0; | |
58 | ||
59 | uuid[hit/4] <<= 4; | |
60 | uuid[hit/4] += n; | |
61 | hit++; | |
62 | } | |
63 | if (hit == 32) | |
64 | return 1; | |
65 | return 0; | |
66 | ||
67 | } | |
68 | ||
69 | ||
70 | /* | |
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 | |
74 | * 0.36.0 | |
75 | * | |
76 | * Return version number as 24 but number - assume version parts | |
77 | * always < 255 | |
78 | */ | |
79 | ||
80 | int md_get_version(int fd) | |
81 | { | |
82 | struct stat stb; | |
83 | mdu_version_t vers; | |
84 | ||
85 | if (fstat(fd, &stb)<0) | |
86 | return -1; | |
87 | if ((S_IFMT&stb.st_mode) != S_IFBLK) | |
88 | return -1; | |
89 | ||
90 | if (ioctl(fd, RAID_VERSION, &vers) == 0) | |
682c7051 | 91 | return (vers.major*10000) + (vers.minor*100) + vers.patchlevel; |
64c4757e NB |
92 | |
93 | if (MAJOR(stb.st_rdev) == MD_MAJOR) | |
682c7051 | 94 | return (3600); |
64c4757e NB |
95 | return -1; |
96 | } | |
97 | ||
98 | ||
99 | int get_linux_version() | |
100 | { | |
101 | struct utsname name; | |
102 | int a,b,c; | |
103 | if (uname(&name) <0) | |
104 | return -1; | |
105 | ||
106 | if (sscanf(name.release, "%d.%d.%d", &a,&b,&c)!= 3) | |
107 | return -1; | |
682c7051 | 108 | return (a*1000000)+(b*1000)+c; |
64c4757e NB |
109 | } |
110 | ||
111 | int enough(int level, int raid_disks, int avail_disks) | |
112 | { | |
113 | switch (level) { | |
114 | case -1: | |
115 | case 0: | |
116 | return avail_disks == raid_disks; | |
117 | case 1: | |
118 | return avail_disks >= 1; | |
119 | case 4: | |
120 | case 5: | |
121 | return avail_disks >= raid_disks-1; | |
122 | default: | |
123 | return 0; | |
124 | } | |
125 | } | |
126 | ||
127 | int same_uuid(int a[4], int b[4]) | |
128 | { | |
129 | if (a[0]==b[0] && | |
130 | a[1]==b[1] && | |
131 | a[2]==b[2] && | |
132 | a[3]==b[3]) | |
133 | return 1; | |
134 | return 0; | |
135 | } | |
136 | ||
137 | void uuid_from_super(int uuid[4], mdp_super_t *super) | |
138 | { | |
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; | |
144 | } else { | |
145 | uuid[1] = 0; | |
146 | uuid[2] = 0; | |
147 | uuid[3] = 0; | |
148 | } | |
149 | } | |
150 | ||
151 | int compare_super(mdp_super_t *first, mdp_super_t *second) | |
152 | { | |
153 | /* | |
154 | * return: | |
155 | * 0 same, or first was empty, and second was copied | |
156 | * 1 second had wrong number | |
157 | * 2 wrong uuid | |
158 | * 3 wrong other info | |
159 | */ | |
160 | int uuid1[4], uuid2[4]; | |
161 | if (second->md_magic != MD_SB_MAGIC) | |
162 | return 1; | |
163 | if (first-> md_magic != MD_SB_MAGIC) { | |
164 | memcpy(first, second, sizeof(*first)); | |
165 | return 0; | |
166 | } | |
167 | ||
168 | uuid_from_super(uuid1, first); | |
169 | uuid_from_super(uuid2, second); | |
170 | if (!same_uuid(uuid1, uuid2)) | |
171 | return 2; | |
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 ) | |
180 | return 3; | |
181 | ||
182 | return 0; | |
183 | } | |
184 | ||
185 | int load_super(int fd, mdp_super_t *super) | |
186 | { | |
187 | /* try to read in the superblock | |
188 | * | |
189 | * return | |
190 | * 0 - success | |
191 | * 1 - no block size | |
192 | * 2 - too small | |
193 | * 3 - no seek | |
194 | * 4 - no read | |
195 | * 5 - no magic | |
196 | * 6 - wrong major version | |
197 | */ | |
198 | long size; | |
199 | long long offset; | |
200 | ||
201 | if (ioctl(fd, BLKGETSIZE, &size)) | |
202 | return 1; | |
203 | ||
204 | if (size < MD_RESERVED_SECTORS*2) | |
205 | return 2; | |
206 | ||
207 | offset = MD_NEW_SIZE_SECTORS(size); | |
208 | ||
209 | offset *= 512; | |
210 | ||
211 | if (lseek64(fd, offset, 0)< 0LL) | |
212 | return 3; | |
213 | ||
682c7051 | 214 | if (read(fd, super, sizeof(*super)) != sizeof(*super)) |
64c4757e NB |
215 | return 4; |
216 | ||
217 | if (super->md_magic != MD_SB_MAGIC) | |
218 | return 5; | |
219 | ||
220 | if (super->major_version != 0) | |
221 | return 6; | |
222 | return 0; | |
223 | } | |
224 | ||
682c7051 NB |
225 | |
226 | int check_ext2(int fd, char *name) | |
227 | { | |
228 | /* | |
229 | * Check for an ext2fs file system. | |
230 | * Superblock is always 1K at 1K offset | |
231 | * | |
232 | * s_magic is le16 at 56 == 0xEF53 | |
233 | * report mtime - le32 at 44 | |
234 | * blocks - le32 at 4 | |
235 | * logblksize - le32 at 24 | |
236 | */ | |
237 | unsigned char sb[1024]; | |
238 | time_t mtime; | |
239 | int size, bsize; | |
240 | if (lseek(fd, 1024,0)!= 1024) | |
241 | return 0; | |
242 | if (read(fd, sb, 1024)!= 1024) | |
243 | return 0; | |
244 | if (sb[56] != 0x53 || sb[57] != 0xef) | |
245 | return 0; | |
246 | ||
247 | mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8; | |
248 | bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8; | |
249 | size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8; | |
250 | fprintf(stderr, Name ": %s appears to contain an ext2fs file system\n", | |
251 | name); | |
252 | fprintf(stderr," size=%dK mtime=%s", | |
253 | size*(1<<bsize), ctime(&mtime)); | |
254 | return 1; | |
255 | } | |
256 | ||
257 | int check_reiser(int fd, char *name) | |
258 | { | |
259 | /* | |
260 | * superblock is at 64K | |
261 | * size is 1024; | |
262 | * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52 | |
263 | * | |
264 | */ | |
265 | unsigned char sb[1024]; | |
266 | int size; | |
267 | if (lseek(fd, 64*1024, 0) != 64*1024) | |
268 | return 0; | |
269 | if (read(fd, sb, 1024) != 1024) | |
270 | return 0; | |
271 | if (strncmp(sb+52, "ReIsErFs",8)!=0 && | |
272 | strncmp(sb+52, "ReIsEr2Fs",9)!=0) | |
273 | return 0; | |
274 | fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name); | |
275 | size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8; | |
276 | fprintf(stderr, " size = %dK\n", size*4); | |
277 | ||
278 | return 1; | |
279 | } | |
280 | ||
281 | int check_raid(int fd, char *name) | |
282 | { | |
283 | mdp_super_t super; | |
284 | time_t crtime; | |
285 | if (load_super(fd, &super)) | |
286 | return 0; | |
287 | /* Looks like a raid array .. */ | |
288 | fprintf(stderr, Name ": %s appear to be part of a raid array:\n", | |
289 | name); | |
290 | crtime = super.ctime; | |
291 | fprintf(stderr, " level=%d disks=%d ctime=%s", | |
292 | super.level, super.raid_disks, ctime(&crtime)); | |
293 | return 1; | |
294 | } | |
295 | ||
296 | ||
297 | int ask(char *mesg) | |
298 | { | |
299 | char *add = ""; | |
300 | int i; | |
301 | for (i=0; i<5; i++) { | |
302 | char buf[100]; | |
303 | fprintf(stderr, "%s%s", mesg, add); | |
304 | fflush(stderr); | |
305 | if (fgets(buf, 100, stdin)==NULL) | |
306 | return 0; | |
307 | if (buf[0]=='y' || buf[0]=='Y') | |
308 | return 1; | |
309 | if (buf[0]=='n' || buf[0]=='N') | |
310 | return 0; | |
311 | add = "(y/n) "; | |
312 | } | |
313 | fprintf(stderr, Name ": assuming 'no'\n"); | |
314 | return 0; | |
315 | } | |
316 | ||
317 | char *map_num(mapping_t *map, int num) | |
318 | { | |
319 | while (map->name) { | |
320 | if (map->num == num) | |
321 | return map->name; | |
322 | map++; | |
323 | } | |
324 | return NULL; | |
325 | } | |
326 | ||
327 | int map_name(mapping_t *map, char *name) | |
328 | { | |
329 | while (map->name) { | |
330 | if (strcmp(map->name, name)==0) | |
331 | return map->num; | |
332 | map++; | |
333 | } | |
334 | return -10; | |
335 | } |