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