extent_map.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include <linux/err.h>
  2. #include <linux/slab.h>
  3. #include <linux/module.h>
  4. #include <linux/spinlock.h>
  5. #include <linux/hardirq.h>
  6. #include "ctree.h"
  7. #include "extent_map.h"
  8. static struct kmem_cache *extent_map_cache;
  9. int __init extent_map_init(void)
  10. {
  11. extent_map_cache = kmem_cache_create("extent_map",
  12. sizeof(struct extent_map), 0,
  13. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  14. if (!extent_map_cache)
  15. return -ENOMEM;
  16. return 0;
  17. }
  18. void extent_map_exit(void)
  19. {
  20. if (extent_map_cache)
  21. kmem_cache_destroy(extent_map_cache);
  22. }
  23. /**
  24. * extent_map_tree_init - initialize extent map tree
  25. * @tree: tree to initialize
  26. *
  27. * Initialize the extent tree @tree. Should be called for each new inode
  28. * or other user of the extent_map interface.
  29. */
  30. void extent_map_tree_init(struct extent_map_tree *tree)
  31. {
  32. tree->map = RB_ROOT;
  33. rwlock_init(&tree->lock);
  34. }
  35. /**
  36. * alloc_extent_map - allocate new extent map structure
  37. *
  38. * Allocate a new extent_map structure. The new structure is
  39. * returned with a reference count of one and needs to be
  40. * freed using free_extent_map()
  41. */
  42. struct extent_map *alloc_extent_map(void)
  43. {
  44. struct extent_map *em;
  45. em = kmem_cache_alloc(extent_map_cache, GFP_NOFS);
  46. if (!em)
  47. return NULL;
  48. em->in_tree = 0;
  49. em->flags = 0;
  50. em->compress_type = BTRFS_COMPRESS_NONE;
  51. atomic_set(&em->refs, 1);
  52. return em;
  53. }
  54. /**
  55. * free_extent_map - drop reference count of an extent_map
  56. * @em: extent map beeing releasead
  57. *
  58. * Drops the reference out on @em by one and free the structure
  59. * if the reference count hits zero.
  60. */
  61. void free_extent_map(struct extent_map *em)
  62. {
  63. if (!em)
  64. return;
  65. WARN_ON(atomic_read(&em->refs) == 0);
  66. if (atomic_dec_and_test(&em->refs)) {
  67. WARN_ON(em->in_tree);
  68. kmem_cache_free(extent_map_cache, em);
  69. }
  70. }
  71. static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
  72. struct rb_node *node)
  73. {
  74. struct rb_node **p = &root->rb_node;
  75. struct rb_node *parent = NULL;
  76. struct extent_map *entry;
  77. while (*p) {
  78. parent = *p;
  79. entry = rb_entry(parent, struct extent_map, rb_node);
  80. WARN_ON(!entry->in_tree);
  81. if (offset < entry->start)
  82. p = &(*p)->rb_left;
  83. else if (offset >= extent_map_end(entry))
  84. p = &(*p)->rb_right;
  85. else
  86. return parent;
  87. }
  88. entry = rb_entry(node, struct extent_map, rb_node);
  89. entry->in_tree = 1;
  90. rb_link_node(node, parent, p);
  91. rb_insert_color(node, root);
  92. return NULL;
  93. }
  94. /*
  95. * search through the tree for an extent_map with a given offset. If
  96. * it can't be found, try to find some neighboring extents
  97. */
  98. static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
  99. struct rb_node **prev_ret,
  100. struct rb_node **next_ret)
  101. {
  102. struct rb_node *n = root->rb_node;
  103. struct rb_node *prev = NULL;
  104. struct rb_node *orig_prev = NULL;
  105. struct extent_map *entry;
  106. struct extent_map *prev_entry = NULL;
  107. while (n) {
  108. entry = rb_entry(n, struct extent_map, rb_node);
  109. prev = n;
  110. prev_entry = entry;
  111. WARN_ON(!entry->in_tree);
  112. if (offset < entry->start)
  113. n = n->rb_left;
  114. else if (offset >= extent_map_end(entry))
  115. n = n->rb_right;
  116. else
  117. return n;
  118. }
  119. if (prev_ret) {
  120. orig_prev = prev;
  121. while (prev && offset >= extent_map_end(prev_entry)) {
  122. prev = rb_next(prev);
  123. prev_entry = rb_entry(prev, struct extent_map, rb_node);
  124. }
  125. *prev_ret = prev;
  126. prev = orig_prev;
  127. }
  128. if (next_ret) {
  129. prev_entry = rb_entry(prev, struct extent_map, rb_node);
  130. while (prev && offset < prev_entry->start) {
  131. prev = rb_prev(prev);
  132. prev_entry = rb_entry(prev, struct extent_map, rb_node);
  133. }
  134. *next_ret = prev;
  135. }
  136. return NULL;
  137. }
  138. /* check to see if two extent_map structs are adjacent and safe to merge */
  139. static int mergable_maps(struct extent_map *prev, struct extent_map *next)
  140. {
  141. if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
  142. return 0;
  143. /*
  144. * don't merge compressed extents, we need to know their
  145. * actual size
  146. */
  147. if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
  148. return 0;
  149. if (extent_map_end(prev) == next->start &&
  150. prev->flags == next->flags &&
  151. prev->bdev == next->bdev &&
  152. ((next->block_start == EXTENT_MAP_HOLE &&
  153. prev->block_start == EXTENT_MAP_HOLE) ||
  154. (next->block_start == EXTENT_MAP_INLINE &&
  155. prev->block_start == EXTENT_MAP_INLINE) ||
  156. (next->block_start == EXTENT_MAP_DELALLOC &&
  157. prev->block_start == EXTENT_MAP_DELALLOC) ||
  158. (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
  159. next->block_start == extent_map_block_end(prev)))) {
  160. return 1;
  161. }
  162. return 0;
  163. }
  164. static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
  165. {
  166. struct extent_map *merge = NULL;
  167. struct rb_node *rb;
  168. if (em->start != 0) {
  169. rb = rb_prev(&em->rb_node);
  170. if (rb)
  171. merge = rb_entry(rb, struct extent_map, rb_node);
  172. if (rb && mergable_maps(merge, em)) {
  173. em->start = merge->start;
  174. em->len += merge->len;
  175. em->block_len += merge->block_len;
  176. em->block_start = merge->block_start;
  177. merge->in_tree = 0;
  178. rb_erase(&merge->rb_node, &tree->map);
  179. free_extent_map(merge);
  180. }
  181. }
  182. rb = rb_next(&em->rb_node);
  183. if (rb)
  184. merge = rb_entry(rb, struct extent_map, rb_node);
  185. if (rb && mergable_maps(em, merge)) {
  186. em->len += merge->len;
  187. em->block_len += merge->len;
  188. rb_erase(&merge->rb_node, &tree->map);
  189. merge->in_tree = 0;
  190. free_extent_map(merge);
  191. }
  192. }
  193. int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len)
  194. {
  195. int ret = 0;
  196. struct extent_map *em;
  197. write_lock(&tree->lock);
  198. em = lookup_extent_mapping(tree, start, len);
  199. WARN_ON(!em || em->start != start);
  200. if (!em)
  201. goto out;
  202. clear_bit(EXTENT_FLAG_PINNED, &em->flags);
  203. try_merge_map(tree, em);
  204. free_extent_map(em);
  205. out:
  206. write_unlock(&tree->lock);
  207. return ret;
  208. }
  209. /**
  210. * add_extent_mapping - add new extent map to the extent tree
  211. * @tree: tree to insert new map in
  212. * @em: map to insert
  213. *
  214. * Insert @em into @tree or perform a simple forward/backward merge with
  215. * existing mappings. The extent_map struct passed in will be inserted
  216. * into the tree directly, with an additional reference taken, or a
  217. * reference dropped if the merge attempt was successful.
  218. */
  219. int add_extent_mapping(struct extent_map_tree *tree,
  220. struct extent_map *em)
  221. {
  222. int ret = 0;
  223. struct rb_node *rb;
  224. struct extent_map *exist;
  225. exist = lookup_extent_mapping(tree, em->start, em->len);
  226. if (exist) {
  227. free_extent_map(exist);
  228. ret = -EEXIST;
  229. goto out;
  230. }
  231. rb = tree_insert(&tree->map, em->start, &em->rb_node);
  232. if (rb) {
  233. ret = -EEXIST;
  234. goto out;
  235. }
  236. atomic_inc(&em->refs);
  237. try_merge_map(tree, em);
  238. out:
  239. return ret;
  240. }
  241. /* simple helper to do math around the end of an extent, handling wrap */
  242. static u64 range_end(u64 start, u64 len)
  243. {
  244. if (start + len < start)
  245. return (u64)-1;
  246. return start + len;
  247. }
  248. struct extent_map *__lookup_extent_mapping(struct extent_map_tree *tree,
  249. u64 start, u64 len, int strict)
  250. {
  251. struct extent_map *em;
  252. struct rb_node *rb_node;
  253. struct rb_node *prev = NULL;
  254. struct rb_node *next = NULL;
  255. u64 end = range_end(start, len);
  256. rb_node = __tree_search(&tree->map, start, &prev, &next);
  257. if (!rb_node) {
  258. if (prev)
  259. rb_node = prev;
  260. else if (next)
  261. rb_node = next;
  262. else
  263. return NULL;
  264. }
  265. em = rb_entry(rb_node, struct extent_map, rb_node);
  266. if (strict && !(end > em->start && start < extent_map_end(em)))
  267. return NULL;
  268. atomic_inc(&em->refs);
  269. return em;
  270. }
  271. /**
  272. * lookup_extent_mapping - lookup extent_map
  273. * @tree: tree to lookup in
  274. * @start: byte offset to start the search
  275. * @len: length of the lookup range
  276. *
  277. * Find and return the first extent_map struct in @tree that intersects the
  278. * [start, len] range. There may be additional objects in the tree that
  279. * intersect, so check the object returned carefully to make sure that no
  280. * additional lookups are needed.
  281. */
  282. struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
  283. u64 start, u64 len)
  284. {
  285. return __lookup_extent_mapping(tree, start, len, 1);
  286. }
  287. /**
  288. * search_extent_mapping - find a nearby extent map
  289. * @tree: tree to lookup in
  290. * @start: byte offset to start the search
  291. * @len: length of the lookup range
  292. *
  293. * Find and return the first extent_map struct in @tree that intersects the
  294. * [start, len] range.
  295. *
  296. * If one can't be found, any nearby extent may be returned
  297. */
  298. struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
  299. u64 start, u64 len)
  300. {
  301. return __lookup_extent_mapping(tree, start, len, 0);
  302. }
  303. /**
  304. * remove_extent_mapping - removes an extent_map from the extent tree
  305. * @tree: extent tree to remove from
  306. * @em: extent map beeing removed
  307. *
  308. * Removes @em from @tree. No reference counts are dropped, and no checks
  309. * are done to see if the range is in use
  310. */
  311. int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
  312. {
  313. int ret = 0;
  314. WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
  315. rb_erase(&em->rb_node, &tree->map);
  316. em->in_tree = 0;
  317. return ret;
  318. }