Subversion Repositories HelenOS

Rev

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

Rev 4572 Rev 4583
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 <fibril_sync.h>
49
#include <fibril_sync.h>
50
#include <adt/list.h>
50
#include <adt/list.h>
51
#include <adt/hash_table.h>
51
#include <adt/hash_table.h>
52
#include <mem.h>
52
#include <mem.h>
53
 
53
 
54
/** Lock protecting the device connection list */
54
/** Lock protecting the device connection list */
55
static FIBRIL_MUTEX_INITIALIZE(dcl_lock);
55
static FIBRIL_MUTEX_INITIALIZE(dcl_lock);
56
/** Device connection list head. */
56
/** Device connection list head. */
57
static LIST_INITIALIZE(dcl_head);
57
static LIST_INITIALIZE(dcl_head);
58
 
58
 
59
#define CACHE_BUCKETS_LOG2      10
59
#define CACHE_BUCKETS_LOG2      10
60
#define CACHE_BUCKETS           (1 << CACHE_BUCKETS_LOG2)
60
#define CACHE_BUCKETS           (1 << CACHE_BUCKETS_LOG2)
61
 
61
 
62
typedef struct {
62
typedef struct {
63
    fibril_mutex_t lock;
63
    fibril_mutex_t lock;
64
    size_t block_size;      /**< Block size. */
64
    size_t block_size;      /**< Block size. */
65
    unsigned block_count;       /**< Total number of blocks. */
65
    unsigned block_count;       /**< Total number of blocks. */
66
    hash_table_t block_hash;
66
    hash_table_t block_hash;
67
    link_t free_head;
67
    link_t free_head;
68
    enum cache_mode mode;
68
    enum cache_mode mode;
69
} cache_t;
69
} cache_t;
70
 
70
 
71
typedef struct {
71
typedef struct {
72
    link_t link;
72
    link_t link;
73
    dev_handle_t dev_handle;
73
    dev_handle_t dev_handle;
74
    int dev_phone;
74
    int dev_phone;
75
    void *com_area;
75
    void *com_area;
76
    size_t com_size;
76
    size_t com_size;
77
    void *bb_buf;
77
    void *bb_buf;
78
    off_t bb_off;
78
    off_t bb_off;
79
    size_t bb_size;
79
    size_t bb_size;
80
    cache_t *cache;
80
    cache_t *cache;
81
} devcon_t;
81
} devcon_t;
82
 
82
 
83
static int write_block(devcon_t *devcon, bn_t boff, size_t block_size,
83
static int read_block(devcon_t *devcon, bn_t boff, size_t block_size);
84
    const void *src);
84
static int write_block(devcon_t *devcon, bn_t boff, size_t block_size);
85
 
85
 
86
static devcon_t *devcon_search(dev_handle_t dev_handle)
86
static devcon_t *devcon_search(dev_handle_t dev_handle)
87
{
87
{
88
    link_t *cur;
88
    link_t *cur;
89
 
89
 
90
    fibril_mutex_lock(&dcl_lock);
90
    fibril_mutex_lock(&dcl_lock);
91
    for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
91
    for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
92
        devcon_t *devcon = list_get_instance(cur, devcon_t, link);
92
        devcon_t *devcon = list_get_instance(cur, devcon_t, link);
93
        if (devcon->dev_handle == dev_handle) {
93
        if (devcon->dev_handle == dev_handle) {
94
            fibril_mutex_unlock(&dcl_lock);
94
            fibril_mutex_unlock(&dcl_lock);
95
            return devcon;
95
            return devcon;
96
        }
96
        }
97
    }
97
    }
98
    fibril_mutex_unlock(&dcl_lock);
98
    fibril_mutex_unlock(&dcl_lock);
99
    return NULL;
99
    return NULL;
100
}
100
}
101
 
101
 
102
static int devcon_add(dev_handle_t dev_handle, int dev_phone, void *com_area,
102
static int devcon_add(dev_handle_t dev_handle, int dev_phone, void *com_area,
103
   size_t com_size)
103
   size_t com_size)
