[eb91db7] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 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_inode.c
|
---|
[c25e39b] | 35 | * @brief Ext4 inode structure operations.
|
---|
[eb91db7] | 36 | */
|
---|
| 37 |
|
---|
[9c0c0e1] | 38 | #include <byteorder.h>
|
---|
[1a7756a] | 39 | #include <errno.h>
|
---|
| 40 | #include <libblock.h>
|
---|
[3711e7e] | 41 | #include "libext4.h"
|
---|
[6c501f8] | 42 |
|
---|
[b3d7277] | 43 | static uint32_t ext4_inode_block_bits_count(uint32_t block_size)
|
---|
| 44 | {
|
---|
| 45 | uint32_t bits = 8;
|
---|
| 46 | uint32_t size = block_size;
|
---|
| 47 |
|
---|
| 48 | do {
|
---|
| 49 | bits++;
|
---|
| 50 | size = size >> 1;
|
---|
| 51 | } while (size > 256);
|
---|
| 52 |
|
---|
| 53 | return bits;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[9b9d37bb] | 56 | uint32_t ext4_inode_get_mode(ext4_superblock_t *sb, ext4_inode_t *inode)
|
---|
| 57 | {
|
---|
| 58 | if (ext4_superblock_get_creator_os(sb) == EXT4_SUPERBLOCK_OS_HURD) {
|
---|
| 59 | return ((uint32_t)uint16_t_le2host(inode->osd2.hurd2.mode_high)) << 16 |
|
---|
| 60 | ((uint32_t)uint16_t_le2host(inode->mode));
|
---|
| 61 | }
|
---|
| 62 | return uint16_t_le2host(inode->mode);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[fe27eb4] | 65 | void ext4_inode_set_mode(ext4_superblock_t *sb, ext4_inode_t *inode, uint32_t mode)
|
---|
[9b9d37bb] | 66 | {
|
---|
[fe27eb4] | 67 | inode->mode = host2uint16_t_le((mode << 16) >> 16);
|
---|
| 68 |
|
---|
| 69 | if (ext4_superblock_get_creator_os(sb) == EXT4_SUPERBLOCK_OS_HURD) {
|
---|
| 70 | inode->osd2.hurd2.mode_high = host2uint16_t_le(mode >> 16);
|
---|
| 71 | }
|
---|
[9b9d37bb] | 72 | }
|
---|
| 73 |
|
---|
[3712434] | 74 | uint32_t ext4_inode_get_uid(ext4_inode_t *inode)
|
---|
[fe27eb4] | 75 | {
|
---|
| 76 | return uint32_t_le2host(inode->uid);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | void ext4_inode_set_uid(ext4_inode_t *inode, uint32_t uid)
|
---|
| 80 | {
|
---|
| 81 | inode->uid = host2uint32_t_le(uid);
|
---|
| 82 | }
|
---|
[3712434] | 83 |
|
---|
[9b9d37bb] | 84 | uint64_t ext4_inode_get_size(ext4_superblock_t *sb, ext4_inode_t *inode)
|
---|
[3712434] | 85 | {
|
---|
[9b9d37bb] | 86 | uint32_t major_rev = ext4_superblock_get_rev_level(sb);
|
---|
| 87 |
|
---|
[acd869e] | 88 | if ((major_rev > 0) && ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)) {
|
---|
[9b9d37bb] | 89 | return ((uint64_t)uint32_t_le2host(inode->size_hi)) << 32 |
|
---|
| 90 | ((uint64_t)uint32_t_le2host(inode->size_lo));
|
---|
[052e82d] | 91 | }
|
---|
[9b9d37bb] | 92 | return uint32_t_le2host(inode->size_lo);
|
---|
[3712434] | 93 | }
|
---|
| 94 |
|
---|
[052e82d] | 95 | void ext4_inode_set_size(ext4_inode_t *inode, uint64_t value) {
|
---|
| 96 | inode->size_lo = host2uint32_t_le((value << 32) >> 32);
|
---|
| 97 | inode->size_hi = host2uint32_t_le(value >> 32);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[fe27eb4] | 100 | uint32_t ext4_inode_get_access_time(ext4_inode_t *inode)
|
---|
| 101 | {
|
---|
| 102 | return uint32_t_le2host(inode->access_time);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | void ext4_inode_set_access_time(ext4_inode_t *inode, uint32_t time)
|
---|
| 106 | {
|
---|
| 107 | inode->access_time = host2uint32_t_le(time);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | uint32_t ext4_inode_get_change_inode_time(ext4_inode_t *inode)
|
---|
| 111 | {
|
---|
| 112 | return uint32_t_le2host(inode->change_inode_time);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | void ext4_inode_set_change_inode_time(ext4_inode_t *inode, uint32_t time)
|
---|
| 116 | {
|
---|
| 117 | inode->change_inode_time = host2uint32_t_le(time);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | uint32_t ext4_inode_get_modification_time(ext4_inode_t *inode)
|
---|
| 121 | {
|
---|
| 122 | return uint32_t_le2host(inode->modification_time);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | void ext4_inode_set_modification_time(ext4_inode_t *inode, uint32_t time)
|
---|
| 126 | {
|
---|
| 127 | inode->modification_time = host2uint32_t_le(time);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | uint32_t ext4_inode_get_deletion_time(ext4_inode_t *inode)
|
---|
| 131 | {
|
---|
| 132 | return uint32_t_le2host(inode->deletion_time);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | void ext4_inode_set_deletion_time(ext4_inode_t *inode, uint32_t time)
|
---|
| 136 | {
|
---|
| 137 | inode->deletion_time = host2uint32_t_le(time);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | uint32_t ext4_inode_get_gid(ext4_inode_t *inode)
|
---|
| 141 | {
|
---|
| 142 | return uint32_t_le2host(inode->gid);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | void ext4_inode_set_gid(ext4_inode_t *inode, uint32_t gid)
|
---|
| 146 | {
|
---|
| 147 | inode->gid = host2uint32_t_le(gid);
|
---|
| 148 | }
|
---|
[3712434] | 149 |
|
---|
| 150 | uint16_t ext4_inode_get_links_count(ext4_inode_t *inode)
|
---|
[6c501f8] | 151 | {
|
---|
[9c0c0e1] | 152 | return uint16_t_le2host(inode->links_count);
|
---|
[6c501f8] | 153 | }
|
---|
| 154 |
|
---|
[fe27eb4] | 155 | void ext4_inode_set_links_count(ext4_inode_t *inode, uint16_t count)
|
---|
| 156 | {
|
---|
| 157 | inode->links_count = host2uint16_t_le(count);
|
---|
| 158 | }
|
---|
[b3d7277] | 159 |
|
---|
| 160 | uint64_t ext4_inode_get_blocks_count(ext4_superblock_t *sb, ext4_inode_t *inode)
|
---|
| 161 | {
|
---|
| 162 | if (ext4_superblock_has_feature_read_only(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
|
---|
| 163 |
|
---|
| 164 | /* 48-bit field */
|
---|
[d9bbe45] | 165 | uint64_t count = ((uint64_t)uint16_t_le2host(inode->osd2.linux2.blocks_high)) << 32 |
|
---|
[b3d7277] | 166 | uint32_t_le2host(inode->blocks_count_lo);
|
---|
| 167 |
|
---|
| 168 | if (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_HUGE_FILE)) {
|
---|
| 169 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
| 170 | uint32_t block_bits = ext4_inode_block_bits_count(block_size);
|
---|
| 171 | return count << (block_bits - 9);
|
---|
| 172 | } else {
|
---|
| 173 | return count;
|
---|
| 174 | }
|
---|
| 175 | } else {
|
---|
| 176 | return uint32_t_le2host(inode->blocks_count_lo);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | int ext4_inode_set_blocks_count(ext4_superblock_t *sb, ext4_inode_t *inode,
|
---|
| 182 | uint64_t count)
|
---|
| 183 | {
|
---|
| 184 | // 32-bit maximum
|
---|
| 185 | uint64_t max = 0;
|
---|
| 186 | max = ~max >> 32;
|
---|
| 187 |
|
---|
| 188 | if (count <= max) {
|
---|
| 189 | inode->blocks_count_lo = host2uint32_t_le(count);
|
---|
| 190 | inode->osd2.linux2.blocks_high = 0;
|
---|
| 191 | ext4_inode_clear_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
|
---|
| 192 | return EOK;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | if (!ext4_superblock_has_feature_read_only(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
|
---|
| 196 | return EINVAL;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | // 48-bit maximum
|
---|
| 200 | max = 0;
|
---|
| 201 | max = ~max >> 16;
|
---|
| 202 |
|
---|
| 203 | if (count <= max) {
|
---|
| 204 | inode->blocks_count_lo = host2uint32_t_le(count);
|
---|
| 205 | inode->osd2.linux2.blocks_high = host2uint16_t_le(count >> 32);
|
---|
| 206 | ext4_inode_clear_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
|
---|
| 207 | } else {
|
---|
| 208 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
| 209 | uint32_t block_bits = ext4_inode_block_bits_count(block_size);
|
---|
| 210 | ext4_inode_set_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
|
---|
| 211 | count = count >> (block_bits - 9);
|
---|
| 212 | inode->blocks_count_lo = host2uint32_t_le(count);
|
---|
| 213 | inode->osd2.linux2.blocks_high = host2uint16_t_le(count >> 32);
|
---|
| 214 | }
|
---|
| 215 | return EOK;
|
---|
| 216 | }
|
---|
[eb91db7] | 217 |
|
---|
[7b9381b] | 218 | uint32_t ext4_inode_get_flags(ext4_inode_t *inode) {
|
---|
| 219 | return uint32_t_le2host(inode->flags);
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[b3d7277] | 222 | void ext4_inode_set_flags(ext4_inode_t *inode, uint32_t flags) {
|
---|
| 223 | inode->flags = host2uint32_t_le(flags);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
[fe27eb4] | 226 | uint32_t ext4_inode_get_generation(ext4_inode_t *inode)
|
---|
| 227 | {
|
---|
| 228 | return uint32_t_le2host(inode->generation);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | void ext4_inode_set_generation(ext4_inode_t *inode, uint32_t generation)
|
---|
| 232 | {
|
---|
| 233 | inode->generation = host2uint32_t_le(generation);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[343ccfd] | 236 | uint64_t ext4_get_inode_file_acl(ext4_inode_t *inode, ext4_superblock_t *sb)
|
---|
| 237 | {
|
---|
| 238 | if (ext4_superblock_get_creator_os(sb) == EXT4_SUPERBLOCK_OS_LINUX) {
|
---|
| 239 | return ((uint32_t)uint16_t_le2host(inode->osd2.linux2.file_acl_high)) << 16 |
|
---|
| 240 | (uint32_t_le2host(inode->file_acl_lo));
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | return uint32_t_le2host(inode->file_acl_lo);
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | void ext4_set_inode_file_acl(ext4_inode_t *inode, ext4_superblock_t *sb,
|
---|
| 247 | uint64_t file_acl)
|
---|
| 248 | {
|
---|
| 249 | inode->file_acl_lo = host2uint32_t_le((file_acl << 32) >> 32);
|
---|
| 250 |
|
---|
| 251 | if (ext4_superblock_get_creator_os(sb) == EXT4_SUPERBLOCK_OS_LINUX) {
|
---|
| 252 | inode->osd2.linux2.file_acl_high = host2uint16_t_le(file_acl >> 32);
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
| 255 |
|
---|
[fe27eb4] | 256 | /***********************************************************************/
|
---|
| 257 |
|
---|
[1e65444] | 258 | uint32_t ext4_inode_get_direct_block(ext4_inode_t *inode, uint32_t idx)
|
---|
[9b9d37bb] | 259 | {
|
---|
| 260 | assert(idx < EXT4_INODE_DIRECT_BLOCK_COUNT);
|
---|
| 261 | return uint32_t_le2host(inode->blocks[idx]);
|
---|
| 262 | }
|
---|
| 263 |
|
---|
[1e65444] | 264 | void ext4_inode_set_direct_block(ext4_inode_t *inode, uint32_t idx, uint32_t fblock)
|
---|
[d5a78e28] | 265 | {
|
---|
| 266 | assert(idx < EXT4_INODE_DIRECT_BLOCK_COUNT);
|
---|
| 267 | inode->blocks[idx] = host2uint32_t_le(fblock);
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[1e65444] | 270 | uint32_t ext4_inode_get_indirect_block(ext4_inode_t *inode, uint32_t idx)
|
---|
[9b9d37bb] | 271 | {
|
---|
| 272 | return uint32_t_le2host(inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK]);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
[1e65444] | 275 | void ext4_inode_set_indirect_block(ext4_inode_t *inode, uint32_t idx, uint32_t fblock)
|
---|
| 276 | {
|
---|
| 277 | inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK] = host2uint32_t_le(fblock);
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 |
|
---|
[1a7756a] | 281 | uint32_t ext4_inode_get_extent_block(ext4_inode_t *inode, uint64_t idx, service_id_t service_id)
|
---|
[acd869e] | 282 | {
|
---|
[1a7756a] | 283 | int rc;
|
---|
[d9bbe45] | 284 |
|
---|
[1a7756a] | 285 | block_t* block = NULL;
|
---|
| 286 |
|
---|
[d9bbe45] | 287 | ext4_extent_header_t *header = ext4_inode_get_extent_header(inode);
|
---|
[1a7756a] | 288 | while (ext4_extent_header_get_depth(header) != 0) {
|
---|
| 289 |
|
---|
[d9bbe45] | 290 | ext4_extent_index_t *extent_index = EXT4_EXTENT_FIRST_INDEX(header);
|
---|
[8958a26] | 291 |
|
---|
| 292 | for (uint16_t i = 0; i < ext4_extent_header_get_entries_count(header); ++i) {
|
---|
[d9bbe45] | 293 | if (idx >= ext4_extent_index_get_first_block(extent_index)) {
|
---|
[1a7756a] | 294 |
|
---|
[d9bbe45] | 295 | uint64_t child = ext4_extent_index_get_leaf(extent_index);
|
---|
[1a7756a] | 296 |
|
---|
| 297 | if (block != NULL) {
|
---|
| 298 | block_put(block);
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | rc = block_get(&block, service_id, child, BLOCK_FLAGS_NONE);
|
---|
| 302 | if (rc != EOK) {
|
---|
| 303 | return 0;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | header = (ext4_extent_header_t *)block->data;
|
---|
| 307 | break;
|
---|
| 308 | }
|
---|
| 309 | }
|
---|
| 310 | }
|
---|
| 311 |
|
---|
[d9bbe45] | 312 | ext4_extent_t *extent = EXT4_EXTENT_FIRST(header);
|
---|
| 313 | uint64_t phys_block = 0;
|
---|
[1a7756a] | 314 |
|
---|
| 315 | for (uint16_t i = 0; i < ext4_extent_header_get_entries_count(header); ++i) {
|
---|
| 316 |
|
---|
[d9bbe45] | 317 | uint32_t first_block = ext4_extent_get_first_block(extent);
|
---|
| 318 | uint16_t block_count = ext4_extent_get_block_count(extent);
|
---|
[8958a26] | 319 |
|
---|
[1a7756a] | 320 | if ((idx >= first_block) && (idx < first_block + block_count)) {
|
---|
[d9bbe45] | 321 | phys_block = ext4_extent_get_start(extent) + idx;
|
---|
| 322 | phys_block -= ext4_extent_get_first_block(extent);
|
---|
| 323 |
|
---|
| 324 | // Memory leak prevention
|
---|
| 325 | if (block != NULL) {
|
---|
| 326 | block_put(block);
|
---|
| 327 | }
|
---|
| 328 | return phys_block;
|
---|
[8958a26] | 329 | }
|
---|
[1a7756a] | 330 | // Go to the next extent
|
---|
| 331 | ++extent;
|
---|
[8958a26] | 332 | }
|
---|
| 333 |
|
---|
[acd869e] | 334 |
|
---|
[1a7756a] | 335 | EXT4FS_DBG("ERROR - reached function end");
|
---|
| 336 | return phys_block;
|
---|
[acd869e] | 337 | }
|
---|
| 338 |
|
---|
[fe27eb4] | 339 | bool ext4_inode_is_type(ext4_superblock_t *sb, ext4_inode_t *inode, uint32_t type)
|
---|
| 340 | {
|
---|
| 341 | uint32_t mode = ext4_inode_get_mode(sb, inode);
|
---|
| 342 | return (mode & EXT4_INODE_MODE_TYPE_MASK) == type;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 |
|
---|
[acd869e] | 346 | ext4_extent_header_t * ext4_inode_get_extent_header(ext4_inode_t *inode)
|
---|
| 347 | {
|
---|
| 348 | return (ext4_extent_header_t *)inode->blocks;
|
---|
| 349 | }
|
---|
[7b9381b] | 350 |
|
---|
[b3d7277] | 351 | // Flags operations
|
---|
[7bc4508] | 352 | bool ext4_inode_has_flag(ext4_inode_t *inode, uint32_t flag)
|
---|
| 353 | {
|
---|
[7b9381b] | 354 | if (ext4_inode_get_flags(inode) & flag) {
|
---|
| 355 | return true;
|
---|
| 356 | }
|
---|
| 357 | return false;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
[b3d7277] | 360 | void ext4_inode_clear_flag(ext4_inode_t *inode, uint32_t clear_flag)
|
---|
| 361 | {
|
---|
| 362 | uint32_t flags = ext4_inode_get_flags(inode);
|
---|
| 363 | flags = flags & (~clear_flag);
|
---|
| 364 | ext4_inode_set_flags(inode, flags);
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | void ext4_inode_set_flag(ext4_inode_t *inode, uint32_t set_flag)
|
---|
| 368 | {
|
---|
| 369 | uint32_t flags = ext4_inode_get_flags(inode);
|
---|
| 370 | flags = flags | set_flag;
|
---|
| 371 | ext4_inode_set_flags(inode, flags);
|
---|
| 372 | }
|
---|
| 373 |
|
---|
[43a9968] | 374 | bool ext4_inode_can_truncate(ext4_superblock_t *sb, ext4_inode_t *inode)
|
---|
[12b4a7f] | 375 | {
|
---|
| 376 | if (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_APPEND)
|
---|
| 377 | || ext4_inode_has_flag(inode, EXT4_INODE_FLAG_IMMUTABLE)) {
|
---|
| 378 | return false;
|
---|
| 379 | }
|
---|
| 380 |
|
---|
[e31e56a1] | 381 | if (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)
|
---|
| 382 | || ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_DIRECTORY)) {
|
---|
[12b4a7f] | 383 | return true;
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | return false;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
[eb91db7] | 389 | /**
|
---|
| 390 | * @}
|
---|
| 391 | */
|
---|