Subversion Repositories HelenOS

Rev

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

Rev 3537 Rev 3538
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
 
51
 
52
/** Lock protecting the device connection list */
52
/** Lock protecting the device connection list */
53
static futex_t dcl_lock = FUTEX_INITIALIZER;
53
static futex_t dcl_lock = FUTEX_INITIALIZER;
54
/** Device connection list head. */
54
/** Device connection list head. */
55
static LIST_INITIALIZE(dcl_head);
55
static LIST_INITIALIZE(dcl_head);
56
 
56
 
57
typedef struct {
57
typedef struct {
58
    link_t link;
58
    link_t link;
59
    int dev_handle;
59
    int dev_handle;
60
    int dev_phone;
60
    int dev_phone;
61
    void *com_area;
61
    void *com_area;
62
    size_t com_size;
62
    size_t com_size;
63
    void *bb_buf;
63
    void *bb_buf;
64
    off_t bb_off;
64
    off_t bb_off;
65
    size_t bb_size;
65
    size_t bb_size;
66
} devcon_t;
66
} devcon_t;
67
 
67
 
68
static devcon_t *devcon_search(dev_handle_t dev_handle)
68
static devcon_t *devcon_search(dev_handle_t dev_handle)
69
{
69
{
70
    link_t *cur;
70
    link_t *cur;
71
 
71
 
72
    futex_down(&dcl_lock);
72
    futex_down(&dcl_lock);
73
    for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
73
    for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
74
        devcon_t *devcon = list_get_instance(cur, devcon_t, link);
74
        devcon_t *devcon = list_get_instance(cur, devcon_t, link);
75
        if (devcon->dev_handle == dev_handle) {
75
        if (devcon->dev_handle == dev_handle) {
76
            futex_up(&dcl_lock);
76
            futex_up(&dcl_lock);
77
            return devcon;
77
            return devcon;
78
        }
78
        }
79
    }
79
    }
80
    futex_up(&dcl_lock);
80
    futex_up(&dcl_lock);
81
    return NULL;
81
    return NULL;
82
}
82
}
83
 
83
 
84
static int devcon_add(dev_handle_t dev_handle, int dev_phone, void *com_area,
84
static int devcon_add(dev_handle_t dev_handle, int dev_phone, void *com_area,
85
   size_t com_size)
85
   size_t com_size)
86
{
86
{
87
    link_t *cur;
87
    link_t *cur;
88
    devcon_t *devcon;
88
    devcon_t *devcon;
89
 
89
 
90
    devcon = malloc(sizeof(devcon_t));
90
    devcon = malloc(sizeof(devcon_t));
91
    if (!devcon)
91
    if (!devcon)
92
        return ENOMEM;
92
        return ENOMEM;
93
   
93
   
94
    link_initialize(&devcon->link);
94
    link_initialize(&devcon->link);
95
    devcon->dev_handle = dev_handle;
95
    devcon->dev_handle = dev_handle;
96
    devcon->dev_phone = dev_phone;
96
    devcon->dev_phone = dev_phone;
97
    devcon->com_area = com_area;
97
    devcon->com_area = com_area;
98
    devcon->com_size = com_size;
98
    devcon->com_size = com_size;
99
    devcon->bb_buf = NULL;
99
    devcon->bb_buf = NULL;
100
    devcon->bb_off = 0;
100
    devcon->bb_off = 0;
101
    devcon->bb_size = 0;
101
    devcon->bb_size = 0;
102
 
102
 
103
    futex_down(&dcl_lock);
103
    futex_down(&dcl_lock);
104
    for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
104
    for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
105
        devcon_t *d = list_get_instance(cur, devcon_t, link);
105
        devcon_t *d = list_get_instance(cur, devcon_t, link);
106
        if (d->dev_handle == dev_handle) {
106
        if (d->dev_handle == dev_handle) {
107
            futex_up(&dcl_lock);
107
            futex_up(&dcl_lock);
108
            free(devcon);
108
            free(devcon);
109
            return EEXIST;
109
            return EEXIST;
110
        }
110
        }
111
    }
111
    }
