From 8cc74f4f279cadaf40e7b084127ed6327497e3b2 Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Tue, 7 Jan 2014 13:00:45 +0000 Subject: [PATCH] Move libext2fs functions to separate files --- Makefile | 4 +- bmove.c | 12 +--- bmove.h | 25 ++++++++ check_uninit.c | 86 ++++++++++++++++++++++++++++ check_uninit.h | 22 +++++++ ext2fsP.h | 146 ----------------------------------------------- realloc-inodes.c | 87 +--------------------------- 7 files changed, 139 insertions(+), 243 deletions(-) create mode 100644 bmove.h create mode 100644 check_uninit.c create mode 100644 check_uninit.h delete mode 100644 ext2fsP.h diff --git a/Makefile b/Makefile index 927fa66..7e8057e 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ all: realloc-inodes e2patch -realloc-inodes: realloc-inodes.c bmove.c ext2fsP.h Makefile patch_io.c patch_io.h patch.c patch.h - gcc -g -Wsign-compare -Wall -o realloc-inodes -lcom_err -lext2fs realloc-inodes.c bmove.c patch_io.c patch.c +realloc-inodes: realloc-inodes.c bmove.c bmove.h check_uninit.c check_uninit.h Makefile patch_io.c patch_io.h patch.c patch.h + gcc -g -Wsign-compare -Wall -o realloc-inodes -lcom_err -lext2fs realloc-inodes.c bmove.c patch_io.c patch.c check_uninit.c e2patch: e2patch.c patch.c patch.h gcc -g -Wsign-compare -Wall -o e2patch -lcom_err -lext2fs e2patch.c patch.c diff --git a/bmove.c b/bmove.c index 2761c6a..adf75a5 100644 --- a/bmove.c +++ b/bmove.c @@ -12,18 +12,8 @@ #include #include -#if HAVE_UNISTD_H -#include -#endif -#if HAVE_SYS_TYPES_H -#include -#endif -#if HAVE_SYS_TIME_H -#include -#endif -#include -#include "ext2fsP.h" +#include "bmove.h" struct process_block_struct { ext2_ino_t ino; diff --git a/bmove.h b/bmove.h new file mode 100644 index 0000000..b1c8fe6 --- /dev/null +++ b/bmove.h @@ -0,0 +1,25 @@ +/* + * bmove.h --- header for bmove.c + * + * Copyright (C) 2014 Vitaliy Filippov + * + * %Begin-Header% + * This file may be redistributed under the terms of the GNU Library + * General Public License, version 2. + * %End-Header% + */ + +#ifndef E2_BMOVE_H +#define E2_BMOVE_H + +#include + +errcode_t ext2fs_move_blocks(ext2_filsys fs, + ext2fs_block_bitmap reserve, + ext2fs_block_bitmap alloc_map, + int flags); + +#define EXT2_BMOVE_GET_DBLIST 0x0001 +#define EXT2_BMOVE_DEBUG 0x0002 + +#endif diff --git a/check_uninit.c b/check_uninit.c new file mode 100644 index 0000000..699c183 --- /dev/null +++ b/check_uninit.c @@ -0,0 +1,86 @@ +/* + * check_uninit.c --- check_block_uninit() and check_inode_uninit() functions + * copy-pasted from e2fsprogs: lib/ext2fs/alloc.c + * + * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o. + * + * %Begin-Header% + * This file may be redistributed under the terms of the GNU Library + * General Public License, version 2. + * %End-Header% + */ + +#include "check_uninit.h" + +/* + * Check for uninit block bitmaps and deal with them appropriately + */ +void check_block_uninit(ext2_filsys fs, ext2fs_block_bitmap map, dgrp_t group) +{ + blk_t i; + blk64_t blk, super_blk, old_desc_blk, new_desc_blk; + int old_desc_blocks; + + if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super, + EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) || + !(ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT))) + return; + + blk = (group * fs->super->s_blocks_per_group) + + fs->super->s_first_data_block; + + ext2fs_super_and_bgd_loc2(fs, group, &super_blk, + &old_desc_blk, &new_desc_blk, 0); + + if (fs->super->s_feature_incompat & + EXT2_FEATURE_INCOMPAT_META_BG) + old_desc_blocks = fs->super->s_first_meta_bg; + else + old_desc_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks; + + for (i=0; i < fs->super->s_blocks_per_group; i++, blk++) + ext2fs_fast_unmark_block_bitmap2(map, blk); + + blk = (group * fs->super->s_blocks_per_group) + + fs->super->s_first_data_block; + for (i=0; i < fs->super->s_blocks_per_group; i++, blk++) { + if ((blk == super_blk) || + (old_desc_blk && old_desc_blocks && + (blk >= old_desc_blk) && + (blk < old_desc_blk + old_desc_blocks)) || + (new_desc_blk && (blk == new_desc_blk)) || + (blk == ext2fs_block_bitmap_loc(fs, group)) || + (blk == ext2fs_inode_bitmap_loc(fs, group)) || + (blk >= ext2fs_inode_table_loc(fs, group) && + (blk < ext2fs_inode_table_loc(fs, group) + + fs->inode_blocks_per_group))) + ext2fs_fast_mark_block_bitmap2(map, blk); + } + ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT); + ext2fs_group_desc_csum_set(fs, group); + ext2fs_mark_super_dirty(fs); + ext2fs_mark_bb_dirty(fs); +} + +/* + * Check for uninit inode bitmaps and deal with them appropriately + */ +void check_inode_uninit(ext2_filsys fs, ext2fs_inode_bitmap map, dgrp_t group) +{ + ext2_ino_t i, ino; + + if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super, + EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) || + !(ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT))) + return; + + ino = (group * fs->super->s_inodes_per_group) + 1; + for (i=0; i < fs->super->s_inodes_per_group; i++, ino++) + ext2fs_fast_unmark_inode_bitmap2(map, ino); + + ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT); + ext2fs_group_desc_csum_set(fs, group); + ext2fs_mark_ib_dirty(fs); + ext2fs_mark_super_dirty(fs); + check_block_uninit(fs, fs->block_map, group); +} diff --git a/check_uninit.h b/check_uninit.h new file mode 100644 index 0000000..9a7582a --- /dev/null +++ b/check_uninit.h @@ -0,0 +1,22 @@ +/* + * check_uninit.h --- check_block_uninit() and check_inode_uninit() functions + * copy-pasted from e2fsprogs: lib/ext2fs/alloc.c + * These functions are static in libext2, but we need them. + * + * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o. + * + * %Begin-Header% + * This file may be redistributed under the terms of the GNU Library + * General Public License, version 2. + * %End-Header% + */ + +#ifndef E2_CHECK_UNINIT_H +#define E2_CHECK_UNINIT_H + +#include + +void check_block_uninit(ext2_filsys fs, ext2fs_block_bitmap map, dgrp_t group); +void check_inode_uninit(ext2_filsys fs, ext2fs_inode_bitmap map, dgrp_t group); + +#endif diff --git a/ext2fsP.h b/ext2fsP.h deleted file mode 100644 index 96f01af..0000000 --- a/ext2fsP.h +++ /dev/null @@ -1,146 +0,0 @@ -/* - * ext2fsP.h --- private header file for ext2 library - * - * Copyright (C) 1997 Theodore Ts'o. - * - * %Begin-Header% - * This file may be redistributed under the terms of the GNU Library - * General Public License, version 2. - * %End-Header% - */ - -#include - -#define EXT2FS_MAX_NESTED_LINKS 8 - -/* - * Badblocks list - */ -struct ext2_struct_u32_list { - int magic; - int num; - int size; - __u32 *list; - int badblocks_flags; -}; - -struct ext2_struct_u32_iterate { - int magic; - ext2_u32_list bb; - int ptr; -}; - - -/* - * Directory block iterator definition - */ -struct ext2_struct_dblist { - int magic; - ext2_filsys fs; - unsigned long long size; - unsigned long long count; - int sorted; - struct ext2_db_entry2 * list; -}; - -/* - * For directory iterators - */ -struct dir_context { - ext2_ino_t dir; - int flags; - char *buf; - int (*func)(ext2_ino_t dir, - int entry, - struct ext2_dir_entry *dirent, - int offset, - int blocksize, - char *buf, - void *priv_data); - void *priv_data; - errcode_t errcode; -}; - -/* - * Inode cache structure - */ -struct ext2_inode_cache { - void * buffer; - blk_t buffer_blk; - int cache_last; - int cache_size; - int refcount; - struct ext2_inode_cache_ent *cache; -}; - -struct ext2_inode_cache_ent { - ext2_ino_t ino; - struct ext2_inode inode; -}; - -/* Function prototypes */ - -extern int ext2fs_process_dir_block(ext2_filsys fs, - blk64_t *blocknr, - e2_blkcnt_t blockcnt, - blk64_t ref_block, - int ref_offset, - void *priv_data); - -/* Generic numeric progress meter */ - -struct ext2fs_numeric_progress_struct { - __u64 max; - int log_max; - int skip_progress; -}; - -extern void ext2fs_numeric_progress_init(ext2_filsys fs, - struct ext2fs_numeric_progress_struct * progress, - const char *label, __u64 max); -extern void ext2fs_numeric_progress_update(ext2_filsys fs, - struct ext2fs_numeric_progress_struct * progress, - __u64 val); -extern void ext2fs_numeric_progress_close(ext2_filsys fs, - struct ext2fs_numeric_progress_struct * progress, - const char *message); - -/* - * 64-bit bitmap support - */ - -extern errcode_t ext2fs_alloc_generic_bmap(ext2_filsys fs, errcode_t magic, - int type, __u64 start, __u64 end, - __u64 real_end, - const char * description, - ext2fs_generic_bitmap *bmap); - -extern void ext2fs_free_generic_bmap(ext2fs_generic_bitmap bmap); - -extern errcode_t ext2fs_copy_generic_bmap(ext2fs_generic_bitmap src, - ext2fs_generic_bitmap *dest); - -extern errcode_t ext2fs_resize_generic_bmap(ext2fs_generic_bitmap bmap, - __u64 new_end, - __u64 new_real_end); -extern errcode_t ext2fs_fudge_generic_bmap_end(ext2fs_generic_bitmap bitmap, - errcode_t neq, - __u64 end, __u64 *oend); -extern int ext2fs_mark_generic_bmap(ext2fs_generic_bitmap bitmap, - __u64 arg); -extern int ext2fs_unmark_generic_bmap(ext2fs_generic_bitmap bitmap, - __u64 arg); -extern int ext2fs_test_generic_bmap(ext2fs_generic_bitmap bitmap, - __u64 arg); -extern errcode_t ext2fs_set_generic_bmap_range(ext2fs_generic_bitmap bitmap, - __u64 start, unsigned int num, - void *in); -extern errcode_t ext2fs_get_generic_bmap_range(ext2fs_generic_bitmap bitmap, - __u64 start, unsigned int num, - void *out); -extern void ext2fs_warn_bitmap32(ext2fs_generic_bitmap bitmap,const char *func); - -extern int ext2fs_mem_is_zero(const char *mem, size_t len); - -#define EXT2_BMOVE_GET_DBLIST 0x0001 -#define EXT2_BMOVE_DEBUG 0x0002 diff --git a/realloc-inodes.c b/realloc-inodes.c index f7354b2..c487546 100644 --- a/realloc-inodes.c +++ b/realloc-inodes.c @@ -53,16 +53,13 @@ #include #include +#include "bmove.h" +#include "check_uninit.h" #include "patch_io.h" #define _(a) (a) #define min(a,b) ((a)<(b)?(a):(b)) -errcode_t ext2fs_move_blocks(ext2_filsys fs, - ext2fs_block_bitmap reserve, - ext2fs_block_bitmap alloc_map, - int flags); - // "local data" for the inode reallocation process typedef struct { @@ -140,83 +137,6 @@ ext2_ino_t realloc_search_inode_map(realloc_data *rd, ext2_ino_t old) return 0; } -// Copy-paste from e2fsprogs: lib/ext2fs/alloc.c - -/* - * Check for uninit block bitmaps and deal with them appropriately - */ -static void check_block_uninit(ext2_filsys fs, ext2fs_block_bitmap map, - dgrp_t group) -{ - blk_t i; - blk64_t blk, super_blk, old_desc_blk, new_desc_blk; - int old_desc_blocks; - - if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super, - EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) || - !(ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT))) - return; - - blk = (group * fs->super->s_blocks_per_group) + - fs->super->s_first_data_block; - - ext2fs_super_and_bgd_loc2(fs, group, &super_blk, - &old_desc_blk, &new_desc_blk, 0); - - if (fs->super->s_feature_incompat & - EXT2_FEATURE_INCOMPAT_META_BG) - old_desc_blocks = fs->super->s_first_meta_bg; - else - old_desc_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks; - - for (i=0; i < fs->super->s_blocks_per_group; i++, blk++) - ext2fs_fast_unmark_block_bitmap2(map, blk); - - blk = (group * fs->super->s_blocks_per_group) + - fs->super->s_first_data_block; - for (i=0; i < fs->super->s_blocks_per_group; i++, blk++) { - if ((blk == super_blk) || - (old_desc_blk && old_desc_blocks && - (blk >= old_desc_blk) && - (blk < old_desc_blk + old_desc_blocks)) || - (new_desc_blk && (blk == new_desc_blk)) || - (blk == ext2fs_block_bitmap_loc(fs, group)) || - (blk == ext2fs_inode_bitmap_loc(fs, group)) || - (blk >= ext2fs_inode_table_loc(fs, group) && - (blk < ext2fs_inode_table_loc(fs, group) - + fs->inode_blocks_per_group))) - ext2fs_fast_mark_block_bitmap2(map, blk); - } - ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT); - ext2fs_group_desc_csum_set(fs, group); - ext2fs_mark_super_dirty(fs); - ext2fs_mark_bb_dirty(fs); -} - -/* - * Check for uninit inode bitmaps and deal with them appropriately - */ -static void check_inode_uninit(ext2_filsys fs, ext2fs_inode_bitmap map, - dgrp_t group) -{ - ext2_ino_t i, ino; - - if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super, - EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) || - !(ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT))) - return; - - ino = (group * fs->super->s_inodes_per_group) + 1; - for (i=0; i < fs->super->s_inodes_per_group; i++, ino++) - ext2fs_fast_unmark_inode_bitmap2(map, ino); - - ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT); - ext2fs_group_desc_csum_set(fs, group); - ext2fs_mark_ib_dirty(fs); - ext2fs_mark_super_dirty(fs); - check_block_uninit(fs, fs->block_map, group); -} - /** * Move inodes from the end of each block group inode table * so the tables can be shrinked @@ -366,8 +286,7 @@ static int change_inode_numbers_callback(ext2_ino_t dir, int entry, { new_ino = dirent->inode; } - new_ino = 1 + (new_ino-1)/rd->ig_old*rd->ig_new + - (new_ino-1)%rd->ig_old; + new_ino = 1 + (new_ino-1)/rd->ig_old*rd->ig_new + (new_ino-1)%rd->ig_old; if (new_ino != dirent->inode) { dirent->inode = new_ino;