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