2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
6 * Copyright (c) 2012 Martin Sucha
7 * Copyright (c) 2012 Frantisek Princ
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
34 /** @addtogroup lwext4
38 * @file ext4_block_group.h
39 * @brief Block group function set.
42 #ifndef EXT4_BLOCK_GROUP_H_
43 #define EXT4_BLOCK_GROUP_H_
45 #include <ext4_config.h>
46 #include <ext4_types.h>
47 #include <ext4_super.h>
52 /**@brief Get address of block with data block bitmap.
53 * @param bg pointer to block group
54 * @param s pointer to superblock
55 * @return Address of block with block bitmap
57 static inline uint64_t ext4_bg_get_block_bitmap(struct ext4_bgroup *bg,
58 struct ext4_sblock *s)
60 uint64_t v = to_le32(bg->block_bitmap_lo);
62 if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
63 v |= (uint64_t)to_le32(bg->block_bitmap_hi) << 32;
68 /**@brief Get address of block with i-node bitmap.
69 * @param bg Pointer to block group
70 * @param s Pointer to superblock
71 * @return Address of block with i-node bitmap
73 static inline uint64_t ext4_bg_get_inode_bitmap(struct ext4_bgroup *bg,
74 struct ext4_sblock *s)
77 uint64_t v = to_le32(bg->inode_bitmap_lo);
79 if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
80 v |= (uint64_t)to_le32(bg->inode_bitmap_hi) << 32;
85 /**@brief Get address of the first block of the i-node table.
86 * @param bg Pointer to block group
87 * @param s Pointer to superblock
88 * @return Address of first block of i-node table
90 static inline uint64_t
91 ext4_bg_get_inode_table_first_block(struct ext4_bgroup *bg,
92 struct ext4_sblock *s)
94 uint64_t v = to_le32(bg->inode_table_first_block_lo);
96 if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
97 v |= (uint64_t)to_le32(bg->inode_table_first_block_hi) << 32;
102 /**@brief Get number of free blocks in block group.
103 * @param bg Pointer to block group
104 * @param sb Pointer to superblock
105 * @return Number of free blocks in block group
107 static inline uint32_t ext4_bg_get_free_blocks_count(struct ext4_bgroup *bg,
108 struct ext4_sblock *s)
110 uint32_t v = to_le16(bg->free_blocks_count_lo);
112 if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
113 v |= (uint32_t)to_le16(bg->free_blocks_count_hi) << 16;
118 /**@brief Set number of free blocks in block group.
119 * @param bg Pointer to block group
120 * @param s Pointer to superblock
121 * @param cnt Number of free blocks in block group
123 static inline void ext4_bg_set_free_blocks_count(struct ext4_bgroup *bg,
124 struct ext4_sblock *s,
127 bg->free_blocks_count_lo = to_le16((cnt << 16) >> 16);
128 if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
129 bg->free_blocks_count_hi = to_le16(cnt >> 16);
132 /**@brief Get number of free i-nodes in block group.
133 * @param bg Pointer to block group
134 * @param s Pointer to superblock
135 * @return Number of free i-nodes in block group
137 static inline uint32_t ext4_bg_get_free_inodes_count(struct ext4_bgroup *bg,
138 struct ext4_sblock *s)
140 uint32_t v = to_le16(bg->free_inodes_count_lo);
142 if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
143 v |= (uint32_t)to_le16(bg->free_inodes_count_hi) << 16;
148 /**@brief Set number of free i-nodes in block group.
149 * @param bg Pointer to block group
150 * @param s Pointer to superblock
151 * @param cnt Number of free i-nodes in block group
153 static inline void ext4_bg_set_free_inodes_count(struct ext4_bgroup *bg,
154 struct ext4_sblock *s,
157 bg->free_inodes_count_lo = to_le16((cnt << 16) >> 16);
158 if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
159 bg->free_inodes_count_hi = to_le16(cnt >> 16);
162 /**@brief Get number of used directories in block group.
163 * @param bg Pointer to block group
164 * @param s Pointer to superblock
165 * @return Number of used directories in block group
167 static inline uint32_t ext4_bg_get_used_dirs_count(struct ext4_bgroup *bg,
168 struct ext4_sblock *s)
170 uint32_t v = to_le16(bg->used_dirs_count_lo);
172 if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
173 v |= (uint32_t)to_le16(bg->used_dirs_count_hi) << 16;
178 /**@brief Set number of used directories in block group.
179 * @param bg Pointer to block group
180 * @param s Pointer to superblock
181 * @param cnt Number of used directories in block group
183 static inline void ext4_bg_set_used_dirs_count(struct ext4_bgroup *bg,
184 struct ext4_sblock *s,
187 bg->used_dirs_count_lo = to_le16((cnt << 16) >> 16);
188 if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
189 bg->used_dirs_count_hi = to_le16(cnt >> 16);
192 /**@brief Get number of unused i-nodes.
193 * @param bg Pointer to block group
194 * @param s Pointer to superblock
195 * @return Number of unused i-nodes
197 static inline uint32_t ext4_bg_get_itable_unused(struct ext4_bgroup *bg,
198 struct ext4_sblock *s)
201 uint32_t v = to_le16(bg->itable_unused_lo);
203 if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
204 v |= (uint32_t)to_le16(bg->itable_unused_hi) << 16;
209 /**@brief Set number of unused i-nodes.
210 * @param bg Pointer to block group
211 * @param s Pointer to superblock
212 * @param cnt Number of unused i-nodes
214 static inline void ext4_bg_set_itable_unused(struct ext4_bgroup *bg,
215 struct ext4_sblock *s,
218 bg->itable_unused_lo = to_le16((cnt << 16) >> 16);
219 if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
220 bg->itable_unused_hi = to_le16(cnt >> 16);
223 /**@brief Set checksum of block group.
224 * @param bg Pointer to block group
225 * @param crc Cheksum of block group
227 static inline void ext4_bg_set_checksum(struct ext4_bgroup *bg, uint16_t crc)
229 bg->checksum = to_le16(crc);
232 /**@brief Check if block group has a flag.
233 * @param bg Pointer to block group
234 * @param flag Flag to be checked
235 * @return True if flag is set to 1
237 static inline bool ext4_bg_has_flag(struct ext4_bgroup *bg, uint32_t f)
239 return to_le16(bg->flags) & f;
242 /**@brief Set flag of block group.
243 * @param bg Pointer to block group
244 * @param flag Flag to be set
246 static inline void ext4_bg_set_flag(struct ext4_bgroup *bg, uint32_t f)
248 uint16_t flags = to_le16(bg->flags);
250 bg->flags = to_le16(flags);
253 /**@brief Clear flag of block group.
254 * @param bg Pointer to block group
255 * @param flag Flag to be cleared
257 static inline void ext4_bg_clear_flag(struct ext4_bgroup *bg, uint32_t f)
259 uint16_t flags = to_le16(bg->flags);
261 bg->flags = to_le16(flags);
264 /**@brief Calculate CRC16 of the block group.
265 * @param crc Init value
266 * @param buffer Input buffer
267 * @param len Sizeof input buffer
268 * @return Computed CRC16*/
269 uint16_t ext4_bg_crc16(uint16_t crc, const uint8_t *buffer, size_t len);
271 #endif /* EXT4_BLOCK_GROUP_H_ */