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

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

getter and setter for inode blocks count

  • Property mode set to 100644
File size: 8.5 KB
RevLine 
[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]43static 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]56uint32_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
65bool ext4_inode_is_type(ext4_superblock_t *sb, ext4_inode_t *inode, uint32_t type)
66{
67 uint32_t mode = ext4_inode_get_mode(sb, inode);
68 return (mode & EXT4_INODE_MODE_TYPE_MASK) == type;
69}
70
[3712434]71/*
72uint32_t ext4_inode_get_uid(ext4_inode_t *inode)
73*/
74
[9b9d37bb]75uint64_t ext4_inode_get_size(ext4_superblock_t *sb, ext4_inode_t *inode)
[3712434]76{
[9b9d37bb]77 uint32_t major_rev = ext4_superblock_get_rev_level(sb);
78
[acd869e]79 if ((major_rev > 0) && ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)) {
[9b9d37bb]80 return ((uint64_t)uint32_t_le2host(inode->size_hi)) << 32 |
81 ((uint64_t)uint32_t_le2host(inode->size_lo));
[052e82d]82 }
[9b9d37bb]83 return uint32_t_le2host(inode->size_lo);
[3712434]84}
85
[052e82d]86void ext4_inode_set_size(ext4_inode_t *inode, uint64_t value) {
87 inode->size_lo = host2uint32_t_le((value << 32) >> 32);
88 inode->size_hi = host2uint32_t_le(value >> 32);
89}
90
[3712434]91/*
92extern uint32_t ext4_inode_get_access_time(ext4_inode_t *);
93extern uint32_t ext4_inode_get_change_inode_time(ext4_inode_t *);
94extern uint32_t ext4_inode_get_modification_time(ext4_inode_t *);
95extern uint32_t ext4_inode_get_deletion_time(ext4_inode_t *);
96extern uint32_t ext4_inode_get_gid(ext4_inode_t *);
97*/
98
99uint16_t ext4_inode_get_links_count(ext4_inode_t *inode)
[6c501f8]100{
[9c0c0e1]101 return uint16_t_le2host(inode->links_count);
[6c501f8]102}
103
[b3d7277]104
105uint64_t ext4_inode_get_blocks_count(ext4_superblock_t *sb, ext4_inode_t *inode)
106{
107 uint64_t count;
108
109 if (ext4_superblock_has_feature_read_only(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
110
111 /* 48-bit field */
112 count = ((uint64_t)uint16_t_le2host(inode->osd2.linux2.blocks_high)) << 32 |
113 uint32_t_le2host(inode->blocks_count_lo);
114
115 if (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_HUGE_FILE)) {
116 uint32_t block_size = ext4_superblock_get_block_size(sb);
117 uint32_t block_bits = ext4_inode_block_bits_count(block_size);
118 return count << (block_bits - 9);
119 } else {
120 return count;
121 }
122 } else {
123 return uint32_t_le2host(inode->blocks_count_lo);
124 }
125
126}
127
128int ext4_inode_set_blocks_count(ext4_superblock_t *sb, ext4_inode_t *inode,
129 uint64_t count)
130{
131 // 32-bit maximum
132 uint64_t max = 0;
133 max = ~max >> 32;
134
135 if (count <= max) {
136 inode->blocks_count_lo = host2uint32_t_le(count);
137 inode->osd2.linux2.blocks_high = 0;
138 ext4_inode_clear_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
139 return EOK;
140 }
141
142 if (!ext4_superblock_has_feature_read_only(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
143 return EINVAL;
144 }
145
146 // 48-bit maximum
147 max = 0;
148 max = ~max >> 16;
149
150 if (count <= max) {
151 inode->blocks_count_lo = host2uint32_t_le(count);
152 inode->osd2.linux2.blocks_high = host2uint16_t_le(count >> 32);
153 ext4_inode_clear_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
154 } else {
155 uint32_t block_size = ext4_superblock_get_block_size(sb);
156 uint32_t block_bits = ext4_inode_block_bits_count(block_size);
157 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
158 count = count >> (block_bits - 9);
159 inode->blocks_count_lo = host2uint32_t_le(count);
160 inode->osd2.linux2.blocks_high = host2uint16_t_le(count >> 32);
161 }
162 return EOK;
163
164
165}
[eb91db7]166
[7b9381b]167uint32_t ext4_inode_get_flags(ext4_inode_t *inode) {
168 return uint32_t_le2host(inode->flags);
169}
170
[b3d7277]171void ext4_inode_set_flags(ext4_inode_t *inode, uint32_t flags) {
172 inode->flags = host2uint32_t_le(flags);
173}
174
[1e65444]175uint32_t ext4_inode_get_direct_block(ext4_inode_t *inode, uint32_t idx)
[9b9d37bb]176{
177 assert(idx < EXT4_INODE_DIRECT_BLOCK_COUNT);
178 return uint32_t_le2host(inode->blocks[idx]);
179}
180
[1e65444]181void ext4_inode_set_direct_block(ext4_inode_t *inode, uint32_t idx, uint32_t fblock)
[d5a78e28]182{
183 assert(idx < EXT4_INODE_DIRECT_BLOCK_COUNT);
184 inode->blocks[idx] = host2uint32_t_le(fblock);
185}
186
[1e65444]187uint32_t ext4_inode_get_indirect_block(ext4_inode_t *inode, uint32_t idx)
[9b9d37bb]188{
189 return uint32_t_le2host(inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK]);
190}
191
[1e65444]192void ext4_inode_set_indirect_block(ext4_inode_t *inode, uint32_t idx, uint32_t fblock)
193{
194 inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK] = host2uint32_t_le(fblock);
195}
196
197
[1a7756a]198uint32_t ext4_inode_get_extent_block(ext4_inode_t *inode, uint64_t idx, service_id_t service_id)
[acd869e]199{
[8958a26]200 ext4_extent_header_t *header = ext4_inode_get_extent_header(inode);
[1a7756a]201 ext4_extent_t *extent;
202 ext4_extent_index_t *extent_index;
[8958a26]203
[1a7756a]204 uint32_t first_block;
205 uint16_t block_count;
206 uint64_t phys_block = 0;
207 uint64_t child;
[8958a26]208
[1a7756a]209 int rc;
210 block_t* block = NULL;
211
212 while (ext4_extent_header_get_depth(header) != 0) {
213
214 extent_index = EXT4_EXTENT_FIRST_INDEX(header);
[8958a26]215
216 for (uint16_t i = 0; i < ext4_extent_header_get_entries_count(header); ++i) {
[1a7756a]217 if(idx >= ext4_extent_index_get_first_block(extent_index)) {
218
219 child = ext4_extent_index_get_leaf(extent_index);
220
221 if (block != NULL) {
222 block_put(block);
223 }
224
225 rc = block_get(&block, service_id, child, BLOCK_FLAGS_NONE);
226 if (rc != EOK) {
227 return 0;
228 }
229
230 header = (ext4_extent_header_t *)block->data;
231 break;
232 }
233 }
234 }
235
236 extent = EXT4_EXTENT_FIRST(header);
237
238 for (uint16_t i = 0; i < ext4_extent_header_get_entries_count(header); ++i) {
239
240 first_block = ext4_extent_get_first_block(extent);
241 block_count = ext4_extent_get_block_count(extent);
[8958a26]242
[1a7756a]243 if ((idx >= first_block) && (idx < first_block + block_count)) {
244 phys_block = ext4_extent_get_start(extent) + idx;
245 phys_block -= ext4_extent_get_first_block(extent);
[8958a26]246
[1a7756a]247 // Memory leak prevention
248 if (block != NULL) {
249 block_put(block);
[8958a26]250 }
[1a7756a]251 return phys_block;
[8958a26]252 }
[1a7756a]253 // Go to the next extent
254 ++extent;
[8958a26]255 }
256
[acd869e]257
[1a7756a]258 EXT4FS_DBG("ERROR - reached function end");
259 return phys_block;
[acd869e]260}
261
262ext4_extent_header_t * ext4_inode_get_extent_header(ext4_inode_t *inode)
263{
264 return (ext4_extent_header_t *)inode->blocks;
265}
[7b9381b]266
[b3d7277]267// Flags operations
[7bc4508]268bool ext4_inode_has_flag(ext4_inode_t *inode, uint32_t flag)
269{
[7b9381b]270 if (ext4_inode_get_flags(inode) & flag) {
271 return true;
272 }
273 return false;
274}
275
[b3d7277]276void ext4_inode_clear_flag(ext4_inode_t *inode, uint32_t clear_flag)
277{
278 uint32_t flags = ext4_inode_get_flags(inode);
279 flags = flags & (~clear_flag);
280 ext4_inode_set_flags(inode, flags);
281}
282
283void ext4_inode_set_flag(ext4_inode_t *inode, uint32_t set_flag)
284{
285 uint32_t flags = ext4_inode_get_flags(inode);
286 flags = flags | set_flag;
287 ext4_inode_set_flags(inode, flags);
288}
289
[43a9968]290bool ext4_inode_can_truncate(ext4_superblock_t *sb, ext4_inode_t *inode)
[12b4a7f]291{
292 if (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_APPEND)
293 || ext4_inode_has_flag(inode, EXT4_INODE_FLAG_IMMUTABLE)) {
294 return false;
295 }
296
[e31e56a1]297 if (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)
298 || ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_DIRECTORY)) {
[12b4a7f]299 return true;
300 }
301
302 return false;
303}
304
[eb91db7]305/**
306 * @}
307 */
Note: See TracBrowser for help on using the repository browser.