[syslinux:pathbased] ext2: clean up types in the bmap functions
syslinux-bot for H. Peter Anvin
hpa at zytor.com
Tue Feb 16 09:27:19 PST 2010
Commit-ID: 5bce60d1b4222fb36ab786fc89e1fa98a9edf7e1
Gitweb: http://syslinux.zytor.com/commit/5bce60d1b4222fb36ab786fc89e1fa98a9edf7e1
Author: H. Peter Anvin <hpa at zytor.com>
AuthorDate: Tue, 16 Feb 2010 09:24:21 -0800
Committer: H. Peter Anvin <hpa at zytor.com>
CommitDate: Tue, 16 Feb 2010 09:24:21 -0800
ext2: clean up types in the bmap functions
No need to take an explicit fs pointer when we're passing an inode
anyway...
Signed-off-by: H. Peter Anvin <hpa at zytor.com>
---
core/fs/ext2/bmap.c | 88 +++++++++++++++++------------------
core/fs/ext2/ext2.c | 119 ++++++++++++++++++++++++------------------------
core/fs/ext2/ext2_fs.h | 40 ++++++++--------
3 files changed, 123 insertions(+), 124 deletions(-)
diff --git a/core/fs/ext2/bmap.c b/core/fs/ext2/bmap.c
index b6dce4b..d943202 100644
--- a/core/fs/ext2/bmap.c
+++ b/core/fs/ext2/bmap.c
@@ -6,13 +6,13 @@
*/
#include <stdio.h>
+#include <dprintf.h>
#include <fs.h>
#include <disk.h>
#include <cache.h>
#include "ext2_fs.h"
-
-static struct ext4_extent_header *
+static struct ext4_extent_header *
ext4_find_leaf(struct fs_info *fs, struct ext4_extent_header *eh,
block_t block)
{
@@ -20,44 +20,44 @@ ext4_find_leaf(struct fs_info *fs, struct ext4_extent_header *eh,
struct cache_struct *cs;
block_t blk;
int i;
-
- while (1) {
+
+ while (1) {
if (eh->eh_magic != EXT4_EXT_MAGIC)
return NULL;
- if (eh->eh_depth == 0)
+ if (eh->eh_depth == 0)
return eh;
-
- index = EXT4_FIRST_INDEX(eh);
+
+ index = EXT4_FIRST_INDEX(eh);
for (i = 0; i < (int)eh->eh_entries; i++) {
if (block < index[i].ei_block)
break;
}
if (--i < 0)
return NULL;
-
+
blk = index[i].ei_leaf_hi;
- blk = (blk << 32) + index[i].ei_leaf_lo;
+ blk = (blk << 32) + index[i].ei_leaf_lo;
cs = get_cache_block(fs->fs_dev, blk);
eh = (struct ext4_extent_header *)cs->data;
}
}
/* handle the ext4 extents to get the phsical block number */
-static block_t bmap_extent(struct fs_info *fs, struct inode *inode,
- block_t block)
+static block_t bmap_extent(struct inode *inode, block_t block)
{
+ struct fs_info *fs = inode->fs;
struct ext4_extent_header *leaf;
struct ext4_extent *ext;
int i;
block_t start;
-
+
leaf = ext4_find_leaf(fs, (struct ext4_extent_header *)inode->pvt, block);
if (!leaf) {
printf("ERROR, extent leaf not found\n");
return 0;
}
- ext = EXT4_FIRST_EXTENT(leaf);
+ ext = EXT4_FIRST_EXTENT(leaf);
for (i = 0; i < leaf->eh_entries; i++) {
if (block < ext[i].ee_block)
break;
@@ -66,93 +66,91 @@ static block_t bmap_extent(struct fs_info *fs, struct inode *inode,
printf("ERROR, not find the right block\n");
return 0;
}
-
+
/* got it */
block -= ext[i].ee_block;
if (block >= ext[i].ee_len)
return 0;
start = ext[i].ee_start_hi;
start = (start << 32) + ext[i].ee_start_lo;
-
+
return start + block;
}
-/*
- * handle the traditional block map, like indirect, double indirect
- * and triple indirect
+/*
+ * handle the traditional block map, like indirect, double indirect
+ * and triple indirect
*/
-static block_t bmap_traditional(struct fs_info *fs, struct inode *inode,
- block_t block)
+static block_t bmap_traditional(struct inode *inode, block_t block)
{
+ struct fs_info *fs = inode->fs;
int addr_per_block = BLOCK_SIZE(fs) >> 2;
- block_t direct_blocks = EXT2_NDIR_BLOCKS,
+ uint32_t direct_blocks = EXT2_NDIR_BLOCKS,
indirect_blocks = addr_per_block,
double_blocks = addr_per_block * addr_per_block,
triple_blocks = double_blocks * addr_per_block;
struct cache_struct *cs;
-
+
/* direct blocks */
if (block < direct_blocks)
return ((uint32_t *)inode->pvt)[block];
-
+
/* indirect blocks */
block -= direct_blocks;
if (block < indirect_blocks) {
block_t ind_block = ((uint32_t *)inode->pvt)[EXT2_IND_BLOCK];
-
+
if (!ind_block)
return 0;
cs = get_cache_block(fs->fs_dev, ind_block);
-
+
return ((uint32_t *)cs->data)[block];
}
-
-
+
+
/* double indirect blocks */
block -= indirect_blocks;
if (block < double_blocks) {
block_t dou_block = ((uint32_t *)inode->pvt)[EXT2_DIND_BLOCK];
-
+
if (!dou_block)
- return 0;
+ return 0;
cs = get_cache_block(fs->fs_dev, dou_block);
-
+
dou_block = ((uint32_t *)cs->data)[block / indirect_blocks];
if (!dou_block)
return 0;
cs = get_cache_block(fs->fs_dev, dou_block);
-
+
return ((uint32_t *)cs->data)[block % addr_per_block];
}
-
+
/* triple indirect block */
block -= double_blocks;
if (block < triple_blocks) {
block_t tri_block = ((uint32_t *)inode->pvt)[EXT2_TIND_BLOCK];
-
+
if (!tri_block)
return 0;
cs = get_cache_block(fs->fs_dev, tri_block);
-
+
tri_block = ((uint32_t *)cs->data)[block / double_blocks];
if (!tri_block)
return 0;
cs = get_cache_block(fs->fs_dev, tri_block);
-
+
tri_block = (block / addr_per_block) % addr_per_block;
tri_block = ((uint32_t *)cs->data)[tri_block];
if (!tri_block)
return 0;
cs = get_cache_block(fs->fs_dev, tri_block);
-
+
return ((uint32_t *)cs->data)[block % addr_per_block];
}
-
-
- /* File too big, can not handle */
- printf("ERROR, file too big\n");
+
+ /* This can't happen... */
return 0;
}
@@ -168,14 +166,14 @@ static block_t bmap_traditional(struct fs_info *fs, struct inode *inode,
* @retrun: the physic block number.
*
*/
-block_t ext2_bmap(struct fs_info *fs, struct inode * inode, block_t block)
+block_t ext2_bmap(struct inode *inode, block_t block)
{
block_t ret;
-
+
if (inode->flags & EXT4_EXTENTS_FLAG)
- ret = bmap_extent(fs, inode, block);
+ ret = bmap_extent(inode, block);
else
- ret = bmap_traditional(fs, inode, block);
-
+ ret = bmap_traditional(inode, block);
+
return ret;
}
diff --git a/core/fs/ext2/ext2.c b/core/fs/ext2/ext2.c
index 1a81dd5..7e19cf3 100644
--- a/core/fs/ext2/ext2.c
+++ b/core/fs/ext2/ext2.c
@@ -1,3 +1,4 @@
+#include <dprintf.h>
#include <stdio.h>
#include <string.h>
#include <sys/dirent.h>
@@ -24,13 +25,13 @@ struct ext2_group_desc * ext2_get_group_desc(struct fs_info *fs,
"block_group >= groups_count - "
"block_group = %d, groups_count = %d",
group_num, sbi->s_groups_count);
-
+
return NULL;
- }
-
+ }
+
desc_block = group_num / sbi->s_desc_per_block;
desc_index = group_num % sbi->s_desc_per_block;
-
+
desc_block += sbi->s_first_data_block + 1;
cs = get_cache_block(fs->fs_dev, desc_block);
@@ -46,19 +47,19 @@ struct ext2_group_desc * ext2_get_group_desc(struct fs_info *fs,
*
* Convert a linear sector index in a file to linear sector number
*
- * well, alought this function converts a linear sector number to
+ * well, alought this function converts a linear sector number to
* physic sector number, it uses block cache in the implemention.
- *
+ *
* @param: lin_sector, the lineral sector index
- *
+ *
* @return: physic sector number
*/
static sector_t linsector(struct inode *inode, uint32_t lin_sector)
{
struct fs_info *fs = inode->fs;
int blk_bits = fs->block_shift - fs->sector_shift;
- block_t block = ext2_bmap(fs, inode, lin_sector >> blk_bits);
-
+ block_t block = ext2_bmap(inode, lin_sector >> blk_bits);
+
return (block << blk_bits) + (lin_sector & ((1 << blk_bits) - 1));
}
@@ -71,30 +72,30 @@ static sector_t linsector(struct inode *inode, uint32_t lin_sector)
* n ext2 block pointer, i.e. anything *except the superblock
*
*/
-static void getlinsec_ext(struct fs_info *fs, char *buf,
+static void getlinsec_ext(struct fs_info *fs, char *buf,
sector_t sector, int sector_cnt)
{
int ext_cnt = 0;
int sec_per_block = 1 << (fs->block_shift - fs->sector_shift);
struct disk *disk = fs->fs_dev->disk;
-
+
if (sector < sec_per_block) {
ext_cnt = sec_per_block - sector;
memset(buf, 0, ext_cnt << fs->sector_shift);
buf += ext_cnt << fs->sector_shift;
}
-
+
sector += ext_cnt;
sector_cnt -= ext_cnt;
disk->rdwr_sectors(disk, buf, sector, sector_cnt, 0);
}
/*
- * Get multiple sectors from a file
+ * Get multiple sectors from a file
*
- * Alought we have made the buffer data based on block size,
- * we use sector for implemention; because reading multiple
- * sectors (then can be multiple blocks) is what the function
+ * Alought we have made the buffer data based on block size,
+ * we use sector for implemention; because reading multiple
+ * sectors (then can be multiple blocks) is what the function
* do. So, let it be based on sectors.
*
*/
@@ -105,59 +106,59 @@ static uint32_t ext2_getfssec(struct file *file, char *buf,
struct fs_info *fs = file->fs;
int sector_left, next_sector, sector_idx;
int frag_start, con_sec_cnt;
- int bytes_read = sectors << fs->sector_shift;
+ int bytes_read = sectors << fs->sector_shift;
uint32_t bytesleft = inode->size - file->offset;
-
+
sector_left = (bytesleft + SECTOR_SIZE(fs) - 1) >> fs->sector_shift;
if (sectors > sector_left)
sectors = sector_left;
-
+
sector_idx = file->offset >> fs->sector_shift;
while (sectors) {
/*
* get the frament
*/
next_sector = frag_start = linsector(inode, sector_idx);
- con_sec_cnt = 0;
-
+ con_sec_cnt = 0;
+
/* get the consective sectors count */
- do {
+ do {
con_sec_cnt ++;
sectors --;
if (sectors <= 0)
break;
-
+
/* if sectors >= the sectors left in the 64K block, break and read */
if (sectors >= (((~(uint32_t)buf&0xffff)|((uint32_t)buf&0xffff0000)) + 1))
break;
-
+
sector_idx++;
next_sector++;
- } while (next_sector == linsector(inode, sector_idx));
-
-#if 0
- printf("You are reading data stored at sector --0x%x--0x%x\n",
+ } while (next_sector == linsector(inode, sector_idx));
+
+#if 0
+ printf("You are reading data stored at sector --0x%x--0x%x\n",
frag_start, frag_start + con_sec_cnt -1);
-#endif
+#endif
getlinsec_ext(fs, buf, frag_start, con_sec_cnt);
buf += con_sec_cnt << fs->sector_shift;
} while(sectors);
-
- if (bytes_read >= bytesleft) {
+
+ if (bytes_read >= bytesleft) {
bytes_read = bytesleft;
*have_more = 0;
} else {
*have_more = 1;
- }
+ }
file->offset += bytes_read;
-
+
return bytes_read;
}
-
+
/*
* Unlike strncmp, ext2_match_entry returns 1 for success, 0 for failure.
*/
-static inline bool ext2_match_entry (const char *name, size_t len,
+static inline bool ext2_match_entry(const char *name, size_t len,
struct ext2_dir_entry * de)
{
if (!de->d_inode)
@@ -179,7 +180,7 @@ static inline struct ext2_dir_entry *ext2_next_entry(struct ext2_dir_entry *p)
/*
* find a dir entry, return it if found, or return NULL.
*/
-static struct ext2_dir_entry *
+static struct ext2_dir_entry *
ext2_find_entry(struct fs_info *fs, struct inode *inode, const char *dname)
{
block_t index = 0;
@@ -190,7 +191,7 @@ ext2_find_entry(struct fs_info *fs, struct inode *inode, const char *dname)
size_t dname_len = strlen(dname);
while (i < inode->size) {
- if (!(block = ext2_bmap(fs, inode, index++)))
+ if (!(block = ext2_bmap(inode, index++)))
return NULL;
cs = get_cache_block(fs->fs_dev, block);
offset = 0;
@@ -219,18 +220,18 @@ static struct ext2_inode *ext2_get_inode(struct fs_info *fs, int inr)
struct cache_struct *cs;
uint32_t inode_group, inode_offset;
uint32_t block_num, block_off;
-
+
inr--;
inode_group = inr / EXT2_INODES_PER_GROUP(fs);
inode_offset = inr % EXT2_INODES_PER_GROUP(fs);
desc = ext2_get_group_desc(fs, inode_group);
if (!desc)
return NULL;
-
- block_num = desc->bg_inode_table +
+
+ block_num = desc->bg_inode_table +
inode_offset / EXT2_INODES_PER_BLOCK(fs);
block_off = inode_offset % EXT2_INODES_PER_BLOCK(fs);
-
+
cs = get_cache_block(fs->fs_dev, block_num);
return cs->data + block_off * EXT2_SB(fs)->s_inode_size;
@@ -244,7 +245,7 @@ static inline int get_inode_mode(int mode)
else if (mode == T_IFLNK)
mode = I_SYMLINK;
else
- mode = I_FILE; /* we treat others as FILE */
+ mode = I_FILE; /* we treat others as FILE */
return mode;
}
@@ -266,12 +267,12 @@ static struct inode *ext2_iget_by_inr(struct fs_info *fs, uint32_t inr)
{
struct ext2_inode *e_inode;
struct inode *inode;
-
+
e_inode = ext2_get_inode(fs, inr);
if (!(inode = alloc_inode(fs, inr, EXT2_N_BLOCKS*sizeof(uint32_t *))))
return NULL;
fill_inode(inode, e_inode);
-
+
return inode;
}
@@ -288,7 +289,7 @@ static struct inode *ext2_iget(char *dname, struct inode *parent)
de = ext2_find_entry(fs, parent, dname);
if (!de)
return NULL;
-
+
return ext2_iget_by_inr(fs, de->d_inode);
}
@@ -300,7 +301,7 @@ int ext2_readlink(struct inode *inode, char *buf)
bool fast_symlink;
struct cache_struct *cs;
size_t bytes = inode->size;
-
+
if (inode->size > BLOCK_SIZE(fs))
return -1; /* Error! */
@@ -316,7 +317,7 @@ int ext2_readlink(struct inode *inode, char *buf)
}
/*
- * Read one directory entry at a time
+ * Read one directory entry at a time
*/
static struct dirent * ext2_readdir(struct file *file)
{
@@ -327,12 +328,12 @@ static struct dirent * ext2_readdir(struct file *file)
struct cache_struct *cs;
block_t index = file->offset >> fs->block_shift;
block_t block;
-
- if (!(block = ext2_bmap(fs, inode, index)))
- return NULL;
+
+ if (!(block = ext2_bmap(inode, index)))
+ return NULL;
cs = get_cache_block(fs->fs_dev, block);
de = (struct ext2_dir_entry *)(cs->data + (file->offset & (BLOCK_SIZE(fs) - 1)));
-
+
if (!(dirent = malloc(sizeof(*dirent)))) {
malloc_error("dirent structure in ext2_readdir");
return NULL;
@@ -343,9 +344,9 @@ static struct dirent * ext2_readdir(struct file *file)
dirent->d_type = de->d_file_type;
memcpy(dirent->d_name, de->d_name, de->d_name_len);
dirent->d_name[de->d_name_len] = '\0';
-
+
file->offset += de->d_rec_len; /* Update for next reading */
-
+
return dirent;
}
@@ -357,10 +358,10 @@ static int ext2_fs_init(struct fs_info *fs)
struct disk *disk = fs->fs_dev->disk;
struct ext2_sb_info *sbi;
struct ext2_super_block sb;
-
+
/* read the super block */
disk->rdwr_sectors(disk, &sb, 2, 2, 0);
-
+
/* check if it is ext2, since we also support btrfs now */
if (sb.s_magic != EXT2_SUPER_MAGIC)
return -1;
@@ -371,7 +372,7 @@ static int ext2_fs_init(struct fs_info *fs)
return -1;
}
fs->fs_info = sbi;
-
+
if (sb.s_magic != EXT2_SUPER_MAGIC) {
printf("ext2 mount error: it's not a EXT2/3/4 file system!\n");
return 0;
@@ -381,15 +382,15 @@ static int ext2_fs_init(struct fs_info *fs)
fs->block_shift = sb.s_log_block_size + 10;
fs->sector_size = 1 << fs->sector_shift;
fs->block_size = 1 << fs->block_shift;
-
+
sbi->s_inodes_per_group = sb.s_inodes_per_group;
sbi->s_blocks_per_group = sb.s_blocks_per_group;
sbi->s_inodes_per_block = BLOCK_SIZE(fs) / sb.s_inode_size;
if (sb.s_desc_size < sizeof(struct ext2_group_desc))
sb.s_desc_size = sizeof(struct ext2_group_desc);
sbi->s_desc_per_block = BLOCK_SIZE(fs) / sb.s_desc_size;
- sbi->s_groups_count = (sb.s_blocks_count - sb.s_first_data_block
- + EXT2_BLOCKS_PER_GROUP(fs) - 1)
+ sbi->s_groups_count = (sb.s_blocks_count - sb.s_first_data_block
+ + EXT2_BLOCKS_PER_GROUP(fs) - 1)
/ EXT2_BLOCKS_PER_GROUP(fs);
sbi->s_first_data_block = sb.s_first_data_block;
sbi->s_inode_size = sb.s_inode_size;
diff --git a/core/fs/ext2/ext2_fs.h b/core/fs/ext2/ext2_fs.h
index 2f5bc6e..82f508f 100644
--- a/core/fs/ext2/ext2_fs.h
+++ b/core/fs/ext2/ext2_fs.h
@@ -41,11 +41,11 @@
*/
#define S_IFDIR 0040000 // Directory
#define S_IFCHR 0020000 // Character device
-#define S_IFBLK 0060000 // Block device
+#define S_IFBLK 0060000 // Block device
#define S_IFREG 0100000 // Regular file
#define S_IFIFO 0010000 // FIFO
-#define S_IFLNK 0120000 // Symbolic link
-#define S_IFSOCK 0140000 // Socket
+#define S_IFLNK 0120000 // Symbolic link
+#define S_IFSOCK 0140000 // Socket
#define S_IFSHIFT 12
@@ -64,7 +64,7 @@
/*
* super block structure:
- * include/linux/ext2_fs.h
+ * include/linux/ext2_fs.h
*/
struct ext2_super_block {
uint32_t s_inodes_count; /* Inodes count */
@@ -92,16 +92,16 @@ struct ext2_super_block {
uint32_t s_rev_level; /* Revision level */
uint16_t s_def_resuid; /* Default uid for reserved blocks */
uint16_t s_def_resgid; /* Default gid for reserved blocks */
-
- uint32_t s_first_ino; /* First non-reserved inode */
- uint16_t s_inode_size; /* size of inode structure */
- uint16_t s_block_group_nr; /* block group # of this superblock */
- uint32_t s_feature_compat; /* compatible feature set */
- uint32_t s_feature_incompat; /* incompatible feature set */
- uint32_t s_feature_ro_compat; /* readonly-compatible feature set */
+
+ uint32_t s_first_ino; /* First non-reserved inode */
+ uint16_t s_inode_size; /* size of inode structure */
+ uint16_t s_block_group_nr; /* block group # of this superblock */
+ uint32_t s_feature_compat; /* compatible feature set */
+ uint32_t s_feature_incompat; /* incompatible feature set */
+ uint32_t s_feature_ro_compat; /* readonly-compatible feature set */
uint8_t s_uuid[16]; /* 128-bit uuid for volume */
- char s_volume_name[16]; /* volume name */
- char s_last_mounted[64]; /* directory where last mounted */
+ char s_volume_name[16]; /* volume name */
+ char s_last_mounted[64]; /* directory where last mounted */
uint32_t s_algorithm_usage_bitmap; /* For compression */
uint8_t s_prealloc_blocks; /* Nr of blocks to try to preallocate*/
uint8_t s_prealloc_dir_blocks;
@@ -126,7 +126,7 @@ struct ext2_super_block {
uint32_t s_r_blocks_count_hi; /* Reserved blocks count */
uint32_t s_free_blocks_count_hi;/* Free blocks count */
uint16_t s_min_extra_isize; /* All inodes have at least # bytes */
- uint16_t s_want_extra_isize; /* New inodes should reserve # bytes */
+ uint16_t s_want_extra_isize; /* New inodes should reserve # bytes */
uint32_t s_flags; /* Miscellaneous flags */
uint16_t s_raid_stride; /* RAID stride */
uint16_t s_mmp_interval; /* # seconds to wait in MMP checking */
@@ -146,7 +146,7 @@ struct ext2_super_block {
#endif
*******************************************************************************/
-/*
+/*
* ext2 group desc structure:
*/
struct ext2_group_desc {
@@ -216,7 +216,7 @@ struct ext2_dir_entry {
/*******************************************************************************
#define EXT2_DIR_PAD 4
-#define EXT2_DIR_ROUND (EXT2_DIR_PAD - 1)
+#define EXT2_DIR_ROUND (EXT2_DIR_PAD - 1)
#define EXT2_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT2_DIR_ROUND) & \
~EXT2_DIR_ROUND)
*******************************************************************************/
@@ -267,7 +267,7 @@ struct ext4_extent_header {
/*
- * The ext2 super block information in memory
+ * The ext2 super block information in memory
*/
struct ext2_sb_info {
uint32_t s_inodes_per_block;/* Number of inodes per block */
@@ -290,9 +290,9 @@ static inline struct ext2_sb_info *EXT2_SB(struct fs_info *fs)
#define EXT2_DESC_PER_BLOCK(fs) (EXT2_SB(fs)->s_desc_per_block)
-/*
- * functions
+/*
+ * functions
*/
-block_t ext2_bmap(struct fs_info *, struct inode *, block_t);
+block_t ext2_bmap(struct inode *, block_t);
#endif /* ext2_fs.h */
More information about the Syslinux-commits
mailing list