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