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
RevLine 
[eb91db7]1/*
[f22d5ef0]2 * Copyright (c) 2012 Frantisek Princ
[eb91db7]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 * @{
[38542dc]31 */
[eb91db7]32/**
[38542dc]33 * @file libext4_block_group.c
34 * @brief Ext4 block group structure operations.
[eb91db7]35 */
36
[3711e7e]37#include <byteorder.h>
38#include "libext4.h"
[eb91db7]39
[3d93289a]40/** Get address of block with data block bitmap.
41 *
[38542dc]42 * @param bg Pointer to block group
43 * @param sb Pointer to superblock
44 *
45 * @return Address of block with block bitmap
46 *
[3d93289a]47 */
[fe27eb4]48uint64_t ext4_block_group_get_block_bitmap(ext4_block_group_t *bg,
[38542dc]49 ext4_superblock_t *sb)
[3712434]50{
[38542dc]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
[fe27eb4]56 return uint32_t_le2host(bg->block_bitmap_lo);
[3712434]57}
58
[3d93289a]59/** Set address of block with data block bitmap.
60 *
[38542dc]61 * @param bg Pointer to block group
62 * @param sb Pointer to superblock
63 * @param block_bitmap Address of block with block bitmap
64 *
[3d93289a]65 */
[fe27eb4]66void ext4_block_group_set_block_bitmap(ext4_block_group_t *bg,
[38542dc]67 ext4_superblock_t *sb, uint64_t block_bitmap)
[3712434]68{
[fe27eb4]69 bg->block_bitmap_lo = host2uint32_t_le((block_bitmap << 32) >> 32);
[38542dc]70
71 if (ext4_superblock_get_desc_size(sb) >
72 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
[fe27eb4]73 bg->block_bitmap_hi = host2uint32_t_le(block_bitmap >> 32);
74}
75
[3d93289a]76/** Get address of block with i-node bitmap.
77 *
[38542dc]78 * @param bg Pointer to block group
79 * @param sb Pointer to superblock
80 *
81 * @return Address of block with i-node bitmap
82 *
[3d93289a]83 */
[fe27eb4]84uint64_t ext4_block_group_get_inode_bitmap(ext4_block_group_t *bg,
[38542dc]85 ext4_superblock_t *sb)
[fe27eb4]86{
[38542dc]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
[fe27eb4]92 return uint32_t_le2host(bg->inode_bitmap_lo);
93}
94
[3d93289a]95/** Set address of block with i-node bitmap.
96 *
[38542dc]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 *
[3d93289a]101 */
[fe27eb4]102void ext4_block_group_set_inode_bitmap(ext4_block_group_t *bg,
[38542dc]103 ext4_superblock_t *sb, uint64_t inode_bitmap)
[fe27eb4]104{
105 bg->inode_bitmap_lo = host2uint32_t_le((inode_bitmap << 32) >> 32);
[38542dc]106
107 if (ext4_superblock_get_desc_size(sb) >
108 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
[fe27eb4]109 bg->inode_bitmap_hi = host2uint32_t_le(inode_bitmap >> 32);
[3712434]110}
111
[3d93289a]112/** Get address of the first block of the i-node table.
113 *
[38542dc]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 *
[3d93289a]119 */
[fe27eb4]120uint64_t ext4_block_group_get_inode_table_first_block(ext4_block_group_t *bg,
[38542dc]121 ext4_superblock_t *sb)
[3711e7e]122{
[38542dc]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
[fe27eb4]129 return uint32_t_le2host(bg->inode_table_first_block_lo);
[3712434]130}
131
[3d93289a]132/** Set address of the first block of the i-node table.
133 *
[38542dc]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 *
[3d93289a]138 */
[fe27eb4]139void ext4_block_group_set_inode_table_first_block(ext4_block_group_t *bg,
[38542dc]140 ext4_superblock_t *sb, uint64_t inode_table_first)
[3712434]141{
[3d93289a]142 bg->inode_table_first_block_lo =
[38542dc]143 host2uint32_t_le((inode_table_first << 32) >> 32);
144
[3d93289a]145 if (ext4_superblock_get_desc_size(sb) >
[38542dc]146 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
[3d93289a]147 bg->inode_table_first_block_hi =
[38542dc]148 host2uint32_t_le(inode_table_first >> 32);
[3712434]149}
150
[3d93289a]151/** Get number of free blocks in block group.
152 *
[38542dc]153 * @param bg Pointer to block group
154 * @param sb Pointer to superblock
155 *
156 * @return Number of free blocks in block group
157 *
[3d93289a]158 */
[fe27eb4]159uint32_t ext4_block_group_get_free_blocks_count(ext4_block_group_t *bg,
[38542dc]160 ext4_superblock_t *sb)
[fe27eb4]161{
[3d93289a]162 if (ext4_superblock_get_desc_size(sb) >
[38542dc]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
[fe27eb4]168 return uint16_t_le2host(bg->free_blocks_count_lo);
169}
170
[3d93289a]171/** Set number of free blocks in block group.
172 *
[38542dc]173 * @param bg Pointer to block group
174 * @param sb Pointer to superblock
175 * @param value Number of free blocks in block group
176 *
[3d93289a]177 */
[fe27eb4]178void ext4_block_group_set_free_blocks_count(ext4_block_group_t *bg,
[38542dc]179 ext4_superblock_t *sb, uint32_t value)
[fe27eb4]180{
[052e82d]181 bg->free_blocks_count_lo = host2uint16_t_le((value << 16) >> 16);
[38542dc]182 if (ext4_superblock_get_desc_size(sb) >
183 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
[fe27eb4]184 bg->free_blocks_count_hi = host2uint16_t_le(value >> 16);
[052e82d]185}
186
[3d93289a]187/** Get number of free i-nodes in block group.
188 *
[38542dc]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 *
[3d93289a]194 */
[fe27eb4]195uint32_t ext4_block_group_get_free_inodes_count(ext4_block_group_t *bg,
[38542dc]196 ext4_superblock_t *sb)
[3712434]197{
[38542dc]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
[fe27eb4]204 return uint16_t_le2host(bg->free_inodes_count_lo);
[3712434]205}
206
[3d93289a]207/** Set number of free i-nodes in block group.
208 *
[38542dc]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 *
[3d93289a]213 */
[fe27eb4]214void ext4_block_group_set_free_inodes_count(ext4_block_group_t *bg,
[38542dc]215 ext4_superblock_t *sb, uint32_t value)
[3712434]216{
[fe27eb4]217 bg->free_inodes_count_lo = host2uint16_t_le((value << 16) >> 16);
[38542dc]218 if (ext4_superblock_get_desc_size(sb) >
219 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
[fe27eb4]220 bg->free_inodes_count_hi = host2uint16_t_le(value >> 16);
221}
222
[3d93289a]223/** Get number of used directories in block group.
224 *
[38542dc]225 * @param bg Pointer to block group
226 * @param sb Pointer to superblock
227 *
228 * @return Number of used directories in block group
229 *
[3d93289a]230 */
[fe27eb4]231uint32_t ext4_block_group_get_used_dirs_count(ext4_block_group_t *bg,
[38542dc]232 ext4_superblock_t *sb)
[fe27eb4]233{
[38542dc]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
[fe27eb4]240 return uint16_t_le2host(bg->used_dirs_count_lo);
241}
242
[3d93289a]243/** Set number of used directories in block group.
244 *
[38542dc]245 * @param bg Pointer to block group
246 * @param sb Pointer to superblock
247 * @param value Number of used directories in block group
248 *
[3d93289a]249 */
[fe27eb4]250void ext4_block_group_set_used_dirs_count(ext4_block_group_t *bg,
[38542dc]251 ext4_superblock_t *sb, uint32_t count)
[fe27eb4]252{
253 bg->used_dirs_count_lo = host2uint16_t_le((count << 16) >> 16);
[38542dc]254 if (ext4_superblock_get_desc_size(sb) >
255 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
[fe27eb4]256 bg->used_dirs_count_hi = host2uint16_t_le(count >> 16);
[3712434]257}
258
[3d93289a]259/** Get flags of block group.
260 *
[38542dc]261 * @param bg Pointer to block group
262 *
263 * @return Flags of block group
264 *
[3d93289a]265 */
[3712434]266uint16_t ext4_block_group_get_flags(ext4_block_group_t *bg)
267{
268 return uint16_t_le2host(bg->flags);
269}
270
[3d93289a]271/** Set flags for block group.
272 *
[38542dc]273 * @param bg Pointer to block group
274 * @param flags Flags for block group
275 *
[3d93289a]276 */
[81ee87cd]277void ext4_block_group_set_flags(ext4_block_group_t *bg, uint16_t flags)
278{
279 bg->flags = host2uint16_t_le(flags);
280}
281
[3d93289a]282/** Get number of unused i-nodes.
283 *
[38542dc]284 * @param bg Pointer to block group
285 * @param sb Pointer to superblock
286 *
287 * @return Number of unused i-nodes
288 *
[3d93289a]289 */
[fe27eb4]290uint32_t ext4_block_group_get_itable_unused(ext4_block_group_t *bg,
[38542dc]291 ext4_superblock_t *sb)
[3712434]292{
[38542dc]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
[fe27eb4]299 return uint16_t_le2host(bg->itable_unused_lo);
300}
301
[3d93289a]302/** Set number of unused i-nodes.
303 *
[38542dc]304 * @param bg Pointer to block group
305 * @param sb Pointer to superblock
306 * @param value Number of unused i-nodes
307 *
[3d93289a]308 */
[fe27eb4]309void ext4_block_group_set_itable_unused(ext4_block_group_t *bg,
[38542dc]310 ext4_superblock_t *sb, uint32_t value)
[fe27eb4]311{
312 bg->itable_unused_lo = host2uint16_t_le((value << 16) >> 16);
[38542dc]313 if (ext4_superblock_get_desc_size(sb) >
314 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
[fe27eb4]315 bg->itable_unused_hi = host2uint16_t_le(value >> 16);
[3712434]316}
317
[3d93289a]318/** Get checksum of block group.
319 *
[38542dc]320 * @param bg Pointer to block group
321 *
322 * @return checksum of block group
323 *
[3d93289a]324 */
[3712434]325uint16_t ext4_block_group_get_checksum(ext4_block_group_t *bg)
326{
327 return uint16_t_le2host(bg->checksum);
[3711e7e]328}
[eb91db7]329
[3d93289a]330/** Set checksum of block group.
331 *
[38542dc]332 * @param bg Pointer to block group
333 * @param checksum Cheksum of block group
334 *
[3d93289a]335 */
[fe27eb4]336void ext4_block_group_set_checksum(ext4_block_group_t *bg, uint16_t checksum)
337{
338 bg->checksum = host2uint16_t_le(checksum);
339}
340
[3d93289a]341/** Check if block group has a flag.
342 *
[38542dc]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 *
[3d93289a]348 */
[81092ce]349bool ext4_block_group_has_flag(ext4_block_group_t *bg, uint32_t flag)
350{
[38542dc]351 if (ext4_block_group_get_flags(bg) & flag)
[81092ce]352 return true;
[38542dc]353
[81092ce]354 return false;
355}
356
[3d93289a]357/** Set (add) flag of block group.
358 *
[38542dc]359 * @param bg Pointer to block group
360 * @param flag Flag to be set
361 *
[3d93289a]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 *
[38542dc]372 * @param bg Pointer to block group
373 * @param flag Flag to be cleared
374 *
[3d93289a]375 */
[81ee87cd]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
[eb91db7]383/**
384 * @}
[38542dc]385 */
Note: See TracBrowser for help on using the repository browser.