From 93d5c38791a3778eed18fe968cdd136d23d0fa07 Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Wed, 21 May 2003 17:28:29 -0400 Subject: [PATCH] mke2fs.c (PRS, set_fs_defaults): If the sector size of the device is larger than the default block size, then use the sector size of the device as the default block size. getsectsize.c (ext2fs_get_device_sectsize): New function which returns the hardware sector size (if it is available). --- lib/ext2fs/ChangeLog | 5 +++ lib/ext2fs/Makefile.in | 6 ++++ lib/ext2fs/ext2fs.h | 3 ++ lib/ext2fs/getsectsize.c | 60 ++++++++++++++++++++++++++++++++++++ lib/ext2fs/tst_getsectsize.c | 47 ++++++++++++++++++++++++++++ misc/ChangeLog | 6 ++++ misc/mke2fs.c | 16 ++++++++-- 7 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 lib/ext2fs/getsectsize.c create mode 100644 lib/ext2fs/tst_getsectsize.c diff --git a/lib/ext2fs/ChangeLog b/lib/ext2fs/ChangeLog index 5153d71f..d9a50191 100644 --- a/lib/ext2fs/ChangeLog +++ b/lib/ext2fs/ChangeLog @@ -1,3 +1,8 @@ +2003-05-21 Theodore Ts'o + + * getsectsize.c (ext2fs_get_device_sectsize): New function which + returns the hardware sector size (if it is available). + 2003-05-13 Theodore Ts'o * unix_io.c: Add #ifdef NO_IO_CACHE which disables all userspace diff --git a/lib/ext2fs/Makefile.in b/lib/ext2fs/Makefile.in index 679d0d11..c99537a8 100644 --- a/lib/ext2fs/Makefile.in +++ b/lib/ext2fs/Makefile.in @@ -40,6 +40,7 @@ OBJS= $(DEBUGFS_LIB_OBJS) $(RESIZE_LIB_OBJS) $(E2IMAGE_LIB_OBJS) \ gen_bitmap.o \ get_pathname.o \ getsize.o \ + getsectsize.o \ icount.o \ initialize.o \ inline.o \ @@ -91,6 +92,7 @@ SRCS= ext2_err.c \ $(srcdir)/gen_bitmap.c \ $(srcdir)/get_pathname.c \ $(srcdir)/getsize.c \ + $(srcdir)/getsectsize.c \ $(srcdir)/icount.c \ $(srcdir)/imager.c \ $(srcdir)/initialize.c \ @@ -203,6 +205,10 @@ tst_bitops: tst_bitops.o inline.o $(STATIC_LIBEXT2FS) $(CC) -o tst_bitops tst_bitops.o inline.o \ $(STATIC_LIBEXT2FS) $(LIBCOM_ERR) +tst_getsectsize: tst_getsectsize.o getsectsize.o $(STATIC_LIBEXT2FS) + $(CC) -o tst_sectgetsize tst_getsectsize.o getsectsize.o \ + -DDEBUG $(STATIC_LIBEXT2FS) $(LIBCOM_ERR) + mkjournal: mkjournal.c $(STATIC_LIBEXT2FS) $(CC) -o mkjournal $(srcdir)/mkjournal.c -DDEBUG $(STATIC_LIBEXT2FS) $(LIBCOM_ERR) $(ALL_CFLAGS) diff --git a/lib/ext2fs/ext2fs.h b/lib/ext2fs/ext2fs.h index 926d5af4..cd9344d4 100644 --- a/lib/ext2fs/ext2fs.h +++ b/lib/ext2fs/ext2fs.h @@ -720,6 +720,9 @@ extern void ext2fs_u32_list_free(ext2_u32_list bb); extern errcode_t ext2fs_get_device_size(const char *file, int blocksize, blk_t *retblocks); +/* getsectsize.c */ +errcode_t ext2fs_get_device_sectsize(const char *file, int *sectsize); + /* imager.c */ extern errcode_t ext2fs_image_inode_write(ext2_filsys fs, int fd, int flags); extern errcode_t ext2fs_image_inode_read(ext2_filsys fs, int fd, int flags); diff --git a/lib/ext2fs/getsectsize.c b/lib/ext2fs/getsectsize.c new file mode 100644 index 00000000..66c6df63 --- /dev/null +++ b/lib/ext2fs/getsectsize.c @@ -0,0 +1,60 @@ +/* + * getsectsize.c --- get the sector size of a device. + * + * Copyright (C) 1995, 1995 Theodore Ts'o. + * Copyright (C) 2003 VMware, Inc. + * + * %Begin-Header% + * This file may be redistributed under the terms of the GNU Public + * License. + * %End-Header% + */ + +#define _LARGEFILE_SOURCE +#define _LARGEFILE64_SOURCE + +#include +#if HAVE_UNISTD_H +#include +#endif +#if HAVE_ERRNO_H +#include +#endif +#include +#ifdef HAVE_LINUX_FD_H +#include +#include +#endif + +#if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE) +#define BLKSSZGET _IO(0x12,104)/* get block device sector size */ +#endif + +#include "ext2_fs.h" +#include "ext2fs.h" + +/* + * Returns the number of blocks in a partition + */ +errcode_t ext2fs_get_device_sectsize(const char *file, int *sectsize) +{ + int fd; + +#ifdef HAVE_OPEN64 + fd = open64(file, O_RDONLY); +#else + fd = open(file, O_RDONLY); +#endif + if (fd < 0) + return errno; + +#ifdef BLKSSZGET + if (ioctl(fd, BLKSSZGET, sectsize) >= 0) { + close(fd); + return 0; + } +#endif + *sectsize = 0; + close(fd); + return 0; +} diff --git a/lib/ext2fs/tst_getsectsize.c b/lib/ext2fs/tst_getsectsize.c new file mode 100644 index 00000000..9967b615 --- /dev/null +++ b/lib/ext2fs/tst_getsectsize.c @@ -0,0 +1,47 @@ +/* + * tst_getsize.c --- this function tests the getsize function + * + * Copyright (C) 1997 by Theodore Ts'o. + * + * %Begin-Header% + * This file may be redistributed under the terms of the GNU Public + * License. + * %End-Header% + */ + +#include +#include +#if HAVE_UNISTD_H +#include +#endif +#include +#include +#include +#include +#if HAVE_ERRNO_H +#include +#endif + +#include "ext2_fs.h" +#include "ext2fs.h" + +int main(int argc, char **argv) +{ + int sectsize; + int retval; + + if (argc < 2) { + fprintf(stderr, "Usage: %s device\n", argv[0]); + exit(1); + } + + retval = ext2fs_get_device_sectsize(argv[1], §size); + if (retval) { + com_err(argv[0], retval, + "while calling ext2fs_get_device_sectsize"); + exit(1); + } + printf("Device %s has a hardware sector size of %d.\n", + argv[1], sectsize); + exit(0); +} diff --git a/misc/ChangeLog b/misc/ChangeLog index 22e94b25..3f94898b 100644 --- a/misc/ChangeLog +++ b/misc/ChangeLog @@ -1,3 +1,9 @@ +2003-05-21 Theodore Ts'o + + * mke2fs.c (PRS, set_fs_defaults): If the sector size of the + device is larger than the default block size, then use the + sector size of the device as the default block size. + 2003-05-18 Theodore Ts'o * badblocks.c: Use an unsigned integer to support 4-byte test diff --git a/misc/mke2fs.c b/misc/mke2fs.c index d30729a5..545ec96c 100644 --- a/misc/mke2fs.c +++ b/misc/mke2fs.c @@ -147,7 +147,8 @@ struct mke2fs_defaults { static void set_fs_defaults(const char *fs_type, struct ext2_super_block *super, - int blocksize, int *inode_ratio) + int blocksize, int sector_size, + int *inode_ratio) { int megs; int ratio = 0; @@ -170,6 +171,8 @@ static void set_fs_defaults(const char *fs_type, blocksize : p->inode_ratio; use_bsize = p->blocksize; } + if (use_bsize < sector_size) + use_bsize = sector_size; if (blocksize <= 0) { if (use_bsize == DEF_MAX_BLOCKSIZE) use_bsize = sys_page_size; @@ -799,6 +802,7 @@ static void PRS(int argc, char *argv[]) int inode_ratio = 0; int inode_size = 0; int reserved_ratio = 5; + int sector_size = 0; ext2_ino_t num_inodes = 0; errcode_t retval; char * oldpath = getenv("PATH"); @@ -1187,7 +1191,15 @@ static void PRS(int argc, char *argv[]) ((tmp = getenv("MKE2FS_FIRST_META_BG")))) param.s_first_meta_bg = atoi(tmp); - set_fs_defaults(fs_type, ¶m, blocksize, &inode_ratio); + /* Get the hardware sector size, if available */ + retval = ext2fs_get_device_sectsize(device_name, §or_size); + if (retval) { + com_err(program_name, retval, + _("while trying to determine hardware sector size")); + exit(1); + } + + set_fs_defaults(fs_type, ¶m, blocksize, sector_size, &inode_ratio); blocksize = EXT2_BLOCK_SIZE(¶m); if (param.s_blocks_per_group) {