source: mainline/uspace/lib/block/block.c@ 24e58cc

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

remove cross-include (thx Jiri Zarevucky)

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