3
0

bitmaps.c 4.8 KB

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