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