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