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