~martin-decky/helenos/rcu

« back to all changes in this revision

Viewing changes to uspace/lib/block/libblock.c

  • Committer: Jakub Jermar
  • Date: 2011-06-02 21:26:44 UTC
  • mfrom: (720.2.82 ext2-merge)
  • Revision ID: jakub@jermar.eu-20110602212644-t5p3o4bux1n8ybvd
Merge from http://ho.st.dcs.fmph.uniba.sk/~mato/bzr/helenos-ext2.

Changes made against the ext2 branch parent:
- removed .bzrignore
- removed all traces of pipefs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * Copyright (c) 2008 Jakub Jermar
3
3
 * Copyright (c) 2008 Martin Decky
 
4
 * Copyright (c) 2011 Martin Sucha
4
5
 * All rights reserved.
5
6
 *
6
7
 * Redistribution and use in source and binary forms, with or without
826
827
        return get_num_blocks(devcon->dev_phone, nblocks);
827
828
}
828
829
 
 
830
/** Read bytes directly from the device (bypass cache)
 
831
 * 
 
832
 * @param devmap_handle Device handle of the block device.
 
833
 * @param abs_offset    Absolute offset in bytes where to start reading
 
834
 * @param bytes                 Number of bytes to read
 
835
 * @param data                  Buffer that receives the data
 
836
 * 
 
837
 * @return              EOK on success or negative error code on failure.
 
838
 */
 
839
int block_read_bytes_direct(devmap_handle_t devmap_handle, aoff64_t abs_offset,
 
840
    size_t bytes, void *data)
 
841
{
 
842
        int rc;
 
843
        size_t phys_block_size;
 
844
        size_t buf_size;
 
845
        void *buffer;
 
846
        aoff64_t first_block;
 
847
        aoff64_t last_block;
 
848
        size_t blocks;
 
849
        size_t offset;
 
850
        
 
851
        rc = block_get_bsize(devmap_handle, &phys_block_size);
 
852
        if (rc != EOK) {
 
853
                return rc;
 
854
        }
 
855
        
 
856
        /* calculate data position and required space */
 
857
        first_block = abs_offset / phys_block_size;
 
858
        offset = abs_offset % phys_block_size;
 
859
        last_block = (abs_offset + bytes - 1) / phys_block_size;
 
860
        blocks = last_block - first_block + 1;
 
861
        buf_size = blocks * phys_block_size;
 
862
        
 
863
        /* read the data into memory */
 
864
        buffer = malloc(buf_size);
 
865
        if (buffer == NULL) {
 
866
                return ENOMEM;
 
867
        }
 
868
        
 
869
        rc = block_read_direct(devmap_handle, first_block, blocks, buffer);
 
870
        if (rc != EOK) {
 
871
                free(buffer);
 
872
                return rc;
 
873
        }
 
874
        
 
875
        /* copy the data from the buffer */
 
876
        memcpy(data, buffer + offset, bytes);
 
877
        free(buffer);
 
878
        
 
879
        return EOK;
 
880
}
 
881
 
829
882
/** Read blocks from block device.
830
883
 *
831
884
 * @param devcon        Device connection.