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