]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
4 | * Copyright (C) 2001-2002 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 "mdadm.h" | |
31 | #include "md_p.h" | |
32 | #include <sys/utsname.h> | |
33 | #include <ctype.h> | |
34 | ||
35 | /* | |
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 | |
39 | * else return 1 | |
40 | */ | |
41 | int parse_uuid(char *str, int uuid[4]) | |
42 | { | |
43 | int hit = 0; /* number of Hex digIT */ | |
44 | int i; | |
45 | char c; | |
46 | for (i=0; i<4; i++) uuid[i]=0; | |
47 | ||
48 | while ((c= *str++)) { | |
49 | int n; | |
50 | if (c>='0' && c<='9') | |
51 | n = c-'0'; | |
52 | else if (c>='a' && c <= 'f') | |
53 | n = 10 + c - 'a'; | |
54 | else if (c>='A' && c <= 'F') | |
55 | n = 10 + c - 'A'; | |
56 | else if (strchr(":. -", c)) | |
57 | continue; | |
58 | else return 0; | |
59 | ||
60 | if (hit<32) { | |
61 | uuid[hit/8] <<= 4; | |
62 | uuid[hit/8] += n; | |
63 | } | |
64 | hit++; | |
65 | } | |
66 | if (hit == 32) | |
67 | return 1; | |
68 | return 0; | |
69 | ||
70 | } | |
71 | ||
72 | ||
73 | /* | |
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 | |
77 | * 0.36.0 | |
78 | * | |
79 | * Return version number as 24 but number - assume version parts | |
80 | * always < 255 | |
81 | */ | |
82 | ||
83 | int md_get_version(int fd) | |
84 | { | |
85 | struct stat stb; | |
86 | mdu_version_t vers; | |
87 | ||
88 | if (fstat(fd, &stb)<0) | |
89 | return -1; | |
90 | if ((S_IFMT&stb.st_mode) != S_IFBLK) | |
91 | return -1; | |
92 | ||
93 | if (ioctl(fd, RAID_VERSION, &vers) == 0) | |
94 | return (vers.major*10000) + (vers.minor*100) + vers.patchlevel; | |
95 | if (errno == EACCES) | |
96 | return -1; | |
97 | if (major(stb.st_rdev) == MD_MAJOR) | |
98 | return (3600); | |
99 | return -1; | |
100 | } | |
101 | ||
102 | ||
103 | int get_linux_version() | |
104 | { | |
105 | struct utsname name; | |
106 | char *cp; | |
107 | int a,b,c; | |
108 | if (uname(&name) <0) | |
109 | return -1; | |
110 | ||
111 | cp = name.release; | |
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); | |
117 | ||
118 | return (a*1000000)+(b*1000)+c; | |
119 | } | |
120 | ||
121 | int enough(int level, int raid_disks, int avail_disks) | |
122 | { | |
123 | switch (level) { | |
124 | case 10: return 1; /* a lie, but it is hard to tell */ | |
125 | ||
126 | case -4: | |
127 | return avail_disks>= 1; | |
128 | case -1: | |
129 | case 0: | |
130 | return avail_disks == raid_disks; | |
131 | case 1: | |
132 | return avail_disks >= 1; | |
133 | case 4: | |
134 | case 5: | |
135 | return avail_disks >= raid_disks-1; | |
136 | case 6: | |
137 | return avail_disks >= raid_disks-2; | |
138 | default: | |
139 | return 0; | |
140 | } | |
141 | } | |
142 | ||
143 | int same_uuid(int a[4], int b[4]) | |
144 | { | |
145 | if (a[0]==b[0] && | |
146 | a[1]==b[1] && | |
147 | a[2]==b[2] && | |
148 | a[3]==b[3]) | |
149 | return 1; | |
150 | return 0; | |
151 | } | |
152 | ||
153 | int check_ext2(int fd, char *name) | |
154 | { | |
155 | /* | |
156 | * Check for an ext2fs file system. | |
157 | * Superblock is always 1K at 1K offset | |
158 | * | |
159 | * s_magic is le16 at 56 == 0xEF53 | |
160 | * report mtime - le32 at 44 | |
161 | * blocks - le32 at 4 | |
162 | * logblksize - le32 at 24 | |
163 | */ | |
164 | unsigned char sb[1024]; | |
165 | time_t mtime; | |
166 | int size, bsize; | |
167 | if (lseek(fd, 1024,0)!= 1024) | |
168 | return 0; | |
169 | if (read(fd, sb, 1024)!= 1024) | |
170 | return 0; | |
171 | if (sb[56] != 0x53 || sb[57] != 0xef) | |
172 | return 0; | |
173 | ||
174 | mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8; | |
175 | bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8; | |
176 | size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8; | |
177 | fprintf(stderr, Name ": %s appears to contain an ext2fs file system\n", | |
178 | name); | |
179 | fprintf(stderr," size=%dK mtime=%s", | |
180 | size*(1<<bsize), ctime(&mtime)); | |
181 | return 1; | |
182 | } | |
183 | ||
184 | int check_reiser(int fd, char *name) | |
185 | { | |
186 | /* | |
187 | * superblock is at 64K | |
188 | * size is 1024; | |
189 | * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52 | |
190 | * | |
191 | */ | |
192 | unsigned char sb[1024]; | |
193 | int size; | |
194 | if (lseek(fd, 64*1024, 0) != 64*1024) | |
195 | return 0; | |
196 | if (read(fd, sb, 1024) != 1024) | |
197 | return 0; | |
198 | if (strncmp(sb+52, "ReIsErFs",8)!=0 && | |
199 | strncmp(sb+52, "ReIsEr2Fs",9)!=0) | |
200 | return 0; | |
201 | fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name); | |
202 | size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8; | |
203 | fprintf(stderr, " size = %dK\n", size*4); | |
204 | ||
205 | return 1; | |
206 | } | |
207 | ||
208 | int check_raid(int fd, char *name) | |
209 | { | |
210 | void *super; | |
211 | struct mdinfo info; | |
212 | time_t crtime; | |
213 | ||
214 | int i; | |
215 | for (i=0; superlist[i]; i++) { | |
216 | if (superlist[i]->load_super(fd, &super, name)) | |
217 | continue; | |
218 | /* Looks like a raid array .. */ | |
219 | fprintf(stderr, Name ": %s appears to be part of a raid array:\n", | |
220 | name); | |
221 | superlist[i]->getinfo_super(&info, super); | |
222 | free(super); | |
223 | crtime = info.array.ctime; | |
224 | fprintf(stderr, " level=%d devices=%d ctime=%s", | |
225 | info.array.level, info.array.raid_disks, ctime(&crtime)); | |
226 | return 1; | |
227 | } | |
228 | return 0; | |
229 | } | |
230 | ||
231 | ||
232 | int ask(char *mesg) | |
233 | { | |
234 | char *add = ""; | |
235 | int i; | |
236 | for (i=0; i<5; i++) { | |
237 | char buf[100]; | |
238 | fprintf(stderr, "%s%s", mesg, add); | |
239 | fflush(stderr); | |
240 | if (fgets(buf, 100, stdin)==NULL) | |
241 | return 0; | |
242 | if (buf[0]=='y' || buf[0]=='Y') | |
243 | return 1; | |
244 | if (buf[0]=='n' || buf[0]=='N') | |
245 | return 0; | |
246 | add = "(y/n) "; | |
247 | } | |
248 | fprintf(stderr, Name ": assuming 'no'\n"); | |
249 | return 0; | |
250 | } | |
251 | ||
252 | char *map_num(mapping_t *map, int num) | |
253 | { | |
254 | while (map->name) { | |
255 | if (map->num == num) | |
256 | return map->name; | |
257 | map++; | |
258 | } | |
259 | return NULL; | |
260 | } | |
261 | ||
262 | int map_name(mapping_t *map, char *name) | |
263 | { | |
264 | while (map->name) { | |
265 | if (strcmp(map->name, name)==0) | |
266 | return map->num; | |
267 | map++; | |
268 | } | |
269 | return UnSet; | |
270 | } | |
271 | ||
272 | ||
273 | int is_standard(char *dev, int *nump) | |
274 | { | |
275 | /* tests if dev is a "standard" md dev name. | |
276 | * i.e if the last component is "/dNN" or "/mdNN", | |
277 | * where NN is a string of digits | |
278 | */ | |
279 | char *d = strrchr(dev, '/'); | |
280 | int type=0; | |
281 | int num; | |
282 | if (!d) | |
283 | return 0; | |
284 | if (strncmp(d, "/d",2)==0) | |
285 | d += 2, type=1; /* /dev/md/dN{pM} */ | |
286 | else if (strncmp(d, "/md_d", 5)==0) | |
287 | d += 5, type=1; /* /dev/md_dNpM */ | |
288 | else if (strncmp(d, "/md", 3)==0) | |
289 | d += 3, type=-1; /* /dev/mdN */ | |
290 | else if (d-dev > 3 && strncmp(d-2, "md/", 3)==0) | |
291 | type = -1; /* /dev/md/N */ | |
292 | else | |
293 | return 0; | |
294 | if (!*d) | |
295 | return 0; | |
296 | num = atoi(d); | |
297 | while (isdigit(*d)) | |
298 | d++; | |
299 | if (*d) | |
300 | return 0; | |
301 | if (nump) *nump = num; | |
302 | ||
303 | return type; | |
304 | } | |
305 | ||
306 | ||
307 | /* | |
308 | * convert a major/minor pair for a block device into a name in /dev, if possible. | |
309 | * On the first call, walk /dev collecting name. | |
310 | * Put them in a simple linked listfor now. | |
311 | */ | |
312 | struct devmap { | |
313 | int major, minor; | |
314 | char *name; | |
315 | struct devmap *next; | |
316 | } *devlist = NULL; | |
317 | int devlist_ready = 0; | |
318 | ||
319 | #ifdef UCLIBC | |
320 | char *map_dev(int major, int minor) | |
321 | { | |
322 | #if 0 | |
323 | fprintf(stderr, "Warning - fail to map %d,%d to a device name\n", | |
324 | major, minor); | |
325 | #endif | |
326 | return NULL; | |
327 | } | |
328 | #else | |
329 | #define __USE_XOPEN_EXTENDED | |
330 | #include <ftw.h> | |
331 | ||
332 | ||
333 | #ifndef __dietlibc__ | |
334 | int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s) | |
335 | #else | |
336 | int add_dev(const char *name, const struct stat *stb, int flag) | |
337 | #endif | |
338 | { | |
339 | if ((stb->st_mode&S_IFMT)== S_IFBLK) { | |
340 | char *n = strdup(name); | |
341 | struct devmap *dm = malloc(sizeof(*dm)); | |
342 | if (dm) { | |
343 | dm->major = major(stb->st_rdev); | |
344 | dm->minor = minor(stb->st_rdev); | |
345 | dm->name = n; | |
346 | dm->next = devlist; | |
347 | devlist = dm; | |
348 | } | |
349 | } | |
350 | return 0; | |
351 | } | |
352 | ||
353 | /* | |
354 | * Find a block device with the right major/minor number. | |
355 | * Avoid /dev/mdNN and /dev/md/dNN if possible | |
356 | */ | |
357 | char *map_dev(int major, int minor) | |
358 | { | |
359 | struct devmap *p; | |
360 | char *std = NULL; | |
361 | if (!devlist_ready) { | |
362 | #ifndef __dietlibc__ | |
363 | nftw("/dev", add_dev, 10, FTW_PHYS); | |
364 | #else | |
365 | ftw("/dev", add_dev, 10); | |
366 | #endif | |
367 | devlist_ready=1; | |
368 | } | |
369 | ||
370 | for (p=devlist; p; p=p->next) | |
371 | if (p->major == major && | |
372 | p->minor == minor) { | |
373 | if (is_standard(p->name, NULL)) | |
374 | std = p->name; | |
375 | else | |
376 | return p->name; | |
377 | } | |
378 | return std; | |
379 | } | |
380 | ||
381 | #endif | |
382 | ||
383 | unsigned long calc_csum(void *super, int bytes) | |
384 | { | |
385 | unsigned long long newcsum = 0; | |
386 | int i; | |
387 | unsigned int csum; | |
388 | unsigned int *superc = (unsigned int*) super; | |
389 | ||
390 | for(i=0; i<bytes/4; i++) | |
391 | newcsum+= superc[i]; | |
392 | csum = (newcsum& 0xffffffff) + (newcsum>>32); | |
393 | return csum; | |
394 | } | |
395 | ||
396 | char *human_size(long long bytes) | |
397 | { | |
398 | static char buf[30]; | |
399 | ||
400 | ||
401 | if (bytes < 5000*1024) | |
402 | buf[0]=0; | |
403 | else if (bytes < 2*1024LL*1024LL*1024LL) | |
404 | sprintf(buf, " (%ld.%02ld MiB %ld.%02ld MB)", | |
405 | (long)(bytes>>20), | |
406 | (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100), | |
407 | (long)(bytes/1000/1000), | |
408 | (long)(((bytes%1000000)+5000)/10000) | |
409 | ); | |
410 | else | |
411 | sprintf(buf, " (%ld.%02ld GiB %ld.%02ld GB)", | |
412 | (long)(bytes>>30), | |
413 | (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100), | |
414 | (long)(bytes/1000LL/1000LL/1000LL), | |
415 | (long)((((bytes/1000)%1000000)+5000)/10000) | |
416 | ); | |
417 | return buf; | |
418 | } | |
419 | ||
420 | char *human_size_brief(long long bytes) | |
421 | { | |
422 | static char buf[30]; | |
423 | ||
424 | ||
425 | if (bytes < 5000*1024) | |
426 | sprintf(buf, "%ld.%02ldKiB", | |
427 | (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024) | |
428 | ); | |
429 | else if (bytes < 2*1024LL*1024LL*1024LL) | |
430 | sprintf(buf, "%ld.%02ldMiB", | |
431 | (long)(bytes>>20), | |
432 | (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100) | |
433 | ); | |
434 | else | |
435 | sprintf(buf, "%ld.%02ldGiB", | |
436 | (long)(bytes>>30), | |
437 | (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100) | |
438 | ); | |
439 | return buf; | |
440 | } | |
441 | ||
442 | int get_mdp_major(void) | |
443 | { | |
444 | static int mdp_major = -1; | |
445 | FILE *fl; | |
446 | char *w; | |
447 | int have_block = 0; | |
448 | int have_devices = 0; | |
449 | int last_num = -1; | |
450 | ||
451 | if (mdp_major != -1) | |
452 | return mdp_major; | |
453 | fl = fopen("/proc/devices", "r"); | |
454 | if (!fl) | |
455 | return -1; | |
456 | while ((w = conf_word(fl, 1))) { | |
457 | if (have_block && strcmp(w, "devices:")==0) | |
458 | have_devices = 1; | |
459 | have_block = (strcmp(w, "Block")==0); | |
460 | if (isdigit(w[0])) | |
461 | last_num = atoi(w); | |
462 | if (have_devices && strcmp(w, "mdp")==0) | |
463 | mdp_major = last_num; | |
464 | free(w); | |
465 | } | |
466 | fclose(fl); | |
467 | return mdp_major; | |
468 | } | |
469 | ||
470 | ||
471 | ||
472 | char *get_md_name(int dev) | |
473 | { | |
474 | /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */ | |
475 | /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */ | |
476 | static char devname[50]; | |
477 | struct stat stb; | |
478 | dev_t rdev; | |
479 | char *dn; | |
480 | ||
481 | if (dev < 0) { | |
482 | int mdp = get_mdp_major(); | |
483 | if (mdp < 0) return NULL; | |
484 | rdev = makedev(mdp, (-1-dev)<<6); | |
485 | sprintf(devname, "/dev/md/d%d", -1-dev); | |
486 | if (stat(devname, &stb) == 0 | |
487 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
488 | && (stb.st_rdev == rdev)) | |
489 | return devname; | |
490 | } else { | |
491 | rdev = makedev(MD_MAJOR, dev); | |
492 | sprintf(devname, "/dev/md%d", dev); | |
493 | if (stat(devname, &stb) == 0 | |
494 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
495 | && (stb.st_rdev == rdev)) | |
496 | return devname; | |
497 | ||
498 | sprintf(devname, "/dev/md/%d", dev); | |
499 | if (stat(devname, &stb) == 0 | |
500 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
501 | && (stb.st_rdev == rdev)) | |
502 | return devname; | |
503 | } | |
504 | dn = map_dev(major(rdev), minor(rdev)); | |
505 | if (dn) | |
506 | return dn; | |
507 | sprintf(devname, "/dev/.tmp.md%d", dev); | |
508 | if (mknod(devname, S_IFBLK | 0600, rdev) == -1) | |
509 | if (errno != EEXIST) | |
510 | return NULL; | |
511 | ||
512 | if (stat(devname, &stb) == 0 | |
513 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
514 | && (stb.st_rdev == rdev)) | |
515 | return devname; | |
516 | unlink(devname); | |
517 | return NULL; | |
518 | } | |
519 | ||
520 | void put_md_name(char *name) | |
521 | { | |
522 | if (strncmp(name, "/dev/.tmp.md", 12)==0) | |
523 | unlink(name); | |
524 | } | |
525 | ||
526 | ||
527 | ||
528 | ||
529 | struct superswitch *superlist[] = { &super0, NULL }; | |
530 | ||
531 | struct superswitch *super_by_version(int vers) | |
532 | { | |
533 | if (vers == 0) return &super0; | |
534 | return NULL; | |
535 | } | |
536 | ||
537 | struct superswitch *guess_super(int fd, char *dev) | |
538 | { | |
539 | /* try each load_super to find the best match, | |
540 | * and return the best superswitch | |
541 | */ | |
542 | struct superswitch *best = NULL, *ss; | |
543 | int bestrv = 0; | |
544 | void *sbp = NULL; | |
545 | int i; | |
546 | ||
547 | for (i=0 ; superlist[i]; i++) { | |
548 | int rv; | |
549 | ss = superlist[i]; | |
550 | rv = ss->load_super(fd, &sbp, NULL); | |
551 | if (rv == 0) { | |
552 | free(sbp); | |
553 | return ss; | |
554 | } | |
555 | if (rv > bestrv) { | |
556 | bestrv = rv; | |
557 | best = ss; | |
558 | } | |
559 | } | |
560 | if (bestrv > 2) /* FIXME */ | |
561 | return best; | |
562 | return NULL; | |
563 | } |