| 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 | * @file libext4_inode.c
|
|---|
| 34 | * @brief Ext4 i-node structure operations.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <byteorder.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <block.h>
|
|---|
| 40 | #include "libext4.h"
|
|---|
| 41 |
|
|---|
| 42 | /** Compute number of bits for block count.
|
|---|
| 43 | *
|
|---|
| 44 | * @param block_size Filesystem block_size
|
|---|
| 45 | *
|
|---|
| 46 | * @return Number of bits
|
|---|
| 47 | *
|
|---|
| 48 | */
|
|---|
| 49 | static uint32_t ext4_inode_block_bits_count(uint32_t block_size)
|
|---|
| 50 | {
|
|---|
| 51 | uint32_t bits = 8;
|
|---|
| 52 | uint32_t size = block_size;
|
|---|
| 53 |
|
|---|
| 54 | do {
|
|---|
| 55 | bits++;
|
|---|
| 56 | size = size >> 1;
|
|---|
| 57 | } while (size > 256);
|
|---|
| 58 |
|
|---|
| 59 | return bits;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | /** Get mode of the i-node.
|
|---|
| 63 | *
|
|---|
| 64 | * @param sb Superblock
|
|---|
| 65 | * @param inode I-node to load mode from
|
|---|
| 66 | *
|
|---|
| 67 | * @return Mode of the i-node
|
|---|
| 68 | *
|
|---|
| 69 | */
|
|---|
| 70 | uint32_t ext4_inode_get_mode(ext4_superblock_t *sb, ext4_inode_t *inode)
|
|---|
| 71 | {
|
|---|
| 72 | if (ext4_superblock_get_creator_os(sb) == EXT4_SUPERBLOCK_OS_HURD) {
|
|---|
| 73 | return ((uint32_t) uint16_t_le2host(inode->osd2.hurd2.mode_high)) << 16 |
|
|---|
| 74 | ((uint32_t) uint16_t_le2host(inode->mode));
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | return uint16_t_le2host(inode->mode);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /** Set mode of the i-node.
|
|---|
| 81 | *
|
|---|
| 82 | * @param sb Superblock
|
|---|
| 83 | * @param inode I-node to set mode to
|
|---|
| 84 | * @param mode Mode to set to i-node
|
|---|
| 85 | *
|
|---|
| 86 | */
|
|---|
| 87 | void ext4_inode_set_mode(ext4_superblock_t *sb, ext4_inode_t *inode, uint32_t mode)
|
|---|
| 88 | {
|
|---|
| 89 | inode->mode = host2uint16_t_le((mode << 16) >> 16);
|
|---|
| 90 |
|
|---|
| 91 | if (ext4_superblock_get_creator_os(sb) == EXT4_SUPERBLOCK_OS_HURD)
|
|---|
| 92 | inode->osd2.hurd2.mode_high = host2uint16_t_le(mode >> 16);
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /** Get ID of the i-node owner (user id).
|
|---|
| 96 | *
|
|---|
| 97 | * @param inode I-node to load uid from
|
|---|
| 98 | *
|
|---|
| 99 | * @return User ID of the i-node owner
|
|---|
| 100 | *
|
|---|
| 101 | */
|
|---|
| 102 | uint32_t ext4_inode_get_uid(ext4_inode_t *inode)
|
|---|
| 103 | {
|
|---|
| 104 | return uint32_t_le2host(inode->uid);
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | /** Set ID of the i-node owner.
|
|---|
| 108 | *
|
|---|
| 109 | * @param inode I-node to set uid to
|
|---|
| 110 | * @param uid ID of the i-node owner
|
|---|
| 111 | *
|
|---|
| 112 | */
|
|---|
| 113 | void ext4_inode_set_uid(ext4_inode_t *inode, uint32_t uid)
|
|---|
| 114 | {
|
|---|
| 115 | inode->uid = host2uint32_t_le(uid);
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | /** Get real i-node size.
|
|---|
| 119 | *
|
|---|
| 120 | * @param sb Superblock
|
|---|
| 121 | * @param inode I-node to load size from
|
|---|
| 122 | *
|
|---|
| 123 | * @return Real size of i-node
|
|---|
| 124 | *
|
|---|
| 125 | */
|
|---|
| 126 | uint64_t ext4_inode_get_size(ext4_superblock_t *sb, ext4_inode_t *inode)
|
|---|
| 127 | {
|
|---|
| 128 | uint32_t major_rev = ext4_superblock_get_rev_level(sb);
|
|---|
| 129 |
|
|---|
| 130 | if ((major_rev > 0) &&
|
|---|
| 131 | (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)))
|
|---|
| 132 | return ((uint64_t)uint32_t_le2host(inode->size_hi)) << 32 |
|
|---|
| 133 | ((uint64_t)uint32_t_le2host(inode->size_lo));
|
|---|
| 134 |
|
|---|
| 135 | return uint32_t_le2host(inode->size_lo);
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | /** Set real i-node size.
|
|---|
| 139 | *
|
|---|
| 140 | * @param inode I-node to set size to
|
|---|
| 141 | * @param size Size of the i-node
|
|---|
| 142 | *
|
|---|
| 143 | */
|
|---|
| 144 | void ext4_inode_set_size(ext4_inode_t *inode, uint64_t size)
|
|---|
| 145 | {
|
|---|
| 146 | inode->size_lo = host2uint32_t_le((size << 32) >> 32);
|
|---|
| 147 | inode->size_hi = host2uint32_t_le(size >> 32);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /** Get time, when i-node was last accessed.
|
|---|
| 151 | *
|
|---|
| 152 | * @param inode I-node
|
|---|
| 153 | *
|
|---|
| 154 | * @return Time of the last access (POSIX)
|
|---|
| 155 | *
|
|---|
| 156 | */
|
|---|
| 157 | uint32_t ext4_inode_get_access_time(ext4_inode_t *inode)
|
|---|
| 158 | {
|
|---|
| 159 | return uint32_t_le2host(inode->access_time);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | /** Set time, when i-node was last accessed.
|
|---|
| 163 | *
|
|---|
| 164 | * @param inode I-node
|
|---|
| 165 | * @param time Time of the last access (POSIX)
|
|---|
| 166 | *
|
|---|
| 167 | */
|
|---|
| 168 | void ext4_inode_set_access_time(ext4_inode_t *inode, uint32_t time)
|
|---|
| 169 | {
|
|---|
| 170 | inode->access_time = host2uint32_t_le(time);
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | /** Get time, when i-node was last changed.
|
|---|
| 174 | *
|
|---|
| 175 | * @param inode I-node
|
|---|
| 176 | *
|
|---|
| 177 | * @return Time of the last change (POSIX)
|
|---|
| 178 | *
|
|---|
| 179 | */
|
|---|
| 180 | uint32_t ext4_inode_get_change_inode_time(ext4_inode_t *inode)
|
|---|
| 181 | {
|
|---|
| 182 | return uint32_t_le2host(inode->change_inode_time);
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | /** Set time, when i-node was last changed.
|
|---|
| 186 | *
|
|---|
| 187 | * @param inode I-node
|
|---|
| 188 | * @param time Time of the last change (POSIX)
|
|---|
| 189 | *
|
|---|
| 190 | */
|
|---|
| 191 | void ext4_inode_set_change_inode_time(ext4_inode_t *inode, uint32_t time)
|
|---|
| 192 | {
|
|---|
| 193 | inode->change_inode_time = host2uint32_t_le(time);
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /** Get time, when i-node content was last modified.
|
|---|
| 197 | *
|
|---|
| 198 | * @param inode I-node
|
|---|
| 199 | *
|
|---|
| 200 | * @return Time of the last content modification (POSIX)
|
|---|
| 201 | *
|
|---|
| 202 | */
|
|---|
| 203 | uint32_t ext4_inode_get_modification_time(ext4_inode_t *inode)
|
|---|
| 204 | {
|
|---|
| 205 | return uint32_t_le2host(inode->modification_time);
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | /** Set time, when i-node content was last modified.
|
|---|
| 209 | *
|
|---|
| 210 | * @param inode I-node
|
|---|
| 211 | * @param time Time of the last content modification (POSIX)
|
|---|
| 212 | *
|
|---|
| 213 | */
|
|---|
| 214 | void ext4_inode_set_modification_time(ext4_inode_t *inode, uint32_t time)
|
|---|
| 215 | {
|
|---|
| 216 | inode->modification_time = host2uint32_t_le(time);
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | /** Get time, when i-node was deleted.
|
|---|
| 220 | *
|
|---|
| 221 | * @param inode I-node
|
|---|
| 222 | *
|
|---|
| 223 | * @return Time of the delete action (POSIX)
|
|---|
| 224 | *
|
|---|
| 225 | */
|
|---|
| 226 | uint32_t ext4_inode_get_deletion_time(ext4_inode_t *inode)
|
|---|
| 227 | {
|
|---|
| 228 | return uint32_t_le2host(inode->deletion_time);
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | /** Set time, when i-node was deleted.
|
|---|
| 232 | *
|
|---|
| 233 | * @param inode I-node
|
|---|
| 234 | * @param time Time of the delete action (POSIX)
|
|---|
| 235 | *
|
|---|
| 236 | */
|
|---|
| 237 | void ext4_inode_set_deletion_time(ext4_inode_t *inode, uint32_t time)
|
|---|
| 238 | {
|
|---|
| 239 | inode->deletion_time = host2uint32_t_le(time);
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | /** Get ID of the i-node owner's group.
|
|---|
| 243 | *
|
|---|
| 244 | * @param inode I-node to load gid from
|
|---|
| 245 | *
|
|---|
| 246 | * @return Group ID of the i-node owner
|
|---|
| 247 | *
|
|---|
| 248 | */
|
|---|
| 249 | uint32_t ext4_inode_get_gid(ext4_inode_t *inode)
|
|---|
| 250 | {
|
|---|
| 251 | return uint32_t_le2host(inode->gid);
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | /** Set ID ot the i-node owner's group.
|
|---|
| 255 | *
|
|---|
| 256 | * @param inode I-node to set gid to
|
|---|
| 257 | * @param gid Group ID of the i-node owner
|
|---|
| 258 | *
|
|---|
| 259 | */
|
|---|
| 260 | void ext4_inode_set_gid(ext4_inode_t *inode, uint32_t gid)
|
|---|
| 261 | {
|
|---|
| 262 | inode->gid = host2uint32_t_le(gid);
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | /** Get number of links to i-node.
|
|---|
| 266 | *
|
|---|
| 267 | * @param inode I-node to load number of links from
|
|---|
| 268 | *
|
|---|
| 269 | * @return Number of links to i-node
|
|---|
| 270 | *
|
|---|
| 271 | */
|
|---|
| 272 | uint16_t ext4_inode_get_links_count(ext4_inode_t *inode)
|
|---|
| 273 | {
|
|---|
| 274 | return uint16_t_le2host(inode->links_count);
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | /** Set number of links to i-node.
|
|---|
| 278 | *
|
|---|
| 279 | * @param inode I-node to set number of links to
|
|---|
| 280 | * @param count Number of links to i-node
|
|---|
| 281 | *
|
|---|
| 282 | */
|
|---|
| 283 | void ext4_inode_set_links_count(ext4_inode_t *inode, uint16_t count)
|
|---|
| 284 | {
|
|---|
| 285 | inode->links_count = host2uint16_t_le(count);
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | /** Get number of 512-bytes blocks used for i-node.
|
|---|
| 289 | *
|
|---|
| 290 | * @param sb Superblock
|
|---|
| 291 | * @param inode I-node
|
|---|
| 292 | *
|
|---|
| 293 | * @return Number of 512-bytes blocks
|
|---|
| 294 | *
|
|---|
| 295 | */
|
|---|
| 296 | uint64_t ext4_inode_get_blocks_count(ext4_superblock_t *sb, ext4_inode_t *inode)
|
|---|
| 297 | {
|
|---|
| 298 | if (ext4_superblock_has_feature_read_only(sb,
|
|---|
| 299 | EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
|
|---|
| 300 | /* 48-bit field */
|
|---|
| 301 | uint64_t count = ((uint64_t)
|
|---|
| 302 | uint16_t_le2host(inode->osd2.linux2.blocks_high)) << 32 |
|
|---|
| 303 | uint32_t_le2host(inode->blocks_count_lo);
|
|---|
| 304 |
|
|---|
| 305 | if (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_HUGE_FILE)) {
|
|---|
| 306 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 307 | uint32_t block_bits = ext4_inode_block_bits_count(block_size);
|
|---|
| 308 | return count << (block_bits - 9);
|
|---|
| 309 | } else
|
|---|
| 310 | return count;
|
|---|
| 311 | } else
|
|---|
| 312 | return uint32_t_le2host(inode->blocks_count_lo);
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | /** Set number of 512-bytes blocks used for i-node.
|
|---|
| 316 | *
|
|---|
| 317 | * @param sb Superblock
|
|---|
| 318 | * @param inode I-node
|
|---|
| 319 | * @param count Number of 512-bytes blocks
|
|---|
| 320 | *
|
|---|
| 321 | * @return Error code
|
|---|
| 322 | *
|
|---|
| 323 | */
|
|---|
| 324 | int ext4_inode_set_blocks_count(ext4_superblock_t *sb, ext4_inode_t *inode,
|
|---|
| 325 | uint64_t count)
|
|---|
| 326 | {
|
|---|
| 327 | /* 32-bit maximum */
|
|---|
| 328 | uint64_t max = 0;
|
|---|
| 329 | max = ~max >> 32;
|
|---|
| 330 |
|
|---|
| 331 | if (count <= max) {
|
|---|
| 332 | inode->blocks_count_lo = host2uint32_t_le(count);
|
|---|
| 333 | inode->osd2.linux2.blocks_high = 0;
|
|---|
| 334 | ext4_inode_clear_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
|
|---|
| 335 |
|
|---|
| 336 | return EOK;
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | /* Check if there can be used huge files (many blocks) */
|
|---|
| 340 | if (!ext4_superblock_has_feature_read_only(sb,
|
|---|
| 341 | EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
|
|---|
| 342 | return EINVAL;
|
|---|
| 343 |
|
|---|
| 344 | /* 48-bit maximum */
|
|---|
| 345 | max = 0;
|
|---|
| 346 | max = ~max >> 16;
|
|---|
| 347 |
|
|---|
| 348 | if (count <= max) {
|
|---|
| 349 | inode->blocks_count_lo = host2uint32_t_le(count);
|
|---|
| 350 | inode->osd2.linux2.blocks_high = host2uint16_t_le(count >> 32);
|
|---|
| 351 | ext4_inode_clear_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
|
|---|
| 352 | } else {
|
|---|
| 353 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 354 | uint32_t block_bits = ext4_inode_block_bits_count(block_size);
|
|---|
| 355 | ext4_inode_set_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
|
|---|
| 356 | count = count >> (block_bits - 9);
|
|---|
| 357 | inode->blocks_count_lo = host2uint32_t_le(count);
|
|---|
| 358 | inode->osd2.linux2.blocks_high = host2uint16_t_le(count >> 32);
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 | return EOK;
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | /** Get flags (features) of i-node.
|
|---|
| 365 | *
|
|---|
| 366 | * @param inode I-node to get flags from
|
|---|
| 367 | *
|
|---|
| 368 | * @return Flags (bitmap)
|
|---|
| 369 | *
|
|---|
| 370 | */
|
|---|
| 371 | uint32_t ext4_inode_get_flags(ext4_inode_t *inode)
|
|---|
| 372 | {
|
|---|
| 373 | return uint32_t_le2host(inode->flags);
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| 376 | /** Set flags (features) of i-node.
|
|---|
| 377 | *
|
|---|
| 378 | * @param inode I-node to set flags to
|
|---|
| 379 | * @param flags Flags to set to i-node
|
|---|
| 380 | *
|
|---|
| 381 | */
|
|---|
| 382 | void ext4_inode_set_flags(ext4_inode_t *inode, uint32_t flags)
|
|---|
| 383 | {
|
|---|
| 384 | inode->flags = host2uint32_t_le(flags);
|
|---|
| 385 | }
|
|---|
| 386 |
|
|---|
| 387 | /** Get file generation (used by NFS).
|
|---|
| 388 | *
|
|---|
| 389 | * @param inode I-node
|
|---|
| 390 | *
|
|---|
| 391 | * @return File generation
|
|---|
| 392 | *
|
|---|
| 393 | */
|
|---|
| 394 | uint32_t ext4_inode_get_generation(ext4_inode_t *inode)
|
|---|
| 395 | {
|
|---|
| 396 | return uint32_t_le2host(inode->generation);
|
|---|
| 397 | }
|
|---|
| 398 |
|
|---|
| 399 | /** Set file generation (used by NFS).
|
|---|
| 400 | *
|
|---|
| 401 | * @param inode I-node
|
|---|
| 402 | * @param generation File generation
|
|---|
| 403 | *
|
|---|
| 404 | */
|
|---|
| 405 | void ext4_inode_set_generation(ext4_inode_t *inode, uint32_t generation)
|
|---|
| 406 | {
|
|---|
| 407 | inode->generation = host2uint32_t_le(generation);
|
|---|
| 408 | }
|
|---|
| 409 |
|
|---|
| 410 | /** Get address of block, where are extended attributes located.
|
|---|
| 411 | *
|
|---|
| 412 | * @param inode I-node
|
|---|
| 413 | * @param sb Superblock
|
|---|
| 414 | *
|
|---|
| 415 | * @return Block address
|
|---|
| 416 | *
|
|---|
| 417 | */
|
|---|
| 418 | uint64_t ext4_inode_get_file_acl(ext4_inode_t *inode, ext4_superblock_t *sb)
|
|---|
| 419 | {
|
|---|
| 420 | if (ext4_superblock_get_creator_os(sb) == EXT4_SUPERBLOCK_OS_LINUX)
|
|---|
| 421 | return ((uint32_t)
|
|---|
| 422 | uint16_t_le2host(inode->osd2.linux2.file_acl_high)) << 16 |
|
|---|
| 423 | (uint32_t_le2host(inode->file_acl_lo));
|
|---|
| 424 |
|
|---|
| 425 | return uint32_t_le2host(inode->file_acl_lo);
|
|---|
| 426 | }
|
|---|
| 427 |
|
|---|
| 428 | /** Set address of block, where are extended attributes located.
|
|---|
| 429 | *
|
|---|
| 430 | * @param inode I-node
|
|---|
| 431 | * @param sb Superblock
|
|---|
| 432 | * @param file_acl Block address
|
|---|
| 433 | *
|
|---|
| 434 | */
|
|---|
| 435 | void ext4_inode_set_file_acl(ext4_inode_t *inode, ext4_superblock_t *sb,
|
|---|
| 436 | uint64_t file_acl)
|
|---|
| 437 | {
|
|---|
| 438 | inode->file_acl_lo = host2uint32_t_le((file_acl << 32) >> 32);
|
|---|
| 439 |
|
|---|
| 440 | if (ext4_superblock_get_creator_os(sb) == EXT4_SUPERBLOCK_OS_LINUX)
|
|---|
| 441 | inode->osd2.linux2.file_acl_high = host2uint16_t_le(file_acl >> 32);
|
|---|
| 442 | }
|
|---|
| 443 |
|
|---|
| 444 | /** Get block address of specified direct block.
|
|---|
| 445 | *
|
|---|
| 446 | * @param inode I-node to load block from
|
|---|
| 447 | * @param idx Index of logical block
|
|---|
| 448 | *
|
|---|
| 449 | * @return Physical block address
|
|---|
| 450 | *
|
|---|
| 451 | */
|
|---|
| 452 | uint32_t ext4_inode_get_direct_block(ext4_inode_t *inode, uint32_t idx)
|
|---|
| 453 | {
|
|---|
| 454 | assert(idx < EXT4_INODE_DIRECT_BLOCK_COUNT);
|
|---|
| 455 |
|
|---|
| 456 | return uint32_t_le2host(inode->blocks[idx]);
|
|---|
| 457 | }
|
|---|
| 458 |
|
|---|
| 459 | /** Set block address of specified direct block.
|
|---|
| 460 | *
|
|---|
| 461 | * @param inode I-node to set block address to
|
|---|
| 462 | * @param idx Index of logical block
|
|---|
| 463 | * @param fblock Physical block address
|
|---|
| 464 | *
|
|---|
| 465 | */
|
|---|
| 466 | void ext4_inode_set_direct_block(ext4_inode_t *inode, uint32_t idx, uint32_t fblock)
|
|---|
| 467 | {
|
|---|
| 468 | assert(idx < EXT4_INODE_DIRECT_BLOCK_COUNT);
|
|---|
| 469 |
|
|---|
| 470 | inode->blocks[idx] = host2uint32_t_le(fblock);
|
|---|
| 471 | }
|
|---|
| 472 |
|
|---|
| 473 | /** Get block address of specified indirect block.
|
|---|
| 474 | *
|
|---|
| 475 | * @param inode I-node to get block address from
|
|---|
| 476 | * @param idx Index of indirect block
|
|---|
| 477 | *
|
|---|
| 478 | * @return Physical block address
|
|---|
| 479 | *
|
|---|
| 480 | */
|
|---|
| 481 | uint32_t ext4_inode_get_indirect_block(ext4_inode_t *inode, uint32_t idx)
|
|---|
| 482 | {
|
|---|
| 483 | return uint32_t_le2host(inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK]);
|
|---|
| 484 | }
|
|---|
| 485 |
|
|---|
| 486 | /** Set block address of specified indirect block.
|
|---|
| 487 | *
|
|---|
| 488 | * @param inode I-node to set block address to
|
|---|
| 489 | * @param idx Index of indirect block
|
|---|
| 490 | * @param fblock Physical block address
|
|---|
| 491 | *
|
|---|
| 492 | */
|
|---|
| 493 | void ext4_inode_set_indirect_block(ext4_inode_t *inode, uint32_t idx,
|
|---|
| 494 | uint32_t fblock)
|
|---|
| 495 | {
|
|---|
| 496 | inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK] =
|
|---|
| 497 | host2uint32_t_le(fblock);
|
|---|
| 498 | }
|
|---|
| 499 |
|
|---|
| 500 | /** Check if i-node has specified type.
|
|---|
| 501 | *
|
|---|
| 502 | * @param sb Superblock
|
|---|
| 503 | * @param inode I-node to check type of
|
|---|
| 504 | * @param type Type to check
|
|---|
| 505 | *
|
|---|
| 506 | * @return Result of check operation
|
|---|
| 507 | *
|
|---|
| 508 | */
|
|---|
| 509 | bool ext4_inode_is_type(ext4_superblock_t *sb, ext4_inode_t *inode,
|
|---|
| 510 | uint32_t type)
|
|---|
| 511 | {
|
|---|
| 512 | uint32_t mode = ext4_inode_get_mode(sb, inode);
|
|---|
| 513 | return (mode & EXT4_INODE_MODE_TYPE_MASK) == type;
|
|---|
| 514 | }
|
|---|
| 515 |
|
|---|
| 516 | /** Get extent header from the root of the extent tree.
|
|---|
| 517 | *
|
|---|
| 518 | * @param inode I-node to get extent header from
|
|---|
| 519 | *
|
|---|
| 520 | * @return Pointer to extent header of the root node
|
|---|
| 521 | *
|
|---|
| 522 | */
|
|---|
| 523 | ext4_extent_header_t * ext4_inode_get_extent_header(ext4_inode_t *inode)
|
|---|
| 524 | {
|
|---|
| 525 | return (ext4_extent_header_t *) inode->blocks;
|
|---|
| 526 | }
|
|---|
| 527 |
|
|---|
| 528 | /** Check if i-node has specified flag.
|
|---|
| 529 | *
|
|---|
| 530 | * @param inode I-node to check flags of
|
|---|
| 531 | * @param flag Flag to check
|
|---|
| 532 | *
|
|---|
| 533 | * @return Result of check operation
|
|---|
| 534 | *
|
|---|
| 535 | */
|
|---|
| 536 | bool ext4_inode_has_flag(ext4_inode_t *inode, uint32_t flag)
|
|---|
| 537 | {
|
|---|
| 538 | if (ext4_inode_get_flags(inode) & flag)
|
|---|
| 539 | return true;
|
|---|
| 540 |
|
|---|
| 541 | return false;
|
|---|
| 542 | }
|
|---|
| 543 |
|
|---|
| 544 | /** Remove specified flag from i-node.
|
|---|
| 545 | *
|
|---|
| 546 | * @param inode I-node to clear flag on
|
|---|
| 547 | * @param clear_flag Flag to be cleared
|
|---|
| 548 | *
|
|---|
| 549 | */
|
|---|
| 550 | void ext4_inode_clear_flag(ext4_inode_t *inode, uint32_t clear_flag)
|
|---|
| 551 | {
|
|---|
| 552 | uint32_t flags = ext4_inode_get_flags(inode);
|
|---|
| 553 | flags = flags & (~clear_flag);
|
|---|
| 554 | ext4_inode_set_flags(inode, flags);
|
|---|
| 555 | }
|
|---|
| 556 |
|
|---|
| 557 | /** Set specified flag to i-node.
|
|---|
| 558 | *
|
|---|
| 559 | * @param inode I-node to set flag on
|
|---|
| 560 | * @param set_flag Flag to be set
|
|---|
| 561 | *
|
|---|
| 562 | */
|
|---|
| 563 | void ext4_inode_set_flag(ext4_inode_t *inode, uint32_t set_flag)
|
|---|
| 564 | {
|
|---|
| 565 | uint32_t flags = ext4_inode_get_flags(inode);
|
|---|
| 566 | flags = flags | set_flag;
|
|---|
| 567 | ext4_inode_set_flags(inode, flags);
|
|---|
| 568 | }
|
|---|
| 569 |
|
|---|
| 570 | /** Check if i-node can be truncated.
|
|---|
| 571 | *
|
|---|
| 572 | * @param sb Superblock
|
|---|
| 573 | * @param inode I-node to check
|
|---|
| 574 | *
|
|---|
| 575 | * @return Result of the check operation
|
|---|
| 576 | *
|
|---|
| 577 | */
|
|---|
| 578 | bool ext4_inode_can_truncate(ext4_superblock_t *sb, ext4_inode_t *inode)
|
|---|
| 579 | {
|
|---|
| 580 | if ((ext4_inode_has_flag(inode, EXT4_INODE_FLAG_APPEND)) ||
|
|---|
| 581 | (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_IMMUTABLE)))
|
|---|
| 582 | return false;
|
|---|
| 583 |
|
|---|
| 584 | if ((ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)) ||
|
|---|
| 585 | (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_DIRECTORY)))
|
|---|
| 586 | return true;
|
|---|
| 587 |
|
|---|
| 588 | return false;
|
|---|
| 589 | }
|
|---|
| 590 |
|
|---|
| 591 | /**
|
|---|
| 592 | * @}
|
|---|
| 593 | */
|
|---|