source: mainline/uspace/lib/block/libblock.c@ c3f95d8

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

Merged mainline changes

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