[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_superblock.c
|
---|
[9875711] | 35 | * @brief Ext4 superblock operations.
|
---|
[eb91db7] | 36 | */
|
---|
| 37 |
|
---|
[01ab41b] | 38 | #include <byteorder.h>
|
---|
| 39 | #include <errno.h>
|
---|
| 40 | #include <libblock.h>
|
---|
| 41 | #include <malloc.h>
|
---|
[3711e7e] | 42 | #include "libext4.h"
|
---|
[eb91db7] | 43 |
|
---|
[3712434] | 44 | uint32_t ext4_superblock_get_inodes_count(ext4_superblock_t *sb)
|
---|
[01ab41b] | 45 | {
|
---|
[3712434] | 46 | return uint32_t_le2host(sb->inodes_count);
|
---|
[01ab41b] | 47 | }
|
---|
| 48 |
|
---|
[3712434] | 49 | uint64_t ext4_superblock_get_blocks_count(ext4_superblock_t *sb)
|
---|
| 50 | {
|
---|
| 51 | return ((uint64_t)uint32_t_le2host(sb->blocks_count_hi) << 32) |
|
---|
| 52 | uint32_t_le2host(sb->blocks_count_lo);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | uint64_t ext4_superblock_get_reserved_blocks_count(ext4_superblock_t *sb)
|
---|
| 56 | {
|
---|
| 57 | return ((uint64_t)uint32_t_le2host(sb->reserved_blocks_count_hi) << 32) |
|
---|
| 58 | uint32_t_le2host(sb->reserved_blocks_count_lo);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | uint64_t ext4_superblock_get_free_blocks_count(ext4_superblock_t *sb)
|
---|
| 62 | {
|
---|
| 63 | return ((uint64_t)uint32_t_le2host(sb->free_blocks_count_hi) << 32) |
|
---|
| 64 | uint32_t_le2host(sb->free_blocks_count_lo);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | uint32_t ext4_superblock_get_free_inodes_count(ext4_superblock_t *sb)
|
---|
| 68 | {
|
---|
| 69 | return uint32_t_le2host(sb->free_inodes_count);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | uint32_t ext4_superblock_get_first_data_block(ext4_superblock_t *sb)
|
---|
[01ab41b] | 73 | {
|
---|
[9c0c0e1] | 74 | return uint32_t_le2host(sb->first_data_block);
|
---|
[01ab41b] | 75 | }
|
---|
| 76 |
|
---|
[3712434] | 77 | uint32_t ext4_superblock_get_log_block_size(ext4_superblock_t *sb)
|
---|
[01ab41b] | 78 | {
|
---|
[9c0c0e1] | 79 | return uint32_t_le2host(sb->log_block_size);
|
---|
[01ab41b] | 80 | }
|
---|
| 81 |
|
---|
| 82 | uint32_t ext4_superblock_get_block_size(ext4_superblock_t *sb)
|
---|
| 83 | {
|
---|
[3712434] | 84 | return 1024 << ext4_superblock_get_log_block_size(sb);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 |
|
---|
| 88 | uint32_t ext4_superblock_get_blocks_per_group(ext4_superblock_t *sb)
|
---|
| 89 | {
|
---|
| 90 | return uint32_t_le2host(sb->blocks_per_group);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | uint32_t ext4_superblock_get_inodes_per_group(ext4_superblock_t *sb)
|
---|
| 94 | {
|
---|
| 95 | return uint32_t_le2host(sb->inodes_per_group);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | uint32_t ext4_superblock_get_mount_time(ext4_superblock_t *sb)
|
---|
| 99 | {
|
---|
| 100 | return uint32_t_le2host(sb->mount_time);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | uint32_t ext4_superblock_get_write_time(ext4_superblock_t *sb)
|
---|
| 104 | {
|
---|
| 105 | return uint32_t_le2host(sb->write_time);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | uint16_t ext4_superblock_get_mount_count(ext4_superblock_t *sb)
|
---|
| 109 | {
|
---|
| 110 | return uint16_t_le2host(sb->mount_count);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | uint16_t ext4_superblock_get_max_mount_count(ext4_superblock_t *sb)
|
---|
| 114 | {
|
---|
| 115 | return uint16_t_le2host(sb->max_mount_count);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | uint16_t ext4_superblock_get_magic(ext4_superblock_t *sb)
|
---|
| 119 | {
|
---|
| 120 | return uint16_t_le2host(sb->magic);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | uint16_t ext4_superblock_get_state(ext4_superblock_t *sb)
|
---|
| 124 | {
|
---|
| 125 | return uint16_t_le2host(sb->state);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | uint16_t ext4_superblock_get_errors(ext4_superblock_t *sb)
|
---|
| 129 | {
|
---|
| 130 | return uint16_t_le2host(sb->errors);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 | uint16_t ext4_superblock_get_minor_rev_level(ext4_superblock_t *sb)
|
---|
| 135 | {
|
---|
| 136 | return uint16_t_le2host(sb->minor_rev_level);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | uint32_t ext4_superblock_get_last_check_time(ext4_superblock_t *sb)
|
---|
| 140 | {
|
---|
| 141 | return uint32_t_le2host(sb->last_check_time);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | uint32_t ext4_superblock_get_check_interval(ext4_superblock_t *sb){
|
---|
| 145 | return uint32_t_le2host(sb->check_interval);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | uint32_t ext4_superblock_get_creator_os(ext4_superblock_t *sb)
|
---|
| 149 | {
|
---|
| 150 | return uint32_t_le2host(sb->creator_os);
|
---|
[01ab41b] | 151 | }
|
---|
| 152 |
|
---|
[9c0c0e1] | 153 | uint32_t ext4_superblock_get_rev_level(ext4_superblock_t *sb)
|
---|
| 154 | {
|
---|
| 155 | return uint32_t_le2host(sb->rev_level);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[3711e7e] | 158 | uint16_t ext4_superblock_get_inode_size(ext4_superblock_t *sb)
|
---|
| 159 | {
|
---|
| 160 | if (ext4_superblock_get_rev_level(sb) == 0) {
|
---|
| 161 | return EXT4_REV0_INODE_SIZE;
|
---|
| 162 | }
|
---|
| 163 | return uint16_t_le2host(sb->inode_size);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[3712434] | 166 | uint16_t ext4_superblock_get_block_group_number(ext4_superblock_t *sb)
|
---|
[3711e7e] | 167 | {
|
---|
[3712434] | 168 | return uint16_t_le2host(sb->block_group_number);
|
---|
[3711e7e] | 169 | }
|
---|
| 170 |
|
---|
[9c0c0e1] | 171 | uint32_t ext4_superblock_get_features_compatible(ext4_superblock_t *sb)
|
---|
| 172 | {
|
---|
| 173 | return uint32_t_le2host(sb->features_compatible);
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | uint32_t ext4_superblock_get_features_incompatible(ext4_superblock_t *sb)
|
---|
| 177 | {
|
---|
| 178 | return uint32_t_le2host(sb->features_incompatible);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | uint32_t ext4_superblock_get_features_read_only(ext4_superblock_t *sb)
|
---|
| 182 | {
|
---|
| 183 | return uint32_t_le2host(sb->features_read_only);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
[01ab41b] | 186 |
|
---|
[7bc4508] | 187 | uint32_t* ext4_superblock_get_hash_seed(ext4_superblock_t *sb)
|
---|
| 188 | {
|
---|
| 189 | return sb->hash_seed;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[c25e39b] | 192 | uint16_t ext4_superblock_get_desc_size(ext4_superblock_t *sb)
|
---|
| 193 | {
|
---|
| 194 | uint16_t size = uint16_t_le2host(sb->desc_size);
|
---|
| 195 |
|
---|
| 196 | if (size < EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 197 | size = EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | return size;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[7bc4508] | 203 | uint32_t ext4_superblock_get_flags(ext4_superblock_t *sb)
|
---|
| 204 | {
|
---|
| 205 | return uint32_t_le2host(sb->flags);
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 |
|
---|
[3712434] | 209 | /*
|
---|
[c25e39b] | 210 | * More complex superblock operations
|
---|
[01ab41b] | 211 | */
|
---|
[3712434] | 212 |
|
---|
[7bc4508] | 213 | bool ext4_superblock_has_flag(ext4_superblock_t *sb, uint32_t flag)
|
---|
| 214 | {
|
---|
| 215 | if (ext4_superblock_get_flags(sb) & flag) {
|
---|
| 216 | return true;
|
---|
| 217 | }
|
---|
| 218 | return false;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[c25e39b] | 221 | // Feature checkers
|
---|
| 222 | bool ext4_superblock_has_feature_compatible(ext4_superblock_t *sb, uint32_t feature)
|
---|
| 223 | {
|
---|
| 224 | if (ext4_superblock_get_features_compatible(sb) & feature) {
|
---|
| 225 | return true;
|
---|
| 226 | }
|
---|
| 227 | return false;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | bool ext4_superblock_has_feature_incompatible(ext4_superblock_t *sb, uint32_t feature)
|
---|
| 231 | {
|
---|
| 232 | if (ext4_superblock_get_features_incompatible(sb) & feature) {
|
---|
| 233 | return true;
|
---|
| 234 | }
|
---|
| 235 | return false;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | bool ext4_superblock_has_feature_read_only(ext4_superblock_t *sb, uint32_t feature)
|
---|
| 239 | {
|
---|
| 240 | if (ext4_superblock_get_features_read_only(sb) & feature) {
|
---|
| 241 | return true;
|
---|
| 242 | }
|
---|
| 243 | return false;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 |
|
---|
[01ab41b] | 247 | int ext4_superblock_read_direct(service_id_t service_id,
|
---|
| 248 | ext4_superblock_t **superblock)
|
---|
| 249 | {
|
---|
| 250 | void *data;
|
---|
| 251 | int rc;
|
---|
| 252 |
|
---|
| 253 | data = malloc(EXT4_SUPERBLOCK_SIZE);
|
---|
| 254 | if (data == NULL) {
|
---|
| 255 | return ENOMEM;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | rc = block_read_bytes_direct(service_id, EXT4_SUPERBLOCK_OFFSET,
|
---|
| 259 | EXT4_SUPERBLOCK_SIZE, data);
|
---|
| 260 |
|
---|
| 261 | if (rc != EOK) {
|
---|
| 262 | free(data);
|
---|
| 263 | return rc;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | (*superblock) = data;
|
---|
| 267 |
|
---|
| 268 | return EOK;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | int ext4_superblock_check_sanity(ext4_superblock_t *sb)
|
---|
| 272 | {
|
---|
[9c0c0e1] | 273 | if (ext4_superblock_get_magic(sb) != EXT4_SUPERBLOCK_MAGIC) {
|
---|
| 274 | return ENOTSUP;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | // TODO more checks !!!
|
---|
| 278 |
|
---|
[01ab41b] | 279 | return EOK;
|
---|
| 280 | }
|
---|
[eb91db7] | 281 |
|
---|
| 282 | /**
|
---|
| 283 | * @}
|
---|
| 284 | */
|
---|