Rev 3538 | Rev 3540 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3521 | jermar | 1 | /* |
2 | * Copyright (c) 2008 Jakub Jermar |
||
3 | * Copyright (c) 2008 Martin Decky |
||
4 | * All rights reserved. |
||
5 | * |
||
6 | * Redistribution and use in source and binary forms, with or without |
||
7 | * modification, are permitted provided that the following conditions |
||
8 | * are met: |
||
9 | * |
||
10 | * - Redistributions of source code must retain the above copyright |
||
11 | * notice, this list of conditions and the following disclaimer. |
||
12 | * - Redistributions in binary form must reproduce the above copyright |
||
13 | * notice, this list of conditions and the following disclaimer in the |
||
14 | * documentation and/or other materials provided with the distribution. |
||
15 | * - The name of the author may not be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | */ |
||
29 | |||
30 | /** @addtogroup libblock |
||
31 | * @{ |
||
32 | */ |
||
33 | /** |
||
34 | * @file |
||
35 | * @brief |
||
36 | */ |
||
37 | |||
38 | #include "libblock.h" |
||
39 | #include "../../srv/vfs/vfs.h" |
||
40 | #include "../../srv/rd/rd.h" |
||
3530 | jermar | 41 | #include <ipc/devmap.h> |
42 | #include <ipc/services.h> |
||
3521 | jermar | 43 | #include <errno.h> |
3530 | jermar | 44 | #include <sys/mman.h> |
3521 | jermar | 45 | #include <async.h> |
46 | #include <ipc/ipc.h> |
||
47 | #include <as.h> |
||
48 | #include <assert.h> |
||
3531 | jermar | 49 | #include <futex.h> |
50 | #include <libadt/list.h> |
||
3539 | jermar | 51 | #include <libadt/hash_table.h> |
3521 | jermar | 52 | |
3531 | jermar | 53 | /** Lock protecting the device connection list */ |
54 | static futex_t dcl_lock = FUTEX_INITIALIZER; |
||
55 | /** Device connection list head. */ |
||
56 | static LIST_INITIALIZE(dcl_head); |
||
3530 | jermar | 57 | |
3539 | jermar | 58 | #define CACHE_BUCKETS_LOG2 10 |
59 | #define CACHE_BUCKETS (1 << CACHE_BUCKETS_LOG2) |
||
60 | |||
3531 | jermar | 61 | typedef struct { |
3539 | jermar | 62 | futex_t lock; |
63 | size_t block_size; /**< Block size. */ |
||
64 | unsigned block_count; /**< Total number of blocks. */ |
||
65 | hash_table_t block_hash; |
||
66 | link_t free_head; |
||
67 | } cache_t; |
||
68 | |||
69 | typedef struct { |
||
3531 | jermar | 70 | link_t link; |
71 | int dev_handle; |
||
72 | int dev_phone; |
||
73 | void *com_area; |
||
74 | size_t com_size; |
||
75 | void *bb_buf; |
||
76 | off_t bb_off; |
||
77 | size_t bb_size; |
||
3539 | jermar | 78 | cache_t *cache; |
3531 | jermar | 79 | } devcon_t; |
80 | |||
81 | static devcon_t *devcon_search(dev_handle_t dev_handle) |
||
82 | { |
||
83 | link_t *cur; |
||
84 | |||
85 | futex_down(&dcl_lock); |
||
86 | for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) { |
||
87 | devcon_t *devcon = list_get_instance(cur, devcon_t, link); |
||
88 | if (devcon->dev_handle == dev_handle) { |
||
89 | futex_up(&dcl_lock); |
||
90 | return devcon; |
||
91 | } |
||
92 | } |
||
93 | futex_up(&dcl_lock); |
||
94 | return NULL; |
||
95 | } |
||
96 | |||
97 | static int devcon_add(dev_handle_t dev_handle, int dev_phone, void *com_area, |
||
3537 | jermar | 98 | size_t com_size) |
3531 | jermar | 99 | { |
100 | link_t *cur; |
||
101 | devcon_t *devcon; |
||
102 | |||
103 | devcon = malloc(sizeof(devcon_t)); |
||
104 | if (!devcon) |
||
105 | return ENOMEM; |
||
106 | |||
107 | link_initialize(&devcon->link); |
||
108 | devcon->dev_handle = dev_handle; |
||
109 | devcon->dev_phone = dev_phone; |
||
110 | devcon->com_area = com_area; |
||
111 | devcon->com_size = com_size; |
||
3537 | jermar | 112 | devcon->bb_buf = NULL; |
113 | devcon->bb_off = 0; |
||
114 | devcon->bb_size = 0; |
||
3539 | jermar | 115 | devcon->cache = NULL; |
3531 | jermar | 116 | |
117 | futex_down(&dcl_lock); |
||
118 | for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) { |
||
119 | devcon_t *d = list_get_instance(cur, devcon_t, link); |
||
120 | if (d->dev_handle == dev_handle) { |
||
121 | futex_up(&dcl_lock); |
||
122 | free(devcon); |
||
123 | return EEXIST; |
||
124 | } |
||
125 | } |
||
126 | list_append(&devcon->link, &dcl_head); |
||
127 | futex_up(&dcl_lock); |
||
128 | return EOK; |
||
129 | } |
||
130 | |||
131 | static void devcon_remove(devcon_t *devcon) |
||
132 | { |
||
133 | futex_down(&dcl_lock); |
||
134 | list_remove(&devcon->link); |
||
135 | futex_up(&dcl_lock); |
||
136 | } |
||
137 | |||
3537 | jermar | 138 | int block_init(dev_handle_t dev_handle, size_t com_size) |
3530 | jermar | 139 | { |
140 | int rc; |
||
3531 | jermar | 141 | int dev_phone; |
142 | void *com_area; |
||
143 | |||
144 | com_area = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE, |
||
3530 | jermar | 145 | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); |
3531 | jermar | 146 | if (!com_area) { |
3530 | jermar | 147 | return ENOMEM; |
148 | } |
||
149 | dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, |
||
150 | DEVMAP_CONNECT_TO_DEVICE, dev_handle); |
||
151 | |||
152 | if (dev_phone < 0) { |
||
3531 | jermar | 153 | munmap(com_area, com_size); |
3530 | jermar | 154 | return dev_phone; |
155 | } |
||
156 | |||
3531 | jermar | 157 | rc = ipc_share_out_start(dev_phone, com_area, |
3530 | jermar | 158 | AS_AREA_READ | AS_AREA_WRITE); |
159 | if (rc != EOK) { |
||
3531 | jermar | 160 | munmap(com_area, com_size); |
3530 | jermar | 161 | ipc_hangup(dev_phone); |
162 | return rc; |
||
163 | } |
||
3531 | jermar | 164 | |
3537 | jermar | 165 | rc = devcon_add(dev_handle, dev_phone, com_area, com_size); |
3531 | jermar | 166 | if (rc != EOK) { |
167 | munmap(com_area, com_size); |
||
168 | ipc_hangup(dev_phone); |
||
169 | return rc; |
||
170 | } |
||
171 | |||
3530 | jermar | 172 | return EOK; |
173 | } |
||
174 | |||
175 | void block_fini(dev_handle_t dev_handle) |
||
176 | { |
||
3531 | jermar | 177 | devcon_t *devcon = devcon_search(dev_handle); |
178 | assert(devcon); |
||
179 | |||
180 | devcon_remove(devcon); |
||
181 | |||
3537 | jermar | 182 | if (devcon->bb_buf) |
183 | free(devcon->bb_buf); |
||
3539 | jermar | 184 | |
185 | if (devcon->cache) { |
||
186 | hash_table_destroy(&devcon->cache->block_hash); |
||
187 | free(devcon->cache); |
||
188 | } |
||
189 | |||
3531 | jermar | 190 | munmap(devcon->com_area, devcon->com_size); |
191 | ipc_hangup(devcon->dev_phone); |
||
192 | |||
193 | free(devcon); |
||
3530 | jermar | 194 | } |
195 | |||
3537 | jermar | 196 | int block_bb_read(dev_handle_t dev_handle, off_t off, size_t size) |
197 | { |
||
198 | void *bb_buf; |
||
3538 | jermar | 199 | int rc; |
3537 | jermar | 200 | |
201 | devcon_t *devcon = devcon_search(dev_handle); |
||
202 | if (!devcon) |
||
203 | return ENOENT; |
||
204 | if (devcon->bb_buf) |
||
205 | return EEXIST; |
||
206 | bb_buf = malloc(size); |
||
207 | if (!bb_buf) |
||
208 | return ENOMEM; |
||
209 | |||
210 | off_t bufpos = 0; |
||
211 | size_t buflen = 0; |
||
3538 | jermar | 212 | rc = block_read(dev_handle, &bufpos, &buflen, &off, |
213 | bb_buf, size, size); |
||
214 | if (rc != EOK) { |
||
3537 | jermar | 215 | free(bb_buf); |
3538 | jermar | 216 | return rc; |
3537 | jermar | 217 | } |
218 | devcon->bb_buf = bb_buf; |
||
219 | devcon->bb_off = off; |
||
220 | devcon->bb_size = size; |
||
221 | |||
222 | return EOK; |
||
223 | } |
||
224 | |||
3530 | jermar | 225 | void *block_bb_get(dev_handle_t dev_handle) |
226 | { |
||
3531 | jermar | 227 | devcon_t *devcon = devcon_search(dev_handle); |
228 | assert(devcon); |
||
229 | return devcon->bb_buf; |
||
3530 | jermar | 230 | } |
231 | |||
3539 | jermar | 232 | static hash_index_t cache_hash(unsigned long *key) |
233 | { |
||
234 | return *key & (CACHE_BUCKETS - 1); |
||
235 | } |
||
236 | |||
237 | static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item) |
||
238 | { |
||
239 | block_t *b = hash_table_get_instance(item, block_t, hash_link); |
||
240 | return b->boff == *key; |
||
241 | } |
||
242 | |||
243 | static void cache_remove_callback(link_t *item) |
||
244 | { |
||
245 | } |
||
246 | |||
247 | static hash_table_operations_t cache_ops = { |
||
248 | .hash = cache_hash, |
||
249 | .compare = cache_compare, |
||
250 | .remove_callback = cache_remove_callback |
||
251 | }; |
||
252 | |||
253 | int block_cache_init(dev_handle_t dev_handle, size_t size, unsigned blocks) |
||
254 | { |
||
255 | devcon_t *devcon = devcon_search(dev_handle); |
||
256 | cache_t *cache; |
||
257 | if (!devcon) |
||
258 | return ENOENT; |
||
259 | if (devcon->cache) |
||
260 | return EEXIST; |
||
261 | cache = malloc(sizeof(cache_t)); |
||
262 | if (!cache) |
||
263 | return ENOMEM; |
||
264 | |||
265 | futex_initialize(&cache->lock, 0); |
||
266 | list_initialize(&cache->free_head); |
||
267 | cache->block_size = size; |
||
268 | cache->block_count = blocks; |
||
269 | |||
270 | if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1, |
||
271 | &cache_ops)) { |
||
272 | free(cache); |
||
273 | return ENOMEM; |
||
274 | } |
||
275 | |||
276 | devcon->cache = cache; |
||
277 | return EOK; |
||
278 | } |
||
279 | |||
3521 | jermar | 280 | /** Read data from a block device. |
281 | * |
||
3530 | jermar | 282 | * @param dev_handle Device handle of the block device. |
3521 | jermar | 283 | * @param bufpos Pointer to the first unread valid offset within the |
284 | * communication buffer. |
||
285 | * @param buflen Pointer to the number of unread bytes that are ready in |
||
286 | * the communication buffer. |
||
287 | * @param pos Device position to be read. |
||
288 | * @param dst Destination buffer. |
||
289 | * @param size Size of the destination buffer. |
||
290 | * @param block_size Block size to be used for the transfer. |
||
291 | * |
||
3538 | jermar | 292 | * @return EOK on success or a negative return code on failure. |
3521 | jermar | 293 | */ |
3538 | jermar | 294 | int |
3530 | jermar | 295 | block_read(int dev_handle, off_t *bufpos, size_t *buflen, off_t *pos, void *dst, |
296 | size_t size, size_t block_size) |
||
3521 | jermar | 297 | { |
298 | off_t offset = 0; |
||
299 | size_t left = size; |
||
3531 | jermar | 300 | devcon_t *devcon = devcon_search(dev_handle); |
301 | assert(devcon); |
||
3521 | jermar | 302 | |
303 | while (left > 0) { |
||
304 | size_t rd; |
||
305 | |||
306 | if (*bufpos + left < *buflen) |
||
307 | rd = left; |
||
308 | else |
||
309 | rd = *buflen - *bufpos; |
||
310 | |||
311 | if (rd > 0) { |
||
312 | /* |
||
313 | * Copy the contents of the communication buffer to the |
||
314 | * destination buffer. |
||
315 | */ |
||
3531 | jermar | 316 | memcpy(dst + offset, devcon->com_area + *bufpos, rd); |
3521 | jermar | 317 | offset += rd; |
318 | *bufpos += rd; |
||
319 | *pos += rd; |
||
320 | left -= rd; |
||
321 | } |
||
322 | |||
323 | if (*bufpos == *buflen) { |
||
324 | /* Refill the communication buffer with a new block. */ |
||
325 | ipcarg_t retval; |
||
3531 | jermar | 326 | int rc = async_req_2_1(devcon->dev_phone, RD_READ_BLOCK, |
3521 | jermar | 327 | *pos / block_size, block_size, &retval); |
328 | if ((rc != EOK) || (retval != EOK)) |
||
3538 | jermar | 329 | return (rc != EOK ? rc : retval); |
3521 | jermar | 330 | |
331 | *bufpos = 0; |
||
332 | *buflen = block_size; |
||
333 | } |
||
334 | } |
||
335 | |||
3538 | jermar | 336 | return EOK; |
3521 | jermar | 337 | } |
338 | |||
339 | block_t *block_get(dev_handle_t dev_handle, off_t offset, size_t bs) |
||
340 | { |
||
341 | /* FIXME */ |
||
342 | block_t *b; |
||
343 | off_t bufpos = 0; |
||
344 | size_t buflen = 0; |
||
345 | off_t pos = offset * bs; |
||
346 | |||
347 | b = malloc(sizeof(block_t)); |
||
348 | if (!b) |
||
349 | return NULL; |
||
350 | |||
351 | b->data = malloc(bs); |
||
352 | if (!b->data) { |
||
353 | free(b); |
||
354 | return NULL; |
||
355 | } |
||
356 | b->size = bs; |
||
357 | |||
3538 | jermar | 358 | if (block_read(dev_handle, &bufpos, &buflen, &pos, b->data, |
359 | bs, bs) != EOK) { |
||
3521 | jermar | 360 | free(b->data); |
361 | free(b); |
||
362 | return NULL; |
||
363 | } |
||
364 | |||
365 | return b; |
||
366 | } |
||
367 | |||
368 | void block_put(block_t *block) |
||
369 | { |
||
370 | /* FIXME */ |
||
371 | free(block->data); |
||
372 | free(block); |
||
373 | } |
||
374 | |||
375 | /** @} |
||
376 | */ |