Subversion Repositories HelenOS

Rev

Rev 4439 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4439 Rev 4537
1
/*
1
/*
2
 * Copyright (c) 2008 Jakub Jermar
2
 * Copyright (c) 2008 Jakub Jermar
3
 * Copyright (c) 2008 Martin Decky
3
 * Copyright (c) 2008 Martin Decky
4
 * All rights reserved.
4
 * All rights reserved.
5
 *
5
 *
6
 * Redistribution and use in source and binary forms, with or without
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
7
 * modification, are permitted provided that the following conditions
8
 * are met:
8
 * are met:
9
 *
9
 *
10
 * - Redistributions of source code must retain the above copyright
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
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
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.
16
 *   derived from this software without specific prior written permission.
17
 *
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
28
 */
29
 
29
 
30
/** @addtogroup libblock
30
/** @addtogroup libblock
31
 * @{
31
 * @{
32
 */
32
 */
33
/**
33
/**
34
 * @file
34
 * @file
35
 * @brief
35
 * @brief
36
 */
36
 */
37
 
37
 
38
#include "libblock.h"
38
#include "libblock.h"
39
#include "../../srv/vfs/vfs.h"
39
#include "../../srv/vfs/vfs.h"
40
#include <ipc/devmap.h>
40
#include <ipc/devmap.h>
41
#include <ipc/bd.h>
41
#include <ipc/bd.h>
42
#include <ipc/services.h>
42
#include <ipc/services.h>
43
#include <errno.h>
43
#include <errno.h>
44
#include <sys/mman.h>
44
#include <sys/mman.h>
45
#include <async.h>
45
#include <async.h>
46
#include <ipc/ipc.h>
46
#include <ipc/ipc.h>
47
#include <as.h>
47
#include <as.h>
48
#include <assert.h>
48
#include <assert.h>
49
#include <futex.h>
49
#include <futex.h>
50
#include <libadt/list.h>
50
#include <adt/list.h>
51
#include <libadt/hash_table.h>
51
#include <adt/hash_table.h>
-
 
52
#include <mem.h>
52
 
53
 
53
/** Lock protecting the device connection list */
54
/** Lock protecting the device connection list */
54
static futex_t dcl_lock = FUTEX_INITIALIZER;
55
static futex_t dcl_lock = FUTEX_INITIALIZER;
55
/** Device connection list head. */
56
/** Device connection list head. */
56
static LIST_INITIALIZE(dcl_head);
57
static LIST_INITIALIZE(dcl_head);
57
 
58
 
58
#define CACHE_BUCKETS_LOG2      10
59
#define CACHE_BUCKETS_LOG2      10
59
#define CACHE_BUCKETS           (1 << CACHE_BUCKETS_LOG2)
60
#define CACHE_BUCKETS           (1 << CACHE_BUCKETS_LOG2)
60
 
61
 
61
typedef struct {
62
typedef struct {
62
    futex_t lock;
63
    futex_t lock;
63
    size_t block_size;      /**< Block size. */
64
    size_t block_size;      /**< Block size. */
64
    unsigned block_count;       /**< Total number of blocks. */
65
    unsigned block_count;       /**< Total number of blocks. */
65
    hash_table_t block_hash;
66
    hash_table_t block_hash;
66
    link_t free_head;
67
    link_t free_head;
67
} cache_t;
68
} cache_t;
68
 
69
 
69
typedef struct {
70
typedef struct {
70
    link_t link;
71
    link_t link;
71
    dev_handle_t dev_handle;
72
    dev_handle_t dev_handle;
72
    int dev_phone;
73
    int dev_phone;
73
    void *com_area;
74
    void *com_area;
74
    size_t com_size;
75
    size_t com_size;
75
    void *bb_buf;
76
    void *bb_buf;
76
    off_t bb_off;
77
    off_t bb_off;
77
    size_t bb_size;
78
    size_t bb_size;
78
    cache_t *cache;
79
    cache_t *cache;
79
} devcon_t;
80
} devcon_t;
80
 
81
 
