[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 |
|
---|
[9b9d37bb] | 43 | uint32_t ext4_inode_get_mode(ext4_superblock_t *sb, ext4_inode_t *inode)
|
---|
| 44 | {
|
---|
| 45 | if (ext4_superblock_get_creator_os(sb) == EXT4_SUPERBLOCK_OS_HURD) {
|
---|
| 46 | return ((uint32_t)uint16_t_le2host(inode->osd2.hurd2.mode_high)) << 16 |
|
---|
| 47 | ((uint32_t)uint16_t_le2host(inode->mode));
|
---|
| 48 | }
|
---|
| 49 | return uint16_t_le2host(inode->mode);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | bool ext4_inode_is_type(ext4_superblock_t *sb, ext4_inode_t *inode, uint32_t type)
|
---|
| 53 | {
|
---|
| 54 | uint32_t mode = ext4_inode_get_mode(sb, inode);
|
---|
| 55 | return (mode & EXT4_INODE_MODE_TYPE_MASK) == type;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[3712434] | 58 | /*
|
---|
| 59 | uint32_t ext4_inode_get_uid(ext4_inode_t *inode)
|
---|
| 60 | */
|
---|
| 61 |
|
---|
[9b9d37bb] | 62 | uint64_t ext4_inode_get_size(ext4_superblock_t *sb, ext4_inode_t *inode)
|
---|
[3712434] | 63 | {
|
---|
[9b9d37bb] | 64 | uint32_t major_rev = ext4_superblock_get_rev_level(sb);
|
---|
| 65 |
|
---|
[acd869e] | 66 | if ((major_rev > 0) && ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)) {
|
---|
[9b9d37bb] | 67 | return ((uint64_t)uint32_t_le2host(inode->size_hi)) << 32 |
|
---|
| 68 | ((uint64_t)uint32_t_le2host(inode->size_lo));
|
---|
[052e82d] | 69 | }
|
---|
[9b9d37bb] | 70 | return uint32_t_le2host(inode->size_lo);
|
---|
[3712434] | 71 | }
|
---|
| 72 |
|
---|
[052e82d] | 73 | void ext4_inode_set_size(ext4_inode_t *inode, uint64_t value) {
|
---|
| 74 | inode->size_lo = host2uint32_t_le((value << 32) >> 32);
|
---|
| 75 | inode->size_hi = host2uint32_t_le(value >> 32);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[3712434] | 78 | /*
|
---|
| 79 | extern uint32_t ext4_inode_get_access_time(ext4_inode_t *);
|
---|
| 80 | extern uint32_t ext4_inode_get_change_inode_time(ext4_inode_t *);
|
---|
| 81 | extern uint32_t ext4_inode_get_modification_time(ext4_inode_t *);
|
---|
| 82 | extern uint32_t ext4_inode_get_deletion_time(ext4_inode_t *);
|
---|
| 83 | extern uint32_t ext4_inode_get_gid(ext4_inode_t *);
|
---|
| 84 | */
|
---|
| 85 |
|
---|
| 86 | uint16_t ext4_inode_get_links_count(ext4_inode_t *inode)
|
---|
[6c501f8] | 87 | {
|
---|
[9c0c0e1] | 88 | return uint16_t_le2host(inode->links_count);
|
---|
[6c501f8] | 89 | }
|
---|
| 90 |
|
---|
[3712434] | 91 | /*
|
---|
| 92 | extern uint64_t ext4_inode_get_blocks_count(ext4_inode_t *);
|
---|
| 93 | */
|
---|
[eb91db7] | 94 |
|
---|
[7b9381b] | 95 | uint32_t ext4_inode_get_flags(ext4_inode_t *inode) {
|
---|
| 96 | return uint32_t_le2host(inode->flags);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[9b9d37bb] | 99 | uint32_t ext4_inode_get_direct_block(ext4_inode_t *inode, uint8_t idx)
|
---|
| 100 | {
|
---|
| 101 | assert(idx < EXT4_INODE_DIRECT_BLOCK_COUNT);
|
---|
| 102 | return uint32_t_le2host(inode->blocks[idx]);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[d5a78e28] | 105 | void ext4_inode_set_direct_block(ext4_inode_t *inode, uint8_t idx, uint32_t fblock)
|
---|
| 106 | {
|
---|
| 107 | assert(idx < EXT4_INODE_DIRECT_BLOCK_COUNT);
|
---|
| 108 | inode->blocks[idx] = host2uint32_t_le(fblock);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[9b9d37bb] | 111 | uint32_t ext4_inode_get_indirect_block(ext4_inode_t *inode, uint8_t idx)
|
---|
| 112 | {
|
---|
| 113 | assert(idx < EXT4_INODE_INDIRECT_BLOCK_COUNT);
|
---|
| 114 | return uint32_t_le2host(inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK]);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[1a7756a] | 117 | uint32_t ext4_inode_get_extent_block(ext4_inode_t *inode, uint64_t idx, service_id_t service_id)
|
---|
[acd869e] | 118 | {
|
---|
[8958a26] | 119 | ext4_extent_header_t *header = ext4_inode_get_extent_header(inode);
|
---|
[1a7756a] | 120 | ext4_extent_t *extent;
|
---|
| 121 | ext4_extent_index_t *extent_index;
|
---|
[8958a26] | 122 |
|
---|
[1a7756a] | 123 | uint32_t first_block;
|
---|
| 124 | uint16_t block_count;
|
---|
| 125 | uint64_t phys_block = 0;
|
---|
| 126 | uint64_t child;
|
---|
[8958a26] | 127 |
|
---|
[1a7756a] | 128 | int rc;
|
---|
| 129 | block_t* block = NULL;
|
---|
| 130 |
|
---|
| 131 | while (ext4_extent_header_get_depth(header) != 0) {
|
---|
| 132 |
|
---|
| 133 | extent_index = EXT4_EXTENT_FIRST_INDEX(header);
|
---|
[8958a26] | 134 |
|
---|
| 135 | for (uint16_t i = 0; i < ext4_extent_header_get_entries_count(header); ++i) {
|
---|
[1a7756a] | 136 | if(idx >= ext4_extent_index_get_first_block(extent_index)) {
|
---|
| 137 |
|
---|
| 138 | child = ext4_extent_index_get_leaf(extent_index);
|
---|
| 139 |
|
---|
| 140 | if (block != NULL) {
|
---|
| 141 | block_put(block);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | rc = block_get(&block, service_id, child, BLOCK_FLAGS_NONE);
|
---|
| 145 | if (rc != EOK) {
|
---|
| 146 | return 0;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | header = (ext4_extent_header_t *)block->data;
|
---|
| 150 | break;
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | extent = EXT4_EXTENT_FIRST(header);
|
---|
| 156 |
|
---|
| 157 | for (uint16_t i = 0; i < ext4_extent_header_get_entries_count(header); ++i) {
|
---|
| 158 |
|
---|
| 159 | first_block = ext4_extent_get_first_block(extent);
|
---|
| 160 | block_count = ext4_extent_get_block_count(extent);
|
---|
[8958a26] | 161 |
|
---|
[1a7756a] | 162 | if ((idx >= first_block) && (idx < first_block + block_count)) {
|
---|
| 163 | phys_block = ext4_extent_get_start(extent) + idx;
|
---|
| 164 | phys_block -= ext4_extent_get_first_block(extent);
|
---|
[8958a26] | 165 |
|
---|
[1a7756a] | 166 | // Memory leak prevention
|
---|
| 167 | if (block != NULL) {
|
---|
| 168 | block_put(block);
|
---|
[8958a26] | 169 | }
|
---|
[1a7756a] | 170 | return phys_block;
|
---|
[8958a26] | 171 | }
|
---|
[1a7756a] | 172 | // Go to the next extent
|
---|
| 173 | ++extent;
|
---|
[8958a26] | 174 | }
|
---|
| 175 |
|
---|
[acd869e] | 176 |
|
---|
[1a7756a] | 177 | EXT4FS_DBG("ERROR - reached function end");
|
---|
| 178 | return phys_block;
|
---|
[acd869e] | 179 | }
|
---|
| 180 |
|
---|
| 181 | ext4_extent_header_t * ext4_inode_get_extent_header(ext4_inode_t *inode)
|
---|
| 182 | {
|
---|
| 183 | return (ext4_extent_header_t *)inode->blocks;
|
---|
| 184 | }
|
---|
[7b9381b] | 185 |
|
---|
| 186 | // Flags checker
|
---|
[7bc4508] | 187 | bool ext4_inode_has_flag(ext4_inode_t *inode, uint32_t flag)
|
---|
| 188 | {
|
---|
[7b9381b] | 189 | if (ext4_inode_get_flags(inode) & flag) {
|
---|
| 190 | return true;
|
---|
| 191 | }
|
---|
| 192 | return false;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[12b4a7f] | 195 | bool ext4_inode_can_truncate(ext4_inode_t *inode)
|
---|
| 196 | {
|
---|
| 197 | if (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_APPEND)
|
---|
| 198 | || ext4_inode_has_flag(inode, EXT4_INODE_FLAG_IMMUTABLE)) {
|
---|
| 199 | return false;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 |
|
---|
| 203 | if (ext4_inode_get_mode(inode) == EXT4_INODE_MODE_FILE
|
---|
| 204 | || ext4_inode_get_mode(inode) == EXT4_INODE_MODE_DIRECTORY) {
|
---|
| 205 | return true;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | return false;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
[eb91db7] | 211 | /**
|
---|
| 212 | * @}
|
---|
| 213 | */
|
---|