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