OSDN Git Service

libext2fs: 32-bit bitmap refactorization, part 1
authorTheodore Ts'o <tytso@mit.edu>
Mon, 23 Jul 2007 02:59:50 +0000 (22:59 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Mon, 23 Jul 2007 02:59:50 +0000 (22:59 -0400)
Move the 32-bit specific bitmap code into gen_bitmap.c, and the
high-level interfaces into bitmaps.c.  Eventually we'll move the
new-style bitmap code into gen_bitmap64.c, but first we need to
isolate the code with knowledge of the bitmap internals in one place
first.

In this patch we move allocation, free, copy, clear, set_padding, and
fudge_end function into gen_bitmap.c, and make sure that the bitmaps.c
and bitops.c no longer have any knowledge of the bitmap internals.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
lib/ext2fs/Makefile.in
lib/ext2fs/bitmaps.c
lib/ext2fs/bitops.c
lib/ext2fs/ext2fs.h
lib/ext2fs/freefs.c
lib/ext2fs/gen_bitmap.c

index 199164a..db6820b 100644 (file)
@@ -187,12 +187,12 @@ ext2fs.pc: $(srcdir)/ext2fs.pc.in $(top_builddir)/config.status
        @echo " CONFIG.STATUS $@"
        @cd $(top_builddir); CONFIG_FILES=lib/ext2fs/ext2fs.pc ./config.status
 
-tst_badblocks: tst_badblocks.o freefs.o \
+tst_badblocks: tst_badblocks.o freefs.o bitmaps.o rw_bitmaps.o \
                read_bb_file.o write_bb_file.o badblocks.o 
        @echo " LD $@"
        @$(CC) -o tst_badblocks tst_badblocks.o freefs.o \
-               read_bb_file.o write_bb_file.o badblocks.o \
-               inline.o bitops.o gen_bitmap.o $(LIBCOM_ERR)
+               read_bb_file.o write_bb_file.o badblocks.o rw_bitmaps.o \
+               inline.o bitops.o gen_bitmap.o bitmaps.o $(LIBCOM_ERR)
 
 tst_icount: icount.c initialize.o  $(STATIC_LIBEXT2FS)
        @echo " LD $@"
index 696baad..e6c2849 100644 (file)
 #include "ext2_fs.h"
 #include "ext2fs.h"
 
-static errcode_t make_bitmap(__u32 start, __u32 end, __u32 real_end,
-                            const char *descr, char *init_map,
-                            ext2fs_generic_bitmap *ret)
+void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap)
 {
-       ext2fs_generic_bitmap   bitmap;
-       errcode_t               retval;
-       size_t                  size;
-
-       retval = ext2fs_get_mem(sizeof(struct ext2fs_struct_generic_bitmap), 
-                               &bitmap);
-       if (retval)
-               return retval;
-
-       bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
-       bitmap->fs = NULL;
-       bitmap->start = start;
-       bitmap->end = end;
-       bitmap->real_end = real_end;
-       bitmap->base_error_code = EXT2_ET_BAD_GENERIC_MARK;
-       if (descr) {
-               retval = ext2fs_get_mem(strlen(descr)+1, &bitmap->description);
-               if (retval) {
-                       ext2fs_free_mem(&bitmap);
-                       return retval;
-               }
-               strcpy(bitmap->description, descr);
-       } else
-               bitmap->description = 0;
-
-       size = (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1);
-       retval = ext2fs_get_mem(size, &bitmap->bitmap);
-       if (retval) {
-               ext2fs_free_mem(&bitmap->description);
-               ext2fs_free_mem(&bitmap);
-               return retval;
-       }
-
-       if (init_map)
-               memcpy(bitmap->bitmap, init_map, size);
-       else
-               memset(bitmap->bitmap, 0, size);
-       *ret = bitmap;
-       return 0;
+       ext2fs_free_generic_bitmap(bitmap);
 }
 