104
{
104
{
105
    link_t *cur;
105
    link_t *cur;
106
    devcon_t *devcon;
106
    devcon_t *devcon;
107
 
107
 
108
    devcon = malloc(sizeof(devcon_t));
108
    devcon = malloc(sizeof(devcon_t));
109
    if (!devcon)
109
    if (!devcon)
110
        return ENOMEM;
110
        return ENOMEM;
111
   
111
   
112
    link_initialize(&devcon->link);
112
    link_initialize(&devcon->link);
113
    devcon->dev_handle = dev_handle;
113
    devcon->dev_handle = dev_handle;
114
    devcon->dev_phone = dev_phone;
114
    devcon->dev_phone = dev_phone;
115
    devcon->com_area = com_area;
115
    devcon->com_area = com_area;
116
    devcon->com_size = com_size;
116
    devcon->com_size = com_size;
117
    devcon->bb_buf = NULL;
117
    devcon->bb_buf = NULL;
118
    devcon->bb_off = 0;
118
    devcon->bb_off = 0;
119
    devcon->bb_size = 0;
119
    devcon->bb_size = 0;
120
    devcon->cache = NULL;
120
    devcon->cache = NULL;
121
 
121
 
122
    fibril_mutex_lock(&dcl_lock);
122
    fibril_mutex_lock(&dcl_lock);
123
    for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
123
    for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
124
        devcon_t *d = list_get_instance(cur, devcon_t, link);
124
        devcon_t *d = list_get_instance(cur, devcon_t, link);
125
        if (d->dev_handle == dev_handle) {
125
        if (d->dev_handle == dev_handle) {
126
            fibril_mutex_unlock(&dcl_lock);
126
            fibril_mutex_unlock(&dcl_lock);
127
            free(devcon);
127
            free(devcon);
128
            return EEXIST;
128
            return EEXIST;
129
        }
129
        }
130
    }
130
    }
131
    list_append(&devcon->link, &dcl_head);
131
    list_append(&devcon->link, &dcl_head);
132
    fibril_mutex_unlock(&dcl_lock);
132
    fibril_mutex_unlock(&dcl_lock);
133
    return EOK;
133
    return EOK;
134
}
134
}
135
 
135
 
136
static void devcon_remove(devcon_t *devcon)
136
static void devcon_remove(devcon_t *devcon)
137
{
137
{
138
    fibril_mutex_lock(&dcl_lock);
138
    fibril_mutex_lock(&dcl_lock);
139
    list_remove(&devcon->link);
139
    list_remove(&devcon->link);
140
    fibril_mutex_unlock(&dcl_lock);
140
    fibril_mutex_unlock(&dcl_lock);
141
}
141
}
142
 
142
 
143
int block_init(dev_handle_t dev_handle, size_t com_size)
143
int block_init(dev_handle_t dev_handle, size_t com_size)
144
{
144
{
145
    int rc;
145
    int rc;
146
    int dev_phone;
146
    int dev_phone;
147
    void *com_area;
147
    void *com_area;
148
   
148
   
149
    com_area = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE,
149
    com_area = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE,
150
        MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
150
        MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
151
    if (!com_area) {
151
    if (!com_area) {
152
        return ENOMEM;
152
        return ENOMEM;
153
    }
153
    }
154
 
154
 
155
    dev_phone = devmap_device_connect(dev_handle, IPC_FLAG_BLOCKING);
155
    dev_phone = devmap_device_connect(dev_handle, IPC_FLAG_BLOCKING);
156
    if (dev_phone < 0) {
156
    if (dev_phone < 0) {
157
        munmap(com_area, com_size);
157
        munmap(com_area, com_size);
158
        return dev_phone;
158
        return dev_phone;
159
    }
159
    }
160
 
160
 
161
    rc = ipc_share_out_start(dev_phone, com_area,
161
    rc = ipc_share_out_start(dev_phone, com_area,
162
        AS_AREA_READ | AS_AREA_WRITE);
162
        AS_AREA_READ | AS_AREA_WRITE);
163
    if (rc != EOK) {
163
    if (rc != EOK) {
164
            munmap(com_area, com_size);
164
            munmap(com_area, com_size);
165
        ipc_hangup(dev_phone);
165
        ipc_hangup(dev_phone);
166
        return rc;
166
        return rc;
167
    }
167
    }
168
   
168
   
169
    rc = devcon_add(dev_handle, dev_phone, com_area, com_size);
169
    rc = devcon_add(dev_handle, dev_phone, com_area, com_size);
170
    if (rc != EOK) {
170
    if (rc != EOK) {
171
        munmap(com_area, com_size);
171
        munmap(com_area, com_size);
172
        ipc_hangup(dev_phone);
172
        ipc_hangup(dev_phone);
173
        return rc;
173
        return rc;
174
    }
174
    }
175
 
175
 
176
    return EOK;
176
    return EOK;
177
}
177
}
178
 
