[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 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file libext4_block_group.c
|
---|
[81a7858] | 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 | *
|
---|
| 43 | * @param bg pointer to block group
|
---|
| 44 | * @param sb pointer to superblock
|
---|
| 45 | * @return address of block with block bitmap
|
---|
| 46 | */
|
---|
[fe27eb4] | 47 | uint64_t ext4_block_group_get_block_bitmap(ext4_block_group_t *bg,
|
---|
| 48 | ext4_superblock_t *sb)
|
---|
[3712434] | 49 | {
|
---|
[fe27eb4] | 50 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_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 | }
|
---|
[3712434] | 56 | }
|
---|
| 57 |
|
---|
[3d93289a] | 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 | */
|
---|
[fe27eb4] | 64 | void ext4_block_group_set_block_bitmap(ext4_block_group_t *bg,
|
---|
| 65 | ext4_superblock_t *sb, uint64_t block_bitmap)
|
---|
[3712434] | 66 | {
|
---|
[fe27eb4] | 67 | bg->block_bitmap_lo = host2uint32_t_le((block_bitmap << 32) >> 32);
|
---|
| 68 |
|
---|
| 69 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 70 | bg->block_bitmap_hi = host2uint32_t_le(block_bitmap >> 32);
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[3d93289a] | 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 | */
|
---|
[fe27eb4] | 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_BLOCK_MIN_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 |
|
---|
[3d93289a] | 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 | */
|
---|
[fe27eb4] | 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_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 104 | bg->inode_bitmap_hi = host2uint32_t_le(inode_bitmap >> 32);
|
---|
| 105 | }
|
---|
[3712434] | 106 | }
|
---|
| 107 |
|
---|
[3d93289a] | 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 | */
|
---|
[fe27eb4] | 114 | uint64_t ext4_block_group_get_inode_table_first_block(ext4_block_group_t *bg,
|
---|
| 115 | ext4_superblock_t *sb)
|
---|
[3711e7e] | 116 | {
|
---|
[fe27eb4] | 117 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_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 | }
|
---|
[3712434] | 123 | }
|
---|
| 124 |
|
---|
[3d93289a] | 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 | */
|
---|
[fe27eb4] | 131 | void ext4_block_group_set_inode_table_first_block(ext4_block_group_t *bg,
|
---|
[3d93289a] | 132 | ext4_superblock_t *sb, uint64_t inode_table_first)
|
---|
[3712434] | 133 | {
|
---|
[3d93289a] | 134 | bg->inode_table_first_block_lo =
|
---|
| 135 | host2uint32_t_le((inode_table_first << 32) >> 32);
|
---|
[fe27eb4] | 136 |
|
---|
[3d93289a] | 137 | if (ext4_superblock_get_desc_size(sb) >
|
---|
| 138 | EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 139 |
|
---|
| 140 | bg->inode_table_first_block_hi =
|
---|
| 141 | host2uint32_t_le(inode_table_first >> 32);
|
---|
[fe27eb4] | 142 | }
|
---|
[3712434] | 143 | }
|
---|
| 144 |
|
---|
[3d93289a] | 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 | */
|
---|
[fe27eb4] | 151 | uint32_t ext4_block_group_get_free_blocks_count(ext4_block_group_t *bg,
|
---|
| 152 | ext4_superblock_t *sb)
|
---|
| 153 | {
|
---|
[3d93289a] | 154 | if (ext4_superblock_get_desc_size(sb) >
|
---|
| 155 | EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 156 |
|
---|
[fe27eb4] | 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 |
|
---|
[3d93289a] | 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 | */
|
---|
[fe27eb4] | 170 | void ext4_block_group_set_free_blocks_count(ext4_block_group_t *bg,
|
---|
| 171 | ext4_superblock_t *sb, uint32_t value)
|
---|
| 172 | {
|
---|
[052e82d] | 173 | bg->free_blocks_count_lo = host2uint16_t_le((value << 16) >> 16);
|
---|
[fe27eb4] | 174 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 175 | bg->free_blocks_count_hi = host2uint16_t_le(value >> 16);
|
---|
| 176 | }
|
---|
[052e82d] | 177 | }
|
---|
| 178 |
|
---|
[3d93289a] | 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 | */
|
---|
[fe27eb4] | 185 | uint32_t ext4_block_group_get_free_inodes_count(ext4_block_group_t *bg,
|
---|
| 186 | ext4_superblock_t *sb)
|
---|
[3712434] | 187 | {
|
---|
[fe27eb4] | 188 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_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 | }
|
---|
[3712434] | 194 | }
|
---|
| 195 |
|
---|
[3d93289a] | 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 | */
|
---|
[fe27eb4] | 202 | void ext4_block_group_set_free_inodes_count(ext4_block_group_t *bg,
|
---|
| 203 | ext4_superblock_t *sb, uint32_t value)
|
---|
[3712434] | 204 | {
|
---|
[fe27eb4] | 205 | bg->free_inodes_count_lo = host2uint16_t_le((value << 16) >> 16);
|
---|
| 206 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 207 | bg->free_inodes_count_hi = host2uint16_t_le(value >> 16);
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 |
|
---|
[3d93289a] | 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 | */
|
---|
[fe27eb4] | 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_BLOCK_MIN_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 |
|
---|
[3d93289a] | 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 | */
|
---|
[fe27eb4] | 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_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 239 | bg->used_dirs_count_hi = host2uint16_t_le(count >> 16);
|
---|
| 240 | }
|
---|
[3712434] | 241 | }
|
---|
| 242 |
|
---|
[3d93289a] | 243 | /** Get flags of block group.
|
---|
| 244 | *
|
---|
| 245 | * @param bg pointer to block group
|
---|
| 246 | * @return flags of block group
|
---|
| 247 | */
|
---|
[3712434] | 248 | uint16_t ext4_block_group_get_flags(ext4_block_group_t *bg)
|
---|
| 249 | {
|
---|
| 250 | return uint16_t_le2host(bg->flags);
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[3d93289a] | 253 | /** Set flags for block group.
|
---|
| 254 | *
|
---|
| 255 | * @param bg pointer to block group
|
---|
| 256 | * @param flags flags for block group
|
---|
| 257 | */
|
---|
[81ee87cd] | 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 |
|
---|
[3d93289a] | 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 | */
|
---|
[fe27eb4] | 269 | uint32_t ext4_block_group_get_itable_unused(ext4_block_group_t *bg,
|
---|
| 270 | ext4_superblock_t *sb)
|
---|
[3712434] | 271 | {
|
---|
[fe27eb4] | 272 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_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 |
|
---|
[3d93289a] | 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 | */
|
---|
[fe27eb4] | 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_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 291 | bg->itable_unused_hi = host2uint16_t_le(value >> 16);
|
---|
| 292 | }
|
---|
| 293 |
|
---|
[3712434] | 294 | }
|
---|
| 295 |
|
---|
[3d93289a] | 296 | /** Get checksum of block group.
|
---|
| 297 | *
|
---|
| 298 | * @param bg pointer to block group
|
---|
| 299 | * @return checksum of block group
|
---|
| 300 | */
|
---|
[3712434] | 301 | uint16_t ext4_block_group_get_checksum(ext4_block_group_t *bg)
|
---|
| 302 | {
|
---|
| 303 | return uint16_t_le2host(bg->checksum);
|
---|
[3711e7e] | 304 | }
|
---|
[eb91db7] | 305 |
|
---|
[3d93289a] | 306 | /** Set checksum of block group.
|
---|
| 307 | *
|
---|
| 308 | * @param bg pointer to block group
|
---|
| 309 | * @param checksum cheksum of block group
|
---|
| 310 | */
|
---|
[fe27eb4] | 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 |
|
---|
[3d93289a] | 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 | */
|
---|
[81092ce] | 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 |
|
---|
[3d93289a] | 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 | */
|
---|
[81ee87cd] | 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 |
|
---|
[eb91db7] | 355 | /**
|
---|
| 356 | * @}
|
---|
| 357 | */
|
---|