ext4_block_group.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. * @return Address of block with block bitmap
  68. */
  69. static inline void ext4_bg_set_block_bitmap(struct ext4_bgroup *bg,
  70. struct ext4_sblock *s, uint64_t blk)
  71. {
  72. bg->block_bitmap_lo = to_le32((uint32_t)blk);
  73. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  74. bg->block_bitmap_hi = to_le32(blk >> 32);
  75. }
  76. /**@brief Get address of block with i-node bitmap.
  77. * @param bg Pointer to block group
  78. * @param s Pointer to superblock
  79. * @return Address of block with i-node bitmap
  80. */
  81. static inline uint64_t ext4_bg_get_inode_bitmap(struct ext4_bgroup *bg,
  82. struct ext4_sblock *s)
  83. {
  84. uint64_t v = to_le32(bg->inode_bitmap_lo);
  85. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  86. v |= (uint64_t)to_le32(bg->inode_bitmap_hi) << 32;
  87. return v;
  88. }
  89. /**@brief Set address of block with i-node bitmap.
  90. * @param bg Pointer to block group
  91. * @param s Pointer to superblock
  92. * @param blk block to set
  93. * @return Address of block with i-node bitmap
  94. */
  95. static inline void ext4_bg_set_inode_bitmap(struct ext4_bgroup *bg,
  96. struct ext4_sblock *s, uint64_t blk)
  97. {
  98. bg->inode_bitmap_lo = to_le32((uint32_t)blk);
  99. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  100. bg->inode_bitmap_hi = to_le32(blk >> 32);
  101. }
  102. /**@brief Get address of the first block of the i-node table.
  103. * @param bg Pointer to block group
  104. * @param s Pointer to superblock
  105. * @return Address of first block of i-node table
  106. */
  107. static inline uint64_t
  108. ext4_bg_get_inode_table_first_block(struct ext4_bgroup *bg,
  109. struct ext4_sblock *s)
  110. {
  111. uint64_t v = to_le32(bg->inode_table_first_block_lo);
  112. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  113. v |= (uint64_t)to_le32(bg->inode_table_first_block_hi) << 32;
  114. return v;
  115. }
  116. /**@brief Set address of the first block of the i-node table.
  117. * @param bg Pointer to block group
  118. * @param s Pointer to superblock
  119. * @param blk block to set
  120. * @return Address of first block of i-node table
  121. */
  122. static inline void
  123. ext4_bg_set_inode_table_first_block(struct ext4_bgroup *bg,
  124. struct ext4_sblock *s, uint64_t blk)
  125. {
  126. bg->inode_table_first_block_lo = to_le32((uint32_t)blk);
  127. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  128. bg->inode_table_first_block_hi = to_le32(blk >> 32);
  129. }
  130. /**@brief Get number of free blocks in block group.
  131. * @param bg Pointer to block group
  132. * @param sb Pointer to superblock
  133. * @return Number of free blocks in block group
  134. */
  135. static inline uint32_t ext4_bg_get_free_blocks_count(struct ext4_bgroup *bg,
  136. struct ext4_sblock *s)
  137. {
  138. uint32_t v = to_le16(bg->free_blocks_count_lo);
  139. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  140. v |= (uint32_t)to_le16(bg->free_blocks_count_hi) << 16;
  141. return v;
  142. }
  143. /**@brief Set number of free blocks in block group.
  144. * @param bg Pointer to block group
  145. * @param s Pointer to superblock
  146. * @param cnt Number of free blocks in block group
  147. */
  148. static inline void ext4_bg_set_free_blocks_count(struct ext4_bgroup *bg,
  149. struct ext4_sblock *s,
  150. uint32_t cnt)
  151. {
  152. bg->free_blocks_count_lo = to_le16((cnt << 16) >> 16);
  153. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  154. bg->free_blocks_count_hi = to_le16(cnt >> 16);
  155. }
  156. /**@brief Get number of free i-nodes in block group.
  157. * @param bg Pointer to block group
  158. * @param s Pointer to superblock
  159. * @return Number of free i-nodes in block group
  160. */
  161. static inline uint32_t ext4_bg_get_free_inodes_count(struct ext4_bgroup *bg,
  162. struct ext4_sblock *s)
  163. {
  164. uint32_t v = to_le16(bg->free_inodes_count_lo);
  165. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  166. v |= (uint32_t)to_le16(bg->free_inodes_count_hi) << 16;
  167. return v;
  168. }
  169. /**@brief Set number of free i-nodes in block group.
  170. * @param bg Pointer to block group
  171. * @param s Pointer to superblock
  172. * @param cnt Number of free i-nodes in block group
  173. */
  174. static inline void ext4_bg_set_free_inodes_count(struct ext4_bgroup *bg,
  175. struct ext4_sblock *s,
  176. uint32_t cnt)
  177. {
  178. bg->free_inodes_count_lo = to_le16((cnt << 16) >> 16);
  179. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  180. bg->free_inodes_count_hi = to_le16(cnt >> 16);
  181. }
  182. /**@brief Get number of used directories in block group.
  183. * @param bg Pointer to block group
  184. * @param s Pointer to superblock
  185. * @return Number of used directories in block group
  186. */
  187. static inline uint32_t ext4_bg_get_used_dirs_count(struct ext4_bgroup *bg,
  188. struct ext4_sblock *s)
  189. {
  190. uint32_t v = to_le16(bg->used_dirs_count_lo);
  191. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  192. v |= (uint32_t)to_le16(bg->used_dirs_count_hi) << 16;
  193. return v;
  194. }
  195. /**@brief Set number of used directories in block group.
  196. * @param bg Pointer to block group
  197. * @param s Pointer to superblock
  198. * @param cnt Number of used directories in block group
  199. */
  200. static inline void ext4_bg_set_used_dirs_count(struct ext4_bgroup *bg,
  201. struct ext4_sblock *s,
  202. uint32_t cnt)
  203. {
  204. bg->used_dirs_count_lo = to_le16((cnt << 16) >> 16);
  205. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  206. bg->used_dirs_count_hi = to_le16(cnt >> 16);
  207. }
  208. /**@brief Get number of unused i-nodes.
  209. * @param bg Pointer to block group
  210. * @param s Pointer to superblock
  211. * @return Number of unused i-nodes
  212. */
  213. static inline uint32_t ext4_bg_get_itable_unused(struct ext4_bgroup *bg,
  214. struct ext4_sblock *s)
  215. {
  216. uint32_t v = to_le16(bg->itable_unused_lo);
  217. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  218. v |= (uint32_t)to_le16(bg->itable_unused_hi) << 16;
  219. return v;
  220. }
  221. /**@brief Set number of unused i-nodes.
  222. * @param bg Pointer to block group
  223. * @param s Pointer to superblock
  224. * @param cnt Number of unused i-nodes
  225. */
  226. static inline void ext4_bg_set_itable_unused(struct ext4_bgroup *bg,
  227. struct ext4_sblock *s,
  228. uint32_t cnt)
  229. {
  230. bg->itable_unused_lo = to_le16((cnt << 16) >> 16);
  231. if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
  232. bg->itable_unused_hi = to_le16(cnt >> 16);
  233. }
  234. /**@brief Set checksum of block group.
  235. * @param bg Pointer to block group
  236. * @param crc Cheksum of block group
  237. */
  238. static inline void ext4_bg_set_checksum(struct ext4_bgroup *bg, uint16_t crc)
  239. {
  240. bg->checksum = to_le16(crc);
  241. }
  242. /**@brief Check if block group has a flag.
  243. * @param bg Pointer to block group
  244. * @param flag Flag to be checked
  245. * @return True if flag is set to 1
  246. */
  247. static inline bool ext4_bg_has_flag(struct ext4_bgroup *bg, uint32_t f)
  248. {
  249. return to_le16(bg->flags) & f;
  250. }
  251. /**@brief Set flag of block group.
  252. * @param bg Pointer to block group
  253. * @param flag Flag to be set
  254. */
  255. static inline void ext4_bg_set_flag(struct ext4_bgroup *bg, uint32_t f)
  256. {
  257. uint16_t flags = to_le16(bg->flags);
  258. flags |= f;
  259. bg->flags = to_le16(flags);
  260. }
  261. /**@brief Clear flag of block group.
  262. * @param bg Pointer to block group
  263. * @param flag Flag to be cleared
  264. */
  265. static inline void ext4_bg_clear_flag(struct ext4_bgroup *bg, uint32_t f)
  266. {
  267. uint16_t flags = to_le16(bg->flags);
  268. flags &= ~f;
  269. bg->flags = to_le16(flags);
  270. }
  271. /**@brief Calculate CRC16 of the block group.
  272. * @param crc Init value
  273. * @param buffer Input buffer
  274. * @param len Sizeof input buffer
  275. * @return Computed CRC16*/
  276. uint16_t ext4_bg_crc16(uint16_t crc, const uint8_t *buffer, size_t len);
  277. #endif /* EXT4_BLOCK_GROUP_H_ */
  278. /**
  279. * @}
  280. */