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