ordered-data.c 26 KB

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