extent_map.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #include <linux/err.h>
  2. #include <linux/slab.h>
  3. #include <linux/spinlock.h>
  4. #include <linux/hardirq.h>
  5. #include "ctree.h"
  6. #include "extent_map.h"
  7. static struct kmem_cache *extent_map_cache;
  8. int __init extent_map_init(void)
  9. {
  10. extent_map_cache = kmem_cache_create("btrfs_extent_map",
  11. sizeof(struct extent_map), 0,
  12. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  13. if (!extent_map_cache)
  14. return -ENOMEM;
  15. return 0;
  16. }
  17. void extent_map_exit(void)
  18. {
  19. if (extent_map_cache)
  20. kmem_cache_destroy(extent_map_cache);
  21. }
  22. /**
  23. * extent_map_tree_init - initialize extent map tree
  24. * @tree: tree to initialize
  25. *
  26. * Initialize the extent tree @tree. Should be called for each new inode
  27. * or other user of the extent_map interface.
  28. */
  29. void extent_map_tree_init(struct extent_map_tree *tree)
  30. {
  31. tree->map = RB_ROOT;
  32. INIT_LIST_HEAD(&tree->modified_extents);
  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_zalloc(extent_map_cache, GFP_NOFS);
  46. if (!em)
  47. return NULL;
  48. RB_CLEAR_NODE(&em->rb_node);
  49. em->flags = 0;
  50. em->compress_type = BTRFS_COMPRESS_NONE;
  51. em->generation = 0;
  52. atomic_set(&em->refs, 1);
  53. INIT_LIST_HEAD(&em->list);
  54. return em;
  55. }
  56. /**
  57. * free_extent_map - drop reference count of an extent_map
  58. * @em: extent map beeing releasead
  59. *
  60. * Drops the reference out on @em by one and free the structure
  61. * if the reference count hits zero.
  62. */
  63. void free_extent_map(struct extent_map *em)
  64. {
  65. if (!em)
  66. return;
  67. WARN_ON(atomic_read(&em->refs) == 0);
  68. if (atomic_dec_and_test(&em->refs)) {
  69. WARN_ON(extent_map_in_tree(em));
  70. WARN_ON(!list_empty(&em->list));
  71. if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
  72. kfree(em->map_lookup);
  73. kmem_cache_free(extent_map_cache, em);
  74. }
  75. }
  76. /* simple helper to do math around the end of an extent, handling wrap */
  77. static u64 range_end(u64 start, u64 len)
  78. {
  79. if (start + len < start)
  80. return (u64)-1;
  81. return start + len;
  82. }
  83. static int tree_insert(struct rb_root *root, struct extent_map *em)
  84. {
  85. struct rb_node **p = &root->rb_node;
  86. struct rb_node *parent = NULL;
  87. struct extent_map *entry = NULL;
  88. struct rb_node *orig_parent = NULL;
  89. u64 end = range_end(em->start, em->len);
  90. while (*p) {
  91. parent = *p;
  92. entry = rb_entry(parent, struct extent_map, rb_node);
  93. if (em->start < entry->start)
  94. p = &(*p)->rb_left;
  95. else if (em->start >= extent_map_end(entry))
  96. p = &(*p)->rb_right;
  97. else
  98. return -EEXIST;
  99. }
  100. orig_parent = parent;
  101. while (parent && em->start >= extent_map_end(entry)) {
  102. parent = rb_next(parent);
  103. entry = rb_entry(parent, struct extent_map, rb_node);
  104. }
  105. if (parent)
  106. if (end > entry->start && em->start < extent_map_end(entry))
  107. return -EEXIST;
  108. parent = orig_parent;
  109. entry = rb_entry(parent, struct extent_map, rb_node);
  110. while (parent && em->start < entry->start) {
  111. parent = rb_prev(parent);
  112. entry = rb_entry(parent, struct extent_map, rb_node);
  113. }
  114. if (parent)
  115. if (end > entry->start && em->start < extent_map_end(entry))
  116. return -EEXIST;
  117. rb_link_node(&em->rb_node, orig_parent, p);
  118. rb_insert_color(&em->rb_node, root);
  119. return 0;
  120. }
  121. /*
  122. * search through the tree for an extent_map with a given offset. If
  123. * it can't be found, try to find some neighboring extents
  124. */
  125. static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
  126. struct rb_node **prev_ret,
  127. struct rb_node **next_ret)
  128. {
  129. struct rb_node *n = root->rb_node;
  130. struct rb_node *prev = NULL;
  131. struct rb_node *orig_prev = NULL;
  132. struct extent_map *entry;
  133. struct extent_map *prev_entry = NULL;
  134. while (n) {
  135. entry = rb_entry(n, struct extent_map, rb_node);
  136. prev = n;
  137. prev_entry = entry;
  138. if (offset < entry->start)
  139. n = n->rb_left;
  140. else if (offset >= extent_map_end(entry))
  141. n = n->rb_right;
  142. else
  143. return n;
  144. }
  145. if (prev_ret) {
  146. orig_prev = prev;
  147. while (prev && offset >= extent_map_end(prev_entry)) {
  148. prev = rb_next(prev);
  149. prev_entry = rb_entry(prev, struct extent_map, rb_node);
  150. }
  151. *prev_ret = prev;
  152. prev = orig_prev;
  153. }
  154. if (next_ret) {
  155. prev_entry = rb_entry(prev, struct extent_map, rb_node);
  156. while (prev && offset < prev_entry->start) {
  157. prev = rb_prev(prev);
  158. prev_entry = rb_entry(prev, struct extent_map, rb_node);
  159. }
  160. *next_ret = prev;
  161. }
  162. return NULL;
  163. }
  164. /* check to see if two extent_map structs are adjacent and safe to merge */
  165. static int mergable_maps(struct extent_map *prev, struct extent_map *next)
  166. {
  167. if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
  168. return 0;
  169. /*
  170. * don't merge compressed extents, we need to know their
  171. * actual size
  172. */
  173. if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
  174. return 0;
  175. if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
  176. test_bit(EXTENT_FLAG_LOGGING, &next->flags))
  177. return 0;
  178. /*
  179. * We don't want to merge stuff that hasn't been written to the log yet
  180. * since it may not reflect exactly what is on disk, and that would be
  181. * bad.
  182. */
  183. if (!list_empty(&prev->list) || !list_empty(&next->list))
  184. return 0;
  185. if (extent_map_end(prev) == next->start &&
  186. prev->flags == next->flags &&
  187. prev->bdev == next->bdev &&
  188. ((next->block_start == EXTENT_MAP_HOLE &&
  189. prev->block_start == EXTENT_MAP_HOLE) ||
  190. (next->block_start == EXTENT_MAP_INLINE &&
  191. prev->block_start == EXTENT_MAP_INLINE) ||
  192. (next->block_start == EXTENT_MAP_DELALLOC &&
  193. prev->block_start == EXTENT_MAP_DELALLOC) ||
  194. (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
  195. next->block_start == extent_map_block_end(prev)))) {
  196. return 1;
  197. }
  198. return 0;
  199. }
  200. static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
  201. {
  202. struct extent_map *merge = NULL;
  203. struct rb_node *rb;
  204. if (em->start != 0) {
  205. rb = rb_prev(&em->rb_node);
  206. if (rb)
  207. merge = rb_entry(rb, struct extent_map, rb_node);
  208. if (rb && mergable_maps(merge, em)) {
  209. em->start = merge->start;
  210. em->orig_start = merge->orig_start;
  211. em->len += merge->len;
  212. em->block_len += merge->block_len;
  213. em->block_start = merge->block_start;
  214. em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
  215. em->mod_start = merge->mod_start;
  216. em->generation = max(em->generation, merge->generation);
  217. rb_erase(&merge->rb_node, &tree->map);
  218. RB_CLEAR_NODE(&merge->rb_node);
  219. free_extent_map(merge);
  220. }
  221. }
  222. rb = rb_next(&em->rb_node);
  223. if (rb)
  224. merge = rb_entry(rb, struct extent_map, rb_node);
  225. if (rb && mergable_maps(em, merge)) {
  226. em->len += merge->len;
  227. em->block_len += merge->block_len;
  228. rb_erase(&merge->rb_node, &tree->map);
  229. RB_CLEAR_NODE(&merge->rb_node);
  230. em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
  231. em->generation = max(em->generation, merge->generation);
  232. free_extent_map(merge);
  233. }
  234. }
  235. /**
  236. * unpin_extent_cache - unpin an extent from the cache
  237. * @tree: tree to unpin the extent in
  238. * @start: logical offset in the file
  239. * @len: length of the extent
  240. * @gen: generation that this extent has been modified in
  241. *
  242. * Called after an extent has been written to disk properly. Set the generation
  243. * to the generation that actually added the file item to the inode so we know
  244. * we need to sync this extent when we call fsync().
  245. */
  246. int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
  247. u64 gen)
  248. {
  249. int ret = 0;
  250. struct extent_map *em;
  251. bool prealloc = false;
  252. write_lock(&tree->lock);
  253. em = lookup_extent_mapping(tree, start, len);
  254. WARN_ON(!em || em->start != start);
  255. if (!em)
  256. goto out;
  257. em->generation = gen;
  258. clear_bit(EXTENT_FLAG_PINNED, &em->flags);
  259. em->mod_start = em->start;
  260. em->mod_len = em->len;
  261. if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
  262. prealloc = true;
  263. clear_bit(EXTENT_FLAG_FILLING, &em->flags);
  264. }
  265. try_merge_map(tree, em);
  266. if (prealloc) {
  267. em->mod_start = em->start;
  268. em->mod_len = em->len;
  269. }
  270. free_extent_map(em);
  271. out:
  272. write_unlock(&tree->lock);
  273. return ret;
  274. }
  275. void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
  276. {
  277. clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
  278. if (extent_map_in_tree(em))
  279. try_merge_map(tree, em);
  280. }
  281. static inline void setup_extent_mapping(struct extent_map_tree *tree,
  282. struct extent_map *em,
  283. int modified)
  284. {
  285. atomic_inc(&em->refs);
  286. em->mod_start = em->start;
  287. em->mod_len = em->len;
  288. if (modified)
  289. list_move(&em->list, &tree->modified_extents);
  290. else
  291. try_merge_map(tree, em);
  292. }
  293. /**
  294. * add_extent_mapping - add new extent map to the extent tree
  295. * @tree: tree to insert new map in
  296. * @em: map to insert
  297. *
  298. * Insert @em into @tree or perform a simple forward/backward merge with
  299. * existing mappings. The extent_map struct passed in will be inserted
  300. * into the tree directly, with an additional reference taken, or a
  301. * reference dropped if the merge attempt was successful.
  302. */
  303. int add_extent_mapping(struct extent_map_tree *tree,
  304. struct extent_map *em, int modified)
  305. {
  306. int ret = 0;
  307. ret = tree_insert(&tree->map, em);
  308. if (ret)
  309. goto out;
  310. setup_extent_mapping(tree, em, modified);
  311. out:
  312. return ret;
  313. }
  314. static struct extent_map *
  315. __lookup_extent_mapping(struct extent_map_tree *tree,
  316. u64 start, u64 len, int strict)
  317. {
  318. struct extent_map *em;
  319. struct rb_node *rb_node;
  320. struct rb_node *prev = NULL;
  321. struct rb_node *next = NULL;
  322. u64 end = range_end(start, len);
  323. rb_node = __tree_search(&tree->map, start, &prev, &next);
  324. if (!rb_node) {
  325. if (prev)
  326. rb_node = prev;
  327. else if (next)
  328. rb_node = next;
  329. else
  330. return NULL;
  331. }
  332. em = rb_entry(rb_node, struct extent_map, rb_node);
  333. if (strict && !(end > em->start && start < extent_map_end(em)))
  334. return NULL;
  335. atomic_inc(&em->refs);
  336. return em;
  337. }
  338. /**
  339. * lookup_extent_mapping - lookup extent_map
  340. * @tree: tree to lookup in
  341. * @start: byte offset to start the search
  342. * @len: length of the lookup range
  343. *
  344. * Find and return the first extent_map struct in @tree that intersects the
  345. * [start, len] range. There may be additional objects in the tree that
  346. * intersect, so check the object returned carefully to make sure that no
  347. * additional lookups are needed.
  348. */
  349. struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
  350. u64 start, u64 len)
  351. {
  352. return __lookup_extent_mapping(tree, start, len, 1);
  353. }
  354. /**
  355. * search_extent_mapping - find a nearby extent map
  356. * @tree: tree to lookup in
  357. * @start: byte offset to start the search
  358. * @len: length of the lookup range
  359. *
  360. * Find and return the first extent_map struct in @tree that intersects the
  361. * [start, len] range.
  362. *
  363. * If one can't be found, any nearby extent may be returned
  364. */
  365. struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
  366. u64 start, u64 len)
  367. {
  368. return __lookup_extent_mapping(tree, start, len, 0);
  369. }
  370. /**
  371. * remove_extent_mapping - removes an extent_map from the extent tree
  372. * @tree: extent tree to remove from
  373. * @em: extent map beeing removed
  374. *
  375. * Removes @em from @tree. No reference counts are dropped, and no checks
  376. * are done to see if the range is in use
  377. */
  378. int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
  379. {
  380. int ret = 0;
  381. WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
  382. rb_erase(&em->rb_node, &tree->map);
  383. if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
  384. list_del_init(&em->list);
  385. RB_CLEAR_NODE(&em->rb_node);
  386. return ret;
  387. }
  388. void replace_extent_mapping(struct extent_map_tree *tree,
  389. struct extent_map *cur,
  390. struct extent_map *new,
  391. int modified)
  392. {
  393. WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));
  394. ASSERT(extent_map_in_tree(cur));
  395. if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))
  396. list_del_init(&cur->list);
  397. rb_replace_node(&cur->rb_node, &new->rb_node, &tree->map);
  398. RB_CLEAR_NODE(&cur->rb_node);
  399. setup_extent_mapping(tree, new, modified);
  400. }