Subversion Repositories HelenOS

Rev

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

Rev 3538 Rev 3539
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 "../../srv/rd/rd.h"
40
#include "../../srv/rd/rd.h"
41
#include <ipc/devmap.h>
41
#include <ipc/devmap.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 <libadt/list.h>
-
 
51
#include <libadt/hash_table.h>
51
 
52
 
52
/** Lock protecting the device connection list */
53
/** Lock protecting the device connection list */
53
static futex_t dcl_lock = FUTEX_INITIALIZER;
54
static futex_t dcl_lock = FUTEX_INITIALIZER;
54
/** Device connection list head. */
55
/** Device connection list head. */
55
static LIST_INITIALIZE(dcl_head);
56
static LIST_INITIALIZE(dcl_head);
56
 
57
 
-
 
58
#define CACHE_BUCKETS_LOG2      10
-
 
59
#define CACHE_BUCKETS           (1 << CACHE_BUCKETS_LOG2)
-
 
60
 
-
 
61
typedef struct {
-
 
62
    futex_t lock;
-
 
63
    size_t block_size;      /**< Block size. */
-
 
64
    unsigned block_count;       /**< Total number of blocks. */
-
 
65
    hash_table_t block_hash;
-
 
66
    link_t free_head;
-
 
67
} cache_t;
-
 
68
 
57
typedef struct {
69
typedef struct {
58
    link_t link;
70
    link_t link;
59
    int dev_handle;
71
    int dev_handle;
60
    int dev_phone;
72
    int dev_phone;
61
    void *com_area;
73
    void *com_area;
62
    size_t com_size;
74
    size_t com_size;
63
    void *bb_buf;
75
    void *bb_buf;
64
    off_t bb_off;
76
    off_t bb_off;
65
    size_t bb_size;
77
    size_t bb_size;
-
 
78
    cache_t *cache;
66
} devcon_t;
79
} devcon_t;
67
 
80
 
68
static devcon_t *devcon_search(dev_handle_t dev_handle)
81
static devcon_t *devcon_search(dev_handle_t dev_handle)
69
{
82
{
70
    link_t *cur;
83
    link_t *cur;
71
 
84
 
72
    futex_down(&dcl_lock);
85
    futex_down(&dcl_lock);
73
    for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
86
    for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
74
        devcon_t *devcon = list_get_instance(cur, devcon_t, link);
87
        devcon_t *devcon = list_get_instance(cur, devcon_t, link);
75
        if (devcon->dev_handle == dev_handle) {
88
        if (devcon->dev_handle == dev_handle) {
76
            futex_up(&dcl_lock);
89
            futex_up(&dcl_lock);
77
            return devcon;
90
            return devcon;
78
        }
91
        }
79
    }
92
    }
80
    futex_up(&dcl_lock);
93
    futex_up(&dcl_lock);
81
    return NULL;
94
    return NULL;
82
}
95
}
83
 
96
 
84
static int devcon_add(dev_handle_t dev_handle, int dev_phone, void *com_area,
97
static int devcon_add(dev_handle_t dev_handle, int dev_phone, void *com_area,
85
   size_t com_size)
98
   size_t com_size)
86
{
99
{
87
    link_t *cur;
100
    link_t *cur;
88
    devcon_t *devcon;
101
    devcon_t *devcon;
89
 
102
 
90
    devcon = malloc(sizeof(devcon_t));
103
    devcon = malloc(sizeof(devcon_t));
91
    if (!devcon)
104
    if (!devcon)
92
        return ENOMEM;
105
        return ENOMEM;
93
   
106
   
94
    link_initialize(&devcon->link);
107
    link_initialize(&devcon->link);
95
    devcon->dev_handle = dev_handle;
108
    devcon->dev_handle = dev_handle;
96
    devcon->dev_phone = dev_phone;
109
    devcon->dev_phone = dev_phone;
97
    devcon->com_area = com_area;
110
    devcon->com_area = com_area;
98
    devcon->com_size = com_size;
111
    devcon->com_size = com_size;
99
    devcon->bb_buf = NULL;
112
    devcon->bb_buf = NULL;
100
    devcon->bb_off = 0;
113
    devcon->bb_off = 0;
101
    devcon->bb_size = 0;
114
    devcon->bb_size = 0;
-
 
115
    devcon->cache = NULL;
102
 
116
 
103
    futex_down(&dcl_lock);
117
    futex_down(&dcl_lock);
104
    for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
118
    for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
105
        devcon_t *d = list_get_instance(cur, devcon_t, link);
119
        devcon_t *d = list_get_instance(cur, devcon_t, link);
106
        if (d->dev_handle == dev_handle) {
120
        if (d->dev_handle == dev_handle) {
107
            futex_up(&dcl_lock);
121
            futex_up(&dcl_lock);
108
            free(devcon);
122
            free(devcon);
109
            return EEXIST;
123
            return EEXIST;
110
        }
124
        }
111
    }
125
    }
