transaction.c (7174109c6548c4db85a383b8ae9d01469cddd110) transaction.c (161c3549b45aeef05451b6822d8aaaf39c7bedce)
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,

--- 218 unchanged lines hidden (view full) ---

227 kmem_cache_free(btrfs_transaction_cachep, cur_trans);
228 return -EROFS;
229 }
230
231 atomic_set(&cur_trans->num_writers, 1);
232 extwriter_counter_init(cur_trans, type);
233 init_waitqueue_head(&cur_trans->writer_wait);
234 init_waitqueue_head(&cur_trans->commit_wait);
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,

--- 218 unchanged lines hidden (view full) ---

227 kmem_cache_free(btrfs_transaction_cachep, cur_trans);
228 return -EROFS;
229 }
230
231 atomic_set(&cur_trans->num_writers, 1);
232 extwriter_counter_init(cur_trans, type);
233 init_waitqueue_head(&cur_trans->writer_wait);
234 init_waitqueue_head(&cur_trans->commit_wait);
235 init_waitqueue_head(&cur_trans->pending_wait);
235 cur_trans->state = TRANS_STATE_RUNNING;
236 /*
237 * One for this trans handle, one so it will live on until we
238 * commit the transaction.
239 */
240 atomic_set(&cur_trans->use_count, 2);
241 cur_trans->have_free_bgs = 0;
236 cur_trans->state = TRANS_STATE_RUNNING;
237 /*
238 * One for this trans handle, one so it will live on until we
239 * commit the transaction.
240 */
241 atomic_set(&cur_trans->use_count, 2);
242 cur_trans->have_free_bgs = 0;
243 atomic_set(&cur_trans->pending_ordered, 0);
242 cur_trans->start_time = get_seconds();
243 cur_trans->dirty_bg_run = 0;
244
245 memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs));
246
247 cur_trans->delayed_refs.href_root = RB_ROOT;
248 cur_trans->delayed_refs.dirty_extent_root = RB_ROOT;
249 atomic_set(&cur_trans->delayed_refs.num_entries, 0);

--- 11 unchanged lines hidden (view full) ---

261 "creating a fresh transaction\n");
262 atomic64_set(&fs_info->tree_mod_seq, 0);
263
264 spin_lock_init(&cur_trans->delayed_refs.lock);
265
266 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
267 INIT_LIST_HEAD(&cur_trans->pending_chunks);
268 INIT_LIST_HEAD(&cur_trans->switch_commits);
244 cur_trans->start_time = get_seconds();
245 cur_trans->dirty_bg_run = 0;
246
247 memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs));
248
249 cur_trans->delayed_refs.href_root = RB_ROOT;
250 cur_trans->delayed_refs.dirty_extent_root = RB_ROOT;
251 atomic_set(&cur_trans->delayed_refs.num_entries, 0);

--- 11 unchanged lines hidden (view full) ---

263 "creating a fresh transaction\n");
264 atomic64_set(&fs_info->tree_mod_seq, 0);
265
266 spin_lock_init(&cur_trans->delayed_refs.lock);
267
268 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
269 INIT_LIST_HEAD(&cur_trans->pending_chunks);
270 INIT_LIST_HEAD(&cur_trans->switch_commits);
269 INIT_LIST_HEAD(&cur_trans->pending_ordered);
270 INIT_LIST_HEAD(&cur_trans->dirty_bgs);
271 INIT_LIST_HEAD(&cur_trans->io_bgs);
272 INIT_LIST_HEAD(&cur_trans->dropped_roots);
273 mutex_init(&cur_trans->cache_write_mutex);
274 cur_trans->num_dirty_bgs = 0;
275 spin_lock_init(&cur_trans->dirty_bgs_lock);
276 INIT_LIST_HEAD(&cur_trans->deleted_bgs);
277 spin_lock_init(&cur_trans->deleted_bgs_lock);

--- 197 unchanged lines hidden (view full) ---

