gfs2-utils: master - libgfs2: Clean up device geometry code

Steven Whitehouse swhiteho at fedoraproject.org
Thu Dec 1 15:05:33 UTC 2011


Gitweb:        http://git.fedorahosted.org/git/gfs2-utils.git?p=gfs2-utils.git;a=commitdiff;h=d427382d1a9fe110357e886d99d02f639593641d
Commit:        d427382d1a9fe110357e886d99d02f639593641d
Parent:        61c1a2ba81b7cd7f529261055e9f74af1722d141
Author:        Steven Whitehouse <swhiteho at redhat.com>
AuthorDate:    Thu Dec 1 11:49:10 2011 +0000
Committer:     Steven Whitehouse <swhiteho at redhat.com>
CommitterDate: Thu Dec 1 11:49:10 2011 +0000

libgfs2: Clean up device geometry code

The main part of this patch is the creation of a new structure
which will contain all the information that we can discover
about a device. Currently this means a struct stat plus the
results of various ioctls, and the size. A device in this case
might also be a regular file, of course.

Since all the code does is gather information, we can use the
same code in all the utilities.

The existing struct device had two fields which were unused
or almost entirely unused. Those have been removed, greatly
simplifying the code in that area.

At the same time this code also fixes a bug in the device
topology ioctls (the alignment offset and optimal i/o size
were switched in the original code).

I've done a few quick tests and all appears to be working
correctly. There should be no noticeable changes in
behaviour to the user. All the calculations should give
the same results, both before and after (aside from the ones
affected by the topology bug).

Signed-off-by: Steven Whitehouse <swhiteho at redhat.com>
---
 gfs2/edit/hexedit.c            |   11 ++--
 gfs2/edit/savemeta.c           |   10 +--
 gfs2/fsck/initialize.c         |   10 ++-
 gfs2/fsck/rgrepair.c           |   10 +---
 gfs2/libgfs2/Makefile.am       |    2 +-
 gfs2/libgfs2/device_geometry.c |  149 +++++++++++++++++-----------------------
 gfs2/libgfs2/fs_geometry.c     |    5 +-
 gfs2/libgfs2/libgfs2.h         |   40 +++++------
 gfs2/libgfs2/size.c            |   69 ------------------
 gfs2/mkfs/main_grow.c          |   11 +--
 gfs2/mkfs/main_mkfs.c          |   53 +++++----------
 11 files changed, 120 insertions(+), 250 deletions(-)

diff --git a/gfs2/edit/hexedit.c b/gfs2/edit/hexedit.c
index 952723f..267e8ca 100644
--- a/gfs2/edit/hexedit.c
+++ b/gfs2/edit/hexedit.c
@@ -1744,6 +1744,10 @@ static void read_superblock(int fd)
 	sbd.bsize = sbd.sd_sb.sb_bsize;
 	if (!sbd.bsize)
 		sbd.bsize = GFS2_DEFAULT_BSIZE;
+	if (lgfs2_get_dev_info(fd, &sbd.dinfo)) {
+		perror(sbd.device_name);
+		exit(-1);
+	}
 	compute_constants(&sbd);
 	if (sbd.gfs1 || (sbd.sd_sb.sb_header.mh_magic == GFS2_MAGIC &&
 		     sbd.sd_sb.sb_header.mh_type == GFS2_METATYPE_SB))
@@ -1751,12 +1755,7 @@ static void read_superblock(int fd)
 	else {
 		block = starting_blk = 0;
 	}
