Changeset 375ab5e in mainline for uspace/srv/fs/fat/fat_ops.c
- Timestamp:
- 2011-08-24T20:10:43Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- eb660787
- Parents:
- 7fadb65 (diff), 842a2d2 (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/srv/fs/fat/fat_ops.c
r7fadb65 r375ab5e 1 1 /* 2 2 * Copyright (c) 2008 Jakub Jermar 3 * Copyright (c) 2011 Oleg Romanenko 3 4 * All rights reserved. 4 5 * … … 29 30 /** @addtogroup fs 30 31 * @{ 31 */ 32 */ 32 33 33 34 /** … … 39 40 #include "fat_dentry.h" 40 41 #include "fat_fat.h" 42 #include "fat_directory.h" 41 43 #include "../../vfs/vfs.h" 42 44 #include <libfs.h> … … 56 58 #include <align.h> 57 59 #include <malloc.h> 60 #include <str.h> 58 61 59 62 #define FAT_NODE(node) ((node) ? (fat_node_t *) (node)->data : NULL) … … 104 107 node->dirty = false; 105 108 node->lastc_cached_valid = false; 106 node->lastc_cached_value = FAT_CLST_LAST1;109 node->lastc_cached_value = 0; 107 110 node->currc_cached_valid = false; 108 111 node->currc_cached_bn = 0; 109 node->currc_cached_value = FAT_CLST_LAST1;112 node->currc_cached_value = 0; 110 113 } 111 114 … … 116 119 fat_dentry_t *d; 117 120 int rc; 118 121 119 122 assert(node->dirty); 120 123 121 124 bs = block_bb_get(node->idx->service_id); 122 125 123 126 /* Read the block that contains the dentry of interest. */ 124 127 rc = _fat_block_get(&b, bs, node->idx->service_id, node->idx->pfc, … … 136 139 d->attr = FAT_ATTR_SUBDIR; 137 140 } 138 141 139 142 /* TODO: update other fields? (e.g time fields) */ 140 143 141 144 b->dirty = true; /* need to sync block */ 142 145 rc = block_put(b); … … 255 258 fn->data = nodep; 256 259 nodep->bp = fn; 257 260 258 261 *nodepp = nodep; 259 262 return EOK; … … 291 294 * We must instantiate the node from the file system. 292 295 */ 293 296 294 297 assert(idxp->pfc); 295 298 … … 309 312 310 313 d = ((fat_dentry_t *)b->data) + (idxp->pdi % DPS(bs)); 314 if (FAT_IS_FAT32(bs)) { 315 nodep->firstc = uint16_t_le2host(d->firstc_lo) | 316 (uint16_t_le2host(d->firstc_hi) << 16); 317 } 318 else 319 nodep->firstc = uint16_t_le2host(d->firstc); 320 311 321 if (d->attr & FAT_ATTR_SUBDIR) { 312 /* 322 /* 313 323 * The only directory which does not have this bit set is the 314 324 * root directory itself. The root directory node is handled … … 316 326 */ 317 327 nodep->type = FAT_DIRECTORY; 328 318 329 /* 319 330 * Unfortunately, the 'size' field of the FAT dentry is not … … 321 332 * size of the directory by walking the FAT. 322 333 */ 323 uint16_t clusters; 324 rc = fat_clusters_get(&clusters, bs, idxp->service_id, 325 uint16_t_le2host(d->firstc)); 334 uint32_t clusters; 335 rc = fat_clusters_get(&clusters, bs, idxp->service_id, nodep->firstc); 326 336 if (rc != EOK) { 327 337 (void) block_put(b); … … 334 344 nodep->size = uint32_t_le2host(d->size); 335 345 } 336 nodep->firstc = uint16_t_le2host(d->firstc); 346 337 347 nodep->lnkcnt = 1; 338 348 nodep->refcnt = 1; … … 363 373 int fat_match(fs_node_t **rfn, fs_node_t *pfn, const char *component) 364 374 { 365 fat_bs_t *bs;366 375 fat_node_t *parentp = FAT_NODE(pfn); 367 char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; 368 unsigned i, j; 369 unsigned blocks; 376 char name[FAT_LFN_NAME_SIZE]; 370 377 fat_dentry_t *d; 371 378 service_id_t service_id; 372 block_t *b;373 379 int rc; 374 380 … … 376 382 service_id = parentp->idx->service_id; 377 383 fibril_mutex_unlock(&parentp->idx->lock); 378 379 bs = block_bb_get(service_id); 380 blocks = parentp->size / BPS(bs); 381 for (i = 0; i < blocks; i++) { 382 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE); 383 if (rc != EOK) 384 return rc; 385 for (j = 0; j < DPS(bs); j++) { 386 d = ((fat_dentry_t *)b->data) + j; 387 switch (fat_classify_dentry(d)) { 388 case FAT_DENTRY_SKIP: 389 case FAT_DENTRY_FREE: 390 continue; 391 case FAT_DENTRY_LAST: 392 /* miss */ 393 rc = block_put(b); 394 *rfn = NULL; 395 return rc; 396 default: 397 case FAT_DENTRY_VALID: 398 fat_dentry_name_get(d, name); 399 break; 384 385 fat_directory_t di; 386 rc = fat_directory_open(parentp, &di); 387 if (rc != EOK) 388 return rc; 389 390 while (fat_directory_read(&di, name, &d) == EOK) { 391 if (fat_dentry_namecmp(name, component) == 0) { 392 /* hit */ 393 fat_node_t *nodep; 394 aoff64_t o = di.pos % (BPS(di.bs) / sizeof(fat_dentry_t)); 395 fat_idx_t *idx = fat_idx_get_by_pos(service_id, 396 parentp->firstc, di.bnum * DPS(di.bs) + o); 397 if (!idx) { 398 /* 399 * Can happen if memory is low or if we 400 * run out of 32-bit indices. 401 */ 402 rc = fat_directory_close(&di); 403 return (rc == EOK) ? ENOMEM : rc; 400 404 } 401 if (fat_dentry_namecmp(name, component) == 0) { 402 /* hit */ 403 fat_node_t *nodep; 404 fat_idx_t *idx = fat_idx_get_by_pos(service_id, 405 parentp->firstc, i * DPS(bs) + j); 406 if (!idx) { 407 /* 408 * Can happen if memory is low or if we 409 * run out of 32-bit indices. 410 */ 411 rc = block_put(b); 412 return (rc == EOK) ? ENOMEM : rc; 413 } 414 rc = fat_node_get_core(&nodep, idx); 415 fibril_mutex_unlock(&idx->lock); 416 if (rc != EOK) { 417 (void) block_put(b); 418 return rc; 419 } 420 *rfn = FS_NODE(nodep); 421 rc = block_put(b); 422 if (rc != EOK) 423 (void) fat_node_put(*rfn); 405 rc = fat_node_get_core(&nodep, idx); 406 fibril_mutex_unlock(&idx->lock); 407 if (rc != EOK) { 408 (void) fat_directory_close(&di); 424 409 return rc; 425 410 } 426 } 427 rc = block_put(b); 428 if (rc != EOK) 411 *rfn = FS_NODE(nodep); 412 rc = fat_directory_close(&di); 413 if (rc != EOK) 414 (void) fat_node_put(*rfn); 429 415 return rc; 430 } 431 416 } else { 417 rc = fat_directory_next(&di); 418 if (rc != EOK) 419 break; 420 } 421 } 422 (void) fat_directory_close(&di); 432 423 *rfn = NULL; 433 424 return EOK; … … 591 582 fat_bs_t *bs; 592 583 block_t *b; 593 unsigned i, j; 594 unsigned blocks; 595 fat_cluster_t mcl, lcl; 584 fat_directory_t di; 585 fat_dentry_t de; 596 586 int rc; 597 587 … … 607 597 fibril_mutex_unlock(&childp->lock); 608 598 609 if (!fat_dentry_name_verify(name)) { 610 /* 611 * Attempt to create unsupported name. 612 */ 599 if (!fat_valid_name(name)) 613 600 return ENOTSUP; 614 } 615 616 /* 617 * Get us an unused parent node's dentry or grow the parent and allocate 618 * a new one. 619 */ 620 601 621 602 fibril_mutex_lock(&parentp->idx->lock); 622 603 bs = block_bb_get(parentp->idx->service_id); 623 624 blocks = parentp->size / BPS(bs); 625 626 for (i = 0; i < blocks; i++) { 627 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE); 628 if (rc != EOK) { 629 fibril_mutex_unlock(&parentp->idx->lock); 630 return rc; 631 } 632 for (j = 0; j < DPS(bs); j++) { 633 d = ((fat_dentry_t *)b->data) + j; 634 switch (fat_classify_dentry(d)) { 635 case FAT_DENTRY_SKIP: 636 case FAT_DENTRY_VALID: 637 /* skipping used and meta entries */ 638 continue; 639 case FAT_DENTRY_FREE: 640 case FAT_DENTRY_LAST: 641 /* found an empty slot */ 642 goto hit; 643 } 644 } 645 rc = block_put(b); 646 if (rc != EOK) { 647 fibril_mutex_unlock(&parentp->idx->lock); 648 return rc; 649 } 650 } 651 j = 0; 652 653 /* 654 * We need to grow the parent in order to create a new unused dentry. 655 */ 656 if (parentp->firstc == FAT_CLST_ROOT) { 657 /* Can't grow the root directory. */ 658 fibril_mutex_unlock(&parentp->idx->lock); 659 return ENOSPC; 660 } 661 rc = fat_alloc_clusters(bs, parentp->idx->service_id, 1, &mcl, &lcl); 662 if (rc != EOK) { 663 fibril_mutex_unlock(&parentp->idx->lock); 664 return rc; 665 } 666 rc = fat_zero_cluster(bs, parentp->idx->service_id, mcl); 667 if (rc != EOK) { 668 (void) fat_free_clusters(bs, parentp->idx->service_id, mcl); 669 fibril_mutex_unlock(&parentp->idx->lock); 670 return rc; 671 } 672 rc = fat_append_clusters(bs, parentp, mcl, lcl); 673 if (rc != EOK) { 674 (void) fat_free_clusters(bs, parentp->idx->service_id, mcl); 675 fibril_mutex_unlock(&parentp->idx->lock); 676 return rc; 677 } 678 parentp->size += BPS(bs) * SPC(bs); 679 parentp->dirty = true; /* need to sync node */ 680 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE); 681 if (rc != EOK) { 682 fibril_mutex_unlock(&parentp->idx->lock); 683 return rc; 684 } 685 d = (fat_dentry_t *)b->data; 686 687 hit: 604 rc = fat_directory_open(parentp, &di); 605 if (rc != EOK) 606 return rc; 607 688 608 /* 689 609 * At this point we only establish the link between the parent and the … … 692 612 * dentry data is kept in the child node structure. 693 613 */ 694 memset(d, 0, sizeof(fat_dentry_t)); 695 fat_dentry_name_set(d, name); 696 b->dirty = true; /* need to sync block */ 697 rc = block_put(b); 614 memset(&de, 0, sizeof(fat_dentry_t)); 615 616 rc = fat_directory_write(&di, name, &de); 617 if (rc!=EOK) 618 return rc; 619 rc = fat_directory_close(&di); 620 if (rc!=EOK) 621 return rc; 622 698 623 fibril_mutex_unlock(&parentp->idx->lock); 699 if (rc != EOK) 624 if (rc != EOK) 700 625 return rc; 701 626 702 627 fibril_mutex_lock(&childp->idx->lock); 703 628 704 629 if (childp->type == FAT_DIRECTORY) { 705 630 /* … … 720 645 d = (fat_dentry_t *) b->data; 721 646 if ((fat_classify_dentry(d) == FAT_DENTRY_LAST) || 722 ( str_cmp((char *) d->name, FAT_NAME_DOT)) == 0) {647 (bcmp(d->name, FAT_NAME_DOT, FAT_NAME_LEN)) == 0) { 723 648 memset(d, 0, sizeof(fat_dentry_t)); 724 649 memcpy(d->name, FAT_NAME_DOT, FAT_NAME_LEN); … … 730 655 d++; 731 656 if ((fat_classify_dentry(d) == FAT_DENTRY_LAST) || 732 ( str_cmp((char *) d->name, FAT_NAME_DOT_DOT) == 0)) {657 (bcmp(d->name, FAT_NAME_DOT_DOT, FAT_NAME_LEN) == 0)) { 733 658 memset(d, 0, sizeof(fat_dentry_t)); 734 659 memcpy(d->name, FAT_NAME_DOT_DOT, FAT_NAME_LEN); 735 660 memcpy(d->ext, FAT_EXT_PAD, FAT_EXT_LEN); 736 661 d->attr = FAT_ATTR_SUBDIR; 737 d->firstc = (parentp->firstc == FAT_ CLST_ROOT) ?738 host2uint16_t_le(FAT_CLST_R ES0) :662 d->firstc = (parentp->firstc == FAT_ROOT_CLST(bs)) ? 663 host2uint16_t_le(FAT_CLST_ROOTPAR) : 739 664 host2uint16_t_le(parentp->firstc); 740 665 /* TODO: initialize also the date/time members. */ … … 750 675 751 676 childp->idx->pfc = parentp->firstc; 752 childp->idx->pdi = i * DPS(bs) + j;677 childp->idx->pdi = di.pos; /* di.pos holds absolute position of SFN entry */ 753 678 fibril_mutex_unlock(&childp->idx->lock); 754 679 … … 770 695 fat_node_t *parentp = FAT_NODE(pfn); 771 696 fat_node_t *childp = FAT_NODE(cfn); 772 fat_bs_t *bs;773 fat_dentry_t *d;774 block_t *b;775 697 bool has_children; 776 698 int rc; … … 778 700 if (!parentp) 779 701 return EBUSY; 780 702 781 703 rc = fat_has_children(&has_children, cfn); 782 704 if (rc != EOK) … … 789 711 assert(childp->lnkcnt == 1); 790 712 fibril_mutex_lock(&childp->idx->lock); 791 bs = block_bb_get(childp->idx->service_id); 792 793 rc = _fat_block_get(&b, bs, childp->idx->service_id, childp->idx->pfc, 794 NULL, (childp->idx->pdi * sizeof(fat_dentry_t)) / BPS(bs), 795 BLOCK_FLAGS_NONE); 796 if (rc != EOK) 713 714 fat_directory_t di; 715 rc = fat_directory_open(parentp,&di); 716 if (rc != EOK) 797 717 goto error; 798 d = (fat_dentry_t *)b->data + 799 (childp->idx->pdi % (BPS(bs) / sizeof(fat_dentry_t))); 800 /* mark the dentry as not-currently-used */ 801 d->name[0] = FAT_DENTRY_ERASED; 802 b->dirty = true; /* need to sync block */ 803 rc = block_put(b); 718 rc = fat_directory_seek(&di, childp->idx->pdi); 719 if (rc != EOK) 720 goto error; 721 rc = fat_directory_erase(&di); 722 if (rc != EOK) 723 goto error; 724 rc = fat_directory_close(&di); 804 725 if (rc != EOK) 805 726 goto error; … … 820 741 821 742 error: 822 fibril_mutex_unlock(&parentp->idx->lock); 743 (void) fat_directory_close(&di); 744 fibril_mutex_unlock(&childp->idx->lock); 823 745 fibril_mutex_unlock(&childp->lock); 824 fibril_mutex_unlock(& childp->idx->lock);746 fibril_mutex_unlock(&parentp->lock); 825 747 return rc; 826 748 } … … 839 761 return EOK; 840 762 } 841 763 842 764 fibril_mutex_lock(&nodep->idx->lock); 843 765 bs = block_bb_get(nodep->idx->service_id); … … 847 769 for (i = 0; i < blocks; i++) { 848 770 fat_dentry_t *d; 849 771 850 772 rc = fat_block_get(&b, bs, nodep, i, BLOCK_FLAGS_NONE); 851 773 if (rc != EOK) { … … 875 797 if (rc != EOK) { 876 798 fibril_mutex_unlock(&nodep->idx->lock); 877 return rc; 799 return rc; 878 800 } 879 801 } … … 946 868 fat_bs_t *bs; 947 869 int rc; 948 870 949 871 /* Check for option enabling write through. */ 950 872 if (str_cmp(opts, "wtcache") == 0) … … 1003 925 return ENOMEM; 1004 926 } 927 1005 928 fs_node_initialize(rfn); 1006 929 fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t)); … … 1027 950 1028 951 rootp->type = FAT_DIRECTORY; 1029 rootp->firstc = FAT_ CLST_ROOT;952 rootp->firstc = FAT_ROOT_CLST(bs); 1030 953 rootp->refcnt = 1; 1031 954 rootp->lnkcnt = 0; /* FS root is not linked */ 1032 rootp->size = RDE(bs) * sizeof(fat_dentry_t); 955 956 if (FAT_IS_FAT32(bs)) { 957 uint32_t clusters; 958 rc = fat_clusters_get(&clusters, bs, service_id, rootp->firstc); 959 if (rc != EOK) { 960 free(rfn); 961 free(rootp); 962 (void) block_cache_fini(service_id); 963 block_fini(service_id); 964 fat_idx_fini_by_service_id(service_id); 965 return ENOTSUP; 966 } 967 rootp->size = BPS(bs) * SPC(bs) * clusters; 968 } else 969 rootp->size = RDE(bs) * sizeof(fat_dentry_t); 970 1033 971 rootp->idx = ridxp; 1034 972 ridxp->nodep = rootp; 1035 973 rootp->bp = rfn; 1036 974 rfn->data = rootp; 1037 975 1038 976 fibril_mutex_unlock(&ridxp->lock); 1039 977 … … 1064 1002 return EBUSY; 1065 1003 } 1066 1004 1067 1005 /* 1068 1006 * Put the root node and force it to the FAT free node list. … … 1141 1079 } 1142 1080 } else { 1143 unsigned bnum;1144 1081 aoff64_t spos = pos; 1145 char name[FAT_ NAME_LEN + 1 + FAT_EXT_LEN + 1];1082 char name[FAT_LFN_NAME_SIZE]; 1146 1083 fat_dentry_t *d; 1147 1084 … … 1150 1087 assert(BPS(bs) % sizeof(fat_dentry_t) == 0); 1151 1088 1152 /* 1153 * Our strategy for readdir() is to use the position pointer as 1154 * an index into the array of all dentries. On entry, it points 1155 * to the first unread dentry. If we skip any dentries, we bump 1156 * the position pointer accordingly. 1157 */ 1158 bnum = (pos * sizeof(fat_dentry_t)) / BPS(bs); 1159 while (bnum < nodep->size / BPS(bs)) { 1160 aoff64_t o; 1161 1162 rc = fat_block_get(&b, bs, nodep, bnum, 1163 BLOCK_FLAGS_NONE); 1164 if (rc != EOK) 1165 goto err; 1166 for (o = pos % (BPS(bs) / sizeof(fat_dentry_t)); 1167 o < BPS(bs) / sizeof(fat_dentry_t); 1168 o++, pos++) { 1169 d = ((fat_dentry_t *)b->data) + o; 1170 switch (fat_classify_dentry(d)) { 1171 case FAT_DENTRY_SKIP: 1172 case FAT_DENTRY_FREE: 1173 continue; 1174 case FAT_DENTRY_LAST: 1175 rc = block_put(b); 1176 if (rc != EOK) 1177 goto err; 1178 goto miss; 1179 default: 1180 case FAT_DENTRY_VALID: 1181 fat_dentry_name_get(d, name); 1182 rc = block_put(b); 1183 if (rc != EOK) 1184 goto err; 1185 goto hit; 1186 } 1187 } 1188 rc = block_put(b); 1189 if (rc != EOK) 1190 goto err; 1191 bnum++; 1192 } 1089 fat_directory_t di; 1090 rc = fat_directory_open(nodep, &di); 1091 if (rc != EOK) goto err; 1092 rc = fat_directory_seek(&di, pos); 1093 if (rc != EOK) { 1094 (void) fat_directory_close(&di); 1095 goto err; 1096 } 1097 1098 rc = fat_directory_read(&di, name, &d); 1099 if (rc == EOK) goto hit; 1100 if (rc == ENOENT) goto miss; 1101 1102 err: 1103 (void) fat_node_put(fn); 1104 async_answer_0(callid, rc); 1105 return rc; 1106 1193 1107 miss: 1108 rc = fat_directory_close(&di); 1109 if (rc!=EOK) 1110 goto err; 1194 1111 rc = fat_node_put(fn); 1195 1112 async_answer_0(callid, rc != EOK ? rc : ENOENT); … … 1197 1114 return rc != EOK ? rc : ENOENT; 1198 1115 1199 err:1200 (void) fat_node_put(fn);1201 async_answer_0(callid, rc);1202 return rc;1203 1204 1116 hit: 1117 pos = di.pos; 1118 rc = fat_directory_close(&di); 1119 if (rc!=EOK) 1120 goto err; 1205 1121 (void) async_data_read_finalize(callid, name, str_size(name) + 1); 1206 bytes = (pos - spos) +1;1122 bytes = (pos - spos)+1; 1207 1123 } 1208 1124 … … 1231 1147 return ENOENT; 1232 1148 nodep = FAT_NODE(fn); 1233 1149 1234 1150 ipc_callid_t callid; 1235 1151 size_t len; … … 1247 1163 * but this one greatly simplifies fat_write(). Note that we can afford 1248 1164 * to do this because the client must be ready to handle the return 1249 * value signalizing a smaller number of bytes written. 1250 */ 1165 * value signalizing a smaller number of bytes written. 1166 */ 1251 1167 bytes = min(len, BPS(bs) - pos % BPS(bs)); 1252 1168 if (bytes == BPS(bs)) 1253 1169 flags |= BLOCK_FLAGS_NOREAD; 1254 1170 1255 1171 boundary = ROUND_UP(nodep->size, BPC(bs)); 1256 1172 if (pos < boundary) { … … 1295 1211 */ 1296 1212 unsigned nclsts; 1297 fat_cluster_t mcl, lcl; 1298 1213 fat_cluster_t mcl, lcl; 1214 1299 1215 nclsts = (ROUND_UP(pos + bytes, BPC(bs)) - boundary) / BPC(bs); 1300 1216 /* create an independent chain of nclsts clusters in all FATs */ … … 1380 1296 nodep->size = size; 1381 1297 nodep->dirty = true; /* need to sync node */ 1382 rc = EOK; 1298 rc = EOK; 1383 1299 } else { 1384 1300 /* … … 1401 1317 nodep->size = size; 1402 1318 nodep->dirty = true; /* need to sync node */ 1403 rc = EOK; 1319 rc = EOK; 1404 1320 } 1405 1321 out: … … 1444 1360 if (!fn) 1445 1361 return ENOENT; 1446 1362 1447 1363 fat_node_t *nodep = FAT_NODE(fn); 1448 1364 1449 1365 nodep->dirty = true; 1450 1366 rc = fat_node_sync(nodep); 1451 1367 1452 1368 fat_node_put(fn); 1453 1369 return rc;
Note:
See TracChangeset
for help on using the changeset viewer.