]>
Commit | Line | Data |
---|---|---|
64c4757e | 1 | /* |
9a9dab36 | 2 | * mdadm - manage Linux "md" devices aka RAID arrays. |
64c4757e | 3 | * |
e736b623 | 4 | * Copyright (C) 2001-2009 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 | |
e736b623 | 22 | * Email: <neilb@suse.de> |
64c4757e NB |
23 | */ |
24 | ||
9a9dab36 | 25 | #include "mdadm.h" |
64c4757e | 26 | #include "md_p.h" |
edd8d13c | 27 | #include <sys/socket.h> |
64c4757e | 28 | #include <sys/utsname.h> |
9fe32043 | 29 | #include <sys/wait.h> |
edd8d13c | 30 | #include <sys/un.h> |
98c6faba | 31 | #include <ctype.h> |
a322f70c | 32 | #include <dirent.h> |
a931db9e | 33 | #include <signal.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 | ||
eb3929a4 | 152 | #ifndef MDASSEMBLE |
84e11361 N |
153 | long long parse_size(char *size) |
154 | { | |
155 | /* parse 'size' which should be a number optionally | |
156 | * followed by 'K', 'M', or 'G'. | |
157 | * Without a suffix, K is assumed. | |
158 | * Number returned is in sectors (half-K) | |
159 | */ | |
160 | char *c; | |
161 | long long s = strtoll(size, &c, 10); | |
162 | if (s > 0) { | |
163 | switch (*c) { | |
164 | case 'K': | |
165 | c++; | |
166 | default: | |
167 | s *= 2; | |
168 | break; | |
169 | case 'M': | |
170 | c++; | |
171 | s *= 1024 * 2; | |
172 | break; | |
173 | case 'G': | |
174 | c++; | |
175 | s *= 1024 * 1024 * 2; | |
176 | break; | |
177 | } | |
178 | } | |
179 | if (*c) | |
180 | s = 0; | |
181 | return s; | |
182 | } | |
183 | ||
4a06e2c2 N |
184 | int parse_layout_10(char *layout) |
185 | { | |
186 | int copies, rv; | |
187 | char *cp; | |
188 | /* Parse the layout string for raid10 */ | |
189 | /* 'f', 'o' or 'n' followed by a number <= raid_disks */ | |
190 | if ((layout[0] != 'n' && layout[0] != 'f' && layout[0] != 'o') || | |
191 | (copies = strtoul(layout+1, &cp, 10)) < 1 || | |
192 | copies > 200 || | |
193 | *cp) | |
194 | return -1; | |
195 | if (layout[0] == 'n') | |
196 | rv = 256 + copies; | |
197 | else if (layout[0] == 'o') | |
198 | rv = 0x10000 + (copies<<8) + 1; | |
199 | else | |
200 | rv = 1 + (copies<<8); | |
201 | return rv; | |
202 | } | |
203 | ||
204 | int parse_layout_faulty(char *layout) | |
205 | { | |
206 | /* Parse the layout string for 'faulty' */ | |
207 | int ln = strcspn(layout, "0123456789"); | |
208 | char *m = strdup(layout); | |
209 | int mode; | |
210 | m[ln] = 0; | |
211 | mode = map_name(faultylayout, m); | |
212 | if (mode == UnSet) | |
213 | return -1; | |
214 | ||
215 | return mode | (atoi(layout+ln)<< ModeShift); | |
216 | } | |
eb3929a4 | 217 | #endif |
4a06e2c2 | 218 | |
0430ed48 NB |
219 | void remove_partitions(int fd) |
220 | { | |
221 | /* remove partitions from this block devices. | |
222 | * This is used for components added to an array | |
223 | */ | |
224 | #ifdef BLKPG_DEL_PARTITION | |
225 | struct blkpg_ioctl_arg a; | |
226 | struct blkpg_partition p; | |
227 | ||
228 | a.op = BLKPG_DEL_PARTITION; | |
229 | a.data = (void*)&p; | |
230 | a.datalen = sizeof(p); | |
231 | a.flags = 0; | |
232 | memset(a.data, 0, a.datalen); | |
233 | for (p.pno=0; p.pno < 16; p.pno++) | |
234 | ioctl(fd, BLKPG, &a); | |
235 | #endif | |
236 | } | |
237 | ||
583315d9 | 238 | int enough(int level, int raid_disks, int layout, int clean, |
265e0f17 | 239 | char *avail, int avail_disks) |
64c4757e | 240 | { |
265e0f17 | 241 | int copies, first; |
64c4757e | 242 | switch (level) { |
265e0f17 NB |
243 | case 10: |
244 | /* This is the tricky one - we need to check | |
245 | * which actual disks are present. | |
246 | */ | |
702b557b | 247 | copies = (layout&255)* ((layout>>8) & 255); |
265e0f17 NB |
248 | first=0; |
249 | do { | |
250 | /* there must be one of the 'copies' form 'first' */ | |
251 | int n = copies; | |
252 | int cnt=0; | |
253 | while (n--) { | |
254 | if (avail[first]) | |
255 | cnt++; | |
256 | first = (first+1) % raid_disks; | |
257 | } | |
258 | if (cnt == 0) | |
259 | return 0; | |
260 | ||
261 | } while (first != 0); | |
262 | return 1; | |
e5329c37 | 263 | |
e0d19036 NB |
264 | case -4: |
265 | return avail_disks>= 1; | |
64c4757e NB |
266 | case -1: |
267 | case 0: | |
268 | return avail_disks == raid_disks; | |
269 | case 1: | |
270 | return avail_disks >= 1; | |
271 | case 4: | |
272 | case 5: | |
583315d9 NB |
273 | if (clean) |
274 | return avail_disks >= raid_disks-1; | |
275 | else | |
276 | return avail_disks >= raid_disks; | |
98c6faba | 277 | case 6: |
583315d9 NB |
278 | if (clean) |
279 | return avail_disks >= raid_disks-2; | |
280 | else | |
281 | return avail_disks >= raid_disks; | |
64c4757e NB |
282 | default: |
283 | return 0; | |
284 | } | |
285 | } | |
286 | ||
36ba7d48 | 287 | const int uuid_match_any[4] = { ~0, ~0, ~0, ~0 }; |
f277ce36 | 288 | int same_uuid(int a[4], int b[4], int swapuuid) |
64c4757e | 289 | { |
36ba7d48 DW |
290 | if (memcmp(a, uuid_match_any, sizeof(int[4])) == 0 || |
291 | memcmp(b, uuid_match_any, sizeof(int[4])) == 0) | |
292 | return 1; | |
293 | ||
f277ce36 NB |
294 | if (swapuuid) { |
295 | /* parse uuids are hostendian. | |
296 | * uuid's from some superblocks are big-ending | |
aba69144 | 297 | * if there is a difference, we need to swap.. |
f277ce36 NB |
298 | */ |
299 | unsigned char *ac = (unsigned char *)a; | |
300 | unsigned char *bc = (unsigned char *)b; | |
301 | int i; | |
302 | for (i=0; i<16; i+= 4) { | |
303 | if (ac[i+0] != bc[i+3] || | |
304 | ac[i+1] != bc[i+2] || | |
305 | ac[i+2] != bc[i+1] || | |
306 | ac[i+3] != bc[i+0]) | |
307 | return 0; | |
308 | } | |
309 | return 1; | |
310 | } else { | |
311 | if (a[0]==b[0] && | |
312 | a[1]==b[1] && | |
313 | a[2]==b[2] && | |
314 | a[3]==b[3]) | |
315 | return 1; | |
316 | return 0; | |
317 | } | |
64c4757e | 318 | } |
350f29f9 NB |
319 | void copy_uuid(void *a, int b[4], int swapuuid) |
320 | { | |
321 | if (swapuuid) { | |
322 | /* parse uuids are hostendian. | |
323 | * uuid's from some superblocks are big-ending | |
324 | * if there is a difference, we need to swap.. | |
325 | */ | |
326 | unsigned char *ac = (unsigned char *)a; | |
327 | unsigned char *bc = (unsigned char *)b; | |
328 | int i; | |
329 | for (i=0; i<16; i+= 4) { | |
330 | ac[i+0] = bc[i+3]; | |
331 | ac[i+1] = bc[i+2]; | |
332 | ac[i+2] = bc[i+1]; | |
333 | ac[i+3] = bc[i+0]; | |
334 | } | |
335 | } else | |
336 | memcpy(a, b, 16); | |
337 | } | |
64c4757e | 338 | |
ff54de6e | 339 | char *fname_from_uuid(struct supertype *st, struct mdinfo *info, char *buf, char sep) |
d7288ddc | 340 | { |
9968e376 DW |
341 | int i, j; |
342 | int id; | |
d7288ddc N |
343 | char uuid[16]; |
344 | char *c = buf; | |
345 | strcpy(c, "UUID-"); | |
346 | c += strlen(c); | |
347 | copy_uuid(uuid, info->uuid, st->ss->swapuuid); | |
9968e376 DW |
348 | for (i = 0; i < 4; i++) { |
349 | id = uuid[i]; | |
350 | if (i) | |
ff54de6e | 351 | *c++ = sep; |
9968e376 DW |
352 | for (j = 3; j >= 0; j--) { |
353 | sprintf(c,"%02x", (unsigned char) uuid[j+4*i]); | |
354 | c+= 2; | |
355 | } | |
d7288ddc N |
356 | } |
357 | return buf; | |
358 | } | |
359 | ||
435d4ebb | 360 | #ifndef MDASSEMBLE |
682c7051 NB |
361 | int check_ext2(int fd, char *name) |
362 | { | |
363 | /* | |
364 | * Check for an ext2fs file system. | |
365 | * Superblock is always 1K at 1K offset | |
366 | * | |
367 | * s_magic is le16 at 56 == 0xEF53 | |
368 | * report mtime - le32 at 44 | |
369 | * blocks - le32 at 4 | |
370 | * logblksize - le32 at 24 | |
371 | */ | |
372 | unsigned char sb[1024]; | |
373 | time_t mtime; | |
374 | int size, bsize; | |
375 | if (lseek(fd, 1024,0)!= 1024) | |
376 | return 0; | |
377 | if (read(fd, sb, 1024)!= 1024) | |
378 | return 0; | |
379 | if (sb[56] != 0x53 || sb[57] != 0xef) | |
380 | return 0; | |
381 | ||
382 | mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8; | |
383 | bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8; | |
384 | size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8; | |
385 | fprintf(stderr, Name ": %s appears to contain an ext2fs file system\n", | |
386 | name); | |
387 | fprintf(stderr," size=%dK mtime=%s", | |
388 | size*(1<<bsize), ctime(&mtime)); | |
389 | return 1; | |
390 | } | |
391 | ||
392 | int check_reiser(int fd, char *name) | |
393 | { | |
394 | /* | |
395 | * superblock is at 64K | |
396 | * size is 1024; | |
397 | * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52 | |
398 | * | |
399 | */ | |
400 | unsigned char sb[1024]; | |
881990a2 | 401 | unsigned long size; |
682c7051 NB |
402 | if (lseek(fd, 64*1024, 0) != 64*1024) |
403 | return 0; | |
404 | if (read(fd, sb, 1024) != 1024) | |
405 | return 0; | |
a46f4061 NB |
406 | if (strncmp((char*)sb+52, "ReIsErFs",8)!=0 && |
407 | strncmp((char*)sb+52, "ReIsEr2Fs",9)!=0) | |
682c7051 NB |
408 | return 0; |
409 | fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name); | |
410 | size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8; | |
881990a2 | 411 | fprintf(stderr, " size = %luK\n", size*4); |
aba69144 | 412 | |
682c7051 NB |
413 | return 1; |
414 | } | |
415 | ||
416 | int check_raid(int fd, char *name) | |
417 | { | |
4b1ac34b | 418 | struct mdinfo info; |
682c7051 | 419 | time_t crtime; |
d078d77c | 420 | char *level; |
82d9eba6 | 421 | struct supertype *st = guess_super(fd); |
f9ce90ba | 422 | |
82d9eba6 | 423 | if (!st) return 0; |
3da92f27 | 424 | st->ss->load_super(st, fd, name); |
82d9eba6 NB |
425 | /* Looks like a raid array .. */ |
426 | fprintf(stderr, Name ": %s appears to be part of a raid array:\n", | |
427 | name); | |
3da92f27 NB |
428 | st->ss->getinfo_super(st, &info); |
429 | st->ss->free_super(st); | |
82d9eba6 | 430 | crtime = info.array.ctime; |
d078d77c NB |
431 | level = map_num(pers, info.array.level); |
432 | if (!level) level = "-unknown-"; | |
433 | fprintf(stderr, " level=%s devices=%d ctime=%s", | |
434 | level, info.array.raid_disks, ctime(&crtime)); | |
82d9eba6 | 435 | return 1; |
682c7051 NB |
436 | } |
437 | ||
682c7051 NB |
438 | int ask(char *mesg) |
439 | { | |
440 | char *add = ""; | |
441 | int i; | |
442 | for (i=0; i<5; i++) { | |
443 | char buf[100]; | |
444 | fprintf(stderr, "%s%s", mesg, add); | |
445 | fflush(stderr); | |
446 | if (fgets(buf, 100, stdin)==NULL) | |
447 | return 0; | |
448 | if (buf[0]=='y' || buf[0]=='Y') | |
449 | return 1; | |
450 | if (buf[0]=='n' || buf[0]=='N') | |
451 | return 0; | |
452 | add = "(y/n) "; | |
453 | } | |
454 | fprintf(stderr, Name ": assuming 'no'\n"); | |
455 | return 0; | |
456 | } | |
435d4ebb | 457 | #endif /* MDASSEMBLE */ |
682c7051 NB |
458 | |
459 | char *map_num(mapping_t *map, int num) | |
460 | { | |
461 | while (map->name) { | |
462 | if (map->num == num) | |
463 | return map->name; | |
464 | map++; | |
465 | } | |
466 | return NULL; | |
467 | } | |
468 | ||
469 | int map_name(mapping_t *map, char *name) | |
470 | { | |
471 | while (map->name) { | |
472 | if (strcmp(map->name, name)==0) | |
473 | return map->num; | |
474 | map++; | |
475 | } | |
98c6faba | 476 | return UnSet; |
682c7051 | 477 | } |
82b27616 | 478 | |
e5329c37 | 479 | |
8d80900b | 480 | int is_standard(char *dev, int *nump) |
e5329c37 NB |
481 | { |
482 | /* tests if dev is a "standard" md dev name. | |
483 | * i.e if the last component is "/dNN" or "/mdNN", | |
aba69144 | 484 | * where NN is a string of digits |
598f0d58 NB |
485 | * Returns 1 if a partitionable standard, |
486 | * -1 if non-partitonable, | |
487 | * 0 if not a standard name. | |
e5329c37 | 488 | */ |
8d80900b NB |
489 | char *d = strrchr(dev, '/'); |
490 | int type=0; | |
491 | int num; | |
492 | if (!d) | |
e5329c37 | 493 | return 0; |
8d80900b NB |
494 | if (strncmp(d, "/d",2)==0) |
495 | d += 2, type=1; /* /dev/md/dN{pM} */ | |
496 | else if (strncmp(d, "/md_d", 5)==0) | |
2b4ca8f0 | 497 | d += 5, type=1; /* /dev/md_dN{pM} */ |
8d80900b NB |
498 | else if (strncmp(d, "/md", 3)==0) |
499 | d += 3, type=-1; /* /dev/mdN */ | |
500 | else if (d-dev > 3 && strncmp(d-2, "md/", 3)==0) | |
5a6d1148 | 501 | d += 1, type=-1; /* /dev/md/N */ |
e5329c37 NB |
502 | else |
503 | return 0; | |
8d80900b | 504 | if (!*d) |
e5329c37 | 505 | return 0; |
8d80900b NB |
506 | num = atoi(d); |
507 | while (isdigit(*d)) | |
508 | d++; | |
509 | if (*d) | |
e5329c37 | 510 | return 0; |
8d80900b NB |
511 | if (nump) *nump = num; |
512 | ||
513 | return type; | |
e5329c37 NB |
514 | } |
515 | ||
516 | ||
82b27616 NB |
517 | /* |
518 | * convert a major/minor pair for a block device into a name in /dev, if possible. | |
519 | * On the first call, walk /dev collecting name. | |
520 | * Put them in a simple linked listfor now. | |
521 | */ | |
522 | struct devmap { | |
523 | int major, minor; | |
524 | char *name; | |
525 | struct devmap *next; | |
526 | } *devlist = NULL; | |
527 | int devlist_ready = 0; | |
528 | ||
82b27616 NB |
529 | int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s) |
530 | { | |
bed256c2 | 531 | struct stat st; |
bf68e9d9 | 532 | |
bed256c2 | 533 | if (S_ISLNK(stb->st_mode)) { |
bf68e9d9 DW |
534 | if (stat(name, &st) != 0) |
535 | return 0; | |
bed256c2 | 536 | stb = &st; |
82b27616 | 537 | } |
bed256c2 NB |
538 | |
539 | if ((stb->st_mode&S_IFMT)== S_IFBLK) { | |
540 | char *n = strdup(name); | |
541 | struct devmap *dm = malloc(sizeof(*dm)); | |
542 | if (strncmp(n, "/dev/./", 7)==0) | |
543 | strcpy(n+4, name+6); | |
544 | if (dm) { | |
545 | dm->major = major(stb->st_rdev); | |
546 | dm->minor = minor(stb->st_rdev); | |
547 | dm->name = n; | |
548 | dm->next = devlist; | |
549 | devlist = dm; | |
550 | } | |
551 | } | |
552 | return 0; | |
82b27616 NB |
553 | } |
554 | ||
45e878bb NB |
555 | #ifndef HAVE_NFTW |
556 | #ifdef HAVE_FTW | |
557 | int add_dev_1(const char *name, const struct stat *stb, int flag) | |
558 | { | |
559 | return add_dev(name, stb, flag, NULL); | |
560 | } | |
561 | int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags) | |
562 | { | |
563 | return ftw(path, add_dev_1, nopenfd); | |
564 | } | |
565 | #else | |
45e878bb NB |
566 | int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags) |
567 | { | |
568 | return 0; | |
569 | } | |
570 | #endif /* HAVE_FTW */ | |
571 | #endif /* HAVE_NFTW */ | |
572 | ||
dd0781e5 NB |
573 | /* |
574 | * Find a block device with the right major/minor number. | |
b79713f8 | 575 | * If we find multiple names, choose the shortest. |
70ef16db | 576 | * If we find a name in /dev/md/, we prefer that. |
b79713f8 | 577 | * This applies only to names for MD devices. |
dd0781e5 | 578 | */ |
16c6fa80 | 579 | char *map_dev(int major, int minor, int create) |
82b27616 | 580 | { |
dd0781e5 | 581 | struct devmap *p; |
70ef16db | 582 | char *regular = NULL, *preferred=NULL; |
e7bb5d23 | 583 | int did_check = 0; |
eed35d66 | 584 | |
e81cdd9f | 585 | if (major == 0 && minor == 0) |
eed35d66 | 586 | return NULL; |
e81cdd9f | 587 | |
e7bb5d23 | 588 | retry: |
dd0781e5 | 589 | if (!devlist_ready) { |
0a416ec3 NB |
590 | char *dev = "/dev"; |
591 | struct stat stb; | |
eed35d66 NB |
592 | while(devlist) { |
593 | struct devmap *d = devlist; | |
594 | devlist = d->next; | |
595 | free(d->name); | |
596 | free(d); | |
597 | } | |
0a416ec3 NB |
598 | if (lstat(dev, &stb)==0 && |
599 | S_ISLNK(stb.st_mode)) | |
600 | dev = "/dev/."; | |
601 | nftw(dev, add_dev, 10, FTW_PHYS); | |
dd0781e5 | 602 | devlist_ready=1; |
e7bb5d23 | 603 | did_check = 1; |
dd0781e5 | 604 | } |
82b27616 | 605 | |
dd0781e5 NB |
606 | for (p=devlist; p; p=p->next) |
607 | if (p->major == major && | |
608 | p->minor == minor) { | |
70ef16db N |
609 | if (strncmp(p->name, "/dev/md/",8) == 0) { |
610 | if (preferred == NULL || | |
611 | strlen(p->name) < strlen(preferred)) | |
612 | preferred = p->name; | |
b79713f8 | 613 | } else { |
70ef16db N |
614 | if (regular == NULL || |
615 | strlen(p->name) < strlen(regular)) | |
616 | regular = p->name; | |
b79713f8 | 617 | } |
dd0781e5 | 618 | } |
70ef16db | 619 | if (!regular && !preferred && !did_check) { |
e7bb5d23 NB |
620 | devlist_ready = 0; |
621 | goto retry; | |
622 | } | |
70ef16db | 623 | if (create && !regular && !preferred) { |
16c6fa80 | 624 | static char buf[30]; |
382245c3 | 625 | snprintf(buf, sizeof(buf), "%d:%d", major, minor); |
70ef16db | 626 | regular = buf; |
16c6fa80 NB |
627 | } |
628 | ||
70ef16db | 629 | return preferred ? preferred : regular; |
82b27616 NB |
630 | } |
631 | ||
4b1ac34b | 632 | unsigned long calc_csum(void *super, int bytes) |
82b27616 | 633 | { |
56eb10c0 | 634 | unsigned long long newcsum = 0; |
82b27616 | 635 | int i; |
4b1ac34b NB |
636 | unsigned int csum; |
637 | unsigned int *superc = (unsigned int*) super; | |
82b27616 | 638 | |
4b1ac34b | 639 | for(i=0; i<bytes/4; i++) |
82b27616 NB |
640 | newcsum+= superc[i]; |
641 | csum = (newcsum& 0xffffffff) + (newcsum>>32); | |
570c0542 | 642 | #ifdef __alpha__ |
aba69144 | 643 | /* The in-kernel checksum calculation is always 16bit on |
570c0542 NB |
644 | * the alpha, though it is 32 bit on i386... |
645 | * I wonder what it is elsewhere... (it uses and API in | |
646 | * a way that it shouldn't). | |
647 | */ | |
648 | csum = (csum & 0xffff) + (csum >> 16); | |
649 | csum = (csum & 0xffff) + (csum >> 16); | |
650 | #endif | |
82b27616 NB |
651 | return csum; |
652 | } | |
cd29a5c8 | 653 | |
435d4ebb | 654 | #ifndef MDASSEMBLE |
56eb10c0 | 655 | char *human_size(long long bytes) |
cd29a5c8 NB |
656 | { |
657 | static char buf[30]; | |
d5d3721e NB |
658 | |
659 | /* We convert bytes to either centi-M{ega,ibi}bytes or | |
660 | * centi-G{igi,ibi}bytes, with appropriate rounding, | |
661 | * and then print 1/100th of those as a decimal. | |
662 | * We allow upto 2048Megabytes before converting to | |
663 | * gigabytes, as that shows more precision and isn't | |
664 | * too large a number. | |
665 | * Terrabytes are not yet handled. | |
666 | */ | |
cd29a5c8 | 667 | |
56eb10c0 | 668 | if (bytes < 5000*1024) |
cd29a5c8 | 669 | buf[0]=0; |
d5d3721e NB |
670 | else if (bytes < 2*1024LL*1024LL*1024LL) { |
671 | long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2; | |
672 | long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2; | |
8f23b0b3 | 673 | snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)", |
d5d3721e NB |
674 | cMiB/100 , cMiB % 100, |
675 | cMB/100, cMB % 100); | |
676 | } else { | |
677 | long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2; | |
678 | long cGB = (bytes / (1000000000LL/200LL ) +1) /2; | |
8f23b0b3 | 679 | snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)", |
d5d3721e NB |
680 | cGiB/100 , cGiB % 100, |
681 | cGB/100, cGB % 100); | |
682 | } | |
cd29a5c8 NB |
683 | return buf; |
684 | } | |
e0d19036 NB |
685 | |
686 | char *human_size_brief(long long bytes) | |
687 | { | |
688 | static char buf[30]; | |
e0d19036 NB |
689 | |
690 | if (bytes < 5000*1024) | |
8f23b0b3 | 691 | snprintf(buf, sizeof(buf), "%ld.%02ldKiB", |
bd526cee | 692 | (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024) |
e0d19036 NB |
693 | ); |
694 | else if (bytes < 2*1024LL*1024LL*1024LL) | |
8f23b0b3 | 695 | snprintf(buf, sizeof(buf), "%ld.%02ldMiB", |
e0d19036 | 696 | (long)(bytes>>20), |
bd526cee | 697 | (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100) |
e0d19036 NB |
698 | ); |
699 | else | |
8f23b0b3 | 700 | snprintf(buf, sizeof(buf), "%ld.%02ldGiB", |
e0d19036 | 701 | (long)(bytes>>30), |
bd526cee | 702 | (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100) |
e0d19036 NB |
703 | ); |
704 | return buf; | |
705 | } | |
e4965ef8 N |
706 | |
707 | void print_r10_layout(int layout) | |
708 | { | |
709 | int near = layout & 255; | |
710 | int far = (layout >> 8) & 255; | |
711 | int offset = (layout&0x10000); | |
712 | char *sep = ""; | |
713 | ||
714 | if (near != 1) { | |
715 | printf("%s near=%d", sep, near); | |
716 | sep = ","; | |
717 | } | |
718 | if (far != 1) | |
719 | printf("%s %s=%d", sep, offset?"offset":"far", far); | |
720 | if (near*far == 1) | |
721 | printf("NO REDUNDANCY"); | |
722 | } | |
435d4ebb | 723 | #endif |
e0d19036 | 724 | |
5f8097be NB |
725 | unsigned long long calc_array_size(int level, int raid_disks, int layout, |
726 | int chunksize, unsigned long long devsize) | |
727 | { | |
728 | int data_disks = 0; | |
729 | switch (level) { | |
730 | case 0: data_disks = raid_disks; break; | |
731 | case 1: data_disks = 1; break; | |
732 | case 4: | |
733 | case 5: data_disks = raid_disks - 1; break; | |
734 | case 6: data_disks = raid_disks - 2; break; | |
735 | case 10: data_disks = raid_disks / (layout & 255) / ((layout>>8)&255); | |
736 | break; | |
737 | } | |
738 | devsize &= ~(unsigned long long)((chunksize>>9)-1); | |
739 | return data_disks * devsize; | |
740 | } | |
741 | ||
dd0781e5 | 742 | int get_mdp_major(void) |
98c6faba | 743 | { |
dd0781e5 NB |
744 | static int mdp_major = -1; |
745 | FILE *fl; | |
98c6faba NB |
746 | char *w; |
747 | int have_block = 0; | |
748 | int have_devices = 0; | |
749 | int last_num = -1; | |
dd0781e5 NB |
750 | |
751 | if (mdp_major != -1) | |
752 | return mdp_major; | |
753 | fl = fopen("/proc/devices", "r"); | |
98c6faba | 754 | if (!fl) |
dd0781e5 | 755 | return -1; |
98c6faba NB |
756 | while ((w = conf_word(fl, 1))) { |
757 | if (have_block && strcmp(w, "devices:")==0) | |
758 | have_devices = 1; | |
759 | have_block = (strcmp(w, "Block")==0); | |
760 | if (isdigit(w[0])) | |
761 | last_num = atoi(w); | |
762 | if (have_devices && strcmp(w, "mdp")==0) | |
763 | mdp_major = last_num; | |
764 | free(w); | |
765 | } | |
766 | fclose(fl); | |
dd0781e5 | 767 | return mdp_major; |
98c6faba NB |
768 | } |
769 | ||
0e600426 | 770 | #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) |
e0d19036 NB |
771 | char *get_md_name(int dev) |
772 | { | |
773 | /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */ | |
98c6faba | 774 | /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */ |
e0d19036 NB |
775 | static char devname[50]; |
776 | struct stat stb; | |
98c6faba | 777 | dev_t rdev; |
dd0781e5 | 778 | char *dn; |
98c6faba NB |
779 | |
780 | if (dev < 0) { | |
dd0781e5 NB |
781 | int mdp = get_mdp_major(); |
782 | if (mdp < 0) return NULL; | |
0df46c2a | 783 | rdev = makedev(mdp, (-1-dev)<<6); |
8f23b0b3 | 784 | snprintf(devname, sizeof(devname), "/dev/md/d%d", -1-dev); |
98c6faba NB |
785 | if (stat(devname, &stb) == 0 |
786 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
787 | && (stb.st_rdev == rdev)) | |
788 | return devname; | |
789 | } else { | |
0df46c2a | 790 | rdev = makedev(MD_MAJOR, dev); |
8f23b0b3 | 791 | snprintf(devname, sizeof(devname), "/dev/md%d", dev); |
98c6faba NB |
792 | if (stat(devname, &stb) == 0 |
793 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
794 | && (stb.st_rdev == rdev)) | |
795 | return devname; | |
796 | ||
8f23b0b3 | 797 | snprintf(devname, sizeof(devname), "/dev/md/%d", dev); |
98c6faba NB |
798 | if (stat(devname, &stb) == 0 |
799 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
800 | && (stb.st_rdev == rdev)) | |
801 | return devname; | |
802 | } | |
16c6fa80 | 803 | dn = map_dev(major(rdev), minor(rdev), 0); |
dd0781e5 NB |
804 | if (dn) |
805 | return dn; | |
8f23b0b3 | 806 | snprintf(devname, sizeof(devname), "/dev/.tmp.md%d", dev); |
e0d19036 | 807 | if (mknod(devname, S_IFBLK | 0600, rdev) == -1) |
dd0781e5 NB |
808 | if (errno != EEXIST) |
809 | return NULL; | |
e0d19036 NB |
810 | |
811 | if (stat(devname, &stb) == 0 | |
812 | && (S_IFMT&stb.st_mode) == S_IFBLK | |
813 | && (stb.st_rdev == rdev)) | |
814 | return devname; | |
815 | unlink(devname); | |
816 | return NULL; | |
817 | } | |
818 | ||
819 | void put_md_name(char *name) | |
820 | { | |
821 | if (strncmp(name, "/dev/.tmp.md", 12)==0) | |
822 | unlink(name); | |
823 | } | |
ea24acd0 | 824 | |
ea24acd0 NB |
825 | int find_free_devnum(int use_partitions) |
826 | { | |
827 | int devnum; | |
828 | for (devnum = 127; devnum != 128; | |
a56fb7ec | 829 | devnum = devnum ? devnum-1 : (1<<20)-1) { |
ea24acd0 NB |
830 | char *dn; |
831 | int _devnum; | |
832 | ||
833 | _devnum = use_partitions ? (-1-devnum) : devnum; | |
834 | if (mddev_busy(_devnum)) | |
835 | continue; | |
836 | /* make sure it is new to /dev too, at least as a | |
837 | * non-standard */ | |
838 | dn = map_dev(dev2major(_devnum), dev2minor(_devnum), 0); | |
839 | if (dn && ! is_standard(dn, NULL)) | |
840 | continue; | |
841 | break; | |
842 | } | |
843 | if (devnum == 128) | |
844 | return NoMdDev; | |
845 | return use_partitions ? (-1-devnum) : devnum; | |
846 | } | |
435d4ebb | 847 | #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */ |
f9ce90ba | 848 | |
8b0dabea NB |
849 | int dev_open(char *dev, int flags) |
850 | { | |
851 | /* like 'open', but if 'dev' matches %d:%d, create a temp | |
852 | * block device and open that | |
853 | */ | |
854 | char *e; | |
855 | int fd = -1; | |
856 | char devname[32]; | |
e81cdd9f | 857 | int major; |
8b0dabea | 858 | int minor; |
e81cdd9f NB |
859 | |
860 | if (!dev) return -1; | |
861 | ||
862 | major = strtoul(dev, &e, 0); | |
8b0dabea NB |
863 | if (e > dev && *e == ':' && e[1] && |
864 | (minor = strtoul(e+1, &e, 0)) >= 0 && | |
865 | *e == 0) { | |
8c210183 NB |
866 | snprintf(devname, sizeof(devname), "/dev/.tmp.md.%d:%d:%d", |
867 | (int)getpid(), major, minor); | |
8b0dabea | 868 | if (mknod(devname, S_IFBLK|0600, makedev(major, minor))==0) { |
6416d527 | 869 | fd = open(devname, flags|O_DIRECT); |
8b0dabea NB |
870 | unlink(devname); |
871 | } | |
872 | } else | |
6416d527 | 873 | fd = open(dev, flags|O_DIRECT); |
8b0dabea NB |
874 | return fd; |
875 | } | |
f9ce90ba | 876 | |
e8a70c89 N |
877 | int open_dev(int devnum) |
878 | { | |
879 | char buf[20]; | |
880 | ||
881 | sprintf(buf, "%d:%d", dev2major(devnum), dev2minor(devnum)); | |
882 | return dev_open(buf, O_RDWR); | |
883 | } | |
884 | ||
a931db9e NB |
885 | int open_dev_excl(int devnum) |
886 | { | |
887 | char buf[20]; | |
888 | int i; | |
889 | ||
890 | sprintf(buf, "%d:%d", dev2major(devnum), dev2minor(devnum)); | |
891 | for (i=0 ; i<25 ; i++) { | |
892 | int fd = dev_open(buf, O_RDWR|O_EXCL); | |
893 | if (fd >= 0) | |
894 | return fd; | |
895 | if (errno != EBUSY) | |
896 | return fd; | |
897 | usleep(200000); | |
898 | } | |
899 | return -1; | |
900 | } | |
901 | ||
9008ed1c N |
902 | int same_dev(char *one, char *two) |
903 | { | |
904 | struct stat st1, st2; | |
905 | if (stat(one, &st1) != 0) | |
906 | return 0; | |
907 | if (stat(two, &st2) != 0) | |
908 | return 0; | |
909 | if ((st1.st_mode & S_IFMT) != S_IFBLK) | |
910 | return 0; | |
911 | if ((st2.st_mode & S_IFMT) != S_IFBLK) | |
912 | return 0; | |
913 | return st1.st_rdev == st2.st_rdev; | |
914 | } | |
915 | ||
a7c6e3fb | 916 | void wait_for(char *dev, int fd) |
a714580e N |
917 | { |
918 | int i; | |
a7c6e3fb N |
919 | struct stat stb_want; |
920 | ||
921 | if (fstat(fd, &stb_want) != 0 || | |
922 | (stb_want.st_mode & S_IFMT) != S_IFBLK) | |
923 | return; | |
a714580e N |
924 | |
925 | for (i=0 ; i<25 ; i++) { | |
926 | struct stat stb; | |
a7c6e3fb N |
927 | if (stat(dev, &stb) == 0 && |
928 | (stb.st_mode & S_IFMT) == S_IFBLK && | |
929 | (stb.st_rdev == stb_want.st_rdev)) | |
a714580e N |
930 | return; |
931 | usleep(200000); | |
932 | } | |
436305c6 DW |
933 | if (i == 25) |
934 | dprintf("%s: timeout waiting for %s\n", __func__, dev); | |
a714580e N |
935 | } |
936 | ||
cdddbdbc | 937 | struct superswitch *superlist[] = { &super0, &super1, &super_ddf, &super_imsm, NULL }; |
f9ce90ba | 938 | |
ea24acd0 | 939 | #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) |
f7dd881f | 940 | |
1686dc25 | 941 | struct supertype *super_by_fd(int fd) |
f9ce90ba | 942 | { |
1686dc25 NB |
943 | mdu_array_info_t array; |
944 | int vers; | |
945 | int minor; | |
946 | struct supertype *st = NULL; | |
7e0f6979 | 947 | struct mdinfo *sra; |
142cb9e1 | 948 | char *verstr; |
1686dc25 NB |
949 | char version[20]; |
950 | int i; | |
f7e7067b | 951 | char *subarray = NULL; |
1686dc25 NB |
952 | |
953 | sra = sysfs_read(fd, 0, GET_VERSION); | |
954 | ||
955 | if (sra) { | |
7e0f6979 NB |
956 | vers = sra->array.major_version; |
957 | minor = sra->array.minor_version; | |
142cb9e1 | 958 | verstr = sra->text_version; |
1686dc25 NB |
959 | } else { |
960 | if (ioctl(fd, GET_ARRAY_INFO, &array)) | |
961 | array.major_version = array.minor_version = 0; | |
962 | vers = array.major_version; | |
963 | minor = array.minor_version; | |
142cb9e1 | 964 | verstr = ""; |
6fbba4c9 | 965 | } |
82d9eba6 | 966 | |
1686dc25 NB |
967 | if (vers != -1) { |
968 | sprintf(version, "%d.%d", vers, minor); | |
969 | verstr = version; | |
6fbba4c9 | 970 | } |
3c558363 | 971 | if (minor == -2 && is_subarray(verstr)) { |
f7e7067b NB |
972 | char *dev = verstr+1; |
973 | subarray = strchr(dev, '/'); | |
974 | int devnum; | |
975 | if (subarray) | |
976 | *subarray++ = '\0'; | |
77472ff8 | 977 | devnum = devname2devnum(dev); |
f7e7067b NB |
978 | subarray = strdup(subarray); |
979 | if (sra) | |
980 | sysfs_free(sra); | |
981 | sra = sysfs_read(-1, devnum, GET_VERSION); | |
603f24a0 N |
982 | if (sra && sra->text_version[0]) |
983 | verstr = sra->text_version; | |
984 | else | |
985 | verstr = "-no-metadata-"; | |
f7e7067b NB |
986 | } |
987 | ||
988 | for (i = 0; st == NULL && superlist[i] ; i++) | |
989 | st = superlist[i]->match_metadata_desc(verstr); | |
1686dc25 NB |
990 | |
991 | if (sra) | |
992 | sysfs_free(sra); | |
f7e7067b | 993 | if (st) { |
3b0896f8 | 994 | st->sb = NULL; |
f7e7067b NB |
995 | if (subarray) { |
996 | strncpy(st->subarray, subarray, 32); | |
997 | st->subarray[31] = 0; | |
998 | free(subarray); | |
999 | } else | |
1000 | st->subarray[0] = 0; | |
1001 | } | |
82d9eba6 | 1002 | return st; |
f9ce90ba | 1003 | } |
ea24acd0 NB |
1004 | #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */ |
1005 | ||
f9ce90ba | 1006 | |
159c3a1a | 1007 | struct supertype *dup_super(struct supertype *orig) |
3da92f27 | 1008 | { |
159c3a1a | 1009 | struct supertype *st; |
1686dc25 | 1010 | |
d2ca6449 NB |
1011 | if (!orig) |
1012 | return orig; | |
159c3a1a | 1013 | st = malloc(sizeof(*st)); |
3da92f27 NB |
1014 | if (!st) |
1015 | return st; | |
ef609477 | 1016 | memset(st, 0, sizeof(*st)); |
159c3a1a NB |
1017 | st->ss = orig->ss; |
1018 | st->max_devs = orig->max_devs; | |
1019 | st->minor_version = orig->minor_version; | |
f7e7067b | 1020 | strcpy(st->subarray, orig->subarray); |
159c3a1a NB |
1021 | st->sb = NULL; |
1022 | st->info = NULL; | |
1023 | return st; | |
3da92f27 NB |
1024 | } |
1025 | ||
82d9eba6 | 1026 | struct supertype *guess_super(int fd) |
f9ce90ba NB |
1027 | { |
1028 | /* try each load_super to find the best match, | |
1029 | * and return the best superswitch | |
1030 | */ | |
82d9eba6 NB |
1031 | struct superswitch *ss; |
1032 | struct supertype *st; | |
570c0542 NB |
1033 | unsigned long besttime = 0; |
1034 | int bestsuper = -1; | |
f9ce90ba NB |
1035 | int i; |
1036 | ||
82d9eba6 | 1037 | st = malloc(sizeof(*st)); |
f9ce90ba NB |
1038 | for (i=0 ; superlist[i]; i++) { |
1039 | int rv; | |
1040 | ss = superlist[i]; | |
ef609477 | 1041 | memset(st, 0, sizeof(*st)); |
3da92f27 | 1042 | rv = ss->load_super(st, fd, NULL); |
570c0542 NB |
1043 | if (rv == 0) { |
1044 | struct mdinfo info; | |
3da92f27 | 1045 | st->ss->getinfo_super(st, &info); |
570c0542 NB |
1046 | if (bestsuper == -1 || |
1047 | besttime < info.array.ctime) { | |
1048 | bestsuper = i; | |
1049 | besttime = info.array.ctime; | |
570c0542 | 1050 | } |
3da92f27 | 1051 | ss->free_super(st); |
570c0542 NB |
1052 | } |
1053 | } | |
1054 | if (bestsuper != -1) { | |
1055 | int rv; | |
ef609477 | 1056 | memset(st, 0, sizeof(*st)); |
3da92f27 | 1057 | rv = superlist[bestsuper]->load_super(st, fd, NULL); |
f9ce90ba | 1058 | if (rv == 0) { |
5e747af2 | 1059 | superlist[bestsuper]->free_super(st); |
82d9eba6 | 1060 | return st; |
f9ce90ba NB |
1061 | } |
1062 | } | |
570c0542 | 1063 | free(st); |
f9ce90ba NB |
1064 | return NULL; |
1065 | } | |
fe6729fa | 1066 | |
beae1dfe NB |
1067 | /* Return size of device in bytes */ |
1068 | int get_dev_size(int fd, char *dname, unsigned long long *sizep) | |
1069 | { | |
1070 | unsigned long long ldsize; | |
c2c9bb6f NB |
1071 | struct stat st; |
1072 | ||
1073 | if (fstat(fd, &st) != -1 && S_ISREG(st.st_mode)) | |
1074 | ldsize = (unsigned long long)st.st_size; | |
1075 | else | |
beae1dfe NB |
1076 | #ifdef BLKGETSIZE64 |
1077 | if (ioctl(fd, BLKGETSIZE64, &ldsize) != 0) | |
1078 | #endif | |
1079 | { | |
1080 | unsigned long dsize; | |
1081 | if (ioctl(fd, BLKGETSIZE, &dsize) == 0) { | |
1082 | ldsize = dsize; | |
1083 | ldsize <<= 9; | |
1084 | } else { | |
1085 | if (dname) | |
1086 | fprintf(stderr, Name ": Cannot get size of %s: %s\b", | |
1087 | dname, strerror(errno)); | |
1088 | return 0; | |
1089 | } | |
1090 | } | |
1091 | *sizep = ldsize; | |
1092 | return 1; | |
1093 | } | |
8fac0577 | 1094 | |
8382f19b NB |
1095 | void get_one_disk(int mdfd, mdu_array_info_t *ainf, mdu_disk_info_t *disk) |
1096 | { | |
1097 | int d; | |
1098 | ioctl(mdfd, GET_ARRAY_INFO, ainf); | |
1099 | for (d = 0 ; d < ainf->raid_disks + ainf->nr_disks ; d++) | |
1100 | if (ioctl(mdfd, GET_DISK_INFO, disk) == 0) | |
1101 | return; | |
1102 | } | |
63152c1b | 1103 | |
a322f70c DW |
1104 | int open_container(int fd) |
1105 | { | |
1106 | /* 'fd' is a block device. Find out if it is in use | |
1107 | * by a container, and return an open fd on that container. | |
1108 | */ | |
1109 | char path[256]; | |
1110 | char *e; | |
1111 | DIR *dir; | |
1112 | struct dirent *de; | |
1113 | int dfd, n; | |
1114 | char buf[200]; | |
1115 | int major, minor; | |
1116 | struct stat st; | |
1117 | ||
1118 | if (fstat(fd, &st) != 0) | |
1119 | return -1; | |
1120 | sprintf(path, "/sys/dev/block/%d:%d/holders", | |
1121 | (int)major(st.st_rdev), (int)minor(st.st_rdev)); | |
1122 | e = path + strlen(path); | |
1123 | ||
1124 | dir = opendir(path); | |
1125 | if (!dir) | |
1126 | return -1; | |
1127 | while ((de = readdir(dir))) { | |
1128 | if (de->d_ino == 0) | |
1129 | continue; | |
1130 | if (de->d_name[0] == '.') | |
1131 | continue; | |
1132 | sprintf(e, "/%s/dev", de->d_name); | |
1133 | dfd = open(path, O_RDONLY); | |
1134 | if (dfd < 0) | |
1135 | continue; | |
1136 | n = read(dfd, buf, sizeof(buf)); | |
1137 | close(dfd); | |
1138 | if (n <= 0 || n >= sizeof(buf)) | |
1139 | continue; | |
1140 | buf[n] = 0; | |
1141 | if (sscanf(buf, "%d:%d", &major, &minor) != 2) | |
1142 | continue; | |
1143 | sprintf(buf, "%d:%d", major, minor); | |
1144 | dfd = dev_open(buf, O_RDONLY); | |
1145 | if (dfd >= 0) { | |
1146 | closedir(dir); | |
1147 | return dfd; | |
1148 | } | |
1149 | } | |
355726fa | 1150 | closedir(dir); |
a322f70c DW |
1151 | return -1; |
1152 | } | |
1153 | ||
7801ac20 N |
1154 | int add_disk(int mdfd, struct supertype *st, |
1155 | struct mdinfo *sra, struct mdinfo *info) | |
1156 | { | |
1157 | /* Add a device to an array, in one of 2 ways. */ | |
1158 | int rv; | |
1159 | #ifndef MDASSEMBLE | |
1160 | if (st->ss->external) { | |
462906cd N |
1161 | rv = sysfs_add_disk(sra, info, |
1162 | info->disk.state & (1<<MD_DISK_SYNC)); | |
7801ac20 N |
1163 | if (! rv) { |
1164 | struct mdinfo *sd2; | |
f35f2525 N |
1165 | for (sd2 = sra->devs; sd2; sd2=sd2->next) |
1166 | if (sd2 == info) | |
1167 | break; | |
1168 | if (sd2 == NULL) { | |
1169 | sd2 = malloc(sizeof(*sd2)); | |
1170 | *sd2 = *info; | |
1171 | sd2->next = sra->devs; | |
1172 | sra->devs = sd2; | |
1173 | } | |
7801ac20 N |
1174 | } |
1175 | } else | |
1176 | #endif | |
1177 | rv = ioctl(mdfd, ADD_NEW_DISK, &info->disk); | |
1178 | return rv; | |
1179 | } | |
1180 | ||
f35f2525 N |
1181 | int set_array_info(int mdfd, struct supertype *st, struct mdinfo *info) |
1182 | { | |
1183 | /* Initialise kernel's knowledge of array. | |
1184 | * This varies between externally managed arrays | |
1185 | * and older kernels | |
1186 | */ | |
1187 | int vers = md_get_version(mdfd); | |
1188 | int rv; | |
1189 | ||
1190 | #ifndef MDASSEMBLE | |
1191 | if (st->ss->external) | |
1192 | rv = sysfs_set_array(info, vers); | |
1193 | else | |
1194 | #endif | |
1195 | if ((vers % 100) >= 1) { /* can use different versions */ | |
1196 | mdu_array_info_t inf; | |
1197 | memset(&inf, 0, sizeof(inf)); | |
1198 | inf.major_version = info->array.major_version; | |
1199 | inf.minor_version = info->array.minor_version; | |
1200 | rv = ioctl(mdfd, SET_ARRAY_INFO, &inf); | |
1201 | } else | |
1202 | rv = ioctl(mdfd, SET_ARRAY_INFO, NULL); | |
1203 | return rv; | |
1204 | } | |
1205 | ||
2f6079dc NB |
1206 | char *devnum2devname(int num) |
1207 | { | |
1208 | char name[100]; | |
1209 | if (num > 0) | |
1210 | sprintf(name, "md%d", num); | |
1211 | else | |
1212 | sprintf(name, "md_d%d", -1-num); | |
1213 | return strdup(name); | |
1214 | } | |
1215 | ||
77472ff8 NB |
1216 | int devname2devnum(char *name) |
1217 | { | |
1218 | char *ep; | |
1219 | int num; | |
1220 | if (strncmp(name, "md_d", 4)==0) | |
1221 | num = -1-strtoul(name+4, &ep, 10); | |
1222 | else | |
1223 | num = strtoul(name+2, &ep, 10); | |
1224 | return num; | |
1225 | } | |
1226 | ||
c94709e8 | 1227 | int stat2devnum(struct stat *st) |
2f6079dc | 1228 | { |
d7ab966b N |
1229 | char path[30]; |
1230 | char link[200]; | |
1231 | char *cp; | |
1232 | int n; | |
1233 | ||
c94709e8 DW |
1234 | if ((S_IFMT & st->st_mode) == S_IFBLK) { |
1235 | if (major(st->st_rdev) == MD_MAJOR) | |
1236 | return minor(st->st_rdev); | |
d7ab966b N |
1237 | else if (major(st->st_rdev) == get_mdp_major()) |
1238 | return -1- (minor(st->st_rdev)>>MdpMinorShift); | |
1239 | ||
1240 | /* must be an extended-minor partition. Look at the | |
1241 | * /sys/dev/block/%d:%d link which must look like | |
1242 | * ../../block/mdXXX/mdXXXpYY | |
1243 | */ | |
1244 | sprintf(path, "/sys/dev/block/%d:%d", major(st->st_rdev), | |
1245 | minor(st->st_rdev)); | |
1246 | n = readlink(path, link, sizeof(link)-1); | |
1247 | if (n <= 0) | |
1248 | return NoMdDev; | |
1249 | link[n] = 0; | |
1250 | cp = strrchr(link, '/'); | |
1251 | if (cp) *cp = 0; | |
1252 | cp = strchr(link, '/'); | |
1253 | if (cp && strncmp(cp, "/md", 3) == 0) | |
1254 | return devname2devnum(cp+1); | |
2f6079dc | 1255 | } |
d7ab966b | 1256 | return NoMdDev; |
c94709e8 DW |
1257 | |
1258 | } | |
1259 | ||
1260 | int fd2devnum(int fd) | |
1261 | { | |
1262 | struct stat stb; | |
1263 | if (fstat(fd, &stb) == 0) | |
1264 | return stat2devnum(&stb); | |
d7ab966b | 1265 | return NoMdDev; |
2f6079dc NB |
1266 | } |
1267 | ||
a931db9e NB |
1268 | int mdmon_running(int devnum) |
1269 | { | |
1270 | char path[100]; | |
1271 | char pid[10]; | |
1272 | int fd; | |
1273 | int n; | |
1274 | sprintf(path, "/var/run/mdadm/%s.pid", devnum2devname(devnum)); | |
1275 | fd = open(path, O_RDONLY, 0); | |
1276 | ||
1277 | if (fd < 0) | |
1278 | return 0; | |
1279 | n = read(fd, pid, 9); | |
1280 | close(fd); | |
1281 | if (n <= 0) | |
1282 | return 0; | |
1283 | if (kill(atoi(pid), 0) == 0) | |
1284 | return 1; | |
1285 | return 0; | |
1286 | } | |
1287 | ||
1288 | int signal_mdmon(int devnum) | |
1289 | { | |
1290 | char path[100]; | |
1291 | char pid[10]; | |
1292 | int fd; | |
1293 | int n; | |
1294 | sprintf(path, "/var/run/mdadm/%s.pid", devnum2devname(devnum)); | |
1295 | fd = open(path, O_RDONLY, 0); | |
1296 | ||
1297 | if (fd < 0) | |
1298 | return 0; | |
1299 | n = read(fd, pid, 9); | |
1300 | close(fd); | |
1301 | if (n <= 0) | |
1302 | return 0; | |
1303 | if (kill(atoi(pid), SIGUSR1) == 0) | |
1304 | return 1; | |
1305 | return 0; | |
1306 | } | |
1307 | ||
8850ee3e N |
1308 | int start_mdmon(int devnum) |
1309 | { | |
1310 | int i; | |
44d2e365 | 1311 | int len; |
9fe32043 N |
1312 | pid_t pid; |
1313 | int status; | |
44d2e365 N |
1314 | char pathbuf[1024]; |
1315 | char *paths[4] = { | |
1316 | pathbuf, | |
1317 | "/sbin/mdmon", | |
1318 | "mdmon", | |
1319 | NULL | |
1320 | }; | |
8850ee3e | 1321 | |
40ebbb9c | 1322 | if (check_env("MDADM_NO_MDMON")) |
8850ee3e N |
1323 | return 0; |
1324 | ||
44d2e365 N |
1325 | len = readlink("/proc/self/exe", pathbuf, sizeof(pathbuf)); |
1326 | if (len > 0) { | |
1327 | char *sl; | |
1328 | pathbuf[len] = 0; | |
1329 | sl = strrchr(pathbuf, '/'); | |
1330 | if (sl) | |
1331 | sl++; | |
1332 | else | |
1333 | sl = pathbuf; | |
1334 | strcpy(sl, "mdmon"); | |
1335 | } else | |
1336 | pathbuf[0] = '\0'; | |
1337 | ||
8850ee3e N |
1338 | switch(fork()) { |
1339 | case 0: | |
1340 | /* FIXME yuk. CLOSE_EXEC?? */ | |
1341 | for (i=3; i < 100; i++) | |
1342 | close(i); | |
44d2e365 N |
1343 | for (i=0; paths[i]; i++) |
1344 | if (paths[i][0]) | |
1345 | execl(paths[i], "mdmon", | |
e8a70c89 N |
1346 | devnum2devname(devnum), |
1347 | NULL); | |
8850ee3e N |
1348 | exit(1); |
1349 | case -1: fprintf(stderr, Name ": cannot run mdmon. " | |
1350 | "Array remains readonly\n"); | |
1351 | return -1; | |
9fe32043 N |
1352 | default: /* parent - good */ |
1353 | pid = wait(&status); | |
1354 | if (pid < 0 || status != 0) | |
1355 | return -1; | |
8850ee3e N |
1356 | } |
1357 | return 0; | |
1358 | } | |
1359 | ||
40ebbb9c | 1360 | int check_env(char *name) |
5dcfcb71 | 1361 | { |
40ebbb9c | 1362 | char *val = getenv(name); |
5dcfcb71 DW |
1363 | |
1364 | if (val && atoi(val) == 1) | |
1365 | return 1; | |
1366 | ||
1367 | return 0; | |
1368 | } | |
1369 | ||
148acb7b DW |
1370 | __u32 random32(void) |
1371 | { | |
1372 | __u32 rv; | |
1373 | int rfd = open("/dev/urandom", O_RDONLY); | |
1374 | if (rfd < 0 || read(rfd, &rv, 4) != 4) | |
1375 | rv = random(); | |
1376 | if (rfd >= 0) | |
1377 | close(rfd); | |
1378 | return rv; | |
1379 | } | |
1380 | ||
0e600426 | 1381 | #ifndef MDASSEMBLE |
edd8d13c NB |
1382 | int flush_metadata_updates(struct supertype *st) |
1383 | { | |
1384 | int sfd; | |
1385 | if (!st->updates) { | |
1386 | st->update_tail = NULL; | |
1387 | return -1; | |
1388 | } | |
1389 | ||
1390 | sfd = connect_monitor(devnum2devname(st->container_dev)); | |
1391 | if (sfd < 0) | |
1392 | return -1; | |
1393 | ||
1394 | while (st->updates) { | |
1395 | struct metadata_update *mu = st->updates; | |
1396 | st->updates = mu->next; | |
1397 | ||
1398 | send_message(sfd, mu, 0); | |
1399 | wait_reply(sfd, 0); | |
1400 | free(mu->buf); | |
1401 | free(mu); | |
1402 | } | |
1403 | ack(sfd, 0); | |
1404 | wait_reply(sfd, 0); | |
1405 | close(sfd); | |
1406 | st->update_tail = NULL; | |
1407 | return 0; | |
1408 | } | |
1409 | ||
1410 | void append_metadata_update(struct supertype *st, void *buf, int len) | |
1411 | { | |
1412 | ||
1413 | struct metadata_update *mu = malloc(sizeof(*mu)); | |
1414 | ||
1415 | mu->buf = buf; | |
1416 | mu->len = len; | |
1417 | mu->space = NULL; | |
1418 | mu->next = NULL; | |
1419 | *st->update_tail = mu; | |
1420 | st->update_tail = &mu->next; | |
1421 | } | |
0e600426 | 1422 | #endif /* MDASSEMBLE */ |
a931db9e | 1423 | |
fe6729fa NB |
1424 | #ifdef __TINYC__ |
1425 | /* tinyc doesn't optimize this check in ioctl.h out ... */ | |
1426 | unsigned int __invalid_size_argument_for_IOC = 0; | |
1427 | #endif | |
1428 |