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