81
static devcon_t *devcon_search(dev_handle_t dev_handle)
82
static devcon_t *devcon_search(dev_handle_t dev_handle)
82
{
83
{
83
    link_t *cur;
84
    link_t *cur;
84
 
85
 
85
    futex_down(&dcl_lock);
86
    futex_down(&dcl_lock);
86
    for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
87
    for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
87
        devcon_t *devcon = list_get_instance(cur, devcon_t, link);
88
        devcon_t *devcon = list_get_instance(cur, devcon_t, link);
88
        if (devcon->dev_handle == dev_handle) {
89
        if (devcon->dev_handle == dev_handle) {
89
            futex_up(&dcl_lock);
90
            futex_up(&dcl_lock);
90
            return devcon;
91
            return devcon;
91
        }
92
        }
92
    }
93
    }
93
    futex_up(&dcl_lock);
94
    futex_up(&dcl_lock);
94
    return NULL;
95
    return NULL;
95
}
96
}
96
 
97
 
97
static int devcon_add(dev_handle_t dev_handle, int dev_phone, void *com_area,
98
static int devcon_add(dev_handle_t dev_handle, int dev_phone, void *com_area,
98
   size_t com_size)
99
   size_t com_size)
99
{
100
{
100
    link_t *cur;
101
    link_t *cur;
101
    devcon_t *devcon;
102
    devcon_t *devcon;
102
 
103
 
103
    devcon = malloc(sizeof(devcon_t));
104
    devcon = malloc(sizeof(devcon_t));
104
    if (!devcon)
105
    if (!devcon)
105
        return ENOMEM;
106
        return ENOMEM;
106
   
107
   
107
    link_initialize(&devcon->link);
108
    link_initialize(&devcon->link);
108
    devcon->dev_handle = dev_handle;
109
    devcon->dev_handle = dev_handle;
109
    devcon->dev_phone = dev_phone;
110
    devcon->dev_phone = dev_phone;
110
    devcon->com_area = com_area;
111
    devcon->com_area = com_area;
111
    devcon->com_size = com_size;
112
    devcon->com_size = com_size;
112
    devcon->bb_buf = NULL;
113
    devcon->bb_buf = NULL;
113
    devcon->bb_off = 0;
114
    devcon->bb_off = 0;
114
    devcon->bb_size = 0;
115
    devcon->bb_size = 0;
115
    devcon->cache = NULL;
116
    devcon->cache = NULL;
116
 
117
 
117
    futex_down(&dcl_lock);
118
    futex_down(&dcl_lock);
118
    for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
119
    for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
119
        devcon_t *d = list_get_instance(cur, devcon_t, link);
120
        devcon_t *d = list_get_instance(cur, devcon_t, link);
120
        if (d->dev_handle == dev_handle) {
121
        if (d->dev_handle == dev_handle) {
121
            futex_up(&dcl_lock);
122
            futex_up(&dcl_lock);
122
            free(devcon);
123
            free(devcon);
123
            return EEXIST;
124
            return EEXIST;
124
        }
125
        }
125
    }
126
    }
126
    list_append(&devcon->link, &dcl_head);
127
    list_append(&devcon->link, &dcl_head);
127
    futex_up(&dcl_lock);
128
    futex_up(&dcl_lock);
128
    return EOK;
129
    return EOK;
129
}
130
}
130
 
131
 
131
static void devcon_remove(devcon_t *devcon)
132
static void devcon_remove(devcon_t *devcon)
132
{
133
{
133
    futex_down(&dcl_lock);
134
    futex_down(&dcl_lock);
134
    list_remove(&devcon->link);
135
    list_remove(&devcon->link);
135
    futex_up(&dcl_lock);
136
    futex_up(&dcl_lock);
136
}
137
}
137
 
138
 
