Fix freeing of updates that have been handled by monitor.
[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
NB
31#include "md_p.h"
32#include <sys/utsname.h>
98c6faba 33#include <ctype.h>
a322f70c 34#include <dirent.h>
a931db9e 35#include <signal.h>
0a816ef9
NB
36
37/*
38 * following taken from linux/blkpg.h because they aren't
39 * anywhere else and it isn't safe to #include linux/ * stuff.
40 */
41
42#define BLKPG _IO(0x12,105)
43
44/* The argument structure */
45struct blkpg_ioctl_arg {
46 int op;
47 int flags;
48 int datalen;
49 void *data;
50};
51
52/* The subfunctions (for the op field) */
53#define BLKPG_ADD_PARTITION 1
54#define BLKPG_DEL_PARTITION 2
55
56/* Sizes of name fields. Unused at present. */
57#define BLKPG_DEVNAMELTH 64
58#define BLKPG_VOLNAMELTH 64
59
60/* The data structure for ADD_PARTITION and DEL_PARTITION */
61struct blkpg_partition {
62 long long start; /* starting offset in bytes */
63 long long length; /* length in bytes */
64 int pno; /* partition number */
65 char devname[BLKPG_DEVNAMELTH]; /* partition name, like sda5 or c0d1p2,
66 to be used in kernel messages */
67 char volname[BLKPG_VOLNAMELTH]; /* volume label */
68};
64c4757e
NB
69
70/*
71 * Parse a 128 bit uuid in 4 integers
72 * format is 32 hexx nibbles with options :.<space> separator
73 * If not exactly 32 hex digits are found, return 0
74 * else return 1
75 */
76int parse_uuid(char *str, int uuid[4])
77{
aba69144
NB
78 int hit = 0; /* number of Hex digIT */
79 int i;
80 char c;
81 for (i=0; i<4; i++) uuid[i]=0;
82
83 while ((c= *str++)) {
84 int n;
85 if (c>='0' && c<='9')
86 n = c-'0';
87 else if (c>='a' && c <= 'f')
88 n = 10 + c - 'a';
89 else if (c>='A' && c <= 'F')
90 n = 10 + c - 'A';
91 else if (strchr(":. -", c))
92 continue;
93 else return 0;
94
95 if (hit<32) {
96 uuid[hit/8] <<= 4;
97 uuid[hit/8] += n;
98 }
99 hit++;
82b27616 100 }
aba69144
NB
101 if (hit == 32)
102 return 1;
103 return 0;
64c4757e
NB
104}
105
106
107/*
108 * Get the md version number.
109 * We use the RAID_VERSION ioctl if it is supported
110 * If not, but we have a block device with major '9', we assume
111 * 0.36.0
112 *
113 * Return version number as 24 but number - assume version parts
114 * always < 255
115 */
116
117int md_get_version(int fd)
118{
119 struct stat stb;
120 mdu_version_t vers;
121
122 if (fstat(fd, &stb)<0)
123 return -1;
124 if ((S_IFMT&stb.st_mode) != S_IFBLK)
125 return -1;
126
127 if (ioctl(fd, RAID_VERSION, &vers) == 0)
682c7051 128 return (vers.major*10000) + (vers.minor*100) + vers.patchlevel;
5787fa49
NB
129 if (errno == EACCES)
130 return -1;
0df46c2a 131 if (major(stb.st_rdev) == MD_MAJOR)
682c7051 132 return (3600);
64c4757e
NB
133 return -1;
134}
135
64c4757e
NB
136int get_linux_version()
137{
138 struct utsname name;
98c6faba 139 char *cp;
64c4757e
NB
140 int a,b,c;
141 if (uname(&name) <0)
142 return -1;
143
98c6faba
NB
144 cp = name.release;
145 a = strtoul(cp, &cp, 10);
146 if (*cp != '.') return -1;
147 b = strtoul(cp+1, &cp, 10);
148 if (*cp != '.') return -1;
149 c = strtoul(cp+1, NULL, 10);
150
682c7051 151 return (a*1000000)+(b*1000)+c;
64c4757e
NB
152}
153
0430ed48
NB
154void remove_partitions(int fd)
155{
156 /* remove partitions from this block devices.
157 * This is used for components added to an array
158 */
159#ifdef BLKPG_DEL_PARTITION
160 struct blkpg_ioctl_arg a;
161 struct blkpg_partition p;
162
163 a.op = BLKPG_DEL_PARTITION;
164 a.data = (void*)&p;
165 a.datalen = sizeof(p);
166 a.flags = 0;
167 memset(a.data, 0, a.datalen);
168 for (p.pno=0; p.pno < 16; p.pno++)
169 ioctl(fd, BLKPG, &a);
170#endif
171}
172
583315d9 173int enough(int level, int raid_disks, int layout, int clean,
265e0f17 174 char *avail, int avail_disks)
64c4757e 175{
265e0f17 176 int copies, first;
64c4757e 177 switch (level) {
265e0f17
NB
178 case 10:
179 /* This is the tricky one - we need to check
180 * which actual disks are present.
181 */
702b557b 182 copies = (layout&255)* ((layout>>8) & 255);
265e0f17
NB
183 first=0;
184 do {
185 /* there must be one of the 'copies' form 'first' */
186 int n = copies;
187 int cnt=0;
188 while (n--) {
189 if (avail[first])
190 cnt++;
191 first = (first+1) % raid_disks;
192 }
193 if (cnt == 0)
194 return 0;
195
196 } while (first != 0);
197 return 1;
e5329c37 198
e0d19036
NB
199 case -4:
200 return avail_disks>= 1;
64c4757e
NB
201 case -1:
202 case 0:
203 return avail_disks == raid_disks;
204 case 1:
205 return avail_disks >= 1;
206 case 4:
207 case 5:
583315d9
NB
208 if (clean)
209 return avail_disks >= raid_disks-1;
210 else
211 return avail_disks >= raid_disks;
98c6faba 212 case 6:
583315d9
NB
213 if (clean)
214 return avail_disks >= raid_disks-2;
215 else
216 return avail_disks >= raid_disks;
64c4757e
NB
217 default:
218 return 0;
219 }
220}
221
f277ce36 222int same_uuid(int a[4], int b[4], int swapuuid)
64c4757e 223{
f277ce36
NB
224 if (swapuuid) {
225 /* parse uuids are hostendian.
226 * uuid's from some superblocks are big-ending
aba69144 227 * if there is a difference, we need to swap..
f277ce36
NB
228 */
229 unsigned char *ac = (unsigned char *)a;
230 unsigned char *bc = (unsigned char *)b;
231 int i;
232 for (i=0; i<16; i+= 4) {
233 if (ac[i+0] != bc[i+3] ||
234 ac[i+1] != bc[i+2] ||
235 ac[i+2] != bc[i+1] ||
236 ac[i+3] != bc[i+0])
237 return 0;
238 }
239 return 1;
240 } else {
241 if (a[0]==b[0] &&
242 a[1]==b[1] &&
243 a[2]==b[2] &&
244 a[3]==b[3])
245 return 1;
246 return 0;
247 }
64c4757e 248}
350f29f9
NB
249void copy_uuid(void *a, int b[4], int swapuuid)
250{
251 if (swapuuid) {
252 /* parse uuids are hostendian.
253 * uuid's from some superblocks are big-ending
254 * if there is a difference, we need to swap..
255 */
256 unsigned char *ac = (unsigned char *)a;
257 unsigned char *bc = (unsigned char *)b;
258 int i;
259 for (i=0; i<16; i+= 4) {
260 ac[i+0] = bc[i+3];
261 ac[i+1] = bc[i+2];
262 ac[i+2] = bc[i+1];
263 ac[i+3] = bc[i+0];
264 }
265 } else
266 memcpy(a, b, 16);
267}
64c4757e 268
435d4ebb 269#ifndef MDASSEMBLE
682c7051
NB
270int check_ext2(int fd, char *name)
271{
272 /*
273 * Check for an ext2fs file system.
274 * Superblock is always 1K at 1K offset
275 *
276 * s_magic is le16 at 56 == 0xEF53
277 * report mtime - le32 at 44
278 * blocks - le32 at 4
279 * logblksize - le32 at 24
280 */
281 unsigned char sb[1024];
282 time_t mtime;
283 int size, bsize;
284 if (lseek(fd, 1024,0)!= 1024)
285 return 0;
286 if (read(fd, sb, 1024)!= 1024)
287 return 0;
288 if (sb[56] != 0x53 || sb[57] != 0xef)
289 return 0;
290
291 mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8;
292 bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8;
293 size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8;
294 fprintf(stderr, Name ": %s appears to contain an ext2fs file system\n",
295 name);
296 fprintf(stderr," size=%dK mtime=%s",
297 size*(1<<bsize), ctime(&mtime));
298 return 1;
299}
300
301int check_reiser(int fd, char *name)
302{
303 /*
304 * superblock is at 64K
305 * size is 1024;
306 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
307 *
308 */
309 unsigned char sb[1024];
881990a2 310 unsigned long size;
682c7051
NB
311 if (lseek(fd, 64*1024, 0) != 64*1024)
312 return 0;
313 if (read(fd, sb, 1024) != 1024)
314 return 0;
a46f4061
NB
315 if (strncmp((char*)sb+52, "ReIsErFs",8)!=0 &&
316 strncmp((char*)sb+52, "ReIsEr2Fs",9)!=0)
682c7051
NB
317 return 0;
318 fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name);
319 size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8;
881990a2 320 fprintf(stderr, " size = %luK\n", size*4);
aba69144 321
682c7051
NB
322 return 1;
323}
324
325int check_raid(int fd, char *name)
326{
4b1ac34b 327 struct mdinfo info;
682c7051 328 time_t crtime;
d078d77c 329 char *level;
82d9eba6 330 struct supertype *st = guess_super(fd);
f9ce90ba 331
82d9eba6 332 if (!st) return 0;
3da92f27 333 st->ss->load_super(st, fd, name);
82d9eba6
NB
334 /* Looks like a raid array .. */
335 fprintf(stderr, Name ": %s appears to be part of a raid array:\n",
336 name);
3da92f27
NB
337 st->ss->getinfo_super(st, &info);
338 st->ss->free_super(st);
82d9eba6 339 crtime = info.array.ctime;
d078d77c
NB
340 level = map_num(pers, info.array.level);
341 if (!level) level = "-unknown-";
342 fprintf(stderr, " level=%s devices=%d ctime=%s",
343 level, info.array.raid_disks, ctime(&crtime));
82d9eba6 344 return 1;
682c7051
NB
345}
346
682c7051
NB
347int ask(char *mesg)
348{
349 char *add = "";
350 int i;
351 for (i=0; i<5; i++) {
352 char buf[100];
353 fprintf(stderr, "%s%s", mesg, add);
354 fflush(stderr);
355 if (fgets(buf, 100, stdin)==NULL)
356 return 0;
357 if (buf[0]=='y' || buf[0]=='Y')
358 return 1;
359 if (buf[0]=='n' || buf[0]=='N')
360 return 0;
361 add = "(y/n) ";
362 }
363 fprintf(stderr, Name ": assuming 'no'\n");
364 return 0;
365}
435d4ebb 366#endif /* MDASSEMBLE */
682c7051
NB
367
368char *map_num(mapping_t *map, int num)
369{
370 while (map->name) {
371 if (map->num == num)
372 return map->name;
373 map++;
374 }
375 return NULL;
376}
377
378int map_name(mapping_t *map, char *name)
379{
380 while (map->name) {
381 if (strcmp(map->name, name)==0)
382 return map->num;
383 map++;
384 }
98c6faba 385 return UnSet;
682c7051 386}
82b27616 387
e5329c37 388
8d80900b 389int is_standard(char *dev, int *nump)
e5329c37
NB
390{
391 /* tests if dev is a "standard" md dev name.
392 * i.e if the last component is "/dNN" or "/mdNN",
aba69144 393 * where NN is a string of digits
598f0d58
NB
394 * Returns 1 if a partitionable standard,
395 * -1 if non-partitonable,
396 * 0 if not a standard name.
e5329c37 397 */
8d80900b
NB
398 char *d = strrchr(dev, '/');
399 int type=0;
400 int num;
401 if (!d)
e5329c37 402 return 0;
8d80900b
NB
403 if (strncmp(d, "/d",2)==0)
404 d += 2, type=1; /* /dev/md/dN{pM} */
405 else if (strncmp(d, "/md_d", 5)==0)
406 d += 5, type=1; /* /dev/md_dNpM */
407 else if (strncmp(d, "/md", 3)==0)
408 d += 3, type=-1; /* /dev/mdN */
409 else if (d-dev > 3 && strncmp(d-2, "md/", 3)==0)
5a6d1148 410 d += 1, type=-1; /* /dev/md/N */
e5329c37
NB
411 else
412 return 0;
8d80900b 413 if (!*d)
e5329c37 414 return 0;
8d80900b
NB
415 num = atoi(d);
416 while (isdigit(*d))
417 d++;
418 if (*d)
e5329c37 419 return 0;
8d80900b
NB
420 if (nump) *nump = num;
421
422 return type;
e5329c37
NB
423}
424
425
82b27616
NB
426/*
427 * convert a major/minor pair for a block device into a name in /dev, if possible.
428 * On the first call, walk /dev collecting name.
429 * Put them in a simple linked listfor now.
430 */
431struct devmap {
432 int major, minor;
433 char *name;
434 struct devmap *next;
435} *devlist = NULL;
436int devlist_ready = 0;
437
82b27616
NB
438int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
439{
bed256c2
NB
440 struct stat st;
441 if (S_ISLNK(stb->st_mode)) {
442 stat(name, &st);
443 stb = &st;
82b27616 444 }
bed256c2
NB
445
446 if ((stb->st_mode&S_IFMT)== S_IFBLK) {
447 char *n = strdup(name);
448 struct devmap *dm = malloc(sizeof(*dm));
449 if (strncmp(n, "/dev/./", 7)==0)
450 strcpy(n+4, name+6);
451 if (dm) {
452 dm->major = major(stb->st_rdev);
453 dm->minor = minor(stb->st_rdev);
454 dm->name = n;
455 dm->next = devlist;
456 devlist = dm;
457 }
458 }
459 return 0;
82b27616
NB
460}
461
45e878bb
NB
462#ifndef HAVE_NFTW
463#ifdef HAVE_FTW
464int add_dev_1(const char *name, const struct stat *stb, int flag)
465{
466 return add_dev(name, stb, flag, NULL);
467}
468int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
469{
470 return ftw(path, add_dev_1, nopenfd);
471}
472#else
45e878bb
NB
473int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
474{
475 return 0;
476}
477#endif /* HAVE_FTW */
478#endif /* HAVE_NFTW */
479
dd0781e5
NB
480/*
481 * Find a block device with the right major/minor number.
b79713f8
NB
482 * If we find multiple names, choose the shortest.
483 * If we find a non-standard name, it is probably there
484 * deliberately so prefer it over a standard name.
485 * This applies only to names for MD devices.
dd0781e5 486 */
16c6fa80 487char *map_dev(int major, int minor, int create)
82b27616 488{
dd0781e5 489 struct devmap *p;
b79713f8 490 char *std = NULL, *nonstd=NULL;
e7bb5d23 491 int did_check = 0;
eed35d66 492
e81cdd9f 493 if (major == 0 && minor == 0)
eed35d66 494 return NULL;
e81cdd9f 495
e7bb5d23 496 retry:
dd0781e5 497 if (!devlist_ready) {
0a416ec3
NB
498 char *dev = "/dev";
499 struct stat stb;
eed35d66
NB
500 while(devlist) {
501 struct devmap *d = devlist;
502 devlist = d->next;
503 free(d->name);
504 free(d);
505 }
0a416ec3
NB
506 if (lstat(dev, &stb)==0 &&
507 S_ISLNK(stb.st_mode))
508 dev = "/dev/.";
509 nftw(dev, add_dev, 10, FTW_PHYS);
dd0781e5 510 devlist_ready=1;
e7bb5d23 511 did_check = 1;
dd0781e5 512 }
82b27616 513
dd0781e5
NB
514 for (p=devlist; p; p=p->next)
515 if (p->major == major &&
516 p->minor == minor) {
b79713f8
NB
517 if (is_standard(p->name, NULL)) {
518 if (std == NULL ||
519 strlen(p->name) < strlen(std))
520 std = p->name;
521 } else {
522 if (nonstd == NULL ||
523 strlen(p->name) < strlen(nonstd))
524 nonstd = p->name;
525 }
dd0781e5 526 }
e7bb5d23
NB
527 if (!std && !nonstd && !did_check) {
528 devlist_ready = 0;
529 goto retry;
530 }
16c6fa80
NB
531 if (create && !std && !nonstd) {
532 static char buf[30];
382245c3 533 snprintf(buf, sizeof(buf), "%d:%d", major, minor);
16c6fa80
NB
534 nonstd = buf;
535 }
536
b79713f8 537 return nonstd ? nonstd : std;
82b27616
NB
538}
539
4b1ac34b 540unsigned long calc_csum(void *super, int bytes)
82b27616 541{
56eb10c0 542 unsigned long long newcsum = 0;
82b27616 543 int i;
4b1ac34b
NB
544 unsigned int csum;
545 unsigned int *superc = (unsigned int*) super;
82b27616 546
4b1ac34b 547 for(i=0; i<bytes/4; i++)
82b27616
NB
548 newcsum+= superc[i];
549 csum = (newcsum& 0xffffffff) + (newcsum>>32);
570c0542 550#ifdef __alpha__
aba69144 551/* The in-kernel checksum calculation is always 16bit on
570c0542
NB
552 * the alpha, though it is 32 bit on i386...
553 * I wonder what it is elsewhere... (it uses and API in
554 * a way that it shouldn't).
555 */
556 csum = (csum & 0xffff) + (csum >> 16);
557 csum = (csum & 0xffff) + (csum >> 16);
558#endif
82b27616
NB
559 return csum;
560}
cd29a5c8 561
435d4ebb 562#ifndef MDASSEMBLE
56eb10c0 563char *human_size(long long bytes)
cd29a5c8
NB
564{
565 static char buf[30];
d5d3721e
NB
566
567 /* We convert bytes to either centi-M{ega,ibi}bytes or
568 * centi-G{igi,ibi}bytes, with appropriate rounding,
569 * and then print 1/100th of those as a decimal.
570 * We allow upto 2048Megabytes before converting to
571 * gigabytes, as that shows more precision and isn't
572 * too large a number.
573 * Terrabytes are not yet handled.
574 */
cd29a5c8 575
56eb10c0 576 if (bytes < 5000*1024)
cd29a5c8 577 buf[0]=0;
d5d3721e
NB
578 else if (bytes < 2*1024LL*1024LL*1024LL) {
579 long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2;
580 long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2;
8f23b0b3 581 snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)",
d5d3721e
NB
582 cMiB/100 , cMiB % 100,
583 cMB/100, cMB % 100);
584 } else {
585 long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2;
586 long cGB = (bytes / (1000000000LL/200LL ) +1) /2;
8f23b0b3 587 snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)",
d5d3721e
NB
588 cGiB/100 , cGiB % 100,
589 cGB/100, cGB % 100);
590 }
cd29a5c8
NB
591 return buf;
592}
e0d19036
NB
593
594char *human_size_brief(long long bytes)
595{
596 static char buf[30];
e0d19036
NB
597
598 if (bytes < 5000*1024)
8f23b0b3 599 snprintf(buf, sizeof(buf), "%ld.%02ldKiB",
bd526cee 600 (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
e0d19036
NB
601 );
602 else if (bytes < 2*1024LL*1024LL*1024LL)
8f23b0b3 603 snprintf(buf, sizeof(buf), "%ld.%02ldMiB",
e0d19036 604 (long)(bytes>>20),
bd526cee 605 (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
e0d19036
NB
606 );
607 else
8f23b0b3 608 snprintf(buf, sizeof(buf), "%ld.%02ldGiB",
e0d19036 609 (long)(bytes>>30),
bd526cee 610 (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
e0d19036
NB
611 );
612 return buf;
613}
435d4ebb 614#endif
e0d19036 615
5f8097be
NB
616unsigned long long calc_array_size(int level, int raid_disks, int layout,
617 int chunksize, unsigned long long devsize)
618{
619 int data_disks = 0;
620 switch (level) {
621 case 0: data_disks = raid_disks; break;
622 case 1: data_disks = 1; break;
623 case 4:
624 case 5: data_disks = raid_disks - 1; break;
625 case 6: data_disks = raid_disks - 2; break;
626 case 10: data_disks = raid_disks / (layout & 255) / ((layout>>8)&255);
627 break;
628 }
629 devsize &= ~(unsigned long long)((chunksize>>9)-1);
630 return data_disks * devsize;
631}
632
435d4ebb 633#if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
dd0781e5 634int get_mdp_major(void)
98c6faba 635{
dd0781e5
NB
636static int mdp_major = -1;
637 FILE *fl;
98c6faba
NB
638 char *w;
639 int have_block = 0;
640 int have_devices = 0;
641 int last_num = -1;
dd0781e5
NB
642
643 if (mdp_major != -1)
644 return mdp_major;
645 fl = fopen("/proc/devices", "r");
98c6faba 646 if (!fl)
dd0781e5 647 return -1;
98c6faba
NB
648 while ((w = conf_word(fl, 1))) {
649 if (have_block && strcmp(w, "devices:")==0)
650 have_devices = 1;
651 have_block = (strcmp(w, "Block")==0);
652 if (isdigit(w[0]))
653 last_num = atoi(w);
654 if (have_devices && strcmp(w, "mdp")==0)
655 mdp_major = last_num;
656 free(w);
657 }
658 fclose(fl);
dd0781e5 659 return mdp_major;
98c6faba
NB
660}
661
662
e0d19036 663
e0d19036
NB
664char *get_md_name(int dev)
665{
666 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
98c6faba 667 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
e0d19036
NB
668 static char devname[50];
669 struct stat stb;
98c6faba 670 dev_t rdev;
dd0781e5 671 char *dn;
98c6faba
NB
672
673 if (dev < 0) {
dd0781e5
NB
674 int mdp = get_mdp_major();
675 if (mdp < 0) return NULL;
0df46c2a 676 rdev = makedev(mdp, (-1-dev)<<6);
8f23b0b3 677 snprintf(devname, sizeof(devname), "/dev/md/d%d", -1-dev);
98c6faba
NB
678 if (stat(devname, &stb) == 0
679 && (S_IFMT&stb.st_mode) == S_IFBLK
680 && (stb.st_rdev == rdev))
681 return devname;
682 } else {
0df46c2a 683 rdev = makedev(MD_MAJOR, dev);
8f23b0b3 684 snprintf(devname, sizeof(devname), "/dev/md%d", dev);
98c6faba
NB
685 if (stat(devname, &stb) == 0
686 && (S_IFMT&stb.st_mode) == S_IFBLK
687 && (stb.st_rdev == rdev))
688 return devname;
689
8f23b0b3 690 snprintf(devname, sizeof(devname), "/dev/md/%d", dev);
98c6faba
NB
691 if (stat(devname, &stb) == 0
692 && (S_IFMT&stb.st_mode) == S_IFBLK
693 && (stb.st_rdev == rdev))
694 return devname;
695 }
16c6fa80 696 dn = map_dev(major(rdev), minor(rdev), 0);
dd0781e5
NB
697 if (dn)
698 return dn;
8f23b0b3 699 snprintf(devname, sizeof(devname), "/dev/.tmp.md%d", dev);
e0d19036 700 if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
dd0781e5
NB
701 if (errno != EEXIST)
702 return NULL;
e0d19036
NB
703
704 if (stat(devname, &stb) == 0
705 && (S_IFMT&stb.st_mode) == S_IFBLK
706 && (stb.st_rdev == rdev))
707 return devname;
708 unlink(devname);
709 return NULL;
710}
711
712void put_md_name(char *name)
713{
714 if (strncmp(name, "/dev/.tmp.md", 12)==0)
715 unlink(name);
716}
ea24acd0 717
ea24acd0
NB
718int find_free_devnum(int use_partitions)
719{
720 int devnum;
721 for (devnum = 127; devnum != 128;
722 devnum = devnum ? devnum-1 : (1<<22)-1) {
723 char *dn;
724 int _devnum;
725
726 _devnum = use_partitions ? (-1-devnum) : devnum;
727 if (mddev_busy(_devnum))
728 continue;
729 /* make sure it is new to /dev too, at least as a
730 * non-standard */
731 dn = map_dev(dev2major(_devnum), dev2minor(_devnum), 0);
732 if (dn && ! is_standard(dn, NULL))
733 continue;
734 break;
735 }
736 if (devnum == 128)
737 return NoMdDev;
738 return use_partitions ? (-1-devnum) : devnum;
739}
435d4ebb 740#endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
f9ce90ba 741
8b0dabea
NB
742int dev_open(char *dev, int flags)
743{
744 /* like 'open', but if 'dev' matches %d:%d, create a temp
745 * block device and open that
746 */
747 char *e;
748 int fd = -1;
749 char devname[32];
e81cdd9f 750 int major;
8b0dabea 751 int minor;
e81cdd9f
NB
752
753 if (!dev) return -1;
754
755 major = strtoul(dev, &e, 0);
8b0dabea
NB
756 if (e > dev && *e == ':' && e[1] &&
757 (minor = strtoul(e+1, &e, 0)) >= 0 &&
758 *e == 0) {
8c210183
NB
759 snprintf(devname, sizeof(devname), "/dev/.tmp.md.%d:%d:%d",
760 (int)getpid(), major, minor);
8b0dabea
NB
761 if (mknod(devname, S_IFBLK|0600, makedev(major, minor))==0) {
762 fd = open(devname, flags);
763 unlink(devname);
764 }
765 } else
766 fd = open(dev, flags);
767 return fd;
768}
f9ce90ba 769
a931db9e
NB
770int open_dev_excl(int devnum)
771{
772 char buf[20];
773 int i;
774
775 sprintf(buf, "%d:%d", dev2major(devnum), dev2minor(devnum));
776 for (i=0 ; i<25 ; i++) {
777 int fd = dev_open(buf, O_RDWR|O_EXCL);
778 if (fd >= 0)
779 return fd;
780 if (errno != EBUSY)
781 return fd;
782 usleep(200000);
783 }
784 return -1;
785}
786
cdddbdbc 787struct superswitch *superlist[] = { &super0, &super1, &super_ddf, &super_imsm, NULL };
f9ce90ba 788
ea24acd0 789#if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
f7dd881f
DW
790
791struct supertype supertype_container_member;
792
1686dc25 793struct supertype *super_by_fd(int fd)
f9ce90ba 794{
1686dc25
NB
795 mdu_array_info_t array;
796 int vers;
797 int minor;
798 struct supertype *st = NULL;
7e0f6979 799 struct mdinfo *sra;
142cb9e1 800 char *verstr;
1686dc25
NB
801 char version[20];
802 int i;
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 }
f7dd881f
DW
822 if (minor == -2 && verstr[0] == '/')
823 st = &supertype_container_member;
824 else
825 for (i = 0; st == NULL && superlist[i] ; i++)
826 st = superlist[i]->match_metadata_desc(verstr);
1686dc25
NB
827
828 if (sra)
829 sysfs_free(sra);
3b0896f8
NB
830 if (st)
831 st->sb = NULL;
82d9eba6 832 return st;
f9ce90ba 833}
ea24acd0
NB
834#endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
835
f9ce90ba 836
159c3a1a 837struct supertype *dup_super(struct supertype *orig)
3da92f27 838{
159c3a1a 839 struct supertype *st;
1686dc25 840
159c3a1a 841 st = malloc(sizeof(*st));
3da92f27
NB
842 if (!st)
843 return st;
159c3a1a
NB
844 st->ss = orig->ss;
845 st->max_devs = orig->max_devs;
846 st->minor_version = orig->minor_version;
847 st->sb = NULL;
848 st->info = NULL;
849 return st;
3da92f27
NB
850}
851
82d9eba6 852struct supertype *guess_super(int fd)
f9ce90ba
NB
853{
854 /* try each load_super to find the best match,
855 * and return the best superswitch
856 */
82d9eba6
NB
857 struct superswitch *ss;
858 struct supertype *st;
570c0542
NB
859 unsigned long besttime = 0;
860 int bestsuper = -1;
f9ce90ba
NB
861 int i;
862
82d9eba6
NB
863 st = malloc(sizeof(*st));
864 memset(st, 0, sizeof(*st));
f9ce90ba
NB
865 for (i=0 ; superlist[i]; i++) {
866 int rv;
867 ss = superlist[i];
f277ce36 868 st->ss = NULL;
3da92f27 869 rv = ss->load_super(st, fd, NULL);
570c0542
NB
870 if (rv == 0) {
871 struct mdinfo info;
3da92f27 872 st->ss->getinfo_super(st, &info);
570c0542
NB
873 if (bestsuper == -1 ||
874 besttime < info.array.ctime) {
875 bestsuper = i;
876 besttime = info.array.ctime;
570c0542 877 }
3da92f27 878 ss->free_super(st);
570c0542
NB
879 }
880 }
881 if (bestsuper != -1) {
882 int rv;
f277ce36 883 st->ss = NULL;
3da92f27 884 rv = superlist[bestsuper]->load_super(st, fd, NULL);
f9ce90ba 885 if (rv == 0) {
5e747af2 886 superlist[bestsuper]->free_super(st);
82d9eba6 887 return st;
f9ce90ba
NB
888 }
889 }
570c0542 890 free(st);
f9ce90ba
NB
891 return NULL;
892}
fe6729fa 893
beae1dfe
NB
894/* Return size of device in bytes */
895int get_dev_size(int fd, char *dname, unsigned long long *sizep)
896{
897 unsigned long long ldsize;
c2c9bb6f
NB
898 struct stat st;
899
900 if (fstat(fd, &st) != -1 && S_ISREG(st.st_mode))
901 ldsize = (unsigned long long)st.st_size;
902 else
beae1dfe
NB
903#ifdef BLKGETSIZE64
904 if (ioctl(fd, BLKGETSIZE64, &ldsize) != 0)
905#endif
906 {
907 unsigned long dsize;
908 if (ioctl(fd, BLKGETSIZE, &dsize) == 0) {
909 ldsize = dsize;
910 ldsize <<= 9;
911 } else {
912 if (dname)
913 fprintf(stderr, Name ": Cannot get size of %s: %s\b",
914 dname, strerror(errno));
915 return 0;
916 }
917 }
918 *sizep = ldsize;
919 return 1;
920}
8fac0577 921
8382f19b
NB
922void get_one_disk(int mdfd, mdu_array_info_t *ainf, mdu_disk_info_t *disk)
923{
924 int d;
925 ioctl(mdfd, GET_ARRAY_INFO, ainf);
926 for (d = 0 ; d < ainf->raid_disks + ainf->nr_disks ; d++)
927 if (ioctl(mdfd, GET_DISK_INFO, disk) == 0)
928 return;
929}
63152c1b 930
a322f70c
DW
931int open_container(int fd)
932{
933 /* 'fd' is a block device. Find out if it is in use
934 * by a container, and return an open fd on that container.
935 */
936 char path[256];
937 char *e;
938 DIR *dir;
939 struct dirent *de;
940 int dfd, n;
941 char buf[200];
942 int major, minor;
943 struct stat st;
944
945 if (fstat(fd, &st) != 0)
946 return -1;
947 sprintf(path, "/sys/dev/block/%d:%d/holders",
948 (int)major(st.st_rdev), (int)minor(st.st_rdev));
949 e = path + strlen(path);
950
951 dir = opendir(path);
952 if (!dir)
953 return -1;
954 while ((de = readdir(dir))) {
955 if (de->d_ino == 0)
956 continue;
957 if (de->d_name[0] == '.')
958 continue;
959 sprintf(e, "/%s/dev", de->d_name);
960 dfd = open(path, O_RDONLY);
961 if (dfd < 0)
962 continue;
963 n = read(dfd, buf, sizeof(buf));
964 close(dfd);
965 if (n <= 0 || n >= sizeof(buf))
966 continue;
967 buf[n] = 0;
968 if (sscanf(buf, "%d:%d", &major, &minor) != 2)
969 continue;
970 sprintf(buf, "%d:%d", major, minor);
971 dfd = dev_open(buf, O_RDONLY);
972 if (dfd >= 0) {
973 closedir(dir);
974 return dfd;
975 }
976 }
355726fa 977 closedir(dir);
a322f70c
DW
978 return -1;
979}
980
2f6079dc
NB
981char *devnum2devname(int num)
982{
983 char name[100];
984 if (num > 0)
985 sprintf(name, "md%d", num);
986 else
987 sprintf(name, "md_d%d", -1-num);
988 return strdup(name);
989}
990
991int fd2devnum(int fd)
992{
993 struct stat stb;
994 if (fstat(fd, &stb) == 0 &&
995 (S_IFMT&stb.st_mode)==S_IFBLK) {
996 if (major(stb.st_rdev) == MD_MAJOR)
997 return minor(stb.st_rdev);
998 else
999 return -1- (minor(stb.st_rdev)>>6);
1000 }
1001 return -1;
1002}
1003
a931db9e
NB
1004int mdmon_running(int devnum)
1005{
1006 char path[100];
1007 char pid[10];
1008 int fd;
1009 int n;
1010 sprintf(path, "/var/run/mdadm/%s.pid", devnum2devname(devnum));
1011 fd = open(path, O_RDONLY, 0);
1012
1013 if (fd < 0)
1014 return 0;
1015 n = read(fd, pid, 9);
1016 close(fd);
1017 if (n <= 0)
1018 return 0;
1019 if (kill(atoi(pid), 0) == 0)
1020 return 1;
1021 return 0;
1022}
1023
1024int signal_mdmon(int devnum)
1025{
1026 char path[100];
1027 char pid[10];
1028 int fd;
1029 int n;
1030 sprintf(path, "/var/run/mdadm/%s.pid", devnum2devname(devnum));
1031 fd = open(path, O_RDONLY, 0);
1032
1033 if (fd < 0)
1034 return 0;
1035 n = read(fd, pid, 9);
1036 close(fd);
1037 if (n <= 0)
1038 return 0;
1039 if (kill(atoi(pid), SIGUSR1) == 0)
1040 return 1;
1041 return 0;
1042}
1043
1044
1045
fe6729fa
NB
1046#ifdef __TINYC__
1047/* tinyc doesn't optimize this check in ioctl.h out ... */
1048unsigned int __invalid_size_argument_for_IOC = 0;
1049#endif
1050