178
 
179
void block_fini(dev_handle_t dev_handle)
179
void block_fini(dev_handle_t dev_handle)
180
{
180
{
181
    devcon_t *devcon = devcon_search(dev_handle);
181
    devcon_t *devcon = devcon_search(dev_handle);
182
    assert(devcon);
182
    assert(devcon);
183
   
183
   
184
    devcon_remove(devcon);
184
    devcon_remove(devcon);
185
 
185
 
186
    if (devcon->bb_buf)
186
    if (devcon->bb_buf)
187
        free(devcon->bb_buf);
187
        free(devcon->bb_buf);
188
 
188
 
189
    if (devcon->cache) {
189
    if (devcon->cache) {
190
        hash_table_destroy(&devcon->cache->block_hash);
190
        hash_table_destroy(&devcon->cache->block_hash);
191
        free(devcon->cache);
191
        free(devcon->cache);
192
    }
192
    }
193
 
193
 
194
    munmap(devcon->com_area, devcon->com_size);
194
    munmap(devcon->com_area, devcon->com_size);
195
    ipc_hangup(devcon->dev_phone);
195
    ipc_hangup(devcon->dev_phone);
196
 
196
 
197
    free(devcon);  
197
    free(devcon);  
198
}
198
}
199
 
199
 
200
int block_bb_read(dev_handle_t dev_handle, off_t off, size_t size)
200
int block_bb_read(dev_handle_t dev_handle, off_t off, size_t size)
201
{
201
{
202
    void *bb_buf;
202
    void *bb_buf;
203
    int rc;
203
    int rc;
204
 
204
 
205
    devcon_t *devcon = devcon_search(dev_handle);
205
    devcon_t *devcon = devcon_search(dev_handle);
206
    if (!devcon)
206
    if (!devcon)
207
        return ENOENT;
207
        return ENOENT;
208
    if (devcon->bb_buf)
208
    if (devcon->bb_buf)
209
        return EEXIST;
209
        return EEXIST;
210
    bb_buf = malloc(size);
210
    bb_buf = malloc(size);
211
    if (!bb_buf)
211
    if (!bb_buf)
212
        return ENOMEM;
212
        return ENOMEM;
213
   
213
   
214
    off_t bufpos = 0;
-
 
215
    size_t buflen = 0;
-
 
216
    rc = block_read(dev_handle, &bufpos, &buflen, &off,
-
 
217
        bb_buf, size, size);
214
    rc = read_block(devcon, 0, size);
218
    if (rc != EOK) {
215
    if (rc != EOK) {
219
            free(bb_buf);
216
            free(bb_buf);
220
        return rc;
217
        return rc;
221
    }
218
    }
-
 
219
 
-
 
220
    memcpy(bb_buf, devcon->com_area, size);
-
 
221
 
222
    devcon->bb_buf = bb_buf;
222
    devcon->bb_buf = bb_buf;
223
    devcon->bb_off = off;
223
    devcon->bb_off = off;
224
    devcon->bb_size = size;
224
    devcon->bb_size = size;
225
 
225
 
226
    return EOK;
226
    return EOK;
227
}
227
}
228
 
228
 
229
void *block_bb_get(dev_handle_t dev_handle)
229
void *block_bb_get(dev_handle_t dev_handle)
230
{
230
{
231
    devcon_t *devcon = devcon_search(dev_handle);
231
    devcon_t *devcon = devcon_search(dev_handle);
232
    assert(devcon);
232
    assert(devcon);
233
    return devcon->bb_buf;
233
    return devcon->bb_buf;
234
}
234
}
235
 
