ext2fs_inline.c 7.3 KB

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