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/module.h> |
| 20 | #include <linux/fs.h> |
Chris Mason | 3408878 | 2007-06-12 11:36:58 -0400 | [diff] [blame^] | 21 | #include <linux/sched.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 |
| 33 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 34 | static void put_transaction(struct btrfs_transaction *transaction) |
| 35 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 36 | WARN_ON(transaction->use_count == 0); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 37 | transaction->use_count--; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 38 | if (transaction->use_count == 0) { |
| 39 | WARN_ON(total_trans == 0); |
| 40 | total_trans--; |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 41 | list_del_init(&transaction->list); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 42 | memset(transaction, 0, sizeof(*transaction)); |
| 43 | kmem_cache_free(btrfs_transaction_cachep, transaction); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 44 | } |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static int join_transaction(struct btrfs_root *root) |
| 48 | { |
| 49 | struct btrfs_transaction *cur_trans; |
| 50 | cur_trans = root->fs_info->running_transaction; |
| 51 | if (!cur_trans) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 52 | cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, |
| 53 | GFP_NOFS); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 54 | total_trans++; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 55 | BUG_ON(!cur_trans); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 56 | root->fs_info->generation++; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 57 | root->fs_info->running_transaction = cur_trans; |
| 58 | cur_trans->num_writers = 0; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 59 | cur_trans->transid = root->fs_info->generation; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 60 | init_waitqueue_head(&cur_trans->writer_wait); |
| 61 | init_waitqueue_head(&cur_trans->commit_wait); |
| 62 | cur_trans->in_commit = 0; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 63 | cur_trans->use_count = 1; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 64 | cur_trans->commit_done = 0; |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 65 | cur_trans->start_time = get_seconds(); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 66 | list_add_tail(&cur_trans->list, &root->fs_info->trans_list); |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 67 | init_bit_radix(&cur_trans->dirty_pages); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 68 | } |
| 69 | cur_trans->num_writers++; |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, |
| 74 | int num_blocks) |
| 75 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 76 | struct btrfs_trans_handle *h = |
| 77 | kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 78 | int ret; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 79 | u64 running_trans_id; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 80 | |
| 81 | mutex_lock(&root->fs_info->trans_mutex); |
| 82 | ret = join_transaction(root); |
| 83 | BUG_ON(ret); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 84 | running_trans_id = root->fs_info->running_transaction->transid; |
| 85 | |
| 86 | if (root != root->fs_info->tree_root && root->last_trans < |
| 87 | running_trans_id) { |
| 88 | radix_tree_tag_set(&root->fs_info->fs_roots_radix, |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 89 | (unsigned long)root->root_key.objectid, |
| 90 | BTRFS_ROOT_TRANS_TAG); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 91 | root->commit_root = root->node; |
| 92 | get_bh(root->node); |
| 93 | } |
| 94 | root->last_trans = running_trans_id; |
| 95 | h->transid = running_trans_id; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 96 | h->transaction = root->fs_info->running_transaction; |
| 97 | h->blocks_reserved = num_blocks; |
| 98 | h->blocks_used = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 99 | h->block_group = NULL; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 100 | root->fs_info->running_transaction->use_count++; |
| 101 | mutex_unlock(&root->fs_info->trans_mutex); |
| 102 | return h; |
| 103 | } |
| 104 | |
| 105 | int btrfs_end_transaction(struct btrfs_trans_handle *trans, |
| 106 | struct btrfs_root *root) |
| 107 | { |
| 108 | struct btrfs_transaction *cur_trans; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 109 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 110 | mutex_lock(&root->fs_info->trans_mutex); |
| 111 | cur_trans = root->fs_info->running_transaction; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 112 | WARN_ON(cur_trans->num_writers < 1); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 113 | if (waitqueue_active(&cur_trans->writer_wait)) |
| 114 | wake_up(&cur_trans->writer_wait); |
| 115 | cur_trans->num_writers--; |
| 116 | put_transaction(cur_trans); |
| 117 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 118 | memset(trans, 0, sizeof(*trans)); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 119 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, |
| 125 | struct btrfs_root *root) |
| 126 | { |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 127 | unsigned long gang[16]; |
| 128 | int ret; |
| 129 | int i; |
| 130 | int err; |
| 131 | int werr = 0; |
| 132 | struct page *page; |
| 133 | struct radix_tree_root *dirty_pages; |
| 134 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 135 | |
| 136 | if (!trans || !trans->transaction) { |
| 137 | return filemap_write_and_wait(btree_inode->i_mapping); |
| 138 | } |
| 139 | dirty_pages = &trans->transaction->dirty_pages; |
| 140 | while(1) { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 141 | ret = find_first_radix_bit(dirty_pages, gang, |
| 142 | 0, ARRAY_SIZE(gang)); |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 143 | if (!ret) |
| 144 | break; |
| 145 | for (i = 0; i < ret; i++) { |
| 146 | /* FIXME EIO */ |
| 147 | clear_radix_bit(dirty_pages, gang[i]); |
| 148 | page = find_lock_page(btree_inode->i_mapping, |
| 149 | gang[i]); |
| 150 | if (!page) |
| 151 | continue; |
| 152 | err = write_one_page(page, 0); |
| 153 | if (err) |
| 154 | werr = err; |
| 155 | page_cache_release(page); |
| 156 | } |
| 157 | } |
| 158 | err = filemap_fdatawait(btree_inode->i_mapping); |
| 159 | if (err) |
| 160 | werr = err; |
| 161 | return werr; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, |
| 165 | struct btrfs_root *root) |
| 166 | { |
| 167 | int ret; |
| 168 | u64 old_extent_block; |
| 169 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 170 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 171 | struct btrfs_root *extent_root = fs_info->extent_root; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 172 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 173 | btrfs_write_dirty_block_groups(trans, extent_root); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 174 | while(1) { |
| 175 | old_extent_block = btrfs_root_blocknr(&extent_root->root_item); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 176 | if (old_extent_block == bh_blocknr(extent_root->node)) |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 177 | break; |
| 178 | btrfs_set_root_blocknr(&extent_root->root_item, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 179 | bh_blocknr(extent_root->node)); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 180 | ret = btrfs_update_root(trans, tree_root, |
| 181 | &extent_root->root_key, |
| 182 | &extent_root->root_item); |
| 183 | BUG_ON(ret); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 184 | btrfs_write_dirty_block_groups(trans, extent_root); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static int wait_for_commit(struct btrfs_root *root, |
| 190 | struct btrfs_transaction *commit) |
| 191 | { |
| 192 | DEFINE_WAIT(wait); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 193 | while(!commit->commit_done) { |
| 194 | prepare_to_wait(&commit->commit_wait, &wait, |
| 195 | TASK_UNINTERRUPTIBLE); |
| 196 | if (commit->commit_done) |
| 197 | break; |
| 198 | mutex_unlock(&root->fs_info->trans_mutex); |
| 199 | schedule(); |
| 200 | mutex_lock(&root->fs_info->trans_mutex); |
| 201 | } |
| 202 | finish_wait(&commit->commit_wait, &wait); |
| 203 | return 0; |
| 204 | } |
| 205 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 206 | struct dirty_root { |
| 207 | struct list_head list; |
| 208 | struct btrfs_key snap_key; |
| 209 | struct buffer_head *commit_root; |
| 210 | struct btrfs_root *root; |
| 211 | }; |
| 212 | |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 213 | static int add_dirty_roots(struct btrfs_trans_handle *trans, |
| 214 | struct radix_tree_root *radix, |
| 215 | struct list_head *list) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 216 | { |
| 217 | struct dirty_root *dirty; |
| 218 | struct btrfs_root *gang[8]; |
| 219 | struct btrfs_root *root; |
| 220 | int i; |
| 221 | int ret; |
| 222 | int err; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 223 | while(1) { |
| 224 | ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, |
| 225 | ARRAY_SIZE(gang), |
| 226 | BTRFS_ROOT_TRANS_TAG); |
| 227 | if (ret == 0) |
| 228 | break; |
| 229 | for (i = 0; i < ret; i++) { |
| 230 | root = gang[i]; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 231 | radix_tree_tag_clear(radix, |
| 232 | (unsigned long)root->root_key.objectid, |
| 233 | BTRFS_ROOT_TRANS_TAG); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 234 | if (root->commit_root == root->node) { |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 235 | WARN_ON(bh_blocknr(root->node) != |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 236 | btrfs_root_blocknr(&root->root_item)); |
| 237 | brelse(root->commit_root); |
| 238 | root->commit_root = NULL; |
| 239 | continue; |
| 240 | } |
| 241 | dirty = kmalloc(sizeof(*dirty), GFP_NOFS); |
| 242 | BUG_ON(!dirty); |
| 243 | memcpy(&dirty->snap_key, &root->root_key, |
| 244 | sizeof(root->root_key)); |
| 245 | dirty->commit_root = root->commit_root; |
| 246 | root->commit_root = NULL; |
| 247 | dirty->root = root; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 248 | root->root_key.offset = root->fs_info->generation; |
| 249 | btrfs_set_root_blocknr(&root->root_item, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 250 | bh_blocknr(root->node)); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 251 | err = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 252 | &root->root_key, |
| 253 | &root->root_item); |
| 254 | BUG_ON(err); |
| 255 | list_add(&dirty->list, list); |
| 256 | } |
| 257 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 258 | return 0; |
| 259 | } |
| 260 | |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 261 | static int drop_dirty_roots(struct btrfs_root *tree_root, |
| 262 | struct list_head *list) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 263 | { |
| 264 | struct dirty_root *dirty; |
| 265 | struct btrfs_trans_handle *trans; |
| 266 | int ret; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 267 | while(!list_empty(list)) { |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 268 | mutex_lock(&tree_root->fs_info->fs_mutex); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 269 | dirty = list_entry(list->next, struct dirty_root, list); |
| 270 | list_del_init(&dirty->list); |
| 271 | trans = btrfs_start_transaction(tree_root, 1); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 272 | ret = btrfs_drop_snapshot(trans, dirty->root, |
| 273 | dirty->commit_root); |
| 274 | BUG_ON(ret); |
| 275 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 276 | ret = btrfs_del_root(trans, tree_root, &dirty->snap_key); |
| 277 | BUG_ON(ret); |
| 278 | ret = btrfs_end_transaction(trans, tree_root); |
| 279 | BUG_ON(ret); |
| 280 | kfree(dirty); |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 281 | mutex_unlock(&tree_root->fs_info->fs_mutex); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 282 | } |
| 283 | return 0; |
| 284 | } |
| 285 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 286 | int btrfs_commit_transaction(struct btrfs_trans_handle *trans, |
| 287 | struct btrfs_root *root) |
| 288 | { |
| 289 | int ret = 0; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 290 | struct btrfs_transaction *cur_trans; |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 291 | struct btrfs_transaction *prev_trans = NULL; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 292 | struct list_head dirty_fs_roots; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 293 | DEFINE_WAIT(wait); |
| 294 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 295 | INIT_LIST_HEAD(&dirty_fs_roots); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 296 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 297 | mutex_lock(&root->fs_info->trans_mutex); |
| 298 | if (trans->transaction->in_commit) { |
| 299 | cur_trans = trans->transaction; |
| 300 | trans->transaction->use_count++; |
| 301 | btrfs_end_transaction(trans, root); |
| 302 | ret = wait_for_commit(root, cur_trans); |
| 303 | BUG_ON(ret); |
| 304 | put_transaction(cur_trans); |
| 305 | mutex_unlock(&root->fs_info->trans_mutex); |
| 306 | return 0; |
| 307 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 308 | cur_trans = trans->transaction; |
| 309 | trans->transaction->in_commit = 1; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 310 | while (trans->transaction->num_writers > 1) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 311 | WARN_ON(cur_trans != trans->transaction); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 312 | prepare_to_wait(&trans->transaction->writer_wait, &wait, |
| 313 | TASK_UNINTERRUPTIBLE); |
| 314 | if (trans->transaction->num_writers <= 1) |
| 315 | break; |
| 316 | mutex_unlock(&root->fs_info->trans_mutex); |
| 317 | schedule(); |
| 318 | mutex_lock(&root->fs_info->trans_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 319 | finish_wait(&trans->transaction->writer_wait, &wait); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 320 | } |
| 321 | finish_wait(&trans->transaction->writer_wait, &wait); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 322 | WARN_ON(cur_trans != trans->transaction); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 323 | add_dirty_roots(trans, &root->fs_info->fs_roots_radix, &dirty_fs_roots); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 324 | ret = btrfs_commit_tree_roots(trans, root); |
| 325 | BUG_ON(ret); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 326 | cur_trans = root->fs_info->running_transaction; |
| 327 | root->fs_info->running_transaction = NULL; |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 328 | if (cur_trans->list.prev != &root->fs_info->trans_list) { |
| 329 | prev_trans = list_entry(cur_trans->list.prev, |
| 330 | struct btrfs_transaction, list); |
| 331 | if (prev_trans->commit_done) |
| 332 | prev_trans = NULL; |
| 333 | else |
| 334 | prev_trans->use_count++; |
| 335 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 336 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 337 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 338 | ret = btrfs_write_and_wait_transaction(trans, root); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 339 | if (prev_trans) { |
| 340 | mutex_lock(&root->fs_info->trans_mutex); |
| 341 | wait_for_commit(root, prev_trans); |
| 342 | put_transaction(prev_trans); |
| 343 | mutex_unlock(&root->fs_info->trans_mutex); |
| 344 | } |
| 345 | btrfs_set_super_generation(root->fs_info->disk_super, |
| 346 | cur_trans->transid); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 347 | BUG_ON(ret); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 348 | write_ctree_super(trans, root); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 349 | |
| 350 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 351 | btrfs_finish_extent_commit(trans, root); |
| 352 | mutex_lock(&root->fs_info->trans_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 353 | cur_trans->commit_done = 1; |
| 354 | wake_up(&cur_trans->commit_wait); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 355 | put_transaction(cur_trans); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 356 | put_transaction(cur_trans); |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 357 | if (root->fs_info->closing) |
| 358 | list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); |
| 359 | else |
| 360 | list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 361 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 362 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 363 | |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 364 | if (root->fs_info->closing) { |
| 365 | mutex_unlock(&root->fs_info->fs_mutex); |
| 366 | drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); |
| 367 | mutex_lock(&root->fs_info->fs_mutex); |
| 368 | } |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 369 | return ret; |
| 370 | } |
| 371 | |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 372 | void btrfs_transaction_cleaner(struct work_struct *work) |
| 373 | { |
| 374 | struct btrfs_fs_info *fs_info = container_of(work, |
| 375 | struct btrfs_fs_info, |
| 376 | trans_work.work); |
| 377 | |
| 378 | struct btrfs_root *root = fs_info->tree_root; |
| 379 | struct btrfs_transaction *cur; |
| 380 | struct btrfs_trans_handle *trans; |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 381 | struct list_head dirty_roots; |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 382 | unsigned long now; |
| 383 | unsigned long delay = HZ * 30; |
| 384 | int ret; |
| 385 | |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 386 | INIT_LIST_HEAD(&dirty_roots); |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 387 | mutex_lock(&root->fs_info->fs_mutex); |
| 388 | mutex_lock(&root->fs_info->trans_mutex); |
| 389 | cur = root->fs_info->running_transaction; |
| 390 | if (!cur) { |
| 391 | mutex_unlock(&root->fs_info->trans_mutex); |
| 392 | goto out; |
| 393 | } |
| 394 | now = get_seconds(); |
| 395 | if (now < cur->start_time || now - cur->start_time < 30) { |
| 396 | mutex_unlock(&root->fs_info->trans_mutex); |
| 397 | delay = HZ * 5; |
| 398 | goto out; |
| 399 | } |
| 400 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 401 | trans = btrfs_start_transaction(root, 1); |
| 402 | ret = btrfs_commit_transaction(trans, root); |
| 403 | out: |
| 404 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | ad693af | 2007-06-09 08:19:57 -0400 | [diff] [blame] | 405 | |
| 406 | mutex_lock(&root->fs_info->trans_mutex); |
| 407 | list_splice_init(&root->fs_info->dead_roots, &dirty_roots); |
| 408 | mutex_unlock(&root->fs_info->trans_mutex); |
| 409 | |
| 410 | if (!list_empty(&dirty_roots)) { |
| 411 | drop_dirty_roots(root, &dirty_roots); |
| 412 | } |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 413 | btrfs_transaction_queue_work(root, delay); |
| 414 | } |
| 415 | |
| 416 | void btrfs_transaction_queue_work(struct btrfs_root *root, int delay) |
| 417 | { |
| 418 | queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay); |
| 419 | } |
| 420 | |
| 421 | void btrfs_transaction_flush_work(struct btrfs_root *root) |
| 422 | { |
| 423 | cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work); |
| 424 | flush_workqueue(trans_wq); |
| 425 | } |
| 426 | |
| 427 | void __init btrfs_init_transaction_sys(void) |
| 428 | { |
| 429 | trans_wq = create_workqueue("btrfs"); |
| 430 | } |
| 431 | |
| 432 | void __exit btrfs_exit_transaction_sys(void) |
| 433 | { |
| 434 | destroy_workqueue(trans_wq); |
| 435 | } |
| 436 | |