ordered-data.c 33 KB

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