Rev 3535 | Rev 3598 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3535 | Rev 3561 | ||
---|---|---|---|
Line 46... | Line 46... | ||
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; |
Line 80... | Line 93... | ||
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, void *bb_buf, off_t bb_off, size_t bb_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)); |
Line 94... | Line 107... | ||
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 = bb_buf; |
112 | devcon->bb_buf = NULL; |
100 | devcon->bb_off = bb_off; |
113 | devcon->bb_off = 0; |
101 | devcon->bb_size = bb_size; |
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) { |
Line 119... | Line 133... | ||
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 |
- | |
125 | block_init(dev_handle_t dev_handle, size_t com_size, off_t bb_off, |
138 | int block_init(dev_handle_t dev_handle, size_t com_size) |
126 | size_t bb_size) |
- | |
127 | { |
139 | { |
128 | int rc; |
140 | int rc; |
129 | int dev_phone; |
141 | int dev_phone; |
130 | void *com_area; |
142 | void *com_area; |
131 | void *bb_buf; |
- | |
132 | - | ||
133 | bb_buf = malloc(bb_size); |
- | |
134 | if (!bb_buf) |
- | |
135 | return ENOMEM; |
- | |
136 | 143 | ||
137 | com_area = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE, |
144 | com_area = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE, |
138 | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); |
145 | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); |
139 | if (!com_area) { |
146 | if (!com_area) { |
140 | free(bb_buf); |
- | |
141 | return ENOMEM; |
147 | return ENOMEM; |
142 | } |
148 | } |
143 | dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, |
149 | dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, |
144 | DEVMAP_CONNECT_TO_DEVICE, dev_handle); |
150 | DEVMAP_CONNECT_TO_DEVICE, dev_handle); |
145 | 151 | ||
146 | if (dev_phone < 0) { |
152 | if (dev_phone < 0) { |
147 | free(bb_buf); |
- | |
148 | munmap(com_area, com_size); |
153 | munmap(com_area, com_size); |
149 | return dev_phone; |
154 | return dev_phone; |
150 | } |
155 | } |
151 | 156 | ||
152 | rc = ipc_share_out_start(dev_phone, com_area, |
157 | rc = ipc_share_out_start(dev_phone, com_area, |
153 | AS_AREA_READ | AS_AREA_WRITE); |
158 | AS_AREA_READ | AS_AREA_WRITE); |
154 | if (rc != EOK) { |
159 | if (rc != EOK) { |
155 | free(bb_buf); |
- | |
156 | munmap(com_area, com_size); |
160 | munmap(com_area, com_size); |
157 | ipc_hangup(dev_phone); |
161 | ipc_hangup(dev_phone); |
158 | return rc; |
162 | return rc; |
159 | } |
163 | } |
160 | 164 | ||
161 | rc = devcon_add(dev_handle, dev_phone, com_area, com_size, bb_buf, |
165 | rc = devcon_add(dev_handle, dev_phone, com_area, com_size); |
162 | bb_off, bb_size); |
- | |
163 | if (rc != EOK) { |
166 | if (rc != EOK) { |
164 | free(bb_buf); |
- | |
165 | munmap(com_area, com_size); |
167 | munmap(com_area, com_size); |
166 | ipc_hangup(dev_phone); |
168 | ipc_hangup(dev_phone); |
167 | return rc; |
169 | return rc; |
168 | } |
170 | } |
169 | 171 | ||
170 | off_t bufpos = 0; |
- | |
171 | size_t buflen = 0; |
- | |
172 | if (!block_read(dev_handle, &bufpos, &buflen, &bb_off, |
- | |
173 | bb_buf, bb_size, bb_size)) { |
- | |
174 | block_fini(dev_handle); |
- | |
175 | return EIO; /* XXX real error code */ |
- | |
176 | } |
- | |
177 | - | ||
178 | return EOK; |
172 | return EOK; |
179 | } |
173 | } |
180 | 174 | ||
181 | void block_fini(dev_handle_t dev_handle) |
175 | void block_fini(dev_handle_t dev_handle) |
182 | { |
176 | { |
183 | devcon_t *devcon = devcon_search(dev_handle); |
177 | devcon_t *devcon = devcon_search(dev_handle); |
184 | assert(devcon); |
178 | assert(devcon); |
185 | 179 | ||
186 | devcon_remove(devcon); |
180 | devcon_remove(devcon); |
187 | 181 | ||
- | 182 | if (devcon->bb_buf) |
|
188 | 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 | ||
189 | munmap(devcon->com_area, devcon->com_size); |
190 | munmap(devcon->com_area, devcon->com_size); |
190 | ipc_hangup(devcon->dev_phone); |
191 | ipc_hangup(devcon->dev_phone); |
191 | 192 | ||
192 | free(devcon); |
193 | free(devcon); |
193 | } |
194 | } |
194 | 195 | ||
- | 196 | int block_bb_read(dev_handle_t dev_handle, off_t off, size_t size) |
|
- | 197 | { |
|
- | 198 | void *bb_buf; |
|
- | 199 | int rc; |
|
- | 200 | ||
- | 201 | devcon_t *devcon = devcon_search(dev_handle); |
|
- | 202 | if (!devcon) |
|
- | 203 | return ENOENT; |
|
- | 204 | if (devcon->bb_buf) |
|
- | 205 | return EEXIST; |
|
- | 206 | bb_buf = malloc(size); |
|
- | 207 | if (!bb_buf) |
|
- | 208 | return ENOMEM; |
|
- | 209 | ||
- | 210 | off_t bufpos = 0; |
|
- | 211 | size_t buflen = 0; |
|
- | 212 | rc = block_read(dev_handle, &bufpos, &buflen, &off, |
|
- | 213 | bb_buf, size, size); |
|
- | 214 | if (rc != EOK) { |
|
- | 215 | free(bb_buf); |
|
- | 216 | return rc; |
|
- | 217 | } |
|
- | 218 | devcon->bb_buf = bb_buf; |
|
- | 219 | devcon->bb_off = off; |
|
- | 220 | devcon->bb_size = size; |
|
- | 221 | ||
- | 222 | return EOK; |
|
- | 223 | } |
|
- | 224 | ||
195 | void *block_bb_get(dev_handle_t dev_handle) |
225 | void *block_bb_get(dev_handle_t dev_handle) |
196 | { |
226 | { |
197 | devcon_t *devcon = devcon_search(dev_handle); |
227 | devcon_t *devcon = devcon_search(dev_handle); |
198 | assert(devcon); |
228 | assert(devcon); |
199 | return devcon->bb_buf; |
229 | return devcon->bb_buf; |
200 | } |
230 | } |
201 | 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, 1); |
|
- | 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 | } |
|
- | 279 | ||
- | 280 | static bool cache_can_grow(cache_t *cache) |
|
- | 281 | { |
|
- | 282 | return true; |
|
- | 283 | } |
|
- | 284 | ||
- | 285 | static void block_initialize(block_t *b) |
|
- | 286 | { |
|
- | 287 | futex_initialize(&b->lock, 1); |
|
- | 288 | b->refcnt = 1; |
|
- | 289 | b->dirty = false; |
|
- | 290 | rwlock_initialize(&b->contents_lock); |
|
- | 291 | link_initialize(&b->free_link); |
|
- | 292 | link_initialize(&b->hash_link); |
|
- | 293 | } |
|
- | 294 | ||
- | 295 | /** Instantiate a block in memory and get a reference to it. |
|
- | 296 | * |
|
- | 297 | * @param dev_handle Device handle of the block device. |
|
- | 298 | * @param boff Block offset. |
|
- | 299 | * |
|
- | 300 | * @return Block structure. |
|
- | 301 | */ |
|
- | 302 | block_t *block_get(dev_handle_t dev_handle, off_t boff) |
|
- | 303 | { |
|
- | 304 | devcon_t *devcon; |
|
- | 305 | cache_t *cache; |
|
- | 306 | block_t *b; |
|
- | 307 | link_t *l; |
|
- | 308 | unsigned long key = boff; |
|
- | 309 | ||
- | 310 | devcon = devcon_search(dev_handle); |
|
- | 311 | ||
- | 312 | assert(devcon); |
|
- | 313 | assert(devcon->cache); |
|
- | 314 | ||
- | 315 | cache = devcon->cache; |
|
- | 316 | futex_down(&cache->lock); |
|
- | 317 | l = hash_table_find(&cache->block_hash, &key); |
|
- | 318 | if (l) { |
|
- | 319 | /* |
|
- | 320 | * We found the block in the cache. |
|
- | 321 | */ |
|
- | 322 | b = hash_table_get_instance(l, block_t, hash_link); |
|
- | 323 | futex_down(&b->lock); |
|
- | 324 | if (b->refcnt++ == 0) |
|
- | 325 | list_remove(&b->free_link); |
|
- | 326 | futex_up(&b->lock); |
|
- | 327 | futex_up(&cache->lock); |
|
- | 328 | } else { |
|
- | 329 | /* |
|
- | 330 | * The block was not found in the cache. |
|
- | 331 | */ |
|
- | 332 | int rc; |
|
- | 333 | off_t bufpos = 0; |
|
- | 334 | size_t buflen = 0; |
|
- | 335 | off_t pos = boff * cache->block_size; |
|
- | 336 | bool sync = false; |
|
- | 337 | ||
- | 338 | if (cache_can_grow(cache)) { |
|
- | 339 | /* |
|
- | 340 | * We can grow the cache by allocating new blocks. |
|
- | 341 | * Should the allocation fail, we fail over and try to |
|
- | 342 | * recycle a block from the cache. |
|
- | 343 | */ |
|
- | 344 | b = malloc(sizeof(block_t)); |
|
- | 345 | if (!b) |
|
- | 346 | goto recycle; |
|
- | 347 | b->data = malloc(cache->block_size); |
|
- | 348 | if (!b->data) { |
|
- | 349 | free(b); |
|
- | 350 | goto recycle; |
|
- | 351 | } |
|
- | 352 | } else { |
|
- | 353 | /* |
|
- | 354 | * Try to recycle a block from the free list. |
|
- | 355 | */ |
|
- | 356 | unsigned long temp_key; |
|
- | 357 | recycle: |
|
- | 358 | assert(!list_empty(&cache->free_head)); |
|
- | 359 | l = cache->free_head.next; |
|
- | 360 | list_remove(l); |
|
- | 361 | b = hash_table_get_instance(l, block_t, hash_link); |
|
- | 362 | sync = b->dirty; |
|
- | 363 | temp_key = b->boff; |
|
- | 364 | hash_table_remove(&cache->block_hash, &temp_key, 1); |
|
- | 365 | } |
|
- | 366 | ||
- | 367 | block_initialize(b); |
|
- | 368 | b->dev_handle = dev_handle; |
|
- | 369 | b->size = cache->block_size; |
|
- | 370 | b->boff = boff; |
|
- | 371 | hash_table_insert(&cache->block_hash, &key, &b->hash_link); |
|
- | 372 | ||
- | 373 | /* |
|
- | 374 | * Lock the block before releasing the cache lock. Thus we don't |
|
- | 375 | * kill concurent operations on the cache while doing I/O on the |
|
- | 376 | * block. |
|
- | 377 | */ |
|
- | 378 | futex_down(&b->lock); |
|
- | 379 | futex_up(&cache->lock); |
|
- | 380 | ||
- | 381 | if (sync) { |
|
- | 382 | /* |
|
- | 383 | * The block is dirty and needs to be written back to |
|
- | 384 | * the device before we can read in the new contents. |
|
- | 385 | */ |
|
- | 386 | abort(); /* TODO: block_write() */ |
|
- | 387 | } |
|
- | 388 | /* |
|
- | 389 | * The block contains old or no data. We need to read the new |
|
- | 390 | * contents from the device. |
|
- | 391 | */ |
|
- | 392 | rc = block_read(dev_handle, &bufpos, &buflen, &pos, b->data, |
|
- | 393 | cache->block_size, cache->block_size); |
|
- | 394 | assert(rc == EOK); |
|
- | 395 | ||
- | 396 | futex_up(&b->lock); |
|
- | 397 | } |
|
- | 398 | return b; |
|
- | 399 | } |
|
- | 400 | ||
- | 401 | /** Release a reference to a block. |
|
- | 402 | * |
|
- | 403 | * If the last reference is dropped, the block is put on the free list. |
|
- | 404 | * |
|
- | 405 | * @param block Block of which a reference is to be released. |
|
- | 406 | */ |
|
- | 407 | void block_put(block_t *block) |
|
- | 408 | { |
|
- | 409 | devcon_t *devcon = devcon_search(block->dev_handle); |
|
- | 410 | cache_t *cache; |
|
- | 411 | ||
- | 412 | assert(devcon); |
|
- | 413 | assert(devcon->cache); |
|
- | 414 | ||
- | 415 | cache = devcon->cache; |
|
- | 416 | futex_down(&cache->lock); |
|
- | 417 | futex_down(&block->lock); |
|
- | 418 | if (!--block->refcnt) { |
|
- | 419 | /* |
|
- | 420 | * Last reference to the block was dropped, put the block on the |
|
- | 421 | * free list. |
|
- | 422 | */ |
|
- | 423 | list_append(&block->free_link, &cache->free_head); |
|
- | 424 | } |
|
- | 425 | futex_up(&block->lock); |
|
- | 426 | futex_up(&cache->lock); |
|
- | 427 | } |
|
- | 428 | ||
202 | /** Read data from a block device. |
429 | /** Read data from a block device. |
203 | * |
430 | * |
204 | * @param dev_handle Device handle of the block device. |
431 | * @param dev_handle Device handle of the block device. |
205 | * @param bufpos Pointer to the first unread valid offset within the |
432 | * @param bufpos Pointer to the first unread valid offset within the |
206 | * communication buffer. |
433 | * communication buffer. |
Line 209... | Line 436... | ||
209 | * @param pos Device position to be read. |
436 | * @param pos Device position to be read. |
210 | * @param dst Destination buffer. |
437 | * @param dst Destination buffer. |
211 | * @param size Size of the destination buffer. |
438 | * @param size Size of the destination buffer. |
212 | * @param block_size Block size to be used for the transfer. |
439 | * @param block_size Block size to be used for the transfer. |
213 | * |
440 | * |
214 | * @return True on success, false on failure. |
441 | * @return EOK on success or a negative return code on failure. |
215 | */ |
442 | */ |
216 | bool |
443 | int |
217 | block_read(int dev_handle, off_t *bufpos, size_t *buflen, off_t *pos, void *dst, |
444 | block_read(int dev_handle, off_t *bufpos, size_t *buflen, off_t *pos, void *dst, |
218 | size_t size, size_t block_size) |
445 | size_t size, size_t block_size) |
219 | { |
446 | { |
220 | off_t offset = 0; |
447 | off_t offset = 0; |
221 | size_t left = size; |
448 | size_t left = size; |
Line 246... | Line 473... | ||
246 | /* Refill the communication buffer with a new block. */ |
473 | /* Refill the communication buffer with a new block. */ |
247 | ipcarg_t retval; |
474 | ipcarg_t retval; |
248 | int rc = async_req_2_1(devcon->dev_phone, RD_READ_BLOCK, |
475 | int rc = async_req_2_1(devcon->dev_phone, RD_READ_BLOCK, |
249 | *pos / block_size, block_size, &retval); |
476 | *pos / block_size, block_size, &retval); |
250 | if ((rc != EOK) || (retval != EOK)) |
477 | if ((rc != EOK) || (retval != EOK)) |
251 | return false; |
478 | return (rc != EOK ? rc : retval); |
252 | 479 | ||
253 | *bufpos = 0; |
480 | *bufpos = 0; |
254 | *buflen = block_size; |
481 | *buflen = block_size; |
255 | } |
482 | } |
256 | } |
483 | } |
257 | 484 | ||
258 | return true; |
- | |
259 | } |
- | |
260 | - | ||
261 | block_t *block_get(dev_handle_t dev_handle, off_t offset, size_t bs) |
- | |
262 | { |
- | |
263 | /* FIXME */ |
- | |
264 | block_t *b; |
- | |
265 | off_t bufpos = 0; |
- | |
266 | size_t buflen = 0; |
- | |
267 | off_t pos = offset * bs; |
- | |
268 | - | ||
269 | b = malloc(sizeof(block_t)); |
- | |
270 | if (!b) |
- | |
271 | return NULL; |
- | |
272 | - | ||
273 | b->data = malloc(bs); |
- | |
274 | if (!b->data) { |
- | |
275 | free(b); |
- | |
276 | return NULL; |
- | |
277 | } |
- | |
278 | b->size = bs; |
- | |
279 | - | ||
280 | if (!block_read(dev_handle, &bufpos, &buflen, &pos, b->data, |
- | |
281 | bs, bs)) { |
- | |
282 | free(b->data); |
- | |
283 | free(b); |
- | |
284 | return NULL; |
- | |
285 | } |
- | |
286 | - | ||
287 | return b; |
485 | return EOK; |
288 | } |
- | |
289 | - | ||
290 | void block_put(block_t *block) |
- | |
291 | { |
- | |
292 | /* FIXME */ |
- | |
293 | free(block->data); |
- | |
294 | free(block); |
- | |
295 | } |
486 | } |
296 | 487 | ||
297 | /** @} |
488 | /** @} |
298 | */ |
489 | */ |