ext2fs_inline.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * ext2fs.h --- ext2fs
  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 "ext2fs.h"
  13. #include "bitops.h"
  14. #include <string.h>
  15. /*
  16. * Allocate memory
  17. */
  18. errcode_t ext2fs_get_mem(unsigned long size, void *ptr)
  19. {
  20. void **pp = (void **)ptr;
  21. *pp = malloc(size);
  22. if (!*pp)
  23. return EXT2_ET_NO_MEMORY;
  24. return 0;
  25. }
  26. /*
  27. * Free memory
  28. */
  29. errcode_t ext2fs_free_mem(void *ptr)
  30. {
  31. void **pp = (void **)ptr;
  32. free(*pp);
  33. *pp = 0;
  34. return 0;
  35. }
  36. /*
  37. * Resize memory
  38. */
  39. errcode_t ext2fs_resize_mem(unsigned long EXT2FS_ATTR((unused)) old_size,
  40. unsigned long size, void *ptr)
  41. {
  42. void *p;
  43. /* Use "memcpy" for pointer assignments here to avoid problems
  44. * with C99 strict type aliasing rules. */
  45. memcpy(&p, ptr, sizeof (p));
  46. p = xrealloc(p, size);
  47. memcpy(ptr, &p, sizeof (p));
  48. return 0;
  49. }
  50. /*
  51. * Mark a filesystem superblock as dirty
  52. */
  53. void ext2fs_mark_super_dirty(ext2_filsys fs)
  54. {
  55. fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_CHANGED;
  56. }
  57. /*
  58. * Mark a filesystem as changed
  59. */
  60. void ext2fs_mark_changed(ext2_filsys fs)
  61. {
  62. fs->flags |= EXT2_FLAG_CHANGED;
  63. }
  64. /*
  65. * Check to see if a filesystem has changed
  66. */
  67. int ext2fs_test_changed(ext2_filsys fs)
  68. {
  69. return (fs->flags & EXT2_FLAG_CHANGED);
  70. }
  71. /*
  72. * Mark a filesystem as valid
  73. */
  74. void ext2fs_mark_valid(ext2_filsys fs)
  75. {
  76. fs->flags |= EXT2_FLAG_VALID;
  77. }
  78. /*
  79. * Mark a filesystem as NOT valid
  80. */
  81. void ext2fs_unmark_valid(ext2_filsys fs)
  82. {
  83. fs->flags &= ~EXT2_FLAG_VALID;
  84. }
  85. /*
  86. * Check to see if a filesystem is valid
  87. */
  88. int ext2fs_test_valid(ext2_filsys fs)
  89. {
  90. return (fs->flags & EXT2_FLAG_VALID);
  91. }
  92. /*
  93. * Mark the inode bitmap as dirty
  94. */
  95. void ext2fs_mark_ib_dirty(ext2_filsys fs)
  96. {
  97. fs->flags |= EXT2_FLAG_IB_DIRTY | EXT2_FLAG_CHANGED;
  98. }
  99. /*
  100. * Mark the block bitmap as dirty
  101. */
  102. void ext2fs_mark_bb_dirty(ext2_filsys fs)
  103. {
  104. fs->flags |= EXT2_FLAG_BB_DIRTY | EXT2_FLAG_CHANGED;
  105. }
  106. /*
  107. * Check to see if a filesystem's inode bitmap is dirty
  108. */
  109. int ext2fs_test_ib_dirty(ext2_filsys fs)
  110. {
  111. return (fs->flags & EXT2_FLAG_IB_DIRTY);
  112. }
  113. /*
  114. * Check to see if a filesystem's block bitmap is dirty
  115. */
  116. int ext2fs_test_bb_dirty(ext2_filsys fs)
  117. {
  118. return (fs->flags & EXT2_FLAG_BB_DIRTY);
  119. }
  120. /*
  121. * Return the group # of a block
  122. */
  123. int ext2fs_group_of_blk(ext2_filsys fs, blk_t blk)
  124. {
  125. return (blk - fs->super->s_first_data_block) /
  126. fs->super->s_blocks_per_group;
  127. }
  128. /*
  129. * Return the group # of an inode number
  130. */
  131. int ext2fs_group_of_ino(ext2_filsys fs, ext2_ino_t ino)
  132. {
  133. return (ino - 1) / fs->super->s_inodes_per_group;
  134. }
  135. blk_t ext2fs_inode_data_blocks(ext2_filsys fs,
  136. struct ext2_inode *inode)
  137. {
  138. return inode->i_blocks -
  139. (inode->i_file_acl ? fs->blocksize >> 9 : 0);
  140. }
  141. __u16 ext2fs_swab16(__u16 val)
  142. {
  143. return (val >> 8) | (val << 8);
  144. }
  145. __u32 ext2fs_swab32(__u32 val)
  146. {
  147. return ((val>>24) | ((val>>8)&0xFF00) |
  148. ((val<<8)&0xFF0000) | (val<<24));
  149. }
  150. int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
  151. blk_t bitno);
  152. int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
  153. blk_t bitno)
  154. {
  155. if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
  156. ext2fs_warn_bitmap2(bitmap, EXT2FS_TEST_ERROR, bitno);
  157. return 0;
  158. }
  159. return ext2fs_test_bit(bitno - bitmap->start, bitmap->bitmap);
  160. }
  161. int ext2fs_mark_block_bitmap(ext2fs_block_bitmap bitmap,
  162. blk_t block)
  163. {
  164. return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap)
  165. bitmap,
  166. block);
  167. }
  168. int ext2fs_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
  169. blk_t block)
  170. {
  171. return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
  172. block);
  173. }
  174. int ext2fs_test_block_bitmap(ext2fs_block_bitmap bitmap,
  175. blk_t block)
  176. {
  177. return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
  178. block);
  179. }
  180. int ext2fs_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
  181. ext2_ino_t inode)
  182. {
  183. return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
  184. inode);
  185. }
  186. int ext2fs_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
  187. ext2_ino_t inode)
  188. {
  189. return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
  190. inode);
  191. }
  192. int ext2fs_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
  193. ext2_ino_t inode)
  194. {
  195. return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
  196. inode);
  197. }
  198. void ext2fs_fast_mark_block_bitmap(ext2fs_block_bitmap bitmap,
  199. blk_t block)
  200. {
  201. ext2fs_set_bit(block - bitmap->start, bitmap->bitmap);
  202. }
  203. void ext2fs_fast_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
  204. blk_t block)
  205. {
  206. ext2fs_clear_bit(block - bitmap->start, bitmap->bitmap);
  207. }
  208. int ext2fs_fast_test_block_bitmap(ext2fs_block_bitmap bitmap,
  209. blk_t block)
  210. {
  211. return ext2fs_test_bit(block - bitmap->start, bitmap->bitmap);
  212. }
  213. void ext2fs_fast_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
  214. ext2_ino_t inode)
  215. {
  216. ext2fs_set_bit(inode - bitmap->start, bitmap->bitmap);
  217. }
  218. void ext2fs_fast_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
  219. ext2_ino_t inode)
  220. {
  221. ext2fs_clear_bit(inode - bitmap->start, bitmap->bitmap);
  222. }
  223. int ext2fs_fast_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
  224. ext2_ino_t inode)
  225. {
  226. return ext2fs_test_bit(inode - bitmap->start, bitmap->bitmap);
  227. }
  228. blk_t ext2fs_get_block_bitmap_start(ext2fs_block_bitmap bitmap)
  229. {
  230. return bitmap->start;
  231. }
  232. ext2_ino_t ext2fs_get_inode_bitmap_start(ext2fs_inode_bitmap bitmap)
  233. {
  234. return bitmap->start;
  235. }
  236. blk_t ext2fs_get_block_bitmap_end(ext2fs_block_bitmap bitmap)
  237. {
  238. return bitmap->end;
  239. }
  240. ext2_ino_t ext2fs_get_inode_bitmap_end(ext2fs_inode_bitmap bitmap)
  241. {
  242. return bitmap->end;
  243. }
  244. int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
  245. blk_t block, int num)
  246. {
  247. int i;
  248. if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
  249. ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
  250. block, bitmap->description);
  251. return 0;
  252. }
  253. for (i=0; i < num; i++) {
  254. if (ext2fs_fast_test_block_bitmap(bitmap, block+i))
  255. return 0;
  256. }
  257. return 1;
  258. }
  259. int ext2fs_fast_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
  260. blk_t block, int num)
  261. {
  262. int i;
  263. for (i=0; i < num; i++) {
  264. if (ext2fs_fast_test_block_bitmap(bitmap, block+i))
  265. return 0;
  266. }
  267. return 1;
  268. }
  269. void ext2fs_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
  270. blk_t block, int num)
  271. {
  272. int i;
  273. if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
  274. ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
  275. bitmap->description);
  276. return;
  277. }
  278. for (i=0; i < num; i++)
  279. ext2fs_set_bit(block + i - bitmap->start, bitmap->bitmap);
  280. }
  281. void ext2fs_fast_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
  282. blk_t block, int num)
  283. {
  284. int i;
  285. for (i=0; i < num; i++)
  286. ext2fs_set_bit(block + i - bitmap->start, bitmap->bitmap);
  287. }
  288. void ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
  289. blk_t block, int num)
  290. {
  291. int i;
  292. if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
  293. ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK, block,
  294. bitmap->description);
  295. return;
  296. }
  297. for (i=0; i < num; i++)
  298. ext2fs_clear_bit(block + i - bitmap->start, bitmap->bitmap);
  299. }
  300. void ext2fs_fast_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
  301. blk_t block, int num)
  302. {
  303. int i;
  304. for (i=0; i < num; i++)
  305. ext2fs_clear_bit(block + i - bitmap->start, bitmap->bitmap);
  306. }