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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Chris Mason | 3408878 | 2007-06-12 11:36:58 -0400 | [diff] [blame] | 21 | #include <linux/sched.h> |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 22 | #include <linux/writeback.h> |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 23 | #include <linux/pagemap.h> |
Chris Mason | 5f2cc08 | 2008-11-07 18:22:45 -0500 | [diff] [blame] | 24 | #include <linux/blkdev.h> |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 25 | #include "ctree.h" |
| 26 | #include "disk-io.h" |
| 27 | #include "transaction.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 28 | #include "locking.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 | { |
Josef Bacik | 13c5a93 | 2011-04-11 15:45:29 -0400 | [diff] [blame] | 35 | WARN_ON(atomic_read(&transaction->use_count) == 0); |
| 36 | if (atomic_dec_and_test(&transaction->use_count)) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 37 | memset(transaction, 0, sizeof(*transaction)); |
| 38 | kmem_cache_free(btrfs_transaction_cachep, transaction); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 39 | } |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 40 | } |
| 41 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 42 | static noinline void switch_commit_root(struct btrfs_root *root) |
| 43 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 44 | free_extent_buffer(root->commit_root); |
| 45 | root->commit_root = btrfs_root_node(root); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 46 | } |
| 47 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 48 | /* |
| 49 | * either allocate a new transaction or hop into the existing one |
| 50 | */ |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 51 | static noinline int join_transaction(struct btrfs_root *root) |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 52 | { |
| 53 | struct btrfs_transaction *cur_trans; |
| 54 | cur_trans = root->fs_info->running_transaction; |
| 55 | if (!cur_trans) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 56 | cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, |
| 57 | GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 58 | if (!cur_trans) |
| 59 | return -ENOMEM; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 60 | root->fs_info->generation++; |
Josef Bacik | 13c5a93 | 2011-04-11 15:45:29 -0400 | [diff] [blame] | 61 | atomic_set(&cur_trans->num_writers, 1); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 62 | cur_trans->num_joined = 0; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 63 | cur_trans->transid = root->fs_info->generation; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 64 | init_waitqueue_head(&cur_trans->writer_wait); |
| 65 | init_waitqueue_head(&cur_trans->commit_wait); |
| 66 | cur_trans->in_commit = 0; |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 67 | cur_trans->blocked = 0; |
Josef Bacik | 13c5a93 | 2011-04-11 15:45:29 -0400 | [diff] [blame] | 68 | atomic_set(&cur_trans->use_count, 1); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 69 | cur_trans->commit_done = 0; |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 70 | cur_trans->start_time = get_seconds(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 71 | |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 72 | cur_trans->delayed_refs.root = RB_ROOT; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 73 | cur_trans->delayed_refs.num_entries = 0; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 74 | cur_trans->delayed_refs.num_heads_ready = 0; |
| 75 | cur_trans->delayed_refs.num_heads = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 76 | cur_trans->delayed_refs.flushing = 0; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 77 | cur_trans->delayed_refs.run_delayed_start = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 78 | spin_lock_init(&cur_trans->delayed_refs.lock); |
| 79 | |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 80 | INIT_LIST_HEAD(&cur_trans->pending_snapshots); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 81 | list_add_tail(&cur_trans->list, &root->fs_info->trans_list); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 82 | extent_io_tree_init(&cur_trans->dirty_pages, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 83 | root->fs_info->btree_inode->i_mapping, |
| 84 | GFP_NOFS); |
Chris Mason | 48ec2cf | 2008-06-09 09:35:50 -0400 | [diff] [blame] | 85 | spin_lock(&root->fs_info->new_trans_lock); |
| 86 | root->fs_info->running_transaction = cur_trans; |
| 87 | spin_unlock(&root->fs_info->new_trans_lock); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 88 | } else { |
Josef Bacik | 13c5a93 | 2011-04-11 15:45:29 -0400 | [diff] [blame] | 89 | atomic_inc(&cur_trans->num_writers); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 90 | cur_trans->num_joined++; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 91 | } |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 92 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 96 | /* |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 97 | * this does all the record keeping required to make sure that a reference |
| 98 | * counted root is properly recorded in a given transaction. This is required |
| 99 | * to make sure the old root from before we joined the transaction is deleted |
| 100 | * when the transaction commits |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 101 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 102 | static noinline int record_root_in_trans(struct btrfs_trans_handle *trans, |
| 103 | struct btrfs_root *root) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 104 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 105 | if (root->ref_cows && root->last_trans < trans->transid) { |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 106 | WARN_ON(root == root->fs_info->extent_root); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 107 | WARN_ON(root->commit_root != root->node); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 108 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 109 | radix_tree_tag_set(&root->fs_info->fs_roots_radix, |
| 110 | (unsigned long)root->root_key.objectid, |
| 111 | BTRFS_ROOT_TRANS_TAG); |
| 112 | root->last_trans = trans->transid; |
| 113 | btrfs_init_reloc_root(trans, root); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 114 | } |
| 115 | return 0; |
| 116 | } |
| 117 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 118 | int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, |
| 119 | struct btrfs_root *root) |
| 120 | { |
| 121 | if (!root->ref_cows) |
| 122 | return 0; |
| 123 | |
| 124 | mutex_lock(&root->fs_info->trans_mutex); |
| 125 | if (root->last_trans == trans->transid) { |
| 126 | mutex_unlock(&root->fs_info->trans_mutex); |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | record_root_in_trans(trans, root); |
| 131 | mutex_unlock(&root->fs_info->trans_mutex); |
| 132 | return 0; |
| 133 | } |
| 134 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 135 | /* wait for commit against the current transaction to become unblocked |
| 136 | * when this is done, it is safe to start a new transaction, but the current |
| 137 | * transaction might not be fully on disk. |
| 138 | */ |
Chris Mason | 37d1aee | 2008-07-31 10:48:37 -0400 | [diff] [blame] | 139 | static void wait_current_trans(struct btrfs_root *root) |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 140 | { |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 141 | struct btrfs_transaction *cur_trans; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 142 | |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 143 | cur_trans = root->fs_info->running_transaction; |
Chris Mason | 37d1aee | 2008-07-31 10:48:37 -0400 | [diff] [blame] | 144 | if (cur_trans && cur_trans->blocked) { |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 145 | DEFINE_WAIT(wait); |
Josef Bacik | 13c5a93 | 2011-04-11 15:45:29 -0400 | [diff] [blame] | 146 | atomic_inc(&cur_trans->use_count); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 147 | while (1) { |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 148 | prepare_to_wait(&root->fs_info->transaction_wait, &wait, |
| 149 | TASK_UNINTERRUPTIBLE); |
Zhao Lei | 471fa17 | 2010-03-25 12:35:14 +0000 | [diff] [blame] | 150 | if (!cur_trans->blocked) |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 151 | break; |
Zhao Lei | 471fa17 | 2010-03-25 12:35:14 +0000 | [diff] [blame] | 152 | mutex_unlock(&root->fs_info->trans_mutex); |
| 153 | schedule(); |
| 154 | mutex_lock(&root->fs_info->trans_mutex); |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 155 | } |
Zhao Lei | 471fa17 | 2010-03-25 12:35:14 +0000 | [diff] [blame] | 156 | finish_wait(&root->fs_info->transaction_wait, &wait); |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 157 | put_transaction(cur_trans); |
| 158 | } |
Chris Mason | 37d1aee | 2008-07-31 10:48:37 -0400 | [diff] [blame] | 159 | } |
| 160 | |
Josef Bacik | 249ac1e | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 161 | enum btrfs_trans_type { |
| 162 | TRANS_START, |
| 163 | TRANS_JOIN, |
| 164 | TRANS_USERSPACE, |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 165 | TRANS_JOIN_NOLOCK, |
Josef Bacik | 249ac1e | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 166 | }; |
| 167 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 168 | static int may_wait_transaction(struct btrfs_root *root, int type) |
Chris Mason | 37d1aee | 2008-07-31 10:48:37 -0400 | [diff] [blame] | 169 | { |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 170 | if (!root->fs_info->log_root_recovering && |
Josef Bacik | 249ac1e | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 171 | ((type == TRANS_START && !root->fs_info->open_ioctl_trans) || |
| 172 | type == TRANS_USERSPACE)) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 173 | return 1; |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, |
| 178 | u64 num_items, int type) |
| 179 | { |
| 180 | struct btrfs_trans_handle *h; |
| 181 | struct btrfs_transaction *cur_trans; |
Josef Bacik | 06d5a58 | 2011-04-05 11:57:27 -0400 | [diff] [blame] | 182 | int retries = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 183 | int ret; |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 184 | |
| 185 | if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) |
| 186 | return ERR_PTR(-EROFS); |
Josef Bacik | 2a1eb46 | 2011-04-13 15:15:59 -0400 | [diff] [blame^] | 187 | |
| 188 | if (current->journal_info) { |
| 189 | WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK); |
| 190 | h = current->journal_info; |
| 191 | h->use_count++; |
| 192 | h->orig_rsv = h->block_rsv; |
| 193 | h->block_rsv = NULL; |
| 194 | goto got_it; |
| 195 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 196 | again: |
| 197 | h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); |
| 198 | if (!h) |
| 199 | return ERR_PTR(-ENOMEM); |
| 200 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 201 | if (type != TRANS_JOIN_NOLOCK) |
| 202 | mutex_lock(&root->fs_info->trans_mutex); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 203 | if (may_wait_transaction(root, type)) |
Chris Mason | 37d1aee | 2008-07-31 10:48:37 -0400 | [diff] [blame] | 204 | wait_current_trans(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 205 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 206 | ret = join_transaction(root); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 207 | if (ret < 0) { |
Yoshinori Sano | 6e8df2a | 2011-04-03 12:31:28 +0000 | [diff] [blame] | 208 | kmem_cache_free(btrfs_trans_handle_cachep, h); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 209 | if (type != TRANS_JOIN_NOLOCK) |
| 210 | mutex_unlock(&root->fs_info->trans_mutex); |
| 211 | return ERR_PTR(ret); |
| 212 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 213 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 214 | cur_trans = root->fs_info->running_transaction; |
Josef Bacik | 13c5a93 | 2011-04-11 15:45:29 -0400 | [diff] [blame] | 215 | atomic_inc(&cur_trans->use_count); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 216 | if (type != TRANS_JOIN_NOLOCK) |
| 217 | mutex_unlock(&root->fs_info->trans_mutex); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 218 | |
| 219 | h->transid = cur_trans->transid; |
| 220 | h->transaction = cur_trans; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 221 | h->blocks_used = 0; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 222 | h->block_group = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 223 | h->bytes_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 224 | h->delayed_ref_updates = 0; |
Josef Bacik | 2a1eb46 | 2011-04-13 15:15:59 -0400 | [diff] [blame^] | 225 | h->use_count = 1; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 226 | h->block_rsv = NULL; |
Josef Bacik | 2a1eb46 | 2011-04-13 15:15:59 -0400 | [diff] [blame^] | 227 | h->orig_rsv = NULL; |
Chris Mason | b7ec40d | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 228 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 229 | smp_mb(); |
| 230 | if (cur_trans->blocked && may_wait_transaction(root, type)) { |
| 231 | btrfs_commit_transaction(h, root); |
| 232 | goto again; |
| 233 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 234 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 235 | if (num_items > 0) { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 236 | ret = btrfs_trans_reserve_metadata(h, root, num_items); |
Josef Bacik | 06d5a58 | 2011-04-05 11:57:27 -0400 | [diff] [blame] | 237 | if (ret == -EAGAIN && !retries) { |
| 238 | retries++; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 239 | btrfs_commit_transaction(h, root); |
| 240 | goto again; |
Josef Bacik | 06d5a58 | 2011-04-05 11:57:27 -0400 | [diff] [blame] | 241 | } else if (ret == -EAGAIN) { |
| 242 | /* |
| 243 | * We have already retried and got EAGAIN, so really we |
| 244 | * don't have space, so set ret to -ENOSPC. |
| 245 | */ |
| 246 | ret = -ENOSPC; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 247 | } |
Josef Bacik | 06d5a58 | 2011-04-05 11:57:27 -0400 | [diff] [blame] | 248 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 249 | if (ret < 0) { |
| 250 | btrfs_end_transaction(h, root); |
| 251 | return ERR_PTR(ret); |
| 252 | } |
| 253 | } |
| 254 | |
Josef Bacik | 2a1eb46 | 2011-04-13 15:15:59 -0400 | [diff] [blame^] | 255 | got_it: |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 256 | if (type != TRANS_JOIN_NOLOCK) |
| 257 | mutex_lock(&root->fs_info->trans_mutex); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 258 | record_root_in_trans(h, root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 259 | if (type != TRANS_JOIN_NOLOCK) |
| 260 | mutex_unlock(&root->fs_info->trans_mutex); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 261 | |
| 262 | if (!current->journal_info && type != TRANS_USERSPACE) |
| 263 | current->journal_info = h; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 264 | return h; |
| 265 | } |
| 266 | |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 267 | struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 268 | int num_items) |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 269 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 270 | return start_transaction(root, num_items, TRANS_START); |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 271 | } |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 272 | struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 273 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 274 | return start_transaction(root, 0, TRANS_JOIN); |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 275 | } |
| 276 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 277 | struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 278 | { |
| 279 | return start_transaction(root, 0, TRANS_JOIN_NOLOCK); |
| 280 | } |
| 281 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 282 | struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root) |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 283 | { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 284 | return start_transaction(root, 0, TRANS_USERSPACE); |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 285 | } |
| 286 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 287 | /* wait for a transaction commit to be fully complete */ |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 288 | static noinline int wait_for_commit(struct btrfs_root *root, |
| 289 | struct btrfs_transaction *commit) |
| 290 | { |
| 291 | DEFINE_WAIT(wait); |
| 292 | mutex_lock(&root->fs_info->trans_mutex); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 293 | while (!commit->commit_done) { |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 294 | prepare_to_wait(&commit->commit_wait, &wait, |
| 295 | TASK_UNINTERRUPTIBLE); |
| 296 | if (commit->commit_done) |
| 297 | break; |
| 298 | mutex_unlock(&root->fs_info->trans_mutex); |
| 299 | schedule(); |
| 300 | mutex_lock(&root->fs_info->trans_mutex); |
| 301 | } |
| 302 | mutex_unlock(&root->fs_info->trans_mutex); |
| 303 | finish_wait(&commit->commit_wait, &wait); |
| 304 | return 0; |
| 305 | } |
| 306 | |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 307 | int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid) |
| 308 | { |
| 309 | struct btrfs_transaction *cur_trans = NULL, *t; |
| 310 | int ret; |
| 311 | |
| 312 | mutex_lock(&root->fs_info->trans_mutex); |
| 313 | |
| 314 | ret = 0; |
| 315 | if (transid) { |
| 316 | if (transid <= root->fs_info->last_trans_committed) |
| 317 | goto out_unlock; |
| 318 | |
| 319 | /* find specified transaction */ |
| 320 | list_for_each_entry(t, &root->fs_info->trans_list, list) { |
| 321 | if (t->transid == transid) { |
| 322 | cur_trans = t; |
| 323 | break; |
| 324 | } |
| 325 | if (t->transid > transid) |
| 326 | break; |
| 327 | } |
| 328 | ret = -EINVAL; |
| 329 | if (!cur_trans) |
| 330 | goto out_unlock; /* bad transid */ |
| 331 | } else { |
| 332 | /* find newest transaction that is committing | committed */ |
| 333 | list_for_each_entry_reverse(t, &root->fs_info->trans_list, |
| 334 | list) { |
| 335 | if (t->in_commit) { |
| 336 | if (t->commit_done) |
| 337 | goto out_unlock; |
| 338 | cur_trans = t; |
| 339 | break; |
| 340 | } |
| 341 | } |
| 342 | if (!cur_trans) |
| 343 | goto out_unlock; /* nothing committing|committed */ |
| 344 | } |
| 345 | |
Josef Bacik | 13c5a93 | 2011-04-11 15:45:29 -0400 | [diff] [blame] | 346 | atomic_inc(&cur_trans->use_count); |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 347 | mutex_unlock(&root->fs_info->trans_mutex); |
| 348 | |
| 349 | wait_for_commit(root, cur_trans); |
| 350 | |
| 351 | mutex_lock(&root->fs_info->trans_mutex); |
| 352 | put_transaction(cur_trans); |
| 353 | ret = 0; |
| 354 | out_unlock: |
| 355 | mutex_unlock(&root->fs_info->trans_mutex); |
| 356 | return ret; |
| 357 | } |
| 358 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 359 | #if 0 |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 360 | /* |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 361 | * rate limit against the drop_snapshot code. This helps to slow down new |
| 362 | * operations if the drop_snapshot code isn't able to keep up. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 363 | */ |
Chris Mason | 37d1aee | 2008-07-31 10:48:37 -0400 | [diff] [blame] | 364 | static void throttle_on_drops(struct btrfs_root *root) |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 365 | { |
| 366 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 367 | int harder_count = 0; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 368 | |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 369 | harder: |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 370 | if (atomic_read(&info->throttles)) { |
| 371 | DEFINE_WAIT(wait); |
| 372 | int thr; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 373 | thr = atomic_read(&info->throttle_gen); |
| 374 | |
| 375 | do { |
| 376 | prepare_to_wait(&info->transaction_throttle, |
| 377 | &wait, TASK_UNINTERRUPTIBLE); |
| 378 | if (!atomic_read(&info->throttles)) { |
| 379 | finish_wait(&info->transaction_throttle, &wait); |
| 380 | break; |
| 381 | } |
| 382 | schedule(); |
| 383 | finish_wait(&info->transaction_throttle, &wait); |
| 384 | } while (thr == atomic_read(&info->throttle_gen)); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 385 | harder_count++; |
| 386 | |
| 387 | if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 && |
| 388 | harder_count < 2) |
| 389 | goto harder; |
| 390 | |
| 391 | if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 && |
| 392 | harder_count < 10) |
| 393 | goto harder; |
| 394 | |
| 395 | if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 && |
| 396 | harder_count < 20) |
| 397 | goto harder; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 398 | } |
| 399 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 400 | #endif |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 401 | |
Chris Mason | 37d1aee | 2008-07-31 10:48:37 -0400 | [diff] [blame] | 402 | void btrfs_throttle(struct btrfs_root *root) |
| 403 | { |
| 404 | mutex_lock(&root->fs_info->trans_mutex); |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 405 | if (!root->fs_info->open_ioctl_trans) |
| 406 | wait_current_trans(root); |
Chris Mason | 37d1aee | 2008-07-31 10:48:37 -0400 | [diff] [blame] | 407 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 37d1aee | 2008-07-31 10:48:37 -0400 | [diff] [blame] | 408 | } |
| 409 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 410 | static int should_end_transaction(struct btrfs_trans_handle *trans, |
| 411 | struct btrfs_root *root) |
| 412 | { |
| 413 | int ret; |
| 414 | ret = btrfs_block_rsv_check(trans, root, |
| 415 | &root->fs_info->global_block_rsv, 0, 5); |
| 416 | return ret ? 1 : 0; |
| 417 | } |
| 418 | |
| 419 | int btrfs_should_end_transaction(struct btrfs_trans_handle *trans, |
| 420 | struct btrfs_root *root) |
| 421 | { |
| 422 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 423 | int updates; |
| 424 | |
| 425 | if (cur_trans->blocked || cur_trans->delayed_refs.flushing) |
| 426 | return 1; |
| 427 | |
| 428 | updates = trans->delayed_ref_updates; |
| 429 | trans->delayed_ref_updates = 0; |
| 430 | if (updates) |
| 431 | btrfs_run_delayed_refs(trans, root, updates); |
| 432 | |
| 433 | return should_end_transaction(trans, root); |
| 434 | } |
| 435 | |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 436 | static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 437 | struct btrfs_root *root, int throttle, int lock) |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 438 | { |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 439 | struct btrfs_transaction *cur_trans = trans->transaction; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 440 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 441 | int count = 0; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 442 | |
Josef Bacik | 2a1eb46 | 2011-04-13 15:15:59 -0400 | [diff] [blame^] | 443 | if (--trans->use_count) { |
| 444 | trans->block_rsv = trans->orig_rsv; |
| 445 | return 0; |
| 446 | } |
| 447 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 448 | while (count < 4) { |
| 449 | unsigned long cur = trans->delayed_ref_updates; |
| 450 | trans->delayed_ref_updates = 0; |
| 451 | if (cur && |
| 452 | trans->transaction->delayed_refs.num_heads_ready > 64) { |
| 453 | trans->delayed_ref_updates = 0; |
Chris Mason | b7ec40d | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 454 | |
| 455 | /* |
| 456 | * do a full flush if the transaction is trying |
| 457 | * to close |
| 458 | */ |
| 459 | if (trans->transaction->delayed_refs.flushing) |
| 460 | cur = 0; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 461 | btrfs_run_delayed_refs(trans, root, cur); |
| 462 | } else { |
| 463 | break; |
| 464 | } |
| 465 | count++; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 466 | } |
| 467 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 468 | btrfs_trans_release_metadata(trans, root); |
| 469 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 470 | if (lock && !root->fs_info->open_ioctl_trans && |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 471 | should_end_transaction(trans, root)) |
| 472 | trans->transaction->blocked = 1; |
| 473 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 474 | if (lock && cur_trans->blocked && !cur_trans->in_commit) { |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 475 | if (throttle) |
| 476 | return btrfs_commit_transaction(trans, root); |
| 477 | else |
| 478 | wake_up_process(info->transaction_kthread); |
| 479 | } |
| 480 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 481 | WARN_ON(cur_trans != info->running_transaction); |
Josef Bacik | 13c5a93 | 2011-04-11 15:45:29 -0400 | [diff] [blame] | 482 | WARN_ON(atomic_read(&cur_trans->num_writers) < 1); |
| 483 | atomic_dec(&cur_trans->num_writers); |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 484 | |
Sage Weil | 99d16cbc | 2010-10-29 15:37:34 -0400 | [diff] [blame] | 485 | smp_mb(); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 486 | if (waitqueue_active(&cur_trans->writer_wait)) |
| 487 | wake_up(&cur_trans->writer_wait); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 488 | put_transaction(cur_trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 489 | |
| 490 | if (current->journal_info == trans) |
| 491 | current->journal_info = NULL; |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 492 | memset(trans, 0, sizeof(*trans)); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 493 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 494 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 495 | if (throttle) |
| 496 | btrfs_run_delayed_iputs(root); |
| 497 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 498 | return 0; |
| 499 | } |
| 500 | |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 501 | int btrfs_end_transaction(struct btrfs_trans_handle *trans, |
| 502 | struct btrfs_root *root) |
| 503 | { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 504 | return __btrfs_end_transaction(trans, root, 0, 1); |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, |
| 508 | struct btrfs_root *root) |
| 509 | { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 510 | return __btrfs_end_transaction(trans, root, 1, 1); |
| 511 | } |
| 512 | |
| 513 | int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans, |
| 514 | struct btrfs_root *root) |
| 515 | { |
| 516 | return __btrfs_end_transaction(trans, root, 0, 0); |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 517 | } |
| 518 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 519 | /* |
| 520 | * when btree blocks are allocated, they have some corresponding bits set for |
| 521 | * them in one of two extent_io trees. This is used to make sure all of |
Chris Mason | 690587d | 2009-10-13 13:29:19 -0400 | [diff] [blame] | 522 | * those extents are sent to disk but does not wait on them |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 523 | */ |
Chris Mason | 690587d | 2009-10-13 13:29:19 -0400 | [diff] [blame] | 524 | int btrfs_write_marked_extents(struct btrfs_root *root, |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 525 | struct extent_io_tree *dirty_pages, int mark) |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 526 | { |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 527 | int ret; |
Chris Mason | 777e6bd | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 528 | int err = 0; |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 529 | int werr = 0; |
| 530 | struct page *page; |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 531 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | 777e6bd | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 532 | u64 start = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 533 | u64 end; |
| 534 | unsigned long index; |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 535 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 536 | while (1) { |
Chris Mason | 777e6bd | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 537 | ret = find_first_extent_bit(dirty_pages, start, &start, &end, |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 538 | mark); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 539 | if (ret) |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 540 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 541 | while (start <= end) { |
Chris Mason | 777e6bd | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 542 | cond_resched(); |
| 543 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 544 | index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 545 | start = (u64)(index + 1) << PAGE_CACHE_SHIFT; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 546 | page = find_get_page(btree_inode->i_mapping, index); |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 547 | if (!page) |
| 548 | continue; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 549 | |
| 550 | btree_lock_page_hook(page); |
| 551 | if (!page->mapping) { |
| 552 | unlock_page(page); |
| 553 | page_cache_release(page); |
| 554 | continue; |
| 555 | } |
| 556 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 557 | if (PageWriteback(page)) { |
| 558 | if (PageDirty(page)) |
| 559 | wait_on_page_writeback(page); |
| 560 | else { |
| 561 | unlock_page(page); |
| 562 | page_cache_release(page); |
| 563 | continue; |
| 564 | } |
| 565 | } |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 566 | err = write_one_page(page, 0); |
| 567 | if (err) |
| 568 | werr = err; |
| 569 | page_cache_release(page); |
| 570 | } |
| 571 | } |
Chris Mason | 690587d | 2009-10-13 13:29:19 -0400 | [diff] [blame] | 572 | if (err) |
| 573 | werr = err; |
| 574 | return werr; |
| 575 | } |
| 576 | |
| 577 | /* |
| 578 | * when btree blocks are allocated, they have some corresponding bits set for |
| 579 | * them in one of two extent_io trees. This is used to make sure all of |
| 580 | * those extents are on disk for transaction or log commit. We wait |
| 581 | * on all the pages and clear them from the dirty pages state tree |
| 582 | */ |
| 583 | int btrfs_wait_marked_extents(struct btrfs_root *root, |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 584 | struct extent_io_tree *dirty_pages, int mark) |
Chris Mason | 690587d | 2009-10-13 13:29:19 -0400 | [diff] [blame] | 585 | { |
| 586 | int ret; |
| 587 | int err = 0; |
| 588 | int werr = 0; |
| 589 | struct page *page; |
| 590 | struct inode *btree_inode = root->fs_info->btree_inode; |
| 591 | u64 start = 0; |
| 592 | u64 end; |
| 593 | unsigned long index; |
| 594 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 595 | while (1) { |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 596 | ret = find_first_extent_bit(dirty_pages, start, &start, &end, |
| 597 | mark); |
Chris Mason | 777e6bd | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 598 | if (ret) |
| 599 | break; |
| 600 | |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 601 | clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 602 | while (start <= end) { |
Chris Mason | 777e6bd | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 603 | index = start >> PAGE_CACHE_SHIFT; |
| 604 | start = (u64)(index + 1) << PAGE_CACHE_SHIFT; |
| 605 | page = find_get_page(btree_inode->i_mapping, index); |
| 606 | if (!page) |
| 607 | continue; |
| 608 | if (PageDirty(page)) { |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 609 | btree_lock_page_hook(page); |
| 610 | wait_on_page_writeback(page); |
Chris Mason | 777e6bd | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 611 | err = write_one_page(page, 0); |
| 612 | if (err) |
| 613 | werr = err; |
| 614 | } |
Chris Mason | 105d931 | 2008-11-18 12:13:12 -0500 | [diff] [blame] | 615 | wait_on_page_writeback(page); |
Chris Mason | 777e6bd | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 616 | page_cache_release(page); |
| 617 | cond_resched(); |
| 618 | } |
| 619 | } |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 620 | if (err) |
| 621 | werr = err; |
| 622 | return werr; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 623 | } |
| 624 | |
Chris Mason | 690587d | 2009-10-13 13:29:19 -0400 | [diff] [blame] | 625 | /* |
| 626 | * when btree blocks are allocated, they have some corresponding bits set for |
| 627 | * them in one of two extent_io trees. This is used to make sure all of |
| 628 | * those extents are on disk for transaction or log commit |
| 629 | */ |
| 630 | int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 631 | struct extent_io_tree *dirty_pages, int mark) |
Chris Mason | 690587d | 2009-10-13 13:29:19 -0400 | [diff] [blame] | 632 | { |
| 633 | int ret; |
| 634 | int ret2; |
| 635 | |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 636 | ret = btrfs_write_marked_extents(root, dirty_pages, mark); |
| 637 | ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark); |
Chris Mason | 690587d | 2009-10-13 13:29:19 -0400 | [diff] [blame] | 638 | return ret || ret2; |
| 639 | } |
| 640 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 641 | int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, |
| 642 | struct btrfs_root *root) |
| 643 | { |
| 644 | if (!trans || !trans->transaction) { |
| 645 | struct inode *btree_inode; |
| 646 | btree_inode = root->fs_info->btree_inode; |
| 647 | return filemap_write_and_wait(btree_inode->i_mapping); |
| 648 | } |
| 649 | return btrfs_write_and_wait_marked_extents(root, |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 650 | &trans->transaction->dirty_pages, |
| 651 | EXTENT_DIRTY); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 652 | } |
| 653 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 654 | /* |
| 655 | * this is used to update the root pointer in the tree of tree roots. |
| 656 | * |
| 657 | * But, in the case of the extent allocation tree, updating the root |
| 658 | * pointer may allocate blocks which may change the root of the extent |
| 659 | * allocation tree. |
| 660 | * |
| 661 | * So, this loops and repeats and makes sure the cowonly root didn't |
| 662 | * change while the root pointer was being updated in the metadata. |
| 663 | */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 664 | static int update_cowonly_root(struct btrfs_trans_handle *trans, |
| 665 | struct btrfs_root *root) |
| 666 | { |
| 667 | int ret; |
| 668 | u64 old_root_bytenr; |
Yan, Zheng | 86b9f2e | 2009-11-12 09:36:50 +0000 | [diff] [blame] | 669 | u64 old_root_used; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 670 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
| 671 | |
Yan, Zheng | 86b9f2e | 2009-11-12 09:36:50 +0000 | [diff] [blame] | 672 | old_root_used = btrfs_root_used(&root->root_item); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 673 | btrfs_write_dirty_block_groups(trans, root); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 674 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 675 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 676 | old_root_bytenr = btrfs_root_bytenr(&root->root_item); |
Yan, Zheng | 86b9f2e | 2009-11-12 09:36:50 +0000 | [diff] [blame] | 677 | if (old_root_bytenr == root->node->start && |
| 678 | old_root_used == btrfs_root_used(&root->root_item)) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 679 | break; |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 680 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 681 | btrfs_set_root_node(&root->root_item, root->node); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 682 | ret = btrfs_update_root(trans, tree_root, |
| 683 | &root->root_key, |
| 684 | &root->root_item); |
| 685 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 686 | |
Yan, Zheng | 86b9f2e | 2009-11-12 09:36:50 +0000 | [diff] [blame] | 687 | old_root_used = btrfs_root_used(&root->root_item); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 688 | ret = btrfs_write_dirty_block_groups(trans, root); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 689 | BUG_ON(ret); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 690 | } |
Yan Zheng | 276e680 | 2009-07-30 09:40:40 -0400 | [diff] [blame] | 691 | |
| 692 | if (root != root->fs_info->extent_root) |
| 693 | switch_commit_root(root); |
| 694 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 695 | return 0; |
| 696 | } |
| 697 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 698 | /* |
| 699 | * update all the cowonly tree roots on disk |
| 700 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 701 | static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans, |
| 702 | struct btrfs_root *root) |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 703 | { |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 704 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 705 | struct list_head *next; |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 706 | struct extent_buffer *eb; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 707 | int ret; |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 708 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 709 | ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); |
| 710 | BUG_ON(ret); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 711 | |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 712 | eb = btrfs_lock_root_node(fs_info->tree_root); |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 713 | btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 714 | btrfs_tree_unlock(eb); |
| 715 | free_extent_buffer(eb); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 716 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 717 | ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); |
| 718 | BUG_ON(ret); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 719 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 720 | while (!list_empty(&fs_info->dirty_cowonly_roots)) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 721 | next = fs_info->dirty_cowonly_roots.next; |
| 722 | list_del_init(next); |
| 723 | root = list_entry(next, struct btrfs_root, dirty_list); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 724 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 725 | update_cowonly_root(trans, root); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 726 | } |
Yan Zheng | 276e680 | 2009-07-30 09:40:40 -0400 | [diff] [blame] | 727 | |
| 728 | down_write(&fs_info->extent_commit_sem); |
| 729 | switch_commit_root(fs_info->extent_root); |
| 730 | up_write(&fs_info->extent_commit_sem); |
| 731 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 732 | return 0; |
| 733 | } |
| 734 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 735 | /* |
| 736 | * dead roots are old snapshots that need to be deleted. This allocates |
| 737 | * a dirty root struct and adds it into the list of dead roots that need to |
| 738 | * be deleted |
| 739 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 740 | int btrfs_add_dead_root(struct btrfs_root *root) |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 741 | { |
Yan Zheng | b48652c | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 742 | mutex_lock(&root->fs_info->trans_mutex); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 743 | list_add(&root->root_list, &root->fs_info->dead_roots); |
Yan Zheng | b48652c | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 744 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 745 | return 0; |
| 746 | } |
| 747 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 748 | /* |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 749 | * update all the cowonly tree roots on disk |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 750 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 751 | static noinline int commit_fs_roots(struct btrfs_trans_handle *trans, |
| 752 | struct btrfs_root *root) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 753 | { |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 754 | struct btrfs_root *gang[8]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 755 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 756 | int i; |
| 757 | int ret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 758 | int err = 0; |
| 759 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 760 | while (1) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 761 | ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, |
| 762 | (void **)gang, 0, |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 763 | ARRAY_SIZE(gang), |
| 764 | BTRFS_ROOT_TRANS_TAG); |
| 765 | if (ret == 0) |
| 766 | break; |
| 767 | for (i = 0; i < ret; i++) { |
| 768 | root = gang[i]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 769 | radix_tree_tag_clear(&fs_info->fs_roots_radix, |
| 770 | (unsigned long)root->root_key.objectid, |
| 771 | BTRFS_ROOT_TRANS_TAG); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 772 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 773 | btrfs_free_log(trans, root); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 774 | btrfs_update_reloc_root(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 775 | btrfs_orphan_commit_root(trans, root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 776 | |
Yan Zheng | 978d910 | 2009-06-15 20:01:02 -0400 | [diff] [blame] | 777 | if (root->commit_root != root->node) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 778 | switch_commit_root(root); |
Yan Zheng | 978d910 | 2009-06-15 20:01:02 -0400 | [diff] [blame] | 779 | btrfs_set_root_node(&root->root_item, |
| 780 | root->node); |
| 781 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 782 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 783 | err = btrfs_update_root(trans, fs_info->tree_root, |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 784 | &root->root_key, |
| 785 | &root->root_item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 786 | if (err) |
| 787 | break; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 788 | } |
| 789 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 790 | return err; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 791 | } |
| 792 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 793 | /* |
| 794 | * defrag a given btree. If cacheonly == 1, this won't read from the disk, |
| 795 | * otherwise every leaf in the btree is read and defragged. |
| 796 | */ |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 797 | int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) |
| 798 | { |
| 799 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 800 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 801 | int ret; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 802 | unsigned long nr; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 803 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 804 | if (xchg(&root->defrag_running, 1)) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 805 | return 0; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 806 | |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 807 | while (1) { |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 808 | trans = btrfs_start_transaction(root, 0); |
| 809 | if (IS_ERR(trans)) |
| 810 | return PTR_ERR(trans); |
| 811 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 812 | ret = btrfs_defrag_leaves(trans, root, cacheonly); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 813 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 814 | nr = trans->blocks_used; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 815 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 816 | btrfs_btree_balance_dirty(info->tree_root, nr); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 817 | cond_resched(); |
| 818 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 819 | if (root->fs_info->closing || ret != -EAGAIN) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 820 | break; |
| 821 | } |
| 822 | root->defrag_running = 0; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 823 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 824 | } |
| 825 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 826 | #if 0 |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 827 | /* |
Chris Mason | b7ec40d | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 828 | * when dropping snapshots, we generate a ton of delayed refs, and it makes |
| 829 | * sense not to join the transaction while it is trying to flush the current |
| 830 | * queue of delayed refs out. |
| 831 | * |
| 832 | * This is used by the drop snapshot code only |
| 833 | */ |
| 834 | static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info) |
| 835 | { |
| 836 | DEFINE_WAIT(wait); |
| 837 | |
| 838 | mutex_lock(&info->trans_mutex); |
| 839 | while (info->running_transaction && |
| 840 | info->running_transaction->delayed_refs.flushing) { |
| 841 | prepare_to_wait(&info->transaction_wait, &wait, |
| 842 | TASK_UNINTERRUPTIBLE); |
| 843 | mutex_unlock(&info->trans_mutex); |
Chris Mason | 59bc5c7 | 2009-04-24 14:39:25 -0400 | [diff] [blame] | 844 | |
Chris Mason | b7ec40d | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 845 | schedule(); |
Chris Mason | 59bc5c7 | 2009-04-24 14:39:25 -0400 | [diff] [blame] | 846 | |
Chris Mason | b7ec40d | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 847 | mutex_lock(&info->trans_mutex); |
| 848 | finish_wait(&info->transaction_wait, &wait); |
| 849 | } |
| 850 | mutex_unlock(&info->trans_mutex); |
| 851 | return 0; |
| 852 | } |
| 853 | |
| 854 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 855 | * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on |
| 856 | * all of them |
| 857 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 858 | int btrfs_drop_dead_root(struct btrfs_root *root) |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 859 | { |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 860 | struct btrfs_trans_handle *trans; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 861 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 862 | unsigned long nr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 863 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 864 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 865 | while (1) { |
| 866 | /* |
| 867 | * we don't want to jump in and create a bunch of |
| 868 | * delayed refs if the transaction is starting to close |
| 869 | */ |
| 870 | wait_transaction_pre_flush(tree_root->fs_info); |
| 871 | trans = btrfs_start_transaction(tree_root, 1); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 872 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 873 | /* |
| 874 | * we've joined a transaction, make sure it isn't |
| 875 | * closing right now |
| 876 | */ |
| 877 | if (trans->transaction->delayed_refs.flushing) { |
| 878 | btrfs_end_transaction(trans, tree_root); |
| 879 | continue; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 880 | } |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 881 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 882 | ret = btrfs_drop_snapshot(trans, root); |
| 883 | if (ret != -EAGAIN) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 884 | break; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 885 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 886 | ret = btrfs_update_root(trans, tree_root, |
| 887 | &root->root_key, |
| 888 | &root->root_item); |
| 889 | if (ret) |
| 890 | break; |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 891 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 892 | nr = trans->blocks_used; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 893 | ret = btrfs_end_transaction(trans, tree_root); |
| 894 | BUG_ON(ret); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 895 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 896 | btrfs_btree_balance_dirty(tree_root, nr); |
Chris Mason | 4dc11904 | 2007-10-15 16:18:14 -0400 | [diff] [blame] | 897 | cond_resched(); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 898 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 899 | BUG_ON(ret); |
| 900 | |
| 901 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
| 902 | BUG_ON(ret); |
| 903 | |
| 904 | nr = trans->blocks_used; |
| 905 | ret = btrfs_end_transaction(trans, tree_root); |
| 906 | BUG_ON(ret); |
| 907 | |
| 908 | free_extent_buffer(root->node); |
| 909 | free_extent_buffer(root->commit_root); |
| 910 | kfree(root); |
| 911 | |
| 912 | btrfs_btree_balance_dirty(tree_root, nr); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 913 | return ret; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 914 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 915 | #endif |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 916 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 917 | /* |
| 918 | * new snapshots need to be created at a very specific time in the |
| 919 | * transaction commit. This does the actual creation |
| 920 | */ |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 921 | static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 922 | struct btrfs_fs_info *fs_info, |
| 923 | struct btrfs_pending_snapshot *pending) |
| 924 | { |
| 925 | struct btrfs_key key; |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 926 | struct btrfs_root_item *new_root_item; |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 927 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 928 | struct btrfs_root *root = pending->root; |
Sage Weil | 6bdb72d | 2010-03-15 17:27:13 +0000 | [diff] [blame] | 929 | struct btrfs_root *parent_root; |
| 930 | struct inode *parent_inode; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 931 | struct dentry *parent; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 932 | struct dentry *dentry; |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 933 | struct extent_buffer *tmp; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 934 | struct extent_buffer *old; |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 935 | int ret; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 936 | u64 to_reserve = 0; |
Sage Weil | 6bdb72d | 2010-03-15 17:27:13 +0000 | [diff] [blame] | 937 | u64 index = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 938 | u64 objectid; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 939 | u64 root_flags; |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 940 | |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 941 | new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); |
| 942 | if (!new_root_item) { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 943 | pending->error = -ENOMEM; |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 944 | goto fail; |
| 945 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 946 | |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 947 | ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 948 | if (ret) { |
| 949 | pending->error = ret; |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 950 | goto fail; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 951 | } |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 952 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 953 | btrfs_reloc_pre_snapshot(trans, pending, &to_reserve); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 954 | btrfs_orphan_pre_snapshot(trans, pending, &to_reserve); |
| 955 | |
| 956 | if (to_reserve > 0) { |
| 957 | ret = btrfs_block_rsv_add(trans, root, &pending->block_rsv, |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 958 | to_reserve); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 959 | if (ret) { |
| 960 | pending->error = ret; |
| 961 | goto fail; |
| 962 | } |
| 963 | } |
| 964 | |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 965 | key.objectid = objectid; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 966 | key.offset = (u64)-1; |
| 967 | key.type = BTRFS_ROOT_ITEM_KEY; |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 968 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 969 | trans->block_rsv = &pending->block_rsv; |
Sage Weil | 6bdb72d | 2010-03-15 17:27:13 +0000 | [diff] [blame] | 970 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 971 | dentry = pending->dentry; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 972 | parent = dget_parent(dentry); |
| 973 | parent_inode = parent->d_inode; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 974 | parent_root = BTRFS_I(parent_inode)->root; |
Sage Weil | 6bdb72d | 2010-03-15 17:27:13 +0000 | [diff] [blame] | 975 | record_root_in_trans(trans, parent_root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 976 | |
Sage Weil | 6bdb72d | 2010-03-15 17:27:13 +0000 | [diff] [blame] | 977 | /* |
| 978 | * insert the directory item |
| 979 | */ |
Sage Weil | 6bdb72d | 2010-03-15 17:27:13 +0000 | [diff] [blame] | 980 | ret = btrfs_set_inode_index(parent_inode, &index); |
| 981 | BUG_ON(ret); |
| 982 | ret = btrfs_insert_dir_item(trans, parent_root, |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 983 | dentry->d_name.name, dentry->d_name.len, |
| 984 | parent_inode->i_ino, &key, |
| 985 | BTRFS_FT_DIR, index); |
Sage Weil | 6bdb72d | 2010-03-15 17:27:13 +0000 | [diff] [blame] | 986 | BUG_ON(ret); |
| 987 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 988 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
| 989 | dentry->d_name.len * 2); |
Sage Weil | 6bdb72d | 2010-03-15 17:27:13 +0000 | [diff] [blame] | 990 | ret = btrfs_update_inode(trans, parent_root, parent_inode); |
| 991 | BUG_ON(ret); |
| 992 | |
| 993 | record_root_in_trans(trans, root); |
| 994 | btrfs_set_root_last_snapshot(&root->root_item, trans->transid); |
| 995 | memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); |
Li Zefan | 08fe4db | 2011-03-28 02:01:25 +0000 | [diff] [blame] | 996 | btrfs_check_and_init_root_item(new_root_item); |
Sage Weil | 6bdb72d | 2010-03-15 17:27:13 +0000 | [diff] [blame] | 997 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 998 | root_flags = btrfs_root_flags(new_root_item); |
| 999 | if (pending->readonly) |
| 1000 | root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; |
| 1001 | else |
| 1002 | root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; |
| 1003 | btrfs_set_root_flags(new_root_item, root_flags); |
| 1004 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1005 | old = btrfs_lock_root_node(root); |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1006 | btrfs_cow_block(trans, root, old, NULL, 0, &old); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1007 | btrfs_set_lock_blocking(old); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1008 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1009 | btrfs_copy_root(trans, root, old, &tmp, objectid); |
| 1010 | btrfs_tree_unlock(old); |
| 1011 | free_extent_buffer(old); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1012 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1013 | btrfs_set_root_node(new_root_item, tmp); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1014 | /* record when the snapshot was created in key.offset */ |
| 1015 | key.offset = trans->transid; |
| 1016 | ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1017 | btrfs_tree_unlock(tmp); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1018 | free_extent_buffer(tmp); |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 1019 | BUG_ON(ret); |
| 1020 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1021 | /* |
| 1022 | * insert root back/forward references |
| 1023 | */ |
| 1024 | ret = btrfs_add_root_ref(trans, tree_root, objectid, |
| 1025 | parent_root->root_key.objectid, |
| 1026 | parent_inode->i_ino, index, |
| 1027 | dentry->d_name.name, dentry->d_name.len); |
| 1028 | BUG_ON(ret); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 1029 | dput(parent); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1030 | |
| 1031 | key.offset = (u64)-1; |
| 1032 | pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key); |
| 1033 | BUG_ON(IS_ERR(pending->snap)); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 1034 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1035 | btrfs_reloc_post_snapshot(trans, pending); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 1036 | btrfs_orphan_post_snapshot(trans, pending); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1037 | fail: |
Sage Weil | 6bdb72d | 2010-03-15 17:27:13 +0000 | [diff] [blame] | 1038 | kfree(new_root_item); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1039 | btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1); |
| 1040 | return 0; |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1041 | } |
| 1042 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1043 | /* |
| 1044 | * create all the snapshots we've scheduled for creation |
| 1045 | */ |
Chris Mason | 80b6794 | 2008-02-01 16:35:04 -0500 | [diff] [blame] | 1046 | static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, |
| 1047 | struct btrfs_fs_info *fs_info) |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1048 | { |
| 1049 | struct btrfs_pending_snapshot *pending; |
| 1050 | struct list_head *head = &trans->transaction->pending_snapshots; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1051 | int ret; |
| 1052 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1053 | list_for_each_entry(pending, head, list) { |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1054 | ret = create_pending_snapshot(trans, fs_info, pending); |
| 1055 | BUG_ON(ret); |
| 1056 | } |
| 1057 | return 0; |
| 1058 | } |
| 1059 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1060 | static void update_super_roots(struct btrfs_root *root) |
| 1061 | { |
| 1062 | struct btrfs_root_item *root_item; |
| 1063 | struct btrfs_super_block *super; |
| 1064 | |
| 1065 | super = &root->fs_info->super_copy; |
| 1066 | |
| 1067 | root_item = &root->fs_info->chunk_root->root_item; |
| 1068 | super->chunk_root = root_item->bytenr; |
| 1069 | super->chunk_root_generation = root_item->generation; |
| 1070 | super->chunk_root_level = root_item->level; |
| 1071 | |
| 1072 | root_item = &root->fs_info->tree_root->root_item; |
| 1073 | super->root = root_item->bytenr; |
| 1074 | super->generation = root_item->generation; |
| 1075 | super->root_level = root_item->level; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 1076 | if (super->cache_generation != 0 || btrfs_test_opt(root, SPACE_CACHE)) |
| 1077 | super->cache_generation = root_item->generation; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1078 | } |
| 1079 | |
Chris Mason | f36f304 | 2009-07-30 10:04:48 -0400 | [diff] [blame] | 1080 | int btrfs_transaction_in_commit(struct btrfs_fs_info *info) |
| 1081 | { |
| 1082 | int ret = 0; |
| 1083 | spin_lock(&info->new_trans_lock); |
| 1084 | if (info->running_transaction) |
| 1085 | ret = info->running_transaction->in_commit; |
| 1086 | spin_unlock(&info->new_trans_lock); |
| 1087 | return ret; |
| 1088 | } |
| 1089 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 1090 | int btrfs_transaction_blocked(struct btrfs_fs_info *info) |
| 1091 | { |
| 1092 | int ret = 0; |
| 1093 | spin_lock(&info->new_trans_lock); |
| 1094 | if (info->running_transaction) |
| 1095 | ret = info->running_transaction->blocked; |
| 1096 | spin_unlock(&info->new_trans_lock); |
| 1097 | return ret; |
| 1098 | } |
| 1099 | |
Sage Weil | bb9c12c | 2010-10-29 15:37:34 -0400 | [diff] [blame] | 1100 | /* |
| 1101 | * wait for the current transaction commit to start and block subsequent |
| 1102 | * transaction joins |
| 1103 | */ |
| 1104 | static void wait_current_trans_commit_start(struct btrfs_root *root, |
| 1105 | struct btrfs_transaction *trans) |
| 1106 | { |
| 1107 | DEFINE_WAIT(wait); |
| 1108 | |
| 1109 | if (trans->in_commit) |
| 1110 | return; |
| 1111 | |
| 1112 | while (1) { |
| 1113 | prepare_to_wait(&root->fs_info->transaction_blocked_wait, &wait, |
| 1114 | TASK_UNINTERRUPTIBLE); |
| 1115 | if (trans->in_commit) { |
| 1116 | finish_wait(&root->fs_info->transaction_blocked_wait, |
| 1117 | &wait); |
| 1118 | break; |
| 1119 | } |
| 1120 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1121 | schedule(); |
| 1122 | mutex_lock(&root->fs_info->trans_mutex); |
| 1123 | finish_wait(&root->fs_info->transaction_blocked_wait, &wait); |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | /* |
| 1128 | * wait for the current transaction to start and then become unblocked. |
| 1129 | * caller holds ref. |
| 1130 | */ |
| 1131 | static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root, |
| 1132 | struct btrfs_transaction *trans) |
| 1133 | { |
| 1134 | DEFINE_WAIT(wait); |
| 1135 | |
| 1136 | if (trans->commit_done || (trans->in_commit && !trans->blocked)) |
| 1137 | return; |
| 1138 | |
| 1139 | while (1) { |
| 1140 | prepare_to_wait(&root->fs_info->transaction_wait, &wait, |
| 1141 | TASK_UNINTERRUPTIBLE); |
| 1142 | if (trans->commit_done || |
| 1143 | (trans->in_commit && !trans->blocked)) { |
| 1144 | finish_wait(&root->fs_info->transaction_wait, |
| 1145 | &wait); |
| 1146 | break; |
| 1147 | } |
| 1148 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1149 | schedule(); |
| 1150 | mutex_lock(&root->fs_info->trans_mutex); |
| 1151 | finish_wait(&root->fs_info->transaction_wait, |
| 1152 | &wait); |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | /* |
| 1157 | * commit transactions asynchronously. once btrfs_commit_transaction_async |
| 1158 | * returns, any subsequent transaction will not be allowed to join. |
| 1159 | */ |
| 1160 | struct btrfs_async_commit { |
| 1161 | struct btrfs_trans_handle *newtrans; |
| 1162 | struct btrfs_root *root; |
| 1163 | struct delayed_work work; |
| 1164 | }; |
| 1165 | |
| 1166 | static void do_async_commit(struct work_struct *work) |
| 1167 | { |
| 1168 | struct btrfs_async_commit *ac = |
| 1169 | container_of(work, struct btrfs_async_commit, work.work); |
| 1170 | |
| 1171 | btrfs_commit_transaction(ac->newtrans, ac->root); |
| 1172 | kfree(ac); |
| 1173 | } |
| 1174 | |
| 1175 | int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, |
| 1176 | struct btrfs_root *root, |
| 1177 | int wait_for_unblock) |
| 1178 | { |
| 1179 | struct btrfs_async_commit *ac; |
| 1180 | struct btrfs_transaction *cur_trans; |
| 1181 | |
| 1182 | ac = kmalloc(sizeof(*ac), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 1183 | if (!ac) |
| 1184 | return -ENOMEM; |
Sage Weil | bb9c12c | 2010-10-29 15:37:34 -0400 | [diff] [blame] | 1185 | |
| 1186 | INIT_DELAYED_WORK(&ac->work, do_async_commit); |
| 1187 | ac->root = root; |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1188 | ac->newtrans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 1189 | if (IS_ERR(ac->newtrans)) { |
| 1190 | int err = PTR_ERR(ac->newtrans); |
| 1191 | kfree(ac); |
| 1192 | return err; |
| 1193 | } |
Sage Weil | bb9c12c | 2010-10-29 15:37:34 -0400 | [diff] [blame] | 1194 | |
| 1195 | /* take transaction reference */ |
| 1196 | mutex_lock(&root->fs_info->trans_mutex); |
| 1197 | cur_trans = trans->transaction; |
Josef Bacik | 13c5a93 | 2011-04-11 15:45:29 -0400 | [diff] [blame] | 1198 | atomic_inc(&cur_trans->use_count); |
Sage Weil | bb9c12c | 2010-10-29 15:37:34 -0400 | [diff] [blame] | 1199 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1200 | |
| 1201 | btrfs_end_transaction(trans, root); |
| 1202 | schedule_delayed_work(&ac->work, 0); |
| 1203 | |
| 1204 | /* wait for transaction to start and unblock */ |
| 1205 | mutex_lock(&root->fs_info->trans_mutex); |
| 1206 | if (wait_for_unblock) |
| 1207 | wait_current_trans_commit_start_and_unblock(root, cur_trans); |
| 1208 | else |
| 1209 | wait_current_trans_commit_start(root, cur_trans); |
| 1210 | put_transaction(cur_trans); |
| 1211 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1212 | |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
| 1216 | /* |
| 1217 | * btrfs_transaction state sequence: |
| 1218 | * in_commit = 0, blocked = 0 (initial) |
| 1219 | * in_commit = 1, blocked = 1 |
| 1220 | * blocked = 0 |
| 1221 | * commit_done = 1 |
| 1222 | */ |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 1223 | int btrfs_commit_transaction(struct btrfs_trans_handle *trans, |
| 1224 | struct btrfs_root *root) |
| 1225 | { |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1226 | unsigned long joined = 0; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 1227 | struct btrfs_transaction *cur_trans; |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 1228 | struct btrfs_transaction *prev_trans = NULL; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 1229 | DEFINE_WAIT(wait); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1230 | int ret; |
Chris Mason | 89573b9 | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1231 | int should_grow = 0; |
| 1232 | unsigned long now = get_seconds(); |
Sage Weil | dccae99 | 2009-04-02 16:59:01 -0400 | [diff] [blame] | 1233 | int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 1234 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 1235 | btrfs_run_ordered_operations(root, 0); |
| 1236 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1237 | /* make a pass through all the delayed refs we have so far |
| 1238 | * any runnings procs may add more while we are here |
| 1239 | */ |
| 1240 | ret = btrfs_run_delayed_refs(trans, root, 0); |
| 1241 | BUG_ON(ret); |
| 1242 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1243 | btrfs_trans_release_metadata(trans, root); |
| 1244 | |
Chris Mason | b7ec40d | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1245 | cur_trans = trans->transaction; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1246 | /* |
| 1247 | * set the flushing flag so procs in this transaction have to |
| 1248 | * start sending their work down. |
| 1249 | */ |
Chris Mason | b7ec40d | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1250 | cur_trans->delayed_refs.flushing = 1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1251 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1252 | ret = btrfs_run_delayed_refs(trans, root, 0); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1253 | BUG_ON(ret); |
| 1254 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 1255 | mutex_lock(&root->fs_info->trans_mutex); |
Chris Mason | b7ec40d | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1256 | if (cur_trans->in_commit) { |
Josef Bacik | 13c5a93 | 2011-04-11 15:45:29 -0400 | [diff] [blame] | 1257 | atomic_inc(&cur_trans->use_count); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1258 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 1259 | btrfs_end_transaction(trans, root); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1260 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 1261 | ret = wait_for_commit(root, cur_trans); |
| 1262 | BUG_ON(ret); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1263 | |
| 1264 | mutex_lock(&root->fs_info->trans_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 1265 | put_transaction(cur_trans); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1266 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1267 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 1268 | return 0; |
| 1269 | } |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 1270 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1271 | trans->transaction->in_commit = 1; |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 1272 | trans->transaction->blocked = 1; |
Sage Weil | bb9c12c | 2010-10-29 15:37:34 -0400 | [diff] [blame] | 1273 | wake_up(&root->fs_info->transaction_blocked_wait); |
| 1274 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1275 | if (cur_trans->list.prev != &root->fs_info->trans_list) { |
| 1276 | prev_trans = list_entry(cur_trans->list.prev, |
| 1277 | struct btrfs_transaction, list); |
| 1278 | if (!prev_trans->commit_done) { |
Josef Bacik | 13c5a93 | 2011-04-11 15:45:29 -0400 | [diff] [blame] | 1279 | atomic_inc(&prev_trans->use_count); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1280 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1281 | |
| 1282 | wait_for_commit(root, prev_trans); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1283 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1284 | mutex_lock(&root->fs_info->trans_mutex); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1285 | put_transaction(prev_trans); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1286 | } |
| 1287 | } |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1288 | |
Chris Mason | 89573b9 | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1289 | if (now < cur_trans->start_time || now - cur_trans->start_time < 1) |
| 1290 | should_grow = 1; |
| 1291 | |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1292 | do { |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1293 | int snap_pending = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1294 | joined = cur_trans->num_joined; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1295 | if (!list_empty(&trans->transaction->pending_snapshots)) |
| 1296 | snap_pending = 1; |
| 1297 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1298 | WARN_ON(cur_trans != trans->transaction); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 1299 | mutex_unlock(&root->fs_info->trans_mutex); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1300 | |
Sage Weil | 0bdb1db | 2010-02-19 14:13:50 -0800 | [diff] [blame] | 1301 | if (flush_on_commit || snap_pending) { |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 1302 | btrfs_start_delalloc_inodes(root, 1); |
| 1303 | ret = btrfs_wait_ordered_extents(root, 0, 1); |
Sage Weil | ebecd3d9 | 2009-07-24 13:17:44 -0400 | [diff] [blame] | 1304 | BUG_ON(ret); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1305 | } |
| 1306 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 1307 | /* |
| 1308 | * rename don't use btrfs_join_transaction, so, once we |
| 1309 | * set the transaction to blocked above, we aren't going |
| 1310 | * to get any new ordered operations. We can safely run |
| 1311 | * it here and no for sure that nothing new will be added |
| 1312 | * to the list |
| 1313 | */ |
| 1314 | btrfs_run_ordered_operations(root, 1); |
| 1315 | |
Chris Mason | ed3b3d314 | 2010-05-25 10:12:41 -0400 | [diff] [blame] | 1316 | prepare_to_wait(&cur_trans->writer_wait, &wait, |
| 1317 | TASK_UNINTERRUPTIBLE); |
| 1318 | |
Chris Mason | 89573b9 | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1319 | smp_mb(); |
Josef Bacik | 13c5a93 | 2011-04-11 15:45:29 -0400 | [diff] [blame] | 1320 | if (atomic_read(&cur_trans->num_writers) > 1) |
Sage Weil | 99d16cbc | 2010-10-29 15:37:34 -0400 | [diff] [blame] | 1321 | schedule_timeout(MAX_SCHEDULE_TIMEOUT); |
| 1322 | else if (should_grow) |
| 1323 | schedule_timeout(1); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1324 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 1325 | mutex_lock(&root->fs_info->trans_mutex); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1326 | finish_wait(&cur_trans->writer_wait, &wait); |
Josef Bacik | 13c5a93 | 2011-04-11 15:45:29 -0400 | [diff] [blame] | 1327 | } while (atomic_read(&cur_trans->num_writers) > 1 || |
Chris Mason | 89573b9 | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1328 | (should_grow && cur_trans->num_joined != joined)); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1329 | |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1330 | ret = create_pending_snapshots(trans, root->fs_info); |
| 1331 | BUG_ON(ret); |
| 1332 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1333 | ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); |
| 1334 | BUG_ON(ret); |
| 1335 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1336 | WARN_ON(cur_trans != trans->transaction); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1337 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1338 | /* btrfs_commit_tree_roots is responsible for getting the |
| 1339 | * various roots consistent with each other. Every pointer |
| 1340 | * in the tree of tree roots has to point to the most up to date |
| 1341 | * root for every subvolume and other tree. So, we have to keep |
| 1342 | * the tree logging code from jumping in and changing any |
| 1343 | * of the trees. |
| 1344 | * |
| 1345 | * At this point in the commit, there can't be any tree-log |
| 1346 | * writers, but a little lower down we drop the trans mutex |
| 1347 | * and let new people in. By holding the tree_log_mutex |
| 1348 | * from now until after the super is written, we avoid races |
| 1349 | * with the tree-log code. |
| 1350 | */ |
| 1351 | mutex_lock(&root->fs_info->tree_log_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 1352 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1353 | ret = commit_fs_roots(trans, root); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1354 | BUG_ON(ret); |
| 1355 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1356 | /* commit_fs_roots gets rid of all the tree log roots, it is now |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1357 | * safe to free the root of tree log roots |
| 1358 | */ |
| 1359 | btrfs_free_log_root_tree(trans, root->fs_info); |
| 1360 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1361 | ret = commit_cowonly_roots(trans, root); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 1362 | BUG_ON(ret); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1363 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 1364 | btrfs_prepare_extent_commit(trans, root); |
| 1365 | |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1366 | cur_trans = root->fs_info->running_transaction; |
Chris Mason | cee36a0 | 2008-01-15 08:40:48 -0500 | [diff] [blame] | 1367 | spin_lock(&root->fs_info->new_trans_lock); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1368 | root->fs_info->running_transaction = NULL; |
Chris Mason | cee36a0 | 2008-01-15 08:40:48 -0500 | [diff] [blame] | 1369 | spin_unlock(&root->fs_info->new_trans_lock); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1370 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1371 | btrfs_set_root_node(&root->fs_info->tree_root->root_item, |
| 1372 | root->fs_info->tree_root->node); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1373 | switch_commit_root(root->fs_info->tree_root); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1374 | |
| 1375 | btrfs_set_root_node(&root->fs_info->chunk_root->root_item, |
| 1376 | root->fs_info->chunk_root->node); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1377 | switch_commit_root(root->fs_info->chunk_root); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1378 | |
| 1379 | update_super_roots(root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1380 | |
| 1381 | if (!root->fs_info->log_root_recovering) { |
| 1382 | btrfs_set_super_log_root(&root->fs_info->super_copy, 0); |
| 1383 | btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0); |
| 1384 | } |
| 1385 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1386 | memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy, |
| 1387 | sizeof(root->fs_info->super_copy)); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1388 | |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 1389 | trans->transaction->blocked = 0; |
Chris Mason | b7ec40d | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1390 | |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 1391 | wake_up(&root->fs_info->transaction_wait); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1392 | |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1393 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 1394 | ret = btrfs_write_and_wait_transaction(trans, root); |
| 1395 | BUG_ON(ret); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 1396 | write_ctree_super(trans, root, 0); |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 1397 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1398 | /* |
| 1399 | * the super is written, we can safely allow the tree-loggers |
| 1400 | * to go about their business |
| 1401 | */ |
| 1402 | mutex_unlock(&root->fs_info->tree_log_mutex); |
| 1403 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 1404 | btrfs_finish_extent_commit(trans, root); |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 1405 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 1406 | mutex_lock(&root->fs_info->trans_mutex); |
| 1407 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1408 | cur_trans->commit_done = 1; |
Chris Mason | b7ec40d | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1409 | |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1410 | root->fs_info->last_trans_committed = cur_trans->transid; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1411 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1412 | wake_up(&cur_trans->commit_wait); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1413 | |
Josef Bacik | 13c5a93 | 2011-04-11 15:45:29 -0400 | [diff] [blame] | 1414 | list_del_init(&cur_trans->list); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 1415 | put_transaction(cur_trans); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1416 | put_transaction(cur_trans); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1417 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 1418 | trace_btrfs_transaction_commit(root); |
| 1419 | |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1420 | mutex_unlock(&root->fs_info->trans_mutex); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1421 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1422 | if (current->journal_info == trans) |
| 1423 | current->journal_info = NULL; |
| 1424 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1425 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 1426 | |
| 1427 | if (current != root->fs_info->transaction_kthread) |
| 1428 | btrfs_run_delayed_iputs(root); |
| 1429 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 1430 | return ret; |
| 1431 | } |
| 1432 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1433 | /* |
| 1434 | * interface function to delete all the snapshots we have scheduled for deletion |
| 1435 | */ |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1436 | int btrfs_clean_old_snapshots(struct btrfs_root *root) |
| 1437 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1438 | LIST_HEAD(list); |
| 1439 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1440 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1441 | mutex_lock(&fs_info->trans_mutex); |
| 1442 | list_splice_init(&fs_info->dead_roots, &list); |
| 1443 | mutex_unlock(&fs_info->trans_mutex); |
| 1444 | |
| 1445 | while (!list_empty(&list)) { |
| 1446 | root = list_entry(list.next, struct btrfs_root, root_list); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1447 | list_del(&root->root_list); |
| 1448 | |
| 1449 | if (btrfs_header_backref_rev(root->node) < |
| 1450 | BTRFS_MIXED_BACKREF_REV) |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1451 | btrfs_drop_snapshot(root, NULL, 0); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1452 | else |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1453 | btrfs_drop_snapshot(root, NULL, 1); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1454 | } |
| 1455 | return 0; |
| 1456 | } |