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