source: mainline/uspace/lib/ext4/libext4_directory.c@ 73196d2

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

Skeleton for adding directory entry (without indexing)

  • Property mode set to 100644
File size: 10.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_directory.c
35 * @brief Ext4 directory structure operations.
36 */
37
38#include <byteorder.h>
39#include <errno.h>
40#include <malloc.h>
41#include <string.h>
42#include "libext4.h"
43
44static int ext4_directory_iterator_set(ext4_directory_iterator_t *,
45 uint32_t);
46
47
48uint32_t ext4_directory_entry_ll_get_inode(ext4_directory_entry_ll_t *de)
49{
50 return uint32_t_le2host(de->inode);
51}
52
53void ext4_directory_entry_ll_set_inode(ext4_directory_entry_ll_t *de,
54 uint32_t inode)
55{
56 de->inode = host2uint32_t_le(inode);
57}
58
59uint16_t ext4_directory_entry_ll_get_entry_length(
60 ext4_directory_entry_ll_t *de)
61{
62 return uint16_t_le2host(de->entry_length);
63}
64
65void ext4_directory_entry_ll_set_entry_length(ext4_directory_entry_ll_t *de,
66 uint16_t length)
67{
68 de->entry_length = host2uint16_t_le(length);
69}
70
71uint16_t ext4_directory_entry_ll_get_name_length(
72 ext4_superblock_t *sb, ext4_directory_entry_ll_t *de)
73{
74 if (ext4_superblock_get_rev_level(sb) == 0 &&
75 ext4_superblock_get_minor_rev_level(sb) < 5) {
76
77 return ((uint16_t)de->name_length_high) << 8 |
78 ((uint16_t)de->name_length);
79
80 }
81 return de->name_length;
82
83}
84
85void ext4_directory_entry_ll_set_name_length(ext4_superblock_t *sb,
86 ext4_directory_entry_ll_t *de, uint16_t length)
87{
88 de->name_length = (length << 8) >> 8;
89
90 if (ext4_superblock_get_rev_level(sb) == 0 &&
91 ext4_superblock_get_minor_rev_level(sb) < 5) {
92
93 de->name_length_high = length >> 8;
94 }
95}
96
97
98int ext4_directory_iterator_init(ext4_directory_iterator_t *it,
99 ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, aoff64_t pos)
100{
101 it->inode_ref = inode_ref;
102 it->fs = fs;
103 it->current = NULL;
104 it->current_offset = 0;
105 it->current_block = NULL;
106
107 return ext4_directory_iterator_seek(it, pos);
108}
109
110
111int ext4_directory_iterator_next(ext4_directory_iterator_t *it)
112{
113 uint16_t skip;
114
115 assert(it->current != NULL);
116
117 skip = ext4_directory_entry_ll_get_entry_length(it->current);
118
119 return ext4_directory_iterator_seek(it, it->current_offset + skip);
120}
121
122
123int ext4_directory_iterator_seek(ext4_directory_iterator_t *it, aoff64_t pos)
124{
125 int rc;
126
127 uint64_t size;
128 aoff64_t current_block_idx;
129 aoff64_t next_block_idx;
130 uint32_t next_block_phys_idx;
131 uint32_t block_size;
132
133 size = ext4_inode_get_size(it->fs->superblock, it->inode_ref->inode);
134
135 /* The iterator is not valid until we seek to the desired position */
136 it->current = NULL;
137
138 /* Are we at the end? */
139 if (pos >= size) {
140 if (it->current_block) {
141 rc = block_put(it->current_block);
142 it->current_block = NULL;
143 if (rc != EOK) {
144 return rc;
145 }
146 }
147
148 it->current_offset = pos;
149 return EOK;
150 }
151
152 block_size = ext4_superblock_get_block_size(it->fs->superblock);
153 current_block_idx = it->current_offset / block_size;
154 next_block_idx = pos / block_size;
155
156 /* If we don't have a block or are moving accross block boundary,
157 * we need to get another block
158 */
159 if (it->current_block == NULL || current_block_idx != next_block_idx) {
160 if (it->current_block) {
161 rc = block_put(it->current_block);
162 it->current_block = NULL;
163 if (rc != EOK) {
164 return rc;
165 }
166 }
167
168 rc = ext4_filesystem_get_inode_data_block_index(it->fs,
169 it->inode_ref->inode, next_block_idx, &next_block_phys_idx);
170 if (rc != EOK) {
171 return rc;
172 }
173
174 rc = block_get(&it->current_block, it->fs->device, next_block_phys_idx,
175 BLOCK_FLAGS_NONE);
176 if (rc != EOK) {
177 it->current_block = NULL;
178 return rc;
179 }
180 }
181
182 it->current_offset = pos;
183
184 return ext4_directory_iterator_set(it, block_size);
185}
186
187static int ext4_directory_iterator_set(ext4_directory_iterator_t *it,
188 uint32_t block_size)
189{
190 uint32_t offset_in_block = it->current_offset % block_size;
191
192 it->current = NULL;
193
194 /* Ensure proper alignment */
195 if ((offset_in_block % 4) != 0) {
196 return EIO;
197 }
198
199 /* Ensure that the core of the entry does not overflow the block */
200 if (offset_in_block > block_size - 8) {
201 return EIO;
202 }
203
204 ext4_directory_entry_ll_t *entry = it->current_block->data + offset_in_block;
205
206 /* Ensure that the whole entry does not overflow the block */
207 uint16_t length = ext4_directory_entry_ll_get_entry_length(entry);
208 if (offset_in_block + length > block_size) {
209 return EIO;
210 }
211
212 /* Ensure the name length is not too large */
213 if (ext4_directory_entry_ll_get_name_length(it->fs->superblock,
214 entry) > length-8) {
215 return EIO;
216 }
217
218 it->current = entry;
219 return EOK;
220}
221
222
223int ext4_directory_iterator_fini(ext4_directory_iterator_t *it)
224{
225 int rc;
226
227 it->fs = NULL;
228 it->inode_ref = NULL;
229 it->current = NULL;
230
231 if (it->current_block) {
232 rc = block_put(it->current_block);
233 if (rc != EOK) {
234 return rc;
235 }
236 }
237
238 return EOK;
239}
240
241int ext4_directory_add_entry(ext4_filesystem_t *fs, ext4_inode_ref_t * inode_ref,
242 const char *entry_name, uint32_t child_inode)
243{
244 int rc;
245
246 // USE index if allowed
247
248 uint16_t name_len = strlen(entry_name);
249 uint16_t required_len = 8 + name_len + (4 - name_len % 4);
250
251 ext4_directory_iterator_t it;
252 rc = ext4_directory_iterator_init(&it, fs, inode_ref, 0);
253 if (rc != EOK) {
254 return rc;
255 }
256
257 while (it.current != NULL) {
258 uint32_t entry_inode = ext4_directory_entry_ll_get_inode(it.current);
259 uint16_t rec_len = ext4_directory_entry_ll_get_entry_length(it.current);
260
261 if ((entry_inode == 0) && (rec_len >= required_len)) {
262
263 // Don't touch entry length
264 ext4_directory_entry_ll_set_inode(it.current, child_inode);
265 ext4_directory_entry_ll_set_name_length(fs->superblock, it.current, name_len);
266 it.current_block->dirty = true;
267 return ext4_directory_iterator_fini(&it);
268 }
269
270 if (entry_inode != 0) {
271
272 uint16_t used_name_len = ext4_directory_entry_ll_get_name_length(
273 fs->superblock, it.current);
274 uint16_t free_space = rec_len - 8 - (used_name_len + (4- used_name_len % 4));
275
276 if (free_space >= required_len) {
277 uint16_t used_len = rec_len - free_space;
278
279 // Cut tail of current entry
280 ext4_directory_entry_ll_set_entry_length(it.current, used_len);
281
282 // Jump to newly created
283 rc = ext4_directory_iterator_next(&it);
284 if (rc != EOK) {
285 return rc;
286 }
287
288 // We are sure, that both entries are in the same data block
289 // dirtyness will be set now
290
291 ext4_directory_entry_ll_set_inode(it.current, child_inode);
292 ext4_directory_entry_ll_set_entry_length(it.current, free_space);
293 ext4_directory_entry_ll_set_name_length(
294 fs->superblock, it.current, name_len);
295 memcpy(it.current->name, entry_name, name_len);
296 it.current_block->dirty = true;
297 return ext4_directory_iterator_fini(&it);
298 }
299
300 }
301
302 rc = ext4_directory_iterator_next(&it);
303 if (rc != EOK) {
304 return rc;
305 }
306 }
307
308 // TODO - no free space found - alloc data block
309 // and fill the whole block with new entry
310
311 // TODO
312 return EOK;
313}
314
315int ext4_directory_find_entry(ext4_directory_iterator_t *it,
316 ext4_inode_ref_t *parent, const char *name)
317{
318 int rc;
319 uint32_t name_size = strlen(name);
320
321 // Index search
322 if (ext4_superblock_has_feature_compatible(it->fs->superblock, EXT4_FEATURE_COMPAT_DIR_INDEX) &&
323 ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX)) {
324
325 rc = ext4_directory_dx_find_entry(it, it->fs, parent, name_size, name);
326
327 // Check if index is not corrupted
328 if (rc != EXT4_ERR_BAD_DX_DIR) {
329
330 if (rc != EOK) {
331 return rc;
332 }
333 return EOK;
334 }
335
336 EXT4FS_DBG("index is corrupted - doing linear search");
337 }
338
339 bool found = false;
340 // Linear search
341 while (it->current != NULL) {
342 uint32_t inode = ext4_directory_entry_ll_get_inode(it->current);
343
344 /* ignore empty directory entries */
345 if (inode != 0) {
346 uint16_t entry_name_size = ext4_directory_entry_ll_get_name_length(
347 it->fs->superblock, it->current);
348
349 if (entry_name_size == name_size && bcmp(name, it->current->name,
350 name_size) == 0) {
351 found = true;
352 break;
353 }
354 }
355
356 rc = ext4_directory_iterator_next(it);
357 if (rc != EOK) {
358 return rc;
359 }
360 }
361
362 if (!found) {
363 return ENOENT;
364 }
365
366 return EOK;
367}
368
369
370int ext4_directory_remove_entry(ext4_filesystem_t* fs,
371 ext4_inode_ref_t *parent, const char *name)
372{
373 int rc;
374 ext4_directory_iterator_t it;
375
376 if (!ext4_inode_is_type(fs->superblock, parent->inode,
377 EXT4_INODE_MODE_DIRECTORY)) {
378 return ENOTDIR;
379 }
380
381 rc = ext4_directory_iterator_init(&it, fs, parent, 0);
382 if (rc != EOK) {
383 return rc;
384 }
385
386 rc = ext4_directory_find_entry(&it, parent, name);
387 if (rc != EOK) {
388 ext4_directory_iterator_fini(&it);
389 return rc;
390 }
391
392 // TODO modify HTREE index if exists
393
394 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
395 uint32_t pos = it.current_offset % block_size;
396
397 ext4_directory_entry_ll_set_inode(it.current, 0);
398
399 if (pos != 0) {
400 uint32_t offset = 0;
401
402 ext4_directory_entry_ll_t *tmp_dentry = it.current_block->data;
403 uint16_t tmp_dentry_length =
404 ext4_directory_entry_ll_get_entry_length(tmp_dentry);
405
406 while ((offset + tmp_dentry_length) < pos) {
407 offset += ext4_directory_entry_ll_get_entry_length(tmp_dentry);
408 tmp_dentry = it.current_block->data + offset;
409 tmp_dentry_length =
410 ext4_directory_entry_ll_get_entry_length(tmp_dentry);
411 }
412
413 assert(tmp_dentry_length + offset == pos);
414
415 uint16_t del_entry_length =
416 ext4_directory_entry_ll_get_entry_length(it.current);
417 ext4_directory_entry_ll_set_entry_length(tmp_dentry,
418 tmp_dentry_length + del_entry_length);
419
420 }
421
422
423 it.current_block->dirty = true;
424
425 ext4_directory_iterator_fini(&it);
426 return EOK;
427}
428
429
430/**
431 * @}
432 */
Note: See TracBrowser for help on using the repository browser.