]>
Commit | Line | Data |
---|---|---|
64c4757e | 1 | /* |
9a9dab36 | 2 | * mdadm - manage Linux "md" devices aka RAID arrays. |
64c4757e | 3 | * |
cd29a5c8 | 4 | * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au> |
64c4757e NB |
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 | ||
9a9dab36 | 30 | #include "mdadm.h" |
64c4757e NB |
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 | ||
82b27616 NB |
59 | if (hit<32) { |
60 | uuid[hit/8] <<= 4; | |
61 | uuid[hit/8] += n; | |
62 | } | |
64c4757e NB |
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) | |
682c7051 | 93 | return (vers.major*10000) + (vers.minor*100) + vers.patchlevel; |
64c4757e NB |
94 | |
95 | if (MAJOR(stb.st_rdev) == MD_MAJOR) | |
682c7051 | 96 | return (3600); |
64c4757e NB |
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; | |
682c7051 | 110 | return (a*1000000)+(b*1000)+c; |
64c4757e NB |
111 | } |
112 | ||
113 | int enough(int level, int raid_disks, int avail_disks) | |
114 | { | |
115 | switch (level) { | |
e0d19036 NB |
116 | case -4: |
117 | return avail_disks>= 1; | |
64c4757e NB |
118 | case -1: |
119 | case 0: | |
120 | return avail_disks == raid_disks; | |
121 | case 1: | |
122 | return avail_disks >= 1; | |
123 | case 4: | |
124 | case 5: | |
125 | return avail_disks >= raid_disks-1; | |
126 | default: | |
127 | return 0; | |
128 | } | |
129 | } | |
130 | ||
131 | int same_uuid(int a[4], int b[4]) | |
132 | { | |
133 | if (a[0]==b[0] && | |
134 | a[1]==b[1] && | |
135 | a[2]==b[2] && | |
136 | a[3]==b[3]) | |
137 | return 1; | |
138 | return 0; | |
139 | } | |
140 | ||
141 | void uuid_from_super(int uuid[4], mdp_super_t *super) | |
142 | { | |
143 | uuid[0] = super->set_uuid0; | |
144 | if (super->minor_version >= 90) { | |
145 | uuid[1] = super->set_uuid1; | |
146 | uuid[2] = super->set_uuid2; | |
147 | uuid[3] = super->set_uuid3; | |
148 | } else { | |
149 | uuid[1] = 0; | |
150 | uuid[2] = 0; | |
151 | uuid[3] = 0; | |
152 | } | |
153 | } | |
154 | ||
155 | int compare_super(mdp_super_t *first, mdp_super_t *second) | |
156 | { | |
157 | /* | |
158 | * return: | |
159 | * 0 same, or first was empty, and second was copied | |
160 | * 1 second had wrong number | |
161 | * 2 wrong uuid | |
162 | * 3 wrong other info | |
163 | */ | |
164 | int uuid1[4], uuid2[4]; | |
165 | if (second->md_magic != MD_SB_MAGIC) | |
166 | return 1; | |
167 | if (first-> md_magic != MD_SB_MAGIC) { | |
168 | memcpy(first, second, sizeof(*first)); | |
169 | return 0; | |
170 | } | |
171 | ||
172 | uuid_from_super(uuid1, first); | |
173 | uuid_from_super(uuid2, second); | |
174 | if (!same_uuid(uuid1, uuid2)) | |
175 | return 2; | |
176 | if (first->major_version != second->major_version || | |
177 | first->minor_version != second->minor_version || | |
178 | first->patch_version != second->patch_version || | |
179 | first->gvalid_words != second->gvalid_words || | |
180 | first->ctime != second->ctime || | |
181 | first->level != second->level || | |
182 | first->size != second->size || | |
183 | first->raid_disks != second->raid_disks ) | |
184 | return 3; | |
185 | ||
186 | return 0; | |
187 | } | |
188 | ||
189 | int load_super(int fd, mdp_super_t *super) | |
190 | { | |
191 | /* try to read in the superblock | |
192 | * | |
193 | * return | |
194 | * 0 - success | |
195 | * 1 - no block size | |
196 | * 2 - too small | |
197 | * 3 - no seek | |
198 | * 4 - no read | |
199 | * 5 - no magic | |
200 | * 6 - wrong major version | |
201 | */ | |
e0d19036 NB |
202 | unsigned long size; |
203 | unsigned long long offset; | |
64c4757e NB |
204 | |
205 | if (ioctl(fd, BLKGETSIZE, &size)) | |
206 | return 1; | |
207 | ||
208 | if (size < MD_RESERVED_SECTORS*2) | |
209 | return 2; | |
210 | ||
211 | offset = MD_NEW_SIZE_SECTORS(size); | |
212 | ||
213 | offset *= 512; | |
214 | ||
215 | if (lseek64(fd, offset, 0)< 0LL) | |
216 | return 3; | |
217 | ||
682c7051 | 218 | if (read(fd, super, sizeof(*super)) != sizeof(*super)) |
64c4757e NB |
219 | return 4; |
220 | ||
221 | if (super->md_magic != MD_SB_MAGIC) | |
222 | return 5; | |
223 | ||
224 | if (super->major_version != 0) | |
225 | return 6; | |
226 | return 0; | |
227 | } | |
228 | ||
82b27616 NB |
229 | int store_super(int fd, mdp_super_t *super) |
230 | { | |
231 | long size; | |
232 | long long offset; | |
233 | ||
234 | if (ioctl(fd, BLKGETSIZE, &size)) | |
235 | return 1; | |
236 | ||
237 | if (size < MD_RESERVED_SECTORS*2) | |
238 | return 2; | |
239 | ||
240 | offset = MD_NEW_SIZE_SECTORS(size); | |
241 | ||
242 | offset *= 512; | |
243 | ||
244 | if (lseek64(fd, offset, 0)< 0LL) | |
245 | return 3; | |
246 | ||
247 | if (write(fd, super, sizeof(*super)) != sizeof(*super)) | |
248 | return 4; | |
249 | ||
250 | return 0; | |
251 | } | |
252 | ||
253 | ||
682c7051 NB |
254 | |
255 | int check_ext2(int fd, char *name) | |
256 | { | |
257 | /* | |
258 | * Check for an ext2fs file system. | |
259 | * Superblock is always 1K at 1K offset | |
260 | * | |
261 | * s_magic is le16 at 56 == 0xEF53 | |
262 | * report mtime - le32 at 44 | |
263 | * blocks - le32 at 4 | |
264 | * logblksize - le32 at 24 | |
265 | */ | |
266 | unsigned char sb[1024]; | |
267 | time_t mtime; | |
268 | int size, bsize; | |
269 | if (lseek(fd, 1024,0)!= 1024) | |
270 | return 0; | |
271 | if (read(fd, sb, 1024)!= 1024) | |
272 | return 0; | |
273 | if (sb[56] != 0x53 || sb[57] != 0xef) | |
274 | return 0; | |
275 | ||
276 | mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8; | |
277 | bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8; | |
278 | size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8; | |
279 | fprintf(stderr, Name ": %s appears to contain an ext2fs file system\n", | |
280 | name); | |
281 | fprintf(stderr," size=%dK mtime=%s", | |
282 | size*(1<<bsize), ctime(&mtime)); | |
283 | return 1; | |
284 | } | |
285 | ||
286 | int check_reiser(int fd, char *name) | |
287 | { | |
288 | /* | |
289 | * superblock is at 64K | |
290 | * size is 1024; | |
291 | * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52 | |
292 | * | |
293 | */ | |
294 | unsigned char sb[1024]; | |
295 | int size; | |
296 | if (lseek(fd, 64*1024, 0) != 64*1024) | |
297 | return 0; | |
298 | if (read(fd, sb, 1024) != 1024) | |
299 | return 0; | |
300 | if (strncmp(sb+52, "ReIsErFs",8)!=0 && | |
301 | strncmp(sb+52, "ReIsEr2Fs",9)!=0) | |
302 | return 0; | |
303 | fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name); | |
304 | size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8; | |
305 | fprintf(stderr, " size = %dK\n", size*4); | |
306 | ||
307 | return 1; | |
308 | } | |
309 | ||
310 | int check_raid(int fd, char *name) | |
311 | { | |
312 | mdp_super_t super; | |
313 | time_t crtime; | |
314 | if (load_super(fd, &super)) | |
315 | return 0; | |
316 | /* Looks like a raid array .. */ | |
317 | fprintf(stderr, Name ": %s appear to be part of a raid array:\n", | |
318 | name); | |
319 | crtime = super.ctime; | |
320 | fprintf(stderr, " level=%d disks=%d ctime=%s", | |
321 | super.level, super.raid_disks, ctime(&crtime)); | |
322 | return 1; | |
323 | } | |
324 | ||
325 | ||
326 | int ask(char *mesg) | |
327 | { | |
328 | char *add = ""; | |
329 | int i; | |
330 | for (i=0; i<5; i++) { | |
331 | char buf[100]; | |
332 | fprintf(stderr, "%s%s", mesg, add); | |
333 | fflush(stderr); | |
334 | if (fgets(buf, 100, stdin)==NULL) | |
335 | return 0; | |
336 | if (buf[0]=='y' || buf[0]=='Y') | |
337 | return 1; | |
338 | if (buf[0]=='n' || buf[0]=='N') | |
339 | return 0; | |
340 | add = "(y/n) "; | |
341 | } | |
342 | fprintf(stderr, Name ": assuming 'no'\n"); | |
343 | return 0; | |
344 | } | |
345 | ||
346 | char *map_num(mapping_t *map, int num) | |
347 | { | |
348 | while (map->name) { | |
349 | if (map->num == num) | |
350 | return map->name; | |
351 | map++; | |
352 | } | |
353 | return NULL; | |
354 | } | |
355 | ||
356 | int map_name(mapping_t *map, char *name) | |
357 | { | |
358 | while (map->name) { | |
359 | if (strcmp(map->name, name)==0) | |
360 | return map->num; | |
361 | map++; | |
362 | } | |
363 | return -10; | |
364 | } | |
82b27616 NB |
365 | |
366 | /* | |
367 | * convert a major/minor pair for a block device into a name in /dev, if possible. | |
368 | * On the first call, walk /dev collecting name. | |
369 | * Put them in a simple linked listfor now. | |
370 | */ | |
371 | struct devmap { | |
372 | int major, minor; | |
373 | char *name; | |
374 | struct devmap *next; | |
375 | } *devlist = NULL; | |
376 | int devlist_ready = 0; | |
377 | ||
378 | #define __USE_XOPEN_EXTENDED | |
379 | #include <ftw.h> | |
380 | ||
381 | ||
382 | int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s) | |
383 | { | |
384 | if ((stb->st_mode&S_IFMT)== S_IFBLK) { | |
385 | char *n = strdup(name); | |
386 | struct devmap *dm = malloc(sizeof(*dm)); | |
387 | if (dm) { | |
388 | dm->major = MAJOR(stb->st_rdev); | |
389 | dm->minor = MINOR(stb->st_rdev); | |
390 | dm->name = n; | |
391 | dm->next = devlist; | |
392 | devlist = dm; | |
393 | } | |
394 | } | |
395 | return 0; | |
396 | } | |
397 | ||
398 | char *map_dev(int major, int minor) | |
399 | { | |
400 | struct devmap *p; | |
401 | if (!devlist_ready) { | |
402 | nftw("/dev", add_dev, 10, FTW_PHYS); | |
403 | devlist_ready=1; | |
404 | } | |
405 | ||
406 | for (p=devlist; p; p=p->next) | |
407 | if (p->major == major && | |
408 | p->minor == minor) | |
409 | return p->name; | |
410 | return NULL; | |
411 | } | |
412 | ||
413 | ||
414 | int calc_sb_csum(mdp_super_t *super) | |
415 | { | |
416 | unsigned int oldcsum = super->sb_csum; | |
56eb10c0 | 417 | unsigned long long newcsum = 0; |
82b27616 NB |
418 | unsigned long csum; |
419 | int i; | |
420 | unsigned int *superc = (int*) super; | |
421 | super->sb_csum = 0; | |
422 | ||
423 | for(i=0; i<MD_SB_BYTES/4; i++) | |
424 | newcsum+= superc[i]; | |
425 | csum = (newcsum& 0xffffffff) + (newcsum>>32); | |
426 | super->sb_csum = oldcsum; | |
427 | return csum; | |
428 | } | |
cd29a5c8 | 429 | |
56eb10c0 | 430 | char *human_size(long long bytes) |
cd29a5c8 NB |
431 | { |
432 | static char buf[30]; | |
56eb10c0 | 433 | |
cd29a5c8 | 434 | |
56eb10c0 | 435 | if (bytes < 5000*1024) |
cd29a5c8 | 436 | buf[0]=0; |
56eb10c0 | 437 | else if (bytes < 2*1024LL*1024LL*1024LL) |
e0d19036 | 438 | sprintf(buf, " (%ld.%02ld MiB %ld.%02ld MB)", |
56eb10c0 NB |
439 | (long)(bytes>>20), |
440 | (long)(bytes&0xfffff)/(0x100000/100), | |
441 | (long)(bytes/1000/1000), | |
442 | (long)((bytes%1000000)/10000) | |
443 | ); | |
cd29a5c8 | 444 | else |
e0d19036 | 445 | sprintf(buf, " (%ld.%02ld GiB %ld.%02ld GB)", |
56eb10c0 NB |
446 | (long)(bytes>>30), |
447 | (long)((bytes>>10)&0xfffff)/(0x100000/100), | |
448 | (long)(bytes/1000LL/1000LL/1000LL), | |
449 | (long)(((bytes/1000)%1000000)/10000) | |
450 | ); | |
cd29a5c8 NB |
451 | return buf; |
452 | } | |
e0d19036 NB |
453 | |
454 | char *human_size_brief(long long bytes) | |
455 | { | |
456 | static char buf[30]; | |
457 | ||
458 | ||
459 | if (bytes < 5000*1024) | |
460 | sprintf(buf, "%ld.%02ldKiB", | |
461 | (long)(bytes>>10), (long)((bytes&1023)*100/1024) | |
462 | ); | |
463 | else if (bytes < 2*1024LL*1024LL*1024LL) | |
464 | sprintf(buf, "%ld.%02ldMiB", | |
465 | (long)(bytes>>20), | |
466 | (long)(bytes&0xfffff)/(0x100000/100) | |
467 | ); | |
468 | else | |
469 | sprintf(buf, "%ld.%02ldGiB", | |
470 | (long)(bytes>>30), | |
471 | (long)((bytes>>10)&0xfffff)/(0x100000/100) | |
472 | ); | |
473 | return buf; | |
474 | } | |
475 | ||
476 | ||
477 | #define MD_MAJOR 9 | |
478 | char *get_md_name(int dev) | |
479 | { | |
480 | /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */ | |
481 | static char devname[50]; | |
482 | struct stat stb; | |
483 | dev_t rdev = MKDEV(MD_MAJOR, dev); | |
484 | ||
485 | sprintf(devname, "/dev/md%d", dev); | |
486 | if (stat(devname, &stb) == 0 | |
487 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
488 | && (stb.st_rdev == rdev)) | |
489 | return devname; | |
490 | ||
491 | sprintf(devname, "/dev/md/%d", dev); | |
492 | if (stat(devname, &stb) == 0 | |
493 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
494 | && (stb.st_rdev == rdev)) | |
495 | return devname; | |
496 | ||
497 | sprintf(devname, "/dev/.tmp.md%d", dev); | |
498 | if (mknod(devname, S_IFBLK | 0600, rdev) == -1) | |
499 | return NULL; | |
500 | ||
501 | if (stat(devname, &stb) == 0 | |
502 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
503 | && (stb.st_rdev == rdev)) | |
504 | return devname; | |
505 | unlink(devname); | |
506 | return NULL; | |
507 | } | |
508 | ||
509 | void put_md_name(char *name) | |
510 | { | |
511 | if (strncmp(name, "/dev/.tmp.md", 12)==0) | |
512 | unlink(name); | |
513 | } |