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