Changeset a35b458 in mainline for uspace/lib/ext4/src/filesystem.c


Ignore:
Timestamp:
2018-03-02T20:10:49Z (7 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
f1380b7
Parents:
3061bc1
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2018-02-28 17:38:31)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2018-03-02 20:10:49)
Message:

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/ext4/src/filesystem.c

    r3061bc1 ra35b458  
    289289                return EOK;
    290290        }
    291        
     291
    292292        /*
    293293         * Check incompatible features - if filesystem has some,
     
    300300        if (incompatible_features > 0)
    301301                return ENOTSUP;
    302        
     302
    303303        /*
    304304         * Check read-only features, if filesystem has some,
     
    313313                return EOK;
    314314        }
    315        
     315
    316316        return EOK;
    317317}
     
    331331        uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
    332332        uint32_t first_block = ext4_superblock_get_first_data_block(sb);
    333        
     333
    334334        /* First block == 0 or 1 */
    335335        if (first_block == 0)
     
    351351{
    352352        uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
    353        
     353
    354354        if (ext4_superblock_get_first_data_block(sb) == 0)
    355355                return bgid * blocks_per_group + index;
     
    392392        uint64_t bitmap_inode_addr = ext4_block_group_get_inode_bitmap(
    393393            bg_ref->block_group, bg_ref->fs->superblock);
    394        
     394
    395395        block_t *bitmap_block;
    396396        errno_t rc = block_get(&bitmap_block, bg_ref->fs->device,
     
    398398        if (rc != EOK)
    399399                return rc;
    400        
     400
    401401        uint8_t *bitmap = bitmap_block->data;
    402        
     402
    403403        /* Initialize all bitmap bits to zero */
    404404        uint32_t block_size = ext4_superblock_get_block_size(sb);
    405405        memset(bitmap, 0, block_size);
    406        
     406
    407407        /* Determine the number of reserved blocks in the group */
    408408        uint32_t reserved_cnt = ext4_filesystem_bg_get_backup_blocks(bg_ref);
     
    439439
    440440        bitmap_block->dirty = true;
    441        
     441
    442442        /* Save bitmap */
    443443        return block_put(bitmap_block);
     
    457457            bg_ref->block_group, bg_ref->fs->superblock);
    458458        block_t *bitmap_block;
    459        
     459
    460460        errno_t rc = block_get(&bitmap_block, bg_ref->fs->device,
    461461            bitmap_block_addr, BLOCK_FLAGS_NOREAD);
    462462        if (rc != EOK)
    463463                return rc;
    464        
     464
    465465        uint8_t *bitmap = bitmap_block->data;
    466        
     466
    467467        /* Initialize all bitmap bits to zero */
    468468        uint32_t block_size = ext4_superblock_get_block_size(bg_ref->fs->superblock);
     
    470470            ext4_superblock_get_inodes_per_group(bg_ref->fs->superblock);
    471471        memset(bitmap, 0, (inodes_per_group + 7) / 8);
    472        
     472
    473473        uint32_t start_bit = inodes_per_group;
    474474        uint32_t end_bit = block_size * 8;
    475        
     475
    476476        uint32_t i;
    477477        for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
    478478                ext4_bitmap_set_bit(bitmap, i);
    479        
     479
    480480        if (i < end_bit)
    481481                memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
    482        
     482
    483483        bitmap_block->dirty = true;
    484        
     484
    485485        /* Save bitmap */
    486486        return block_put(bitmap_block);
     
    497497{
    498498        ext4_superblock_t *sb = bg_ref->fs->superblock;
    499        
     499
    500500        uint32_t inode_size = ext4_superblock_get_inode_size(sb);
    501501        uint32_t block_size = ext4_superblock_get_block_size(sb);
    502502        uint32_t inodes_per_block = block_size / inode_size;
    503        
     503
    504504        uint32_t inodes_in_group =
    505505            ext4_superblock_get_inodes_in_group(sb, bg_ref->index);
    506        
     506
    507507        uint32_t table_blocks = inodes_in_group / inodes_per_block;
    508        
     508
    509509        if (inodes_in_group % inodes_per_block)
    510510                table_blocks++;
    511        
     511
    512512        /* Compute initialization bounds */
    513513        uint32_t first_block = ext4_block_group_get_inode_table_first_block(
    514514            bg_ref->block_group, sb);
    515        
     515
    516516        uint32_t last_block = first_block + table_blocks - 1;
    517        
     517
    518518        /* Initialization of all itable blocks */
    519519        for (uint32_t fblock = first_block; fblock <= last_block; ++fblock) {
     
    523523                if (rc != EOK)
    524524                        return rc;
    525                
     525
    526526                memset(block->data, 0, block_size);
    527527                block->dirty = true;
    528                
     528
    529529                rc = block_put(block);
    530530                if (rc != EOK)
    531531                        return rc;
    532532        }
    533        
     533
    534534        return EOK;
    535535}
     
    552552        if (newref == NULL)
    553553                return ENOMEM;
    554        
     554
    555555        /* Compute number of descriptors, that fits in one data block */
    556556        uint32_t descriptors_per_block =
    557557            ext4_superblock_get_block_size(fs->superblock) /
    558558            ext4_superblock_get_desc_size(fs->superblock);
    559        
     559
    560560        /* Block group descriptor table starts at the next block after superblock */
    561561        aoff64_t block_id =
    562562            ext4_superblock_get_first_data_block(fs->superblock) + 1;
    563        
     563
    564564        /* Find the block containing the descriptor we are looking for */
    565565        block_id += bgid / descriptors_per_block;
    566566        uint32_t offset = (bgid % descriptors_per_block) *
    567567            ext4_superblock_get_desc_size(fs->superblock);
    568        
     568
    569569        /* Load block with descriptors */
    570570        errno_t rc = block_get(&newref->block, fs->device, block_id, 0);
     
    573573                return rc;
    574574        }
    575        
     575
    576576        /* Initialize in-memory representation */
    577577        newref->block_group = newref->block->data + offset;
     
    579579        newref->index = bgid;
    580580        newref->dirty = false;
    581        
     581
    582582        *ref = newref;
    583        
     583
    584584        if (ext4_block_group_has_flag(newref->block_group,
    585585            EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
     
    590590                        return rc;
    591591                }
    592                
     592
    593593                ext4_block_group_clear_flag(newref->block_group,
    594594                    EXT4_BLOCK_GROUP_BLOCK_UNINIT);
    595                
     595
    596596                newref->dirty = true;
    597597        }
    598        
     598
    599599        if (ext4_block_group_has_flag(newref->block_group,
    600600            EXT4_BLOCK_GROUP_INODE_UNINIT)) {
     
    605605                        return rc;
    606606                }
    607                
     607
    608608                ext4_block_group_clear_flag(newref->block_group,
    609609                    EXT4_BLOCK_GROUP_INODE_UNINIT);
    610                
     610
    611611                if (!ext4_block_group_has_flag(newref->block_group,
    612612                    EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
     
    614614                        if (rc != EOK)
    615615                                return rc;
    616                        
     616
    617617                        ext4_block_group_set_flag(newref->block_group,
    618618                            EXT4_BLOCK_GROUP_ITABLE_ZEROED);
    619619                }
    620                
     620
    621621                newref->dirty = true;
    622622        }
    623        
     623
    624624        return EOK;
    625625}
     
    645645                void *base = bg;
    646646                void *checksum = &bg->checksum;
    647                
     647
    648648                uint32_t offset = (uint32_t) (checksum - base);
    649                
     649
    650650                /* Convert block group index to little endian */
    651651                uint32_t le_group = host2uint32_t_le(bgid);
    652                
     652
    653653                /* Initialization */
    654654                crc = crc16_ibm(~0, sb->uuid, sizeof(sb->uuid));
    655                
     655
    656656                /* Include index of block group */
    657657                crc = crc16_ibm(crc, (uint8_t *) &le_group, sizeof(le_group));
    658                
     658
    659659                /* Compute crc from the first part (stop before checksum field) */
    660660                crc = crc16_ibm(crc, (uint8_t *) bg, offset);
    661                
     661
    662662                /* Skip checksum */
    663663                offset += sizeof(bg->checksum);
    664                
     664
    665665                /* Checksum of the rest of block group descriptor */
    666666                if ((ext4_superblock_has_feature_incompatible(sb,
     
    670670                            ext4_superblock_get_desc_size(sb) - offset);
    671671        }
    672        
     672
    673673        return crc;
    674674}
     
    813813                    ref->block_group);
    814814                ext4_block_group_set_checksum(ref->block_group, checksum);
    815                
     815
    816816                /* Mark block dirty for writing changes to physical device */
    817817                ref->block->dirty = true;
    818818        }
    819        
     819
    820820        /* Put back block, that contains block group descriptor */
    821821        errno_t rc = block_put(ref->block);
    822822        free(ref);
    823        
     823
    824824        return rc;
    825825}
     
    842842        if (newref == NULL)
    843843                return ENOMEM;
    844        
     844
    845845        /* Compute number of i-nodes, that fits in one data block */
    846846        uint32_t inodes_per_group =
    847847            ext4_superblock_get_inodes_per_group(fs->superblock);
    848        
     848
    849849        /*
    850850         * Inode numbers are 1-based, but it is simpler to work with 0-based
     
    854854        uint32_t block_group = index / inodes_per_group;
    855855        uint32_t offset_in_group = index % inodes_per_group;
    856        
     856
    857857        /* Load block group, where i-node is located */
    858858        ext4_block_group_ref_t *bg_ref;
     
    862862                return rc;
    863863        }
    864        
     864
    865865        /* Load block address, where i-node table is located */
    866866        uint32_t inode_table_start =
    867867            ext4_block_group_get_inode_table_first_block(bg_ref->block_group,
    868868            fs->superblock);
    869        
     869
    870870        /* Put back block group reference (not needed more) */
    871871        rc = ext4_filesystem_put_block_group_ref(bg_ref);
     
    874874                return rc;
    875875        }
    876        
     876
    877877        /* Compute position of i-node in the block group */
    878878        uint16_t inode_size = ext4_superblock_get_inode_size(fs->superblock);
    879879        uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
    880880        uint32_t byte_offset_in_group = offset_in_group * inode_size;
    881        
     881
    882882        /* Compute block address */
    883883        aoff64_t block_id = inode_table_start + (byte_offset_in_group / block_size);
     
    887887                return rc;
    888888        }
    889        
     889
    890890        /* Compute position of i-node in the data block */
    891891        uint32_t offset_in_block = byte_offset_in_group % block_size;
    892892        newref->inode = newref->block->data + offset_in_block;
    893        
     893
    894894        /* We need to store the original value of index in the reference */
    895895        newref->index = index + 1;
    896896        newref->fs = fs;
    897897        newref->dirty = false;
    898        
     898
    899899        *ref = newref;
    900        
     900
    901901        return EOK;
    902902}
     
    916916                ref->block->dirty = true;
    917917        }
    918        
     918
    919919        /* Put back block, that contains i-node */
    920920        errno_t rc = block_put(ref->block);
    921921        free(ref);
    922        
     922
    923923        return rc;
    924924}
     
    940940        if (flags & L_DIRECTORY)
    941941                is_dir = true;
    942        
     942
    943943        /* Allocate inode by allocation algorithm */
    944944        uint32_t index;
     
    946946        if (rc != EOK)
    947947                return rc;
    948        
     948
    949949        /* Load i-node from on-disk i-node table */
    950950        rc = ext4_filesystem_get_inode_ref(fs, index, inode_ref);
     
    953953                return rc;
    954954        }
    955        
     955
    956956        /* Initialize i-node */
    957957        ext4_inode_t *inode = (*inode_ref)->inode;
    958        
     958
    959959        uint16_t mode;
    960960        if (is_dir) {
     
    963963                 * 0777 (octal) == rwxrwxrwx
    964964                 */
    965                
     965
    966966                mode = 0777;
    967967                mode |= EXT4_INODE_MODE_DIRECTORY;
     
    973973                 * 0666 (octal) == rw-rw-rw-
    974974                 */
    975                
     975
    976976                mode = 0666;
    977977                mode |= EXT4_INODE_MODE_FILE;
     
    979979                ext4_inode_set_links_count(inode, 0);
    980980        }
    981        
     981
    982982        ext4_inode_set_uid(inode, 0);
    983983        ext4_inode_set_gid(inode, 0);
     
    990990        ext4_inode_set_flags(inode, 0);
    991991        ext4_inode_set_generation(inode, 0);
    992        
     992
    993993        /* Reset blocks array */
    994994        for (uint32_t i = 0; i < EXT4_INODE_BLOCKS; i++)
    995995                inode->blocks[i] = 0;
    996        
     996
    997997        /* Initialize extents if needed */
    998998        if (ext4_superblock_has_feature_incompatible(
    999999            fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
    10001000                ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
    1001                
     1001
    10021002                /* Initialize extent root header */
    10031003                ext4_extent_header_t *header = ext4_inode_get_extent_header(inode);
     
    10061006                ext4_extent_header_set_generation(header, 0);
    10071007                ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
    1008                
     1008
    10091009                uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
    10101010                    sizeof(ext4_extent_header_t)) / sizeof(ext4_extent_t);
    1011                
     1011
    10121012                ext4_extent_header_set_max_entries_count(header, max_entries);
    10131013        }
    1014        
     1014
    10151015        (*inode_ref)->dirty = true;
    1016        
     1016
    10171017        return EOK;
    10181018}
     
    10281028{
    10291029        ext4_filesystem_t *fs = inode_ref->fs;
    1030        
     1030
    10311031        /* For extents must be data block destroyed by other way */
    10321032        if ((ext4_superblock_has_feature_incompatible(fs->superblock,
     
    10361036                goto finish;
    10371037        }
    1038        
     1038
    10391039        /* Release all indirect (no data) blocks */
    1040        
     1040
    10411041        /* 1) Single indirect */
    10421042        uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
     
    10451045                if (rc != EOK)
    10461046                        return rc;
    1047                
     1047
    10481048                ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
    10491049        }
    1050        
     1050
    10511051        block_t *block;
    10521052        uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
    10531053        uint32_t count = block_size / sizeof(uint32_t);
    1054        
     1054
    10551055        /* 2) Double indirect */
    10561056        fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
     
    10591059                if (rc != EOK)
    10601060                        return rc;
    1061                
     1061
    10621062                uint32_t ind_block;
    10631063                for (uint32_t offset = 0; offset < count; ++offset) {
    10641064                        ind_block = uint32_t_le2host(((uint32_t *) block->data)[offset]);
    1065                        
     1065
    10661066                        if (ind_block != 0) {
    10671067                                rc = ext4_balloc_free_block(inode_ref, ind_block);
     
    10721072                        }
    10731073                }
    1074                
     1074
    10751075                rc = block_put(block);
    10761076                if (rc != EOK)
     
    10801080                if (rc != EOK)
    10811081                        return rc;
    1082                
     1082
    10831083                ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
    10841084        }
    1085        
     1085
    10861086        /* 3) Tripple indirect */
    10871087        block_t *subblock;
     
    10911091                if (rc != EOK)
    10921092                        return rc;
    1093                
     1093
    10941094                uint32_t ind_block;
    10951095                for (uint32_t offset = 0; offset < count; ++offset) {
    10961096                        ind_block = uint32_t_le2host(((uint32_t *) block->data)[offset]);
    1097                        
     1097
    10981098                        if (ind_block != 0) {
    10991099                                rc = block_get(&subblock, fs->device, ind_block,
     
    11031103                                        return rc;
    11041104                                }
    1105                                
     1105
    11061106                                uint32_t ind_subblock;
    11071107                                for (uint32_t suboffset = 0; suboffset < count;
     
    11091109                                        ind_subblock = uint32_t_le2host(((uint32_t *)
    11101110                                            subblock->data)[suboffset]);
    1111                                        
     1111
    11121112                                        if (ind_subblock != 0) {
    11131113                                                rc = ext4_balloc_free_block(inode_ref, ind_subblock);
     
    11191119                                        }
    11201120                                }
    1121                                
     1121
    11221122                                rc = block_put(subblock);
    11231123                                if (rc != EOK) {
     
    11261126                                }
    11271127                        }
    1128                        
     1128
    11291129                        rc = ext4_balloc_free_block(inode_ref, ind_block);
    11301130                        if (rc != EOK) {
     
    11331133                        }
    11341134                }
    1135                
     1135
    11361136                rc = block_put(block);
    11371137                if (rc != EOK)
     
    11411141                if (rc != EOK)
    11421142                        return rc;
    1143                
     1143
    11441144                ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
    11451145        }
    1146        
     1146
    11471147finish:
    11481148        /* Mark inode dirty for writing to the physical device */
    11491149        inode_ref->dirty = true;
    1150        
     1150
    11511151        /* Free block with extended attributes if present */
    11521152        uint32_t xattr_block = ext4_inode_get_file_acl(
     
    11561156                if (rc != EOK)
    11571157                        return rc;
    1158                
     1158
    11591159                ext4_inode_set_file_acl(inode_ref->inode, fs->superblock, 0);
    11601160        }
    1161        
     1161
    11621162        /* Free inode by allocator */
    11631163        errno_t rc;
     
    11671167        else
    11681168                rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
    1169        
     1169
    11701170        return rc;
    11711171}
     
    11831183{
    11841184        ext4_superblock_t *sb = inode_ref->fs->superblock;
    1185        
     1185
    11861186        /* Check flags, if i-node can be truncated */
    11871187        if (!ext4_inode_can_truncate(sb, inode_ref->inode))
    11881188                return EINVAL;
    1189        
     1189
    11901190        /* If sizes are equal, nothing has to be done. */
    11911191        aoff64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
    11921192        if (old_size == new_size)
    11931193                return EOK;
    1194        
     1194
    11951195        /* It's not suppported to make the larger file by truncate operation */
    11961196        if (old_size < new_size)
    11971197                return EINVAL;
    1198        
     1198
    11991199        /* Compute how many blocks will be released */
    12001200        aoff64_t size_diff = old_size - new_size;
     
    12031203        if (size_diff % block_size != 0)
    12041204                diff_blocks_count++;
    1205        
     1205
    12061206        uint32_t old_blocks_count = old_size / block_size;
    12071207        if (old_size % block_size != 0)
    12081208                old_blocks_count++;
    1209        
     1209
    12101210        if ((ext4_superblock_has_feature_incompatible(inode_ref->fs->superblock,
    12111211            EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
     
    12181218        } else {
    12191219                /* Release data blocks from the end of file */
    1220                
     1220
    12211221                /* Starting from 1 because of logical blocks are numbered from 0 */
    12221222                for (uint32_t i = 1; i <= diff_blocks_count; ++i) {
     
    12271227                }
    12281228        }
    1229        
     1229
    12301230        /* Update i-node */
    12311231        ext4_inode_set_size(inode_ref->inode, new_size);
    12321232        inode_ref->dirty = true;
    1233        
     1233
    12341234        return EOK;
    12351235}
     
    12481248{
    12491249        ext4_filesystem_t *fs = inode_ref->fs;
    1250        
     1250
    12511251        /* For empty file is situation simple */
    12521252        if (ext4_inode_get_size(fs->superblock, inode_ref->inode) == 0) {
     
    12541254                return EOK;
    12551255        }
    1256        
     1256
    12571257        uint32_t current_block;
    1258        
     1258
    12591259        /* Handle i-node using extents */
    12601260        if ((ext4_superblock_has_feature_incompatible(fs->superblock,
     
    12641264                if (rc != EOK)
    12651265                        return rc;
    1266                
     1266
    12671267                *fblock = current_block;
    12681268                return EOK;
    12691269        }
    1270        
     1270
    12711271        ext4_inode_t *inode = inode_ref->inode;
    1272        
     1272
    12731273        /* Direct block are read directly from array in i-node structure */
    12741274        if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
     
    12771277                return EOK;
    12781278        }
    1279        
     1279
    12801280        /* Determine indirection level of the target block */
    12811281        unsigned int level = 0;
     
    12861286                }
    12871287        }
    1288        
     1288
    12891289        if (level == 0)
    12901290                return EIO;
    1291        
     1291
    12921292        /* Compute offsets for the topmost level */
    12931293        aoff64_t block_offset_in_level =
     
    12961296        uint32_t offset_in_block =
    12971297            block_offset_in_level / fs->inode_blocks_per_level[level - 1];
    1298        
     1298
    12991299        /* Sparse file */
    13001300        if (current_block == 0) {
     
    13021302                return EOK;
    13031303        }
    1304        
     1304
    13051305        block_t *block;
    1306        
     1306
    13071307        /*
    13081308         * Navigate through other levels, until we find the block number
     
    13141314                if (rc != EOK)
    13151315                        return rc;
    1316                
     1316
    13171317                /* Read block address from indirect block */
    13181318                current_block =
    13191319                    uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
    1320                
     1320
    13211321                /* Put back indirect block untouched */
    13221322                rc = block_put(block);
    13231323                if (rc != EOK)
    13241324                        return rc;
    1325                
     1325
    13261326                /* Check for sparse file */
    13271327                if (current_block == 0) {
     
    13291329                        return EOK;
    13301330                }
    1331                
     1331
    13321332                /* Jump to the next level */
    13331333                level--;
    1334                
     1334
    13351335                /* Termination condition - we have address of data block loaded */
    13361336                if (level == 0)
    13371337                        break;
    1338                
     1338
    13391339                /* Visit the next level */
    13401340                block_offset_in_level %= fs->inode_blocks_per_level[level];
     
    13421342                    block_offset_in_level / fs->inode_blocks_per_level[level - 1];
    13431343        }
    1344        
     1344
    13451345        *fblock = current_block;
    1346        
     1346
    13471347        return EOK;
    13481348}
     
    13611361{
    13621362        ext4_filesystem_t *fs = inode_ref->fs;
    1363        
     1363
    13641364        /* Handle inode using extents */
    13651365        if ((ext4_superblock_has_feature_compatible(fs->superblock,
     
    13691369                return ENOTSUP;
    13701370        }
    1371        
     1371
    13721372        /* Handle simple case when we are dealing with direct reference */
    13731373        if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
    13741374                ext4_inode_set_direct_block(inode_ref->inode, (uint32_t) iblock, fblock);
    13751375                inode_ref->dirty = true;
    1376                
     1376
    13771377                return EOK;
    13781378        }
    1379        
     1379
    13801380        /* Determine the indirection level needed to get the desired block */
    13811381        unsigned int level = 0;
     
    13861386                }
    13871387        }
    1388        
     1388
    13891389        if (level == 0)
    13901390                return EIO;
    1391        
     1391
    13921392        uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
    1393        
     1393
    13941394        /* Compute offsets for the topmost level */
    13951395        aoff64_t block_offset_in_level =
     
    13991399        uint32_t offset_in_block =
    14001400            block_offset_in_level / fs->inode_blocks_per_level[level - 1];
    1401        
     1401
    14021402        uint32_t new_block_addr;
    14031403        block_t *block;
    14041404        block_t *new_block;
    1405        
     1405
    14061406        /* Is needed to allocate indirect block on the i-node level */
    14071407        if (current_block == 0) {
     
    14101410                if (rc != EOK)
    14111411                        return rc;
    1412                
     1412
    14131413                /* Update i-node */
    14141414                ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
    14151415                    new_block_addr);
    14161416                inode_ref->dirty = true;
    1417                
     1417
    14181418                /* Load newly allocated block */
    14191419                rc = block_get(&new_block, fs->device, new_block_addr,
     
    14231423                        return rc;
    14241424                }
    1425                
     1425
    14261426                /* Initialize new block */
    14271427                memset(new_block->data, 0, block_size);
    14281428                new_block->dirty = true;
    1429                
     1429
    14301430                /* Put back the allocated block */
    14311431                rc = block_put(new_block);
    14321432                if (rc != EOK)
    14331433                        return rc;
    1434                
     1434
    14351435                current_block = new_block_addr;
    14361436        }
    1437        
     1437
    14381438        /*
    14391439         * Navigate through other levels, until we find the block number
     
    14441444                if (rc != EOK)
    14451445                        return rc;
    1446                
     1446
    14471447                current_block =
    14481448                    uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
    1449                
     1449
    14501450                if ((level > 1) && (current_block == 0)) {
    14511451                        /* Allocate new block */
     
    14551455                                return rc;
    14561456                        }
    1457                        
     1457
    14581458                        /* Load newly allocated block */
    14591459                        rc = block_get(&new_block, fs->device, new_block_addr,
     
    14631463                                return rc;
    14641464                        }
    1465                        
     1465
    14661466                        /* Initialize allocated block */
    14671467                        memset(new_block->data, 0, block_size);
    14681468                        new_block->dirty = true;
    1469                        
     1469
    14701470                        rc = block_put(new_block);
    14711471                        if (rc != EOK) {
     
    14731473                                return rc;
    14741474                        }
    1475                        
     1475
    14761476                        /* Write block address to the parent */
    14771477                        ((uint32_t *) block->data)[offset_in_block] =
     
    14801480                        current_block = new_block_addr;
    14811481                }
    1482                
     1482
    14831483                /* Will be finished, write the fblock address */
    14841484                if (level == 1) {
     
    14871487                        block->dirty = true;
    14881488                }
    1489                
     1489
    14901490                rc = block_put(block);
    14911491                if (rc != EOK)
    14921492                        return rc;
    1493                
     1493
    14941494                level--;
    1495                
     1495
    14961496                /*
    14971497                 * If we are on the last level, break here as
     
    15001500                if (level == 0)
    15011501                        break;
    1502                
     1502
    15031503                /* Visit the next level */
    15041504                block_offset_in_level %= fs->inode_blocks_per_level[level];
     
    15061506                    block_offset_in_level / fs->inode_blocks_per_level[level - 1];
    15071507        }
    1508        
     1508
    15091509        return EOK;
    15101510}
     
    15221522{
    15231523        uint32_t fblock;
    1524        
     1524
    15251525        ext4_filesystem_t *fs = inode_ref->fs;
    1526        
     1526
    15271527        /* Extents are handled otherwise = there is not support in this function */
    15281528        assert(!(ext4_superblock_has_feature_incompatible(fs->superblock,
    15291529            EXT4_FEATURE_INCOMPAT_EXTENTS) &&
    15301530            (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
    1531        
     1531
    15321532        ext4_inode_t *inode = inode_ref->inode;
    1533        
     1533
    15341534        /* Handle simple case when we are dealing with direct reference */
    15351535        if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
    15361536                fblock = ext4_inode_get_direct_block(inode, iblock);
    1537                
     1537
    15381538                /* Sparse file */
    15391539                if (fblock == 0)
    15401540                        return EOK;
    1541                
     1541
    15421542                ext4_inode_set_direct_block(inode, iblock, 0);
    15431543                return ext4_balloc_free_block(inode_ref, fblock);
    15441544        }
    1545        
     1545
    15461546        /* Determine the indirection level needed to get the desired block */
    15471547        unsigned int level = 0;
     
    15521552                }
    15531553        }
    1554        
     1554
    15551555        if (level == 0)
    15561556                return EIO;
    1557        
     1557
    15581558        /* Compute offsets for the topmost level */
    15591559        aoff64_t block_offset_in_level =
     
    15631563        uint32_t offset_in_block =
    15641564            block_offset_in_level / fs->inode_blocks_per_level[level - 1];
    1565        
     1565
    15661566        /*
    15671567         * Navigate through other levels, until we find the block number
     
    15701570        block_t *block;
    15711571        while (level > 0) {
    1572                
     1572
    15731573                /* Sparse check */
    15741574                if (current_block == 0)
    15751575                        return EOK;
    1576                
     1576
    15771577                errno_t rc = block_get(&block, fs->device, current_block, 0);
    15781578                if (rc != EOK)
    15791579                        return rc;
    1580                
     1580
    15811581                current_block =
    15821582                    uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
    1583                
     1583
    15841584                /* Set zero if physical data block address found */
    15851585                if (level == 1) {
     
    15881588                        block->dirty = true;
    15891589                }
    1590                
     1590
    15911591                rc = block_put(block);
    15921592                if (rc != EOK)
    15931593                        return rc;
    1594                
     1594
    15951595                level--;
    1596                
     1596
    15971597                /*
    15981598                 * If we are on the last level, break here as
     
    16011601                if (level == 0)
    16021602                        break;
    1603                
     1603
    16041604                /* Visit the next level */
    16051605                block_offset_in_level %= fs->inode_blocks_per_level[level];
     
    16071607                    block_offset_in_level / fs->inode_blocks_per_level[level - 1];
    16081608        }
    1609        
     1609
    16101610        fblock = current_block;
    16111611        if (fblock == 0)
    16121612                return EOK;
    1613        
     1613
    16141614        /* Physical block is not referenced, it can be released */
    16151615        return ext4_balloc_free_block(inode_ref, fblock);
     
    16331633            (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)))
    16341634                return ext4_extent_append_block(inode_ref, iblock, fblock, true);
    1635        
     1635
    16361636        ext4_superblock_t *sb = inode_ref->fs->superblock;
    1637        
     1637
    16381638        /* Compute next block index and allocate data block */
    16391639        uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
    16401640        uint32_t block_size = ext4_superblock_get_block_size(sb);
    1641        
     1641
    16421642        /* Align size i-node size */
    16431643        if ((inode_size % block_size) != 0)
    16441644                inode_size += block_size - (inode_size % block_size);
    1645        
     1645
    16461646        /* Logical blocks are numbered from 0 */
    16471647        uint32_t new_block_idx = inode_size / block_size;
    1648        
     1648
    16491649        /* Allocate new physical block */
    16501650        uint32_t phys_block;
     
    16521652        if (rc != EOK)
    16531653                return rc;
    1654        
     1654
    16551655        /* Add physical block address to the i-node */
    16561656        rc = ext4_filesystem_set_inode_data_block_index(inode_ref,
     
    16601660                return rc;
    16611661        }
    1662        
     1662
    16631663        /* Update i-node */
    16641664        ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
    16651665        inode_ref->dirty = true;
    1666        
     1666
    16671667        *fblock = phys_block;
    16681668        *iblock = new_block_idx;
    1669        
     1669
    16701670        return EOK;
    16711671}
Note: See TracChangeset for help on using the changeset viewer.