235
 
236
static hash_index_t cache_hash(unsigned long *key)
236
static hash_index_t cache_hash(unsigned long *key)
237
{
237
{
238
    return *key & (CACHE_BUCKETS - 1);
238
    return *key & (CACHE_BUCKETS - 1);
239
}
239
}
240
 
240
 
241
static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item)
241
static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item)
242
{
242
{
243
    block_t *b = hash_table_get_instance(item, block_t, hash_link);
243
    block_t *b = hash_table_get_instance(item, block_t, hash_link);
244
    return b->boff == *key;
244
    return b->boff == *key;
245
}
245
}
246
 
246
 
247
static void cache_remove_callback(link_t *item)
247
static void cache_remove_callback(link_t *item)
248
{
248
{
249
}
249
}
250
 
250
 
251
static hash_table_operations_t cache_ops = {
251
static hash_table_operations_t cache_ops = {
252
    .hash = cache_hash,
252
    .hash = cache_hash,
253
    .compare = cache_compare,
253
    .compare = cache_compare,
254
    .remove_callback = cache_remove_callback
254
    .remove_callback = cache_remove_callback
255
};
255
};
256
 
256
 
257
int block_cache_init(dev_handle_t dev_handle, size_t size, unsigned blocks,
257
int block_cache_init(dev_handle_t dev_handle, size_t size, unsigned blocks,
258
    enum cache_mode mode)
258
    enum cache_mode mode)
259
{
259
{
260
    devcon_t *devcon = devcon_search(dev_handle);
260
    devcon_t *devcon = devcon_search(dev_handle);
261
    cache_t *cache;
261
    cache_t *cache;
262
    if (!devcon)
262
    if (!devcon)
263
        return ENOENT;
263
        return ENOENT;
264
    if (devcon->cache)
264
    if (devcon->cache)
265
        return EEXIST;
265
        return EEXIST;
266
    cache = malloc(sizeof(cache_t));
266
    cache = malloc(sizeof(cache_t));
267
    if (!cache)
267
    if (!cache)
268
        return ENOMEM;
268
        return ENOMEM;
269
   
269
   
270
    fibril_mutex_initialize(&cache->lock);
270
    fibril_mutex_initialize(&cache->lock);
271
    list_initialize(&cache->free_head);
271
    list_initialize(&cache->free_head);
272
    cache->block_size = size;
272
    cache->block_size = size;
273
    cache->block_count = blocks;
273
    cache->block_count = blocks;
274
    cache->mode = mode;
274
    cache->mode = mode;
275
 
275
 
276
    if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1,
276
    if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1,
277
        &cache_ops)) {
277
        &cache_ops)) {
278
        free(cache);
278
        free(cache);
279
        return ENOMEM;
279
        return ENOMEM;
280
    }
280
    }
281
 
281
 
282
    devcon->cache = cache;
282
    devcon->cache = cache;
283
    return EOK;
283
    return EOK;
284
}
284
}
285
 
285
 
286
static bool cache_can_grow(cache_t *cache)
286
static bool cache_can_grow(cache_t *cache)
287
{
287
{
288
    return true;
288
    return true;
289
}
289
}
290
 
290
 
291
static void block_initialize(block_t *b)
291
static void block_initialize(block_t *b)
292
{
292
{
293
    fibril_mutex_initialize(&b->lock);
293
    fibril_mutex_initialize(&b->lock);
294
    b->refcnt = 1;
294
    b->refcnt = 1;
295
    b->dirty = false;
295
    b->dirty = false;
296
    fibril_rwlock_initialize(&b->contents_lock);
296
    fibril_rwlock_initialize(&b->contents_lock);
297
    link_initialize(&b->free_link);
297
    link_initialize(&b->free_link);
298
    link_initialize(&b->hash_link);
298
    link_initialize(&b->hash_link);
299
}
299
}
300
 
300
 