475 goto got_it;
476 }
477
478 /*
479 * Do the reservation before we join the transaction so we can do all
480 * the appropriate flushing if need be.
481 */
482 if (num_items > 0 && root != root->fs_info->chunk_root) {
271 INIT_LIST_HEAD(&cur_trans->dirty_bgs);
272 INIT_LIST_HEAD(&cur_trans->io_bgs);
273 INIT_LIST_HEAD(&cur_trans->dropped_roots);
274 mutex_init(&cur_trans->cache_write_mutex);
275 cur_trans->num_dirty_bgs = 0;
276 spin_lock_init(&cur_trans->dirty_bgs_lock);
277 INIT_LIST_HEAD(&cur_trans->deleted_bgs);
278 spin_lock_init(&cur_trans->deleted_bgs_lock);

--- 197 unchanged lines hidden (view full) ---

476 goto got_it;
477 }
478
479 /*
480 * Do the reservation before we join the transaction so we can do all
481 * the appropriate flushing if need be.
482 */
483 if (num_items > 0 && root != root->fs_info->chunk_root) {
483 qgroup_reserved = num_items * root->nodesize;
484 ret = btrfs_qgroup_reserve_meta(root, qgroup_reserved);
485 if (ret)
486 return ERR_PTR(ret);
484 if (root->fs_info->quota_enabled &&
485 is_fstree(root->root_key.objectid)) {
486 qgroup_reserved = num_items * root->nodesize;
487 ret = btrfs_qgroup_reserve(root, qgroup_reserved);
488 if (ret)
489 return ERR_PTR(ret);
490 }
487
488 num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
489 /*
490 * Do the reservation for the relocation root creation
491 */
492 if (need_reserve_reloc_root(root)) {
493 num_bytes += root->nodesize;
494 reloc_reserved = true;

--- 44 unchanged lines hidden (view full) ---

539 }
540
541 cur_trans = root->fs_info->running_transaction;
542
543 h->transid = cur_trans->transid;
544 h->transaction = cur_trans;
545 h->root = root;
546 h->use_count = 1;
491
492 num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
493 /*
494 * Do the reservation for the relocation root creation
495 */
496 if (need_reserve_reloc_root(root)) {
497 num_bytes += root->nodesize;
498 reloc_reserved = true;

--- 44 unchanged lines hidden (view full) ---

543 }
544
545 cur_trans = root->fs_info->running_transaction;
546
547 h->transid = cur_trans->transid;
548 h->transaction = cur_trans;
549 h->root = root;
550 h->use_count = 1;
547
548 h->type = type;
549 h->can_flush_pending_bgs = true;
550 INIT_LIST_HEAD(&h->qgroup_ref_list);
551 INIT_LIST_HEAD(&h->new_bgs);
551 h->type = type;
552 h->can_flush_pending_bgs = true;
553 INIT_LIST_HEAD(&h->qgroup_ref_list);
554 INIT_LIST_HEAD(&h->new_bgs);
552 INIT_LIST_HEAD(&h->ordered);
553
554 smp_mb();
555 if (cur_trans->state >= TRANS_STATE_BLOCKED &&
556 may_wait_transaction(root, type)) {
557 current->journal_info = h;
558 btrfs_commit_transaction(h, root);
559 goto again;
560 }
561
562 if (num_bytes) {
563 trace_btrfs_space_reservation(root->fs_info, "transaction",
564 h->transid, num_bytes, 1);
565 h->block_rsv = &root->fs_info->trans_block_rsv;
566 h->bytes_reserved = num_bytes;
567 h->reloc_reserved = reloc_reserved;
568 }
555
556 smp_mb();
557 if (cur_trans->state >= TRANS_STATE_BLOCKED &&
558 may_wait_transaction(root, type)) {
559 current->journal_info = h;
560 btrfs_commit_transaction(h, root);
561 goto again;
562 }
563
564 if (num_bytes) {
565 trace_btrfs_space_reservation(root->fs_info, "transaction",
566 h->transid, num_bytes, 1);
567 h->block_rsv = &root->fs_info->trans_block_rsv;
568 h->bytes_reserved = num_bytes;
569 h->reloc_reserved = reloc_reserved;
570 }
571 h->qgroup_reserved = qgroup_reserved;
569
570got_it:
571 btrfs_record_root_in_trans(h, root);
572
573 if (!current->journal_info && type != TRANS_USERSPACE)
574 current->journal_info = h;
575 return h;
576
577join_fail:
578 if (type & __TRANS_FREEZABLE)
579 sb_end_intwrite(root->fs_info->sb);
580 kmem_cache_free(btrfs_trans_handle_cachep, h);
581alloc_fail:
582 if (num_bytes)
583 btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv,
584 num_bytes);
585reserve_fail:
572
573got_it:
574 btrfs_record_root_in_trans(h, root);
575
576 if (!current->journal_info && type != TRANS_USERSPACE)
577 current->journal_info = h;
578 return h;
579
580join_fail:
581 if (type & __TRANS_FREEZABLE)
582 sb_end_intwrite(root->fs_info->sb);
583 kmem_cache_free(btrfs_trans_handle_cachep, h);
584alloc_fail:
585 if (num_bytes)
586 btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv,
587 num_bytes);
588reserve_fail:
586 btrfs_qgroup_free_meta(root, qgroup_reserved);
589 if (qgroup_reserved)
590 btrfs_qgroup_free(root, qgroup_reserved);
587 return ERR_PTR(ret);
588}
589
590struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
591 unsigned int num_items)
592{
593 return start_transaction(root, num_items, TRANS_START,
594 BTRFS_RESERVE_FLUSH_ALL);

--- 180 unchanged lines hidden (view full) ---

775 }
776
777 btrfs_trans_release_metadata(trans, root);
778 trans->block_rsv = NULL;
779
780 if (!list_empty(&trans->new_bgs))
781 btrfs_create_pending_block_groups(trans, root);
782
591 return ERR_PTR(ret);
592}
593
594struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
595 unsigned int num_items)
596{
597 return start_transaction(root, num_items, TRANS_START,
598 BTRFS_RESERVE_FLUSH_ALL);

--- 180 unchanged lines hidden (view full) ---

779 }
780
781 btrfs_trans_release_metadata(trans, root);
782 trans->block_rsv = NULL;
783
784 if (!list_empty(&trans->new_bgs))
785 btrfs_create_pending_block_groups(trans, root);
786
783 if (!list_empty(&trans->ordered)) {
784 spin_lock(&info->trans_lock);
785 list_splice_init(&trans->ordered, &cur_trans->pending_ordered);
786 spin_unlock(&info->trans_lock);
787 }
788
789 trans->delayed_ref_updates = 0;
790 if (!trans->sync) {
791 must_run_delayed_refs =
792 btrfs_should_throttle_delayed_refs(trans, root);
793 cur = max_t(unsigned long, cur, 32);
794
795 /*
796 * don't make the caller wait if they are from a NOLOCK
797 * or ATTACH transaction, it will deadlock with commit
798 */
799 if (must_run_delayed_refs == 1 &&
800 (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH)))
801 must_run_delayed_refs = 2;
802 }
803
787 trans->delayed_ref_updates = 0;
788 if (!trans->sync) {
789 must_run_delayed_refs =
790 btrfs_should_throttle_delayed_refs(trans, root);
791 cur = max_t(unsigned long, cur, 32);
792
793 /*
794 * don't make the caller wait if they are from a NOLOCK
795 * or ATTACH transaction, it will deadlock with commit
796 */
797 if (must_run_delayed_refs == 1 &&
798 (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH)))
799 must_run_delayed_refs = 2;
800 }
801
802 if (trans->qgroup_reserved) {
803 /*
804 * the same root has to be passed here between start_transaction
805 * and end_transaction. Subvolume quota depends on this.
806 */
807 btrfs_qgroup_free(trans->root, trans->qgroup_reserved);
808 trans->qgroup_reserved = 0;
809 }
810
804 btrfs_trans_release_metadata(trans, root);
805 trans->block_rsv = NULL;
806
807 if (!list_empty(&trans->new_bgs))
808 btrfs_create_pending_block_groups(trans, root);
809
810 btrfs_trans_release_chunk_metadata(trans);
811