-errcode_t ext2fs_allocate_generic_bitmap(__u32 start,
-                                        __u32 end,
-                                        __u32 real_end,
-                                        const char *descr,
-                                        ext2fs_generic_bitmap *ret)
+void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap)
 {
-       return make_bitmap(start, end, real_end, descr, 0, ret);
+       ext2fs_free_generic_bitmap(bitmap);
 }
 
 errcode_t ext2fs_copy_bitmap(ext2fs_generic_bitmap src,
                             ext2fs_generic_bitmap *dest)
 {
-       errcode_t               retval;
-       ext2fs_generic_bitmap   new_map;
-
-       retval = make_bitmap(src->start, src->end, src->real_end,
-                            src->description, src->bitmap, &new_map);
-       if (retval)
-               return retval;
-       new_map->magic = src->magic;
-       new_map->fs = src->fs;
-       new_map->base_error_code = src->base_error_code;
-       *dest = new_map;
-       return 0;
+       return (ext2fs_copy_generic_bitmap(src, dest));
 }
 
 void ext2fs_set_bitmap_padding(ext2fs_generic_bitmap map)
 {
-       __u32   i, j;
-
-       /* Protect loop from wrap-around if map->real_end is maxed */
-       for (i=map->end+1, j = i - map->start; 
-            i <= map->real_end && i > map->end; 
-            i++, j++)
-               ext2fs_set_bit(j, map->bitmap);
-
-       return;
+       ext2fs_set_generic_bitmap_padding(map);
 }      
 
 errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
                                       const char *descr,
                                       ext2fs_inode_bitmap *ret)
 {
-       ext2fs_inode_bitmap bitmap;
        errcode_t       retval;
        __u32           start, end, real_end;
 
@@ -127,24 +63,15 @@ errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
        end = fs->super->s_inodes_count;
        real_end = (EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count);
 
-       retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
-                                               descr, &bitmap);
-       if (retval)
-               return retval;
-       
-       bitmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
-       bitmap->fs = fs;
-       bitmap->base_error_code = EXT2_ET_BAD_INODE_MARK;
-       
-       *ret = bitmap;
-       return 0;
+       return (ext2fs_make_generic_bitmap(EXT2_ET_MAGIC_INODE_BITMAP, fs,
+                                          start, end, real_end,
+                                          descr, 0, ret));
 }
 
 errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
                                       const char *descr,
                                       ext2fs_block_bitmap *ret)
 {
-       ext2fs_block_bitmap bitmap;
        errcode_t       retval;
        __u32           start, end, real_end;
 
@@ -157,59 +84,36 @@ errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
        real_end = (EXT2_BLOCKS_PER_GROUP(fs->super)  
                    * fs->group_desc_count)-1 + start;
        
-       retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
-                                               descr, &bitmap);
-       if (retval)
-               return retval;
-
-       bitmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
-       bitmap->fs = fs;
-       bitmap->base_error_code = EXT2_ET_BAD_BLOCK_MARK;
-       
-       *ret = bitmap;
-       return 0;
+       return (ext2fs_make_generic_bitmap(EXT2_ET_MAGIC_BLOCK_BITMAP, fs,
+                                          start, end, real_end,
+                                          descr, 0, ret));
 }
 
 errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
                                        ext2_ino_t end, ext2_ino_t *oend)
 {
-       EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_INODE_BITMAP);
-       
-       if (end > bitmap->real_end)
-               return EXT2_ET_FUDGE_INODE_BITMAP_END;
-       if (oend)
-               *oend = bitmap->end;
-       bitmap->end = end;
-       return 0;
+
+       return (ext2fs_fudge_generic_bitmap_end(bitmap,
+                                               EXT2_ET_MAGIC_INODE_BITMAP,
+                                               EXT2_ET_FUDGE_INODE_BITMAP_END,
+                                               end, oend));
 }
 
 errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
                                        blk_t end, blk_t *oend)
 {
-       EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
-       
-       if (end > bitmap->real_end)
-               return EXT2_ET_FUDGE_BLOCK_BITMAP_END;
-       if (oend)
-               *oend = bitmap->end;
-       bitmap->end = end;
-       return 0;
+       return (ext2fs_fudge_generic_bitmap_end(bitmap,
+                                               EXT2_ET_MAGIC_BLOCK_BITMAP,
+                                               EXT2_ET_FUDGE_BLOCK_BITMAP_END,
+                                               end, oend));
 }
 
 void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap)
 {
-       if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
-               return;
-
-       memset(bitmap->bitmap, 0,
-              (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));
+       ext2fs_clear_generic_bitmap(bitmap);
 }
 
 void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap)
 {
-       if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
-               return;
-
-       memset(bitmap->bitmap, 0,
-              (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));
+       ext2fs_clear_generic_bitmap(bitmap);
 }
index 78632c6..9c34847 100644 (file)
@@ -76,16 +76,3 @@ void ext2fs_warn_bitmap(errcode_t errcode, unsigned long arg,
                com_err(0, errcode, "#%lu", arg);
 #endif
 }
-
-void ext2fs_warn_bitmap2(ext2fs_generic_bitmap bitmap,
-                           int code, unsigned long arg)
-{
-#ifndef OMIT_COM_ERR
-       if (bitmap->description)
-               com_err(0, bitmap->base_error_code+code,
-                       "#%lu for %s", arg, bitmap->description);
-       else
-               com_err(0, bitmap->base_error_code + code, "#%lu", arg);
-#endif
-}
-
index 7645210..540af9d 100644 (file)
@@ -551,15 +551,12 @@ extern errcode_t ext2fs_update_bb_inode(ext2_filsys fs,
                                        ext2_badblocks_list bb_list);
 
 /* bitmaps.c */