112
    list_append(&devcon->link, &dcl_head);
126
    list_append(&devcon->link, &dcl_head);
113
    futex_up(&dcl_lock);
127
    futex_up(&dcl_lock);
114
    return EOK;
128
    return EOK;
115
}
129
}
116
 
130
 
117
static void devcon_remove(devcon_t *devcon)
131
static void devcon_remove(devcon_t *devcon)
118
{
132
{
119
    futex_down(&dcl_lock);
133
    futex_down(&dcl_lock);
120
    list_remove(&devcon->link);
134
    list_remove(&devcon->link);
121
    futex_up(&dcl_lock);
135
    futex_up(&dcl_lock);
122
}
136
}
123
 
137
 
124
int block_init(dev_handle_t dev_handle, size_t com_size)
138
int block_init(dev_handle_t dev_handle, size_t com_size)
125
{
139
{
126
    int rc;
140
    int rc;
127
    int dev_phone;
141
    int dev_phone;
128
    void *com_area;
142
    void *com_area;
129
   
143
   
130
    com_area = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE,
144
    com_area = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE,
131
        MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
145
        MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
132
    if (!com_area) {
146
    if (!com_area) {
133
        return ENOMEM;
147
        return ENOMEM;
134
    }
148
    }
135
    dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
149
    dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
136
        DEVMAP_CONNECT_TO_DEVICE, dev_handle);
150
        DEVMAP_CONNECT_TO_DEVICE, dev_handle);
137
 
151
 
138
    if (dev_phone < 0) {
152
    if (dev_phone < 0) {
139
        munmap(com_area, com_size);
153
        munmap(com_area, com_size);
140
        return dev_phone;
154
        return dev_phone;
141
    }
155
    }
142
 
156
 
143
    rc = ipc_share_out_start(dev_phone, com_area,
157
    rc = ipc_share_out_start(dev_phone, com_area,
144
        AS_AREA_READ | AS_AREA_WRITE);
158
        AS_AREA_READ | AS_AREA_WRITE);
145
    if (rc != EOK) {
159
    if (rc != EOK) {
146
            munmap(com_area, com_size);
160
            munmap(com_area, com_size);
147
        ipc_hangup(dev_phone);
161
        ipc_hangup(dev_phone);
148
        return rc;
162
        return rc;
149
    }
163
    }
150
   
164
   
151
    rc = devcon_add(dev_handle, dev_phone, com_area, com_size);
165
    rc = devcon_add(dev_handle, dev_phone, com_area, com_size);
152
    if (rc != EOK) {
166
    if (rc != EOK) {
153
        munmap(com_area, com_size);
167
        munmap(com_area, com_size);
154
        ipc_hangup(dev_phone);
168
        ipc_hangup(dev_phone);
155
        return rc;
169
        return rc;
156
    }
170
    }
157
 
171
 
158
    return EOK;
172
    return EOK;
159
}
173
}
160
 
174
 
161
void block_fini(dev_handle_t dev_handle)
175
void block_fini(dev_handle_t dev_handle)
162
{
176
{
163
    devcon_t *devcon = devcon_search(dev_handle);
177
    devcon_t *devcon = devcon_search(dev_handle);
164
    assert(devcon);
178
    assert(devcon);
165
   
179
   
166
    devcon_remove(devcon);
180
    devcon_remove(devcon);
167
 
181
 
168
    if (devcon->bb_buf)
182
    if (devcon->bb_buf)
169
        free(devcon->bb_buf);
183
        free(devcon->bb_buf);
-
 
184
 
-
 
185
    if (devcon->cache) {
-
 
186
        hash_table_destroy(&devcon->cache->block_hash);
-
 
187
        free(devcon->cache);
-
 
188
    }
-
 
189
 
170
    munmap(devcon->com_area, devcon->com_size);
190
    munmap(devcon->com_area, devcon->com_size);
171
    ipc_hangup(devcon->dev_phone);
191
    ipc_hangup(devcon->dev_phone);
172
 
192
 
173
    free(devcon);  
193
    free(devcon);  
174
}
194
}
175
 
195
 
