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) { 208db94535dSChris Mason old_extent_block = btrfs_root_bytenr(&extent_root->root_item); 209db94535dSChris Mason if (old_extent_block == extent_root->node->start) 21079154b1bSChris Mason break; 211db94535dSChris Mason btrfs_set_root_bytenr(&extent_root->root_item, 212db94535dSChris Mason extent_root->node->start); 213db94535dSChris Mason btrfs_set_root_level(&extent_root->root_item, 214db94535dSChris Mason btrfs_header_level(extent_root->node)); 21579154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 21679154b1bSChris Mason &extent_root->root_key, 21779154b1bSChris Mason &extent_root->root_item); 21879154b1bSChris Mason BUG_ON(ret); 2199078a3e1SChris Mason btrfs_write_dirty_block_groups(trans, extent_root); 22079154b1bSChris Mason } 22179154b1bSChris Mason return 0; 22279154b1bSChris Mason } 22379154b1bSChris Mason 22479154b1bSChris Mason static int wait_for_commit(struct btrfs_root *root, 22579154b1bSChris Mason struct btrfs_transaction *commit) 22679154b1bSChris Mason { 22779154b1bSChris Mason DEFINE_WAIT(wait); 228ccd467d6SChris Mason mutex_lock(&root->fs_info->trans_mutex); 22979154b1bSChris Mason while(!commit->commit_done) { 23079154b1bSChris Mason prepare_to_wait(&commit->commit_wait, &wait, 23179154b1bSChris Mason TASK_UNINTERRUPTIBLE); 23279154b1bSChris Mason if (commit->commit_done) 23379154b1bSChris Mason break; 23479154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 23579154b1bSChris Mason schedule(); 23679154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 23779154b1bSChris Mason } 238ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 23979154b1bSChris Mason finish_wait(&commit->commit_wait, &wait); 24079154b1bSChris Mason return 0; 24179154b1bSChris Mason } 24279154b1bSChris Mason 2430f7d52f4SChris Mason struct dirty_root { 2440f7d52f4SChris Mason struct list_head list; 2450f7d52f4SChris Mason struct btrfs_root *root; 24658176a96SJosef Bacik struct btrfs_root *latest_root; 2470f7d52f4SChris Mason }; 2480f7d52f4SChris Mason 2495ce14bbcSChris Mason int btrfs_add_dead_root(struct btrfs_root *root, 2505ce14bbcSChris Mason struct btrfs_root *latest, 2515ce14bbcSChris Mason struct list_head *dead_list) 2525eda7b5eSChris Mason { 2535eda7b5eSChris Mason struct dirty_root *dirty; 2545eda7b5eSChris Mason 2555eda7b5eSChris Mason dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 2565eda7b5eSChris Mason if (!dirty) 2575eda7b5eSChris Mason return -ENOMEM; 2585eda7b5eSChris Mason dirty->root = root; 2595ce14bbcSChris Mason dirty->latest_root = latest; 2605eda7b5eSChris Mason list_add(&dirty->list, dead_list); 2615eda7b5eSChris Mason return 0; 2625eda7b5eSChris Mason } 2635eda7b5eSChris Mason 26435b7e476SChris Mason static int add_dirty_roots(struct btrfs_trans_handle *trans, 26535b7e476SChris Mason struct radix_tree_root *radix, 26635b7e476SChris Mason struct list_head *list) 2670f7d52f4SChris Mason { 2680f7d52f4SChris Mason struct dirty_root *dirty; 2690f7d52f4SChris Mason struct btrfs_root *gang[8]; 2700f7d52f4SChris Mason struct btrfs_root *root; 2710f7d52f4SChris Mason int i; 2720f7d52f4SChris Mason int ret; 27354aa1f4dSChris Mason int err = 0; 2745eda7b5eSChris Mason u32 refs; 27554aa1f4dSChris Mason 2760f7d52f4SChris Mason while(1) { 2770f7d52f4SChris Mason ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, 2780f7d52f4SChris Mason ARRAY_SIZE(gang), 2790f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 2800f7d52f4SChris Mason if (ret == 0) 2810f7d52f4SChris Mason break; 2820f7d52f4SChris Mason for (i = 0; i < ret; i++) { 2830f7d52f4SChris Mason root = gang[i]; 2842619ba1fSChris Mason radix_tree_tag_clear(radix, 2852619ba1fSChris Mason (unsigned long)root->root_key.objectid, 2860f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 2870f7d52f4SChris Mason if (root->commit_root == root->node) { 288db94535dSChris Mason WARN_ON(root->node->start != 289db94535dSChris Mason btrfs_root_bytenr(&root->root_item)); 2905f39d397SChris Mason free_extent_buffer(root->commit_root); 2910f7d52f4SChris Mason root->commit_root = NULL; 29258176a96SJosef Bacik 29358176a96SJosef Bacik /* make sure to update the root on disk 29458176a96SJosef Bacik * so we get any updates to the block used 29558176a96SJosef Bacik * counts 29658176a96SJosef Bacik */ 29758176a96SJosef Bacik err = btrfs_update_root(trans, 29858176a96SJosef Bacik root->fs_info->tree_root, 29958176a96SJosef Bacik &root->root_key, 30058176a96SJosef Bacik &root->root_item); 3010f7d52f4SChris Mason continue; 3020f7d52f4SChris Mason } 3030f7d52f4SChris Mason dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 3040f7d52f4SChris Mason BUG_ON(!dirty); 3059f3a7427SChris Mason dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS); 3069f3a7427SChris Mason BUG_ON(!dirty->root); 3079f3a7427SChris Mason 3089f3a7427SChris Mason memset(&root->root_item.drop_progress, 0, 3099f3a7427SChris Mason sizeof(struct btrfs_disk_key)); 3109f3a7427SChris Mason root->root_item.drop_level = 0; 3119f3a7427SChris Mason 3129f3a7427SChris Mason memcpy(dirty->root, root, sizeof(*root)); 3139f3a7427SChris Mason dirty->root->node = root->commit_root; 31458176a96SJosef Bacik dirty->latest_root = root; 3150f7d52f4SChris Mason root->commit_root = NULL; 3165eda7b5eSChris Mason 3170f7d52f4SChris Mason root->root_key.offset = root->fs_info->generation; 318db94535dSChris Mason btrfs_set_root_bytenr(&root->root_item, 319db94535dSChris Mason root->node->start); 320db94535dSChris Mason btrfs_set_root_level(&root->root_item, 321db94535dSChris Mason btrfs_header_level(root->node)); 3220f7d52f4SChris Mason err = btrfs_insert_root(trans, root->fs_info->tree_root, 3230f7d52f4SChris Mason &root->root_key, 3240f7d52f4SChris Mason &root->root_item); 32554aa1f4dSChris Mason if (err) 32654aa1f4dSChris Mason break; 3279f3a7427SChris Mason 3289f3a7427SChris Mason refs = btrfs_root_refs(&dirty->root->root_item); 3299f3a7427SChris Mason btrfs_set_root_refs(&dirty->root->root_item, refs - 1); 3305eda7b5eSChris Mason err = btrfs_update_root(trans, root->fs_info->tree_root, 3319f3a7427SChris Mason &dirty->root->root_key, 3329f3a7427SChris Mason &dirty->root->root_item); 3335eda7b5eSChris Mason 3345eda7b5eSChris Mason BUG_ON(err); 3359f3a7427SChris Mason if (refs == 1) { 3360f7d52f4SChris Mason list_add(&dirty->list, list); 3379f3a7427SChris Mason } else { 3389f3a7427SChris Mason WARN_ON(1); 3399f3a7427SChris Mason kfree(dirty->root); 3405eda7b5eSChris Mason kfree(dirty); 3410f7d52f4SChris Mason } 3420f7d52f4SChris Mason } 3439f3a7427SChris Mason } 34454aa1f4dSChris Mason return err; 3450f7d52f4SChris Mason } 3460f7d52f4SChris Mason 347e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) 348e9d0b13bSChris Mason { 349e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 350e9d0b13bSChris Mason int ret; 351e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 352d3c2fdcfSChris Mason unsigned long nr; 353e9d0b13bSChris Mason 354e9d0b13bSChris Mason if (root->defrag_running) 355e9d0b13bSChris Mason return 0; 356e9d0b13bSChris Mason 357e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 3586b80053dSChris Mason while (1) { 359e9d0b13bSChris Mason root->defrag_running = 1; 360e9d0b13bSChris Mason ret = btrfs_defrag_leaves(trans, root, cacheonly); 361d3c2fdcfSChris Mason nr = trans->blocks_used; 362e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 363e9d0b13bSChris Mason mutex_unlock(&info->fs_mutex); 364e9d0b13bSChris Mason 365d3c2fdcfSChris Mason btrfs_btree_balance_dirty(info->tree_root, nr); 366e9d0b13bSChris Mason cond_resched(); 367e9d0b13bSChris Mason 368e9d0b13bSChris Mason mutex_lock(&info->fs_mutex); 369e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 370e9d0b13bSChris Mason if (ret != -EAGAIN) 371e9d0b13bSChris Mason break; 372e9d0b13bSChris Mason } 373e9d0b13bSChris Mason root->defrag_running = 0; 374e9d0b13bSChris Mason radix_tree_tag_clear(&info->fs_roots_radix, 375e9d0b13bSChris Mason (unsigned long)root->root_key.objectid, 376e9d0b13bSChris Mason BTRFS_ROOT_DEFRAG_TAG); 377e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 378e9d0b13bSChris Mason return 0; 379e9d0b13bSChris Mason } 380e9d0b13bSChris Mason 3816702ed49SChris Mason int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info) 3826702ed49SChris Mason { 3836702ed49SChris Mason struct btrfs_root *gang[1]; 3846702ed49SChris Mason struct btrfs_root *root; 3856702ed49SChris Mason int i; 3866702ed49SChris Mason int ret; 3876702ed49SChris Mason int err = 0; 3886702ed49SChris Mason u64 last = 0; 3896702ed49SChris Mason 3906702ed49SChris Mason while(1) { 3916702ed49SChris Mason ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix, 3926702ed49SChris Mason (void **)gang, last, 3936702ed49SChris Mason ARRAY_SIZE(gang), 3946702ed49SChris Mason BTRFS_ROOT_DEFRAG_TAG); 3956702ed49SChris Mason if (ret == 0) 3966702ed49SChris Mason break; 3976702ed49SChris Mason for (i = 0; i < ret; i++) { 3986702ed49SChris Mason root = gang[i]; 3996702ed49SChris Mason last = root->root_key.objectid + 1; 400f510cfecSChris Mason btrfs_defrag_root(root, 1); 4016702ed49SChris Mason } 4026702ed49SChris Mason } 4036b80053dSChris Mason btrfs_defrag_root(info->extent_root, 1); 4046702ed49SChris Mason return err; 4056702ed49SChris Mason } 4066702ed49SChris Mason 40735b7e476SChris Mason static int drop_dirty_roots(struct btrfs_root *tree_root, 40835b7e476SChris Mason struct list_head *list) 4090f7d52f4SChris Mason { 4100f7d52f4SChris Mason struct dirty_root *dirty; 4110f7d52f4SChris Mason struct btrfs_trans_handle *trans; 412d3c2fdcfSChris Mason unsigned long nr; 413db94535dSChris Mason u64 num_bytes; 414db94535dSChris Mason u64 bytes_used; 41554aa1f4dSChris Mason int ret = 0; 4169f3a7427SChris Mason int err; 4179f3a7427SChris Mason 4180f7d52f4SChris Mason while(!list_empty(list)) { 41958176a96SJosef Bacik struct btrfs_root *root; 42058176a96SJosef Bacik 421facda1e7SChris Mason mutex_lock(&tree_root->fs_info->fs_mutex); 4220f7d52f4SChris Mason dirty = list_entry(list->next, struct dirty_root, list); 4230f7d52f4SChris Mason list_del_init(&dirty->list); 4245eda7b5eSChris Mason 425db94535dSChris Mason num_bytes = btrfs_root_used(&dirty->root->root_item); 42658176a96SJosef Bacik root = dirty->latest_root; 42758176a96SJosef Bacik 4289f3a7427SChris Mason while(1) { 4290f7d52f4SChris Mason trans = btrfs_start_transaction(tree_root, 1); 4309f3a7427SChris Mason ret = btrfs_drop_snapshot(trans, dirty->root); 4319f3a7427SChris Mason if (ret != -EAGAIN) { 4329f3a7427SChris Mason break; 4339f3a7427SChris Mason } 43458176a96SJosef Bacik 4359f3a7427SChris Mason err = btrfs_update_root(trans, 4369f3a7427SChris Mason tree_root, 4379f3a7427SChris Mason &dirty->root->root_key, 4389f3a7427SChris Mason &dirty->root->root_item); 4399f3a7427SChris Mason if (err) 4409f3a7427SChris Mason ret = err; 441d3c2fdcfSChris Mason nr = trans->blocks_used; 4429f3a7427SChris Mason ret = btrfs_end_transaction(trans, tree_root); 4430f7d52f4SChris Mason BUG_ON(ret); 444f4468e94SChris Mason mutex_unlock(&tree_root->fs_info->fs_mutex); 445d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 446*4dc11904SChris Mason cond_resched(); 447f4468e94SChris Mason mutex_lock(&tree_root->fs_info->fs_mutex); 4489f3a7427SChris Mason } 4499f3a7427SChris Mason BUG_ON(ret); 45058176a96SJosef Bacik 451db94535dSChris Mason num_bytes -= btrfs_root_used(&dirty->root->root_item); 452db94535dSChris Mason bytes_used = btrfs_root_used(&root->root_item); 453db94535dSChris Mason if (num_bytes) { 45458176a96SJosef Bacik record_root_in_trans(root); 4555f39d397SChris Mason btrfs_set_root_used(&root->root_item, 456db94535dSChris Mason bytes_used - num_bytes); 45758176a96SJosef Bacik } 4589f3a7427SChris Mason ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); 45958176a96SJosef Bacik if (ret) { 46058176a96SJosef Bacik BUG(); 46154aa1f4dSChris Mason break; 46258176a96SJosef Bacik } 463d3c2fdcfSChris Mason nr = trans->blocks_used; 4640f7d52f4SChris Mason ret = btrfs_end_transaction(trans, tree_root); 4650f7d52f4SChris Mason BUG_ON(ret); 4665eda7b5eSChris Mason 467f510cfecSChris Mason free_extent_buffer(dirty->root->node); 4685eda7b5eSChris Mason kfree(dirty->root); 4690f7d52f4SChris Mason kfree(dirty); 470facda1e7SChris Mason mutex_unlock(&tree_root->fs_info->fs_mutex); 471d3c2fdcfSChris Mason 472d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 473*4dc11904SChris Mason cond_resched(); 4740f7d52f4SChris Mason } 47554aa1f4dSChris Mason return ret; 4760f7d52f4SChris Mason } 4770f7d52f4SChris Mason 47879154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 47979154b1bSChris Mason struct btrfs_root *root) 48079154b1bSChris Mason { 48115ee9bc7SJosef Bacik unsigned long joined = 0; 48215ee9bc7SJosef Bacik unsigned long timeout = 1; 48379154b1bSChris Mason struct btrfs_transaction *cur_trans; 4848fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 4850f7d52f4SChris Mason struct list_head dirty_fs_roots; 4861a5bc167SChris Mason struct extent_map_tree pinned_copy; 48779154b1bSChris Mason DEFINE_WAIT(wait); 48815ee9bc7SJosef Bacik int ret; 48979154b1bSChris Mason 4901a5bc167SChris Mason extent_map_tree_init(&pinned_copy, 4911a5bc167SChris Mason root->fs_info->btree_inode->i_mapping, GFP_NOFS); 4920f7d52f4SChris Mason INIT_LIST_HEAD(&dirty_fs_roots); 493d6e4a428SChris Mason 49479154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 49579154b1bSChris Mason if (trans->transaction->in_commit) { 49679154b1bSChris Mason cur_trans = trans->transaction; 49779154b1bSChris Mason trans->transaction->use_count++; 498ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 49979154b1bSChris Mason btrfs_end_transaction(trans, root); 500ccd467d6SChris Mason 501ccd467d6SChris Mason mutex_unlock(&root->fs_info->fs_mutex); 50279154b1bSChris Mason ret = wait_for_commit(root, cur_trans); 50379154b1bSChris Mason BUG_ON(ret); 50415ee9bc7SJosef Bacik 50515ee9bc7SJosef Bacik mutex_lock(&root->fs_info->trans_mutex); 50679154b1bSChris Mason put_transaction(cur_trans); 50715ee9bc7SJosef Bacik mutex_unlock(&root->fs_info->trans_mutex); 50815ee9bc7SJosef Bacik 509ccd467d6SChris Mason mutex_lock(&root->fs_info->fs_mutex); 51079154b1bSChris Mason return 0; 51179154b1bSChris Mason } 5122c90e5d6SChris Mason trans->transaction->in_commit = 1; 513ccd467d6SChris Mason cur_trans = trans->transaction; 514ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 515ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 516ccd467d6SChris Mason struct btrfs_transaction, list); 517ccd467d6SChris Mason if (!prev_trans->commit_done) { 518ccd467d6SChris Mason prev_trans->use_count++; 519ccd467d6SChris Mason mutex_unlock(&root->fs_info->fs_mutex); 520ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 521ccd467d6SChris Mason 522ccd467d6SChris Mason wait_for_commit(root, prev_trans); 523ccd467d6SChris Mason 524ccd467d6SChris Mason mutex_lock(&root->fs_info->fs_mutex); 525ccd467d6SChris Mason mutex_lock(&root->fs_info->trans_mutex); 52615ee9bc7SJosef Bacik put_transaction(prev_trans); 527ccd467d6SChris Mason } 528ccd467d6SChris Mason } 52915ee9bc7SJosef Bacik 53015ee9bc7SJosef Bacik do { 53115ee9bc7SJosef Bacik joined = cur_trans->num_joined; 5322c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 53315ee9bc7SJosef Bacik prepare_to_wait(&cur_trans->writer_wait, &wait, 53479154b1bSChris Mason TASK_UNINTERRUPTIBLE); 53515ee9bc7SJosef Bacik 53615ee9bc7SJosef Bacik if (cur_trans->num_writers > 1) 53715ee9bc7SJosef Bacik timeout = MAX_SCHEDULE_TIMEOUT; 53815ee9bc7SJosef Bacik else 53915ee9bc7SJosef Bacik timeout = 1; 54015ee9bc7SJosef Bacik 541ccd467d6SChris Mason mutex_unlock(&root->fs_info->fs_mutex); 54279154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 54315ee9bc7SJosef Bacik 54415ee9bc7SJosef Bacik schedule_timeout(timeout); 54515ee9bc7SJosef Bacik 546ccd467d6SChris Mason mutex_lock(&root->fs_info->fs_mutex); 54779154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 54815ee9bc7SJosef Bacik finish_wait(&cur_trans->writer_wait, &wait); 54915ee9bc7SJosef Bacik } while (cur_trans->num_writers > 1 || 55015ee9bc7SJosef Bacik (cur_trans->num_joined != joined)); 55115ee9bc7SJosef Bacik 5522c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 55354aa1f4dSChris Mason ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, 55454aa1f4dSChris Mason &dirty_fs_roots); 55554aa1f4dSChris Mason BUG_ON(ret); 55654aa1f4dSChris Mason 55779154b1bSChris Mason ret = btrfs_commit_tree_roots(trans, root); 55879154b1bSChris Mason BUG_ON(ret); 55954aa1f4dSChris Mason 56078fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 56178fae27eSChris Mason root->fs_info->running_transaction = NULL; 5624b52dff6SChris Mason btrfs_set_super_generation(&root->fs_info->super_copy, 5634b52dff6SChris Mason cur_trans->transid); 5644b52dff6SChris Mason btrfs_set_super_root(&root->fs_info->super_copy, 565db94535dSChris Mason root->fs_info->tree_root->node->start); 566db94535dSChris Mason btrfs_set_super_root_level(&root->fs_info->super_copy, 567db94535dSChris Mason btrfs_header_level(root->fs_info->tree_root->node)); 5685f39d397SChris Mason 5695f39d397SChris Mason write_extent_buffer(root->fs_info->sb_buffer, 5705f39d397SChris Mason &root->fs_info->super_copy, 0, 5714b52dff6SChris Mason sizeof(root->fs_info->super_copy)); 572ccd467d6SChris Mason 573ccd467d6SChris Mason btrfs_copy_pinned(root, &pinned_copy); 574ccd467d6SChris Mason 57578fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 5768fd17795SChris Mason mutex_unlock(&root->fs_info->fs_mutex); 57779154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 57879154b1bSChris Mason BUG_ON(ret); 57979154b1bSChris Mason write_ctree_super(trans, root); 5808fd17795SChris Mason mutex_lock(&root->fs_info->fs_mutex); 581ccd467d6SChris Mason btrfs_finish_extent_commit(trans, root, &pinned_copy); 58278fae27eSChris Mason mutex_lock(&root->fs_info->trans_mutex); 5832c90e5d6SChris Mason cur_trans->commit_done = 1; 58415ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 5852c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 58679154b1bSChris Mason put_transaction(cur_trans); 58778fae27eSChris Mason put_transaction(cur_trans); 58858176a96SJosef Bacik 589facda1e7SChris Mason if (root->fs_info->closing) 590facda1e7SChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); 591facda1e7SChris Mason else 592facda1e7SChris Mason list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); 59358176a96SJosef Bacik 59478fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 5952c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 59679154b1bSChris Mason 597facda1e7SChris Mason if (root->fs_info->closing) { 598facda1e7SChris Mason mutex_unlock(&root->fs_info->fs_mutex); 5990f7d52f4SChris Mason drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); 600facda1e7SChris Mason mutex_lock(&root->fs_info->fs_mutex); 601facda1e7SChris Mason } 60279154b1bSChris Mason return ret; 60379154b1bSChris Mason } 60479154b1bSChris Mason 605e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root) 606e9d0b13bSChris Mason { 607e9d0b13bSChris Mason struct list_head dirty_roots; 608e9d0b13bSChris Mason INIT_LIST_HEAD(&dirty_roots); 609e9d0b13bSChris Mason 610e9d0b13bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 611e9d0b13bSChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_roots); 612e9d0b13bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 613e9d0b13bSChris Mason 614e9d0b13bSChris Mason if (!list_empty(&dirty_roots)) { 615e9d0b13bSChris Mason drop_dirty_roots(root, &dirty_roots); 616e9d0b13bSChris Mason } 617e9d0b13bSChris Mason return 0; 618e9d0b13bSChris Mason } 61908607c1bSChris Mason void btrfs_transaction_cleaner(struct work_struct *work) 62008607c1bSChris Mason { 62108607c1bSChris Mason struct btrfs_fs_info *fs_info = container_of(work, 62208607c1bSChris Mason struct btrfs_fs_info, 62308607c1bSChris Mason trans_work.work); 62408607c1bSChris Mason 62508607c1bSChris Mason struct btrfs_root *root = fs_info->tree_root; 62608607c1bSChris Mason struct btrfs_transaction *cur; 62708607c1bSChris Mason struct btrfs_trans_handle *trans; 62808607c1bSChris Mason unsigned long now; 62908607c1bSChris Mason unsigned long delay = HZ * 30; 63008607c1bSChris Mason int ret; 63108607c1bSChris Mason 63208607c1bSChris Mason mutex_lock(&root->fs_info->fs_mutex); 63308607c1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 63408607c1bSChris Mason cur = root->fs_info->running_transaction; 63508607c1bSChris Mason if (!cur) { 63608607c1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 63708607c1bSChris Mason goto out; 63808607c1bSChris Mason } 63908607c1bSChris Mason now = get_seconds(); 64008607c1bSChris Mason if (now < cur->start_time || now - cur->start_time < 30) { 64108607c1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 64208607c1bSChris Mason delay = HZ * 5; 64308607c1bSChris Mason goto out; 64408607c1bSChris Mason } 64508607c1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 6466702ed49SChris Mason btrfs_defrag_dirty_roots(root->fs_info); 64708607c1bSChris Mason trans = btrfs_start_transaction(root, 1); 64808607c1bSChris Mason ret = btrfs_commit_transaction(trans, root); 64908607c1bSChris Mason out: 65008607c1bSChris Mason mutex_unlock(&root->fs_info->fs_mutex); 651e9d0b13bSChris Mason btrfs_clean_old_snapshots(root); 65208607c1bSChris Mason btrfs_transaction_queue_work(root, delay); 65308607c1bSChris Mason } 65408607c1bSChris Mason 65508607c1bSChris Mason void btrfs_transaction_queue_work(struct btrfs_root *root, int delay) 65608607c1bSChris Mason { 65708607c1bSChris Mason queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay); 65808607c1bSChris Mason } 65908607c1bSChris Mason 66008607c1bSChris Mason void btrfs_transaction_flush_work(struct btrfs_root *root) 66108607c1bSChris Mason { 66208607c1bSChris Mason cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work); 66308607c1bSChris Mason flush_workqueue(trans_wq); 66408607c1bSChris Mason } 66508607c1bSChris Mason 66608607c1bSChris Mason void __init btrfs_init_transaction_sys(void) 66708607c1bSChris Mason { 66808607c1bSChris Mason trans_wq = create_workqueue("btrfs"); 66908607c1bSChris Mason } 67008607c1bSChris Mason 67108607c1bSChris Mason void __exit btrfs_exit_transaction_sys(void) 67208607c1bSChris Mason { 67308607c1bSChris Mason destroy_workqueue(trans_wq); 67408607c1bSChris Mason } 67508607c1bSChris Mason 676