source: mainline/uspace/lib/ext4/libext4_inode.c@ c83a55c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c83a55c was 7bc4508, checked in by Frantisek Princ <frantisek.princ@…>, 14 years ago

First part of reading htree directory index - the most important functionality (computing hashes) still missing

  • Property mode set to 100644
File size: 5.3 KB
Line 
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
35 * @brief Ext4 inode operations.
36 */
37
38#include <byteorder.h>
39#include <errno.h>
40#include <libblock.h>
41#include "libext4.h"
42
43uint32_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
52bool 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
58/*
59uint32_t ext4_inode_get_uid(ext4_inode_t *inode)
60*/
61
62uint64_t ext4_inode_get_size(ext4_superblock_t *sb, ext4_inode_t *inode)
63{
64 uint32_t major_rev = ext4_superblock_get_rev_level(sb);
65
66 if ((major_rev > 0) && ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)) {
67 return ((uint64_t)uint32_t_le2host(inode->size_hi)) << 32 |
68 ((uint64_t)uint32_t_le2host(inode->size_lo));
69 }
70 return uint32_t_le2host(inode->size_lo);
71}
72
73/*
74extern uint32_t ext4_inode_get_access_time(ext4_inode_t *);
75extern uint32_t ext4_inode_get_change_inode_time(ext4_inode_t *);
76extern uint32_t ext4_inode_get_modification_time(ext4_inode_t *);
77extern uint32_t ext4_inode_get_deletion_time(ext4_inode_t *);
78extern uint32_t ext4_inode_get_gid(ext4_inode_t *);
79*/
80
81uint16_t ext4_inode_get_links_count(ext4_inode_t *inode)
82{
83 return uint16_t_le2host(inode->links_count);
84}
85
86/*
87extern uint64_t ext4_inode_get_blocks_count(ext4_inode_t *);
88*/
89
90uint32_t ext4_inode_get_flags(ext4_inode_t *inode) {
91 return uint32_t_le2host(inode->flags);
92}
93
94uint32_t ext4_inode_get_direct_block(ext4_inode_t *inode, uint8_t idx)
95{
96 assert(idx < EXT4_INODE_DIRECT_BLOCK_COUNT);
97 return uint32_t_le2host(inode->blocks[idx]);
98}
99
100uint32_t ext4_inode_get_indirect_block(ext4_inode_t *inode, uint8_t idx)
101{
102 assert(idx < EXT4_INODE_INDIRECT_BLOCK_COUNT);
103 return uint32_t_le2host(inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK]);
104}
105
106uint32_t ext4_inode_get_extent_block(ext4_inode_t *inode, uint64_t idx, service_id_t service_id)
107{
108 ext4_extent_header_t *header = ext4_inode_get_extent_header(inode);
109 ext4_extent_t *extent;
110 ext4_extent_index_t *extent_index;
111
112 uint32_t first_block;
113 uint16_t block_count;
114 uint64_t phys_block = 0;
115 uint64_t child;
116
117 int rc;
118 block_t* block = NULL;
119
120 while (ext4_extent_header_get_depth(header) != 0) {
121
122 extent_index = EXT4_EXTENT_FIRST_INDEX(header);
123
124 for (uint16_t i = 0; i < ext4_extent_header_get_entries_count(header); ++i) {
125 if(idx >= ext4_extent_index_get_first_block(extent_index)) {
126
127 child = ext4_extent_index_get_leaf(extent_index);
128
129 if (block != NULL) {
130 block_put(block);
131 }
132
133 rc = block_get(&block, service_id, child, BLOCK_FLAGS_NONE);
134 if (rc != EOK) {
135 return 0;
136 }
137 EXT4FS_DBG("get block");
138
139
140 header = (ext4_extent_header_t *)block->data;
141 break;
142 }
143 }
144 }
145
146 extent = EXT4_EXTENT_FIRST(header);
147
148 for (uint16_t i = 0; i < ext4_extent_header_get_entries_count(header); ++i) {
149
150 first_block = ext4_extent_get_first_block(extent);
151 block_count = ext4_extent_get_block_count(extent);
152
153 if ((idx >= first_block) && (idx < first_block + block_count)) {
154 phys_block = ext4_extent_get_start(extent) + idx;
155 phys_block -= ext4_extent_get_first_block(extent);
156
157 // Memory leak prevention
158 if (block != NULL) {
159 block_put(block);
160 }
161 return phys_block;
162 }
163 // Go to the next extent
164 ++extent;
165 }
166
167
168 EXT4FS_DBG("ERROR - reached function end");
169 return phys_block;
170}
171
172ext4_extent_header_t * ext4_inode_get_extent_header(ext4_inode_t *inode)
173{
174 return (ext4_extent_header_t *)inode->blocks;
175}
176
177// Flags checker
178bool ext4_inode_has_flag(ext4_inode_t *inode, uint32_t flag)
179{
180 if (ext4_inode_get_flags(inode) & flag) {
181 return true;
182 }
183 return false;
184}
185
186/**
187 * @}
188 */
Note: See TracBrowser for help on using the repository browser.