ordered-data.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/gfp.h>
  19. #include <linux/slab.h>
  20. #include <linux/blkdev.h>
  21. #include <linux/writeback.h>
  22. #include <linux/pagevec.h>
  23. #include "ctree.h"
  24. #include "transaction.h"
  25. #include "btrfs_inode.h"
  26. #include "extent_io.h"
  27. static u64 entry_end(struct btrfs_ordered_extent *entry)
  28. {
  29. if (entry->file_offset + entry->len < entry->file_offset)
  30. return (u64)-1;
  31. return entry->file_offset + entry->len;
  32. }
  33. /* returns NULL if the insertion worked, or it returns the node it did find
  34. * in the tree
  35. */
  36. static struct rb_node *tree_insert(struct rb_root *root, u64 file_offset,
  37. struct rb_node *node)
  38. {
  39. struct rb_node ** p = &root->rb_node;
  40. struct rb_node * parent = NULL;
  41. struct btrfs_ordered_extent *entry;
  42. while(*p) {
  43. parent = *p;
  44. entry = rb_entry(parent, struct btrfs_ordered_extent, rb_node);
  45. if (file_offset < entry->file_offset)
  46. p = &(*p)->rb_left;
  47. else if (file_offset >= entry_end(entry))
  48. p = &(*p)->rb_right;
  49. else
  50. return parent;
  51. }
  52. rb_link_node(node, parent, p);
  53. rb_insert_color(node, root);
  54. return NULL;
  55. }
  56. /*
  57. * look for a given offset in the tree, and if it can't be found return the
  58. * first lesser offset
  59. */
  60. static struct rb_node *__tree_search(struct rb_root *root, u64 file_offset,
  61. struct rb_node **prev_ret)
  62. {
  63. struct rb_node * n = root->rb_node;
  64. struct rb_node *prev = NULL;
  65. struct rb_node *test;
  66. struct btrfs_ordered_extent *entry;
  67. struct btrfs_ordered_extent *prev_entry = NULL;
  68. while(n) {
  69. entry = rb_entry(n, struct btrfs_ordered_extent, rb_node);
  70. prev = n;
  71. prev_entry = entry;
  72. if (file_offset < entry->file_offset)
  73. n = n->rb_left;
  74. else if (file_offset >= entry_end(entry))
  75. n = n->rb_right;
  76. else
  77. return n;
  78. }
  79. if (!prev_ret)
  80. return NULL;
  81. while(prev && file_offset >= entry_end(prev_entry)) {
  82. test = rb_next(prev);
  83. if (!test)
  84. break;
  85. prev_entry = rb_entry(test, struct btrfs_ordered_extent,
  86. rb_node);
  87. if (file_offset < entry_end(prev_entry))
  88. break;
  89. prev = test;
  90. }
  91. if (prev)
  92. prev_entry = rb_entry(prev, struct btrfs_ordered_extent,
  93. rb_node);
  94. while(prev && file_offset < entry_end(prev_entry)) {
  95. test = rb_prev(prev);
  96. if (!test)
  97. break;
  98. prev_entry = rb_entry(test, struct btrfs_ordered_extent,
  99. rb_node);
  100. prev = test;
  101. }
  102. *prev_ret = prev;
  103. return NULL;
  104. }
  105. /*
  106. * helper to check if a given offset is inside a given entry
  107. */
  108. static int offset_in_entry(struct btrfs_ordered_extent *entry, u64 file_offset)
  109. {
  110. if (file_offset < entry->file_offset ||
  111. entry->file_offset + entry->len <= file_offset)
  112. return 0;
  113. return 1;
  114. }
  115. /*
  116. * look find the first ordered struct that has this offset, otherwise
  117. * the first one less than this offset
  118. */
  119. static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree,
  120. u64 file_offset)
  121. {
  122. struct rb_root *root = &tree->tree;
  123. struct rb_node *prev;
  124. struct rb_node *ret;
  125. struct btrfs_ordered_extent *entry;
  126. if (tree->last) {
  127. entry = rb_entry(tree->last, struct btrfs_ordered_extent,
  128. rb_node);
  129. if (offset_in_entry(entry, file_offset))
  130. return tree->last;
  131. }
  132. ret = __tree_search(root, file_offset, &prev);
  133. if (!ret)
  134. ret = prev;
  135. if (ret)
  136. tree->last = ret;
  137. return ret;
  138. }
  139. /* allocate and add a new ordered_extent into the per-inode tree.
  140. * file_offset is the logical offset in the file
  141. *
  142. * start is the disk block number of an extent already reserved in the
  143. * extent allocation tree
  144. *
  145. * len is the length of the extent
  146. *
  147. * This also sets the EXTENT_ORDERED bit on the range in the inode.
  148. *
  149. * The tree is given a single reference on the ordered extent that was
  150. * inserted.
  151. */
  152. int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
  153. u64 start, u64 len, u64 disk_len, int type)
  154. {
  155. struct btrfs_ordered_inode_tree *tree;
  156. struct rb_node *node;
  157. struct btrfs_ordered_extent *entry;
  158. tree = &BTRFS_I(inode)->ordered_tree;
  159. entry = kzalloc(sizeof(*entry), GFP_NOFS);
  160. if (!entry)
  161. return -ENOMEM;
  162. mutex_lock(&tree->mutex);
  163. entry->file_offset = file_offset;
  164. entry->start = start;
  165. entry->len = len;
  166. entry->disk_len = disk_len;
  167. entry->inode = inode;
  168. if (type != BTRFS_ORDERED_IO_DONE && type != BTRFS_ORDERED_COMPLETE)
  169. set_bit(type, &entry->flags);
  170. /* one ref for the tree */
  171. atomic_set(&entry->refs, 1);
  172. init_waitqueue_head(&entry->wait);
  173. INIT_LIST_HEAD(&entry->list);
  174. INIT_LIST_HEAD(&entry->root_extent_list);
  175. node = tree_insert(&tree->tree, file_offset,
  176. &entry->rb_node);
  177. if (node) {
  178. printk("warning dup entry from add_ordered_extent\n");
  179. BUG();
  180. }
  181. set_extent_ordered(&BTRFS_I(inode)->io_tree, file_offset,
  182. entry_end(entry) - 1, GFP_NOFS);
  183. spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
  184. list_add_tail(&entry->root_extent_list,
  185. &BTRFS_I(inode)->root->fs_info->ordered_extents);
  186. spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
  187. mutex_unlock(&tree->mutex);
  188. BUG_ON(node);
  189. return 0;
  190. }
  191. /*
  192. * Add a struct btrfs_ordered_sum into the list of checksums to be inserted
  193. * when an ordered extent is finished. If the list covers more than one
  194. * ordered extent, it is split across multiples.
  195. */
  196. int btrfs_add_ordered_sum(struct inode *inode,
  197. struct btrfs_ordered_extent *entry,
  198. struct btrfs_ordered_sum *sum)
  199. {
  200. struct btrfs_ordered_inode_tree *tree;
  201. tree = &BTRFS_I(inode)->ordered_tree;
  202. mutex_lock(&tree->mutex);
  203. list_add_tail(&sum->list, &entry->list);
  204. mutex_unlock(&tree->mutex);
  205. return 0;
  206. }
  207. /*
  208. * this is used to account for finished IO across a given range
  209. * of the file. The IO should not span ordered extents. If
  210. * a given ordered_extent is completely done, 1 is returned, otherwise
  211. * 0.
  212. *
  213. * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
  214. * to make sure this function only returns 1 once for a given ordered extent.
  215. */
  216. int btrfs_dec_test_ordered_pending(struct inode *inode,
  217. u64 file_offset, u64 io_size)
  218. {
  219. struct btrfs_ordered_inode_tree *tree;
  220. struct rb_node *node;
  221. struct btrfs_ordered_extent *entry;
  222. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  223. int ret;
  224. tree = &BTRFS_I(inode)->ordered_tree;
  225. mutex_lock(&tree->mutex);
  226. clear_extent_ordered(io_tree, file_offset, file_offset + io_size - 1,
  227. GFP_NOFS);
  228. node = tree_search(tree, file_offset);
  229. if (!node) {
  230. ret = 1;
  231. goto out;
  232. }
  233. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  234. if (!offset_in_entry(entry, file_offset)) {
  235. ret = 1;
  236. goto out;
  237. }
  238. ret = test_range_bit(io_tree, entry->file_offset,
  239. entry->file_offset + entry->len - 1,
  240. EXTENT_ORDERED, 0);
  241. if (ret == 0)
  242. ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
  243. out:
  244. mutex_unlock(&tree->mutex);
  245. return ret == 0;
  246. }
  247. /*
  248. * used to drop a reference on an ordered extent. This will free
  249. * the extent if the last reference is dropped
  250. */
  251. int btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
  252. {
  253. struct list_head *cur;
  254. struct btrfs_ordered_sum *sum;
  255. if (atomic_dec_and_test(&entry->refs)) {
  256. while(!list_empty(&entry->list)) {
  257. cur = entry->list.next;
  258. sum = list_entry(cur, struct btrfs_ordered_sum, list);
  259. list_del(&sum->list);
  260. kfree(sum);
  261. }
  262. kfree(entry);
  263. }
  264. return 0;
  265. }
  266. /*
  267. * remove an ordered extent from the tree. No references are dropped
  268. * but, anyone waiting on this extent is woken up.
  269. */
  270. int btrfs_remove_ordered_extent(struct inode *inode,
  271. struct btrfs_ordered_extent *entry)
  272. {
  273. struct btrfs_ordered_inode_tree *tree;
  274. struct rb_node *node;
  275. tree = &BTRFS_I(inode)->ordered_tree;
  276. mutex_lock(&tree->mutex);
  277. node = &entry->rb_node;
  278. rb_erase(node, &tree->tree);
  279. tree->last = NULL;
  280. set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
  281. spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
  282. list_del_init(&entry->root_extent_list);
  283. spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
  284. mutex_unlock(&tree->mutex);
  285. wake_up(&entry->wait);
  286. return 0;
  287. }
  288. /*
  289. * wait for all the ordered extents in a root. This is done when balancing
  290. * space between drives.
  291. */
  292. int btrfs_wait_ordered_extents(struct btrfs_root *root, int nocow_only)
  293. {
  294. struct list_head splice;
  295. struct list_head *cur;
  296. struct btrfs_ordered_extent *ordered;
  297. struct inode *inode;
  298. INIT_LIST_HEAD(&splice);
  299. spin_lock(&root->fs_info->ordered_extent_lock);
  300. list_splice_init(&root->fs_info->ordered_extents, &splice);
  301. while (!list_empty(&splice)) {
  302. cur = splice.next;
  303. ordered = list_entry(cur, struct btrfs_ordered_extent,
  304. root_extent_list);
  305. if (nocow_only &&
  306. !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags) &&
  307. !test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
  308. list_move(&ordered->root_extent_list,
  309. &root->fs_info->ordered_extents);
  310. cond_resched_lock(&root->fs_info->ordered_extent_lock);
  311. continue;
  312. }
  313. list_del_init(&ordered->root_extent_list);
  314. atomic_inc(&ordered->refs);
  315. /*
  316. * the inode may be getting freed (in sys_unlink path).
  317. */
  318. inode = igrab(ordered->inode);
  319. spin_unlock(&root->fs_info->ordered_extent_lock);
  320. if (inode) {
  321. btrfs_start_ordered_extent(inode, ordered, 1);
  322. btrfs_put_ordered_extent(ordered);
  323. iput(inode);
  324. } else {
  325. btrfs_put_ordered_extent(ordered);
  326. }
  327. spin_lock(&root->fs_info->ordered_extent_lock);
  328. }
  329. spin_unlock(&root->fs_info->ordered_extent_lock);
  330. return 0;
  331. }
  332. /*
  333. * Used to start IO or wait for a given ordered extent to finish.
  334. *
  335. * If wait is one, this effectively waits on page writeback for all the pages
  336. * in the extent, and it waits on the io completion code to insert
  337. * metadata into the btree corresponding to the extent
  338. */
  339. void btrfs_start_ordered_extent(struct inode *inode,
  340. struct btrfs_ordered_extent *entry,
  341. int wait)
  342. {
  343. u64 start = entry->file_offset;
  344. u64 end = start + entry->len - 1;
  345. /*
  346. * pages in the range can be dirty, clean or writeback. We
  347. * start IO on any dirty ones so the wait doesn't stall waiting
  348. * for pdflush to find them
  349. */
  350. btrfs_fdatawrite_range(inode->i_mapping, start, end, WB_SYNC_NONE);
  351. if (wait) {
  352. wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
  353. &entry->flags));
  354. }
  355. }
  356. /*
  357. * Used to wait on ordered extents across a large range of bytes.
  358. */
  359. int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
  360. {
  361. u64 end;
  362. u64 orig_end;
  363. u64 wait_end;
  364. struct btrfs_ordered_extent *ordered;
  365. if (start + len < start) {
  366. orig_end = INT_LIMIT(loff_t);
  367. } else {
  368. orig_end = start + len - 1;
  369. if (orig_end > INT_LIMIT(loff_t))
  370. orig_end = INT_LIMIT(loff_t);
  371. }
  372. wait_end = orig_end;
  373. again:
  374. /* start IO across the range first to instantiate any delalloc
  375. * extents
  376. */
  377. btrfs_fdatawrite_range(inode->i_mapping, start, orig_end, WB_SYNC_NONE);
  378. btrfs_wait_on_page_writeback_range(inode->i_mapping,
  379. start >> PAGE_CACHE_SHIFT,
  380. orig_end >> PAGE_CACHE_SHIFT);
  381. end = orig_end;
  382. while(1) {
  383. ordered = btrfs_lookup_first_ordered_extent(inode, end);
  384. if (!ordered) {
  385. break;
  386. }
  387. if (ordered->file_offset > orig_end) {
  388. btrfs_put_ordered_extent(ordered);
  389. break;
  390. }
  391. if (ordered->file_offset + ordered->len < start) {
  392. btrfs_put_ordered_extent(ordered);
  393. break;
  394. }
  395. btrfs_start_ordered_extent(inode, ordered, 1);
  396. end = ordered->file_offset;
  397. btrfs_put_ordered_extent(ordered);
  398. if (end == 0 || end == start)
  399. break;
  400. end--;
  401. }
  402. if (test_range_bit(&BTRFS_I(inode)->io_tree, start, orig_end,
  403. EXTENT_ORDERED | EXTENT_DELALLOC, 0)) {
  404. printk("inode %lu still ordered or delalloc after wait "
  405. "%llu %llu\n", inode->i_ino,
  406. (unsigned long long)start,
  407. (unsigned long long)orig_end);
  408. goto again;
  409. }
  410. return 0;
  411. }
  412. /*
  413. * find an ordered extent corresponding to file_offset. return NULL if
  414. * nothing is found, otherwise take a reference on the extent and return it
  415. */
  416. struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
  417. u64 file_offset)
  418. {
  419. struct btrfs_ordered_inode_tree *tree;
  420. struct rb_node *node;
  421. struct btrfs_ordered_extent *entry = NULL;
  422. tree = &BTRFS_I(inode)->ordered_tree;
  423. mutex_lock(&tree->mutex);
  424. node = tree_search(tree, file_offset);
  425. if (!node)
  426. goto out;
  427. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  428. if (!offset_in_entry(entry, file_offset))
  429. entry = NULL;
  430. if (entry)
  431. atomic_inc(&entry->refs);
  432. out:
  433. mutex_unlock(&tree->mutex);
  434. return entry;
  435. }
  436. /*
  437. * lookup and return any extent before 'file_offset'. NULL is returned
  438. * if none is found
  439. */
  440. struct btrfs_ordered_extent *
  441. btrfs_lookup_first_ordered_extent(struct inode * inode, u64 file_offset)
  442. {
  443. struct btrfs_ordered_inode_tree *tree;
  444. struct rb_node *node;
  445. struct btrfs_ordered_extent *entry = NULL;
  446. tree = &BTRFS_I(inode)->ordered_tree;
  447. mutex_lock(&tree->mutex);
  448. node = tree_search(tree, file_offset);
  449. if (!node)
  450. goto out;
  451. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  452. atomic_inc(&entry->refs);
  453. out:
  454. mutex_unlock(&tree->mutex);
  455. return entry;
  456. }
  457. /*
  458. * After an extent is done, call this to conditionally update the on disk
  459. * i_size. i_size is updated to cover any fully written part of the file.
  460. */
  461. int btrfs_ordered_update_i_size(struct inode *inode,
  462. struct btrfs_ordered_extent *ordered)
  463. {
  464. struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
  465. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  466. u64 disk_i_size;
  467. u64 new_i_size;
  468. u64 i_size_test;
  469. struct rb_node *node;
  470. struct btrfs_ordered_extent *test;
  471. mutex_lock(&tree->mutex);
  472. disk_i_size = BTRFS_I(inode)->disk_i_size;
  473. /*
  474. * if the disk i_size is already at the inode->i_size, or
  475. * this ordered extent is inside the disk i_size, we're done
  476. */
  477. if (disk_i_size >= inode->i_size ||
  478. ordered->file_offset + ordered->len <= disk_i_size) {
  479. goto out;
  480. }
  481. /*
  482. * we can't update the disk_isize if there are delalloc bytes
  483. * between disk_i_size and this ordered extent
  484. */
  485. if (test_range_bit(io_tree, disk_i_size,
  486. ordered->file_offset + ordered->len - 1,
  487. EXTENT_DELALLOC, 0)) {
  488. goto out;
  489. }
  490. /*
  491. * walk backward from this ordered extent to disk_i_size.
  492. * if we find an ordered extent then we can't update disk i_size
  493. * yet
  494. */
  495. node = &ordered->rb_node;
  496. while(1) {
  497. node = rb_prev(node);
  498. if (!node)
  499. break;
  500. test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  501. if (test->file_offset + test->len <= disk_i_size)
  502. break;
  503. if (test->file_offset >= inode->i_size)
  504. break;
  505. if (test->file_offset >= disk_i_size)
  506. goto out;
  507. }
  508. new_i_size = min_t(u64, entry_end(ordered), i_size_read(inode));
  509. /*
  510. * at this point, we know we can safely update i_size to at least
  511. * the offset from this ordered extent. But, we need to
  512. * walk forward and see if ios from higher up in the file have
  513. * finished.
  514. */
  515. node = rb_next(&ordered->rb_node);
  516. i_size_test = 0;
  517. if (node) {
  518. /*
  519. * do we have an area where IO might have finished
  520. * between our ordered extent and the next one.
  521. */
  522. test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  523. if (test->file_offset > entry_end(ordered)) {
  524. i_size_test = test->file_offset;
  525. }
  526. } else {
  527. i_size_test = i_size_read(inode);
  528. }
  529. /*
  530. * i_size_test is the end of a region after this ordered
  531. * extent where there are no ordered extents. As long as there
  532. * are no delalloc bytes in this area, it is safe to update
  533. * disk_i_size to the end of the region.
  534. */
  535. if (i_size_test > entry_end(ordered) &&
  536. !test_range_bit(io_tree, entry_end(ordered), i_size_test - 1,
  537. EXTENT_DELALLOC, 0)) {
  538. new_i_size = min_t(u64, i_size_test, i_size_read(inode));
  539. }
  540. BTRFS_I(inode)->disk_i_size = new_i_size;
  541. out:
  542. mutex_unlock(&tree->mutex);
  543. return 0;
  544. }
  545. /*
  546. * search the ordered extents for one corresponding to 'offset' and
  547. * try to find a checksum. This is used because we allow pages to
  548. * be reclaimed before their checksum is actually put into the btree
  549. */
  550. int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u32 *sum)
  551. {
  552. struct btrfs_ordered_sum *ordered_sum;
  553. struct btrfs_sector_sum *sector_sums;
  554. struct btrfs_ordered_extent *ordered;
  555. struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
  556. struct list_head *cur;
  557. unsigned long num_sectors;
  558. unsigned long i;
  559. u32 sectorsize = BTRFS_I(inode)->root->sectorsize;
  560. int ret = 1;
  561. ordered = btrfs_lookup_ordered_extent(inode, offset);
  562. if (!ordered)
  563. return 1;
  564. mutex_lock(&tree->mutex);
  565. list_for_each_prev(cur, &ordered->list) {
  566. ordered_sum = list_entry(cur, struct btrfs_ordered_sum, list);
  567. if (offset >= ordered_sum->file_offset) {
  568. num_sectors = ordered_sum->len / sectorsize;
  569. sector_sums = ordered_sum->sums;
  570. for (i = 0; i < num_sectors; i++) {
  571. if (sector_sums[i].offset == offset) {
  572. *sum = sector_sums[i].sum;
  573. ret = 0;
  574. goto out;
  575. }
  576. }
  577. }
  578. }
  579. out:
  580. mutex_unlock(&tree->mutex);
  581. btrfs_put_ordered_extent(ordered);
  582. return ret;
  583. }
  584. /**
  585. * taken from mm/filemap.c because it isn't exported
  586. *
  587. * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
  588. * @mapping: address space structure to write
  589. * @start: offset in bytes where the range starts
  590. * @end: offset in bytes where the range ends (inclusive)
  591. * @sync_mode: enable synchronous operation
  592. *
  593. * Start writeback against all of a mapping's dirty pages that lie
  594. * within the byte offsets <start, end> inclusive.
  595. *
  596. * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
  597. * opposed to a regular memory cleansing writeback. The difference between
  598. * these two operations is that if a dirty page/buffer is encountered, it must
  599. * be waited upon, and not just skipped over.
  600. */
  601. int btrfs_fdatawrite_range(struct address_space *mapping, loff_t start,
  602. loff_t end, int sync_mode)
  603. {
  604. struct writeback_control wbc = {
  605. .sync_mode = sync_mode,
  606. .nr_to_write = mapping->nrpages * 2,
  607. .range_start = start,
  608. .range_end = end,
  609. .for_writepages = 1,
  610. };
  611. return btrfs_writepages(mapping, &wbc);
  612. }
  613. /**
  614. * taken from mm/filemap.c because it isn't exported
  615. *
  616. * wait_on_page_writeback_range - wait for writeback to complete
  617. * @mapping: target address_space
  618. * @start: beginning page index
  619. * @end: ending page index
  620. *
  621. * Wait for writeback to complete against pages indexed by start->end
  622. * inclusive
  623. */
  624. int btrfs_wait_on_page_writeback_range(struct address_space *mapping,
  625. pgoff_t start, pgoff_t end)
  626. {
  627. struct pagevec pvec;
  628. int nr_pages;
  629. int ret = 0;
  630. pgoff_t index;
  631. if (end < start)
  632. return 0;
  633. pagevec_init(&pvec, 0);
  634. index = start;
  635. while ((index <= end) &&
  636. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  637. PAGECACHE_TAG_WRITEBACK,
  638. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {
  639. unsigned i;
  640. for (i = 0; i < nr_pages; i++) {
  641. struct page *page = pvec.pages[i];
  642. /* until radix tree lookup accepts end_index */
  643. if (page->index > end)
  644. continue;
  645. wait_on_page_writeback(page);
  646. if (PageError(page))
  647. ret = -EIO;
  648. }
  649. pagevec_release(&pvec);
  650. cond_resched();
  651. }
  652. /* Check for outstanding write errors */
  653. if (test_and_clear_bit(AS_ENOSPC, &mapping->flags))
  654. ret = -ENOSPC;
  655. if (test_and_clear_bit(AS_EIO, &mapping->flags))
  656. ret = -EIO;
  657. return ret;
  658. }