301
/** Instantiate a block in memory and get a reference to it.
301
/** Instantiate a block in memory and get a reference to it.
302
 *
302
 *
303
 * @param dev_handle        Device handle of the block device.
303
 * @param dev_handle        Device handle of the block device.
304
 * @param boff          Block offset.
304
 * @param boff          Block offset.
305
 * @param flags         If BLOCK_FLAGS_NOREAD is specified, block_get()
305
 * @param flags         If BLOCK_FLAGS_NOREAD is specified, block_get()
306
 *              will not read the contents of the block from the
306
 *              will not read the contents of the block from the
307
 *              device.
307
 *              device.
308
 *
308
 *
309
 * @return          Block structure.
309
 * @return          Block structure.
310
 */
310
 */
311
block_t *block_get(dev_handle_t dev_handle, bn_t boff, int flags)
311
block_t *block_get(dev_handle_t dev_handle, bn_t boff, int flags)
312
{
312
{
313
    devcon_t *devcon;
313
    devcon_t *devcon;
314
    cache_t *cache;
314
    cache_t *cache;
315
    block_t *b;
315
    block_t *b;
316
    link_t *l;
316
    link_t *l;
317
    unsigned long key = boff;
317
    unsigned long key = boff;
318
   
318
   
319
    devcon = devcon_search(dev_handle);
319
    devcon = devcon_search(dev_handle);
320
 
320
 
321
    assert(devcon);
321
    assert(devcon);
322
    assert(devcon->cache);
322
    assert(devcon->cache);
323
   
323
   
324
    cache = devcon->cache;
324
    cache = devcon->cache;
325
    fibril_mutex_lock(&cache->lock);
325
    fibril_mutex_lock(&cache->lock);
326
    l = hash_table_find(&cache->block_hash, &key);
326
    l = hash_table_find(&cache->block_hash, &key);
327
    if (l) {
327
    if (l) {
328
        /*
328
        /*
329
         * We found the block in the cache.
329
         * We found the block in the cache.
330
         */
330
         */
331
        b = hash_table_get_instance(l, block_t, hash_link);
331
        b = hash_table_get_instance(l, block_t, hash_link);
332
        fibril_mutex_lock(&b->lock);
332
        fibril_mutex_lock(&b->lock);
333
        if (b->refcnt++ == 0)
333
        if (b->refcnt++ == 0)
334
            list_remove(&b->free_link);
334
            list_remove(&b->free_link);
335
        fibril_mutex_unlock(&b->lock);
335
        fibril_mutex_unlock(&b->lock);
336
        fibril_mutex_unlock(&cache->lock);
336
        fibril_mutex_unlock(&cache->lock);
337
    } else {
337
    } else {
338
        /*
338
        /*
339
         * The block was not found in the cache.
339
         * The block was not found in the cache.
340
         */
340
         */
341
        int rc;
341
        int rc;
342
        off_t bufpos = 0;
-
 
343
        size_t buflen = 0;
-
 
344
        off_t pos = boff * cache->block_size;
-
 
345
        bool sync = false;
342
        bool sync = false;
346
 
343
 
347
        if (cache_can_grow(cache)) {
344
        if (cache_can_grow(cache)) {
348
            /*
345
            /*
349
             * We can grow the cache by allocating new blocks.
346
             * We can grow the cache by allocating new blocks.
350
             * Should the allocation fail, we fail over and try to
347
             * Should the allocation fail, we fail over and try to
351
             * recycle a block from the cache.
348
             * recycle a block from the cache.
352
             */
349
             */
353
            b = malloc(sizeof(block_t));
350
            b = malloc(sizeof(block_t));
354
            if (!b)
351
            if (!b)
355
                goto recycle;
352
                goto recycle;
356
            b->data = malloc(cache->block_size);
353
            b->data = malloc(cache->block_size);
357
            if (!b->data) {
354
            if (!b->data) {
358
                free(b);
355
                free(b);
359
                goto recycle;
356
                goto recycle;
360
            }
357
            }
361
        } else {
358
        } else {
362
            /*
359
            /*
363
             * Try to recycle a block from the free list.
360
             * Try to recycle a block from the free list.
364
             */
361
             */
365
            unsigned long temp_key;
362
            unsigned long temp_key;
366
recycle:
363
recycle:
367
            assert(!list_empty(&cache->free_head));
364
            assert(!list_empty(&cache->free_head));
368
            l = cache->free_head.next;
365
            l = cache->free_head.next;
369
            list_remove(l);
366
            list_remove(l);
370
            b = hash_table_get_instance(l, block_t, hash_link);
367
            b = hash_table_get_instance(l, block_t, hash_link);
371
            sync = b->dirty;
368
            sync = b->dirty;
372
            temp_key = b->boff;
369
            temp_key = b->boff;
373
            hash_table_remove(&cache->block_hash, &temp_key, 1);
370
            hash_table_remove(&cache->block_hash, &temp_key, 1);
374
        }
371
        }
375
 
372
 
376
        block_initialize(b);
373
        block_initialize(b);
377
        b->dev_handle = dev_handle;
374
        b->dev_handle = dev_handle;
378
        b->size = cache->block_size;
375
        b->size = cache->block_size;
379
        b->boff = boff;
376
        b->boff = boff;
380
        hash_table_insert(&cache->block_hash, &key, &b->hash_link);
377
        hash_table_insert(&cache->block_hash, &key, &b->hash_link);
381
 
378
 
382
        /*
379
        /*
383
         * Lock the block before releasing the cache lock. Thus we don't
380
         * Lock the block before releasing the cache lock. Thus we don't
384
         * kill concurent operations on the cache while doing I/O on the
381
         * kill concurent operations on the cache while doing I/O on the
385
         * block.
382
         * block.
386
         */
383
         */
387
        fibril_mutex_lock(&b->lock);
384
        fibril_mutex_lock(&b->lock);
388
        fibril_mutex_unlock(&cache->lock);
385
        fibril_mutex_unlock(&cache->lock);
389
 
386
 
390
        if (sync) {
387
        if (sync) {
391
            /*
388
            /*
392
             * The block is dirty and needs to be written back to
389
             * The block is dirty and needs to be written back to
393
             * the device before we can read in the new contents.
390
             * the device before we can read in the new contents.
394
             */
391
             */
395
            abort();    /* TODO: block_write() */
392
            abort();    /* TODO: block_write() */
396
        }
393
        }
397
        if (!(flags & BLOCK_FLAGS_NOREAD)) {
394
        if (!(flags & BLOCK_FLAGS_NOREAD)) {
398
            /*
395
            /*
399
             * The block contains old or no data. We need to read
396
             * The block contains old or no data. We need to read
400
             * the new contents from the device.
397
             * the new contents from the device.
401
             */
398
             */
402
            rc = block_read(dev_handle, &bufpos, &buflen, &pos,
-
 
403
                b->data, cache->block_size, cache->block_size);
399
            rc = read_block(devcon, b->boff, cache->block_size);
404
            assert(rc == EOK);
400
            assert(rc == EOK);
-
 
401
            memcpy(b->data, devcon->com_area, cache->block_size);
405
        }
402
        }
406
 
403
 
407
        fibril_mutex_unlock(&b->lock);
404
        fibril_mutex_unlock(&b->lock);
408
    }
405
    }
409
    return b;
406
    return b;
410
}
407
}
411
 
