323
325
* Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
326
unsigned char sb[1024];
328
330
if (lseek(fd, 64*1024, 0) != 64*1024)
330
332
if (read(fd, sb, 1024) != 1024)
332
if (strncmp((char *)sb+52, "ReIsErFs",8)!=0 &&
333
strncmp((char *)sb+52, "ReIsEr2Fs",9)!=0)
334
if (strncmp((char*)sb+52, "ReIsErFs",8)!=0 &&
335
strncmp((char*)sb+52, "ReIsEr2Fs",9)!=0)
335
337
fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name);
336
338
size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8;
414
416
else if (strncmp(d, "/md", 3)==0)
415
417
d += 3, type=-1; /* /dev/mdN */
416
418
else if (d-dev > 3 && strncmp(d-2, "md/", 3)==0)
417
d++, type = -1; /* /dev/md/N */
419
d += 1, type=-1; /* /dev/md/N */
455
#define __USE_XOPEN_EXTENDED
462
int add_dev_1(const char *name, const struct stat *stb, int flag)
464
return add_dev(name, stb, flag, NULL);
466
int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
468
ftw(path, add_dev_1, nopenfd);
460
472
int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
462
int add_dev(const char *name, const struct stat *stb, int flag)
465
474
if ((stb->st_mode&S_IFMT)== S_IFBLK) {
466
475
char *n = strdup(name);
480
489
* Find a block device with the right major/minor number.
481
* Avoid /dev/mdNN and /dev/md/dNN if possible
490
* If we find multiple names, choose the shortest.
491
* If we find a non-standard name, it is probably there
492
* deliberately so prefer it over a standard name.
493
* This applies only to names for MD devices.
483
495
char *map_dev(int major, int minor)
485
497
struct devmap *p;
498
char *std = NULL, *nonstd=NULL;
487
499
if (!devlist_ready) {
489
500
nftw("/dev", add_dev, 10, FTW_PHYS);
491
ftw("/dev", add_dev, 10);
496
504
for (p=devlist; p; p=p->next)
497
505
if (p->major == major &&
498
506
p->minor == minor) {
499
if (is_standard(p->name, NULL))
507
if (is_standard(p->name, NULL)) {
509
strlen(p->name) < strlen(std))
512
if (nonstd == NULL ||
513
strlen(p->name) < strlen(nonstd))
517
return nonstd ? nonstd : std;
525
538
char *human_size(long long bytes)
527
540
static char buf[30];
542
/* We convert bytes to either centi-M{ega,ibi}bytes or
543
* centi-G{igi,ibi}bytes, with appropriate rounding,
544
* and then print 1/100th of those as a decimal.
545
* We allow upto 2048Megabytes before converting to
546
* gigabytes, as that shows more precision and isn't
547
* too large a number.
548
* Terrabytes are not yet handled.
530
551
if (bytes < 1024*1024)
532
else if (bytes < 1024LL*1024LL*1024LL)
533
sprintf(buf, " (%ld.%02ld MiB %ld.%02ld MB)",
534
(long)((bytes+0x100000/200)>>20),
535
(long)((bytes+0x100000/200)&0xfffff)/(0x100000/100),
536
(long)((bytes+5000)/1000/1000),
537
(long)(((bytes+5000)%1000000)/10000)
540
sprintf(buf, " (%ld.%02ld GiB %ld.%02ld GB)",
541
(long)((bytes+0x100000*1024/200)>>30),
542
(long)(((bytes+0x100000*1024/200)>>10)&0xfffff)/(0x100000/100),
543
(long)((bytes+5000000LL)/1000LL/1000LL/1000LL),
544
(long)((((bytes+5000000LL)/1000)%1000000)/10000)
553
else if (bytes < 2*1024LL*1024LL*1024LL) {
554
long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2;
555
long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2;
556
snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)",
557
cMiB/100 , cMiB % 100,
560
long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2;
561
long cGB = (bytes / (1000000000LL/200LL ) +1) /2;
562
snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)",
563
cGiB/100 , cGiB % 100,
551
571
static char buf[30];
554
if (bytes < 1024*1024)
555
sprintf(buf, "%ld.%02ldKiB",
556
(long)((bytes+512/100)>>10),
557
(long)(((bytes+512/100)&1023)*100/1024)
574
if (bytes < 5000*1024)
575
snprintf(buf, sizeof(buf), "%ld.%02ldKiB",
576
(long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
559
else if (bytes < 1024LL*1024LL*1024LL)
560
sprintf(buf, "%ld.%02ldMiB",
561
(long)((bytes+0x100000/200)>>20),
562
(long)((bytes+0x100000/200)&0xfffff)/(0x100000/100)
578
else if (bytes < 2*1024LL*1024LL*1024LL)
579
snprintf(buf, sizeof(buf), "%ld.%02ldMiB",
581
(long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
565
sprintf(buf, "%ld.%02ldGiB",
566
(long)((bytes+0x100000*1024/200)>>30),
567
(long)(((bytes+0x100000*1024/200)>>10)&0xfffff)/(0x100000/100)
584
snprintf(buf, sizeof(buf), "%ld.%02ldGiB",
586
(long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
612
631
int mdp = get_mdp_major();
613
632
if (mdp < 0) return NULL;
614
633
rdev = makedev(mdp, (-1-dev)<<6);
615
sprintf(devname, "/dev/md/d%d", -1-dev);
634
snprintf(devname, sizeof(devname), "/dev/md/d%d", -1-dev);
616
635
if (stat(devname, &stb) == 0
617
636
&& (S_IFMT&stb.st_mode) == S_IFBLK
618
637
&& (stb.st_rdev == rdev))
621
640
rdev = makedev(MD_MAJOR, dev);
622
sprintf(devname, "/dev/md%d", dev);
641
snprintf(devname, sizeof(devname), "/dev/md%d", dev);
623
642
if (stat(devname, &stb) == 0
624
643
&& (S_IFMT&stb.st_mode) == S_IFBLK
625
644
&& (stb.st_rdev == rdev))
628
sprintf(devname, "/dev/md/%d", dev);
647
snprintf(devname, sizeof(devname), "/dev/md/%d", dev);
629
648
if (stat(devname, &stb) == 0
630
649
&& (S_IFMT&stb.st_mode) == S_IFBLK
631
650
&& (stb.st_rdev == rdev))
634
653
dn = map_dev(major(rdev), minor(rdev));
637
sprintf(devname, "/dev/.tmp.md%d", dev);
656
snprintf(devname, sizeof(devname), "/dev/.tmp.md%d", dev);
638
657
if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
639
658
if (errno != EEXIST)