[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_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 |
|
---|
[fe27eb4] | 49 | void ext4_superblock_set_inodes_count(ext4_superblock_t *sb, uint32_t count)
|
---|
| 50 | {
|
---|
| 51 | sb->inodes_count = host2uint32_t_le(count);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[3712434] | 54 | uint64_t ext4_superblock_get_blocks_count(ext4_superblock_t *sb)
|
---|
| 55 | {
|
---|
| 56 | return ((uint64_t)uint32_t_le2host(sb->blocks_count_hi) << 32) |
|
---|
| 57 | uint32_t_le2host(sb->blocks_count_lo);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[fe27eb4] | 60 | void ext4_superblock_set_blocks_count(ext4_superblock_t *sb, uint64_t count)
|
---|
| 61 | {
|
---|
| 62 | sb->blocks_count_lo = host2uint32_t_le((count << 32) >> 32);
|
---|
| 63 | sb->blocks_count_hi = host2uint32_t_le(count >> 32);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[3712434] | 66 | uint64_t ext4_superblock_get_reserved_blocks_count(ext4_superblock_t *sb)
|
---|
| 67 | {
|
---|
| 68 | return ((uint64_t)uint32_t_le2host(sb->reserved_blocks_count_hi) << 32) |
|
---|
| 69 | uint32_t_le2host(sb->reserved_blocks_count_lo);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[fe27eb4] | 72 | void ext4_superblock_set_reserved_blocks_count(ext4_superblock_t *sb, uint64_t count)
|
---|
| 73 | {
|
---|
| 74 | sb->reserved_blocks_count_lo = host2uint32_t_le((count << 32) >> 32);
|
---|
| 75 | sb->reserved_blocks_count_hi = host2uint32_t_le(count >> 32);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[3712434] | 78 | uint64_t ext4_superblock_get_free_blocks_count(ext4_superblock_t *sb)
|
---|
| 79 | {
|
---|
| 80 | return ((uint64_t)uint32_t_le2host(sb->free_blocks_count_hi) << 32) |
|
---|
| 81 | uint32_t_le2host(sb->free_blocks_count_lo);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[ae3d4f8] | 84 | void ext4_superblock_set_free_blocks_count(ext4_superblock_t *sb, uint64_t count)
|
---|
| 85 | {
|
---|
| 86 | sb->free_blocks_count_lo = host2uint32_t_le((count << 32) >> 32);
|
---|
| 87 | sb->free_blocks_count_hi = host2uint32_t_le(count >> 32);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[3712434] | 90 | uint32_t ext4_superblock_get_free_inodes_count(ext4_superblock_t *sb)
|
---|
| 91 | {
|
---|
| 92 | return uint32_t_le2host(sb->free_inodes_count);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[fe27eb4] | 95 | void ext4_superblock_set_free_inodes_count(ext4_superblock_t *sb, uint32_t count)
|
---|
| 96 | {
|
---|
| 97 | sb->free_inodes_count = host2uint32_t_le(count);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[3712434] | 100 | uint32_t ext4_superblock_get_first_data_block(ext4_superblock_t *sb)
|
---|
[01ab41b] | 101 | {
|
---|
[9c0c0e1] | 102 | return uint32_t_le2host(sb->first_data_block);
|
---|
[01ab41b] | 103 | }
|
---|
| 104 |
|
---|
[fe27eb4] | 105 | void ext4_superblock_set_first_data_block(ext4_superblock_t *sb, uint32_t first)
|
---|
| 106 | {
|
---|
| 107 | sb->first_data_block = host2uint32_t_le(first);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[3712434] | 110 | uint32_t ext4_superblock_get_log_block_size(ext4_superblock_t *sb)
|
---|
[01ab41b] | 111 | {
|
---|
[9c0c0e1] | 112 | return uint32_t_le2host(sb->log_block_size);
|
---|
[01ab41b] | 113 | }
|
---|
| 114 |
|
---|
[fe27eb4] | 115 | void ext4_superblock_set_log_block_size(ext4_superblock_t *sb, uint32_t log_size)
|
---|
| 116 | {
|
---|
| 117 | sb->log_block_size = host2uint32_t_le(log_size);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[01ab41b] | 120 | uint32_t ext4_superblock_get_block_size(ext4_superblock_t *sb)
|
---|
| 121 | {
|
---|
[3712434] | 122 | return 1024 << ext4_superblock_get_log_block_size(sb);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[fe27eb4] | 125 | void ext4_superblock_set_block_size(ext4_superblock_t *sb, uint32_t size)
|
---|
| 126 | {
|
---|
| 127 | uint32_t log = 0;
|
---|
| 128 | uint32_t tmp = size / EXT4_MIN_BLOCK_SIZE;
|
---|
| 129 |
|
---|
| 130 | tmp >>= 1;
|
---|
| 131 | while (tmp) {
|
---|
| 132 | log++;
|
---|
| 133 | tmp >>= 1;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | ext4_superblock_set_log_block_size(sb, log);
|
---|
| 137 | }
|
---|
[3712434] | 138 |
|
---|
| 139 | uint32_t ext4_superblock_get_blocks_per_group(ext4_superblock_t *sb)
|
---|
| 140 | {
|
---|
| 141 | return uint32_t_le2host(sb->blocks_per_group);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[fe27eb4] | 144 | void ext4_superblock_set_blocks_per_group(ext4_superblock_t *sb, uint32_t blocks)
|
---|
| 145 | {
|
---|
| 146 | sb->blocks_per_group = host2uint32_t_le(blocks);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[3712434] | 149 | uint32_t ext4_superblock_get_inodes_per_group(ext4_superblock_t *sb)
|
---|
| 150 | {
|
---|
| 151 | return uint32_t_le2host(sb->inodes_per_group);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[fe27eb4] | 154 | void ext4_superblock_set_inodes_per_group(ext4_superblock_t *sb, uint32_t inodes)
|
---|
| 155 | {
|
---|
| 156 | sb->inodes_per_group = host2uint32_t_le(inodes);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[3712434] | 159 | uint32_t ext4_superblock_get_mount_time(ext4_superblock_t *sb)
|
---|
| 160 | {
|
---|
| 161 | return uint32_t_le2host(sb->mount_time);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[fe27eb4] | 164 | void ext4_superblock_set_mount_time(ext4_superblock_t *sb, uint32_t time)
|
---|
| 165 | {
|
---|
| 166 | sb->mount_time = host2uint32_t_le(time);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[3712434] | 169 | uint32_t ext4_superblock_get_write_time(ext4_superblock_t *sb)
|
---|
| 170 | {
|
---|
| 171 | return uint32_t_le2host(sb->write_time);
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[fe27eb4] | 174 | void ext4_superblock_set_write_time(ext4_superblock_t *sb, uint32_t time)
|
---|
| 175 | {
|
---|
| 176 | sb->write_time = host2uint32_t_le(time);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[3712434] | 179 | uint16_t ext4_superblock_get_mount_count(ext4_superblock_t *sb)
|
---|
| 180 | {
|
---|
| 181 | return uint16_t_le2host(sb->mount_count);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[fe27eb4] | 184 | void ext4_superblock_set_mount_count(ext4_superblock_t *sb, uint16_t count)
|
---|
| 185 | {
|
---|
| 186 | sb->mount_count = host2uint16_t_le(count);
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[3712434] | 189 | uint16_t ext4_superblock_get_max_mount_count(ext4_superblock_t *sb)
|
---|
| 190 | {
|
---|
| 191 | return uint16_t_le2host(sb->max_mount_count);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
[fe27eb4] | 194 | void ext4_superblock_set_max_mount_count(ext4_superblock_t *sb, uint16_t count)
|
---|
| 195 | {
|
---|
| 196 | sb->max_mount_count = host2uint16_t_le(count);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[3712434] | 199 | uint16_t ext4_superblock_get_magic(ext4_superblock_t *sb)
|
---|
| 200 | {
|
---|
| 201 | return uint16_t_le2host(sb->magic);
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | uint16_t ext4_superblock_get_state(ext4_superblock_t *sb)
|
---|
| 205 | {
|
---|
| 206 | return uint16_t_le2host(sb->state);
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[fe27eb4] | 209 | void ext4_superblock_set_state(ext4_superblock_t *sb, uint16_t state)
|
---|
| 210 | {
|
---|
| 211 | sb->state = host2uint16_t_le(state);
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[3712434] | 214 | uint16_t ext4_superblock_get_errors(ext4_superblock_t *sb)
|
---|
| 215 | {
|
---|
| 216 | return uint16_t_le2host(sb->errors);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[fe27eb4] | 219 | void ext4_superblock_set_errors(ext4_superblock_t *sb, uint16_t errors)
|
---|
| 220 | {
|
---|
| 221 | sb->errors = host2uint16_t_le(errors);
|
---|
| 222 | }
|
---|
[3712434] | 223 |
|
---|
| 224 | uint16_t ext4_superblock_get_minor_rev_level(ext4_superblock_t *sb)
|
---|
| 225 | {
|
---|
| 226 | return uint16_t_le2host(sb->minor_rev_level);
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[fe27eb4] | 229 | void ext4_superblock_set_minor_rev_level(ext4_superblock_t *sb, uint16_t level)
|
---|
| 230 | {
|
---|
| 231 | sb->minor_rev_level = host2uint16_t_le(level);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[3712434] | 234 | uint32_t ext4_superblock_get_last_check_time(ext4_superblock_t *sb)
|
---|
| 235 | {
|
---|
| 236 | return uint32_t_le2host(sb->last_check_time);
|
---|
| 237 | }
|
---|
| 238 |
|
---|
[fe27eb4] | 239 | void ext4_superblock_set_last_check_time(ext4_superblock_t *sb, uint32_t time)
|
---|
| 240 | {
|
---|
| 241 | sb->state = host2uint32_t_le(time);
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[3712434] | 244 | uint32_t ext4_superblock_get_check_interval(ext4_superblock_t *sb){
|
---|
| 245 | return uint32_t_le2host(sb->check_interval);
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[fe27eb4] | 248 | void ext4_superblock_set_check_interval(ext4_superblock_t *sb, uint32_t interval)
|
---|
| 249 | {
|
---|
| 250 | sb->check_interval = host2uint32_t_le(interval);
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[3712434] | 253 | uint32_t ext4_superblock_get_creator_os(ext4_superblock_t *sb)
|
---|
| 254 | {
|
---|
| 255 | return uint32_t_le2host(sb->creator_os);
|
---|
[01ab41b] | 256 | }
|
---|
| 257 |
|
---|
[fe27eb4] | 258 | void ext4_superblock_set_creator_os(ext4_superblock_t *sb, uint32_t os)
|
---|
| 259 | {
|
---|
| 260 | sb->creator_os = host2uint32_t_le(os);
|
---|
| 261 | }
|
---|
| 262 |
|
---|
[9c0c0e1] | 263 | uint32_t ext4_superblock_get_rev_level(ext4_superblock_t *sb)
|
---|
| 264 | {
|
---|
| 265 | return uint32_t_le2host(sb->rev_level);
|
---|
| 266 | }
|
---|
| 267 |
|
---|
[fe27eb4] | 268 | void ext4_superblock_set_rev_level(ext4_superblock_t *sb, uint32_t level)
|
---|
| 269 | {
|
---|
| 270 | sb->rev_level = host2uint32_t_le(level);
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | uint16_t ext4_superblock_get_def_resuid(ext4_superblock_t *sb)
|
---|
| 274 | {
|
---|
| 275 | return uint16_t_le2host(sb->def_resuid);
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | void ext4_superblock_set_def_resuid(ext4_superblock_t *sb, uint16_t uid)
|
---|
| 279 | {
|
---|
| 280 | sb->def_resuid = host2uint16_t_le(uid);
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | uint16_t ext4_superblock_get_def_resgid(ext4_superblock_t *sb)
|
---|
| 284 | {
|
---|
| 285 | return uint16_t_le2host(sb->def_resgid);
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | void ext4_superblock_set_def_resgid(ext4_superblock_t *sb, uint16_t gid)
|
---|
| 289 | {
|
---|
| 290 | sb->def_resgid = host2uint16_t_le(gid);
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | uint32_t ext4_superblock_get_first_inode(ext4_superblock_t *sb)
|
---|
| 294 | {
|
---|
| 295 | return uint32_t_le2host(sb->first_inode);
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | void ext4_superblock_set_first_inode(ext4_superblock_t *sb, uint32_t first_inode)
|
---|
| 299 | {
|
---|
| 300 | sb->first_inode = host2uint32_t_le(first_inode);
|
---|
| 301 | }
|
---|
| 302 |
|
---|
[3711e7e] | 303 | uint16_t ext4_superblock_get_inode_size(ext4_superblock_t *sb)
|
---|
| 304 | {
|
---|
| 305 | if (ext4_superblock_get_rev_level(sb) == 0) {
|
---|
| 306 | return EXT4_REV0_INODE_SIZE;
|
---|
| 307 | }
|
---|
| 308 | return uint16_t_le2host(sb->inode_size);
|
---|
| 309 | }
|
---|
| 310 |
|
---|
[fe27eb4] | 311 | void ext4_superblock_set_inode_size(ext4_superblock_t *sb, uint16_t size)
|
---|
| 312 | {
|
---|
| 313 | sb->inode_size = host2uint16_t_le(size);
|
---|
| 314 | }
|
---|
| 315 |
|
---|
[3712434] | 316 | uint16_t ext4_superblock_get_block_group_number(ext4_superblock_t *sb)
|
---|
[3711e7e] | 317 | {
|
---|
[3712434] | 318 | return uint16_t_le2host(sb->block_group_number);
|
---|
[3711e7e] | 319 | }
|
---|
| 320 |
|
---|
[fe27eb4] | 321 | void ext4_superblock_set_block_group_number(ext4_superblock_t *sb, uint16_t bg)
|
---|
| 322 | {
|
---|
| 323 | sb->block_group_number = host2uint16_t_le(bg);
|
---|
| 324 | }
|
---|
| 325 |
|
---|
[9c0c0e1] | 326 | uint32_t ext4_superblock_get_features_compatible(ext4_superblock_t *sb)
|
---|
| 327 | {
|
---|
| 328 | return uint32_t_le2host(sb->features_compatible);
|
---|
| 329 | }
|
---|
| 330 |
|
---|
[fe27eb4] | 331 | void ext4_superblock_set_features_compatible(ext4_superblock_t *sb, uint32_t features)
|
---|
| 332 | {
|
---|
| 333 | sb->features_compatible = host2uint32_t_le(features);
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[9c0c0e1] | 336 | uint32_t ext4_superblock_get_features_incompatible(ext4_superblock_t *sb)
|
---|
| 337 | {
|
---|
| 338 | return uint32_t_le2host(sb->features_incompatible);
|
---|
| 339 | }
|
---|
| 340 |
|
---|
[fe27eb4] | 341 | void ext4_superblock_set_features_incompatible(ext4_superblock_t *sb, uint32_t features)
|
---|
| 342 | {
|
---|
| 343 | sb->features_incompatible = host2uint32_t_le(features);
|
---|
| 344 | }
|
---|
| 345 |
|
---|
[9c0c0e1] | 346 | uint32_t ext4_superblock_get_features_read_only(ext4_superblock_t *sb)
|
---|
| 347 | {
|
---|
| 348 | return uint32_t_le2host(sb->features_read_only);
|
---|
| 349 | }
|
---|
| 350 |
|
---|
[fe27eb4] | 351 | void ext4_superblock_set_features_read_only(ext4_superblock_t *sb, uint32_t features)
|
---|
| 352 | {
|
---|
| 353 | sb->features_read_only = host2uint32_t_le(features);
|
---|
| 354 | }
|
---|
[01ab41b] | 355 |
|
---|
[291af81] | 356 | const uint8_t * ext4_superblock_get_uuid(ext4_superblock_t *sb)
|
---|
| 357 | {
|
---|
| 358 | return sb->uuid;
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 | void ext4_superblock_set_uuid(ext4_superblock_t *sb, const uint8_t *uuid)
|
---|
| 362 | {
|
---|
| 363 | memcpy(sb->uuid, uuid, sizeof(sb->uuid));
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | const char * ext4_superblock_get_volume_name(ext4_superblock_t *sb)
|
---|
| 367 | {
|
---|
| 368 | return sb->volume_name;
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | void ext4_superblock_set_volume_name(ext4_superblock_t *sb, const char *name)
|
---|
| 372 | {
|
---|
| 373 | memcpy(sb->volume_name, name, sizeof(sb->volume_name));
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 | const char * ext4_superblock_get_last_mounted(ext4_superblock_t *sb)
|
---|
| 377 | {
|
---|
| 378 | return sb->last_mounted;
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | void ext4_superblock_set_last_mounted(ext4_superblock_t *sb, const char *last)
|
---|
| 382 | {
|
---|
| 383 | memcpy(sb->last_mounted, last, sizeof(sb->last_mounted));
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 |
|
---|
[ebcaff4] | 387 | uint32_t ext4_superblock_get_last_orphan(ext4_superblock_t *sb)
|
---|
| 388 | {
|
---|
| 389 | return uint32_t_le2host(sb->last_orphan);
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | void ext4_superblock_set_last_orphan(ext4_superblock_t *sb, uint32_t last_orphan)
|
---|
| 393 | {
|
---|
| 394 | sb->last_orphan = host2uint32_t_le(last_orphan);
|
---|
| 395 | }
|
---|
| 396 |
|
---|
[7bc4508] | 397 | uint32_t* ext4_superblock_get_hash_seed(ext4_superblock_t *sb)
|
---|
| 398 | {
|
---|
| 399 | return sb->hash_seed;
|
---|
| 400 | }
|
---|
| 401 |
|
---|
[7eb033ce] | 402 | uint8_t ext4_superblock_get_default_hash_version(ext4_superblock_t *sb)
|
---|
| 403 | {
|
---|
| 404 | return sb->default_hash_version;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | void ext4_superblock_set_default_hash_version(ext4_superblock_t *sb, uint8_t version)
|
---|
| 408 | {
|
---|
| 409 | sb->default_hash_version = version;
|
---|
| 410 | }
|
---|
| 411 |
|
---|
[c25e39b] | 412 | uint16_t ext4_superblock_get_desc_size(ext4_superblock_t *sb)
|
---|
| 413 | {
|
---|
| 414 | uint16_t size = uint16_t_le2host(sb->desc_size);
|
---|
| 415 |
|
---|
| 416 | if (size < EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 417 | size = EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE;
|
---|
| 418 | }
|
---|
| 419 |
|
---|
| 420 | return size;
|
---|
| 421 | }
|
---|
| 422 |
|
---|
[fe27eb4] | 423 | void ext4_superblock_set_desc_size(ext4_superblock_t *sb, uint16_t size)
|
---|
| 424 | {
|
---|
| 425 | sb->desc_size = host2uint16_t_le(size);
|
---|
| 426 | }
|
---|
| 427 |
|
---|
[7bc4508] | 428 | uint32_t ext4_superblock_get_flags(ext4_superblock_t *sb)
|
---|
| 429 | {
|
---|
| 430 | return uint32_t_le2host(sb->flags);
|
---|
| 431 | }
|
---|
| 432 |
|
---|
[fe27eb4] | 433 | void ext4_superblock_set_flags(ext4_superblock_t *sb, uint32_t flags)
|
---|
| 434 | {
|
---|
| 435 | sb->flags = host2uint32_t_le(flags);
|
---|
| 436 | }
|
---|
| 437 |
|
---|
[7bc4508] | 438 |
|
---|
[3712434] | 439 | /*
|
---|
[c25e39b] | 440 | * More complex superblock operations
|
---|
[01ab41b] | 441 | */
|
---|
[3712434] | 442 |
|
---|
[7bc4508] | 443 | bool ext4_superblock_has_flag(ext4_superblock_t *sb, uint32_t flag)
|
---|
| 444 | {
|
---|
| 445 | if (ext4_superblock_get_flags(sb) & flag) {
|
---|
| 446 | return true;
|
---|
| 447 | }
|
---|
| 448 | return false;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
[c25e39b] | 451 | // Feature checkers
|
---|
| 452 | bool ext4_superblock_has_feature_compatible(ext4_superblock_t *sb, uint32_t feature)
|
---|
| 453 | {
|
---|
| 454 | if (ext4_superblock_get_features_compatible(sb) & feature) {
|
---|
| 455 | return true;
|
---|
| 456 | }
|
---|
| 457 | return false;
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | bool ext4_superblock_has_feature_incompatible(ext4_superblock_t *sb, uint32_t feature)
|
---|
| 461 | {
|
---|
| 462 | if (ext4_superblock_get_features_incompatible(sb) & feature) {
|
---|
| 463 | return true;
|
---|
| 464 | }
|
---|
| 465 | return false;
|
---|
| 466 | }
|
---|
| 467 |
|
---|
| 468 | bool ext4_superblock_has_feature_read_only(ext4_superblock_t *sb, uint32_t feature)
|
---|
| 469 | {
|
---|
| 470 | if (ext4_superblock_get_features_read_only(sb) & feature) {
|
---|
| 471 | return true;
|
---|
| 472 | }
|
---|
| 473 | return false;
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 |
|
---|
[01ab41b] | 477 | int ext4_superblock_read_direct(service_id_t service_id,
|
---|
| 478 | ext4_superblock_t **superblock)
|
---|
| 479 | {
|
---|
| 480 | int rc;
|
---|
| 481 |
|
---|
[d9bbe45] | 482 | void *data = malloc(EXT4_SUPERBLOCK_SIZE);
|
---|
[01ab41b] | 483 | if (data == NULL) {
|
---|
| 484 | return ENOMEM;
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | rc = block_read_bytes_direct(service_id, EXT4_SUPERBLOCK_OFFSET,
|
---|
| 488 | EXT4_SUPERBLOCK_SIZE, data);
|
---|
| 489 |
|
---|
| 490 | if (rc != EOK) {
|
---|
| 491 | free(data);
|
---|
| 492 | return rc;
|
---|
| 493 | }
|
---|
| 494 |
|
---|
| 495 | (*superblock) = data;
|
---|
| 496 |
|
---|
| 497 | return EOK;
|
---|
| 498 | }
|
---|
| 499 |
|
---|
[ae3d4f8] | 500 | int ext4_superblock_write_direct(service_id_t service_id,
|
---|
| 501 | ext4_superblock_t *sb)
|
---|
| 502 | {
|
---|
| 503 | int rc;
|
---|
| 504 | uint32_t phys_block_size;
|
---|
| 505 |
|
---|
| 506 | rc = block_get_bsize(service_id, &phys_block_size);
|
---|
| 507 | if (rc != EOK) {
|
---|
| 508 | // TODO error
|
---|
| 509 | return rc;
|
---|
| 510 | }
|
---|
| 511 |
|
---|
[d9bbe45] | 512 | uint64_t first_block = EXT4_SUPERBLOCK_OFFSET / phys_block_size;
|
---|
| 513 | uint32_t block_count = EXT4_SUPERBLOCK_SIZE / phys_block_size;
|
---|
[ae3d4f8] | 514 |
|
---|
| 515 | if (EXT4_SUPERBLOCK_SIZE % phys_block_size) {
|
---|
| 516 | block_count++;
|
---|
| 517 | }
|
---|
| 518 |
|
---|
| 519 | return block_write_direct(service_id, first_block, block_count, sb);
|
---|
| 520 |
|
---|
| 521 | }
|
---|
| 522 |
|
---|
| 523 |
|
---|
[01ab41b] | 524 | int ext4_superblock_check_sanity(ext4_superblock_t *sb)
|
---|
| 525 | {
|
---|
[9c0c0e1] | 526 | if (ext4_superblock_get_magic(sb) != EXT4_SUPERBLOCK_MAGIC) {
|
---|
| 527 | return ENOTSUP;
|
---|
| 528 | }
|
---|
| 529 |
|
---|
[fe27eb4] | 530 | // block size
|
---|
| 531 | // desc size
|
---|
| 532 |
|
---|
| 533 |
|
---|
[9c0c0e1] | 534 | // TODO more checks !!!
|
---|
| 535 |
|
---|
[01ab41b] | 536 | return EOK;
|
---|
| 537 | }
|
---|
[eb91db7] | 538 |
|
---|
[b12ca16] | 539 | uint32_t ext4_superblock_get_block_group_count(ext4_superblock_t *sb)
|
---|
| 540 | {
|
---|
| 541 | uint64_t blocks_count = ext4_superblock_get_blocks_count(sb);
|
---|
| 542 | uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
|
---|
| 543 |
|
---|
| 544 | uint32_t block_groups_count = blocks_count / blocks_per_group;
|
---|
| 545 |
|
---|
| 546 | if (blocks_count % blocks_per_group) {
|
---|
| 547 | block_groups_count++;
|
---|
| 548 | }
|
---|
| 549 |
|
---|
| 550 | return block_groups_count;
|
---|
| 551 |
|
---|
| 552 | }
|
---|
| 553 |
|
---|
| 554 | uint32_t ext4_superblock_get_blocks_in_group(ext4_superblock_t *sb, uint32_t bgid)
|
---|
| 555 | {
|
---|
| 556 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
---|
| 557 | uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
|
---|
| 558 | uint64_t total_blocks = ext4_superblock_get_blocks_count(sb);
|
---|
| 559 |
|
---|
| 560 | if (bgid < block_group_count - 1) {
|
---|
| 561 | return blocks_per_group;
|
---|
| 562 | } else {
|
---|
| 563 | return (total_blocks - ((block_group_count - 1) * blocks_per_group));
|
---|
| 564 | }
|
---|
| 565 |
|
---|
| 566 | }
|
---|
| 567 |
|
---|
| 568 | uint32_t ext4_superblock_get_inodes_in_group(ext4_superblock_t *sb, uint32_t bgid)
|
---|
| 569 | {
|
---|
| 570 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
---|
| 571 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
| 572 | uint32_t total_inodes = ext4_superblock_get_inodes_count(sb);
|
---|
| 573 |
|
---|
| 574 | if (bgid < block_group_count - 1) {
|
---|
| 575 | return inodes_per_group;
|
---|
| 576 | } else {
|
---|
| 577 | return (total_inodes - ((block_group_count - 1) * inodes_per_group));
|
---|
| 578 | }
|
---|
| 579 |
|
---|
| 580 | }
|
---|
| 581 |
|
---|
[eb91db7] | 582 | /**
|
---|
| 583 | * @}
|
---|
| 584 | */
|
---|