]> git.ipfire.org Git - thirdparty/mdadm.git/blame - util.c
Add nodes option while creating md
[thirdparty/mdadm.git] / util.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
6f02172d 4 * Copyright (C) 2001-2013 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>
a7dec3fd 31#include <sys/resource.h>
a9c15847
N
32#include <sys/vfs.h>
33#include <linux/magic.h>
98c6faba 34#include <ctype.h>
a322f70c 35#include <dirent.h>
a931db9e 36#include <signal.h>
0a816ef9
NB
37
38/*
39 * following taken from linux/blkpg.h because they aren't
40 * anywhere else and it isn't safe to #include linux/ * stuff.
41 */
42
43#define BLKPG _IO(0x12,105)
44
45/* The argument structure */
46struct blkpg_ioctl_arg {
5d500228
N
47 int op;
48 int flags;
49 int datalen;
50 void *data;
0a816ef9
NB
51};
52
53/* The subfunctions (for the op field) */
54#define BLKPG_ADD_PARTITION 1
55#define BLKPG_DEL_PARTITION 2
56
57/* Sizes of name fields. Unused at present. */
58#define BLKPG_DEVNAMELTH 64
59#define BLKPG_VOLNAMELTH 64
60
61/* The data structure for ADD_PARTITION and DEL_PARTITION */
62struct blkpg_partition {
63 long long start; /* starting offset in bytes */
64 long long length; /* length in bytes */
65 int pno; /* partition number */
66 char devname[BLKPG_DEVNAMELTH]; /* partition name, like sda5 or c0d1p2,
67 to be used in kernel messages */
68 char volname[BLKPG_VOLNAMELTH]; /* volume label */
69};
64c4757e 70
0f22b998 71#include "part.h"
056b331e
N
72
73/* Force a compilation error if condition is true */
74#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
75
76/* Force a compilation error if condition is true, but also produce a
77 result (of value 0 and type size_t), so the expression can be used
78 e.g. in a structure initializer (or where-ever else comma expressions
79 aren't permitted). */
80#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
81
64c4757e
NB
82/*
83 * Parse a 128 bit uuid in 4 integers
84 * format is 32 hexx nibbles with options :.<space> separator
85 * If not exactly 32 hex digits are found, return 0
86 * else return 1
87 */
88int parse_uuid(char *str, int uuid[4])
89{
aba69144
NB
90 int hit = 0; /* number of Hex digIT */
91 int i;
92 char c;
ca3b6696
N
93 for (i = 0; i < 4; i++)
94 uuid[i] = 0;
aba69144 95
ca3b6696 96 while ((c = *str++) != 0) {
aba69144 97 int n;
ca3b6696 98 if (c >= '0' && c <= '9')
aba69144 99 n = c-'0';
ca3b6696 100 else if (c >= 'a' && c <= 'f')
aba69144 101 n = 10 + c - 'a';
ca3b6696 102 else if (c >= 'A' && c <= 'F')
aba69144
NB
103 n = 10 + c - 'A';
104 else if (strchr(":. -", c))
105 continue;
106 else return 0;
107
108 if (hit<32) {
109 uuid[hit/8] <<= 4;
110 uuid[hit/8] += n;
111 }
112 hit++;
82b27616 113 }
aba69144
NB
114 if (hit == 32)
115 return 1;
116 return 0;
64c4757e
NB
117}
118
64c4757e
NB
119/*
120 * Get the md version number.
121 * We use the RAID_VERSION ioctl if it is supported
122 * If not, but we have a block device with major '9', we assume
123 * 0.36.0
124 *
125 * Return version number as 24 but number - assume version parts
126 * always < 255
127 */
128
129int md_get_version(int fd)
130{
5d500228
N
131 struct stat stb;
132 mdu_version_t vers;
133
134 if (fstat(fd, &stb)<0)
135 return -1;
136 if ((S_IFMT&stb.st_mode) != S_IFBLK)
137 return -1;
138
139 if (ioctl(fd, RAID_VERSION, &vers) == 0)
140 return (vers.major*10000) + (vers.minor*100) + vers.patchlevel;
141 if (errno == EACCES)
142 return -1;
143 if (major(stb.st_rdev) == MD_MAJOR)
144 return (3600);
64c4757e 145 return -1;
64c4757e
NB
146}
147
64c4757e
NB
148int get_linux_version()
149{
150 struct utsname name;
98c6faba 151 char *cp;
f161d047 152 int a = 0, b = 0,c = 0;
64c4757e
NB
153 if (uname(&name) <0)
154 return -1;
155
98c6faba
NB
156 cp = name.release;
157 a = strtoul(cp, &cp, 10);
f161d047
N
158 if (*cp == '.')
159 b = strtoul(cp+1, &cp, 10);
160 if (*cp == '.')
161 c = strtoul(cp+1, &cp, 10);
98c6faba 162
682c7051 163 return (a*1000000)+(b*1000)+c;
64c4757e
NB
164}
165
71204a50 166#ifndef MDASSEMBLE
bc77ed53
DW
167int mdadm_version(char *version)
168{
169 int a, b, c;
170 char *cp;
171
172 if (!version)
173 version = Version;
174
175 cp = strchr(version, '-');
176 if (!cp || *(cp+1) != ' ' || *(cp+2) != 'v')
177 return -1;
178 cp += 3;
179 a = strtoul(cp, &cp, 10);
180 if (*cp != '.')
181 return -1;
182 b = strtoul(cp+1, &cp, 10);
183 if (*cp == '.')
184 c = strtoul(cp+1, &cp, 10);
185 else
186 c = 0;
187 if (*cp != ' ' && *cp != '-')
188 return -1;
189 return (a*1000000)+(b*1000)+c;
190}
191
51d4261c 192unsigned long long parse_size(char *size)
84e11361
N
193{
194 /* parse 'size' which should be a number optionally
195 * followed by 'K', 'M', or 'G'.
196 * Without a suffix, K is assumed.
197 * Number returned is in sectors (half-K)
822e393a 198 * INVALID_SECTORS returned on error.
84e11361
N
199 */
200 char *c;
201 long long s = strtoll(size, &c, 10);
202 if (s > 0) {
203 switch (*c) {
204 case 'K':
205 c++;
206 default:
207 s *= 2;
208 break;
209 case 'M':
210 c++;
211 s *= 1024 * 2;
212 break;
213 case 'G':
214 c++;
215 s *= 1024 * 1024 * 2;
216 break;
72ca9bcf
N
217 case 's': /* sectors */
218 c++;
219 break;
84e11361 220 }
51d4261c 221 } else
822e393a 222 s = INVALID_SECTORS;
84e11361 223 if (*c)
822e393a 224 s = INVALID_SECTORS;
84e11361
N
225 return s;
226}
227
4a06e2c2
N
228int parse_layout_10(char *layout)
229{
230 int copies, rv;
231 char *cp;
232 /* Parse the layout string for raid10 */
233 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
234 if ((layout[0] != 'n' && layout[0] != 'f' && layout[0] != 'o') ||
235 (copies = strtoul(layout+1, &cp, 10)) < 1 ||
236 copies > 200 ||
237 *cp)
238 return -1;
239 if (layout[0] == 'n')
240 rv = 256 + copies;
241 else if (layout[0] == 'o')
242 rv = 0x10000 + (copies<<8) + 1;
243 else
244 rv = 1 + (copies<<8);
245 return rv;
246}
247
248int parse_layout_faulty(char *layout)
249{
250 /* Parse the layout string for 'faulty' */
251 int ln = strcspn(layout, "0123456789");
503975b9 252 char *m = xstrdup(layout);
4a06e2c2
N
253 int mode;
254 m[ln] = 0;
255 mode = map_name(faultylayout, m);
256 if (mode == UnSet)
257 return -1;
258
259 return mode | (atoi(layout+ln)<< ModeShift);
260}
79868890
N
261
262long parse_num(char *num)
263{
264 /* Either return a valid number, or -1 */
265 char *c;
266 long rv = strtol(num, &c, 10);
267 if (rv < 0 || *c || !num[0])
268 return -1;
269 else
270 return rv;
271}
eb3929a4 272#endif
4a06e2c2 273
0430ed48
NB
274void remove_partitions(int fd)
275{
276 /* remove partitions from this block devices.
277 * This is used for components added to an array
278 */
279#ifdef BLKPG_DEL_PARTITION
280 struct blkpg_ioctl_arg a;
281 struct blkpg_partition p;
282
283 a.op = BLKPG_DEL_PARTITION;
284 a.data = (void*)&p;
285 a.datalen = sizeof(p);
286 a.flags = 0;
287 memset(a.data, 0, a.datalen);
ca3b6696 288 for (p.pno = 0; p.pno < 16; p.pno++)
0430ed48
NB
289 ioctl(fd, BLKPG, &a);
290#endif
291}
292
691c6ee1
N
293int test_partition(int fd)
294{
295 /* Check if fd is a whole-disk or a partition.
296 * BLKPG will return EINVAL on a partition, and BLKPG_DEL_PARTITION
297 * will return ENXIO on an invalid partition number.
298 */
299 struct blkpg_ioctl_arg a;
300 struct blkpg_partition p;
301 a.op = BLKPG_DEL_PARTITION;
302 a.data = (void*)&p;
303 a.datalen = sizeof(p);
304 a.flags = 0;
305 memset(a.data, 0, a.datalen);
306 p.pno = 1<<30;
307 if (ioctl(fd, BLKPG, &a) == 0)
308 /* Very unlikely, but not a partition */
309 return 0;
357ac106 310 if (errno == ENXIO || errno == ENOTTY)
691c6ee1
N
311 /* not a partition */
312 return 0;
313
314 return 1;
315}
316
bfd76b93
CA
317int test_partition_from_id(dev_t id)
318{
319 char buf[20];
320 int fd, rv;
321
322 sprintf(buf, "%d:%d", major(id), minor(id));
323 fd = dev_open(buf, O_RDONLY);
324 if (fd < 0)
325 return -1;
326 rv = test_partition(fd);
327 close(fd);
328 return rv;
329}
691c6ee1 330
de5a472e 331int enough(int level, int raid_disks, int layout, int clean, char *avail)
64c4757e 332{
265e0f17 333 int copies, first;
de5a472e
N
334 int i;
335 int avail_disks = 0;
336
337 for (i = 0; i < raid_disks; i++)
338 avail_disks += !!avail[i];
339
64c4757e 340 switch (level) {
265e0f17
NB
341 case 10:
342 /* This is the tricky one - we need to check
343 * which actual disks are present.
344 */
702b557b 345 copies = (layout&255)* ((layout>>8) & 255);
ca3b6696 346 first = 0;
265e0f17
NB
347 do {
348 /* there must be one of the 'copies' form 'first' */
349 int n = copies;
ca3b6696 350 int cnt = 0;
b3ec716d 351 int this = first;
265e0f17 352 while (n--) {
b3ec716d 353 if (avail[this])
265e0f17 354 cnt++;
b3ec716d 355 this = (this+1) % raid_disks;
265e0f17
NB
356 }
357 if (cnt == 0)
358 return 0;
b3ec716d 359 first = (first+(layout&255)) % raid_disks;
265e0f17
NB
360 } while (first != 0);
361 return 1;
e5329c37 362
df0d4ea0 363 case LEVEL_MULTIPATH:
e0d19036 364 return avail_disks>= 1;
df0d4ea0 365 case LEVEL_LINEAR:
64c4757e
NB
366 case 0:
367 return avail_disks == raid_disks;
368 case 1:
369 return avail_disks >= 1;
370 case 4:
330d6900
N
371 if (avail_disks == raid_disks - 1 &&
372 !avail[raid_disks - 1])
373 /* If just the parity device is missing, then we
374 * have enough, even if not clean
375 */
376 return 1;
377 /* FALL THROUGH */
64c4757e 378 case 5:
583315d9
NB
379 if (clean)
380 return avail_disks >= raid_disks-1;
381 else
382 return avail_disks >= raid_disks;
98c6faba 383 case 6:
583315d9
NB
384 if (clean)
385 return avail_disks >= raid_disks-2;
386 else
387 return avail_disks >= raid_disks;
64c4757e
NB
388 default:
389 return 0;
390 }
391}
392
8453e704
N
393int enough_fd(int fd)
394{
395 struct mdu_array_info_s array;
396 struct mdu_disk_info_s disk;
aabe020d 397 int i, rv;
8453e704
N
398 char *avail;
399
400 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0 ||
401 array.raid_disks <= 0)
402 return 0;
503975b9 403 avail = xcalloc(array.raid_disks, 1);
ca3b6696 404 for (i = 0; i < MAX_DISKS && array.nr_disks > 0; i++) {
8453e704
N
405 disk.number = i;
406 if (ioctl(fd, GET_DISK_INFO, &disk) != 0)
407 continue;
9e6d9291
N
408 if (disk.major == 0 && disk.minor == 0)
409 continue;
410 array.nr_disks--;
411
8453e704
N
412 if (! (disk.state & (1<<MD_DISK_SYNC)))
413 continue;
414 if (disk.raid_disk < 0 || disk.raid_disk >= array.raid_disks)
415 continue;
8453e704
N
416 avail[disk.raid_disk] = 1;
417 }
418 /* This is used on an active array, so assume it is clean */
aabe020d 419 rv = enough(array.level, array.raid_disks, array.layout,
de5a472e 420 1, avail);
aabe020d
JS
421 free(avail);
422 return rv;
8453e704
N
423}
424
22e263f6
AC
425const int uuid_zero[4] = { 0, 0, 0, 0 };
426
f277ce36 427int same_uuid(int a[4], int b[4], int swapuuid)
64c4757e 428{
f277ce36
NB
429 if (swapuuid) {
430 /* parse uuids are hostendian.
431 * uuid's from some superblocks are big-ending
aba69144 432 * if there is a difference, we need to swap..
f277ce36
NB
433 */
434 unsigned char *ac = (unsigned char *)a;
435 unsigned char *bc = (unsigned char *)b;
436 int i;
ca3b6696 437 for (i = 0; i < 16; i += 4) {
f277ce36
NB
438 if (ac[i+0] != bc[i+3] ||
439 ac[i+1] != bc[i+2] ||
440 ac[i+2] != bc[i+1] ||
441 ac[i+3] != bc[i+0])
442 return 0;
443 }
444 return 1;
445 } else {
446 if (a[0]==b[0] &&
447 a[1]==b[1] &&
448 a[2]==b[2] &&
449 a[3]==b[3])
450 return 1;
451 return 0;
452 }
64c4757e 453}
ca3b6696 454
350f29f9
NB
455void copy_uuid(void *a, int b[4], int swapuuid)
456{
457 if (swapuuid) {
458 /* parse uuids are hostendian.
459 * uuid's from some superblocks are big-ending
460 * if there is a difference, we need to swap..
461 */
462 unsigned char *ac = (unsigned char *)a;
463 unsigned char *bc = (unsigned char *)b;
464 int i;
ca3b6696 465 for (i = 0; i < 16; i += 4) {
350f29f9
NB
466 ac[i+0] = bc[i+3];
467 ac[i+1] = bc[i+2];
468 ac[i+2] = bc[i+1];
469 ac[i+3] = bc[i+0];
470 }
471 } else
472 memcpy(a, b, 16);
473}
64c4757e 474
aae5a112 475char *__fname_from_uuid(int id[4], int swap, char *buf, char sep)
d7288ddc 476{
9968e376 477 int i, j;
d7288ddc
N
478 char uuid[16];
479 char *c = buf;
480 strcpy(c, "UUID-");
481 c += strlen(c);
aae5a112 482 copy_uuid(uuid, id, swap);
9968e376 483 for (i = 0; i < 4; i++) {
9968e376 484 if (i)
ff54de6e 485 *c++ = sep;
9968e376
DW
486 for (j = 3; j >= 0; j--) {
487 sprintf(c,"%02x", (unsigned char) uuid[j+4*i]);
488 c+= 2;
489 }
d7288ddc
N
490 }
491 return buf;
aae5a112
DW
492
493}
494
495char *fname_from_uuid(struct supertype *st, struct mdinfo *info, char *buf, char sep)
496{
86983cce
N
497 // dirty hack to work around an issue with super1 superblocks...
498 // super1 superblocks need swapuuid set in order for assembly to
499 // work, but can't have it set if we want this printout to match
500 // all the other uuid printouts in super1.c, so we force swapuuid
501 // to 1 to make our printout match the rest of super1
502 return __fname_from_uuid(info->uuid, (st->ss == &super1) ? 1 : st->ss->swapuuid, buf, sep);
d7288ddc
N
503}
504
435d4ebb 505#ifndef MDASSEMBLE
682c7051
NB
506int check_ext2(int fd, char *name)
507{
508 /*
509 * Check for an ext2fs file system.
510 * Superblock is always 1K at 1K offset
511 *
512 * s_magic is le16 at 56 == 0xEF53
513 * report mtime - le32 at 44
514 * blocks - le32 at 4
515 * logblksize - le32 at 24
516 */
517 unsigned char sb[1024];
518 time_t mtime;
2538ba2a
N
519 unsigned long long size;
520 int bsize;
682c7051
NB
521 if (lseek(fd, 1024,0)!= 1024)
522 return 0;
523 if (read(fd, sb, 1024)!= 1024)
524 return 0;
525 if (sb[56] != 0x53 || sb[57] != 0xef)
526 return 0;
527
528 mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8;
529 bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8;
530 size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8;
2538ba2a 531 size <<= bsize;
e7b84f9d 532 pr_err("%s appears to contain an ext2fs file system\n",
682c7051 533 name);
2538ba2a 534 cont_err("size=%lluK mtime=%s", size, ctime(&mtime));
682c7051
NB
535 return 1;
536}
537
538int check_reiser(int fd, char *name)
539{
540 /*
541 * superblock is at 64K
542 * size is 1024;
543 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
544 *
545 */
546 unsigned char sb[1024];
2538ba2a 547 unsigned long long size;
682c7051
NB
548 if (lseek(fd, 64*1024, 0) != 64*1024)
549 return 0;
550 if (read(fd, sb, 1024) != 1024)
551 return 0;
ca3b6696
N
552 if (strncmp((char*)sb+52, "ReIsErFs",8) != 0 &&
553 strncmp((char*)sb+52, "ReIsEr2Fs",9) != 0)
682c7051 554 return 0;
e7b84f9d 555 pr_err("%s appears to contain a reiserfs file system\n",name);
682c7051 556 size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8;
2538ba2a 557 cont_err("size = %lluK\n", size*4);
aba69144 558
682c7051
NB
559 return 1;
560}
561
562int check_raid(int fd, char *name)
563{
4b1ac34b 564 struct mdinfo info;
682c7051 565 time_t crtime;
d078d77c 566 char *level;
82d9eba6 567 struct supertype *st = guess_super(fd);
f9ce90ba 568
cb8f6859
N
569 if (!st)
570 return 0;
3da92f27 571 st->ss->load_super(st, fd, name);
82d9eba6 572 /* Looks like a raid array .. */
e7b84f9d 573 pr_err("%s appears to be part of a raid array:\n",
82d9eba6 574 name);
a5d85af7 575 st->ss->getinfo_super(st, &info, NULL);
3da92f27 576 st->ss->free_super(st);
82d9eba6 577 crtime = info.array.ctime;
d078d77c
NB
578 level = map_num(pers, info.array.level);
579 if (!level) level = "-unknown-";
d33f1518
N
580 cont_err("level=%s devices=%d ctime=%s",
581 level, info.array.raid_disks, ctime(&crtime));
82d9eba6 582 return 1;
682c7051
NB
583}
584
682c7051
NB
585int ask(char *mesg)
586{
587 char *add = "";
588 int i;
ca3b6696 589 for (i = 0; i < 5; i++) {
682c7051
NB
590 char buf[100];
591 fprintf(stderr, "%s%s", mesg, add);
592 fflush(stderr);
593 if (fgets(buf, 100, stdin)==NULL)
594 return 0;
595 if (buf[0]=='y' || buf[0]=='Y')
596 return 1;
597 if (buf[0]=='n' || buf[0]=='N')
598 return 0;
599 add = "(y/n) ";
600 }
e7b84f9d 601 pr_err("assuming 'no'\n");
682c7051
NB
602 return 0;
603}
435d4ebb 604#endif /* MDASSEMBLE */
682c7051 605
8d80900b 606int is_standard(char *dev, int *nump)
e5329c37
NB
607{
608 /* tests if dev is a "standard" md dev name.
609 * i.e if the last component is "/dNN" or "/mdNN",
aba69144 610 * where NN is a string of digits
598f0d58
NB
611 * Returns 1 if a partitionable standard,
612 * -1 if non-partitonable,
613 * 0 if not a standard name.
e5329c37 614 */
8d80900b 615 char *d = strrchr(dev, '/');
ca3b6696 616 int type = 0;
8d80900b
NB
617 int num;
618 if (!d)
e5329c37 619 return 0;
ca3b6696
N
620 if (strncmp(d, "/d",2) == 0)
621 d += 2, type = 1; /* /dev/md/dN{pM} */
622 else if (strncmp(d, "/md_d", 5) == 0)
623 d += 5, type = 1; /* /dev/md_dN{pM} */
624 else if (strncmp(d, "/md", 3) == 0)
625 d += 3, type = -1; /* /dev/mdN */
626 else if (d-dev > 3 && strncmp(d-2, "md/", 3) == 0)
627 d += 1, type = -1; /* /dev/md/N */
e5329c37
NB
628 else
629 return 0;
8d80900b 630 if (!*d)
e5329c37 631 return 0;
8d80900b
NB
632 num = atoi(d);
633 while (isdigit(*d))
634 d++;
635 if (*d)
e5329c37 636 return 0;
8d80900b
NB
637 if (nump) *nump = num;
638
639 return type;
e5329c37
NB
640}
641
4b1ac34b 642unsigned long calc_csum(void *super, int bytes)
82b27616 643{
56eb10c0 644 unsigned long long newcsum = 0;
82b27616 645 int i;
4b1ac34b
NB
646 unsigned int csum;
647 unsigned int *superc = (unsigned int*) super;
82b27616 648
ca3b6696
N
649 for(i = 0; i < bytes/4; i++)
650 newcsum += superc[i];
82b27616 651 csum = (newcsum& 0xffffffff) + (newcsum>>32);
570c0542 652#ifdef __alpha__
aba69144 653/* The in-kernel checksum calculation is always 16bit on
570c0542 654 * the alpha, though it is 32 bit on i386...
ca3b6696 655 * I wonder what it is elsewhere... (it uses an API in
570c0542
NB
656 * a way that it shouldn't).
657 */
658 csum = (csum & 0xffff) + (csum >> 16);
659 csum = (csum & 0xffff) + (csum >> 16);
660#endif
82b27616
NB
661 return csum;
662}
cd29a5c8 663
435d4ebb 664#ifndef MDASSEMBLE
56eb10c0 665char *human_size(long long bytes)
cd29a5c8
NB
666{
667 static char buf[30];
d5d3721e
NB
668
669 /* We convert bytes to either centi-M{ega,ibi}bytes or
670 * centi-G{igi,ibi}bytes, with appropriate rounding,
671 * and then print 1/100th of those as a decimal.
672 * We allow upto 2048Megabytes before converting to
673 * gigabytes, as that shows more precision and isn't
674 * too large a number.
01619b48 675 * Terabytes are not yet handled.
d5d3721e 676 */
cd29a5c8 677
56eb10c0 678 if (bytes < 5000*1024)
ca3b6696 679 buf[0] = 0;
d5d3721e 680 else if (bytes < 2*1024LL*1024LL*1024LL) {
93d3bd3b 681 long cMiB = (bytes * 200LL / (1LL<<20) + 1) / 2;
d5d3721e 682 long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2;
8f23b0b3 683 snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)",
d5d3721e
NB
684 cMiB/100 , cMiB % 100,
685 cMB/100, cMB % 100);
686 } else {
93d3bd3b 687 long cGiB = (bytes * 200LL / (1LL<<30) +1) / 2;
d5d3721e 688 long cGB = (bytes / (1000000000LL/200LL ) +1) /2;
8f23b0b3 689 snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)",
d5d3721e
NB
690 cGiB/100 , cGiB % 100,
691 cGB/100, cGB % 100);
692 }
cd29a5c8
NB
693 return buf;
694}
e0d19036 695
f0ec6710 696char *human_size_brief(long long bytes, int prefix)
e0d19036
NB
697{
698 static char buf[30];
e0d19036 699
570abc6f
MN
700 /* We convert bytes to either centi-M{ega,ibi}bytes or
701 * centi-G{igi,ibi}bytes, with appropriate rounding,
702 * and then print 1/100th of those as a decimal.
703 * We allow upto 2048Megabytes before converting to
704 * gigabytes, as that shows more precision and isn't
705 * too large a number.
706 * Terabytes are not yet handled.
f0ec6710
MN
707 *
708 * If prefix == IEC, we mean prefixes like kibi,mebi,gibi etc.
709 * If prefix == JEDEC, we mean prefixes like kilo,mega,giga etc.
570abc6f
MN
710 */
711
e0d19036 712 if (bytes < 5000*1024)
570abc6f 713 buf[0] = 0;
f0ec6710
MN
714 else if (prefix == IEC) {
715 if (bytes < 2*1024LL*1024LL*1024LL) {
93d3bd3b 716 long cMiB = (bytes * 200LL / (1LL<<20) +1) /2;
f0ec6710
MN
717 snprintf(buf, sizeof(buf), "%ld.%02ldMiB",
718 cMiB/100 , cMiB % 100);
719 } else {
93d3bd3b 720 long cGiB = (bytes * 200LL / (1LL<<30) +1) /2;
f0ec6710
MN
721 snprintf(buf, sizeof(buf), "%ld.%02ldGiB",
722 cGiB/100 , cGiB % 100);
723 }
724 }
725 else if (prefix == JEDEC) {
726 if (bytes < 2*1024LL*1024LL*1024LL) {
727 long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2;
728 snprintf(buf, sizeof(buf), "%ld.%02ldMB",
729 cMB/100, cMB % 100);
730 } else {
731 long cGB = (bytes / (1000000000LL/200LL ) +1) /2;
732 snprintf(buf, sizeof(buf), "%ld.%02ldGB",
733 cGB/100 , cGB % 100);
734 }
570abc6f 735 }
f0ec6710
MN
736 else
737 buf[0] = 0;
570abc6f 738
e0d19036
NB
739 return buf;
740}
e4965ef8
N
741
742void print_r10_layout(int layout)
743{
744 int near = layout & 255;
745 int far = (layout >> 8) & 255;
746 int offset = (layout&0x10000);
747 char *sep = "";
748
749 if (near != 1) {
750 printf("%s near=%d", sep, near);
751 sep = ",";
752 }
753 if (far != 1)
754 printf("%s %s=%d", sep, offset?"offset":"far", far);
755 if (near*far == 1)
756 printf("NO REDUNDANCY");
757}
435d4ebb 758#endif
e0d19036 759
5f8097be
NB
760unsigned long long calc_array_size(int level, int raid_disks, int layout,
761 int chunksize, unsigned long long devsize)
577e8448 762{
e03640bd
CA
763 if (level == 1)
764 return devsize;
577e8448
AK
765 devsize &= ~(unsigned long long)((chunksize>>9)-1);
766 return get_data_disks(level, layout, raid_disks) * devsize;
767}
768
769int get_data_disks(int level, int layout, int raid_disks)
5f8097be
NB
770{
771 int data_disks = 0;
772 switch (level) {
ca3b6696
N
773 case 0: data_disks = raid_disks;
774 break;
775 case 1: data_disks = 1;
776 break;
5f8097be 777 case 4:
ca3b6696
N
778 case 5: data_disks = raid_disks - 1;
779 break;
780 case 6: data_disks = raid_disks - 2;
781 break;
5f8097be
NB
782 case 10: data_disks = raid_disks / (layout & 255) / ((layout>>8)&255);
783 break;
784 }
577e8448
AK
785
786 return data_disks;
5f8097be
NB
787}
788
4dd2df09
N
789int devnm2devid(char *devnm)
790{
791 /* First look in /sys/block/$DEVNM/dev for %d:%d
792 * If that fails, try parsing out a number
793 */
794 char path[100];
795 char *ep;
796 int fd;
797 int mjr,mnr;
798
799 sprintf(path, "/sys/block/%s/dev", devnm);
800 fd = open(path, O_RDONLY);
801 if (fd >= 0) {
802 char buf[20];
803 int n = read(fd, buf, sizeof(buf));
804 close(fd);
805 if (n > 0)
806 buf[n] = 0;
807 if (n > 0 && sscanf(buf, "%d:%d\n", &mjr, &mnr) == 2)
808 return makedev(mjr, mnr);
809 }
810 if (strncmp(devnm, "md_d", 4) == 0 &&
811 isdigit(devnm[4]) &&
812 (mnr = strtoul(devnm+4, &ep, 10)) >= 0 &&
813 ep > devnm && *ep == 0)
814 return makedev(get_mdp_major(), mnr << MdpMinorShift);
815
816 if (strncmp(devnm, "md", 2) == 0 &&
817 isdigit(devnm[2]) &&
818 (mnr = strtoul(devnm+2, &ep, 10)) >= 0 &&
819 ep > devnm && *ep == 0)
820 return makedev(MD_MAJOR, mnr);
821
822 return 0;
823}
824
5a23a06e 825#if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
4dd2df09 826char *get_md_name(char *devnm)
e0d19036
NB
827{
828 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
98c6faba 829 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
4dd2df09 830
e0d19036
NB
831 static char devname[50];
832 struct stat stb;
4dd2df09 833 dev_t rdev = devnm2devid(devnm);
dd0781e5 834 char *dn;
98c6faba 835
4dd2df09
N
836 if (rdev == 0)
837 return 0;
838 if (strncmp(devnm, "md_", 3) == 0) {
839 snprintf(devname, sizeof(devname), "/dev/md/%s",
840 devnm + 3);
98c6faba
NB
841 if (stat(devname, &stb) == 0
842 && (S_IFMT&stb.st_mode) == S_IFBLK
843 && (stb.st_rdev == rdev))
844 return devname;
845 }
4dd2df09
N
846 snprintf(devname, sizeof(devname), "/dev/%s", devnm);
847 if (stat(devname, &stb) == 0
848 && (S_IFMT&stb.st_mode) == S_IFBLK
849 && (stb.st_rdev == rdev))
850 return devname;
851
852 snprintf(devname, sizeof(devname), "/dev/md/%s", devnm+2);
853 if (stat(devname, &stb) == 0
854 && (S_IFMT&stb.st_mode) == S_IFBLK
855 && (stb.st_rdev == rdev))
856 return devname;
857
16c6fa80 858 dn = map_dev(major(rdev), minor(rdev), 0);
dd0781e5
NB
859 if (dn)
860 return dn;
4dd2df09 861 snprintf(devname, sizeof(devname), "/dev/.tmp.%s", devnm);
e0d19036 862 if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
dd0781e5
NB
863 if (errno != EEXIST)
864 return NULL;
e0d19036
NB
865
866 if (stat(devname, &stb) == 0
867 && (S_IFMT&stb.st_mode) == S_IFBLK
868 && (stb.st_rdev == rdev))
869 return devname;
870 unlink(devname);
871 return NULL;
872}
873
874void put_md_name(char *name)
875{
ca3b6696 876 if (strncmp(name, "/dev/.tmp.md", 12) == 0)
e0d19036
NB
877 unlink(name);
878}
435d4ebb 879#endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
f9ce90ba 880
cc742d38
N
881int get_maj_min(char *dev, int *major, int *minor)
882{
883 char *e;
884 *major = strtoul(dev, &e, 0);
885 return (e > dev && *e == ':' && e[1] &&
886 (*minor = strtoul(e+1, &e, 0)) >= 0 &&
887 *e == 0);
888}
889
8b0dabea
NB
890int dev_open(char *dev, int flags)
891{
892 /* like 'open', but if 'dev' matches %d:%d, create a temp
893 * block device and open that
894 */
8b0dabea
NB
895 int fd = -1;
896 char devname[32];
e81cdd9f 897 int major;
8b0dabea 898 int minor;
e81cdd9f
NB
899
900 if (!dev) return -1;
6df6a774 901 flags |= O_DIRECT;
e81cdd9f 902
cc742d38 903 if (get_maj_min(dev, &major, &minor)) {
9dc7d357
N
904 snprintf(devname, sizeof(devname), "/dev/.tmp.md.%d:%d:%d",
905 (int)getpid(), major, minor);
906 if (mknod(devname, S_IFBLK|0600, makedev(major, minor)) == 0) {
907 fd = open(devname, flags);
908 unlink(devname);
6df6a774
N
909 }
910 if (fd < 0) {
9dc7d357 911 /* Try /tmp as /dev appear to be read-only */
6df6a774
N
912 snprintf(devname, sizeof(devname), "/tmp/.tmp.md.%d:%d:%d",
913 (int)getpid(), major, minor);
ca3b6696 914 if (mknod(devname, S_IFBLK|0600, makedev(major, minor)) == 0) {
6df6a774
N
915 fd = open(devname, flags);
916 unlink(devname);
917 }
8b0dabea
NB
918 }
919 } else
6df6a774 920 fd = open(dev, flags);
8b0dabea
NB
921 return fd;
922}
f9ce90ba 923
4dd2df09 924int open_dev_flags(char *devnm, int flags)
e8a70c89 925{
4dd2df09 926 int devid;
e8a70c89
N
927 char buf[20];
928
4dd2df09
N
929 devid = devnm2devid(devnm);
930 sprintf(buf, "%d:%d", major(devid), minor(devid));
d998b738
N
931 return dev_open(buf, flags);
932}
933
4dd2df09 934int open_dev(char *devnm)
d998b738 935{
4dd2df09 936 return open_dev_flags(devnm, O_RDONLY);
e8a70c89
N
937}
938
4dd2df09 939int open_dev_excl(char *devnm)
a931db9e
NB
940{
941 char buf[20];
942 int i;
7187750e 943 int flags = O_RDWR;
4dd2df09 944 int devid = devnm2devid(devnm);
399e0b97 945 long delay = 1000;
a931db9e 946
4dd2df09 947 sprintf(buf, "%d:%d", major(devid), minor(devid));
ca3b6696 948 for (i = 0 ; i < 25 ; i++) {
7187750e 949 int fd = dev_open(buf, flags|O_EXCL);
a931db9e
NB
950 if (fd >= 0)
951 return fd;
7187750e
N
952 if (errno == EACCES && flags == O_RDWR) {
953 flags = O_RDONLY;
954 continue;
955 }
a931db9e
NB
956 if (errno != EBUSY)
957 return fd;
399e0b97
N
958 usleep(delay);
959 if (delay < 200000)
960 delay *= 2;
a931db9e
NB
961 }
962 return -1;
963}
964
9008ed1c
N
965int same_dev(char *one, char *two)
966{
967 struct stat st1, st2;
968 if (stat(one, &st1) != 0)
969 return 0;
970 if (stat(two, &st2) != 0)
971 return 0;
972 if ((st1.st_mode & S_IFMT) != S_IFBLK)
973 return 0;
974 if ((st2.st_mode & S_IFMT) != S_IFBLK)
975 return 0;
976 return st1.st_rdev == st2.st_rdev;
977}
978
a7c6e3fb 979void wait_for(char *dev, int fd)
a714580e
N
980{
981 int i;
a7c6e3fb 982 struct stat stb_want;
399e0b97 983 long delay = 1000;
a7c6e3fb
N
984
985 if (fstat(fd, &stb_want) != 0 ||
986 (stb_want.st_mode & S_IFMT) != S_IFBLK)
987 return;
a714580e 988
ca3b6696 989 for (i = 0 ; i < 25 ; i++) {
a714580e 990 struct stat stb;
a7c6e3fb
N
991 if (stat(dev, &stb) == 0 &&
992 (stb.st_mode & S_IFMT) == S_IFBLK &&
993 (stb.st_rdev == stb_want.st_rdev))
a714580e 994 return;
399e0b97
N
995 usleep(delay);
996 if (delay < 200000)
997 delay *= 2;
a714580e 998 }
436305c6 999 if (i == 25)
1ade5cc1 1000 dprintf("timeout waiting for %s\n", dev);
a714580e
N
1001}
1002
0f22b998
N
1003struct superswitch *superlist[] =
1004{
1005 &super0, &super1,
1006 &super_ddf, &super_imsm,
0592faeb 1007 &mbr, &gpt,
0f22b998 1008 NULL };
f9ce90ba 1009
ea24acd0 1010#if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
f7dd881f 1011
4725bc31 1012struct supertype *super_by_fd(int fd, char **subarrayp)
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;
4dd2df09 1023 char container[32] = "";
1686dc25 1024
4dd2df09 1025 sra = sysfs_read(fd, NULL, GET_VERSION);
1686dc25
NB
1026
1027 if (sra) {
7e0f6979
NB
1028 vers = sra->array.major_version;
1029 minor = sra->array.minor_version;
142cb9e1 1030 verstr = sra->text_version;
1686dc25
NB
1031 } else {
1032 if (ioctl(fd, GET_ARRAY_INFO, &array))
1033 array.major_version = array.minor_version = 0;
1034 vers = array.major_version;
1035 minor = array.minor_version;
142cb9e1 1036 verstr = "";
6fbba4c9 1037 }
82d9eba6 1038
1686dc25
NB
1039 if (vers != -1) {
1040 sprintf(version, "%d.%d", vers, minor);
1041 verstr = version;
6fbba4c9 1042 }
3c558363 1043 if (minor == -2 && is_subarray(verstr)) {
f7e7067b 1044 char *dev = verstr+1;
5f7e44b2 1045
f7e7067b 1046 subarray = strchr(dev, '/');
92d49ecf 1047 if (subarray) {
f7e7067b 1048 *subarray++ = '\0';
503975b9 1049 subarray = xstrdup(subarray);
92d49ecf 1050 }
4dd2df09 1051 strcpy(container, dev);
f7e7067b
NB
1052 if (sra)
1053 sysfs_free(sra);
5f7e44b2 1054 sra = sysfs_read(-1, container, GET_VERSION);
603f24a0
N
1055 if (sra && sra->text_version[0])
1056 verstr = sra->text_version;
1057 else
1058 verstr = "-no-metadata-";
f7e7067b
NB
1059 }
1060
1061 for (i = 0; st == NULL && superlist[i] ; i++)
1062 st = superlist[i]->match_metadata_desc(verstr);
1686dc25
NB
1063
1064 if (sra)
1065 sysfs_free(sra);
f7e7067b 1066 if (st) {
3b0896f8 1067 st->sb = NULL;
1f49fb3a
N
1068 if (subarrayp)
1069 *subarrayp = subarray;
4dd2df09
N
1070 strcpy(st->container_devnm, container);
1071 strcpy(st->devnm, fd2devnm(fd));
4725bc31
N
1072 } else
1073 free(subarray);
5f7e44b2 1074
82d9eba6 1075 return st;
f9ce90ba 1076}
ea24acd0
NB
1077#endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
1078
0f0749ad 1079int dev_size_from_id(dev_t id, unsigned long long *size)
52d5d101
AC
1080{
1081 char buf[20];
1082 int fd;
1083
1084 sprintf(buf, "%d:%d", major(id), minor(id));
1085 fd = dev_open(buf, O_RDONLY);
1086 if (fd < 0)
1087 return 0;
1088 if (get_dev_size(fd, NULL, size)) {
1089 close(fd);
1090 return 1;
1091 }
1092 close(fd);
1093 return 0;
1094}
f9ce90ba 1095
159c3a1a 1096struct supertype *dup_super(struct supertype *orig)
3da92f27 1097{
159c3a1a 1098 struct supertype *st;
1686dc25 1099
d2ca6449
NB
1100 if (!orig)
1101 return orig;
503975b9 1102 st = xcalloc(1, sizeof(*st));
159c3a1a
NB
1103 st->ss = orig->ss;
1104 st->max_devs = orig->max_devs;
1105 st->minor_version = orig->minor_version;
cb8f6859 1106 st->ignore_hw_compat = orig->ignore_hw_compat;
23bf42cc 1107 st->data_offset = orig->data_offset;
159c3a1a
NB
1108 st->sb = NULL;
1109 st->info = NULL;
1110 return st;
3da92f27
NB
1111}
1112
54887ad8 1113struct supertype *guess_super_type(int fd, enum guess_types guess_type)
f9ce90ba
NB
1114{
1115 /* try each load_super to find the best match,
1116 * and return the best superswitch
1117 */
82d9eba6
NB
1118 struct superswitch *ss;
1119 struct supertype *st;
f21e18ca 1120 time_t besttime = 0;
570c0542 1121 int bestsuper = -1;
f9ce90ba
NB
1122 int i;
1123
503975b9 1124 st = xcalloc(1, sizeof(*st));
4dd2df09 1125 st->container_devnm[0] = 0;
d1d599ea 1126
ca3b6696 1127 for (i = 0 ; superlist[i]; i++) {
f9ce90ba
NB
1128 int rv;
1129 ss = superlist[i];
54887ad8
N
1130 if (guess_type == guess_array && ss->add_to_super == NULL)
1131 continue;
1132 if (guess_type == guess_partitions && ss->add_to_super != NULL)
1133 continue;
ef609477 1134 memset(st, 0, sizeof(*st));
df3346e6 1135 st->ignore_hw_compat = 1;
3da92f27 1136 rv = ss->load_super(st, fd, NULL);
570c0542
NB
1137 if (rv == 0) {
1138 struct mdinfo info;
a5d85af7 1139 st->ss->getinfo_super(st, &info, NULL);
570c0542
NB
1140 if (bestsuper == -1 ||
1141 besttime < info.array.ctime) {
1142 bestsuper = i;
1143 besttime = info.array.ctime;
570c0542 1144 }
3da92f27 1145 ss->free_super(st);
570c0542
NB
1146 }
1147 }
1148 if (bestsuper != -1) {
1149 int rv;
ef609477 1150 memset(st, 0, sizeof(*st));
df3346e6 1151 st->ignore_hw_compat = 1;
3da92f27 1152 rv = superlist[bestsuper]->load_super(st, fd, NULL);
f9ce90ba 1153 if (rv == 0) {
5e747af2 1154 superlist[bestsuper]->free_super(st);
82d9eba6 1155 return st;
f9ce90ba
NB
1156 }
1157 }
570c0542 1158 free(st);
f9ce90ba
NB
1159 return NULL;
1160}
fe6729fa 1161
beae1dfe
NB
1162/* Return size of device in bytes */
1163int get_dev_size(int fd, char *dname, unsigned long long *sizep)
1164{
1165 unsigned long long ldsize;
c2c9bb6f
NB
1166 struct stat st;
1167
1168 if (fstat(fd, &st) != -1 && S_ISREG(st.st_mode))
1169 ldsize = (unsigned long long)st.st_size;
1170 else
beae1dfe
NB
1171#ifdef BLKGETSIZE64
1172 if (ioctl(fd, BLKGETSIZE64, &ldsize) != 0)
1173#endif
1174 {
1175 unsigned long dsize;
1176 if (ioctl(fd, BLKGETSIZE, &dsize) == 0) {
1177 ldsize = dsize;
1178 ldsize <<= 9;
1179 } else {
1180 if (dname)
e7b84f9d 1181 pr_err("Cannot get size of %s: %s\b",
beae1dfe
NB
1182 dname, strerror(errno));
1183 return 0;
1184 }
1185 }
1186 *sizep = ldsize;
1187 return 1;
1188}
8fac0577 1189
3a371610
N
1190/* Return true if this can only be a container, not a member device.
1191 * i.e. is and md device and size is zero
1192 */
1193int must_be_container(int fd)
1194{
1195 unsigned long long size;
1196 if (md_get_version(fd) < 0)
1197 return 0;
1198 if (get_dev_size(fd, NULL, &size) == 0)
1199 return 1;
1200 if (size == 0)
1201 return 1;
1202 return 0;
1203}
034b203a
TM
1204
1205/* Sets endofpart parameter to the last block used by the last GPT partition on the device.
1206 * Returns: 1 if successful
1207 * -1 for unknown partition type
1208 * 0 for other errors
1209 */
1210static int get_gpt_last_partition_end(int fd, unsigned long long *endofpart)
1211{
056b331e 1212 struct GPT gpt;
034b203a 1213 unsigned char empty_gpt_entry[16]= {0};
db7fdfe4
JS
1214 struct GPT_part_entry *part;
1215 char buf[512];
034b203a
TM
1216 unsigned long long curr_part_end;
1217 unsigned all_partitions, entry_size;
f21e18ca 1218 unsigned part_nr;
034b203a
TM
1219
1220 *endofpart = 0;
1221
056b331e 1222 BUILD_BUG_ON(sizeof(gpt) != 512);
73e658d8 1223 /* skip protective MBR */
034b203a 1224 lseek(fd, 512, SEEK_SET);
73e658d8 1225 /* read GPT header */
056b331e 1226 if (read(fd, &gpt, 512) != 512)
034b203a
TM
1227 return 0;
1228
1229 /* get the number of partition entries and the entry size */
056b331e
N
1230 all_partitions = __le32_to_cpu(gpt.part_cnt);
1231 entry_size = __le32_to_cpu(gpt.part_size);
034b203a
TM
1232
1233 /* Check GPT signature*/
056b331e 1234 if (gpt.magic != GPT_SIGNATURE_MAGIC)
034b203a
TM
1235 return -1;
1236
1237 /* sanity checks */
1238 if (all_partitions > 1024 ||
db7fdfe4 1239 entry_size > sizeof(buf))
034b203a
TM
1240 return -1;
1241
db7fdfe4
JS
1242 part = (struct GPT_part_entry *)buf;
1243
ca3b6696 1244 for (part_nr = 0; part_nr < all_partitions; part_nr++) {
73e658d8 1245 /* read partition entry */
db7fdfe4 1246 if (read(fd, buf, entry_size) != (ssize_t)entry_size)
73e658d8
LB
1247 return 0;
1248
034b203a 1249 /* is this valid partition? */
db7fdfe4 1250 if (memcmp(part->type_guid, empty_gpt_entry, 16) != 0) {
034b203a 1251 /* check the last lba for the current partition */
db7fdfe4 1252 curr_part_end = __le64_to_cpu(part->ending_lba);
034b203a
TM
1253 if (curr_part_end > *endofpart)
1254 *endofpart = curr_part_end;
1255 }
1256
034b203a
TM
1257 }
1258 return 1;
1259}
1260
1261/* Sets endofpart parameter to the last block used by the last partition on the device.
1262 * Returns: 1 if successful
1263 * -1 for unknown partition type
1264 * 0 for other errors
1265 */
1266static int get_last_partition_end(int fd, unsigned long long *endofpart)
1267{
056b331e 1268 struct MBR boot_sect;
034b203a
TM
1269 struct MBR_part_record *part;
1270 unsigned long long curr_part_end;
f21e18ca 1271 unsigned part_nr;
034b203a
TM
1272 int retval = 0;
1273
1274 *endofpart = 0;
1275
056b331e 1276 BUILD_BUG_ON(sizeof(boot_sect) != 512);
034b203a
TM
1277 /* read MBR */
1278 lseek(fd, 0, 0);
056b331e 1279 if (read(fd, &boot_sect, 512) != 512)
034b203a
TM
1280 goto abort;
1281
1282 /* check MBP signature */
056b331e 1283 if (boot_sect.magic == MBR_SIGNATURE_MAGIC) {
034b203a
TM
1284 retval = 1;
1285 /* found the correct signature */
056b331e 1286 part = boot_sect.parts;
034b203a 1287
ca3b6696 1288 for (part_nr = 0; part_nr < MBR_PARTITIONS; part_nr++) {
034b203a
TM
1289 /* check for GPT type */
1290 if (part->part_type == MBR_GPT_PARTITION_TYPE) {
1291 retval = get_gpt_last_partition_end(fd, endofpart);
1292 break;
1293 }
1294 /* check the last used lba for the current partition */
1295 curr_part_end = __le32_to_cpu(part->first_sect_lba) +
1296 __le32_to_cpu(part->blocks_num);
1297 if (curr_part_end > *endofpart)
1298 *endofpart = curr_part_end;
1299
1300 part++;
1301 }
1302 } else {
1303 /* Unknown partition table */
1304 retval = -1;
1305 }
1306 abort:
1307 return retval;
1308}
1309
53ed6ac3
KW
1310int check_partitions(int fd, char *dname, unsigned long long freesize,
1311 unsigned long long size)
034b203a
TM
1312{
1313 /*
1314 * Check where the last partition ends
1315 */
1316 unsigned long long endofpart;
1317 int ret;
1318
1319 if ((ret = get_last_partition_end(fd, &endofpart)) > 0) {
1320 /* There appears to be a partition table here */
1321 if (freesize == 0) {
1322 /* partitions will not be visible in new device */
e7b84f9d
N
1323 pr_err("partition table exists on %s but will be lost or\n"
1324 " meaningless after creating array\n",
1325 dname);
034b203a
TM
1326 return 1;
1327 } else if (endofpart > freesize) {
1328 /* last partition overlaps metadata */
e7b84f9d
N
1329 pr_err("metadata will over-write last partition on %s.\n",
1330 dname);
034b203a 1331 return 1;
53ed6ac3
KW
1332 } else if (size && endofpart > size) {
1333 /* partitions will be truncated in new device */
e7b84f9d
N
1334 pr_err("array size is too small to cover all partitions on %s.\n",
1335 dname);
53ed6ac3 1336 return 1;
034b203a
TM
1337 }
1338 }
1339 return 0;
1340}
1341
a322f70c
DW
1342int open_container(int fd)
1343{
1344 /* 'fd' is a block device. Find out if it is in use
1345 * by a container, and return an open fd on that container.
1346 */
1347 char path[256];
1348 char *e;
1349 DIR *dir;
1350 struct dirent *de;
1351 int dfd, n;
1352 char buf[200];
1353 int major, minor;
1354 struct stat st;
1355
1356 if (fstat(fd, &st) != 0)
1357 return -1;
1358 sprintf(path, "/sys/dev/block/%d:%d/holders",
1359 (int)major(st.st_rdev), (int)minor(st.st_rdev));
1360 e = path + strlen(path);
1361
1362 dir = opendir(path);
1363 if (!dir)
1364 return -1;
1365 while ((de = readdir(dir))) {
1366 if (de->d_ino == 0)
1367 continue;
1368 if (de->d_name[0] == '.')
1369 continue;
a994592d
N
1370 /* Need to make sure it is a container and not a volume */
1371 sprintf(e, "/%s/md/metadata_version", de->d_name);
1372 dfd = open(path, O_RDONLY);
1373 if (dfd < 0)
1374 continue;
1375 n = read(dfd, buf, sizeof(buf));
1376 close(dfd);
1377 if (n <= 0 || (unsigned)n >= sizeof(buf))
1378 continue;
1379 buf[n] = 0;
1380 if (strncmp(buf, "external", 8) != 0 ||
1381 n < 10 ||
1382 buf[9] == '/')
1383 continue;
a322f70c
DW
1384 sprintf(e, "/%s/dev", de->d_name);
1385 dfd = open(path, O_RDONLY);
1386 if (dfd < 0)
1387 continue;
1388 n = read(dfd, buf, sizeof(buf));
1389 close(dfd);
f21e18ca 1390 if (n <= 0 || (unsigned)n >= sizeof(buf))
a322f70c
DW
1391 continue;
1392 buf[n] = 0;
1393 if (sscanf(buf, "%d:%d", &major, &minor) != 2)
1394 continue;
1395 sprintf(buf, "%d:%d", major, minor);
1396 dfd = dev_open(buf, O_RDONLY);
1397 if (dfd >= 0) {
1398 closedir(dir);
1399 return dfd;
1400 }
1401 }
355726fa 1402 closedir(dir);
a322f70c
DW
1403 return -1;
1404}
1405
33414a01
DW
1406struct superswitch *version_to_superswitch(char *vers)
1407{
1408 int i;
1409
1410 for (i = 0; superlist[i]; i++) {
1411 struct superswitch *ss = superlist[i];
1412
1413 if (strcmp(vers, ss->name) == 0)
1414 return ss;
1415 }
1416
1417 return NULL;
1418}
1419
4dd2df09
N
1420int metadata_container_matches(char *metadata, char *devnm)
1421{
1422 /* Check if 'devnm' is the container named in 'metadata'
1423 * which is
1424 * /containername/componentname or
1425 * -containername/componentname
1426 */
1427 int l;
1428 if (*metadata != '/' && *metadata != '-')
1429 return 0;
1430 l = strlen(devnm);
1431 if (strncmp(metadata+1, devnm, l) != 0)
1432 return 0;
1433 if (metadata[l+1] != '/')
1434 return 0;
1435 return 1;
1436}
1437
1438int metadata_subdev_matches(char *metadata, char *devnm)
1439{
1440 /* Check if 'devnm' is the subdev named in 'metadata'
1441 * which is
1442 * /containername/subdev or
1443 * -containername/subdev
1444 */
1445 char *sl;
1446 if (*metadata != '/' && *metadata != '-')
1447 return 0;
1448 sl = strchr(metadata+1, '/');
1449 if (!sl)
1450 return 0;
1451 if (strcmp(sl+1, devnm) == 0)
1452 return 1;
1453 return 0;
1454}
1455
33414a01
DW
1456int is_container_member(struct mdstat_ent *mdstat, char *container)
1457{
1458 if (mdstat->metadata_version == NULL ||
1459 strncmp(mdstat->metadata_version, "external:", 9) != 0 ||
4dd2df09 1460 !metadata_container_matches(mdstat->metadata_version+9, container))
33414a01
DW
1461 return 0;
1462
1463 return 1;
1464}
1465
1466int is_subarray_active(char *subarray, char *container)
1467{
1468 struct mdstat_ent *mdstat = mdstat_read(0, 0);
1469 struct mdstat_ent *ent;
1470
e5408a32
DW
1471 for (ent = mdstat; ent; ent = ent->next)
1472 if (is_container_member(ent, container))
e5e5d7ce 1473 if (strcmp(to_subarray(ent, container), subarray) == 0)
33414a01 1474 break;
33414a01
DW
1475
1476 free_mdstat(mdstat);
1477
1478 return ent != NULL;
1479}
1480
1481/* open_subarray - opens a subarray in a container
1482 * @dev: container device name
feab51f8 1483 * @st: empty supertype
33414a01
DW
1484 * @quiet: block reporting errors flag
1485 *
1486 * On success returns an fd to a container and fills in *st
1487 */
feab51f8 1488int open_subarray(char *dev, char *subarray, struct supertype *st, int quiet)
33414a01
DW
1489{
1490 struct mdinfo *mdi;
a951a4f7 1491 struct mdinfo *info;
33414a01 1492 int fd, err = 1;
4dd2df09 1493 char *_devnm;
33414a01
DW
1494
1495 fd = open(dev, O_RDWR|O_EXCL);
1496 if (fd < 0) {
1497 if (!quiet)
e7b84f9d 1498 pr_err("Couldn't open %s, aborting\n",
33414a01 1499 dev);
b990032d 1500 return -1;
33414a01
DW
1501 }
1502
4dd2df09
N
1503 _devnm = fd2devnm(fd);
1504 if (_devnm == NULL) {
33414a01 1505 if (!quiet)
e7b84f9d
N
1506 pr_err("Failed to determine device number for %s\n",
1507 dev);
33414a01
DW
1508 goto close_fd;
1509 }
4dd2df09 1510 strcpy(st->devnm, _devnm);
33414a01 1511
4dd2df09 1512 mdi = sysfs_read(fd, st->devnm, GET_VERSION|GET_LEVEL);
33414a01
DW
1513 if (!mdi) {
1514 if (!quiet)
e7b84f9d 1515 pr_err("Failed to read sysfs for %s\n",
33414a01
DW
1516 dev);
1517 goto close_fd;
1518 }
1519
1520 if (mdi->array.level != UnSet) {
1521 if (!quiet)
e7b84f9d 1522 pr_err("%s is not a container\n", dev);
33414a01
DW
1523 goto free_sysfs;
1524 }
1525
1526 st->ss = version_to_superswitch(mdi->text_version);
1527 if (!st->ss) {
1528 if (!quiet)
e7b84f9d
N
1529 pr_err("Operation not supported for %s metadata\n",
1530 mdi->text_version);
33414a01
DW
1531 goto free_sysfs;
1532 }
1533
4dd2df09 1534 if (st->devnm[0] == 0) {
33414a01 1535 if (!quiet)
e7b84f9d 1536 pr_err("Failed to allocate device name\n");
33414a01
DW
1537 goto free_sysfs;
1538 }
1539
db20d413 1540 if (!st->ss->load_container) {
33414a01 1541 if (!quiet)
e7b84f9d 1542 pr_err("%s is not a container\n", dev);
4dd2df09 1543 goto free_sysfs;
33414a01
DW
1544 }
1545
db20d413 1546 if (st->ss->load_container(st, fd, NULL)) {
33414a01 1547 if (!quiet)
e7b84f9d 1548 pr_err("Failed to load metadata for %s\n",
db20d413 1549 dev);
4dd2df09 1550 goto free_sysfs;
33414a01
DW
1551 }
1552
a951a4f7
N
1553 info = st->ss->container_content(st, subarray);
1554 if (!info) {
1555 if (!quiet)
e7b84f9d 1556 pr_err("Failed to find subarray-%s in %s\n",
a951a4f7
N
1557 subarray, dev);
1558 goto free_super;
1559 }
1560 free(info);
1561
33414a01
DW
1562 err = 0;
1563
1564 free_super:
1565 if (err)
1566 st->ss->free_super(st);
33414a01
DW
1567 free_sysfs:
1568 sysfs_free(mdi);
1569 close_fd:
1570 if (err)
1571 close(fd);
1572
1573 if (err)
1574 return -1;
1575 else
1576 return fd;
1577}
1578
7801ac20
N
1579int add_disk(int mdfd, struct supertype *st,
1580 struct mdinfo *sra, struct mdinfo *info)
1581{
1582 /* Add a device to an array, in one of 2 ways. */
1583 int rv;
1584#ifndef MDASSEMBLE
1585 if (st->ss->external) {
d23534e4
DW
1586 if (info->disk.state & (1<<MD_DISK_SYNC))
1587 info->recovery_start = MaxSector;
1588 else
1589 info->recovery_start = 0;
2904b26f 1590 rv = sysfs_add_disk(sra, info, 0);
7801ac20
N
1591 if (! rv) {
1592 struct mdinfo *sd2;
f35f2525
N
1593 for (sd2 = sra->devs; sd2; sd2=sd2->next)
1594 if (sd2 == info)
1595 break;
1596 if (sd2 == NULL) {
503975b9 1597 sd2 = xmalloc(sizeof(*sd2));
f35f2525
N
1598 *sd2 = *info;
1599 sd2->next = sra->devs;
1600 sra->devs = sd2;
1601 }
7801ac20
N
1602 }
1603 } else
1604#endif
1605 rv = ioctl(mdfd, ADD_NEW_DISK, &info->disk);
1606 return rv;
1607}
1608
de6ae750
N
1609int remove_disk(int mdfd, struct supertype *st,
1610 struct mdinfo *sra, struct mdinfo *info)
1611{
1612 int rv;
1613 /* Remove the disk given by 'info' from the array */
1614#ifndef MDASSEMBLE
1615 if (st->ss->external)
1616 rv = sysfs_set_str(sra, info, "slot", "none");
1617 else
1618#endif
1619 rv = ioctl(mdfd, HOT_REMOVE_DISK, makedev(info->disk.major,
1620 info->disk.minor));
1621 return rv;
1622}
1623
f35f2525
N
1624int set_array_info(int mdfd, struct supertype *st, struct mdinfo *info)
1625{
1626 /* Initialise kernel's knowledge of array.
1627 * This varies between externally managed arrays
1628 * and older kernels
1629 */
1630 int vers = md_get_version(mdfd);
1631 int rv;
1632
1633#ifndef MDASSEMBLE
1634 if (st->ss->external)
1635 rv = sysfs_set_array(info, vers);
1636 else
1637#endif
1638 if ((vers % 100) >= 1) { /* can use different versions */
1639 mdu_array_info_t inf;
1640 memset(&inf, 0, sizeof(inf));
1641 inf.major_version = info->array.major_version;
1642 inf.minor_version = info->array.minor_version;
1643 rv = ioctl(mdfd, SET_ARRAY_INFO, &inf);
1644 } else
1645 rv = ioctl(mdfd, SET_ARRAY_INFO, NULL);
1646 return rv;
1647}
1648
1e5c6983
DW
1649unsigned long long min_recovery_start(struct mdinfo *array)
1650{
1651 /* find the minimum recovery_start in an array for metadata
1652 * formats that only record per-array recovery progress instead
1653 * of per-device
1654 */
1655 unsigned long long recovery_start = MaxSector;
1656 struct mdinfo *d;
1657
1658 for (d = array->devs; d; d = d->next)
1659 recovery_start = min(recovery_start, d->recovery_start);
1660
1661 return recovery_start;
1662}
1663
4dd2df09 1664int mdmon_pid(char *devnm)
a931db9e
NB
1665{
1666 char path[100];
1667 char pid[10];
1668 int fd;
1669 int n;
10013317 1670
4dd2df09 1671 sprintf(path, "%s/%s.pid", MDMON_DIR, devnm);
10013317 1672
24f6f99b 1673 fd = open(path, O_RDONLY | O_NOATIME, 0);
a931db9e
NB
1674
1675 if (fd < 0)
cf556303 1676 return -1;
a931db9e
NB
1677 n = read(fd, pid, 9);
1678 close(fd);
1679 if (n <= 0)
cf556303 1680 return -1;
24f6f99b 1681 return atoi(pid);
a931db9e
NB
1682}
1683
4dd2df09 1684int mdmon_running(char *devnm)
a931db9e 1685{
4dd2df09 1686 int pid = mdmon_pid(devnm);
24f6f99b 1687 if (pid <= 0)
a931db9e 1688 return 0;
24f6f99b 1689 if (kill(pid, 0) == 0)
a931db9e
NB
1690 return 1;
1691 return 0;
1692}
1693
4dd2df09 1694int start_mdmon(char *devnm)
8850ee3e 1695{
1913c325 1696 int i, skipped;
44d2e365 1697 int len;
ca3b6696 1698 pid_t pid;
9fe32043 1699 int status;
44d2e365
N
1700 char pathbuf[1024];
1701 char *paths[4] = {
1702 pathbuf,
85945e19 1703 BINDIR "/mdmon",
2f1bcf43 1704 "./mdmon",
44d2e365
N
1705 NULL
1706 };
8850ee3e 1707
40ebbb9c 1708 if (check_env("MDADM_NO_MDMON"))
8850ee3e
N
1709 return 0;
1710
9cf014ec 1711 len = readlink("/proc/self/exe", pathbuf, sizeof(pathbuf)-1);
44d2e365
N
1712 if (len > 0) {
1713 char *sl;
1714 pathbuf[len] = 0;
1715 sl = strrchr(pathbuf, '/');
1716 if (sl)
1717 sl++;
1718 else
1719 sl = pathbuf;
1720 strcpy(sl, "mdmon");
1721 } else
1722 pathbuf[0] = '\0';
1723
0f7bdf89 1724 /* First try to run systemctl */
9540cc24
N
1725 if (!check_env("MDADM_NO_SYSTEMCTL"))
1726 switch(fork()) {
1727 case 0:
1728 /* FIXME yuk. CLOSE_EXEC?? */
1729 skipped = 0;
1730 for (i = 3; skipped < 20; i++)
1731 if (close(i) < 0)
1732 skipped++;
1733 else
1734 skipped = 0;
1735
1736 /* Don't want to see error messages from
1737 * systemctl. If the service doesn't exist,
1738 * we start mdmon ourselves.
1739 */
1740 close(2);
1741 open("/dev/null", O_WRONLY);
1742 snprintf(pathbuf, sizeof(pathbuf), "mdmon@%s.service",
1743 devnm);
1744 status = execl("/usr/bin/systemctl", "systemctl",
1745 "start",
1746 pathbuf, NULL);
1747 status = execl("/bin/systemctl", "systemctl", "start",
1748 pathbuf, NULL);
1749 exit(1);
7a862a02 1750 case -1: pr_err("cannot run mdmon. Array remains readonly\n");
9540cc24
N
1751 return -1;
1752 default: /* parent - good */
1753 pid = wait(&status);
1754 if (pid >= 0 && status == 0)
1755 return 0;
1756 }
0f7bdf89
JS
1757
1758 /* That failed, try running mdmon directly */
8850ee3e
N
1759 switch(fork()) {
1760 case 0:
1761 /* FIXME yuk. CLOSE_EXEC?? */
1913c325 1762 skipped = 0;
ca3b6696 1763 for (i = 3; skipped < 20; i++)
1913c325
N
1764 if (close(i) < 0)
1765 skipped++;
1766 else
1767 skipped = 0;
1768
ca3b6696 1769 for (i = 0; paths[i]; i++)
a0963a86 1770 if (paths[i][0]) {
2f1bcf43 1771 execl(paths[i], paths[i],
4dd2df09 1772 devnm, NULL);
a0963a86 1773 }
8850ee3e 1774 exit(1);
7a862a02 1775 case -1: pr_err("cannot run mdmon. Array remains readonly\n");
8850ee3e 1776 return -1;
9fe32043
N
1777 default: /* parent - good */
1778 pid = wait(&status);
15c10423 1779 if (pid < 0 || status != 0) {
7a862a02 1780 pr_err("failed to launch mdmon. Array remains readonly\n");
9fe32043 1781 return -1;
15c10423 1782 }
8850ee3e
N
1783 }
1784 return 0;
1785}
1786
148acb7b
DW
1787__u32 random32(void)
1788{
1789 __u32 rv;
1790 int rfd = open("/dev/urandom", O_RDONLY);
1791 if (rfd < 0 || read(rfd, &rv, 4) != 4)
1792 rv = random();
1793 if (rfd >= 0)
1794 close(rfd);
1795 return rv;
1796}
1797
0e600426 1798#ifndef MDASSEMBLE
edd8d13c
NB
1799int flush_metadata_updates(struct supertype *st)
1800{
1801 int sfd;
1802 if (!st->updates) {
1803 st->update_tail = NULL;
1804 return -1;
1805 }
1806
4dd2df09 1807 sfd = connect_monitor(st->container_devnm);
edd8d13c
NB
1808 if (sfd < 0)
1809 return -1;
1810
1811 while (st->updates) {
1812 struct metadata_update *mu = st->updates;
1813 st->updates = mu->next;
1814
1815 send_message(sfd, mu, 0);
1816 wait_reply(sfd, 0);
1817 free(mu->buf);
1818 free(mu);
1819 }
1820 ack(sfd, 0);
1821 wait_reply(sfd, 0);
1822 close(sfd);
1823 st->update_tail = NULL;
1824 return 0;
1825}
1826
1827void append_metadata_update(struct supertype *st, void *buf, int len)
1828{
1829
503975b9 1830 struct metadata_update *mu = xmalloc(sizeof(*mu));
edd8d13c
NB
1831
1832 mu->buf = buf;
1833 mu->len = len;
1834 mu->space = NULL;
cb23f1f4 1835 mu->space_list = NULL;
edd8d13c
NB
1836 mu->next = NULL;
1837 *st->update_tail = mu;
1838 st->update_tail = &mu->next;
1839}
0e600426 1840#endif /* MDASSEMBLE */
a931db9e 1841
fe6729fa
NB
1842#ifdef __TINYC__
1843/* tinyc doesn't optimize this check in ioctl.h out ... */
1844unsigned int __invalid_size_argument_for_IOC = 0;
1845#endif
1846
6d11ec6f
AK
1847int experimental(void)
1848{
1849 if (check_env("MDADM_EXPERIMENTAL"))
1850 return 1;
1851 else {
7a862a02 1852 pr_err("To use this feature MDADM_EXPERIMENTAL environment variable has to be defined.\n");
6d11ec6f
AK
1853 return 0;
1854 }
1855}
1856
326727d9
AC
1857/* Pick all spares matching given criteria from a container
1858 * if min_size == 0 do not check size
1859 * if domlist == NULL do not check domains
1860 * if spare_group given add it to domains of each spare
1861 * metadata allows to test domains using metadata of destination array */
1862struct mdinfo *container_choose_spares(struct supertype *st,
1863 unsigned long long min_size,
1864 struct domainlist *domlist,
1865 char *spare_group,
1866 const char *metadata, int get_one)
1867{
1868 struct mdinfo *d, **dp, *disks = NULL;
1869
1870 /* get list of all disks in container */
1871 if (st->ss->getinfo_super_disks)
1872 disks = st->ss->getinfo_super_disks(st);
1873
1874 if (!disks)
1875 return disks;
1876 /* find spare devices on the list */
1877 dp = &disks->devs;
1878 disks->array.spare_disks = 0;
1879 while (*dp) {
1880 int found = 0;
1881 d = *dp;
1882 if (d->disk.state == 0) {
1883 /* check if size is acceptable */
1884 unsigned long long dev_size;
1885 dev_t dev = makedev(d->disk.major,d->disk.minor);
1886
1887 if (!min_size ||
1888 (dev_size_from_id(dev, &dev_size) &&
1889 dev_size >= min_size))
1890 found = 1;
1891 /* check if domain matches */
1892 if (found && domlist) {
4dd2df09 1893 struct dev_policy *pol = devid_policy(dev);
326727d9
AC
1894 if (spare_group)
1895 pol_add(&pol, pol_domain,
1896 spare_group, NULL);
e5508b36 1897 if (domain_test(domlist, pol, metadata) != 1)
326727d9
AC
1898 found = 0;
1899 dev_policy_free(pol);
1900 }
1901 }
1902 if (found) {
1903 dp = &d->next;
1904 disks->array.spare_disks++;
1905 if (get_one) {
1906 sysfs_free(*dp);
1907 d->next = NULL;
1908 }
1909 } else {
1910 *dp = d->next;
1911 d->next = NULL;
1912 sysfs_free(d);
1913 }
1914 }
1915 return disks;
1916}
9eafa1de
MN
1917
1918/* Checks if paths point to the same device
1919 * Returns 0 if they do.
1920 * Returns 1 if they don't.
1921 * Returns -1 if something went wrong,
1922 * e.g. paths are empty or the files
1923 * they point to don't exist */
1924int compare_paths (char* path1, char* path2)
1925{
1926 struct stat st1,st2;
1927
1928 if (path1 == NULL || path2 == NULL)
1929 return -1;
1930 if (stat(path1,&st1) != 0)
1931 return -1;
1932 if (stat(path2,&st2) != 0)
1933 return -1;
1934 if ((st1.st_ino == st2.st_ino) && (st1.st_dev == st2.st_dev))
1935 return 0;
1936 return 1;
1937}
a7dec3fd
N
1938
1939/* Make sure we can open as many devices as needed */
1940void enable_fds(int devices)
1941{
1942 unsigned int fds = 20 + devices;
1943 struct rlimit lim;
1944 if (getrlimit(RLIMIT_NOFILE, &lim) != 0
1945 || lim.rlim_cur >= fds)
1946 return;
1947 if (lim.rlim_max < fds)
1948 lim.rlim_max = fds;
1949 lim.rlim_cur = fds;
1950 setrlimit(RLIMIT_NOFILE, &lim);
1951}
a9c15847
N
1952
1953int in_initrd(void)
1954{
1955 /* This is based on similar function in systemd. */
1956 struct statfs s;
76d0f188
JS
1957 /* statfs.f_type is signed long on s390x and MIPS, causing all
1958 sorts of sign extension problems with RAMFS_MAGIC being
1959 defined as 0x858458f6 */
a9c15847 1960 return statfs("/", &s) >= 0 &&
7ac5d47e 1961 ((unsigned long)s.f_type == TMPFS_MAGIC ||
76d0f188
JS
1962 ((unsigned long)s.f_type & 0xFFFFFFFFUL) ==
1963 ((unsigned long)RAMFS_MAGIC & 0xFFFFFFFFUL));
a9c15847 1964}
8832342d
N
1965
1966void reopen_mddev(int mdfd)
1967{
1968 /* Re-open without any O_EXCL, but keep
1969 * the same fd
1970 */
1971 char *devnm;
1972 int fd;
1973 devnm = fd2devnm(mdfd);
1974 close(mdfd);
1975 fd = open_dev(devnm);
1976 if (fd >= 0 && fd != mdfd)
1977 dup2(fd, mdfd);
1978}