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