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 | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 22 | #include "ctree.h" |
| 23 | #include "disk-io.h" |
| 24 | #include "transaction.h" |
| 25 | |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 26 | static int total_trans = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 27 | extern struct kmem_cache *btrfs_trans_handle_cachep; |
| 28 | extern struct kmem_cache *btrfs_transaction_cachep; |
| 29 | |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 30 | static struct workqueue_struct *trans_wq; |
| 31 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 32 | #define BTRFS_ROOT_TRANS_TAG 0 |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 33 | #define BTRFS_ROOT_DEFRAG_TAG 1 |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 34 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 35 | static void put_transaction(struct btrfs_transaction *transaction) |
| 36 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 37 | WARN_ON(transaction->use_count == 0); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 38 | transaction->use_count--; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 39 | if (transaction->use_count == 0) { |
| 40 | WARN_ON(total_trans == 0); |
| 41 | total_trans--; |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 42 | list_del_init(&transaction->list); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 43 | memset(transaction, 0, sizeof(*transaction)); |
| 44 | kmem_cache_free(btrfs_transaction_cachep, transaction); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 45 | } |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static int join_transaction(struct btrfs_root *root) |
| 49 | { |
| 50 | struct btrfs_transaction *cur_trans; |
| 51 | cur_trans = root->fs_info->running_transaction; |
| 52 | if (!cur_trans) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 53 | cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, |
| 54 | GFP_NOFS); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 55 | total_trans++; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 56 | BUG_ON(!cur_trans); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 57 | root->fs_info->generation++; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 58 | root->fs_info->running_transaction = cur_trans; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 59 | cur_trans->num_writers = 1; |
| 60 | cur_trans->num_joined = 0; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 61 | cur_trans->transid = root->fs_info->generation; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 62 | init_waitqueue_head(&cur_trans->writer_wait); |
| 63 | init_waitqueue_head(&cur_trans->commit_wait); |
| 64 | cur_trans->in_commit = 0; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 65 | cur_trans->use_count = 1; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 66 | cur_trans->commit_done = 0; |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 67 | cur_trans->start_time = get_seconds(); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 68 | list_add_tail(&cur_trans->list, &root->fs_info->trans_list); |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 69 | init_bit_radix(&cur_trans->dirty_pages); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 70 | } else { |
| 71 | cur_trans->num_writers++; |
| 72 | cur_trans->num_joined++; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 73 | } |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 74 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 78 | static int record_root_in_trans(struct btrfs_root *root) |
| 79 | { |
| 80 | u64 running_trans_id = root->fs_info->running_transaction->transid; |
| 81 | if (root->ref_cows && root->last_trans < running_trans_id) { |
| 82 | WARN_ON(root == root->fs_info->extent_root); |
| 83 | if (root->root_item.refs != 0) { |
| 84 | radix_tree_tag_set(&root->fs_info->fs_roots_radix, |
| 85 | (unsigned long)root->root_key.objectid, |
| 86 | BTRFS_ROOT_TRANS_TAG); |
| 87 | radix_tree_tag_set(&root->fs_info->fs_roots_radix, |
| 88 | (unsigned long)root->root_key.objectid, |
| 89 | BTRFS_ROOT_DEFRAG_TAG); |
| 90 | root->commit_root = root->node; |
| 91 | get_bh(root->node); |
| 92 | } else { |
| 93 | WARN_ON(1); |
| 94 | } |
| 95 | root->last_trans = running_trans_id; |
| 96 | } |
| 97 | return 0; |
| 98 | } |
| 99 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 100 | struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, |
| 101 | int num_blocks) |
| 102 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 103 | struct btrfs_trans_handle *h = |
| 104 | kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 105 | int ret; |
| 106 | |
| 107 | mutex_lock(&root->fs_info->trans_mutex); |
| 108 | ret = join_transaction(root); |
| 109 | BUG_ON(ret); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 110 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 111 | record_root_in_trans(root); |
| 112 | h->transid = root->fs_info->running_transaction->transid; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 113 | h->transaction = root->fs_info->running_transaction; |
| 114 | h->blocks_reserved = num_blocks; |
| 115 | h->blocks_used = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 116 | h->block_group = NULL; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 117 | h->alloc_exclude_nr = 0; |
| 118 | h->alloc_exclude_start = 0; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 119 | root->fs_info->running_transaction->use_count++; |
| 120 | mutex_unlock(&root->fs_info->trans_mutex); |
| 121 | return h; |
| 122 | } |
| 123 | |
| 124 | int btrfs_end_transaction(struct btrfs_trans_handle *trans, |
| 125 | struct btrfs_root *root) |
| 126 | { |
| 127 | struct btrfs_transaction *cur_trans; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 128 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 129 | mutex_lock(&root->fs_info->trans_mutex); |
| 130 | cur_trans = root->fs_info->running_transaction; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 131 | WARN_ON(cur_trans != trans->transaction); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 132 | WARN_ON(cur_trans->num_writers < 1); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 133 | cur_trans->num_writers--; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 134 | if (waitqueue_active(&cur_trans->writer_wait)) |
| 135 | wake_up(&cur_trans->writer_wait); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 136 | put_transaction(cur_trans); |
| 137 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 138 | memset(trans, 0, sizeof(*trans)); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 139 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, |
| 145 | struct btrfs_root *root) |
| 146 | { |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 147 | unsigned long gang[16]; |
| 148 | int ret; |
| 149 | int i; |
| 150 | int err; |
| 151 | int werr = 0; |
| 152 | struct page *page; |
| 153 | struct radix_tree_root *dirty_pages; |
| 154 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 155 | |
| 156 | if (!trans || !trans->transaction) { |
| 157 | return filemap_write_and_wait(btree_inode->i_mapping); |
| 158 | } |
| 159 | dirty_pages = &trans->transaction->dirty_pages; |
| 160 | while(1) { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 161 | ret = find_first_radix_bit(dirty_pages, gang, |
| 162 | 0, ARRAY_SIZE(gang)); |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 163 | if (!ret) |
| 164 | break; |
| 165 | for (i = 0; i < ret; i++) { |
| 166 | /* FIXME EIO */ |
| 167 | clear_radix_bit(dirty_pages, gang[i]); |
| 168 | page = find_lock_page(btree_inode->i_mapping, |
| 169 | gang[i]); |
| 170 | if (!page) |
| 171 | continue; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 172 | if (PageWriteback(page)) { |
| 173 | if (PageDirty(page)) |
| 174 | wait_on_page_writeback(page); |
| 175 | else { |
| 176 | unlock_page(page); |
| 177 | page_cache_release(page); |
| 178 | continue; |
| 179 | } |
| 180 | } |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 181 | err = write_one_page(page, 0); |
| 182 | if (err) |
| 183 | werr = err; |
| 184 | page_cache_release(page); |
| 185 | } |
| 186 | } |
| 187 | err = filemap_fdatawait(btree_inode->i_mapping); |
| 188 | if (err) |
| 189 | werr = err; |
| 190 | return werr; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, |
| 194 | struct btrfs_root *root) |
| 195 | { |
| 196 | int ret; |
| 197 | u64 old_extent_block; |
| 198 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 199 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 200 | struct btrfs_root *extent_root = fs_info->extent_root; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 201 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 202 | btrfs_write_dirty_block_groups(trans, extent_root); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 203 | while(1) { |
| 204 | old_extent_block = btrfs_root_blocknr(&extent_root->root_item); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 205 | if (old_extent_block == bh_blocknr(extent_root->node)) |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 206 | break; |
| 207 | btrfs_set_root_blocknr(&extent_root->root_item, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 208 | bh_blocknr(extent_root->node)); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 209 | ret = btrfs_update_root(trans, tree_root, |
| 210 | &extent_root->root_key, |
| 211 | &extent_root->root_item); |
| 212 | BUG_ON(ret); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 213 | btrfs_write_dirty_block_groups(trans, extent_root); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 214 | } |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static int wait_for_commit(struct btrfs_root *root, |
| 219 | struct btrfs_transaction *commit) |
| 220 | { |
| 221 | DEFINE_WAIT(wait); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 222 | mutex_lock(&root->fs_info->trans_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 223 | while(!commit->commit_done) { |
| 224 | prepare_to_wait(&commit->commit_wait, &wait, |
| 225 | TASK_UNINTERRUPTIBLE); |
| 226 | if (commit->commit_done) |
| 227 | break; |
| 228 | mutex_unlock(&root->fs_info->trans_mutex); |
| 229 | schedule(); |
| 230 | mutex_lock(&root->fs_info->trans_mutex); |
| 231 | } |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 232 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 233 | finish_wait(&commit->commit_wait, &wait); |
| 234 | return 0; |
| 235 | } |
| 236 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 237 | struct dirty_root { |
| 238 | struct list_head list; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 239 | struct btrfs_root *root; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 240 | struct btrfs_root *latest_root; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 241 | }; |
| 242 | |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 243 | int btrfs_add_dead_root(struct btrfs_root *root, |
| 244 | struct btrfs_root *latest, |
| 245 | struct list_head *dead_list) |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 246 | { |
| 247 | struct dirty_root *dirty; |
| 248 | |
| 249 | dirty = kmalloc(sizeof(*dirty), GFP_NOFS); |
| 250 | if (!dirty) |
| 251 | return -ENOMEM; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 252 | dirty->root = root; |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 253 | dirty->latest_root = latest; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 254 | list_add(&dirty->list, dead_list); |
| 255 | return 0; |
| 256 | } |
| 257 | |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 258 | static int add_dirty_roots(struct btrfs_trans_handle *trans, |
| 259 | struct radix_tree_root *radix, |
| 260 | struct list_head *list) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 261 | { |
| 262 | struct dirty_root *dirty; |
| 263 | struct btrfs_root *gang[8]; |
| 264 | struct btrfs_root *root; |
| 265 | int i; |
| 266 | int ret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 267 | int err = 0; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 268 | u32 refs; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 269 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 270 | while(1) { |
| 271 | ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, |
| 272 | ARRAY_SIZE(gang), |
| 273 | BTRFS_ROOT_TRANS_TAG); |
| 274 | if (ret == 0) |
| 275 | break; |
| 276 | for (i = 0; i < ret; i++) { |
| 277 | root = gang[i]; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 278 | radix_tree_tag_clear(radix, |
| 279 | (unsigned long)root->root_key.objectid, |
| 280 | BTRFS_ROOT_TRANS_TAG); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 281 | if (root->commit_root == root->node) { |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 282 | WARN_ON(bh_blocknr(root->node) != |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 283 | btrfs_root_blocknr(&root->root_item)); |
| 284 | brelse(root->commit_root); |
| 285 | root->commit_root = NULL; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 286 | |
| 287 | /* make sure to update the root on disk |
| 288 | * so we get any updates to the block used |
| 289 | * counts |
| 290 | */ |
| 291 | err = btrfs_update_root(trans, |
| 292 | root->fs_info->tree_root, |
| 293 | &root->root_key, |
| 294 | &root->root_item); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 295 | continue; |
| 296 | } |
| 297 | dirty = kmalloc(sizeof(*dirty), GFP_NOFS); |
| 298 | BUG_ON(!dirty); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 299 | dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS); |
| 300 | BUG_ON(!dirty->root); |
| 301 | |
| 302 | memset(&root->root_item.drop_progress, 0, |
| 303 | sizeof(struct btrfs_disk_key)); |
| 304 | root->root_item.drop_level = 0; |
| 305 | |
| 306 | memcpy(dirty->root, root, sizeof(*root)); |
| 307 | dirty->root->node = root->commit_root; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 308 | dirty->latest_root = root; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 309 | root->commit_root = NULL; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 310 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 311 | root->root_key.offset = root->fs_info->generation; |
| 312 | btrfs_set_root_blocknr(&root->root_item, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 313 | bh_blocknr(root->node)); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 314 | err = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 315 | &root->root_key, |
| 316 | &root->root_item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 317 | if (err) |
| 318 | break; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 319 | |
| 320 | refs = btrfs_root_refs(&dirty->root->root_item); |
| 321 | btrfs_set_root_refs(&dirty->root->root_item, refs - 1); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 322 | err = btrfs_update_root(trans, root->fs_info->tree_root, |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 323 | &dirty->root->root_key, |
| 324 | &dirty->root->root_item); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 325 | |
| 326 | BUG_ON(err); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 327 | if (refs == 1) { |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 328 | list_add(&dirty->list, list); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 329 | } else { |
| 330 | WARN_ON(1); |
| 331 | kfree(dirty->root); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 332 | kfree(dirty); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 333 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 334 | } |
| 335 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 336 | return err; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 337 | } |
| 338 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 339 | int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) |
| 340 | { |
| 341 | struct btrfs_fs_info *info = root->fs_info; |
| 342 | int ret; |
| 343 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame^] | 344 | unsigned long nr; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 345 | |
| 346 | if (root->defrag_running) |
| 347 | return 0; |
| 348 | |
| 349 | trans = btrfs_start_transaction(root, 1); |
| 350 | while (1) { |
| 351 | root->defrag_running = 1; |
| 352 | ret = btrfs_defrag_leaves(trans, root, cacheonly); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame^] | 353 | nr = trans->blocks_used; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 354 | btrfs_end_transaction(trans, root); |
| 355 | mutex_unlock(&info->fs_mutex); |
| 356 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame^] | 357 | btrfs_btree_balance_dirty(info->tree_root, nr); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 358 | cond_resched(); |
| 359 | |
| 360 | mutex_lock(&info->fs_mutex); |
| 361 | trans = btrfs_start_transaction(root, 1); |
| 362 | if (ret != -EAGAIN) |
| 363 | break; |
| 364 | } |
| 365 | root->defrag_running = 0; |
| 366 | radix_tree_tag_clear(&info->fs_roots_radix, |
| 367 | (unsigned long)root->root_key.objectid, |
| 368 | BTRFS_ROOT_DEFRAG_TAG); |
| 369 | btrfs_end_transaction(trans, root); |
| 370 | return 0; |
| 371 | } |
| 372 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 373 | int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info) |
| 374 | { |
| 375 | struct btrfs_root *gang[1]; |
| 376 | struct btrfs_root *root; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 377 | int i; |
| 378 | int ret; |
| 379 | int err = 0; |
| 380 | u64 last = 0; |
| 381 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 382 | while(1) { |
| 383 | ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix, |
| 384 | (void **)gang, last, |
| 385 | ARRAY_SIZE(gang), |
| 386 | BTRFS_ROOT_DEFRAG_TAG); |
| 387 | if (ret == 0) |
| 388 | break; |
| 389 | for (i = 0; i < ret; i++) { |
| 390 | root = gang[i]; |
| 391 | last = root->root_key.objectid + 1; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 392 | btrfs_defrag_root(root, 1); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 393 | } |
| 394 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 395 | btrfs_defrag_root(info->extent_root, 1); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 396 | return err; |
| 397 | } |
| 398 | |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 399 | static int drop_dirty_roots(struct btrfs_root *tree_root, |
| 400 | struct list_head *list) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 401 | { |
| 402 | struct dirty_root *dirty; |
| 403 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame^] | 404 | unsigned long nr; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 405 | u64 num_blocks; |
| 406 | u64 blocks_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 407 | int ret = 0; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 408 | int err; |
| 409 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 410 | while(!list_empty(list)) { |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 411 | struct btrfs_root *root; |
| 412 | |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 413 | mutex_lock(&tree_root->fs_info->fs_mutex); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 414 | dirty = list_entry(list->next, struct dirty_root, list); |
| 415 | list_del_init(&dirty->list); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 416 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 417 | num_blocks = btrfs_root_blocks_used(&dirty->root->root_item); |
| 418 | root = dirty->latest_root; |
| 419 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 420 | while(1) { |
| 421 | trans = btrfs_start_transaction(tree_root, 1); |
| 422 | ret = btrfs_drop_snapshot(trans, dirty->root); |
| 423 | if (ret != -EAGAIN) { |
| 424 | break; |
| 425 | } |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 426 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 427 | err = btrfs_update_root(trans, |
| 428 | tree_root, |
| 429 | &dirty->root->root_key, |
| 430 | &dirty->root->root_item); |
| 431 | if (err) |
| 432 | ret = err; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame^] | 433 | nr = trans->blocks_used; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 434 | ret = btrfs_end_transaction(trans, tree_root); |
| 435 | BUG_ON(ret); |
Chris Mason | f4468e9 | 2007-08-08 10:08:58 -0400 | [diff] [blame] | 436 | mutex_unlock(&tree_root->fs_info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame^] | 437 | btrfs_btree_balance_dirty(tree_root, nr); |
Chris Mason | f4468e9 | 2007-08-08 10:08:58 -0400 | [diff] [blame] | 438 | schedule(); |
| 439 | |
| 440 | mutex_lock(&tree_root->fs_info->fs_mutex); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 441 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 442 | BUG_ON(ret); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 443 | |
| 444 | num_blocks -= btrfs_root_blocks_used(&dirty->root->root_item); |
| 445 | blocks_used = btrfs_root_blocks_used(&root->root_item); |
| 446 | if (num_blocks) { |
| 447 | record_root_in_trans(root); |
| 448 | btrfs_set_root_blocks_used(&root->root_item, |
| 449 | blocks_used - num_blocks); |
| 450 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 451 | ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 452 | if (ret) { |
| 453 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 454 | break; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 455 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame^] | 456 | nr = trans->blocks_used; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 457 | ret = btrfs_end_transaction(trans, tree_root); |
| 458 | BUG_ON(ret); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 459 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 460 | kfree(dirty->root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 461 | kfree(dirty); |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 462 | mutex_unlock(&tree_root->fs_info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame^] | 463 | |
| 464 | btrfs_btree_balance_dirty(tree_root, nr); |
Chris Mason | f4468e9 | 2007-08-08 10:08:58 -0400 | [diff] [blame] | 465 | schedule(); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 466 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 467 | return ret; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 468 | } |
| 469 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 470 | int btrfs_commit_transaction(struct btrfs_trans_handle *trans, |
| 471 | struct btrfs_root *root) |
| 472 | { |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 473 | unsigned long joined = 0; |
| 474 | unsigned long timeout = 1; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 475 | struct btrfs_transaction *cur_trans; |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 476 | struct btrfs_transaction *prev_trans = NULL; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 477 | struct list_head dirty_fs_roots; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 478 | struct radix_tree_root pinned_copy; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 479 | DEFINE_WAIT(wait); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 480 | int ret; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 481 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 482 | init_bit_radix(&pinned_copy); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 483 | INIT_LIST_HEAD(&dirty_fs_roots); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 484 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 485 | mutex_lock(&root->fs_info->trans_mutex); |
| 486 | if (trans->transaction->in_commit) { |
| 487 | cur_trans = trans->transaction; |
| 488 | trans->transaction->use_count++; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 489 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 490 | btrfs_end_transaction(trans, root); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 491 | |
| 492 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 493 | ret = wait_for_commit(root, cur_trans); |
| 494 | BUG_ON(ret); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 495 | |
| 496 | mutex_lock(&root->fs_info->trans_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 497 | put_transaction(cur_trans); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 498 | mutex_unlock(&root->fs_info->trans_mutex); |
| 499 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 500 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 501 | return 0; |
| 502 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 503 | trans->transaction->in_commit = 1; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 504 | cur_trans = trans->transaction; |
| 505 | if (cur_trans->list.prev != &root->fs_info->trans_list) { |
| 506 | prev_trans = list_entry(cur_trans->list.prev, |
| 507 | struct btrfs_transaction, list); |
| 508 | if (!prev_trans->commit_done) { |
| 509 | prev_trans->use_count++; |
| 510 | mutex_unlock(&root->fs_info->fs_mutex); |
| 511 | mutex_unlock(&root->fs_info->trans_mutex); |
| 512 | |
| 513 | wait_for_commit(root, prev_trans); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 514 | |
| 515 | mutex_lock(&root->fs_info->fs_mutex); |
| 516 | mutex_lock(&root->fs_info->trans_mutex); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 517 | put_transaction(prev_trans); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 518 | } |
| 519 | } |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 520 | |
| 521 | do { |
| 522 | joined = cur_trans->num_joined; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 523 | WARN_ON(cur_trans != trans->transaction); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 524 | prepare_to_wait(&cur_trans->writer_wait, &wait, |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 525 | TASK_UNINTERRUPTIBLE); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 526 | |
| 527 | if (cur_trans->num_writers > 1) |
| 528 | timeout = MAX_SCHEDULE_TIMEOUT; |
| 529 | else |
| 530 | timeout = 1; |
| 531 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 532 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 533 | mutex_unlock(&root->fs_info->trans_mutex); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 534 | |
| 535 | schedule_timeout(timeout); |
| 536 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 537 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 538 | mutex_lock(&root->fs_info->trans_mutex); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 539 | finish_wait(&cur_trans->writer_wait, &wait); |
| 540 | } while (cur_trans->num_writers > 1 || |
| 541 | (cur_trans->num_joined != joined)); |
| 542 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 543 | WARN_ON(cur_trans != trans->transaction); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 544 | ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, |
| 545 | &dirty_fs_roots); |
| 546 | BUG_ON(ret); |
| 547 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 548 | ret = btrfs_commit_tree_roots(trans, root); |
| 549 | BUG_ON(ret); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 550 | |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 551 | cur_trans = root->fs_info->running_transaction; |
| 552 | root->fs_info->running_transaction = NULL; |
Chris Mason | 4b52dff | 2007-06-26 10:06:50 -0400 | [diff] [blame] | 553 | btrfs_set_super_generation(&root->fs_info->super_copy, |
| 554 | cur_trans->transid); |
| 555 | btrfs_set_super_root(&root->fs_info->super_copy, |
| 556 | bh_blocknr(root->fs_info->tree_root->node)); |
| 557 | memcpy(root->fs_info->disk_super, &root->fs_info->super_copy, |
| 558 | sizeof(root->fs_info->super_copy)); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 559 | |
| 560 | btrfs_copy_pinned(root, &pinned_copy); |
| 561 | |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 562 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 563 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 564 | ret = btrfs_write_and_wait_transaction(trans, root); |
| 565 | BUG_ON(ret); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 566 | write_ctree_super(trans, root); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 567 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 568 | btrfs_finish_extent_commit(trans, root, &pinned_copy); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 569 | mutex_lock(&root->fs_info->trans_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 570 | cur_trans->commit_done = 1; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 571 | root->fs_info->last_trans_committed = cur_trans->transid; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 572 | wake_up(&cur_trans->commit_wait); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 573 | put_transaction(cur_trans); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 574 | put_transaction(cur_trans); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 575 | |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 576 | if (root->fs_info->closing) |
| 577 | list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); |
| 578 | else |
| 579 | list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 580 | |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 581 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 582 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 583 | |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 584 | if (root->fs_info->closing) { |
| 585 | mutex_unlock(&root->fs_info->fs_mutex); |
| 586 | drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); |
| 587 | mutex_lock(&root->fs_info->fs_mutex); |
| 588 | } |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 589 | return ret; |
| 590 | } |
| 591 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 592 | int btrfs_clean_old_snapshots(struct btrfs_root *root) |
| 593 | { |
| 594 | struct list_head dirty_roots; |
| 595 | INIT_LIST_HEAD(&dirty_roots); |
| 596 | |
| 597 | mutex_lock(&root->fs_info->trans_mutex); |
| 598 | list_splice_init(&root->fs_info->dead_roots, &dirty_roots); |
| 599 | mutex_unlock(&root->fs_info->trans_mutex); |
| 600 | |
| 601 | if (!list_empty(&dirty_roots)) { |
| 602 | drop_dirty_roots(root, &dirty_roots); |
| 603 | } |
| 604 | return 0; |
| 605 | } |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 606 | void btrfs_transaction_cleaner(struct work_struct *work) |
| 607 | { |
| 608 | struct btrfs_fs_info *fs_info = container_of(work, |
| 609 | struct btrfs_fs_info, |
| 610 | trans_work.work); |
| 611 | |
| 612 | struct btrfs_root *root = fs_info->tree_root; |
| 613 | struct btrfs_transaction *cur; |
| 614 | struct btrfs_trans_handle *trans; |
| 615 | unsigned long now; |
| 616 | unsigned long delay = HZ * 30; |
| 617 | int ret; |
| 618 | |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 619 | mutex_lock(&root->fs_info->fs_mutex); |
| 620 | mutex_lock(&root->fs_info->trans_mutex); |
| 621 | cur = root->fs_info->running_transaction; |
| 622 | if (!cur) { |
| 623 | mutex_unlock(&root->fs_info->trans_mutex); |
| 624 | goto out; |
| 625 | } |
| 626 | now = get_seconds(); |
| 627 | if (now < cur->start_time || now - cur->start_time < 30) { |
| 628 | mutex_unlock(&root->fs_info->trans_mutex); |
| 629 | delay = HZ * 5; |
| 630 | goto out; |
| 631 | } |
| 632 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 633 | btrfs_defrag_dirty_roots(root->fs_info); |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 634 | trans = btrfs_start_transaction(root, 1); |
| 635 | ret = btrfs_commit_transaction(trans, root); |
| 636 | out: |
| 637 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 638 | btrfs_clean_old_snapshots(root); |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 639 | btrfs_transaction_queue_work(root, delay); |
| 640 | } |
| 641 | |
| 642 | void btrfs_transaction_queue_work(struct btrfs_root *root, int delay) |
| 643 | { |
| 644 | queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay); |
| 645 | } |
| 646 | |
| 647 | void btrfs_transaction_flush_work(struct btrfs_root *root) |
| 648 | { |
| 649 | cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work); |
| 650 | flush_workqueue(trans_wq); |
| 651 | } |
| 652 | |
| 653 | void __init btrfs_init_transaction_sys(void) |
| 654 | { |
| 655 | trans_wq = create_workqueue("btrfs"); |
| 656 | } |
| 657 | |
| 658 | void __exit btrfs_exit_transaction_sys(void) |
| 659 | { |
| 660 | destroy_workqueue(trans_wq); |
| 661 | } |
| 662 | |