ordered-data.c 31 KB

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