Changeset 6bc542b in mainline for uspace/lib/fdisk/src/fdisk.c
- Timestamp:
- 2015-07-02T21:53:12Z (9 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 99c23405
- Parents:
- 6a0d4ce2
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/fdisk/src/fdisk.c
r6a0d4ce2 r6bc542b 58 58 }; 59 59 60 static int fdisk_part_spec_prepare(fdisk_dev_t *, fdisk_part_spec_t *, 61 vbd_part_spec_t *); 62 60 63 static void fdisk_dev_info_delete(fdisk_dev_info_t *info) 61 64 { … … 497 500 int rc; 498 501 499 part = calloc(1, sizeof(fdisk_part_t)); 500 if (part == NULL) 501 return ENOMEM; 502 502 printf("fdisk_part_create()\n"); 503 504 rc = fdisk_part_spec_prepare(dev, pspec, &vpspec); 505 if (rc != EOK) 506 return EIO; 507 508 printf("fdisk_part_create() - call vbd_part_create\n"); 503 509 rc = vbd_part_create(dev->fdisk->vbd, dev->sid, &vpspec, &partid); 504 if (rc != EOK) { 505 free(part); 506 return EIO; 507 } 508 509 rc = fdisk_part_add(dev, partid, rpart); 510 if (rc != EOK) 511 return EIO; 512 513 printf("fdisk_part_create() - call fdisk_part_add\n"); 514 rc = fdisk_part_add(dev, partid, &part); 510 515 if (rc != EOK) { 511 516 /* Try rolling back */ … … 514 519 } 515 520 516 (*rpart)->fstype = pspec->fstype; 517 (*rpart)->capacity = pspec->capacity; 518 521 printf("fdisk_part_create() - done\n"); 522 part->fstype = pspec->fstype; 523 part->capacity = pspec->capacity; 524 525 if (rpart != NULL) 526 *rpart = part; 519 527 return EOK; 520 528 } … … 650 658 } 651 659 660 /** Get free partition index. */ 661 static int fdisk_part_get_free_idx(fdisk_dev_t *dev, int *rindex) 662 { 663 link_t *link; 664 fdisk_part_t *part; 665 int nidx; 666 667 link = list_first(&dev->parts_idx); 668 nidx = 1; 669 while (link != NULL) { 670 part = list_get_instance(link, fdisk_part_t, ldev_idx); 671 if (part->index > nidx) 672 break; 673 nidx = part->index; 674 link = list_next(link, &dev->parts_idx); 675 } 676 677 if (nidx > 4 /* XXXX actual number of slots*/) { 678 return ELIMIT; 679 } 680 681 *rindex = nidx; 682 return EOK; 683 } 684 685 /** Get free range of blocks. 686 * 687 * Get free range of blocks of at least the specified size (first fit). 688 */ 689 static int fdisk_part_get_free_range(fdisk_dev_t *dev, aoff64_t nblocks, 690 aoff64_t *rblock0, aoff64_t *rnblocks) 691 { 692 link_t *link; 693 fdisk_part_t *part; 694 uint64_t avail; 695 int nba; 696 697 link = list_first(&dev->parts_ba); 698 nba = dev->dinfo.ablock0; 699 while (link != NULL) { 700 part = list_get_instance(link, fdisk_part_t, ldev_ba); 701 if (part->block0 - nba >= nblocks) 702 break; 703 nba = part->block0 + part->nblocks; 704 link = list_next(link, &dev->parts_ba); 705 } 706 707 if (link != NULL) { 708 /* Free range before a partition */ 709 avail = part->block0 - nba; 710 } else { 711 /* Free range at the end */ 712 avail = dev->dinfo.ablock0 + dev->dinfo.anblocks - nba; 713 714 /* Verify that the range is large enough */ 715 if (avail < nblocks) 716 return ELIMIT; 717 } 718 719 *rblock0 = nba; 720 *rnblocks = avail; 721 return EOK; 722 } 723 724 /** Prepare new partition specification for VBD. */ 725 static int fdisk_part_spec_prepare(fdisk_dev_t *dev, fdisk_part_spec_t *pspec, 726 vbd_part_spec_t *vpspec) 727 { 728 uint64_t cbytes; 729 aoff64_t req_blocks; 730 aoff64_t fblock0; 731 aoff64_t fnblocks; 732 uint64_t block_size; 733 unsigned i; 734 int index; 735 int rc; 736 737 // pspec->fstype 738 printf("fdisk_part_spec_prepare()\n"); 739 block_size = dev->dinfo.block_size; 740 cbytes = pspec->capacity.value; 741 for (i = 0; i < pspec->capacity.cunit; i++) 742 cbytes = cbytes * 1000; 743 744 req_blocks = (cbytes + block_size - 1) / block_size; 745 746 rc = fdisk_part_get_free_idx(dev, &index); 747 if (rc != EOK) 748 return EIO; 749 750 rc = fdisk_part_get_free_range(dev, req_blocks, &fblock0, &fnblocks); 751 if (rc != EOK) 752 return EIO; 753 754 vpspec->index = index; 755 vpspec->block0 = fblock0; 756 vpspec->nblocks = req_blocks; 757 vpspec->ptype = 42; 758 return EOK; 759 } 760 652 761 /** @} 653 762 */
Note:
See TracChangeset
for help on using the changeset viewer.