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; 60e18e4809SChris Mason root->fs_info->last_alloc = 0; 61*4529ba49SChris Mason root->fs_info->last_data_alloc = 0; 6215ee9bc7SJosef Bacik cur_trans->num_writers = 1; 6315ee9bc7SJosef Bacik cur_trans->num_joined = 0; 640f7d52f4SChris Mason cur_trans->transid = root->fs_info->generation; 6579154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 6679154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 6779154b1bSChris Mason cur_trans->in_commit = 0; 68d5719762SChris Mason cur_trans->use_count = 1; 6979154b1bSChris Mason cur_trans->commit_done = 0; 7008607c1bSChris Mason cur_trans->start_time = get_seconds(); 713063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 728fd17795SChris Mason list_add_tail(&cur_trans->list, &root->fs_info->trans_list); 73dc17ff8fSChris Mason btrfs_ordered_inode_tree_init(&cur_trans->ordered_inode_tree); 74d1310b2eSChris Mason extent_io_tree_init(&cur_trans->dirty_pages, 755f39d397SChris Mason root->fs_info->btree_inode->i_mapping, 765f39d397SChris Mason GFP_NOFS); 7715ee9bc7SJosef Bacik } else { 7879154b1bSChris Mason cur_trans->num_writers++; 7915ee9bc7SJosef Bacik cur_trans->num_joined++; 8015ee9bc7SJosef Bacik } 8115ee9bc7SJosef Bacik 8279154b1bSChris Mason return 0; 8379154b1bSChris Mason } 8479154b1bSChris Mason 856702ed49SChris Mason static int record_root_in_trans(struct btrfs_root *root) 866702ed49SChris Mason { 876702ed49SChris Mason u64 running_trans_id = root->fs_info->running_transaction->transid; 886702ed49SChris Mason if (root->ref_cows && root->last_trans < running_trans_id) { 896702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 906702ed49SChris Mason if (root->root_item.refs != 0) { 916702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 926702ed49SChris Mason (unsigned long)root->root_key.objectid, 936702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 946702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 956702ed49SChris Mason (unsigned long)root->root_key.objectid, 966702ed49SChris Mason BTRFS_ROOT_DEFRAG_TAG); 976702ed49SChris Mason root->commit_root = root->node; 985f39d397SChris Mason extent_buffer_get(root->node); 996702ed49SChris Mason } else { 1006702ed49SChris Mason WARN_ON(1); 1016702ed49SChris Mason } 1026702ed49SChris Mason root->last_trans = running_trans_id; 1036702ed49SChris Mason } 1046702ed49SChris Mason return 0; 1056702ed49SChris Mason } 1066702ed49SChris Mason 10779154b1bSChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 10879154b1bSChris Mason int num_blocks) 10979154b1bSChris Mason { 1102c90e5d6SChris Mason struct btrfs_trans_handle *h = 1112c90e5d6SChris Mason kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 11279154b1bSChris Mason int ret; 11379154b1bSChris Mason 11479154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 11579154b1bSChris Mason ret = join_transaction(root); 11679154b1bSChris Mason BUG_ON(ret); 1170f7d52f4SChris Mason 1186702ed49SChris Mason record_root_in_trans(root); 1196702ed49SChris Mason h->transid = root->fs_info->running_transaction->transid; 12079154b1bSChris Mason h->transaction = root->fs_info->running_transaction; 12179154b1bSChris Mason h->blocks_reserved = num_blocks; 12279154b1bSChris Mason h->blocks_used = 0; 12331f3c99bSChris Mason h->block_group = NULL; 12426b8003fSChris Mason h->alloc_exclude_nr = 0; 12526b8003fSChris Mason h->alloc_exclude_start = 0; 12679154b1bSChris Mason root->fs_info->running_transaction->use_count++; 12779154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 12879154b1bSChris Mason return h; 12979154b1bSChris Mason } 13079154b1bSChris Mason 13179154b1bSChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 13279154b1bSChris Mason struct btrfs_root *root) 13379154b1bSChris Mason { 13479154b1bSChris Mason struct btrfs_transaction *cur_trans; 135d6e4a428SChris Mason 13679154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 13779154b1bSChris Mason cur_trans = root->fs_info->running_transaction; 138ccd467d6SChris Mason WARN_ON(cur_trans != trans->transaction); 139d5719762SChris Mason WARN_ON(cur_trans->num_writers < 1); 140ccd467d6SChris Mason cur_trans->num_writers--; 14179154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 14279154b1bSChris Mason wake_up(&cur_trans->writer_wait); 14379154b1bSChris Mason put_transaction(cur_trans); 14479154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 145d6025579SChris Mason memset(trans, 0, sizeof(*trans)); 1462c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 14779154b1bSChris Mason return 0; 14879154b1bSChris Mason } 14979154b1bSChris Mason 15079154b1bSChris Mason 15179154b1bSChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 15279154b1bSChris Mason struct btrfs_root *root) 15379154b1bSChris Mason { 1547c4452b9SChris Mason int ret; 1557c4452b9SChris Mason int err; 1567c4452b9SChris Mason int werr = 0; 157d1310b2eSChris Mason struct extent_io_tree *dirty_pages; 1587c4452b9SChris Mason struct page *page; 1597c4452b9SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 1605f39d397SChris Mason u64 start; 1615f39d397SChris Mason u64 end; 1625f39d397SChris Mason unsigned long index; 1637c4452b9SChris Mason 1647c4452b9SChris Mason if (!trans || !trans->transaction) { 1657c4452b9SChris Mason return filemap_write_and_wait(btree_inode->i_mapping); 1667c4452b9SChris Mason } 1677c4452b9SChris Mason dirty_pages = &trans->transaction->dirty_pages; 1687c4452b9SChris Mason while(1) { 1695f39d397SChris Mason ret = find_first_extent_bit(dirty_pages, 0, &start, &end, 1705f39d397SChris Mason EXTENT_DIRTY); 1715f39d397SChris Mason if (ret) 1727c4452b9SChris Mason break; 1735f39d397SChris Mason clear_extent_dirty(dirty_pages, start, end, GFP_NOFS); 1745f39d397SChris Mason while(start <= end) { 1755f39d397SChris Mason index = start >> PAGE_CACHE_SHIFT; 17635ebb934SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 1775f39d397SChris Mason page = find_lock_page(btree_inode->i_mapping, index); 1787c4452b9SChris Mason if (!page) 1797c4452b9SChris Mason continue; 1806702ed49SChris Mason if (PageWriteback(page)) { 1816702ed49SChris Mason if (PageDirty(page)) 1826702ed49SChris Mason wait_on_page_writeback(page); 1836702ed49SChris Mason else { 1846702ed49SChris Mason unlock_page(page); 1856702ed49SChris Mason page_cache_release(page); 1866702ed49SChris Mason continue; 1876702ed49SChris Mason } 1886702ed49SChris Mason } 1897c4452b9SChris Mason err = write_one_page(page, 0); 1907c4452b9SChris Mason if (err) 1917c4452b9SChris Mason werr = err; 1927c4452b9SChris Mason page_cache_release(page); 1937c4452b9SChris Mason } 1947c4452b9SChris Mason } 1957c4452b9SChris Mason err = filemap_fdatawait(btree_inode->i_mapping); 1967c4452b9SChris Mason if (err) 1977c4452b9SChris Mason werr = err; 1987c4452b9SChris Mason return werr; 19979154b1bSChris Mason } 20079154b1bSChris Mason 20179154b1bSChris Mason int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, 20279154b1bSChris Mason struct btrfs_root *root) 20379154b1bSChris Mason { 20479154b1bSChris Mason int ret; 20579154b1bSChris Mason u64 old_extent_block; 20679154b1bSChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 20779154b1bSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 20879154b1bSChris Mason struct btrfs_root *extent_root = fs_info->extent_root; 20979154b1bSChris Mason 2109078a3e1SChris Mason btrfs_write_dirty_block_groups(trans, extent_root); 21179154b1bSChris Mason while(1) { 212db94535dSChris Mason old_extent_block = btrfs_root_bytenr(&extent_root->root_item); 213db94535dSChris Mason if (old_extent_block == extent_root->node->start) 21479154b1bSChris Mason break; 215db94535dSChris Mason btrfs_set_root_bytenr(&extent_root->root_item, 216db94535dSChris Mason extent_root->node->start); 217db94535dSChris Mason btrfs_set_root_level(&extent_root->root_item, 218db94535dSChris Mason btrfs_header_level(extent_root->node)); 21979154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 22079154b1bSChris Mason &extent_root->root_key, 22179154b1bSChris Mason &extent_root->root_item); 22279154b1bSChris Mason BUG_ON(ret); 2239078a3e1SChris Mason btrfs_write_dirty_block_groups(trans, extent_root); 22479154b1bSChris Mason } 22579154b1bSChris Mason return 0; 22679154b1bSChris Mason } 22779154b1bSChris Mason 22879154b1bSChris Mason static int wait_for_commit(struct btrfs_root *root, 22979154b1bSChris Mason struct btrfs_transaction *commit) 23079154b1bSChris Mason { 23179154b1bSChris Mason DEFINE_WAIT(wait); 232ccd467d6SChris Mason mutex_lock(&root->fs_info->trans_mutex); 23379154b1bSChris Mason while(!commit->commit_done) { 23479154b1bSChris Mason prepare_to_wait(&commit->commit_wait, &wait, 23579154b1bSChris Mason TASK_UNINTERRUPTIBLE); 23679154b1bSChris Mason if (commit->commit_done) 23779154b1bSChris Mason break; 23879154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 23979154b1bSChris Mason schedule(); 24079154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 24179154b1bSChris Mason } 242ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 24379154b1bSChris Mason finish_wait(&commit->commit_wait, &wait); 24479154b1bSChris Mason return 0; 24579154b1bSChris Mason } 24679154b1bSChris Mason 2470f7d52f4SChris Mason struct dirty_root { 2480f7d52f4SChris Mason struct list_head list; 2490f7d52f4SChris Mason struct btrfs_root *root; 25058176a96SJosef Bacik struct btrfs_root *latest_root; 2510f7d52f4SChris Mason }; 2520f7d52f4SChris Mason 2535ce14bbcSChris Mason int btrfs_add_dead_root(struct btrfs_root *root, 2545ce14bbcSChris Mason struct btrfs_root *latest, 2555ce14bbcSChris Mason struct list_head *dead_list) 2565eda7b5eSChris Mason { 2575eda7b5eSChris Mason struct dirty_root *dirty; 2585eda7b5eSChris Mason 2595eda7b5eSChris Mason dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 2605eda7b5eSChris Mason if (!dirty) 2615eda7b5eSChris Mason return -ENOMEM; 2625eda7b5eSChris Mason dirty->root = root; 2635ce14bbcSChris Mason dirty->latest_root = latest; 2645eda7b5eSChris Mason list_add(&dirty->list, dead_list); 2655eda7b5eSChris Mason return 0; 2665eda7b5eSChris Mason } 2675eda7b5eSChris Mason 26835b7e476SChris Mason static int add_dirty_roots(struct btrfs_trans_handle *trans, 26935b7e476SChris Mason struct radix_tree_root *radix, 27035b7e476SChris Mason struct list_head *list) 2710f7d52f4SChris Mason { 2720f7d52f4SChris Mason struct dirty_root *dirty; 2730f7d52f4SChris Mason struct btrfs_root *gang[8]; 2740f7d52f4SChris Mason struct btrfs_root *root; 2750f7d52f4SChris Mason int i; 2760f7d52f4SChris Mason int ret; 27754aa1f4dSChris Mason int err = 0; 2785eda7b5eSChris Mason u32 refs; 27954aa1f4dSChris Mason 2800f7d52f4SChris Mason while(1) { 2810f7d52f4SChris Mason ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, 2820f7d52f4SChris Mason ARRAY_SIZE(gang), 2830f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 2840f7d52f4SChris Mason if (ret == 0) 2850f7d52f4SChris Mason break; 2860f7d52f4SChris Mason for (i = 0; i < ret; i++) { 2870f7d52f4SChris Mason root = gang[i]; 2882619ba1fSChris Mason radix_tree_tag_clear(radix, 2892619ba1fSChris Mason (unsigned long)root->root_key.objectid, 2900f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 2910f7d52f4SChris Mason if (root->commit_root == root->node) { 292db94535dSChris Mason WARN_ON(root->node->start != 293db94535dSChris Mason btrfs_root_bytenr(&root->root_item)); 2945f39d397SChris Mason free_extent_buffer(root->commit_root); 2950f7d52f4SChris Mason root->commit_root = NULL; 29658176a96SJosef Bacik 29758176a96SJosef Bacik /* make sure to update the root on disk 29858176a96SJosef Bacik * so we get any updates to the block used 29958176a96SJosef Bacik * counts 30058176a96SJosef Bacik */ 30158176a96SJosef Bacik err = btrfs_update_root(trans, 30258176a96SJosef Bacik root->fs_info->tree_root, 30358176a96SJosef Bacik &root->root_key, 30458176a96SJosef Bacik &root->root_item); 3050f7d52f4SChris Mason continue; 3060f7d52f4SChris Mason } 3070f7d52f4SChris Mason dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 3080f7d52f4SChris Mason BUG_ON(!dirty); 3099f3a7427SChris Mason dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS); 3109f3a7427SChris Mason BUG_ON(!dirty->root); 3119f3a7427SChris Mason 3129f3a7427SChris Mason memset(&root->root_item.drop_progress, 0, 3139f3a7427SChris Mason sizeof(struct btrfs_disk_key)); 3149f3a7427SChris Mason root->root_item.drop_level = 0; 3159f3a7427SChris Mason 3169f3a7427SChris Mason memcpy(dirty->root, root, sizeof(*root)); 3179f3a7427SChris Mason dirty->root->node = root->commit_root; 31858176a96SJosef Bacik dirty->latest_root = root; 3190f7d52f4SChris Mason root->commit_root = NULL; 3205eda7b5eSChris Mason 3210f7d52f4SChris Mason root->root_key.offset = root->fs_info->generation; 322db94535dSChris Mason btrfs_set_root_bytenr(&root->root_item, 323db94535dSChris Mason root->node->start); 324db94535dSChris Mason btrfs_set_root_level(&root->root_item, 325db94535dSChris Mason btrfs_header_level(root->node)); 3260f7d52f4SChris Mason err = btrfs_insert_root(trans, root->fs_info->tree_root, 3270f7d52f4SChris Mason &root->root_key, 3280f7d52f4SChris Mason &root->root_item); 32954aa1f4dSChris Mason if (err) 33054aa1f4dSChris Mason break; 3319f3a7427SChris Mason 3329f3a7427SChris Mason refs = btrfs_root_refs(&dirty->root->root_item); 3339f3a7427SChris Mason btrfs_set_root_refs(&dirty->root->root_item, refs - 1); 3345eda7b5eSChris Mason err = btrfs_update_root(trans, root->fs_info->tree_root, 3359f3a7427SChris Mason &dirty->root->root_key, 3369f3a7427SChris Mason &dirty->root->root_item); 3375eda7b5eSChris Mason 3385eda7b5eSChris Mason BUG_ON(err); 3399f3a7427SChris Mason if (refs == 1) { 3400f7d52f4SChris Mason list_add(&dirty->list, list); 3419f3a7427SChris Mason } else { 3429f3a7427SChris Mason WARN_ON(1); 3439f3a7427SChris Mason kfree(dirty->root); 3445eda7b5eSChris Mason kfree(dirty); 3450f7d52f4SChris Mason } 3460f7d52f4SChris Mason } 3479f3a7427SChris Mason } 34854aa1f4dSChris Mason return err; 3490f7d52f4SChris Mason } 3500f7d52f4SChris Mason 351e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) 352e9d0b13bSChris Mason { 353e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 354e9d0b13bSChris Mason int ret; 355e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 356d3c2fdcfSChris Mason unsigned long nr; 357e9d0b13bSChris Mason 358e9d0b13bSChris Mason if (root->defrag_running) 359e9d0b13bSChris Mason return 0; 360e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 3616b80053dSChris Mason while (1) { 362e9d0b13bSChris Mason root->defrag_running = 1; 363e9d0b13bSChris Mason ret = btrfs_defrag_leaves(trans, root, cacheonly); 364d3c2fdcfSChris Mason nr = trans->blocks_used; 365e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 366e9d0b13bSChris Mason mutex_unlock(&info->fs_mutex); 367d3c2fdcfSChris Mason btrfs_btree_balance_dirty(info->tree_root, nr); 368e9d0b13bSChris Mason cond_resched(); 369e9d0b13bSChris Mason 370e9d0b13bSChris Mason mutex_lock(&info->fs_mutex); 371e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 372e9d0b13bSChris Mason if (ret != -EAGAIN) 373e9d0b13bSChris Mason break; 374e9d0b13bSChris Mason } 375e9d0b13bSChris Mason root->defrag_running = 0; 376e9d0b13bSChris Mason radix_tree_tag_clear(&info->fs_roots_radix, 377e9d0b13bSChris Mason (unsigned long)root->root_key.objectid, 378e9d0b13bSChris Mason BTRFS_ROOT_DEFRAG_TAG); 379e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 380e9d0b13bSChris Mason return 0; 381e9d0b13bSChris Mason } 382e9d0b13bSChris Mason 3836702ed49SChris Mason int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info) 3846702ed49SChris Mason { 3856702ed49SChris Mason struct btrfs_root *gang[1]; 3866702ed49SChris Mason struct btrfs_root *root; 3876702ed49SChris Mason int i; 3886702ed49SChris Mason int ret; 3896702ed49SChris Mason int err = 0; 3906702ed49SChris Mason u64 last = 0; 3916702ed49SChris Mason 3926702ed49SChris Mason while(1) { 3936702ed49SChris Mason ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix, 3946702ed49SChris Mason (void **)gang, last, 3956702ed49SChris Mason ARRAY_SIZE(gang), 3966702ed49SChris Mason BTRFS_ROOT_DEFRAG_TAG); 3976702ed49SChris Mason if (ret == 0) 3986702ed49SChris Mason break; 3996702ed49SChris Mason for (i = 0; i < ret; i++) { 4006702ed49SChris Mason root = gang[i]; 4016702ed49SChris Mason last = root->root_key.objectid + 1; 402f510cfecSChris Mason btrfs_defrag_root(root, 1); 4036702ed49SChris Mason } 4046702ed49SChris Mason } 4056b80053dSChris Mason btrfs_defrag_root(info->extent_root, 1); 4066702ed49SChris Mason return err; 4076702ed49SChris Mason } 4086702ed49SChris Mason 40935b7e476SChris Mason static int drop_dirty_roots(struct btrfs_root *tree_root, 41035b7e476SChris Mason struct list_head *list) 4110f7d52f4SChris Mason { 4120f7d52f4SChris Mason struct dirty_root *dirty; 4130f7d52f4SChris Mason struct btrfs_trans_handle *trans; 414d3c2fdcfSChris Mason unsigned long nr; 415db94535dSChris Mason u64 num_bytes; 416db94535dSChris Mason u64 bytes_used; 41754aa1f4dSChris Mason int ret = 0; 4189f3a7427SChris Mason int err; 4199f3a7427SChris Mason 4200f7d52f4SChris Mason while(!list_empty(list)) { 42158176a96SJosef Bacik struct btrfs_root *root; 42258176a96SJosef Bacik 423facda1e7SChris Mason mutex_lock(&tree_root->fs_info->fs_mutex); 4240f7d52f4SChris Mason dirty = list_entry(list->next, struct dirty_root, list); 4250f7d52f4SChris Mason list_del_init(&dirty->list); 4265eda7b5eSChris Mason 427db94535dSChris Mason num_bytes = btrfs_root_used(&dirty->root->root_item); 42858176a96SJosef Bacik root = dirty->latest_root; 429e2008b61SChris Mason root->fs_info->throttles++; 43058176a96SJosef Bacik 4319f3a7427SChris Mason while(1) { 4320f7d52f4SChris Mason trans = btrfs_start_transaction(tree_root, 1); 4339f3a7427SChris Mason ret = btrfs_drop_snapshot(trans, dirty->root); 4349f3a7427SChris Mason if (ret != -EAGAIN) { 4359f3a7427SChris Mason break; 4369f3a7427SChris Mason } 43758176a96SJosef Bacik 4389f3a7427SChris Mason err = btrfs_update_root(trans, 4399f3a7427SChris Mason tree_root, 4409f3a7427SChris Mason &dirty->root->root_key, 4419f3a7427SChris Mason &dirty->root->root_item); 4429f3a7427SChris Mason if (err) 4439f3a7427SChris Mason ret = err; 444d3c2fdcfSChris Mason nr = trans->blocks_used; 4459f3a7427SChris Mason ret = btrfs_end_transaction(trans, tree_root); 4460f7d52f4SChris Mason BUG_ON(ret); 447f4468e94SChris Mason mutex_unlock(&tree_root->fs_info->fs_mutex); 448d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 4494dc11904SChris Mason cond_resched(); 450f4468e94SChris Mason mutex_lock(&tree_root->fs_info->fs_mutex); 4519f3a7427SChris Mason } 4529f3a7427SChris Mason BUG_ON(ret); 453e2008b61SChris Mason root->fs_info->throttles--; 45458176a96SJosef Bacik 455db94535dSChris Mason num_bytes -= btrfs_root_used(&dirty->root->root_item); 456db94535dSChris Mason bytes_used = btrfs_root_used(&root->root_item); 457db94535dSChris Mason if (num_bytes) { 45858176a96SJosef Bacik record_root_in_trans(root); 4595f39d397SChris Mason btrfs_set_root_used(&root->root_item, 460db94535dSChris Mason bytes_used - num_bytes); 46158176a96SJosef Bacik } 4629f3a7427SChris Mason ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); 46358176a96SJosef Bacik if (ret) { 46458176a96SJosef Bacik BUG(); 46554aa1f4dSChris Mason break; 46658176a96SJosef Bacik } 467d3c2fdcfSChris Mason nr = trans->blocks_used; 4680f7d52f4SChris Mason ret = btrfs_end_transaction(trans, tree_root); 4690f7d52f4SChris Mason BUG_ON(ret); 4705eda7b5eSChris Mason 471f510cfecSChris Mason free_extent_buffer(dirty->root->node); 4725eda7b5eSChris Mason kfree(dirty->root); 4730f7d52f4SChris Mason kfree(dirty); 474facda1e7SChris Mason mutex_unlock(&tree_root->fs_info->fs_mutex); 475d3c2fdcfSChris Mason 476d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 4774dc11904SChris Mason cond_resched(); 4780f7d52f4SChris Mason } 47954aa1f4dSChris Mason return ret; 4800f7d52f4SChris Mason } 4810f7d52f4SChris Mason 482dc17ff8fSChris Mason int btrfs_write_ordered_inodes(struct btrfs_trans_handle *trans, 483dc17ff8fSChris Mason struct btrfs_root *root) 484dc17ff8fSChris Mason { 485dc17ff8fSChris Mason struct btrfs_transaction *cur_trans = trans->transaction; 486dc17ff8fSChris Mason struct inode *inode; 487dc17ff8fSChris Mason u64 root_objectid = 0; 488dc17ff8fSChris Mason u64 objectid = 0; 489dc17ff8fSChris Mason int ret; 490dc17ff8fSChris Mason 491e2008b61SChris Mason root->fs_info->throttles++; 492dc17ff8fSChris Mason while(1) { 493dc17ff8fSChris Mason ret = btrfs_find_first_ordered_inode( 494dc17ff8fSChris Mason &cur_trans->ordered_inode_tree, 4954d5e74bcSChris Mason &root_objectid, &objectid, &inode); 496dc17ff8fSChris Mason if (!ret) 497dc17ff8fSChris Mason break; 498dc17ff8fSChris Mason 499dc17ff8fSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 500dc17ff8fSChris Mason mutex_unlock(&root->fs_info->fs_mutex); 5014d5e74bcSChris Mason 502dc17ff8fSChris Mason if (S_ISREG(inode->i_mode)) 503dc17ff8fSChris Mason filemap_fdatawrite(inode->i_mapping); 504dc17ff8fSChris Mason iput(inode); 5054d5e74bcSChris Mason 506dc17ff8fSChris Mason mutex_lock(&root->fs_info->fs_mutex); 507dc17ff8fSChris Mason mutex_lock(&root->fs_info->trans_mutex); 508dc17ff8fSChris Mason } 509dc17ff8fSChris Mason while(1) { 510dc17ff8fSChris Mason root_objectid = 0; 511dc17ff8fSChris Mason objectid = 0; 512dc17ff8fSChris Mason ret = btrfs_find_del_first_ordered_inode( 513dc17ff8fSChris Mason &cur_trans->ordered_inode_tree, 5144d5e74bcSChris Mason &root_objectid, &objectid, &inode); 515dc17ff8fSChris Mason if (!ret) 516dc17ff8fSChris Mason break; 517dc17ff8fSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 518dc17ff8fSChris Mason mutex_unlock(&root->fs_info->fs_mutex); 5194d5e74bcSChris Mason 520dc17ff8fSChris Mason if (S_ISREG(inode->i_mode)) 521dc17ff8fSChris Mason filemap_write_and_wait(inode->i_mapping); 5222da98f00SChris Mason atomic_dec(&inode->i_count); 523dc17ff8fSChris Mason iput(inode); 5244d5e74bcSChris Mason 525dc17ff8fSChris Mason mutex_lock(&root->fs_info->fs_mutex); 526dc17ff8fSChris Mason mutex_lock(&root->fs_info->trans_mutex); 527dc17ff8fSChris Mason } 528e2008b61SChris Mason root->fs_info->throttles--; 5293063d29fSChris Mason return 0; 5303063d29fSChris Mason } 5313063d29fSChris Mason 5323063d29fSChris Mason static int create_pending_snapshot(struct btrfs_trans_handle *trans, 5333063d29fSChris Mason struct btrfs_fs_info *fs_info, 5343063d29fSChris Mason struct btrfs_pending_snapshot *pending) 5353063d29fSChris Mason { 5363063d29fSChris Mason struct btrfs_key key; 5373063d29fSChris Mason struct btrfs_root_item new_root_item; 5383063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 5393063d29fSChris Mason struct btrfs_root *root = pending->root; 5403063d29fSChris Mason struct extent_buffer *tmp; 5413063d29fSChris Mason int ret; 5423063d29fSChris Mason u64 objectid; 5433063d29fSChris Mason 5443063d29fSChris Mason ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid); 5453063d29fSChris Mason if (ret) 5463063d29fSChris Mason goto fail; 5473063d29fSChris Mason 5483063d29fSChris Mason memcpy(&new_root_item, &root->root_item, sizeof(new_root_item)); 5493063d29fSChris Mason 5503063d29fSChris Mason key.objectid = objectid; 5513063d29fSChris Mason key.offset = 1; 5523063d29fSChris Mason btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 5533063d29fSChris Mason 5543063d29fSChris Mason extent_buffer_get(root->node); 5553063d29fSChris Mason btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp); 5563063d29fSChris Mason free_extent_buffer(tmp); 5573063d29fSChris Mason 5583063d29fSChris Mason btrfs_copy_root(trans, root, root->node, &tmp, objectid); 5593063d29fSChris Mason 5603063d29fSChris Mason btrfs_set_root_bytenr(&new_root_item, tmp->start); 5613063d29fSChris Mason btrfs_set_root_level(&new_root_item, btrfs_header_level(tmp)); 5623063d29fSChris Mason ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 5633063d29fSChris Mason &new_root_item); 5643063d29fSChris Mason free_extent_buffer(tmp); 5653063d29fSChris Mason if (ret) 5663063d29fSChris Mason goto fail; 5673063d29fSChris Mason 5683063d29fSChris Mason /* 5693063d29fSChris Mason * insert the directory item 5703063d29fSChris Mason */ 5713063d29fSChris Mason key.offset = (u64)-1; 5723063d29fSChris Mason ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, 5733063d29fSChris Mason pending->name, strlen(pending->name), 5743063d29fSChris Mason root->fs_info->sb->s_root->d_inode->i_ino, 5753063d29fSChris Mason &key, BTRFS_FT_DIR); 5763063d29fSChris Mason 5773063d29fSChris Mason if (ret) 5783063d29fSChris Mason goto fail; 5793063d29fSChris Mason 5803063d29fSChris Mason ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root, 5813063d29fSChris Mason pending->name, strlen(pending->name), objectid, 5823063d29fSChris Mason root->fs_info->sb->s_root->d_inode->i_ino); 5833063d29fSChris Mason fail: 5843063d29fSChris Mason return ret; 5853063d29fSChris Mason } 5863063d29fSChris Mason 5873063d29fSChris Mason static int create_pending_snapshots(struct btrfs_trans_handle *trans, 5883063d29fSChris Mason struct btrfs_fs_info *fs_info) 5893063d29fSChris Mason { 5903063d29fSChris Mason struct btrfs_pending_snapshot *pending; 5913063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 5923063d29fSChris Mason int ret; 5933063d29fSChris Mason 5943063d29fSChris Mason while(!list_empty(head)) { 5953063d29fSChris Mason pending = list_entry(head->next, 5963063d29fSChris Mason struct btrfs_pending_snapshot, list); 5973063d29fSChris Mason ret = create_pending_snapshot(trans, fs_info, pending); 5983063d29fSChris Mason BUG_ON(ret); 5993063d29fSChris Mason list_del(&pending->list); 6003063d29fSChris Mason kfree(pending->name); 6013063d29fSChris Mason kfree(pending); 6023063d29fSChris Mason } 603dc17ff8fSChris Mason return 0; 604dc17ff8fSChris Mason } 605dc17ff8fSChris Mason 60679154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 60779154b1bSChris Mason struct btrfs_root *root) 60879154b1bSChris Mason { 60915ee9bc7SJosef Bacik unsigned long joined = 0; 61015ee9bc7SJosef Bacik unsigned long timeout = 1; 61179154b1bSChris Mason struct btrfs_transaction *cur_trans; 6128fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 6130f7d52f4SChris Mason struct list_head dirty_fs_roots; 614d1310b2eSChris Mason struct extent_io_tree *pinned_copy; 61579154b1bSChris Mason DEFINE_WAIT(wait); 61615ee9bc7SJosef Bacik int ret; 61779154b1bSChris Mason 6180f7d52f4SChris Mason INIT_LIST_HEAD(&dirty_fs_roots); 619d6e4a428SChris Mason 62079154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 62179154b1bSChris Mason if (trans->transaction->in_commit) { 62279154b1bSChris Mason cur_trans = trans->transaction; 62379154b1bSChris Mason trans->transaction->use_count++; 624ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 62579154b1bSChris Mason btrfs_end_transaction(trans, root); 626ccd467d6SChris Mason 627ccd467d6SChris Mason mutex_unlock(&root->fs_info->fs_mutex); 62879154b1bSChris Mason ret = wait_for_commit(root, cur_trans); 62979154b1bSChris Mason BUG_ON(ret); 63015ee9bc7SJosef Bacik 63115ee9bc7SJosef Bacik mutex_lock(&root->fs_info->trans_mutex); 63279154b1bSChris Mason put_transaction(cur_trans); 63315ee9bc7SJosef Bacik mutex_unlock(&root->fs_info->trans_mutex); 63415ee9bc7SJosef Bacik 635ccd467d6SChris Mason mutex_lock(&root->fs_info->fs_mutex); 63679154b1bSChris Mason return 0; 63779154b1bSChris Mason } 6384313b399SChris Mason 6394313b399SChris Mason pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS); 6404313b399SChris Mason if (!pinned_copy) 6414313b399SChris Mason return -ENOMEM; 6424313b399SChris Mason 643d1310b2eSChris Mason extent_io_tree_init(pinned_copy, 6444313b399SChris Mason root->fs_info->btree_inode->i_mapping, GFP_NOFS); 6454313b399SChris Mason 6462c90e5d6SChris Mason trans->transaction->in_commit = 1; 647ccd467d6SChris Mason cur_trans = trans->transaction; 648ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 649ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 650ccd467d6SChris Mason struct btrfs_transaction, list); 651ccd467d6SChris Mason if (!prev_trans->commit_done) { 652ccd467d6SChris Mason prev_trans->use_count++; 653ccd467d6SChris Mason mutex_unlock(&root->fs_info->fs_mutex); 654ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 655ccd467d6SChris Mason 656ccd467d6SChris Mason wait_for_commit(root, prev_trans); 657ccd467d6SChris Mason 658ccd467d6SChris Mason mutex_lock(&root->fs_info->fs_mutex); 659ccd467d6SChris Mason mutex_lock(&root->fs_info->trans_mutex); 66015ee9bc7SJosef Bacik put_transaction(prev_trans); 661ccd467d6SChris Mason } 662ccd467d6SChris Mason } 66315ee9bc7SJosef Bacik 66415ee9bc7SJosef Bacik do { 66515ee9bc7SJosef Bacik joined = cur_trans->num_joined; 6662c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 66715ee9bc7SJosef Bacik prepare_to_wait(&cur_trans->writer_wait, &wait, 66879154b1bSChris Mason TASK_UNINTERRUPTIBLE); 66915ee9bc7SJosef Bacik 67015ee9bc7SJosef Bacik if (cur_trans->num_writers > 1) 67115ee9bc7SJosef Bacik timeout = MAX_SCHEDULE_TIMEOUT; 67215ee9bc7SJosef Bacik else 67315ee9bc7SJosef Bacik timeout = 1; 67415ee9bc7SJosef Bacik 675ccd467d6SChris Mason mutex_unlock(&root->fs_info->fs_mutex); 67679154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 67715ee9bc7SJosef Bacik 67815ee9bc7SJosef Bacik schedule_timeout(timeout); 67915ee9bc7SJosef Bacik 680ccd467d6SChris Mason mutex_lock(&root->fs_info->fs_mutex); 68179154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 68215ee9bc7SJosef Bacik finish_wait(&cur_trans->writer_wait, &wait); 683dc17ff8fSChris Mason ret = btrfs_write_ordered_inodes(trans, root); 684dc17ff8fSChris Mason 68515ee9bc7SJosef Bacik } while (cur_trans->num_writers > 1 || 68615ee9bc7SJosef Bacik (cur_trans->num_joined != joined)); 68715ee9bc7SJosef Bacik 6883063d29fSChris Mason ret = create_pending_snapshots(trans, root->fs_info); 6893063d29fSChris Mason BUG_ON(ret); 6903063d29fSChris Mason 6912c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 692dc17ff8fSChris Mason 69354aa1f4dSChris Mason ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, 69454aa1f4dSChris Mason &dirty_fs_roots); 69554aa1f4dSChris Mason BUG_ON(ret); 69654aa1f4dSChris Mason 69779154b1bSChris Mason ret = btrfs_commit_tree_roots(trans, root); 69879154b1bSChris Mason BUG_ON(ret); 69954aa1f4dSChris Mason 70078fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 701cee36a03SChris Mason spin_lock(&root->fs_info->new_trans_lock); 70278fae27eSChris Mason root->fs_info->running_transaction = NULL; 703cee36a03SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 7044b52dff6SChris Mason btrfs_set_super_generation(&root->fs_info->super_copy, 7054b52dff6SChris Mason cur_trans->transid); 7064b52dff6SChris Mason btrfs_set_super_root(&root->fs_info->super_copy, 707db94535dSChris Mason root->fs_info->tree_root->node->start); 708db94535dSChris Mason btrfs_set_super_root_level(&root->fs_info->super_copy, 709db94535dSChris Mason btrfs_header_level(root->fs_info->tree_root->node)); 7105f39d397SChris Mason 7115f39d397SChris Mason write_extent_buffer(root->fs_info->sb_buffer, 7125f39d397SChris Mason &root->fs_info->super_copy, 0, 7134b52dff6SChris Mason sizeof(root->fs_info->super_copy)); 714ccd467d6SChris Mason 7154313b399SChris Mason btrfs_copy_pinned(root, pinned_copy); 716ccd467d6SChris Mason 71778fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 7188fd17795SChris Mason mutex_unlock(&root->fs_info->fs_mutex); 71979154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 72079154b1bSChris Mason BUG_ON(ret); 72179154b1bSChris Mason write_ctree_super(trans, root); 7224313b399SChris Mason 7238fd17795SChris Mason mutex_lock(&root->fs_info->fs_mutex); 7244313b399SChris Mason btrfs_finish_extent_commit(trans, root, pinned_copy); 72578fae27eSChris Mason mutex_lock(&root->fs_info->trans_mutex); 7264313b399SChris Mason 7274313b399SChris Mason kfree(pinned_copy); 7284313b399SChris Mason 7292c90e5d6SChris Mason cur_trans->commit_done = 1; 73015ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 7312c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 73279154b1bSChris Mason put_transaction(cur_trans); 73378fae27eSChris Mason put_transaction(cur_trans); 73458176a96SJosef Bacik 735facda1e7SChris Mason if (root->fs_info->closing) 736facda1e7SChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); 737facda1e7SChris Mason else 738facda1e7SChris Mason list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); 73958176a96SJosef Bacik 74078fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 7412c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 74279154b1bSChris Mason 743facda1e7SChris Mason if (root->fs_info->closing) { 744facda1e7SChris Mason mutex_unlock(&root->fs_info->fs_mutex); 7450f7d52f4SChris Mason drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); 746facda1e7SChris Mason mutex_lock(&root->fs_info->fs_mutex); 747facda1e7SChris Mason } 74879154b1bSChris Mason return ret; 74979154b1bSChris Mason } 75079154b1bSChris Mason 751e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root) 752e9d0b13bSChris Mason { 753e9d0b13bSChris Mason struct list_head dirty_roots; 754e9d0b13bSChris Mason INIT_LIST_HEAD(&dirty_roots); 755e9d0b13bSChris Mason 756e9d0b13bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 757e9d0b13bSChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_roots); 758e9d0b13bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 759e9d0b13bSChris Mason 760e9d0b13bSChris Mason if (!list_empty(&dirty_roots)) { 761e9d0b13bSChris Mason drop_dirty_roots(root, &dirty_roots); 762e9d0b13bSChris Mason } 763e9d0b13bSChris Mason return 0; 764e9d0b13bSChris Mason } 7656da6abaeSChris Mason #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) 7666da6abaeSChris Mason void btrfs_transaction_cleaner(void *p) 7676da6abaeSChris Mason #else 76808607c1bSChris Mason void btrfs_transaction_cleaner(struct work_struct *work) 7696da6abaeSChris Mason #endif 77008607c1bSChris Mason { 7716da6abaeSChris Mason #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) 7726da6abaeSChris Mason struct btrfs_fs_info *fs_info = p; 7736da6abaeSChris Mason #else 77408607c1bSChris Mason struct btrfs_fs_info *fs_info = container_of(work, 77508607c1bSChris Mason struct btrfs_fs_info, 77608607c1bSChris Mason trans_work.work); 77708607c1bSChris Mason 7786da6abaeSChris Mason #endif 77908607c1bSChris Mason struct btrfs_root *root = fs_info->tree_root; 78008607c1bSChris Mason struct btrfs_transaction *cur; 78108607c1bSChris Mason struct btrfs_trans_handle *trans; 78208607c1bSChris Mason unsigned long now; 78308607c1bSChris Mason unsigned long delay = HZ * 30; 78408607c1bSChris Mason int ret; 78508607c1bSChris Mason 78608607c1bSChris Mason mutex_lock(&root->fs_info->fs_mutex); 78708607c1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 78808607c1bSChris Mason cur = root->fs_info->running_transaction; 78908607c1bSChris Mason if (!cur) { 79008607c1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 79108607c1bSChris Mason goto out; 79208607c1bSChris Mason } 79308607c1bSChris Mason now = get_seconds(); 79408607c1bSChris Mason if (now < cur->start_time || now - cur->start_time < 30) { 79508607c1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 79608607c1bSChris Mason delay = HZ * 5; 79708607c1bSChris Mason goto out; 79808607c1bSChris Mason } 79908607c1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 8006702ed49SChris Mason btrfs_defrag_dirty_roots(root->fs_info); 80108607c1bSChris Mason trans = btrfs_start_transaction(root, 1); 80208607c1bSChris Mason ret = btrfs_commit_transaction(trans, root); 80308607c1bSChris Mason out: 80408607c1bSChris Mason mutex_unlock(&root->fs_info->fs_mutex); 805e9d0b13bSChris Mason btrfs_clean_old_snapshots(root); 80608607c1bSChris Mason btrfs_transaction_queue_work(root, delay); 80708607c1bSChris Mason } 80808607c1bSChris Mason 80908607c1bSChris Mason void btrfs_transaction_queue_work(struct btrfs_root *root, int delay) 81008607c1bSChris Mason { 81108607c1bSChris Mason queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay); 81208607c1bSChris Mason } 81308607c1bSChris Mason 81408607c1bSChris Mason void btrfs_transaction_flush_work(struct btrfs_root *root) 81508607c1bSChris Mason { 81608607c1bSChris Mason cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work); 81708607c1bSChris Mason flush_workqueue(trans_wq); 81808607c1bSChris Mason } 81908607c1bSChris Mason 82008607c1bSChris Mason void __init btrfs_init_transaction_sys(void) 82108607c1bSChris Mason { 82208607c1bSChris Mason trans_wq = create_workqueue("btrfs"); 82308607c1bSChris Mason } 82408607c1bSChris Mason 82517636e03SChristian Hesse void btrfs_exit_transaction_sys(void) 82608607c1bSChris Mason { 82708607c1bSChris Mason destroy_workqueue(trans_wq); 82808607c1bSChris Mason } 82908607c1bSChris Mason 830