ordered-data.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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. 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. INIT_LIST_HEAD(&entry->trans_list);
  202. trace_btrfs_ordered_extent_add(inode, entry);
  203. spin_lock_irq(&tree->lock);
  204. node = tree_insert(&tree->tree, file_offset,
  205. &entry->rb_node);
  206. if (node)
  207. ordered_data_tree_panic(inode, -EEXIST, file_offset);
  208. spin_unlock_irq(&tree->lock);
  209. spin_lock(&root->ordered_extent_lock);
  210. list_add_tail(&entry->root_extent_list,
  211. &root->ordered_extents);
  212. root->nr_ordered_extents++;
  213. if (root->nr_ordered_extents == 1) {
  214. spin_lock(&root->fs_info->ordered_root_lock);
  215. BUG_ON(!list_empty(&root->ordered_root));
  216. list_add_tail(&root->ordered_root,
  217. &root->fs_info->ordered_roots);
  218. spin_unlock(&root->fs_info->ordered_root_lock);
  219. }
  220. spin_unlock(&root->ordered_extent_lock);
  221. return 0;
  222. }
  223. int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
  224. u64 start, u64 len, u64 disk_len, int type)
  225. {
  226. return __btrfs_add_ordered_extent(inode, file_offset, start, len,
  227. disk_len, type, 0,
  228. BTRFS_COMPRESS_NONE);
  229. }
  230. int btrfs_add_ordered_extent_dio(struct inode *inode, u64 file_offset,
  231. u64 start, u64 len, u64 disk_len, int type)
  232. {
  233. return __btrfs_add_ordered_extent(inode, file_offset, start, len,
  234. disk_len, type, 1,
  235. BTRFS_COMPRESS_NONE);
  236. }
  237. int btrfs_add_ordered_extent_compress(struct inode *inode, u64 file_offset,
  238. u64 start, u64 len, u64 disk_len,
  239. int type, int compress_type)
  240. {
  241. return __btrfs_add_ordered_extent(inode, file_offset, start, len,
  242. disk_len, type, 0,
  243. compress_type);
  244. }
  245. /*
  246. * Add a struct btrfs_ordered_sum into the list of checksums to be inserted
  247. * when an ordered extent is finished. If the list covers more than one
  248. * ordered extent, it is split across multiples.
  249. */
  250. void btrfs_add_ordered_sum(struct inode *inode,
  251. struct btrfs_ordered_extent *entry,
  252. struct btrfs_ordered_sum *sum)
  253. {
  254. struct btrfs_ordered_inode_tree *tree;
  255. tree = &BTRFS_I(inode)->ordered_tree;
  256. spin_lock_irq(&tree->lock);
  257. list_add_tail(&sum->list, &entry->list);
  258. WARN_ON(entry->csum_bytes_left < sum->len);
  259. entry->csum_bytes_left -= sum->len;
  260. if (entry->csum_bytes_left == 0)
  261. wake_up(&entry->wait);
  262. spin_unlock_irq(&tree->lock);
  263. }
  264. /*
  265. * this is used to account for finished IO across a given range
  266. * of the file. The IO may span ordered extents. If
  267. * a given ordered_extent is completely done, 1 is returned, otherwise
  268. * 0.
  269. *
  270. * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
  271. * to make sure this function only returns 1 once for a given ordered extent.
  272. *
  273. * file_offset is updated to one byte past the range that is recorded as
  274. * complete. This allows you to walk forward in the file.
  275. */
  276. int btrfs_dec_test_first_ordered_pending(struct inode *inode,
  277. struct btrfs_ordered_extent **cached,
  278. u64 *file_offset, u64 io_size, int uptodate)
  279. {
  280. struct btrfs_ordered_inode_tree *tree;
  281. struct rb_node *node;
  282. struct btrfs_ordered_extent *entry = NULL;
  283. int ret;
  284. unsigned long flags;
  285. u64 dec_end;
  286. u64 dec_start;
  287. u64 to_dec;
  288. tree = &BTRFS_I(inode)->ordered_tree;
  289. spin_lock_irqsave(&tree->lock, flags);
  290. node = tree_search(tree, *file_offset);
  291. if (!node) {
  292. ret = 1;
  293. goto out;
  294. }
  295. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  296. if (!offset_in_entry(entry, *file_offset)) {
  297. ret = 1;
  298. goto out;
  299. }
  300. dec_start = max(*file_offset, entry->file_offset);
  301. dec_end = min(*file_offset + io_size, entry->file_offset +
  302. entry->len);
  303. *file_offset = dec_end;
  304. if (dec_start > dec_end) {
  305. btrfs_crit(BTRFS_I(inode)->root->fs_info,
  306. "bad ordering dec_start %llu end %llu", dec_start, dec_end);
  307. }
  308. to_dec = dec_end - dec_start;
  309. if (to_dec > entry->bytes_left) {
  310. btrfs_crit(BTRFS_I(inode)->root->fs_info,
  311. "bad ordered accounting left %llu size %llu",
  312. entry->bytes_left, to_dec);
  313. }
  314. entry->bytes_left -= to_dec;
  315. if (!uptodate)
  316. set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
  317. if (entry->bytes_left == 0) {
  318. ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
  319. if (waitqueue_active(&entry->wait))
  320. wake_up(&entry->wait);
  321. } else {
  322. ret = 1;
  323. }
  324. out:
  325. if (!ret && cached && entry) {
  326. *cached = entry;
  327. atomic_inc(&entry->refs);
  328. }
  329. spin_unlock_irqrestore(&tree->lock, flags);
  330. return ret == 0;
  331. }
  332. /*
  333. * this is used to account for finished IO across a given range
  334. * of the file. The IO should not span ordered extents. If
  335. * a given ordered_extent is completely done, 1 is returned, otherwise
  336. * 0.
  337. *
  338. * test_and_set_bit on a flag in the struct btrfs_ordered_extent is used
  339. * to make sure this function only returns 1 once for a given ordered extent.
  340. */
  341. int btrfs_dec_test_ordered_pending(struct inode *inode,
  342. struct btrfs_ordered_extent **cached,
  343. u64 file_offset, u64 io_size, int uptodate)
  344. {
  345. struct btrfs_ordered_inode_tree *tree;
  346. struct rb_node *node;
  347. struct btrfs_ordered_extent *entry = NULL;
  348. unsigned long flags;
  349. int ret;
  350. tree = &BTRFS_I(inode)->ordered_tree;
  351. spin_lock_irqsave(&tree->lock, flags);
  352. if (cached && *cached) {
  353. entry = *cached;
  354. goto have_entry;
  355. }
  356. node = tree_search(tree, file_offset);
  357. if (!node) {
  358. ret = 1;
  359. goto out;
  360. }
  361. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  362. have_entry:
  363. if (!offset_in_entry(entry, file_offset)) {
  364. ret = 1;
  365. goto out;
  366. }
  367. if (io_size > entry->bytes_left) {
  368. btrfs_crit(BTRFS_I(inode)->root->fs_info,
  369. "bad ordered accounting left %llu size %llu",
  370. entry->bytes_left, io_size);
  371. }
  372. entry->bytes_left -= io_size;
  373. if (!uptodate)
  374. set_bit(BTRFS_ORDERED_IOERR, &entry->flags);
  375. if (entry->bytes_left == 0) {
  376. ret = test_and_set_bit(BTRFS_ORDERED_IO_DONE, &entry->flags);
  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. ordered = list_first_entry(&log->logged_list[index],
  445. struct btrfs_ordered_extent,
  446. log_list);
  447. list_del_init(&ordered->log_list);
  448. spin_unlock_irq(&log->log_extents_lock[index]);
  449. if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
  450. !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
  451. struct inode *inode = ordered->inode;
  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. list_add_tail(&ordered->trans_list, &trans->ordered);
  460. spin_lock_irq(&log->log_extents_lock[index]);
  461. }
  462. spin_unlock_irq(&log->log_extents_lock[index]);
  463. }
  464. void btrfs_free_logged_extents(struct btrfs_root *log, u64 transid)
  465. {
  466. struct btrfs_ordered_extent *ordered;
  467. int index = transid % 2;
  468. spin_lock_irq(&log->log_extents_lock[index]);
  469. while (!list_empty(&log->logged_list[index])) {
  470. ordered = list_first_entry(&log->logged_list[index],
  471. struct btrfs_ordered_extent,
  472. log_list);
  473. list_del_init(&ordered->log_list);
  474. spin_unlock_irq(&log->log_extents_lock[index]);
  475. btrfs_put_ordered_extent(ordered);
  476. spin_lock_irq(&log->log_extents_lock[index]);
  477. }
  478. spin_unlock_irq(&log->log_extents_lock[index]);
  479. }
  480. /*
  481. * used to drop a reference on an ordered extent. This will free
  482. * the extent if the last reference is dropped
  483. */
  484. void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
  485. {
  486. struct list_head *cur;
  487. struct btrfs_ordered_sum *sum;
  488. trace_btrfs_ordered_extent_put(entry->inode, entry);
  489. if (atomic_dec_and_test(&entry->refs)) {
  490. if (entry->inode)
  491. btrfs_add_delayed_iput(entry->inode);
  492. while (!list_empty(&entry->list)) {
  493. cur = entry->list.next;
  494. sum = list_entry(cur, struct btrfs_ordered_sum, list);
  495. list_del(&sum->list);
  496. kfree(sum);
  497. }
  498. kmem_cache_free(btrfs_ordered_extent_cache, entry);
  499. }
  500. }
  501. /*
  502. * remove an ordered extent from the tree. No references are dropped
  503. * and waiters are woken up.
  504. */
  505. void btrfs_remove_ordered_extent(struct inode *inode,
  506. struct btrfs_ordered_extent *entry)
  507. {
  508. struct btrfs_ordered_inode_tree *tree;
  509. struct btrfs_root *root = BTRFS_I(inode)->root;
  510. struct rb_node *node;
  511. tree = &BTRFS_I(inode)->ordered_tree;
  512. spin_lock_irq(&tree->lock);
  513. node = &entry->rb_node;
  514. rb_erase(node, &tree->tree);
  515. if (tree->last == node)
  516. tree->last = NULL;
  517. set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
  518. spin_unlock_irq(&tree->lock);
  519. spin_lock(&root->ordered_extent_lock);
  520. list_del_init(&entry->root_extent_list);
  521. root->nr_ordered_extents--;
  522. trace_btrfs_ordered_extent_remove(inode, entry);
  523. if (!root->nr_ordered_extents) {
  524. spin_lock(&root->fs_info->ordered_root_lock);
  525. BUG_ON(list_empty(&root->ordered_root));
  526. list_del_init(&root->ordered_root);
  527. spin_unlock(&root->fs_info->ordered_root_lock);
  528. }
  529. spin_unlock(&root->ordered_extent_lock);
  530. wake_up(&entry->wait);
  531. }
  532. static void btrfs_run_ordered_extent_work(struct btrfs_work *work)
  533. {
  534. struct btrfs_ordered_extent *ordered;
  535. ordered = container_of(work, struct btrfs_ordered_extent, flush_work);
  536. btrfs_start_ordered_extent(ordered->inode, ordered, 1);
  537. complete(&ordered->completion);
  538. }
  539. /*
  540. * wait for all the ordered extents in a root. This is done when balancing
  541. * space between drives.
  542. */
  543. int btrfs_wait_ordered_extents(struct btrfs_root *root, int nr)
  544. {
  545. struct list_head splice, works;
  546. struct btrfs_ordered_extent *ordered, *next;
  547. int count = 0;
  548. INIT_LIST_HEAD(&splice);
  549. INIT_LIST_HEAD(&works);
  550. mutex_lock(&root->ordered_extent_mutex);
  551. spin_lock(&root->ordered_extent_lock);
  552. list_splice_init(&root->ordered_extents, &splice);
  553. while (!list_empty(&splice) && nr) {
  554. ordered = list_first_entry(&splice, struct btrfs_ordered_extent,
  555. root_extent_list);
  556. list_move_tail(&ordered->root_extent_list,
  557. &root->ordered_extents);
  558. atomic_inc(&ordered->refs);
  559. spin_unlock(&root->ordered_extent_lock);
  560. btrfs_init_work(&ordered->flush_work,
  561. btrfs_flush_delalloc_helper,
  562. btrfs_run_ordered_extent_work, NULL, NULL);
  563. list_add_tail(&ordered->work_list, &works);
  564. btrfs_queue_work(root->fs_info->flush_workers,
  565. &ordered->flush_work);
  566. cond_resched();
  567. spin_lock(&root->ordered_extent_lock);
  568. if (nr != -1)
  569. nr--;
  570. count++;
  571. }
  572. list_splice_tail(&splice, &root->ordered_extents);
  573. spin_unlock(&root->ordered_extent_lock);
  574. list_for_each_entry_safe(ordered, next, &works, work_list) {
  575. list_del_init(&ordered->work_list);
  576. wait_for_completion(&ordered->completion);
  577. btrfs_put_ordered_extent(ordered);
  578. cond_resched();
  579. }
  580. mutex_unlock(&root->ordered_extent_mutex);
  581. return count;
  582. }
  583. void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, int nr)
  584. {
  585. struct btrfs_root *root;
  586. struct list_head splice;
  587. int done;
  588. INIT_LIST_HEAD(&splice);
  589. mutex_lock(&fs_info->ordered_operations_mutex);
  590. spin_lock(&fs_info->ordered_root_lock);
  591. list_splice_init(&fs_info->ordered_roots, &splice);
  592. while (!list_empty(&splice) && nr) {
  593. root = list_first_entry(&splice, struct btrfs_root,
  594. ordered_root);
  595. root = btrfs_grab_fs_root(root);
  596. BUG_ON(!root);
  597. list_move_tail(&root->ordered_root,
  598. &fs_info->ordered_roots);
  599. spin_unlock(&fs_info->ordered_root_lock);
  600. done = btrfs_wait_ordered_extents(root, nr);
  601. btrfs_put_fs_root(root);
  602. spin_lock(&fs_info->ordered_root_lock);
  603. if (nr != -1) {
  604. nr -= done;
  605. WARN_ON(nr < 0);
  606. }
  607. }
  608. list_splice_tail(&splice, &fs_info->ordered_roots);
  609. spin_unlock(&fs_info->ordered_root_lock);
  610. mutex_unlock(&fs_info->ordered_operations_mutex);
  611. }
  612. /*
  613. * Used to start IO or wait for a given ordered extent to finish.
  614. *
  615. * If wait is one, this effectively waits on page writeback for all the pages
  616. * in the extent, and it waits on the io completion code to insert
  617. * metadata into the btree corresponding to the extent
  618. */
  619. void btrfs_start_ordered_extent(struct inode *inode,
  620. struct btrfs_ordered_extent *entry,
  621. int wait)
  622. {
  623. u64 start = entry->file_offset;
  624. u64 end = start + entry->len - 1;
  625. trace_btrfs_ordered_extent_start(inode, entry);
  626. /*
  627. * pages in the range can be dirty, clean or writeback. We
  628. * start IO on any dirty ones so the wait doesn't stall waiting
  629. * for the flusher thread to find them
  630. */
  631. if (!test_bit(BTRFS_ORDERED_DIRECT, &entry->flags))
  632. filemap_fdatawrite_range(inode->i_mapping, start, end);
  633. if (wait) {
  634. wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
  635. &entry->flags));
  636. }
  637. }
  638. /*
  639. * Used to wait on ordered extents across a large range of bytes.
  640. */
  641. int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
  642. {
  643. int ret = 0;
  644. u64 end;
  645. u64 orig_end;
  646. struct btrfs_ordered_extent *ordered;
  647. if (start + len < start) {
  648. orig_end = INT_LIMIT(loff_t);
  649. } else {
  650. orig_end = start + len - 1;
  651. if (orig_end > INT_LIMIT(loff_t))
  652. orig_end = INT_LIMIT(loff_t);
  653. }
  654. /* start IO across the range first to instantiate any delalloc
  655. * extents
  656. */
  657. ret = btrfs_fdatawrite_range(inode, start, orig_end);
  658. if (ret)
  659. return ret;
  660. ret = filemap_fdatawait_range(inode->i_mapping, start, orig_end);
  661. if (ret)
  662. return ret;
  663. end = orig_end;
  664. while (1) {
  665. ordered = btrfs_lookup_first_ordered_extent(inode, end);
  666. if (!ordered)
  667. break;
  668. if (ordered->file_offset > orig_end) {
  669. btrfs_put_ordered_extent(ordered);
  670. break;
  671. }
  672. if (ordered->file_offset + ordered->len <= start) {
  673. btrfs_put_ordered_extent(ordered);
  674. break;
  675. }
  676. btrfs_start_ordered_extent(inode, ordered, 1);
  677. end = ordered->file_offset;
  678. if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags))
  679. ret = -EIO;
  680. btrfs_put_ordered_extent(ordered);
  681. if (ret || end == 0 || end == start)
  682. break;
  683. end--;
  684. }
  685. return ret;
  686. }
  687. /*
  688. * find an ordered extent corresponding to file_offset. return NULL if
  689. * nothing is found, otherwise take a reference on the extent and return it
  690. */
  691. struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
  692. u64 file_offset)
  693. {
  694. struct btrfs_ordered_inode_tree *tree;
  695. struct rb_node *node;
  696. struct btrfs_ordered_extent *entry = NULL;
  697. tree = &BTRFS_I(inode)->ordered_tree;
  698. spin_lock_irq(&tree->lock);
  699. node = tree_search(tree, file_offset);
  700. if (!node)
  701. goto out;
  702. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  703. if (!offset_in_entry(entry, file_offset))
  704. entry = NULL;
  705. if (entry)
  706. atomic_inc(&entry->refs);
  707. out:
  708. spin_unlock_irq(&tree->lock);
  709. return entry;
  710. }
  711. /* Since the DIO code tries to lock a wide area we need to look for any ordered
  712. * extents that exist in the range, rather than just the start of the range.
  713. */
  714. struct btrfs_ordered_extent *btrfs_lookup_ordered_range(struct inode *inode,
  715. u64 file_offset,
  716. u64 len)
  717. {
  718. struct btrfs_ordered_inode_tree *tree;
  719. struct rb_node *node;
  720. struct btrfs_ordered_extent *entry = NULL;
  721. tree = &BTRFS_I(inode)->ordered_tree;
  722. spin_lock_irq(&tree->lock);
  723. node = tree_search(tree, file_offset);
  724. if (!node) {
  725. node = tree_search(tree, file_offset + len);
  726. if (!node)
  727. goto out;
  728. }
  729. while (1) {
  730. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  731. if (range_overlaps(entry, file_offset, len))
  732. break;
  733. if (entry->file_offset >= file_offset + len) {
  734. entry = NULL;
  735. break;
  736. }
  737. entry = NULL;
  738. node = rb_next(node);
  739. if (!node)
  740. break;
  741. }
  742. out:
  743. if (entry)
  744. atomic_inc(&entry->refs);
  745. spin_unlock_irq(&tree->lock);
  746. return entry;
  747. }
  748. /*
  749. * lookup and return any extent before 'file_offset'. NULL is returned
  750. * if none is found
  751. */
  752. struct btrfs_ordered_extent *
  753. btrfs_lookup_first_ordered_extent(struct inode *inode, u64 file_offset)
  754. {
  755. struct btrfs_ordered_inode_tree *tree;
  756. struct rb_node *node;
  757. struct btrfs_ordered_extent *entry = NULL;
  758. tree = &BTRFS_I(inode)->ordered_tree;
  759. spin_lock_irq(&tree->lock);
  760. node = tree_search(tree, file_offset);
  761. if (!node)
  762. goto out;
  763. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  764. atomic_inc(&entry->refs);
  765. out:
  766. spin_unlock_irq(&tree->lock);
  767. return entry;
  768. }
  769. /*
  770. * After an extent is done, call this to conditionally update the on disk
  771. * i_size. i_size is updated to cover any fully written part of the file.
  772. */
  773. int btrfs_ordered_update_i_size(struct inode *inode, u64 offset,
  774. struct btrfs_ordered_extent *ordered)
  775. {
  776. struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
  777. u64 disk_i_size;
  778. u64 new_i_size;
  779. u64 i_size = i_size_read(inode);
  780. struct rb_node *node;
  781. struct rb_node *prev = NULL;
  782. struct btrfs_ordered_extent *test;
  783. int ret = 1;
  784. spin_lock_irq(&tree->lock);
  785. if (ordered) {
  786. offset = entry_end(ordered);
  787. if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags))
  788. offset = min(offset,
  789. ordered->file_offset +
  790. ordered->truncated_len);
  791. } else {
  792. offset = ALIGN(offset, BTRFS_I(inode)->root->sectorsize);
  793. }
  794. disk_i_size = BTRFS_I(inode)->disk_i_size;
  795. /* truncate file */
  796. if (disk_i_size > i_size) {
  797. BTRFS_I(inode)->disk_i_size = i_size;
  798. ret = 0;
  799. goto out;
  800. }
  801. /*
  802. * if the disk i_size is already at the inode->i_size, or
  803. * this ordered extent is inside the disk i_size, we're done
  804. */
  805. if (disk_i_size == i_size)
  806. goto out;
  807. /*
  808. * We still need to update disk_i_size if outstanding_isize is greater
  809. * than disk_i_size.
  810. */
  811. if (offset <= disk_i_size &&
  812. (!ordered || ordered->outstanding_isize <= disk_i_size))
  813. goto out;
  814. /*
  815. * walk backward from this ordered extent to disk_i_size.
  816. * if we find an ordered extent then we can't update disk i_size
  817. * yet
  818. */
  819. if (ordered) {
  820. node = rb_prev(&ordered->rb_node);
  821. } else {
  822. prev = tree_search(tree, offset);
  823. /*
  824. * we insert file extents without involving ordered struct,
  825. * so there should be no ordered struct cover this offset
  826. */
  827. if (prev) {
  828. test = rb_entry(prev, struct btrfs_ordered_extent,
  829. rb_node);
  830. BUG_ON(offset_in_entry(test, offset));
  831. }
  832. node = prev;
  833. }
  834. for (; node; node = rb_prev(node)) {
  835. test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  836. /* We treat this entry as if it doesnt exist */
  837. if (test_bit(BTRFS_ORDERED_UPDATED_ISIZE, &test->flags))
  838. continue;
  839. if (test->file_offset + test->len <= disk_i_size)
  840. break;
  841. if (test->file_offset >= i_size)
  842. break;
  843. if (entry_end(test) > disk_i_size) {
  844. /*
  845. * we don't update disk_i_size now, so record this
  846. * undealt i_size. Or we will not know the real
  847. * i_size.
  848. */
  849. if (test->outstanding_isize < offset)
  850. test->outstanding_isize = offset;
  851. if (ordered &&
  852. ordered->outstanding_isize >
  853. test->outstanding_isize)
  854. test->outstanding_isize =
  855. ordered->outstanding_isize;
  856. goto out;
  857. }
  858. }
  859. new_i_size = min_t(u64, offset, i_size);
  860. /*
  861. * Some ordered extents may completed before the current one, and
  862. * we hold the real i_size in ->outstanding_isize.
  863. */
  864. if (ordered && ordered->outstanding_isize > new_i_size)
  865. new_i_size = min_t(u64, ordered->outstanding_isize, i_size);
  866. BTRFS_I(inode)->disk_i_size = new_i_size;
  867. ret = 0;
  868. out:
  869. /*
  870. * We need to do this because we can't remove ordered extents until
  871. * after the i_disk_size has been updated and then the inode has been
  872. * updated to reflect the change, so we need to tell anybody who finds
  873. * this ordered extent that we've already done all the real work, we
  874. * just haven't completed all the other work.
  875. */
  876. if (ordered)
  877. set_bit(BTRFS_ORDERED_UPDATED_ISIZE, &ordered->flags);
  878. spin_unlock_irq(&tree->lock);
  879. return ret;
  880. }
  881. /*
  882. * search the ordered extents for one corresponding to 'offset' and
  883. * try to find a checksum. This is used because we allow pages to
  884. * be reclaimed before their checksum is actually put into the btree
  885. */
  886. int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr,
  887. u32 *sum, int len)
  888. {
  889. struct btrfs_ordered_sum *ordered_sum;
  890. struct btrfs_ordered_extent *ordered;
  891. struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
  892. unsigned long num_sectors;
  893. unsigned long i;
  894. u32 sectorsize = BTRFS_I(inode)->root->sectorsize;
  895. int index = 0;
  896. ordered = btrfs_lookup_ordered_extent(inode, offset);
  897. if (!ordered)
  898. return 0;
  899. spin_lock_irq(&tree->lock);
  900. list_for_each_entry_reverse(ordered_sum, &ordered->list, list) {
  901. if (disk_bytenr >= ordered_sum->bytenr &&
  902. disk_bytenr < ordered_sum->bytenr + ordered_sum->len) {
  903. i = (disk_bytenr - ordered_sum->bytenr) >>
  904. inode->i_sb->s_blocksize_bits;
  905. num_sectors = ordered_sum->len >>
  906. inode->i_sb->s_blocksize_bits;
  907. num_sectors = min_t(int, len - index, num_sectors - i);
  908. memcpy(sum + index, ordered_sum->sums + i,
  909. num_sectors);
  910. index += (int)num_sectors;
  911. if (index == len)
  912. goto out;
  913. disk_bytenr += num_sectors * sectorsize;
  914. }
  915. }
  916. out:
  917. spin_unlock_irq(&tree->lock);
  918. btrfs_put_ordered_extent(ordered);
  919. return index;
  920. }
  921. int __init ordered_data_init(void)
  922. {
  923. btrfs_ordered_extent_cache = kmem_cache_create("btrfs_ordered_extent",
  924. sizeof(struct btrfs_ordered_extent), 0,
  925. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
  926. NULL);
  927. if (!btrfs_ordered_extent_cache)
  928. return -ENOMEM;
  929. return 0;
  930. }
  931. void ordered_data_exit(void)
  932. {
  933. if (btrfs_ordered_extent_cache)
  934. kmem_cache_destroy(btrfs_ordered_extent_cache);
  935. }