Changes in uspace/lib/c/generic/vfs/vfs.c [89e780d:27b76ca] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/vfs/vfs.c
r89e780d r27b76ca 33 33 */ 34 34 35 #include <vfs/canonify.h> 35 36 #include <vfs/vfs.h> 36 #include <vfs/ canonify.h>37 #include <vfs/vfs_sess.h> 37 38 #include <macros.h> 38 39 #include <stdlib.h> … … 44 45 #include <sys/types.h> 45 46 #include <ipc/services.h> 46 #include < ipc/ns.h>47 #include <ns.h> 47 48 #include <async.h> 48 49 #include <fibril_synch.h> … … 54 55 #include <ipc/devmap.h> 55 56 56 static async_sess_t vfs_session; 57 58 static FIBRIL_MUTEX_INITIALIZE(vfs_phone_mutex); 59 static int vfs_phone = -1; 57 static FIBRIL_MUTEX_INITIALIZE(vfs_mutex); 58 static async_sess_t *vfs_sess = NULL; 60 59 61 60 static FIBRIL_MUTEX_INITIALIZE(cwd_mutex); … … 65 64 static size_t cwd_size = 0; 66 65 66 /** Start an async exchange on the VFS session. 67 * 68 * @return New exchange. 69 * 70 */ 71 async_exch_t *vfs_exchange_begin(void) 72 { 73 fibril_mutex_lock(&vfs_mutex); 74 75 while (vfs_sess == NULL) 76 vfs_sess = service_connect_blocking(EXCHANGE_PARALLEL, SERVICE_VFS, 77 0, 0); 78 79 fibril_mutex_unlock(&vfs_mutex); 80 81 return async_exchange_begin(vfs_sess); 82 } 83 84 /** Finish an async exchange on the VFS session. 85 * 86 * @param exch Exchange to be finished. 87 * 88 */ 89 void vfs_exchange_end(async_exch_t *exch) 90 { 91 async_exchange_end(exch); 92 } 93 67 94 char *absolutize(const char *path, size_t *retlen) 68 95 { 69 96 char *ncwd_path; 70 97 char *ncwd_path_nc; 71 size_t total_size;72 98 73 99 fibril_mutex_lock(&cwd_mutex); … … 78 104 return NULL; 79 105 } 80 total_size = cwd_size + 1 + size + 1; 81 ncwd_path_nc = malloc(total_size); 106 ncwd_path_nc = malloc(cwd_size + 1 + size + 1); 82 107 if (!ncwd_path_nc) { 83 108 fibril_mutex_unlock(&cwd_mutex); 84 109 return NULL; 85 110 } 86 str_cpy(ncwd_path_nc, total_size, cwd_path);111 str_cpy(ncwd_path_nc, cwd_size + 1 + size + 1, cwd_path); 87 112 ncwd_path_nc[cwd_size] = '/'; 88 113 ncwd_path_nc[cwd_size + 1] = '\0'; 89 114 } else { 90 total_size = size + 1; 91 ncwd_path_nc = malloc(total_size); 115 ncwd_path_nc = malloc(size + 1); 92 116 if (!ncwd_path_nc) { 93 117 fibril_mutex_unlock(&cwd_mutex); … … 96 120 ncwd_path_nc[0] = '\0'; 97 121 } 98 str_append(ncwd_path_nc, total_size, path);122 str_append(ncwd_path_nc, cwd_size + 1 + size + 1, path); 99 123 ncwd_path = canonify(ncwd_path_nc, retlen); 100 124 if (!ncwd_path) { … … 118 142 } 119 143 120 /** Connect to VFS service and create session. */121 static void vfs_connect(void)122 {123 while (vfs_phone < 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);150 }151 152 144 int mount(const char *fs_name, const char *mp, const char *fqdn, 153 145 const char *opts, unsigned int flags) … … 186 178 } 187 179 188 int vfs_phone= vfs_exchange_begin();180 async_exch_t *exch = vfs_exchange_begin(); 189 181 190 182 sysarg_t rc_orig; 191 aid_t req = async_send_2( vfs_phone, VFS_IN_MOUNT, devmap_handle, flags, NULL);192 sysarg_t rc = async_data_write_start( vfs_phone, (void *) mpa, mpa_size);193 if (rc != EOK) { 194 vfs_exchange_end( vfs_phone);183 aid_t req = async_send_2(exch, VFS_IN_MOUNT, devmap_handle, flags, NULL); 184 sysarg_t rc = async_data_write_start(exch, (void *) mpa, mpa_size); 185 if (rc != EOK) { 186 vfs_exchange_end(exch); 195 187 free(mpa); 196 188 async_wait_for(req, &rc_orig); … … 205 197 } 206 198 207 rc = async_data_write_start( vfs_phone, (void *) opts, str_size(opts));208 if (rc != EOK) { 209 vfs_exchange_end( vfs_phone);199 rc = async_data_write_start(exch, (void *) opts, str_size(opts)); 200 if (rc != EOK) { 201 vfs_exchange_end(exch); 210 202 free(mpa); 211 203 async_wait_for(req, &rc_orig); … … 220 212 } 221 213 222 rc = async_data_write_start( vfs_phone, (void *) fs_name, str_size(fs_name));223 if (rc != EOK) { 224 vfs_exchange_end( vfs_phone);214 rc = async_data_write_start(exch, (void *) fs_name, str_size(fs_name)); 215 if (rc != EOK) { 216 vfs_exchange_end(exch); 225 217 free(mpa); 226 218 async_wait_for(req, &rc_orig); … … 236 228 237 229 /* Ask VFS whether it likes fs_name. */ 238 rc = async_req_0_0( vfs_phone, IPC_M_PING);239 if (rc != EOK) { 240 vfs_exchange_end( vfs_phone);230 rc = async_req_0_0(exch, VFS_IN_PING); 231 if (rc != EOK) { 232 vfs_exchange_end(exch); 241 233 free(mpa); 242 234 async_wait_for(req, &rc_orig); … … 251 243 } 252 244 253 vfs_exchange_end( vfs_phone);245 vfs_exchange_end(exch); 254 246 free(mpa); 255 247 async_wait_for(req, &rc); … … 273 265 return ENOMEM; 274 266 275 int vfs_phone= vfs_exchange_begin();276 277 req = async_send_0( vfs_phone, VFS_IN_UNMOUNT, NULL);278 rc = async_data_write_start( vfs_phone, (void *) mpa, mpa_size);279 if (rc != EOK) { 280 vfs_exchange_end( vfs_phone);267 async_exch_t *exch = vfs_exchange_begin(); 268 269 req = async_send_0(exch, VFS_IN_UNMOUNT, NULL); 270 rc = async_data_write_start(exch, (void *) mpa, mpa_size); 271 if (rc != EOK) { 272 vfs_exchange_end(exch); 281 273 free(mpa); 282 274 async_wait_for(req, &rc_orig); … … 288 280 289 281 290 vfs_exchange_end( vfs_phone);282 vfs_exchange_end(exch); 291 283 free(mpa); 292 284 async_wait_for(req, &rc); … … 297 289 static int open_internal(const char *abs, size_t abs_size, int lflag, int oflag) 298 290 { 299 int vfs_phone= vfs_exchange_begin();291 async_exch_t *exch = vfs_exchange_begin(); 300 292 301 293 ipc_call_t answer; 302 aid_t req = async_send_3( vfs_phone, VFS_IN_OPEN, lflag, oflag, 0, &answer);303 sysarg_t rc = async_data_write_start( vfs_phone, abs, abs_size);304 305 if (rc != EOK) { 306 vfs_exchange_end( vfs_phone);294 aid_t req = async_send_3(exch, VFS_IN_OPEN, lflag, oflag, 0, &answer); 295 sysarg_t rc = async_data_write_start(exch, abs, abs_size); 296 297 if (rc != EOK) { 298 vfs_exchange_end(exch); 307 299 308 300 sysarg_t rc_orig; … … 315 307 } 316 308 317 vfs_exchange_end( vfs_phone);309 vfs_exchange_end(exch); 318 310 async_wait_for(req, &rc); 319 311 … … 337 329 } 338 330 339 int open_node(fdi_node_t *node, int oflag)340 {341 int vfs_phone = vfs_exchange_begin();342 343 ipc_call_t answer;344 aid_t req = async_send_4(vfs_phone, VFS_IN_OPEN_NODE, node->fs_handle,345 node->devmap_handle, node->index, oflag, &answer);346 347 vfs_exchange_end(vfs_phone);348 349 sysarg_t rc;350 async_wait_for(req, &rc);351 352 if (rc != EOK)353 return (int) rc;354 355 return (int) IPC_GET_ARG1(answer);356 }357 358 331 int close(int fildes) 359 332 { 360 333 sysarg_t rc; 361 334 362 int vfs_phone = vfs_exchange_begin(); 363 364 rc = async_req_1_0(vfs_phone, VFS_IN_CLOSE, fildes); 365 366 vfs_exchange_end(vfs_phone); 367 368 return (int)rc; 335 async_exch_t *exch = vfs_exchange_begin(); 336 rc = async_req_1_0(exch, VFS_IN_CLOSE, fildes); 337 vfs_exchange_end(exch); 338 339 return (int) rc; 369 340 } 370 341 … … 374 345 ipc_call_t answer; 375 346 aid_t req; 376 377 int vfs_phone= vfs_exchange_begin();378 379 req = async_send_1( vfs_phone, VFS_IN_READ, fildes, &answer);380 rc = async_data_read_start( vfs_phone, (void *)buf, nbyte);381 if (rc != EOK) { 382 vfs_exchange_end( vfs_phone);347 348 async_exch_t *exch = vfs_exchange_begin(); 349 350 req = async_send_1(exch, VFS_IN_READ, fildes, &answer); 351 rc = async_data_read_start(exch, (void *)buf, nbyte); 352 if (rc != EOK) { 353 vfs_exchange_end(exch); 383 354 384 355 sysarg_t rc_orig; … … 390 361 return (ssize_t) rc_orig; 391 362 } 392 vfs_exchange_end( vfs_phone);363 vfs_exchange_end(exch); 393 364 async_wait_for(req, &rc); 394 365 if (rc == EOK) … … 403 374 ipc_call_t answer; 404 375 aid_t req; 405 406 int vfs_phone= vfs_exchange_begin();407 408 req = async_send_1( vfs_phone, VFS_IN_WRITE, fildes, &answer);409 rc = async_data_write_start( vfs_phone, (void *)buf, nbyte);410 if (rc != EOK) { 411 vfs_exchange_end( vfs_phone);376 377 async_exch_t *exch = vfs_exchange_begin(); 378 379 req = async_send_1(exch, VFS_IN_WRITE, fildes, &answer); 380 rc = async_data_write_start(exch, (void *)buf, nbyte); 381 if (rc != EOK) { 382 vfs_exchange_end(exch); 412 383 413 384 sysarg_t rc_orig; … … 419 390 return (ssize_t) rc_orig; 420 391 } 421 vfs_exchange_end( vfs_phone);392 vfs_exchange_end(exch); 422 393 async_wait_for(req, &rc); 423 394 if (rc == EOK) … … 427 398 } 428 399 400 /** Read entire buffer. 401 * 402 * In face of short reads this function continues reading until either 403 * the entire buffer is read or no more data is available (at end of file). 404 * 405 * @param fildes File descriptor 406 * @param buf Buffer, @a nbytes bytes long 407 * @param nbytes Number of bytes to read 408 * 409 * @return On success, positive number of bytes read. 410 * On failure, negative error code from read(). 411 */ 412 ssize_t read_all(int fildes, void *buf, size_t nbyte) 413 { 414 ssize_t cnt = 0; 415 size_t nread = 0; 416 uint8_t *bp = (uint8_t *) buf; 417 418 do { 419 bp += cnt; 420 nread += cnt; 421 cnt = read(fildes, bp, nbyte - nread); 422 } while (cnt > 0 && (nbyte - nread - cnt) > 0); 423 424 if (cnt < 0) 425 return cnt; 426 427 return nread + cnt; 428 } 429 430 /** Write entire buffer. 431 * 432 * This function fails if it cannot write exactly @a len bytes to the file. 433 * 434 * @param fildes File descriptor 435 * @param buf Data, @a nbytes bytes long 436 * @param nbytes Number of bytes to write 437 * 438 * @return EOK on error, return value from write() if writing 439 * failed. 440 */ 441 ssize_t write_all(int fildes, const void *buf, size_t nbyte) 442 { 443 ssize_t cnt = 0; 444 ssize_t nwritten = 0; 445 const uint8_t *bp = (uint8_t *) buf; 446 447 do { 448 bp += cnt; 449 nwritten += cnt; 450 cnt = write(fildes, bp, nbyte - nwritten); 451 } while (cnt > 0 && ((ssize_t )nbyte - nwritten - cnt) > 0); 452 453 if (cnt < 0) 454 return cnt; 455 456 if ((ssize_t)nbyte - nwritten - cnt > 0) 457 return EIO; 458 459 return nbyte; 460 } 461 429 462 int fsync(int fildes) 430 463 { 431 int vfs_phone = vfs_exchange_begin(); 432 433 sysarg_t rc = async_req_1_0(vfs_phone, VFS_IN_SYNC, fildes); 434 435 vfs_exchange_end(vfs_phone); 464 async_exch_t *exch = vfs_exchange_begin(); 465 sysarg_t rc = async_req_1_0(exch, VFS_IN_SYNC, fildes); 466 vfs_exchange_end(exch); 436 467 437 468 return (int) rc; … … 440 471 off64_t lseek(int fildes, off64_t offset, int whence) 441 472 { 442 int vfs_phone= vfs_exchange_begin();473 async_exch_t *exch = vfs_exchange_begin(); 443 474 444 475 sysarg_t newoff_lo; 445 476 sysarg_t newoff_hi; 446 sysarg_t rc = async_req_4_2( vfs_phone, VFS_IN_SEEK, fildes,477 sysarg_t rc = async_req_4_2(exch, VFS_IN_SEEK, fildes, 447 478 LOWER32(offset), UPPER32(offset), whence, 448 479 &newoff_lo, &newoff_hi); 449 480 450 vfs_exchange_end( vfs_phone);481 vfs_exchange_end(exch); 451 482 452 483 if (rc != EOK) … … 460 491 sysarg_t rc; 461 492 462 int vfs_phone = vfs_exchange_begin(); 463 464 rc = async_req_3_0(vfs_phone, VFS_IN_TRUNCATE, fildes, 493 async_exch_t *exch = vfs_exchange_begin(); 494 rc = async_req_3_0(exch, VFS_IN_TRUNCATE, fildes, 465 495 LOWER32(length), UPPER32(length)); 466 vfs_exchange_end( vfs_phone);496 vfs_exchange_end(exch); 467 497 468 498 return (int) rc; … … 473 503 sysarg_t rc; 474 504 aid_t req; 475 476 int vfs_phone= vfs_exchange_begin();477 478 req = async_send_1( vfs_phone, VFS_IN_FSTAT, fildes, NULL);479 rc = async_data_read_start( vfs_phone, (void *) stat, sizeof(struct stat));480 if (rc != EOK) { 481 vfs_exchange_end( vfs_phone);505 506 async_exch_t *exch = vfs_exchange_begin(); 507 508 req = async_send_1(exch, VFS_IN_FSTAT, fildes, NULL); 509 rc = async_data_read_start(exch, (void *) stat, sizeof(struct stat)); 510 if (rc != EOK) { 511 vfs_exchange_end(exch); 482 512 483 513 sysarg_t rc_orig; … … 489 519 return (ssize_t) rc_orig; 490 520 } 491 vfs_exchange_end( vfs_phone);521 vfs_exchange_end(exch); 492 522 async_wait_for(req, &rc); 493 523 … … 506 536 return ENOMEM; 507 537 508 int vfs_phone= vfs_exchange_begin();509 510 req = async_send_0( vfs_phone, VFS_IN_STAT, NULL);511 rc = async_data_write_start( vfs_phone, pa, pa_size);512 if (rc != EOK) { 513 vfs_exchange_end( vfs_phone);538 async_exch_t *exch = vfs_exchange_begin(); 539 540 req = async_send_0(exch, VFS_IN_STAT, NULL); 541 rc = async_data_write_start(exch, pa, pa_size); 542 if (rc != EOK) { 543 vfs_exchange_end(exch); 514 544 free(pa); 515 545 async_wait_for(req, &rc_orig); … … 519 549 return (int) rc_orig; 520 550 } 521 rc = async_data_read_start( vfs_phone, stat, sizeof(struct stat));522 if (rc != EOK) { 523 vfs_exchange_end( vfs_phone);551 rc = async_data_read_start(exch, stat, sizeof(struct stat)); 552 if (rc != EOK) { 553 vfs_exchange_end(exch); 524 554 free(pa); 525 555 async_wait_for(req, &rc_orig); … … 529 559 return (int) rc_orig; 530 560 } 531 vfs_exchange_end( vfs_phone);561 vfs_exchange_end(exch); 532 562 free(pa); 533 563 async_wait_for(req, &rc); … … 590 620 return ENOMEM; 591 621 592 int vfs_phone= vfs_exchange_begin();593 594 req = async_send_1( vfs_phone, VFS_IN_MKDIR, mode, NULL);595 rc = async_data_write_start( vfs_phone, pa, pa_size);596 if (rc != EOK) { 597 vfs_exchange_end( vfs_phone);622 async_exch_t *exch = vfs_exchange_begin(); 623 624 req = async_send_1(exch, VFS_IN_MKDIR, mode, NULL); 625 rc = async_data_write_start(exch, pa, pa_size); 626 if (rc != EOK) { 627 vfs_exchange_end(exch); 598 628 free(pa); 599 629 … … 606 636 return (int) rc_orig; 607 637 } 608 vfs_exchange_end( vfs_phone);638 vfs_exchange_end(exch); 609 639 free(pa); 610 640 async_wait_for(req, &rc); … … 621 651 if (!pa) 622 652 return ENOMEM; 623 624 int vfs_phone= vfs_exchange_begin();625 626 req = async_send_0( vfs_phone, VFS_IN_UNLINK, NULL);627 rc = async_data_write_start( vfs_phone, pa, pa_size);628 if (rc != EOK) { 629 vfs_exchange_end( vfs_phone);653 654 async_exch_t *exch = vfs_exchange_begin(); 655 656 req = async_send_0(exch, VFS_IN_UNLINK, NULL); 657 rc = async_data_write_start(exch, pa, pa_size); 658 if (rc != EOK) { 659 vfs_exchange_end(exch); 630 660 free(pa); 631 661 … … 638 668 return (int) rc_orig; 639 669 } 640 vfs_exchange_end( vfs_phone);670 vfs_exchange_end(exch); 641 671 free(pa); 642 672 async_wait_for(req, &rc); … … 671 701 return ENOMEM; 672 702 } 673 674 int vfs_phone= vfs_exchange_begin();675 676 req = async_send_0( vfs_phone, VFS_IN_RENAME, NULL);677 rc = async_data_write_start( vfs_phone, olda, olda_size);678 if (rc != EOK) { 679 vfs_exchange_end( vfs_phone);703 704 async_exch_t *exch = vfs_exchange_begin(); 705 706 req = async_send_0(exch, VFS_IN_RENAME, NULL); 707 rc = async_data_write_start(exch, olda, olda_size); 708 if (rc != EOK) { 709 vfs_exchange_end(exch); 680 710 free(olda); 681 711 free(newa); … … 686 716 return (int) rc_orig; 687 717 } 688 rc = async_data_write_start( vfs_phone, newa, newa_size);689 if (rc != EOK) { 690 vfs_exchange_end( vfs_phone);718 rc = async_data_write_start(exch, newa, newa_size); 719 if (rc != EOK) { 720 vfs_exchange_end(exch); 691 721 free(olda); 692 722 free(newa); … … 697 727 return (int) rc_orig; 698 728 } 699 vfs_exchange_end( vfs_phone);729 vfs_exchange_end(exch); 700 730 free(olda); 701 731 free(newa); … … 753 783 } 754 784 755 int fd_phone(int fildes)785 async_sess_t *fd_session(exch_mgmt_t mgmt, int fildes) 756 786 { 757 787 struct stat stat; 758 int rc; 759 760 rc = fstat(fildes, &stat); 761 762 if (!stat.device) 763 return -1; 764 765 return devmap_device_connect(stat.device, 0); 766 } 767 768 int fd_node(int fildes, fdi_node_t *node) 769 { 770 struct stat stat; 771 int rc; 772 773 rc = fstat(fildes, &stat); 774 775 if (rc == EOK) { 776 node->fs_handle = stat.fs_handle; 777 node->devmap_handle = stat.devmap_handle; 778 node->index = stat.index; 779 } 780 781 return rc; 788 int rc = fstat(fildes, &stat); 789 if (rc != 0) { 790 errno = rc; 791 return NULL; 792 } 793 794 if (!stat.device) { 795 errno = ENOENT; 796 return NULL; 797 } 798 799 return devmap_device_connect(mgmt, stat.device, 0); 782 800 } 783 801 784 802 int dup2(int oldfd, int newfd) 785 803 { 786 int vfs_phone= vfs_exchange_begin();804 async_exch_t *exch = vfs_exchange_begin(); 787 805 788 806 sysarg_t ret; 789 sysarg_t rc = async_req_2_1( vfs_phone, VFS_IN_DUP, oldfd, newfd, &ret);790 791 vfs_exchange_end( vfs_phone);807 sysarg_t rc = async_req_2_1(exch, VFS_IN_DUP, oldfd, newfd, &ret); 808 809 vfs_exchange_end(exch); 792 810 793 811 if (rc == EOK) … … 797 815 } 798 816 817 int fd_wait(void) 818 { 819 async_exch_t *exch = vfs_exchange_begin(); 820 821 sysarg_t ret; 822 sysarg_t rc = async_req_0_1(exch, VFS_IN_WAIT_HANDLE, &ret); 823 824 vfs_exchange_end(exch); 825 826 if (rc == EOK) 827 return (int) ret; 828 829 return (int) rc; 830 } 831 799 832 /** @} 800 833 */
Note:
See TracChangeset
for help on using the changeset viewer.