ordered-data.c 22 KB

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