176
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)
177
{
197
{
178
    void *bb_buf;
198
    void *bb_buf;
179
    int rc;
199
    int rc;
180
 
200
 
181
    devcon_t *devcon = devcon_search(dev_handle);
201
    devcon_t *devcon = devcon_search(dev_handle);
182
    if (!devcon)
202
    if (!devcon)
183
        return ENOENT;
203
        return ENOENT;
184
    if (devcon->bb_buf)
204
    if (devcon->bb_buf)
185
        return EEXIST;
205
        return EEXIST;
186
    bb_buf = malloc(size);
206
    bb_buf = malloc(size);
187
    if (!bb_buf)
207
    if (!bb_buf)
188
        return ENOMEM;
208
        return ENOMEM;
189
   
209
   
190
    off_t bufpos = 0;
210
    off_t bufpos = 0;
191
    size_t buflen = 0;
211
    size_t buflen = 0;
192
    rc = block_read(dev_handle, &bufpos, &buflen, &off,
212
    rc = block_read(dev_handle, &bufpos, &buflen, &off,
193
        bb_buf, size, size);
213
        bb_buf, size, size);
194
    if (rc != EOK) {
214
    if (rc != EOK) {
195
            free(bb_buf);
215
            free(bb_buf);
196
        return rc;
216
        return rc;
197
    }
217
    }
198
    devcon->bb_buf = bb_buf;
218
    devcon->bb_buf = bb_buf;
199
    devcon->bb_off = off;
219
    devcon->bb_off = off;
200
    devcon->bb_size = size;
220
    devcon->bb_size = size;
201
 
221
 
202
    return EOK;
222
    return EOK;
203
}
223
}
204
 
224
 
205
void *block_bb_get(dev_handle_t dev_handle)
225
void *block_bb_get(dev_handle_t dev_handle)
206
{
226
{
207
    devcon_t *devcon = devcon_search(dev_handle);
227
    devcon_t *devcon = devcon_search(dev_handle);
208
    assert(devcon);
228
    assert(devcon);
209
    return devcon->bb_buf;
229
    return devcon->bb_buf;
210
}
230
}
-
 
231
 
-
 
232
static hash_index_t cache_hash(unsigned long *key)
-
 
233
{
-
 
234
    return *key & (CACHE_BUCKETS - 1);
-
 
235
}
-
 
236
 
-
 
237
static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item)
-
 
238
{
-
 
239
    block_t *b = hash_table_get_instance(item, block_t, hash_link);
-
 
240
    return b->boff == *key;
-
 
241
}
-
 
242
 
-
 
243
static void cache_remove_callback(link_t *item)
-
 
244
{
-
 
245
}
-
 
246
 
-
 
247
static hash_table_operations_t cache_ops = {
-
 
248
    .hash = cache_hash,
-
 
249
    .compare = cache_compare,
-
 
250
    .remove_callback = cache_remove_callback
-
 
251
};
-
 
252
 
-
 
253
int block_cache_init(dev_handle_t dev_handle, size_t size, unsigned blocks)
-
 
254
{
-
 
255
    devcon_t *devcon = devcon_search(dev_handle);
-
 
256
    cache_t *cache;
-
 
257
    if (!devcon)
-
 
258
        return ENOENT;
-
 
259
    if (devcon->cache)
-
 
260
        return EEXIST;
-
 
261
    cache = malloc(sizeof(cache_t));
-
 
262
    if (!cache)
-
 
263
        return ENOMEM;
-
 
264
   
-
 
265
    futex_initialize(&cache->lock, 0);
-
 
266
    list_initialize(&cache->free_head);
-
 
267
    cache->block_size = size;
-
 
268
    cache->block_count = blocks;
-
 
269
 
-
 
270
    if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1,
-
 
271
        &cache_ops)) {
-
 
272
        free(cache);
-
 
273
        return ENOMEM;
-
 
274
    }
-
 
275
 
-
 
276
    devcon->cache = cache;
-
 
277
    return EOK;
-
 
278
}
211
 
279
 
212
/** Read data from a block device.
280
/** Read data from a block device.
213
 *
281
 *
214
 * @param dev_handle    Device handle of the block device.
282
 * @param dev_handle    Device handle of the block device.
215
 * @param bufpos    Pointer to the first unread valid offset within the
283
 * @param bufpos    Pointer to the first unread valid offset within the
216
 *          communication buffer.
284
 *          communication buffer.
217
 * @param buflen    Pointer to the number of unread bytes that are ready in
285
 * @param buflen    Pointer to the number of unread bytes that are ready in
218
 *          the communication buffer.
286
 *          the communication buffer.
219
 * @param pos       Device position to be read.
287
 * @param pos       Device position to be read.
220
 * @param dst       Destination buffer.
288
 * @param dst       Destination buffer.
221
 * @param size      Size of the destination buffer.
289
 * @param size      Size of the destination buffer.
222
 * @param block_size    Block size to be used for the transfer.
290
 * @param block_size    Block size to be used for the transfer.
223
 *
291
 *
224
 * @return      EOK on success or a negative return code on failure.
292
 * @return      EOK on success or a negative return code on failure.
225
 */
293
 */
