Re: non-linux support | |
| [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] | |
Hello,
This patch didn't get applied. Was there a problem with it?
Samuel
Samuel Thibault, le Tue 18 Dec 2007 02:16:59 +0000, a écrit :
> BLK* and HDIO_* are not available on all platform. Work around this in
> a few places, use mkswap's find_size as fallback for BLKGETSIZE, and
> disable blockdev.
>
Signed-off-by: Samuel Thibault <samuel.thibault@xxxxxxxxxxxx>
diff --git a/disk-utils/Makefile.am b/disk-utils/Makefile.am
index 8f80624..ec8923c 100644
--- a/disk-utils/Makefile.am
+++ b/disk-utils/Makefile.am
@@ -2,20 +2,21 @@ include $(top_srcdir)/config/include-Makefile.am
utils_common = ../lib/linux_version.c ../lib/blkdev.c
-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
if LINUX
-dist_man_MANS += fdformat.8
+dist_man_MANS += fdformat.8 blockdev.8
+sbin_PROGRAMS += blockdev
+blockdev_SOURCES = blockdev.c $(utils_common)
usrbinexec_PROGRAMS += fdformat
endif
@@ -34,8 +35,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 2a63ac9..2d082ef 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 557cde4..33f37c4 100644
--- a/disk-utils/mkfs.bfs.c
+++ b/disk-utils/mkfs.bfs.c
@@ -16,11 +16,7 @@
#include <string.h>
#include <time.h>
#include "nls.h"
-
-/* cannot include <linux/fs.h> */
-#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/mkfs.minix.c b/disk-utils/mkfs.minix.c
index 5a1e134..a5e0992 100644
--- a/disk-utils/mkfs.minix.c
+++ b/disk-utils/mkfs.minix.c
@@ -184,37 +184,6 @@ check_mount(void) {
die(_("%s is mounted; will not make a filesystem here!"));
}
-static long
-valid_offset (int fd, int offset) {
- char ch;
-
- if (lseek (fd, offset, 0) < 0)
- return 0;
- if (read (fd, &ch, 1) < 1)
- return 0;
- return 1;
-}
-
-static int
-count_blocks (int fd) {
- int high, low;
-
- low = 0;
- for (high = 1; valid_offset (fd, high); high *= 2)
- low = high;
- while (low < high - 1)
- {
- const int mid = (low + high) / 2;
-
- if (valid_offset (fd, mid))
- low = mid;
- else
- high = mid;
- }
- valid_offset (fd, 0);
- return (low + 1);
-}
-
static void
write_tables(void) {
/* Mark the super block valid. */
diff --git a/disk-utils/mkswap.c b/disk-utils/mkswap.c
index bb12672..5e62b2d 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;
diff --git a/fdisk/cfdisk.c b/fdisk/cfdisk.c
index 9d32a19..300518d 100644
--- a/fdisk/cfdisk.c
+++ b/fdisk/cfdisk.c
@@ -1685,6 +1685,7 @@ fill_p_info(void) {
clear_warning();
}
+#ifdef BLKFLSBUF
/* Blocks are visible in more than one way:
e.g. as block on /dev/hda and as block on /dev/hda3
By a bug in the Linux buffer cache, we will see the old
@@ -1694,6 +1695,7 @@ fill_p_info(void) {
so this only plays a role if we want to show volume labels. */
ioctl(fd, BLKFLSBUF); /* ignore errors */
/* e.g. Permission Denied */
+#endif
if (blkdev_get_sectors(fd, &llsectors) == -1)
fatal(_("Cannot get disk size"), 3);
@@ -1903,10 +1905,12 @@ write_part_table(void) {
}
if (is_bdev) {
+#ifdef BLKRRPART
sync();
sleep(2);
if (!ioctl(fd,BLKRRPART))
changed = TRUE;
+#endif
sync();
sleep(4);
diff --git a/fdisk/fdisk.c b/fdisk/fdisk.c
index c5e3f06..9bbef47 100644
--- a/fdisk/fdisk.c
+++ b/fdisk/fdisk.c
@@ -2232,10 +2232,15 @@ reread_partition_table(int leave) {
i = fstat(fd, &statbuf);
if (i == 0 && S_ISBLK(statbuf.st_mode)) {
- printf(_("Calling ioctl() to re-read partition table.\n"));
sync();
sleep(2);
+#ifdef BLKRRPART
+ printf(_("Calling ioctl() to re-read partition table.\n"));
i = ioctl(fd, BLKRRPART);
+#else
+ errno = ENOSYS;
+ i = 1;
+#endif
}
if (i) {
diff --git a/fdisk/fdisksgilabel.c b/fdisk/fdisksgilabel.c
index 14b2d61..9f87751 100644
--- a/fdisk/fdisksgilabel.c
+++ b/fdisk/fdisksgilabel.c
@@ -713,6 +713,7 @@ create_sgilabel(void)
res = blkdev_get_sectors(fd, &llsectors);
+#ifdef HDIO_GETGEO
if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
heads = geometry.heads;
sectors = geometry.sectors;
@@ -733,6 +734,7 @@ create_sgilabel(void)
" > 33.8 GB.\n"), disk_device, cylinders);
}
}
+#endif
for (i = 0; i < 4; i++) {
old[i].sysid = 0;
if (valid_part_table_flag(MBRbuffer)) {
diff --git a/fdisk/fdisksunlabel.c b/fdisk/fdisksunlabel.c
index f4e8671..e6f1725 100644
--- a/fdisk/fdisksunlabel.c
+++ b/fdisk/fdisksunlabel.c
@@ -212,6 +212,7 @@ void create_sunlabel(void)
res = blkdev_get_sectors(fd, &llsectors);
sec_fac = sector_size / 512;
+#ifdef HDIO_GETGEO
if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
heads = geometry.heads;
sectors = geometry.sectors;
@@ -228,7 +229,9 @@ void create_sunlabel(void)
"This value may be truncated for devices"
" > 33.8 GB.\n"), disk_device, cylinders);
}
- } else {
+ } else
+#endif
+ {
heads = read_int(1,1,1024,0,_("Heads"));
sectors = read_int(1,1,1024,0,_("Sectors/track"));
cylinders = read_int(1,1,65535,0,_("Cylinders"));
diff --git a/fdisk/gpt.c b/fdisk/gpt.c
index 2c96b02..8cdf57f 100644
--- a/fdisk/gpt.c
+++ b/fdisk/gpt.c
@@ -131,6 +131,7 @@ last_lba(int fd)
return sectors - 1;
}
+#ifdef __linux__
static ssize_t
read_lastoddsector(int fd, uint64_t lba, void *buffer, size_t count)
{
@@ -148,6 +149,7 @@ read_lastoddsector(int fd, uint64_t lba, void *buffer, size_t count)
return !rc;
}
+#endif
static ssize_t
read_lba(int fd, uint64_t lba, void *buffer, size_t bytes)
@@ -159,6 +161,7 @@ read_lba(int fd, uint64_t lba, void *buffer, size_t bytes)
lseek(fd, offset, SEEK_SET);
bytesread = read(fd, buffer, bytes);
+#ifdef __linux__
/* Kludge. This is necessary to read/write the last
block of an odd-sized disk, until Linux 2.5.x kernel fixes.
This is only used by gpt.c, and only to read
@@ -166,6 +169,7 @@ read_lba(int fd, uint64_t lba, void *buffer, size_t bytes)
*/
if (!bytesread && !(last_lba(fd) & 1) && lba == last_lba(fd))
bytesread = read_lastoddsector(fd, lba, buffer, bytes);
+#endif
return bytesread;
}
diff --git a/fdisk/sfdisk.c b/fdisk/sfdisk.c
index a3b6617..2c0b174 100644
--- a/fdisk/sfdisk.c
+++ b/fdisk/sfdisk.c
@@ -446,7 +446,10 @@ get_geometry(char *dev, int fd, int silent) {
unsigned long long sectors;
struct geometry R;
- if (ioctl(fd, HDIO_GETGEO, &g)) {
+#ifdef HDIO_GETGEO
+ if (ioctl(fd, HDIO_GETGEO, &g))
+#endif
+ {
g.heads = g.sectors = g.cylinders = g.start = 0;
if (!silent)
do_warn(_("Disk %s: cannot get geometry\n"), dev);
@@ -790,7 +793,12 @@ add_sector_and_offset(struct disk_desc *z) {
/* tell the kernel to reread the partition tables */
static int
reread_ioctl(int fd) {
- if (ioctl(fd, BLKRRPART)) {
+#ifdef BLKRRPART
+ if (ioctl(fd, BLKRRPART))
+#else
+ errno = ENOSYS;
+#endif
+ {
perror("BLKRRPART");
/* 2.6.8 returns EIO for a zero table */
diff --git a/include/blkdev.h b/include/blkdev.h
index 1f0fe67..8cc15e0 100644
--- a/include/blkdev.h
+++ b/include/blkdev.h
@@ -7,6 +7,7 @@
#define DEFAULT_SECTOR_SIZE 512
#ifndef BLKROSET
+#ifdef __linux__
#define BLKROSET _IO(0x12,93) /* set device read-only (0 = read-write) */
#define BLKROGET _IO(0x12,94) /* get read-only status (0 = read_write) */
@@ -31,10 +32,14 @@
#define BLKBSZSET _IOW(0x12,113,size_t)
#define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
+#endif
+
#endif /* BLKROSET */
#ifndef HDIO_GETGEO
+#ifdef __linux__
#define HDIO_GETGEO 0x0301
+#endif
struct hd_geometry {
unsigned char heads;
unsigned char sectors;
@@ -43,6 +48,9 @@ struct hd_geometry {
};
#endif
+/* Determine size in bytes */
+off_t blkdev_find_size (int fd);
+
/* get size in bytes */
int blkdev_get_size(int fd, unsigned long long *bytes);
diff --git a/lib/blkdev.c b/lib/blkdev.c
index 29c5d0b..1580a4b 100644
--- a/lib/blkdev.c
+++ b/lib/blkdev.c
@@ -1,17 +1,52 @@
#include <sys/types.h>
#include <sys/ioctl.h>
+#include <unistd.h>
#include "blkdev.h"
#include "linux_version.h"
+static long
+blkdev_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;
+}
+
+off_t
+blkdev_find_size (int fd) {
+ off_t high, low;
+
+ low = 0;
+ for (high = 1; high > 0 && blkdev_valid_offset (fd, high); high *= 2)
+ low = high;
+ while (low < high - 1)
+ {
+ const off_t mid = (low + high) / 2;
+
+ if (blkdev_valid_offset (fd, mid))
+ low = mid;
+ else
+ high = mid;
+ }
+ blkdev_valid_offset (fd, 0);
+ return (low + 1);
+}
+
/* get size in bytes */
int
blkdev_get_size(int fd, unsigned long long *bytes)
{
+#ifdef __linux__
unsigned long size;
- int ver = get_linux_version();
+#ifdef BLKGETSIZE64
+ int ver = get_linux_version();
+ /* TODO: use stat as well */
/* kernels 2.4.15-2.4.17, had a broken BLKGETSIZE64 */
if (ver >= KERNEL_VERSION (2,6,0) ||
(ver >= KERNEL_VERSION (2,4,18) && ver < KERNEL_VERSION (2,5,0))) {
@@ -19,12 +54,18 @@ blkdev_get_size(int fd, unsigned long long *bytes)
if (ioctl(fd, BLKGETSIZE64, bytes) >= 0)
return 0;
}
+#endif
+#ifdef BLKGETSIZE
if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
*bytes = ((unsigned long long)size << 9);
return 0;
}
return -1;
+#endif
+#endif
+ *bytes = blkdev_find_size(fd);
+ return 0;
}
/* get 512-byte sector count */
@@ -45,14 +86,21 @@ blkdev_get_sectors(int fd, unsigned long long *sectors)
int
blkdev_get_sector_size(int fd, int *sector_size)
{
+#ifdef BLKSSZGET
+#ifdef __linux__
if (get_linux_version() < KERNEL_VERSION(2,3,3)) {
*sector_size = DEFAULT_SECTOR_SIZE;
return 0;
}
+#endif
if (ioctl(fd, BLKSSZGET, sector_size) >= 0)
return 0;
return -1;
+#else
+ *sector_size = DEFAULT_SECTOR_SIZE;
+ return 0;
+#endif
}
--
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
[Site Home] [Netdev] [Ethernet Bridging] [Linux Wireless] [Kernel Newbies] [Memory] [Security] [Linux for Hams] [Netfilter] [Bugtraq] [Rubini] [Photo] [Yosemite] [Yosemite News] [MIPS Linux] [ARM Linux] [Linux RAID] [Linux Admin] [Samba] [Video 4 Linux] [Linux Resources]