ordered-data.c 27 KB

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