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