Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 1 | #include <linux/module.h> |
| 2 | #include <linux/fs.h> |
| 3 | #include "ctree.h" |
| 4 | #include "disk-io.h" |
| 5 | #include "transaction.h" |
| 6 | |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 7 | static int total_trans = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 8 | extern struct kmem_cache *btrfs_trans_handle_cachep; |
| 9 | extern struct kmem_cache *btrfs_transaction_cachep; |
| 10 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 11 | #define BTRFS_ROOT_TRANS_TAG 0 |
| 12 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 13 | #define TRANS_MAGIC 0xE1E10E |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 14 | static void put_transaction(struct btrfs_transaction *transaction) |
| 15 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 16 | WARN_ON(transaction->use_count == 0); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 17 | transaction->use_count--; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 18 | WARN_ON(transaction->magic != TRANS_MAGIC); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 19 | if (transaction->use_count == 0) { |
| 20 | WARN_ON(total_trans == 0); |
| 21 | total_trans--; |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 22 | list_del_init(&transaction->list); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 23 | memset(transaction, 0, sizeof(*transaction)); |
| 24 | kmem_cache_free(btrfs_transaction_cachep, transaction); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 25 | } |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | static int join_transaction(struct btrfs_root *root) |
| 29 | { |
| 30 | struct btrfs_transaction *cur_trans; |
| 31 | cur_trans = root->fs_info->running_transaction; |
| 32 | if (!cur_trans) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 33 | cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, |
| 34 | GFP_NOFS); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 35 | total_trans++; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 36 | BUG_ON(!cur_trans); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 37 | root->fs_info->generation++; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 38 | root->fs_info->running_transaction = cur_trans; |
| 39 | cur_trans->num_writers = 0; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 40 | cur_trans->transid = root->fs_info->generation; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 41 | init_waitqueue_head(&cur_trans->writer_wait); |
| 42 | init_waitqueue_head(&cur_trans->commit_wait); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 43 | cur_trans->magic = TRANS_MAGIC; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 44 | cur_trans->in_commit = 0; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 45 | cur_trans->use_count = 1; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 46 | cur_trans->commit_done = 0; |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 47 | list_add_tail(&cur_trans->list, &root->fs_info->trans_list); |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 48 | init_bit_radix(&cur_trans->dirty_pages); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 49 | } |
| 50 | cur_trans->num_writers++; |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, |
| 55 | int num_blocks) |
| 56 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 57 | struct btrfs_trans_handle *h = |
| 58 | kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 59 | int ret; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 60 | u64 running_trans_id; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 61 | |
| 62 | mutex_lock(&root->fs_info->trans_mutex); |
| 63 | ret = join_transaction(root); |
| 64 | BUG_ON(ret); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 65 | running_trans_id = root->fs_info->running_transaction->transid; |
| 66 | |
| 67 | if (root != root->fs_info->tree_root && root->last_trans < |
| 68 | running_trans_id) { |
| 69 | radix_tree_tag_set(&root->fs_info->fs_roots_radix, |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 70 | (unsigned long)root->root_key.objectid, |
| 71 | BTRFS_ROOT_TRANS_TAG); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 72 | root->commit_root = root->node; |
| 73 | get_bh(root->node); |
| 74 | } |
| 75 | root->last_trans = running_trans_id; |
| 76 | h->transid = running_trans_id; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 77 | h->transaction = root->fs_info->running_transaction; |
| 78 | h->blocks_reserved = num_blocks; |
| 79 | h->blocks_used = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 80 | h->block_group = NULL; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 81 | root->fs_info->running_transaction->use_count++; |
| 82 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 83 | h->magic = h->magic2 = TRANS_MAGIC; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 84 | return h; |
| 85 | } |
| 86 | |
| 87 | int btrfs_end_transaction(struct btrfs_trans_handle *trans, |
| 88 | struct btrfs_root *root) |
| 89 | { |
| 90 | struct btrfs_transaction *cur_trans; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 91 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 92 | WARN_ON(trans->magic != TRANS_MAGIC); |
| 93 | WARN_ON(trans->magic2 != TRANS_MAGIC); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 94 | mutex_lock(&root->fs_info->trans_mutex); |
| 95 | cur_trans = root->fs_info->running_transaction; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 96 | WARN_ON(cur_trans->num_writers < 1); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 97 | if (waitqueue_active(&cur_trans->writer_wait)) |
| 98 | wake_up(&cur_trans->writer_wait); |
| 99 | cur_trans->num_writers--; |
| 100 | put_transaction(cur_trans); |
| 101 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 102 | memset(trans, 0, sizeof(*trans)); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 103 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, |
| 109 | struct btrfs_root *root) |
| 110 | { |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 111 | unsigned long gang[16]; |
| 112 | int ret; |
| 113 | int i; |
| 114 | int err; |
| 115 | int werr = 0; |
| 116 | struct page *page; |
| 117 | struct radix_tree_root *dirty_pages; |
| 118 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 119 | |
| 120 | if (!trans || !trans->transaction) { |
| 121 | return filemap_write_and_wait(btree_inode->i_mapping); |
| 122 | } |
| 123 | dirty_pages = &trans->transaction->dirty_pages; |
| 124 | while(1) { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame^] | 125 | ret = find_first_radix_bit(dirty_pages, gang, |
| 126 | 0, ARRAY_SIZE(gang)); |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 127 | if (!ret) |
| 128 | break; |
| 129 | for (i = 0; i < ret; i++) { |
| 130 | /* FIXME EIO */ |
| 131 | clear_radix_bit(dirty_pages, gang[i]); |
| 132 | page = find_lock_page(btree_inode->i_mapping, |
| 133 | gang[i]); |
| 134 | if (!page) |
| 135 | continue; |
| 136 | err = write_one_page(page, 0); |
| 137 | if (err) |
| 138 | werr = err; |
| 139 | page_cache_release(page); |
| 140 | } |
| 141 | } |
| 142 | err = filemap_fdatawait(btree_inode->i_mapping); |
| 143 | if (err) |
| 144 | werr = err; |
| 145 | return werr; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, |
| 149 | struct btrfs_root *root) |
| 150 | { |
| 151 | int ret; |
| 152 | u64 old_extent_block; |
| 153 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 154 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 155 | struct btrfs_root *extent_root = fs_info->extent_root; |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 156 | struct btrfs_root *dev_root = fs_info->dev_root; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 157 | |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 158 | if (btrfs_super_device_root(fs_info->disk_super) != |
| 159 | bh_blocknr(dev_root->node)) { |
| 160 | btrfs_set_super_device_root(fs_info->disk_super, |
| 161 | bh_blocknr(dev_root->node)); |
| 162 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 163 | btrfs_write_dirty_block_groups(trans, extent_root); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 164 | while(1) { |
| 165 | old_extent_block = btrfs_root_blocknr(&extent_root->root_item); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 166 | if (old_extent_block == bh_blocknr(extent_root->node)) |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 167 | break; |
| 168 | btrfs_set_root_blocknr(&extent_root->root_item, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 169 | bh_blocknr(extent_root->node)); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 170 | ret = btrfs_update_root(trans, tree_root, |
| 171 | &extent_root->root_key, |
| 172 | &extent_root->root_item); |
| 173 | BUG_ON(ret); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 174 | btrfs_write_dirty_block_groups(trans, extent_root); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 175 | } |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static int wait_for_commit(struct btrfs_root *root, |
| 180 | struct btrfs_transaction *commit) |
| 181 | { |
| 182 | DEFINE_WAIT(wait); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 183 | while(!commit->commit_done) { |
| 184 | prepare_to_wait(&commit->commit_wait, &wait, |
| 185 | TASK_UNINTERRUPTIBLE); |
| 186 | if (commit->commit_done) |
| 187 | break; |
| 188 | mutex_unlock(&root->fs_info->trans_mutex); |
| 189 | schedule(); |
| 190 | mutex_lock(&root->fs_info->trans_mutex); |
| 191 | } |
| 192 | finish_wait(&commit->commit_wait, &wait); |
| 193 | return 0; |
| 194 | } |
| 195 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 196 | struct dirty_root { |
| 197 | struct list_head list; |
| 198 | struct btrfs_key snap_key; |
| 199 | struct buffer_head *commit_root; |
| 200 | struct btrfs_root *root; |
| 201 | }; |
| 202 | |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 203 | static int add_dirty_roots(struct btrfs_trans_handle *trans, |
| 204 | struct radix_tree_root *radix, |
| 205 | struct list_head *list) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 206 | { |
| 207 | struct dirty_root *dirty; |
| 208 | struct btrfs_root *gang[8]; |
| 209 | struct btrfs_root *root; |
| 210 | int i; |
| 211 | int ret; |
| 212 | int err; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 213 | while(1) { |
| 214 | ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, |
| 215 | ARRAY_SIZE(gang), |
| 216 | BTRFS_ROOT_TRANS_TAG); |
| 217 | if (ret == 0) |
| 218 | break; |
| 219 | for (i = 0; i < ret; i++) { |
| 220 | root = gang[i]; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 221 | radix_tree_tag_clear(radix, |
| 222 | (unsigned long)root->root_key.objectid, |
| 223 | BTRFS_ROOT_TRANS_TAG); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 224 | if (root->commit_root == root->node) { |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 225 | WARN_ON(bh_blocknr(root->node) != |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 226 | btrfs_root_blocknr(&root->root_item)); |
| 227 | brelse(root->commit_root); |
| 228 | root->commit_root = NULL; |
| 229 | continue; |
| 230 | } |
| 231 | dirty = kmalloc(sizeof(*dirty), GFP_NOFS); |
| 232 | BUG_ON(!dirty); |
| 233 | memcpy(&dirty->snap_key, &root->root_key, |
| 234 | sizeof(root->root_key)); |
| 235 | dirty->commit_root = root->commit_root; |
| 236 | root->commit_root = NULL; |
| 237 | dirty->root = root; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 238 | root->root_key.offset = root->fs_info->generation; |
| 239 | btrfs_set_root_blocknr(&root->root_item, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 240 | bh_blocknr(root->node)); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 241 | err = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 242 | &root->root_key, |
| 243 | &root->root_item); |
| 244 | BUG_ON(err); |
| 245 | list_add(&dirty->list, list); |
| 246 | } |
| 247 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 248 | return 0; |
| 249 | } |
| 250 | |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 251 | static int drop_dirty_roots(struct btrfs_root *tree_root, |
| 252 | struct list_head *list) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 253 | { |
| 254 | struct dirty_root *dirty; |
| 255 | struct btrfs_trans_handle *trans; |
| 256 | int ret; |
| 257 | |
| 258 | while(!list_empty(list)) { |
| 259 | dirty = list_entry(list->next, struct dirty_root, list); |
| 260 | list_del_init(&dirty->list); |
| 261 | trans = btrfs_start_transaction(tree_root, 1); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 262 | ret = btrfs_drop_snapshot(trans, dirty->root, |
| 263 | dirty->commit_root); |
| 264 | BUG_ON(ret); |
| 265 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 266 | ret = btrfs_del_root(trans, tree_root, &dirty->snap_key); |
| 267 | BUG_ON(ret); |
| 268 | ret = btrfs_end_transaction(trans, tree_root); |
| 269 | BUG_ON(ret); |
| 270 | kfree(dirty); |
| 271 | } |
| 272 | return 0; |
| 273 | } |
| 274 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 275 | int btrfs_commit_transaction(struct btrfs_trans_handle *trans, |
| 276 | struct btrfs_root *root) |
| 277 | { |
| 278 | int ret = 0; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 279 | struct btrfs_transaction *cur_trans; |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 280 | struct btrfs_transaction *prev_trans = NULL; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 281 | struct list_head dirty_fs_roots; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 282 | DEFINE_WAIT(wait); |
| 283 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 284 | INIT_LIST_HEAD(&dirty_fs_roots); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 285 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 286 | mutex_lock(&root->fs_info->trans_mutex); |
| 287 | if (trans->transaction->in_commit) { |
| 288 | cur_trans = trans->transaction; |
| 289 | trans->transaction->use_count++; |
| 290 | btrfs_end_transaction(trans, root); |
| 291 | ret = wait_for_commit(root, cur_trans); |
| 292 | BUG_ON(ret); |
| 293 | put_transaction(cur_trans); |
| 294 | mutex_unlock(&root->fs_info->trans_mutex); |
| 295 | return 0; |
| 296 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 297 | cur_trans = trans->transaction; |
| 298 | trans->transaction->in_commit = 1; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 299 | while (trans->transaction->num_writers > 1) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 300 | WARN_ON(cur_trans != trans->transaction); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 301 | prepare_to_wait(&trans->transaction->writer_wait, &wait, |
| 302 | TASK_UNINTERRUPTIBLE); |
| 303 | if (trans->transaction->num_writers <= 1) |
| 304 | break; |
| 305 | mutex_unlock(&root->fs_info->trans_mutex); |
| 306 | schedule(); |
| 307 | mutex_lock(&root->fs_info->trans_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 308 | finish_wait(&trans->transaction->writer_wait, &wait); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 309 | } |
| 310 | finish_wait(&trans->transaction->writer_wait, &wait); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 311 | WARN_ON(cur_trans != trans->transaction); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 312 | 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] | 313 | ret = btrfs_commit_tree_roots(trans, root); |
| 314 | BUG_ON(ret); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 315 | cur_trans = root->fs_info->running_transaction; |
| 316 | root->fs_info->running_transaction = NULL; |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 317 | if (cur_trans->list.prev != &root->fs_info->trans_list) { |
| 318 | prev_trans = list_entry(cur_trans->list.prev, |
| 319 | struct btrfs_transaction, list); |
| 320 | if (prev_trans->commit_done) |
| 321 | prev_trans = NULL; |
| 322 | else |
| 323 | prev_trans->use_count++; |
| 324 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 325 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 326 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 327 | ret = btrfs_write_and_wait_transaction(trans, root); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 328 | if (prev_trans) { |
| 329 | mutex_lock(&root->fs_info->trans_mutex); |
| 330 | wait_for_commit(root, prev_trans); |
| 331 | put_transaction(prev_trans); |
| 332 | mutex_unlock(&root->fs_info->trans_mutex); |
| 333 | } |
| 334 | btrfs_set_super_generation(root->fs_info->disk_super, |
| 335 | cur_trans->transid); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 336 | BUG_ON(ret); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 337 | write_ctree_super(trans, root); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 338 | |
| 339 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 340 | btrfs_finish_extent_commit(trans, root); |
| 341 | mutex_lock(&root->fs_info->trans_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 342 | cur_trans->commit_done = 1; |
| 343 | wake_up(&cur_trans->commit_wait); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 344 | put_transaction(cur_trans); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 345 | put_transaction(cur_trans); |
| 346 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 347 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 348 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 349 | drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 350 | return ret; |
| 351 | } |
| 352 | |