ordered-data.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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_ALL);
  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. /* The compression code will leave pages locked but return from
  379. * writepage without setting the page writeback. Starting again
  380. * with WB_SYNC_ALL will end up waiting for the IO to actually start.
  381. */
  382. btrfs_fdatawrite_range(inode->i_mapping, start, orig_end, WB_SYNC_ALL);
  383. btrfs_wait_on_page_writeback_range(inode->i_mapping,
  384. start >> PAGE_CACHE_SHIFT,
  385. orig_end >> PAGE_CACHE_SHIFT);
  386. end = orig_end;
  387. while(1) {
  388. ordered = btrfs_lookup_first_ordered_extent(inode, end);
  389. if (!ordered) {
  390. break;
  391. }
  392. if (ordered->file_offset > orig_end) {
  393. btrfs_put_ordered_extent(ordered);
  394. break;
  395. }
  396. if (ordered->file_offset + ordered->len < start) {
  397. btrfs_put_ordered_extent(ordered);
  398. break;
  399. }
  400. btrfs_start_ordered_extent(inode, ordered, 1);
  401. end = ordered->file_offset;
  402. btrfs_put_ordered_extent(ordered);
  403. if (end == 0 || end == start)
  404. break;
  405. end--;
  406. }
  407. if (test_range_bit(&BTRFS_I(inode)->io_tree, start, orig_end,
  408. EXTENT_ORDERED | EXTENT_DELALLOC, 0)) {
  409. schedule_timeout(1);
  410. goto again;
  411. }
  412. return 0;
  413. }
  414. /*
  415. * find an ordered extent corresponding to file_offset. return NULL if
  416. * nothing is found, otherwise take a reference on the extent and return it
  417. */
  418. struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
  419. u64 file_offset)
  420. {
  421. struct btrfs_ordered_inode_tree *tree;
  422. struct rb_node *node;
  423. struct btrfs_ordered_extent *entry = NULL;
  424. tree = &BTRFS_I(inode)->ordered_tree;
  425. mutex_lock(&tree->mutex);
  426. node = tree_search(tree, file_offset);
  427. if (!node)
  428. goto out;
  429. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  430. if (!offset_in_entry(entry, file_offset))
  431. entry = NULL;
  432. if (entry)
  433. atomic_inc(&entry->refs);
  434. out:
  435. mutex_unlock(&tree->mutex);
  436. return entry;
  437. }
  438. /*
  439. * lookup and return any extent before 'file_offset'. NULL is returned
  440. * if none is found
  441. */
  442. struct btrfs_ordered_extent *
  443. btrfs_lookup_first_ordered_extent(struct inode * inode, u64 file_offset)
  444. {
  445. struct btrfs_ordered_inode_tree *tree;
  446. struct rb_node *node;
  447. struct btrfs_ordered_extent *entry = NULL;
  448. tree = &BTRFS_I(inode)->ordered_tree;
  449. mutex_lock(&tree->mutex);
  450. node = tree_search(tree, file_offset);
  451. if (!node)
  452. goto out;
  453. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  454. atomic_inc(&entry->refs);
  455. out:
  456. mutex_unlock(&tree->mutex);
  457. return entry;
  458. }
  459. /*
  460. * After an extent is done, call this to conditionally update the on disk
  461. * i_size. i_size is updated to cover any fully written part of the file.
  462. */
  463. int btrfs_ordered_update_i_size(struct inode *inode,
  464. struct btrfs_ordered_extent *ordered)
  465. {
  466. struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
  467. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  468. u64 disk_i_size;
  469. u64 new_i_size;
  470. u64 i_size_test;
  471. struct rb_node *node;
  472. struct btrfs_ordered_extent *test;
  473. mutex_lock(&tree->mutex);
  474. disk_i_size = BTRFS_I(inode)->disk_i_size;
  475. /*
  476. * if the disk i_size is already at the inode->i_size, or
  477. * this ordered extent is inside the disk i_size, we're done
  478. */
  479. if (disk_i_size >= inode->i_size ||
  480. ordered->file_offset + ordered->len <= disk_i_size) {
  481. goto out;
  482. }
  483. /*
  484. * we can't update the disk_isize if there are delalloc bytes
  485. * between disk_i_size and this ordered extent
  486. */
  487. if (test_range_bit(io_tree, disk_i_size,
  488. ordered->file_offset + ordered->len - 1,
  489. EXTENT_DELALLOC, 0)) {
  490. goto out;
  491. }
  492. /*
  493. * walk backward from this ordered extent to disk_i_size.
  494. * if we find an ordered extent then we can't update disk i_size
  495. * yet
  496. */
  497. node = &ordered->rb_node;
  498. while(1) {
  499. node = rb_prev(node);
  500. if (!node)
  501. break;
  502. test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  503. if (test->file_offset + test->len <= disk_i_size)
  504. break;
  505. if (test->file_offset >= inode->i_size)
  506. break;
  507. if (test->file_offset >= disk_i_size)
  508. goto out;
  509. }
  510. new_i_size = min_t(u64, entry_end(ordered), i_size_read(inode));
  511. /*
  512. * at this point, we know we can safely update i_size to at least
  513. * the offset from this ordered extent. But, we need to
  514. * walk forward and see if ios from higher up in the file have
  515. * finished.
  516. */
  517. node = rb_next(&ordered->rb_node);
  518. i_size_test = 0;
  519. if (node) {
  520. /*
  521. * do we have an area where IO might have finished
  522. * between our ordered extent and the next one.
  523. */
  524. test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  525. if (test->file_offset > entry_end(ordered)) {
  526. i_size_test = test->file_offset;
  527. }
  528. } else {
  529. i_size_test = i_size_read(inode);
  530. }
  531. /*
  532. * i_size_test is the end of a region after this ordered
  533. * extent where there are no ordered extents. As long as there
  534. * are no delalloc bytes in this area, it is safe to update
  535. * disk_i_size to the end of the region.
  536. */
  537. if (i_size_test > entry_end(ordered) &&
  538. !test_range_bit(io_tree, entry_end(ordered), i_size_test - 1,
  539. EXTENT_DELALLOC, 0)) {
  540. new_i_size = min_t(u64, i_size_test, i_size_read(inode));
  541. }
  542. BTRFS_I(inode)->disk_i_size = new_i_size;
  543. out:
  544. mutex_unlock(&tree->mutex);
  545. return 0;
  546. }
  547. /*
  548. * search the ordered extents for one corresponding to 'offset' and
  549. * try to find a checksum. This is used because we allow pages to
  550. * be reclaimed before their checksum is actually put into the btree
  551. */
  552. int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u32 *sum)
  553. {
  554. struct btrfs_ordered_sum *ordered_sum;
  555. struct btrfs_sector_sum *sector_sums;
  556. struct btrfs_ordered_extent *ordered;
  557. struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
  558. struct list_head *cur;
  559. unsigned long num_sectors;
  560. unsigned long i;
  561. u32 sectorsize = BTRFS_I(inode)->root->sectorsize;
  562. int ret = 1;
  563. ordered = btrfs_lookup_ordered_extent(inode, offset);
  564. if (!ordered)
  565. return 1;
  566. mutex_lock(&tree->mutex);
  567. list_for_each_prev(cur, &ordered->list) {
  568. ordered_sum = list_entry(cur, struct btrfs_ordered_sum, list);
  569. if (offset >= ordered_sum->file_offset) {
  570. num_sectors = ordered_sum->len / sectorsize;
  571. sector_sums = ordered_sum->sums;
  572. for (i = 0; i < num_sectors; i++) {
  573. if (sector_sums[i].offset == offset) {
  574. *sum = sector_sums[i].sum;
  575. ret = 0;
  576. goto out;
  577. }
  578. }
  579. }
  580. }
  581. out:
  582. mutex_unlock(&tree->mutex);
  583. btrfs_put_ordered_extent(ordered);
  584. return ret;
  585. }
  586. /**
  587. * taken from mm/filemap.c because it isn't exported
  588. *
  589. * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
  590. * @mapping: address space structure to write
  591. * @start: offset in bytes where the range starts
  592. * @end: offset in bytes where the range ends (inclusive)
  593. * @sync_mode: enable synchronous operation
  594. *
  595. * Start writeback against all of a mapping's dirty pages that lie
  596. * within the byte offsets <start, end> inclusive.
  597. *
  598. * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
  599. * opposed to a regular memory cleansing writeback. The difference between
  600. * these two operations is that if a dirty page/buffer is encountered, it must
  601. * be waited upon, and not just skipped over.
  602. */
  603. int btrfs_fdatawrite_range(struct address_space *mapping, loff_t start,
  604. loff_t end, int sync_mode)
  605. {
  606. struct writeback_control wbc = {
  607. .sync_mode = sync_mode,
  608. .nr_to_write = mapping->nrpages * 2,
  609. .range_start = start,
  610. .range_end = end,
  611. .for_writepages = 1,
  612. };
  613. return btrfs_writepages(mapping, &wbc);
  614. }
  615. /**
  616. * taken from mm/filemap.c because it isn't exported
  617. *
  618. * wait_on_page_writeback_range - wait for writeback to complete
  619. * @mapping: target address_space
  620. * @start: beginning page index
  621. * @end: ending page index
  622. *
  623. * Wait for writeback to complete against pages indexed by start->end
  624. * inclusive
  625. */
  626. int btrfs_wait_on_page_writeback_range(struct address_space *mapping,
  627. pgoff_t start, pgoff_t end)
  628. {
  629. struct pagevec pvec;
  630. int nr_pages;
  631. int ret = 0;
  632. pgoff_t index;
  633. if (end < start)
  634. return 0;
  635. pagevec_init(&pvec, 0);
  636. index = start;
  637. while ((index <= end) &&
  638. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  639. PAGECACHE_TAG_WRITEBACK,
  640. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {
  641. unsigned i;
  642. for (i = 0; i < nr_pages; i++) {
  643. struct page *page = pvec.pages[i];
  644. /* until radix tree lookup accepts end_index */
  645. if (page->index > end)
  646. continue;
  647. wait_on_page_writeback(page);
  648. if (PageError(page))
  649. ret = -EIO;
  650. }
  651. pagevec_release(&pvec);
  652. cond_resched();
  653. }
  654. /* Check for outstanding write errors */
  655. if (test_and_clear_bit(AS_ENOSPC, &mapping->flags))
  656. ret = -ENOSPC;
  657. if (test_and_clear_bit(AS_EIO, &mapping->flags))
  658. ret = -EIO;
  659. return ret;
  660. }