Rev 4550 | Rev 4572 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 4550 | Rev 4553 | ||
|---|---|---|---|
| Line 44... | Line 44... | ||
| 44 | #include <sys/mman.h> |
44 | #include <sys/mman.h> |
| 45 | #include <async.h> |
45 | #include <async.h> |
| 46 | #include <ipc/ipc.h> |
46 | #include <ipc/ipc.h> |
| 47 | #include <as.h> |
47 | #include <as.h> |
| 48 | #include <assert.h> |
48 | #include <assert.h> |
| 49 | #include <futex.h> |
49 | #include <fibril_sync.h> |
| 50 | #include <adt/list.h> |
50 | #include <adt/list.h> |
| 51 | #include <adt/hash_table.h> |
51 | #include <adt/hash_table.h> |
| 52 | #include <mem.h> |
52 | #include <mem.h> |
| 53 | 53 | ||
| 54 | /** Lock protecting the device connection list */ |
54 | /** Lock protecting the device connection list */ |
| 55 | static futex_t dcl_lock = FUTEX_INITIALIZER; |
55 | static FIBRIL_MUTEX_INITIALIZE(dcl_lock); |
| 56 | /** Device connection list head. */ |
56 | /** Device connection list head. */ |
| 57 | static LIST_INITIALIZE(dcl_head); |
57 | static LIST_INITIALIZE(dcl_head); |
| 58 | 58 | ||
| 59 | #define CACHE_BUCKETS_LOG2 10 |
59 | #define CACHE_BUCKETS_LOG2 10 |
| 60 | #define CACHE_BUCKETS (1 << CACHE_BUCKETS_LOG2) |
60 | #define CACHE_BUCKETS (1 << CACHE_BUCKETS_LOG2) |
| 61 | 61 | ||
| 62 | typedef struct { |
62 | typedef struct { |
| 63 | futex_t lock; |
63 | fibril_mutex_t lock; |
| 64 | size_t block_size; /**< Block size. */ |
64 | size_t block_size; /**< Block size. */ |
| 65 | unsigned block_count; /**< Total number of blocks. */ |
65 | unsigned block_count; /**< Total number of blocks. */ |
| 66 | hash_table_t block_hash; |
66 | hash_table_t block_hash; |
| 67 | link_t free_head; |
67 | link_t free_head; |
| 68 | } cache_t; |
68 | } cache_t; |
| Line 81... | Line 81... | ||
| 81 | 81 | ||
| 82 | static devcon_t *devcon_search(dev_handle_t dev_handle) |
82 | static devcon_t *devcon_search(dev_handle_t dev_handle) |
| 83 | { |
83 | { |
| 84 | link_t *cur; |
84 | link_t *cur; |
| 85 | 85 | ||
| 86 | futex_down(&dcl_lock); |
86 | fibril_mutex_lock(&dcl_lock); |
| 87 | for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) { |
87 | for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) { |
| 88 | devcon_t *devcon = list_get_instance(cur, devcon_t, link); |
88 | devcon_t *devcon = list_get_instance(cur, devcon_t, link); |
| 89 | if (devcon->dev_handle == dev_handle) { |
89 | if (devcon->dev_handle == dev_handle) { |
| 90 | futex_up(&dcl_lock); |
90 | fibril_mutex_unlock(&dcl_lock); |
| 91 | return devcon; |
91 | return devcon; |
| 92 | } |
92 | } |
| 93 | } |
93 | } |
| 94 | futex_up(&dcl_lock); |
94 | fibril_mutex_unlock(&dcl_lock); |
| 95 | return NULL; |
95 | return NULL; |
| 96 | } |
96 | } |
| 97 | 97 | ||
| 98 | static int devcon_add(dev_handle_t dev_handle, int dev_phone, void *com_area, |
98 | static int devcon_add(dev_handle_t dev_handle, int dev_phone, void *com_area, |
| 99 | size_t com_size) |
99 | size_t com_size) |
| Line 113... | Line 113... | ||
| 113 | devcon->bb_buf = NULL; |
113 | devcon->bb_buf = NULL; |
| 114 | devcon->bb_off = 0; |
114 | devcon->bb_off = 0; |
| 115 | devcon->bb_size = 0; |
115 | devcon->bb_size = 0; |
| 116 | devcon->cache = NULL; |
116 | devcon->cache = NULL; |
| 117 | 117 | ||
| 118 | futex_down(&dcl_lock); |
118 | fibril_mutex_lock(&dcl_lock); |
| 119 | for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) { |
119 | for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) { |
| 120 | devcon_t *d = list_get_instance(cur, devcon_t, link); |
120 | devcon_t *d = list_get_instance(cur, devcon_t, link); |
| 121 | if (d->dev_handle == dev_handle) { |
121 | if (d->dev_handle == dev_handle) { |
| 122 | futex_up(&dcl_lock); |
122 | fibril_mutex_unlock(&dcl_lock); |
| 123 | free(devcon); |
123 | free(devcon); |
| 124 | return EEXIST; |
124 | return EEXIST; |
| 125 | } |
125 | } |
| 126 | } |
126 | } |
| 127 | list_append(&devcon->link, &dcl_head); |
127 | list_append(&devcon->link, &dcl_head); |
| 128 | futex_up(&dcl_lock); |
128 | fibril_mutex_unlock(&dcl_lock); |
| 129 | return EOK; |
129 | return EOK; |
| 130 | } |
130 | } |
| 131 | 131 | ||
| 132 | static void devcon_remove(devcon_t *devcon) |
132 | static void devcon_remove(devcon_t *devcon) |
| 133 | { |
133 | { |
| 134 | futex_down(&dcl_lock); |
134 | fibril_mutex_lock(&dcl_lock); |
| 135 | list_remove(&devcon->link); |
135 | list_remove(&devcon->link); |
| 136 | futex_up(&dcl_lock); |
136 | fibril_mutex_unlock(&dcl_lock); |
| 137 | } |
137 | } |
| 138 | 138 | ||
| 139 | int block_init(dev_handle_t dev_handle, size_t com_size) |
139 | int block_init(dev_handle_t dev_handle, size_t com_size) |
| 140 | { |
140 | { |
| 141 | int rc; |
141 | int rc; |
| Line 260... | Line 260... | ||
| 260 | return EEXIST; |
260 | return EEXIST; |
| 261 | cache = malloc(sizeof(cache_t)); |
261 | cache = malloc(sizeof(cache_t)); |
| 262 | if (!cache) |
262 | if (!cache) |
| 263 | return ENOMEM; |
263 | return ENOMEM; |
| 264 | 264 | ||
| 265 | futex_initialize(&cache->lock, 1); |
265 | fibril_mutex_initialize(&cache->lock); |
| 266 | list_initialize(&cache->free_head); |
266 | list_initialize(&cache->free_head); |
| 267 | cache->block_size = size; |
267 | cache->block_size = size; |
| 268 | cache->block_count = blocks; |
268 | cache->block_count = blocks; |
| 269 | 269 | ||
| 270 | if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1, |
270 | if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1, |
| Line 282... | Line 282... | ||
| 282 | return true; |
282 | return true; |
| 283 | } |
283 | } |
| 284 | 284 | ||
| 285 | static void block_initialize(block_t *b) |
285 | static void block_initialize(block_t *b) |
| 286 | { |
286 | { |
| 287 | futex_initialize(&b->lock, 1); |
287 | fibril_mutex_initialize(&b->lock); |
| 288 | b->refcnt = 1; |
288 | b->refcnt = 1; |
| 289 | b->dirty = false; |
289 | b->dirty = false; |
| 290 | rwlock_initialize(&b->contents_lock); |
290 | fibril_rwlock_initialize(&b->contents_lock); |
| 291 | link_initialize(&b->free_link); |
291 | link_initialize(&b->free_link); |
| 292 | link_initialize(&b->hash_link); |
292 | link_initialize(&b->hash_link); |
| 293 | } |
293 | } |
| 294 | 294 | ||
| 295 | /** Instantiate a block in memory and get a reference to it. |
295 | /** Instantiate a block in memory and get a reference to it. |
| Line 314... | Line 314... | ||
| 314 | 314 | ||
| 315 | assert(devcon); |
315 | assert(devcon); |
| 316 | assert(devcon->cache); |
316 | assert(devcon->cache); |
| 317 | 317 | ||
| 318 | cache = devcon->cache; |
318 | cache = devcon->cache; |
| 319 | futex_down(&cache->lock); |
319 | fibril_mutex_lock(&cache->lock); |
| 320 | l = hash_table_find(&cache->block_hash, &key); |
320 | l = hash_table_find(&cache->block_hash, &key); |
| 321 | if (l) { |
321 | if (l) { |
| 322 | /* |
322 | /* |
| 323 | * We found the block in the cache. |
323 | * We found the block in the cache. |
| 324 | */ |
324 | */ |
| 325 | b = hash_table_get_instance(l, block_t, hash_link); |
325 | b = hash_table_get_instance(l, block_t, hash_link); |
| 326 | futex_down(&b->lock); |
326 | fibril_mutex_lock(&b->lock); |
| 327 | if (b->refcnt++ == 0) |
327 | if (b->refcnt++ == 0) |
| 328 | list_remove(&b->free_link); |
328 | list_remove(&b->free_link); |
| 329 | futex_up(&b->lock); |
329 | fibril_mutex_unlock(&b->lock); |
| 330 | futex_up(&cache->lock); |
330 | fibril_mutex_unlock(&cache->lock); |
| 331 | } else { |
331 | } else { |
| 332 | /* |
332 | /* |
| 333 | * The block was not found in the cache. |
333 | * The block was not found in the cache. |
| 334 | */ |
334 | */ |
| 335 | int rc; |
335 | int rc; |
| Line 376... | Line 376... | ||
| 376 | /* |
376 | /* |
| 377 | * Lock the block before releasing the cache lock. Thus we don't |
377 | * Lock the block before releasing the cache lock. Thus we don't |
| 378 | * kill concurent operations on the cache while doing I/O on the |
378 | * kill concurent operations on the cache while doing I/O on the |
| 379 | * block. |
379 | * block. |
| 380 | */ |
380 | */ |
| 381 | futex_down(&b->lock); |
381 | fibril_mutex_lock(&b->lock); |
| 382 | futex_up(&cache->lock); |
382 | fibril_mutex_unlock(&cache->lock); |
| 383 | 383 | ||
| 384 | if (sync) { |
384 | if (sync) { |
| 385 | /* |
385 | /* |
| 386 | * The block is dirty and needs to be written back to |
386 | * The block is dirty and needs to be written back to |
| 387 | * the device before we can read in the new contents. |
387 | * the device before we can read in the new contents. |
| Line 391... | Line 391... | ||
| 391 | if (!(flags & BLOCK_FLAGS_NOREAD)) { |
391 | if (!(flags & BLOCK_FLAGS_NOREAD)) { |
| 392 | /* |
392 | /* |
| 393 | * The block contains old or no data. We need to read |
393 | * The block contains old or no data. We need to read |
| 394 | * the new contents from the device. |
394 | * the new contents from the device. |
| 395 | */ |
395 | */ |
| 396 | async_serialize_start(); |
- | |
| 397 | rc = block_read(dev_handle, &bufpos, &buflen, &pos, |
396 | rc = block_read(dev_handle, &bufpos, &buflen, &pos, |
| 398 | b->data, cache->block_size, cache->block_size); |
397 | b->data, cache->block_size, cache->block_size); |
| 399 | async_serialize_end(); |
- | |
| 400 | assert(rc == EOK); |
398 | assert(rc == EOK); |
| 401 | } |
399 | } |
| 402 | 400 | ||
| 403 | futex_up(&b->lock); |
401 | fibril_mutex_unlock(&b->lock); |
| 404 | } |
402 | } |
| 405 | return b; |
403 | return b; |
| 406 | } |
404 | } |
| 407 | 405 | ||
| 408 | /** Release a reference to a block. |
406 | /** Release a reference to a block. |
| Line 418... | Line 416... | ||
| 418 | 416 | ||
| 419 | assert(devcon); |
417 | assert(devcon); |
| 420 | assert(devcon->cache); |
418 | assert(devcon->cache); |
| 421 | 419 | ||
| 422 | cache = devcon->cache; |
420 | cache = devcon->cache; |
| 423 | futex_down(&cache->lock); |
421 | fibril_mutex_lock(&cache->lock); |
| 424 | futex_down(&block->lock); |
422 | fibril_mutex_lock(&block->lock); |
| 425 | if (!--block->refcnt) { |
423 | if (!--block->refcnt) { |
| 426 | /* |
424 | /* |
| 427 | * Last reference to the block was dropped, put the block on the |
425 | * Last reference to the block was dropped, put the block on the |
| 428 | * free list. |
426 | * free list. |
| 429 | */ |
427 | */ |
| 430 | list_append(&block->free_link, &cache->free_head); |
428 | list_append(&block->free_link, &cache->free_head); |
| 431 | } |
429 | } |
| 432 | futex_up(&block->lock); |
430 | fibril_mutex_unlock(&block->lock); |
| 433 | futex_up(&cache->lock); |
431 | fibril_mutex_unlock(&cache->lock); |
| 434 | } |
432 | } |
| 435 | 433 | ||
| 436 | /** Read data from a block device. |
434 | /** Read data from a block device. |
| 437 | * |
435 | * |
| 438 | * @param dev_handle Device handle of the block device. |
436 | * @param dev_handle Device handle of the block device. |