source: mainline/uspace/lib/ext4/libext4_directory_index.c@ 565b6ff

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 565b6ff was 565b6ff, checked in by Frantisek Princ <frantisek.princ@…>, 13 years ago
  • bugfix in adding directory entry (linear)
  • added skeleton for using directory index (not used yet)
  • Property mode set to 100644
File size: 13.0 KB
RevLine 
[0dc91833]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_directory_index.c
[c25e39b]35 * @brief Ext4 directory index operations.
[0dc91833]36 */
37
[343ccfd]38#include <byteorder.h>
[0dc91833]39#include <errno.h>
40#include "libext4.h"
41
[343ccfd]42
43uint8_t ext4_directory_dx_root_info_get_hash_version(
44 ext4_directory_dx_root_info_t *root_info)
45{
46 return root_info->hash_version;
47}
48
49void ext4_directory_dx_root_info_set_hash_version(
50 ext4_directory_dx_root_info_t *root_info, uint8_t version)
51{
52 root_info->hash_version = version;
53}
54
55uint8_t ext4_directory_dx_root_info_get_info_length(
56 ext4_directory_dx_root_info_t *root_info)
57{
58 return root_info->info_length;
59}
60
61void ext4_directory_dx_root_info_set_info_length(
62 ext4_directory_dx_root_info_t *root_info, uint8_t info_length)
63{
64 root_info->info_length = info_length;
65}
66
67uint8_t ext4_directory_dx_root_info_get_indirect_levels(
68 ext4_directory_dx_root_info_t *root_info)
69{
70 return root_info->indirect_levels;
71}
72
73void ext4_directory_dx_root_info_set_indirect_levels(
74 ext4_directory_dx_root_info_t *root_info, uint8_t levels)
75{
76 root_info->indirect_levels = levels;
77}
78
79uint16_t ext4_directory_dx_countlimit_get_limit(
80 ext4_directory_dx_countlimit_t *countlimit)
81{
82 return uint16_t_le2host(countlimit->limit);
83}
84
85void ext4_directory_dx_countlimit_set_limit(
86 ext4_directory_dx_countlimit_t *countlimit, uint16_t limit)
87{
88 countlimit->limit = host2uint16_t_le(limit);
89}
90
91uint16_t ext4_directory_dx_countlimit_get_count(
92 ext4_directory_dx_countlimit_t *countlimit)
93{
94 return uint16_t_le2host(countlimit->count);
95}
96
97void ext4_directory_dx_countlimit_set_count(
98 ext4_directory_dx_countlimit_t *countlimit, uint16_t count)
99{
100 countlimit->count = host2uint16_t_le(count);
101}
102
103uint32_t ext4_directory_dx_entry_get_hash(ext4_directory_dx_entry_t *entry)
104{
105 return uint32_t_le2host(entry->hash);
106}
107
108void ext4_directory_dx_entry_set_hash(ext4_directory_dx_entry_t *entry,
109 uint32_t hash)
110{
111 entry->hash = host2uint32_t_le(hash);
112}
113
114uint32_t ext4_directory_dx_entry_get_block(ext4_directory_dx_entry_t *entry)
115{
116 return uint32_t_le2host(entry->block);
117}
118
119void ext4_directory_dx_entry_set_block(ext4_directory_dx_entry_t *entry,
120 uint32_t block)
121{
122 entry->block = host2uint32_t_le(block);
123}
124
125
126/**************************************************************************/
127
128
[0dc91833]129static int ext4_directory_hinfo_init(ext4_hash_info_t *hinfo, block_t *root_block,
130 ext4_superblock_t *sb, size_t name_len, const char *name)
131{
132
[d9bbe45]133 ext4_directory_dx_root_t *root = (ext4_directory_dx_root_t *)root_block->data;
[0dc91833]134
135 if (root->info.hash_version != EXT4_HASH_VERSION_TEA &&
136 root->info.hash_version != EXT4_HASH_VERSION_HALF_MD4 &&
137 root->info.hash_version != EXT4_HASH_VERSION_LEGACY) {
138 return EXT4_ERR_BAD_DX_DIR;
139 }
140
141 // Check unused flags
142 if (root->info.unused_flags != 0) {
143 EXT4FS_DBG("ERR: unused_flags = \%u", root->info.unused_flags);
144 return EXT4_ERR_BAD_DX_DIR;
145 }
146
147 // Check indirect levels
148 if (root->info.indirect_levels > 1) {
149 EXT4FS_DBG("ERR: indirect_levels = \%u", root->info.indirect_levels);
150 return EXT4_ERR_BAD_DX_DIR;
151 }
152
[d9bbe45]153 uint32_t block_size = ext4_superblock_get_block_size(sb);
[0dc91833]154
[d9bbe45]155 uint32_t entry_space = block_size;
[0dc91833]156 entry_space -= 2 * sizeof(ext4_directory_dx_dot_entry_t);
157 entry_space -= sizeof(ext4_directory_dx_root_info_t);
158 entry_space = entry_space / sizeof(ext4_directory_dx_entry_t);
159
[d9bbe45]160 uint16_t limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)&root->entries);
[0dc91833]161 if (limit != entry_space) {
162 return EXT4_ERR_BAD_DX_DIR;
163 }
164
165 hinfo->hash_version = ext4_directory_dx_root_info_get_hash_version(&root->info);
166 if ((hinfo->hash_version <= EXT4_HASH_VERSION_TEA)
167 && (ext4_superblock_has_flag(sb, EXT4_SUPERBLOCK_FLAGS_UNSIGNED_HASH))) {
168 // 3 is magic from ext4 linux implementation
169 hinfo->hash_version += 3;
170 }
171
172 hinfo->seed = ext4_superblock_get_hash_seed(sb);
173
174 if (name) {
175 ext4_hash_string(hinfo, name_len, name);
176 }
177
178 return EOK;
179}
180
181static int ext4_directory_dx_get_leaf(ext4_hash_info_t *hinfo,
182 ext4_filesystem_t *fs, ext4_inode_t *inode, block_t *root_block,
183 ext4_directory_dx_block_t **dx_block, ext4_directory_dx_block_t *dx_blocks)
184{
185 int rc;
[d9bbe45]186
[0dc91833]187 ext4_directory_dx_block_t *tmp_dx_block = dx_blocks;
188
[d9bbe45]189 ext4_directory_dx_root_t *root = (ext4_directory_dx_root_t *)root_block->data;
190 ext4_directory_dx_entry_t *entries = (ext4_directory_dx_entry_t *)&root->entries;
[0dc91833]191
[d9bbe45]192 uint16_t limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)entries);
193 uint8_t indirect_level = ext4_directory_dx_root_info_get_indirect_levels(&root->info);
[0dc91833]194
[d9bbe45]195 block_t *tmp_block = root_block;
196 ext4_directory_dx_entry_t *p, *q, *m, *at;
[0dc91833]197 while (true) {
198
[d9bbe45]199 uint16_t count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)entries);
[0dc91833]200 if ((count == 0) || (count > limit)) {
201 return EXT4_ERR_BAD_DX_DIR;
202 }
203
204 p = entries + 1;
205 q = entries + count - 1;
206
207 while (p <= q) {
208 m = p + (q - p) / 2;
209 if (ext4_directory_dx_entry_get_hash(m) > hinfo->hash) {
210 q = m - 1;
211 } else {
212 p = m + 1;
213 }
214 }
215
216 at = p - 1;
217
218 tmp_dx_block->block = tmp_block;
219 tmp_dx_block->entries = entries;
220 tmp_dx_block->position = at;
221
222 if (indirect_level == 0) {
223 *dx_block = tmp_dx_block;
224 return EOK;
225 }
226
[d9bbe45]227 uint32_t next_block = ext4_directory_dx_entry_get_block(at);
[0dc91833]228
229 indirect_level--;
230
[d9bbe45]231 uint32_t fblock;
[0dc91833]232 rc = ext4_filesystem_get_inode_data_block_index(fs, inode, next_block, &fblock);
233 if (rc != EOK) {
234 return rc;
235 }
236
237 rc = block_get(&tmp_block, fs->device, fblock, BLOCK_FLAGS_NONE);
238 if (rc != EOK) {
239 return rc;
240 }
241
242 entries = ((ext4_directory_dx_node_t *) tmp_block->data)->entries;
243 limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)entries);
244
[d9bbe45]245 uint16_t entry_space = ext4_superblock_get_block_size(fs->superblock)
246 - sizeof(ext4_directory_dx_dot_entry_t);
[0dc91833]247 entry_space = entry_space / sizeof(ext4_directory_dx_entry_t);
248
249
250 if (limit != entry_space) {
251 block_put(tmp_block);
252 return EXT4_ERR_BAD_DX_DIR;
253 }
254
255 ++tmp_dx_block;
256 }
257
258 // Unreachable
259 return EOK;
260}
261
262
[d9bbe45]263static int ext4_directory_dx_find_dir_entry(block_t *block,
[0dc91833]264 ext4_superblock_t *sb, size_t name_len, const char *name,
265 ext4_directory_entry_ll_t **res_entry, aoff64_t *block_offset)
266{
267
[d9bbe45]268 aoff64_t offset = 0;
269 ext4_directory_entry_ll_t *dentry = (ext4_directory_entry_ll_t *)block->data;
270 uint8_t *addr_limit = block->data + ext4_superblock_get_block_size(sb);
[0dc91833]271
272 while ((uint8_t *)dentry < addr_limit) {
273
274 if ((uint8_t*) dentry + name_len > addr_limit) {
275 break;
276 }
277
278 if (dentry->inode != 0) {
279 if (name_len == ext4_directory_entry_ll_get_name_length(sb, dentry)) {
280 // Compare names
281 if (bcmp((uint8_t *)name, dentry->name, name_len) == 0) {
282 *block_offset = offset;
283 *res_entry = dentry;
284 return 1;
285 }
286 }
287 }
288
289
290 // Goto next entry
[d9bbe45]291 uint16_t dentry_len = ext4_directory_entry_ll_get_entry_length(dentry);
[0dc91833]292
293 if (dentry_len == 0) {
294 // TODO error
295 return -1;
296 }
297
298 offset += dentry_len;
299 dentry = (ext4_directory_entry_ll_t *)((uint8_t *)dentry + dentry_len);
300 }
301
302 return 0;
303}
304
305static int ext4_directory_dx_next_block(ext4_filesystem_t *fs, ext4_inode_t *inode, uint32_t hash,
306 ext4_directory_dx_block_t *handle, ext4_directory_dx_block_t *handles)
307{
[d9bbe45]308 int rc;
309
[0dc91833]310
[d9bbe45]311 uint32_t num_handles = 0;
312 ext4_directory_dx_block_t *p = handle;
[0dc91833]313
314 while (1) {
315
316 p->position++;
[d9bbe45]317 uint16_t count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)p->entries);
[0dc91833]318
319 if (p->position < p->entries + count) {
320 break;
321 }
322
323 if (p == handles) {
324 return 0;
325 }
326
327 num_handles++;
328 p--;
329 }
330
[d9bbe45]331 uint32_t current_hash = ext4_directory_dx_entry_get_hash(p->position);
[0dc91833]332
333 if ((hash & 1) == 0) {
334 if ((current_hash & ~1) != hash) {
335 return 0;
336 }
337 }
338
[d9bbe45]339
[0dc91833]340 while (num_handles--) {
341
[d9bbe45]342 uint32_t block_idx = ext4_directory_dx_entry_get_block(p->position);
343 uint32_t block_addr;
[0dc91833]344 rc = ext4_filesystem_get_inode_data_block_index(fs, inode, block_idx, &block_addr);
345 if (rc != EOK) {
346 return rc;
347 }
348
[d9bbe45]349 block_t *block;
[0dc91833]350 rc = block_get(&block, fs->device, block_addr, BLOCK_FLAGS_NONE);
351 if (rc != EOK) {
352 return rc;
353 }
354
355 p++;
356
357 block_put(p->block);
358 p->block = block;
359 p->entries = ((ext4_directory_dx_node_t *) block->data)->entries;
360 p->position = p->entries;
361 }
362
363 return 1;
364
365}
366
367int ext4_directory_dx_find_entry(ext4_directory_iterator_t *it,
368 ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, size_t len, const char *name)
369{
370 int rc;
371
372 // get direct block 0 (index root)
[d9bbe45]373 uint32_t root_block_addr;
[0dc91833]374 rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref->inode, 0, &root_block_addr);
375 if (rc != EOK) {
376 return rc;
377 }
378
[d9bbe45]379 block_t *root_block;
[0dc91833]380 rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
381 if (rc != EOK) {
382 it->current_block = NULL;
383 return rc;
384 }
385
[d9bbe45]386 ext4_hash_info_t hinfo;
[0dc91833]387 rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, len, name);
388 if (rc != EOK) {
389 block_put(root_block);
390 return EXT4_ERR_BAD_DX_DIR;
391 }
392
[565b6ff]393 // Hardcoded number 2 means maximum height of index tree !!!
[d9bbe45]394 ext4_directory_dx_block_t dx_blocks[2];
395 ext4_directory_dx_block_t *dx_block;
[0dc91833]396 rc = ext4_directory_dx_get_leaf(&hinfo, fs, inode_ref->inode, root_block, &dx_block, dx_blocks);
397 if (rc != EOK) {
398 block_put(root_block);
399 return EXT4_ERR_BAD_DX_DIR;
400 }
401
402
[d9bbe45]403 do {
[0dc91833]404
[d9bbe45]405 uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
406 uint32_t leaf_block_addr;
[0dc91833]407 rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref->inode, leaf_block_idx, &leaf_block_addr);
408 if (rc != EOK) {
409 return EXT4_ERR_BAD_DX_DIR;
410 }
411
[d9bbe45]412 block_t *leaf_block;
[0dc91833]413 rc = block_get(&leaf_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
414 if (rc != EOK) {
415 return EXT4_ERR_BAD_DX_DIR;
416 }
417
[d9bbe45]418 aoff64_t block_offset;
419 ext4_directory_entry_ll_t *res_dentry;
420 rc = ext4_directory_dx_find_dir_entry(leaf_block, fs->superblock, len, name,
[0dc91833]421 &res_dentry, &block_offset);
422
423 // Found => return it
424 if (rc == 1) {
425 it->fs = fs;
426 it->inode_ref = inode_ref;
427 it->current_block = leaf_block;
428 it->current_offset = block_offset;
429 it->current = res_dentry;
430 return EOK;
431 }
432
433 block_put(leaf_block);
434
435 // ERROR - corrupted index
436 if (rc == -1) {
437 // TODO cleanup
438 return EXT4_ERR_BAD_DX_DIR;
439 }
440
441 rc = ext4_directory_dx_next_block(fs, inode_ref->inode, hinfo.hash, dx_block, &dx_blocks[0]);
442 if (rc < 0) {
443 // TODO cleanup
444 return EXT4_ERR_BAD_DX_DIR;
445 }
446
447 } while (rc == 1);
448
449 return ENOENT;
450}
451
[565b6ff]452int ext4_directory_dx_add_entry(ext4_filesystem_t *fs,
453 ext4_inode_ref_t *parent, size_t name_size, const char *name)
454{
455 // TODO delete this command
456 return EXT4_ERR_BAD_DX_DIR;
457
458 EXT4FS_DBG("NOT REACHED");
459
460 int rc;
461
462 // get direct block 0 (index root)
463 uint32_t root_block_addr;
464 rc = ext4_filesystem_get_inode_data_block_index(fs, parent->inode, 0, &root_block_addr);
465 if (rc != EOK) {
466 return rc;
467 }
468
469 block_t *root_block;
470 rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
471 if (rc != EOK) {
472 return rc;
473 }
474
475 ext4_hash_info_t hinfo;
476 rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_size, name);
477 if (rc != EOK) {
478 block_put(root_block);
479 return EXT4_ERR_BAD_DX_DIR;
480 }
481
482 // Hardcoded number 2 means maximum height of index tree !!!
483 ext4_directory_dx_block_t dx_blocks[2];
484 ext4_directory_dx_block_t *dx_block;
485 rc = ext4_directory_dx_get_leaf(&hinfo, fs, parent->inode, root_block, &dx_block, dx_blocks);
486 if (rc != EOK) {
487 block_put(root_block);
488 return EXT4_ERR_BAD_DX_DIR;
489 }
490
491 // TODO
492 /*
493 * 1) try to write entry
494 * 2) split leaves if necessary
495 * 3) return
496 */
497
498
499}
[0dc91833]500
501
502/**
503 * @}
504 */
Note: See TracBrowser for help on using the repository browser.