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 | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 22 | memset(transaction, 0, sizeof(*transaction)); |
| 23 | kmem_cache_free(btrfs_transaction_cachep, transaction); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 24 | } |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | static int join_transaction(struct btrfs_root *root) |
| 28 | { |
| 29 | struct btrfs_transaction *cur_trans; |
| 30 | cur_trans = root->fs_info->running_transaction; |
| 31 | if (!cur_trans) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 32 | cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, |
| 33 | GFP_NOFS); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 34 | total_trans++; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 35 | BUG_ON(!cur_trans); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame^] | 36 | root->fs_info->generation++; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 37 | root->fs_info->running_transaction = cur_trans; |
| 38 | cur_trans->num_writers = 0; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame^] | 39 | cur_trans->transid = root->fs_info->generation; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 40 | init_waitqueue_head(&cur_trans->writer_wait); |
| 41 | init_waitqueue_head(&cur_trans->commit_wait); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 42 | cur_trans->magic = TRANS_MAGIC; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 43 | cur_trans->in_commit = 0; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 44 | cur_trans->use_count = 1; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 45 | cur_trans->commit_done = 0; |
| 46 | } |
| 47 | cur_trans->num_writers++; |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, |
| 52 | int num_blocks) |
| 53 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 54 | struct btrfs_trans_handle *h = |
| 55 | kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 56 | int ret; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame^] | 57 | u64 running_trans_id; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 58 | |
| 59 | mutex_lock(&root->fs_info->trans_mutex); |
| 60 | ret = join_transaction(root); |
| 61 | BUG_ON(ret); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame^] | 62 | running_trans_id = root->fs_info->running_transaction->transid; |
| 63 | |
| 64 | if (root != root->fs_info->tree_root && root->last_trans < |
| 65 | running_trans_id) { |
| 66 | radix_tree_tag_set(&root->fs_info->fs_roots_radix, |
| 67 | (unsigned long)root, BTRFS_ROOT_TRANS_TAG); |
| 68 | root->commit_root = root->node; |
| 69 | get_bh(root->node); |
| 70 | } |
| 71 | root->last_trans = running_trans_id; |
| 72 | h->transid = running_trans_id; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 73 | h->transaction = root->fs_info->running_transaction; |
| 74 | h->blocks_reserved = num_blocks; |
| 75 | h->blocks_used = 0; |
| 76 | root->fs_info->running_transaction->use_count++; |
| 77 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 78 | h->magic = h->magic2 = TRANS_MAGIC; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 79 | return h; |
| 80 | } |
| 81 | |
| 82 | int btrfs_end_transaction(struct btrfs_trans_handle *trans, |
| 83 | struct btrfs_root *root) |
| 84 | { |
| 85 | struct btrfs_transaction *cur_trans; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 86 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 87 | WARN_ON(trans->magic != TRANS_MAGIC); |
| 88 | WARN_ON(trans->magic2 != TRANS_MAGIC); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 89 | mutex_lock(&root->fs_info->trans_mutex); |
| 90 | cur_trans = root->fs_info->running_transaction; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 91 | WARN_ON(cur_trans->num_writers < 1); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 92 | if (waitqueue_active(&cur_trans->writer_wait)) |
| 93 | wake_up(&cur_trans->writer_wait); |
| 94 | cur_trans->num_writers--; |
| 95 | put_transaction(cur_trans); |
| 96 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 97 | memset(trans, 0, sizeof(*trans)); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 98 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, |
| 104 | struct btrfs_root *root) |
| 105 | { |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 106 | filemap_write_and_wait(root->fs_info->btree_inode->i_mapping); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, |
| 111 | struct btrfs_root *root) |
| 112 | { |
| 113 | int ret; |
| 114 | u64 old_extent_block; |
| 115 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 116 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 117 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 118 | struct btrfs_root *inode_root = fs_info->inode_root; |
| 119 | |
| 120 | btrfs_set_root_blocknr(&inode_root->root_item, |
| 121 | inode_root->node->b_blocknr); |
| 122 | ret = btrfs_update_root(trans, tree_root, |
| 123 | &inode_root->root_key, |
| 124 | &inode_root->root_item); |
| 125 | BUG_ON(ret); |
| 126 | while(1) { |
| 127 | old_extent_block = btrfs_root_blocknr(&extent_root->root_item); |
| 128 | if (old_extent_block == extent_root->node->b_blocknr) |
| 129 | break; |
| 130 | btrfs_set_root_blocknr(&extent_root->root_item, |
| 131 | extent_root->node->b_blocknr); |
| 132 | ret = btrfs_update_root(trans, tree_root, |
| 133 | &extent_root->root_key, |
| 134 | &extent_root->root_item); |
| 135 | BUG_ON(ret); |
| 136 | } |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static int wait_for_commit(struct btrfs_root *root, |
| 141 | struct btrfs_transaction *commit) |
| 142 | { |
| 143 | DEFINE_WAIT(wait); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 144 | while(!commit->commit_done) { |
| 145 | prepare_to_wait(&commit->commit_wait, &wait, |
| 146 | TASK_UNINTERRUPTIBLE); |
| 147 | if (commit->commit_done) |
| 148 | break; |
| 149 | mutex_unlock(&root->fs_info->trans_mutex); |
| 150 | schedule(); |
| 151 | mutex_lock(&root->fs_info->trans_mutex); |
| 152 | } |
| 153 | finish_wait(&commit->commit_wait, &wait); |
| 154 | return 0; |
| 155 | } |
| 156 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame^] | 157 | struct dirty_root { |
| 158 | struct list_head list; |
| 159 | struct btrfs_key snap_key; |
| 160 | struct buffer_head *commit_root; |
| 161 | struct btrfs_root *root; |
| 162 | }; |
| 163 | |
| 164 | int add_dirty_roots(struct btrfs_trans_handle *trans, |
| 165 | struct radix_tree_root *radix, struct list_head *list) |
| 166 | { |
| 167 | struct dirty_root *dirty; |
| 168 | struct btrfs_root *gang[8]; |
| 169 | struct btrfs_root *root; |
| 170 | int i; |
| 171 | int ret; |
| 172 | int err; |
| 173 | printk("add dirty\n"); |
| 174 | while(1) { |
| 175 | ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, |
| 176 | ARRAY_SIZE(gang), |
| 177 | BTRFS_ROOT_TRANS_TAG); |
| 178 | if (ret == 0) |
| 179 | break; |
| 180 | for (i = 0; i < ret; i++) { |
| 181 | root = gang[i]; |
| 182 | radix_tree_tag_clear(radix, (unsigned long)root, |
| 183 | BTRFS_ROOT_TRANS_TAG); |
| 184 | if (root->commit_root == root->node) { |
| 185 | WARN_ON(root->node->b_blocknr != |
| 186 | btrfs_root_blocknr(&root->root_item)); |
| 187 | brelse(root->commit_root); |
| 188 | root->commit_root = NULL; |
| 189 | continue; |
| 190 | } |
| 191 | dirty = kmalloc(sizeof(*dirty), GFP_NOFS); |
| 192 | BUG_ON(!dirty); |
| 193 | memcpy(&dirty->snap_key, &root->root_key, |
| 194 | sizeof(root->root_key)); |
| 195 | dirty->commit_root = root->commit_root; |
| 196 | root->commit_root = NULL; |
| 197 | dirty->root = root; |
| 198 | printk("adding dirty root %Lu gen %Lu blocknr %Lu\n", root->root_key.objectid, root->root_key.offset, dirty->commit_root->b_blocknr); |
| 199 | root->root_key.offset = root->fs_info->generation; |
| 200 | btrfs_set_root_blocknr(&root->root_item, |
| 201 | root->node->b_blocknr); |
| 202 | err = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 203 | &root->root_key, |
| 204 | &root->root_item); |
| 205 | BUG_ON(err); |
| 206 | list_add(&dirty->list, list); |
| 207 | } |
| 208 | } |
| 209 | printk("add dirty done\n"); |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | int drop_dirty_roots(struct btrfs_root *tree_root, struct list_head *list) |
| 214 | { |
| 215 | struct dirty_root *dirty; |
| 216 | struct btrfs_trans_handle *trans; |
| 217 | int ret; |
| 218 | |
| 219 | while(!list_empty(list)) { |
| 220 | dirty = list_entry(list->next, struct dirty_root, list); |
| 221 | list_del_init(&dirty->list); |
| 222 | trans = btrfs_start_transaction(tree_root, 1); |
| 223 | printk("drop snapshot root %p, commit_root blocknr %Lu generation %Lu\n", dirty->root, dirty->commit_root->b_blocknr, dirty->snap_key.offset); |
| 224 | ret = btrfs_drop_snapshot(trans, dirty->root, |
| 225 | dirty->commit_root); |
| 226 | BUG_ON(ret); |
| 227 | |
| 228 | printk("del root objectid %Lu, offset %Lu\n", dirty->snap_key.objectid, dirty->snap_key.offset); |
| 229 | ret = btrfs_del_root(trans, tree_root, &dirty->snap_key); |
| 230 | BUG_ON(ret); |
| 231 | ret = btrfs_end_transaction(trans, tree_root); |
| 232 | BUG_ON(ret); |
| 233 | kfree(dirty); |
| 234 | } |
| 235 | return 0; |
| 236 | } |
| 237 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 238 | int btrfs_commit_transaction(struct btrfs_trans_handle *trans, |
| 239 | struct btrfs_root *root) |
| 240 | { |
| 241 | int ret = 0; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 242 | struct btrfs_transaction *cur_trans; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame^] | 243 | struct list_head dirty_fs_roots; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 244 | DEFINE_WAIT(wait); |
| 245 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame^] | 246 | INIT_LIST_HEAD(&dirty_fs_roots); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 247 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 248 | mutex_lock(&root->fs_info->trans_mutex); |
| 249 | if (trans->transaction->in_commit) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 250 | printk("already in commit!, waiting\n"); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 251 | cur_trans = trans->transaction; |
| 252 | trans->transaction->use_count++; |
| 253 | btrfs_end_transaction(trans, root); |
| 254 | ret = wait_for_commit(root, cur_trans); |
| 255 | BUG_ON(ret); |
| 256 | put_transaction(cur_trans); |
| 257 | mutex_unlock(&root->fs_info->trans_mutex); |
| 258 | return 0; |
| 259 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 260 | cur_trans = trans->transaction; |
| 261 | trans->transaction->in_commit = 1; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 262 | while (trans->transaction->num_writers > 1) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 263 | WARN_ON(cur_trans != trans->transaction); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 264 | prepare_to_wait(&trans->transaction->writer_wait, &wait, |
| 265 | TASK_UNINTERRUPTIBLE); |
| 266 | if (trans->transaction->num_writers <= 1) |
| 267 | break; |
| 268 | mutex_unlock(&root->fs_info->trans_mutex); |
| 269 | schedule(); |
| 270 | mutex_lock(&root->fs_info->trans_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 271 | finish_wait(&trans->transaction->writer_wait, &wait); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 272 | } |
| 273 | finish_wait(&trans->transaction->writer_wait, &wait); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 274 | WARN_ON(cur_trans != trans->transaction); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame^] | 275 | 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] | 276 | ret = btrfs_commit_tree_roots(trans, root); |
| 277 | BUG_ON(ret); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 278 | cur_trans = root->fs_info->running_transaction; |
| 279 | root->fs_info->running_transaction = NULL; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame^] | 280 | btrfs_set_super_generation(root->fs_info->disk_super, |
| 281 | root->fs_info->generation + 1); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 282 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 283 | ret = btrfs_write_and_wait_transaction(trans, root); |
| 284 | BUG_ON(ret); |
| 285 | |
| 286 | write_ctree_super(trans, root); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 287 | btrfs_finish_extent_commit(trans, root); |
| 288 | mutex_lock(&root->fs_info->trans_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 289 | cur_trans->commit_done = 1; |
| 290 | wake_up(&cur_trans->commit_wait); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 291 | put_transaction(cur_trans); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 292 | put_transaction(cur_trans); |
| 293 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 294 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 295 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame^] | 296 | drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 297 | return ret; |
| 298 | } |
| 299 | |