Changeset 5d95f02 in mainline for uspace/srv/fs/fat/fat_fat.c
- Timestamp:
- 2011-08-26T23:04:07Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 4bf6895
- Parents:
- 0dbe5ac
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/fs/fat/fat_fat.c
r0dbe5ac r5d95f02 51 51 #include <mem.h> 52 52 53 /*54 * Convenience macros for computing some frequently used values from the55 * primitive boot sector members.56 */57 #define CLBN2PBN(bs, cl, bn) \58 (SSA((bs)) + ((cl) - FAT_CLST_FIRST) * SPC((bs)) + (bn) % SPC((bs)))59 60 53 #define IS_ODD(number) (number & 0x1) 61 54 … … 244 237 * @return EOK on success or a negative error code. 245 238 */ 246 int fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, aoff64_t pos) 239 int 240 fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, aoff64_t pos) 247 241 { 248 242 block_t *b; … … 286 280 } 287 281 288 /** Get cluster from the first FAT. FAT12 version282 /** Get cluster from the first FAT. 289 283 * 290 284 * @param bs Buffer holding the boot sector for the file system. … … 304 298 int rc; 305 299 306 offset = (clst + clst /2);300 offset = (clst + clst / 2); 307 301 if (offset / BPS(bs) >= SF(bs)) 308 302 return ERANGE; … … 313 307 return rc; 314 308 315 byte1 = ((uint8_t *) b->data)[offset % BPS(bs)];309 byte1 = ((uint8_t *) b->data)[offset % BPS(bs)]; 316 310 /* This cluster access spans a sector boundary. Check only for FAT12 */ 317 311 if ((offset % BPS(bs)) + 1 == BPS(bs)) { 318 /* Is itlast sector of FAT? */312 /* Is this the last sector of FAT? */ 319 313 if (offset / BPS(bs) < SF(bs)) { 320 /* No . Readingnext sector */314 /* No, read the next sector */ 321 315 rc = block_get(&b1, service_id, 1 + RSCNT(bs) + 322 SF(bs)*fatno + offset / BPS(bs), BLOCK_FLAGS_NONE); 316 SF(bs) * fatno + offset / BPS(bs), 317 BLOCK_FLAGS_NONE); 323 318 if (rc != EOK) { 324 319 block_put(b); … … 336 331 return rc; 337 332 } 338 } 339 else { 340 /* Yes. It is last sector of FAT */ 333 } else { 334 /* Yes. This is the last sector of FAT */ 341 335 block_put(b); 342 336 return ERANGE; 343 337 } 344 } 345 else 346 byte2 = ((uint8_t*) b->data)[(offset % BPS(bs))+1]; 338 } else 339 byte2 = ((uint8_t *) b->data)[(offset % BPS(bs)) + 1]; 347 340 348 341 *value = uint16_t_le2host(byte1 | (byte2 << 8)); … … 353 346 354 347 rc = block_put(b); 348 355 349 return rc; 356 350 } 357 351 358 /** Get cluster from the first FAT. FAT16 version352 /** Get cluster from the first FAT. 359 353 * 360 354 * @param bs Buffer holding the boot sector for the file system. … … 387 381 } 388 382 389 /** Get cluster from the first FAT. FAT32 version383 /** Get cluster from the first FAT. 390 384 * 391 385 * @param bs Buffer holding the boot sector for the file system. … … 411 405 return rc; 412 406 413 *value = uint32_t_le2host(*(uint32_t *)(b->data + offset % BPS(bs))) & FAT32_MASK; 407 *value = uint32_t_le2host(*(uint32_t *)(b->data + offset % BPS(bs))) & 408 FAT32_MASK; 414 409 415 410 rc = block_put(b); … … 436 431 assert(fatno < FATCNT(bs)); 437 432 438 if (FAT_IS_FAT12(bs)) {433 if (FAT_IS_FAT12(bs)) 439 434 rc = fat_get_cluster_fat12(bs, service_id, fatno, clst, value); 440 } 441 else { 442 if (FAT_IS_FAT32(bs)) 443 rc = fat_get_cluster_fat32(bs, service_id, fatno, clst, value); 444 else 445 rc = fat_get_cluster_fat16(bs, service_id, fatno, clst, value); 446 } 435 else if (FAT_IS_FAT16(bs)) 436 rc = fat_get_cluster_fat16(bs, service_id, fatno, clst, value); 437 else 438 rc = fat_get_cluster_fat32(bs, service_id, fatno, clst, value); 447 439 448 440 return rc; 449 441 } 450 442 451 /** Set cluster in one instance of FAT. FAT12 version.443 /** Set cluster in one instance of FAT. 452 444 * 453 445 * @param bs Buffer holding the boot sector for the file system. … … 463 455 fat_cluster_t clst, fat_cluster_t value) 464 456 { 465 block_t *b, *b1 =NULL;457 block_t *b, *b1 = NULL; 466 458 aoff64_t offset; 467 459 uint16_t byte1, byte2; 468 460 int rc; 469 461 470 offset = (clst + clst /2);462 offset = (clst + clst / 2); 471 463 if (offset / BPS(bs) >= SF(bs)) 472 464 return ERANGE; … … 479 471 byte1 = ((uint8_t*) b->data)[offset % BPS(bs)]; 480 472 bool border = false; 481 /* This cluster access spans a sector boundary. Check only for FAT12*/482 if ((offset % BPS(bs)) +1 == BPS(bs)) {483 /* Is it last sector of FAT? */473 /* This cluster access spans a sector boundary. */ 474 if ((offset % BPS(bs)) + 1 == BPS(bs)) { 475 /* Is it the last sector of FAT? */ 484 476 if (offset / BPS(bs) < SF(bs)) { 485 /* No . Readingnext sector */477 /* No, read the next sector */ 486 478 rc = block_get(&b1, service_id, 1 + RSCNT(bs) + 487 SF(bs)*fatno + offset / BPS(bs), BLOCK_FLAGS_NONE); 479 SF(bs) * fatno + offset / BPS(bs), 480 BLOCK_FLAGS_NONE); 488 481 if (rc != EOK) { 489 482 block_put(b); … … 494 487 * first byte of next sector 495 488 */ 496 byte2 = ((uint8_t *) b1->data)[0];489 byte2 = ((uint8_t *) b1->data)[0]; 497 490 border = true; 498 } 499 else { 500 /* Yes. It is last sector of fat */ 491 } else { 492 /* Yes. This is the last sector of FAT */ 501 493 block_put(b); 502 494 return ERANGE; … … 504 496 } 505 497 else 506 byte2 = ((uint8_t*) b->data)[(offset % BPS(bs)) +1];498 byte2 = ((uint8_t*) b->data)[(offset % BPS(bs)) + 1]; 507 499 508 500 if (IS_ODD(clst)) { … … 519 511 byte2 = byte2 | (value >> 8); 520 512 521 ((uint8_t *) b->data)[(offset % BPS(bs))] = byte1;513 ((uint8_t *) b->data)[(offset % BPS(bs))] = byte1; 522 514 if (border) { 523 ((uint8_t *) b1->data)[0] = byte2;515 ((uint8_t *) b1->data)[0] = byte2; 524 516 525 517 b1->dirty = true; … … 530 522 } 531 523 } else 532 ((uint8_t *) b->data)[(offset % BPS(bs))+1] = byte2;524 ((uint8_t *) b->data)[(offset % BPS(bs)) + 1] = byte2; 533 525 534 526 b->dirty = true; /* need to sync block */ 535 527 rc = block_put(b); 528 536 529 return rc; 537 530 } 538 531 539 /** Set cluster in one instance of FAT. FAT16 version.532 /** Set cluster in one instance of FAT. 540 533 * 541 534 * @param bs Buffer holding the boot sector for the file system. … … 566 559 b->dirty = true; /* need to sync block */ 567 560 rc = block_put(b); 561 568 562 return rc; 569 563 } 570 564 571 /** Set cluster in one instance of FAT. FAT32 version.565 /** Set cluster in one instance of FAT. 572 566 * 573 567 * @param bs Buffer holding the boot sector for the file system. … … 602 596 b->dirty = true; /* need to sync block */ 603 597 rc = block_put(b); 598 604 599 return rc; 605 600 } … … 625 620 if (FAT_IS_FAT12(bs)) 626 621 rc = fat_set_cluster_fat12(bs, service_id, fatno, clst, value); 627 else if (FAT_IS_FAT32(bs)) 622 else if (FAT_IS_FAT16(bs)) 623 rc = fat_set_cluster_fat16(bs, service_id, fatno, clst, value); 624 else 628 625 rc = fat_set_cluster_fat32(bs, service_id, fatno, clst, value); 629 else630 rc = fat_set_cluster_fat16(bs, service_id, fatno, clst, value);631 626 632 627 return rc; … … 695 690 */ 696 691 fibril_mutex_lock(&fat_alloc_lock); 697 for (clst=FAT_CLST_FIRST; clst < CC(bs)+2 && found < nclsts; clst++) { 692 for (clst = FAT_CLST_FIRST; clst < CC(bs) + 2 && found < nclsts; 693 clst++) { 698 694 rc = fat_get_cluster(bs, service_id, FAT1, clst, &value); 699 695 if (rc != EOK) 700 break;696 break; 701 697 702 698 if (value == FAT_CLST_RES0) { 703 /*704 * The cluster is free. Put it into our stack705 * of found clusters and mark it as non-free.706 */707 lifo[found] = clst;708 rc = fat_set_cluster(bs, service_id, FAT1, clst,709 (found == 0) ? clst_last1 : lifo[found - 1]);710 if (rc != EOK)711 break;712 713 found++;699 /* 700 * The cluster is free. Put it into our stack 701 * of found clusters and mark it as non-free. 702 */ 703 lifo[found] = clst; 704 rc = fat_set_cluster(bs, service_id, FAT1, clst, 705 (found == 0) ? clst_last1 : lifo[found - 1]); 706 if (rc != EOK) 707 break; 708 709 found++; 714 710 } 715 711 } … … 727 723 728 724 /* If something wrong - free the clusters */ 729 if (found > 0) { 730 while (found--) { 731 rc = fat_set_cluster(bs, service_id, FAT1, lifo[found], 725 while (found--) { 726 (void) fat_set_cluster(bs, service_id, FAT1, lifo[found], 732 727 FAT_CLST_RES0); 733 }734 728 } 735 729 736 730 free(lifo); 737 731 fibril_mutex_unlock(&fat_alloc_lock); 732 738 733 return ENOSPC; 739 734 } … … 757 752 while (firstc < FAT_CLST_LAST1(bs)) { 758 753 assert(firstc >= FAT_CLST_FIRST && firstc < clst_bad); 754 759 755 rc = fat_get_cluster(bs, service_id, FAT1, firstc, &nextc); 760 756 if (rc != EOK) 761 757 return rc; 758 762 759 for (fatno = FAT1; fatno < FATCNT(bs); fatno++) { 763 760 rc = fat_set_cluster(bs, service_id, fatno, firstc, … … 766 763 return rc; 767 764 } 768 769 765 firstc = nextc; 770 766 } … … 782 778 * @return EOK on success or a negative error code. 783 779 */ 784 int 785 fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, 780 int fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, 786 781 fat_cluster_t lcl) 787 782 { … … 942 937 * sanitized to support file systems with this property. 943 938 */ 944 if (!FAT_IS_FAT32(bs) && (RDE(bs) * sizeof(fat_dentry_t)) % BPS(bs) != 0) 939 if (!FAT_IS_FAT32(bs) && 940 (RDE(bs) * sizeof(fat_dentry_t)) % BPS(bs) != 0) 945 941 return ENOTSUP; 946 942 … … 955 951 return EIO; 956 952 957 /* Check that first byte of FAT contains the media descriptor. */ 953 /* 954 * Check that first byte of FAT contains the media descriptor. 955 */ 958 956 if ((e0 & 0xff) != bs->mdesc) 959 957 return ENOTSUP; … … 964 962 */ 965 963 if (!FAT_IS_FAT12(bs) && 966 964 ((e0 >> 8) != (FAT_MASK(bs) >> 8) || e1 != FAT_MASK(bs))) 967 965 return ENOTSUP; 968 966 }
Note:
See TracChangeset
for help on using the changeset viewer.