ctree.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #ifndef __CTREE__
  2. #define __CTREE__
  3. #include "list.h"
  4. #define CTREE_BLOCKSIZE 1024
  5. /*
  6. * the key defines the order in the tree, and so it also defines (optimal)
  7. * block layout. objectid corresonds to the inode number. The flags
  8. * tells us things about the object, and is a kind of stream selector.
  9. * so for a given inode, keys with flags of 1 might refer to the inode
  10. * data, flags of 2 may point to file data in the btree and flags == 3
  11. * may point to extents.
  12. *
  13. * offset is the starting byte offset for this key in the stream.
  14. */
  15. struct key {
  16. u64 objectid;
  17. u32 flags;
  18. u64 offset;
  19. } __attribute__ ((__packed__));
  20. /*
  21. * every tree block (leaf or node) starts with this header.
  22. */
  23. struct header {
  24. u64 fsid[2]; /* FS specific uuid */
  25. u64 blocknr; /* which block this node is supposed to live in */
  26. u64 parentid; /* objectid of the tree root */
  27. u32 csum;
  28. u32 ham;
  29. u16 nritems;
  30. u16 flags;
  31. /* generation flags to be added */
  32. } __attribute__ ((__packed__));
  33. #define NODEPTRS_PER_BLOCK ((CTREE_BLOCKSIZE - sizeof(struct header)) / \
  34. (sizeof(struct key) + sizeof(u64)))
  35. #define MAX_LEVEL 8
  36. #define node_level(f) ((f) & (MAX_LEVEL-1))
  37. #define is_leaf(f) (node_level(f) == 0)
  38. struct tree_buffer;
  39. /*
  40. * in ram representation of the tree. extent_root is used for all allocations
  41. * and for the extent tree extent_root root. current_insert is used
  42. * only for the extent tree.
  43. */
  44. struct ctree_root {
  45. struct tree_buffer *node;
  46. struct ctree_root *extent_root;
  47. struct key current_insert;
  48. int fp;
  49. struct radix_tree_root cache_radix;
  50. struct list_head trans;
  51. struct list_head cache;
  52. int cache_size;
  53. };
  54. /*
  55. * describes a tree on disk
  56. */
  57. struct ctree_root_info {
  58. u64 fsid[2]; /* FS specific uuid */
  59. u64 blocknr; /* blocknr of this block */
  60. u64 objectid; /* inode number of this root */
  61. u64 tree_root; /* the tree root block */
  62. u32 csum;
  63. u32 ham;
  64. u64 snapuuid[2]; /* root specific uuid */
  65. } __attribute__ ((__packed__));
  66. /*
  67. * the super block basically lists the main trees of the FS
  68. * it currently lacks any block count etc etc
  69. */
  70. struct ctree_super_block {
  71. struct ctree_root_info root_info;
  72. struct ctree_root_info extent_info;
  73. } __attribute__ ((__packed__));
  74. /*
  75. * A leaf is full of items. The exact type of item is defined by
  76. * the key flags parameter. offset and size tell us where to find
  77. * the item in the leaf (relative to the start of the data area)
  78. */
  79. struct item {
  80. struct key key;
  81. u16 offset;
  82. u16 size;
  83. } __attribute__ ((__packed__));
  84. /*
  85. * leaves have an item area and a data area:
  86. * [item0, item1....itemN] [free space] [dataN...data1, data0]
  87. *
  88. * The data is separate from the items to get the keys closer together
  89. * during searches.
  90. */
  91. #define LEAF_DATA_SIZE (CTREE_BLOCKSIZE - sizeof(struct header))
  92. struct leaf {
  93. struct header header;
  94. union {
  95. struct item items[LEAF_DATA_SIZE/sizeof(struct item)];
  96. u8 data[CTREE_BLOCKSIZE-sizeof(struct header)];
  97. };
  98. } __attribute__ ((__packed__));
  99. /*
  100. * all non-leaf blocks are nodes, they hold only keys and pointers to
  101. * other blocks
  102. */
  103. struct node {
  104. struct header header;
  105. struct key keys[NODEPTRS_PER_BLOCK];
  106. u64 blockptrs[NODEPTRS_PER_BLOCK];
  107. } __attribute__ ((__packed__));
  108. /*
  109. * items in the extent btree are used to record the objectid of the
  110. * owner of the block and the number of references
  111. */
  112. struct extent_item {
  113. u32 refs;
  114. u64 owner;
  115. } __attribute__ ((__packed__));
  116. /*
  117. * ctree_paths remember the path taken from the root down to the leaf.
  118. * level 0 is always the leaf, and nodes[1...MAX_LEVEL] will point
  119. * to any other levels that are present.
  120. *
  121. * The slots array records the index of the item or block pointer
  122. * used while walking the tree.
  123. */
  124. struct ctree_path {
  125. struct tree_buffer *nodes[MAX_LEVEL];
  126. int slots[MAX_LEVEL];
  127. };
  128. struct tree_buffer *alloc_free_block(struct ctree_root *root);
  129. int btrfs_inc_ref(struct ctree_root *root, struct tree_buffer *buf);
  130. int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks);
  131. int search_slot(struct ctree_root *root, struct key *key, struct ctree_path *p, int ins_len, int cow);
  132. void release_path(struct ctree_root *root, struct ctree_path *p);
  133. void init_path(struct ctree_path *p);
  134. int del_item(struct ctree_root *root, struct ctree_path *path);
  135. int insert_item(struct ctree_root *root, struct key *key, void *data, int data_size);
  136. int next_leaf(struct ctree_root *root, struct ctree_path *path);
  137. int leaf_free_space(struct leaf *leaf);
  138. #endif