source: mainline/uspace/lib/libblock/libblock.c@ 9a66bc2e

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

Add block_cache_fini().

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