ordered-data.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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 = igrab(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_irq(&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_irq(&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_irq(&tree->lock);
  238. list_add_tail(&sum->list, &entry->list);
  239. spin_unlock_irq(&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, int uptodate)
  256. {
  257. struct btrfs_ordered_inode_tree *tree;
  258. struct rb_node *node;
  259. struct btrfs_ordered_extent *entry = NULL;
  260. int ret;
  261. unsigned long flags;
  262. u64 dec_end;
  263. u64 dec_start;
  264. u64 to_dec;
  265. tree = &BTRFS_I(inode)->ordered_tree;
  266. spin_lock_irqsave(&tree->lock, flags);
  267. node = tree_search(tree, *file_offset);
  268. if (!node) {
  269. ret = 1;
  270. goto out;
  271. }
  272. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  273. if (!offset_in_entry(entry, *file_offset)) {
  274. ret = 1;
  275. goto out;
  276. }
  277. dec_start = max(*file_offset, entry->file_offset);
  278. dec_end = min(*file_offset + io_size, entry->file_offset +
  279. entry->len);
  280. *file_offset = dec_end;
  281. if (dec_start > dec_end) {
  282. printk(KERN_CRIT "bad ordering dec_start %llu end %llu\n",
  283. (unsigned long long)dec_start,
  284. (unsigned long long)dec_end);
  285. }
  286. to_dec = dec_end - dec_start;
  287. if (to_dec > entry->bytes_left) {
  288. printk(KERN_CRIT "bad ordered accounting left %llu size %llu\n",
  289. (unsigned long long)entry->bytes_left,
  290. (unsigned long long)to_dec);
  291. }
  292. entry->bytes_left -= to_dec;
  293. if (!uptodate)
  294. set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
  295. if (entry->bytes_left == 0)
  296. ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
  297. else
  298. ret = 1;
  299. out:
  300. if (!ret && cached && entry) {
  301. *cached = entry;
  302. atomic_inc(&entry->refs);
  303. }
  304. spin_unlock_irqrestore(&tree->lock, flags);
  305. return ret == 0;
  306. }
  307. /*
  308. * this is used to account for finished IO across a given range
  309. * of the file. The IO should not span ordered extents. If
  310. * a given ordered_extent is completely done, 1 is returned, otherwise
  311. * 0.
  312. *
  313. * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
  314. * to make sure this function only returns 1 once for a given ordered extent.
  315. */
  316. int btrfs_dec_test_ordered_pending(struct inode *inode,
  317. struct btrfs_ordered_extent **cached,
  318. u64 file_offset, u64 io_size, int uptodate)
  319. {
  320. struct btrfs_ordered_inode_tree *tree;
  321. struct rb_node *node;
  322. struct btrfs_ordered_extent *entry = NULL;
  323. unsigned long flags;
  324. int ret;
  325. tree = &BTRFS_I(inode)->ordered_tree;
  326. spin_lock_irqsave(&tree->lock, flags);
  327. if (cached && *cached) {
  328. entry = *cached;
  329. goto have_entry;
  330. }
  331. node = tree_search(tree, file_offset);
  332. if (!node) {
  333. ret = 1;
  334. goto out;
  335. }
  336. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  337. have_entry:
  338. if (!offset_in_entry(entry, file_offset)) {
  339. ret = 1;
  340. goto out;
  341. }
  342. if (io_size > entry->bytes_left) {
  343. printk(KERN_CRIT "bad ordered accounting left %llu size %llu\n",
  344. (unsigned long long)entry->bytes_left,
  345. (unsigned long long)io_size);
  346. }
  347. entry->bytes_left -= io_size;
  348. if (!uptodate)
  349. set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
  350. if (entry->bytes_left == 0)
  351. ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
  352. else
  353. ret = 1;
  354. out:
  355. if (!ret && cached && entry) {
  356. *cached = entry;
  357. atomic_inc(&entry->refs);
  358. }
  359. spin_unlock_irqrestore(&tree->lock, flags);
  360. return ret == 0;
  361. }
  362. /*
  363. * used to drop a reference on an ordered extent. This will free
  364. * the extent if the last reference is dropped
  365. */
  366. void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
  367. {
  368. struct list_head *cur;
  369. struct btrfs_ordered_sum *sum;
  370. trace_btrfs_ordered_extent_put(entry->inode, entry);
  371. if (atomic_dec_and_test(&entry->refs)) {
  372. if (entry->inode)
  373. btrfs_add_delayed_iput(entry->inode);
  374. while (!list_empty(&entry->list)) {
  375. cur = entry->list.next;
  376. sum = list_entry(cur, struct btrfs_ordered_sum, list);
  377. list_del(&sum->list);
  378. kfree(sum);
  379. }
  380. kfree(entry);
  381. }
  382. }
  383. /*
  384. * remove an ordered extent from the tree. No references are dropped
  385. * and waiters are woken up.
  386. */
  387. void btrfs_remove_ordered_extent(struct inode *inode,
  388. struct btrfs_ordered_extent *entry)
  389. {
  390. struct btrfs_ordered_inode_tree *tree;
  391. struct btrfs_root *root = BTRFS_I(inode)->root;
  392. struct rb_node *node;
  393. tree = &BTRFS_I(inode)->ordered_tree;
  394. spin_lock_irq(&tree->lock);
  395. node = &entry->rb_node;
  396. rb_erase(node, &tree->tree);
  397. tree->last = NULL;
  398. set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
  399. spin_unlock_irq(&tree->lock);
  400. spin_lock(&root->fs_info->ordered_extent_lock);
  401. list_del_init(&entry->root_extent_list);
  402. trace_btrfs_ordered_extent_remove(inode, entry);
  403. /*
  404. * we have no more ordered extents for this inode and
  405. * no dirty pages. We can safely remove it from the
  406. * list of ordered extents
  407. */
  408. if (RB_EMPTY_ROOT(&tree->tree) &&
  409. !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) {
  410. list_del_init(&BTRFS_I(inode)->ordered_operations);
  411. }
  412. spin_unlock(&root->fs_info->ordered_extent_lock);
  413. wake_up(&entry->wait);
  414. }
  415. /*
  416. * wait for all the ordered extents in a root. This is done when balancing
  417. * space between drives.
  418. */
  419. void btrfs_wait_ordered_extents(struct btrfs_root *root,
  420. int nocow_only, int delay_iput)
  421. {
  422. struct list_head splice;
  423. struct list_head *cur;
  424. struct btrfs_ordered_extent *ordered;
  425. struct inode *inode;
  426. INIT_LIST_HEAD(&splice);
  427. spin_lock(&root->fs_info->ordered_extent_lock);
  428. list_splice_init(&root->fs_info->ordered_extents, &splice);
  429. while (!list_empty(&splice)) {
  430. cur = splice.next;
  431. ordered = list_entry(cur, struct btrfs_ordered_extent,
  432. root_extent_list);
  433. if (nocow_only &&
  434. !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags) &&
  435. !test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
  436. list_move(&ordered->root_extent_list,
  437. &root->fs_info->ordered_extents);
  438. cond_resched_lock(&root->fs_info->ordered_extent_lock);
  439. continue;
  440. }
  441. list_del_init(&ordered->root_extent_list);
  442. atomic_inc(&ordered->refs);
  443. /*
  444. * the inode may be getting freed (in sys_unlink path).
  445. */
  446. inode = igrab(ordered->inode);
  447. spin_unlock(&root->fs_info->ordered_extent_lock);
  448. if (inode) {
  449. btrfs_start_ordered_extent(inode, ordered, 1);
  450. btrfs_put_ordered_extent(ordered);
  451. if (delay_iput)
  452. btrfs_add_delayed_iput(inode);
  453. else
  454. iput(inode);
  455. } else {
  456. btrfs_put_ordered_extent(ordered);
  457. }
  458. spin_lock(&root->fs_info->ordered_extent_lock);
  459. }
  460. spin_unlock(&root->fs_info->ordered_extent_lock);
  461. }
  462. /*
  463. * this is used during transaction commit to write all the inodes
  464. * added to the ordered operation list. These files must be fully on
  465. * disk before the transaction commits.
  466. *
  467. * we have two modes here, one is to just start the IO via filemap_flush
  468. * and the other is to wait for all the io. When we wait, we have an
  469. * extra check to make sure the ordered operation list really is empty
  470. * before we return
  471. */
  472. void btrfs_run_ordered_operations(struct btrfs_root *root, int wait)
  473. {
  474. struct btrfs_inode *btrfs_inode;
  475. struct inode *inode;
  476. struct list_head splice;
  477. INIT_LIST_HEAD(&splice);
  478. mutex_lock(&root->fs_info->ordered_operations_mutex);
  479. spin_lock(&root->fs_info->ordered_extent_lock);
  480. again:
  481. list_splice_init(&root->fs_info->ordered_operations, &splice);
  482. while (!list_empty(&splice)) {
  483. btrfs_inode = list_entry(splice.next, struct btrfs_inode,
  484. ordered_operations);
  485. inode = &btrfs_inode->vfs_inode;
  486. list_del_init(&btrfs_inode->ordered_operations);
  487. /*
  488. * the inode may be getting freed (in sys_unlink path).
  489. */
  490. inode = igrab(inode);
  491. if (!wait && inode) {
  492. list_add_tail(&BTRFS_I(inode)->ordered_operations,
  493. &root->fs_info->ordered_operations);
  494. }
  495. spin_unlock(&root->fs_info->ordered_extent_lock);
  496. if (inode) {
  497. if (wait)
  498. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  499. else
  500. filemap_flush(inode->i_mapping);
  501. btrfs_add_delayed_iput(inode);
  502. }
  503. cond_resched();
  504. spin_lock(&root->fs_info->ordered_extent_lock);
  505. }
  506. if (wait && !list_empty(&root->fs_info->ordered_operations))
  507. goto again;
  508. spin_unlock(&root->fs_info->ordered_extent_lock);
  509. mutex_unlock(&root->fs_info->ordered_operations_mutex);
  510. }
  511. /*
  512. * Used to start IO or wait for a given ordered extent to finish.
  513. *
  514. * If wait is one, this effectively waits on page writeback for all the pages
  515. * in the extent, and it waits on the io completion code to insert
  516. * metadata into the btree corresponding to the extent
  517. */
  518. void btrfs_start_ordered_extent(struct inode *inode,
  519. struct btrfs_ordered_extent *entry,
  520. int wait)
  521. {
  522. u64 start = entry->file_offset;
  523. u64 end = start + entry->len - 1;
  524. trace_btrfs_ordered_extent_start(inode, entry);
  525. /*
  526. * pages in the range can be dirty, clean or writeback. We
  527. * start IO on any dirty ones so the wait doesn't stall waiting
  528. * for pdflush to find them
  529. */
  530. if (!test_bit(BTRFS_ORDERED_DIRECT, &entry->flags))
  531. filemap_fdatawrite_range(inode->i_mapping, start, end);
  532. if (wait) {
  533. wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
  534. &entry->flags));
  535. }
  536. }
  537. /*
  538. * Used to wait on ordered extents across a large range of bytes.
  539. */
  540. void btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
  541. {
  542. u64 end;
  543. u64 orig_end;
  544. struct btrfs_ordered_extent *ordered;
  545. int found;
  546. if (start + len < start) {
  547. orig_end = INT_LIMIT(loff_t);
  548. } else {
  549. orig_end = start + len - 1;
  550. if (orig_end > INT_LIMIT(loff_t))
  551. orig_end = INT_LIMIT(loff_t);
  552. }
  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. /*
  558. * So with compression we will find and lock a dirty page and clear the
  559. * first one as dirty, setup an async extent, and immediately return
  560. * with the entire range locked but with nobody actually marked with
  561. * writeback. So we can't just filemap_write_and_wait_range() and
  562. * expect it to work since it will just kick off a thread to do the
  563. * actual work. So we need to call filemap_fdatawrite_range _again_
  564. * since it will wait on the page lock, which won't be unlocked until
  565. * after the pages have been marked as writeback and so we're good to go
  566. * from there. We have to do this otherwise we'll miss the ordered
  567. * extents and that results in badness. Please Josef, do not think you
  568. * know better and pull this out at some point in the future, it is
  569. * right and you are wrong.
  570. */
  571. if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  572. &BTRFS_I(inode)->runtime_flags))
  573. filemap_fdatawrite_range(inode->i_mapping, start, orig_end);
  574. filemap_fdatawait_range(inode->i_mapping, start, orig_end);
  575. end = orig_end;
  576. found = 0;
  577. while (1) {
  578. ordered = btrfs_lookup_first_ordered_extent(inode, end);
  579. if (!ordered)
  580. break;
  581. if (ordered->file_offset > orig_end) {
  582. btrfs_put_ordered_extent(ordered);
  583. break;
  584. }
  585. if (ordered->file_offset + ordered->len < start) {
  586. btrfs_put_ordered_extent(ordered);
  587. break;
  588. }
  589. found++;
  590. btrfs_start_ordered_extent(inode, ordered, 1);
  591. end = ordered->file_offset;
  592. btrfs_put_ordered_extent(ordered);
  593. if (end == 0 || end == start)
  594. break;
  595. end--;
  596. }
  597. }
  598. /*
  599. * find an ordered extent corresponding to file_offset. return NULL if
  600. * nothing is found, otherwise take a reference on the extent and return it
  601. */
  602. struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
  603. u64 file_offset)
  604. {
  605. struct btrfs_ordered_inode_tree *tree;
  606. struct rb_node *node;
  607. struct btrfs_ordered_extent *entry = NULL;
  608. tree = &BTRFS_I(inode)->ordered_tree;
  609. spin_lock_irq(&tree->lock);
  610. node = tree_search(tree, file_offset);
  611. if (!node)
  612. goto out;
  613. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  614. if (!offset_in_entry(entry, file_offset))
  615. entry = NULL;
  616. if (entry)
  617. atomic_inc(&entry->refs);
  618. out:
  619. spin_unlock_irq(&tree->lock);
  620. return entry;
  621. }
  622. /* Since the DIO code tries to lock a wide area we need to look for any ordered
  623. * extents that exist in the range, rather than just the start of the range.
  624. */
  625. struct btrfs_ordered_extent *btrfs_lookup_ordered_range(struct inode *inode,
  626. u64 file_offset,
  627. u64 len)
  628. {
  629. struct btrfs_ordered_inode_tree *tree;
  630. struct rb_node *node;
  631. struct btrfs_ordered_extent *entry = NULL;
  632. tree = &BTRFS_I(inode)->ordered_tree;
  633. spin_lock_irq(&tree->lock);
  634. node = tree_search(tree, file_offset);
  635. if (!node) {
  636. node = tree_search(tree, file_offset + len);
  637. if (!node)
  638. goto out;
  639. }
  640. while (1) {
  641. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  642. if (range_overlaps(entry, file_offset, len))
  643. break;
  644. if (entry->file_offset >= file_offset + len) {
  645. entry = NULL;
  646. break;
  647. }
  648. entry = NULL;
  649. node = rb_next(node);
  650. if (!node)
  651. break;
  652. }
  653. out:
  654. if (entry)
  655. atomic_inc(&entry->refs);
  656. spin_unlock_irq(&tree->lock);
  657. return entry;
  658. }
  659. /*
  660. * lookup and return any extent before 'file_offset'. NULL is returned
  661. * if none is found
  662. */
  663. struct btrfs_ordered_extent *
  664. btrfs_lookup_first_ordered_extent(struct inode *inode, u64 file_offset)
  665. {
  666. struct btrfs_ordered_inode_tree *tree;
  667. struct rb_node *node;
  668. struct btrfs_ordered_extent *entry = NULL;
  669. tree = &BTRFS_I(inode)->ordered_tree;
  670. spin_lock_irq(&tree->lock);
  671. node = tree_search(tree, file_offset);
  672. if (!node)
  673. goto out;
  674. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  675. atomic_inc(&entry->refs);
  676. out:
  677. spin_unlock_irq(&tree->lock);
  678. return entry;
  679. }
  680. /*
  681. * After an extent is done, call this to conditionally update the on disk
  682. * i_size. i_size is updated to cover any fully written part of the file.
  683. */
  684. int btrfs_ordered_update_i_size(struct inode *inode, u64 offset,
  685. struct btrfs_ordered_extent *ordered)
  686. {
  687. struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
  688. u64 disk_i_size;
  689. u64 new_i_size;
  690. u64 i_size_test;
  691. u64 i_size = i_size_read(inode);
  692. struct rb_node *node;
  693. struct rb_node *prev = NULL;
  694. struct btrfs_ordered_extent *test;
  695. int ret = 1;
  696. if (ordered)
  697. offset = entry_end(ordered);
  698. else
  699. offset = ALIGN(offset, BTRFS_I(inode)->root->sectorsize);
  700. spin_lock_irq(&tree->lock);
  701. disk_i_size = BTRFS_I(inode)->disk_i_size;
  702. /* truncate file */
  703. if (disk_i_size > i_size) {
  704. BTRFS_I(inode)->disk_i_size = i_size;
  705. ret = 0;
  706. goto out;
  707. }
  708. /*
  709. * if the disk i_size is already at the inode->i_size, or
  710. * this ordered extent is inside the disk i_size, we're done
  711. */
  712. if (disk_i_size == i_size || offset <= disk_i_size) {
  713. goto out;
  714. }
  715. /*
  716. * walk backward from this ordered extent to disk_i_size.
  717. * if we find an ordered extent then we can't update disk i_size
  718. * yet
  719. */
  720. if (ordered) {
  721. node = rb_prev(&ordered->rb_node);
  722. } else {
  723. prev = tree_search(tree, offset);
  724. /*
  725. * we insert file extents without involving ordered struct,
  726. * so there should be no ordered struct cover this offset
  727. */
  728. if (prev) {
  729. test = rb_entry(prev, struct btrfs_ordered_extent,
  730. rb_node);
  731. BUG_ON(offset_in_entry(test, offset));
  732. }
  733. node = prev;
  734. }
  735. for (; node; node = rb_prev(node)) {
  736. test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  737. /* We treat this entry as if it doesnt exist */
  738. if (test_bit(BTRFS_ORDERED_UPDATED_ISIZE, &test->flags))
  739. continue;
  740. if (test->file_offset + test->len <= disk_i_size)
  741. break;
  742. if (test->file_offset >= i_size)
  743. break;
  744. if (test->file_offset >= disk_i_size)
  745. goto out;
  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. /*
  763. * We are looking for an area between our current extent and the next
  764. * ordered extent to update the i_size to. There are 3 cases here
  765. *
  766. * 1) We don't actually have anything and we can update to i_size.
  767. * 2) We have stuff but they already did their i_size update so again we
  768. * can just update to i_size.
  769. * 3) We have an outstanding ordered extent so the most we can update
  770. * our disk_i_size to is the start of the next offset.
  771. */
  772. i_size_test = i_size;
  773. for (; node; node = rb_next(node)) {
  774. test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  775. if (test_bit(BTRFS_ORDERED_UPDATED_ISIZE, &test->flags))
  776. continue;
  777. if (test->file_offset > offset) {
  778. i_size_test = test->file_offset;
  779. break;
  780. }
  781. }
  782. /*
  783. * i_size_test is the end of a region after this ordered
  784. * extent where there are no ordered extents, we can safely set
  785. * disk_i_size to this.
  786. */
  787. if (i_size_test > offset)
  788. new_i_size = min_t(u64, i_size_test, i_size);
  789. BTRFS_I(inode)->disk_i_size = new_i_size;
  790. ret = 0;
  791. out:
  792. /*
  793. * We need to do this because we can't remove ordered extents until
  794. * after the i_disk_size has been updated and then the inode has been
  795. * updated to reflect the change, so we need to tell anybody who finds
  796. * this ordered extent that we've already done all the real work, we
  797. * just haven't completed all the other work.
  798. */
  799. if (ordered)
  800. set_bit(BTRFS_ORDERED_UPDATED_ISIZE, &ordered->flags);
  801. spin_unlock_irq(&tree->lock);
  802. return ret;
  803. }
  804. /*
  805. * search the ordered extents for one corresponding to 'offset' and
  806. * try to find a checksum. This is used because we allow pages to
  807. * be reclaimed before their checksum is actually put into the btree
  808. */
  809. int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr,
  810. u32 *sum)
  811. {
  812. struct btrfs_ordered_sum *ordered_sum;
  813. struct btrfs_sector_sum *sector_sums;
  814. struct btrfs_ordered_extent *ordered;
  815. struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
  816. unsigned long num_sectors;
  817. unsigned long i;
  818. u32 sectorsize = BTRFS_I(inode)->root->sectorsize;
  819. int ret = 1;
  820. ordered = btrfs_lookup_ordered_extent(inode, offset);
  821. if (!ordered)
  822. return 1;
  823. spin_lock_irq(&tree->lock);
  824. list_for_each_entry_reverse(ordered_sum, &ordered->list, list) {
  825. if (disk_bytenr >= ordered_sum->bytenr) {
  826. num_sectors = ordered_sum->len / sectorsize;
  827. sector_sums = ordered_sum->sums;
  828. for (i = 0; i < num_sectors; i++) {
  829. if (sector_sums[i].bytenr == disk_bytenr) {
  830. *sum = sector_sums[i].sum;
  831. ret = 0;
  832. goto out;
  833. }
  834. }
  835. }
  836. }
  837. out:
  838. spin_unlock_irq(&tree->lock);
  839. btrfs_put_ordered_extent(ordered);
  840. return ret;
  841. }
  842. /*
  843. * add a given inode to the list of inodes that must be fully on
  844. * disk before a transaction commit finishes.
  845. *
  846. * This basically gives us the ext3 style data=ordered mode, and it is mostly
  847. * used to make sure renamed files are fully on disk.
  848. *
  849. * It is a noop if the inode is already fully on disk.
  850. *
  851. * If trans is not null, we'll do a friendly check for a transaction that
  852. * is already flushing things and force the IO down ourselves.
  853. */
  854. void btrfs_add_ordered_operation(struct btrfs_trans_handle *trans,
  855. struct btrfs_root *root, struct inode *inode)
  856. {
  857. u64 last_mod;
  858. last_mod = max(BTRFS_I(inode)->generation, BTRFS_I(inode)->last_trans);
  859. /*
  860. * if this file hasn't been changed since the last transaction
  861. * commit, we can safely return without doing anything
  862. */
  863. if (last_mod < root->fs_info->last_trans_committed)
  864. return;
  865. /*
  866. * the transaction is already committing. Just start the IO and
  867. * don't bother with all of this list nonsense
  868. */
  869. if (trans && root->fs_info->running_transaction->blocked) {
  870. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  871. return;
  872. }
  873. spin_lock(&root->fs_info->ordered_extent_lock);
  874. if (list_empty(&BTRFS_I(inode)->ordered_operations)) {
  875. list_add_tail(&BTRFS_I(inode)->ordered_operations,
  876. &root->fs_info->ordered_operations);
  877. }
  878. spin_unlock(&root->fs_info->ordered_extent_lock);
  879. }