From: Samuel Thibault Date: Wed, 23 Jul 2008 23:15:07 +0000 (+0200) Subject: disk-utils: clean up code, use blkdev_* functions X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=098fa6b1201bd66bc8fcb54b103ff241e3136dca;p=util-linux disk-utils: clean up code, use blkdev_* functions [kzak@redhat.com: split the original patch to small patches] Signed-off-by: Samuel Thibault Signed-off-by: Karel Zak --- diff --git a/disk-utils/Makefile.am b/disk-utils/Makefile.am index ef9ceab4..f2d62452 100644 --- a/disk-utils/Makefile.am +++ b/disk-utils/Makefile.am @@ -2,25 +2,26 @@ include $(top_srcdir)/config/include-Makefile.am utils_common = ../lib/blkdev.c if LINUX -utils_common += ../lib/linux_version.c +utils_common += ../lib/linux_version.c endif -dist_man_MANS = blockdev.8 isosize.8 mkfs.8 mkswap.8 \ +dist_man_MANS = isosize.8 mkfs.8 mkswap.8 \ fsck.minix.8 mkfs.minix.8 mkfs.bfs.8 -sbin_PROGRAMS = mkfs mkswap blockdev fsck.minix mkfs.minix mkfs.bfs +sbin_PROGRAMS = mkfs mkswap fsck.minix mkfs.minix mkfs.bfs fsck_minix_SOURCES = fsck.minix.c bitops.h minix.h mkfs_minix_SOURCES = mkfs.minix.c bitops.h minix.h $(utils_common) -mkfs_bfs_SOURCES = mkfs.bfs.c +mkfs_bfs_SOURCES = mkfs.bfs.c $(utils_common) mkswap_SOURCES = mkswap.c swapheader.h $(utils_common) -blockdev_SOURCES = blockdev.c $(utils_common) usrbinexec_PROGRAMS = isosize usrsbinexec_PROGRAMS = if LINUX -dist_man_MANS += fdformat.8 +dist_man_MANS += fdformat.8 blockdev.8 +sbin_PROGRAMS += blockdev usrsbinexec_PROGRAMS += fdformat +blockdev_SOURCES = blockdev.c $(utils_common) endif if BUILD_ELVTUNE @@ -38,8 +39,8 @@ endif if BUILD_CRAMFS sbin_PROGRAMS += fsck.cramfs mkfs.cramfs -fsck_cramfs_SOURCES = fsck.cramfs.c cramfs.h -mkfs_cramfs_SOURCES = mkfs.cramfs.c cramfs.h ../lib/md5.c +fsck_cramfs_SOURCES = fsck.cramfs.c cramfs.h $(utils_common) +mkfs_cramfs_SOURCES = mkfs.cramfs.c cramfs.h ../lib/md5.c $(utils_common) fsck_cramfs_LDADD = -lz mkfs_cramfs_LDADD = -lz endif diff --git a/disk-utils/fsck.cramfs.c b/disk-utils/fsck.cramfs.c index 2a63ac90..2d082efb 100644 --- a/disk-utils/fsck.cramfs.c +++ b/disk-utils/fsck.cramfs.c @@ -56,8 +56,7 @@ #include "cramfs.h" #include "nls.h" - -#define BLKGETSIZE _IO(0x12,96) /* return device size */ +#include "blkdev.h" static const char *progname = "cramfsck"; @@ -152,10 +151,11 @@ static void test_super(int *start, size_t *length) { die(FSCK_ERROR, 1, "open failed: %s", filename); } if (S_ISBLK(st.st_mode)) { - if (ioctl(fd, BLKGETSIZE, length) < 0) { + unsigned long long bytes; + if (blkdev_get_size(fd, &bytes)) { die(FSCK_ERROR, 1, "ioctl failed: unable to determine device size: %s", filename); } - *length = *length * 512; + *length = bytes; } else if (S_ISREG(st.st_mode)) { *length = st.st_size; diff --git a/disk-utils/mkfs.bfs.c b/disk-utils/mkfs.bfs.c index 557cde48..33f37c4f 100644 --- a/disk-utils/mkfs.bfs.c +++ b/disk-utils/mkfs.bfs.c @@ -16,11 +16,7 @@ #include #include #include "nls.h" - -/* cannot include */ -#ifndef BLKGETSIZE -#define BLKGETSIZE _IO(0x12,96) /* return device size */ -#endif +#include "blkdev.h" #define BFS_ROOT_INO 2 #define BFS_NAMELEN 14 @@ -95,8 +91,8 @@ int main(int argc, char *argv[]) { char *device, *volume, *fsname; int inodes; - unsigned long total_blocks, ino_bytes, ino_blocks, data_blocks; - unsigned long user_specified_total_blocks = 0; + unsigned long long total_blocks, ino_bytes, ino_blocks, data_blocks; + unsigned long long user_specified_total_blocks = 0; int verbose = 0; int fd; struct bfssb sb; @@ -177,19 +173,19 @@ main(int argc, char *argv[]) { } if (optind == argc-1) - user_specified_total_blocks = atoi(argv[optind]); + user_specified_total_blocks = atoll(argv[optind]); else if (optind != argc) usage(); - if (ioctl(fd, BLKGETSIZE, &total_blocks) == -1) { + if (blkdev_get_sectors(fd, &total_blocks) == -1) { if (!user_specified_total_blocks) { - perror("BLKGETSIZE"); + perror("blkdev_get_sectors"); fatal(_("cannot get size of %s"), device); } total_blocks = user_specified_total_blocks; } else if (user_specified_total_blocks) { if (user_specified_total_blocks > total_blocks) - fatal(_("blocks argument too large, max is %lu"), + fatal(_("blocks argument too large, max is %llu"), total_blocks); total_blocks = user_specified_total_blocks; } @@ -213,7 +209,7 @@ main(int argc, char *argv[]) { /* mimic the behaviour of SCO's mkfs - maybe this limit is needed */ if (data_blocks < 32) - fatal(_("not enough space, need at least %lu blocks"), + fatal(_("not enough space, need at least %llu blocks"), ino_blocks + 33); memset(&sb, 0, sizeof(sb)); @@ -233,9 +229,9 @@ main(int argc, char *argv[]) { fprintf(stderr, _("Inodes: %d (in 1 block)\n"), inodes); else - fprintf(stderr, _("Inodes: %d (in %ld blocks)\n"), + fprintf(stderr, _("Inodes: %d (in %lld blocks)\n"), inodes, ino_blocks); - fprintf(stderr, _("Blocks: %ld\n"), total_blocks); + fprintf(stderr, _("Blocks: %lld\n"), total_blocks); fprintf(stderr, _("Inode end: %d, Data end: %d\n"), sb.s_start-1, sb.s_end); } diff --git a/disk-utils/mkswap.c b/disk-utils/mkswap.c index 8bf358ed..0bac74ab 100644 --- a/disk-utils/mkswap.c +++ b/disk-utils/mkswap.c @@ -388,35 +388,6 @@ check_blocks(void) { printf(_("%lu bad pages\n"), badpages); } -static long -valid_offset (int fd, off_t offset) { - char ch; - - if (lseek (fd, offset, 0) < 0) - return 0; - if (read (fd, &ch, 1) < 1) - return 0; - return 1; -} - -static off_t -find_size (int fd) { - off_t high, low; - - low = 0; - for (high = 1; high > 0 && valid_offset (fd, high); high *= 2) - low = high; - while (low < high - 1) { - const off_t mid = (low + high) / 2; - - if (valid_offset (fd, mid)) - low = mid; - else - high = mid; - } - return (low + 1); -} - /* return size in pages, to avoid integer overflow */ static unsigned long get_size(const char *file) { @@ -431,7 +402,7 @@ get_size(const char *file) { if (blkdev_get_size(fd, &size) == 0) size /= pagesize; else - size = find_size(fd) / pagesize; + size = blkdev_find_size(fd) / pagesize; close(fd); return size;