Changeset a35b458 in mainline for uspace/lib/ext4


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.

Location:
uspace/lib/ext4
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/ext4/include/ext4/types.h

    r3061bc1 ra35b458  
    6666        uint16_t def_resuid;                /* Default uid for reserved blocks */
    6767        uint16_t def_resgid;                /* Default gid for reserved blocks */
    68        
     68
    6969        /* Fields for EXT4_DYNAMIC_REV superblocks only. */
    7070        uint32_t first_inode;             /* First non-reserved inode */
     
    7878        char last_mounted[64];            /* Directory where last mounted */
    7979        uint32_t algorithm_usage_bitmap;  /* For compression */
    80        
     80
    8181        /*
    8282         * Performance hints. Directory preallocation should only
     
    8686        uint8_t prealloc_dir_blocks;    /* Number to preallocate for dirs */
    8787        uint16_t reserved_gdt_blocks;   /* Per group desc for online growth */
    88        
     88
    8989        /*
    9090         * Journaling support valid if EXT4_FEATURE_COMPAT_HAS_JOURNAL set.
     
    251251        uint16_t itable_unused_lo;            /* Unused inodes count */
    252252        uint16_t checksum;                    /* crc16(sb_uuid+group+desc) */
    253        
     253
    254254        uint32_t block_bitmap_hi;             /* Blocks bitmap block MSB */
    255255        uint32_t inode_bitmap_hi;             /* I-nodes bitmap block MSB */
     
    307307        uint32_t size_hi;
    308308        uint32_t obso_faddr;                 /* Obsoleted fragment address */
    309        
     309
    310310        union {
    311311                struct {
     
    324324                } hurd2;
    325325        } __attribute__ ((packed)) osd2;
    326        
     326
    327327        uint16_t extra_isize;
    328328        uint16_t pad1;
     
    403403        uint16_t entry_length;  /* Distance to the next directory entry */
    404404        uint8_t name_length;    /* Lower 8 bits of name length */
    405        
     405
    406406        union {
    407407                uint8_t name_length_high;  /* Higher 8 bits of name length */
    408408                uint8_t inode_type;        /* Type of referenced inode (in rev >= 0.5) */
    409409        } __attribute__ ((packed));
    410        
     410
    411411        uint8_t name[EXT4_DIRECTORY_FILENAME_LEN];  /* Entry name */
    412412} __attribute__((packed)) ext4_directory_entry_ll_t;
     
    495495typedef struct ext4_extent_index {
    496496        uint32_t first_block;  /* Index covers logical blocks from 'block' */
    497        
     497
    498498        /**
    499499         * Pointer to the physical block of the next
  • uspace/lib/ext4/src/balloc.c

    r3061bc1 ra35b458  
    5757        ext4_filesystem_t *fs = inode_ref->fs;
    5858        ext4_superblock_t *sb = fs->superblock;
    59        
     59
    6060        /* Compute indexes */
    6161        uint32_t block_group = ext4_filesystem_blockaddr2group(sb, block_addr);
    6262        uint32_t index_in_group =
    6363            ext4_filesystem_blockaddr2_index_in_group(sb, block_addr);
    64        
     64
    6565        /* Load block group reference */
    6666        ext4_block_group_ref_t *bg_ref;
     
    6868        if (rc != EOK)
    6969                return rc;
    70        
     70
    7171        /* Load block with bitmap */
    7272        uint32_t bitmap_block_addr =
     
    7878                return rc;
    7979        }
    80        
     80
    8181        /* Modify bitmap */
    8282        ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
    8383        bitmap_block->dirty = true;
    84        
     84
    8585        /* Release block with bitmap */
    8686        rc = block_put(bitmap_block);
     
    9090                return rc;
    9191        }
    92        
     92
    9393        uint32_t block_size = ext4_superblock_get_block_size(sb);
    94        
     94
    9595        /* Update superblock free blocks count */
    9696        uint32_t sb_free_blocks =
     
    9898        sb_free_blocks++;
    9999        ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
    100        
     100
    101101        /* Update inode blocks count */
    102102        uint64_t ino_blocks =
     
    105105        ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
    106106        inode_ref->dirty = true;
    107        
     107
    108108        /* Update block group free blocks count */
    109109        uint32_t free_blocks =
     
    113113            sb, free_blocks);
    114114        bg_ref->dirty = true;
    115        
     115
    116116        /* Release block group reference */
    117117        return ext4_filesystem_put_block_group_ref(bg_ref);
     
    346346{
    347347        uint32_t allocated_block = 0;
    348        
     348
    349349        uint32_t bitmap_block_addr;
    350350        block_t *bitmap_block;
     
    352352        uint32_t free_blocks;
    353353        uint32_t goal;
    354        
     354
    355355        /* Find GOAL */
    356356        errno_t rc = ext4_balloc_find_goal(inode_ref, &goal);
     
    359359
    360360        ext4_superblock_t *sb = inode_ref->fs->superblock;
    361        
     361
    362362        /* Load block group number for goal and relative index */
    363363        uint32_t block_group = ext4_filesystem_blockaddr2group(sb, goal);
    364364        uint32_t index_in_group =
    365365            ext4_filesystem_blockaddr2_index_in_group(sb, goal);
    366        
     366
    367367        /* Load block group reference */
    368368        ext4_block_group_ref_t *bg_ref;
     
    378378                goto goal_failed;
    379379        }
    380        
     380
    381381        /* Compute indexes */
    382382        uint32_t first_in_group =
    383383            ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
    384        
     384
    385385        uint32_t first_in_group_index =
    386386            ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
    387        
     387
    388388        if (index_in_group < first_in_group_index)
    389389                index_in_group = first_in_group_index;
    390        
     390
    391391        /* Load block with bitmap */
    392392        bitmap_block_addr =
    393393            ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
    394        
     394
    395395        rc = block_get(&bitmap_block, inode_ref->fs->device,
    396396            bitmap_block_addr, BLOCK_FLAGS_NONE);
     
    399399                return rc;
    400400        }
    401        
     401
    402402        /* Check if goal is free */
    403403        if (ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group)) {
     
    409409                        return rc;
    410410                }
    411                
     411
    412412                allocated_block =
    413413                    ext4_filesystem_index_in_group2blockaddr(sb, index_in_group,
    414414                    block_group);
    415                
     415
    416416                goto success;
    417417        }
    418        
     418
    419419        uint32_t blocks_in_group =
    420420            ext4_superblock_get_blocks_in_group(sb, block_group);
    421        
     421
    422422        uint32_t end_idx = (index_in_group + 63) & ~63;
    423423        if (end_idx > blocks_in_group)
    424424                end_idx = blocks_in_group;
    425        
     425
    426426        /* Try to find free block near to goal */
    427427        for (uint32_t tmp_idx = index_in_group + 1; tmp_idx < end_idx;
     
    433433                        if (rc != EOK)
    434434                                return rc;
    435                        
     435
    436436                        allocated_block =
    437437                            ext4_filesystem_index_in_group2blockaddr(sb, tmp_idx,
    438438                            block_group);
    439                        
     439
    440440                        goto success;
    441441                }
    442442        }
    443        
     443
    444444        /* Find free BYTE in bitmap */
    445445        rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data,
     
    450450                if (rc != EOK)
    451451                        return rc;
    452                
     452
    453453                allocated_block =
    454454                    ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
    455455                    block_group);
    456                
     456
    457457                goto success;
    458458        }
    459        
     459
    460460        /* Find free bit in bitmap */
    461461        rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
     
    466466                if (rc != EOK)
    467467                        return rc;
    468                
     468
    469469                allocated_block =
    470470                    ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
    471471                    block_group);
    472                
     472
    473473                goto success;
    474474        }
    475        
     475
    476476        /* No free block found yet */
    477477        rc = block_put(bitmap_block);
     
    486486        if (rc != EOK)
    487487                return rc;
    488        
     488
    489489        /* Try other block groups */
    490490        uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
    491        
     491
    492492        uint32_t bgid = (block_group + 1) % block_group_count;
    493493        uint32_t count = block_group_count;
    494        
     494
    495495        while (count > 0) {
    496496                rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, bgid,
     
    509509                bitmap_block_addr =
    510510                    ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
    511                
     511
    512512                rc = block_get(&bitmap_block, inode_ref->fs->device,
    513513                    bitmap_block_addr, 0);
     
    516516                        return rc;
    517517                }
    518                
     518
    519519                /* Compute indexes */
    520520                first_in_group =
     
    523523                    ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
    524524                blocks_in_group = ext4_superblock_get_blocks_in_group(sb, bgid);
    525                
     525
    526526                first_in_group_index =
    527527                    ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
    528                
     528
    529529                if (index_in_group < first_in_group_index)
    530530                        index_in_group = first_in_group_index;
    531                
     531
    532532                /* Try to find free byte in bitmap */
    533533                rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data,
     
    540540                                return rc;
    541541                        }
    542                        
     542
    543543                        allocated_block =
    544544                            ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
    545545                            bgid);
    546                        
     546
    547547                        goto success;
    548548                }
    549                
     549
    550550                /* Try to find free bit in bitmap */
    551551                rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
     
    558558                                return rc;
    559559                        }
    560                        
     560
    561561                        allocated_block =
    562562                            ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
    563563                            bgid);
    564                        
     564
    565565                        goto success;
    566566                }
    567                
     567
    568568                rc = block_put(bitmap_block);
    569569                if (rc != EOK) {
     
    576576                if (rc != EOK)
    577577                        return rc;
    578                
     578
    579579                /* Goto next group */
    580580                bgid = (bgid + 1) % block_group_count;
    581581                count--;
    582582        }
    583        
     583
    584584        return ENOSPC;
    585        
     585
    586586success:
    587587        /* Empty command - because of syntax */
    588588        ;
    589        
     589
    590590        uint32_t block_size = ext4_superblock_get_block_size(sb);
    591        
     591
    592592        /* Update superblock free blocks count */
    593593        uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
    594594        sb_free_blocks--;
    595595        ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
    596        
     596
    597597        /* Update inode blocks (different block size!) count */
    598598        uint64_t ino_blocks =
     
    601601        ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
    602602        inode_ref->dirty = true;
    603        
     603
    604604        /* Update block group free blocks count */
    605605        uint32_t bg_free_blocks =
     
    609609            bg_free_blocks);
    610610        bg_ref->dirty = true;
    611        
     611
    612612        rc = ext4_filesystem_put_block_group_ref(bg_ref);
    613        
     613
    614614        *fblock = allocated_block;
    615615        return rc;
     
    629629{
    630630        errno_t rc;
    631        
     631
    632632        ext4_filesystem_t *fs = inode_ref->fs;
    633633        ext4_superblock_t *sb = fs->superblock;
    634        
     634
    635635        /* Compute indexes */
    636636        uint32_t block_group = ext4_filesystem_blockaddr2group(sb, fblock);
    637637        uint32_t index_in_group =
    638638            ext4_filesystem_blockaddr2_index_in_group(sb, fblock);
    639        
     639
    640640        /* Load block group reference */
    641641        ext4_block_group_ref_t *bg_ref;
     
    643643        if (rc != EOK)
    644644                return rc;
    645        
     645
    646646        /* Load block with bitmap */
    647647        uint32_t bitmap_block_addr =
     
    653653                return rc;
    654654        }
    655        
     655
    656656        /* Check if block is free */
    657657        *free = ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group);
    658        
     658
    659659        /* Allocate block if possible */
    660660        if (*free) {
     
    662662                bitmap_block->dirty = true;
    663663        }
    664        
     664
    665665        /* Release block with bitmap */
    666666        rc = block_put(bitmap_block);
     
    670670                return rc;
    671671        }
    672        
     672
    673673        /* If block is not free, return */
    674674        if (!(*free))
    675675                goto terminate;
    676        
     676
    677677        uint32_t block_size = ext4_superblock_get_block_size(sb);
    678        
     678
    679679        /* Update superblock free blocks count */
    680680        uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
    681681        sb_free_blocks--;
    682682        ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
    683        
     683
    684684        /* Update inode blocks count */
    685685        uint64_t ino_blocks =
     
    688688        ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
    689689        inode_ref->dirty = true;
    690        
     690
    691691        /* Update block group free blocks count */
    692692        uint32_t free_blocks =
     
    696696            sb, free_blocks);
    697697        bg_ref->dirty = true;
    698        
     698
    699699terminate:
    700700        return ext4_filesystem_put_block_group_ref(bg_ref);
  • uspace/lib/ext4/src/bitmap.c

    r3061bc1 ra35b458  
    5252        uint32_t byte_index = index / 8;
    5353        uint32_t bit_index = index % 8;
    54        
     54
    5555        uint8_t *target = bitmap + byte_index;
    56        
     56
    5757        *target &= ~ (1 << bit_index);
    5858}
     
    7373        uint32_t remaining = count;
    7474        uint32_t byte_index;
    75        
     75
    7676        /* Align index to multiple of 8 */
    7777        while (((idx % 8) != 0) && (remaining > 0)) {
    7878                byte_index = idx / 8;
    7979                uint32_t bit_index = idx % 8;
    80                
     80
    8181                target = bitmap + byte_index;
    8282                *target &= ~ (1 << bit_index);
    83                
     83
    8484                idx++;
    8585                remaining--;
    8686        }
    87        
     87
    8888        /* For < 8 bits this check necessary */
    8989        if (remaining == 0)
    9090                return;
    91        
     91
    9292        assert((idx % 8) == 0);
    93        
     93
    9494        byte_index = idx / 8;
    9595        target = bitmap + byte_index;
    96        
     96
    9797        /* Zero the whole bytes */
    9898        while (remaining >= 8) {
    9999                *target = 0;
    100                
     100
    101101                idx += 8;
    102102                remaining -= 8;
    103103                target++;
    104104        }
    105        
     105
    106106        assert(remaining < 8);
    107        
     107
    108108        /* Zero remaining bytes */
    109109        while (remaining != 0) {
    110110                byte_index = idx / 8;
    111111                uint32_t bit_index = idx % 8;
    112                
     112
    113113                target = bitmap + byte_index;
    114114                *target &= ~ (1 << bit_index);
    115                
     115
    116116                idx++;
    117117                remaining--;
     
    129129        uint32_t byte_index = index / 8;
    130130        uint32_t bit_index = index % 8;
    131        
     131
    132132        uint8_t *target = bitmap + byte_index;
    133        
     133
    134134        *target |= 1 << bit_index;
    135135}
     
    147147        uint32_t byte_index = index / 8;
    148148        uint32_t bit_index = index % 8;
    149        
     149
    150150        uint8_t *target = bitmap + byte_index;
    151        
     151
    152152        if (*target & (1 << bit_index))
    153153                return false;
     
    173173{
    174174        uint32_t idx;
    175        
     175
    176176        /* Align idx */
    177177        if (start % 8)
     
    179179        else
    180180                idx = start;
    181        
     181
    182182        uint8_t *pos = bitmap + (idx / 8);
    183        
     183
    184184        /* Try to find free byte */
    185185        while (idx < max) {
    186186                if (*pos == 0) {
    187187                        *pos |= 1;
    188                        
     188
    189189                        *index = idx;
    190190                        return EOK;
    191191                }
    192                
     192
    193193                idx += 8;
    194194                ++pos;
    195195        }
    196        
     196
    197197        /* Free byte not found */
    198198        return ENOSPC;
     
    217217        uint32_t idx = start_idx;
    218218        bool byte_part = false;
    219        
     219
    220220        /* Check the rest of first byte */
    221221        while ((idx % 8) != 0) {
    222222                byte_part = true;
    223                
     223
    224224                if ((*pos & (1 << (idx % 8))) == 0) {
    225225                        *pos |= (1 << (idx % 8));
     
    227227                        return EOK;
    228228                }
    229                
     229
    230230                ++idx;
    231231        }
    232        
     232
    233233        if (byte_part)
    234234                ++pos;
    235        
     235
    236236        /* Check the whole bytes (255 = 11111111 binary) */
    237237        while (idx < max) {
     
    240240                        break;
    241241                }
    242                
     242
    243243                idx += 8;
    244244                ++pos;
    245245        }
    246        
     246
    247247        /* If idx < max, some free bit found */
    248248        if (idx < max) {
     
    252252                                /* Free bit found */
    253253                                *pos |= (1 << i);
    254                                
     254
    255255                                *index = idx;
    256256                                return EOK;
    257257                        }
    258                        
     258
    259259                        idx++;
    260260                }
    261261        }
    262        
     262
    263263        /* Free bit not found */
    264264        return ENOSPC;
  • uspace/lib/ext4/src/block_group.c

    r3061bc1 ra35b458  
    7070{
    7171        bg->block_bitmap_lo = host2uint32_t_le((block_bitmap << 32) >> 32);
    72        
     72
    7373        if (ext4_superblock_get_desc_size(sb) >
    7474            EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
     
    106106{
    107107        bg->inode_bitmap_lo = host2uint32_t_le((inode_bitmap << 32) >> 32);
    108        
     108
    109109        if (ext4_superblock_get_desc_size(sb) >
    110110            EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
     
    144144        bg->inode_table_first_block_lo =
    145145            host2uint32_t_le((inode_table_first << 32) >> 32);
    146        
     146
    147147        if (ext4_superblock_get_desc_size(sb) >
    148148            EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
     
    353353        if (ext4_block_group_get_flags(bg) & flag)
    354354                return true;
    355        
     355
    356356        return false;
    357357}
  • uspace/lib/ext4/src/directory.c

    r3061bc1 ra35b458  
    110110                return ((uint16_t)de->name_length_high) << 8 |
    111111                    ((uint16_t)de->name_length);
    112        
     112
    113113        return de->name_length;
    114114
     
    126126{
    127127        de->name_length = (length << 8) >> 8;
    128        
     128
    129129        if ((ext4_superblock_get_rev_level(sb) == 0) &&
    130130            (ext4_superblock_get_minor_rev_level(sb) < 5))
    131131                de->name_length_high = length >> 8;
    132        
     132
    133133        /* Else do nothing */
    134134}
     
    148148            (ext4_superblock_get_minor_rev_level(sb) >= 5))
    149149                return de->inode_type;
    150        
     150
    151151        return EXT4_DIRECTORY_FILETYPE_UNKNOWN;
    152152}
     
    165165            (ext4_superblock_get_minor_rev_level(sb) >= 5))
    166166                de->inode_type = type;
    167        
     167
    168168        /* Else do nothing */
    169169}
     
    190190        it->current_offset = 0;
    191191        it->current_block = NULL;
    192        
     192
    193193        return ext4_directory_iterator_seek(it, pos);
    194194}
     
    204204{
    205205        assert(it->current != NULL);
    206        
     206
    207207        uint16_t skip = ext4_directory_entry_ll_get_entry_length(it->current);
    208        
     208
    209209        return ext4_directory_iterator_seek(it, it->current_offset + skip);
    210210}
     
    224224        uint64_t size = ext4_inode_get_size(it->inode_ref->fs->superblock,
    225225            it->inode_ref->inode);
    226        
     226
    227227        /* The iterator is not valid until we seek to the desired position */
    228228        it->current = NULL;
    229        
     229
    230230        /* Are we at the end? */
    231231        if (pos >= size) {
     
    233233                        errno_t rc = block_put(it->current_block);
    234234                        it->current_block = NULL;
    235                        
     235
    236236                        if (rc != EOK)
    237237                                return rc;
    238238                }
    239                
     239
    240240                it->current_offset = pos;
    241241                return EOK;
    242242        }
    243        
     243
    244244        /* Compute next block address */
    245245        uint32_t block_size =
     
    247247        aoff64_t current_block_idx = it->current_offset / block_size;
    248248        aoff64_t next_block_idx = pos / block_size;
    249        
     249
    250250        /*
    251251         * If we don't have a block or are moving accross block boundary,
     
    257257                        errno_t rc = block_put(it->current_block);
    258258                        it->current_block = NULL;
    259                        
     259
    260260                        if (rc != EOK)
    261261                                return rc;
    262262                }
    263                
     263
    264264                uint32_t next_block_phys_idx;
    265265                errno_t rc = ext4_filesystem_get_inode_data_block_index(it->inode_ref,
     
    267267                if (rc != EOK)
    268268                        return rc;
    269                
     269
    270270                rc = block_get(&it->current_block, it->inode_ref->fs->device,
    271271                    next_block_phys_idx, BLOCK_FLAGS_NONE);
     
    275275                }
    276276        }
    277        
     277
    278278        it->current_offset = pos;
    279        
     279
    280280        return ext4_directory_iterator_set(it, block_size);
    281281}
     
    293293{
    294294        it->current = NULL;
    295        
     295
    296296        uint32_t offset_in_block = it->current_offset % block_size;
    297        
     297
    298298        /* Ensure proper alignment */
    299299        if ((offset_in_block % 4) != 0)
    300300                return EIO;
    301        
     301
    302302        /* Ensure that the core of the entry does not overflow the block */
    303303        if (offset_in_block > block_size - 8)
    304304                return EIO;
    305        
     305
    306306        ext4_directory_entry_ll_t *entry =
    307307            it->current_block->data + offset_in_block;
    308        
     308
    309309        /* Ensure that the whole entry does not overflow the block */
    310310        uint16_t length = ext4_directory_entry_ll_get_entry_length(entry);
    311311        if (offset_in_block + length > block_size)
    312312                return EIO;
    313        
     313
    314314        /* Ensure the name length is not too large */
    315315        if (ext4_directory_entry_ll_get_name_length(
    316316            it->inode_ref->fs->superblock, entry) > length-8)
    317317                return EIO;
    318        
     318
    319319        /* Everything OK - "publish" the entry */
    320320        it->current = entry;
     
    335335        it->inode_ref = NULL;
    336336        it->current = NULL;
    337        
     337
    338338        if (it->current_block)
    339339                return block_put(it->current_block);
    340        
     340
    341341        return EOK;
    342342}
     
    359359        uint32_t block_size = ext4_superblock_get_block_size(sb);
    360360        assert(entry_len <= block_size);
    361        
     361
    362362        /* Set basic attributes */
    363363        ext4_directory_entry_ll_set_inode(entry, child->index);
    364364        ext4_directory_entry_ll_set_entry_length(entry, entry_len);
    365365        ext4_directory_entry_ll_set_name_length(sb, entry, name_len);
    366        
     366
    367367        /* Write name */
    368368        memcpy(entry->name, name, name_len);
    369        
     369
    370370        /* Set type of entry */
    371371        if (ext4_inode_is_type(sb, child->inode, EXT4_INODE_MODE_DIRECTORY))
     
    390390{
    391391        ext4_filesystem_t *fs = parent->fs;
    392        
     392
    393393        /* Index adding (if allowed) */
    394394        if ((ext4_superblock_has_feature_compatible(fs->superblock,
     
    405405                parent->dirty = true;
    406406        }
    407        
     407
    408408        /* Linear algorithm */
    409        
     409
    410410        uint32_t iblock = 0;
    411411        uint32_t fblock = 0;
     
    413413        uint32_t inode_size = ext4_inode_get_size(fs->superblock, parent->inode);
    414414        uint32_t total_blocks = inode_size / block_size;
    415        
     415
    416416        uint32_t name_len = str_size(name);
    417        
     417
    418418        /* Find block, where is space for new entry and try to add */
    419419        bool success = false;
     
    423423                if (rc != EOK)
    424424                        return rc;
    425                
     425
    426426                block_t *block;
    427427                rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
    428428                if (rc != EOK)
    429429                        return rc;
    430                
     430
    431431                /* If adding is successful, function can finish */
    432432                rc = ext4_directory_try_insert_entry(fs->superblock, block,
     
    434434                if (rc == EOK)
    435435                        success = true;
    436                
     436
    437437                rc = block_put(block);
    438438                if (rc != EOK)
    439439                        return rc;
    440                
     440
    441441                if (success)
    442442                        return EOK;
    443443        }
    444        
     444
    445445        /* No free block found - needed to allocate next data block */
    446        
     446
    447447        iblock = 0;
    448448        fblock = 0;
     
    450450        if (rc != EOK)
    451451                return rc;
    452        
     452
    453453        /* Load new block */
    454454        block_t *new_block;
     
    456456        if (rc != EOK)
    457457                return rc;
    458        
     458
    459459        /* Fill block with zeroes */
    460460        memset(new_block->data, 0, block_size);
     
    462462        ext4_directory_write_entry(fs->superblock, block_entry, block_size,
    463463            child, name, name_len);
    464        
     464
    465465        /* Save new block */
    466466        new_block->dirty = true;
    467467        rc = block_put(new_block);
    468        
     468
    469469        return rc;
    470470}
     
    483483{
    484484        uint32_t name_len = str_size(name);
    485        
     485
    486486        ext4_superblock_t *sb = parent->fs->superblock;
    487        
     487
    488488        /* Index search */
    489489        if ((ext4_superblock_has_feature_compatible(sb,
     
    492492                errno_t rc = ext4_directory_dx_find_entry(result, parent, name_len,
    493493                    name);
    494                
     494
    495495                /* Check if index is not corrupted */
    496496                if (rc != EXT4_ERR_BAD_DX_DIR) {
    497497                        if (rc != EOK)
    498498                                return rc;
    499                        
     499
    500500                        return EOK;
    501501                }
    502                
     502
    503503                /* Needed to clear dir index flag if corrupted */
    504504                ext4_inode_clear_flag(parent->inode, EXT4_INODE_FLAG_INDEX);
    505505                parent->dirty = true;
    506506        }
    507        
     507
    508508        /* Linear algorithm */
    509        
     509
    510510        uint32_t iblock;
    511511        uint32_t fblock;
     
    513513        uint32_t inode_size = ext4_inode_get_size(sb, parent->inode);
    514514        uint32_t total_blocks = inode_size / block_size;
    515        
     515
    516516        /* Walk through all data blocks */
    517517        for (iblock = 0; iblock < total_blocks; ++iblock) {
     
    521521                if (rc != EOK)
    522522                        return rc;
    523                
     523
    524524                /* Load data block */
    525525                block_t *block;
     
    527527                if (rc != EOK)
    528528                        return rc;
    529                
     529
    530530                /* Try to find entry in block */
    531531                ext4_directory_entry_ll_t *res_entry;
     
    537537                        return EOK;
    538538                }
    539                
     539
    540540                /* Entry not found - put block and continue to the next block */
    541                
     541
    542542                rc = block_put(block);
    543543                if (rc != EOK)
    544544                        return rc;
    545545        }
    546        
     546
    547547        /* Entry was not found */
    548        
     548
    549549        result->block = NULL;
    550550        result->dentry =  NULL;
    551        
     551
    552552        return ENOENT;
    553553}
     
    567567            EXT4_INODE_MODE_DIRECTORY))
    568568                return ENOTDIR;
    569        
     569
    570570        /* Try to find entry */
    571571        ext4_directory_search_result_t result;
     
    573573        if (rc != EOK)
    574574                return rc;
    575        
     575
    576576        /* Invalidate entry */
    577577        ext4_directory_entry_ll_set_inode(result.dentry, 0);
    578        
     578
    579579        /* Store entry position in block */
    580580        uint32_t pos = (void *) result.dentry - result.block->data;
    581        
     581
    582582        /*
    583583         * If entry is not the first in block, it must be merged
     
    586586        if (pos != 0) {
    587587                uint32_t offset = 0;
    588                
     588
    589589                /* Start from the first entry in block */
    590590                ext4_directory_entry_ll_t *tmp_dentry = result.block->data;
    591591                uint16_t tmp_dentry_length =
    592592                    ext4_directory_entry_ll_get_entry_length(tmp_dentry);
    593                
     593
    594594                /* Find direct predecessor of removed entry */
    595595                while ((offset + tmp_dentry_length) < pos) {
     
    600600                            ext4_directory_entry_ll_get_entry_length(tmp_dentry);
    601601                }
    602                
     602
    603603                assert(tmp_dentry_length + offset == pos);
    604                
     604
    605605                /* Add to removed entry length to predecessor's length */
    606606                uint16_t del_entry_length =
     
    609609                    tmp_dentry_length + del_entry_length);
    610610        }
    611        
     611
    612612        result.block->dirty = true;
    613        
     613
    614614        return ext4_directory_destroy_result(&result);
    615615}
     
    633633        uint32_t block_size = ext4_superblock_get_block_size(sb);
    634634        uint16_t required_len = sizeof(ext4_fake_directory_entry_t) + name_len;
    635        
     635
    636636        if ((required_len % 4) != 0)
    637637                required_len += 4 - (required_len % 4);
    638        
     638
    639639        /* Initialize pointers, stop means to upper bound */
    640640        ext4_directory_entry_ll_t *dentry = target_block->data;
    641641        ext4_directory_entry_ll_t *stop = target_block->data + block_size;
    642        
     642
    643643        /*
    644644         * Walk through the block and check for invalid entries
     
    648648                uint32_t inode = ext4_directory_entry_ll_get_inode(dentry);
    649649                uint16_t rec_len = ext4_directory_entry_ll_get_entry_length(dentry);
    650                
     650
    651651                /* If invalid and large enough entry, use it */
    652652                if ((inode == 0) && (rec_len >= required_len)) {
     
    654654                            name, name_len);
    655655                        target_block->dirty = true;
    656                        
     656
    657657                        return EOK;
    658658                }
    659                
     659
    660660                /* Valid entry, try to split it */
    661661                if (inode != 0) {
    662662                        uint16_t used_name_len =
    663663                            ext4_directory_entry_ll_get_name_length(sb, dentry);
    664                        
     664
    665665                        uint16_t used_space =
    666666                            sizeof(ext4_fake_directory_entry_t) + used_name_len;
    667                        
     667
    668668                        if ((used_name_len % 4) != 0)
    669669                                used_space += 4 - (used_name_len % 4);
    670                        
     670
    671671                        uint16_t free_space = rec_len - used_space;
    672                        
     672
    673673                        /* There is free space for new entry */
    674674                        if (free_space >= required_len) {
     
    679679                                ext4_directory_write_entry(sb, new_entry,
    680680                                    free_space, child, name, name_len);
    681                                
     681
    682682                                target_block->dirty = true;
    683                                
     683
    684684                                return EOK;
    685685                        }
    686686                }
    687                
     687
    688688                /* Jump to the next entry */
    689689                dentry = (void *) dentry + rec_len;
    690690        }
    691        
     691
    692692        /* No free space found for new entry */
    693693        return ENOSPC;
     
    711711        ext4_directory_entry_ll_t *dentry =
    712712            (ext4_directory_entry_ll_t *) block->data;
    713        
     713
    714714        /* Set upper bound for cycling */
    715715        uint8_t *addr_limit = block->data + ext4_superblock_get_block_size(sb);
    716        
     716
    717717        /* Walk through the block and check entries */
    718718        while ((uint8_t *) dentry < addr_limit) {
     
    720720                if ((uint8_t *) dentry + name_len > addr_limit)
    721721                        break;
    722                
     722
    723723                /* Valid entry - check it */
    724724                if (dentry->inode != 0) {
     
    733733                        }
    734734                }
    735                
     735
    736736                uint16_t dentry_len =
    737737                    ext4_directory_entry_ll_get_entry_length(dentry);
    738                
     738
    739739                /* Corrupted entry */
    740740                if (dentry_len == 0)
    741741                        return EINVAL;
    742                
     742
    743743                /* Jump to next entry */
    744744                dentry = (ext4_directory_entry_ll_t *) ((uint8_t *) dentry + dentry_len);
    745745        }
    746        
     746
    747747        /* Entry not found */
    748748        return ENOENT;
     
    760760        if (result->block)
    761761                return block_put(result->block);
    762        
     762
    763763        return EOK;
    764764}
  • uspace/lib/ext4/src/directory_index.c

    r3061bc1 ra35b458  
    244244        if (rc != EOK)
    245245                return rc;
    246        
     246
    247247        block_t *block;
    248248        rc = block_get(&block, dir->fs->device, fblock, BLOCK_FLAGS_NONE);
    249249        if (rc != EOK)
    250250                return rc;
    251        
     251
    252252        /* Initialize pointers to data structures */
    253253        ext4_directory_dx_root_t *root = block->data;
    254254        ext4_directory_dx_root_info_t *info = &(root->info);
    255        
     255
    256256        /* Initialize root info structure */
    257257        uint8_t hash_version =
    258258            ext4_superblock_get_default_hash_version(dir->fs->superblock);
    259        
     259
    260260        ext4_directory_dx_root_info_set_hash_version(info, hash_version);
    261261        ext4_directory_dx_root_info_set_indirect_levels(info, 0);
    262262        ext4_directory_dx_root_info_set_info_length(info, 8);
    263        
     263
    264264        /* Set limit and current number of entries */
    265265        ext4_directory_dx_countlimit_t *countlimit =
    266266            (ext4_directory_dx_countlimit_t *) &root->entries;
    267267        ext4_directory_dx_countlimit_set_count(countlimit, 1);
    268        
     268
    269269        uint32_t block_size =
    270270            ext4_superblock_get_block_size(dir->fs->superblock);
     
    274274        uint16_t root_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
    275275        ext4_directory_dx_countlimit_set_limit(countlimit, root_limit);
    276        
     276
    277277        /* Append new block, where will be new entries inserted in the future */
    278278        uint32_t iblock;
     
    282282                return rc;
    283283        }
    284        
     284
    285285        block_t *new_block;
    286286        rc = block_get(&new_block, dir->fs->device, fblock, BLOCK_FLAGS_NOREAD);
     
    289289                return rc;
    290290        }
    291        
     291
    292292        /* Fill the whole block with empty entry */
    293293        ext4_directory_entry_ll_t *block_entry = new_block->data;
    294294        ext4_directory_entry_ll_set_entry_length(block_entry, block_size);
    295295        ext4_directory_entry_ll_set_inode(block_entry, 0);
    296        
     296
    297297        new_block->dirty = true;
    298298        rc = block_put(new_block);
     
    301301                return rc;
    302302        }
    303        
     303
    304304        /* Connect new block to the only entry in index */
    305305        ext4_directory_dx_entry_t *entry = root->entries;
    306306        ext4_directory_dx_entry_set_block(entry, iblock);
    307        
     307
    308308        block->dirty = true;
    309        
     309
    310310        return block_put(block);
    311311}
     
    328328        ext4_directory_dx_root_t *root =
    329329            (ext4_directory_dx_root_t *) root_block->data;
    330        
     330
    331331        if ((root->info.hash_version != EXT4_HASH_VERSION_TEA) &&
    332332            (root->info.hash_version != EXT4_HASH_VERSION_HALF_MD4) &&
    333333            (root->info.hash_version != EXT4_HASH_VERSION_LEGACY))
    334334                return EXT4_ERR_BAD_DX_DIR;
    335        
     335
    336336        /* Check unused flags */
    337337        if (root->info.unused_flags != 0)
    338338                return EXT4_ERR_BAD_DX_DIR;
    339        
     339
    340340        /* Check indirect levels */
    341341        if (root->info.indirect_levels > 1)
    342342                return EXT4_ERR_BAD_DX_DIR;
    343        
     343
    344344        /* Check if node limit is correct */
    345345        uint32_t block_size = ext4_superblock_get_block_size(sb);
     
    348348        entry_space -= sizeof(ext4_directory_dx_root_info_t);
    349349        entry_space = entry_space / sizeof(ext4_directory_dx_entry_t);
    350        
     350
    351351        uint16_t limit = ext4_directory_dx_countlimit_get_limit(
    352352            (ext4_directory_dx_countlimit_t *) &root->entries);
    353353        if (limit != entry_space)
    354354                return EXT4_ERR_BAD_DX_DIR;
    355        
     355
    356356        /* Check hash version and modify if necessary */
    357357        hinfo->hash_version =
     
    362362                hinfo->hash_version += 3;
    363363        }
    364        
     364
    365365        /* Load hash seed from superblock */
    366366        hinfo->seed = ext4_superblock_get_hash_seed(sb);
    367        
     367
    368368        /* Compute hash value of name */
    369369        if (name)
    370370                ext4_hash_string(hinfo, name_len, name);
    371        
     371
    372372        return EOK;
    373373}
     
    393393        ext4_directory_dx_entry_t *entries =
    394394            (ext4_directory_dx_entry_t *) &root->entries;
    395        
     395
    396396        uint16_t limit = ext4_directory_dx_countlimit_get_limit(
    397397            (ext4_directory_dx_countlimit_t *) entries);
    398398        uint8_t indirect_level =
    399399            ext4_directory_dx_root_info_get_indirect_levels(&root->info);
    400        
     400
    401401        block_t *tmp_block = root_block;
    402402        ext4_directory_dx_entry_t *p;
     
    404404        ext4_directory_dx_entry_t *m;
    405405        ext4_directory_dx_entry_t *at;
    406        
     406
    407407        /* Walk through the index tree */
    408408        while (true) {
     
    411411                if ((count == 0) || (count > limit))
    412412                        return EXT4_ERR_BAD_DX_DIR;
    413                
     413
    414414                /* Do binary search in every node */
    415415                p = entries + 1;
    416416                q = entries + count - 1;
    417                
     417
    418418                while (p <= q) {
    419419                        m = p + (q - p) / 2;
     
    423423                                p = m + 1;
    424424                }
    425                
     425
    426426                at = p - 1;
    427                
     427
    428428                /* Write results */
    429429                tmp_dx_block->block = tmp_block;
    430430                tmp_dx_block->entries = entries;
    431431                tmp_dx_block->position = at;
    432                
     432
    433433                /* Is algorithm in the leaf? */
    434434                if (indirect_level == 0) {
     
    436436                        return EOK;
    437437                }
    438                
     438
    439439                /* Goto child node */
    440440                uint32_t next_block = ext4_directory_dx_entry_get_block(at);
    441                
     441
    442442                indirect_level--;
    443                
     443
    444444                uint32_t fblock;
    445445                errno_t rc = ext4_filesystem_get_inode_data_block_index(inode_ref,
     
    447447                if (rc != EOK)
    448448                        return rc;
    449                
     449
    450450                rc = block_get(&tmp_block, inode_ref->fs->device, fblock,
    451451                    BLOCK_FLAGS_NONE);
    452452                if (rc != EOK)
    453453                        return rc;
    454                
     454
    455455                entries = ((ext4_directory_dx_node_t *) tmp_block->data)->entries;
    456456                limit = ext4_directory_dx_countlimit_get_limit(
    457457                    (ext4_directory_dx_countlimit_t *) entries);
    458                
     458
    459459                uint16_t entry_space =
    460460                    ext4_superblock_get_block_size(inode_ref->fs->superblock) -
    461461                    sizeof(ext4_directory_dx_dot_entry_t);
    462462                entry_space = entry_space / sizeof(ext4_directory_dx_entry_t);
    463                
     463
    464464                if (limit != entry_space) {
    465465                        block_put(tmp_block);
    466466                        return EXT4_ERR_BAD_DX_DIR;
    467467                }
    468                
     468
    469469                ++tmp_dx_block;
    470470        }
    471        
     471
    472472        /* Unreachable */
    473473        return EOK;
     
    490490        uint32_t num_handles = 0;
    491491        ext4_directory_dx_block_t *p = dx_block;
    492        
     492
    493493        /* Try to find data block with next bunch of entries */
    494494        while (true) {
     
    496496                uint16_t count = ext4_directory_dx_countlimit_get_count(
    497497                    (ext4_directory_dx_countlimit_t *) p->entries);
    498                
     498
    499499                if (p->position < p->entries + count)
    500500                        break;
    501                
     501
    502502                if (p == dx_blocks)
    503503                        return EOK;
    504                
     504
    505505                num_handles++;
    506506                p--;
    507507        }
    508        
     508
    509509        /* Check hash collision (if not occured - no next block cannot be used) */
    510510        uint32_t current_hash = ext4_directory_dx_entry_get_hash(p->position);
     
    513513                        return 0;
    514514        }
    515        
     515
    516516        /* Fill new path */
    517517        while (num_handles--) {
     
    519519                    ext4_directory_dx_entry_get_block(p->position);
    520520                uint32_t block_addr;
    521                
     521
    522522                errno_t rc = ext4_filesystem_get_inode_data_block_index(inode_ref,
    523523                    block_idx, &block_addr);
    524524                if (rc != EOK)
    525525                        return rc;
    526                
     526
    527527                block_t *block;
    528528                rc = block_get(&block, inode_ref->fs->device, block_addr, BLOCK_FLAGS_NONE);
    529529                if (rc != EOK)
    530530                        return rc;
    531                
     531
    532532                p++;
    533                
     533
    534534                /* Don't forget to put old block (prevent memory leak) */
    535535                rc = block_put(p->block);
    536536                if (rc != EOK)
    537537                        return rc;
    538                
     538
    539539                p->block = block;
    540540                p->entries = ((ext4_directory_dx_node_t *) block->data)->entries;
    541541                p->position = p->entries;
    542542        }
    543        
     543
    544544        return ENOENT;
    545545}
     
    566566        if (rc != EOK)
    567567                return rc;
    568        
     568
    569569        ext4_filesystem_t *fs = inode_ref->fs;
    570        
     570
    571571        block_t *root_block;
    572572        rc = block_get(&root_block, fs->device, root_block_addr,
     
    574574        if (rc != EOK)
    575575                return rc;
    576        
     576
    577577        /* Initialize hash info (compute hash value) */
    578578        ext4_hash_info_t hinfo;
     
    583583                return EXT4_ERR_BAD_DX_DIR;
    584584        }
    585        
     585
    586586        /*
    587587         * Hardcoded number 2 means maximum height of index tree,
     
    591591        ext4_directory_dx_block_t *dx_block;
    592592        ext4_directory_dx_block_t *tmp;
    593        
     593
    594594        rc = ext4_directory_dx_get_leaf(&hinfo, inode_ref, root_block,
    595595            &dx_block, dx_blocks);
     
    598598                return EXT4_ERR_BAD_DX_DIR;
    599599        }
    600        
     600
    601601        do {
    602602                /* Load leaf block */
     
    604604                    ext4_directory_dx_entry_get_block(dx_block->position);
    605605                uint32_t leaf_block_addr;
    606                
     606
    607607                rc = ext4_filesystem_get_inode_data_block_index(inode_ref,
    608608                    leaf_block_idx, &leaf_block_addr);
    609609                if (rc != EOK)
    610610                        goto cleanup;
    611                
     611
    612612                block_t *leaf_block;
    613613                rc = block_get(&leaf_block, fs->device, leaf_block_addr,
     
    615615                if (rc != EOK)
    616616                        goto cleanup;
    617                
     617
    618618                /* Linear search inside block */
    619619                ext4_directory_entry_ll_t *res_dentry;
    620620                rc = ext4_directory_find_in_block(leaf_block, fs->superblock,
    621621                    name_len, name, &res_dentry);
    622                
     622
    623623                /* Found => return it */
    624624                if (rc == EOK) {
     
    627627                        goto cleanup;
    628628                }
    629                
     629
    630630                /* Not found, leave untouched */
    631631                rc2 = block_put(leaf_block);
    632632                if (rc2 != EOK)
    633633                        goto cleanup;
    634                
     634
    635635                if (rc != ENOENT)
    636636                        goto cleanup;
    637                
     637
    638638                /* check if the next block could be checked */
    639639                rc = ext4_directory_dx_next_block(inode_ref, hinfo.hash,
     
    643643
    644644        } while (rc == ENOENT);
    645        
     645
    646646        /* Entry not found */
    647647        rc = ENOENT;
    648        
     648
    649649cleanup:
    650650        /* The whole path must be released (preventing memory leak) */
    651651        tmp = dx_blocks;
    652        
     652
    653653        while (tmp <= dx_block) {
    654654                rc2 = block_put(tmp->block);
     
    657657                ++tmp;
    658658        }
    659        
     659
    660660        return rc;
    661661}
     
    677677        ext4_dx_sort_entry_t const *entry1 = arg1;
    678678        ext4_dx_sort_entry_t const *entry2 = arg2;
    679        
     679
    680680        if (entry1->hash == entry2->hash)
    681681                return 0;
    682        
     682
    683683        if (entry1->hash < entry2->hash)
    684684                return -1;
     
    701701        ext4_directory_dx_entry_t *old_index_entry = index_block->position;
    702702        ext4_directory_dx_entry_t *new_index_entry = old_index_entry + 1;
    703        
     703
    704704        ext4_directory_dx_countlimit_t *countlimit =
    705705            (ext4_directory_dx_countlimit_t *) index_block->entries;
    706706        uint32_t count = ext4_directory_dx_countlimit_get_count(countlimit);
    707        
     707
    708708        ext4_directory_dx_entry_t *start_index = index_block->entries;
    709709        size_t bytes = (void *) (start_index + count) - (void *) (new_index_entry);
    710        
     710
    711711        memmove(new_index_entry + 1, new_index_entry, bytes);
    712        
     712
    713713        ext4_directory_dx_entry_set_block(new_index_entry, iblock);
    714714        ext4_directory_dx_entry_set_hash(new_index_entry, hash);
    715        
     715
    716716        ext4_directory_dx_countlimit_set_count(countlimit, count + 1);
    717        
     717
    718718        index_block->block->dirty = true;
    719719}
     
    733733{
    734734        errno_t rc = EOK;
    735        
     735
    736736        /* Allocate buffer for directory entries */
    737737        uint32_t block_size =
     
    740740        if (entry_buffer == NULL)
    741741                return ENOMEM;
    742        
     742
    743743        /* dot entry has the smallest size available */
    744744        uint32_t max_entry_count =
    745745            block_size / sizeof(ext4_directory_dx_dot_entry_t);
    746        
     746
    747747        /* Allocate sort entry */
    748748        ext4_dx_sort_entry_t *sort_array =
     
    752752                return ENOMEM;
    753753        }
    754        
     754
    755755        uint32_t idx = 0;
    756756        uint32_t real_size = 0;
    757        
     757
    758758        /* Initialize hinfo */
    759759        ext4_hash_info_t tmp_hinfo;
    760760        memcpy(&tmp_hinfo, hinfo, sizeof(ext4_hash_info_t));
    761        
     761
    762762        /* Load all valid entries to the buffer */
    763763        ext4_directory_entry_ll_t *dentry = old_data_block->data;
     
    769769                            inode_ref->fs->superblock, dentry);
    770770                        ext4_hash_string(&tmp_hinfo, len, (char *) dentry->name);
    771                        
     771
    772772                        uint32_t rec_len = 8 + len;
    773                        
     773
    774774                        if ((rec_len % 4) != 0)
    775775                                rec_len += 4 - (rec_len % 4);
    776                        
     776
    777777                        memcpy(entry_buffer_ptr, dentry, rec_len);
    778                        
     778
    779779                        sort_array[idx].dentry = entry_buffer_ptr;
    780780                        sort_array[idx].rec_len = rec_len;
    781781                        sort_array[idx].hash = tmp_hinfo.hash;
    782                        
     782
    783783                        entry_buffer_ptr += rec_len;
    784784                        real_size += rec_len;
    785785                        idx++;
    786786                }
    787                
     787
    788788                dentry = (void *) dentry +
    789789                    ext4_directory_entry_ll_get_entry_length(dentry);
    790790        }
    791        
     791
    792792        /* Sort all entries */
    793793        qsort(sort_array, idx, sizeof(ext4_dx_sort_entry_t),
    794794            ext4_directory_dx_entry_comparator);
    795        
     795
    796796        /* Allocate new block for store the second part of entries */
    797797        uint32_t new_fblock;
     
    804804                return rc;
    805805        }
    806        
     806
    807807        /* Load new block */
    808808        block_t *new_data_block_tmp;
     
    814814                return rc;
    815815        }
    816        
     816
    817817        /*
    818818         * Distribute entries to two blocks (by size)
     
    828828                        break;
    829829                }
    830                
     830
    831831                current_size += sort_array[i].rec_len;
    832832        }
    833        
     833
    834834        /* Check hash collision */
    835835        uint32_t continued = 0;
    836836        if (new_hash == sort_array[mid-1].hash)
    837837                continued = 1;
    838        
     838
    839839        uint32_t offset = 0;
    840840        void *ptr;
    841        
     841
    842842        /* First part - to the old block */
    843843        for (uint32_t i = 0; i < mid; ++i) {
    844844                ptr = old_data_block->data + offset;
    845845                memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
    846                
     846
    847847                ext4_directory_entry_ll_t *tmp = ptr;
    848848                if (i < (mid - 1))
     
    852852                        ext4_directory_entry_ll_set_entry_length(tmp,
    853853                            block_size - offset);
    854                
     854
    855855                offset += sort_array[i].rec_len;
    856856        }
    857        
     857
    858858        /* Second part - to the new block */
    859859        offset = 0;
     
    861861                ptr = new_data_block_tmp->data + offset;
    862862                memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
    863                
     863
    864864                ext4_directory_entry_ll_t *tmp = ptr;
    865865                if (i < (idx - 1))
     
    869869                        ext4_directory_entry_ll_set_entry_length(tmp,
    870870                            block_size - offset);
    871                
     871
    872872                offset += sort_array[i].rec_len;
    873873        }
    874        
     874
    875875        /* Do some steps to finish operation */
    876876        old_data_block->dirty = true;
    877877        new_data_block_tmp->dirty = true;
    878        
     878
    879879        free(sort_array);
    880880        free(entry_buffer);
    881        
     881
    882882        ext4_directory_dx_insert_entry(index_block, new_hash + continued,
    883883            new_iblock);
    884        
     884
    885885        *new_data_block = new_data_block_tmp;
    886        
     886
    887887        return EOK;
    888888}
     
    907907                entries =
    908908                    ((ext4_directory_dx_node_t *) dx_block->block->data)->entries;
    909        
     909
    910910        ext4_directory_dx_countlimit_t *countlimit =
    911911            (ext4_directory_dx_countlimit_t *) entries;
    912        
     912
    913913        uint16_t leaf_limit =
    914914            ext4_directory_dx_countlimit_get_limit(countlimit);
    915915        uint16_t leaf_count =
    916916            ext4_directory_dx_countlimit_get_count(countlimit);
    917        
     917
    918918        /* Check if is necessary to split index block */
    919919        if (leaf_limit == leaf_count) {
    920920                size_t levels = dx_block - dx_blocks;
    921                
     921
    922922                ext4_directory_dx_entry_t *root_entries =
    923923                    ((ext4_directory_dx_root_t *) dx_blocks[0].block->data)->entries;
    924                
     924
    925925                ext4_directory_dx_countlimit_t *root_countlimit =
    926926                    (ext4_directory_dx_countlimit_t *) root_entries;
     
    929929                uint16_t root_count =
    930930                    ext4_directory_dx_countlimit_get_count(root_countlimit);
    931                
     931
    932932                /* Linux limitation */
    933933                if ((levels > 0) && (root_limit == root_count))
    934934                        return ENOSPC;
    935                
     935
    936936                /* Add new block to directory */
    937937                uint32_t new_fblock;
     
    941941                if (rc != EOK)
    942942                        return rc;
    943                
     943
    944944                /* load new block */
    945945                block_t *new_block;
     
    948948                if (rc != EOK)
    949949                        return rc;
    950                
     950
    951951                ext4_directory_dx_node_t *new_node = new_block->data;
    952952                ext4_directory_dx_entry_t *new_entries = new_node->entries;
    953                
     953
    954954                uint32_t block_size =
    955955                    ext4_superblock_get_block_size(inode_ref->fs->superblock);
    956                
     956
    957957                /* Split leaf node */
    958958                if (levels > 0) {
     
    961961                        uint32_t hash_right =
    962962                            ext4_directory_dx_entry_get_hash(entries + count_left);
    963                        
     963
    964964                        /* Copy data to new node */
    965965                        memcpy((void *) new_entries, (void *) (entries + count_left),
    966966                            count_right * sizeof(ext4_directory_dx_entry_t));
    967                        
     967
    968968                        /* Initialize new node */
    969969                        ext4_directory_dx_countlimit_t *left_countlimit =
     
    971971                        ext4_directory_dx_countlimit_t *right_countlimit =
    972972                            (ext4_directory_dx_countlimit_t *) new_entries;
    973                        
     973
    974974                        ext4_directory_dx_countlimit_set_count(left_countlimit, count_left);
    975975                        ext4_directory_dx_countlimit_set_count(right_countlimit, count_right);
    976                        
     976
    977977                        uint32_t entry_space =
    978978                            block_size - sizeof(ext4_fake_directory_entry_t);
     
    980980                            entry_space / sizeof(ext4_directory_dx_entry_t);
    981981                        ext4_directory_dx_countlimit_set_limit(right_countlimit, node_limit);
    982                        
     982
    983983                        /* Which index block is target for new entry */
    984984                        uint32_t position_index = (dx_block->position - dx_block->entries);
    985985                        if (position_index >= count_left) {
    986986                                dx_block->block->dirty = true;
    987                                
     987
    988988                                block_t *block_tmp = dx_block->block;
    989989                                dx_block->block = new_block;
     
    991991                                    new_entries + position_index - count_left;
    992992                                dx_block->entries = new_entries;
    993                                
     993
    994994                                new_block = block_tmp;
    995995                        }
    996                        
     996
    997997                        /* Finally insert new entry */
    998998                        ext4_directory_dx_insert_entry(dx_blocks, hash_right, new_iblock);
    999                        
     999
    10001000                        return block_put(new_block);
    10011001                } else {
    10021002                        /* Create second level index */
    1003                        
     1003
    10041004                        /* Copy data from root to child block */
    10051005                        memcpy((void *) new_entries, (void *) entries,
    10061006                            leaf_count * sizeof(ext4_directory_dx_entry_t));
    1007                        
     1007
    10081008                        ext4_directory_dx_countlimit_t *new_countlimit =
    10091009                            (ext4_directory_dx_countlimit_t *) new_entries;
    1010                        
     1010
    10111011                        uint32_t entry_space =
    10121012                            block_size - sizeof(ext4_fake_directory_entry_t);
     
    10141014                            entry_space / sizeof(ext4_directory_dx_entry_t);
    10151015                        ext4_directory_dx_countlimit_set_limit(new_countlimit, node_limit);
    1016                        
     1016
    10171017                        /* Set values in root node */
    10181018                        ext4_directory_dx_countlimit_t *new_root_countlimit =
    10191019                            (ext4_directory_dx_countlimit_t *) entries;
    1020                        
     1020
    10211021                        ext4_directory_dx_countlimit_set_count(new_root_countlimit, 1);
    10221022                        ext4_directory_dx_entry_set_block(entries, new_iblock);
    1023                        
     1023
    10241024                        ((ext4_directory_dx_root_t *)
    10251025                            dx_blocks[0].block->data)->info.indirect_levels = 1;
    1026                        
     1026
    10271027                        /* Add new entry to the path */
    10281028                        dx_block = dx_blocks + 1;
     
    10321032                }
    10331033        }
    1034        
     1034
    10351035        return EOK;
    10361036}
     
    10491049{
    10501050        errno_t rc2 = EOK;
    1051        
     1051
    10521052        /* Get direct block 0 (index root) */
    10531053        uint32_t root_block_addr;
     
    10561056        if (rc != EOK)
    10571057                return rc;
    1058        
     1058
    10591059        ext4_filesystem_t *fs = parent->fs;
    1060        
     1060
    10611061        block_t *root_block;
    10621062        rc = block_get(&root_block, fs->device, root_block_addr,
     
    10641064        if (rc != EOK)
    10651065                return rc;
    1066        
     1066
    10671067        /* Initialize hinfo structure (mainly compute hash) */
    10681068        uint32_t name_len = str_size(name);
     
    10741074                return EXT4_ERR_BAD_DX_DIR;
    10751075        }
    1076        
     1076
    10771077        /*
    10781078         * Hardcoded number 2 means maximum height of index
     
    10821082        ext4_directory_dx_block_t *dx_block;
    10831083        ext4_directory_dx_block_t *dx_it;
    1084        
     1084
    10851085        rc = ext4_directory_dx_get_leaf(&hinfo, parent, root_block,
    10861086            &dx_block, dx_blocks);
     
    10891089                goto release_index;
    10901090        }
    1091        
     1091
    10921092        /* Try to insert to existing data block */
    10931093        uint32_t leaf_block_idx =
     
    10981098        if (rc != EOK)
    10991099                goto release_index;
    1100        
     1100
    11011101        block_t *target_block;
    11021102        rc = block_get(&target_block, fs->device, leaf_block_addr,
     
    11041104        if (rc != EOK)
    11051105                goto release_index;
    1106        
     1106
    11071107        /* Check if insert operation passed */
    11081108        rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child,
     
    11101110        if (rc == EOK)
    11111111                goto release_target_index;
    1112        
     1112
    11131113        /*
    11141114         * Check if there is needed to split index node
     
    11181118        if (rc != EOK)
    11191119                goto release_target_index;
    1120        
     1120
    11211121        /* Split entries to two blocks (includes sorting by hash value) */
    11221122        block_t *new_block = NULL;
     
    11271127                goto release_target_index;
    11281128        }
    1129        
     1129
    11301130        /* Where to save new entry */
    11311131        uint32_t new_block_hash =
     
    11371137                rc = ext4_directory_try_insert_entry(fs->superblock, target_block,
    11381138                    child, name, name_len);
    1139        
     1139
    11401140        /* Cleanup */
    11411141        rc = block_put(new_block);
    11421142        if (rc != EOK)
    11431143                return rc;
    1144        
     1144
    11451145        /* Cleanup operations */
    1146        
     1146
    11471147release_target_index:
    11481148        rc2 = rc;
    1149        
     1149
    11501150        rc = block_put(target_block);
    11511151        if (rc != EOK)
    11521152                return rc;
    1153        
     1153
    11541154release_index:
    11551155        if (rc != EOK)
    11561156                rc2 = rc;
    1157        
     1157
    11581158        dx_it = dx_blocks;
    1159        
     1159
    11601160        while (dx_it <= dx_block) {
    11611161                rc = block_put(dx_it->block);
    11621162                if (rc != EOK)
    11631163                        return rc;
    1164                
     1164
    11651165                dx_it++;
    11661166        }
    1167        
     1167
    11681168        return rc2;
    11691169}
  • uspace/lib/ext4/src/extent.c

    r3061bc1 ra35b458  
    295295        ext4_extent_index_t *l;
    296296        ext4_extent_index_t *m;
    297        
     297
    298298        uint16_t entries_count =
    299299            ext4_extent_header_get_entries_count(header);
    300        
     300
    301301        /* Initialize bounds */
    302302        l = EXT4_EXTENT_FIRST_INDEX(header) + 1;
    303303        r = EXT4_EXTENT_FIRST_INDEX(header) + entries_count - 1;
    304        
     304
    305305        /* Do binary search */
    306306        while (l <= r) {
    307307                m = l + (r - l) / 2;
    308308                uint32_t first_block = ext4_extent_index_get_first_block(m);
    309                
     309
    310310                if (iblock < first_block)
    311311                        r = m - 1;
     
    313313                        l = m + 1;
    314314        }
    315        
     315
    316316        /* Set output value */
    317317        *index = l - 1;
     
    332332        ext4_extent_t *l;
    333333        ext4_extent_t *m;
    334        
     334
    335335        uint16_t entries_count =
    336336            ext4_extent_header_get_entries_count(header);
    337        
     337
    338338        if (entries_count == 0) {
    339339                /* this leaf is empty */
     
    341341                return;
    342342        }
    343        
     343
    344344        /* Initialize bounds */
    345345        l = EXT4_EXTENT_FIRST(header) + 1;
    346346        r = EXT4_EXTENT_FIRST(header) + entries_count - 1;
    347        
     347
    348348        /* Do binary search */
    349349        while (l <= r) {
    350350                m = l + (r - l) / 2;
    351351                uint32_t first_block = ext4_extent_get_first_block(m);
    352                
     352
    353353                if (iblock < first_block)
    354354                        r = m - 1;
     
    356356                        l = m + 1;
    357357        }
    358        
     358
    359359        /* Set output value */
    360360        *extent = l - 1;
     
    379379        uint64_t inode_size =
    380380            ext4_inode_get_size(inode_ref->fs->superblock, inode_ref->inode);
    381        
     381
    382382        uint32_t block_size =
    383383            ext4_superblock_get_block_size(inode_ref->fs->superblock);
    384        
     384
    385385        uint32_t last_idx = (inode_size - 1) / block_size;
    386        
     386
    387387        /* Check if requested iblock is not over size of i-node */
    388388        if (iblock > last_idx) {
     
    390390                return EOK;
    391391        }
    392        
     392
    393393        block_t *block = NULL;
    394        
     394
    395395        /* Walk through extent tree */
    396396        ext4_extent_header_t *header =
    397397            ext4_inode_get_extent_header(inode_ref->inode);
    398        
     398
    399399        while (ext4_extent_header_get_depth(header) != 0) {
    400400                /* Search index in node */
    401401                ext4_extent_index_t *index;
    402402                ext4_extent_binsearch_idx(header, &index, iblock);
    403                
     403
    404404                /* Load child node and set values for the next iteration */
    405405                uint64_t child = ext4_extent_index_get_leaf(index);
    406                
     406
    407407                if (block != NULL) {
    408408                        rc = block_put(block);
     
    410410                                return rc;
    411411                }
    412                
     412
    413413                rc = block_get(&block, inode_ref->fs->device, child,
    414414                    BLOCK_FLAGS_NONE);
    415415                if (rc != EOK)
    416416                        return rc;
    417                
     417
    418418                header = (ext4_extent_header_t *)block->data;
    419419        }
    420        
     420
    421421        /* Search extent in the leaf block */
    422422        ext4_extent_t* extent = NULL;
    423423        ext4_extent_binsearch(header, &extent, iblock);
    424        
     424
    425425        /* Prevent empty leaf */
    426426        if (extent == NULL) {
     
    431431                uint32_t first = ext4_extent_get_first_block(extent);
    432432                phys_block = ext4_extent_get_start(extent) + iblock - first;
    433                
     433
    434434                *fblock = phys_block;
    435435        }
    436        
     436
    437437        /* Cleanup */
    438438        if (block != NULL)
    439439                rc = block_put(block);
    440        
     440
    441441        return rc;
    442442}
     
    459459        ext4_extent_header_t *eh =
    460460            ext4_inode_get_extent_header(inode_ref->inode);
    461        
     461
    462462        uint16_t depth = ext4_extent_header_get_depth(eh);
    463        
     463
    464464        ext4_extent_path_t *tmp_path;
    465        
     465
    466466        /* Added 2 for possible tree growing */
    467467        tmp_path = malloc(sizeof(ext4_extent_path_t) * (depth + 2));
    468468        if (tmp_path == NULL)
    469469                return ENOMEM;
    470        
     470
    471471        /* Initialize structure for algorithm start */
    472472        tmp_path[0].block = inode_ref->block;
    473473        tmp_path[0].header = eh;
    474        
     474
    475475        /* Walk through the extent tree */
    476476        uint16_t pos = 0;
     
    480480                ext4_extent_binsearch_idx(tmp_path[pos].header,
    481481                    &tmp_path[pos].index, iblock);
    482                
     482
    483483                tmp_path[pos].depth = depth;
    484484                tmp_path[pos].extent = NULL;
    485                
     485
    486486                assert(tmp_path[pos].index != NULL);
    487                
     487
    488488                /* Load information for the next iteration */
    489489                uint64_t fblock = ext4_extent_index_get_leaf(tmp_path[pos].index);
    490                
     490
    491491                block_t *block;
    492492                rc = block_get(&block, inode_ref->fs->device, fblock,
     
    494494                if (rc != EOK)
    495495                        goto cleanup;
    496                
     496
    497497                pos++;
    498                
     498
    499499                eh = (ext4_extent_header_t *)block->data;
    500500                tmp_path[pos].block = block;
    501501                tmp_path[pos].header = eh;
    502502        }
    503        
     503
    504504        tmp_path[pos].depth = 0;
    505505        tmp_path[pos].extent = NULL;
    506506        tmp_path[pos].index = NULL;
    507        
     507
    508508        /* Find extent in the leaf node */
    509509        ext4_extent_binsearch(tmp_path[pos].header, &tmp_path[pos].extent, iblock);
    510510        *ret_path = tmp_path;
    511        
     511
    512512        return EOK;
    513        
     513
    514514cleanup:
    515515        ;
     
    528528                }
    529529        }
    530        
     530
    531531        /* Destroy temporary data structure */
    532532        free(tmp_path);
    533        
     533
    534534        return rc;
    535535}
     
    549549        uint64_t start = ext4_extent_get_start(extent);
    550550        uint16_t block_count = ext4_extent_get_block_count(extent);
    551        
     551
    552552        return ext4_balloc_free_blocks(inode_ref, start, block_count);
    553553}
     
    569569{
    570570        uint32_t fblock = ext4_extent_index_get_leaf(index);
    571        
     571
    572572        block_t* block;
    573573        errno_t rc = block_get(&block, inode_ref->fs->device, fblock, BLOCK_FLAGS_NONE);
    574574        if (rc != EOK)
    575575                return rc;
    576        
     576
    577577        ext4_extent_header_t *header = block->data;
    578        
     578
    579579        if (ext4_extent_header_get_depth(header)) {
    580580                /* The node is non-leaf, do recursion */
    581581                ext4_extent_index_t *idx = EXT4_EXTENT_FIRST_INDEX(header);
    582                
     582
    583583                /* Release all subbranches */
    584584                for (uint32_t i = 0;
     
    592592                /* Leaf node reached */
    593593                ext4_extent_t *ext = EXT4_EXTENT_FIRST(header);
    594                
     594
    595595                /* Release all extents and stop recursion */
    596596                for (uint32_t i = 0;
     
    602602                }
    603603        }
    604        
     604
    605605        /* Release data block where the node was stored */
    606        
     606
    607607        rc = block_put(block);
    608608        if (rc != EOK)
    609609                return rc;
    610        
     610
    611611        return ext4_balloc_free_block(inode_ref, fblock);
    612612}
     
    626626        if (rc != EOK)
    627627                return rc;
    628        
     628
    629629        /* Jump to last item of the path (extent) */
    630630        ext4_extent_path_t *path_ptr = path;
    631631        while (path_ptr->depth != 0)
    632632                path_ptr++;
    633        
     633
    634634        assert(path_ptr->extent != NULL);
    635        
     635
    636636        /* First extent maybe released partially */
    637637        uint32_t first_iblock =
     
    639639        uint32_t first_fblock =
    640640            ext4_extent_get_start(path_ptr->extent) + iblock_from - first_iblock;
    641        
     641
    642642        uint16_t block_count = ext4_extent_get_block_count(path_ptr->extent);
    643        
     643
    644644        uint16_t delete_count = block_count -
    645645            (ext4_extent_get_start(path_ptr->extent) - first_fblock);
    646        
     646
    647647        /* Release all blocks */
    648648        rc = ext4_balloc_free_blocks(inode_ref, first_fblock, delete_count);
    649649        if (rc != EOK)
    650650                goto cleanup;
    651        
     651
    652652        /* Correct counter */
    653653        block_count -= delete_count;
    654654        ext4_extent_set_block_count(path_ptr->extent, block_count);
    655        
     655
    656656        /* Initialize the following loop */
    657657        uint16_t entries =
     
    659659        ext4_extent_t *tmp_ext = path_ptr->extent + 1;
    660660        ext4_extent_t *stop_ext = EXT4_EXTENT_FIRST(path_ptr->header) + entries;
    661        
     661
    662662        /* If first extent empty, release it */
    663663        if (block_count == 0)
    664664                entries--;
    665        
     665
    666666        /* Release all successors of the first extent in the same node */
    667667        while (tmp_ext < stop_ext) {
    668668                first_fblock = ext4_extent_get_start(tmp_ext);
    669669                delete_count = ext4_extent_get_block_count(tmp_ext);
    670                
     670
    671671                rc = ext4_balloc_free_blocks(inode_ref, first_fblock, delete_count);
    672672                if (rc != EOK)
    673673                        goto cleanup;
    674                
     674
    675675                entries--;
    676676                tmp_ext++;
    677677        }
    678        
     678
    679679        ext4_extent_header_set_entries_count(path_ptr->header, entries);
    680680        path_ptr->block->dirty = true;
    681        
     681
    682682        /* If leaf node is empty, parent entry must be modified */
    683683        bool remove_parent_record = false;
    684        
     684
    685685        /* Don't release root block (including inode data) !!! */
    686686        if ((path_ptr != path) && (entries == 0)) {
     
    688688                if (rc != EOK)
    689689                        goto cleanup;
    690                
     690
    691691                remove_parent_record = true;
    692692        }
    693        
     693
    694694        /* Jump to the parent */
    695695        --path_ptr;
    696        
     696
    697697        /* Release all successors in all tree levels */
    698698        while (path_ptr >= path) {
     
    701701                ext4_extent_index_t *stop =
    702702                    EXT4_EXTENT_FIRST_INDEX(path_ptr->header) + entries;
    703                
     703
    704704                /* Correct entries count because of changes in the previous iteration */
    705705                if (remove_parent_record)
    706706                        entries--;
    707                
     707
    708708                /* Iterate over all entries and release the whole subtrees */
    709709                while (index < stop) {
     
    711711                        if (rc != EOK)
    712712                                goto cleanup;
    713                        
     713
    714714                        ++index;
    715715                        --entries;
    716716                }
    717                
     717
    718718                ext4_extent_header_set_entries_count(path_ptr->header, entries);
    719719                path_ptr->block->dirty = true;
    720                
     720
    721721                /* Free the node if it is empty */
    722722                if ((entries == 0) && (path_ptr != path)) {
     
    724724                        if (rc != EOK)
    725725                                goto cleanup;
    726                        
     726
    727727                        /* Mark parent to be checked */
    728728                        remove_parent_record = true;
    729729                } else
    730730                        remove_parent_record = false;
    731                
     731
    732732                --path_ptr;
    733733        }
    734        
     734
    735735cleanup:
    736736        ;
     
    749749                }
    750750        }
    751        
     751
    752752        /* Destroy temporary data structure */
    753753        free(path);
    754        
     754
    755755        return rc;
    756756}
     
    771771{
    772772        ext4_extent_path_t *path_ptr = path + path->depth;
    773        
     773
    774774        uint32_t block_size =
    775775            ext4_superblock_get_block_size(inode_ref->fs->superblock);
    776        
     776
    777777        /* Start splitting */
    778778        while (path_ptr > path) {
     
    781781                uint16_t limit =
    782782                    ext4_extent_header_get_max_entries_count(path_ptr->header);
    783                
     783
    784784                if (entries == limit) {
    785785                        /* Full node - allocate block for new one */
     
    788788                        if (rc != EOK)
    789789                                return rc;
    790                        
     790
    791791                        block_t *block;
    792792                        rc = block_get(&block, inode_ref->fs->device, fblock,
     
    796796                                return rc;
    797797                        }
    798                        
     798
    799799                        /* Put back not modified old block */
    800800                        rc = block_put(path_ptr->block);
     
    804804                                return rc;
    805805                        }
    806                        
     806
    807807                        /* Initialize newly allocated block and remember it */
    808808                        memset(block->data, 0, block_size);
    809809                        path_ptr->block = block;
    810                        
     810
    811811                        /* Update pointers in extent path structure */
    812812                        path_ptr->header = block->data;
     
    823823                                    sizeof(ext4_extent_t);
    824824                        }
    825                        
     825
    826826                        /* Initialize on-disk structure (header) */
    827827                        ext4_extent_header_set_entries_count(path_ptr->header, 1);
     
    830830                        ext4_extent_header_set_depth(path_ptr->header, path_ptr->depth);
    831831                        ext4_extent_header_set_generation(path_ptr->header, 0);
    832                        
     832
    833833                        path_ptr->block->dirty = true;
    834                        
     834
    835835                        /* Jump to the preceeding item */
    836836                        path_ptr--;
     
    845845                                ext4_extent_set_first_block(path_ptr->extent, iblock);
    846846                        }
    847                        
     847
    848848                        ext4_extent_header_set_entries_count(path_ptr->header, entries + 1);
    849849                        path_ptr->block->dirty = true;
    850                        
     850
    851851                        /* No more splitting needed */
    852852                        return EOK;
    853853                }
    854854        }
    855        
     855
    856856        assert(path_ptr == path);
    857        
     857
    858858        /* Should be the root split too? */
    859        
     859
    860860        uint16_t entries = ext4_extent_header_get_entries_count(path->header);
    861861        uint16_t limit = ext4_extent_header_get_max_entries_count(path->header);
    862        
     862
    863863        if (entries == limit) {
    864864                uint32_t new_fblock;
     
    866866                if (rc != EOK)
    867867                        return rc;
    868                
     868
    869869                block_t *block;
    870870                rc = block_get(&block, inode_ref->fs->device, new_fblock,
     
    872872                if (rc != EOK)
    873873                        return rc;
    874                
     874
    875875                /* Initialize newly allocated block */
    876876                memset(block->data, 0, block_size);
    877                
     877
    878878                /* Move data from root to the new block */
    879879                memcpy(block->data, inode_ref->inode->blocks,
    880880                    EXT4_INODE_BLOCKS * sizeof(uint32_t));
    881                
     881
    882882                /* Data block is initialized */
    883                
     883
    884884                block_t *root_block = path->block;
    885885                uint16_t root_depth = path->depth;
    886886                ext4_extent_header_t *root_header = path->header;
    887                
     887
    888888                /* Make space for tree growing */
    889889                ext4_extent_path_t *new_root = path;
    890890                ext4_extent_path_t *old_root = path + 1;
    891                
     891
    892892                size_t nbytes = sizeof(ext4_extent_path_t) * (path->depth + 1);
    893893                memmove(old_root, new_root, nbytes);
    894894                memset(new_root, 0, sizeof(ext4_extent_path_t));
    895                
     895
    896896                /* Update old root structure */
    897897                old_root->block = block;
    898898                old_root->header = (ext4_extent_header_t *)block->data;
    899                
     899
    900900                /* Add new entry and update limit for entries */
    901901                if (old_root->depth) {
     
    913913                        old_root->index = NULL;
    914914                }
    915                
     915
    916916                ext4_extent_header_set_entries_count(old_root->header, entries + 1);
    917917                ext4_extent_header_set_max_entries_count(old_root->header, limit);
    918                
     918
    919919                old_root->block->dirty = true;
    920                
     920
    921921                /* Re-initialize new root metadata */
    922922                new_root->depth = root_depth + 1;
     
    925925                new_root->extent = NULL;
    926926                new_root->index = EXT4_EXTENT_FIRST_INDEX(new_root->header);
    927                
     927
    928928                ext4_extent_header_set_depth(new_root->header, new_root->depth);
    929                
     929
    930930                /* Create new entry in root */
    931931                ext4_extent_header_set_entries_count(new_root->header, 1);
    932932                ext4_extent_index_set_first_block(new_root->index, 0);
    933933                ext4_extent_index_set_leaf(new_root->index, new_fblock);
    934                
     934
    935935                new_root->block->dirty = true;
    936936        } else {
     
    943943                        ext4_extent_set_first_block(path->extent, iblock);
    944944                }
    945                
     945
    946946                ext4_extent_header_set_entries_count(path->header, entries + 1);
    947947                path->block->dirty = true;
    948948        }
    949        
     949
    950950        return EOK;
    951951}
     
    970970        uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
    971971        uint32_t block_size = ext4_superblock_get_block_size(sb);
    972        
     972
    973973        /* Calculate number of new logical block */
    974974        uint32_t new_block_idx = 0;
     
    976976                if ((inode_size % block_size) != 0)
    977977                        inode_size += block_size - (inode_size % block_size);
    978                
     978
    979979                new_block_idx = inode_size / block_size;
    980980        }
    981        
     981
    982982        /* Load the nearest leaf (with extent) */
    983983        ext4_extent_path_t *path;
     
    985985        if (rc != EOK)
    986986                return rc;
    987        
     987
    988988        /* Jump to last item of the path (extent) */
    989989        ext4_extent_path_t *path_ptr = path;
    990990        while (path_ptr->depth != 0)
    991991                path_ptr++;
    992        
     992
    993993        /* Add new extent to the node if not present */
    994994        if (path_ptr->extent == NULL)
    995995                goto append_extent;
    996        
     996
    997997        uint16_t block_count = ext4_extent_get_block_count(path_ptr->extent);
    998998        uint16_t block_limit = (1 << 15);
    999        
     999
    10001000        uint32_t phys_block = 0;
    10011001        if (block_count < block_limit) {
     
    10061006                        if (rc != EOK)
    10071007                                goto finish;
    1008                        
     1008
    10091009                        /* Initialize extent */
    10101010                        ext4_extent_set_first_block(path_ptr->extent, new_block_idx);
    10111011                        ext4_extent_set_start(path_ptr->extent, phys_block);
    10121012                        ext4_extent_set_block_count(path_ptr->extent, 1);
    1013                        
     1013
    10141014                        /* Update i-node */
    10151015                        if (update_size) {
     
    10171017                                inode_ref->dirty = true;
    10181018                        }
    1019                        
     1019
    10201020                        path_ptr->block->dirty = true;
    1021                        
     1021
    10221022                        goto finish;
    10231023                } else {
     
    10251025                        phys_block = ext4_extent_get_start(path_ptr->extent);
    10261026                        phys_block += ext4_extent_get_block_count(path_ptr->extent);
    1027                        
     1027
    10281028                        /* Check if the following block is free for allocation */
    10291029                        bool free;
     
    10311031                        if (rc != EOK)
    10321032                                goto finish;
    1033                        
     1033
    10341034                        if (!free) {
    10351035                                /* Target is not free, new block must be appended to new extent */
    10361036                                goto append_extent;
    10371037                        }
    1038                        
     1038
    10391039                        /* Update extent */
    10401040                        ext4_extent_set_block_count(path_ptr->extent, block_count + 1);
    1041                        
     1041
    10421042                        /* Update i-node */
    10431043                        if (update_size) {
     
    10451045                                inode_ref->dirty = true;
    10461046                        }
    1047                        
     1047
    10481048                        path_ptr->block->dirty = true;
    1049                        
     1049
    10501050                        goto finish;
    10511051                }
    10521052        }
    1053        
    1054        
     1053
     1054
    10551055append_extent:
    10561056        /* Append new extent to the tree */
    10571057        phys_block = 0;
    1058        
     1058
    10591059        /* Allocate new data block */
    10601060        rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
    10611061        if (rc != EOK)
    10621062                goto finish;
    1063        
     1063
    10641064        /* Append extent for new block (includes tree splitting if needed) */
    10651065        rc = ext4_extent_append_extent(inode_ref, path, new_block_idx);
     
    10681068                goto finish;
    10691069        }
    1070        
     1070
    10711071        uint32_t tree_depth = ext4_extent_header_get_depth(path->header);
    10721072        path_ptr = path + tree_depth;
    1073        
     1073
    10741074        /* Initialize newly created extent */
    10751075        ext4_extent_set_block_count(path_ptr->extent, 1);
    10761076        ext4_extent_set_first_block(path_ptr->extent, new_block_idx);
    10771077        ext4_extent_set_start(path_ptr->extent, phys_block);
    1078        
     1078
    10791079        /* Update i-node */
    10801080        if (update_size) {
     
    10821082                inode_ref->dirty = true;
    10831083        }
    1084        
     1084
    10851085        path_ptr->block->dirty = true;
    1086        
     1086
    10871087finish:
    10881088        ;
     
    10931093        *iblock = new_block_idx;
    10941094        *fblock = phys_block;
    1095        
     1095
    10961096        /*
    10971097         * Put loaded blocks
     
    11051105                }
    11061106        }
    1107        
     1107
    11081108        /* Destroy temporary data structure */
    11091109        free(path);
    1110        
     1110
    11111111        return rc;
    11121112}
  • 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}
  • uspace/lib/ext4/src/ialloc.c

    r3061bc1 ra35b458  
    100100{
    101101        ext4_superblock_t *sb = fs->superblock;
    102        
     102
    103103        /* Compute index of block group and load it */
    104104        uint32_t block_group = ext4_ialloc_get_bgid_of_inode(sb, index);
    105        
     105
    106106        ext4_block_group_ref_t *bg_ref;
    107107        errno_t rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
    108108        if (rc != EOK)
    109109                return rc;
    110        
     110
    111111        /* Load i-node bitmap */
    112112        uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
     
    117117        if (rc != EOK)
    118118                return rc;
    119        
     119
    120120        /* Free i-node in the bitmap */
    121121        uint32_t index_in_group = ext4_ialloc_inode2index_in_group(sb, index);
    122122        ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
    123123        bitmap_block->dirty = true;
    124        
     124
    125125        /* Put back the block with bitmap */
    126126        rc = block_put(bitmap_block);
     
    130130                return rc;
    131131        }
    132        
     132
    133133        /* If released i-node is a directory, decrement used directories count */
    134134        if (is_dir) {
     
    139139                    bg_used_dirs);
    140140        }
    141        
     141
    142142        /* Update block group free inodes count */
    143143        uint32_t free_inodes = ext4_block_group_get_free_inodes_count(
     
    146146        ext4_block_group_set_free_inodes_count(bg_ref->block_group, sb,
    147147            free_inodes);
    148        
     148
    149149        bg_ref->dirty = true;
    150        
     150
    151151        /* Put back the modified block group */
    152152        rc = ext4_filesystem_put_block_group_ref(bg_ref);
    153153        if (rc != EOK)
    154154                return rc;
    155        
     155
    156156        /* Update superblock free inodes count */
    157157        uint32_t sb_free_inodes =
     
    159159        sb_free_inodes++;
    160160        ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
    161        
     161
    162162        return EOK;
    163163}
     
    178178{
    179179        ext4_superblock_t *sb = fs->superblock;
    180        
     180
    181181        uint32_t bgid = 0;
    182182        uint32_t bg_count = ext4_superblock_get_block_group_count(sb);
    183183        uint32_t sb_free_inodes = ext4_superblock_get_free_inodes_count(sb);
    184184        uint32_t avg_free_inodes = sb_free_inodes / bg_count;
    185        
     185
    186186        /* Try to find free i-node in all block groups */
    187187        while (bgid < bg_count) {
     
    191191                if (rc != EOK)
    192192                        return rc;
    193                
     193
    194194                ext4_block_group_t *bg = bg_ref->block_group;
    195                
     195
    196196                /* Read necessary values for algorithm */
    197197                uint32_t free_blocks = ext4_block_group_get_free_blocks_count(bg, sb);
    198198                uint32_t free_inodes = ext4_block_group_get_free_inodes_count(bg, sb);
    199199                uint32_t used_dirs = ext4_block_group_get_used_dirs_count(bg, sb);
    200                
     200
    201201                /*
    202202                 * Check if this block group is a good candidate
     
    216216                        uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
    217217                            bg_ref->block_group, sb);
    218                        
     218
    219219                        block_t *bitmap_block;
    220220                        rc = block_get(&bitmap_block, fs->device, bitmap_block_addr,
     
    224224                                return rc;
    225225                        }
    226                        
     226
    227227                        /* Try to allocate i-node in the bitmap */
    228228                        uint32_t inodes_in_group = ext4_superblock_get_inodes_in_group(sb, bgid);
     
    230230                        rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
    231231                            0, &index_in_group, inodes_in_group);
    232                        
     232
    233233                        /* Block group has not any free i-node */
    234234                        if (rc == ENOSPC) {
     
    246246                                continue;
    247247                        }
    248                        
     248
    249249                        /* Free i-node found, save the bitmap */
    250250                        bitmap_block->dirty = true;
    251                        
     251
    252252                        rc = block_put(bitmap_block);
    253253                        if (rc != EOK) {
     
    255255                                return rc;
    256256                        }
    257                        
     257
    258258                        /* Modify filesystem counters */
    259259                        free_inodes--;
    260260                        ext4_block_group_set_free_inodes_count(bg, sb, free_inodes);
    261                        
     261
    262262                        /* Increment used directories counter */
    263263                        if (is_dir) {
     
    265265                                ext4_block_group_set_used_dirs_count(bg, sb, used_dirs);
    266266                        }
    267                        
     267
    268268                        /* Decrease unused inodes count */
    269269                        if (ext4_block_group_has_flag(bg,
     
    271271                                uint32_t unused =
    272272                                    ext4_block_group_get_itable_unused(bg, sb);
    273                                
     273
    274274                                uint32_t inodes_in_group =
    275275                                    ext4_superblock_get_inodes_in_group(sb, bgid);
    276                                
     276
    277277                                uint32_t free = inodes_in_group - unused;
    278                                
     278
    279279                                if (index_in_group >= free) {
    280280                                        unused = inodes_in_group - (index_in_group + 1);
     
    282282                                }
    283283                        }
    284                        
     284
    285285                        /* Save modified block group */
    286286                        bg_ref->dirty = true;
    287                        
     287
    288288                        rc = ext4_filesystem_put_block_group_ref(bg_ref);
    289289                        if (rc != EOK)
    290290                                return rc;
    291                        
     291
    292292                        /* Update superblock */
    293293                        sb_free_inodes--;
    294294                        ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
    295                        
     295
    296296                        /* Compute the absolute i-nodex number */
    297297                        *index = ext4_ialloc_index_in_group2inode(sb, index_in_group, bgid);
    298                        
     298
    299299                        return EOK;
    300300                }
    301                
     301
    302302                /* Block group not modified, put it and jump to the next block group */
    303303                rc = ext4_filesystem_put_block_group_ref(bg_ref);
     
    307307                ++bgid;
    308308        }
    309        
     309
    310310        return ENOSPC;
    311311}
  • uspace/lib/ext4/src/inode.c

    r3061bc1 ra35b458  
    5353        uint32_t bits = 8;
    5454        uint32_t size = block_size;
    55        
     55
    5656        do {
    5757                bits++;
    5858                size = size >> 1;
    5959        } while (size > 256);
    60        
     60
    6161        return bits;
    6262}
     
    7676                    ((uint32_t) uint16_t_le2host(inode->mode));
    7777        }
    78        
     78
    7979        return uint16_t_le2host(inode->mode);
    8080}
     
    9090{
    9191        inode->mode = host2uint16_t_le((mode << 16) >> 16);
    92        
     92
    9393        if (ext4_superblock_get_creator_os(sb) == EXT4_SUPERBLOCK_OS_HURD)
    9494                inode->osd2.hurd2.mode_high = host2uint16_t_le(mode >> 16);
     
    129129{
    130130        uint32_t major_rev = ext4_superblock_get_rev_level(sb);
    131        
     131
    132132        if ((major_rev > 0) &&
    133133            (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)))
    134134                return ((uint64_t)uint32_t_le2host(inode->size_hi)) << 32 |
    135135                    ((uint64_t)uint32_t_le2host(inode->size_lo));
    136        
     136
    137137        return uint32_t_le2host(inode->size_lo);
    138138}
     
    304304                    uint16_t_le2host(inode->osd2.linux2.blocks_high)) << 32 |
    305305                    uint32_t_le2host(inode->blocks_count_lo);
    306                
     306
    307307                if (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_HUGE_FILE)) {
    308308                        uint32_t block_size = ext4_superblock_get_block_size(sb);
     
    330330        uint64_t max = 0;
    331331        max = ~max >> 32;
    332        
     332
    333333        if (count <= max) {
    334334                inode->blocks_count_lo = host2uint32_t_le(count);
    335335                inode->osd2.linux2.blocks_high = 0;
    336336                ext4_inode_clear_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
    337                
     337
    338338                return EOK;
    339339        }
    340        
     340
    341341        /* Check if there can be used huge files (many blocks) */
    342342        if (!ext4_superblock_has_feature_read_only(sb,
    343343            EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
    344344                return EINVAL;
    345        
     345
    346346        /* 48-bit maximum */
    347347        max = 0;
    348348        max = ~max >> 16;
    349        
     349
    350350        if (count <= max) {
    351351                inode->blocks_count_lo = host2uint32_t_le(count);
     
    360360                inode->osd2.linux2.blocks_high = host2uint16_t_le(count >> 32);
    361361        }
    362        
     362
    363363        return EOK;
    364364}
     
    424424                    uint16_t_le2host(inode->osd2.linux2.file_acl_high)) << 16 |
    425425                    (uint32_t_le2host(inode->file_acl_lo));
    426        
     426
    427427        return uint32_t_le2host(inode->file_acl_lo);
    428428}
     
    439439{
    440440        inode->file_acl_lo = host2uint32_t_le((file_acl << 32) >> 32);
    441        
     441
    442442        if (ext4_superblock_get_creator_os(sb) == EXT4_SUPERBLOCK_OS_LINUX)
    443443                inode->osd2.linux2.file_acl_high = host2uint16_t_le(file_acl >> 32);
     
    455455{
    456456        assert(idx < EXT4_INODE_DIRECT_BLOCK_COUNT);
    457        
     457
    458458        return uint32_t_le2host(inode->blocks[idx]);
    459459}
     
    469469{
    470470        assert(idx < EXT4_INODE_DIRECT_BLOCK_COUNT);
    471        
     471
    472472        inode->blocks[idx] = host2uint32_t_le(fblock);
    473473}
     
    540540        if (ext4_inode_get_flags(inode) & flag)
    541541                return true;
    542        
     542
    543543        return false;
    544544}
     
    583583            (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_IMMUTABLE)))
    584584                return false;
    585        
     585
    586586        if ((ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)) ||
    587587            (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_DIRECTORY)))
    588588                return true;
    589        
     589
    590590        return false;
    591591}
  • uspace/lib/ext4/src/ops.c

    r3061bc1 ra35b458  
    117117        node_key_t *key = (node_key_t *)key_arg;
    118118        ext4_node_t *enode = hash_table_get_inst(item, ext4_node_t, link);
    119        
     119
    120120        return key->service_id == enode->instance->service_id
    121121                && key->index == enode->inode_ref->index;
     
    142142        if (!hash_table_create(&open_nodes, 0, 0, &open_nodes_ops))
    143143                return ENOMEM;
    144        
     144
    145145        return EOK;
    146146}
     
    173173{
    174174        fibril_mutex_lock(&instance_list_mutex);
    175        
     175
    176176        if (list_empty(&instance_list)) {
    177177                fibril_mutex_unlock(&instance_list_mutex);
    178178                return EINVAL;
    179179        }
    180        
     180
    181181        list_foreach(instance_list, link, ext4_instance_t, tmp) {
    182182                if (tmp->service_id == service_id) {
     
    186186                }
    187187        }
    188        
     188
    189189        fibril_mutex_unlock(&instance_list_mutex);
    190190        return EINVAL;
     
    219219        ext4_node_t *eparent = EXT4_NODE(pfn);
    220220        ext4_filesystem_t *fs = eparent->instance->filesystem;
    221        
     221
    222222        if (!ext4_inode_is_type(fs->superblock, eparent->inode_ref->inode,
    223223            EXT4_INODE_MODE_DIRECTORY))
    224224                return ENOTDIR;
    225        
     225
    226226        /* Try to find entry */
    227227        ext4_directory_search_result_t result;
     
    233233                        return EOK;
    234234                }
    235                
    236                 return rc;
    237         }
    238        
     235
     236                return rc;
     237        }
     238
    239239        /* Load node from search result */
    240240        uint32_t inode = ext4_directory_entry_ll_get_inode(result.dentry);
     
    268268        if (rc != EOK)
    269269                return rc;
    270        
     270
    271271        return ext4_node_get_core(rfn, inst, index);
    272272}
     
    285285{
    286286        fibril_mutex_lock(&open_nodes_lock);
    287        
     287
    288288        /* Check if the node is not already open */
    289289        node_key_t key = {
     
    291291                .index = index
    292292        };
    293        
     293
    294294        ht_link_t *already_open = hash_table_find(&open_nodes, &key);
    295295        ext4_node_t *enode = NULL;
     
    298298                *rfn = enode->fs_node;
    299299                enode->references++;
    300                
     300
    301301                fibril_mutex_unlock(&open_nodes_lock);
    302302                return EOK;
    303303        }
    304        
     304
    305305        /* Prepare new enode */
    306306        enode = malloc(sizeof(ext4_node_t));
     
    309309                return ENOMEM;
    310310        }
    311        
     311
    312312        /* Prepare new fs_node and initialize */
    313313        fs_node_t *fs_node = malloc(sizeof(fs_node_t));
     
    317317                return ENOMEM;
    318318        }
    319        
     319
    320320        fs_node_initialize(fs_node);
    321        
     321
    322322        /* Load i-node from filesystem */
    323323        ext4_inode_ref_t *inode_ref;
     
    330330                return rc;
    331331        }
    332        
     332
    333333        /* Initialize enode */
    334334        enode->inode_ref = inode_ref;
     
    336336        enode->references = 1;
    337337        enode->fs_node = fs_node;
    338        
     338
    339339        fs_node->data = enode;
    340340        *rfn = fs_node;
    341        
     341
    342342        hash_table_insert(&open_nodes, &enode->link);
    343343        inst->open_nodes_count++;
    344        
     344
    345345        fibril_mutex_unlock(&open_nodes_lock);
    346        
     346
    347347        return EOK;
    348348}
     
    360360        assert(enode->instance->open_nodes_count > 0);
    361361        enode->instance->open_nodes_count--;
    362        
     362
    363363        /* Put inode back in filesystem */
    364364        errno_t rc = ext4_filesystem_put_inode_ref(enode->inode_ref);
    365365        if (rc != EOK)
    366366                return rc;
    367        
     367
    368368        /* Destroy data structure */
    369369        free(enode->fs_node);
    370370        free(enode);
    371        
     371
    372372        return EOK;
    373373}
     
    399399{
    400400        fibril_mutex_lock(&open_nodes_lock);
    401        
     401
    402402        ext4_node_t *enode = EXT4_NODE(fn);
    403403        assert(enode->references > 0);
     
    410410                }
    411411        }
    412        
     412
    413413        fibril_mutex_unlock(&open_nodes_lock);
    414        
     414
    415415        return EOK;
    416416}
     
    432432        if (enode == NULL)
    433433                return ENOMEM;
    434        
     434
    435435        /* Allocate fs_node */
    436436        fs_node_t *fs_node;
     
    440440                return ENOMEM;
    441441        }
    442        
     442
    443443        /* Load instance */
    444444        ext4_instance_t *inst;
     
    449449                return rc;
    450450        }
    451        
     451
    452452        /* Allocate new i-node in filesystem */
    453453        ext4_inode_ref_t *inode_ref;
     
    458458                return rc;
    459459        }
    460        
     460
    461461        /* Do some interconnections in references */
    462462        enode->inode_ref = inode_ref;
    463463        enode->instance = inst;
    464464        enode->references = 1;
    465        
     465
    466466        fibril_mutex_lock(&open_nodes_lock);
    467467        hash_table_insert(&open_nodes, &enode->link);
    468468        fibril_mutex_unlock(&open_nodes_lock);
    469469        inst->open_nodes_count++;
    470        
     470
    471471        enode->inode_ref->dirty = true;
    472        
     472
    473473        fs_node_initialize(fs_node);
    474474        fs_node->data = enode;
    475475        enode->fs_node = fs_node;
    476476        *rfn = fs_node;
    477        
     477
    478478        return EOK;
    479479}
     
    495495                return rc;
    496496        }
    497        
     497
    498498        if (has_children) {
    499499                ext4_node_put(fn);
    500500                return EINVAL;
    501501        }
    502        
     502
    503503        ext4_node_t *enode = EXT4_NODE(fn);
    504504        ext4_inode_ref_t *inode_ref = enode->inode_ref;
    505        
     505
    506506        /* Release data blocks */
    507507        rc = ext4_filesystem_truncate_inode(inode_ref, 0);
     
    510510                return rc;
    511511        }
    512        
     512
    513513        /*
    514514         * TODO: Sset real deletion time when it will be supported.
     
    517517        ext4_inode_set_deletion_time(inode_ref->inode, 0xdeadbeef);
    518518        inode_ref->dirty = true;
    519        
     519
    520520        /* Free inode */
    521521        rc = ext4_filesystem_free_inode(inode_ref);
     
    524524                return rc;
    525525        }
    526        
     526
    527527        return ext4_node_put(fn);
    528528}
     
    542542        if (str_size(name) > EXT4_DIRECTORY_FILENAME_LEN)
    543543                return ENAMETOOLONG;
    544        
     544
    545545        ext4_node_t *parent = EXT4_NODE(pfn);
    546546        ext4_node_t *child = EXT4_NODE(cfn);
    547547        ext4_filesystem_t *fs = parent->instance->filesystem;
    548        
     548
    549549        /* Add entry to parent directory */
    550550        errno_t rc = ext4_directory_add_entry(parent->inode_ref, name,
     
    552552        if (rc != EOK)
    553553                return rc;
    554        
     554
    555555        /* Fill new dir -> add '.' and '..' entries */
    556556        if (ext4_inode_is_type(fs->superblock, child->inode_ref->inode,
     
    562562                        return rc;
    563563                }
    564                
     564
    565565                rc = ext4_directory_add_entry(child->inode_ref, "..",
    566566                    parent->inode_ref);
     
    570570                        return rc;
    571571                }
    572                
     572
    573573                /* Initialize directory index if supported */
    574574                if (ext4_superblock_has_feature_compatible(fs->superblock,
     
    577577                        if (rc != EOK)
    578578                                return rc;
    579                        
     579
    580580                        ext4_inode_set_flag(child->inode_ref->inode,
    581581                            EXT4_INODE_FLAG_INDEX);
    582582                        child->inode_ref->dirty = true;
    583583                }
    584        
     584
    585585                uint16_t parent_links =
    586586                    ext4_inode_get_links_count(parent->inode_ref->inode);
    587587                parent_links++;
    588588                ext4_inode_set_links_count(parent->inode_ref->inode, parent_links);
    589                
     589
    590590                parent->inode_ref->dirty = true;
    591591        }
    592        
     592
    593593        uint16_t child_links =
    594594            ext4_inode_get_links_count(child->inode_ref->inode);
    595595        child_links++;
    596596        ext4_inode_set_links_count(child->inode_ref->inode, child_links);
    597        
     597
    598598        child->inode_ref->dirty = true;
    599        
     599
    600600        return EOK;
    601601}
     
    616616        if (rc != EOK)
    617617                return rc;
    618        
     618
    619619        /* Cannot unlink non-empty node */
    620620        if (has_children)
    621621                return ENOTEMPTY;
    622        
     622
    623623        /* Remove entry from parent directory */
    624624        ext4_inode_ref_t *parent = EXT4_NODE(pfn)->inode_ref;
     
    626626        if (rc != EOK)
    627627                return rc;
    628        
     628
    629629        /* Decrement links count */
    630630        ext4_inode_ref_t *child_inode_ref = EXT4_NODE(cfn)->inode_ref;
    631        
     631
    632632        uint32_t lnk_count =
    633633            ext4_inode_get_links_count(child_inode_ref->inode);
    634634        lnk_count--;
    635        
     635
    636636        /* If directory - handle links from parent */
    637637        if ((lnk_count <= 1) && (ext4_is_directory(cfn))) {
    638638                assert(lnk_count == 1);
    639                
     639
    640640                lnk_count--;
    641                
     641
    642642                ext4_inode_ref_t *parent_inode_ref = EXT4_NODE(pfn)->inode_ref;
    643                
     643
    644644                uint32_t parent_lnk_count = ext4_inode_get_links_count(
    645645                    parent_inode_ref->inode);
    646                
     646
    647647                parent_lnk_count--;
    648648                ext4_inode_set_links_count(parent_inode_ref->inode, parent_lnk_count);
    649                
     649
    650650                parent->dirty = true;
    651651        }
     
    659659         * parent->dirty = true;
    660660         */
    661        
     661
    662662        /*
    663663         * TODO: Update timestamp for inode.
     
    666666         *     (uint32_t) now);
    667667         */
    668        
     668
    669669        ext4_inode_set_links_count(child_inode_ref->inode, lnk_count);
    670670        child_inode_ref->dirty = true;
    671        
     671
    672672        return EOK;
    673673}
     
    687687        ext4_node_t *enode = EXT4_NODE(fn);
    688688        ext4_filesystem_t *fs = enode->instance->filesystem;
    689        
     689
    690690        /* Check if node is directory */
    691691        if (!ext4_inode_is_type(fs->superblock, enode->inode_ref->inode,
     
    694694                return EOK;
    695695        }
    696        
     696
    697697        ext4_directory_iterator_t it;
    698698        errno_t rc = ext4_directory_iterator_init(&it, enode->inode_ref, 0);
    699699        if (rc != EOK)
    700700                return rc;
    701        
     701
    702702        /* Find a non-empty directory entry */
    703703        bool found = false;
     
    712712                        }
    713713                }
    714                
     714
    715715                rc = ext4_directory_iterator_next(&it);
    716716                if (rc != EOK) {
     
    719719                }
    720720        }
    721        
     721
    722722        rc = ext4_directory_iterator_fini(&it);
    723723        if (rc != EOK)
    724724                return rc;
    725        
     725
    726726        *has_children = found;
    727        
     727
    728728        return EOK;
    729729}
     
    767767        ext4_node_t *enode = EXT4_NODE(fn);
    768768        uint32_t lnkcnt = ext4_inode_get_links_count(enode->inode_ref->inode);
    769        
     769
    770770        if (ext4_is_directory(fn)) {
    771771                if (lnkcnt > 1)
     
    774774                        return 0;
    775775        }
    776        
     776
    777777        /* For regular files return real links count */
    778778        return lnkcnt;
     
    790790        ext4_node_t *enode = EXT4_NODE(fn);
    791791        ext4_superblock_t *sb = enode->instance->filesystem->superblock;
    792        
     792
    793793        return ext4_inode_is_type(sb, enode->inode_ref->inode,
    794794            EXT4_INODE_MODE_DIRECTORY);
     
    806806        ext4_node_t *enode = EXT4_NODE(fn);
    807807        ext4_superblock_t *sb = enode->instance->filesystem->superblock;
    808        
     808
    809809        return ext4_inode_is_type(sb, enode->inode_ref->inode,
    810810            EXT4_INODE_MODE_FILE);
     
    928928{
    929929        ext4_filesystem_t *fs;
    930        
     930
    931931        /* Allocate instance structure */
    932932        ext4_instance_t *inst = (ext4_instance_t *)
     
    934934        if (inst == NULL)
    935935                return ENOMEM;
    936        
     936
    937937        enum cache_mode cmode;
    938938        if (str_cmp(opts, "wtcache") == 0)
     
    940940        else
    941941                cmode = CACHE_MODE_WB;
    942        
     942
    943943        /* Initialize instance */
    944944        link_initialize(&inst->link);
    945945        inst->service_id = service_id;
    946946        inst->open_nodes_count = 0;
    947        
     947
    948948        /* Initialize the filesystem */
    949949        aoff64_t rnsize;
     
    953953                return rc;
    954954        }
    955        
     955
    956956        /* Add instance to the list */
    957957        fibril_mutex_lock(&instance_list_mutex);
    958958        list_append(&inst->link, &instance_list);
    959959        fibril_mutex_unlock(&instance_list_mutex);
    960        
     960
    961961        *index = EXT4_INODE_ROOT_INDEX;
    962962        *size = rnsize;
    963        
     963
    964964        return EOK;
    965965}
     
    980980        if (rc != EOK)
    981981                return rc;
    982        
     982
    983983        fibril_mutex_lock(&open_nodes_lock);
    984        
     984
    985985        if (inst->open_nodes_count != 0) {
    986986                fibril_mutex_unlock(&open_nodes_lock);
    987987                return EBUSY;
    988988        }
    989        
     989
    990990        /* Remove the instance from the list */
    991991        fibril_mutex_lock(&instance_list_mutex);
    992992        list_remove(&inst->link);
    993993        fibril_mutex_unlock(&instance_list_mutex);
    994        
     994
    995995        fibril_mutex_unlock(&open_nodes_lock);
    996        
     996
    997997        rc = ext4_filesystem_close(inst->filesystem);
    998998        if (rc != EOK) {
     
    10281028                return EINVAL;
    10291029        }
    1030        
     1030
    10311031        ext4_instance_t *inst;
    10321032        errno_t rc = ext4_instance_get(service_id, &inst);
     
    10351035                return rc;
    10361036        }
    1037        
     1037
    10381038        /* Load i-node */
    10391039        ext4_inode_ref_t *inode_ref;
     
    10431043                return rc;
    10441044        }
    1045        
     1045
    10461046        /* Read from i-node by type */
    10471047        if (ext4_inode_is_type(inst->filesystem->superblock, inode_ref->inode,
     
    10581058                rc = ENOTSUP;
    10591059        }
    1060        
     1060
    10611061        errno_t const rc2 = ext4_filesystem_put_inode_ref(inode_ref);
    1062        
     1062
    10631063        return rc == EOK ? rc2 : rc;
    10641064}
     
    10761076        if ((name_size == 1) && (name[0] == '.'))
    10771077                return true;
    1078        
     1078
    10791079        if ((name_size == 2) && (name[0] == '.') && (name[1] == '.'))
    10801080                return true;
    1081        
     1081
    10821082        return false;
    10831083}
     
    11041104                return rc;
    11051105        }
    1106        
     1106
    11071107        /*
    11081108         * Find next interesting directory entry.
     
    11141114                if (it.current->inode == 0)
    11151115                        goto skip;
    1116                
     1116
    11171117                uint16_t name_size = ext4_directory_entry_ll_get_name_length(
    11181118                    inst->filesystem->superblock, it.current);
    1119                
     1119
    11201120                /* Skip . and .. */
    11211121                if (ext4_is_dots(it.current->name, name_size))
    11221122                        goto skip;
    1123                
     1123
    11241124                /*
    11251125                 * The on-disk entry does not contain \0 at the end
     
    11331133                        return ENOMEM;
    11341134                }
    1135                
     1135
    11361136                memcpy(buf, &it.current->name, name_size);
    11371137                *(buf + name_size) = 0;
    11381138                found = true;
    1139                
     1139
    11401140                (void) async_data_read_finalize(callid, buf, name_size + 1);
    11411141                free(buf);
    11421142                break;
    1143                
     1143
    11441144skip:
    11451145                rc = ext4_directory_iterator_next(&it);
     
    11501150                }
    11511151        }
    1152        
     1152
    11531153        uint64_t next;
    11541154        if (found) {
     
    11561156                if (rc != EOK)
    11571157                        return rc;
    1158                
     1158
    11591159                next = it.current_offset;
    11601160        }
    1161        
     1161
    11621162        rc = ext4_directory_iterator_fini(&it);
    11631163        if (rc != EOK)
    11641164                return rc;
    1165        
     1165
    11661166        /* Prepare return values */
    11671167        if (found) {
     
    11911191        ext4_superblock_t *sb = inst->filesystem->superblock;
    11921192        uint64_t file_size = ext4_inode_get_size(sb, inode_ref->inode);
    1193        
     1193
    11941194        if (pos >= file_size) {
    11951195                /* Read 0 bytes successfully */
     
    11981198                return EOK;
    11991199        }
    1200        
     1200
    12011201        /* For now, we only read data from one block at a time */
    12021202        uint32_t block_size = ext4_superblock_get_block_size(sb);
     
    12041204        uint32_t offset_in_block = pos % block_size;
    12051205        uint32_t bytes = min(block_size - offset_in_block, size);
    1206        
     1206
    12071207        /* Handle end of file */
    12081208        if (pos + bytes > file_size)
    12091209                bytes = file_size - pos;
    1210        
     1210
    12111211        /* Get the real block number */
    12121212        uint32_t fs_block;
     
    12171217                return rc;
    12181218        }
    1219        
     1219
    12201220        /*
    12211221         * Check for sparse file.
     
    12311231                        return ENOMEM;
    12321232                }
    1233                
     1233
    12341234                memset(buffer, 0, bytes);
    1235                
     1235
    12361236                rc = async_data_read_finalize(callid, buffer, bytes);
    12371237                *rbytes = bytes;
    1238                
     1238
    12391239                free(buffer);
    12401240                return rc;
    12411241        }
    1242        
     1242
    12431243        /* Usual case - we need to read a block from device */
    12441244        block_t *block;
     
    12481248                return rc;
    12491249        }
    1250        
     1250
    12511251        assert(offset_in_block + bytes <= block_size);
    12521252        rc = async_data_read_finalize(callid, block->data + offset_in_block, bytes);
     
    12551255                return rc;
    12561256        }
    1257        
     1257
    12581258        rc = block_put(block);
    12591259        if (rc != EOK)
    12601260                return rc;
    1261        
     1261
    12621262        *rbytes = bytes;
    12631263        return EOK;
     
    12821282        if (rc != EOK)
    12831283                return rc;
    1284        
     1284
    12851285        ipc_callid_t callid;
    12861286        size_t len;
     
    12901290                goto exit;
    12911291        }
    1292        
     1292
    12931293        ext4_node_t *enode = EXT4_NODE(fn);
    12941294        ext4_filesystem_t *fs = enode->instance->filesystem;
    1295        
     1295
    12961296        uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
    1297        
     1297
    12981298        /* Prevent writing to more than one block */
    12991299        uint32_t bytes = min(len, block_size - (pos % block_size));
    1300        
     1300
    13011301        int flags = BLOCK_FLAGS_NONE;
    13021302        if (bytes == block_size)
    13031303                flags = BLOCK_FLAGS_NOREAD;
    1304        
     1304
    13051305        uint32_t iblock =  pos / block_size;
    13061306        uint32_t fblock;
    1307        
     1307
    13081308        /* Load inode */
    13091309        ext4_inode_ref_t *inode_ref = enode->inode_ref;
     
    13141314                goto exit;
    13151315        }
    1316        
     1316
    13171317        /* Check for sparse file */
    13181318        if (fblock == 0) {
     
    13231323                            ext4_inode_get_size(fs->superblock, inode_ref->inode) /
    13241324                            block_size;
    1325                        
     1325
    13261326                        while (last_iblock < iblock) {
    13271327                                rc = ext4_extent_append_block(inode_ref, &last_iblock,
     
    13321332                                }
    13331333                        }
    1334                        
     1334
    13351335                        rc = ext4_extent_append_block(inode_ref, &last_iblock,
    13361336                            &fblock, false);
     
    13451345                                goto exit;
    13461346                        }
    1347                        
     1347
    13481348                        rc = ext4_filesystem_set_inode_data_block_index(inode_ref,
    13491349                            iblock, fblock);
     
    13541354                        }
    13551355                }
    1356                
     1356
    13571357                flags = BLOCK_FLAGS_NOREAD;
    13581358                inode_ref->dirty = true;
    13591359        }
    1360        
     1360
    13611361        /* Load target block */
    13621362        block_t *write_block;
     
    13661366                goto exit;
    13671367        }
    1368        
     1368
    13691369        if (flags == BLOCK_FLAGS_NOREAD)
    13701370                memset(write_block->data, 0, block_size);
     
    14191419        if (rc != EOK)
    14201420                return rc;
    1421        
     1421
    14221422        ext4_node_t *enode = EXT4_NODE(fn);
    14231423        ext4_inode_ref_t *inode_ref = enode->inode_ref;
    1424        
     1424
    14251425        rc = ext4_filesystem_truncate_inode(inode_ref, new_size);
    14261426        errno_t const rc2 = ext4_node_put(fn);
    1427        
     1427
    14281428        return rc == EOK ? rc2 : rc;
    14291429}
     
    14561456        if (rc != EOK)
    14571457                return rc;
    1458        
     1458
    14591459        /* Destroy the inode */
    14601460        return ext4_destroy_node(fn);
     
    14731473        if (rc != EOK)
    14741474                return rc;
    1475        
     1475
    14761476        ext4_node_t *enode = EXT4_NODE(fn);
    14771477        enode->inode_ref->dirty = true;
    1478        
     1478
    14791479        return ext4_node_put(fn);
    14801480}
  • uspace/lib/ext4/src/superblock.c

    r3061bc1 ra35b458  
    241241        uint32_t log = 0;
    242242        uint32_t tmp = size / EXT4_MIN_BLOCK_SIZE;
    243        
     243
    244244        tmp >>= 1;
    245245        while (tmp) {
     
    247247                tmp >>= 1;
    248248        }
    249        
     249
    250250        ext4_superblock_set_log_block_size(sb, log);
    251251}
     
    297297        uint32_t log = 0;
    298298        uint32_t tmp = size / EXT4_MIN_BLOCK_SIZE;
    299        
     299
    300300        tmp >>= 1;
    301301        while (tmp) {
     
    303303                tmp >>= 1;
    304304        }
    305        
     305
    306306        ext4_superblock_set_log_frag_size(sb, log);
    307307}
     
    736736        if (ext4_superblock_get_rev_level(sb) == 0)
    737737                return EXT4_REV0_INODE_SIZE;
    738        
     738
    739739        return uint16_t_le2host(sb->inode_size);
    740740}
     
    10011001{
    10021002        uint16_t size = uint16_t_le2host(sb->desc_size);
    1003        
     1003
    10041004        if (size < EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
    10051005                size = EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE;
    1006        
     1006
    10071007        return size;
    10081008}
     
    10211021                sb->desc_size =
    10221022                    host2uint16_t_le(EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE);
    1023        
     1023
    10241024        sb->desc_size = host2uint16_t_le(size);
    10251025}
     
    10641064        if (ext4_superblock_get_flags(sb) & flag)
    10651065                return true;
    1066        
     1066
    10671067        return false;
    10681068}
     
    10811081        if (ext4_superblock_get_features_compatible(sb) & feature)
    10821082                return true;
    1083        
     1083
    10841084        return false;
    10851085}
     
    10981098        if (ext4_superblock_get_features_incompatible(sb) & feature)
    10991099                return true;
    1100        
     1100
    11011101        return false;
    11021102}
     
    11151115        if (ext4_superblock_get_features_read_only(sb) & feature)
    11161116                return true;
    1117        
     1117
    11181118        return false;
    11191119}
     
    11331133        if (data == NULL)
    11341134                return ENOMEM;
    1135        
     1135
    11361136        /* Read data from block device */
    11371137        errno_t rc = block_read_bytes_direct(service_id, EXT4_SUPERBLOCK_OFFSET,
    11381138            EXT4_SUPERBLOCK_SIZE, data);
    1139        
     1139
    11401140        if (rc != EOK) {
    11411141                free(data);
    11421142                return rc;
    11431143        }
    1144        
     1144
    11451145        /* Set output value */
    11461146        (*sb) = data;
    1147        
     1147
    11481148        return EOK;
    11491149}
     
    11641164        if (rc != EOK)
    11651165                return rc;
    1166        
     1166
    11671167        /* Compute address of the first block */
    11681168        uint64_t first_block = EXT4_SUPERBLOCK_OFFSET / phys_block_size;
    1169        
     1169
    11701170        /* Compute number of block to write */
    11711171        size_t block_count = EXT4_SUPERBLOCK_SIZE / phys_block_size;
    1172        
     1172
    11731173        /* Check alignment */
    11741174        if (EXT4_SUPERBLOCK_SIZE % phys_block_size)
    11751175                block_count++;
    1176        
     1176
    11771177        /* Write data */
    11781178        return block_write_direct(service_id, first_block, block_count, sb);
     
    12031203        if (ext4_superblock_get_magic(sb) != EXT4_SUPERBLOCK_MAGIC)
    12041204                return ENOTSUP;
    1205        
     1205
    12061206        if (ext4_superblock_get_inodes_count(sb) == 0)
    12071207                return ENOTSUP;
    1208        
     1208
    12091209        if (ext4_superblock_get_blocks_count(sb) == 0)
    12101210                return ENOTSUP;
    1211        
     1211
    12121212        if (ext4_superblock_get_blocks_per_group(sb) == 0)
    12131213                return ENOTSUP;
    1214        
     1214
    12151215        if (ext4_superblock_get_inodes_per_group(sb) == 0)
    12161216                return ENOTSUP;
    1217        
     1217
    12181218        if (ext4_superblock_get_inode_size(sb) < 128)
    12191219                return ENOTSUP;
    1220        
     1220
    12211221        if (ext4_superblock_get_first_inode(sb) < 11)
    12221222                return ENOTSUP;
    1223        
     1223
    12241224        if (ext4_superblock_get_desc_size(sb) <
    12251225            EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
    12261226                return ENOTSUP;
    1227        
     1227
    12281228        if (ext4_superblock_get_desc_size(sb) >
    12291229            EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE)
    12301230                return ENOTSUP;
    1231        
     1231
    12321232        return EOK;
    12331233}
     
    12441244        uint64_t blocks_count = ext4_superblock_get_blocks_count(sb);
    12451245        uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
    1246        
     1246
    12471247        uint32_t block_groups_count = blocks_count / blocks_per_group;
    1248        
     1248
    12491249        if (blocks_count % blocks_per_group)
    12501250                block_groups_count++;
    1251        
     1251
    12521252        return block_groups_count;
    12531253}
     
    12691269        uint64_t total_blocks =
    12701270            ext4_superblock_get_blocks_count(sb);
    1271        
     1271
    12721272        if (bgid < block_group_count - 1)
    12731273                return blocks_per_group;
     
    12921292        uint32_t total_inodes =
    12931293            ext4_superblock_get_inodes_count(sb);
    1294        
     1294
    12951295        if (bgid < block_group_count - 1)
    12961296                return inodes_per_group;
Note: See TracChangeset for help on using the changeset viewer.