source: mainline/uspace/lib/ext4/libext4_extent.c@ 38384ae

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

find extent procedure with path saving (it's slow), for loading block will be probably used not saving variant

  • Property mode set to 100644
File size: 8.6 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_extent.c
35 * @brief Ext4 extent structures operations.
36 */
37
38#include <byteorder.h>
39#include <errno.h>
40#include <malloc.h>
41#include "libext4.h"
42
43uint32_t ext4_extent_get_first_block(ext4_extent_t *extent)
44{
45 return uint32_t_le2host(extent->first_block);
46}
47
48void ext4_extent_set_first_block(ext4_extent_t *extent, uint32_t first_block)
49{
50 extent->first_block = host2uint32_t_le(first_block);
51}
52
53uint16_t ext4_extent_get_block_count(ext4_extent_t *extent)
54{
55 return uint16_t_le2host(extent->block_count);
56}
57
58void ext4_extent_set_block_count(ext4_extent_t *extent, uint16_t block_count)
59{
60 extent->block_count = host2uint16_t_le(block_count);
61}
62
63uint64_t ext4_extent_get_start(ext4_extent_t *extent)
64{
65 return ((uint64_t)uint16_t_le2host(extent->start_hi)) << 32 |
66 ((uint64_t)uint32_t_le2host(extent->start_lo));
67}
68
69void ext4_extent_set_start(ext4_extent_t *extent, uint64_t start)
70{
71 extent->start_lo = host2uint32_t_le((start << 32) >> 32);
72 extent->start_hi = host2uint16_t_le((uint16_t)(start >> 32));
73}
74
75uint32_t ext4_extent_index_get_first_block(ext4_extent_index_t *index)
76{
77 return uint32_t_le2host(index->first_block);
78}
79
80void ext4_extent_index_set_first_block(ext4_extent_index_t *index,
81 uint32_t first)
82{
83 index->first_block = host2uint32_t_le(first);
84}
85
86uint64_t ext4_extent_index_get_leaf(ext4_extent_index_t *index)
87{
88 return ((uint64_t)uint16_t_le2host(index->leaf_hi)) << 32 |
89 ((uint64_t)uint32_t_le2host(index->leaf_lo));
90}
91
92void ext4_extent_index_set_leaf(ext4_extent_index_t *index, uint64_t leaf)
93{
94 index->leaf_lo = host2uint32_t_le((leaf << 32) >> 32);
95 index->leaf_hi = host2uint16_t_le((uint16_t)(leaf >> 32));
96}
97
98uint16_t ext4_extent_header_get_magic(ext4_extent_header_t *header)
99{
100 return uint16_t_le2host(header->magic);
101}
102
103void ext4_extent_header_set_magic(ext4_extent_header_t *header, uint16_t magic)
104{
105 header->magic = host2uint16_t_le(magic);
106}
107
108uint16_t ext4_extent_header_get_entries_count(ext4_extent_header_t *header)
109{
110 return uint16_t_le2host(header->entries_count);
111}
112
113void ext4_extent_header_set_entries_count(ext4_extent_header_t *header,
114 uint16_t count)
115{
116 header->entries_count = host2uint16_t_le(count);
117}
118
119uint16_t ext4_extent_header_get_max_entries_count(ext4_extent_header_t *header)
120{
121 return uint16_t_le2host(header->max_entries_count);
122}
123
124void ext4_extent_header_set_max_entries_count(ext4_extent_header_t *header,
125 uint16_t max_count)
126{
127 header->max_entries_count = host2uint16_t_le(max_count);
128}
129
130uint16_t ext4_extent_header_get_depth(ext4_extent_header_t *header)
131{
132 return uint16_t_le2host(header->depth);
133}
134
135void ext4_extent_header_set_depth(ext4_extent_header_t *header, uint16_t depth)
136{
137 header->depth = host2uint16_t_le(depth);
138}
139
140uint32_t ext4_extent_header_get_generation(ext4_extent_header_t *header)
141{
142 return uint32_t_le2host(header->generation);
143}
144
145void ext4_extent_header_set_generation(ext4_extent_header_t *header,
146 uint32_t generation)
147{
148 header->generation = host2uint32_t_le(generation);
149}
150
151/**
152 * Binary search in extent index node
153 */
154static void ext4_extent_binsearch_idx(ext4_extent_header_t *header,
155 ext4_extent_index_t **index, uint32_t iblock)
156{
157 ext4_extent_index_t *r, *l, *m;
158
159 uint16_t entries_count = ext4_extent_header_get_entries_count(header);
160
161 if (entries_count == 1) {
162 *index = EXT4_EXTENT_FIRST_INDEX(header);
163 return;
164 }
165
166 l = EXT4_EXTENT_FIRST_INDEX(header) + 1;
167 r = l + entries_count - 1;
168
169 while (l <= r) {
170 m = l + (r - l) / 2;
171 uint32_t first_block = ext4_extent_index_get_first_block(m);
172 if (iblock < first_block) {
173 r = m - 1;
174 } else {
175 l = m + 1;
176 }
177 }
178
179 *index = l - 1;
180}
181
182/**
183 * Binary search in extent leaf node
184 */
185static void ext4_extent_binsearch(ext4_extent_header_t *header,
186 ext4_extent_t **extent, uint32_t iblock)
187{
188 ext4_extent_t *r, *l, *m;
189
190 uint16_t entries_count = ext4_extent_header_get_entries_count(header);
191
192 if (entries_count == 0) {
193 // this leaf is empty
194 EXT4FS_DBG("EMPTY LEAF");
195 *extent = NULL;
196 return;
197 }
198
199 if (entries_count == 1) {
200 *extent = EXT4_EXTENT_FIRST(header);
201 return;
202 }
203
204 l = EXT4_EXTENT_FIRST(header) + 1;
205 r = l + entries_count - 1;
206
207 while (l <= r) {
208 m = l + (r - l) / 2;
209 uint32_t first_block = ext4_extent_get_first_block(m);
210 if (iblock < first_block) {
211 r = m - 1;
212 } else {
213 l = m + 1;
214 }
215 }
216
217 *extent = l - 1;
218}
219
220// Reading routine without saving blocks to path
221//static int ext4_extent_find_extent(ext4_filesystem_t *fs,
222// ext4_inode_ref_t *inode_ref, uint32_t iblock, ext4_extent_t *extent)
223//{
224// int rc;
225//
226// block_t* block = NULL;
227//
228// ext4_extent_header_t *header = ext4_inode_get_extent_header(inode_ref->inode);
229// while (ext4_extent_header_get_depth(header) != 0) {
230//
231// ext4_extent_index_t *index;
232// ext4_extent_binsearch_idx(header, &index, iblock);
233//
234// uint64_t child = ext4_extent_index_get_leaf(index);
235//
236// if (block != NULL) {
237// block_put(block);
238// }
239//
240// rc = block_get(&block, fs->device, child, BLOCK_FLAGS_NONE);
241// if (rc != EOK) {
242// return rc;
243// }
244//
245// header = (ext4_extent_header_t *)block->data;
246// }
247//
248//
249// ext4_extent_t* tmp_extent;
250// ext4_extent_binsearch(header, &tmp_extent, iblock);
251//
252// memcpy(extent, tmp_extent, sizeof(ext4_extent_t));
253//
254// return EOK;
255//}
256
257static int ext4_extent_find_extent(ext4_filesystem_t *fs,
258 ext4_inode_ref_t *inode_ref, uint32_t iblock, ext4_extent_path_t **ret_path)
259{
260 int rc;
261
262 ext4_extent_header_t *eh =
263 ext4_inode_get_extent_header(inode_ref->inode);
264
265 uint16_t depth = ext4_extent_header_get_depth(eh);
266
267 ext4_extent_path_t *tmp_path;
268
269 // Added 2 for possible tree growing
270 tmp_path = malloc(sizeof(ext4_extent_path_t) * (depth + 2));
271 if (tmp_path == NULL) {
272 return ENOMEM;
273 }
274
275 tmp_path[0].block = inode_ref->block;
276 tmp_path[0].header = eh;
277
278 uint16_t pos = 0;
279 while (ext4_extent_header_get_depth(eh) != 0) {
280
281 ext4_extent_binsearch_idx(tmp_path[pos].header, &tmp_path[pos].index, iblock);
282
283 tmp_path[pos].depth = depth;
284 tmp_path[pos].extent = NULL;
285
286 assert(tmp_path[pos].index != NULL);
287
288 uint64_t fblock = ext4_extent_index_get_leaf(tmp_path[pos].index);
289
290 block_t *block;
291 rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
292 if (rc != EOK) {
293 // TODO cleanup
294 EXT4FS_DBG("ERRRR");
295 return rc;
296 }
297
298 pos++;
299
300 eh = (ext4_extent_header_t *)block->data;
301 tmp_path[pos].block = block;
302 tmp_path[pos].header = eh;
303
304 }
305
306 tmp_path[pos].depth = 0;
307 tmp_path[pos].extent = NULL;
308 tmp_path[pos].index = NULL;
309
310 /* find extent */
311 ext4_extent_binsearch(tmp_path[pos].header, &tmp_path[pos].extent, iblock);
312
313 *ret_path = tmp_path;
314
315 return EOK;
316}
317
318
319int ext4_extent_find_block(ext4_filesystem_t *fs,
320 ext4_inode_ref_t *inode_ref, uint32_t iblock, uint32_t *fblock)
321{
322 int rc;
323
324 ext4_extent_path_t *path;
325 rc = ext4_extent_find_extent(fs, inode_ref, iblock, &path);
326 if (rc != EOK) {
327 return rc;
328 }
329
330 uint16_t depth = path->depth;
331
332 ext4_extent_t *extent = path[depth].extent;
333
334 uint32_t phys_block;
335 phys_block = ext4_extent_get_start(extent) + iblock;
336 phys_block -= ext4_extent_get_first_block(extent);
337
338 *fblock = phys_block;
339
340 // Put loaded blocks
341 // From 1 -> 0 is a block with inode data
342 for (uint16_t i = 1; i < depth; ++i) {
343 if (path[i].block) {
344 block_put(path[i].block);
345 }
346 }
347
348 // Destroy temporary data structure
349 free(path);
350
351 return EOK;
352
353}
354
355/**
356 * @}
357 */
Note: See TracBrowser for help on using the repository browser.