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