Introduce MaxSector
[thirdparty/mdadm.git] / util.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
e736b623 4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
64c4757e
NB
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
e736b623 22 * Email: <neilb@suse.de>
64c4757e
NB
23 */
24
9a9dab36 25#include "mdadm.h"
64c4757e 26#include "md_p.h"
edd8d13c 27#include <sys/socket.h>
64c4757e 28#include <sys/utsname.h>
9fe32043 29#include <sys/wait.h>
edd8d13c 30#include <sys/un.h>
98c6faba 31#include <ctype.h>
a322f70c 32#include <dirent.h>
a931db9e 33#include <signal.h>
0a816ef9
NB
34
35/*
36 * following taken from linux/blkpg.h because they aren't
37 * anywhere else and it isn't safe to #include linux/ * stuff.
38 */
39
40#define BLKPG _IO(0x12,105)
41
42/* The argument structure */
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};
64c4757e
NB
67
68/*
69 * Parse a 128 bit uuid in 4 integers
70 * format is 32 hexx nibbles with options :.<space> separator
71 * If not exactly 32 hex digits are found, return 0
72 * else return 1
73 */
74int parse_uuid(char *str, int uuid[4])
75{
aba69144
NB
76 int hit = 0; /* number of Hex digIT */
77 int i;
78 char c;
79 for (i=0; i<4; i++) uuid[i]=0;
80
81 while ((c= *str++)) {
82 int n;
83 if (c>='0' && c<='9')
84 n = c-'0';
85 else if (c>='a' && c <= 'f')
86 n = 10 + c - 'a';
87 else if (c>='A' && c <= 'F')
88 n = 10 + c - 'A';
89 else if (strchr(":. -", c))
90 continue;
91 else return 0;
92
93 if (hit<32) {
94 uuid[hit/8] <<= 4;
95 uuid[hit/8] += n;
96 }
97 hit++;
82b27616 98 }
aba69144
NB
99 if (hit == 32)
100 return 1;
101 return 0;
64c4757e
NB
102}
103
104
105/*
106 * Get the md version number.
107 * We use the RAID_VERSION ioctl if it is supported
108 * If not, but we have a block device with major '9', we assume
109 * 0.36.0
110 *
111 * Return version number as 24 but number - assume version parts
112 * always < 255
113 */
114
115int md_get_version(int fd)
116{
117 struct stat stb;
118 mdu_version_t vers;
119
120 if (fstat(fd, &stb)<0)
121 return -1;
122 if ((S_IFMT&stb.st_mode) != S_IFBLK)
123 return -1;
124
125 if (ioctl(fd, RAID_VERSION, &vers) == 0)
682c7051 126 return (vers.major*10000) + (vers.minor*100) + vers.patchlevel;
5787fa49
NB
127 if (errno == EACCES)
128 return -1;
0df46c2a 129 if (major(stb.st_rdev) == MD_MAJOR)
682c7051 130 return (3600);
64c4757e
NB
131 return -1;
132}
133
64c4757e
NB
134int get_linux_version()
135{
136 struct utsname name;
98c6faba 137 char *cp;
64c4757e
NB
138 int a,b,c;
139 if (uname(&name) <0)
140 return -1;
141
98c6faba
NB
142 cp = name.release;
143 a = strtoul(cp, &cp, 10);
144 if (*cp != '.') return -1;
145 b = strtoul(cp+1, &cp, 10);
146 if (*cp != '.') return -1;
147 c = strtoul(cp+1, NULL, 10);
148
682c7051 149 return (a*1000000)+(b*1000)+c;
64c4757e
NB
150}
151
eb3929a4 152#ifndef MDASSEMBLE
84e11361
N
153long long parse_size(char *size)
154{
155 /* parse 'size' which should be a number optionally
156 * followed by 'K', 'M', or 'G'.
157 * Without a suffix, K is assumed.
158 * Number returned is in sectors (half-K)
159 */
160 char *c;
161 long long s = strtoll(size, &c, 10);
162 if (s > 0) {
163 switch (*c) {
164 case 'K':
165 c++;
166 default:
167 s *= 2;
168 break;
169 case 'M':
170 c++;
171 s *= 1024 * 2;
172 break;
173 case 'G':
174 c++;
175 s *= 1024 * 1024 * 2;
176 break;
177 }
178 }
179 if (*c)
180 s = 0;
181 return s;
182}
183
4a06e2c2
N
184int 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
204int parse_layout_faulty(char *layout)
205{
206 /* Parse the layout string for 'faulty' */
207 int ln = strcspn(layout, "0123456789");
208 char *m = strdup(layout);
209 int mode;
210 m[ln] = 0;
211 mode = map_name(faultylayout, m);
212 if (mode == UnSet)
213 return -1;
214
215 return mode | (atoi(layout+ln)<< ModeShift);
216}
eb3929a4 217#endif
4a06e2c2 218
0430ed48
NB
219void remove_partitions(int fd)
220{
221 /* remove partitions from this block devices.
222 * This is used for components added to an array
223 */
224#ifdef BLKPG_DEL_PARTITION
225 struct blkpg_ioctl_arg a;
226 struct blkpg_partition p;
227
228 a.op = BLKPG_DEL_PARTITION;
229 a.data = (void*)&p;
230 a.datalen = sizeof(p);
231 a.flags = 0;
232 memset(a.data, 0, a.datalen);
233 for (p.pno=0; p.pno < 16; p.pno++)
234 ioctl(fd, BLKPG, &a);
235#endif
236}
237
583315d9 238int enough(int level, int raid_disks, int layout, int clean,
265e0f17 239 char *avail, int avail_disks)
64c4757e 240{
265e0f17 241 int copies, first;
64c4757e 242 switch (level) {
265e0f17
NB
243 case 10:
244 /* This is the tricky one - we need to check
245 * which actual disks are present.
246 */
702b557b 247 copies = (layout&255)* ((layout>>8) & 255);
265e0f17
NB
248 first=0;
249 do {
250 /* there must be one of the 'copies' form 'first' */
251 int n = copies;
252 int cnt=0;
253 while (n--) {
254 if (avail[first])
255 cnt++;
256 first = (first+1) % raid_disks;
257 }
258 if (cnt == 0)
259 return 0;
260
261 } while (first != 0);
262 return 1;
e5329c37 263
df0d4ea0 264 case LEVEL_MULTIPATH:
e0d19036 265 return avail_disks>= 1;
df0d4ea0 266 case LEVEL_LINEAR:
64c4757e
NB
267 case 0:
268 return avail_disks == raid_disks;
269 case 1:
270 return avail_disks >= 1;
271 case 4:
272 case 5:
583315d9
NB
273 if (clean)
274 return avail_disks >= raid_disks-1;
275 else
276 return avail_disks >= raid_disks;
98c6faba 277 case 6:
583315d9
NB
278 if (clean)
279 return avail_disks >= raid_disks-2;
280 else
281 return avail_disks >= raid_disks;
64c4757e
NB
282 default:
283 return 0;
284 }
285}
286
36ba7d48 287const int uuid_match_any[4] = { ~0, ~0, ~0, ~0 };
f277ce36 288int same_uuid(int a[4], int b[4], int swapuuid)
64c4757e 289{
36ba7d48
DW
290 if (memcmp(a, uuid_match_any, sizeof(int[4])) == 0 ||
291 memcmp(b, uuid_match_any, sizeof(int[4])) == 0)
292 return 1;
293
f277ce36
NB
294 if (swapuuid) {
295 /* parse uuids are hostendian.
296 * uuid's from some superblocks are big-ending
aba69144 297 * if there is a difference, we need to swap..
f277ce36
NB
298 */
299 unsigned char *ac = (unsigned char *)a;
300 unsigned char *bc = (unsigned char *)b;
301 int i;
302 for (i=0; i<16; i+= 4) {
303 if (ac[i+0] != bc[i+3] ||
304 ac[i+1] != bc[i+2] ||
305 ac[i+2] != bc[i+1] ||
306 ac[i+3] != bc[i+0])
307 return 0;
308 }
309 return 1;
310 } else {
311 if (a[0]==b[0] &&
312 a[1]==b[1] &&
313 a[2]==b[2] &&
314 a[3]==b[3])
315 return 1;
316 return 0;
317 }
64c4757e 318}
350f29f9
NB
319void copy_uuid(void *a, int b[4], int swapuuid)
320{
321 if (swapuuid) {
322 /* parse uuids are hostendian.
323 * uuid's from some superblocks are big-ending
324 * if there is a difference, we need to swap..
325 */
326 unsigned char *ac = (unsigned char *)a;
327 unsigned char *bc = (unsigned char *)b;
328 int i;
329 for (i=0; i<16; i+= 4) {
330 ac[i+0] = bc[i+3];
331 ac[i+1] = bc[i+2];
332 ac[i+2] = bc[i+1];
333 ac[i+3] = bc[i+0];
334 }
335 } else
336 memcpy(a, b, 16);
337}
64c4757e 338
aae5a112 339char *__fname_from_uuid(int id[4], int swap, char *buf, char sep)
d7288ddc 340{
9968e376 341 int i, j;
d7288ddc
N
342 char uuid[16];
343 char *c = buf;
344 strcpy(c, "UUID-");
345 c += strlen(c);
aae5a112 346 copy_uuid(uuid, id, swap);
9968e376 347 for (i = 0; i < 4; i++) {
9968e376 348 if (i)
ff54de6e 349 *c++ = sep;
9968e376
DW
350 for (j = 3; j >= 0; j--) {
351 sprintf(c,"%02x", (unsigned char) uuid[j+4*i]);
352 c+= 2;
353 }
d7288ddc
N
354 }
355 return buf;
aae5a112
DW
356
357}
358
359char *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);
d7288ddc
N
362}
363
435d4ebb 364#ifndef MDASSEMBLE
682c7051
NB
365int 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
396int 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];
881990a2 405 unsigned long size;
682c7051
NB
406 if (lseek(fd, 64*1024, 0) != 64*1024)
407 return 0;
408 if (read(fd, sb, 1024) != 1024)
409 return 0;
a46f4061
NB
410 if (strncmp((char*)sb+52, "ReIsErFs",8)!=0 &&
411 strncmp((char*)sb+52, "ReIsEr2Fs",9)!=0)
682c7051
NB
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;
881990a2 415 fprintf(stderr, " size = %luK\n", size*4);
aba69144 416
682c7051
NB
417 return 1;
418}
419
420int check_raid(int fd, char *name)
421{
4b1ac34b 422 struct mdinfo info;
682c7051 423 time_t crtime;
d078d77c 424 char *level;
82d9eba6 425 struct supertype *st = guess_super(fd);
f9ce90ba 426
82d9eba6 427 if (!st) return 0;
3da92f27 428 st->ss->load_super(st, fd, name);
82d9eba6
NB
429 /* Looks like a raid array .. */
430 fprintf(stderr, Name ": %s appears to be part of a raid array:\n",
431 name);
3da92f27
NB
432 st->ss->getinfo_super(st, &info);
433 st->ss->free_super(st);
82d9eba6 434 crtime = info.array.ctime;
d078d77c
NB
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));
82d9eba6 439 return 1;
682c7051
NB
440}
441
682c7051
NB
442int 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}
435d4ebb 461#endif /* MDASSEMBLE */
682c7051
NB
462
463char *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
473int 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 }
98c6faba 480 return UnSet;
682c7051 481}
82b27616 482
e5329c37 483
8d80900b 484int is_standard(char *dev, int *nump)
e5329c37
NB
485{
486 /* tests if dev is a "standard" md dev name.
487 * i.e if the last component is "/dNN" or "/mdNN",
aba69144 488 * where NN is a string of digits
598f0d58
NB
489 * Returns 1 if a partitionable standard,
490 * -1 if non-partitonable,
491 * 0 if not a standard name.
e5329c37 492 */
8d80900b
NB
493 char *d = strrchr(dev, '/');
494 int type=0;
495 int num;
496 if (!d)
e5329c37 497 return 0;
8d80900b
NB
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)
2b4ca8f0 501 d += 5, type=1; /* /dev/md_dN{pM} */
8d80900b
NB
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)
5a6d1148 505 d += 1, type=-1; /* /dev/md/N */
e5329c37
NB
506 else
507 return 0;
8d80900b 508 if (!*d)
e5329c37 509 return 0;
8d80900b
NB
510 num = atoi(d);
511 while (isdigit(*d))
512 d++;
513 if (*d)
e5329c37 514 return 0;
8d80900b
NB
515 if (nump) *nump = num;
516
517 return type;
e5329c37
NB
518}
519
520
82b27616
NB
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 */
526struct devmap {
527 int major, minor;
528 char *name;
529 struct devmap *next;
530} *devlist = NULL;
531int devlist_ready = 0;
532
82b27616
NB
533int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
534{
bed256c2 535 struct stat st;
bf68e9d9 536
bed256c2 537 if (S_ISLNK(stb->st_mode)) {
bf68e9d9
DW
538 if (stat(name, &st) != 0)
539 return 0;
bed256c2 540 stb = &st;
82b27616 541 }
bed256c2
NB
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;
82b27616
NB
557}
558
45e878bb
NB
559#ifndef HAVE_NFTW
560#ifdef HAVE_FTW
561int add_dev_1(const char *name, const struct stat *stb, int flag)
562{
563 return add_dev(name, stb, flag, NULL);
564}
565int 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
45e878bb
NB
570int 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
dd0781e5
NB
577/*
578 * Find a block device with the right major/minor number.
b79713f8 579 * If we find multiple names, choose the shortest.
70ef16db 580 * If we find a name in /dev/md/, we prefer that.
b79713f8 581 * This applies only to names for MD devices.
dd0781e5 582 */
16c6fa80 583char *map_dev(int major, int minor, int create)
82b27616 584{
dd0781e5 585 struct devmap *p;
70ef16db 586 char *regular = NULL, *preferred=NULL;
e7bb5d23 587 int did_check = 0;
eed35d66 588
e81cdd9f 589 if (major == 0 && minor == 0)
eed35d66 590 return NULL;
e81cdd9f 591
e7bb5d23 592 retry:
dd0781e5 593 if (!devlist_ready) {
0a416ec3
NB
594 char *dev = "/dev";
595 struct stat stb;
eed35d66
NB
596 while(devlist) {
597 struct devmap *d = devlist;
598 devlist = d->next;
599 free(d->name);
600 free(d);
601 }
0a416ec3
NB
602 if (lstat(dev, &stb)==0 &&
603 S_ISLNK(stb.st_mode))
604 dev = "/dev/.";
605 nftw(dev, add_dev, 10, FTW_PHYS);
dd0781e5 606 devlist_ready=1;
e7bb5d23 607 did_check = 1;
dd0781e5 608 }
82b27616 609
dd0781e5
NB
610 for (p=devlist; p; p=p->next)
611 if (p->major == major &&
612 p->minor == minor) {
70ef16db
N
613 if (strncmp(p->name, "/dev/md/",8) == 0) {
614 if (preferred == NULL ||
615 strlen(p->name) < strlen(preferred))
616 preferred = p->name;
b79713f8 617 } else {
70ef16db
N
618 if (regular == NULL ||
619 strlen(p->name) < strlen(regular))
620 regular = p->name;
b79713f8 621 }
dd0781e5 622 }
70ef16db 623 if (!regular && !preferred && !did_check) {
e7bb5d23
NB
624 devlist_ready = 0;
625 goto retry;
626 }
70ef16db 627 if (create && !regular && !preferred) {
16c6fa80 628 static char buf[30];
382245c3 629 snprintf(buf, sizeof(buf), "%d:%d", major, minor);
70ef16db 630 regular = buf;
16c6fa80
NB
631 }
632
70ef16db 633 return preferred ? preferred : regular;
82b27616
NB
634}
635
4b1ac34b 636unsigned long calc_csum(void *super, int bytes)
82b27616 637{
56eb10c0 638 unsigned long long newcsum = 0;
82b27616 639 int i;
4b1ac34b
NB
640 unsigned int csum;
641 unsigned int *superc = (unsigned int*) super;
82b27616 642
4b1ac34b 643 for(i=0; i<bytes/4; i++)
82b27616
NB
644 newcsum+= superc[i];
645 csum = (newcsum& 0xffffffff) + (newcsum>>32);
570c0542 646#ifdef __alpha__
aba69144 647/* The in-kernel checksum calculation is always 16bit on
570c0542
NB
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
82b27616
NB
655 return csum;
656}
cd29a5c8 657
435d4ebb 658#ifndef MDASSEMBLE
56eb10c0 659char *human_size(long long bytes)
cd29a5c8
NB
660{
661 static char buf[30];
d5d3721e
NB
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 */
cd29a5c8 671
56eb10c0 672 if (bytes < 5000*1024)
cd29a5c8 673 buf[0]=0;
d5d3721e
NB
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;
8f23b0b3 677 snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)",
d5d3721e
NB
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;
8f23b0b3 683 snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)",
d5d3721e
NB
684 cGiB/100 , cGiB % 100,
685 cGB/100, cGB % 100);
686 }
cd29a5c8
NB
687 return buf;
688}
e0d19036
NB
689
690char *human_size_brief(long long bytes)
691{
692 static char buf[30];
e0d19036
NB
693
694 if (bytes < 5000*1024)
8f23b0b3 695 snprintf(buf, sizeof(buf), "%ld.%02ldKiB",
bd526cee 696 (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
e0d19036
NB
697 );
698 else if (bytes < 2*1024LL*1024LL*1024LL)
8f23b0b3 699 snprintf(buf, sizeof(buf), "%ld.%02ldMiB",
e0d19036 700 (long)(bytes>>20),
bd526cee 701 (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
e0d19036
NB
702 );
703 else
8f23b0b3 704 snprintf(buf, sizeof(buf), "%ld.%02ldGiB",
e0d19036 705 (long)(bytes>>30),
bd526cee 706 (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
e0d19036
NB
707 );
708 return buf;
709}
e4965ef8
N
710
711void 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}
435d4ebb 727#endif
e0d19036 728
5f8097be
NB
729unsigned 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
dd0781e5 746int get_mdp_major(void)
98c6faba 747{
dd0781e5
NB
748static int mdp_major = -1;
749 FILE *fl;
98c6faba
NB
750 char *w;
751 int have_block = 0;
752 int have_devices = 0;
753 int last_num = -1;
dd0781e5
NB
754
755 if (mdp_major != -1)
756 return mdp_major;
757 fl = fopen("/proc/devices", "r");
98c6faba 758 if (!fl)
dd0781e5 759 return -1;
98c6faba
NB
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);
dd0781e5 771 return mdp_major;
98c6faba
NB
772}
773
0e600426 774#if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
e0d19036
NB
775char *get_md_name(int dev)
776{
777 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
98c6faba 778 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
e0d19036
NB
779 static char devname[50];
780 struct stat stb;
98c6faba 781 dev_t rdev;
dd0781e5 782 char *dn;
98c6faba
NB
783
784 if (dev < 0) {
dd0781e5
NB
785 int mdp = get_mdp_major();
786 if (mdp < 0) return NULL;
0df46c2a 787 rdev = makedev(mdp, (-1-dev)<<6);
8f23b0b3 788 snprintf(devname, sizeof(devname), "/dev/md/d%d", -1-dev);
98c6faba
NB
789 if (stat(devname, &stb) == 0
790 && (S_IFMT&stb.st_mode) == S_IFBLK
791 && (stb.st_rdev == rdev))
792 return devname;
793 } else {
0df46c2a 794 rdev = makedev(MD_MAJOR, dev);
8f23b0b3 795 snprintf(devname, sizeof(devname), "/dev/md%d", dev);
98c6faba
NB
796 if (stat(devname, &stb) == 0
797 && (S_IFMT&stb.st_mode) == S_IFBLK
798 && (stb.st_rdev == rdev))
799 return devname;
800
8f23b0b3 801 snprintf(devname, sizeof(devname), "/dev/md/%d", dev);
98c6faba
NB
802 if (stat(devname, &stb) == 0
803 && (S_IFMT&stb.st_mode) == S_IFBLK
804 && (stb.st_rdev == rdev))
805 return devname;
806 }
16c6fa80 807 dn = map_dev(major(rdev), minor(rdev), 0);
dd0781e5
NB
808 if (dn)
809 return dn;
8f23b0b3 810 snprintf(devname, sizeof(devname), "/dev/.tmp.md%d", dev);
e0d19036 811 if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
dd0781e5
NB
812 if (errno != EEXIST)
813 return NULL;
e0d19036
NB
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
823void put_md_name(char *name)
824{
825 if (strncmp(name, "/dev/.tmp.md", 12)==0)
826 unlink(name);
827}
ea24acd0 828
ea24acd0
NB
829int find_free_devnum(int use_partitions)
830{
831 int devnum;
832 for (devnum = 127; devnum != 128;
a56fb7ec 833 devnum = devnum ? devnum-1 : (1<<20)-1) {
ea24acd0
NB
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}
435d4ebb 851#endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
f9ce90ba 852
8b0dabea
NB
853int 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];
e81cdd9f 861 int major;
8b0dabea 862 int minor;
e81cdd9f
NB
863
864 if (!dev) return -1;
865
866 major = strtoul(dev, &e, 0);
8b0dabea
NB
867 if (e > dev && *e == ':' && e[1] &&
868 (minor = strtoul(e+1, &e, 0)) >= 0 &&
869 *e == 0) {
8c210183
NB
870 snprintf(devname, sizeof(devname), "/dev/.tmp.md.%d:%d:%d",
871 (int)getpid(), major, minor);
8b0dabea 872 if (mknod(devname, S_IFBLK|0600, makedev(major, minor))==0) {
6416d527 873 fd = open(devname, flags|O_DIRECT);
8b0dabea
NB
874 unlink(devname);
875 }
876 } else
6416d527 877 fd = open(dev, flags|O_DIRECT);
8b0dabea
NB
878 return fd;
879}
f9ce90ba 880
e8a70c89
N
881int 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
a931db9e
NB
889int 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
9008ed1c
N
906int 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
a7c6e3fb 920void wait_for(char *dev, int fd)
a714580e
N
921{
922 int i;
a7c6e3fb
N
923 struct stat stb_want;
924
925 if (fstat(fd, &stb_want) != 0 ||
926 (stb_want.st_mode & S_IFMT) != S_IFBLK)
927 return;
a714580e
N
928
929 for (i=0 ; i<25 ; i++) {
930 struct stat stb;
a7c6e3fb
N
931 if (stat(dev, &stb) == 0 &&
932 (stb.st_mode & S_IFMT) == S_IFBLK &&
933 (stb.st_rdev == stb_want.st_rdev))
a714580e
N
934 return;
935 usleep(200000);
936 }
436305c6
DW
937 if (i == 25)
938 dprintf("%s: timeout waiting for %s\n", __func__, dev);
a714580e
N
939}
940
cdddbdbc 941struct superswitch *superlist[] = { &super0, &super1, &super_ddf, &super_imsm, NULL };
f9ce90ba 942
ea24acd0 943#if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
f7dd881f 944
1686dc25 945struct supertype *super_by_fd(int fd)
f9ce90ba 946{
1686dc25
NB
947 mdu_array_info_t array;
948 int vers;
949 int minor;
950 struct supertype *st = NULL;
7e0f6979 951 struct mdinfo *sra;
142cb9e1 952 char *verstr;
1686dc25
NB
953 char version[20];
954 int i;
f7e7067b 955 char *subarray = NULL;
1686dc25
NB
956
957 sra = sysfs_read(fd, 0, GET_VERSION);
958
959 if (sra) {
7e0f6979
NB
960 vers = sra->array.major_version;
961 minor = sra->array.minor_version;
142cb9e1 962 verstr = sra->text_version;
1686dc25
NB
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;
142cb9e1 968 verstr = "";
6fbba4c9 969 }
82d9eba6 970
1686dc25
NB
971 if (vers != -1) {
972 sprintf(version, "%d.%d", vers, minor);
973 verstr = version;
6fbba4c9 974 }
3c558363 975 if (minor == -2 && is_subarray(verstr)) {
f7e7067b
NB
976 char *dev = verstr+1;
977 subarray = strchr(dev, '/');
978 int devnum;
979 if (subarray)
980 *subarray++ = '\0';
77472ff8 981 devnum = devname2devnum(dev);
f7e7067b
NB
982 subarray = strdup(subarray);
983 if (sra)
984 sysfs_free(sra);
985 sra = sysfs_read(-1, devnum, GET_VERSION);
603f24a0
N
986 if (sra && sra->text_version[0])
987 verstr = sra->text_version;
988 else
989 verstr = "-no-metadata-";
f7e7067b
NB
990 }
991
992 for (i = 0; st == NULL && superlist[i] ; i++)
993 st = superlist[i]->match_metadata_desc(verstr);
1686dc25
NB
994
995 if (sra)
996 sysfs_free(sra);
f7e7067b 997 if (st) {
3b0896f8 998 st->sb = NULL;
f7e7067b
NB
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 }
82d9eba6 1006 return st;
f9ce90ba 1007}
ea24acd0
NB
1008#endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
1009
f9ce90ba 1010
159c3a1a 1011struct supertype *dup_super(struct supertype *orig)
3da92f27 1012{
159c3a1a 1013 struct supertype *st;
1686dc25 1014
d2ca6449
NB
1015 if (!orig)
1016 return orig;
159c3a1a 1017 st = malloc(sizeof(*st));
3da92f27
NB
1018 if (!st)
1019 return st;
ef609477 1020 memset(st, 0, sizeof(*st));
159c3a1a
NB
1021 st->ss = orig->ss;
1022 st->max_devs = orig->max_devs;
1023 st->minor_version = orig->minor_version;
f7e7067b 1024 strcpy(st->subarray, orig->subarray);
159c3a1a
NB
1025 st->sb = NULL;
1026 st->info = NULL;
1027 return st;
3da92f27
NB
1028}
1029
82d9eba6 1030struct supertype *guess_super(int fd)
f9ce90ba
NB
1031{
1032 /* try each load_super to find the best match,
1033 * and return the best superswitch
1034 */
82d9eba6
NB
1035 struct superswitch *ss;
1036 struct supertype *st;
570c0542
NB
1037 unsigned long besttime = 0;
1038 int bestsuper = -1;
f9ce90ba
NB
1039 int i;
1040
82d9eba6 1041 st = malloc(sizeof(*st));
f9ce90ba
NB
1042 for (i=0 ; superlist[i]; i++) {
1043 int rv;
1044 ss = superlist[i];
ef609477 1045 memset(st, 0, sizeof(*st));
3da92f27 1046 rv = ss->load_super(st, fd, NULL);
570c0542
NB
1047 if (rv == 0) {
1048 struct mdinfo info;
3da92f27 1049 st->ss->getinfo_super(st, &info);
570c0542
NB
1050 if (bestsuper == -1 ||
1051 besttime < info.array.ctime) {
1052 bestsuper = i;
1053 besttime = info.array.ctime;
570c0542 1054 }
3da92f27 1055 ss->free_super(st);
570c0542
NB
1056 }
1057 }
1058 if (bestsuper != -1) {
1059 int rv;
ef609477 1060 memset(st, 0, sizeof(*st));
3da92f27 1061 rv = superlist[bestsuper]->load_super(st, fd, NULL);
f9ce90ba 1062 if (rv == 0) {
5e747af2 1063 superlist[bestsuper]->free_super(st);
82d9eba6 1064 return st;
f9ce90ba
NB
1065 }
1066 }
570c0542 1067 free(st);
f9ce90ba
NB
1068 return NULL;
1069}
fe6729fa 1070
beae1dfe
NB
1071/* Return size of device in bytes */
1072int get_dev_size(int fd, char *dname, unsigned long long *sizep)
1073{
1074 unsigned long long ldsize;
c2c9bb6f
NB
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
beae1dfe
NB
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}
8fac0577 1098
8382f19b
NB
1099void 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}
63152c1b 1107
a322f70c
DW
1108int 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 }
355726fa 1154 closedir(dir);
a322f70c
DW
1155 return -1;
1156}
1157
7801ac20
N
1158int 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) {
462906cd
N
1165 rv = sysfs_add_disk(sra, info,
1166 info->disk.state & (1<<MD_DISK_SYNC));
7801ac20
N
1167 if (! rv) {
1168 struct mdinfo *sd2;
f35f2525
N
1169 for (sd2 = sra->devs; sd2; sd2=sd2->next)
1170 if (sd2 == info)
1171 break;
1172 if (sd2 == NULL) {
1173 sd2 = malloc(sizeof(*sd2));
1174 *sd2 = *info;
1175 sd2->next = sra->devs;
1176 sra->devs = sd2;
1177 }
7801ac20
N
1178 }
1179 } else
1180#endif
1181 rv = ioctl(mdfd, ADD_NEW_DISK, &info->disk);
1182 return rv;
1183}
1184
f35f2525
N
1185int set_array_info(int mdfd, struct supertype *st, struct mdinfo *info)
1186{
1187 /* Initialise kernel's knowledge of array.
1188 * This varies between externally managed arrays
1189 * and older kernels
1190 */
1191 int vers = md_get_version(mdfd);
1192 int rv;
1193
1194#ifndef MDASSEMBLE
1195 if (st->ss->external)
1196 rv = sysfs_set_array(info, vers);
1197 else
1198#endif
1199 if ((vers % 100) >= 1) { /* can use different versions */
1200 mdu_array_info_t inf;
1201 memset(&inf, 0, sizeof(inf));
1202 inf.major_version = info->array.major_version;
1203 inf.minor_version = info->array.minor_version;
1204 rv = ioctl(mdfd, SET_ARRAY_INFO, &inf);
1205 } else
1206 rv = ioctl(mdfd, SET_ARRAY_INFO, NULL);
1207 return rv;
1208}
1209
2f6079dc
NB
1210char *devnum2devname(int num)
1211{
1212 char name[100];
1f0769d7 1213 if (num >= 0)
2f6079dc
NB
1214 sprintf(name, "md%d", num);
1215 else
1216 sprintf(name, "md_d%d", -1-num);
1217 return strdup(name);
1218}
1219
77472ff8
NB
1220int devname2devnum(char *name)
1221{
1222 char *ep;
1223 int num;
1224 if (strncmp(name, "md_d", 4)==0)
1225 num = -1-strtoul(name+4, &ep, 10);
1226 else
1227 num = strtoul(name+2, &ep, 10);
1228 return num;
1229}
1230
c94709e8 1231int stat2devnum(struct stat *st)
2f6079dc 1232{
d7ab966b
N
1233 char path[30];
1234 char link[200];
1235 char *cp;
1236 int n;
1237
c94709e8
DW
1238 if ((S_IFMT & st->st_mode) == S_IFBLK) {
1239 if (major(st->st_rdev) == MD_MAJOR)
1240 return minor(st->st_rdev);
d7ab966b
N
1241 else if (major(st->st_rdev) == get_mdp_major())
1242 return -1- (minor(st->st_rdev)>>MdpMinorShift);
1243
1244 /* must be an extended-minor partition. Look at the
1245 * /sys/dev/block/%d:%d link which must look like
1246 * ../../block/mdXXX/mdXXXpYY
1247 */
1248 sprintf(path, "/sys/dev/block/%d:%d", major(st->st_rdev),
1249 minor(st->st_rdev));
1250 n = readlink(path, link, sizeof(link)-1);
1251 if (n <= 0)
1252 return NoMdDev;
1253 link[n] = 0;
1254 cp = strrchr(link, '/');
1255 if (cp) *cp = 0;
1256 cp = strchr(link, '/');
1257 if (cp && strncmp(cp, "/md", 3) == 0)
1258 return devname2devnum(cp+1);
2f6079dc 1259 }
d7ab966b 1260 return NoMdDev;
c94709e8
DW
1261
1262}
1263
1264int fd2devnum(int fd)
1265{
1266 struct stat stb;
1267 if (fstat(fd, &stb) == 0)
1268 return stat2devnum(&stb);
d7ab966b 1269 return NoMdDev;
2f6079dc
NB
1270}
1271
a931db9e
NB
1272int mdmon_running(int devnum)
1273{
1274 char path[100];
1275 char pid[10];
1276 int fd;
1277 int n;
1278 sprintf(path, "/var/run/mdadm/%s.pid", devnum2devname(devnum));
1279 fd = open(path, O_RDONLY, 0);
1280
1281 if (fd < 0)
1282 return 0;
1283 n = read(fd, pid, 9);
1284 close(fd);
1285 if (n <= 0)
1286 return 0;
1287 if (kill(atoi(pid), 0) == 0)
1288 return 1;
1289 return 0;
1290}
1291
1292int signal_mdmon(int devnum)
1293{
1294 char path[100];
1295 char pid[10];
1296 int fd;
1297 int n;
1298 sprintf(path, "/var/run/mdadm/%s.pid", devnum2devname(devnum));
1299 fd = open(path, O_RDONLY, 0);
1300
1301 if (fd < 0)
1302 return 0;
1303 n = read(fd, pid, 9);
1304 close(fd);
1305 if (n <= 0)
1306 return 0;
1307 if (kill(atoi(pid), SIGUSR1) == 0)
1308 return 1;
1309 return 0;
1310}
1311
8850ee3e
N
1312int start_mdmon(int devnum)
1313{
1314 int i;
44d2e365 1315 int len;
9fe32043
N
1316 pid_t pid;
1317 int status;
44d2e365
N
1318 char pathbuf[1024];
1319 char *paths[4] = {
1320 pathbuf,
1321 "/sbin/mdmon",
1322 "mdmon",
1323 NULL
1324 };
8850ee3e 1325
40ebbb9c 1326 if (check_env("MDADM_NO_MDMON"))
8850ee3e
N
1327 return 0;
1328
44d2e365
N
1329 len = readlink("/proc/self/exe", pathbuf, sizeof(pathbuf));
1330 if (len > 0) {
1331 char *sl;
1332 pathbuf[len] = 0;
1333 sl = strrchr(pathbuf, '/');
1334 if (sl)
1335 sl++;
1336 else
1337 sl = pathbuf;
1338 strcpy(sl, "mdmon");
1339 } else
1340 pathbuf[0] = '\0';
1341
8850ee3e
N
1342 switch(fork()) {
1343 case 0:
1344 /* FIXME yuk. CLOSE_EXEC?? */
1345 for (i=3; i < 100; i++)
1346 close(i);
44d2e365
N
1347 for (i=0; paths[i]; i++)
1348 if (paths[i][0])
1349 execl(paths[i], "mdmon",
e8a70c89
N
1350 devnum2devname(devnum),
1351 NULL);
8850ee3e
N
1352 exit(1);
1353 case -1: fprintf(stderr, Name ": cannot run mdmon. "
1354 "Array remains readonly\n");
1355 return -1;
9fe32043
N
1356 default: /* parent - good */
1357 pid = wait(&status);
1358 if (pid < 0 || status != 0)
1359 return -1;
8850ee3e
N
1360 }
1361 return 0;
1362}
1363
40ebbb9c 1364int check_env(char *name)
5dcfcb71 1365{
40ebbb9c 1366 char *val = getenv(name);
5dcfcb71
DW
1367
1368 if (val && atoi(val) == 1)
1369 return 1;
1370
1371 return 0;
1372}
1373
148acb7b
DW
1374__u32 random32(void)
1375{
1376 __u32 rv;
1377 int rfd = open("/dev/urandom", O_RDONLY);
1378 if (rfd < 0 || read(rfd, &rv, 4) != 4)
1379 rv = random();
1380 if (rfd >= 0)
1381 close(rfd);
1382 return rv;
1383}
1384
0e600426 1385#ifndef MDASSEMBLE
edd8d13c
NB
1386int flush_metadata_updates(struct supertype *st)
1387{
1388 int sfd;
1389 if (!st->updates) {
1390 st->update_tail = NULL;
1391 return -1;
1392 }
1393
1394 sfd = connect_monitor(devnum2devname(st->container_dev));
1395 if (sfd < 0)
1396 return -1;
1397
1398 while (st->updates) {
1399 struct metadata_update *mu = st->updates;
1400 st->updates = mu->next;
1401
1402 send_message(sfd, mu, 0);
1403 wait_reply(sfd, 0);
1404 free(mu->buf);
1405 free(mu);
1406 }
1407 ack(sfd, 0);
1408 wait_reply(sfd, 0);
1409 close(sfd);
1410 st->update_tail = NULL;
1411 return 0;
1412}
1413
1414void append_metadata_update(struct supertype *st, void *buf, int len)
1415{
1416
1417 struct metadata_update *mu = malloc(sizeof(*mu));
1418
1419 mu->buf = buf;
1420 mu->len = len;
1421 mu->space = NULL;
1422 mu->next = NULL;
1423 *st->update_tail = mu;
1424 st->update_tail = &mu->next;
1425}
0e600426 1426#endif /* MDASSEMBLE */
a931db9e 1427
fe6729fa
NB
1428#ifdef __TINYC__
1429/* tinyc doesn't optimize this check in ioctl.h out ... */
1430unsigned int __invalid_size_argument_for_IOC = 0;
1431#endif
1432