source: mainline/uspace/lib/block/libblock.c@ 4802dd7

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

Factor out client and server IPC stubs for block devices.

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