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