408
 
412
/** Release a reference to a block.
409
/** Release a reference to a block.
413
 *
410
 *
414
 * If the last reference is dropped, the block is put on the free list.
411
 * If the last reference is dropped, the block is put on the free list.
415
 *
412
 *
416
 * @param block     Block of which a reference is to be released.
413
 * @param block     Block of which a reference is to be released.
417
 */
414
 */
418
void block_put(block_t *block)
415
void block_put(block_t *block)
419
{
416
{
420
    devcon_t *devcon = devcon_search(block->dev_handle);
417
    devcon_t *devcon = devcon_search(block->dev_handle);
421
    cache_t *cache;
418
    cache_t *cache;
422
    int rc;
419
    int rc;
423
 
420
 
424
    assert(devcon);
421
    assert(devcon);
425
    assert(devcon->cache);
422
    assert(devcon->cache);
426
 
423
 
427
    cache = devcon->cache;
424
    cache = devcon->cache;
428
    fibril_mutex_lock(&cache->lock);
425
    fibril_mutex_lock(&cache->lock);
429
    fibril_mutex_lock(&block->lock);
426
    fibril_mutex_lock(&block->lock);
430
    if (!--block->refcnt) {
427
    if (!--block->refcnt) {
431
        /*
428
        /*
432
         * Last reference to the block was dropped, put the block on the
429
         * Last reference to the block was dropped, put the block on the
433
         * free list.
430
         * free list.
434
         */
431
         */
435
        list_append(&block->free_link, &cache->free_head);
432
        list_append(&block->free_link, &cache->free_head);
436
        if (cache->mode != CACHE_MODE_WB && block->dirty) {
433
        if (cache->mode != CACHE_MODE_WB && block->dirty) {
437
            rc = write_block(devcon, block->boff, block->size,
434
            memcpy(devcon->com_area, block->data, block->size);
438
                block->data);
435
            rc = write_block(devcon, block->boff, block->size);
439
            assert(rc == EOK);
436
            assert(rc == EOK);
440
 
437
 
441
            block->dirty = false;
438
            block->dirty = false;
442
        }
439
        }
443
    }
440
    }
444
    fibril_mutex_unlock(&block->lock);
441
    fibril_mutex_unlock(&block->lock);
445
    fibril_mutex_unlock(&cache->lock);
442
    fibril_mutex_unlock(&cache->lock);
446
}
443
}
447
 
