source: mainline/uspace/lib/libblock/libblock.c@ 86018c1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 86018c1 was 08232ee, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Obtain block device size automatically, if possible. Implement ftell(). Fix seek implementation in VFS if whence == SEEK_END.

  • Property mode set to 100644
File size: 20.0 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 devcon_remove(devcon);
201
202 if (devcon->bb_buf)
203 free(devcon->bb_buf);
204
205 if (devcon->cache) {
206 hash_table_destroy(&devcon->cache->block_hash);
207 free(devcon->cache);
208 }
209
210 munmap(devcon->comm_area, devcon->comm_size);
211 ipc_hangup(devcon->dev_phone);
212
213 free(devcon);
214}
215
216int block_bb_read(dev_handle_t dev_handle, bn_t ba)
217{
218 void *bb_buf;
219 int rc;
220
221 devcon_t *devcon = devcon_search(dev_handle);
222 if (!devcon)
223 return ENOENT;
224 if (devcon->bb_buf)
225 return EEXIST;
226 bb_buf = malloc(devcon->pblock_size);
227 if (!bb_buf)
228 return ENOMEM;
229
230 fibril_mutex_lock(&devcon->comm_area_lock);
231 rc = read_blocks(devcon, 0, 1);
232 if (rc != EOK) {
233 fibril_mutex_unlock(&devcon->comm_area_lock);
234 free(bb_buf);
235 return rc;
236 }
237 memcpy(bb_buf, devcon->comm_area, devcon->pblock_size);
238 fibril_mutex_unlock(&devcon->comm_area_lock);
239
240 devcon->bb_buf = bb_buf;
241 devcon->bb_addr = ba;
242
243 return EOK;
244}
245
246void *block_bb_get(dev_handle_t dev_handle)
247{
248 devcon_t *devcon = devcon_search(dev_handle);
249 assert(devcon);
250 return devcon->bb_buf;
251}
252
253static hash_index_t cache_hash(unsigned long *key)
254{
255 return *key & (CACHE_BUCKETS - 1);
256}
257
258static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item)
259{
260 block_t *b = hash_table_get_instance(item, block_t, hash_link);
261 return b->boff == *key;
262}
263
264static void cache_remove_callback(link_t *item)
265{
266}
267
268static hash_table_operations_t cache_ops = {
269 .hash = cache_hash,
270 .compare = cache_compare,
271 .remove_callback = cache_remove_callback
272};
273
274int block_cache_init(dev_handle_t dev_handle, size_t size, unsigned blocks,
275 enum cache_mode mode)
276{
277 devcon_t *devcon = devcon_search(dev_handle);
278 cache_t *cache;
279 if (!devcon)
280 return ENOENT;
281 if (devcon->cache)
282 return EEXIST;
283 cache = malloc(sizeof(cache_t));
284 if (!cache)
285 return ENOMEM;
286
287 fibril_mutex_initialize(&cache->lock);
288 list_initialize(&cache->free_head);
289 cache->lblock_size = size;
290 cache->block_count = blocks;
291 cache->blocks_cached = 0;
292 cache->mode = mode;
293
294 /* No block size translation a.t.m. */
295 assert(cache->lblock_size == devcon->pblock_size);
296
297 if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1,
298 &cache_ops)) {
299 free(cache);
300 return ENOMEM;
301 }
302
303 devcon->cache = cache;
304 return EOK;
305}
306
307#define CACHE_LO_WATERMARK 10
308#define CACHE_HI_WATERMARK 20
309static bool cache_can_grow(cache_t *cache)
310{
311 if (cache->blocks_cached < CACHE_LO_WATERMARK)
312 return true;
313 if (!list_empty(&cache->free_head))
314 return false;
315 return true;
316}
317
318static void block_initialize(block_t *b)
319{
320 fibril_mutex_initialize(&b->lock);
321 b->refcnt = 1;
322 b->dirty = false;
323 b->toxic = false;
324 fibril_rwlock_initialize(&b->contents_lock);
325 link_initialize(&b->free_link);
326 link_initialize(&b->hash_link);
327}
328
329/** Instantiate a block in memory and get a reference to it.
330 *
331 * @param block Pointer to where the function will store the
332 * block pointer on success.
333 * @param dev_handle Device handle of the block device.
334 * @param boff Block offset.
335 * @param flags If BLOCK_FLAGS_NOREAD is specified, block_get()
336 * will not read the contents of the block from the
337 * device.
338 *
339 * @return EOK on success or a negative error code.
340 */
341int block_get(block_t **block, dev_handle_t dev_handle, bn_t boff, int flags)
342{
343 devcon_t *devcon;
344 cache_t *cache;
345 block_t *b;
346 link_t *l;
347 unsigned long key = boff;
348 int rc;
349
350 devcon = devcon_search(dev_handle);
351
352 assert(devcon);
353 assert(devcon->cache);
354
355 cache = devcon->cache;
356
357retry:
358 rc = EOK;
359 b = NULL;
360
361 fibril_mutex_lock(&cache->lock);
362 l = hash_table_find(&cache->block_hash, &key);
363 if (l) {
364 /*
365 * We found the block in the cache.
366 */
367 b = hash_table_get_instance(l, block_t, hash_link);
368 fibril_mutex_lock(&b->lock);
369 if (b->refcnt++ == 0)
370 list_remove(&b->free_link);
371 if (b->toxic)
372 rc = EIO;
373 fibril_mutex_unlock(&b->lock);
374 fibril_mutex_unlock(&cache->lock);
375 } else {
376 /*
377 * The block was not found in the cache.
378 */
379 if (cache_can_grow(cache)) {
380 /*
381 * We can grow the cache by allocating new blocks.
382 * Should the allocation fail, we fail over and try to
383 * recycle a block from the cache.
384 */
385 b = malloc(sizeof(block_t));
386 if (!b)
387 goto recycle;
388 b->data = malloc(cache->lblock_size);
389 if (!b->data) {
390 free(b);
391 goto recycle;
392 }
393 cache->blocks_cached++;
394 } else {
395 /*
396 * Try to recycle a block from the free list.
397 */
398 unsigned long temp_key;
399recycle:
400 if (list_empty(&cache->free_head)) {
401 fibril_mutex_unlock(&cache->lock);
402 rc = ENOMEM;
403 goto out;
404 }
405 l = cache->free_head.next;
406 b = list_get_instance(l, block_t, free_link);
407
408 fibril_mutex_lock(&b->lock);
409 if (b->dirty) {
410 /*
411 * The block needs to be written back to the
412 * device before it changes identity. Do this
413 * while not holding the cache lock so that
414 * concurrency is not impeded. Also move the
415 * block to the end of the free list so that we
416 * do not slow down other instances of
417 * block_get() draining the free list.
418 */
419 list_remove(&b->free_link);
420 list_append(&b->free_link, &cache->free_head);
421 fibril_mutex_unlock(&cache->lock);
422 fibril_mutex_lock(&devcon->comm_area_lock);
423 memcpy(devcon->comm_area, b->data, b->size);
424 rc = write_blocks(devcon, b->boff, 1);
425 fibril_mutex_unlock(&devcon->comm_area_lock);
426 if (rc != EOK) {
427 /*
428 * We did not manage to write the block
429 * to the device. Keep it around for
430 * another try. Hopefully, we will grab
431 * another block next time.
432 */
433 fibril_mutex_unlock(&b->lock);
434 goto retry;
435 }
436 b->dirty = false;
437 if (!fibril_mutex_trylock(&cache->lock)) {
438 /*
439 * Somebody is probably racing with us.
440 * Unlock the block and retry.
441 */
442 fibril_mutex_unlock(&b->lock);
443 goto retry;
444 }
445
446 }
447 fibril_mutex_unlock(&b->lock);
448
449 /*
450 * Unlink the block from the free list and the hash
451 * table.
452 */
453 list_remove(&b->free_link);
454 temp_key = b->boff;
455 hash_table_remove(&cache->block_hash, &temp_key, 1);
456 }
457
458 block_initialize(b);
459 b->dev_handle = dev_handle;
460 b->size = cache->lblock_size;
461 b->boff = boff;
462 hash_table_insert(&cache->block_hash, &key, &b->hash_link);
463
464 /*
465 * Lock the block before releasing the cache lock. Thus we don't
466 * kill concurrent operations on the cache while doing I/O on
467 * the block.
468 */
469 fibril_mutex_lock(&b->lock);
470 fibril_mutex_unlock(&cache->lock);
471
472 if (!(flags & BLOCK_FLAGS_NOREAD)) {
473 /*
474 * The block contains old or no data. We need to read
475 * the new contents from the device.
476 */
477 fibril_mutex_lock(&devcon->comm_area_lock);
478 rc = read_blocks(devcon, b->boff, 1);
479 memcpy(b->data, devcon->comm_area, cache->lblock_size);
480 fibril_mutex_unlock(&devcon->comm_area_lock);
481 if (rc != EOK)
482 b->toxic = true;
483 } else
484 rc = EOK;
485
486 fibril_mutex_unlock(&b->lock);
487 }
488out:
489 if ((rc != EOK) && b) {
490 assert(b->toxic);
491 (void) block_put(b);
492 b = NULL;
493 }
494 *block = b;
495 return rc;
496}
497
498/** Release a reference to a block.
499 *
500 * If the last reference is dropped, the block is put on the free list.
501 *
502 * @param block Block of which a reference is to be released.
503 *
504 * @return EOK on success or a negative error code.
505 */
506int block_put(block_t *block)
507{
508 devcon_t *devcon = devcon_search(block->dev_handle);
509 cache_t *cache;
510 unsigned blocks_cached;
511 enum cache_mode mode;
512 int rc = EOK;
513
514 assert(devcon);
515 assert(devcon->cache);
516
517 cache = devcon->cache;
518
519retry:
520 fibril_mutex_lock(&cache->lock);
521 blocks_cached = cache->blocks_cached;
522 mode = cache->mode;
523 fibril_mutex_unlock(&cache->lock);
524
525 /*
526 * Determine whether to sync the block. Syncing the block is best done
527 * when not holding the cache lock as it does not impede concurrency.
528 * Since the situation may have changed when we unlocked the cache, the
529 * blocks_cached and mode variables are mere hints. We will recheck the
530 * conditions later when the cache lock is held again.
531 */
532 fibril_mutex_lock(&block->lock);
533 if (block->toxic)
534 block->dirty = false; /* will not write back toxic block */
535 if (block->dirty && (block->refcnt == 1) &&
536 (blocks_cached > CACHE_HI_WATERMARK || mode != CACHE_MODE_WB)) {
537 fibril_mutex_lock(&devcon->comm_area_lock);
538 memcpy(devcon->comm_area, block->data, block->size);
539 rc = write_blocks(devcon, block->boff, 1);
540 fibril_mutex_unlock(&devcon->comm_area_lock);
541 block->dirty = false;
542 }
543 fibril_mutex_unlock(&block->lock);
544
545 fibril_mutex_lock(&cache->lock);
546 fibril_mutex_lock(&block->lock);
547 if (!--block->refcnt) {
548 /*
549 * Last reference to the block was dropped. Either free the
550 * block or put it on the free list. In case of an I/O error,
551 * free the block.
552 */
553 if ((cache->blocks_cached > CACHE_HI_WATERMARK) ||
554 (rc != EOK)) {
555 /*
556 * Currently there are too many cached blocks or there
557 * was an I/O error when writing the block back to the
558 * device.
559 */
560 if (block->dirty) {
561 /*
562 * We cannot sync the block while holding the
563 * cache lock. Release everything and retry.
564 */
565 block->refcnt++;
566 fibril_mutex_unlock(&block->lock);
567 fibril_mutex_unlock(&cache->lock);
568 goto retry;
569 }
570 /*
571 * Take the block out of the cache and free it.
572 */
573 unsigned long key = block->boff;
574 hash_table_remove(&cache->block_hash, &key, 1);
575 free(block);
576 free(block->data);
577 cache->blocks_cached--;
578 fibril_mutex_unlock(&cache->lock);
579 return rc;
580 }
581 /*
582 * Put the block on the free list.
583 */
584 if (cache->mode != CACHE_MODE_WB && block->dirty) {
585 /*
586 * We cannot sync the block while holding the cache
587 * lock. Release everything and retry.
588 */
589 block->refcnt++;
590 fibril_mutex_unlock(&block->lock);
591 fibril_mutex_unlock(&cache->lock);
592 goto retry;
593 }
594 list_append(&block->free_link, &cache->free_head);
595 }
596 fibril_mutex_unlock(&block->lock);
597 fibril_mutex_unlock(&cache->lock);
598
599 return rc;
600}
601
602/** Read sequential data from a block device.
603 *
604 * @param dev_handle Device handle of the block device.
605 * @param bufpos Pointer to the first unread valid offset within the
606 * communication buffer.
607 * @param buflen Pointer to the number of unread bytes that are ready in
608 * the communication buffer.
609 * @param pos Device position to be read.
610 * @param dst Destination buffer.
611 * @param size Size of the destination buffer.
612 * @param block_size Block size to be used for the transfer.
613 *
614 * @return EOK on success or a negative return code on failure.
615 */
616int block_seqread(dev_handle_t dev_handle, off_t *bufpos, size_t *buflen,
617 off_t *pos, void *dst, size_t size)
618{
619 off_t offset = 0;
620 size_t left = size;
621 size_t block_size;
622 devcon_t *devcon;
623
624 devcon = devcon_search(dev_handle);
625 assert(devcon);
626 block_size = devcon->pblock_size;
627
628 fibril_mutex_lock(&devcon->comm_area_lock);
629 while (left > 0) {
630 size_t rd;
631
632 if (*bufpos + left < *buflen)
633 rd = left;
634 else
635 rd = *buflen - *bufpos;
636
637 if (rd > 0) {
638 /*
639 * Copy the contents of the communication buffer to the
640 * destination buffer.
641 */
642 memcpy(dst + offset, devcon->comm_area + *bufpos, rd);
643 offset += rd;
644 *bufpos += rd;
645 *pos += rd;
646 left -= rd;
647 }
648
649 if (*bufpos == (off_t) *buflen) {
650 /* Refill the communication buffer with a new block. */
651 int rc;
652
653 rc = read_blocks(devcon, *pos / block_size, 1);
654 if (rc != EOK) {
655 fibril_mutex_unlock(&devcon->comm_area_lock);
656 return rc;
657 }
658
659 *bufpos = 0;
660 *buflen = block_size;
661 }
662 }
663 fibril_mutex_unlock(&devcon->comm_area_lock);
664
665 return EOK;
666}
667
668/** Read blocks directly from device (bypass cache).
669 *
670 * @param dev_handle Device handle of the block device.
671 * @param ba Address of first block.
672 * @param cnt Number of blocks.
673 * @param src Buffer for storing the data.
674 *
675 * @return EOK on success or negative error code on failure.
676 */
677int block_read_direct(dev_handle_t dev_handle, bn_t ba, size_t cnt, void *buf)
678{
679 devcon_t *devcon;
680 int rc;
681
682 devcon = devcon_search(dev_handle);
683 assert(devcon);
684
685 fibril_mutex_lock(&devcon->comm_area_lock);
686
687 rc = read_blocks(devcon, ba, cnt);
688 if (rc == EOK)
689 memcpy(buf, devcon->comm_area, devcon->pblock_size * cnt);
690
691 fibril_mutex_unlock(&devcon->comm_area_lock);
692
693 return rc;
694}
695
696/** Write blocks directly to device (bypass cache).
697 *
698 * @param dev_handle Device handle of the block device.
699 * @param ba Address of first block.
700 * @param cnt Number of blocks.
701 * @param src The data to be written.
702 *
703 * @return EOK on success or negative error code on failure.
704 */
705int block_write_direct(dev_handle_t dev_handle, bn_t ba, size_t cnt,
706 const void *data)
707{
708 devcon_t *devcon;
709 int rc;
710
711 devcon = devcon_search(dev_handle);
712 assert(devcon);
713
714 fibril_mutex_lock(&devcon->comm_area_lock);
715
716 memcpy(devcon->comm_area, data, devcon->pblock_size * cnt);
717 rc = write_blocks(devcon, ba, cnt);
718
719 fibril_mutex_unlock(&devcon->comm_area_lock);
720
721 return rc;
722}
723
724/** Get device block size.
725 *
726 * @param dev_handle Device handle of the block device.
727 * @param bsize Output block size.
728 *
729 * @return EOK on success or negative error code on failure.
730 */
731int block_get_bsize(dev_handle_t dev_handle, size_t *bsize)
732{
733 devcon_t *devcon;
734
735 devcon = devcon_search(dev_handle);
736 assert(devcon);
737
738 return get_block_size(devcon->dev_phone, bsize);
739}
740
741/** Get number of blocks on device.
742 *
743 * @param dev_handle Device handle of the block device.
744 * @param nblocks Output number of blocks.
745 *
746 * @return EOK on success or negative error code on failure.
747 */
748int block_get_nblocks(dev_handle_t dev_handle, bn_t *nblocks)
749{
750 devcon_t *devcon;
751
752 devcon = devcon_search(dev_handle);
753 assert(devcon);
754
755 return get_num_blocks(devcon->dev_phone, nblocks);
756}
757
758/** Read blocks from block device.
759 *
760 * @param devcon Device connection.
761 * @param ba Address of first block.
762 * @param cnt Number of blocks.
763 * @param src Buffer for storing the data.
764 *
765 * @return EOK on success or negative error code on failure.
766 */
767static int read_blocks(devcon_t *devcon, bn_t ba, size_t cnt)
768{
769 int rc;
770
771 assert(devcon);
772 rc = async_req_3_0(devcon->dev_phone, BD_READ_BLOCKS, LOWER32(ba),
773 UPPER32(ba), cnt);
774 return rc;
775}
776
777/** Write block to block device.
778 *
779 * @param devcon Device connection.
780 * @param ba Address of first block.
781 * @param cnt Number of blocks.
782 * @param src Buffer containing the data to write.
783 *
784 * @return EOK on success or negative error code on failure.
785 */
786static int write_blocks(devcon_t *devcon, bn_t ba, size_t cnt)
787{
788 int rc;
789
790 assert(devcon);
791 rc = async_req_3_0(devcon->dev_phone, BD_WRITE_BLOCKS, LOWER32(ba),
792 UPPER32(ba), cnt);
793 return rc;
794}
795
796/** Get block size used by the device. */
797static int get_block_size(int dev_phone, size_t *bsize)
798{
799 ipcarg_t bs;
800 int rc;
801
802 rc = async_req_0_1(dev_phone, BD_GET_BLOCK_SIZE, &bs);
803 if (rc == EOK)
804 *bsize = (size_t) bs;
805
806 return rc;
807}
808
809/** Get total number of blocks on block device. */
810static int get_num_blocks(int dev_phone, bn_t *nblocks)
811{
812 ipcarg_t nb_l, nb_h;
813 int rc;
814
815 rc = async_req_0_2(dev_phone, BD_GET_NUM_BLOCKS, &nb_l, &nb_h);
816 if (rc == EOK) {
817 *nblocks = (bn_t) MERGE_LOUP32(nb_l, nb_h);
818 }
819
820 return rc;
821}
822
823/** @}
824 */
Note: See TracBrowser for help on using the repository browser.