Changes in uspace/srv/fs/fat/fat_ops.c [991f645:4cac2d69] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/fs/fat/fat_ops.c
r991f645 r4cac2d69 45 45 #include <ipc/services.h> 46 46 #include <ipc/devmap.h> 47 #include <macros.h>48 47 #include <async.h> 49 48 #include <errno.h> 50 #include <str .h>49 #include <string.h> 51 50 #include <byteorder.h> 52 51 #include <adt/hash_table.h> … … 60 59 #define FS_NODE(node) ((node) ? (node)->bp : NULL) 61 60 62 #define DPS(bs) (BPS((bs)) / sizeof(fat_dentry_t))63 #define BPC(bs) (BPS((bs)) * SPC((bs)))64 65 61 /** Mutex protecting the list of cached free FAT nodes. */ 66 62 static FIBRIL_MUTEX_INITIALIZE(ffn_mutex); … … 72 68 * Forward declarations of FAT libfs operations. 73 69 */ 74 static int fat_root_get(fs_node_t **, dev map_handle_t);70 static int fat_root_get(fs_node_t **, dev_handle_t); 75 71 static int fat_match(fs_node_t **, fs_node_t *, const char *); 76 static int fat_node_get(fs_node_t **, dev map_handle_t, fs_index_t);72 static int fat_node_get(fs_node_t **, dev_handle_t, fs_index_t); 77 73 static int fat_node_open(fs_node_t *); 78 74 static int fat_node_put(fs_node_t *); 79 static int fat_create_node(fs_node_t **, dev map_handle_t, int);75 static int fat_create_node(fs_node_t **, dev_handle_t, int); 80 76 static int fat_destroy_node(fs_node_t *); 81 77 static int fat_link(fs_node_t *, fs_node_t *, const char *); … … 83 79 static int fat_has_children(bool *, fs_node_t *); 84 80 static fs_index_t fat_index_get(fs_node_t *); 85 static aoff64_t fat_size_get(fs_node_t *);81 static size_t fat_size_get(fs_node_t *); 86 82 static unsigned fat_lnkcnt_get(fs_node_t *); 87 83 static char fat_plb_get_char(unsigned); 88 84 static bool fat_is_directory(fs_node_t *); 89 85 static bool fat_is_file(fs_node_t *node); 90 static dev map_handle_t fat_device_get(fs_node_t *node);86 static dev_handle_t fat_device_get(fs_node_t *node); 91 87 92 88 /* … … 104 100 node->refcnt = 0; 105 101 node->dirty = false; 106 node->lastc_cached_valid = false;107 node->lastc_cached_value = FAT_CLST_LAST1;108 node->currc_cached_valid = false;109 node->currc_cached_bn = 0;110 node->currc_cached_value = FAT_CLST_LAST1;111 102 } 112 103 … … 116 107 fat_bs_t *bs; 117 108 fat_dentry_t *d; 109 uint16_t bps; 110 unsigned dps; 118 111 int rc; 119 112 120 113 assert(node->dirty); 121 114 122 bs = block_bb_get(node->idx->devmap_handle); 115 bs = block_bb_get(node->idx->dev_handle); 116 bps = uint16_t_le2host(bs->bps); 117 dps = bps / sizeof(fat_dentry_t); 123 118 124 119 /* Read the block that contains the dentry of interest. */ 125 rc = _fat_block_get(&b, bs, node->idx->devmap_handle, node->idx->pfc, 126 NULL, (node->idx->pdi * sizeof(fat_dentry_t)) / BPS(bs), 127 BLOCK_FLAGS_NONE); 120 rc = _fat_block_get(&b, bs, node->idx->dev_handle, node->idx->pfc, 121 (node->idx->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE); 128 122 if (rc != EOK) 129 123 return rc; 130 124 131 d = ((fat_dentry_t *)b->data) + (node->idx->pdi % DPS(bs));125 d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps); 132 126 133 127 d->firstc = host2uint16_t_le(node->firstc); … … 145 139 } 146 140 147 static int fat_node_fini_by_dev map_handle(devmap_handle_t devmap_handle)141 static int fat_node_fini_by_dev_handle(dev_handle_t dev_handle) 148 142 { 149 143 link_t *lnk; … … 170 164 goto restart; 171 165 } 172 if (nodep->idx->dev map_handle != devmap_handle) {166 if (nodep->idx->dev_handle != dev_handle) { 173 167 fibril_mutex_unlock(&nodep->idx->lock); 174 168 fibril_mutex_unlock(&nodep->lock); … … 271 265 fat_dentry_t *d; 272 266 fat_node_t *nodep = NULL; 267 unsigned bps; 268 unsigned spc; 269 unsigned dps; 273 270 int rc; 274 271 … … 299 296 return rc; 300 297 301 bs = block_bb_get(idxp->devmap_handle); 298 bs = block_bb_get(idxp->dev_handle); 299 bps = uint16_t_le2host(bs->bps); 300 spc = bs->spc; 301 dps = bps / sizeof(fat_dentry_t); 302 302 303 303 /* Read the block that contains the dentry of interest. */ 304 rc = _fat_block_get(&b, bs, idxp->dev map_handle, idxp->pfc, NULL,305 (idxp->pdi * sizeof(fat_dentry_t)) / BPS(bs), BLOCK_FLAGS_NONE);304 rc = _fat_block_get(&b, bs, idxp->dev_handle, idxp->pfc, 305 (idxp->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE); 306 306 if (rc != EOK) { 307 307 (void) fat_node_put(FS_NODE(nodep)); … … 309 309 } 310 310 311 d = ((fat_dentry_t *)b->data) + (idxp->pdi % DPS(bs));311 d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps); 312 312 if (d->attr & FAT_ATTR_SUBDIR) { 313 313 /* … … 323 323 */ 324 324 uint16_t clusters; 325 rc = fat_clusters_get(&clusters, bs, idxp->dev map_handle,325 rc = fat_clusters_get(&clusters, bs, idxp->dev_handle, 326 326 uint16_t_le2host(d->firstc)); 327 327 if (rc != EOK) { … … 329 329 return rc; 330 330 } 331 nodep->size = BPS(bs) * SPC(bs)* clusters;331 nodep->size = bps * spc * clusters; 332 332 } else { 333 333 nodep->type = FAT_FILE; … … 356 356 */ 357 357 358 int fat_root_get(fs_node_t **rfn, dev map_handle_t devmap_handle)359 { 360 return fat_node_get(rfn, dev map_handle, 0);358 int fat_root_get(fs_node_t **rfn, dev_handle_t dev_handle) 359 { 360 return fat_node_get(rfn, dev_handle, 0); 361 361 } 362 362 … … 367 367 char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; 368 368 unsigned i, j; 369 unsigned bps; /* bytes per sector */ 370 unsigned dps; /* dentries per sector */ 369 371 unsigned blocks; 370 372 fat_dentry_t *d; 371 devmap_handle_t devmap_handle;372 373 block_t *b; 373 374 int rc; 374 375 375 376 fibril_mutex_lock(&parentp->idx->lock); 376 devmap_handle = parentp->idx->devmap_handle; 377 fibril_mutex_unlock(&parentp->idx->lock); 378 379 bs = block_bb_get(devmap_handle); 380 blocks = parentp->size / BPS(bs); 377 bs = block_bb_get(parentp->idx->dev_handle); 378 bps = uint16_t_le2host(bs->bps); 379 dps = bps / sizeof(fat_dentry_t); 380 blocks = parentp->size / bps; 381 381 for (i = 0; i < blocks; i++) { 382 382 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE); 383 if (rc != EOK) 383 if (rc != EOK) { 384 fibril_mutex_unlock(&parentp->idx->lock); 384 385 return rc; 385 for (j = 0; j < DPS(bs); j++) { 386 } 387 for (j = 0; j < dps; j++) { 386 388 d = ((fat_dentry_t *)b->data) + j; 387 389 switch (fat_classify_dentry(d)) { … … 392 394 /* miss */ 393 395 rc = block_put(b); 396 fibril_mutex_unlock(&parentp->idx->lock); 394 397 *rfn = NULL; 395 398 return rc; … … 402 405 /* hit */ 403 406 fat_node_t *nodep; 404 fat_idx_t *idx = fat_idx_get_by_pos(devmap_handle, 405 parentp->firstc, i * DPS(bs) + j); 407 /* 408 * Assume tree hierarchy for locking. We 409 * already have the parent and now we are going 410 * to lock the child. Never lock in the oposite 411 * order. 412 */ 413 fat_idx_t *idx = fat_idx_get_by_pos( 414 parentp->idx->dev_handle, parentp->firstc, 415 i * dps + j); 416 fibril_mutex_unlock(&parentp->idx->lock); 406 417 if (!idx) { 407 418 /* … … 426 437 } 427 438 rc = block_put(b); 428 if (rc != EOK) 439 if (rc != EOK) { 440 fibril_mutex_unlock(&parentp->idx->lock); 429 441 return rc; 430 } 431 442 } 443 } 444 445 fibril_mutex_unlock(&parentp->idx->lock); 432 446 *rfn = NULL; 433 447 return EOK; … … 435 449 436 450 /** Instantiate a FAT in-core node. */ 437 int fat_node_get(fs_node_t **rfn, dev map_handle_t devmap_handle, fs_index_t index)451 int fat_node_get(fs_node_t **rfn, dev_handle_t dev_handle, fs_index_t index) 438 452 { 439 453 fat_node_t *nodep; … … 441 455 int rc; 442 456 443 idxp = fat_idx_get_by_index(dev map_handle, index);457 idxp = fat_idx_get_by_index(dev_handle, index); 444 458 if (!idxp) { 445 459 *rfn = NULL; … … 492 506 } 493 507 494 int fat_create_node(fs_node_t **rfn, dev map_handle_t devmap_handle, int flags)508 int fat_create_node(fs_node_t **rfn, dev_handle_t dev_handle, int flags) 495 509 { 496 510 fat_idx_t *idxp; … … 498 512 fat_bs_t *bs; 499 513 fat_cluster_t mcl, lcl; 514 uint16_t bps; 500 515 int rc; 501 516 502 bs = block_bb_get(devmap_handle); 517 bs = block_bb_get(dev_handle); 518 bps = uint16_t_le2host(bs->bps); 503 519 if (flags & L_DIRECTORY) { 504 520 /* allocate a cluster */ 505 rc = fat_alloc_clusters(bs, dev map_handle, 1, &mcl, &lcl);521 rc = fat_alloc_clusters(bs, dev_handle, 1, &mcl, &lcl); 506 522 if (rc != EOK) 507 523 return rc; 508 524 /* populate the new cluster with unused dentries */ 509 rc = fat_zero_cluster(bs, dev map_handle, mcl);525 rc = fat_zero_cluster(bs, dev_handle, mcl); 510 526 if (rc != EOK) { 511 (void) fat_free_clusters(bs, dev map_handle, mcl);527 (void) fat_free_clusters(bs, dev_handle, mcl); 512 528 return rc; 513 529 } … … 516 532 rc = fat_node_get_new(&nodep); 517 533 if (rc != EOK) { 518 (void) fat_free_clusters(bs, dev map_handle, mcl);534 (void) fat_free_clusters(bs, dev_handle, mcl); 519 535 return rc; 520 536 } 521 rc = fat_idx_get_new(&idxp, dev map_handle);522 if (rc != EOK) { 523 (void) fat_free_clusters(bs, dev map_handle, mcl);537 rc = fat_idx_get_new(&idxp, dev_handle); 538 if (rc != EOK) { 539 (void) fat_free_clusters(bs, dev_handle, mcl); 524 540 (void) fat_node_put(FS_NODE(nodep)); 525 541 return rc; … … 529 545 nodep->type = FAT_DIRECTORY; 530 546 nodep->firstc = mcl; 531 nodep->size = BPS(bs) * SPC(bs);547 nodep->size = bps * bs->spc; 532 548 } else { 533 549 nodep->type = FAT_FILE; … … 570 586 assert(!has_children); 571 587 572 bs = block_bb_get(nodep->idx->dev map_handle);588 bs = block_bb_get(nodep->idx->dev_handle); 573 589 if (nodep->firstc != FAT_CLST_RES0) { 574 590 assert(nodep->size); 575 591 /* Free all clusters allocated to the node. */ 576 rc = fat_free_clusters(bs, nodep->idx->dev map_handle,592 rc = fat_free_clusters(bs, nodep->idx->dev_handle, 577 593 nodep->firstc); 578 594 } … … 592 608 block_t *b; 593 609 unsigned i, j; 610 uint16_t bps; 611 unsigned dps; 594 612 unsigned blocks; 595 613 fat_cluster_t mcl, lcl; … … 620 638 621 639 fibril_mutex_lock(&parentp->idx->lock); 622 bs = block_bb_get(parentp->idx->devmap_handle); 623 624 blocks = parentp->size / BPS(bs); 640 bs = block_bb_get(parentp->idx->dev_handle); 641 bps = uint16_t_le2host(bs->bps); 642 dps = bps / sizeof(fat_dentry_t); 643 644 blocks = parentp->size / bps; 625 645 626 646 for (i = 0; i < blocks; i++) { … … 630 650 return rc; 631 651 } 632 for (j = 0; j < DPS(bs); j++) {652 for (j = 0; j < dps; j++) { 633 653 d = ((fat_dentry_t *)b->data) + j; 634 654 switch (fat_classify_dentry(d)) { … … 659 679 return ENOSPC; 660 680 } 661 rc = fat_alloc_clusters(bs, parentp->idx->dev map_handle, 1, &mcl, &lcl);681 rc = fat_alloc_clusters(bs, parentp->idx->dev_handle, 1, &mcl, &lcl); 662 682 if (rc != EOK) { 663 683 fibril_mutex_unlock(&parentp->idx->lock); 664 684 return rc; 665 685 } 666 rc = fat_zero_cluster(bs, parentp->idx->dev map_handle, mcl);667 if (rc != EOK) { 668 (void) fat_free_clusters(bs, parentp->idx->dev map_handle, mcl);686 rc = fat_zero_cluster(bs, parentp->idx->dev_handle, mcl); 687 if (rc != EOK) { 688 (void) fat_free_clusters(bs, parentp->idx->dev_handle, mcl); 669 689 fibril_mutex_unlock(&parentp->idx->lock); 670 690 return rc; 671 691 } 672 rc = fat_append_clusters(bs, parentp, mcl , lcl);673 if (rc != EOK) { 674 (void) fat_free_clusters(bs, parentp->idx->dev map_handle, mcl);692 rc = fat_append_clusters(bs, parentp, mcl); 693 if (rc != EOK) { 694 (void) fat_free_clusters(bs, parentp->idx->dev_handle, mcl); 675 695 fibril_mutex_unlock(&parentp->idx->lock); 676 696 return rc; 677 697 } 678 parentp->size += BPS(bs) * SPC(bs);698 parentp->size += bps * bs->spc; 679 699 parentp->dirty = true; /* need to sync node */ 680 700 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE); … … 702 722 fibril_mutex_lock(&childp->idx->lock); 703 723 704 if (childp->type == FAT_DIRECTORY) { 724 /* 725 * If possible, create the Sub-directory Identifier Entry and the 726 * Sub-directory Parent Pointer Entry (i.e. "." and ".."). These entries 727 * are not mandatory according to Standard ECMA-107 and HelenOS VFS does 728 * not use them anyway, so this is rather a sign of our good will. 729 */ 730 rc = fat_block_get(&b, bs, childp, 0, BLOCK_FLAGS_NONE); 731 if (rc != EOK) { 705 732 /* 706 * If possible, create the Sub-directory Identifier Entry and 707 * the Sub-directory Parent Pointer Entry (i.e. "." and ".."). 708 * These entries are not mandatory according to Standard 709 * ECMA-107 and HelenOS VFS does not use them anyway, so this is 710 * rather a sign of our good will. 733 * Rather than returning an error, simply skip the creation of 734 * these two entries. 711 735 */ 712 rc = fat_block_get(&b, bs, childp, 0, BLOCK_FLAGS_NONE); 713 if (rc != EOK) { 714 /* 715 * Rather than returning an error, simply skip the 716 * creation of these two entries. 717 */ 718 goto skip_dots; 719 } 720 d = (fat_dentry_t *) b->data; 721 if ((fat_classify_dentry(d) == FAT_DENTRY_LAST) || 722 (str_cmp((char *) d->name, FAT_NAME_DOT)) == 0) { 723 memset(d, 0, sizeof(fat_dentry_t)); 724 str_cpy((char *) d->name, 8, FAT_NAME_DOT); 725 str_cpy((char *) d->ext, 3, FAT_EXT_PAD); 726 d->attr = FAT_ATTR_SUBDIR; 727 d->firstc = host2uint16_t_le(childp->firstc); 728 /* TODO: initialize also the date/time members. */ 729 } 730 d++; 731 if ((fat_classify_dentry(d) == FAT_DENTRY_LAST) || 732 (str_cmp((char *) d->name, FAT_NAME_DOT_DOT) == 0)) { 733 memset(d, 0, sizeof(fat_dentry_t)); 734 str_cpy((char *) d->name, 8, FAT_NAME_DOT_DOT); 735 str_cpy((char *) d->ext, 3, FAT_EXT_PAD); 736 d->attr = FAT_ATTR_SUBDIR; 737 d->firstc = (parentp->firstc == FAT_CLST_ROOT) ? 738 host2uint16_t_le(FAT_CLST_RES0) : 739 host2uint16_t_le(parentp->firstc); 740 /* TODO: initialize also the date/time members. */ 741 } 742 b->dirty = true; /* need to sync block */ 743 /* 744 * Ignore the return value as we would have fallen through on error 745 * anyway. 746 */ 747 (void) block_put(b); 748 } 736 goto skip_dots; 737 } 738 d = (fat_dentry_t *)b->data; 739 if (fat_classify_dentry(d) == FAT_DENTRY_LAST || 740 str_cmp(d->name, FAT_NAME_DOT) == 0) { 741 memset(d, 0, sizeof(fat_dentry_t)); 742 str_cpy(d->name, 8, FAT_NAME_DOT); 743 str_cpy(d->ext, 3, FAT_EXT_PAD); 744 d->attr = FAT_ATTR_SUBDIR; 745 d->firstc = host2uint16_t_le(childp->firstc); 746 /* TODO: initialize also the date/time members. */ 747 } 748 d++; 749 if (fat_classify_dentry(d) == FAT_DENTRY_LAST || 750 str_cmp(d->name, FAT_NAME_DOT_DOT) == 0) { 751 memset(d, 0, sizeof(fat_dentry_t)); 752 str_cpy(d->name, 8, FAT_NAME_DOT_DOT); 753 str_cpy(d->ext, 3, FAT_EXT_PAD); 754 d->attr = FAT_ATTR_SUBDIR; 755 d->firstc = (parentp->firstc == FAT_CLST_ROOT) ? 756 host2uint16_t_le(FAT_CLST_RES0) : 757 host2uint16_t_le(parentp->firstc); 758 /* TODO: initialize also the date/time members. */ 759 } 760 b->dirty = true; /* need to sync block */ 761 /* 762 * Ignore the return value as we would have fallen through on error 763 * anyway. 764 */ 765 (void) block_put(b); 749 766 skip_dots: 750 767 751 768 childp->idx->pfc = parentp->firstc; 752 childp->idx->pdi = i * DPS(bs)+ j;769 childp->idx->pdi = i * dps + j; 753 770 fibril_mutex_unlock(&childp->idx->lock); 754 771 … … 772 789 fat_bs_t *bs; 773 790 fat_dentry_t *d; 791 uint16_t bps; 774 792 block_t *b; 775 793 bool has_children; … … 789 807 assert(childp->lnkcnt == 1); 790 808 fibril_mutex_lock(&childp->idx->lock); 791 bs = block_bb_get(childp->idx->devmap_handle); 792 793 rc = _fat_block_get(&b, bs, childp->idx->devmap_handle, childp->idx->pfc, 794 NULL, (childp->idx->pdi * sizeof(fat_dentry_t)) / BPS(bs), 809 bs = block_bb_get(childp->idx->dev_handle); 810 bps = uint16_t_le2host(bs->bps); 811 812 rc = _fat_block_get(&b, bs, childp->idx->dev_handle, childp->idx->pfc, 813 (childp->idx->pdi * sizeof(fat_dentry_t)) / bps, 795 814 BLOCK_FLAGS_NONE); 796 815 if (rc != EOK) 797 816 goto error; 798 817 d = (fat_dentry_t *)b->data + 799 (childp->idx->pdi % ( BPS(bs)/ sizeof(fat_dentry_t)));818 (childp->idx->pdi % (bps / sizeof(fat_dentry_t))); 800 819 /* mark the dentry as not-currently-used */ 801 820 d->name[0] = FAT_DENTRY_ERASED; … … 829 848 fat_bs_t *bs; 830 849 fat_node_t *nodep = FAT_NODE(fn); 850 unsigned bps; 851 unsigned dps; 831 852 unsigned blocks; 832 853 block_t *b; … … 840 861 841 862 fibril_mutex_lock(&nodep->idx->lock); 842 bs = block_bb_get(nodep->idx->devmap_handle); 843 844 blocks = nodep->size / BPS(bs); 863 bs = block_bb_get(nodep->idx->dev_handle); 864 bps = uint16_t_le2host(bs->bps); 865 dps = bps / sizeof(fat_dentry_t); 866 867 blocks = nodep->size / bps; 845 868 846 869 for (i = 0; i < blocks; i++) { … … 852 875 return rc; 853 876 } 854 for (j = 0; j < DPS(bs); j++) {877 for (j = 0; j < dps; j++) { 855 878 d = ((fat_dentry_t *)b->data) + j; 856 879 switch (fat_classify_dentry(d)) { … … 889 912 } 890 913 891 aoff64_t fat_size_get(fs_node_t *fn)914 size_t fat_size_get(fs_node_t *fn) 892 915 { 893 916 return FAT_NODE(fn)->size; … … 914 937 } 915 938 916 dev map_handle_t fat_device_get(fs_node_t *node)939 dev_handle_t fat_device_get(fs_node_t *node) 917 940 { 918 941 return 0; … … 946 969 void fat_mounted(ipc_callid_t rid, ipc_call_t *request) 947 970 { 948 dev map_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);971 dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); 949 972 enum cache_mode cmode; 950 973 fat_bs_t *bs; 974 uint16_t bps; 975 uint16_t rde; 951 976 952 977 /* Accept the mount options */ … … 968 993 969 994 /* initialize libblock */ 970 rc = block_init(dev map_handle, BS_SIZE);995 rc = block_init(dev_handle, BS_SIZE); 971 996 if (rc != EOK) { 972 997 ipc_answer_0(rid, rc); … … 975 1000 976 1001 /* prepare the boot block */ 977 rc = block_bb_read(dev map_handle, BS_BLOCK);978 if (rc != EOK) { 979 block_fini(dev map_handle);1002 rc = block_bb_read(dev_handle, BS_BLOCK); 1003 if (rc != EOK) { 1004 block_fini(dev_handle); 980 1005 ipc_answer_0(rid, rc); 981 1006 return; … … 983 1008 984 1009 /* get the buffer with the boot sector */ 985 bs = block_bb_get(devmap_handle); 986 987 if (BPS(bs) != BS_SIZE) { 988 block_fini(devmap_handle); 1010 bs = block_bb_get(dev_handle); 1011 1012 /* Read the number of root directory entries. */ 1013 bps = uint16_t_le2host(bs->bps); 1014 rde = uint16_t_le2host(bs->root_ent_max); 1015 1016 if (bps != BS_SIZE) { 1017 block_fini(dev_handle); 989 1018 ipc_answer_0(rid, ENOTSUP); 990 1019 return; … … 992 1021 993 1022 /* Initialize the block cache */ 994 rc = block_cache_init(dev map_handle, BPS(bs), 0 /* XXX */, cmode);995 if (rc != EOK) { 996 block_fini(dev map_handle);1023 rc = block_cache_init(dev_handle, bps, 0 /* XXX */, cmode); 1024 if (rc != EOK) { 1025 block_fini(dev_handle); 997 1026 ipc_answer_0(rid, rc); 998 1027 return; … … 1000 1029 1001 1030 /* Do some simple sanity checks on the file system. */ 1002 rc = fat_sanity_check(bs, dev map_handle);1003 if (rc != EOK) { 1004 (void) block_cache_fini(dev map_handle);1005 block_fini(dev map_handle);1031 rc = fat_sanity_check(bs, dev_handle); 1032 if (rc != EOK) { 1033 (void) block_cache_fini(dev_handle); 1034 block_fini(dev_handle); 1006 1035 ipc_answer_0(rid, rc); 1007 1036 return; 1008 1037 } 1009 1038 1010 rc = fat_idx_init_by_dev map_handle(devmap_handle);1011 if (rc != EOK) { 1012 (void) block_cache_fini(dev map_handle);1013 block_fini(dev map_handle);1039 rc = fat_idx_init_by_dev_handle(dev_handle); 1040 if (rc != EOK) { 1041 (void) block_cache_fini(dev_handle); 1042 block_fini(dev_handle); 1014 1043 ipc_answer_0(rid, rc); 1015 1044 return; … … 1019 1048 fs_node_t *rfn = (fs_node_t *)malloc(sizeof(fs_node_t)); 1020 1049 if (!rfn) { 1021 (void) block_cache_fini(dev map_handle);1022 block_fini(dev map_handle);1023 fat_idx_fini_by_dev map_handle(devmap_handle);1050 (void) block_cache_fini(dev_handle); 1051 block_fini(dev_handle); 1052 fat_idx_fini_by_dev_handle(dev_handle); 1024 1053 ipc_answer_0(rid, ENOMEM); 1025 1054 return; … … 1029 1058 if (!rootp) { 1030 1059 free(rfn); 1031 (void) block_cache_fini(dev map_handle);1032 block_fini(dev map_handle);1033 fat_idx_fini_by_dev map_handle(devmap_handle);1060 (void) block_cache_fini(dev_handle); 1061 block_fini(dev_handle); 1062 fat_idx_fini_by_dev_handle(dev_handle); 1034 1063 ipc_answer_0(rid, ENOMEM); 1035 1064 return; … … 1037 1066 fat_node_initialize(rootp); 1038 1067 1039 fat_idx_t *ridxp = fat_idx_get_by_pos(dev map_handle, FAT_CLST_ROOTPAR, 0);1068 fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0); 1040 1069 if (!ridxp) { 1041 1070 free(rfn); 1042 1071 free(rootp); 1043 (void) block_cache_fini(dev map_handle);1044 block_fini(dev map_handle);1045 fat_idx_fini_by_dev map_handle(devmap_handle);1072 (void) block_cache_fini(dev_handle); 1073 block_fini(dev_handle); 1074 fat_idx_fini_by_dev_handle(dev_handle); 1046 1075 ipc_answer_0(rid, ENOMEM); 1047 1076 return; … … 1054 1083 rootp->refcnt = 1; 1055 1084 rootp->lnkcnt = 0; /* FS root is not linked */ 1056 rootp->size = RDE(bs)* sizeof(fat_dentry_t);1085 rootp->size = rde * sizeof(fat_dentry_t); 1057 1086 rootp->idx = ridxp; 1058 1087 ridxp->nodep = rootp; … … 1072 1101 void fat_unmounted(ipc_callid_t rid, ipc_call_t *request) 1073 1102 { 1074 dev map_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);1103 dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); 1075 1104 fs_node_t *fn; 1076 1105 fat_node_t *nodep; 1077 1106 int rc; 1078 1107 1079 rc = fat_root_get(&fn, dev map_handle);1108 rc = fat_root_get(&fn, dev_handle); 1080 1109 if (rc != EOK) { 1081 1110 ipc_answer_0(rid, rc); … … 1105 1134 * stop using libblock for this instance. 1106 1135 */ 1107 (void) fat_node_fini_by_dev map_handle(devmap_handle);1108 fat_idx_fini_by_dev map_handle(devmap_handle);1109 (void) block_cache_fini(dev map_handle);1110 block_fini(dev map_handle);1136 (void) fat_node_fini_by_dev_handle(dev_handle); 1137 fat_idx_fini_by_dev_handle(dev_handle); 1138 (void) block_cache_fini(dev_handle); 1139 block_fini(dev_handle); 1111 1140 1112 1141 ipc_answer_0(rid, EOK); … … 1125 1154 void fat_read(ipc_callid_t rid, ipc_call_t *request) 1126 1155 { 1127 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request); 1128 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); 1129 aoff64_t pos = 1130 (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request)); 1156 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); 1157 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); 1158 off_t pos = (off_t)IPC_GET_ARG3(*request); 1131 1159 fs_node_t *fn; 1132 1160 fat_node_t *nodep; 1133 1161 fat_bs_t *bs; 1162 uint16_t bps; 1134 1163 size_t bytes; 1135 1164 block_t *b; 1136 1165 int rc; 1137 1166 1138 rc = fat_node_get(&fn, dev map_handle, index);1167 rc = fat_node_get(&fn, dev_handle, index); 1139 1168 if (rc != EOK) { 1140 1169 ipc_answer_0(rid, rc); … … 1156 1185 } 1157 1186 1158 bs = block_bb_get(devmap_handle); 1187 bs = block_bb_get(dev_handle); 1188 bps = uint16_t_le2host(bs->bps); 1159 1189 1160 1190 if (nodep->type == FAT_FILE) { … … 1169 1199 (void) async_data_read_finalize(callid, NULL, 0); 1170 1200 } else { 1171 bytes = min(len, BPS(bs) - pos % BPS(bs));1201 bytes = min(len, bps - pos % bps); 1172 1202 bytes = min(bytes, nodep->size - pos); 1173 rc = fat_block_get(&b, bs, nodep, pos / BPS(bs),1203 rc = fat_block_get(&b, bs, nodep, pos / bps, 1174 1204 BLOCK_FLAGS_NONE); 1175 1205 if (rc != EOK) { … … 1179 1209 return; 1180 1210 } 1181 (void) async_data_read_finalize(callid, 1182 b ->data + pos % BPS(bs), bytes);1211 (void) async_data_read_finalize(callid, b->data + pos % bps, 1212 bytes); 1183 1213 rc = block_put(b); 1184 1214 if (rc != EOK) { … … 1190 1220 } else { 1191 1221 unsigned bnum; 1192 aoff64_t spos = pos;1222 off_t spos = pos; 1193 1223 char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; 1194 1224 fat_dentry_t *d; 1195 1225 1196 1226 assert(nodep->type == FAT_DIRECTORY); 1197 assert(nodep->size % BPS(bs)== 0);1198 assert( BPS(bs)% sizeof(fat_dentry_t) == 0);1227 assert(nodep->size % bps == 0); 1228 assert(bps % sizeof(fat_dentry_t) == 0); 1199 1229 1200 1230 /* … … 1204 1234 * the position pointer accordingly. 1205 1235 */ 1206 bnum = (pos * sizeof(fat_dentry_t)) / BPS(bs);1207 while (bnum < nodep->size / BPS(bs)) {1208 aoff64_t o;1236 bnum = (pos * sizeof(fat_dentry_t)) / bps; 1237 while (bnum < nodep->size / bps) { 1238 off_t o; 1209 1239 1210 1240 rc = fat_block_get(&b, bs, nodep, bnum, … … 1212 1242 if (rc != EOK) 1213 1243 goto err; 1214 for (o = pos % ( BPS(bs)/ sizeof(fat_dentry_t));1215 o < BPS(bs)/ sizeof(fat_dentry_t);1244 for (o = pos % (bps / sizeof(fat_dentry_t)); 1245 o < bps / sizeof(fat_dentry_t); 1216 1246 o++, pos++) { 1217 1247 d = ((fat_dentry_t *)b->data) + o; … … 1262 1292 void fat_write(ipc_callid_t rid, ipc_call_t *request) 1263 1293 { 1264 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request); 1265 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); 1266 aoff64_t pos = 1267 (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request)); 1294 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); 1295 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); 1296 off_t pos = (off_t)IPC_GET_ARG3(*request); 1268 1297 fs_node_t *fn; 1269 1298 fat_node_t *nodep; … … 1271 1300 size_t bytes, size; 1272 1301 block_t *b; 1273 aoff64_t boundary; 1302 uint16_t bps; 1303 unsigned spc; 1304 unsigned bpc; /* bytes per cluster */ 1305 off_t boundary; 1274 1306 int flags = BLOCK_FLAGS_NONE; 1275 1307 int rc; 1276 1308 1277 rc = fat_node_get(&fn, dev map_handle, index);1309 rc = fat_node_get(&fn, dev_handle, index); 1278 1310 if (rc != EOK) { 1279 1311 ipc_answer_0(rid, rc); … … 1295 1327 } 1296 1328 1297 bs = block_bb_get(devmap_handle); 1329 bs = block_bb_get(dev_handle); 1330 bps = uint16_t_le2host(bs->bps); 1331 spc = bs->spc; 1332 bpc = bps * spc; 1298 1333 1299 1334 /* … … 1304 1339 * value signalizing a smaller number of bytes written. 1305 1340 */ 1306 bytes = min(len, BPS(bs) - pos % BPS(bs));1307 if (bytes == BPS(bs))1341 bytes = min(len, bps - pos % bps); 1342 if (bytes == bps) 1308 1343 flags |= BLOCK_FLAGS_NOREAD; 1309 1344 1310 boundary = ROUND_UP(nodep->size, BPC(bs));1345 boundary = ROUND_UP(nodep->size, bpc); 1311 1346 if (pos < boundary) { 1312 1347 /* … … 1323 1358 return; 1324 1359 } 1325 rc = fat_block_get(&b, bs, nodep, pos / BPS(bs), flags);1360 rc = fat_block_get(&b, bs, nodep, pos / bps, flags); 1326 1361 if (rc != EOK) { 1327 1362 (void) fat_node_put(fn); … … 1330 1365 return; 1331 1366 } 1332 (void) async_data_write_finalize(callid, 1333 b ->data + pos % BPS(bs), bytes);1367 (void) async_data_write_finalize(callid, b->data + pos % bps, 1368 bytes); 1334 1369 b->dirty = true; /* need to sync block */ 1335 1370 rc = block_put(b); … … 1355 1390 fat_cluster_t mcl, lcl; 1356 1391 1357 nclsts = (ROUND_UP(pos + bytes, BPC(bs)) - boundary) / BPC(bs);1392 nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc; 1358 1393 /* create an independent chain of nclsts clusters in all FATs */ 1359 rc = fat_alloc_clusters(bs, dev map_handle, nclsts, &mcl, &lcl);1394 rc = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl); 1360 1395 if (rc != EOK) { 1361 1396 /* could not allocate a chain of nclsts clusters */ … … 1368 1403 rc = fat_fill_gap(bs, nodep, mcl, pos); 1369 1404 if (rc != EOK) { 1370 (void) fat_free_clusters(bs, dev map_handle, mcl);1405 (void) fat_free_clusters(bs, dev_handle, mcl); 1371 1406 (void) fat_node_put(fn); 1372 1407 ipc_answer_0(callid, rc); … … 1374 1409 return; 1375 1410 } 1376 rc = _fat_block_get(&b, bs, dev map_handle, lcl, NULL,1377 (pos / BPS(bs)) % SPC(bs),flags);1411 rc = _fat_block_get(&b, bs, dev_handle, lcl, (pos / bps) % spc, 1412 flags); 1378 1413 if (rc != EOK) { 1379 (void) fat_free_clusters(bs, dev map_handle, mcl);1414 (void) fat_free_clusters(bs, dev_handle, mcl); 1380 1415 (void) fat_node_put(fn); 1381 1416 ipc_answer_0(callid, rc); … … 1383 1418 return; 1384 1419 } 1385 (void) async_data_write_finalize(callid, 1386 b ->data + pos % BPS(bs), bytes);1420 (void) async_data_write_finalize(callid, b->data + pos % bps, 1421 bytes); 1387 1422 b->dirty = true; /* need to sync block */ 1388 1423 rc = block_put(b); 1389 1424 if (rc != EOK) { 1390 (void) fat_free_clusters(bs, dev map_handle, mcl);1425 (void) fat_free_clusters(bs, dev_handle, mcl); 1391 1426 (void) fat_node_put(fn); 1392 1427 ipc_answer_0(rid, rc); … … 1397 1432 * node's cluster chain. 1398 1433 */ 1399 rc = fat_append_clusters(bs, nodep, mcl , lcl);1434 rc = fat_append_clusters(bs, nodep, mcl); 1400 1435 if (rc != EOK) { 1401 (void) fat_free_clusters(bs, dev map_handle, mcl);1436 (void) fat_free_clusters(bs, dev_handle, mcl); 1402 1437 (void) fat_node_put(fn); 1403 1438 ipc_answer_0(rid, rc); … … 1414 1449 void fat_truncate(ipc_callid_t rid, ipc_call_t *request) 1415 1450 { 1416 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request); 1417 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); 1418 aoff64_t size = 1419 (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request)); 1451 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); 1452 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); 1453 size_t size = (off_t)IPC_GET_ARG3(*request); 1420 1454 fs_node_t *fn; 1421 1455 fat_node_t *nodep; 1422 1456 fat_bs_t *bs; 1457 uint16_t bps; 1458 uint8_t spc; 1459 unsigned bpc; /* bytes per cluster */ 1423 1460 int rc; 1424 1461 1425 rc = fat_node_get(&fn, dev map_handle, index);1462 rc = fat_node_get(&fn, dev_handle, index); 1426 1463 if (rc != EOK) { 1427 1464 ipc_answer_0(rid, rc); … … 1434 1471 nodep = FAT_NODE(fn); 1435 1472 1436 bs = block_bb_get(devmap_handle); 1473 bs = block_bb_get(dev_handle); 1474 bps = uint16_t_le2host(bs->bps); 1475 spc = bs->spc; 1476 bpc = bps * spc; 1437 1477 1438 1478 if (nodep->size == size) { … … 1444 1484 */ 1445 1485 rc = EINVAL; 1446 } else if (ROUND_UP(nodep->size, BPC(bs)) == ROUND_UP(size, BPC(bs))) {1486 } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) { 1447 1487 /* 1448 1488 * The node will be shrunk, but no clusters will be deallocated. … … 1461 1501 } else { 1462 1502 fat_cluster_t lastc; 1463 rc = fat_cluster_walk(bs, dev map_handle, nodep->firstc,1464 &lastc, NULL, (size - 1) / BPC(bs));1503 rc = fat_cluster_walk(bs, dev_handle, nodep->firstc, 1504 &lastc, NULL, (size - 1) / bpc); 1465 1505 if (rc != EOK) 1466 1506 goto out; … … 1486 1526 void fat_destroy(ipc_callid_t rid, ipc_call_t *request) 1487 1527 { 1488 dev map_handle_t devmap_handle = (devmap_handle_t)IPC_GET_ARG1(*request);1528 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); 1489 1529 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); 1490 1530 fs_node_t *fn; 1491 1531 int rc; 1492 1532 1493 rc = fat_node_get(&fn, dev map_handle, index);1533 rc = fat_node_get(&fn, dev_handle, index); 1494 1534 if (rc != EOK) { 1495 1535 ipc_answer_0(rid, rc); … … 1517 1557 void fat_sync(ipc_callid_t rid, ipc_call_t *request) 1518 1558 { 1519 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request); 1520 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); 1521 1522 fs_node_t *fn; 1523 int rc = fat_node_get(&fn, devmap_handle, index); 1524 if (rc != EOK) { 1525 ipc_answer_0(rid, rc); 1526 return; 1527 } 1528 if (!fn) { 1529 ipc_answer_0(rid, ENOENT); 1530 return; 1531 } 1532 1533 fat_node_t *nodep = FAT_NODE(fn); 1534 1535 nodep->dirty = true; 1536 rc = fat_node_sync(nodep); 1537 1538 fat_node_put(fn); 1539 ipc_answer_0(rid, rc); 1559 /* Dummy implementation */ 1560 ipc_answer_0(rid, EOK); 1540 1561 } 1541 1562
Note:
See TracChangeset
for help on using the changeset viewer.