| 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_superblock.c
|
|---|
| 35 | * @brief Ext4 superblock operations.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <byteorder.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <block.h>
|
|---|
| 41 | #include <malloc.h>
|
|---|
| 42 | #include "libext4.h"
|
|---|
| 43 |
|
|---|
| 44 | /** Get number of i-nodes in the whole filesystem.
|
|---|
| 45 | *
|
|---|
| 46 | * @param sb Superblock
|
|---|
| 47 | *
|
|---|
| 48 | * @return Number of i-nodes
|
|---|
| 49 | *
|
|---|
| 50 | */
|
|---|
| 51 | uint32_t ext4_superblock_get_inodes_count(ext4_superblock_t *sb)
|
|---|
| 52 | {
|
|---|
| 53 | return uint32_t_le2host(sb->inodes_count);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | /** Set number of i-nodes in the whole filesystem.
|
|---|
| 57 | *
|
|---|
| 58 | * @param sb Superblock
|
|---|
| 59 | * @param count Number of i-nodes
|
|---|
| 60 | *
|
|---|
| 61 | */
|
|---|
| 62 | void ext4_superblock_set_inodes_count(ext4_superblock_t *sb, uint32_t count)
|
|---|
| 63 | {
|
|---|
| 64 | sb->inodes_count = host2uint32_t_le(count);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /** Get number of data blocks in the whole filesystem.
|
|---|
| 68 | *
|
|---|
| 69 | * @param sb Superblock
|
|---|
| 70 | *
|
|---|
| 71 | * @return Number of data blocks
|
|---|
| 72 | *
|
|---|
| 73 | */
|
|---|
| 74 | uint64_t ext4_superblock_get_blocks_count(ext4_superblock_t *sb)
|
|---|
| 75 | {
|
|---|
| 76 | return ((uint64_t) uint32_t_le2host(sb->blocks_count_hi) << 32) |
|
|---|
| 77 | uint32_t_le2host(sb->blocks_count_lo);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /** Set number of data blocks in the whole filesystem.
|
|---|
| 81 | *
|
|---|
| 82 | * @param sb Superblock
|
|---|
| 83 | * @param count Number of data blocks
|
|---|
| 84 | *
|
|---|
| 85 | */
|
|---|
| 86 | void ext4_superblock_set_blocks_count(ext4_superblock_t *sb, uint64_t count)
|
|---|
| 87 | {
|
|---|
| 88 | sb->blocks_count_lo = host2uint32_t_le((count << 32) >> 32);
|
|---|
| 89 | sb->blocks_count_hi = host2uint32_t_le(count >> 32);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /** Get number of reserved data blocks in the whole filesystem.
|
|---|
| 93 | *
|
|---|
| 94 | * @param sb Superblock
|
|---|
| 95 | *
|
|---|
| 96 | * @return Number of reserved data blocks
|
|---|
| 97 | *
|
|---|
| 98 | */
|
|---|
| 99 | uint64_t ext4_superblock_get_reserved_blocks_count(ext4_superblock_t *sb)
|
|---|
| 100 | {
|
|---|
| 101 | return ((uint64_t)
|
|---|
| 102 | uint32_t_le2host(sb->reserved_blocks_count_hi) << 32) |
|
|---|
| 103 | uint32_t_le2host(sb->reserved_blocks_count_lo);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /** Set number of reserved data blocks in the whole filesystem.
|
|---|
| 107 | *
|
|---|
| 108 | * @param sb Superblock
|
|---|
| 109 | * @param count Number of reserved data blocks
|
|---|
| 110 | *
|
|---|
| 111 | */
|
|---|
| 112 | void ext4_superblock_set_reserved_blocks_count(ext4_superblock_t *sb,
|
|---|
| 113 | uint64_t count)
|
|---|
| 114 | {
|
|---|
| 115 | sb->reserved_blocks_count_lo = host2uint32_t_le((count << 32) >> 32);
|
|---|
| 116 | sb->reserved_blocks_count_hi = host2uint32_t_le(count >> 32);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | /** Get number of free data blocks in the whole filesystem.
|
|---|
| 120 | *
|
|---|
| 121 | * @param sb Superblock
|
|---|
| 122 | *
|
|---|
| 123 | * @return Number of free data blocks
|
|---|
| 124 | *
|
|---|
| 125 | */
|
|---|
| 126 | uint64_t ext4_superblock_get_free_blocks_count(ext4_superblock_t *sb)
|
|---|
| 127 | {
|
|---|
| 128 | return ((uint64_t)
|
|---|
| 129 | uint32_t_le2host(sb->free_blocks_count_hi) << 32) |
|
|---|
| 130 | uint32_t_le2host(sb->free_blocks_count_lo);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /** Set number of free data blocks in the whole filesystem.
|
|---|
| 134 | *
|
|---|
| 135 | * @param sb Superblock
|
|---|
| 136 | * @param count Number of free data blocks
|
|---|
| 137 | *
|
|---|
| 138 | */
|
|---|
| 139 | void ext4_superblock_set_free_blocks_count(ext4_superblock_t *sb,
|
|---|
| 140 | uint64_t count)
|
|---|
| 141 | {
|
|---|
| 142 | sb->free_blocks_count_lo = host2uint32_t_le((count << 32) >> 32);
|
|---|
| 143 | sb->free_blocks_count_hi = host2uint32_t_le(count >> 32);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | /** Get number of free i-nodes in the whole filesystem.
|
|---|
| 147 | *
|
|---|
| 148 | * @param sb Superblock
|
|---|
| 149 | *
|
|---|
| 150 | * @return Number of free i-nodes
|
|---|
| 151 | *
|
|---|
| 152 | */
|
|---|
| 153 | uint32_t ext4_superblock_get_free_inodes_count(ext4_superblock_t *sb)
|
|---|
| 154 | {
|
|---|
| 155 | return uint32_t_le2host(sb->free_inodes_count);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | /** Set number of free i-nodes in the whole filesystem.
|
|---|
| 159 | *
|
|---|
| 160 | * @param sb Superblock
|
|---|
| 161 | * @param count Number of free i-nodes
|
|---|
| 162 | *
|
|---|
| 163 | */
|
|---|
| 164 | void ext4_superblock_set_free_inodes_count(ext4_superblock_t *sb,
|
|---|
| 165 | uint32_t count)
|
|---|
| 166 | {
|
|---|
| 167 | sb->free_inodes_count = host2uint32_t_le(count);
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | /** Get index of first data block (block where the superblock is located)
|
|---|
| 171 | *
|
|---|
| 172 | * @param sb Superblock
|
|---|
| 173 | *
|
|---|
| 174 | * @return Index of the first data block
|
|---|
| 175 | *
|
|---|
| 176 | */
|
|---|
| 177 | uint32_t ext4_superblock_get_first_data_block(ext4_superblock_t *sb)
|
|---|
| 178 | {
|
|---|
| 179 | return uint32_t_le2host(sb->first_data_block);
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /** Set index of first data block (block where the superblock is located)
|
|---|
| 183 | *
|
|---|
| 184 | * @param sb Superblock
|
|---|
| 185 | * @param first Index of the first data block
|
|---|
| 186 | *
|
|---|
| 187 | */
|
|---|
| 188 | void ext4_superblock_set_first_data_block(ext4_superblock_t *sb,
|
|---|
| 189 | uint32_t first)
|
|---|
| 190 | {
|
|---|
| 191 | sb->first_data_block = host2uint32_t_le(first);
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | /** Get logarithmic block size (1024 << size == block_size)
|
|---|
| 195 | *
|
|---|
| 196 | * @param sb Superblock
|
|---|
| 197 | *
|
|---|
| 198 | * @return Logarithmic block size
|
|---|
| 199 | *
|
|---|
| 200 | */
|
|---|
| 201 | uint32_t ext4_superblock_get_log_block_size(ext4_superblock_t *sb)
|
|---|
| 202 | {
|
|---|
| 203 | return uint32_t_le2host(sb->log_block_size);
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | /** Set logarithmic block size (1024 << size == block_size)
|
|---|
| 207 | *
|
|---|
| 208 | * @param sb Superblock
|
|---|
| 209 | *
|
|---|
| 210 | * @return Logarithmic block size
|
|---|
| 211 | *
|
|---|
| 212 | */
|
|---|
| 213 | void ext4_superblock_set_log_block_size(ext4_superblock_t *sb,
|
|---|
| 214 | uint32_t log_size)
|
|---|
| 215 | {
|
|---|
| 216 | sb->log_block_size = host2uint32_t_le(log_size);
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | /** Get size of data block (in bytes).
|
|---|
| 220 | *
|
|---|
| 221 | * @param sb Superblock
|
|---|
| 222 | *
|
|---|
| 223 | * @return Size of data block
|
|---|
| 224 | *
|
|---|
| 225 | */
|
|---|
| 226 | uint32_t ext4_superblock_get_block_size(ext4_superblock_t *sb)
|
|---|
| 227 | {
|
|---|
| 228 | return 1024 << ext4_superblock_get_log_block_size(sb);
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | /** Set size of data block (in bytes).
|
|---|
| 232 | *
|
|---|
| 233 | * @param sb Superblock
|
|---|
| 234 | * @param size Size of data block (must be power of 2, at least 1024)
|
|---|
| 235 | *
|
|---|
| 236 | */
|
|---|
| 237 | void ext4_superblock_set_block_size(ext4_superblock_t *sb, uint32_t size)
|
|---|
| 238 | {
|
|---|
| 239 | uint32_t log = 0;
|
|---|
| 240 | uint32_t tmp = size / EXT4_MIN_BLOCK_SIZE;
|
|---|
| 241 |
|
|---|
| 242 | tmp >>= 1;
|
|---|
| 243 | while (tmp) {
|
|---|
| 244 | log++;
|
|---|
| 245 | tmp >>= 1;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | ext4_superblock_set_log_block_size(sb, log);
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | /** Get logarithmic fragment size (1024 << size)
|
|---|
| 252 | *
|
|---|
| 253 | * @param sb Superblock
|
|---|
| 254 | *
|
|---|
| 255 | * @return Logarithmic fragment size
|
|---|
| 256 | *
|
|---|
| 257 | */
|
|---|
| 258 | uint32_t ext4_superblock_get_log_frag_size(ext4_superblock_t *sb)
|
|---|
| 259 | {
|
|---|
| 260 | return uint32_t_le2host(sb->log_frag_size);
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | /** Set logarithmic fragment size (1024 << size)
|
|---|
| 264 | *
|
|---|
| 265 | * @param sb Superblock
|
|---|
| 266 | * @param frag_size Logarithmic fragment size
|
|---|
| 267 | *
|
|---|
| 268 | */
|
|---|
| 269 | void ext4_superblock_set_log_frag_size(ext4_superblock_t *sb,
|
|---|
| 270 | uint32_t frag_size)
|
|---|
| 271 | {
|
|---|
| 272 | sb->log_frag_size = host2uint32_t_le(frag_size);
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | /** Get size of fragment (in bytes).
|
|---|
| 276 | *
|
|---|
| 277 | * @param sb Superblock
|
|---|
| 278 | *
|
|---|
| 279 | * @return Size of fragment
|
|---|
| 280 | *
|
|---|
| 281 | */
|
|---|
| 282 | uint32_t ext4_superblock_get_frag_size(ext4_superblock_t *sb)
|
|---|
| 283 | {
|
|---|
| 284 | return 1024 << ext4_superblock_get_log_frag_size(sb);
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | /** Set size of fragment (in bytes).
|
|---|
| 288 | *
|
|---|
| 289 | * @param sb Superblock
|
|---|
| 290 | * @param size Size of fragment (must be power of 2, at least 1024)
|
|---|
| 291 | *
|
|---|
| 292 | */
|
|---|
| 293 | void ext4_superblock_set_frag_size(ext4_superblock_t *sb, uint32_t size)
|
|---|
| 294 | {
|
|---|
| 295 | uint32_t log = 0;
|
|---|
| 296 | uint32_t tmp = size / EXT4_MIN_BLOCK_SIZE;
|
|---|
| 297 |
|
|---|
| 298 | tmp >>= 1;
|
|---|
| 299 | while (tmp) {
|
|---|
| 300 | log++;
|
|---|
| 301 | tmp >>= 1;
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | ext4_superblock_set_log_frag_size(sb, log);
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | /** Get number of data blocks per block group (except last BG)
|
|---|
| 308 | *
|
|---|
| 309 | * @param sb Superblock
|
|---|
| 310 | *
|
|---|
| 311 | * @return Data blocks per block group
|
|---|
| 312 | *
|
|---|
| 313 | */
|
|---|
| 314 | uint32_t ext4_superblock_get_blocks_per_group(ext4_superblock_t *sb)
|
|---|
| 315 | {
|
|---|
| 316 | return uint32_t_le2host(sb->blocks_per_group);
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | /** Set number of data blocks per block group (except last BG)
|
|---|
| 320 | *
|
|---|
| 321 | * @param sb Superblock
|
|---|
| 322 | * @param blocks Data blocks per block group
|
|---|
| 323 | *
|
|---|
| 324 | */
|
|---|
| 325 | void ext4_superblock_set_blocks_per_group(ext4_superblock_t *sb,
|
|---|
| 326 | uint32_t blocks)
|
|---|
| 327 | {
|
|---|
| 328 | sb->blocks_per_group = host2uint32_t_le(blocks);
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | /** Get number of fragments per block group (except last BG)
|
|---|
| 332 | *
|
|---|
| 333 | * @param sb Superblock
|
|---|
| 334 | *
|
|---|
| 335 | * @return Fragments per block group
|
|---|
| 336 | *
|
|---|
| 337 | */
|
|---|
| 338 | uint32_t ext4_superblock_get_frags_per_group(ext4_superblock_t *sb)
|
|---|
| 339 | {
|
|---|
| 340 | return uint32_t_le2host(sb->frags_per_group);
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | /** Set number of fragment per block group (except last BG)
|
|---|
| 344 | *
|
|---|
| 345 | * @param sb Superblock
|
|---|
| 346 | * @param frags Fragments per block group
|
|---|
| 347 | */
|
|---|
| 348 | void ext4_superblock_set_frags_per_group(ext4_superblock_t *sb, uint32_t frags)
|
|---|
| 349 | {
|
|---|
| 350 | sb->frags_per_group = host2uint32_t_le(frags);
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | /** Get number of i-nodes per block group (except last BG)
|
|---|
| 354 | *
|
|---|
| 355 | * @param sb Superblock
|
|---|
| 356 | *
|
|---|
| 357 | * @return I-nodes per block group
|
|---|
| 358 | *
|
|---|
| 359 | */
|
|---|
| 360 | uint32_t ext4_superblock_get_inodes_per_group(ext4_superblock_t *sb)
|
|---|
| 361 | {
|
|---|
| 362 | return uint32_t_le2host(sb->inodes_per_group);
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | /** Set number of i-nodes per block group (except last BG)
|
|---|
| 366 | *
|
|---|
| 367 | * @param sb Superblock
|
|---|
| 368 | * @param inodes I-nodes per block group
|
|---|
| 369 | *
|
|---|
| 370 | */
|
|---|
| 371 | void ext4_superblock_set_inodes_per_group(ext4_superblock_t *sb,
|
|---|
| 372 | uint32_t inodes)
|
|---|
| 373 | {
|
|---|
| 374 | sb->inodes_per_group = host2uint32_t_le(inodes);
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | /** Get time when filesystem was mounted (POSIX time).
|
|---|
| 378 | *
|
|---|
| 379 | * @param sb Superblock
|
|---|
| 380 | *
|
|---|
| 381 | * @return Mount time
|
|---|
| 382 | *
|
|---|
| 383 | */
|
|---|
| 384 | uint32_t ext4_superblock_get_mount_time(ext4_superblock_t *sb)
|
|---|
| 385 | {
|
|---|
| 386 | return uint32_t_le2host(sb->mount_time);
|
|---|
| 387 | }
|
|---|
| 388 |
|
|---|
| 389 | /** Set time when filesystem was mounted (POSIX time).
|
|---|
| 390 | *
|
|---|
| 391 | * @param sb Superblock
|
|---|
| 392 | * @param time Mount time
|
|---|
| 393 | *
|
|---|
| 394 | */
|
|---|
| 395 | void ext4_superblock_set_mount_time(ext4_superblock_t *sb, uint32_t time)
|
|---|
| 396 | {
|
|---|
| 397 | sb->mount_time = host2uint32_t_le(time);
|
|---|
| 398 | }
|
|---|
| 399 |
|
|---|
| 400 | /** Get time when filesystem was last accesed by write operation (POSIX time).
|
|---|
| 401 | *
|
|---|
| 402 | * @param sb Superblock
|
|---|
| 403 | *
|
|---|
| 404 | * @return Write time
|
|---|
| 405 | *
|
|---|
| 406 | */
|
|---|
| 407 | uint32_t ext4_superblock_get_write_time(ext4_superblock_t *sb)
|
|---|
| 408 | {
|
|---|
| 409 | return uint32_t_le2host(sb->write_time);
|
|---|
| 410 | }
|
|---|
| 411 |
|
|---|
| 412 | /** Set time when filesystem was last accesed by write operation (POSIX time).
|
|---|
| 413 | *
|
|---|
| 414 | * @param sb Superblock
|
|---|
| 415 | * @param time Write time
|
|---|
| 416 | *
|
|---|
| 417 | */
|
|---|
| 418 | void ext4_superblock_set_write_time(ext4_superblock_t *sb, uint32_t time)
|
|---|
| 419 | {
|
|---|
| 420 | sb->write_time = host2uint32_t_le(time);
|
|---|
| 421 | }
|
|---|
| 422 |
|
|---|
| 423 | /** Get number of mount from last filesystem check.
|
|---|
| 424 | *
|
|---|
| 425 | * @param sb Superblock
|
|---|
| 426 | *
|
|---|
| 427 | * @return Number of mounts
|
|---|
| 428 | *
|
|---|
| 429 | */
|
|---|
| 430 | uint16_t ext4_superblock_get_mount_count(ext4_superblock_t *sb)
|
|---|
| 431 | {
|
|---|
| 432 | return uint16_t_le2host(sb->mount_count);
|
|---|
| 433 | }
|
|---|
| 434 |
|
|---|
| 435 | /** Set number of mount from last filesystem check.
|
|---|
| 436 | *
|
|---|
| 437 | * @param sb Superblock
|
|---|
| 438 | * @param count Number of mounts
|
|---|
| 439 | *
|
|---|
| 440 | */
|
|---|
| 441 | void ext4_superblock_set_mount_count(ext4_superblock_t *sb, uint16_t count)
|
|---|
| 442 | {
|
|---|
| 443 | sb->mount_count = host2uint16_t_le(count);
|
|---|
| 444 | }
|
|---|
| 445 |
|
|---|
| 446 | /** Get maximum number of mount from last filesystem check.
|
|---|
| 447 | *
|
|---|
| 448 | * @param sb Superblock
|
|---|
| 449 | *
|
|---|
| 450 | * @return Maximum number of mounts
|
|---|
| 451 | *
|
|---|
| 452 | */
|
|---|
| 453 | uint16_t ext4_superblock_get_max_mount_count(ext4_superblock_t *sb)
|
|---|
| 454 | {
|
|---|
| 455 | return uint16_t_le2host(sb->max_mount_count);
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 | /** Set maximum number of mount from last filesystem check.
|
|---|
| 459 | *
|
|---|
| 460 | * @param sb Superblock
|
|---|
| 461 | * @param count Maximum number of mounts
|
|---|
| 462 | *
|
|---|
| 463 | */
|
|---|
| 464 | void ext4_superblock_set_max_mount_count(ext4_superblock_t *sb, uint16_t count)
|
|---|
| 465 | {
|
|---|
| 466 | sb->max_mount_count = host2uint16_t_le(count);
|
|---|
| 467 | }
|
|---|
| 468 |
|
|---|
| 469 | /** Get superblock magic value.
|
|---|
| 470 | *
|
|---|
| 471 | * @param sb Superblock
|
|---|
| 472 | *
|
|---|
| 473 | * @return Magic value
|
|---|
| 474 | *
|
|---|
| 475 | */
|
|---|
| 476 | uint16_t ext4_superblock_get_magic(ext4_superblock_t *sb)
|
|---|
| 477 | {
|
|---|
| 478 | return uint16_t_le2host(sb->magic);
|
|---|
| 479 | }
|
|---|
| 480 |
|
|---|
| 481 | /** Set superblock magic value.
|
|---|
| 482 | *
|
|---|
| 483 | * @param sb Superblock
|
|---|
| 484 | * @param magic Magic value
|
|---|
| 485 | *
|
|---|
| 486 | */
|
|---|
| 487 | void ext4_superblock_set_magic(ext4_superblock_t *sb, uint16_t magic)
|
|---|
| 488 | {
|
|---|
| 489 | sb->magic = host2uint16_t_le(magic);
|
|---|
| 490 | }
|
|---|
| 491 |
|
|---|
| 492 | /** Get filesystem state.
|
|---|
| 493 | *
|
|---|
| 494 | * @param sb Superblock
|
|---|
| 495 | *
|
|---|
| 496 | * @return Filesystem state
|
|---|
| 497 | *
|
|---|
| 498 | */
|
|---|
| 499 | uint16_t ext4_superblock_get_state(ext4_superblock_t *sb)
|
|---|
| 500 | {
|
|---|
| 501 | return uint16_t_le2host(sb->state);
|
|---|
| 502 | }
|
|---|
| 503 |
|
|---|
| 504 | /** Set filesystem state.
|
|---|
| 505 | *
|
|---|
| 506 | * @param sb Superblock
|
|---|
| 507 | * @param state Filesystem state
|
|---|
| 508 | *
|
|---|
| 509 | */
|
|---|
| 510 | void ext4_superblock_set_state(ext4_superblock_t *sb, uint16_t state)
|
|---|
| 511 | {
|
|---|
| 512 | sb->state = host2uint16_t_le(state);
|
|---|
| 513 | }
|
|---|
| 514 |
|
|---|
| 515 | /** Get behavior code when errors detected.
|
|---|
| 516 | *
|
|---|
| 517 | * @param sb Superblock
|
|---|
| 518 | *
|
|---|
| 519 | * @return Behavior code
|
|---|
| 520 | *
|
|---|
| 521 | */
|
|---|
| 522 | uint16_t ext4_superblock_get_errors(ext4_superblock_t *sb)
|
|---|
| 523 | {
|
|---|
| 524 | return uint16_t_le2host(sb->errors);
|
|---|
| 525 | }
|
|---|
| 526 |
|
|---|
| 527 | /** Set behavior code when errors detected.
|
|---|
| 528 | *
|
|---|
| 529 | * @param sb Superblock
|
|---|
| 530 | * @param errors Behavior code
|
|---|
| 531 | *
|
|---|
| 532 | */
|
|---|
| 533 | void ext4_superblock_set_errors(ext4_superblock_t *sb, uint16_t errors)
|
|---|
| 534 | {
|
|---|
| 535 | sb->errors = host2uint16_t_le(errors);
|
|---|
| 536 | }
|
|---|
| 537 |
|
|---|
| 538 | /** Get minor revision level of the filesystem.
|
|---|
| 539 | *
|
|---|
| 540 | * @param sb Superblock
|
|---|
| 541 | *
|
|---|
| 542 | * @return Minor revision level
|
|---|
| 543 | *
|
|---|
| 544 | */
|
|---|
| 545 | uint16_t ext4_superblock_get_minor_rev_level(ext4_superblock_t *sb)
|
|---|
| 546 | {
|
|---|
| 547 | return uint16_t_le2host(sb->minor_rev_level);
|
|---|
| 548 | }
|
|---|
| 549 |
|
|---|
| 550 | /** Set minor revision level of the filesystem.
|
|---|
| 551 | *
|
|---|
| 552 | * @param sb Superblock
|
|---|
| 553 | * @param level Minor revision level
|
|---|
| 554 | *
|
|---|
| 555 | */
|
|---|
| 556 | void ext4_superblock_set_minor_rev_level(ext4_superblock_t *sb, uint16_t level)
|
|---|
| 557 | {
|
|---|
| 558 | sb->minor_rev_level = host2uint16_t_le(level);
|
|---|
| 559 | }
|
|---|
| 560 |
|
|---|
| 561 | /** Get time of the last filesystem check.
|
|---|
| 562 | *
|
|---|
| 563 | * @param sb Superblock
|
|---|
| 564 | *
|
|---|
| 565 | * @return Time of the last check (POSIX)
|
|---|
| 566 | *
|
|---|
| 567 | */
|
|---|
| 568 | uint32_t ext4_superblock_get_last_check_time(ext4_superblock_t *sb)
|
|---|
| 569 | {
|
|---|
| 570 | return uint32_t_le2host(sb->last_check_time);
|
|---|
| 571 | }
|
|---|
| 572 |
|
|---|
| 573 | /** Set time of the last filesystem check.
|
|---|
| 574 | *
|
|---|
| 575 | * @param sb Superblock
|
|---|
| 576 | * @param time Time of the last check (POSIX)
|
|---|
| 577 | *
|
|---|
| 578 | */
|
|---|
| 579 | void ext4_superblock_set_last_check_time(ext4_superblock_t *sb, uint32_t time)
|
|---|
| 580 | {
|
|---|
| 581 | sb->state = host2uint32_t_le(time);
|
|---|
| 582 | }
|
|---|
| 583 |
|
|---|
| 584 | /** Get maximum time interval between two filesystem checks.
|
|---|
| 585 | *
|
|---|
| 586 | * @param sb Superblock
|
|---|
| 587 | *
|
|---|
| 588 | * @return Time interval between two check (POSIX)
|
|---|
| 589 | *
|
|---|
| 590 | */
|
|---|
| 591 | uint32_t ext4_superblock_get_check_interval(ext4_superblock_t *sb)
|
|---|
| 592 | {
|
|---|
| 593 | return uint32_t_le2host(sb->check_interval);
|
|---|
| 594 | }
|
|---|
| 595 |
|
|---|
| 596 | /** Set maximum time interval between two filesystem checks.
|
|---|
| 597 | *
|
|---|
| 598 | * @param sb Superblock
|
|---|
| 599 | * @param interval Time interval between two check (POSIX)
|
|---|
| 600 | *
|
|---|
| 601 | */
|
|---|
| 602 | void ext4_superblock_set_check_interval(ext4_superblock_t *sb, uint32_t interval)
|
|---|
| 603 | {
|
|---|
| 604 | sb->check_interval = host2uint32_t_le(interval);
|
|---|
| 605 | }
|
|---|
| 606 |
|
|---|
| 607 | /** Get operation system identifier, on which the filesystem was created.
|
|---|
| 608 | *
|
|---|
| 609 | * @param sb Superblock
|
|---|
| 610 | *
|
|---|
| 611 | * @return Operation system identifier
|
|---|
| 612 | *
|
|---|
| 613 | */
|
|---|
| 614 | uint32_t ext4_superblock_get_creator_os(ext4_superblock_t *sb)
|
|---|
| 615 | {
|
|---|
| 616 | return uint32_t_le2host(sb->creator_os);
|
|---|
| 617 | }
|
|---|
| 618 |
|
|---|
| 619 | /** Set operation system identifier, on which the filesystem was created.
|
|---|
| 620 | *
|
|---|
| 621 | * @param sb Superblock
|
|---|
| 622 | * @param os Operation system identifier
|
|---|
| 623 | *
|
|---|
| 624 | */
|
|---|
| 625 | void ext4_superblock_set_creator_os(ext4_superblock_t *sb, uint32_t os)
|
|---|
| 626 | {
|
|---|
| 627 | sb->creator_os = host2uint32_t_le(os);
|
|---|
| 628 | }
|
|---|
| 629 |
|
|---|
| 630 | /** Get revision level of the filesystem.
|
|---|
| 631 | *
|
|---|
| 632 | * @param sb Superblock
|
|---|
| 633 | *
|
|---|
| 634 | * @return Revision level
|
|---|
| 635 | *
|
|---|
| 636 | */
|
|---|
| 637 | uint32_t ext4_superblock_get_rev_level(ext4_superblock_t *sb)
|
|---|
| 638 | {
|
|---|
| 639 | return uint32_t_le2host(sb->rev_level);
|
|---|
| 640 | }
|
|---|
| 641 |
|
|---|
| 642 | /** Set revision level of the filesystem.
|
|---|
| 643 | *
|
|---|
| 644 | * @param sb Superblock
|
|---|
| 645 | * @param level Revision level
|
|---|
| 646 | *
|
|---|
| 647 | */
|
|---|
| 648 | void ext4_superblock_set_rev_level(ext4_superblock_t *sb, uint32_t level)
|
|---|
| 649 | {
|
|---|
| 650 | sb->rev_level = host2uint32_t_le(level);
|
|---|
| 651 | }
|
|---|
| 652 |
|
|---|
| 653 | /** Get default user id for reserved blocks.
|
|---|
| 654 | *
|
|---|
| 655 | * @param sb Superblock
|
|---|
| 656 | *
|
|---|
| 657 | * @return Default user id for reserved blocks.
|
|---|
| 658 | *
|
|---|
| 659 | */
|
|---|
| 660 | uint16_t ext4_superblock_get_def_resuid(ext4_superblock_t *sb)
|
|---|
| 661 | {
|
|---|
| 662 | return uint16_t_le2host(sb->def_resuid);
|
|---|
| 663 | }
|
|---|
| 664 |
|
|---|
| 665 | /** Set default user id for reserved blocks.
|
|---|
| 666 | *
|
|---|
| 667 | * @param sb Superblock
|
|---|
| 668 | * @param uid Default user id for reserved blocks.
|
|---|
| 669 | *
|
|---|
| 670 | */
|
|---|
| 671 | void ext4_superblock_set_def_resuid(ext4_superblock_t *sb, uint16_t uid)
|
|---|
| 672 | {
|
|---|
| 673 | sb->def_resuid = host2uint16_t_le(uid);
|
|---|
| 674 | }
|
|---|
| 675 |
|
|---|
| 676 | /** Get default group id for reserved blocks.
|
|---|
| 677 | *
|
|---|
| 678 | * @param sb Superblock
|
|---|
| 679 | *
|
|---|
| 680 | * @return Default group id for reserved blocks.
|
|---|
| 681 | *
|
|---|
| 682 | */
|
|---|
| 683 | uint16_t ext4_superblock_get_def_resgid(ext4_superblock_t *sb)
|
|---|
| 684 | {
|
|---|
| 685 | return uint16_t_le2host(sb->def_resgid);
|
|---|
| 686 | }
|
|---|
| 687 |
|
|---|
| 688 | /** Set default group id for reserved blocks.
|
|---|
| 689 | *
|
|---|
| 690 | * @param sb Superblock
|
|---|
| 691 | * @param gid Default group id for reserved blocks.
|
|---|
| 692 | *
|
|---|
| 693 | */
|
|---|
| 694 | void ext4_superblock_set_def_resgid(ext4_superblock_t *sb, uint16_t gid)
|
|---|
| 695 | {
|
|---|
| 696 | sb->def_resgid = host2uint16_t_le(gid);
|
|---|
| 697 | }
|
|---|
| 698 |
|
|---|
| 699 | /** Get index of the first i-node, which can be used for allocation.
|
|---|
| 700 | *
|
|---|
| 701 | * @param sb Superblock
|
|---|
| 702 | *
|
|---|
| 703 | * @return I-node index
|
|---|
| 704 | *
|
|---|
| 705 | */
|
|---|
| 706 | uint32_t ext4_superblock_get_first_inode(ext4_superblock_t *sb)
|
|---|
| 707 | {
|
|---|
| 708 | return uint32_t_le2host(sb->first_inode);
|
|---|
| 709 | }
|
|---|
| 710 |
|
|---|
| 711 | /** Set index of the first i-node, which can be used for allocation.
|
|---|
| 712 | *
|
|---|
| 713 | * @param sb Superblock
|
|---|
| 714 | * @param first_inode I-node index
|
|---|
| 715 | *
|
|---|
| 716 | */
|
|---|
| 717 | void ext4_superblock_set_first_inode(ext4_superblock_t *sb,
|
|---|
| 718 | uint32_t first_inode)
|
|---|
| 719 | {
|
|---|
| 720 | sb->first_inode = host2uint32_t_le(first_inode);
|
|---|
| 721 | }
|
|---|
| 722 |
|
|---|
| 723 | /** Get size of i-node structure.
|
|---|
| 724 | *
|
|---|
| 725 | * For the oldest revision return constant number.
|
|---|
| 726 | *
|
|---|
| 727 | * @param sb Superblock
|
|---|
| 728 | *
|
|---|
| 729 | * @return Size of i-node structure
|
|---|
| 730 | *
|
|---|
| 731 | */
|
|---|
| 732 | uint16_t ext4_superblock_get_inode_size(ext4_superblock_t *sb)
|
|---|
| 733 | {
|
|---|
| 734 | if (ext4_superblock_get_rev_level(sb) == 0)
|
|---|
| 735 | return EXT4_REV0_INODE_SIZE;
|
|---|
| 736 |
|
|---|
| 737 | return uint16_t_le2host(sb->inode_size);
|
|---|
| 738 | }
|
|---|
| 739 |
|
|---|
| 740 | /** Set size of i-node structure.
|
|---|
| 741 | *
|
|---|
| 742 | * @param sb Superblock
|
|---|
| 743 | * @param size Size of i-node structure
|
|---|
| 744 | *
|
|---|
| 745 | */
|
|---|
| 746 | void ext4_superblock_set_inode_size(ext4_superblock_t *sb, uint16_t size)
|
|---|
| 747 | {
|
|---|
| 748 | sb->inode_size = host2uint16_t_le(size);
|
|---|
| 749 | }
|
|---|
| 750 |
|
|---|
| 751 | /** Get index of block group, where superblock copy is located.
|
|---|
| 752 | *
|
|---|
| 753 | * @param sb Superblock
|
|---|
| 754 | *
|
|---|
| 755 | * @return Block group index
|
|---|
| 756 | *
|
|---|
| 757 | */
|
|---|
| 758 | uint16_t ext4_superblock_get_block_group_index(ext4_superblock_t *sb)
|
|---|
| 759 | {
|
|---|
| 760 | return uint16_t_le2host(sb->block_group_index);
|
|---|
| 761 | }
|
|---|
| 762 |
|
|---|
| 763 | /** Set index of block group, where superblock copy is located.
|
|---|
| 764 | *
|
|---|
| 765 | * @param sb Superblock
|
|---|
| 766 | * @param bgid Block group index
|
|---|
| 767 | *
|
|---|
| 768 | */
|
|---|
| 769 | void ext4_superblock_set_block_group_index(ext4_superblock_t *sb, uint16_t bgid)
|
|---|
| 770 | {
|
|---|
| 771 | sb->block_group_index = host2uint16_t_le(bgid);
|
|---|
| 772 | }
|
|---|
| 773 |
|
|---|
| 774 | /** Get compatible features supported by the filesystem.
|
|---|
| 775 | *
|
|---|
| 776 | * @param sb Superblock
|
|---|
| 777 | *
|
|---|
| 778 | * @return Compatible features bitmap
|
|---|
| 779 | *
|
|---|
| 780 | */
|
|---|
| 781 | uint32_t ext4_superblock_get_features_compatible(ext4_superblock_t *sb)
|
|---|
| 782 | {
|
|---|
| 783 | return uint32_t_le2host(sb->features_compatible);
|
|---|
| 784 | }
|
|---|
| 785 |
|
|---|
| 786 | /** Set compatible features supported by the filesystem.
|
|---|
| 787 | *
|
|---|
| 788 | * @param sb Superblock
|
|---|
| 789 | * @param features Compatible features bitmap
|
|---|
| 790 | *
|
|---|
| 791 | */
|
|---|
| 792 | void ext4_superblock_set_features_compatible(ext4_superblock_t *sb,
|
|---|
| 793 | uint32_t features)
|
|---|
| 794 | {
|
|---|
| 795 | sb->features_compatible = host2uint32_t_le(features);
|
|---|
| 796 | }
|
|---|
| 797 |
|
|---|
| 798 | /** Get incompatible features supported by the filesystem.
|
|---|
| 799 | *
|
|---|
| 800 | * @param sb Superblock
|
|---|
| 801 | *
|
|---|
| 802 | * @return Incompatible features bitmap
|
|---|
| 803 | *
|
|---|
| 804 | */
|
|---|
| 805 | uint32_t ext4_superblock_get_features_incompatible(ext4_superblock_t *sb)
|
|---|
| 806 | {
|
|---|
| 807 | return uint32_t_le2host(sb->features_incompatible);
|
|---|
| 808 | }
|
|---|
| 809 |
|
|---|
| 810 | /** Set incompatible features supported by the filesystem.
|
|---|
| 811 | *
|
|---|
| 812 | * @param sb Superblock
|
|---|
| 813 | * @param features Incompatible features bitmap
|
|---|
| 814 | *
|
|---|
| 815 | */
|
|---|
| 816 | void ext4_superblock_set_features_incompatible(ext4_superblock_t *sb,
|
|---|
| 817 | uint32_t features)
|
|---|
| 818 | {
|
|---|
| 819 | sb->features_incompatible = host2uint32_t_le(features);
|
|---|
| 820 | }
|
|---|
| 821 |
|
|---|
| 822 | /** Get compatible features supported by the filesystem.
|
|---|
| 823 | *
|
|---|
| 824 | * @param sb Superblock
|
|---|
| 825 | *
|
|---|
| 826 | * @return Read-only compatible features bitmap
|
|---|
| 827 | *
|
|---|
| 828 | */
|
|---|
| 829 | uint32_t ext4_superblock_get_features_read_only(ext4_superblock_t *sb)
|
|---|
| 830 | {
|
|---|
| 831 | return uint32_t_le2host(sb->features_read_only);
|
|---|
| 832 | }
|
|---|
| 833 |
|
|---|
| 834 | /** Set compatible features supported by the filesystem.
|
|---|
| 835 | *
|
|---|
| 836 | * @param sb Superblock
|
|---|
| 837 | * @param feature Read-only compatible features bitmap
|
|---|
| 838 | *
|
|---|
| 839 | */
|
|---|
| 840 | void ext4_superblock_set_features_read_only(ext4_superblock_t *sb,
|
|---|
| 841 | uint32_t features)
|
|---|
| 842 | {
|
|---|
| 843 | sb->features_read_only = host2uint32_t_le(features);
|
|---|
| 844 | }
|
|---|
| 845 |
|
|---|
| 846 | /** Get UUID of the filesystem.
|
|---|
| 847 | *
|
|---|
| 848 | * @param sb superblock
|
|---|
| 849 | *
|
|---|
| 850 | * @return Pointer to UUID array
|
|---|
| 851 | *
|
|---|
| 852 | */
|
|---|
| 853 | const uint8_t *ext4_superblock_get_uuid(ext4_superblock_t *sb)
|
|---|
| 854 | {
|
|---|
| 855 | return sb->uuid;
|
|---|
| 856 | }
|
|---|
| 857 |
|
|---|
| 858 | /** Set UUID of the filesystem.
|
|---|
| 859 | *
|
|---|
| 860 | * @param sb Superblock
|
|---|
| 861 | * @param uuid Pointer to UUID array
|
|---|
| 862 | *
|
|---|
| 863 | */
|
|---|
| 864 | void ext4_superblock_set_uuid(ext4_superblock_t *sb, const uint8_t *uuid)
|
|---|
| 865 | {
|
|---|
| 866 | memcpy(sb->uuid, uuid, sizeof(sb->uuid));
|
|---|
| 867 | }
|
|---|
| 868 |
|
|---|
| 869 | /** Get name of the filesystem volume.
|
|---|
| 870 | *
|
|---|
| 871 | * @param sb Superblock
|
|---|
| 872 | *
|
|---|
| 873 | * @return Name of the volume
|
|---|
| 874 | *
|
|---|
| 875 | */
|
|---|
| 876 | const char *ext4_superblock_get_volume_name(ext4_superblock_t *sb)
|
|---|
| 877 | {
|
|---|
| 878 | return sb->volume_name;
|
|---|
| 879 | }
|
|---|
| 880 |
|
|---|
| 881 | /** Set name of the filesystem volume.
|
|---|
| 882 | *
|
|---|
| 883 | * @param sb Superblock
|
|---|
| 884 | * @param name New name of the volume
|
|---|
| 885 | */
|
|---|
| 886 | void ext4_superblock_set_volume_name(ext4_superblock_t *sb, const char *name)
|
|---|
| 887 | {
|
|---|
| 888 | memcpy(sb->volume_name, name, sizeof(sb->volume_name));
|
|---|
| 889 | }
|
|---|
| 890 |
|
|---|
| 891 | /** Get name of the directory, where this filesystem was mounted at last.
|
|---|
| 892 | *
|
|---|
| 893 | * @param sb Superblock
|
|---|
| 894 | *
|
|---|
| 895 | * @return Directory name
|
|---|
| 896 | *
|
|---|
| 897 | */
|
|---|
| 898 | const char *ext4_superblock_get_last_mounted(ext4_superblock_t *sb)
|
|---|
| 899 | {
|
|---|
| 900 | return sb->last_mounted;
|
|---|
| 901 | }
|
|---|
| 902 |
|
|---|
| 903 | /** Set name of the directory, where this filesystem was mounted at last.
|
|---|
| 904 | *
|
|---|
| 905 | * @param sb Superblock
|
|---|
| 906 | * @param last Directory name
|
|---|
| 907 | *
|
|---|
| 908 | */
|
|---|
| 909 | void ext4_superblock_set_last_mounted(ext4_superblock_t *sb, const char *last)
|
|---|
| 910 | {
|
|---|
| 911 | memcpy(sb->last_mounted, last, sizeof(sb->last_mounted));
|
|---|
| 912 | }
|
|---|
| 913 |
|
|---|
| 914 | /** Get last orphaned i-node index.
|
|---|
| 915 | *
|
|---|
| 916 | * Orphans are stored in linked list.
|
|---|
| 917 | *
|
|---|
| 918 | * @param sb Superblock
|
|---|
| 919 | *
|
|---|
| 920 | * @return Last orphaned i-node index
|
|---|
| 921 | *
|
|---|
| 922 | */
|
|---|
| 923 | uint32_t ext4_superblock_get_last_orphan(ext4_superblock_t *sb)
|
|---|
| 924 | {
|
|---|
| 925 | return uint32_t_le2host(sb->last_orphan);
|
|---|
| 926 | }
|
|---|
| 927 |
|
|---|
| 928 | /** Set last orphaned i-node index.
|
|---|
| 929 | *
|
|---|
| 930 | * Orphans are stored in linked list.
|
|---|
| 931 | *
|
|---|
| 932 | * @param sb Superblock
|
|---|
| 933 | * @param last_orphan Last orphaned i-node index
|
|---|
| 934 | *
|
|---|
| 935 | */
|
|---|
| 936 | void ext4_superblock_set_last_orphan(ext4_superblock_t *sb,
|
|---|
| 937 | uint32_t last_orphan)
|
|---|
| 938 | {
|
|---|
| 939 | sb->last_orphan = host2uint32_t_le(last_orphan);
|
|---|
| 940 | }
|
|---|
| 941 |
|
|---|
| 942 | /** Get hash seed for directory index hash function.
|
|---|
| 943 | *
|
|---|
| 944 | * @param sb Superblock
|
|---|
| 945 | *
|
|---|
| 946 | * @return Hash seed pointer
|
|---|
| 947 | *
|
|---|
| 948 | */
|
|---|
| 949 | const uint32_t *ext4_superblock_get_hash_seed(ext4_superblock_t *sb)
|
|---|
| 950 | {
|
|---|
| 951 | return sb->hash_seed;
|
|---|
| 952 | }
|
|---|
| 953 |
|
|---|
| 954 | /** Set hash seed for directory index hash function.
|
|---|
| 955 | *
|
|---|
| 956 | * @param sb Superblock
|
|---|
| 957 | * @param seed Hash seed pointer
|
|---|
| 958 | *
|
|---|
| 959 | */
|
|---|
| 960 | void ext4_superblock_set_hash_seed(ext4_superblock_t *sb, const uint32_t *seed)
|
|---|
| 961 | {
|
|---|
| 962 | memcpy(sb->hash_seed, seed, sizeof(sb->hash_seed));
|
|---|
| 963 | }
|
|---|
| 964 |
|
|---|
| 965 | /** Get default version of the hash algorithm version for directory index.
|
|---|
| 966 | *
|
|---|
| 967 | * @param sb Superblock
|
|---|
| 968 | *
|
|---|
| 969 | * @return Default hash version
|
|---|
| 970 | *
|
|---|
| 971 | */
|
|---|
| 972 | uint8_t ext4_superblock_get_default_hash_version(ext4_superblock_t *sb)
|
|---|
| 973 | {
|
|---|
| 974 | return sb->default_hash_version;
|
|---|
| 975 | }
|
|---|
| 976 |
|
|---|
| 977 | /** Set default version of the hash algorithm version for directory index.
|
|---|
| 978 | *
|
|---|
| 979 | * @param sb Superblock
|
|---|
| 980 | * @param version Default hash version
|
|---|
| 981 | *
|
|---|
| 982 | */
|
|---|
| 983 | void ext4_superblock_set_default_hash_version(ext4_superblock_t *sb,
|
|---|
| 984 | uint8_t version)
|
|---|
| 985 | {
|
|---|
| 986 | sb->default_hash_version = version;
|
|---|
| 987 | }
|
|---|
| 988 |
|
|---|
| 989 | /** Get size of block group descriptor structure.
|
|---|
| 990 | *
|
|---|
| 991 | * Output value is checked for minimal size.
|
|---|
| 992 | *
|
|---|
| 993 | * @param sb Superblock
|
|---|
| 994 | *
|
|---|
| 995 | * @return Size of block group descriptor
|
|---|
| 996 | *
|
|---|
| 997 | */
|
|---|
| 998 | uint16_t ext4_superblock_get_desc_size(ext4_superblock_t *sb)
|
|---|
| 999 | {
|
|---|
| 1000 | uint16_t size = uint16_t_le2host(sb->desc_size);
|
|---|
| 1001 |
|
|---|
| 1002 | if (size < EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
|
|---|
| 1003 | size = EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE;
|
|---|
| 1004 |
|
|---|
| 1005 | return size;
|
|---|
| 1006 | }
|
|---|
| 1007 |
|
|---|
| 1008 | /** Set size of block group descriptor structure.
|
|---|
| 1009 | *
|
|---|
| 1010 | * Input value is checked for minimal size.
|
|---|
| 1011 | *
|
|---|
| 1012 | * @param sb Superblock
|
|---|
| 1013 | * @param size Size of block group descriptor
|
|---|
| 1014 | *
|
|---|
| 1015 | */
|
|---|
| 1016 | void ext4_superblock_set_desc_size(ext4_superblock_t *sb, uint16_t size)
|
|---|
| 1017 | {
|
|---|
| 1018 | if (size < EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
|
|---|
| 1019 | sb->desc_size =
|
|---|
| 1020 | host2uint16_t_le(EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE);
|
|---|
| 1021 |
|
|---|
| 1022 | sb->desc_size = host2uint16_t_le(size);
|
|---|
| 1023 | }
|
|---|
| 1024 |
|
|---|
| 1025 | /** Get superblock flags.
|
|---|
| 1026 | *
|
|---|
| 1027 | * @param sb Superblock
|
|---|
| 1028 | *
|
|---|
| 1029 | * @return Flags from the superblock
|
|---|
| 1030 | *
|
|---|
| 1031 | */
|
|---|
| 1032 | uint32_t ext4_superblock_get_flags(ext4_superblock_t *sb)
|
|---|
| 1033 | {
|
|---|
| 1034 | return uint32_t_le2host(sb->flags);
|
|---|
| 1035 | }
|
|---|
| 1036 |
|
|---|
| 1037 | /** Set superblock flags.
|
|---|
| 1038 | *
|
|---|
| 1039 | * @param sb Superblock
|
|---|
| 1040 | * @param flags Flags for the superblock
|
|---|
| 1041 | *
|
|---|
| 1042 | */
|
|---|
| 1043 | void ext4_superblock_set_flags(ext4_superblock_t *sb, uint32_t flags)
|
|---|
| 1044 | {
|
|---|
| 1045 | sb->flags = host2uint32_t_le(flags);
|
|---|
| 1046 | }
|
|---|
| 1047 |
|
|---|
| 1048 | /*
|
|---|
| 1049 | * More complex superblock operations
|
|---|
| 1050 | */
|
|---|
| 1051 |
|
|---|
| 1052 | /** Check if superblock has specified flag.
|
|---|
| 1053 | *
|
|---|
| 1054 | * @param sb Superblock
|
|---|
| 1055 | * @param flag Flag to be checked
|
|---|
| 1056 | *
|
|---|
| 1057 | * @return True, if superblock has the flag
|
|---|
| 1058 | *
|
|---|
| 1059 | */
|
|---|
| 1060 | bool ext4_superblock_has_flag(ext4_superblock_t *sb, uint32_t flag)
|
|---|
| 1061 | {
|
|---|
| 1062 | if (ext4_superblock_get_flags(sb) & flag)
|
|---|
| 1063 | return true;
|
|---|
| 1064 |
|
|---|
| 1065 | return false;
|
|---|
| 1066 | }
|
|---|
| 1067 |
|
|---|
| 1068 | /** Check if filesystem supports compatible feature.
|
|---|
| 1069 | *
|
|---|
| 1070 | * @param sb Superblock
|
|---|
| 1071 | * @param feature Feature to be checked
|
|---|
| 1072 | *
|
|---|
| 1073 | * @return True, if filesystem supports the feature
|
|---|
| 1074 | *
|
|---|
| 1075 | */
|
|---|
| 1076 | bool ext4_superblock_has_feature_compatible(ext4_superblock_t *sb,
|
|---|
| 1077 | uint32_t feature)
|
|---|
| 1078 | {
|
|---|
| 1079 | if (ext4_superblock_get_features_compatible(sb) & feature)
|
|---|
| 1080 | return true;
|
|---|
| 1081 |
|
|---|
| 1082 | return false;
|
|---|
| 1083 | }
|
|---|
| 1084 |
|
|---|
| 1085 | /** Check if filesystem supports incompatible feature.
|
|---|
| 1086 | *
|
|---|
| 1087 | * @param sb Superblock
|
|---|
| 1088 | * @param feature Feature to be checked
|
|---|
| 1089 | *
|
|---|
| 1090 | * @return True, if filesystem supports the feature
|
|---|
| 1091 | *
|
|---|
| 1092 | */
|
|---|
| 1093 | bool ext4_superblock_has_feature_incompatible(ext4_superblock_t *sb,
|
|---|
| 1094 | uint32_t feature)
|
|---|
| 1095 | {
|
|---|
| 1096 | if (ext4_superblock_get_features_incompatible(sb) & feature)
|
|---|
| 1097 | return true;
|
|---|
| 1098 |
|
|---|
| 1099 | return false;
|
|---|
| 1100 | }
|
|---|
| 1101 |
|
|---|
| 1102 | /** Check if filesystem supports read-only compatible feature.
|
|---|
| 1103 | *
|
|---|
| 1104 | * @param sb Superblock
|
|---|
| 1105 | * @param feature Feature to be checked
|
|---|
| 1106 | *
|
|---|
| 1107 | * @return True, if filesystem supports the feature
|
|---|
| 1108 | *
|
|---|
| 1109 | */
|
|---|
| 1110 | bool ext4_superblock_has_feature_read_only(ext4_superblock_t *sb,
|
|---|
| 1111 | uint32_t feature)
|
|---|
| 1112 | {
|
|---|
| 1113 | if (ext4_superblock_get_features_read_only(sb) & feature)
|
|---|
| 1114 | return true;
|
|---|
| 1115 |
|
|---|
| 1116 | return false;
|
|---|
| 1117 | }
|
|---|
| 1118 |
|
|---|
| 1119 | /** Read superblock directly from block device.
|
|---|
| 1120 | *
|
|---|
| 1121 | * @param service_id Block device identifier
|
|---|
| 1122 | * @param sb Output pointer to memory structure
|
|---|
| 1123 | *
|
|---|
| 1124 | * @return Eerror code.
|
|---|
| 1125 | *
|
|---|
| 1126 | */
|
|---|
| 1127 | int ext4_superblock_read_direct(service_id_t service_id, ext4_superblock_t **sb)
|
|---|
| 1128 | {
|
|---|
| 1129 | /* Allocated memory for superblock structure */
|
|---|
| 1130 | void *data = malloc(EXT4_SUPERBLOCK_SIZE);
|
|---|
| 1131 | if (data == NULL)
|
|---|
| 1132 | return ENOMEM;
|
|---|
| 1133 |
|
|---|
| 1134 | /* Read data from block device */
|
|---|
| 1135 | int rc = block_read_bytes_direct(service_id, EXT4_SUPERBLOCK_OFFSET,
|
|---|
| 1136 | EXT4_SUPERBLOCK_SIZE, data);
|
|---|
| 1137 |
|
|---|
| 1138 | if (rc != EOK) {
|
|---|
| 1139 | free(data);
|
|---|
| 1140 | return rc;
|
|---|
| 1141 | }
|
|---|
| 1142 |
|
|---|
| 1143 | /* Set output value */
|
|---|
| 1144 | (*sb) = data;
|
|---|
| 1145 |
|
|---|
| 1146 | return EOK;
|
|---|
| 1147 | }
|
|---|
| 1148 |
|
|---|
| 1149 | /** Write superblock structure directly to block device.
|
|---|
| 1150 | *
|
|---|
| 1151 | * @param service_id Block device identifier
|
|---|
| 1152 | * @param sb Superblock to be written
|
|---|
| 1153 | *
|
|---|
| 1154 | * @return Error code
|
|---|
| 1155 | *
|
|---|
| 1156 | */
|
|---|
| 1157 | int ext4_superblock_write_direct(service_id_t service_id, ext4_superblock_t *sb)
|
|---|
| 1158 | {
|
|---|
| 1159 | /* Load physical block size from block device */
|
|---|
| 1160 | size_t phys_block_size;
|
|---|
| 1161 | int rc = block_get_bsize(service_id, &phys_block_size);
|
|---|
| 1162 | if (rc != EOK)
|
|---|
| 1163 | return rc;
|
|---|
| 1164 |
|
|---|
| 1165 | /* Compute address of the first block */
|
|---|
| 1166 | uint64_t first_block = EXT4_SUPERBLOCK_OFFSET / phys_block_size;
|
|---|
| 1167 |
|
|---|
| 1168 | /* Compute number of block to write */
|
|---|
| 1169 | size_t block_count = EXT4_SUPERBLOCK_SIZE / phys_block_size;
|
|---|
| 1170 |
|
|---|
| 1171 | /* Check alignment */
|
|---|
| 1172 | if (EXT4_SUPERBLOCK_SIZE % phys_block_size)
|
|---|
| 1173 | block_count++;
|
|---|
| 1174 |
|
|---|
| 1175 | /* Write data */
|
|---|
| 1176 | return block_write_direct(service_id, first_block, block_count, sb);
|
|---|
| 1177 | }
|
|---|
| 1178 |
|
|---|
| 1179 | /** Check sanity of the superblock.
|
|---|
| 1180 | *
|
|---|
| 1181 | * This check is performed at mount time.
|
|---|
| 1182 | * Checks are described by one-line comments in the code.
|
|---|
| 1183 | *
|
|---|
| 1184 | * @param sb Superblock to check
|
|---|
| 1185 | *
|
|---|
| 1186 | * @return Error code
|
|---|
| 1187 | *
|
|---|
| 1188 | */
|
|---|
| 1189 | int ext4_superblock_check_sanity(ext4_superblock_t *sb)
|
|---|
| 1190 | {
|
|---|
| 1191 | if (ext4_superblock_get_magic(sb) != EXT4_SUPERBLOCK_MAGIC)
|
|---|
| 1192 | return ENOTSUP;
|
|---|
| 1193 |
|
|---|
| 1194 | if (ext4_superblock_get_inodes_count(sb) == 0)
|
|---|
| 1195 | return ENOTSUP;
|
|---|
| 1196 |
|
|---|
| 1197 | if (ext4_superblock_get_blocks_count(sb) == 0)
|
|---|
| 1198 | return ENOTSUP;
|
|---|
| 1199 |
|
|---|
| 1200 | if (ext4_superblock_get_blocks_per_group(sb) == 0)
|
|---|
| 1201 | return ENOTSUP;
|
|---|
| 1202 |
|
|---|
| 1203 | if (ext4_superblock_get_inodes_per_group(sb) == 0)
|
|---|
| 1204 | return ENOTSUP;
|
|---|
| 1205 |
|
|---|
| 1206 | if (ext4_superblock_get_inode_size(sb) < 128)
|
|---|
| 1207 | return ENOTSUP;
|
|---|
| 1208 |
|
|---|
| 1209 | if (ext4_superblock_get_first_inode(sb) < 11)
|
|---|
| 1210 | return ENOTSUP;
|
|---|
| 1211 |
|
|---|
| 1212 | if (ext4_superblock_get_desc_size(sb) <
|
|---|
| 1213 | EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
|
|---|
| 1214 | return ENOTSUP;
|
|---|
| 1215 |
|
|---|
| 1216 | if (ext4_superblock_get_desc_size(sb) >
|
|---|
| 1217 | EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE)
|
|---|
| 1218 | return ENOTSUP;
|
|---|
| 1219 |
|
|---|
| 1220 | return EOK;
|
|---|
| 1221 | }
|
|---|
| 1222 |
|
|---|
| 1223 | /** Compute number of block groups in the filesystem.
|
|---|
| 1224 | *
|
|---|
| 1225 | * @param sb Superblock
|
|---|
| 1226 | *
|
|---|
| 1227 | * @return Number of block groups
|
|---|
| 1228 | *
|
|---|
| 1229 | */
|
|---|
| 1230 | uint32_t ext4_superblock_get_block_group_count(ext4_superblock_t *sb)
|
|---|
| 1231 | {
|
|---|
| 1232 | uint64_t blocks_count = ext4_superblock_get_blocks_count(sb);
|
|---|
| 1233 | uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
|
|---|
| 1234 |
|
|---|
| 1235 | uint32_t block_groups_count = blocks_count / blocks_per_group;
|
|---|
| 1236 |
|
|---|
| 1237 | if (blocks_count % blocks_per_group)
|
|---|
| 1238 | block_groups_count++;
|
|---|
| 1239 |
|
|---|
| 1240 | return block_groups_count;
|
|---|
| 1241 | }
|
|---|
| 1242 |
|
|---|
| 1243 | /** Compute number of blocks in specified block group.
|
|---|
| 1244 | *
|
|---|
| 1245 | * @param sb Superblock
|
|---|
| 1246 | * @param bgid Block group index
|
|---|
| 1247 | *
|
|---|
| 1248 | * @return Number of blocks
|
|---|
| 1249 | *
|
|---|
| 1250 | */
|
|---|
| 1251 | uint32_t ext4_superblock_get_blocks_in_group(ext4_superblock_t *sb, uint32_t bgid)
|
|---|
| 1252 | {
|
|---|
| 1253 | uint32_t block_group_count =
|
|---|
| 1254 | ext4_superblock_get_block_group_count(sb);
|
|---|
| 1255 | uint32_t blocks_per_group =
|
|---|
| 1256 | ext4_superblock_get_blocks_per_group(sb);
|
|---|
| 1257 | uint64_t total_blocks =
|
|---|
| 1258 | ext4_superblock_get_blocks_count(sb);
|
|---|
| 1259 |
|
|---|
| 1260 | if (bgid < block_group_count - 1)
|
|---|
| 1261 | return blocks_per_group;
|
|---|
| 1262 | else
|
|---|
| 1263 | return (total_blocks - ((block_group_count - 1) * blocks_per_group));
|
|---|
| 1264 | }
|
|---|
| 1265 |
|
|---|
| 1266 | /** Compute number of i-nodes in specified block group.
|
|---|
| 1267 | *
|
|---|
| 1268 | * @param sb Superblock
|
|---|
| 1269 | * @param bgid Block group index
|
|---|
| 1270 | *
|
|---|
| 1271 | * @return Number of i-nodes
|
|---|
| 1272 | *
|
|---|
| 1273 | */
|
|---|
| 1274 | uint32_t ext4_superblock_get_inodes_in_group(ext4_superblock_t *sb, uint32_t bgid)
|
|---|
| 1275 | {
|
|---|
| 1276 | uint32_t block_group_count =
|
|---|
| 1277 | ext4_superblock_get_block_group_count(sb);
|
|---|
| 1278 | uint32_t inodes_per_group =
|
|---|
| 1279 | ext4_superblock_get_inodes_per_group(sb);
|
|---|
| 1280 | uint32_t total_inodes =
|
|---|
| 1281 | ext4_superblock_get_inodes_count(sb);
|
|---|
| 1282 |
|
|---|
| 1283 | if (bgid < block_group_count - 1)
|
|---|
| 1284 | return inodes_per_group;
|
|---|
| 1285 | else
|
|---|
| 1286 | return (total_inodes - ((block_group_count - 1) * inodes_per_group));
|
|---|
| 1287 | }
|
|---|
| 1288 |
|
|---|
| 1289 | /**
|
|---|
| 1290 | * @}
|
|---|
| 1291 | */
|
|---|