+extern void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap);
+extern void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap);
 extern errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs);
 extern errcode_t ext2fs_write_block_bitmap (ext2_filsys fs);
 extern errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs);
 extern errcode_t ext2fs_read_block_bitmap(ext2_filsys fs);
-extern errcode_t ext2fs_allocate_generic_bitmap(__u32 start,
-                                               __u32 end,
-                                               __u32 real_end,
-                                               const char *descr,
-                                               ext2fs_generic_bitmap *ret);
 extern errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
                                              const char *descr,
                                              ext2fs_block_bitmap *ret);
@@ -750,13 +747,32 @@ extern errcode_t ext2fs_sync_device(int fd, int flushb);
 
 /* freefs.c */
 extern void ext2fs_free(ext2_filsys fs);
-extern void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap);
-extern void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap);
-extern void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap);
 extern void ext2fs_free_dblist(ext2_dblist dblist);
 extern void ext2fs_badblocks_list_free(ext2_badblocks_list bb);
 extern void ext2fs_u32_list_free(ext2_u32_list bb);
 
+/* gen_bitmap.c */
+extern void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap);
+extern errcode_t ext2fs_make_generic_bitmap(errcode_t magic, ext2_filsys fs, 
+                                           __u32 start, __u32 end, 
+                                           __u32 real_end,
+                                           const char *descr, char *init_map,
+                                           ext2fs_generic_bitmap *ret);
+extern errcode_t ext2fs_allocate_generic_bitmap(__u32 start,
+                                               __u32 end,
+                                               __u32 real_end,
+                                               const char *descr,
+                                               ext2fs_generic_bitmap *ret);
+extern errcode_t ext2fs_copy_generic_bitmap(ext2fs_generic_bitmap src,
+                                           ext2fs_generic_bitmap *dest);
+extern void ext2fs_clear_generic_bitmap(ext2fs_generic_bitmap bitmap);
+extern errcode_t ext2fs_fudge_generic_bitmap_end(ext2fs_inode_bitmap bitmap,
+                                                errcode_t magic, 
+                                                errcode_t neq,
+                                                ext2_ino_t end, 
+                                                ext2_ino_t *oend);
+extern void ext2fs_set_generic_bitmap_padding(ext2fs_generic_bitmap map);
+
 /* getsize.c */
 extern errcode_t ext2fs_get_device_size(const char *file, int blocksize,
                                        blk_t *retblocks);
index 029ffaa..d6ac901 100644 (file)
@@ -58,41 +58,6 @@ void ext2fs_free(ext2_filsys fs)
        ext2fs_free_mem(&fs);
 }
 
-void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap)
-{
-       if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_GENERIC_BITMAP))
-               return;
-
-       bitmap->magic = 0;
-       if (bitmap->description) {
-               ext2fs_free_mem(&bitmap->description);
-               bitmap->description = 0;
-       }
-       if (bitmap->bitmap) {
-               ext2fs_free_mem(&bitmap->bitmap);
-               bitmap->bitmap = 0;
-       }
-       ext2fs_free_mem(&bitmap);
-}
-
-void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap)
-{
-       if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
-               return;
-
-       bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
-       ext2fs_free_generic_bitmap(bitmap);
-}
-
-void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap)
-{
-       if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
-               return;
-
-       bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
-       ext2fs_free_generic_bitmap(bitmap);
-}
-
 /*
  * Free the inode cache structure
  */
index 53a9b9e..ce8312f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * gen_bitmap.c --- Generic bitmap routines that used to be inlined.
+ * gen_bitmap.c --- Generic (32-bit) bitmap routines
  * 
  * Copyright (C) 2001 Theodore Ts'o.
  *
 #include "ext2_fs.h"
 #include "ext2fs.h"
 
