ordered-data.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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. struct btrfs_ordered_extent *ordered;
  466. int found;
  467. if (start + len < start) {
  468. orig_end = INT_LIMIT(loff_t);
  469. } else {
  470. orig_end = start + len - 1;
  471. if (orig_end > INT_LIMIT(loff_t))
  472. orig_end = INT_LIMIT(loff_t);
  473. }
  474. again:
  475. /* start IO across the range first to instantiate any delalloc
  476. * extents
  477. */
  478. filemap_fdatawrite_range(inode->i_mapping, start, orig_end);
  479. /* The compression code will leave pages locked but return from
  480. * writepage without setting the page writeback. Starting again
  481. * with WB_SYNC_ALL will end up waiting for the IO to actually start.
  482. */
  483. filemap_fdatawrite_range(inode->i_mapping, start, orig_end);
  484. filemap_fdatawait_range(inode->i_mapping, start, orig_end);
  485. end = orig_end;
  486. found = 0;
  487. while (1) {
  488. ordered = btrfs_lookup_first_ordered_extent(inode, end);
  489. if (!ordered)
  490. break;
  491. if (ordered->file_offset > orig_end) {
  492. btrfs_put_ordered_extent(ordered);
  493. break;
  494. }
  495. if (ordered->file_offset + ordered->len < start) {
  496. btrfs_put_ordered_extent(ordered);
  497. break;
  498. }
  499. found++;
  500. btrfs_start_ordered_extent(inode, ordered, 1);
  501. end = ordered->file_offset;
  502. btrfs_put_ordered_extent(ordered);
  503. if (end == 0 || end == start)
  504. break;
  505. end--;
  506. }
  507. if (found || test_range_bit(&BTRFS_I(inode)->io_tree, start, orig_end,
  508. EXTENT_DELALLOC, 0, NULL)) {
  509. schedule_timeout(1);
  510. goto again;
  511. }
  512. return 0;
  513. }
  514. /*
  515. * find an ordered extent corresponding to file_offset. return NULL if
  516. * nothing is found, otherwise take a reference on the extent and return it
  517. */
  518. struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
  519. u64 file_offset)
  520. {
  521. struct btrfs_ordered_inode_tree *tree;
  522. struct rb_node *node;
  523. struct btrfs_ordered_extent *entry = NULL;
  524. tree = &BTRFS_I(inode)->ordered_tree;
  525. spin_lock(&tree->lock);
  526. node = tree_search(tree, file_offset);
  527. if (!node)
  528. goto out;
  529. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  530. if (!offset_in_entry(entry, file_offset))
  531. entry = NULL;
  532. if (entry)
  533. atomic_inc(&entry->refs);
  534. out:
  535. spin_unlock(&tree->lock);
  536. return entry;
  537. }
  538. /* Since the DIO code tries to lock a wide area we need to look for any ordered
  539. * extents that exist in the range, rather than just the start of the range.
  540. */
  541. struct btrfs_ordered_extent *btrfs_lookup_ordered_range(struct inode *inode,
  542. u64 file_offset,
  543. u64 len)
  544. {
  545. struct btrfs_ordered_inode_tree *tree;
  546. struct rb_node *node;
  547. struct btrfs_ordered_extent *entry = NULL;
  548. tree = &BTRFS_I(inode)->ordered_tree;
  549. spin_lock(&tree->lock);
  550. node = tree_search(tree, file_offset);
  551. if (!node) {
  552. node = tree_search(tree, file_offset + len);
  553. if (!node)
  554. goto out;
  555. }
  556. while (1) {
  557. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  558. if (range_overlaps(entry, file_offset, len))
  559. break;
  560. if (entry->file_offset >= file_offset + len) {
  561. entry = NULL;
  562. break;
  563. }
  564. entry = NULL;
  565. node = rb_next(node);
  566. if (!node)
  567. break;
  568. }
  569. out:
  570. if (entry)
  571. atomic_inc(&entry->refs);
  572. spin_unlock(&tree->lock);
  573. return entry;
  574. }
  575. /*
  576. * lookup and return any extent before 'file_offset'. NULL is returned
  577. * if none is found
  578. */
  579. struct btrfs_ordered_extent *
  580. btrfs_lookup_first_ordered_extent(struct inode *inode, u64 file_offset)
  581. {
  582. struct btrfs_ordered_inode_tree *tree;
  583. struct rb_node *node;
  584. struct btrfs_ordered_extent *entry = NULL;
  585. tree = &BTRFS_I(inode)->ordered_tree;
  586. spin_lock(&tree->lock);
  587. node = tree_search(tree, file_offset);
  588. if (!node)
  589. goto out;
  590. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  591. atomic_inc(&entry->refs);
  592. out:
  593. spin_unlock(&tree->lock);
  594. return entry;
  595. }
  596. /*
  597. * After an extent is done, call this to conditionally update the on disk
  598. * i_size. i_size is updated to cover any fully written part of the file.
  599. */
  600. int btrfs_ordered_update_i_size(struct inode *inode, u64 offset,
  601. struct btrfs_ordered_extent *ordered)
  602. {
  603. struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
  604. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  605. u64 disk_i_size;
  606. u64 new_i_size;
  607. u64 i_size_test;
  608. u64 i_size = i_size_read(inode);
  609. struct rb_node *node;
  610. struct rb_node *prev = NULL;
  611. struct btrfs_ordered_extent *test;
  612. int ret = 1;
  613. if (ordered)
  614. offset = entry_end(ordered);
  615. else
  616. offset = ALIGN(offset, BTRFS_I(inode)->root->sectorsize);
  617. spin_lock(&tree->lock);
  618. disk_i_size = BTRFS_I(inode)->disk_i_size;
  619. /* truncate file */
  620. if (disk_i_size > i_size) {
  621. BTRFS_I(inode)->disk_i_size = i_size;
  622. ret = 0;
  623. goto out;
  624. }
  625. /*
  626. * if the disk i_size is already at the inode->i_size, or
  627. * this ordered extent is inside the disk i_size, we're done
  628. */
  629. if (disk_i_size == i_size || offset <= disk_i_size) {
  630. goto out;
  631. }
  632. /*
  633. * we can't update the disk_isize if there are delalloc bytes
  634. * between disk_i_size and this ordered extent
  635. */
  636. if (test_range_bit(io_tree, disk_i_size, offset - 1,
  637. EXTENT_DELALLOC, 0, NULL)) {
  638. goto out;
  639. }
  640. /*
  641. * walk backward from this ordered extent to disk_i_size.
  642. * if we find an ordered extent then we can't update disk i_size
  643. * yet
  644. */
  645. if (ordered) {
  646. node = rb_prev(&ordered->rb_node);
  647. } else {
  648. prev = tree_search(tree, offset);
  649. /*
  650. * we insert file extents without involving ordered struct,
  651. * so there should be no ordered struct cover this offset
  652. */
  653. if (prev) {
  654. test = rb_entry(prev, struct btrfs_ordered_extent,
  655. rb_node);
  656. BUG_ON(offset_in_entry(test, offset));
  657. }
  658. node = prev;
  659. }
  660. while (node) {
  661. test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  662. if (test->file_offset + test->len <= disk_i_size)
  663. break;
  664. if (test->file_offset >= i_size)
  665. break;
  666. if (test->file_offset >= disk_i_size)
  667. goto out;
  668. node = rb_prev(node);
  669. }
  670. new_i_size = min_t(u64, offset, i_size);
  671. /*
  672. * at this point, we know we can safely update i_size to at least
  673. * the offset from this ordered extent. But, we need to
  674. * walk forward and see if ios from higher up in the file have
  675. * finished.
  676. */
  677. if (ordered) {
  678. node = rb_next(&ordered->rb_node);
  679. } else {
  680. if (prev)
  681. node = rb_next(prev);
  682. else
  683. node = rb_first(&tree->tree);
  684. }
  685. i_size_test = 0;
  686. if (node) {
  687. /*
  688. * do we have an area where IO might have finished
  689. * between our ordered extent and the next one.
  690. */
  691. test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  692. if (test->file_offset > offset)
  693. i_size_test = test->file_offset;
  694. } else {
  695. i_size_test = i_size;
  696. }
  697. /*
  698. * i_size_test is the end of a region after this ordered
  699. * extent where there are no ordered extents. As long as there
  700. * are no delalloc bytes in this area, it is safe to update
  701. * disk_i_size to the end of the region.
  702. */
  703. if (i_size_test > offset &&
  704. !test_range_bit(io_tree, offset, i_size_test - 1,
  705. EXTENT_DELALLOC, 0, NULL)) {
  706. new_i_size = min_t(u64, i_size_test, i_size);
  707. }
  708. BTRFS_I(inode)->disk_i_size = new_i_size;
  709. ret = 0;
  710. out:
  711. /*
  712. * we need to remove the ordered extent with the tree lock held
  713. * so that other people calling this function don't find our fully
  714. * processed ordered entry and skip updating the i_size
  715. */
  716. if (ordered)
  717. __btrfs_remove_ordered_extent(inode, ordered);
  718. spin_unlock(&tree->lock);
  719. if (ordered)
  720. wake_up(&ordered->wait);
  721. return ret;
  722. }
  723. /*
  724. * search the ordered extents for one corresponding to 'offset' and
  725. * try to find a checksum. This is used because we allow pages to
  726. * be reclaimed before their checksum is actually put into the btree
  727. */
  728. int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr,
  729. u32 *sum)
  730. {
  731. struct btrfs_ordered_sum *ordered_sum;
  732. struct btrfs_sector_sum *sector_sums;
  733. struct btrfs_ordered_extent *ordered;
  734. struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
  735. unsigned long num_sectors;
  736. unsigned long i;
  737. u32 sectorsize = BTRFS_I(inode)->root->sectorsize;
  738. int ret = 1;
  739. ordered = btrfs_lookup_ordered_extent(inode, offset);
  740. if (!ordered)
  741. return 1;
  742. spin_lock(&tree->lock);
  743. list_for_each_entry_reverse(ordered_sum, &ordered->list, list) {
  744. if (disk_bytenr >= ordered_sum->bytenr) {
  745. num_sectors = ordered_sum->len / sectorsize;
  746. sector_sums = ordered_sum->sums;
  747. for (i = 0; i < num_sectors; i++) {
  748. if (sector_sums[i].bytenr == disk_bytenr) {
  749. *sum = sector_sums[i].sum;
  750. ret = 0;
  751. goto out;
  752. }
  753. }
  754. }
  755. }
  756. out:
  757. spin_unlock(&tree->lock);
  758. btrfs_put_ordered_extent(ordered);
  759. return ret;
  760. }
  761. /*
  762. * add a given inode to the list of inodes that must be fully on
  763. * disk before a transaction commit finishes.
  764. *
  765. * This basically gives us the ext3 style data=ordered mode, and it is mostly
  766. * used to make sure renamed files are fully on disk.
  767. *
  768. * It is a noop if the inode is already fully on disk.
  769. *
  770. * If trans is not null, we'll do a friendly check for a transaction that
  771. * is already flushing things and force the IO down ourselves.
  772. */
  773. int btrfs_add_ordered_operation(struct btrfs_trans_handle *trans,
  774. struct btrfs_root *root,
  775. struct inode *inode)
  776. {
  777. u64 last_mod;
  778. last_mod = max(BTRFS_I(inode)->generation, BTRFS_I(inode)->last_trans);
  779. /*
  780. * if this file hasn't been changed since the last transaction
  781. * commit, we can safely return without doing anything
  782. */
  783. if (last_mod < root->fs_info->last_trans_committed)
  784. return 0;
  785. /*
  786. * the transaction is already committing. Just start the IO and
  787. * don't bother with all of this list nonsense
  788. */
  789. if (trans && root->fs_info->running_transaction->blocked) {
  790. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  791. return 0;
  792. }
  793. spin_lock(&root->fs_info->ordered_extent_lock);
  794. if (list_empty(&BTRFS_I(inode)->ordered_operations)) {
  795. list_add_tail(&BTRFS_I(inode)->ordered_operations,
  796. &root->fs_info->ordered_operations);
  797. }
  798. spin_unlock(&root->fs_info->ordered_extent_lock);
  799. return 0;
  800. }