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