source: mainline/uspace/lib/block/block.c@ f27f3fd

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

libblock is not the best place to define SCSI structures.

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