444
 
448
/** Read data from a block device.
445
/** Read sequential data from a block device.
449
 *
446
 *
450
 * @param dev_handle    Device handle of the block device.
447
 * @param dev_handle    Device handle of the block device.
451
 * @param bufpos    Pointer to the first unread valid offset within the
448
 * @param bufpos    Pointer to the first unread valid offset within the
452
 *          communication buffer.
449
 *          communication buffer.
453
 * @param buflen    Pointer to the number of unread bytes that are ready in
450
 * @param buflen    Pointer to the number of unread bytes that are ready in
454
 *          the communication buffer.
451
 *          the communication buffer.
455
 * @param pos       Device position to be read.
452
 * @param pos       Device position to be read.
456
 * @param dst       Destination buffer.
453
 * @param dst       Destination buffer.
457
 * @param size      Size of the destination buffer.
454
 * @param size      Size of the destination buffer.
458
 * @param block_size    Block size to be used for the transfer.
455
 * @param block_size    Block size to be used for the transfer.
459
 *
456
 *
460
 * @return      EOK on success or a negative return code on failure.
457
 * @return      EOK on success or a negative return code on failure.
461
 */
458
 */
462
int
-
 
463
block_read(dev_handle_t dev_handle, off_t *bufpos, size_t *buflen, off_t *pos,
459
int block_seqread(dev_handle_t dev_handle, off_t *bufpos, size_t *buflen,
464
    void *dst, size_t size, size_t block_size)
460
    off_t *pos, void *dst, size_t size, size_t block_size)
