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