source: mainline/uspace/lib/ext4/libext4_filesystem.c@ 2d2c6ce

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

Skeleton for inode allocation and initialization

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