Changeset de5b708 in mainline


Ignore:
Timestamp:
2017-05-12T17:15:49Z (8 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.

Location:
uspace/lib/ext4
Files:
3 edited

Legend:

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

    rbe39fc6 rde5b708  
    3939#include "ext4/types.h"
    4040
    41 extern int ext4_filesystem_init(ext4_filesystem_t *, ext4_instance_t *,
    42     service_id_t, enum cache_mode, aoff64_t *);
    43 extern int ext4_filesystem_fini(ext4_filesystem_t *);
     41extern int ext4_filesystem_open(ext4_instance_t *, service_id_t,
     42    enum cache_mode, aoff64_t *, ext4_filesystem_t **);
     43extern int ext4_filesystem_close(ext4_filesystem_t *);
    4444extern uint32_t ext4_filesystem_blockaddr2_index_in_group(ext4_superblock_t *,
    4545    uint32_t);
  • 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
  • uspace/lib/ext4/src/ops.c

    rbe39fc6 rde5b708  
    928928    fs_index_t *index, aoff64_t *size, unsigned *lnkcnt)
    929929{
    930         /* Allocate libext4 filesystem structure */
    931         ext4_filesystem_t *fs = (ext4_filesystem_t *)
    932             malloc(sizeof(ext4_filesystem_t));
    933         if (fs == NULL)
    934                 return ENOMEM;
     930        ext4_filesystem_t *fs;
    935931       
    936932        /* Allocate instance structure */
    937933        ext4_instance_t *inst = (ext4_instance_t *)
    938934            malloc(sizeof(ext4_instance_t));
    939         if (inst == NULL) {
    940                 free(fs);
     935        if (inst == NULL)
    941936                return ENOMEM;
    942         }
    943937       
    944938        enum cache_mode cmode;
     
    951945        link_initialize(&inst->link);
    952946        inst->service_id = service_id;
    953         inst->filesystem = fs;
    954947        inst->open_nodes_count = 0;
    955948       
    956949        /* Initialize the filesystem */
    957950        aoff64_t rnsize;
    958         int rc = ext4_filesystem_init(fs, inst, service_id, cmode, &rnsize);
    959         if (rc != EOK) {
    960                 free(fs);
     951        int rc = ext4_filesystem_open(inst, service_id, cmode, &rnsize, &fs);
     952        if (rc != EOK) {
    961953                free(inst);
    962954                return rc;
     
    1005997        fibril_mutex_unlock(&open_nodes_lock);
    1006998       
    1007         return ext4_filesystem_fini(inst->filesystem);
     999        rc = ext4_filesystem_close(inst->filesystem);
     1000        if (rc != EOK) {
     1001                fibril_mutex_lock(&instance_list_mutex);
     1002                list_append(&inst->link, &instance_list);
     1003                fibril_mutex_unlock(&instance_list_mutex);
     1004        }
     1005
     1006        free(inst);
     1007        return EOK;
    10081008}
    10091009
Note: See TracChangeset for help on using the changeset viewer.