Changeset e272949 in mainline


Ignore:
Timestamp:
2011-02-13T20:27:30Z (13 years ago)
Author:
Martin Sucha <sucha14@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
566c401
Parents:
36bca8eb
Message:

Refactor direct reading of superblock

Location:
uspace
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/ext2info/ext2info.c

    r36bca8eb re272949  
    9292        }
    9393
    94         rc = ext2_superblock_read_direct(&superblock, handle);
     94        rc = ext2_superblock_read_direct(handle, &superblock);
    9595        if (rc != EOK)  {
    9696                printf(NAME ": Error reading superblock.\n");
  • uspace/lib/block/libblock.c

    r36bca8eb re272949  
    22 * Copyright (c) 2008 Jakub Jermar
    33 * Copyright (c) 2008 Martin Decky
     4 * Copyright (c) 2011 Martin Sucha
    45 * All rights reserved.
    56 *
     
    809810}
    810811
     812/** Read bytes directly from the device (bypass cache)
     813 *
     814 * @param devmap_handle Device handle of the block device.
     815 * @param abs_offset    Absolute offset in bytes where to start reading
     816 * @param bytes                 Number of bytes to read
     817 * @param data                  Buffer that receives the data
     818 *
     819 * @return              EOK on success or negative error code on failure.
     820 */
     821int block_read_bytes_direct(devmap_handle_t devmap_handle, aoff64_t abs_offset,
     822    size_t bytes, void *data)
     823{
     824        int rc;
     825        size_t phys_block_size;
     826        size_t buf_size;
     827        void *buffer;
     828        aoff64_t first_block;
     829        aoff64_t last_block;
     830        size_t blocks;
     831        size_t offset;
     832       
     833        rc = block_get_bsize(devmap_handle, &phys_block_size);
     834        if (rc != EOK) {
     835                return rc;
     836        }
     837       
     838        // calculate data position and required space
     839        first_block = abs_offset / phys_block_size;
     840        offset = abs_offset % phys_block_size;
     841        last_block = (abs_offset + bytes - 1) / phys_block_size;
     842        blocks = last_block - first_block + 1;
     843        buf_size = blocks * phys_block_size;
     844       
     845        // read the data into memory
     846        buffer = malloc(buf_size);
     847        if (buffer == NULL) {
     848                return ENOMEM;
     849        }
     850       
     851        rc = block_read_direct(devmap_handle, first_block, blocks, buffer);
     852        if (rc != EOK) {
     853                free(buffer);
     854                return rc;
     855        }
     856       
     857        // copy the data from the buffer
     858        memcpy(data, buffer + offset, bytes);
     859        free(buffer);
     860       
     861        return EOK;
     862}
     863
    811864/** Read blocks from block device.
    812865 *
  • uspace/lib/block/libblock.h

    r36bca8eb re272949  
    22 * Copyright (c) 2008 Jakub Jermar
    33 * Copyright (c) 2008 Martin Decky
     4 * Copyright (c) 2011 Martin Sucha
    45 * All rights reserved.
    56 *
     
    113114extern int block_get_nblocks(devmap_handle_t, aoff64_t *);
    114115extern int block_read_direct(devmap_handle_t, aoff64_t, size_t, void *);
     116extern int block_read_bytes_direct(devmap_handle_t, aoff64_t, size_t, void *);
    115117extern int block_write_direct(devmap_handle_t, aoff64_t, size_t, const void *);
    116118
  • uspace/lib/ext2/libext2.h

    r36bca8eb re272949  
    7575inline uint32_t ext2_superblock_get_fragments_per_group(ext2_superblock_t *sb);
    7676
    77 int ext2_superblock_read_direct(ext2_superblock_t **superblock,
    78                                                                 devmap_handle_t dev);
     77int ext2_superblock_read_direct(devmap_handle_t, ext2_superblock_t **);
    7978
    8079int ext2_filesystem_init(ext2_filesystem_t *fs, devmap_handle_t dev);
  • uspace/lib/ext2/libext2_filesystem.c

    r36bca8eb re272949  
    3737#include <errno.h>
    3838#include <libblock.h>
    39 #include <mem.h>
    4039#include <malloc.h>
    41 
    42 int ext2_superblock_read_direct(ext2_superblock_t **superblock,
    43                                                                 devmap_handle_t dev) {
    44         int rc;
    45         size_t phys_block_size;
    46         size_t buf_size;
    47         uint8_t *buffer;
    48         size_t first_block;
    49         size_t last_block;
    50         size_t blocks;
    51         size_t offset;
    52         ext2_superblock_t *tmp_superblock;
    53        
    54         rc = block_get_bsize(dev, &phys_block_size);
    55         if (rc != EOK) {
    56                 return rc;
    57         }
    58        
    59         // calculate superblock position and required space
    60         first_block = EXT2_SUPERBLOCK_OFFSET / phys_block_size;
    61         offset = EXT2_SUPERBLOCK_OFFSET % phys_block_size;
    62         last_block = EXT2_SUPERBLOCK_LAST_BYTE / phys_block_size;
    63         blocks = last_block - first_block + 1;
    64         buf_size = blocks * phys_block_size;
    65        
    66         // read the superblock into memory
    67         buffer = malloc(buf_size);
    68         if (buffer == NULL) {
    69                 return ENOMEM;
    70         }
    71        
    72         rc = block_read_direct(dev, first_block, blocks, buffer);
    73         if (rc != EOK) {
    74                 free(buffer);
    75                 return rc;
    76         }
    77        
    78         // copy the superblock from the buffer
    79         // as it may not be at the start of the block
    80         // (i.e. blocks larger than 1K)
    81         tmp_superblock = malloc(EXT2_SUPERBLOCK_SIZE);
    82         if (tmp_superblock == NULL) {
    83                 free(buffer);
    84                 return ENOMEM;
    85         }
    86        
    87         memcpy(tmp_superblock, buffer + offset, EXT2_SUPERBLOCK_SIZE);
    88         free(buffer);
    89         (*superblock) = tmp_superblock;
    90        
    91         return EOK;
    92 }
    9340
    9441/**
     
    10855        }
    10956       
    110         rc = ext2_superblock_read_direct(&temp_superblock, dev);
     57        rc = ext2_superblock_read_direct(dev, &temp_superblock);
    11158        if (rc != EOK) {
    11259                block_fini(dev);
  • uspace/lib/ext2/libext2_superblock.c

    r36bca8eb re272949  
    3535
    3636#include "libext2.h"
     37#include <errno.h>
     38#include <malloc.h>
     39#include <libblock.h>
    3740
    3841/**
     
    104107
    105108
     109/** Read a superblock directly from device (i.e. no libblock cache)
     110 *
     111 * @param devmap_handle Device handle of the block device.
     112 * @param superblock    Pointer where to store pointer to new superblock
     113 *
     114 * @return              EOK on success or negative error code on failure.
     115 */
     116int ext2_superblock_read_direct(devmap_handle_t devmap_handle,
     117    ext2_superblock_t **superblock)
     118{
     119        void *data;
     120        int rc;
     121       
     122        data = malloc(EXT2_SUPERBLOCK_SIZE);
     123        if (data == NULL) {
     124                return ENOMEM;
     125        }
     126       
     127        rc = block_read_bytes_direct(devmap_handle, EXT2_SUPERBLOCK_OFFSET,
     128            EXT2_SUPERBLOCK_SIZE, data);
     129        if (rc != EOK) {
     130                free(data);
     131                return rc;
     132        }
     133       
     134        (*superblock) = data;
     135        return EOK;
     136}
     137
     138
    106139/** @}
    107140 */
Note: See TracChangeset for help on using the changeset viewer.