Release mdadm-3.1.2
[thirdparty/mdadm.git] / util.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
e736b623 4 * Copyright (C) 2001-2009 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
e736b623 22 * Email: <neilb@suse.de>
64c4757e
NB
23 */
24
9a9dab36 25#include "mdadm.h"
64c4757e 26#include "md_p.h"
edd8d13c 27#include <sys/socket.h>
64c4757e 28#include <sys/utsname.h>
9fe32043 29#include <sys/wait.h>
edd8d13c 30#include <sys/un.h>
98c6faba 31#include <ctype.h>
a322f70c 32#include <dirent.h>
a931db9e 33#include <signal.h>
0a816ef9
NB
34
35/*
36 * following taken from linux/blkpg.h because they aren't
37 * anywhere else and it isn't safe to #include linux/ * stuff.
38 */
39
40#define BLKPG _IO(0x12,105)
41
42/* The argument structure */
43struct blkpg_ioctl_arg {
44 int op;
45 int flags;
46 int datalen;
47 void *data;
48};
49
50/* The subfunctions (for the op field) */
51#define BLKPG_ADD_PARTITION 1
52#define BLKPG_DEL_PARTITION 2
53
54/* Sizes of name fields. Unused at present. */
55#define BLKPG_DEVNAMELTH 64
56#define BLKPG_VOLNAMELTH 64
57
58/* The data structure for ADD_PARTITION and DEL_PARTITION */
59struct blkpg_partition {
60 long long start; /* starting offset in bytes */
61 long long length; /* length in bytes */
62 int pno; /* partition number */
63 char devname[BLKPG_DEVNAMELTH]; /* partition name, like sda5 or c0d1p2,
64 to be used in kernel messages */
65 char volname[BLKPG_VOLNAMELTH]; /* volume label */
66};
64c4757e 67
034b203a
TM
68/* partition table structures so we can check metadata position
69 * against the end of the last partition.
70 * Only handle MBR ant GPT partition tables.
71 */
72struct MBR_part_record {
73 __u8 bootable;
74 __u8 first_head;
75 __u8 first_sector;
76 __u8 first_cyl;
77 __u8 part_type;
78 __u8 last_head;
79 __u8 last_sector;
80 __u8 last_cyl;
81 __u32 first_sect_lba;
82 __u32 blocks_num;
83};
84
056b331e
N
85struct MBR {
86 __u8 pad[446];
87 struct MBR_part_record parts[4];
88 __u16 magic;
89} __attribute__((packed));
90
034b203a
TM
91struct GPT_part_entry {
92 unsigned char type_guid[16];
93 unsigned char partition_guid[16];
056b331e
N
94 __u64 starting_lba;
95 __u64 ending_lba;
034b203a
TM
96 unsigned char attr_bits[8];
97 unsigned char name[72];
056b331e
N
98} __attribute__((packed));
99
100struct GPT {
101 __u64 magic;
102 __u32 revision;
103 __u32 header_size;
104 __u32 crc;
105 __u32 pad1;
106 __u64 current_lba;
107 __u64 backup_lba;
108 __u64 first_lba;
109 __u64 last_lba;
110 __u8 guid[16];
111 __u64 part_start;
112 __u32 part_cnt;
113 __u32 part_size;
114 __u32 part_crc;
115 __u8 pad2[420];
116} __attribute__((packed));
117
118/* Force a compilation error if condition is true */
119#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
120
121/* Force a compilation error if condition is true, but also produce a
122 result (of value 0 and type size_t), so the expression can be used
123 e.g. in a structure initializer (or where-ever else comma expressions
124 aren't permitted). */
125#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
126
034b203a
TM
127
128/* MBR/GPT magic numbers */
129#define MBR_SIGNATURE_MAGIC __cpu_to_le16(0xAA55)
130#define GPT_SIGNATURE_MAGIC __cpu_to_le64(0x5452415020494645ULL)
131
034b203a
TM
132#define MBR_PARTITIONS 4
133#define MBR_GPT_PARTITION_TYPE 0xEE
034b203a 134
64c4757e
NB
135/*
136 * Parse a 128 bit uuid in 4 integers
137 * format is 32 hexx nibbles with options :.<space> separator
138 * If not exactly 32 hex digits are found, return 0
139 * else return 1
140 */
141int parse_uuid(char *str, int uuid[4])
142{
aba69144
NB
143 int hit = 0; /* number of Hex digIT */
144 int i;
145 char c;
146 for (i=0; i<4; i++) uuid[i]=0;
147
148 while ((c= *str++)) {
149 int n;
150 if (c>='0' && c<='9')
151 n = c-'0';
152 else if (c>='a' && c <= 'f')
153 n = 10 + c - 'a';
154 else if (c>='A' && c <= 'F')
155 n = 10 + c - 'A';
156 else if (strchr(":. -", c))
157 continue;
158 else return 0;
159
160 if (hit<32) {
161 uuid[hit/8] <<= 4;
162 uuid[hit/8] += n;
163 }
164 hit++;
82b27616 165 }
aba69144
NB
166 if (hit == 32)
167 return 1;
168 return 0;
64c4757e
NB
169}
170
171
172/*
173 * Get the md version number.
174 * We use the RAID_VERSION ioctl if it is supported
175 * If not, but we have a block device with major '9', we assume
176 * 0.36.0
177 *
178 * Return version number as 24 but number - assume version parts
179 * always < 255
180 */
181
182int md_get_version(int fd)
183{
184 struct stat stb;
185 mdu_version_t vers;
186
187 if (fstat(fd, &stb)<0)
188 return -1;
189 if ((S_IFMT&stb.st_mode) != S_IFBLK)
190 return -1;
191
192 if (ioctl(fd, RAID_VERSION, &vers) == 0)
682c7051 193 return (vers.major*10000) + (vers.minor*100) + vers.patchlevel;
5787fa49
NB
194 if (errno == EACCES)
195 return -1;
0df46c2a 196 if (major(stb.st_rdev) == MD_MAJOR)
682c7051 197 return (3600);
64c4757e
NB
198 return -1;
199}
200
64c4757e
NB
201int get_linux_version()
202{
203 struct utsname name;
98c6faba 204 char *cp;
64c4757e
NB
205 int a,b,c;
206 if (uname(&name) <0)
207 return -1;
208
98c6faba
NB
209 cp = name.release;
210 a = strtoul(cp, &cp, 10);
211 if (*cp != '.') return -1;
212 b = strtoul(cp+1, &cp, 10);
213 if (*cp != '.') return -1;
214 c = strtoul(cp+1, NULL, 10);
215
682c7051 216 return (a*1000000)+(b*1000)+c;
64c4757e
NB
217}
218
eb3929a4 219#ifndef MDASSEMBLE
84e11361
N
220long long parse_size(char *size)
221{
222 /* parse 'size' which should be a number optionally
223 * followed by 'K', 'M', or 'G'.
224 * Without a suffix, K is assumed.
225 * Number returned is in sectors (half-K)
226 */
227 char *c;
228 long long s = strtoll(size, &c, 10);
229 if (s > 0) {
230 switch (*c) {
231 case 'K':
232 c++;
233 default:
234 s *= 2;
235 break;
236 case 'M':
237 c++;
238 s *= 1024 * 2;
239 break;
240 case 'G':
241 c++;
242 s *= 1024 * 1024 * 2;
243 break;
244 }
245 }
246 if (*c)
247 s = 0;
248 return s;
249}
250
4a06e2c2
N
251int parse_layout_10(char *layout)
252{
253 int copies, rv;
254 char *cp;
255 /* Parse the layout string for raid10 */
256 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
257 if ((layout[0] != 'n' && layout[0] != 'f' && layout[0] != 'o') ||
258 (copies = strtoul(layout+1, &cp, 10)) < 1 ||
259 copies > 200 ||
260 *cp)
261 return -1;
262 if (layout[0] == 'n')
263 rv = 256 + copies;
264 else if (layout[0] == 'o')
265 rv = 0x10000 + (copies<<8) + 1;
266 else
267 rv = 1 + (copies<<8);
268 return rv;
269}
270
271int parse_layout_faulty(char *layout)
272{
273 /* Parse the layout string for 'faulty' */
274 int ln = strcspn(layout, "0123456789");
275 char *m = strdup(layout);
276 int mode;
277 m[ln] = 0;
278 mode = map_name(faultylayout, m);
279 if (mode == UnSet)
280 return -1;
281
282 return mode | (atoi(layout+ln)<< ModeShift);
283}
eb3929a4 284#endif
4a06e2c2 285
0430ed48
NB
286void remove_partitions(int fd)
287{
288 /* remove partitions from this block devices.
289 * This is used for components added to an array
290 */
291#ifdef BLKPG_DEL_PARTITION
292 struct blkpg_ioctl_arg a;
293 struct blkpg_partition p;
294
295 a.op = BLKPG_DEL_PARTITION;
296 a.data = (void*)&p;
297 a.datalen = sizeof(p);
298 a.flags = 0;
299 memset(a.data, 0, a.datalen);
300 for (p.pno=0; p.pno < 16; p.pno++)
301 ioctl(fd, BLKPG, &a);
302#endif
303}
304
583315d9 305int enough(int level, int raid_disks, int layout, int clean,
265e0f17 306 char *avail, int avail_disks)
64c4757e 307{
265e0f17 308 int copies, first;
64c4757e 309 switch (level) {
265e0f17
NB
310 case 10:
311 /* This is the tricky one - we need to check
312 * which actual disks are present.
313 */
702b557b 314 copies = (layout&255)* ((layout>>8) & 255);
265e0f17
NB
315 first=0;
316 do {
317 /* there must be one of the 'copies' form 'first' */
318 int n = copies;
319 int cnt=0;
320 while (n--) {
321 if (avail[first])
322 cnt++;
323 first = (first+1) % raid_disks;
324 }
325 if (cnt == 0)
326 return 0;
327
328 } while (first != 0);
329 return 1;
e5329c37 330
df0d4ea0 331 case LEVEL_MULTIPATH:
e0d19036 332 return avail_disks>= 1;
df0d4ea0 333 case LEVEL_LINEAR:
64c4757e
NB
334 case 0:
335 return avail_disks == raid_disks;
336 case 1:
337 return avail_disks >= 1;
338 case 4:
339 case 5:
583315d9
NB
340 if (clean)
341 return avail_disks >= raid_disks-1;
342 else
343 return avail_disks >= raid_disks;
98c6faba 344 case 6:
583315d9
NB
345 if (clean)
346 return avail_disks >= raid_disks-2;
347 else
348 return avail_disks >= raid_disks;
64c4757e
NB
349 default:
350 return 0;
351 }
352}
353
36ba7d48 354const int uuid_match_any[4] = { ~0, ~0, ~0, ~0 };
f277ce36 355int same_uuid(int a[4], int b[4], int swapuuid)
64c4757e 356{
36ba7d48
DW
357 if (memcmp(a, uuid_match_any, sizeof(int[4])) == 0 ||
358 memcmp(b, uuid_match_any, sizeof(int[4])) == 0)
359 return 1;
360
f277ce36
NB
361 if (swapuuid) {
362 /* parse uuids are hostendian.
363 * uuid's from some superblocks are big-ending
aba69144 364 * if there is a difference, we need to swap..
f277ce36
NB
365 */
366 unsigned char *ac = (unsigned char *)a;
367 unsigned char *bc = (unsigned char *)b;
368 int i;
369 for (i=0; i<16; i+= 4) {
370 if (ac[i+0] != bc[i+3] ||
371 ac[i+1] != bc[i+2] ||
372 ac[i+2] != bc[i+1] ||
373 ac[i+3] != bc[i+0])
374 return 0;
375 }
376 return 1;
377 } else {
378 if (a[0]==b[0] &&
379 a[1]==b[1] &&
380 a[2]==b[2] &&
381 a[3]==b[3])
382 return 1;
383 return 0;
384 }
64c4757e 385}
350f29f9
NB
386void copy_uuid(void *a, int b[4], int swapuuid)
387{
388 if (swapuuid) {
389 /* parse uuids are hostendian.
390 * uuid's from some superblocks are big-ending
391 * if there is a difference, we need to swap..
392 */
393 unsigned char *ac = (unsigned char *)a;
394 unsigned char *bc = (unsigned char *)b;
395 int i;
396 for (i=0; i<16; i+= 4) {
397 ac[i+0] = bc[i+3];
398 ac[i+1] = bc[i+2];
399 ac[i+2] = bc[i+1];
400 ac[i+3] = bc[i+0];
401 }
402 } else
403 memcpy(a, b, 16);
404}
64c4757e 405
aae5a112 406char *__fname_from_uuid(int id[4], int swap, char *buf, char sep)
d7288ddc 407{
9968e376 408 int i, j;
d7288ddc
N
409 char uuid[16];
410 char *c = buf;
411 strcpy(c, "UUID-");
412 c += strlen(c);
aae5a112 413 copy_uuid(uuid, id, swap);
9968e376 414 for (i = 0; i < 4; i++) {
9968e376 415 if (i)
ff54de6e 416 *c++ = sep;
9968e376
DW
417 for (j = 3; j >= 0; j--) {
418 sprintf(c,"%02x", (unsigned char) uuid[j+4*i]);
419 c+= 2;
420 }
d7288ddc
N
421 }
422 return buf;
aae5a112
DW
423
424}
425
426char *fname_from_uuid(struct supertype *st, struct mdinfo *info, char *buf, char sep)
427{
428 return __fname_from_uuid(info->uuid, st->ss->swapuuid, buf, sep);
d7288ddc
N
429}
430
435d4ebb 431#ifndef MDASSEMBLE
682c7051
NB
432int check_ext2(int fd, char *name)
433{
434 /*
435 * Check for an ext2fs file system.
436 * Superblock is always 1K at 1K offset
437 *
438 * s_magic is le16 at 56 == 0xEF53
439 * report mtime - le32 at 44
440 * blocks - le32 at 4
441 * logblksize - le32 at 24
442 */
443 unsigned char sb[1024];
444 time_t mtime;
445 int size, bsize;
446 if (lseek(fd, 1024,0)!= 1024)
447 return 0;
448 if (read(fd, sb, 1024)!= 1024)
449 return 0;
450 if (sb[56] != 0x53 || sb[57] != 0xef)
451 return 0;
452
453 mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8;
454 bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8;
455 size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8;
456 fprintf(stderr, Name ": %s appears to contain an ext2fs file system\n",
457 name);
458 fprintf(stderr," size=%dK mtime=%s",
459 size*(1<<bsize), ctime(&mtime));
460 return 1;
461}
462
463int check_reiser(int fd, char *name)
464{
465 /*
466 * superblock is at 64K
467 * size is 1024;
468 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
469 *
470 */
471 unsigned char sb[1024];
881990a2 472 unsigned long size;
682c7051
NB
473 if (lseek(fd, 64*1024, 0) != 64*1024)
474 return 0;
475 if (read(fd, sb, 1024) != 1024)
476 return 0;
a46f4061
NB
477 if (strncmp((char*)sb+52, "ReIsErFs",8)!=0 &&
478 strncmp((char*)sb+52, "ReIsEr2Fs",9)!=0)
682c7051
NB
479 return 0;
480 fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name);
481 size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8;
881990a2 482 fprintf(stderr, " size = %luK\n", size*4);
aba69144 483
682c7051
NB
484 return 1;
485}
486
487int check_raid(int fd, char *name)
488{
4b1ac34b 489 struct mdinfo info;
682c7051 490 time_t crtime;
d078d77c 491 char *level;
82d9eba6 492 struct supertype *st = guess_super(fd);
f9ce90ba 493
82d9eba6 494 if (!st) return 0;
3da92f27 495 st->ss->load_super(st, fd, name);
82d9eba6
NB
496 /* Looks like a raid array .. */
497 fprintf(stderr, Name ": %s appears to be part of a raid array:\n",
498 name);
3da92f27
NB
499 st->ss->getinfo_super(st, &info);
500 st->ss->free_super(st);
82d9eba6 501 crtime = info.array.ctime;
d078d77c
NB
502 level = map_num(pers, info.array.level);
503 if (!level) level = "-unknown-";
504 fprintf(stderr, " level=%s devices=%d ctime=%s",
505 level, info.array.raid_disks, ctime(&crtime));
82d9eba6 506 return 1;
682c7051
NB
507}
508
682c7051
NB
509int ask(char *mesg)
510{
511 char *add = "";
512 int i;
513 for (i=0; i<5; i++) {
514 char buf[100];
515 fprintf(stderr, "%s%s", mesg, add);
516 fflush(stderr);
517 if (fgets(buf, 100, stdin)==NULL)
518 return 0;
519 if (buf[0]=='y' || buf[0]=='Y')
520 return 1;
521 if (buf[0]=='n' || buf[0]=='N')
522 return 0;
523 add = "(y/n) ";
524 }
525 fprintf(stderr, Name ": assuming 'no'\n");
526 return 0;
527}
435d4ebb 528#endif /* MDASSEMBLE */
682c7051
NB
529
530char *map_num(mapping_t *map, int num)
531{
532 while (map->name) {
533 if (map->num == num)
534 return map->name;
535 map++;
536 }
537 return NULL;
538}
539
540int map_name(mapping_t *map, char *name)
541{
542 while (map->name) {
543 if (strcmp(map->name, name)==0)
544 return map->num;
545 map++;
546 }
98c6faba 547 return UnSet;
682c7051 548}
82b27616 549
e5329c37 550
8d80900b 551int is_standard(char *dev, int *nump)
e5329c37
NB
552{
553 /* tests if dev is a "standard" md dev name.
554 * i.e if the last component is "/dNN" or "/mdNN",
aba69144 555 * where NN is a string of digits
598f0d58
NB
556 * Returns 1 if a partitionable standard,
557 * -1 if non-partitonable,
558 * 0 if not a standard name.
e5329c37 559 */
8d80900b
NB
560 char *d = strrchr(dev, '/');
561 int type=0;
562 int num;
563 if (!d)
e5329c37 564 return 0;
8d80900b
NB
565 if (strncmp(d, "/d",2)==0)
566 d += 2, type=1; /* /dev/md/dN{pM} */
567 else if (strncmp(d, "/md_d", 5)==0)
2b4ca8f0 568 d += 5, type=1; /* /dev/md_dN{pM} */
8d80900b
NB
569 else if (strncmp(d, "/md", 3)==0)
570 d += 3, type=-1; /* /dev/mdN */
571 else if (d-dev > 3 && strncmp(d-2, "md/", 3)==0)
5a6d1148 572 d += 1, type=-1; /* /dev/md/N */
e5329c37
NB
573 else
574 return 0;
8d80900b 575 if (!*d)
e5329c37 576 return 0;
8d80900b
NB
577 num = atoi(d);
578 while (isdigit(*d))
579 d++;
580 if (*d)
e5329c37 581 return 0;
8d80900b
NB
582 if (nump) *nump = num;
583
584 return type;
e5329c37
NB
585}
586
587
82b27616
NB
588/*
589 * convert a major/minor pair for a block device into a name in /dev, if possible.
590 * On the first call, walk /dev collecting name.
591 * Put them in a simple linked listfor now.
592 */
593struct devmap {
594 int major, minor;
595 char *name;
596 struct devmap *next;
597} *devlist = NULL;
598int devlist_ready = 0;
599
82b27616
NB
600int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
601{
bed256c2 602 struct stat st;
bf68e9d9 603
bed256c2 604 if (S_ISLNK(stb->st_mode)) {
bf68e9d9
DW
605 if (stat(name, &st) != 0)
606 return 0;
bed256c2 607 stb = &st;
82b27616 608 }
bed256c2
NB
609
610 if ((stb->st_mode&S_IFMT)== S_IFBLK) {
611 char *n = strdup(name);
612 struct devmap *dm = malloc(sizeof(*dm));
613 if (strncmp(n, "/dev/./", 7)==0)
614 strcpy(n+4, name+6);
615 if (dm) {
616 dm->major = major(stb->st_rdev);
617 dm->minor = minor(stb->st_rdev);
618 dm->name = n;
619 dm->next = devlist;
620 devlist = dm;
621 }
622 }
623 return 0;
82b27616
NB
624}
625
45e878bb
NB
626#ifndef HAVE_NFTW
627#ifdef HAVE_FTW
628int add_dev_1(const char *name, const struct stat *stb, int flag)
629{
630 return add_dev(name, stb, flag, NULL);
631}
632int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
633{
634 return ftw(path, add_dev_1, nopenfd);
635}
636#else
45e878bb
NB
637int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
638{
639 return 0;
640}
641#endif /* HAVE_FTW */
642#endif /* HAVE_NFTW */
643
dd0781e5
NB
644/*
645 * Find a block device with the right major/minor number.
b79713f8 646 * If we find multiple names, choose the shortest.
70ef16db 647 * If we find a name in /dev/md/, we prefer that.
b79713f8 648 * This applies only to names for MD devices.
dd0781e5 649 */
16c6fa80 650char *map_dev(int major, int minor, int create)
82b27616 651{
dd0781e5 652 struct devmap *p;
70ef16db 653 char *regular = NULL, *preferred=NULL;
e7bb5d23 654 int did_check = 0;
eed35d66 655
e81cdd9f 656 if (major == 0 && minor == 0)
eed35d66 657 return NULL;
e81cdd9f 658
e7bb5d23 659 retry:
dd0781e5 660 if (!devlist_ready) {
0a416ec3
NB
661 char *dev = "/dev";
662 struct stat stb;
eed35d66
NB
663 while(devlist) {
664 struct devmap *d = devlist;
665 devlist = d->next;
666 free(d->name);
667 free(d);
668 }
0a416ec3
NB
669 if (lstat(dev, &stb)==0 &&
670 S_ISLNK(stb.st_mode))
671 dev = "/dev/.";
672 nftw(dev, add_dev, 10, FTW_PHYS);
dd0781e5 673 devlist_ready=1;
e7bb5d23 674 did_check = 1;
dd0781e5 675 }
82b27616 676
dd0781e5
NB
677 for (p=devlist; p; p=p->next)
678 if (p->major == major &&
679 p->minor == minor) {
70ef16db
N
680 if (strncmp(p->name, "/dev/md/",8) == 0) {
681 if (preferred == NULL ||
682 strlen(p->name) < strlen(preferred))
683 preferred = p->name;
b79713f8 684 } else {
70ef16db
N
685 if (regular == NULL ||
686 strlen(p->name) < strlen(regular))
687 regular = p->name;
b79713f8 688 }
dd0781e5 689 }
70ef16db 690 if (!regular && !preferred && !did_check) {
e7bb5d23
NB
691 devlist_ready = 0;
692 goto retry;
693 }
70ef16db 694 if (create && !regular && !preferred) {
16c6fa80 695 static char buf[30];
382245c3 696 snprintf(buf, sizeof(buf), "%d:%d", major, minor);
70ef16db 697 regular = buf;
16c6fa80
NB
698 }
699
70ef16db 700 return preferred ? preferred : regular;
82b27616
NB
701}
702
4b1ac34b 703unsigned long calc_csum(void *super, int bytes)
82b27616 704{
56eb10c0 705 unsigned long long newcsum = 0;
82b27616 706 int i;
4b1ac34b
NB
707 unsigned int csum;
708 unsigned int *superc = (unsigned int*) super;
82b27616 709
4b1ac34b 710 for(i=0; i<bytes/4; i++)
82b27616
NB
711 newcsum+= superc[i];
712 csum = (newcsum& 0xffffffff) + (newcsum>>32);
570c0542 713#ifdef __alpha__
aba69144 714/* The in-kernel checksum calculation is always 16bit on
570c0542
NB
715 * the alpha, though it is 32 bit on i386...
716 * I wonder what it is elsewhere... (it uses and API in
717 * a way that it shouldn't).
718 */
719 csum = (csum & 0xffff) + (csum >> 16);
720 csum = (csum & 0xffff) + (csum >> 16);
721#endif
82b27616
NB
722 return csum;
723}
cd29a5c8 724
435d4ebb 725#ifndef MDASSEMBLE
56eb10c0 726char *human_size(long long bytes)
cd29a5c8
NB
727{
728 static char buf[30];
d5d3721e
NB
729
730 /* We convert bytes to either centi-M{ega,ibi}bytes or
731 * centi-G{igi,ibi}bytes, with appropriate rounding,
732 * and then print 1/100th of those as a decimal.
733 * We allow upto 2048Megabytes before converting to
734 * gigabytes, as that shows more precision and isn't
735 * too large a number.
736 * Terrabytes are not yet handled.
737 */
cd29a5c8 738
56eb10c0 739 if (bytes < 5000*1024)
cd29a5c8 740 buf[0]=0;
d5d3721e
NB
741 else if (bytes < 2*1024LL*1024LL*1024LL) {
742 long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2;
743 long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2;
8f23b0b3 744 snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)",
d5d3721e
NB
745 cMiB/100 , cMiB % 100,
746 cMB/100, cMB % 100);
747 } else {
748 long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2;
749 long cGB = (bytes / (1000000000LL/200LL ) +1) /2;
8f23b0b3 750 snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)",
d5d3721e
NB
751 cGiB/100 , cGiB % 100,
752 cGB/100, cGB % 100);
753 }
cd29a5c8
NB
754 return buf;
755}
e0d19036
NB
756
757char *human_size_brief(long long bytes)
758{
759 static char buf[30];
e0d19036
NB
760
761 if (bytes < 5000*1024)
8f23b0b3 762 snprintf(buf, sizeof(buf), "%ld.%02ldKiB",
bd526cee 763 (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
e0d19036
NB
764 );
765 else if (bytes < 2*1024LL*1024LL*1024LL)
8f23b0b3 766 snprintf(buf, sizeof(buf), "%ld.%02ldMiB",
e0d19036 767 (long)(bytes>>20),
bd526cee 768 (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
e0d19036
NB
769 );
770 else
8f23b0b3 771 snprintf(buf, sizeof(buf), "%ld.%02ldGiB",
e0d19036 772 (long)(bytes>>30),
bd526cee 773 (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
e0d19036
NB
774 );
775 return buf;
776}
e4965ef8
N
777
778void print_r10_layout(int layout)
779{
780 int near = layout & 255;
781 int far = (layout >> 8) & 255;
782 int offset = (layout&0x10000);
783 char *sep = "";
784
785 if (near != 1) {
786 printf("%s near=%d", sep, near);
787 sep = ",";
788 }
789 if (far != 1)
790 printf("%s %s=%d", sep, offset?"offset":"far", far);
791 if (near*far == 1)
792 printf("NO REDUNDANCY");
793}
435d4ebb 794#endif
e0d19036 795
5f8097be
NB
796unsigned long long calc_array_size(int level, int raid_disks, int layout,
797 int chunksize, unsigned long long devsize)
798{
799 int data_disks = 0;
800 switch (level) {
801 case 0: data_disks = raid_disks; break;
802 case 1: data_disks = 1; break;
803 case 4:
804 case 5: data_disks = raid_disks - 1; break;
805 case 6: data_disks = raid_disks - 2; break;
806 case 10: data_disks = raid_disks / (layout & 255) / ((layout>>8)&255);
807 break;
808 }
809 devsize &= ~(unsigned long long)((chunksize>>9)-1);
810 return data_disks * devsize;
811}
812
dd0781e5 813int get_mdp_major(void)
98c6faba 814{
dd0781e5
NB
815static int mdp_major = -1;
816 FILE *fl;
98c6faba
NB
817 char *w;
818 int have_block = 0;
819 int have_devices = 0;
820 int last_num = -1;
dd0781e5
NB
821
822 if (mdp_major != -1)
823 return mdp_major;
824 fl = fopen("/proc/devices", "r");
98c6faba 825 if (!fl)
dd0781e5 826 return -1;
98c6faba
NB
827 while ((w = conf_word(fl, 1))) {
828 if (have_block && strcmp(w, "devices:")==0)
829 have_devices = 1;
830 have_block = (strcmp(w, "Block")==0);
831 if (isdigit(w[0]))
832 last_num = atoi(w);
833 if (have_devices && strcmp(w, "mdp")==0)
834 mdp_major = last_num;
835 free(w);
836 }
837 fclose(fl);
dd0781e5 838 return mdp_major;
98c6faba
NB
839}
840
0e600426 841#if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
e0d19036
NB
842char *get_md_name(int dev)
843{
844 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
98c6faba 845 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
e0d19036
NB
846 static char devname[50];
847 struct stat stb;
98c6faba 848 dev_t rdev;
dd0781e5 849 char *dn;
98c6faba
NB
850
851 if (dev < 0) {
dd0781e5
NB
852 int mdp = get_mdp_major();
853 if (mdp < 0) return NULL;
0df46c2a 854 rdev = makedev(mdp, (-1-dev)<<6);
8f23b0b3 855 snprintf(devname, sizeof(devname), "/dev/md/d%d", -1-dev);
98c6faba
NB
856 if (stat(devname, &stb) == 0
857 && (S_IFMT&stb.st_mode) == S_IFBLK
858 && (stb.st_rdev == rdev))
859 return devname;
860 } else {
0df46c2a 861 rdev = makedev(MD_MAJOR, dev);
8f23b0b3 862 snprintf(devname, sizeof(devname), "/dev/md%d", dev);
98c6faba
NB
863 if (stat(devname, &stb) == 0
864 && (S_IFMT&stb.st_mode) == S_IFBLK
865 && (stb.st_rdev == rdev))
866 return devname;
867
8f23b0b3 868 snprintf(devname, sizeof(devname), "/dev/md/%d", dev);
98c6faba
NB
869 if (stat(devname, &stb) == 0
870 && (S_IFMT&stb.st_mode) == S_IFBLK
871 && (stb.st_rdev == rdev))
872 return devname;
873 }
16c6fa80 874 dn = map_dev(major(rdev), minor(rdev), 0);
dd0781e5
NB
875 if (dn)
876 return dn;
8f23b0b3 877 snprintf(devname, sizeof(devname), "/dev/.tmp.md%d", dev);
e0d19036 878 if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
dd0781e5
NB
879 if (errno != EEXIST)
880 return NULL;
e0d19036
NB
881
882 if (stat(devname, &stb) == 0
883 && (S_IFMT&stb.st_mode) == S_IFBLK
884 && (stb.st_rdev == rdev))
885 return devname;
886 unlink(devname);
887 return NULL;
888}
889
890void put_md_name(char *name)
891{
892 if (strncmp(name, "/dev/.tmp.md", 12)==0)
893 unlink(name);
894}
ea24acd0 895
ea24acd0
NB
896int find_free_devnum(int use_partitions)
897{
898 int devnum;
899 for (devnum = 127; devnum != 128;
a56fb7ec 900 devnum = devnum ? devnum-1 : (1<<20)-1) {
ea24acd0
NB
901 char *dn;
902 int _devnum;
903
904 _devnum = use_partitions ? (-1-devnum) : devnum;
905 if (mddev_busy(_devnum))
906 continue;
907 /* make sure it is new to /dev too, at least as a
908 * non-standard */
909 dn = map_dev(dev2major(_devnum), dev2minor(_devnum), 0);
910 if (dn && ! is_standard(dn, NULL))
911 continue;
912 break;
913 }
914 if (devnum == 128)
915 return NoMdDev;
916 return use_partitions ? (-1-devnum) : devnum;
917}
435d4ebb 918#endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
f9ce90ba 919
8b0dabea
NB
920int dev_open(char *dev, int flags)
921{
922 /* like 'open', but if 'dev' matches %d:%d, create a temp
923 * block device and open that
924 */
925 char *e;
926 int fd = -1;
927 char devname[32];
e81cdd9f 928 int major;
8b0dabea 929 int minor;
e81cdd9f
NB
930
931 if (!dev) return -1;
932
933 major = strtoul(dev, &e, 0);
8b0dabea
NB
934 if (e > dev && *e == ':' && e[1] &&
935 (minor = strtoul(e+1, &e, 0)) >= 0 &&
936 *e == 0) {
8c210183
NB
937 snprintf(devname, sizeof(devname), "/dev/.tmp.md.%d:%d:%d",
938 (int)getpid(), major, minor);
8b0dabea 939 if (mknod(devname, S_IFBLK|0600, makedev(major, minor))==0) {
6416d527 940 fd = open(devname, flags|O_DIRECT);
8b0dabea
NB
941 unlink(devname);
942 }
943 } else
6416d527 944 fd = open(dev, flags|O_DIRECT);
8b0dabea
NB
945 return fd;
946}
f9ce90ba 947
e8a70c89
N
948int open_dev(int devnum)
949{
950 char buf[20];
951
952 sprintf(buf, "%d:%d", dev2major(devnum), dev2minor(devnum));
953 return dev_open(buf, O_RDWR);
954}
955
a931db9e
NB
956int open_dev_excl(int devnum)
957{
958 char buf[20];
959 int i;
960
961 sprintf(buf, "%d:%d", dev2major(devnum), dev2minor(devnum));
962 for (i=0 ; i<25 ; i++) {
963 int fd = dev_open(buf, O_RDWR|O_EXCL);
964 if (fd >= 0)
965 return fd;
966 if (errno != EBUSY)
967 return fd;
968 usleep(200000);
969 }
970 return -1;
971}
972
9008ed1c
N
973int same_dev(char *one, char *two)
974{
975 struct stat st1, st2;
976 if (stat(one, &st1) != 0)
977 return 0;
978 if (stat(two, &st2) != 0)
979 return 0;
980 if ((st1.st_mode & S_IFMT) != S_IFBLK)
981 return 0;
982 if ((st2.st_mode & S_IFMT) != S_IFBLK)
983 return 0;
984 return st1.st_rdev == st2.st_rdev;
985}
986
a7c6e3fb 987void wait_for(char *dev, int fd)
a714580e
N
988{
989 int i;
a7c6e3fb
N
990 struct stat stb_want;
991
992 if (fstat(fd, &stb_want) != 0 ||
993 (stb_want.st_mode & S_IFMT) != S_IFBLK)
994 return;
a714580e
N
995
996 for (i=0 ; i<25 ; i++) {
997 struct stat stb;
a7c6e3fb
N
998 if (stat(dev, &stb) == 0 &&
999 (stb.st_mode & S_IFMT) == S_IFBLK &&
1000 (stb.st_rdev == stb_want.st_rdev))
a714580e
N
1001 return;
1002 usleep(200000);
1003 }
436305c6
DW
1004 if (i == 25)
1005 dprintf("%s: timeout waiting for %s\n", __func__, dev);
a714580e
N
1006}
1007
cdddbdbc 1008struct superswitch *superlist[] = { &super0, &super1, &super_ddf, &super_imsm, NULL };
f9ce90ba 1009
ea24acd0 1010#if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
f7dd881f 1011
1686dc25 1012struct supertype *super_by_fd(int fd)
f9ce90ba 1013{
1686dc25
NB
1014 mdu_array_info_t array;
1015 int vers;
1016 int minor;
1017 struct supertype *st = NULL;
7e0f6979 1018 struct mdinfo *sra;
142cb9e1 1019 char *verstr;
1686dc25
NB
1020 char version[20];
1021 int i;
f7e7067b 1022 char *subarray = NULL;
1686dc25
NB
1023
1024 sra = sysfs_read(fd, 0, GET_VERSION);
1025
1026 if (sra) {
7e0f6979
NB
1027 vers = sra->array.major_version;
1028 minor = sra->array.minor_version;
142cb9e1 1029 verstr = sra->text_version;
1686dc25
NB
1030 } else {
1031 if (ioctl(fd, GET_ARRAY_INFO, &array))
1032 array.major_version = array.minor_version = 0;
1033 vers = array.major_version;
1034 minor = array.minor_version;
142cb9e1 1035 verstr = "";
6fbba4c9 1036 }
82d9eba6 1037
1686dc25
NB
1038 if (vers != -1) {
1039 sprintf(version, "%d.%d", vers, minor);
1040 verstr = version;
6fbba4c9 1041 }
3c558363 1042 if (minor == -2 && is_subarray(verstr)) {
f7e7067b
NB
1043 char *dev = verstr+1;
1044 subarray = strchr(dev, '/');
1045 int devnum;
1046 if (subarray)
1047 *subarray++ = '\0';
77472ff8 1048 devnum = devname2devnum(dev);
f7e7067b
NB
1049 subarray = strdup(subarray);
1050 if (sra)
1051 sysfs_free(sra);
1052 sra = sysfs_read(-1, devnum, GET_VERSION);
603f24a0
N
1053 if (sra && sra->text_version[0])
1054 verstr = sra->text_version;
1055 else
1056 verstr = "-no-metadata-";
f7e7067b
NB
1057 }
1058
1059 for (i = 0; st == NULL && superlist[i] ; i++)
1060 st = superlist[i]->match_metadata_desc(verstr);
1686dc25
NB
1061
1062 if (sra)
1063 sysfs_free(sra);
f7e7067b 1064 if (st) {
3b0896f8 1065 st->sb = NULL;
f7e7067b
NB
1066 if (subarray) {
1067 strncpy(st->subarray, subarray, 32);
1068 st->subarray[31] = 0;
1069 free(subarray);
1070 } else
1071 st->subarray[0] = 0;
1072 }
82d9eba6 1073 return st;
f9ce90ba 1074}
ea24acd0
NB
1075#endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
1076
f9ce90ba 1077
159c3a1a 1078struct supertype *dup_super(struct supertype *orig)
3da92f27 1079{
159c3a1a 1080 struct supertype *st;
1686dc25 1081
d2ca6449
NB
1082 if (!orig)
1083 return orig;
159c3a1a 1084 st = malloc(sizeof(*st));
3da92f27
NB
1085 if (!st)
1086 return st;
ef609477 1087 memset(st, 0, sizeof(*st));
159c3a1a
NB
1088 st->ss = orig->ss;
1089 st->max_devs = orig->max_devs;
1090 st->minor_version = orig->minor_version;
f7e7067b 1091 strcpy(st->subarray, orig->subarray);
159c3a1a
NB
1092 st->sb = NULL;
1093 st->info = NULL;
1094 return st;
3da92f27
NB
1095}
1096
82d9eba6 1097struct supertype *guess_super(int fd)
f9ce90ba
NB
1098{
1099 /* try each load_super to find the best match,
1100 * and return the best superswitch
1101 */
82d9eba6
NB
1102 struct superswitch *ss;
1103 struct supertype *st;
570c0542
NB
1104 unsigned long besttime = 0;
1105 int bestsuper = -1;
f9ce90ba
NB
1106 int i;
1107
82d9eba6 1108 st = malloc(sizeof(*st));
f9ce90ba
NB
1109 for (i=0 ; superlist[i]; i++) {
1110 int rv;
1111 ss = superlist[i];
ef609477 1112 memset(st, 0, sizeof(*st));
3da92f27 1113 rv = ss->load_super(st, fd, NULL);
570c0542
NB
1114 if (rv == 0) {
1115 struct mdinfo info;
3da92f27 1116 st->ss->getinfo_super(st, &info);
570c0542
NB
1117 if (bestsuper == -1 ||
1118 besttime < info.array.ctime) {
1119 bestsuper = i;
1120 besttime = info.array.ctime;
570c0542 1121 }
3da92f27 1122 ss->free_super(st);
570c0542
NB
1123 }
1124 }
1125 if (bestsuper != -1) {
1126 int rv;
ef609477 1127 memset(st, 0, sizeof(*st));
3da92f27 1128 rv = superlist[bestsuper]->load_super(st, fd, NULL);
f9ce90ba 1129 if (rv == 0) {
5e747af2 1130 superlist[bestsuper]->free_super(st);
82d9eba6 1131 return st;
f9ce90ba
NB
1132 }
1133 }
570c0542 1134 free(st);
f9ce90ba
NB
1135 return NULL;
1136}
fe6729fa 1137
beae1dfe
NB
1138/* Return size of device in bytes */
1139int get_dev_size(int fd, char *dname, unsigned long long *sizep)
1140{
1141 unsigned long long ldsize;
c2c9bb6f
NB
1142 struct stat st;
1143
1144 if (fstat(fd, &st) != -1 && S_ISREG(st.st_mode))
1145 ldsize = (unsigned long long)st.st_size;
1146 else
beae1dfe
NB
1147#ifdef BLKGETSIZE64
1148 if (ioctl(fd, BLKGETSIZE64, &ldsize) != 0)
1149#endif
1150 {
1151 unsigned long dsize;
1152 if (ioctl(fd, BLKGETSIZE, &dsize) == 0) {
1153 ldsize = dsize;
1154 ldsize <<= 9;
1155 } else {
1156 if (dname)
1157 fprintf(stderr, Name ": Cannot get size of %s: %s\b",
1158 dname, strerror(errno));
1159 return 0;
1160 }
1161 }
1162 *sizep = ldsize;
1163 return 1;
1164}
8fac0577 1165
034b203a
TM
1166
1167/* Sets endofpart parameter to the last block used by the last GPT partition on the device.
1168 * Returns: 1 if successful
1169 * -1 for unknown partition type
1170 * 0 for other errors
1171 */
1172static int get_gpt_last_partition_end(int fd, unsigned long long *endofpart)
1173{
056b331e 1174 struct GPT gpt;
034b203a
TM
1175 unsigned char buf[512];
1176 unsigned char empty_gpt_entry[16]= {0};
1177 struct GPT_part_entry *part;
1178 unsigned long long curr_part_end;
1179 unsigned all_partitions, entry_size;
1180 int part_nr;
1181
1182 *endofpart = 0;
1183
056b331e 1184 BUILD_BUG_ON(sizeof(gpt) != 512);
034b203a
TM
1185 /* read GPT header */
1186 lseek(fd, 512, SEEK_SET);
056b331e 1187 if (read(fd, &gpt, 512) != 512)
034b203a
TM
1188 return 0;
1189
1190 /* get the number of partition entries and the entry size */
056b331e
N
1191 all_partitions = __le32_to_cpu(gpt.part_cnt);
1192 entry_size = __le32_to_cpu(gpt.part_size);
034b203a
TM
1193
1194 /* Check GPT signature*/
056b331e 1195 if (gpt.magic != GPT_SIGNATURE_MAGIC)
034b203a
TM
1196 return -1;
1197
1198 /* sanity checks */
1199 if (all_partitions > 1024 ||
1200 entry_size > 512)
1201 return -1;
1202
1203 /* read first GPT partition entries */
1204 if (read(fd, buf, 512) != 512)
1205 return 0;
1206
1207 part = (struct GPT_part_entry*)buf;
1208
1209 for (part_nr=0; part_nr < all_partitions; part_nr++) {
1210 /* is this valid partition? */
1211 if (memcmp(part->type_guid, empty_gpt_entry, 16) != 0) {
1212 /* check the last lba for the current partition */
056b331e 1213 curr_part_end = __le64_to_cpu(part->ending_lba);
034b203a
TM
1214 if (curr_part_end > *endofpart)
1215 *endofpart = curr_part_end;
1216 }
1217
1218 part = (struct GPT_part_entry*)((unsigned char*)part + entry_size);
1219
1220 if ((unsigned char *)part >= buf + 512) {
1221 if (read(fd, buf, 512) != 512)
1222 return 0;
1223 part = (struct GPT_part_entry*)buf;
1224 }
1225 }
1226 return 1;
1227}
1228
1229/* Sets endofpart parameter to the last block used by the last partition on the device.
1230 * Returns: 1 if successful
1231 * -1 for unknown partition type
1232 * 0 for other errors
1233 */
1234static int get_last_partition_end(int fd, unsigned long long *endofpart)
1235{
056b331e 1236 struct MBR boot_sect;
034b203a
TM
1237 struct MBR_part_record *part;
1238 unsigned long long curr_part_end;
1239 int part_nr;
1240 int retval = 0;
1241
1242 *endofpart = 0;
1243
056b331e 1244 BUILD_BUG_ON(sizeof(boot_sect) != 512);
034b203a
TM
1245 /* read MBR */
1246 lseek(fd, 0, 0);
056b331e 1247 if (read(fd, &boot_sect, 512) != 512)
034b203a
TM
1248 goto abort;
1249
1250 /* check MBP signature */
056b331e 1251 if (boot_sect.magic == MBR_SIGNATURE_MAGIC) {
034b203a
TM
1252 retval = 1;
1253 /* found the correct signature */
056b331e 1254 part = boot_sect.parts;
034b203a
TM
1255
1256 for (part_nr=0; part_nr < MBR_PARTITIONS; part_nr++) {
1257 /* check for GPT type */
1258 if (part->part_type == MBR_GPT_PARTITION_TYPE) {
1259 retval = get_gpt_last_partition_end(fd, endofpart);
1260 break;
1261 }
1262 /* check the last used lba for the current partition */
1263 curr_part_end = __le32_to_cpu(part->first_sect_lba) +
1264 __le32_to_cpu(part->blocks_num);
1265 if (curr_part_end > *endofpart)
1266 *endofpart = curr_part_end;
1267
1268 part++;
1269 }
1270 } else {
1271 /* Unknown partition table */
1272 retval = -1;
1273 }
1274 abort:
1275 return retval;
1276}
1277
1278int check_partitions(int fd, char *dname, unsigned long long freesize)
1279{
1280 /*
1281 * Check where the last partition ends
1282 */
1283 unsigned long long endofpart;
1284 int ret;
1285
1286 if ((ret = get_last_partition_end(fd, &endofpart)) > 0) {
1287 /* There appears to be a partition table here */
1288 if (freesize == 0) {
1289 /* partitions will not be visible in new device */
1290 fprintf(stderr,
1291 Name ": partition table exists on %s but will be lost or\n"
1292 " meaningless after creating array\n",
1293 dname);
1294 return 1;
1295 } else if (endofpart > freesize) {
1296 /* last partition overlaps metadata */
1297 fprintf(stderr,
1298 Name ": metadata will over-write last partition on %s.\n",
1299 dname);
1300 return 1;
1301 }
1302 }
1303 return 0;
1304}
1305
8382f19b
NB
1306void get_one_disk(int mdfd, mdu_array_info_t *ainf, mdu_disk_info_t *disk)
1307{
1308 int d;
1309 ioctl(mdfd, GET_ARRAY_INFO, ainf);
1310 for (d = 0 ; d < ainf->raid_disks + ainf->nr_disks ; d++)
1311 if (ioctl(mdfd, GET_DISK_INFO, disk) == 0)
1312 return;
1313}
63152c1b 1314
a322f70c
DW
1315int open_container(int fd)
1316{
1317 /* 'fd' is a block device. Find out if it is in use
1318 * by a container, and return an open fd on that container.
1319 */
1320 char path[256];
1321 char *e;
1322 DIR *dir;
1323 struct dirent *de;
1324 int dfd, n;
1325 char buf[200];
1326 int major, minor;
1327 struct stat st;
1328
1329 if (fstat(fd, &st) != 0)
1330 return -1;
1331 sprintf(path, "/sys/dev/block/%d:%d/holders",
1332 (int)major(st.st_rdev), (int)minor(st.st_rdev));
1333 e = path + strlen(path);
1334
1335 dir = opendir(path);
1336 if (!dir)
1337 return -1;
1338 while ((de = readdir(dir))) {
1339 if (de->d_ino == 0)
1340 continue;
1341 if (de->d_name[0] == '.')
1342 continue;
1343 sprintf(e, "/%s/dev", de->d_name);
1344 dfd = open(path, O_RDONLY);
1345 if (dfd < 0)
1346 continue;
1347 n = read(dfd, buf, sizeof(buf));
1348 close(dfd);
1349 if (n <= 0 || n >= sizeof(buf))
1350 continue;
1351 buf[n] = 0;
1352 if (sscanf(buf, "%d:%d", &major, &minor) != 2)
1353 continue;
1354 sprintf(buf, "%d:%d", major, minor);
1355 dfd = dev_open(buf, O_RDONLY);
1356 if (dfd >= 0) {
1357 closedir(dir);
1358 return dfd;
1359 }
1360 }
355726fa 1361 closedir(dir);
a322f70c
DW
1362 return -1;
1363}
1364
7801ac20
N
1365int add_disk(int mdfd, struct supertype *st,
1366 struct mdinfo *sra, struct mdinfo *info)
1367{
1368 /* Add a device to an array, in one of 2 ways. */
1369 int rv;
1370#ifndef MDASSEMBLE
1371 if (st->ss->external) {
d23534e4
DW
1372 if (info->disk.state & (1<<MD_DISK_SYNC))
1373 info->recovery_start = MaxSector;
1374 else
1375 info->recovery_start = 0;
2904b26f 1376 rv = sysfs_add_disk(sra, info, 0);
7801ac20
N
1377 if (! rv) {
1378 struct mdinfo *sd2;
f35f2525
N
1379 for (sd2 = sra->devs; sd2; sd2=sd2->next)
1380 if (sd2 == info)
1381 break;
1382 if (sd2 == NULL) {
1383 sd2 = malloc(sizeof(*sd2));
1384 *sd2 = *info;
1385 sd2->next = sra->devs;
1386 sra->devs = sd2;
1387 }
7801ac20
N
1388 }
1389 } else
1390#endif
1391 rv = ioctl(mdfd, ADD_NEW_DISK, &info->disk);
1392 return rv;
1393}
1394
f35f2525
N
1395int set_array_info(int mdfd, struct supertype *st, struct mdinfo *info)
1396{
1397 /* Initialise kernel's knowledge of array.
1398 * This varies between externally managed arrays
1399 * and older kernels
1400 */
1401 int vers = md_get_version(mdfd);
1402 int rv;
1403
1404#ifndef MDASSEMBLE
1405 if (st->ss->external)
1406 rv = sysfs_set_array(info, vers);
1407 else
1408#endif
1409 if ((vers % 100) >= 1) { /* can use different versions */
1410 mdu_array_info_t inf;
1411 memset(&inf, 0, sizeof(inf));
1412 inf.major_version = info->array.major_version;
1413 inf.minor_version = info->array.minor_version;
1414 rv = ioctl(mdfd, SET_ARRAY_INFO, &inf);
1415 } else
1416 rv = ioctl(mdfd, SET_ARRAY_INFO, NULL);
1417 return rv;
1418}
1419
1e5c6983
DW
1420unsigned long long min_recovery_start(struct mdinfo *array)
1421{
1422 /* find the minimum recovery_start in an array for metadata
1423 * formats that only record per-array recovery progress instead
1424 * of per-device
1425 */
1426 unsigned long long recovery_start = MaxSector;
1427 struct mdinfo *d;
1428
1429 for (d = array->devs; d; d = d->next)
1430 recovery_start = min(recovery_start, d->recovery_start);
1431
1432 return recovery_start;
1433}
1434
2f6079dc
NB
1435char *devnum2devname(int num)
1436{
1437 char name[100];
1f0769d7 1438 if (num >= 0)
2f6079dc
NB
1439 sprintf(name, "md%d", num);
1440 else
1441 sprintf(name, "md_d%d", -1-num);
1442 return strdup(name);
1443}
1444
77472ff8
NB
1445int devname2devnum(char *name)
1446{
1447 char *ep;
1448 int num;
1449 if (strncmp(name, "md_d", 4)==0)
1450 num = -1-strtoul(name+4, &ep, 10);
1451 else
1452 num = strtoul(name+2, &ep, 10);
1453 return num;
1454}
1455
c94709e8 1456int stat2devnum(struct stat *st)
2f6079dc 1457{
d7ab966b
N
1458 char path[30];
1459 char link[200];
1460 char *cp;
1461 int n;
1462
c94709e8
DW
1463 if ((S_IFMT & st->st_mode) == S_IFBLK) {
1464 if (major(st->st_rdev) == MD_MAJOR)
1465 return minor(st->st_rdev);
d7ab966b
N
1466 else if (major(st->st_rdev) == get_mdp_major())
1467 return -1- (minor(st->st_rdev)>>MdpMinorShift);
1468
1469 /* must be an extended-minor partition. Look at the
1470 * /sys/dev/block/%d:%d link which must look like
1471 * ../../block/mdXXX/mdXXXpYY
1472 */
1473 sprintf(path, "/sys/dev/block/%d:%d", major(st->st_rdev),
1474 minor(st->st_rdev));
1475 n = readlink(path, link, sizeof(link)-1);
1476 if (n <= 0)
1477 return NoMdDev;
1478 link[n] = 0;
1479 cp = strrchr(link, '/');
1480 if (cp) *cp = 0;
1481 cp = strchr(link, '/');
1482 if (cp && strncmp(cp, "/md", 3) == 0)
1483 return devname2devnum(cp+1);
2f6079dc 1484 }
d7ab966b 1485 return NoMdDev;
c94709e8
DW
1486
1487}
1488
1489int fd2devnum(int fd)
1490{
1491 struct stat stb;
1492 if (fstat(fd, &stb) == 0)
1493 return stat2devnum(&stb);
d7ab966b 1494 return NoMdDev;
2f6079dc
NB
1495}
1496
5d4d1b26
N
1497char *pid_dir = VAR_RUN;
1498
24f6f99b 1499int mdmon_pid(int devnum)
a931db9e
NB
1500{
1501 char path[100];
1502 char pid[10];
1503 int fd;
1504 int n;
5d4d1b26 1505 sprintf(path, "%s/%s.pid", pid_dir, devnum2devname(devnum));
24f6f99b 1506 fd = open(path, O_RDONLY | O_NOATIME, 0);
a931db9e
NB
1507
1508 if (fd < 0)
cf556303 1509 return -1;
a931db9e
NB
1510 n = read(fd, pid, 9);
1511 close(fd);
1512 if (n <= 0)
cf556303 1513 return -1;
24f6f99b 1514 return atoi(pid);
a931db9e
NB
1515}
1516
24f6f99b 1517int mdmon_running(int devnum)
a931db9e 1518{
24f6f99b
N
1519 int pid = mdmon_pid(devnum);
1520 if (pid <= 0)
a931db9e 1521 return 0;
24f6f99b 1522 if (kill(pid, 0) == 0)
a931db9e
NB
1523 return 1;
1524 return 0;
1525}
1526
8850ee3e
N
1527int start_mdmon(int devnum)
1528{
1529 int i;
44d2e365 1530 int len;
9fe32043
N
1531 pid_t pid;
1532 int status;
44d2e365
N
1533 char pathbuf[1024];
1534 char *paths[4] = {
1535 pathbuf,
1536 "/sbin/mdmon",
1537 "mdmon",
1538 NULL
1539 };
8850ee3e 1540
40ebbb9c 1541 if (check_env("MDADM_NO_MDMON"))
8850ee3e
N
1542 return 0;
1543
44d2e365
N
1544 len = readlink("/proc/self/exe", pathbuf, sizeof(pathbuf));
1545 if (len > 0) {
1546 char *sl;
1547 pathbuf[len] = 0;
1548 sl = strrchr(pathbuf, '/');
1549 if (sl)
1550 sl++;
1551 else
1552 sl = pathbuf;
1553 strcpy(sl, "mdmon");
1554 } else
1555 pathbuf[0] = '\0';
1556
8850ee3e
N
1557 switch(fork()) {
1558 case 0:
1559 /* FIXME yuk. CLOSE_EXEC?? */
1560 for (i=3; i < 100; i++)
1561 close(i);
44d2e365
N
1562 for (i=0; paths[i]; i++)
1563 if (paths[i][0])
1564 execl(paths[i], "mdmon",
e8a70c89
N
1565 devnum2devname(devnum),
1566 NULL);
8850ee3e
N
1567 exit(1);
1568 case -1: fprintf(stderr, Name ": cannot run mdmon. "
1569 "Array remains readonly\n");
1570 return -1;
9fe32043
N
1571 default: /* parent - good */
1572 pid = wait(&status);
1573 if (pid < 0 || status != 0)
1574 return -1;
8850ee3e
N
1575 }
1576 return 0;
1577}
1578
40ebbb9c 1579int check_env(char *name)
5dcfcb71 1580{
40ebbb9c 1581 char *val = getenv(name);
5dcfcb71
DW
1582
1583 if (val && atoi(val) == 1)
1584 return 1;
1585
1586 return 0;
1587}
1588
148acb7b
DW
1589__u32 random32(void)
1590{
1591 __u32 rv;
1592 int rfd = open("/dev/urandom", O_RDONLY);
1593 if (rfd < 0 || read(rfd, &rv, 4) != 4)
1594 rv = random();
1595 if (rfd >= 0)
1596 close(rfd);
1597 return rv;
1598}
1599
0e600426 1600#ifndef MDASSEMBLE
edd8d13c
NB
1601int flush_metadata_updates(struct supertype *st)
1602{
1603 int sfd;
1604 if (!st->updates) {
1605 st->update_tail = NULL;
1606 return -1;
1607 }
1608
1609 sfd = connect_monitor(devnum2devname(st->container_dev));
1610 if (sfd < 0)
1611 return -1;
1612
1613 while (st->updates) {
1614 struct metadata_update *mu = st->updates;
1615 st->updates = mu->next;
1616
1617 send_message(sfd, mu, 0);
1618 wait_reply(sfd, 0);
1619 free(mu->buf);
1620 free(mu);
1621 }
1622 ack(sfd, 0);
1623 wait_reply(sfd, 0);
1624 close(sfd);
1625 st->update_tail = NULL;
1626 return 0;
1627}
1628
1629void append_metadata_update(struct supertype *st, void *buf, int len)
1630{
1631
1632 struct metadata_update *mu = malloc(sizeof(*mu));
1633
1634 mu->buf = buf;
1635 mu->len = len;
1636 mu->space = NULL;
1637 mu->next = NULL;
1638 *st->update_tail = mu;
1639 st->update_tail = &mu->next;
1640}
0e600426 1641#endif /* MDASSEMBLE */
a931db9e 1642
fe6729fa
NB
1643#ifdef __TINYC__
1644/* tinyc doesn't optimize this check in ioctl.h out ... */
1645unsigned int __invalid_size_argument_for_IOC = 0;
1646#endif
1647