[eb91db7] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 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
|
---|
[c25e39b] | 35 | * @brief Ext4 block group structure operations
|
---|
[eb91db7] | 36 | */
|
---|
| 37 |
|
---|
[3711e7e] | 38 | #include <byteorder.h>
|
---|
| 39 | #include "libext4.h"
|
---|
[eb91db7] | 40 |
|
---|
[fe27eb4] | 41 | uint64_t ext4_block_group_get_block_bitmap(ext4_block_group_t *bg,
|
---|
| 42 | ext4_superblock_t *sb)
|
---|
[3712434] | 43 | {
|
---|
[fe27eb4] | 44 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 45 | return ((uint64_t)uint32_t_le2host(bg->block_bitmap_hi) << 32) |
|
---|
| 46 | uint32_t_le2host(bg->block_bitmap_lo);
|
---|
| 47 | } else {
|
---|
| 48 | return uint32_t_le2host(bg->block_bitmap_lo);
|
---|
| 49 | }
|
---|
[3712434] | 50 | }
|
---|
| 51 |
|
---|
[fe27eb4] | 52 | void ext4_block_group_set_block_bitmap(ext4_block_group_t *bg,
|
---|
| 53 | ext4_superblock_t *sb, uint64_t block_bitmap)
|
---|
[3712434] | 54 | {
|
---|
[fe27eb4] | 55 | bg->block_bitmap_lo = host2uint32_t_le((block_bitmap << 32) >> 32);
|
---|
| 56 |
|
---|
| 57 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 58 | bg->block_bitmap_hi = host2uint32_t_le(block_bitmap >> 32);
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | uint64_t ext4_block_group_get_inode_bitmap(ext4_block_group_t *bg,
|
---|
| 63 | ext4_superblock_t *sb)
|
---|
| 64 | {
|
---|
| 65 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 66 | return ((uint64_t)uint32_t_le2host(bg->inode_bitmap_hi) << 32) |
|
---|
| 67 | uint32_t_le2host(bg->inode_bitmap_lo);
|
---|
| 68 | } else {
|
---|
| 69 | return uint32_t_le2host(bg->inode_bitmap_lo);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | void ext4_block_group_set_inode_bitmap(ext4_block_group_t *bg,
|
---|
| 75 | ext4_superblock_t *sb, uint64_t inode_bitmap)
|
---|
| 76 | {
|
---|
| 77 | bg->inode_bitmap_lo = host2uint32_t_le((inode_bitmap << 32) >> 32);
|
---|
| 78 |
|
---|
| 79 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 80 | bg->inode_bitmap_hi = host2uint32_t_le(inode_bitmap >> 32);
|
---|
| 81 | }
|
---|
[3712434] | 82 | }
|
---|
| 83 |
|
---|
[fe27eb4] | 84 | uint64_t ext4_block_group_get_inode_table_first_block(ext4_block_group_t *bg,
|
---|
| 85 | ext4_superblock_t *sb)
|
---|
[3711e7e] | 86 | {
|
---|
[fe27eb4] | 87 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 88 | return ((uint64_t)uint32_t_le2host(bg->inode_table_first_block_hi) << 32) |
|
---|
| 89 | uint32_t_le2host(bg->inode_table_first_block_lo);
|
---|
| 90 | } else {
|
---|
| 91 | return uint32_t_le2host(bg->inode_table_first_block_lo);
|
---|
| 92 | }
|
---|
[3712434] | 93 | }
|
---|
| 94 |
|
---|
[fe27eb4] | 95 | void ext4_block_group_set_inode_table_first_block(ext4_block_group_t *bg,
|
---|
| 96 | ext4_superblock_t *sb, uint64_t ino_tbl_first)
|
---|
[3712434] | 97 | {
|
---|
[fe27eb4] | 98 | bg->inode_table_first_block_lo = host2uint32_t_le((ino_tbl_first << 32) >> 32);
|
---|
| 99 |
|
---|
| 100 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 101 | bg->inode_table_first_block_hi = host2uint32_t_le(ino_tbl_first >> 32);
|
---|
| 102 | }
|
---|
[3712434] | 103 | }
|
---|
| 104 |
|
---|
[fe27eb4] | 105 | uint32_t ext4_block_group_get_free_blocks_count(ext4_block_group_t *bg,
|
---|
| 106 | ext4_superblock_t *sb)
|
---|
| 107 | {
|
---|
| 108 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 109 | return ((uint32_t)uint16_t_le2host(bg->free_blocks_count_hi) << 16) |
|
---|
| 110 | uint16_t_le2host(bg->free_blocks_count_lo);
|
---|
| 111 | } else {
|
---|
| 112 | return uint16_t_le2host(bg->free_blocks_count_lo);
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | void ext4_block_group_set_free_blocks_count(ext4_block_group_t *bg,
|
---|
| 117 | ext4_superblock_t *sb, uint32_t value)
|
---|
| 118 | {
|
---|
[052e82d] | 119 | bg->free_blocks_count_lo = host2uint16_t_le((value << 16) >> 16);
|
---|
[fe27eb4] | 120 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 121 | bg->free_blocks_count_hi = host2uint16_t_le(value >> 16);
|
---|
| 122 | }
|
---|
[052e82d] | 123 | }
|
---|
| 124 |
|
---|
[fe27eb4] | 125 | uint32_t ext4_block_group_get_free_inodes_count(ext4_block_group_t *bg,
|
---|
| 126 | ext4_superblock_t *sb)
|
---|
[3712434] | 127 | {
|
---|
[fe27eb4] | 128 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 129 | return ((uint32_t)uint16_t_le2host(bg->free_inodes_count_hi) << 16) |
|
---|
| 130 | uint16_t_le2host(bg->free_inodes_count_lo);
|
---|
| 131 | } else {
|
---|
| 132 | return uint16_t_le2host(bg->free_inodes_count_lo);
|
---|
| 133 | }
|
---|
[3712434] | 134 | }
|
---|
| 135 |
|
---|
[fe27eb4] | 136 | void ext4_block_group_set_free_inodes_count(ext4_block_group_t *bg,
|
---|
| 137 | ext4_superblock_t *sb, uint32_t value)
|
---|
[3712434] | 138 | {
|
---|
[fe27eb4] | 139 | bg->free_inodes_count_lo = host2uint16_t_le((value << 16) >> 16);
|
---|
| 140 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 141 | bg->free_inodes_count_hi = host2uint16_t_le(value >> 16);
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 |
|
---|
| 146 | uint32_t ext4_block_group_get_used_dirs_count(ext4_block_group_t *bg,
|
---|
| 147 | ext4_superblock_t *sb)
|
---|
| 148 | {
|
---|
| 149 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 150 | return ((uint32_t)uint16_t_le2host(bg->used_dirs_count_hi) << 16) |
|
---|
| 151 | uint16_t_le2host(bg->used_dirs_count_lo);
|
---|
| 152 | } else {
|
---|
| 153 | return uint16_t_le2host(bg->used_dirs_count_lo);
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | void ext4_block_group_set_used_dirs_count(ext4_block_group_t *bg,
|
---|
| 158 | ext4_superblock_t *sb, uint32_t count)
|
---|
| 159 | {
|
---|
| 160 | bg->used_dirs_count_lo = host2uint16_t_le((count << 16) >> 16);
|
---|
| 161 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 162 | bg->used_dirs_count_hi = host2uint16_t_le(count >> 16);
|
---|
| 163 | }
|
---|
[3712434] | 164 | }
|
---|
| 165 |
|
---|
| 166 | uint16_t ext4_block_group_get_flags(ext4_block_group_t *bg)
|
---|
| 167 | {
|
---|
| 168 | return uint16_t_le2host(bg->flags);
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[fe27eb4] | 171 | uint32_t ext4_block_group_get_itable_unused(ext4_block_group_t *bg,
|
---|
| 172 | ext4_superblock_t *sb)
|
---|
[3712434] | 173 | {
|
---|
[fe27eb4] | 174 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 175 | return ((uint32_t)uint16_t_le2host(bg->itable_unused_hi) << 16) |
|
---|
| 176 | uint16_t_le2host(bg->itable_unused_lo);
|
---|
| 177 | } else {
|
---|
| 178 | return uint16_t_le2host(bg->itable_unused_lo);
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | void ext4_block_group_set_itable_unused(ext4_block_group_t *bg,
|
---|
| 183 | ext4_superblock_t *sb, uint32_t value)
|
---|
| 184 | {
|
---|
| 185 | bg->itable_unused_lo = host2uint16_t_le((value << 16) >> 16);
|
---|
| 186 | if (ext4_superblock_get_desc_size(sb) > EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 187 | bg->itable_unused_hi = host2uint16_t_le(value >> 16);
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[3712434] | 190 | }
|
---|
| 191 |
|
---|
| 192 | uint16_t ext4_block_group_get_checksum(ext4_block_group_t *bg)
|
---|
| 193 | {
|
---|
| 194 | return uint16_t_le2host(bg->checksum);
|
---|
[3711e7e] | 195 | }
|
---|
[eb91db7] | 196 |
|
---|
[fe27eb4] | 197 | void ext4_block_group_set_checksum(ext4_block_group_t *bg, uint16_t checksum)
|
---|
| 198 | {
|
---|
| 199 | bg->checksum = host2uint16_t_le(checksum);
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[eb91db7] | 202 | /**
|
---|
| 203 | * @}
|
---|
| 204 | */
|
---|