source: mainline/uspace/lib/block/libblock.c@ 326d86e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 326d86e was c7bbf029, checked in by Martin Decky <martin@…>, 14 years ago

improve stack traces and assertions
reduce header files pollution

  • Property mode set to 100644
File size: 22.7 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 <as.h>
47#include <assert.h>
48#include <fibril_synch.h>
49#include <adt/list.h>
50#include <adt/hash_table.h>
51#include <macros.h>
52#include <mem.h>
53#include <malloc.h>
54#include <stdio.h>
55#include <sys/typefmt.h>
56#include <stacktrace.h>
57
58/** Lock protecting the device connection list */
59static FIBRIL_MUTEX_INITIALIZE(dcl_lock);
60/** Device connection list head. */
61static LIST_INITIALIZE(dcl_head);
62
63#define CACHE_BUCKETS_LOG2 10
64#define CACHE_BUCKETS (1 << CACHE_BUCKETS_LOG2)
65
66typedef struct {
67 fibril_mutex_t lock;
68 size_t lblock_size; /**< Logical block size. */
69 unsigned blocks_cluster; /**< Physical blocks per block_t */
70 unsigned block_count; /**< Total number of blocks. */
71 unsigned blocks_cached; /**< Number of cached blocks. */
72 hash_table_t block_hash;
73 link_t free_head;
74 enum cache_mode mode;
75} cache_t;
76
77typedef struct {
78 link_t link;
79 devmap_handle_t devmap_handle;
80 int dev_phone;
81 fibril_mutex_t comm_area_lock;
82 void *comm_area;
83 size_t comm_size;
84 void *bb_buf;
85 aoff64_t bb_addr;
86 size_t pblock_size; /**< Physical block size. */
87 cache_t *cache;
88} devcon_t;
89
90static int read_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt);
91static int write_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt);
92static int get_block_size(int dev_phone, size_t *bsize);
93static int get_num_blocks(int dev_phone, aoff64_t *nblocks);
94static aoff64_t ba_ltop(devcon_t *devcon, aoff64_t lba);
95
96static devcon_t *devcon_search(devmap_handle_t devmap_handle)
97{
98 link_t *cur;
99
100 fibril_mutex_lock(&dcl_lock);
101 for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
102 devcon_t *devcon = list_get_instance(cur, devcon_t, link);
103 if (devcon->devmap_handle == devmap_handle) {
104 fibril_mutex_unlock(&dcl_lock);
105 return devcon;
106 }
107 }
108 fibril_mutex_unlock(&dcl_lock);
109 return NULL;
110}
111
112static int devcon_add(devmap_handle_t devmap_handle, int dev_phone, size_t bsize,
113 void *comm_area, size_t comm_size)
114{
115 link_t *cur;
116 devcon_t *devcon;
117
118 if (comm_size < bsize)
119 return EINVAL;
120
121 devcon = malloc(sizeof(devcon_t));
122 if (!devcon)
123 return ENOMEM;
124
125 link_initialize(&devcon->link);
126 devcon->devmap_handle = devmap_handle;
127 devcon->dev_phone = dev_phone;
128 fibril_mutex_initialize(&devcon->comm_area_lock);
129 devcon->comm_area = comm_area;
130 devcon->comm_size = comm_size;
131 devcon->bb_buf = NULL;
132 devcon->bb_addr = 0;
133 devcon->pblock_size = bsize;
134 devcon->cache = NULL;
135
136 fibril_mutex_lock(&dcl_lock);
137 for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
138 devcon_t *d = list_get_instance(cur, devcon_t, link);
139 if (d->devmap_handle == devmap_handle) {
140 fibril_mutex_unlock(&dcl_lock);
141 free(devcon);
142 return EEXIST;
143 }
144 }
145 list_append(&devcon->link, &dcl_head);
146 fibril_mutex_unlock(&dcl_lock);
147 return EOK;
148}
149
150static void devcon_remove(devcon_t *devcon)
151{
152 fibril_mutex_lock(&dcl_lock);
153 list_remove(&devcon->link);
154 fibril_mutex_unlock(&dcl_lock);
155}
156
157int block_init(devmap_handle_t devmap_handle, size_t comm_size)
158{
159 int rc;
160 int dev_phone;
161 void *comm_area;
162 size_t bsize;
163
164 comm_area = mmap(NULL, comm_size, PROTO_READ | PROTO_WRITE,
165 MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
166 if (!comm_area) {
167 return ENOMEM;
168 }
169
170 dev_phone = devmap_device_connect(devmap_handle, IPC_FLAG_BLOCKING);
171 if (dev_phone < 0) {
172 munmap(comm_area, comm_size);
173 return dev_phone;
174 }
175
176 rc = async_share_out_start(dev_phone, comm_area,
177 AS_AREA_READ | AS_AREA_WRITE);
178 if (rc != EOK) {
179 munmap(comm_area, comm_size);
180 async_hangup(dev_phone);
181 return rc;
182 }
183
184 if (get_block_size(dev_phone, &bsize) != EOK) {
185 munmap(comm_area, comm_size);
186 async_hangup(dev_phone);
187 return rc;
188 }
189
190 rc = devcon_add(devmap_handle, dev_phone, bsize, comm_area, comm_size);
191 if (rc != EOK) {
192 munmap(comm_area, comm_size);
193 async_hangup(dev_phone);
194 return rc;
195 }
196
197 return EOK;
198}
199
200void block_fini(devmap_handle_t devmap_handle)
201{
202 devcon_t *devcon = devcon_search(devmap_handle);
203 assert(devcon);
204
205 if (devcon->cache)
206 (void) block_cache_fini(devmap_handle);
207
208 devcon_remove(devcon);
209
210 if (devcon->bb_buf)
211 free(devcon->bb_buf);
212
213 munmap(devcon->comm_area, devcon->comm_size);
214 async_hangup(devcon->dev_phone);
215
216 free(devcon);
217}
218
219int block_bb_read(devmap_handle_t devmap_handle, aoff64_t ba)
220{
221 void *bb_buf;
222 int rc;
223
224 devcon_t *devcon = devcon_search(devmap_handle);
225 if (!devcon)
226 return ENOENT;
227 if (devcon->bb_buf)
228 return EEXIST;
229 bb_buf = malloc(devcon->pblock_size);
230 if (!bb_buf)
231 return ENOMEM;
232
233 fibril_mutex_lock(&devcon->comm_area_lock);
234 rc = read_blocks(devcon, 0, 1);
235 if (rc != EOK) {
236 fibril_mutex_unlock(&devcon->comm_area_lock);
237 free(bb_buf);
238 return rc;
239 }
240 memcpy(bb_buf, devcon->comm_area, devcon->pblock_size);
241 fibril_mutex_unlock(&devcon->comm_area_lock);
242
243 devcon->bb_buf = bb_buf;
244 devcon->bb_addr = ba;
245
246 return EOK;
247}
248
249void *block_bb_get(devmap_handle_t devmap_handle)
250{
251 devcon_t *devcon = devcon_search(devmap_handle);
252 assert(devcon);
253 return devcon->bb_buf;
254}
255
256static hash_index_t cache_hash(unsigned long *key)
257{
258 return *key & (CACHE_BUCKETS - 1);
259}
260
261static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item)
262{
263 block_t *b = hash_table_get_instance(item, block_t, hash_link);
264 return b->lba == *key;
265}
266
267static void cache_remove_callback(link_t *item)
268{
269}
270
271static hash_table_operations_t cache_ops = {
272 .hash = cache_hash,
273 .compare = cache_compare,
274 .remove_callback = cache_remove_callback
275};
276
277int block_cache_init(devmap_handle_t devmap_handle, size_t size, unsigned blocks,
278 enum cache_mode mode)
279{
280 devcon_t *devcon = devcon_search(devmap_handle);
281 cache_t *cache;
282 if (!devcon)
283 return ENOENT;
284 if (devcon->cache)
285 return EEXIST;
286 cache = malloc(sizeof(cache_t));
287 if (!cache)
288 return ENOMEM;
289
290 fibril_mutex_initialize(&cache->lock);
291 list_initialize(&cache->free_head);
292 cache->lblock_size = size;
293 cache->block_count = blocks;
294 cache->blocks_cached = 0;
295 cache->mode = mode;
296
297 /* Allow 1:1 or small-to-large block size translation */
298 if (cache->lblock_size % devcon->pblock_size != 0) {
299 free(cache);
300 return ENOTSUP;
301 }
302
303 cache->blocks_cluster = cache->lblock_size / devcon->pblock_size;
304
305 if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1,
306 &cache_ops)) {
307 free(cache);
308 return ENOMEM;
309 }
310
311 devcon->cache = cache;
312 return EOK;
313}
314
315int block_cache_fini(devmap_handle_t devmap_handle)
316{
317 devcon_t *devcon = devcon_search(devmap_handle);
318 cache_t *cache;
319 int rc;
320
321 if (!devcon)
322 return ENOENT;
323 if (!devcon->cache)
324 return EOK;
325 cache = devcon->cache;
326
327 /*
328 * We are expecting to find all blocks for this device handle on the
329 * free list, i.e. the block reference count should be zero. Do not
330 * bother with the cache and block locks because we are single-threaded.
331 */
332 while (!list_empty(&cache->free_head)) {
333 block_t *b = list_get_instance(cache->free_head.next,
334 block_t, free_link);
335
336 list_remove(&b->free_link);
337 if (b->dirty) {
338 memcpy(devcon->comm_area, b->data, b->size);
339 rc = write_blocks(devcon, b->pba, cache->blocks_cluster);
340 if (rc != EOK)
341 return rc;
342 }
343
344 unsigned long key = b->lba;
345 hash_table_remove(&cache->block_hash, &key, 1);
346
347 free(b->data);
348 free(b);
349 }
350
351 hash_table_destroy(&cache->block_hash);
352 devcon->cache = NULL;
353 free(cache);
354
355 return EOK;
356}
357
358#define CACHE_LO_WATERMARK 10
359#define CACHE_HI_WATERMARK 20
360static bool cache_can_grow(cache_t *cache)
361{
362 if (cache->blocks_cached < CACHE_LO_WATERMARK)
363 return true;
364 if (!list_empty(&cache->free_head))
365 return false;
366 return true;
367}
368
369static void block_initialize(block_t *b)
370{
371 fibril_mutex_initialize(&b->lock);
372 b->refcnt = 1;
373 b->dirty = false;
374 b->toxic = false;
375 fibril_rwlock_initialize(&b->contents_lock);
376 link_initialize(&b->free_link);
377 link_initialize(&b->hash_link);
378}
379
380/** Instantiate a block in memory and get a reference to it.
381 *
382 * @param block Pointer to where the function will store the
383 * block pointer on success.
384 * @param devmap_handle Device handle of the block device.
385 * @param ba Block address (logical).
386 * @param flags If BLOCK_FLAGS_NOREAD is specified, block_get()
387 * will not read the contents of the block from the
388 * device.
389 *
390 * @return EOK on success or a negative error code.
391 */
392int block_get(block_t **block, devmap_handle_t devmap_handle, aoff64_t ba, int flags)
393{
394 devcon_t *devcon;
395 cache_t *cache;
396 block_t *b;
397 link_t *l;
398 unsigned long key = ba;
399 int rc;
400
401 devcon = devcon_search(devmap_handle);
402
403 assert(devcon);
404 assert(devcon->cache);
405
406 cache = devcon->cache;
407
408retry:
409 rc = EOK;
410 b = NULL;
411
412 fibril_mutex_lock(&cache->lock);
413 l = hash_table_find(&cache->block_hash, &key);
414 if (l) {
415found:
416 /*
417 * We found the block in the cache.
418 */
419 b = hash_table_get_instance(l, block_t, hash_link);
420 fibril_mutex_lock(&b->lock);
421 if (b->refcnt++ == 0)
422 list_remove(&b->free_link);
423 if (b->toxic)
424 rc = EIO;
425 fibril_mutex_unlock(&b->lock);
426 fibril_mutex_unlock(&cache->lock);
427 } else {
428 /*
429 * The block was not found in the cache.
430 */
431 if (cache_can_grow(cache)) {
432 /*
433 * We can grow the cache by allocating new blocks.
434 * Should the allocation fail, we fail over and try to
435 * recycle a block from the cache.
436 */
437 b = malloc(sizeof(block_t));
438 if (!b)
439 goto recycle;
440 b->data = malloc(cache->lblock_size);
441 if (!b->data) {
442 free(b);
443 b = NULL;
444 goto recycle;
445 }
446 cache->blocks_cached++;
447 } else {
448 /*
449 * Try to recycle a block from the free list.
450 */
451 unsigned long temp_key;
452recycle:
453 if (list_empty(&cache->free_head)) {
454 fibril_mutex_unlock(&cache->lock);
455 rc = ENOMEM;
456 goto out;
457 }
458 l = cache->free_head.next;
459 b = list_get_instance(l, block_t, free_link);
460
461 fibril_mutex_lock(&b->lock);
462 if (b->dirty) {
463 /*
464 * The block needs to be written back to the
465 * device before it changes identity. Do this
466 * while not holding the cache lock so that
467 * concurrency is not impeded. Also move the
468 * block to the end of the free list so that we
469 * do not slow down other instances of
470 * block_get() draining the free list.
471 */
472 list_remove(&b->free_link);
473 list_append(&b->free_link, &cache->free_head);
474 fibril_mutex_unlock(&cache->lock);
475 fibril_mutex_lock(&devcon->comm_area_lock);
476 memcpy(devcon->comm_area, b->data, b->size);
477 rc = write_blocks(devcon, b->pba,
478 cache->blocks_cluster);
479 fibril_mutex_unlock(&devcon->comm_area_lock);
480 if (rc != EOK) {
481 /*
482 * We did not manage to write the block
483 * to the device. Keep it around for
484 * another try. Hopefully, we will grab
485 * another block next time.
486 */
487 fibril_mutex_unlock(&b->lock);
488 goto retry;
489 }
490 b->dirty = false;
491 if (!fibril_mutex_trylock(&cache->lock)) {
492 /*
493 * Somebody is probably racing with us.
494 * Unlock the block and retry.
495 */
496 fibril_mutex_unlock(&b->lock);
497 goto retry;
498 }
499 l = hash_table_find(&cache->block_hash, &key);
500 if (l) {
501 /*
502 * Someone else must have already
503 * instantiated the block while we were
504 * not holding the cache lock.
505 * Leave the recycled block on the
506 * freelist and continue as if we
507 * found the block of interest during
508 * the first try.
509 */
510 fibril_mutex_unlock(&b->lock);
511 goto found;
512 }
513
514 }
515 fibril_mutex_unlock(&b->lock);
516
517 /*
518 * Unlink the block from the free list and the hash
519 * table.
520 */
521 list_remove(&b->free_link);
522 temp_key = b->lba;
523 hash_table_remove(&cache->block_hash, &temp_key, 1);
524 }
525
526 block_initialize(b);
527 b->devmap_handle = devmap_handle;
528 b->size = cache->lblock_size;
529 b->lba = ba;
530 b->pba = ba_ltop(devcon, b->lba);
531 hash_table_insert(&cache->block_hash, &key, &b->hash_link);
532
533 /*
534 * Lock the block before releasing the cache lock. Thus we don't
535 * kill concurrent operations on the cache while doing I/O on
536 * the block.
537 */
538 fibril_mutex_lock(&b->lock);
539 fibril_mutex_unlock(&cache->lock);
540
541 if (!(flags & BLOCK_FLAGS_NOREAD)) {
542 /*
543 * The block contains old or no data. We need to read
544 * the new contents from the device.
545 */
546 fibril_mutex_lock(&devcon->comm_area_lock);
547 rc = read_blocks(devcon, b->pba, cache->blocks_cluster);
548 memcpy(b->data, devcon->comm_area, cache->lblock_size);
549 fibril_mutex_unlock(&devcon->comm_area_lock);
550 if (rc != EOK)
551 b->toxic = true;
552 } else
553 rc = EOK;
554
555 fibril_mutex_unlock(&b->lock);
556 }
557out:
558 if ((rc != EOK) && b) {
559 assert(b->toxic);
560 (void) block_put(b);
561 b = NULL;
562 }
563 *block = b;
564 return rc;
565}
566
567/** Release a reference to a block.
568 *
569 * If the last reference is dropped, the block is put on the free list.
570 *
571 * @param block Block of which a reference is to be released.
572 *
573 * @return EOK on success or a negative error code.
574 */
575int block_put(block_t *block)
576{
577 devcon_t *devcon = devcon_search(block->devmap_handle);
578 cache_t *cache;
579 unsigned blocks_cached;
580 enum cache_mode mode;
581 int rc = EOK;
582
583 assert(devcon);
584 assert(devcon->cache);
585 assert(block->refcnt >= 1);
586
587 cache = devcon->cache;
588
589retry:
590 fibril_mutex_lock(&cache->lock);
591 blocks_cached = cache->blocks_cached;
592 mode = cache->mode;
593 fibril_mutex_unlock(&cache->lock);
594
595 /*
596 * Determine whether to sync the block. Syncing the block is best done
597 * when not holding the cache lock as it does not impede concurrency.
598 * Since the situation may have changed when we unlocked the cache, the
599 * blocks_cached and mode variables are mere hints. We will recheck the
600 * conditions later when the cache lock is held again.
601 */
602 fibril_mutex_lock(&block->lock);
603 if (block->toxic)
604 block->dirty = false; /* will not write back toxic block */
605 if (block->dirty && (block->refcnt == 1) &&
606 (blocks_cached > CACHE_HI_WATERMARK || mode != CACHE_MODE_WB)) {
607 fibril_mutex_lock(&devcon->comm_area_lock);
608 memcpy(devcon->comm_area, block->data, block->size);
609 rc = write_blocks(devcon, block->pba, cache->blocks_cluster);
610 fibril_mutex_unlock(&devcon->comm_area_lock);
611 block->dirty = false;
612 }
613 fibril_mutex_unlock(&block->lock);
614
615 fibril_mutex_lock(&cache->lock);
616 fibril_mutex_lock(&block->lock);
617 if (!--block->refcnt) {
618 /*
619 * Last reference to the block was dropped. Either free the
620 * block or put it on the free list. In case of an I/O error,
621 * free the block.
622 */
623 if ((cache->blocks_cached > CACHE_HI_WATERMARK) ||
624 (rc != EOK)) {
625 /*
626 * Currently there are too many cached blocks or there
627 * was an I/O error when writing the block back to the
628 * device.
629 */
630 if (block->dirty) {
631 /*
632 * We cannot sync the block while holding the
633 * cache lock. Release everything and retry.
634 */
635 block->refcnt++;
636 fibril_mutex_unlock(&block->lock);
637 fibril_mutex_unlock(&cache->lock);
638 goto retry;
639 }
640 /*
641 * Take the block out of the cache and free it.
642 */
643 unsigned long key = block->lba;
644 hash_table_remove(&cache->block_hash, &key, 1);
645 fibril_mutex_unlock(&block->lock);
646 free(block->data);
647 free(block);
648 cache->blocks_cached--;
649 fibril_mutex_unlock(&cache->lock);
650 return rc;
651 }
652 /*
653 * Put the block on the free list.
654 */
655 if (cache->mode != CACHE_MODE_WB && block->dirty) {
656 /*
657 * We cannot sync the block while holding the cache
658 * lock. Release everything and retry.
659 */
660 block->refcnt++;
661 fibril_mutex_unlock(&block->lock);
662 fibril_mutex_unlock(&cache->lock);
663 goto retry;
664 }
665 list_append(&block->free_link, &cache->free_head);
666 }
667 fibril_mutex_unlock(&block->lock);
668 fibril_mutex_unlock(&cache->lock);
669
670 return rc;
671}
672
673/** Read sequential data from a block device.
674 *
675 * @param devmap_handle Device handle of the block device.
676 * @param bufpos Pointer to the first unread valid offset within the
677 * communication buffer.
678 * @param buflen Pointer to the number of unread bytes that are ready in
679 * the communication buffer.
680 * @param pos Device position to be read.
681 * @param dst Destination buffer.
682 * @param size Size of the destination buffer.
683 * @param block_size Block size to be used for the transfer.
684 *
685 * @return EOK on success or a negative return code on failure.
686 */
687int block_seqread(devmap_handle_t devmap_handle, size_t *bufpos, size_t *buflen,
688 aoff64_t *pos, void *dst, size_t size)
689{
690 size_t offset = 0;
691 size_t left = size;
692 size_t block_size;
693 devcon_t *devcon;
694
695 devcon = devcon_search(devmap_handle);
696 assert(devcon);
697 block_size = devcon->pblock_size;
698
699 fibril_mutex_lock(&devcon->comm_area_lock);
700 while (left > 0) {
701 size_t rd;
702
703 if (*bufpos + left < *buflen)
704 rd = left;
705 else
706 rd = *buflen - *bufpos;
707
708 if (rd > 0) {
709 /*
710 * Copy the contents of the communication buffer to the
711 * destination buffer.
712 */
713 memcpy(dst + offset, devcon->comm_area + *bufpos, rd);
714 offset += rd;
715 *bufpos += rd;
716 *pos += rd;
717 left -= rd;
718 }
719
720 if (*bufpos == *buflen) {
721 /* Refill the communication buffer with a new block. */
722 int rc;
723
724 rc = read_blocks(devcon, *pos / block_size, 1);
725 if (rc != EOK) {
726 fibril_mutex_unlock(&devcon->comm_area_lock);
727 return rc;
728 }
729
730 *bufpos = 0;
731 *buflen = block_size;
732 }
733 }
734 fibril_mutex_unlock(&devcon->comm_area_lock);
735
736 return EOK;
737}
738
739/** Read blocks directly from device (bypass cache).
740 *
741 * @param devmap_handle Device handle of the block device.
742 * @param ba Address of first block (physical).
743 * @param cnt Number of blocks.
744 * @param src Buffer for storing the data.
745 *
746 * @return EOK on success or negative error code on failure.
747 */
748int block_read_direct(devmap_handle_t devmap_handle, aoff64_t ba, size_t cnt, void *buf)
749{
750 devcon_t *devcon;
751 int rc;
752
753 devcon = devcon_search(devmap_handle);
754 assert(devcon);
755
756 fibril_mutex_lock(&devcon->comm_area_lock);
757
758 rc = read_blocks(devcon, ba, cnt);
759 if (rc == EOK)
760 memcpy(buf, devcon->comm_area, devcon->pblock_size * cnt);
761
762 fibril_mutex_unlock(&devcon->comm_area_lock);
763
764 return rc;
765}
766
767/** Write blocks directly to device (bypass cache).
768 *
769 * @param devmap_handle Device handle of the block device.
770 * @param ba Address of first block (physical).
771 * @param cnt Number of blocks.
772 * @param src The data to be written.
773 *
774 * @return EOK on success or negative error code on failure.
775 */
776int block_write_direct(devmap_handle_t devmap_handle, aoff64_t ba, size_t cnt,
777 const void *data)
778{
779 devcon_t *devcon;
780 int rc;
781
782 devcon = devcon_search(devmap_handle);
783 assert(devcon);
784
785 fibril_mutex_lock(&devcon->comm_area_lock);
786
787 memcpy(devcon->comm_area, data, devcon->pblock_size * cnt);
788 rc = write_blocks(devcon, ba, cnt);
789
790 fibril_mutex_unlock(&devcon->comm_area_lock);
791
792 return rc;
793}
794
795/** Get device block size.
796 *
797 * @param devmap_handle Device handle of the block device.
798 * @param bsize Output block size.
799 *
800 * @return EOK on success or negative error code on failure.
801 */
802int block_get_bsize(devmap_handle_t devmap_handle, size_t *bsize)
803{
804 devcon_t *devcon;
805
806 devcon = devcon_search(devmap_handle);
807 assert(devcon);
808
809 return get_block_size(devcon->dev_phone, bsize);
810}
811
812/** Get number of blocks on device.
813 *
814 * @param devmap_handle Device handle of the block device.
815 * @param nblocks Output number of blocks.
816 *
817 * @return EOK on success or negative error code on failure.
818 */
819int block_get_nblocks(devmap_handle_t devmap_handle, aoff64_t *nblocks)
820{
821 devcon_t *devcon;
822
823 devcon = devcon_search(devmap_handle);
824 assert(devcon);
825
826 return get_num_blocks(devcon->dev_phone, nblocks);
827}
828
829/** Read blocks from block device.
830 *
831 * @param devcon Device connection.
832 * @param ba Address of first block.
833 * @param cnt Number of blocks.
834 * @param src Buffer for storing the data.
835 *
836 * @return EOK on success or negative error code on failure.
837 */
838static int read_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt)
839{
840 int rc;
841
842 assert(devcon);
843 rc = async_req_3_0(devcon->dev_phone, BD_READ_BLOCKS, LOWER32(ba),
844 UPPER32(ba), cnt);
845 if (rc != EOK) {
846 printf("Error %d reading %zu blocks starting at block %" PRIuOFF64
847 " from device handle %" PRIun "\n", rc, cnt, ba,
848 devcon->devmap_handle);
849#ifndef NDEBUG
850 stacktrace_print();
851#endif
852 }
853 return rc;
854}
855
856/** Write block to block device.
857 *
858 * @param devcon Device connection.
859 * @param ba Address of first block.
860 * @param cnt Number of blocks.
861 * @param src Buffer containing the data to write.
862 *
863 * @return EOK on success or negative error code on failure.
864 */
865static int write_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt)
866{
867 int rc;
868
869 assert(devcon);
870 rc = async_req_3_0(devcon->dev_phone, BD_WRITE_BLOCKS, LOWER32(ba),
871 UPPER32(ba), cnt);
872 if (rc != EOK) {
873 printf("Error %d writing %zu blocks starting at block %" PRIuOFF64
874 " to device handle %" PRIun "\n", rc, cnt, ba, devcon->devmap_handle);
875#ifndef NDEBUG
876 stacktrace_print();
877#endif
878 }
879 return rc;
880}
881
882/** Get block size used by the device. */
883static int get_block_size(int dev_phone, size_t *bsize)
884{
885 sysarg_t bs;
886 int rc;
887
888 rc = async_req_0_1(dev_phone, BD_GET_BLOCK_SIZE, &bs);
889 if (rc == EOK)
890 *bsize = (size_t) bs;
891
892 return rc;
893}
894
895/** Get total number of blocks on block device. */
896static int get_num_blocks(int dev_phone, aoff64_t *nblocks)
897{
898 sysarg_t nb_l, nb_h;
899 int rc;
900
901 rc = async_req_0_2(dev_phone, BD_GET_NUM_BLOCKS, &nb_l, &nb_h);
902 if (rc == EOK) {
903 *nblocks = (aoff64_t) MERGE_LOUP32(nb_l, nb_h);
904 }
905
906 return rc;
907}
908
909/** Convert logical block address to physical block address. */
910static aoff64_t ba_ltop(devcon_t *devcon, aoff64_t lba)
911{
912 assert(devcon->cache != NULL);
913 return lba * devcon->cache->blocks_cluster;
914}
915
916/** @}
917 */
Note: See TracBrowser for help on using the repository browser.