| 1 | /* | 
|---|
| 2 | * Copyright (c) 2008 Jakub Jermar | 
|---|
| 3 | * Copyright (c) 2008 Martin Decky | 
|---|
| 4 | * Copyright (c) 2011 Martin Sucha | 
|---|
| 5 | * All rights reserved. | 
|---|
| 6 | * | 
|---|
| 7 | * Redistribution and use in source and binary forms, with or without | 
|---|
| 8 | * modification, are permitted provided that the following conditions | 
|---|
| 9 | * are met: | 
|---|
| 10 | * | 
|---|
| 11 | * - Redistributions of source code must retain the above copyright | 
|---|
| 12 | *   notice, this list of conditions and the following disclaimer. | 
|---|
| 13 | * - Redistributions in binary form must reproduce the above copyright | 
|---|
| 14 | *   notice, this list of conditions and the following disclaimer in the | 
|---|
| 15 | *   documentation and/or other materials provided with the distribution. | 
|---|
| 16 | * - The name of the author may not be used to endorse or promote products | 
|---|
| 17 | *   derived from this software without specific prior written permission. | 
|---|
| 18 | * | 
|---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | 
|---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | 
|---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | 
|---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | 
|---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 
|---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|---|
| 29 | */ | 
|---|
| 30 |  | 
|---|
| 31 | /** @addtogroup libblock | 
|---|
| 32 | * @{ | 
|---|
| 33 | */ | 
|---|
| 34 | /** | 
|---|
| 35 | * @file | 
|---|
| 36 | */ | 
|---|
| 37 |  | 
|---|
| 38 | #ifndef LIBBLOCK_LIBBLOCK_H_ | 
|---|
| 39 | #define LIBBLOCK_LIBBLOCK_H_ | 
|---|
| 40 |  | 
|---|
| 41 | #include <stdint.h> | 
|---|
| 42 | #include <async.h> | 
|---|
| 43 | #include <fibril_synch.h> | 
|---|
| 44 | #include <adt/hash_table.h> | 
|---|
| 45 | #include <adt/list.h> | 
|---|
| 46 | #include <loc.h> | 
|---|
| 47 |  | 
|---|
| 48 | /* | 
|---|
| 49 | * Flags that can be used with block_get(). | 
|---|
| 50 | */ | 
|---|
| 51 |  | 
|---|
| 52 | /** | 
|---|
| 53 | * This macro is a symbolic value for situations where no special flags are | 
|---|
| 54 | * needed. | 
|---|
| 55 | */ | 
|---|
| 56 | #define BLOCK_FLAGS_NONE        0 | 
|---|
| 57 |  | 
|---|
| 58 | /** | 
|---|
| 59 | * When the client of block_get() intends to overwrite the current contents of | 
|---|
| 60 | * the block, this flag is used to avoid the unnecessary read. | 
|---|
| 61 | */ | 
|---|
| 62 | #define BLOCK_FLAGS_NOREAD      1 | 
|---|
| 63 |  | 
|---|
| 64 | typedef struct block { | 
|---|
| 65 | /** Mutex protecting the reference count. */ | 
|---|
| 66 | fibril_mutex_t lock; | 
|---|
| 67 | /** Number of references to the block_t structure. */ | 
|---|
| 68 | unsigned refcnt; | 
|---|
| 69 | /** If true, the block needs to be written back to the block device. */ | 
|---|
| 70 | bool dirty; | 
|---|
| 71 | /** If true, the blcok does not contain valid data. */ | 
|---|
| 72 | bool toxic; | 
|---|
| 73 | /** Readers / Writer lock protecting the contents of the block. */ | 
|---|
| 74 | fibril_rwlock_t contents_lock; | 
|---|
| 75 | /** Service ID of service providing the block device. */ | 
|---|
| 76 | service_id_t service_id; | 
|---|
| 77 | /** Logical block address */ | 
|---|
| 78 | aoff64_t lba; | 
|---|
| 79 | /** Physical block address */ | 
|---|
| 80 | aoff64_t pba; | 
|---|
| 81 | /** Size of the block. */ | 
|---|
| 82 | size_t size; | 
|---|
| 83 | /** Link for placing the block into the free block list. */ | 
|---|
| 84 | link_t free_link; | 
|---|
| 85 | /** Link for placing the block into the block hash table. */ | 
|---|
| 86 | ht_link_t hash_link; | 
|---|
| 87 | /** Buffer with the block data. */ | 
|---|
| 88 | void *data; | 
|---|
| 89 | } block_t; | 
|---|
| 90 |  | 
|---|
| 91 | /** Caching mode */ | 
|---|
| 92 | enum cache_mode { | 
|---|
| 93 | /** Write-Through */ | 
|---|
| 94 | CACHE_MODE_WT, | 
|---|
| 95 | /** Write-Back */ | 
|---|
| 96 | CACHE_MODE_WB | 
|---|
| 97 | }; | 
|---|
| 98 |  | 
|---|
| 99 | typedef struct { | 
|---|
| 100 | uint16_t size; | 
|---|
| 101 | uint8_t first_session; | 
|---|
| 102 | uint8_t last_session; | 
|---|
| 103 |  | 
|---|
| 104 | uint8_t res0; | 
|---|
| 105 | uint8_t adr_ctrl; | 
|---|
| 106 | uint8_t first_track; | 
|---|
| 107 | uint8_t res1; | 
|---|
| 108 |  | 
|---|
| 109 | uint32_t first_lba; | 
|---|
| 110 | } __attribute__((packed)) toc_block_t; | 
|---|
| 111 |  | 
|---|
| 112 | extern int block_init(exch_mgmt_t, service_id_t, size_t); | 
|---|
| 113 | extern void block_fini(service_id_t); | 
|---|
| 114 |  | 
|---|
| 115 | extern int block_bb_read(service_id_t, aoff64_t); | 
|---|
| 116 | extern void *block_bb_get(service_id_t); | 
|---|
| 117 |  | 
|---|
| 118 | extern int block_cache_init(service_id_t, size_t, unsigned, enum cache_mode); | 
|---|
| 119 | extern int block_cache_fini(service_id_t); | 
|---|
| 120 |  | 
|---|
| 121 | extern int block_get(block_t **, service_id_t, aoff64_t, int); | 
|---|
| 122 | extern int block_put(block_t *); | 
|---|
| 123 |  | 
|---|
| 124 | extern int block_seqread(service_id_t, void *, size_t *, size_t *, aoff64_t *, | 
|---|
| 125 | void *, size_t); | 
|---|
| 126 |  | 
|---|
| 127 | extern int block_get_bsize(service_id_t, size_t *); | 
|---|
| 128 | extern int block_get_nblocks(service_id_t, aoff64_t *); | 
|---|
| 129 | extern toc_block_t *block_get_toc(service_id_t, uint8_t); | 
|---|
| 130 | extern int block_read_direct(service_id_t, aoff64_t, size_t, void *); | 
|---|
| 131 | extern int block_read_bytes_direct(service_id_t, aoff64_t, size_t, void *); | 
|---|
| 132 | extern int block_write_direct(service_id_t, aoff64_t, size_t, const void *); | 
|---|
| 133 |  | 
|---|
| 134 | #endif | 
|---|
| 135 |  | 
|---|
| 136 | /** @} | 
|---|
| 137 | */ | 
|---|