ordered-data.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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. {
  394. struct btrfs_ordered_inode_tree *tree;
  395. struct btrfs_ordered_extent *ordered;
  396. struct rb_node *n;
  397. tree = &BTRFS_I(inode)->ordered_tree;
  398. spin_lock_irq(&tree->lock);
  399. for (n = rb_first(&tree->tree); n; n = rb_next(n)) {
  400. ordered = rb_entry(n, struct btrfs_ordered_extent, rb_node);
  401. if (test_and_set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags))
  402. continue;
  403. list_add_tail(&ordered->log_list, logged_list);
  404. atomic_inc(&ordered->refs);
  405. }
  406. spin_unlock_irq(&tree->lock);
  407. }
  408. void btrfs_put_logged_extents(struct list_head *logged_list)
  409. {
  410. struct btrfs_ordered_extent *ordered;
  411. while (!list_empty(logged_list)) {
  412. ordered = list_first_entry(logged_list,
  413. struct btrfs_ordered_extent,
  414. log_list);
  415. list_del_init(&ordered->log_list);
  416. btrfs_put_ordered_extent(ordered);
  417. }
  418. }
  419. void btrfs_submit_logged_extents(struct list_head *logged_list,
  420. struct btrfs_root *log)
  421. {
  422. int index = log->log_transid % 2;
  423. spin_lock_irq(&log->log_extents_lock[index]);
  424. list_splice_tail(logged_list, &log->logged_list[index]);
  425. spin_unlock_irq(&log->log_extents_lock[index]);
  426. }
  427. void btrfs_wait_logged_extents(struct btrfs_trans_handle *trans,
  428. struct btrfs_root *log, u64 transid)
  429. {
  430. struct btrfs_ordered_extent *ordered;
  431. int index = transid % 2;
  432. spin_lock_irq(&log->log_extents_lock[index]);
  433. while (!list_empty(&log->logged_list[index])) {
  434. ordered = list_first_entry(&log->logged_list[index],
  435. struct btrfs_ordered_extent,
  436. log_list);
  437. list_del_init(&ordered->log_list);
  438. spin_unlock_irq(&log->log_extents_lock[index]);
  439. if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
  440. !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
  441. struct inode *inode = ordered->inode;
  442. u64 start = ordered->file_offset;
  443. u64 end = ordered->file_offset + ordered->len - 1;
  444. WARN_ON(!inode);
  445. filemap_fdatawrite_range(inode->i_mapping, start, end);
  446. }
  447. wait_event(ordered->wait, test_bit(BTRFS_ORDERED_IO_DONE,
  448. &ordered->flags));
  449. list_add_tail(&ordered->trans_list, &trans->ordered);
  450. spin_lock_irq(&log->log_extents_lock[index]);
  451. }
  452. spin_unlock_irq(&log->log_extents_lock[index]);
  453. }
  454. void btrfs_free_logged_extents(struct btrfs_root *log, u64 transid)
  455. {
  456. struct btrfs_ordered_extent *ordered;
  457. int index = transid % 2;
  458. spin_lock_irq(&log->log_extents_lock[index]);
  459. while (!list_empty(&log->logged_list[index])) {
  460. ordered = list_first_entry(&log->logged_list[index],
  461. struct btrfs_ordered_extent,
  462. log_list);
  463. list_del_init(&ordered->log_list);
  464. spin_unlock_irq(&log->log_extents_lock[index]);
  465. btrfs_put_ordered_extent(ordered);
  466. spin_lock_irq(&log->log_extents_lock[index]);
  467. }
  468. spin_unlock_irq(&log->log_extents_lock[index]);
  469. }
  470. /*
  471. * used to drop a reference on an ordered extent. This will free
  472. * the extent if the last reference is dropped
  473. */
  474. void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
  475. {
  476. struct list_head *cur;
  477. struct btrfs_ordered_sum *sum;
  478. trace_btrfs_ordered_extent_put(entry->inode, entry);
  479. if (atomic_dec_and_test(&entry->refs)) {
  480. if (entry->inode)
  481. btrfs_add_delayed_iput(entry->inode);
  482. while (!list_empty(&entry->list)) {
  483. cur = entry->list.next;
  484. sum = list_entry(cur, struct btrfs_ordered_sum, list);
  485. list_del(&sum->list);
  486. kfree(sum);
  487. }
  488. kmem_cache_free(btrfs_ordered_extent_cache, entry);
  489. }
  490. }
  491. /*
  492. * remove an ordered extent from the tree. No references are dropped
  493. * and waiters are woken up.
  494. */
  495. void btrfs_remove_ordered_extent(struct inode *inode,
  496. struct btrfs_ordered_extent *entry)
  497. {
  498. struct btrfs_ordered_inode_tree *tree;
  499. struct btrfs_root *root = BTRFS_I(inode)->root;
  500. struct rb_node *node;
  501. tree = &BTRFS_I(inode)->ordered_tree;
  502. spin_lock_irq(&tree->lock);
  503. node = &entry->rb_node;
  504. rb_erase(node, &tree->tree);
  505. if (tree->last == node)
  506. tree->last = NULL;
  507. set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
  508. spin_unlock_irq(&tree->lock);
  509. spin_lock(&root->ordered_extent_lock);
  510. list_del_init(&entry->root_extent_list);
  511. root->nr_ordered_extents--;
  512. trace_btrfs_ordered_extent_remove(inode, entry);
  513. if (!root->nr_ordered_extents) {
  514. spin_lock(&root->fs_info->ordered_root_lock);
  515. BUG_ON(list_empty(&root->ordered_root));
  516. list_del_init(&root->ordered_root);
  517. spin_unlock(&root->fs_info->ordered_root_lock);
  518. }
  519. spin_unlock(&root->ordered_extent_lock);
  520. wake_up(&entry->wait);
  521. }
  522. static void btrfs_run_ordered_extent_work(struct btrfs_work *work)
  523. {
  524. struct btrfs_ordered_extent *ordered;
  525. ordered = container_of(work, struct btrfs_ordered_extent, flush_work);
  526. btrfs_start_ordered_extent(ordered->inode, ordered, 1);
  527. complete(&ordered->completion);
  528. }
  529. /*
  530. * wait for all the ordered extents in a root. This is done when balancing
  531. * space between drives.
  532. */
  533. int btrfs_wait_ordered_extents(struct btrfs_root *root, int nr)
  534. {
  535. struct list_head splice, works;
  536. struct btrfs_ordered_extent *ordered, *next;
  537. int count = 0;
  538. INIT_LIST_HEAD(&splice);
  539. INIT_LIST_HEAD(&works);
  540. mutex_lock(&root->ordered_extent_mutex);
  541. spin_lock(&root->ordered_extent_lock);
  542. list_splice_init(&root->ordered_extents, &splice);
  543. while (!list_empty(&splice) && nr) {
  544. ordered = list_first_entry(&splice, struct btrfs_ordered_extent,
  545. root_extent_list);
  546. list_move_tail(&ordered->root_extent_list,
  547. &root->ordered_extents);
  548. atomic_inc(&ordered->refs);
  549. spin_unlock(&root->ordered_extent_lock);
  550. btrfs_init_work(&ordered->flush_work,
  551. btrfs_flush_delalloc_helper,
  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. * Used to start IO or wait for a given ordered extent to finish.
  604. *
  605. * If wait is one, this effectively waits on page writeback for all the pages
  606. * in the extent, and it waits on the io completion code to insert
  607. * metadata into the btree corresponding to the extent
  608. */
  609. void btrfs_start_ordered_extent(struct inode *inode,
  610. struct btrfs_ordered_extent *entry,
  611. int wait)
  612. {
  613. u64 start = entry->file_offset;
  614. u64 end = start + entry->len - 1;
  615. trace_btrfs_ordered_extent_start(inode, entry);
  616. /*
  617. * pages in the range can be dirty, clean or writeback. We
  618. * start IO on any dirty ones so the wait doesn't stall waiting
  619. * for the flusher thread to find them
  620. */
  621. if (!test_bit(BTRFS_ORDERED_DIRECT, &entry->flags))
  622. filemap_fdatawrite_range(inode->i_mapping, start, end);
  623. if (wait) {
  624. wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
  625. &entry->flags));
  626. }
  627. }
  628. /*
  629. * Used to wait on ordered extents across a large range of bytes.
  630. */
  631. int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
  632. {
  633. int ret = 0;
  634. u64 end;
  635. u64 orig_end;
  636. struct btrfs_ordered_extent *ordered;
  637. if (start + len < start) {
  638. orig_end = INT_LIMIT(loff_t);
  639. } else {
  640. orig_end = start + len - 1;
  641. if (orig_end > INT_LIMIT(loff_t))
  642. orig_end = INT_LIMIT(loff_t);
  643. }
  644. /* start IO across the range first to instantiate any delalloc
  645. * extents
  646. */
  647. ret = filemap_fdatawrite_range(inode->i_mapping, start, orig_end);
  648. if (ret)
  649. return ret;
  650. /*
  651. * So with compression we will find and lock a dirty page and clear the
  652. * first one as dirty, setup an async extent, and immediately return
  653. * with the entire range locked but with nobody actually marked with
  654. * writeback. So we can't just filemap_write_and_wait_range() and
  655. * expect it to work since it will just kick off a thread to do the
  656. * actual work. So we need to call filemap_fdatawrite_range _again_
  657. * since it will wait on the page lock, which won't be unlocked until
  658. * after the pages have been marked as writeback and so we're good to go
  659. * from there. We have to do this otherwise we'll miss the ordered
  660. * extents and that results in badness. Please Josef, do not think you
  661. * know better and pull this out at some point in the future, it is
  662. * right and you are wrong.
  663. */
  664. if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  665. &BTRFS_I(inode)->runtime_flags)) {
  666. ret = filemap_fdatawrite_range(inode->i_mapping, start,
  667. orig_end);
  668. if (ret)
  669. return ret;
  670. }
  671. ret = filemap_fdatawait_range(inode->i_mapping, start, orig_end);
  672. if (ret)
  673. return ret;
  674. end = orig_end;
  675. while (1) {
  676. ordered = btrfs_lookup_first_ordered_extent(inode, end);
  677. if (!ordered)
  678. break;
  679. if (ordered->file_offset > orig_end) {
  680. btrfs_put_ordered_extent(ordered);
  681. break;
  682. }
  683. if (ordered->file_offset + ordered->len <= start) {
  684. btrfs_put_ordered_extent(ordered);
  685. break;
  686. }
  687. btrfs_start_ordered_extent(inode, ordered, 1);
  688. end = ordered->file_offset;
  689. if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags))
  690. ret = -EIO;
  691. btrfs_put_ordered_extent(ordered);
  692. if (ret || end == 0 || end == start)
  693. break;
  694. end--;
  695. }
  696. return ret;
  697. }
  698. /*
  699. * find an ordered extent corresponding to file_offset. return NULL if
  700. * nothing is found, otherwise take a reference on the extent and return it
  701. */
  702. struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
  703. u64 file_offset)
  704. {
  705. struct btrfs_ordered_inode_tree *tree;
  706. struct rb_node *node;
  707. struct btrfs_ordered_extent *entry = NULL;
  708. tree = &BTRFS_I(inode)->ordered_tree;
  709. spin_lock_irq(&tree->lock);
  710. node = tree_search(tree, file_offset);
  711. if (!node)
  712. goto out;
  713. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  714. if (!offset_in_entry(entry, file_offset))
  715. entry = NULL;
  716. if (entry)
  717. atomic_inc(&entry->refs);
  718. out:
  719. spin_unlock_irq(&tree->lock);
  720. return entry;
  721. }
  722. /* Since the DIO code tries to lock a wide area we need to look for any ordered
  723. * extents that exist in the range, rather than just the start of the range.
  724. */
  725. struct btrfs_ordered_extent *btrfs_lookup_ordered_range(struct inode *inode,
  726. u64 file_offset,
  727. u64 len)
  728. {
  729. struct btrfs_ordered_inode_tree *tree;
  730. struct rb_node *node;
  731. struct btrfs_ordered_extent *entry = NULL;
  732. tree = &BTRFS_I(inode)->ordered_tree;
  733. spin_lock_irq(&tree->lock);
  734. node = tree_search(tree, file_offset);
  735. if (!node) {
  736. node = tree_search(tree, file_offset + len);
  737. if (!node)
  738. goto out;
  739. }
  740. while (1) {
  741. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  742. if (range_overlaps(entry, file_offset, len))
  743. break;
  744. if (entry->file_offset >= file_offset + len) {
  745. entry = NULL;
  746. break;
  747. }
  748. entry = NULL;
  749. node = rb_next(node);
  750. if (!node)
  751. break;
  752. }
  753. out:
  754. if (entry)
  755. atomic_inc(&entry->refs);
  756. spin_unlock_irq(&tree->lock);
  757. return entry;
  758. }
  759. /*
  760. * lookup and return any extent before 'file_offset'. NULL is returned
  761. * if none is found
  762. */
  763. struct btrfs_ordered_extent *
  764. btrfs_lookup_first_ordered_extent(struct inode *inode, u64 file_offset)
  765. {
  766. struct btrfs_ordered_inode_tree *tree;
  767. struct rb_node *node;
  768. struct btrfs_ordered_extent *entry = NULL;
  769. tree = &BTRFS_I(inode)->ordered_tree;
  770. spin_lock_irq(&tree->lock);
  771. node = tree_search(tree, file_offset);
  772. if (!node)
  773. goto out;
  774. entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  775. atomic_inc(&entry->refs);
  776. out:
  777. spin_unlock_irq(&tree->lock);
  778. return entry;
  779. }
  780. /*
  781. * After an extent is done, call this to conditionally update the on disk
  782. * i_size. i_size is updated to cover any fully written part of the file.
  783. */
  784. int btrfs_ordered_update_i_size(struct inode *inode, u64 offset,
  785. struct btrfs_ordered_extent *ordered)
  786. {
  787. struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
  788. u64 disk_i_size;
  789. u64 new_i_size;
  790. u64 i_size = i_size_read(inode);
  791. struct rb_node *node;
  792. struct rb_node *prev = NULL;
  793. struct btrfs_ordered_extent *test;
  794. int ret = 1;
  795. spin_lock_irq(&tree->lock);
  796. if (ordered) {
  797. offset = entry_end(ordered);
  798. if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags))
  799. offset = min(offset,
  800. ordered->file_offset +
  801. ordered->truncated_len);
  802. } else {
  803. offset = ALIGN(offset, BTRFS_I(inode)->root->sectorsize);
  804. }
  805. disk_i_size = BTRFS_I(inode)->disk_i_size;
  806. /* truncate file */
  807. if (disk_i_size > i_size) {
  808. BTRFS_I(inode)->disk_i_size = i_size;
  809. ret = 0;
  810. goto out;
  811. }
  812. /*
  813. * if the disk i_size is already at the inode->i_size, or
  814. * this ordered extent is inside the disk i_size, we're done
  815. */
  816. if (disk_i_size == i_size)
  817. goto out;
  818. /*
  819. * We still need to update disk_i_size if outstanding_isize is greater
  820. * than disk_i_size.
  821. */
  822. if (offset <= disk_i_size &&
  823. (!ordered || ordered->outstanding_isize <= disk_i_size))
  824. goto out;
  825. /*
  826. * walk backward from this ordered extent to disk_i_size.
  827. * if we find an ordered extent then we can't update disk i_size
  828. * yet
  829. */
  830. if (ordered) {
  831. node = rb_prev(&ordered->rb_node);
  832. } else {
  833. prev = tree_search(tree, offset);
  834. /*
  835. * we insert file extents without involving ordered struct,
  836. * so there should be no ordered struct cover this offset
  837. */
  838. if (prev) {
  839. test = rb_entry(prev, struct btrfs_ordered_extent,
  840. rb_node);
  841. BUG_ON(offset_in_entry(test, offset));
  842. }
  843. node = prev;
  844. }
  845. for (; node; node = rb_prev(node)) {
  846. test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
  847. /* We treat this entry as if it doesnt exist */
  848. if (test_bit(BTRFS_ORDERED_UPDATED_ISIZE, &test->flags))
  849. continue;
  850. if (test->file_offset + test->len <= disk_i_size)
  851. break;
  852. if (test->file_offset >= i_size)
  853. break;
  854. if (entry_end(test) > disk_i_size) {
  855. /*
  856. * we don't update disk_i_size now, so record this
  857. * undealt i_size. Or we will not know the real
  858. * i_size.
  859. */
  860. if (test->outstanding_isize < offset)
  861. test->outstanding_isize = offset;
  862. if (ordered &&
  863. ordered->outstanding_isize >
  864. test->outstanding_isize)
  865. test->outstanding_isize =
  866. ordered->outstanding_isize;
  867. goto out;
  868. }
  869. }
  870. new_i_size = min_t(u64, offset, i_size);
  871. /*
  872. * Some ordered extents may completed before the current one, and
  873. * we hold the real i_size in ->outstanding_isize.
  874. */
  875. if (ordered && ordered->outstanding_isize > new_i_size)
  876. new_i_size = min_t(u64, ordered->outstanding_isize, i_size);
  877. BTRFS_I(inode)->disk_i_size = new_i_size;
  878. ret = 0;
  879. out:
  880. /*
  881. * We need to do this because we can't remove ordered extents until
  882. * after the i_disk_size has been updated and then the inode has been
  883. * updated to reflect the change, so we need to tell anybody who finds
  884. * this ordered extent that we've already done all the real work, we
  885. * just haven't completed all the other work.
  886. */
  887. if (ordered)
  888. set_bit(BTRFS_ORDERED_UPDATED_ISIZE, &ordered->flags);
  889. spin_unlock_irq(&tree->lock);
  890. return ret;
  891. }
  892. /*
  893. * search the ordered extents for one corresponding to 'offset' and
  894. * try to find a checksum. This is used because we allow pages to
  895. * be reclaimed before their checksum is actually put into the btree
  896. */
  897. int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr,
  898. u32 *sum, int len)
  899. {
  900. struct btrfs_ordered_sum *ordered_sum;
  901. struct btrfs_ordered_extent *ordered;
  902. struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
  903. unsigned long num_sectors;
  904. unsigned long i;
  905. u32 sectorsize = BTRFS_I(inode)->root->sectorsize;
  906. int index = 0;
  907. ordered = btrfs_lookup_ordered_extent(inode, offset);
  908. if (!ordered)
  909. return 0;
  910. spin_lock_irq(&tree->lock);
  911. list_for_each_entry_reverse(ordered_sum, &ordered->list, list) {
  912. if (disk_bytenr >= ordered_sum->bytenr &&
  913. disk_bytenr < ordered_sum->bytenr + ordered_sum->len) {
  914. i = (disk_bytenr - ordered_sum->bytenr) >>
  915. inode->i_sb->s_blocksize_bits;
  916. num_sectors = ordered_sum->len >>
  917. inode->i_sb->s_blocksize_bits;
  918. num_sectors = min_t(int, len - index, num_sectors - i);
  919. memcpy(sum + index, ordered_sum->sums + i,
  920. num_sectors);
  921. index += (int)num_sectors;
  922. if (index == len)
  923. goto out;
  924. disk_bytenr += num_sectors * sectorsize;
  925. }
  926. }
  927. out:
  928. spin_unlock_irq(&tree->lock);
  929. btrfs_put_ordered_extent(ordered);
  930. return index;
  931. }
  932. int __init ordered_data_init(void)
  933. {
  934. btrfs_ordered_extent_cache = kmem_cache_create("btrfs_ordered_extent",
  935. sizeof(struct btrfs_ordered_extent), 0,
  936. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
  937. NULL);
  938. if (!btrfs_ordered_extent_cache)
  939. return -ENOMEM;
  940. return 0;
  941. }
  942. void ordered_data_exit(void)
  943. {
  944. if (btrfs_ordered_extent_cache)
  945. kmem_cache_destroy(btrfs_ordered_extent_cache);
  946. }