--- 401 unchanged lines hidden (view full) ---

1213 }
1214
1215 err = btrfs_update_root(trans, fs_info->tree_root,
1216 &root->root_key,
1217 &root->root_item);
1218 spin_lock(&fs_info->fs_roots_radix_lock);
1219 if (err)
1220 break;
811 btrfs_trans_release_metadata(trans, root);
812 trans->block_rsv = NULL;
813
814 if (!list_empty(&trans->new_bgs))
815 btrfs_create_pending_block_groups(trans, root);
816
817 btrfs_trans_release_chunk_metadata(trans);
818

--- 401 unchanged lines hidden (view full) ---

1220 }
1221
1222 err = btrfs_update_root(trans, fs_info->tree_root,
1223 &root->root_key,
1224 &root->root_item);
1225 spin_lock(&fs_info->fs_roots_radix_lock);
1226 if (err)
1227 break;
1221 btrfs_qgroup_free_meta_all(root);
1222 }
1223 }
1224 spin_unlock(&fs_info->fs_roots_radix_lock);
1225 return err;
1226}
1227
1228/*
1229 * defrag a given btree.

--- 541 unchanged lines hidden (view full) ---

1771
1772static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
1773{
1774 if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
1775 btrfs_wait_ordered_roots(fs_info, -1);
1776}
1777
1778static inline void
1228 }
1229 }
1230 spin_unlock(&fs_info->fs_roots_radix_lock);
1231 return err;
1232}
1233
1234/*
1235 * defrag a given btree.

--- 541 unchanged lines hidden (view full) ---

1777
1778static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
1779{
1780 if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
1781 btrfs_wait_ordered_roots(fs_info, -1);
1782}
1783
1784static inline void
1779btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans,
1780 struct btrfs_fs_info *fs_info)
1785btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans)
1781{
1786{
1782 struct btrfs_ordered_extent *ordered;
1783
1784 spin_lock(&fs_info->trans_lock);
1785 while (!list_empty(&cur_trans->pending_ordered)) {
1786 ordered = list_first_entry(&cur_trans->pending_ordered,
1787 struct btrfs_ordered_extent,
1788 trans_list);
1789 list_del_init(&ordered->trans_list);
1790 spin_unlock(&fs_info->trans_lock);
1791
1792 wait_event(ordered->wait, test_bit(BTRFS_ORDERED_COMPLETE,
1793 &ordered->flags));
1794 btrfs_put_ordered_extent(ordered);
1795 spin_lock(&fs_info->trans_lock);
1796 }
1797 spin_unlock(&fs_info->trans_lock);
1787 wait_event(cur_trans->pending_wait,
1788 atomic_read(&cur_trans->pending_ordered) == 0);
1798}
1799
1800int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1801 struct btrfs_root *root)
1802{
1803 struct btrfs_transaction *cur_trans = trans->transaction;
1804 struct btrfs_transaction *prev_trans = NULL;
1805 struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode);

--- 12 unchanged lines hidden (view full) ---

1818 ret = btrfs_run_delayed_refs(trans, root, 0);
1819 if (ret) {
1820 btrfs_end_transaction(trans, root);
1821 return ret;
1822 }
1823
1824 btrfs_trans_release_metadata(trans, root);
1825 trans->block_rsv = NULL;
1789}
1790
1791int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1792 struct btrfs_root *root)
1793{
1794 struct btrfs_transaction *cur_trans = trans->transaction;
1795 struct btrfs_transaction *prev_trans = NULL;
1796 struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode);

--- 12 unchanged lines hidden (view full) ---

1809 ret = btrfs_run_delayed_refs(trans, root, 0);
1810 if (ret) {
1811 btrfs_end_transaction(trans, root);
1812 return ret;
1813 }
1814
1815 btrfs_trans_release_metadata(trans, root);
1816 trans->block_rsv = NULL;
1817 if (trans->qgroup_reserved) {
1818 btrfs_qgroup_free(root, trans->qgroup_reserved);
1819 trans->qgroup_reserved = 0;
1820 }
1826
1827 cur_trans = trans->transaction;
1828
1829 /*
1830 * set the flushing flag so procs in this transaction have to
1831 * start sending their work down.
1832 */
1833 cur_trans->delayed_refs.flushing = 1;

