[eb91db7] | 1 | /*
|
---|
[f22d5ef0] | 2 | * Copyright (c) 2012 Frantisek Princ
|
---|
[eb91db7] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libext4
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file libext4_superblock.c
|
---|
[9875711] | 35 | * @brief Ext4 superblock operations.
|
---|
[eb91db7] | 36 | */
|
---|
| 37 |
|
---|
[01ab41b] | 38 | #include <byteorder.h>
|
---|
| 39 | #include <errno.h>
|
---|
| 40 | #include <libblock.h>
|
---|
| 41 | #include <malloc.h>
|
---|
[3711e7e] | 42 | #include "libext4.h"
|
---|
[eb91db7] | 43 |
|
---|
[9fc72fb3] | 44 | /** Get number of i-nodes in the whole filesystem.
|
---|
| 45 | *
|
---|
| 46 | * @param sb superblock
|
---|
| 47 | * @return number of i-nodes
|
---|
| 48 | */
|
---|
[3712434] | 49 | uint32_t ext4_superblock_get_inodes_count(ext4_superblock_t *sb)
|
---|
[01ab41b] | 50 | {
|
---|
[3712434] | 51 | return uint32_t_le2host(sb->inodes_count);
|
---|
[01ab41b] | 52 | }
|
---|
| 53 |
|
---|
[9fc72fb3] | 54 | /** Set number of i-nodes in the whole filesystem.
|
---|
| 55 | *
|
---|
| 56 | * @param sb superblock
|
---|
| 57 | * @param count number of i-nodes
|
---|
| 58 | */
|
---|
[fe27eb4] | 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 |
|
---|
[9fc72fb3] | 64 | /** Get number of data blocks in the whole filesystem.
|
---|
| 65 | *
|
---|
| 66 | * @param sb superblock
|
---|
| 67 | * @return number of data blocks
|
---|
| 68 | */
|
---|
[3712434] | 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 |
|
---|
[9fc72fb3] | 75 | /** Set number of data blocks in the whole filesystem.
|
---|
| 76 | *
|
---|
| 77 | * @param sb superblock
|
---|
| 78 | * @param count number of data blocks
|
---|
| 79 | */
|
---|
[fe27eb4] | 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 |
|
---|
[9fc72fb3] | 86 | /** Get number of reserved data blocks in the whole filesystem.
|
---|
| 87 | *
|
---|
| 88 | * @param sb superblock
|
---|
| 89 | * @return number of reserved data blocks
|
---|
| 90 | */
|
---|
[3712434] | 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 |
|
---|
[9fc72fb3] | 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 | */
|
---|
[fe27eb4] | 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 |
|
---|
[9fc72fb3] | 108 | /** Get number of free data blocks in the whole filesystem.
|
---|
| 109 | *
|
---|
| 110 | * @param sb superblock
|
---|
| 111 | * @return number of free data blocks
|
---|
| 112 | */
|
---|
[3712434] | 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 |
|
---|
[9fc72fb3] | 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 | */
|
---|
[ae3d4f8] | 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 |
|
---|
[9fc72fb3] | 130 | /** Get number of free i-nodes in the whole filesystem.
|
---|
| 131 | *
|
---|
| 132 | * @param sb superblock
|
---|
| 133 | * @return number of free i-nodes
|
---|
| 134 | */
|
---|
[3712434] | 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 |
|
---|
[9fc72fb3] | 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 | */
|
---|
[fe27eb4] | 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 |
|
---|
[9fc72fb3] | 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 | */
|
---|
[3712434] | 155 | uint32_t ext4_superblock_get_first_data_block(ext4_superblock_t *sb)
|
---|
[01ab41b] | 156 | {
|
---|
[9c0c0e1] | 157 | return uint32_t_le2host(sb->first_data_block);
|
---|
[01ab41b] | 158 | }
|
---|
| 159 |
|
---|
[9fc72fb3] | 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 | */
|
---|
[fe27eb4] | 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 |
|
---|
[9fc72fb3] | 170 | /** Get logarithmic block size (1024 << size == block_size)
|
---|
| 171 | *
|
---|
| 172 | * @param sb superblock
|
---|
| 173 | * @return logarithmic block size
|
---|
| 174 | */
|
---|
[3712434] | 175 | uint32_t ext4_superblock_get_log_block_size(ext4_superblock_t *sb)
|
---|
[01ab41b] | 176 | {
|
---|
[9c0c0e1] | 177 | return uint32_t_le2host(sb->log_block_size);
|
---|
[01ab41b] | 178 | }
|
---|
| 179 |
|
---|
[9fc72fb3] | 180 | /** Set logarithmic block size (1024 << size == block_size)
|
---|
| 181 | *
|
---|
| 182 | * @param sb superblock
|
---|
| 183 | * @return logarithmic block size
|
---|
| 184 | */
|
---|
[fe27eb4] | 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 |
|
---|
[9fc72fb3] | 190 | /** Get size of data block (in bytes).
|
---|
| 191 | *
|
---|
| 192 | * @param sb superblock
|
---|
| 193 | * @return size of data block
|
---|
| 194 | */
|
---|
[01ab41b] | 195 | uint32_t ext4_superblock_get_block_size(ext4_superblock_t *sb)
|
---|
| 196 | {
|
---|
[3712434] | 197 | return 1024 << ext4_superblock_get_log_block_size(sb);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[9fc72fb3] | 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 | */
|
---|
[fe27eb4] | 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 | }
|
---|
[3712434] | 218 |
|
---|
[9fc72fb3] | 219 | /** Get number of data blocks per block group (except last BG)
|
---|
| 220 | *
|
---|
| 221 | * @param sb superblock
|
---|
| 222 | * @return data blocks per block group
|
---|
| 223 | */
|
---|
[3712434] | 224 | uint32_t ext4_superblock_get_blocks_per_group(ext4_superblock_t *sb)
|
---|
| 225 | {
|
---|
| 226 | return uint32_t_le2host(sb->blocks_per_group);
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[9fc72fb3] | 229 | /** Set number of data blocks per block group (except last BG)
|
---|
| 230 | *
|
---|
| 231 | * @param sb superblock
|
---|
| 232 | * @param blocks data blocks per block group
|
---|
| 233 | */
|
---|
[fe27eb4] | 234 | void ext4_superblock_set_blocks_per_group(ext4_superblock_t *sb, uint32_t blocks)
|
---|
| 235 | {
|
---|
| 236 | sb->blocks_per_group = host2uint32_t_le(blocks);
|
---|
| 237 | }
|
---|
| 238 |
|
---|
[9fc72fb3] | 239 | /** Get number of i-nodes per block group (except last BG)
|
---|
| 240 | *
|
---|
| 241 | * @param sb superblock
|
---|
| 242 | * @return i-nodes per block group
|
---|
| 243 | */
|
---|
[3712434] | 244 | uint32_t ext4_superblock_get_inodes_per_group(ext4_superblock_t *sb)
|
---|
| 245 | {
|
---|
| 246 | return uint32_t_le2host(sb->inodes_per_group);
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[9fc72fb3] | 249 | /** Set number of i-nodes per block group (except last BG)
|
---|
| 250 | *
|
---|
| 251 | * @param sb superblock
|
---|
| 252 | * @param inodes i-nodes per block group
|
---|
| 253 | */
|
---|
[fe27eb4] | 254 | void ext4_superblock_set_inodes_per_group(ext4_superblock_t *sb, uint32_t inodes)
|
---|
| 255 | {
|
---|
| 256 | sb->inodes_per_group = host2uint32_t_le(inodes);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[9fc72fb3] | 259 | /** Get time when filesystem was mounted (POSIX time).
|
---|
| 260 | *
|
---|
| 261 | * @param sb superblock
|
---|
| 262 | * @return mount time
|
---|
| 263 | */
|
---|
[3712434] | 264 | uint32_t ext4_superblock_get_mount_time(ext4_superblock_t *sb)
|
---|
| 265 | {
|
---|
| 266 | return uint32_t_le2host(sb->mount_time);
|
---|
| 267 | }
|
---|
| 268 |
|
---|
[9fc72fb3] | 269 | /** Set time when filesystem was mounted (POSIX time).
|
---|
| 270 | *
|
---|
| 271 | * @param sb superblock
|
---|
| 272 | * @param time mount time
|
---|
| 273 | */
|
---|
[fe27eb4] | 274 | void ext4_superblock_set_mount_time(ext4_superblock_t *sb, uint32_t time)
|
---|
| 275 | {
|
---|
| 276 | sb->mount_time = host2uint32_t_le(time);
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[9fc72fb3] | 279 | /** Get time when filesystem was last accesed by write operation (POSIX time).
|
---|
| 280 | *
|
---|
| 281 | * @param sb superblock
|
---|
| 282 | * @return write time
|
---|
| 283 | */
|
---|
[3712434] | 284 | uint32_t ext4_superblock_get_write_time(ext4_superblock_t *sb)
|
---|
| 285 | {
|
---|
| 286 | return uint32_t_le2host(sb->write_time);
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[9fc72fb3] | 289 | /** Set time when filesystem was last accesed by write operation (POSIX time).
|
---|
| 290 | *
|
---|
| 291 | * @param sb superblock
|
---|
| 292 | * @param time write time
|
---|
| 293 | */
|
---|
[fe27eb4] | 294 | void ext4_superblock_set_write_time(ext4_superblock_t *sb, uint32_t time)
|
---|
| 295 | {
|
---|
| 296 | sb->write_time = host2uint32_t_le(time);
|
---|
| 297 | }
|
---|
| 298 |
|
---|
[9fc72fb3] | 299 | /** Get number of mount from last filesystem check.
|
---|
| 300 | *
|
---|
| 301 | * @param sb superblock
|
---|
| 302 | * @return number of mounts
|
---|
| 303 | */
|
---|
[3712434] | 304 | uint16_t ext4_superblock_get_mount_count(ext4_superblock_t *sb)
|
---|
| 305 | {
|
---|
| 306 | return uint16_t_le2host(sb->mount_count);
|
---|
| 307 | }
|
---|
| 308 |
|
---|
[9fc72fb3] | 309 | /** Set number of mount from last filesystem check.
|
---|
| 310 | *
|
---|
| 311 | * @param sb superblock
|
---|
| 312 | * @param count number of mounts
|
---|
| 313 | */
|
---|
[fe27eb4] | 314 | void ext4_superblock_set_mount_count(ext4_superblock_t *sb, uint16_t count)
|
---|
| 315 | {
|
---|
| 316 | sb->mount_count = host2uint16_t_le(count);
|
---|
| 317 | }
|
---|
| 318 |
|
---|
[9fc72fb3] | 319 | /** Get maximum number of mount from last filesystem check.
|
---|
| 320 | *
|
---|
| 321 | * @param sb superblock
|
---|
| 322 | * @return maximum number of mounts
|
---|
| 323 | */
|
---|
[3712434] | 324 | uint16_t ext4_superblock_get_max_mount_count(ext4_superblock_t *sb)
|
---|
| 325 | {
|
---|
| 326 | return uint16_t_le2host(sb->max_mount_count);
|
---|
| 327 | }
|
---|
| 328 |
|
---|
[9fc72fb3] | 329 | /** Set maximum number of mount from last filesystem check.
|
---|
| 330 | *
|
---|
| 331 | * @param sb superblock
|
---|
| 332 | * @param count maximum number of mounts
|
---|
| 333 | */
|
---|
[fe27eb4] | 334 | void ext4_superblock_set_max_mount_count(ext4_superblock_t *sb, uint16_t count)
|
---|
| 335 | {
|
---|
| 336 | sb->max_mount_count = host2uint16_t_le(count);
|
---|
| 337 | }
|
---|
| 338 |
|
---|
[9fc72fb3] | 339 | /** Get superblock magic value.
|
---|
| 340 | *
|
---|
| 341 | * @param sb superblock
|
---|
| 342 | * @return magic value
|
---|
| 343 | */
|
---|
[3712434] | 344 | uint16_t ext4_superblock_get_magic(ext4_superblock_t *sb)
|
---|
| 345 | {
|
---|
| 346 | return uint16_t_le2host(sb->magic);
|
---|
| 347 | }
|
---|
| 348 |
|
---|
[9fc72fb3] | 349 | /** Set superblock magic value.
|
---|
| 350 | *
|
---|
| 351 | * @param sb superblock
|
---|
| 352 | * @param magic value
|
---|
| 353 | */
|
---|
| 354 | void ext4_superblock_set_magic(ext4_superblock_t *sb, uint16_t magic)
|
---|
| 355 | {
|
---|
| 356 | sb->magic = host2uint16_t_le(magic);
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | /** TODO comment
|
---|
| 360 | *
|
---|
| 361 | */
|
---|
[3712434] | 362 | uint16_t ext4_superblock_get_state(ext4_superblock_t *sb)
|
---|
| 363 | {
|
---|
| 364 | return uint16_t_le2host(sb->state);
|
---|
| 365 | }
|
---|
| 366 |
|
---|
[9fc72fb3] | 367 | /** TODO comment
|
---|
| 368 | *
|
---|
| 369 | */
|
---|
[fe27eb4] | 370 | void ext4_superblock_set_state(ext4_superblock_t *sb, uint16_t state)
|
---|
| 371 | {
|
---|
| 372 | sb->state = host2uint16_t_le(state);
|
---|
| 373 | }
|
---|
| 374 |
|
---|
[9fc72fb3] | 375 | /** TODO comment
|
---|
| 376 | *
|
---|
| 377 | */
|
---|
[3712434] | 378 | uint16_t ext4_superblock_get_errors(ext4_superblock_t *sb)
|
---|
| 379 | {
|
---|
| 380 | return uint16_t_le2host(sb->errors);
|
---|
| 381 | }
|
---|
| 382 |
|
---|
[9fc72fb3] | 383 | /** TODO comment
|
---|
| 384 | *
|
---|
| 385 | */
|
---|
[fe27eb4] | 386 | void ext4_superblock_set_errors(ext4_superblock_t *sb, uint16_t errors)
|
---|
| 387 | {
|
---|
| 388 | sb->errors = host2uint16_t_le(errors);
|
---|
| 389 | }
|
---|
[3712434] | 390 |
|
---|
[9fc72fb3] | 391 | /** TODO comment
|
---|
| 392 | *
|
---|
| 393 | */
|
---|
[3712434] | 394 | uint16_t ext4_superblock_get_minor_rev_level(ext4_superblock_t *sb)
|
---|
| 395 | {
|
---|
| 396 | return uint16_t_le2host(sb->minor_rev_level);
|
---|
| 397 | }
|
---|
| 398 |
|
---|
[9fc72fb3] | 399 | /** TODO comment
|
---|
| 400 | *
|
---|
| 401 | */
|
---|
[fe27eb4] | 402 | void ext4_superblock_set_minor_rev_level(ext4_superblock_t *sb, uint16_t level)
|
---|
| 403 | {
|
---|
| 404 | sb->minor_rev_level = host2uint16_t_le(level);
|
---|
| 405 | }
|
---|
| 406 |
|
---|
[9fc72fb3] | 407 | /** TODO comment
|
---|
| 408 | *
|
---|
| 409 | */
|
---|
[3712434] | 410 | uint32_t ext4_superblock_get_last_check_time(ext4_superblock_t *sb)
|
---|
| 411 | {
|
---|
| 412 | return uint32_t_le2host(sb->last_check_time);
|
---|
| 413 | }
|
---|
| 414 |
|
---|
[9fc72fb3] | 415 | /** TODO comment
|
---|
| 416 | *
|
---|
| 417 | */
|
---|
[fe27eb4] | 418 | void ext4_superblock_set_last_check_time(ext4_superblock_t *sb, uint32_t time)
|
---|
| 419 | {
|
---|
| 420 | sb->state = host2uint32_t_le(time);
|
---|
| 421 | }
|
---|
| 422 |
|
---|
[9fc72fb3] | 423 | /** TODO comment
|
---|
| 424 | *
|
---|
| 425 | */
|
---|
[3712434] | 426 | uint32_t ext4_superblock_get_check_interval(ext4_superblock_t *sb){
|
---|
| 427 | return uint32_t_le2host(sb->check_interval);
|
---|
| 428 | }
|
---|
| 429 |
|
---|
[9fc72fb3] | 430 | /** TODO comment
|
---|
| 431 | *
|
---|
| 432 | */
|
---|
[fe27eb4] | 433 | void ext4_superblock_set_check_interval(ext4_superblock_t *sb, uint32_t interval)
|
---|
| 434 | {
|
---|
| 435 | sb->check_interval = host2uint32_t_le(interval);
|
---|
| 436 | }
|
---|
| 437 |
|
---|
[9fc72fb3] | 438 | /** TODO comment
|
---|
| 439 | *
|
---|
| 440 | */
|
---|
[3712434] | 441 | uint32_t ext4_superblock_get_creator_os(ext4_superblock_t *sb)
|
---|
| 442 | {
|
---|
| 443 | return uint32_t_le2host(sb->creator_os);
|
---|
[01ab41b] | 444 | }
|
---|
| 445 |
|
---|
[9fc72fb3] | 446 | /** TODO comment
|
---|
| 447 | *
|
---|
| 448 | */
|
---|
[fe27eb4] | 449 | void ext4_superblock_set_creator_os(ext4_superblock_t *sb, uint32_t os)
|
---|
| 450 | {
|
---|
| 451 | sb->creator_os = host2uint32_t_le(os);
|
---|
| 452 | }
|
---|
| 453 |
|
---|
[9fc72fb3] | 454 | /** TODO comment
|
---|
| 455 | *
|
---|
| 456 | */
|
---|
[9c0c0e1] | 457 | uint32_t ext4_superblock_get_rev_level(ext4_superblock_t *sb)
|
---|
| 458 | {
|
---|
| 459 | return uint32_t_le2host(sb->rev_level);
|
---|
| 460 | }
|
---|
| 461 |
|
---|
[9fc72fb3] | 462 | /** TODO comment
|
---|
| 463 | *
|
---|
| 464 | */
|
---|
[fe27eb4] | 465 | void ext4_superblock_set_rev_level(ext4_superblock_t *sb, uint32_t level)
|
---|
| 466 | {
|
---|
| 467 | sb->rev_level = host2uint32_t_le(level);
|
---|
| 468 | }
|
---|
| 469 |
|
---|
[9fc72fb3] | 470 | /** TODO comment
|
---|
| 471 | *
|
---|
| 472 | */
|
---|
[fe27eb4] | 473 | uint16_t ext4_superblock_get_def_resuid(ext4_superblock_t *sb)
|
---|
| 474 | {
|
---|
| 475 | return uint16_t_le2host(sb->def_resuid);
|
---|
| 476 | }
|
---|
| 477 |
|
---|
[9fc72fb3] | 478 | /** TODO comment
|
---|
| 479 | *
|
---|
| 480 | */
|
---|
[fe27eb4] | 481 | void ext4_superblock_set_def_resuid(ext4_superblock_t *sb, uint16_t uid)
|
---|
| 482 | {
|
---|
| 483 | sb->def_resuid = host2uint16_t_le(uid);
|
---|
| 484 | }
|
---|
| 485 |
|
---|
[9fc72fb3] | 486 | /** TODO comment
|
---|
| 487 | *
|
---|
| 488 | */
|
---|
[fe27eb4] | 489 | uint16_t ext4_superblock_get_def_resgid(ext4_superblock_t *sb)
|
---|
| 490 | {
|
---|
| 491 | return uint16_t_le2host(sb->def_resgid);
|
---|
| 492 | }
|
---|
| 493 |
|
---|
[9fc72fb3] | 494 | /** TODO comment
|
---|
| 495 | *
|
---|
| 496 | */
|
---|
[fe27eb4] | 497 | void ext4_superblock_set_def_resgid(ext4_superblock_t *sb, uint16_t gid)
|
---|
| 498 | {
|
---|
| 499 | sb->def_resgid = host2uint16_t_le(gid);
|
---|
| 500 | }
|
---|
| 501 |
|
---|
[9fc72fb3] | 502 | /** TODO comment
|
---|
| 503 | *
|
---|
| 504 | */
|
---|
[fe27eb4] | 505 | uint32_t ext4_superblock_get_first_inode(ext4_superblock_t *sb)
|
---|
| 506 | {
|
---|
| 507 | return uint32_t_le2host(sb->first_inode);
|
---|
| 508 | }
|
---|
| 509 |
|
---|
[9fc72fb3] | 510 | /** TODO comment
|
---|
| 511 | *
|
---|
| 512 | */
|
---|
[fe27eb4] | 513 | void ext4_superblock_set_first_inode(ext4_superblock_t *sb, uint32_t first_inode)
|
---|
| 514 | {
|
---|
| 515 | sb->first_inode = host2uint32_t_le(first_inode);
|
---|
| 516 | }
|
---|
| 517 |
|
---|
[9fc72fb3] | 518 | /** TODO comment
|
---|
| 519 | *
|
---|
| 520 | */
|
---|
[3711e7e] | 521 | uint16_t ext4_superblock_get_inode_size(ext4_superblock_t *sb)
|
---|
| 522 | {
|
---|
| 523 | if (ext4_superblock_get_rev_level(sb) == 0) {
|
---|
| 524 | return EXT4_REV0_INODE_SIZE;
|
---|
| 525 | }
|
---|
| 526 | return uint16_t_le2host(sb->inode_size);
|
---|
| 527 | }
|
---|
| 528 |
|
---|
[9fc72fb3] | 529 | /** TODO comment
|
---|
| 530 | *
|
---|
| 531 | */
|
---|
[fe27eb4] | 532 | void ext4_superblock_set_inode_size(ext4_superblock_t *sb, uint16_t size)
|
---|
| 533 | {
|
---|
| 534 | sb->inode_size = host2uint16_t_le(size);
|
---|
| 535 | }
|
---|
| 536 |
|
---|
[9fc72fb3] | 537 | /** TODO comment
|
---|
| 538 | *
|
---|
| 539 | */
|
---|
[3712434] | 540 | uint16_t ext4_superblock_get_block_group_number(ext4_superblock_t *sb)
|
---|
[3711e7e] | 541 | {
|
---|
[3712434] | 542 | return uint16_t_le2host(sb->block_group_number);
|
---|
[3711e7e] | 543 | }
|
---|
| 544 |
|
---|
[9fc72fb3] | 545 | /** TODO comment
|
---|
| 546 | *
|
---|
| 547 | */
|
---|
[fe27eb4] | 548 | void ext4_superblock_set_block_group_number(ext4_superblock_t *sb, uint16_t bg)
|
---|
| 549 | {
|
---|
| 550 | sb->block_group_number = host2uint16_t_le(bg);
|
---|
| 551 | }
|
---|
| 552 |
|
---|
[9fc72fb3] | 553 | /** TODO comment
|
---|
| 554 | *
|
---|
| 555 | */
|
---|
[9c0c0e1] | 556 | uint32_t ext4_superblock_get_features_compatible(ext4_superblock_t *sb)
|
---|
| 557 | {
|
---|
| 558 | return uint32_t_le2host(sb->features_compatible);
|
---|
| 559 | }
|
---|
| 560 |
|
---|
[9fc72fb3] | 561 | /** TODO comment
|
---|
| 562 | *
|
---|
| 563 | */
|
---|
[fe27eb4] | 564 | void ext4_superblock_set_features_compatible(ext4_superblock_t *sb, uint32_t features)
|
---|
| 565 | {
|
---|
| 566 | sb->features_compatible = host2uint32_t_le(features);
|
---|
| 567 | }
|
---|
| 568 |
|
---|
[9fc72fb3] | 569 | /** TODO comment
|
---|
| 570 | *
|
---|
| 571 | */
|
---|
[9c0c0e1] | 572 | uint32_t ext4_superblock_get_features_incompatible(ext4_superblock_t *sb)
|
---|
| 573 | {
|
---|
| 574 | return uint32_t_le2host(sb->features_incompatible);
|
---|
| 575 | }
|
---|
| 576 |
|
---|
[9fc72fb3] | 577 | /** TODO comment
|
---|
| 578 | *
|
---|
| 579 | */
|
---|
[fe27eb4] | 580 | void ext4_superblock_set_features_incompatible(ext4_superblock_t *sb, uint32_t features)
|
---|
| 581 | {
|
---|
| 582 | sb->features_incompatible = host2uint32_t_le(features);
|
---|
| 583 | }
|
---|
| 584 |
|
---|
[9fc72fb3] | 585 | /** TODO comment
|
---|
| 586 | *
|
---|
| 587 | */
|
---|
[9c0c0e1] | 588 | uint32_t ext4_superblock_get_features_read_only(ext4_superblock_t *sb)
|
---|
| 589 | {
|
---|
| 590 | return uint32_t_le2host(sb->features_read_only);
|
---|
| 591 | }
|
---|
| 592 |
|
---|
[9fc72fb3] | 593 | /** TODO comment
|
---|
| 594 | *
|
---|
| 595 | */
|
---|
[fe27eb4] | 596 | void ext4_superblock_set_features_read_only(ext4_superblock_t *sb, uint32_t features)
|
---|
| 597 | {
|
---|
| 598 | sb->features_read_only = host2uint32_t_le(features);
|
---|
| 599 | }
|
---|
[01ab41b] | 600 |
|
---|
[9fc72fb3] | 601 | /** TODO comment
|
---|
| 602 | *
|
---|
| 603 | */
|
---|
[291af81] | 604 | const uint8_t * ext4_superblock_get_uuid(ext4_superblock_t *sb)
|
---|
| 605 | {
|
---|
| 606 | return sb->uuid;
|
---|
| 607 | }
|
---|
| 608 |
|
---|
[9fc72fb3] | 609 | /** TODO comment
|
---|
| 610 | *
|
---|
| 611 | */
|
---|
[291af81] | 612 | void ext4_superblock_set_uuid(ext4_superblock_t *sb, const uint8_t *uuid)
|
---|
| 613 | {
|
---|
| 614 | memcpy(sb->uuid, uuid, sizeof(sb->uuid));
|
---|
| 615 | }
|
---|
| 616 |
|
---|
| 617 | const char * ext4_superblock_get_volume_name(ext4_superblock_t *sb)
|
---|
| 618 | {
|
---|
| 619 | return sb->volume_name;
|
---|
| 620 | }
|
---|
| 621 |
|
---|
[9fc72fb3] | 622 | /** TODO comment
|
---|
| 623 | *
|
---|
| 624 | */
|
---|
[291af81] | 625 | void ext4_superblock_set_volume_name(ext4_superblock_t *sb, const char *name)
|
---|
| 626 | {
|
---|
| 627 | memcpy(sb->volume_name, name, sizeof(sb->volume_name));
|
---|
| 628 | }
|
---|
| 629 |
|
---|
[9fc72fb3] | 630 | /** TODO comment
|
---|
| 631 | *
|
---|
| 632 | */
|
---|
[291af81] | 633 | const char * ext4_superblock_get_last_mounted(ext4_superblock_t *sb)
|
---|
| 634 | {
|
---|
| 635 | return sb->last_mounted;
|
---|
| 636 | }
|
---|
| 637 |
|
---|
[9fc72fb3] | 638 | /** TODO comment
|
---|
| 639 | *
|
---|
| 640 | */
|
---|
[291af81] | 641 | void ext4_superblock_set_last_mounted(ext4_superblock_t *sb, const char *last)
|
---|
| 642 | {
|
---|
| 643 | memcpy(sb->last_mounted, last, sizeof(sb->last_mounted));
|
---|
| 644 | }
|
---|
| 645 |
|
---|
[9fc72fb3] | 646 | /** TODO comment
|
---|
| 647 | *
|
---|
| 648 | */
|
---|
[ebcaff4] | 649 | uint32_t ext4_superblock_get_last_orphan(ext4_superblock_t *sb)
|
---|
| 650 | {
|
---|
| 651 | return uint32_t_le2host(sb->last_orphan);
|
---|
| 652 | }
|
---|
| 653 |
|
---|
[9fc72fb3] | 654 | /** TODO comment
|
---|
| 655 | *
|
---|
| 656 | */
|
---|
[ebcaff4] | 657 | void ext4_superblock_set_last_orphan(ext4_superblock_t *sb, uint32_t last_orphan)
|
---|
| 658 | {
|
---|
| 659 | sb->last_orphan = host2uint32_t_le(last_orphan);
|
---|
| 660 | }
|
---|
| 661 |
|
---|
[9fc72fb3] | 662 | /** TODO comment
|
---|
| 663 | *
|
---|
| 664 | */
|
---|
[7bc4508] | 665 | uint32_t* ext4_superblock_get_hash_seed(ext4_superblock_t *sb)
|
---|
| 666 | {
|
---|
| 667 | return sb->hash_seed;
|
---|
| 668 | }
|
---|
| 669 |
|
---|
[9fc72fb3] | 670 | /** TODO comment
|
---|
| 671 | *
|
---|
| 672 | */
|
---|
[7eb033ce] | 673 | uint8_t ext4_superblock_get_default_hash_version(ext4_superblock_t *sb)
|
---|
| 674 | {
|
---|
| 675 | return sb->default_hash_version;
|
---|
| 676 | }
|
---|
| 677 |
|
---|
[9fc72fb3] | 678 | /** TODO comment
|
---|
| 679 | *
|
---|
| 680 | */
|
---|
[7eb033ce] | 681 | void ext4_superblock_set_default_hash_version(ext4_superblock_t *sb, uint8_t version)
|
---|
| 682 | {
|
---|
| 683 | sb->default_hash_version = version;
|
---|
| 684 | }
|
---|
| 685 |
|
---|
[9fc72fb3] | 686 | /** TODO comment
|
---|
| 687 | *
|
---|
| 688 | */
|
---|
[c25e39b] | 689 | uint16_t ext4_superblock_get_desc_size(ext4_superblock_t *sb)
|
---|
| 690 | {
|
---|
| 691 | uint16_t size = uint16_t_le2host(sb->desc_size);
|
---|
| 692 |
|
---|
| 693 | if (size < EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
| 694 | size = EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE;
|
---|
| 695 | }
|
---|
| 696 |
|
---|
| 697 | return size;
|
---|
| 698 | }
|
---|
| 699 |
|
---|
[9fc72fb3] | 700 | /** TODO comment
|
---|
| 701 | *
|
---|
| 702 | */
|
---|
[fe27eb4] | 703 | void ext4_superblock_set_desc_size(ext4_superblock_t *sb, uint16_t size)
|
---|
| 704 | {
|
---|
| 705 | sb->desc_size = host2uint16_t_le(size);
|
---|
| 706 | }
|
---|
| 707 |
|
---|
[9fc72fb3] | 708 | /** TODO comment
|
---|
| 709 | *
|
---|
| 710 | */
|
---|
[7bc4508] | 711 | uint32_t ext4_superblock_get_flags(ext4_superblock_t *sb)
|
---|
| 712 | {
|
---|
| 713 | return uint32_t_le2host(sb->flags);
|
---|
| 714 | }
|
---|
| 715 |
|
---|
[9fc72fb3] | 716 | /** TODO comment
|
---|
| 717 | *
|
---|
| 718 | */
|
---|
[fe27eb4] | 719 | void ext4_superblock_set_flags(ext4_superblock_t *sb, uint32_t flags)
|
---|
| 720 | {
|
---|
| 721 | sb->flags = host2uint32_t_le(flags);
|
---|
| 722 | }
|
---|
| 723 |
|
---|
[7bc4508] | 724 |
|
---|
[3712434] | 725 | /*
|
---|
[c25e39b] | 726 | * More complex superblock operations
|
---|
[01ab41b] | 727 | */
|
---|
[3712434] | 728 |
|
---|
[9fc72fb3] | 729 | /** TODO comment
|
---|
| 730 | *
|
---|
| 731 | */
|
---|
[7bc4508] | 732 | bool ext4_superblock_has_flag(ext4_superblock_t *sb, uint32_t flag)
|
---|
| 733 | {
|
---|
| 734 | if (ext4_superblock_get_flags(sb) & flag) {
|
---|
| 735 | return true;
|
---|
| 736 | }
|
---|
| 737 | return false;
|
---|
| 738 | }
|
---|
| 739 |
|
---|
[9fc72fb3] | 740 | /** TODO comment
|
---|
| 741 | *
|
---|
| 742 | */
|
---|
[c25e39b] | 743 | bool ext4_superblock_has_feature_compatible(ext4_superblock_t *sb, uint32_t feature)
|
---|
| 744 | {
|
---|
| 745 | if (ext4_superblock_get_features_compatible(sb) & feature) {
|
---|
| 746 | return true;
|
---|
| 747 | }
|
---|
| 748 | return false;
|
---|
| 749 | }
|
---|
| 750 |
|
---|
[9fc72fb3] | 751 | /** TODO comment
|
---|
| 752 | *
|
---|
| 753 | */
|
---|
[c25e39b] | 754 | bool ext4_superblock_has_feature_incompatible(ext4_superblock_t *sb, uint32_t feature)
|
---|
| 755 | {
|
---|
| 756 | if (ext4_superblock_get_features_incompatible(sb) & feature) {
|
---|
| 757 | return true;
|
---|
| 758 | }
|
---|
| 759 | return false;
|
---|
| 760 | }
|
---|
| 761 |
|
---|
[9fc72fb3] | 762 | /** TODO comment
|
---|
| 763 | *
|
---|
| 764 | */
|
---|
[c25e39b] | 765 | bool ext4_superblock_has_feature_read_only(ext4_superblock_t *sb, uint32_t feature)
|
---|
| 766 | {
|
---|
| 767 | if (ext4_superblock_get_features_read_only(sb) & feature) {
|
---|
| 768 | return true;
|
---|
| 769 | }
|
---|
| 770 | return false;
|
---|
| 771 | }
|
---|
| 772 |
|
---|
[9fc72fb3] | 773 | /** TODO comment
|
---|
| 774 | *
|
---|
| 775 | */
|
---|
[01ab41b] | 776 | int ext4_superblock_read_direct(service_id_t service_id,
|
---|
| 777 | ext4_superblock_t **superblock)
|
---|
| 778 | {
|
---|
| 779 | int rc;
|
---|
| 780 |
|
---|
[d9bbe45] | 781 | void *data = malloc(EXT4_SUPERBLOCK_SIZE);
|
---|
[01ab41b] | 782 | if (data == NULL) {
|
---|
| 783 | return ENOMEM;
|
---|
| 784 | }
|
---|
| 785 |
|
---|
| 786 | rc = block_read_bytes_direct(service_id, EXT4_SUPERBLOCK_OFFSET,
|
---|
| 787 | EXT4_SUPERBLOCK_SIZE, data);
|
---|
| 788 |
|
---|
| 789 | if (rc != EOK) {
|
---|
| 790 | free(data);
|
---|
| 791 | return rc;
|
---|
| 792 | }
|
---|
| 793 |
|
---|
| 794 | (*superblock) = data;
|
---|
| 795 |
|
---|
| 796 | return EOK;
|
---|
| 797 | }
|
---|
| 798 |
|
---|
[9fc72fb3] | 799 | /** TODO comment
|
---|
| 800 | *
|
---|
| 801 | */
|
---|
[ae3d4f8] | 802 | int ext4_superblock_write_direct(service_id_t service_id,
|
---|
| 803 | ext4_superblock_t *sb)
|
---|
| 804 | {
|
---|
| 805 | int rc;
|
---|
| 806 | uint32_t phys_block_size;
|
---|
| 807 |
|
---|
| 808 | rc = block_get_bsize(service_id, &phys_block_size);
|
---|
| 809 | if (rc != EOK) {
|
---|
| 810 | // TODO error
|
---|
| 811 | return rc;
|
---|
| 812 | }
|
---|
| 813 |
|
---|
[d9bbe45] | 814 | uint64_t first_block = EXT4_SUPERBLOCK_OFFSET / phys_block_size;
|
---|
| 815 | uint32_t block_count = EXT4_SUPERBLOCK_SIZE / phys_block_size;
|
---|
[ae3d4f8] | 816 |
|
---|
| 817 | if (EXT4_SUPERBLOCK_SIZE % phys_block_size) {
|
---|
| 818 | block_count++;
|
---|
| 819 | }
|
---|
| 820 |
|
---|
| 821 | return block_write_direct(service_id, first_block, block_count, sb);
|
---|
| 822 |
|
---|
| 823 | }
|
---|
| 824 |
|
---|
[9fc72fb3] | 825 | /** TODO comment
|
---|
| 826 | *
|
---|
| 827 | */
|
---|
[01ab41b] | 828 | int ext4_superblock_check_sanity(ext4_superblock_t *sb)
|
---|
| 829 | {
|
---|
[9c0c0e1] | 830 | if (ext4_superblock_get_magic(sb) != EXT4_SUPERBLOCK_MAGIC) {
|
---|
| 831 | return ENOTSUP;
|
---|
| 832 | }
|
---|
| 833 |
|
---|
[fe27eb4] | 834 | // block size
|
---|
| 835 | // desc size
|
---|
| 836 |
|
---|
| 837 |
|
---|
[9c0c0e1] | 838 | // TODO more checks !!!
|
---|
| 839 |
|
---|
[01ab41b] | 840 | return EOK;
|
---|
| 841 | }
|
---|
[eb91db7] | 842 |
|
---|
[9fc72fb3] | 843 | /** TODO comment
|
---|
| 844 | *
|
---|
| 845 | */
|
---|
[b12ca16] | 846 | uint32_t ext4_superblock_get_block_group_count(ext4_superblock_t *sb)
|
---|
| 847 | {
|
---|
| 848 | uint64_t blocks_count = ext4_superblock_get_blocks_count(sb);
|
---|
| 849 | uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
|
---|
| 850 |
|
---|
| 851 | uint32_t block_groups_count = blocks_count / blocks_per_group;
|
---|
| 852 |
|
---|
| 853 | if (blocks_count % blocks_per_group) {
|
---|
| 854 | block_groups_count++;
|
---|
| 855 | }
|
---|
| 856 |
|
---|
| 857 | return block_groups_count;
|
---|
| 858 |
|
---|
| 859 | }
|
---|
| 860 |
|
---|
[9fc72fb3] | 861 | /** TODO comment
|
---|
| 862 | *
|
---|
| 863 | */
|
---|
[b12ca16] | 864 | uint32_t ext4_superblock_get_blocks_in_group(ext4_superblock_t *sb, uint32_t bgid)
|
---|
| 865 | {
|
---|
| 866 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
---|
| 867 | uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
|
---|
| 868 | uint64_t total_blocks = ext4_superblock_get_blocks_count(sb);
|
---|
| 869 |
|
---|
| 870 | if (bgid < block_group_count - 1) {
|
---|
| 871 | return blocks_per_group;
|
---|
| 872 | } else {
|
---|
| 873 | return (total_blocks - ((block_group_count - 1) * blocks_per_group));
|
---|
| 874 | }
|
---|
| 875 |
|
---|
| 876 | }
|
---|
| 877 |
|
---|
[9fc72fb3] | 878 | /** TODO comment
|
---|
| 879 | *
|
---|
| 880 | */
|
---|
[b12ca16] | 881 | uint32_t ext4_superblock_get_inodes_in_group(ext4_superblock_t *sb, uint32_t bgid)
|
---|
| 882 | {
|
---|
| 883 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
---|
| 884 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
| 885 | uint32_t total_inodes = ext4_superblock_get_inodes_count(sb);
|
---|
| 886 |
|
---|
| 887 | if (bgid < block_group_count - 1) {
|
---|
| 888 | return inodes_per_group;
|
---|
| 889 | } else {
|
---|
| 890 | return (total_inodes - ((block_group_count - 1) * inodes_per_group));
|
---|
| 891 | }
|
---|
| 892 |
|
---|
| 893 | }
|
---|
| 894 |
|
---|
[eb91db7] | 895 | /**
|
---|
| 896 | * @}
|
---|
| 897 | */
|
---|