bitmaps.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. }
  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. }