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