mkfs.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #define _XOPEN_SOURCE 500
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include "kerncompat.h"
  9. #include "radix-tree.h"
  10. #include "ctree.h"
  11. #include "disk-io.h"
  12. int mkfs(int fd)
  13. {
  14. struct ctree_root_info info[2];
  15. struct leaf empty_leaf;
  16. struct item item;
  17. struct extent_item extent_item;
  18. int ret;
  19. /* setup the super block area */
  20. memset(info, 0, sizeof(info));
  21. info[0].blocknr = 16;
  22. info[0].objectid = 1;
  23. info[0].tree_root = 17;
  24. info[0].alloc_extent.blocknr = 0;
  25. info[0].alloc_extent.num_blocks = 64;
  26. /* 0-17 are used (inclusive) */
  27. info[0].alloc_extent.num_used = 18;
  28. info[1].blocknr = 16;
  29. info[1].objectid = 2;
  30. info[1].tree_root = 64;
  31. info[1].alloc_extent.blocknr = 64;
  32. info[1].alloc_extent.num_blocks = 64;
  33. info[1].alloc_extent.num_used = 1;
  34. ret = pwrite(fd, info, sizeof(info),
  35. CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
  36. if (ret != sizeof(info))
  37. return -1;
  38. /* create leaves for the tree root and extent root */
  39. memset(&empty_leaf, 0, sizeof(empty_leaf));
  40. empty_leaf.header.parentid = 1;
  41. empty_leaf.header.blocknr = 17;
  42. ret = pwrite(fd, &empty_leaf, sizeof(empty_leaf), 17 * CTREE_BLOCKSIZE);
  43. if (ret != sizeof(empty_leaf))
  44. return -1;
  45. empty_leaf.header.parentid = 2;
  46. empty_leaf.header.blocknr = 64;
  47. empty_leaf.header.nritems = 2;
  48. item.key.objectid = 0;
  49. item.key.offset = 64;
  50. item.key.flags = 0;
  51. item.offset = LEAF_DATA_SIZE - sizeof(struct extent_item);
  52. item.size = sizeof(struct extent_item);
  53. extent_item.refs = 1;
  54. extent_item.owner = 1;
  55. memcpy(empty_leaf.items, &item, sizeof(item));
  56. memcpy(empty_leaf.data + item.offset, &extent_item, item.size);
  57. item.key.objectid = 64;
  58. item.key.offset = 64;
  59. item.offset = LEAF_DATA_SIZE - sizeof(struct extent_item) * 2;
  60. extent_item.owner = 2;
  61. memcpy(empty_leaf.items + 1, &item, sizeof(item));
  62. memcpy(empty_leaf.data + item.offset, &extent_item, item.size);
  63. ret = pwrite(fd, &empty_leaf, sizeof(empty_leaf), 64 * CTREE_BLOCKSIZE);
  64. if (ret != sizeof(empty_leaf))
  65. return -1;
  66. return 0;
  67. }