ordered-data.c 30 KB

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