--- 35 unchanged lines hidden (view full) ---

1869 ret = btrfs_start_dirty_block_groups(trans, root);
1870 }
1871 if (ret) {
1872 btrfs_end_transaction(trans, root);
1873 return ret;
1874 }
1875
1876 spin_lock(&root->fs_info->trans_lock);
1821
1822 cur_trans = trans->transaction;
1823
1824 /*
1825 * set the flushing flag so procs in this transaction have to
1826 * start sending their work down.
1827 */
1828 cur_trans->delayed_refs.flushing = 1;

--- 35 unchanged lines hidden (view full) ---

1864 ret = btrfs_start_dirty_block_groups(trans, root);
1865 }
1866 if (ret) {
1867 btrfs_end_transaction(trans, root);
1868 return ret;
1869 }
1870
1871 spin_lock(&root->fs_info->trans_lock);
1877 list_splice_init(&trans->ordered, &cur_trans->pending_ordered);
1878 if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
1879 spin_unlock(&root->fs_info->trans_lock);
1880 atomic_inc(&cur_trans->use_count);
1881 ret = btrfs_end_transaction(trans, root);
1882
1883 wait_for_commit(root, cur_trans);
1884
1885 if (unlikely(cur_trans->aborted))

--- 42 unchanged lines hidden (view full) ---

