blob: 46f40564c16885f9a01f8ef08496d82ed42b14a7 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
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 Mason79154b12007-03-22 15:59:16 -040019#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason34088782007-06-12 11:36:58 -040021#include <linux/sched.h>
Chris Masond3c2fdcf2007-09-17 10:58:06 -040022#include <linux/writeback.h>
Chris Mason5f39d392007-10-15 16:14:19 -040023#include <linux/pagemap.h>
Chris Mason5f2cc082008-11-07 18:22:45 -050024#include <linux/blkdev.h>
Chris Mason79154b12007-03-22 15:59:16 -040025#include "ctree.h"
26#include "disk-io.h"
27#include "transaction.h"
Chris Mason925baed2008-06-25 16:01:30 -040028#include "locking.h"
Chris Masone02119d2008-09-05 16:13:11 -040029#include "tree-log.h"
Chris Mason79154b12007-03-22 15:59:16 -040030
Chris Mason0f7d52f2007-04-09 10:42:37 -040031#define BTRFS_ROOT_TRANS_TAG 0
32
Chris Mason80b67942008-02-01 16:35:04 -050033static noinline void put_transaction(struct btrfs_transaction *transaction)
Chris Mason79154b12007-03-22 15:59:16 -040034{
Josef Bacik13c5a932011-04-11 15:45:29 -040035 WARN_ON(atomic_read(&transaction->use_count) == 0);
36 if (atomic_dec_and_test(&transaction->use_count)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -040037 memset(transaction, 0, sizeof(*transaction));
38 kmem_cache_free(btrfs_transaction_cachep, transaction);
Chris Mason78fae272007-03-25 11:35:08 -040039 }
Chris Mason79154b12007-03-22 15:59:16 -040040}
41
Josef Bacik817d52f2009-07-13 21:29:25 -040042static noinline void switch_commit_root(struct btrfs_root *root)
43{
Josef Bacik817d52f2009-07-13 21:29:25 -040044 free_extent_buffer(root->commit_root);
45 root->commit_root = btrfs_root_node(root);
Josef Bacik817d52f2009-07-13 21:29:25 -040046}
47
Chris Masond352ac62008-09-29 15:18:18 -040048/*
49 * either allocate a new transaction or hop into the existing one
50 */
Chris Mason80b67942008-02-01 16:35:04 -050051static noinline int join_transaction(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -040052{
53 struct btrfs_transaction *cur_trans;
54 cur_trans = root->fs_info->running_transaction;
55 if (!cur_trans) {
Chris Mason2c90e5d2007-04-02 10:50:19 -040056 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
57 GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +000058 if (!cur_trans)
59 return -ENOMEM;
Chris Mason0f7d52f2007-04-09 10:42:37 -040060 root->fs_info->generation++;
Josef Bacik13c5a932011-04-11 15:45:29 -040061 atomic_set(&cur_trans->num_writers, 1);
Josef Bacik15ee9bc2007-08-10 16:22:09 -040062 cur_trans->num_joined = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040063 cur_trans->transid = root->fs_info->generation;
Chris Mason79154b12007-03-22 15:59:16 -040064 init_waitqueue_head(&cur_trans->writer_wait);
65 init_waitqueue_head(&cur_trans->commit_wait);
66 cur_trans->in_commit = 0;
Chris Masonf9295742008-07-17 12:54:14 -040067 cur_trans->blocked = 0;
Josef Bacik13c5a932011-04-11 15:45:29 -040068 atomic_set(&cur_trans->use_count, 1);
Chris Mason79154b12007-03-22 15:59:16 -040069 cur_trans->commit_done = 0;
Chris Mason08607c12007-06-08 15:33:54 -040070 cur_trans->start_time = get_seconds();
Chris Mason56bec292009-03-13 10:10:06 -040071
Eric Paris6bef4d32010-02-23 19:43:04 +000072 cur_trans->delayed_refs.root = RB_ROOT;
Chris Mason56bec292009-03-13 10:10:06 -040073 cur_trans->delayed_refs.num_entries = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -040074 cur_trans->delayed_refs.num_heads_ready = 0;
75 cur_trans->delayed_refs.num_heads = 0;
Chris Mason56bec292009-03-13 10:10:06 -040076 cur_trans->delayed_refs.flushing = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -040077 cur_trans->delayed_refs.run_delayed_start = 0;
Chris Mason56bec292009-03-13 10:10:06 -040078 spin_lock_init(&cur_trans->delayed_refs.lock);
79
Chris Mason3063d292008-01-08 15:46:30 -050080 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
Chris Mason8fd17792007-04-19 21:01:03 -040081 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
Chris Masond1310b22008-01-24 16:13:08 -050082 extent_io_tree_init(&cur_trans->dirty_pages,
Chris Mason5f39d392007-10-15 16:14:19 -040083 root->fs_info->btree_inode->i_mapping,
84 GFP_NOFS);
Chris Mason48ec2cf2008-06-09 09:35:50 -040085 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 Bacik15ee9bc2007-08-10 16:22:09 -040088 } else {
Josef Bacik13c5a932011-04-11 15:45:29 -040089 atomic_inc(&cur_trans->num_writers);
Josef Bacik15ee9bc2007-08-10 16:22:09 -040090 cur_trans->num_joined++;
Chris Mason79154b12007-03-22 15:59:16 -040091 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -040092
Chris Mason79154b12007-03-22 15:59:16 -040093 return 0;
94}
95
Chris Masond352ac62008-09-29 15:18:18 -040096/*
Chris Masond3977122009-01-05 21:25:51 -050097 * 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 Masond352ac62008-09-29 15:18:18 -0400101 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400102static noinline int record_root_in_trans(struct btrfs_trans_handle *trans,
103 struct btrfs_root *root)
Chris Mason6702ed42007-08-07 16:15:09 -0400104{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400105 if (root->ref_cows && root->last_trans < trans->transid) {
Chris Mason6702ed42007-08-07 16:15:09 -0400106 WARN_ON(root == root->fs_info->extent_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400107 WARN_ON(root->commit_root != root->node);
Yan Zheng31153d82008-07-28 15:32:19 -0400108
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400109 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 Mason6702ed42007-08-07 16:15:09 -0400114 }
115 return 0;
116}
117
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400118int 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 Masond352ac62008-09-29 15:18:18 -0400135/* 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 Mason37d1aee2008-07-31 10:48:37 -0400139static void wait_current_trans(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -0400140{
Chris Masonf9295742008-07-17 12:54:14 -0400141 struct btrfs_transaction *cur_trans;
Chris Mason79154b12007-03-22 15:59:16 -0400142
Chris Masonf9295742008-07-17 12:54:14 -0400143 cur_trans = root->fs_info->running_transaction;
Chris Mason37d1aee2008-07-31 10:48:37 -0400144 if (cur_trans && cur_trans->blocked) {
Chris Masonf9295742008-07-17 12:54:14 -0400145 DEFINE_WAIT(wait);
Josef Bacik13c5a932011-04-11 15:45:29 -0400146 atomic_inc(&cur_trans->use_count);
Chris Masond3977122009-01-05 21:25:51 -0500147 while (1) {
Chris Masonf9295742008-07-17 12:54:14 -0400148 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
149 TASK_UNINTERRUPTIBLE);
Zhao Lei471fa172010-03-25 12:35:14 +0000150 if (!cur_trans->blocked)
Chris Masonf9295742008-07-17 12:54:14 -0400151 break;
Zhao Lei471fa172010-03-25 12:35:14 +0000152 mutex_unlock(&root->fs_info->trans_mutex);
153 schedule();
154 mutex_lock(&root->fs_info->trans_mutex);
Chris Masonf9295742008-07-17 12:54:14 -0400155 }
Zhao Lei471fa172010-03-25 12:35:14 +0000156 finish_wait(&root->fs_info->transaction_wait, &wait);
Chris Masonf9295742008-07-17 12:54:14 -0400157 put_transaction(cur_trans);
158 }
Chris Mason37d1aee2008-07-31 10:48:37 -0400159}
160
Josef Bacik249ac1e2009-11-10 21:23:48 -0500161enum btrfs_trans_type {
162 TRANS_START,
163 TRANS_JOIN,
164 TRANS_USERSPACE,
Josef Bacik0af3d002010-06-21 14:48:16 -0400165 TRANS_JOIN_NOLOCK,
Josef Bacik249ac1e2009-11-10 21:23:48 -0500166};
167
Yan, Zhenga22285a2010-05-16 10:48:46 -0400168static int may_wait_transaction(struct btrfs_root *root, int type)
Chris Mason37d1aee2008-07-31 10:48:37 -0400169{
Chris Mason4bef0842008-09-08 11:18:08 -0400170 if (!root->fs_info->log_root_recovering &&
Josef Bacik249ac1e2009-11-10 21:23:48 -0500171 ((type == TRANS_START && !root->fs_info->open_ioctl_trans) ||
172 type == TRANS_USERSPACE))
Yan, Zhenga22285a2010-05-16 10:48:46 -0400173 return 1;
174 return 0;
175}
176
177static 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 Bacik06d5a582011-04-05 11:57:27 -0400182 int retries = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400183 int ret;
liuboacce9522011-01-06 19:30:25 +0800184
185 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
186 return ERR_PTR(-EROFS);
Josef Bacik2a1eb462011-04-13 15:15:59 -0400187
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, Zhenga22285a2010-05-16 10:48:46 -0400196again:
197 h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
198 if (!h)
199 return ERR_PTR(-ENOMEM);
200
Josef Bacik0af3d002010-06-21 14:48:16 -0400201 if (type != TRANS_JOIN_NOLOCK)
202 mutex_lock(&root->fs_info->trans_mutex);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400203 if (may_wait_transaction(root, type))
Chris Mason37d1aee2008-07-31 10:48:37 -0400204 wait_current_trans(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400205
Chris Mason79154b12007-03-22 15:59:16 -0400206 ret = join_transaction(root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +0000207 if (ret < 0) {
Yoshinori Sano6e8df2a2011-04-03 12:31:28 +0000208 kmem_cache_free(btrfs_trans_handle_cachep, h);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +0000209 if (type != TRANS_JOIN_NOLOCK)
210 mutex_unlock(&root->fs_info->trans_mutex);
211 return ERR_PTR(ret);
212 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400213
Yan, Zhenga22285a2010-05-16 10:48:46 -0400214 cur_trans = root->fs_info->running_transaction;
Josef Bacik13c5a932011-04-11 15:45:29 -0400215 atomic_inc(&cur_trans->use_count);
Josef Bacik0af3d002010-06-21 14:48:16 -0400216 if (type != TRANS_JOIN_NOLOCK)
217 mutex_unlock(&root->fs_info->trans_mutex);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400218
219 h->transid = cur_trans->transid;
220 h->transaction = cur_trans;
Chris Mason79154b12007-03-22 15:59:16 -0400221 h->blocks_used = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500222 h->block_group = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400223 h->bytes_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400224 h->delayed_ref_updates = 0;
Josef Bacik2a1eb462011-04-13 15:15:59 -0400225 h->use_count = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400226 h->block_rsv = NULL;
Josef Bacik2a1eb462011-04-13 15:15:59 -0400227 h->orig_rsv = NULL;
Chris Masonb7ec40d2009-03-12 20:12:45 -0400228
Yan, Zhenga22285a2010-05-16 10:48:46 -0400229 smp_mb();
230 if (cur_trans->blocked && may_wait_transaction(root, type)) {
231 btrfs_commit_transaction(h, root);
232 goto again;
233 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400234
Yan, Zhenga22285a2010-05-16 10:48:46 -0400235 if (num_items > 0) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -0400236 ret = btrfs_trans_reserve_metadata(h, root, num_items);
Josef Bacik06d5a582011-04-05 11:57:27 -0400237 if (ret == -EAGAIN && !retries) {
238 retries++;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400239 btrfs_commit_transaction(h, root);
240 goto again;
Josef Bacik06d5a582011-04-05 11:57:27 -0400241 } 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, Zhenga22285a2010-05-16 10:48:46 -0400247 }
Josef Bacik06d5a582011-04-05 11:57:27 -0400248
Yan, Zhenga22285a2010-05-16 10:48:46 -0400249 if (ret < 0) {
250 btrfs_end_transaction(h, root);
251 return ERR_PTR(ret);
252 }
253 }
254
Josef Bacik2a1eb462011-04-13 15:15:59 -0400255got_it:
Josef Bacik0af3d002010-06-21 14:48:16 -0400256 if (type != TRANS_JOIN_NOLOCK)
257 mutex_lock(&root->fs_info->trans_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400258 record_root_in_trans(h, root);
Josef Bacik0af3d002010-06-21 14:48:16 -0400259 if (type != TRANS_JOIN_NOLOCK)
260 mutex_unlock(&root->fs_info->trans_mutex);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400261
262 if (!current->journal_info && type != TRANS_USERSPACE)
263 current->journal_info = h;
Chris Mason79154b12007-03-22 15:59:16 -0400264 return h;
265}
266
Chris Masonf9295742008-07-17 12:54:14 -0400267struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
Yan, Zhenga22285a2010-05-16 10:48:46 -0400268 int num_items)
Chris Masonf9295742008-07-17 12:54:14 -0400269{
Yan, Zhenga22285a2010-05-16 10:48:46 -0400270 return start_transaction(root, num_items, TRANS_START);
Chris Masonf9295742008-07-17 12:54:14 -0400271}
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400272struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
Chris Masonf9295742008-07-17 12:54:14 -0400273{
Yan, Zhenga22285a2010-05-16 10:48:46 -0400274 return start_transaction(root, 0, TRANS_JOIN);
Chris Masonf9295742008-07-17 12:54:14 -0400275}
276
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400277struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
Josef Bacik0af3d002010-06-21 14:48:16 -0400278{
279 return start_transaction(root, 0, TRANS_JOIN_NOLOCK);
280}
281
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400282struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
Sage Weil9ca9ee02008-08-04 10:41:27 -0400283{
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400284 return start_transaction(root, 0, TRANS_USERSPACE);
Sage Weil9ca9ee02008-08-04 10:41:27 -0400285}
286
Chris Masond352ac62008-09-29 15:18:18 -0400287/* wait for a transaction commit to be fully complete */
Chris Mason89ce8a62008-06-25 16:01:31 -0400288static 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 Masond3977122009-01-05 21:25:51 -0500293 while (!commit->commit_done) {
Chris Mason89ce8a62008-06-25 16:01:31 -0400294 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 Weil46204592010-10-29 15:41:32 -0400307int 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 Bacik13c5a932011-04-11 15:45:29 -0400346 atomic_inc(&cur_trans->use_count);
Sage Weil46204592010-10-29 15:41:32 -0400347 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;
354out_unlock:
355 mutex_unlock(&root->fs_info->trans_mutex);
356 return ret;
357}
358
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400359#if 0
Chris Masond352ac62008-09-29 15:18:18 -0400360/*
Chris Masond3977122009-01-05 21:25:51 -0500361 * 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 Masond352ac62008-09-29 15:18:18 -0400363 */
Chris Mason37d1aee2008-07-31 10:48:37 -0400364static void throttle_on_drops(struct btrfs_root *root)
Chris Masonab78c842008-07-29 16:15:18 -0400365{
366 struct btrfs_fs_info *info = root->fs_info;
Chris Mason2dd3e672008-08-04 08:20:15 -0400367 int harder_count = 0;
Chris Masonab78c842008-07-29 16:15:18 -0400368
Chris Mason2dd3e672008-08-04 08:20:15 -0400369harder:
Chris Masonab78c842008-07-29 16:15:18 -0400370 if (atomic_read(&info->throttles)) {
371 DEFINE_WAIT(wait);
372 int thr;
Chris Masonab78c842008-07-29 16:15:18 -0400373 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 Mason2dd3e672008-08-04 08:20:15 -0400385 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 Masonab78c842008-07-29 16:15:18 -0400398 }
399}
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400400#endif
Chris Masonab78c842008-07-29 16:15:18 -0400401
Chris Mason37d1aee2008-07-31 10:48:37 -0400402void btrfs_throttle(struct btrfs_root *root)
403{
404 mutex_lock(&root->fs_info->trans_mutex);
Sage Weil9ca9ee02008-08-04 10:41:27 -0400405 if (!root->fs_info->open_ioctl_trans)
406 wait_current_trans(root);
Chris Mason37d1aee2008-07-31 10:48:37 -0400407 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason37d1aee2008-07-31 10:48:37 -0400408}
409
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400410static 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
419int 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 Mason89ce8a62008-06-25 16:01:31 -0400436static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
Josef Bacik0af3d002010-06-21 14:48:16 -0400437 struct btrfs_root *root, int throttle, int lock)
Chris Mason79154b12007-03-22 15:59:16 -0400438{
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400439 struct btrfs_transaction *cur_trans = trans->transaction;
Chris Masonab78c842008-07-29 16:15:18 -0400440 struct btrfs_fs_info *info = root->fs_info;
Chris Masonc3e69d52009-03-13 10:17:05 -0400441 int count = 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400442
Josef Bacik2a1eb462011-04-13 15:15:59 -0400443 if (--trans->use_count) {
444 trans->block_rsv = trans->orig_rsv;
445 return 0;
446 }
447
Chris Masonc3e69d52009-03-13 10:17:05 -0400448 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 Masonb7ec40d2009-03-12 20:12:45 -0400454
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 Masonc3e69d52009-03-13 10:17:05 -0400461 btrfs_run_delayed_refs(trans, root, cur);
462 } else {
463 break;
464 }
465 count++;
Chris Mason56bec292009-03-13 10:10:06 -0400466 }
467
Yan, Zhenga22285a2010-05-16 10:48:46 -0400468 btrfs_trans_release_metadata(trans, root);
469
Josef Bacik0af3d002010-06-21 14:48:16 -0400470 if (lock && !root->fs_info->open_ioctl_trans &&
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400471 should_end_transaction(trans, root))
472 trans->transaction->blocked = 1;
473
Josef Bacik0af3d002010-06-21 14:48:16 -0400474 if (lock && cur_trans->blocked && !cur_trans->in_commit) {
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400475 if (throttle)
476 return btrfs_commit_transaction(trans, root);
477 else
478 wake_up_process(info->transaction_kthread);
479 }
480
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400481 WARN_ON(cur_trans != info->running_transaction);
Josef Bacik13c5a932011-04-11 15:45:29 -0400482 WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
483 atomic_dec(&cur_trans->num_writers);
Chris Mason89ce8a62008-06-25 16:01:31 -0400484
Sage Weil99d16cbc2010-10-29 15:37:34 -0400485 smp_mb();
Chris Mason79154b12007-03-22 15:59:16 -0400486 if (waitqueue_active(&cur_trans->writer_wait))
487 wake_up(&cur_trans->writer_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400488 put_transaction(cur_trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400489
490 if (current->journal_info == trans)
491 current->journal_info = NULL;
Chris Masond6025572007-03-30 14:27:56 -0400492 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400493 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Masonab78c842008-07-29 16:15:18 -0400494
Yan, Zheng24bbcf02009-11-12 09:36:34 +0000495 if (throttle)
496 btrfs_run_delayed_iputs(root);
497
Chris Mason79154b12007-03-22 15:59:16 -0400498 return 0;
499}
500
Chris Mason89ce8a62008-06-25 16:01:31 -0400501int btrfs_end_transaction(struct btrfs_trans_handle *trans,
502 struct btrfs_root *root)
503{
Josef Bacik0af3d002010-06-21 14:48:16 -0400504 return __btrfs_end_transaction(trans, root, 0, 1);
Chris Mason89ce8a62008-06-25 16:01:31 -0400505}
506
507int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
508 struct btrfs_root *root)
509{
Josef Bacik0af3d002010-06-21 14:48:16 -0400510 return __btrfs_end_transaction(trans, root, 1, 1);
511}
512
513int 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 Mason89ce8a62008-06-25 16:01:31 -0400517}
518
Chris Masond352ac62008-09-29 15:18:18 -0400519/*
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 Mason690587d2009-10-13 13:29:19 -0400522 * those extents are sent to disk but does not wait on them
Chris Masond352ac62008-09-29 15:18:18 -0400523 */
Chris Mason690587d2009-10-13 13:29:19 -0400524int btrfs_write_marked_extents(struct btrfs_root *root,
Yan, Zheng8cef4e12009-11-12 09:33:26 +0000525 struct extent_io_tree *dirty_pages, int mark)
Chris Mason79154b12007-03-22 15:59:16 -0400526{
Chris Mason7c4452b2007-04-28 09:29:35 -0400527 int ret;
Chris Mason777e6bd2008-08-15 15:34:15 -0400528 int err = 0;
Chris Mason7c4452b2007-04-28 09:29:35 -0400529 int werr = 0;
530 struct page *page;
Chris Mason7c4452b2007-04-28 09:29:35 -0400531 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason777e6bd2008-08-15 15:34:15 -0400532 u64 start = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400533 u64 end;
534 unsigned long index;
Chris Mason7c4452b2007-04-28 09:29:35 -0400535
Chris Masond3977122009-01-05 21:25:51 -0500536 while (1) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400537 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
Yan, Zheng8cef4e12009-11-12 09:33:26 +0000538 mark);
Chris Mason5f39d392007-10-15 16:14:19 -0400539 if (ret)
Chris Mason7c4452b2007-04-28 09:29:35 -0400540 break;
Chris Masond3977122009-01-05 21:25:51 -0500541 while (start <= end) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400542 cond_resched();
543
Chris Mason5f39d392007-10-15 16:14:19 -0400544 index = start >> PAGE_CACHE_SHIFT;
Chris Mason35ebb932007-10-30 16:56:53 -0400545 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
Chris Mason4bef0842008-09-08 11:18:08 -0400546 page = find_get_page(btree_inode->i_mapping, index);
Chris Mason7c4452b2007-04-28 09:29:35 -0400547 if (!page)
548 continue;
Chris Mason4bef0842008-09-08 11:18:08 -0400549
550 btree_lock_page_hook(page);
551 if (!page->mapping) {
552 unlock_page(page);
553 page_cache_release(page);
554 continue;
555 }
556
Chris Mason6702ed42007-08-07 16:15:09 -0400557 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 Mason7c4452b2007-04-28 09:29:35 -0400566 err = write_one_page(page, 0);
567 if (err)
568 werr = err;
569 page_cache_release(page);
570 }
571 }
Chris Mason690587d2009-10-13 13:29:19 -0400572 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 */
583int btrfs_wait_marked_extents(struct btrfs_root *root,
Yan, Zheng8cef4e12009-11-12 09:33:26 +0000584 struct extent_io_tree *dirty_pages, int mark)
Chris Mason690587d2009-10-13 13:29:19 -0400585{
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 Masond3977122009-01-05 21:25:51 -0500595 while (1) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +0000596 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
597 mark);
Chris Mason777e6bd2008-08-15 15:34:15 -0400598 if (ret)
599 break;
600
Yan, Zheng8cef4e12009-11-12 09:33:26 +0000601 clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500602 while (start <= end) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400603 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 Mason4bef0842008-09-08 11:18:08 -0400609 btree_lock_page_hook(page);
610 wait_on_page_writeback(page);
Chris Mason777e6bd2008-08-15 15:34:15 -0400611 err = write_one_page(page, 0);
612 if (err)
613 werr = err;
614 }
Chris Mason105d9312008-11-18 12:13:12 -0500615 wait_on_page_writeback(page);
Chris Mason777e6bd2008-08-15 15:34:15 -0400616 page_cache_release(page);
617 cond_resched();
618 }
619 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400620 if (err)
621 werr = err;
622 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400623}
624
Chris Mason690587d2009-10-13 13:29:19 -0400625/*
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 */
630int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
Yan, Zheng8cef4e12009-11-12 09:33:26 +0000631 struct extent_io_tree *dirty_pages, int mark)
Chris Mason690587d2009-10-13 13:29:19 -0400632{
633 int ret;
634 int ret2;
635
Yan, Zheng8cef4e12009-11-12 09:33:26 +0000636 ret = btrfs_write_marked_extents(root, dirty_pages, mark);
637 ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
Chris Mason690587d2009-10-13 13:29:19 -0400638 return ret || ret2;
639}
640
Chris Masond0c803c2008-09-11 16:17:57 -0400641int 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, Zheng8cef4e12009-11-12 09:33:26 +0000650 &trans->transaction->dirty_pages,
651 EXTENT_DIRTY);
Chris Masond0c803c2008-09-11 16:17:57 -0400652}
653
Chris Masond352ac62008-09-29 15:18:18 -0400654/*
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 Mason0b86a832008-03-24 15:01:56 -0400664static int update_cowonly_root(struct btrfs_trans_handle *trans,
665 struct btrfs_root *root)
666{
667 int ret;
668 u64 old_root_bytenr;
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000669 u64 old_root_used;
Chris Mason0b86a832008-03-24 15:01:56 -0400670 struct btrfs_root *tree_root = root->fs_info->tree_root;
671
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000672 old_root_used = btrfs_root_used(&root->root_item);
Chris Mason0b86a832008-03-24 15:01:56 -0400673 btrfs_write_dirty_block_groups(trans, root);
Chris Mason56bec292009-03-13 10:10:06 -0400674
Chris Masond3977122009-01-05 21:25:51 -0500675 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -0400676 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000677 if (old_root_bytenr == root->node->start &&
678 old_root_used == btrfs_root_used(&root->root_item))
Chris Mason0b86a832008-03-24 15:01:56 -0400679 break;
Chris Mason87ef2bb2008-10-30 11:23:27 -0400680
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400681 btrfs_set_root_node(&root->root_item, root->node);
Chris Mason0b86a832008-03-24 15:01:56 -0400682 ret = btrfs_update_root(trans, tree_root,
683 &root->root_key,
684 &root->root_item);
685 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -0400686
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000687 old_root_used = btrfs_root_used(&root->root_item);
Yan Zheng4a8c9a62009-07-22 10:07:05 -0400688 ret = btrfs_write_dirty_block_groups(trans, root);
Chris Mason56bec292009-03-13 10:10:06 -0400689 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -0400690 }
Yan Zheng276e6802009-07-30 09:40:40 -0400691
692 if (root != root->fs_info->extent_root)
693 switch_commit_root(root);
694
Chris Mason0b86a832008-03-24 15:01:56 -0400695 return 0;
696}
697
Chris Masond352ac62008-09-29 15:18:18 -0400698/*
699 * update all the cowonly tree roots on disk
700 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400701static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
702 struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -0400703{
Chris Mason79154b12007-03-22 15:59:16 -0400704 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400705 struct list_head *next;
Yan Zheng84234f32008-10-29 14:49:05 -0400706 struct extent_buffer *eb;
Chris Mason56bec292009-03-13 10:10:06 -0400707 int ret;
Yan Zheng84234f32008-10-29 14:49:05 -0400708
Chris Mason56bec292009-03-13 10:10:06 -0400709 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
710 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400711
Yan Zheng84234f32008-10-29 14:49:05 -0400712 eb = btrfs_lock_root_node(fs_info->tree_root);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400713 btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
Yan Zheng84234f32008-10-29 14:49:05 -0400714 btrfs_tree_unlock(eb);
715 free_extent_buffer(eb);
Chris Mason79154b12007-03-22 15:59:16 -0400716
Chris Mason56bec292009-03-13 10:10:06 -0400717 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
718 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400719
Chris Masond3977122009-01-05 21:25:51 -0500720 while (!list_empty(&fs_info->dirty_cowonly_roots)) {
Chris Mason0b86a832008-03-24 15:01:56 -0400721 next = fs_info->dirty_cowonly_roots.next;
722 list_del_init(next);
723 root = list_entry(next, struct btrfs_root, dirty_list);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400724
Chris Mason0b86a832008-03-24 15:01:56 -0400725 update_cowonly_root(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -0400726 }
Yan Zheng276e6802009-07-30 09:40:40 -0400727
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 Mason79154b12007-03-22 15:59:16 -0400732 return 0;
733}
734
Chris Masond352ac62008-09-29 15:18:18 -0400735/*
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 Zheng5d4f98a2009-06-10 10:45:14 -0400740int btrfs_add_dead_root(struct btrfs_root *root)
Chris Mason5eda7b52007-06-22 14:16:25 -0400741{
Yan Zhengb48652c2008-08-04 23:23:47 -0400742 mutex_lock(&root->fs_info->trans_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400743 list_add(&root->root_list, &root->fs_info->dead_roots);
Yan Zhengb48652c2008-08-04 23:23:47 -0400744 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason5eda7b52007-06-22 14:16:25 -0400745 return 0;
746}
747
Chris Masond352ac62008-09-29 15:18:18 -0400748/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400749 * update all the cowonly tree roots on disk
Chris Masond352ac62008-09-29 15:18:18 -0400750 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400751static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
752 struct btrfs_root *root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400753{
Chris Mason0f7d52f2007-04-09 10:42:37 -0400754 struct btrfs_root *gang[8];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400755 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400756 int i;
757 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400758 int err = 0;
759
Chris Masond3977122009-01-05 21:25:51 -0500760 while (1) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400761 ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
762 (void **)gang, 0,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400763 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 Zheng5d4f98a2009-06-10 10:45:14 -0400769 radix_tree_tag_clear(&fs_info->fs_roots_radix,
770 (unsigned long)root->root_key.objectid,
771 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400772
Chris Masone02119d2008-09-05 16:13:11 -0400773 btrfs_free_log(trans, root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400774 btrfs_update_reloc_root(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -0400775 btrfs_orphan_commit_root(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -0400776
Yan Zheng978d9102009-06-15 20:01:02 -0400777 if (root->commit_root != root->node) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400778 switch_commit_root(root);
Yan Zheng978d9102009-06-15 20:01:02 -0400779 btrfs_set_root_node(&root->root_item,
780 root->node);
781 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400782
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400783 err = btrfs_update_root(trans, fs_info->tree_root,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400784 &root->root_key,
785 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400786 if (err)
787 break;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400788 }
789 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400790 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400791}
792
Chris Masond352ac62008-09-29 15:18:18 -0400793/*
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 Masone9d0b132007-08-10 14:06:19 -0400797int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
798{
799 struct btrfs_fs_info *info = root->fs_info;
Chris Masone9d0b132007-08-10 14:06:19 -0400800 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400801 int ret;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400802 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400803
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400804 if (xchg(&root->defrag_running, 1))
Chris Masone9d0b132007-08-10 14:06:19 -0400805 return 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400806
Chris Mason6b800532007-10-15 16:17:34 -0400807 while (1) {
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400808 trans = btrfs_start_transaction(root, 0);
809 if (IS_ERR(trans))
810 return PTR_ERR(trans);
811
Chris Masone9d0b132007-08-10 14:06:19 -0400812 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400813
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400814 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400815 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400816 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400817 cond_resched();
818
Chris Mason3f157a22008-06-25 16:01:31 -0400819 if (root->fs_info->closing || ret != -EAGAIN)
Chris Masone9d0b132007-08-10 14:06:19 -0400820 break;
821 }
822 root->defrag_running = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400823 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -0400824}
825
Yan Zheng2c47e6052009-06-27 21:07:35 -0400826#if 0
Chris Masond352ac62008-09-29 15:18:18 -0400827/*
Chris Masonb7ec40d2009-03-12 20:12:45 -0400828 * 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 */
834static 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 Mason59bc5c72009-04-24 14:39:25 -0400844
Chris Masonb7ec40d2009-03-12 20:12:45 -0400845 schedule();
Chris Mason59bc5c72009-04-24 14:39:25 -0400846
Chris Masonb7ec40d2009-03-12 20:12:45 -0400847 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 Masond352ac62008-09-29 15:18:18 -0400855 * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
856 * all of them
857 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400858int btrfs_drop_dead_root(struct btrfs_root *root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400859{
Chris Mason0f7d52f2007-04-09 10:42:37 -0400860 struct btrfs_trans_handle *trans;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400861 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400862 unsigned long nr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400863 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -0400864
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400865 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 Bacik58176a92007-08-29 15:47:34 -0400872
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400873 /*
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 Bacik58176a92007-08-29 15:47:34 -0400880 }
Chris Masona2135012008-06-25 16:01:30 -0400881
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400882 ret = btrfs_drop_snapshot(trans, root);
883 if (ret != -EAGAIN)
Chris Mason54aa1f42007-06-22 14:16:25 -0400884 break;
Chris Masona2135012008-06-25 16:01:30 -0400885
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400886 ret = btrfs_update_root(trans, tree_root,
887 &root->root_key,
888 &root->root_item);
889 if (ret)
890 break;
Yanbcc63ab2008-07-30 16:29:20 -0400891
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400892 nr = trans->blocks_used;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400893 ret = btrfs_end_transaction(trans, tree_root);
894 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400895
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400896 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400897 cond_resched();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400898 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400899 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 Mason54aa1f42007-06-22 14:16:25 -0400913 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400914}
Yan Zheng2c47e6052009-06-27 21:07:35 -0400915#endif
Chris Mason0f7d52f2007-04-09 10:42:37 -0400916
Chris Masond352ac62008-09-29 15:18:18 -0400917/*
918 * new snapshots need to be created at a very specific time in the
919 * transaction commit. This does the actual creation
920 */
Chris Mason80b67942008-02-01 16:35:04 -0500921static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
Chris Mason3063d292008-01-08 15:46:30 -0500922 struct btrfs_fs_info *fs_info,
923 struct btrfs_pending_snapshot *pending)
924{
925 struct btrfs_key key;
Chris Mason80b67942008-02-01 16:35:04 -0500926 struct btrfs_root_item *new_root_item;
Chris Mason3063d292008-01-08 15:46:30 -0500927 struct btrfs_root *tree_root = fs_info->tree_root;
928 struct btrfs_root *root = pending->root;
Sage Weil6bdb72d2010-03-15 17:27:13 +0000929 struct btrfs_root *parent_root;
930 struct inode *parent_inode;
Josef Bacik6a912212010-11-20 09:48:00 +0000931 struct dentry *parent;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400932 struct dentry *dentry;
Chris Mason3063d292008-01-08 15:46:30 -0500933 struct extent_buffer *tmp;
Chris Mason925baed2008-06-25 16:01:30 -0400934 struct extent_buffer *old;
Chris Mason3063d292008-01-08 15:46:30 -0500935 int ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -0400936 u64 to_reserve = 0;
Sage Weil6bdb72d2010-03-15 17:27:13 +0000937 u64 index = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400938 u64 objectid;
Li Zefanb83cc962010-12-20 16:04:08 +0800939 u64 root_flags;
Chris Mason3063d292008-01-08 15:46:30 -0500940
Chris Mason80b67942008-02-01 16:35:04 -0500941 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
942 if (!new_root_item) {
Yan, Zhenga22285a2010-05-16 10:48:46 -0400943 pending->error = -ENOMEM;
Chris Mason80b67942008-02-01 16:35:04 -0500944 goto fail;
945 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400946
Chris Mason3063d292008-01-08 15:46:30 -0500947 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400948 if (ret) {
949 pending->error = ret;
Chris Mason3063d292008-01-08 15:46:30 -0500950 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400951 }
Chris Mason3063d292008-01-08 15:46:30 -0500952
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400953 btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
Yan, Zhengd68fc572010-05-16 10:49:58 -0400954 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 Bacik8bb8ab22010-10-15 16:52:49 -0400958 to_reserve);
Yan, Zhengd68fc572010-05-16 10:49:58 -0400959 if (ret) {
960 pending->error = ret;
961 goto fail;
962 }
963 }
964
Chris Mason3063d292008-01-08 15:46:30 -0500965 key.objectid = objectid;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400966 key.offset = (u64)-1;
967 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Mason3063d292008-01-08 15:46:30 -0500968
Yan, Zhenga22285a2010-05-16 10:48:46 -0400969 trans->block_rsv = &pending->block_rsv;
Sage Weil6bdb72d2010-03-15 17:27:13 +0000970
Yan, Zhenga22285a2010-05-16 10:48:46 -0400971 dentry = pending->dentry;
Josef Bacik6a912212010-11-20 09:48:00 +0000972 parent = dget_parent(dentry);
973 parent_inode = parent->d_inode;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400974 parent_root = BTRFS_I(parent_inode)->root;
Sage Weil6bdb72d2010-03-15 17:27:13 +0000975 record_root_in_trans(trans, parent_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400976
Sage Weil6bdb72d2010-03-15 17:27:13 +0000977 /*
978 * insert the directory item
979 */
Sage Weil6bdb72d2010-03-15 17:27:13 +0000980 ret = btrfs_set_inode_index(parent_inode, &index);
981 BUG_ON(ret);
982 ret = btrfs_insert_dir_item(trans, parent_root,
Yan, Zhenga22285a2010-05-16 10:48:46 -0400983 dentry->d_name.name, dentry->d_name.len,
984 parent_inode->i_ino, &key,
985 BTRFS_FT_DIR, index);
Sage Weil6bdb72d2010-03-15 17:27:13 +0000986 BUG_ON(ret);
987
Yan, Zhenga22285a2010-05-16 10:48:46 -0400988 btrfs_i_size_write(parent_inode, parent_inode->i_size +
989 dentry->d_name.len * 2);
Sage Weil6bdb72d2010-03-15 17:27:13 +0000990 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 Zefan08fe4db2011-03-28 02:01:25 +0000996 btrfs_check_and_init_root_item(new_root_item);
Sage Weil6bdb72d2010-03-15 17:27:13 +0000997
Li Zefanb83cc962010-12-20 16:04:08 +0800998 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 Mason925baed2008-06-25 16:01:30 -04001005 old = btrfs_lock_root_node(root);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001006 btrfs_cow_block(trans, root, old, NULL, 0, &old);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001007 btrfs_set_lock_blocking(old);
Chris Mason3063d292008-01-08 15:46:30 -05001008
Chris Mason925baed2008-06-25 16:01:30 -04001009 btrfs_copy_root(trans, root, old, &tmp, objectid);
1010 btrfs_tree_unlock(old);
1011 free_extent_buffer(old);
Chris Mason3063d292008-01-08 15:46:30 -05001012
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001013 btrfs_set_root_node(new_root_item, tmp);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001014 /* 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 Mason925baed2008-06-25 16:01:30 -04001017 btrfs_tree_unlock(tmp);
Chris Mason3063d292008-01-08 15:46:30 -05001018 free_extent_buffer(tmp);
Chris Mason0660b5a2008-11-17 20:37:39 -05001019 BUG_ON(ret);
1020
Yan, Zhenga22285a2010-05-16 10:48:46 -04001021 /*
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 Bacik6a912212010-11-20 09:48:00 +00001029 dput(parent);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001030
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, Zhengd68fc572010-05-16 10:49:58 -04001034
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001035 btrfs_reloc_post_snapshot(trans, pending);
Yan, Zhengd68fc572010-05-16 10:49:58 -04001036 btrfs_orphan_post_snapshot(trans, pending);
Chris Mason3063d292008-01-08 15:46:30 -05001037fail:
Sage Weil6bdb72d2010-03-15 17:27:13 +00001038 kfree(new_root_item);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001039 btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
1040 return 0;
Chris Mason3063d292008-01-08 15:46:30 -05001041}
1042
Chris Masond352ac62008-09-29 15:18:18 -04001043/*
1044 * create all the snapshots we've scheduled for creation
1045 */
Chris Mason80b67942008-02-01 16:35:04 -05001046static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
1047 struct btrfs_fs_info *fs_info)
Chris Mason3063d292008-01-08 15:46:30 -05001048{
1049 struct btrfs_pending_snapshot *pending;
1050 struct list_head *head = &trans->transaction->pending_snapshots;
Chris Mason3de45862008-11-17 21:02:50 -05001051 int ret;
1052
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001053 list_for_each_entry(pending, head, list) {
Chris Mason3de45862008-11-17 21:02:50 -05001054 ret = create_pending_snapshot(trans, fs_info, pending);
1055 BUG_ON(ret);
1056 }
1057 return 0;
1058}
1059
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001060static 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 Bacik0af3d002010-06-21 14:48:16 -04001076 if (super->cache_generation != 0 || btrfs_test_opt(root, SPACE_CACHE))
1077 super->cache_generation = root_item->generation;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001078}
1079
Chris Masonf36f3042009-07-30 10:04:48 -04001080int 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, Zheng8929ecfa2010-05-16 10:49:58 -04001090int 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 Weilbb9c12c2010-10-29 15:37:34 -04001100/*
1101 * wait for the current transaction commit to start and block subsequent
1102 * transaction joins
1103 */
1104static 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 */
1131static 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 */
1160struct btrfs_async_commit {
1161 struct btrfs_trans_handle *newtrans;
1162 struct btrfs_root *root;
1163 struct delayed_work work;
1164};
1165
1166static 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
1175int 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 Itohdb5b4932011-03-23 08:14:16 +00001183 if (!ac)
1184 return -ENOMEM;
Sage Weilbb9c12c2010-10-29 15:37:34 -04001185
1186 INIT_DELAYED_WORK(&ac->work, do_async_commit);
1187 ac->root = root;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001188 ac->newtrans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001189 if (IS_ERR(ac->newtrans)) {
1190 int err = PTR_ERR(ac->newtrans);
1191 kfree(ac);
1192 return err;
1193 }
Sage Weilbb9c12c2010-10-29 15:37:34 -04001194
1195 /* take transaction reference */
1196 mutex_lock(&root->fs_info->trans_mutex);
1197 cur_trans = trans->transaction;
Josef Bacik13c5a932011-04-11 15:45:29 -04001198 atomic_inc(&cur_trans->use_count);
Sage Weilbb9c12c2010-10-29 15:37:34 -04001199 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 Mason79154b12007-03-22 15:59:16 -04001223int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1224 struct btrfs_root *root)
1225{
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001226 unsigned long joined = 0;
Chris Mason79154b12007-03-22 15:59:16 -04001227 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -04001228 struct btrfs_transaction *prev_trans = NULL;
Chris Mason79154b12007-03-22 15:59:16 -04001229 DEFINE_WAIT(wait);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001230 int ret;
Chris Mason89573b92009-03-12 20:12:45 -04001231 int should_grow = 0;
1232 unsigned long now = get_seconds();
Sage Weildccae992009-04-02 16:59:01 -04001233 int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
Chris Mason79154b12007-03-22 15:59:16 -04001234
Chris Mason5a3f23d2009-03-31 13:27:11 -04001235 btrfs_run_ordered_operations(root, 0);
1236
Chris Mason56bec292009-03-13 10:10:06 -04001237 /* 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, Zhenga22285a2010-05-16 10:48:46 -04001243 btrfs_trans_release_metadata(trans, root);
1244
Chris Masonb7ec40d2009-03-12 20:12:45 -04001245 cur_trans = trans->transaction;
Chris Mason56bec292009-03-13 10:10:06 -04001246 /*
1247 * set the flushing flag so procs in this transaction have to
1248 * start sending their work down.
1249 */
Chris Masonb7ec40d2009-03-12 20:12:45 -04001250 cur_trans->delayed_refs.flushing = 1;
Chris Mason56bec292009-03-13 10:10:06 -04001251
Chris Masonc3e69d52009-03-13 10:17:05 -04001252 ret = btrfs_run_delayed_refs(trans, root, 0);
Chris Mason56bec292009-03-13 10:10:06 -04001253 BUG_ON(ret);
1254
Chris Mason79154b12007-03-22 15:59:16 -04001255 mutex_lock(&root->fs_info->trans_mutex);
Chris Masonb7ec40d2009-03-12 20:12:45 -04001256 if (cur_trans->in_commit) {
Josef Bacik13c5a932011-04-11 15:45:29 -04001257 atomic_inc(&cur_trans->use_count);
Chris Masonccd467d2007-06-28 15:57:36 -04001258 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -04001259 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -04001260
Chris Mason79154b12007-03-22 15:59:16 -04001261 ret = wait_for_commit(root, cur_trans);
1262 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001263
1264 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -04001265 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001266 mutex_unlock(&root->fs_info->trans_mutex);
1267
Chris Mason79154b12007-03-22 15:59:16 -04001268 return 0;
1269 }
Chris Mason4313b392008-01-03 09:08:48 -05001270
Chris Mason2c90e5d2007-04-02 10:50:19 -04001271 trans->transaction->in_commit = 1;
Chris Masonf9295742008-07-17 12:54:14 -04001272 trans->transaction->blocked = 1;
Sage Weilbb9c12c2010-10-29 15:37:34 -04001273 wake_up(&root->fs_info->transaction_blocked_wait);
1274
Chris Masonccd467d2007-06-28 15:57:36 -04001275 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 Bacik13c5a932011-04-11 15:45:29 -04001279 atomic_inc(&prev_trans->use_count);
Chris Masonccd467d2007-06-28 15:57:36 -04001280 mutex_unlock(&root->fs_info->trans_mutex);
1281
1282 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -04001283
Chris Masonccd467d2007-06-28 15:57:36 -04001284 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001285 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -04001286 }
1287 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001288
Chris Mason89573b92009-03-12 20:12:45 -04001289 if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
1290 should_grow = 1;
1291
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001292 do {
Yan Zheng7ea394f2008-08-05 13:05:02 -04001293 int snap_pending = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001294 joined = cur_trans->num_joined;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001295 if (!list_empty(&trans->transaction->pending_snapshots))
1296 snap_pending = 1;
1297
Chris Mason2c90e5d2007-04-02 10:50:19 -04001298 WARN_ON(cur_trans != trans->transaction);
Chris Mason79154b12007-03-22 15:59:16 -04001299 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001300
Sage Weil0bdb1db2010-02-19 14:13:50 -08001301 if (flush_on_commit || snap_pending) {
Yan, Zheng24bbcf02009-11-12 09:36:34 +00001302 btrfs_start_delalloc_inodes(root, 1);
1303 ret = btrfs_wait_ordered_extents(root, 0, 1);
Sage Weilebecd3d92009-07-24 13:17:44 -04001304 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001305 }
1306
Chris Mason5a3f23d2009-03-31 13:27:11 -04001307 /*
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 Masoned3b3d3142010-05-25 10:12:41 -04001316 prepare_to_wait(&cur_trans->writer_wait, &wait,
1317 TASK_UNINTERRUPTIBLE);
1318
Chris Mason89573b92009-03-12 20:12:45 -04001319 smp_mb();
Josef Bacik13c5a932011-04-11 15:45:29 -04001320 if (atomic_read(&cur_trans->num_writers) > 1)
Sage Weil99d16cbc2010-10-29 15:37:34 -04001321 schedule_timeout(MAX_SCHEDULE_TIMEOUT);
1322 else if (should_grow)
1323 schedule_timeout(1);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001324
Chris Mason79154b12007-03-22 15:59:16 -04001325 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001326 finish_wait(&cur_trans->writer_wait, &wait);
Josef Bacik13c5a932011-04-11 15:45:29 -04001327 } while (atomic_read(&cur_trans->num_writers) > 1 ||
Chris Mason89573b92009-03-12 20:12:45 -04001328 (should_grow && cur_trans->num_joined != joined));
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001329
Chris Mason3063d292008-01-08 15:46:30 -05001330 ret = create_pending_snapshots(trans, root->fs_info);
1331 BUG_ON(ret);
1332
Chris Mason56bec292009-03-13 10:10:06 -04001333 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1334 BUG_ON(ret);
1335
Chris Mason2c90e5d2007-04-02 10:50:19 -04001336 WARN_ON(cur_trans != trans->transaction);
Chris Masondc17ff82008-01-08 15:46:30 -05001337
Chris Masone02119d2008-09-05 16:13:11 -04001338 /* 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 Yan1a40e232008-09-26 10:09:34 -04001352
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001353 ret = commit_fs_roots(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04001354 BUG_ON(ret);
1355
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001356 /* commit_fs_roots gets rid of all the tree log roots, it is now
Chris Masone02119d2008-09-05 16:13:11 -04001357 * safe to free the root of tree log roots
1358 */
1359 btrfs_free_log_root_tree(trans, root->fs_info);
1360
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001361 ret = commit_cowonly_roots(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -04001362 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -04001363
Yan Zheng11833d62009-09-11 16:11:19 -04001364 btrfs_prepare_extent_commit(trans, root);
1365
Chris Mason78fae272007-03-25 11:35:08 -04001366 cur_trans = root->fs_info->running_transaction;
Chris Masoncee36a02008-01-15 08:40:48 -05001367 spin_lock(&root->fs_info->new_trans_lock);
Chris Mason78fae272007-03-25 11:35:08 -04001368 root->fs_info->running_transaction = NULL;
Chris Masoncee36a02008-01-15 08:40:48 -05001369 spin_unlock(&root->fs_info->new_trans_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04001370
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001371 btrfs_set_root_node(&root->fs_info->tree_root->root_item,
1372 root->fs_info->tree_root->node);
Josef Bacik817d52f2009-07-13 21:29:25 -04001373 switch_commit_root(root->fs_info->tree_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001374
1375 btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
1376 root->fs_info->chunk_root->node);
Josef Bacik817d52f2009-07-13 21:29:25 -04001377 switch_commit_root(root->fs_info->chunk_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001378
1379 update_super_roots(root);
Chris Masone02119d2008-09-05 16:13:11 -04001380
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 Masona061fc82008-05-07 11:43:44 -04001386 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
1387 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -04001388
Chris Masonf9295742008-07-17 12:54:14 -04001389 trans->transaction->blocked = 0;
Chris Masonb7ec40d2009-03-12 20:12:45 -04001390
Chris Masonf9295742008-07-17 12:54:14 -04001391 wake_up(&root->fs_info->transaction_wait);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001392
Chris Mason78fae272007-03-25 11:35:08 -04001393 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -04001394 ret = btrfs_write_and_wait_transaction(trans, root);
1395 BUG_ON(ret);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001396 write_ctree_super(trans, root, 0);
Chris Mason4313b392008-01-03 09:08:48 -05001397
Chris Masone02119d2008-09-05 16:13:11 -04001398 /*
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 Zheng11833d62009-09-11 16:11:19 -04001404 btrfs_finish_extent_commit(trans, root);
Chris Mason4313b392008-01-03 09:08:48 -05001405
Zheng Yan1a40e232008-09-26 10:09:34 -04001406 mutex_lock(&root->fs_info->trans_mutex);
1407
Chris Mason2c90e5d2007-04-02 10:50:19 -04001408 cur_trans->commit_done = 1;
Chris Masonb7ec40d2009-03-12 20:12:45 -04001409
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001410 root->fs_info->last_trans_committed = cur_trans->transid;
Josef Bacik817d52f2009-07-13 21:29:25 -04001411
Chris Mason2c90e5d2007-04-02 10:50:19 -04001412 wake_up(&cur_trans->commit_wait);
Chris Mason3de45862008-11-17 21:02:50 -05001413
Josef Bacik13c5a932011-04-11 15:45:29 -04001414 list_del_init(&cur_trans->list);
Chris Mason79154b12007-03-22 15:59:16 -04001415 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -04001416 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -04001417
liubo1abe9b82011-03-24 11:18:59 +00001418 trace_btrfs_transaction_commit(root);
1419
Chris Mason78fae272007-03-25 11:35:08 -04001420 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason3de45862008-11-17 21:02:50 -05001421
Josef Bacik9ed74f22009-09-11 16:12:44 -04001422 if (current->journal_info == trans)
1423 current->journal_info = NULL;
1424
Chris Mason2c90e5d2007-04-02 10:50:19 -04001425 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00001426
1427 if (current != root->fs_info->transaction_kthread)
1428 btrfs_run_delayed_iputs(root);
1429
Chris Mason79154b12007-03-22 15:59:16 -04001430 return ret;
1431}
1432
Chris Masond352ac62008-09-29 15:18:18 -04001433/*
1434 * interface function to delete all the snapshots we have scheduled for deletion
1435 */
Chris Masone9d0b132007-08-10 14:06:19 -04001436int btrfs_clean_old_snapshots(struct btrfs_root *root)
1437{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001438 LIST_HEAD(list);
1439 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone9d0b132007-08-10 14:06:19 -04001440
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001441 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, Zheng76dda932009-09-21 16:00:26 -04001447 list_del(&root->root_list);
1448
1449 if (btrfs_header_backref_rev(root->node) <
1450 BTRFS_MIXED_BACKREF_REV)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001451 btrfs_drop_snapshot(root, NULL, 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04001452 else
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001453 btrfs_drop_snapshot(root, NULL, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001454 }
1455 return 0;
1456}