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