226
int
294
int
227
block_read(int dev_handle, off_t *bufpos, size_t *buflen, off_t *pos, void *dst,
295
block_read(int dev_handle, off_t *bufpos, size_t *buflen, off_t *pos, void *dst,
228
    size_t size, size_t block_size)
296
    size_t size, size_t block_size)
229
{
297
{
230
    off_t offset = 0;
298
    off_t offset = 0;
231
    size_t left = size;
299
    size_t left = size;
232
    devcon_t *devcon = devcon_search(dev_handle);
300
    devcon_t *devcon = devcon_search(dev_handle);
233
    assert(devcon);
301
    assert(devcon);
234
   
302
   
235
    while (left > 0) {
303
    while (left > 0) {
236
        size_t rd;
304
        size_t rd;
237
       
305
       
238
        if (*bufpos + left < *buflen)
306
        if (*bufpos + left < *buflen)
239
            rd = left;
307
            rd = left;
240
        else
308
        else
241
            rd = *buflen - *bufpos;
309
            rd = *buflen - *bufpos;
242
       
310
       
243
        if (rd > 0) {
311
        if (rd > 0) {
244
            /*
312
            /*
245
             * Copy the contents of the communication buffer to the
313
             * Copy the contents of the communication buffer to the
246
             * destination buffer.
314
             * destination buffer.
247
             */
315
             */
248
            memcpy(dst + offset, devcon->com_area + *bufpos, rd);
316
            memcpy(dst + offset, devcon->com_area + *bufpos, rd);
249
            offset += rd;
317
            offset += rd;
250
            *bufpos += rd;
318
            *bufpos += rd;
251
            *pos += rd;
319
            *pos += rd;
252
            left -= rd;
320
            left -= rd;
253
        }
321
        }
254
       
322
       
255
        if (*bufpos == *buflen) {
323
        if (*bufpos == *buflen) {
256
            /* Refill the communication buffer with a new block. */
324
            /* Refill the communication buffer with a new block. */
257
            ipcarg_t retval;
325
            ipcarg_t retval;
258
            int rc = async_req_2_1(devcon->dev_phone, RD_READ_BLOCK,
326
            int rc = async_req_2_1(devcon->dev_phone, RD_READ_BLOCK,
259
                *pos / block_size, block_size, &retval);
327
                *pos / block_size, block_size, &retval);
260
            if ((rc != EOK) || (retval != EOK))
328
            if ((rc != EOK) || (retval != EOK))
261
                return (rc != EOK ? rc : retval);
329
                return (rc != EOK ? rc : retval);
262
           
330
           
263
            *bufpos = 0;
331
            *bufpos = 0;
264
            *buflen = block_size;
332
            *buflen = block_size;
265
        }
333
        }
266
    }
334
    }
267
   
335
   
268
    return EOK;
336
    return EOK;
269
}
337
}
270
 
338
 
271
block_t *block_get(dev_handle_t dev_handle, off_t offset, size_t bs)
339
block_t *block_get(dev_handle_t dev_handle, off_t offset, size_t bs)
272
{
340
{
273
    /* FIXME */
341
    /* FIXME */
274
    block_t *b;
342
    block_t *b;
275
    off_t bufpos = 0;
343
    off_t bufpos = 0;
276
    size_t buflen = 0;
344
    size_t buflen = 0;
277
    off_t pos = offset * bs;
345
    off_t pos = offset * bs;
278
 
346
 
279
    b = malloc(sizeof(block_t));
347
    b = malloc(sizeof(block_t));
280
    if (!b)
348
    if (!b)
281
        return NULL;
349
        return NULL;
282
   
350
   
283
    b->data = malloc(bs);
351
    b->data = malloc(bs);
284
    if (!b->data) {
352
    if (!b->data) {
285
        free(b);
353
        free(b);
286
        return NULL;
354
        return NULL;
287
    }
355
    }
288
    b->size = bs;
356
    b->size = bs;
289
 
357
 
290
    if (block_read(dev_handle, &bufpos, &buflen, &pos, b->data,
358
    if (block_read(dev_handle, &bufpos, &buflen, &pos, b->data,
291
        bs, bs) != EOK) {
359
        bs, bs) != EOK) {
292
        free(b->data);
360
        free(b->data);
293
        free(b);
361
        free(b);
294
        return NULL;
362
        return NULL;
295
    }
363
    }
296
 
364
 
297
    return b;
365
    return b;
298
}
366
}
299
 
367
 
300
void block_put(block_t *block)
368
void block_put(block_t *block)
301
{
369
{
302
    /* FIXME */
370
    /* FIXME */
303
    free(block->data);
371
    free(block->data);
304
    free(block);
372
    free(block);
305
}
373
}
306
 
374
 
307
/** @}
375
/** @}
308
 */
376
 */
309
 
377