Whamcloud - gitweb
libext2fs: use strcpy()/strcat() instead of sprintf() in bmap functions
[tools/e2fsprogs.git] / lib / ext2fs / gen_bitmap.c
index ed3afef..0bff854 100644 (file)
@@ -4,12 +4,13 @@
  * Copyright (C) 2001 Theodore Ts'o.
  *
  * %Begin-Header%
- * This file may be redistributed under the terms of the GNU Public
- * License.
+ * This file may be redistributed under the terms of the GNU Library
+ * General Public License, version 2.
  * %End-Header%
  */
 
 
+#include "config.h"
 #include <stdio.h>
 #include <string.h>
 #if HAVE_UNISTD_H
@@ -38,6 +39,16 @@ struct ext2fs_struct_generic_bitmap {
        __u32           reserved[7];
 };
 
+#define EXT2FS_IS_32_BITMAP(bmap) \
+       (((bmap)->magic == EXT2_ET_MAGIC_GENERIC_BITMAP) || \
+        ((bmap)->magic == EXT2_ET_MAGIC_BLOCK_BITMAP) || \
+        ((bmap)->magic == EXT2_ET_MAGIC_INODE_BITMAP))
+
+#define EXT2FS_IS_64_BITMAP(bmap) \
+       (((bmap)->magic == EXT2_ET_MAGIC_GENERIC_BITMAP64) || \
+        ((bmap)->magic == EXT2_ET_MAGIC_BLOCK_BITMAP64) || \
+        ((bmap)->magic == EXT2_ET_MAGIC_INODE_BITMAP64))
+
 /*
  * Used by previously inlined function, so we have to export this and
  * not change the function signature
@@ -103,6 +114,8 @@ errcode_t ext2fs_make_generic_bitmap(errcode_t magic, ext2_filsys fs,
                bitmap->description = 0;
 
        size = (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1);
+       /* Round up to allow for the BT x86 instruction */
+       size = (size + 7) & ~3;
        retval = ext2fs_get_mem(size, &bitmap->bitmap);
        if (retval) {
                ext2fs_free_mem(&bitmap->description);
@@ -158,6 +171,18 @@ void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap)
 int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
                                        blk_t bitno)
 {
+       if (!EXT2FS_IS_32_BITMAP(bitmap)) {
+               if (EXT2FS_IS_64_BITMAP(bitmap)) {
+                       ext2fs_warn_bitmap32(bitmap, __func__);
+                       return ext2fs_test_generic_bmap(bitmap, bitno);
+               }
+#ifndef OMIT_COM_ERR
+               com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
+                       "test_bitmap(%lu)", (unsigned long) bitno);
+#endif
+               return 0;
+       }
+
        if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
                ext2fs_warn_bitmap2(bitmap, EXT2FS_TEST_ERROR, bitno);
                return 0;
@@ -168,6 +193,18 @@ int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
 int ext2fs_mark_generic_bitmap(ext2fs_generic_bitmap bitmap,
                                         __u32 bitno)
 {
+       if (!EXT2FS_IS_32_BITMAP(bitmap)) {
+               if (EXT2FS_IS_64_BITMAP(bitmap)) {
+                       ext2fs_warn_bitmap32(bitmap, __func__);
+                       return ext2fs_mark_generic_bmap(bitmap, bitno);
+               }
+#ifndef OMIT_COM_ERR
+               com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
+                       "mark_bitmap(%lu)", (unsigned long) bitno);
+#endif
+               return 0;
+       }
+
        if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
                ext2fs_warn_bitmap2(bitmap, EXT2FS_MARK_ERROR, bitno);
                return 0;
@@ -178,6 +215,18 @@ int ext2fs_mark_generic_bitmap(ext2fs_generic_bitmap bitmap,
 int ext2fs_unmark_generic_bitmap(ext2fs_generic_bitmap bitmap,
                                           blk_t bitno)
 {
+       if (!EXT2FS_IS_32_BITMAP(bitmap)) {
+               if (EXT2FS_IS_64_BITMAP(bitmap)) {
+                       ext2fs_warn_bitmap32(bitmap, __func__);
+                       return ext2fs_unmark_generic_bmap(bitmap, bitno);
+               }
+#ifndef OMIT_COM_ERR
+               com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
+                       "mark_bitmap(%lu)", (unsigned long) bitno);
+#endif
+               return 0;
+       }
+
        if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
                ext2fs_warn_bitmap2(bitmap, EXT2FS_UNMARK_ERROR, bitno);
                return 0;
@@ -187,18 +236,51 @@ int ext2fs_unmark_generic_bitmap(ext2fs_generic_bitmap bitmap,
 
 __u32 ext2fs_get_generic_bitmap_start(ext2fs_generic_bitmap bitmap)
 {
+       if (!EXT2FS_IS_32_BITMAP(bitmap)) {
+               if (EXT2FS_IS_64_BITMAP(bitmap)) {
+                       ext2fs_warn_bitmap32(bitmap, __func__);
+                       return ext2fs_get_generic_bmap_start(bitmap);
+               }
+#ifndef OMIT_COM_ERR
+               com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
+                       "get_bitmap_start");
+#endif
+               return 0;
+       }
+
        return bitmap->start;
 }
 
 __u32 ext2fs_get_generic_bitmap_end(ext2fs_generic_bitmap bitmap)
 {
+       if (!EXT2FS_IS_32_BITMAP(bitmap)) {
+               if (EXT2FS_IS_64_BITMAP(bitmap)) {
+                       ext2fs_warn_bitmap32(bitmap, __func__);
+                       return ext2fs_get_generic_bmap_end(bitmap);
+               }
+#ifndef OMIT_COM_ERR
+               com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
+                       "get_bitmap_end");
+#endif
+               return 0;
+       }
        return bitmap->end;
 }
 
 void ext2fs_clear_generic_bitmap(ext2fs_generic_bitmap bitmap)
 {
-       if (check_magic(bitmap))
+       if (!EXT2FS_IS_32_BITMAP(bitmap)) {
+               if (EXT2FS_IS_64_BITMAP(bitmap)) {
+                       ext2fs_warn_bitmap32(bitmap, __func__);
+                       ext2fs_clear_generic_bmap(bitmap);
+                       return;
+               }
+#ifndef OMIT_COM_ERR
+               com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
+                       "clear_generic_bitmap");
+#endif
                return;
+       }
 
        memset(bitmap->bitmap, 0,
               (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));
@@ -422,6 +504,30 @@ static int ext2fs_test_clear_generic_bitmap_range(ext2fs_generic_bitmap bitmap,
        return ext2fs_mem_is_zero(ADDR + start_byte, len_byte);
 }
 
+errcode_t ext2fs_find_first_zero_generic_bitmap(ext2fs_generic_bitmap bitmap,
+                                               __u32 start, __u32 end,
+                                               __u32 *out)
+{
+       blk_t b;
+
+       if (start < bitmap->start || end > bitmap->end || start > end) {
+               ext2fs_warn_bitmap2(bitmap, EXT2FS_TEST_ERROR, start);
+               return EINVAL;
+       }
+
+       while (start <= end) {
+               b = ext2fs_test_bit(start - bitmap->start, bitmap->bitmap);
+               if (!b) {
+                       *out = start;
+                       return 0;
+               }
+               start++;
+       }
+
+       return ENOENT;
+}
+
+
 int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
                                   blk_t block, int num)
 {
@@ -476,3 +582,4 @@ void ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
                ext2fs_fast_clear_bit(block + i - bitmap->start,
                                      bitmap->bitmap);
 }
+