ordered-data.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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\n", 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. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) &&
  182. !(type == BTRFS_ORDERED_NOCOW))
  183. entry->csum_bytes_left = disk_len;
  184. entry->disk_len = disk_len;
  185. entry->bytes_left = len;
  186. entry->inode = igrab(inode);
  187. entry->compress_type = compress_type;
  188. entry->truncated_len = (u64)-1;
  189. if (type != BTRFS_ORDERED_IO_DONE && type != BTRFS_ORDERED_COMPLETE)
  190. set_bit(type, &entry->flags);
  191. if (dio)
  192. set_bit(BTRFS_ORDERED_DIRECT, &entry->flags);
  193. /* one ref for the tree */
  194. atomic_set(&entry->refs, 1);
  195. init_waitqueue_head(&entry->wait);
  196. INIT_LIST_HEAD(&entry->list);
  197. INIT_LIST_HEAD(&entry->root_extent_list);
  198. INIT_LIST_HEAD(&entry->work_list);
  199. init_completion(&entry->completion);
  200. INIT_LIST_HEAD(&entry->log_list);
  201. trace_btrfs_ordered_extent_add(inode, entry);
  202. spin_lock_irq(&tree->lock);
  203. node = tree_insert(&tree->tree, file_offset,
  204. &entry->rb_node);
  205. if (node)
  206. ordered_data_tree_panic(inode, -EEXIST, file_offset);
  207. spin_unlock_irq(&tree->lock);
  208. spin_lock(&root->ordered_extent_lock);
  209. list_add_tail(&entry->root_extent_list,
  210. &root->ordered_extents);
  211. root->nr_ordered_extents++;
  212. if (root->nr_ordered_extents == 1) {
  213. spin_lock(&root->fs_info->ordered_root_lock);
  214. BUG_ON(!list_empty(&root->ordered_root));
  215. list_add_tail(&root->ordered_root,
  216. &root->fs_info->ordered_roots);
  217. spin_unlock(&root->fs_info->ordered_root_lock);
  218. }
  219. spin_unlock(&root->ordered_extent_lock);
  220. return 0;
  221. }
  222. int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
  223. u64 start, u64 len, u64 disk_len, int type)
  224. {
  225. return __btrfs_add_ordered_extent(inode, file_offset, start, len,
  226. disk_len, type, 0,
  227. BTRFS_COMPRESS_NONE);
  228. }
  229. int btrfs_add_ordered_extent_dio(struct inode *inode, u64 file_offset,
  230. u64 start, u64 len, u64 disk_len, int type)
  231. {
  232. return __btrfs_add_ordered_extent(inode, file_offset, start, len,
  233. disk_len, type, 1,
  234. BTRFS_COMPRESS_NONE);
  235. }
  236. int btrfs_add_ordered_extent_compress(struct inode *inode, u64 file_offset,
  237. u64 start, u64 len, u64 disk_len,
  238. int type, int compress_type)
  239. {
  240. return __btrfs_add_ordered_extent(inode, file_offset, start, len,
  241. disk_len, type, 0,
  242. compress_type);
  243. }
  244. /*
  245. * Add a struct btrfs_ordered_sum into the list of checksums to be inserted
  246. * when an ordered extent is finished. If the list covers more than one
  247. * ordered extent, it is split across multiples.
  248. */
  249. void btrfs_add_ordered_sum(struct inode *inode,
  250. struct btrfs_ordered_extent *entry,
  251. struct btrfs_ordered_sum *sum)
  252. {
  253. struct btrfs_ordered_inode_tree *tree;
  254. tree = &BTRFS_I(inode)->ordered_tree;
  255. spin_lock_irq(&tree->lock);
  256. list_add_tail(&sum->list, &entry->list);
  257. WARN_ON(entry->csum_bytes_left < sum->len);
  258. entry->csum_bytes_left -= sum->len;
  259. if (entry->csum_bytes_left == 0)
  260. wake_up(&entry->wait);
  261. spin_unlock_irq(&tree->lock);
  262. }
  263. /*
  264. * this is used to account for finished IO across a given range
  265. * of the file. The IO may span ordered extents. If
  266. * a given ordered_extent is completely done, 1 is returned, otherwise
  267. * 0.
  268. *
  269. * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
  270. * to make sure this function only returns 1 once for a given ordered extent.
  271. *
  272. * file_offset is updated to one byte past the range that is recorded as
  273. * complete. This allows you to walk forward in the file.
  274. */
  275. int btrfs_dec_test_first_ordered_pending(struct inode *inode,
  276. struct btrfs_ordered_extent **cached,
  277. u64 *file_offset, u64 io_size, int uptodate)
  278. {
  279. struct btrfs_ordered_inode_tree *tree;
  280. struct rb_node *node;
  281. struct btrfs_ordered_extent *entry = NULL;
  282. int ret;
  283. unsigned long flags;
  284. u64 dec_end;
  285. u64 dec_start;
  286. u64 to_dec;
  287. tree = &BTRFS_I(inode)->ordered_tree;
  288. spin_lock_irqsave(&tree->lock, flags);
  289. node = tree_search(tree, *file_offset);
  290. if (!node) {
  291. ret = 1;
  292. goto out;
  293. }
  294. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  295. if (!offset_in_entry(entry, *file_offset)) {
  296. ret = 1;
  297. goto out;
  298. }
  299. dec_start = max(*file_offset, entry->file_offset);
  300. dec_end = min(*file_offset + io_size, entry->file_offset +
  301. entry->len);
  302. *file_offset = dec_end;
  303. if (dec_start > dec_end) {
  304. btrfs_crit(BTRFS_I(inode)->root->fs_info,
  305. "bad ordering dec_start %llu end %llu", dec_start, dec_end);
  306. }
  307. to_dec = dec_end - dec_start;
  308. if (to_dec > entry->bytes_left) {
  309. btrfs_crit(BTRFS_I(inode)->root->fs_info,
  310. "bad ordered accounting left %llu size %llu",
  311. entry->bytes_left, to_dec);
  312. }
  313. entry->bytes_left -= to_dec;
  314. if (!uptodate)
  315. set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
  316. if (entry->bytes_left == 0) {
  317. ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
  318. if (waitqueue_active(&entry->wait))
  319. wake_up(&entry->wait);
  320. } else {
  321. ret = 1;
  322. }
  323. out:
  324. if (!ret && cached && entry) {
  325. *cached = entry;
  326. atomic_inc(&entry->refs);
  327. }
  328. spin_unlock_irqrestore(&tree->lock, flags);
  329. return ret == 0;
  330. }
  331. /*
  332. * this is used to account for finished IO across a given range
  333. * of the file. The IO should not span ordered extents. If
  334. * a given ordered_extent is completely done, 1 is returned, otherwise
  335. * 0.
  336. *
  337. * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
  338. * to make sure this function only returns 1 once for a given ordered extent.
  339. */
  340. int btrfs_dec_test_ordered_pending(struct inode *inode,
  341. struct btrfs_ordered_extent **cached,
  342. u64 file_offset, u64 io_size, int uptodate)
  343. {
  344. struct btrfs_ordered_inode_tree *tree;
  345. struct rb_node *node;
  346. struct btrfs_ordered_extent *entry = NULL;
  347. unsigned long flags;
  348. int ret;
  349. tree = &BTRFS_I(inode)->ordered_tree;
  350. spin_lock_irqsave(&tree->lock, flags);
  351. if (cached && *cached) {
  352. entry = *cached;
  353. goto have_entry;
  354. }
  355. node = tree_search(tree, file_offset);
  356. if (!node) {
  357. ret = 1;
  358. goto out;
  359. }
  360. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  361. have_entry:
  362. if (!offset_in_entry(entry, file_offset)) {
  363. ret = 1;
  364. goto out;
  365. }
  366. if (io_size > entry->bytes_left) {
  367. btrfs_crit(BTRFS_I(inode)->root->fs_info,
  368. "bad ordered accounting left %llu size %llu",
  369. entry->bytes_left, io_size);
  370. }
  371. entry->bytes_left -= io_size;
  372. if (!uptodate)
  373. set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
  374. if (entry->bytes_left == 0) {
  375. ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
  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. {
  393. struct btrfs_ordered_inode_tree *tree;
  394. struct btrfs_ordered_extent *ordered;
  395. struct rb_node *n;
  396. tree = &BTRFS_I(inode)->ordered_tree;
  397. spin_lock_irq(&tree->lock);
  398. for (n = rb_first(&tree->tree); n; n = rb_next(n)) {
  399. ordered = rb_entry(n, struct btrfs_ordered_extent, rb_node);
  400. if (!list_empty(&ordered->log_list))
  401. continue;
  402. list_add_tail(&ordered->log_list, logged_list);
  403. atomic_inc(&ordered->refs);
  404. }
  405. spin_unlock_irq(&tree->lock);
  406. }
  407. void btrfs_put_logged_extents(struct list_head *logged_list)
  408. {
  409. struct btrfs_ordered_extent *ordered;
  410. while (!list_empty(logged_list)) {
  411. ordered = list_first_entry(logged_list,
  412. struct btrfs_ordered_extent,
  413. log_list);
  414. list_del_init(&ordered->log_list);
  415. btrfs_put_ordered_extent(ordered);
  416. }
  417. }
  418. void btrfs_submit_logged_extents(struct list_head *logged_list,
  419. struct btrfs_root *log)
  420. {
  421. int index = log->log_transid % 2;
  422. spin_lock_irq(&log->log_extents_lock[index]);
  423. list_splice_tail(logged_list, &log->logged_list[index]);
  424. spin_unlock_irq(&log->log_extents_lock[index]);
  425. }
  426. void btrfs_wait_logged_extents(struct btrfs_root *log, u64 transid)
  427. {
  428. struct btrfs_ordered_extent *ordered;
  429. int index = transid % 2;
  430. spin_lock_irq(&log->log_extents_lock[index]);
  431. while (!list_empty(&log->logged_list[index])) {
  432. ordered = list_first_entry(&log->logged_list[index],
  433. struct btrfs_ordered_extent,
  434. log_list);
  435. list_del_init(&ordered->log_list);
  436. spin_unlock_irq(&log->log_extents_lock[index]);
  437. wait_event(ordered->wait, test_bit(BTRFS_ORDERED_IO_DONE,
  438. &ordered->flags));
  439. btrfs_put_ordered_extent(ordered);
  440. spin_lock_irq(&log->log_extents_lock[index]);
  441. }
  442. spin_unlock_irq(&log->log_extents_lock[index]);
  443. }
  444. void btrfs_free_logged_extents(struct btrfs_root *log, u64 transid)
  445. {
  446. struct btrfs_ordered_extent *ordered;
  447. int index = transid % 2;
  448. spin_lock_irq(&log->log_extents_lock[index]);
  449. while (!list_empty(&log->logged_list[index])) {
  450. ordered = list_first_entry(&log->logged_list[index],
  451. struct btrfs_ordered_extent,
  452. log_list);
  453. list_del_init(&ordered->log_list);
  454. spin_unlock_irq(&log->log_extents_lock[index]);
  455. btrfs_put_ordered_extent(ordered);
  456. spin_lock_irq(&log->log_extents_lock[index]);
  457. }
  458. spin_unlock_irq(&log->log_extents_lock[index]);
  459. }
  460. /*
  461. * used to drop a reference on an ordered extent. This will free
  462. * the extent if the last reference is dropped
  463. */
  464. void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
  465. {
  466. struct list_head *cur;
  467. struct btrfs_ordered_sum *sum;
  468. trace_btrfs_ordered_extent_put(entry->inode, entry);
  469. if (atomic_dec_and_test(&entry->refs)) {
  470. if (entry->inode)
  471. btrfs_add_delayed_iput(entry->inode);
  472. while (!list_empty(&entry->list)) {
  473. cur = entry->list.next;
  474. sum = list_entry(cur, struct btrfs_ordered_sum, list);
  475. list_del(&sum->list);
  476. kfree(sum);
  477. }
  478. kmem_cache_free(btrfs_ordered_extent_cache, entry);
  479. }
  480. }
  481. /*
  482. * remove an ordered extent from the tree. No references are dropped
  483. * and waiters are woken up.
  484. */
  485. void btrfs_remove_ordered_extent(struct inode *inode,
  486. struct btrfs_ordered_extent *entry)
  487. {
  488. struct btrfs_ordered_inode_tree *tree;
  489. struct btrfs_root *root = BTRFS_I(inode)->root;
  490. struct rb_node *node;
  491. tree = &BTRFS_I(inode)->ordered_tree;
  492. spin_lock_irq(&tree->lock);
  493. node = &entry->rb_node;
  494. rb_erase(node, &tree->tree);
  495. if (tree->last == node)
  496. tree->last = NULL;
  497. set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
  498. spin_unlock_irq(&tree->lock);
  499. spin_lock(&root->ordered_extent_lock);
  500. list_del_init(&entry->root_extent_list);
  501. root->nr_ordered_extents--;
  502. trace_btrfs_ordered_extent_remove(inode, entry);
  503. /*
  504. * we have no more ordered extents for this inode and
  505. * no dirty pages. We can safely remove it from the
  506. * list of ordered extents
  507. */
  508. if (RB_EMPTY_ROOT(&tree->tree) &&
  509. !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) {
  510. spin_lock(&root->fs_info->ordered_root_lock);
  511. list_del_init(&BTRFS_I(inode)->ordered_operations);
  512. spin_unlock(&root->fs_info->ordered_root_lock);
  513. }
  514. if (!root->nr_ordered_extents) {
  515. spin_lock(&root->fs_info->ordered_root_lock);
  516. BUG_ON(list_empty(&root->ordered_root));
  517. list_del_init(&root->ordered_root);
  518. spin_unlock(&root->fs_info->ordered_root_lock);
  519. }
  520. spin_unlock(&root->ordered_extent_lock);
  521. wake_up(&entry->wait);
  522. }
  523. static void btrfs_run_ordered_extent_work(struct btrfs_work *work)
  524. {
  525. struct btrfs_ordered_extent *ordered;
  526. ordered = container_of(work, struct btrfs_ordered_extent, flush_work);
  527. btrfs_start_ordered_extent(ordered->inode, ordered, 1);
  528. complete(&ordered->completion);
  529. }
  530. /*
  531. * wait for all the ordered extents in a root. This is done when balancing
  532. * space between drives.
  533. */
  534. int btrfs_wait_ordered_extents(struct btrfs_root *root, int nr)
  535. {
  536. struct list_head splice, works;
  537. struct btrfs_ordered_extent *ordered, *next;
  538. int count = 0;
  539. INIT_LIST_HEAD(&splice);
  540. INIT_LIST_HEAD(&works);
  541. mutex_lock(&root->ordered_extent_mutex);
  542. spin_lock(&root->ordered_extent_lock);
  543. list_splice_init(&root->ordered_extents, &splice);
  544. while (!list_empty(&splice) && nr) {
  545. ordered = list_first_entry(&splice, struct btrfs_ordered_extent,
  546. root_extent_list);
  547. list_move_tail(&ordered->root_extent_list,
  548. &root->ordered_extents);
  549. atomic_inc(&ordered->refs);
  550. spin_unlock(&root->ordered_extent_lock);
  551. btrfs_init_work(&ordered->flush_work,
  552. btrfs_run_ordered_extent_work, NULL, NULL);
  553. list_add_tail(&ordered->work_list, &works);
  554. btrfs_queue_work(root->fs_info->flush_workers,
  555. &ordered->flush_work);
  556. cond_resched();
  557. spin_lock(&root->ordered_extent_lock);
  558. if (nr != -1)
  559. nr--;
  560. count++;
  561. }
  562. list_splice_tail(&splice, &root->ordered_extents);
  563. spin_unlock(&root->ordered_extent_lock);
  564. list_for_each_entry_safe(ordered, next, &works, work_list) {
  565. list_del_init(&ordered->work_list);
  566. wait_for_completion(&ordered->completion);
  567. btrfs_put_ordered_extent(ordered);
  568. cond_resched();
  569. }
  570. mutex_unlock(&root->ordered_extent_mutex);
  571. return count;
  572. }
  573. void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, int nr)
  574. {
  575. struct btrfs_root *root;
  576. struct list_head splice;
  577. int done;
  578. INIT_LIST_HEAD(&splice);
  579. mutex_lock(&fs_info->ordered_operations_mutex);
  580. spin_lock(&fs_info->ordered_root_lock);
  581. list_splice_init(&fs_info->ordered_roots, &splice);
  582. while (!list_empty(&splice) && nr) {
  583. root = list_first_entry(&splice, struct btrfs_root,
  584. ordered_root);
  585. root = btrfs_grab_fs_root(root);
  586. BUG_ON(!root);
  587. list_move_tail(&root->ordered_root,
  588. &fs_info->ordered_roots);
  589. spin_unlock(&fs_info->ordered_root_lock);
  590. done = btrfs_wait_ordered_extents(root, nr);
  591. btrfs_put_fs_root(root);
  592. spin_lock(&fs_info->ordered_root_lock);
  593. if (nr != -1) {
  594. nr -= done;
  595. WARN_ON(nr < 0);
  596. }
  597. }
  598. list_splice_tail(&splice, &fs_info->ordered_roots);
  599. spin_unlock(&fs_info->ordered_root_lock);
  600. mutex_unlock(&fs_info->ordered_operations_mutex);
  601. }
  602. /*
  603. * this is used during transaction commit to write all the inodes
  604. * added to the ordered operation list. These files must be fully on
  605. * disk before the transaction commits.
  606. *
  607. * we have two modes here, one is to just start the IO via filemap_flush
  608. * and the other is to wait for all the io. When we wait, we have an
  609. * extra check to make sure the ordered operation list really is empty
  610. * before we return
  611. */
  612. int btrfs_run_ordered_operations(struct btrfs_trans_handle *trans,
  613. struct btrfs_root *root, int wait)
  614. {
  615. struct btrfs_inode *btrfs_inode;
  616. struct inode *inode;
  617. struct btrfs_transaction *cur_trans = trans->transaction;
  618. struct list_head splice;
  619. struct list_head works;
  620. struct btrfs_delalloc_work *work, *next;
  621. int ret = 0;
  622. INIT_LIST_HEAD(&splice);
  623. INIT_LIST_HEAD(&works);
  624. mutex_lock(&root->fs_info->ordered_extent_flush_mutex);
  625. spin_lock(&root->fs_info->ordered_root_lock);
  626. list_splice_init(&cur_trans->ordered_operations, &splice);
  627. while (!list_empty(&splice)) {
  628. btrfs_inode = list_entry(splice.next, struct btrfs_inode,
  629. ordered_operations);
  630. inode = &btrfs_inode->vfs_inode;
  631. list_del_init(&btrfs_inode->ordered_operations);
  632. /*
  633. * the inode may be getting freed (in sys_unlink path).
  634. */
  635. inode = igrab(inode);
  636. if (!inode)
  637. continue;
  638. if (!wait)
  639. list_add_tail(&BTRFS_I(inode)->ordered_operations,
  640. &cur_trans->ordered_operations);
  641. spin_unlock(&root->fs_info->ordered_root_lock);
  642. work = btrfs_alloc_delalloc_work(inode, wait, 1);
  643. if (!work) {
  644. spin_lock(&root->fs_info->ordered_root_lock);
  645. if (list_empty(&BTRFS_I(inode)->ordered_operations))
  646. list_add_tail(&btrfs_inode->ordered_operations,
  647. &splice);
  648. list_splice_tail(&splice,
  649. &cur_trans->ordered_operations);
  650. spin_unlock(&root->fs_info->ordered_root_lock);
  651. ret = -ENOMEM;
  652. goto out;
  653. }
  654. list_add_tail(&work->list, &works);
  655. btrfs_queue_work(root->fs_info->flush_workers,
  656. &work->work);
  657. cond_resched();
  658. spin_lock(&root->fs_info->ordered_root_lock);
  659. }
  660. spin_unlock(&root->fs_info->ordered_root_lock);
  661. out:
  662. list_for_each_entry_safe(work, next, &works, list) {
  663. list_del_init(&work->list);
  664. btrfs_wait_and_free_delalloc_work(work);
  665. }
  666. mutex_unlock(&root->fs_info->ordered_extent_flush_mutex);
  667. return ret;
  668. }
  669. /*
  670. * Used to start IO or wait for a given ordered extent to finish.
  671. *
  672. * If wait is one, this effectively waits on page writeback for all the pages
  673. * in the extent, and it waits on the io completion code to insert
  674. * metadata into the btree corresponding to the extent
  675. */
  676. void btrfs_start_ordered_extent(struct inode *inode,
  677. struct btrfs_ordered_extent *entry,
  678. int wait)
  679. {
  680. u64 start = entry->file_offset;
  681. u64 end = start + entry->len - 1;
  682. trace_btrfs_ordered_extent_start(inode, entry);
  683. /*
  684. * pages in the range can be dirty, clean or writeback. We
  685. * start IO on any dirty ones so the wait doesn't stall waiting
  686. * for the flusher thread to find them
  687. */
  688. if (!test_bit(BTRFS_ORDERED_DIRECT, &entry->flags))
  689. filemap_fdatawrite_range(inode->i_mapping, start, end);
  690. if (wait) {
  691. wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
  692. &entry->flags));
  693. }
  694. }
  695. /*
  696. * Used to wait on ordered extents across a large range of bytes.
  697. */
  698. int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
  699. {
  700. int ret = 0;
  701. u64 end;
  702. u64 orig_end;
  703. struct btrfs_ordered_extent *ordered;
  704. if (start + len < start) {
  705. orig_end = INT_LIMIT(loff_t);
  706. } else {
  707. orig_end = start + len - 1;
  708. if (orig_end > INT_LIMIT(loff_t))
  709. orig_end = INT_LIMIT(loff_t);
  710. }
  711. /* start IO across the range first to instantiate any delalloc
  712. * extents
  713. */
  714. ret = filemap_fdatawrite_range(inode->i_mapping, start, orig_end);
  715. if (ret)
  716. return ret;
  717. /*
  718. * So with compression we will find and lock a dirty page and clear the
  719. * first one as dirty, setup an async extent, and immediately return
  720. * with the entire range locked but with nobody actually marked with
  721. * writeback. So we can't just filemap_write_and_wait_range() and
  722. * expect it to work since it will just kick off a thread to do the
  723. * actual work. So we need to call filemap_fdatawrite_range _again_
  724. * since it will wait on the page lock, which won't be unlocked until
  725. * after the pages have been marked as writeback and so we're good to go
  726. * from there. We have to do this otherwise we'll miss the ordered
  727. * extents and that results in badness. Please Josef, do not think you
  728. * know better and pull this out at some point in the future, it is
  729. * right and you are wrong.
  730. */
  731. if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  732. &BTRFS_I(inode)->runtime_flags)) {
  733. ret = filemap_fdatawrite_range(inode->i_mapping, start,
  734. orig_end);
  735. if (ret)
  736. return ret;
  737. }
  738. ret = filemap_fdatawait_range(inode->i_mapping, start, orig_end);
  739. if (ret)
  740. return ret;
  741. end = orig_end;
  742. while (1) {
  743. ordered = btrfs_lookup_first_ordered_extent(inode, end);
  744. if (!ordered)
  745. break;
  746. if (ordered->file_offset > orig_end) {
  747. btrfs_put_ordered_extent(ordered);
  748. break;
  749. }
  750. if (ordered->file_offset + ordered->len <= start) {
  751. btrfs_put_ordered_extent(ordered);
  752. break;
  753. }
  754. btrfs_start_ordered_extent(inode, ordered, 1);
  755. end = ordered->file_offset;
  756. if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags))
  757. ret = -EIO;
  758. btrfs_put_ordered_extent(ordered);
  759. if (ret || end == 0 || end == start)
  760. break;
  761. end--;
  762. }
  763. return ret;
  764. }
  765. /*
  766. * find an ordered extent corresponding to file_offset. return NULL if
  767. * nothing is found, otherwise take a reference on the extent and return it
  768. */
  769. struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
  770. u64 file_offset)
  771. {
  772. struct btrfs_ordered_inode_tree *tree;
  773. struct rb_node *node;
  774. struct btrfs_ordered_extent *entry = NULL;
  775. tree = &BTRFS_I(inode)->ordered_tree;
  776. spin_lock_irq(&tree->lock);
  777. node = tree_search(tree, file_offset);
  778. if (!node)
  779. goto out;
  780. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  781. if (!offset_in_entry(entry, file_offset))
  782. entry = NULL;
  783. if (entry)
  784. atomic_inc(&entry->refs);
  785. out:
  786. spin_unlock_irq(&tree->lock);
  787. return entry;
  788. }
  789. /* Since the DIO code tries to lock a wide area we need to look for any ordered
  790. * extents that exist in the range, rather than just the start of the range.
  791. */
  792. struct btrfs_ordered_extent *btrfs_lookup_ordered_range(struct inode *inode,
  793. u64 file_offset,
  794. u64 len)
  795. {
  796. struct btrfs_ordered_inode_tree *tree;
  797. struct rb_node *node;
  798. struct btrfs_ordered_extent *entry = NULL;
  799. tree = &BTRFS_I(inode)->ordered_tree;
  800. spin_lock_irq(&tree->lock);
  801. node = tree_search(tree, file_offset);
  802. if (!node) {
  803. node = tree_search(tree, file_offset + len);
  804. if (!node)
  805. goto out;
  806. }
  807. while (1) {
  808. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  809. if (range_overlaps(entry, file_offset, len))
  810. break;
  811. if (entry->file_offset >= file_offset + len) {
  812. entry = NULL;
  813. break;
  814. }
  815. entry = NULL;
  816. node = rb_next(node);
  817. if (!node)
  818. break;
  819. }
  820. out:
  821. if (entry)
  822. atomic_inc(&entry->refs);
  823. spin_unlock_irq(&tree->lock);
  824. return entry;
  825. }
  826. /*
  827. * lookup and return any extent before 'file_offset'. NULL is returned
  828. * if none is found
  829. */
  830. struct btrfs_ordered_extent *
  831. btrfs_lookup_first_ordered_extent(struct inode *inode, u64 file_offset)
  832. {
  833. struct btrfs_ordered_inode_tree *tree;
  834. struct rb_node *node;
  835. struct btrfs_ordered_extent *entry = NULL;
  836. tree = &BTRFS_I(inode)->ordered_tree;
  837. spin_lock_irq(&tree->lock);
  838. node = tree_search(tree, file_offset);
  839. if (!node)
  840. goto out;
  841. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  842. atomic_inc(&entry->refs);
  843. out:
  844. spin_unlock_irq(&tree->lock);
  845. return entry;
  846. }
  847. /*
  848. * After an extent is done, call this to conditionally update the on disk
  849. * i_size. i_size is updated to cover any fully written part of the file.
  850. */
  851. int btrfs_ordered_update_i_size(struct inode *inode, u64 offset,
  852. struct btrfs_ordered_extent *ordered)
  853. {
  854. struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
  855. u64 disk_i_size;
  856. u64 new_i_size;
  857. u64 i_size = i_size_read(inode);
  858. struct rb_node *node;
  859. struct rb_node *prev = NULL;
  860. struct btrfs_ordered_extent *test;
  861. int ret = 1;
  862. spin_lock_irq(&tree->lock);
  863. if (ordered) {
  864. offset = entry_end(ordered);
  865. if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags))
  866. offset = min(offset,
  867. ordered->file_offset +
  868. ordered->truncated_len);
  869. } else {
  870. offset = ALIGN(offset, BTRFS_I(inode)->root->sectorsize);
  871. }
  872. disk_i_size = BTRFS_I(inode)->disk_i_size;
  873. /* truncate file */
  874. if (disk_i_size > i_size) {
  875. BTRFS_I(inode)->disk_i_size = i_size;
  876. ret = 0;
  877. goto out;
  878. }
  879. /*
  880. * if the disk i_size is already at the inode->i_size, or
  881. * this ordered extent is inside the disk i_size, we're done
  882. */
  883. if (disk_i_size == i_size)
  884. goto out;
  885. /*
  886. * We still need to update disk_i_size if outstanding_isize is greater
  887. * than disk_i_size.
  888. */
  889. if (offset <= disk_i_size &&
  890. (!ordered || ordered->outstanding_isize <= disk_i_size))
  891. goto out;
  892. /*
  893. * walk backward from this ordered extent to disk_i_size.
  894. * if we find an ordered extent then we can't update disk i_size
  895. * yet
  896. */
  897. if (ordered) {
  898. node = rb_prev(&ordered->rb_node);
  899. } else {
  900. prev = tree_search(tree, offset);
  901. /*
  902. * we insert file extents without involving ordered struct,
  903. * so there should be no ordered struct cover this offset
  904. */
  905. if (prev) {
  906. test = rb_entry(prev, struct btrfs_ordered_extent,
  907. rb_node);
  908. BUG_ON(offset_in_entry(test, offset));
  909. }
  910. node = prev;
  911. }
  912. for (; node; node = rb_prev(node)) {
  913. test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  914. /* We treat this entry as if it doesnt exist */
  915. if (test_bit(BTRFS_ORDERED_UPDATED_ISIZE, &test->flags))
  916. continue;
  917. if (test->file_offset + test->len <= disk_i_size)
  918. break;
  919. if (test->file_offset >= i_size)
  920. break;
  921. if (entry_end(test) > disk_i_size) {
  922. /*
  923. * we don't update disk_i_size now, so record this
  924. * undealt i_size. Or we will not know the real
  925. * i_size.
  926. */
  927. if (test->outstanding_isize < offset)
  928. test->outstanding_isize = offset;
  929. if (ordered &&
  930. ordered->outstanding_isize >
  931. test->outstanding_isize)
  932. test->outstanding_isize =
  933. ordered->outstanding_isize;
  934. goto out;
  935. }
  936. }
  937. new_i_size = min_t(u64, offset, i_size);
  938. /*
  939. * Some ordered extents may completed before the current one, and
  940. * we hold the real i_size in ->outstanding_isize.
  941. */
  942. if (ordered && ordered->outstanding_isize > new_i_size)
  943. new_i_size = min_t(u64, ordered->outstanding_isize, i_size);
  944. BTRFS_I(inode)->disk_i_size = new_i_size;
  945. ret = 0;
  946. out:
  947. /*
  948. * We need to do this because we can't remove ordered extents until
  949. * after the i_disk_size has been updated and then the inode has been
  950. * updated to reflect the change, so we need to tell anybody who finds
  951. * this ordered extent that we've already done all the real work, we
  952. * just haven't completed all the other work.
  953. */
  954. if (ordered)
  955. set_bit(BTRFS_ORDERED_UPDATED_ISIZE, &ordered->flags);
  956. spin_unlock_irq(&tree->lock);
  957. return ret;
  958. }
  959. /*
  960. * search the ordered extents for one corresponding to 'offset' and
  961. * try to find a checksum. This is used because we allow pages to
  962. * be reclaimed before their checksum is actually put into the btree
  963. */
  964. int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr,
  965. u32 *sum, int len)
  966. {
  967. struct btrfs_ordered_sum *ordered_sum;
  968. struct btrfs_ordered_extent *ordered;
  969. struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
  970. unsigned long num_sectors;
  971. unsigned long i;
  972. u32 sectorsize = BTRFS_I(inode)->root->sectorsize;
  973. int index = 0;
  974. ordered = btrfs_lookup_ordered_extent(inode, offset);
  975. if (!ordered)
  976. return 0;
  977. spin_lock_irq(&tree->lock);
  978. list_for_each_entry_reverse(ordered_sum, &ordered->list, list) {
  979. if (disk_bytenr >= ordered_sum->bytenr &&
  980. disk_bytenr < ordered_sum->bytenr + ordered_sum->len) {
  981. i = (disk_bytenr - ordered_sum->bytenr) >>
  982. inode->i_sb->s_blocksize_bits;
  983. num_sectors = ordered_sum->len >>
  984. inode->i_sb->s_blocksize_bits;
  985. num_sectors = min_t(int, len - index, num_sectors - i);
  986. memcpy(sum + index, ordered_sum->sums + i,
  987. num_sectors);
  988. index += (int)num_sectors;
  989. if (index == len)
  990. goto out;
  991. disk_bytenr += num_sectors * sectorsize;
  992. }
  993. }
  994. out:
  995. spin_unlock_irq(&tree->lock);
  996. btrfs_put_ordered_extent(ordered);
  997. return index;
  998. }
  999. /*
  1000. * add a given inode to the list of inodes that must be fully on
  1001. * disk before a transaction commit finishes.
  1002. *
  1003. * This basically gives us the ext3 style data=ordered mode, and it is mostly
  1004. * used to make sure renamed files are fully on disk.
  1005. *
  1006. * It is a noop if the inode is already fully on disk.
  1007. *
  1008. * If trans is not null, we'll do a friendly check for a transaction that
  1009. * is already flushing things and force the IO down ourselves.
  1010. */
  1011. void btrfs_add_ordered_operation(struct btrfs_trans_handle *trans,
  1012. struct btrfs_root *root, struct inode *inode)
  1013. {
  1014. struct btrfs_transaction *cur_trans = trans->transaction;
  1015. u64 last_mod;
  1016. last_mod = max(BTRFS_I(inode)->generation, BTRFS_I(inode)->last_trans);
  1017. /*
  1018. * if this file hasn't been changed since the last transaction
  1019. * commit, we can safely return without doing anything
  1020. */
  1021. if (last_mod <= root->fs_info->last_trans_committed)
  1022. return;
  1023. spin_lock(&root->fs_info->ordered_root_lock);
  1024. if (list_empty(&BTRFS_I(inode)->ordered_operations)) {
  1025. list_add_tail(&BTRFS_I(inode)->ordered_operations,
  1026. &cur_trans->ordered_operations);
  1027. }
  1028. spin_unlock(&root->fs_info->ordered_root_lock);
  1029. }
  1030. int __init ordered_data_init(void)
  1031. {
  1032. btrfs_ordered_extent_cache = kmem_cache_create("btrfs_ordered_extent",
  1033. sizeof(struct btrfs_ordered_extent), 0,
  1034. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
  1035. NULL);
  1036. if (!btrfs_ordered_extent_cache)
  1037. return -ENOMEM;
  1038. return 0;
  1039. }
  1040. void ordered_data_exit(void)
  1041. {
  1042. if (btrfs_ordered_extent_cache)
  1043. kmem_cache_destroy(btrfs_ordered_extent_cache);
  1044. }