Rev 2958 | Rev 3091 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2958 | Rev 3087 | ||
---|---|---|---|
Line 48... | Line 48... | ||
48 | #include <assert.h> |
48 | #include <assert.h> |
49 | #include <sys/types.h> |
49 | #include <sys/types.h> |
50 | #include <libadt/hash_table.h> |
50 | #include <libadt/hash_table.h> |
51 | #include <as.h> |
51 | #include <as.h> |
52 | #include <libfs.h> |
52 | #include <libfs.h> |
- | 53 | #include <ipc/services.h> |
|
- | 54 | #include <ipc/devmap.h> |
|
- | 55 | #include <sys/mman.h> |
|
- | 56 | #include <byteorder.h> |
|
53 | 57 | ||
54 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
58 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
55 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
59 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
56 | 60 | ||
57 | #define DENTRIES_BUCKETS 256 |
61 | #define DENTRIES_BUCKETS 256 |
58 | 62 | ||
59 | #define NAMES_BUCKETS 4 |
63 | #define NAMES_BUCKETS 4 |
60 | 64 | ||
- | 65 | #define BLOCK_SIZE 1024 // FIXME |
|
- | 66 | #define RD_BASE 1024 // FIXME |
|
- | 67 | #define RD_READ_BLOCK (RD_BASE + 1) |
|
- | 68 | ||
61 | /* |
69 | /* |
62 | * For now, we don't distinguish between different dev_handles/instances. All |
70 | * For now, we don't distinguish between different dev_handles/instances. All |
63 | * requests resolve to the only instance, rooted in the following variable. |
71 | * requests resolve to the only instance, rooted in the following variable. |
64 | */ |
72 | */ |
65 | static tmpfs_dentry_t *root; |
73 | static tmpfs_dentry_t *root; |
Line 138... | Line 146... | ||
138 | }; |
146 | }; |
139 | 147 | ||
140 | /** Hash table of all directory entries. */ |
148 | /** Hash table of all directory entries. */ |
141 | hash_table_t dentries; |
149 | hash_table_t dentries; |
142 | 150 | ||
- | 151 | struct rdentry { |
|
- | 152 | uint8_t type; |
|
- | 153 | uint32_t len; |
|
- | 154 | } __attribute__((packed)); |
|
- | 155 | ||
143 | /* Implementation of hash table interface for the dentries hash table. */ |
156 | /* Implementation of hash table interface for the dentries hash table. */ |
144 | static hash_index_t dentries_hash(unsigned long *key) |
157 | static hash_index_t dentries_hash(unsigned long *key) |
145 | { |
158 | { |
146 | return *key % DENTRIES_BUCKETS; |
159 | return *key % DENTRIES_BUCKETS; |
147 | } |
160 | } |
Line 235... | Line 248... | ||
235 | } |
248 | } |
236 | root->lnkcnt = 1; |
249 | root->lnkcnt = 1; |
237 | return true; |
250 | return true; |
238 | } |
251 | } |
239 | 252 | ||
- | 253 | static bool tmpfs_blockread(int phone, void *buffer, size_t *bufpos, size_t *buflen, size_t *pos, void *dst, size_t size) |
|
- | 254 | { |
|
- | 255 | size_t offset = 0; |
|
- | 256 | size_t left = size; |
|
- | 257 | ||
- | 258 | while (left > 0) { |
|
- | 259 | size_t rd; |
|
- | 260 | ||
- | 261 | if (*bufpos + left < *buflen) |
|
- | 262 | rd = left; |
|
- | 263 | else |
|
- | 264 | rd = *buflen - *bufpos; |
|
- | 265 | ||
- | 266 | if (rd > 0) { |
|
- | 267 | memcpy(dst + offset, buffer + *bufpos, rd); |
|
- | 268 | offset += rd; |
|
- | 269 | *bufpos += rd; |
|
- | 270 | *pos += rd; |
|
- | 271 | left -= rd; |
|
- | 272 | } |
|
- | 273 | ||
- | 274 | if (*bufpos == *buflen) { |
|
- | 275 | int retval; |
|
- | 276 | int rc = ipc_call_sync_2_1(phone, RD_READ_BLOCK, *pos / BLOCK_SIZE, BLOCK_SIZE, (sysarg_t *) &retval); |
|
- | 277 | if ((rc != EOK) || (retval != EOK)) |
|
- | 278 | return false; |
|
- | 279 | ||
- | 280 | *bufpos = 0; |
|
- | 281 | *buflen = BLOCK_SIZE; |
|
- | 282 | } |
|
- | 283 | } |
|
- | 284 | ||
- | 285 | return true; |
|
- | 286 | } |
|
- | 287 | ||
- | 288 | static bool tmpfs_restore_recursion(int phone, void *block, size_t *bufpos, size_t *buflen, size_t *pos, tmpfs_dentry_t *parent) |
|
- | 289 | { |
|
- | 290 | struct rdentry entry; |
|
- | 291 | ||
- | 292 | do { |
|
- | 293 | char *fname; |
|
- | 294 | tmpfs_dentry_t *node; |
|
- | 295 | uint32_t size; |
|
- | 296 | ||
- | 297 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, &entry, sizeof(entry))) |
|
- | 298 | return false; |
|
- | 299 | ||
- | 300 | entry.len = uint32_t_le2host(entry.len); |
|
- | 301 | ||
- | 302 | switch (entry.type) { |
|
- | 303 | case 0: |
|
- | 304 | break; |
|
- | 305 | case 1: |
|
- | 306 | fname = malloc(entry.len + 1); |
|
- | 307 | if (fname == NULL) |
|
- | 308 | return false; |
|
- | 309 | ||
- | 310 | node = (tmpfs_dentry_t *) tmpfs_create_node(L_FILE); |
|
- | 311 | if (node == NULL) { |
|
- | 312 | free(fname); |
|
- | 313 | return false; |
|
- | 314 | } |
|
- | 315 | ||
- | 316 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, fname, entry.len)) { |
|
- | 317 | tmpfs_destroy_node((void *) node); |
|
- | 318 | free(fname); |
|
- | 319 | return false; |
|
- | 320 | } |
|
- | 321 | fname[entry.len] = 0; |
|
- | 322 | ||
- | 323 | if (!tmpfs_link_node((void *) parent, (void *) node, fname)) { |
|
- | 324 | tmpfs_destroy_node((void *) node); |
|
- | 325 | free(fname); |
|
- | 326 | return false; |
|
- | 327 | } |
|
- | 328 | free(fname); |
|
- | 329 | ||
- | 330 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, &size, sizeof(size))) |
|
- | 331 | return false; |
|
- | 332 | ||
- | 333 | size = uint32_t_le2host(size); |
|
- | 334 | ||
- | 335 | node->data = malloc(size); |
|
- | 336 | if (node->data == NULL) |
|
- | 337 | return false; |
|
- | 338 | ||
- | 339 | node->size = size; |
|
- | 340 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, node->data, size)) |
|
- | 341 | return false; |
|
- | 342 | ||
- | 343 | break; |
|
- | 344 | case 2: |
|
- | 345 | fname = malloc(entry.len + 1); |
|
- | 346 | if (fname == NULL) |
|
- | 347 | return false; |
|
- | 348 | ||
- | 349 | node = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY); |
|
- | 350 | if (node == NULL) { |
|
- | 351 | free(fname); |
|
- | 352 | return false; |
|
- | 353 | } |
|
- | 354 | ||
- | 355 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, fname, entry.len)) { |
|
- | 356 | tmpfs_destroy_node((void *) node); |
|
- | 357 | free(fname); |
|
- | 358 | return false; |
|
- | 359 | } |
|
- | 360 | fname[entry.len] = 0; |
|
- | 361 | ||
- | 362 | if (!tmpfs_link_node((void *) parent, (void *) node, fname)) { |
|
- | 363 | tmpfs_destroy_node((void *) node); |
|
- | 364 | free(fname); |
|
- | 365 | return false; |
|
- | 366 | } |
|
- | 367 | free(fname); |
|
- | 368 | ||
- | 369 | if (!tmpfs_restore_recursion(phone, block, bufpos, buflen, pos, node)) |
|
- | 370 | return false; |
|
- | 371 | ||
- | 372 | break; |
|
- | 373 | default: |
|
- | 374 | return false; |
|
- | 375 | } |
|
- | 376 | } while (entry.type != 0); |
|
- | 377 | ||
- | 378 | return true; |
|
- | 379 | } |
|
- | 380 | ||
- | 381 | static bool tmpfs_restore(dev_handle_t dev) |
|
- | 382 | { |
|
- | 383 | void *block = mmap(NULL, BLOCK_SIZE, |
|
- | 384 | PROTO_READ | PROTO_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); |
|
- | 385 | ||
- | 386 | if (block == NULL) |
|
- | 387 | return false; |
|
- | 388 | ||
- | 389 | int phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CONNECT_TO_DEVICE, dev); |
|
- | 390 | ||
- | 391 | if (phone < 0) { |
|
- | 392 | munmap(block, BLOCK_SIZE); |
|
- | 393 | return false; |
|
- | 394 | } |
|
- | 395 | ||
- | 396 | if (ipc_share_out_start(phone, block, AS_AREA_READ | AS_AREA_WRITE) != EOK) |
|
- | 397 | goto error; |
|
- | 398 | ||
- | 399 | size_t bufpos = 0; |
|
- | 400 | size_t buflen = 0; |
|
- | 401 | size_t pos = 0; |
|
- | 402 | ||
- | 403 | char tag[6]; |
|
- | 404 | if (!tmpfs_blockread(phone, block, &bufpos, &buflen, &pos, tag, 5)) |
|
- | 405 | goto error; |
|
- | 406 | ||
- | 407 | tag[5] = 0; |
|
- | 408 | if (strcmp(tag, "TMPFS") != 0) |
|
- | 409 | goto error; |
|
- | 410 | ||
- | 411 | if (!tmpfs_restore_recursion(phone, block, &bufpos, &buflen, &pos, root)) |
|
- | 412 | goto error; |
|
- | 413 | ||
- | 414 | ipc_hangup(phone); |
|
- | 415 | munmap(block, BLOCK_SIZE); |
|
- | 416 | return true; |
|
- | 417 | ||
- | 418 | error: |
|
- | 419 | ipc_hangup(phone); |
|
- | 420 | munmap(block, BLOCK_SIZE); |
|
- | 421 | return false; |
|
- | 422 | } |
|
- | 423 | ||
240 | /** Compare one component of path to a directory entry. |
424 | /** Compare one component of path to a directory entry. |
241 | * |
425 | * |
242 | * @param parentp Pointer to node from which we descended. |
426 | * @param parentp Pointer to node from which we descended. |
243 | * @param childp Pointer to node to compare the path component with. |
427 | * @param childp Pointer to node to compare the path component with. |
244 | * @param component Array of characters holding component name. |
428 | * @param component Array of characters holding component name. |
Line 393... | Line 577... | ||
393 | return EOK; |
577 | return EOK; |
394 | } |
578 | } |
395 | 579 | ||
396 | void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request) |
580 | void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request) |
397 | { |
581 | { |
398 | dev_handle_t mr_dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
582 | dev_handle_t mr_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
399 | fs_index_t mr_index = (fs_index_t)IPC_GET_ARG2(*request); |
583 | fs_index_t mr_index = (fs_index_t) IPC_GET_ARG2(*request); |
400 | fs_handle_t mp_fs_handle = (fs_handle_t)IPC_GET_ARG3(*request); |
584 | fs_handle_t mp_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request); |
401 | dev_handle_t mp_dev_handle = (dev_handle_t)IPC_GET_ARG4(*request); |
585 | dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG4(*request); |
402 | fs_index_t mp_index = (fs_index_t)IPC_GET_ARG5(*request); |
586 | fs_index_t mp_index = (fs_index_t) IPC_GET_ARG5(*request); |
- | 587 | ||
403 | if ((mr_index == root->index) && |
588 | if ((mr_index == root->index) && |
404 | (mp_fs_handle == tmpfs_reg.fs_handle) && |
589 | (mp_fs_handle == tmpfs_reg.fs_handle) && |
405 | (mp_index == mr_index)) |
590 | (mp_index == mr_index)) { |
- | 591 | ||
- | 592 | if (mr_dev_handle >= 0) { |
|
- | 593 | if (tmpfs_restore(mr_dev_handle)) |
|
- | 594 | ipc_answer_0(rid, EOK); |
|
- | 595 | else |
|
- | 596 | ipc_answer_0(rid, ELIMIT); |
|
- | 597 | } else |
|
406 | ipc_answer_0(rid, EOK); |
598 | ipc_answer_0(rid, EOK); |
407 | else |
599 | } else |
408 | ipc_answer_0(rid, ENOTSUP); |
600 | ipc_answer_0(rid, ENOTSUP); |
409 | } |
601 | } |
410 | 602 | ||
411 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request) |
603 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request) |
412 | { |
604 | { |