138
int block_init(dev_handle_t dev_handle, size_t com_size)
139
int block_init(dev_handle_t dev_handle, size_t com_size)
139
{
140
{
140
    int rc;
141
    int rc;
141
    int dev_phone;
142
    int dev_phone;
142
    void *com_area;
143
    void *com_area;
143
   
144
   
144
    com_area = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE,
145
    com_area = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE,
145
        MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
146
        MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
146
    if (!com_area) {
147
    if (!com_area) {
147
        return ENOMEM;
148
        return ENOMEM;
148
    }
149
    }
149
 
150
 
150
    dev_phone = devmap_device_connect(dev_handle, IPC_FLAG_BLOCKING);
151
    dev_phone = devmap_device_connect(dev_handle, IPC_FLAG_BLOCKING);
151
    if (dev_phone < 0) {
152
    if (dev_phone < 0) {
152
        munmap(com_area, com_size);
153
        munmap(com_area, com_size);
153
        return dev_phone;
154
        return dev_phone;
154
    }
155
    }
155
 
156
 
156
    rc = ipc_share_out_start(dev_phone, com_area,
157
    rc = ipc_share_out_start(dev_phone, com_area,
157
        AS_AREA_READ | AS_AREA_WRITE);
158
        AS_AREA_READ | AS_AREA_WRITE);
158
    if (rc != EOK) {
159
    if (rc != EOK) {
159
            munmap(com_area, com_size);
160
            munmap(com_area, com_size);
160
        ipc_hangup(dev_phone);
161
        ipc_hangup(dev_phone);
161
        return rc;
162
        return rc;
162
    }
163
    }
163
   
164
   
164
    rc = devcon_add(dev_handle, dev_phone, com_area, com_size);
165
    rc = devcon_add(dev_handle, dev_phone, com_area, com_size);
165
    if (rc != EOK) {
166
    if (rc != EOK) {
166
        munmap(com_area, com_size);
167
        munmap(com_area, com_size);
167
        ipc_hangup(dev_phone);
168
        ipc_hangup(dev_phone);
168
        return rc;
169
        return rc;
169
    }
170
    }
170
 
171
 
171
    return EOK;
172
    return EOK;
172
}
173
}
173
 
174
 
174
void block_fini(dev_handle_t dev_handle)
175
void block_fini(dev_handle_t dev_handle)
175
{
176
{
176
    devcon_t *devcon = devcon_search(dev_handle);
177
    devcon_t *devcon = devcon_search(dev_handle);
177
    assert(devcon);
178
    assert(devcon);
178
   
179
   
179
    devcon_remove(devcon);
180
    devcon_remove(devcon);
180
 
181
 
181
    if (devcon->bb_buf)
182
    if (devcon->bb_buf)
182
        free(devcon->bb_buf);
183
        free(devcon->bb_buf);
183
 
184
 
184
    if (devcon->cache) {
185
    if (devcon->cache) {
185
        hash_table_destroy(&devcon->cache->block_hash);
186
        hash_table_destroy(&devcon->cache->block_hash);
186
        free(devcon->cache);
187
        free(devcon->cache);
187
    }
188
    }
188
 
189
 
189
    munmap(devcon->com_area, devcon->com_size);
190
    munmap(devcon->com_area, devcon->com_size);
190
    ipc_hangup(devcon->dev_phone);
191
    ipc_hangup(devcon->dev_phone);
191
 
192
 
192
    free(devcon);  
193
    free(devcon);  
193
}
194
}
194
 
195
 
195
int block_bb_read(dev_handle_t dev_handle, off_t off, size_t size)
196
int block_bb_read(dev_handle_t dev_handle, off_t off, size_t size)
196
{
197
{
197
    void *bb_buf;
198
    void *bb_buf;
198
    int rc;
199
    int rc;
199
 
200
 
200
    devcon_t *devcon = devcon_search(dev_handle);
201
    devcon_t *devcon = devcon_search(dev_handle);
201
    if (!devcon)
202
    if (!devcon)
202
        return ENOENT;
203
        return ENOENT;
203
    if (devcon->bb_buf)
204
    if (devcon->bb_buf)
204
        return EEXIST;
205
        return EEXIST;
205
    bb_buf = malloc(size);
206
    bb_buf = malloc(size);
206
    if (!bb_buf)
207
    if (!bb_buf)
207
        return ENOMEM;
208
        return ENOMEM;
208
   
209
   
209
    off_t bufpos = 0;
210
    off_t bufpos = 0;
210
    size_t buflen = 0;
211
    size_t buflen = 0;
211
    rc = block_read(dev_handle, &bufpos, &buflen, &off,
212
    rc = block_read(dev_handle, &bufpos, &buflen, &off,
212
        bb_buf, size, size);
213
        bb_buf, size, size);
213
    if (rc != EOK) {
214
    if (rc != EOK) {
214
            free(bb_buf);
215
            free(bb_buf);
215
        return rc;
216
        return rc;
216
    }
217
    }
217
    devcon->bb_buf = bb_buf;
218
    devcon->bb_buf = bb_buf;
218
    devcon->bb_off = off;
219
    devcon->bb_off = off;
219
    devcon->bb_size = size;
220
    devcon->bb_size = size;
220
 
221
 
221
    return EOK;
222
    return EOK;
222
}
223
}
223
 
