ordered-data.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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. * The tree is given a single reference on the ordered extent that was
  148. * inserted.
  149. */
  150. int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
  151. u64 start, u64 len, u64 disk_len, int type)
  152. {
  153. struct btrfs_ordered_inode_tree *tree;
  154. struct rb_node *node;
  155. struct btrfs_ordered_extent *entry;
  156. tree = &BTRFS_I(inode)->ordered_tree;
  157. entry = kzalloc(sizeof(*entry), GFP_NOFS);
  158. if (!entry)
  159. return -ENOMEM;
  160. entry->file_offset = file_offset;
  161. entry->start = start;
  162. entry->len = len;
  163. entry->disk_len = disk_len;
  164. entry->bytes_left = len;
  165. entry->inode = inode;
  166. if (type != BTRFS_ORDERED_IO_DONE && type != BTRFS_ORDERED_COMPLETE)
  167. set_bit(type, &entry->flags);
  168. /* one ref for the tree */
  169. atomic_set(&entry->refs, 1);
  170. init_waitqueue_head(&entry->wait);
  171. INIT_LIST_HEAD(&entry->list);
  172. INIT_LIST_HEAD(&entry->root_extent_list);
  173. spin_lock(&tree->lock);
  174. node = tree_insert(&tree->tree, file_offset,
  175. &entry->rb_node);
  176. BUG_ON(node);
  177. spin_unlock(&tree->lock);
  178. spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
  179. list_add_tail(&entry->root_extent_list,
  180. &BTRFS_I(inode)->root->fs_info->ordered_extents);
  181. spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
  182. BUG_ON(node);
  183. return 0;
  184. }
  185. /*
  186. * Add a struct btrfs_ordered_sum into the list of checksums to be inserted
  187. * when an ordered extent is finished. If the list covers more than one
  188. * ordered extent, it is split across multiples.
  189. */
  190. int btrfs_add_ordered_sum(struct inode *inode,
  191. struct btrfs_ordered_extent *entry,
  192. struct btrfs_ordered_sum *sum)
  193. {
  194. struct btrfs_ordered_inode_tree *tree;
  195. tree = &BTRFS_I(inode)->ordered_tree;
  196. spin_lock(&tree->lock);
  197. list_add_tail(&sum->list, &entry->list);
  198. spin_unlock(&tree->lock);
  199. return 0;
  200. }
  201. /*
  202. * this is used to account for finished IO across a given range
  203. * of the file. The IO should not span ordered extents. If
  204. * a given ordered_extent is completely done, 1 is returned, otherwise
  205. * 0.
  206. *
  207. * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
  208. * to make sure this function only returns 1 once for a given ordered extent.
  209. */
  210. int btrfs_dec_test_ordered_pending(struct inode *inode,
  211. struct btrfs_ordered_extent **cached,
  212. u64 file_offset, u64 io_size)
  213. {
  214. struct btrfs_ordered_inode_tree *tree;
  215. struct rb_node *node;
  216. struct btrfs_ordered_extent *entry = NULL;
  217. int ret;
  218. tree = &BTRFS_I(inode)->ordered_tree;
  219. spin_lock(&tree->lock);
  220. node = tree_search(tree, file_offset);
  221. if (!node) {
  222. ret = 1;
  223. goto out;
  224. }
  225. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  226. if (!offset_in_entry(entry, file_offset)) {
  227. ret = 1;
  228. goto out;
  229. }
  230. if (io_size > entry->bytes_left) {
  231. printk(KERN_CRIT "bad ordered accounting left %llu size %llu\n",
  232. (unsigned long long)entry->bytes_left,
  233. (unsigned long long)io_size);
  234. }
  235. entry->bytes_left -= io_size;
  236. if (entry->bytes_left == 0)
  237. ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
  238. else
  239. ret = 1;
  240. out:
  241. if (!ret && cached && entry) {
  242. *cached = entry;
  243. atomic_inc(&entry->refs);
  244. }
  245. spin_unlock(&tree->lock);
  246. return ret == 0;
  247. }
  248. /*
  249. * used to drop a reference on an ordered extent. This will free
  250. * the extent if the last reference is dropped
  251. */
  252. int btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
  253. {
  254. struct list_head *cur;
  255. struct btrfs_ordered_sum *sum;
  256. if (atomic_dec_and_test(&entry->refs)) {
  257. while (!list_empty(&entry->list)) {
  258. cur = entry->list.next;
  259. sum = list_entry(cur, struct btrfs_ordered_sum, list);
  260. list_del(&sum->list);
  261. kfree(sum);
  262. }
  263. kfree(entry);
  264. }
  265. return 0;
  266. }
  267. /*
  268. * remove an ordered extent from the tree. No references are dropped
  269. * and you must wake_up entry->wait. You must hold the tree lock
  270. * while you call this function.
  271. */
  272. static int __btrfs_remove_ordered_extent(struct inode *inode,
  273. struct btrfs_ordered_extent *entry)
  274. {
  275. struct btrfs_ordered_inode_tree *tree;
  276. struct btrfs_root *root = BTRFS_I(inode)->root;
  277. struct rb_node *node;
  278. tree = &BTRFS_I(inode)->ordered_tree;
  279. node = &entry->rb_node;
  280. rb_erase(node, &tree->tree);
  281. tree->last = NULL;
  282. set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
  283. spin_lock(&BTRFS_I(inode)->accounting_lock);
  284. WARN_ON(!BTRFS_I(inode)->outstanding_extents);
  285. BTRFS_I(inode)->outstanding_extents--;
  286. spin_unlock(&BTRFS_I(inode)->accounting_lock);
  287. btrfs_unreserve_metadata_for_delalloc(BTRFS_I(inode)->root,
  288. inode, 1);
  289. spin_lock(&root->fs_info->ordered_extent_lock);
  290. list_del_init(&entry->root_extent_list);
  291. /*
  292. * we have no more ordered extents for this inode and
  293. * no dirty pages. We can safely remove it from the
  294. * list of ordered extents
  295. */
  296. if (RB_EMPTY_ROOT(&tree->tree) &&
  297. !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) {
  298. list_del_init(&BTRFS_I(inode)->ordered_operations);
  299. }
  300. spin_unlock(&root->fs_info->ordered_extent_lock);
  301. return 0;
  302. }
  303. /*
  304. * remove an ordered extent from the tree. No references are dropped
  305. * but any waiters are woken.
  306. */
  307. int btrfs_remove_ordered_extent(struct inode *inode,
  308. struct btrfs_ordered_extent *entry)
  309. {
  310. struct btrfs_ordered_inode_tree *tree;
  311. int ret;
  312. tree = &BTRFS_I(inode)->ordered_tree;
  313. spin_lock(&tree->lock);
  314. ret = __btrfs_remove_ordered_extent(inode, entry);
  315. spin_unlock(&tree->lock);
  316. wake_up(&entry->wait);
  317. return ret;
  318. }
  319. /*
  320. * wait for all the ordered extents in a root. This is done when balancing
  321. * space between drives.
  322. */
  323. int btrfs_wait_ordered_extents(struct btrfs_root *root,
  324. int nocow_only, int delay_iput)
  325. {
  326. struct list_head splice;
  327. struct list_head *cur;
  328. struct btrfs_ordered_extent *ordered;
  329. struct inode *inode;
  330. INIT_LIST_HEAD(&splice);
  331. spin_lock(&root->fs_info->ordered_extent_lock);
  332. list_splice_init(&root->fs_info->ordered_extents, &splice);
  333. while (!list_empty(&splice)) {
  334. cur = splice.next;
  335. ordered = list_entry(cur, struct btrfs_ordered_extent,
  336. root_extent_list);
  337. if (nocow_only &&
  338. !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags) &&
  339. !test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
  340. list_move(&ordered->root_extent_list,
  341. &root->fs_info->ordered_extents);
  342. cond_resched_lock(&root->fs_info->ordered_extent_lock);
  343. continue;
  344. }
  345. list_del_init(&ordered->root_extent_list);
  346. atomic_inc(&ordered->refs);
  347. /*
  348. * the inode may be getting freed (in sys_unlink path).
  349. */
  350. inode = igrab(ordered->inode);
  351. spin_unlock(&root->fs_info->ordered_extent_lock);
  352. if (inode) {
  353. btrfs_start_ordered_extent(inode, ordered, 1);
  354. btrfs_put_ordered_extent(ordered);
  355. if (delay_iput)
  356. btrfs_add_delayed_iput(inode);
  357. else
  358. iput(inode);
  359. } else {
  360. btrfs_put_ordered_extent(ordered);
  361. }
  362. spin_lock(&root->fs_info->ordered_extent_lock);
  363. }
  364. spin_unlock(&root->fs_info->ordered_extent_lock);
  365. return 0;
  366. }
  367. /*
  368. * this is used during transaction commit to write all the inodes
  369. * added to the ordered operation list. These files must be fully on
  370. * disk before the transaction commits.
  371. *
  372. * we have two modes here, one is to just start the IO via filemap_flush
  373. * and the other is to wait for all the io. When we wait, we have an
  374. * extra check to make sure the ordered operation list really is empty
  375. * before we return
  376. */
  377. int btrfs_run_ordered_operations(struct btrfs_root *root, int wait)
  378. {
  379. struct btrfs_inode *btrfs_inode;
  380. struct inode *inode;
  381. struct list_head splice;
  382. INIT_LIST_HEAD(&splice);
  383. mutex_lock(&root->fs_info->ordered_operations_mutex);
  384. spin_lock(&root->fs_info->ordered_extent_lock);
  385. again:
  386. list_splice_init(&root->fs_info->ordered_operations, &splice);
  387. while (!list_empty(&splice)) {
  388. btrfs_inode = list_entry(splice.next, struct btrfs_inode,
  389. ordered_operations);
  390. inode = &btrfs_inode->vfs_inode;
  391. list_del_init(&btrfs_inode->ordered_operations);
  392. /*
  393. * the inode may be getting freed (in sys_unlink path).
  394. */
  395. inode = igrab(inode);
  396. if (!wait && inode) {
  397. list_add_tail(&BTRFS_I(inode)->ordered_operations,
  398. &root->fs_info->ordered_operations);
  399. }
  400. spin_unlock(&root->fs_info->ordered_extent_lock);
  401. if (inode) {
  402. if (wait)
  403. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  404. else
  405. filemap_flush(inode->i_mapping);
  406. btrfs_add_delayed_iput(inode);
  407. }
  408. cond_resched();
  409. spin_lock(&root->fs_info->ordered_extent_lock);
  410. }
  411. if (wait && !list_empty(&root->fs_info->ordered_operations))
  412. goto again;
  413. spin_unlock(&root->fs_info->ordered_extent_lock);
  414. mutex_unlock(&root->fs_info->ordered_operations_mutex);
  415. return 0;
  416. }
  417. /*
  418. * Used to start IO or wait for a given ordered extent to finish.
  419. *
  420. * If wait is one, this effectively waits on page writeback for all the pages
  421. * in the extent, and it waits on the io completion code to insert
  422. * metadata into the btree corresponding to the extent
  423. */
  424. void btrfs_start_ordered_extent(struct inode *inode,
  425. struct btrfs_ordered_extent *entry,
  426. int wait)
  427. {
  428. u64 start = entry->file_offset;
  429. u64 end = start + entry->len - 1;
  430. /*
  431. * pages in the range can be dirty, clean or writeback. We
  432. * start IO on any dirty ones so the wait doesn't stall waiting
  433. * for pdflush to find them
  434. */
  435. filemap_fdatawrite_range(inode->i_mapping, start, end);
  436. if (wait) {
  437. wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
  438. &entry->flags));
  439. }
  440. }
  441. /*
  442. * Used to wait on ordered extents across a large range of bytes.
  443. */
  444. int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
  445. {
  446. u64 end;
  447. u64 orig_end;
  448. u64 wait_end;
  449. struct btrfs_ordered_extent *ordered;
  450. int found;
  451. if (start + len < start) {
  452. orig_end = INT_LIMIT(loff_t);
  453. } else {
  454. orig_end = start + len - 1;
  455. if (orig_end > INT_LIMIT(loff_t))
  456. orig_end = INT_LIMIT(loff_t);
  457. }
  458. wait_end = orig_end;
  459. again:
  460. /* start IO across the range first to instantiate any delalloc
  461. * extents
  462. */
  463. filemap_fdatawrite_range(inode->i_mapping, start, orig_end);
  464. /* The compression code will leave pages locked but return from
  465. * writepage without setting the page writeback. Starting again
  466. * with WB_SYNC_ALL will end up waiting for the IO to actually start.
  467. */
  468. filemap_fdatawrite_range(inode->i_mapping, start, orig_end);
  469. filemap_fdatawait_range(inode->i_mapping, start, orig_end);
  470. end = orig_end;
  471. found = 0;
  472. while (1) {
  473. ordered = btrfs_lookup_first_ordered_extent(inode, end);
  474. if (!ordered)
  475. break;
  476. if (ordered->file_offset > orig_end) {
  477. btrfs_put_ordered_extent(ordered);
  478. break;
  479. }
  480. if (ordered->file_offset + ordered->len < start) {
  481. btrfs_put_ordered_extent(ordered);
  482. break;
  483. }
  484. found++;
  485. btrfs_start_ordered_extent(inode, ordered, 1);
  486. end = ordered->file_offset;
  487. btrfs_put_ordered_extent(ordered);
  488. if (end == 0 || end == start)
  489. break;
  490. end--;
  491. }
  492. if (found || test_range_bit(&BTRFS_I(inode)->io_tree, start, orig_end,
  493. EXTENT_DELALLOC, 0, NULL)) {
  494. schedule_timeout(1);
  495. goto again;
  496. }
  497. return 0;
  498. }
  499. /*
  500. * find an ordered extent corresponding to file_offset. return NULL if
  501. * nothing is found, otherwise take a reference on the extent and return it
  502. */
  503. struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
  504. u64 file_offset)
  505. {
  506. struct btrfs_ordered_inode_tree *tree;
  507. struct rb_node *node;
  508. struct btrfs_ordered_extent *entry = NULL;
  509. tree = &BTRFS_I(inode)->ordered_tree;
  510. spin_lock(&tree->lock);
  511. node = tree_search(tree, file_offset);
  512. if (!node)
  513. goto out;
  514. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  515. if (!offset_in_entry(entry, file_offset))
  516. entry = NULL;
  517. if (entry)
  518. atomic_inc(&entry->refs);
  519. out:
  520. spin_unlock(&tree->lock);
  521. return entry;
  522. }
  523. /*
  524. * lookup and return any extent before 'file_offset'. NULL is returned
  525. * if none is found
  526. */
  527. struct btrfs_ordered_extent *
  528. btrfs_lookup_first_ordered_extent(struct inode *inode, u64 file_offset)
  529. {
  530. struct btrfs_ordered_inode_tree *tree;
  531. struct rb_node *node;
  532. struct btrfs_ordered_extent *entry = NULL;
  533. tree = &BTRFS_I(inode)->ordered_tree;
  534. spin_lock(&tree->lock);
  535. node = tree_search(tree, file_offset);
  536. if (!node)
  537. goto out;
  538. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  539. atomic_inc(&entry->refs);
  540. out:
  541. spin_unlock(&tree->lock);
  542. return entry;
  543. }
  544. /*
  545. * After an extent is done, call this to conditionally update the on disk
  546. * i_size. i_size is updated to cover any fully written part of the file.
  547. */
  548. int btrfs_ordered_update_i_size(struct inode *inode, u64 offset,
  549. struct btrfs_ordered_extent *ordered)
  550. {
  551. struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
  552. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  553. u64 disk_i_size;
  554. u64 new_i_size;
  555. u64 i_size_test;
  556. u64 i_size = i_size_read(inode);
  557. struct rb_node *node;
  558. struct rb_node *prev = NULL;
  559. struct btrfs_ordered_extent *test;
  560. int ret = 1;
  561. if (ordered)
  562. offset = entry_end(ordered);
  563. else
  564. offset = ALIGN(offset, BTRFS_I(inode)->root->sectorsize);
  565. spin_lock(&tree->lock);
  566. disk_i_size = BTRFS_I(inode)->disk_i_size;
  567. /* truncate file */
  568. if (disk_i_size > i_size) {
  569. BTRFS_I(inode)->disk_i_size = i_size;
  570. ret = 0;
  571. goto out;
  572. }
  573. /*
  574. * if the disk i_size is already at the inode->i_size, or
  575. * this ordered extent is inside the disk i_size, we're done
  576. */
  577. if (disk_i_size == i_size || offset <= disk_i_size) {
  578. goto out;
  579. }
  580. /*
  581. * we can't update the disk_isize if there are delalloc bytes
  582. * between disk_i_size and this ordered extent
  583. */
  584. if (test_range_bit(io_tree, disk_i_size, offset - 1,
  585. EXTENT_DELALLOC, 0, NULL)) {
  586. goto out;
  587. }
  588. /*
  589. * walk backward from this ordered extent to disk_i_size.
  590. * if we find an ordered extent then we can't update disk i_size
  591. * yet
  592. */
  593. if (ordered) {
  594. node = rb_prev(&ordered->rb_node);
  595. } else {
  596. prev = tree_search(tree, offset);
  597. /*
  598. * we insert file extents without involving ordered struct,
  599. * so there should be no ordered struct cover this offset
  600. */
  601. if (prev) {
  602. test = rb_entry(prev, struct btrfs_ordered_extent,
  603. rb_node);
  604. BUG_ON(offset_in_entry(test, offset));
  605. }
  606. node = prev;
  607. }
  608. while (node) {
  609. test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  610. if (test->file_offset + test->len <= disk_i_size)
  611. break;
  612. if (test->file_offset >= i_size)
  613. break;
  614. if (test->file_offset >= disk_i_size)
  615. goto out;
  616. node = rb_prev(node);
  617. }
  618. new_i_size = min_t(u64, offset, i_size);
  619. /*
  620. * at this point, we know we can safely update i_size to at least
  621. * the offset from this ordered extent. But, we need to
  622. * walk forward and see if ios from higher up in the file have
  623. * finished.
  624. */
  625. if (ordered) {
  626. node = rb_next(&ordered->rb_node);
  627. } else {
  628. if (prev)
  629. node = rb_next(prev);
  630. else
  631. node = rb_first(&tree->tree);
  632. }
  633. i_size_test = 0;
  634. if (node) {
  635. /*
  636. * do we have an area where IO might have finished
  637. * between our ordered extent and the next one.
  638. */
  639. test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  640. if (test->file_offset > offset)
  641. i_size_test = test->file_offset;
  642. } else {
  643. i_size_test = i_size;
  644. }
  645. /*
  646. * i_size_test is the end of a region after this ordered
  647. * extent where there are no ordered extents. As long as there
  648. * are no delalloc bytes in this area, it is safe to update
  649. * disk_i_size to the end of the region.
  650. */
  651. if (i_size_test > offset &&
  652. !test_range_bit(io_tree, offset, i_size_test - 1,
  653. EXTENT_DELALLOC, 0, NULL)) {
  654. new_i_size = min_t(u64, i_size_test, i_size);
  655. }
  656. BTRFS_I(inode)->disk_i_size = new_i_size;
  657. ret = 0;
  658. out:
  659. /*
  660. * we need to remove the ordered extent with the tree lock held
  661. * so that other people calling this function don't find our fully
  662. * processed ordered entry and skip updating the i_size
  663. */
  664. if (ordered)
  665. __btrfs_remove_ordered_extent(inode, ordered);
  666. spin_unlock(&tree->lock);
  667. if (ordered)
  668. wake_up(&ordered->wait);
  669. return ret;
  670. }
  671. /*
  672. * search the ordered extents for one corresponding to 'offset' and
  673. * try to find a checksum. This is used because we allow pages to
  674. * be reclaimed before their checksum is actually put into the btree
  675. */
  676. int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr,
  677. u32 *sum)
  678. {
  679. struct btrfs_ordered_sum *ordered_sum;
  680. struct btrfs_sector_sum *sector_sums;
  681. struct btrfs_ordered_extent *ordered;
  682. struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
  683. unsigned long num_sectors;
  684. unsigned long i;
  685. u32 sectorsize = BTRFS_I(inode)->root->sectorsize;
  686. int ret = 1;
  687. ordered = btrfs_lookup_ordered_extent(inode, offset);
  688. if (!ordered)
  689. return 1;
  690. spin_lock(&tree->lock);
  691. list_for_each_entry_reverse(ordered_sum, &ordered->list, list) {
  692. if (disk_bytenr >= ordered_sum->bytenr) {
  693. num_sectors = ordered_sum->len / sectorsize;
  694. sector_sums = ordered_sum->sums;
  695. for (i = 0; i < num_sectors; i++) {
  696. if (sector_sums[i].bytenr == disk_bytenr) {
  697. *sum = sector_sums[i].sum;
  698. ret = 0;
  699. goto out;
  700. }
  701. }
  702. }
  703. }
  704. out:
  705. spin_unlock(&tree->lock);
  706. btrfs_put_ordered_extent(ordered);
  707. return ret;
  708. }
  709. /*
  710. * add a given inode to the list of inodes that must be fully on
  711. * disk before a transaction commit finishes.
  712. *
  713. * This basically gives us the ext3 style data=ordered mode, and it is mostly
  714. * used to make sure renamed files are fully on disk.
  715. *
  716. * It is a noop if the inode is already fully on disk.
  717. *
  718. * If trans is not null, we'll do a friendly check for a transaction that
  719. * is already flushing things and force the IO down ourselves.
  720. */
  721. int btrfs_add_ordered_operation(struct btrfs_trans_handle *trans,
  722. struct btrfs_root *root,
  723. struct inode *inode)
  724. {
  725. u64 last_mod;
  726. last_mod = max(BTRFS_I(inode)->generation, BTRFS_I(inode)->last_trans);
  727. /*
  728. * if this file hasn't been changed since the last transaction
  729. * commit, we can safely return without doing anything
  730. */
  731. if (last_mod < root->fs_info->last_trans_committed)
  732. return 0;
  733. /*
  734. * the transaction is already committing. Just start the IO and
  735. * don't bother with all of this list nonsense
  736. */
  737. if (trans && root->fs_info->running_transaction->blocked) {
  738. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  739. return 0;
  740. }
  741. spin_lock(&root->fs_info->ordered_extent_lock);
  742. if (list_empty(&BTRFS_I(inode)->ordered_operations)) {
  743. list_add_tail(&BTRFS_I(inode)->ordered_operations,
  744. &root->fs_info->ordered_operations);
  745. }
  746. spin_unlock(&root->fs_info->ordered_extent_lock);
  747. return 0;
  748. }