source: mainline/uspace/lib/ext4/libext4_block_group.c@ a3da2b2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a3da2b2 was 38542dc, checked in by Martin Decky <martin@…>, 13 years ago

ext4 code review and coding style cleanup

  • Property mode set to 100644
File size: 10.8 KB
Line 
1/*
2 * Copyright (c) 2012 Frantisek Princ
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libext4
30 * @{
31 */
32/**
33 * @file libext4_block_group.c
34 * @brief Ext4 block group structure operations.
35 */
36
37#include <byteorder.h>
38#include "libext4.h"
39
40/** Get address of block with data block bitmap.
41 *
42 * @param bg Pointer to block group
43 * @param sb Pointer to superblock
44 *
45 * @return Address of block with block bitmap
46 *
47 */
48uint64_t ext4_block_group_get_block_bitmap(ext4_block_group_t *bg,
49 ext4_superblock_t *sb)
50{
51 if (ext4_superblock_get_desc_size(sb) >
52 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
53 return ((uint64_t) uint32_t_le2host(bg->block_bitmap_hi) << 32) |
54 uint32_t_le2host(bg->block_bitmap_lo);
55 else
56 return uint32_t_le2host(bg->block_bitmap_lo);
57}
58
59/** Set address of block with data block bitmap.
60 *
61 * @param bg Pointer to block group
62 * @param sb Pointer to superblock
63 * @param block_bitmap Address of block with block bitmap
64 *
65 */
66void ext4_block_group_set_block_bitmap(ext4_block_group_t *bg,
67 ext4_superblock_t *sb, uint64_t block_bitmap)
68{
69 bg->block_bitmap_lo = host2uint32_t_le((block_bitmap << 32) >> 32);
70
71 if (ext4_superblock_get_desc_size(sb) >
72 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
73 bg->block_bitmap_hi = host2uint32_t_le(block_bitmap >> 32);
74}
75
76/** Get address of block with i-node bitmap.
77 *
78 * @param bg Pointer to block group
79 * @param sb Pointer to superblock
80 *
81 * @return Address of block with i-node bitmap
82 *
83 */
84uint64_t ext4_block_group_get_inode_bitmap(ext4_block_group_t *bg,
85 ext4_superblock_t *sb)
86{
87 if (ext4_superblock_get_desc_size(sb) >
88 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
89 return ((uint64_t) uint32_t_le2host(bg->inode_bitmap_hi) << 32) |
90 uint32_t_le2host(bg->inode_bitmap_lo);
91 else
92 return uint32_t_le2host(bg->inode_bitmap_lo);
93}
94
95/** Set address of block with i-node bitmap.
96 *
97 * @param bg Pointer to block group
98 * @param sb Pointer to superblock
99 * @param inode_bitmap Address of block with i-node bitmap
100 *
101 */
102void ext4_block_group_set_inode_bitmap(ext4_block_group_t *bg,
103 ext4_superblock_t *sb, uint64_t inode_bitmap)
104{
105 bg->inode_bitmap_lo = host2uint32_t_le((inode_bitmap << 32) >> 32);
106
107 if (ext4_superblock_get_desc_size(sb) >
108 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
109 bg->inode_bitmap_hi = host2uint32_t_le(inode_bitmap >> 32);
110}
111
112/** Get address of the first block of the i-node table.
113 *
114 * @param bg Pointer to block group
115 * @param sb Pointer to superblock
116 *
117 * @return Address of first block of i-node table
118 *
119 */
120uint64_t ext4_block_group_get_inode_table_first_block(ext4_block_group_t *bg,
121 ext4_superblock_t *sb)
122{
123 if (ext4_superblock_get_desc_size(sb) >
124 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
125 return ((uint64_t)
126 uint32_t_le2host(bg->inode_table_first_block_hi) << 32) |
127 uint32_t_le2host(bg->inode_table_first_block_lo);
128 else
129 return uint32_t_le2host(bg->inode_table_first_block_lo);
130}
131
132/** Set address of the first block of the i-node table.
133 *
134 * @param bg Pointer to block group
135 * @param sb Pointer to superblock
136 * @param inode_table_first Address of first block of i-node table
137 *
138 */
139void ext4_block_group_set_inode_table_first_block(ext4_block_group_t *bg,
140 ext4_superblock_t *sb, uint64_t inode_table_first)
141{
142 bg->inode_table_first_block_lo =
143 host2uint32_t_le((inode_table_first << 32) >> 32);
144
145 if (ext4_superblock_get_desc_size(sb) >
146 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
147 bg->inode_table_first_block_hi =
148 host2uint32_t_le(inode_table_first >> 32);
149}
150
151/** Get number of free blocks in block group.
152 *
153 * @param bg Pointer to block group
154 * @param sb Pointer to superblock
155 *
156 * @return Number of free blocks in block group
157 *
158 */
159uint32_t ext4_block_group_get_free_blocks_count(ext4_block_group_t *bg,
160 ext4_superblock_t *sb)
161{
162 if (ext4_superblock_get_desc_size(sb) >
163 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
164 return ((uint32_t)
165 uint16_t_le2host(bg->free_blocks_count_hi) << 16) |
166 uint16_t_le2host(bg->free_blocks_count_lo);
167 else
168 return uint16_t_le2host(bg->free_blocks_count_lo);
169}
170
171/** Set number of free blocks in block group.
172 *
173 * @param bg Pointer to block group
174 * @param sb Pointer to superblock
175 * @param value Number of free blocks in block group
176 *
177 */
178void ext4_block_group_set_free_blocks_count(ext4_block_group_t *bg,
179 ext4_superblock_t *sb, uint32_t value)
180{
181 bg->free_blocks_count_lo = host2uint16_t_le((value << 16) >> 16);
182 if (ext4_superblock_get_desc_size(sb) >
183 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
184 bg->free_blocks_count_hi = host2uint16_t_le(value >> 16);
185}
186
187/** Get number of free i-nodes in block group.
188 *
189 * @param bg Pointer to block group
190 * @param sb Pointer to superblock
191 *
192 * @return Number of free i-nodes in block group
193 *
194 */
195uint32_t ext4_block_group_get_free_inodes_count(ext4_block_group_t *bg,
196 ext4_superblock_t *sb)
197{
198 if (ext4_superblock_get_desc_size(sb) >
199 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
200 return ((uint32_t)
201 uint16_t_le2host(bg->free_inodes_count_hi) << 16) |
202 uint16_t_le2host(bg->free_inodes_count_lo);
203 else
204 return uint16_t_le2host(bg->free_inodes_count_lo);
205}
206
207/** Set number of free i-nodes in block group.
208 *
209 * @param bg Pointer to block group
210 * @param sb Pointer to superblock
211 * @param value Number of free i-nodes in block group
212 *
213 */
214void ext4_block_group_set_free_inodes_count(ext4_block_group_t *bg,
215 ext4_superblock_t *sb, uint32_t value)
216{
217 bg->free_inodes_count_lo = host2uint16_t_le((value << 16) >> 16);
218 if (ext4_superblock_get_desc_size(sb) >
219 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
220 bg->free_inodes_count_hi = host2uint16_t_le(value >> 16);
221}
222
223/** Get number of used directories in block group.
224 *
225 * @param bg Pointer to block group
226 * @param sb Pointer to superblock
227 *
228 * @return Number of used directories in block group
229 *
230 */
231uint32_t ext4_block_group_get_used_dirs_count(ext4_block_group_t *bg,
232 ext4_superblock_t *sb)
233{
234 if (ext4_superblock_get_desc_size(sb) >
235 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
236 return ((uint32_t)
237 uint16_t_le2host(bg->used_dirs_count_hi) << 16) |
238 uint16_t_le2host(bg->used_dirs_count_lo);
239 else
240 return uint16_t_le2host(bg->used_dirs_count_lo);
241}
242
243/** Set number of used directories in block group.
244 *
245 * @param bg Pointer to block group
246 * @param sb Pointer to superblock
247 * @param value Number of used directories in block group
248 *
249 */
250void ext4_block_group_set_used_dirs_count(ext4_block_group_t *bg,
251 ext4_superblock_t *sb, uint32_t count)
252{
253 bg->used_dirs_count_lo = host2uint16_t_le((count << 16) >> 16);
254 if (ext4_superblock_get_desc_size(sb) >
255 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
256 bg->used_dirs_count_hi = host2uint16_t_le(count >> 16);
257}
258
259/** Get flags of block group.
260 *
261 * @param bg Pointer to block group
262 *
263 * @return Flags of block group
264 *
265 */
266uint16_t ext4_block_group_get_flags(ext4_block_group_t *bg)
267{
268 return uint16_t_le2host(bg->flags);
269}
270
271/** Set flags for block group.
272 *
273 * @param bg Pointer to block group
274 * @param flags Flags for block group
275 *
276 */
277void ext4_block_group_set_flags(ext4_block_group_t *bg, uint16_t flags)
278{
279 bg->flags = host2uint16_t_le(flags);
280}
281
282/** Get number of unused i-nodes.
283 *
284 * @param bg Pointer to block group
285 * @param sb Pointer to superblock
286 *
287 * @return Number of unused i-nodes
288 *
289 */
290uint32_t ext4_block_group_get_itable_unused(ext4_block_group_t *bg,
291 ext4_superblock_t *sb)
292{
293 if (ext4_superblock_get_desc_size(sb) >
294 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
295 return ((uint32_t)
296 uint16_t_le2host(bg->itable_unused_hi) << 16) |
297 uint16_t_le2host(bg->itable_unused_lo);
298 else
299 return uint16_t_le2host(bg->itable_unused_lo);
300}
301
302/** Set number of unused i-nodes.
303 *
304 * @param bg Pointer to block group
305 * @param sb Pointer to superblock
306 * @param value Number of unused i-nodes
307 *
308 */
309void ext4_block_group_set_itable_unused(ext4_block_group_t *bg,
310 ext4_superblock_t *sb, uint32_t value)
311{
312 bg->itable_unused_lo = host2uint16_t_le((value << 16) >> 16);
313 if (ext4_superblock_get_desc_size(sb) >
314 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
315 bg->itable_unused_hi = host2uint16_t_le(value >> 16);
316}
317
318/** Get checksum of block group.
319 *
320 * @param bg Pointer to block group
321 *
322 * @return checksum of block group
323 *
324 */
325uint16_t ext4_block_group_get_checksum(ext4_block_group_t *bg)
326{
327 return uint16_t_le2host(bg->checksum);
328}
329
330/** Set checksum of block group.
331 *
332 * @param bg Pointer to block group
333 * @param checksum Cheksum of block group
334 *
335 */
336void ext4_block_group_set_checksum(ext4_block_group_t *bg, uint16_t checksum)
337{
338 bg->checksum = host2uint16_t_le(checksum);
339}
340
341/** Check if block group has a flag.
342 *
343 * @param bg Pointer to block group
344 * @param flag Flag to be checked
345 *
346 * @return True if flag is set to 1
347 *
348 */
349bool ext4_block_group_has_flag(ext4_block_group_t *bg, uint32_t flag)
350{
351 if (ext4_block_group_get_flags(bg) & flag)
352 return true;
353
354 return false;
355}
356
357/** Set (add) flag of block group.
358 *
359 * @param bg Pointer to block group
360 * @param flag Flag to be set
361 *
362 */
363void ext4_block_group_set_flag(ext4_block_group_t *bg, uint32_t set_flag)
364{
365 uint32_t flags = ext4_block_group_get_flags(bg);
366 flags = flags | set_flag;
367 ext4_block_group_set_flags(bg, flags);
368}
369
370/** Clear (remove) flag of block group.
371 *
372 * @param bg Pointer to block group
373 * @param flag Flag to be cleared
374 *
375 */
376void ext4_block_group_clear_flag(ext4_block_group_t *bg, uint32_t clear_flag)
377{
378 uint32_t flags = ext4_block_group_get_flags(bg);
379 flags = flags & (~clear_flag);
380 ext4_block_group_set_flags(bg, flags);
381}
382
383/**
384 * @}
385 */
Note: See TracBrowser for help on using the repository browser.