ordered-data.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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. #include "disk-io.h"
  27. static struct kmem_cache *btrfs_ordered_extent_cache;
  28. static u64 entry_end(struct btrfs_ordered_extent *entry)
  29. {
  30. if (entry->file_offset + entry->len < entry->file_offset)
  31. return (u64)-1;
  32. return entry->file_offset + entry->len;
  33. }
  34. /* returns NULL if the insertion worked, or it returns the node it did find
  35. * in the tree
  36. */
  37. static struct rb_node *tree_insert(struct rb_root *root, u64 file_offset,
  38. struct rb_node *node)
  39. {
  40. struct rb_node **p = &root->rb_node;
  41. struct rb_node *parent = NULL;
  42. struct btrfs_ordered_extent *entry;
  43. while (*p) {
  44. parent = *p;
  45. entry = rb_entry(parent, struct btrfs_ordered_extent, rb_node);
  46. if (file_offset < entry->file_offset)
  47. p = &(*p)->rb_left;
  48. else if (file_offset >= entry_end(entry))
  49. p = &(*p)->rb_right;
  50. else
  51. return parent;
  52. }
  53. rb_link_node(node, parent, p);
  54. rb_insert_color(node, root);
  55. return NULL;
  56. }
  57. static void ordered_data_tree_panic(struct inode *inode, int errno,
  58. u64 offset)
  59. {
  60. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  61. btrfs_panic(fs_info, errno, "Inconsistency in ordered tree at offset "
  62. "%llu", offset);
  63. }
  64. /*
  65. * look for a given offset in the tree, and if it can't be found return the
  66. * first lesser offset
  67. */
  68. static struct rb_node *__tree_search(struct rb_root *root, u64 file_offset,
  69. struct rb_node **prev_ret)
  70. {
  71. struct rb_node *n = root->rb_node;
  72. struct rb_node *prev = NULL;
  73. struct rb_node *test;
  74. struct btrfs_ordered_extent *entry;
  75. struct btrfs_ordered_extent *prev_entry = NULL;
  76. while (n) {
  77. entry = rb_entry(n, struct btrfs_ordered_extent, rb_node);
  78. prev = n;
  79. prev_entry = entry;
  80. if (file_offset < entry->file_offset)
  81. n = n->rb_left;
  82. else if (file_offset >= entry_end(entry))
  83. n = n->rb_right;
  84. else
  85. return n;
  86. }
  87. if (!prev_ret)
  88. return NULL;
  89. while (prev && file_offset >= entry_end(prev_entry)) {
  90. test = rb_next(prev);
  91. if (!test)
  92. break;
  93. prev_entry = rb_entry(test, struct btrfs_ordered_extent,
  94. rb_node);
  95. if (file_offset < entry_end(prev_entry))
  96. break;
  97. prev = test;
  98. }
  99. if (prev)
  100. prev_entry = rb_entry(prev, struct btrfs_ordered_extent,
  101. rb_node);
  102. while (prev && file_offset < entry_end(prev_entry)) {
  103. test = rb_prev(prev);
  104. if (!test)
  105. break;
  106. prev_entry = rb_entry(test, struct btrfs_ordered_extent,
  107. rb_node);
  108. prev = test;
  109. }
  110. *prev_ret = prev;
  111. return NULL;
  112. }
  113. /*
  114. * helper to check if a given offset is inside a given entry
  115. */
  116. static int offset_in_entry(struct btrfs_ordered_extent *entry, u64 file_offset)
  117. {
  118. if (file_offset < entry->file_offset ||
  119. entry->file_offset + entry->len <= file_offset)
  120. return 0;
  121. return 1;
  122. }
  123. static int range_overlaps(struct btrfs_ordered_extent *entry, u64 file_offset,
  124. u64 len)
  125. {
  126. if (file_offset + len <= entry->file_offset ||
  127. entry->file_offset + entry->len <= file_offset)
  128. return 0;
  129. return 1;
  130. }
  131. /*
  132. * look find the first ordered struct that has this offset, otherwise
  133. * the first one less than this offset
  134. */
  135. static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree,
  136. u64 file_offset)
  137. {
  138. struct rb_root *root = &tree->tree;
  139. struct rb_node *prev = NULL;
  140. struct rb_node *ret;
  141. struct btrfs_ordered_extent *entry;
  142. if (tree->last) {
  143. entry = rb_entry(tree->last, struct btrfs_ordered_extent,
  144. rb_node);
  145. if (offset_in_entry(entry, file_offset))
  146. return tree->last;
  147. }
  148. ret = __tree_search(root, file_offset, &prev);
  149. if (!ret)
  150. ret = prev;
  151. if (ret)
  152. tree->last = ret;
  153. return ret;
  154. }
  155. /* allocate and add a new ordered_extent into the per-inode tree.
  156. * file_offset is the logical offset in the file
  157. *
  158. * start is the disk block number of an extent already reserved in the
  159. * extent allocation tree
  160. *
  161. * len is the length of the extent
  162. *
  163. * The tree is given a single reference on the ordered extent that was
  164. * inserted.
  165. */
  166. static int __btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
  167. u64 start, u64 len, u64 disk_len,
  168. int type, int dio, int compress_type)
  169. {
  170. struct btrfs_root *root = BTRFS_I(inode)->root;
  171. struct btrfs_ordered_inode_tree *tree;
  172. struct rb_node *node;
  173. struct btrfs_ordered_extent *entry;
  174. tree = &BTRFS_I(inode)->ordered_tree;
  175. entry = kmem_cache_zalloc(btrfs_ordered_extent_cache, GFP_NOFS);
  176. if (!entry)
  177. return -ENOMEM;
  178. entry->file_offset = file_offset;
  179. entry->start = start;
  180. entry->len = len;
  181. entry->disk_len = disk_len;
  182. entry->bytes_left = len;
  183. entry->inode = igrab(inode);
  184. entry->compress_type = compress_type;
  185. entry->truncated_len = (u64)-1;
  186. if (type != BTRFS_ORDERED_IO_DONE && type != BTRFS_ORDERED_COMPLETE)
  187. set_bit(type, &entry->flags);
  188. if (dio)
  189. set_bit(BTRFS_ORDERED_DIRECT, &entry->flags);
  190. /* one ref for the tree */
  191. atomic_set(&entry->refs, 1);
  192. init_waitqueue_head(&entry->wait);
  193. INIT_LIST_HEAD(&entry->list);
  194. INIT_LIST_HEAD(&entry->root_extent_list);
  195. INIT_LIST_HEAD(&entry->work_list);
  196. init_completion(&entry->completion);
  197. INIT_LIST_HEAD(&entry->log_list);
  198. INIT_LIST_HEAD(&entry->trans_list);
  199. trace_btrfs_ordered_extent_add(inode, entry);
  200. spin_lock_irq(&tree->lock);
  201. node = tree_insert(&tree->tree, file_offset,
  202. &entry->rb_node);
  203. if (node)
  204. ordered_data_tree_panic(inode, -EEXIST, file_offset);
  205. spin_unlock_irq(&tree->lock);
  206. spin_lock(&root->ordered_extent_lock);
  207. list_add_tail(&entry->root_extent_list,
  208. &root->ordered_extents);
  209. root->nr_ordered_extents++;
  210. if (root->nr_ordered_extents == 1) {
  211. spin_lock(&root->fs_info->ordered_root_lock);
  212. BUG_ON(!list_empty(&root->ordered_root));
  213. list_add_tail(&root->ordered_root,
  214. &root->fs_info->ordered_roots);
  215. spin_unlock(&root->fs_info->ordered_root_lock);
  216. }
  217. spin_unlock(&root->ordered_extent_lock);
  218. return 0;
  219. }
  220. int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
  221. u64 start, u64 len, u64 disk_len, int type)
  222. {
  223. return __btrfs_add_ordered_extent(inode, file_offset, start, len,
  224. disk_len, type, 0,
  225. BTRFS_COMPRESS_NONE);
  226. }
  227. int btrfs_add_ordered_extent_dio(struct inode *inode, u64 file_offset,
  228. u64 start, u64 len, u64 disk_len, int type)
  229. {
  230. return __btrfs_add_ordered_extent(inode, file_offset, start, len,
  231. disk_len, type, 1,
  232. BTRFS_COMPRESS_NONE);
  233. }
  234. int btrfs_add_ordered_extent_compress(struct inode *inode, u64 file_offset,
  235. u64 start, u64 len, u64 disk_len,
  236. int type, int compress_type)
  237. {
  238. return __btrfs_add_ordered_extent(inode, file_offset, start, len,
  239. disk_len, type, 0,
  240. compress_type);
  241. }
  242. /*
  243. * Add a struct btrfs_ordered_sum into the list of checksums to be inserted
  244. * when an ordered extent is finished. If the list covers more than one
  245. * ordered extent, it is split across multiples.
  246. */
  247. void btrfs_add_ordered_sum(struct inode *inode,
  248. struct btrfs_ordered_extent *entry,
  249. struct btrfs_ordered_sum *sum)
  250. {
  251. struct btrfs_ordered_inode_tree *tree;
  252. tree = &BTRFS_I(inode)->ordered_tree;
  253. spin_lock_irq(&tree->lock);
  254. list_add_tail(&sum->list, &entry->list);
  255. spin_unlock_irq(&tree->lock);
  256. }
  257. /*
  258. * this is used to account for finished IO across a given range
  259. * of the file. The IO may span ordered extents. If
  260. * a given ordered_extent is completely done, 1 is returned, otherwise
  261. * 0.
  262. *
  263. * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
  264. * to make sure this function only returns 1 once for a given ordered extent.
  265. *
  266. * file_offset is updated to one byte past the range that is recorded as
  267. * complete. This allows you to walk forward in the file.
  268. */
  269. int btrfs_dec_test_first_ordered_pending(struct inode *inode,
  270. struct btrfs_ordered_extent **cached,
  271. u64 *file_offset, u64 io_size, int uptodate)
  272. {
  273. struct btrfs_ordered_inode_tree *tree;
  274. struct rb_node *node;
  275. struct btrfs_ordered_extent *entry = NULL;
  276. int ret;
  277. unsigned long flags;
  278. u64 dec_end;
  279. u64 dec_start;
  280. u64 to_dec;
  281. tree = &BTRFS_I(inode)->ordered_tree;
  282. spin_lock_irqsave(&tree->lock, flags);
  283. node = tree_search(tree, *file_offset);
  284. if (!node) {
  285. ret = 1;
  286. goto out;
  287. }
  288. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  289. if (!offset_in_entry(entry, *file_offset)) {
  290. ret = 1;
  291. goto out;
  292. }
  293. dec_start = max(*file_offset, entry->file_offset);
  294. dec_end = min(*file_offset + io_size, entry->file_offset +
  295. entry->len);
  296. *file_offset = dec_end;
  297. if (dec_start > dec_end) {
  298. btrfs_crit(BTRFS_I(inode)->root->fs_info,
  299. "bad ordering dec_start %llu end %llu", dec_start, dec_end);
  300. }
  301. to_dec = dec_end - dec_start;
  302. if (to_dec > entry->bytes_left) {
  303. btrfs_crit(BTRFS_I(inode)->root->fs_info,
  304. "bad ordered accounting left %llu size %llu",
  305. entry->bytes_left, to_dec);
  306. }
  307. entry->bytes_left -= to_dec;
  308. if (!uptodate)
  309. set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
  310. if (entry->bytes_left == 0) {
  311. ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
  312. /*
  313. * Implicit memory barrier after test_and_set_bit
  314. */
  315. if (waitqueue_active(&entry->wait))
  316. wake_up(&entry->wait);
  317. } else {
  318. ret = 1;
  319. }
  320. out:
  321. if (!ret && cached && entry) {
  322. *cached = entry;
  323. atomic_inc(&entry->refs);
  324. }
  325. spin_unlock_irqrestore(&tree->lock, flags);
  326. return ret == 0;
  327. }
  328. /*
  329. * this is used to account for finished IO across a given range
  330. * of the file. The IO should not span ordered extents. If
  331. * a given ordered_extent is completely done, 1 is returned, otherwise
  332. * 0.
  333. *
  334. * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
  335. * to make sure this function only returns 1 once for a given ordered extent.
  336. */
  337. int btrfs_dec_test_ordered_pending(struct inode *inode,
  338. struct btrfs_ordered_extent **cached,
  339. u64 file_offset, u64 io_size, int uptodate)
  340. {
  341. struct btrfs_ordered_inode_tree *tree;
  342. struct rb_node *node;
  343. struct btrfs_ordered_extent *entry = NULL;
  344. unsigned long flags;
  345. int ret;
  346. tree = &BTRFS_I(inode)->ordered_tree;
  347. spin_lock_irqsave(&tree->lock, flags);
  348. if (cached && *cached) {
  349. entry = *cached;
  350. goto have_entry;
  351. }
  352. node = tree_search(tree, file_offset);
  353. if (!node) {
  354. ret = 1;
  355. goto out;
  356. }
  357. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  358. have_entry:
  359. if (!offset_in_entry(entry, file_offset)) {
  360. ret = 1;
  361. goto out;
  362. }
  363. if (io_size > entry->bytes_left) {
  364. btrfs_crit(BTRFS_I(inode)->root->fs_info,
  365. "bad ordered accounting left %llu size %llu",
  366. entry->bytes_left, io_size);
  367. }
  368. entry->bytes_left -= io_size;
  369. if (!uptodate)
  370. set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
  371. if (entry->bytes_left == 0) {
  372. ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
  373. /*
  374. * Implicit memory barrier after test_and_set_bit
  375. */
  376. if (waitqueue_active(&entry->wait))
  377. wake_up(&entry->wait);
  378. } else {
  379. ret = 1;
  380. }
  381. out:
  382. if (!ret && cached && entry) {
  383. *cached = entry;
  384. atomic_inc(&entry->refs);
  385. }
  386. spin_unlock_irqrestore(&tree->lock, flags);
  387. return ret == 0;
  388. }
  389. /* Needs to either be called under a log transaction or the log_mutex */
  390. void btrfs_get_logged_extents(struct inode *inode,
  391. struct list_head *logged_list,
  392. const loff_t start,
  393. const loff_t end)
  394. {
  395. struct btrfs_ordered_inode_tree *tree;
  396. struct btrfs_ordered_extent *ordered;
  397. struct rb_node *n;
  398. struct rb_node *prev;
  399. tree = &BTRFS_I(inode)->ordered_tree;
  400. spin_lock_irq(&tree->lock);
  401. n = __tree_search(&tree->tree, end, &prev);
  402. if (!n)
  403. n = prev;
  404. for (; n; n = rb_prev(n)) {
  405. ordered = rb_entry(n, struct btrfs_ordered_extent, rb_node);
  406. if (ordered->file_offset > end)
  407. continue;
  408. if (entry_end(ordered) <= start)
  409. break;
  410. if (test_and_set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags))
  411. continue;
  412. list_add(&ordered->log_list, logged_list);
  413. atomic_inc(&ordered->refs);
  414. }
  415. spin_unlock_irq(&tree->lock);
  416. }
  417. void btrfs_put_logged_extents(struct list_head *logged_list)
  418. {
  419. struct btrfs_ordered_extent *ordered;
  420. while (!list_empty(logged_list)) {
  421. ordered = list_first_entry(logged_list,
  422. struct btrfs_ordered_extent,
  423. log_list);
  424. list_del_init(&ordered->log_list);
  425. btrfs_put_ordered_extent(ordered);
  426. }
  427. }
  428. void btrfs_submit_logged_extents(struct list_head *logged_list,
  429. struct btrfs_root *log)
  430. {
  431. int index = log->log_transid % 2;
  432. spin_lock_irq(&log->log_extents_lock[index]);
  433. list_splice_tail(logged_list, &log->logged_list[index]);
  434. spin_unlock_irq(&log->log_extents_lock[index]);
  435. }
  436. void btrfs_wait_logged_extents(struct btrfs_trans_handle *trans,
  437. struct btrfs_root *log, u64 transid)
  438. {
  439. struct btrfs_ordered_extent *ordered;
  440. int index = transid % 2;
  441. spin_lock_irq(&log->log_extents_lock[index]);
  442. while (!list_empty(&log->logged_list[index])) {
  443. struct inode *inode;
  444. ordered = list_first_entry(&log->logged_list[index],
  445. struct btrfs_ordered_extent,
  446. log_list);
  447. list_del_init(&ordered->log_list);
  448. inode = ordered->inode;
  449. spin_unlock_irq(&log->log_extents_lock[index]);
  450. if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
  451. !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
  452. u64 start = ordered->file_offset;
  453. u64 end = ordered->file_offset + ordered->len - 1;
  454. WARN_ON(!inode);
  455. filemap_fdatawrite_range(inode->i_mapping, start, end);
  456. }
  457. wait_event(ordered->wait, test_bit(BTRFS_ORDERED_IO_DONE,
  458. &ordered->flags));
  459. /*
  460. * In order to keep us from losing our ordered extent
  461. * information when committing the transaction we have to make
  462. * sure that any logged extents are completed when we go to
  463. * commit the transaction. To do this we simply increase the
  464. * current transactions pending_ordered counter and decrement it
  465. * when the ordered extent completes.
  466. */
  467. if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
  468. struct btrfs_ordered_inode_tree *tree;
  469. tree = &BTRFS_I(inode)->ordered_tree;
  470. spin_lock_irq(&tree->lock);
  471. if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
  472. set_bit(BTRFS_ORDERED_PENDING, &ordered->flags);
  473. atomic_inc(&trans->transaction->pending_ordered);
  474. }
  475. spin_unlock_irq(&tree->lock);
  476. }
  477. btrfs_put_ordered_extent(ordered);
  478. spin_lock_irq(&log->log_extents_lock[index]);
  479. }
  480. spin_unlock_irq(&log->log_extents_lock[index]);
  481. }
  482. void btrfs_free_logged_extents(struct btrfs_root *log, u64 transid)
  483. {
  484. struct btrfs_ordered_extent *ordered;
  485. int index = transid % 2;
  486. spin_lock_irq(&log->log_extents_lock[index]);
  487. while (!list_empty(&log->logged_list[index])) {
  488. ordered = list_first_entry(&log->logged_list[index],
  489. struct btrfs_ordered_extent,
  490. log_list);
  491. list_del_init(&ordered->log_list);
  492. spin_unlock_irq(&log->log_extents_lock[index]);
  493. btrfs_put_ordered_extent(ordered);
  494. spin_lock_irq(&log->log_extents_lock[index]);
  495. }
  496. spin_unlock_irq(&log->log_extents_lock[index]);
  497. }
  498. /*
  499. * used to drop a reference on an ordered extent. This will free
  500. * the extent if the last reference is dropped
  501. */
  502. void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
  503. {
  504. struct list_head *cur;
  505. struct btrfs_ordered_sum *sum;
  506. trace_btrfs_ordered_extent_put(entry->inode, entry);
  507. if (atomic_dec_and_test(&entry->refs)) {
  508. ASSERT(list_empty(&entry->log_list));
  509. ASSERT(list_empty(&entry->trans_list));
  510. ASSERT(list_empty(&entry->root_extent_list));
  511. ASSERT(RB_EMPTY_NODE(&entry->rb_node));
  512. if (entry->inode)
  513. btrfs_add_delayed_iput(entry->inode);
  514. while (!list_empty(&entry->list)) {
  515. cur = entry->list.next;
  516. sum = list_entry(cur, struct btrfs_ordered_sum, list);
  517. list_del(&sum->list);
  518. kfree(sum);
  519. }
  520. kmem_cache_free(btrfs_ordered_extent_cache, entry);
  521. }
  522. }
  523. /*
  524. * remove an ordered extent from the tree. No references are dropped
  525. * and waiters are woken up.
  526. */
  527. void btrfs_remove_ordered_extent(struct inode *inode,
  528. struct btrfs_ordered_extent *entry)
  529. {
  530. struct btrfs_ordered_inode_tree *tree;
  531. struct btrfs_root *root = BTRFS_I(inode)->root;
  532. struct rb_node *node;
  533. bool dec_pending_ordered = false;
  534. tree = &BTRFS_I(inode)->ordered_tree;
  535. spin_lock_irq(&tree->lock);
  536. node = &entry->rb_node;
  537. rb_erase(node, &tree->tree);
  538. RB_CLEAR_NODE(node);
  539. if (tree->last == node)
  540. tree->last = NULL;
  541. set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
  542. if (test_and_clear_bit(BTRFS_ORDERED_PENDING, &entry->flags))
  543. dec_pending_ordered = true;
  544. spin_unlock_irq(&tree->lock);
  545. /*
  546. * The current running transaction is waiting on us, we need to let it
  547. * know that we're complete and wake it up.
  548. */
  549. if (dec_pending_ordered) {
  550. struct btrfs_transaction *trans;
  551. /*
  552. * The checks for trans are just a formality, it should be set,
  553. * but if it isn't we don't want to deref/assert under the spin
  554. * lock, so be nice and check if trans is set, but ASSERT() so
  555. * if it isn't set a developer will notice.
  556. */
  557. spin_lock(&root->fs_info->trans_lock);
  558. trans = root->fs_info->running_transaction;
  559. if (trans)
  560. atomic_inc(&trans->use_count);
  561. spin_unlock(&root->fs_info->trans_lock);
  562. ASSERT(trans);
  563. if (trans) {
  564. if (atomic_dec_and_test(&trans->pending_ordered))
  565. wake_up(&trans->pending_wait);
  566. btrfs_put_transaction(trans);
  567. }
  568. }
  569. spin_lock(&root->ordered_extent_lock);
  570. list_del_init(&entry->root_extent_list);
  571. root->nr_ordered_extents--;
  572. trace_btrfs_ordered_extent_remove(inode, entry);
  573. if (!root->nr_ordered_extents) {
  574. spin_lock(&root->fs_info->ordered_root_lock);
  575. BUG_ON(list_empty(&root->ordered_root));
  576. list_del_init(&root->ordered_root);
  577. spin_unlock(&root->fs_info->ordered_root_lock);
  578. }
  579. spin_unlock(&root->ordered_extent_lock);
  580. wake_up(&entry->wait);
  581. }
  582. static void btrfs_run_ordered_extent_work(struct btrfs_work *work)
  583. {
  584. struct btrfs_ordered_extent *ordered;
  585. ordered = container_of(work, struct btrfs_ordered_extent, flush_work);
  586. btrfs_start_ordered_extent(ordered->inode, ordered, 1);
  587. complete(&ordered->completion);
  588. }
  589. /*
  590. * wait for all the ordered extents in a root. This is done when balancing
  591. * space between drives.
  592. */
  593. int btrfs_wait_ordered_extents(struct btrfs_root *root, int nr)
  594. {
  595. struct list_head splice, works;
  596. struct btrfs_ordered_extent *ordered, *next;
  597. int count = 0;
  598. INIT_LIST_HEAD(&splice);
  599. INIT_LIST_HEAD(&works);
  600. mutex_lock(&root->ordered_extent_mutex);
  601. spin_lock(&root->ordered_extent_lock);
  602. list_splice_init(&root->ordered_extents, &splice);
  603. while (!list_empty(&splice) && nr) {
  604. ordered = list_first_entry(&splice, struct btrfs_ordered_extent,
  605. root_extent_list);
  606. list_move_tail(&ordered->root_extent_list,
  607. &root->ordered_extents);
  608. atomic_inc(&ordered->refs);
  609. spin_unlock(&root->ordered_extent_lock);
  610. btrfs_init_work(&ordered->flush_work,
  611. btrfs_flush_delalloc_helper,
  612. btrfs_run_ordered_extent_work, NULL, NULL);
  613. list_add_tail(&ordered->work_list, &works);
  614. btrfs_queue_work(root->fs_info->flush_workers,
  615. &ordered->flush_work);
  616. cond_resched();
  617. spin_lock(&root->ordered_extent_lock);
  618. if (nr != -1)
  619. nr--;
  620. count++;
  621. }
  622. list_splice_tail(&splice, &root->ordered_extents);
  623. spin_unlock(&root->ordered_extent_lock);
  624. list_for_each_entry_safe(ordered, next, &works, work_list) {
  625. list_del_init(&ordered->work_list);
  626. wait_for_completion(&ordered->completion);
  627. btrfs_put_ordered_extent(ordered);
  628. cond_resched();
  629. }
  630. mutex_unlock(&root->ordered_extent_mutex);
  631. return count;
  632. }
  633. void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, int nr)
  634. {
  635. struct btrfs_root *root;
  636. struct list_head splice;
  637. int done;
  638. INIT_LIST_HEAD(&splice);
  639. mutex_lock(&fs_info->ordered_operations_mutex);
  640. spin_lock(&fs_info->ordered_root_lock);
  641. list_splice_init(&fs_info->ordered_roots, &splice);
  642. while (!list_empty(&splice) && nr) {
  643. root = list_first_entry(&splice, struct btrfs_root,
  644. ordered_root);
  645. root = btrfs_grab_fs_root(root);
  646. BUG_ON(!root);
  647. list_move_tail(&root->ordered_root,
  648. &fs_info->ordered_roots);
  649. spin_unlock(&fs_info->ordered_root_lock);
  650. done = btrfs_wait_ordered_extents(root, nr);
  651. btrfs_put_fs_root(root);
  652. spin_lock(&fs_info->ordered_root_lock);
  653. if (nr != -1) {
  654. nr -= done;
  655. WARN_ON(nr < 0);
  656. }
  657. }
  658. list_splice_tail(&splice, &fs_info->ordered_roots);
  659. spin_unlock(&fs_info->ordered_root_lock);
  660. mutex_unlock(&fs_info->ordered_operations_mutex);
  661. }
  662. /*
  663. * Used to start IO or wait for a given ordered extent to finish.
  664. *
  665. * If wait is one, this effectively waits on page writeback for all the pages
  666. * in the extent, and it waits on the io completion code to insert
  667. * metadata into the btree corresponding to the extent
  668. */
  669. void btrfs_start_ordered_extent(struct inode *inode,
  670. struct btrfs_ordered_extent *entry,
  671. int wait)
  672. {
  673. u64 start = entry->file_offset;
  674. u64 end = start + entry->len - 1;
  675. trace_btrfs_ordered_extent_start(inode, entry);
  676. /*
  677. * pages in the range can be dirty, clean or writeback. We
  678. * start IO on any dirty ones so the wait doesn't stall waiting
  679. * for the flusher thread to find them
  680. */
  681. if (!test_bit(BTRFS_ORDERED_DIRECT, &entry->flags))
  682. filemap_fdatawrite_range(inode->i_mapping, start, end);
  683. if (wait) {
  684. wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
  685. &entry->flags));
  686. }
  687. }
  688. /*
  689. * Used to wait on ordered extents across a large range of bytes.
  690. */
  691. int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
  692. {
  693. int ret = 0;
  694. int ret_wb = 0;
  695. u64 end;
  696. u64 orig_end;
  697. struct btrfs_ordered_extent *ordered;
  698. if (start + len < start) {
  699. orig_end = INT_LIMIT(loff_t);
  700. } else {
  701. orig_end = start + len - 1;
  702. if (orig_end > INT_LIMIT(loff_t))
  703. orig_end = INT_LIMIT(loff_t);
  704. }
  705. /* start IO across the range first to instantiate any delalloc
  706. * extents
  707. */
  708. ret = btrfs_fdatawrite_range(inode, start, orig_end);
  709. if (ret)
  710. return ret;
  711. /*
  712. * If we have a writeback error don't return immediately. Wait first
  713. * for any ordered extents that haven't completed yet. This is to make
  714. * sure no one can dirty the same page ranges and call writepages()
  715. * before the ordered extents complete - to avoid failures (-EEXIST)
  716. * when adding the new ordered extents to the ordered tree.
  717. */
  718. ret_wb = filemap_fdatawait_range(inode->i_mapping, start, orig_end);
  719. end = orig_end;
  720. while (1) {
  721. ordered = btrfs_lookup_first_ordered_extent(inode, end);
  722. if (!ordered)
  723. break;
  724. if (ordered->file_offset > orig_end) {
  725. btrfs_put_ordered_extent(ordered);
  726. break;
  727. }
  728. if (ordered->file_offset + ordered->len <= start) {
  729. btrfs_put_ordered_extent(ordered);
  730. break;
  731. }
  732. btrfs_start_ordered_extent(inode, ordered, 1);
  733. end = ordered->file_offset;
  734. if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags))
  735. ret = -EIO;
  736. btrfs_put_ordered_extent(ordered);
  737. if (ret || end == 0 || end == start)
  738. break;
  739. end--;
  740. }
  741. return ret_wb ? ret_wb : ret;
  742. }
  743. /*
  744. * find an ordered extent corresponding to file_offset. return NULL if
  745. * nothing is found, otherwise take a reference on the extent and return it
  746. */
  747. struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
  748. u64 file_offset)
  749. {
  750. struct btrfs_ordered_inode_tree *tree;
  751. struct rb_node *node;
  752. struct btrfs_ordered_extent *entry = NULL;
  753. tree = &BTRFS_I(inode)->ordered_tree;
  754. spin_lock_irq(&tree->lock);
  755. node = tree_search(tree, file_offset);
  756. if (!node)
  757. goto out;
  758. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  759. if (!offset_in_entry(entry, file_offset))
  760. entry = NULL;
  761. if (entry)
  762. atomic_inc(&entry->refs);
  763. out:
  764. spin_unlock_irq(&tree->lock);
  765. return entry;
  766. }
  767. /* Since the DIO code tries to lock a wide area we need to look for any ordered
  768. * extents that exist in the range, rather than just the start of the range.
  769. */
  770. struct btrfs_ordered_extent *btrfs_lookup_ordered_range(struct inode *inode,
  771. u64 file_offset,
  772. u64 len)
  773. {
  774. struct btrfs_ordered_inode_tree *tree;
  775. struct rb_node *node;
  776. struct btrfs_ordered_extent *entry = NULL;
  777. tree = &BTRFS_I(inode)->ordered_tree;
  778. spin_lock_irq(&tree->lock);
  779. node = tree_search(tree, file_offset);
  780. if (!node) {
  781. node = tree_search(tree, file_offset + len);
  782. if (!node)
  783. goto out;
  784. }
  785. while (1) {
  786. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  787. if (range_overlaps(entry, file_offset, len))
  788. break;
  789. if (entry->file_offset >= file_offset + len) {
  790. entry = NULL;
  791. break;
  792. }
  793. entry = NULL;
  794. node = rb_next(node);
  795. if (!node)
  796. break;
  797. }
  798. out:
  799. if (entry)
  800. atomic_inc(&entry->refs);
  801. spin_unlock_irq(&tree->lock);
  802. return entry;
  803. }
  804. bool btrfs_have_ordered_extents_in_range(struct inode *inode,
  805. u64 file_offset,
  806. u64 len)
  807. {
  808. struct btrfs_ordered_extent *oe;
  809. oe = btrfs_lookup_ordered_range(inode, file_offset, len);
  810. if (oe) {
  811. btrfs_put_ordered_extent(oe);
  812. return true;
  813. }
  814. return false;
  815. }
  816. /*
  817. * lookup and return any extent before 'file_offset'. NULL is returned
  818. * if none is found
  819. */
  820. struct btrfs_ordered_extent *
  821. btrfs_lookup_first_ordered_extent(struct inode *inode, u64 file_offset)
  822. {
  823. struct btrfs_ordered_inode_tree *tree;
  824. struct rb_node *node;
  825. struct btrfs_ordered_extent *entry = NULL;
  826. tree = &BTRFS_I(inode)->ordered_tree;
  827. spin_lock_irq(&tree->lock);
  828. node = tree_search(tree, file_offset);
  829. if (!node)
  830. goto out;
  831. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  832. atomic_inc(&entry->refs);
  833. out:
  834. spin_unlock_irq(&tree->lock);
  835. return entry;
  836. }
  837. /*
  838. * After an extent is done, call this to conditionally update the on disk
  839. * i_size. i_size is updated to cover any fully written part of the file.
  840. */
  841. int btrfs_ordered_update_i_size(struct inode *inode, u64 offset,
  842. struct btrfs_ordered_extent *ordered)
  843. {
  844. struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
  845. u64 disk_i_size;
  846. u64 new_i_size;
  847. u64 i_size = i_size_read(inode);
  848. struct rb_node *node;
  849. struct rb_node *prev = NULL;
  850. struct btrfs_ordered_extent *test;
  851. int ret = 1;
  852. spin_lock_irq(&tree->lock);
  853. if (ordered) {
  854. offset = entry_end(ordered);
  855. if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags))
  856. offset = min(offset,
  857. ordered->file_offset +
  858. ordered->truncated_len);
  859. } else {
  860. offset = ALIGN(offset, BTRFS_I(inode)->root->sectorsize);
  861. }
  862. disk_i_size = BTRFS_I(inode)->disk_i_size;
  863. /* truncate file */
  864. if (disk_i_size > i_size) {
  865. BTRFS_I(inode)->disk_i_size = i_size;
  866. ret = 0;
  867. goto out;
  868. }
  869. /*
  870. * if the disk i_size is already at the inode->i_size, or
  871. * this ordered extent is inside the disk i_size, we're done
  872. */
  873. if (disk_i_size == i_size)
  874. goto out;
  875. /*
  876. * We still need to update disk_i_size if outstanding_isize is greater
  877. * than disk_i_size.
  878. */
  879. if (offset <= disk_i_size &&
  880. (!ordered || ordered->outstanding_isize <= disk_i_size))
  881. goto out;
  882. /*
  883. * walk backward from this ordered extent to disk_i_size.
  884. * if we find an ordered extent then we can't update disk i_size
  885. * yet
  886. */
  887. if (ordered) {
  888. node = rb_prev(&ordered->rb_node);
  889. } else {
  890. prev = tree_search(tree, offset);
  891. /*
  892. * we insert file extents without involving ordered struct,
  893. * so there should be no ordered struct cover this offset
  894. */
  895. if (prev) {
  896. test = rb_entry(prev, struct btrfs_ordered_extent,
  897. rb_node);
  898. BUG_ON(offset_in_entry(test, offset));
  899. }
  900. node = prev;
  901. }
  902. for (; node; node = rb_prev(node)) {
  903. test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  904. /* We treat this entry as if it doesnt exist */
  905. if (test_bit(BTRFS_ORDERED_UPDATED_ISIZE, &test->flags))
  906. continue;
  907. if (test->file_offset + test->len <= disk_i_size)
  908. break;
  909. if (test->file_offset >= i_size)
  910. break;
  911. if (entry_end(test) > disk_i_size) {
  912. /*
  913. * we don't update disk_i_size now, so record this
  914. * undealt i_size. Or we will not know the real
  915. * i_size.
  916. */
  917. if (test->outstanding_isize < offset)
  918. test->outstanding_isize = offset;
  919. if (ordered &&
  920. ordered->outstanding_isize >
  921. test->outstanding_isize)
  922. test->outstanding_isize =
  923. ordered->outstanding_isize;
  924. goto out;
  925. }
  926. }
  927. new_i_size = min_t(u64, offset, i_size);
  928. /*
  929. * Some ordered extents may completed before the current one, and
  930. * we hold the real i_size in ->outstanding_isize.
  931. */
  932. if (ordered && ordered->outstanding_isize > new_i_size)
  933. new_i_size = min_t(u64, ordered->outstanding_isize, i_size);
  934. BTRFS_I(inode)->disk_i_size = new_i_size;
  935. ret = 0;
  936. out:
  937. /*
  938. * We need to do this because we can't remove ordered extents until
  939. * after the i_disk_size has been updated and then the inode has been
  940. * updated to reflect the change, so we need to tell anybody who finds
  941. * this ordered extent that we've already done all the real work, we
  942. * just haven't completed all the other work.
  943. */
  944. if (ordered)
  945. set_bit(BTRFS_ORDERED_UPDATED_ISIZE, &ordered->flags);
  946. spin_unlock_irq(&tree->lock);
  947. return ret;
  948. }
  949. /*
  950. * search the ordered extents for one corresponding to 'offset' and
  951. * try to find a checksum. This is used because we allow pages to
  952. * be reclaimed before their checksum is actually put into the btree
  953. */
  954. int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr,
  955. u32 *sum, int len)
  956. {
  957. struct btrfs_ordered_sum *ordered_sum;
  958. struct btrfs_ordered_extent *ordered;
  959. struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
  960. unsigned long num_sectors;
  961. unsigned long i;
  962. u32 sectorsize = BTRFS_I(inode)->root->sectorsize;
  963. int index = 0;
  964. ordered = btrfs_lookup_ordered_extent(inode, offset);
  965. if (!ordered)
  966. return 0;
  967. spin_lock_irq(&tree->lock);
  968. list_for_each_entry_reverse(ordered_sum, &ordered->list, list) {
  969. if (disk_bytenr >= ordered_sum->bytenr &&
  970. disk_bytenr < ordered_sum->bytenr + ordered_sum->len) {
  971. i = (disk_bytenr - ordered_sum->bytenr) >>
  972. inode->i_sb->s_blocksize_bits;
  973. num_sectors = ordered_sum->len >>
  974. inode->i_sb->s_blocksize_bits;
  975. num_sectors = min_t(int, len - index, num_sectors - i);
  976. memcpy(sum + index, ordered_sum->sums + i,
  977. num_sectors);
  978. index += (int)num_sectors;
  979. if (index == len)
  980. goto out;
  981. disk_bytenr += num_sectors * sectorsize;
  982. }
  983. }
  984. out:
  985. spin_unlock_irq(&tree->lock);
  986. btrfs_put_ordered_extent(ordered);
  987. return index;
  988. }
  989. int __init ordered_data_init(void)
  990. {
  991. btrfs_ordered_extent_cache = kmem_cache_create("btrfs_ordered_extent",
  992. sizeof(struct btrfs_ordered_extent), 0,
  993. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
  994. NULL);
  995. if (!btrfs_ordered_extent_cache)
  996. return -ENOMEM;
  997. return 0;
  998. }
  999. void ordered_data_exit(void)
  1000. {
  1001. if (btrfs_ordered_extent_cache)
  1002. kmem_cache_destroy(btrfs_ordered_extent_cache);
  1003. }