ordered-data.c 23 KB

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