112
    list_append(&devcon->link, &dcl_head);
112
    list_append(&devcon->link, &dcl_head);
113
    futex_up(&dcl_lock);
113
    futex_up(&dcl_lock);
114
    return EOK;
114
    return EOK;
115
}
115
}
116
 
116
 
117
static void devcon_remove(devcon_t *devcon)
117
static void devcon_remove(devcon_t *devcon)
118
{
118
{
119
    futex_down(&dcl_lock);
119
    futex_down(&dcl_lock);
120
    list_remove(&devcon->link);
120
    list_remove(&devcon->link);
121
    futex_up(&dcl_lock);
121
    futex_up(&dcl_lock);
122
}
122
}
123
 
123
 
124
int block_init(dev_handle_t dev_handle, size_t com_size)
124
int block_init(dev_handle_t dev_handle, size_t com_size)
125
{
125
{
126
    int rc;
126
    int rc;
127
    int dev_phone;
127
    int dev_phone;
128
    void *com_area;
128
    void *com_area;
129
   
129
   
130
    com_area = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE,
130
    com_area = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE,
131
        MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
131
        MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
132
    if (!com_area) {
132
    if (!com_area) {
133
        return ENOMEM;
133
        return ENOMEM;
134
    }
134
    }
135
    dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
135
    dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
136
        DEVMAP_CONNECT_TO_DEVICE, dev_handle);
136
        DEVMAP_CONNECT_TO_DEVICE, dev_handle);
137
 
137
 
138
    if (dev_phone < 0) {
138
    if (dev_phone < 0) {
139
        munmap(com_area, com_size);
139
        munmap(com_area, com_size);
140
        return dev_phone;
140
        return dev_phone;
141
    }
141
    }
142
 
142
 
143
    rc = ipc_share_out_start(dev_phone, com_area,
143
    rc = ipc_share_out_start(dev_phone, com_area,
144
        AS_AREA_READ | AS_AREA_WRITE);
144
        AS_AREA_READ | AS_AREA_WRITE);
145
    if (rc != EOK) {
145
    if (rc != EOK) {
146
            munmap(com_area, com_size);
146
            munmap(com_area, com_size);
147
        ipc_hangup(dev_phone);
147
        ipc_hangup(dev_phone);
148
        return rc;
148
        return rc;
149
    }
149
    }
150
   
150
   
151
    rc = devcon_add(dev_handle, dev_phone, com_area, com_size);
151
    rc = devcon_add(dev_handle, dev_phone, com_area, com_size);
152
    if (rc != EOK) {
152
    if (rc != EOK) {
153
        munmap(com_area, com_size);
153
        munmap(com_area, com_size);
154
        ipc_hangup(dev_phone);
154
        ipc_hangup(dev_phone);
155
        return rc;
155
        return rc;
156
    }
156
    }
157
 
157
 
158
    return EOK;
158
    return EOK;
159
}
159
}
160
 
160
 
161
void block_fini(dev_handle_t dev_handle)
161
void block_fini(dev_handle_t dev_handle)
162
{
162
{
163
    devcon_t *devcon = devcon_search(dev_handle);
163
    devcon_t *devcon = devcon_search(dev_handle);
164
    assert(devcon);
164
    assert(devcon);
165
   
165
   
166
    devcon_remove(devcon);
166
    devcon_remove(devcon);
167
 
167
 
168
    if (devcon->bb_buf)
168
    if (devcon->bb_buf)
169
        free(devcon->bb_buf);
169
        free(devcon->bb_buf);
170
    munmap(devcon->com_area, devcon->com_size);
170
    munmap(devcon->com_area, devcon->com_size);
171
    ipc_hangup(devcon->dev_phone);
171
    ipc_hangup(devcon->dev_phone);
172
 
172
 
173
    free(devcon);  
173
    free(devcon);  
174
}
174
}
175
 
175
 
