ctree.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 tree_buffer *commit_root;
  47. struct ctree_root *extent_root;
  48. struct key current_insert;
  49. struct key last_insert;
  50. int fp;
  51. struct radix_tree_root cache_radix;
  52. struct radix_tree_root pinned_radix;
  53. struct list_head trans;
  54. struct list_head cache;
  55. int cache_size;
  56. };
  57. /*
  58. * describes a tree on disk
  59. */
  60. struct ctree_root_info {
  61. u64 fsid[2]; /* FS specific uuid */
  62. u64 blocknr; /* blocknr of this block */
  63. u64 objectid; /* inode number of this root */
  64. u64 tree_root; /* the tree root block */
  65. u32 csum;
  66. u32 ham;
  67. u64 snapuuid[2]; /* root specific uuid */
  68. } __attribute__ ((__packed__));
  69. /*
  70. * the super block basically lists the main trees of the FS
  71. * it currently lacks any block count etc etc
  72. */
  73. struct ctree_super_block {
  74. struct ctree_root_info root_info;
  75. struct ctree_root_info extent_info;
  76. } __attribute__ ((__packed__));
  77. /*
  78. * A leaf is full of items. The exact type of item is defined by
  79. * the key flags parameter. offset and size tell us where to find
  80. * the item in the leaf (relative to the start of the data area)
  81. */
  82. struct item {
  83. struct key key;
  84. u16 offset;
  85. u16 size;
  86. } __attribute__ ((__packed__));
  87. /*
  88. * leaves have an item area and a data area:
  89. * [item0, item1....itemN] [free space] [dataN...data1, data0]
  90. *
  91. * The data is separate from the items to get the keys closer together
  92. * during searches.
  93. */
  94. #define LEAF_DATA_SIZE (CTREE_BLOCKSIZE - sizeof(struct header))
  95. struct leaf {
  96. struct header header;
  97. union {
  98. struct item items[LEAF_DATA_SIZE/sizeof(struct item)];
  99. u8 data[CTREE_BLOCKSIZE-sizeof(struct header)];
  100. };
  101. } __attribute__ ((__packed__));
  102. /*
  103. * all non-leaf blocks are nodes, they hold only keys and pointers to
  104. * other blocks
  105. */
  106. struct node {
  107. struct header header;
  108. struct key keys[NODEPTRS_PER_BLOCK];
  109. u64 blockptrs[NODEPTRS_PER_BLOCK];
  110. } __attribute__ ((__packed__));
  111. /*
  112. * items in the extent btree are used to record the objectid of the
  113. * owner of the block and the number of references
  114. */
  115. struct extent_item {
  116. u32 refs;
  117. u64 owner;
  118. } __attribute__ ((__packed__));
  119. /*
  120. * ctree_paths remember the path taken from the root down to the leaf.
  121. * level 0 is always the leaf, and nodes[1...MAX_LEVEL] will point
  122. * to any other levels that are present.
  123. *
  124. * The slots array records the index of the item or block pointer
  125. * used while walking the tree.
  126. */
  127. struct ctree_path {
  128. struct tree_buffer *nodes[MAX_LEVEL];
  129. int slots[MAX_LEVEL];
  130. };
  131. struct tree_buffer *alloc_free_block(struct ctree_root *root);
  132. int btrfs_inc_ref(struct ctree_root *root, struct tree_buffer *buf);
  133. int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks);
  134. int search_slot(struct ctree_root *root, struct key *key, struct ctree_path *p, int ins_len, int cow);
  135. void release_path(struct ctree_root *root, struct ctree_path *p);
  136. void init_path(struct ctree_path *p);
  137. int del_item(struct ctree_root *root, struct ctree_path *path);
  138. int insert_item(struct ctree_root *root, struct key *key, void *data, int data_size);
  139. int next_leaf(struct ctree_root *root, struct ctree_path *path);
  140. int leaf_free_space(struct leaf *leaf);
  141. int btrfs_drop_snapshot(struct ctree_root *root, struct tree_buffer *snap);
  142. int btrfs_finish_extent_commit(struct ctree_root *root);
  143. #endif