]>
Commit | Line | Data |
---|---|---|
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 | if (hit<32) { | |
60 | uuid[hit/8] <<= 4; | |
61 | uuid[hit/8] += n; | |
62 | } | |
63 | hit++; | |
64 | } | |
65 | if (hit == 32) | |
66 | return 1; | |
67 | return 0; | |
68 | ||
69 | } | |
70 | ||
71 | ||
72 | /* | |
73 | * Get the md version number. | |
74 | * We use the RAID_VERSION ioctl if it is supported | |
75 | * If not, but we have a block device with major '9', we assume | |
76 | * 0.36.0 | |
77 | * | |
78 | * Return version number as 24 but number - assume version parts | |
79 | * always < 255 | |
80 | */ | |
81 | ||
82 | int md_get_version(int fd) | |
83 | { | |
84 | struct stat stb; | |
85 | mdu_version_t vers; | |
86 | ||
87 | if (fstat(fd, &stb)<0) | |
88 | return -1; | |
89 | if ((S_IFMT&stb.st_mode) != S_IFBLK) | |
90 | return -1; | |
91 | ||
92 | if (ioctl(fd, RAID_VERSION, &vers) == 0) | |
93 | return (vers.major*10000) + (vers.minor*100) + vers.patchlevel; | |
94 | ||
95 | if (MAJOR(stb.st_rdev) == MD_MAJOR) | |
96 | return (3600); | |
97 | return -1; | |
98 | } | |
99 | ||
100 | ||
101 | int get_linux_version() | |
102 | { | |
103 | struct utsname name; | |
104 | int a,b,c; | |
105 | if (uname(&name) <0) | |
106 | return -1; | |
107 | ||
108 | if (sscanf(name.release, "%d.%d.%d", &a,&b,&c)!= 3) | |
109 | return -1; | |
110 | return (a*1000000)+(b*1000)+c; | |
111 | } | |
112 | ||
113 | int enough(int level, int raid_disks, int avail_disks) | |
114 | { | |
115 | switch (level) { | |
116 | case -1: | |
117 | case 0: | |
118 | return avail_disks == raid_disks; | |
119 | case 1: | |
120 | return avail_disks >= 1; | |
121 | case 4: | |
122 | case 5: | |
123 | return avail_disks >= raid_disks-1; | |
124 | default: | |
125 | return 0; | |
126 | } | |
127 | } | |
128 | ||
129 | int same_uuid(int a[4], int b[4]) | |
130 | { | |
131 | if (a[0]==b[0] && | |
132 | a[1]==b[1] && | |
133 | a[2]==b[2] && | |
134 | a[3]==b[3]) | |
135 | return 1; | |
136 | return 0; | |
137 | } | |
138 | ||
139 | void uuid_from_super(int uuid[4], mdp_super_t *super) | |
140 | { | |
141 | uuid[0] = super->set_uuid0; | |
142 | if (super->minor_version >= 90) { | |
143 | uuid[1] = super->set_uuid1; | |
144 | uuid[2] = super->set_uuid2; | |
145 | uuid[3] = super->set_uuid3; | |
146 | } else { | |
147 | uuid[1] = 0; | |
148 | uuid[2] = 0; | |
149 | uuid[3] = 0; | |
150 | } | |
151 | } | |
152 | ||
153 | int compare_super(mdp_super_t *first, mdp_super_t *second) | |
154 | { | |
155 | /* | |
156 | * return: | |
157 | * 0 same, or first was empty, and second was copied | |
158 | * 1 second had wrong number | |
159 | * 2 wrong uuid | |
160 | * 3 wrong other info | |
161 | */ | |
162 | int uuid1[4], uuid2[4]; | |
163 | if (second->md_magic != MD_SB_MAGIC) | |
164 | return 1; | |
165 | if (first-> md_magic != MD_SB_MAGIC) { | |
166 | memcpy(first, second, sizeof(*first)); | |
167 | return 0; | |
168 | } | |
169 | ||
170 | uuid_from_super(uuid1, first); | |
171 | uuid_from_super(uuid2, second); | |
172 | if (!same_uuid(uuid1, uuid2)) | |
173 | return 2; | |
174 | if (first->major_version != second->major_version || | |
175 | first->minor_version != second->minor_version || | |
176 | first->patch_version != second->patch_version || | |
177 | first->gvalid_words != second->gvalid_words || | |
178 | first->ctime != second->ctime || | |
179 | first->level != second->level || | |
180 | first->size != second->size || | |
181 | first->raid_disks != second->raid_disks ) | |
182 | return 3; | |
183 | ||
184 | return 0; | |
185 | } | |
186 | ||
187 | int load_super(int fd, mdp_super_t *super) | |
188 | { | |
189 | /* try to read in the superblock | |
190 | * | |
191 | * return | |
192 | * 0 - success | |
193 | * 1 - no block size | |
194 | * 2 - too small | |
195 | * 3 - no seek | |
196 | * 4 - no read | |
197 | * 5 - no magic | |
198 | * 6 - wrong major version | |
199 | */ | |
200 | long size; | |
201 | long long offset; | |
202 | ||
203 | if (ioctl(fd, BLKGETSIZE, &size)) | |
204 | return 1; | |
205 | ||
206 | if (size < MD_RESERVED_SECTORS*2) | |
207 | return 2; | |
208 | ||
209 | offset = MD_NEW_SIZE_SECTORS(size); | |
210 | ||
211 | offset *= 512; | |
212 | ||
213 | if (lseek64(fd, offset, 0)< 0LL) | |
214 | return 3; | |
215 | ||
216 | if (read(fd, super, sizeof(*super)) != sizeof(*super)) | |
217 | return 4; | |
218 | ||
219 | if (super->md_magic != MD_SB_MAGIC) | |
220 | return 5; | |
221 | ||
222 | if (super->major_version != 0) | |
223 | return 6; | |
224 | return 0; | |
225 | } | |
226 | ||
227 | int store_super(int fd, mdp_super_t *super) | |
228 | { | |
229 | long size; | |
230 | long long offset; | |
231 | ||
232 | if (ioctl(fd, BLKGETSIZE, &size)) | |
233 | return 1; | |
234 | ||
235 | if (size < MD_RESERVED_SECTORS*2) | |
236 | return 2; | |
237 | ||
238 | offset = MD_NEW_SIZE_SECTORS(size); | |
239 | ||
240 | offset *= 512; | |
241 | ||
242 | if (lseek64(fd, offset, 0)< 0LL) | |
243 | return 3; | |
244 | ||
245 | if (write(fd, super, sizeof(*super)) != sizeof(*super)) | |
246 | return 4; | |
247 | ||
248 | return 0; | |
249 | } | |
250 | ||
251 | ||
252 | ||
253 | int check_ext2(int fd, char *name) | |
254 | { | |
255 | /* | |
256 | * Check for an ext2fs file system. | |
257 | * Superblock is always 1K at 1K offset | |
258 | * | |
259 | * s_magic is le16 at 56 == 0xEF53 | |
260 | * report mtime - le32 at 44 | |
261 | * blocks - le32 at 4 | |
262 | * logblksize - le32 at 24 | |
263 | */ | |
264 | unsigned char sb[1024]; | |
265 | time_t mtime; | |
266 | int size, bsize; | |
267 | if (lseek(fd, 1024,0)!= 1024) | |
268 | return 0; | |
269 | if (read(fd, sb, 1024)!= 1024) | |
270 | return 0; | |
271 | if (sb[56] != 0x53 || sb[57] != 0xef) | |
272 | return 0; | |
273 | ||
274 | mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8; | |
275 | bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8; | |
276 | size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8; | |
277 | fprintf(stderr, Name ": %s appears to contain an ext2fs file system\n", | |
278 | name); | |
279 | fprintf(stderr," size=%dK mtime=%s", | |
280 | size*(1<<bsize), ctime(&mtime)); | |
281 | return 1; | |
282 | } | |
283 | ||
284 | int check_reiser(int fd, char *name) | |
285 | { | |
286 | /* | |
287 | * superblock is at 64K | |
288 | * size is 1024; | |
289 | * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52 | |
290 | * | |
291 | */ | |
292 | unsigned char sb[1024]; | |
293 | int size; | |
294 | if (lseek(fd, 64*1024, 0) != 64*1024) | |
295 | return 0; | |
296 | if (read(fd, sb, 1024) != 1024) | |
297 | return 0; | |
298 | if (strncmp(sb+52, "ReIsErFs",8)!=0 && | |
299 | strncmp(sb+52, "ReIsEr2Fs",9)!=0) | |
300 | return 0; | |
301 | fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name); | |
302 | size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8; | |
303 | fprintf(stderr, " size = %dK\n", size*4); | |
304 | ||
305 | return 1; | |
306 | } | |
307 | ||
308 | int check_raid(int fd, char *name) | |
309 | { | |
310 | mdp_super_t super; | |
311 | time_t crtime; | |
312 | if (load_super(fd, &super)) | |
313 | return 0; | |
314 | /* Looks like a raid array .. */ | |
315 | fprintf(stderr, Name ": %s appear to be part of a raid array:\n", | |
316 | name); | |
317 | crtime = super.ctime; | |
318 | fprintf(stderr, " level=%d disks=%d ctime=%s", | |
319 | super.level, super.raid_disks, ctime(&crtime)); | |
320 | return 1; | |
321 | } | |
322 | ||
323 | ||
324 | int ask(char *mesg) | |
325 | { | |
326 | char *add = ""; | |
327 | int i; | |
328 | for (i=0; i<5; i++) { | |
329 | char buf[100]; | |
330 | fprintf(stderr, "%s%s", mesg, add); | |
331 | fflush(stderr); | |
332 | if (fgets(buf, 100, stdin)==NULL) | |
333 | return 0; | |
334 | if (buf[0]=='y' || buf[0]=='Y') | |
335 | return 1; | |
336 | if (buf[0]=='n' || buf[0]=='N') | |
337 | return 0; | |
338 | add = "(y/n) "; | |
339 | } | |
340 | fprintf(stderr, Name ": assuming 'no'\n"); | |
341 | return 0; | |
342 | } | |
343 | ||
344 | char *map_num(mapping_t *map, int num) | |
345 | { | |
346 | while (map->name) { | |
347 | if (map->num == num) | |
348 | return map->name; | |
349 | map++; | |
350 | } | |
351 | return NULL; | |
352 | } | |
353 | ||
354 | int map_name(mapping_t *map, char *name) | |
355 | { | |
356 | while (map->name) { | |
357 | if (strcmp(map->name, name)==0) | |
358 | return map->num; | |
359 | map++; | |
360 | } | |
361 | return -10; | |
362 | } | |
363 | ||
364 | /* | |
365 | * convert a major/minor pair for a block device into a name in /dev, if possible. | |
366 | * On the first call, walk /dev collecting name. | |
367 | * Put them in a simple linked listfor now. | |
368 | */ | |
369 | struct devmap { | |
370 | int major, minor; | |
371 | char *name; | |
372 | struct devmap *next; | |
373 | } *devlist = NULL; | |
374 | int devlist_ready = 0; | |
375 | ||
376 | #define __USE_XOPEN_EXTENDED | |
377 | #include <ftw.h> | |
378 | ||
379 | ||
380 | int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s) | |
381 | { | |
382 | if ((stb->st_mode&S_IFMT)== S_IFBLK) { | |
383 | char *n = strdup(name); | |
384 | struct devmap *dm = malloc(sizeof(*dm)); | |
385 | if (dm) { | |
386 | dm->major = MAJOR(stb->st_rdev); | |
387 | dm->minor = MINOR(stb->st_rdev); | |
388 | dm->name = n; | |
389 | dm->next = devlist; | |
390 | devlist = dm; | |
391 | } | |
392 | } | |
393 | return 0; | |
394 | } | |
395 | ||
396 | char *map_dev(int major, int minor) | |
397 | { | |
398 | struct devmap *p; | |
399 | if (!devlist_ready) { | |
400 | nftw("/dev", add_dev, 10, FTW_PHYS); | |
401 | devlist_ready=1; | |
402 | } | |
403 | ||
404 | for (p=devlist; p; p=p->next) | |
405 | if (p->major == major && | |
406 | p->minor == minor) | |
407 | return p->name; | |
408 | return NULL; | |
409 | } | |
410 | ||
411 | ||
412 | int calc_sb_csum(mdp_super_t *super) | |
413 | { | |
414 | unsigned int oldcsum = super->sb_csum; | |
415 | unsigned long long newcsum = 0; /* FIXME why does this make it work?? */ | |
416 | unsigned long csum; | |
417 | int i; | |
418 | unsigned int *superc = (int*) super; | |
419 | super->sb_csum = 0; | |
420 | ||
421 | for(i=0; i<MD_SB_BYTES/4; i++) | |
422 | newcsum+= superc[i]; | |
423 | csum = (newcsum& 0xffffffff) + (newcsum>>32); | |
424 | super->sb_csum = oldcsum; | |
425 | return csum; | |
426 | } |