source: mainline/uspace/lib/ext4/libext4_extent.c@ bed78cb

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

added TODOs for comments and first part of superblock comments

  • Property mode set to 100644
File size: 27.4 KB
Line 
1/*
2 * Copyright (c) 2012 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_extent.c
35 * @brief Ext4 extent structures operations.
36 */
37
38#include <byteorder.h>
39#include <errno.h>
40#include <malloc.h>
41#include "libext4.h"
42
43/** Get logical number of the block covered by extent.
44 *
45 * @param extent extent to load number from
46 * @return logical number of the first block covered by extent
47 */
48uint32_t ext4_extent_get_first_block(ext4_extent_t *extent)
49{
50 return uint32_t_le2host(extent->first_block);
51}
52
53/** Set logical number of the first block covered by extent.
54 *
55 * @param extent extent to set number to
56 * @param iblock logical number of the first block covered by extent
57 */
58void ext4_extent_set_first_block(ext4_extent_t *extent, uint32_t iblock)
59{
60 extent->first_block = host2uint32_t_le(iblock);
61}
62
63/** Get number of blocks covered by extent.
64 *
65 * @param extent extent to load count from
66 * @return number of blocks covered by extent
67 */
68uint16_t ext4_extent_get_block_count(ext4_extent_t *extent)
69{
70 return uint16_t_le2host(extent->block_count);
71}
72
73/** Set number of blocks covered by extent.
74 *
75 * @param extent extent to load count from
76 * @param count number of blocks covered by extent
77 */
78void ext4_extent_set_block_count(ext4_extent_t *extent, uint16_t count)
79{
80 extent->block_count = host2uint16_t_le(count);
81}
82
83/** Get physical number of the first block covered by extent.
84 *
85 * @param extent extent to load number
86 * @return physical number of the first block covered by extent
87 */
88uint64_t ext4_extent_get_start(ext4_extent_t *extent)
89{
90 return ((uint64_t)uint16_t_le2host(extent->start_hi)) << 32 |
91 ((uint64_t)uint32_t_le2host(extent->start_lo));
92}
93
94/** Set physical number of the first block covered by extent.
95 *
96 * @param extent extent to load number
97 * @param fblock physical number of the first block covered by extent
98 */
99void ext4_extent_set_start(ext4_extent_t *extent, uint64_t fblock)
100{
101 extent->start_lo = host2uint32_t_le((fblock << 32) >> 32);
102 extent->start_hi = host2uint16_t_le((uint16_t)(fblock >> 32));
103}
104
105/** Get logical number of the block covered by extent index.
106 *
107 * @param index extent index to load number from
108 * @return logical number of the first block covered by extent index
109 */
110uint32_t ext4_extent_index_get_first_block(ext4_extent_index_t *index)
111{
112 return uint32_t_le2host(index->first_block);
113}
114
115/** Set logical number of the block covered by extent index.
116 *
117 * @param index extent index to set number to
118 * @param iblock logical number of the first block covered by extent index
119 */
120void ext4_extent_index_set_first_block(ext4_extent_index_t *index,
121 uint32_t iblock)
122{
123 index->first_block = host2uint32_t_le(iblock);
124}
125
126/** Get physical number of block where the child node is located.
127 *
128 * @param index extent index to load number from
129 * @return physical number of the block with child node
130 */
131uint64_t ext4_extent_index_get_leaf(ext4_extent_index_t *index)
132{
133 return ((uint64_t)uint16_t_le2host(index->leaf_hi)) << 32 |
134 ((uint64_t)uint32_t_le2host(index->leaf_lo));
135}
136
137/** Set physical number of block where the child node is located.
138 *
139 * @param index extent index to set number to
140 * @param fblock physical number of the block with child node
141 */
142void ext4_extent_index_set_leaf(ext4_extent_index_t *index, uint64_t fblock)
143{
144 index->leaf_lo = host2uint32_t_le((fblock << 32) >> 32);
145 index->leaf_hi = host2uint16_t_le((uint16_t)(fblock >> 32));
146}
147
148/** Get magic value from extent header.
149 *
150 * @param header extent header to load value from
151 * @return magic value of extent header
152 */
153uint16_t ext4_extent_header_get_magic(ext4_extent_header_t *header)
154{
155 return uint16_t_le2host(header->magic);
156}
157
158/** Set magic value to extent header.
159 *
160 * @param header extent header to set value to
161 * @param magic magic value of extent header
162 */
163void ext4_extent_header_set_magic(ext4_extent_header_t *header, uint16_t magic)
164{
165 header->magic = host2uint16_t_le(magic);
166}
167
168/** Get number of entries from extent header
169 *
170 * @param header extent header to get value from
171 * @return number of entries covered by extent header
172 */
173uint16_t ext4_extent_header_get_entries_count(ext4_extent_header_t *header)
174{
175 return uint16_t_le2host(header->entries_count);
176}
177
178/** Set number of entries to extent header
179 *
180 * @param header extent header to set value to
181 * @param count number of entries covered by extent header
182 */
183void ext4_extent_header_set_entries_count(ext4_extent_header_t *header,
184 uint16_t count)
185{
186 header->entries_count = host2uint16_t_le(count);
187}
188
189/** Get maximum number of entries from extent header
190 *
191 * @param header extent header to get value from
192 * @return maximum number of entries covered by extent header
193 */
194uint16_t ext4_extent_header_get_max_entries_count(ext4_extent_header_t *header)
195{
196 return uint16_t_le2host(header->max_entries_count);
197}
198
199/** Set maximum number of entries to extent header
200 *
201 * @param header extent header to set value to
202 * @param max_count maximum number of entries covered by extent header
203 */
204void ext4_extent_header_set_max_entries_count(ext4_extent_header_t *header,
205 uint16_t max_count)
206{
207 header->max_entries_count = host2uint16_t_le(max_count);
208}
209
210/** Get depth of extent subtree.
211 *
212 * @param header extent header to get value from
213 * @return depth of extent subtree
214 */
215uint16_t ext4_extent_header_get_depth(ext4_extent_header_t *header)
216{
217 return uint16_t_le2host(header->depth);
218}
219
220/** Set depth of extent subtree.
221 *
222 * @param header extent header to set value to
223 * @param depth depth of extent subtree
224 */
225void ext4_extent_header_set_depth(ext4_extent_header_t *header, uint16_t depth)
226{
227 header->depth = host2uint16_t_le(depth);
228}
229
230/** Get generation from extent header
231 *
232 * @param header extent header to get value from
233 * @return generation
234 */
235uint32_t ext4_extent_header_get_generation(ext4_extent_header_t *header)
236{
237 return uint32_t_le2host(header->generation);
238}
239
240/** Set generation to extent header
241 *
242 * @param header extent header to set value to
243 * @param generation generation
244 */
245void ext4_extent_header_set_generation(ext4_extent_header_t *header,
246 uint32_t generation)
247{
248 header->generation = host2uint32_t_le(generation);
249}
250
251/** Binary search in extent index node.
252 *
253 * @param header extent header of index node
254 * @param index output value - found index will be set here
255 * @param iblock logical block number to find in index node
256 */
257static void ext4_extent_binsearch_idx(ext4_extent_header_t *header,
258 ext4_extent_index_t **index, uint32_t iblock)
259{
260 ext4_extent_index_t *r, *l, *m;
261
262 uint16_t entries_count = ext4_extent_header_get_entries_count(header);
263
264 // Check trivial situation
265 if (entries_count == 1) {
266 *index = EXT4_EXTENT_FIRST_INDEX(header);
267 return;
268 }
269
270 // Initialize bounds
271 l = EXT4_EXTENT_FIRST_INDEX(header) + 1;
272 r = l + entries_count - 1;
273
274 // Do binary search
275 while (l <= r) {
276 m = l + (r - l) / 2;
277 uint32_t first_block = ext4_extent_index_get_first_block(m);
278 if (iblock < first_block) {
279 r = m - 1;
280 } else {
281 l = m + 1;
282 }
283 }
284
285 // Set output value
286 *index = l - 1;
287}
288
289/** Binary search in extent leaf node.
290 * @param header extent header of leaf node
291 * @param extent output value - found extent will be set here,
292 * or NULL if node is empty
293 * @param iblock logical block number to find in leaf node
294 *
295 */
296static void ext4_extent_binsearch(ext4_extent_header_t *header,
297 ext4_extent_t **extent, uint32_t iblock)
298{
299 ext4_extent_t *r, *l, *m;
300
301 uint16_t entries_count = ext4_extent_header_get_entries_count(header);
302
303 if (entries_count == 0) {
304 // this leaf is empty
305 *extent = NULL;
306 return;
307 }
308
309 // Check trivial situation
310 if (entries_count == 1) {
311 *extent = EXT4_EXTENT_FIRST(header);
312 return;
313 }
314
315 // Initialize bounds
316 l = EXT4_EXTENT_FIRST(header) + 1;
317 r = l + entries_count - 1;
318
319 // Do binary search
320 while (l < r) {
321 m = l + (r - l) / 2;
322 uint32_t first_block = ext4_extent_get_first_block(m);
323 if (iblock < first_block) {
324 r = m - 1;
325 } else {
326 l = m + 1;
327 }
328 }
329
330 // Set output value
331 *extent = l - 1;
332}
333
334/** Find physical block in the extent tree by logical block number.
335 *
336 * There is no need to save path in the tree during this algorithm.
337 *
338 * @param inode_ref i-node to load block from
339 * @param iblock logical block number to find
340 * @param fblock output value for physical block number
341 * @return error code
342 */
343int ext4_extent_find_block(ext4_inode_ref_t *inode_ref,
344 uint32_t iblock, uint32_t *fblock)
345{
346 int rc;
347
348 // Compute bound defined by i-node size
349 uint64_t inode_size = ext4_inode_get_size(
350 inode_ref->fs->superblock, inode_ref->inode);
351
352 uint32_t block_size = ext4_superblock_get_block_size(
353 inode_ref->fs->superblock);
354
355 uint32_t last_idx = (inode_size - 1) / block_size;
356
357 // Check if requested iblock is not over size of i-node
358 if (iblock > last_idx) {
359 *fblock = 0;
360 return EOK;
361 }
362
363 block_t* block = NULL;
364
365 // Walk through extent tree
366 ext4_extent_header_t *header = ext4_inode_get_extent_header(inode_ref->inode);
367 while (ext4_extent_header_get_depth(header) != 0) {
368
369 // Search index in node
370 ext4_extent_index_t *index;
371 ext4_extent_binsearch_idx(header, &index, iblock);
372
373 // Load child node and set values for the next iteration
374 uint64_t child = ext4_extent_index_get_leaf(index);
375
376 if (block != NULL) {
377 block_put(block);
378 }
379
380 rc = block_get(&block, inode_ref->fs->device, child, BLOCK_FLAGS_NONE);
381 if (rc != EOK) {
382 return rc;
383 }
384
385 header = (ext4_extent_header_t *)block->data;
386 }
387
388 // Search extent in the leaf block
389 ext4_extent_t* extent = NULL;
390 ext4_extent_binsearch(header, &extent, iblock);
391
392 // Prevent empty leaf
393 if (extent == NULL) {
394 *fblock = 0;
395 } else {
396
397 // Compute requested physical block address
398 uint32_t phys_block;
399 phys_block = ext4_extent_get_start(extent) + iblock;
400 phys_block -= ext4_extent_get_first_block(extent);
401
402 *fblock = phys_block;
403 }
404
405 // Cleanup
406 if (block != NULL) {
407 block_put(block);
408 }
409
410 return EOK;
411}
412
413
414/** Find extent for specified iblock.
415 *
416 * This function is used for finding block in the extent tree with
417 * saving the path through the tree for possible future modifications.
418 *
419 * @param inode_ref i-node to read extent tree from
420 * @param iblock iblock to find extent for
421 * @param ret_path output value for loaded path from extent tree
422 * @return error code
423 */
424static int ext4_extent_find_extent(ext4_inode_ref_t *inode_ref,
425 uint32_t iblock, ext4_extent_path_t **ret_path)
426{
427 int rc;
428
429 ext4_extent_header_t *eh =
430 ext4_inode_get_extent_header(inode_ref->inode);
431
432 uint16_t depth = ext4_extent_header_get_depth(eh);
433
434 ext4_extent_path_t *tmp_path;
435
436 // Added 2 for possible tree growing
437 tmp_path = malloc(sizeof(ext4_extent_path_t) * (depth + 2));
438 if (tmp_path == NULL) {
439 return ENOMEM;
440 }
441
442 // Initialize structure for algorithm start
443 tmp_path[0].block = inode_ref->block;
444 tmp_path[0].header = eh;
445
446 // Walk through the extent tree
447 uint16_t pos = 0;
448 while (ext4_extent_header_get_depth(eh) != 0) {
449
450 // Search index in index node by iblock
451 ext4_extent_binsearch_idx(tmp_path[pos].header, &tmp_path[pos].index, iblock);
452
453 tmp_path[pos].depth = depth;
454 tmp_path[pos].extent = NULL;
455
456 assert(tmp_path[pos].index != NULL);
457
458 // Load information for the next iteration
459 uint64_t fblock = ext4_extent_index_get_leaf(tmp_path[pos].index);
460
461 block_t *block;
462 rc = block_get(&block, inode_ref->fs->device, fblock, BLOCK_FLAGS_NONE);
463 if (rc != EOK) {
464 goto cleanup;
465 }
466
467 pos++;
468
469 eh = (ext4_extent_header_t *)block->data;
470 tmp_path[pos].block = block;
471 tmp_path[pos].header = eh;
472
473 }
474
475 tmp_path[pos].depth = 0;
476 tmp_path[pos].extent = NULL;
477 tmp_path[pos].index = NULL;
478
479 // Find extent in the leaf node
480 ext4_extent_binsearch(tmp_path[pos].header, &tmp_path[pos].extent, iblock);
481
482 *ret_path = tmp_path;
483
484 return EOK;
485
486cleanup:
487 // Put loaded blocks
488 // From 1 -> 0 is a block with inode data
489 for (uint16_t i = 1; i < tmp_path->depth; ++i) {
490 if (tmp_path[i].block) {
491 block_put(tmp_path[i].block);
492 }
493 }
494
495 // Destroy temporary data structure
496 free(tmp_path);
497
498 return rc;
499}
500
501/** Release extent and all data blocks covered by the extent.
502 *
503 * @param inode_ref i-node to release extent and block from
504 * @param extent extent to release
505 * @return error code
506 */
507static int ext4_extent_release(
508 ext4_inode_ref_t *inode_ref, ext4_extent_t *extent)
509{
510 int rc;
511
512 // Compute number of the first physical block to release
513 uint64_t start = ext4_extent_get_start(extent);
514 uint16_t block_count = ext4_extent_get_block_count(extent);
515
516 rc = ext4_balloc_free_blocks(inode_ref, start, block_count);
517 if (rc != EOK) {
518 EXT4FS_DBG("Error in releasing data blocks");
519 return rc;
520 }
521
522 return EOK;
523}
524
525/** Recursively release the whole branch of the extent tree.
526 *
527 * For each entry of the node release the subbranch and finally release
528 * the node. In the leaf node all extents will be released.
529 *
530 * @param inode_ref i-node where the branch is released
531 * @param index index in the non-leaf node to be released
532 * with the whole subtree
533 * @return error code
534 */
535static int ext4_extent_release_branch(ext4_inode_ref_t *inode_ref,
536 ext4_extent_index_t *index)
537{
538 int rc;
539
540 block_t* block;
541
542 uint32_t fblock = ext4_extent_index_get_leaf(index);
543
544 rc = block_get(&block, inode_ref->fs->device, fblock, BLOCK_FLAGS_NOREAD);
545 if (rc != EOK) {
546 EXT4FS_DBG("ERROR get_block");
547 return rc;
548 }
549
550 ext4_extent_header_t *header = block->data;
551
552 if (ext4_extent_header_get_depth(header)) {
553
554 // The node is non-leaf, do recursion
555
556 ext4_extent_index_t *idx = EXT4_EXTENT_FIRST_INDEX(header);
557
558 // Release all subbranches
559 for (uint32_t i = 0; i < ext4_extent_header_get_entries_count(header); ++i, ++idx) {
560 rc = ext4_extent_release_branch(inode_ref, idx);
561 if (rc != EOK) {
562 EXT4FS_DBG("error recursion");
563 return rc;
564 }
565 }
566 } else {
567
568 // Leaf node reached
569 ext4_extent_t *ext = EXT4_EXTENT_FIRST(header);
570
571 // Release all extents and stop recursion
572
573 for (uint32_t i = 0; i < ext4_extent_header_get_entries_count(header); ++i, ++ext) {
574 rc = ext4_extent_release(inode_ref, ext);
575 if (rc != EOK) {
576 EXT4FS_DBG("error recursion");
577 return rc;
578 }
579 }
580 }
581
582 // Release data block where the node was stored
583
584 rc = block_put(block);
585 if (rc != EOK) {
586 EXT4FS_DBG("ERROR put_block");
587 return rc;
588 }
589
590 ext4_balloc_free_block(inode_ref, fblock);
591
592 return EOK;
593}
594
595/** Release all data blocks starting from specified logical block.
596 *
597 * @param inode_ref i-node to release blocks from
598 * @param iblock_from first logical block to release
599 */
600int ext4_extent_release_blocks_from(ext4_inode_ref_t *inode_ref,
601 uint32_t iblock_from)
602{
603 int rc = EOK;
604
605 // Find the first extent to modify
606 ext4_extent_path_t *path;
607 rc = ext4_extent_find_extent(inode_ref, iblock_from, &path);
608 if (rc != EOK) {
609 return rc;
610 }
611
612 // Jump to last item of the path (extent)
613 ext4_extent_path_t *path_ptr = path;
614 while (path_ptr->depth != 0) {
615 path_ptr++;
616 }
617
618 assert(path_ptr->extent != NULL);
619
620 // First extent maybe released partially
621 uint32_t first_fblock;
622 first_fblock = ext4_extent_get_start(path_ptr->extent) + iblock_from;
623 first_fblock -= ext4_extent_get_first_block(path_ptr->extent);
624
625 uint16_t block_count = ext4_extent_get_block_count(path_ptr->extent);
626
627 uint16_t delete_count = block_count - first_fblock +
628 ext4_extent_get_start(path_ptr->extent);
629
630 // Release all blocks
631 rc = ext4_balloc_free_blocks(inode_ref, first_fblock, delete_count);
632 if (rc != EOK) {
633 goto cleanup;
634 }
635
636 // Correct counter
637 block_count -= delete_count;
638 ext4_extent_set_block_count(path_ptr->extent, block_count);
639
640 // Initialize the following loop
641 uint16_t entries = ext4_extent_header_get_entries_count(path_ptr->header);
642 ext4_extent_t *tmp_ext = path_ptr->extent + 1;
643 ext4_extent_t *stop_ext = EXT4_EXTENT_FIRST(path_ptr->header) + entries;
644
645 // If first extent empty, release it
646 if (block_count == 0) {
647 entries--;
648 ext4_extent_header_set_entries_count(path_ptr->header, entries);
649 }
650
651 // Release all successors of the first extent in the same node
652 while (tmp_ext < stop_ext) {
653 first_fblock = ext4_extent_get_start(tmp_ext);
654 delete_count = ext4_extent_get_block_count(tmp_ext);
655
656 rc = ext4_balloc_free_blocks(inode_ref, first_fblock, delete_count);
657 if (rc != EOK) {
658 goto cleanup;
659 }
660
661 entries--;
662 ext4_extent_header_set_entries_count(path_ptr->header, entries);
663
664 tmp_ext++;
665 }
666
667 // If leaf node is empty, the whole tree must be checked and the node will be released
668 bool check_tree = false;
669
670 // Don't release root block (including inode data) !!!
671 if ((path_ptr != path) && (entries == 0)) {
672 rc = ext4_balloc_free_block(inode_ref, path_ptr->block->lba);
673 if (rc != EOK) {
674 goto cleanup;
675 }
676 check_tree = true;
677 }
678
679 // Jump to the parent
680 --path_ptr;
681
682 // release all successors in all tree levels
683 while (path_ptr >= path) {
684 entries = ext4_extent_header_get_entries_count(path_ptr->header);
685 ext4_extent_index_t *index = path_ptr->index + 1;
686 ext4_extent_index_t *stop =
687 EXT4_EXTENT_FIRST_INDEX(path_ptr->header) + entries;
688
689 // Correct entry because of changes in the previous iteration
690 if (check_tree) {
691 entries--;
692 ext4_extent_header_set_entries_count(path_ptr->header, entries);
693 } else {
694 // TODO check this condition
695 break;
696 }
697
698 // Iterate over all entries and release the whole subtrees
699 while (index < stop) {
700 rc = ext4_extent_release_branch(inode_ref, index);
701 if (rc != EOK) {
702 goto cleanup;
703 }
704 ++index;
705 --entries;
706 ext4_extent_header_set_entries_count(path_ptr->header, entries);
707 }
708
709 path_ptr->block->dirty = true;
710
711 // Free the node if it is empty
712 if ((entries == 0) && (path_ptr != path)) {
713 rc = ext4_balloc_free_block(inode_ref, path_ptr->block->lba);
714 if (rc != EOK) {
715 goto cleanup;
716 }
717
718 // Mark parent to be checked
719 check_tree = true;
720 } else {
721 check_tree = false;
722 }
723
724 --path_ptr;
725 }
726
727
728cleanup:
729 // Put loaded blocks
730 // From 1 -> 0 is a block with inode data
731 for (uint16_t i = 1; i <= path->depth; ++i) {
732 if (path[i].block) {
733 block_put(path[i].block);
734 }
735 }
736
737 // Destroy temporary data structure
738 free(path);
739
740 return rc;
741}
742
743
744/** Append new extent to the i-node and do some splitting if necessary.
745 *
746 * @param inode_ref i-node to append extent to
747 * @param path path in the extent tree for possible splitting
748 * @param last_path_item input/output parameter for pointer to the last
749 * valid item in the extent tree path
750 * @param iblock logical index of block to append extent for
751 * @return error code
752 */
753static int ext4_extent_append_extent(ext4_inode_ref_t *inode_ref,
754 ext4_extent_path_t *path, ext4_extent_path_t **last_path_item,
755 uint32_t iblock)
756{
757 EXT4FS_DBG("");
758 int rc;
759
760 ext4_extent_path_t *path_ptr = *last_path_item;
761
762 uint16_t entries = ext4_extent_header_get_entries_count(path_ptr->header);
763 uint16_t limit = ext4_extent_header_get_max_entries_count(path_ptr->header);
764
765 // Trivial way - no splitting
766 if (entries < limit) {
767 EXT4FS_DBG("adding extent entry");
768
769 ext4_extent_header_set_entries_count(path_ptr->header, entries + 1);
770 path_ptr->extent = EXT4_EXTENT_FIRST(path_ptr->header) + entries;
771 ext4_extent_set_block_count(path_ptr->extent, 0);
772 ext4_extent_set_first_block(path_ptr->extent, iblock);
773 ext4_extent_set_start(path_ptr->extent, 0);
774 path_ptr->block->dirty = true;
775
776 return EOK;
777 }
778
779 uint32_t block_size =
780 ext4_superblock_get_block_size(inode_ref->fs->superblock);
781
782 // Trivial tree - grow (extents were in root node)
783 if (path_ptr == path) {
784
785 uint32_t new_fblock;
786 rc = ext4_balloc_alloc_block(inode_ref, &new_fblock);
787 if (rc != EOK) {
788 EXT4FS_DBG("error in block allocation");
789 return rc;
790 }
791
792 block_t *block;
793 rc = block_get(&block, inode_ref->fs->device,
794 new_fblock, BLOCK_FLAGS_NOREAD);
795 if (rc != EOK) {
796 EXT4FS_DBG("error in block_get");
797 return rc;
798 }
799
800 memset(block->data, 0, block_size);
801
802 // Move data from root to the new block
803 memcpy(block->data, inode_ref->inode->blocks,
804 EXT4_INODE_BLOCKS * sizeof(uint32_t));
805
806 path_ptr++;
807 path_ptr->block = block;
808 path_ptr->header = (ext4_extent_header_t *)block->data;
809 path_ptr->depth = ext4_extent_header_get_depth(path_ptr->header);
810 path_ptr->index = NULL;
811
812 uint16_t entries = ext4_extent_header_get_entries_count(path_ptr->header);
813 path_ptr->extent = EXT4_EXTENT_FIRST(path_ptr->header) + entries;
814 ext4_extent_header_set_entries_count(path_ptr->header, entries + 1);
815 uint16_t limit = (block_size - sizeof(ext4_extent_header_t)) /
816 sizeof(ext4_extent_t);
817 ext4_extent_header_set_max_entries_count(path_ptr->header, limit);
818
819 // Modify root (in inode)
820 path->depth = 1;
821 path->extent = NULL;
822 path->index = EXT4_EXTENT_FIRST_INDEX(path->header);
823
824 ext4_extent_header_set_depth(path->header, path_ptr->depth + 1);
825 ext4_extent_header_set_entries_count(path->header, 1);
826
827 ext4_extent_index_set_first_block(path->index, 0);
828 ext4_extent_index_set_leaf(path->index, new_fblock);
829
830 path_ptr->block->dirty = true;
831 path->block->dirty = true;
832
833 *last_path_item = path_ptr;
834
835 return EOK;
836 }
837
838 assert(false);
839
840 // start splitting
841 uint32_t fblock = 0;
842 while (path_ptr > path) {
843
844 // TODO
845
846 rc = ext4_balloc_alloc_block(inode_ref, &fblock);
847 if (rc != EOK) {
848 return rc;
849 }
850
851 block_t *block;
852 rc = block_get(&block, inode_ref->fs->device, fblock, BLOCK_FLAGS_NOREAD);
853 if (rc != EOK) {
854 ext4_balloc_free_block(inode_ref, fblock);
855 return rc;
856 }
857
858 // Init block
859 memset(block->data, 0, block_size);
860
861 // Not modified
862 block_put(path_ptr->block);
863 path_ptr->block = block;
864
865 path_ptr->header = block->data;
866
867 if (path_ptr->depth) {
868 path_ptr->index = EXT4_EXTENT_FIRST_INDEX(path_ptr->header);
869 } else {
870 path_ptr->extent = EXT4_EXTENT_FIRST(path_ptr->header);
871 }
872
873 path_ptr--;
874 }
875
876 // If splitting reached root node
877 if (path_ptr == path) {
878
879 uint32_t new_fblock;
880 rc = ext4_balloc_alloc_block(inode_ref, &new_fblock);
881 if (rc != EOK) {
882 EXT4FS_DBG("error in block allocation");
883 return rc;
884 }
885
886 block_t *block;
887 rc = block_get(&block, inode_ref->fs->device,
888 new_fblock, BLOCK_FLAGS_NOREAD);
889 if (rc != EOK) {
890 EXT4FS_DBG("error in block_get");
891 return rc;
892 }
893
894 memset(block->data, 0, block_size);
895
896 // Move data from root to the new block
897 memcpy(block->data, inode_ref->inode->blocks,
898 EXT4_INODE_BLOCKS * sizeof(uint32_t));
899
900 path_ptr++;
901 path_ptr->block = block;
902 path_ptr->header = (ext4_extent_header_t *)block->data;
903 path_ptr->depth = ext4_extent_header_get_depth(path_ptr->header);
904 path_ptr->index = NULL;
905
906 uint16_t entries = ext4_extent_header_get_entries_count(path_ptr->header);
907 path_ptr->extent = EXT4_EXTENT_FIRST(path_ptr->header) + entries;
908 ext4_extent_header_set_entries_count(path_ptr->header, entries + 1);
909 uint16_t limit = (block_size - sizeof(ext4_extent_header_t)) /
910 sizeof(ext4_extent_t);
911 ext4_extent_header_set_max_entries_count(path_ptr->header, limit);
912
913 // Modify root (in inode)
914 path->depth = 1;
915 path->extent = NULL;
916 path->index = EXT4_EXTENT_FIRST_INDEX(path->header);
917
918 ext4_extent_header_set_depth(path->header, path_ptr->depth + 1);
919 ext4_extent_header_set_entries_count(path->header, 1);
920
921 ext4_extent_index_set_first_block(path->index, 0);
922 ext4_extent_index_set_leaf(path->index, new_fblock);
923
924 path_ptr->block->dirty = true;
925 path->block->dirty = true;
926
927 *last_path_item = path_ptr;
928
929 return EOK;
930 }
931
932 return EOK;
933}
934
935/** TODO comment
936 *
937 */
938int ext4_extent_append_block(ext4_inode_ref_t *inode_ref,
939 uint32_t *iblock, uint32_t *fblock)
940{
941 EXT4FS_DBG("");
942 int rc = EOK;
943
944 ext4_superblock_t *sb = inode_ref->fs->superblock;
945 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
946 uint32_t block_size = ext4_superblock_get_block_size(sb);
947
948 // Calculate number of new logical block
949 uint32_t new_block_idx = 0;
950 if (inode_size > 0) {
951 if ((inode_size % block_size) != 0) {
952 inode_size += block_size - (inode_size % block_size);
953 }
954 new_block_idx = inode_size / block_size;
955 }
956
957 // Load the nearest leaf (with extent)
958 ext4_extent_path_t *path;
959 rc = ext4_extent_find_extent(inode_ref, new_block_idx, &path);
960 if (rc != EOK) {
961 return rc;
962 }
963
964 // Jump to last item of the path (extent)
965 ext4_extent_path_t *path_ptr = path;
966 while (path_ptr->depth != 0) {
967 path_ptr++;
968 }
969
970 // Add new extent to the node
971 if (path_ptr->extent == NULL) {
972 goto append_extent;
973 }
974
975 uint16_t block_count = ext4_extent_get_block_count(path_ptr->extent);
976 uint16_t block_limit = (1 << 15);
977
978 uint32_t phys_block = 0;
979 if (block_count < block_limit) {
980
981 if (block_count == 0) {
982
983 rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
984 if (rc != EOK) {
985 goto finish;
986 }
987
988 ext4_extent_set_first_block(path_ptr->extent, new_block_idx);
989 ext4_extent_set_start(path_ptr->extent, phys_block);
990 ext4_extent_set_block_count(path_ptr->extent, 1);
991
992 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
993 inode_ref->dirty = true;
994
995 path_ptr->block->dirty = true;
996
997 goto finish;
998 } else {
999
1000 phys_block = ext4_extent_get_start(path_ptr->extent);
1001 phys_block += ext4_extent_get_block_count(path_ptr->extent);
1002
1003 bool free;
1004 rc = ext4_balloc_try_alloc_block(inode_ref, phys_block, &free);
1005 if (rc != EOK) {
1006 goto finish;
1007 }
1008
1009 if (! free) {
1010 // target is not free
1011 goto append_extent;
1012 }
1013
1014
1015 ext4_extent_set_block_count(path_ptr->extent, block_count + 1);
1016
1017 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1018 inode_ref->dirty = true;
1019
1020 path_ptr->block->dirty = true;
1021
1022 goto finish;
1023 }
1024 }
1025
1026append_extent:
1027
1028 phys_block = 0;
1029 // Allocate and insert insert new block
1030 rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
1031 if (rc != EOK) {
1032 EXT4FS_DBG("error in block allocation, rc = \%d", rc);
1033 goto finish;
1034 }
1035
1036 rc = ext4_extent_append_extent(inode_ref, path, &path_ptr, new_block_idx);
1037 if (rc != EOK) {
1038 ext4_balloc_free_block(inode_ref, phys_block);
1039 goto finish;
1040 }
1041
1042 ext4_extent_set_block_count(path_ptr->extent, 1);
1043 ext4_extent_set_first_block(path_ptr->extent, new_block_idx);
1044 ext4_extent_set_start(path_ptr->extent, phys_block);
1045
1046 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1047 inode_ref->dirty = true;
1048
1049 path_ptr->block->dirty = true;
1050
1051finish:
1052
1053 *iblock = new_block_idx;
1054 *fblock = phys_block;
1055
1056 // Put loaded blocks
1057 // From 1 -> 0 is a block with inode data
1058 for (uint16_t i = 1; i <= path->depth; ++i) {
1059 if (path[i].block) {
1060 block_put(path[i].block);
1061 }
1062 }
1063
1064 // Destroy temporary data structure
1065 free(path);
1066
1067 return rc;
1068}
1069
1070/**
1071 * @}
1072 */
Note: See TracBrowser for help on using the repository browser.