+/* 
+ * Used by previously inlined function, so we have to export this and
+ * not change the function signature
+ */
+void ext2fs_warn_bitmap2(ext2fs_generic_bitmap bitmap,
+                           int code, unsigned long arg)
+{
+#ifndef OMIT_COM_ERR
+       if (bitmap->description)
+               com_err(0, bitmap->base_error_code+code,
+                       "#%lu for %s", arg, bitmap->description);
+       else
+               com_err(0, bitmap->base_error_code + code, "#%lu", arg);
+#endif
+}
+
+static errcode_t check_magic(ext2fs_generic_bitmap bitmap)
+{
+       if (!bitmap || !((bitmap->magic == EXT2_ET_MAGIC_GENERIC_BITMAP) ||
+                        (bitmap->magic == EXT2_ET_MAGIC_INODE_BITMAP) ||
+                        (bitmap->magic == EXT2_ET_MAGIC_BLOCK_BITMAP)))
+               return EXT2_ET_MAGIC_GENERIC_BITMAP;
+       return 0;
+}
+
+errcode_t ext2fs_make_generic_bitmap(errcode_t magic, ext2_filsys fs, 
+                                    __u32 start, __u32 end, __u32 real_end,
+                                    const char *descr, char *init_map,
+                                    ext2fs_generic_bitmap *ret)
+{
+       ext2fs_generic_bitmap   bitmap;
+       errcode_t               retval;
+       size_t                  size;
+
+       retval = ext2fs_get_mem(sizeof(struct ext2fs_struct_generic_bitmap), 
+                               &bitmap);
+       if (retval)
+               return retval;
+
+       bitmap->magic = magic;
+       bitmap->fs = fs;
+       bitmap->start = start;
+       bitmap->end = end;
+       bitmap->real_end = real_end;
+       switch (magic) {
+       case EXT2_ET_MAGIC_INODE_BITMAP:
+               bitmap->base_error_code = EXT2_ET_BAD_INODE_MARK;
+               break;
+       case EXT2_ET_MAGIC_BLOCK_BITMAP:
+               bitmap->base_error_code = EXT2_ET_BAD_BLOCK_MARK;
+               break;
+       default:
+               bitmap->base_error_code = EXT2_ET_BAD_GENERIC_MARK;
+       }
+       if (descr) {
+               retval = ext2fs_get_mem(strlen(descr)+1, &bitmap->description);
+               if (retval) {
+                       ext2fs_free_mem(&bitmap);
+                       return retval;
+               }
+               strcpy(bitmap->description, descr);
+       } else
+               bitmap->description = 0;
+
+       size = (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1);
+       retval = ext2fs_get_mem(size, &bitmap->bitmap);
+       if (retval) {
+               ext2fs_free_mem(&bitmap->description);
+               ext2fs_free_mem(&bitmap);
+               return retval;
+       }
+
+       if (init_map)
+               memcpy(bitmap->bitmap, init_map, size);
+       else
+               memset(bitmap->bitmap, 0, size);
+       *ret = bitmap;
+       return 0;
+}
+
+errcode_t ext2fs_allocate_generic_bitmap(__u32 start,
+                                        __u32 end,
+                                        __u32 real_end,
+                                        const char *descr,
+                                        ext2fs_generic_bitmap *ret)
+{
+       return ext2fs_make_generic_bitmap(EXT2_ET_MAGIC_GENERIC_BITMAP, 0, 
+                                         start, end, real_end, descr, 0, ret);
+}
+
+errcode_t ext2fs_copy_generic_bitmap(ext2fs_generic_bitmap src,
+                                    ext2fs_generic_bitmap *dest)
+{
+       return (ext2fs_make_generic_bitmap(src->magic, src->fs,
+                                          src->start, src->end, 
+                                          src->real_end,
+                                          src->description, src->bitmap,
+                                          dest));
+}
+
+void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap)
+{
+       if (check_magic(bitmap))
+               return;
+
+       bitmap->magic = 0;
+       if (bitmap->description) {
+               ext2fs_free_mem(&bitmap->description);
+               bitmap->description = 0;
+       }
+       if (bitmap->bitmap) {
+               ext2fs_free_mem(&bitmap->bitmap);
+               bitmap->bitmap = 0;
+       }
+       ext2fs_free_mem(&bitmap);
+}
+
 int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
                                        blk_t bitno)
 {
@@ -67,6 +184,40 @@ __u32 ext2fs_get_generic_bitmap_end(ext2fs_generic_bitmap bitmap)
        return bitmap->end;
 }
 
+void ext2fs_clear_generic_bitmap(ext2fs_generic_bitmap bitmap)
+{
+       if (check_magic(bitmap))
+               return;
+
+       memset(bitmap->bitmap, 0,
+              (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));
+}
+
+errcode_t ext2fs_fudge_generic_bitmap_end(ext2fs_inode_bitmap bitmap,
+                                         errcode_t magic, errcode_t neq,
+                                         ext2_ino_t end, ext2_ino_t *oend)
+{
+       EXT2_CHECK_MAGIC(bitmap, magic);
+       
+       if (end > bitmap->real_end)
+               return neq;
+       if (oend)
+               *oend = bitmap->end;
+       bitmap->end = end;
+       return 0;
+}
+
+void ext2fs_set_generic_bitmap_padding(ext2fs_generic_bitmap map)
+{
+       __u32   i, j;
+
+       /* Protect loop from wrap-around if map->real_end is maxed */
+       for (i=map->end+1, j = i - map->start; 
+            i <= map->real_end && i > map->end; 
+            i++, j++)
+               ext2fs_set_bit(j, map->bitmap);
+}      
+
 int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
                                   blk_t block, int num)
 {