224
 
224
void *block_bb_get(dev_handle_t dev_handle)
225
void *block_bb_get(dev_handle_t dev_handle)
225
{
226
{
226
    devcon_t *devcon = devcon_search(dev_handle);
227
    devcon_t *devcon = devcon_search(dev_handle);
227
    assert(devcon);
228
    assert(devcon);
228
    return devcon->bb_buf;
229
    return devcon->bb_buf;
229
}
230
}
230
 
231
 
231
static hash_index_t cache_hash(unsigned long *key)
232
static hash_index_t cache_hash(unsigned long *key)
232
{
233
{
233
    return *key & (CACHE_BUCKETS - 1);
234
    return *key & (CACHE_BUCKETS - 1);
234
}
235
}
235
 
236
 
236
static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item)
237
static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item)
237
{
238
{
238
    block_t *b = hash_table_get_instance(item, block_t, hash_link);
239
    block_t *b = hash_table_get_instance(item, block_t, hash_link);
239
    return b->boff == *key;
240
    return b->boff == *key;
240
}
241
}
241
 
242
 
242
static void cache_remove_callback(link_t *item)
243
static void cache_remove_callback(link_t *item)
243
{
244
{
244
}
245
}
245
 
246
 
246
static hash_table_operations_t cache_ops = {
247
static hash_table_operations_t cache_ops = {
247
    .hash = cache_hash,
248
    .hash = cache_hash,
248
    .compare = cache_compare,
249
    .compare = cache_compare,
249
    .remove_callback = cache_remove_callback
250
    .remove_callback = cache_remove_callback
250
};
251
};
251
 
252
 
252
int block_cache_init(dev_handle_t dev_handle, size_t size, unsigned blocks)
253
int block_cache_init(dev_handle_t dev_handle, size_t size, unsigned blocks)
253
{
254
{
254
    devcon_t *devcon = devcon_search(dev_handle);
255
    devcon_t *devcon = devcon_search(dev_handle);
255
    cache_t *cache;
256
    cache_t *cache;
256
    if (!devcon)
257
    if (!devcon)
257
        return ENOENT;
258
        return ENOENT;
258
    if (devcon->cache)
259
    if (devcon->cache)
259
        return EEXIST;
260
        return EEXIST;
260
    cache = malloc(sizeof(cache_t));
261
    cache = malloc(sizeof(cache_t));
261
    if (!cache)
262
    if (!cache)
262
        return ENOMEM;
263
        return ENOMEM;
263
   
264
   
264
    futex_initialize(&cache->lock, 1);
265
    futex_initialize(&cache->lock, 1);
265
    list_initialize(&cache->free_head);
266
    list_initialize(&cache->free_head);
266
    cache->block_size = size;
267
    cache->block_size = size;
267
    cache->block_count = blocks;
268
    cache->block_count = blocks;
268
 
269
 
269
    if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1,
270
    if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1,
270
        &cache_ops)) {
271
        &cache_ops)) {
271
        free(cache);
272
        free(cache);
272
        return ENOMEM;
273
        return ENOMEM;
273
    }
274
    }
274
 
275
 
275
    devcon->cache = cache;
276
    devcon->cache = cache;
276
    return EOK;
277
    return EOK;
277
}
278
}
278
 
279
 
279
static bool cache_can_grow(cache_t *cache)
280
static bool cache_can_grow(cache_t *cache)
280
{
281
{
281
    return true;
282
    return true;
282
}
283
}
283
 
284
 
284
static void block_initialize(block_t *b)
285
static void block_initialize(block_t *b)
285
{
286
{
286
    futex_initialize(&b->lock, 1);
287
    futex_initialize(&b->lock, 1);
287
    b->refcnt = 1;
288
    b->refcnt = 1;
288
    b->dirty = false;
289
    b->dirty = false;
289
    rwlock_initialize(&b->contents_lock);
290
    rwlock_initialize(&b->contents_lock);
290
    link_initialize(&b->free_link);
291
    link_initialize(&b->free_link);
291
    link_initialize(&b->hash_link);
292
    link_initialize(&b->hash_link);
292
}
293
}
293
 
294
 
