source: mainline/uspace/lib/ext4/libext4_filesystem.c@ 1d69c69

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

some TODO's solved + removed debugging messages

  • Property mode set to 100644
File size: 21.1 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_filesystem.c
35 * @brief TODO
36 */
37
38#include <byteorder.h>
39#include <errno.h>
40#include <malloc.h>
41#include "libext4.h"
42
43int ext4_filesystem_init(ext4_filesystem_t *fs, service_id_t service_id)
44{
45 int rc;
46
47 fs->device = service_id;
48
49 // TODO what does constant 2048 mean?
50 rc = block_init(EXCHANGE_SERIALIZE, fs->device, 2048);
51 if (rc != EOK) {
52 return rc;
53 }
54
55 /* Read superblock from device */
56 ext4_superblock_t *temp_superblock;
57 rc = ext4_superblock_read_direct(fs->device, &temp_superblock);
58 if (rc != EOK) {
59 block_fini(fs->device);
60 return rc;
61 }
62
63 /* Read block size from superblock and check */
64 uint32_t block_size = ext4_superblock_get_block_size(temp_superblock);
65 if (block_size > EXT4_MAX_BLOCK_SIZE) {
66 block_fini(fs->device);
67 return ENOTSUP;
68 }
69
70 /* Initialize block caching */
71 rc = block_cache_init(service_id, block_size, 0, CACHE_MODE_WT);
72 if (rc != EOK) {
73 block_fini(fs->device);
74 return rc;
75 }
76
77 uint32_t block_ids_per_block = block_size / sizeof(uint32_t);
78 fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
79 fs->inode_blocks_per_level[0] = 1;
80 for (int i = 1; i < 4; i++) {
81 fs->inode_blocks_per_level[i] = fs->inode_blocks_per_level[i-1] *
82 block_ids_per_block;
83 fs->inode_block_limits[i] = fs->inode_block_limits[i-1] +
84 fs->inode_blocks_per_level[i];
85 }
86
87 /* Return loaded superblock */
88 fs->superblock = temp_superblock;
89
90 return EOK;
91}
92
93int ext4_filesystem_fini(ext4_filesystem_t *fs, bool write_sb)
94{
95 int rc = EOK;
96
97 if (write_sb) {
98 rc = ext4_superblock_write_direct(fs->device, fs->superblock);
99 }
100
101 free(fs->superblock);
102 block_fini(fs->device);
103
104 return rc;
105}
106
107int ext4_filesystem_check_sanity(ext4_filesystem_t *fs)
108{
109 int rc;
110
111 rc = ext4_superblock_check_sanity(fs->superblock);
112 if (rc != EOK) {
113 return rc;
114 }
115
116 return EOK;
117}
118
119int ext4_filesystem_check_features(ext4_filesystem_t *fs, bool *o_read_only)
120{
121 /* Feature flags are present in rev 1 and later */
122 if (ext4_superblock_get_rev_level(fs->superblock) == 0) {
123 *o_read_only = false;
124 return EOK;
125 }
126
127 uint32_t incompatible_features;
128 incompatible_features = ext4_superblock_get_features_incompatible(fs->superblock);
129 incompatible_features &= ~EXT4_FEATURE_INCOMPAT_SUPP;
130 if (incompatible_features > 0) {
131 *o_read_only = true;
132 return ENOTSUP;
133 }
134
135 uint32_t compatible_read_only;
136 compatible_read_only = ext4_superblock_get_features_read_only(fs->superblock);
137 compatible_read_only &= ~EXT4_FEATURE_RO_COMPAT_SUPP;
138 if (compatible_read_only > 0) {
139 *o_read_only = true;
140 }
141
142 return EOK;
143}
144
145int ext4_filesystem_get_block_group_ref(ext4_filesystem_t *fs, uint32_t bgid,
146 ext4_block_group_ref_t **ref)
147{
148 int rc;
149
150 ext4_block_group_ref_t *newref = malloc(sizeof(ext4_block_group_ref_t));
151 if (newref == NULL) {
152 return ENOMEM;
153 }
154
155 uint32_t descriptors_per_block = ext4_superblock_get_block_size(fs->superblock)
156 / ext4_superblock_get_desc_size(fs->superblock);
157
158 /* Block group descriptor table starts at the next block after superblock */
159 aoff64_t block_id = ext4_superblock_get_first_data_block(fs->superblock) + 1;
160
161 /* Find the block containing the descriptor we are looking for */
162 block_id += bgid / descriptors_per_block;
163 uint32_t offset = (bgid % descriptors_per_block) * ext4_superblock_get_desc_size(fs->superblock);
164
165 rc = block_get(&newref->block, fs->device, block_id, 0);
166 if (rc != EOK) {
167 free(newref);
168 return rc;
169 }
170
171 newref->block_group = newref->block->data + offset;
172 newref->dirty = false;
173
174 *ref = newref;
175
176 return EOK;
177}
178
179int ext4_filesystem_put_block_group_ref(ext4_block_group_ref_t *ref)
180{
181 int rc;
182
183 if (ref->dirty) {
184 ref->block->dirty = true;
185 }
186
187 rc = block_put(ref->block);
188 free(ref);
189
190 return rc;
191}
192
193int ext4_filesystem_get_inode_ref(ext4_filesystem_t *fs, uint32_t index,
194 ext4_inode_ref_t **ref)
195{
196 int rc;
197
198 ext4_inode_ref_t *newref = malloc(sizeof(ext4_inode_ref_t));
199 if (newref == NULL) {
200 return ENOMEM;
201 }
202
203 uint32_t inodes_per_group =
204 ext4_superblock_get_inodes_per_group(fs->superblock);
205
206 /* inode numbers are 1-based, but it is simpler to work with 0-based
207 * when computing indices
208 */
209 index -= 1;
210 uint32_t block_group = index / inodes_per_group;
211 uint32_t offset_in_group = index % inodes_per_group;
212
213 ext4_block_group_ref_t *bg_ref;
214 rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
215 if (rc != EOK) {
216 free(newref);
217 return rc;
218 }
219
220 uint32_t inode_table_start = ext4_block_group_get_inode_table_first_block(
221 bg_ref->block_group, fs->superblock);
222
223 rc = ext4_filesystem_put_block_group_ref(bg_ref);
224 if (rc != EOK) {
225 free(newref);
226 return rc;
227 }
228
229 uint16_t inode_size = ext4_superblock_get_inode_size(fs->superblock);
230 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
231 uint32_t byte_offset_in_group = offset_in_group * inode_size;
232
233 aoff64_t block_id = inode_table_start + (byte_offset_in_group / block_size);
234 rc = block_get(&newref->block, fs->device, block_id, 0);
235 if (rc != EOK) {
236 free(newref);
237 return rc;
238 }
239
240 uint32_t offset_in_block = byte_offset_in_group % block_size;
241 newref->inode = newref->block->data + offset_in_block;
242 /* we decremented index above, but need to store the original value
243 * in the reference
244 */
245 newref->index = index+1;
246 newref->dirty = false;
247
248 *ref = newref;
249
250 return EOK;
251}
252
253
254int ext4_filesystem_put_inode_ref(ext4_inode_ref_t *ref)
255{
256 int rc;
257
258 if (ref->dirty) {
259 ref->block->dirty = true;
260 }
261
262 rc = block_put(ref->block);
263 free(ref);
264
265 return rc;
266}
267
268int ext4_filesystem_alloc_inode(ext4_filesystem_t *fs,
269 ext4_inode_ref_t **inode_ref, int flags)
270{
271 int rc;
272
273 bool is_dir = false;
274 if (flags & L_DIRECTORY) {
275 is_dir = true;
276 }
277
278 // allocate inode
279 uint32_t index;
280 rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
281 if (rc != EOK) {
282 return rc;
283 }
284
285 // TODO extents, dir_index etc...
286
287 rc = ext4_filesystem_get_inode_ref(fs, index, inode_ref);
288 if (rc != EOK) {
289 ext4_ialloc_free_inode(fs, index, is_dir);
290 return rc;
291 }
292
293 // init inode
294 ext4_inode_t *inode = (*inode_ref)->inode;
295
296 if (is_dir) {
297 ext4_inode_set_mode(fs->superblock, inode, EXT4_INODE_MODE_DIRECTORY);
298 ext4_inode_set_links_count(inode, 1); // '.' entry
299 } else {
300 ext4_inode_set_mode(fs->superblock, inode, EXT4_INODE_MODE_FILE);
301 ext4_inode_set_links_count(inode, 0);
302 }
303
304 ext4_inode_set_uid(inode, 0);
305 ext4_inode_set_gid(inode, 0);
306 ext4_inode_set_size(inode, 0);
307 ext4_inode_set_access_time(inode, 0);
308 ext4_inode_set_change_inode_time(inode, 0);
309 ext4_inode_set_modification_time(inode, 0);
310 ext4_inode_set_deletion_time(inode, 0);
311 ext4_inode_set_blocks_count(fs->superblock, inode, 0);
312 ext4_inode_set_flags(inode, 0);
313 ext4_inode_set_generation(inode, 0);
314
315 for (uint32_t i = 0; i < EXT4_INODE_BLOCKS; i++) {
316 inode->blocks[i] = 0;
317 }
318
319 (*inode_ref)->dirty = true;
320
321 return EOK;
322}
323
324int ext4_filesystem_free_inode(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref)
325{
326 int rc;
327
328 // release all indirect (no data) blocks
329
330 // 1) Single indirect
331 uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
332 if (fblock != 0) {
333 rc = ext4_balloc_free_block(fs, inode_ref, fblock);
334 if (rc != EOK) {
335 return rc;
336 }
337
338 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
339 }
340
341 block_t *block;
342 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
343 uint32_t count = block_size / sizeof(uint32_t);
344
345 // 2) Double indirect
346 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
347 if (fblock != 0) {
348 rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
349 if (rc != EOK) {
350 // TODO error
351 }
352
353 uint32_t ind_block;
354 for (uint32_t offset = 0; offset < count; ++offset) {
355 ind_block = uint32_t_le2host(((uint32_t*)block->data)[offset]);
356
357 if (ind_block != 0) {
358 rc = ext4_balloc_free_block(fs, inode_ref, ind_block);
359 if (rc != EOK) {
360 // TODO error
361 }
362 }
363 }
364
365 block_put(block);
366 rc = ext4_balloc_free_block(fs, inode_ref, fblock);
367 if (rc != EOK) {
368 // TODO error
369 }
370
371 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
372 }
373
374
375 // 3) Tripple indirect
376 block_t *subblock;
377 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
378 if (fblock != 0) {
379 rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
380 if (rc != EOK) {
381 // TODO error
382 }
383
384 uint32_t ind_block;
385 for (uint32_t offset = 0; offset < count; ++offset) {
386 ind_block = uint32_t_le2host(((uint32_t*)block->data)[offset]);
387
388 if (ind_block != 0) {
389 rc = block_get(&subblock, fs->device, ind_block, BLOCK_FLAGS_NONE);
390 if (rc != EOK) {
391 // TODO error
392 }
393
394 uint32_t ind_subblock;
395 for (uint32_t suboffset = 0; suboffset < count; ++suboffset) {
396 ind_subblock = uint32_t_le2host(((uint32_t*)subblock->data)[suboffset]);
397
398 if (ind_subblock != 0) {
399 rc = ext4_balloc_free_block(fs, inode_ref, ind_subblock);
400 if (rc != EOK) {
401 // TODO error
402 }
403 }
404
405 }
406 block_put(subblock);
407
408 }
409
410 rc = ext4_balloc_free_block(fs, inode_ref, ind_block);
411 if (rc != EOK) {
412 // TODO error
413 }
414
415
416 }
417
418 block_put(block);
419 rc = ext4_balloc_free_block(fs, inode_ref, fblock);
420 if (rc != EOK) {
421 // TODO error
422 }
423
424 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
425 }
426
427 inode_ref->dirty = true;
428
429 // Free inode
430 if (ext4_inode_is_type(fs->superblock, inode_ref->inode,
431 EXT4_INODE_MODE_DIRECTORY)) {
432 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
433 } else {
434 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
435 }
436 if (rc != EOK) {
437 return rc;
438 }
439
440 return EOK;
441}
442
443int ext4_filesystem_truncate_inode(ext4_filesystem_t *fs,
444 ext4_inode_ref_t *inode_ref, aoff64_t new_size)
445{
446 int rc;
447
448 if (! ext4_inode_can_truncate(fs->superblock, inode_ref->inode)) {
449 // Unable to truncate
450 return EINVAL;
451 }
452
453 aoff64_t old_size = ext4_inode_get_size(fs->superblock, inode_ref->inode);
454 if (old_size == new_size) {
455 // Nothing to do
456 return EOK;
457 }
458
459 // It's not suppported to make the larger file
460 if (old_size < new_size) {
461 return EINVAL;
462 }
463
464 aoff64_t size_diff = old_size - new_size;
465 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
466 uint32_t diff_blocks_count = size_diff / block_size;
467 if (size_diff % block_size != 0) {
468 diff_blocks_count++;
469 }
470
471 uint32_t old_blocks_count = old_size / block_size;
472 if (old_size % block_size != 0) {
473 old_blocks_count++;
474 }
475
476 // starting from 1 because of logical blocks are numbered from 0
477 for (uint32_t i = 1; i <= diff_blocks_count; ++i) {
478 rc = ext4_filesystem_release_inode_block(fs, inode_ref, old_blocks_count - i);
479 if (rc != EOK) {
480 return rc;
481 }
482 }
483
484 ext4_inode_set_size(inode_ref->inode, new_size);
485
486 inode_ref->dirty = true;
487
488 return EOK;
489}
490
491int ext4_filesystem_get_inode_data_block_index(ext4_filesystem_t *fs, ext4_inode_t* inode,
492 aoff64_t iblock, uint32_t* fblock)
493{
494 int rc;
495
496
497 uint32_t current_block;
498
499 /* Handle inode using extents */
500 if (ext4_superblock_has_feature_compatible(fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
501 ext4_inode_has_flag(inode, EXT4_INODE_FLAG_EXTENTS)) {
502 current_block = ext4_inode_get_extent_block(inode, iblock, fs->device);
503 *fblock = current_block;
504 return EOK;
505
506 }
507
508 /* Handle simple case when we are dealing with direct reference */
509 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
510 current_block = ext4_inode_get_direct_block(inode, (uint32_t)iblock);
511 *fblock = current_block;
512 return EOK;
513 }
514
515 /* Determine the indirection level needed to get the desired block */
516 int level = -1;
517 for (int i = 1; i < 4; i++) {
518 if (iblock < fs->inode_block_limits[i]) {
519 level = i;
520 break;
521 }
522 }
523
524 if (level == -1) {
525 return EIO;
526 }
527
528 /* Compute offsets for the topmost level */
529 aoff64_t block_offset_in_level = iblock - fs->inode_block_limits[level-1];
530 current_block = ext4_inode_get_indirect_block(inode, level-1);
531 uint32_t offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
532
533 if (current_block == 0) {
534 *fblock = 0;
535 return EOK;
536 }
537
538 block_t *block;
539
540 /* Navigate through other levels, until we find the block number
541 * or find null reference meaning we are dealing with sparse file
542 */
543 while (level > 0) {
544 rc = block_get(&block, fs->device, current_block, 0);
545 if (rc != EOK) {
546 return rc;
547 }
548
549 current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
550
551 rc = block_put(block);
552 if (rc != EOK) {
553 return rc;
554 }
555
556 if (current_block == 0) {
557 /* This is a sparse file */
558 *fblock = 0;
559 return EOK;
560 }
561
562 level -= 1;
563
564 /* If we are on the last level, break here as
565 * there is no next level to visit
566 */
567 if (level == 0) {
568 break;
569 }
570
571 /* Visit the next level */
572 block_offset_in_level %= fs->inode_blocks_per_level[level];
573 offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
574 }
575
576 *fblock = current_block;
577
578 return EOK;
579}
580
581
582int ext4_filesystem_set_inode_data_block_index(ext4_filesystem_t *fs,
583 ext4_inode_ref_t *inode_ref, aoff64_t iblock, uint32_t fblock)
584{
585 int rc;
586
587
588 /* Handle inode using extents */
589 if (ext4_superblock_has_feature_compatible(fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
590 ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
591 // TODO
592 return ENOTSUP;
593 }
594
595 /* Handle simple case when we are dealing with direct reference */
596 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
597 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock, fblock);
598 inode_ref->dirty = true;
599 return EOK;
600 }
601
602 /* Determine the indirection level needed to get the desired block */
603 int level = -1;
604 for (int i = 1; i < 4; i++) {
605 if (iblock < fs->inode_block_limits[i]) {
606 level = i;
607 break;
608 }
609 }
610
611 if (level == -1) {
612 return EIO;
613 }
614
615
616
617 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
618
619 /* Compute offsets for the topmost level */
620 aoff64_t block_offset_in_level = iblock - fs->inode_block_limits[level-1];
621 uint32_t current_block = ext4_inode_get_indirect_block(inode_ref->inode, level-1);
622 uint32_t offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
623
624 uint32_t new_block_addr;
625 block_t *block, *new_block;
626
627 if (current_block == 0) {
628 rc = ext4_balloc_alloc_block(fs, inode_ref, &new_block_addr);
629 if (rc != EOK) {
630 // TODO error
631 EXT4FS_DBG("error in allocation");
632 }
633
634 ext4_inode_set_indirect_block(inode_ref->inode, level - 1, new_block_addr);
635
636 inode_ref->dirty = true;
637
638 rc = block_get(&new_block, fs->device, new_block_addr, BLOCK_FLAGS_NOREAD);
639 if (rc != EOK) {
640 EXT4FS_DBG("block load error");
641 // TODO error
642 }
643
644 memset(new_block->data, 0, block_size);
645 new_block->dirty = true;
646
647 rc = block_put(new_block);
648 if (rc != EOK) {
649 EXT4FS_DBG("block put error");
650 }
651
652// EXT4FS_DBG("allocated indirect block for level \%u, during setting iblock \%u", level, (uint32_t)iblock);
653
654 current_block = new_block_addr;
655 }
656
657 /* Navigate through other levels, until we find the block number
658 * or find null reference meaning we are dealing with sparse file
659 */
660 while (level > 0) {
661
662 rc = block_get(&block, fs->device, current_block, 0);
663 if (rc != EOK) {
664 return rc;
665 }
666
667 current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
668
669 if ((level > 1) && (current_block == 0)) {
670 rc = ext4_balloc_alloc_block(fs, inode_ref, &new_block_addr);
671 if (rc != EOK) {
672 // TODO error
673 EXT4FS_DBG("allocation error");
674 }
675
676 rc = block_get(&new_block, fs->device, new_block_addr, BLOCK_FLAGS_NOREAD);
677 if (rc != EOK) {
678 // TODO error
679
680 EXT4FS_DBG("BBB: error block loading");
681
682 }
683 memset(new_block->data, 0, block_size);
684 new_block->dirty = true;
685
686 rc = block_put(new_block);
687 if (rc != EOK) {
688 EXT4FS_DBG("BBB: error indirect block saving");
689 }
690
691 ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(new_block_addr);
692 block->dirty = true;
693 current_block = new_block_addr;
694 }
695
696 if (level == 1) {
697 ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(fblock);
698 block->dirty = true;
699 }
700
701 rc = block_put(block);
702 if (rc != EOK) {
703 return rc;
704 }
705
706 level -= 1;
707
708 /* If we are on the last level, break here as
709 * there is no next level to visit
710 */
711 if (level == 0) {
712 break;
713 }
714
715 /* Visit the next level */
716 block_offset_in_level %= fs->inode_blocks_per_level[level];
717 offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
718 }
719
720 return EOK;
721}
722
723int ext4_filesystem_release_inode_block(ext4_filesystem_t *fs,
724 ext4_inode_ref_t *inode_ref, uint32_t iblock)
725{
726 int rc;
727
728
729 /* TODO handle extents */
730
731
732 uint32_t fblock;
733 ext4_inode_t *inode = inode_ref->inode;
734
735 /* Handle simple case when we are dealing with direct reference */
736 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
737 fblock = ext4_inode_get_direct_block(inode, iblock);
738 // Sparse file
739 if (fblock == 0) {
740 return EOK;
741 }
742
743 ext4_inode_set_direct_block(inode, iblock, 0);
744 return ext4_balloc_free_block(fs, inode_ref, fblock);
745 }
746
747
748 /* Determine the indirection level needed to get the desired block */
749 int level = -1;
750 for (int i = 1; i < 4; i++) {
751 if (iblock < fs->inode_block_limits[i]) {
752 level = i;
753 break;
754 }
755 }
756
757 if (level == -1) {
758 return EIO;
759 }
760
761 /* Compute offsets for the topmost level */
762 aoff64_t block_offset_in_level = iblock - fs->inode_block_limits[level-1];
763 uint32_t current_block = ext4_inode_get_indirect_block(inode, level-1);
764 uint32_t offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
765
766 /* Navigate through other levels, until we find the block number
767 * or find null reference meaning we are dealing with sparse file
768 */
769 block_t *block;
770 while (level > 0) {
771 rc = block_get(&block, fs->device, current_block, 0);
772 if (rc != EOK) {
773 return rc;
774 }
775
776 current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
777
778 // Set zero
779 if (level == 1) {
780 ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(0);
781 block->dirty = true;
782 }
783
784 rc = block_put(block);
785 if (rc != EOK) {
786 return rc;
787 }
788
789 level -= 1;
790
791 /* If we are on the last level, break here as
792 * there is no next level to visit
793 */
794 if (level == 0) {
795 break;
796 }
797
798 /* Visit the next level */
799 block_offset_in_level %= fs->inode_blocks_per_level[level];
800 offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
801 }
802
803 fblock = current_block;
804
805 if (fblock == 0) {
806 return EOK;
807 }
808
809 return ext4_balloc_free_block(fs, inode_ref, fblock);
810
811}
812
813int ext4_filesystem_add_orphan(ext4_filesystem_t *fs,
814 ext4_inode_ref_t *inode_ref)
815{
816 uint32_t next_orphan = ext4_superblock_get_last_orphan(fs->superblock);
817 ext4_inode_set_deletion_time(inode_ref->inode, next_orphan);
818 ext4_superblock_set_last_orphan(fs->superblock, inode_ref->index);
819 inode_ref->dirty = true;
820
821 return EOK;
822}
823
824int ext4_filesystem_delete_orphan(ext4_filesystem_t *fs,
825 ext4_inode_ref_t *inode_ref)
826{
827 int rc;
828
829 uint32_t last_orphan = ext4_superblock_get_last_orphan(fs->superblock);
830 assert(last_orphan > 0);
831
832 uint32_t next_orphan = ext4_inode_get_deletion_time(inode_ref->inode);
833
834 if (last_orphan == inode_ref->index) {
835 ext4_superblock_set_last_orphan(fs->superblock, next_orphan);
836 ext4_inode_set_deletion_time(inode_ref->inode, 0);
837 inode_ref->dirty = true;
838 return EOK;
839 }
840
841 ext4_inode_ref_t *current;
842 rc = ext4_filesystem_get_inode_ref(fs, last_orphan, &current);
843 if (rc != EOK) {
844 return rc;
845 }
846 next_orphan = ext4_inode_get_deletion_time(current->inode);
847
848 bool found;
849
850 while (next_orphan != 0) {
851 if (next_orphan == inode_ref->index) {
852 next_orphan = ext4_inode_get_deletion_time(inode_ref->inode);
853 ext4_inode_set_deletion_time(current->inode, next_orphan);
854 current->dirty = true;
855 found = true;
856 break;
857 }
858
859 ext4_filesystem_put_inode_ref(current);
860
861 rc = ext4_filesystem_get_inode_ref(fs, next_orphan, &current);
862 if (rc != EOK) {
863 return rc;
864 }
865 next_orphan = ext4_inode_get_deletion_time(current->inode);
866
867 }
868
869 ext4_inode_set_deletion_time(inode_ref->inode, 0);
870
871 rc = ext4_filesystem_put_inode_ref(current);
872 if (rc != EOK) {
873 return rc;
874 }
875
876 if (!found) {
877 return ENOENT;
878 }
879
880 return EOK;
881}
882
883/**
884 * @}
885 */
Note: See TracBrowser for help on using the repository browser.