-	device_geometry(&sbd);
-	if (fix_device_geometry(&sbd)) {
-		fprintf(stderr, "Device is too small (%llu bytes)\n",
-			(unsigned long long)sbd.device.length << GFS2_BASIC_BLOCK_SHIFT);
-		exit(-1);
-	}
+	fix_device_geometry(&sbd);
 	if(sbd.gfs1) {
 		sbd.sd_inptrs = (sbd.bsize - sizeof(struct gfs_indirect)) /
 			sizeof(uint64_t);
diff --git a/gfs2/edit/savemeta.c b/gfs2/edit/savemeta.c
index 9d790fe..ed9a7b0 100644
--- a/gfs2/edit/savemeta.c
+++ b/gfs2/edit/savemeta.c
@@ -654,15 +654,11 @@ void savemeta(char *out_fn, int saveoption, int gziplevel)
 	if (!sbd.gfs1)
 		sbd.bsize = BUFSIZE;
 	if (!slow) {
-		if (device_geometry(&sbd)) {
-			fprintf(stderr, "Geometery error\n");
-			exit(-1);
-		}
-		if (fix_device_geometry(&sbd)) {
-			fprintf(stderr, "Device is too small (%llu bytes)\n",
-				(unsigned long long)sbd.device.length << GFS2_BASIC_BLOCK_SHIFT);
+		if (lgfs2_get_dev_info(sbd.device_fd, &sbd.dinfo)) {
+			perror(sbd.device_name);
 			exit(-1);
 		}
+		fix_device_geometry(&sbd);
 		sbd.rgtree.osi_node = NULL;
 		if (!sbd.gfs1)
 			sbd.sd_sb.sb_bsize = GFS2_DEFAULT_BSIZE;
diff --git a/gfs2/fsck/initialize.c b/gfs2/fsck/initialize.c
index 132d65a..a1f99d6 100644
--- a/gfs2/fsck/initialize.c
+++ b/gfs2/fsck/initialize.c
@@ -1091,7 +1091,7 @@ static int peruse_metadata(struct gfs2_sbd *sdp, uint64_t startblock)
  */
 static int sb_repair(struct gfs2_sbd *sdp)
 {
-	uint64_t real_device_size, half;
+	uint64_t half;
 	uint32_t known_bsize = 0;
 	unsigned char uuid[16];
 	int error = 0;
@@ -1109,8 +1109,7 @@ static int sb_repair(struct gfs2_sbd *sdp)
 		log_warn(_("Block size not apparent; checking elsewhere.\n"));
 		/* First, figure out the device size.  We need that so we can
 		   find a suitable start point to determine what's what. */
-		device_size(sdp->device_fd, &real_device_size);
-		half = real_device_size / 2; /* in bytes */
+		half = sdp->dinfo.size / 2; /* in bytes */
 		half /= sdp->bsize;
 		/* Start looking halfway through the device for gfs2
 		   structures.  If there aren't any at all, forget it. */
@@ -1466,6 +1465,11 @@ int initialize(struct gfs2_sbd *sdp, int force_check, int preen,
 		was_mounted_ro = 1;
 	}
 
+	if (lgfs2_get_dev_info(sdp->device_fd, &sdp->dinfo)) {
+		perror(sdp->device_name);
+		return FSCK_ERROR;
+	}
+
 	/* read in sb from disk */
 	if (fill_super_block(sdp))
 		return FSCK_ERROR;
diff --git a/gfs2/fsck/rgrepair.c b/gfs2/fsck/rgrepair.c
index c11558b..c3467ba 100644
--- a/gfs2/fsck/rgrepair.c
+++ b/gfs2/fsck/rgrepair.c
@@ -638,15 +638,7 @@ static int gfs2_rindex_calculate(struct gfs2_sbd *sdp, int *num_rgs)
 	*num_rgs = sdp->md.riinode->i_di.di_size / sizeof(struct gfs2_rindex);
 
 	sdp->rgcalc.osi_node = NULL;
-	if (device_geometry(sdp)) {
-		fprintf(stderr, _("Geometry error\n"));
-		exit(-1);
-	}
-	if (fix_device_geometry(sdp)) {
-		fprintf(stderr, _("Device is too small (%llu bytes)\n"),
-			(unsigned long long)sdp->device.length << GFS2_BASIC_BLOCK_SHIFT);
-		exit(-1);
-	}
+	fix_device_geometry(sdp);
 
 	/* Try all possible rgrp sizes: 2048, 1024, 512, 256, 128, 64, 32 */
 	for (sdp->rgsize = GFS2_DEFAULT_RGSIZE; sdp->rgsize >= 32;
diff --git a/gfs2/libgfs2/Makefile.am b/gfs2/libgfs2/Makefile.am
index bb5b0c5..ab03348 100644
--- a/gfs2/libgfs2/Makefile.am
+++ b/gfs2/libgfs2/Makefile.am
@@ -5,7 +5,7 @@ noinst_HEADERS		= libgfs2.h
 noinst_LTLIBRARIES	= libgfs2.la
 
 libgfs2_la_SOURCES	= block_list.c fs_bits.c gfs1.c misc.c rgrp.c super.c \
-			  buf.c fs_geometry.c gfs2_disk_hash.c ondisk.c size.c \
+			  buf.c fs_geometry.c gfs2_disk_hash.c ondisk.c \
 			  device_geometry.c fs_ops.c gfs2_log.c recovery.c \
 			  structures.c
 
diff --git a/gfs2/libgfs2/device_geometry.c b/gfs2/libgfs2/device_geometry.c
index b1ffdc3..1bee9c2 100644
--- a/gfs2/libgfs2/device_geometry.c
+++ b/gfs2/libgfs2/device_geometry.c
@@ -12,117 +12,96 @@
 #include <errno.h>
 #include <sys/ioctl.h>
 #include <linux/types.h>
+#include <linux/fs.h>
 
 #include "libgfs2.h"
 
+#ifndef BLKSSZGET
 #define BLKSSZGET _IO(0x12,104)   /* logical_block_size */
+#endif
+
+#ifndef BLKIOMIN
 #define BLKIOMIN _IO(0x12,120)    /* minimum_io_size */
+#endif
+
+#ifndef BLKIOOPT
 #define BLKIOOPT _IO(0x12,121)    /* optimal_io_size */
-#define BLKALIGNOFF _IO(0x12,122) /* alignment_offset */
-#define BLKPBSZGET _IO(0x12,123)  /* physical_block_size */
+#endif
 
-/**
- * device_topology - Get the device topology
- * Values not fetched are returned as zero.
- */
-int device_topology(struct gfs2_sbd *sdp)
-{
-	if (ioctl(sdp->device_fd, BLKSSZGET, &sdp->logical_block_size) < 0)
-		sdp->logical_block_size = 0;
-	if (ioctl(sdp->device_fd, BLKIOMIN, &sdp->minimum_io_size) < 0)
-		sdp->minimum_io_size = 0;
-	if (ioctl(sdp->device_fd, BLKALIGNOFF, &sdp->optimal_io_size) < 0)
-		sdp->optimal_io_size = 0;
-	if (ioctl(sdp->device_fd, BLKIOOPT, &sdp->alignment_offset) < 0)
-		sdp->alignment_offset = 0;
-	if (ioctl(sdp->device_fd, BLKPBSZGET, &sdp->physical_block_size) < 0)
-		sdp->physical_block_size = 0;
-	if (!sdp->debug)
-		return 0;
-
-	printf("\nDevice Topology:\n");
-	printf("  Logical block size: %u\n", sdp->logical_block_size);
-	printf("  Physical block size: %u\n", sdp->physical_block_size);
-	printf("  Minimum I/O size: %u\n", sdp->minimum_io_size);
-	printf("  Optimal I/O size: %u (0 means unknown)\n",
-	       sdp->optimal_io_size);
-	printf("  Alignment offset: %u\n", sdp->alignment_offset);
-	return 0;
-}
+#ifndef BLKALIGNOFF
+#define BLKALIGNOFF _IO(0x12,122) /* alignment_offset */
+#endif
 
-/**
- * device_geometry - Get the size of a device
- * @w: the command line
- *
- */
+#ifndef BLKPBSZGET
+#define BLKPBSZGET _IO(0x12,123)  /* physical_block_size */
+#endif
 
-int device_geometry(struct gfs2_sbd *sdp)
+int lgfs2_get_dev_info(int fd, struct lgfs2_dev_info *i)
 {
-	struct device *device = &sdp->device;
-	uint64_t bytes;
-	int error;
+	int ret;
+	int ro = 0;
+	off_t off;
+
+	memset(i, 0, sizeof(*i));
+
+	ret = fstat(fd, &i->stat);
+	if (ret < 0)
+		return ret;
+
+	switch (i->stat.st_mode & S_IFMT) {
+	case S_IFREG:
+		i->size = i->stat.st_size;
+		ret = fcntl(fd, F_GETFL, 0);
+		if (ret & O_RDONLY)
+			i->readonly = 1;
+		i->io_optimal_size = i->stat.st_blksize;
+		goto size_check;
+	case S_IFBLK:
+		break;
+	default:
+		errno = ENOTBLK;
+		return -1;
+	}
 
-	error = device_size(sdp->device_fd, &bytes);
-	if (error)
-		return error;
+	ioctl(fd, BLKRAGET, &i->ra_pages);
+	ioctl(fd, BLKBSZGET, &i->soft_block_size);
+	ioctl(fd, BLKSSZGET, &i->logical_block_size);
+	ioctl(fd, BLKIOMIN, &i->io_min_size);
+	ioctl(fd, BLKIOOPT, &i->io_optimal_size);
+	ioctl(fd, BLKPBSZGET, &i->physical_block_size);
+	ioctl(fd, BLKALIGNOFF, &i->io_align_offset);
+	ioctl(fd, BLKROGET, &ro);
+	if (ro)
+		i->readonly = 1;
+	off = lseek(fd, 0, SEEK_END);
+	if (off < 0)
+		return -1;
+	i->size = off;
 
-	if (sdp->debug)
-		printf("\nPartition size = %"PRIu64"\n",
-		       bytes >> GFS2_BASIC_BLOCK_SHIFT);
+size_check:
+	if (i->size < (1 << 20)) {
+		errno = ENOSPC;
+		return -1;
+	}
 
-	device->start = 0;
-	device->length = bytes >> GFS2_BASIC_BLOCK_SHIFT;
 	return 0;
 }
 
 /**
  * fix_device_geometry - round off address and lengths and convert to FS blocks
- * @w: the command line
+ * @sdp: The super block
  *
  */
 
-int fix_device_geometry(struct gfs2_sbd *sdp)
+void fix_device_geometry(struct gfs2_sbd *sdp)
 {
 	struct device *device = &sdp->device;
-	unsigned int bbsize = sdp->bsize >> GFS2_BASIC_BLOCK_SHIFT;
-	uint64_t start, length;
-	unsigned int remainder;
-
-	if (sdp->debug) {
-		printf("\nDevice Geometry:  (in basic blocks)\n");
-		printf("  start = %"PRIu64", length = %"PRIu64", rgf_flags = 0x%.8X\n",
-		       device->start,
-		       device->length,
-		       device->rgf_flags);
-	}
-
-	start = device->start;
-	length = device->length;
 
-	if (length < 1 << (20 - GFS2_BASIC_BLOCK_SHIFT)) {
-		errno = ENOSPC;
-		return -1;
-	}
-
-	remainder = start % bbsize;
-	if (remainder) {
-		length -= bbsize - remainder;
-		start += bbsize - remainder;
-	}
-
-	start /= bbsize;
-	length /= bbsize;
-
-	device->start = start;
-	device->length = length;
-	sdp->device_size = start + length;
+	device->length = sdp->dinfo.size / sdp->bsize;
 
 	if (sdp->debug) {
 		printf("\nDevice Geometry:  (in FS blocks)\n");
-		printf("  start = %"PRIu64", length = %"
-		       PRIu64", rgf_flags = 0x%.8X\n",
-		       device->start, device->length, device->rgf_flags);
-		printf("\nDevice Size: %"PRIu64"\n", sdp->device_size);
+		printf("  length = %"PRIu64"\n", device->length);
+		printf("\nDevice Size: %"PRIu64"\n", sdp->dinfo.size);
 	}
-	return 0;
 }
diff --git a/gfs2/libgfs2/fs_geometry.c b/gfs2/libgfs2/fs_geometry.c
index 5ff9813..fb55697 100644
--- a/gfs2/libgfs2/fs_geometry.c
+++ b/gfs2/libgfs2/fs_geometry.c
@@ -85,9 +85,6 @@ void compute_rgrp_layout(struct gfs2_sbd *sdp, struct osi_root *rgtree,
 	sdp->new_rgrps = 0;
 	dev = &sdp->device;
 
-	/* Reserve space for the superblock */
-	dev->start += sdp->sb_addr + 1;
-
 	/* If this is a new file system, compute the length and number */
 	/* of rgs based on the size of the device.                     */
 	/* If we have existing RGs (i.e. gfs2_grow) find the last one. */
@@ -128,7 +125,7 @@ void compute_rgrp_layout(struct gfs2_sbd *sdp, struct osi_root *rgtree,
 			rl = rgrp_insert(rgtree, rgaddr);
 			rl->length = rglength;
 		} else {
-			rgaddr = dev->start;
+			rgaddr = sdp->sb_addr + 1;
 			rl = rgrp_insert(rgtree, rgaddr);
 			rl->length = dev->length -
 				(nrgrp - 1) * (dev->length / nrgrp);
diff --git a/gfs2/libgfs2/libgfs2.h b/gfs2/libgfs2/libgfs2.h
index b24e555..c8230d6 100644
--- a/gfs2/libgfs2/libgfs2.h
+++ b/gfs2/libgfs2/libgfs2.h
@@ -10,6 +10,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <linux/types.h>
 #include <linux/limits.h>
 #include <endian.h>
@@ -74,6 +75,20 @@ __BEGIN_DECLS
 #define BLOCKMAP_BYTE_OFFSET4(x) ((x & 0x0000000000000001) << 2)
 #define BLOCKMAP_MASK4 (0xf)
 
+
+struct lgfs2_dev_info {
+	struct stat stat;
+	unsigned readonly:1;
+	long ra_pages;
+	int soft_block_size;
+	int logical_block_size;
+	unsigned int physical_block_size;
+	unsigned int io_min_size;
+	unsigned int io_optimal_size;
+	int io_align_offset;
+	uint64_t size;
+};
+
 static __inline__ __attribute__((noreturn, format (printf, 1, 2)))
 void die(const char *fmt, ...)
 {
@@ -86,9 +101,7 @@ void die(const char *fmt, ...)
 }
 
 struct device {
-	uint64_t start;
 	uint64_t length;
-	uint32_t rgf_flags;
 };
 
 struct gfs2_bitmap
@@ -208,8 +221,8 @@ struct gfs2_sbd {
 
 	int64_t time;
 
+	struct lgfs2_dev_info dinfo;
 	struct device device;
-	uint64_t device_size;
 
 	int device_fd;
 	int path_fd;
@@ -238,12 +251,6 @@ struct gfs2_sbd {
 	char metafs_path[PATH_MAX]; /* where metafs is mounted */
 	struct special_blocks eattr_blocks;
 
-	/* device topology information: */
-	uint32_t logical_block_size;
-	uint32_t minimum_io_size;
-	uint32_t optimal_io_size;
-	uint32_t alignment_offset;
-	uint32_t physical_block_size;
 	uint64_t rg_one_length;
 	uint64_t rg_length;
 	int gfs1;
@@ -311,9 +318,8 @@ extern int brelse(struct gfs2_buffer_head *bh);
 #define bcommit(bl) do { __bcommit(bl, __LINE__, __FUNCTION__); } while(0)
 
 /* device_geometry.c */
-extern int device_topology(struct gfs2_sbd *sdp);
-extern int device_geometry(struct gfs2_sbd *sdp);
-extern int fix_device_geometry(struct gfs2_sbd *sdp);
+extern int lgfs2_get_dev_info(int fd, struct lgfs2_dev_info *i);
+extern void fix_device_geometry(struct gfs2_sbd *sdp);
 
 /* fs_bits.c */
 #define BFITNOENT (0xFFFFFFFF)
@@ -415,16 +421,6 @@ extern unsigned int calc_tree_height(struct gfs2_inode *ip, uint64_t size);
 extern int write_journal(struct gfs2_sbd *sdp, unsigned int j,
 			 unsigned int blocks);
 
-/**
- * device_size - figure out a device's size
- * @fd: the file descriptor of a device
- * @bytes: the number of bytes the device holds
- *
- * Returns: -1 on error (with errno set), 0 on success (with @bytes set)
- */
-
-extern int device_size(int fd, uint64_t *bytes);
-
 /* gfs1.c - GFS1 backward compatibility structures and functions */
 
 #define GFS_FORMAT_SB           (100)  /* Super-Block */
diff --git a/gfs2/libgfs2/size.c b/gfs2/libgfs2/size.c
deleted file mode 100644
index 0cff065..0000000
--- a/gfs2/libgfs2/size.c
+++ /dev/null
@@ -1,69 +0,0 @@
-#include "clusterautoconfig.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdint.h>
-#include <inttypes.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <errno.h>
-#include <sys/mount.h>
-
-#include "libgfs2.h"
-
-#ifndef BLKGETSIZE64
-#define BLKGETSIZE64 _IOR(0x12, 114, size_t)
-#endif
-
-/**
- * do_device_size - determine the size of a Linux block device
- * @device: the path to the device node
- *
- * Returns: -1 on error (with errno set), 0 on success (with @bytes set)
- */
-
-static int do_device_size(int fd, uint64_t *bytes)
-{
-	off_t off;
-
-	off = lseek(fd, 0, SEEK_END);
-	if (off >= 0) {
-		*bytes = off;
-		return 0;
-	}
-
-	return -1;
-}
-
-/**
- * device_size - figure out a device's size
- * @fd: the file descriptor of a device
- * @bytes: the number of bytes the device holds
- *
- * Returns: -1 on error (with errno set), 0 on success (with @bytes set)
- */
-
-int device_size(int fd, uint64_t *bytes)
-{
-	struct stat st;
-	int error;
-
-	error = fstat(fd, &st);
-	if (error)
-		return error;
-
-	if (S_ISREG(st.st_mode)) {
-		*bytes = st.st_size;
-		return 0;
-	} else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
-		return do_device_size(fd, bytes);
-	else if (S_ISDIR(st.st_mode))
-		errno = EISDIR;
-	else
-		errno = EINVAL;
-
-	return -1;
-}
diff --git a/gfs2/mkfs/main_grow.c b/gfs2/mkfs/main_grow.c
index 5ed5a2d..3fe08a1 100644
--- a/gfs2/mkfs/main_grow.c
+++ b/gfs2/mkfs/main_grow.c
@@ -344,8 +344,8 @@ main_grow(int argc, char *argv[])
 			exit(EXIT_FAILURE);
 		}
 
-		if (device_geometry(sdp)) {
-			perror(_("Device geometry error"));
+		if (lgfs2_get_dev_info(sdp->device_fd, &sdp->dinfo) < 0) {
+			perror(sdp->device_name);
 			exit(EXIT_FAILURE);
 		}
 		log_info( _("Initializing lists...\n"));
@@ -363,12 +363,7 @@ main_grow(int argc, char *argv[])
 		if (sdp->gfs1) {
 			die( _("cannot grow gfs1 filesystem\n"));
 		}
-		if (fix_device_geometry(sdp)) {
-			fprintf(stderr, _("Device is too small (%llu bytes)\n"),
-				(unsigned long long)sdp->device.length << GFS2_BASIC_BLOCK_SHIFT);
-			exit(EXIT_FAILURE);
-		}
-
+		fix_device_geometry(sdp);
 		if (mount_gfs2_meta(sdp)) {
 			perror("GFS2 metafs");
 			exit(EXIT_FAILURE);
diff --git a/gfs2/mkfs/main_mkfs.c b/gfs2/mkfs/main_mkfs.c
index 700b434..95afe41 100644
--- a/gfs2/mkfs/main_mkfs.c
+++ b/gfs2/mkfs/main_mkfs.c
@@ -336,16 +336,16 @@ static void verify_bsize(struct gfs2_sbd *sdp)
 		die( _("block size must be a power of two between 512 and "
 		       "%d\n"), getpagesize());
 
-	if (sdp->bsize < sdp->logical_block_size) {
+	if (sdp->bsize < sdp->dinfo.logical_block_size) {
 		die( _("Error: Block size %d is less than minimum logical "
 		       "block size (%d).\n"), sdp->bsize,
-		     sdp->logical_block_size);
+		     sdp->dinfo.logical_block_size);
 	}
 
-	if (sdp->bsize < sdp->physical_block_size) {
+	if (sdp->bsize < sdp->dinfo.physical_block_size) {
 		printf( _("WARNING: Block size %d is inefficient because it "
 			  "is less than the physical block size (%d).\n"),
-			  sdp->bsize, sdp->physical_block_size);
+			  sdp->bsize, sdp->dinfo.physical_block_size);
 		if (sdp->override)
 			return;
 
@@ -528,9 +528,7 @@ void main_mkfs(int argc, char *argv[])
 	struct gfs2_sbd sbd, *sdp = &sbd;
 	int error;
 	int rgsize_specified = 0;
-	uint64_t real_device_size;
 	unsigned char uuid[16];
-	struct stat st_buf;
 
 	memset(sdp, 0, sizeof(struct gfs2_sbd));
 	sdp->bsize = -1;
@@ -555,7 +553,7 @@ void main_mkfs(int argc, char *argv[])
 		exit(EXIT_FAILURE);
 	}
 
-	if (fstat(sdp->device_fd, &st_buf) < 0) {
+	if (lgfs2_get_dev_info(sdp->device_fd, &sdp->dinfo) < 0) {
 		perror(sdp->device_name);
 		exit(EXIT_FAILURE);
 	}
@@ -566,24 +564,19 @@ void main_mkfs(int argc, char *argv[])
 		are_you_sure();
 	}
 
-	if (!S_ISREG(st_buf.st_mode) && device_topology(sdp)) {
-		perror(_("Device topology error\n"));
-		exit(EXIT_FAILURE);
-	}
-
 	if (sdp->bsize == -1) {
-		if (S_ISREG(st_buf.st_mode))
+		if (S_ISREG(sdp->dinfo.stat.st_mode))
 			sdp->bsize = GFS2_DEFAULT_BSIZE;
 		/* See if optimal_io_size (the biggest I/O we can submit
 		   without incurring a penalty) is a suitable block size. */
-		else if (sdp->optimal_io_size <= getpagesize() &&
-		    sdp->optimal_io_size >= sdp->minimum_io_size)
-			sdp->bsize = sdp->optimal_io_size;
+		else if (sdp->dinfo.io_optimal_size <= getpagesize() &&
+		    sdp->dinfo.io_optimal_size >= sdp->dinfo.io_min_size)
+			sdp->bsize = sdp->dinfo.io_optimal_size;
 		/* See if physical_block_size (the smallest unit we can write
 		   without incurring read-modify-write penalty) is suitable. */
-		else if (sdp->physical_block_size <= getpagesize() &&
-			 sdp->physical_block_size >= GFS2_DEFAULT_BSIZE)
-			sdp->bsize = sdp->physical_block_size;
+		else if (sdp->dinfo.physical_block_size <= getpagesize() &&
+			 sdp->dinfo.physical_block_size >= GFS2_DEFAULT_BSIZE)
+			sdp->bsize = sdp->dinfo.physical_block_size;
 		else
 			sdp->bsize = GFS2_DEFAULT_BSIZE;
 
@@ -597,13 +590,6 @@ void main_mkfs(int argc, char *argv[])
 		exit(EXIT_FAILURE);
 	}
 
-	/* Get the device geometry */
-
-	device_size(sdp->device_fd, &real_device_size);
-	if (device_geometry(sdp)) {
-		perror(_("Device geometry error"));
-		exit(EXIT_FAILURE);
-	}
 	/* Convert optional block-count to basic blocks */
 	if (sdp->orig_fssize) {
 		sdp->orig_fssize *= sdp->bsize;
@@ -612,18 +598,13 @@ void main_mkfs(int argc, char *argv[])
 			fprintf(stderr, _("%s: Specified block count is bigger "
 				"than the actual device.\n"), argv[0]);
 			die( _("Device Size is %.2f GB (%llu blocks)\n"),
-			       real_device_size / ((float)(1 << 30)),
-			       (unsigned long long)real_device_size / sdp->bsize);
+			       sdp->dinfo.size / ((float)(1 << 30)),
+			       (unsigned long long)sdp->dinfo.size / sdp->bsize);
 		}
 		sdp->device.length = sdp->orig_fssize;
 	}
-	if (fix_device_geometry(sdp)) {
-		fprintf(stderr, _("Device is too small (%llu bytes)\n"),
-			(unsigned long long)sdp->device.length << GFS2_BASIC_BLOCK_SHIFT);
-		exit(EXIT_FAILURE);
-	}
-
-	if (!S_ISREG(st_buf.st_mode) && discard)
+	fix_device_geometry(sdp);
+	if (!S_ISREG(sdp->dinfo.stat.st_mode) && discard)
 		discard_blocks(sdp);
 
 	/* Compute the resource group layouts */
@@ -698,5 +679,5 @@ void main_mkfs(int argc, char *argv[])
 		exit(EXIT_FAILURE);
 	}
 
-	print_results(sdp, real_device_size, uuid);
+	print_results(sdp, sdp->dinfo.size, uuid);
 }


More information about the cluster-commits mailing list