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