source: mainline/uspace/lib/libblock/libblock.c@ c91f2d1b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c91f2d1b was c91f2d1b, checked in by Jakub Jermar <jakub@…>, 16 years ago

Enable the block_get() and block_put() APIs to return error.

  • Property mode set to 100644
File size: 16.4 KB
Line 
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 <ipc/devmap.h>
41#include <ipc/bd.h>
42#include <ipc/services.h>
43#include <errno.h>
44#include <sys/mman.h>
45#include <async.h>
46#include <ipc/ipc.h>
47#include <as.h>
48#include <assert.h>
49#include <fibril_sync.h>
50#include <adt/list.h>
51#include <adt/hash_table.h>
52#include <mem.h>
53
54/** Lock protecting the device connection list */
55static FIBRIL_MUTEX_INITIALIZE(dcl_lock);
56/** Device connection list head. */
57static LIST_INITIALIZE(dcl_head);
58
59#define CACHE_BUCKETS_LOG2 10
60#define CACHE_BUCKETS (1 << CACHE_BUCKETS_LOG2)
61
62typedef struct {
63 fibril_mutex_t lock;
64 size_t block_size; /**< Block size. */
65 unsigned block_count; /**< Total number of blocks. */
66 unsigned blocks_cached; /**< Number of cached blocks. */
67 hash_table_t block_hash;
68 link_t free_head;
69 enum cache_mode mode;
70} cache_t;
71
72typedef struct {
73 link_t link;
74 dev_handle_t dev_handle;
75 int dev_phone;
76 fibril_mutex_t com_area_lock;
77 void *com_area;
78 size_t com_size;
79 void *bb_buf;
80 off_t bb_off;
81 size_t bb_size;
82 cache_t *cache;
83} devcon_t;
84
85static int read_block(devcon_t *devcon, bn_t boff, size_t block_size);
86static int write_block(devcon_t *devcon, bn_t boff, size_t block_size);
87
88static devcon_t *devcon_search(dev_handle_t dev_handle)
89{
90 link_t *cur;
91
92 fibril_mutex_lock(&dcl_lock);
93 for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
94 devcon_t *devcon = list_get_instance(cur, devcon_t, link);
95 if (devcon->dev_handle == dev_handle) {
96 fibril_mutex_unlock(&dcl_lock);
97 return devcon;
98 }
99 }
100 fibril_mutex_unlock(&dcl_lock);
101 return NULL;
102}
103
104static int devcon_add(dev_handle_t dev_handle, int dev_phone, void *com_area,
105 size_t com_size)
106{
107 link_t *cur;
108 devcon_t *devcon;
109
110 devcon = malloc(sizeof(devcon_t));
111 if (!devcon)
112 return ENOMEM;
113
114 link_initialize(&devcon->link);
115 devcon->dev_handle = dev_handle;
116 devcon->dev_phone = dev_phone;
117 fibril_mutex_initialize(&devcon->com_area_lock);
118 devcon->com_area = com_area;
119 devcon->com_size = com_size;
120 devcon->bb_buf = NULL;
121 devcon->bb_off = 0;
122 devcon->bb_size = 0;
123 devcon->cache = NULL;
124
125 fibril_mutex_lock(&dcl_lock);
126 for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
127 devcon_t *d = list_get_instance(cur, devcon_t, link);
128 if (d->dev_handle == dev_handle) {
129 fibril_mutex_unlock(&dcl_lock);
130 free(devcon);
131 return EEXIST;
132 }
133 }
134 list_append(&devcon->link, &dcl_head);
135 fibril_mutex_unlock(&dcl_lock);
136 return EOK;
137}
138
139static void devcon_remove(devcon_t *devcon)
140{
141 fibril_mutex_lock(&dcl_lock);
142 list_remove(&devcon->link);
143 fibril_mutex_unlock(&dcl_lock);
144}
145
146int block_init(dev_handle_t dev_handle, size_t com_size)
147{
148 int rc;
149 int dev_phone;
150 void *com_area;
151
152 com_area = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE,
153 MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
154 if (!com_area) {
155 return ENOMEM;
156 }
157
158 dev_phone = devmap_device_connect(dev_handle, IPC_FLAG_BLOCKING);
159 if (dev_phone < 0) {
160 munmap(com_area, com_size);
161 return dev_phone;
162 }
163
164 rc = ipc_share_out_start(dev_phone, com_area,
165 AS_AREA_READ | AS_AREA_WRITE);
166 if (rc != EOK) {
167 munmap(com_area, com_size);
168 ipc_hangup(dev_phone);
169 return rc;
170 }
171
172 rc = devcon_add(dev_handle, dev_phone, com_area, com_size);
173 if (rc != EOK) {
174 munmap(com_area, com_size);
175 ipc_hangup(dev_phone);
176 return rc;
177 }
178
179 return EOK;
180}
181
182void block_fini(dev_handle_t dev_handle)
183{
184 devcon_t *devcon = devcon_search(dev_handle);
185 assert(devcon);
186
187 devcon_remove(devcon);
188
189 if (devcon->bb_buf)
190 free(devcon->bb_buf);
191
192 if (devcon->cache) {
193 hash_table_destroy(&devcon->cache->block_hash);
194 free(devcon->cache);
195 }
196
197 munmap(devcon->com_area, devcon->com_size);
198 ipc_hangup(devcon->dev_phone);
199
200 free(devcon);
201}
202
203int block_bb_read(dev_handle_t dev_handle, off_t off, size_t size)
204{
205 void *bb_buf;
206 int rc;
207
208 devcon_t *devcon = devcon_search(dev_handle);
209 if (!devcon)
210 return ENOENT;
211 if (devcon->bb_buf)
212 return EEXIST;
213 bb_buf = malloc(size);
214 if (!bb_buf)
215 return ENOMEM;
216
217 fibril_mutex_lock(&devcon->com_area_lock);
218 rc = read_block(devcon, 0, size);
219 if (rc != EOK) {
220 fibril_mutex_unlock(&devcon->com_area_lock);
221 free(bb_buf);
222 return rc;
223 }
224 memcpy(bb_buf, devcon->com_area, size);
225 fibril_mutex_unlock(&devcon->com_area_lock);
226
227 devcon->bb_buf = bb_buf;
228 devcon->bb_off = off;
229 devcon->bb_size = size;
230
231 return EOK;
232}
233
234void *block_bb_get(dev_handle_t dev_handle)
235{
236 devcon_t *devcon = devcon_search(dev_handle);
237 assert(devcon);
238 return devcon->bb_buf;
239}
240
241static hash_index_t cache_hash(unsigned long *key)
242{
243 return *key & (CACHE_BUCKETS - 1);
244}
245
246static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item)
247{
248 block_t *b = hash_table_get_instance(item, block_t, hash_link);
249 return b->boff == *key;
250}
251
252static void cache_remove_callback(link_t *item)
253{
254}
255
256static hash_table_operations_t cache_ops = {
257 .hash = cache_hash,
258 .compare = cache_compare,
259 .remove_callback = cache_remove_callback
260};
261
262int block_cache_init(dev_handle_t dev_handle, size_t size, unsigned blocks,
263 enum cache_mode mode)
264{
265 devcon_t *devcon = devcon_search(dev_handle);
266 cache_t *cache;
267 if (!devcon)
268 return ENOENT;
269 if (devcon->cache)
270 return EEXIST;
271 cache = malloc(sizeof(cache_t));
272 if (!cache)
273 return ENOMEM;
274
275 fibril_mutex_initialize(&cache->lock);
276 list_initialize(&cache->free_head);
277 cache->block_size = size;
278 cache->block_count = blocks;
279 cache->blocks_cached = 0;
280 cache->mode = mode;
281
282 if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1,
283 &cache_ops)) {
284 free(cache);
285 return ENOMEM;
286 }
287
288 devcon->cache = cache;
289 return EOK;
290}
291
292#define CACHE_LO_WATERMARK 10
293#define CACHE_HI_WATERMARK 20
294static bool cache_can_grow(cache_t *cache)
295{
296 if (cache->blocks_cached < CACHE_LO_WATERMARK)
297 return true;
298 if (!list_empty(&cache->free_head))
299 return false;
300 return true;
301}
302
303static void block_initialize(block_t *b)
304{
305 fibril_mutex_initialize(&b->lock);
306 b->refcnt = 1;
307 b->dirty = false;
308 fibril_rwlock_initialize(&b->contents_lock);
309 link_initialize(&b->free_link);
310 link_initialize(&b->hash_link);
311}
312
313/** Instantiate a block in memory and get a reference to it.
314 *
315 * @param block Pointer to where the function will store the
316 * block pointer on success.
317 * @param dev_handle Device handle of the block device.
318 * @param boff Block offset.
319 * @param flags If BLOCK_FLAGS_NOREAD is specified, block_get()
320 * will not read the contents of the block from the
321 * device.
322 *
323 * @return EOK on success or a negative error code.
324 */
325int block_get(block_t **block, dev_handle_t dev_handle, bn_t boff, int flags)
326{
327 devcon_t *devcon;
328 cache_t *cache;
329 block_t *b;
330 link_t *l;
331 unsigned long key = boff;
332
333 devcon = devcon_search(dev_handle);
334
335 assert(devcon);
336 assert(devcon->cache);
337
338 cache = devcon->cache;
339
340retry:
341 fibril_mutex_lock(&cache->lock);
342 l = hash_table_find(&cache->block_hash, &key);
343 if (l) {
344 /*
345 * We found the block in the cache.
346 */
347 b = hash_table_get_instance(l, block_t, hash_link);
348 fibril_mutex_lock(&b->lock);
349 if (b->refcnt++ == 0)
350 list_remove(&b->free_link);
351 fibril_mutex_unlock(&b->lock);
352 fibril_mutex_unlock(&cache->lock);
353 } else {
354 /*
355 * The block was not found in the cache.
356 */
357 int rc;
358
359 if (cache_can_grow(cache)) {
360 /*
361 * We can grow the cache by allocating new blocks.
362 * Should the allocation fail, we fail over and try to
363 * recycle a block from the cache.
364 */
365 b = malloc(sizeof(block_t));
366 if (!b)
367 goto recycle;
368 b->data = malloc(cache->block_size);
369 if (!b->data) {
370 free(b);
371 goto recycle;
372 }
373 cache->blocks_cached++;
374 } else {
375 /*
376 * Try to recycle a block from the free list.
377 */
378 unsigned long temp_key;
379recycle:
380 assert(!list_empty(&cache->free_head));
381 l = cache->free_head.next;
382 b = list_get_instance(l, block_t, free_link);
383
384 fibril_mutex_lock(&b->lock);
385 if (b->dirty) {
386 /*
387 * The block needs to be written back to the
388 * device before it changes identity. Do this
389 * while not holding the cache lock so that
390 * concurrency is not impeded. Also move the
391 * block to the end of the free list so that we
392 * do not slow down other instances of
393 * block_get() draining the free list.
394 */
395 list_remove(&b->free_link);
396 list_append(&b->free_link, &cache->free_head);
397 fibril_mutex_unlock(&cache->lock);
398 fibril_mutex_lock(&devcon->com_area_lock);
399 memcpy(devcon->com_area, b->data, b->size);
400 rc = write_block(devcon, b->boff,
401 cache->block_size);
402 fibril_mutex_unlock(&devcon->com_area_lock);
403 assert(rc == EOK);
404 b->dirty = false;
405 if (!fibril_mutex_trylock(&cache->lock)) {
406 /*
407 * Somebody is probably racing with us.
408 * Unlock the block and retry.
409 */
410 fibril_mutex_unlock(&b->lock);
411 goto retry;
412 }
413
414 }
415 fibril_mutex_unlock(&b->lock);
416
417 /*
418 * Unlink the block from the free list and the hash
419 * table.
420 */
421 list_remove(&b->free_link);
422 temp_key = b->boff;
423 hash_table_remove(&cache->block_hash, &temp_key, 1);
424 }
425
426 block_initialize(b);
427 b->dev_handle = dev_handle;
428 b->size = cache->block_size;
429 b->boff = boff;
430 hash_table_insert(&cache->block_hash, &key, &b->hash_link);
431
432 /*
433 * Lock the block before releasing the cache lock. Thus we don't
434 * kill concurrent operations on the cache while doing I/O on
435 * the block.
436 */
437 fibril_mutex_lock(&b->lock);
438 fibril_mutex_unlock(&cache->lock);
439
440 if (!(flags & BLOCK_FLAGS_NOREAD)) {
441 /*
442 * The block contains old or no data. We need to read
443 * the new contents from the device.
444 */
445 fibril_mutex_lock(&devcon->com_area_lock);
446 rc = read_block(devcon, b->boff, cache->block_size);
447 assert(rc == EOK);
448 memcpy(b->data, devcon->com_area, cache->block_size);
449 fibril_mutex_unlock(&devcon->com_area_lock);
450 }
451
452 fibril_mutex_unlock(&b->lock);
453 }
454 *block = b;
455 return EOK;
456}
457
458/** Release a reference to a block.
459 *
460 * If the last reference is dropped, the block is put on the free list.
461 *
462 * @param block Block of which a reference is to be released.
463 *
464 * @return EOK on success or a negative error code.
465 */
466int block_put(block_t *block)
467{
468 devcon_t *devcon = devcon_search(block->dev_handle);
469 cache_t *cache;
470 unsigned blocks_cached;
471 enum cache_mode mode;
472 int rc;
473
474 assert(devcon);
475 assert(devcon->cache);
476
477 cache = devcon->cache;
478
479retry:
480 fibril_mutex_lock(&cache->lock);
481 blocks_cached = cache->blocks_cached;
482 mode = cache->mode;
483 fibril_mutex_unlock(&cache->lock);
484
485 /*
486 * Determine whether to sync the block. Syncing the block is best done
487 * when not holding the cache lock as it does not impede concurrency.
488 * Since the situation may have changed when we unlocked the cache, the
489 * blocks_cached and mode variables are mere hints. We will recheck the
490 * conditions later when the cache lock is held again.
491 */
492 fibril_mutex_lock(&block->lock);
493 if (block->dirty && (block->refcnt == 1) &&
494 (blocks_cached > CACHE_HI_WATERMARK || mode != CACHE_MODE_WB)) {
495 fibril_mutex_lock(&devcon->com_area_lock);
496 memcpy(devcon->com_area, block->data, block->size);
497 rc = write_block(devcon, block->boff, block->size);
498 assert(rc == EOK);
499 fibril_mutex_unlock(&devcon->com_area_lock);
500 block->dirty = false;
501 }
502 fibril_mutex_unlock(&block->lock);
503
504 fibril_mutex_lock(&cache->lock);
505 fibril_mutex_lock(&block->lock);
506 if (!--block->refcnt) {
507 /*
508 * Last reference to the block was dropped. Either free the
509 * block or put it on the free list.
510 */
511 if (cache->blocks_cached > CACHE_HI_WATERMARK) {
512 /*
513 * Currently there are too many cached blocks.
514 */
515 if (block->dirty) {
516 /*
517 * We cannot sync the block while holding the
518 * cache lock. Release everything and retry.
519 */
520 block->refcnt++;
521 fibril_mutex_unlock(&block->lock);
522 fibril_mutex_unlock(&cache->lock);
523 goto retry;
524 }
525 /*
526 * Take the block out of the cache and free it.
527 */
528 unsigned long key = block->boff;
529 hash_table_remove(&cache->block_hash, &key, 1);
530 free(block);
531 free(block->data);
532 cache->blocks_cached--;
533 fibril_mutex_unlock(&cache->lock);
534 return;
535 }
536 /*
537 * Put the block on the free list.
538 */
539 if (cache->mode != CACHE_MODE_WB && block->dirty) {
540 /*
541 * We cannot sync the block while holding the cache
542 * lock. Release everything and retry.
543 */
544 block->refcnt++;
545 fibril_mutex_unlock(&block->lock);
546 fibril_mutex_unlock(&cache->lock);
547 goto retry;
548 }
549 list_append(&block->free_link, &cache->free_head);
550 }
551 fibril_mutex_unlock(&block->lock);
552 fibril_mutex_unlock(&cache->lock);
553
554 return EOK;
555}
556
557/** Read sequential data from a block device.
558 *
559 * @param dev_handle Device handle of the block device.
560 * @param bufpos Pointer to the first unread valid offset within the
561 * communication buffer.
562 * @param buflen Pointer to the number of unread bytes that are ready in
563 * the communication buffer.
564 * @param pos Device position to be read.
565 * @param dst Destination buffer.
566 * @param size Size of the destination buffer.
567 * @param block_size Block size to be used for the transfer.
568 *
569 * @return EOK on success or a negative return code on failure.
570 */
571int block_seqread(dev_handle_t dev_handle, off_t *bufpos, size_t *buflen,
572 off_t *pos, void *dst, size_t size, size_t block_size)
573{
574 off_t offset = 0;
575 size_t left = size;
576 devcon_t *devcon = devcon_search(dev_handle);
577 assert(devcon);
578
579 fibril_mutex_lock(&devcon->com_area_lock);
580 while (left > 0) {
581 size_t rd;
582
583 if (*bufpos + left < *buflen)
584 rd = left;
585 else
586 rd = *buflen - *bufpos;
587
588 if (rd > 0) {
589 /*
590 * Copy the contents of the communication buffer to the
591 * destination buffer.
592 */
593 memcpy(dst + offset, devcon->com_area + *bufpos, rd);
594 offset += rd;
595 *bufpos += rd;
596 *pos += rd;
597 left -= rd;
598 }
599
600 if (*bufpos == (off_t) *buflen) {
601 /* Refill the communication buffer with a new block. */
602 int rc;
603
604 rc = read_block(devcon, *pos / block_size, block_size);
605 if (rc != EOK) {
606 fibril_mutex_unlock(&devcon->com_area_lock);
607 return rc;
608 }
609
610 *bufpos = 0;
611 *buflen = block_size;
612 }
613 }
614 fibril_mutex_unlock(&devcon->com_area_lock);
615
616 return EOK;
617}
618
619/** Read block from block device.
620 *
621 * @param devcon Device connection.
622 * @param boff Block index.
623 * @param block_size Block size.
624 * @param src Buffer for storing the data.
625 *
626 * @return EOK on success or negative error code on failure.
627 */
628static int read_block(devcon_t *devcon, bn_t boff, size_t block_size)
629{
630 ipcarg_t retval;
631 int rc;
632
633 assert(devcon);
634 rc = async_req_2_1(devcon->dev_phone, BD_READ_BLOCK, boff, block_size,
635 &retval);
636 if ((rc != EOK) || (retval != EOK))
637 return (rc != EOK ? rc : (int) retval);
638
639 return EOK;
640}
641
642/** Write block to block device.
643 *
644 * @param devcon Device connection.
645 * @param boff Block index.
646 * @param block_size Block size.
647 * @param src Buffer containing the data to write.
648 *
649 * @return EOK on success or negative error code on failure.
650 */
651static int write_block(devcon_t *devcon, bn_t boff, size_t block_size)
652{
653 ipcarg_t retval;
654 int rc;
655
656 assert(devcon);
657 rc = async_req_2_1(devcon->dev_phone, BD_WRITE_BLOCK, boff, block_size,
658 &retval);
659 if ((rc != EOK) || (retval != EOK))
660 return (rc != EOK ? rc : (int) retval);
661
662 return EOK;
663}
664
665/** @}
666 */
Note: See TracBrowser for help on using the repository browser.