source: mainline/uspace/lib/ext4/src/block_group.c@ 3061bc1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3061bc1 was 4bfad34, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Move parts of ext4 filesystem initialization.

  • 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/**
[4bfad34]34 * @file block_group.c
[38542dc]35 * @brief Ext4 block group structure operations.
[eb91db7]36 */
37
[3711e7e]38#include <byteorder.h>
[fcb0d76]39#include "ext4/block_group.h"
40#include "ext4/superblock.h"
[eb91db7]41
[3d93289a]42/** Get address of block with data block bitmap.
43 *
[38542dc]44 * @param bg Pointer to block group
45 * @param sb Pointer to superblock
46 *
47 * @return Address of block with block bitmap
48 *
[3d93289a]49 */
[fe27eb4]50uint64_t ext4_block_group_get_block_bitmap(ext4_block_group_t *bg,
[38542dc]51 ext4_superblock_t *sb)
[3712434]52{
[38542dc]53 if (ext4_superblock_get_desc_size(sb) >
54 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
55 return ((uint64_t) uint32_t_le2host(bg->block_bitmap_hi) << 32) |
56 uint32_t_le2host(bg->block_bitmap_lo);
57 else
[fe27eb4]58 return uint32_t_le2host(bg->block_bitmap_lo);
[3712434]59}
60
[3d93289a]61/** Set address of block with data block bitmap.
62 *
[38542dc]63 * @param bg Pointer to block group
64 * @param sb Pointer to superblock
65 * @param block_bitmap Address of block with block bitmap
66 *
[3d93289a]67 */
[fe27eb4]68void ext4_block_group_set_block_bitmap(ext4_block_group_t *bg,
[38542dc]69 ext4_superblock_t *sb, uint64_t block_bitmap)
[3712434]70{
[fe27eb4]71 bg->block_bitmap_lo = host2uint32_t_le((block_bitmap << 32) >> 32);
[38542dc]72
73 if (ext4_superblock_get_desc_size(sb) >
74 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
[fe27eb4]75 bg->block_bitmap_hi = host2uint32_t_le(block_bitmap >> 32);
76}
77
[3d93289a]78/** Get address of block with i-node bitmap.
79 *
[38542dc]80 * @param bg Pointer to block group
81 * @param sb Pointer to superblock
82 *
83 * @return Address of block with i-node bitmap
84 *
[3d93289a]85 */
[fe27eb4]86uint64_t ext4_block_group_get_inode_bitmap(ext4_block_group_t *bg,
[38542dc]87 ext4_superblock_t *sb)
[fe27eb4]88{
[38542dc]89 if (ext4_superblock_get_desc_size(sb) >
90 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
91 return ((uint64_t) uint32_t_le2host(bg->inode_bitmap_hi) << 32) |
92 uint32_t_le2host(bg->inode_bitmap_lo);
93 else
[fe27eb4]94 return uint32_t_le2host(bg->inode_bitmap_lo);
95}
96
[3d93289a]97/** Set address of block with i-node bitmap.
98 *
[38542dc]99 * @param bg Pointer to block group
100 * @param sb Pointer to superblock
101 * @param inode_bitmap Address of block with i-node bitmap
102 *
[3d93289a]103 */
[fe27eb4]104void ext4_block_group_set_inode_bitmap(ext4_block_group_t *bg,
[38542dc]105 ext4_superblock_t *sb, uint64_t inode_bitmap)
[fe27eb4]106{
107 bg->inode_bitmap_lo = host2uint32_t_le((inode_bitmap << 32) >> 32);
[38542dc]108
109 if (ext4_superblock_get_desc_size(sb) >
110 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
[fe27eb4]111 bg->inode_bitmap_hi = host2uint32_t_le(inode_bitmap >> 32);
[3712434]112}
113
[3d93289a]114/** Get address of the first block of the i-node table.
115 *
[38542dc]116 * @param bg Pointer to block group
117 * @param sb Pointer to superblock
118 *
119 * @return Address of first block of i-node table
120 *
[3d93289a]121 */
[fe27eb4]122uint64_t ext4_block_group_get_inode_table_first_block(ext4_block_group_t *bg,
[38542dc]123 ext4_superblock_t *sb)
[3711e7e]124{
[38542dc]125 if (ext4_superblock_get_desc_size(sb) >
126 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
127 return ((uint64_t)
128 uint32_t_le2host(bg->inode_table_first_block_hi) << 32) |
129 uint32_t_le2host(bg->inode_table_first_block_lo);
130 else
[fe27eb4]131 return uint32_t_le2host(bg->inode_table_first_block_lo);
[3712434]132}
133
[3d93289a]134/** Set address of the first block of the i-node table.
135 *
[38542dc]136 * @param bg Pointer to block group
137 * @param sb Pointer to superblock
138 * @param inode_table_first Address of first block of i-node table
139 *
[3d93289a]140 */
[fe27eb4]141void ext4_block_group_set_inode_table_first_block(ext4_block_group_t *bg,
[38542dc]142 ext4_superblock_t *sb, uint64_t inode_table_first)
[3712434]143{
[3d93289a]144 bg->inode_table_first_block_lo =
[38542dc]145 host2uint32_t_le((inode_table_first << 32) >> 32);
146
[3d93289a]147 if (ext4_superblock_get_desc_size(sb) >
[38542dc]148 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
[3d93289a]149 bg->inode_table_first_block_hi =
[38542dc]150 host2uint32_t_le(inode_table_first >> 32);
[3712434]151}
152
[3d93289a]153/** Get number of free blocks in block group.
154 *
[38542dc]155 * @param bg Pointer to block group
156 * @param sb Pointer to superblock
157 *
158 * @return Number of free blocks in block group
159 *
[3d93289a]160 */
[fe27eb4]161uint32_t ext4_block_group_get_free_blocks_count(ext4_block_group_t *bg,
[38542dc]162 ext4_superblock_t *sb)
[fe27eb4]163{
[3d93289a]164 if (ext4_superblock_get_desc_size(sb) >
[38542dc]165 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
166 return ((uint32_t)
167 uint16_t_le2host(bg->free_blocks_count_hi) << 16) |
168 uint16_t_le2host(bg->free_blocks_count_lo);
169 else
[fe27eb4]170 return uint16_t_le2host(bg->free_blocks_count_lo);
171}
172
[3d93289a]173/** Set number of free blocks in block group.
174 *
[38542dc]175 * @param bg Pointer to block group
176 * @param sb Pointer to superblock
177 * @param value Number of free blocks in block group
178 *
[3d93289a]179 */
[fe27eb4]180void ext4_block_group_set_free_blocks_count(ext4_block_group_t *bg,
[38542dc]181 ext4_superblock_t *sb, uint32_t value)
[fe27eb4]182{
[052e82d]183 bg->free_blocks_count_lo = host2uint16_t_le((value << 16) >> 16);
[38542dc]184 if (ext4_superblock_get_desc_size(sb) >
185 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
[fe27eb4]186 bg->free_blocks_count_hi = host2uint16_t_le(value >> 16);
[052e82d]187}
188
[3d93289a]189/** Get number of free i-nodes in block group.
190 *
[38542dc]191 * @param bg Pointer to block group
192 * @param sb Pointer to superblock
193 *
194 * @return Number of free i-nodes in block group
195 *
[3d93289a]196 */
[fe27eb4]197uint32_t ext4_block_group_get_free_inodes_count(ext4_block_group_t *bg,
[38542dc]198 ext4_superblock_t *sb)
[3712434]199{
[38542dc]200 if (ext4_superblock_get_desc_size(sb) >
201 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
202 return ((uint32_t)
203 uint16_t_le2host(bg->free_inodes_count_hi) << 16) |
204 uint16_t_le2host(bg->free_inodes_count_lo);
205 else
[fe27eb4]206 return uint16_t_le2host(bg->free_inodes_count_lo);
[3712434]207}
208
[3d93289a]209/** Set number of free i-nodes in block group.
210 *
[38542dc]211 * @param bg Pointer to block group
212 * @param sb Pointer to superblock
213 * @param value Number of free i-nodes in block group
214 *
[3d93289a]215 */
[fe27eb4]216void ext4_block_group_set_free_inodes_count(ext4_block_group_t *bg,
[38542dc]217 ext4_superblock_t *sb, uint32_t value)
[3712434]218{
[fe27eb4]219 bg->free_inodes_count_lo = host2uint16_t_le((value << 16) >> 16);
[38542dc]220 if (ext4_superblock_get_desc_size(sb) >
221 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
[fe27eb4]222 bg->free_inodes_count_hi = host2uint16_t_le(value >> 16);
223}
224
[3d93289a]225/** Get number of used directories in block group.
226 *
[38542dc]227 * @param bg Pointer to block group
228 * @param sb Pointer to superblock
229 *
230 * @return Number of used directories in block group
231 *
[3d93289a]232 */
[fe27eb4]233uint32_t ext4_block_group_get_used_dirs_count(ext4_block_group_t *bg,
[38542dc]234 ext4_superblock_t *sb)
[fe27eb4]235{
[38542dc]236 if (ext4_superblock_get_desc_size(sb) >
237 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
238 return ((uint32_t)
239 uint16_t_le2host(bg->used_dirs_count_hi) << 16) |
240 uint16_t_le2host(bg->used_dirs_count_lo);
241 else
[fe27eb4]242 return uint16_t_le2host(bg->used_dirs_count_lo);
243}
244
[3d93289a]245/** Set number of used directories in block group.
246 *
[38542dc]247 * @param bg Pointer to block group
248 * @param sb Pointer to superblock
249 * @param value Number of used directories in block group
250 *
[3d93289a]251 */
[fe27eb4]252void ext4_block_group_set_used_dirs_count(ext4_block_group_t *bg,
[38542dc]253 ext4_superblock_t *sb, uint32_t count)
[fe27eb4]254{
255 bg->used_dirs_count_lo = host2uint16_t_le((count << 16) >> 16);
[38542dc]256 if (ext4_superblock_get_desc_size(sb) >
257 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
[fe27eb4]258 bg->used_dirs_count_hi = host2uint16_t_le(count >> 16);
[3712434]259}
260
[3d93289a]261/** Get flags of block group.
262 *
[38542dc]263 * @param bg Pointer to block group
264 *
265 * @return Flags of block group
266 *
[3d93289a]267 */
[3712434]268uint16_t ext4_block_group_get_flags(ext4_block_group_t *bg)
269{
270 return uint16_t_le2host(bg->flags);
271}
272
[3d93289a]273/** Set flags for block group.
274 *
[38542dc]275 * @param bg Pointer to block group
276 * @param flags Flags for block group
277 *
[3d93289a]278 */
[81ee87cd]279void ext4_block_group_set_flags(ext4_block_group_t *bg, uint16_t flags)
280{
281 bg->flags = host2uint16_t_le(flags);
282}
283
[3d93289a]284/** Get number of unused i-nodes.
285 *
[38542dc]286 * @param bg Pointer to block group
287 * @param sb Pointer to superblock
288 *
289 * @return Number of unused i-nodes
290 *
[3d93289a]291 */
[fe27eb4]292uint32_t ext4_block_group_get_itable_unused(ext4_block_group_t *bg,
[38542dc]293 ext4_superblock_t *sb)
[3712434]294{
[38542dc]295 if (ext4_superblock_get_desc_size(sb) >
296 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
297 return ((uint32_t)
298 uint16_t_le2host(bg->itable_unused_hi) << 16) |
299 uint16_t_le2host(bg->itable_unused_lo);
300 else
[fe27eb4]301 return uint16_t_le2host(bg->itable_unused_lo);
302}
303
[3d93289a]304/** Set number of unused i-nodes.
305 *
[38542dc]306 * @param bg Pointer to block group
307 * @param sb Pointer to superblock
308 * @param value Number of unused i-nodes
309 *
[3d93289a]310 */
[fe27eb4]311void ext4_block_group_set_itable_unused(ext4_block_group_t *bg,
[38542dc]312 ext4_superblock_t *sb, uint32_t value)
[fe27eb4]313{
314 bg->itable_unused_lo = host2uint16_t_le((value << 16) >> 16);
[38542dc]315 if (ext4_superblock_get_desc_size(sb) >
316 EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
[fe27eb4]317 bg->itable_unused_hi = host2uint16_t_le(value >> 16);
[3712434]318}
319
[3d93289a]320/** Get checksum of block group.
321 *
[38542dc]322 * @param bg Pointer to block group
323 *
324 * @return checksum of block group
325 *
[3d93289a]326 */
[3712434]327uint16_t ext4_block_group_get_checksum(ext4_block_group_t *bg)
328{
329 return uint16_t_le2host(bg->checksum);
[3711e7e]330}
[eb91db7]331
[3d93289a]332/** Set checksum of block group.
333 *
[38542dc]334 * @param bg Pointer to block group
335 * @param checksum Cheksum of block group
336 *
[3d93289a]337 */
[fe27eb4]338void ext4_block_group_set_checksum(ext4_block_group_t *bg, uint16_t checksum)
339{
340 bg->checksum = host2uint16_t_le(checksum);
341}
342
[3d93289a]343/** Check if block group has a flag.
344 *
[38542dc]345 * @param bg Pointer to block group
346 * @param flag Flag to be checked
347 *
348 * @return True if flag is set to 1
349 *
[3d93289a]350 */
[81092ce]351bool ext4_block_group_has_flag(ext4_block_group_t *bg, uint32_t flag)
352{
[38542dc]353 if (ext4_block_group_get_flags(bg) & flag)
[81092ce]354 return true;
[38542dc]355
[81092ce]356 return false;
357}
358
[3d93289a]359/** Set (add) flag of block group.
360 *
[38542dc]361 * @param bg Pointer to block group
362 * @param flag Flag to be set
363 *
[3d93289a]364 */
365void ext4_block_group_set_flag(ext4_block_group_t *bg, uint32_t set_flag)
366{
367 uint32_t flags = ext4_block_group_get_flags(bg);
368 flags = flags | set_flag;
369 ext4_block_group_set_flags(bg, flags);
370}
371
372/** Clear (remove) flag of block group.
373 *
[38542dc]374 * @param bg Pointer to block group
375 * @param flag Flag to be cleared
376 *
[3d93289a]377 */
[81ee87cd]378void ext4_block_group_clear_flag(ext4_block_group_t *bg, uint32_t clear_flag)
379{
380 uint32_t flags = ext4_block_group_get_flags(bg);
381 flags = flags & (~clear_flag);
382 ext4_block_group_set_flags(bg, flags);
383}
384
[eb91db7]385/**
386 * @}
[38542dc]387 */
Note: See TracBrowser for help on using the repository browser.