extent_map.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/err.h>
  3. #include <linux/slab.h>
  4. #include <linux/spinlock.h>
  5. #include "ctree.h"
  6. #include "volumes.h"
  7. #include "extent_map.h"
  8. #include "compression.h"
  9. #include "btrfs_inode.h"
  10. static struct kmem_cache *extent_map_cache;
  11. int __init extent_map_init(void)
  12. {
  13. extent_map_cache = kmem_cache_create("btrfs_extent_map",
  14. sizeof(struct extent_map), 0,
  15. SLAB_MEM_SPREAD, NULL);
  16. if (!extent_map_cache)
  17. return -ENOMEM;
  18. return 0;
  19. }
  20. void __cold extent_map_exit(void)
  21. {
  22. kmem_cache_destroy(extent_map_cache);
  23. }
  24. /**
  25. * extent_map_tree_init - initialize extent map tree
  26. * @tree: tree to initialize
  27. *
  28. * Initialize the extent tree @tree. Should be called for each new inode
  29. * or other user of the extent_map interface.
  30. */
  31. void extent_map_tree_init(struct extent_map_tree *tree)
  32. {
  33. tree->map = RB_ROOT_CACHED;
  34. INIT_LIST_HEAD(&tree->modified_extents);
  35. rwlock_init(&tree->lock);
  36. }
  37. /**
  38. * alloc_extent_map - allocate new extent map structure
  39. *
  40. * Allocate a new extent_map structure. The new structure is
  41. * returned with a reference count of one and needs to be
  42. * freed using free_extent_map()
  43. */
  44. struct extent_map *alloc_extent_map(void)
  45. {
  46. struct extent_map *em;
  47. em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS);
  48. if (!em)
  49. return NULL;
  50. RB_CLEAR_NODE(&em->rb_node);
  51. em->compress_type = BTRFS_COMPRESS_NONE;
  52. refcount_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 being released
  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. if (refcount_dec_and_test(&em->refs)) {
  68. WARN_ON(extent_map_in_tree(em));
  69. WARN_ON(!list_empty(&em->list));
  70. if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
  71. kfree(em->map_lookup);
  72. kmem_cache_free(extent_map_cache, em);
  73. }
  74. }
  75. /* simple helper to do math around the end of an extent, handling wrap */
  76. static u64 range_end(u64 start, u64 len)
  77. {
  78. if (start + len < start)
  79. return (u64)-1;
  80. return start + len;
  81. }
  82. static int tree_insert(struct rb_root_cached *root, struct extent_map *em)
  83. {
  84. struct rb_node **p = &root->rb_root.rb_node;
  85. struct rb_node *parent = NULL;
  86. struct extent_map *entry = NULL;
  87. struct rb_node *orig_parent = NULL;
  88. u64 end = range_end(em->start, em->len);
  89. bool leftmost = true;
  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. leftmost = false;
  98. } else {
  99. return -EEXIST;
  100. }
  101. }
  102. orig_parent = parent;
  103. while (parent && em->start >= extent_map_end(entry)) {
  104. parent = rb_next(parent);
  105. entry = rb_entry(parent, struct extent_map, rb_node);
  106. }
  107. if (parent)
  108. if (end > entry->start && em->start < extent_map_end(entry))
  109. return -EEXIST;
  110. parent = orig_parent;
  111. entry = rb_entry(parent, struct extent_map, rb_node);
  112. while (parent && em->start < entry->start) {
  113. parent = rb_prev(parent);
  114. entry = rb_entry(parent, struct extent_map, rb_node);
  115. }
  116. if (parent)
  117. if (end > entry->start && em->start < extent_map_end(entry))
  118. return -EEXIST;
  119. rb_link_node(&em->rb_node, orig_parent, p);
  120. rb_insert_color_cached(&em->rb_node, root, leftmost);
  121. return 0;
  122. }
  123. /*
  124. * search through the tree for an extent_map with a given offset. If
  125. * it can't be found, try to find some neighboring extents
  126. */
  127. static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
  128. struct rb_node **prev_or_next_ret)
  129. {
  130. struct rb_node *n = root->rb_node;
  131. struct rb_node *prev = NULL;
  132. struct rb_node *orig_prev = NULL;
  133. struct extent_map *entry;
  134. struct extent_map *prev_entry = NULL;
  135. ASSERT(prev_or_next_ret);
  136. while (n) {
  137. entry = rb_entry(n, struct extent_map, rb_node);
  138. prev = n;
  139. prev_entry = entry;
  140. if (offset < entry->start)
  141. n = n->rb_left;
  142. else if (offset >= extent_map_end(entry))
  143. n = n->rb_right;
  144. else
  145. return n;
  146. }
  147. orig_prev = prev;
  148. while (prev && offset >= extent_map_end(prev_entry)) {
  149. prev = rb_next(prev);
  150. prev_entry = rb_entry(prev, struct extent_map, rb_node);
  151. }
  152. /*
  153. * Previous extent map found, return as in this case the caller does not
  154. * care about the next one.
  155. */
  156. if (prev) {
  157. *prev_or_next_ret = prev;
  158. return NULL;
  159. }
  160. prev = orig_prev;
  161. prev_entry = rb_entry(prev, struct extent_map, rb_node);
  162. while (prev && offset < prev_entry->start) {
  163. prev = rb_prev(prev);
  164. prev_entry = rb_entry(prev, struct extent_map, rb_node);
  165. }
  166. *prev_or_next_ret = prev;
  167. return NULL;
  168. }
  169. /* check to see if two extent_map structs are adjacent and safe to merge */
  170. static int mergable_maps(struct extent_map *prev, struct extent_map *next)
  171. {
  172. if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
  173. return 0;
  174. /*
  175. * don't merge compressed extents, we need to know their
  176. * actual size
  177. */
  178. if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
  179. return 0;
  180. if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
  181. test_bit(EXTENT_FLAG_LOGGING, &next->flags))
  182. return 0;
  183. /*
  184. * We don't want to merge stuff that hasn't been written to the log yet
  185. * since it may not reflect exactly what is on disk, and that would be
  186. * bad.
  187. */
  188. if (!list_empty(&prev->list) || !list_empty(&next->list))
  189. return 0;
  190. ASSERT(next->block_start != EXTENT_MAP_DELALLOC &&
  191. prev->block_start != EXTENT_MAP_DELALLOC);
  192. if (prev->map_lookup || next->map_lookup)
  193. ASSERT(test_bit(EXTENT_FLAG_FS_MAPPING, &prev->flags) &&
  194. test_bit(EXTENT_FLAG_FS_MAPPING, &next->flags));
  195. if (extent_map_end(prev) == next->start &&
  196. prev->flags == next->flags &&
  197. prev->map_lookup == next->map_lookup &&
  198. ((next->block_start == EXTENT_MAP_HOLE &&
  199. prev->block_start == EXTENT_MAP_HOLE) ||
  200. (next->block_start == EXTENT_MAP_INLINE &&
  201. prev->block_start == EXTENT_MAP_INLINE) ||
  202. (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
  203. next->block_start == extent_map_block_end(prev)))) {
  204. return 1;
  205. }
  206. return 0;
  207. }
  208. static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
  209. {
  210. struct extent_map *merge = NULL;
  211. struct rb_node *rb;
  212. /*
  213. * We can't modify an extent map that is in the tree and that is being
  214. * used by another task, as it can cause that other task to see it in
  215. * inconsistent state during the merging. We always have 1 reference for
  216. * the tree and 1 for this task (which is unpinning the extent map or
  217. * clearing the logging flag), so anything > 2 means it's being used by
  218. * other tasks too.
  219. */
  220. if (refcount_read(&em->refs) > 2)
  221. return;
  222. if (em->start != 0) {
  223. rb = rb_prev(&em->rb_node);
  224. if (rb)
  225. merge = rb_entry(rb, struct extent_map, rb_node);
  226. if (rb && mergable_maps(merge, em)) {
  227. em->start = merge->start;
  228. em->orig_start = merge->orig_start;
  229. em->len += merge->len;
  230. em->block_len += merge->block_len;
  231. em->block_start = merge->block_start;
  232. em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
  233. em->mod_start = merge->mod_start;
  234. em->generation = max(em->generation, merge->generation);
  235. set_bit(EXTENT_FLAG_MERGED, &em->flags);
  236. rb_erase_cached(&merge->rb_node, &tree->map);
  237. RB_CLEAR_NODE(&merge->rb_node);
  238. free_extent_map(merge);
  239. }
  240. }
  241. rb = rb_next(&em->rb_node);
  242. if (rb)
  243. merge = rb_entry(rb, struct extent_map, rb_node);
  244. if (rb && mergable_maps(em, merge)) {
  245. em->len += merge->len;
  246. em->block_len += merge->block_len;
  247. rb_erase_cached(&merge->rb_node, &tree->map);
  248. RB_CLEAR_NODE(&merge->rb_node);
  249. em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
  250. em->generation = max(em->generation, merge->generation);
  251. set_bit(EXTENT_FLAG_MERGED, &em->flags);
  252. free_extent_map(merge);
  253. }
  254. }
  255. /**
  256. * unpin_extent_cache - unpin an extent from the cache
  257. * @tree: tree to unpin the extent in
  258. * @start: logical offset in the file
  259. * @len: length of the extent
  260. * @gen: generation that this extent has been modified in
  261. *
  262. * Called after an extent has been written to disk properly. Set the generation
  263. * to the generation that actually added the file item to the inode so we know
  264. * we need to sync this extent when we call fsync().
  265. */
  266. int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
  267. u64 gen)
  268. {
  269. int ret = 0;
  270. struct extent_map *em;
  271. bool prealloc = false;
  272. write_lock(&tree->lock);
  273. em = lookup_extent_mapping(tree, start, len);
  274. WARN_ON(!em || em->start != start);
  275. if (!em)
  276. goto out;
  277. em->generation = gen;
  278. clear_bit(EXTENT_FLAG_PINNED, &em->flags);
  279. em->mod_start = em->start;
  280. em->mod_len = em->len;
  281. if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
  282. prealloc = true;
  283. clear_bit(EXTENT_FLAG_FILLING, &em->flags);
  284. }
  285. try_merge_map(tree, em);
  286. if (prealloc) {
  287. em->mod_start = em->start;
  288. em->mod_len = em->len;
  289. }
  290. free_extent_map(em);
  291. out:
  292. write_unlock(&tree->lock);
  293. return ret;
  294. }
  295. void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
  296. {
  297. lockdep_assert_held_write(&tree->lock);
  298. clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
  299. if (extent_map_in_tree(em))
  300. try_merge_map(tree, em);
  301. }
  302. static inline void setup_extent_mapping(struct extent_map_tree *tree,
  303. struct extent_map *em,
  304. int modified)
  305. {
  306. refcount_inc(&em->refs);
  307. em->mod_start = em->start;
  308. em->mod_len = em->len;
  309. if (modified)
  310. list_move(&em->list, &tree->modified_extents);
  311. else
  312. try_merge_map(tree, em);
  313. }
  314. static void extent_map_device_set_bits(struct extent_map *em, unsigned bits)
  315. {
  316. struct map_lookup *map = em->map_lookup;
  317. u64 stripe_size = em->orig_block_len;
  318. int i;
  319. for (i = 0; i < map->num_stripes; i++) {
  320. struct btrfs_io_stripe *stripe = &map->stripes[i];
  321. struct btrfs_device *device = stripe->dev;
  322. set_extent_bits_nowait(&device->alloc_state, stripe->physical,
  323. stripe->physical + stripe_size - 1, bits);
  324. }
  325. }
  326. static void extent_map_device_clear_bits(struct extent_map *em, unsigned bits)
  327. {
  328. struct map_lookup *map = em->map_lookup;
  329. u64 stripe_size = em->orig_block_len;
  330. int i;
  331. for (i = 0; i < map->num_stripes; i++) {
  332. struct btrfs_io_stripe *stripe = &map->stripes[i];
  333. struct btrfs_device *device = stripe->dev;
  334. __clear_extent_bit(&device->alloc_state, stripe->physical,
  335. stripe->physical + stripe_size - 1, bits,
  336. NULL, GFP_NOWAIT, NULL);
  337. }
  338. }
  339. /**
  340. * Add new extent map to the extent tree
  341. *
  342. * @tree: tree to insert new map in
  343. * @em: map to insert
  344. * @modified: indicate whether the given @em should be added to the
  345. * modified list, which indicates the extent needs to be logged
  346. *
  347. * Insert @em into @tree or perform a simple forward/backward merge with
  348. * existing mappings. The extent_map struct passed in will be inserted
  349. * into the tree directly, with an additional reference taken, or a
  350. * reference dropped if the merge attempt was successful.
  351. */
  352. int add_extent_mapping(struct extent_map_tree *tree,
  353. struct extent_map *em, int modified)
  354. {
  355. int ret = 0;
  356. lockdep_assert_held_write(&tree->lock);
  357. ret = tree_insert(&tree->map, em);
  358. if (ret)
  359. goto out;
  360. setup_extent_mapping(tree, em, modified);
  361. if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags)) {
  362. extent_map_device_set_bits(em, CHUNK_ALLOCATED);
  363. extent_map_device_clear_bits(em, CHUNK_TRIMMED);
  364. }
  365. out:
  366. return ret;
  367. }
  368. static struct extent_map *
  369. __lookup_extent_mapping(struct extent_map_tree *tree,
  370. u64 start, u64 len, int strict)
  371. {
  372. struct extent_map *em;
  373. struct rb_node *rb_node;
  374. struct rb_node *prev_or_next = NULL;
  375. u64 end = range_end(start, len);
  376. rb_node = __tree_search(&tree->map.rb_root, start, &prev_or_next);
  377. if (!rb_node) {
  378. if (prev_or_next)
  379. rb_node = prev_or_next;
  380. else
  381. return NULL;
  382. }
  383. em = rb_entry(rb_node, struct extent_map, rb_node);
  384. if (strict && !(end > em->start && start < extent_map_end(em)))
  385. return NULL;
  386. refcount_inc(&em->refs);
  387. return em;
  388. }
  389. /**
  390. * lookup_extent_mapping - lookup extent_map
  391. * @tree: tree to lookup in
  392. * @start: byte offset to start the search
  393. * @len: length of the lookup range
  394. *
  395. * Find and return the first extent_map struct in @tree that intersects the
  396. * [start, len] range. There may be additional objects in the tree that
  397. * intersect, so check the object returned carefully to make sure that no
  398. * additional lookups are needed.
  399. */
  400. struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
  401. u64 start, u64 len)
  402. {
  403. return __lookup_extent_mapping(tree, start, len, 1);
  404. }
  405. /**
  406. * search_extent_mapping - find a nearby extent map
  407. * @tree: tree to lookup in
  408. * @start: byte offset to start the search
  409. * @len: length of the lookup range
  410. *
  411. * Find and return the first extent_map struct in @tree that intersects the
  412. * [start, len] range.
  413. *
  414. * If one can't be found, any nearby extent may be returned
  415. */
  416. struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
  417. u64 start, u64 len)
  418. {
  419. return __lookup_extent_mapping(tree, start, len, 0);
  420. }
  421. /**
  422. * remove_extent_mapping - removes an extent_map from the extent tree
  423. * @tree: extent tree to remove from
  424. * @em: extent map being removed
  425. *
  426. * Removes @em from @tree. No reference counts are dropped, and no checks
  427. * are done to see if the range is in use
  428. */
  429. void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
  430. {
  431. lockdep_assert_held_write(&tree->lock);
  432. WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
  433. rb_erase_cached(&em->rb_node, &tree->map);
  434. if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
  435. list_del_init(&em->list);
  436. if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
  437. extent_map_device_clear_bits(em, CHUNK_ALLOCATED);
  438. RB_CLEAR_NODE(&em->rb_node);
  439. }
  440. void replace_extent_mapping(struct extent_map_tree *tree,
  441. struct extent_map *cur,
  442. struct extent_map *new,
  443. int modified)
  444. {
  445. lockdep_assert_held_write(&tree->lock);
  446. WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));
  447. ASSERT(extent_map_in_tree(cur));
  448. if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))
  449. list_del_init(&cur->list);
  450. rb_replace_node_cached(&cur->rb_node, &new->rb_node, &tree->map);
  451. RB_CLEAR_NODE(&cur->rb_node);
  452. setup_extent_mapping(tree, new, modified);
  453. }
  454. static struct extent_map *next_extent_map(const struct extent_map *em)
  455. {
  456. struct rb_node *next;
  457. next = rb_next(&em->rb_node);
  458. if (!next)
  459. return NULL;
  460. return container_of(next, struct extent_map, rb_node);
  461. }
  462. /*
  463. * Get the extent map that immediately follows another one.
  464. *
  465. * @tree: The extent map tree that the extent map belong to.
  466. * Holding read or write access on the tree's lock is required.
  467. * @em: An extent map from the given tree. The caller must ensure that
  468. * between getting @em and between calling this function, the
  469. * extent map @em is not removed from the tree - for example, by
  470. * holding the tree's lock for the duration of those 2 operations.
  471. *
  472. * Returns the extent map that immediately follows @em, or NULL if @em is the
  473. * last extent map in the tree.
  474. */
  475. struct extent_map *btrfs_next_extent_map(const struct extent_map_tree *tree,
  476. const struct extent_map *em)
  477. {
  478. struct extent_map *next;
  479. /* The lock must be acquired either in read mode or write mode. */
  480. lockdep_assert_held(&tree->lock);
  481. ASSERT(extent_map_in_tree(em));
  482. next = next_extent_map(em);
  483. if (next)
  484. refcount_inc(&next->refs);
  485. return next;
  486. }
  487. static struct extent_map *prev_extent_map(struct extent_map *em)
  488. {
  489. struct rb_node *prev;
  490. prev = rb_prev(&em->rb_node);
  491. if (!prev)
  492. return NULL;
  493. return container_of(prev, struct extent_map, rb_node);
  494. }
  495. /*
  496. * Helper for btrfs_get_extent. Given an existing extent in the tree,
  497. * the existing extent is the nearest extent to map_start,
  498. * and an extent that you want to insert, deal with overlap and insert
  499. * the best fitted new extent into the tree.
  500. */
  501. static noinline int merge_extent_mapping(struct extent_map_tree *em_tree,
  502. struct extent_map *existing,
  503. struct extent_map *em,
  504. u64 map_start)
  505. {
  506. struct extent_map *prev;
  507. struct extent_map *next;
  508. u64 start;
  509. u64 end;
  510. u64 start_diff;
  511. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  512. if (existing->start > map_start) {
  513. next = existing;
  514. prev = prev_extent_map(next);
  515. } else {
  516. prev = existing;
  517. next = next_extent_map(prev);
  518. }
  519. start = prev ? extent_map_end(prev) : em->start;
  520. start = max_t(u64, start, em->start);
  521. end = next ? next->start : extent_map_end(em);
  522. end = min_t(u64, end, extent_map_end(em));
  523. start_diff = start - em->start;
  524. em->start = start;
  525. em->len = end - start;
  526. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  527. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  528. em->block_start += start_diff;
  529. em->block_len = em->len;
  530. }
  531. return add_extent_mapping(em_tree, em, 0);
  532. }
  533. /**
  534. * Add extent mapping into em_tree
  535. *
  536. * @fs_info: the filesystem
  537. * @em_tree: extent tree into which we want to insert the extent mapping
  538. * @em_in: extent we are inserting
  539. * @start: start of the logical range btrfs_get_extent() is requesting
  540. * @len: length of the logical range btrfs_get_extent() is requesting
  541. *
  542. * Note that @em_in's range may be different from [start, start+len),
  543. * but they must be overlapped.
  544. *
  545. * Insert @em_in into @em_tree. In case there is an overlapping range, handle
  546. * the -EEXIST by either:
  547. * a) Returning the existing extent in @em_in if @start is within the
  548. * existing em.
  549. * b) Merge the existing extent with @em_in passed in.
  550. *
  551. * Return 0 on success, otherwise -EEXIST.
  552. *
  553. */
  554. int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info,
  555. struct extent_map_tree *em_tree,
  556. struct extent_map **em_in, u64 start, u64 len)
  557. {
  558. int ret;
  559. struct extent_map *em = *em_in;
  560. ret = add_extent_mapping(em_tree, em, 0);
  561. /* it is possible that someone inserted the extent into the tree
  562. * while we had the lock dropped. It is also possible that
  563. * an overlapping map exists in the tree
  564. */
  565. if (ret == -EEXIST) {
  566. struct extent_map *existing;
  567. ret = 0;
  568. existing = search_extent_mapping(em_tree, start, len);
  569. trace_btrfs_handle_em_exist(fs_info, existing, em, start, len);
  570. /*
  571. * existing will always be non-NULL, since there must be
  572. * extent causing the -EEXIST.
  573. */
  574. if (start >= existing->start &&
  575. start < extent_map_end(existing)) {
  576. free_extent_map(em);
  577. *em_in = existing;
  578. ret = 0;
  579. } else {
  580. u64 orig_start = em->start;
  581. u64 orig_len = em->len;
  582. /*
  583. * The existing extent map is the one nearest to
  584. * the [start, start + len) range which overlaps
  585. */
  586. ret = merge_extent_mapping(em_tree, existing,
  587. em, start);
  588. if (ret) {
  589. free_extent_map(em);
  590. *em_in = NULL;
  591. WARN_ONCE(ret,
  592. "unexpected error %d: merge existing(start %llu len %llu) with em(start %llu len %llu)\n",
  593. ret, existing->start, existing->len,
  594. orig_start, orig_len);
  595. }
  596. free_extent_map(existing);
  597. }
  598. }
  599. ASSERT(ret == 0 || ret == -EEXIST);
  600. return ret;
  601. }
  602. /*
  603. * Drop all extent maps from a tree in the fastest possible way, rescheduling
  604. * if needed. This avoids searching the tree, from the root down to the first
  605. * extent map, before each deletion.
  606. */
  607. static void drop_all_extent_maps_fast(struct extent_map_tree *tree)
  608. {
  609. write_lock(&tree->lock);
  610. while (!RB_EMPTY_ROOT(&tree->map.rb_root)) {
  611. struct extent_map *em;
  612. struct rb_node *node;
  613. node = rb_first_cached(&tree->map);
  614. em = rb_entry(node, struct extent_map, rb_node);
  615. clear_bit(EXTENT_FLAG_PINNED, &em->flags);
  616. clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
  617. remove_extent_mapping(tree, em);
  618. free_extent_map(em);
  619. cond_resched_rwlock_write(&tree->lock);
  620. }
  621. write_unlock(&tree->lock);
  622. }
  623. /*
  624. * Drop all extent maps in a given range.
  625. *
  626. * @inode: The target inode.
  627. * @start: Start offset of the range.
  628. * @end: End offset of the range (inclusive value).
  629. * @skip_pinned: Indicate if pinned extent maps should be ignored or not.
  630. *
  631. * This drops all the extent maps that intersect the given range [@start, @end].
  632. * Extent maps that partially overlap the range and extend behind or beyond it,
  633. * are split.
  634. * The caller should have locked an appropriate file range in the inode's io
  635. * tree before calling this function.
  636. */
  637. void btrfs_drop_extent_map_range(struct btrfs_inode *inode, u64 start, u64 end,
  638. bool skip_pinned)
  639. {
  640. struct extent_map *split;
  641. struct extent_map *split2;
  642. struct extent_map *em;
  643. struct extent_map_tree *em_tree = &inode->extent_tree;
  644. u64 len = end - start + 1;
  645. WARN_ON(end < start);
  646. if (end == (u64)-1) {
  647. if (start == 0 && !skip_pinned) {
  648. drop_all_extent_maps_fast(em_tree);
  649. return;
  650. }
  651. len = (u64)-1;
  652. } else {
  653. /* Make end offset exclusive for use in the loop below. */
  654. end++;
  655. }
  656. /*
  657. * It's ok if we fail to allocate the extent maps, see the comment near
  658. * the bottom of the loop below. We only need two spare extent maps in
  659. * the worst case, where the first extent map that intersects our range
  660. * starts before the range and the last extent map that intersects our
  661. * range ends after our range (and they might be the same extent map),
  662. * because we need to split those two extent maps at the boundaries.
  663. */
  664. split = alloc_extent_map();
  665. split2 = alloc_extent_map();
  666. write_lock(&em_tree->lock);
  667. em = lookup_extent_mapping(em_tree, start, len);
  668. while (em) {
  669. /* extent_map_end() returns exclusive value (last byte + 1). */
  670. const u64 em_end = extent_map_end(em);
  671. struct extent_map *next_em = NULL;
  672. u64 gen;
  673. unsigned long flags;
  674. bool modified;
  675. bool compressed;
  676. if (em_end < end) {
  677. next_em = next_extent_map(em);
  678. if (next_em) {
  679. if (next_em->start < end)
  680. refcount_inc(&next_em->refs);
  681. else
  682. next_em = NULL;
  683. }
  684. }
  685. if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
  686. start = em_end;
  687. goto next;
  688. }
  689. flags = em->flags;
  690. clear_bit(EXTENT_FLAG_PINNED, &em->flags);
  691. /*
  692. * In case we split the extent map, we want to preserve the
  693. * EXTENT_FLAG_LOGGING flag on our extent map, but we don't want
  694. * it on the new extent maps.
  695. */
  696. clear_bit(EXTENT_FLAG_LOGGING, &flags);
  697. modified = !list_empty(&em->list);
  698. /*
  699. * The extent map does not cross our target range, so no need to
  700. * split it, we can remove it directly.
  701. */
  702. if (em->start >= start && em_end <= end)
  703. goto remove_em;
  704. gen = em->generation;
  705. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  706. if (em->start < start) {
  707. if (!split) {
  708. split = split2;
  709. split2 = NULL;
  710. if (!split)
  711. goto remove_em;
  712. }
  713. split->start = em->start;
  714. split->len = start - em->start;
  715. if (em->block_start < EXTENT_MAP_LAST_BYTE) {
  716. split->orig_start = em->orig_start;
  717. split->block_start = em->block_start;
  718. if (compressed)
  719. split->block_len = em->block_len;
  720. else
  721. split->block_len = split->len;
  722. split->orig_block_len = max(split->block_len,
  723. em->orig_block_len);
  724. split->ram_bytes = em->ram_bytes;
  725. } else {
  726. split->orig_start = split->start;
  727. split->block_len = 0;
  728. split->block_start = em->block_start;
  729. split->orig_block_len = 0;
  730. split->ram_bytes = split->len;
  731. }
  732. split->generation = gen;
  733. split->flags = flags;
  734. split->compress_type = em->compress_type;
  735. replace_extent_mapping(em_tree, em, split, modified);
  736. free_extent_map(split);
  737. split = split2;
  738. split2 = NULL;
  739. }
  740. if (em_end > end) {
  741. if (!split) {
  742. split = split2;
  743. split2 = NULL;
  744. if (!split)
  745. goto remove_em;
  746. }
  747. split->start = end;
  748. split->len = em_end - end;
  749. split->block_start = em->block_start;
  750. split->flags = flags;
  751. split->compress_type = em->compress_type;
  752. split->generation = gen;
  753. if (em->block_start < EXTENT_MAP_LAST_BYTE) {
  754. split->orig_block_len = max(em->block_len,
  755. em->orig_block_len);
  756. split->ram_bytes = em->ram_bytes;
  757. if (compressed) {
  758. split->block_len = em->block_len;
  759. split->orig_start = em->orig_start;
  760. } else {
  761. const u64 diff = start + len - em->start;
  762. split->block_len = split->len;
  763. split->block_start += diff;
  764. split->orig_start = em->orig_start;
  765. }
  766. } else {
  767. split->ram_bytes = split->len;
  768. split->orig_start = split->start;
  769. split->block_len = 0;
  770. split->orig_block_len = 0;
  771. }
  772. if (extent_map_in_tree(em)) {
  773. replace_extent_mapping(em_tree, em, split,
  774. modified);
  775. } else {
  776. int ret;
  777. ret = add_extent_mapping(em_tree, split,
  778. modified);
  779. /* Logic error, shouldn't happen. */
  780. ASSERT(ret == 0);
  781. if (WARN_ON(ret != 0) && modified)
  782. btrfs_set_inode_full_sync(inode);
  783. }
  784. free_extent_map(split);
  785. split = NULL;
  786. }
  787. remove_em:
  788. if (extent_map_in_tree(em)) {
  789. /*
  790. * If the extent map is still in the tree it means that
  791. * either of the following is true:
  792. *
  793. * 1) It fits entirely in our range (doesn't end beyond
  794. * it or starts before it);
  795. *
  796. * 2) It starts before our range and/or ends after our
  797. * range, and we were not able to allocate the extent
  798. * maps for split operations, @split and @split2.
  799. *
  800. * If we are at case 2) then we just remove the entire
  801. * extent map - this is fine since if anyone needs it to
  802. * access the subranges outside our range, will just
  803. * load it again from the subvolume tree's file extent
  804. * item. However if the extent map was in the list of
  805. * modified extents, then we must mark the inode for a
  806. * full fsync, otherwise a fast fsync will miss this
  807. * extent if it's new and needs to be logged.
  808. */
  809. if ((em->start < start || em_end > end) && modified) {
  810. ASSERT(!split);
  811. btrfs_set_inode_full_sync(inode);
  812. }
  813. remove_extent_mapping(em_tree, em);
  814. }
  815. /*
  816. * Once for the tree reference (we replaced or removed the
  817. * extent map from the tree).
  818. */
  819. free_extent_map(em);
  820. next:
  821. /* Once for us (for our lookup reference). */
  822. free_extent_map(em);
  823. em = next_em;
  824. }
  825. write_unlock(&em_tree->lock);
  826. free_extent_map(split);
  827. free_extent_map(split2);
  828. }
  829. /*
  830. * Replace a range in the inode's extent map tree with a new extent map.
  831. *
  832. * @inode: The target inode.
  833. * @new_em: The new extent map to add to the inode's extent map tree.
  834. * @modified: Indicate if the new extent map should be added to the list of
  835. * modified extents (for fast fsync tracking).
  836. *
  837. * Drops all the extent maps in the inode's extent map tree that intersect the
  838. * range of the new extent map and adds the new extent map to the tree.
  839. * The caller should have locked an appropriate file range in the inode's io
  840. * tree before calling this function.
  841. */
  842. int btrfs_replace_extent_map_range(struct btrfs_inode *inode,
  843. struct extent_map *new_em,
  844. bool modified)
  845. {
  846. const u64 end = new_em->start + new_em->len - 1;
  847. struct extent_map_tree *tree = &inode->extent_tree;
  848. int ret;
  849. ASSERT(!extent_map_in_tree(new_em));
  850. /*
  851. * The caller has locked an appropriate file range in the inode's io
  852. * tree, but getting -EEXIST when adding the new extent map can still
  853. * happen in case there are extents that partially cover the range, and
  854. * this is due to two tasks operating on different parts of the extent.
  855. * See commit 18e83ac75bfe67 ("Btrfs: fix unexpected EEXIST from
  856. * btrfs_get_extent") for an example and details.
  857. */
  858. do {
  859. btrfs_drop_extent_map_range(inode, new_em->start, end, false);
  860. write_lock(&tree->lock);
  861. ret = add_extent_mapping(tree, new_em, modified);
  862. write_unlock(&tree->lock);
  863. } while (ret == -EEXIST);
  864. return ret;
  865. }