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); 70*dc17ff8fSChris Mason btrfs_ordered_inode_tree_init(&cur_trans->ordered_inode_tree); 715f39d397SChris Mason extent_map_tree_init(&cur_trans->dirty_pages, 725f39d397SChris Mason root->fs_info->btree_inode->i_mapping, 735f39d397SChris Mason GFP_NOFS); 7415ee9bc7SJosef Bacik } else { 7579154b1bSChris Mason cur_trans->num_writers++; 7615ee9bc7SJosef Bacik cur_trans->num_joined++; 7715ee9bc7SJosef Bacik } 7815ee9bc7SJosef Bacik 7979154b1bSChris Mason return 0; 8079154b1bSChris Mason } 8179154b1bSChris Mason 826702ed49SChris Mason static int record_root_in_trans(struct btrfs_root *root) 836702ed49SChris Mason { 846702ed49SChris Mason u64 running_trans_id = root->fs_info->running_transaction->transid; 856702ed49SChris Mason if (root->ref_cows && root->last_trans < running_trans_id) { 866702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 876702ed49SChris Mason if (root->root_item.refs != 0) { 886702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 896702ed49SChris Mason (unsigned long)root->root_key.objectid, 906702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 916702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 926702ed49SChris Mason (unsigned long)root->root_key.objectid, 936702ed49SChris Mason BTRFS_ROOT_DEFRAG_TAG); 946702ed49SChris Mason root->commit_root = root->node; 955f39d397SChris Mason extent_buffer_get(root->node); 966702ed49SChris Mason } else { 976702ed49SChris Mason WARN_ON(1); 986702ed49SChris Mason } 996702ed49SChris Mason root->last_trans = running_trans_id; 1006702ed49SChris Mason } 1016702ed49SChris Mason return 0; 1026702ed49SChris Mason } 1036702ed49SChris Mason 10479154b1bSChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 10579154b1bSChris Mason int num_blocks) 10679154b1bSChris Mason { 1072c90e5d6SChris Mason struct btrfs_trans_handle *h = 1082c90e5d6SChris Mason kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 10979154b1bSChris Mason int ret; 11079154b1bSChris Mason 11179154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 11279154b1bSChris Mason ret = join_transaction(root); 11379154b1bSChris Mason BUG_ON(ret); 1140f7d52f4SChris Mason 1156702ed49SChris Mason record_root_in_trans(root); 1166702ed49SChris Mason h->transid = root->fs_info->running_transaction->transid; 11779154b1bSChris Mason h->transaction = root->fs_info->running_transaction; 11879154b1bSChris Mason h->blocks_reserved = num_blocks; 11979154b1bSChris Mason h->blocks_used = 0; 12031f3c99bSChris Mason h->block_group = NULL; 12126b8003fSChris Mason h->alloc_exclude_nr = 0; 12226b8003fSChris Mason h->alloc_exclude_start = 0; 12379154b1bSChris Mason root->fs_info->running_transaction->use_count++; 12479154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 12579154b1bSChris Mason return h; 12679154b1bSChris Mason } 12779154b1bSChris Mason 12879154b1bSChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 12979154b1bSChris Mason struct btrfs_root *root) 13079154b1bSChris Mason { 13179154b1bSChris Mason struct btrfs_transaction *cur_trans; 132d6e4a428SChris Mason 13379154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 13479154b1bSChris Mason cur_trans = root->fs_info->running_transaction; 135ccd467d6SChris Mason WARN_ON(cur_trans != trans->transaction); 136d5719762SChris Mason WARN_ON(cur_trans->num_writers < 1); 137ccd467d6SChris Mason cur_trans->num_writers--; 13879154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 13979154b1bSChris Mason wake_up(&cur_trans->writer_wait); 14079154b1bSChris Mason put_transaction(cur_trans); 14179154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 142d6025579SChris Mason memset(trans, 0, sizeof(*trans)); 1432c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 14479154b1bSChris Mason return 0; 14579154b1bSChris Mason } 14679154b1bSChris Mason 14779154b1bSChris Mason 14879154b1bSChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 14979154b1bSChris Mason struct btrfs_root *root) 15079154b1bSChris Mason { 1517c4452b9SChris Mason int ret; 1527c4452b9SChris Mason int err; 1537c4452b9SChris Mason int werr = 0; 1545f39d397SChris Mason struct extent_map_tree *dirty_pages; 1557c4452b9SChris Mason struct page *page; 1567c4452b9SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 1575f39d397SChris Mason u64 start; 1585f39d397SChris Mason u64 end; 1595f39d397SChris Mason unsigned long index; 1607c4452b9SChris Mason 1617c4452b9SChris Mason if (!trans || !trans->transaction) { 1627c4452b9SChris Mason return filemap_write_and_wait(btree_inode->i_mapping); 1637c4452b9SChris Mason } 1647c4452b9SChris Mason dirty_pages = &trans->transaction->dirty_pages; 1657c4452b9SChris Mason while(1) { 1665f39d397SChris Mason ret = find_first_extent_bit(dirty_pages, 0, &start, &end, 1675f39d397SChris Mason EXTENT_DIRTY); 1685f39d397SChris Mason if (ret) 1697c4452b9SChris Mason break; 1705f39d397SChris Mason clear_extent_dirty(dirty_pages, start, end, GFP_NOFS); 1715f39d397SChris Mason while(start <= end) { 1725f39d397SChris Mason index = start >> PAGE_CACHE_SHIFT; 17335ebb934SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 1745f39d397SChris Mason page = find_lock_page(btree_inode->i_mapping, index); 1757c4452b9SChris Mason if (!page) 1767c4452b9SChris Mason continue; 1776702ed49SChris Mason if (PageWriteback(page)) { 1786702ed49SChris Mason if (PageDirty(page)) 1796702ed49SChris Mason wait_on_page_writeback(page); 1806702ed49SChris Mason else { 1816702ed49SChris Mason unlock_page(page); 1826702ed49SChris Mason page_cache_release(page); 1836702ed49SChris Mason continue; 1846702ed49SChris Mason } 1856702ed49SChris Mason } 1867c4452b9SChris Mason err = write_one_page(page, 0); 1877c4452b9SChris Mason if (err) 1887c4452b9SChris Mason werr = err; 1897c4452b9SChris Mason page_cache_release(page); 1907c4452b9SChris Mason } 1917c4452b9SChris Mason } 1927c4452b9SChris Mason err = filemap_fdatawait(btree_inode->i_mapping); 1937c4452b9SChris Mason if (err) 1947c4452b9SChris Mason werr = err; 1957c4452b9SChris Mason return werr; 19679154b1bSChris Mason } 19779154b1bSChris Mason 19879154b1bSChris Mason int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, 19979154b1bSChris Mason struct btrfs_root *root) 20079154b1bSChris Mason { 20179154b1bSChris Mason int ret; 20279154b1bSChris Mason u64 old_extent_block; 20379154b1bSChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 20479154b1bSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 20579154b1bSChris Mason struct btrfs_root *extent_root = fs_info->extent_root; 20679154b1bSChris Mason 2079078a3e1SChris Mason btrfs_write_dirty_block_groups(trans, extent_root); 20879154b1bSChris Mason while(1) { 209db94535dSChris Mason old_extent_block = btrfs_root_bytenr(&extent_root->root_item); 210db94535dSChris Mason if (old_extent_block == extent_root->node->start) 21179154b1bSChris Mason break; 212db94535dSChris Mason btrfs_set_root_bytenr(&extent_root->root_item, 213db94535dSChris Mason extent_root->node->start); 214db94535dSChris Mason btrfs_set_root_level(&extent_root->root_item, 215db94535dSChris Mason btrfs_header_level(extent_root->node)); 21679154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 21779154b1bSChris Mason &extent_root->root_key, 21879154b1bSChris Mason &extent_root->root_item); 21979154b1bSChris Mason BUG_ON(ret); 2209078a3e1SChris Mason btrfs_write_dirty_block_groups(trans, extent_root); 22179154b1bSChris Mason } 22279154b1bSChris Mason return 0; 22379154b1bSChris Mason } 22479154b1bSChris Mason 22579154b1bSChris Mason static int wait_for_commit(struct btrfs_root *root, 22679154b1bSChris Mason struct btrfs_transaction *commit) 22779154b1bSChris Mason { 22879154b1bSChris Mason DEFINE_WAIT(wait); 229ccd467d6SChris Mason mutex_lock(&root->fs_info->trans_mutex); 23079154b1bSChris Mason while(!commit->commit_done) { 23179154b1bSChris Mason prepare_to_wait(&commit->commit_wait, &wait, 23279154b1bSChris Mason TASK_UNINTERRUPTIBLE); 23379154b1bSChris Mason if (commit->commit_done) 23479154b1bSChris Mason break; 23579154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 23679154b1bSChris Mason schedule(); 23779154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 23879154b1bSChris Mason } 239ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 24079154b1bSChris Mason finish_wait(&commit->commit_wait, &wait); 24179154b1bSChris Mason return 0; 24279154b1bSChris Mason } 24379154b1bSChris Mason 2440f7d52f4SChris Mason struct dirty_root { 2450f7d52f4SChris Mason struct list_head list; 2460f7d52f4SChris Mason struct btrfs_root *root; 24758176a96SJosef Bacik struct btrfs_root *latest_root; 2480f7d52f4SChris Mason }; 2490f7d52f4SChris Mason 2505ce14bbcSChris Mason int btrfs_add_dead_root(struct btrfs_root *root, 2515ce14bbcSChris Mason struct btrfs_root *latest, 2525ce14bbcSChris Mason struct list_head *dead_list) 2535eda7b5eSChris Mason { 2545eda7b5eSChris Mason struct dirty_root *dirty; 2555eda7b5eSChris Mason 2565eda7b5eSChris Mason dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 2575eda7b5eSChris Mason if (!dirty) 2585eda7b5eSChris Mason return -ENOMEM; 2595eda7b5eSChris Mason dirty->root = root; 2605ce14bbcSChris Mason dirty->latest_root = latest; 2615eda7b5eSChris Mason list_add(&dirty->list, dead_list); 2625eda7b5eSChris Mason return 0; 2635eda7b5eSChris Mason } 2645eda7b5eSChris Mason 26535b7e476SChris Mason static int add_dirty_roots(struct btrfs_trans_handle *trans, 26635b7e476SChris Mason struct radix_tree_root *radix, 26735b7e476SChris Mason struct list_head *list) 2680f7d52f4SChris Mason { 2690f7d52f4SChris Mason struct dirty_root *dirty; 2700f7d52f4SChris Mason struct btrfs_root *gang[8]; 2710f7d52f4SChris Mason struct btrfs_root *root; 2720f7d52f4SChris Mason int i; 2730f7d52f4SChris Mason int ret; 27454aa1f4dSChris Mason int err = 0; 2755eda7b5eSChris Mason u32 refs; 27654aa1f4dSChris Mason 2770f7d52f4SChris Mason while(1) { 2780f7d52f4SChris Mason ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, 2790f7d52f4SChris Mason ARRAY_SIZE(gang), 2800f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 2810f7d52f4SChris Mason if (ret == 0) 2820f7d52f4SChris Mason break; 2830f7d52f4SChris Mason for (i = 0; i < ret; i++) { 2840f7d52f4SChris Mason root = gang[i]; 2852619ba1fSChris Mason radix_tree_tag_clear(radix, 2862619ba1fSChris Mason (unsigned long)root->root_key.objectid, 2870f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 2880f7d52f4SChris Mason if (root->commit_root == root->node) { 289db94535dSChris Mason WARN_ON(root->node->start != 290db94535dSChris Mason btrfs_root_bytenr(&root->root_item)); 2915f39d397SChris Mason free_extent_buffer(root->commit_root); 2920f7d52f4SChris Mason root->commit_root = NULL; 29358176a96SJosef Bacik 29458176a96SJosef Bacik /* make sure to update the root on disk 29558176a96SJosef Bacik * so we get any updates to the block used 29658176a96SJosef Bacik * counts 29758176a96SJosef Bacik */ 29858176a96SJosef Bacik err = btrfs_update_root(trans, 29958176a96SJosef Bacik root->fs_info->tree_root, 30058176a96SJosef Bacik &root->root_key, 30158176a96SJosef Bacik &root->root_item); 3020f7d52f4SChris Mason continue; 3030f7d52f4SChris Mason } 3040f7d52f4SChris Mason dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 3050f7d52f4SChris Mason BUG_ON(!dirty); 3069f3a7427SChris Mason dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS); 3079f3a7427SChris Mason BUG_ON(!dirty->root); 3089f3a7427SChris Mason 3099f3a7427SChris Mason memset(&root->root_item.drop_progress, 0, 3109f3a7427SChris Mason sizeof(struct btrfs_disk_key)); 3119f3a7427SChris Mason root->root_item.drop_level = 0; 3129f3a7427SChris Mason 3139f3a7427SChris Mason memcpy(dirty->root, root, sizeof(*root)); 3149f3a7427SChris Mason dirty->root->node = root->commit_root; 31558176a96SJosef Bacik dirty->latest_root = root; 3160f7d52f4SChris Mason root->commit_root = NULL; 3175eda7b5eSChris Mason 3180f7d52f4SChris Mason root->root_key.offset = root->fs_info->generation; 319db94535dSChris Mason btrfs_set_root_bytenr(&root->root_item, 320db94535dSChris Mason root->node->start); 321db94535dSChris Mason btrfs_set_root_level(&root->root_item, 322db94535dSChris Mason btrfs_header_level(root->node)); 3230f7d52f4SChris Mason err = btrfs_insert_root(trans, root->fs_info->tree_root, 3240f7d52f4SChris Mason &root->root_key, 3250f7d52f4SChris Mason &root->root_item); 32654aa1f4dSChris Mason if (err) 32754aa1f4dSChris Mason break; 3289f3a7427SChris Mason 3299f3a7427SChris Mason refs = btrfs_root_refs(&dirty->root->root_item); 3309f3a7427SChris Mason btrfs_set_root_refs(&dirty->root->root_item, refs - 1); 3315eda7b5eSChris Mason err = btrfs_update_root(trans, root->fs_info->tree_root, 3329f3a7427SChris Mason &dirty->root->root_key, 3339f3a7427SChris Mason &dirty->root->root_item); 3345eda7b5eSChris Mason 3355eda7b5eSChris Mason BUG_ON(err); 3369f3a7427SChris Mason if (refs == 1) { 3370f7d52f4SChris Mason list_add(&dirty->list, list); 3389f3a7427SChris Mason } else { 3399f3a7427SChris Mason WARN_ON(1); 3409f3a7427SChris Mason kfree(dirty->root); 3415eda7b5eSChris Mason kfree(dirty); 3420f7d52f4SChris Mason } 3430f7d52f4SChris Mason } 3449f3a7427SChris Mason } 34554aa1f4dSChris Mason return err; 3460f7d52f4SChris Mason } 3470f7d52f4SChris Mason 348e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) 349e9d0b13bSChris Mason { 350e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 351e9d0b13bSChris Mason int ret; 352e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 353d3c2fdcfSChris Mason unsigned long nr; 354e9d0b13bSChris Mason 355e9d0b13bSChris Mason if (root->defrag_running) 356e9d0b13bSChris Mason return 0; 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); 364d3c2fdcfSChris Mason btrfs_btree_balance_dirty(info->tree_root, nr); 365e9d0b13bSChris Mason cond_resched(); 366e9d0b13bSChris Mason 367e9d0b13bSChris Mason mutex_lock(&info->fs_mutex); 368e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 369e9d0b13bSChris Mason if (ret != -EAGAIN) 370e9d0b13bSChris Mason break; 371e9d0b13bSChris Mason } 372e9d0b13bSChris Mason root->defrag_running = 0; 373e9d0b13bSChris Mason radix_tree_tag_clear(&info->fs_roots_radix, 374e9d0b13bSChris Mason (unsigned long)root->root_key.objectid, 375e9d0b13bSChris Mason BTRFS_ROOT_DEFRAG_TAG); 376e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 377e9d0b13bSChris Mason return 0; 378e9d0b13bSChris Mason } 379e9d0b13bSChris Mason 3806702ed49SChris Mason int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info) 3816702ed49SChris Mason { 3826702ed49SChris Mason struct btrfs_root *gang[1]; 3836702ed49SChris Mason struct btrfs_root *root; 3846702ed49SChris Mason int i; 3856702ed49SChris Mason int ret; 3866702ed49SChris Mason int err = 0; 3876702ed49SChris Mason u64 last = 0; 3886702ed49SChris Mason 3896702ed49SChris Mason while(1) { 3906702ed49SChris Mason ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix, 3916702ed49SChris Mason (void **)gang, last, 3926702ed49SChris Mason ARRAY_SIZE(gang), 3936702ed49SChris Mason BTRFS_ROOT_DEFRAG_TAG); 3946702ed49SChris Mason if (ret == 0) 3956702ed49SChris Mason break; 3966702ed49SChris Mason for (i = 0; i < ret; i++) { 3976702ed49SChris Mason root = gang[i]; 3986702ed49SChris Mason last = root->root_key.objectid + 1; 399f510cfecSChris Mason btrfs_defrag_root(root, 1); 4006702ed49SChris Mason } 4016702ed49SChris Mason } 4026b80053dSChris Mason btrfs_defrag_root(info->extent_root, 1); 4036702ed49SChris Mason return err; 4046702ed49SChris Mason } 4056702ed49SChris Mason 40635b7e476SChris Mason static int drop_dirty_roots(struct btrfs_root *tree_root, 40735b7e476SChris Mason struct list_head *list) 4080f7d52f4SChris Mason { 4090f7d52f4SChris Mason struct dirty_root *dirty; 4100f7d52f4SChris Mason struct btrfs_trans_handle *trans; 411d3c2fdcfSChris Mason unsigned long nr; 412db94535dSChris Mason u64 num_bytes; 413db94535dSChris Mason u64 bytes_used; 41454aa1f4dSChris Mason int ret = 0; 4159f3a7427SChris Mason int err; 4169f3a7427SChris Mason 4170f7d52f4SChris Mason while(!list_empty(list)) { 41858176a96SJosef Bacik struct btrfs_root *root; 41958176a96SJosef Bacik 420facda1e7SChris Mason mutex_lock(&tree_root->fs_info->fs_mutex); 4210f7d52f4SChris Mason dirty = list_entry(list->next, struct dirty_root, list); 4220f7d52f4SChris Mason list_del_init(&dirty->list); 4235eda7b5eSChris Mason 424db94535dSChris Mason num_bytes = btrfs_root_used(&dirty->root->root_item); 42558176a96SJosef Bacik root = dirty->latest_root; 42658176a96SJosef Bacik 4279f3a7427SChris Mason while(1) { 4280f7d52f4SChris Mason trans = btrfs_start_transaction(tree_root, 1); 4299f3a7427SChris Mason ret = btrfs_drop_snapshot(trans, dirty->root); 4309f3a7427SChris Mason if (ret != -EAGAIN) { 4319f3a7427SChris Mason break; 4329f3a7427SChris Mason } 43358176a96SJosef Bacik 4349f3a7427SChris Mason err = btrfs_update_root(trans, 4359f3a7427SChris Mason tree_root, 4369f3a7427SChris Mason &dirty->root->root_key, 4379f3a7427SChris Mason &dirty->root->root_item); 4389f3a7427SChris Mason if (err) 4399f3a7427SChris Mason ret = err; 440d3c2fdcfSChris Mason nr = trans->blocks_used; 4419f3a7427SChris Mason ret = btrfs_end_transaction(trans, tree_root); 4420f7d52f4SChris Mason BUG_ON(ret); 443f4468e94SChris Mason mutex_unlock(&tree_root->fs_info->fs_mutex); 444d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 4454dc11904SChris Mason cond_resched(); 446f4468e94SChris Mason mutex_lock(&tree_root->fs_info->fs_mutex); 4479f3a7427SChris Mason } 4489f3a7427SChris Mason BUG_ON(ret); 44958176a96SJosef Bacik 450db94535dSChris Mason num_bytes -= btrfs_root_used(&dirty->root->root_item); 451db94535dSChris Mason bytes_used = btrfs_root_used(&root->root_item); 452db94535dSChris Mason if (num_bytes) { 45358176a96SJosef Bacik record_root_in_trans(root); 4545f39d397SChris Mason btrfs_set_root_used(&root->root_item, 455db94535dSChris Mason bytes_used - num_bytes); 45658176a96SJosef Bacik } 4579f3a7427SChris Mason ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); 45858176a96SJosef Bacik if (ret) { 45958176a96SJosef Bacik BUG(); 46054aa1f4dSChris Mason break; 46158176a96SJosef Bacik } 462d3c2fdcfSChris Mason nr = trans->blocks_used; 4630f7d52f4SChris Mason ret = btrfs_end_transaction(trans, tree_root); 4640f7d52f4SChris Mason BUG_ON(ret); 4655eda7b5eSChris Mason 466f510cfecSChris Mason free_extent_buffer(dirty->root->node); 4675eda7b5eSChris Mason kfree(dirty->root); 4680f7d52f4SChris Mason kfree(dirty); 469facda1e7SChris Mason mutex_unlock(&tree_root->fs_info->fs_mutex); 470d3c2fdcfSChris Mason 471d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 4724dc11904SChris Mason cond_resched(); 4730f7d52f4SChris Mason } 47454aa1f4dSChris Mason return ret; 4750f7d52f4SChris Mason } 4760f7d52f4SChris Mason 477*dc17ff8fSChris Mason int btrfs_write_ordered_inodes(struct btrfs_trans_handle *trans, 478*dc17ff8fSChris Mason struct btrfs_root *root) 479*dc17ff8fSChris Mason { 480*dc17ff8fSChris Mason struct btrfs_transaction *cur_trans = trans->transaction; 481*dc17ff8fSChris Mason struct inode *inode; 482*dc17ff8fSChris Mason u64 root_objectid = 0; 483*dc17ff8fSChris Mason u64 objectid = 0; 484*dc17ff8fSChris Mason u64 transid = trans->transid; 485*dc17ff8fSChris Mason int ret; 486*dc17ff8fSChris Mason 487*dc17ff8fSChris Mason printk("write ordered trans %Lu\n", transid); 488*dc17ff8fSChris Mason while(1) { 489*dc17ff8fSChris Mason ret = btrfs_find_first_ordered_inode( 490*dc17ff8fSChris Mason &cur_trans->ordered_inode_tree, 491*dc17ff8fSChris Mason &root_objectid, &objectid); 492*dc17ff8fSChris Mason if (!ret) 493*dc17ff8fSChris Mason break; 494*dc17ff8fSChris Mason 495*dc17ff8fSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 496*dc17ff8fSChris Mason mutex_unlock(&root->fs_info->fs_mutex); 497*dc17ff8fSChris Mason inode = btrfs_ilookup(root->fs_info->sb, objectid, 498*dc17ff8fSChris Mason root_objectid); 499*dc17ff8fSChris Mason if (inode) { 500*dc17ff8fSChris Mason if (S_ISREG(inode->i_mode)) 501*dc17ff8fSChris Mason filemap_fdatawrite(inode->i_mapping); 502*dc17ff8fSChris Mason iput(inode); 503*dc17ff8fSChris Mason } 504*dc17ff8fSChris Mason mutex_lock(&root->fs_info->fs_mutex); 505*dc17ff8fSChris Mason mutex_lock(&root->fs_info->trans_mutex); 506*dc17ff8fSChris Mason } 507*dc17ff8fSChris Mason while(1) { 508*dc17ff8fSChris Mason root_objectid = 0; 509*dc17ff8fSChris Mason objectid = 0; 510*dc17ff8fSChris Mason ret = btrfs_find_del_first_ordered_inode( 511*dc17ff8fSChris Mason &cur_trans->ordered_inode_tree, 512*dc17ff8fSChris Mason &root_objectid, &objectid); 513*dc17ff8fSChris Mason if (!ret) 514*dc17ff8fSChris Mason break; 515*dc17ff8fSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 516*dc17ff8fSChris Mason mutex_unlock(&root->fs_info->fs_mutex); 517*dc17ff8fSChris Mason inode = btrfs_ilookup(root->fs_info->sb, objectid, 518*dc17ff8fSChris Mason root_objectid); 519*dc17ff8fSChris Mason if (inode) { 520*dc17ff8fSChris Mason if (S_ISREG(inode->i_mode)) 521*dc17ff8fSChris Mason filemap_write_and_wait(inode->i_mapping); 522*dc17ff8fSChris Mason iput(inode); 523*dc17ff8fSChris Mason } 524*dc17ff8fSChris Mason mutex_lock(&root->fs_info->fs_mutex); 525*dc17ff8fSChris Mason mutex_lock(&root->fs_info->trans_mutex); 526*dc17ff8fSChris Mason } 527*dc17ff8fSChris Mason printk("done write ordered trans %Lu\n", transid); 528*dc17ff8fSChris Mason return 0; 529*dc17ff8fSChris Mason } 530*dc17ff8fSChris Mason 53179154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 53279154b1bSChris Mason struct btrfs_root *root) 53379154b1bSChris Mason { 53415ee9bc7SJosef Bacik unsigned long joined = 0; 53515ee9bc7SJosef Bacik unsigned long timeout = 1; 53679154b1bSChris Mason struct btrfs_transaction *cur_trans; 5378fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 5380f7d52f4SChris Mason struct list_head dirty_fs_roots; 5394313b399SChris Mason struct extent_map_tree *pinned_copy; 54079154b1bSChris Mason DEFINE_WAIT(wait); 54115ee9bc7SJosef Bacik int ret; 54279154b1bSChris Mason 5430f7d52f4SChris Mason INIT_LIST_HEAD(&dirty_fs_roots); 544d6e4a428SChris Mason 54579154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 54679154b1bSChris Mason if (trans->transaction->in_commit) { 54779154b1bSChris Mason cur_trans = trans->transaction; 54879154b1bSChris Mason trans->transaction->use_count++; 549ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 55079154b1bSChris Mason btrfs_end_transaction(trans, root); 551ccd467d6SChris Mason 552ccd467d6SChris Mason mutex_unlock(&root->fs_info->fs_mutex); 55379154b1bSChris Mason ret = wait_for_commit(root, cur_trans); 55479154b1bSChris Mason BUG_ON(ret); 55515ee9bc7SJosef Bacik 55615ee9bc7SJosef Bacik mutex_lock(&root->fs_info->trans_mutex); 55779154b1bSChris Mason put_transaction(cur_trans); 55815ee9bc7SJosef Bacik mutex_unlock(&root->fs_info->trans_mutex); 55915ee9bc7SJosef Bacik 560ccd467d6SChris Mason mutex_lock(&root->fs_info->fs_mutex); 56179154b1bSChris Mason return 0; 56279154b1bSChris Mason } 5634313b399SChris Mason 5644313b399SChris Mason pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS); 5654313b399SChris Mason if (!pinned_copy) 5664313b399SChris Mason return -ENOMEM; 5674313b399SChris Mason 5684313b399SChris Mason extent_map_tree_init(pinned_copy, 5694313b399SChris Mason root->fs_info->btree_inode->i_mapping, GFP_NOFS); 5704313b399SChris Mason 5712c90e5d6SChris Mason trans->transaction->in_commit = 1; 572ccd467d6SChris Mason cur_trans = trans->transaction; 573ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 574ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 575ccd467d6SChris Mason struct btrfs_transaction, list); 576ccd467d6SChris Mason if (!prev_trans->commit_done) { 577ccd467d6SChris Mason prev_trans->use_count++; 578ccd467d6SChris Mason mutex_unlock(&root->fs_info->fs_mutex); 579ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 580ccd467d6SChris Mason 581ccd467d6SChris Mason wait_for_commit(root, prev_trans); 582ccd467d6SChris Mason 583ccd467d6SChris Mason mutex_lock(&root->fs_info->fs_mutex); 584ccd467d6SChris Mason mutex_lock(&root->fs_info->trans_mutex); 58515ee9bc7SJosef Bacik put_transaction(prev_trans); 586ccd467d6SChris Mason } 587ccd467d6SChris Mason } 58815ee9bc7SJosef Bacik 58915ee9bc7SJosef Bacik do { 59015ee9bc7SJosef Bacik joined = cur_trans->num_joined; 5912c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 59215ee9bc7SJosef Bacik prepare_to_wait(&cur_trans->writer_wait, &wait, 59379154b1bSChris Mason TASK_UNINTERRUPTIBLE); 59415ee9bc7SJosef Bacik 59515ee9bc7SJosef Bacik if (cur_trans->num_writers > 1) 59615ee9bc7SJosef Bacik timeout = MAX_SCHEDULE_TIMEOUT; 59715ee9bc7SJosef Bacik else 59815ee9bc7SJosef Bacik timeout = 1; 59915ee9bc7SJosef Bacik 600ccd467d6SChris Mason mutex_unlock(&root->fs_info->fs_mutex); 60179154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 60215ee9bc7SJosef Bacik 60315ee9bc7SJosef Bacik schedule_timeout(timeout); 60415ee9bc7SJosef Bacik 605ccd467d6SChris Mason mutex_lock(&root->fs_info->fs_mutex); 60679154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 60715ee9bc7SJosef Bacik finish_wait(&cur_trans->writer_wait, &wait); 608*dc17ff8fSChris Mason ret = btrfs_write_ordered_inodes(trans, root); 609*dc17ff8fSChris Mason 61015ee9bc7SJosef Bacik } while (cur_trans->num_writers > 1 || 61115ee9bc7SJosef Bacik (cur_trans->num_joined != joined)); 61215ee9bc7SJosef Bacik 6132c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 614*dc17ff8fSChris Mason 61554aa1f4dSChris Mason ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, 61654aa1f4dSChris Mason &dirty_fs_roots); 61754aa1f4dSChris Mason BUG_ON(ret); 61854aa1f4dSChris Mason 61979154b1bSChris Mason ret = btrfs_commit_tree_roots(trans, root); 62079154b1bSChris Mason BUG_ON(ret); 62154aa1f4dSChris Mason 62278fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 62378fae27eSChris Mason root->fs_info->running_transaction = NULL; 6244b52dff6SChris Mason btrfs_set_super_generation(&root->fs_info->super_copy, 6254b52dff6SChris Mason cur_trans->transid); 6264b52dff6SChris Mason btrfs_set_super_root(&root->fs_info->super_copy, 627db94535dSChris Mason root->fs_info->tree_root->node->start); 628db94535dSChris Mason btrfs_set_super_root_level(&root->fs_info->super_copy, 629db94535dSChris Mason btrfs_header_level(root->fs_info->tree_root->node)); 6305f39d397SChris Mason 6315f39d397SChris Mason write_extent_buffer(root->fs_info->sb_buffer, 6325f39d397SChris Mason &root->fs_info->super_copy, 0, 6334b52dff6SChris Mason sizeof(root->fs_info->super_copy)); 634ccd467d6SChris Mason 6354313b399SChris Mason btrfs_copy_pinned(root, pinned_copy); 636ccd467d6SChris Mason 63778fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 6388fd17795SChris Mason mutex_unlock(&root->fs_info->fs_mutex); 63979154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 64079154b1bSChris Mason BUG_ON(ret); 64179154b1bSChris Mason write_ctree_super(trans, root); 6424313b399SChris Mason 6438fd17795SChris Mason mutex_lock(&root->fs_info->fs_mutex); 6444313b399SChris Mason btrfs_finish_extent_commit(trans, root, pinned_copy); 64578fae27eSChris Mason mutex_lock(&root->fs_info->trans_mutex); 6464313b399SChris Mason 6474313b399SChris Mason kfree(pinned_copy); 6484313b399SChris Mason 6492c90e5d6SChris Mason cur_trans->commit_done = 1; 65015ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 6512c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 65279154b1bSChris Mason put_transaction(cur_trans); 65378fae27eSChris Mason put_transaction(cur_trans); 65458176a96SJosef Bacik 655facda1e7SChris Mason if (root->fs_info->closing) 656facda1e7SChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); 657facda1e7SChris Mason else 658facda1e7SChris Mason list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); 65958176a96SJosef Bacik 66078fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 6612c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 66279154b1bSChris Mason 663facda1e7SChris Mason if (root->fs_info->closing) { 664facda1e7SChris Mason mutex_unlock(&root->fs_info->fs_mutex); 6650f7d52f4SChris Mason drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); 666facda1e7SChris Mason mutex_lock(&root->fs_info->fs_mutex); 667facda1e7SChris Mason } 66879154b1bSChris Mason return ret; 66979154b1bSChris Mason } 67079154b1bSChris Mason 671e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root) 672e9d0b13bSChris Mason { 673e9d0b13bSChris Mason struct list_head dirty_roots; 674e9d0b13bSChris Mason INIT_LIST_HEAD(&dirty_roots); 675e9d0b13bSChris Mason 676e9d0b13bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 677e9d0b13bSChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_roots); 678e9d0b13bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 679e9d0b13bSChris Mason 680e9d0b13bSChris Mason if (!list_empty(&dirty_roots)) { 681e9d0b13bSChris Mason drop_dirty_roots(root, &dirty_roots); 682e9d0b13bSChris Mason } 683e9d0b13bSChris Mason return 0; 684e9d0b13bSChris Mason } 6856da6abaeSChris Mason #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) 6866da6abaeSChris Mason void btrfs_transaction_cleaner(void *p) 6876da6abaeSChris Mason #else 68808607c1bSChris Mason void btrfs_transaction_cleaner(struct work_struct *work) 6896da6abaeSChris Mason #endif 69008607c1bSChris Mason { 6916da6abaeSChris Mason #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) 6926da6abaeSChris Mason struct btrfs_fs_info *fs_info = p; 6936da6abaeSChris Mason #else 69408607c1bSChris Mason struct btrfs_fs_info *fs_info = container_of(work, 69508607c1bSChris Mason struct btrfs_fs_info, 69608607c1bSChris Mason trans_work.work); 69708607c1bSChris Mason 6986da6abaeSChris Mason #endif 69908607c1bSChris Mason struct btrfs_root *root = fs_info->tree_root; 70008607c1bSChris Mason struct btrfs_transaction *cur; 70108607c1bSChris Mason struct btrfs_trans_handle *trans; 70208607c1bSChris Mason unsigned long now; 70308607c1bSChris Mason unsigned long delay = HZ * 30; 70408607c1bSChris Mason int ret; 70508607c1bSChris Mason 70608607c1bSChris Mason mutex_lock(&root->fs_info->fs_mutex); 70708607c1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 70808607c1bSChris Mason cur = root->fs_info->running_transaction; 70908607c1bSChris Mason if (!cur) { 71008607c1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 71108607c1bSChris Mason goto out; 71208607c1bSChris Mason } 71308607c1bSChris Mason now = get_seconds(); 71408607c1bSChris Mason if (now < cur->start_time || now - cur->start_time < 30) { 71508607c1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 71608607c1bSChris Mason delay = HZ * 5; 71708607c1bSChris Mason goto out; 71808607c1bSChris Mason } 71908607c1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 7206702ed49SChris Mason btrfs_defrag_dirty_roots(root->fs_info); 72108607c1bSChris Mason trans = btrfs_start_transaction(root, 1); 72208607c1bSChris Mason ret = btrfs_commit_transaction(trans, root); 72308607c1bSChris Mason out: 72408607c1bSChris Mason mutex_unlock(&root->fs_info->fs_mutex); 725e9d0b13bSChris Mason btrfs_clean_old_snapshots(root); 72608607c1bSChris Mason btrfs_transaction_queue_work(root, delay); 72708607c1bSChris Mason } 72808607c1bSChris Mason 72908607c1bSChris Mason void btrfs_transaction_queue_work(struct btrfs_root *root, int delay) 73008607c1bSChris Mason { 73108607c1bSChris Mason queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay); 73208607c1bSChris Mason } 73308607c1bSChris Mason 73408607c1bSChris Mason void btrfs_transaction_flush_work(struct btrfs_root *root) 73508607c1bSChris Mason { 73608607c1bSChris Mason cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work); 73708607c1bSChris Mason flush_workqueue(trans_wq); 73808607c1bSChris Mason } 73908607c1bSChris Mason 74008607c1bSChris Mason void __init btrfs_init_transaction_sys(void) 74108607c1bSChris Mason { 74208607c1bSChris Mason trans_wq = create_workqueue("btrfs"); 74308607c1bSChris Mason } 74408607c1bSChris Mason 74517636e03SChristian Hesse void btrfs_exit_transaction_sys(void) 74608607c1bSChris Mason { 74708607c1bSChris Mason destroy_workqueue(trans_wq); 74808607c1bSChris Mason } 74908607c1bSChris Mason 750