extent_map.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len)
  165. {
  166. int ret = 0;
  167. struct extent_map *merge = NULL;
  168. struct rb_node *rb;
  169. struct extent_map *em;
  170. write_lock(&tree->lock);
  171. em = lookup_extent_mapping(tree, start, len);
  172. WARN_ON(!em || em->start != start);
  173. if (!em)
  174. goto out;
  175. clear_bit(EXTENT_FLAG_PINNED, &em->flags);
  176. if (em->start != 0) {
  177. rb = rb_prev(&em->rb_node);
  178. if (rb)
  179. merge = rb_entry(rb, struct extent_map, rb_node);
  180. if (rb && mergable_maps(merge, em)) {
  181. em->start = merge->start;
  182. em->len += merge->len;
  183. em->block_len += merge->block_len;
  184. em->block_start = merge->block_start;
  185. merge->in_tree = 0;
  186. rb_erase(&merge->rb_node, &tree->map);
  187. free_extent_map(merge);
  188. }
  189. }
  190. rb = rb_next(&em->rb_node);
  191. if (rb)
  192. merge = rb_entry(rb, struct extent_map, rb_node);
  193. if (rb && mergable_maps(em, merge)) {
  194. em->len += merge->len;
  195. em->block_len += merge->len;
  196. rb_erase(&merge->rb_node, &tree->map);
  197. merge->in_tree = 0;
  198. free_extent_map(merge);
  199. }
  200. free_extent_map(em);
  201. out:
  202. write_unlock(&tree->lock);
  203. return ret;
  204. }
  205. /**
  206. * add_extent_mapping - add new extent map to the extent tree
  207. * @tree: tree to insert new map in
  208. * @em: map to insert
  209. *
  210. * Insert @em into @tree or perform a simple forward/backward merge with
  211. * existing mappings. The extent_map struct passed in will be inserted
  212. * into the tree directly, with an additional reference taken, or a
  213. * reference dropped if the merge attempt was successful.
  214. */
  215. int add_extent_mapping(struct extent_map_tree *tree,
  216. struct extent_map *em)
  217. {
  218. int ret = 0;
  219. struct extent_map *merge = NULL;
  220. struct rb_node *rb;
  221. struct extent_map *exist;
  222. exist = lookup_extent_mapping(tree, em->start, em->len);
  223. if (exist) {
  224. free_extent_map(exist);
  225. ret = -EEXIST;
  226. goto out;
  227. }
  228. rb = tree_insert(&tree->map, em->start, &em->rb_node);
  229. if (rb) {
  230. ret = -EEXIST;
  231. goto out;
  232. }
  233. atomic_inc(&em->refs);
  234. if (em->start != 0) {
  235. rb = rb_prev(&em->rb_node);
  236. if (rb)
  237. merge = rb_entry(rb, struct extent_map, rb_node);
  238. if (rb && mergable_maps(merge, em)) {
  239. em->start = merge->start;
  240. em->len += merge->len;
  241. em->block_len += merge->block_len;
  242. em->block_start = merge->block_start;
  243. merge->in_tree = 0;
  244. rb_erase(&merge->rb_node, &tree->map);
  245. free_extent_map(merge);
  246. }
  247. }
  248. rb = rb_next(&em->rb_node);
  249. if (rb)
  250. merge = rb_entry(rb, struct extent_map, rb_node);
  251. if (rb && mergable_maps(em, merge)) {
  252. em->len += merge->len;
  253. em->block_len += merge->len;
  254. rb_erase(&merge->rb_node, &tree->map);
  255. merge->in_tree = 0;
  256. free_extent_map(merge);
  257. }
  258. out:
  259. return ret;
  260. }
  261. /* simple helper to do math around the end of an extent, handling wrap */
  262. static u64 range_end(u64 start, u64 len)
  263. {
  264. if (start + len < start)
  265. return (u64)-1;
  266. return start + len;
  267. }
  268. /**
  269. * lookup_extent_mapping - lookup extent_map
  270. * @tree: tree to lookup in
  271. * @start: byte offset to start the search
  272. * @len: length of the lookup range
  273. *
  274. * Find and return the first extent_map struct in @tree that intersects the
  275. * [start, len] range. There may be additional objects in the tree that
  276. * intersect, so check the object returned carefully to make sure that no
  277. * additional lookups are needed.
  278. */
  279. struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
  280. u64 start, u64 len)
  281. {
  282. struct extent_map *em;
  283. struct rb_node *rb_node;
  284. struct rb_node *prev = NULL;
  285. struct rb_node *next = NULL;
  286. u64 end = range_end(start, len);
  287. rb_node = __tree_search(&tree->map, start, &prev, &next);
  288. if (!rb_node && prev) {
  289. em = rb_entry(prev, struct extent_map, rb_node);
  290. if (end > em->start && start < extent_map_end(em))
  291. goto found;
  292. }
  293. if (!rb_node && next) {
  294. em = rb_entry(next, struct extent_map, rb_node);
  295. if (end > em->start && start < extent_map_end(em))
  296. goto found;
  297. }
  298. if (!rb_node) {
  299. em = NULL;
  300. goto out;
  301. }
  302. if (IS_ERR(rb_node)) {
  303. em = ERR_CAST(rb_node);
  304. goto out;
  305. }
  306. em = rb_entry(rb_node, struct extent_map, rb_node);
  307. if (end > em->start && start < extent_map_end(em))
  308. goto found;
  309. em = NULL;
  310. goto out;
  311. found:
  312. atomic_inc(&em->refs);
  313. out:
  314. return em;
  315. }
  316. /**
  317. * search_extent_mapping - find a nearby extent map
  318. * @tree: tree to lookup in
  319. * @start: byte offset to start the search
  320. * @len: length of the lookup range
  321. *
  322. * Find and return the first extent_map struct in @tree that intersects the
  323. * [start, len] range.
  324. *
  325. * If one can't be found, any nearby extent may be returned
  326. */
  327. struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
  328. u64 start, u64 len)
  329. {
  330. struct extent_map *em;
  331. struct rb_node *rb_node;
  332. struct rb_node *prev = NULL;
  333. struct rb_node *next = NULL;
  334. rb_node = __tree_search(&tree->map, start, &prev, &next);
  335. if (!rb_node && prev) {
  336. em = rb_entry(prev, struct extent_map, rb_node);
  337. goto found;
  338. }
  339. if (!rb_node && next) {
  340. em = rb_entry(next, struct extent_map, rb_node);
  341. goto found;
  342. }
  343. if (!rb_node) {
  344. em = NULL;
  345. goto out;
  346. }
  347. if (IS_ERR(rb_node)) {
  348. em = ERR_CAST(rb_node);
  349. goto out;
  350. }
  351. em = rb_entry(rb_node, struct extent_map, rb_node);
  352. goto found;
  353. em = NULL;
  354. goto out;
  355. found:
  356. atomic_inc(&em->refs);
  357. out:
  358. return em;
  359. }
  360. /**
  361. * remove_extent_mapping - removes an extent_map from the extent tree
  362. * @tree: extent tree to remove from
  363. * @em: extent map beeing removed
  364. *
  365. * Removes @em from @tree. No reference counts are dropped, and no checks
  366. * are done to see if the range is in use
  367. */
  368. int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
  369. {
  370. int ret = 0;
  371. WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
  372. rb_erase(&em->rb_node, &tree->map);
  373. em->in_tree = 0;
  374. return ret;
  375. }