ext4_block_group.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
  3. *
  4. *
  5. * HelenOS:
  6. * Copyright (c) 2012 Martin Sucha
  7. * Copyright (c) 2012 Frantisek Princ
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * - The name of the author may not be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  23. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  24. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  27. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  31. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. /** @addtogroup lwext4
  34. * @{
  35. */
  36. /**
  37. * @file ext4_block_group.h
  38. * @brief Block group function set.
  39. */
  40. #ifndef EXT4_BLOCK_GROUP_H_
  41. #define EXT4_BLOCK_GROUP_H_
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. #include <ext4_config.h>
  46. #include <ext4_types.h>
  47. #include <ext4_super.h>
  48. #include <stdint.h>
  49. #include <stdbool.h>
  50. /**@brief Get address of block with data block bitmap.
  51. * @param bg pointer to block group
  52. * @param s pointer to superblock
  53. * @return Address of block with block bitmap
  54. */
  55. static inline uint64_t ext4_bg_get_block_bitmap(struct ext4_bgroup *bg,
  56. struct ext4_sblock *s)
  57. {
  58. uint64_t v = to_le32(bg->block_bitmap_lo);
  59. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  60. v |= (uint64_t)to_le32(bg->block_bitmap_hi) << 32;
  61. return v;
  62. }
  63. /**@brief Set address of block with data block bitmap.
  64. * @param bg pointer to block group
  65. * @param s pointer to superblock
  66. * @param blk block to set
  67. */
  68. static inline void ext4_bg_set_block_bitmap(struct ext4_bgroup *bg,
  69. struct ext4_sblock *s, uint64_t blk)
  70. {
  71. bg->block_bitmap_lo = to_le32((uint32_t)blk);
  72. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  73. bg->block_bitmap_hi = to_le32(blk >> 32);
  74. }
  75. /**@brief Get address of block with i-node bitmap.
  76. * @param bg Pointer to block group
  77. * @param s Pointer to superblock
  78. * @return Address of block with i-node bitmap
  79. */
  80. static inline uint64_t ext4_bg_get_inode_bitmap(struct ext4_bgroup *bg,
  81. struct ext4_sblock *s)
  82. {
  83. uint64_t v = to_le32(bg->inode_bitmap_lo);
  84. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  85. v |= (uint64_t)to_le32(bg->inode_bitmap_hi) << 32;
  86. return v;
  87. }
  88. /**@brief Set address of block with i-node bitmap.
  89. * @param bg Pointer to block group
  90. * @param s Pointer to superblock
  91. * @param blk block to set
  92. */
  93. static inline void ext4_bg_set_inode_bitmap(struct ext4_bgroup *bg,
  94. struct ext4_sblock *s, uint64_t blk)
  95. {
  96. bg->inode_bitmap_lo = to_le32((uint32_t)blk);
  97. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  98. bg->inode_bitmap_hi = to_le32(blk >> 32);
  99. }
  100. /**@brief Get address of the first block of the i-node table.
  101. * @param bg Pointer to block group
  102. * @param s Pointer to superblock
  103. * @return Address of first block of i-node table
  104. */
  105. static inline uint64_t
  106. ext4_bg_get_inode_table_first_block(struct ext4_bgroup *bg,
  107. struct ext4_sblock *s)
  108. {
  109. uint64_t v = to_le32(bg->inode_table_first_block_lo);
  110. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  111. v |= (uint64_t)to_le32(bg->inode_table_first_block_hi) << 32;
  112. return v;
  113. }
  114. /**@brief Set address of the first block of the i-node table.
  115. * @param bg Pointer to block group
  116. * @param s Pointer to superblock
  117. * @param blk block to set
  118. */
  119. static inline void
  120. ext4_bg_set_inode_table_first_block(struct ext4_bgroup *bg,
  121. struct ext4_sblock *s, uint64_t blk)
  122. {
  123. bg->inode_table_first_block_lo = to_le32((uint32_t)blk);
  124. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  125. bg->inode_table_first_block_hi = to_le32(blk >> 32);
  126. }
  127. /**@brief Get number of free blocks in block group.
  128. * @param bg Pointer to block group
  129. * @param s Pointer to superblock
  130. * @return Number of free blocks in block group
  131. */
  132. static inline uint32_t ext4_bg_get_free_blocks_count(struct ext4_bgroup *bg,
  133. struct ext4_sblock *s)
  134. {
  135. uint32_t v = to_le16(bg->free_blocks_count_lo);
  136. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  137. v |= (uint32_t)to_le16(bg->free_blocks_count_hi) << 16;
  138. return v;
  139. }
  140. /**@brief Set number of free blocks in block group.
  141. * @param bg Pointer to block group
  142. * @param s Pointer to superblock
  143. * @param cnt Number of free blocks in block group
  144. */
  145. static inline void ext4_bg_set_free_blocks_count(struct ext4_bgroup *bg,
  146. struct ext4_sblock *s,
  147. uint32_t cnt)
  148. {
  149. bg->free_blocks_count_lo = to_le16((cnt << 16) >> 16);
  150. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  151. bg->free_blocks_count_hi = to_le16(cnt >> 16);
  152. }
  153. /**@brief Get number of free i-nodes in block group.
  154. * @param bg Pointer to block group
  155. * @param s Pointer to superblock
  156. * @return Number of free i-nodes in block group
  157. */
  158. static inline uint32_t ext4_bg_get_free_inodes_count(struct ext4_bgroup *bg,
  159. struct ext4_sblock *s)
  160. {
  161. uint32_t v = to_le16(bg->free_inodes_count_lo);
  162. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  163. v |= (uint32_t)to_le16(bg->free_inodes_count_hi) << 16;
  164. return v;
  165. }
  166. /**@brief Set number of free i-nodes in block group.
  167. * @param bg Pointer to block group
  168. * @param s Pointer to superblock
  169. * @param cnt Number of free i-nodes in block group
  170. */
  171. static inline void ext4_bg_set_free_inodes_count(struct ext4_bgroup *bg,
  172. struct ext4_sblock *s,
  173. uint32_t cnt)
  174. {
  175. bg->free_inodes_count_lo = to_le16((cnt << 16) >> 16);
  176. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  177. bg->free_inodes_count_hi = to_le16(cnt >> 16);
  178. }
  179. /**@brief Get number of used directories in block group.
  180. * @param bg Pointer to block group
  181. * @param s Pointer to superblock
  182. * @return Number of used directories in block group
  183. */
  184. static inline uint32_t ext4_bg_get_used_dirs_count(struct ext4_bgroup *bg,
  185. struct ext4_sblock *s)
  186. {
  187. uint32_t v = to_le16(bg->used_dirs_count_lo);
  188. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  189. v |= (uint32_t)to_le16(bg->used_dirs_count_hi) << 16;
  190. return v;
  191. }
  192. /**@brief Set number of used directories in block group.
  193. * @param bg Pointer to block group
  194. * @param s Pointer to superblock
  195. * @param cnt Number of used directories in block group
  196. */
  197. static inline void ext4_bg_set_used_dirs_count(struct ext4_bgroup *bg,
  198. struct ext4_sblock *s,
  199. uint32_t cnt)
  200. {
  201. bg->used_dirs_count_lo = to_le16((cnt << 16) >> 16);
  202. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  203. bg->used_dirs_count_hi = to_le16(cnt >> 16);
  204. }
  205. /**@brief Get number of unused i-nodes.
  206. * @param bg Pointer to block group
  207. * @param s Pointer to superblock
  208. * @return Number of unused i-nodes
  209. */
  210. static inline uint32_t ext4_bg_get_itable_unused(struct ext4_bgroup *bg,
  211. struct ext4_sblock *s)
  212. {
  213. uint32_t v = to_le16(bg->itable_unused_lo);
  214. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  215. v |= (uint32_t)to_le16(bg->itable_unused_hi) << 16;
  216. return v;
  217. }
  218. /**@brief Set number of unused i-nodes.
  219. * @param bg Pointer to block group
  220. * @param s Pointer to superblock
  221. * @param cnt Number of unused i-nodes
  222. */
  223. static inline void ext4_bg_set_itable_unused(struct ext4_bgroup *bg,
  224. struct ext4_sblock *s,
  225. uint32_t cnt)
  226. {
  227. bg->itable_unused_lo = to_le16((cnt << 16) >> 16);
  228. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  229. bg->itable_unused_hi = to_le16(cnt >> 16);
  230. }
  231. /**@brief Set checksum of block group.
  232. * @param bg Pointer to block group
  233. * @param crc Cheksum of block group
  234. */
  235. static inline void ext4_bg_set_checksum(struct ext4_bgroup *bg, uint16_t crc)
  236. {
  237. bg->checksum = to_le16(crc);
  238. }
  239. /**@brief Check if block group has a flag.
  240. * @param bg Pointer to block group
  241. * @param f Flag to be checked
  242. * @return True if flag is set to 1
  243. */
  244. static inline bool ext4_bg_has_flag(struct ext4_bgroup *bg, uint32_t f)
  245. {
  246. return to_le16(bg->flags) & f;
  247. }
  248. /**@brief Set flag of block group.
  249. * @param bg Pointer to block group
  250. * @param f Flag to be set
  251. */
  252. static inline void ext4_bg_set_flag(struct ext4_bgroup *bg, uint32_t f)
  253. {
  254. uint16_t flags = to_le16(bg->flags);
  255. flags |= f;
  256. bg->flags = to_le16(flags);
  257. }
  258. /**@brief Clear flag of block group.
  259. * @param bg Pointer to block group
  260. * @param f Flag to be cleared
  261. */
  262. static inline void ext4_bg_clear_flag(struct ext4_bgroup *bg, uint32_t f)
  263. {
  264. uint16_t flags = to_le16(bg->flags);
  265. flags &= ~f;
  266. bg->flags = to_le16(flags);
  267. }
  268. /**@brief Calculate CRC16 of the block group.
  269. * @param crc Init value
  270. * @param buffer Input buffer
  271. * @param len Sizeof input buffer
  272. * @return Computed CRC16*/
  273. uint16_t ext4_bg_crc16(uint16_t crc, const uint8_t *buffer, size_t len);
  274. #ifdef __cplusplus
  275. }
  276. #endif
  277. #endif /* EXT4_BLOCK_GROUP_H_ */
  278. /**
  279. * @}
  280. */