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