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