1928
1929 /* some pending stuffs might be added after the previous flush. */
1930 ret = btrfs_run_delayed_items(trans, root);
1931 if (ret)
1932 goto cleanup_transaction;
1933
1934 btrfs_wait_delalloc_flush(root->fs_info);
1935
1872 if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
1873 spin_unlock(&root->fs_info->trans_lock);
1874 atomic_inc(&cur_trans->use_count);
1875 ret = btrfs_end_transaction(trans, root);
1876
1877 wait_for_commit(root, cur_trans);
1878
1879 if (unlikely(cur_trans->aborted))

--- 42 unchanged lines hidden (view full) ---

1922
1923 /* some pending stuffs might be added after the previous flush. */
1924 ret = btrfs_run_delayed_items(trans, root);
1925 if (ret)
1926 goto cleanup_transaction;
1927
1928 btrfs_wait_delalloc_flush(root->fs_info);
1929
1936 btrfs_wait_pending_ordered(cur_trans, root->fs_info);
1930 btrfs_wait_pending_ordered(cur_trans);
1937
1938 btrfs_scrub_pause(root);
1939 /*
1940 * Ok now we need to make sure to block out any other joins while we
1941 * commit the transaction. We could have started a join before setting
1942 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
1943 */
1944 spin_lock(&root->fs_info->trans_lock);

--- 225 unchanged lines hidden (view full) ---

2170 return ret;
2171
2172scrub_continue:
2173 btrfs_scrub_continue(root);
2174cleanup_transaction:
2175 btrfs_trans_release_metadata(trans, root);
2176 btrfs_trans_release_chunk_metadata(trans);
2177 trans->block_rsv = NULL;
1931
1932 btrfs_scrub_pause(root);
1933 /*
1934 * Ok now we need to make sure to block out any other joins while we
1935 * commit the transaction. We could have started a join before setting
1936 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
1937 */
1938 spin_lock(&root->fs_info->trans_lock);

--- 225 unchanged lines hidden (view full) ---

2164 return ret;
2165
2166scrub_continue:
2167 btrfs_scrub_continue(root);
2168cleanup_transaction:
2169 btrfs_trans_release_metadata(trans, root);
2170 btrfs_trans_release_chunk_metadata(trans);
2171 trans->block_rsv = NULL;
2172 if (trans->qgroup_reserved) {
2173 btrfs_qgroup_free(root, trans->qgroup_reserved);
2174 trans->qgroup_reserved = 0;
2175 }
2178 btrfs_warn(root->fs_info, "Skipping commit of aborted transaction.");
2179 if (current->journal_info == trans)
2180 current->journal_info = NULL;
2181 cleanup_transaction(trans, root, ret);
2182
2183 return ret;
2184}
2185

--- 66 unchanged lines hidden ---
2176 btrfs_warn(root->fs_info, "Skipping commit of aborted transaction.");
2177 if (current->journal_info == trans)
2178 current->journal_info = NULL;
2179 cleanup_transaction(trans, root, ret);
2180
2181 return ret;
2182}
2183

--- 66 unchanged lines hidden ---