ordered-data.c 28 KB

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