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