Rev 3542 | Rev 3544 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3542 | Rev 3543 | ||
---|---|---|---|
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 | #include <libadt/hash_table.h> |
52 | 52 | ||
53 | /** Lock protecting the device connection list */ |
53 | /** Lock protecting the device connection list */ |
54 | static futex_t dcl_lock = FUTEX_INITIALIZER; |
54 | static futex_t dcl_lock = FUTEX_INITIALIZER; |
55 | /** Device connection list head. */ |
55 | /** Device connection list head. */ |
56 | static LIST_INITIALIZE(dcl_head); |
56 | static LIST_INITIALIZE(dcl_head); |
57 | 57 | ||
58 | #define CACHE_BUCKETS_LOG2 10 |
58 | #define CACHE_BUCKETS_LOG2 10 |
59 | #define CACHE_BUCKETS (1 << CACHE_BUCKETS_LOG2) |
59 | #define CACHE_BUCKETS (1 << CACHE_BUCKETS_LOG2) |
60 | 60 | ||
61 | typedef struct { |
61 | typedef struct { |
62 | futex_t lock; |
62 | futex_t lock; |
63 | size_t block_size; /**< Block size. */ |
63 | size_t block_size; /**< Block size. */ |
64 | unsigned block_count; /**< Total number of blocks. */ |
64 | unsigned block_count; /**< Total number of blocks. */ |
65 | hash_table_t block_hash; |
65 | hash_table_t block_hash; |
66 | link_t free_head; |
66 | link_t free_head; |
67 | } cache_t; |
67 | } cache_t; |
68 | 68 | ||
69 | typedef struct { |
69 | typedef struct { |
70 | link_t link; |
70 | link_t link; |
71 | int dev_handle; |
71 | int dev_handle; |
72 | int dev_phone; |
72 | int dev_phone; |
73 | void *com_area; |
73 | void *com_area; |
74 | size_t com_size; |
74 | size_t com_size; |
75 | void *bb_buf; |
75 | void *bb_buf; |
76 | off_t bb_off; |
76 | off_t bb_off; |
77 | size_t bb_size; |
77 | size_t bb_size; |
78 | cache_t *cache; |
78 | cache_t *cache; |
79 | } devcon_t; |
79 | } devcon_t; |
80 | 80 | ||
81 | static devcon_t *devcon_search(dev_handle_t dev_handle) |
81 | static devcon_t *devcon_search(dev_handle_t dev_handle) |
82 | { |
82 | { |
83 | link_t *cur; |
83 | link_t *cur; |
84 | 84 | ||
85 | futex_down(&dcl_lock); |
85 | futex_down(&dcl_lock); |
86 | for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) { |
86 | for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) { |
87 | devcon_t *devcon = list_get_instance(cur, devcon_t, link); |
87 | devcon_t *devcon = list_get_instance(cur, devcon_t, link); |
88 | if (devcon->dev_handle == dev_handle) { |
88 | if (devcon->dev_handle == dev_handle) { |
89 | futex_up(&dcl_lock); |
89 | futex_up(&dcl_lock); |
90 | return devcon; |
90 | return devcon; |
91 | } |
91 | } |
92 | } |
92 | } |
93 | futex_up(&dcl_lock); |
93 | futex_up(&dcl_lock); |
94 | return NULL; |
94 | return NULL; |
95 | } |
95 | } |
96 | 96 | ||
97 | 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, |
98 | size_t com_size) |
98 | size_t com_size) |
99 | { |
99 | { |
100 | link_t *cur; |
100 | link_t *cur; |
101 | devcon_t *devcon; |
101 | devcon_t *devcon; |
102 | 102 | ||
103 | devcon = malloc(sizeof(devcon_t)); |
103 | devcon = malloc(sizeof(devcon_t)); |
104 | if (!devcon) |
104 | if (!devcon) |
105 | return ENOMEM; |
105 | return ENOMEM; |
106 | 106 | ||
107 | link_initialize(&devcon->link); |
107 | link_initialize(&devcon->link); |
108 | devcon->dev_handle = dev_handle; |
108 | devcon->dev_handle = dev_handle; |
109 | devcon->dev_phone = dev_phone; |
109 | devcon->dev_phone = dev_phone; |
110 | devcon->com_area = com_area; |
110 | devcon->com_area = com_area; |
111 | devcon->com_size = com_size; |
111 | devcon->com_size = com_size; |
112 | devcon->bb_buf = NULL; |
112 | devcon->bb_buf = NULL; |
113 | devcon->bb_off = 0; |
113 | devcon->bb_off = 0; |
114 | devcon->bb_size = 0; |
114 | devcon->bb_size = 0; |
115 | devcon->cache = NULL; |
115 | devcon->cache = NULL; |
116 | 116 | ||
117 | futex_down(&dcl_lock); |
117 | futex_down(&dcl_lock); |
118 | for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) { |
118 | for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) { |
119 | devcon_t *d = list_get_instance(cur, devcon_t, link); |
119 | devcon_t *d = list_get_instance(cur, devcon_t, link); |
120 | if (d->dev_handle == dev_handle) { |
120 | if (d->dev_handle == dev_handle) { |
121 | futex_up(&dcl_lock); |
121 | futex_up(&dcl_lock); |
122 | free(devcon); |
122 | free(devcon); |
123 | return EEXIST; |
123 | return EEXIST; |
124 | } |
124 | } |
125 | } |
125 | } |
126 | list_append(&devcon->link, &dcl_head); |
126 | list_append(&devcon->link, &dcl_head); |
127 | futex_up(&dcl_lock); |
127 | futex_up(&dcl_lock); |
128 | return EOK; |
128 | return EOK; |
129 | } |
129 | } |
130 | 130 | ||
131 | static void devcon_remove(devcon_t *devcon) |
131 | static void devcon_remove(devcon_t *devcon) |
132 | { |
132 | { |
133 | futex_down(&dcl_lock); |
133 | futex_down(&dcl_lock); |
134 | list_remove(&devcon->link); |
134 | list_remove(&devcon->link); |
135 | futex_up(&dcl_lock); |
135 | futex_up(&dcl_lock); |
136 | } |
136 | } |
137 | 137 | ||
138 | 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) |
139 | { |
139 | { |
140 | int rc; |
140 | int rc; |
141 | int dev_phone; |
141 | int dev_phone; |
142 | void *com_area; |
142 | void *com_area; |
143 | 143 | ||
144 | com_area = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE, |
144 | com_area = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE, |
145 | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); |
145 | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); |
146 | if (!com_area) { |
146 | if (!com_area) { |
147 | return ENOMEM; |
147 | return ENOMEM; |
148 | } |
148 | } |
149 | dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, |
149 | dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, |
150 | DEVMAP_CONNECT_TO_DEVICE, dev_handle); |
150 | DEVMAP_CONNECT_TO_DEVICE, dev_handle); |
151 | 151 | ||
152 | if (dev_phone < 0) { |
152 | if (dev_phone < 0) { |
153 | munmap(com_area, com_size); |
153 | munmap(com_area, com_size); |
154 | return dev_phone; |
154 | return dev_phone; |
155 | } |
155 | } |
156 | 156 | ||
157 | rc = ipc_share_out_start(dev_phone, com_area, |
157 | rc = ipc_share_out_start(dev_phone, com_area, |
158 | AS_AREA_READ | AS_AREA_WRITE); |
158 | AS_AREA_READ | AS_AREA_WRITE); |
159 | if (rc != EOK) { |
159 | if (rc != EOK) { |
160 | munmap(com_area, com_size); |
160 | munmap(com_area, com_size); |
161 | ipc_hangup(dev_phone); |
161 | ipc_hangup(dev_phone); |
162 | return rc; |
162 | return rc; |
163 | } |
163 | } |
164 | 164 | ||
165 | rc = devcon_add(dev_handle, dev_phone, com_area, com_size); |
165 | rc = devcon_add(dev_handle, dev_phone, com_area, com_size); |
166 | if (rc != EOK) { |
166 | if (rc != EOK) { |
167 | munmap(com_area, com_size); |
167 | munmap(com_area, com_size); |
168 | ipc_hangup(dev_phone); |
168 | ipc_hangup(dev_phone); |
169 | return rc; |
169 | return rc; |
170 | } |
170 | } |
171 | 171 | ||
172 | return EOK; |
172 | return EOK; |
173 | } |
173 | } |
174 | 174 | ||
175 | void block_fini(dev_handle_t dev_handle) |
175 | void block_fini(dev_handle_t dev_handle) |
176 | { |
176 | { |
177 | devcon_t *devcon = devcon_search(dev_handle); |
177 | devcon_t *devcon = devcon_search(dev_handle); |
178 | assert(devcon); |
178 | assert(devcon); |
179 | 179 | ||
180 | devcon_remove(devcon); |
180 | devcon_remove(devcon); |
181 | 181 | ||
182 | if (devcon->bb_buf) |
182 | if (devcon->bb_buf) |
183 | free(devcon->bb_buf); |
183 | free(devcon->bb_buf); |
184 | 184 | ||
185 | if (devcon->cache) { |
185 | if (devcon->cache) { |
186 | hash_table_destroy(&devcon->cache->block_hash); |
186 | hash_table_destroy(&devcon->cache->block_hash); |
187 | free(devcon->cache); |
187 | free(devcon->cache); |
188 | } |
188 | } |
189 | 189 | ||
190 | munmap(devcon->com_area, devcon->com_size); |
190 | munmap(devcon->com_area, devcon->com_size); |
191 | ipc_hangup(devcon->dev_phone); |
191 | ipc_hangup(devcon->dev_phone); |
192 | 192 | ||
193 | free(devcon); |
193 | free(devcon); |
194 | } |
194 | } |
195 | 195 | ||
196 | 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) |
197 | { |
197 | { |
198 | void *bb_buf; |
198 | void *bb_buf; |
199 | int rc; |
199 | int rc; |
200 | 200 | ||
201 | devcon_t *devcon = devcon_search(dev_handle); |
201 | devcon_t *devcon = devcon_search(dev_handle); |
202 | if (!devcon) |
202 | if (!devcon) |
203 | return ENOENT; |
203 | return ENOENT; |
204 | if (devcon->bb_buf) |
204 | if (devcon->bb_buf) |
205 | return EEXIST; |
205 | return EEXIST; |
206 | bb_buf = malloc(size); |
206 | bb_buf = malloc(size); |
207 | if (!bb_buf) |
207 | if (!bb_buf) |
208 | return ENOMEM; |
208 | return ENOMEM; |
209 | 209 | ||
210 | off_t bufpos = 0; |
210 | off_t bufpos = 0; |
211 | size_t buflen = 0; |
211 | size_t buflen = 0; |
212 | rc = block_read(dev_handle, &bufpos, &buflen, &off, |
212 | rc = block_read(dev_handle, &bufpos, &buflen, &off, |
213 | bb_buf, size, size); |
213 | bb_buf, size, size); |
214 | if (rc != EOK) { |
214 | if (rc != EOK) { |
215 | free(bb_buf); |
215 | free(bb_buf); |
216 | return rc; |
216 | return rc; |
217 | } |
217 | } |
218 | devcon->bb_buf = bb_buf; |
218 | devcon->bb_buf = bb_buf; |
219 | devcon->bb_off = off; |
219 | devcon->bb_off = off; |
220 | devcon->bb_size = size; |
220 | devcon->bb_size = size; |
221 | 221 | ||
222 | return EOK; |
222 | return EOK; |
223 | } |
223 | } |
224 | 224 | ||
225 | void *block_bb_get(dev_handle_t dev_handle) |
225 | void *block_bb_get(dev_handle_t dev_handle) |
226 | { |
226 | { |
227 | devcon_t *devcon = devcon_search(dev_handle); |
227 | devcon_t *devcon = devcon_search(dev_handle); |
228 | assert(devcon); |
228 | assert(devcon); |
229 | return devcon->bb_buf; |
229 | return devcon->bb_buf; |
230 | } |
230 | } |
231 | 231 | ||
232 | static hash_index_t cache_hash(unsigned long *key) |
232 | static hash_index_t cache_hash(unsigned long *key) |
233 | { |
233 | { |
234 | return *key & (CACHE_BUCKETS - 1); |
234 | return *key & (CACHE_BUCKETS - 1); |
235 | } |
235 | } |
236 | 236 | ||
237 | 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) |
238 | { |
238 | { |
239 | 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); |
240 | return b->boff == *key; |
240 | return b->boff == *key; |
241 | } |
241 | } |
242 | 242 | ||
243 | static void cache_remove_callback(link_t *item) |
243 | static void cache_remove_callback(link_t *item) |
244 | { |
244 | { |
245 | } |
245 | } |
246 | 246 | ||
247 | static hash_table_operations_t cache_ops = { |
247 | static hash_table_operations_t cache_ops = { |
248 | .hash = cache_hash, |
248 | .hash = cache_hash, |
249 | .compare = cache_compare, |
249 | .compare = cache_compare, |
250 | .remove_callback = cache_remove_callback |
250 | .remove_callback = cache_remove_callback |
251 | }; |
251 | }; |
252 | 252 | ||
253 | 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) |
254 | { |
254 | { |
255 | devcon_t *devcon = devcon_search(dev_handle); |
255 | devcon_t *devcon = devcon_search(dev_handle); |
256 | cache_t *cache; |
256 | cache_t *cache; |
257 | if (!devcon) |
257 | if (!devcon) |
258 | return ENOENT; |
258 | return ENOENT; |
259 | if (devcon->cache) |
259 | if (devcon->cache) |
260 | return EEXIST; |
260 | return EEXIST; |
261 | cache = malloc(sizeof(cache_t)); |
261 | cache = malloc(sizeof(cache_t)); |
262 | if (!cache) |
262 | if (!cache) |
263 | return ENOMEM; |
263 | return ENOMEM; |
264 | 264 | ||
265 | futex_initialize(&cache->lock, 1); |
265 | futex_initialize(&cache->lock, 1); |
266 | list_initialize(&cache->free_head); |
266 | list_initialize(&cache->free_head); |
267 | cache->block_size = size; |
267 | cache->block_size = size; |
268 | cache->block_count = blocks; |
268 | cache->block_count = blocks; |
269 | 269 | ||
270 | if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1, |
270 | if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1, |
271 | &cache_ops)) { |
271 | &cache_ops)) { |
272 | free(cache); |
272 | free(cache); |
273 | return ENOMEM; |
273 | return ENOMEM; |
274 | } |
274 | } |
275 | 275 | ||
276 | devcon->cache = cache; |
276 | devcon->cache = cache; |
277 | return EOK; |
277 | return EOK; |
278 | } |
278 | } |
279 | 279 | ||
280 | /** Read data from a block device. |
- | |
281 | * |
- | |
282 | * @param dev_handle Device handle of the block device. |
- | |
283 | * @param bufpos Pointer to the first unread valid offset within the |
- | |
284 | * communication buffer. |
- | |
285 | * @param buflen Pointer to the number of unread bytes that are ready in |
- | |
286 | * the communication buffer. |
- | |
287 | * @param pos Device position to be read. |
- | |
288 | * @param dst Destination buffer. |
- | |
289 | * @param size Size of the destination buffer. |
- | |
290 | * @param block_size Block size to be used for the transfer. |
- | |
291 | * |
- | |
292 | * @return EOK on success or a negative return code on failure. |
- | |
293 | */ |
- | |
294 | int |
- | |
295 | block_read(int dev_handle, off_t *bufpos, size_t *buflen, off_t *pos, void *dst, |
- | |
296 | size_t size, size_t block_size) |
- | |
297 | { |
- | |
298 | off_t offset = 0; |
- | |
299 | size_t left = size; |
- | |
300 | devcon_t *devcon = devcon_search(dev_handle); |
- | |
301 | assert(devcon); |
- | |
302 | - | ||
303 | while (left > 0) { |
- | |
304 | size_t rd; |
- | |
305 | - | ||
306 | if (*bufpos + left < *buflen) |
- | |
307 | rd = left; |
- | |
308 | else |
- | |
309 | rd = *buflen - *bufpos; |
- | |
310 | - | ||
311 | if (rd > 0) { |
- | |
312 | /* |
- | |
313 | * Copy the contents of the communication buffer to the |
- | |
314 | * destination buffer. |
- | |
315 | */ |
- | |
316 | memcpy(dst + offset, devcon->com_area + *bufpos, rd); |
- | |
317 | offset += rd; |
- | |
318 | *bufpos += rd; |
- | |
319 | *pos += rd; |
- | |
320 | left -= rd; |
- | |
321 | } |
- | |
322 | - | ||
323 | if (*bufpos == *buflen) { |
- | |
324 | /* Refill the communication buffer with a new block. */ |
- | |
325 | ipcarg_t retval; |
- | |
326 | int rc = async_req_2_1(devcon->dev_phone, RD_READ_BLOCK, |
- | |
327 | *pos / block_size, block_size, &retval); |
- | |
328 | if ((rc != EOK) || (retval != EOK)) |
- | |
329 | return (rc != EOK ? rc : retval); |
- | |
330 | - | ||
331 | *bufpos = 0; |
- | |
332 | *buflen = block_size; |
- | |
333 | } |
- | |
334 | } |
- | |
335 | - | ||
336 | return EOK; |
- | |
337 | } |
- | |
338 | - | ||
339 | static bool cache_can_grow(cache_t *cache) |
280 | static bool cache_can_grow(cache_t *cache) |
340 | { |
281 | { |
341 | return true; |
282 | return true; |
342 | } |
283 | } |
343 | 284 | ||
344 | static void block_initialize(block_t *b) |
285 | static void block_initialize(block_t *b) |
345 | { |
286 | { |
346 | futex_initialize(&b->lock, 1); |
287 | futex_initialize(&b->lock, 1); |
347 | b->refcnt = 1; |
288 | b->refcnt = 1; |
348 | b->dirty = false; |
289 | b->dirty = false; |
349 | rwlock_initialize(&b->contents_lock); |
290 | rwlock_initialize(&b->contents_lock); |
350 | link_initialize(&b->free_link); |
291 | link_initialize(&b->free_link); |
351 | link_initialize(&b->hash_link); |
292 | link_initialize(&b->hash_link); |
352 | } |
293 | } |
353 | 294 | ||
354 | /** Instantiate a block in memory and get a reference to it. |
295 | /** Instantiate a block in memory and get a reference to it. |
355 | * |
296 | * |
356 | * @param dev_handle Device handle of the block device. |
297 | * @param dev_handle Device handle of the block device. |
357 | * @param boff Block offset. |
298 | * @param boff Block offset. |
358 | * |
299 | * |
359 | * @return Block structure. |
300 | * @return Block structure. |
360 | */ |
301 | */ |
361 | block_t *block_get(dev_handle_t dev_handle, off_t boff) |
302 | block_t *block_get(dev_handle_t dev_handle, off_t boff) |
362 | { |
303 | { |
363 | devcon_t *devcon; |
304 | devcon_t *devcon; |
364 | cache_t *cache; |
305 | cache_t *cache; |
365 | block_t *b; |
306 | block_t *b; |
366 | link_t *l; |
307 | link_t *l; |
367 | unsigned long key = boff; |
308 | unsigned long key = boff; |
368 | 309 | ||
369 | devcon = devcon_search(dev_handle); |
310 | devcon = devcon_search(dev_handle); |
370 | 311 | ||
371 | assert(devcon); |
312 | assert(devcon); |
372 | assert(devcon->cache); |
313 | assert(devcon->cache); |
373 | 314 | ||
374 | cache = devcon->cache; |
315 | cache = devcon->cache; |
375 | futex_down(&cache->lock); |
316 | futex_down(&cache->lock); |
376 | l = hash_table_find(&cache->block_hash, &key); |
317 | l = hash_table_find(&cache->block_hash, &key); |
377 | if (l) { |
318 | if (l) { |
378 | /* |
319 | /* |
379 | * We found the block in the cache. |
320 | * We found the block in the cache. |
380 | */ |
321 | */ |
381 | b = hash_table_get_instance(l, block_t, hash_link); |
322 | b = hash_table_get_instance(l, block_t, hash_link); |
382 | futex_down(&b->lock); |
323 | futex_down(&b->lock); |
383 | if (b->refcnt++ == 0) |
324 | if (b->refcnt++ == 0) |
384 | list_remove(&b->free_link); |
325 | list_remove(&b->free_link); |
385 | futex_up(&b->lock); |
326 | futex_up(&b->lock); |
386 | } else { |
327 | } else { |
387 | /* |
328 | /* |
388 | * The block was not found in the cache. |
329 | * The block was not found in the cache. |
389 | */ |
330 | */ |
390 | int rc; |
331 | int rc; |
391 | off_t bufpos = 0; |
332 | off_t bufpos = 0; |
392 | size_t buflen = 0; |
333 | size_t buflen = 0; |
393 | off_t pos = boff * cache->block_size; |
334 | off_t pos = boff * cache->block_size; |
394 | 335 | ||
395 | if (cache_can_grow(cache)) { |
336 | if (cache_can_grow(cache)) { |
396 | /* |
337 | /* |
397 | * We can grow the cache by allocating new blocks. |
338 | * We can grow the cache by allocating new blocks. |
398 | * Should the allocation fail, we fail over and try to |
339 | * Should the allocation fail, we fail over and try to |
399 | * recycle a block from the cache. |
340 | * recycle a block from the cache. |
400 | */ |
341 | */ |
401 | b = malloc(sizeof(block_t)); |
342 | b = malloc(sizeof(block_t)); |
402 | if (!b) |
343 | if (!b) |
403 | goto recycle; |
344 | goto recycle; |
404 | b->data = malloc(cache->block_size); |
345 | b->data = malloc(cache->block_size); |
405 | if (!b->data) { |
346 | if (!b->data) { |
406 | free(b); |
347 | free(b); |
407 | goto recycle; |
348 | goto recycle; |
408 | } |
349 | } |
409 | } else { |
350 | } else { |
410 | /* |
351 | /* |
411 | * Try to recycle a block from the free list. |
352 | * Try to recycle a block from the free list. |
412 | */ |
353 | */ |
413 | unsigned long temp_key; |
354 | unsigned long temp_key; |
414 | recycle: |
355 | recycle: |
415 | assert(!list_empty(&cache->free_head)); |
356 | assert(!list_empty(&cache->free_head)); |
416 | l = cache->free_head.next; |
357 | l = cache->free_head.next; |
417 | list_remove(l); |
358 | list_remove(l); |
418 | b = hash_table_get_instance(l, block_t, hash_link); |
359 | b = hash_table_get_instance(l, block_t, hash_link); |
419 | assert(!b->dirty); |
360 | assert(!b->dirty); |
420 | temp_key = b->boff; |
361 | temp_key = b->boff; |
421 | hash_table_remove(&cache->block_hash, &temp_key, 1); |
362 | hash_table_remove(&cache->block_hash, &temp_key, 1); |
422 | } |
363 | } |
423 | 364 | ||
424 | block_initialize(b); |
365 | block_initialize(b); |
425 | b->dev_handle = dev_handle; |
366 | b->dev_handle = dev_handle; |
426 | b->size = cache->block_size; |
367 | b->size = cache->block_size; |
427 | b->boff = boff; |
368 | b->boff = boff; |
428 | /* read block from the device */ |
369 | /* read block from the device */ |
429 | rc = block_read(dev_handle, &bufpos, &buflen, &pos, b->data, |
370 | rc = block_read(dev_handle, &bufpos, &buflen, &pos, b->data, |
430 | cache->block_size, cache->block_size); |
371 | cache->block_size, cache->block_size); |
431 | assert(rc == EOK); |
372 | assert(rc == EOK); |
432 | hash_table_insert(&cache->block_hash, &key, &b->hash_link); |
373 | hash_table_insert(&cache->block_hash, &key, &b->hash_link); |
433 | } |
374 | } |
434 | 375 | ||
435 | futex_up(&cache->lock); |
376 | futex_up(&cache->lock); |
436 | return b; |
377 | return b; |
437 | } |
378 | } |
438 | 379 | ||
- | 380 | /** Release a reference to a block. |
|
- | 381 | * |
|
- | 382 | * If the last reference is dropped, the block is put on the free list. If the |
|
- | 383 | * last reference is dropped and the block is dirty, it is first synced with the |
|
- | 384 | * block device. |
|
- | 385 | * |
|
- | 386 | * @param block Block of which a reference is to be released. |
|
- | 387 | */ |
|
439 | void block_put(block_t *block) |
388 | void block_put(block_t *block) |
440 | { |
389 | { |
- | 390 | devcon_t *devcon = devcon_search(block->dev_handle); |
|
- | 391 | cache_t *cache; |
|
- | 392 | ||
- | 393 | assert(devcon); |
|
- | 394 | assert(devcon->cache); |
|
441 | 395 | ||
- | 396 | cache = devcon->cache; |
|
- | 397 | futex_down(&cache->lock); |
|
- | 398 | futex_down(&block->lock); |
|
- | 399 | if (!--block->refcnt) { |
|
- | 400 | /* |
|
- | 401 | * Last reference to the block was dropped, put the block on the |
|
- | 402 | * free list. |
|
- | 403 | */ |
|
- | 404 | list_append(&block->free_link, &cache->free_head); |
|
- | 405 | /* Unlock the cache, but not the block. */ |
|
- | 406 | futex_up(&cache->lock); |
|
- | 407 | if (block->dirty) { |
|
- | 408 | /* |
|
- | 409 | * The block is dirty and there is no one using it |
|
- | 410 | * at the moment, write it back to the device. |
|
- | 411 | */ |
|
- | 412 | ||
- | 413 | /* TODO: block_write() */ |
|
- | 414 | block->dirty = false; |
|
- | 415 | } |
|
- | 416 | } else { |
|
- | 417 | futex_up(&cache->lock); |
|
- | 418 | } |
|
- | 419 | futex_up(&block->lock); |
|
- | 420 | } |
|
- | 421 | ||
- | 422 | /** Read data from a block device. |
|
- | 423 | * |
|
- | 424 | * @param dev_handle Device handle of the block device. |
|
- | 425 | * @param bufpos Pointer to the first unread valid offset within the |
|
- | 426 | * communication buffer. |
|
- | 427 | * @param buflen Pointer to the number of unread bytes that are ready in |
|
- | 428 | * the communication buffer. |
|
- | 429 | * @param pos Device position to be read. |
|
- | 430 | * @param dst Destination buffer. |
|
- | 431 | * @param size Size of the destination buffer. |
|
- | 432 | * @param block_size Block size to be used for the transfer. |
|
- | 433 | * |
|
- | 434 | * @return EOK on success or a negative return code on failure. |
|
- | 435 | */ |
|
- | 436 | int |
|
- | 437 | block_read(int dev_handle, off_t *bufpos, size_t *buflen, off_t *pos, void *dst, |
|
- | 438 | size_t size, size_t block_size) |
|
- | 439 | { |
|
- | 440 | off_t offset = 0; |
|
- | 441 | size_t left = size; |
|
- | 442 | devcon_t *devcon = devcon_search(dev_handle); |
|
- | 443 | assert(devcon); |
|
- | 444 | ||
- | 445 | while (left > 0) { |
|
- | 446 | size_t rd; |
|
- | 447 | ||
- | 448 | if (*bufpos + left < *buflen) |
|
- | 449 | rd = left; |
|
- | 450 | else |
|
- | 451 | rd = *buflen - *bufpos; |
|
- | 452 | ||
- | 453 | if (rd > 0) { |
|
- | 454 | /* |
|
- | 455 | * Copy the contents of the communication buffer to the |
|
- | 456 | * destination buffer. |
|
- | 457 | */ |
|
- | 458 | memcpy(dst + offset, devcon->com_area + *bufpos, rd); |
|
- | 459 | offset += rd; |
|
- | 460 | *bufpos += rd; |
|
- | 461 | *pos += rd; |
|
- | 462 | left -= rd; |
|
- | 463 | } |
|
- | 464 | ||
- | 465 | if (*bufpos == *buflen) { |
|
- | 466 | /* Refill the communication buffer with a new block. */ |
|
- | 467 | ipcarg_t retval; |
|
- | 468 | int rc = async_req_2_1(devcon->dev_phone, RD_READ_BLOCK, |
|
- | 469 | *pos / block_size, block_size, &retval); |
|
- | 470 | if ((rc != EOK) || (retval != EOK)) |
|
- | 471 | return (rc != EOK ? rc : retval); |
|
- | 472 | ||
- | 473 | *bufpos = 0; |
|
- | 474 | *buflen = block_size; |
|
- | 475 | } |
|
- | 476 | } |
|
- | 477 | ||
- | 478 | return EOK; |
|
442 | } |
479 | } |
443 | 480 | ||
444 | /** @} |
481 | /** @} |
445 | */ |
482 | */ |
446 | 483 |