source: mainline/uspace/lib/block/libblock.c@ 84eb7432

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

new async framework with integrated exchange tracking

  • strict isolation between low-level IPC and high-level async framework with integrated exchange tracking
    • each IPC connection is represented by an async_sess_t structure
    • each IPC exchange is represented by an async_exch_t structure
    • exchange management is either based on atomic messages (EXCHANGE_ATOMIC), locking (EXCHANGE_SERIALIZE) or connection cloning (EXCHANGE_CLONE)
  • async_obsolete: temporary compatibility layer to keep old async clients working (several pieces of code are currently broken, but only non-essential functionality)
  • IPC_M_PHONE_HANGUP is now method no. 0 (for elegant boolean evaluation)
  • IPC_M_DEBUG_ALL has been renamed to IPC_M_DEBUG
  • IPC_M_PING has been removed (VFS protocol now has VFS_IN_PING)
  • console routines in libc have been rewritten for better abstraction
  • additional use for libc-private header files (FILE structure opaque to the client)
  • various cstyle changes (typos, indentation, missing externs in header files, improved comments, etc.)
  • Property mode set to 100644
File size: 24.3 KB
Line 
1/*
2 * Copyright (c) 2008 Jakub Jermar
3 * Copyright (c) 2008 Martin Decky
4 * Copyright (c) 2011 Martin Sucha
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * - The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/** @addtogroup libblock
32 * @{
33 */
34/**
35 * @file
36 * @brief
37 */
38
39#include "libblock.h"
40#include "../../srv/vfs/vfs.h"
41#include <ipc/devmap.h>
42#include <ipc/bd.h>
43#include <ipc/services.h>
44#include <errno.h>
45#include <sys/mman.h>
46#include <async.h>
47#include <as.h>
48#include <assert.h>
49#include <fibril_synch.h>
50#include <adt/list.h>
51#include <adt/hash_table.h>
52#include <macros.h>
53#include <mem.h>
54#include <malloc.h>
55#include <stdio.h>
56#include <sys/typefmt.h>
57#include <stacktrace.h>
58
59/** Lock protecting the device connection list */
60static FIBRIL_MUTEX_INITIALIZE(dcl_lock);
61/** Device connection list head. */
62static LIST_INITIALIZE(dcl_head);
63
64#define CACHE_BUCKETS_LOG2 10
65#define CACHE_BUCKETS (1 << CACHE_BUCKETS_LOG2)
66
67typedef struct {
68 fibril_mutex_t lock;
69 size_t lblock_size; /**< Logical block size. */
70 unsigned blocks_cluster; /**< Physical blocks per block_t */
71 unsigned block_count; /**< Total number of blocks. */
72 unsigned blocks_cached; /**< Number of cached blocks. */
73 hash_table_t block_hash;
74 link_t free_head;
75 enum cache_mode mode;
76} cache_t;
77
78typedef struct {
79 link_t link;
80 devmap_handle_t devmap_handle;
81 async_sess_t *sess;
82 fibril_mutex_t comm_area_lock;
83 void *comm_area;
84 size_t comm_size;
85 void *bb_buf;
86 aoff64_t bb_addr;
87 size_t pblock_size; /**< Physical block size. */
88 cache_t *cache;
89} devcon_t;
90
91static int read_blocks(devcon_t *, aoff64_t, size_t);
92static int write_blocks(devcon_t *, aoff64_t, size_t);
93static int get_block_size(async_sess_t *, size_t *);
94static int get_num_blocks(async_sess_t *, aoff64_t *);
95static aoff64_t ba_ltop(devcon_t *, aoff64_t);
96
97static devcon_t *devcon_search(devmap_handle_t devmap_handle)
98{
99 link_t *cur;
100
101 fibril_mutex_lock(&dcl_lock);
102
103 for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
104 devcon_t *devcon = list_get_instance(cur, devcon_t, link);
105 if (devcon->devmap_handle == devmap_handle) {
106 fibril_mutex_unlock(&dcl_lock);
107 return devcon;
108 }
109 }
110
111 fibril_mutex_unlock(&dcl_lock);
112 return NULL;
113}
114
115static int devcon_add(devmap_handle_t devmap_handle, async_sess_t *sess,
116 size_t bsize, void *comm_area, size_t comm_size)
117{
118 link_t *cur;
119 devcon_t *devcon;
120
121 if (comm_size < bsize)
122 return EINVAL;
123
124 devcon = malloc(sizeof(devcon_t));
125 if (!devcon)
126 return ENOMEM;
127
128 link_initialize(&devcon->link);
129 devcon->devmap_handle = devmap_handle;
130 devcon->sess = sess;
131 fibril_mutex_initialize(&devcon->comm_area_lock);
132 devcon->comm_area = comm_area;
133 devcon->comm_size = comm_size;
134 devcon->bb_buf = NULL;
135 devcon->bb_addr = 0;
136 devcon->pblock_size = bsize;
137 devcon->cache = NULL;
138
139 fibril_mutex_lock(&dcl_lock);
140 for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
141 devcon_t *d = list_get_instance(cur, devcon_t, link);
142 if (d->devmap_handle == devmap_handle) {
143 fibril_mutex_unlock(&dcl_lock);
144 free(devcon);
145 return EEXIST;
146 }
147 }
148 list_append(&devcon->link, &dcl_head);
149 fibril_mutex_unlock(&dcl_lock);
150 return EOK;
151}
152
153static void devcon_remove(devcon_t *devcon)
154{
155 fibril_mutex_lock(&dcl_lock);
156 list_remove(&devcon->link);
157 fibril_mutex_unlock(&dcl_lock);
158}
159
160int block_init(exch_mgmt_t mgmt, devmap_handle_t devmap_handle,
161 size_t comm_size)
162{
163 void *comm_area = mmap(NULL, comm_size, PROTO_READ | PROTO_WRITE,
164 MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
165 if (!comm_area)
166 return ENOMEM;
167
168 async_sess_t *sess = devmap_device_connect(mgmt, devmap_handle,
169 IPC_FLAG_BLOCKING);
170 if (!sess) {
171 munmap(comm_area, comm_size);
172 return ENOENT;
173 }
174
175 async_exch_t *exch = async_exchange_begin(sess);
176 int rc = async_share_out_start(exch, comm_area,
177 AS_AREA_READ | AS_AREA_WRITE);
178 async_exchange_end(exch);
179
180 if (rc != EOK) {
181 munmap(comm_area, comm_size);
182 async_hangup(sess);
183 return rc;
184 }
185
186 size_t bsize;
187 rc = get_block_size(sess, &bsize);
188
189 if (rc != EOK) {
190 munmap(comm_area, comm_size);
191 async_hangup(sess);
192 return rc;
193 }
194
195 rc = devcon_add(devmap_handle, sess, bsize, comm_area, comm_size);
196 if (rc != EOK) {
197 munmap(comm_area, comm_size);
198 async_hangup(sess);
199 return rc;
200 }
201
202 return EOK;
203}
204
205void block_fini(devmap_handle_t devmap_handle)
206{
207 devcon_t *devcon = devcon_search(devmap_handle);
208 assert(devcon);
209
210 if (devcon->cache)
211 (void) block_cache_fini(devmap_handle);
212
213 devcon_remove(devcon);
214
215 if (devcon->bb_buf)
216 free(devcon->bb_buf);
217
218 munmap(devcon->comm_area, devcon->comm_size);
219 async_hangup(devcon->sess);
220
221 free(devcon);
222}
223
224int block_bb_read(devmap_handle_t devmap_handle, aoff64_t ba)
225{
226 void *bb_buf;
227 int rc;
228
229 devcon_t *devcon = devcon_search(devmap_handle);
230 if (!devcon)
231 return ENOENT;
232 if (devcon->bb_buf)
233 return EEXIST;
234 bb_buf = malloc(devcon->pblock_size);
235 if (!bb_buf)
236 return ENOMEM;
237
238 fibril_mutex_lock(&devcon->comm_area_lock);
239 rc = read_blocks(devcon, 0, 1);
240 if (rc != EOK) {
241 fibril_mutex_unlock(&devcon->comm_area_lock);
242 free(bb_buf);
243 return rc;
244 }
245 memcpy(bb_buf, devcon->comm_area, devcon->pblock_size);
246 fibril_mutex_unlock(&devcon->comm_area_lock);
247
248 devcon->bb_buf = bb_buf;
249 devcon->bb_addr = ba;
250
251 return EOK;
252}
253
254void *block_bb_get(devmap_handle_t devmap_handle)
255{
256 devcon_t *devcon = devcon_search(devmap_handle);
257 assert(devcon);
258 return devcon->bb_buf;
259}
260
261static hash_index_t cache_hash(unsigned long *key)
262{
263 return *key & (CACHE_BUCKETS - 1);
264}
265
266static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item)
267{
268 block_t *b = hash_table_get_instance(item, block_t, hash_link);
269 return b->lba == *key;
270}
271
272static void cache_remove_callback(link_t *item)
273{
274}
275
276static hash_table_operations_t cache_ops = {
277 .hash = cache_hash,
278 .compare = cache_compare,
279 .remove_callback = cache_remove_callback
280};
281
282int block_cache_init(devmap_handle_t devmap_handle, size_t size, unsigned blocks,
283 enum cache_mode mode)
284{
285 devcon_t *devcon = devcon_search(devmap_handle);
286 cache_t *cache;
287 if (!devcon)
288 return ENOENT;
289 if (devcon->cache)
290 return EEXIST;
291 cache = malloc(sizeof(cache_t));
292 if (!cache)
293 return ENOMEM;
294
295 fibril_mutex_initialize(&cache->lock);
296 list_initialize(&cache->free_head);
297 cache->lblock_size = size;
298 cache->block_count = blocks;
299 cache->blocks_cached = 0;
300 cache->mode = mode;
301
302 /* Allow 1:1 or small-to-large block size translation */
303 if (cache->lblock_size % devcon->pblock_size != 0) {
304 free(cache);
305 return ENOTSUP;
306 }
307
308 cache->blocks_cluster = cache->lblock_size / devcon->pblock_size;
309
310 if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1,
311 &cache_ops)) {
312 free(cache);
313 return ENOMEM;
314 }
315
316 devcon->cache = cache;
317 return EOK;
318}
319
320int block_cache_fini(devmap_handle_t devmap_handle)
321{
322 devcon_t *devcon = devcon_search(devmap_handle);
323 cache_t *cache;
324 int rc;
325
326 if (!devcon)
327 return ENOENT;
328 if (!devcon->cache)
329 return EOK;
330 cache = devcon->cache;
331
332 /*
333 * We are expecting to find all blocks for this device handle on the
334 * free list, i.e. the block reference count should be zero. Do not
335 * bother with the cache and block locks because we are single-threaded.
336 */
337 while (!list_empty(&cache->free_head)) {
338 block_t *b = list_get_instance(cache->free_head.next,
339 block_t, free_link);
340
341 list_remove(&b->free_link);
342 if (b->dirty) {
343 memcpy(devcon->comm_area, b->data, b->size);
344 rc = write_blocks(devcon, b->pba, cache->blocks_cluster);
345 if (rc != EOK)
346 return rc;
347 }
348
349 unsigned long key = b->lba;
350 hash_table_remove(&cache->block_hash, &key, 1);
351
352 free(b->data);
353 free(b);
354 }
355
356 hash_table_destroy(&cache->block_hash);
357 devcon->cache = NULL;
358 free(cache);
359
360 return EOK;
361}
362
363#define CACHE_LO_WATERMARK 10
364#define CACHE_HI_WATERMARK 20
365static bool cache_can_grow(cache_t *cache)
366{
367 if (cache->blocks_cached < CACHE_LO_WATERMARK)
368 return true;
369 if (!list_empty(&cache->free_head))
370 return false;
371 return true;
372}
373
374static void block_initialize(block_t *b)
375{
376 fibril_mutex_initialize(&b->lock);
377 b->refcnt = 1;
378 b->dirty = false;
379 b->toxic = false;
380 fibril_rwlock_initialize(&b->contents_lock);
381 link_initialize(&b->free_link);
382 link_initialize(&b->hash_link);
383}
384
385/** Instantiate a block in memory and get a reference to it.
386 *
387 * @param block Pointer to where the function will store the
388 * block pointer on success.
389 * @param devmap_handle Device handle of the block device.
390 * @param ba Block address (logical).
391 * @param flags If BLOCK_FLAGS_NOREAD is specified, block_get()
392 * will not read the contents of the block from the
393 * device.
394 *
395 * @return EOK on success or a negative error code.
396 */
397int block_get(block_t **block, devmap_handle_t devmap_handle, aoff64_t ba, int flags)
398{
399 devcon_t *devcon;
400 cache_t *cache;
401 block_t *b;
402 link_t *l;
403 unsigned long key = ba;
404 int rc;
405
406 devcon = devcon_search(devmap_handle);
407
408 assert(devcon);
409 assert(devcon->cache);
410
411 cache = devcon->cache;
412
413retry:
414 rc = EOK;
415 b = NULL;
416
417 fibril_mutex_lock(&cache->lock);
418 l = hash_table_find(&cache->block_hash, &key);
419 if (l) {
420found:
421 /*
422 * We found the block in the cache.
423 */
424 b = hash_table_get_instance(l, block_t, hash_link);
425 fibril_mutex_lock(&b->lock);
426 if (b->refcnt++ == 0)
427 list_remove(&b->free_link);
428 if (b->toxic)
429 rc = EIO;
430 fibril_mutex_unlock(&b->lock);
431 fibril_mutex_unlock(&cache->lock);
432 } else {
433 /*
434 * The block was not found in the cache.
435 */
436 if (cache_can_grow(cache)) {
437 /*
438 * We can grow the cache by allocating new blocks.
439 * Should the allocation fail, we fail over and try to
440 * recycle a block from the cache.
441 */
442 b = malloc(sizeof(block_t));
443 if (!b)
444 goto recycle;
445 b->data = malloc(cache->lblock_size);
446 if (!b->data) {
447 free(b);
448 b = NULL;
449 goto recycle;
450 }
451 cache->blocks_cached++;
452 } else {
453 /*
454 * Try to recycle a block from the free list.
455 */
456 unsigned long temp_key;
457recycle:
458 if (list_empty(&cache->free_head)) {
459 fibril_mutex_unlock(&cache->lock);
460 rc = ENOMEM;
461 goto out;
462 }
463 l = cache->free_head.next;
464 b = list_get_instance(l, block_t, free_link);
465
466 fibril_mutex_lock(&b->lock);
467 if (b->dirty) {
468 /*
469 * The block needs to be written back to the
470 * device before it changes identity. Do this
471 * while not holding the cache lock so that
472 * concurrency is not impeded. Also move the
473 * block to the end of the free list so that we
474 * do not slow down other instances of
475 * block_get() draining the free list.
476 */
477 list_remove(&b->free_link);
478 list_append(&b->free_link, &cache->free_head);
479 fibril_mutex_unlock(&cache->lock);
480 fibril_mutex_lock(&devcon->comm_area_lock);
481 memcpy(devcon->comm_area, b->data, b->size);
482 rc = write_blocks(devcon, b->pba,
483 cache->blocks_cluster);
484 fibril_mutex_unlock(&devcon->comm_area_lock);
485 if (rc != EOK) {
486 /*
487 * We did not manage to write the block
488 * to the device. Keep it around for
489 * another try. Hopefully, we will grab
490 * another block next time.
491 */
492 fibril_mutex_unlock(&b->lock);
493 goto retry;
494 }
495 b->dirty = false;
496 if (!fibril_mutex_trylock(&cache->lock)) {
497 /*
498 * Somebody is probably racing with us.
499 * Unlock the block and retry.
500 */
501 fibril_mutex_unlock(&b->lock);
502 goto retry;
503 }
504 l = hash_table_find(&cache->block_hash, &key);
505 if (l) {
506 /*
507 * Someone else must have already
508 * instantiated the block while we were
509 * not holding the cache lock.
510 * Leave the recycled block on the
511 * freelist and continue as if we
512 * found the block of interest during
513 * the first try.
514 */
515 fibril_mutex_unlock(&b->lock);
516 goto found;
517 }
518
519 }
520 fibril_mutex_unlock(&b->lock);
521
522 /*
523 * Unlink the block from the free list and the hash
524 * table.
525 */
526 list_remove(&b->free_link);
527 temp_key = b->lba;
528 hash_table_remove(&cache->block_hash, &temp_key, 1);
529 }
530
531 block_initialize(b);
532 b->devmap_handle = devmap_handle;
533 b->size = cache->lblock_size;
534 b->lba = ba;
535 b->pba = ba_ltop(devcon, b->lba);
536 hash_table_insert(&cache->block_hash, &key, &b->hash_link);
537
538 /*
539 * Lock the block before releasing the cache lock. Thus we don't
540 * kill concurrent operations on the cache while doing I/O on
541 * the block.
542 */
543 fibril_mutex_lock(&b->lock);
544 fibril_mutex_unlock(&cache->lock);
545
546 if (!(flags & BLOCK_FLAGS_NOREAD)) {
547 /*
548 * The block contains old or no data. We need to read
549 * the new contents from the device.
550 */
551 fibril_mutex_lock(&devcon->comm_area_lock);
552 rc = read_blocks(devcon, b->pba, cache->blocks_cluster);
553 memcpy(b->data, devcon->comm_area, cache->lblock_size);
554 fibril_mutex_unlock(&devcon->comm_area_lock);
555 if (rc != EOK)
556 b->toxic = true;
557 } else
558 rc = EOK;
559
560 fibril_mutex_unlock(&b->lock);
561 }
562out:
563 if ((rc != EOK) && b) {
564 assert(b->toxic);
565 (void) block_put(b);
566 b = NULL;
567 }
568 *block = b;
569 return rc;
570}
571
572/** Release a reference to a block.
573 *
574 * If the last reference is dropped, the block is put on the free list.
575 *
576 * @param block Block of which a reference is to be released.
577 *
578 * @return EOK on success or a negative error code.
579 */
580int block_put(block_t *block)
581{
582 devcon_t *devcon = devcon_search(block->devmap_handle);
583 cache_t *cache;
584 unsigned blocks_cached;
585 enum cache_mode mode;
586 int rc = EOK;
587
588 assert(devcon);
589 assert(devcon->cache);
590 assert(block->refcnt >= 1);
591
592 cache = devcon->cache;
593
594retry:
595 fibril_mutex_lock(&cache->lock);
596 blocks_cached = cache->blocks_cached;
597 mode = cache->mode;
598 fibril_mutex_unlock(&cache->lock);
599
600 /*
601 * Determine whether to sync the block. Syncing the block is best done
602 * when not holding the cache lock as it does not impede concurrency.
603 * Since the situation may have changed when we unlocked the cache, the
604 * blocks_cached and mode variables are mere hints. We will recheck the
605 * conditions later when the cache lock is held again.
606 */
607 fibril_mutex_lock(&block->lock);
608 if (block->toxic)
609 block->dirty = false; /* will not write back toxic block */
610 if (block->dirty && (block->refcnt == 1) &&
611 (blocks_cached > CACHE_HI_WATERMARK || mode != CACHE_MODE_WB)) {
612 fibril_mutex_lock(&devcon->comm_area_lock);
613 memcpy(devcon->comm_area, block->data, block->size);
614 rc = write_blocks(devcon, block->pba, cache->blocks_cluster);
615 fibril_mutex_unlock(&devcon->comm_area_lock);
616 block->dirty = false;
617 }
618 fibril_mutex_unlock(&block->lock);
619
620 fibril_mutex_lock(&cache->lock);
621 fibril_mutex_lock(&block->lock);
622 if (!--block->refcnt) {
623 /*
624 * Last reference to the block was dropped. Either free the
625 * block or put it on the free list. In case of an I/O error,
626 * free the block.
627 */
628 if ((cache->blocks_cached > CACHE_HI_WATERMARK) ||
629 (rc != EOK)) {
630 /*
631 * Currently there are too many cached blocks or there
632 * was an I/O error when writing the block back to the
633 * device.
634 */
635 if (block->dirty) {
636 /*
637 * We cannot sync the block while holding the
638 * cache lock. Release everything and retry.
639 */
640 block->refcnt++;
641 fibril_mutex_unlock(&block->lock);
642 fibril_mutex_unlock(&cache->lock);
643 goto retry;
644 }
645 /*
646 * Take the block out of the cache and free it.
647 */
648 unsigned long key = block->lba;
649 hash_table_remove(&cache->block_hash, &key, 1);
650 fibril_mutex_unlock(&block->lock);
651 free(block->data);
652 free(block);
653 cache->blocks_cached--;
654 fibril_mutex_unlock(&cache->lock);
655 return rc;
656 }
657 /*
658 * Put the block on the free list.
659 */
660 if (cache->mode != CACHE_MODE_WB && block->dirty) {
661 /*
662 * We cannot sync the block while holding the cache
663 * lock. Release everything and retry.
664 */
665 block->refcnt++;
666 fibril_mutex_unlock(&block->lock);
667 fibril_mutex_unlock(&cache->lock);
668 goto retry;
669 }
670 list_append(&block->free_link, &cache->free_head);
671 }
672 fibril_mutex_unlock(&block->lock);
673 fibril_mutex_unlock(&cache->lock);
674
675 return rc;
676}
677
678/** Read sequential data from a block device.
679 *
680 * @param devmap_handle Device handle of the block device.
681 * @param bufpos Pointer to the first unread valid offset within the
682 * communication buffer.
683 * @param buflen Pointer to the number of unread bytes that are ready in
684 * the communication buffer.
685 * @param pos Device position to be read.
686 * @param dst Destination buffer.
687 * @param size Size of the destination buffer.
688 * @param block_size Block size to be used for the transfer.
689 *
690 * @return EOK on success or a negative return code on failure.
691 */
692int block_seqread(devmap_handle_t devmap_handle, size_t *bufpos, size_t *buflen,
693 aoff64_t *pos, void *dst, size_t size)
694{
695 size_t offset = 0;
696 size_t left = size;
697 size_t block_size;
698 devcon_t *devcon;
699
700 devcon = devcon_search(devmap_handle);
701 assert(devcon);
702 block_size = devcon->pblock_size;
703
704 fibril_mutex_lock(&devcon->comm_area_lock);
705 while (left > 0) {
706 size_t rd;
707
708 if (*bufpos + left < *buflen)
709 rd = left;
710 else
711 rd = *buflen - *bufpos;
712
713 if (rd > 0) {
714 /*
715 * Copy the contents of the communication buffer to the
716 * destination buffer.
717 */
718 memcpy(dst + offset, devcon->comm_area + *bufpos, rd);
719 offset += rd;
720 *bufpos += rd;
721 *pos += rd;
722 left -= rd;
723 }
724
725 if (*bufpos == *buflen) {
726 /* Refill the communication buffer with a new block. */
727 int rc;
728
729 rc = read_blocks(devcon, *pos / block_size, 1);
730 if (rc != EOK) {
731 fibril_mutex_unlock(&devcon->comm_area_lock);
732 return rc;
733 }
734
735 *bufpos = 0;
736 *buflen = block_size;
737 }
738 }
739 fibril_mutex_unlock(&devcon->comm_area_lock);
740
741 return EOK;
742}
743
744/** Read blocks directly from device (bypass cache).
745 *
746 * @param devmap_handle Device handle of the block device.
747 * @param ba Address of first block (physical).
748 * @param cnt Number of blocks.
749 * @param src Buffer for storing the data.
750 *
751 * @return EOK on success or negative error code on failure.
752 */
753int block_read_direct(devmap_handle_t devmap_handle, aoff64_t ba, size_t cnt, void *buf)
754{
755 devcon_t *devcon;
756 int rc;
757
758 devcon = devcon_search(devmap_handle);
759 assert(devcon);
760
761 fibril_mutex_lock(&devcon->comm_area_lock);
762
763 rc = read_blocks(devcon, ba, cnt);
764 if (rc == EOK)
765 memcpy(buf, devcon->comm_area, devcon->pblock_size * cnt);
766
767 fibril_mutex_unlock(&devcon->comm_area_lock);
768
769 return rc;
770}
771
772/** Write blocks directly to device (bypass cache).
773 *
774 * @param devmap_handle Device handle of the block device.
775 * @param ba Address of first block (physical).
776 * @param cnt Number of blocks.
777 * @param src The data to be written.
778 *
779 * @return EOK on success or negative error code on failure.
780 */
781int block_write_direct(devmap_handle_t devmap_handle, aoff64_t ba, size_t cnt,
782 const void *data)
783{
784 devcon_t *devcon;
785 int rc;
786
787 devcon = devcon_search(devmap_handle);
788 assert(devcon);
789
790 fibril_mutex_lock(&devcon->comm_area_lock);
791
792 memcpy(devcon->comm_area, data, devcon->pblock_size * cnt);
793 rc = write_blocks(devcon, ba, cnt);
794
795 fibril_mutex_unlock(&devcon->comm_area_lock);
796
797 return rc;
798}
799
800/** Get device block size.
801 *
802 * @param devmap_handle Device handle of the block device.
803 * @param bsize Output block size.
804 *
805 * @return EOK on success or negative error code on failure.
806 */
807int block_get_bsize(devmap_handle_t devmap_handle, size_t *bsize)
808{
809 devcon_t *devcon;
810
811 devcon = devcon_search(devmap_handle);
812 assert(devcon);
813
814 return get_block_size(devcon->sess, bsize);
815}
816
817/** Get number of blocks on device.
818 *
819 * @param devmap_handle Device handle of the block device.
820 * @param nblocks Output number of blocks.
821 *
822 * @return EOK on success or negative error code on failure.
823 */
824int block_get_nblocks(devmap_handle_t devmap_handle, aoff64_t *nblocks)
825{
826 devcon_t *devcon = devcon_search(devmap_handle);
827 assert(devcon);
828
829 return get_num_blocks(devcon->sess, nblocks);
830}
831
832/** Read bytes directly from the device (bypass cache)
833 *
834 * @param devmap_handle Device handle of the block device.
835 * @param abs_offset Absolute offset in bytes where to start reading
836 * @param bytes Number of bytes to read
837 * @param data Buffer that receives the data
838 *
839 * @return EOK on success or negative error code on failure.
840 */
841int block_read_bytes_direct(devmap_handle_t devmap_handle, aoff64_t abs_offset,
842 size_t bytes, void *data)
843{
844 int rc;
845 size_t phys_block_size;
846 size_t buf_size;
847 void *buffer;
848 aoff64_t first_block;
849 aoff64_t last_block;
850 size_t blocks;
851 size_t offset;
852
853 rc = block_get_bsize(devmap_handle, &phys_block_size);
854 if (rc != EOK) {
855 return rc;
856 }
857
858 /* calculate data position and required space */
859 first_block = abs_offset / phys_block_size;
860 offset = abs_offset % phys_block_size;
861 last_block = (abs_offset + bytes - 1) / phys_block_size;
862 blocks = last_block - first_block + 1;
863 buf_size = blocks * phys_block_size;
864
865 /* read the data into memory */
866 buffer = malloc(buf_size);
867 if (buffer == NULL) {
868 return ENOMEM;
869 }
870
871 rc = block_read_direct(devmap_handle, first_block, blocks, buffer);
872 if (rc != EOK) {
873 free(buffer);
874 return rc;
875 }
876
877 /* copy the data from the buffer */
878 memcpy(data, buffer + offset, bytes);
879 free(buffer);
880
881 return EOK;
882}
883
884/** Read blocks from block device.
885 *
886 * @param devcon Device connection.
887 * @param ba Address of first block.
888 * @param cnt Number of blocks.
889 * @param src Buffer for storing the data.
890 *
891 * @return EOK on success or negative error code on failure.
892 */
893static int read_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt)
894{
895 assert(devcon);
896
897 async_exch_t *exch = async_exchange_begin(devcon->sess);
898 int rc = async_req_3_0(exch, BD_READ_BLOCKS, LOWER32(ba),
899 UPPER32(ba), cnt);
900 async_exchange_end(exch);
901
902 if (rc != EOK) {
903 printf("Error %d reading %zu blocks starting at block %" PRIuOFF64
904 " from device handle %" PRIun "\n", rc, cnt, ba,
905 devcon->devmap_handle);
906#ifndef NDEBUG
907 stacktrace_print();
908#endif
909 }
910
911 return rc;
912}
913
914/** Write block to block device.
915 *
916 * @param devcon Device connection.
917 * @param ba Address of first block.
918 * @param cnt Number of blocks.
919 * @param src Buffer containing the data to write.
920 *
921 * @return EOK on success or negative error code on failure.
922 */
923static int write_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt)
924{
925 assert(devcon);
926
927 async_exch_t *exch = async_exchange_begin(devcon->sess);
928 int rc = async_req_3_0(exch, BD_WRITE_BLOCKS, LOWER32(ba),
929 UPPER32(ba), cnt);
930 async_exchange_end(exch);
931
932 if (rc != EOK) {
933 printf("Error %d writing %zu blocks starting at block %" PRIuOFF64
934 " to device handle %" PRIun "\n", rc, cnt, ba, devcon->devmap_handle);
935#ifndef NDEBUG
936 stacktrace_print();
937#endif
938 }
939
940 return rc;
941}
942
943/** Get block size used by the device. */
944static int get_block_size(async_sess_t *sess, size_t *bsize)
945{
946 sysarg_t bs;
947
948 async_exch_t *exch = async_exchange_begin(sess);
949 int rc = async_req_0_1(exch, BD_GET_BLOCK_SIZE, &bs);
950 async_exchange_end(exch);
951
952 if (rc == EOK)
953 *bsize = (size_t) bs;
954
955 return rc;
956}
957
958/** Get total number of blocks on block device. */
959static int get_num_blocks(async_sess_t *sess, aoff64_t *nblocks)
960{
961 sysarg_t nb_l;
962 sysarg_t nb_h;
963
964 async_exch_t *exch = async_exchange_begin(sess);
965 int rc = async_req_0_2(exch, BD_GET_NUM_BLOCKS, &nb_l, &nb_h);
966 async_exchange_end(exch);
967
968 if (rc == EOK)
969 *nblocks = (aoff64_t) MERGE_LOUP32(nb_l, nb_h);
970
971 return rc;
972}
973
974/** Convert logical block address to physical block address. */
975static aoff64_t ba_ltop(devcon_t *devcon, aoff64_t lba)
976{
977 assert(devcon->cache != NULL);
978 return lba * devcon->cache->blocks_cluster;
979}
980
981/** @}
982 */
Note: See TracBrowser for help on using the repository browser.