Changeset 06d85e5 in mainline for uspace/lib/ext4/libext4_extent.c


Ignore:
Timestamp:
2012-06-18T11:09:34Z (12 years ago)
Author:
Frantisek Princ <frantisek.princ@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
2616a75b
Parents:
9a487cc
Message:

Most of comments modified by current coding style

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/ext4/libext4_extent.c

    r9a487cc r06d85e5  
    262262        uint16_t entries_count = ext4_extent_header_get_entries_count(header);
    263263
    264         // Initialize bounds
     264        /* Initialize bounds */
    265265        l = EXT4_EXTENT_FIRST_INDEX(header) + 1;
    266266        r = EXT4_EXTENT_FIRST_INDEX(header) + entries_count - 1;
    267267
    268         // Do binary search
     268        /* Do binary search */
    269269        while (l <= r) {
    270270                m = l + (r - l) / 2;
     
    277277        }
    278278
    279         // Set output value
     279        /* Set output value */
    280280        *index = l - 1;
    281281}
     
    296296
    297297        if (entries_count == 0) {
    298                 // this leaf is empty
     298                /* this leaf is empty */
    299299                *extent = NULL;
    300300                return;
    301301        }
    302302
    303         // Initialize bounds
     303        /* Initialize bounds */
    304304        l = EXT4_EXTENT_FIRST(header) + 1;
    305305        r = EXT4_EXTENT_FIRST(header) + entries_count - 1;
    306306
    307         // Do binary search
     307        /* Do binary search */
    308308        while (l <= r) {
    309309                m = l + (r - l) / 2;
     
    316316        }
    317317
    318         // Set output value
     318        /* Set output value */
    319319        *extent = l - 1;
    320320}
     
    334334        int rc;
    335335
    336         // Compute bound defined by i-node size
     336        /* Compute bound defined by i-node size */
    337337        uint64_t inode_size = ext4_inode_get_size(
    338338                        inode_ref->fs->superblock, inode_ref->inode);
     
    343343        uint32_t last_idx = (inode_size - 1) / block_size;
    344344
    345         // Check if requested iblock is not over size of i-node
     345        /* Check if requested iblock is not over size of i-node */
    346346        if (iblock > last_idx) {
    347347                *fblock = 0;
     
    351351        block_t* block = NULL;
    352352
    353         // Walk through extent tree
     353        /* Walk through extent tree */
    354354        ext4_extent_header_t *header = ext4_inode_get_extent_header(inode_ref->inode);
    355355
    356 //      EXT4FS_DBG("inode = \%u", inode_ref->index);
    357 //      EXT4FS_DBG("count = \%u", ext4_extent_header_get_entries_count(header));
    358 
    359356        while (ext4_extent_header_get_depth(header) != 0) {
    360357
    361                 // Search index in node
     358                /* Search index in node */
    362359                ext4_extent_index_t *index;
    363360                ext4_extent_binsearch_idx(header, &index, iblock);
    364361
    365                 // Load child node and set values for the next iteration
     362                /* Load child node and set values for the next iteration */
    366363                uint64_t child = ext4_extent_index_get_leaf(index);
    367364
     
    378375        }
    379376
    380         // Search extent in the leaf block
     377        /* Search extent in the leaf block */
    381378        ext4_extent_t* extent = NULL;
    382379        ext4_extent_binsearch(header, &extent, iblock);
    383380
    384         // Prevent empty leaf
     381        /* Prevent empty leaf */
    385382        if (extent == NULL) {
    386383                *fblock = 0;
    387384        } else {
    388385
    389 //              EXT4FS_DBG("required = \%u, first = \%u, start = \%u, count = \%u", iblock, ext4_extent_get_first_block(extent), (uint32_t)ext4_extent_get_start(extent), ext4_extent_get_block_count(extent));
    390 
    391                 // Compute requested physical block address
     386                /* Compute requested physical block address */
    392387                uint32_t phys_block;
    393388                uint32_t first = ext4_extent_get_first_block(extent);
    394389                phys_block = ext4_extent_get_start(extent) + iblock - first;
    395390
    396 //              phys_block = ext4_extent_get_start(extent) + iblock;
    397 //              phys_block -= ext4_extent_get_first_block(extent);
    398 
    399391                *fblock = phys_block;
    400392        }
    401393
    402         // Cleanup
     394        /* Cleanup */
    403395        if (block != NULL) {
    404396                block_put(block);
     
    431423        ext4_extent_path_t *tmp_path;
    432424
    433         // Added 2 for possible tree growing
     425        /* Added 2 for possible tree growing */
    434426        tmp_path = malloc(sizeof(ext4_extent_path_t) * (depth + 2));
    435427        if (tmp_path == NULL) {
     
    437429        }
    438430
    439         // Initialize structure for algorithm start
     431        /* Initialize structure for algorithm start */
    440432        tmp_path[0].block = inode_ref->block;
    441433        tmp_path[0].header = eh;
    442434
    443         // Walk through the extent tree
     435        /* Walk through the extent tree */
    444436        uint16_t pos = 0;
    445437        while (ext4_extent_header_get_depth(eh) != 0) {
    446438
    447                 // Search index in index node by iblock
     439                /* Search index in index node by iblock */
    448440                ext4_extent_binsearch_idx(tmp_path[pos].header, &tmp_path[pos].index, iblock);
    449441
     
    453445                assert(tmp_path[pos].index != NULL);
    454446
    455                 // Load information for the next iteration
     447                /* Load information for the next iteration */
    456448                uint64_t fblock = ext4_extent_index_get_leaf(tmp_path[pos].index);
    457449
     
    474466        tmp_path[pos].index = NULL;
    475467
    476     // Find extent in the leaf node
     468    /* Find extent in the leaf node */
    477469        ext4_extent_binsearch(tmp_path[pos].header, &tmp_path[pos].extent, iblock);
    478470
     
    482474
    483475cleanup:
    484         // Put loaded blocks
    485         // From 1 -> 0 is a block with inode data
     476        /* Put loaded blocks
     477         * From 1: 0 is a block with inode data
     478         */
    486479        for (uint16_t i = 1; i < tmp_path->depth; ++i) {
    487480                if (tmp_path[i].block) {
     
    490483        }
    491484
    492         // Destroy temporary data structure
     485        /* Destroy temporary data structure */
    493486        free(tmp_path);
    494487
     
    507500        int rc;
    508501
    509         // Compute number of the first physical block to release
     502        /* Compute number of the first physical block to release */
    510503        uint64_t start = ext4_extent_get_start(extent);
    511504        uint16_t block_count = ext4_extent_get_block_count(extent);
     
    549542        if (ext4_extent_header_get_depth(header)) {
    550543
    551                 // The node is non-leaf, do recursion
     544                /* The node is non-leaf, do recursion */
    552545
    553546                ext4_extent_index_t *idx = EXT4_EXTENT_FIRST_INDEX(header);
    554547
    555                 // Release all subbranches
     548                /* Release all subbranches */
    556549                for (uint32_t i = 0; i < ext4_extent_header_get_entries_count(header); ++i, ++idx) {
    557550                        rc = ext4_extent_release_branch(inode_ref, idx);
     
    563556        } else {
    564557
    565                 // Leaf node reached
     558                /* Leaf node reached */
    566559                ext4_extent_t *ext = EXT4_EXTENT_FIRST(header);
    567560
    568                 // Release all extents and stop recursion
     561                /* Release all extents and stop recursion */
    569562
    570563                for (uint32_t i = 0; i < ext4_extent_header_get_entries_count(header); ++i, ++ext) {
     
    577570        }
    578571
    579         // Release data block where the node was stored
     572        /* Release data block where the node was stored */
    580573
    581574        rc = block_put(block);
     
    600593        int rc = EOK;
    601594
    602         // Find the first extent to modify
     595        /* Find the first extent to modify */
    603596        ext4_extent_path_t *path;
    604597        rc = ext4_extent_find_extent(inode_ref, iblock_from, &path);
     
    607600        }
    608601
    609         // Jump to last item of the path (extent)
     602        /* Jump to last item of the path (extent) */
    610603        ext4_extent_path_t *path_ptr = path;
    611604        while (path_ptr->depth != 0) {
     
    615608        assert(path_ptr->extent != NULL);
    616609
    617         // First extent maybe released partially
     610        /* First extent maybe released partially */
    618611        uint32_t first_iblock = ext4_extent_get_first_block(path_ptr->extent);
    619612        uint32_t first_fblock = ext4_extent_get_start(path_ptr->extent) + iblock_from - first_iblock;
     
    625618                        ext4_extent_get_start(path_ptr->extent) - first_fblock);
    626619
    627         // Release all blocks
     620        /* Release all blocks */
    628621        rc = ext4_balloc_free_blocks(inode_ref, first_fblock, delete_count);
    629622        if (rc != EOK) {
     
    631624        }
    632625
    633         // Correct counter
     626        /* Correct counter */
    634627        block_count -= delete_count;
    635628        ext4_extent_set_block_count(path_ptr->extent, block_count);
    636629
    637         // Initialize the following loop
     630        /* Initialize the following loop */
    638631        uint16_t entries = ext4_extent_header_get_entries_count(path_ptr->header);
    639632        ext4_extent_t *tmp_ext = path_ptr->extent + 1;
    640633        ext4_extent_t *stop_ext = EXT4_EXTENT_FIRST(path_ptr->header) + entries;
    641634
    642         // If first extent empty, release it
     635        /* If first extent empty, release it */
    643636        if (block_count == 0) {
    644637                entries--;
    645638        }
    646639
    647         // Release all successors of the first extent in the same node
     640        /* Release all successors of the first extent in the same node */
    648641        while (tmp_ext < stop_ext) {
    649642                first_fblock = ext4_extent_get_start(tmp_ext);
     
    662655        path_ptr->block->dirty = true;
    663656
    664         // If leaf node is empty, parent entry must be modified
     657        /* If leaf node is empty, parent entry must be modified */
    665658        bool remove_parent_record = false;
    666659
    667         // Don't release root block (including inode data) !!!
     660        /* Don't release root block (including inode data) !!! */
    668661        if ((path_ptr != path) && (entries == 0)) {
    669662                rc = ext4_balloc_free_block(inode_ref, path_ptr->block->lba);
     
    674667        }
    675668
    676         // Jump to the parent
     669        /* Jump to the parent */
    677670        --path_ptr;
    678671
    679         // release all successors in all tree levels
     672        /* Release all successors in all tree levels */
    680673        while (path_ptr >= path) {
    681674                entries = ext4_extent_header_get_entries_count(path_ptr->header);
     
    684677                                EXT4_EXTENT_FIRST_INDEX(path_ptr->header) + entries;
    685678
    686                 // Correct entries count because of changes in the previous iteration
     679                /* Correct entries count because of changes in the previous iteration */
    687680                if (remove_parent_record) {
    688681                        entries--;
    689682                }
    690683
    691                 // Iterate over all entries and release the whole subtrees
     684                /* Iterate over all entries and release the whole subtrees */
    692685                while (index < stop) {
    693686                        rc = ext4_extent_release_branch(inode_ref, index);
     
    702695                path_ptr->block->dirty = true;
    703696
    704                 // Free the node if it is empty
     697                /* Free the node if it is empty */
    705698                if ((entries == 0) && (path_ptr != path)) {
    706699                        rc = ext4_balloc_free_block(inode_ref, path_ptr->block->lba);
     
    709702                        }
    710703
    711                         // Mark parent to be checked
     704                        /* Mark parent to be checked */
    712705                        remove_parent_record = true;
    713706                } else {
     
    720713
    721714cleanup:
    722         // Put loaded blocks
    723         // From 1 -> 0 is a block with inode data
     715        /* Put loaded blocks
     716         * starting from 1: 0 is a block with inode data
     717         */
    724718        for (uint16_t i = 1; i <= path->depth; ++i) {
    725719                if (path[i].block) {
     
    728722        }
    729723
    730         // Destroy temporary data structure
     724        /* Destroy temporary data structure */
    731725        free(path);
    732726
     
    748742                uint32_t iblock)
    749743{
    750         EXT4FS_DBG("");
    751744        int rc;
    752745
     
    756749        uint16_t limit = ext4_extent_header_get_max_entries_count(path_ptr->header);
    757750
    758         // Trivial way - no splitting
     751        /* Trivial way - no splitting */
    759752        if (entries < limit) {
    760                 EXT4FS_DBG("adding extent entry");
    761753
    762754                ext4_extent_header_set_entries_count(path_ptr->header, entries + 1);
     
    773765                        ext4_superblock_get_block_size(inode_ref->fs->superblock);
    774766
    775         // Trivial tree - grow (extents were in root node)
     767        /* Trivial tree - grow (extents were in root node) */
    776768        if (path_ptr == path) {
    777769
     
    793785                memset(block->data, 0, block_size);
    794786
    795                 // Move data from root to the new block
     787                /* Move data from root to the new block */
    796788                memcpy(block->data, inode_ref->inode->blocks,
    797789                                EXT4_INODE_BLOCKS * sizeof(uint32_t));
     
    810802                ext4_extent_header_set_max_entries_count(path_ptr->header, limit);
    811803
    812                 // Modify root (in inode)
     804                /* Modify root (in inode) */
    813805                path->depth = 1;
    814806                path->extent = NULL;
     
    829821        }
    830822
    831         assert(false);
    832 
    833         // start splitting
     823        // TODO !!!
     824//      assert(false);
     825
     826        /* Start splitting */
    834827        uint32_t fblock = 0;
    835828        while (path_ptr > path) {
     
    849842                }
    850843
    851                 // Init block
     844                /* Init block */
    852845                memset(block->data, 0, block_size);
    853846
    854                 // Not modified
     847                /* Not modified */
    855848                block_put(path_ptr->block);
    856849                path_ptr->block = block;
     
    867860        }
    868861
    869         // If splitting reached root node
     862        /* If splitting reached root node */
    870863        if (path_ptr == path) {
    871864
     
    887880                memset(block->data, 0, block_size);
    888881
    889                 // Move data from root to the new block
     882                /* Move data from root to the new block */
    890883                memcpy(block->data, inode_ref->inode->blocks,
    891884                                EXT4_INODE_BLOCKS * sizeof(uint32_t));
     
    904897                ext4_extent_header_set_max_entries_count(path_ptr->header, limit);
    905898
    906                 // Modify root (in inode)
     899                /* Modify root (in inode) */
    907900                path->depth = 1;
    908901                path->extent = NULL;
     
    940933                uint32_t *iblock, uint32_t *fblock)
    941934{
    942         EXT4FS_DBG("");
    943935        int rc = EOK;
    944936
     
    947939        uint32_t block_size = ext4_superblock_get_block_size(sb);
    948940
    949         // Calculate number of new logical block
     941        /* Calculate number of new logical block */
    950942        uint32_t new_block_idx = 0;
    951943        if (inode_size > 0) {
     
    956948        }
    957949
    958         // Load the nearest leaf (with extent)
     950        /* Load the nearest leaf (with extent) */
    959951        ext4_extent_path_t *path;
    960952        rc = ext4_extent_find_extent(inode_ref, new_block_idx, &path);
     
    963955        }
    964956
    965         // Jump to last item of the path (extent)
     957        /* Jump to last item of the path (extent) */
    966958        ext4_extent_path_t *path_ptr = path;
    967959        while (path_ptr->depth != 0) {
     
    969961        }
    970962
    971         // Add new extent to the node if not present
     963        /* Add new extent to the node if not present */
    972964        if (path_ptr->extent == NULL) {
    973965                goto append_extent;
     
    980972        if (block_count < block_limit) {
    981973
    982                 // There is space for new block in the extent
     974                /* There is space for new block in the extent */
    983975
    984976                if (block_count == 0) {
    985977
    986                         // Existing extent is empty
     978                        /* Existing extent is empty */
    987979
    988980                        rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
     
    991983                        }
    992984
    993                         // Initialize extent
     985                        /* Initialize extent */
    994986                        ext4_extent_set_first_block(path_ptr->extent, new_block_idx);
    995987                        ext4_extent_set_start(path_ptr->extent, phys_block);
    996988                        ext4_extent_set_block_count(path_ptr->extent, 1);
    997989
    998                         // Update i-node
     990                        /* Update i-node */
    999991                        ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
    1000992                        inode_ref->dirty = true;
     
    1005997                } else {
    1006998
    1007                         // Existing extent contains some blocks
     999                        /* Existing extent contains some blocks */
    10081000
    10091001                        phys_block = ext4_extent_get_start(path_ptr->extent);
    10101002                        phys_block += ext4_extent_get_block_count(path_ptr->extent);
    10111003
    1012                         // Check if the following block is free for allocation
     1004                        /* Check if the following block is free for allocation */
    10131005                        bool free;
    10141006                        rc = ext4_balloc_try_alloc_block(inode_ref, phys_block, &free);
     
    10181010
    10191011                        if (! free) {
    1020                                 // target is not free, new block must be appended to new extent
     1012                                /* target is not free, new block must be appended to new extent */
    10211013                                goto append_extent;
    10221014                        }
    10231015
    10241016
    1025                         // Update extent
     1017                        /* Update extent */
    10261018                        ext4_extent_set_block_count(path_ptr->extent, block_count + 1);
    10271019
    1028                         // Update i-node
     1020                        /* Update i-node */
    10291021                        ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
    10301022                        inode_ref->dirty = true;
     
    10361028        }
    10371029
    1038 // Append new extent to the tree
     1030/* Append new extent to the tree */
    10391031append_extent:
    10401032
    10411033        phys_block = 0;
    10421034
    1043         // Allocate new data block
     1035        /* Allocate new data block */
    10441036        rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
    10451037        if (rc != EOK) {
     
    10481040        }
    10491041
    1050         // Append extent for new block (includes tree splitting if needed)
     1042        /* Append extent for new block (includes tree splitting if needed) */
    10511043        rc = ext4_extent_append_extent(inode_ref, path, &path_ptr, new_block_idx);
    10521044        if (rc != EOK) {
     
    10551047        }
    10561048
    1057         // Initialize newly created extent
     1049        /* Initialize newly created extent */
    10581050        ext4_extent_set_block_count(path_ptr->extent, 1);
    10591051        ext4_extent_set_first_block(path_ptr->extent, new_block_idx);
    10601052        ext4_extent_set_start(path_ptr->extent, phys_block);
    10611053
    1062         // Update i-node
     1054        /* Update i-node */
    10631055        ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
    10641056        inode_ref->dirty = true;
     
    10681060
    10691061finish:
    1070         // Set return values
     1062        /* Set return values */
    10711063        *iblock = new_block_idx;
    10721064        *fblock = phys_block;
    10731065
    1074         // Put loaded blocks
    1075         // From 1 -> 0 is a block with inode data
     1066        /* Put loaded blocks
     1067         * starting from 1: 0 is a block with inode data
     1068         */
    10761069        for (uint16_t i = 1; i <= path->depth; ++i) {
    10771070                if (path[i].block) {
     
    10801073        }
    10811074
    1082         // Destroy temporary data structure
     1075        /* Destroy temporary data structure */
    10831076        free(path);
    10841077
Note: See TracChangeset for help on using the changeset viewer.