]> git.ipfire.org Git - thirdparty/mdadm.git/blob - util.c
mdadm.8: correct typo: pairty
[thirdparty/mdadm.git] / util.c
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 /* partition table structures so we can check metadata position
69 * against the end of the last partition.
70 * Only handle MBR ant GPT partition tables.
71 */
72 struct MBR_part_record {
73 __u8 bootable;
74 __u8 first_head;
75 __u8 first_sector;
76 __u8 first_cyl;
77 __u8 part_type;
78 __u8 last_head;
79 __u8 last_sector;
80 __u8 last_cyl;
81 __u32 first_sect_lba;
82 __u32 blocks_num;
83 };
84
85 struct GPT_part_entry {
86 unsigned char type_guid[16];
87 unsigned char partition_guid[16];
88 unsigned char starting_lba[8];
89 unsigned char ending_lba[8];
90 unsigned char attr_bits[8];
91 unsigned char name[72];
92 };
93
94 /* MBR/GPT magic numbers */
95 #define MBR_SIGNATURE_MAGIC __cpu_to_le16(0xAA55)
96 #define GPT_SIGNATURE_MAGIC __cpu_to_le64(0x5452415020494645ULL)
97
98 #define MBR_SIGNATURE_OFFSET 510
99 #define MBR_PARTITION_TABLE_OFFSET 446
100 #define MBR_PARTITIONS 4
101 #define MBR_GPT_PARTITION_TYPE 0xEE
102 #define GPT_ALL_PARTITIONS_OFFSET 80
103 #define GPT_ENTRY_SIZE_OFFSET 84
104
105 /*
106 * Parse a 128 bit uuid in 4 integers
107 * format is 32 hexx nibbles with options :.<space> separator
108 * If not exactly 32 hex digits are found, return 0
109 * else return 1
110 */
111 int parse_uuid(char *str, int uuid[4])
112 {
113 int hit = 0; /* number of Hex digIT */
114 int i;
115 char c;
116 for (i=0; i<4; i++) uuid[i]=0;
117
118 while ((c= *str++)) {
119 int n;
120 if (c>='0' && c<='9')
121 n = c-'0';
122 else if (c>='a' && c <= 'f')
123 n = 10 + c - 'a';
124 else if (c>='A' && c <= 'F')
125 n = 10 + c - 'A';
126 else if (strchr(":. -", c))
127 continue;
128 else return 0;
129
130 if (hit<32) {
131 uuid[hit/8] <<= 4;
132 uuid[hit/8] += n;
133 }
134 hit++;
135 }
136 if (hit == 32)
137 return 1;
138 return 0;
139 }
140
141
142 /*
143 * Get the md version number.
144 * We use the RAID_VERSION ioctl if it is supported
145 * If not, but we have a block device with major '9', we assume
146 * 0.36.0
147 *
148 * Return version number as 24 but number - assume version parts
149 * always < 255
150 */
151
152 int md_get_version(int fd)
153 {
154 struct stat stb;
155 mdu_version_t vers;
156
157 if (fstat(fd, &stb)<0)
158 return -1;
159 if ((S_IFMT&stb.st_mode) != S_IFBLK)
160 return -1;
161
162 if (ioctl(fd, RAID_VERSION, &vers) == 0)
163 return (vers.major*10000) + (vers.minor*100) + vers.patchlevel;
164 if (errno == EACCES)
165 return -1;
166 if (major(stb.st_rdev) == MD_MAJOR)
167 return (3600);
168 return -1;
169 }
170
171 int get_linux_version()
172 {
173 struct utsname name;
174 char *cp;
175 int a,b,c;
176 if (uname(&name) <0)
177 return -1;
178
179 cp = name.release;
180 a = strtoul(cp, &cp, 10);
181 if (*cp != '.') return -1;
182 b = strtoul(cp+1, &cp, 10);
183 if (*cp != '.') return -1;
184 c = strtoul(cp+1, NULL, 10);
185
186 return (a*1000000)+(b*1000)+c;
187 }
188
189 #ifndef MDASSEMBLE
190 long long parse_size(char *size)
191 {
192 /* parse 'size' which should be a number optionally
193 * followed by 'K', 'M', or 'G'.
194 * Without a suffix, K is assumed.
195 * Number returned is in sectors (half-K)
196 */
197 char *c;
198 long long s = strtoll(size, &c, 10);
199 if (s > 0) {
200 switch (*c) {
201 case 'K':
202 c++;
203 default:
204 s *= 2;
205 break;
206 case 'M':
207 c++;
208 s *= 1024 * 2;
209 break;
210 case 'G':
211 c++;
212 s *= 1024 * 1024 * 2;
213 break;
214 }
215 }
216 if (*c)
217 s = 0;
218 return s;
219 }
220
221 int parse_layout_10(char *layout)
222 {
223 int copies, rv;
224 char *cp;
225 /* Parse the layout string for raid10 */
226 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
227 if ((layout[0] != 'n' && layout[0] != 'f' && layout[0] != 'o') ||
228 (copies = strtoul(layout+1, &cp, 10)) < 1 ||
229 copies > 200 ||
230 *cp)
231 return -1;
232 if (layout[0] == 'n')
233 rv = 256 + copies;
234 else if (layout[0] == 'o')
235 rv = 0x10000 + (copies<<8) + 1;
236 else
237 rv = 1 + (copies<<8);
238 return rv;
239 }
240
241 int parse_layout_faulty(char *layout)
242 {
243 /* Parse the layout string for 'faulty' */
244 int ln = strcspn(layout, "0123456789");
245 char *m = strdup(layout);
246 int mode;
247 m[ln] = 0;
248 mode = map_name(faultylayout, m);
249 if (mode == UnSet)
250 return -1;
251
252 return mode | (atoi(layout+ln)<< ModeShift);
253 }
254 #endif
255
256 void remove_partitions(int fd)
257 {
258 /* remove partitions from this block devices.
259 * This is used for components added to an array
260 */
261 #ifdef BLKPG_DEL_PARTITION
262 struct blkpg_ioctl_arg a;
263 struct blkpg_partition p;
264
265 a.op = BLKPG_DEL_PARTITION;
266 a.data = (void*)&p;
267 a.datalen = sizeof(p);
268 a.flags = 0;
269 memset(a.data, 0, a.datalen);
270 for (p.pno=0; p.pno < 16; p.pno++)
271 ioctl(fd, BLKPG, &a);
272 #endif
273 }
274
275 int enough(int level, int raid_disks, int layout, int clean,
276 char *avail, int avail_disks)
277 {
278 int copies, first;
279 switch (level) {
280 case 10:
281 /* This is the tricky one - we need to check
282 * which actual disks are present.
283 */
284 copies = (layout&255)* ((layout>>8) & 255);
285 first=0;
286 do {
287 /* there must be one of the 'copies' form 'first' */
288 int n = copies;
289 int cnt=0;
290 while (n--) {
291 if (avail[first])
292 cnt++;
293 first = (first+1) % raid_disks;
294 }
295 if (cnt == 0)
296 return 0;
297
298 } while (first != 0);
299 return 1;
300
301 case LEVEL_MULTIPATH:
302 return avail_disks>= 1;
303 case LEVEL_LINEAR:
304 case 0:
305 return avail_disks == raid_disks;
306 case 1:
307 return avail_disks >= 1;
308 case 4:
309 case 5:
310 if (clean)
311 return avail_disks >= raid_disks-1;
312 else
313 return avail_disks >= raid_disks;
314 case 6:
315 if (clean)
316 return avail_disks >= raid_disks-2;
317 else
318 return avail_disks >= raid_disks;
319 default:
320 return 0;
321 }
322 }
323
324 const int uuid_match_any[4] = { ~0, ~0, ~0, ~0 };
325 int same_uuid(int a[4], int b[4], int swapuuid)
326 {
327 if (memcmp(a, uuid_match_any, sizeof(int[4])) == 0 ||
328 memcmp(b, uuid_match_any, sizeof(int[4])) == 0)
329 return 1;
330
331 if (swapuuid) {
332 /* parse uuids are hostendian.
333 * uuid's from some superblocks are big-ending
334 * if there is a difference, we need to swap..
335 */
336 unsigned char *ac = (unsigned char *)a;
337 unsigned char *bc = (unsigned char *)b;
338 int i;
339 for (i=0; i<16; i+= 4) {
340 if (ac[i+0] != bc[i+3] ||
341 ac[i+1] != bc[i+2] ||
342 ac[i+2] != bc[i+1] ||
343 ac[i+3] != bc[i+0])
344 return 0;
345 }
346 return 1;
347 } else {
348 if (a[0]==b[0] &&
349 a[1]==b[1] &&
350 a[2]==b[2] &&
351 a[3]==b[3])
352 return 1;
353 return 0;
354 }
355 }
356 void copy_uuid(void *a, int b[4], int swapuuid)
357 {
358 if (swapuuid) {
359 /* parse uuids are hostendian.
360 * uuid's from some superblocks are big-ending
361 * if there is a difference, we need to swap..
362 */
363 unsigned char *ac = (unsigned char *)a;
364 unsigned char *bc = (unsigned char *)b;
365 int i;
366 for (i=0; i<16; i+= 4) {
367 ac[i+0] = bc[i+3];
368 ac[i+1] = bc[i+2];
369 ac[i+2] = bc[i+1];
370 ac[i+3] = bc[i+0];
371 }
372 } else
373 memcpy(a, b, 16);
374 }
375
376 char *__fname_from_uuid(int id[4], int swap, char *buf, char sep)
377 {
378 int i, j;
379 char uuid[16];
380 char *c = buf;
381 strcpy(c, "UUID-");
382 c += strlen(c);
383 copy_uuid(uuid, id, swap);
384 for (i = 0; i < 4; i++) {
385 if (i)
386 *c++ = sep;
387 for (j = 3; j >= 0; j--) {
388 sprintf(c,"%02x", (unsigned char) uuid[j+4*i]);
389 c+= 2;
390 }
391 }
392 return buf;
393
394 }
395
396 char *fname_from_uuid(struct supertype *st, struct mdinfo *info, char *buf, char sep)
397 {
398 return __fname_from_uuid(info->uuid, st->ss->swapuuid, buf, sep);
399 }
400
401 #ifndef MDASSEMBLE
402 int check_ext2(int fd, char *name)
403 {
404 /*
405 * Check for an ext2fs file system.
406 * Superblock is always 1K at 1K offset
407 *
408 * s_magic is le16 at 56 == 0xEF53
409 * report mtime - le32 at 44
410 * blocks - le32 at 4
411 * logblksize - le32 at 24
412 */
413 unsigned char sb[1024];
414 time_t mtime;
415 int size, bsize;
416 if (lseek(fd, 1024,0)!= 1024)
417 return 0;
418 if (read(fd, sb, 1024)!= 1024)
419 return 0;
420 if (sb[56] != 0x53 || sb[57] != 0xef)
421 return 0;
422
423 mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8;
424 bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8;
425 size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8;
426 fprintf(stderr, Name ": %s appears to contain an ext2fs file system\n",
427 name);
428 fprintf(stderr," size=%dK mtime=%s",
429 size*(1<<bsize), ctime(&mtime));
430 return 1;
431 }
432
433 int check_reiser(int fd, char *name)
434 {
435 /*
436 * superblock is at 64K
437 * size is 1024;
438 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
439 *
440 */
441 unsigned char sb[1024];
442 unsigned long size;
443 if (lseek(fd, 64*1024, 0) != 64*1024)
444 return 0;
445 if (read(fd, sb, 1024) != 1024)
446 return 0;
447 if (strncmp((char*)sb+52, "ReIsErFs",8)!=0 &&
448 strncmp((char*)sb+52, "ReIsEr2Fs",9)!=0)
449 return 0;
450 fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name);
451 size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8;
452 fprintf(stderr, " size = %luK\n", size*4);
453
454 return 1;
455 }
456
457 int check_raid(int fd, char *name)
458 {
459 struct mdinfo info;
460 time_t crtime;
461 char *level;
462 struct supertype *st = guess_super(fd);
463
464 if (!st) return 0;
465 st->ss->load_super(st, fd, name);
466 /* Looks like a raid array .. */
467 fprintf(stderr, Name ": %s appears to be part of a raid array:\n",
468 name);
469 st->ss->getinfo_super(st, &info);
470 st->ss->free_super(st);
471 crtime = info.array.ctime;
472 level = map_num(pers, info.array.level);
473 if (!level) level = "-unknown-";
474 fprintf(stderr, " level=%s devices=%d ctime=%s",
475 level, info.array.raid_disks, ctime(&crtime));
476 return 1;
477 }
478
479 int ask(char *mesg)
480 {
481 char *add = "";
482 int i;
483 for (i=0; i<5; i++) {
484 char buf[100];
485 fprintf(stderr, "%s%s", mesg, add);
486 fflush(stderr);
487 if (fgets(buf, 100, stdin)==NULL)
488 return 0;
489 if (buf[0]=='y' || buf[0]=='Y')
490 return 1;
491 if (buf[0]=='n' || buf[0]=='N')
492 return 0;
493 add = "(y/n) ";
494 }
495 fprintf(stderr, Name ": assuming 'no'\n");
496 return 0;
497 }
498 #endif /* MDASSEMBLE */
499
500 char *map_num(mapping_t *map, int num)
501 {
502 while (map->name) {
503 if (map->num == num)
504 return map->name;
505 map++;
506 }
507 return NULL;
508 }
509
510 int map_name(mapping_t *map, char *name)
511 {
512 while (map->name) {
513 if (strcmp(map->name, name)==0)
514 return map->num;
515 map++;
516 }
517 return UnSet;
518 }
519
520
521 int is_standard(char *dev, int *nump)
522 {
523 /* tests if dev is a "standard" md dev name.
524 * i.e if the last component is "/dNN" or "/mdNN",
525 * where NN is a string of digits
526 * Returns 1 if a partitionable standard,
527 * -1 if non-partitonable,
528 * 0 if not a standard name.
529 */
530 char *d = strrchr(dev, '/');
531 int type=0;
532 int num;
533 if (!d)
534 return 0;
535 if (strncmp(d, "/d",2)==0)
536 d += 2, type=1; /* /dev/md/dN{pM} */
537 else if (strncmp(d, "/md_d", 5)==0)
538 d += 5, type=1; /* /dev/md_dN{pM} */
539 else if (strncmp(d, "/md", 3)==0)
540 d += 3, type=-1; /* /dev/mdN */
541 else if (d-dev > 3 && strncmp(d-2, "md/", 3)==0)
542 d += 1, type=-1; /* /dev/md/N */
543 else
544 return 0;
545 if (!*d)
546 return 0;
547 num = atoi(d);
548 while (isdigit(*d))
549 d++;
550 if (*d)
551 return 0;
552 if (nump) *nump = num;
553
554 return type;
555 }
556
557
558 /*
559 * convert a major/minor pair for a block device into a name in /dev, if possible.
560 * On the first call, walk /dev collecting name.
561 * Put them in a simple linked listfor now.
562 */
563 struct devmap {
564 int major, minor;
565 char *name;
566 struct devmap *next;
567 } *devlist = NULL;
568 int devlist_ready = 0;
569
570 int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
571 {
572 struct stat st;
573
574 if (S_ISLNK(stb->st_mode)) {
575 if (stat(name, &st) != 0)
576 return 0;
577 stb = &st;
578 }
579
580 if ((stb->st_mode&S_IFMT)== S_IFBLK) {
581 char *n = strdup(name);
582 struct devmap *dm = malloc(sizeof(*dm));
583 if (strncmp(n, "/dev/./", 7)==0)
584 strcpy(n+4, name+6);
585 if (dm) {
586 dm->major = major(stb->st_rdev);
587 dm->minor = minor(stb->st_rdev);
588 dm->name = n;
589 dm->next = devlist;
590 devlist = dm;
591 }
592 }
593 return 0;
594 }
595
596 #ifndef HAVE_NFTW
597 #ifdef HAVE_FTW
598 int add_dev_1(const char *name, const struct stat *stb, int flag)
599 {
600 return add_dev(name, stb, flag, NULL);
601 }
602 int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
603 {
604 return ftw(path, add_dev_1, nopenfd);
605 }
606 #else
607 int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
608 {
609 return 0;
610 }
611 #endif /* HAVE_FTW */
612 #endif /* HAVE_NFTW */
613
614 /*
615 * Find a block device with the right major/minor number.
616 * If we find multiple names, choose the shortest.
617 * If we find a name in /dev/md/, we prefer that.
618 * This applies only to names for MD devices.
619 */
620 char *map_dev(int major, int minor, int create)
621 {
622 struct devmap *p;
623 char *regular = NULL, *preferred=NULL;
624 int did_check = 0;
625
626 if (major == 0 && minor == 0)
627 return NULL;
628
629 retry:
630 if (!devlist_ready) {
631 char *dev = "/dev";
632 struct stat stb;
633 while(devlist) {
634 struct devmap *d = devlist;
635 devlist = d->next;
636 free(d->name);
637 free(d);
638 }
639 if (lstat(dev, &stb)==0 &&
640 S_ISLNK(stb.st_mode))
641 dev = "/dev/.";
642 nftw(dev, add_dev, 10, FTW_PHYS);
643 devlist_ready=1;
644 did_check = 1;
645 }
646
647 for (p=devlist; p; p=p->next)
648 if (p->major == major &&
649 p->minor == minor) {
650 if (strncmp(p->name, "/dev/md/",8) == 0) {
651 if (preferred == NULL ||
652 strlen(p->name) < strlen(preferred))
653 preferred = p->name;
654 } else {
655 if (regular == NULL ||
656 strlen(p->name) < strlen(regular))
657 regular = p->name;
658 }
659 }
660 if (!regular && !preferred && !did_check) {
661 devlist_ready = 0;
662 goto retry;
663 }
664 if (create && !regular && !preferred) {
665 static char buf[30];
666 snprintf(buf, sizeof(buf), "%d:%d", major, minor);
667 regular = buf;
668 }
669
670 return preferred ? preferred : regular;
671 }
672
673 unsigned long calc_csum(void *super, int bytes)
674 {
675 unsigned long long newcsum = 0;
676 int i;
677 unsigned int csum;
678 unsigned int *superc = (unsigned int*) super;
679
680 for(i=0; i<bytes/4; i++)
681 newcsum+= superc[i];
682 csum = (newcsum& 0xffffffff) + (newcsum>>32);
683 #ifdef __alpha__
684 /* The in-kernel checksum calculation is always 16bit on
685 * the alpha, though it is 32 bit on i386...
686 * I wonder what it is elsewhere... (it uses and API in
687 * a way that it shouldn't).
688 */
689 csum = (csum & 0xffff) + (csum >> 16);
690 csum = (csum & 0xffff) + (csum >> 16);
691 #endif
692 return csum;
693 }
694
695 #ifndef MDASSEMBLE
696 char *human_size(long long bytes)
697 {
698 static char buf[30];
699
700 /* We convert bytes to either centi-M{ega,ibi}bytes or
701 * centi-G{igi,ibi}bytes, with appropriate rounding,
702 * and then print 1/100th of those as a decimal.
703 * We allow upto 2048Megabytes before converting to
704 * gigabytes, as that shows more precision and isn't
705 * too large a number.
706 * Terrabytes are not yet handled.
707 */
708
709 if (bytes < 5000*1024)
710 buf[0]=0;
711 else if (bytes < 2*1024LL*1024LL*1024LL) {
712 long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2;
713 long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2;
714 snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)",
715 cMiB/100 , cMiB % 100,
716 cMB/100, cMB % 100);
717 } else {
718 long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2;
719 long cGB = (bytes / (1000000000LL/200LL ) +1) /2;
720 snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)",
721 cGiB/100 , cGiB % 100,
722 cGB/100, cGB % 100);
723 }
724 return buf;
725 }
726
727 char *human_size_brief(long long bytes)
728 {
729 static char buf[30];
730
731 if (bytes < 5000*1024)
732 snprintf(buf, sizeof(buf), "%ld.%02ldKiB",
733 (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
734 );
735 else if (bytes < 2*1024LL*1024LL*1024LL)
736 snprintf(buf, sizeof(buf), "%ld.%02ldMiB",
737 (long)(bytes>>20),
738 (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
739 );
740 else
741 snprintf(buf, sizeof(buf), "%ld.%02ldGiB",
742 (long)(bytes>>30),
743 (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
744 );
745 return buf;
746 }
747
748 void print_r10_layout(int layout)
749 {
750 int near = layout & 255;
751 int far = (layout >> 8) & 255;
752 int offset = (layout&0x10000);
753 char *sep = "";
754
755 if (near != 1) {
756 printf("%s near=%d", sep, near);
757 sep = ",";
758 }
759 if (far != 1)
760 printf("%s %s=%d", sep, offset?"offset":"far", far);
761 if (near*far == 1)
762 printf("NO REDUNDANCY");
763 }
764 #endif
765
766 unsigned long long calc_array_size(int level, int raid_disks, int layout,
767 int chunksize, unsigned long long devsize)
768 {
769 int data_disks = 0;
770 switch (level) {
771 case 0: data_disks = raid_disks; break;
772 case 1: data_disks = 1; break;
773 case 4:
774 case 5: data_disks = raid_disks - 1; break;
775 case 6: data_disks = raid_disks - 2; break;
776 case 10: data_disks = raid_disks / (layout & 255) / ((layout>>8)&255);
777 break;
778 }
779 devsize &= ~(unsigned long long)((chunksize>>9)-1);
780 return data_disks * devsize;
781 }
782
783 int get_mdp_major(void)
784 {
785 static int mdp_major = -1;
786 FILE *fl;
787 char *w;
788 int have_block = 0;
789 int have_devices = 0;
790 int last_num = -1;
791
792 if (mdp_major != -1)
793 return mdp_major;
794 fl = fopen("/proc/devices", "r");
795 if (!fl)
796 return -1;
797 while ((w = conf_word(fl, 1))) {
798 if (have_block && strcmp(w, "devices:")==0)
799 have_devices = 1;
800 have_block = (strcmp(w, "Block")==0);
801 if (isdigit(w[0]))
802 last_num = atoi(w);
803 if (have_devices && strcmp(w, "mdp")==0)
804 mdp_major = last_num;
805 free(w);
806 }
807 fclose(fl);
808 return mdp_major;
809 }
810
811 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
812 char *get_md_name(int dev)
813 {
814 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
815 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
816 static char devname[50];
817 struct stat stb;
818 dev_t rdev;
819 char *dn;
820
821 if (dev < 0) {
822 int mdp = get_mdp_major();
823 if (mdp < 0) return NULL;
824 rdev = makedev(mdp, (-1-dev)<<6);
825 snprintf(devname, sizeof(devname), "/dev/md/d%d", -1-dev);
826 if (stat(devname, &stb) == 0
827 && (S_IFMT&stb.st_mode) == S_IFBLK
828 && (stb.st_rdev == rdev))
829 return devname;
830 } else {
831 rdev = makedev(MD_MAJOR, dev);
832 snprintf(devname, sizeof(devname), "/dev/md%d", dev);
833 if (stat(devname, &stb) == 0
834 && (S_IFMT&stb.st_mode) == S_IFBLK
835 && (stb.st_rdev == rdev))
836 return devname;
837
838 snprintf(devname, sizeof(devname), "/dev/md/%d", dev);
839 if (stat(devname, &stb) == 0
840 && (S_IFMT&stb.st_mode) == S_IFBLK
841 && (stb.st_rdev == rdev))
842 return devname;
843 }
844 dn = map_dev(major(rdev), minor(rdev), 0);
845 if (dn)
846 return dn;
847 snprintf(devname, sizeof(devname), "/dev/.tmp.md%d", dev);
848 if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
849 if (errno != EEXIST)
850 return NULL;
851
852 if (stat(devname, &stb) == 0
853 && (S_IFMT&stb.st_mode) == S_IFBLK
854 && (stb.st_rdev == rdev))
855 return devname;
856 unlink(devname);
857 return NULL;
858 }
859
860 void put_md_name(char *name)
861 {
862 if (strncmp(name, "/dev/.tmp.md", 12)==0)
863 unlink(name);
864 }
865
866 int find_free_devnum(int use_partitions)
867 {
868 int devnum;
869 for (devnum = 127; devnum != 128;
870 devnum = devnum ? devnum-1 : (1<<20)-1) {
871 char *dn;
872 int _devnum;
873
874 _devnum = use_partitions ? (-1-devnum) : devnum;
875 if (mddev_busy(_devnum))
876 continue;
877 /* make sure it is new to /dev too, at least as a
878 * non-standard */
879 dn = map_dev(dev2major(_devnum), dev2minor(_devnum), 0);
880 if (dn && ! is_standard(dn, NULL))
881 continue;
882 break;
883 }
884 if (devnum == 128)
885 return NoMdDev;
886 return use_partitions ? (-1-devnum) : devnum;
887 }
888 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
889
890 int dev_open(char *dev, int flags)
891 {
892 /* like 'open', but if 'dev' matches %d:%d, create a temp
893 * block device and open that
894 */
895 char *e;
896 int fd = -1;
897 char devname[32];
898 int major;
899 int minor;
900
901 if (!dev) return -1;
902
903 major = strtoul(dev, &e, 0);
904 if (e > dev && *e == ':' && e[1] &&
905 (minor = strtoul(e+1, &e, 0)) >= 0 &&
906 *e == 0) {
907 snprintf(devname, sizeof(devname), "/dev/.tmp.md.%d:%d:%d",
908 (int)getpid(), major, minor);
909 if (mknod(devname, S_IFBLK|0600, makedev(major, minor))==0) {
910 fd = open(devname, flags|O_DIRECT);
911 unlink(devname);
912 }
913 } else
914 fd = open(dev, flags|O_DIRECT);
915 return fd;
916 }
917
918 int open_dev(int devnum)
919 {
920 char buf[20];
921
922 sprintf(buf, "%d:%d", dev2major(devnum), dev2minor(devnum));
923 return dev_open(buf, O_RDWR);
924 }
925
926 int open_dev_excl(int devnum)
927 {
928 char buf[20];
929 int i;
930
931 sprintf(buf, "%d:%d", dev2major(devnum), dev2minor(devnum));
932 for (i=0 ; i<25 ; i++) {
933 int fd = dev_open(buf, O_RDWR|O_EXCL);
934 if (fd >= 0)
935 return fd;
936 if (errno != EBUSY)
937 return fd;
938 usleep(200000);
939 }
940 return -1;
941 }
942
943 int same_dev(char *one, char *two)
944 {
945 struct stat st1, st2;
946 if (stat(one, &st1) != 0)
947 return 0;
948 if (stat(two, &st2) != 0)
949 return 0;
950 if ((st1.st_mode & S_IFMT) != S_IFBLK)
951 return 0;
952 if ((st2.st_mode & S_IFMT) != S_IFBLK)
953 return 0;
954 return st1.st_rdev == st2.st_rdev;
955 }
956
957 void wait_for(char *dev, int fd)
958 {
959 int i;
960 struct stat stb_want;
961
962 if (fstat(fd, &stb_want) != 0 ||
963 (stb_want.st_mode & S_IFMT) != S_IFBLK)
964 return;
965
966 for (i=0 ; i<25 ; i++) {
967 struct stat stb;
968 if (stat(dev, &stb) == 0 &&
969 (stb.st_mode & S_IFMT) == S_IFBLK &&
970 (stb.st_rdev == stb_want.st_rdev))
971 return;
972 usleep(200000);
973 }
974 if (i == 25)
975 dprintf("%s: timeout waiting for %s\n", __func__, dev);
976 }
977
978 struct superswitch *superlist[] = { &super0, &super1, &super_ddf, &super_imsm, NULL };
979
980 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
981
982 struct supertype *super_by_fd(int fd)
983 {
984 mdu_array_info_t array;
985 int vers;
986 int minor;
987 struct supertype *st = NULL;
988 struct mdinfo *sra;
989 char *verstr;
990 char version[20];
991 int i;
992 char *subarray = NULL;
993
994 sra = sysfs_read(fd, 0, GET_VERSION);
995
996 if (sra) {
997 vers = sra->array.major_version;
998 minor = sra->array.minor_version;
999 verstr = sra->text_version;
1000 } else {
1001 if (ioctl(fd, GET_ARRAY_INFO, &array))
1002 array.major_version = array.minor_version = 0;
1003 vers = array.major_version;
1004 minor = array.minor_version;
1005 verstr = "";
1006 }
1007
1008 if (vers != -1) {
1009 sprintf(version, "%d.%d", vers, minor);
1010 verstr = version;
1011 }
1012 if (minor == -2 && is_subarray(verstr)) {
1013 char *dev = verstr+1;
1014 subarray = strchr(dev, '/');
1015 int devnum;
1016 if (subarray)
1017 *subarray++ = '\0';
1018 devnum = devname2devnum(dev);
1019 subarray = strdup(subarray);
1020 if (sra)
1021 sysfs_free(sra);
1022 sra = sysfs_read(-1, devnum, GET_VERSION);
1023 if (sra && sra->text_version[0])
1024 verstr = sra->text_version;
1025 else
1026 verstr = "-no-metadata-";
1027 }
1028
1029 for (i = 0; st == NULL && superlist[i] ; i++)
1030 st = superlist[i]->match_metadata_desc(verstr);
1031
1032 if (sra)
1033 sysfs_free(sra);
1034 if (st) {
1035 st->sb = NULL;
1036 if (subarray) {
1037 strncpy(st->subarray, subarray, 32);
1038 st->subarray[31] = 0;
1039 free(subarray);
1040 } else
1041 st->subarray[0] = 0;
1042 }
1043 return st;
1044 }
1045 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
1046
1047
1048 struct supertype *dup_super(struct supertype *orig)
1049 {
1050 struct supertype *st;
1051
1052 if (!orig)
1053 return orig;
1054 st = malloc(sizeof(*st));
1055 if (!st)
1056 return st;
1057 memset(st, 0, sizeof(*st));
1058 st->ss = orig->ss;
1059 st->max_devs = orig->max_devs;
1060 st->minor_version = orig->minor_version;
1061 strcpy(st->subarray, orig->subarray);
1062 st->sb = NULL;
1063 st->info = NULL;
1064 return st;
1065 }
1066
1067 struct supertype *guess_super(int fd)
1068 {
1069 /* try each load_super to find the best match,
1070 * and return the best superswitch
1071 */
1072 struct superswitch *ss;
1073 struct supertype *st;
1074 unsigned long besttime = 0;
1075 int bestsuper = -1;
1076 int i;
1077
1078 st = malloc(sizeof(*st));
1079 for (i=0 ; superlist[i]; i++) {
1080 int rv;
1081 ss = superlist[i];
1082 memset(st, 0, sizeof(*st));
1083 rv = ss->load_super(st, fd, NULL);
1084 if (rv == 0) {
1085 struct mdinfo info;
1086 st->ss->getinfo_super(st, &info);
1087 if (bestsuper == -1 ||
1088 besttime < info.array.ctime) {
1089 bestsuper = i;
1090 besttime = info.array.ctime;
1091 }
1092 ss->free_super(st);
1093 }
1094 }
1095 if (bestsuper != -1) {
1096 int rv;
1097 memset(st, 0, sizeof(*st));
1098 rv = superlist[bestsuper]->load_super(st, fd, NULL);
1099 if (rv == 0) {
1100 superlist[bestsuper]->free_super(st);
1101 return st;
1102 }
1103 }
1104 free(st);
1105 return NULL;
1106 }
1107
1108 /* Return size of device in bytes */
1109 int get_dev_size(int fd, char *dname, unsigned long long *sizep)
1110 {
1111 unsigned long long ldsize;
1112 struct stat st;
1113
1114 if (fstat(fd, &st) != -1 && S_ISREG(st.st_mode))
1115 ldsize = (unsigned long long)st.st_size;
1116 else
1117 #ifdef BLKGETSIZE64
1118 if (ioctl(fd, BLKGETSIZE64, &ldsize) != 0)
1119 #endif
1120 {
1121 unsigned long dsize;
1122 if (ioctl(fd, BLKGETSIZE, &dsize) == 0) {
1123 ldsize = dsize;
1124 ldsize <<= 9;
1125 } else {
1126 if (dname)
1127 fprintf(stderr, Name ": Cannot get size of %s: %s\b",
1128 dname, strerror(errno));
1129 return 0;
1130 }
1131 }
1132 *sizep = ldsize;
1133 return 1;
1134 }
1135
1136
1137 /* Sets endofpart parameter to the last block used by the last GPT partition on the device.
1138 * Returns: 1 if successful
1139 * -1 for unknown partition type
1140 * 0 for other errors
1141 */
1142 static int get_gpt_last_partition_end(int fd, unsigned long long *endofpart)
1143 {
1144 unsigned char buf[512];
1145 __u64 *buf64;
1146 unsigned char empty_gpt_entry[16]= {0};
1147 struct GPT_part_entry *part;
1148 unsigned long long curr_part_end;
1149 unsigned all_partitions, entry_size;
1150 int part_nr;
1151
1152 *endofpart = 0;
1153
1154 /* read GPT header */
1155 lseek(fd, 512, SEEK_SET);
1156 if (read(fd, buf, 512) != 512)
1157 return 0;
1158
1159 /* get the number of partition entries and the entry size */
1160 all_partitions = __le32_to_cpu(buf[GPT_ALL_PARTITIONS_OFFSET]);
1161 entry_size = __le32_to_cpu(buf[GPT_ENTRY_SIZE_OFFSET]);
1162
1163 /* Check GPT signature*/
1164 buf64 = (__u64*)buf;
1165 if (buf64[0] != GPT_SIGNATURE_MAGIC)
1166 return -1;
1167
1168 /* sanity checks */
1169 if (all_partitions > 1024 ||
1170 entry_size > 512)
1171 return -1;
1172
1173 /* read first GPT partition entries */
1174 if (read(fd, buf, 512) != 512)
1175 return 0;
1176
1177 part = (struct GPT_part_entry*)buf;
1178
1179 for (part_nr=0; part_nr < all_partitions; part_nr++) {
1180 /* is this valid partition? */
1181 if (memcmp(part->type_guid, empty_gpt_entry, 16) != 0) {
1182 /* check the last lba for the current partition */
1183 buf64 = (__u64*)part->ending_lba;
1184 curr_part_end = __le64_to_cpu(buf64[0]);
1185 if (curr_part_end > *endofpart)
1186 *endofpart = curr_part_end;
1187 }
1188
1189 part = (struct GPT_part_entry*)((unsigned char*)part + entry_size);
1190
1191 if ((unsigned char *)part >= buf + 512) {
1192 if (read(fd, buf, 512) != 512)
1193 return 0;
1194 part = (struct GPT_part_entry*)buf;
1195 }
1196 }
1197 return 1;
1198 }
1199
1200 /* Sets endofpart parameter to the last block used by the last partition on the device.
1201 * Returns: 1 if successful
1202 * -1 for unknown partition type
1203 * 0 for other errors
1204 */
1205 static int get_last_partition_end(int fd, unsigned long long *endofpart)
1206 {
1207 unsigned char boot_sect[512];
1208 struct MBR_part_record *part;
1209 unsigned long long curr_part_end;
1210 int part_nr;
1211 int retval = 0;
1212
1213 *endofpart = 0;
1214
1215 /* read MBR */
1216 lseek(fd, 0, 0);
1217 if (read(fd, boot_sect, 512) != 512)
1218 goto abort;
1219
1220 /* check MBP signature */
1221 if (*((__u16*)(boot_sect + MBR_SIGNATURE_OFFSET))
1222 == MBR_SIGNATURE_MAGIC) {
1223 retval = 1;
1224 /* found the correct signature */
1225 part = (struct MBR_part_record*)
1226 (boot_sect + MBR_PARTITION_TABLE_OFFSET);
1227
1228 for (part_nr=0; part_nr < MBR_PARTITIONS; part_nr++) {
1229 /* check for GPT type */
1230 if (part->part_type == MBR_GPT_PARTITION_TYPE) {
1231 retval = get_gpt_last_partition_end(fd, endofpart);
1232 break;
1233 }
1234 /* check the last used lba for the current partition */
1235 curr_part_end = __le32_to_cpu(part->first_sect_lba) +
1236 __le32_to_cpu(part->blocks_num);
1237 if (curr_part_end > *endofpart)
1238 *endofpart = curr_part_end;
1239
1240 part++;
1241 }
1242 } else {
1243 /* Unknown partition table */
1244 retval = -1;
1245 }
1246 abort:
1247 return retval;
1248 }
1249
1250 int check_partitions(int fd, char *dname, unsigned long long freesize)
1251 {
1252 /*
1253 * Check where the last partition ends
1254 */
1255 unsigned long long endofpart;
1256 int ret;
1257
1258 if ((ret = get_last_partition_end(fd, &endofpart)) > 0) {
1259 /* There appears to be a partition table here */
1260 if (freesize == 0) {
1261 /* partitions will not be visible in new device */
1262 fprintf(stderr,
1263 Name ": partition table exists on %s but will be lost or\n"
1264 " meaningless after creating array\n",
1265 dname);
1266 return 1;
1267 } else if (endofpart > freesize) {
1268 /* last partition overlaps metadata */
1269 fprintf(stderr,
1270 Name ": metadata will over-write last partition on %s.\n",
1271 dname);
1272 return 1;
1273 }
1274 }
1275 return 0;
1276 }
1277
1278 void get_one_disk(int mdfd, mdu_array_info_t *ainf, mdu_disk_info_t *disk)
1279 {
1280 int d;
1281 ioctl(mdfd, GET_ARRAY_INFO, ainf);
1282 for (d = 0 ; d < ainf->raid_disks + ainf->nr_disks ; d++)
1283 if (ioctl(mdfd, GET_DISK_INFO, disk) == 0)
1284 return;
1285 }
1286
1287 int open_container(int fd)
1288 {
1289 /* 'fd' is a block device. Find out if it is in use
1290 * by a container, and return an open fd on that container.
1291 */
1292 char path[256];
1293 char *e;
1294 DIR *dir;
1295 struct dirent *de;
1296 int dfd, n;
1297 char buf[200];
1298 int major, minor;
1299 struct stat st;
1300
1301 if (fstat(fd, &st) != 0)
1302 return -1;
1303 sprintf(path, "/sys/dev/block/%d:%d/holders",
1304 (int)major(st.st_rdev), (int)minor(st.st_rdev));
1305 e = path + strlen(path);
1306
1307 dir = opendir(path);
1308 if (!dir)
1309 return -1;
1310 while ((de = readdir(dir))) {
1311 if (de->d_ino == 0)
1312 continue;
1313 if (de->d_name[0] == '.')
1314 continue;
1315 sprintf(e, "/%s/dev", de->d_name);
1316 dfd = open(path, O_RDONLY);
1317 if (dfd < 0)
1318 continue;
1319 n = read(dfd, buf, sizeof(buf));
1320 close(dfd);
1321 if (n <= 0 || n >= sizeof(buf))
1322 continue;
1323 buf[n] = 0;
1324 if (sscanf(buf, "%d:%d", &major, &minor) != 2)
1325 continue;
1326 sprintf(buf, "%d:%d", major, minor);
1327 dfd = dev_open(buf, O_RDONLY);
1328 if (dfd >= 0) {
1329 closedir(dir);
1330 return dfd;
1331 }
1332 }
1333 closedir(dir);
1334 return -1;
1335 }
1336
1337 int add_disk(int mdfd, struct supertype *st,
1338 struct mdinfo *sra, struct mdinfo *info)
1339 {
1340 /* Add a device to an array, in one of 2 ways. */
1341 int rv;
1342 #ifndef MDASSEMBLE
1343 if (st->ss->external) {
1344 if (info->disk.state & (1<<MD_DISK_SYNC))
1345 info->recovery_start = MaxSector;
1346 else
1347 info->recovery_start = 0;
1348 rv = sysfs_add_disk(sra, info, 0);
1349 if (! rv) {
1350 struct mdinfo *sd2;
1351 for (sd2 = sra->devs; sd2; sd2=sd2->next)
1352 if (sd2 == info)
1353 break;
1354 if (sd2 == NULL) {
1355 sd2 = malloc(sizeof(*sd2));
1356 *sd2 = *info;
1357 sd2->next = sra->devs;
1358 sra->devs = sd2;
1359 }
1360 }
1361 } else
1362 #endif
1363 rv = ioctl(mdfd, ADD_NEW_DISK, &info->disk);
1364 return rv;
1365 }
1366
1367 int set_array_info(int mdfd, struct supertype *st, struct mdinfo *info)
1368 {
1369 /* Initialise kernel's knowledge of array.
1370 * This varies between externally managed arrays
1371 * and older kernels
1372 */
1373 int vers = md_get_version(mdfd);
1374 int rv;
1375
1376 #ifndef MDASSEMBLE
1377 if (st->ss->external)
1378 rv = sysfs_set_array(info, vers);
1379 else
1380 #endif
1381 if ((vers % 100) >= 1) { /* can use different versions */
1382 mdu_array_info_t inf;
1383 memset(&inf, 0, sizeof(inf));
1384 inf.major_version = info->array.major_version;
1385 inf.minor_version = info->array.minor_version;
1386 rv = ioctl(mdfd, SET_ARRAY_INFO, &inf);
1387 } else
1388 rv = ioctl(mdfd, SET_ARRAY_INFO, NULL);
1389 return rv;
1390 }
1391
1392 unsigned long long min_recovery_start(struct mdinfo *array)
1393 {
1394 /* find the minimum recovery_start in an array for metadata
1395 * formats that only record per-array recovery progress instead
1396 * of per-device
1397 */
1398 unsigned long long recovery_start = MaxSector;
1399 struct mdinfo *d;
1400
1401 for (d = array->devs; d; d = d->next)
1402 recovery_start = min(recovery_start, d->recovery_start);
1403
1404 return recovery_start;
1405 }
1406
1407 char *devnum2devname(int num)
1408 {
1409 char name[100];
1410 if (num >= 0)
1411 sprintf(name, "md%d", num);
1412 else
1413 sprintf(name, "md_d%d", -1-num);
1414 return strdup(name);
1415 }
1416
1417 int devname2devnum(char *name)
1418 {
1419 char *ep;
1420 int num;
1421 if (strncmp(name, "md_d", 4)==0)
1422 num = -1-strtoul(name+4, &ep, 10);
1423 else
1424 num = strtoul(name+2, &ep, 10);
1425 return num;
1426 }
1427
1428 int stat2devnum(struct stat *st)
1429 {
1430 char path[30];
1431 char link[200];
1432 char *cp;
1433 int n;
1434
1435 if ((S_IFMT & st->st_mode) == S_IFBLK) {
1436 if (major(st->st_rdev) == MD_MAJOR)
1437 return minor(st->st_rdev);
1438 else if (major(st->st_rdev) == get_mdp_major())
1439 return -1- (minor(st->st_rdev)>>MdpMinorShift);
1440
1441 /* must be an extended-minor partition. Look at the
1442 * /sys/dev/block/%d:%d link which must look like
1443 * ../../block/mdXXX/mdXXXpYY
1444 */
1445 sprintf(path, "/sys/dev/block/%d:%d", major(st->st_rdev),
1446 minor(st->st_rdev));
1447 n = readlink(path, link, sizeof(link)-1);
1448 if (n <= 0)
1449 return NoMdDev;
1450 link[n] = 0;
1451 cp = strrchr(link, '/');
1452 if (cp) *cp = 0;
1453 cp = strchr(link, '/');
1454 if (cp && strncmp(cp, "/md", 3) == 0)
1455 return devname2devnum(cp+1);
1456 }
1457 return NoMdDev;
1458
1459 }
1460
1461 int fd2devnum(int fd)
1462 {
1463 struct stat stb;
1464 if (fstat(fd, &stb) == 0)
1465 return stat2devnum(&stb);
1466 return NoMdDev;
1467 }
1468
1469 char *pid_dir = VAR_RUN;
1470
1471 int mdmon_pid(int devnum)
1472 {
1473 char path[100];
1474 char pid[10];
1475 int fd;
1476 int n;
1477 sprintf(path, "%s/%s.pid", pid_dir, devnum2devname(devnum));
1478 fd = open(path, O_RDONLY | O_NOATIME, 0);
1479
1480 if (fd < 0)
1481 return -1;
1482 n = read(fd, pid, 9);
1483 close(fd);
1484 if (n <= 0)
1485 return -1;
1486 return atoi(pid);
1487 }
1488
1489 int mdmon_running(int devnum)
1490 {
1491 int pid = mdmon_pid(devnum);
1492 if (pid <= 0)
1493 return 0;
1494 if (kill(pid, 0) == 0)
1495 return 1;
1496 return 0;
1497 }
1498
1499 int start_mdmon(int devnum)
1500 {
1501 int i;
1502 int len;
1503 pid_t pid;
1504 int status;
1505 char pathbuf[1024];
1506 char *paths[4] = {
1507 pathbuf,
1508 "/sbin/mdmon",
1509 "mdmon",
1510 NULL
1511 };
1512
1513 if (check_env("MDADM_NO_MDMON"))
1514 return 0;
1515
1516 len = readlink("/proc/self/exe", pathbuf, sizeof(pathbuf));
1517 if (len > 0) {
1518 char *sl;
1519 pathbuf[len] = 0;
1520 sl = strrchr(pathbuf, '/');
1521 if (sl)
1522 sl++;
1523 else
1524 sl = pathbuf;
1525 strcpy(sl, "mdmon");
1526 } else
1527 pathbuf[0] = '\0';
1528
1529 switch(fork()) {
1530 case 0:
1531 /* FIXME yuk. CLOSE_EXEC?? */
1532 for (i=3; i < 100; i++)
1533 close(i);
1534 for (i=0; paths[i]; i++)
1535 if (paths[i][0])
1536 execl(paths[i], "mdmon",
1537 devnum2devname(devnum),
1538 NULL);
1539 exit(1);
1540 case -1: fprintf(stderr, Name ": cannot run mdmon. "
1541 "Array remains readonly\n");
1542 return -1;
1543 default: /* parent - good */
1544 pid = wait(&status);
1545 if (pid < 0 || status != 0)
1546 return -1;
1547 }
1548 return 0;
1549 }
1550
1551 int check_env(char *name)
1552 {
1553 char *val = getenv(name);
1554
1555 if (val && atoi(val) == 1)
1556 return 1;
1557
1558 return 0;
1559 }
1560
1561 __u32 random32(void)
1562 {
1563 __u32 rv;
1564 int rfd = open("/dev/urandom", O_RDONLY);
1565 if (rfd < 0 || read(rfd, &rv, 4) != 4)
1566 rv = random();
1567 if (rfd >= 0)
1568 close(rfd);
1569 return rv;
1570 }
1571
1572 #ifndef MDASSEMBLE
1573 int flush_metadata_updates(struct supertype *st)
1574 {
1575 int sfd;
1576 if (!st->updates) {
1577 st->update_tail = NULL;
1578 return -1;
1579 }
1580
1581 sfd = connect_monitor(devnum2devname(st->container_dev));
1582 if (sfd < 0)
1583 return -1;
1584
1585 while (st->updates) {
1586 struct metadata_update *mu = st->updates;
1587 st->updates = mu->next;
1588
1589 send_message(sfd, mu, 0);
1590 wait_reply(sfd, 0);
1591 free(mu->buf);
1592 free(mu);
1593 }
1594 ack(sfd, 0);
1595 wait_reply(sfd, 0);
1596 close(sfd);
1597 st->update_tail = NULL;
1598 return 0;
1599 }
1600
1601 void append_metadata_update(struct supertype *st, void *buf, int len)
1602 {
1603
1604 struct metadata_update *mu = malloc(sizeof(*mu));
1605
1606 mu->buf = buf;
1607 mu->len = len;
1608 mu->space = NULL;
1609 mu->next = NULL;
1610 *st->update_tail = mu;
1611 st->update_tail = &mu->next;
1612 }
1613 #endif /* MDASSEMBLE */
1614
1615 #ifdef __TINYC__
1616 /* tinyc doesn't optimize this check in ioctl.h out ... */
1617 unsigned int __invalid_size_argument_for_IOC = 0;
1618 #endif
1619