Changeset ad7a6c9 in mainline for uspace/lib/c/generic/vfs/vfs.c
- Timestamp:
- 2011-03-30T13:10:24Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 4ae90f9
- Parents:
- 6e50466 (diff), d6b81941 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/vfs/vfs.c
r6e50466 rad7a6c9 1 1 /* 2 * Copyright (c) 2008 Jakub Jermar 2 * Copyright (c) 2008 Jakub Jermar 3 3 * All rights reserved. 4 4 * … … 43 43 #include <sys/stat.h> 44 44 #include <sys/types.h> 45 #include <ipc/ipc.h>46 45 #include <ipc/services.h> 46 #include <ipc/ns.h> 47 47 #include <async.h> 48 #include <atomic.h> 49 #include <futex.h> 48 #include <fibril_synch.h> 50 49 #include <errno.h> 50 #include <assert.h> 51 51 #include <str.h> 52 52 #include <devmap.h> … … 54 54 #include <ipc/devmap.h> 55 55 56 static async_sess_t vfs_session; 57 58 static FIBRIL_MUTEX_INITIALIZE(vfs_phone_mutex); 56 59 static int vfs_phone = -1; 57 static futex_t vfs_phone_futex = FUTEX_INITIALIZER; 58 static futex_t cwd_futex = FUTEX_INITIALIZER;60 61 static FIBRIL_MUTEX_INITIALIZE(cwd_mutex); 59 62 60 63 static int cwd_fd = -1; … … 66 69 char *ncwd_path; 67 70 char *ncwd_path_nc; 68 69 futex_down(&cwd_futex); 71 size_t total_size; 72 73 fibril_mutex_lock(&cwd_mutex); 70 74 size_t size = str_size(path); 71 75 if (*path != '/') { 72 76 if (!cwd_path) { 73 f utex_up(&cwd_futex);77 fibril_mutex_unlock(&cwd_mutex); 74 78 return NULL; 75 79 } 76 ncwd_path_nc = malloc(cwd_size + 1 + size + 1); 80 total_size = cwd_size + 1 + size + 1; 81 ncwd_path_nc = malloc(total_size); 77 82 if (!ncwd_path_nc) { 78 f utex_up(&cwd_futex);83 fibril_mutex_unlock(&cwd_mutex); 79 84 return NULL; 80 85 } 81 str_cpy(ncwd_path_nc, cwd_size + 1 + size + 1, cwd_path);86 str_cpy(ncwd_path_nc, total_size, cwd_path); 82 87 ncwd_path_nc[cwd_size] = '/'; 83 88 ncwd_path_nc[cwd_size + 1] = '\0'; 84 89 } else { 85 ncwd_path_nc = malloc(size + 1); 90 total_size = size + 1; 91 ncwd_path_nc = malloc(total_size); 86 92 if (!ncwd_path_nc) { 87 f utex_up(&cwd_futex);93 fibril_mutex_unlock(&cwd_mutex); 88 94 return NULL; 89 95 } 90 96 ncwd_path_nc[0] = '\0'; 91 97 } 92 str_append(ncwd_path_nc, cwd_size + 1 + size + 1, path);98 str_append(ncwd_path_nc, total_size, path); 93 99 ncwd_path = canonify(ncwd_path_nc, retlen); 94 100 if (!ncwd_path) { 95 f utex_up(&cwd_futex);101 fibril_mutex_unlock(&cwd_mutex); 96 102 free(ncwd_path_nc); 97 103 return NULL; … … 105 111 free(ncwd_path_nc); 106 112 if (!ncwd_path) { 107 f utex_up(&cwd_futex);113 fibril_mutex_unlock(&cwd_mutex); 108 114 return NULL; 109 115 } 110 f utex_up(&cwd_futex);116 fibril_mutex_unlock(&cwd_mutex); 111 117 return ncwd_path; 112 118 } 113 119 120 /** Connect to VFS service and create session. */ 114 121 static void vfs_connect(void) 115 122 { 116 123 while (vfs_phone < 0) 117 vfs_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VFS, 0, 0); 124 vfs_phone = service_connect_blocking(SERVICE_VFS, 0, 0); 125 126 async_session_create(&vfs_session, vfs_phone, 0); 127 } 128 129 /** Start an async exchange on the VFS session. 130 * 131 * @return New phone to be used during the exchange. 132 */ 133 static int vfs_exchange_begin(void) 134 { 135 fibril_mutex_lock(&vfs_phone_mutex); 136 if (vfs_phone < 0) 137 vfs_connect(); 138 fibril_mutex_unlock(&vfs_phone_mutex); 139 140 return async_exchange_begin(&vfs_session); 141 } 142 143 /** End an async exchange on the VFS session. 144 * 145 * @param phone Phone used during the exchange. 146 */ 147 static void vfs_exchange_end(int phone) 148 { 149 async_exchange_end(&vfs_session, phone); 118 150 } 119 151 … … 154 186 } 155 187 156 futex_down(&vfs_phone_futex); 157 async_serialize_start(); 158 vfs_connect(); 159 188 int vfs_phone = vfs_exchange_begin(); 189 160 190 sysarg_t rc_orig; 161 191 aid_t req = async_send_2(vfs_phone, VFS_IN_MOUNT, devmap_handle, flags, NULL); 162 192 sysarg_t rc = async_data_write_start(vfs_phone, (void *) mpa, mpa_size); 163 193 if (rc != EOK) { 164 async_wait_for(req, &rc_orig); 165 async_serialize_end(); 166 futex_up(&vfs_phone_futex); 194 vfs_exchange_end(vfs_phone); 167 195 free(mpa); 196 async_wait_for(req, &rc_orig); 168 197 169 198 if (null_id != -1) … … 178 207 rc = async_data_write_start(vfs_phone, (void *) opts, str_size(opts)); 179 208 if (rc != EOK) { 180 async_wait_for(req, &rc_orig); 181 async_serialize_end(); 182 futex_up(&vfs_phone_futex); 209 vfs_exchange_end(vfs_phone); 183 210 free(mpa); 211 async_wait_for(req, &rc_orig); 184 212 185 213 if (null_id != -1) … … 194 222 rc = async_data_write_start(vfs_phone, (void *) fs_name, str_size(fs_name)); 195 223 if (rc != EOK) { 196 async_wait_for(req, &rc_orig); 197 async_serialize_end(); 198 futex_up(&vfs_phone_futex); 224 vfs_exchange_end(vfs_phone); 199 225 free(mpa); 226 async_wait_for(req, &rc_orig); 200 227 201 228 if (null_id != -1) … … 211 238 rc = async_req_0_0(vfs_phone, IPC_M_PING); 212 239 if (rc != EOK) { 213 async_wait_for(req, &rc_orig); 214 async_serialize_end(); 215 futex_up(&vfs_phone_futex); 240 vfs_exchange_end(vfs_phone); 216 241 free(mpa); 242 async_wait_for(req, &rc_orig); 217 243 218 244 if (null_id != -1) … … 225 251 } 226 252 227 async_wait_for(req, &rc); 228 async_serialize_end(); 229 futex_up(&vfs_phone_futex); 253 vfs_exchange_end(vfs_phone); 230 254 free(mpa); 255 async_wait_for(req, &rc); 231 256 232 257 if ((rc != EOK) && (null_id != -1)) … … 248 273 return ENOMEM; 249 274 250 futex_down(&vfs_phone_futex); 251 async_serialize_start(); 252 vfs_connect(); 275 int vfs_phone = vfs_exchange_begin(); 253 276 254 277 req = async_send_0(vfs_phone, VFS_IN_UNMOUNT, NULL); 255 278 rc = async_data_write_start(vfs_phone, (void *) mpa, mpa_size); 256 279 if (rc != EOK) { 257 async_wait_for(req, &rc_orig); 258 async_serialize_end(); 259 futex_up(&vfs_phone_futex); 280 vfs_exchange_end(vfs_phone); 260 281 free(mpa); 261 if (rc_orig == EOK) 262 return (int) rc; 263 else 264 return (int) rc_orig; 265 } 266 267 268 async_wait_for(req, &rc); 269 async_serialize_end(); 270 futex_up(&vfs_phone_futex); 282 async_wait_for(req, &rc_orig); 283 if (rc_orig == EOK) 284 return (int) rc; 285 else 286 return (int) rc_orig; 287 } 288 289 290 vfs_exchange_end(vfs_phone); 271 291 free(mpa); 292 async_wait_for(req, &rc); 272 293 273 294 return (int) rc; … … 276 297 static int open_internal(const char *abs, size_t abs_size, int lflag, int oflag) 277 298 { 278 futex_down(&vfs_phone_futex); 279 async_serialize_start(); 280 vfs_connect(); 299 int vfs_phone = vfs_exchange_begin(); 281 300 282 301 ipc_call_t answer; … … 285 304 286 305 if (rc != EOK) { 306 vfs_exchange_end(vfs_phone); 307 287 308 sysarg_t rc_orig; 288 309 async_wait_for(req, &rc_orig); 289 310 290 async_serialize_end(); 291 futex_up(&vfs_phone_futex); 292 293 if (rc_orig == EOK) 294 return (int) rc; 295 else 296 return (int) rc_orig; 297 } 298 299 async_wait_for(req, &rc); 300 async_serialize_end(); 301 futex_up(&vfs_phone_futex); 311 if (rc_orig == EOK) 312 return (int) rc; 313 else 314 return (int) rc_orig; 315 } 316 317 vfs_exchange_end(vfs_phone); 318 async_wait_for(req, &rc); 302 319 303 320 if (rc != EOK) … … 322 339 int open_node(fdi_node_t *node, int oflag) 323 340 { 324 futex_down(&vfs_phone_futex); 325 async_serialize_start(); 326 vfs_connect(); 341 int vfs_phone = vfs_exchange_begin(); 327 342 328 343 ipc_call_t answer; … … 330 345 node->devmap_handle, node->index, oflag, &answer); 331 346 332 sysarg_t rc;333 async_wait_for(req, &rc); 334 async_serialize_end();335 futex_up(&vfs_phone_futex);347 vfs_exchange_end(vfs_phone); 348 349 sysarg_t rc; 350 async_wait_for(req, &rc); 336 351 337 352 if (rc != EOK) … … 345 360 sysarg_t rc; 346 361 347 futex_down(&vfs_phone_futex); 348 async_serialize_start(); 349 vfs_connect(); 362 int vfs_phone = vfs_exchange_begin(); 350 363 351 364 rc = async_req_1_0(vfs_phone, VFS_IN_CLOSE, fildes); 352 365 353 async_serialize_end(); 354 futex_up(&vfs_phone_futex); 366 vfs_exchange_end(vfs_phone); 355 367 356 368 return (int)rc; … … 363 375 aid_t req; 364 376 365 futex_down(&vfs_phone_futex); 366 async_serialize_start(); 367 vfs_connect(); 377 int vfs_phone = vfs_exchange_begin(); 368 378 369 379 req = async_send_1(vfs_phone, VFS_IN_READ, fildes, &answer); 370 380 rc = async_data_read_start(vfs_phone, (void *)buf, nbyte); 371 381 if (rc != EOK) { 382 vfs_exchange_end(vfs_phone); 383 372 384 sysarg_t rc_orig; 373 374 async_wait_for(req, &rc_orig); 375 async_serialize_end(); 376 futex_up(&vfs_phone_futex); 385 async_wait_for(req, &rc_orig); 386 377 387 if (rc_orig == EOK) 378 388 return (ssize_t) rc; … … 380 390 return (ssize_t) rc_orig; 381 391 } 382 async_wait_for(req, &rc); 383 async_serialize_end(); 384 futex_up(&vfs_phone_futex); 392 vfs_exchange_end(vfs_phone); 393 async_wait_for(req, &rc); 385 394 if (rc == EOK) 386 395 return (ssize_t) IPC_GET_ARG1(answer); … … 395 404 aid_t req; 396 405 397 futex_down(&vfs_phone_futex); 398 async_serialize_start(); 399 vfs_connect(); 406 int vfs_phone = vfs_exchange_begin(); 400 407 401 408 req = async_send_1(vfs_phone, VFS_IN_WRITE, fildes, &answer); 402 409 rc = async_data_write_start(vfs_phone, (void *)buf, nbyte); 403 410 if (rc != EOK) { 411 vfs_exchange_end(vfs_phone); 412 404 413 sysarg_t rc_orig; 405 406 async_wait_for(req, &rc_orig); 407 async_serialize_end(); 408 futex_up(&vfs_phone_futex); 414 async_wait_for(req, &rc_orig); 415 409 416 if (rc_orig == EOK) 410 417 return (ssize_t) rc; … … 412 419 return (ssize_t) rc_orig; 413 420 } 414 async_wait_for(req, &rc); 415 async_serialize_end(); 416 futex_up(&vfs_phone_futex); 421 vfs_exchange_end(vfs_phone); 422 async_wait_for(req, &rc); 417 423 if (rc == EOK) 418 424 return (ssize_t) IPC_GET_ARG1(answer); … … 423 429 int fsync(int fildes) 424 430 { 425 futex_down(&vfs_phone_futex); 426 async_serialize_start(); 427 vfs_connect(); 431 int vfs_phone = vfs_exchange_begin(); 428 432 429 433 sysarg_t rc = async_req_1_0(vfs_phone, VFS_IN_SYNC, fildes); 430 434 431 async_serialize_end(); 432 futex_up(&vfs_phone_futex); 435 vfs_exchange_end(vfs_phone); 433 436 434 437 return (int) rc; … … 437 440 off64_t lseek(int fildes, off64_t offset, int whence) 438 441 { 439 futex_down(&vfs_phone_futex); 440 async_serialize_start(); 441 vfs_connect(); 442 int vfs_phone = vfs_exchange_begin(); 442 443 443 444 sysarg_t newoff_lo; … … 447 448 &newoff_lo, &newoff_hi); 448 449 449 async_serialize_end(); 450 futex_up(&vfs_phone_futex); 450 vfs_exchange_end(vfs_phone); 451 451 452 452 if (rc != EOK) … … 460 460 sysarg_t rc; 461 461 462 futex_down(&vfs_phone_futex); 463 async_serialize_start(); 464 vfs_connect(); 462 int vfs_phone = vfs_exchange_begin(); 465 463 466 464 rc = async_req_3_0(vfs_phone, VFS_IN_TRUNCATE, fildes, 467 465 LOWER32(length), UPPER32(length)); 468 async_serialize_end(); 469 futex_up(&vfs_phone_futex); 466 vfs_exchange_end(vfs_phone); 470 467 471 468 return (int) rc; … … 477 474 aid_t req; 478 475 479 futex_down(&vfs_phone_futex); 480 async_serialize_start(); 481 vfs_connect(); 476 int vfs_phone = vfs_exchange_begin(); 482 477 483 478 req = async_send_1(vfs_phone, VFS_IN_FSTAT, fildes, NULL); 484 479 rc = async_data_read_start(vfs_phone, (void *) stat, sizeof(struct stat)); 485 480 if (rc != EOK) { 481 vfs_exchange_end(vfs_phone); 482 486 483 sysarg_t rc_orig; 487 488 async_wait_for(req, &rc_orig); 489 async_serialize_end(); 490 futex_up(&vfs_phone_futex); 484 async_wait_for(req, &rc_orig); 485 491 486 if (rc_orig == EOK) 492 487 return (ssize_t) rc; … … 494 489 return (ssize_t) rc_orig; 495 490 } 496 async_wait_for(req, &rc); 497 async_serialize_end(); 498 futex_up(&vfs_phone_futex); 491 vfs_exchange_end(vfs_phone); 492 async_wait_for(req, &rc); 499 493 500 494 return rc; … … 512 506 return ENOMEM; 513 507 514 futex_down(&vfs_phone_futex); 515 async_serialize_start(); 516 vfs_connect(); 508 int vfs_phone = vfs_exchange_begin(); 517 509 518 510 req = async_send_0(vfs_phone, VFS_IN_STAT, NULL); 519 511 rc = async_data_write_start(vfs_phone, pa, pa_size); 520 512 if (rc != EOK) { 521 async_wait_for(req, &rc_orig); 522 async_serialize_end(); 523 futex_up(&vfs_phone_futex); 513 vfs_exchange_end(vfs_phone); 524 514 free(pa); 515 async_wait_for(req, &rc_orig); 525 516 if (rc_orig == EOK) 526 517 return (int) rc; … … 530 521 rc = async_data_read_start(vfs_phone, stat, sizeof(struct stat)); 531 522 if (rc != EOK) { 532 async_wait_for(req, &rc_orig); 533 async_serialize_end(); 534 futex_up(&vfs_phone_futex); 523 vfs_exchange_end(vfs_phone); 535 524 free(pa); 536 if (rc_orig == EOK) 537 return (int) rc; 538 else 539 return (int) rc_orig; 540 } 541 async_wait_for(req, &rc); 542 async_serialize_end(); 543 futex_up(&vfs_phone_futex); 525 async_wait_for(req, &rc_orig); 526 if (rc_orig == EOK) 527 return (int) rc; 528 else 529 return (int) rc_orig; 530 } 531 vfs_exchange_end(vfs_phone); 544 532 free(pa); 533 async_wait_for(req, &rc); 545 534 return rc; 546 535 } … … 601 590 return ENOMEM; 602 591 603 futex_down(&vfs_phone_futex); 604 async_serialize_start(); 605 vfs_connect(); 592 int vfs_phone = vfs_exchange_begin(); 606 593 607 594 req = async_send_1(vfs_phone, VFS_IN_MKDIR, mode, NULL); 608 595 rc = async_data_write_start(vfs_phone, pa, pa_size); 609 596 if (rc != EOK) { 597 vfs_exchange_end(vfs_phone); 598 free(pa); 599 610 600 sysarg_t rc_orig; 611 612 async_wait_for(req, &rc_orig); 613 async_serialize_end(); 614 futex_up(&vfs_phone_futex); 615 free(pa); 616 if (rc_orig == EOK) 617 return (int) rc; 618 else 619 return (int) rc_orig; 620 } 621 async_wait_for(req, &rc); 622 async_serialize_end(); 623 futex_up(&vfs_phone_futex); 601 async_wait_for(req, &rc_orig); 602 603 if (rc_orig == EOK) 604 return (int) rc; 605 else 606 return (int) rc_orig; 607 } 608 vfs_exchange_end(vfs_phone); 624 609 free(pa); 610 async_wait_for(req, &rc); 625 611 return rc; 626 612 } … … 636 622 return ENOMEM; 637 623 638 futex_down(&vfs_phone_futex); 639 async_serialize_start(); 640 vfs_connect(); 624 int vfs_phone = vfs_exchange_begin(); 641 625 642 626 req = async_send_0(vfs_phone, VFS_IN_UNLINK, NULL); 643 627 rc = async_data_write_start(vfs_phone, pa, pa_size); 644 628 if (rc != EOK) { 629 vfs_exchange_end(vfs_phone); 630 free(pa); 631 645 632 sysarg_t rc_orig; 646 647 async_wait_for(req, &rc_orig); 648 async_serialize_end(); 649 futex_up(&vfs_phone_futex); 650 free(pa); 651 if (rc_orig == EOK) 652 return (int) rc; 653 else 654 return (int) rc_orig; 655 } 656 async_wait_for(req, &rc); 657 async_serialize_end(); 658 futex_up(&vfs_phone_futex); 633 async_wait_for(req, &rc_orig); 634 635 if (rc_orig == EOK) 636 return (int) rc; 637 else 638 return (int) rc_orig; 639 } 640 vfs_exchange_end(vfs_phone); 659 641 free(pa); 642 async_wait_for(req, &rc); 660 643 return rc; 661 644 } … … 689 672 } 690 673 691 futex_down(&vfs_phone_futex); 692 async_serialize_start(); 693 vfs_connect(); 674 int vfs_phone = vfs_exchange_begin(); 694 675 695 676 req = async_send_0(vfs_phone, VFS_IN_RENAME, NULL); 696 677 rc = async_data_write_start(vfs_phone, olda, olda_size); 697 678 if (rc != EOK) { 698 async_wait_for(req, &rc_orig); 699 async_serialize_end(); 700 futex_up(&vfs_phone_futex); 679 vfs_exchange_end(vfs_phone); 701 680 free(olda); 702 681 free(newa); 682 async_wait_for(req, &rc_orig); 703 683 if (rc_orig == EOK) 704 684 return (int) rc; … … 708 688 rc = async_data_write_start(vfs_phone, newa, newa_size); 709 689 if (rc != EOK) { 710 async_wait_for(req, &rc_orig); 711 async_serialize_end(); 712 futex_up(&vfs_phone_futex); 690 vfs_exchange_end(vfs_phone); 713 691 free(olda); 714 692 free(newa); 715 if (rc_orig == EOK) 716 return (int) rc; 717 else 718 return (int) rc_orig; 719 } 720 async_wait_for(req, &rc); 721 async_serialize_end(); 722 futex_up(&vfs_phone_futex); 693 async_wait_for(req, &rc_orig); 694 if (rc_orig == EOK) 695 return (int) rc; 696 else 697 return (int) rc_orig; 698 } 699 vfs_exchange_end(vfs_phone); 723 700 free(olda); 724 701 free(newa); 702 async_wait_for(req, &rc); 725 703 return rc; 726 704 } … … 740 718 } 741 719 742 f utex_down(&cwd_futex);720 fibril_mutex_lock(&cwd_mutex); 743 721 744 722 if (cwd_fd >= 0) … … 753 731 cwd_size = abs_size; 754 732 755 f utex_up(&cwd_futex);733 fibril_mutex_unlock(&cwd_mutex); 756 734 return EOK; 757 735 } … … 762 740 return NULL; 763 741 764 f utex_down(&cwd_futex);742 fibril_mutex_lock(&cwd_mutex); 765 743 766 744 if ((cwd_size == 0) || (size < cwd_size + 1)) { 767 f utex_up(&cwd_futex);745 fibril_mutex_unlock(&cwd_mutex); 768 746 return NULL; 769 747 } 770 748 771 749 str_cpy(buf, size, cwd_path); 772 f utex_up(&cwd_futex);750 fibril_mutex_unlock(&cwd_mutex); 773 751 774 752 return buf; … … 778 756 { 779 757 struct stat stat; 780 int rc; 781 782 rc = fstat(fildes, &stat); 783 758 759 int rc = fstat(fildes, &stat); 760 if (rc != 0) 761 return rc; 762 784 763 if (!stat.device) 785 764 return -1; … … 806 785 int dup2(int oldfd, int newfd) 807 786 { 808 futex_down(&vfs_phone_futex); 809 async_serialize_start(); 810 vfs_connect(); 787 int vfs_phone = vfs_exchange_begin(); 811 788 812 789 sysarg_t ret; 813 790 sysarg_t rc = async_req_2_1(vfs_phone, VFS_IN_DUP, oldfd, newfd, &ret); 814 791 815 async_serialize_end(); 816 futex_up(&vfs_phone_futex); 792 vfs_exchange_end(vfs_phone); 817 793 818 794 if (rc == EOK)
Note:
See TracChangeset
for help on using the changeset viewer.