465
{
461
{
466
    off_t offset = 0;
462
    off_t offset = 0;
467
    size_t left = size;
463
    size_t left = size;
468
    devcon_t *devcon = devcon_search(dev_handle);
464
    devcon_t *devcon = devcon_search(dev_handle);
469
    assert(devcon);
465
    assert(devcon);
470
   
466
   
471
    while (left > 0) {
467
    while (left > 0) {
472
        size_t rd;
468
        size_t rd;
473
       
469
       
474
        if (*bufpos + left < *buflen)
470
        if (*bufpos + left < *buflen)
475
            rd = left;
471
            rd = left;
476
        else
472
        else
477
            rd = *buflen - *bufpos;
473
            rd = *buflen - *bufpos;
478
       
474
       
479
        if (rd > 0) {
475
        if (rd > 0) {
480
            /*
476
            /*
481
             * Copy the contents of the communication buffer to the
477
             * Copy the contents of the communication buffer to the
482
             * destination buffer.
478
             * destination buffer.
483
             */
479
             */
484
            memcpy(dst + offset, devcon->com_area + *bufpos, rd);
480
            memcpy(dst + offset, devcon->com_area + *bufpos, rd);
485
            offset += rd;
481
            offset += rd;
486
            *bufpos += rd;
482
            *bufpos += rd;
487
            *pos += rd;
483
            *pos += rd;
488
            left -= rd;
484
            left -= rd;
489
        }
485
        }
490
       
486
       
491
        if (*bufpos == (off_t) *buflen) {
487
        if (*bufpos == (off_t) *buflen) {
492
            /* Refill the communication buffer with a new block. */
488
            /* Refill the communication buffer with a new block. */
493
            ipcarg_t retval;
489
            int rc;
494
            int rc = async_req_2_1(devcon->dev_phone, BD_READ_BLOCK,
-
 
-
 
490
 
495
                *pos / block_size, block_size, &retval);
491
            rc = read_block(devcon, *pos / block_size, block_size);
496
            if ((rc != EOK) || (retval != EOK))
492
            if (rc != EOK)
497
                return (rc != EOK ? rc : (int) retval);
493
                return rc;
498
           
494
           
499
            *bufpos = 0;
495
            *bufpos = 0;
500
            *buflen = block_size;
496
            *buflen = block_size;
501
        }
497
        }
502
    }
498
    }
503
   
499
   
504
    return EOK;
500
    return EOK;
505
}
501
}
506
 
502
 
-
 
503
/** Read block from block device.
-
 
504
 *
-
 
505
 * @param devcon    Device connection.
-
 
506
 * @param boff      Block index.
-
 
507
 * @param block_size    Block size.
-
 
508
 * @param src       Buffer for storing the data.
-
 
509
 *
-
 
510
 * @return      EOK on success or negative error code on failure.
-
 
511
 */
-
 
512
static int read_block(devcon_t *devcon, bn_t boff, size_t block_size)
-
 
513
{
-
 
514
    ipcarg_t retval;
-
 
515
    int rc;
-
 
516
 
-
 
517
    assert(devcon);
-
 
518
    rc = async_req_2_1(devcon->dev_phone, BD_READ_BLOCK, boff, block_size,
-
 
519
        &retval);
-
 
520
    if ((rc != EOK) || (retval != EOK))
-
 
521
        return (rc != EOK ? rc : (int) retval);
-
 
522
 
-
 
523
    return EOK;
-
 
524
}
-
 
525
 
507
/** Write block to block device.
526
/** Write block to block device.
508
 *
527
 *
509
 * @param devcon    Device connection.
528
 * @param devcon    Device connection.
510
 * @param boff      Block index.
529
 * @param boff      Block index.
511
 * @param block_size    Block size.
530
 * @param block_size    Block size.
512
 * @param src       Buffer containing the data to write.
531
 * @param src       Buffer containing the data to write.
513
 *
532
 *
514
 * @return      EOK on success or negative error code on failure.
533
 * @return      EOK on success or negative error code on failure.
515
 */
534
 */
516
static int write_block(devcon_t *devcon, bn_t boff, size_t block_size,
535
static int write_block(devcon_t *devcon, bn_t boff, size_t block_size)
517
    const void *src)
-
 
518
{
536
{
519
    ipcarg_t retval;
537
    ipcarg_t retval;
520
    int rc;
538
    int rc;
521
 
539
 
522
    assert(devcon);
540
    assert(devcon);
523
    memcpy(devcon->com_area, src, block_size);
-
 
524
   
-
 
525
    rc = async_req_2_1(devcon->dev_phone, BD_WRITE_BLOCK,
541
    rc = async_req_2_1(devcon->dev_phone, BD_WRITE_BLOCK, boff, block_size,
526
        boff, block_size, &retval);
542
        &retval);
527
    if ((rc != EOK) || (retval != EOK))
543
    if ((rc != EOK) || (retval != EOK))
528
        return (rc != EOK ? rc : (int) retval);
544
        return (rc != EOK ? rc : (int) retval);
529
 
545
 
530
    return EOK;
546
    return EOK;
531
}
547
}
532
 
548
 
533
/** @}
549
/** @}
534
 */
550
 */
535
 
551