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