blob: 955f76eb0fa8c7b8f85e74ef3a62cace554d04c0 [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,
David Sterbaf993c882011-04-20 23:35:57 +020083 root->fs_info->btree_inode->i_mapping);
Chris Mason48ec2cf2008-06-09 09:35:50 -040084 spin_lock(&root->fs_info->new_trans_lock);
85 root->fs_info->running_transaction = cur_trans;
86 spin_unlock(&root->fs_info->new_trans_lock);
Josef Bacik15ee9bc2007-08-10 16:22:09 -040087 } else {
Josef Bacik13c5a932011-04-11 15:45:29 -040088 atomic_inc(&cur_trans->num_writers);
Josef Bacik15ee9bc2007-08-10 16:22:09 -040089 cur_trans->num_joined++;
Chris Mason79154b12007-03-22 15:59:16 -040090 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -040091
Chris Mason79154b12007-03-22 15:59:16 -040092 return 0;
93}
94
Chris Masond352ac62008-09-29 15:18:18 -040095/*
Chris Masond3977122009-01-05 21:25:51 -050096 * this does all the record keeping required to make sure that a reference
97 * counted root is properly recorded in a given transaction. This is required
98 * to make sure the old root from before we joined the transaction is deleted
99 * when the transaction commits
Chris Masond352ac62008-09-29 15:18:18 -0400100 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400101static noinline int record_root_in_trans(struct btrfs_trans_handle *trans,
102 struct btrfs_root *root)
Chris Mason6702ed42007-08-07 16:15:09 -0400103{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400104 if (root->ref_cows && root->last_trans < trans->transid) {
Chris Mason6702ed42007-08-07 16:15:09 -0400105 WARN_ON(root == root->fs_info->extent_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400106 WARN_ON(root->commit_root != root->node);
Yan Zheng31153d82008-07-28 15:32:19 -0400107
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400108 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
109 (unsigned long)root->root_key.objectid,
110 BTRFS_ROOT_TRANS_TAG);
111 root->last_trans = trans->transid;
112 btrfs_init_reloc_root(trans, root);
Chris Mason6702ed42007-08-07 16:15:09 -0400113 }
114 return 0;
115}
116
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400117int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
118 struct btrfs_root *root)
119{
120 if (!root->ref_cows)
121 return 0;
122
123 mutex_lock(&root->fs_info->trans_mutex);
124 if (root->last_trans == trans->transid) {
125 mutex_unlock(&root->fs_info->trans_mutex);
126 return 0;
127 }
128
129 record_root_in_trans(trans, root);
130 mutex_unlock(&root->fs_info->trans_mutex);
131 return 0;
132}
133
Chris Masond352ac62008-09-29 15:18:18 -0400134/* wait for commit against the current transaction to become unblocked
135 * when this is done, it is safe to start a new transaction, but the current
136 * transaction might not be fully on disk.
137 */
Chris Mason37d1aee2008-07-31 10:48:37 -0400138static void wait_current_trans(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -0400139{
Chris Masonf9295742008-07-17 12:54:14 -0400140 struct btrfs_transaction *cur_trans;
Chris Mason79154b12007-03-22 15:59:16 -0400141
Chris Masonf9295742008-07-17 12:54:14 -0400142 cur_trans = root->fs_info->running_transaction;
Chris Mason37d1aee2008-07-31 10:48:37 -0400143 if (cur_trans && cur_trans->blocked) {
Chris Masonf9295742008-07-17 12:54:14 -0400144 DEFINE_WAIT(wait);
Josef Bacik13c5a932011-04-11 15:45:29 -0400145 atomic_inc(&cur_trans->use_count);
Chris Masond3977122009-01-05 21:25:51 -0500146 while (1) {
Chris Masonf9295742008-07-17 12:54:14 -0400147 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
148 TASK_UNINTERRUPTIBLE);
Zhao Lei471fa172010-03-25 12:35:14 +0000149 if (!cur_trans->blocked)
Chris Masonf9295742008-07-17 12:54:14 -0400150 break;
Zhao Lei471fa172010-03-25 12:35:14 +0000151 mutex_unlock(&root->fs_info->trans_mutex);
152 schedule();
153 mutex_lock(&root->fs_info->trans_mutex);
Chris Masonf9295742008-07-17 12:54:14 -0400154 }
Zhao Lei471fa172010-03-25 12:35:14 +0000155 finish_wait(&root->fs_info->transaction_wait, &wait);
Chris Masonf9295742008-07-17 12:54:14 -0400156 put_transaction(cur_trans);
157 }
Chris Mason37d1aee2008-07-31 10:48:37 -0400158}
159
Josef Bacik249ac1e2009-11-10 21:23:48 -0500160enum btrfs_trans_type {
161 TRANS_START,
162 TRANS_JOIN,
163 TRANS_USERSPACE,
Josef Bacik0af3d002010-06-21 14:48:16 -0400164 TRANS_JOIN_NOLOCK,
Josef Bacik249ac1e2009-11-10 21:23:48 -0500165};
166
Yan, Zhenga22285a2010-05-16 10:48:46 -0400167static int may_wait_transaction(struct btrfs_root *root, int type)
Chris Mason37d1aee2008-07-31 10:48:37 -0400168{
Chris Mason4bef0842008-09-08 11:18:08 -0400169 if (!root->fs_info->log_root_recovering &&
Josef Bacik249ac1e2009-11-10 21:23:48 -0500170 ((type == TRANS_START && !root->fs_info->open_ioctl_trans) ||
171 type == TRANS_USERSPACE))
Yan, Zhenga22285a2010-05-16 10:48:46 -0400172 return 1;
173 return 0;
174}
175
176static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
177 u64 num_items, int type)
178{
179 struct btrfs_trans_handle *h;
180 struct btrfs_transaction *cur_trans;
Josef Bacik06d5a582011-04-05 11:57:27 -0400181 int retries = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400182 int ret;
liuboacce9522011-01-06 19:30:25 +0800183
184 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
185 return ERR_PTR(-EROFS);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400186again:
187 h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
188 if (!h)
189 return ERR_PTR(-ENOMEM);
190
Josef Bacik0af3d002010-06-21 14:48:16 -0400191 if (type != TRANS_JOIN_NOLOCK)
192 mutex_lock(&root->fs_info->trans_mutex);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400193 if (may_wait_transaction(root, type))
Chris Mason37d1aee2008-07-31 10:48:37 -0400194 wait_current_trans(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400195
Chris Mason79154b12007-03-22 15:59:16 -0400196 ret = join_transaction(root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +0000197 if (ret < 0) {
Yoshinori Sano6e8df2a2011-04-03 12:31:28 +0000198 kmem_cache_free(btrfs_trans_handle_cachep, h);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +0000199 if (type != TRANS_JOIN_NOLOCK)
200 mutex_unlock(&root->fs_info->trans_mutex);
201 return ERR_PTR(ret);
202 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400203
Yan, Zhenga22285a2010-05-16 10:48:46 -0400204 cur_trans = root->fs_info->running_transaction;
Josef Bacik13c5a932011-04-11 15:45:29 -0400205 atomic_inc(&cur_trans->use_count);
Josef Bacik0af3d002010-06-21 14:48:16 -0400206 if (type != TRANS_JOIN_NOLOCK)
207 mutex_unlock(&root->fs_info->trans_mutex);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400208
209 h->transid = cur_trans->transid;
210 h->transaction = cur_trans;
Chris Mason79154b12007-03-22 15:59:16 -0400211 h->blocks_used = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500212 h->block_group = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400213 h->bytes_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400214 h->delayed_ref_updates = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400215 h->block_rsv = NULL;
Chris Masonb7ec40d2009-03-12 20:12:45 -0400216
Yan, Zhenga22285a2010-05-16 10:48:46 -0400217 smp_mb();
218 if (cur_trans->blocked && may_wait_transaction(root, type)) {
219 btrfs_commit_transaction(h, root);
220 goto again;
221 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400222
Yan, Zhenga22285a2010-05-16 10:48:46 -0400223 if (num_items > 0) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -0400224 ret = btrfs_trans_reserve_metadata(h, root, num_items);
Josef Bacik06d5a582011-04-05 11:57:27 -0400225 if (ret == -EAGAIN && !retries) {
226 retries++;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400227 btrfs_commit_transaction(h, root);
228 goto again;
Josef Bacik06d5a582011-04-05 11:57:27 -0400229 } else if (ret == -EAGAIN) {
230 /*
231 * We have already retried and got EAGAIN, so really we
232 * don't have space, so set ret to -ENOSPC.
233 */
234 ret = -ENOSPC;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400235 }
Josef Bacik06d5a582011-04-05 11:57:27 -0400236
Yan, Zhenga22285a2010-05-16 10:48:46 -0400237 if (ret < 0) {
238 btrfs_end_transaction(h, root);
239 return ERR_PTR(ret);
240 }
241 }
242
Josef Bacik0af3d002010-06-21 14:48:16 -0400243 if (type != TRANS_JOIN_NOLOCK)
244 mutex_lock(&root->fs_info->trans_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400245 record_root_in_trans(h, root);
Josef Bacik0af3d002010-06-21 14:48:16 -0400246 if (type != TRANS_JOIN_NOLOCK)
247 mutex_unlock(&root->fs_info->trans_mutex);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400248
249 if (!current->journal_info && type != TRANS_USERSPACE)
250 current->journal_info = h;
Chris Mason79154b12007-03-22 15:59:16 -0400251 return h;
252}
253
Chris Masonf9295742008-07-17 12:54:14 -0400254struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
Yan, Zhenga22285a2010-05-16 10:48:46 -0400255 int num_items)
Chris Masonf9295742008-07-17 12:54:14 -0400256{
Yan, Zhenga22285a2010-05-16 10:48:46 -0400257 return start_transaction(root, num_items, TRANS_START);
Chris Masonf9295742008-07-17 12:54:14 -0400258}
259struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
260 int num_blocks)
261{
Yan, Zhenga22285a2010-05-16 10:48:46 -0400262 return start_transaction(root, 0, TRANS_JOIN);
Chris Masonf9295742008-07-17 12:54:14 -0400263}
264
Josef Bacik0af3d002010-06-21 14:48:16 -0400265struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root,
266 int num_blocks)
267{
268 return start_transaction(root, 0, TRANS_JOIN_NOLOCK);
269}
270
Sage Weil9ca9ee02008-08-04 10:41:27 -0400271struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
272 int num_blocks)
273{
Yan, Zhenga22285a2010-05-16 10:48:46 -0400274 return start_transaction(r, 0, TRANS_USERSPACE);
Sage Weil9ca9ee02008-08-04 10:41:27 -0400275}
276
Chris Masond352ac62008-09-29 15:18:18 -0400277/* wait for a transaction commit to be fully complete */
Chris Mason89ce8a62008-06-25 16:01:31 -0400278static noinline int wait_for_commit(struct btrfs_root *root,
279 struct btrfs_transaction *commit)
280{
281 DEFINE_WAIT(wait);
282 mutex_lock(&root->fs_info->trans_mutex);
Chris Masond3977122009-01-05 21:25:51 -0500283 while (!commit->commit_done) {
Chris Mason89ce8a62008-06-25 16:01:31 -0400284 prepare_to_wait(&commit->commit_wait, &wait,
285 TASK_UNINTERRUPTIBLE);
286 if (commit->commit_done)
287 break;
288 mutex_unlock(&root->fs_info->trans_mutex);
289 schedule();
290 mutex_lock(&root->fs_info->trans_mutex);
291 }
292 mutex_unlock(&root->fs_info->trans_mutex);
293 finish_wait(&commit->commit_wait, &wait);
294 return 0;
295}
296
Sage Weil46204592010-10-29 15:41:32 -0400297int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
298{
299 struct btrfs_transaction *cur_trans = NULL, *t;
300 int ret;
301
302 mutex_lock(&root->fs_info->trans_mutex);
303
304 ret = 0;
305 if (transid) {
306 if (transid <= root->fs_info->last_trans_committed)
307 goto out_unlock;
308
309 /* find specified transaction */
310 list_for_each_entry(t, &root->fs_info->trans_list, list) {
311 if (t->transid == transid) {
312 cur_trans = t;
313 break;
314 }
315 if (t->transid > transid)
316 break;
317 }
318 ret = -EINVAL;
319 if (!cur_trans)
320 goto out_unlock; /* bad transid */
321 } else {
322 /* find newest transaction that is committing | committed */
323 list_for_each_entry_reverse(t, &root->fs_info->trans_list,
324 list) {
325 if (t->in_commit) {
326 if (t->commit_done)
327 goto out_unlock;
328 cur_trans = t;
329 break;
330 }
331 }
332 if (!cur_trans)
333 goto out_unlock; /* nothing committing|committed */
334 }
335
Josef Bacik13c5a932011-04-11 15:45:29 -0400336 atomic_inc(&cur_trans->use_count);
Sage Weil46204592010-10-29 15:41:32 -0400337 mutex_unlock(&root->fs_info->trans_mutex);
338
339 wait_for_commit(root, cur_trans);
340
341 mutex_lock(&root->fs_info->trans_mutex);
342 put_transaction(cur_trans);
343 ret = 0;
344out_unlock:
345 mutex_unlock(&root->fs_info->trans_mutex);
346 return ret;
347}
348
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400349#if 0
Chris Masond352ac62008-09-29 15:18:18 -0400350/*
Chris Masond3977122009-01-05 21:25:51 -0500351 * rate limit against the drop_snapshot code. This helps to slow down new
352 * operations if the drop_snapshot code isn't able to keep up.
Chris Masond352ac62008-09-29 15:18:18 -0400353 */
Chris Mason37d1aee2008-07-31 10:48:37 -0400354static void throttle_on_drops(struct btrfs_root *root)
Chris Masonab78c842008-07-29 16:15:18 -0400355{
356 struct btrfs_fs_info *info = root->fs_info;
Chris Mason2dd3e672008-08-04 08:20:15 -0400357 int harder_count = 0;
Chris Masonab78c842008-07-29 16:15:18 -0400358
Chris Mason2dd3e672008-08-04 08:20:15 -0400359harder:
Chris Masonab78c842008-07-29 16:15:18 -0400360 if (atomic_read(&info->throttles)) {
361 DEFINE_WAIT(wait);
362 int thr;
Chris Masonab78c842008-07-29 16:15:18 -0400363 thr = atomic_read(&info->throttle_gen);
364
365 do {
366 prepare_to_wait(&info->transaction_throttle,
367 &wait, TASK_UNINTERRUPTIBLE);
368 if (!atomic_read(&info->throttles)) {
369 finish_wait(&info->transaction_throttle, &wait);
370 break;
371 }
372 schedule();
373 finish_wait(&info->transaction_throttle, &wait);
374 } while (thr == atomic_read(&info->throttle_gen));
Chris Mason2dd3e672008-08-04 08:20:15 -0400375 harder_count++;
376
377 if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
378 harder_count < 2)
379 goto harder;
380
381 if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
382 harder_count < 10)
383 goto harder;
384
385 if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
386 harder_count < 20)
387 goto harder;
Chris Masonab78c842008-07-29 16:15:18 -0400388 }
389}
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400390#endif
Chris Masonab78c842008-07-29 16:15:18 -0400391
Chris Mason37d1aee2008-07-31 10:48:37 -0400392void btrfs_throttle(struct btrfs_root *root)
393{
394 mutex_lock(&root->fs_info->trans_mutex);
Sage Weil9ca9ee02008-08-04 10:41:27 -0400395 if (!root->fs_info->open_ioctl_trans)
396 wait_current_trans(root);
Chris Mason37d1aee2008-07-31 10:48:37 -0400397 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason37d1aee2008-07-31 10:48:37 -0400398}
399
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400400static int should_end_transaction(struct btrfs_trans_handle *trans,
401 struct btrfs_root *root)
402{
403 int ret;
404 ret = btrfs_block_rsv_check(trans, root,
405 &root->fs_info->global_block_rsv, 0, 5);
406 return ret ? 1 : 0;
407}
408
409int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
410 struct btrfs_root *root)
411{
412 struct btrfs_transaction *cur_trans = trans->transaction;
413 int updates;
414
415 if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
416 return 1;
417
418 updates = trans->delayed_ref_updates;
419 trans->delayed_ref_updates = 0;
420 if (updates)
421 btrfs_run_delayed_refs(trans, root, updates);
422
423 return should_end_transaction(trans, root);
424}
425
Chris Mason89ce8a62008-06-25 16:01:31 -0400426static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
Josef Bacik0af3d002010-06-21 14:48:16 -0400427 struct btrfs_root *root, int throttle, int lock)
Chris Mason79154b12007-03-22 15:59:16 -0400428{
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400429 struct btrfs_transaction *cur_trans = trans->transaction;
Chris Masonab78c842008-07-29 16:15:18 -0400430 struct btrfs_fs_info *info = root->fs_info;
Chris Masonc3e69d52009-03-13 10:17:05 -0400431 int count = 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400432
Chris Masonc3e69d52009-03-13 10:17:05 -0400433 while (count < 4) {
434 unsigned long cur = trans->delayed_ref_updates;
435 trans->delayed_ref_updates = 0;
436 if (cur &&
437 trans->transaction->delayed_refs.num_heads_ready > 64) {
438 trans->delayed_ref_updates = 0;
Chris Masonb7ec40d2009-03-12 20:12:45 -0400439
440 /*
441 * do a full flush if the transaction is trying
442 * to close
443 */
444 if (trans->transaction->delayed_refs.flushing)
445 cur = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -0400446 btrfs_run_delayed_refs(trans, root, cur);
447 } else {
448 break;
449 }
450 count++;
Chris Mason56bec292009-03-13 10:10:06 -0400451 }
452
Yan, Zhenga22285a2010-05-16 10:48:46 -0400453 btrfs_trans_release_metadata(trans, root);
454
Josef Bacik0af3d002010-06-21 14:48:16 -0400455 if (lock && !root->fs_info->open_ioctl_trans &&
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400456 should_end_transaction(trans, root))
457 trans->transaction->blocked = 1;
458
Josef Bacik0af3d002010-06-21 14:48:16 -0400459 if (lock && cur_trans->blocked && !cur_trans->in_commit) {
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400460 if (throttle)
461 return btrfs_commit_transaction(trans, root);
462 else
463 wake_up_process(info->transaction_kthread);
464 }
465
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400466 WARN_ON(cur_trans != info->running_transaction);
Josef Bacik13c5a932011-04-11 15:45:29 -0400467 WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
468 atomic_dec(&cur_trans->num_writers);
Chris Mason89ce8a62008-06-25 16:01:31 -0400469
Sage Weil99d16cb2010-10-29 15:37:34 -0400470 smp_mb();
Chris Mason79154b12007-03-22 15:59:16 -0400471 if (waitqueue_active(&cur_trans->writer_wait))
472 wake_up(&cur_trans->writer_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400473 put_transaction(cur_trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400474
475 if (current->journal_info == trans)
476 current->journal_info = NULL;
Chris Masond6025572007-03-30 14:27:56 -0400477 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400478 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Masonab78c842008-07-29 16:15:18 -0400479
Yan, Zheng24bbcf042009-11-12 09:36:34 +0000480 if (throttle)
481 btrfs_run_delayed_iputs(root);
482
Chris Mason79154b12007-03-22 15:59:16 -0400483 return 0;
484}
485
Chris Mason89ce8a62008-06-25 16:01:31 -0400486int btrfs_end_transaction(struct btrfs_trans_handle *trans,
487 struct btrfs_root *root)
488{
Josef Bacik0af3d002010-06-21 14:48:16 -0400489 return __btrfs_end_transaction(trans, root, 0, 1);
Chris Mason89ce8a62008-06-25 16:01:31 -0400490}
491
492int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
493 struct btrfs_root *root)
494{
Josef Bacik0af3d002010-06-21 14:48:16 -0400495 return __btrfs_end_transaction(trans, root, 1, 1);
496}
497
498int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
499 struct btrfs_root *root)
500{
501 return __btrfs_end_transaction(trans, root, 0, 0);
Chris Mason89ce8a62008-06-25 16:01:31 -0400502}
503
Chris Masond352ac62008-09-29 15:18:18 -0400504/*
505 * when btree blocks are allocated, they have some corresponding bits set for
506 * them in one of two extent_io trees. This is used to make sure all of
Chris Mason690587d2009-10-13 13:29:19 -0400507 * those extents are sent to disk but does not wait on them
Chris Masond352ac62008-09-29 15:18:18 -0400508 */
Chris Mason690587d2009-10-13 13:29:19 -0400509int btrfs_write_marked_extents(struct btrfs_root *root,
Yan, Zheng8cef4e12009-11-12 09:33:26 +0000510 struct extent_io_tree *dirty_pages, int mark)
Chris Mason79154b12007-03-22 15:59:16 -0400511{
Chris Mason7c4452b2007-04-28 09:29:35 -0400512 int ret;
Chris Mason777e6bd2008-08-15 15:34:15 -0400513 int err = 0;
Chris Mason7c4452b2007-04-28 09:29:35 -0400514 int werr = 0;
515 struct page *page;
Chris Mason7c4452b2007-04-28 09:29:35 -0400516 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason777e6bd2008-08-15 15:34:15 -0400517 u64 start = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400518 u64 end;
519 unsigned long index;
Chris Mason7c4452b2007-04-28 09:29:35 -0400520
Chris Masond3977122009-01-05 21:25:51 -0500521 while (1) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400522 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
Yan, Zheng8cef4e12009-11-12 09:33:26 +0000523 mark);
Chris Mason5f39d392007-10-15 16:14:19 -0400524 if (ret)
Chris Mason7c4452b2007-04-28 09:29:35 -0400525 break;
Chris Masond3977122009-01-05 21:25:51 -0500526 while (start <= end) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400527 cond_resched();
528
Chris Mason5f39d392007-10-15 16:14:19 -0400529 index = start >> PAGE_CACHE_SHIFT;
Chris Mason35ebb932007-10-30 16:56:53 -0400530 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
Chris Mason4bef0842008-09-08 11:18:08 -0400531 page = find_get_page(btree_inode->i_mapping, index);
Chris Mason7c4452b2007-04-28 09:29:35 -0400532 if (!page)
533 continue;
Chris Mason4bef0842008-09-08 11:18:08 -0400534
535 btree_lock_page_hook(page);
536 if (!page->mapping) {
537 unlock_page(page);
538 page_cache_release(page);
539 continue;
540 }
541
Chris Mason6702ed42007-08-07 16:15:09 -0400542 if (PageWriteback(page)) {
543 if (PageDirty(page))
544 wait_on_page_writeback(page);
545 else {
546 unlock_page(page);
547 page_cache_release(page);
548 continue;
549 }
550 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400551 err = write_one_page(page, 0);
552 if (err)
553 werr = err;
554 page_cache_release(page);
555 }
556 }
Chris Mason690587d2009-10-13 13:29:19 -0400557 if (err)
558 werr = err;
559 return werr;
560}
561
562/*
563 * when btree blocks are allocated, they have some corresponding bits set for
564 * them in one of two extent_io trees. This is used to make sure all of
565 * those extents are on disk for transaction or log commit. We wait
566 * on all the pages and clear them from the dirty pages state tree
567 */
568int btrfs_wait_marked_extents(struct btrfs_root *root,
Yan, Zheng8cef4e12009-11-12 09:33:26 +0000569 struct extent_io_tree *dirty_pages, int mark)
Chris Mason690587d2009-10-13 13:29:19 -0400570{
571 int ret;
572 int err = 0;
573 int werr = 0;
574 struct page *page;
575 struct inode *btree_inode = root->fs_info->btree_inode;
576 u64 start = 0;
577 u64 end;
578 unsigned long index;
579
Chris Masond3977122009-01-05 21:25:51 -0500580 while (1) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +0000581 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
582 mark);
Chris Mason777e6bd2008-08-15 15:34:15 -0400583 if (ret)
584 break;
585
Yan, Zheng8cef4e12009-11-12 09:33:26 +0000586 clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500587 while (start <= end) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400588 index = start >> PAGE_CACHE_SHIFT;
589 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
590 page = find_get_page(btree_inode->i_mapping, index);
591 if (!page)
592 continue;
593 if (PageDirty(page)) {
Chris Mason4bef0842008-09-08 11:18:08 -0400594 btree_lock_page_hook(page);
595 wait_on_page_writeback(page);
Chris Mason777e6bd2008-08-15 15:34:15 -0400596 err = write_one_page(page, 0);
597 if (err)
598 werr = err;
599 }
Chris Mason105d9312008-11-18 12:13:12 -0500600 wait_on_page_writeback(page);
Chris Mason777e6bd2008-08-15 15:34:15 -0400601 page_cache_release(page);
602 cond_resched();
603 }
604 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400605 if (err)
606 werr = err;
607 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400608}
609
Chris Mason690587d2009-10-13 13:29:19 -0400610/*
611 * when btree blocks are allocated, they have some corresponding bits set for
612 * them in one of two extent_io trees. This is used to make sure all of
613 * those extents are on disk for transaction or log commit
614 */
615int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
Yan, Zheng8cef4e12009-11-12 09:33:26 +0000616 struct extent_io_tree *dirty_pages, int mark)
Chris Mason690587d2009-10-13 13:29:19 -0400617{
618 int ret;
619 int ret2;
620
Yan, Zheng8cef4e12009-11-12 09:33:26 +0000621 ret = btrfs_write_marked_extents(root, dirty_pages, mark);
622 ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
Chris Mason690587d2009-10-13 13:29:19 -0400623 return ret || ret2;
624}
625
Chris Masond0c803c2008-09-11 16:17:57 -0400626int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
627 struct btrfs_root *root)
628{
629 if (!trans || !trans->transaction) {
630 struct inode *btree_inode;
631 btree_inode = root->fs_info->btree_inode;
632 return filemap_write_and_wait(btree_inode->i_mapping);
633 }
634 return btrfs_write_and_wait_marked_extents(root,
Yan, Zheng8cef4e12009-11-12 09:33:26 +0000635 &trans->transaction->dirty_pages,
636 EXTENT_DIRTY);
Chris Masond0c803c2008-09-11 16:17:57 -0400637}
638
Chris Masond352ac62008-09-29 15:18:18 -0400639/*
640 * this is used to update the root pointer in the tree of tree roots.
641 *
642 * But, in the case of the extent allocation tree, updating the root
643 * pointer may allocate blocks which may change the root of the extent
644 * allocation tree.
645 *
646 * So, this loops and repeats and makes sure the cowonly root didn't
647 * change while the root pointer was being updated in the metadata.
648 */
Chris Mason0b86a832008-03-24 15:01:56 -0400649static int update_cowonly_root(struct btrfs_trans_handle *trans,
650 struct btrfs_root *root)
651{
652 int ret;
653 u64 old_root_bytenr;
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000654 u64 old_root_used;
Chris Mason0b86a832008-03-24 15:01:56 -0400655 struct btrfs_root *tree_root = root->fs_info->tree_root;
656
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000657 old_root_used = btrfs_root_used(&root->root_item);
Chris Mason0b86a832008-03-24 15:01:56 -0400658 btrfs_write_dirty_block_groups(trans, root);
Chris Mason56bec292009-03-13 10:10:06 -0400659
Chris Masond3977122009-01-05 21:25:51 -0500660 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -0400661 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000662 if (old_root_bytenr == root->node->start &&
663 old_root_used == btrfs_root_used(&root->root_item))
Chris Mason0b86a832008-03-24 15:01:56 -0400664 break;
Chris Mason87ef2bb2008-10-30 11:23:27 -0400665
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400666 btrfs_set_root_node(&root->root_item, root->node);
Chris Mason0b86a832008-03-24 15:01:56 -0400667 ret = btrfs_update_root(trans, tree_root,
668 &root->root_key,
669 &root->root_item);
670 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -0400671
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000672 old_root_used = btrfs_root_used(&root->root_item);
Yan Zheng4a8c9a62009-07-22 10:07:05 -0400673 ret = btrfs_write_dirty_block_groups(trans, root);
Chris Mason56bec292009-03-13 10:10:06 -0400674 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -0400675 }
Yan Zheng276e6802009-07-30 09:40:40 -0400676
677 if (root != root->fs_info->extent_root)
678 switch_commit_root(root);
679
Chris Mason0b86a832008-03-24 15:01:56 -0400680 return 0;
681}
682
Chris Masond352ac62008-09-29 15:18:18 -0400683/*
684 * update all the cowonly tree roots on disk
685 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400686static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
687 struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -0400688{
Chris Mason79154b12007-03-22 15:59:16 -0400689 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400690 struct list_head *next;
Yan Zheng84234f32008-10-29 14:49:05 -0400691 struct extent_buffer *eb;
Chris Mason56bec292009-03-13 10:10:06 -0400692 int ret;
Yan Zheng84234f32008-10-29 14:49:05 -0400693
Chris Mason56bec292009-03-13 10:10:06 -0400694 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
695 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400696
Yan Zheng84234f32008-10-29 14:49:05 -0400697 eb = btrfs_lock_root_node(fs_info->tree_root);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400698 btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
Yan Zheng84234f32008-10-29 14:49:05 -0400699 btrfs_tree_unlock(eb);
700 free_extent_buffer(eb);
Chris Mason79154b12007-03-22 15:59:16 -0400701
Chris Mason56bec292009-03-13 10:10:06 -0400702 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
703 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400704
Chris Masond3977122009-01-05 21:25:51 -0500705 while (!list_empty(&fs_info->dirty_cowonly_roots)) {
Chris Mason0b86a832008-03-24 15:01:56 -0400706 next = fs_info->dirty_cowonly_roots.next;
707 list_del_init(next);
708 root = list_entry(next, struct btrfs_root, dirty_list);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400709
Chris Mason0b86a832008-03-24 15:01:56 -0400710 update_cowonly_root(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -0400711 }
Yan Zheng276e6802009-07-30 09:40:40 -0400712
713 down_write(&fs_info->extent_commit_sem);
714 switch_commit_root(fs_info->extent_root);
715 up_write(&fs_info->extent_commit_sem);
716
Chris Mason79154b12007-03-22 15:59:16 -0400717 return 0;
718}
719
Chris Masond352ac62008-09-29 15:18:18 -0400720/*
721 * dead roots are old snapshots that need to be deleted. This allocates
722 * a dirty root struct and adds it into the list of dead roots that need to
723 * be deleted
724 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400725int btrfs_add_dead_root(struct btrfs_root *root)
Chris Mason5eda7b52007-06-22 14:16:25 -0400726{
Yan Zhengb48652c2008-08-04 23:23:47 -0400727 mutex_lock(&root->fs_info->trans_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400728 list_add(&root->root_list, &root->fs_info->dead_roots);
Yan Zhengb48652c2008-08-04 23:23:47 -0400729 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason5eda7b52007-06-22 14:16:25 -0400730 return 0;
731}
732
Chris Masond352ac62008-09-29 15:18:18 -0400733/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400734 * update all the cowonly tree roots on disk
Chris Masond352ac62008-09-29 15:18:18 -0400735 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400736static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
737 struct btrfs_root *root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400738{
Chris Mason0f7d52f2007-04-09 10:42:37 -0400739 struct btrfs_root *gang[8];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400740 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400741 int i;
742 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400743 int err = 0;
744
Chris Masond3977122009-01-05 21:25:51 -0500745 while (1) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400746 ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
747 (void **)gang, 0,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400748 ARRAY_SIZE(gang),
749 BTRFS_ROOT_TRANS_TAG);
750 if (ret == 0)
751 break;
752 for (i = 0; i < ret; i++) {
753 root = gang[i];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400754 radix_tree_tag_clear(&fs_info->fs_roots_radix,
755 (unsigned long)root->root_key.objectid,
756 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400757
Chris Masone02119d2008-09-05 16:13:11 -0400758 btrfs_free_log(trans, root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400759 btrfs_update_reloc_root(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -0400760 btrfs_orphan_commit_root(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -0400761
Yan Zheng978d9102009-06-15 20:01:02 -0400762 if (root->commit_root != root->node) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400763 switch_commit_root(root);
Yan Zheng978d9102009-06-15 20:01:02 -0400764 btrfs_set_root_node(&root->root_item,
765 root->node);
766 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400767
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400768 err = btrfs_update_root(trans, fs_info->tree_root,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400769 &root->root_key,
770 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400771 if (err)
772 break;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400773 }
774 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400775 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400776}
777
Chris Masond352ac62008-09-29 15:18:18 -0400778/*
779 * defrag a given btree. If cacheonly == 1, this won't read from the disk,
780 * otherwise every leaf in the btree is read and defragged.
781 */
Chris Masone9d0b132007-08-10 14:06:19 -0400782int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
783{
784 struct btrfs_fs_info *info = root->fs_info;
Chris Masone9d0b132007-08-10 14:06:19 -0400785 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400786 int ret;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400787 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400788
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400789 if (xchg(&root->defrag_running, 1))
Chris Masone9d0b132007-08-10 14:06:19 -0400790 return 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400791
Chris Mason6b800532007-10-15 16:17:34 -0400792 while (1) {
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400793 trans = btrfs_start_transaction(root, 0);
794 if (IS_ERR(trans))
795 return PTR_ERR(trans);
796
Chris Masone9d0b132007-08-10 14:06:19 -0400797 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400798
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400799 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400800 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400801 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400802 cond_resched();
803
Chris Mason3f157a22008-06-25 16:01:31 -0400804 if (root->fs_info->closing || ret != -EAGAIN)
Chris Masone9d0b132007-08-10 14:06:19 -0400805 break;
806 }
807 root->defrag_running = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400808 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -0400809}
810
Yan Zheng2c47e6052009-06-27 21:07:35 -0400811#if 0
Chris Masond352ac62008-09-29 15:18:18 -0400812/*
Chris Masonb7ec40d2009-03-12 20:12:45 -0400813 * when dropping snapshots, we generate a ton of delayed refs, and it makes
814 * sense not to join the transaction while it is trying to flush the current
815 * queue of delayed refs out.
816 *
817 * This is used by the drop snapshot code only
818 */
819static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info)
820{
821 DEFINE_WAIT(wait);
822
823 mutex_lock(&info->trans_mutex);
824 while (info->running_transaction &&
825 info->running_transaction->delayed_refs.flushing) {
826 prepare_to_wait(&info->transaction_wait, &wait,
827 TASK_UNINTERRUPTIBLE);
828 mutex_unlock(&info->trans_mutex);
Chris Mason59bc5c72009-04-24 14:39:25 -0400829
Chris Masonb7ec40d2009-03-12 20:12:45 -0400830 schedule();
Chris Mason59bc5c72009-04-24 14:39:25 -0400831
Chris Masonb7ec40d2009-03-12 20:12:45 -0400832 mutex_lock(&info->trans_mutex);
833 finish_wait(&info->transaction_wait, &wait);
834 }
835 mutex_unlock(&info->trans_mutex);
836 return 0;
837}
838
839/*
Chris Masond352ac62008-09-29 15:18:18 -0400840 * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
841 * all of them
842 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400843int btrfs_drop_dead_root(struct btrfs_root *root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400844{
Chris Mason0f7d52f2007-04-09 10:42:37 -0400845 struct btrfs_trans_handle *trans;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400846 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400847 unsigned long nr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400848 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -0400849
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400850 while (1) {
851 /*
852 * we don't want to jump in and create a bunch of
853 * delayed refs if the transaction is starting to close
854 */
855 wait_transaction_pre_flush(tree_root->fs_info);
856 trans = btrfs_start_transaction(tree_root, 1);
Josef Bacik58176a92007-08-29 15:47:34 -0400857
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400858 /*
859 * we've joined a transaction, make sure it isn't
860 * closing right now
861 */
862 if (trans->transaction->delayed_refs.flushing) {
863 btrfs_end_transaction(trans, tree_root);
864 continue;
Josef Bacik58176a92007-08-29 15:47:34 -0400865 }
Chris Masona2135012008-06-25 16:01:30 -0400866
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400867 ret = btrfs_drop_snapshot(trans, root);
868 if (ret != -EAGAIN)
Chris Mason54aa1f42007-06-22 14:16:25 -0400869 break;
Chris Masona2135012008-06-25 16:01:30 -0400870
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400871 ret = btrfs_update_root(trans, tree_root,
872 &root->root_key,
873 &root->root_item);
874 if (ret)
875 break;
Yanbcc63ab2008-07-30 16:29:20 -0400876
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400877 nr = trans->blocks_used;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400878 ret = btrfs_end_transaction(trans, tree_root);
879 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400880
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400881 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400882 cond_resched();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400883 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400884 BUG_ON(ret);
885
886 ret = btrfs_del_root(trans, tree_root, &root->root_key);
887 BUG_ON(ret);
888
889 nr = trans->blocks_used;
890 ret = btrfs_end_transaction(trans, tree_root);
891 BUG_ON(ret);
892
893 free_extent_buffer(root->node);
894 free_extent_buffer(root->commit_root);
895 kfree(root);
896
897 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -0400898 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400899}
Yan Zheng2c47e6052009-06-27 21:07:35 -0400900#endif
Chris Mason0f7d52f2007-04-09 10:42:37 -0400901
Chris Masond352ac62008-09-29 15:18:18 -0400902/*
903 * new snapshots need to be created at a very specific time in the
904 * transaction commit. This does the actual creation
905 */
Chris Mason80b67942008-02-01 16:35:04 -0500906static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
Chris Mason3063d292008-01-08 15:46:30 -0500907 struct btrfs_fs_info *fs_info,
908 struct btrfs_pending_snapshot *pending)
909{
910 struct btrfs_key key;
Chris Mason80b67942008-02-01 16:35:04 -0500911 struct btrfs_root_item *new_root_item;
Chris Mason3063d292008-01-08 15:46:30 -0500912 struct btrfs_root *tree_root = fs_info->tree_root;
913 struct btrfs_root *root = pending->root;
Sage Weil6bdb72d2010-03-15 17:27:13 +0000914 struct btrfs_root *parent_root;
915 struct inode *parent_inode;
Josef Bacik6a912212010-11-20 09:48:00 +0000916 struct dentry *parent;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400917 struct dentry *dentry;
Chris Mason3063d292008-01-08 15:46:30 -0500918 struct extent_buffer *tmp;
Chris Mason925baed2008-06-25 16:01:30 -0400919 struct extent_buffer *old;
Chris Mason3063d292008-01-08 15:46:30 -0500920 int ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -0400921 u64 to_reserve = 0;
Sage Weil6bdb72d2010-03-15 17:27:13 +0000922 u64 index = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400923 u64 objectid;
Li Zefanb83cc962010-12-20 16:04:08 +0800924 u64 root_flags;
Chris Mason3063d292008-01-08 15:46:30 -0500925
Chris Mason80b67942008-02-01 16:35:04 -0500926 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
927 if (!new_root_item) {
Yan, Zhenga22285a2010-05-16 10:48:46 -0400928 pending->error = -ENOMEM;
Chris Mason80b67942008-02-01 16:35:04 -0500929 goto fail;
930 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400931
Chris Mason3063d292008-01-08 15:46:30 -0500932 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400933 if (ret) {
934 pending->error = ret;
Chris Mason3063d292008-01-08 15:46:30 -0500935 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400936 }
Chris Mason3063d292008-01-08 15:46:30 -0500937
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400938 btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
Yan, Zhengd68fc572010-05-16 10:49:58 -0400939 btrfs_orphan_pre_snapshot(trans, pending, &to_reserve);
940
941 if (to_reserve > 0) {
942 ret = btrfs_block_rsv_add(trans, root, &pending->block_rsv,
Josef Bacik8bb8ab22010-10-15 16:52:49 -0400943 to_reserve);
Yan, Zhengd68fc572010-05-16 10:49:58 -0400944 if (ret) {
945 pending->error = ret;
946 goto fail;
947 }
948 }
949
Chris Mason3063d292008-01-08 15:46:30 -0500950 key.objectid = objectid;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400951 key.offset = (u64)-1;
952 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Mason3063d292008-01-08 15:46:30 -0500953
Yan, Zhenga22285a2010-05-16 10:48:46 -0400954 trans->block_rsv = &pending->block_rsv;
Sage Weil6bdb72d2010-03-15 17:27:13 +0000955
Yan, Zhenga22285a2010-05-16 10:48:46 -0400956 dentry = pending->dentry;
Josef Bacik6a912212010-11-20 09:48:00 +0000957 parent = dget_parent(dentry);
958 parent_inode = parent->d_inode;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400959 parent_root = BTRFS_I(parent_inode)->root;
Sage Weil6bdb72d2010-03-15 17:27:13 +0000960 record_root_in_trans(trans, parent_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400961
Sage Weil6bdb72d2010-03-15 17:27:13 +0000962 /*
963 * insert the directory item
964 */
Sage Weil6bdb72d2010-03-15 17:27:13 +0000965 ret = btrfs_set_inode_index(parent_inode, &index);
966 BUG_ON(ret);
967 ret = btrfs_insert_dir_item(trans, parent_root,
Yan, Zhenga22285a2010-05-16 10:48:46 -0400968 dentry->d_name.name, dentry->d_name.len,
969 parent_inode->i_ino, &key,
970 BTRFS_FT_DIR, index);
Sage Weil6bdb72d2010-03-15 17:27:13 +0000971 BUG_ON(ret);
972
Yan, Zhenga22285a2010-05-16 10:48:46 -0400973 btrfs_i_size_write(parent_inode, parent_inode->i_size +
974 dentry->d_name.len * 2);
Sage Weil6bdb72d2010-03-15 17:27:13 +0000975 ret = btrfs_update_inode(trans, parent_root, parent_inode);
976 BUG_ON(ret);
977
978 record_root_in_trans(trans, root);
979 btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
980 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
Li Zefan08fe4db2011-03-28 02:01:25 +0000981 btrfs_check_and_init_root_item(new_root_item);
Sage Weil6bdb72d2010-03-15 17:27:13 +0000982
Li Zefanb83cc962010-12-20 16:04:08 +0800983 root_flags = btrfs_root_flags(new_root_item);
984 if (pending->readonly)
985 root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
986 else
987 root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
988 btrfs_set_root_flags(new_root_item, root_flags);
989
Chris Mason925baed2008-06-25 16:01:30 -0400990 old = btrfs_lock_root_node(root);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400991 btrfs_cow_block(trans, root, old, NULL, 0, &old);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400992 btrfs_set_lock_blocking(old);
Chris Mason3063d292008-01-08 15:46:30 -0500993
Chris Mason925baed2008-06-25 16:01:30 -0400994 btrfs_copy_root(trans, root, old, &tmp, objectid);
995 btrfs_tree_unlock(old);
996 free_extent_buffer(old);
Chris Mason3063d292008-01-08 15:46:30 -0500997
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400998 btrfs_set_root_node(new_root_item, tmp);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400999 /* record when the snapshot was created in key.offset */
1000 key.offset = trans->transid;
1001 ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
Chris Mason925baed2008-06-25 16:01:30 -04001002 btrfs_tree_unlock(tmp);
Chris Mason3063d292008-01-08 15:46:30 -05001003 free_extent_buffer(tmp);
Chris Mason0660b5a2008-11-17 20:37:39 -05001004 BUG_ON(ret);
1005
Yan, Zhenga22285a2010-05-16 10:48:46 -04001006 /*
1007 * insert root back/forward references
1008 */
1009 ret = btrfs_add_root_ref(trans, tree_root, objectid,
1010 parent_root->root_key.objectid,
1011 parent_inode->i_ino, index,
1012 dentry->d_name.name, dentry->d_name.len);
1013 BUG_ON(ret);
Josef Bacik6a912212010-11-20 09:48:00 +00001014 dput(parent);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001015
1016 key.offset = (u64)-1;
1017 pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
1018 BUG_ON(IS_ERR(pending->snap));
Yan, Zhengd68fc572010-05-16 10:49:58 -04001019
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001020 btrfs_reloc_post_snapshot(trans, pending);
Yan, Zhengd68fc572010-05-16 10:49:58 -04001021 btrfs_orphan_post_snapshot(trans, pending);
Chris Mason3063d292008-01-08 15:46:30 -05001022fail:
Sage Weil6bdb72d2010-03-15 17:27:13 +00001023 kfree(new_root_item);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001024 btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
1025 return 0;
Chris Mason3063d292008-01-08 15:46:30 -05001026}
1027
Chris Masond352ac62008-09-29 15:18:18 -04001028/*
1029 * create all the snapshots we've scheduled for creation
1030 */
Chris Mason80b67942008-02-01 16:35:04 -05001031static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
1032 struct btrfs_fs_info *fs_info)
Chris Mason3063d292008-01-08 15:46:30 -05001033{
1034 struct btrfs_pending_snapshot *pending;
1035 struct list_head *head = &trans->transaction->pending_snapshots;
Chris Mason3de45862008-11-17 21:02:50 -05001036 int ret;
1037
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001038 list_for_each_entry(pending, head, list) {
Chris Mason3de45862008-11-17 21:02:50 -05001039 ret = create_pending_snapshot(trans, fs_info, pending);
1040 BUG_ON(ret);
1041 }
1042 return 0;
1043}
1044
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001045static void update_super_roots(struct btrfs_root *root)
1046{
1047 struct btrfs_root_item *root_item;
1048 struct btrfs_super_block *super;
1049
1050 super = &root->fs_info->super_copy;
1051
1052 root_item = &root->fs_info->chunk_root->root_item;
1053 super->chunk_root = root_item->bytenr;
1054 super->chunk_root_generation = root_item->generation;
1055 super->chunk_root_level = root_item->level;
1056
1057 root_item = &root->fs_info->tree_root->root_item;
1058 super->root = root_item->bytenr;
1059 super->generation = root_item->generation;
1060 super->root_level = root_item->level;
Josef Bacik0af3d002010-06-21 14:48:16 -04001061 if (super->cache_generation != 0 || btrfs_test_opt(root, SPACE_CACHE))
1062 super->cache_generation = root_item->generation;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001063}
1064
Chris Masonf36f3042009-07-30 10:04:48 -04001065int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1066{
1067 int ret = 0;
1068 spin_lock(&info->new_trans_lock);
1069 if (info->running_transaction)
1070 ret = info->running_transaction->in_commit;
1071 spin_unlock(&info->new_trans_lock);
1072 return ret;
1073}
1074
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001075int btrfs_transaction_blocked(struct btrfs_fs_info *info)
1076{
1077 int ret = 0;
1078 spin_lock(&info->new_trans_lock);
1079 if (info->running_transaction)
1080 ret = info->running_transaction->blocked;
1081 spin_unlock(&info->new_trans_lock);
1082 return ret;
1083}
1084
Sage Weilbb9c12c2010-10-29 15:37:34 -04001085/*
1086 * wait for the current transaction commit to start and block subsequent
1087 * transaction joins
1088 */
1089static void wait_current_trans_commit_start(struct btrfs_root *root,
1090 struct btrfs_transaction *trans)
1091{
1092 DEFINE_WAIT(wait);
1093
1094 if (trans->in_commit)
1095 return;
1096
1097 while (1) {
1098 prepare_to_wait(&root->fs_info->transaction_blocked_wait, &wait,
1099 TASK_UNINTERRUPTIBLE);
1100 if (trans->in_commit) {
1101 finish_wait(&root->fs_info->transaction_blocked_wait,
1102 &wait);
1103 break;
1104 }
1105 mutex_unlock(&root->fs_info->trans_mutex);
1106 schedule();
1107 mutex_lock(&root->fs_info->trans_mutex);
1108 finish_wait(&root->fs_info->transaction_blocked_wait, &wait);
1109 }
1110}
1111
1112/*
1113 * wait for the current transaction to start and then become unblocked.
1114 * caller holds ref.
1115 */
1116static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1117 struct btrfs_transaction *trans)
1118{
1119 DEFINE_WAIT(wait);
1120
1121 if (trans->commit_done || (trans->in_commit && !trans->blocked))
1122 return;
1123
1124 while (1) {
1125 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
1126 TASK_UNINTERRUPTIBLE);
1127 if (trans->commit_done ||
1128 (trans->in_commit && !trans->blocked)) {
1129 finish_wait(&root->fs_info->transaction_wait,
1130 &wait);
1131 break;
1132 }
1133 mutex_unlock(&root->fs_info->trans_mutex);
1134 schedule();
1135 mutex_lock(&root->fs_info->trans_mutex);
1136 finish_wait(&root->fs_info->transaction_wait,
1137 &wait);
1138 }
1139}
1140
1141/*
1142 * commit transactions asynchronously. once btrfs_commit_transaction_async
1143 * returns, any subsequent transaction will not be allowed to join.
1144 */
1145struct btrfs_async_commit {
1146 struct btrfs_trans_handle *newtrans;
1147 struct btrfs_root *root;
1148 struct delayed_work work;
1149};
1150
1151static void do_async_commit(struct work_struct *work)
1152{
1153 struct btrfs_async_commit *ac =
1154 container_of(work, struct btrfs_async_commit, work.work);
1155
1156 btrfs_commit_transaction(ac->newtrans, ac->root);
1157 kfree(ac);
1158}
1159
1160int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1161 struct btrfs_root *root,
1162 int wait_for_unblock)
1163{
1164 struct btrfs_async_commit *ac;
1165 struct btrfs_transaction *cur_trans;
1166
1167 ac = kmalloc(sizeof(*ac), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00001168 if (!ac)
1169 return -ENOMEM;
Sage Weilbb9c12c2010-10-29 15:37:34 -04001170
1171 INIT_DELAYED_WORK(&ac->work, do_async_commit);
1172 ac->root = root;
1173 ac->newtrans = btrfs_join_transaction(root, 0);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001174 if (IS_ERR(ac->newtrans)) {
1175 int err = PTR_ERR(ac->newtrans);
1176 kfree(ac);
1177 return err;
1178 }
Sage Weilbb9c12c2010-10-29 15:37:34 -04001179
1180 /* take transaction reference */
1181 mutex_lock(&root->fs_info->trans_mutex);
1182 cur_trans = trans->transaction;
Josef Bacik13c5a932011-04-11 15:45:29 -04001183 atomic_inc(&cur_trans->use_count);
Sage Weilbb9c12c2010-10-29 15:37:34 -04001184 mutex_unlock(&root->fs_info->trans_mutex);
1185
1186 btrfs_end_transaction(trans, root);
1187 schedule_delayed_work(&ac->work, 0);
1188
1189 /* wait for transaction to start and unblock */
1190 mutex_lock(&root->fs_info->trans_mutex);
1191 if (wait_for_unblock)
1192 wait_current_trans_commit_start_and_unblock(root, cur_trans);
1193 else
1194 wait_current_trans_commit_start(root, cur_trans);
1195 put_transaction(cur_trans);
1196 mutex_unlock(&root->fs_info->trans_mutex);
1197
1198 return 0;
1199}
1200
1201/*
1202 * btrfs_transaction state sequence:
1203 * in_commit = 0, blocked = 0 (initial)
1204 * in_commit = 1, blocked = 1
1205 * blocked = 0
1206 * commit_done = 1
1207 */
Chris Mason79154b12007-03-22 15:59:16 -04001208int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1209 struct btrfs_root *root)
1210{
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001211 unsigned long joined = 0;
Chris Mason79154b12007-03-22 15:59:16 -04001212 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -04001213 struct btrfs_transaction *prev_trans = NULL;
Chris Mason79154b12007-03-22 15:59:16 -04001214 DEFINE_WAIT(wait);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001215 int ret;
Chris Mason89573b92009-03-12 20:12:45 -04001216 int should_grow = 0;
1217 unsigned long now = get_seconds();
Sage Weildccae992009-04-02 16:59:01 -04001218 int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
Chris Mason79154b12007-03-22 15:59:16 -04001219
Chris Mason5a3f23d2009-03-31 13:27:11 -04001220 btrfs_run_ordered_operations(root, 0);
1221
Chris Mason56bec292009-03-13 10:10:06 -04001222 /* make a pass through all the delayed refs we have so far
1223 * any runnings procs may add more while we are here
1224 */
1225 ret = btrfs_run_delayed_refs(trans, root, 0);
1226 BUG_ON(ret);
1227
Yan, Zhenga22285a2010-05-16 10:48:46 -04001228 btrfs_trans_release_metadata(trans, root);
1229
Chris Masonb7ec40d2009-03-12 20:12:45 -04001230 cur_trans = trans->transaction;
Chris Mason56bec292009-03-13 10:10:06 -04001231 /*
1232 * set the flushing flag so procs in this transaction have to
1233 * start sending their work down.
1234 */
Chris Masonb7ec40d2009-03-12 20:12:45 -04001235 cur_trans->delayed_refs.flushing = 1;
Chris Mason56bec292009-03-13 10:10:06 -04001236
Chris Masonc3e69d52009-03-13 10:17:05 -04001237 ret = btrfs_run_delayed_refs(trans, root, 0);
Chris Mason56bec292009-03-13 10:10:06 -04001238 BUG_ON(ret);
1239
Chris Mason79154b12007-03-22 15:59:16 -04001240 mutex_lock(&root->fs_info->trans_mutex);
Chris Masonb7ec40d2009-03-12 20:12:45 -04001241 if (cur_trans->in_commit) {
Josef Bacik13c5a932011-04-11 15:45:29 -04001242 atomic_inc(&cur_trans->use_count);
Chris Masonccd467d2007-06-28 15:57:36 -04001243 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -04001244 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -04001245
Chris Mason79154b12007-03-22 15:59:16 -04001246 ret = wait_for_commit(root, cur_trans);
1247 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001248
1249 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -04001250 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001251 mutex_unlock(&root->fs_info->trans_mutex);
1252
Chris Mason79154b12007-03-22 15:59:16 -04001253 return 0;
1254 }
Chris Mason4313b392008-01-03 09:08:48 -05001255
Chris Mason2c90e5d2007-04-02 10:50:19 -04001256 trans->transaction->in_commit = 1;
Chris Masonf9295742008-07-17 12:54:14 -04001257 trans->transaction->blocked = 1;
Sage Weilbb9c12c2010-10-29 15:37:34 -04001258 wake_up(&root->fs_info->transaction_blocked_wait);
1259
Chris Masonccd467d2007-06-28 15:57:36 -04001260 if (cur_trans->list.prev != &root->fs_info->trans_list) {
1261 prev_trans = list_entry(cur_trans->list.prev,
1262 struct btrfs_transaction, list);
1263 if (!prev_trans->commit_done) {
Josef Bacik13c5a932011-04-11 15:45:29 -04001264 atomic_inc(&prev_trans->use_count);
Chris Masonccd467d2007-06-28 15:57:36 -04001265 mutex_unlock(&root->fs_info->trans_mutex);
1266
1267 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -04001268
Chris Masonccd467d2007-06-28 15:57:36 -04001269 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001270 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -04001271 }
1272 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001273
Chris Mason89573b92009-03-12 20:12:45 -04001274 if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
1275 should_grow = 1;
1276
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001277 do {
Yan Zheng7ea394f2008-08-05 13:05:02 -04001278 int snap_pending = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001279 joined = cur_trans->num_joined;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001280 if (!list_empty(&trans->transaction->pending_snapshots))
1281 snap_pending = 1;
1282
Chris Mason2c90e5d2007-04-02 10:50:19 -04001283 WARN_ON(cur_trans != trans->transaction);
Chris Mason79154b12007-03-22 15:59:16 -04001284 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001285
Sage Weil0bdb1db2010-02-19 14:13:50 -08001286 if (flush_on_commit || snap_pending) {
Yan, Zheng24bbcf042009-11-12 09:36:34 +00001287 btrfs_start_delalloc_inodes(root, 1);
1288 ret = btrfs_wait_ordered_extents(root, 0, 1);
Sage Weilebecd3d2009-07-24 13:17:44 -04001289 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001290 }
1291
Chris Mason5a3f23d2009-03-31 13:27:11 -04001292 /*
1293 * rename don't use btrfs_join_transaction, so, once we
1294 * set the transaction to blocked above, we aren't going
1295 * to get any new ordered operations. We can safely run
1296 * it here and no for sure that nothing new will be added
1297 * to the list
1298 */
1299 btrfs_run_ordered_operations(root, 1);
1300
Chris Masoned3b3d3142010-05-25 10:12:41 -04001301 prepare_to_wait(&cur_trans->writer_wait, &wait,
1302 TASK_UNINTERRUPTIBLE);
1303
Chris Mason89573b92009-03-12 20:12:45 -04001304 smp_mb();
Josef Bacik13c5a932011-04-11 15:45:29 -04001305 if (atomic_read(&cur_trans->num_writers) > 1)
Sage Weil99d16cb2010-10-29 15:37:34 -04001306 schedule_timeout(MAX_SCHEDULE_TIMEOUT);
1307 else if (should_grow)
1308 schedule_timeout(1);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001309
Chris Mason79154b12007-03-22 15:59:16 -04001310 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001311 finish_wait(&cur_trans->writer_wait, &wait);
Josef Bacik13c5a932011-04-11 15:45:29 -04001312 } while (atomic_read(&cur_trans->num_writers) > 1 ||
Chris Mason89573b92009-03-12 20:12:45 -04001313 (should_grow && cur_trans->num_joined != joined));
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001314
Chris Mason3063d292008-01-08 15:46:30 -05001315 ret = create_pending_snapshots(trans, root->fs_info);
1316 BUG_ON(ret);
1317
Chris Mason56bec292009-03-13 10:10:06 -04001318 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1319 BUG_ON(ret);
1320
Chris Mason2c90e5d2007-04-02 10:50:19 -04001321 WARN_ON(cur_trans != trans->transaction);
Chris Masondc17ff82008-01-08 15:46:30 -05001322
Chris Masone02119d2008-09-05 16:13:11 -04001323 /* btrfs_commit_tree_roots is responsible for getting the
1324 * various roots consistent with each other. Every pointer
1325 * in the tree of tree roots has to point to the most up to date
1326 * root for every subvolume and other tree. So, we have to keep
1327 * the tree logging code from jumping in and changing any
1328 * of the trees.
1329 *
1330 * At this point in the commit, there can't be any tree-log
1331 * writers, but a little lower down we drop the trans mutex
1332 * and let new people in. By holding the tree_log_mutex
1333 * from now until after the super is written, we avoid races
1334 * with the tree-log code.
1335 */
1336 mutex_lock(&root->fs_info->tree_log_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04001337
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001338 ret = commit_fs_roots(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04001339 BUG_ON(ret);
1340
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001341 /* commit_fs_roots gets rid of all the tree log roots, it is now
Chris Masone02119d2008-09-05 16:13:11 -04001342 * safe to free the root of tree log roots
1343 */
1344 btrfs_free_log_root_tree(trans, root->fs_info);
1345
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001346 ret = commit_cowonly_roots(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -04001347 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -04001348
Yan Zheng11833d62009-09-11 16:11:19 -04001349 btrfs_prepare_extent_commit(trans, root);
1350
Chris Mason78fae272007-03-25 11:35:08 -04001351 cur_trans = root->fs_info->running_transaction;
Chris Masoncee36a02008-01-15 08:40:48 -05001352 spin_lock(&root->fs_info->new_trans_lock);
Chris Mason78fae272007-03-25 11:35:08 -04001353 root->fs_info->running_transaction = NULL;
Chris Masoncee36a02008-01-15 08:40:48 -05001354 spin_unlock(&root->fs_info->new_trans_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04001355
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001356 btrfs_set_root_node(&root->fs_info->tree_root->root_item,
1357 root->fs_info->tree_root->node);
Josef Bacik817d52f2009-07-13 21:29:25 -04001358 switch_commit_root(root->fs_info->tree_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001359
1360 btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
1361 root->fs_info->chunk_root->node);
Josef Bacik817d52f2009-07-13 21:29:25 -04001362 switch_commit_root(root->fs_info->chunk_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001363
1364 update_super_roots(root);
Chris Masone02119d2008-09-05 16:13:11 -04001365
1366 if (!root->fs_info->log_root_recovering) {
1367 btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1368 btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1369 }
1370
Chris Masona061fc82008-05-07 11:43:44 -04001371 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
1372 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -04001373
Chris Masonf9295742008-07-17 12:54:14 -04001374 trans->transaction->blocked = 0;
Chris Masonb7ec40d2009-03-12 20:12:45 -04001375
Chris Masonf9295742008-07-17 12:54:14 -04001376 wake_up(&root->fs_info->transaction_wait);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001377
Chris Mason78fae272007-03-25 11:35:08 -04001378 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -04001379 ret = btrfs_write_and_wait_transaction(trans, root);
1380 BUG_ON(ret);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001381 write_ctree_super(trans, root, 0);
Chris Mason4313b392008-01-03 09:08:48 -05001382
Chris Masone02119d2008-09-05 16:13:11 -04001383 /*
1384 * the super is written, we can safely allow the tree-loggers
1385 * to go about their business
1386 */
1387 mutex_unlock(&root->fs_info->tree_log_mutex);
1388
Yan Zheng11833d62009-09-11 16:11:19 -04001389 btrfs_finish_extent_commit(trans, root);
Chris Mason4313b392008-01-03 09:08:48 -05001390
Zheng Yan1a40e232008-09-26 10:09:34 -04001391 mutex_lock(&root->fs_info->trans_mutex);
1392
Chris Mason2c90e5d2007-04-02 10:50:19 -04001393 cur_trans->commit_done = 1;
Chris Masonb7ec40d2009-03-12 20:12:45 -04001394
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001395 root->fs_info->last_trans_committed = cur_trans->transid;
Josef Bacik817d52f2009-07-13 21:29:25 -04001396
Chris Mason2c90e5d2007-04-02 10:50:19 -04001397 wake_up(&cur_trans->commit_wait);
Chris Mason3de45862008-11-17 21:02:50 -05001398
Josef Bacik13c5a932011-04-11 15:45:29 -04001399 list_del_init(&cur_trans->list);
Chris Mason79154b12007-03-22 15:59:16 -04001400 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -04001401 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -04001402
liubo1abe9b82011-03-24 11:18:59 +00001403 trace_btrfs_transaction_commit(root);
1404
Chris Mason78fae272007-03-25 11:35:08 -04001405 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason3de45862008-11-17 21:02:50 -05001406
Josef Bacik9ed74f22009-09-11 16:12:44 -04001407 if (current->journal_info == trans)
1408 current->journal_info = NULL;
1409
Chris Mason2c90e5d2007-04-02 10:50:19 -04001410 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Yan, Zheng24bbcf042009-11-12 09:36:34 +00001411
1412 if (current != root->fs_info->transaction_kthread)
1413 btrfs_run_delayed_iputs(root);
1414
Chris Mason79154b12007-03-22 15:59:16 -04001415 return ret;
1416}
1417
Chris Masond352ac62008-09-29 15:18:18 -04001418/*
1419 * interface function to delete all the snapshots we have scheduled for deletion
1420 */
Chris Masone9d0b132007-08-10 14:06:19 -04001421int btrfs_clean_old_snapshots(struct btrfs_root *root)
1422{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001423 LIST_HEAD(list);
1424 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone9d0b132007-08-10 14:06:19 -04001425
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001426 mutex_lock(&fs_info->trans_mutex);
1427 list_splice_init(&fs_info->dead_roots, &list);
1428 mutex_unlock(&fs_info->trans_mutex);
1429
1430 while (!list_empty(&list)) {
1431 root = list_entry(list.next, struct btrfs_root, root_list);
Yan, Zheng76dda932009-09-21 16:00:26 -04001432 list_del(&root->root_list);
1433
1434 if (btrfs_header_backref_rev(root->node) <
1435 BTRFS_MIXED_BACKREF_REV)
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001436 btrfs_drop_snapshot(root, NULL, 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04001437 else
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001438 btrfs_drop_snapshot(root, NULL, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001439 }
1440 return 0;
1441}