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