libgfs2: Clean up device geometry code
From d427382d1a9fe110357e886d99d02f639593641d Mon Sep 17 00:00:00 2001
From: Steven Whitehouse <[email protected]>
Date: Thu, 1 Dec 2011 11:49:10 +0000
Subject: [PATCH] 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).
gfs2/edit/hexedit.c +5 -6
@@ -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 );
gfs2/edit/savemeta.c +3 -7
@@ -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 ;
gfs2/fsck/initialize.c +7 -3
@@ -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 ;
gfs2/fsck/rgrepair.c +1 -9
@@ -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;
gfs2/libgfs2/Makefile.am +1 -1
@@ -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
gfs2/libgfs2/device_geometry.c +64 -85
181 lines not on this page · raw message
gfs2/libgfs2/fs_geometry.c +1 -4
@@ -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);
gfs2/libgfs2/libgfs2.h +18 -22
@@ -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 */
gfs2/libgfs2/size.c deleted +0 -69
69 lines not on this page · raw message
gfs2/mkfs/main_grow.c +3 -8
@@ -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 );
gfs2/mkfs/main_mkfs.c +17 -36
109 lines not on this page · raw message
Signature · 1 line
1.7.4.4
----- Original Message -----
| >From d427382d1a9fe110357e886d99d02f639593641d Mon Sep 17 00:00:00 | >2001 | From: Steven Whitehouse <[email protected]> | Date: Thu, 1 Dec 2011 11:49:10 +0000 | Subject: [PATCH] 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 <[email protected]>
Hi,
ACK,
Regards,
Bob Peterson
Red Hat File Systems