Rev 4420 | 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 | |||
4001 | decky | 30 | /** @addtogroup libblock |
3521 | jermar | 31 | * @{ |
4001 | decky | 32 | */ |
3521 | jermar | 33 | /** |
34 | * @file |
||
35 | * @brief |
||
36 | */ |
||
37 | |||
4001 | decky | 38 | #include "libblock.h" |
3521 | jermar | 39 | #include "../../srv/vfs/vfs.h" |
3530 | jermar | 40 | #include <ipc/devmap.h> |
4420 | trochtova | 41 | #include <ipc/bd.h> |
3530 | jermar | 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; |
4439 | trochtova | 71 | dev_handle_t dev_handle; |
3531 | jermar | 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 | |||
4420 | trochtova | 150 | dev_phone = devmap_device_connect(dev_handle, IPC_FLAG_BLOCKING); |
3530 | jermar | 151 | if (dev_phone < 0) { |
3531 | jermar | 152 | munmap(com_area, com_size); |
3530 | jermar | 153 | return dev_phone; |
154 | } |
||
155 | |||
3531 | jermar | 156 | rc = ipc_share_out_start(dev_phone, com_area, |
3530 | jermar | 157 | AS_AREA_READ | AS_AREA_WRITE); |
158 | if (rc != EOK) { |
||
3531 | jermar | 159 | munmap(com_area, com_size); |
3530 | jermar | 160 | ipc_hangup(dev_phone); |
161 | return rc; |
||
162 | } |
||
3531 | jermar | 163 | |
3537 | jermar | 164 | rc = devcon_add(dev_handle, dev_phone, com_area, com_size); |
3531 | jermar | 165 | if (rc != EOK) { |
166 | munmap(com_area, com_size); |
||
167 | ipc_hangup(dev_phone); |
||
168 | return rc; |
||
169 | } |
||
170 | |||
3530 | jermar | 171 | return EOK; |
172 | } |
||
173 | |||
174 | void block_fini(dev_handle_t dev_handle) |
||
175 | { |
||
3531 | jermar | 176 | devcon_t *devcon = devcon_search(dev_handle); |
177 | assert(devcon); |
||
178 | |||
179 | devcon_remove(devcon); |
||
180 | |||
3537 | jermar | 181 | if (devcon->bb_buf) |
182 | free(devcon->bb_buf); |
||
3539 | jermar | 183 | |
184 | if (devcon->cache) { |
||
185 | hash_table_destroy(&devcon->cache->block_hash); |
||
186 | free(devcon->cache); |
||
187 | } |
||
188 | |||
3531 | jermar | 189 | munmap(devcon->com_area, devcon->com_size); |
190 | ipc_hangup(devcon->dev_phone); |
||
191 | |||
192 | free(devcon); |
||
3530 | jermar | 193 | } |
194 | |||
3537 | jermar | 195 | int block_bb_read(dev_handle_t dev_handle, off_t off, size_t size) |
196 | { |
||
197 | void *bb_buf; |
||
3538 | jermar | 198 | int rc; |
3537 | jermar | 199 | |
200 | devcon_t *devcon = devcon_search(dev_handle); |
||
201 | if (!devcon) |
||
202 | return ENOENT; |
||
203 | if (devcon->bb_buf) |
||
204 | return EEXIST; |
||
205 | bb_buf = malloc(size); |
||
206 | if (!bb_buf) |
||
207 | return ENOMEM; |
||
208 | |||
209 | off_t bufpos = 0; |
||
210 | size_t buflen = 0; |
||
3538 | jermar | 211 | rc = block_read(dev_handle, &bufpos, &buflen, &off, |
212 | bb_buf, size, size); |
||
213 | if (rc != EOK) { |
||
3537 | jermar | 214 | free(bb_buf); |
3538 | jermar | 215 | return rc; |
3537 | jermar | 216 | } |
217 | devcon->bb_buf = bb_buf; |
||
218 | devcon->bb_off = off; |
||
219 | devcon->bb_size = size; |
||
220 | |||
221 | return EOK; |
||
222 | } |
||
223 | |||
3530 | jermar | 224 | void *block_bb_get(dev_handle_t dev_handle) |
225 | { |
||
3531 | jermar | 226 | devcon_t *devcon = devcon_search(dev_handle); |
227 | assert(devcon); |
||
228 | return devcon->bb_buf; |
||
3530 | jermar | 229 | } |
230 | |||
3539 | jermar | 231 | static hash_index_t cache_hash(unsigned long *key) |
232 | { |
||
233 | return *key & (CACHE_BUCKETS - 1); |
||
234 | } |
||
235 | |||
236 | static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item) |
||
237 | { |
||
238 | block_t *b = hash_table_get_instance(item, block_t, hash_link); |
||
239 | return b->boff == *key; |
||
240 | } |
||
241 | |||
242 | static void cache_remove_callback(link_t *item) |
||
243 | { |
||
244 | } |
||
245 | |||
246 | static hash_table_operations_t cache_ops = { |
||
247 | .hash = cache_hash, |
||
248 | .compare = cache_compare, |
||
249 | .remove_callback = cache_remove_callback |
||
250 | }; |
||
251 | |||
252 | int block_cache_init(dev_handle_t dev_handle, size_t size, unsigned blocks) |
||
253 | { |
||
254 | devcon_t *devcon = devcon_search(dev_handle); |
||
255 | cache_t *cache; |
||
256 | if (!devcon) |
||
257 | return ENOENT; |
||
258 | if (devcon->cache) |
||
259 | return EEXIST; |
||
260 | cache = malloc(sizeof(cache_t)); |
||
261 | if (!cache) |
||
262 | return ENOMEM; |
||
263 | |||
3540 | jermar | 264 | futex_initialize(&cache->lock, 1); |
3539 | jermar | 265 | list_initialize(&cache->free_head); |
266 | cache->block_size = size; |
||
267 | cache->block_count = blocks; |
||
268 | |||
269 | if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1, |
||
270 | &cache_ops)) { |
||
271 | free(cache); |
||
272 | return ENOMEM; |
||
273 | } |
||
274 | |||
275 | devcon->cache = cache; |
||
276 | return EOK; |
||
277 | } |
||
278 | |||
3540 | jermar | 279 | static bool cache_can_grow(cache_t *cache) |
3521 | jermar | 280 | { |
3540 | jermar | 281 | return true; |
282 | } |
||
283 | |||
284 | static void block_initialize(block_t *b) |
||
285 | { |
||
286 | futex_initialize(&b->lock, 1); |
||
287 | b->refcnt = 1; |
||
288 | b->dirty = false; |
||
289 | rwlock_initialize(&b->contents_lock); |
||
290 | link_initialize(&b->free_link); |
||
291 | link_initialize(&b->hash_link); |
||
292 | } |
||
293 | |||
294 | /** Instantiate a block in memory and get a reference to it. |
||
295 | * |
||
296 | * @param dev_handle Device handle of the block device. |
||
297 | * @param boff Block offset. |
||
3595 | jermar | 298 | * @param flags If BLOCK_FLAGS_NOREAD is specified, block_get() |
299 | * will not read the contents of the block from the |
||
300 | * device. |
||
3540 | jermar | 301 | * |
302 | * @return Block structure. |
||
303 | */ |
||
3595 | jermar | 304 | block_t *block_get(dev_handle_t dev_handle, bn_t boff, int flags) |
3540 | jermar | 305 | { |
306 | devcon_t *devcon; |
||
307 | cache_t *cache; |
||
3521 | jermar | 308 | block_t *b; |
3540 | jermar | 309 | link_t *l; |
310 | unsigned long key = boff; |
||
311 | |||
312 | devcon = devcon_search(dev_handle); |
||
3521 | jermar | 313 | |
3540 | jermar | 314 | assert(devcon); |
315 | assert(devcon->cache); |
||
3521 | jermar | 316 | |
3540 | jermar | 317 | cache = devcon->cache; |
318 | futex_down(&cache->lock); |
||
319 | l = hash_table_find(&cache->block_hash, &key); |
||
320 | if (l) { |
||
321 | /* |
||
322 | * We found the block in the cache. |
||
323 | */ |
||
324 | b = hash_table_get_instance(l, block_t, hash_link); |
||
325 | futex_down(&b->lock); |
||
326 | if (b->refcnt++ == 0) |
||
327 | list_remove(&b->free_link); |
||
328 | futex_up(&b->lock); |
||
3544 | jermar | 329 | futex_up(&cache->lock); |
3540 | jermar | 330 | } else { |
331 | /* |
||
332 | * The block was not found in the cache. |
||
333 | */ |
||
334 | int rc; |
||
335 | off_t bufpos = 0; |
||
336 | size_t buflen = 0; |
||
337 | off_t pos = boff * cache->block_size; |
||
3544 | jermar | 338 | bool sync = false; |
3521 | jermar | 339 | |
3540 | jermar | 340 | if (cache_can_grow(cache)) { |
341 | /* |
||
342 | * We can grow the cache by allocating new blocks. |
||
343 | * Should the allocation fail, we fail over and try to |
||
344 | * recycle a block from the cache. |
||
345 | */ |
||
346 | b = malloc(sizeof(block_t)); |
||
347 | if (!b) |
||
348 | goto recycle; |
||
349 | b->data = malloc(cache->block_size); |
||
350 | if (!b->data) { |
||
351 | free(b); |
||
352 | goto recycle; |
||
353 | } |
||
354 | } else { |
||
355 | /* |
||
356 | * Try to recycle a block from the free list. |
||
357 | */ |
||
358 | unsigned long temp_key; |
||
359 | recycle: |
||
360 | assert(!list_empty(&cache->free_head)); |
||
361 | l = cache->free_head.next; |
||
362 | list_remove(l); |
||
363 | b = hash_table_get_instance(l, block_t, hash_link); |
||
3544 | jermar | 364 | sync = b->dirty; |
3540 | jermar | 365 | temp_key = b->boff; |
366 | hash_table_remove(&cache->block_hash, &temp_key, 1); |
||
367 | } |
||
368 | |||
369 | block_initialize(b); |
||
370 | b->dev_handle = dev_handle; |
||
371 | b->size = cache->block_size; |
||
372 | b->boff = boff; |
||
3544 | jermar | 373 | hash_table_insert(&cache->block_hash, &key, &b->hash_link); |
374 | |||
375 | /* |
||
376 | * Lock the block before releasing the cache lock. Thus we don't |
||
377 | * kill concurent operations on the cache while doing I/O on the |
||
378 | * block. |
||
379 | */ |
||
380 | futex_down(&b->lock); |
||
381 | futex_up(&cache->lock); |
||
382 | |||
383 | if (sync) { |
||
384 | /* |
||
385 | * The block is dirty and needs to be written back to |
||
386 | * the device before we can read in the new contents. |
||
387 | */ |
||
388 | abort(); /* TODO: block_write() */ |
||
389 | } |
||
3595 | jermar | 390 | if (!(flags & BLOCK_FLAGS_NOREAD)) { |
391 | /* |
||
392 | * The block contains old or no data. We need to read |
||
393 | * the new contents from the device. |
||
394 | */ |
||
395 | rc = block_read(dev_handle, &bufpos, &buflen, &pos, |
||
396 | b->data, cache->block_size, cache->block_size); |
||
397 | assert(rc == EOK); |
||
398 | } |
||
3544 | jermar | 399 | |
400 | futex_up(&b->lock); |
||
3521 | jermar | 401 | } |
402 | return b; |
||
403 | } |
||
404 | |||
3543 | jermar | 405 | /** Release a reference to a block. |
406 | * |
||
3544 | jermar | 407 | * If the last reference is dropped, the block is put on the free list. |
3543 | jermar | 408 | * |
409 | * @param block Block of which a reference is to be released. |
||
410 | */ |
||
3521 | jermar | 411 | void block_put(block_t *block) |
412 | { |
||
3543 | jermar | 413 | devcon_t *devcon = devcon_search(block->dev_handle); |
414 | cache_t *cache; |
||
415 | |||
416 | assert(devcon); |
||
417 | assert(devcon->cache); |
||
418 | |||
419 | cache = devcon->cache; |
||
420 | futex_down(&cache->lock); |
||
421 | futex_down(&block->lock); |
||
422 | if (!--block->refcnt) { |
||
423 | /* |
||
424 | * Last reference to the block was dropped, put the block on the |
||
425 | * free list. |
||
426 | */ |
||
427 | list_append(&block->free_link, &cache->free_head); |
||
428 | } |
||
429 | futex_up(&block->lock); |
||
3544 | jermar | 430 | futex_up(&cache->lock); |
3543 | jermar | 431 | } |
432 | |||
433 | /** Read data from a block device. |
||
434 | * |
||
435 | * @param dev_handle Device handle of the block device. |
||
436 | * @param bufpos Pointer to the first unread valid offset within the |
||
437 | * communication buffer. |
||
438 | * @param buflen Pointer to the number of unread bytes that are ready in |
||
439 | * the communication buffer. |
||
440 | * @param pos Device position to be read. |
||
441 | * @param dst Destination buffer. |
||
442 | * @param size Size of the destination buffer. |
||
443 | * @param block_size Block size to be used for the transfer. |
||
444 | * |
||
445 | * @return EOK on success or a negative return code on failure. |
||
446 | */ |
||
447 | int |
||
4439 | trochtova | 448 | block_read(dev_handle_t dev_handle, off_t *bufpos, size_t *buflen, off_t *pos, |
449 | void *dst, size_t size, size_t block_size) |
||
3543 | jermar | 450 | { |
451 | off_t offset = 0; |
||
452 | size_t left = size; |
||
453 | devcon_t *devcon = devcon_search(dev_handle); |
||
454 | assert(devcon); |
||
3540 | jermar | 455 | |
3543 | jermar | 456 | while (left > 0) { |
457 | size_t rd; |
||
458 | |||
459 | if (*bufpos + left < *buflen) |
||
460 | rd = left; |
||
461 | else |
||
462 | rd = *buflen - *bufpos; |
||
463 | |||
464 | if (rd > 0) { |
||
465 | /* |
||
466 | * Copy the contents of the communication buffer to the |
||
467 | * destination buffer. |
||
468 | */ |
||
469 | memcpy(dst + offset, devcon->com_area + *bufpos, rd); |
||
470 | offset += rd; |
||
471 | *bufpos += rd; |
||
472 | *pos += rd; |
||
473 | left -= rd; |
||
474 | } |
||
475 | |||
476 | if (*bufpos == *buflen) { |
||
477 | /* Refill the communication buffer with a new block. */ |
||
478 | ipcarg_t retval; |
||
4420 | trochtova | 479 | int rc = async_req_2_1(devcon->dev_phone, BD_READ_BLOCK, |
3543 | jermar | 480 | *pos / block_size, block_size, &retval); |
481 | if ((rc != EOK) || (retval != EOK)) |
||
482 | return (rc != EOK ? rc : retval); |
||
483 | |||
484 | *bufpos = 0; |
||
485 | *buflen = block_size; |
||
486 | } |
||
487 | } |
||
488 | |||
489 | return EOK; |
||
3521 | jermar | 490 | } |
491 | |||
492 | /** @} |
||
493 | */ |