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


Ignore:
Timestamp:
2017-05-12T17:15:49Z (7 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
395df52
Parents:
be39fc6
Message:

Restructure ext4 filesystem opening/closing.

File:
1 edited

Legend:

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

    rbe39fc6 rde5b708  
    5656static int ext4_filesystem_check_features(ext4_filesystem_t *, bool *);
    5757
    58 /** Initialize filesystem and read all needed data.
     58/** Initialize filesystem for opening.
     59 *
     60 * But do not mark mounted just yet.
    5961 *
    6062 * @param fs         Filesystem instance to be initialized
    6163 * @param service_id Identifier if device with the filesystem
    6264 * @param cmode      Cache mode
    63  * @param read_only  Place to store read only flag
    64  * @param index      Output value - index of root node
    65  * @param size       Output value - size of root node
    66  * @param lnkcnt     Output value - link count of root node
    6765 *
    6866 * @return Error code
    6967 *
    7068 */
    71 int ext4_filesystem_init(ext4_filesystem_t *fs, ext4_instance_t *inst,
    72     service_id_t service_id, enum cache_mode cmode, aoff64_t *size)
     69static int ext4_filesystem_init(ext4_filesystem_t *fs, service_id_t service_id,
     70    enum cache_mode cmode)
    7371{
    7472        int rc;
     
    122120                goto err_2;
    123121        }
    124        
     122
    125123        rc = ext4_superblock_check_sanity(fs->superblock);
    126124        if (rc != EOK)
     
    133131                goto err_2;
    134132
    135         /* Read root node */
    136         fs_node_t *root_node;
    137         rc = ext4_node_get_core(&root_node, inst, EXT4_INODE_ROOT_INDEX);
    138         if (rc != EOK)
    139                 goto err_2;
    140 
    141         /* Mark system as mounted */
    142         ext4_superblock_set_state(fs->superblock, EXT4_SUPERBLOCK_STATE_ERROR_FS);
    143         rc = ext4_superblock_write_direct(fs->device, fs->superblock);
    144         if (rc != EOK)
    145                 goto err_3;
    146 
    147         uint16_t mnt_count = ext4_superblock_get_mount_count(fs->superblock);
    148         ext4_superblock_set_mount_count(fs->superblock, mnt_count + 1);
    149 
    150         ext4_node_t *enode = EXT4_NODE(root_node);
    151        
    152         *size = ext4_inode_get_size(fs->superblock, enode->inode_ref->inode);
    153 
    154         ext4_node_put(root_node);
    155133        return EOK;
    156 err_3:
    157         ext4_node_put(root_node);
    158134err_2:
    159135        block_cache_fini(fs->device);
     
    166142}
    167143
    168 /** Destroy filesystem instance (used by unmount operation).
     144/** Finalize filesystem.
     145 *
     146 * @param fs Filesystem to be finalized
     147 *
     148 */
     149static void ext4_filesystem_fini(ext4_filesystem_t *fs)
     150{
     151        /* Release memory space for superblock */
     152        free(fs->superblock);
     153
     154        /* Finish work with block library */
     155        block_cache_fini(fs->device);
     156        block_fini(fs->device);
     157}
     158
     159/** Open filesystem and read all needed data.
     160 *
     161 * @param fs         Filesystem to be initialized
     162 * @param inst       Instance
     163 * @param service_id Identifier if device with the filesystem
     164 * @param cmode      Cache mode
     165 * @param size       Output value - size of root node
     166 *
     167 * @return Error code
     168 *
     169 */
     170int ext4_filesystem_open(ext4_instance_t *inst, service_id_t service_id,
     171    enum cache_mode cmode, aoff64_t *size, ext4_filesystem_t **rfs)
     172{
     173        ext4_filesystem_t *fs = NULL;
     174        fs_node_t *root_node = NULL;
     175        int rc;
     176
     177        fs = calloc(1, sizeof(ext4_filesystem_t));
     178        if (fs == NULL) {
     179                rc = ENOMEM;
     180                goto error;
     181        }
     182
     183        inst->filesystem = fs;
     184
     185        /* Initialize the file system for opening */
     186        rc = ext4_filesystem_init(fs, service_id, cmode);
     187        if (rc != EOK)
     188                goto error;
     189
     190        /* Read root node */
     191        rc = ext4_node_get_core(&root_node, inst, EXT4_INODE_ROOT_INDEX);
     192        if (rc != EOK)
     193                goto error;
     194
     195        /* Mark system as mounted */
     196        ext4_superblock_set_state(fs->superblock, EXT4_SUPERBLOCK_STATE_ERROR_FS);
     197        rc = ext4_superblock_write_direct(fs->device, fs->superblock);
     198        if (rc != EOK)
     199                goto error;
     200
     201        uint16_t mnt_count = ext4_superblock_get_mount_count(fs->superblock);
     202        ext4_superblock_set_mount_count(fs->superblock, mnt_count + 1);
     203
     204        ext4_node_t *enode = EXT4_NODE(root_node);
     205
     206        *size = ext4_inode_get_size(fs->superblock, enode->inode_ref->inode);
     207
     208        ext4_node_put(root_node);
     209        *rfs = fs;
     210        return EOK;
     211error:
     212        if (root_node != NULL)
     213                ext4_node_put(root_node);
     214
     215        if (fs != NULL) {
     216                ext4_filesystem_fini(fs);
     217                free(fs);
     218        }
     219
     220        return rc;
     221}
     222
     223/** Close filesystem.
    169224 *
    170225 * @param fs Filesystem to be destroyed
    171226 *
    172  * @return Error code
    173  *
    174  */
    175 int ext4_filesystem_fini(ext4_filesystem_t *fs)
     227 * @return EOK or negative error code. On error the state of the file
     228 *         system is unchanged.
     229 *
     230 */
     231int ext4_filesystem_close(ext4_filesystem_t *fs)
    176232{
    177233        /* Write the superblock to the device */
    178234        ext4_superblock_set_state(fs->superblock, EXT4_SUPERBLOCK_STATE_VALID_FS);
    179235        int rc = ext4_superblock_write_direct(fs->device, fs->superblock);
    180        
    181         /* Release memory space for superblock */
    182         free(fs->superblock);
    183 
    184         /* Finish work with block library */
    185         block_cache_fini(fs->device);
    186         block_fini(fs->device);
    187        
    188         return rc;
     236        if (rc != EOK)
     237                return rc;
     238
     239        ext4_filesystem_fini(fs);
     240        return EOK;
    189241}
    190242
Note: See TracChangeset for help on using the changeset viewer.