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