source: mainline/uspace/lib/ext4/libext4_filesystem.c@ 3e2952b

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

more debugged version of releasing file/dir using extent

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