3
0

bitmaps.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * bitmaps.c --- routines to read, write, and manipulate the inode and
  4. * block bitmaps.
  5. *
  6. * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
  7. *
  8. * %Begin-Header%
  9. * This file may be redistributed under the terms of the GNU Public
  10. * License.
  11. * %End-Header%
  12. */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #if HAVE_UNISTD_H
  16. #include <unistd.h>
  17. #endif
  18. #include <fcntl.h>
  19. #include <time.h>
  20. #if HAVE_SYS_STAT_H
  21. #include <sys/stat.h>
  22. #endif
  23. #if HAVE_SYS_TYPES_H
  24. #include <sys/types.h>
  25. #endif
  26. #include "ext2_fs.h"
  27. #include "ext2fs.h"
  28. static errcode_t make_bitmap(__u32 start, __u32 end, __u32 real_end,
  29. const char *descr, char *init_map,
  30. ext2fs_generic_bitmap *ret)
  31. {
  32. ext2fs_generic_bitmap bitmap;
  33. errcode_t retval;
  34. size_t size;
  35. retval = ext2fs_get_mem(sizeof(struct ext2fs_struct_generic_bitmap),
  36. &bitmap);
  37. if (retval)
  38. return retval;
  39. bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
  40. bitmap->fs = NULL;
  41. bitmap->start = start;
  42. bitmap->end = end;
  43. bitmap->real_end = real_end;
  44. bitmap->base_error_code = EXT2_ET_BAD_GENERIC_MARK;
  45. if (descr) {
  46. retval = ext2fs_get_mem(strlen(descr)+1, &bitmap->description);
  47. if (retval) {
  48. ext2fs_free_mem(&bitmap);
  49. return retval;
  50. }
  51. strcpy(bitmap->description, descr);
  52. } else
  53. bitmap->description = 0;
  54. size = (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1);
  55. retval = ext2fs_get_mem(size, &bitmap->bitmap);
  56. if (retval) {
  57. ext2fs_free_mem(&bitmap->description);
  58. ext2fs_free_mem(&bitmap);
  59. return retval;
  60. }
  61. if (init_map)
  62. memcpy(bitmap->bitmap, init_map, size);
  63. else
  64. memset(bitmap->bitmap, 0, size);
  65. *ret = bitmap;
  66. return 0;
  67. }
  68. errcode_t ext2fs_allocate_generic_bitmap(__u32 start,
  69. __u32 end,
  70. __u32 real_end,
  71. const char *descr,
  72. ext2fs_generic_bitmap *ret)
  73. {
  74. return make_bitmap(start, end, real_end, descr, 0, ret);
  75. }
  76. errcode_t ext2fs_copy_bitmap(ext2fs_generic_bitmap src,
  77. ext2fs_generic_bitmap *dest)
  78. {
  79. errcode_t retval;
  80. ext2fs_generic_bitmap new_map;
  81. retval = make_bitmap(src->start, src->end, src->real_end,
  82. src->description, src->bitmap, &new_map);
  83. if (retval)
  84. return retval;
  85. new_map->magic = src->magic;
  86. new_map->fs = src->fs;
  87. new_map->base_error_code = src->base_error_code;
  88. *dest = new_map;
  89. return 0;
  90. }
  91. void ext2fs_set_bitmap_padding(ext2fs_generic_bitmap map)
  92. {
  93. __u32 i, j;
  94. for (i=map->end+1, j = i - map->start; i <= map->real_end; i++, j++)
  95. ext2fs_set_bit(j, map->bitmap);
  96. return;
  97. }
  98. errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
  99. const char *descr,
  100. ext2fs_inode_bitmap *ret)
  101. {
  102. ext2fs_inode_bitmap bitmap;
  103. errcode_t retval;
  104. __u32 start, end, real_end;
  105. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  106. fs->write_bitmaps = ext2fs_write_bitmaps;
  107. start = 1;
  108. end = fs->super->s_inodes_count;
  109. real_end = (EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count);
  110. retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
  111. descr, &bitmap);
  112. if (retval)
  113. return retval;
  114. bitmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
  115. bitmap->fs = fs;
  116. bitmap->base_error_code = EXT2_ET_BAD_INODE_MARK;
  117. *ret = bitmap;
  118. return 0;
  119. }
  120. errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
  121. const char *descr,
  122. ext2fs_block_bitmap *ret)
  123. {
  124. ext2fs_block_bitmap bitmap;
  125. errcode_t retval;
  126. __u32 start, end, real_end;
  127. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  128. fs->write_bitmaps = ext2fs_write_bitmaps;
  129. start = fs->super->s_first_data_block;
  130. end = fs->super->s_blocks_count-1;
  131. real_end = (EXT2_BLOCKS_PER_GROUP(fs->super)
  132. * fs->group_desc_count)-1 + start;
  133. retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
  134. descr, &bitmap);
  135. if (retval)
  136. return retval;
  137. bitmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
  138. bitmap->fs = fs;
  139. bitmap->base_error_code = EXT2_ET_BAD_BLOCK_MARK;
  140. *ret = bitmap;
  141. return 0;
  142. }
  143. errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
  144. ext2_ino_t end, ext2_ino_t *oend)
  145. {
  146. EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_INODE_BITMAP);
  147. if (end > bitmap->real_end)
  148. return EXT2_ET_FUDGE_INODE_BITMAP_END;
  149. if (oend)
  150. *oend = bitmap->end;
  151. bitmap->end = end;
  152. return 0;
  153. }
  154. errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
  155. blk_t end, blk_t *oend)
  156. {
  157. EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
  158. if (end > bitmap->real_end)
  159. return EXT2_ET_FUDGE_BLOCK_BITMAP_END;
  160. if (oend)
  161. *oend = bitmap->end;
  162. bitmap->end = end;
  163. return 0;
  164. }
  165. void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap)
  166. {
  167. if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
  168. return;
  169. memset(bitmap->bitmap, 0,
  170. (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));
  171. }
  172. void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap)
  173. {
  174. if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
  175. return;
  176. memset(bitmap->bitmap, 0,
  177. (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));
  178. }