16cbd5570SChris Mason /* 26cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 36cbd5570SChris Mason * 46cbd5570SChris Mason * This program is free software; you can redistribute it and/or 56cbd5570SChris Mason * modify it under the terms of the GNU General Public 66cbd5570SChris Mason * License v2 as published by the Free Software Foundation. 76cbd5570SChris Mason * 86cbd5570SChris Mason * This program is distributed in the hope that it will be useful, 96cbd5570SChris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of 106cbd5570SChris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 116cbd5570SChris Mason * General Public License for more details. 126cbd5570SChris Mason * 136cbd5570SChris Mason * You should have received a copy of the GNU General Public 146cbd5570SChris Mason * License along with this program; if not, write to the 156cbd5570SChris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 166cbd5570SChris Mason * Boston, MA 021110-1307, USA. 176cbd5570SChris Mason */ 186cbd5570SChris Mason 1979154b1bSChris Mason #include <linux/fs.h> 2034088780SChris Mason #include <linux/sched.h> 21d3c2fdcfSChris Mason #include <linux/writeback.h> 225f39d397SChris Mason #include <linux/pagemap.h> 2379154b1bSChris Mason #include "ctree.h" 2479154b1bSChris Mason #include "disk-io.h" 2579154b1bSChris Mason #include "transaction.h" 2679154b1bSChris Mason 2778fae27eSChris Mason static int total_trans = 0; 282c90e5d6SChris Mason extern struct kmem_cache *btrfs_trans_handle_cachep; 292c90e5d6SChris Mason extern struct kmem_cache *btrfs_transaction_cachep; 302c90e5d6SChris Mason 3108607c1bSChris Mason static struct workqueue_struct *trans_wq; 3208607c1bSChris Mason 330f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 346702ed49SChris Mason #define BTRFS_ROOT_DEFRAG_TAG 1 350f7d52f4SChris Mason 3679154b1bSChris Mason static void put_transaction(struct btrfs_transaction *transaction) 3779154b1bSChris Mason { 382c90e5d6SChris Mason WARN_ON(transaction->use_count == 0); 3979154b1bSChris Mason transaction->use_count--; 4078fae27eSChris Mason if (transaction->use_count == 0) { 4178fae27eSChris Mason WARN_ON(total_trans == 0); 4278fae27eSChris Mason total_trans--; 438fd17795SChris Mason list_del_init(&transaction->list); 442c90e5d6SChris Mason memset(transaction, 0, sizeof(*transaction)); 452c90e5d6SChris Mason kmem_cache_free(btrfs_transaction_cachep, transaction); 4679154b1bSChris Mason } 4778fae27eSChris Mason } 4879154b1bSChris Mason 4979154b1bSChris Mason static int join_transaction(struct btrfs_root *root) 5079154b1bSChris Mason { 5179154b1bSChris Mason struct btrfs_transaction *cur_trans; 5279154b1bSChris Mason cur_trans = root->fs_info->running_transaction; 5379154b1bSChris Mason if (!cur_trans) { 542c90e5d6SChris Mason cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, 552c90e5d6SChris Mason GFP_NOFS); 5678fae27eSChris Mason total_trans++; 5779154b1bSChris Mason BUG_ON(!cur_trans); 580f7d52f4SChris Mason root->fs_info->generation++; 5979154b1bSChris Mason root->fs_info->running_transaction = cur_trans; 6015ee9bc7SJosef Bacik cur_trans->num_writers = 1; 6115ee9bc7SJosef Bacik cur_trans->num_joined = 0; 620f7d52f4SChris Mason cur_trans->transid = root->fs_info->generation; 6379154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 6479154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 6579154b1bSChris Mason cur_trans->in_commit = 0; 66d5719762SChris Mason cur_trans->use_count = 1; 6779154b1bSChris Mason cur_trans->commit_done = 0; 6808607c1bSChris Mason cur_trans->start_time = get_seconds(); 698fd17795SChris Mason list_add_tail(&cur_trans->list, &root->fs_info->trans_list); 705f39d397SChris Mason extent_map_tree_init(&cur_trans->dirty_pages, 715f39d397SChris Mason root->fs_info->btree_inode->i_mapping, 725f39d397SChris Mason GFP_NOFS); 7315ee9bc7SJosef Bacik } else { 7479154b1bSChris Mason cur_trans->num_writers++; 7515ee9bc7SJosef Bacik cur_trans->num_joined++; 7615ee9bc7SJosef Bacik } 7715ee9bc7SJosef Bacik 7879154b1bSChris Mason return 0; 7979154b1bSChris Mason } 8079154b1bSChris Mason 816702ed49SChris Mason static int record_root_in_trans(struct btrfs_root *root) 826702ed49SChris Mason { 836702ed49SChris Mason u64 running_trans_id = root->fs_info->running_transaction->transid; 846702ed49SChris Mason if (root->ref_cows && root->last_trans < running_trans_id) { 856702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 866702ed49SChris Mason if (root->root_item.refs != 0) { 876702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 886702ed49SChris Mason (unsigned long)root->root_key.objectid, 896702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 906702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 916702ed49SChris Mason (unsigned long)root->root_key.objectid, 926702ed49SChris Mason BTRFS_ROOT_DEFRAG_TAG); 936702ed49SChris Mason root->commit_root = root->node; 945f39d397SChris Mason extent_buffer_get(root->node); 956702ed49SChris Mason } else { 966702ed49SChris Mason WARN_ON(1); 976702ed49SChris Mason } 986702ed49SChris Mason root->last_trans = running_trans_id; 996702ed49SChris Mason } 1006702ed49SChris Mason return 0; 1016702ed49SChris Mason } 1026702ed49SChris Mason 10379154b1bSChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 10479154b1bSChris Mason int num_blocks) 10579154b1bSChris Mason { 1062c90e5d6SChris Mason struct btrfs_trans_handle *h = 1072c90e5d6SChris Mason kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 10879154b1bSChris Mason int ret; 10979154b1bSChris Mason 11079154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 11179154b1bSChris Mason ret = join_transaction(root); 11279154b1bSChris Mason BUG_ON(ret); 1130f7d52f4SChris Mason 1146702ed49SChris Mason record_root_in_trans(root); 1156702ed49SChris Mason h->transid = root->fs_info->running_transaction->transid; 11679154b1bSChris Mason h->transaction = root->fs_info->running_transaction; 11779154b1bSChris Mason h->blocks_reserved = num_blocks; 11879154b1bSChris Mason h->blocks_used = 0; 11931f3c99bSChris Mason h->block_group = NULL; 12026b8003fSChris Mason h->alloc_exclude_nr = 0; 12126b8003fSChris Mason h->alloc_exclude_start = 0; 12279154b1bSChris Mason root->fs_info->running_transaction->use_count++; 12379154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 12479154b1bSChris Mason return h; 12579154b1bSChris Mason } 12679154b1bSChris Mason 12779154b1bSChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 12879154b1bSChris Mason struct btrfs_root *root) 12979154b1bSChris Mason { 13079154b1bSChris Mason struct btrfs_transaction *cur_trans; 131d6e4a428SChris Mason 13279154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 13379154b1bSChris Mason cur_trans = root->fs_info->running_transaction; 134ccd467d6SChris Mason WARN_ON(cur_trans != trans->transaction); 135d5719762SChris Mason WARN_ON(cur_trans->num_writers < 1); 136ccd467d6SChris Mason cur_trans->num_writers--; 13779154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 13879154b1bSChris Mason wake_up(&cur_trans->writer_wait); 13979154b1bSChris Mason put_transaction(cur_trans); 14079154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 141d6025579SChris Mason memset(trans, 0, sizeof(*trans)); 1422c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 14379154b1bSChris Mason return 0; 14479154b1bSChris Mason } 14579154b1bSChris Mason 14679154b1bSChris Mason 14779154b1bSChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 14879154b1bSChris Mason struct btrfs_root *root) 14979154b1bSChris Mason { 1507c4452b9SChris Mason int ret; 1517c4452b9SChris Mason int err; 1527c4452b9SChris Mason int werr = 0; 1535f39d397SChris Mason struct extent_map_tree *dirty_pages; 1547c4452b9SChris Mason struct page *page; 1557c4452b9SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 1565f39d397SChris Mason u64 start; 1575f39d397SChris Mason u64 end; 1585f39d397SChris Mason unsigned long index; 1597c4452b9SChris Mason 1607c4452b9SChris Mason if (!trans || !trans->transaction) { 1617c4452b9SChris Mason return filemap_write_and_wait(btree_inode->i_mapping); 1627c4452b9SChris Mason } 1637c4452b9SChris Mason dirty_pages = &trans->transaction->dirty_pages; 1647c4452b9SChris Mason while(1) { 1655f39d397SChris Mason ret = find_first_extent_bit(dirty_pages, 0, &start, &end, 1665f39d397SChris Mason EXTENT_DIRTY); 1675f39d397SChris Mason if (ret) 1687c4452b9SChris Mason break; 1695f39d397SChris Mason clear_extent_dirty(dirty_pages, start, end, GFP_NOFS); 1705f39d397SChris Mason while(start <= end) { 1715f39d397SChris Mason index = start >> PAGE_CACHE_SHIFT; 1725f39d397SChris Mason start = (index + 1) << PAGE_CACHE_SHIFT; 1735f39d397SChris Mason page = find_lock_page(btree_inode->i_mapping, index); 1747c4452b9SChris Mason if (!page) 1757c4452b9SChris Mason continue; 1766702ed49SChris Mason if (PageWriteback(page)) { 1776702ed49SChris Mason if (PageDirty(page)) 1786702ed49SChris Mason wait_on_page_writeback(page); 1796702ed49SChris Mason else { 1806702ed49SChris Mason unlock_page(page); 1816702ed49SChris Mason page_cache_release(page); 1826702ed49SChris Mason continue; 1836702ed49SChris Mason } 1846702ed49SChris Mason } 1857c4452b9SChris Mason err = write_one_page(page, 0); 1867c4452b9SChris Mason if (err) 1877c4452b9SChris Mason werr = err; 1887c4452b9SChris Mason page_cache_release(page); 1897c4452b9SChris Mason } 1907c4452b9SChris Mason } 1917c4452b9SChris Mason err = filemap_fdatawait(btree_inode->i_mapping); 1927c4452b9SChris Mason if (err) 1937c4452b9SChris Mason werr = err; 1947c4452b9SChris Mason return werr; 19579154b1bSChris Mason } 19679154b1bSChris Mason 19779154b1bSChris Mason int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, 19879154b1bSChris Mason struct btrfs_root *root) 19979154b1bSChris Mason { 20079154b1bSChris Mason int ret; 20179154b1bSChris Mason u64 old_extent_block; 20279154b1bSChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 20379154b1bSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 20479154b1bSChris Mason struct btrfs_root *extent_root = fs_info->extent_root; 20579154b1bSChris Mason 2069078a3e1SChris Mason btrfs_write_dirty_block_groups(trans, extent_root); 20779154b1bSChris Mason while(1) { 20879154b1bSChris Mason old_extent_block = btrfs_root_blocknr(&extent_root->root_item); 2095f39d397SChris Mason if (old_extent_block == 2105f39d397SChris Mason extent_buffer_blocknr(extent_root->node)) 21179154b1bSChris Mason break; 21279154b1bSChris Mason btrfs_set_root_blocknr(&extent_root->root_item, 2135f39d397SChris Mason extent_buffer_blocknr(extent_root->node)); 21479154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 21579154b1bSChris Mason &extent_root->root_key, 21679154b1bSChris Mason &extent_root->root_item); 21779154b1bSChris Mason BUG_ON(ret); 2189078a3e1SChris Mason btrfs_write_dirty_block_groups(trans, extent_root); 21979154b1bSChris Mason } 22079154b1bSChris Mason return 0; 22179154b1bSChris Mason } 22279154b1bSChris Mason 22379154b1bSChris Mason static int wait_for_commit(struct btrfs_root *root, 22479154b1bSChris Mason struct btrfs_transaction *commit) 22579154b1bSChris Mason { 22679154b1bSChris Mason DEFINE_WAIT(wait); 227ccd467d6SChris Mason mutex_lock(&root->fs_info->trans_mutex); 22879154b1bSChris Mason while(!commit->commit_done) { 22979154b1bSChris Mason prepare_to_wait(&commit->commit_wait, &wait, 23079154b1bSChris Mason TASK_UNINTERRUPTIBLE); 23179154b1bSChris Mason if (commit->commit_done) 23279154b1bSChris Mason break; 23379154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 23479154b1bSChris Mason schedule(); 23579154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 23679154b1bSChris Mason } 237ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 23879154b1bSChris Mason finish_wait(&commit->commit_wait, &wait); 23979154b1bSChris Mason return 0; 24079154b1bSChris Mason } 24179154b1bSChris Mason 2420f7d52f4SChris Mason struct dirty_root { 2430f7d52f4SChris Mason struct list_head list; 2440f7d52f4SChris Mason struct btrfs_root *root; 24558176a96SJosef Bacik struct btrfs_root *latest_root; 2460f7d52f4SChris Mason }; 2470f7d52f4SChris Mason 2485ce14bbcSChris Mason int btrfs_add_dead_root(struct btrfs_root *root, 2495ce14bbcSChris Mason struct btrfs_root *latest, 2505ce14bbcSChris Mason struct list_head *dead_list) 2515eda7b5eSChris Mason { 2525eda7b5eSChris Mason struct dirty_root *dirty; 2535eda7b5eSChris Mason 2545eda7b5eSChris Mason dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 2555eda7b5eSChris Mason if (!dirty) 2565eda7b5eSChris Mason return -ENOMEM; 2575eda7b5eSChris Mason dirty->root = root; 2585ce14bbcSChris Mason dirty->latest_root = latest; 2595eda7b5eSChris Mason list_add(&dirty->list, dead_list); 2605eda7b5eSChris Mason return 0; 2615eda7b5eSChris Mason } 2625eda7b5eSChris Mason 26335b7e476SChris Mason static int add_dirty_roots(struct btrfs_trans_handle *trans, 26435b7e476SChris Mason struct radix_tree_root *radix, 26535b7e476SChris Mason struct list_head *list) 2660f7d52f4SChris Mason { 2670f7d52f4SChris Mason struct dirty_root *dirty; 2680f7d52f4SChris Mason struct btrfs_root *gang[8]; 2690f7d52f4SChris Mason struct btrfs_root *root; 2700f7d52f4SChris Mason int i; 2710f7d52f4SChris Mason int ret; 27254aa1f4dSChris Mason int err = 0; 2735eda7b5eSChris Mason u32 refs; 27454aa1f4dSChris Mason 2750f7d52f4SChris Mason while(1) { 2760f7d52f4SChris Mason ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, 2770f7d52f4SChris Mason ARRAY_SIZE(gang), 2780f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 2790f7d52f4SChris Mason if (ret == 0) 2800f7d52f4SChris Mason break; 2810f7d52f4SChris Mason for (i = 0; i < ret; i++) { 2820f7d52f4SChris Mason root = gang[i]; 2832619ba1fSChris Mason radix_tree_tag_clear(radix, 2842619ba1fSChris Mason (unsigned long)root->root_key.objectid, 2850f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 2860f7d52f4SChris Mason if (root->commit_root == root->node) { 2875f39d397SChris Mason WARN_ON(extent_buffer_blocknr(root->node) != 2880f7d52f4SChris Mason btrfs_root_blocknr(&root->root_item)); 2895f39d397SChris Mason free_extent_buffer(root->commit_root); 2900f7d52f4SChris Mason root->commit_root = NULL; 29158176a96SJosef Bacik 29258176a96SJosef Bacik /* make sure to update the root on disk 29358176a96SJosef Bacik * so we get any updates to the block used 29458176a96SJosef Bacik * counts 29558176a96SJosef Bacik */ 29658176a96SJosef Bacik err = btrfs_update_root(trans, 29758176a96SJosef Bacik root->fs_info->tree_root, 29858176a96SJosef Bacik &root->root_key, 29958176a96SJosef Bacik &root->root_item); 3000f7d52f4SChris Mason continue; 3010f7d52f4SChris Mason } 3020f7d52f4SChris Mason dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 3030f7d52f4SChris Mason BUG_ON(!dirty); 3049f3a7427SChris Mason dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS); 3059f3a7427SChris Mason BUG_ON(!dirty->root); 3069f3a7427SChris Mason 3079f3a7427SChris Mason memset(&root->root_item.drop_progress, 0, 3089f3a7427SChris Mason sizeof(struct btrfs_disk_key)); 3099f3a7427SChris Mason root->root_item.drop_level = 0; 3109f3a7427SChris Mason 3119f3a7427SChris Mason memcpy(dirty->root, root, sizeof(*root)); 3129f3a7427SChris Mason dirty->root->node = root->commit_root; 31358176a96SJosef Bacik dirty->latest_root = root; 3140f7d52f4SChris Mason root->commit_root = NULL; 3155eda7b5eSChris Mason 3160f7d52f4SChris Mason root->root_key.offset = root->fs_info->generation; 3170f7d52f4SChris Mason btrfs_set_root_blocknr(&root->root_item, 3185f39d397SChris Mason extent_buffer_blocknr(root->node)); 3190f7d52f4SChris Mason err = btrfs_insert_root(trans, root->fs_info->tree_root, 3200f7d52f4SChris Mason &root->root_key, 3210f7d52f4SChris Mason &root->root_item); 32254aa1f4dSChris Mason if (err) 32354aa1f4dSChris Mason break; 3249f3a7427SChris Mason 3259f3a7427SChris Mason refs = btrfs_root_refs(&dirty->root->root_item); 3269f3a7427SChris Mason btrfs_set_root_refs(&dirty->root->root_item, refs - 1); 3275eda7b5eSChris Mason err = btrfs_update_root(trans, root->fs_info->tree_root, 3289f3a7427SChris Mason &dirty->root->root_key, 3299f3a7427SChris Mason &dirty->root->root_item); 3305eda7b5eSChris Mason 3315eda7b5eSChris Mason BUG_ON(err); 3329f3a7427SChris Mason if (refs == 1) { 3330f7d52f4SChris Mason list_add(&dirty->list, list); 3349f3a7427SChris Mason } else { 3359f3a7427SChris Mason WARN_ON(1); 3369f3a7427SChris Mason kfree(dirty->root); 3375eda7b5eSChris Mason kfree(dirty); 3380f7d52f4SChris Mason } 3390f7d52f4SChris Mason } 3409f3a7427SChris Mason } 34154aa1f4dSChris Mason return err; 3420f7d52f4SChris Mason } 3430f7d52f4SChris Mason 344e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) 345e9d0b13bSChris Mason { 346e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 347e9d0b13bSChris Mason int ret; 348e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 349d3c2fdcfSChris Mason unsigned long nr; 350e9d0b13bSChris Mason 351e9d0b13bSChris Mason if (root->defrag_running) 352e9d0b13bSChris Mason return 0; 353e9d0b13bSChris Mason 354e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 355*f510cfecSChris Mason while (0) { 356e9d0b13bSChris Mason root->defrag_running = 1; 357e9d0b13bSChris Mason ret = btrfs_defrag_leaves(trans, root, cacheonly); 358d3c2fdcfSChris Mason nr = trans->blocks_used; 359e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 360e9d0b13bSChris Mason mutex_unlock(&info->fs_mutex); 361e9d0b13bSChris Mason 362d3c2fdcfSChris Mason btrfs_btree_balance_dirty(info->tree_root, nr); 363e9d0b13bSChris Mason cond_resched(); 364e9d0b13bSChris Mason 365e9d0b13bSChris Mason mutex_lock(&info->fs_mutex); 366e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 367e9d0b13bSChris Mason if (ret != -EAGAIN) 368e9d0b13bSChris Mason break; 369e9d0b13bSChris Mason } 370e9d0b13bSChris Mason root->defrag_running = 0; 371e9d0b13bSChris Mason radix_tree_tag_clear(&info->fs_roots_radix, 372e9d0b13bSChris Mason (unsigned long)root->root_key.objectid, 373e9d0b13bSChris Mason BTRFS_ROOT_DEFRAG_TAG); 374e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 375e9d0b13bSChris Mason return 0; 376e9d0b13bSChris Mason } 377e9d0b13bSChris Mason 3786702ed49SChris Mason int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info) 3796702ed49SChris Mason { 3806702ed49SChris Mason struct btrfs_root *gang[1]; 3816702ed49SChris Mason struct btrfs_root *root; 3826702ed49SChris Mason int i; 3836702ed49SChris Mason int ret; 3846702ed49SChris Mason int err = 0; 3856702ed49SChris Mason u64 last = 0; 3866702ed49SChris Mason 3876702ed49SChris Mason while(1) { 3886702ed49SChris Mason ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix, 3896702ed49SChris Mason (void **)gang, last, 3906702ed49SChris Mason ARRAY_SIZE(gang), 3916702ed49SChris Mason BTRFS_ROOT_DEFRAG_TAG); 3926702ed49SChris Mason if (ret == 0) 3936702ed49SChris Mason break; 3946702ed49SChris Mason for (i = 0; i < ret; i++) { 3956702ed49SChris Mason root = gang[i]; 3966702ed49SChris Mason last = root->root_key.objectid + 1; 397*f510cfecSChris Mason btrfs_defrag_root(root, 1); 3986702ed49SChris Mason } 3996702ed49SChris Mason } 4005f39d397SChris Mason // btrfs_defrag_root(info->extent_root, 1); 4016702ed49SChris Mason return err; 4026702ed49SChris Mason } 4036702ed49SChris Mason 40435b7e476SChris Mason static int drop_dirty_roots(struct btrfs_root *tree_root, 40535b7e476SChris Mason struct list_head *list) 4060f7d52f4SChris Mason { 4070f7d52f4SChris Mason struct dirty_root *dirty; 4080f7d52f4SChris Mason struct btrfs_trans_handle *trans; 409d3c2fdcfSChris Mason unsigned long nr; 41058176a96SJosef Bacik u64 num_blocks; 41158176a96SJosef Bacik u64 blocks_used; 41254aa1f4dSChris Mason int ret = 0; 4139f3a7427SChris Mason int err; 4149f3a7427SChris Mason 4150f7d52f4SChris Mason while(!list_empty(list)) { 41658176a96SJosef Bacik struct btrfs_root *root; 41758176a96SJosef Bacik 418facda1e7SChris Mason mutex_lock(&tree_root->fs_info->fs_mutex); 4190f7d52f4SChris Mason dirty = list_entry(list->next, struct dirty_root, list); 4200f7d52f4SChris Mason list_del_init(&dirty->list); 4215eda7b5eSChris Mason 4225f39d397SChris Mason num_blocks = btrfs_root_used(&dirty->root->root_item); 42358176a96SJosef Bacik root = dirty->latest_root; 42458176a96SJosef Bacik 4259f3a7427SChris Mason while(1) { 4260f7d52f4SChris Mason trans = btrfs_start_transaction(tree_root, 1); 4279f3a7427SChris Mason ret = btrfs_drop_snapshot(trans, dirty->root); 4289f3a7427SChris Mason if (ret != -EAGAIN) { 4299f3a7427SChris Mason break; 4309f3a7427SChris Mason } 43158176a96SJosef Bacik 4329f3a7427SChris Mason err = btrfs_update_root(trans, 4339f3a7427SChris Mason tree_root, 4349f3a7427SChris Mason &dirty->root->root_key, 4359f3a7427SChris Mason &dirty->root->root_item); 4369f3a7427SChris Mason if (err) 4379f3a7427SChris Mason ret = err; 438d3c2fdcfSChris Mason nr = trans->blocks_used; 4399f3a7427SChris Mason ret = btrfs_end_transaction(trans, tree_root); 4400f7d52f4SChris Mason BUG_ON(ret); 441f4468e94SChris Mason mutex_unlock(&tree_root->fs_info->fs_mutex); 442d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 443f4468e94SChris Mason schedule(); 444f4468e94SChris Mason 445f4468e94SChris Mason mutex_lock(&tree_root->fs_info->fs_mutex); 4469f3a7427SChris Mason } 4479f3a7427SChris Mason BUG_ON(ret); 44858176a96SJosef Bacik 4495f39d397SChris Mason num_blocks -= btrfs_root_used(&dirty->root->root_item); 4505f39d397SChris Mason blocks_used = btrfs_root_used(&root->root_item); 45158176a96SJosef Bacik if (num_blocks) { 45258176a96SJosef Bacik record_root_in_trans(root); 4535f39d397SChris Mason btrfs_set_root_used(&root->root_item, 45458176a96SJosef Bacik blocks_used - num_blocks); 45558176a96SJosef Bacik } 4569f3a7427SChris Mason ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); 45758176a96SJosef Bacik if (ret) { 45858176a96SJosef Bacik BUG(); 45954aa1f4dSChris Mason break; 46058176a96SJosef Bacik } 461d3c2fdcfSChris Mason nr = trans->blocks_used; 4620f7d52f4SChris Mason ret = btrfs_end_transaction(trans, tree_root); 4630f7d52f4SChris Mason BUG_ON(ret); 4645eda7b5eSChris Mason 465*f510cfecSChris Mason free_extent_buffer(dirty->root->node); 4665eda7b5eSChris Mason kfree(dirty->root); 4670f7d52f4SChris Mason kfree(dirty); 468facda1e7SChris Mason mutex_unlock(&tree_root->fs_info->fs_mutex); 469d3c2fdcfSChris Mason 470d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 471f4468e94SChris Mason schedule(); 4720f7d52f4SChris Mason } 47354aa1f4dSChris Mason return ret; 4740f7d52f4SChris Mason } 4750f7d52f4SChris Mason 47679154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 47779154b1bSChris Mason struct btrfs_root *root) 47879154b1bSChris Mason { 47915ee9bc7SJosef Bacik unsigned long joined = 0; 48015ee9bc7SJosef Bacik unsigned long timeout = 1; 48179154b1bSChris Mason struct btrfs_transaction *cur_trans; 4828fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 4830f7d52f4SChris Mason struct list_head dirty_fs_roots; 484ccd467d6SChris Mason struct radix_tree_root pinned_copy; 48579154b1bSChris Mason DEFINE_WAIT(wait); 48615ee9bc7SJosef Bacik int ret; 48779154b1bSChris Mason 488ccd467d6SChris Mason init_bit_radix(&pinned_copy); 4890f7d52f4SChris Mason INIT_LIST_HEAD(&dirty_fs_roots); 490d6e4a428SChris Mason 49179154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 49279154b1bSChris Mason if (trans->transaction->in_commit) { 49379154b1bSChris Mason cur_trans = trans->transaction; 49479154b1bSChris Mason trans->transaction->use_count++; 495ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 49679154b1bSChris Mason btrfs_end_transaction(trans, root); 497ccd467d6SChris Mason 498ccd467d6SChris Mason mutex_unlock(&root->fs_info->fs_mutex); 49979154b1bSChris Mason ret = wait_for_commit(root, cur_trans); 50079154b1bSChris Mason BUG_ON(ret); 50115ee9bc7SJosef Bacik 50215ee9bc7SJosef Bacik mutex_lock(&root->fs_info->trans_mutex); 50379154b1bSChris Mason put_transaction(cur_trans); 50415ee9bc7SJosef Bacik mutex_unlock(&root->fs_info->trans_mutex); 50515ee9bc7SJosef Bacik 506ccd467d6SChris Mason mutex_lock(&root->fs_info->fs_mutex); 50779154b1bSChris Mason return 0; 50879154b1bSChris Mason } 5092c90e5d6SChris Mason trans->transaction->in_commit = 1; 510ccd467d6SChris Mason cur_trans = trans->transaction; 511ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 512ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 513ccd467d6SChris Mason struct btrfs_transaction, list); 514ccd467d6SChris Mason if (!prev_trans->commit_done) { 515ccd467d6SChris Mason prev_trans->use_count++; 516ccd467d6SChris Mason mutex_unlock(&root->fs_info->fs_mutex); 517ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 518ccd467d6SChris Mason 519ccd467d6SChris Mason wait_for_commit(root, prev_trans); 520ccd467d6SChris Mason 521ccd467d6SChris Mason mutex_lock(&root->fs_info->fs_mutex); 522ccd467d6SChris Mason mutex_lock(&root->fs_info->trans_mutex); 52315ee9bc7SJosef Bacik put_transaction(prev_trans); 524ccd467d6SChris Mason } 525ccd467d6SChris Mason } 52615ee9bc7SJosef Bacik 52715ee9bc7SJosef Bacik do { 52815ee9bc7SJosef Bacik joined = cur_trans->num_joined; 5292c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 53015ee9bc7SJosef Bacik prepare_to_wait(&cur_trans->writer_wait, &wait, 53179154b1bSChris Mason TASK_UNINTERRUPTIBLE); 53215ee9bc7SJosef Bacik 53315ee9bc7SJosef Bacik if (cur_trans->num_writers > 1) 53415ee9bc7SJosef Bacik timeout = MAX_SCHEDULE_TIMEOUT; 53515ee9bc7SJosef Bacik else 53615ee9bc7SJosef Bacik timeout = 1; 53715ee9bc7SJosef Bacik 538ccd467d6SChris Mason mutex_unlock(&root->fs_info->fs_mutex); 53979154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 54015ee9bc7SJosef Bacik 54115ee9bc7SJosef Bacik schedule_timeout(timeout); 54215ee9bc7SJosef Bacik 543ccd467d6SChris Mason mutex_lock(&root->fs_info->fs_mutex); 54479154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 54515ee9bc7SJosef Bacik finish_wait(&cur_trans->writer_wait, &wait); 54615ee9bc7SJosef Bacik } while (cur_trans->num_writers > 1 || 54715ee9bc7SJosef Bacik (cur_trans->num_joined != joined)); 54815ee9bc7SJosef Bacik 5492c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 55054aa1f4dSChris Mason ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, 55154aa1f4dSChris Mason &dirty_fs_roots); 55254aa1f4dSChris Mason BUG_ON(ret); 55354aa1f4dSChris Mason 55479154b1bSChris Mason ret = btrfs_commit_tree_roots(trans, root); 55579154b1bSChris Mason BUG_ON(ret); 55654aa1f4dSChris Mason 55778fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 55878fae27eSChris Mason root->fs_info->running_transaction = NULL; 5594b52dff6SChris Mason btrfs_set_super_generation(&root->fs_info->super_copy, 5604b52dff6SChris Mason cur_trans->transid); 5614b52dff6SChris Mason btrfs_set_super_root(&root->fs_info->super_copy, 5625f39d397SChris Mason extent_buffer_blocknr(root->fs_info->tree_root->node)); 5635f39d397SChris Mason 5645f39d397SChris Mason write_extent_buffer(root->fs_info->sb_buffer, 5655f39d397SChris Mason &root->fs_info->super_copy, 0, 5664b52dff6SChris Mason sizeof(root->fs_info->super_copy)); 567ccd467d6SChris Mason 568ccd467d6SChris Mason btrfs_copy_pinned(root, &pinned_copy); 569ccd467d6SChris Mason 57078fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 5718fd17795SChris Mason mutex_unlock(&root->fs_info->fs_mutex); 57279154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 57379154b1bSChris Mason BUG_ON(ret); 57479154b1bSChris Mason write_ctree_super(trans, root); 5758fd17795SChris Mason mutex_lock(&root->fs_info->fs_mutex); 576ccd467d6SChris Mason btrfs_finish_extent_commit(trans, root, &pinned_copy); 57778fae27eSChris Mason mutex_lock(&root->fs_info->trans_mutex); 5782c90e5d6SChris Mason cur_trans->commit_done = 1; 57915ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 5802c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 58179154b1bSChris Mason put_transaction(cur_trans); 58278fae27eSChris Mason put_transaction(cur_trans); 58358176a96SJosef Bacik 584facda1e7SChris Mason if (root->fs_info->closing) 585facda1e7SChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); 586facda1e7SChris Mason else 587facda1e7SChris Mason list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); 58858176a96SJosef Bacik 58978fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 5902c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 59179154b1bSChris Mason 592facda1e7SChris Mason if (root->fs_info->closing) { 593facda1e7SChris Mason mutex_unlock(&root->fs_info->fs_mutex); 5940f7d52f4SChris Mason drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); 595facda1e7SChris Mason mutex_lock(&root->fs_info->fs_mutex); 596facda1e7SChris Mason } 59779154b1bSChris Mason return ret; 59879154b1bSChris Mason } 59979154b1bSChris Mason 600e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root) 601e9d0b13bSChris Mason { 602e9d0b13bSChris Mason struct list_head dirty_roots; 603e9d0b13bSChris Mason INIT_LIST_HEAD(&dirty_roots); 604e9d0b13bSChris Mason 605e9d0b13bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 606e9d0b13bSChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_roots); 607e9d0b13bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 608e9d0b13bSChris Mason 609e9d0b13bSChris Mason if (!list_empty(&dirty_roots)) { 610e9d0b13bSChris Mason drop_dirty_roots(root, &dirty_roots); 611e9d0b13bSChris Mason } 612e9d0b13bSChris Mason return 0; 613e9d0b13bSChris Mason } 61408607c1bSChris Mason void btrfs_transaction_cleaner(struct work_struct *work) 61508607c1bSChris Mason { 61608607c1bSChris Mason struct btrfs_fs_info *fs_info = container_of(work, 61708607c1bSChris Mason struct btrfs_fs_info, 61808607c1bSChris Mason trans_work.work); 61908607c1bSChris Mason 62008607c1bSChris Mason struct btrfs_root *root = fs_info->tree_root; 62108607c1bSChris Mason struct btrfs_transaction *cur; 62208607c1bSChris Mason struct btrfs_trans_handle *trans; 62308607c1bSChris Mason unsigned long now; 62408607c1bSChris Mason unsigned long delay = HZ * 30; 62508607c1bSChris Mason int ret; 62608607c1bSChris Mason 62708607c1bSChris Mason mutex_lock(&root->fs_info->fs_mutex); 62808607c1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 62908607c1bSChris Mason cur = root->fs_info->running_transaction; 63008607c1bSChris Mason if (!cur) { 63108607c1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 63208607c1bSChris Mason goto out; 63308607c1bSChris Mason } 63408607c1bSChris Mason now = get_seconds(); 63508607c1bSChris Mason if (now < cur->start_time || now - cur->start_time < 30) { 63608607c1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 63708607c1bSChris Mason delay = HZ * 5; 63808607c1bSChris Mason goto out; 63908607c1bSChris Mason } 64008607c1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 6416702ed49SChris Mason btrfs_defrag_dirty_roots(root->fs_info); 64208607c1bSChris Mason trans = btrfs_start_transaction(root, 1); 64308607c1bSChris Mason ret = btrfs_commit_transaction(trans, root); 64408607c1bSChris Mason out: 64508607c1bSChris Mason mutex_unlock(&root->fs_info->fs_mutex); 646e9d0b13bSChris Mason btrfs_clean_old_snapshots(root); 64708607c1bSChris Mason btrfs_transaction_queue_work(root, delay); 64808607c1bSChris Mason } 64908607c1bSChris Mason 65008607c1bSChris Mason void btrfs_transaction_queue_work(struct btrfs_root *root, int delay) 65108607c1bSChris Mason { 65208607c1bSChris Mason queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay); 65308607c1bSChris Mason } 65408607c1bSChris Mason 65508607c1bSChris Mason void btrfs_transaction_flush_work(struct btrfs_root *root) 65608607c1bSChris Mason { 65708607c1bSChris Mason cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work); 65808607c1bSChris Mason flush_workqueue(trans_wq); 65908607c1bSChris Mason } 66008607c1bSChris Mason 66108607c1bSChris Mason void __init btrfs_init_transaction_sys(void) 66208607c1bSChris Mason { 66308607c1bSChris Mason trans_wq = create_workqueue("btrfs"); 66408607c1bSChris Mason } 66508607c1bSChris Mason 66608607c1bSChris Mason void __exit btrfs_exit_transaction_sys(void) 66708607c1bSChris Mason { 66808607c1bSChris Mason destroy_workqueue(trans_wq); 66908607c1bSChris Mason } 67008607c1bSChris Mason 671