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 | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 21 | #include <linux/writeback.h> |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 22 | #include <linux/pagemap.h> |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 23 | #include "ctree.h" |
| 24 | #include "disk-io.h" |
| 25 | #include "transaction.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 26 | #include "locking.h" |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 27 | |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 28 | static int total_trans = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 29 | extern struct kmem_cache *btrfs_trans_handle_cachep; |
| 30 | extern struct kmem_cache *btrfs_transaction_cachep; |
| 31 | |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 32 | static struct workqueue_struct *trans_wq; |
| 33 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 34 | #define BTRFS_ROOT_TRANS_TAG 0 |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 35 | #define BTRFS_ROOT_DEFRAG_TAG 1 |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 36 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 37 | static noinline void put_transaction(struct btrfs_transaction *transaction) |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 38 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 39 | WARN_ON(transaction->use_count == 0); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 40 | transaction->use_count--; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 41 | if (transaction->use_count == 0) { |
| 42 | WARN_ON(total_trans == 0); |
| 43 | total_trans--; |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 44 | list_del_init(&transaction->list); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 45 | memset(transaction, 0, sizeof(*transaction)); |
| 46 | kmem_cache_free(btrfs_transaction_cachep, transaction); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 47 | } |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 48 | } |
| 49 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 50 | static noinline int join_transaction(struct btrfs_root *root) |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 51 | { |
| 52 | struct btrfs_transaction *cur_trans; |
| 53 | cur_trans = root->fs_info->running_transaction; |
| 54 | if (!cur_trans) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 55 | cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, |
| 56 | GFP_NOFS); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 57 | total_trans++; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 58 | BUG_ON(!cur_trans); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 59 | root->fs_info->generation++; |
Chris Mason | e18e480 | 2008-01-18 10:54:22 -0500 | [diff] [blame] | 60 | root->fs_info->last_alloc = 0; |
Chris Mason | 4529ba4 | 2008-01-31 16:45:07 -0500 | [diff] [blame] | 61 | root->fs_info->last_data_alloc = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 62 | cur_trans->num_writers = 1; |
| 63 | cur_trans->num_joined = 0; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 64 | cur_trans->transid = root->fs_info->generation; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 65 | init_waitqueue_head(&cur_trans->writer_wait); |
| 66 | init_waitqueue_head(&cur_trans->commit_wait); |
| 67 | cur_trans->in_commit = 0; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 68 | cur_trans->use_count = 1; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 69 | cur_trans->commit_done = 0; |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 70 | cur_trans->start_time = get_seconds(); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 71 | INIT_LIST_HEAD(&cur_trans->pending_snapshots); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 72 | list_add_tail(&cur_trans->list, &root->fs_info->trans_list); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 73 | btrfs_ordered_inode_tree_init(&cur_trans->ordered_inode_tree); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 74 | extent_io_tree_init(&cur_trans->dirty_pages, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 75 | root->fs_info->btree_inode->i_mapping, |
| 76 | GFP_NOFS); |
Chris Mason | 48ec2cf | 2008-06-09 09:35:50 -0400 | [diff] [blame] | 77 | spin_lock(&root->fs_info->new_trans_lock); |
| 78 | root->fs_info->running_transaction = cur_trans; |
| 79 | spin_unlock(&root->fs_info->new_trans_lock); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 80 | } else { |
| 81 | cur_trans->num_writers++; |
| 82 | cur_trans->num_joined++; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 83 | } |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 84 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 88 | static noinline int record_root_in_trans(struct btrfs_root *root) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 89 | { |
| 90 | u64 running_trans_id = root->fs_info->running_transaction->transid; |
| 91 | if (root->ref_cows && root->last_trans < running_trans_id) { |
| 92 | WARN_ON(root == root->fs_info->extent_root); |
| 93 | if (root->root_item.refs != 0) { |
| 94 | radix_tree_tag_set(&root->fs_info->fs_roots_radix, |
| 95 | (unsigned long)root->root_key.objectid, |
| 96 | BTRFS_ROOT_TRANS_TAG); |
| 97 | radix_tree_tag_set(&root->fs_info->fs_roots_radix, |
| 98 | (unsigned long)root->root_key.objectid, |
| 99 | BTRFS_ROOT_DEFRAG_TAG); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 100 | root->commit_root = btrfs_root_node(root); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 101 | } else { |
| 102 | WARN_ON(1); |
| 103 | } |
| 104 | root->last_trans = running_trans_id; |
| 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 109 | struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, |
| 110 | int num_blocks) |
| 111 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 112 | struct btrfs_trans_handle *h = |
| 113 | kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 114 | int ret; |
| 115 | |
| 116 | mutex_lock(&root->fs_info->trans_mutex); |
| 117 | ret = join_transaction(root); |
| 118 | BUG_ON(ret); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 119 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 120 | record_root_in_trans(root); |
| 121 | h->transid = root->fs_info->running_transaction->transid; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 122 | h->transaction = root->fs_info->running_transaction; |
| 123 | h->blocks_reserved = num_blocks; |
| 124 | h->blocks_used = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 125 | h->block_group = NULL; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 126 | h->alloc_exclude_nr = 0; |
| 127 | h->alloc_exclude_start = 0; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 128 | root->fs_info->running_transaction->use_count++; |
| 129 | mutex_unlock(&root->fs_info->trans_mutex); |
| 130 | return h; |
| 131 | } |
| 132 | |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame^] | 133 | static noinline int wait_for_commit(struct btrfs_root *root, |
| 134 | struct btrfs_transaction *commit) |
| 135 | { |
| 136 | DEFINE_WAIT(wait); |
| 137 | mutex_lock(&root->fs_info->trans_mutex); |
| 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 | mutex_unlock(&root->fs_info->trans_mutex); |
| 148 | finish_wait(&commit->commit_wait, &wait); |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, |
| 153 | struct btrfs_root *root, int throttle) |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 154 | { |
| 155 | struct btrfs_transaction *cur_trans; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 156 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 157 | mutex_lock(&root->fs_info->trans_mutex); |
| 158 | cur_trans = root->fs_info->running_transaction; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 159 | WARN_ON(cur_trans != trans->transaction); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 160 | WARN_ON(cur_trans->num_writers < 1); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 161 | cur_trans->num_writers--; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame^] | 162 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 163 | if (waitqueue_active(&cur_trans->writer_wait)) |
| 164 | wake_up(&cur_trans->writer_wait); |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame^] | 165 | |
| 166 | if (cur_trans->in_commit && throttle) { |
| 167 | int ret; |
| 168 | mutex_unlock(&root->fs_info->trans_mutex); |
| 169 | ret = wait_for_commit(root, cur_trans); |
| 170 | BUG_ON(ret); |
| 171 | mutex_lock(&root->fs_info->trans_mutex); |
| 172 | } |
| 173 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 174 | put_transaction(cur_trans); |
| 175 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 176 | memset(trans, 0, sizeof(*trans)); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 177 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame^] | 181 | int btrfs_end_transaction(struct btrfs_trans_handle *trans, |
| 182 | struct btrfs_root *root) |
| 183 | { |
| 184 | return __btrfs_end_transaction(trans, root, 0); |
| 185 | } |
| 186 | |
| 187 | int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, |
| 188 | struct btrfs_root *root) |
| 189 | { |
| 190 | return __btrfs_end_transaction(trans, root, 1); |
| 191 | } |
| 192 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 193 | |
| 194 | int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, |
| 195 | struct btrfs_root *root) |
| 196 | { |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 197 | int ret; |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 198 | int err; |
| 199 | int werr = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 200 | struct extent_io_tree *dirty_pages; |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 201 | struct page *page; |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 202 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 203 | u64 start; |
| 204 | u64 end; |
| 205 | unsigned long index; |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 206 | |
| 207 | if (!trans || !trans->transaction) { |
| 208 | return filemap_write_and_wait(btree_inode->i_mapping); |
| 209 | } |
| 210 | dirty_pages = &trans->transaction->dirty_pages; |
| 211 | while(1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 212 | ret = find_first_extent_bit(dirty_pages, 0, &start, &end, |
| 213 | EXTENT_DIRTY); |
| 214 | if (ret) |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 215 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 216 | clear_extent_dirty(dirty_pages, start, end, GFP_NOFS); |
| 217 | while(start <= end) { |
| 218 | index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 219 | start = (u64)(index + 1) << PAGE_CACHE_SHIFT; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 220 | page = find_lock_page(btree_inode->i_mapping, index); |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 221 | if (!page) |
| 222 | continue; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 223 | if (PageWriteback(page)) { |
| 224 | if (PageDirty(page)) |
| 225 | wait_on_page_writeback(page); |
| 226 | else { |
| 227 | unlock_page(page); |
| 228 | page_cache_release(page); |
| 229 | continue; |
| 230 | } |
| 231 | } |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 232 | err = write_one_page(page, 0); |
| 233 | if (err) |
| 234 | werr = err; |
| 235 | page_cache_release(page); |
| 236 | } |
| 237 | } |
| 238 | err = filemap_fdatawait(btree_inode->i_mapping); |
| 239 | if (err) |
| 240 | werr = err; |
| 241 | return werr; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 242 | } |
| 243 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 244 | static int update_cowonly_root(struct btrfs_trans_handle *trans, |
| 245 | struct btrfs_root *root) |
| 246 | { |
| 247 | int ret; |
| 248 | u64 old_root_bytenr; |
| 249 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
| 250 | |
| 251 | btrfs_write_dirty_block_groups(trans, root); |
| 252 | while(1) { |
| 253 | old_root_bytenr = btrfs_root_bytenr(&root->root_item); |
| 254 | if (old_root_bytenr == root->node->start) |
| 255 | break; |
| 256 | btrfs_set_root_bytenr(&root->root_item, |
| 257 | root->node->start); |
| 258 | btrfs_set_root_level(&root->root_item, |
| 259 | btrfs_header_level(root->node)); |
| 260 | ret = btrfs_update_root(trans, tree_root, |
| 261 | &root->root_key, |
| 262 | &root->root_item); |
| 263 | BUG_ON(ret); |
| 264 | btrfs_write_dirty_block_groups(trans, root); |
| 265 | } |
| 266 | return 0; |
| 267 | } |
| 268 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 269 | int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, |
| 270 | struct btrfs_root *root) |
| 271 | { |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 272 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 273 | struct list_head *next; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 274 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 275 | while(!list_empty(&fs_info->dirty_cowonly_roots)) { |
| 276 | next = fs_info->dirty_cowonly_roots.next; |
| 277 | list_del_init(next); |
| 278 | root = list_entry(next, struct btrfs_root, dirty_list); |
| 279 | update_cowonly_root(trans, root); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 280 | } |
| 281 | return 0; |
| 282 | } |
| 283 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 284 | struct dirty_root { |
| 285 | struct list_head list; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 286 | struct btrfs_root *root; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 287 | struct btrfs_root *latest_root; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 288 | }; |
| 289 | |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 290 | int btrfs_add_dead_root(struct btrfs_root *root, |
| 291 | struct btrfs_root *latest, |
| 292 | struct list_head *dead_list) |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 293 | { |
| 294 | struct dirty_root *dirty; |
| 295 | |
| 296 | dirty = kmalloc(sizeof(*dirty), GFP_NOFS); |
| 297 | if (!dirty) |
| 298 | return -ENOMEM; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 299 | dirty->root = root; |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 300 | dirty->latest_root = latest; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 301 | list_add(&dirty->list, dead_list); |
| 302 | return 0; |
| 303 | } |
| 304 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 305 | static noinline int add_dirty_roots(struct btrfs_trans_handle *trans, |
| 306 | struct radix_tree_root *radix, |
| 307 | struct list_head *list) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 308 | { |
| 309 | struct dirty_root *dirty; |
| 310 | struct btrfs_root *gang[8]; |
| 311 | struct btrfs_root *root; |
| 312 | int i; |
| 313 | int ret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 314 | int err = 0; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 315 | u32 refs; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 316 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 317 | while(1) { |
| 318 | ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, |
| 319 | ARRAY_SIZE(gang), |
| 320 | BTRFS_ROOT_TRANS_TAG); |
| 321 | if (ret == 0) |
| 322 | break; |
| 323 | for (i = 0; i < ret; i++) { |
| 324 | root = gang[i]; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 325 | radix_tree_tag_clear(radix, |
| 326 | (unsigned long)root->root_key.objectid, |
| 327 | BTRFS_ROOT_TRANS_TAG); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 328 | if (root->commit_root == root->node) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 329 | WARN_ON(root->node->start != |
| 330 | btrfs_root_bytenr(&root->root_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 331 | free_extent_buffer(root->commit_root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 332 | root->commit_root = NULL; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 333 | |
| 334 | /* make sure to update the root on disk |
| 335 | * so we get any updates to the block used |
| 336 | * counts |
| 337 | */ |
| 338 | err = btrfs_update_root(trans, |
| 339 | root->fs_info->tree_root, |
| 340 | &root->root_key, |
| 341 | &root->root_item); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 342 | continue; |
| 343 | } |
| 344 | dirty = kmalloc(sizeof(*dirty), GFP_NOFS); |
| 345 | BUG_ON(!dirty); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 346 | dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS); |
| 347 | BUG_ON(!dirty->root); |
| 348 | |
| 349 | memset(&root->root_item.drop_progress, 0, |
| 350 | sizeof(struct btrfs_disk_key)); |
| 351 | root->root_item.drop_level = 0; |
| 352 | |
| 353 | memcpy(dirty->root, root, sizeof(*root)); |
| 354 | dirty->root->node = root->commit_root; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 355 | dirty->latest_root = root; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 356 | root->commit_root = NULL; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 357 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 358 | root->root_key.offset = root->fs_info->generation; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 359 | btrfs_set_root_bytenr(&root->root_item, |
| 360 | root->node->start); |
| 361 | btrfs_set_root_level(&root->root_item, |
| 362 | btrfs_header_level(root->node)); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 363 | err = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 364 | &root->root_key, |
| 365 | &root->root_item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 366 | if (err) |
| 367 | break; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 368 | |
| 369 | refs = btrfs_root_refs(&dirty->root->root_item); |
| 370 | btrfs_set_root_refs(&dirty->root->root_item, refs - 1); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 371 | err = btrfs_update_root(trans, root->fs_info->tree_root, |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 372 | &dirty->root->root_key, |
| 373 | &dirty->root->root_item); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 374 | |
| 375 | BUG_ON(err); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 376 | if (refs == 1) { |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 377 | list_add(&dirty->list, list); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 378 | } else { |
| 379 | WARN_ON(1); |
| 380 | kfree(dirty->root); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 381 | kfree(dirty); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 382 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 383 | } |
| 384 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 385 | return err; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 386 | } |
| 387 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 388 | int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) |
| 389 | { |
| 390 | struct btrfs_fs_info *info = root->fs_info; |
| 391 | int ret; |
| 392 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 393 | unsigned long nr; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 394 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 395 | smp_mb(); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 396 | if (root->defrag_running) |
| 397 | return 0; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 398 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 399 | while (1) { |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 400 | root->defrag_running = 1; |
| 401 | ret = btrfs_defrag_leaves(trans, root, cacheonly); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 402 | nr = trans->blocks_used; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 403 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 404 | btrfs_btree_balance_dirty(info->tree_root, nr); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 405 | cond_resched(); |
| 406 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 407 | trans = btrfs_start_transaction(root, 1); |
| 408 | if (ret != -EAGAIN) |
| 409 | break; |
| 410 | } |
| 411 | root->defrag_running = 0; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 412 | smp_mb(); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 413 | radix_tree_tag_clear(&info->fs_roots_radix, |
| 414 | (unsigned long)root->root_key.objectid, |
| 415 | BTRFS_ROOT_DEFRAG_TAG); |
| 416 | btrfs_end_transaction(trans, root); |
| 417 | return 0; |
| 418 | } |
| 419 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 420 | int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info) |
| 421 | { |
| 422 | struct btrfs_root *gang[1]; |
| 423 | struct btrfs_root *root; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 424 | int i; |
| 425 | int ret; |
| 426 | int err = 0; |
| 427 | u64 last = 0; |
| 428 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 429 | while(1) { |
| 430 | ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix, |
| 431 | (void **)gang, last, |
| 432 | ARRAY_SIZE(gang), |
| 433 | BTRFS_ROOT_DEFRAG_TAG); |
| 434 | if (ret == 0) |
| 435 | break; |
| 436 | for (i = 0; i < ret; i++) { |
| 437 | root = gang[i]; |
| 438 | last = root->root_key.objectid + 1; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 439 | btrfs_defrag_root(root, 1); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 440 | } |
| 441 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 442 | btrfs_defrag_root(info->extent_root, 1); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 443 | return err; |
| 444 | } |
| 445 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 446 | static noinline int drop_dirty_roots(struct btrfs_root *tree_root, |
| 447 | struct list_head *list) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 448 | { |
| 449 | struct dirty_root *dirty; |
| 450 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 451 | unsigned long nr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 452 | u64 num_bytes; |
| 453 | u64 bytes_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 454 | int ret = 0; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 455 | int err; |
| 456 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 457 | while(!list_empty(list)) { |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 458 | struct btrfs_root *root; |
| 459 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 460 | dirty = list_entry(list->next, struct dirty_root, list); |
| 461 | list_del_init(&dirty->list); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 462 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 463 | num_bytes = btrfs_root_used(&dirty->root->root_item); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 464 | root = dirty->latest_root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 465 | atomic_inc(&root->fs_info->throttles); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 466 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 467 | mutex_lock(&root->fs_info->drop_mutex); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 468 | while(1) { |
| 469 | trans = btrfs_start_transaction(tree_root, 1); |
| 470 | ret = btrfs_drop_snapshot(trans, dirty->root); |
| 471 | if (ret != -EAGAIN) { |
| 472 | break; |
| 473 | } |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 474 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 475 | err = btrfs_update_root(trans, |
| 476 | tree_root, |
| 477 | &dirty->root->root_key, |
| 478 | &dirty->root->root_item); |
| 479 | if (err) |
| 480 | ret = err; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 481 | nr = trans->blocks_used; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 482 | ret = btrfs_end_transaction(trans, tree_root); |
| 483 | BUG_ON(ret); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 484 | |
| 485 | mutex_unlock(&root->fs_info->drop_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 486 | btrfs_btree_balance_dirty(tree_root, nr); |
Chris Mason | 4dc11904 | 2007-10-15 16:18:14 -0400 | [diff] [blame] | 487 | cond_resched(); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 488 | mutex_lock(&root->fs_info->drop_mutex); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 489 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 490 | BUG_ON(ret); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 491 | atomic_dec(&root->fs_info->throttles); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 492 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 493 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 494 | num_bytes -= btrfs_root_used(&dirty->root->root_item); |
| 495 | bytes_used = btrfs_root_used(&root->root_item); |
| 496 | if (num_bytes) { |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 497 | record_root_in_trans(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 498 | btrfs_set_root_used(&root->root_item, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 499 | bytes_used - num_bytes); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 500 | } |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 501 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 502 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 503 | ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 504 | if (ret) { |
| 505 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 506 | break; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 507 | } |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 508 | mutex_unlock(&root->fs_info->drop_mutex); |
| 509 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 510 | nr = trans->blocks_used; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 511 | ret = btrfs_end_transaction(trans, tree_root); |
| 512 | BUG_ON(ret); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 513 | |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 514 | free_extent_buffer(dirty->root->node); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 515 | kfree(dirty->root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 516 | kfree(dirty); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 517 | |
| 518 | btrfs_btree_balance_dirty(tree_root, nr); |
Chris Mason | 4dc11904 | 2007-10-15 16:18:14 -0400 | [diff] [blame] | 519 | cond_resched(); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 520 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 521 | return ret; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 522 | } |
| 523 | |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 524 | int btrfs_write_ordered_inodes(struct btrfs_trans_handle *trans, |
| 525 | struct btrfs_root *root) |
| 526 | { |
| 527 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 528 | struct inode *inode; |
| 529 | u64 root_objectid = 0; |
| 530 | u64 objectid = 0; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 531 | int ret; |
| 532 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 533 | atomic_inc(&root->fs_info->throttles); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 534 | while(1) { |
| 535 | ret = btrfs_find_first_ordered_inode( |
| 536 | &cur_trans->ordered_inode_tree, |
Chris Mason | 4d5e74b | 2008-01-16 16:09:22 -0500 | [diff] [blame] | 537 | &root_objectid, &objectid, &inode); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 538 | if (!ret) |
| 539 | break; |
| 540 | |
| 541 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 4d5e74b | 2008-01-16 16:09:22 -0500 | [diff] [blame] | 542 | |
Chris Mason | 81d7ed2 | 2008-04-25 08:51:48 -0400 | [diff] [blame] | 543 | if (S_ISREG(inode->i_mode)) { |
| 544 | atomic_inc(&BTRFS_I(inode)->ordered_writeback); |
Chris Mason | 4d5e74b | 2008-01-16 16:09:22 -0500 | [diff] [blame] | 545 | filemap_fdatawrite(inode->i_mapping); |
Chris Mason | 81d7ed2 | 2008-04-25 08:51:48 -0400 | [diff] [blame] | 546 | atomic_dec(&BTRFS_I(inode)->ordered_writeback); |
| 547 | } |
Chris Mason | 4d5e74b | 2008-01-16 16:09:22 -0500 | [diff] [blame] | 548 | iput(inode); |
| 549 | |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 550 | mutex_lock(&root->fs_info->trans_mutex); |
| 551 | } |
| 552 | while(1) { |
| 553 | root_objectid = 0; |
| 554 | objectid = 0; |
| 555 | ret = btrfs_find_del_first_ordered_inode( |
| 556 | &cur_trans->ordered_inode_tree, |
Chris Mason | 4d5e74b | 2008-01-16 16:09:22 -0500 | [diff] [blame] | 557 | &root_objectid, &objectid, &inode); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 558 | if (!ret) |
| 559 | break; |
| 560 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 4d5e74b | 2008-01-16 16:09:22 -0500 | [diff] [blame] | 561 | |
Chris Mason | 81d7ed2 | 2008-04-25 08:51:48 -0400 | [diff] [blame] | 562 | if (S_ISREG(inode->i_mode)) { |
| 563 | atomic_inc(&BTRFS_I(inode)->ordered_writeback); |
Chris Mason | 4d5e74b | 2008-01-16 16:09:22 -0500 | [diff] [blame] | 564 | filemap_write_and_wait(inode->i_mapping); |
Chris Mason | 81d7ed2 | 2008-04-25 08:51:48 -0400 | [diff] [blame] | 565 | atomic_dec(&BTRFS_I(inode)->ordered_writeback); |
| 566 | } |
Chris Mason | 4d5e74b | 2008-01-16 16:09:22 -0500 | [diff] [blame] | 567 | atomic_dec(&inode->i_count); |
| 568 | iput(inode); |
| 569 | |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 570 | mutex_lock(&root->fs_info->trans_mutex); |
| 571 | } |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 572 | atomic_dec(&root->fs_info->throttles); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 573 | return 0; |
| 574 | } |
| 575 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 576 | static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 577 | struct btrfs_fs_info *fs_info, |
| 578 | struct btrfs_pending_snapshot *pending) |
| 579 | { |
| 580 | struct btrfs_key key; |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 581 | struct btrfs_root_item *new_root_item; |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 582 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 583 | struct btrfs_root *root = pending->root; |
| 584 | struct extent_buffer *tmp; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 585 | struct extent_buffer *old; |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 586 | int ret; |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 587 | int namelen; |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 588 | u64 objectid; |
| 589 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 590 | new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); |
| 591 | if (!new_root_item) { |
| 592 | ret = -ENOMEM; |
| 593 | goto fail; |
| 594 | } |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 595 | ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid); |
| 596 | if (ret) |
| 597 | goto fail; |
| 598 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 599 | memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 600 | |
| 601 | key.objectid = objectid; |
| 602 | key.offset = 1; |
| 603 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 604 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 605 | old = btrfs_lock_root_node(root); |
| 606 | btrfs_cow_block(trans, root, old, NULL, 0, &old); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 607 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 608 | btrfs_copy_root(trans, root, old, &tmp, objectid); |
| 609 | btrfs_tree_unlock(old); |
| 610 | free_extent_buffer(old); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 611 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 612 | btrfs_set_root_bytenr(new_root_item, tmp->start); |
| 613 | btrfs_set_root_level(new_root_item, btrfs_header_level(tmp)); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 614 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 615 | new_root_item); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 616 | btrfs_tree_unlock(tmp); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 617 | free_extent_buffer(tmp); |
| 618 | if (ret) |
| 619 | goto fail; |
| 620 | |
| 621 | /* |
| 622 | * insert the directory item |
| 623 | */ |
| 624 | key.offset = (u64)-1; |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 625 | namelen = strlen(pending->name); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 626 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 627 | pending->name, namelen, |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 628 | root->fs_info->sb->s_root->d_inode->i_ino, |
| 629 | &key, BTRFS_FT_DIR); |
| 630 | |
| 631 | if (ret) |
| 632 | goto fail; |
| 633 | |
| 634 | ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root, |
| 635 | pending->name, strlen(pending->name), objectid, |
| 636 | root->fs_info->sb->s_root->d_inode->i_ino); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 637 | |
| 638 | /* Invalidate existing dcache entry for new snapshot. */ |
| 639 | btrfs_invalidate_dcache_root(root, pending->name, namelen); |
| 640 | |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 641 | fail: |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 642 | kfree(new_root_item); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 643 | return ret; |
| 644 | } |
| 645 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 646 | static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, |
| 647 | struct btrfs_fs_info *fs_info) |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 648 | { |
| 649 | struct btrfs_pending_snapshot *pending; |
| 650 | struct list_head *head = &trans->transaction->pending_snapshots; |
| 651 | int ret; |
| 652 | |
| 653 | while(!list_empty(head)) { |
| 654 | pending = list_entry(head->next, |
| 655 | struct btrfs_pending_snapshot, list); |
| 656 | ret = create_pending_snapshot(trans, fs_info, pending); |
| 657 | BUG_ON(ret); |
| 658 | list_del(&pending->list); |
| 659 | kfree(pending->name); |
| 660 | kfree(pending); |
| 661 | } |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 662 | return 0; |
| 663 | } |
| 664 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 665 | int btrfs_commit_transaction(struct btrfs_trans_handle *trans, |
| 666 | struct btrfs_root *root) |
| 667 | { |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 668 | unsigned long joined = 0; |
| 669 | unsigned long timeout = 1; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 670 | struct btrfs_transaction *cur_trans; |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 671 | struct btrfs_transaction *prev_trans = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 672 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 673 | struct list_head dirty_fs_roots; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 674 | struct extent_io_tree *pinned_copy; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 675 | DEFINE_WAIT(wait); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 676 | int ret; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 677 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 678 | INIT_LIST_HEAD(&dirty_fs_roots); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 679 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 680 | mutex_lock(&root->fs_info->trans_mutex); |
| 681 | if (trans->transaction->in_commit) { |
| 682 | cur_trans = trans->transaction; |
| 683 | trans->transaction->use_count++; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 684 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 685 | btrfs_end_transaction(trans, root); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 686 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 687 | ret = wait_for_commit(root, cur_trans); |
| 688 | BUG_ON(ret); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 689 | |
| 690 | mutex_lock(&root->fs_info->trans_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 691 | put_transaction(cur_trans); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 692 | mutex_unlock(&root->fs_info->trans_mutex); |
| 693 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 694 | return 0; |
| 695 | } |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 696 | |
| 697 | pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS); |
| 698 | if (!pinned_copy) |
| 699 | return -ENOMEM; |
| 700 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 701 | extent_io_tree_init(pinned_copy, |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 702 | root->fs_info->btree_inode->i_mapping, GFP_NOFS); |
| 703 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 704 | trans->transaction->in_commit = 1; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame^] | 705 | printk("trans %Lu in commit\n", trans->transid); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 706 | cur_trans = trans->transaction; |
| 707 | if (cur_trans->list.prev != &root->fs_info->trans_list) { |
| 708 | prev_trans = list_entry(cur_trans->list.prev, |
| 709 | struct btrfs_transaction, list); |
| 710 | if (!prev_trans->commit_done) { |
| 711 | prev_trans->use_count++; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 712 | mutex_unlock(&root->fs_info->trans_mutex); |
| 713 | |
| 714 | wait_for_commit(root, prev_trans); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 715 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 716 | mutex_lock(&root->fs_info->trans_mutex); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 717 | put_transaction(prev_trans); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 718 | } |
| 719 | } |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 720 | |
| 721 | do { |
| 722 | joined = cur_trans->num_joined; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 723 | WARN_ON(cur_trans != trans->transaction); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 724 | prepare_to_wait(&cur_trans->writer_wait, &wait, |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 725 | TASK_UNINTERRUPTIBLE); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 726 | |
| 727 | if (cur_trans->num_writers > 1) |
| 728 | timeout = MAX_SCHEDULE_TIMEOUT; |
| 729 | else |
| 730 | timeout = 1; |
| 731 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 732 | mutex_unlock(&root->fs_info->trans_mutex); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 733 | |
| 734 | schedule_timeout(timeout); |
| 735 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 736 | mutex_lock(&root->fs_info->trans_mutex); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 737 | finish_wait(&cur_trans->writer_wait, &wait); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 738 | ret = btrfs_write_ordered_inodes(trans, root); |
| 739 | |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 740 | } while (cur_trans->num_writers > 1 || |
| 741 | (cur_trans->num_joined != joined)); |
| 742 | |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 743 | ret = create_pending_snapshots(trans, root->fs_info); |
| 744 | BUG_ON(ret); |
| 745 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 746 | WARN_ON(cur_trans != trans->transaction); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 747 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 748 | ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, |
| 749 | &dirty_fs_roots); |
| 750 | BUG_ON(ret); |
| 751 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 752 | ret = btrfs_commit_tree_roots(trans, root); |
| 753 | BUG_ON(ret); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 754 | |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 755 | cur_trans = root->fs_info->running_transaction; |
Chris Mason | cee36a0 | 2008-01-15 08:40:48 -0500 | [diff] [blame] | 756 | spin_lock(&root->fs_info->new_trans_lock); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 757 | root->fs_info->running_transaction = NULL; |
Chris Mason | cee36a0 | 2008-01-15 08:40:48 -0500 | [diff] [blame] | 758 | spin_unlock(&root->fs_info->new_trans_lock); |
Chris Mason | 4b52dff | 2007-06-26 10:06:50 -0400 | [diff] [blame] | 759 | btrfs_set_super_generation(&root->fs_info->super_copy, |
| 760 | cur_trans->transid); |
| 761 | btrfs_set_super_root(&root->fs_info->super_copy, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 762 | root->fs_info->tree_root->node->start); |
| 763 | btrfs_set_super_root_level(&root->fs_info->super_copy, |
| 764 | btrfs_header_level(root->fs_info->tree_root->node)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 765 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 766 | btrfs_set_super_chunk_root(&root->fs_info->super_copy, |
| 767 | chunk_root->node->start); |
| 768 | btrfs_set_super_chunk_root_level(&root->fs_info->super_copy, |
| 769 | btrfs_header_level(chunk_root->node)); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 770 | memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy, |
| 771 | sizeof(root->fs_info->super_copy)); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 772 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 773 | btrfs_copy_pinned(root, pinned_copy); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 774 | |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 775 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 776 | ret = btrfs_write_and_wait_transaction(trans, root); |
| 777 | BUG_ON(ret); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 778 | write_ctree_super(trans, root); |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 779 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 780 | btrfs_finish_extent_commit(trans, root, pinned_copy); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 781 | mutex_lock(&root->fs_info->trans_mutex); |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 782 | |
| 783 | kfree(pinned_copy); |
| 784 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 785 | cur_trans->commit_done = 1; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame^] | 786 | printk("trans %Lu done in commit\n", cur_trans->transid); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 787 | root->fs_info->last_trans_committed = cur_trans->transid; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 788 | wake_up(&cur_trans->commit_wait); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 789 | put_transaction(cur_trans); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 790 | put_transaction(cur_trans); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 791 | |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 792 | if (root->fs_info->closing) |
| 793 | list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); |
| 794 | else |
| 795 | list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 796 | |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 797 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 798 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 799 | |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 800 | if (root->fs_info->closing) { |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 801 | drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); |
Chris Mason | facda1e | 2007-06-08 18:11:48 -0400 | [diff] [blame] | 802 | } |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 803 | return ret; |
| 804 | } |
| 805 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 806 | int btrfs_clean_old_snapshots(struct btrfs_root *root) |
| 807 | { |
| 808 | struct list_head dirty_roots; |
| 809 | INIT_LIST_HEAD(&dirty_roots); |
| 810 | |
| 811 | mutex_lock(&root->fs_info->trans_mutex); |
| 812 | list_splice_init(&root->fs_info->dead_roots, &dirty_roots); |
| 813 | mutex_unlock(&root->fs_info->trans_mutex); |
| 814 | |
| 815 | if (!list_empty(&dirty_roots)) { |
| 816 | drop_dirty_roots(root, &dirty_roots); |
| 817 | } |
| 818 | return 0; |
| 819 | } |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 820 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
| 821 | void btrfs_transaction_cleaner(void *p) |
| 822 | #else |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 823 | void btrfs_transaction_cleaner(struct work_struct *work) |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 824 | #endif |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 825 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 826 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
| 827 | struct btrfs_fs_info *fs_info = p; |
| 828 | #else |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 829 | struct btrfs_fs_info *fs_info = container_of(work, |
| 830 | struct btrfs_fs_info, |
| 831 | trans_work.work); |
| 832 | |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 833 | #endif |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 834 | struct btrfs_root *root = fs_info->tree_root; |
| 835 | struct btrfs_transaction *cur; |
| 836 | struct btrfs_trans_handle *trans; |
| 837 | unsigned long now; |
| 838 | unsigned long delay = HZ * 30; |
| 839 | int ret; |
| 840 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 841 | smp_mb(); |
Chris Mason | d6bfde8 | 2008-04-30 13:59:35 -0400 | [diff] [blame] | 842 | if (root->fs_info->closing) |
| 843 | goto out; |
| 844 | |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 845 | mutex_lock(&root->fs_info->trans_mutex); |
| 846 | cur = root->fs_info->running_transaction; |
| 847 | if (!cur) { |
| 848 | mutex_unlock(&root->fs_info->trans_mutex); |
| 849 | goto out; |
| 850 | } |
| 851 | now = get_seconds(); |
| 852 | if (now < cur->start_time || now - cur->start_time < 30) { |
| 853 | mutex_unlock(&root->fs_info->trans_mutex); |
| 854 | delay = HZ * 5; |
| 855 | goto out; |
| 856 | } |
| 857 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 858 | btrfs_defrag_dirty_roots(root->fs_info); |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 859 | trans = btrfs_start_transaction(root, 1); |
| 860 | ret = btrfs_commit_transaction(trans, root); |
| 861 | out: |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 862 | btrfs_clean_old_snapshots(root); |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 863 | btrfs_transaction_queue_work(root, delay); |
| 864 | } |
| 865 | |
| 866 | void btrfs_transaction_queue_work(struct btrfs_root *root, int delay) |
| 867 | { |
Chris Mason | d6bfde8 | 2008-04-30 13:59:35 -0400 | [diff] [blame] | 868 | if (!root->fs_info->closing) |
| 869 | queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay); |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | void btrfs_transaction_flush_work(struct btrfs_root *root) |
| 873 | { |
Chris Mason | d6bfde8 | 2008-04-30 13:59:35 -0400 | [diff] [blame] | 874 | cancel_delayed_work(&root->fs_info->trans_work); |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 875 | flush_workqueue(trans_wq); |
| 876 | } |
| 877 | |
| 878 | void __init btrfs_init_transaction_sys(void) |
| 879 | { |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 880 | trans_wq = create_workqueue("btrfs-transaction"); |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 881 | } |
| 882 | |
Christian Hesse | 17636e0 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 883 | void btrfs_exit_transaction_sys(void) |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 884 | { |
| 885 | destroy_workqueue(trans_wq); |
| 886 | } |
| 887 | |