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