176
int block_bb_read(dev_handle_t dev_handle, off_t off, size_t size)
176
int block_bb_read(dev_handle_t dev_handle, off_t off, size_t size)
177
{
177
{
178
    void *bb_buf;
178
    void *bb_buf;
-
 
179
    int rc;
179
 
180
 
180
    devcon_t *devcon = devcon_search(dev_handle);
181
    devcon_t *devcon = devcon_search(dev_handle);
181
    if (!devcon)
182
    if (!devcon)
182
        return ENOENT;
183
        return ENOENT;
183
    if (devcon->bb_buf)
184
    if (devcon->bb_buf)
184
        return EEXIST;
185
        return EEXIST;
185
    bb_buf = malloc(size);
186
    bb_buf = malloc(size);
186
    if (!bb_buf)
187
    if (!bb_buf)
187
        return ENOMEM;
188
        return ENOMEM;
188
   
189
   
189
    off_t bufpos = 0;
190
    off_t bufpos = 0;
190
    size_t buflen = 0;
191
    size_t buflen = 0;
191
    if (!block_read(dev_handle, &bufpos, &buflen, &off,
192
    rc = block_read(dev_handle, &bufpos, &buflen, &off,
192
        bb_buf, size, size)) {
193
        bb_buf, size, size);
-
 
194
    if (rc != EOK) {
193
            free(bb_buf);
195
            free(bb_buf);
194
        return EIO; /* XXX real error code */
196
        return rc;
195
    }
197
    }
196
    devcon->bb_buf = bb_buf;
198
    devcon->bb_buf = bb_buf;
197
    devcon->bb_off = off;
199
    devcon->bb_off = off;
198
    devcon->bb_size = size;
200
    devcon->bb_size = size;
199
 
201
 
200
    return EOK;
202
    return EOK;
201
}
203
}
202
 
204
 
203
void *block_bb_get(dev_handle_t dev_handle)
205
void *block_bb_get(dev_handle_t dev_handle)
204
{
206
{
205
    devcon_t *devcon = devcon_search(dev_handle);
207
    devcon_t *devcon = devcon_search(dev_handle);
206
    assert(devcon);
208
    assert(devcon);
207
    return devcon->bb_buf;
209
    return devcon->bb_buf;
208
}
210
}
209
 
211
 
210
/** Read data from a block device.
212
/** Read data from a block device.
211
 *
213
 *
212
 * @param dev_handle    Device handle of the block device.
214
 * @param dev_handle    Device handle of the block device.
213
 * @param bufpos    Pointer to the first unread valid offset within the
215
 * @param bufpos    Pointer to the first unread valid offset within the
214
 *          communication buffer.
216
 *          communication buffer.
215
 * @param buflen    Pointer to the number of unread bytes that are ready in
217
 * @param buflen    Pointer to the number of unread bytes that are ready in
216
 *          the communication buffer.
218
 *          the communication buffer.
217
 * @param pos       Device position to be read.
219
 * @param pos       Device position to be read.
218
 * @param dst       Destination buffer.
220
 * @param dst       Destination buffer.
219
 * @param size      Size of the destination buffer.
221
 * @param size      Size of the destination buffer.
220
 * @param block_size    Block size to be used for the transfer.
222
 * @param block_size    Block size to be used for the transfer.
221
 *
223
 *
222
 * @return      True on success, false on failure.
224
 * @return      EOK on success or a negative return code on failure.
223
 */
225
 */
224
bool
226
int
225
block_read(int dev_handle, off_t *bufpos, size_t *buflen, off_t *pos, void *dst,
227
block_read(int dev_handle, off_t *bufpos, size_t *buflen, off_t *pos, void *dst,
226
    size_t size, size_t block_size)
228
    size_t size, size_t block_size)
