Changes in uspace/lib/c/generic/vfs/vfs.c [96b02eb9:8e80d3f] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/vfs/vfs.c
r96b02eb9 r8e80d3f 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 rc = async_data_read_start(vfs_phone, (void *)buf, nbyte); 371 if (rc != EOK) { 380 rc = async_data_read_start_generic(vfs_phone, (void *) buf, nbyte, 381 IPC_XF_RESTRICT); 382 if (rc != EOK) { 383 vfs_exchange_end(vfs_phone); 384 372 385 sysarg_t rc_orig; 373 374 async_wait_for(req, &rc_orig); 375 async_serialize_end(); 376 futex_up(&vfs_phone_futex); 386 async_wait_for(req, &rc_orig); 387 377 388 if (rc_orig == EOK) 378 389 return (ssize_t) rc; … … 380 391 return (ssize_t) rc_orig; 381 392 } 382 async_wait_for(req, &rc); 383 async_serialize_end(); 384 futex_up(&vfs_phone_futex); 393 vfs_exchange_end(vfs_phone); 394 async_wait_for(req, &rc); 385 395 if (rc == EOK) 386 396 return (ssize_t) IPC_GET_ARG1(answer); … … 395 405 aid_t req; 396 406 397 futex_down(&vfs_phone_futex); 398 async_serialize_start(); 399 vfs_connect(); 407 int vfs_phone = vfs_exchange_begin(); 400 408 401 409 req = async_send_1(vfs_phone, VFS_IN_WRITE, fildes, &answer); 402 rc = async_data_write_start(vfs_phone, (void *)buf, nbyte); 403 if (rc != EOK) { 410 rc = async_data_write_start_generic(vfs_phone, (void *) buf, nbyte, 411 IPC_XF_RESTRICT); 412 if (rc != EOK) { 413 vfs_exchange_end(vfs_phone); 414 404 415 sysarg_t rc_orig; 405 406 async_wait_for(req, &rc_orig); 407 async_serialize_end(); 408 futex_up(&vfs_phone_futex); 416 async_wait_for(req, &rc_orig); 417 409 418 if (rc_orig == EOK) 410 419 return (ssize_t) rc; … … 412 421 return (ssize_t) rc_orig; 413 422 } 414 async_wait_for(req, &rc); 415 async_serialize_end(); 416 futex_up(&vfs_phone_futex); 423 vfs_exchange_end(vfs_phone); 424 async_wait_for(req, &rc); 417 425 if (rc == EOK) 418 426 return (ssize_t) IPC_GET_ARG1(answer); … … 423 431 int fsync(int fildes) 424 432 { 425 futex_down(&vfs_phone_futex); 426 async_serialize_start(); 427 vfs_connect(); 433 int vfs_phone = vfs_exchange_begin(); 428 434 429 435 sysarg_t rc = async_req_1_0(vfs_phone, VFS_IN_SYNC, fildes); 430 436 431 async_serialize_end(); 432 futex_up(&vfs_phone_futex); 437 vfs_exchange_end(vfs_phone); 433 438 434 439 return (int) rc; … … 437 442 off64_t lseek(int fildes, off64_t offset, int whence) 438 443 { 439 futex_down(&vfs_phone_futex); 440 async_serialize_start(); 441 vfs_connect(); 444 int vfs_phone = vfs_exchange_begin(); 442 445 443 446 sysarg_t newoff_lo; … … 447 450 &newoff_lo, &newoff_hi); 448 451 449 async_serialize_end(); 450 futex_up(&vfs_phone_futex); 452 vfs_exchange_end(vfs_phone); 451 453 452 454 if (rc != EOK) … … 460 462 sysarg_t rc; 461 463 462 futex_down(&vfs_phone_futex); 463 async_serialize_start(); 464 vfs_connect(); 464 int vfs_phone = vfs_exchange_begin(); 465 465 466 466 rc = async_req_3_0(vfs_phone, VFS_IN_TRUNCATE, fildes, 467 467 LOWER32(length), UPPER32(length)); 468 async_serialize_end(); 469 futex_up(&vfs_phone_futex); 468 vfs_exchange_end(vfs_phone); 470 469 471 470 return (int) rc; … … 477 476 aid_t req; 478 477 479 futex_down(&vfs_phone_futex); 480 async_serialize_start(); 481 vfs_connect(); 478 int vfs_phone = vfs_exchange_begin(); 482 479 483 480 req = async_send_1(vfs_phone, VFS_IN_FSTAT, fildes, NULL); 484 481 rc = async_data_read_start(vfs_phone, (void *) stat, sizeof(struct stat)); 485 482 if (rc != EOK) { 483 vfs_exchange_end(vfs_phone); 484 486 485 sysarg_t rc_orig; 487 488 async_wait_for(req, &rc_orig); 489 async_serialize_end(); 490 futex_up(&vfs_phone_futex); 486 async_wait_for(req, &rc_orig); 487 491 488 if (rc_orig == EOK) 492 489 return (ssize_t) rc; … … 494 491 return (ssize_t) rc_orig; 495 492 } 496 async_wait_for(req, &rc); 497 async_serialize_end(); 498 futex_up(&vfs_phone_futex); 493 vfs_exchange_end(vfs_phone); 494 async_wait_for(req, &rc); 499 495 500 496 return rc; … … 512 508 return ENOMEM; 513 509 514 futex_down(&vfs_phone_futex); 515 async_serialize_start(); 516 vfs_connect(); 510 int vfs_phone = vfs_exchange_begin(); 517 511 518 512 req = async_send_0(vfs_phone, VFS_IN_STAT, NULL); 519 513 rc = async_data_write_start(vfs_phone, pa, pa_size); 520 514 if (rc != EOK) { 521 async_wait_for(req, &rc_orig); 522 async_serialize_end(); 523 futex_up(&vfs_phone_futex); 515 vfs_exchange_end(vfs_phone); 524 516 free(pa); 517 async_wait_for(req, &rc_orig); 525 518 if (rc_orig == EOK) 526 519 return (int) rc; … … 530 523 rc = async_data_read_start(vfs_phone, stat, sizeof(struct stat)); 531 524 if (rc != EOK) { 532 async_wait_for(req, &rc_orig); 533 async_serialize_end(); 534 futex_up(&vfs_phone_futex); 525 vfs_exchange_end(vfs_phone); 535 526 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); 527 async_wait_for(req, &rc_orig); 528 if (rc_orig == EOK) 529 return (int) rc; 530 else 531 return (int) rc_orig; 532 } 533 vfs_exchange_end(vfs_phone); 544 534 free(pa); 535 async_wait_for(req, &rc); 545 536 return rc; 546 537 } … … 601 592 return ENOMEM; 602 593 603 futex_down(&vfs_phone_futex); 604 async_serialize_start(); 605 vfs_connect(); 594 int vfs_phone = vfs_exchange_begin(); 606 595 607 596 req = async_send_1(vfs_phone, VFS_IN_MKDIR, mode, NULL); 608 597 rc = async_data_write_start(vfs_phone, pa, pa_size); 609 598 if (rc != EOK) { 599 vfs_exchange_end(vfs_phone); 600 free(pa); 601 610 602 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); 603 async_wait_for(req, &rc_orig); 604 605 if (rc_orig == EOK) 606 return (int) rc; 607 else 608 return (int) rc_orig; 609 } 610 vfs_exchange_end(vfs_phone); 624 611 free(pa); 612 async_wait_for(req, &rc); 625 613 return rc; 626 614 } … … 636 624 return ENOMEM; 637 625 638 futex_down(&vfs_phone_futex); 639 async_serialize_start(); 640 vfs_connect(); 626 int vfs_phone = vfs_exchange_begin(); 641 627 642 628 req = async_send_0(vfs_phone, VFS_IN_UNLINK, NULL); 643 629 rc = async_data_write_start(vfs_phone, pa, pa_size); 644 630 if (rc != EOK) { 631 vfs_exchange_end(vfs_phone); 632 free(pa); 633 645 634 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); 635 async_wait_for(req, &rc_orig); 636 637 if (rc_orig == EOK) 638 return (int) rc; 639 else 640 return (int) rc_orig; 641 } 642 vfs_exchange_end(vfs_phone); 659 643 free(pa); 644 async_wait_for(req, &rc); 660 645 return rc; 661 646 } … … 689 674 } 690 675 691 futex_down(&vfs_phone_futex); 692 async_serialize_start(); 693 vfs_connect(); 676 int vfs_phone = vfs_exchange_begin(); 694 677 695 678 req = async_send_0(vfs_phone, VFS_IN_RENAME, NULL); 696 679 rc = async_data_write_start(vfs_phone, olda, olda_size); 697 680 if (rc != EOK) { 698 async_wait_for(req, &rc_orig); 699 async_serialize_end(); 700 futex_up(&vfs_phone_futex); 681 vfs_exchange_end(vfs_phone); 701 682 free(olda); 702 683 free(newa); 684 async_wait_for(req, &rc_orig); 703 685 if (rc_orig == EOK) 704 686 return (int) rc; … … 708 690 rc = async_data_write_start(vfs_phone, newa, newa_size); 709 691 if (rc != EOK) { 710 async_wait_for(req, &rc_orig); 711 async_serialize_end(); 712 futex_up(&vfs_phone_futex); 692 vfs_exchange_end(vfs_phone); 713 693 free(olda); 714 694 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); 695 async_wait_for(req, &rc_orig); 696 if (rc_orig == EOK) 697 return (int) rc; 698 else 699 return (int) rc_orig; 700 } 701 vfs_exchange_end(vfs_phone); 723 702 free(olda); 724 703 free(newa); 704 async_wait_for(req, &rc); 725 705 return rc; 726 706 } … … 740 720 } 741 721 742 f utex_down(&cwd_futex);722 fibril_mutex_lock(&cwd_mutex); 743 723 744 724 if (cwd_fd >= 0) … … 753 733 cwd_size = abs_size; 754 734 755 f utex_up(&cwd_futex);735 fibril_mutex_unlock(&cwd_mutex); 756 736 return EOK; 757 737 } … … 762 742 return NULL; 763 743 764 f utex_down(&cwd_futex);744 fibril_mutex_lock(&cwd_mutex); 765 745 766 746 if ((cwd_size == 0) || (size < cwd_size + 1)) { 767 f utex_up(&cwd_futex);747 fibril_mutex_unlock(&cwd_mutex); 768 748 return NULL; 769 749 } 770 750 771 751 str_cpy(buf, size, cwd_path); 772 f utex_up(&cwd_futex);752 fibril_mutex_unlock(&cwd_mutex); 773 753 774 754 return buf; … … 778 758 { 779 759 struct stat stat; 780 int rc; 781 782 rc = fstat(fildes, &stat); 783 760 761 int rc = fstat(fildes, &stat); 762 if (rc != 0) 763 return rc; 764 784 765 if (!stat.device) 785 766 return -1; … … 806 787 int dup2(int oldfd, int newfd) 807 788 { 808 futex_down(&vfs_phone_futex); 809 async_serialize_start(); 810 vfs_connect(); 789 int vfs_phone = vfs_exchange_begin(); 811 790 812 791 sysarg_t ret; 813 792 sysarg_t rc = async_req_2_1(vfs_phone, VFS_IN_DUP, oldfd, newfd, &ret); 814 793 815 async_serialize_end(); 816 futex_up(&vfs_phone_futex); 794 vfs_exchange_end(vfs_phone); 817 795 818 796 if (rc == EOK)
Note:
See TracChangeset
for help on using the changeset viewer.