Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 19 | #include <linux/fs.h> |
Chris Mason | 3408878 | 2007-06-12 11:36:58 -0400 | [diff] [blame] | 20 | #include <linux/sched.h> |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 21 | #include <linux/writeback.h> |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 22 | #include <linux/pagemap.h> |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 23 | #include "ctree.h" |
| 24 | #include "disk-io.h" |
| 25 | #include "transaction.h" |
| 26 | |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 27 | static int total_trans = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 28 | extern struct kmem_cache *btrfs_trans_handle_cachep; |
| 29 | extern struct kmem_cache *btrfs_transaction_cachep; |
| 30 | |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 31 | static struct workqueue_struct *trans_wq; |
| 32 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 33 | #define BTRFS_ROOT_TRANS_TAG 0 |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 34 | #define BTRFS_ROOT_DEFRAG_TAG 1 |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 35 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 36 | static void put_transaction(struct btrfs_transaction *transaction) |
| 37 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 38 | WARN_ON(transaction->use_count == 0); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 39 | transaction->use_count--; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 40 | if (transaction->use_count == 0) { |
| 41 | WARN_ON(total_trans == 0); |
| 42 | total_trans--; |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 43 | list_del_init(&transaction->list); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 44 | memset(transaction, 0, sizeof(*transaction)); |
| 45 | kmem_cache_free(btrfs_transaction_cachep, transaction); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 46 | } |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static int join_transaction(struct btrfs_root *root) |
| 50 | { |
| 51 | struct btrfs_transaction *cur_trans; |
| 52 | cur_trans = root->fs_info->running_transaction; |
| 53 | if (!cur_trans) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 54 | cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, |
| 55 | GFP_NOFS); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 56 | total_trans++; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 57 | BUG_ON(!cur_trans); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 58 | root->fs_info->generation++; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 59 | root->fs_info->running_transaction = cur_trans; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 60 | cur_trans->num_writers = 1; |
| 61 | cur_trans->num_joined = 0; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 62 | cur_trans->transid = root->fs_info->generation; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 63 | init_waitqueue_head(&cur_trans->writer_wait); |
| 64 | init_waitqueue_head(&cur_trans->commit_wait); |
| 65 | cur_trans->in_commit = 0; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 66 | cur_trans->use_count = 1; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 67 | cur_trans->commit_done = 0; |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 68 | cur_trans->start_time = get_seconds(); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 69 | list_add_tail(&cur_trans->list, &root->fs_info->trans_list); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame^] | 70 | btrfs_ordered_inode_tree_init(&cur_trans->ordered_inode_tree); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 71 | extent_map_tree_init(&cur_trans->dirty_pages, |
| 72 | root->fs_info->btree_inode->i_mapping, |
| 73 | GFP_NOFS); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 74 | } else { |
| 75 | cur_trans->num_writers++; |
| 76 | cur_trans->num_joined++; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 77 | } |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 78 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 82 | static int record_root_in_trans(struct btrfs_root *root) |
| 83 | { |
| 84 | u64 running_trans_id = root->fs_info->running_transaction->transid; |
| 85 | if (root->ref_cows && root->last_trans < running_trans_id) { |
| 86 | WARN_ON(root == root->fs_info->extent_root); |
| 87 | if (root->root_item.refs != 0) { |
| 88 | radix_tree_tag_set(&root->fs_info->fs_roots_radix, |
| 89 | (unsigned long)root->root_key.objectid, |
| 90 | BTRFS_ROOT_TRANS_TAG); |
| 91 | radix_tree_tag_set(&root->fs_info->fs_roots_radix, |
| 92 | (unsigned long)root->root_key.objectid, |
| 93 | BTRFS_ROOT_DEFRAG_TAG); |
| 94 | root->commit_root = root->node; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 95 | extent_buffer_get(root->node); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 96 | } else { |
| 97 | WARN_ON(1); |
| 98 | } |
| 99 | root->last_trans = running_trans_id; |
| 100 | } |
| 101 | return 0; |
| 102 | } |
| 103 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 104 | struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, |
| 105 | int num_blocks) |
| 106 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 107 | struct btrfs_trans_handle *h = |
| 108 | kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 109 | int ret; |
| 110 | |
| 111 | mutex_lock(&root->fs_info->trans_mutex); |
| 112 | ret = join_transaction(root); |
| 113 | BUG_ON(ret); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 114 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 115 | record_root_in_trans(root); |
| 116 | h->transid = root->fs_info->running_transaction->transid; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 117 | h->transaction = root->fs_info->running_transaction; |
| 118 | h->blocks_reserved = num_blocks; |
| 119 | h->blocks_used = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 120 | h->block_group = NULL; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 121 | h->alloc_exclude_nr = 0; |
| 122 | h->alloc_exclude_start = 0; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 123 | root->fs_info->running_transaction->use_count++; |
| 124 | mutex_unlock(&root->fs_info->trans_mutex); |
| 125 | return h; |
| 126 | } |
| 127 | |
| 128 | int btrfs_end_transaction(struct btrfs_trans_handle *trans, |
| 129 | struct btrfs_root *root) |
| 130 | { |
| 131 | struct btrfs_transaction *cur_trans; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 132 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 133 | mutex_lock(&root->fs_info->trans_mutex); |
| 134 | cur_trans = root->fs_info->running_transaction; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 135 | WARN_ON(cur_trans != trans->transaction); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 136 | WARN_ON(cur_trans->num_writers < 1); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 137 | cur_trans->num_writers--; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 138 | if (waitqueue_active(&cur_trans->writer_wait)) |
| 139 | wake_up(&cur_trans->writer_wait); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 140 | put_transaction(cur_trans); |
| 141 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 142 | memset(trans, 0, sizeof(*trans)); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 143 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, |
| 149 | struct btrfs_root *root) |
| 150 | { |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 151 | int ret; |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 152 | int err; |
| 153 | int werr = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 154 | struct extent_map_tree *dirty_pages; |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 155 | struct page *page; |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 156 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 157 | u64 start; |
| 158 | u64 end; |
| 159 | unsigned long index; |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 160 | |
| 161 | if (!trans || !trans->transaction) { |
| 162 | return filemap_write_and_wait(btree_inode->i_mapping); |
| 163 | } |
| 164 | dirty_pages = &trans->transaction->dirty_pages; |
| 165 | while(1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 166 | ret = find_first_extent_bit(dirty_pages, 0, &start, &end, |
| 167 | EXTENT_DIRTY); |
| 168 | if (ret) |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 169 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 170 | clear_extent_dirty(dirty_pages, start, end, GFP_NOFS); |
| 171 | while(start <= end) { |
| 172 | index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 173 | start = (u64)(index + 1) << PAGE_CACHE_SHIFT; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 174 | page = find_lock_page(btree_inode->i_mapping, index); |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 175 | if (!page) |
| 176 | continue; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 177 | if (PageWriteback(page)) { |
| 178 | if (PageDirty(page)) |
| 179 | wait_on_page_writeback(page); |
| 180 | else { |
| 181 | unlock_page(page); |
| 182 | page_cache_release(page); |
| 183 | continue; |
| 184 | } |
| 185 | } |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 186 | err = write_one_page(page, 0); |
| 187 | if (err) |
| 188 | werr = err; |
| 189 | page_cache_release(page); |
| 190 | } |
| 191 | } |
| 192 | err = filemap_fdatawait(btree_inode->i_mapping); |
| 193 | if (err) |
| 194 | werr = err; |
| 195 | return werr; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, |
| 199 | struct btrfs_root *root) |
| 200 | { |
| 201 | int ret; |
| 202 | u64 old_extent_block; |
| 203 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 204 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 205 | struct btrfs_root *extent_root = fs_info->extent_root; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 206 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 207 | btrfs_write_dirty_block_groups(trans, extent_root); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 208 | while(1) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 209 | old_extent_block = btrfs_root_bytenr(&extent_root->root_item); |
| 210 | if (old_extent_block == extent_root->node->start) |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 211 | break; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 212 | btrfs_set_root_bytenr(&extent_root->root_item, |
| 213 | extent_root->node->start); |
| 214 | btrfs_set_root_level(&extent_root->root_item, |
| 215 | btrfs_header_level(extent_root->node)); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 216 | ret = btrfs_update_root(trans, tree_root, |
| 217 | &extent_root->root_key, |
| 218 | &extent_root->root_item); |
| 219 | BUG_ON(ret); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 220 | btrfs_write_dirty_block_groups(trans, extent_root); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 221 | } |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static int wait_for_commit(struct btrfs_root *root, |
| 226 | struct btrfs_transaction *commit) |
| 227 | { |
| 228 | DEFINE_WAIT(wait); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 229 | mutex_lock(&root->fs_info->trans_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 230 | while(!commit->commit_done) { |
| 231 | prepare_to_wait(&commit->commit_wait, &wait, |
| 232 | TASK_UNINTERRUPTIBLE); |
| 233 | if (commit->commit_done) |
| 234 | break; |
| 235 | mutex_unlock(&root->fs_info->trans_mutex); |
| 236 | schedule(); |
| 237 | mutex_lock(&root->fs_info->trans_mutex); |
| 238 | } |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 239 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 240 | finish_wait(&commit->commit_wait, &wait); |
| 241 | return 0; |
| 242 | } |
| 243 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 244 | struct dirty_root { |
| 245 | struct list_head list; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 246 | struct btrfs_root *root; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 247 | struct btrfs_root *latest_root; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 248 | }; |
| 249 | |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 250 | int btrfs_add_dead_root(struct btrfs_root *root, |
| 251 | struct btrfs_root *latest, |
| 252 | struct list_head *dead_list) |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 253 | { |
| 254 | struct dirty_root *dirty; |
| 255 | |
| 256 | dirty = kmalloc(sizeof(*dirty), GFP_NOFS); |
| 257 | if (!dirty) |
| 258 | return -ENOMEM; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 259 | dirty->root = root; |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 260 | dirty->latest_root = latest; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 261 | list_add(&dirty->list, dead_list); |
| 262 | return 0; |
| 263 | } |
| 264 | |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 265 | static int add_dirty_roots(struct btrfs_trans_handle *trans, |
| 266 | struct radix_tree_root *radix, |
| 267 | struct list_head *list) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 268 | { |
| 269 | struct dirty_root *dirty; |
| 270 | struct btrfs_root *gang[8]; |
| 271 | struct btrfs_root *root; |
| 272 | int i; |
| 273 | int ret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 274 | int err = 0; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 275 | u32 refs; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 276 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 277 | while(1) { |
| 278 | ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, |
| 279 | ARRAY_SIZE(gang), |
| 280 | BTRFS_ROOT_TRANS_TAG); |
| 281 | if (ret == 0) |
| 282 | break; |
| 283 | for (i = 0; i < ret; i++) { |
| 284 | root = gang[i]; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 285 | radix_tree_tag_clear(radix, |
| 286 | (unsigned long)root->root_key.objectid, |
| 287 | BTRFS_ROOT_TRANS_TAG); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 288 | if (root->commit_root == root->node) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 289 | WARN_ON(root->node->start != |
| 290 | btrfs_root_bytenr(&root->root_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 291 | free_extent_buffer(root->commit_root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 292 | root->commit_root = NULL; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 293 | |
| 294 | /* make sure to update the root on disk |
| 295 | * so we get any updates to the block used |
| 296 | * counts |
| 297 | */ |
| 298 | err = btrfs_update_root(trans, |
| 299 | root->fs_info->tree_root, |
| 300 | &root->root_key, |
| 301 | &root->root_item); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 302 | continue; |
| 303 | } |
| 304 | dirty = kmalloc(sizeof(*dirty), GFP_NOFS); |
| 305 | BUG_ON(!dirty); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 306 | dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS); |
| 307 | BUG_ON(!dirty->root); |
| 308 | |
| 309 | memset(&root->root_item.drop_progress, 0, |
| 310 | sizeof(struct btrfs_disk_key)); |
| 311 | root->root_item.drop_level = 0; |
| 312 | |
| 313 | memcpy(dirty->root, root, sizeof(*root)); |
| 314 | dirty->root->node = root->commit_root; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 315 | dirty->latest_root = root; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 316 | root->commit_root = NULL; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 317 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 318 | root->root_key.offset = root->fs_info->generation; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 319 | btrfs_set_root_bytenr(&root->root_item, |
| 320 | root->node->start); |
| 321 | btrfs_set_root_level(&root->root_item, |
| 322 | btrfs_header_level(root->node)); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 323 | err = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 324 | &root->root_key, |
| 325 | &root->root_item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 326 | if (err) |
| 327 | break; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 328 | |
| 329 | refs = btrfs_root_refs(&dirty->root->root_item); |
| 330 | btrfs_set_root_refs(&dirty->root->root_item, refs - 1); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 331 | err = btrfs_update_root(trans, root->fs_info->tree_root, |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 332 | &dirty->root->root_key, |
| 333 | &dirty->root->root_item); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 334 | |
| 335 | BUG_ON(err); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 336 | if (refs == 1) { |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 337 | list_add(&dirty->list, list); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 338 | } else { |
| 339 | WARN_ON(1); |
| 340 | kfree(dirty->root); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 341 | kfree(dirty); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 342 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 343 | } |
| 344 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 345 | return err; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 346 | } |
| 347 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 348 | int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) |
| 349 | { |
| 350 | struct btrfs_fs_info *info = root->fs_info; |
| 351 | int ret; |
| 352 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 353 | unsigned long nr; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 354 | |
| 355 | if (root->defrag_running) |
| 356 | return 0; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 357 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 358 | while (1) { |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 359 | root->defrag_running = 1; |
| 360 | ret = btrfs_defrag_leaves(trans, root, cacheonly); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 361 | nr = trans->blocks_used; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 362 | btrfs_end_transaction(trans, root); |
| 363 | mutex_unlock(&info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 364 | btrfs_btree_balance_dirty(info->tree_root, nr); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 365 | cond_resched(); |
| 366 | |
| 367 | mutex_lock(&info->fs_mutex); |
| 368 | trans = btrfs_start_transaction(root, 1); |
| 369 | if (ret != -EAGAIN) |
| 370 | break; |
| 371 | } |
| 372 | root->defrag_running = 0; |
| 373 | radix_tree_tag_clear(&info->fs_roots_radix, |
| 374 | (unsigned long)root->root_key.objectid, |
| 375 | BTRFS_ROOT_DEFRAG_TAG); |
| 376 | btrfs_end_transaction(trans, root); |
| 377 | return 0; |
| 378 | } |
| 379 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 380 | int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info) |
| 381 | { |
| 382 | struct btrfs_root *gang[1]; |
| 383 | struct btrfs_root *root; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 384 | int i; |
| 385 | int ret; |
| 386 | int err = 0; |
| 387 | u64 last = 0; |
| 388 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 389 | while(1) { |
| 390 | ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix, |
| 391 | (void **)gang, last, |
| 392 | ARRAY_SIZE(gang), |
| 393 | BTRFS_ROOT_DEFRAG_TAG); |
| 394 | if (ret == 0) |
| 395 | break; |
| 396 | for (i = 0; i < ret; i++) { |
| 397 | root = gang[i]; |
| 398 | last = root->root_key.objectid + 1; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 399 | btrfs_defrag_root(root, 1); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 400 | } |
| 401 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 402 | btrfs_defrag_root(info->extent_root, 1); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 403 | return err; |
| 404 | } |
| 405 | |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 406 | static int drop_dirty_roots(struct btrfs_root *tree_root, |
| 407 | struct list_head *list) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 408 | { |
| 409 | struct dirty_root *dirty; |
| 410 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 411 | unsigned long nr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 412 | u64 num_bytes; |
| 413 | u64 bytes_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 414 | int ret = 0; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 415 | int err; |
| 416 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 417 | while(!list_empty(list)) { |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 418 | struct btrfs_root *root; |
| 419 | |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 420 | mutex_lock(&tree_root->fs_info->fs_mutex); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 421 | dirty = list_entry(list->next, struct dirty_root, list); |
| 422 | list_del_init(&dirty->list); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 423 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 424 | num_bytes = btrfs_root_used(&dirty->root->root_item); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 425 | root = dirty->latest_root; |
| 426 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 427 | while(1) { |
| 428 | trans = btrfs_start_transaction(tree_root, 1); |
| 429 | ret = btrfs_drop_snapshot(trans, dirty->root); |
| 430 | if (ret != -EAGAIN) { |
| 431 | break; |
| 432 | } |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 433 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 434 | err = btrfs_update_root(trans, |
| 435 | tree_root, |
| 436 | &dirty->root->root_key, |
| 437 | &dirty->root->root_item); |
| 438 | if (err) |
| 439 | ret = err; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 440 | nr = trans->blocks_used; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 441 | ret = btrfs_end_transaction(trans, tree_root); |
| 442 | BUG_ON(ret); |
Chris Mason | f4468e9 | 2007-08-08 10:08:58 -0400 | [diff] [blame] | 443 | mutex_unlock(&tree_root->fs_info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 444 | btrfs_btree_balance_dirty(tree_root, nr); |
Chris Mason | 4dc11904 | 2007-10-15 16:18:14 -0400 | [diff] [blame] | 445 | cond_resched(); |
Chris Mason | f4468e9 | 2007-08-08 10:08:58 -0400 | [diff] [blame] | 446 | mutex_lock(&tree_root->fs_info->fs_mutex); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 447 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 448 | BUG_ON(ret); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 449 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 450 | num_bytes -= btrfs_root_used(&dirty->root->root_item); |
| 451 | bytes_used = btrfs_root_used(&root->root_item); |
| 452 | if (num_bytes) { |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 453 | record_root_in_trans(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 454 | btrfs_set_root_used(&root->root_item, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 455 | bytes_used - num_bytes); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 456 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 457 | ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 458 | if (ret) { |
| 459 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 460 | break; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 461 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 462 | nr = trans->blocks_used; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 463 | ret = btrfs_end_transaction(trans, tree_root); |
| 464 | BUG_ON(ret); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 465 | |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 466 | free_extent_buffer(dirty->root->node); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 467 | kfree(dirty->root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 468 | kfree(dirty); |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 469 | mutex_unlock(&tree_root->fs_info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 470 | |
| 471 | btrfs_btree_balance_dirty(tree_root, nr); |
Chris Mason | 4dc11904 | 2007-10-15 16:18:14 -0400 | [diff] [blame] | 472 | cond_resched(); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 473 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 474 | return ret; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 475 | } |
| 476 | |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame^] | 477 | int btrfs_write_ordered_inodes(struct btrfs_trans_handle *trans, |
| 478 | struct btrfs_root *root) |
| 479 | { |
| 480 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 481 | struct inode *inode; |
| 482 | u64 root_objectid = 0; |
| 483 | u64 objectid = 0; |
| 484 | u64 transid = trans->transid; |
| 485 | int ret; |
| 486 | |
| 487 | printk("write ordered trans %Lu\n", transid); |
| 488 | while(1) { |
| 489 | ret = btrfs_find_first_ordered_inode( |
| 490 | &cur_trans->ordered_inode_tree, |
| 491 | &root_objectid, &objectid); |
| 492 | if (!ret) |
| 493 | break; |
| 494 | |
| 495 | mutex_unlock(&root->fs_info->trans_mutex); |
| 496 | mutex_unlock(&root->fs_info->fs_mutex); |
| 497 | inode = btrfs_ilookup(root->fs_info->sb, objectid, |
| 498 | root_objectid); |
| 499 | if (inode) { |
| 500 | if (S_ISREG(inode->i_mode)) |
| 501 | filemap_fdatawrite(inode->i_mapping); |
| 502 | iput(inode); |
| 503 | } |
| 504 | mutex_lock(&root->fs_info->fs_mutex); |
| 505 | mutex_lock(&root->fs_info->trans_mutex); |
| 506 | } |
| 507 | while(1) { |
| 508 | root_objectid = 0; |
| 509 | objectid = 0; |
| 510 | ret = btrfs_find_del_first_ordered_inode( |
| 511 | &cur_trans->ordered_inode_tree, |
| 512 | &root_objectid, &objectid); |
| 513 | if (!ret) |
| 514 | break; |
| 515 | mutex_unlock(&root->fs_info->trans_mutex); |
| 516 | mutex_unlock(&root->fs_info->fs_mutex); |
| 517 | inode = btrfs_ilookup(root->fs_info->sb, objectid, |
| 518 | root_objectid); |
| 519 | if (inode) { |
| 520 | if (S_ISREG(inode->i_mode)) |
| 521 | filemap_write_and_wait(inode->i_mapping); |
| 522 | iput(inode); |
| 523 | } |
| 524 | mutex_lock(&root->fs_info->fs_mutex); |
| 525 | mutex_lock(&root->fs_info->trans_mutex); |
| 526 | } |
| 527 | printk("done write ordered trans %Lu\n", transid); |
| 528 | return 0; |
| 529 | } |
| 530 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 531 | int btrfs_commit_transaction(struct btrfs_trans_handle *trans, |
| 532 | struct btrfs_root *root) |
| 533 | { |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 534 | unsigned long joined = 0; |
| 535 | unsigned long timeout = 1; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 536 | struct btrfs_transaction *cur_trans; |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 537 | struct btrfs_transaction *prev_trans = NULL; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 538 | struct list_head dirty_fs_roots; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 539 | struct extent_map_tree *pinned_copy; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 540 | DEFINE_WAIT(wait); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 541 | int ret; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 542 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 543 | INIT_LIST_HEAD(&dirty_fs_roots); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 544 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 545 | mutex_lock(&root->fs_info->trans_mutex); |
| 546 | if (trans->transaction->in_commit) { |
| 547 | cur_trans = trans->transaction; |
| 548 | trans->transaction->use_count++; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 549 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 550 | btrfs_end_transaction(trans, root); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 551 | |
| 552 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 553 | ret = wait_for_commit(root, cur_trans); |
| 554 | BUG_ON(ret); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 555 | |
| 556 | mutex_lock(&root->fs_info->trans_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 557 | put_transaction(cur_trans); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 558 | mutex_unlock(&root->fs_info->trans_mutex); |
| 559 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 560 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 561 | return 0; |
| 562 | } |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 563 | |
| 564 | pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS); |
| 565 | if (!pinned_copy) |
| 566 | return -ENOMEM; |
| 567 | |
| 568 | extent_map_tree_init(pinned_copy, |
| 569 | root->fs_info->btree_inode->i_mapping, GFP_NOFS); |
| 570 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 571 | trans->transaction->in_commit = 1; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 572 | cur_trans = trans->transaction; |
| 573 | if (cur_trans->list.prev != &root->fs_info->trans_list) { |
| 574 | prev_trans = list_entry(cur_trans->list.prev, |
| 575 | struct btrfs_transaction, list); |
| 576 | if (!prev_trans->commit_done) { |
| 577 | prev_trans->use_count++; |
| 578 | mutex_unlock(&root->fs_info->fs_mutex); |
| 579 | mutex_unlock(&root->fs_info->trans_mutex); |
| 580 | |
| 581 | wait_for_commit(root, prev_trans); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 582 | |
| 583 | mutex_lock(&root->fs_info->fs_mutex); |
| 584 | mutex_lock(&root->fs_info->trans_mutex); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 585 | put_transaction(prev_trans); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 586 | } |
| 587 | } |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 588 | |
| 589 | do { |
| 590 | joined = cur_trans->num_joined; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 591 | WARN_ON(cur_trans != trans->transaction); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 592 | prepare_to_wait(&cur_trans->writer_wait, &wait, |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 593 | TASK_UNINTERRUPTIBLE); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 594 | |
| 595 | if (cur_trans->num_writers > 1) |
| 596 | timeout = MAX_SCHEDULE_TIMEOUT; |
| 597 | else |
| 598 | timeout = 1; |
| 599 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 600 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 601 | mutex_unlock(&root->fs_info->trans_mutex); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 602 | |
| 603 | schedule_timeout(timeout); |
| 604 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 605 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 606 | mutex_lock(&root->fs_info->trans_mutex); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 607 | finish_wait(&cur_trans->writer_wait, &wait); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame^] | 608 | ret = btrfs_write_ordered_inodes(trans, root); |
| 609 | |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 610 | } while (cur_trans->num_writers > 1 || |
| 611 | (cur_trans->num_joined != joined)); |
| 612 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 613 | WARN_ON(cur_trans != trans->transaction); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame^] | 614 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 615 | ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, |
| 616 | &dirty_fs_roots); |
| 617 | BUG_ON(ret); |
| 618 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 619 | ret = btrfs_commit_tree_roots(trans, root); |
| 620 | BUG_ON(ret); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 621 | |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 622 | cur_trans = root->fs_info->running_transaction; |
| 623 | root->fs_info->running_transaction = NULL; |
Chris Mason | 4b52dff | 2007-06-26 10:06:50 -0400 | [diff] [blame] | 624 | btrfs_set_super_generation(&root->fs_info->super_copy, |
| 625 | cur_trans->transid); |
| 626 | btrfs_set_super_root(&root->fs_info->super_copy, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 627 | root->fs_info->tree_root->node->start); |
| 628 | btrfs_set_super_root_level(&root->fs_info->super_copy, |
| 629 | btrfs_header_level(root->fs_info->tree_root->node)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 630 | |
| 631 | write_extent_buffer(root->fs_info->sb_buffer, |
| 632 | &root->fs_info->super_copy, 0, |
| 633 | sizeof(root->fs_info->super_copy)); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 634 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 635 | btrfs_copy_pinned(root, pinned_copy); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 636 | |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 637 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 638 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 639 | ret = btrfs_write_and_wait_transaction(trans, root); |
| 640 | BUG_ON(ret); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 641 | write_ctree_super(trans, root); |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 642 | |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 643 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 644 | btrfs_finish_extent_commit(trans, root, pinned_copy); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 645 | mutex_lock(&root->fs_info->trans_mutex); |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 646 | |
| 647 | kfree(pinned_copy); |
| 648 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 649 | cur_trans->commit_done = 1; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 650 | root->fs_info->last_trans_committed = cur_trans->transid; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 651 | wake_up(&cur_trans->commit_wait); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 652 | put_transaction(cur_trans); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 653 | put_transaction(cur_trans); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 654 | |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 655 | if (root->fs_info->closing) |
| 656 | list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); |
| 657 | else |
| 658 | list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 659 | |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 660 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 661 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 662 | |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 663 | if (root->fs_info->closing) { |
| 664 | mutex_unlock(&root->fs_info->fs_mutex); |
| 665 | drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); |
| 666 | mutex_lock(&root->fs_info->fs_mutex); |
| 667 | } |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 668 | return ret; |
| 669 | } |
| 670 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 671 | int btrfs_clean_old_snapshots(struct btrfs_root *root) |
| 672 | { |
| 673 | struct list_head dirty_roots; |
| 674 | INIT_LIST_HEAD(&dirty_roots); |
| 675 | |
| 676 | mutex_lock(&root->fs_info->trans_mutex); |
| 677 | list_splice_init(&root->fs_info->dead_roots, &dirty_roots); |
| 678 | mutex_unlock(&root->fs_info->trans_mutex); |
| 679 | |
| 680 | if (!list_empty(&dirty_roots)) { |
| 681 | drop_dirty_roots(root, &dirty_roots); |
| 682 | } |
| 683 | return 0; |
| 684 | } |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 685 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
| 686 | void btrfs_transaction_cleaner(void *p) |
| 687 | #else |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 688 | void btrfs_transaction_cleaner(struct work_struct *work) |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 689 | #endif |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 690 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 691 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
| 692 | struct btrfs_fs_info *fs_info = p; |
| 693 | #else |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 694 | struct btrfs_fs_info *fs_info = container_of(work, |
| 695 | struct btrfs_fs_info, |
| 696 | trans_work.work); |
| 697 | |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 698 | #endif |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 699 | struct btrfs_root *root = fs_info->tree_root; |
| 700 | struct btrfs_transaction *cur; |
| 701 | struct btrfs_trans_handle *trans; |
| 702 | unsigned long now; |
| 703 | unsigned long delay = HZ * 30; |
| 704 | int ret; |
| 705 | |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 706 | mutex_lock(&root->fs_info->fs_mutex); |
| 707 | mutex_lock(&root->fs_info->trans_mutex); |
| 708 | cur = root->fs_info->running_transaction; |
| 709 | if (!cur) { |
| 710 | mutex_unlock(&root->fs_info->trans_mutex); |
| 711 | goto out; |
| 712 | } |
| 713 | now = get_seconds(); |
| 714 | if (now < cur->start_time || now - cur->start_time < 30) { |
| 715 | mutex_unlock(&root->fs_info->trans_mutex); |
| 716 | delay = HZ * 5; |
| 717 | goto out; |
| 718 | } |
| 719 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 720 | btrfs_defrag_dirty_roots(root->fs_info); |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 721 | trans = btrfs_start_transaction(root, 1); |
| 722 | ret = btrfs_commit_transaction(trans, root); |
| 723 | out: |
| 724 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 725 | btrfs_clean_old_snapshots(root); |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 726 | btrfs_transaction_queue_work(root, delay); |
| 727 | } |
| 728 | |
| 729 | void btrfs_transaction_queue_work(struct btrfs_root *root, int delay) |
| 730 | { |
| 731 | queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay); |
| 732 | } |
| 733 | |
| 734 | void btrfs_transaction_flush_work(struct btrfs_root *root) |
| 735 | { |
| 736 | cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work); |
| 737 | flush_workqueue(trans_wq); |
| 738 | } |
| 739 | |
| 740 | void __init btrfs_init_transaction_sys(void) |
| 741 | { |
| 742 | trans_wq = create_workqueue("btrfs"); |
| 743 | } |
| 744 | |
Christian Hesse | 17636e0 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 745 | void btrfs_exit_transaction_sys(void) |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 746 | { |
| 747 | destroy_workqueue(trans_wq); |
| 748 | } |
| 749 | |