294
/** Instantiate a block in memory and get a reference to it.
295
/** Instantiate a block in memory and get a reference to it.
295
 *
296
 *
296
 * @param dev_handle        Device handle of the block device.
297
 * @param dev_handle        Device handle of the block device.
297
 * @param boff          Block offset.
298
 * @param boff          Block offset.
298
 * @param flags         If BLOCK_FLAGS_NOREAD is specified, block_get()
299
 * @param flags         If BLOCK_FLAGS_NOREAD is specified, block_get()
299
 *              will not read the contents of the block from the
300
 *              will not read the contents of the block from the
300
 *              device.
301
 *              device.
301
 *
302
 *
302
 * @return          Block structure.
303
 * @return          Block structure.
303
 */
304
 */
304
block_t *block_get(dev_handle_t dev_handle, bn_t boff, int flags)
305
block_t *block_get(dev_handle_t dev_handle, bn_t boff, int flags)
305
{
306
{
306
    devcon_t *devcon;
307
    devcon_t *devcon;
307
    cache_t *cache;
308
    cache_t *cache;
308
    block_t *b;
309
    block_t *b;
309
    link_t *l;
310
    link_t *l;
310
    unsigned long key = boff;
311
    unsigned long key = boff;
311
   
312
   
312
    devcon = devcon_search(dev_handle);
313
    devcon = devcon_search(dev_handle);
313
 
314
 
314
    assert(devcon);
315
    assert(devcon);
315
    assert(devcon->cache);
316
    assert(devcon->cache);
316
   
317
   
317
    cache = devcon->cache;
318
    cache = devcon->cache;
318
    futex_down(&cache->lock);
319
    futex_down(&cache->lock);
319
    l = hash_table_find(&cache->block_hash, &key);
320
    l = hash_table_find(&cache->block_hash, &key);
320
    if (l) {
321
    if (l) {
321
        /*
322
        /*
322
         * We found the block in the cache.
323
         * We found the block in the cache.
323
         */
324
         */
324
        b = hash_table_get_instance(l, block_t, hash_link);
325
        b = hash_table_get_instance(l, block_t, hash_link);
325
        futex_down(&b->lock);
326
        futex_down(&b->lock);
326
        if (b->refcnt++ == 0)
327
        if (b->refcnt++ == 0)
327
            list_remove(&b->free_link);
328
            list_remove(&b->free_link);
328
        futex_up(&b->lock);
329
        futex_up(&b->lock);
329
        futex_up(&cache->lock);
330
        futex_up(&cache->lock);
330
    } else {
331
    } else {
331
        /*
332
        /*
332
         * The block was not found in the cache.
333
         * The block was not found in the cache.
333
         */
334
         */
334
        int rc;
335
        int rc;
335
        off_t bufpos = 0;
336
        off_t bufpos = 0;
336
        size_t buflen = 0;
337
        size_t buflen = 0;
337
        off_t pos = boff * cache->block_size;
338
        off_t pos = boff * cache->block_size;
338
        bool sync = false;
339
        bool sync = false;
339
 
340
 
340
        if (cache_can_grow(cache)) {
341
        if (cache_can_grow(cache)) {
341
            /*
342
            /*
342
             * We can grow the cache by allocating new blocks.
343
             * We can grow the cache by allocating new blocks.
343
             * Should the allocation fail, we fail over and try to
344
             * Should the allocation fail, we fail over and try to
344
             * recycle a block from the cache.
345
             * recycle a block from the cache.
345
             */
346
             */
346
            b = malloc(sizeof(block_t));
347
            b = malloc(sizeof(block_t));
347
            if (!b)
348
            if (!b)
348
                goto recycle;
349
                goto recycle;
349
            b->data = malloc(cache->block_size);
350
            b->data = malloc(cache->block_size);
350
            if (!b->data) {
351
            if (!b->data) {
351
                free(b);
352
                free(b);
352
                goto recycle;
353
                goto recycle;
353
            }
354
            }
354
        } else {
355
        } else {
355
            /*
356
            /*
356
             * Try to recycle a block from the free list.
357
             * Try to recycle a block from the free list.
357
             */
358
             */
358
            unsigned long temp_key;
359
            unsigned long temp_key;
359
recycle:
360
recycle:
360
            assert(!list_empty(&cache->free_head));
361
            assert(!list_empty(&cache->free_head));
361
            l = cache->free_head.next;
362
            l = cache->free_head.next;
362
            list_remove(l);
363
            list_remove(l);
363
            b = hash_table_get_instance(l, block_t, hash_link);
364
            b = hash_table_get_instance(l, block_t, hash_link);
364
            sync = b->dirty;
365
            sync = b->dirty;
365
            temp_key = b->boff;
366
            temp_key = b->boff;
366
            hash_table_remove(&cache->block_hash, &temp_key, 1);
367
            hash_table_remove(&cache->block_hash, &temp_key, 1);
367
        }
368
        }
368
 
369
 
369
        block_initialize(b);
370
        block_initialize(b);
370
        b->dev_handle = dev_handle;
371
        b->dev_handle = dev_handle;
371
        b->size = cache->block_size;
372
        b->size = cache->block_size;
372
        b->boff = boff;
373
        b->boff = boff;
373
        hash_table_insert(&cache->block_hash, &key, &b->hash_link);
374
        hash_table_insert(&cache->block_hash, &key, &b->hash_link);
374
 
375
 
375
        /*
376
        /*
376
         * Lock the block before releasing the cache lock. Thus we don't
377
         * Lock the block before releasing the cache lock. Thus we don't
377
         * kill concurent operations on the cache while doing I/O on the
378
         * kill concurent operations on the cache while doing I/O on the
378
         * block.
379
         * block.
379
         */
380
         */
380
        futex_down(&b->lock);
381
        futex_down(&b->lock);
381
        futex_up(&cache->lock);
382
        futex_up(&cache->lock);
382
 
383
 
383
        if (sync) {
384
        if (sync) {
384
            /*
385
            /*
385
             * The block is dirty and needs to be written back to
386
             * The block is dirty and needs to be written back to
386
             * the device before we can read in the new contents.
387
             * the device before we can read in the new contents.
387
             */
388
             */
388
            abort();    /* TODO: block_write() */
389
            abort();    /* TODO: block_write() */
389
        }
390
        }
390
        if (!(flags & BLOCK_FLAGS_NOREAD)) {
391
        if (!(flags & BLOCK_FLAGS_NOREAD)) {
391
            /*
392
            /*
392
             * The block contains old or no data. We need to read
393
             * The block contains old or no data. We need to read
393
             * the new contents from the device.
394
             * the new contents from the device.
394
             */
395
             */
395
            rc = block_read(dev_handle, &bufpos, &buflen, &pos,
396
            rc = block_read(dev_handle, &bufpos, &buflen, &pos,
396
                b->data, cache->block_size, cache->block_size);
397
                b->data, cache->block_size, cache->block_size);
397
            assert(rc == EOK);
398
            assert(rc == EOK);
398
        }
399
        }
399
 
400
 
400
        futex_up(&b->lock);
401
        futex_up(&b->lock);
401
    }
402
    }
402
    return b;
403
    return b;
403
}
404
}
404
 
