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

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

do not intermix low-level IPC methods with async framework methods

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