]>
Commit | Line | Data |
---|---|---|
64c4757e | 1 | /* |
9a9dab36 | 2 | * mdadm - manage Linux "md" devices aka RAID arrays. |
64c4757e | 3 | * |
4f589ad0 | 4 | * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de> |
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> | |
98c6faba | 33 | #include <ctype.h> |
0a816ef9 NB |
34 | |
35 | /* | |
36 | * following taken from linux/blkpg.h because they aren't | |
37 | * anywhere else and it isn't safe to #include linux/ * stuff. | |
38 | */ | |
39 | ||
40 | #define BLKPG _IO(0x12,105) | |
41 | ||
42 | /* The argument structure */ | |
43 | struct blkpg_ioctl_arg { | |
44 | int op; | |
45 | int flags; | |
46 | int datalen; | |
47 | void *data; | |
48 | }; | |
49 | ||
50 | /* The subfunctions (for the op field) */ | |
51 | #define BLKPG_ADD_PARTITION 1 | |
52 | #define BLKPG_DEL_PARTITION 2 | |
53 | ||
54 | /* Sizes of name fields. Unused at present. */ | |
55 | #define BLKPG_DEVNAMELTH 64 | |
56 | #define BLKPG_VOLNAMELTH 64 | |
57 | ||
58 | /* The data structure for ADD_PARTITION and DEL_PARTITION */ | |
59 | struct blkpg_partition { | |
60 | long long start; /* starting offset in bytes */ | |
61 | long long length; /* length in bytes */ | |
62 | int pno; /* partition number */ | |
63 | char devname[BLKPG_DEVNAMELTH]; /* partition name, like sda5 or c0d1p2, | |
64 | to be used in kernel messages */ | |
65 | char volname[BLKPG_VOLNAMELTH]; /* volume label */ | |
66 | }; | |
64c4757e NB |
67 | |
68 | /* | |
69 | * Parse a 128 bit uuid in 4 integers | |
70 | * format is 32 hexx nibbles with options :.<space> separator | |
71 | * If not exactly 32 hex digits are found, return 0 | |
72 | * else return 1 | |
73 | */ | |
74 | int parse_uuid(char *str, int uuid[4]) | |
75 | { | |
aba69144 NB |
76 | int hit = 0; /* number of Hex digIT */ |
77 | int i; | |
78 | char c; | |
79 | for (i=0; i<4; i++) uuid[i]=0; | |
80 | ||
81 | while ((c= *str++)) { | |
82 | int n; | |
83 | if (c>='0' && c<='9') | |
84 | n = c-'0'; | |
85 | else if (c>='a' && c <= 'f') | |
86 | n = 10 + c - 'a'; | |
87 | else if (c>='A' && c <= 'F') | |
88 | n = 10 + c - 'A'; | |
89 | else if (strchr(":. -", c)) | |
90 | continue; | |
91 | else return 0; | |
92 | ||
93 | if (hit<32) { | |
94 | uuid[hit/8] <<= 4; | |
95 | uuid[hit/8] += n; | |
96 | } | |
97 | hit++; | |
82b27616 | 98 | } |
aba69144 NB |
99 | if (hit == 32) |
100 | return 1; | |
101 | return 0; | |
64c4757e NB |
102 | } |
103 | ||
104 | ||
105 | /* | |
106 | * Get the md version number. | |
107 | * We use the RAID_VERSION ioctl if it is supported | |
108 | * If not, but we have a block device with major '9', we assume | |
109 | * 0.36.0 | |
110 | * | |
111 | * Return version number as 24 but number - assume version parts | |
112 | * always < 255 | |
113 | */ | |
114 | ||
115 | int md_get_version(int fd) | |
116 | { | |
117 | struct stat stb; | |
118 | mdu_version_t vers; | |
119 | ||
120 | if (fstat(fd, &stb)<0) | |
121 | return -1; | |
122 | if ((S_IFMT&stb.st_mode) != S_IFBLK) | |
123 | return -1; | |
124 | ||
125 | if (ioctl(fd, RAID_VERSION, &vers) == 0) | |
682c7051 | 126 | return (vers.major*10000) + (vers.minor*100) + vers.patchlevel; |
5787fa49 NB |
127 | if (errno == EACCES) |
128 | return -1; | |
0df46c2a | 129 | if (major(stb.st_rdev) == MD_MAJOR) |
682c7051 | 130 | return (3600); |
64c4757e NB |
131 | return -1; |
132 | } | |
133 | ||
64c4757e NB |
134 | int get_linux_version() |
135 | { | |
136 | struct utsname name; | |
98c6faba | 137 | char *cp; |
64c4757e NB |
138 | int a,b,c; |
139 | if (uname(&name) <0) | |
140 | return -1; | |
141 | ||
98c6faba NB |
142 | cp = name.release; |
143 | a = strtoul(cp, &cp, 10); | |
144 | if (*cp != '.') return -1; | |
145 | b = strtoul(cp+1, &cp, 10); | |
146 | if (*cp != '.') return -1; | |
147 | c = strtoul(cp+1, NULL, 10); | |
148 | ||
682c7051 | 149 | return (a*1000000)+(b*1000)+c; |
64c4757e NB |
150 | } |
151 | ||
0430ed48 NB |
152 | void remove_partitions(int fd) |
153 | { | |
154 | /* remove partitions from this block devices. | |
155 | * This is used for components added to an array | |
156 | */ | |
157 | #ifdef BLKPG_DEL_PARTITION | |
158 | struct blkpg_ioctl_arg a; | |
159 | struct blkpg_partition p; | |
160 | ||
161 | a.op = BLKPG_DEL_PARTITION; | |
162 | a.data = (void*)&p; | |
163 | a.datalen = sizeof(p); | |
164 | a.flags = 0; | |
165 | memset(a.data, 0, a.datalen); | |
166 | for (p.pno=0; p.pno < 16; p.pno++) | |
167 | ioctl(fd, BLKPG, &a); | |
168 | #endif | |
169 | } | |
170 | ||
583315d9 | 171 | int enough(int level, int raid_disks, int layout, int clean, |
265e0f17 | 172 | char *avail, int avail_disks) |
64c4757e | 173 | { |
265e0f17 | 174 | int copies, first; |
64c4757e | 175 | switch (level) { |
265e0f17 NB |
176 | case 10: |
177 | /* This is the tricky one - we need to check | |
178 | * which actual disks are present. | |
179 | */ | |
702b557b | 180 | copies = (layout&255)* ((layout>>8) & 255); |
265e0f17 NB |
181 | first=0; |
182 | do { | |
183 | /* there must be one of the 'copies' form 'first' */ | |
184 | int n = copies; | |
185 | int cnt=0; | |
186 | while (n--) { | |
187 | if (avail[first]) | |
188 | cnt++; | |
189 | first = (first+1) % raid_disks; | |
190 | } | |
191 | if (cnt == 0) | |
192 | return 0; | |
193 | ||
194 | } while (first != 0); | |
195 | return 1; | |
e5329c37 | 196 | |
e0d19036 NB |
197 | case -4: |
198 | return avail_disks>= 1; | |
64c4757e NB |
199 | case -1: |
200 | case 0: | |
201 | return avail_disks == raid_disks; | |
202 | case 1: | |
203 | return avail_disks >= 1; | |
204 | case 4: | |
205 | case 5: | |
583315d9 NB |
206 | if (clean) |
207 | return avail_disks >= raid_disks-1; | |
208 | else | |
209 | return avail_disks >= raid_disks; | |
98c6faba | 210 | case 6: |
583315d9 NB |
211 | if (clean) |
212 | return avail_disks >= raid_disks-2; | |
213 | else | |
214 | return avail_disks >= raid_disks; | |
64c4757e NB |
215 | default: |
216 | return 0; | |
217 | } | |
218 | } | |
219 | ||
f277ce36 | 220 | int same_uuid(int a[4], int b[4], int swapuuid) |
64c4757e | 221 | { |
f277ce36 NB |
222 | if (swapuuid) { |
223 | /* parse uuids are hostendian. | |
224 | * uuid's from some superblocks are big-ending | |
aba69144 | 225 | * if there is a difference, we need to swap.. |
f277ce36 NB |
226 | */ |
227 | unsigned char *ac = (unsigned char *)a; | |
228 | unsigned char *bc = (unsigned char *)b; | |
229 | int i; | |
230 | for (i=0; i<16; i+= 4) { | |
231 | if (ac[i+0] != bc[i+3] || | |
232 | ac[i+1] != bc[i+2] || | |
233 | ac[i+2] != bc[i+1] || | |
234 | ac[i+3] != bc[i+0]) | |
235 | return 0; | |
236 | } | |
237 | return 1; | |
238 | } else { | |
239 | if (a[0]==b[0] && | |
240 | a[1]==b[1] && | |
241 | a[2]==b[2] && | |
242 | a[3]==b[3]) | |
243 | return 1; | |
244 | return 0; | |
245 | } | |
64c4757e | 246 | } |
350f29f9 NB |
247 | void copy_uuid(void *a, int b[4], int swapuuid) |
248 | { | |
249 | if (swapuuid) { | |
250 | /* parse uuids are hostendian. | |
251 | * uuid's from some superblocks are big-ending | |
252 | * if there is a difference, we need to swap.. | |
253 | */ | |
254 | unsigned char *ac = (unsigned char *)a; | |
255 | unsigned char *bc = (unsigned char *)b; | |
256 | int i; | |
257 | for (i=0; i<16; i+= 4) { | |
258 | ac[i+0] = bc[i+3]; | |
259 | ac[i+1] = bc[i+2]; | |
260 | ac[i+2] = bc[i+1]; | |
261 | ac[i+3] = bc[i+0]; | |
262 | } | |
263 | } else | |
264 | memcpy(a, b, 16); | |
265 | } | |
64c4757e | 266 | |
435d4ebb | 267 | #ifndef MDASSEMBLE |
682c7051 NB |
268 | int check_ext2(int fd, char *name) |
269 | { | |
270 | /* | |
271 | * Check for an ext2fs file system. | |
272 | * Superblock is always 1K at 1K offset | |
273 | * | |
274 | * s_magic is le16 at 56 == 0xEF53 | |
275 | * report mtime - le32 at 44 | |
276 | * blocks - le32 at 4 | |
277 | * logblksize - le32 at 24 | |
278 | */ | |
279 | unsigned char sb[1024]; | |
280 | time_t mtime; | |
281 | int size, bsize; | |
282 | if (lseek(fd, 1024,0)!= 1024) | |
283 | return 0; | |
284 | if (read(fd, sb, 1024)!= 1024) | |
285 | return 0; | |
286 | if (sb[56] != 0x53 || sb[57] != 0xef) | |
287 | return 0; | |
288 | ||
289 | mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8; | |
290 | bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8; | |
291 | size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8; | |
292 | fprintf(stderr, Name ": %s appears to contain an ext2fs file system\n", | |
293 | name); | |
294 | fprintf(stderr," size=%dK mtime=%s", | |
295 | size*(1<<bsize), ctime(&mtime)); | |
296 | return 1; | |
297 | } | |
298 | ||
299 | int check_reiser(int fd, char *name) | |
300 | { | |
301 | /* | |
302 | * superblock is at 64K | |
303 | * size is 1024; | |
304 | * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52 | |
305 | * | |
306 | */ | |
307 | unsigned char sb[1024]; | |
881990a2 | 308 | unsigned long size; |
682c7051 NB |
309 | if (lseek(fd, 64*1024, 0) != 64*1024) |
310 | return 0; | |
311 | if (read(fd, sb, 1024) != 1024) | |
312 | return 0; | |
a46f4061 NB |
313 | if (strncmp((char*)sb+52, "ReIsErFs",8)!=0 && |
314 | strncmp((char*)sb+52, "ReIsEr2Fs",9)!=0) | |
682c7051 NB |
315 | return 0; |
316 | fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name); | |
317 | size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8; | |
881990a2 | 318 | fprintf(stderr, " size = %luK\n", size*4); |
aba69144 | 319 | |
682c7051 NB |
320 | return 1; |
321 | } | |
322 | ||
323 | int check_raid(int fd, char *name) | |
324 | { | |
4b1ac34b | 325 | struct mdinfo info; |
682c7051 | 326 | time_t crtime; |
d078d77c | 327 | char *level; |
82d9eba6 | 328 | struct supertype *st = guess_super(fd); |
f9ce90ba | 329 | |
82d9eba6 | 330 | if (!st) return 0; |
3da92f27 | 331 | st->ss->load_super(st, fd, name); |
82d9eba6 NB |
332 | /* Looks like a raid array .. */ |
333 | fprintf(stderr, Name ": %s appears to be part of a raid array:\n", | |
334 | name); | |
3da92f27 NB |
335 | st->ss->getinfo_super(st, &info); |
336 | st->ss->free_super(st); | |
82d9eba6 | 337 | crtime = info.array.ctime; |
d078d77c NB |
338 | level = map_num(pers, info.array.level); |
339 | if (!level) level = "-unknown-"; | |
340 | fprintf(stderr, " level=%s devices=%d ctime=%s", | |
341 | level, info.array.raid_disks, ctime(&crtime)); | |
82d9eba6 | 342 | return 1; |
682c7051 NB |
343 | } |
344 | ||
682c7051 NB |
345 | int ask(char *mesg) |
346 | { | |
347 | char *add = ""; | |
348 | int i; | |
349 | for (i=0; i<5; i++) { | |
350 | char buf[100]; | |
351 | fprintf(stderr, "%s%s", mesg, add); | |
352 | fflush(stderr); | |
353 | if (fgets(buf, 100, stdin)==NULL) | |
354 | return 0; | |
355 | if (buf[0]=='y' || buf[0]=='Y') | |
356 | return 1; | |
357 | if (buf[0]=='n' || buf[0]=='N') | |
358 | return 0; | |
359 | add = "(y/n) "; | |
360 | } | |
361 | fprintf(stderr, Name ": assuming 'no'\n"); | |
362 | return 0; | |
363 | } | |
435d4ebb | 364 | #endif /* MDASSEMBLE */ |
682c7051 NB |
365 | |
366 | char *map_num(mapping_t *map, int num) | |
367 | { | |
368 | while (map->name) { | |
369 | if (map->num == num) | |
370 | return map->name; | |
371 | map++; | |
372 | } | |
373 | return NULL; | |
374 | } | |
375 | ||
376 | int map_name(mapping_t *map, char *name) | |
377 | { | |
378 | while (map->name) { | |
379 | if (strcmp(map->name, name)==0) | |
380 | return map->num; | |
381 | map++; | |
382 | } | |
98c6faba | 383 | return UnSet; |
682c7051 | 384 | } |
82b27616 | 385 | |
e5329c37 | 386 | |
8d80900b | 387 | int is_standard(char *dev, int *nump) |
e5329c37 NB |
388 | { |
389 | /* tests if dev is a "standard" md dev name. | |
390 | * i.e if the last component is "/dNN" or "/mdNN", | |
aba69144 | 391 | * where NN is a string of digits |
e5329c37 | 392 | */ |
8d80900b NB |
393 | char *d = strrchr(dev, '/'); |
394 | int type=0; | |
395 | int num; | |
396 | if (!d) | |
e5329c37 | 397 | return 0; |
8d80900b NB |
398 | if (strncmp(d, "/d",2)==0) |
399 | d += 2, type=1; /* /dev/md/dN{pM} */ | |
400 | else if (strncmp(d, "/md_d", 5)==0) | |
401 | d += 5, type=1; /* /dev/md_dNpM */ | |
402 | else if (strncmp(d, "/md", 3)==0) | |
403 | d += 3, type=-1; /* /dev/mdN */ | |
404 | else if (d-dev > 3 && strncmp(d-2, "md/", 3)==0) | |
5a6d1148 | 405 | d += 1, type=-1; /* /dev/md/N */ |
e5329c37 NB |
406 | else |
407 | return 0; | |
8d80900b | 408 | if (!*d) |
e5329c37 | 409 | return 0; |
8d80900b NB |
410 | num = atoi(d); |
411 | while (isdigit(*d)) | |
412 | d++; | |
413 | if (*d) | |
e5329c37 | 414 | return 0; |
8d80900b NB |
415 | if (nump) *nump = num; |
416 | ||
417 | return type; | |
e5329c37 NB |
418 | } |
419 | ||
420 | ||
82b27616 NB |
421 | /* |
422 | * convert a major/minor pair for a block device into a name in /dev, if possible. | |
423 | * On the first call, walk /dev collecting name. | |
424 | * Put them in a simple linked listfor now. | |
425 | */ | |
426 | struct devmap { | |
427 | int major, minor; | |
428 | char *name; | |
429 | struct devmap *next; | |
430 | } *devlist = NULL; | |
431 | int devlist_ready = 0; | |
432 | ||
82b27616 NB |
433 | int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s) |
434 | { | |
bed256c2 NB |
435 | struct stat st; |
436 | if (S_ISLNK(stb->st_mode)) { | |
437 | stat(name, &st); | |
438 | stb = &st; | |
82b27616 | 439 | } |
bed256c2 NB |
440 | |
441 | if ((stb->st_mode&S_IFMT)== S_IFBLK) { | |
442 | char *n = strdup(name); | |
443 | struct devmap *dm = malloc(sizeof(*dm)); | |
444 | if (strncmp(n, "/dev/./", 7)==0) | |
445 | strcpy(n+4, name+6); | |
446 | if (dm) { | |
447 | dm->major = major(stb->st_rdev); | |
448 | dm->minor = minor(stb->st_rdev); | |
449 | dm->name = n; | |
450 | dm->next = devlist; | |
451 | devlist = dm; | |
452 | } | |
453 | } | |
454 | return 0; | |
82b27616 NB |
455 | } |
456 | ||
45e878bb NB |
457 | #ifndef HAVE_NFTW |
458 | #ifdef HAVE_FTW | |
459 | int add_dev_1(const char *name, const struct stat *stb, int flag) | |
460 | { | |
461 | return add_dev(name, stb, flag, NULL); | |
462 | } | |
463 | int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags) | |
464 | { | |
465 | return ftw(path, add_dev_1, nopenfd); | |
466 | } | |
467 | #else | |
45e878bb NB |
468 | int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags) |
469 | { | |
470 | return 0; | |
471 | } | |
472 | #endif /* HAVE_FTW */ | |
473 | #endif /* HAVE_NFTW */ | |
474 | ||
dd0781e5 NB |
475 | /* |
476 | * Find a block device with the right major/minor number. | |
b79713f8 NB |
477 | * If we find multiple names, choose the shortest. |
478 | * If we find a non-standard name, it is probably there | |
479 | * deliberately so prefer it over a standard name. | |
480 | * This applies only to names for MD devices. | |
dd0781e5 | 481 | */ |
16c6fa80 | 482 | char *map_dev(int major, int minor, int create) |
82b27616 | 483 | { |
dd0781e5 | 484 | struct devmap *p; |
b79713f8 | 485 | char *std = NULL, *nonstd=NULL; |
e7bb5d23 | 486 | int did_check = 0; |
eed35d66 | 487 | |
e81cdd9f | 488 | if (major == 0 && minor == 0) |
eed35d66 | 489 | return NULL; |
e81cdd9f | 490 | |
e7bb5d23 | 491 | retry: |
dd0781e5 | 492 | if (!devlist_ready) { |
0a416ec3 NB |
493 | char *dev = "/dev"; |
494 | struct stat stb; | |
eed35d66 NB |
495 | while(devlist) { |
496 | struct devmap *d = devlist; | |
497 | devlist = d->next; | |
498 | free(d->name); | |
499 | free(d); | |
500 | } | |
0a416ec3 NB |
501 | if (lstat(dev, &stb)==0 && |
502 | S_ISLNK(stb.st_mode)) | |
503 | dev = "/dev/."; | |
504 | nftw(dev, add_dev, 10, FTW_PHYS); | |
dd0781e5 | 505 | devlist_ready=1; |
e7bb5d23 | 506 | did_check = 1; |
dd0781e5 | 507 | } |
82b27616 | 508 | |
dd0781e5 NB |
509 | for (p=devlist; p; p=p->next) |
510 | if (p->major == major && | |
511 | p->minor == minor) { | |
b79713f8 NB |
512 | if (is_standard(p->name, NULL)) { |
513 | if (std == NULL || | |
514 | strlen(p->name) < strlen(std)) | |
515 | std = p->name; | |
516 | } else { | |
517 | if (nonstd == NULL || | |
518 | strlen(p->name) < strlen(nonstd)) | |
519 | nonstd = p->name; | |
520 | } | |
dd0781e5 | 521 | } |
e7bb5d23 NB |
522 | if (!std && !nonstd && !did_check) { |
523 | devlist_ready = 0; | |
524 | goto retry; | |
525 | } | |
16c6fa80 NB |
526 | if (create && !std && !nonstd) { |
527 | static char buf[30]; | |
382245c3 | 528 | snprintf(buf, sizeof(buf), "%d:%d", major, minor); |
16c6fa80 NB |
529 | nonstd = buf; |
530 | } | |
531 | ||
b79713f8 | 532 | return nonstd ? nonstd : std; |
82b27616 NB |
533 | } |
534 | ||
4b1ac34b | 535 | unsigned long calc_csum(void *super, int bytes) |
82b27616 | 536 | { |
56eb10c0 | 537 | unsigned long long newcsum = 0; |
82b27616 | 538 | int i; |
4b1ac34b NB |
539 | unsigned int csum; |
540 | unsigned int *superc = (unsigned int*) super; | |
82b27616 | 541 | |
4b1ac34b | 542 | for(i=0; i<bytes/4; i++) |
82b27616 NB |
543 | newcsum+= superc[i]; |
544 | csum = (newcsum& 0xffffffff) + (newcsum>>32); | |
570c0542 | 545 | #ifdef __alpha__ |
aba69144 | 546 | /* The in-kernel checksum calculation is always 16bit on |
570c0542 NB |
547 | * the alpha, though it is 32 bit on i386... |
548 | * I wonder what it is elsewhere... (it uses and API in | |
549 | * a way that it shouldn't). | |
550 | */ | |
551 | csum = (csum & 0xffff) + (csum >> 16); | |
552 | csum = (csum & 0xffff) + (csum >> 16); | |
553 | #endif | |
82b27616 NB |
554 | return csum; |
555 | } | |
cd29a5c8 | 556 | |
435d4ebb | 557 | #ifndef MDASSEMBLE |
56eb10c0 | 558 | char *human_size(long long bytes) |
cd29a5c8 NB |
559 | { |
560 | static char buf[30]; | |
d5d3721e NB |
561 | |
562 | /* We convert bytes to either centi-M{ega,ibi}bytes or | |
563 | * centi-G{igi,ibi}bytes, with appropriate rounding, | |
564 | * and then print 1/100th of those as a decimal. | |
565 | * We allow upto 2048Megabytes before converting to | |
566 | * gigabytes, as that shows more precision and isn't | |
567 | * too large a number. | |
568 | * Terrabytes are not yet handled. | |
569 | */ | |
cd29a5c8 | 570 | |
56eb10c0 | 571 | if (bytes < 5000*1024) |
cd29a5c8 | 572 | buf[0]=0; |
d5d3721e NB |
573 | else if (bytes < 2*1024LL*1024LL*1024LL) { |
574 | long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2; | |
575 | long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2; | |
8f23b0b3 | 576 | snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)", |
d5d3721e NB |
577 | cMiB/100 , cMiB % 100, |
578 | cMB/100, cMB % 100); | |
579 | } else { | |
580 | long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2; | |
581 | long cGB = (bytes / (1000000000LL/200LL ) +1) /2; | |
8f23b0b3 | 582 | snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)", |
d5d3721e NB |
583 | cGiB/100 , cGiB % 100, |
584 | cGB/100, cGB % 100); | |
585 | } | |
cd29a5c8 NB |
586 | return buf; |
587 | } | |
e0d19036 NB |
588 | |
589 | char *human_size_brief(long long bytes) | |
590 | { | |
591 | static char buf[30]; | |
e0d19036 NB |
592 | |
593 | if (bytes < 5000*1024) | |
8f23b0b3 | 594 | snprintf(buf, sizeof(buf), "%ld.%02ldKiB", |
bd526cee | 595 | (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024) |
e0d19036 NB |
596 | ); |
597 | else if (bytes < 2*1024LL*1024LL*1024LL) | |
8f23b0b3 | 598 | snprintf(buf, sizeof(buf), "%ld.%02ldMiB", |
e0d19036 | 599 | (long)(bytes>>20), |
bd526cee | 600 | (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100) |
e0d19036 NB |
601 | ); |
602 | else | |
8f23b0b3 | 603 | snprintf(buf, sizeof(buf), "%ld.%02ldGiB", |
e0d19036 | 604 | (long)(bytes>>30), |
bd526cee | 605 | (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100) |
e0d19036 NB |
606 | ); |
607 | return buf; | |
608 | } | |
e4965ef8 N |
609 | |
610 | void print_r10_layout(int layout) | |
611 | { | |
612 | int near = layout & 255; | |
613 | int far = (layout >> 8) & 255; | |
614 | int offset = (layout&0x10000); | |
615 | char *sep = ""; | |
616 | ||
617 | if (near != 1) { | |
618 | printf("%s near=%d", sep, near); | |
619 | sep = ","; | |
620 | } | |
621 | if (far != 1) | |
622 | printf("%s %s=%d", sep, offset?"offset":"far", far); | |
623 | if (near*far == 1) | |
624 | printf("NO REDUNDANCY"); | |
625 | } | |
435d4ebb | 626 | #endif |
e0d19036 | 627 | |
435d4ebb | 628 | #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) |
dd0781e5 | 629 | int get_mdp_major(void) |
98c6faba | 630 | { |
dd0781e5 NB |
631 | static int mdp_major = -1; |
632 | FILE *fl; | |
98c6faba NB |
633 | char *w; |
634 | int have_block = 0; | |
635 | int have_devices = 0; | |
636 | int last_num = -1; | |
dd0781e5 NB |
637 | |
638 | if (mdp_major != -1) | |
639 | return mdp_major; | |
640 | fl = fopen("/proc/devices", "r"); | |
98c6faba | 641 | if (!fl) |
dd0781e5 | 642 | return -1; |
98c6faba NB |
643 | while ((w = conf_word(fl, 1))) { |
644 | if (have_block && strcmp(w, "devices:")==0) | |
645 | have_devices = 1; | |
646 | have_block = (strcmp(w, "Block")==0); | |
647 | if (isdigit(w[0])) | |
648 | last_num = atoi(w); | |
649 | if (have_devices && strcmp(w, "mdp")==0) | |
650 | mdp_major = last_num; | |
651 | free(w); | |
652 | } | |
653 | fclose(fl); | |
dd0781e5 | 654 | return mdp_major; |
98c6faba NB |
655 | } |
656 | ||
657 | ||
e0d19036 | 658 | |
e0d19036 NB |
659 | char *get_md_name(int dev) |
660 | { | |
661 | /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */ | |
98c6faba | 662 | /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */ |
e0d19036 NB |
663 | static char devname[50]; |
664 | struct stat stb; | |
98c6faba | 665 | dev_t rdev; |
dd0781e5 | 666 | char *dn; |
98c6faba NB |
667 | |
668 | if (dev < 0) { | |
dd0781e5 NB |
669 | int mdp = get_mdp_major(); |
670 | if (mdp < 0) return NULL; | |
0df46c2a | 671 | rdev = makedev(mdp, (-1-dev)<<6); |
8f23b0b3 | 672 | snprintf(devname, sizeof(devname), "/dev/md/d%d", -1-dev); |
98c6faba NB |
673 | if (stat(devname, &stb) == 0 |
674 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
675 | && (stb.st_rdev == rdev)) | |
676 | return devname; | |
677 | } else { | |
0df46c2a | 678 | rdev = makedev(MD_MAJOR, dev); |
8f23b0b3 | 679 | snprintf(devname, sizeof(devname), "/dev/md%d", dev); |
98c6faba NB |
680 | if (stat(devname, &stb) == 0 |
681 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
682 | && (stb.st_rdev == rdev)) | |
683 | return devname; | |
684 | ||
8f23b0b3 | 685 | snprintf(devname, sizeof(devname), "/dev/md/%d", dev); |
98c6faba NB |
686 | if (stat(devname, &stb) == 0 |
687 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
688 | && (stb.st_rdev == rdev)) | |
689 | return devname; | |
690 | } | |
16c6fa80 | 691 | dn = map_dev(major(rdev), minor(rdev), 0); |
dd0781e5 NB |
692 | if (dn) |
693 | return dn; | |
8f23b0b3 | 694 | snprintf(devname, sizeof(devname), "/dev/.tmp.md%d", dev); |
e0d19036 | 695 | if (mknod(devname, S_IFBLK | 0600, rdev) == -1) |
dd0781e5 NB |
696 | if (errno != EEXIST) |
697 | return NULL; | |
e0d19036 NB |
698 | |
699 | if (stat(devname, &stb) == 0 | |
700 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
701 | && (stb.st_rdev == rdev)) | |
702 | return devname; | |
703 | unlink(devname); | |
704 | return NULL; | |
705 | } | |
706 | ||
707 | void put_md_name(char *name) | |
708 | { | |
709 | if (strncmp(name, "/dev/.tmp.md", 12)==0) | |
710 | unlink(name); | |
711 | } | |
ea24acd0 NB |
712 | |
713 | static int dev2major(int d) | |
714 | { | |
715 | if (d >= 0) | |
716 | return MD_MAJOR; | |
717 | else | |
718 | return get_mdp_major(); | |
719 | } | |
720 | ||
721 | static int dev2minor(int d) | |
722 | { | |
723 | if (d >= 0) | |
724 | return d; | |
725 | return (-1-d) << MdpMinorShift; | |
726 | } | |
727 | ||
728 | int find_free_devnum(int use_partitions) | |
729 | { | |
730 | int devnum; | |
731 | for (devnum = 127; devnum != 128; | |
732 | devnum = devnum ? devnum-1 : (1<<22)-1) { | |
733 | char *dn; | |
734 | int _devnum; | |
735 | ||
736 | _devnum = use_partitions ? (-1-devnum) : devnum; | |
737 | if (mddev_busy(_devnum)) | |
738 | continue; | |
739 | /* make sure it is new to /dev too, at least as a | |
740 | * non-standard */ | |
741 | dn = map_dev(dev2major(_devnum), dev2minor(_devnum), 0); | |
742 | if (dn && ! is_standard(dn, NULL)) | |
743 | continue; | |
744 | break; | |
745 | } | |
746 | if (devnum == 128) | |
747 | return NoMdDev; | |
748 | return use_partitions ? (-1-devnum) : devnum; | |
749 | } | |
435d4ebb | 750 | #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */ |
f9ce90ba | 751 | |
8b0dabea NB |
752 | int dev_open(char *dev, int flags) |
753 | { | |
754 | /* like 'open', but if 'dev' matches %d:%d, create a temp | |
755 | * block device and open that | |
756 | */ | |
757 | char *e; | |
758 | int fd = -1; | |
759 | char devname[32]; | |
e81cdd9f | 760 | int major; |
8b0dabea | 761 | int minor; |
e81cdd9f NB |
762 | |
763 | if (!dev) return -1; | |
764 | ||
765 | major = strtoul(dev, &e, 0); | |
8b0dabea NB |
766 | if (e > dev && *e == ':' && e[1] && |
767 | (minor = strtoul(e+1, &e, 0)) >= 0 && | |
768 | *e == 0) { | |
769 | snprintf(devname, sizeof(devname), "/dev/.tmp.md.%d:%d", major, minor); | |
770 | if (mknod(devname, S_IFBLK|0600, makedev(major, minor))==0) { | |
771 | fd = open(devname, flags); | |
772 | unlink(devname); | |
773 | } | |
774 | } else | |
775 | fd = open(dev, flags); | |
776 | return fd; | |
777 | } | |
f9ce90ba | 778 | |
82d9eba6 | 779 | struct superswitch *superlist[] = { &super0, &super1, NULL }; |
f9ce90ba | 780 | |
ea24acd0 | 781 | #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) |
1686dc25 | 782 | struct supertype *super_by_fd(int fd) |
f9ce90ba | 783 | { |
1686dc25 NB |
784 | mdu_array_info_t array; |
785 | int vers; | |
786 | int minor; | |
787 | struct supertype *st = NULL; | |
7e0f6979 | 788 | struct mdinfo *sra; |
142cb9e1 | 789 | char *verstr; |
1686dc25 NB |
790 | char version[20]; |
791 | int i; | |
792 | ||
793 | sra = sysfs_read(fd, 0, GET_VERSION); | |
794 | ||
795 | if (sra) { | |
7e0f6979 NB |
796 | vers = sra->array.major_version; |
797 | minor = sra->array.minor_version; | |
142cb9e1 | 798 | verstr = sra->text_version; |
1686dc25 NB |
799 | } else { |
800 | if (ioctl(fd, GET_ARRAY_INFO, &array)) | |
801 | array.major_version = array.minor_version = 0; | |
802 | vers = array.major_version; | |
803 | minor = array.minor_version; | |
142cb9e1 | 804 | verstr = ""; |
6fbba4c9 | 805 | } |
82d9eba6 | 806 | |
1686dc25 NB |
807 | if (vers != -1) { |
808 | sprintf(version, "%d.%d", vers, minor); | |
809 | verstr = version; | |
6fbba4c9 | 810 | } |
1686dc25 NB |
811 | for (i = 0; st == NULL && superlist[i] ; i++) |
812 | st = superlist[i]->match_metadata_desc(verstr); | |
813 | ||
814 | if (sra) | |
815 | sysfs_free(sra); | |
3b0896f8 NB |
816 | if (st) |
817 | st->sb = NULL; | |
82d9eba6 | 818 | return st; |
f9ce90ba | 819 | } |
ea24acd0 NB |
820 | #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */ |
821 | ||
f9ce90ba | 822 | |
3da92f27 NB |
823 | struct supertype *dup_super(struct supertype *st) |
824 | { | |
1686dc25 NB |
825 | struct supertype *stnew = NULL; |
826 | char *verstr = NULL; | |
827 | char version[20]; | |
828 | int i; | |
829 | ||
3da92f27 NB |
830 | if (!st) |
831 | return st; | |
1686dc25 NB |
832 | |
833 | if (st->minor_version == -1) | |
834 | sprintf(version, "%d", st->ss->major); | |
835 | else | |
836 | sprintf(version, "%d.%d", st->ss->major, st->minor_version); | |
837 | verstr = version; | |
838 | ||
839 | for (i = 0; stnew == NULL && superlist[i] ; i++) | |
840 | stnew = superlist[i]->match_metadata_desc(verstr); | |
841 | ||
ff1f6545 NB |
842 | if (stnew) |
843 | stnew->sb = NULL; | |
1686dc25 | 844 | return stnew; |
3da92f27 NB |
845 | } |
846 | ||
82d9eba6 | 847 | struct supertype *guess_super(int fd) |
f9ce90ba NB |
848 | { |
849 | /* try each load_super to find the best match, | |
850 | * and return the best superswitch | |
851 | */ | |
82d9eba6 NB |
852 | struct superswitch *ss; |
853 | struct supertype *st; | |
570c0542 NB |
854 | unsigned long besttime = 0; |
855 | int bestsuper = -1; | |
f9ce90ba NB |
856 | int i; |
857 | ||
82d9eba6 NB |
858 | st = malloc(sizeof(*st)); |
859 | memset(st, 0, sizeof(*st)); | |
f9ce90ba NB |
860 | for (i=0 ; superlist[i]; i++) { |
861 | int rv; | |
862 | ss = superlist[i]; | |
f277ce36 | 863 | st->ss = NULL; |
3da92f27 | 864 | rv = ss->load_super(st, fd, NULL); |
570c0542 NB |
865 | if (rv == 0) { |
866 | struct mdinfo info; | |
3da92f27 | 867 | st->ss->getinfo_super(st, &info); |
570c0542 NB |
868 | if (bestsuper == -1 || |
869 | besttime < info.array.ctime) { | |
870 | bestsuper = i; | |
871 | besttime = info.array.ctime; | |
570c0542 | 872 | } |
3da92f27 | 873 | ss->free_super(st); |
570c0542 NB |
874 | } |
875 | } | |
876 | if (bestsuper != -1) { | |
877 | int rv; | |
f277ce36 | 878 | st->ss = NULL; |
3da92f27 | 879 | rv = superlist[bestsuper]->load_super(st, fd, NULL); |
f9ce90ba | 880 | if (rv == 0) { |
5e747af2 | 881 | superlist[bestsuper]->free_super(st); |
82d9eba6 | 882 | return st; |
f9ce90ba NB |
883 | } |
884 | } | |
570c0542 | 885 | free(st); |
f9ce90ba NB |
886 | return NULL; |
887 | } | |
fe6729fa | 888 | |
beae1dfe NB |
889 | /* Return size of device in bytes */ |
890 | int get_dev_size(int fd, char *dname, unsigned long long *sizep) | |
891 | { | |
892 | unsigned long long ldsize; | |
c2c9bb6f NB |
893 | struct stat st; |
894 | ||
895 | if (fstat(fd, &st) != -1 && S_ISREG(st.st_mode)) | |
896 | ldsize = (unsigned long long)st.st_size; | |
897 | else | |
beae1dfe NB |
898 | #ifdef BLKGETSIZE64 |
899 | if (ioctl(fd, BLKGETSIZE64, &ldsize) != 0) | |
900 | #endif | |
901 | { | |
902 | unsigned long dsize; | |
903 | if (ioctl(fd, BLKGETSIZE, &dsize) == 0) { | |
904 | ldsize = dsize; | |
905 | ldsize <<= 9; | |
906 | } else { | |
907 | if (dname) | |
908 | fprintf(stderr, Name ": Cannot get size of %s: %s\b", | |
909 | dname, strerror(errno)); | |
910 | return 0; | |
911 | } | |
912 | } | |
913 | *sizep = ldsize; | |
914 | return 1; | |
915 | } | |
8fac0577 | 916 | |
8382f19b NB |
917 | void get_one_disk(int mdfd, mdu_array_info_t *ainf, mdu_disk_info_t *disk) |
918 | { | |
919 | int d; | |
920 | ioctl(mdfd, GET_ARRAY_INFO, ainf); | |
921 | for (d = 0 ; d < ainf->raid_disks + ainf->nr_disks ; d++) | |
922 | if (ioctl(mdfd, GET_DISK_INFO, disk) == 0) | |
923 | return; | |
924 | } | |
63152c1b | 925 | |
fe6729fa NB |
926 | #ifdef __TINYC__ |
927 | /* tinyc doesn't optimize this check in ioctl.h out ... */ | |
928 | unsigned int __invalid_size_argument_for_IOC = 0; | |
929 | #endif | |
930 |