source: mainline/uspace/lib/block/block.c@ 5dfb70c9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5dfb70c9 was 7354b5e, checked in by Jakub Jermar <jakub@…>, 8 years ago

Remove sys/typefmt.h

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