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 <ipc/ipc.h>
|
---|
47 | #include <as.h>
|
---|
48 | #include <assert.h>
|
---|
49 | #include <fibril_sync.h>
|
---|
50 | #include <adt/list.h>
|
---|
51 | #include <adt/hash_table.h>
|
---|
52 | #include <mem.h>
|
---|
53 |
|
---|
54 | /** Lock protecting the device connection list */
|
---|
55 | static FIBRIL_MUTEX_INITIALIZE(dcl_lock);
|
---|
56 | /** Device connection list head. */
|
---|
57 | static LIST_INITIALIZE(dcl_head);
|
---|
58 |
|
---|
59 | #define CACHE_BUCKETS_LOG2 10
|
---|
60 | #define CACHE_BUCKETS (1 << CACHE_BUCKETS_LOG2)
|
---|
61 |
|
---|
62 | typedef struct {
|
---|
63 | fibril_mutex_t lock;
|
---|
64 | size_t block_size; /**< Block size. */
|
---|
65 | unsigned block_count; /**< Total number of blocks. */
|
---|
66 | unsigned blocks_cached; /**< Number of cached blocks. */
|
---|
67 | hash_table_t block_hash;
|
---|
68 | link_t free_head;
|
---|
69 | enum cache_mode mode;
|
---|
70 | } cache_t;
|
---|
71 |
|
---|
72 | typedef struct {
|
---|
73 | link_t link;
|
---|
74 | dev_handle_t dev_handle;
|
---|
75 | int dev_phone;
|
---|
76 | fibril_mutex_t com_area_lock;
|
---|
77 | void *com_area;
|
---|
78 | size_t com_size;
|
---|
79 | void *bb_buf;
|
---|
80 | off_t bb_off;
|
---|
81 | size_t bb_size;
|
---|
82 | cache_t *cache;
|
---|
83 | } devcon_t;
|
---|
84 |
|
---|
85 | static int read_block(devcon_t *devcon, bn_t boff, size_t block_size);
|
---|
86 | static int write_block(devcon_t *devcon, bn_t boff, size_t block_size);
|
---|
87 |
|
---|
88 | static devcon_t *devcon_search(dev_handle_t dev_handle)
|
---|
89 | {
|
---|
90 | link_t *cur;
|
---|
91 |
|
---|
92 | fibril_mutex_lock(&dcl_lock);
|
---|
93 | for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
|
---|
94 | devcon_t *devcon = list_get_instance(cur, devcon_t, link);
|
---|
95 | if (devcon->dev_handle == dev_handle) {
|
---|
96 | fibril_mutex_unlock(&dcl_lock);
|
---|
97 | return devcon;
|
---|
98 | }
|
---|
99 | }
|
---|
100 | fibril_mutex_unlock(&dcl_lock);
|
---|
101 | return NULL;
|
---|
102 | }
|
---|
103 |
|
---|
104 | static int devcon_add(dev_handle_t dev_handle, int dev_phone, void *com_area,
|
---|
105 | size_t com_size)
|
---|
106 | {
|
---|
107 | link_t *cur;
|
---|
108 | devcon_t *devcon;
|
---|
109 |
|
---|
110 | devcon = malloc(sizeof(devcon_t));
|
---|
111 | if (!devcon)
|
---|
112 | return ENOMEM;
|
---|
113 |
|
---|
114 | link_initialize(&devcon->link);
|
---|
115 | devcon->dev_handle = dev_handle;
|
---|
116 | devcon->dev_phone = dev_phone;
|
---|
117 | fibril_mutex_initialize(&devcon->com_area_lock);
|
---|
118 | devcon->com_area = com_area;
|
---|
119 | devcon->com_size = com_size;
|
---|
120 | devcon->bb_buf = NULL;
|
---|
121 | devcon->bb_off = 0;
|
---|
122 | devcon->bb_size = 0;
|
---|
123 | devcon->cache = NULL;
|
---|
124 |
|
---|
125 | fibril_mutex_lock(&dcl_lock);
|
---|
126 | for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
|
---|
127 | devcon_t *d = list_get_instance(cur, devcon_t, link);
|
---|
128 | if (d->dev_handle == dev_handle) {
|
---|
129 | fibril_mutex_unlock(&dcl_lock);
|
---|
130 | free(devcon);
|
---|
131 | return EEXIST;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | list_append(&devcon->link, &dcl_head);
|
---|
135 | fibril_mutex_unlock(&dcl_lock);
|
---|
136 | return EOK;
|
---|
137 | }
|
---|
138 |
|
---|
139 | static void devcon_remove(devcon_t *devcon)
|
---|
140 | {
|
---|
141 | fibril_mutex_lock(&dcl_lock);
|
---|
142 | list_remove(&devcon->link);
|
---|
143 | fibril_mutex_unlock(&dcl_lock);
|
---|
144 | }
|
---|
145 |
|
---|
146 | int block_init(dev_handle_t dev_handle, size_t com_size)
|
---|
147 | {
|
---|
148 | int rc;
|
---|
149 | int dev_phone;
|
---|
150 | void *com_area;
|
---|
151 |
|
---|
152 | com_area = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE,
|
---|
153 | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
|
---|
154 | if (!com_area) {
|
---|
155 | return ENOMEM;
|
---|
156 | }
|
---|
157 |
|
---|
158 | dev_phone = devmap_device_connect(dev_handle, IPC_FLAG_BLOCKING);
|
---|
159 | if (dev_phone < 0) {
|
---|
160 | munmap(com_area, com_size);
|
---|
161 | return dev_phone;
|
---|
162 | }
|
---|
163 |
|
---|
164 | rc = ipc_share_out_start(dev_phone, com_area,
|
---|
165 | AS_AREA_READ | AS_AREA_WRITE);
|
---|
166 | if (rc != EOK) {
|
---|
167 | munmap(com_area, com_size);
|
---|
168 | ipc_hangup(dev_phone);
|
---|
169 | return rc;
|
---|
170 | }
|
---|
171 |
|
---|
172 | rc = devcon_add(dev_handle, dev_phone, com_area, com_size);
|
---|
173 | if (rc != EOK) {
|
---|
174 | munmap(com_area, com_size);
|
---|
175 | ipc_hangup(dev_phone);
|
---|
176 | return rc;
|
---|
177 | }
|
---|
178 |
|
---|
179 | return EOK;
|
---|
180 | }
|
---|
181 |
|
---|
182 | void block_fini(dev_handle_t dev_handle)
|
---|
183 | {
|
---|
184 | devcon_t *devcon = devcon_search(dev_handle);
|
---|
185 | assert(devcon);
|
---|
186 |
|
---|
187 | devcon_remove(devcon);
|
---|
188 |
|
---|
189 | if (devcon->bb_buf)
|
---|
190 | free(devcon->bb_buf);
|
---|
191 |
|
---|
192 | if (devcon->cache) {
|
---|
193 | hash_table_destroy(&devcon->cache->block_hash);
|
---|
194 | free(devcon->cache);
|
---|
195 | }
|
---|
196 |
|
---|
197 | munmap(devcon->com_area, devcon->com_size);
|
---|
198 | ipc_hangup(devcon->dev_phone);
|
---|
199 |
|
---|
200 | free(devcon);
|
---|
201 | }
|
---|
202 |
|
---|
203 | int block_bb_read(dev_handle_t dev_handle, off_t off, size_t size)
|
---|
204 | {
|
---|
205 | void *bb_buf;
|
---|
206 | int rc;
|
---|
207 |
|
---|
208 | devcon_t *devcon = devcon_search(dev_handle);
|
---|
209 | if (!devcon)
|
---|
210 | return ENOENT;
|
---|
211 | if (devcon->bb_buf)
|
---|
212 | return EEXIST;
|
---|
213 | bb_buf = malloc(size);
|
---|
214 | if (!bb_buf)
|
---|
215 | return ENOMEM;
|
---|
216 |
|
---|
217 | fibril_mutex_lock(&devcon->com_area_lock);
|
---|
218 | rc = read_block(devcon, 0, size);
|
---|
219 | if (rc != EOK) {
|
---|
220 | fibril_mutex_unlock(&devcon->com_area_lock);
|
---|
221 | free(bb_buf);
|
---|
222 | return rc;
|
---|
223 | }
|
---|
224 | memcpy(bb_buf, devcon->com_area, size);
|
---|
225 | fibril_mutex_unlock(&devcon->com_area_lock);
|
---|
226 |
|
---|
227 | devcon->bb_buf = bb_buf;
|
---|
228 | devcon->bb_off = off;
|
---|
229 | devcon->bb_size = size;
|
---|
230 |
|
---|
231 | return EOK;
|
---|
232 | }
|
---|
233 |
|
---|
234 | void *block_bb_get(dev_handle_t dev_handle)
|
---|
235 | {
|
---|
236 | devcon_t *devcon = devcon_search(dev_handle);
|
---|
237 | assert(devcon);
|
---|
238 | return devcon->bb_buf;
|
---|
239 | }
|
---|
240 |
|
---|
241 | static hash_index_t cache_hash(unsigned long *key)
|
---|
242 | {
|
---|
243 | return *key & (CACHE_BUCKETS - 1);
|
---|
244 | }
|
---|
245 |
|
---|
246 | static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item)
|
---|
247 | {
|
---|
248 | block_t *b = hash_table_get_instance(item, block_t, hash_link);
|
---|
249 | return b->boff == *key;
|
---|
250 | }
|
---|
251 |
|
---|
252 | static void cache_remove_callback(link_t *item)
|
---|
253 | {
|
---|
254 | }
|
---|
255 |
|
---|
256 | static hash_table_operations_t cache_ops = {
|
---|
257 | .hash = cache_hash,
|
---|
258 | .compare = cache_compare,
|
---|
259 | .remove_callback = cache_remove_callback
|
---|
260 | };
|
---|
261 |
|
---|
262 | int block_cache_init(dev_handle_t dev_handle, size_t size, unsigned blocks,
|
---|
263 | enum cache_mode mode)
|
---|
264 | {
|
---|
265 | devcon_t *devcon = devcon_search(dev_handle);
|
---|
266 | cache_t *cache;
|
---|
267 | if (!devcon)
|
---|
268 | return ENOENT;
|
---|
269 | if (devcon->cache)
|
---|
270 | return EEXIST;
|
---|
271 | cache = malloc(sizeof(cache_t));
|
---|
272 | if (!cache)
|
---|
273 | return ENOMEM;
|
---|
274 |
|
---|
275 | fibril_mutex_initialize(&cache->lock);
|
---|
276 | list_initialize(&cache->free_head);
|
---|
277 | cache->block_size = size;
|
---|
278 | cache->block_count = blocks;
|
---|
279 | cache->blocks_cached = 0;
|
---|
280 | cache->mode = mode;
|
---|
281 |
|
---|
282 | if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1,
|
---|
283 | &cache_ops)) {
|
---|
284 | free(cache);
|
---|
285 | return ENOMEM;
|
---|
286 | }
|
---|
287 |
|
---|
288 | devcon->cache = cache;
|
---|
289 | return EOK;
|
---|
290 | }
|
---|
291 |
|
---|
292 | #define CACHE_LO_WATERMARK 10
|
---|
293 | #define CACHE_HI_WATERMARK 20
|
---|
294 | static bool cache_can_grow(cache_t *cache)
|
---|
295 | {
|
---|
296 | if (cache->blocks_cached < CACHE_LO_WATERMARK)
|
---|
297 | return true;
|
---|
298 | if (!list_empty(&cache->free_head))
|
---|
299 | return false;
|
---|
300 | return true;
|
---|
301 | }
|
---|
302 |
|
---|
303 | static void block_initialize(block_t *b)
|
---|
304 | {
|
---|
305 | fibril_mutex_initialize(&b->lock);
|
---|
306 | b->refcnt = 1;
|
---|
307 | b->dirty = false;
|
---|
308 | b->toxic = false;
|
---|
309 | fibril_rwlock_initialize(&b->contents_lock);
|
---|
310 | link_initialize(&b->free_link);
|
---|
311 | link_initialize(&b->hash_link);
|
---|
312 | }
|
---|
313 |
|
---|
314 | /** Instantiate a block in memory and get a reference to it.
|
---|
315 | *
|
---|
316 | * @param block Pointer to where the function will store the
|
---|
317 | * block pointer on success.
|
---|
318 | * @param dev_handle Device handle of the block device.
|
---|
319 | * @param boff Block offset.
|
---|
320 | * @param flags If BLOCK_FLAGS_NOREAD is specified, block_get()
|
---|
321 | * will not read the contents of the block from the
|
---|
322 | * device.
|
---|
323 | *
|
---|
324 | * @return EOK on success or a negative error code.
|
---|
325 | */
|
---|
326 | int block_get(block_t **block, dev_handle_t dev_handle, bn_t boff, int flags)
|
---|
327 | {
|
---|
328 | devcon_t *devcon;
|
---|
329 | cache_t *cache;
|
---|
330 | block_t *b;
|
---|
331 | link_t *l;
|
---|
332 | unsigned long key = boff;
|
---|
333 | int rc;
|
---|
334 |
|
---|
335 | devcon = devcon_search(dev_handle);
|
---|
336 |
|
---|
337 | assert(devcon);
|
---|
338 | assert(devcon->cache);
|
---|
339 |
|
---|
340 | cache = devcon->cache;
|
---|
341 |
|
---|
342 | retry:
|
---|
343 | rc = EOK;
|
---|
344 | b = NULL;
|
---|
345 |
|
---|
346 | fibril_mutex_lock(&cache->lock);
|
---|
347 | l = hash_table_find(&cache->block_hash, &key);
|
---|
348 | if (l) {
|
---|
349 | /*
|
---|
350 | * We found the block in the cache.
|
---|
351 | */
|
---|
352 | b = hash_table_get_instance(l, block_t, hash_link);
|
---|
353 | fibril_mutex_lock(&b->lock);
|
---|
354 | if (b->refcnt++ == 0)
|
---|
355 | list_remove(&b->free_link);
|
---|
356 | if (b->toxic)
|
---|
357 | rc = EIO;
|
---|
358 | fibril_mutex_unlock(&b->lock);
|
---|
359 | fibril_mutex_unlock(&cache->lock);
|
---|
360 | } else {
|
---|
361 | /*
|
---|
362 | * The block was not found in the cache.
|
---|
363 | */
|
---|
364 | if (cache_can_grow(cache)) {
|
---|
365 | /*
|
---|
366 | * We can grow the cache by allocating new blocks.
|
---|
367 | * Should the allocation fail, we fail over and try to
|
---|
368 | * recycle a block from the cache.
|
---|
369 | */
|
---|
370 | b = malloc(sizeof(block_t));
|
---|
371 | if (!b)
|
---|
372 | goto recycle;
|
---|
373 | b->data = malloc(cache->block_size);
|
---|
374 | if (!b->data) {
|
---|
375 | free(b);
|
---|
376 | goto recycle;
|
---|
377 | }
|
---|
378 | cache->blocks_cached++;
|
---|
379 | } else {
|
---|
380 | /*
|
---|
381 | * Try to recycle a block from the free list.
|
---|
382 | */
|
---|
383 | unsigned long temp_key;
|
---|
384 | recycle:
|
---|
385 | if (list_empty(&cache->free_head)) {
|
---|
386 | fibril_mutex_unlock(&cache->lock);
|
---|
387 | rc = ENOMEM;
|
---|
388 | goto out;
|
---|
389 | }
|
---|
390 | l = cache->free_head.next;
|
---|
391 | b = list_get_instance(l, block_t, free_link);
|
---|
392 |
|
---|
393 | fibril_mutex_lock(&b->lock);
|
---|
394 | if (b->dirty) {
|
---|
395 | /*
|
---|
396 | * The block needs to be written back to the
|
---|
397 | * device before it changes identity. Do this
|
---|
398 | * while not holding the cache lock so that
|
---|
399 | * concurrency is not impeded. Also move the
|
---|
400 | * block to the end of the free list so that we
|
---|
401 | * do not slow down other instances of
|
---|
402 | * block_get() draining the free list.
|
---|
403 | */
|
---|
404 | list_remove(&b->free_link);
|
---|
405 | list_append(&b->free_link, &cache->free_head);
|
---|
406 | fibril_mutex_unlock(&cache->lock);
|
---|
407 | fibril_mutex_lock(&devcon->com_area_lock);
|
---|
408 | memcpy(devcon->com_area, b->data, b->size);
|
---|
409 | rc = write_block(devcon, b->boff,
|
---|
410 | cache->block_size);
|
---|
411 | fibril_mutex_unlock(&devcon->com_area_lock);
|
---|
412 | if (rc != EOK) {
|
---|
413 | /*
|
---|
414 | * We did not manage to write the block
|
---|
415 | * to the device. Keep it around for
|
---|
416 | * another try. Hopefully, we will grab
|
---|
417 | * another block next time.
|
---|
418 | */
|
---|
419 | fibril_mutex_unlock(&b->lock);
|
---|
420 | goto retry;
|
---|
421 | }
|
---|
422 | b->dirty = false;
|
---|
423 | if (!fibril_mutex_trylock(&cache->lock)) {
|
---|
424 | /*
|
---|
425 | * Somebody is probably racing with us.
|
---|
426 | * Unlock the block and retry.
|
---|
427 | */
|
---|
428 | fibril_mutex_unlock(&b->lock);
|
---|
429 | goto retry;
|
---|
430 | }
|
---|
431 |
|
---|
432 | }
|
---|
433 | fibril_mutex_unlock(&b->lock);
|
---|
434 |
|
---|
435 | /*
|
---|
436 | * Unlink the block from the free list and the hash
|
---|
437 | * table.
|
---|
438 | */
|
---|
439 | list_remove(&b->free_link);
|
---|
440 | temp_key = b->boff;
|
---|
441 | hash_table_remove(&cache->block_hash, &temp_key, 1);
|
---|
442 | }
|
---|
443 |
|
---|
444 | block_initialize(b);
|
---|
445 | b->dev_handle = dev_handle;
|
---|
446 | b->size = cache->block_size;
|
---|
447 | b->boff = boff;
|
---|
448 | hash_table_insert(&cache->block_hash, &key, &b->hash_link);
|
---|
449 |
|
---|
450 | /*
|
---|
451 | * Lock the block before releasing the cache lock. Thus we don't
|
---|
452 | * kill concurrent operations on the cache while doing I/O on
|
---|
453 | * the block.
|
---|
454 | */
|
---|
455 | fibril_mutex_lock(&b->lock);
|
---|
456 | fibril_mutex_unlock(&cache->lock);
|
---|
457 |
|
---|
458 | if (!(flags & BLOCK_FLAGS_NOREAD)) {
|
---|
459 | /*
|
---|
460 | * The block contains old or no data. We need to read
|
---|
461 | * the new contents from the device.
|
---|
462 | */
|
---|
463 | fibril_mutex_lock(&devcon->com_area_lock);
|
---|
464 | rc = read_block(devcon, b->boff, cache->block_size);
|
---|
465 | memcpy(b->data, devcon->com_area, cache->block_size);
|
---|
466 | fibril_mutex_unlock(&devcon->com_area_lock);
|
---|
467 | if (rc != EOK)
|
---|
468 | b->toxic = true;
|
---|
469 | } else
|
---|
470 | rc = EOK;
|
---|
471 |
|
---|
472 | fibril_mutex_unlock(&b->lock);
|
---|
473 | }
|
---|
474 | out:
|
---|
475 | if ((rc != EOK) && b) {
|
---|
476 | assert(b->toxic);
|
---|
477 | (void) block_put(b);
|
---|
478 | b = NULL;
|
---|
479 | }
|
---|
480 | *block = b;
|
---|
481 | return rc;
|
---|
482 | }
|
---|
483 |
|
---|
484 | /** Release a reference to a block.
|
---|
485 | *
|
---|
486 | * If the last reference is dropped, the block is put on the free list.
|
---|
487 | *
|
---|
488 | * @param block Block of which a reference is to be released.
|
---|
489 | *
|
---|
490 | * @return EOK on success or a negative error code.
|
---|
491 | */
|
---|
492 | int block_put(block_t *block)
|
---|
493 | {
|
---|
494 | devcon_t *devcon = devcon_search(block->dev_handle);
|
---|
495 | cache_t *cache;
|
---|
496 | unsigned blocks_cached;
|
---|
497 | enum cache_mode mode;
|
---|
498 | int rc = EOK;
|
---|
499 |
|
---|
500 | assert(devcon);
|
---|
501 | assert(devcon->cache);
|
---|
502 |
|
---|
503 | cache = devcon->cache;
|
---|
504 |
|
---|
505 | retry:
|
---|
506 | fibril_mutex_lock(&cache->lock);
|
---|
507 | blocks_cached = cache->blocks_cached;
|
---|
508 | mode = cache->mode;
|
---|
509 | fibril_mutex_unlock(&cache->lock);
|
---|
510 |
|
---|
511 | /*
|
---|
512 | * Determine whether to sync the block. Syncing the block is best done
|
---|
513 | * when not holding the cache lock as it does not impede concurrency.
|
---|
514 | * Since the situation may have changed when we unlocked the cache, the
|
---|
515 | * blocks_cached and mode variables are mere hints. We will recheck the
|
---|
516 | * conditions later when the cache lock is held again.
|
---|
517 | */
|
---|
518 | fibril_mutex_lock(&block->lock);
|
---|
519 | if (block->toxic)
|
---|
520 | block->dirty = false; /* will not write back toxic block */
|
---|
521 | if (block->dirty && (block->refcnt == 1) &&
|
---|
522 | (blocks_cached > CACHE_HI_WATERMARK || mode != CACHE_MODE_WB)) {
|
---|
523 | fibril_mutex_lock(&devcon->com_area_lock);
|
---|
524 | memcpy(devcon->com_area, block->data, block->size);
|
---|
525 | rc = write_block(devcon, block->boff, block->size);
|
---|
526 | fibril_mutex_unlock(&devcon->com_area_lock);
|
---|
527 | block->dirty = false;
|
---|
528 | }
|
---|
529 | fibril_mutex_unlock(&block->lock);
|
---|
530 |
|
---|
531 | fibril_mutex_lock(&cache->lock);
|
---|
532 | fibril_mutex_lock(&block->lock);
|
---|
533 | if (!--block->refcnt) {
|
---|
534 | /*
|
---|
535 | * Last reference to the block was dropped. Either free the
|
---|
536 | * block or put it on the free list. In case of an I/O error,
|
---|
537 | * free the block.
|
---|
538 | */
|
---|
539 | if ((cache->blocks_cached > CACHE_HI_WATERMARK) ||
|
---|
540 | (rc != EOK)) {
|
---|
541 | /*
|
---|
542 | * Currently there are too many cached blocks or there
|
---|
543 | * was an I/O error when writing the block back to the
|
---|
544 | * device.
|
---|
545 | */
|
---|
546 | if (block->dirty) {
|
---|
547 | /*
|
---|
548 | * We cannot sync the block while holding the
|
---|
549 | * cache lock. Release everything and retry.
|
---|
550 | */
|
---|
551 | block->refcnt++;
|
---|
552 | fibril_mutex_unlock(&block->lock);
|
---|
553 | fibril_mutex_unlock(&cache->lock);
|
---|
554 | goto retry;
|
---|
555 | }
|
---|
556 | /*
|
---|
557 | * Take the block out of the cache and free it.
|
---|
558 | */
|
---|
559 | unsigned long key = block->boff;
|
---|
560 | hash_table_remove(&cache->block_hash, &key, 1);
|
---|
561 | free(block);
|
---|
562 | free(block->data);
|
---|
563 | cache->blocks_cached--;
|
---|
564 | fibril_mutex_unlock(&cache->lock);
|
---|
565 | return rc;
|
---|
566 | }
|
---|
567 | /*
|
---|
568 | * Put the block on the free list.
|
---|
569 | */
|
---|
570 | if (cache->mode != CACHE_MODE_WB && block->dirty) {
|
---|
571 | /*
|
---|
572 | * We cannot sync the block while holding the cache
|
---|
573 | * lock. Release everything and retry.
|
---|
574 | */
|
---|
575 | block->refcnt++;
|
---|
576 | fibril_mutex_unlock(&block->lock);
|
---|
577 | fibril_mutex_unlock(&cache->lock);
|
---|
578 | goto retry;
|
---|
579 | }
|
---|
580 | list_append(&block->free_link, &cache->free_head);
|
---|
581 | }
|
---|
582 | fibril_mutex_unlock(&block->lock);
|
---|
583 | fibril_mutex_unlock(&cache->lock);
|
---|
584 |
|
---|
585 | return rc;
|
---|
586 | }
|
---|
587 |
|
---|
588 | /** Read sequential data from a block device.
|
---|
589 | *
|
---|
590 | * @param dev_handle Device handle of the block device.
|
---|
591 | * @param bufpos Pointer to the first unread valid offset within the
|
---|
592 | * communication buffer.
|
---|
593 | * @param buflen Pointer to the number of unread bytes that are ready in
|
---|
594 | * the communication buffer.
|
---|
595 | * @param pos Device position to be read.
|
---|
596 | * @param dst Destination buffer.
|
---|
597 | * @param size Size of the destination buffer.
|
---|
598 | * @param block_size Block size to be used for the transfer.
|
---|
599 | *
|
---|
600 | * @return EOK on success or a negative return code on failure.
|
---|
601 | */
|
---|
602 | int block_seqread(dev_handle_t dev_handle, off_t *bufpos, size_t *buflen,
|
---|
603 | off_t *pos, void *dst, size_t size, size_t block_size)
|
---|
604 | {
|
---|
605 | off_t offset = 0;
|
---|
606 | size_t left = size;
|
---|
607 | devcon_t *devcon = devcon_search(dev_handle);
|
---|
608 | assert(devcon);
|
---|
609 |
|
---|
610 | fibril_mutex_lock(&devcon->com_area_lock);
|
---|
611 | while (left > 0) {
|
---|
612 | size_t rd;
|
---|
613 |
|
---|
614 | if (*bufpos + left < *buflen)
|
---|
615 | rd = left;
|
---|
616 | else
|
---|
617 | rd = *buflen - *bufpos;
|
---|
618 |
|
---|
619 | if (rd > 0) {
|
---|
620 | /*
|
---|
621 | * Copy the contents of the communication buffer to the
|
---|
622 | * destination buffer.
|
---|
623 | */
|
---|
624 | memcpy(dst + offset, devcon->com_area + *bufpos, rd);
|
---|
625 | offset += rd;
|
---|
626 | *bufpos += rd;
|
---|
627 | *pos += rd;
|
---|
628 | left -= rd;
|
---|
629 | }
|
---|
630 |
|
---|
631 | if (*bufpos == (off_t) *buflen) {
|
---|
632 | /* Refill the communication buffer with a new block. */
|
---|
633 | int rc;
|
---|
634 |
|
---|
635 | rc = read_block(devcon, *pos / block_size, block_size);
|
---|
636 | if (rc != EOK) {
|
---|
637 | fibril_mutex_unlock(&devcon->com_area_lock);
|
---|
638 | return rc;
|
---|
639 | }
|
---|
640 |
|
---|
641 | *bufpos = 0;
|
---|
642 | *buflen = block_size;
|
---|
643 | }
|
---|
644 | }
|
---|
645 | fibril_mutex_unlock(&devcon->com_area_lock);
|
---|
646 |
|
---|
647 | return EOK;
|
---|
648 | }
|
---|
649 |
|
---|
650 | /** Read block from block device.
|
---|
651 | *
|
---|
652 | * @param devcon Device connection.
|
---|
653 | * @param boff Block index.
|
---|
654 | * @param block_size Block size.
|
---|
655 | * @param src Buffer for storing the data.
|
---|
656 | *
|
---|
657 | * @return EOK on success or negative error code on failure.
|
---|
658 | */
|
---|
659 | static int read_block(devcon_t *devcon, bn_t boff, size_t block_size)
|
---|
660 | {
|
---|
661 | ipcarg_t retval;
|
---|
662 | int rc;
|
---|
663 |
|
---|
664 | assert(devcon);
|
---|
665 | rc = async_req_2_1(devcon->dev_phone, BD_READ_BLOCK, boff, block_size,
|
---|
666 | &retval);
|
---|
667 | if ((rc != EOK) || (retval != EOK))
|
---|
668 | return (rc != EOK ? rc : (int) retval);
|
---|
669 |
|
---|
670 | return EOK;
|
---|
671 | }
|
---|
672 |
|
---|
673 | /** Write block to block device.
|
---|
674 | *
|
---|
675 | * @param devcon Device connection.
|
---|
676 | * @param boff Block index.
|
---|
677 | * @param block_size Block size.
|
---|
678 | * @param src Buffer containing the data to write.
|
---|
679 | *
|
---|
680 | * @return EOK on success or negative error code on failure.
|
---|
681 | */
|
---|
682 | static int write_block(devcon_t *devcon, bn_t boff, size_t block_size)
|
---|
683 | {
|
---|
684 | ipcarg_t retval;
|
---|
685 | int rc;
|
---|
686 |
|
---|
687 | assert(devcon);
|
---|
688 | rc = async_req_2_1(devcon->dev_phone, BD_WRITE_BLOCK, boff, block_size,
|
---|
689 | &retval);
|
---|
690 | if ((rc != EOK) || (retval != EOK))
|
---|
691 | return (rc != EOK ? rc : (int) retval);
|
---|
692 |
|
---|
693 | return EOK;
|
---|
694 | }
|
---|
695 |
|
---|
696 | /** @}
|
---|
697 | */
|
---|