extent-io-tree.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/slab.h>
  3. #include <trace/events/btrfs.h>
  4. #include "messages.h"
  5. #include "ctree.h"
  6. #include "extent_io.h"
  7. #include "extent-io-tree.h"
  8. #include "btrfs_inode.h"
  9. static struct kmem_cache *extent_state_cache;
  10. static inline bool extent_state_in_tree(const struct extent_state *state)
  11. {
  12. return !RB_EMPTY_NODE(&state->rb_node);
  13. }
  14. #ifdef CONFIG_BTRFS_DEBUG
  15. static LIST_HEAD(states);
  16. static DEFINE_SPINLOCK(leak_lock);
  17. static inline void btrfs_leak_debug_add_state(struct extent_state *state)
  18. {
  19. unsigned long flags;
  20. spin_lock_irqsave(&leak_lock, flags);
  21. list_add(&state->leak_list, &states);
  22. spin_unlock_irqrestore(&leak_lock, flags);
  23. }
  24. static inline void btrfs_leak_debug_del_state(struct extent_state *state)
  25. {
  26. unsigned long flags;
  27. spin_lock_irqsave(&leak_lock, flags);
  28. list_del(&state->leak_list);
  29. spin_unlock_irqrestore(&leak_lock, flags);
  30. }
  31. static inline void btrfs_extent_state_leak_debug_check(void)
  32. {
  33. struct extent_state *state;
  34. while (!list_empty(&states)) {
  35. state = list_entry(states.next, struct extent_state, leak_list);
  36. pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
  37. state->start, state->end, state->state,
  38. extent_state_in_tree(state),
  39. refcount_read(&state->refs));
  40. list_del(&state->leak_list);
  41. WARN_ON_ONCE(1);
  42. kmem_cache_free(extent_state_cache, state);
  43. }
  44. }
  45. #define btrfs_debug_check_extent_io_range(tree, start, end) \
  46. __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
  47. static inline void __btrfs_debug_check_extent_io_range(const char *caller,
  48. struct extent_io_tree *tree,
  49. u64 start, u64 end)
  50. {
  51. const struct btrfs_inode *inode;
  52. u64 isize;
  53. if (tree->owner != IO_TREE_INODE_IO)
  54. return;
  55. inode = extent_io_tree_to_inode_const(tree);
  56. isize = i_size_read(&inode->vfs_inode);
  57. if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
  58. btrfs_debug_rl(inode->root->fs_info,
  59. "%s: ino %llu isize %llu odd range [%llu,%llu]",
  60. caller, btrfs_ino(inode), isize, start, end);
  61. }
  62. }
  63. #else
  64. #define btrfs_leak_debug_add_state(state) do {} while (0)
  65. #define btrfs_leak_debug_del_state(state) do {} while (0)
  66. #define btrfs_extent_state_leak_debug_check() do {} while (0)
  67. #define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
  68. #endif
  69. /*
  70. * The only tree allowed to set the inode is IO_TREE_INODE_IO.
  71. */
  72. static bool is_inode_io_tree(const struct extent_io_tree *tree)
  73. {
  74. return tree->owner == IO_TREE_INODE_IO;
  75. }
  76. /* Return the inode if it's valid for the given tree, otherwise NULL. */
  77. struct btrfs_inode *extent_io_tree_to_inode(struct extent_io_tree *tree)
  78. {
  79. if (tree->owner == IO_TREE_INODE_IO)
  80. return tree->inode;
  81. return NULL;
  82. }
  83. /* Read-only access to the inode. */
  84. const struct btrfs_inode *extent_io_tree_to_inode_const(const struct extent_io_tree *tree)
  85. {
  86. if (tree->owner == IO_TREE_INODE_IO)
  87. return tree->inode;
  88. return NULL;
  89. }
  90. /* For read-only access to fs_info. */
  91. const struct btrfs_fs_info *extent_io_tree_to_fs_info(const struct extent_io_tree *tree)
  92. {
  93. if (tree->owner == IO_TREE_INODE_IO)
  94. return tree->inode->root->fs_info;
  95. return tree->fs_info;
  96. }
  97. void extent_io_tree_init(struct btrfs_fs_info *fs_info,
  98. struct extent_io_tree *tree, unsigned int owner)
  99. {
  100. tree->state = RB_ROOT;
  101. spin_lock_init(&tree->lock);
  102. tree->fs_info = fs_info;
  103. tree->owner = owner;
  104. }
  105. /*
  106. * Empty an io tree, removing and freeing every extent state record from the
  107. * tree. This should be called once we are sure no other task can access the
  108. * tree anymore, so no tree updates happen after we empty the tree and there
  109. * aren't any waiters on any extent state record (EXTENT_LOCK_BITS are never
  110. * set on any extent state when calling this function).
  111. */
  112. void extent_io_tree_release(struct extent_io_tree *tree)
  113. {
  114. struct rb_root root;
  115. struct extent_state *state;
  116. struct extent_state *tmp;
  117. spin_lock(&tree->lock);
  118. root = tree->state;
  119. tree->state = RB_ROOT;
  120. rbtree_postorder_for_each_entry_safe(state, tmp, &root, rb_node) {
  121. /* Clear node to keep free_extent_state() happy. */
  122. RB_CLEAR_NODE(&state->rb_node);
  123. ASSERT(!(state->state & EXTENT_LOCK_BITS));
  124. /*
  125. * No need for a memory barrier here, as we are holding the tree
  126. * lock and we only change the waitqueue while holding that lock
  127. * (see wait_extent_bit()).
  128. */
  129. ASSERT(!waitqueue_active(&state->wq));
  130. free_extent_state(state);
  131. cond_resched_lock(&tree->lock);
  132. }
  133. /*
  134. * Should still be empty even after a reschedule, no other task should
  135. * be accessing the tree anymore.
  136. */
  137. ASSERT(RB_EMPTY_ROOT(&tree->state));
  138. spin_unlock(&tree->lock);
  139. }
  140. static struct extent_state *alloc_extent_state(gfp_t mask)
  141. {
  142. struct extent_state *state;
  143. /*
  144. * The given mask might be not appropriate for the slab allocator,
  145. * drop the unsupported bits
  146. */
  147. mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
  148. state = kmem_cache_alloc(extent_state_cache, mask);
  149. if (!state)
  150. return state;
  151. state->state = 0;
  152. RB_CLEAR_NODE(&state->rb_node);
  153. btrfs_leak_debug_add_state(state);
  154. refcount_set(&state->refs, 1);
  155. init_waitqueue_head(&state->wq);
  156. trace_alloc_extent_state(state, mask, _RET_IP_);
  157. return state;
  158. }
  159. static struct extent_state *alloc_extent_state_atomic(struct extent_state *prealloc)
  160. {
  161. if (!prealloc)
  162. prealloc = alloc_extent_state(GFP_ATOMIC);
  163. return prealloc;
  164. }
  165. void free_extent_state(struct extent_state *state)
  166. {
  167. if (!state)
  168. return;
  169. if (refcount_dec_and_test(&state->refs)) {
  170. WARN_ON(extent_state_in_tree(state));
  171. btrfs_leak_debug_del_state(state);
  172. trace_free_extent_state(state, _RET_IP_);
  173. kmem_cache_free(extent_state_cache, state);
  174. }
  175. }
  176. static int add_extent_changeset(struct extent_state *state, u32 bits,
  177. struct extent_changeset *changeset,
  178. int set)
  179. {
  180. int ret;
  181. if (!changeset)
  182. return 0;
  183. if (set && (state->state & bits) == bits)
  184. return 0;
  185. if (!set && (state->state & bits) == 0)
  186. return 0;
  187. changeset->bytes_changed += state->end - state->start + 1;
  188. ret = ulist_add(&changeset->range_changed, state->start, state->end,
  189. GFP_ATOMIC);
  190. return ret;
  191. }
  192. static inline struct extent_state *next_state(struct extent_state *state)
  193. {
  194. struct rb_node *next = rb_next(&state->rb_node);
  195. if (next)
  196. return rb_entry(next, struct extent_state, rb_node);
  197. else
  198. return NULL;
  199. }
  200. static inline struct extent_state *prev_state(struct extent_state *state)
  201. {
  202. struct rb_node *next = rb_prev(&state->rb_node);
  203. if (next)
  204. return rb_entry(next, struct extent_state, rb_node);
  205. else
  206. return NULL;
  207. }
  208. /*
  209. * Search @tree for an entry that contains @offset. Such entry would have
  210. * entry->start <= offset && entry->end >= offset.
  211. *
  212. * @tree: the tree to search
  213. * @offset: offset that should fall within an entry in @tree
  214. * @node_ret: pointer where new node should be anchored (used when inserting an
  215. * entry in the tree)
  216. * @parent_ret: points to entry which would have been the parent of the entry,
  217. * containing @offset
  218. *
  219. * Return a pointer to the entry that contains @offset byte address and don't change
  220. * @node_ret and @parent_ret.
  221. *
  222. * If no such entry exists, return pointer to entry that ends before @offset
  223. * and fill parameters @node_ret and @parent_ret, ie. does not return NULL.
  224. */
  225. static inline struct extent_state *tree_search_for_insert(struct extent_io_tree *tree,
  226. u64 offset,
  227. struct rb_node ***node_ret,
  228. struct rb_node **parent_ret)
  229. {
  230. struct rb_root *root = &tree->state;
  231. struct rb_node **node = &root->rb_node;
  232. struct rb_node *prev = NULL;
  233. struct extent_state *entry = NULL;
  234. while (*node) {
  235. prev = *node;
  236. entry = rb_entry(prev, struct extent_state, rb_node);
  237. if (offset < entry->start)
  238. node = &(*node)->rb_left;
  239. else if (offset > entry->end)
  240. node = &(*node)->rb_right;
  241. else
  242. return entry;
  243. }
  244. if (node_ret)
  245. *node_ret = node;
  246. if (parent_ret)
  247. *parent_ret = prev;
  248. /* Search neighbors until we find the first one past the end */
  249. while (entry && offset > entry->end)
  250. entry = next_state(entry);
  251. return entry;
  252. }
  253. /*
  254. * Search offset in the tree or fill neighbor rbtree node pointers.
  255. *
  256. * @tree: the tree to search
  257. * @offset: offset that should fall within an entry in @tree
  258. * @next_ret: pointer to the first entry whose range ends after @offset
  259. * @prev_ret: pointer to the first entry whose range begins before @offset
  260. *
  261. * Return a pointer to the entry that contains @offset byte address. If no
  262. * such entry exists, then return NULL and fill @prev_ret and @next_ret.
  263. * Otherwise return the found entry and other pointers are left untouched.
  264. */
  265. static struct extent_state *tree_search_prev_next(struct extent_io_tree *tree,
  266. u64 offset,
  267. struct extent_state **prev_ret,
  268. struct extent_state **next_ret)
  269. {
  270. struct rb_root *root = &tree->state;
  271. struct rb_node **node = &root->rb_node;
  272. struct extent_state *orig_prev;
  273. struct extent_state *entry = NULL;
  274. ASSERT(prev_ret);
  275. ASSERT(next_ret);
  276. while (*node) {
  277. entry = rb_entry(*node, struct extent_state, rb_node);
  278. if (offset < entry->start)
  279. node = &(*node)->rb_left;
  280. else if (offset > entry->end)
  281. node = &(*node)->rb_right;
  282. else
  283. return entry;
  284. }
  285. orig_prev = entry;
  286. while (entry && offset > entry->end)
  287. entry = next_state(entry);
  288. *next_ret = entry;
  289. entry = orig_prev;
  290. while (entry && offset < entry->start)
  291. entry = prev_state(entry);
  292. *prev_ret = entry;
  293. return NULL;
  294. }
  295. /*
  296. * Inexact rb-tree search, return the next entry if @offset is not found
  297. */
  298. static inline struct extent_state *tree_search(struct extent_io_tree *tree, u64 offset)
  299. {
  300. return tree_search_for_insert(tree, offset, NULL, NULL);
  301. }
  302. static void extent_io_tree_panic(const struct extent_io_tree *tree,
  303. const struct extent_state *state,
  304. const char *opname,
  305. int err)
  306. {
  307. btrfs_panic(extent_io_tree_to_fs_info(tree), err,
  308. "extent io tree error on %s state start %llu end %llu",
  309. opname, state->start, state->end);
  310. }
  311. static void merge_prev_state(struct extent_io_tree *tree, struct extent_state *state)
  312. {
  313. struct extent_state *prev;
  314. prev = prev_state(state);
  315. if (prev && prev->end == state->start - 1 && prev->state == state->state) {
  316. if (is_inode_io_tree(tree))
  317. btrfs_merge_delalloc_extent(extent_io_tree_to_inode(tree),
  318. state, prev);
  319. state->start = prev->start;
  320. rb_erase(&prev->rb_node, &tree->state);
  321. RB_CLEAR_NODE(&prev->rb_node);
  322. free_extent_state(prev);
  323. }
  324. }
  325. static void merge_next_state(struct extent_io_tree *tree, struct extent_state *state)
  326. {
  327. struct extent_state *next;
  328. next = next_state(state);
  329. if (next && next->start == state->end + 1 && next->state == state->state) {
  330. if (is_inode_io_tree(tree))
  331. btrfs_merge_delalloc_extent(extent_io_tree_to_inode(tree),
  332. state, next);
  333. state->end = next->end;
  334. rb_erase(&next->rb_node, &tree->state);
  335. RB_CLEAR_NODE(&next->rb_node);
  336. free_extent_state(next);
  337. }
  338. }
  339. /*
  340. * Utility function to look for merge candidates inside a given range. Any
  341. * extents with matching state are merged together into a single extent in the
  342. * tree. Extents with EXTENT_IO in their state field are not merged because
  343. * the end_io handlers need to be able to do operations on them without
  344. * sleeping (or doing allocations/splits).
  345. *
  346. * This should be called with the tree lock held.
  347. */
  348. static void merge_state(struct extent_io_tree *tree, struct extent_state *state)
  349. {
  350. if (state->state & (EXTENT_LOCK_BITS | EXTENT_BOUNDARY))
  351. return;
  352. merge_prev_state(tree, state);
  353. merge_next_state(tree, state);
  354. }
  355. static void set_state_bits(struct extent_io_tree *tree,
  356. struct extent_state *state,
  357. u32 bits, struct extent_changeset *changeset)
  358. {
  359. u32 bits_to_set = bits & ~EXTENT_CTLBITS;
  360. int ret;
  361. if (is_inode_io_tree(tree))
  362. btrfs_set_delalloc_extent(extent_io_tree_to_inode(tree), state, bits);
  363. ret = add_extent_changeset(state, bits_to_set, changeset, 1);
  364. BUG_ON(ret < 0);
  365. state->state |= bits_to_set;
  366. }
  367. /*
  368. * Insert an extent_state struct into the tree. 'bits' are set on the
  369. * struct before it is inserted.
  370. *
  371. * Returns a pointer to the struct extent_state record containing the range
  372. * requested for insertion, which may be the same as the given struct or it
  373. * may be an existing record in the tree that was expanded to accommodate the
  374. * requested range. In case of an extent_state different from the one that was
  375. * given, the later can be freed or reused by the caller.
  376. *
  377. * On error it returns an error pointer.
  378. *
  379. * The tree lock is not taken internally. This is a utility function and
  380. * probably isn't what you want to call (see set/clear_extent_bit).
  381. */
  382. static struct extent_state *insert_state(struct extent_io_tree *tree,
  383. struct extent_state *state,
  384. u32 bits,
  385. struct extent_changeset *changeset)
  386. {
  387. struct rb_node **node;
  388. struct rb_node *parent = NULL;
  389. const u64 start = state->start - 1;
  390. const u64 end = state->end + 1;
  391. const bool try_merge = !(bits & (EXTENT_LOCK_BITS | EXTENT_BOUNDARY));
  392. set_state_bits(tree, state, bits, changeset);
  393. node = &tree->state.rb_node;
  394. while (*node) {
  395. struct extent_state *entry;
  396. parent = *node;
  397. entry = rb_entry(parent, struct extent_state, rb_node);
  398. if (state->end < entry->start) {
  399. if (try_merge && end == entry->start &&
  400. state->state == entry->state) {
  401. if (is_inode_io_tree(tree))
  402. btrfs_merge_delalloc_extent(
  403. extent_io_tree_to_inode(tree),
  404. state, entry);
  405. entry->start = state->start;
  406. merge_prev_state(tree, entry);
  407. state->state = 0;
  408. return entry;
  409. }
  410. node = &(*node)->rb_left;
  411. } else if (state->end > entry->end) {
  412. if (try_merge && entry->end == start &&
  413. state->state == entry->state) {
  414. if (is_inode_io_tree(tree))
  415. btrfs_merge_delalloc_extent(
  416. extent_io_tree_to_inode(tree),
  417. state, entry);
  418. entry->end = state->end;
  419. merge_next_state(tree, entry);
  420. state->state = 0;
  421. return entry;
  422. }
  423. node = &(*node)->rb_right;
  424. } else {
  425. return ERR_PTR(-EEXIST);
  426. }
  427. }
  428. rb_link_node(&state->rb_node, parent, node);
  429. rb_insert_color(&state->rb_node, &tree->state);
  430. return state;
  431. }
  432. /*
  433. * Insert state to @tree to the location given by @node and @parent.
  434. */
  435. static void insert_state_fast(struct extent_io_tree *tree,
  436. struct extent_state *state, struct rb_node **node,
  437. struct rb_node *parent, unsigned bits,
  438. struct extent_changeset *changeset)
  439. {
  440. set_state_bits(tree, state, bits, changeset);
  441. rb_link_node(&state->rb_node, parent, node);
  442. rb_insert_color(&state->rb_node, &tree->state);
  443. merge_state(tree, state);
  444. }
  445. /*
  446. * Split a given extent state struct in two, inserting the preallocated
  447. * struct 'prealloc' as the newly created second half. 'split' indicates an
  448. * offset inside 'orig' where it should be split.
  449. *
  450. * Before calling,
  451. * the tree has 'orig' at [orig->start, orig->end]. After calling, there
  452. * are two extent state structs in the tree:
  453. * prealloc: [orig->start, split - 1]
  454. * orig: [ split, orig->end ]
  455. *
  456. * The tree locks are not taken by this function. They need to be held
  457. * by the caller.
  458. */
  459. static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
  460. struct extent_state *prealloc, u64 split)
  461. {
  462. struct rb_node *parent = NULL;
  463. struct rb_node **node;
  464. if (is_inode_io_tree(tree))
  465. btrfs_split_delalloc_extent(extent_io_tree_to_inode(tree), orig,
  466. split);
  467. prealloc->start = orig->start;
  468. prealloc->end = split - 1;
  469. prealloc->state = orig->state;
  470. orig->start = split;
  471. parent = &orig->rb_node;
  472. node = &parent;
  473. while (*node) {
  474. struct extent_state *entry;
  475. parent = *node;
  476. entry = rb_entry(parent, struct extent_state, rb_node);
  477. if (prealloc->end < entry->start) {
  478. node = &(*node)->rb_left;
  479. } else if (prealloc->end > entry->end) {
  480. node = &(*node)->rb_right;
  481. } else {
  482. free_extent_state(prealloc);
  483. return -EEXIST;
  484. }
  485. }
  486. rb_link_node(&prealloc->rb_node, parent, node);
  487. rb_insert_color(&prealloc->rb_node, &tree->state);
  488. return 0;
  489. }
  490. /*
  491. * Utility function to clear some bits in an extent state struct. It will
  492. * optionally wake up anyone waiting on this state (wake == 1).
  493. *
  494. * If no bits are set on the state struct after clearing things, the
  495. * struct is freed and removed from the tree
  496. */
  497. static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
  498. struct extent_state *state,
  499. u32 bits, int wake,
  500. struct extent_changeset *changeset)
  501. {
  502. struct extent_state *next;
  503. u32 bits_to_clear = bits & ~EXTENT_CTLBITS;
  504. int ret;
  505. if (is_inode_io_tree(tree))
  506. btrfs_clear_delalloc_extent(extent_io_tree_to_inode(tree), state,
  507. bits);
  508. ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
  509. BUG_ON(ret < 0);
  510. state->state &= ~bits_to_clear;
  511. if (wake)
  512. wake_up(&state->wq);
  513. if (state->state == 0) {
  514. next = next_state(state);
  515. if (extent_state_in_tree(state)) {
  516. rb_erase(&state->rb_node, &tree->state);
  517. RB_CLEAR_NODE(&state->rb_node);
  518. free_extent_state(state);
  519. } else {
  520. WARN_ON(1);
  521. }
  522. } else {
  523. merge_state(tree, state);
  524. next = next_state(state);
  525. }
  526. return next;
  527. }
  528. /*
  529. * Detect if extent bits request NOWAIT semantics and set the gfp mask accordingly,
  530. * unset the EXTENT_NOWAIT bit.
  531. */
  532. static void set_gfp_mask_from_bits(u32 *bits, gfp_t *mask)
  533. {
  534. *mask = (*bits & EXTENT_NOWAIT ? GFP_NOWAIT : GFP_NOFS);
  535. *bits &= EXTENT_NOWAIT - 1;
  536. }
  537. /*
  538. * Clear some bits on a range in the tree. This may require splitting or
  539. * inserting elements in the tree, so the gfp mask is used to indicate which
  540. * allocations or sleeping are allowed.
  541. *
  542. * The range [start, end] is inclusive.
  543. *
  544. * This takes the tree lock, and returns 0 on success and < 0 on error.
  545. */
  546. int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  547. u32 bits, struct extent_state **cached_state,
  548. struct extent_changeset *changeset)
  549. {
  550. struct extent_state *state;
  551. struct extent_state *cached;
  552. struct extent_state *prealloc = NULL;
  553. u64 last_end;
  554. int err;
  555. int clear = 0;
  556. int wake;
  557. int delete = (bits & EXTENT_CLEAR_ALL_BITS);
  558. gfp_t mask;
  559. set_gfp_mask_from_bits(&bits, &mask);
  560. btrfs_debug_check_extent_io_range(tree, start, end);
  561. trace_btrfs_clear_extent_bit(tree, start, end - start + 1, bits);
  562. if (delete)
  563. bits |= ~EXTENT_CTLBITS;
  564. if (bits & EXTENT_DELALLOC)
  565. bits |= EXTENT_NORESERVE;
  566. wake = ((bits & EXTENT_LOCK_BITS) ? 1 : 0);
  567. if (bits & (EXTENT_LOCK_BITS | EXTENT_BOUNDARY))
  568. clear = 1;
  569. again:
  570. if (!prealloc) {
  571. /*
  572. * Don't care for allocation failure here because we might end
  573. * up not needing the pre-allocated extent state at all, which
  574. * is the case if we only have in the tree extent states that
  575. * cover our input range and don't cover too any other range.
  576. * If we end up needing a new extent state we allocate it later.
  577. */
  578. prealloc = alloc_extent_state(mask);
  579. }
  580. spin_lock(&tree->lock);
  581. if (cached_state) {
  582. cached = *cached_state;
  583. if (clear) {
  584. *cached_state = NULL;
  585. cached_state = NULL;
  586. }
  587. if (cached && extent_state_in_tree(cached) &&
  588. cached->start <= start && cached->end > start) {
  589. if (clear)
  590. refcount_dec(&cached->refs);
  591. state = cached;
  592. goto hit_next;
  593. }
  594. if (clear)
  595. free_extent_state(cached);
  596. }
  597. /* This search will find the extents that end after our range starts. */
  598. state = tree_search(tree, start);
  599. if (!state)
  600. goto out;
  601. hit_next:
  602. if (state->start > end)
  603. goto out;
  604. WARN_ON(state->end < start);
  605. last_end = state->end;
  606. /* The state doesn't have the wanted bits, go ahead. */
  607. if (!(state->state & bits)) {
  608. state = next_state(state);
  609. goto next;
  610. }
  611. /*
  612. * | ---- desired range ---- |
  613. * | state | or
  614. * | ------------- state -------------- |
  615. *
  616. * We need to split the extent we found, and may flip bits on second
  617. * half.
  618. *
  619. * If the extent we found extends past our range, we just split and
  620. * search again. It'll get split again the next time though.
  621. *
  622. * If the extent we found is inside our range, we clear the desired bit
  623. * on it.
  624. */
  625. if (state->start < start) {
  626. prealloc = alloc_extent_state_atomic(prealloc);
  627. if (!prealloc)
  628. goto search_again;
  629. err = split_state(tree, state, prealloc, start);
  630. if (err)
  631. extent_io_tree_panic(tree, state, "split", err);
  632. prealloc = NULL;
  633. if (err)
  634. goto out;
  635. if (state->end <= end) {
  636. state = clear_state_bit(tree, state, bits, wake, changeset);
  637. goto next;
  638. }
  639. goto search_again;
  640. }
  641. /*
  642. * | ---- desired range ---- |
  643. * | state |
  644. * We need to split the extent, and clear the bit on the first half.
  645. */
  646. if (state->start <= end && state->end > end) {
  647. prealloc = alloc_extent_state_atomic(prealloc);
  648. if (!prealloc)
  649. goto search_again;
  650. err = split_state(tree, state, prealloc, end + 1);
  651. if (err)
  652. extent_io_tree_panic(tree, state, "split", err);
  653. if (wake)
  654. wake_up(&state->wq);
  655. clear_state_bit(tree, prealloc, bits, wake, changeset);
  656. prealloc = NULL;
  657. goto out;
  658. }
  659. state = clear_state_bit(tree, state, bits, wake, changeset);
  660. next:
  661. if (last_end == (u64)-1)
  662. goto out;
  663. start = last_end + 1;
  664. if (start <= end && state && !need_resched())
  665. goto hit_next;
  666. search_again:
  667. if (start > end)
  668. goto out;
  669. spin_unlock(&tree->lock);
  670. if (gfpflags_allow_blocking(mask))
  671. cond_resched();
  672. goto again;
  673. out:
  674. spin_unlock(&tree->lock);
  675. if (prealloc)
  676. free_extent_state(prealloc);
  677. return 0;
  678. }
  679. /*
  680. * Wait for one or more bits to clear on a range in the state tree.
  681. * The range [start, end] is inclusive.
  682. * The tree lock is taken by this function
  683. */
  684. static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  685. u32 bits, struct extent_state **cached_state)
  686. {
  687. struct extent_state *state;
  688. btrfs_debug_check_extent_io_range(tree, start, end);
  689. spin_lock(&tree->lock);
  690. again:
  691. /*
  692. * Maintain cached_state, as we may not remove it from the tree if there
  693. * are more bits than the bits we're waiting on set on this state.
  694. */
  695. if (cached_state && *cached_state) {
  696. state = *cached_state;
  697. if (extent_state_in_tree(state) &&
  698. state->start <= start && start < state->end)
  699. goto process_node;
  700. }
  701. while (1) {
  702. /*
  703. * This search will find all the extents that end after our
  704. * range starts.
  705. */
  706. state = tree_search(tree, start);
  707. process_node:
  708. if (!state)
  709. break;
  710. if (state->start > end)
  711. goto out;
  712. if (state->state & bits) {
  713. DEFINE_WAIT(wait);
  714. start = state->start;
  715. refcount_inc(&state->refs);
  716. prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
  717. spin_unlock(&tree->lock);
  718. schedule();
  719. spin_lock(&tree->lock);
  720. finish_wait(&state->wq, &wait);
  721. free_extent_state(state);
  722. goto again;
  723. }
  724. start = state->end + 1;
  725. if (start > end)
  726. break;
  727. if (!cond_resched_lock(&tree->lock)) {
  728. state = next_state(state);
  729. goto process_node;
  730. }
  731. }
  732. out:
  733. /* This state is no longer useful, clear it and free it up. */
  734. if (cached_state && *cached_state) {
  735. state = *cached_state;
  736. *cached_state = NULL;
  737. free_extent_state(state);
  738. }
  739. spin_unlock(&tree->lock);
  740. }
  741. static void cache_state_if_flags(struct extent_state *state,
  742. struct extent_state **cached_ptr,
  743. unsigned flags)
  744. {
  745. if (cached_ptr && !(*cached_ptr)) {
  746. if (!flags || (state->state & flags)) {
  747. *cached_ptr = state;
  748. refcount_inc(&state->refs);
  749. }
  750. }
  751. }
  752. static void cache_state(struct extent_state *state,
  753. struct extent_state **cached_ptr)
  754. {
  755. return cache_state_if_flags(state, cached_ptr, EXTENT_LOCK_BITS | EXTENT_BOUNDARY);
  756. }
  757. /*
  758. * Find the first state struct with 'bits' set after 'start', and return it.
  759. * tree->lock must be held. NULL will returned if nothing was found after
  760. * 'start'.
  761. */
  762. static struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
  763. u64 start, u32 bits)
  764. {
  765. struct extent_state *state;
  766. /*
  767. * This search will find all the extents that end after our range
  768. * starts.
  769. */
  770. state = tree_search(tree, start);
  771. while (state) {
  772. if (state->end >= start && (state->state & bits))
  773. return state;
  774. state = next_state(state);
  775. }
  776. return NULL;
  777. }
  778. /*
  779. * Find the first offset in the io tree with one or more @bits set.
  780. *
  781. * Note: If there are multiple bits set in @bits, any of them will match.
  782. *
  783. * Return true if we find something, and update @start_ret and @end_ret.
  784. * Return false if we found nothing.
  785. */
  786. bool find_first_extent_bit(struct extent_io_tree *tree, u64 start,
  787. u64 *start_ret, u64 *end_ret, u32 bits,
  788. struct extent_state **cached_state)
  789. {
  790. struct extent_state *state;
  791. bool ret = false;
  792. spin_lock(&tree->lock);
  793. if (cached_state && *cached_state) {
  794. state = *cached_state;
  795. if (state->end == start - 1 && extent_state_in_tree(state)) {
  796. while ((state = next_state(state)) != NULL) {
  797. if (state->state & bits)
  798. break;
  799. }
  800. /*
  801. * If we found the next extent state, clear cached_state
  802. * so that we can cache the next extent state below and
  803. * avoid future calls going over the same extent state
  804. * again. If we haven't found any, clear as well since
  805. * it's now useless.
  806. */
  807. free_extent_state(*cached_state);
  808. *cached_state = NULL;
  809. if (state)
  810. goto got_it;
  811. goto out;
  812. }
  813. free_extent_state(*cached_state);
  814. *cached_state = NULL;
  815. }
  816. state = find_first_extent_bit_state(tree, start, bits);
  817. got_it:
  818. if (state) {
  819. cache_state_if_flags(state, cached_state, 0);
  820. *start_ret = state->start;
  821. *end_ret = state->end;
  822. ret = true;
  823. }
  824. out:
  825. spin_unlock(&tree->lock);
  826. return ret;
  827. }
  828. /*
  829. * Find a contiguous area of bits
  830. *
  831. * @tree: io tree to check
  832. * @start: offset to start the search from
  833. * @start_ret: the first offset we found with the bits set
  834. * @end_ret: the final contiguous range of the bits that were set
  835. * @bits: bits to look for
  836. *
  837. * set_extent_bit and clear_extent_bit can temporarily split contiguous ranges
  838. * to set bits appropriately, and then merge them again. During this time it
  839. * will drop the tree->lock, so use this helper if you want to find the actual
  840. * contiguous area for given bits. We will search to the first bit we find, and
  841. * then walk down the tree until we find a non-contiguous area. The area
  842. * returned will be the full contiguous area with the bits set.
  843. */
  844. int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
  845. u64 *start_ret, u64 *end_ret, u32 bits)
  846. {
  847. struct extent_state *state;
  848. int ret = 1;
  849. ASSERT(!btrfs_fs_incompat(extent_io_tree_to_fs_info(tree), NO_HOLES));
  850. spin_lock(&tree->lock);
  851. state = find_first_extent_bit_state(tree, start, bits);
  852. if (state) {
  853. *start_ret = state->start;
  854. *end_ret = state->end;
  855. while ((state = next_state(state)) != NULL) {
  856. if (state->start > (*end_ret + 1))
  857. break;
  858. *end_ret = state->end;
  859. }
  860. ret = 0;
  861. }
  862. spin_unlock(&tree->lock);
  863. return ret;
  864. }
  865. /*
  866. * Find a contiguous range of bytes in the file marked as delalloc, not more
  867. * than 'max_bytes'. start and end are used to return the range,
  868. *
  869. * True is returned if we find something, false if nothing was in the tree.
  870. */
  871. bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
  872. u64 *end, u64 max_bytes,
  873. struct extent_state **cached_state)
  874. {
  875. struct extent_state *state;
  876. u64 cur_start = *start;
  877. bool found = false;
  878. u64 total_bytes = 0;
  879. spin_lock(&tree->lock);
  880. /*
  881. * This search will find all the extents that end after our range
  882. * starts.
  883. */
  884. state = tree_search(tree, cur_start);
  885. if (!state) {
  886. *end = (u64)-1;
  887. goto out;
  888. }
  889. while (state) {
  890. if (found && (state->start != cur_start ||
  891. (state->state & EXTENT_BOUNDARY))) {
  892. goto out;
  893. }
  894. if (!(state->state & EXTENT_DELALLOC)) {
  895. if (!found)
  896. *end = state->end;
  897. goto out;
  898. }
  899. if (!found) {
  900. *start = state->start;
  901. *cached_state = state;
  902. refcount_inc(&state->refs);
  903. }
  904. found = true;
  905. *end = state->end;
  906. cur_start = state->end + 1;
  907. total_bytes += state->end - state->start + 1;
  908. if (total_bytes >= max_bytes)
  909. break;
  910. state = next_state(state);
  911. }
  912. out:
  913. spin_unlock(&tree->lock);
  914. return found;
  915. }
  916. /*
  917. * Set some bits on a range in the tree. This may require allocations or
  918. * sleeping. By default all allocations use GFP_NOFS, use EXTENT_NOWAIT for
  919. * GFP_NOWAIT.
  920. *
  921. * If any of the exclusive bits are set, this will fail with -EEXIST if some
  922. * part of the range already has the desired bits set. The extent_state of the
  923. * existing range is returned in failed_state in this case, and the start of the
  924. * existing range is returned in failed_start. failed_state is used as an
  925. * optimization for wait_extent_bit, failed_start must be used as the source of
  926. * truth as failed_state may have changed since we returned.
  927. *
  928. * [start, end] is inclusive This takes the tree lock.
  929. */
  930. static int __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  931. u32 bits, u64 *failed_start,
  932. struct extent_state **failed_state,
  933. struct extent_state **cached_state,
  934. struct extent_changeset *changeset)
  935. {
  936. struct extent_state *state;
  937. struct extent_state *prealloc = NULL;
  938. struct rb_node **p = NULL;
  939. struct rb_node *parent = NULL;
  940. int ret = 0;
  941. u64 last_start;
  942. u64 last_end;
  943. u32 exclusive_bits = (bits & EXTENT_LOCK_BITS);
  944. gfp_t mask;
  945. set_gfp_mask_from_bits(&bits, &mask);
  946. btrfs_debug_check_extent_io_range(tree, start, end);
  947. trace_btrfs_set_extent_bit(tree, start, end - start + 1, bits);
  948. if (exclusive_bits)
  949. ASSERT(failed_start);
  950. else
  951. ASSERT(failed_start == NULL && failed_state == NULL);
  952. again:
  953. if (!prealloc) {
  954. /*
  955. * Don't care for allocation failure here because we might end
  956. * up not needing the pre-allocated extent state at all, which
  957. * is the case if we only have in the tree extent states that
  958. * cover our input range and don't cover too any other range.
  959. * If we end up needing a new extent state we allocate it later.
  960. */
  961. prealloc = alloc_extent_state(mask);
  962. }
  963. /* Optimistically preallocate the extent changeset ulist node. */
  964. if (changeset)
  965. extent_changeset_prealloc(changeset, mask);
  966. spin_lock(&tree->lock);
  967. if (cached_state && *cached_state) {
  968. state = *cached_state;
  969. if (state->start <= start && state->end > start &&
  970. extent_state_in_tree(state))
  971. goto hit_next;
  972. }
  973. /*
  974. * This search will find all the extents that end after our range
  975. * starts.
  976. */
  977. state = tree_search_for_insert(tree, start, &p, &parent);
  978. if (!state) {
  979. prealloc = alloc_extent_state_atomic(prealloc);
  980. if (!prealloc)
  981. goto search_again;
  982. prealloc->start = start;
  983. prealloc->end = end;
  984. insert_state_fast(tree, prealloc, p, parent, bits, changeset);
  985. cache_state(prealloc, cached_state);
  986. prealloc = NULL;
  987. goto out;
  988. }
  989. hit_next:
  990. last_start = state->start;
  991. last_end = state->end;
  992. /*
  993. * | ---- desired range ---- |
  994. * | state |
  995. *
  996. * Just lock what we found and keep going
  997. */
  998. if (state->start == start && state->end <= end) {
  999. if (state->state & exclusive_bits) {
  1000. *failed_start = state->start;
  1001. cache_state(state, failed_state);
  1002. ret = -EEXIST;
  1003. goto out;
  1004. }
  1005. set_state_bits(tree, state, bits, changeset);
  1006. cache_state(state, cached_state);
  1007. merge_state(tree, state);
  1008. if (last_end == (u64)-1)
  1009. goto out;
  1010. start = last_end + 1;
  1011. state = next_state(state);
  1012. if (start < end && state && state->start == start &&
  1013. !need_resched())
  1014. goto hit_next;
  1015. goto search_again;
  1016. }
  1017. /*
  1018. * | ---- desired range ---- |
  1019. * | state |
  1020. * or
  1021. * | ------------- state -------------- |
  1022. *
  1023. * We need to split the extent we found, and may flip bits on second
  1024. * half.
  1025. *
  1026. * If the extent we found extends past our range, we just split and
  1027. * search again. It'll get split again the next time though.
  1028. *
  1029. * If the extent we found is inside our range, we set the desired bit
  1030. * on it.
  1031. */
  1032. if (state->start < start) {
  1033. if (state->state & exclusive_bits) {
  1034. *failed_start = start;
  1035. cache_state(state, failed_state);
  1036. ret = -EEXIST;
  1037. goto out;
  1038. }
  1039. /*
  1040. * If this extent already has all the bits we want set, then
  1041. * skip it, not necessary to split it or do anything with it.
  1042. */
  1043. if ((state->state & bits) == bits) {
  1044. start = state->end + 1;
  1045. cache_state(state, cached_state);
  1046. goto search_again;
  1047. }
  1048. prealloc = alloc_extent_state_atomic(prealloc);
  1049. if (!prealloc)
  1050. goto search_again;
  1051. ret = split_state(tree, state, prealloc, start);
  1052. if (ret)
  1053. extent_io_tree_panic(tree, state, "split", ret);
  1054. prealloc = NULL;
  1055. if (ret)
  1056. goto out;
  1057. if (state->end <= end) {
  1058. set_state_bits(tree, state, bits, changeset);
  1059. cache_state(state, cached_state);
  1060. merge_state(tree, state);
  1061. if (last_end == (u64)-1)
  1062. goto out;
  1063. start = last_end + 1;
  1064. state = next_state(state);
  1065. if (start < end && state && state->start == start &&
  1066. !need_resched())
  1067. goto hit_next;
  1068. }
  1069. goto search_again;
  1070. }
  1071. /*
  1072. * | ---- desired range ---- |
  1073. * | state | or | state |
  1074. *
  1075. * There's a hole, we need to insert something in it and ignore the
  1076. * extent we found.
  1077. */
  1078. if (state->start > start) {
  1079. u64 this_end;
  1080. struct extent_state *inserted_state;
  1081. if (end < last_start)
  1082. this_end = end;
  1083. else
  1084. this_end = last_start - 1;
  1085. prealloc = alloc_extent_state_atomic(prealloc);
  1086. if (!prealloc)
  1087. goto search_again;
  1088. /*
  1089. * Avoid to free 'prealloc' if it can be merged with the later
  1090. * extent.
  1091. */
  1092. prealloc->start = start;
  1093. prealloc->end = this_end;
  1094. inserted_state = insert_state(tree, prealloc, bits, changeset);
  1095. if (IS_ERR(inserted_state)) {
  1096. ret = PTR_ERR(inserted_state);
  1097. extent_io_tree_panic(tree, prealloc, "insert", ret);
  1098. }
  1099. cache_state(inserted_state, cached_state);
  1100. if (inserted_state == prealloc)
  1101. prealloc = NULL;
  1102. start = this_end + 1;
  1103. goto search_again;
  1104. }
  1105. /*
  1106. * | ---- desired range ---- |
  1107. * | state |
  1108. *
  1109. * We need to split the extent, and set the bit on the first half
  1110. */
  1111. if (state->start <= end && state->end > end) {
  1112. if (state->state & exclusive_bits) {
  1113. *failed_start = start;
  1114. cache_state(state, failed_state);
  1115. ret = -EEXIST;
  1116. goto out;
  1117. }
  1118. prealloc = alloc_extent_state_atomic(prealloc);
  1119. if (!prealloc)
  1120. goto search_again;
  1121. ret = split_state(tree, state, prealloc, end + 1);
  1122. if (ret) {
  1123. extent_io_tree_panic(tree, state, "split", ret);
  1124. prealloc = NULL;
  1125. goto out;
  1126. }
  1127. set_state_bits(tree, prealloc, bits, changeset);
  1128. cache_state(prealloc, cached_state);
  1129. merge_state(tree, prealloc);
  1130. prealloc = NULL;
  1131. goto out;
  1132. }
  1133. search_again:
  1134. if (start > end)
  1135. goto out;
  1136. spin_unlock(&tree->lock);
  1137. if (gfpflags_allow_blocking(mask))
  1138. cond_resched();
  1139. goto again;
  1140. out:
  1141. spin_unlock(&tree->lock);
  1142. if (prealloc)
  1143. free_extent_state(prealloc);
  1144. return ret;
  1145. }
  1146. int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  1147. u32 bits, struct extent_state **cached_state)
  1148. {
  1149. return __set_extent_bit(tree, start, end, bits, NULL, NULL,
  1150. cached_state, NULL);
  1151. }
  1152. /*
  1153. * Convert all bits in a given range from one bit to another
  1154. *
  1155. * @tree: the io tree to search
  1156. * @start: the start offset in bytes
  1157. * @end: the end offset in bytes (inclusive)
  1158. * @bits: the bits to set in this range
  1159. * @clear_bits: the bits to clear in this range
  1160. * @cached_state: state that we're going to cache
  1161. *
  1162. * This will go through and set bits for the given range. If any states exist
  1163. * already in this range they are set with the given bit and cleared of the
  1164. * clear_bits. This is only meant to be used by things that are mergeable, ie.
  1165. * converting from say DELALLOC to DIRTY. This is not meant to be used with
  1166. * boundary bits like LOCK.
  1167. *
  1168. * All allocations are done with GFP_NOFS.
  1169. */
  1170. int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  1171. u32 bits, u32 clear_bits,
  1172. struct extent_state **cached_state)
  1173. {
  1174. struct extent_state *state;
  1175. struct extent_state *prealloc = NULL;
  1176. struct rb_node **p = NULL;
  1177. struct rb_node *parent = NULL;
  1178. int ret = 0;
  1179. u64 last_start;
  1180. u64 last_end;
  1181. bool first_iteration = true;
  1182. btrfs_debug_check_extent_io_range(tree, start, end);
  1183. trace_btrfs_convert_extent_bit(tree, start, end - start + 1, bits,
  1184. clear_bits);
  1185. again:
  1186. if (!prealloc) {
  1187. /*
  1188. * Best effort, don't worry if extent state allocation fails
  1189. * here for the first iteration. We might have a cached state
  1190. * that matches exactly the target range, in which case no
  1191. * extent state allocations are needed. We'll only know this
  1192. * after locking the tree.
  1193. */
  1194. prealloc = alloc_extent_state(GFP_NOFS);
  1195. if (!prealloc && !first_iteration)
  1196. return -ENOMEM;
  1197. }
  1198. spin_lock(&tree->lock);
  1199. if (cached_state && *cached_state) {
  1200. state = *cached_state;
  1201. if (state->start <= start && state->end > start &&
  1202. extent_state_in_tree(state))
  1203. goto hit_next;
  1204. }
  1205. /*
  1206. * This search will find all the extents that end after our range
  1207. * starts.
  1208. */
  1209. state = tree_search_for_insert(tree, start, &p, &parent);
  1210. if (!state) {
  1211. prealloc = alloc_extent_state_atomic(prealloc);
  1212. if (!prealloc) {
  1213. ret = -ENOMEM;
  1214. goto out;
  1215. }
  1216. prealloc->start = start;
  1217. prealloc->end = end;
  1218. insert_state_fast(tree, prealloc, p, parent, bits, NULL);
  1219. cache_state(prealloc, cached_state);
  1220. prealloc = NULL;
  1221. goto out;
  1222. }
  1223. hit_next:
  1224. last_start = state->start;
  1225. last_end = state->end;
  1226. /*
  1227. * | ---- desired range ---- |
  1228. * | state |
  1229. *
  1230. * Just lock what we found and keep going.
  1231. */
  1232. if (state->start == start && state->end <= end) {
  1233. set_state_bits(tree, state, bits, NULL);
  1234. cache_state(state, cached_state);
  1235. state = clear_state_bit(tree, state, clear_bits, 0, NULL);
  1236. if (last_end == (u64)-1)
  1237. goto out;
  1238. start = last_end + 1;
  1239. if (start < end && state && state->start == start &&
  1240. !need_resched())
  1241. goto hit_next;
  1242. goto search_again;
  1243. }
  1244. /*
  1245. * | ---- desired range ---- |
  1246. * | state |
  1247. * or
  1248. * | ------------- state -------------- |
  1249. *
  1250. * We need to split the extent we found, and may flip bits on second
  1251. * half.
  1252. *
  1253. * If the extent we found extends past our range, we just split and
  1254. * search again. It'll get split again the next time though.
  1255. *
  1256. * If the extent we found is inside our range, we set the desired bit
  1257. * on it.
  1258. */
  1259. if (state->start < start) {
  1260. prealloc = alloc_extent_state_atomic(prealloc);
  1261. if (!prealloc) {
  1262. ret = -ENOMEM;
  1263. goto out;
  1264. }
  1265. ret = split_state(tree, state, prealloc, start);
  1266. if (ret)
  1267. extent_io_tree_panic(tree, state, "split", ret);
  1268. prealloc = NULL;
  1269. if (ret)
  1270. goto out;
  1271. if (state->end <= end) {
  1272. set_state_bits(tree, state, bits, NULL);
  1273. cache_state(state, cached_state);
  1274. state = clear_state_bit(tree, state, clear_bits, 0, NULL);
  1275. if (last_end == (u64)-1)
  1276. goto out;
  1277. start = last_end + 1;
  1278. if (start < end && state && state->start == start &&
  1279. !need_resched())
  1280. goto hit_next;
  1281. }
  1282. goto search_again;
  1283. }
  1284. /*
  1285. * | ---- desired range ---- |
  1286. * | state | or | state |
  1287. *
  1288. * There's a hole, we need to insert something in it and ignore the
  1289. * extent we found.
  1290. */
  1291. if (state->start > start) {
  1292. u64 this_end;
  1293. struct extent_state *inserted_state;
  1294. if (end < last_start)
  1295. this_end = end;
  1296. else
  1297. this_end = last_start - 1;
  1298. prealloc = alloc_extent_state_atomic(prealloc);
  1299. if (!prealloc) {
  1300. ret = -ENOMEM;
  1301. goto out;
  1302. }
  1303. /*
  1304. * Avoid to free 'prealloc' if it can be merged with the later
  1305. * extent.
  1306. */
  1307. prealloc->start = start;
  1308. prealloc->end = this_end;
  1309. inserted_state = insert_state(tree, prealloc, bits, NULL);
  1310. if (IS_ERR(inserted_state)) {
  1311. ret = PTR_ERR(inserted_state);
  1312. extent_io_tree_panic(tree, prealloc, "insert", ret);
  1313. goto out;
  1314. }
  1315. cache_state(inserted_state, cached_state);
  1316. if (inserted_state == prealloc)
  1317. prealloc = NULL;
  1318. start = this_end + 1;
  1319. goto search_again;
  1320. }
  1321. /*
  1322. * | ---- desired range ---- |
  1323. * | state |
  1324. *
  1325. * We need to split the extent, and set the bit on the first half.
  1326. */
  1327. if (state->start <= end && state->end > end) {
  1328. prealloc = alloc_extent_state_atomic(prealloc);
  1329. if (!prealloc) {
  1330. ret = -ENOMEM;
  1331. goto out;
  1332. }
  1333. ret = split_state(tree, state, prealloc, end + 1);
  1334. if (ret)
  1335. extent_io_tree_panic(tree, state, "split", ret);
  1336. set_state_bits(tree, prealloc, bits, NULL);
  1337. cache_state(prealloc, cached_state);
  1338. clear_state_bit(tree, prealloc, clear_bits, 0, NULL);
  1339. prealloc = NULL;
  1340. goto out;
  1341. }
  1342. search_again:
  1343. if (start > end)
  1344. goto out;
  1345. spin_unlock(&tree->lock);
  1346. cond_resched();
  1347. first_iteration = false;
  1348. goto again;
  1349. out:
  1350. spin_unlock(&tree->lock);
  1351. if (prealloc)
  1352. free_extent_state(prealloc);
  1353. return ret;
  1354. }
  1355. /*
  1356. * Find the first range that has @bits not set. This range could start before
  1357. * @start.
  1358. *
  1359. * @tree: the tree to search
  1360. * @start: offset at/after which the found extent should start
  1361. * @start_ret: records the beginning of the range
  1362. * @end_ret: records the end of the range (inclusive)
  1363. * @bits: the set of bits which must be unset
  1364. *
  1365. * Since unallocated range is also considered one which doesn't have the bits
  1366. * set it's possible that @end_ret contains -1, this happens in case the range
  1367. * spans (last_range_end, end of device]. In this case it's up to the caller to
  1368. * trim @end_ret to the appropriate size.
  1369. */
  1370. void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
  1371. u64 *start_ret, u64 *end_ret, u32 bits)
  1372. {
  1373. struct extent_state *state;
  1374. struct extent_state *prev = NULL, *next = NULL;
  1375. spin_lock(&tree->lock);
  1376. /* Find first extent with bits cleared */
  1377. while (1) {
  1378. state = tree_search_prev_next(tree, start, &prev, &next);
  1379. if (!state && !next && !prev) {
  1380. /*
  1381. * Tree is completely empty, send full range and let
  1382. * caller deal with it
  1383. */
  1384. *start_ret = 0;
  1385. *end_ret = -1;
  1386. goto out;
  1387. } else if (!state && !next) {
  1388. /*
  1389. * We are past the last allocated chunk, set start at
  1390. * the end of the last extent.
  1391. */
  1392. *start_ret = prev->end + 1;
  1393. *end_ret = -1;
  1394. goto out;
  1395. } else if (!state) {
  1396. state = next;
  1397. }
  1398. /*
  1399. * At this point 'state' either contains 'start' or start is
  1400. * before 'state'
  1401. */
  1402. if (in_range(start, state->start, state->end - state->start + 1)) {
  1403. if (state->state & bits) {
  1404. /*
  1405. * |--range with bits sets--|
  1406. * |
  1407. * start
  1408. */
  1409. start = state->end + 1;
  1410. } else {
  1411. /*
  1412. * 'start' falls within a range that doesn't
  1413. * have the bits set, so take its start as the
  1414. * beginning of the desired range
  1415. *
  1416. * |--range with bits cleared----|
  1417. * |
  1418. * start
  1419. */
  1420. *start_ret = state->start;
  1421. break;
  1422. }
  1423. } else {
  1424. /*
  1425. * |---prev range---|---hole/unset---|---node range---|
  1426. * |
  1427. * start
  1428. *
  1429. * or
  1430. *
  1431. * |---hole/unset--||--first node--|
  1432. * 0 |
  1433. * start
  1434. */
  1435. if (prev)
  1436. *start_ret = prev->end + 1;
  1437. else
  1438. *start_ret = 0;
  1439. break;
  1440. }
  1441. }
  1442. /*
  1443. * Find the longest stretch from start until an entry which has the
  1444. * bits set
  1445. */
  1446. while (state) {
  1447. if (state->end >= start && !(state->state & bits)) {
  1448. *end_ret = state->end;
  1449. } else {
  1450. *end_ret = state->start - 1;
  1451. break;
  1452. }
  1453. state = next_state(state);
  1454. }
  1455. out:
  1456. spin_unlock(&tree->lock);
  1457. }
  1458. /*
  1459. * Count the number of bytes in the tree that have a given bit(s) set for a
  1460. * given range.
  1461. *
  1462. * @tree: The io tree to search.
  1463. * @start: The start offset of the range. This value is updated to the
  1464. * offset of the first byte found with the given bit(s), so it
  1465. * can end up being bigger than the initial value.
  1466. * @search_end: The end offset (inclusive value) of the search range.
  1467. * @max_bytes: The maximum byte count we are interested. The search stops
  1468. * once it reaches this count.
  1469. * @bits: The bits the range must have in order to be accounted for.
  1470. * If multiple bits are set, then only subranges that have all
  1471. * the bits set are accounted for.
  1472. * @contig: Indicate if we should ignore holes in the range or not. If
  1473. * this is true, then stop once we find a hole.
  1474. * @cached_state: A cached state to be used across multiple calls to this
  1475. * function in order to speedup searches. Use NULL if this is
  1476. * called only once or if each call does not start where the
  1477. * previous one ended.
  1478. *
  1479. * Returns the total number of bytes found within the given range that have
  1480. * all given bits set. If the returned number of bytes is greater than zero
  1481. * then @start is updated with the offset of the first byte with the bits set.
  1482. */
  1483. u64 count_range_bits(struct extent_io_tree *tree,
  1484. u64 *start, u64 search_end, u64 max_bytes,
  1485. u32 bits, int contig,
  1486. struct extent_state **cached_state)
  1487. {
  1488. struct extent_state *state = NULL;
  1489. struct extent_state *cached;
  1490. u64 cur_start = *start;
  1491. u64 total_bytes = 0;
  1492. u64 last = 0;
  1493. int found = 0;
  1494. if (WARN_ON(search_end < cur_start))
  1495. return 0;
  1496. spin_lock(&tree->lock);
  1497. if (!cached_state || !*cached_state)
  1498. goto search;
  1499. cached = *cached_state;
  1500. if (!extent_state_in_tree(cached))
  1501. goto search;
  1502. if (cached->start <= cur_start && cur_start <= cached->end) {
  1503. state = cached;
  1504. } else if (cached->start > cur_start) {
  1505. struct extent_state *prev;
  1506. /*
  1507. * The cached state starts after our search range's start. Check
  1508. * if the previous state record starts at or before the range we
  1509. * are looking for, and if so, use it - this is a common case
  1510. * when there are holes between records in the tree. If there is
  1511. * no previous state record, we can start from our cached state.
  1512. */
  1513. prev = prev_state(cached);
  1514. if (!prev)
  1515. state = cached;
  1516. else if (prev->start <= cur_start && cur_start <= prev->end)
  1517. state = prev;
  1518. }
  1519. /*
  1520. * This search will find all the extents that end after our range
  1521. * starts.
  1522. */
  1523. search:
  1524. if (!state)
  1525. state = tree_search(tree, cur_start);
  1526. while (state) {
  1527. if (state->start > search_end)
  1528. break;
  1529. if (contig && found && state->start > last + 1)
  1530. break;
  1531. if (state->end >= cur_start && (state->state & bits) == bits) {
  1532. total_bytes += min(search_end, state->end) + 1 -
  1533. max(cur_start, state->start);
  1534. if (total_bytes >= max_bytes)
  1535. break;
  1536. if (!found) {
  1537. *start = max(cur_start, state->start);
  1538. found = 1;
  1539. }
  1540. last = state->end;
  1541. } else if (contig && found) {
  1542. break;
  1543. }
  1544. state = next_state(state);
  1545. }
  1546. if (cached_state) {
  1547. free_extent_state(*cached_state);
  1548. *cached_state = state;
  1549. if (state)
  1550. refcount_inc(&state->refs);
  1551. }
  1552. spin_unlock(&tree->lock);
  1553. return total_bytes;
  1554. }
  1555. /*
  1556. * Check if the single @bit exists in the given range.
  1557. */
  1558. bool test_range_bit_exists(struct extent_io_tree *tree, u64 start, u64 end, u32 bit)
  1559. {
  1560. struct extent_state *state = NULL;
  1561. bool bitset = false;
  1562. ASSERT(is_power_of_2(bit));
  1563. spin_lock(&tree->lock);
  1564. state = tree_search(tree, start);
  1565. while (state && start <= end) {
  1566. if (state->start > end)
  1567. break;
  1568. if (state->state & bit) {
  1569. bitset = true;
  1570. break;
  1571. }
  1572. /* If state->end is (u64)-1, start will overflow to 0 */
  1573. start = state->end + 1;
  1574. if (start > end || start == 0)
  1575. break;
  1576. state = next_state(state);
  1577. }
  1578. spin_unlock(&tree->lock);
  1579. return bitset;
  1580. }
  1581. /*
  1582. * Check if the whole range [@start,@end) contains the single @bit set.
  1583. */
  1584. bool test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bit,
  1585. struct extent_state *cached)
  1586. {
  1587. struct extent_state *state = NULL;
  1588. bool bitset = true;
  1589. ASSERT(is_power_of_2(bit));
  1590. spin_lock(&tree->lock);
  1591. if (cached && extent_state_in_tree(cached) && cached->start <= start &&
  1592. cached->end > start)
  1593. state = cached;
  1594. else
  1595. state = tree_search(tree, start);
  1596. while (state && start <= end) {
  1597. if (state->start > start) {
  1598. bitset = false;
  1599. break;
  1600. }
  1601. if (state->start > end)
  1602. break;
  1603. if ((state->state & bit) == 0) {
  1604. bitset = false;
  1605. break;
  1606. }
  1607. if (state->end == (u64)-1)
  1608. break;
  1609. /*
  1610. * Last entry (if state->end is (u64)-1 and overflow happens),
  1611. * or next entry starts after the range.
  1612. */
  1613. start = state->end + 1;
  1614. if (start > end || start == 0)
  1615. break;
  1616. state = next_state(state);
  1617. }
  1618. /* We ran out of states and were still inside of our range. */
  1619. if (!state)
  1620. bitset = false;
  1621. spin_unlock(&tree->lock);
  1622. return bitset;
  1623. }
  1624. /* Wrappers around set/clear extent bit */
  1625. int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  1626. u32 bits, struct extent_changeset *changeset)
  1627. {
  1628. /*
  1629. * We don't support EXTENT_LOCK_BITS yet, as current changeset will
  1630. * record any bits changed, so for EXTENT_LOCK_BITS case, it will either
  1631. * fail with -EEXIST or changeset will record the whole range.
  1632. */
  1633. ASSERT(!(bits & EXTENT_LOCK_BITS));
  1634. return __set_extent_bit(tree, start, end, bits, NULL, NULL, NULL, changeset);
  1635. }
  1636. int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  1637. u32 bits, struct extent_changeset *changeset)
  1638. {
  1639. /*
  1640. * Don't support EXTENT_LOCK_BITS case, same reason as
  1641. * set_record_extent_bits().
  1642. */
  1643. ASSERT(!(bits & EXTENT_LOCK_BITS));
  1644. return __clear_extent_bit(tree, start, end, bits, NULL, changeset);
  1645. }
  1646. bool __try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,
  1647. struct extent_state **cached)
  1648. {
  1649. int err;
  1650. u64 failed_start;
  1651. err = __set_extent_bit(tree, start, end, bits, &failed_start,
  1652. NULL, cached, NULL);
  1653. if (err == -EEXIST) {
  1654. if (failed_start > start)
  1655. clear_extent_bit(tree, start, failed_start - 1, bits, cached);
  1656. return 0;
  1657. }
  1658. return 1;
  1659. }
  1660. /*
  1661. * Either insert or lock state struct between start and end use mask to tell
  1662. * us if waiting is desired.
  1663. */
  1664. int __lock_extent(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,
  1665. struct extent_state **cached_state)
  1666. {
  1667. struct extent_state *failed_state = NULL;
  1668. int err;
  1669. u64 failed_start;
  1670. err = __set_extent_bit(tree, start, end, bits, &failed_start,
  1671. &failed_state, cached_state, NULL);
  1672. while (err == -EEXIST) {
  1673. if (failed_start != start)
  1674. clear_extent_bit(tree, start, failed_start - 1,
  1675. bits, cached_state);
  1676. wait_extent_bit(tree, failed_start, end, bits, &failed_state);
  1677. err = __set_extent_bit(tree, start, end, bits,
  1678. &failed_start, &failed_state,
  1679. cached_state, NULL);
  1680. }
  1681. return err;
  1682. }
  1683. void __cold extent_state_free_cachep(void)
  1684. {
  1685. btrfs_extent_state_leak_debug_check();
  1686. kmem_cache_destroy(extent_state_cache);
  1687. }
  1688. int __init extent_state_init_cachep(void)
  1689. {
  1690. extent_state_cache = kmem_cache_create("btrfs_extent_state",
  1691. sizeof(struct extent_state), 0, 0,
  1692. NULL);
  1693. if (!extent_state_cache)
  1694. return -ENOMEM;
  1695. return 0;
  1696. }