Create default directory structure in mkfs
[lwext4.git] / lwext4 / ext4_block_group.h
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
34 /** @addtogroup lwext4
35  * @{
36  */
37 /**
38  * @file  ext4_block_group.h
39  * @brief Block group function set.
40  */
41
42 #ifndef EXT4_BLOCK_GROUP_H_
43 #define EXT4_BLOCK_GROUP_H_
44
45 #include "ext4_config.h"
46 #include "ext4_types.h"
47 #include "ext4_super.h"
48
49 #include <stdint.h>
50 #include <stdbool.h>
51
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
56  */
57 static inline uint64_t ext4_bg_get_block_bitmap(struct ext4_bgroup *bg,
58                                                 struct ext4_sblock *s)
59 {
60         uint64_t v = to_le32(bg->block_bitmap_lo);
61
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;
64
65         return v;
66 }
67
68 /**@brief Set address of block with data block bitmap.
69  * @param bg pointer to block group
70  * @param s pointer to superblock
71  * @param blk block to set
72  * @return Address of block with block bitmap
73  */
74 static inline void ext4_bg_set_block_bitmap(struct ext4_bgroup *bg,
75                                             struct ext4_sblock *s, uint64_t blk)
76 {
77
78         bg->block_bitmap_lo = to_le32(blk);
79         if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
80                 bg->block_bitmap_hi = to_le32(blk >> 32);
81
82 }
83
84 /**@brief Get address of block with i-node bitmap.
85  * @param bg Pointer to block group
86  * @param s Pointer to superblock
87  * @return Address of block with i-node bitmap
88  */
89 static inline uint64_t ext4_bg_get_inode_bitmap(struct ext4_bgroup *bg,
90                                                 struct ext4_sblock *s)
91 {
92
93         uint64_t v = to_le32(bg->inode_bitmap_lo);
94
95         if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
96                 v |= (uint64_t)to_le32(bg->inode_bitmap_hi) << 32;
97
98         return v;
99 }
100
101 /**@brief Set address of block with i-node bitmap.
102  * @param bg Pointer to block group
103  * @param s Pointer to superblock
104  * @param blk block to set
105  * @return Address of block with i-node bitmap
106  */
107 static inline void ext4_bg_set_inode_bitmap(struct ext4_bgroup *bg,
108                                             struct ext4_sblock *s, uint64_t blk)
109 {
110         bg->inode_bitmap_lo = to_le32(blk);
111         if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
112                 bg->inode_bitmap_hi = to_le32(blk >> 32);
113
114 }
115
116
117 /**@brief Get address of the first block of the i-node table.
118  * @param bg Pointer to block group
119  * @param s Pointer to superblock
120  * @return Address of first block of i-node table
121  */
122 static inline uint64_t
123 ext4_bg_get_inode_table_first_block(struct ext4_bgroup *bg,
124                                     struct ext4_sblock *s)
125 {
126         uint64_t v = to_le32(bg->inode_table_first_block_lo);
127
128         if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
129                 v |= (uint64_t)to_le32(bg->inode_table_first_block_hi) << 32;
130
131         return v;
132 }
133
134 /**@brief Set address of the first block of the i-node table.
135  * @param bg Pointer to block group
136  * @param s Pointer to superblock
137  * @param blk block to set
138  * @return Address of first block of i-node table
139  */
140 static inline void
141 ext4_bg_set_inode_table_first_block(struct ext4_bgroup *bg,
142                                     struct ext4_sblock *s, uint64_t blk)
143 {
144         bg->inode_table_first_block_lo = to_le32(blk);
145         if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
146                 bg->inode_table_first_block_hi = to_le32(blk >> 32);
147 }
148
149 /**@brief Get number of free blocks in block group.
150  * @param bg Pointer to block group
151  * @param sb Pointer to superblock
152  * @return Number of free blocks in block group
153  */
154 static inline uint32_t ext4_bg_get_free_blocks_count(struct ext4_bgroup *bg,
155                                                      struct ext4_sblock *s)
156 {
157         uint32_t v = to_le16(bg->free_blocks_count_lo);
158
159         if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
160                 v |= (uint32_t)to_le16(bg->free_blocks_count_hi) << 16;
161
162         return v;
163 }
164
165 /**@brief Set number of free blocks in block group.
166  * @param bg Pointer to block group
167  * @param s Pointer to superblock
168  * @param cnt Number of free blocks in block group
169  */
170 static inline void ext4_bg_set_free_blocks_count(struct ext4_bgroup *bg,
171                                                  struct ext4_sblock *s,
172                                                  uint32_t cnt)
173 {
174         bg->free_blocks_count_lo = to_le16((cnt << 16) >> 16);
175         if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
176                 bg->free_blocks_count_hi = to_le16(cnt >> 16);
177 }
178
179 /**@brief Get number of free i-nodes in block group.
180  * @param bg Pointer to block group
181  * @param s Pointer to superblock
182  * @return Number of free i-nodes in block group
183  */
184 static inline uint32_t ext4_bg_get_free_inodes_count(struct ext4_bgroup *bg,
185                                                      struct ext4_sblock *s)
186 {
187         uint32_t v = to_le16(bg->free_inodes_count_lo);
188
189         if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
190                 v |= (uint32_t)to_le16(bg->free_inodes_count_hi) << 16;
191
192         return v;
193 }
194
195 /**@brief Set number of free i-nodes in block group.
196  * @param bg Pointer to block group
197  * @param s Pointer to superblock
198  * @param cnt Number of free i-nodes in block group
199  */
200 static inline void ext4_bg_set_free_inodes_count(struct ext4_bgroup *bg,
201                                                  struct ext4_sblock *s,
202                                                  uint32_t cnt)
203 {
204         bg->free_inodes_count_lo = to_le16((cnt << 16) >> 16);
205         if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
206                 bg->free_inodes_count_hi = to_le16(cnt >> 16);
207 }
208
209 /**@brief Get number of used directories in block group.
210  * @param bg Pointer to block group
211  * @param s Pointer to superblock
212  * @return Number of used directories in block group
213  */
214 static inline uint32_t ext4_bg_get_used_dirs_count(struct ext4_bgroup *bg,
215                                                    struct ext4_sblock *s)
216 {
217         uint32_t v = to_le16(bg->used_dirs_count_lo);
218
219         if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
220                 v |= (uint32_t)to_le16(bg->used_dirs_count_hi) << 16;
221
222         return v;
223 }
224
225 /**@brief Set number of used directories in block group.
226  * @param bg Pointer to block group
227  * @param s Pointer to superblock
228  * @param cnt Number of used directories in block group
229  */
230 static inline void ext4_bg_set_used_dirs_count(struct ext4_bgroup *bg,
231                                                struct ext4_sblock *s,
232                                                uint32_t cnt)
233 {
234         bg->used_dirs_count_lo = to_le16((cnt << 16) >> 16);
235         if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
236                 bg->used_dirs_count_hi = to_le16(cnt >> 16);
237 }
238
239 /**@brief Get number of unused i-nodes.
240  * @param bg Pointer to block group
241  * @param s Pointer to superblock
242  * @return Number of unused i-nodes
243  */
244 static inline uint32_t ext4_bg_get_itable_unused(struct ext4_bgroup *bg,
245                                                  struct ext4_sblock *s)
246 {
247
248         uint32_t v = to_le16(bg->itable_unused_lo);
249
250         if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
251                 v |= (uint32_t)to_le16(bg->itable_unused_hi) << 16;
252
253         return v;
254 }
255
256 /**@brief Set number of unused i-nodes.
257  * @param bg Pointer to block group
258  * @param s Pointer to superblock
259  * @param cnt Number of unused i-nodes
260  */
261 static inline void ext4_bg_set_itable_unused(struct ext4_bgroup *bg,
262                                              struct ext4_sblock *s,
263                                              uint32_t cnt)
264 {
265         bg->itable_unused_lo = to_le16((cnt << 16) >> 16);
266         if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
267                 bg->itable_unused_hi = to_le16(cnt >> 16);
268 }
269
270 /**@brief  Set checksum of block group.
271  * @param bg Pointer to block group
272  * @param crc Cheksum of block group
273  */
274 static inline void ext4_bg_set_checksum(struct ext4_bgroup *bg, uint16_t crc)
275 {
276         bg->checksum = to_le16(crc);
277 }
278
279 /**@brief Check if block group has a flag.
280  * @param bg Pointer to block group
281  * @param flag Flag to be checked
282  * @return True if flag is set to 1
283  */
284 static inline bool ext4_bg_has_flag(struct ext4_bgroup *bg, uint32_t f)
285 {
286         return to_le16(bg->flags) & f;
287 }
288
289 /**@brief Set flag of block group.
290  * @param bg Pointer to block group
291  * @param flag Flag to be set
292  */
293 static inline void ext4_bg_set_flag(struct ext4_bgroup *bg, uint32_t f)
294 {
295         uint16_t flags = to_le16(bg->flags);
296         flags |= f;
297         bg->flags = to_le16(flags);
298 }
299
300 /**@brief Clear flag of block group.
301  * @param bg Pointer to block group
302  * @param flag Flag to be cleared
303  */
304 static inline void ext4_bg_clear_flag(struct ext4_bgroup *bg, uint32_t f)
305 {
306         uint16_t flags = to_le16(bg->flags);
307         flags &= ~f;
308         bg->flags = to_le16(flags);
309 }
310
311 /**@brief Calculate CRC16 of the block group.
312  * @param crc Init value
313  * @param buffer Input buffer
314  * @param len Sizeof input buffer
315  * @return Computed CRC16*/
316 uint16_t ext4_bg_crc16(uint16_t crc, const uint8_t *buffer, size_t len);
317
318 #endif /* EXT4_BLOCK_GROUP_H_ */
319
320 /**
321  * @}
322  */