405
 
405
/** Release a reference to a block.
406
/** Release a reference to a block.
406
 *
407
 *
407
 * If the last reference is dropped, the block is put on the free list.
408
 * If the last reference is dropped, the block is put on the free list.
408
 *
409
 *
409
 * @param block     Block of which a reference is to be released.
410
 * @param block     Block of which a reference is to be released.
410
 */
411
 */
411
void block_put(block_t *block)
412
void block_put(block_t *block)
412
{
413
{
413
    devcon_t *devcon = devcon_search(block->dev_handle);
414
    devcon_t *devcon = devcon_search(block->dev_handle);
414
    cache_t *cache;
415
    cache_t *cache;
415
 
416
 
416
    assert(devcon);
417
    assert(devcon);
417
    assert(devcon->cache);
418
    assert(devcon->cache);
418
 
419
 
419
    cache = devcon->cache;
420
    cache = devcon->cache;
420
    futex_down(&cache->lock);
421
    futex_down(&cache->lock);
421
    futex_down(&block->lock);
422
    futex_down(&block->lock);
422
    if (!--block->refcnt) {
423
    if (!--block->refcnt) {
423
        /*
424
        /*
424
         * Last reference to the block was dropped, put the block on the
425
         * Last reference to the block was dropped, put the block on the
425
         * free list.
426
         * free list.
426
         */
427
         */
427
        list_append(&block->free_link, &cache->free_head);
428
        list_append(&block->free_link, &cache->free_head);
428
    }
429
    }
429
    futex_up(&block->lock);
430
    futex_up(&block->lock);
430
    futex_up(&cache->lock);
431
    futex_up(&cache->lock);
431
}
432
}
432
 
