ordered-data.c 29 KB

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