ordered-data.c 22 KB

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