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