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