PageRenderTime 52ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/btrfs/extent_io.c

https://github.com/mstsirkin/linux
C | 2015 lines | 1528 code | 210 blank | 277 comment | 338 complexity | d75ea07dcfe2865ddc06de1a6ffb9a26 MD5 | raw file
  1. #include <linux/bitops.h>
  2. #include <linux/slab.h>
  3. #include <linux/bio.h>
  4. #include <linux/mm.h>
  5. #include <linux/pagemap.h>
  6. #include <linux/page-flags.h>
  7. #include <linux/module.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/swap.h>
  11. #include <linux/writeback.h>
  12. #include <linux/pagevec.h>
  13. #include <linux/prefetch.h>
  14. #include <linux/cleancache.h>
  15. #include "extent_io.h"
  16. #include "extent_map.h"
  17. #include "compat.h"
  18. #include "ctree.h"
  19. #include "btrfs_inode.h"
  20. static struct kmem_cache *extent_state_cache;
  21. static struct kmem_cache *extent_buffer_cache;
  22. static LIST_HEAD(buffers);
  23. static LIST_HEAD(states);
  24. #define LEAK_DEBUG 0
  25. #if LEAK_DEBUG
  26. static DEFINE_SPINLOCK(leak_lock);
  27. #endif
  28. #define BUFFER_LRU_MAX 64
  29. struct tree_entry {
  30. u64 start;
  31. u64 end;
  32. struct rb_node rb_node;
  33. };
  34. struct extent_page_data {
  35. struct bio *bio;
  36. struct extent_io_tree *tree;
  37. get_extent_t *get_extent;
  38. /* tells writepage not to lock the state bits for this range
  39. * it still does the unlocking
  40. */
  41. unsigned int extent_locked:1;
  42. /* tells the submit_bio code to use a WRITE_SYNC */
  43. unsigned int sync_io:1;
  44. };
  45. int __init extent_io_init(void)
  46. {
  47. extent_state_cache = kmem_cache_create("extent_state",
  48. sizeof(struct extent_state), 0,
  49. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  50. if (!extent_state_cache)
  51. return -ENOMEM;
  52. extent_buffer_cache = kmem_cache_create("extent_buffers",
  53. sizeof(struct extent_buffer), 0,
  54. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  55. if (!extent_buffer_cache)
  56. goto free_state_cache;
  57. return 0;
  58. free_state_cache:
  59. kmem_cache_destroy(extent_state_cache);
  60. return -ENOMEM;
  61. }
  62. void extent_io_exit(void)
  63. {
  64. struct extent_state *state;
  65. struct extent_buffer *eb;
  66. while (!list_empty(&states)) {
  67. state = list_entry(states.next, struct extent_state, leak_list);
  68. printk(KERN_ERR "btrfs state leak: start %llu end %llu "
  69. "state %lu in tree %p refs %d\n",
  70. (unsigned long long)state->start,
  71. (unsigned long long)state->end,
  72. state->state, state->tree, atomic_read(&state->refs));
  73. list_del(&state->leak_list);
  74. kmem_cache_free(extent_state_cache, state);
  75. }
  76. while (!list_empty(&buffers)) {
  77. eb = list_entry(buffers.next, struct extent_buffer, leak_list);
  78. printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
  79. "refs %d\n", (unsigned long long)eb->start,
  80. eb->len, atomic_read(&eb->refs));
  81. list_del(&eb->leak_list);
  82. kmem_cache_free(extent_buffer_cache, eb);
  83. }
  84. if (extent_state_cache)
  85. kmem_cache_destroy(extent_state_cache);
  86. if (extent_buffer_cache)
  87. kmem_cache_destroy(extent_buffer_cache);
  88. }
  89. void extent_io_tree_init(struct extent_io_tree *tree,
  90. struct address_space *mapping)
  91. {
  92. tree->state = RB_ROOT;
  93. INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
  94. tree->ops = NULL;
  95. tree->dirty_bytes = 0;
  96. spin_lock_init(&tree->lock);
  97. spin_lock_init(&tree->buffer_lock);
  98. tree->mapping = mapping;
  99. }
  100. static struct extent_state *alloc_extent_state(gfp_t mask)
  101. {
  102. struct extent_state *state;
  103. #if LEAK_DEBUG
  104. unsigned long flags;
  105. #endif
  106. state = kmem_cache_alloc(extent_state_cache, mask);
  107. if (!state)
  108. return state;
  109. state->state = 0;
  110. state->private = 0;
  111. state->tree = NULL;
  112. #if LEAK_DEBUG
  113. spin_lock_irqsave(&leak_lock, flags);
  114. list_add(&state->leak_list, &states);
  115. spin_unlock_irqrestore(&leak_lock, flags);
  116. #endif
  117. atomic_set(&state->refs, 1);
  118. init_waitqueue_head(&state->wq);
  119. return state;
  120. }
  121. void free_extent_state(struct extent_state *state)
  122. {
  123. if (!state)
  124. return;
  125. if (atomic_dec_and_test(&state->refs)) {
  126. #if LEAK_DEBUG
  127. unsigned long flags;
  128. #endif
  129. WARN_ON(state->tree);
  130. #if LEAK_DEBUG
  131. spin_lock_irqsave(&leak_lock, flags);
  132. list_del(&state->leak_list);
  133. spin_unlock_irqrestore(&leak_lock, flags);
  134. #endif
  135. kmem_cache_free(extent_state_cache, state);
  136. }
  137. }
  138. static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
  139. struct rb_node *node)
  140. {
  141. struct rb_node **p = &root->rb_node;
  142. struct rb_node *parent = NULL;
  143. struct tree_entry *entry;
  144. while (*p) {
  145. parent = *p;
  146. entry = rb_entry(parent, struct tree_entry, rb_node);
  147. if (offset < entry->start)
  148. p = &(*p)->rb_left;
  149. else if (offset > entry->end)
  150. p = &(*p)->rb_right;
  151. else
  152. return parent;
  153. }
  154. entry = rb_entry(node, struct tree_entry, rb_node);
  155. rb_link_node(node, parent, p);
  156. rb_insert_color(node, root);
  157. return NULL;
  158. }
  159. static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
  160. struct rb_node **prev_ret,
  161. struct rb_node **next_ret)
  162. {
  163. struct rb_root *root = &tree->state;
  164. struct rb_node *n = root->rb_node;
  165. struct rb_node *prev = NULL;
  166. struct rb_node *orig_prev = NULL;
  167. struct tree_entry *entry;
  168. struct tree_entry *prev_entry = NULL;
  169. while (n) {
  170. entry = rb_entry(n, struct tree_entry, rb_node);
  171. prev = n;
  172. prev_entry = entry;
  173. if (offset < entry->start)
  174. n = n->rb_left;
  175. else if (offset > entry->end)
  176. n = n->rb_right;
  177. else
  178. return n;
  179. }
  180. if (prev_ret) {
  181. orig_prev = prev;
  182. while (prev && offset > prev_entry->end) {
  183. prev = rb_next(prev);
  184. prev_entry = rb_entry(prev, struct tree_entry, rb_node);
  185. }
  186. *prev_ret = prev;
  187. prev = orig_prev;
  188. }
  189. if (next_ret) {
  190. prev_entry = rb_entry(prev, struct tree_entry, rb_node);
  191. while (prev && offset < prev_entry->start) {
  192. prev = rb_prev(prev);
  193. prev_entry = rb_entry(prev, struct tree_entry, rb_node);
  194. }
  195. *next_ret = prev;
  196. }
  197. return NULL;
  198. }
  199. static inline struct rb_node *tree_search(struct extent_io_tree *tree,
  200. u64 offset)
  201. {
  202. struct rb_node *prev = NULL;
  203. struct rb_node *ret;
  204. ret = __etree_search(tree, offset, &prev, NULL);
  205. if (!ret)
  206. return prev;
  207. return ret;
  208. }
  209. static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
  210. struct extent_state *other)
  211. {
  212. if (tree->ops && tree->ops->merge_extent_hook)
  213. tree->ops->merge_extent_hook(tree->mapping->host, new,
  214. other);
  215. }
  216. /*
  217. * utility function to look for merge candidates inside a given range.
  218. * Any extents with matching state are merged together into a single
  219. * extent in the tree. Extents with EXTENT_IO in their state field
  220. * are not merged because the end_io handlers need to be able to do
  221. * operations on them without sleeping (or doing allocations/splits).
  222. *
  223. * This should be called with the tree lock held.
  224. */
  225. static void merge_state(struct extent_io_tree *tree,
  226. struct extent_state *state)
  227. {
  228. struct extent_state *other;
  229. struct rb_node *other_node;
  230. if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
  231. return;
  232. other_node = rb_prev(&state->rb_node);
  233. if (other_node) {
  234. other = rb_entry(other_node, struct extent_state, rb_node);
  235. if (other->end == state->start - 1 &&
  236. other->state == state->state) {
  237. merge_cb(tree, state, other);
  238. state->start = other->start;
  239. other->tree = NULL;
  240. rb_erase(&other->rb_node, &tree->state);
  241. free_extent_state(other);
  242. }
  243. }
  244. other_node = rb_next(&state->rb_node);
  245. if (other_node) {
  246. other = rb_entry(other_node, struct extent_state, rb_node);
  247. if (other->start == state->end + 1 &&
  248. other->state == state->state) {
  249. merge_cb(tree, state, other);
  250. state->end = other->end;
  251. other->tree = NULL;
  252. rb_erase(&other->rb_node, &tree->state);
  253. free_extent_state(other);
  254. }
  255. }
  256. }
  257. static void set_state_cb(struct extent_io_tree *tree,
  258. struct extent_state *state, int *bits)
  259. {
  260. if (tree->ops && tree->ops->set_bit_hook)
  261. tree->ops->set_bit_hook(tree->mapping->host, state, bits);
  262. }
  263. static void clear_state_cb(struct extent_io_tree *tree,
  264. struct extent_state *state, int *bits)
  265. {
  266. if (tree->ops && tree->ops->clear_bit_hook)
  267. tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
  268. }
  269. static void set_state_bits(struct extent_io_tree *tree,
  270. struct extent_state *state, int *bits);
  271. /*
  272. * insert an extent_state struct into the tree. 'bits' are set on the
  273. * struct before it is inserted.
  274. *
  275. * This may return -EEXIST if the extent is already there, in which case the
  276. * state struct is freed.
  277. *
  278. * The tree lock is not taken internally. This is a utility function and
  279. * probably isn't what you want to call (see set/clear_extent_bit).
  280. */
  281. static int insert_state(struct extent_io_tree *tree,
  282. struct extent_state *state, u64 start, u64 end,
  283. int *bits)
  284. {
  285. struct rb_node *node;
  286. if (end < start) {
  287. printk(KERN_ERR "btrfs end < start %llu %llu\n",
  288. (unsigned long long)end,
  289. (unsigned long long)start);
  290. WARN_ON(1);
  291. }
  292. state->start = start;
  293. state->end = end;
  294. set_state_bits(tree, state, bits);
  295. node = tree_insert(&tree->state, end, &state->rb_node);
  296. if (node) {
  297. struct extent_state *found;
  298. found = rb_entry(node, struct extent_state, rb_node);
  299. printk(KERN_ERR "btrfs found node %llu %llu on insert of "
  300. "%llu %llu\n", (unsigned long long)found->start,
  301. (unsigned long long)found->end,
  302. (unsigned long long)start, (unsigned long long)end);
  303. return -EEXIST;
  304. }
  305. state->tree = tree;
  306. merge_state(tree, state);
  307. return 0;
  308. }
  309. static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
  310. u64 split)
  311. {
  312. if (tree->ops && tree->ops->split_extent_hook)
  313. tree->ops->split_extent_hook(tree->mapping->host, orig, split);
  314. }
  315. /*
  316. * split a given extent state struct in two, inserting the preallocated
  317. * struct 'prealloc' as the newly created second half. 'split' indicates an
  318. * offset inside 'orig' where it should be split.
  319. *
  320. * Before calling,
  321. * the tree has 'orig' at [orig->start, orig->end]. After calling, there
  322. * are two extent state structs in the tree:
  323. * prealloc: [orig->start, split - 1]
  324. * orig: [ split, orig->end ]
  325. *
  326. * The tree locks are not taken by this function. They need to be held
  327. * by the caller.
  328. */
  329. static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
  330. struct extent_state *prealloc, u64 split)
  331. {
  332. struct rb_node *node;
  333. split_cb(tree, orig, split);
  334. prealloc->start = orig->start;
  335. prealloc->end = split - 1;
  336. prealloc->state = orig->state;
  337. orig->start = split;
  338. node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
  339. if (node) {
  340. free_extent_state(prealloc);
  341. return -EEXIST;
  342. }
  343. prealloc->tree = tree;
  344. return 0;
  345. }
  346. /*
  347. * utility function to clear some bits in an extent state struct.
  348. * it will optionally wake up any one waiting on this state (wake == 1), or
  349. * forcibly remove the state from the tree (delete == 1).
  350. *
  351. * If no bits are set on the state struct after clearing things, the
  352. * struct is freed and removed from the tree
  353. */
  354. static int clear_state_bit(struct extent_io_tree *tree,
  355. struct extent_state *state,
  356. int *bits, int wake)
  357. {
  358. int bits_to_clear = *bits & ~EXTENT_CTLBITS;
  359. int ret = state->state & bits_to_clear;
  360. if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
  361. u64 range = state->end - state->start + 1;
  362. WARN_ON(range > tree->dirty_bytes);
  363. tree->dirty_bytes -= range;
  364. }
  365. clear_state_cb(tree, state, bits);
  366. state->state &= ~bits_to_clear;
  367. if (wake)
  368. wake_up(&state->wq);
  369. if (state->state == 0) {
  370. if (state->tree) {
  371. rb_erase(&state->rb_node, &tree->state);
  372. state->tree = NULL;
  373. free_extent_state(state);
  374. } else {
  375. WARN_ON(1);
  376. }
  377. } else {
  378. merge_state(tree, state);
  379. }
  380. return ret;
  381. }
  382. static struct extent_state *
  383. alloc_extent_state_atomic(struct extent_state *prealloc)
  384. {
  385. if (!prealloc)
  386. prealloc = alloc_extent_state(GFP_ATOMIC);
  387. return prealloc;
  388. }
  389. /*
  390. * clear some bits on a range in the tree. This may require splitting
  391. * or inserting elements in the tree, so the gfp mask is used to
  392. * indicate which allocations or sleeping are allowed.
  393. *
  394. * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
  395. * the given range from the tree regardless of state (ie for truncate).
  396. *
  397. * the range [start, end] is inclusive.
  398. *
  399. * This takes the tree lock, and returns < 0 on error, > 0 if any of the
  400. * bits were already set, or zero if none of the bits were already set.
  401. */
  402. int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  403. int bits, int wake, int delete,
  404. struct extent_state **cached_state,
  405. gfp_t mask)
  406. {
  407. struct extent_state *state;
  408. struct extent_state *cached;
  409. struct extent_state *prealloc = NULL;
  410. struct rb_node *next_node;
  411. struct rb_node *node;
  412. u64 last_end;
  413. int err;
  414. int set = 0;
  415. int clear = 0;
  416. if (delete)
  417. bits |= ~EXTENT_CTLBITS;
  418. bits |= EXTENT_FIRST_DELALLOC;
  419. if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
  420. clear = 1;
  421. again:
  422. if (!prealloc && (mask & __GFP_WAIT)) {
  423. prealloc = alloc_extent_state(mask);
  424. if (!prealloc)
  425. return -ENOMEM;
  426. }
  427. spin_lock(&tree->lock);
  428. if (cached_state) {
  429. cached = *cached_state;
  430. if (clear) {
  431. *cached_state = NULL;
  432. cached_state = NULL;
  433. }
  434. if (cached && cached->tree && cached->start <= start &&
  435. cached->end > start) {
  436. if (clear)
  437. atomic_dec(&cached->refs);
  438. state = cached;
  439. goto hit_next;
  440. }
  441. if (clear)
  442. free_extent_state(cached);
  443. }
  444. /*
  445. * this search will find the extents that end after
  446. * our range starts
  447. */
  448. node = tree_search(tree, start);
  449. if (!node)
  450. goto out;
  451. state = rb_entry(node, struct extent_state, rb_node);
  452. hit_next:
  453. if (state->start > end)
  454. goto out;
  455. WARN_ON(state->end < start);
  456. last_end = state->end;
  457. /*
  458. * | ---- desired range ---- |
  459. * | state | or
  460. * | ------------- state -------------- |
  461. *
  462. * We need to split the extent we found, and may flip
  463. * bits on second half.
  464. *
  465. * If the extent we found extends past our range, we
  466. * just split and search again. It'll get split again
  467. * the next time though.
  468. *
  469. * If the extent we found is inside our range, we clear
  470. * the desired bit on it.
  471. */
  472. if (state->start < start) {
  473. prealloc = alloc_extent_state_atomic(prealloc);
  474. BUG_ON(!prealloc);
  475. err = split_state(tree, state, prealloc, start);
  476. BUG_ON(err == -EEXIST);
  477. prealloc = NULL;
  478. if (err)
  479. goto out;
  480. if (state->end <= end) {
  481. set |= clear_state_bit(tree, state, &bits, wake);
  482. if (last_end == (u64)-1)
  483. goto out;
  484. start = last_end + 1;
  485. }
  486. goto search_again;
  487. }
  488. /*
  489. * | ---- desired range ---- |
  490. * | state |
  491. * We need to split the extent, and clear the bit
  492. * on the first half
  493. */
  494. if (state->start <= end && state->end > end) {
  495. prealloc = alloc_extent_state_atomic(prealloc);
  496. BUG_ON(!prealloc);
  497. err = split_state(tree, state, prealloc, end + 1);
  498. BUG_ON(err == -EEXIST);
  499. if (wake)
  500. wake_up(&state->wq);
  501. set |= clear_state_bit(tree, prealloc, &bits, wake);
  502. prealloc = NULL;
  503. goto out;
  504. }
  505. if (state->end < end && prealloc && !need_resched())
  506. next_node = rb_next(&state->rb_node);
  507. else
  508. next_node = NULL;
  509. set |= clear_state_bit(tree, state, &bits, wake);
  510. if (last_end == (u64)-1)
  511. goto out;
  512. start = last_end + 1;
  513. if (start <= end && next_node) {
  514. state = rb_entry(next_node, struct extent_state,
  515. rb_node);
  516. if (state->start == start)
  517. goto hit_next;
  518. }
  519. goto search_again;
  520. out:
  521. spin_unlock(&tree->lock);
  522. if (prealloc)
  523. free_extent_state(prealloc);
  524. return set;
  525. search_again:
  526. if (start > end)
  527. goto out;
  528. spin_unlock(&tree->lock);
  529. if (mask & __GFP_WAIT)
  530. cond_resched();
  531. goto again;
  532. }
  533. static int wait_on_state(struct extent_io_tree *tree,
  534. struct extent_state *state)
  535. __releases(tree->lock)
  536. __acquires(tree->lock)
  537. {
  538. DEFINE_WAIT(wait);
  539. prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
  540. spin_unlock(&tree->lock);
  541. schedule();
  542. spin_lock(&tree->lock);
  543. finish_wait(&state->wq, &wait);
  544. return 0;
  545. }
  546. /*
  547. * waits for one or more bits to clear on a range in the state tree.
  548. * The range [start, end] is inclusive.
  549. * The tree lock is taken by this function
  550. */
  551. int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
  552. {
  553. struct extent_state *state;
  554. struct rb_node *node;
  555. spin_lock(&tree->lock);
  556. again:
  557. while (1) {
  558. /*
  559. * this search will find all the extents that end after
  560. * our range starts
  561. */
  562. node = tree_search(tree, start);
  563. if (!node)
  564. break;
  565. state = rb_entry(node, struct extent_state, rb_node);
  566. if (state->start > end)
  567. goto out;
  568. if (state->state & bits) {
  569. start = state->start;
  570. atomic_inc(&state->refs);
  571. wait_on_state(tree, state);
  572. free_extent_state(state);
  573. goto again;
  574. }
  575. start = state->end + 1;
  576. if (start > end)
  577. break;
  578. cond_resched_lock(&tree->lock);
  579. }
  580. out:
  581. spin_unlock(&tree->lock);
  582. return 0;
  583. }
  584. static void set_state_bits(struct extent_io_tree *tree,
  585. struct extent_state *state,
  586. int *bits)
  587. {
  588. int bits_to_set = *bits & ~EXTENT_CTLBITS;
  589. set_state_cb(tree, state, bits);
  590. if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
  591. u64 range = state->end - state->start + 1;
  592. tree->dirty_bytes += range;
  593. }
  594. state->state |= bits_to_set;
  595. }
  596. static void cache_state(struct extent_state *state,
  597. struct extent_state **cached_ptr)
  598. {
  599. if (cached_ptr && !(*cached_ptr)) {
  600. if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
  601. *cached_ptr = state;
  602. atomic_inc(&state->refs);
  603. }
  604. }
  605. }
  606. static void uncache_state(struct extent_state **cached_ptr)
  607. {
  608. if (cached_ptr && (*cached_ptr)) {
  609. struct extent_state *state = *cached_ptr;
  610. *cached_ptr = NULL;
  611. free_extent_state(state);
  612. }
  613. }
  614. /*
  615. * set some bits on a range in the tree. This may require allocations or
  616. * sleeping, so the gfp mask is used to indicate what is allowed.
  617. *
  618. * If any of the exclusive bits are set, this will fail with -EEXIST if some
  619. * part of the range already has the desired bits set. The start of the
  620. * existing range is returned in failed_start in this case.
  621. *
  622. * [start, end] is inclusive This takes the tree lock.
  623. */
  624. int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  625. int bits, int exclusive_bits, u64 *failed_start,
  626. struct extent_state **cached_state, gfp_t mask)
  627. {
  628. struct extent_state *state;
  629. struct extent_state *prealloc = NULL;
  630. struct rb_node *node;
  631. int err = 0;
  632. u64 last_start;
  633. u64 last_end;
  634. bits |= EXTENT_FIRST_DELALLOC;
  635. again:
  636. if (!prealloc && (mask & __GFP_WAIT)) {
  637. prealloc = alloc_extent_state(mask);
  638. BUG_ON(!prealloc);
  639. }
  640. spin_lock(&tree->lock);
  641. if (cached_state && *cached_state) {
  642. state = *cached_state;
  643. if (state->start <= start && state->end > start &&
  644. state->tree) {
  645. node = &state->rb_node;
  646. goto hit_next;
  647. }
  648. }
  649. /*
  650. * this search will find all the extents that end after
  651. * our range starts.
  652. */
  653. node = tree_search(tree, start);
  654. if (!node) {
  655. prealloc = alloc_extent_state_atomic(prealloc);
  656. BUG_ON(!prealloc);
  657. err = insert_state(tree, prealloc, start, end, &bits);
  658. prealloc = NULL;
  659. BUG_ON(err == -EEXIST);
  660. goto out;
  661. }
  662. state = rb_entry(node, struct extent_state, rb_node);
  663. hit_next:
  664. last_start = state->start;
  665. last_end = state->end;
  666. /*
  667. * | ---- desired range ---- |
  668. * | state |
  669. *
  670. * Just lock what we found and keep going
  671. */
  672. if (state->start == start && state->end <= end) {
  673. struct rb_node *next_node;
  674. if (state->state & exclusive_bits) {
  675. *failed_start = state->start;
  676. err = -EEXIST;
  677. goto out;
  678. }
  679. set_state_bits(tree, state, &bits);
  680. cache_state(state, cached_state);
  681. merge_state(tree, state);
  682. if (last_end == (u64)-1)
  683. goto out;
  684. start = last_end + 1;
  685. next_node = rb_next(&state->rb_node);
  686. if (next_node && start < end && prealloc && !need_resched()) {
  687. state = rb_entry(next_node, struct extent_state,
  688. rb_node);
  689. if (state->start == start)
  690. goto hit_next;
  691. }
  692. goto search_again;
  693. }
  694. /*
  695. * | ---- desired range ---- |
  696. * | state |
  697. * or
  698. * | ------------- state -------------- |
  699. *
  700. * We need to split the extent we found, and may flip bits on
  701. * second half.
  702. *
  703. * If the extent we found extends past our
  704. * range, we just split and search again. It'll get split
  705. * again the next time though.
  706. *
  707. * If the extent we found is inside our range, we set the
  708. * desired bit on it.
  709. */
  710. if (state->start < start) {
  711. if (state->state & exclusive_bits) {
  712. *failed_start = start;
  713. err = -EEXIST;
  714. goto out;
  715. }
  716. prealloc = alloc_extent_state_atomic(prealloc);
  717. BUG_ON(!prealloc);
  718. err = split_state(tree, state, prealloc, start);
  719. BUG_ON(err == -EEXIST);
  720. prealloc = NULL;
  721. if (err)
  722. goto out;
  723. if (state->end <= end) {
  724. set_state_bits(tree, state, &bits);
  725. cache_state(state, cached_state);
  726. merge_state(tree, state);
  727. if (last_end == (u64)-1)
  728. goto out;
  729. start = last_end + 1;
  730. }
  731. goto search_again;
  732. }
  733. /*
  734. * | ---- desired range ---- |
  735. * | state | or | state |
  736. *
  737. * There's a hole, we need to insert something in it and
  738. * ignore the extent we found.
  739. */
  740. if (state->start > start) {
  741. u64 this_end;
  742. if (end < last_start)
  743. this_end = end;
  744. else
  745. this_end = last_start - 1;
  746. prealloc = alloc_extent_state_atomic(prealloc);
  747. BUG_ON(!prealloc);
  748. /*
  749. * Avoid to free 'prealloc' if it can be merged with
  750. * the later extent.
  751. */
  752. err = insert_state(tree, prealloc, start, this_end,
  753. &bits);
  754. BUG_ON(err == -EEXIST);
  755. if (err) {
  756. free_extent_state(prealloc);
  757. prealloc = NULL;
  758. goto out;
  759. }
  760. cache_state(prealloc, cached_state);
  761. prealloc = NULL;
  762. start = this_end + 1;
  763. goto search_again;
  764. }
  765. /*
  766. * | ---- desired range ---- |
  767. * | state |
  768. * We need to split the extent, and set the bit
  769. * on the first half
  770. */
  771. if (state->start <= end && state->end > end) {
  772. if (state->state & exclusive_bits) {
  773. *failed_start = start;
  774. err = -EEXIST;
  775. goto out;
  776. }
  777. prealloc = alloc_extent_state_atomic(prealloc);
  778. BUG_ON(!prealloc);
  779. err = split_state(tree, state, prealloc, end + 1);
  780. BUG_ON(err == -EEXIST);
  781. set_state_bits(tree, prealloc, &bits);
  782. cache_state(prealloc, cached_state);
  783. merge_state(tree, prealloc);
  784. prealloc = NULL;
  785. goto out;
  786. }
  787. goto search_again;
  788. out:
  789. spin_unlock(&tree->lock);
  790. if (prealloc)
  791. free_extent_state(prealloc);
  792. return err;
  793. search_again:
  794. if (start > end)
  795. goto out;
  796. spin_unlock(&tree->lock);
  797. if (mask & __GFP_WAIT)
  798. cond_resched();
  799. goto again;
  800. }
  801. /* wrappers around set/clear extent bit */
  802. int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
  803. gfp_t mask)
  804. {
  805. return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
  806. NULL, mask);
  807. }
  808. int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  809. int bits, gfp_t mask)
  810. {
  811. return set_extent_bit(tree, start, end, bits, 0, NULL,
  812. NULL, mask);
  813. }
  814. int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  815. int bits, gfp_t mask)
  816. {
  817. return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
  818. }
  819. int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
  820. struct extent_state **cached_state, gfp_t mask)
  821. {
  822. return set_extent_bit(tree, start, end,
  823. EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
  824. 0, NULL, cached_state, mask);
  825. }
  826. int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
  827. gfp_t mask)
  828. {
  829. return clear_extent_bit(tree, start, end,
  830. EXTENT_DIRTY | EXTENT_DELALLOC |
  831. EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
  832. }
  833. int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
  834. gfp_t mask)
  835. {
  836. return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
  837. NULL, mask);
  838. }
  839. int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
  840. struct extent_state **cached_state, gfp_t mask)
  841. {
  842. return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
  843. NULL, cached_state, mask);
  844. }
  845. static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
  846. u64 end, struct extent_state **cached_state,
  847. gfp_t mask)
  848. {
  849. return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
  850. cached_state, mask);
  851. }
  852. /*
  853. * either insert or lock state struct between start and end use mask to tell
  854. * us if waiting is desired.
  855. */
  856. int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  857. int bits, struct extent_state **cached_state, gfp_t mask)
  858. {
  859. int err;
  860. u64 failed_start;
  861. while (1) {
  862. err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
  863. EXTENT_LOCKED, &failed_start,
  864. cached_state, mask);
  865. if (err == -EEXIST && (mask & __GFP_WAIT)) {
  866. wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
  867. start = failed_start;
  868. } else {
  869. break;
  870. }
  871. WARN_ON(start > end);
  872. }
  873. return err;
  874. }
  875. int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
  876. {
  877. return lock_extent_bits(tree, start, end, 0, NULL, mask);
  878. }
  879. int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
  880. gfp_t mask)
  881. {
  882. int err;
  883. u64 failed_start;
  884. err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
  885. &failed_start, NULL, mask);
  886. if (err == -EEXIST) {
  887. if (failed_start > start)
  888. clear_extent_bit(tree, start, failed_start - 1,
  889. EXTENT_LOCKED, 1, 0, NULL, mask);
  890. return 0;
  891. }
  892. return 1;
  893. }
  894. int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
  895. struct extent_state **cached, gfp_t mask)
  896. {
  897. return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
  898. mask);
  899. }
  900. int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
  901. {
  902. return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
  903. mask);
  904. }
  905. /*
  906. * helper function to set both pages and extents in the tree writeback
  907. */
  908. static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
  909. {
  910. unsigned long index = start >> PAGE_CACHE_SHIFT;
  911. unsigned long end_index = end >> PAGE_CACHE_SHIFT;
  912. struct page *page;
  913. while (index <= end_index) {
  914. page = find_get_page(tree->mapping, index);
  915. BUG_ON(!page);
  916. set_page_writeback(page);
  917. page_cache_release(page);
  918. index++;
  919. }
  920. return 0;
  921. }
  922. /* find the first state struct with 'bits' set after 'start', and
  923. * return it. tree->lock must be held. NULL will returned if
  924. * nothing was found after 'start'
  925. */
  926. struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
  927. u64 start, int bits)
  928. {
  929. struct rb_node *node;
  930. struct extent_state *state;
  931. /*
  932. * this search will find all the extents that end after
  933. * our range starts.
  934. */
  935. node = tree_search(tree, start);
  936. if (!node)
  937. goto out;
  938. while (1) {
  939. state = rb_entry(node, struct extent_state, rb_node);
  940. if (state->end >= start && (state->state & bits))
  941. return state;
  942. node = rb_next(node);
  943. if (!node)
  944. break;
  945. }
  946. out:
  947. return NULL;
  948. }
  949. /*
  950. * find the first offset in the io tree with 'bits' set. zero is
  951. * returned if we find something, and *start_ret and *end_ret are
  952. * set to reflect the state struct that was found.
  953. *
  954. * If nothing was found, 1 is returned, < 0 on error
  955. */
  956. int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
  957. u64 *start_ret, u64 *end_ret, int bits)
  958. {
  959. struct extent_state *state;
  960. int ret = 1;
  961. spin_lock(&tree->lock);
  962. state = find_first_extent_bit_state(tree, start, bits);
  963. if (state) {
  964. *start_ret = state->start;
  965. *end_ret = state->end;
  966. ret = 0;
  967. }
  968. spin_unlock(&tree->lock);
  969. return ret;
  970. }
  971. /*
  972. * find a contiguous range of bytes in the file marked as delalloc, not
  973. * more than 'max_bytes'. start and end are used to return the range,
  974. *
  975. * 1 is returned if we find something, 0 if nothing was in the tree
  976. */
  977. static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
  978. u64 *start, u64 *end, u64 max_bytes,
  979. struct extent_state **cached_state)
  980. {
  981. struct rb_node *node;
  982. struct extent_state *state;
  983. u64 cur_start = *start;
  984. u64 found = 0;
  985. u64 total_bytes = 0;
  986. spin_lock(&tree->lock);
  987. /*
  988. * this search will find all the extents that end after
  989. * our range starts.
  990. */
  991. node = tree_search(tree, cur_start);
  992. if (!node) {
  993. if (!found)
  994. *end = (u64)-1;
  995. goto out;
  996. }
  997. while (1) {
  998. state = rb_entry(node, struct extent_state, rb_node);
  999. if (found && (state->start != cur_start ||
  1000. (state->state & EXTENT_BOUNDARY))) {
  1001. goto out;
  1002. }
  1003. if (!(state->state & EXTENT_DELALLOC)) {
  1004. if (!found)
  1005. *end = state->end;
  1006. goto out;
  1007. }
  1008. if (!found) {
  1009. *start = state->start;
  1010. *cached_state = state;
  1011. atomic_inc(&state->refs);
  1012. }
  1013. found++;
  1014. *end = state->end;
  1015. cur_start = state->end + 1;
  1016. node = rb_next(node);
  1017. if (!node)
  1018. break;
  1019. total_bytes += state->end - state->start + 1;
  1020. if (total_bytes >= max_bytes)
  1021. break;
  1022. }
  1023. out:
  1024. spin_unlock(&tree->lock);
  1025. return found;
  1026. }
  1027. static noinline int __unlock_for_delalloc(struct inode *inode,
  1028. struct page *locked_page,
  1029. u64 start, u64 end)
  1030. {
  1031. int ret;
  1032. struct page *pages[16];
  1033. unsigned long index = start >> PAGE_CACHE_SHIFT;
  1034. unsigned long end_index = end >> PAGE_CACHE_SHIFT;
  1035. unsigned long nr_pages = end_index - index + 1;
  1036. int i;
  1037. if (index == locked_page->index && end_index == index)
  1038. return 0;
  1039. while (nr_pages > 0) {
  1040. ret = find_get_pages_contig(inode->i_mapping, index,
  1041. min_t(unsigned long, nr_pages,
  1042. ARRAY_SIZE(pages)), pages);
  1043. for (i = 0; i < ret; i++) {
  1044. if (pages[i] != locked_page)
  1045. unlock_page(pages[i]);
  1046. page_cache_release(pages[i]);
  1047. }
  1048. nr_pages -= ret;
  1049. index += ret;
  1050. cond_resched();
  1051. }
  1052. return 0;
  1053. }
  1054. static noinline int lock_delalloc_pages(struct inode *inode,
  1055. struct page *locked_page,
  1056. u64 delalloc_start,
  1057. u64 delalloc_end)
  1058. {
  1059. unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
  1060. unsigned long start_index = index;
  1061. unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
  1062. unsigned long pages_locked = 0;
  1063. struct page *pages[16];
  1064. unsigned long nrpages;
  1065. int ret;
  1066. int i;
  1067. /* the caller is responsible for locking the start index */
  1068. if (index == locked_page->index && index == end_index)
  1069. return 0;
  1070. /* skip the page at the start index */
  1071. nrpages = end_index - index + 1;
  1072. while (nrpages > 0) {
  1073. ret = find_get_pages_contig(inode->i_mapping, index,
  1074. min_t(unsigned long,
  1075. nrpages, ARRAY_SIZE(pages)), pages);
  1076. if (ret == 0) {
  1077. ret = -EAGAIN;
  1078. goto done;
  1079. }
  1080. /* now we have an array of pages, lock them all */
  1081. for (i = 0; i < ret; i++) {
  1082. /*
  1083. * the caller is taking responsibility for
  1084. * locked_page
  1085. */
  1086. if (pages[i] != locked_page) {
  1087. lock_page(pages[i]);
  1088. if (!PageDirty(pages[i]) ||
  1089. pages[i]->mapping != inode->i_mapping) {
  1090. ret = -EAGAIN;
  1091. unlock_page(pages[i]);
  1092. page_cache_release(pages[i]);
  1093. goto done;
  1094. }
  1095. }
  1096. page_cache_release(pages[i]);
  1097. pages_locked++;
  1098. }
  1099. nrpages -= ret;
  1100. index += ret;
  1101. cond_resched();
  1102. }
  1103. ret = 0;
  1104. done:
  1105. if (ret && pages_locked) {
  1106. __unlock_for_delalloc(inode, locked_page,
  1107. delalloc_start,
  1108. ((u64)(start_index + pages_locked - 1)) <<
  1109. PAGE_CACHE_SHIFT);
  1110. }
  1111. return ret;
  1112. }
  1113. /*
  1114. * find a contiguous range of bytes in the file marked as delalloc, not
  1115. * more than 'max_bytes'. start and end are used to return the range,
  1116. *
  1117. * 1 is returned if we find something, 0 if nothing was in the tree
  1118. */
  1119. static noinline u64 find_lock_delalloc_range(struct inode *inode,
  1120. struct extent_io_tree *tree,
  1121. struct page *locked_page,
  1122. u64 *start, u64 *end,
  1123. u64 max_bytes)
  1124. {
  1125. u64 delalloc_start;
  1126. u64 delalloc_end;
  1127. u64 found;
  1128. struct extent_state *cached_state = NULL;
  1129. int ret;
  1130. int loops = 0;
  1131. again:
  1132. /* step one, find a bunch of delalloc bytes starting at start */
  1133. delalloc_start = *start;
  1134. delalloc_end = 0;
  1135. found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
  1136. max_bytes, &cached_state);
  1137. if (!found || delalloc_end <= *start) {
  1138. *start = delalloc_start;
  1139. *end = delalloc_end;
  1140. free_extent_state(cached_state);
  1141. return found;
  1142. }
  1143. /*
  1144. * start comes from the offset of locked_page. We have to lock
  1145. * pages in order, so we can't process delalloc bytes before
  1146. * locked_page
  1147. */
  1148. if (delalloc_start < *start)
  1149. delalloc_start = *start;
  1150. /*
  1151. * make sure to limit the number of pages we try to lock down
  1152. * if we're looping.
  1153. */
  1154. if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
  1155. delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
  1156. /* step two, lock all the pages after the page that has start */
  1157. ret = lock_delalloc_pages(inode, locked_page,
  1158. delalloc_start, delalloc_end);
  1159. if (ret == -EAGAIN) {
  1160. /* some of the pages are gone, lets avoid looping by
  1161. * shortening the size of the delalloc range we're searching
  1162. */
  1163. free_extent_state(cached_state);
  1164. if (!loops) {
  1165. unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
  1166. max_bytes = PAGE_CACHE_SIZE - offset;
  1167. loops = 1;
  1168. goto again;
  1169. } else {
  1170. found = 0;
  1171. goto out_failed;
  1172. }
  1173. }
  1174. BUG_ON(ret);
  1175. /* step three, lock the state bits for the whole range */
  1176. lock_extent_bits(tree, delalloc_start, delalloc_end,
  1177. 0, &cached_state, GFP_NOFS);
  1178. /* then test to make sure it is all still delalloc */
  1179. ret = test_range_bit(tree, delalloc_start, delalloc_end,
  1180. EXTENT_DELALLOC, 1, cached_state);
  1181. if (!ret) {
  1182. unlock_extent_cached(tree, delalloc_start, delalloc_end,
  1183. &cached_state, GFP_NOFS);
  1184. __unlock_for_delalloc(inode, locked_page,
  1185. delalloc_start, delalloc_end);
  1186. cond_resched();
  1187. goto again;
  1188. }
  1189. free_extent_state(cached_state);
  1190. *start = delalloc_start;
  1191. *end = delalloc_end;
  1192. out_failed:
  1193. return found;
  1194. }
  1195. int extent_clear_unlock_delalloc(struct inode *inode,
  1196. struct extent_io_tree *tree,
  1197. u64 start, u64 end, struct page *locked_page,
  1198. unsigned long op)
  1199. {
  1200. int ret;
  1201. struct page *pages[16];
  1202. unsigned long index = start >> PAGE_CACHE_SHIFT;
  1203. unsigned long end_index = end >> PAGE_CACHE_SHIFT;
  1204. unsigned long nr_pages = end_index - index + 1;
  1205. int i;
  1206. int clear_bits = 0;
  1207. if (op & EXTENT_CLEAR_UNLOCK)
  1208. clear_bits |= EXTENT_LOCKED;
  1209. if (op & EXTENT_CLEAR_DIRTY)
  1210. clear_bits |= EXTENT_DIRTY;
  1211. if (op & EXTENT_CLEAR_DELALLOC)
  1212. clear_bits |= EXTENT_DELALLOC;
  1213. clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
  1214. if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
  1215. EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
  1216. EXTENT_SET_PRIVATE2)))
  1217. return 0;
  1218. while (nr_pages > 0) {
  1219. ret = find_get_pages_contig(inode->i_mapping, index,
  1220. min_t(unsigned long,
  1221. nr_pages, ARRAY_SIZE(pages)), pages);
  1222. for (i = 0; i < ret; i++) {
  1223. if (op & EXTENT_SET_PRIVATE2)
  1224. SetPagePrivate2(pages[i]);
  1225. if (pages[i] == locked_page) {
  1226. page_cache_release(pages[i]);
  1227. continue;
  1228. }
  1229. if (op & EXTENT_CLEAR_DIRTY)
  1230. clear_page_dirty_for_io(pages[i]);
  1231. if (op & EXTENT_SET_WRITEBACK)
  1232. set_page_writeback(pages[i]);
  1233. if (op & EXTENT_END_WRITEBACK)
  1234. end_page_writeback(pages[i]);
  1235. if (op & EXTENT_CLEAR_UNLOCK_PAGE)
  1236. unlock_page(pages[i]);
  1237. page_cache_release(pages[i]);
  1238. }
  1239. nr_pages -= ret;
  1240. index += ret;
  1241. cond_resched();
  1242. }
  1243. return 0;
  1244. }
  1245. /*
  1246. * count the number of bytes in the tree that have a given bit(s)
  1247. * set. This can be fairly slow, except for EXTENT_DIRTY which is
  1248. * cached. The total number found is returned.
  1249. */
  1250. u64 count_range_bits(struct extent_io_tree *tree,
  1251. u64 *start, u64 search_end, u64 max_bytes,
  1252. unsigned long bits, int contig)
  1253. {
  1254. struct rb_node *node;
  1255. struct extent_state *state;
  1256. u64 cur_start = *start;
  1257. u64 total_bytes = 0;
  1258. u64 last = 0;
  1259. int found = 0;
  1260. if (search_end <= cur_start) {
  1261. WARN_ON(1);
  1262. return 0;
  1263. }
  1264. spin_lock(&tree->lock);
  1265. if (cur_start == 0 && bits == EXTENT_DIRTY) {
  1266. total_bytes = tree->dirty_bytes;
  1267. goto out;
  1268. }
  1269. /*
  1270. * this search will find all the extents that end after
  1271. * our range starts.
  1272. */
  1273. node = tree_search(tree, cur_start);
  1274. if (!node)
  1275. goto out;
  1276. while (1) {
  1277. state = rb_entry(node, struct extent_state, rb_node);
  1278. if (state->start > search_end)
  1279. break;
  1280. if (contig && found && state->start > last + 1)
  1281. break;
  1282. if (state->end >= cur_start && (state->state & bits) == bits) {
  1283. total_bytes += min(search_end, state->end) + 1 -
  1284. max(cur_start, state->start);
  1285. if (total_bytes >= max_bytes)
  1286. break;
  1287. if (!found) {
  1288. *start = max(cur_start, state->start);
  1289. found = 1;
  1290. }
  1291. last = state->end;
  1292. } else if (contig && found) {
  1293. break;
  1294. }
  1295. node = rb_next(node);
  1296. if (!node)
  1297. break;
  1298. }
  1299. out:
  1300. spin_unlock(&tree->lock);
  1301. return total_bytes;
  1302. }
  1303. /*
  1304. * set the private field for a given byte offset in the tree. If there isn't
  1305. * an extent_state there already, this does nothing.
  1306. */
  1307. int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
  1308. {
  1309. struct rb_node *node;
  1310. struct extent_state *state;
  1311. int ret = 0;
  1312. spin_lock(&tree->lock);
  1313. /*
  1314. * this search will find all the extents that end after
  1315. * our range starts.
  1316. */
  1317. node = tree_search(tree, start);
  1318. if (!node) {
  1319. ret = -ENOENT;
  1320. goto out;
  1321. }
  1322. state = rb_entry(node, struct extent_state, rb_node);
  1323. if (state->start != start) {
  1324. ret = -ENOENT;
  1325. goto out;
  1326. }
  1327. state->private = private;
  1328. out:
  1329. spin_unlock(&tree->lock);
  1330. return ret;
  1331. }
  1332. int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
  1333. {
  1334. struct rb_node *node;
  1335. struct extent_state *state;
  1336. int ret = 0;
  1337. spin_lock(&tree->lock);
  1338. /*
  1339. * this search will find all the extents that end after
  1340. * our range starts.
  1341. */
  1342. node = tree_search(tree, start);
  1343. if (!node) {
  1344. ret = -ENOENT;
  1345. goto out;
  1346. }
  1347. state = rb_entry(node, struct extent_state, rb_node);
  1348. if (state->start != start) {
  1349. ret = -ENOENT;
  1350. goto out;
  1351. }
  1352. *private = state->private;
  1353. out:
  1354. spin_unlock(&tree->lock);
  1355. return ret;
  1356. }
  1357. /*
  1358. * searches a range in the state tree for a given mask.
  1359. * If 'filled' == 1, this returns 1 only if every extent in the tree
  1360. * has the bits set. Otherwise, 1 is returned if any bit in the
  1361. * range is found set.
  1362. */
  1363. int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
  1364. int bits, int filled, struct extent_state *cached)
  1365. {
  1366. struct extent_state *state = NULL;
  1367. struct rb_node *node;
  1368. int bitset = 0;
  1369. spin_lock(&tree->lock);
  1370. if (cached && cached->tree && cached->start <= start &&
  1371. cached->end > start)
  1372. node = &cached->rb_node;
  1373. else
  1374. node = tree_search(tree, start);
  1375. while (node && start <= end) {
  1376. state = rb_entry(node, struct extent_state, rb_node);
  1377. if (filled && state->start > start) {
  1378. bitset = 0;
  1379. break;
  1380. }
  1381. if (state->start > end)
  1382. break;
  1383. if (state->state & bits) {
  1384. bitset = 1;
  1385. if (!filled)
  1386. break;
  1387. } else if (filled) {
  1388. bitset = 0;
  1389. break;
  1390. }
  1391. if (state->end == (u64)-1)
  1392. break;
  1393. start = state->end + 1;
  1394. if (start > end)
  1395. break;
  1396. node = rb_next(node);
  1397. if (!node) {
  1398. if (filled)
  1399. bitset = 0;
  1400. break;
  1401. }
  1402. }
  1403. spin_unlock(&tree->lock);
  1404. return bitset;
  1405. }
  1406. /*
  1407. * helper function to set a given page up to date if all the
  1408. * extents in the tree for that page are up to date
  1409. */
  1410. static int check_page_uptodate(struct extent_io_tree *tree,
  1411. struct page *page)
  1412. {
  1413. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  1414. u64 end = start + PAGE_CACHE_SIZE - 1;
  1415. if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
  1416. SetPageUptodate(page);
  1417. return 0;
  1418. }
  1419. /*
  1420. * helper function to unlock a page if all the extents in the tree
  1421. * for that page are unlocked
  1422. */
  1423. static int check_page_locked(struct extent_io_tree *tree,
  1424. struct page *page)
  1425. {
  1426. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  1427. u64 end = start + PAGE_CACHE_SIZE - 1;
  1428. if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
  1429. unlock_page(page);
  1430. return 0;
  1431. }
  1432. /*
  1433. * helper function to end page writeback if all the extents
  1434. * in the tree for that page are done with writeback
  1435. */
  1436. static int check_page_writeback(struct extent_io_tree *tree,
  1437. struct page *page)
  1438. {
  1439. end_page_writeback(page);
  1440. return 0;
  1441. }
  1442. /* lots and lots of room for performance fixes in the end_bio funcs */
  1443. /*
  1444. * after a writepage IO is done, we need to:
  1445. * clear the uptodate bits on error
  1446. * clear the writeback bits in the extent tree for this IO
  1447. * end_page_writeback if the page has no more pending IO
  1448. *
  1449. * Scheduling is not allowed, so the extent state tree is expected
  1450. * to have one and only one object corresponding to this IO.
  1451. */
  1452. static void end_bio_extent_writepage(struct bio *bio, int err)
  1453. {
  1454. int uptodate = err == 0;
  1455. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  1456. struct extent_io_tree *tree;
  1457. u64 start;
  1458. u64 end;
  1459. int whole_page;
  1460. int ret;
  1461. do {
  1462. struct page *page = bvec->bv_page;
  1463. tree = &BTRFS_I(page->mapping->host)->io_tree;
  1464. start = ((u64)page->index << PAGE_CACHE_SHIFT) +
  1465. bvec->bv_offset;
  1466. end = start + bvec->bv_len - 1;
  1467. if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
  1468. whole_page = 1;
  1469. else
  1470. whole_page = 0;
  1471. if (--bvec >= bio->bi_io_vec)
  1472. prefetchw(&bvec->bv_page->flags);
  1473. if (tree->ops && tree->ops->writepage_end_io_hook) {
  1474. ret = tree->ops->writepage_end_io_hook(page, start,
  1475. end, NULL, uptodate);
  1476. if (ret)
  1477. uptodate = 0;
  1478. }
  1479. if (!uptodate && tree->ops &&
  1480. tree->ops->writepage_io_failed_hook) {
  1481. ret = tree->ops->writepage_io_failed_hook(bio, page,
  1482. start, end, NULL);
  1483. if (ret == 0) {
  1484. uptodate = (err == 0);
  1485. continue;
  1486. }
  1487. }
  1488. if (!uptodate) {
  1489. clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
  1490. ClearPageUptodate(page);
  1491. SetPageError(page);
  1492. }
  1493. if (whole_page)
  1494. end_page_writeback(page);
  1495. else
  1496. check_page_writeback(tree, page);
  1497. } while (bvec >= bio->bi_io_vec);
  1498. bio_put(bio);
  1499. }
  1500. /*
  1501. * after a readpage IO is done, we need to:
  1502. * clear the uptodate bits on error
  1503. * set the uptodate bits if things worked
  1504. * set the page up to date if all extents in the tree are uptodate
  1505. * clear the lock bit in the extent tree
  1506. * unlock the page if there are no other extents locked for it
  1507. *
  1508. * Scheduling is not allowed, so the extent state tree is expected
  1509. * to have one and only one object corresponding to this IO.
  1510. */
  1511. static void end_bio_extent_readpage(struct bio *bio, int err)
  1512. {
  1513. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  1514. struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
  1515. struct bio_vec *bvec = bio->bi_io_vec;
  1516. struct extent_io_tree *tree;
  1517. u64 start;
  1518. u64 end;
  1519. int whole_page;
  1520. int ret;
  1521. if (err)
  1522. uptodate = 0;
  1523. do {
  1524. struct page *page = bvec->bv_page;
  1525. struct extent_state *cached = NULL;
  1526. struct extent_state *state;
  1527. tree = &BTRFS_I(page->mapping->host)->io_tree;
  1528. start = ((u64)page->index << PAGE_CACHE_SHIFT) +
  1529. bvec->bv_offset;
  1530. end = start + bvec->bv_len - 1;
  1531. if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
  1532. whole_page = 1;
  1533. else
  1534. whole_page = 0;
  1535. if (++bvec <= bvec_end)
  1536. prefetchw(&bvec->bv_page->flags);
  1537. spin_lock(&tree->lock);
  1538. state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
  1539. if (state && state->start == start) {
  1540. /*
  1541. * take a reference on the state, unlock will drop
  1542. * the ref
  1543. */
  1544. cache_state(state, &cached);
  1545. }
  1546. spin_unlock(&tree->lock);
  1547. if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
  1548. ret = tree->ops->readpage_end_io_hook(page, start, end,
  1549. state);
  1550. if (ret)
  1551. uptodate = 0;
  1552. }
  1553. if (!uptodate && tree->ops &&
  1554. tree->ops->readpage_io_failed_hook) {
  1555. ret = tree->ops->readpage_io_failed_hook(bio, page,
  1556. start, end, NULL);
  1557. if (ret == 0) {
  1558. uptodate =
  1559. test_bit(BIO_UPTODATE, &bio->bi_flags);
  1560. if (err)
  1561. uptodate = 0;
  1562. uncache_state(&cached);
  1563. continue;
  1564. }
  1565. }
  1566. if (uptodate) {
  1567. set_extent_uptodate(tree, start, end, &cached,
  1568. GFP_ATOMIC);
  1569. }
  1570. unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
  1571. if (whole_page) {
  1572. if (uptodate) {
  1573. SetPageUptodate(page);
  1574. } else {
  1575. ClearPageUptodate(page);
  1576. SetPageError(page);
  1577. }
  1578. unlock_page(page);
  1579. } else {
  1580. if (uptodate) {
  1581. check_page_uptodate(tree, page);
  1582. } else {
  1583. ClearPageUptodate(page);
  1584. SetPageError(page);
  1585. }
  1586. check_page_locked(tree, page);
  1587. }
  1588. } while (bvec <= bvec_end);
  1589. bio_put(bio);
  1590. }
  1591. struct bio *
  1592. btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
  1593. gfp_t gfp_flags)
  1594. {
  1595. struct bio *bio;
  1596. bio = bio_alloc(gfp_flags, nr_vecs);
  1597. if (bio == NULL && (current->flags & PF_MEMALLOC)) {
  1598. while (!bio && (nr_vecs /= 2))
  1599. bio = bio_alloc(gfp_flags, nr_vecs);
  1600. }
  1601. if (bio) {
  1602. bio->bi_size = 0;
  1603. bio->bi_bdev = bdev;
  1604. bio->bi_sector = first_sector;
  1605. }
  1606. return bio;
  1607. }
  1608. static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
  1609. unsigned long bio_flags)
  1610. {
  1611. int ret = 0;
  1612. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  1613. struct page *page = bvec->bv_page;
  1614. struct extent_io_tree *tree = bio->bi_private;
  1615. u64 start;
  1616. start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
  1617. bio->bi_private = NULL;
  1618. bio_get(bio);
  1619. if (tree->ops && tree->ops->submit_bio_hook)
  1620. ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
  1621. mirror_num, bio_flags, start);
  1622. else
  1623. submit_bio(rw, bio);
  1624. if (bio_flagged(bio, BIO_EOPNOTSUPP))
  1625. ret = -EOPNOTSUPP;
  1626. bio_put(bio);
  1627. return ret;
  1628. }
  1629. static int submit_extent_page(int rw, struct extent_io_tree *tree,
  1630. struct page *page, sector_t sector,
  1631. size_t size, unsigned long offset,
  1632. struct block_device *bdev,
  1633. struct bio **bio_ret,
  1634. unsigned long max_pages,
  1635. bio_end_io_t end_io_func,
  1636. int mirror_num,
  1637. unsigned long prev_bio_flags,
  1638. unsigned long bio_flags)
  1639. {
  1640. int ret = 0;
  1641. struct bio *bio;
  1642. int nr;
  1643. int contig = 0;
  1644. int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
  1645. int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
  1646. size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
  1647. if (bio_ret && *bio_ret) {
  1648. bio = *bio_ret;
  1649. if (old_compressed)
  1650. contig = bio->bi_sector == sector;
  1651. else
  1652. contig = bio->bi_sector + (bio->bi_size >> 9) ==
  1653. sector;
  1654. if (prev_bio_flags != bio_flags || !contig ||
  1655. (tree->ops && tree->ops->merge_bio_hook &&
  1656. tree->ops->merge_bio_hook(page, offset, page_size, bio,
  1657. bio_flags)) ||
  1658. bio_add_page(bio, page, page_size, offset) < page_size) {
  1659. ret = submit_one_bio(rw, bio, mirror_num,
  1660. prev_bio_flags);
  1661. bio = NULL;
  1662. } else {
  1663. return 0;
  1664. }
  1665. }
  1666. if (this_compressed)
  1667. nr = BIO_MAX_PAGES;
  1668. else
  1669. nr = bio_get_nr_vecs(bdev);
  1670. bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
  1671. if (!bio)
  1672. return -ENOMEM;
  1673. bio_add_page(bio, page, page_size, offset);
  1674. bio->bi_end_io = end_io_func;
  1675. bio->bi_private = tree;
  1676. if (bio_ret)
  1677. *bio_ret = bio;
  1678. else
  1679. ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
  1680. return ret;
  1681. }
  1682. void set_page_extent_mapped(struct page *page)
  1683. {
  1684. if (!PagePrivate(page)) {
  1685. SetPagePrivate(page);
  1686. page_cache_get(page);
  1687. set_page_private(page, EXTENT_PAGE_PRIVATE);
  1688. }
  1689. }
  1690. static void set_page_extent_head(struct page *page, unsigned long len)
  1691. {
  1692. WARN_ON(!PagePrivate(page));
  1693. set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
  1694. }
  1695. /*
  1696. * basic readpage implementation. Locked extent state structs are inserted
  1697. * into the tree that are removed when the IO is done (by the end_io
  1698. * handlers)
  1699. */
  1700. static int __extent_read_full_page(struct extent_io_tree *tree,
  1701. struct page *page,
  1702. get_extent_t *get_extent,
  1703. struct bio **bio, int mirror_num,
  1704. unsigned long *bio_flags)
  1705. {
  1706. struct inode *inode = page->mapping->host;
  1707. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  1708. u64 page_end = start + PAGE_CACHE_SIZE - 1;
  1709. u64 end;
  1710. u64 cur = start;
  1711. u64 extent_offset;
  1712. u64 last_byte = i_size_read(inode);
  1713. u64 block_start;
  1714. u64 cur_end;
  1715. sector_t sector;
  1716. struct extent_map *em;
  1717. struct block_device *bdev;
  1718. struct btrfs_ordered_extent *ordered;
  1719. int ret;
  1720. int nr = 0;
  1721. size_t pg_offset = 0;
  1722. size_t iosize;
  1723. size_t disk_io_size;
  1724. size_t blocksize = inode->i_sb->s_blocksize;
  1725. unsigned long this_bio_flag = 0;
  1726. set_page_extent_mapped(page);
  1727. if (!PageUptodate(page)) {
  1728. if (cleancache_get_page(page) == 0) {
  1729. BUG_ON(blocksize != PAGE_SIZE);
  1730. goto out;
  1731. }
  1732. }
  1733. end = page_end;
  1734. while (1) {
  1735. lock_extent(tree, start, end, GFP_NOFS);
  1736. ordered = btrfs_lookup_ordered_extent(inode, start);
  1737. if (!ordered)
  1738. break;
  1739. unlock_extent(tree, start, end, GFP_NOFS);
  1740. btrfs_start_ordered_extent(inode, ordered, 1);
  1741. btrfs_put_ordered_extent(ordered);
  1742. }
  1743. if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
  1744. char *userpage;
  1745. size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
  1746. if (zero_offset) {
  1747. iosize = PAGE_CACHE_SIZE - zero_offset;
  1748. userpage = kmap_atomic(page, KM_USER0);
  1749. memset(userpage + zero_offset, 0, iosize);
  1750. flush_dcache_page(page);
  1751. kunmap_atomic(userpage, KM_USER0);
  1752. }
  1753. }
  1754. while (cur <= end) {
  1755. if (cur >= last_byte) {
  1756. char *userpage;
  1757. struct extent_state *cached = NULL;
  1758. iosize = PAGE_CACHE_SIZE - pg_offset;
  1759. userpage = kmap_atomic(page, KM_USER0);
  1760. memset(userpage + pg_offset, 0, iosize);
  1761. flush_dcache_page(page);
  1762. kunmap_atomic(userpage, KM_USER0);
  1763. set_extent_uptodate(tree, cur, cur + iosize - 1,
  1764. &cached, GFP_NOFS);
  1765. unlock_extent_cached(tree, cur, cur + iosize - 1,
  1766. &cached, GFP_NOFS);
  1767. break;
  1768. }
  1769. em = get_extent(inode, page, pg_offset, cur,
  1770. end - cur + 1, 0);
  1771. if (IS_ERR_OR_NULL(em)) {
  1772. SetPageError(page);
  1773. unlock_extent(tree, cur, end, GFP_NOFS);
  1774. break;
  1775. }
  1776. extent_offset = cur - em->start;
  1777. BUG_ON(extent_map_end(em) <= cur);
  1778. BUG_ON(end < cur);
  1779. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  1780. this_bio_flag = EXTENT_BIO_COMPRESSED;
  1781. extent_set_compress_type(&this_bio_flag,
  1782. em->compress_type);
  1783. }
  1784. iosize = min(extent_map_end(em) - cur, end - cur + 1);
  1785. cur_end = min(extent_map_end(em) - 1, end);
  1786. iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
  1787. if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
  1788. disk_io_size = em->block_len;
  1789. sector = em->block_start >> 9;
  1790. } else {
  1791. sector = (em->block_start + extent_offset) >> 9;
  1792. disk_io_size = iosize;
  1793. }
  1794. bdev = em->bdev;
  1795. block_start = em->block_start;
  1796. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  1797. block_start = EXTENT_MAP_HOLE;
  1798. free_extent_map(em);
  1799. em = NULL;
  1800. /* we've found a hole, just zero and go on */
  1801. if (block_start == EXTENT_MAP_HOLE) {
  1802. char *userpage;
  1803. struct extent_state *cached = NULL;
  1804. userpage = kmap_atomic(page, KM_USER0);
  1805. memset(userpage + pg_offset, 0, iosize)