433
 
433
/** Read data from a block device.
434
/** Read data from a block device.
434
 *
435
 *
435
 * @param dev_handle    Device handle of the block device.
436
 * @param dev_handle    Device handle of the block device.
436
 * @param bufpos    Pointer to the first unread valid offset within the
437
 * @param bufpos    Pointer to the first unread valid offset within the
437
 *          communication buffer.
438
 *          communication buffer.
438
 * @param buflen    Pointer to the number of unread bytes that are ready in
439
 * @param buflen    Pointer to the number of unread bytes that are ready in
439
 *          the communication buffer.
440
 *          the communication buffer.
440
 * @param pos       Device position to be read.
441
 * @param pos       Device position to be read.
441
 * @param dst       Destination buffer.
442
 * @param dst       Destination buffer.
442
 * @param size      Size of the destination buffer.
443
 * @param size      Size of the destination buffer.
443
 * @param block_size    Block size to be used for the transfer.
444
 * @param block_size    Block size to be used for the transfer.
444
 *
445
 *
445
 * @return      EOK on success or a negative return code on failure.
446
 * @return      EOK on success or a negative return code on failure.
446
 */
447
 */
447
int
448
int
448
block_read(dev_handle_t dev_handle, off_t *bufpos, size_t *buflen, off_t *pos,
449
block_read(dev_handle_t dev_handle, off_t *bufpos, size_t *buflen, off_t *pos,
449
    void *dst, size_t size, size_t block_size)
450
    void *dst, size_t size, size_t block_size)
450
{
451
{
451
    off_t offset = 0;
452
    off_t offset = 0;
452
    size_t left = size;
453
    size_t left = size;
453
    devcon_t *devcon = devcon_search(dev_handle);
454
    devcon_t *devcon = devcon_search(dev_handle);
454
    assert(devcon);
455
    assert(devcon);
455
   
456
   
456
    while (left > 0) {
457
    while (left > 0) {
457
        size_t rd;
458
        size_t rd;
458
       
459
       
459
        if (*bufpos + left < *buflen)
460
        if (*bufpos + left < *buflen)
460
            rd = left;
461
            rd = left;
461
        else
462
        else
462
            rd = *buflen - *bufpos;
463
            rd = *buflen - *bufpos;
463
       
464
       
464
        if (rd > 0) {
465
        if (rd > 0) {
465
            /*
466
            /*
466
             * Copy the contents of the communication buffer to the
467
             * Copy the contents of the communication buffer to the
467
             * destination buffer.
468
             * destination buffer.
468
             */
469
             */
469
            memcpy(dst + offset, devcon->com_area + *bufpos, rd);
470
            memcpy(dst + offset, devcon->com_area + *bufpos, rd);
470
            offset += rd;
471
            offset += rd;
471
            *bufpos += rd;
472
            *bufpos += rd;
472
            *pos += rd;
473
            *pos += rd;
473
            left -= rd;
474
            left -= rd;
474
        }
475
        }
475
       
476
       
476
        if (*bufpos == *buflen) {
477
        if (*bufpos == *buflen) {
477
            /* Refill the communication buffer with a new block. */
478
            /* Refill the communication buffer with a new block. */
478
            ipcarg_t retval;
479
            ipcarg_t retval;
479
            int rc = async_req_2_1(devcon->dev_phone, BD_READ_BLOCK,
480
            int rc = async_req_2_1(devcon->dev_phone, BD_READ_BLOCK,
480
                *pos / block_size, block_size, &retval);
481
                *pos / block_size, block_size, &retval);
481
            if ((rc != EOK) || (retval != EOK))
482
            if ((rc != EOK) || (retval != EOK))
482
                return (rc != EOK ? rc : retval);
483
                return (rc != EOK ? rc : retval);
483
           
484
           
484
            *bufpos = 0;
485
            *bufpos = 0;
485
            *buflen = block_size;
486
            *buflen = block_size;
486
        }
487
        }
487
    }
488
    }
488
   
489
   
489
    return EOK;
490
    return EOK;
490
}
491
}
491
 
492
 
492
/** @}
493
/** @}
493
 */
494
 */
494
 
495