227
{
229
{
228
    off_t offset = 0;
230
    off_t offset = 0;
229
    size_t left = size;
231
    size_t left = size;
230
    devcon_t *devcon = devcon_search(dev_handle);
232
    devcon_t *devcon = devcon_search(dev_handle);
231
    assert(devcon);
233
    assert(devcon);
232
   
234
   
233
    while (left > 0) {
235
    while (left > 0) {
234
        size_t rd;
236
        size_t rd;
235
       
237
       
236
        if (*bufpos + left < *buflen)
238
        if (*bufpos + left < *buflen)
237
            rd = left;
239
            rd = left;
238
        else
240
        else
239
            rd = *buflen - *bufpos;
241
            rd = *buflen - *bufpos;
240
       
242
       
241
        if (rd > 0) {
243
        if (rd > 0) {
242
            /*
244
            /*
243
             * Copy the contents of the communication buffer to the
245
             * Copy the contents of the communication buffer to the
244
             * destination buffer.
246
             * destination buffer.
245
             */
247
             */
246
            memcpy(dst + offset, devcon->com_area + *bufpos, rd);
248
            memcpy(dst + offset, devcon->com_area + *bufpos, rd);
247
            offset += rd;
249
            offset += rd;
248
            *bufpos += rd;
250
            *bufpos += rd;
249
            *pos += rd;
251
            *pos += rd;
250
            left -= rd;
252
            left -= rd;
251
        }
253
        }
252
       
254
       
253
        if (*bufpos == *buflen) {
255
        if (*bufpos == *buflen) {
254
            /* Refill the communication buffer with a new block. */
256
            /* Refill the communication buffer with a new block. */
255
            ipcarg_t retval;
257
            ipcarg_t retval;
256
            int rc = async_req_2_1(devcon->dev_phone, RD_READ_BLOCK,
258
            int rc = async_req_2_1(devcon->dev_phone, RD_READ_BLOCK,
257
                *pos / block_size, block_size, &retval);
259
                *pos / block_size, block_size, &retval);
258
            if ((rc != EOK) || (retval != EOK))
260
            if ((rc != EOK) || (retval != EOK))
259
                return false;
261
                return (rc != EOK ? rc : retval);
260
           
262
           
261
            *bufpos = 0;
263
            *bufpos = 0;
262
            *buflen = block_size;
264
            *buflen = block_size;
263
        }
265
        }
264
    }
266
    }
265
   
267
   
266
    return true;
268
    return EOK;
267
}
269
}
268
 
270
 
269
block_t *block_get(dev_handle_t dev_handle, off_t offset, size_t bs)
271
block_t *block_get(dev_handle_t dev_handle, off_t offset, size_t bs)
270
{
272
{
271
    /* FIXME */
273
    /* FIXME */
272
    block_t *b;
274
    block_t *b;
273
    off_t bufpos = 0;
275
    off_t bufpos = 0;
274
    size_t buflen = 0;
276
    size_t buflen = 0;
275
    off_t pos = offset * bs;
277
    off_t pos = offset * bs;
276
 
278
 
277
    b = malloc(sizeof(block_t));
279
    b = malloc(sizeof(block_t));
278
    if (!b)
280
    if (!b)
279
        return NULL;
281
        return NULL;
280
   
282
   
281
    b->data = malloc(bs);
283
    b->data = malloc(bs);
282
    if (!b->data) {
284
    if (!b->data) {
283
        free(b);
285
        free(b);
284
        return NULL;
286
        return NULL;
285
    }
287
    }
286
    b->size = bs;
288
    b->size = bs;
287
 
289
 
288
    if (!block_read(dev_handle, &bufpos, &buflen, &pos, b->data,
290
    if (block_read(dev_handle, &bufpos, &buflen, &pos, b->data,
289
        bs, bs)) {
291
        bs, bs) != EOK) {
290
        free(b->data);
292
        free(b->data);
291
        free(b);
293
        free(b);
292
        return NULL;
294
        return NULL;
293
    }
295
    }
294
 
296
 
295
    return b;
297
    return b;
296
}
298
}
297
 
299
 
298
void block_put(block_t *block)
300
void block_put(block_t *block)
299
{
301
{
300
    /* FIXME */
302
    /* FIXME */
301
    free(block->data);
303
    free(block->data);
302
    free(block);
304
    free(block);
303
}
305
}
304
 
306
 
305
/** @}
307
/** @}
306
 */
308
 */
307
 
309