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

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

Add missing copyright headers to ext4

Those files are based on ext2 filesystem driver code.

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